Report less cascading errors
[mono.git] / mcs / mcs / dynamic.cs
index 8cf647fc330beae5eeddea5a4609b36013550359..0246c43a1b67fa9cba25972b19e283fd7558a5b2 100644 (file)
@@ -6,13 +6,14 @@
 // Dual licensed under the terms of the MIT X11 or GNU GPL
 //
 // Copyright 2009 Novell, Inc
+// Copyright 2011 Xamarin Inc.
 //
 
 using System;
 using System.Linq;
 using SLE = System.Linq.Expressions;
 
-#if NET_4_0
+#if NET_4_0 || MONODROID
 using System.Dynamic;
 #endif
 
@@ -47,10 +48,11 @@ namespace Mono.CSharp
                        this.loc = loc;
                }
 
-               protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
+               public override TypeSpec ResolveAsType (IMemberContext ec)
                {
+                       eclass = ExprClass.Type;
                        type = ec.Module.Compiler.BuiltinTypes.Dynamic;
-                       return this;
+                       return type;
                }
        }
 
@@ -61,7 +63,7 @@ namespace Mono.CSharp
        //
        public class RuntimeValueExpression : Expression, IDynamicAssign, IMemoryLocation
        {
-#if !NET_4_0
+#if !NET_4_0 && !MONODROID
                public class DynamicMetaObject
                {
                        public TypeSpec RuntimeType;
@@ -94,9 +96,14 @@ namespace Mono.CSharp
                        throw new NotImplementedException ();
                }
 
+               public override bool ContainsEmitWithAwait ()
+               {
+                       throw new NotSupportedException ();
+               }
+
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       throw new NotImplementedException ();
+                       throw new NotSupportedException ();
                }
 
                protected override Expression DoResolve (ResolveContext ec)
@@ -121,7 +128,7 @@ namespace Mono.CSharp
                        throw new NotImplementedException ();
                }
 
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
                {
                        throw new NotImplementedException ();
                }
@@ -139,7 +146,7 @@ namespace Mono.CSharp
                        return base.MakeExpression (ctx);
 #else
 
-       #if NET_4_0             
+#if NET_4_0 || MONODROID
                                if (type.IsStruct && !obj.Expression.Type.IsValueType)
                                        return SLE.Expression.Unbox (obj.Expression, type.GetMetaInfo ());
 
@@ -174,7 +181,7 @@ namespace Mono.CSharp
                        return this;
                }
 
-#if NET_4_0
+#if NET_4_0 || MONODROID
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
 #if STATIC
@@ -232,7 +239,7 @@ namespace Mono.CSharp
                        {
                                Child = new IntConstant (ec.BuiltinTypes, (int) (flags | statement.flags), statement.loc);
 
-                               type = ec.Module.PredefinedTypes.BinderFlags.Resolve (loc);
+                               type = ec.Module.PredefinedTypes.BinderFlags.Resolve ();
                                eclass = Child.eclass;
                                return this;
                        }
@@ -246,7 +253,7 @@ namespace Mono.CSharp
                protected CSharpBinderFlags flags;
 
                TypeSpec binder_type;
-               TypeParameter[] context_mvars;
+               TypeParameters context_mvars;
 
                public DynamicExpressionStatement (IDynamicBinder binder, Arguments args, Location loc)
                {
@@ -261,6 +268,11 @@ namespace Mono.CSharp
                        }
                }
 
+               public override bool ContainsEmitWithAwait ()
+               {
+                       return arguments.ContainsEmitWithAwait ();
+               }
+
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        ec.Report.Error (1963, loc, "An expression tree cannot contain a dynamic operation");
@@ -283,9 +295,9 @@ namespace Mono.CSharp
                        int errors = rc.Report.Errors;
                        var pt = rc.Module.PredefinedTypes;
 
-                       binder_type = pt.Binder.Resolve (loc);
-                       pt.CallSite.Resolve (loc);
-                       pt.CallSiteGeneric.Resolve (loc);
+                       binder_type = pt.Binder.Resolve ();
+                       pt.CallSite.Resolve ();
+                       pt.CallSiteGeneric.Resolve ();
 
                        eclass = ExprClass.Value;
 
