*** empty log message ***
[mono.git] / mcs / mcs / enum.cs
index bf7a3b1de3444c1dfabe54ff4c003eeff60df881..264dc59f94868ede6e119be6e608c191331a2bc1 100755 (executable)
@@ -2,6 +2,7 @@
 // enum.cs: Enum handling.
 //
 // Author: Miguel de Icaza (miguel@gnu.org)
+//         Ravi Pratap     (ravi@ximian.com)
 //
 // Licensed under the terms of the GNU GPL
 //
@@ -13,24 +14,34 @@ using System.Collections;
 using System.Reflection;
 using System.Reflection.Emit;
 
-namespace CIR {
+namespace Mono.CSharp {
 
+       /// <summary>
+       ///   Enumeration container
+       /// </summary>
        public class Enum : DeclSpace {
-
                ArrayList ordered_enums;
-               public readonly string BaseType;
-               public readonly string EnumName;
-               int mod_flags;
-               public TypeBuilder EnumBuilder;
+               
+               public Expression BaseType;
                public Attributes  OptAttributes;
                
-               Type UnderlyingType;
-
-               public readonly RootContext RootContext;
+               public Type UnderlyingType;
 
                Hashtable member_to_location;
-               ArrayList field_builders;
+               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;
+               
                public const int AllowedModifiers =
                        Modifiers.NEW |
                        Modifiers.PUBLIC |
@@ -38,24 +49,27 @@ namespace CIR {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Enum (RootContext rc, string type, int mod_flags, string name, Attributes attrs, Location l)
-                       : base (name, l)
+               public Enum (TypeContainer parent, Expression type, int mod_flags, string name, Attributes attrs, Location l)
+                       : base (parent, name, l)
                {
-                       RootContext = rc;
                        this.BaseType = type;
-                       this.EnumName = name;
-                       this.mod_flags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC);
+                       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 ();
                }
 
-               // <summary>
-               //   Adds @name to the enumeration space, with @expr
-               //   being its definition.  
-               // </summary>
-               public AdditionResult AddEnumMember (string name, Expression expr, Location loc)
+               /// <summary>
+               ///   Adds @name to the enumeration space, with @expr
+               ///   being its definition.  
+               /// </summary>
+               public AdditionResult AddEnumMember (string name, Expression expr, Location loc,
+                                                    Attributes opt_attrs)
                {
                        if (defined_names.Contains (name))
                                return AdditionResult.NameExists;
@@ -64,60 +78,109 @@ namespace CIR {
 
                        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;
                }
 
-               public void DefineEnum (object parent_builder)
+               //
+               // This is used by corlib compilation: we map from our
+               // type to a type that is consumable by the DefineField
+               //
+               Type MapToInternalType (Type t)
                {
-                       TypeAttributes attr = TypeAttributes.Class | TypeAttributes.Sealed;
+                       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 = Modifiers.TypeAttr (ModFlags, IsTopLevel);
 
-                       UnderlyingType = RootContext.TypeManager.LookupType (BaseType);
+                       attr |= TypeAttributes.Class | TypeAttributes.Sealed;
 
-                       if (UnderlyingType != TypeManager.int32_type && UnderlyingType != TypeManager.uint32_type &&
-                           UnderlyingType != TypeManager.int64_type && UnderlyingType != TypeManager.uint64_type &&
-                           UnderlyingType != TypeManager.short_type && UnderlyingType != TypeManager.ushort_type &&
-                           UnderlyingType != TypeManager.byte_type  && UnderlyingType != TypeManager.sbyte_type) {
+                       UnderlyingType = ResolveType (BaseType, false, Location);
+
+                       if (UnderlyingType != TypeManager.int32_type &&
+                           UnderlyingType != TypeManager.uint32_type &&
+                           UnderlyingType != TypeManager.int64_type &&
+                           UnderlyingType != TypeManager.uint64_type &&
+                           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, long, or ulong expected");
-                               return;
+                                             "Type byte, sbyte, short, char, ushort, int, uint, " +
+                                             "long, or ulong expected (got: " +
+                                             TypeManager.CSharpName (UnderlyingType) + ")");
+                               return null;
                        }
 
-                       if (parent_builder is ModuleBuilder) {
-                               ModuleBuilder builder = (ModuleBuilder) parent_builder;
-
-                               if ((ModFlags & Modifiers.PUBLIC) != 0)
-                                       attr |= TypeAttributes.Public;
-                               else
-                                       attr |= TypeAttributes.NotPublic;
+                       if (IsTopLevel) {
+                               if (TypeManager.NamespaceClash (Name))
+                                       return null;
                                
-                               EnumBuilder = builder.DefineType (EnumName, attr, TypeManager.enum_type);
+                               ModuleBuilder builder = CodeGen.ModuleBuilder;
 
+                               TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
                        } else {
-                               TypeBuilder builder = (TypeBuilder) parent_builder;
+                               TypeBuilder builder = Parent.TypeBuilder;
 
-                               if ((ModFlags & Modifiers.PUBLIC) != 0)
-                                       attr |= TypeAttributes.NestedPublic;
-                               else
-                                       attr |= TypeAttributes.NestedPrivate;
-                               
-                               EnumBuilder = builder.DefineNestedType (EnumName, attr, TypeManager.enum_type);
+                               TypeBuilder = builder.DefineNestedType (
+                                       Basename, attr, TypeManager.enum_type);
                        }
 
-                       EnumBuilder.DefineField ("value__", UnderlyingType,
-                                                FieldAttributes.Public | FieldAttributes.SpecialName);
+                       //
+                       // Call MapToInternalType for corlib
+                       //
+                       TypeBuilder.DefineField ("value__", UnderlyingType,
+                                                FieldAttributes.Public | FieldAttributes.SpecialName
+                                                | FieldAttributes.RTSpecialName);
 
-                       RootContext.TypeManager.AddEnumType (EnumName, EnumBuilder, this);
+                       TypeManager.AddEnumType (Name, TypeBuilder, this);
 
-                       return;
+                       return TypeBuilder;
                }
 
-               bool IsValidEnumLiteral (Expression e)
+               bool IsValidEnumConstant (Expression e)
                {
-                       if (!(e is Literal))
+                       if (!(e is Constant))
                                return false;
 
-                       if (e is IntLiteral || e is UIntLiteral || e is LongLiteral || e is ULongLiteral)
+                       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 CharConstant)
                                return true;
                        else
                                return false;
@@ -186,140 +249,396 @@ namespace CIR {
                        return null;
                }
 
-               void error31 (Literal l, Location loc)
+               void Error_ConstantValueCannotBeConverted (object val, Location loc)
                {
-                       Report.Error (31, loc, "Constant value '" + l.AsString () +
-                                     "' cannot be converted" +
-                                     " to a " + TypeManager.CSharpName (UnderlyingType));
+                       if (val is Constant)
+                               Report.Error (31, loc, "Constant value '" + ((Constant) val).AsString () +
+                                             "' cannot be converted" +
+                                             " to a " + TypeManager.CSharpName (UnderlyingType));
+                       else 
+                               Report.Error (31, loc, "Constant value '" + val +
+                                             "' cannot be converted" +
+                                             " to a " + TypeManager.CSharpName (UnderlyingType));
                        return;
                }
-               
-               public void Populate (TypeContainer tc)
+
+               /// <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)
                {
-                       //
-                       // If there was an error during DefineEnum, return
-                       //
-                       if (EnumBuilder == null){
-                               return;
-                       }
-                       
-                       EmitContext ec = new EmitContext (tc, null, UnderlyingType, ModFlags);
+                       expr_type = TypeManager.TypeToCoreType (expr_type);
+
+                       if (expr_type == TypeManager.void_type)
+                               return false;
                        
-                       object default_value = 0;
+                       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;
+                       }       
                        
-                       FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
-                                            | FieldAttributes.Literal;
+                       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)
+               {
                        
-                       foreach (string name in ordered_enums) {
-                               Expression e = this [name];
-                               Location loc = (Location) member_to_location [name];
+                       object default_value = null;
+                       Constant c = null;
+
+                       default_value = member_to_value [name];
+
+                       if (default_value != null)
+                               return default_value;
+
+                       //
+                       // 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;
+                       }
 
-                               if (e != null) {
-                                       e = Expression.Reduce (ec, e);
+                       //
+                       // So if the above doesn't happen, we have a member that is undefined
+                       // We now proceed to define it 
+                       //
+                       Expression val = this [name];
 
-                                       if (IsValidEnumLiteral (e)) {
-                                               Literal l = (Literal) e;
-                                               default_value = l.GetValue ();
+                       if (val == null) {
+                               
+                               int idx = ordered_enums.IndexOf (name);
+
+                               if (idx == 0)
+                                       default_value = 0;
+                               else {
+                                       for (int i = 0; i < idx; ++i) {
+                                               string n = (string) ordered_enums [i];
+                                               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);
 
-                                               if (default_value == null) {
-                                                       error31 (l, loc);
-                                                       return;
-                                               }
+                                               current_ec = old_ec;
                                                
-                                       } else {
-                                               Report.Error (1008, loc,
-                                                 "Type byte, sbyte, short, ushort, int, uint, long, or ulong expected");
-                                               return;
+                                               in_transit.Remove (name);
+                                               if (default_value == null)
+                                                       return null;
                                        }
+                                       
+                                       default_value = GetNextDefaultValue (default_value);
                                }
                                
-                               FieldBuilder fb = EnumBuilder.DefineField (name, UnderlyingType, attr);
+                       } else {
+                               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 (default_value == null) {
-                                       Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
-                                                     "fit in its type");
-                                       return;
+                               if (val == null)
+                                       return null;
+
+                               if (!IsValidEnumConstant (val)) {
+                                       Report.Error (
+                                               1008, loc,
+                                               "Type byte, sbyte, short, ushort, int, uint, long, or " +
+                                               "ulong expected (have: " + val + ")");
+                                       return null;
                                }
 
-                               try {
-                                       default_value = Convert.ChangeType (default_value, UnderlyingType);
-                               } catch {
-                                       error31 ((Literal) e, loc);
-                                       return;
+                               c = (Constant) val;
+                               default_value = c.GetValue ();
+
+                               if (default_value == null) {
+                                       Error_ConstantValueCannotBeConverted (c, loc);
+                                       return null;
                                }
 
-                               fb.SetConstant (default_value);
-                               field_builders.Add (fb);
+                               if (val is EnumConstant){
+                                       Type etype = TypeManager.EnumToUnderlying (c.Type);
+                                       
+                                       if (!ImplicitConversionExists (etype, UnderlyingType)){
+                                               Convert.Error_CannotImplicitConversion (
+                                                       loc, c.Type, UnderlyingType);
+                                               return null;
+                                       }
+                               }
+                       }
 
-                               if (!TypeManager.RegisterField (fb, default_value))
-                                       return;
+                       FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
+                                       | FieldAttributes.Literal;
+                       
+                       FieldBuilder fb = TypeBuilder.DefineField (name, TypeBuilder, attr);
 
-                               default_value = GetNextDefaultValue (default_value);
+                       bool fail;
+                       default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
+                       if (fail){
+                               Error_ConstantValueCannotBeConverted (c, loc);
+                               return null;
                        }
 
-                       if (OptAttributes == null)
-                               return;
+                       fb.SetConstant (default_value);
+                       field_builders.Add (fb);
+                       member_to_value [name] = default_value;
+
+                       if (!TypeManager.RegisterFieldValue (fb, default_value))
+                               return null;
+
+                       //
+                       // Now apply attributes
+                       //
+                       Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name]); 
+                       
+                       return default_value;
+               }
+
+               public override bool DefineMembers (TypeContainer parent)
+               {
+                       return true;
+               }
+               
+               public override bool Define (TypeContainer parent)
+               {
+                       //
+                       // If there was an error during DefineEnum, return
+                       //
+                       if (TypeBuilder == null)
+                               return false;
+                       
+                       EmitContext ec = new EmitContext (this, this, Location, null,
+                                                         UnderlyingType, ModFlags, false);
+
                        
-                       if (OptAttributes.AttributeSections == null)
-                               return;
+                       object default_value = 0;
+                       
+                       FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
+                                            | FieldAttributes.Literal;
+
                        
-                       foreach (AttributeSection asec in OptAttributes.AttributeSections) {
-                               if (asec.Attributes == null)
+                       foreach (string name in ordered_enums) {
+                               //
+                               // Have we already been defined, thanks to some cross-referencing ?
+                               // 
+                               if (member_to_value.Contains (name))
                                        continue;
                                
-                               foreach (Attribute a in asec.Attributes) {
-                                       CustomAttributeBuilder cb = a.Resolve (ec);
+                               Location loc = (Mono.CSharp.Location) member_to_location [name];
+
+                               if (this [name] != null) {
+                                       default_value = LookupEnumValue (ec, name, loc);
+
+                                       if (default_value == null)
+                                               return true;
+                               } else {
+                                       if (name == "value__"){
+                                               Report.Error (76, loc, "The name `value__' is reserved for enumerations");
+                                               return false;
+                                       }
 
-                                       if (cb == null)
-                                               continue;
+                                       FieldBuilder fb = TypeBuilder.DefineField (
+                                               name, TypeBuilder, attr);
                                        
-                                       EnumBuilder.SetCustomAttribute (cb);
+                                       if (default_value == null) {
+                                          Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
+                                                             "fit in its type");
+                                               return false;
+                                       }
+
+                                       bool fail;
+                                       default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
+                                       if (fail){
+                                               Error_ConstantValueCannotBeConverted (default_value, loc);
+                                               return false;
+                                       }
+
+                                       fb.SetConstant (default_value);
+                                       field_builders.Add (fb);
+                                       member_to_value [name] = default_value;
+                                       
+                                       if (!TypeManager.RegisterFieldValue (fb, default_value))
+                                               return false;
+
+                                       //
+                                       // Apply attributes on the enum member
+                                       //
+                                       Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name]);
                                }
+
+                               default_value = GetNextDefaultValue (default_value);
                        }
+                       
+                       Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
+
+                       return true;
                }
                
                //
-               // 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 ();
 
                        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);
                        }
 
-                       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 void CloseEnum ()
-               {
-                       EnumBuilder.CreateType ();
-               }
-               
                public ArrayList ValueNames {
                        get {
                                return ordered_enums;
                        }
                }
 
-               public int ModFlags {
-                       get {
-                               return mod_flags;
-                       }
-               }
-               
                // indexer
                public Expression this [string name] {
                        get {