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;
34 public const int METHOD_GENERIC = 0x10000;
36 public const int Accessibility =
37 PUBLIC | PROTECTED | INTERNAL | PRIVATE;
38 public const int AllowedExplicitImplFlags =
41 static public string Name (int i)
48 case Modifiers.PUBLIC:
50 case Modifiers.PROTECTED:
51 s = "protected"; break;
52 case Modifiers.INTERNAL:
53 s = "internal"; break;
54 case Modifiers.PRIVATE:
56 case Modifiers.ABSTRACT:
57 s = "abstract"; break;
58 case Modifiers.SEALED:
60 case Modifiers.STATIC:
62 case Modifiers.READONLY:
63 s = "readonly"; break;
64 case Modifiers.VIRTUAL:
66 case Modifiers.OVERRIDE:
67 s = "override"; break;
68 case Modifiers.EXTERN:
70 case Modifiers.VOLATILE:
71 s = "volatile"; break;
72 case Modifiers.UNSAFE:
79 public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
84 if ((mod_flags & PUBLIC) != 0)
85 t |= TypeAttributes.Public;
86 if ((mod_flags & PRIVATE) != 0)
87 t |= TypeAttributes.NotPublic;
89 if ((mod_flags & PUBLIC) != 0)
90 t |= TypeAttributes.NestedPublic;
91 if ((mod_flags & PRIVATE) != 0)
92 t |= TypeAttributes.NestedPrivate;
93 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
94 t |= TypeAttributes.NestedFamORAssem;
95 if ((mod_flags & PROTECTED) != 0)
96 t |= TypeAttributes.NestedFamily;
97 if ((mod_flags & INTERNAL) != 0)
98 t |= TypeAttributes.NestedAssembly;
101 if ((mod_flags & SEALED) != 0)
102 t |= TypeAttributes.Sealed;
103 if ((mod_flags & ABSTRACT) != 0)
104 t |= TypeAttributes.Abstract;
109 public static TypeAttributes TypeAttr (int mod_flags, TypeContainer caller)
111 TypeAttributes t = TypeAttr (mod_flags, caller.IsTopLevel);
113 // If we do not have static constructors, static methods
114 // can be invoked without initializing the type.
115 if (!caller.UserDefinedStaticConstructor &&
116 !(caller is Interface))
117 t |= TypeAttributes.BeforeFieldInit;
122 public static FieldAttributes FieldAttr (int mod_flags)
124 FieldAttributes fa = 0;
126 if ((mod_flags & PUBLIC) != 0)
127 fa |= FieldAttributes.Public;
128 if ((mod_flags & PRIVATE) != 0)
129 fa |= FieldAttributes.Private;
130 if ((mod_flags & PROTECTED) != 0){
131 if ((mod_flags & INTERNAL) != 0)
132 fa |= FieldAttributes.FamORAssem;
134 fa |= FieldAttributes.Family;
136 if ((mod_flags & INTERNAL) != 0)
137 fa |= FieldAttributes.Assembly;
140 if ((mod_flags & STATIC) != 0)
141 fa |= FieldAttributes.Static;
142 if ((mod_flags & READONLY) != 0)
143 fa |= FieldAttributes.InitOnly;
148 public static MethodAttributes MethodAttr (int mod_flags)
150 MethodAttributes ma = MethodAttributes.HideBySig;
152 if ((mod_flags & PUBLIC) != 0)
153 ma |= MethodAttributes.Public;
154 if ((mod_flags & PRIVATE) != 0)
155 ma |= MethodAttributes.Private;
156 if ((mod_flags & PROTECTED) != 0){
157 if ((mod_flags & INTERNAL) != 0)
158 ma |= MethodAttributes.FamORAssem;
160 ma |= MethodAttributes.Family;
162 if ((mod_flags & INTERNAL) != 0)
163 ma |= MethodAttributes.Assembly;
166 if ((mod_flags & STATIC) != 0)
167 ma |= MethodAttributes.Static;
168 if ((mod_flags & ABSTRACT) != 0){
169 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual;
171 if ((mod_flags & SEALED) != 0)
172 ma |= MethodAttributes.Final;
174 if ((mod_flags & VIRTUAL) != 0)
175 ma |= MethodAttributes.Virtual;
177 if ((mod_flags & OVERRIDE) != 0)
178 ma |= MethodAttributes.Virtual;
180 if ((ma & MethodAttributes.Virtual) != 0)
181 ma |= MethodAttributes.NewSlot;
188 // Checks the object @mod modifiers to be in @allowed.
189 // Returns the new mask. Side effect: reports any
190 // incorrect attributes.
192 public static int Check (int allowed, int mod, int def_access, Location l)
194 int invalid_flags = (~allowed) & mod;
197 if (invalid_flags == 0){
200 if ((mod & Modifiers.UNSAFE) != 0){
201 if (!RootContext.Unsafe){
202 Report.Error (227, l,
203 "Unsafe code requires the -unsafe command " +
204 "line option to be specified");
209 // If no accessibility bits provided
210 // then provide the defaults.
212 if ((mod & Accessibility) == 0){
218 // Make sure that no conflicting accessibility
219 // bits have been set. Protected+Internal is
220 // allowed, that is why they are placed on bits
221 // 1 and 4 (so the shift 3 basically merges them)
225 a = ((a & 2) >> 1) + (a & 5);
226 a = ((a & 4) >> 2) + (a & 3);
228 Report.Error (107, l, "More than one protection modifier specified");
233 for (i = 1; i <= TOP; i <<= 1){
234 if ((i & invalid_flags) == 0)
237 Error_InvalidModifier (l, Name (i));
240 return allowed & mod;
243 public static void Error_InvalidModifier (Location l, string name)
245 Report.Error (106, l, "the modifier " + name + " is not valid for this item");