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