Beispiel #1
0
        protected override Expression CreateExpressionTree(ResolveContext ec, TypeSpec delegate_type)
        {
            if (ec.IsInProbingMode)
            {
                return(this);
            }

            BlockContext bc = new BlockContext(ec.MemberContext, ec.ConstructorBlock, ec.BuiltinTypes.Void)
            {
                CurrentAnonymousMethod = ec.CurrentAnonymousMethod
            };

            Expression args = Parameters.CreateExpressionTree(bc, loc);
            Expression expr = Block.CreateExpressionTree(ec);

            if (expr == null)
            {
                return(null);
            }

            Arguments arguments = new Arguments(2);

            arguments.Add(new Argument(expr));
            arguments.Add(new Argument(args));
            return(CreateExpressionFactoryCall(ec, "Lambda",
                                               new TypeArguments(new TypeExpression(delegate_type, loc)),
                                               arguments));
        }
        public override bool Resolve(BlockContext bc)
        {
            if (!base.Resolve(bc))
            {
                return(false);
            }

            expr = expr.Resolve(bc);

            return(expr != null);
        }
                public override bool Resolve(BlockContext ec)
                {
                    TypeExpression    storey_type_expr = new TypeExpression(host.Definition, loc);
                    List <Expression> init             = null;

                    if (host.hoisted_this != null)
                    {
                        init = new List <Expression> (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
                        HoistedThis ht   = host.hoisted_this;
                        FieldExpr   from = new FieldExpr(ht.Field, loc);
                        from.InstanceExpression = new CompilerGeneratedThis(ec.CurrentType, loc);
                        init.Add(new ElementInitializer(ht.Field.Name, from, loc));
                    }

                    if (host.hoisted_params != null)
                    {
                        if (init == null)
                        {
                            init = new List <Expression> (host.HoistedParameters.Count);
                        }

                        for (int i = 0; i < host.hoisted_params.Count; ++i)
                        {
                            HoistedParameter hp    = host.hoisted_params [i];
                            HoistedParameter hp_cp = host.hoisted_params_copy [i];

                            FieldExpr from = new FieldExpr(hp_cp.Field, loc);
                            from.InstanceExpression = new CompilerGeneratedThis(ec.CurrentType, loc);

                            init.Add(new ElementInitializer(hp.Field.Name, from, loc));
                        }
                    }

                    if (init != null)
                    {
                        new_storey = new NewInitialize(storey_type_expr, null,
                                                       new CollectionOrObjectInitializers(init, loc), loc);
                    }
                    else
                    {
                        new_storey = new New(storey_type_expr, null, loc);
                    }

                    new_storey = new_storey.Resolve(ec);
                    if (new_storey != null)
                    {
                        new_storey = Convert.ImplicitConversionRequired(ec, new_storey, host_method.MemberType, loc);
                    }

                    ec.CurrentBranching.CurrentUsageVector.Goto();
                    return(true);
                }
Beispiel #4
0
        public string [] GetCompletions(string input, out string prefix)
        {
            prefix = "";
            if (input == null || input.Length == 0)
            {
                return(null);
            }

            lock (evaluator_lock){
                if (!inited)
                {
                    Init();
                }

                bool         partial_input;
                CSharpParser parser = ParseString(ParseMode.GetCompletions, input, out partial_input);
                if (parser == null)
                {
                    return(null);
                }

                Class parser_result = parser.InteractiveResult;

#if NET_4_0
                var access = AssemblyBuilderAccess.RunAndCollect;
#else
                var access = AssemblyBuilderAccess.Run;
#endif
                var a = new AssemblyDefinitionDynamic(module, "completions");
                a.Create(AppDomain.CurrentDomain, access);
                module.SetDeclaringAssembly(a);

                // Need to setup MemberCache
                parser_result.CreateContainer();

                var          method = parser_result.Members[0] as Method;
                BlockContext bc     = new BlockContext(method, method.Block, ctx.BuiltinTypes.Void);

                try {
                    method.Block.Resolve(null, bc, method);
                } catch (CompletionResult cr) {
                    prefix = cr.BaseText;
                    return(cr.Result);
                }
            }
            return(null);
        }
        public override bool Resolve(BlockContext bc)
        {
            expr = expr.Resolve(bc);
            if (expr == null)
            {
                return(false);
            }

            machine_initializer = bc.CurrentAnonymousMethod as T;

            if (!bc.CurrentBranching.CurrentUsageVector.IsUnreachable)
            {
                unwind_protect = bc.CurrentBranching.AddResumePoint(this, this, out resume_pc);
            }

            return(true);
        }
Beispiel #6
0
        public override Expression CreateExpressionTree(ResolveContext ec)
        {
            BlockContext bc   = new BlockContext(ec.MemberContext, Block, ReturnType);
            Expression   args = parameters.CreateExpressionTree(bc, loc);
            Expression   expr = Block.CreateExpressionTree(ec);

            if (expr == null)
            {
                return(null);
            }

            Arguments arguments = new Arguments(2);

            arguments.Add(new Argument(expr));
            arguments.Add(new Argument(args));
            return(CreateExpressionFactoryCall(ec, "Lambda",
                                               new TypeArguments(new TypeExpression(type, loc)),
                                               arguments));
        }
Beispiel #7
0
        protected override bool DoResolve(BlockContext ec)
        {
            //
            // When delegate returns void, only expression statements can be used
            //
            if (ec.ReturnType.Kind == MemberKind.Void)
            {
                Expr = Expr.Resolve(ec);
                if (Expr == null)
                {
                    return(false);
                }

                statement = Expr as ExpressionStatement;
                if (statement == null)
                {
                    Expr.Error_InvalidExpressionStatement(ec);
                }

                return(true);
            }

            return(base.DoResolve(ec));
        }
        protected void EmitCall(EmitContext ec, Expression binder, Arguments arguments, bool isStatement)
        {
            //
            // This method generates all internal infrastructure for a dynamic call. The
            // reason why it's quite complicated is the mixture of dynamic and anonymous
            // methods. Dynamic itself requires a temporary class (ContainerX) and anonymous
            // methods can generate temporary storey as well (AnonStorey). Handling MVAR
            // type parameters rewrite is non-trivial in such case as there are various
            // combinations possible therefore the mutator is not straightforward. Secondly
            // we need to keep both MVAR(possibly VAR for anon storey) and type VAR to emit
            // correct Site field type and its access from EmitContext.
            //

            int dyn_args_count = arguments == null ? 0 : arguments.Count;
            int default_args   = isStatement ? 1 : 2;
            var module         = ec.Module;

            bool has_ref_out_argument = false;
            var  targs = new TypeExpression[dyn_args_count + default_args];

            targs[0] = new TypeExpression(module.PredefinedTypes.CallSite.TypeSpec, loc);

            TypeExpression[]     targs_for_instance = null;
            TypeParameterMutator mutator;

            var site_container = ec.CreateDynamicSite();

            if (context_mvars != null)
            {
                TypeParameters tparam;
                TypeContainer  sc = site_container;
                do
                {
                    tparam = sc.CurrentTypeParameters;
                    sc     = sc.Parent;
                } while (tparam == null);

                mutator = new TypeParameterMutator(context_mvars, tparam);

                if (!ec.IsAnonymousStoreyMutateRequired)
                {
                    targs_for_instance    = new TypeExpression[targs.Length];
                    targs_for_instance[0] = targs[0];
                }
            }
            else
            {
                mutator = null;
            }

            for (int i = 0; i < dyn_args_count; ++i)
            {
                Argument a = arguments[i];
                if (a.ArgType == Argument.AType.Out || a.ArgType == Argument.AType.Ref)
                {
                    has_ref_out_argument = true;
                }

                var t = a.Type;

                // Convert any internal type like dynamic or null to object
                if (t.Kind == MemberKind.InternalCompilerType)
                {
                    t = ec.BuiltinTypes.Object;
                }

                if (targs_for_instance != null)
                {
                    targs_for_instance[i + 1] = new TypeExpression(t, loc);
                }

                if (mutator != null)
                {
                    t = t.Mutate(mutator);
                }

                targs[i + 1] = new TypeExpression(t, loc);
            }

            TypeExpr del_type = null;
            TypeExpr del_type_instance_access = null;

            if (!has_ref_out_argument)
            {
                string d_name = isStatement ? "Action" : "Func";

                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, LookupMode.Normal, loc);
                }

                if (te != null)
                {
                    if (!isStatement)
                    {
                        var t = type;
                        if (t.Kind == MemberKind.InternalCompilerType)
                        {
                            t = ec.BuiltinTypes.Object;
                        }

                        if (targs_for_instance != null)
                        {
                            targs_for_instance[targs_for_instance.Length - 1] = new TypeExpression(t, loc);
                        }

                        if (mutator != null)
                        {
                            t = t.Mutate(mutator);
                        }

                        targs[targs.Length - 1] = new TypeExpression(t, loc);
                    }

                    del_type = new GenericTypeExpr(te.Type, new TypeArguments(targs), loc);
                    if (targs_for_instance != null)
                    {
                        del_type_instance_access = new GenericTypeExpr(te.Type, new TypeArguments(targs_for_instance), loc);
                    }
                    else
                    {
                        del_type_instance_access = del_type;
                    }
                }
            }

            //
            // Create custom delegate when no appropriate predefined delegate has been found
            //
            Delegate d;

            if (del_type == null)
            {
                TypeSpec    rt = isStatement ? ec.BuiltinTypes.Void : type;
                Parameter[] p  = new Parameter[dyn_args_count + 1];
                p[0] = new Parameter(targs[0], "p0", Parameter.Modifier.NONE, null, loc);

                var site  = ec.CreateDynamicSite();
                int index = site.Containers == null ? 0 : site.Containers.Count;

                if (mutator != null)
                {
                    rt = mutator.Mutate(rt);
                }

                for (int i = 1; i < dyn_args_count + 1; ++i)
                {
                    p[i] = new Parameter(targs[i], "p" + i.ToString("X"), arguments[i - 1].Modifier, null, 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.CreateContainer();
                d.DefineContainer();
                d.Define();

                site.AddTypeContainer(d);
                del_type = new TypeExpression(d.CurrentType, loc);
                if (targs_for_instance != null)
                {
                    del_type_instance_access = null;
                }
                else
                {
                    del_type_instance_access = del_type;
                }
            }
            else
            {
                d = null;
            }

            var site_type_decl = new GenericTypeExpr(module.PredefinedTypes.CallSiteGeneric.TypeSpec, new TypeArguments(del_type), loc);
            var field          = site_container.CreateCallSiteField(site_type_decl, loc);

            if (field == null)
            {
                return;
            }

            if (del_type_instance_access == null)
            {
                var dt = d.CurrentType.DeclaringType.MakeGenericType(module, context_mvars.Types);
                del_type_instance_access = new TypeExpression(MemberCache.GetMember(dt, d.CurrentType), loc);
            }

            var instanceAccessExprType = new GenericTypeExpr(module.PredefinedTypes.CallSiteGeneric.TypeSpec,
                                                             new TypeArguments(del_type_instance_access), loc);

            if (instanceAccessExprType.ResolveAsType(ec.MemberContext) == null)
            {
                return;
            }

            bool inflate_using_mvar = context_mvars != null && ec.IsAnonymousStoreyMutateRequired;

            TypeSpec gt;

            if (inflate_using_mvar || context_mvars == null)
            {
                gt = site_container.CurrentType;
            }
            else
            {
                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.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));
            }

            FieldExpr site_field_expr = new FieldExpr(MemberCache.GetMember(gt, field), loc);

            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)));

            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);
                }

                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;
                    }
                }

                Expression target = new DelegateInvocation(new MemberAccess(site_field_expr, "Target", loc).Resolve(bc), args, loc).Resolve(bc);
                if (target != null)
                {
                    target.Emit(ec);
                }
            }
        }
 public override bool Resolve(BlockContext ec)
 {
     return(true);
 }
 protected override bool DoResolve(BlockContext ec)
 {
     iterator = ec.CurrentIterator;
     return(Yield.CheckContext(ec, loc));
 }
