1 | // |
---|
2 | // modifiers.cs: Modifiers handling |
---|
3 | // |
---|
4 | // Authors: Miguel de Icaza (miguel@gnu.org) |
---|
5 | // Marek Safar (marek.safar@gmail.com) |
---|
6 | // |
---|
7 | // Dual licensed under the terms of the MIT X11 or GNU GPL |
---|
8 | // |
---|
9 | // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com) |
---|
10 | // Copyright 2004-2010 Novell, Inc |
---|
11 | // |
---|
12 | |
---|
13 | using System; |
---|
14 | |
---|
15 | #if STATIC |
---|
16 | using IKVM.Reflection; |
---|
17 | #else |
---|
18 | using System.Reflection; |
---|
19 | #endif |
---|
20 | |
---|
21 | namespace Mono.CSharp |
---|
22 | { |
---|
23 | [Flags] |
---|
24 | public enum Modifiers |
---|
25 | { |
---|
26 | PROTECTED = 0x0001, |
---|
27 | PUBLIC = 0x0002, |
---|
28 | PRIVATE = 0x0004, |
---|
29 | INTERNAL = 0x0008, |
---|
30 | NEW = 0x0010, |
---|
31 | ABSTRACT = 0x0020, |
---|
32 | SEALED = 0x0040, |
---|
33 | STATIC = 0x0080, |
---|
34 | READONLY = 0x0100, |
---|
35 | VIRTUAL = 0x0200, |
---|
36 | OVERRIDE = 0x0400, |
---|
37 | EXTERN = 0x0800, |
---|
38 | VOLATILE = 0x1000, |
---|
39 | UNSAFE = 0x2000, |
---|
40 | ASYNC = 0x4000, |
---|
41 | TOP = 0x8000, |
---|
42 | |
---|
43 | // |
---|
44 | // Compiler specific flags |
---|
45 | // |
---|
46 | PROPERTY_CUSTOM = 0x10000, |
---|
47 | |
---|
48 | PARTIAL = 0x20000, |
---|
49 | DEFAULT_ACCESS_MODIFIER = 0x40000, |
---|
50 | METHOD_EXTENSION = 0x80000, |
---|
51 | COMPILER_GENERATED = 0x100000, |
---|
52 | BACKING_FIELD = 0x200000, |
---|
53 | DEBUGGER_HIDDEN = 0x400000, |
---|
54 | DEBUGGER_STEP_THROUGH = 0x800000, |
---|
55 | |
---|
56 | AccessibilityMask = PUBLIC | PROTECTED | INTERNAL | PRIVATE, |
---|
57 | AllowedExplicitImplFlags = UNSAFE | EXTERN, |
---|
58 | } |
---|
59 | |
---|
60 | static class ModifiersExtensions |
---|
61 | { |
---|
62 | public static string AccessibilityName (Modifiers mod) |
---|
63 | { |
---|
64 | switch (mod & Modifiers.AccessibilityMask) { |
---|
65 | case Modifiers.PUBLIC: |
---|
66 | return "public"; |
---|
67 | case Modifiers.PROTECTED: |
---|
68 | return "protected"; |
---|
69 | case Modifiers.PROTECTED | Modifiers.INTERNAL: |
---|
70 | return "protected internal"; |
---|
71 | case Modifiers.INTERNAL: |
---|
72 | return "internal"; |
---|
73 | case Modifiers.PRIVATE: |
---|
74 | return "private"; |
---|
75 | default: |
---|
76 | throw new NotImplementedException (mod.ToString ()); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | static public string Name (Modifiers i) |
---|
81 | { |
---|
82 | string s = ""; |
---|
83 | |
---|
84 | switch (i) { |
---|
85 | case Modifiers.NEW: |
---|
86 | s = "new"; break; |
---|
87 | case Modifiers.PUBLIC: |
---|
88 | s = "public"; break; |
---|
89 | case Modifiers.PROTECTED: |
---|
90 | s = "protected"; break; |
---|
91 | case Modifiers.INTERNAL: |
---|
92 | s = "internal"; break; |
---|
93 | case Modifiers.PRIVATE: |
---|
94 | s = "private"; break; |
---|
95 | case Modifiers.ABSTRACT: |
---|
96 | s = "abstract"; break; |
---|
97 | case Modifiers.SEALED: |
---|
98 | s = "sealed"; break; |
---|
99 | case Modifiers.STATIC: |
---|
100 | s = "static"; break; |
---|
101 | case Modifiers.READONLY: |
---|
102 | s = "readonly"; break; |
---|
103 | case Modifiers.VIRTUAL: |
---|
104 | s = "virtual"; break; |
---|
105 | case Modifiers.OVERRIDE: |
---|
106 | s = "override"; break; |
---|
107 | case Modifiers.EXTERN: |
---|
108 | s = "extern"; break; |
---|
109 | case Modifiers.VOLATILE: |
---|
110 | s = "volatile"; break; |
---|
111 | case Modifiers.UNSAFE: |
---|
112 | s = "unsafe"; break; |
---|
113 | case Modifiers.ASYNC: |
---|
114 | s = "async"; break; |
---|
115 | } |
---|
116 | |
---|
117 | return s; |
---|
118 | } |
---|
119 | |
---|
120 | // |
---|
121 | // Used by custom property accessors to check whether @modA is more restrictive than @modB |
---|
122 | // |
---|
123 | public static bool IsRestrictedModifier (Modifiers modA, Modifiers modB) |
---|
124 | { |
---|
125 | Modifiers flags = 0; |
---|
126 | |
---|
127 | if ((modB & Modifiers.PUBLIC) != 0) { |
---|
128 | flags = Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE; |
---|
129 | } else if ((modB & Modifiers.PROTECTED) != 0) { |
---|
130 | if ((modB & Modifiers.INTERNAL) != 0) |
---|
131 | flags = Modifiers.PROTECTED | Modifiers.INTERNAL; |
---|
132 | |
---|
133 | flags |= Modifiers.PRIVATE; |
---|
134 | } else if ((modB & Modifiers.INTERNAL) != 0) |
---|
135 | flags = Modifiers.PRIVATE; |
---|
136 | |
---|
137 | return modB != modA && (modA & (~flags)) == 0; |
---|
138 | } |
---|
139 | |
---|
140 | public static TypeAttributes TypeAttr (Modifiers mod_flags, bool is_toplevel) |
---|
141 | { |
---|
142 | TypeAttributes t = 0; |
---|
143 | |
---|
144 | if (is_toplevel){ |
---|
145 | if ((mod_flags & Modifiers.PUBLIC) != 0) |
---|
146 | t = TypeAttributes.Public; |
---|
147 | else if ((mod_flags & Modifiers.PRIVATE) != 0) |
---|
148 | t = TypeAttributes.NotPublic; |
---|
149 | } else { |
---|
150 | if ((mod_flags & Modifiers.PUBLIC) != 0) |
---|
151 | t = TypeAttributes.NestedPublic; |
---|
152 | else if ((mod_flags & Modifiers.PRIVATE) != 0) |
---|
153 | t = TypeAttributes.NestedPrivate; |
---|
154 | else if ((mod_flags & (Modifiers.PROTECTED | Modifiers.INTERNAL)) == (Modifiers.PROTECTED | Modifiers.INTERNAL)) |
---|
155 | t = TypeAttributes.NestedFamORAssem; |
---|
156 | else if ((mod_flags & Modifiers.PROTECTED) != 0) |
---|
157 | t = TypeAttributes.NestedFamily; |
---|
158 | else if ((mod_flags & Modifiers.INTERNAL) != 0) |
---|
159 | t = TypeAttributes.NestedAssembly; |
---|
160 | } |
---|
161 | |
---|
162 | if ((mod_flags & Modifiers.SEALED) != 0) |
---|
163 | t |= TypeAttributes.Sealed; |
---|
164 | if ((mod_flags & Modifiers.ABSTRACT) != 0) |
---|
165 | t |= TypeAttributes.Abstract; |
---|
166 | |
---|
167 | return t; |
---|
168 | } |
---|
169 | |
---|
170 | public static FieldAttributes FieldAttr (Modifiers mod_flags) |
---|
171 | { |
---|
172 | FieldAttributes fa = 0; |
---|
173 | |
---|
174 | if ((mod_flags & Modifiers.PUBLIC) != 0) |
---|
175 | fa |= FieldAttributes.Public; |
---|
176 | if ((mod_flags & Modifiers.PRIVATE) != 0) |
---|
177 | fa |= FieldAttributes.Private; |
---|
178 | if ((mod_flags & Modifiers.PROTECTED) != 0) { |
---|
179 | if ((mod_flags & Modifiers.INTERNAL) != 0) |
---|
180 | fa |= FieldAttributes.FamORAssem; |
---|
181 | else |
---|
182 | fa |= FieldAttributes.Family; |
---|
183 | } else { |
---|
184 | if ((mod_flags & Modifiers.INTERNAL) != 0) |
---|
185 | fa |= FieldAttributes.Assembly; |
---|
186 | } |
---|
187 | |
---|
188 | if ((mod_flags & Modifiers.STATIC) != 0) |
---|
189 | fa |= FieldAttributes.Static; |
---|
190 | if ((mod_flags & Modifiers.READONLY) != 0) |
---|
191 | fa |= FieldAttributes.InitOnly; |
---|
192 | |
---|
193 | return fa; |
---|
194 | } |
---|
195 | |
---|
196 | public static MethodAttributes MethodAttr (Modifiers mod_flags) |
---|
197 | { |
---|
198 | MethodAttributes ma = MethodAttributes.HideBySig; |
---|
199 | |
---|
200 | switch (mod_flags & Modifiers.AccessibilityMask) { |
---|
201 | case Modifiers.PUBLIC: |
---|
202 | ma |= MethodAttributes.Public; |
---|
203 | break; |
---|
204 | case Modifiers.PRIVATE: |
---|
205 | ma |= MethodAttributes.Private; |
---|
206 | break; |
---|
207 | case Modifiers.PROTECTED | Modifiers.INTERNAL: |
---|
208 | ma |= MethodAttributes.FamORAssem; |
---|
209 | break; |
---|
210 | case Modifiers.PROTECTED: |
---|
211 | ma |= MethodAttributes.Family; |
---|
212 | break; |
---|
213 | case Modifiers.INTERNAL: |
---|
214 | ma |= MethodAttributes.Assembly; |
---|
215 | break; |
---|
216 | default: |
---|
217 | throw new NotImplementedException (mod_flags.ToString ()); |
---|
218 | } |
---|
219 | |
---|
220 | if ((mod_flags & Modifiers.STATIC) != 0) |
---|
221 | ma |= MethodAttributes.Static; |
---|
222 | if ((mod_flags & Modifiers.ABSTRACT) != 0) { |
---|
223 | ma |= MethodAttributes.Abstract | MethodAttributes.Virtual; |
---|
224 | } |
---|
225 | if ((mod_flags & Modifiers.SEALED) != 0) |
---|
226 | ma |= MethodAttributes.Final; |
---|
227 | |
---|
228 | if ((mod_flags & Modifiers.VIRTUAL) != 0) |
---|
229 | ma |= MethodAttributes.Virtual; |
---|
230 | |
---|
231 | if ((mod_flags & Modifiers.OVERRIDE) != 0) { |
---|
232 | ma |= MethodAttributes.Virtual; |
---|
233 | } else { |
---|
234 | if ((ma & MethodAttributes.Virtual) != 0) |
---|
235 | ma |= MethodAttributes.NewSlot; |
---|
236 | } |
---|
237 | |
---|
238 | return ma; |
---|
239 | } |
---|
240 | |
---|
241 | // <summary> |
---|
242 | // Checks the object @mod modifiers to be in @allowed. |
---|
243 | // Returns the new mask. Side effect: reports any |
---|
244 | // incorrect attributes. |
---|
245 | // </summary> |
---|
246 | public static Modifiers Check (Modifiers allowed, Modifiers mod, Modifiers def_access, Location l, Report Report) |
---|
247 | { |
---|
248 | int invalid_flags = (~(int) allowed) & ((int) mod & ((int) Modifiers.TOP - 1)); |
---|
249 | int i; |
---|
250 | |
---|
251 | if (invalid_flags == 0){ |
---|
252 | // |
---|
253 | // If no accessibility bits provided |
---|
254 | // then provide the defaults. |
---|
255 | // |
---|
256 | if ((mod & Modifiers.AccessibilityMask) == 0) { |
---|
257 | mod |= def_access; |
---|
258 | if (def_access != 0) |
---|
259 | mod |= Modifiers.DEFAULT_ACCESS_MODIFIER; |
---|
260 | return mod; |
---|
261 | } |
---|
262 | |
---|
263 | return mod; |
---|
264 | } |
---|
265 | |
---|
266 | for (i = 1; i < (int) Modifiers.TOP; i <<= 1) { |
---|
267 | if ((i & invalid_flags) == 0) |
---|
268 | continue; |
---|
269 | |
---|
270 | Error_InvalidModifier ((Modifiers)i, l, Report); |
---|
271 | } |
---|
272 | |
---|
273 | return allowed & mod; |
---|
274 | } |
---|
275 | |
---|
276 | static void Error_InvalidModifier (Modifiers mod, Location l, Report Report) |
---|
277 | { |
---|
278 | Report.Error (106, l, "The modifier `{0}' is not valid for this item", |
---|
279 | Name (mod)); |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|