New tests.
[mono.git] / mcs / mcs / dynamic.cs
index 23d50727e53d7e9d1d99407b4ebb4055d3febe1c..3a466401fb857036e2506f1ab314108f870ab70f 100644 (file)
@@ -9,12 +9,11 @@
 //
 
 using System;
-using System.Collections;
 using System.Reflection.Emit;
+using SLE = System.Linq.Expressions;
 
 #if NET_4_0
 using System.Dynamic;
-using SLE = System.Linq.Expressions;
 #endif
 
 namespace Mono.CSharp
@@ -72,18 +71,18 @@ namespace Mono.CSharp
 #if !NET_4_0
                public class DynamicMetaObject
                {
-                       public Type RuntimeType;
-                       public Type LimitType;
+                       public TypeSpec RuntimeType;
+                       public TypeSpec LimitType;
+                       public SLE.Expression Expression;
                }
 #endif
 
                readonly DynamicMetaObject obj;
 
-               public RuntimeValueExpression (DynamicMetaObject obj, bool isCompileTimeType)
+               public RuntimeValueExpression (DynamicMetaObject obj, TypeSpec type)
                {
                        this.obj = obj;
-                       this.type = isCompileTimeType ? obj.LimitType : obj.RuntimeType;
-                       this.type = obj.LimitType;
+                       this.type = type;
                        this.eclass = ExprClass.Variable;
                }
 
@@ -97,7 +96,7 @@ namespace Mono.CSharp
                        throw new NotImplementedException ();
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return this;
                }
@@ -126,7 +125,6 @@ namespace Mono.CSharp
 
                #endregion
 
-#if NET_4_0
                public SLE.Expression MakeAssignExpression (BuilderContext ctx)
                {
                        return obj.Expression;
@@ -134,9 +132,8 @@ namespace Mono.CSharp
 
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       return SLE.Expression.Convert (obj.Expression, type);
+                       return SLE.Expression.Convert (obj.Expression, type.GetMetaInfo ());
                }
-#endif
 
                public DynamicMetaObject MetaObject {
                        get { return obj; }
@@ -150,15 +147,15 @@ namespace Mono.CSharp
        //
        public class DynamicResultCast : ShimExpression
        {
-               public DynamicResultCast (Type type, Expression expr)
+               public DynamicResultCast (TypeSpec type, Expression expr)
                        : base (expr)
                {
                        this.type = type;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       expr = expr.DoResolve (ec);
+                       expr = expr.Resolve (ec);
                        eclass = ExprClass.Value;
                        return this;
                }
@@ -166,7 +163,7 @@ namespace Mono.CSharp
 #if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       return SLE.Expression.Block (expr.MakeExpression (ctx), SLE.Expression.Default (type));
+                       return SLE.Expression.Block (expr.MakeExpression (ctx), SLE.Expression.Default (type.GetMetaInfo ()));
                }
 #endif
        }
@@ -204,7 +201,6 @@ namespace Mono.CSharp
                                        new MemberName (CompilerGeneratedClass.MakeName (null, "c", "DynamicSites", 0)),
                                        Modifiers.INTERNAL | Modifiers.STATIC)
                        {
-                               ModFlags &= ~Modifiers.SEALED;
                        }
                }
 
@@ -219,15 +215,15 @@ namespace Mono.CSharp
                        CSharpBinderFlags flags;
 
                        public BinderFlags (CSharpBinderFlags flags, DynamicExpressionStatement statement)
-                               : base ()
+                               : base (statement.loc)
                        {
                                this.flags = flags;
                                this.statement = statement;
                        }
 
