2 // modifiers.cs: Modifier handling.
5 using System
.Reflection
;
13 // The ordering of the following 4 constants
14 // has been carefully done.
33 // Compiler specific flags
35 PROPERTY_CUSTOM
= 0x4000,
37 DEFAULT_ACCESS_MODIFER
= 0x40000,
38 METHOD_EXTENSION
= 0x80000,
39 COMPILER_GENERATED
= 0x100000,
40 BACKING_FIELD
= 0x200000,
41 DEBUGGER_HIDDEN
= 0x400000,
43 AccessibilityMask
= PUBLIC
| PROTECTED
| INTERNAL
| PRIVATE
,
44 AllowedExplicitImplFlags
= UNSAFE
| EXTERN
,
47 static class ModifiersExtensions
49 static public string Name (Modifiers 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
.FamORAssem
)
101 return "protected internal";
103 if (ma
== MethodAttributes
.Private
)
106 throw new NotImplementedException (ma
.ToString ());
109 public static TypeAttributes
TypeAttr (Modifiers mod_flags
, bool is_toplevel
)
111 TypeAttributes t
= 0;
114 if ((mod_flags
& Modifiers
.PUBLIC
) != 0)
115 t
= TypeAttributes
.Public
;
116 else if ((mod_flags
& Modifiers
.PRIVATE
) != 0)
117 t
= TypeAttributes
.NotPublic
;
119 if ((mod_flags
& Modifiers
.PUBLIC
) != 0)
120 t
= TypeAttributes
.NestedPublic
;
121 else if ((mod_flags
& Modifiers
.PRIVATE
) != 0)
122 t
= TypeAttributes
.NestedPrivate
;
123 else if ((mod_flags
& (Modifiers
.PROTECTED
| Modifiers
.INTERNAL
)) == (Modifiers
.PROTECTED
| Modifiers
.INTERNAL
))
124 t
= TypeAttributes
.NestedFamORAssem
;
125 else if ((mod_flags
& Modifiers
.PROTECTED
) != 0)
126 t
= TypeAttributes
.NestedFamily
;
127 else if ((mod_flags
& Modifiers
.INTERNAL
) != 0)
128 t
= TypeAttributes
.NestedAssembly
;
131 if ((mod_flags
& Modifiers
.SEALED
) != 0)
132 t
|= TypeAttributes
.Sealed
;
133 if ((mod_flags
& Modifiers
.ABSTRACT
) != 0)
134 t
|= TypeAttributes
.Abstract
;
139 public static FieldAttributes
FieldAttr (Modifiers mod_flags
)
141 FieldAttributes fa
= 0;
143 if ((mod_flags
& Modifiers
.PUBLIC
) != 0)
144 fa
|= FieldAttributes
.Public
;
145 if ((mod_flags
& Modifiers
.PRIVATE
) != 0)
146 fa
|= FieldAttributes
.Private
;
147 if ((mod_flags
& Modifiers
.PROTECTED
) != 0) {
148 if ((mod_flags
& Modifiers
.INTERNAL
) != 0)
149 fa
|= FieldAttributes
.FamORAssem
;
151 fa
|= FieldAttributes
.Family
;
153 if ((mod_flags
& Modifiers
.INTERNAL
) != 0)
154 fa
|= FieldAttributes
.Assembly
;
157 if ((mod_flags
& Modifiers
.STATIC
) != 0)
158 fa
|= FieldAttributes
.Static
;
159 if ((mod_flags
& Modifiers
.READONLY
) != 0)
160 fa
|= FieldAttributes
.InitOnly
;
165 public static MethodAttributes
MethodAttr (Modifiers mod_flags
)
167 MethodAttributes ma
= MethodAttributes
.HideBySig
;
169 if ((mod_flags
& Modifiers
.PUBLIC
) != 0)
170 ma
|= MethodAttributes
.Public
;
171 else if ((mod_flags
& Modifiers
.PRIVATE
) != 0)
172 ma
|= MethodAttributes
.Private
;
173 else if ((mod_flags
& Modifiers
.PROTECTED
) != 0) {
174 if ((mod_flags
& Modifiers
.INTERNAL
) != 0)
175 ma
|= MethodAttributes
.FamORAssem
;
177 ma
|= MethodAttributes
.Family
;
179 if ((mod_flags
& Modifiers
.INTERNAL
) != 0)
180 ma
|= MethodAttributes
.Assembly
;
183 if ((mod_flags
& Modifiers
.STATIC
) != 0)
184 ma
|= MethodAttributes
.Static
;
185 if ((mod_flags
& Modifiers
.ABSTRACT
) != 0) {
186 ma
|= MethodAttributes
.Abstract
| MethodAttributes
.Virtual
;
188 if ((mod_flags
& Modifiers
.SEALED
) != 0)
189 ma
|= MethodAttributes
.Final
;
191 if ((mod_flags
& Modifiers
.VIRTUAL
) != 0)
192 ma
|= MethodAttributes
.Virtual
;
194 if ((mod_flags
& Modifiers
.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 Modifiers
Check (Modifiers allowed
, Modifiers mod
, Modifiers def_access
, Location l
, Report Report
)
211 int invalid_flags
= (~
(int) allowed
) & ((int) mod
& ((int) Modifiers
.TOP
- 1));
214 if (invalid_flags
== 0){
216 if ((mod
& Modifiers
.UNSAFE
) != 0){
217 RootContext
.CheckUnsafeOption (l
, Report
);
221 // If no accessibility bits provided
222 // then provide the defaults.
224 if ((mod
& Modifiers
.AccessibilityMask
) == 0) {
227 mod
|= Modifiers
.DEFAULT_ACCESS_MODIFER
;
232 // Make sure that no conflicting accessibility
233 // bits have been set. Protected+Internal is
234 // allowed, that is why they are placed on bits
235 // 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", Report
);
248 for (i
= 1; i
<= (int) Modifiers
.TOP
; i
<<= 1) {
249 if ((i
& invalid_flags
) == 0)
252 Error_InvalidModifier (l
, Name ((Modifiers
) i
), Report
);
255 return allowed
& mod
;
258 public static void Error_InvalidModifier (Location l
, string name
, Report Report
)
260 Report
.Error (106, l
, "The modifier `" + name
+ "' is not valid for this item", Report
);