@@ -337,7 +349,7 @@ namespace Mono.CSharp
                        var site_container = ec.CreateDynamicSite ();
 
                        if (context_mvars != null) {
-                               TypeParameter[] tparam;
+                               TypeParameters tparam;
                                TypeContainer sc = site_container;
                                do {
                                        tparam = sc.CurrentTypeParameters;
@@ -382,7 +394,7 @@ namespace Mono.CSharp
                                TypeExpr te = null;
                                Namespace type_ns = module.GlobalRootNamespace.GetNamespace ("System", true);
                                if (type_ns != null) {
-                                       te = type_ns.LookupType (module, d_name, dyn_args_count + default_args, true, Location.Null);
+                                       te = type_ns.LookupType (module, d_name, dyn_args_count + default_args, LookupMode.Normal, loc);
                                }
 
                                if (te != null) {
@@ -418,7 +430,7 @@ namespace Mono.CSharp
                                p[0] = new Parameter (targs[0], "p0", Parameter.Modifier.NONE, null, loc);
 
                                var site = ec.CreateDynamicSite ();
-                               int index = site.Types == null ? 0 : site.Types.Count;
+                               int index = site.Containers == null ? 0 : site.Containers.Count;
 
                                if (mutator != null)
                                        rt = mutator.Mutate (rt);
@@ -427,17 +439,17 @@ namespace Mono.CSharp
                                        p[i] = new Parameter (targs[i], "p" + i.ToString ("X"), arguments[i - 1].Modifier, null, loc);
                                }
 
-                               d = new Delegate (site.NamespaceEntry, site, new TypeExpression (rt, loc),
+                               d = new Delegate (site, new TypeExpression (rt, loc),
                                        Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED,
                                        new MemberName ("Container" + index.ToString ("X")),
                                        new ParametersCompiled (p), null);
 
-                               d.CreateType ();
-                               d.DefineType ();
+                               d.CreateContainer ();
+                               d.DefineContainer ();
                                d.Define ();
-                               d.Emit ();
+                               d.PrepareEmit ();
 
-                               site.AddDelegate (d);
+                               site.AddTypeContainer (d);
                                del_type = new TypeExpression (d.CurrentType, loc);
                                if (targs_for_instance != null) {
                                        del_type_instance_access = null;
@@ -454,17 +466,14 @@ namespace Mono.CSharp
                                return;
 
                        if (del_type_instance_access == null) {
-                               var dt = d.CurrentType.DeclaringType.MakeGenericType (module, context_mvars.Select (l => l.Type).ToArray ());
+                               var dt = d.CurrentType.DeclaringType.MakeGenericType (module, context_mvars.Types);
                                del_type_instance_access = new TypeExpression (MemberCache.GetMember (dt, d.CurrentType), loc);
                        }
 
-                       FullNamedExpression instanceAccessExprType = new GenericTypeExpr (module.PredefinedTypes.CallSiteGeneric.TypeSpec,
+                       var instanceAccessExprType = new GenericTypeExpr (module.PredefinedTypes.CallSiteGeneric.TypeSpec,
                                new TypeArguments (del_type_instance_access), loc);
 
-                       BlockContext bc = new BlockContext (ec.MemberContext, null, ec.BuiltinTypes.Void);
-
-                       instanceAccessExprType = instanceAccessExprType.ResolveAsTypeStep (bc, false);
-                       if (instanceAccessExprType == null)
+                       if (instanceAccessExprType.ResolveAsType (ec.MemberContext) == null)
                                return;
 
                        bool inflate_using_mvar = context_mvars != null && ec.IsAnonymousStoreyMutateRequired;
@@ -473,12 +482,12 @@ namespace Mono.CSharp
                        if (inflate_using_mvar || context_mvars == null) {
                                gt = site_container.CurrentType;
                        } else {
-                               gt = site_container.CurrentType.MakeGenericType (module, context_mvars.Select (l => l.Type).ToArray ());
+                               gt = site_container.CurrentType.MakeGenericType (module, context_mvars.Types);
                        }
 
                        // When site container already exists the inflated version has to be
                        // updated manually to contain newly created field
-                       if (gt is InflatedTypeSpec && site_container.Fields.Count > 1) {
+                       if (gt is InflatedTypeSpec && site_container.AnonymousMethodsCounter > 1) {
                                var tparams = gt.MemberDefinition.TypeParametersCount > 0 ? gt.MemberDefinition.TypeParameters : TypeParameterSpec.EmptyTypes;
                                var inflator = new TypeParameterInflator (module, gt, tparams, gt.TypeArguments);
                                gt.MemberCache.AddMember (field.InflateMember (inflator));
@@ -486,41 +495,41 @@ namespace Mono.CSharp
 
                        FieldExpr site_field_expr = new FieldExpr (MemberCache.GetMember (gt, field), loc);
 
-                       SymbolWriter.OpenCompilerGeneratedBlock (ec);
+                       BlockContext bc = new BlockContext (ec.MemberContext, null, ec.BuiltinTypes.Void);
 
                        Arguments args = new Arguments (1);
                        args.Add (new Argument (binder));
                        StatementExpression s = new StatementExpression (new SimpleAssign (site_field_expr, new Invocation (new MemberAccess (instanceAccessExprType, "Create"), args)));
-                       
-                       if (s.Resolve (bc)) {
-                               Statement init = new If (new Binary (Binary.Operator.Equality, site_field_expr, new NullLiteral (loc), loc), s, loc);
-                               init.Emit (ec);
-                       }
-
-                       args = new Arguments (1 + dyn_args_count);
-                       args.Add (new Argument (site_field_expr));
-                       if (arguments != null) {
-                               int arg_pos = 1;
-                               foreach (Argument a in arguments) {
-                                       if (a is NamedArgument) {
-                                               // Name is not valid in this context
-                                               args.Add (new Argument (a.Expr, a.ArgType));
-                                       } else {
-                                               args.Add (a);
-                                       }
-
-                                       if (inflate_using_mvar && a.Type != targs[arg_pos].Type)
-                                               a.Expr.Type = targs[arg_pos].Type;
 
-                                       ++arg_pos;
+                       using (ec.With (BuilderContext.Options.OmitDebugInfo, true)) {
+                               if (s.Resolve (bc)) {
+                                       Statement init = new If (new Binary (Binary.Operator.Equality, site_field_expr, new NullLiteral (loc)), s, loc);
+                                       init.Emit (ec);
                                }
-                       }
 
-                       Expression target = new DelegateInvocation (new MemberAccess (site_field_expr, "Target", loc).Resolve (bc), args, loc).Resolve (bc);
-                       if (target != null)
-                               target.Emit (ec);
+                               args = new Arguments (1 + dyn_args_count);
+                               args.Add (new Argument (site_field_expr));
+                               if (arguments != null) {
+                                       int arg_pos = 1;
+                                       foreach (Argument a in arguments) {
+                                               if (a is NamedArgument) {
+                                                       // Name is not valid in this context
+                                                       args.Add (new Argument (a.Expr, a.ArgType));
+                                               } else {
+                                                       args.Add (a);
+                                               }
+
+                                               if (inflate_using_mvar && a.Type != targs[arg_pos].Type)
+                                                       a.Expr.Type = targs[arg_pos].Type;
+
+                                               ++arg_pos;
+                                       }
+                               }
 
-                       SymbolWriter.CloseCompilerGeneratedBlock (ec);
+                               Expression target = new DelegateInvocation (new MemberAccess (site_field_expr, "Target", loc).Resolve (bc), args, loc).Resolve (bc);
+                               if (target != null)
+                                       target.Emit (ec);
+                       }
                }
 
                public static MemberAccess GetBinderNamespace (Location loc)
@@ -559,7 +568,7 @@ namespace Mono.CSharp
 
                                binder_args.Add (new Argument (new BinderFlags (0, this)));
                                binder_args.Add (new Argument (new StringLiteral (ec.BuiltinTypes, name, loc)));
-                               binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                               binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
 
                                return new Invocation (GetBinder ("IsEvent", loc), binder_args);
                        }
@@ -599,7 +608,9 @@ namespace Mono.CSharp
                public override void EmitStatement (EmitContext ec)
                {
                        var stmt = new If (condition, new StatementExpression (invoke), new StatementExpression (assign), loc);
-                       stmt.Emit (ec);
+                       using (ec.With (BuilderContext.Options.OmitDebugInfo, true)) {
+                               stmt.Emit (ec);
+                       }
                }
        }
 
@@ -620,8 +631,8 @@ namespace Mono.CSharp
                        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 (type, loc), loc)));
-                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                       binder_args.Add (new Argument (new TypeOf (type, loc)));
+                       binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
                        return new Invocation (GetBinder ("Convert", loc), binder_args);
                }
        }
