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 = 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;
30 public const int PROPERTY_CUSTOM = 0x4000; // Custom property modifier
33 // We use this internally to flag that the method contains an iterator
35 public const int METHOD_YIELDS = 0x8000;
36 public const int METHOD_GENERIC = 0x10000;
38 public const int Accessibility =
39 PUBLIC | PROTECTED | INTERNAL | PRIVATE;
40 public const int AllowedExplicitImplFlags =
43 static public string Name (int i)
50 case Modifiers.PUBLIC:
52 case Modifiers.PROTECTED:
53 s = "protected"; break;
54 case Modifiers.INTERNAL:
55 s = "internal"; break;
56 case Modifiers.PRIVATE:
58 case Modifiers.ABSTRACT:
59 s = "abstract"; break;
60 case Modifiers.SEALED:
62 case Modifiers.STATIC:
64 case Modifiers.READONLY:
65 s = "readonly"; break;
66 case Modifiers.VIRTUAL:
68 case Modifiers.OVERRIDE:
69 s = "override"; break;
70 case Modifiers.EXTERN:
72 case Modifiers.VOLATILE:
73 s = "volatile"; break;
74 case Modifiers.UNSAFE:
81 public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
86 if ((mod_flags & PUBLIC) != 0)
87 t |= TypeAttributes.Public;
88 if ((mod_flags & PRIVATE) != 0)
89 t |= TypeAttributes.NotPublic;
91 if ((mod_flags & PUBLIC) != 0)
92 t |= TypeAttributes.NestedPublic;
93 if ((mod_flags & PRIVATE) != 0)
94 t |= TypeAttributes.NestedPrivate;
95 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
96 t |= TypeAttributes.NestedFamORAssem;
97 if ((mod_flags & PROTECTED) != 0)
98 t |= TypeAttributes.NestedFamily;
99 if ((mod_flags & INTERNAL) != 0)
100 t |= TypeAttributes.NestedAssembly;
103 if ((mod_flags & SEALED) != 0)
104 t |= TypeAttributes.Sealed;
105 if ((mod_flags & ABSTRACT) != 0)
106 t |= TypeAttributes.Abstract;
111 public static TypeAttributes TypeAttr (int mod_flags, TypeContainer caller)
113 TypeAttributes t = TypeAttr (mod_flags, caller.IsTopLevel);
115 // If we do not have static constructors, static methods
116 // can be invoked without initializing the type.
117 if (!caller.UserDefinedStaticConstructor &&
118 (caller.Kind != Kind.Interface))
119 t |= TypeAttributes.BeforeFieldInit;
124 public static FieldAttributes FieldAttr (int mod_flags)
126 FieldAttributes fa = 0;
128 if ((mod_flags & PUBLIC) != 0)
129 fa |= FieldAttributes.Public;
130 if ((mod_flags & PRIVATE) != 0)
131 fa |= FieldAttributes.Private;
132 if ((mod_flags & PROTECTED) != 0){
133 if ((mod_flags & INTERNAL) != 0)
134 fa |= FieldAttributes.FamORAssem;
136 fa |= FieldAttributes.Family;
138 if ((mod_flags & INTERNAL) != 0)
139 fa |= FieldAttributes.Assembly;
142 if ((mod_flags & STATIC) != 0)
143 fa |= FieldAttributes.Static;
144 if ((mod_flags & READONLY) != 0)
145 fa |= FieldAttributes.InitOnly;
150 public static MethodAttributes MethodAttr (int mod_flags)
152 MethodAttributes ma = MethodAttributes.HideBySig;
154 if ((mod_flags & PUBLIC) != 0)
155 ma |= MethodAttributes.Public;
156 if ((mod_flags & PRIVATE) != 0)
157 ma |= MethodAttributes.Private;
158 if ((mod_flags & PROTECTED) != 0){
159 if ((mod_flags & INTERNAL) != 0)
160 ma |= MethodAttributes.FamORAssem;
162 ma |= MethodAttributes.Family;
164 if ((mod_flags & INTERNAL) != 0)
165 ma |= MethodAttributes.Assembly;
168 if ((mod_flags & STATIC) != 0)
169 ma |= MethodAttributes.Static;
170 if ((mod_flags & ABSTRACT) != 0){
171 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual;
173 if ((mod_flags & SEALED) != 0)
174 ma |= MethodAttributes.Final;
176 if ((mod_flags & VIRTUAL) != 0)
177 ma |= MethodAttributes.Virtual;
179 if ((mod_flags & OVERRIDE) != 0)
180 ma |= MethodAttributes.Virtual;
182 if ((ma & MethodAttributes.Virtual) != 0)
183 ma |= MethodAttributes.NewSlot;
190 // Checks the object @mod modifiers to be in @allowed.
191 // Returns the new mask. Side effect: reports any
192 // incorrect attributes.
194 public static int Check (int allowed, int mod, int def_access, Location l)
196 int invalid_flags = (~allowed) & mod;
199 if (invalid_flags == 0){
202 if ((mod & Modifiers.UNSAFE) != 0){
203 if (!RootContext.Unsafe){
204 Report.Error (227, l,
205 "Unsafe code requires the -unsafe command " +
206 "line option to be specified");
211 // If no accessibility bits provided
212 // then provide the defaults.
214 if ((mod & Accessibility) == 0){
220 // Make sure that no conflicting accessibility
221 // bits have been set. Protected+Internal is
222 // allowed, that is why they are placed on bits
223 // 1 and 4 (so the shift 3 basically merges them)
227 a = ((a & 2) >> 1) + (a & 5);
228 a = ((a & 4) >> 2) + (a & 3);
230 Report.Error (107, l, "More than one protection modifier specified");
235 for (i = 1; i <= TOP; i <<= 1){
236 if ((i & invalid_flags) == 0)
239 Error_InvalidModifier (l, Name (i));
242 return allowed & mod;
245 public static void Error_InvalidModifier (Location l, string name)
247 Report.Error (106, l, "the modifier " + name + " is not valid for this item");