2001-10-04 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / modifiers.cs
1 //
2 // modifiers.cs: Modifier handling.
3 // 
4 using System;
5 using System.Reflection;
6
7 namespace CIR {
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 TOP       = 0x0800;
27
28                 public const int Accessibility =
29                         PUBLIC | PROTECTED | INTERNAL | PRIVATE;
30                 
31                 static public string Name (int i)
32                 {
33                         string s = "";
34                         
35                         switch (i) {
36                         case Modifiers.NEW:
37                                 s = "new"; break;
38                         case Modifiers.PUBLIC:
39                                 s = "public"; break;
40                         case Modifiers.PROTECTED:
41                                 s = "protected"; break;
42                         case Modifiers.INTERNAL:
43                                 s = "internal"; break;
44                         case Modifiers.PRIVATE:
45                                 s = "private"; break;
46                         case Modifiers.ABSTRACT:
47                                 s = "abstract"; break;
48                         case Modifiers.SEALED:
49                                 s = "sealed"; break;
50                         case Modifiers.STATIC:
51                                 s = "static"; break;
52                         case Modifiers.READONLY:
53                                 s = "readonly"; break;
54                         case Modifiers.VIRTUAL:
55                                 s = "virtual"; break;
56                         case Modifiers.OVERRIDE:
57                                 s = "override"; break;
58                         case Modifiers.EXTERN:
59                                 s = "extern"; break;
60                         }
61
62                         return s;
63                 }
64
65                 public static TypeAttributes TypeAttr (int mod_flags, TypeContainer caller)
66                 {
67                         TypeAttributes t = 0;
68                         bool top_level = caller.IsTopLevel;
69                         
70                         if (top_level){
71                                 if ((mod_flags & PUBLIC) != 0)
72                                         t |= TypeAttributes.Public;
73                                 if ((mod_flags & PRIVATE) != 0)
74                                         t |= TypeAttributes.NotPublic;
75                         } else {
76                                 if ((mod_flags & PUBLIC) != 0)
77                                         t |= TypeAttributes.NestedPublic;
78                                 if ((mod_flags & PRIVATE) != 0)
79                                         t |= TypeAttributes.NestedPrivate;
80                                 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
81                                         t |= TypeAttributes.NestedFamORAssem;
82                                 if ((mod_flags & PROTECTED) != 0)
83                                         t |= TypeAttributes.NestedFamily;
84                                 if ((mod_flags & INTERNAL) != 0)
85                                         t |= TypeAttributes.NestedAssembly;
86                         }
87                         
88                         if ((mod_flags & SEALED) != 0)
89                                 t |= TypeAttributes.Sealed;
90                         if ((mod_flags & ABSTRACT) != 0)
91                                 t |= TypeAttributes.Abstract;
92
93                         // If we do not have static constructors, static methods
94                         // can be invoked without initializing the type.
95                         if (!caller.HaveStaticConstructor)
96                                 t |= TypeAttributes.BeforeFieldInit;
97                                 
98                         return t;
99                 }
100
101                 public static FieldAttributes FieldAttr (int mod_flags)
102                 {
103                         FieldAttributes fa = 0;
104
105                         if ((mod_flags & PUBLIC) != 0)
106                                 fa |= FieldAttributes.Public;
107                         if ((mod_flags & PRIVATE) != 0)
108                                 fa |= FieldAttributes.Private;
109                         if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
110                                 fa |= FieldAttributes.FamORAssem;
111                         if ((mod_flags & PROTECTED) != 0)
112                                 fa |= FieldAttributes.Family;
113                         if ((mod_flags & INTERNAL) != 0)
114                                 fa |= FieldAttributes.Assembly;
115                         
116                         if ((mod_flags & STATIC) != 0)
117                                 fa |= FieldAttributes.Static;
118                         if ((mod_flags & READONLY) != 0)
119                                 fa |= FieldAttributes.InitOnly;
120
121                         return fa;
122                 }
123
124                 public static MethodAttributes MethodAttr (int mod_flags)
125                 {
126                         MethodAttributes ma = 0;
127
128                         if ((mod_flags & PUBLIC) != 0)
129                                 ma |= MethodAttributes.Public;
130                         if ((mod_flags & PRIVATE) != 0)
131                                 ma |= MethodAttributes.Private;
132                         if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
133                                 ma |= MethodAttributes.FamORAssem;
134                         if ((mod_flags & PROTECTED) != 0)
135                                 ma |= MethodAttributes.Family;
136                         if ((mod_flags & INTERNAL) != 0)
137                                 ma |= MethodAttributes.Assembly;
138                         
139
140                         if ((mod_flags & STATIC) != 0)
141                                 ma |= MethodAttributes.Static;
142                         if ((mod_flags & ABSTRACT) != 0){
143                                 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual |
144                                         MethodAttributes.NewSlot;
145                         }
146                         if ((mod_flags & SEALED) != 0)
147                                 ma |= MethodAttributes.Final;
148                         if ((mod_flags & VIRTUAL) != 0)
149                                 ma |= MethodAttributes.Virtual;
150
151                         if ((mod_flags & OVERRIDE) != 0)
152                                 ma |= MethodAttributes.Virtual;
153                         
154                         if ((mod_flags & NEW) != 0)
155                                 ma |= MethodAttributes.NewSlot;
156                         
157                         return ma;
158                 }
159
160                 // <summary>
161                 //   Checks the object @mod modifiers to be in @allowed.
162                 //   Returns the new mask.  Side effect: reports any
163                 //   incorrect attributes. 
164                 // </summary>
165                 public static int Check (int allowed, int mod, int def_access)
166                 {
167                         int invalid_flags  = (~allowed) & mod;
168                         int i;
169
170                         if (invalid_flags == 0){
171                                 int a = mod;
172
173                                 //
174                                 // If no accessibility bits provided
175                                 // then provide the defaults.
176                                 //
177                                 if ((mod & Accessibility) == 0){
178                                         mod |= def_access;
179                                         return mod;
180                                 }
181
182                                 //
183                                 // Make sure that no conflicting accessibility
184                                 // bits have been set.  Protected+Internal is
185                                 // allowed, that is why they are placed on bits
186                                 // 1 and 4 (so the shift 3 basically merges them)
187                                 //
188                                 a &= 15;
189                                 a |= (a >> 3);
190                                 a = ((a & 2) >> 1) + (a & 5);
191                                 a = ((a & 4) >> 2) + (a & 3);
192                                 if (a > 1)
193                                         CSharpParser.error (107, "More than one protection modifier specified");
194                                 
195                                 return mod;
196                         }
197                         
198                         for (i = 1; i < TOP; i <<= 1){
199                                 if ((i & invalid_flags) == 0)
200                                         continue;
201
202                                 CSharpParser.error (106, "the modifier `" + Name (i) + "' is not valid for this item");
203                         }
204
205                         return allowed & mod;
206                 }
207         }
208 }