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