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