Esempio n. 1
0
        private ICodeNode ConvertMemberInit(MethodInvocationExpression invocation)
        {
            if (invocation.Arguments.Count != 2)
            {
                return(null);
            }

            ObjectCreationExpression creation = Visit(invocation.Arguments[0]) as ObjectCreationExpression;

            if (creation == null || creation.Initializer != null)
            {
                return(null);
            }

            ArrayCreationExpression initializerArray = Visit(invocation.Arguments[1]) as ArrayCreationExpression;

            if (initializerArray == null || initializerArray.Dimensions == null || initializerArray.Dimensions.Count != 1 || initializerArray.Initializer == null ||
                initializerArray.Initializer.Expressions == null ||
                initializerArray.Initializer.Expressions.Any(expr => expr.CodeNodeType != CodeNodeType.BinaryExpression || !(expr as BinaryExpression).IsAssignmentExpression))
            {
                return(null);
            }

            if (initializerArray.Initializer.Expressions.Count > 0)
            {
                creation.Initializer             = new InitializerExpression(initializerArray.Initializer.Expression, InitializerType.ObjectInitializer);
                creation.Initializer.IsMultiLine = true;
            }

            return(creation);
        }
        public CCodeBoxForPrimitiveValuesOrEnumsDeclaration(INamedTypeSymbol type)
            : base(new BoxMethod(type))
        {
            this.type = type;

            var specialTypeConstructorMethod = new CCodeSpecialTypeOrEnumConstructorDeclaration.SpecialTypeConstructorMethod(type);
            var objectCreationExpression     = new ObjectCreationExpression
            {
                Type        = type,
                IsReference = true,
                Method      = specialTypeConstructorMethod,
                Arguments   =
                {
                    new Parameter
                    {
                        ParameterSymbol =
                            specialTypeConstructorMethod.Parameters.First()
                    }
                }
            };

            MethodBodyOpt = new MethodBody(Method)
            {
                Statements = { new ReturnStatement {
                                   ExpressionOpt = objectCreationExpression
                               } }
            };
        }
Esempio n. 3
0
        public ICodeNode VisitObjectCreationExpression(ObjectCreationExpression node)
        {
            if (node.Type == null || node.Constructor == null || !node.Type.IsGenericInstance)
            {
                return(null);
            }

            TypeDefinition typeDef = node.Type.Resolve();

            if (!typeDef.IsAnonymous())
            {
                return(null);
            }

            initializerExpressions = new BlockExpression(null);
            ProcessAnonymousType(typeDef, node.Type as GenericInstanceType, node.Constructor.Resolve(), node.Arguments);

            node.Initializer = new InitializerExpression(initializerExpressions, InitializerType.ObjectInitializer);

            List <Instruction> instructions = new List <Instruction>(node.MappedInstructions);

            foreach (Expression argument in node.Arguments)
            {
                instructions.AddRange(argument.UnderlyingSameMethodInstructions);
            }
            InitializerExpression initializer = new InitializerExpression(initializerExpressions, InitializerType.AnonymousInitializer);

            return(new AnonymousObjectCreationExpression(node.Constructor, typeDef, initializer, instructions));
        }
Esempio n. 4
0
        private ICodeNode ConvertListInit(MethodInvocationExpression invocation)
        {
            if (invocation.Arguments.Count != 2)
            {
                return(null);
            }

            ObjectCreationExpression creation = Visit(invocation.Arguments[0]) as ObjectCreationExpression;

            if (creation == null || creation.Initializer != null)
            {
                return(null);
            }

            ArrayCreationExpression initializerArray = Visit(invocation.Arguments[1]) as ArrayCreationExpression;

            if (initializerArray == null || initializerArray.Dimensions == null || initializerArray.Dimensions.Count != 1 || initializerArray.Initializer == null ||
                initializerArray.Initializer.Expressions == null)
            {
                return(null);
            }

            creation.Initializer             = new InitializerExpression(initializerArray.Initializer.Expression, InitializerType.CollectionInitializer);
            creation.Initializer.IsMultiLine = true;
            return(creation);
        }
