* XmlTypeAttribute.cs: added property AnonymousType for 2.0
[mono.git] / mcs / mcs / const.cs
index e639f9b559f294748c551dafe363257039ba2189..ef9f2b2888ff27247d642f8d54faa7ed9e89e7f7 100644 (file)
@@ -24,7 +24,6 @@ namespace Mono.CSharp {
        }
 
        public class Const : FieldMember, IConstant {
-               Expression Expr;
                Constant value;
                bool in_transit;
 
@@ -35,12 +34,12 @@ namespace Mono.CSharp {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Const (TypeContainer parent, Expression constant_type, string name,
+               public Const (DeclSpace 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, loc), expr, attrs)
+                               new MemberName (name, loc), attrs)
                {
-                       Expr = expr;
+                       initializer = expr;
                        ModFlags |= Modifiers.STATIC;
                }
 
@@ -49,7 +48,7 @@ namespace Mono.CSharp {
                        // Constant.Define can be called when the parent type hasn't yet been populated
                        // and it's base types need not have been populated.  So, we defer this check
                        // to the second time Define () is called on this member.
-                       if (Parent.BaseCache == null)
+                       if (Parent.PartialContainer.BaseCache == null)
                                return true;
                        return base.CheckBase ();
                }
@@ -74,9 +73,8 @@ namespace Mono.CSharp {
                        // Decimals cannot be emitted into the constant blob.  So, convert to 'readonly'.
                        if (ttype == TypeManager.decimal_type) {
                                field_attr |= FieldAttributes.InitOnly;
-                               Parent.RegisterFieldForInitialization (this);
-                       }
-                       else {
+                               Parent.PartialContainer.RegisterFieldForInitialization (this);
+                       } else {
                                field_attr |= FieldAttributes.Literal;
                        }
 
@@ -109,8 +107,13 @@ namespace Mono.CSharp {
                        base.Emit ();
                }
 
-               public static void Error_ExpressionMustBeConstant (Location loc, string e_name)
+               public static void Error_ExpressionMustBeConstant (Type constantType, Location loc, string e_name)
                {
+                       if (constantType != null && TypeManager.IsValueType (constantType) &&
+                               !TypeManager.IsBuiltinOrEnum (constantType)) {
+                               Report.Error (283, loc, "The type `{0}' cannot be declared const", TypeManager.CSharpName (constantType));
+                               return;
+                       }
                        Report.Error (133, loc, "The expression being assigned to `{0}' must be constant", e_name);
                }
 
@@ -120,6 +123,12 @@ namespace Mono.CSharp {
                                mc.GetSignatureForError ());
                }
 
+               public static void Error_ConstantCanBeInitializedWithNullOnly (Location loc, string name)
+               {
+                       Report.Error (134, loc, "`{0}': the constant of reference type other than string can only be initialized with null",
+                               name);
+               }
+
                #region IConstant Members
 
                public bool ResolveValue ()
@@ -136,8 +145,9 @@ namespace Mono.CSharp {
                        }
 
                        in_transit = true;
-                       EmitContext ec = new EmitContext (Parent, Location, null, MemberType, ModFlags);
-                       value = Expr.ResolveAsConstant (ec, this);
+                       // TODO: IResolveContext here
+                       EmitContext ec = new EmitContext (this, Parent, Location, null, MemberType, ModFlags);
+                       value = initializer.ResolveAsConstant (ec, this);
                        in_transit = false;
 
                        if (value == null)
@@ -148,8 +158,7 @@ namespace Mono.CSharp {
                                return false;
 
                        if (!MemberType.IsValueType && MemberType != TypeManager.string_type && !value.IsDefaultValue) {
-                               Report.Error (134, Location, "`{0}': A const of reference other than string can only be initialized with null",
-                                       GetSignatureForError ());
+                               Error_ConstantCanBeInitializedWithNullOnly (Location, GetSignatureForError ());
                                return false;
                        }
 
@@ -165,7 +174,7 @@ namespace Mono.CSharp {
                #endregion
        }
 
-       public class ExternalConstant: IConstant
+       public class ExternalConstant : IConstant
        {
                FieldInfo fi;
                Constant value;