2004-10-14 Umadevi S <sumadevi@novell.com>
[mono.git] / mcs / gmcs / const.cs
index 693f1ebf1fa51d8bd00689b561a2991c0ecce404..f3fece7deba6cec9581d4c08c1f2cfc5a246ee52 100755 (executable)
@@ -24,13 +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;
 
@@ -47,6 +46,7 @@ namespace Mono.CSharp {
                                new MemberName (name), null, attrs, loc)
                {
                        Expr = expr;
+                       ModFlags |= Modifiers.STATIC;
                }
 
                public FieldAttributes FieldAttr {
@@ -73,14 +73,12 @@ namespace Mono.CSharp {
                /// </summary>
                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);
                        
@@ -92,10 +90,7 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       if (!CheckBase ())
-                               return false;
-
-                       FieldBuilder = Parent.TypeBuilder.DefineField (Name, type, FieldAttr);
+                       FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType, FieldAttr);
 
                        TypeManager.RegisterConstant (FieldBuilder, this);
 
@@ -196,7 +191,7 @@ namespace Mono.CSharp {
                                value = null;
                                return false;
                        }
-                       
+
                        Expr = Expr.Resolve (const_ec);
 
                        in_transit = false;
@@ -222,15 +217,8 @@ namespace Mono.CSharp {
                                        Expr = ch_expr.Expr;
                                else if ((ec_expr != null) && (ec_expr.Child is Constant))
                                        Expr = ec_expr.Child;
-                               else if (Expr is ArrayCreation) {
-                                       ArrayCreation ac = (ArrayCreation) Expr;
-
-                                       Expr = ac.TurnIntoConstant ();
-                                       if (Expr == null){
-                                               Report.Error (150, Location, "A constant value is expected");
-                                               value = null;
-                                               return false;
-                                       }
+                               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");
@@ -241,8 +229,8 @@ namespace Mono.CSharp {
                                ce = Expr as Constant;
                        }
 
-                       if (type != real_expr.Type) {
-                               ce = ChangeType (Location, ce, type);
+                       if (MemberType != real_expr.Type) {
+                               ce = ChangeType (Location, ce, MemberType);
                                if (ce == null){
                                        value = null;
                                        return false;
@@ -251,13 +239,13 @@ namespace Mono.CSharp {
                        }
                        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,