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