Beispiel #11
0
        public override bool Resolve(BlockContext bc)
        {
            if (bc.CurrentBlock is Linq.QueryBlock)
            {
                bc.Report.Error(1995, loc,
                                "The `await' operator may only be used in a query expression within the first collection expression of the initial `from' clause or within the collection expression of a `join' clause");
                return(false);
            }

            if (!base.Resolve(bc))
            {
                return(false);
            }

            type = expr.Type;
            Arguments args = new Arguments(0);

            //
            // The await expression is of dynamic type
            //
            if (type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
            {
                result_type = type;
                expr        = new Invocation(new MemberAccess(expr, "GetAwaiter"), args).Resolve(bc);
                return(true);
            }

            //
            // Check whether the expression is awaitable
            //
            Expression ama = new AwaitableMemberAccess(expr).Resolve(bc);

            if (ama == null)
            {
                return(false);
            }

            var errors_printer = new SessionReportPrinter();
            var old            = bc.Report.SetPrinter(errors_printer);

            ama = new Invocation(ama, args).Resolve(bc);
            bc.Report.SetPrinter(old);

            if (errors_printer.ErrorsCount > 0 || !MemberAccess.IsValidDotExpression(ama.Type))
            {
                bc.Report.Error(1986, expr.Location,
                                "The `await' operand type `{0}' must have suitable GetAwaiter method",
                                expr.Type.GetSignatureForError());

                return(false);
            }

            var awaiter_type = ama.Type;

            awaiter_definition = bc.Module.GetAwaiter(awaiter_type);

            if (!awaiter_definition.IsValidPattern)
            {
                Error_WrongAwaiterPattern(bc, awaiter_type);
                return(false);
            }

            if (!awaiter_definition.INotifyCompletion)
            {
                bc.Report.Error(4027, loc, "The awaiter type `{0}' must implement interface `{1}'",
                                awaiter_type.GetSignatureForError(), bc.Module.PredefinedTypes.INotifyCompletion.GetSignatureForError());
                return(false);
            }

            expr        = ama;
            result_type = awaiter_definition.GetResult.ReturnType;

            return(true);
        }
 public override bool ResolveUnreachable(BlockContext ec, bool warn)
 {
     return(true);
 }