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