@@ -640,7 +651,7 @@ namespace Mono.CSharp
                        Arguments binder_args = new Arguments (3);
 
                        binder_args.Add (new Argument (new BinderFlags (0, this)));
-                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                       binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
 
                        return new Invocation (GetBinder ("InvokeConstructor", loc), binder_args);
@@ -673,7 +684,7 @@ namespace Mono.CSharp
                        Arguments binder_args = new Arguments (3);
 
                        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 (ec.CurrentType, loc)));
                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
 
                        isSet |= (flags & CSharpBinderFlags.ValueFromCompoundAssignment) != 0;
@@ -715,7 +726,7 @@ namespace Mono.CSharp
 
        class DynamicInvocation : DynamicExpressionStatement, IDynamicBinder
        {
-               ATypeNameExpression member;
+               readonly ATypeNameExpression member;
 
                public DynamicInvocation (ATypeNameExpression member, Arguments args, Location loc)
                        : base (null, args, loc)
@@ -724,15 +735,6 @@ namespace Mono.CSharp
                        this.member = member;
                }
 
-               //
-               // When a return type is known not to be dynamic
-               //
-               public DynamicInvocation (ATypeNameExpression member, Arguments args, TypeSpec type, Location loc)
-                       : this (member, args, loc)
-               {
-                       this.type = type;
-               }
-
                public static DynamicInvocation CreateSpecialNameInvoke (ATypeNameExpression member, Arguments args, Location loc)
                {
                        return new DynamicInvocation (member, args, loc) {
@@ -763,7 +765,7 @@ namespace Mono.CSharp
                                if (ta.Resolve (ec)) {
                                        var targs = new ArrayInitializer (ta.Count, loc);
                                        foreach (TypeSpec t in ta.Arguments)
-                                               targs.Add (new TypeOf (new TypeExpression (t, loc), loc));
+                                               targs.Add (new TypeOf (t, loc));
 
                                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (targs, loc)));
                                }
