- throw new Exception (
- String.Format ("LookupConstantValue: This should never be reached {0} {1}", Type, type));
- }
-
- Constant retval;
- if (type == TypeManager.int32_type)
- retval = new IntConstant ((int) constant_value, loc);
- else if (type == TypeManager.uint32_type)
- retval = new UIntConstant ((uint) constant_value, loc);
- else if (type == TypeManager.int64_type)
- retval = new LongConstant ((long) constant_value, loc);
- else if (type == TypeManager.uint64_type)
- retval = new ULongConstant ((ulong) constant_value, loc);
- else if (type == TypeManager.float_type)
- retval = new FloatConstant ((float) constant_value, loc);
- else if (type == TypeManager.double_type)
- retval = new DoubleConstant ((double) constant_value, loc);
- else if (type == TypeManager.string_type)
- retval = new StringConstant ((string) constant_value, loc);
- else if (type == TypeManager.short_type)
- retval = new ShortConstant ((short) constant_value, loc);
- else if (type == TypeManager.ushort_type)
- retval = new UShortConstant ((ushort) constant_value, loc);
- else if (type == TypeManager.sbyte_type)
- retval = new SByteConstant ((sbyte) constant_value, loc);
- else if (type == TypeManager.byte_type)
- retval = new ByteConstant ((byte) constant_value, loc);
- else if (type == TypeManager.char_type)
- retval = new CharConstant ((char) constant_value, loc);
- else if (type == TypeManager.bool_type)
- retval = new BoolConstant ((bool) constant_value, loc);
- else if (type == TypeManager.decimal_type)
- retval = new DecimalConstant ((decimal) constant_value, loc);
- else
- throw new Exception ("LookupConstantValue: Unhandled constant type: " + type);
-
- return retval;
- }
-
- protected static void CheckRange (bool inCheckedContext, ulong value, ulong max)
- {
- if (!inCheckedContext)
- return;
-
- if (value > max)
- throw new OverflowException ();
+ throw new InternalErrorException ("Missing constant conversion between `{0}' and `{1}'",
+ TypeManager.CSharpName (Type), TypeManager.CSharpName (type));
+ }
+
+ return CreateConstant (type, constant_value, loc);
+ }
+
+ /// Returns a constant instance based on Type
+ /// The returned value is already resolved.
+ public static Constant CreateConstant (Type t, object v, Location loc)
+ {
+ if (t == TypeManager.int32_type)
+ return new IntConstant ((int) v, loc);
+ if (t == TypeManager.string_type)
+ return new StringConstant ((string) v, loc);
+ if (t == TypeManager.uint32_type)
+ return new UIntConstant ((uint) v, loc);
+ if (t == TypeManager.int64_type)
+ return new LongConstant ((long) v, loc);
+ if (t == TypeManager.uint64_type)
+ return new ULongConstant ((ulong) v, loc);
+ if (t == TypeManager.float_type)
+ return new FloatConstant ((float) v, loc);
+ if (t == TypeManager.double_type)
+ return new DoubleConstant ((double) v, loc);
+ if (t == TypeManager.short_type)
+ return new ShortConstant ((short)v, loc);
+ if (t == TypeManager.ushort_type)
+ return new UShortConstant ((ushort)v, loc);
+ if (t == TypeManager.sbyte_type)
+ return new SByteConstant ((sbyte)v, loc);
+ if (t == TypeManager.byte_type)
+ return new ByteConstant ((byte)v, loc);
+ if (t == TypeManager.char_type)
+ return new CharConstant ((char)v, loc);
+ if (t == TypeManager.bool_type)
+ return new BoolConstant ((bool) v, loc);
+ if (t == TypeManager.decimal_type)
+ return new DecimalConstant ((decimal) v, loc);
+ if (TypeManager.IsEnumType (t)) {
+ Type real_type = TypeManager.GetEnumUnderlyingType (t);
+ return new EnumConstant (CreateConstant (real_type, v, loc), t);
+ }
+ if (v == null && !TypeManager.IsValueType (t))
+ return new EmptyConstantCast (new NullLiteral (loc), t);
+
+ throw new Exception ("Unknown type for constant (" + t +
+ "), details: " + v);
+ }
+
+ public override Expression CreateExpressionTree (EmitContext ec)
+ {
+ ArrayList args = new ArrayList (2);
+ args.Add (new Argument (this));
+ args.Add (new Argument (
+ new TypeOf (new TypeExpression (type, loc), loc)));
+
+ return CreateExpressionFactoryCall ("Constant", args);