Wiring of BuildinType
[mono.git] / mcs / mcs / constant.cs
index d5b6f8faf73b7f878f9d3f5d15b04aa74e2f3cbf..cab0daa3b7fc33278242e5baae6c074d9abed3bf 100644 (file)
@@ -9,11 +9,15 @@
 // Copyright 2003-2008 Novell, Inc.
 //
 
-namespace Mono.CSharp {
+using System;
+
+#if STATIC
+using IKVM.Reflection.Emit;
+#else
+using System.Reflection.Emit;
+#endif
 
-       using System;
-       using System.Reflection.Emit;
-       using System.Collections;
+namespace Mono.CSharp {
 
        /// <summary>
        ///   Base class for constants and literals.
@@ -25,36 +29,9 @@ namespace Mono.CSharp {
                        this.loc = loc;
                }
 
-               /// <remarks>
-               ///   This is different from ToString in that ToString
-               ///   is supposed to be there for debugging purposes,
-               ///   and is not guaranteed to be useful for anything else,
-               ///   AsString() will provide something that can be used
-               ///   for round-tripping C# code.  Maybe it can be used
-               ///   for IL assembly as well.
-               /// </remarks>
-               public abstract string AsString ();
-
                override public string ToString ()
                {
-                       return this.GetType ().Name + " (" + AsString () + ")";
-               }
-
-               public override bool GetAttributableValue (EmitContext 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;
+                       return this.GetType ().Name + " (" + GetValueAsLiteral () + ")";
                }
 
                /// <summary>
@@ -63,40 +40,42 @@ namespace Mono.CSharp {
                /// </summary>
                public abstract object GetValue ();
 
+               public abstract long GetValueAsLong ();
+
+               public abstract string GetValueAsLiteral ();
+
+#if !STATIC
+               //
+               // Returns an object value which is typed to contant type
+               //
                public virtual object GetTypedValue ()
                {
                        return GetValue ();
                }
+#endif
 
-               /// <summary>
-               ///   Constants are always born in a fully resolved state
-               /// </summary>
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       return this;
-               }
-
-               public override void Error_ValueCannotBeConverted (EmitContext 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) &&
                                (TypeManager.IsPrimitiveType (type) || type == TypeManager.decimal_type)) {
-                               Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
-                                       AsString (), TypeManager.CSharpName (target));
+                               ec.Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
+                                       GetValueAsLiteral (), TypeManager.CSharpName (target));
                        } else {
                                base.Error_ValueCannotBeConverted (ec, loc, target, expl);
                        }
                }
 
-               public Constant ImplicitConversionRequired (EmitContext ec, Type type, Location loc)
+               public Constant ImplicitConversionRequired (ResolveContext ec, TypeSpec type, Location loc)
                {
-                       Constant c = ConvertImplicitly (type);
+                       Constant c = ConvertImplicitly (ec, type);
                        if (c == null)
                                Error_ValueCannotBeConverted (ec, loc, type, false);
+
                        return c;
                }
 
-               public virtual Constant ConvertImplicitly (Type type)
+               public virtual Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
                {
                        if (this.type == type)
                                return this;
@@ -115,60 +94,76 @@ namespace Mono.CSharp {
                                  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);
+                       return CreateConstant (rc, type, constant_value, loc);
+               }
+
+               //
+               //  Returns a constant instance based on Type
+               //
+               public static Constant CreateConstant (ResolveContext rc, TypeSpec t, object v, Location loc)
+               {
+                       return CreateConstantFromValue (t, v, loc).Resolve (rc);
+               }
+
+               public static Constant CreateConstantFromValue (TypeSpec t, object v, Location loc)
+               {
+                       if (t.BuildinType > 0) {
+                               switch (t.BuildinType) {
+                               case BuildinTypeSpec.Type.Int:
+                                       return new IntConstant ((int) v, loc);
+                               case BuildinTypeSpec.Type.String:
+                                       return new StringConstant ((string) v, loc);
+                               case BuildinTypeSpec.Type.UInt:
+                                       return new UIntConstant ((uint) v, loc);
+                               case BuildinTypeSpec.Type.Long:
+                                       return new LongConstant ((long) v, loc);
+                               case BuildinTypeSpec.Type.ULong:
+                                       return new ULongConstant ((ulong) v, loc);
+                               case BuildinTypeSpec.Type.Float:
+                                       return new FloatConstant ((float) v, loc);
+                               case BuildinTypeSpec.Type.Double:
+                                       return new DoubleConstant ((double) v, loc);
+                               case BuildinTypeSpec.Type.Short:
+                                       return new ShortConstant ((short) v, loc);
+                               case BuildinTypeSpec.Type.UShort:
+                                       return new UShortConstant ((ushort) v, loc);
+                               case BuildinTypeSpec.Type.SByte:
+                                       return new SByteConstant ((sbyte) v, loc);
+                               case BuildinTypeSpec.Type.Byte:
+                                       return new ByteConstant ((byte) v, loc);
+                               case BuildinTypeSpec.Type.Char:
+                                       return new CharConstant ((char) v, loc);
+                               case BuildinTypeSpec.Type.Bool:
+                                       return new BoolConstant ((bool) v, loc);
+                               case BuildinTypeSpec.Type.Decimal:
+                                       return new DecimalConstant ((decimal) v, loc);
+                               }
+                       }
+
+                       if (t.IsEnum) {
+                               var real_type = EnumSpec.GetUnderlyingType (t);
+                               return new EnumConstant (CreateConstantFromValue (real_type, v, loc).Resolve (null), t);
+                       }
+
+                       if (v == null) {
+                               if (TypeManager.IsNullableType (t))
+                                       return Nullable.LiftedNull.Create (t, loc);
+
+                               if (TypeManager.IsReferenceType (t))
+                                       return new NullConstant (t, loc);
+                       }
+
+                       throw new InternalErrorException ("Constant value `{0}' has unexpected underlying type `{1}'",
+                               v, TypeManager.CSharpName (t));
+               }
+
+               public override Expression CreateExpressionTree (ResolveContext ec)
+               {
+                       Arguments args = new Arguments (2);
                        args.Add (new Argument (this));
-                       args.Add (new Argument (
-                               new TypeOf (new TypeExpression (type, loc), loc)));
+                       args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
 
-                       return CreateExpressionFactoryCall ("Constant", args);
+                       return CreateExpressionFactoryCall (ec, "Constant", args);
                }
 
 