@@ -771,7 +773,7 @@ namespace Mono.CSharp
                                binder_args.Add (new Argument (new NullLiteral (loc)));
                        }
 
-                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                       binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
 
                        Expression real_args;
                        if (args == null) {
@@ -817,7 +819,7 @@ namespace Mono.CSharp
 
                        binder_args.Add (new Argument (new BinderFlags (flags, this)));
                        binder_args.Add (new Argument (new StringLiteral (ec.BuiltinTypes, name, loc)));
-                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                       binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
 
                        isSet |= (flags & CSharpBinderFlags.ValueFromCompoundAssignment) != 0;
@@ -898,7 +900,7 @@ namespace Mono.CSharp
                        throw new NotImplementedException ();
                }
 
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
                {
                        EmitCall (ec, setter, setter_args, !leave_copy);
                }
@@ -938,7 +940,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 TypeOf (ec.CurrentType, loc)));
                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
 
                        return new Invocation (GetBinder ("UnaryOperation", loc), binder_args);
@@ -947,15 +949,15 @@ namespace Mono.CSharp
 
        sealed class DynamicSiteClass : HoistedStoreyClass
        {
-               public DynamicSiteClass (TypeContainer parent, MemberBase host, TypeParameter[] tparams)
-                       : base (parent, MakeMemberName (host, "DynamicSite", parent.DynamicSitesCounter, tparams, Location.Null), tparams, Modifiers.STATIC)
+               public DynamicSiteClass (TypeDefinition parent, MemberBase host, TypeParameters tparams)
+                       : base (parent, MakeMemberName (host, "DynamicSite", parent.DynamicSitesCounter, tparams, Location.Null), tparams, Modifiers.STATIC, MemberKind.Class)
                {
                        parent.DynamicSitesCounter++;
                }
 
                public FieldSpec CreateCallSiteField (FullNamedExpression type, Location loc)
                {
-                       int index = fields == null ? 0 : fields.Count;
+                       int index = AnonymousMethodsCounter++;
                        Field f = new HoistedField (this, type, Modifiers.PUBLIC | Modifiers.STATIC, "Site" + index.ToString ("X"), null, loc);
                        f.Define ();