2010-01-15 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / mcs / nullable.cs
index 2c0073703fde9d45b3320f824630f0687dd8bb34..3d37b687536b1459d3f2b58d914d3d9baba137b6 100644 (file)
@@ -14,7 +14,6 @@
 using System;
 using System.Reflection;
 using System.Reflection.Emit;
-using System.Collections;
        
 namespace Mono.CSharp.Nullable
 {
@@ -38,7 +37,7 @@ namespace Mono.CSharp.Nullable
                {
                        if (TypeManager.generic_nullable_type == null) {
                                TypeManager.generic_nullable_type = TypeManager.CoreLookupType (ec.Compiler,
-                                       "System", "Nullable`1", Kind.Struct, true);
+                                       "System", "Nullable`1", MemberKind.Struct, true);
                        }
 
                        TypeArguments args = new TypeArguments (underlying);
@@ -56,22 +55,22 @@ namespace Mono.CSharp.Nullable
        {
                public readonly Type Type;
                public readonly Type UnderlyingType;
-               public MethodInfo HasValue;
-               public MethodInfo Value;
-               public MethodInfo GetValueOrDefault;
-               public ConstructorInfo Constructor;
+               public MethodSpec HasValue;
+               public MethodSpec Value;
+               public MethodSpec GetValueOrDefault;
+               public MethodSpec Constructor;
 
                public NullableInfo (Type type)
                {
                        Type = type;
                        UnderlyingType = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (type) [0]);
 
-                       PropertyInfo has_value_pi = TypeManager.GetPredefinedProperty (type, "HasValue", Location.Null, Type.EmptyTypes);
-                       PropertyInfo value_pi = TypeManager.GetPredefinedProperty (type, "Value", Location.Null, Type.EmptyTypes);
+                       var has_value_pi = TypeManager.GetPredefinedProperty (type, "HasValue", Location.Null, Type.EmptyTypes);
+                       var value_pi = TypeManager.GetPredefinedProperty (type, "Value", Location.Null, Type.EmptyTypes);
                        GetValueOrDefault = TypeManager.GetPredefinedMethod (type, "GetValueOrDefault", Location.Null, Type.EmptyTypes);
 
-                       HasValue = has_value_pi.GetGetMethod (false);
-                       Value = value_pi.GetGetMethod (false);
+                       HasValue = Import.CreateMethod (has_value_pi.MetaInfo.GetGetMethod (false));
+                       Value = Import.CreateMethod (value_pi.MetaInfo.GetGetMethod (false));
 
                        // When compiling corlib
                        if (TypeManager.IsBeingCompiled (type)) {
@@ -80,19 +79,19 @@ namespace Mono.CSharp.Nullable
                                // TODO: check for correct overload
                                Constructor c = ((Constructor) tc.InstanceConstructors [0]);
 
-                               Constructor = TypeBuilder.GetConstructor (type, c.ConstructorBuilder);
+                               Constructor = Import.CreateMethod (TypeBuilder.GetConstructor (type, c.ConstructorBuilder));
                                return;
                        }
 
 #if MS_COMPATIBLE
-                       if (TypeManager.IsBeingCompiled (UnderlyingType)) {
-                               ConstructorInfo cinfo = TypeManager.DropGenericTypeArguments (type).GetConstructors ()[0];
-                               Constructor = TypeBuilder.GetConstructor (type, cinfo);
-                               return;
-                       }
+//                     if (TypeManager.IsBeingCompiled (UnderlyingType)) {
+//                             ConstructorInfo cinfo = TypeManager.DropGenericTypeArguments (type).GetConstructors ()[0];
+//                             Constructor = TypeBuilder.GetConstructor (type, cinfo);
+//                             return;
+//                     }
 #endif
 
-                       Constructor = type.GetConstructor (new Type[] { UnderlyingType });
+                       Constructor = Import.CreateMethod (type.GetConstructor (new Type[] { UnderlyingType }));
                }
        }
 
