*** empty log message ***
[mono.git] / mcs / mcs / enum.cs
index 003506f0b6e0569635986307e0ede5c9a6e3a79d..264dc59f94868ede6e119be6e608c191331a2bc1 100755 (executable)
@@ -137,15 +137,19 @@ namespace Mono.CSharp {
                            UnderlyingType != TypeManager.short_type &&
                            UnderlyingType != TypeManager.ushort_type &&
                            UnderlyingType != TypeManager.byte_type  &&
+                           UnderlyingType != TypeManager.char_type  &&
                            UnderlyingType != TypeManager.sbyte_type) {
                                Report.Error (1008, Location,
-                                             "Type byte, sbyte, short, ushort, int, uint, " +
+                                             "Type byte, sbyte, short, char, ushort, int, uint, " +
                                              "long, or ulong expected (got: " +
                                              TypeManager.CSharpName (UnderlyingType) + ")");
                                return null;
                        }
 
                        if (IsTopLevel) {
+                               if (TypeManager.NamespaceClash (Name))
+                                       return null;
+                               
                                ModuleBuilder builder = CodeGen.ModuleBuilder;
 
                                TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
@@ -175,7 +179,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;
@@ -257,12 +262,143 @@ namespace Mono.CSharp {
                        return;
                }
 
+               /// <summary>
+               ///  Determines if a standard implicit conversion exists from
+               ///  expr_type to target_type
+               /// </summary>
+               public static bool ImplicitConversionExists (Type expr_type, Type target_type)
+               {
+                       expr_type = TypeManager.TypeToCoreType (expr_type);
+
+                       if (expr_type == TypeManager.void_type)
+                               return false;
+                       
+                       if (expr_type == target_type)
+                               return true;
+
+                       // First numeric conversions 
+
+                       if (expr_type == TypeManager.sbyte_type){
+                               //
+                               // From sbyte to short, int, long, float, double.
+                               //
+                               if ((target_type == TypeManager.int32_type) || 
+                                   (target_type == TypeManager.int64_type) ||
+                                   (target_type == TypeManager.double_type) ||
+                                   (target_type == TypeManager.float_type)  ||
+                                   (target_type == TypeManager.short_type) ||
+                                   (target_type == TypeManager.decimal_type))
+                                       return true;
+                               
+                       } else if (expr_type == TypeManager.byte_type){
+                               //
+                               // From byte to short, ushort, int, uint, long, ulong, float, double
+                               // 
+                               if ((target_type == TypeManager.short_type) ||
+                                   (target_type == TypeManager.ushort_type) ||
+                                   (target_type == TypeManager.int32_type) ||
+                                   (target_type == TypeManager.uint32_type) ||
+                                   (target_type == TypeManager.uint64_type) ||
+                                   (target_type == TypeManager.int64_type) ||
+                                   (target_type == TypeManager.float_type) ||
+                                   (target_type == TypeManager.double_type) ||
+                                   (target_type == TypeManager.decimal_type))
+                                       return true;
+       
+                       } else if (expr_type == TypeManager.short_type){
+                               //
+                               // From short to int, long, float, double
+                               // 
+                               if ((target_type == TypeManager.int32_type) ||
+                                   (target_type == TypeManager.int64_type) ||
+                                   (target_type == TypeManager.double_type) ||
+                                   (target_type == TypeManager.float_type) ||
+                                   (target_type == TypeManager.decimal_type))
+                                       return true;
+                                       
+                       } else if (expr_type == TypeManager.ushort_type){
+                               //
+                               // From ushort to int, uint, long, ulong, float, double
+                               //
+                               if ((target_type == TypeManager.uint32_type) ||
+                                   (target_type == TypeManager.uint64_type) ||
+                                   (target_type == TypeManager.int32_type) ||
+                                   (target_type == TypeManager.int64_type) ||
+                                   (target_type == TypeManager.double_type) ||
+                                   (target_type == TypeManager.float_type) ||
+                                   (target_type == TypeManager.decimal_type))
+                                       return true;
+                                   
+                       } else if (expr_type == TypeManager.int32_type){
+                               //
+                               // From int to long, float, double
+                               //
+                               if ((target_type == TypeManager.int64_type) ||
+                                   (target_type == TypeManager.double_type) ||
+                                   (target_type == TypeManager.float_type) ||
+                                   (target_type == TypeManager.decimal_type))
+                                       return true;
+                                       
+                       } else if (expr_type == TypeManager.uint32_type){
+                               //
+                               // From uint to long, ulong, float, double
+                               //
+                               if ((target_type == TypeManager.int64_type) ||
+                                   (target_type == TypeManager.uint64_type) ||
+                                   (target_type == TypeManager.double_type) ||
+                                   (target_type == TypeManager.float_type) ||
+                                   (target_type == TypeManager.decimal_type))
+                                       return true;
+                                       
+                       } else if ((expr_type == TypeManager.uint64_type) ||
+                                  (expr_type == TypeManager.int64_type)) {
+                               //
+                               // From long/ulong to float, double
+                               //
+                               if ((target_type == TypeManager.double_type) ||
+                                   (target_type == TypeManager.float_type) ||
+                                   (target_type == TypeManager.decimal_type))
+                                       return true;
+                                   
+                       } else if (expr_type == TypeManager.char_type){
+                               //
+                               // From char to ushort, int, uint, long, ulong, float, double
+                               // 
+                               if ((target_type == TypeManager.ushort_type) ||
+                                   (target_type == TypeManager.int32_type) ||
+                                   (target_type == TypeManager.uint32_type) ||
+                                   (target_type == TypeManager.uint64_type) ||
+                                   (target_type == TypeManager.int64_type) ||
+                                   (target_type == TypeManager.float_type) ||
+                                   (target_type == TypeManager.double_type) ||
+                                   (target_type == TypeManager.decimal_type))
+                                       return true;
+
+                       } else if (expr_type == TypeManager.float_type){
+                               //
+                               // float to double
+                               //
+                               if (target_type == TypeManager.double_type)
+                                       return true;
+                       }       
+                       
+                       return false;
+               }
+
+               //
+               // Horrible, horrible.  But there is no other way we can pass the EmitContext
+               // to the recursive definition triggered by the evaluation of a forward
+               // expression
+               //
+               static EmitContext current_ec = null;
+               
                /// <summary>
                ///  This is used to lookup the value of an enum member. If the member is undefined,
                ///  it attempts to define it and return its value
                /// </summary>
                public object LookupEnumValue (EmitContext ec, string name, Location loc)
                {
+                       
                        object default_value = null;
                        Constant c = null;
 
@@ -302,7 +438,14 @@ namespace Mono.CSharp {
                                                Location m_loc = (Mono.CSharp.Location)
                                                        member_to_location [n];
                                                in_transit.Add (name, true);
+
+                                               EmitContext old_ec = current_ec;
+                                               current_ec = ec;
+                       
                                                default_value = LookupEnumValue (ec, n, m_loc);
+
+                                               current_ec = old_ec;
+                                               
                                                in_transit.Remove (name);
                                                if (default_value == null)
                                                        return null;
@@ -315,39 +458,53 @@ namespace Mono.CSharp {
                                bool old = ec.InEnumContext;
                                ec.InEnumContext = true;
                                in_transit.Add (name, true);
+
+                               EmitContext old_ec = current_ec;
+                               current_ec = ec;
                                val = val.Resolve (ec);
+                               current_ec = old_ec;
+                               
                                in_transit.Remove (name);
                                ec.InEnumContext = old;
-                               
+
                                if (val == null)
                                        return null;
 
-                               if (IsValidEnumConstant (val)) {
-                                       c = (Constant) val;
-                                       default_value = c.GetValue ();
-                                       
-                                       if (default_value == null) {
-                                               Error_ConstantValueCannotBeConverted (c, loc);
-                                               return null;
-                                       }
-                                       
-                               } else {
+                               if (!IsValidEnumConstant (val)) {
                                        Report.Error (
                                                1008, loc,
                                                "Type byte, sbyte, short, ushort, int, uint, long, or " +
                                                "ulong expected (have: " + val + ")");
                                        return null;
                                }
+
+                               c = (Constant) val;
+                               default_value = c.GetValue ();
+
+                               if (default_value == null) {
+                                       Error_ConstantValueCannotBeConverted (c, loc);
+                                       return null;
+                               }
+
+                               if (val is EnumConstant){
+                                       Type etype = TypeManager.EnumToUnderlying (c.Type);
+                                       
+                                       if (!ImplicitConversionExists (etype, UnderlyingType)){
+                                               Convert.Error_CannotImplicitConversion (
+                                                       loc, c.Type, UnderlyingType);
+                                               return null;
+                                       }
+                               }
                        }
 
                        FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
                                        | FieldAttributes.Literal;
                        
-                       FieldBuilder fb = TypeBuilder.DefineField (name, UnderlyingType, attr);
+                       FieldBuilder fb = TypeBuilder.DefineField (name, TypeBuilder, attr);
 
-                       try {
-                               default_value = TypeManager.ChangeType (default_value, UnderlyingType);
-                       } catch {
+                       bool fail;
+                       default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
+                       if (fail){
                                Error_ConstantValueCannotBeConverted (c, loc);
                                return null;
                        }
@@ -362,7 +519,7 @@ namespace Mono.CSharp {
                        //
                        // Now apply attributes
                        //
-                       Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc); 
+                       Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name]); 
                        
                        return default_value;
                }
@@ -380,8 +537,9 @@ namespace Mono.CSharp {
                        if (TypeBuilder == null)
                                return false;
                        
-                       EmitContext ec = new EmitContext (parent, this, Location, null,
+                       EmitContext ec = new EmitContext (this, this, Location, null,
                                                          UnderlyingType, ModFlags, false);
+
                        
                        object default_value = 0;
                        
@@ -403,20 +561,24 @@ 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 " +
                                                              "fit in its type");
                                                return false;
                                        }
-                                       
-                                       try {
-                                               default_value = TypeManager.ChangeType (default_value, UnderlyingType);
-                                       } catch {
+
+                                       bool fail;
+                                       default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
+                                       if (fail){
                                                Error_ConstantValueCannotBeConverted (default_value, loc);
                                                return false;
                                        }
@@ -431,13 +593,13 @@ namespace Mono.CSharp {
                                        //
                                        // Apply attributes on the enum member
                                        //
-                                       Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc);
+                                       Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name]);
                                }
 
                                default_value = GetNextDefaultValue (default_value);
                        }
                        
-                       Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
+                       Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
 
                        return true;
                }
@@ -451,6 +613,12 @@ namespace Mono.CSharp {
                        ArrayList members = new ArrayList ();
 
                        if ((mt & MemberTypes.Field) != 0) {
+                               if (criteria is string){
+                                       if (member_to_value [criteria] == null && current_ec != null){
+                                               LookupEnumValue (current_ec, (string) criteria, Location.Null);
+                                       }
+                               }
+                               
                                foreach (FieldBuilder fb in field_builders)
                                        if (filter (fb, criteria) == true)
                                                members.Add (fb);