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 = 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;
37 public const int PARTIAL = 0x20000;
38 public const int DEFAULT_ACCESS_MODIFER = 0x40000;
39 public const int METHOD_EXTENSION = 0x80000;
40 public const int COMPILER_GENERATED = 0x100000;
41 public const int BACKING_FIELD = 0x200000 | COMPILER_GENERATED;
42 public const int DEBUGGER_HIDDEN = 0x400000;
44 public const int Accessibility =
45 PUBLIC | PROTECTED | INTERNAL | PRIVATE;
46 public const int AllowedExplicitImplFlags =
49 static public string Name (int i)
56 case Modifiers.PUBLIC:
58 case Modifiers.PROTECTED:
59 s = "protected"; break;
60 case Modifiers.INTERNAL:
61 s = "internal"; break;
62 case Modifiers.PRIVATE:
64 case Modifiers.ABSTRACT:
65 s = "abstract"; break;
66 case Modifiers.SEALED:
68 case Modifiers.STATIC:
70 case Modifiers.READONLY:
71 s = "readonly"; break;
72 case Modifiers.VIRTUAL:
74 case Modifiers.OVERRIDE:
75 s = "override"; break;
76 case Modifiers.EXTERN:
78 case Modifiers.VOLATILE:
79 s = "volatile"; break;
80 case Modifiers.UNSAFE:
87 public static string GetDescription (MethodAttributes ma)
89 ma &= MethodAttributes.MemberAccessMask;
91 if (ma == MethodAttributes.Assembly)
94 if (ma == MethodAttributes.Family)
97 if (ma == MethodAttributes.Public)
100 if (ma == MethodAttributes.FamANDAssem)
101 return "protected internal";
103 if (ma == MethodAttributes.Private)
106 throw new NotImplementedException (ma.ToString ());
109 public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
111 TypeAttributes t = 0;
114 if ((mod_flags & PUBLIC) != 0)
115 t |= TypeAttributes.Public;
116 if ((mod_flags & PRIVATE) != 0)
117 t |= TypeAttributes.NotPublic;
119 if ((mod_flags & PUBLIC) != 0)
120 t |= TypeAttributes.NestedPublic;
121 if ((mod_flags & PRIVATE) != 0)
122 t |= TypeAttributes.NestedPrivate;
123 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
124 t |= TypeAttributes.NestedFamORAssem;
125 if ((mod_flags & PROTECTED) != 0)
126 t |= TypeAttributes.NestedFamily;
127 if ((mod_flags & INTERNAL) != 0)
128 t |= TypeAttributes.NestedAssembly;
131 if ((mod_flags & SEALED) != 0)
132 t |= TypeAttributes.Sealed;
133 if ((mod_flags & ABSTRACT) != 0)
134 t |= TypeAttributes.Abstract;
139 public static FieldAttributes FieldAttr (int mod_flags)
141 FieldAttributes fa = 0;
143 if ((mod_flags & PUBLIC) != 0)
144 fa |= FieldAttributes.Public;
145 if ((mod_flags & PRIVATE) != 0)
146 fa |= FieldAttributes.Private;
147 if ((mod_flags & PROTECTED) != 0){
148 if ((mod_flags & INTERNAL) != 0)
149 fa |= FieldAttributes.FamORAssem;
151 fa |= FieldAttributes.Family;
153 if ((mod_flags & INTERNAL) != 0)
154 fa |= FieldAttributes.Assembly;
157 if ((mod_flags & STATIC) != 0)
158 fa |= FieldAttributes.Static;
159 if ((mod_flags & READONLY) != 0)
160 fa |= FieldAttributes.InitOnly;
165 public static MethodAttributes MethodAttr (int mod_flags)
167 MethodAttributes ma = MethodAttributes.HideBySig;
169 if ((mod_flags & PUBLIC) != 0)
170 ma |= MethodAttributes.Public;
171 else if ((mod_flags & PRIVATE) != 0)
172 ma |= MethodAttributes.Private;
173 else if ((mod_flags & PROTECTED) != 0){
174 if ((mod_flags & INTERNAL) != 0)
175 ma |= MethodAttributes.FamORAssem;
177 ma |= MethodAttributes.Family;
179 if ((mod_flags & INTERNAL) != 0)
180 ma |= MethodAttributes.Assembly;
183 if ((mod_flags & STATIC) != 0)
184 ma |= MethodAttributes.Static;
185 if ((mod_flags & ABSTRACT) != 0){
186 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual;
188 if ((mod_flags & SEALED) != 0)
189 ma |= MethodAttributes.Final;
191 if ((mod_flags & VIRTUAL) != 0)
192 ma |= MethodAttributes.Virtual;
194 if ((mod_flags & OVERRIDE) != 0)
195 ma |= MethodAttributes.Virtual;
197 if ((ma & MethodAttributes.Virtual) != 0)
198 ma |= MethodAttributes.NewSlot;
205 // Checks the object @mod modifiers to be in @allowed.
206 // Returns the new mask. Side effect: reports any
207 // incorrect attributes.
209 public static int Check (int allowed, int mod, int def_access, Location l)
211 int invalid_flags = (~allowed) & (mod & (Modifiers.TOP - 1));
214 if (invalid_flags == 0){
217 if ((mod & Modifiers.UNSAFE) != 0){
218 RootContext.CheckUnsafeOption (l);
222 // If no accessibility bits provided
223 // then provide the defaults.
225 if ((mod & Accessibility) == 0){
228 mod |= DEFAULT_ACCESS_MODIFER;
233 // Make sure that no conflicting accessibility
234 // bits have been set. Protected+Internal is
235 // allowed, that is why they are placed on bits
236 // 1 and 4 (so the shift 3 basically merges them)
240 a = ((a & 2) >> 1) + (a & 5);
241 a = ((a & 4) >> 2) + (a & 3);
243 Report.Error (107, l, "More than one protection modifier specified");
248 for (i = 1; i <= TOP; i <<= 1){
249 if ((i & invalid_flags) == 0)
252 Error_InvalidModifier (l, Name (i));
255 return allowed & mod;
258 public static void Error_InvalidModifier (Location l, string name)
260 Report.Error (106, l, "The modifier `" + name + "' is not valid for this item");