New tests.
[mono.git] / mcs / mcs / modifiers.cs
1 //
2 // modifiers.cs: Modifier handling.
3 // 
4 using System;
5 using System.Reflection;
6
7 namespace Mono.CSharp
8 {
9         [Flags]
10         public enum Modifiers
11         {
12                 //
13                 // The ordering of the following 4 constants
14                 // has been carefully done.
15                 //
16                 PROTECTED = 0x0001,
17                 PUBLIC    = 0x0002,
18                 PRIVATE   = 0x0004,
19                 INTERNAL  = 0x0008,
20                 NEW       = 0x0010,
21                 ABSTRACT  = 0x0020,
22                 SEALED    = 0x0040,
23                 STATIC    = 0x0080,
24                 READONLY  = 0x0100,
25                 VIRTUAL   = 0x0200,
26                 OVERRIDE  = 0x0400,
27                 EXTERN    = 0x0800,
28                 VOLATILE  = 0x1000,
29                 UNSAFE    = 0x2000,
30                 TOP       = 0x4000,
31
32                 //
33                 // Compiler specific flags
34                 //
35                 PROPERTY_CUSTOM                 = 0x4000,
36                 OVERRIDE_UNCHECKED              = 0x8000,
37                 PARTIAL                                 = 0x20000,
38                 DEFAULT_ACCESS_MODIFER  = 0x40000,
39                 METHOD_EXTENSION                = 0x80000,
40                 COMPILER_GENERATED              = 0x100000,
41                 BACKING_FIELD                   = 0x200000,
42                 DEBUGGER_HIDDEN                 = 0x400000,
43
44                 AccessibilityMask = PUBLIC | PROTECTED | INTERNAL | PRIVATE,
45                 AllowedExplicitImplFlags = UNSAFE | EXTERN,
46         }
47
48         static class ModifiersExtensions
49         {
50                 public static string AccessibilityName (Modifiers mod)
51                 {
52                         switch (mod & Modifiers.AccessibilityMask) {
53                         case Modifiers.PUBLIC:
54                                 return "public";
55                         case Modifiers.PROTECTED:
56                                 return "protected";
57                         case Modifiers.PROTECTED | Modifiers.INTERNAL:
58                                 return "protected internal";
59                         case Modifiers.INTERNAL:
60                                 return "internal";
61                         case Modifiers.PRIVATE:
62                                 return "private";
63                         default:
64                                 throw new NotImplementedException (mod.ToString ());
65                         }
66                 }
67
68                 static public string Name (Modifiers i)
69                 {
70                         string s = "";
71                         
72                         switch (i) {
73                         case Modifiers.NEW:
74                                 s = "new"; break;
75                         case Modifiers.PUBLIC:
76                                 s = "public"; break;
77                         case Modifiers.PROTECTED:
78                                 s = "protected"; break;
79                         case Modifiers.INTERNAL:
80                                 s = "internal"; break;
81                         case Modifiers.PRIVATE:
82                                 s = "private"; break;
83                         case Modifiers.ABSTRACT:
84                                 s = "abstract"; break;
85                         case Modifiers.SEALED:
86                                 s = "sealed"; break;
87                         case Modifiers.STATIC:
88                                 s = "static"; break;
89                         case Modifiers.READONLY:
90                                 s = "readonly"; break;
91                         case Modifiers.VIRTUAL:
92                                 s = "virtual"; break;
93                         case Modifiers.OVERRIDE:
94                                 s = "override"; break;
95                         case Modifiers.EXTERN:
96                                 s = "extern"; break;
97                         case Modifiers.VOLATILE:
98                                 s = "volatile"; break;
99                         case Modifiers.UNSAFE:
100                                 s = "unsafe"; break;
101                         }
102
103                         return s;
104                 }
105
106                 //
107                 // Used by custom property accessors to check whether @modA is more restrictive than @modB
108                 //
109                 public static bool IsRestrictedModifier (Modifiers modA, Modifiers modB)
110                 {
111                         Modifiers flags = 0;
112
113                         if ((modB & Modifiers.PUBLIC) != 0) {
114                                 flags = Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
115                         } else if ((modB & Modifiers.PROTECTED) != 0) {
116                                 if ((modB & Modifiers.INTERNAL) != 0)
117                                         flags = Modifiers.PROTECTED | Modifiers.INTERNAL;
118
119                                 flags |= Modifiers.PRIVATE;
120                         } else if ((modB & Modifiers.INTERNAL) != 0)
121                                 flags = Modifiers.PRIVATE;
122
123                         return modB != modA && (modA & (~flags)) == 0;
124                 }
125
126                 public static TypeAttributes TypeAttr (Modifiers mod_flags, bool is_toplevel)
127                 {
128                         TypeAttributes t = 0;
129
130                         if (is_toplevel){
131                                 if ((mod_flags & Modifiers.PUBLIC) != 0)
132                                         t = TypeAttributes.Public;
133                                 else if ((mod_flags & Modifiers.PRIVATE) != 0)
134                                         t = TypeAttributes.NotPublic;
135                         } else {
136                                 if ((mod_flags & Modifiers.PUBLIC) != 0)
137                                         t = TypeAttributes.NestedPublic;
138                                 else if ((mod_flags & Modifiers.PRIVATE) != 0)
139                                         t = TypeAttributes.NestedPrivate;
140                                 else if ((mod_flags & (Modifiers.PROTECTED | Modifiers.INTERNAL)) == (Modifiers.PROTECTED | Modifiers.INTERNAL))
141                                         t = TypeAttributes.NestedFamORAssem;
142                                 else if ((mod_flags & Modifiers.PROTECTED) != 0)
143                                         t = TypeAttributes.NestedFamily;
144                                 else if ((mod_flags & Modifiers.INTERNAL) != 0)
145                                         t = TypeAttributes.NestedAssembly;
146                         }
147
148                         if ((mod_flags & Modifiers.SEALED) != 0)
149                                 t |= TypeAttributes.Sealed;
150                         if ((mod_flags & Modifiers.ABSTRACT) != 0)
151                                 t |= TypeAttributes.Abstract;
152
153                         return t;
154                 }
155
156                 public static FieldAttributes FieldAttr (Modifiers mod_flags)
157                 {
158                         FieldAttributes fa = 0;
159
160                         if ((mod_flags & Modifiers.PUBLIC) != 0)
161                                 fa |= FieldAttributes.Public;
162                         if ((mod_flags & Modifiers.PRIVATE) != 0)
163                                 fa |= FieldAttributes.Private;
164                         if ((mod_flags & Modifiers.PROTECTED) != 0) {
165                                 if ((mod_flags & Modifiers.INTERNAL) != 0)
166                                         fa |= FieldAttributes.FamORAssem;
167                                 else 
168                                         fa |= FieldAttributes.Family;
169                         } else {
170                                 if ((mod_flags & Modifiers.INTERNAL) != 0)
171                                         fa |= FieldAttributes.Assembly;
172                         }
173
174                         if ((mod_flags & Modifiers.STATIC) != 0)
175                                 fa |= FieldAttributes.Static;
176                         if ((mod_flags & Modifiers.READONLY) != 0)
177                                 fa |= FieldAttributes.InitOnly;
178
179                         return fa;
180                 }
181
182                 public static MethodAttributes MethodAttr (Modifiers mod_flags)
183                 {
184                         MethodAttributes ma = MethodAttributes.HideBySig;
185
186                         switch (mod_flags & Modifiers.AccessibilityMask) {
187                         case Modifiers.PUBLIC:
188                                 ma |= MethodAttributes.Public;
189                                 break;
190                         case Modifiers.PRIVATE:
191                                 ma |= MethodAttributes.Private;
192                                 break;
193                         case Modifiers.PROTECTED | Modifiers.INTERNAL:
194                                 ma |= MethodAttributes.FamORAssem;
195                                 break;
196                         case Modifiers.PROTECTED:
197                                 ma |= MethodAttributes.Family;
198                                 break;
199                         case Modifiers.INTERNAL:
200                                 ma |= MethodAttributes.Assembly;
201                                 break;
202                         default:
203                                 throw new NotImplementedException (mod_flags.ToString ());
204                         }
205
206                         if ((mod_flags & Modifiers.STATIC) != 0)
207                                 ma |= MethodAttributes.Static;
208                         if ((mod_flags & Modifiers.ABSTRACT) != 0) {
209                                 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual;
210                         }
211                         if ((mod_flags & Modifiers.SEALED) != 0)
212                                 ma |= MethodAttributes.Final;
213
214                         if ((mod_flags & Modifiers.VIRTUAL) != 0)
215                                 ma |= MethodAttributes.Virtual;
216
217                         if ((mod_flags & Modifiers.OVERRIDE) != 0) {
218                                 ma |= MethodAttributes.Virtual;
219                         } else {
220                                 if ((ma & MethodAttributes.Virtual) != 0)
221                                         ma |= MethodAttributes.NewSlot;
222                         }
223                         
224                         return ma;
225                 }
226
227                 // <summary>
228                 //   Checks the object @mod modifiers to be in @allowed.
229                 //   Returns the new mask.  Side effect: reports any
230                 //   incorrect attributes. 
231                 // </summary>
232                 public static Modifiers Check (Modifiers allowed, Modifiers mod, Modifiers def_access, Location l, Report Report)
233                 {
234                         int invalid_flags = (~(int) allowed) & ((int) mod & ((int) Modifiers.TOP - 1));
235                         int i;
236
237                         if (invalid_flags == 0){
238
239                                 if ((mod & Modifiers.UNSAFE) != 0){
240                                         RootContext.CheckUnsafeOption (l, Report);
241                                 }
242                                 
243                                 //
244                                 // If no accessibility bits provided
245                                 // then provide the defaults.
246                                 //
247                                 if ((mod & Modifiers.AccessibilityMask) == 0) {
248                                         mod |= def_access;
249                                         if (def_access != 0)
250                                                 mod |= Modifiers.DEFAULT_ACCESS_MODIFER;
251                                         return mod;
252                                 }
253
254                                 //
255                                 // Make sure that no conflicting accessibility
256                                 // bits have been set.  Protected+Internal is
257                                 // allowed, that is why they are placed on bits
258                                 // 1 and 4 (so the shift 3 basically merges them)
259                                 //
260                                 int a = (int) mod;
261                                 a &= 15;
262                                 a |= (a >> 3);
263                                 a = ((a & 2) >> 1) + (a & 5);
264                                 a = ((a & 4) >> 2) + (a & 3);
265                                 if (a > 1)
266                                         Report.Error (107, l, "More than one protection modifier specified");
267                                 
268                                 return mod;
269                         }
270
271                         for (i = 1; i <= (int) Modifiers.TOP; i <<= 1) {
272                                 if ((i & invalid_flags) == 0)
273                                         continue;
274
275                                 Error_InvalidModifier (l, Name ((Modifiers) i), Report);
276                         }
277
278                         return allowed & mod;
279                 }
280
281                 public static void Error_InvalidModifier (Location l, string name, Report Report)
282                 {
283                         Report.Error (106, l, "The modifier `{0}' is not valid for this item", name);
284                 }
285         }
286 }