@@ -205,20 +204,18 @@ namespace Mono.CSharp.Nullable
                                LocalVariable.Emit (ec);
                }
 
-#if NET_4_0
                public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
                {
                        return expr.MakeExpression (ctx);
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        type = storey.MutateType (type);
-                       info.Constructor = storey.MutateConstructor (info.Constructor);
-                       info.HasValue = storey.MutateGenericMethod (info.HasValue);
-                       info.GetValueOrDefault = storey.MutateGenericMethod (info.GetValueOrDefault);
-                       info.Value = storey.MutateGenericMethod (info.Value);
+                       storey.MutateConstructor (info.Constructor);
+                       storey.MutateGenericMethod (info.HasValue);
+                       storey.MutateGenericMethod (info.GetValueOrDefault);
+                       storey.MutateGenericMethod (info.Value);
                }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
@@ -284,7 +281,7 @@ namespace Mono.CSharp.Nullable
                        public override void Emit (EmitContext ec)
                        {
                                expr.Emit (ec);
-                               ec.ig.Emit (OpCodes.Newobj, info.Constructor);
+                               ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) info.Constructor.MetaInfo);
                        }
                }
        }
@@ -330,7 +327,7 @@ namespace Mono.CSharp.Nullable
                public override void Emit (EmitContext ec)
                {
                        child.Emit (ec);
-                       ec.ig.Emit (OpCodes.Newobj, info.Constructor);
+                       ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) info.Constructor.MetaInfo);
                }
        }
 
@@ -485,9 +482,6 @@ namespace Mono.CSharp.Nullable
 
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        unwrap = Unwrap.Create (Expr, false);
                        if (unwrap == null)
                                return null;
@@ -525,7 +519,7 @@ namespace Mono.CSharp.Nullable
                                EmitOperator (ec, ni.UnderlyingType);
                        }
 
-                       ig.Emit (OpCodes.Newobj, ni.Constructor);
+                       ig.Emit (OpCodes.Newobj, (ConstructorInfo) ni.Constructor.MetaInfo);
                        ig.Emit (OpCodes.Br_S, end_label);
 
                        ig.MarkLabel (is_null_label);
@@ -579,7 +573,7 @@ namespace Mono.CSharp.Nullable
                bool left_null_lifted, right_null_lifted;
                Expression left_orig, right_orig;
                Expression user_operator;
-               ConstructorInfo wrap_ctor;
+               MethodSpec wrap_ctor;
 
                public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
                                             Location loc)
@@ -619,9 +613,6 @@ namespace Mono.CSharp.Nullable
 
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        if ((Oper & Operator.LogicalMask) != 0) {
                                Error_OperatorCannotBeApplied (ec, left, right);
                                return null;
@@ -813,7 +804,7 @@ namespace Mono.CSharp.Nullable
                        EmitOperator (ec, left.Type);
 
                        if (wrap_ctor != null)
-                               ig.Emit (OpCodes.Newobj, wrap_ctor);
+                               ig.Emit (OpCodes.Newobj, (ConstructorInfo) wrap_ctor.MetaInfo);
 
                        ig.Emit (OpCodes.Br_S, end_label);
                        ig.MarkLabel (is_null_label);
@@ -1055,9 +1046,6 @@ namespace Mono.CSharp.Nullable
 
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        left = left.Resolve (ec);
                        right = right.Resolve (ec);
 
@@ -1136,8 +1124,6 @@ namespace Mono.CSharp.Nullable
                        this.expr = expr;
                        this.Mode = mode;
                        this.loc = loc;
-
-                       eclass = ExprClass.Value;
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
@@ -1159,6 +1145,8 @@ namespace Mono.CSharp.Nullable
                        if (underlying == null)
                                return null;
 
+
+                       eclass = ExprClass.Value;
                        type = expr.Type;
                        return this;
                }