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;
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 {
/// <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);
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);
value = null;
return false;
}
-
+
Expr = Expr.Resolve (const_ec);
in_transit = false;
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");
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;
}
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,
/// <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);
OptAttributes.Emit (const_ec, this);
}
- base.Emit (parent);
+ base.Emit ();
}
}
}