Example #1
0
        public override BoundNode VisitObjectCreationExpression(BoundObjectCreationExpression node)
        {
            var rewritten = (BoundObjectCreationExpression)base.VisitObjectCreationExpression(node);

            if (
                !TypeSymbol.Equals(rewritten.Type, node.Type, TypeCompareKind.ConsiderEverything2) &&
                (object)node.Constructor != null
                )
            {
                MethodSymbol ctor = VisitMethodSymbol(node.Constructor);
                rewritten = rewritten.Update(
                    ctor,
                    rewritten.Arguments,
                    rewritten.ArgumentNamesOpt,
                    rewritten.ArgumentRefKindsOpt,
                    rewritten.Expanded,
                    rewritten.ArgsToParamsOpt,
                    rewritten.DefaultArguments,
                    rewritten.ConstantValueOpt,
                    rewritten.InitializerExpressionOpt,
                    rewritten.Type
                    );
            }

            return(rewritten);
        }
Example #2
0
        public override BoundNode VisitObjectCreationExpression(BoundObjectCreationExpression node)
        {
            Debug.Assert(node != null);

            // Start by rewriting the arguments:
            var rewrittenArguments = VisitList(node.Arguments);

            // If the mapping from arguments to parameters is perfectly in order, no complex rewriting is needed.
            if (node.ArgsToParamsOpt.IsNull && !node.Expanded)
            {
                return(node.Update(node.ConstructorOpt, rewrittenArguments, node.ArgumentNamesOpt, node.ArgumentRefKindsOpt, node.Expanded, node.ArgsToParamsOpt, node.ConstantValueOpt, node.Type));
            }

            var argumentRefKinds = node.ArgumentRefKindsOpt;
            ReadOnlyArray <LocalSymbol> temps;

            Debug.Assert(node.ConstructorOpt != null, "rewriting arguments when there is no constructor");

            RewriteArguments(node.ConstructorOpt,
                             node.Expanded,
                             node.ArgsToParamsOpt,
                             ref argumentRefKinds,
                             ref rewrittenArguments,
                             out temps);

            if (temps.IsNullOrEmpty)
            {
                return(node.Update(
                           node.ConstructorOpt,
                           rewrittenArguments,
                           ReadOnlyArray <string> .Null,
                           argumentRefKinds,
                           false,
                           ReadOnlyArray <int> .Null,
                           node.ConstantValueOpt,
                           node.Type));
            }
            else
            {
                return(new BoundSequence(
                           null,
                           null,
                           temps,
                           ReadOnlyArray <BoundExpression> .Empty,
                           new BoundObjectCreationExpression(
                               node.Syntax,
                               node.SyntaxTree,
                               node.ConstructorOpt,
                               rewrittenArguments,
                               ReadOnlyArray <string> .Null,
                               argumentRefKinds,
                               false,
                               ReadOnlyArray <int> .Null,
                               node.ConstantValueOpt,
                               node.Type),
                           node.Type
                           ));
            }
        }
Example #3
0
 public override object VisitObjectCreationExpression(BoundObjectCreationExpression node, object arg)
 {
     foreach (var e in node.Arguments)
     {
         VisitExpression(e);
     }
     return(null);
 }
Example #4
0
        internal void Parse(BoundObjectCreationExpression boundObjectCreationExpression)
        {
            base.Parse(boundObjectCreationExpression);
            Method = boundObjectCreationExpression.Constructor;
            if (boundObjectCreationExpression.InitializerExpressionOpt != null)
            {
                this.InitializerExpressionOpt = Deserialize(boundObjectCreationExpression.InitializerExpressionOpt) as Expression;
            }

            foreach (var expression in boundObjectCreationExpression.Arguments)
            {
                var argument = Deserialize(expression) as Expression;
                Debug.Assert(argument != null);
                Arguments.Add(argument);
            }
        }
Example #5
0
        public override BoundNode VisitObjectCreationExpression(BoundObjectCreationExpression node)
        {
            var rewritten = (BoundObjectCreationExpression)base.VisitObjectCreationExpression(node);
            if (rewritten.Type != node.Type && (object)node.Constructor != null)
            {
                MethodSymbol ctor = VisitMethodSymbol(node.Constructor);
                rewritten = rewritten.Update(
                    ctor,
                    rewritten.Arguments,
                    rewritten.ArgumentNamesOpt,
                    rewritten.ArgumentRefKindsOpt,
                    rewritten.Expanded,
                    rewritten.ArgsToParamsOpt,
                    rewritten.ConstantValueOpt,
                    rewritten.InitializerExpressionOpt,
                    rewritten.BinderOpt,
                    rewritten.Type);
            }

            return rewritten;
        }
