(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / mcs / modifiers.cs
blob922d67dab4850448a94284461ff98e32000683f0
1 //
2 // modifiers.cs: Modifier handling.
3 //
4 using System;
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;
31 // We use this internally to flag that the method contains an iterator
33 public const int METHOD_YIELDS = 0x8000;
35 public const int Accessibility =
36 PUBLIC | PROTECTED | INTERNAL | PRIVATE;
37 public const int AllowedExplicitImplFlags =
38 UNSAFE | EXTERN;
40 static public string Name (int i)
42 string s = "";
44 switch (i) {
45 case Modifiers.NEW:
46 s = "new"; break;
47 case Modifiers.PUBLIC:
48 s = "public"; break;
49 case Modifiers.PROTECTED:
50 s = "protected"; break;
51 case Modifiers.INTERNAL:
52 s = "internal"; break;
53 case Modifiers.PRIVATE:
54 s = "private"; break;
55 case Modifiers.ABSTRACT:
56 s = "abstract"; break;
57 case Modifiers.SEALED:
58 s = "sealed"; break;
59 case Modifiers.STATIC:
60 s = "static"; break;
61 case Modifiers.READONLY:
62 s = "readonly"; break;
63 case Modifiers.VIRTUAL:
64 s = "virtual"; break;
65 case Modifiers.OVERRIDE:
66 s = "override"; break;
67 case Modifiers.EXTERN:
68 s = "extern"; break;
69 case Modifiers.VOLATILE:
70 s = "volatile"; break;
71 case Modifiers.UNSAFE:
72 s = "unsafe"; break;
75 return s;
78 public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
80 TypeAttributes t = 0;
82 if (is_toplevel){
83 if ((mod_flags & PUBLIC) != 0)
84 t |= TypeAttributes.Public;
85 if ((mod_flags & PRIVATE) != 0)
86 t |= TypeAttributes.NotPublic;
87 } else {
88 if ((mod_flags & PUBLIC) != 0)
89 t |= TypeAttributes.NestedPublic;
90 if ((mod_flags & PRIVATE) != 0)
91 t |= TypeAttributes.NestedPrivate;
92 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
93 t |= TypeAttributes.NestedFamORAssem;
94 if ((mod_flags & PROTECTED) != 0)
95 t |= TypeAttributes.NestedFamily;
96 if ((mod_flags & INTERNAL) != 0)
97 t |= TypeAttributes.NestedAssembly;
100 if ((mod_flags & SEALED) != 0)
101 t |= TypeAttributes.Sealed;
102 if ((mod_flags & ABSTRACT) != 0)
103 t |= TypeAttributes.Abstract;
105 return t;
108 public static TypeAttributes TypeAttr (int mod_flags, TypeContainer caller)
110 TypeAttributes t = TypeAttr (mod_flags, caller.IsTopLevel);
112 // If we do not have static constructors, static methods
113 // can be invoked without initializing the type.
114 if (!caller.UserDefinedStaticConstructor &&
115 (caller.Kind != Kind.Interface))
116 t |= TypeAttributes.BeforeFieldInit;
118 return t;
121 public static FieldAttributes FieldAttr (int mod_flags)
123 FieldAttributes fa = 0;
125 if ((mod_flags & PUBLIC) != 0)
126 fa |= FieldAttributes.Public;
127 if ((mod_flags & PRIVATE) != 0)
128 fa |= FieldAttributes.Private;
129 if ((mod_flags & PROTECTED) != 0){
130 if ((mod_flags & INTERNAL) != 0)
131 fa |= FieldAttributes.FamORAssem;
132 else
133 fa |= FieldAttributes.Family;
134 } else {
135 if ((mod_flags & INTERNAL) != 0)
136 fa |= FieldAttributes.Assembly;
139 if ((mod_flags & STATIC) != 0)
140 fa |= FieldAttributes.Static;
141 if ((mod_flags & READONLY) != 0)
142 fa |= FieldAttributes.InitOnly;
144 return fa;
147 public static MethodAttributes MethodAttr (int mod_flags)
149 MethodAttributes ma = MethodAttributes.HideBySig;
151 if ((mod_flags & PUBLIC) != 0)
152 ma |= MethodAttributes.Public;
153 if ((mod_flags & PRIVATE) != 0)
154 ma |= MethodAttributes.Private;
155 if ((mod_flags & PROTECTED) != 0){
156 if ((mod_flags & INTERNAL) != 0)
157 ma |= MethodAttributes.FamORAssem;
158 else
159 ma |= MethodAttributes.Family;
160 } else {
161 if ((mod_flags & INTERNAL) != 0)
162 ma |= MethodAttributes.Assembly;
165 if ((mod_flags & STATIC) != 0)
166 ma |= MethodAttributes.Static;
167 if ((mod_flags & ABSTRACT) != 0){
168 ma |= MethodAttributes.Abstract | MethodAttributes.Virtual;
170 if ((mod_flags & SEALED) != 0)
171 ma |= MethodAttributes.Final;
173 if ((mod_flags & VIRTUAL) != 0)
174 ma |= MethodAttributes.Virtual;
176 if ((mod_flags & OVERRIDE) != 0)
177 ma |= MethodAttributes.Virtual;
178 else {
179 if ((ma & MethodAttributes.Virtual) != 0)
180 ma |= MethodAttributes.NewSlot;
183 return ma;
186 // <summary>
187 // Checks the object @mod modifiers to be in @allowed.
188 // Returns the new mask. Side effect: reports any
189 // incorrect attributes.
190 // </summary>
191 public static int Check (int allowed, int mod, int def_access, Location l)
193 int invalid_flags = (~allowed) & mod;
194 int i;
196 if (invalid_flags == 0){
197 int a = mod;
199 if ((mod & Modifiers.UNSAFE) != 0){
200 if (!RootContext.Unsafe){
201 Report.Error (227, l,
202 "Unsafe code requires the -unsafe command " +
203 "line option to be specified");
208 // If no accessibility bits provided
209 // then provide the defaults.
211 if ((mod & Accessibility) == 0){
212 mod |= def_access;
213 return mod;
217 // Make sure that no conflicting accessibility
218 // bits have been set. Protected+Internal is
219 // allowed, that is why they are placed on bits
220 // 1 and 4 (so the shift 3 basically merges them)
222 a &= 15;
223 a |= (a >> 3);
224 a = ((a & 2) >> 1) + (a & 5);
225 a = ((a & 4) >> 2) + (a & 3);
226 if (a > 1)
227 Report.Error (107, l, "More than one protection modifier specified");
229 return mod;
232 for (i = 1; i <= TOP; i <<= 1){
233 if ((i & invalid_flags) == 0)
234 continue;
236 Error_InvalidModifier (l, Name (i));
239 return allowed & mod;
242 public static void Error_InvalidModifier (Location l, string name)
244 Report.Error (106, l, "the modifier " + name + " is not valid for this item");