Esempio n. 5
0
 public override INode VisitObjectCreationExpression(ObjectCreationExpression objectCreationExpression)
 {
     return(new ObjectCreationExpression(objectCreationExpression.Context, objectCreationExpression.Children.Select(Visit))
     {
         Type = objectCreationExpression.Type
     });
 }
            private bool CheckDelegateCreation(BinaryExpression theAssignExpression)
            {
                if (theAssignExpression.Left.CodeNodeType != CodeNodeType.VariableReferenceExpression ||
                    theAssignExpression.Right.CodeNodeType != CodeNodeType.ObjectCreationExpression)
                {
                    return(false);
                }

                ObjectCreationExpression theObjectCreation = theAssignExpression.Right as ObjectCreationExpression;

                if (theObjectCreation.Arguments.Count != 0)
                {
                    return(false);
                }

                TypeDefinition createdObjectType = theObjectCreation.ExpressionType.Resolve();

                if (createdObjectType == null || !CheckTypeForCompilerGeneratedAttribute(createdObjectType))
                {
                    return(false);
                }

                this.delegateTypeDef      = createdObjectType;
                delegateVariableReference = (theAssignExpression.Left as VariableReferenceExpression).Variable;
                return(true);
            }
 private TypeReference GetUseInObjectCreation(ObjectCreationExpression objectCreationExpression, VariableReference variable)
 {
     V_0 = null;
     V_1 = objectCreationExpression.get_Arguments().GetEnumerator();
     try
     {
         while (V_1.MoveNext())
         {
             V_2 = V_1.get_Current();
             if (V_2 as VariableReferenceExpression == null || (object)(V_2 as VariableReferenceExpression).get_Variable() != (object)variable)
             {
                 continue;
             }
             V_0 = V_2;
         }
     }
     finally
     {
         if (V_1 != null)
         {
             V_1.Dispose();
         }
     }
     return(objectCreationExpression.get_Constructor().get_Parameters().get_Item(objectCreationExpression.get_Arguments().IndexOf(V_0)).ResolveParameterType(objectCreationExpression.get_Constructor()));
 }
            public override ICodeNode VisitObjectCreationExpression(ObjectCreationExpression node)
            {
                if (state == State.ReplaceDelegate && node.Arguments != null && node.Arguments.Count == 2 &&
                    node.Arguments[0].CodeNodeType == CodeNodeType.VariableReferenceExpression &&
                    node.Arguments[1].CodeNodeType == CodeNodeType.MethodReferenceExpression &&
                    delegateCopies.Contains((node.Arguments[0] as VariableReferenceExpression).Variable))
                {
                    //final check inserted here for optimization
                    TypeDefinition objectType = node.Constructor.DeclaringType.Resolve();
                    if (objectType == null || objectType.BaseType == null || objectType.BaseType.FullName != "System.MulticastDelegate")
                    {
                        return(base.VisitObjectCreationExpression(node));
                    }

                    MethodReference  methodReference  = (node.Arguments[1] as MethodReferenceExpression).Method;
                    MethodDefinition methodDefinition = (node.Arguments[1] as MethodReferenceExpression).MethodDefinition;

                    MethodSpecificContext delegateMethodContext = new MethodSpecificContext(methodDefinition.Body);
                    DecompilationContext  innerContext          = new DecompilationContext(delegateMethodContext, context.TypeContext, context.ModuleContext, context.AssemblyContext);
                    delegateMethodContext.FieldToExpression = fieldDefToAssignedValueMap;

                    BlockStatement methodStatements = methodDefinition.Body.DecompileLambda(Language, innerContext);

                    if ((methodStatements.Statements.Count == 1) && (methodStatements.Statements[0].CodeNodeType == CodeNodeType.ExpressionStatement) &&
                        ((methodStatements.Statements[0] as ExpressionStatement).Expression.CodeNodeType == CodeNodeType.ReturnExpression))
                    {
                        ReturnExpression          returnExpression          = (methodStatements.Statements[0] as ExpressionStatement).Expression as ReturnExpression;
                        ShortFormReturnExpression shortFormReturnExpression = new ShortFormReturnExpression(returnExpression.Value, returnExpression.MappedInstructions);
                        methodStatements = new BlockStatement();
                        methodStatements.Statements.Add(new ExpressionStatement(shortFormReturnExpression));
                    }

                    this.context.MethodContext.VariableDefinitionToNameMap.AddRange(innerContext.MethodContext.VariableDefinitionToNameMap);
                    this.context.MethodContext.VariableNamesCollection.UnionWith(innerContext.MethodContext.VariableNamesCollection);
                    this.context.MethodContext.AddInnerMethodParametersToContext(innerContext.MethodContext);
                    this.context.MethodContext.GotoStatements.AddRange(innerContext.MethodContext.GotoStatements);
                    this.context.MethodContext.GotoLabels.AddRange(innerContext.MethodContext.GotoLabels);

                    ExpressionCollection expressionCollection = new ExpressionCollection();
                    bool hasAnonymousParamterer = LambdaExpressionsHelper.HasAnonymousParameter(methodDefinition.Parameters);
                    foreach (ParameterDefinition parameter in methodDefinition.Parameters)
                    {
                        expressionCollection.Add(new LambdaParameterExpression(parameter, !hasAnonymousParamterer, null));
                    }

                    delegatesFound.Add(methodStatements);

                    LambdaExpression lambdaExpression =
                        new LambdaExpression(expressionCollection, methodStatements, methodDefinition.IsAsync(), methodDefinition.IsFunction(), methodReference.Parameters, false,
                                             node.Arguments[1].MappedInstructions)
                    {
                        ExpressionType = objectType
                    };

                    DelegateCreationExpression result = new DelegateCreationExpression(node.Constructor.DeclaringType, lambdaExpression, node.Arguments[0], node.MappedInstructions);
                    return(result);
                }

                return(base.VisitObjectCreationExpression(node));
            }
        public override ICodeNode VisitObjectCreationExpression(ObjectCreationExpression node)
        {
            base.VisitObjectCreationExpression(node);

            FixArguments(node.Constructor, node.Arguments);

            return(node);
        }
 public override void VisitObjectCreationExpression(ObjectCreationExpression node)
 {
     this.VisitObjectCreationExpression(node);
     if (node.get_Constructor() != null)
     {
         this.CheckArguments(node.get_Constructor().get_Parameters(), node.get_Arguments());
     }
     return;
 }
