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