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