2004-10-14 Umadevi S <sumadevi@novell.com>
[mono.git] / mcs / gmcs / const.cs
index 79ae07b7716a62d744cf6bdb069398bd2bd6726c..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;
 
@@ -41,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 {
@@ -71,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);
                        
@@ -92,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);
 
@@ -207,7 +191,7 @@ namespace Mono.CSharp {
                                value = null;
                                return false;
                        }
-                       
+
                        Expr = Expr.Resolve (const_ec);
 
                        in_transit = false;
@@ -233,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");
@@ -252,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;
@@ -262,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,
@@ -291,7 +268,7 @@ namespace Mono.CSharp {
                /// <summary>
                ///  Emits the field value by evaluating the expression
                /// </summary>
-               public override void Emit (TypeContainer parent)
+               public override void Emit ()
                {
                        object value;
                        LookupConstantValue (out value);
@@ -300,7 +277,7 @@ namespace Mono.CSharp {
                                OptAttributes.Emit (const_ec, this);
                        }
 
-                       base.Emit (parent);
+                       base.Emit ();
                }
        }
 }