Example #6
0
        private void EmitObjectCreationExpression(BoundObjectCreationExpression expression, bool used)
        {
            MethodSymbol constructor = expression.Constructor;
            if (constructor.IsDefaultValueTypeConstructor())
            {
                EmitInitObj(expression.Type, used, expression.Syntax);
            }
            else
            {
                if (!used &&
                    expression.Constructor.OriginalDefinition == _module.Compilation.GetSpecialTypeMember(SpecialMember.System_Nullable_T__ctor))
                {
                    // creating nullable has no side-effects, so we will just evaluate the arg
                    EmitExpression(expression.Arguments[0], used: false);
                }
                else
                {
                    EmitArguments(expression.Arguments, constructor.Parameters);

                    var stackAdjustment = GetObjCreationStackBehavior(expression);
                    _builder.EmitOpCode(ILOpCode.Newobj, stackAdjustment);

                    // for variadic ctors emit expanded ctor token
                    EmitSymbolToken(constructor, expression.Syntax,
                                    constructor.IsVararg ? (BoundArgListOperator)expression.Arguments[expression.Arguments.Length - 1] : null);

                    EmitPopIfUnused(used);
                }
            }
        }
Example #7
0
        private static int GetObjCreationStackBehavior(BoundObjectCreationExpression objCreation)
        {
            int stack = 0;

            // Constructor puts the return value on the stack.
            stack += 1;

            if (objCreation.Constructor.IsVararg)
            {
                // Constructor pops all the arguments, fixed and variadic.
                int fixedArgCount = objCreation.Arguments.Length - 1;
                int varArgCount = ((BoundArgListOperator)objCreation.Arguments[fixedArgCount]).Arguments.Length;
                stack -= fixedArgCount;
                stack -= varArgCount;
            }
            else
            {
                // Constructor pops all the arguments.
                stack -= objCreation.Arguments.Length;
            }

            return stack;
        }
Example #8
0
        public override BoundNode VisitObjectCreationExpression(BoundObjectCreationExpression node)
        {
            var constructor = node.Constructor;
            var rewrittenArguments = VisitArguments(node.Arguments, constructor.Parameters);
            Debug.Assert(node.InitializerExpressionOpt == null);

            return node.Update(constructor, rewrittenArguments, node.ArgumentNamesOpt, node.ArgumentRefKindsOpt,
                node.Expanded, node.ArgsToParamsOpt, node.ConstantValue, null, node.Type);
        }
Example #9
0
 public override BoundNode VisitObjectCreationExpression(BoundObjectCreationExpression node)
 {
     ImmutableArray<BoundExpression> arguments = this.VisitList(node.Arguments);
     Debug.Assert(node.InitializerExpressionOpt == null);
     TypeSymbol type = this.VisitType(node.Type);
     return node.Update(node.Constructor, arguments, node.ArgumentNamesOpt, node.ArgumentRefKindsOpt, node.Expanded, node.ArgsToParamsOpt, node.ConstantValueOpt, null, type);
 }
        public override BoundNode VisitObjectCreationExpression(BoundObjectCreationExpression node)
        {
            var rewritten = (BoundObjectCreationExpression)base.VisitObjectCreationExpression(node);
            if (rewritten.Type != node.Type && (object)node.Constructor != null)
            {
                MethodSymbol ctor = VisitMethodSymbol(node.Constructor);
                rewritten = rewritten.Update(
                    ctor,
                    rewritten.Arguments,
                    rewritten.ArgumentNamesOpt,
                    rewritten.ArgumentRefKindsOpt,
                    rewritten.Expanded,
                    rewritten.ArgsToParamsOpt,
                    rewritten.ConstantValueOpt,
                    rewritten.InitializerExpressionOpt,
                    rewritten.Type);
            }

            return rewritten;
        }
Example #11
0
        private void EmitObjectCreationExpression(BoundObjectCreationExpression expression, bool used)
        {
            MethodSymbol constructor = expression.Constructor;
            if (constructor.IsDefaultValueTypeConstructor())
            {
                EmitInitObj(expression.Type, used, expression.Syntax);
            }
            else
            {
                if (!used && ConstructorNotSideEffecting(constructor))
                {
                    // creating nullable has no side-effects, so we will just evaluate the arguments
                    foreach (var arg in expression.Arguments)
                    {
                        EmitExpression(arg, used: false);
                    }
                }
                else
                {
                    EmitArguments(expression.Arguments, constructor.Parameters);

                    var stackAdjustment = GetObjCreationStackBehavior(expression);
                    _builder.EmitOpCode(ILOpCode.Newobj, stackAdjustment);

                    // for variadic ctors emit expanded ctor token
                    EmitSymbolToken(constructor, expression.Syntax,
                                    constructor.IsVararg ? (BoundArgListOperator)expression.Arguments[expression.Arguments.Length - 1] : null);

                    EmitPopIfUnused(used);
                }
            }
        }
