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;
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;
41 public const int Accessibility =
42 PUBLIC | PROTECTED | INTERNAL | PRIVATE;
43 public const int AllowedExplicitImplFlags =
46 static public string Name (int i)
53 case Modifiers.PUBLIC:
55 case Modifiers.PROTECTED:
56 s = "protected"; break;
57 case Modifiers.INTERNAL:
58 s = "internal"; break;
59 case Modifiers.PRIVATE:
61 case Modifiers.ABSTRACT:
62 s = "abstract"; break;
63 case Modifiers.SEALED:
65 case Modifiers.STATIC:
67 case Modifiers.READONLY:
68 s = "readonly"; break;
69 case Modifiers.VIRTUAL:
71 case Modifiers.OVERRIDE:
72 s = "override"; break;
73 case Modifiers.EXTERN:
75 case Modifiers.VOLATILE:
76 s = "volatile"; break;
77 case Modifiers.UNSAFE:
84 public static string GetDescription (MethodAttributes ma)
86 if ((ma & MethodAttributes.Assembly) != 0)
89 if ((ma & MethodAttributes.Family) != 0)
92 if ((ma & MethodAttributes.Public) != 0)
95 if ((ma & MethodAttributes.FamANDAssem) != 0)
96 return "protected internal";
98 if ((ma & MethodAttributes.Private) != 0)
101 throw new NotImplementedException (ma.ToString ());
104 public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
106 TypeAttributes t = 0;
109 if ((mod_flags & PUBLIC) != 0)
110 t |= TypeAttributes.Public;
111 if ((mod_flags & PRIVATE) != 0)
112 t |= TypeAttributes.NotPublic;
114 if ((mod_flags & PUBLIC) != 0)
115 t |= TypeAttributes.NestedPublic;
116 if ((mod_flags & PRIVATE) != 0)
117 t |= TypeAttributes.NestedPrivate;
118 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
119 t |= TypeAttributes.NestedFamORAssem;
120 if ((mod_flags & PROTECTED) != 0)
121 t |= TypeAttributes.NestedFamily;
122 if ((mod_flags & INTERNAL) != 0)
123 t |= TypeAttributes.NestedAssembly;
126 if ((mod_flags & SEALED) != 0)
127 t |= TypeAttributes.Sealed;
128 if ((mod_flags & ABSTRACT) != 0)
129 t |= TypeAttributes.Abstract;
134 public static FieldAttributes FieldAttr (int mod_flags)
136 FieldAttributes fa = 0;
138 if ((mod_flags & PUBLIC) != 0)
139 fa |= FieldAttributes.Public;
140 if ((mod_flags & PRIVATE) != 0)
141 fa |= FieldAttributes.Private;
142 if ((mod_flags & PROTECTED) != 0){
143 if ((mod_flags & INTERNAL) != 0)
144 fa |= FieldAttributes.FamORAssem;
146 fa |= FieldAttributes.Family;
148 if ((mod_flags & INTERNAL) != 0)
149 fa |= FieldAttributes.Assembly;
152 if ((mod_flags & STATIC) != 0)
153 fa |= FieldAttributes.Static;
154 if ((mod_flags & READONLY) != 0)
155 fa |= FieldAttributes.InitOnly;
160 public static MethodAttributes MethodAttr (int mod_flags)
162 MethodAttributes ma = MethodAttributes.HideBySig;
164 if ((mod_flags & PUBLIC) != 0)
165 ma |= MethodAttributes.Public;
166 if ((mod_flags & PRIVATE) != 0)
167 ma |= MethodAttributes.Private;
168 if ((mod_flags & PROTECTED) != 0){
169 if ((mod_flags & INTERNAL) != 0)
170 ma |= MethodAttributes.FamORAssem;
172 ma |= MethodAttributes.Family;
174 if ((mod_flags & INTERNAL) != 0)
175 ma |= MethodAttributes.Assembly;
178 if ((mod_flags & STATIC) != 0)
179 ma |= MethodAttributes.Static;
180 if ((mod_flags & ABSTRACT) != 0){
181 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual;
183 if ((mod_flags & SEALED) != 0)
184 ma |= MethodAttributes.Final;
186 if ((mod_flags & VIRTUAL) != 0)
187 ma |= MethodAttributes.Virtual;
189 if ((mod_flags & OVERRIDE) != 0)
190 ma |= MethodAttributes.Virtual;
192 if ((ma & MethodAttributes.Virtual) != 0)
193 ma |= MethodAttributes.NewSlot;
200 // Checks the object @mod modifiers to be in @allowed.
201 // Returns the new mask. Side effect: reports any
202 // incorrect attributes.
204 public static int Check (int allowed, int mod, int def_access, Location l)
206 int invalid_flags = (~allowed) & mod;
209 if (invalid_flags == 0){
212 if ((mod & Modifiers.UNSAFE) != 0){
213 RootContext.CheckUnsafeOption (l);
217 // If no accessibility bits provided
218 // then provide the defaults.
220 if ((mod & Accessibility) == 0){
223 mod |= DEFAULT_ACCESS_MODIFER;
228 // Make sure that no conflicting accessibility
229 // bits have been set. Protected+Internal is
230 // allowed, that is why they are placed on bits
231 // 1 and 4 (so the shift 3 basically merges them)
235 a = ((a & 2) >> 1) + (a & 5);
236 a = ((a & 4) >> 2) + (a & 3);
238 Report.Error (107, l, "More than one protection modifier specified");
243 for (i = 1; i <= TOP; i <<= 1){
244 if ((i & invalid_flags) == 0)
247 Error_InvalidModifier (l, Name (i));
250 return allowed & mod;
253 public static void Error_InvalidModifier (Location l, string name)
255 Report.Error (106, l, "The modifier `" + name + "' is not valid for this item");