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