-                       public override Expression DoResolve (ResolveContext ec)
+                       protected override Expression DoResolve (ResolveContext ec)
                        {
-                               Child = new IntConstant ((int) (flags | statement.flags), statement.loc);
+                               Child = new IntConstant ((int) (flags | statement.flags), statement.loc).Resolve (ec);
 
                                type = TypeManager.binder_flags;
                                eclass = Child.eclass;
@@ -264,11 +260,9 @@ namespace Mono.CSharp
                        if (global_site_container == null) {
                                global_site_container = new StaticDataClass ();
                                RootContext.ToplevelTypes.AddCompilerGeneratedClass (global_site_container);
+                               global_site_container.CreateType ();
                                global_site_container.DefineType ();
                                global_site_container.Define ();
-//                             global_site_container.EmitType ();
-
-                               RootContext.RegisterCompilerGeneratedType (global_site_container.TypeBuilder);
                        }
 
                        return global_site_container;
@@ -291,11 +285,8 @@ namespace Mono.CSharp
                        return null;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        if (DoResolveCore (ec))
                                binder_expr = binder.CreateCallSiteBinder (ec, arguments);
 
@@ -308,22 +299,22 @@ namespace Mono.CSharp
 
                        if (TypeManager.binder_type == null) {
                                var t = TypeManager.CoreLookupType (rc.Compiler,
-                                       "Microsoft.CSharp.RuntimeBinder", "Binder", Kind.Class, true);
+                                       "Microsoft.CSharp.RuntimeBinder", "Binder", MemberKind.Class, true);
                                if (t != null)
                                        TypeManager.binder_type = new TypeExpression (t, Location.Null);
                        }
 
                        if (TypeManager.call_site_type == null)
                                TypeManager.call_site_type = TypeManager.CoreLookupType (rc.Compiler,
-                                       "System.Runtime.CompilerServices", "CallSite", Kind.Class, true);
+                                       "System.Runtime.CompilerServices", "CallSite", MemberKind.Class, true);
 
                        if (TypeManager.generic_call_site_type == null)
                                TypeManager.generic_call_site_type = TypeManager.CoreLookupType (rc.Compiler,
-                                       "System.Runtime.CompilerServices", "CallSite`1", Kind.Class, true);
+                                       "System.Runtime.CompilerServices", "CallSite", 1, MemberKind.Class, true);
 
                        if (TypeManager.binder_flags == null) {
                                TypeManager.binder_flags = TypeManager.CoreLookupType (rc.Compiler,
-                                       "Microsoft.CSharp.RuntimeBinder", "CSharpBinderFlags", Kind.Enum, true);
+                                       "Microsoft.CSharp.RuntimeBinder", "CSharpBinderFlags", MemberKind.Enum, true);
                        }
 
                        eclass = ExprClass.Value;
@@ -353,9 +344,9 @@ namespace Mono.CSharp
                {
                        int dyn_args_count = arguments == null ? 0 : arguments.Count;
                        TypeExpr site_type = CreateSiteType (RootContext.ToplevelTypes.Compiler, arguments, dyn_args_count, isStatement);
-                       FieldExpr site_field_expr = new FieldExpr (CreateSiteField (site_type).FieldBuilder, loc);
+                       FieldExpr site_field_expr = new FieldExpr (CreateSiteField (site_type), loc);
 
-                       SymbolWriter.OpenCompilerGeneratedBlock (ec.ig);
+                       SymbolWriter.OpenCompilerGeneratedBlock (ec);
 
                        Arguments args = new Arguments (1);
                        args.Add (new Argument (binder));
@@ -363,7 +354,7 @@ namespace Mono.CSharp
                        
                        BlockContext bc = new BlockContext (ec.MemberContext, null, TypeManager.void_type);             
                        if (s.Resolve (bc)) {
-                               Statement init = new If (new Binary (Binary.Operator.Equality, site_field_expr, new NullLiteral (loc)), s, loc);
+                               Statement init = new If (new Binary (Binary.Operator.Equality, site_field_expr, new NullLiteral (loc), loc), s, loc);
                                init.Emit (ec);
                        }
 
@@ -385,7 +376,7 @@ namespace Mono.CSharp
                        if (target != null)
                                target.Emit (ec);
 
-                       SymbolWriter.CloseCompilerGeneratedBlock (ec.ig);
+                       SymbolWriter.CloseCompilerGeneratedBlock (ec);
                }
 
                public static MemberAccess GetBinderNamespace (Location loc)