Esempio n. 11
0
 public override void ExitObjectCreationExpression(ObjectCreationExpression objectCreationExpression)
 {
     Errors.Add(new CompilationError(objectCreationExpression.Context, "It is not possible to create new objects."));
     if (!(objectCreationExpression.CreatedType is ReferenceType referenceType) || !(referenceType.Declaration is ClassDeclaration))
     {
         Errors.Add(new CompilationError(objectCreationExpression.Context, $"{objectCreationExpression.CreatedType} is not a class. You can not use new."));
     }
     objectCreationExpression.Type = objectCreationExpression.CreatedType;
 }
Esempio n. 12
0
 public override void VisitObjectCreationExpression(ObjectCreationExpression node)
 {
     WriteKeyword("new");
     WriteSpace();
     WriteReference(node.Constructor != null ? node.Constructor.DeclaringType : node.Type);
     WriteToken("(");
     Visit(node.Arguments);
     WriteToken(")");
 }
 public override ICodeNode VisitObjectCreationExpression(ObjectCreationExpression node)
 {
     node = (ObjectCreationExpression)this.VisitObjectCreationExpression(node);
     if (node.get_Arguments().get_Count() != 0)
     {
         this.VisitInvocationArguments(node.get_Arguments(), node.get_Constructor());
     }
     return(node);
 }
 public override void VisitObjectCreationExpression(ObjectCreationExpression node)
 {
     if (state == SearchState.Propagation)
     {
         canBePropagated = false;
         return;
     }
     base.VisitObjectCreationExpression(node);
 }
 public override void VisitObjectCreationExpression(ObjectCreationExpression node)
 {
     if (node.Constructor != null)
     {
         TrySetObjectCreationPendingName(node.Constructor.DeclaringType);
     }
     ClearPendingForSuggestion();
     base.VisitObjectCreationExpression(node);
 }
