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;
39 static public string Name (int i)
46 case Modifiers.PUBLIC:
48 case Modifiers.PROTECTED:
49 s = "protected"; break;
50 case Modifiers.INTERNAL:
51 s = "internal"; break;
52 case Modifiers.PRIVATE:
54 case Modifiers.ABSTRACT:
55 s = "abstract"; break;
56 case Modifiers.SEALED:
58 case Modifiers.STATIC:
60 case Modifiers.READONLY:
61 s = "readonly"; break;
62 case Modifiers.VIRTUAL:
64 case Modifiers.OVERRIDE:
65 s = "override"; break;
66 case Modifiers.EXTERN:
68 case Modifiers.VOLATILE:
69 s = "volatile"; break;
75 public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
80 if ((mod_flags & PUBLIC) != 0)
81 t |= TypeAttributes.Public;
82 if ((mod_flags & PRIVATE) != 0)
83 t |= TypeAttributes.NotPublic;
85 if ((mod_flags & PUBLIC) != 0)
86 t |= TypeAttributes.NestedPublic;
87 if ((mod_flags & PRIVATE) != 0)
88 t |= TypeAttributes.NestedPrivate;
89 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
90 t |= TypeAttributes.NestedFamORAssem;
91 if ((mod_flags & PROTECTED) != 0)
92 t |= TypeAttributes.NestedFamily;
93 if ((mod_flags & INTERNAL) != 0)
94 t |= TypeAttributes.NestedAssembly;
97 if ((mod_flags & SEALED) != 0)
98 t |= TypeAttributes.Sealed;
99 if ((mod_flags & ABSTRACT) != 0)
100 t |= TypeAttributes.Abstract;
105 public static TypeAttributes TypeAttr (int mod_flags, TypeContainer caller)
107 TypeAttributes t = TypeAttr (mod_flags, caller.IsTopLevel);
109 // If we do not have static constructors, static methods
110 // can be invoked without initializing the type.
111 if (!caller.HaveStaticConstructor)
112 t |= TypeAttributes.BeforeFieldInit;
117 public static FieldAttributes FieldAttr (int mod_flags)
119 FieldAttributes fa = 0;
121 if ((mod_flags & PUBLIC) != 0)
122 fa |= FieldAttributes.Public;
123 if ((mod_flags & PRIVATE) != 0)
124 fa |= FieldAttributes.Private;
125 if ((mod_flags & PROTECTED) != 0){
126 if ((mod_flags & INTERNAL) != 0)
127 fa |= FieldAttributes.FamORAssem;
129 fa |= FieldAttributes.Family;
131 if ((mod_flags & INTERNAL) != 0)
132 fa |= FieldAttributes.Assembly;
135 if ((mod_flags & STATIC) != 0)
136 fa |= FieldAttributes.Static;
137 if ((mod_flags & READONLY) != 0)
138 fa |= FieldAttributes.InitOnly;
143 public static MethodAttributes MethodAttr (int mod_flags)
145 MethodAttributes ma = 0;
147 if ((mod_flags & PUBLIC) != 0)
148 ma |= MethodAttributes.Public;
149 if ((mod_flags & PRIVATE) != 0)
150 ma |= MethodAttributes.Private;
151 if ((mod_flags & PROTECTED) != 0){
152 if ((mod_flags & INTERNAL) != 0)
153 ma |= MethodAttributes.FamORAssem;
155 ma |= MethodAttributes.Family;
157 if ((mod_flags & INTERNAL) != 0)
158 ma |= MethodAttributes.Assembly;
161 if ((mod_flags & STATIC) != 0)
162 ma |= MethodAttributes.Static;
163 if ((mod_flags & ABSTRACT) != 0){
164 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual |
165 MethodAttributes.HideBySig;
167 if ((mod_flags & SEALED) != 0)
168 ma |= MethodAttributes.Final;
170 if ((mod_flags & VIRTUAL) != 0)
171 ma |= MethodAttributes.Virtual;
173 if ((mod_flags & OVERRIDE) != 0)
174 ma |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
176 if ((ma & MethodAttributes.Virtual) != 0)
177 ma |= MethodAttributes.NewSlot;
180 if ((mod_flags & NEW) != 0)
181 ma |= MethodAttributes.HideBySig;
187 // Checks the object @mod modifiers to be in @allowed.
188 // Returns the new mask. Side effect: reports any
189 // incorrect attributes.
191 public static int Check (int allowed, int mod, int def_access, Location l)
193 int invalid_flags = (~allowed) & mod;
196 if (invalid_flags == 0){
199 if ((mod & Modifiers.UNSAFE) != 0){
200 if (!RootContext.Unsafe){
201 Report.Error (227, l,
202 "Unsafe code requires the --unsafe command " +
203 "line option to be specified");
208 // If no accessibility bits provided
209 // then provide the defaults.
211 if ((mod & Accessibility) == 0){
217 // Make sure that no conflicting accessibility
218 // bits have been set. Protected+Internal is
219 // allowed, that is why they are placed on bits
220 // 1 and 4 (so the shift 3 basically merges them)
224 a = ((a & 2) >> 1) + (a & 5);
225 a = ((a & 4) >> 2) + (a & 3);
227 Report.Error (107, l, "More than one protection modifier specified");
232 for (i = 1; i < TOP; i <<= 1){
233 if ((i & invalid_flags) == 0)
236 Error_InvalidModifier (l, Name (i));
239 return allowed & mod;
242 public static void Error_InvalidModifier (Location l, string name)
244 Report.Error (106, l, "the modifier " + name + " is not valid for this item");