2008-07-07 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / modifiers.cs
old mode 100755 (executable)
new mode 100644 (file)
index 2cff17f..4fb596d
@@ -4,7 +4,7 @@
 using System;
 using System.Reflection;
 
-namespace CIR {
+namespace Mono.CSharp {
        public class Modifiers {
 
                //
@@ -23,10 +23,28 @@ namespace CIR {
                public const int VIRTUAL   = 0x0200;
                public const int OVERRIDE  = 0x0400;
                public const int EXTERN    = 0x0800;
-               public const int TOP       = 0x0800;
+               public const int VOLATILE  = 0x1000;
+               public const int UNSAFE    = 0x2000;
+               private const int TOP      = 0x2000;
+
+               public const int PROPERTY_CUSTOM = 0x4000; // Custom property modifier
+
+               //
+               // We use this internally to flag that the method contains an iterator
+               //
+               public const int METHOD_YIELDS                  = 0x8000;
+               public const int METHOD_GENERIC                 = 0x10000;
+               public const int PARTIAL                                = 0x20000;
+               public const int DEFAULT_ACCESS_MODIFER = 0x40000;
+               public const int METHOD_EXTENSION               = 0x80000;
+               public const int COMPILER_GENERATED             = 0x100000;
+               public const int BACKING_FIELD                  = 0x200000 | COMPILER_GENERATED;
+               public const int DEBUGGER_HIDDEN                = 0x400000;
 
                public const int Accessibility =
                        PUBLIC | PROTECTED | INTERNAL | PRIVATE;
+               public const int AllowedExplicitImplFlags =
+                       UNSAFE | EXTERN;
                
                static public string Name (int i)
                {
@@ -57,27 +75,64 @@ namespace CIR {
                                s = "override"; break;
                        case Modifiers.EXTERN:
                                s = "extern"; break;
+                       case Modifiers.VOLATILE:
+                               s = "volatile"; break;
+                       case Modifiers.UNSAFE:
+                               s = "unsafe"; break;
                        }
 
                        return s;
                }
 
-               public static TypeAttributes TypeAttr (int mod_flags)
+               public static string GetDescription (MethodAttributes ma)
+               {
+                       ma &= MethodAttributes.MemberAccessMask;
+
+                       if (ma == MethodAttributes.Assembly)
+                               return "internal";
+
+                       if (ma == MethodAttributes.Family)
+                               return "protected";
+
+                       if (ma == MethodAttributes.Public)
+                               return "public";
+
+                       if (ma == MethodAttributes.FamANDAssem)
+                               return "protected internal";
+
+                       if (ma == MethodAttributes.Private)
+                               return "private";
+
+                       throw new NotImplementedException (ma.ToString ());
+               }
+
+               public static TypeAttributes TypeAttr (int mod_flags, bool is_toplevel)
                {
                        TypeAttributes t = 0;
+
+                       if (is_toplevel){
+                               if ((mod_flags & PUBLIC) != 0)
+                                       t |= TypeAttributes.Public;
+                               if ((mod_flags & PRIVATE) != 0)
+                                       t |= TypeAttributes.NotPublic;
+                       } else {
+                               if ((mod_flags & PUBLIC) != 0)
+                                       t |= TypeAttributes.NestedPublic;
+                               if ((mod_flags & PRIVATE) != 0)
+                                       t |= TypeAttributes.NestedPrivate;
+                               if ((mod_flags & PROTECTED) != 0 && (mod_flags & INTERNAL) != 0)
+                                       t |= TypeAttributes.NestedFamORAssem;
+                               if ((mod_flags & PROTECTED) != 0)
+                                       t |= TypeAttributes.NestedFamily;
+                               if ((mod_flags & INTERNAL) != 0)
+                                       t |= TypeAttributes.NestedAssembly;
+                       }
                        
-                       if ((mod_flags & PUBLIC) != 0)
-                               t |= TypeAttributes.Public;
-                       if ((mod_flags & PRIVATE) != 0)
-                               t |= TypeAttributes.NotPublic;
                        if ((mod_flags & SEALED) != 0)
                                t |= TypeAttributes.Sealed;
                        if ((mod_flags & ABSTRACT) != 0)
                                t |= TypeAttributes.Abstract;
 
-                       // FIXME: what happens to protected and internal ? Are
-                       // they connected with NestedAssembly etc. ?
-
                        return t;
                }
 
@@ -85,13 +140,23 @@ namespace CIR {
                {
                        FieldAttributes fa = 0;
 
-                       if ((mod_flags & Modifiers.PUBLIC) != 0)
+                       if ((mod_flags & PUBLIC) != 0)
                                fa |= FieldAttributes.Public;
-                       if ((mod_flags & Modifiers.PRIVATE) != 0)
+                       if ((mod_flags & PRIVATE) != 0)
                                fa |= FieldAttributes.Private;
-                       if ((mod_flags & Modifiers.STATIC) != 0)
+                       if ((mod_flags & PROTECTED) != 0){
+                               if ((mod_flags & INTERNAL) != 0)
+                                       fa |= FieldAttributes.FamORAssem;
+                               else 
+                                       fa |= FieldAttributes.Family;
+                       } else {
+                               if ((mod_flags & INTERNAL) != 0)
+                                       fa |= FieldAttributes.Assembly;
+                       }
+                       
+                       if ((mod_flags & STATIC) != 0)
                                fa |= FieldAttributes.Static;
-                       if ((mod_flags & Modifiers.READONLY) != 0)
+                       if ((mod_flags & READONLY) != 0)
                                fa |= FieldAttributes.InitOnly;
 
                        return fa;
@@ -99,55 +164,68 @@ namespace CIR {
 
                public static MethodAttributes MethodAttr (int mod_flags)
                {
-                       MethodAttributes ma = 0;
+                       MethodAttributes ma = MethodAttributes.HideBySig;
 
-                       if ((mod_flags & Modifiers.PUBLIC) != 0)
+                       if ((mod_flags & PUBLIC) != 0)
                                ma |= MethodAttributes.Public;
-                       if ((mod_flags & Modifiers.PRIVATE) != 0)
+                       else if ((mod_flags & PRIVATE) != 0)
                                ma |= MethodAttributes.Private;
-                       if ((mod_flags & Modifiers.STATIC) != 0)
+                       else if ((mod_flags & PROTECTED) != 0){
+                               if ((mod_flags & INTERNAL) != 0)
+                                       ma |= MethodAttributes.FamORAssem;
+                               else 
+                                       ma |= MethodAttributes.Family;
+                       } else {
+                               if ((mod_flags & INTERNAL) != 0)
+                                       ma |= MethodAttributes.Assembly;
+                       }
+
+                       if ((mod_flags & STATIC) != 0)
                                ma |= MethodAttributes.Static;
-                       if ((mod_flags & Modifiers.ABSTRACT) != 0)
-                               ma |= MethodAttributes.Abstract;
-                       if ((mod_flags & Modifiers.SEALED) != 0)
+                       if ((mod_flags & ABSTRACT) != 0){
+                               ma |= MethodAttributes.Abstract | MethodAttributes.Virtual;
+                       }
+                       if ((mod_flags & SEALED) != 0)
                                ma |= MethodAttributes.Final;
-                       if ((mod_flags & Modifiers.VIRTUAL) != 0)
+
+                       if ((mod_flags & VIRTUAL) != 0)
                                ma |= MethodAttributes.Virtual;
 
+                       if ((mod_flags & OVERRIDE) != 0)
+                               ma |= MethodAttributes.Virtual;
+                       else {
+                               if ((ma & MethodAttributes.Virtual) != 0)
+                                       ma |= MethodAttributes.NewSlot;
+                       }
+                       
                        return ma;
                }
 
-               public static EventAttributes EventAttr (int mod_flags)
-               {
-                       EventAttributes ea = 0;
-
-                       // FIXME : Somebody please help me with this !! What happens to
-                       // public, static etc modifiers ?
-
-                       ea = EventAttributes.RTSpecialName | EventAttributes.SpecialName;
-                       
-                       return ea;
-               }
-               
                // <summary>
                //   Checks the object @mod modifiers to be in @allowed.
                //   Returns the new mask.  Side effect: reports any
                //   incorrect attributes. 
                // </summary>
-               public static int Check (int allowed, int mod, int def_access)
+               public static int Check (int allowed, int mod, int def_access, Location l)
                {
-                       int invalid_flags  = (~allowed) & mod;
+                       int invalid_flags  = (~allowed) & (mod & (Modifiers.TOP - 1));
                        int i;
 
                        if (invalid_flags == 0){
                                int a = mod;
 
+                               if ((mod & Modifiers.UNSAFE) != 0){
+                                       RootContext.CheckUnsafeOption (l);
+                               }
+                               
                                //
                                // If no accessibility bits provided
                                // then provide the defaults.
                                //
                                if ((mod & Accessibility) == 0){
                                        mod |= def_access;
+                                       if (def_access != 0)
+                                               mod |= DEFAULT_ACCESS_MODIFER;
                                        return mod;
                                }
 
@@ -162,19 +240,24 @@ namespace CIR {
                                a = ((a & 2) >> 1) + (a & 5);
                                a = ((a & 4) >> 2) + (a & 3);
                                if (a > 1)
-                                       CSharpParser.error (107, "More than one protection modifier specified");
+                                       Report.Error (107, l, "More than one protection modifier specified");
                                
                                return mod;
                        }
                        
-                       for (i = 1; i < TOP; i <<= 1){
+                       for (i = 1; i <= TOP; i <<= 1){
                                if ((i & invalid_flags) == 0)
                                        continue;
 
-                               CSharpParser.error (106, "the modifier `" + Name (i) + "' is not valid for this item");
+                               Error_InvalidModifier (l, Name (i));
                        }
 
                        return allowed & mod;
                }
+
+               public static void Error_InvalidModifier (Location l, string name)
+               {
+                       Report.Error (106, l, "The modifier `" + name + "' is not valid for this item");
+               }
        }
 }