@@ -407,12 +398,12 @@ namespace Mono.CSharp
                        FullNamedExpression[] targs = new FullNamedExpression[dyn_args_count + default_args];
                        targs [0] = new TypeExpression (TypeManager.call_site_type, loc);
                        for (int i = 0; i < dyn_args_count; ++i) {
-                               Type arg_type;
+                               TypeSpec arg_type;
                                Argument a = arguments [i];
                                if (a.Type == TypeManager.null_type)
                                        arg_type = TypeManager.object_type;
                                else
-                                       arg_type = TypeManager.TypeToReflectionType (a.Type);
+                                       arg_type = a.Type;
 
                                if (a.ArgType == Argument.AType.Out || a.ArgType == Argument.AType.Ref)
                                        has_ref_out_argument = true;
@@ -422,12 +413,12 @@ namespace Mono.CSharp
 
                        TypeExpr del_type = null;
                        if (!has_ref_out_argument) {
-                               string d_name = is_statement ? "Action`" : "Func`";
+                               string d_name = is_statement ? "Action" : "Func";
 
-                               Type t = TypeManager.CoreLookupType (ctx, "System", d_name + (dyn_args_count + default_args), Kind.Delegate, false);
+                               TypeSpec t = TypeManager.CoreLookupType (ctx, "System", d_name, dyn_args_count + default_args, MemberKind.Delegate, false);
                                if (t != null) {
                                        if (!is_statement)
-                                               targs [targs.Length - 1] = new TypeExpression (TypeManager.TypeToReflectionType (type), loc);
+                                               targs [targs.Length - 1] = new TypeExpression (type, loc);
 
                                        del_type = new GenericTypeExpr (t, new TypeArguments (targs), loc);
                                }
@@ -437,7 +428,7 @@ namespace Mono.CSharp
                        // Create custom delegate when no appropriate predefined one is found
                        //
                        if (del_type == null) {
-                               Type rt = is_statement ? TypeManager.void_type : type;
+                               TypeSpec rt = is_statement ? TypeManager.void_type : type;
                                Parameter[] p = new Parameter [dyn_args_count + 1];
                                p[0] = new Parameter (targs [0], "p0", Parameter.Modifier.NONE, null, loc);
 
@@ -445,16 +436,18 @@ namespace Mono.CSharp
                                        p[i] = new Parameter (targs[i], "p" + i.ToString ("X"), arguments[i - 1].Modifier, null, loc);
 
                                TypeContainer parent = CreateSiteContainer ();
-                               Delegate d = new Delegate (parent.NamespaceEntry, parent, new TypeExpression (TypeManager.TypeToReflectionType (rt), loc),
+                               Delegate d = new Delegate (parent.NamespaceEntry, parent, new TypeExpression (rt, loc),
                                        Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED,
                                        new MemberName ("Container" + container_counter++.ToString ("X")),
-                                       new ParametersCompiled (p), null);
+                                       new ParametersCompiled (ctx, p), null);
 
+                               d.CreateType ();
                                d.DefineType ();
                                d.Define ();
+                               d.Emit ();
 
                                parent.AddDelegate (d);
-                               del_type = new TypeExpression (d.TypeBuilder, loc);
+                               del_type = new TypeExpression (d.Definition, loc);
                        }
 
                        TypeExpr site_type = new GenericTypeExpr (TypeManager.generic_call_site_type, new TypeArguments (del_type), loc);
@@ -486,7 +479,7 @@ namespace Mono.CSharp
                        type = TypeManager.bool_type;
 
                        condition = new If (
-                               new Binary (Binary.Operator.Equality, this, new BoolLiteral (true, loc)),
+                               new Binary (Binary.Operator.Equality, this, new BoolLiteral (true, loc), loc),
                                new StatementExpression (invoke), new StatementExpression (assignment),
                                loc);
                }
@@ -510,7 +503,7 @@ namespace Mono.CSharp
 
        class DynamicConversion : DynamicExpressionStatement, IDynamicBinder
        {
-               public DynamicConversion (Type targetType, CSharpBinderFlags flags, Arguments args, Location loc)
+               public DynamicConversion (TypeSpec targetType, CSharpBinderFlags flags, Arguments args, Location loc)
                        : base (null, args, loc)
                {
                        type = targetType;
@@ -520,11 +513,12 @@ namespace Mono.CSharp
 
                public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
                {
-                       Arguments binder_args = new Arguments (2);
+                       Arguments binder_args = new Arguments (3);
 
                        flags |= ec.HasSet (ResolveContext.Options.CheckedScope) ? CSharpBinderFlags.CheckedContext : 0;
 
                        binder_args.Add (new Argument (new BinderFlags (flags, this)));
+                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));                                            
                        binder_args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
                        return new Invocation (GetBinder ("Convert", loc), binder_args);
                }
@@ -532,7 +526,7 @@ namespace Mono.CSharp
 
        class DynamicConstructorBinder : DynamicExpressionStatement, IDynamicBinder
        {
-               public DynamicConstructorBinder (Type type, Arguments args, Location loc)
+               public DynamicConstructorBinder (TypeSpec type, Arguments args, Location loc)
                        : base (null, args, loc)
                {
                        this.type = type;
@@ -581,7 +575,7 @@ namespace Mono.CSharp
                        this.member = member;
                }
 
-               public DynamicInvocation (ATypeNameExpression member, Arguments args, Type type, Location loc)
+               public DynamicInvocation (ATypeNameExpression member, Arguments args, TypeSpec type, Location loc)
                        : this (member, args, loc)
                {
                        // When a return type is known not to be dynamic
@@ -616,8 +610,8 @@ namespace Mono.CSharp
                        if (member != null && member.HasTypeArguments) {
                                TypeArguments ta = member.TypeArguments;
                                if (ta.Resolve (ec)) {
-                                       ArrayList targs = new ArrayList (ta.Count);
-                                       foreach (Type t in ta.Arguments)
+                                       var targs = new ArrayInitializer (ta.Count, loc);
+                                       foreach (TypeSpec t in ta.Arguments)
                                                targs.Add (new TypeOf (new TypeExpression (t, loc), loc));
 
                                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", targs, loc)));
@@ -631,7 +625,9 @@ namespace Mono.CSharp
                        Expression real_args;
                        if (args == null) {
                                // Cannot be null because .NET trips over
-                               real_args = new ArrayCreation (new MemberAccess (GetBinderNamespace (loc), "CSharpArgumentInfo", loc), "[]", new ArrayList (0), loc);
+                               real_args = new ArrayCreation (
+                                       new MemberAccess (GetBinderNamespace (loc), "CSharpArgumentInfo", loc), "[]",
+                                       new ArrayInitializer (0, loc), loc);
                        } else {
                                real_args = new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (ec), loc);
                        }
@@ -761,7 +757,7 @@ namespace Mono.CSharp
 
                public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
                {
-                       Arguments binder_args = new Arguments (3);
+                       Arguments binder_args = new Arguments (4);
 
                        MemberAccess sle = new MemberAccess (new MemberAccess (
                                new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Linq", loc), "Expressions", loc);
@@ -770,6 +766,7 @@ namespace Mono.CSharp
 
                        binder_args.Add (new Argument (new BinderFlags (flags, this)));
                        binder_args.Add (new Argument (new MemberAccess (new MemberAccess (sle, "ExpressionType", loc), name, loc)));
+                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (ec), loc)));
 
                        return new Invocation (GetBinder ("UnaryOperation", loc), binder_args);