0e53748c0fc1f510eab871bae6e4d58d251b336a
[mono.git] / mcs / mcs / modifiers.cs
1 //
2 // modifiers.cs: Modifier handling.
3 // 
4 using System;
5
6 namespace CIR {
7         public class Modifiers {
8
9                 //
10                 // The ordering of the following 4 constants
11                 // has been carefully done.
12                 //
13                 public const int PROTECTED = 0x0001;
14                 public const int PUBLIC    = 0x0002;
15                 public const int PRIVATE   = 0x0004;
16                 public const int INTERNAL  = 0x0008;
17                 public const int NEW       = 0x0010;
18                 public const int ABSTRACT  = 0x0020;
19                 public const int SEALED    = 0x0040;
20                 public const int STATIC    = 0x0080;
21                 public const int READONLY  = 0x0100;
22                 public const int VIRTUAL   = 0x0200;
23                 public const int OVERRIDE  = 0x0400;
24                 public const int EXTERN    = 0x0800;
25                 public const int TOP       = 0x0800;
26
27                 public const int Accessibility =
28                         PUBLIC | PROTECTED | INTERNAL | PRIVATE;
29                 
30                 static public string Name (int i)
31                 {
32                         string s = "";
33                         
34                         switch (i) {
35                         case Modifiers.NEW:
36                                 s = "new"; break;
37                         case Modifiers.PUBLIC:
38                                 s = "public"; break;
39                         case Modifiers.PROTECTED:
40                                 s = "protected"; break;
41                         case Modifiers.INTERNAL:
42                                 s = "internal"; break;
43                         case Modifiers.PRIVATE:
44                                 s = "private"; break;
45                         case Modifiers.ABSTRACT:
46                                 s = "abstract"; break;
47                         case Modifiers.SEALED:
48                                 s = "sealed"; break;
49                         case Modifiers.STATIC:
50                                 s = "static"; break;
51                         case Modifiers.READONLY:
52                                 s = "readonly"; break;
53                         case Modifiers.VIRTUAL:
54                                 s = "virtual"; break;
55                         case Modifiers.OVERRIDE:
56                                 s = "override"; break;
57                         case Modifiers.EXTERN:
58                                 s = "extern"; break;
59                         }
60
61                         return s;
62                 }
63                 
64                 // <summary>
65                 //   Checks the object @mod modifiers to be in @allowed.
66                 //   Returns the new mask.  Side effect: reports any
67                 //   incorrect attributes. 
68                 // </summary>
69                 public static int Check (int allowed, int mod, int def_access)
70                 {
71                         int invalid_flags  = (~allowed) & mod;
72                         int i;
73
74                         if (invalid_flags == 0){
75                                 int a = mod;
76
77                                 //
78                                 // If no accessibility bits provided
79                                 // then provide the defaults.
80                                 //
81                                 if ((mod & Accessibility) == 0){
82                                         mod |= def_access;
83                                         return mod;
84                                 }
85
86                                 //
87                                 // Make sure that no conflicting accessibility
88                                 // bits have been set.  Protected+Internal is
89                                 // allowed, that is why they are placed on bits
90                                 // 1 and 4 (so the shift 3 basically merges them)
91                                 //
92                                 a &= 15;
93                                 a |= (a >> 3);
94                                 a = ((a & 2) >> 1) + (a & 5);
95                                 a = ((a & 4) >> 2) + (a & 3);
96                                 if (a > 1)
97                                         CSC.CSharpParser.error (107, "More than one protecion modifier specified");
98                                 
99                                 return mod;
100                         }
101                         
102                         for (i = 1; i < TOP; i <<= 1){
103                                 if ((i & invalid_flags) == 0)
104                                         continue;
105
106                                 CSC.CSharpParser.error (106, "the modifier `" + Name (i) + "' is not valid for this item");
107                         }
108
109                         return allowed & mod;
110                 }
111         }
112 }