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 private const int TOP = 0x4000;
30 public const int PROPERTY_CUSTOM = 0x4000; // Custom property modifier
33 // Compiler specific flags
35 public const int PARTIAL = 0x20000;
36 public const int DEFAULT_ACCESS_MODIFER = 0x40000;
37 public const int METHOD_EXTENSION = 0x80000;
38 public const int COMPILER_GENERATED = 0x100000;
39 public const int BACKING_FIELD = 0x200000;
40 public const int DEBUGGER_HIDDEN = 0x400000;
42 public const int Accessibility =
43 PUBLIC | PROTECTED | INTERNAL | PRIVATE;
44 public const int AllowedExplicitImplFlags =
47 static public string Name (int i)
54 case Modifiers.PUBLIC:
56 case Modifiers.PROTECTED:
57 s = "protected"; break;
58 case Modifiers.INTERNAL:
59 s = "internal"; break;
60 case Modifiers.PRIVATE:
62 case Modifiers.ABSTRACT:
63 s = "abstract"; break;
64 case Modifiers.SEALED:
66 case Modifiers.STATIC:
68 case Modifiers.READONLY:
69 s = "readonly"; break;
70 case Modifiers.VIRTUAL:
72 case Modifiers.OVERRIDE:
73 s = "override"; break;
74 case Modifiers.EXTERN:
76 case Modifiers.VOLATILE:
77 s = "volatile"; break;
78 case Modifiers.UNSAFE:
85 public static string GetDescription (MethodAttributes ma)
87 ma &= MethodAttributes.MemberAccessMask;
89 if (ma == MethodAttributes.Assembly)
92 if (ma == MethodAttributes.Family)
95 if (ma == MethodAttributes.Public)
98 if (ma == MethodAttributes.FamORAssem)
99 return "protected internal";
101 if (ma == MethodAttributes.Private)
104 throw new NotImplementedException (ma.ToString ());
107 public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
109 TypeAttributes t = 0;
112 if ((mod_flags & PUBLIC) != 0)
113 t = TypeAttributes.Public;
114 else if ((mod_flags & PRIVATE) != 0)
115 t = TypeAttributes.NotPublic;
117 if ((mod_flags & PUBLIC) != 0)
118 t = TypeAttributes.NestedPublic;
119 else if ((mod_flags & PRIVATE) != 0)
120 t = TypeAttributes.NestedPrivate;
121 else if ((mod_flags & (PROTECTED | INTERNAL)) == (PROTECTED | INTERNAL))
122 t = TypeAttributes.NestedFamORAssem;
123 else if ((mod_flags & PROTECTED) != 0)
124 t = TypeAttributes.NestedFamily;
125 else if ((mod_flags & INTERNAL) != 0)
126 t = TypeAttributes.NestedAssembly;
129 if ((mod_flags & SEALED) != 0)
130 t |= TypeAttributes.Sealed;
131 if ((mod_flags & ABSTRACT) != 0)
132 t |= TypeAttributes.Abstract;
137 public static FieldAttributes FieldAttr (int mod_flags)
139 FieldAttributes fa = 0;
141 if ((mod_flags & PUBLIC) != 0)
142 fa |= FieldAttributes.Public;
143 if ((mod_flags & PRIVATE) != 0)
144 fa |= FieldAttributes.Private;
145 if ((mod_flags & PROTECTED) != 0){
146 if ((mod_flags & INTERNAL) != 0)
147 fa |= FieldAttributes.FamORAssem;
149 fa |= FieldAttributes.Family;
151 if ((mod_flags & INTERNAL) != 0)
152 fa |= FieldAttributes.Assembly;
155 if ((mod_flags & STATIC) != 0)
156 fa |= FieldAttributes.Static;
157 if ((mod_flags & READONLY) != 0)
158 fa |= FieldAttributes.InitOnly;
163 public static MethodAttributes MethodAttr (int mod_flags)
165 MethodAttributes ma = MethodAttributes.HideBySig;
167 if ((mod_flags & PUBLIC) != 0)
168 ma |= MethodAttributes.Public;
169 else if ((mod_flags & PRIVATE) != 0)
170 ma |= MethodAttributes.Private;
171 else if ((mod_flags & PROTECTED) != 0){
172 if ((mod_flags & INTERNAL) != 0)
173 ma |= MethodAttributes.FamORAssem;
175 ma |= MethodAttributes.Family;
177 if ((mod_flags & INTERNAL) != 0)
178 ma |= MethodAttributes.Assembly;
181 if ((mod_flags & STATIC) != 0)
182 ma |= MethodAttributes.Static;
183 if ((mod_flags & ABSTRACT) != 0){
184 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual;
186 if ((mod_flags & SEALED) != 0)
187 ma |= MethodAttributes.Final;
189 if ((mod_flags & VIRTUAL) != 0)
190 ma |= MethodAttributes.Virtual;
192 if ((mod_flags & OVERRIDE) != 0)
193 ma |= MethodAttributes.Virtual;
195 if ((ma & MethodAttributes.Virtual) != 0)
196 ma |= MethodAttributes.NewSlot;
203 // Checks the object @mod modifiers to be in @allowed.
204 // Returns the new mask. Side effect: reports any
205 // incorrect attributes.
207 public static int Check (int allowed, int mod, int def_access, Location l)
209 int invalid_flags = (~allowed) & (mod & (Modifiers.TOP - 1));
212 if (invalid_flags == 0){
215 if ((mod & Modifiers.UNSAFE) != 0){
216 RootContext.CheckUnsafeOption (l);
220 // If no accessibility bits provided
221 // then provide the defaults.
223 if ((mod & Accessibility) == 0){
226 mod |= DEFAULT_ACCESS_MODIFER;
231 // Make sure that no conflicting accessibility
232 // bits have been set. Protected+Internal is
233 // allowed, that is why they are placed on bits
234 // 1 and 4 (so the shift 3 basically merges them)
238 a = ((a & 2) >> 1) + (a & 5);
239 a = ((a & 4) >> 2) + (a & 3);
241 Report.Error (107, l, "More than one protection modifier specified");
246 for (i = 1; i <= TOP; i <<= 1){
247 if ((i & invalid_flags) == 0)
250 Error_InvalidModifier (l, Name (i));
253 return allowed & mod;
256 public static void Error_InvalidModifier (Location l, string name)
258 Report.Error (106, l, "The modifier `" + name + "' is not valid for this item");