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