private SyntaxList <StatementSyntax> AddLocalVariables(VBasic.VisualBasicSyntaxNode node)
        {
            _additionalLocals.PushScope();
            IEnumerable <SyntaxNode> csNodes = _wrappedVisitor.Visit(node);

            var additionalDeclarations = new List <StatementSyntax>();

            if (_additionalLocals.Count() > 0)
            {
                var newNames = new Dictionary <string, string>();
                csNodes = csNodes.Select(csNode => csNode.ReplaceNodes(csNode.GetAnnotatedNodes(AdditionalLocals.Annotation), (an, _) => {
                    var id       = (an as IdentifierNameSyntax).Identifier.ValueText;
                    newNames[id] = NameGenerator.GetUniqueVariableNameInScope(_semanticModel, _generatedNames, node, _additionalLocals[id].Prefix);
                    return(SyntaxFactory.IdentifierName(newNames[id]));
                })).ToList();

                foreach (var additionalLocal in _additionalLocals)
                {
                    var decl = CommonConversions.CreateVariableDeclarationAndAssignment(newNames[additionalLocal.Key], additionalLocal.Value.Initializer);
                    additionalDeclarations.Add(SyntaxFactory.LocalDeclarationStatement(decl));
                }
            }
            _additionalLocals.PopScope();

            return(SyntaxFactory.List(additionalDeclarations.Concat(csNodes)));
        }
示例#2
0
        public async Task <SyntaxList <StatementSyntax> > CreateLocalsAsync(VBasic.VisualBasicSyntaxNode vbNode, IEnumerable <StatementSyntax> csNodes, HashSet <string> generatedNames, SemanticModel semanticModel)
        {
            var preDeclarations = new List <StatementSyntax>();
            var postAssignments = new List <StatementSyntax>();

            var additionalDeclarationInfo = GetDeclarations();
            var newNames = additionalDeclarationInfo.ToDictionary(l => l.Id, l =>
                                                                  NameGenerator.GetUniqueVariableNameInScope(semanticModel, generatedNames, vbNode, l.Prefix)
                                                                  );

            foreach (var additionalLocal in additionalDeclarationInfo)
            {
                var decl = CommonConversions.CreateVariableDeclarationAndAssignment(newNames[additionalLocal.Id],
                                                                                    additionalLocal.Initializer, additionalLocal.Type);
                preDeclarations.Add(CS.SyntaxFactory.LocalDeclarationStatement(decl));
            }

            foreach (var additionalAssignment in GetPostAssignments())
            {
                var assign = CS.SyntaxFactory.AssignmentExpression(CS.SyntaxKind.SimpleAssignmentExpression, additionalAssignment.LeftHandSide, additionalAssignment.RightHandSide);
                postAssignments.Add(CS.SyntaxFactory.ExpressionStatement(assign));
            }

            var statementsWithUpdatedIds = ReplaceNames(preDeclarations.Concat(csNodes).Concat(postAssignments), newNames);

            return(CS.SyntaxFactory.List(statementsWithUpdatedIds));
        }
示例#3
0
        public ClassDeclarationSyntax GenerateHelper()
        {
            if (!HasImports)
            {
                return(null);
            }

            var xAttributeList = SyntaxFactory.FieldDeclaration(
                SyntaxFactory.List <AttributeListSyntax>(),
                SyntaxFactory.TokenList(SyntaxFactory.Token(CSSyntaxKind.PrivateKeyword), SyntaxFactory.Token(CSSyntaxKind.StaticKeyword)),
                CommonConversions.CreateVariableDeclarationAndAssignment(
                    "namespaceAttributes", SyntaxFactory.InitializerExpression(
                        CSSyntaxKind.ArrayInitializerExpression,
                        SyntaxFactory.SeparatedList <ExpressionSyntax>(
                            from x in _xNamespaceFields
                            let fieldIdentifierName = SyntaxFactory.IdentifierName(x.Declaration.Variables.Single().Identifier)
                                                      let namespaceNameExpression = SyntaxFactory.MemberAccessExpression(CSSyntaxKind.SimpleMemberAccessExpression, fieldIdentifierName, SyntaxFactory.IdentifierName("NamespaceName"))
                                                                                    let attributeNameExpression = fieldIdentifierName.IsEquivalentTo(DefaultIdentifierName) ? CommonConversions.Literal("xmlns") : BuildXmlnsAttributeName(fieldIdentifierName)
                                                                                                                  let arguments = SyntaxFactory.Argument(attributeNameExpression).Yield().Concat(SyntaxFactory.Argument(namespaceNameExpression))
                                                                                                                                  select SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName("XAttribute")).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(arguments))))),
                    SyntaxFactory.ArrayType(SyntaxFactory.IdentifierName("XAttribute"), SyntaxFactory.SingletonList(SyntaxFactory.ArrayRankSpecifier(SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(SyntaxFactory.OmittedArraySizeExpression()))))));


            var boilerplate = SyntaxFactory.ParseStatement(@"
                TContainer Apply<TContainer>(TContainer x) where TContainer : XContainer
                {
                    foreach (var d in x.Descendants()) {
                        foreach (var n in namespaceAttributes) {
                            var a = d.Attribute(n.Name);
                            if (a != null && a.Value == n.Value) {
                                a.Remove();
                            }
                        }
                    }
                    x.Add(namespaceAttributes);
                    return x;
                }") as LocalFunctionStatementSyntax;

            var applyMethod = SyntaxFactory.MethodDeclaration(
                SyntaxFactory.List <AttributeListSyntax>(),
                SyntaxFactory.TokenList(SyntaxFactory.Token(CSSyntaxKind.InternalKeyword), SyntaxFactory.Token(CSSyntaxKind.StaticKeyword)),
                boilerplate.ReturnType,
                null,
                boilerplate.Identifier,
                boilerplate.TypeParameterList,
                boilerplate.ParameterList,
                boilerplate.ConstraintClauses,
                boilerplate.Body,
                boilerplate.ExpressionBody);

            return(SyntaxFactory.ClassDeclaration(
                       SyntaxFactory.List <AttributeListSyntax>(),
                       SyntaxFactory.TokenList(SyntaxFactory.Token(CSSyntaxKind.InternalKeyword), SyntaxFactory.Token(CSSyntaxKind.StaticKeyword)),
                       HelperClassUniqueIdentifierName.Identifier,
                       null, null,
                       SyntaxFactory.List <TypeParameterConstraintClauseSyntax>(),
                       SyntaxFactory.List(_xNamespaceFields.Concat <MemberDeclarationSyntax>(xAttributeList).Concat(applyMethod))
                       ));
        }