**** Merged r46944 and r47112 from MCS ****
[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 string GetDescription (MethodAttributes ma)
82                 {
83                         if ((ma & MethodAttributes.Assembly) != 0)
84                                 return "internal";
85
86                         if ((ma & MethodAttributes.Family) != 0)
87                                 return "protected";
88
89                         if ((ma & MethodAttributes.Public) != 0)
90                                 return "public";
91
92                         if ((ma & MethodAttributes.FamANDAssem) != 0)
93                                 return "protected internal";
94
95                         if ((ma & MethodAttributes.Private) != 0)
96                                 return "private";
97
98                         throw new NotImplementedException (ma.ToString ());
99                 }
100
101                 public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
102                 {
103                         TypeAttributes t = 0;
104
105                         if (is_toplevel){
106                                 if ((mod_flags & PUBLIC) != 0)
107                                         t |= TypeAttributes.Public;
108                                 if ((mod_flags & PRIVATE) != 0)
109                                         t |= TypeAttributes.NotPublic;
110                         } else {
111                                 if ((mod_flags & PUBLIC) != 0)
112                                         t |= TypeAttributes.NestedPublic;
113                                 if ((mod_flags & PRIVATE) != 0)
114                                         t |= TypeAttributes.NestedPrivate;
115                                 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
116                                         t |= TypeAttributes.NestedFamORAssem;
117                                 if ((mod_flags & PROTECTED) != 0)
118                                         t |= TypeAttributes.NestedFamily;
119                                 if ((mod_flags & INTERNAL) != 0)
120                                         t |= TypeAttributes.NestedAssembly;
121                         }
122                         
123                         if ((mod_flags & SEALED) != 0)
124                                 t |= TypeAttributes.Sealed;
125                         if ((mod_flags & ABSTRACT) != 0)
126                                 t |= TypeAttributes.Abstract;
127
128                         return t;
129                 }
130                 
131                 public static TypeAttributes TypeAttr (int mod_flags, TypeContainer caller)
132                 {
133                         TypeAttributes t = TypeAttr (mod_flags, caller.IsTopLevel);
134
135                         // If we do not have static constructors, static methods
136                         // can be invoked without initializing the type.
137                         if (!caller.UserDefinedStaticConstructor &&
138                             (caller.Kind != Kind.Interface))
139                                 t |= TypeAttributes.BeforeFieldInit;
140                                 
141                         return t;
142                 }
143
144                 public static FieldAttributes FieldAttr (int mod_flags)
145                 {
146                         FieldAttributes fa = 0;
147
148                         if ((mod_flags & PUBLIC) != 0)
149                                 fa |= FieldAttributes.Public;
150                         if ((mod_flags & PRIVATE) != 0)
151                                 fa |= FieldAttributes.Private;
152                         if ((mod_flags & PROTECTED) != 0){
153                                 if ((mod_flags & INTERNAL) != 0)
154                                         fa |= FieldAttributes.FamORAssem;
155                                 else 
156                                         fa |= FieldAttributes.Family;
157                         } else {
158                                 if ((mod_flags & INTERNAL) != 0)
159                                         fa |= FieldAttributes.Assembly;
160                         }
161                         
162                         if ((mod_flags & STATIC) != 0)
163                                 fa |= FieldAttributes.Static;
164                         if ((mod_flags & READONLY) != 0)
165                                 fa |= FieldAttributes.InitOnly;
166
167                         return fa;
168                 }
169
170                 public static MethodAttributes MethodAttr (int mod_flags)
171                 {
172                         MethodAttributes ma = MethodAttributes.HideBySig;
173
174                         if ((mod_flags & PUBLIC) != 0)
175                                 ma |= MethodAttributes.Public;
176                         if ((mod_flags & PRIVATE) != 0)
177                                 ma |= MethodAttributes.Private;
178                         if ((mod_flags & PROTECTED) != 0){
179                                 if ((mod_flags & INTERNAL) != 0)
180                                         ma |= MethodAttributes.FamORAssem;
181                                 else 
182                                         ma |= MethodAttributes.Family;
183                         } else {
184                                 if ((mod_flags & INTERNAL) != 0)
185                                         ma |= MethodAttributes.Assembly;
186                         }
187
188                         if ((mod_flags & STATIC) != 0)
189                                 ma |= MethodAttributes.Static;
190                         if ((mod_flags & ABSTRACT) != 0){
191                                 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual;
192                         }
193                         if ((mod_flags & SEALED) != 0)
194                                 ma |= MethodAttributes.Final;
195
196                         if ((mod_flags & VIRTUAL) != 0)
197                                 ma |= MethodAttributes.Virtual;
198
199                         if ((mod_flags & OVERRIDE) != 0)
200                                 ma |= MethodAttributes.Virtual;
201                         else {
202                                 if ((ma & MethodAttributes.Virtual) != 0)
203                                         ma |= MethodAttributes.NewSlot;
204                         }
205                         
206                         return ma;
207                 }
208
209                 // <summary>
210                 //   Checks the object @mod modifiers to be in @allowed.
211                 //   Returns the new mask.  Side effect: reports any
212                 //   incorrect attributes. 
213                 // </summary>
214                 public static int Check (int allowed, int mod, int def_access, Location l)
215                 {
216                         int invalid_flags  = (~allowed) & mod;
217                         int i;
218
219                         if (invalid_flags == 0){
220                                 int a = mod;
221
222                                 if ((mod & Modifiers.UNSAFE) != 0){
223                                         RootContext.CheckUnsafeOption (l);
224                                 }
225                                 
226                                 //
227                                 // If no accessibility bits provided
228                                 // then provide the defaults.
229                                 //
230                                 if ((mod & Accessibility) == 0){
231                                         mod |= def_access;
232                                         return mod;
233                                 }
234
235                                 //
236                                 // Make sure that no conflicting accessibility
237                                 // bits have been set.  Protected+Internal is
238                                 // allowed, that is why they are placed on bits
239                                 // 1 and 4 (so the shift 3 basically merges them)
240                                 //
241                                 a &= 15;
242                                 a |= (a >> 3);
243                                 a = ((a & 2) >> 1) + (a & 5);
244                                 a = ((a & 4) >> 2) + (a & 3);
245                                 if (a > 1)
246                                         Report.Error (107, l, "More than one protection modifier specified");
247                                 
248                                 return mod;
249                         }
250                         
251                         for (i = 1; i <= TOP; i <<= 1){
252                                 if ((i & invalid_flags) == 0)
253                                         continue;
254
255                                 Error_InvalidModifier (l, Name (i));
256                         }
257
258                         return allowed & mod;
259                 }
260
261                 public static void Error_InvalidModifier (Location l, string name)
262                 {
263                         Report.Error (106, l, "The modifier `" + name + "' is not valid for this item");
264                 }
265         }
266 }