2004-10-14 Umadevi S <sumadevi@novell.com>
[mono.git] / mcs / gmcs / const.cs
index 992104a670250baa791ab982fab6fd965357080d..f3fece7deba6cec9581d4c08c1f2cfc5a246ee52 100755 (executable)
@@ -24,12 +24,12 @@ namespace Mono.CSharp {
        using System.Reflection.Emit;
        using System.Collections;
 
-       public class Const : FieldBase {
+       public class Const : FieldMember {
                public Expression Expr;
                EmitContext const_ec;
 
+               bool resolved = false;
                object ConstantValue = null;
-               Type type;
 
                bool in_transit = false;
 
@@ -40,12 +40,13 @@ namespace Mono.CSharp {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Const (Expression constant_type, string name, Expression expr,
-                             int mod_flags, Attributes attrs, Location loc)
-                       : base (constant_type, mod_flags, AllowedModifiers,
+               public Const (TypeContainer parent, Expression constant_type, string name,
+                             Expression expr, int mod_flags, Attributes attrs, Location loc)
+                       : base (parent, constant_type, mod_flags, AllowedModifiers,
                                new MemberName (name), null, attrs, loc)
                {
                        Expr = expr;
+                       ModFlags |= Modifiers.STATIC;
                }
 
                public FieldAttributes FieldAttr {
@@ -70,16 +71,14 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Defines the constant in the @parent
                /// </summary>
-               public override bool Define (TypeContainer parent)
+               public override bool Define ()
                {
-                       type = parent.ResolveType (Type, false, Location);
-
-                       if (type == null)
+                       if (!base.Define ())
                                return false;
 
-                       const_ec = new EmitContext (parent, Location, null, type, ModFlags);
-                       
-                       Type ttype = type;
+                       const_ec = new EmitContext (Parent, Location, null, MemberType, ModFlags);
+
+                       Type ttype = MemberType;
                        while (ttype.IsArray)
                            ttype = TypeManager.GetElementType (ttype);
                        
@@ -91,21 +90,7 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       Type ptype = parent.TypeBuilder.BaseType;
-
-                       if (ptype != null) {
-                               MemberList list = TypeContainer.FindMembers (
-                                       ptype, MemberTypes.Field, BindingFlags.Public,
-                                       System.Type.FilterName, Name);
-                               
-                               if (list.Count == 0)
-                                       if ((ModFlags & Modifiers.NEW) != 0)
-                                               WarningNotHiding (parent);
-
-                       } else if ((ModFlags & Modifiers.NEW) != 0)
-                               WarningNotHiding (parent);
-
-                       FieldBuilder = parent.TypeBuilder.DefineField (Name, type, FieldAttr);
+                       FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType, FieldAttr);
 
                        TypeManager.RegisterConstant (FieldBuilder, this);
 
@@ -118,6 +103,9 @@ namespace Mono.CSharp {
                //
                public static Constant ChangeType (Location loc, Constant expr, Type type)
                {
+                       if (type == TypeManager.object_type)
+                               return expr;
+
                        bool fail;
 
                        // from the null type to any reference-type.
@@ -178,16 +166,19 @@ namespace Mono.CSharp {
                ///  Looks up the value of a constant field. Defines it if it hasn't
                ///  already been. Similar to LookupEnumValue in spirit.
                /// </summary>
-               public object LookupConstantValue ()
+               public bool LookupConstantValue (out object value)
                {
-                       if (ConstantValue != null)
-                               return ConstantValue;
+                       if (resolved) {
+                               value = ConstantValue;
+                               return true;
+                       }
 
                        if (in_transit) {
                                Report.Error (110, Location,
                                              "The evaluation of the constant value for `" +
                                              Name + "' involves a circular definition.");
-                               return null;
+                               value = null;
+                               return false;
                        }
 
                        in_transit = true;
@@ -196,9 +187,11 @@ namespace Mono.CSharp {
                        //
                        // We might have cleared Expr ourselves in a recursive definition
                        //
-                       if (Expr == null)
-                               return null;
-                       
+                       if (Expr == null){
+                               value = null;
+                               return false;
+                       }
+
                        Expr = Expr.Resolve (const_ec);
 
                        in_transit = false;
@@ -206,48 +199,53 @@ namespace Mono.CSharp {
                        if (Expr == null) {
                                if (errors == Report.Errors)
                                        Report.Error (150, Location, "A constant value is expected");
-                               return null;
+                               value = null;
+                               return false;
                        }
 
+                       Expression real_expr = Expr;
+
                        Constant ce = Expr as Constant;
                        if (ce == null){
                                UnCheckedExpr un_expr = Expr as UnCheckedExpr;
                                CheckedExpr ch_expr = Expr as CheckedExpr;
+                               EmptyCast ec_expr = Expr as EmptyCast;
 
                                if ((un_expr != null) && (un_expr.Expr is Constant))
                                        Expr = un_expr.Expr;
                                else if ((ch_expr != null) && (ch_expr.Expr is Constant))
                                        Expr = ch_expr.Expr;
-                               else if (Expr is ArrayCreation) {
-                                       ArrayCreation ac = (ArrayCreation) Expr;
-
-                                       Expr = ac.TurnIntoConstant ();
-                                       if (Expr == null){
-                                               Report.Error (150, Location, "A constant value is expected");
-                                               return null;
-                                       }
+                               else if ((ec_expr != null) && (ec_expr.Child is Constant))
+                                       Expr = ec_expr.Child;
+                               else if (Expr is ArrayCreation){
+                                       Report.Error (133, Location, "Arrays can not be constant");
                                } else {
                                        if (errors == Report.Errors)
                                                Report.Error (150, Location, "A constant value is expected");
-                                       return null;
+                                       value = null;
+                                       return false;
                                }
+
+                               ce = Expr as Constant;
                        }
 
-                       if (type != ce.Type) {
-                               ce = ChangeType (Location, ce, type);
-                               if (ce == null)
-                                       return null;
+                       if (MemberType != real_expr.Type) {
+                               ce = ChangeType (Location, ce, MemberType);
+                               if (ce == null){
+                                       value = null;
+                                       return false;
+                               }
                                Expr = ce;
                        }
                        ConstantValue = ce.GetValue ();
 
-                       if (type.IsEnum){
+                       if (MemberType.IsEnum){
                                //
                                // This sadly does not work for our user-defined enumerations types ;-(
                                //
                                try {
                                        ConstantValue = System.Enum.ToObject (
-                                               type, ConstantValue);
+                                               MemberType, ConstantValue);
                                } catch (ArgumentException){
                                        Report.Error (
                                                -16, Location,
@@ -259,20 +257,27 @@ namespace Mono.CSharp {
                        FieldBuilder.SetConstant (ConstantValue);
 
                        if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
-                               return null;
+                               throw new Exception ("Cannot register const value");
 
-                       return ConstantValue;
+                       value = ConstantValue;
+                       resolved = true;
+                       return true;
                }
                
                
                /// <summary>
                ///  Emits the field value by evaluating the expression
                /// </summary>
-               public void Emit (TypeContainer parent)
+               public override void Emit ()
                {
-                       LookupConstantValue ();
-                       
-                       return;
+                       object value;
+                       LookupConstantValue (out value);
+
+                       if (OptAttributes != null) {
+                               OptAttributes.Emit (const_ec, this);
+                       }
+
+                       base.Emit ();
                }
        }
 }