Esempio n. 16
0
 public override ICodeNode VisitObjectCreationExpression(ObjectCreationExpression node)
 {
     V_0 = this.rebuildAnonymousInitializersStep.VisitObjectCreationExpression(node);
     if (V_0 != null)
     {
         return(V_0);
     }
     return(this.VisitObjectCreationExpression(node));
 }
Esempio n. 17
0
 public override ICodeNode VisitObjectCreationExpression(ObjectCreationExpression node)
 {
     node = (ObjectCreationExpression)base.VisitObjectCreationExpression(node);
     if (node.Arguments.Count != 0)
     {
         VisitInvocationArguments(node.Arguments, node.Constructor);
     }
     return(node);
 }
 public override void VisitObjectCreationExpression(ObjectCreationExpression node)
 {
     if (this.state == ExpressionPropagationStep.ExpressionTreeVisitor.SearchState.Propagation)
     {
         this.canBePropagated = false;
         return;
     }
     this.VisitObjectCreationExpression(node);
     return;
 }
 public override void VisitObjectCreationExpression(ObjectCreationExpression node)
 {
     if (node.get_Constructor() != null)
     {
         this.TrySetObjectCreationPendingName(node.get_Constructor().get_DeclaringType());
     }
     this.ClearPendingForSuggestion();
     this.VisitObjectCreationExpression(node);
     return;
 }
        public override void VisitObjectCreationExpression(ObjectCreationExpression node)
        {
            base.VisitObjectCreationExpression(node);
            MethodReference calledMethod = node.Constructor;

            if (calledMethod != null)
            {
                FixArguments(calledMethod, node.Arguments);
            }
        }
        public override void VisitObjectCreationExpression(ObjectCreationExpression node)
        {
            base.VisitObjectCreationExpression(node);
            MethodReference ctorMethod = node.Constructor;

            if (ctorMethod != null)
            {
                TraverseMethodParameters(ctorMethod, node.Arguments);
            }
        }
Esempio n. 22
0
        public override void OnInitobj(Instruction instruction)
        {
            var address = (AddressOfExpression)Pop();

            var type = (TypeReference)instruction.Operand;

            var @new = new ObjectCreationExpression(null, type, null);

            Push(new AssignExpression(address.Expression, @new));
        }
        public override ICodeNode VisitObjectCreationExpression(ObjectCreationExpression node)
        {
            ICodeNode result = rebuildAnonymousInitializersStep.VisitObjectCreationExpression(node);

            if (result != null)
            {
                return(result);
            }
            return(base.VisitObjectCreationExpression(node));
        }
Esempio n. 24
0
 public override void VisitObjectCreationExpression(ObjectCreationExpression node)
 {
     this.VisitObjectCreationExpression(node);
     V_0 = node.get_Constructor();
     if (V_0 != null)
     {
         this.TraverseMethodParameters(V_0, node.get_Arguments());
     }
     return;
 }
        public override void VisitObjectCreationExpression(ObjectCreationExpression node)
        {
            base.VisitObjectCreationExpression(node);

            if (node.Constructor != null)
            {
                CheckArguments(node.Constructor.Parameters, node.Arguments);
            }

            // node.Initializer contains BinaryExpressions
        }
Esempio n. 26
0
        public override void OnNewobj(Instruction instruction)
        {
            var constructor = (MethodReference)instruction.Operand;

            var arguments = PopRange(constructor.Parameters.Count);

            var @new = new ObjectCreationExpression(constructor, null, null);

            AddRange(@new.Arguments, arguments);

            Push(@new);
        }
