public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
        {
            string declaredType = node.Declaration.Type.ToString();

            bool basicType = SyntaxFactoryExtensions.IsBasicType(declaredType);

            if (!basicType)
            {
                return(node);
            }

            if (declaredType.Equals("int"))
            {
                return(node);
            }
            if (declaredType.Equals("bool"))
            {
                return(node);
            }
            if (declaredType.Equals("char"))
            {
                return(node);
            }
            if (declaredType.Equals("string"))
            {
                return(node);
            }

            foreach (var variable in node.Declaration.Variables)
            {
                if (variable.Initializer == null)
                {
                    continue;
                }
                var oldRightValue = variable.Initializer.Value;
                markedNodes.Add(oldRightValue);

                var newRightValue = SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression, oldRightValue,
                                                                   SyntaxFactoryExtensions.DefaultValue(declaredType));
                replacementNodes.Add(new Tuple <ExpressionSyntax, ExpressionSyntax> (oldRightValue, newRightValue));
            }

            return(node);
        }
Exemple #2
0
        public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
        {
            var declaration = node;

            List <StatementSyntax> statements = new List <StatementSyntax>();

            markedNodes.Add(node);

            foreach (var variable in declaration.Declaration.Variables)
            {
                int              index           = _virtualizationContext.DataIndex;
                string           name            = variable.Identifier.Text;
                SyntaxAnnotation indexMarker     = new SyntaxAnnotation("index", index + "");
                SyntaxAnnotation nameMarker      = new SyntaxAnnotation("name", name);
                SyntaxAnnotation variableMarker  = new SyntaxAnnotation("type", "variable");
                SyntaxAnnotation codeIndexMarker = new SyntaxAnnotation("code", "undefined");
                SyntaxAnnotation uniqueMarker    = new SyntaxAnnotation("unique", "" + VirtualizationContext.UniqueId);

                var virtualData = new VirtualData();
                virtualData.Type  = declaration.Declaration.Type.ToString();
                virtualData.Index = index;
                virtualData.Name  = name;
                virtualData.Annotations.Add(indexMarker);
                virtualData.Annotations.Add(nameMarker);
                virtualData.Annotations.Add(variableMarker);
                virtualData.Annotations.Add(codeIndexMarker);
                virtualData.Annotations.Add(uniqueMarker);
                _virtualizationContext.data.Add(virtualData);

                //TODO: split for multiple variables in the same declaration
                var initializer = variable.Initializer;
                if (initializer == null)
                {
                    continue;
                }

                SyntaxNode rightValue;

                rightValue = initializer.DescendantNodes().First();
                ExpressionStatementSyntax newNode;
                if (SyntaxFactoryExtensions.IsBasicType(virtualData.Type))
                {
                    newNode = SyntaxFactoryExtensions.DataVirtualAssignment(rightValue, virtualData.Annotations.ToArray())
                              .WithLeadingTrivia(declaration.GetLeadingTrivia())
                              .WithTrailingTrivia(declaration.GetTrailingTrivia())
                    ;
                }
                else
                {
                    newNode = SyntaxFactoryExtensions.DataVirtualAssignment(virtualData.Type, rightValue, virtualData.Annotations.ToArray())
                              .WithLeadingTrivia(declaration.GetLeadingTrivia())
                              .WithTrailingTrivia(declaration.GetTrailingTrivia())
                    ;
                }

                virtualData.Node      = newNode;
                virtualData.Statement = newNode;
                statements.Add(newNode);
            }

            replacementNodes.Add(new Tuple <LocalDeclarationStatementSyntax, List <StatementSyntax> >(node, statements));

            return(node);
        }