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 Accessibility =
31 PUBLIC | PROTECTED | INTERNAL | PRIVATE;
33 static public string Name (int i)
40 case Modifiers.PUBLIC:
42 case Modifiers.PROTECTED:
43 s = "protected"; break;
44 case Modifiers.INTERNAL:
45 s = "internal"; break;
46 case Modifiers.PRIVATE:
48 case Modifiers.ABSTRACT:
49 s = "abstract"; break;
50 case Modifiers.SEALED:
52 case Modifiers.STATIC:
54 case Modifiers.READONLY:
55 s = "readonly"; break;
56 case Modifiers.VIRTUAL:
58 case Modifiers.OVERRIDE:
59 s = "override"; break;
60 case Modifiers.EXTERN:
62 case Modifiers.VOLATILE:
63 s = "volatile"; break;
69 public static TypeAttributes TypeAttr (int mod_flags, TypeContainer caller)
72 bool top_level = caller.IsTopLevel;
75 if ((mod_flags & PUBLIC) != 0)
76 t |= TypeAttributes.Public;
77 if ((mod_flags & PRIVATE) != 0)
78 t |= TypeAttributes.NotPublic;
80 if ((mod_flags & PUBLIC) != 0)
81 t |= TypeAttributes.NestedPublic;
82 if ((mod_flags & PRIVATE) != 0)
83 t |= TypeAttributes.NestedPrivate;
84 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
85 t |= TypeAttributes.NestedFamORAssem;
86 if ((mod_flags & PROTECTED) != 0)
87 t |= TypeAttributes.NestedFamily;
88 if ((mod_flags & INTERNAL) != 0)
89 t |= TypeAttributes.NestedAssembly;
92 if ((mod_flags & SEALED) != 0)
93 t |= TypeAttributes.Sealed;
94 if ((mod_flags & ABSTRACT) != 0)
95 t |= TypeAttributes.Abstract;
97 // If we do not have static constructors, static methods
98 // can be invoked without initializing the type.
99 if (!caller.HaveStaticConstructor)
100 t |= TypeAttributes.BeforeFieldInit;
105 public static FieldAttributes FieldAttr (int mod_flags)
107 FieldAttributes fa = 0;
109 if ((mod_flags & PUBLIC) != 0)
110 fa |= FieldAttributes.Public;
111 if ((mod_flags & PRIVATE) != 0)
112 fa |= FieldAttributes.Private;
113 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
114 fa |= FieldAttributes.FamORAssem;
115 if ((mod_flags & PROTECTED) != 0)
116 fa |= FieldAttributes.Family;
117 if ((mod_flags & INTERNAL) != 0)
118 fa |= FieldAttributes.Assembly;
120 if ((mod_flags & STATIC) != 0)
121 fa |= FieldAttributes.Static;
122 if ((mod_flags & READONLY) != 0)
123 fa |= FieldAttributes.InitOnly;
128 public static MethodAttributes MethodAttr (int mod_flags)
130 MethodAttributes ma = 0;
132 if ((mod_flags & PUBLIC) != 0)
133 ma |= MethodAttributes.Public;
134 if ((mod_flags & PRIVATE) != 0)
135 ma |= MethodAttributes.Private;
136 if ((mod_flags & PROTECTED) != 0){
137 if ((mod_flags & INTERNAL) != 0)
138 ma |= MethodAttributes.FamORAssem;
140 ma |= MethodAttributes.Family;
142 if ((mod_flags & INTERNAL) != 0)
143 ma |= MethodAttributes.Assembly;
146 if ((mod_flags & STATIC) != 0)
147 ma |= MethodAttributes.Static;
148 if ((mod_flags & ABSTRACT) != 0){
149 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual |
150 MethodAttributes.HideBySig;
152 if ((mod_flags & SEALED) != 0)
153 ma |= MethodAttributes.Final;
155 if ((mod_flags & VIRTUAL) != 0)
156 ma |= MethodAttributes.Virtual;
158 if ((mod_flags & OVERRIDE) != 0)
159 ma |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
161 if ((ma & MethodAttributes.Virtual) != 0)
162 ma |= MethodAttributes.NewSlot;
165 if ((mod_flags & NEW) != 0)
166 ma |= MethodAttributes.HideBySig;
172 // Checks the object @mod modifiers to be in @allowed.
173 // Returns the new mask. Side effect: reports any
174 // incorrect attributes.
176 public static int Check (int allowed, int mod, int def_access, Location l)
178 int invalid_flags = (~allowed) & mod;
181 if (invalid_flags == 0){
184 if ((mod & Modifiers.UNSAFE) != 0){
185 if (!RootContext.Unsafe){
186 Report.Error (227, l,
187 "Unsafe code requires the --unsafe command " +
188 "line option to be specified");
193 // If no accessibility bits provided
194 // then provide the defaults.
196 if ((mod & Accessibility) == 0){
202 // Make sure that no conflicting accessibility
203 // bits have been set. Protected+Internal is
204 // allowed, that is why they are placed on bits
205 // 1 and 4 (so the shift 3 basically merges them)
209 a = ((a & 2) >> 1) + (a & 5);
210 a = ((a & 4) >> 2) + (a & 3);
212 Report.Error (107, l, "More than one protection modifier specified");
217 for (i = 1; i < TOP; i <<= 1){
218 if ((i & invalid_flags) == 0)
221 Report.Error (106, l, "the modifier `" + Name (i) +
222 "' is not valid for this item");
225 return allowed & mod;