Example #1
0
        protected virtual void CreateVisitListStatements(MethodGenerationContext context, bool isSeparatedList)
        {
            ITypeSymbol typeSymbol = ((INamedTypeSymbol)context.PropertyType).TypeArguments.Single();

            IMethodSymbol methodSymbol = FindVisitMethod(typeSymbol);

            string methodName = null;

            if (methodSymbol != null)
            {
                methodName = methodSymbol.Name;
            }
            else if (EliminateDefaultVisit)
            {
                methodName = GetMethodName(typeSymbol);
            }

            if (methodName != null)
            {
                string typeName = typeSymbol.Name;

                string variableName = context.CreateVariableName(typeName.Remove(typeName.Length - 6));

                ForEachStatementSyntax forEachStatement = ForEachVisitStatement(
                    typeName,
                    variableName,
                    SimpleMemberAccessExpression(IdentifierName(context.ParameterName), IdentifierName(context.PropertyName)),
                    VisitStatement(methodName, variableName),
                    checkShouldVisit: true);

                context.AddStatement(forEachStatement);
            }
            else
            {
                methodName = (isSeparatedList) ? "VisitSeparatedList" : "VisitList";

                context.AddStatement(VisitStatement(methodName, context.ParameterName, context.PropertyName));
            }
        }
Example #2
0
        public List <MemberDeclarationSyntax> CreateMemberDeclarations()
        {
            var members = new List <MemberDeclarationSyntax>();

            AddIfNotNull(CreateConstructorDeclaration(Depth));
            AddIfNotNull(CreateShouldVisitPropertyDeclaration());
            AddIfNotNull(CreateVisitNodeMethodDeclaration());

            if (!EliminateDefaultVisit &&
                Depth >= SyntaxWalkerDepth.Node)
            {
                AddIfNotNull(CreateVisitListMethodDeclaration());
                AddIfNotNull(CreateVisitSeparatedListMethodDeclaration());
            }

            if (Depth >= SyntaxWalkerDepth.Token)
            {
                AddIfNotNull(CreateVisitTokenListMethodDeclaration());
            }

            var context = new MethodGenerationContext();

            foreach (ISymbol symbol in VisitMethodSymbols)
            {
                context.MethodSymbol = (IMethodSymbol)symbol;

                AddIfNotNull(CreateVisitMethodDeclaration(context));

                context.MethodSymbol = null;
                context.Statements.Clear();
                context.LocalNames.Clear();
            }

            if (EliminateDefaultVisit)
            {
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_BaseTypeSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_CrefSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_ExpressionSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_InterpolatedStringContentSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_MemberCrefSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_MemberDeclarationSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_PatternSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_QueryClauseSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_SelectOrGroupClauseSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_SimpleNameSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_StatementSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_SwitchLabelSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_TypeParameterConstraintSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_TypeSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_VariableDesignationSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_XmlAttributeSyntax));
                AddIfNotNull(CreateVisitAbstractSyntaxMethodDeclaration(Microsoft_CodeAnalysis_CSharp_Syntax_XmlNodeSyntax));
            }

            return(members);

            void AddIfNotNull(MemberDeclarationSyntax memberDeclaration)
            {
                if (memberDeclaration != null)
                {
                    members.Add(memberDeclaration);
                }
            }
        }