Esempio n. 27
0
        /// <summary>
        /// </summary>
        /// <param name="c">
        /// </param>
        /// <param name="newNonStaticMethod">
        /// </param>
        /// <param name="nonStaticType">
        /// </param>
        private static void WriteNewMethod(CCodeWriterBase c, IMethodSymbol newNonStaticMethod, NamedTypeImpl nonStaticType)
        {
            c.WriteMethodDeclaration(newNonStaticMethod, true, true);
            c.NewLine();
            c.OpenBlock();

            var objectCreationExpression = new ObjectCreationExpression {
                Type = nonStaticType, NewOperator = true
            };

            foreach (var parameter in newNonStaticMethod.Parameters)
            {
                Expression parameterExpression = new Parameter {
                    ParameterSymbol = parameter
                };
                if (parameter.Name == "m")
                {
                    parameterExpression = new Cast
                    {
                        Operand        = parameterExpression,
                        MapPointerCast = true,
                        MapPointerCastTypeParameter1 =
                            new Access
                        {
                            AccessType  = Access.AccessTypes.DoubleColon,
                            ReceiverOpt = new TypeExpression {
                                Type = nonStaticType, TypeNameRequred = true
                            },
                            Expression = new Parameter {
                                ParameterSymbol = new ParameterImpl {
                                    Name = "_Memptr"
                                }
                            }
                        },
                        MapPointerCastTypeParameter2 = new TypeExpression {
                            Type = new TypeImpl {
                                TypeKind = TypeKind.TypeParameter, Name = "_Memptr"
                            }
                        },
                    };
                }

                objectCreationExpression.Arguments.Add(parameterExpression);
            }

            new ReturnStatement {
                ExpressionOpt = objectCreationExpression
            }.WriteTo(c);

            c.EndBlockWithoutNewLine();
            c.EndStatement();
        }
Esempio n. 28
0
        static dynamic handle(ObjectCreationExpression v)
        {
            string typename = ((dynamic)v.Type).GenericIdentifier;
            var    obj      = new ObjectInstance(typename, localVarDeclarationName);

            //TODO: argument list needs to be applied to a constructor => v.ArgumentsList
            if (localVarDeclarationName != null)
            {
                executingFunction.Localvars[localVarDeclarationName] = obj;                                           //-> e.g. new Class() will not be a tracked local
            }
            localVarDeclarationName = null;
            return(obj);
        }
        /// <summary>
        /// Determines the use type of <paramref name="variable"/> in <paramref name="objectCreationExpression"/>.
        /// </summary>
        /// <param name="objectCreationExpression">The object creataion expression.</param>
        /// <param name="variable">The variable.</param>
        /// <returns>Returns the ClassHierarchyNode corresponding to the infered type.</returns>
        private TypeReference GetUseInObjectCreation(ObjectCreationExpression objectCreationExpression, VariableReference variable)
        {
            Expression arg = null;

            foreach (Expression expr in objectCreationExpression.Arguments)
            {
                if (expr is VariableReferenceExpression && (expr as VariableReferenceExpression).Variable == variable)
                {
                    arg = expr;
                }
            }
            return(objectCreationExpression.Constructor.Parameters[objectCreationExpression.Arguments.IndexOf(arg)].ResolveParameterType(objectCreationExpression.Constructor));
        }
Esempio n. 30
0
        public static UstNode GetObject(string type)
        {
            UstNode ustNode = null;

            switch (type)
            {
            case IdConstants.RootIdName:
                ustNode = new RootUstNode();
                break;

            case IdConstants.UsingDirectiveIdName:
                ustNode = new UsingDirective();
                break;

            case IdConstants.NamespaceIdName:
                ustNode = new NamespaceDeclaration();
                break;

            case IdConstants.ClassIdName:
                ustNode = new ClassDeclaration();
                break;

            case IdConstants.InterfaceIdName:
                ustNode = new InterfaceDeclaration();
                break;

            case IdConstants.BodyIdName:
                ustNode = new BlockStatement();
                break;

            case IdConstants.ObjectCreationIdName:
                ustNode = new ObjectCreationExpression();
                break;

            case IdConstants.InvocationIdName:
                ustNode = new InvocationExpression();
                break;

            case IdConstants.LiteralIdName:
                ustNode = new LiteralExpression();
                break;

            case IdConstants.MethodIdName:
                ustNode = new MethodDeclaration();
                break;

            default: break;
            }

            return(ustNode);
        }
        private static bool IsInsideInitializerToTypeWithSource(CheckContentAvailabilityEventArgs ea)
        {
            _TypeElement = null;
            _ObjectCreationExpression = ea.Element as ObjectCreationExpression;
            if (_ObjectCreationExpression == null)
            {
                _ObjectCreationExpression = ea.Element.GetParent(LanguageElementType.ObjectCreationExpression) as ObjectCreationExpression;
                if (_ObjectCreationExpression == null)
                    return false;
            }
            _InitializerExpression = _ObjectCreationExpression.ObjectInitializer;
            if (_InitializerExpression == null)
                return false;
            _TypeElement = _ObjectCreationExpression.ObjectType.Resolve(ParserServices.SourceTreeResolver) as ITypeElement;
            if (_TypeElement == null)
                return false;

            return !_TypeElement.InReferencedAssembly;
        }
