// Copyright 2003-2008 Novell, Inc.
//
-using System;
+#if STATIC
+using IKVM.Reflection;
+#else
using System.Reflection;
-using System.Reflection.Emit;
+#endif
namespace Mono.CSharp {
public class Const : FieldBase
{
- public const Modifiers AllowedModifiers =
+ const Modifiers AllowedModifiers =
Modifiers.NEW |
Modifiers.PUBLIC |
Modifiers.PROTECTED |
Modifiers.INTERNAL |
Modifiers.PRIVATE;
- public Const (DeclSpace parent, FullNamedExpression type, Modifiers mod_flags, MemberName name, Attributes attrs)
+ public Const (TypeDefinition parent, FullNamedExpression type, Modifiers mod_flags, MemberName name, Attributes attrs)
: base (parent, type, mod_flags, AllowedModifiers, name, attrs)
{
ModFlags |= Modifiers.STATIC;
FieldAttributes field_attr = FieldAttributes.Static | ModifiersExtensions.FieldAttr (ModFlags);
// Decimals cannot be emitted into the constant blob. So, convert to 'readonly'.
- if (member_type == TypeManager.decimal_type) {
+ if (member_type.BuiltinType == BuiltinTypeSpec.Type.Decimal) {
field_attr |= FieldAttributes.InitOnly;
} else {
field_attr |= FieldAttributes.Literal;
if ((field_attr & FieldAttributes.InitOnly) != 0)
Parent.PartialContainer.RegisterFieldForInitialization (this,
- new FieldInitializer (spec, initializer, this));
+ new FieldInitializer (this, initializer, Location));
if (declarators != null) {
var t = new TypeExpression (MemberType, TypeExpression.Location);
- int index = Parent.PartialContainer.Constants.IndexOf (this);
foreach (var d in declarators) {
var c = new Const (Parent, t, ModFlags & ~Modifiers.STATIC, new MemberName (d.Name.Value, d.Name.Location), OptAttributes);
c.initializer = d.Initializer;
((ConstInitializer) c.initializer).Name = d.Name.Value;
- Parent.PartialContainer.Constants.Insert (++index, c);
+ c.Define ();
+ Parent.PartialContainer.Members.Add (c);
}
}
public override void Emit ()
{
var c = ((ConstSpec) spec).Value as Constant;
- if (c.Type == TypeManager.decimal_type) {
- FieldBuilder.SetCustomAttribute (CreateDecimalConstantAttribute (c, Compiler.PredefinedAttributes));
+ if (c.Type.BuiltinType == BuiltinTypeSpec.Type.Decimal) {
+ Module.PredefinedAttributes.DecimalConstant.EmitAttribute (FieldBuilder, (decimal) c.GetValue (), c.Location);
} else {
- FieldBuilder.SetConstant (c.GetTypedValue ());
+ FieldBuilder.SetConstant (c.GetValue ());
}
base.Emit ();
}
- public static CustomAttributeBuilder CreateDecimalConstantAttribute (Constant c, PredefinedAttributes pa)
- {
- PredefinedAttribute attr = pa.DecimalConstant;
- if (attr.Constructor == null &&
- !attr.ResolveConstructor (c.Location, TypeManager.byte_type, TypeManager.byte_type,
- TypeManager.uint32_type, TypeManager.uint32_type, TypeManager.uint32_type))
- return null;
-
- Decimal d = (Decimal) c.GetValue ();
- int [] bits = Decimal.GetBits (d);
- object [] args = new object [] {
- (byte) (bits [3] >> 16),
- (byte) (bits [3] >> 31),
- (uint) bits [2], (uint) bits [1], (uint) bits [0]
- };
-
- return new CustomAttributeBuilder (attr.Constructor, args);
- }
-
public static void Error_InvalidConstantType (TypeSpec t, Location loc, Report Report)
{
if (t.IsGenericParameter) {
Report.Error (1959, loc,
- "Type parameter `{0}' cannot be declared const", TypeManager.CSharpName (t));
+ "Type parameter `{0}' cannot be declared const", t.GetSignatureForError ());
} else {
Report.Error (283, loc,
- "The type `{0}' cannot be declared const", TypeManager.CSharpName (t));
+ "The type `{0}' cannot be declared const", t.GetSignatureForError ());
}
}
+
+ public override void Accept (StructuralVisitor visitor)
+ {
+ visitor.Visit (this);
+ }
+
+ public override void PrepareEmit ()
+ {
+ base.PrepareEmit ();
+ DefineValue ();
+ }
}
public class ConstSpec : FieldSpec
c = field.ConvertInitializer (rc, c);
if (c == null) {
- if (TypeManager.IsReferenceType (field.MemberType))
+ if (TypeSpec.IsReferenceType (field.MemberType))
Error_ConstantCanBeInitializedWithNullOnly (rc, field.MemberType, expr.Location, GetSignatureForError ());
else if (!(expr is Constant))
Error_ExpressionMustBeConstant (rc, expr.Location, GetSignatureForError ());
else
- expr.Error_ValueCannotBeConverted (rc, expr.Location, field.MemberType, false);
+ expr.Error_ValueCannotBeConverted (rc, field.MemberType, false);
}
expr = c;
return field.Parent.GetSignatureForError () + "." + Name;
}
+
+ public override object Accept (StructuralVisitor visitor)
+ {
+ return visitor.Visit (this);
+ }
}
}