2010-06-15 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / constant.cs
index 12c7892d5fe44a39a2d61db05bb1da9427ae4e42..c04ad0246df1417ee4d111d3245a9f5e4e2370c1 100644 (file)
@@ -39,23 +39,6 @@ namespace Mono.CSharp {
                        return this.GetType ().Name + " (" + AsString () + ")";
                }
 
-               public override bool GetAttributableValue (ResolveContext ec, Type value_type, out object value)
-               {
-                       if (value_type == TypeManager.object_type) {
-                               value = GetTypedValue ();
-                               return true;
-                       }
-
-                       Constant c = ImplicitConversionRequired (ec, value_type, loc);
-                       if (c == null) {
-                               value = null;
-                               return false;
-                       }
-
-                       value = c.GetTypedValue ();
-                       return true;
-               }
-
                /// <summary>
                ///  This is used to obtain the actual value of the literal
                ///  cast into an object.
@@ -67,7 +50,7 @@ namespace Mono.CSharp {
                        return GetValue ();
                }
 
-               public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, Type target, bool expl)
+               public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
                {
                        if (!expl && IsLiteral && 
                                (TypeManager.IsPrimitiveType (target) || type == TypeManager.decimal_type) &&
@@ -79,7 +62,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public Constant ImplicitConversionRequired (ResolveContext ec, Type type, Location loc)
+               public Constant ImplicitConversionRequired (ResolveContext ec, TypeSpec type, Location loc)
                {
                        Constant c = ConvertImplicitly (ec, type);
                        if (c == null)
@@ -88,7 +71,7 @@ namespace Mono.CSharp {
                        return c;
                }
 
-               public virtual Constant ConvertImplicitly (ResolveContext rc, Type type)
+               public virtual Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
                {
                        if (this.type == type)
                                return this;
@@ -113,12 +96,12 @@ namespace Mono.CSharp {
                //
                //  Returns a constant instance based on Type
                //
-               public static Constant CreateConstant (ResolveContext rc, Type t, object v, Location loc)
+               public static Constant CreateConstant (ResolveContext rc, TypeSpec t, object v, Location loc)
                {
                        return CreateConstantFromValue (t, v, loc).Resolve (rc);
                }
 
-               public static Constant CreateConstantFromValue (Type t, object v, Location loc)
+               public static Constant CreateConstantFromValue (TypeSpec t, object v, Location loc)
                {
                        if (t == TypeManager.int32_type)
                                return new IntConstant ((int) v, loc);
@@ -149,7 +132,7 @@ namespace Mono.CSharp {
                        if (t == TypeManager.decimal_type)
                                return new DecimalConstant ((decimal) v, loc);
                        if (TypeManager.IsEnumType (t)) {
-                               Type real_type = TypeManager.GetEnumUnderlyingType (t);
+                               var real_type = EnumSpec.GetUnderlyingType (t);
                                return new EnumConstant (CreateConstantFromValue (real_type, v, loc).Resolve (null), t);
                        }
                        if (v == null) {
@@ -179,12 +162,12 @@ 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 (ResolveContext ec, Type target_type, Location loc)
+               public Constant TryReduce (ResolveContext ec, TypeSpec target_type, Location loc)
                {
                        try {
                                return TryReduce (ec, target_type);
@@ -201,14 +184,14 @@ namespace Mono.CSharp {
                        }
                }
 
-               Constant TryReduce (ResolveContext ec, Type target_type)
+               Constant TryReduce (ResolveContext ec, TypeSpec target_type)
                {
                        if (Type == target_type)
                                return this;
 
                        Constant c;
                        if (TypeManager.IsEnumType (target_type)) {
-                               c = TryReduce (ec, TypeManager.GetEnumUnderlyingType (target_type));
+                               c = TryReduce (ec, EnumSpec.GetUnderlyingType (target_type));
                                if (c == null)
                                        return null;
 
@@ -226,7 +209,7 @@ namespace Mono.CSharp {
                /// 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;
@@ -273,12 +256,7 @@ namespace Mono.CSharp {
 
                public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
                {
-                       return System.Linq.Expressions.Expression.Constant (GetTypedValue (), type);
-               }
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       // A constant cannot be of generic type
+                       return System.Linq.Expressions.Expression.Constant (GetTypedValue (), type.GetMetaInfo ());
                }
 
                public new Constant Resolve (ResolveContext rc)
@@ -309,7 +287,7 @@ namespace Mono.CSharp {
                {
                }
 
-               public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, Type target, bool expl)
+               public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
                {
                        try {
                                ConvertExplicitly (true, target);
@@ -350,13 +328,18 @@ namespace Mono.CSharp {
                {
                        return (object) Value;
                }
+
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Stream.Write (Value);
+               }
                
                public override void Emit (EmitContext ec)
                {
                        if (Value)
-                               ec.ig.Emit (OpCodes.Ldc_I4_1);
+                               ec.Emit (OpCodes.Ldc_I4_1);
                        else
-                               ec.ig.Emit (OpCodes.Ldc_I4_0);
+                               ec.Emit (OpCodes.Ldc_I4_0);
                }
 
                public override bool IsDefaultValue {
@@ -375,7 +358,7 @@ 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;
                }
@@ -391,9 +374,14 @@ namespace Mono.CSharp {
                        Value = v;
                }
 
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Stream.Write (Value);
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       IntLiteral.EmitInt (ec.ig, Value);
+                       ec.EmitInt (Value);
                }
 
                public override string AsString ()
@@ -440,7 +428,7 @@ 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) {
                                if (in_checked_context){
@@ -491,9 +479,14 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Stream.Write ((ushort) Value);
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       IntLiteral.EmitInt (ec.ig, Value);
+                       ec.EmitInt (Value);
                }
 
                static string descape (char c)
@@ -551,7 +544,7 @@ 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){
@@ -610,9 +603,14 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Stream.Write (Value);
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       IntLiteral.EmitInt (ec.ig, Value);
+                       ec.EmitInt (Value);
                }
 
                public override string AsString ()
@@ -652,7 +650,7 @@ 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 && Value < 0)
@@ -711,9 +709,14 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Stream.Write (Value);
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       IntLiteral.EmitInt (ec.ig, Value);
+                       ec.EmitInt (Value);
                }
 
                public override string AsString ()
@@ -753,7 +756,7 @@ 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){
@@ -824,9 +827,14 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Stream.Write (Value);
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       IntLiteral.EmitInt (ec.ig, Value);
+                       ec.EmitInt (Value);
                }
 
                public override string AsString ()
@@ -866,7 +874,7 @@ 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){
@@ -931,61 +939,14 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               static public void EmitInt (ILGenerator ig, int i)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       switch (i){
-                       case -1:
-                               ig.Emit (OpCodes.Ldc_I4_M1);
-                               break;
-                               
-                       case 0:
-                               ig.Emit (OpCodes.Ldc_I4_0);
-                               break;
-                               
-                       case 1:
-                               ig.Emit (OpCodes.Ldc_I4_1);
-                               break;
-                               
-                       case 2:
-                               ig.Emit (OpCodes.Ldc_I4_2);
-                               break;
-                               
-                       case 3:
-                               ig.Emit (OpCodes.Ldc_I4_3);
-                               break;
-                               
-                       case 4:
-                               ig.Emit (OpCodes.Ldc_I4_4);
-                               break;
-                               
-                       case 5:
-                               ig.Emit (OpCodes.Ldc_I4_5);
-                               break;
-                               
-                       case 6:
-                               ig.Emit (OpCodes.Ldc_I4_6);
-                               break;
-                               
-                       case 7:
-                               ig.Emit (OpCodes.Ldc_I4_7);
-                               break;
-                               
-                       case 8:
-                               ig.Emit (OpCodes.Ldc_I4_8);
-                               break;
-
-                       default:
-                               if (i >= -128 && i <= 127){
-                                       ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i);
-                               } else
-                                       ig.Emit (OpCodes.Ldc_I4, i);
-                               break;
-                       }
+                       enc.Stream.Write (Value);
                }
 
                public override void Emit (EmitContext ec)
                {
-                       EmitInt (ec.ig, Value);
+                       ec.EmitInt (Value);
                }
 
                public override string AsString ()
@@ -1025,7 +986,7 @@ 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){
@@ -1086,7 +1047,7 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               public override Constant ConvertImplicitly (ResolveContext rc, Type type)
+               public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
                {
                        if (this.type == type)
                                return this;
@@ -1103,7 +1064,7 @@ 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) {
                                if (Value >= SByte.MinValue && Value <= SByte.MaxValue)
@@ -1159,9 +1120,14 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Stream.Write (Value);
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       IntLiteral.EmitInt (ec.ig, unchecked ((int) Value));
+                       ec.EmitInt (unchecked ((int) Value));
                }
 
                public override string AsString ()
@@ -1201,7 +1167,7 @@ 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){
@@ -1277,26 +1243,14 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               public override void Emit (EmitContext ec)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       EmitLong (ec.ig, Value);
+                       enc.Stream.Write (Value);
                }
 
-               static public void EmitLong (ILGenerator ig, long l)
+               public override void Emit (EmitContext ec)
                {
-                       if (l >= int.MinValue && l <= int.MaxValue) {
-                               IntLiteral.EmitInt (ig, unchecked ((int) l));
-                               ig.Emit (OpCodes.Conv_I8);
-                               return;
-                       }
-
-                       if (l >= 0 && l <= uint.MaxValue) {
-                               IntLiteral.EmitInt (ig, unchecked ((int) l));
-                               ig.Emit (OpCodes.Conv_U8);
-                               return;
-                       }
-                       
-                       ig.Emit (OpCodes.Ldc_I8, l);
+                       ec.EmitLong (Value);
                }
 
                public override string AsString ()
@@ -1336,7 +1290,7 @@ 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){
@@ -1402,7 +1356,7 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               public override Constant ConvertImplicitly (ResolveContext rc, Type type)
+               public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
                {
                        if (Value >= 0 && type == TypeManager.uint64_type) {
                                return new ULongConstant ((ulong) Value, loc).Resolve (rc);
@@ -1428,11 +1382,14 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               public override void Emit (EmitContext ec)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       ILGenerator ig = ec.ig;
+                       enc.Stream.Write (Value);
+               }
 
-                       LongLiteral.EmitLong (ig, unchecked ((long) Value));
+               public override void Emit (EmitContext ec)
+               {
+                       ec.EmitLong (unchecked ((long) Value));
                }
 
                public override string AsString ()
@@ -1472,7 +1429,7 @@ 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 && Value > Byte.MaxValue)
@@ -1542,9 +1499,14 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Stream.Write (Value);
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       ec.ig.Emit (OpCodes.Ldc_R4, Value);
+                       ec.Emit (OpCodes.Ldc_R4, Value);
                }
 
                public override string AsString ()
@@ -1569,7 +1531,7 @@ 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){
@@ -1660,9 +1622,14 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       enc.Stream.Write (Value);
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       ec.ig.Emit (OpCodes.Ldc_R8, Value);
+                       ec.Emit (OpCodes.Ldc_R8, Value);
                }
 
                public override string AsString ()
@@ -1687,7 +1654,7 @@ 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){
@@ -1790,8 +1757,6 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
                        int [] words = decimal.GetBits (Value);
                        int power = (words [3] >> 16) & 0xff;
 
@@ -1805,8 +1770,8 @@ namespace Mono.CSharp {
                                                        return;
                                        }
 
-                                       IntConstant.EmitInt (ig, (int) Value);
-                                       ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_int_arg);
+                                       ec.EmitInt ((int) Value);
+                                       ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_int_arg);
                                        return;
                                }
 
@@ -1819,21 +1784,21 @@ namespace Mono.CSharp {
                                                        return;
                                        }
 
-                                       LongConstant.EmitLong (ig, (long) Value);
-                                       ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_long_arg);
+                                       ec.EmitLong ((long) Value);
+                                       ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_long_arg);
                                        return;
                                }
                        }
 