@@ -177,56 +172,60 @@ namespace Mono.CSharp {
                /// 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 (EmitContext ec, Type target_type, Location loc)
+               public Constant TryReduce (ResolveContext ec, TypeSpec target_type, Location loc)
                {
                        try {
                                return TryReduce (ec, target_type);
                        }
                        catch (OverflowException) {
-                               if (ec.ConstantCheckState) {                            
-                                       Report.Error (221, loc, "Constant value `{0}' cannot be converted to a `{1}' (use `unchecked' syntax to override)",
-                                               GetValue ().ToString (), TypeManager.CSharpName (target_type));
+                               if (ec.ConstantCheckState && Type.BuildinType != BuildinTypeSpec.Type.Decimal) {
+                                       ec.Report.Error (221, loc,
+                                               "Constant value `{0}' cannot be converted to a `{1}' (use `unchecked' syntax to override)",
+                                               GetValueAsLiteral (), target_type.GetSignatureForError ());
                                } else {
                                        Error_ValueCannotBeConverted (ec, loc, target_type, false);
                                }
 
-                               return New.Constantify (target_type);
+                               return New.Constantify (target_type, loc).Resolve (ec);
                        }
                }
 
-               Constant TryReduce (EmitContext ec, Type target_type)
+               Constant TryReduce (ResolveContext ec, TypeSpec target_type)
                {
                        if (Type == target_type)
                                return this;
 
+                       Constant c;
                        if (TypeManager.IsEnumType (target_type)) {
-                               Constant c = TryReduce (ec, TypeManager.GetEnumUnderlyingType (target_type));
+                               c = TryReduce (ec, EnumSpec.GetUnderlyingType (target_type));
                                if (c == null)
                                        return null;
 
-                               return new EnumConstant (c, target_type);
+                               return new EnumConstant (c, target_type).Resolve (ec);
                        }
 
-                       return ConvertExplicitly (ec.ConstantCheckState, target_type);
+                       c = ConvertExplicitly (ec.ConstantCheckState, target_type);
+                       if (c != null)
+                               c = c.Resolve (ec);
+
+                       return c;
                }
 
-               public abstract Constant Increment ();
-               
                /// <summary>
                /// 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;
 
-                       return Type == TypeManager.null_type;
+                       return this is NullLiteral;
                }
 
                public abstract bool IsDefaultValue {
@@ -243,6 +242,10 @@ namespace Mono.CSharp {
                public virtual bool IsLiteral {
                        get { return false; }
                }
+               
+               public virtual bool IsOneInteger {
+                       get { return false; }
+               }               
 
                //
                // Returns true iff 1) the stack type of this is one of Object, 
@@ -257,24 +260,56 @@ namespace Mono.CSharp {
                        // do nothing
                }
 
+               public sealed override Expression Clone (CloneContext clonectx)
+               {
+                       // No cloning is not needed for constants
+                       return this;
+               }
+
                protected override void CloneTo (CloneContext clonectx, Expression target)
                {
-                       // CloneTo: Nothing, we do not keep any state on this expression
+                       throw new NotSupportedException ("should not be reached");
+               }
+
+               public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
+               {
+#if STATIC
+                       return base.MakeExpression (ctx);
+#else
+                       return System.Linq.Expressions.Expression.Constant (GetTypedValue (), type.GetMetaInfo ());
+#endif
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               public new Constant Resolve (ResolveContext rc)
                {
-                       // A constant cannot be of generic type
+                       if (eclass != ExprClass.Unresolved)
+                               return this;
+
+                       // Resolved constant has to be still a constant
+                       Constant c = (Constant) DoResolve (rc);
+                       if (c == null)
+                               return null;
+
+                       if ((c.eclass & ExprClass.Value) == 0) {
+                               c.Error_UnexpectedKind (rc, ResolveFlags.VariableOrValue, loc);
+                               return null;
+                       }
+
+                       if (c.type == null)
+                               throw new InternalErrorException ("Expression `{0}' did not set its type after Resolve", c.GetType ());
+
+                       return c;
                }
        }
 
-       public abstract class IntegralConstant : Constant {
+       public abstract class IntegralConstant : Constant
+       {
                protected IntegralConstant (Location loc) :
                        base (loc)
                {
                }
 
-               public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
+               public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
                {
                        try {
                                ConvertExplicitly (true, target);
@@ -282,10 +317,17 @@ namespace Mono.CSharp {
                        }
                        catch
                        {
-                               Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
+                               ec.Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
                                        GetValue ().ToString (), TypeManager.CSharpName (target));
                        }
                }
+
+               public override string GetValueAsLiteral ()
+               {
+                       return GetValue ().ToString ();
+               }
+               
+               public abstract Constant Increment ();
        }
        
        public class BoolConstant : Constant {
@@ -293,37 +335,45 @@ namespace Mono.CSharp {
                
                public BoolConstant (bool val, Location loc):
                        base (loc)
+               {
+                       Value = val;
+               }
+
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        type = TypeManager.bool_type;
                        eclass = ExprClass.Value;
+                       return this;
+               }
 
-                       Value = val;
+               public override object GetValue ()
+               {
+                       return (object) Value;
                }
 
-               override public string AsString ()
+               public override string GetValueAsLiteral ()
                {
                        return Value ? "true" : "false";
                }
 
-               public override object GetValue ()
+               public override long GetValueAsLong ()
                {
-                       return (object) Value;
+                       return Value ? 1 : 0;
+               }
+
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Encode (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 Constant Increment ()
-               {
-                       throw new NotSupportedException ();
-               }
-       
                public override bool IsDefaultValue {
                        get {
                                return !Value;
@@ -340,32 +390,38 @@ namespace Mono.CSharp {
                        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;
                }
 
        }
 
-       public class ByteConstant : IntegralConstant {
+       public class ByteConstant : IntegralConstant
+       {
                public readonly byte Value;
 
                public ByteConstant (byte v, Location loc):
                        base (loc)
                {
-                       type = TypeManager.byte_type;
-                       eclass = ExprClass.Value;
                        Value = v;
                }
 
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Encode (Value);
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       IntLiteral.EmitInt (ec.ig, Value);
+                       ec.EmitInt (Value);
                }
 
-               public override string AsString ()
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       return Value.ToString ();
+                       type = TypeManager.byte_type;
+                       eclass = ExprClass.Value;
+                       return this;
                }
 
                public override object GetValue ()
@@ -373,6 +429,11 @@ namespace Mono.CSharp {
                        return Value;
                }
 
+               public override long GetValueAsLong ()
+               {
+                       return Value;
+               }
+
                public override Constant Increment ()
                {
                        return new ByteConstant (checked ((byte)(Value + 1)), loc);
@@ -384,6 +445,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override bool IsOneInteger {
+                       get {
+                               return Value == 1;
+                       }
+               }               
+
                public override bool IsNegative {
                        get {
                                return false;
@@ -394,35 +461,36 @@ namespace Mono.CSharp {
                        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) {
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.SByte:
                                if (in_checked_context){
                                        if (Value > SByte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new SByteConstant ((sbyte) Value, Location);
-                       }
-                       if (target_type == TypeManager.short_type)
+                       case BuildinTypeSpec.Type.Short:
                                return new ShortConstant ((short) Value, Location);
-                       if (target_type == TypeManager.ushort_type)
+                       case BuildinTypeSpec.Type.UShort:
                                return new UShortConstant ((ushort) Value, Location);
-                       if (target_type == TypeManager.int32_type)
+                       case BuildinTypeSpec.Type.Int:
                                return new IntConstant ((int) Value, Location);
-                       if (target_type == TypeManager.uint32_type)
+                       case BuildinTypeSpec.Type.UInt:
                                return new UIntConstant ((uint) Value, Location);
-                       if (target_type == TypeManager.int64_type)
+                       case BuildinTypeSpec.Type.Long:
                                return new LongConstant ((long) Value, Location);
-                       if (target_type == TypeManager.uint64_type)
+                       case BuildinTypeSpec.Type.ULong:
                                return new ULongConstant ((ulong) Value, Location);
-                       if (target_type == TypeManager.float_type)
+                       case BuildinTypeSpec.Type.Float:
                                return new FloatConstant ((float) Value, Location);
-                       if (target_type == TypeManager.double_type)
+                       case BuildinTypeSpec.Type.Double:
                                return new DoubleConstant ((double) Value, Location);
-                       if (target_type == TypeManager.char_type)
+                       case BuildinTypeSpec.Type.Char:
                                return new CharConstant ((char) Value, Location);
-                       if (target_type == TypeManager.decimal_type)
+                       case BuildinTypeSpec.Type.Decimal:
                                return new DecimalConstant ((decimal) Value, Location);
+                       }
 
                        return null;
                }
@@ -434,18 +502,28 @@ namespace Mono.CSharp {
 
                public CharConstant (char v, Location loc):
                        base (loc)
+               {
+                       Value = v;
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
                {
                        type = TypeManager.char_type;
                        eclass = ExprClass.Value;
-                       Value = v;
+                       return this;
+               }
+
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Encode ((ushort) Value);
                }
 
                public override void Emit (EmitContext ec)
                {
-                       IntLiteral.EmitInt (ec.ig, Value);
+                       ec.EmitInt (Value);
                }
 
-               static public string descape (char c)
+               static string descape (char c)
                {
                        switch (c){
                        case '\a':
@@ -474,21 +552,21 @@ namespace Mono.CSharp {
                        return c.ToString ();
                }
 
-               public override string AsString ()
+               public override object GetValue ()
                {
-                       return "\"" + descape (Value) + "\"";
+                       return Value;
                }
 
-               public override object GetValue ()
+               public override long GetValueAsLong ()
                {
                        return Value;
                }
 
-               public override Constant Increment ()
+               public override string GetValueAsLiteral ()
                {
-                       return new CharConstant (checked ((char)(Value + 1)), loc);
+                       return "\"" + descape (Value) + "\"";
                }
-               
+
                public override bool IsDefaultValue {
                        get {
                                return Value == 0;
@@ -505,43 +583,43 @@ namespace Mono.CSharp {
                        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){
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.Byte:
+                               if (in_checked_context) {
                                        if (Value < Byte.MinValue || Value > Byte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new ByteConstant ((byte) Value, Location);
-                       }
-                       if (target_type == TypeManager.sbyte_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.SByte:
+                               if (in_checked_context) {
                                        if (Value > SByte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new SByteConstant ((sbyte) Value, Location);
-                       }
-                       if (target_type == TypeManager.short_type) {
-                               if (in_checked_context){
+
+                       case BuildinTypeSpec.Type.Short:
+                               if (in_checked_context) {
                                        if (Value > Int16.MaxValue)
                                                throw new OverflowException ();
-                               }                                       
+                               }
                                return new ShortConstant ((short) Value, Location);
-                       }
-                       if (target_type == TypeManager.int32_type)
+                       case BuildinTypeSpec.Type.Int:
                                return new IntConstant ((int) Value, Location);
-                       if (target_type == TypeManager.uint32_type)
+                       case BuildinTypeSpec.Type.UInt:
                                return new UIntConstant ((uint) Value, Location);
-                       if (target_type == TypeManager.int64_type)
+                       case BuildinTypeSpec.Type.Long:
                                return new LongConstant ((long) Value, Location);
-                       if (target_type == TypeManager.uint64_type)
+                       case BuildinTypeSpec.Type.ULong:
                                return new ULongConstant ((ulong) Value, Location);
-                       if (target_type == TypeManager.float_type)
+                       case BuildinTypeSpec.Type.Float:
                                return new FloatConstant ((float) Value, Location);
-                       if (target_type == TypeManager.double_type)
+                       case BuildinTypeSpec.Type.Double:
                                return new DoubleConstant ((double) Value, Location);
-                       if (target_type == TypeManager.decimal_type)
+                       case BuildinTypeSpec.Type.Decimal:
                                return new DecimalConstant ((decimal) Value, Location);
+                       }
 
                        return null;
                }
@@ -553,20 +631,25 @@ namespace Mono.CSharp {
 
                public SByteConstant (sbyte v, Location loc):
                        base (loc)
+               {
+                       Value = v;
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
                {
                        type = TypeManager.sbyte_type;
                        eclass = ExprClass.Value;
-                       Value = v;
+                       return this;
                }
 
-               public override void Emit (EmitContext ec)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       IntLiteral.EmitInt (ec.ig, Value);
+                       enc.Encode (Value);
                }
 
-               public override string AsString ()
+               public override void Emit (EmitContext ec)
                {
-                       return Value.ToString ();
+                       ec.EmitInt (Value);
                }
 
                public override object GetValue ()
@@ -574,6 +657,11 @@ namespace Mono.CSharp {
                        return Value;
                }
 
+               public override long GetValueAsLong ()
+               {
+                       return Value;
+               }
+
                public override Constant Increment ()
                {
                    return new SByteConstant (checked((sbyte)(Value + 1)), loc);
@@ -591,47 +679,52 @@ namespace Mono.CSharp {
                        }
                }
                
+               public override bool IsOneInteger {
+                       get {
+                               return Value == 1;
+                       }
+               }               
+               
                public override bool IsZeroInteger {
                        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) {
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.Byte:
                                if (in_checked_context && Value < 0)
                                        throw new OverflowException ();
                                return new ByteConstant ((byte) Value, Location);
-                       }
-                       if (target_type == TypeManager.short_type)
+                       case BuildinTypeSpec.Type.Short:
                                return new ShortConstant ((short) Value, Location);
-                       if (target_type == TypeManager.ushort_type) {
+                       case BuildinTypeSpec.Type.UShort:
                                if (in_checked_context && Value < 0)
                                        throw new OverflowException ();
                                return new UShortConstant ((ushort) Value, Location);
-                       } if (target_type == TypeManager.int32_type)
-                                 return new IntConstant ((int) Value, Location);
-                       if (target_type == TypeManager.uint32_type) {
+                       case BuildinTypeSpec.Type.Int:
+                               return new IntConstant ((int) Value, Location);
+                       case BuildinTypeSpec.Type.UInt:
                                if (in_checked_context && Value < 0)
                                        throw new OverflowException ();
                                return new UIntConstant ((uint) Value, Location);
-                       } if (target_type == TypeManager.int64_type)
-                                 return new LongConstant ((long) Value, Location);
-                       if (target_type == TypeManager.uint64_type) {
+                       case BuildinTypeSpec.Type.Long:
+                               return new LongConstant ((long) Value, Location);
+                       case BuildinTypeSpec.Type.ULong:
                                if (in_checked_context && Value < 0)
                                        throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
-                       }
-                       if (target_type == TypeManager.float_type)
+                       case BuildinTypeSpec.Type.Float:
                                return new FloatConstant ((float) Value, Location);
-                       if (target_type == TypeManager.double_type)
+                       case BuildinTypeSpec.Type.Double:
                                return new DoubleConstant ((double) Value, Location);
-                       if (target_type == TypeManager.char_type) {
+                       case BuildinTypeSpec.Type.Char:
                                if (in_checked_context && Value < 0)
                                        throw new OverflowException ();
                                return new CharConstant ((char) Value, Location);
-                       }
-                       if (target_type == TypeManager.decimal_type)
+                       case BuildinTypeSpec.Type.Decimal:
                                return new DecimalConstant ((decimal) Value, Location);
+                       }
 
                        return null;
                }
@@ -643,20 +736,25 @@ namespace Mono.CSharp {
 
                public ShortConstant (short v, Location loc):
                        base (loc)
+               {
+                       Value = v;
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
                {
                        type = TypeManager.short_type;
                        eclass = ExprClass.Value;
-                       Value = v;
+                       return this;
                }
 
-               public override void Emit (EmitContext ec)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       IntLiteral.EmitInt (ec.ig, Value);
+                       enc.Encode (Value);
                }
 
-               public override string AsString ()
+               public override void Emit (EmitContext ec)
                {
-                       return Value.ToString ();
+                       ec.EmitInt (Value);
                }
 
                public override object GetValue ()
@@ -664,6 +762,11 @@ namespace Mono.CSharp {
                        return Value;
                }
 
+               public override long GetValueAsLong ()
+               {
+                       return Value;
+               }
+
                public override Constant Increment ()
                {
                        return new ShortConstant (checked((short)(Value + 1)), loc);
@@ -684,87 +787,100 @@ namespace Mono.CSharp {
                                return Value < 0;
                        }
                }
+               
+               public override bool IsOneInteger {
+                       get {
+                               return Value == 1;
+                       }
+               }               
 
-               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){
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.Byte:
+                               if (in_checked_context) {
                                        if (Value < Byte.MinValue || Value > Byte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new ByteConstant ((byte) Value, Location);
-                       }
-                       if (target_type == TypeManager.sbyte_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.SByte:
+                               if (in_checked_context) {
                                        if (Value < SByte.MinValue || Value > SByte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new SByteConstant ((sbyte) Value, Location);
-                       }
-                       if (target_type == TypeManager.ushort_type) {
+                       case BuildinTypeSpec.Type.UShort:
                                if (in_checked_context && Value < 0)
                                        throw new OverflowException ();
-                               
+
                                return new UShortConstant ((ushort) Value, Location);
-                       }
-                       if (target_type == TypeManager.int32_type)
+                       case BuildinTypeSpec.Type.Int:
                                return new IntConstant ((int) Value, Location);
-                       if (target_type == TypeManager.uint32_type) {
+                       case BuildinTypeSpec.Type.UInt:
                                if (in_checked_context && Value < 0)
                                        throw new OverflowException ();
                                return new UIntConstant ((uint) Value, Location);
-                       }
-                       if (target_type == TypeManager.int64_type)
+                       case BuildinTypeSpec.Type.Long:
                                return new LongConstant ((long) Value, Location);
-                       if (target_type == TypeManager.uint64_type) {
+                       case BuildinTypeSpec.Type.ULong:
                                if (in_checked_context && Value < 0)
                                        throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
-                       }
-                       if (target_type == TypeManager.float_type)
+                       case BuildinTypeSpec.Type.Float:
                                return new FloatConstant ((float) Value, Location);
-                       if (target_type == TypeManager.double_type)
+                       case BuildinTypeSpec.Type.Double:
                                return new DoubleConstant ((double) Value, Location);
-                       if (target_type == TypeManager.char_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.Char:
+                               if (in_checked_context) {
                                        if (Value < Char.MinValue)
                                                throw new OverflowException ();
                                }
                                return new CharConstant ((char) Value, Location);
-                       }
-                       if (target_type == TypeManager.decimal_type)
+                       case BuildinTypeSpec.Type.Decimal:
                                return new DecimalConstant ((decimal) Value, Location);
+                       }
 
                        return null;
                }
 
        }
 
-       public class UShortConstant : IntegralConstant {
+       public class UShortConstant : IntegralConstant
+       {
                public readonly ushort Value;
 
                public UShortConstant (ushort v, Location loc):
                        base (loc)
+               {
+                       Value = v;
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
                {
                        type = TypeManager.ushort_type;
                        eclass = ExprClass.Value;
-                       Value = v;
+                       return this;
                }
 
-               public override void Emit (EmitContext ec)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       IntLiteral.EmitInt (ec.ig, Value);
+                       enc.Encode (Value);
                }
 
-               public override string AsString ()
+               public override void Emit (EmitContext ec)
                {
-                       return Value.ToString ();
+                       ec.EmitInt (Value);
                }
 
                public override object GetValue ()
                {
                        return Value;
                }
+
+               public override long GetValueAsLong ()
+               {
+                       return Value;
+               }
        
                public override Constant Increment ()
                {
@@ -782,55 +898,59 @@ namespace Mono.CSharp {
                                return false;
                        }
                }
+               
+               public override bool IsOneInteger {
+                       get {
+                               return Value == 1;
+                       }
+               }               
        
                public override bool IsZeroInteger {
                        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){
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.Byte:
+                               if (in_checked_context) {
                                        if (Value > Byte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new ByteConstant ((byte) Value, Location);
-                       }
-                       if (target_type == TypeManager.sbyte_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.SByte:
+                               if (in_checked_context) {
                                        if (Value > SByte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new SByteConstant ((sbyte) Value, Location);
-                       }
-                       if (target_type == TypeManager.short_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.Short:
+                               if (in_checked_context) {
                                        if (Value > Int16.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new ShortConstant ((short) Value, Location);
-                       }
-                       if (target_type == TypeManager.int32_type)
+                       case BuildinTypeSpec.Type.Int:
                                return new IntConstant ((int) Value, Location);
-                       if (target_type == TypeManager.uint32_type)
+                       case BuildinTypeSpec.Type.UInt:
                                return new UIntConstant ((uint) Value, Location);
-                       if (target_type == TypeManager.int64_type)
+                       case BuildinTypeSpec.Type.Long:
                                return new LongConstant ((long) Value, Location);
-                       if (target_type == TypeManager.uint64_type)
+                       case BuildinTypeSpec.Type.ULong:
                                return new ULongConstant ((ulong) Value, Location);
-                       if (target_type == TypeManager.float_type)
+                       case BuildinTypeSpec.Type.Float:
                                return new FloatConstant ((float) Value, Location);
-                       if (target_type == TypeManager.double_type)
+                       case BuildinTypeSpec.Type.Double:
                                return new DoubleConstant ((double) Value, Location);
-                       if (target_type == TypeManager.char_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.Char:
+                               if (in_checked_context) {
                                        if (Value > Char.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new CharConstant ((char) Value, Location);
-                       }
-                       if (target_type == TypeManager.decimal_type)
+                       case BuildinTypeSpec.Type.Decimal:
                                return new DecimalConstant ((decimal) Value, Location);
+                       }
 
                        return null;
                }
@@ -842,74 +962,32 @@ namespace Mono.CSharp {
                public IntConstant (int v, Location loc):
                        base (loc)
                {
-                       type = TypeManager.int32_type;
-                       eclass = ExprClass.Value;
                        Value = v;
                }
 
-               static public void EmitInt (ILGenerator ig, int i)
+               protected override Expression DoResolve (ResolveContext rc)
                {
-                       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;
+                       type = TypeManager.int32_type;
+                       eclass = ExprClass.Value;
+                       return this;
+               }
 
-                       default:
-                               if (i >= -128 && i <= 127){
-                                       ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i);
-                               } else
-                                       ig.Emit (OpCodes.Ldc_I4, i);
-                               break;
-                       }
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Encode (Value);
                }
 
                public override void Emit (EmitContext ec)
                {
-                       EmitInt (ec.ig, Value);
+                       ec.EmitInt (Value);
                }
 
-               public override string AsString ()
+               public override object GetValue ()
                {
-                       return Value.ToString ();
+                       return Value;
                }
 
-               public override object GetValue ()
+               public override long GetValueAsLong ()
                {
                        return Value;
                }
@@ -930,82 +1008,83 @@ namespace Mono.CSharp {
                                return Value < 0;
                        }
                }
+               
+               public override bool IsOneInteger {
+                       get {
+                               return Value == 1;
+                       }
+               }               
 
                public override bool IsZeroInteger {
                        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){
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.Byte:
+                               if (in_checked_context) {
                                        if (Value < Byte.MinValue || Value > Byte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new ByteConstant ((byte) Value, Location);
-                       }
-                       if (target_type == TypeManager.sbyte_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.SByte:
+                               if (in_checked_context) {
                                        if (Value < SByte.MinValue || Value > SByte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new SByteConstant ((sbyte) Value, Location);
-                       }
-                       if (target_type == TypeManager.short_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.Short:
+                               if (in_checked_context) {
                                        if (Value < Int16.MinValue || Value > Int16.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new ShortConstant ((short) Value, Location);
-                       }
-                       if (target_type == TypeManager.ushort_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.UShort:
+                               if (in_checked_context) {
                                        if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new UShortConstant ((ushort) Value, Location);
-                       }
-                       if (target_type == TypeManager.uint32_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.UInt:
+                               if (in_checked_context) {
                                        if (Value < UInt32.MinValue)
                                                throw new OverflowException ();
                                }
                                return new UIntConstant ((uint) Value, Location);
-                       }
-                       if (target_type == TypeManager.int64_type)
+                       case BuildinTypeSpec.Type.Long:
                                return new LongConstant ((long) Value, Location);
-                       if (target_type == TypeManager.uint64_type) {
+                       case BuildinTypeSpec.Type.ULong:
                                if (in_checked_context && Value < 0)
                                        throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
-                       }
-                       if (target_type == TypeManager.float_type)
+                       case BuildinTypeSpec.Type.Float:
                                return new FloatConstant ((float) Value, Location);
-                       if (target_type == TypeManager.double_type)
+                       case BuildinTypeSpec.Type.Double:
                                return new DoubleConstant ((double) Value, Location);
-                       if (target_type == TypeManager.char_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.Char:
+                               if (in_checked_context) {
                                        if (Value < Char.MinValue || Value > Char.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new CharConstant ((char) Value, Location);
-                       }
-                       if (target_type == TypeManager.decimal_type)
+                       case BuildinTypeSpec.Type.Decimal:
                                return new DecimalConstant ((decimal) Value, Location);
+                       }
 
                        return null;
                }
 
-               public override Constant ConvertImplicitly (Type type)
+               public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
                {
                        if (this.type == type)
                                return this;
 
                        Constant c = TryImplicitIntConversion (type);
                        if (c != null)
-                               return c;
+                               return c.Resolve (rc);
 
-                       return base.ConvertImplicitly (type);
+                       return base.ConvertImplicitly (rc, type);
                }
 
                /// <summary>
@@ -1013,29 +1092,30 @@ namespace Mono.CSharp {
                ///   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) {
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.SByte:
                                if (Value >= SByte.MinValue && Value <= SByte.MaxValue)
                                        return new SByteConstant ((sbyte) Value, loc);
-                       } 
-                       else if (target_type == TypeManager.byte_type) {
+                               break;
+                       case BuildinTypeSpec.Type.Byte:
                                if (Value >= Byte.MinValue && Value <= Byte.MaxValue)
                                        return new ByteConstant ((byte) Value, loc);
-                       } 
-                       else if (target_type == TypeManager.short_type) {
+                               break;
+                       case BuildinTypeSpec.Type.Short:
                                if (Value >= Int16.MinValue && Value <= Int16.MaxValue)
                                        return new ShortConstant ((short) Value, loc);
-                       } 
-                       else if (target_type == TypeManager.ushort_type) {
+                               break;
+                       case BuildinTypeSpec.Type.UShort:
                                if (Value >= UInt16.MinValue && Value <= UInt16.MaxValue)
                                        return new UShortConstant ((ushort) Value, loc);
-                       } 
-                       else if (target_type == TypeManager.uint32_type) {
+                               break;
+                       case BuildinTypeSpec.Type.UInt:
                                if (Value >= 0)
                                        return new UIntConstant ((uint) Value, loc);
-                       } 
-                       else if (target_type == TypeManager.uint64_type) {
+                               break;
+                       case BuildinTypeSpec.Type.ULong:
                                //
                                // we can optimize this case: a positive int32
                                // always fits on a uint64.  But we need an opcode
@@ -1043,11 +1123,12 @@ namespace Mono.CSharp {
                                //
                                if (Value >= 0)
                                        return new ULongConstant ((ulong) Value, loc);
-                       } 
-                       else if (target_type == TypeManager.double_type)
+                               break;
+                       case BuildinTypeSpec.Type.Double:
                                return new DoubleConstant ((double) Value, loc);
-                       else if (target_type == TypeManager.float_type)
+                       case BuildinTypeSpec.Type.Float:
                                return new FloatConstant ((float) Value, loc);
+                       }
 
                        return null;
                }
@@ -1059,19 +1140,24 @@ namespace Mono.CSharp {
                public UIntConstant (uint v, Location loc):
                        base (loc)
                {
-                       type = TypeManager.uint32_type;
-                       eclass = ExprClass.Value;
                        Value = v;
                }
 
-               public override void Emit (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext rc)
                {
-                       IntLiteral.EmitInt (ec.ig, unchecked ((int) Value));
-               }
+                       type = TypeManager.uint32_type;
+                       eclass = ExprClass.Value;
+                       return this;
+               }
 
-               public override string AsString ()
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       return Value.ToString ();
+                       enc.Encode (Value);
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       ec.EmitInt (unchecked ((int) Value));
                }
 
                public override object GetValue ()
@@ -1079,6 +1165,11 @@ namespace Mono.CSharp {
                        return Value;
                }
 
+               public override long GetValueAsLong ()
+               {
+                       return Value;
+               }
+
                public override Constant Increment ()
                {
                        return new UIntConstant (checked(Value + 1), loc);
@@ -1095,65 +1186,67 @@ namespace Mono.CSharp {
                                return false;
                        }
                }
+               
+               public override bool IsOneInteger {
+                       get {
+                               return Value == 1;
+                       }
+               }               
 
                public override bool IsZeroInteger {
                        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){
-                                       if (Value < Char.MinValue || Value > Char.MaxValue)
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.Byte:
+                               if (in_checked_context) {
+                                       if (Value < 0 || Value > byte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new ByteConstant ((byte) Value, Location);
-                       }
-                       if (target_type == TypeManager.sbyte_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.SByte:
+                               if (in_checked_context) {
                                        if (Value > SByte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new SByteConstant ((sbyte) Value, Location);
-                       }
-                       if (target_type == TypeManager.short_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.Short:
+                               if (in_checked_context) {
                                        if (Value > Int16.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new ShortConstant ((short) Value, Location);
-                       }
-                       if (target_type == TypeManager.ushort_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.UShort:
+                               if (in_checked_context) {
                                        if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new UShortConstant ((ushort) Value, Location);
-                       }
-                       if (target_type == TypeManager.int32_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.Int:
+                               if (in_checked_context) {
                                        if (Value > Int32.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new IntConstant ((int) Value, Location);
-                       }
-                       if (target_type == TypeManager.int64_type)
+                       case BuildinTypeSpec.Type.Long:
                                return new LongConstant ((long) Value, Location);
-                       if (target_type == TypeManager.uint64_type)
+                       case BuildinTypeSpec.Type.ULong:
                                return new ULongConstant ((ulong) Value, Location);
-                       if (target_type == TypeManager.float_type)
+                       case BuildinTypeSpec.Type.Float:
                                return new FloatConstant ((float) Value, Location);
-                       if (target_type == TypeManager.double_type)
+                       case BuildinTypeSpec.Type.Double:
                                return new DoubleConstant ((double) Value, Location);
-                       if (target_type == TypeManager.char_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.Char:
+                               if (in_checked_context) {
                                        if (Value < Char.MinValue || Value > Char.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new CharConstant ((char) Value, Location);
-                       }
-                       if (target_type == TypeManager.decimal_type)
+                       case BuildinTypeSpec.Type.Decimal:
                                return new DecimalConstant ((decimal) Value, Location);
+                       }
 
                        return null;
                }
@@ -1166,36 +1259,24 @@ namespace Mono.CSharp {
                public LongConstant (long v, Location loc):
                        base (loc)
                {
-                       type = TypeManager.int64_type;
-                       eclass = ExprClass.Value;
                        Value = v;
                }
 
-               public override void Emit (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext rc)
                {
-                       EmitLong (ec.ig, Value);
+                       type = TypeManager.int64_type;
+                       eclass = ExprClass.Value;
+                       return this;
                }
 
-               static public void EmitLong (ILGenerator ig, long l)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       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);
+                       enc.Encode (Value);
                }
 
-               public override string AsString ()
+               public override void Emit (EmitContext ec)
                {
-                       return Value.ToString ();
+                       ec.EmitLong (Value);
                }
 
                public override object GetValue ()
@@ -1203,6 +1284,11 @@ namespace Mono.CSharp {
                        return Value;
                }
 
+               public override long GetValueAsLong ()
+               {
+                       return Value;
+               }
+
                public override Constant Increment ()
                {
                        return new LongConstant (checked(Value + 1), loc);
@@ -1219,84 +1305,84 @@ namespace Mono.CSharp {
                                return Value < 0;
                        }
                }
+               
+               public override bool IsOneInteger {
+                       get {
+                               return Value == 1;
+                       }
+               }               
 
                public override bool IsZeroInteger {
                        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){
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.Byte:
+                               if (in_checked_context) {
                                        if (Value < Byte.MinValue || Value > Byte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new ByteConstant ((byte) Value, Location);
-                       }
-                       if (target_type == TypeManager.sbyte_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.SByte:
+                               if (in_checked_context) {
                                        if (Value < SByte.MinValue || Value > SByte.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new SByteConstant ((sbyte) Value, Location);
-                       }
-                       if (target_type == TypeManager.short_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.Short:
+                               if (in_checked_context) {
                                        if (Value < Int16.MinValue || Value > Int16.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new ShortConstant ((short) Value, Location);
-                       }
-                       if (target_type == TypeManager.ushort_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.UShort:
+                               if (in_checked_context) {
                                        if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new UShortConstant ((ushort) Value, Location);
-                       }
-                       if (target_type == TypeManager.int32_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.Int:
+                               if (in_checked_context) {
                                        if (Value < Int32.MinValue || Value > Int32.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new IntConstant ((int) Value, Location);
-                       }
-                       if (target_type == TypeManager.uint32_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.UInt:
+                               if (in_checked_context) {
                                        if (Value < UInt32.MinValue || Value > UInt32.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new UIntConstant ((uint) Value, Location);
-                       }
-                       if (target_type == TypeManager.uint64_type) {
+                       case BuildinTypeSpec.Type.ULong:
                                if (in_checked_context && Value < 0)
                                        throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
-                       }
-                       if (target_type == TypeManager.float_type)
+                       case BuildinTypeSpec.Type.Float:
                                return new FloatConstant ((float) Value, Location);
-                       if (target_type == TypeManager.double_type)
+                       case BuildinTypeSpec.Type.Double:
                                return new DoubleConstant ((double) Value, Location);
-                       if (target_type == TypeManager.char_type) {
-                               if (in_checked_context){
+                       case BuildinTypeSpec.Type.Char:
+                               if (in_checked_context) {
                                        if (Value < Char.MinValue || Value > Char.MaxValue)
                                                throw new OverflowException ();
                                }
                                return new CharConstant ((char) Value, Location);
-                       }
-                       if (target_type == TypeManager.decimal_type)
+                       case BuildinTypeSpec.Type.Decimal:
                                return new DecimalConstant ((decimal) Value, Location);
+                       }
 
                        return null;
                }
 
-               public override Constant ConvertImplicitly (Type type)
+               public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
                {
                        if (Value >= 0 && type == TypeManager.uint64_type) {
-                               return new ULongConstant ((ulong) Value, loc);
+                               return new ULongConstant ((ulong) Value, loc).Resolve (rc);
                        }
 
-                       return base.ConvertImplicitly (type);
+                       return base.ConvertImplicitly (rc, type);
                }
        }
 
@@ -1306,21 +1392,24 @@ namespace Mono.CSharp {
                public ULongConstant (ulong v, Location loc):
                        base (loc)
                {
-                       type = TypeManager.uint64_type;
-                       eclass = ExprClass.Value;
                        Value = v;
                }
 
-               public override void Emit (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext rc)
                {
-                       ILGenerator ig = ec.ig;
+                       type = TypeManager.uint64_type;
+                       eclass = ExprClass.Value;
+                       return this;
+               }
 
-                       LongLiteral.EmitLong (ig, unchecked ((long) Value));
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Encode (Value);
                }
 
-               public override string AsString ()
+               public override void Emit (EmitContext ec)
                {
-                       return Value.ToString ();
+                       ec.EmitLong (unchecked ((long) Value));
                }
 
                public override object GetValue ()
@@ -1328,6 +1417,11 @@ namespace Mono.CSharp {
                        return Value;
                }
 
+               public override long GetValueAsLong ()
+               {
+                       return (long) Value;
+               }
+
                public override Constant Increment ()
                {
                        return new ULongConstant (checked(Value + 1), loc);
@@ -1344,59 +1438,59 @@ namespace Mono.CSharp {
                                return false;
                        }
                }
+               
+               public override bool IsOneInteger {
+                       get {
+                               return Value == 1;
+                       }
+               }               
 
                public override bool IsZeroInteger {
                        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) {
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.Byte:
                                if (in_checked_context && Value > Byte.MaxValue)
                                        throw new OverflowException ();
                                return new ByteConstant ((byte) Value, Location);
-                       }
-                       if (target_type == TypeManager.sbyte_type) {
+                       case BuildinTypeSpec.Type.SByte:
                                if (in_checked_context && Value > ((ulong) SByte.MaxValue))
                                        throw new OverflowException ();
                                return new SByteConstant ((sbyte) Value, Location);
-                       }
-                       if (target_type == TypeManager.short_type) {
+                       case BuildinTypeSpec.Type.Short:
                                if (in_checked_context && Value > ((ulong) Int16.MaxValue))
                                        throw new OverflowException ();
                                return new ShortConstant ((short) Value, Location);
-                       }
-                       if (target_type == TypeManager.ushort_type) {
+                       case BuildinTypeSpec.Type.UShort:
                                if (in_checked_context && Value > UInt16.MaxValue)
                                        throw new OverflowException ();
                                return new UShortConstant ((ushort) Value, Location);
-                       }
-                       if (target_type == TypeManager.int32_type) {
+                       case BuildinTypeSpec.Type.Int:
                                if (in_checked_context && Value > UInt32.MaxValue)
                                        throw new OverflowException ();
                                return new IntConstant ((int) Value, Location);
-                       }
-                       if (target_type == TypeManager.uint32_type) {
-                               if  (in_checked_context && Value > UInt32.MaxValue)
+                       case BuildinTypeSpec.Type.UInt:
+                               if (in_checked_context && Value > UInt32.MaxValue)
                                        throw new OverflowException ();
                                return new UIntConstant ((uint) Value, Location);
-                       }
-                       if (target_type == TypeManager.int64_type) {
+                       case BuildinTypeSpec.Type.Long:
                                if (in_checked_context && Value > Int64.MaxValue)
                                        throw new OverflowException ();
                                return new LongConstant ((long) Value, Location);
-                       }
-                       if (target_type == TypeManager.float_type)
+                       case BuildinTypeSpec.Type.Float:
                                return new FloatConstant ((float) Value, Location);
-                       if (target_type == TypeManager.double_type)
+                       case BuildinTypeSpec.Type.Double:
                                return new DoubleConstant ((double) Value, Location);
-                       if (target_type == TypeManager.char_type) {
+                       case BuildinTypeSpec.Type.Char:
                                if (in_checked_context && Value > Char.MaxValue)
                                        throw new OverflowException ();
                                return new CharConstant ((char) Value, Location);
-                       }
-                       if (target_type == TypeManager.decimal_type)
+                       case BuildinTypeSpec.Type.Decimal:
                                return new DecimalConstant ((decimal) Value, Location);
+                       }
 
                        return null;
                }
@@ -1408,20 +1502,25 @@ namespace Mono.CSharp {
 
                public FloatConstant (float v, Location loc):
                        base (loc)
+               {
+                       Value = v;
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
                {
                        type = TypeManager.float_type;
                        eclass = ExprClass.Value;
-                       Value = v;
+                       return this;
                }
 
-               public override void Emit (EmitContext ec)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       ec.ig.Emit (OpCodes.Ldc_R4, Value);
+                       enc.Encode (Value);
                }
 
-               public override string AsString ()
+               public override void Emit (EmitContext ec)
                {
-                       return Value.ToString ();
+                       ec.Emit (OpCodes.Ldc_R4, Value);
                }
 
                public override object GetValue ()
@@ -1429,9 +1528,14 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override Constant Increment ()
+               public override string GetValueAsLiteral ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override long GetValueAsLong ()
                {
-                       return new FloatConstant (checked(Value + 1), loc);
+                       throw new NotSupportedException ();
                }
 
                public override bool IsDefaultValue {
@@ -1446,75 +1550,68 @@ namespace Mono.CSharp {
                        }
                }
 
-               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){
-                                       if (Value < byte.MinValue || Value > byte.MaxValue)
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.Byte:
+                               if (in_checked_context) {
+                                       if (Value < byte.MinValue || Value > byte.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new ByteConstant ((byte) Value, Location);
-                       }
-                       if (target_type == TypeManager.sbyte_type) {
-                               if (in_checked_context){
-                                       if (Value <  sbyte.MinValue || Value > sbyte.MaxValue)
+                       case BuildinTypeSpec.Type.SByte:
+                               if (in_checked_context) {
+                                       if (Value < sbyte.MinValue || Value > sbyte.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new SByteConstant ((sbyte) Value, Location);
-                       }
-                       if (target_type == TypeManager.short_type) {
-                               if (in_checked_context){
-                                       if (Value < short.MinValue || Value > short.MaxValue)
+                       case BuildinTypeSpec.Type.Short:
+                               if (in_checked_context) {
+                                       if (Value < short.MinValue || Value > short.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new ShortConstant ((short) Value, Location);
-                       }
-                       if (target_type == TypeManager.ushort_type) {
-                               if (in_checked_context){
-                                       if (Value < ushort.MinValue || Value > ushort.MaxValue)
+                       case BuildinTypeSpec.Type.UShort:
+                               if (in_checked_context) {
+                                       if (Value < ushort.MinValue || Value > ushort.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new UShortConstant ((ushort) Value, Location);
-                       }
-                       if (target_type == TypeManager.int32_type) {
-                               if (in_checked_context){
-                                       if (Value < int.MinValue || Value > int.MaxValue)
+                       case BuildinTypeSpec.Type.Int:
+                               if (in_checked_context) {
+                                       if (Value < int.MinValue || Value > int.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new IntConstant ((int) Value, Location);
-                       }
-                       if (target_type == TypeManager.uint32_type) {
-                               if (in_checked_context){
-                                       if (Value < uint.MinValue || Value > uint.MaxValue)
+                       case BuildinTypeSpec.Type.UInt:
+                               if (in_checked_context) {
+                                       if (Value < uint.MinValue || Value > uint.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new UIntConstant ((uint) Value, Location);
-                       }
-                       if (target_type == TypeManager.int64_type) {
-                               if (in_checked_context){
-                                       if (Value < long.MinValue || Value > long.MaxValue)
+                       case BuildinTypeSpec.Type.Long:
+                               if (in_checked_context) {
+                                       if (Value < long.MinValue || Value > long.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new LongConstant ((long) Value, Location);
-                       }
-                       if (target_type == TypeManager.uint64_type) {
-                               if (in_checked_context){
-                                       if (Value < ulong.MinValue || Value > ulong.MaxValue)
+                       case BuildinTypeSpec.Type.ULong:
+                               if (in_checked_context) {
+                                       if (Value < ulong.MinValue || Value > ulong.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new ULongConstant ((ulong) Value, Location);
-                       }
-                       if (target_type == TypeManager.double_type)
+                       case BuildinTypeSpec.Type.Double:
                                return new DoubleConstant ((double) Value, Location);
-                       if (target_type == TypeManager.char_type) {
-                               if (in_checked_context){
-                                       if (Value < (float) char.MinValue || Value > (float) char.MaxValue)
+                       case BuildinTypeSpec.Type.Char:
+                               if (in_checked_context) {
+                                       if (Value < (float) char.MinValue || Value > (float) char.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new CharConstant ((char) Value, Location);
-                       }
-                       if (target_type == TypeManager.decimal_type)
+                       case BuildinTypeSpec.Type.Decimal:
                                return new DecimalConstant ((decimal) Value, Location);
+                       }
 
                        return null;
                }
@@ -1526,20 +1623,25 @@ namespace Mono.CSharp {
 
                public DoubleConstant (double v, Location loc):
                        base (loc)
+               {
+                       Value = v;
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
                {
                        type = TypeManager.double_type;
                        eclass = ExprClass.Value;
-                       Value = v;
+                       return this;
                }
 
-               public override void Emit (EmitContext ec)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       ec.ig.Emit (OpCodes.Ldc_R8, Value);
+                       enc.Encode (Value);
                }
 
-               public override string AsString ()
+               public override void Emit (EmitContext ec)
                {
-                       return Value.ToString ();
+                       ec.Emit (OpCodes.Ldc_R8, Value);
                }
 
                public override object GetValue ()
@@ -1547,9 +1649,14 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override Constant Increment ()
+               public override string GetValueAsLiteral ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override long GetValueAsLong ()
                {
-                       return new DoubleConstant (checked(Value + 1), loc);
+                       throw new NotSupportedException ();
                }
 
                public override bool IsDefaultValue {
@@ -1564,75 +1671,68 @@ namespace Mono.CSharp {
                        }
                }
 
-               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){
-                                       if (Value < Byte.MinValue || Value > Byte.MaxValue)
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.Byte:
+                               if (in_checked_context) {
+                                       if (Value < Byte.MinValue || Value > Byte.MaxValue || double.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new ByteConstant ((byte) Value, Location);
-                       }
-                       if (target_type == TypeManager.sbyte_type) {
-                               if (in_checked_context){
-                                       if (Value < SByte.MinValue || Value > SByte.MaxValue)
+                       case BuildinTypeSpec.Type.SByte:
+                               if (in_checked_context) {
+                                       if (Value < SByte.MinValue || Value > SByte.MaxValue || double.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new SByteConstant ((sbyte) Value, Location);
-                       }
-                       if (target_type == TypeManager.short_type) {
-                               if (in_checked_context){
-                                       if (Value < short.MinValue || Value > short.MaxValue)
+                       case BuildinTypeSpec.Type.Short:
+                               if (in_checked_context) {
+                                       if (Value < short.MinValue || Value > short.MaxValue || double.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new ShortConstant ((short) Value, Location);
-                       }
-                       if (target_type == TypeManager.ushort_type) {
-                               if (in_checked_context){
-                                       if (Value < ushort.MinValue || Value > ushort.MaxValue)
+                       case BuildinTypeSpec.Type.UShort:
+                               if (in_checked_context) {
+                                       if (Value < ushort.MinValue || Value > ushort.MaxValue || double.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new UShortConstant ((ushort) Value, Location);
-                       }
-                       if (target_type == TypeManager.int32_type) {
-                               if (in_checked_context){
-                                       if (Value < int.MinValue || Value > int.MaxValue)
+                       case BuildinTypeSpec.Type.Int:
+                               if (in_checked_context) {
+                                       if (Value < int.MinValue || Value > int.MaxValue || double.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new IntConstant ((int) Value, Location);
-                       }
-                       if (target_type == TypeManager.uint32_type) {
-                               if (in_checked_context){
-                                       if (Value < uint.MinValue || Value > uint.MaxValue)
+                       case BuildinTypeSpec.Type.UInt:
+                               if (in_checked_context) {
+                                       if (Value < uint.MinValue || Value > uint.MaxValue || double.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new UIntConstant ((uint) Value, Location);
-                       }
-                       if (target_type == TypeManager.int64_type) {
-                               if (in_checked_context){
-                                       if (Value < long.MinValue || Value > long.MaxValue)
+                       case BuildinTypeSpec.Type.Long:
+                               if (in_checked_context) {
+                                       if (Value < long.MinValue || Value > long.MaxValue || double.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new LongConstant ((long) Value, Location);
-                       }
-                       if (target_type == TypeManager.uint64_type) {
-                               if (in_checked_context){
-                                       if (Value < ulong.MinValue || Value > ulong.MaxValue)
+                       case BuildinTypeSpec.Type.ULong:
+                               if (in_checked_context) {
+                                       if (Value < ulong.MinValue || Value > ulong.MaxValue || double.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new ULongConstant ((ulong) Value, Location);
-                       }
-                       if (target_type == TypeManager.float_type)
+                       case BuildinTypeSpec.Type.Float:
                                return new FloatConstant ((float) Value, Location);
-                       if (target_type == TypeManager.char_type) {
-                               if (in_checked_context){
-                                       if (Value < (double) char.MinValue || Value > (double) char.MaxValue)
+                       case BuildinTypeSpec.Type.Char:
+                               if (in_checked_context) {
+                                       if (Value < (double) char.MinValue || Value > (double) char.MaxValue || double.IsNaN (Value))
                                                throw new OverflowException ();
                                }
                                return new CharConstant ((char) Value, Location);
-                       }
-                       if (target_type == TypeManager.decimal_type)
+                       case BuildinTypeSpec.Type.Decimal:
                                return new DecimalConstant ((decimal) Value, Location);
+                       }
 
                        return null;
                }
@@ -1645,58 +1745,60 @@ namespace Mono.CSharp {
                public DecimalConstant (decimal d, Location loc):
                        base (loc)
                {
-                       type = TypeManager.decimal_type;
-                       eclass = ExprClass.Value;
                        Value = d;
                }
 
-               override public string AsString ()
+               protected override Expression DoResolve (ResolveContext rc)
                {
-                       return Value.ToString () + "M";
-               }
-
-               public override object GetValue ()
-               {
-                       return (object) Value;
+                       type = TypeManager.decimal_type;
+                       eclass = ExprClass.Value;
+                       return this;
                }
 
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
-                       int [] words = Decimal.GetBits (Value);
+                       int [] words = decimal.GetBits (Value);
                        int power = (words [3] >> 16) & 0xff;
 
-                       if (power == 0 && Value <= int.MaxValue && Value >= int.MinValue)
-                       {
-                               if (TypeManager.void_decimal_ctor_int_arg == null) {
-                                       TypeManager.void_decimal_ctor_int_arg = TypeManager.GetPredefinedConstructor (
-                                               TypeManager.decimal_type, loc, TypeManager.int32_type);
+                       if (power == 0) {
+                               if (Value <= int.MaxValue && Value >= int.MinValue) {
+                                       if (TypeManager.void_decimal_ctor_int_arg == null) {
+                                               TypeManager.void_decimal_ctor_int_arg = TypeManager.GetPredefinedConstructor (
+                                                       TypeManager.decimal_type, loc, TypeManager.int32_type);
+
+                                               if (TypeManager.void_decimal_ctor_int_arg == null)
+                                                       return;
+                                       }
 
-                                       if (TypeManager.void_decimal_ctor_int_arg == null)
-                                               return;
+                                       ec.EmitInt ((int) Value);
+                                       ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_int_arg);
+                                       return;
                                }
 
-                               IntConstant.EmitInt (ig, (int)Value);
-                               ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_int_arg);
-                               return;
-                       }
+                               if (Value <= long.MaxValue && Value >= long.MinValue) {
+                                       if (TypeManager.void_decimal_ctor_long_arg == null) {
+                                               TypeManager.void_decimal_ctor_long_arg = TypeManager.GetPredefinedConstructor (
+                                                       TypeManager.decimal_type, loc, TypeManager.int64_type);
 
-                       
-                       //
-                       // FIXME: we could optimize this, and call a better 
-                       // constructor
-                       //
+                                               if (TypeManager.void_decimal_ctor_long_arg == null)
+                                                       return;
+                                       }
 
-                       IntConstant.EmitInt (ig, words [0]);
-                       IntConstant.EmitInt (ig, words [1]);
-                       IntConstant.EmitInt (ig, words [2]);
+                                       ec.EmitLong ((long) Value);
+                                       ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_long_arg);
+                                       return;
+                               }
+                       }
+
+                       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 (
@@ -1707,12 +1809,7 @@ namespace Mono.CSharp {
                                        return;
                        }
 
-                       ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
-               }
-
-               public override Constant Increment ()
-               {
-                       return new DecimalConstant (checked (Value + 1), loc);
+                       ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
                }
 
                public override bool IsDefaultValue {
@@ -1727,34 +1824,50 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
-               {
-                       if (target_type == TypeManager.sbyte_type)
-                               return new SByteConstant ((sbyte)Value, loc);
-                       if (target_type == TypeManager.byte_type)
-                               return new ByteConstant ((byte)Value, loc);
-                       if (target_type == TypeManager.short_type)
-                               return new ShortConstant ((short)Value, loc);
-                       if (target_type == TypeManager.ushort_type)
-                               return new UShortConstant ((ushort)Value, loc);
-                       if (target_type == TypeManager.int32_type)
-                               return new IntConstant ((int)Value, loc);
-                       if (target_type == TypeManager.uint32_type)
-                               return new UIntConstant ((uint)Value, loc);
-                       if (target_type == TypeManager.int64_type)
-                               return new LongConstant ((long)Value, loc);
-                       if (target_type == TypeManager.uint64_type)
-                               return new ULongConstant ((ulong)Value, loc);
-                       if (target_type == TypeManager.char_type)
-                               return new CharConstant ((char)Value, loc);
-                       if (target_type == TypeManager.float_type)
-                               return new FloatConstant ((float)Value, loc);
-                       if (target_type == TypeManager.double_type)
-                               return new DoubleConstant ((double)Value, loc);
+               public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+               {
+                       switch (target_type.BuildinType) {
+                       case BuildinTypeSpec.Type.SByte:
+                               return new SByteConstant ((sbyte) Value, loc);
+                       case BuildinTypeSpec.Type.Byte:
+                               return new ByteConstant ((byte) Value, loc);
+                       case BuildinTypeSpec.Type.Short:
+                               return new ShortConstant ((short) Value, loc);
+                       case BuildinTypeSpec.Type.UShort:
+                               return new UShortConstant ((ushort) Value, loc);
+                       case BuildinTypeSpec.Type.Int:
+                               return new IntConstant ((int) Value, loc);
+                       case BuildinTypeSpec.Type.UInt:
+                               return new UIntConstant ((uint) Value, loc);
+                       case BuildinTypeSpec.Type.Long:
+                               return new LongConstant ((long) Value, loc);
+                       case BuildinTypeSpec.Type.ULong:
+                               return new ULongConstant ((ulong) Value, loc);
+                       case BuildinTypeSpec.Type.Char:
+                               return new CharConstant ((char) Value, loc);
+                       case BuildinTypeSpec.Type.Float:
+                               return new FloatConstant ((float) Value, loc);
+                       case BuildinTypeSpec.Type.Double:
+                               return new DoubleConstant ((double) Value, loc);
+                       }
 
                        return null;
                }
 
+               public override object GetValue ()
+               {
+                       return Value;
+               }
+
+               public override string GetValueAsLiteral ()
+               {
+                       return Value.ToString () + "M";
+               }
+
+               public override long GetValueAsLong ()
+               {
+                       throw new NotSupportedException ();
+               }
        }
 
        public class StringConstant : Constant {
@@ -1763,26 +1876,36 @@ namespace Mono.CSharp {
                public StringConstant (string s, Location loc):
                        base (loc)
                {
-                       type = TypeManager.string_type;
-                       eclass = ExprClass.Value;
                        Value = s;
                }
 
-               // FIXME: Escape the string.
-               override public string AsString ()
+               protected override Expression DoResolve (ResolveContext rc)
                {
-                       return "\"" + Value + "\"";
+                       type = TypeManager.string_type;
+                       eclass = ExprClass.Value;
+                       return this;
                }
 
                public override object GetValue ()
                {
                        return Value;
                }
+
+               public override string GetValueAsLiteral ()
+               {
+                       // FIXME: Escape the string.
+                       return "\"" + Value + "\"";
+               }
+
+               public override long GetValueAsLong ()
+               {
+                       throw new NotSupportedException ();
+               }
                
                public override void Emit (EmitContext ec)
                {
                        if (Value == null) {
-                               ec.ig.Emit (OpCodes.Ldnull);
+                               ec.Emit (OpCodes.Ldnull);
                                return;
                        }
 
@@ -1790,22 +1913,26 @@ namespace Mono.CSharp {
                        // Use string.Empty for both literals and constants even if
                        // it's not allowed at language level
                        //
-                       if (Value.Length == 0 && RootContext.Optimize && ec.TypeContainer.TypeBuilder != TypeManager.string_type) {                     
+                       if (Value.Length == 0 && ec.Module.Compiler.Settings.Optimize && 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 Constant Increment ()
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       throw new NotSupportedException ();
+                       // cast to object
+                       if (type != targetType)
+                               enc.Encode (type);
+
+                       enc.Encode (Value);
                }
 
                public override bool IsDefaultValue {
@@ -1820,18 +1947,151 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+               public override bool IsNull {
+                       get {
+                               return IsDefaultValue;
+                       }
+               }
+
+               public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
                {
                        return null;
                }
        }
 
+       //
+       // Null constant can have its own type, think of `default (Foo)'
+       //
+       public class NullConstant : Constant
+       {
+               public NullConstant (TypeSpec type, Location loc)
+                       : base (loc)
+               {
+                       eclass = ExprClass.Value;
+                       this.type = type;
+               }
+
+               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 == InternalType.Null)
+                               enc.Encode (TypeManager.string_type);
+
+                       var ac = targetType as ArrayContainer;
+                       if (ac != null) {
+                               if (ac.Rank != 1 || ac.Element.IsArray)
+                                       base.EncodeAttributeValue (rc, enc, targetType);
+                               else
+                                       enc.Encode (uint.MaxValue);
+                       } else {
+                               enc.Encode (byte.MaxValue);
+                       }
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       ec.Emit (OpCodes.Ldnull);
+
+                       // Only to make verifier happy
+                       if (TypeManager.IsGenericParameter (type))
+                               ec.Emit (OpCodes.Unbox_Any, type);
+               }
+
+               public override string ExprClassName {
+                       get {
+                               return GetSignatureForError ();
+                       }
+               }
+
+               public override Constant ConvertExplicitly (bool inCheckedContext, TypeSpec targetType)
+               {
+                       if (targetType.IsPointer) {
+                               if (IsLiteral || this is NullPointer)
+                                       return new EmptyConstantCast (new NullPointer (loc), targetType);
+
+                               return null;
+                       }
+
+                       // Exlude internal compiler types
+                       if (targetType.Kind == MemberKind.InternalCompilerType && targetType != InternalType.Dynamic && targetType != InternalType.Null)
+                               return null;
+
+                       if (!IsLiteral && !Convert.ImplicitStandardConversionExists (this, targetType))
+                               return null;
+
+                       if (TypeManager.IsReferenceType (targetType))
+                               return new NullConstant (targetType, loc);
+
+                       if (TypeManager.IsNullableType (targetType))
+                               return Nullable.LiftedNull.Create (targetType, loc);
+
+                       return null;
+               }
+
+               public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec targetType)
+               {
+                       return ConvertExplicitly (false, targetType);
+               }
+
+               public override string GetSignatureForError ()
+               {
+                       return "null";
+               }
+
+               public override object GetValue ()
+               {
+                       return null;
+               }
+
+               public override string GetValueAsLiteral ()
+               {
+                       return GetSignatureForError ();
+               }
+
+               public override long GetValueAsLong ()
+               {
+                       throw new NotSupportedException ();
+               }
+
+               public override bool IsDefaultValue {
+                       get { return true; }
+               }
+
+               public override bool IsNegative {
+                       get { return false; }
+               }
+
+               public override bool IsNull {
+                       get { return true; }
+               }
+
+               public override bool IsZeroInteger {
+                       get { return true; }
+               }
+       }
+
        /// <summary>
        ///   The value is constant, but when emitted has a side effect.  This is
        ///   used by BitwiseAnd to ensure that the second expression is invoked
        ///   regardless of the value of the left side.  
        /// </summary>
-       
        public class SideEffectConstant : Constant {
                public Constant value;
                Expression side_effect;
@@ -1842,13 +2102,15 @@ namespace Mono.CSharp {
                        while (side_effect is SideEffectConstant)
                                side_effect = ((SideEffectConstant) side_effect).side_effect;
                        this.side_effect = side_effect;
-                       eclass = ExprClass.Value;
-                       type = value.Type;
                }
 
-               public override string AsString ()
+               protected override Expression DoResolve (ResolveContext rc)
                {
-                       return value.AsString ();
+                       value = value.Resolve (rc);
+
+                       type = value.Type;
+                       eclass = ExprClass.Value;
+                       return this;
                }
 
                public override object GetValue ()
@@ -1856,6 +2118,16 @@ namespace Mono.CSharp {
                        return value.GetValue ();
                }
 
+               public override string GetValueAsLiteral ()
+               {
+                       return value.GetValueAsLiteral ();
+               }
+
+               public override long GetValueAsLong ()
+               {
+                       return value.GetValueAsLong ();
+               }
+
                public override void Emit (EmitContext ec)
                {
                        side_effect.EmitSideEffect (ec);
@@ -1872,11 +2144,6 @@ namespace Mono.CSharp {
                        get { return value.IsDefaultValue; }
                }
 
-               public override Constant Increment ()
-               {
-                       throw new NotSupportedException ();
-               }
-               
                public override bool IsNegative {
                        get { return value.IsNegative; }
                }
@@ -1885,10 +2152,16 @@ namespace Mono.CSharp {
                        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);
+                       if (new_value == null)
+                               return null;
+
+                       var c = new SideEffectConstant (new_value, side_effect, new_value.Location);
+                       c.type = target_type;
+                       c.eclass = eclass;
+                       return c;
                }
        }
 }