return this.GetType ().Name + " (" + AsString () + ")";
}
- public override bool GetAttributableValue (ResolveContext ec, Type value_type, out object value)
- {
- if (value_type == TypeManager.object_type) {
- value = GetTypedValue ();
- return true;
- }
-
- Constant c = ImplicitConversionRequired (ec, value_type, loc);
- if (c == null) {
- value = null;
- return false;
- }
-
- value = c.GetTypedValue ();
- return true;
- }
-
/// <summary>
/// This is used to obtain the actual value of the literal
/// cast into an object.
return GetValue ();
}
- public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, Type target, bool expl)
+ public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
{
if (!expl && IsLiteral &&
(TypeManager.IsPrimitiveType (target) || type == TypeManager.decimal_type) &&
}
}
- public Constant ImplicitConversionRequired (ResolveContext ec, Type type, Location loc)
+ public Constant ImplicitConversionRequired (ResolveContext ec, TypeSpec type, Location loc)
{
Constant c = ConvertImplicitly (ec, type);
if (c == null)
return c;
}
- public virtual Constant ConvertImplicitly (ResolveContext rc, Type type)
+ public virtual Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
{
if (this.type == type)
return this;
//
// Returns a constant instance based on Type
//
- public static Constant CreateConstant (ResolveContext rc, Type t, object v, Location loc)
+ public static Constant CreateConstant (ResolveContext rc, TypeSpec t, object v, Location loc)
{
return CreateConstantFromValue (t, v, loc).Resolve (rc);
}
- public static Constant CreateConstantFromValue (Type t, object v, Location loc)
+ public static Constant CreateConstantFromValue (TypeSpec t, object v, Location loc)
{
if (t == TypeManager.int32_type)
return new IntConstant ((int) v, loc);
if (t == TypeManager.decimal_type)
return new DecimalConstant ((decimal) v, loc);
if (TypeManager.IsEnumType (t)) {
- Type real_type = TypeManager.GetEnumUnderlyingType (t);
+ var real_type = EnumSpec.GetUnderlyingType (t);
return new EnumConstant (CreateConstantFromValue (real_type, v, loc).Resolve (null), t);
}
if (v == null) {
/// It throws OverflowException
/// </summary>
// DON'T CALL THIS METHOD DIRECTLY AS IT DOES NOT HANDLE ENUMS
- public abstract Constant ConvertExplicitly (bool in_checked_context, Type target_type);
+ public abstract Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type);
/// <summary>
/// Attempts to do a compile-time folding of a constant cast.
/// </summary>
- public Constant TryReduce (ResolveContext ec, Type target_type, Location loc)
+ public Constant TryReduce (ResolveContext ec, TypeSpec target_type, Location loc)
{
try {
return TryReduce (ec, target_type);
}
}
- Constant TryReduce (ResolveContext ec, Type target_type)
+ Constant TryReduce (ResolveContext ec, TypeSpec target_type)
{
if (Type == target_type)
return this;
Constant c;
if (TypeManager.IsEnumType (target_type)) {
- c = TryReduce (ec, TypeManager.GetEnumUnderlyingType (target_type));
+ c = TryReduce (ec, EnumSpec.GetUnderlyingType (target_type));
if (c == null)
return null;
/// Need to pass type as the constant can require a boxing
/// and in such case no optimization is possible
/// </summary>
- public bool IsDefaultInitializer (Type type)
+ public bool IsDefaultInitializer (TypeSpec type)
{
if (type == Type)
return IsDefaultValue;
public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
{
- return System.Linq.Expressions.Expression.Constant (GetTypedValue (), type);
- }
-
- public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
- {
- // A constant cannot be of generic type
+ return System.Linq.Expressions.Expression.Constant (GetTypedValue (), type.GetMetaInfo ());
}
public new Constant Resolve (ResolveContext rc)
{
}
- public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, Type target, bool expl)
+ public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
{
try {
ConvertExplicitly (true, target);
{
return (object) Value;
}
+
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
public override void Emit (EmitContext ec)
{
if (Value)
- ec.ig.Emit (OpCodes.Ldc_I4_1);
+ ec.Emit (OpCodes.Ldc_I4_1);
else
- ec.ig.Emit (OpCodes.Ldc_I4_0);
+ ec.Emit (OpCodes.Ldc_I4_0);
}
public override bool IsDefaultValue {
get { return Value == false; }
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
return null;
}
Value = v;
}
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
public override void Emit (EmitContext ec)
{
- IntLiteral.EmitInt (ec.ig, Value);
+ ec.EmitInt (Value);
}
public override string AsString ()
get { return Value == 0; }
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.sbyte_type) {
if (in_checked_context){
return this;
}
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write ((ushort) Value);
+ }
+
public override void Emit (EmitContext ec)
{
- IntLiteral.EmitInt (ec.ig, Value);
+ ec.EmitInt (Value);
}
static string descape (char c)
get { return Value == '\0'; }
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.byte_type) {
if (in_checked_context){
return this;
}
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
public override void Emit (EmitContext ec)
{
- IntLiteral.EmitInt (ec.ig, Value);
+ ec.EmitInt (Value);
}
public override string AsString ()
get { return Value == 0; }
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.byte_type) {
if (in_checked_context && Value < 0)
return this;
}
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
public override void Emit (EmitContext ec)
{
- IntLiteral.EmitInt (ec.ig, Value);
+ ec.EmitInt (Value);
}
public override string AsString ()
}
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.byte_type) {
if (in_checked_context){
return this;
}
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
public override void Emit (EmitContext ec)
{
- IntLiteral.EmitInt (ec.ig, Value);
+ ec.EmitInt (Value);
}
public override string AsString ()
get { return Value == 0; }
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.byte_type) {
if (in_checked_context){
return this;
}
- static public void EmitInt (ILGenerator ig, int i)
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- switch (i){
- case -1:
- ig.Emit (OpCodes.Ldc_I4_M1);
- break;
-
- case 0:
- ig.Emit (OpCodes.Ldc_I4_0);
- break;
-
- case 1:
- ig.Emit (OpCodes.Ldc_I4_1);
- break;
-
- case 2:
- ig.Emit (OpCodes.Ldc_I4_2);
- break;
-
- case 3:
- ig.Emit (OpCodes.Ldc_I4_3);
- break;
-
- case 4:
- ig.Emit (OpCodes.Ldc_I4_4);
- break;
-
- case 5:
- ig.Emit (OpCodes.Ldc_I4_5);
- break;
-
- case 6:
- ig.Emit (OpCodes.Ldc_I4_6);
- break;
-
- case 7:
- ig.Emit (OpCodes.Ldc_I4_7);
- break;
-
- case 8:
- ig.Emit (OpCodes.Ldc_I4_8);
- break;
-
- default:
- if (i >= -128 && i <= 127){
- ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i);
- } else
- ig.Emit (OpCodes.Ldc_I4, i);
- break;
- }
+ enc.Stream.Write (Value);
}
public override void Emit (EmitContext ec)
{
- EmitInt (ec.ig, Value);
+ ec.EmitInt (Value);
}
public override string AsString ()
get { return Value == 0; }
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.byte_type) {
if (in_checked_context){
return null;
}
- public override Constant ConvertImplicitly (ResolveContext rc, Type type)
+ public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
{
if (this.type == type)
return this;
/// into a different data type using casts (See Implicit Constant
/// Expression Conversions)
/// </summary>
- Constant TryImplicitIntConversion (Type target_type)
+ Constant TryImplicitIntConversion (TypeSpec target_type)
{
if (target_type == TypeManager.sbyte_type) {
if (Value >= SByte.MinValue && Value <= SByte.MaxValue)
return this;
}
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
public override void Emit (EmitContext ec)
{
- IntLiteral.EmitInt (ec.ig, unchecked ((int) Value));
+ ec.EmitInt (unchecked ((int) Value));
}
public override string AsString ()
get { return Value == 0; }
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.byte_type) {
if (in_checked_context){
return this;
}
- public override void Emit (EmitContext ec)
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- EmitLong (ec.ig, Value);
+ enc.Stream.Write (Value);
}
- static public void EmitLong (ILGenerator ig, long l)
+ public override void Emit (EmitContext ec)
{
- if (l >= int.MinValue && l <= int.MaxValue) {
- IntLiteral.EmitInt (ig, unchecked ((int) l));
- ig.Emit (OpCodes.Conv_I8);
- return;
- }
-
- if (l >= 0 && l <= uint.MaxValue) {
- IntLiteral.EmitInt (ig, unchecked ((int) l));
- ig.Emit (OpCodes.Conv_U8);
- return;
- }
-
- ig.Emit (OpCodes.Ldc_I8, l);
+ ec.EmitLong (Value);
}
public override string AsString ()
get { return Value == 0; }
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.byte_type) {
if (in_checked_context){
return null;
}
- public override Constant ConvertImplicitly (ResolveContext rc, Type type)
+ public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
{
if (Value >= 0 && type == TypeManager.uint64_type) {
return new ULongConstant ((ulong) Value, loc).Resolve (rc);
return this;
}
- public override void Emit (EmitContext ec)
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- ILGenerator ig = ec.ig;
+ enc.Stream.Write (Value);
+ }
- LongLiteral.EmitLong (ig, unchecked ((long) Value));
+ public override void Emit (EmitContext ec)
+ {
+ ec.EmitLong (unchecked ((long) Value));
}
public override string AsString ()
get { return Value == 0; }
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.byte_type) {
if (in_checked_context && Value > Byte.MaxValue)
return this;
}
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
public override void Emit (EmitContext ec)
{
- ec.ig.Emit (OpCodes.Ldc_R4, Value);
+ ec.Emit (OpCodes.Ldc_R4, Value);
}
public override string AsString ()
}
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.byte_type) {
if (in_checked_context){
return this;
}
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
public override void Emit (EmitContext ec)
{
- ec.ig.Emit (OpCodes.Ldc_R8, Value);
+ ec.Emit (OpCodes.Ldc_R8, Value);
}
public override string AsString ()
}
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.byte_type) {
if (in_checked_context){
public override void Emit (EmitContext ec)
{
- ILGenerator ig = ec.ig;
-
int [] words = decimal.GetBits (Value);
int power = (words [3] >> 16) & 0xff;
return;
}
- IntConstant.EmitInt (ig, (int) Value);
- ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_int_arg);
+ ec.EmitInt ((int) Value);
+ ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_int_arg);
return;
}
return;
}
- LongConstant.EmitLong (ig, (long) Value);
- ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_long_arg);
+ ec.EmitLong ((long) Value);
+ ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_long_arg);
return;
}
}
- IntConstant.EmitInt (ig, words [0]);
- IntConstant.EmitInt (ig, words [1]);
- IntConstant.EmitInt (ig, words [2]);
+ ec.EmitInt (words [0]);
+ ec.EmitInt (words [1]);
+ ec.EmitInt (words [2]);
// sign
- IntConstant.EmitInt (ig, words [3] >> 31);
+ ec.EmitInt (words [3] >> 31);
// power
- IntConstant.EmitInt (ig, power);
+ ec.EmitInt (power);
if (TypeManager.void_decimal_ctor_five_args == null) {
TypeManager.void_decimal_ctor_five_args = TypeManager.GetPredefinedConstructor (
return;
}
- ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
+ ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
}
public override bool IsDefaultValue {
}
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
if (target_type == TypeManager.sbyte_type)
return new SByteConstant ((sbyte)Value, loc);
public override void Emit (EmitContext ec)
{
if (Value == null) {
- ec.ig.Emit (OpCodes.Ldnull);
+ ec.Emit (OpCodes.Ldnull);
return;
}
//
if (Value.Length == 0 && RootContext.Optimize && !TypeManager.IsEqual (ec.CurrentType, TypeManager.string_type)) {
if (TypeManager.string_empty == null)
- TypeManager.string_empty = TypeManager.GetPredefinedField (TypeManager.string_type, "Empty", loc);
+ TypeManager.string_empty = TypeManager.GetPredefinedField (TypeManager.string_type, "Empty", loc, TypeManager.string_type);
if (TypeManager.string_empty != null) {
- ec.ig.Emit (OpCodes.Ldsfld, TypeManager.string_empty);
+ ec.Emit (OpCodes.Ldsfld, TypeManager.string_empty);
return;
}
}
- ec.ig.Emit (OpCodes.Ldstr, Value);
+ ec.Emit (OpCodes.Ldstr, Value);
+ }
+
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ // cast to object
+ if (type != targetType)
+ enc.Encode (type);
+
+ enc.Encode (Value);
}
public override bool IsDefaultValue {
}
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
return null;
}
//
public class NullConstant : Constant
{
- public NullConstant (Type type, Location loc)
+ public NullConstant (TypeSpec type, Location loc)
: base (loc)
{
eclass = ExprClass.Value;
return GetSignatureForError ();
}
+ public override Expression CreateExpressionTree (ResolveContext ec)
+ {
+ if (type == InternalType.Null || type == TypeManager.object_type) {
+ // Optimized version, also avoids referencing literal internal type
+ Arguments args = new Arguments (1);
+ args.Add (new Argument (this));
+ return CreateExpressionFactoryCall (ec, "Constant", args);
+ }
+
+ return base.CreateExpressionTree (ec);
+ }
+
protected override Expression DoResolve (ResolveContext ec)
{
return this;
}
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ // Type it as string cast
+ if (targetType == TypeManager.object_type || targetType == TypeManager.null_type)
+ enc.Encode (TypeManager.string_type);
+
+ var ac = targetType as ArrayContainer;
+ if (ac != null) {
+ if (ac.Rank != 1)
+ base.EncodeAttributeValue (rc, enc, targetType);
+ else
+ enc.Stream.Write (uint.MaxValue);
+ } else {
+ enc.Stream.Write (byte.MaxValue);
+ }
+ }
+
public override void Emit (EmitContext ec)
{
- ec.ig.Emit (OpCodes.Ldnull);
+ ec.Emit (OpCodes.Ldnull);
// Only to make verifier happy
if (TypeManager.IsGenericParameter (type))
- ec.ig.Emit (OpCodes.Unbox_Any, type);
+ ec.Emit (OpCodes.Unbox_Any, type);
}
public override string ExprClassName {
return "null";
}
- public override Constant ConvertExplicitly (bool inCheckedContext, Type targetType)
+ public override Constant ConvertExplicitly (bool inCheckedContext, TypeSpec targetType)
{
if (targetType.IsPointer) {
if (IsLiteral || this is NullPointer)
return null;
}
- public override Constant ConvertImplicitly (ResolveContext rc, Type targetType)
+ public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec targetType)
{
return ConvertExplicitly (false, targetType);
}
public override bool IsZeroInteger {
get { return true; }
}
-
- public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
- {
- type = storey.MutateType (type);
- }
}
/// <summary>
get { return value.IsZeroInteger; }
}
- public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
{
Constant new_value = value.ConvertExplicitly (in_checked_context, target_type);
return new_value == null ? null : new SideEffectConstant (new_value, side_effect, new_value.Location);