* Makefile (centum_tests): Add Mono.Security and
[mono.git] / mcs / gmcs / modifiers.cs
1 //
2 // modifiers.cs: Modifier handling.
3 // 
4 using System;
5 using System.Reflection;
6
7 namespace Mono.CSharp {
8         public class Modifiers {
9
10                 //
11                 // The ordering of the following 4 constants
12                 // has been carefully done.
13                 //
14                 public const int PROTECTED = 0x0001;
15                 public const int PUBLIC    = 0x0002;
16                 public const int PRIVATE   = 0x0004;
17                 public const int INTERNAL  = 0x0008;
18                 public const int NEW       = 0x0010;
19                 public const int ABSTRACT  = 0x0020;
20                 public const int SEALED    = 0x0040;
21                 public const int STATIC    = 0x0080;
22                 public const int READONLY  = 0x0100;
23                 public const int VIRTUAL   = 0x0200;
24                 public const int OVERRIDE  = 0x0400;
25                 public const int EXTERN    = 0x0800;
26                 public const int VOLATILE  = 0x1000;
27                 public const int UNSAFE    = 0x2000;
28                 public const int TOP       = 0x2000;
29
30                 public const int PROPERTY_CUSTOM = 0x4000; // Custom property modifier
31
32                 //
33                 // We use this internally to flag that the method contains an iterator
34                 //
35                 public const int METHOD_YIELDS = 0x8000;
36                 public const int METHOD_GENERIC = 0x10000;
37
38                 public const int Accessibility =
39                         PUBLIC | PROTECTED | INTERNAL | PRIVATE;
40                 public const int AllowedExplicitImplFlags =
41                         UNSAFE | EXTERN;
42                 
43                 static public string Name (int i)
44                 {
45                         string s = "";
46                         
47                         switch (i) {
48                         case Modifiers.NEW:
49                                 s = "new"; break;
50                         case Modifiers.PUBLIC:
51                                 s = "public"; break;
52                         case Modifiers.PROTECTED:
53                                 s = "protected"; break;
54                         case Modifiers.INTERNAL:
55                                 s = "internal"; break;
56                         case Modifiers.PRIVATE:
57                                 s = "private"; break;
58                         case Modifiers.ABSTRACT:
59                                 s = "abstract"; break;
60                         case Modifiers.SEALED:
61                                 s = "sealed"; break;
62                         case Modifiers.STATIC:
63                                 s = "static"; break;
64                         case Modifiers.READONLY:
65                                 s = "readonly"; break;
66                         case Modifiers.VIRTUAL:
67                                 s = "virtual"; break;
68                         case Modifiers.OVERRIDE:
69                                 s = "override"; break;
70                         case Modifiers.EXTERN:
71                                 s = "extern"; break;
72                         case Modifiers.VOLATILE:
73                                 s = "volatile"; break;
74                         case Modifiers.UNSAFE:
75                                 s = "unsafe"; break;
76                         }
77
78                         return s;
79                 }
80
81                 public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
82                 {
83                         TypeAttributes t = 0;
84
85                         if (is_toplevel){
86                                 if ((mod_flags & PUBLIC) != 0)
87                                         t |= TypeAttributes.Public;
88                                 if ((mod_flags & PRIVATE) != 0)
89                                         t |= TypeAttributes.NotPublic;
90                         } else {
91                                 if ((mod_flags & PUBLIC) != 0)
92                                         t |= TypeAttributes.NestedPublic;
93                                 if ((mod_flags & PRIVATE) != 0)
94                                         t |= TypeAttributes.NestedPrivate;
95                                 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
96                                         t |= TypeAttributes.NestedFamORAssem;
97                                 if ((mod_flags & PROTECTED) != 0)
98                                         t |= TypeAttributes.NestedFamily;
99                                 if ((mod_flags & INTERNAL) != 0)
100                                         t |= TypeAttributes.NestedAssembly;
101                         }
102                         
103                         if ((mod_flags & SEALED) != 0)
104                                 t |= TypeAttributes.Sealed;
105                         if ((mod_flags & ABSTRACT) != 0)
106                                 t |= TypeAttributes.Abstract;
107
108                         return t;
109                 }
110                 
111                 public static TypeAttributes TypeAttr (int mod_flags, TypeContainer caller)
112                 {
113                         TypeAttributes t = TypeAttr (mod_flags, caller.IsTopLevel);
114
115                         // If we do not have static constructors, static methods
116                         // can be invoked without initializing the type.
117                         if (!caller.UserDefinedStaticConstructor &&
118                             (caller.Kind != Kind.Interface))
119                                 t |= TypeAttributes.BeforeFieldInit;
120                                 
121                         return t;
122                 }
123
124                 public static FieldAttributes FieldAttr (int mod_flags)
125                 {
126                         FieldAttributes fa = 0;
127
128                         if ((mod_flags & PUBLIC) != 0)
129                                 fa |= FieldAttributes.Public;
130                         if ((mod_flags & PRIVATE) != 0)
131                                 fa |= FieldAttributes.Private;
132                         if ((mod_flags & PROTECTED) != 0){
133                                 if ((mod_flags & INTERNAL) != 0)
134                                         fa |= FieldAttributes.FamORAssem;
135                                 else 
136                                         fa |= FieldAttributes.Family;
137                         } else {
138                                 if ((mod_flags & INTERNAL) != 0)
139                                         fa |= FieldAttributes.Assembly;
140                         }
141                         
142                         if ((mod_flags & STATIC) != 0)
143                                 fa |= FieldAttributes.Static;
144                         if ((mod_flags & READONLY) != 0)
145                                 fa |= FieldAttributes.InitOnly;
146
147                         return fa;
148                 }
149
150                 public static MethodAttributes MethodAttr (int mod_flags)
151                 {
152                         MethodAttributes ma = MethodAttributes.HideBySig;
153
154                         if ((mod_flags & PUBLIC) != 0)
155                                 ma |= MethodAttributes.Public;
156                         if ((mod_flags & PRIVATE) != 0)
157                                 ma |= MethodAttributes.Private;
158                         if ((mod_flags & PROTECTED) != 0){
159                                 if ((mod_flags & INTERNAL) != 0)
160                                         ma |= MethodAttributes.FamORAssem;
161                                 else 
162                                         ma |= MethodAttributes.Family;
163                         } else {
164                                 if ((mod_flags & INTERNAL) != 0)
165                                         ma |= MethodAttributes.Assembly;
166                         }
167
168                         if ((mod_flags & STATIC) != 0)
169                                 ma |= MethodAttributes.Static;
170                         if ((mod_flags & ABSTRACT) != 0){
171                                 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual;
172                         }
173                         if ((mod_flags & SEALED) != 0)
174                                 ma |= MethodAttributes.Final;
175
176                         if ((mod_flags & VIRTUAL) != 0)
177                                 ma |= MethodAttributes.Virtual;
178
179                         if ((mod_flags & OVERRIDE) != 0)
180                                 ma |= MethodAttributes.Virtual;
181                         else {
182                                 if ((ma & MethodAttributes.Virtual) != 0)
183                                         ma |= MethodAttributes.NewSlot;
184                         }
185                         
186                         return ma;
187                 }
188
189                 // <summary>
190                 //   Checks the object @mod modifiers to be in @allowed.
191                 //   Returns the new mask.  Side effect: reports any
192                 //   incorrect attributes. 
193                 // </summary>
194                 public static int Check (int allowed, int mod, int def_access, Location l)
195                 {
196                         int invalid_flags  = (~allowed) & mod;
197                         int i;
198
199                         if (invalid_flags == 0){
200                                 int a = mod;
201
202                                 if ((mod & Modifiers.UNSAFE) != 0){
203                                         if (!RootContext.Unsafe){
204                                                 Report.Error (227, l,
205                                                               "Unsafe code requires the -unsafe command " +
206                                                               "line option to be specified");
207                                         }
208                                 }
209                                 
210                                 //
211                                 // If no accessibility bits provided
212                                 // then provide the defaults.
213                                 //
214                                 if ((mod & Accessibility) == 0){
215                                         mod |= def_access;
216                                         return mod;
217                                 }
218
219                                 //
220                                 // Make sure that no conflicting accessibility
221                                 // bits have been set.  Protected+Internal is
222                                 // allowed, that is why they are placed on bits
223                                 // 1 and 4 (so the shift 3 basically merges them)
224                                 //
225                                 a &= 15;
226                                 a |= (a >> 3);
227                                 a = ((a & 2) >> 1) + (a & 5);
228                                 a = ((a & 4) >> 2) + (a & 3);
229                                 if (a > 1)
230                                         Report.Error (107, l, "More than one protection modifier specified");
231                                 
232                                 return mod;
233                         }
234                         
235                         for (i = 1; i <= TOP; i <<= 1){
236                                 if ((i & invalid_flags) == 0)
237                                         continue;
238
239                                 Error_InvalidModifier (l, Name (i));
240                         }
241
242                         return allowed & mod;
243                 }
244
245                 public static void Error_InvalidModifier (Location l, string name)
246                 {
247                         Report.Error (106, l, "the modifier " + name + " is not valid for this item");
248                 }
249         }
250 }