Example #3
0
        protected virtual void CreateVisitStatements(MethodGenerationContext context)
        {
            string      parameterName = context.ParameterName;
            ITypeSymbol propertyType  = context.PropertyType;
            string      propertyName  = context.PropertyName;

            if (propertyType.OriginalDefinition.Equals(SyntaxListSymbol))
            {
                if (UseCustomVisitMethod)
                {
                    CreateVisitListStatements(context, isSeparatedList: false);
                }
                else
                {
                    context.AddStatement(VisitStatement("VisitList", parameterName, propertyName));
                }
            }
            else if (propertyType.OriginalDefinition.Equals(SeparatedSyntaxListSymbol))
            {
                if (UseCustomVisitMethod)
                {
                    CreateVisitListStatements(context, isSeparatedList: true);
                }
                else
                {
                    context.AddStatement(VisitStatement("VisitSeparatedList", parameterName, propertyName));
                }
            }
            else if (propertyType.Equals(SyntaxTokenListSymbol))
            {
                if (Depth >= SyntaxWalkerDepth.Token)
                {
                    context.AddStatement(VisitStatement("VisitTokenList", parameterName, propertyName));
                }
            }
            else if (propertyType.Equals(SyntaxTokenSymbol))
            {
                if (Depth >= SyntaxWalkerDepth.Token)
                {
                    context.AddStatement(VisitStatement("VisitToken", parameterName, propertyName));
                }
            }
            else if (propertyType.EqualsOrInheritsFrom(SyntaxNodeSymbol))
            {
                switch (propertyType.Name)
                {
                case "AccessorListSyntax":
                case "ArgumentListSyntax":
                case "ArrayTypeSyntax":
                case "ArrowExpressionClauseSyntax":
                case "AttributeArgumentListSyntax":
                case "AttributeTargetSpecifierSyntax":
                case "BaseListSyntax":
                case "BlockSyntax":
                case "BracketedArgumentListSyntax":
                case "BracketedParameterListSyntax":
                case "CatchDeclarationSyntax":
                case "CatchFilterClauseSyntax":
                case "ConstructorInitializerSyntax":
                case "CrefBracketedParameterListSyntax":
                case "CrefParameterListSyntax":
                case "CrefSyntax":
                case "ElseClauseSyntax":
                case "EqualsValueClauseSyntax":
                case "ExplicitInterfaceSpecifierSyntax":
                case "ExpressionSyntax":
                case "FinallyClauseSyntax":
                case "FromClauseSyntax":
                case "IdentifierNameSyntax":
                case "InitializerExpressionSyntax":
                case "InterpolationAlignmentClauseSyntax":
                case "InterpolationFormatClauseSyntax":
                case "JoinIntoClauseSyntax":
                case "MemberCrefSyntax":
                case "NameColonSyntax":
                case "NameEqualsSyntax":
                case "NameSyntax":
                case "ParameterListSyntax":
                case "ParameterSyntax":
                case "PatternSyntax":
                case "QueryBodySyntax":
                case "QueryContinuationSyntax":
                case "SelectOrGroupClauseSyntax":
                case "SimpleNameSyntax":
                case "StatementSyntax":
                case "TypeArgumentListSyntax":
                case "TypeParameterListSyntax":
                case "TypeSyntax":
                case "VariableDeclarationSyntax":
                case "VariableDesignationSyntax":
                case "WhenClauseSyntax":
                case "XmlElementEndTagSyntax":
                case "XmlElementStartTagSyntax":
                case "XmlNameSyntax":
                case "XmlPrefixSyntax":
                {
                    if (UseCustomVisitMethod)
                    {
                        CreateTypeVisitStatements(context);
                    }
                    else
                    {
                        context.AddStatement(VisitStatement("Visit", parameterName, propertyName));
                    }

                    break;
                }

                case "CSharpSyntaxNode":
                {
                    if (!UseCustomVisitMethod)
                    {
                        context.AddStatement(VisitStatement("Visit", parameterName, propertyName));
                        break;
                    }

                    if (EliminateDefaultVisit &&
                        propertyName == "Body" &&
                        context.ParameterType.InheritsFrom(Microsoft_CodeAnalysis_CSharp_Syntax_AnonymousFunctionExpressionSyntax))
                    {
                        CreateVisitAnonymousFunctionStatements(context);
                    }
                    else
                    {
                        CreateTypeVisitStatements(context);
                    }

                    break;
                }

                default:
                {
                    throw new InvalidOperationException($"Unrecognized property type '{propertyType.ToDisplayString()}'.");
                }
                }
            }
            else if (!propertyType.SpecialType.Is(SpecialType.System_Int32, SpecialType.System_Boolean))
            {
                throw new InvalidOperationException();
            }
        }