}
public class Const : FieldMember, IConstant {
- Expression Expr;
Constant value;
bool in_transit;
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;
}
// 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 ();
}
// 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;
}
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);
}
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 ()
}
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)
return false;
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;
}
- in_transit = false;
return true;
}
#endregion
}
- public class ExternalConstant: IConstant
+ public class ExternalConstant : IConstant
{
FieldInfo fi;
Constant value;