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