2 // modifiers.cs: Modifier handling.
5 using System.Reflection;
7 namespace Mono.CSharp {
8 public class Modifiers {
11 // The ordering of the following 4 constants
12 // has been carefully done.
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 public const int OVERLOADS = 0x08000;
30 public const int DEFAULT = 0x10000;
31 public const int NONVIRTUAL= 0x20000;
32 private const int TOP = 0x20000;
34 public const int Accessibility =
35 PUBLIC | PROTECTED | INTERNAL | PRIVATE;
37 static public string Name (int i)
44 case Modifiers.PUBLIC:
46 case Modifiers.PROTECTED:
47 s = "protected"; break;
48 case Modifiers.INTERNAL:
49 s = "internal"; break;
50 case Modifiers.PRIVATE:
52 case Modifiers.ABSTRACT:
53 s = "abstract"; break;
54 case Modifiers.SEALED:
56 case Modifiers.STATIC:
58 case Modifiers.READONLY:
59 s = "readonly"; break;
60 case Modifiers.VIRTUAL:
62 case Modifiers.OVERRIDE:
63 s = "override"; break;
64 case Modifiers.EXTERN:
66 case Modifiers.VOLATILE:
67 s = "volatile"; break;
73 public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
78 if ((mod_flags & PUBLIC) != 0)
79 t |= TypeAttributes.Public;
80 if ((mod_flags & PRIVATE) != 0)
81 t |= TypeAttributes.NotPublic;
83 if ((mod_flags & PUBLIC) != 0)
84 t |= TypeAttributes.NestedPublic;
85 if ((mod_flags & PRIVATE) != 0)
86 t |= TypeAttributes.NestedPrivate;
87 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
88 t |= TypeAttributes.NestedFamORAssem;
89 if ((mod_flags & PROTECTED) != 0)
90 t |= TypeAttributes.NestedFamily;
91 if ((mod_flags & INTERNAL) != 0)
92 t |= TypeAttributes.NestedAssembly;
95 if ((mod_flags & SEALED) != 0)
96 t |= TypeAttributes.Sealed;
97 if ((mod_flags & ABSTRACT) != 0)
98 t |= TypeAttributes.Abstract;
103 public static TypeAttributes TypeAttr (int mod_flags, TypeContainer caller)
105 TypeAttributes t = TypeAttr (mod_flags, caller.IsTopLevel);
107 // If we do not have static constructors, static methods
108 // can be invoked without initializing the type.
109 if (!caller.HaveStaticConstructor)
110 t |= TypeAttributes.BeforeFieldInit;
115 public static FieldAttributes FieldAttr (int mod_flags)
117 FieldAttributes fa = 0;
119 if ((mod_flags & PUBLIC) != 0)
120 fa |= FieldAttributes.Public;
121 if ((mod_flags & PRIVATE) != 0)
122 fa |= FieldAttributes.Private;
123 if ((mod_flags & PROTECTED) != 0){
124 if ((mod_flags & INTERNAL) != 0)
125 fa |= FieldAttributes.FamORAssem;
127 fa |= FieldAttributes.Family;
129 if ((mod_flags & INTERNAL) != 0)
130 fa |= FieldAttributes.Assembly;
133 if ((mod_flags & STATIC) != 0)
134 fa |= FieldAttributes.Static;
135 if ((mod_flags & READONLY) != 0)
136 fa |= FieldAttributes.InitOnly;
141 public static MethodAttributes MethodAttr (int mod_flags)
143 MethodAttributes ma = 0;
145 if ((mod_flags & PUBLIC) != 0)
146 ma |= MethodAttributes.Public;
147 if ((mod_flags & PRIVATE) != 0)
148 ma |= MethodAttributes.Private;
149 if ((mod_flags & PROTECTED) != 0){
150 if ((mod_flags & INTERNAL) != 0)
151 ma |= MethodAttributes.FamORAssem;
153 ma |= MethodAttributes.Family;
155 if ((mod_flags & INTERNAL) != 0)
156 ma |= MethodAttributes.Assembly;
159 if ((mod_flags & STATIC) != 0)
160 ma |= MethodAttributes.Static;
161 if ((mod_flags & ABSTRACT) != 0){
162 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual |
163 MethodAttributes.HideBySig;
165 if ((mod_flags & SEALED) != 0)
166 ma |= MethodAttributes.Final;
168 if ((mod_flags & VIRTUAL) != 0)
169 ma |= MethodAttributes.Virtual;
171 if ((mod_flags & OVERRIDE) != 0)
172 ma |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
174 if ((ma & MethodAttributes.Virtual) != 0)
175 ma |= MethodAttributes.NewSlot;
178 if ((mod_flags & NEW) != 0)
179 ma |= MethodAttributes.HideBySig;
185 // Checks the object @mod modifiers to be in @allowed.
186 // Returns the new mask. Side effect: reports any
187 // incorrect attributes.
189 public static int Check (int allowed, int mod, int def_access, Location l)
191 int invalid_flags = (~allowed) & mod;
194 if (invalid_flags == 0){
197 if ((mod & Modifiers.UNSAFE) != 0){
198 if (!RootContext.Unsafe){
199 Report.Error (227, l,
200 "Unsafe code requires the --unsafe command " +
201 "line option to be specified");
206 // If no accessibility bits provided
207 // then provide the defaults.
209 if ((mod & Accessibility) == 0){
215 // Make sure that no conflicting accessibility
216 // bits have been set. Protected+Internal is
217 // allowed, that is why they are placed on bits
218 // 1 and 4 (so the shift 3 basically merges them)
222 a = ((a & 2) >> 1) + (a & 5);
223 a = ((a & 4) >> 2) + (a & 3);
225 Report.Error (107, l, "More than one protection modifier specified");
230 for (i = 1; i < TOP; i <<= 1){
231 if ((i & invalid_flags) == 0)
234 Error_InvalidModifier (l, Name (i));
237 return allowed & mod;
240 public static void Error_InvalidModifier (Location l, string name)
242 Report.Error (106, l, "the modifier " + name + " is not valid for this item");