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;
31 // We use this internally to flag that the method contains an iterator
33 public const int METHOD_YIELDS = 0x8000;
35 public const int Accessibility =
36 PUBLIC | PROTECTED | INTERNAL | PRIVATE;
38 static public string Name (int i)
45 case Modifiers.PUBLIC:
47 case Modifiers.PROTECTED:
48 s = "protected"; break;
49 case Modifiers.INTERNAL:
50 s = "internal"; break;
51 case Modifiers.PRIVATE:
53 case Modifiers.ABSTRACT:
54 s = "abstract"; break;
55 case Modifiers.SEALED:
57 case Modifiers.STATIC:
59 case Modifiers.READONLY:
60 s = "readonly"; break;
61 case Modifiers.VIRTUAL:
63 case Modifiers.OVERRIDE:
64 s = "override"; break;
65 case Modifiers.EXTERN:
67 case Modifiers.VOLATILE:
68 s = "volatile"; break;
74 public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
79 if ((mod_flags & PUBLIC) != 0)
80 t |= TypeAttributes.Public;
81 if ((mod_flags & PRIVATE) != 0)
82 t |= TypeAttributes.NotPublic;
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;
96 if ((mod_flags & SEALED) != 0)
97 t |= TypeAttributes.Sealed;
98 if ((mod_flags & ABSTRACT) != 0)
99 t |= TypeAttributes.Abstract;
104 public static TypeAttributes TypeAttr (int mod_flags, TypeContainer caller)
106 TypeAttributes t = TypeAttr (mod_flags, caller.IsTopLevel);
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;
116 public static FieldAttributes FieldAttr (int mod_flags)
118 FieldAttributes fa = 0;
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;
128 fa |= FieldAttributes.Family;
130 if ((mod_flags & INTERNAL) != 0)
131 fa |= FieldAttributes.Assembly;
134 if ((mod_flags & STATIC) != 0)
135 fa |= FieldAttributes.Static;
136 if ((mod_flags & READONLY) != 0)
137 fa |= FieldAttributes.InitOnly;
142 public static MethodAttributes MethodAttr (int mod_flags)
144 MethodAttributes ma = 0;
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;
154 ma |= MethodAttributes.Family;
156 if ((mod_flags & INTERNAL) != 0)
157 ma |= MethodAttributes.Assembly;
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;
166 if ((mod_flags & SEALED) != 0)
167 ma |= MethodAttributes.Final;
169 if ((mod_flags & VIRTUAL) != 0)
170 ma |= MethodAttributes.Virtual;
172 if ((mod_flags & OVERRIDE) != 0)
173 ma |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
175 if ((ma & MethodAttributes.Virtual) != 0)
176 ma |= MethodAttributes.NewSlot;
179 if ((mod_flags & NEW) != 0)
180 ma |= MethodAttributes.HideBySig;
186 // Checks the object @mod modifiers to be in @allowed.
187 // Returns the new mask. Side effect: reports any
188 // incorrect attributes.
190 public static int Check (int allowed, int mod, int def_access, Location l)
192 int invalid_flags = (~allowed) & mod;
195 if (invalid_flags == 0){
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");
207 // If no accessibility bits provided
208 // then provide the defaults.
210 if ((mod & Accessibility) == 0){
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)
223 a = ((a & 2) >> 1) + (a & 5);
224 a = ((a & 4) >> 2) + (a & 3);
226 Report.Error (107, l, "More than one protection modifier specified");
231 for (i = 1; i < TOP; i <<= 1){
232 if ((i & invalid_flags) == 0)
235 Error_InvalidModifier (l, Name (i));
238 return allowed & mod;
241 public static void Error_InvalidModifier (Location l, string name)
243 Report.Error (106, l, "the modifier " + name + " is not valid for this item");