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