-                       IntConstant.EmitInt (ig, words [0]);
-                       IntConstant.EmitInt (ig, words [1]);
-                       IntConstant.EmitInt (ig, words [2]);
+                       ec.EmitInt (words [0]);
+                       ec.EmitInt (words [1]);
+                       ec.EmitInt (words [2]);
 
                        // sign
-                       IntConstant.EmitInt (ig, words [3] >> 31);
+                       ec.EmitInt (words [3] >> 31);
 
                        // power
-                       IntConstant.EmitInt (ig, power);
+                       ec.EmitInt (power);
 
                        if (TypeManager.void_decimal_ctor_five_args == null) {
                                TypeManager.void_decimal_ctor_five_args = TypeManager.GetPredefinedConstructor (
@@ -1844,7 +1809,7 @@ namespace Mono.CSharp {
                                        return;
                        }
 
-                       ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
+                       ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
                }
 
                public override bool IsDefaultValue {
@@ -1859,7 +1824,7 @@ 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.sbyte_type)
                                return new SByteConstant ((sbyte)Value, loc);
@@ -1919,7 +1884,7 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        if (Value == null) {
-                               ec.ig.Emit (OpCodes.Ldnull);
+                               ec.Emit (OpCodes.Ldnull);
                                return;
                        }
 
@@ -1929,15 +1894,24 @@ namespace Mono.CSharp {
                        //
                        if (Value.Length == 0 && RootContext.Optimize && !TypeManager.IsEqual (ec.CurrentType, TypeManager.string_type)) {
                                if (TypeManager.string_empty == null)
-                                       TypeManager.string_empty = TypeManager.GetPredefinedField (TypeManager.string_type, "Empty", loc);
+                                       TypeManager.string_empty = TypeManager.GetPredefinedField (TypeManager.string_type, "Empty", loc, TypeManager.string_type);
 
                                if (TypeManager.string_empty != null) {
-                                       ec.ig.Emit (OpCodes.Ldsfld, TypeManager.string_empty);
+                                       ec.Emit (OpCodes.Ldsfld, TypeManager.string_empty);
                                        return;
                                }
                        }
 
-                       ec.ig.Emit (OpCodes.Ldstr, Value);
+                       ec.Emit (OpCodes.Ldstr, Value);
+               }
+
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       // cast to object
+                       if (type != targetType)
+                               enc.Encode (type);
+
+                       enc.Encode (Value);
                }
 
                public override bool IsDefaultValue {
@@ -1958,7 +1932,7 @@ 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)
                {
                        return null;
                }
