2002-09-16 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / enum.cs
index 98deac653ed791f3976c01f0cf4da1ae207b7290..691fcbe6b1291424181587d730f6e8bcda8301a1 100755 (executable)
@@ -20,19 +20,25 @@ namespace Mono.CSharp {
        ///   Enumeration container
        /// </summary>
        public class Enum : DeclSpace {
-
                ArrayList ordered_enums;
-               public readonly string BaseType;
+               
+               public Expression BaseType;
                public Attributes  OptAttributes;
                
                public Type UnderlyingType;
 
                Hashtable member_to_location;
+               Hashtable member_to_attributes;
 
                //
                // This is for members that have been defined
                //
                Hashtable member_to_value;
+
+               //
+               // This is used to mark members we're currently defining
+               //
+               Hashtable in_transit;
                
                ArrayList field_builders;
                
@@ -43,16 +49,18 @@ namespace Mono.CSharp {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Enum (TypeContainer parent, string type, int mod_flags, string name, Attributes attrs, Location l)
+               public Enum (TypeContainer parent, Expression type, int mod_flags, string name, Attributes attrs, Location l)
                        : base (parent, name, l)
                {
                        this.BaseType = type;
-                       ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC, l);
+                       ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
+                                                   IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE, l);
                        OptAttributes = attrs;
 
                        ordered_enums = new ArrayList ();
                        member_to_location = new Hashtable ();
                        member_to_value = new Hashtable ();
+                       in_transit = new Hashtable ();
                        field_builders = new ArrayList ();
                }
 
@@ -60,7 +68,8 @@ namespace Mono.CSharp {
                ///   Adds @name to the enumeration space, with @expr
                ///   being its definition.  
                /// </summary>
-               public AdditionResult AddEnumMember (string name, Expression expr, Location loc)
+               public AdditionResult AddEnumMember (string name, Expression expr, Location loc,
+                                                    Attributes opt_attrs)
                {
                        if (defined_names.Contains (name))
                                return AdditionResult.NameExists;
@@ -69,19 +78,58 @@ namespace Mono.CSharp {
 
                        ordered_enums.Add (name);
                        member_to_location.Add (name, loc);
+
+                       if (member_to_attributes == null)
+                               member_to_attributes = new Hashtable ();
+
+                       member_to_attributes.Add (name, opt_attrs);
                        
                        return AdditionResult.Success;
                }
 
+               //
+               // This is used by corlib compilation: we map from our
+               // type to a type that is consumable by the DefineField
+               //
+               Type MapToInternalType (Type t)
+               {
+                       if (t == TypeManager.int32_type)
+                               return typeof (int);
+                       if (t == TypeManager.int64_type)
+                               return typeof (long);
+                       if (t == TypeManager.uint32_type)
+                               return typeof (uint);
+                       if (t == TypeManager.uint64_type)
+                               return typeof (ulong);
+                       if (t == TypeManager.float_type)
+                               return typeof (float);
+                       if (t == TypeManager.double_type)
+                               return typeof (double);
+                       if (t == TypeManager.byte_type)
+                               return typeof (byte);
+                       if (t == TypeManager.sbyte_type)
+                               return typeof (sbyte);
+                       if (t == TypeManager.char_type)
+                               return typeof (char);
+                       if (t == TypeManager.short_type)
+                               return typeof (short);
+                       if (t == TypeManager.ushort_type)
+                               return typeof (ushort);
+
+                       throw new Exception ();
+               }
+               
                public override TypeBuilder DefineType ()
                {
                        if (TypeBuilder != null)
                                return TypeBuilder;
-                       
-                       TypeAttributes attr = TypeAttributes.Class | TypeAttributes.Sealed;
 
-                       UnderlyingType = TypeManager.LookupType (BaseType);
-                       
+                       TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel);
+
+                       attr |= TypeAttributes.Class | TypeAttributes.Sealed;
+
+                       UnderlyingType = ResolveType (BaseType, false, Location);
+
                        if (UnderlyingType != TypeManager.int32_type &&
                            UnderlyingType != TypeManager.uint32_type &&
                            UnderlyingType != TypeManager.int64_type &&
@@ -100,25 +148,17 @@ namespace Mono.CSharp {
                        if (IsTopLevel) {
                                ModuleBuilder builder = CodeGen.ModuleBuilder;
 
-                               if ((ModFlags & Modifiers.PUBLIC) != 0)
-                                       attr |= TypeAttributes.Public;
-                               else
-                                       attr |= TypeAttributes.NotPublic;
-                               
                                TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
                        } else {
                                TypeBuilder builder = Parent.TypeBuilder;
 
-                               if ((ModFlags & Modifiers.PUBLIC) != 0)
-                                       attr |= TypeAttributes.NestedPublic;
-                               else
-                                       attr |= TypeAttributes.NestedPrivate;
-
-                               
                                TypeBuilder = builder.DefineNestedType (
                                        Basename, attr, TypeManager.enum_type);
                        }
 
+                       //
+                       // Call MapToInternalType for corlib
+                       //
                        TypeBuilder.DefineField ("value__", UnderlyingType,
                                                 FieldAttributes.Public | FieldAttributes.SpecialName
                                                 | FieldAttributes.RTSpecialName);
@@ -217,6 +257,129 @@ 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;
+               }
+
                /// <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
@@ -231,9 +394,16 @@ namespace Mono.CSharp {
                        if (default_value != null)
                                return default_value;
 
-                       if (!defined_names.Contains (name)) {
-                               Report.Error (117, loc, "'"+ Name + "' does not contain a definition for '"
-                                             + name + "'");
+                       //
+                       // This may happen if we're calling a method in System.Enum, for instance
+                       // Enum.IsDefined().
+                       //
+                       if (!defined_names.Contains (name))
+                               return null;
+
+                       if (in_transit.Contains (name)) {
+                               Report.Error (110, loc, "The evaluation of the constant value for `" +
+                                             Name + "." + name + "' involves a circular definition.");
                                return null;
                        }
 
@@ -254,45 +424,61 @@ namespace Mono.CSharp {
                                                string n = (string) ordered_enums [i];
                                                Location m_loc = (Mono.CSharp.Location)
                                                        member_to_location [n];
+                                               in_transit.Add (name, true);
                                                default_value = LookupEnumValue (ec, n, m_loc);
+                                               in_transit.Remove (name);
+                                               if (default_value == null)
+                                                       return null;
                                        }
                                        
                                        default_value = GetNextDefaultValue (default_value);
                                }
                                
                        } else {
+                               bool old = ec.InEnumContext;
+                               ec.InEnumContext = true;
+                               in_transit.Add (name, true);
                                val = val.Resolve (ec);
-                               
-                               if (val == null) {
-                                       Report.Error (-12, loc, "Definition is circular.");
+                               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)){
+                                               Expression.Error_CannotConvertImplicit (
+                                                       loc, c.Type, UnderlyingType);
+                                               return null;
+                                       }
+                               }
                        }
 
                        FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
                                        | FieldAttributes.Literal;
                        
                        FieldBuilder fb = TypeBuilder.DefineField (name, UnderlyingType, attr);
-                       
+
                        try {
-                               default_value = Convert.ChangeType (default_value, UnderlyingType);
+                               default_value = TypeManager.ChangeType (default_value, UnderlyingType);
                        } catch {
                                Error_ConstantValueCannotBeConverted (c, loc);
                                return null;
@@ -304,9 +490,19 @@ namespace Mono.CSharp {
 
                        if (!TypeManager.RegisterFieldValue (fb, default_value))
                                return null;
+
+                       //
+                       // Now apply attributes
+                       //
+                       Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc); 
                        
                        return default_value;
                }
+
+               public override bool DefineMembers (TypeContainer parent)
+               {
+                       return true;
+               }
                
                public override bool Define (TypeContainer parent)
                {
@@ -351,7 +547,7 @@ namespace Mono.CSharp {
                                        }
                                        
                                        try {
-                                               default_value = Convert.ChangeType (default_value, UnderlyingType);
+                                               default_value = TypeManager.ChangeType (default_value, UnderlyingType);
                                        } catch {
                                                Error_ConstantValueCannotBeConverted (default_value, loc);
                                                return false;
@@ -363,6 +559,11 @@ namespace Mono.CSharp {
                                        
                                        if (!TypeManager.RegisterFieldValue (fb, default_value))
                                                return false;
+
+                                       //
+                                       // Apply attributes on the enum member
+                                       //
+                                       Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc);
                                }
 
                                default_value = GetNextDefaultValue (default_value);
@@ -374,10 +575,10 @@ namespace Mono.CSharp {
                }
                
                //
-               // Hack around System.Reflection as found everywhere else
+               // IMemberFinder
                //
-               public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf,
-                                                 MemberFilter filter, object criteria)
+               public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
+                                                       MemberFilter filter, object criteria)
                {
                        ArrayList members = new ArrayList ();
 
@@ -387,15 +588,13 @@ namespace Mono.CSharp {
                                                members.Add (fb);
                        }
 
-                       int count = members.Count;
+                       return new MemberList (members);
+               }
 
-                       if (count > 0) {
-                               MemberInfo [] mi = new MemberInfo [count];
-                               members.CopyTo (mi, 0);
-                               return mi;
+               public override MemberCache MemberCache {
+                       get {
+                               return null;
                        }
-
-                       return null;
                }
 
                public ArrayList ValueNames {