2 // modifiers.cs: Modifier handling.
5 using System.Reflection;
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 TOP = 0x0800;
28 public const int Accessibility =
29 PUBLIC | PROTECTED | INTERNAL | PRIVATE;
31 static public string Name (int i)
38 case Modifiers.PUBLIC:
40 case Modifiers.PROTECTED:
41 s = "protected"; break;
42 case Modifiers.INTERNAL:
43 s = "internal"; break;
44 case Modifiers.PRIVATE:
46 case Modifiers.ABSTRACT:
47 s = "abstract"; break;
48 case Modifiers.SEALED:
50 case Modifiers.STATIC:
52 case Modifiers.READONLY:
53 s = "readonly"; break;
54 case Modifiers.VIRTUAL:
56 case Modifiers.OVERRIDE:
57 s = "override"; break;
58 case Modifiers.EXTERN:
65 public static TypeAttributes TypeAttr (int mod_flags, TypeContainer caller)
68 bool top_level = caller.IsTopLevel;
71 if ((mod_flags & PUBLIC) != 0)
72 t |= TypeAttributes.Public;
73 if ((mod_flags & PRIVATE) != 0)
74 t |= TypeAttributes.NotPublic;
76 if ((mod_flags & PUBLIC) != 0)
77 t |= TypeAttributes.NestedPublic;
78 if ((mod_flags & PRIVATE) != 0)
79 t |= TypeAttributes.NestedPrivate;
80 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
81 t |= TypeAttributes.NestedFamORAssem;
82 if ((mod_flags & PROTECTED) != 0)
83 t |= TypeAttributes.NestedFamily;
84 if ((mod_flags & INTERNAL) != 0)
85 t |= TypeAttributes.NestedAssembly;
88 if ((mod_flags & SEALED) != 0)
89 t |= TypeAttributes.Sealed;
90 if ((mod_flags & ABSTRACT) != 0)
91 t |= TypeAttributes.Abstract;
93 // If we have static constructors, the runtime needs to
94 // initialize the class, otherwise we can optimize
96 if (caller.HaveStaticConstructor)
97 t |= TypeAttributes.BeforeFieldInit;
102 public static FieldAttributes FieldAttr (int mod_flags)
104 FieldAttributes fa = 0;
106 if ((mod_flags & PUBLIC) != 0)
107 fa |= FieldAttributes.Public;
108 if ((mod_flags & PRIVATE) != 0)
109 fa |= FieldAttributes.Private;
110 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
111 fa |= FieldAttributes.FamORAssem;
112 if ((mod_flags & PROTECTED) != 0)
113 fa |= FieldAttributes.Family;
114 if ((mod_flags & INTERNAL) != 0)
115 fa |= FieldAttributes.Assembly;
117 if ((mod_flags & STATIC) != 0)
118 fa |= FieldAttributes.Static;
119 if ((mod_flags & READONLY) != 0)
120 fa |= FieldAttributes.InitOnly;
125 public static MethodAttributes MethodAttr (int mod_flags)
127 MethodAttributes ma = 0;
129 if ((mod_flags & PUBLIC) != 0)
130 ma |= MethodAttributes.Public;
131 if ((mod_flags & PRIVATE) != 0)
132 ma |= MethodAttributes.Private;
133 if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
134 ma |= MethodAttributes.FamORAssem;
135 if ((mod_flags & PROTECTED) != 0)
136 ma |= MethodAttributes.Family;
137 if ((mod_flags & INTERNAL) != 0)
138 ma |= MethodAttributes.Assembly;
141 if ((mod_flags & STATIC) != 0)
142 ma |= MethodAttributes.Static;
143 if ((mod_flags & ABSTRACT) != 0)
144 ma |= MethodAttributes.Abstract;
145 if ((mod_flags & SEALED) != 0)
146 ma |= MethodAttributes.Final;
147 if ((mod_flags & VIRTUAL) != 0)
148 ma |= MethodAttributes.Virtual;
154 // Checks the object @mod modifiers to be in @allowed.
155 // Returns the new mask. Side effect: reports any
156 // incorrect attributes.
158 public static int Check (int allowed, int mod, int def_access)
160 int invalid_flags = (~allowed) & mod;
163 if (invalid_flags == 0){
167 // If no accessibility bits provided
168 // then provide the defaults.
170 if ((mod & Accessibility) == 0){
176 // Make sure that no conflicting accessibility
177 // bits have been set. Protected+Internal is
178 // allowed, that is why they are placed on bits
179 // 1 and 4 (so the shift 3 basically merges them)
183 a = ((a & 2) >> 1) + (a & 5);
184 a = ((a & 4) >> 2) + (a & 3);
186 CSharpParser.error (107, "More than one protection modifier specified");
191 for (i = 1; i < TOP; i <<= 1){
192 if ((i & invalid_flags) == 0)
195 CSharpParser.error (106, "the modifier `" + Name (i) + "' is not valid for this item");
198 return allowed & mod;