@@ -1969,7 +1943,7 @@ namespace Mono.CSharp {
        //
        public class NullConstant : Constant
        {
-               public NullConstant (Type type, Location loc)
+               public NullConstant (TypeSpec type, Location loc)
                        : base (loc)
                {
                        eclass = ExprClass.Value;
@@ -1981,18 +1955,47 @@ namespace Mono.CSharp {
                        return GetSignatureForError ();
                }
 
+               public override Expression CreateExpressionTree (ResolveContext ec)
+               {
+                       if (type == InternalType.Null || type == TypeManager.object_type) {
+                               // Optimized version, also avoids referencing literal internal type
+                               Arguments args = new Arguments (1);
+                               args.Add (new Argument (this));
+                               return CreateExpressionFactoryCall (ec, "Constant", args);
+                       }
+
+                       return base.CreateExpressionTree (ec);
+               }
+
                protected override Expression DoResolve (ResolveContext ec)
                {
                        return this;
                }
 
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               {
+                       // Type it as string cast
+                       if (targetType == TypeManager.object_type || targetType == TypeManager.null_type)
+                               enc.Encode (TypeManager.string_type);
+
+                       var ac = targetType as ArrayContainer;
+                       if (ac != null) {
+                               if (ac.Rank != 1)
+                                       base.EncodeAttributeValue (rc, enc, targetType);
+                               else
+                                       enc.Stream.Write (uint.MaxValue);
+                       } else {
+                               enc.Stream.Write (byte.MaxValue);
+                       }
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       ec.ig.Emit (OpCodes.Ldnull);
+                       ec.Emit (OpCodes.Ldnull);
 
                        // Only to make verifier happy
                        if (TypeManager.IsGenericParameter (type))
-                               ec.ig.Emit (OpCodes.Unbox_Any, type);
+                               ec.Emit (OpCodes.Unbox_Any, type);
                }
 
                public override string ExprClassName {
@@ -2006,7 +2009,7 @@ namespace Mono.CSharp {
                        return "null";
                }
 
-               public override Constant ConvertExplicitly (bool inCheckedContext, Type targetType)
+               public override Constant ConvertExplicitly (bool inCheckedContext, TypeSpec targetType)
                {
                        if (targetType.IsPointer) {
                                if (IsLiteral || this is NullPointer)
@@ -2031,7 +2034,7 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               public override Constant ConvertImplicitly (ResolveContext rc, Type targetType)
+               public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec targetType)
                {
                        return ConvertExplicitly (false, targetType);
                }
@@ -2056,11 +2059,6 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return true; }
                }
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       type = storey.MutateType (type);
-               }
        }
 
        /// <summary>
@@ -2123,7 +2121,7 @@ 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);