Esempio n. 32
0
 public void VisitObjectCreationExpression(ObjectCreationExpression creation)
 {
     throw new NotImplementedException();
 }
        private string GetNewConstructorCall(ObjectCreationExpression objectCreationWithInitializer, ITypeElement type)
        {
            string result = String.Empty;
            if (type == null || objectCreationWithInitializer == null || objectCreationWithInitializer.ObjectInitializer == null)
                return result;

            ExpressionCollection arguments = objectCreationWithInitializer.ObjectInitializer.Initializers;
            ExpressionCollection newArgs = new ExpressionCollection();
            foreach (Expression argument in arguments)
            {
                MemberInitializerExpression memberInitializerExpression = argument as MemberInitializerExpression;
                if (memberInitializerExpression == null)
                    continue;

                newArgs.Add(memberInitializerExpression.Value);
            }

            ObjectCreationExpression newObjectCreationExpression = new ObjectCreationExpression(new TypeReferenceExpression(type.Name), newArgs);
            if (newObjectCreationExpression != null)
                result = CodeRush.Language.GenerateElement(newObjectCreationExpression);
            return result;
        }
 public void VisitObjectCreationExpression(ObjectCreationExpression creation)
 {
     foreach(var keyvalue in creation.Items)
         keyvalue.ValueExpression.AcceptWalker(this);
 }
Esempio n. 35
0
        private void Format_Object_Creation_Expression(StringBuilder sb, ObjectCreationExpression oce)
        {
            sb.Append("new ");
            if (oce.IsImplicitlyTyped == false)
            {
                sb.Append(FormatterUtility.FormatDataType(oce.ObjectType, document, controller));
            }

            // Figure out if this is a constructor call or array creation
            // If it is an array then it needs the square brackets. Constructor calls
            // with initialisers can omit the parentheses if there are no arguments.
            string openingBrace = "[";
            string closingBrace = "]";
            bool isArrayCreation = false;
            {
                IToken startToken = document.Tokens.GetTokenAtOffset(oce.StartOffset);
                TokenStream tokenStream = document.GetTokenStream(startToken);

                while (startToken != null && startToken.StartOffset < oce.EndOffset)
                {
                    string tokenText = document.GetSubstring(startToken.TextRange);
                    if (tokenText == "[")
                    {
                        isArrayCreation = true;
                        break;
                    }
                    if (tokenText == "(")
                        break;
                    if (tokenText == "{")
                        break; // If we get to the initialiser, we have gone too far.
                    startToken = tokenStream.Read();
                }

                if (isArrayCreation == false)
                {
                    openingBrace = "(";
                    closingBrace = ")";
                }
            }

            Process_Generic_Type_Arguments(sb, oce.GenericTypeArguments);

            if (!isArrayCreation || (oce.Arguments != null && oce.Arguments.Count != 0) || oce.IsImplicitlyTyped)
            {
                sb.Append(openingBrace);
                if (oce.Arguments != null)
                {
                    for (int i = 0; i < oce.Arguments.Count; i++)
                    {
                        sb.Append(FormatExpression(oce.Arguments[i] as Expression));
                        if (oce.Arguments.Count > 1 && i < oce.Arguments.Count - 1)
                            sb.Append(", ");
                    }
                }
                sb.Append(closingBrace);
            }

            if (oce.Initializer != null)
                sb.Append(FormatExpression(oce.Initializer));
        }