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