Example #12
0
        private void EmitObjectCreationExpression(BoundObjectCreationExpression expression, bool used)
        {
            MethodSymbol constructor = expression.Constructor;
            if (constructor.IsParameterlessValueTypeConstructor(requireSynthesized: true))
            {
                EmitInitObj(expression.Type, used, expression.Syntax);
            }
            else
            {
                EmitArguments(expression.Arguments, constructor.Parameters);

                var stackAdjustment = GetObjCreationStackBehavior(expression);
                builder.EmitOpCode(ILOpCode.Newobj, stackAdjustment);

                // for variadic ctors emit expanded ctor token
                EmitSymbolToken(constructor, expression.Syntax,
                                constructor.IsVararg ? (BoundArgListOperator)expression.Arguments[expression.Arguments.Length - 1] : null);

                EmitPopIfUnused(used);
            }
        }
        public override BoundNode VisitObjectCreationExpression(BoundObjectCreationExpression node)
        {
            Debug.Assert(node != null);

            // Start by rewriting the arguments:
            var rewrittenArguments = VisitList(node.Arguments);

            // If the mapping from arguments to parameters is perfectly in order, no complex rewriting is needed.
            if (node.ArgsToParamsOpt.IsNull && !node.Expanded)
            {
                return node.Update(node.ConstructorOpt, rewrittenArguments, node.ArgumentNamesOpt, node.ArgumentRefKindsOpt, node.Expanded, node.ArgsToParamsOpt, node.ConstantValueOpt, node.Type);
            }

            var argumentRefKinds = node.ArgumentRefKindsOpt;
            ReadOnlyArray<LocalSymbol> temps;

            Debug.Assert(node.ConstructorOpt != null, "rewriting arguments when there is no constructor");

            RewriteArguments(node.ConstructorOpt,
                        node.Expanded,
                        node.ArgsToParamsOpt,
                        ref argumentRefKinds,
                        ref rewrittenArguments,
                        out temps);

            if (temps.IsNullOrEmpty)
            {
                return node.Update(
                    node.ConstructorOpt,
                    rewrittenArguments,
                    ReadOnlyArray<string>.Null,
                    argumentRefKinds,
                    false,
                    ReadOnlyArray<int>.Null,
                    node.ConstantValueOpt,
                    node.Type);
            }
            else
            {
                return new BoundSequence(
                    null,
                    null,
                    temps,
                    ReadOnlyArray<BoundExpression>.Empty,
                    new BoundObjectCreationExpression(
                        node.Syntax,
                        node.SyntaxTree,
                        node.ConstructorOpt,
                        rewrittenArguments,
                        ReadOnlyArray<string>.Null,
                        argumentRefKinds,
                        false,
                        ReadOnlyArray<int>.Null,
                        node.ConstantValueOpt,
                        node.Type),
                    node.Type
                );
            }
        }
Example #14
0
        private void InPlaceCtorCall(BoundExpression target, BoundObjectCreationExpression objCreation, bool used)
        {
            var temp = EmitAddress(target, AddressKind.Writeable);
            Debug.Assert(temp == null, "in-place ctor target should not create temps");

            var constructor = objCreation.Constructor;
            EmitArguments(objCreation.Arguments, constructor.Parameters);
            // -2 to adjust for consumed target address and not produced value.
            var stackAdjustment = GetObjCreationStackBehavior(objCreation) - 2;
            _builder.EmitOpCode(ILOpCode.Call, stackAdjustment);
            // for variadic ctors emit expanded ctor token
            EmitSymbolToken(constructor, objCreation.Syntax,
                            constructor.IsVararg ? (BoundArgListOperator)objCreation.Arguments[objCreation.Arguments.Length - 1] : null);

            if (used)
            {
                Debug.Assert(TargetIsNotOnHeap(target), "cannot read-back the target since it could have been modified");
                EmitExpression(target, used: true);
            }
        }
Example #15
0
 private EEMethodSymbol GetTypeVariablesMethod(EENamedTypeSymbol container, string methodName, NamedTypeSymbol typeVariablesType)
 {
     var syntax = SyntaxFactory.IdentifierName(SyntaxFactory.MissingToken(SyntaxKind.IdentifierToken));
     return this.CreateMethod(container, methodName, syntax, (method, diagnostics) =>
     {
         var type = method.TypeMap.SubstituteNamedType(typeVariablesType);
         var expression = new BoundObjectCreationExpression(syntax, type.InstanceConstructors[0]);
         var statement = new BoundReturnStatement(syntax, expression) { WasCompilerGenerated = true };
         return statement;
     });
 }
Example #16
0
 private static bool IsValidDefaultValue(BoundObjectCreationExpression expression)
 {
     return(expression.Constructor.IsDefaultValueTypeConstructor() && expression.InitializerExpressionOpt == null);
 }
Example #17
0
 private static bool IsValidDefaultValue(BoundObjectCreationExpression expression)
 {
     return expression.Constructor.IsDefaultValueTypeConstructor() && expression.InitializerExpressionOpt == null;
 }