//
//
+//
+// This is needed because the following situation arises:
+//
+// The FieldBuilder is declared with the real type for an enumeration
+//
+// When we attempt to set the value for the constant, the FieldBuilder.SetConstant
+// function aborts because it requires its argument to be of the same type
+//
+
namespace Mono.CSharp {
using System;
using System.Reflection.Emit;
using System.Collections;
-
public class Const : MemberCore {
public readonly string ConstantType;
public Expression Expr;
public Attributes OptAttributes;
public FieldBuilder FieldBuilder;
+
object ConstantValue = null;
Type type;
ConstantType = constant_type;
Name = name;
Expr = expr;
- ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE);
+ ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE, loc);
OptAttributes = attrs;
}
}
}
+#if DEBUG
+ void dump_tree (Type t)
+ {
+ Console.WriteLine ("Dumping hierarchy");
+ while (t != null){
+ Console.WriteLine (" " + t.FullName + " " +
+ (t.GetType ().IsEnum ? "yes" : "no"));
+ t = t.BaseType;
+ }
+ }
+#endif
+
/// <summary>
/// Defines the constant in the @parent
/// </summary>
if (type == null)
return false;
-
+
if (!TypeManager.IsBuiltinType (type) &&
(!type.IsSubclassOf (TypeManager.enum_type))) {
Report.Error (
Report.Error (150, Location, "A constant value is expected");
return null;
}
-
- Expr = Expression.Reduce (ec, Expr);
- if (!(Expr is Literal)) {
+ if (!(Expr is Constant)) {
Report.Error (150, Location, "A constant value is expected");
return null;
}
- ConstantValue = ((Literal) Expr).GetValue ();
+ ConstantValue = ((Constant) Expr).GetValue ();
+
+ if (type.IsEnum){
+ //
+ // This sadly does not work for our user-defined enumerations types ;-(
+ //
+ ConstantValue = System.Enum.ToObject (
+ type, ConstantValue);
+ }
FieldBuilder.SetConstant (ConstantValue);
- if (!TypeManager.RegisterField (FieldBuilder, ConstantValue))
+ if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
return null;
return ConstantValue;
/// </summary>
public void EmitConstant (TypeContainer parent)
{
- if (FieldBuilder == null)
- return;
-
EmitContext ec = new EmitContext (parent, Location, null, type, ModFlags);
LookupConstantValue (ec);