2001-08-21 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / modifiers.cs
1 //
2 // modifiers.cs: Modifier handling.
3 // 
4 using System;
5 using System.Reflection;
6
7 namespace CIR {
8         public class Modifiers {
9
10                 //
11                 // The ordering of the following 4 constants
12                 // has been carefully done.
13                 //
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;
27
28                 public const int Accessibility =
29                         PUBLIC | PROTECTED | INTERNAL | PRIVATE;
30                 
31                 static public string Name (int i)
32                 {
33                         string s = "";
34                         
35                         switch (i) {
36                         case Modifiers.NEW:
37                                 s = "new"; break;
38                         case Modifiers.PUBLIC:
39                                 s = "public"; break;
40                         case Modifiers.PROTECTED:
41                                 s = "protected"; break;
42                         case Modifiers.INTERNAL:
43                                 s = "internal"; break;
44                         case Modifiers.PRIVATE:
45                                 s = "private"; break;
46                         case Modifiers.ABSTRACT:
47                                 s = "abstract"; break;
48                         case Modifiers.SEALED:
49                                 s = "sealed"; break;
50                         case Modifiers.STATIC:
51                                 s = "static"; break;
52                         case Modifiers.READONLY:
53                                 s = "readonly"; break;
54                         case Modifiers.VIRTUAL:
55                                 s = "virtual"; break;
56                         case Modifiers.OVERRIDE:
57                                 s = "override"; break;
58                         case Modifiers.EXTERN:
59                                 s = "extern"; break;
60                         }
61
62                         return s;
63                 }
64
65                 public static TypeAttributes TypeAttr (int mod_flags)
66                 {
67                         TypeAttributes t = 0;
68                         
69                         if ((mod_flags & PUBLIC) != 0)
70                                 t |= TypeAttributes.Public;
71
72                         // FIXME: implement all flags
73
74                         return t;
75                 }
76
77                 public static FieldAttributes FieldAttr (int mod_flags)
78                 {
79                         FieldAttributes fa = 0;
80
81                         if ((mod_flags & Modifiers.PUBLIC) != 0)
82                                 fa |= FieldAttributes.Public;
83                         if ((mod_flags & Modifiers.PRIVATE) != 0)
84                                 fa |= FieldAttributes.Private;
85                         if ((mod_flags & Modifiers.STATIC) != 0)
86                                 fa |= FieldAttributes.Static;
87                         if ((mod_flags & Modifiers.READONLY) != 0)
88                                 fa |= FieldAttributes.InitOnly;
89
90                         return fa;
91                 }
92
93                 public static MethodAttributes MethodAttr (int mod_flags)
94                 {
95                         MethodAttributes ma = 0;
96
97                         if ((mod_flags & Modifiers.PUBLIC) != 0)
98                                 ma |= MethodAttributes.Public;
99                         if ((mod_flags & Modifiers.PRIVATE) != 0)
100                                 ma |= MethodAttributes.Private;
101                         if ((mod_flags & Modifiers.STATIC) != 0)
102                                 ma |= MethodAttributes.Static;
103                         if ((mod_flags & Modifiers.ABSTRACT) != 0)
104                                 ma |= MethodAttributes.Abstract;
105                         if ((mod_flags & Modifiers.SEALED) != 0)
106                                 ma |= MethodAttributes.Final;
107                         if ((mod_flags & Modifiers.VIRTUAL) != 0)
108                                 ma |= MethodAttributes.Virtual;
109
110                         return ma;
111                 }
112                 
113                 // <summary>
114                 //   Checks the object @mod modifiers to be in @allowed.
115                 //   Returns the new mask.  Side effect: reports any
116                 //   incorrect attributes. 
117                 // </summary>
118                 public static int Check (int allowed, int mod, int def_access)
119                 {
120                         int invalid_flags  = (~allowed) & mod;
121                         int i;
122
123                         if (invalid_flags == 0){
124                                 int a = mod;
125
126                                 //
127                                 // If no accessibility bits provided
128                                 // then provide the defaults.
129                                 //
130                                 if ((mod & Accessibility) == 0){
131                                         mod |= def_access;
132                                         return mod;
133                                 }
134
135                                 //
136                                 // Make sure that no conflicting accessibility
137                                 // bits have been set.  Protected+Internal is
138                                 // allowed, that is why they are placed on bits
139                                 // 1 and 4 (so the shift 3 basically merges them)
140                                 //
141                                 a &= 15;
142                                 a |= (a >> 3);
143                                 a = ((a & 2) >> 1) + (a & 5);
144                                 a = ((a & 4) >> 2) + (a & 3);
145                                 if (a > 1)
146                                         CSharpParser.error (107, "More than one protecion modifier specified");
147                                 
148                                 return mod;
149                         }
150                         
151                         for (i = 1; i < TOP; i <<= 1){
152                                 if ((i & invalid_flags) == 0)
153                                         continue;
154
155                                 CSharpParser.error (106, "the modifier `" + Name (i) + "' is not valid for this item");
156                         }
157
158                         return allowed & mod;
159                 }
160         }
161 }