2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / mcs / modifiers.cs
blobbf1e540f0b41cc189182e962ed144a8d6823c0a0
1 //
2 // modifiers.cs: Modifier handling.
3 //
4 using System;
5 using System.Reflection;
7 namespace Mono.CSharp
9 [Flags]
10 public enum Modifiers
13 // The ordering of the following 4 constants
14 // has been carefully done.
16 PROTECTED = 0x0001,
17 PUBLIC = 0x0002,
18 PRIVATE = 0x0004,
19 INTERNAL = 0x0008,
20 NEW = 0x0010,
21 ABSTRACT = 0x0020,
22 SEALED = 0x0040,
23 STATIC = 0x0080,
24 READONLY = 0x0100,
25 VIRTUAL = 0x0200,
26 OVERRIDE = 0x0400,
27 EXTERN = 0x0800,
28 VOLATILE = 0x1000,
29 UNSAFE = 0x2000,
30 TOP = 0x4000,
33 // Compiler specific flags
35 PROPERTY_CUSTOM = 0x4000,
36 PARTIAL = 0x20000,
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)
51 string s = "";
53 switch (i) {
54 case Modifiers.NEW:
55 s = "new"; break;
56 case Modifiers.PUBLIC:
57 s = "public"; break;
58 case Modifiers.PROTECTED:
59 s = "protected"; break;
60 case Modifiers.INTERNAL:
61 s = "internal"; break;
62 case Modifiers.PRIVATE:
63 s = "private"; break;
64 case Modifiers.ABSTRACT:
65 s = "abstract"; break;
66 case Modifiers.SEALED:
67 s = "sealed"; break;
68 case Modifiers.STATIC:
69 s = "static"; break;
70 case Modifiers.READONLY:
71 s = "readonly"; break;
72 case Modifiers.VIRTUAL:
73 s = "virtual"; break;
74 case Modifiers.OVERRIDE:
75 s = "override"; break;
76 case Modifiers.EXTERN:
77 s = "extern"; break;
78 case Modifiers.VOLATILE:
79 s = "volatile"; break;
80 case Modifiers.UNSAFE:
81 s = "unsafe"; break;
84 return s;
87 public static string GetDescription (MethodAttributes ma)
89 ma &= MethodAttributes.MemberAccessMask;
91 if (ma == MethodAttributes.Assembly)
92 return "internal";
94 if (ma == MethodAttributes.Family)
95 return "protected";
97 if (ma == MethodAttributes.Public)
98 return "public";
100 if (ma == MethodAttributes.FamORAssem)
101 return "protected internal";
103 if (ma == MethodAttributes.Private)
104 return "private";
106 throw new NotImplementedException (ma.ToString ());
109 public static TypeAttributes TypeAttr (Modifiers mod_flags, bool is_toplevel)
111 TypeAttributes t = 0;
113 if (is_toplevel){
114 if ((mod_flags & Modifiers.PUBLIC) != 0)
115 t = TypeAttributes.Public;
116 else if ((mod_flags & Modifiers.PRIVATE) != 0)
117 t = TypeAttributes.NotPublic;
118 } else {
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;
136 return t;
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;
150 else
151 fa |= FieldAttributes.Family;
152 } else {
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;
162 return fa;
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;
176 else
177 ma |= MethodAttributes.Family;
178 } else {
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;
196 else {
197 if ((ma & MethodAttributes.Virtual) != 0)
198 ma |= MethodAttributes.NewSlot;
201 return ma;
204 // <summary>
205 // Checks the object @mod modifiers to be in @allowed.
206 // Returns the new mask. Side effect: reports any
207 // incorrect attributes.
208 // </summary>
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));
212 int i;
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) {
225 mod |= def_access;
226 if (def_access != 0)
227 mod |= Modifiers.DEFAULT_ACCESS_MODIFER;
228 return mod;
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)
237 int a = (int) mod;
238 a &= 15;
239 a |= (a >> 3);
240 a = ((a & 2) >> 1) + (a & 5);
241 a = ((a & 4) >> 2) + (a & 3);
242 if (a > 1)
243 Report.Error (107, l, "More than one protection modifier specified", Report);
245 return mod;
248 for (i = 1; i <= (int) Modifiers.TOP; i <<= 1) {
249 if ((i & invalid_flags) == 0)
250 continue;
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);