The logic was wrong here, it worked because of a codepath not covered, fixes #57895
[mono.git] / mcs / mcs / enum.cs
index 134e253130b9095a453444c8f11abcb3f540b353..32d0bf91c9ec3c577b81b3f85368533886e85ea1 100755 (executable)
@@ -23,7 +23,6 @@ namespace Mono.CSharp {
                ArrayList ordered_enums;
                
                public Expression BaseType;
-               public Attributes  OptAttributes;
                
                public Type UnderlyingType;
 
@@ -49,13 +48,13 @@ namespace Mono.CSharp {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Enum (TypeContainer parent, Expression type, int mod_flags, string name, Attributes attrs, Location l)
-                       : base (parent, name, l)
+               public Enum (NamespaceEntry ns, TypeContainer parent, Expression type, int mod_flags,
+                            string name, Attributes attrs, Location l)
+                       : base (ns, parent, name, attrs, l)
                {
                        this.BaseType = type;
                        ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
                                                    IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE, l);
-                       OptAttributes = attrs;
 
                        ordered_enums = new ArrayList ();
                        member_to_location = new Hashtable ();
@@ -74,6 +73,11 @@ namespace Mono.CSharp {
                        if (defined_names.Contains (name))
                                return AdditionResult.NameExists;
 
+                       if (name == "value__") {
+                               Report.Error (76, loc, "An item in an enumeration can't have an identifier `value__'");
+                               return AdditionResult.Error;
+                       }
+
                        DefineName (name, expr);
 
                        ordered_enums.Add (name);
@@ -87,6 +91,11 @@ namespace Mono.CSharp {
                        return AdditionResult.Success;
                }
 
+               public override void ApplyAttributeBuilder (object builder, Attribute a, CustomAttributeBuilder cb)
+               {
+                       ((TypeBuilder) builder).SetCustomAttribute (cb);
+               }
+
                //
                // This is used by corlib compilation: we map from our
                // type to a type that is consumable by the DefineField
@@ -146,7 +155,10 @@ namespace Mono.CSharp {
                        }
 
                        if (IsTopLevel) {
-                               ModuleBuilder builder = CodeGen.ModuleBuilder;
+                               if (TypeManager.NamespaceClash (Name, Location))
+                                       return null;
+                               
+                               ModuleBuilder builder = CodeGen.Module.Builder;
 
                                TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
                        } else {
@@ -175,7 +187,8 @@ namespace Mono.CSharp {
 
                        if (e is IntConstant || e is UIntConstant || e is LongConstant ||
                            e is ByteConstant || e is SByteConstant || e is ShortConstant ||
-                           e is UShortConstant || e is ULongConstant || e is EnumConstant)
+                           e is UShortConstant || e is ULongConstant || e is EnumConstant ||
+                           e is CharConstant)
                                return true;
                        else
                                return false;
@@ -485,7 +498,7 @@ namespace Mono.CSharp {
                                        Type etype = TypeManager.EnumToUnderlying (c.Type);
                                        
                                        if (!ImplicitConversionExists (etype, UnderlyingType)){
-                                               Expression.Error_CannotConvertImplicit (
+                                               Convert.Error_CannotImplicitConversion (
                                                        loc, c.Type, UnderlyingType);
                                                return null;
                                        }
@@ -495,7 +508,7 @@ namespace Mono.CSharp {
                        FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
                                        | FieldAttributes.Literal;
                        
-                       FieldBuilder fb = TypeBuilder.DefineField (name, UnderlyingType, attr);
+                       FieldBuilder fb = TypeBuilder.DefineField (name, TypeBuilder, attr);
 
                        bool fail;
                        default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
@@ -556,10 +569,14 @@ namespace Mono.CSharp {
 
                                        if (default_value == null)
                                                return true;
-
                                } else {
+                                       if (name == "value__"){
+                                               Report.Error (76, loc, "The name `value__' is reserved for enumerations");
+                                               return false;
+                                       }
+
                                        FieldBuilder fb = TypeBuilder.DefineField (
-                                               name, UnderlyingType, attr);
+                                               name, TypeBuilder, attr);
                                        
                                        if (default_value == null) {
                                           Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
@@ -589,12 +606,61 @@ namespace Mono.CSharp {
 
                                default_value = GetNextDefaultValue (default_value);
                        }
+                       return true;
+               }
                        
-                       Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
+               public override void Emit (TypeContainer tc)
+               {
+                       if (OptAttributes != null) {
+                               EmitContext ec = new EmitContext (tc, this, Location, null, null, ModFlags, false);
+                               Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
+                       }
+
+                       base.Emit (tc);
+               }
+               
+               protected override bool IsIdentifierClsCompliant (DeclSpace ds)
+               {
+                       if (!base.IsIdentifierClsCompliant (ds))
+                               return false;
+
+                       for (int i = 1; i < ordered_enums.Count; ++i) {
+                               string checked_name = ordered_enums [i] as string;
+                               for (int ii = 0; ii < ordered_enums.Count; ++ii) {
+                                       if (ii == i)
+                                               continue;
+
+                                       string enumerator_name = ordered_enums [ii] as string;
+                                       if (String.Compare (checked_name, enumerator_name, true) == 0) {
+                                               Report.SymbolRelatedToPreviousError ((Location)member_to_location [enumerator_name], enumerator_name);
+                                               Report.Error_T (3005, (Location)member_to_location [checked_name], GetEnumeratorName (checked_name));
+                                               break;
+                                       }
+                               }
+                       }
+                       return true;
+               }
+
+               protected override bool VerifyClsCompliance (DeclSpace ds)
+               {
+                       if (!base.VerifyClsCompliance (ds))
+                               return false;
+
+                       if (!AttributeTester.IsClsCompliant (UnderlyingType)) {
+                               Report.Error_T (3009, Location, GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
+                       }
 
                        return true;
                }
                
+               /// <summary>
+               /// Returns full enum name.
+               /// </summary>
+               string GetEnumeratorName (string valueName)
+               {
+                       return String.Concat (Name, ".", valueName);
+               }
+
                //
                // IMemberFinder
                //