Ejemplo n.º 1
0
        private Dictionary <ISymbol, string> GetSymbolsToRename()
        {
            ImmutableArray <ISymbol> declarationSymbols = DeclarationSemanticModel.GetDeclaredSymbols(
                BodyOrExpressionBody,
                excludeAnonymousTypeProperty: true,
                cancellationToken: CancellationToken);

            if (!declarationSymbols.Any())
            {
                return(null);
            }

            declarationSymbols = declarationSymbols.RemoveAll(f => f.IsKind(SymbolKind.NamedType, SymbolKind.Field));

            if (!declarationSymbols.Any())
            {
                return(null);
            }

            int position = Node.SpanStart;

            ImmutableArray <ISymbol> invocationSymbols = InvocationSemanticModel.GetSymbolsDeclaredInEnclosingSymbol(
                position,
                excludeAnonymousTypeProperty: true,
                cancellationToken: CancellationToken);

            invocationSymbols = invocationSymbols.AddRange(InvocationSemanticModel.LookupSymbols(position));

            var reservedNames = new HashSet <string>(invocationSymbols.Select(f => f.Name));

            List <ISymbol> symbols = null;

            foreach (ISymbol symbol in declarationSymbols)
            {
                if (reservedNames.Contains(symbol.Name))
                {
                    (symbols ?? (symbols = new List <ISymbol>())).Add(symbol);
                }
            }

            if (symbols == null)
            {
                return(null);
            }

            reservedNames.UnionWith(declarationSymbols.Select(f => f.Name));

            var symbolMap = new Dictionary <ISymbol, string>();

            foreach (ISymbol symbol in symbols)
            {
                string newName = NameGenerator.Default.EnsureUniqueName(symbol.Name, reservedNames);

                symbolMap.Add(symbol, newName);

                reservedNames.Add(newName);
            }

            return(symbolMap);
        }
Ejemplo n.º 2
0
        private Dictionary <ISymbol, string> GetSymbolsToRename()
        {
            ImmutableArray <ISymbol> invocationSymbols = InvocationSemanticModel.GetSymbolsDeclaredInEnclosingSymbol(
                InvocationExpression.SpanStart,
                excludeAnonymousTypeProperty: true,
                cancellationToken: CancellationToken);

            if (invocationSymbols.Any())
            {
                ImmutableArray <ISymbol> declarationSymbols = DeclarationSemanticModel.GetDeclaredSymbols(
                    MethodDeclaration.BodyOrExpressionBody(),
                    excludeAnonymousTypeProperty: true,
                    cancellationToken: CancellationToken);

                if (declarationSymbols.Any())
                {
                    var reservedNames = new HashSet <string>(invocationSymbols.Select(f => f.Name));

                    List <ISymbol> symbols = null;

                    foreach (ISymbol symbol in declarationSymbols)
                    {
                        if (reservedNames.Contains(symbol.Name))
                        {
                            (symbols ?? (symbols = new List <ISymbol>())).Add(symbol);
                        }
                    }

                    if (symbols != null)
                    {
                        reservedNames.UnionWith(declarationSymbols.Select(f => f.Name));

                        var symbolMap = new Dictionary <ISymbol, string>();

                        foreach (ISymbol symbol in symbols)
                        {
                            string newName = NameGenerator.Default.EnsureUniqueName(symbol.Name, reservedNames);

                            symbolMap.Add(symbol, newName);

                            reservedNames.Add(newName);
                        }

                        return(symbolMap);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
        private Dictionary <SyntaxNode, object> GetReplacementMap(SyntaxNode node, Dictionary <ISymbol, string> symbolMap)
        {
            var replacementMap = new Dictionary <SyntaxNode, object>();

            foreach (SyntaxNode descendant in node.DescendantNodesAndSelf(node.Span))
            {
                SyntaxKind kind = descendant.Kind();

                if (kind == SyntaxKind.IdentifierName)
                {
                    var identifierName = (IdentifierNameSyntax)descendant;

                    ISymbol symbol = DeclarationSemanticModel.GetSymbol(identifierName, CancellationToken);

                    if (symbol != null)
                    {
                        if (symbol.IsParameter())
                        {
                            foreach (ParameterInfo parameterInfo in ParameterInfos)
                            {
                                if (parameterInfo.ParameterSymbol.OriginalDefinition.Equals(symbol))
                                {
                                    replacementMap.Add(identifierName, parameterInfo.Expression);
                                    break;
                                }
                            }
                        }
                        else if (symbol.IsTypeParameter())
                        {
                            var typeParameter = (ITypeParameterSymbol)symbol;

                            ImmutableArray <ITypeSymbol> typeArguments = MethodSymbol.TypeArguments;

                            if (typeArguments.Length > typeParameter.Ordinal)
                            {
                                replacementMap.Add(identifierName, typeArguments[typeParameter.Ordinal].ToMinimalTypeSyntax(DeclarationSemanticModel, identifierName.SpanStart));
                            }
                        }
                        else if (symbol.IsStatic &&
                                 !identifierName.IsParentKind(SyntaxKind.SimpleMemberAccessExpression))
                        {
                            INamedTypeSymbol containingType = symbol.ContainingType;

                            if (!InvocationEnclosingType
                                .BaseTypesAndSelf()
                                .Any(f => f.Equals(containingType)))
                            {
                                replacementMap.Add(identifierName, CSharpFactory.SimpleMemberAccessExpression(containingType.ToTypeSyntax().WithSimplifierAnnotation(), identifierName));
                            }
                        }

                        if (symbolMap != null &&
                            symbolMap.TryGetValue(symbol, out string name))
                        {
                            replacementMap.Add(identifierName, SyntaxFactory.IdentifierName(name));
                        }
                    }
                }
                else if (symbolMap != null)
                {
                    if (kind.Is(
                            SyntaxKind.VariableDeclarator,
                            SyntaxKind.SingleVariableDesignation,
                            SyntaxKind.Parameter,
                            SyntaxKind.TypeParameter,
                            SyntaxKind.ForEachStatement,
                            SyntaxKind.ForEachVariableStatement))
                    {
                        ISymbol symbol = DeclarationSemanticModel.GetDeclaredSymbol(descendant, CancellationToken);

                        Debug.Assert(symbol != null || (descendant as ForEachVariableStatementSyntax)?.Variable?.Kind() == SyntaxKind.TupleExpression, kind.ToString());

                        if (symbol != null &&
                            symbolMap.TryGetValue(symbol, out string name))
                        {
                            replacementMap.Add(descendant, name);
                        }
                    }
                }
            }

            return(replacementMap);
        }
Ejemplo n.º 4
0
        public TNode RewriteNode <TNode>(TNode node) where TNode : SyntaxNode
        {
            Dictionary <ISymbol, string> symbolMap = GetSymbolsToRename();

            var replacementMap = new Dictionary <SyntaxNode, object>();

            foreach (SyntaxNode descendant in node.DescendantNodes(node.Span))
            {
                SyntaxKind kind = descendant.Kind();

                if (kind == SyntaxKind.IdentifierName)
                {
                    var identifierName = (IdentifierNameSyntax)descendant;

                    ISymbol symbol = DeclarationSemanticModel.GetSymbol(identifierName, CancellationToken);

                    if (symbol != null)
                    {
                        if (symbol.IsParameter())
                        {
                            foreach (ParameterInfo parameterInfo in ParameterInfos)
                            {
                                if (parameterInfo.ParameterSymbol.OriginalDefinition.Equals(symbol))
                                {
                                    replacementMap.Add(identifierName, parameterInfo.Expression);
                                    break;
                                }
                            }
                        }
                        else if (symbol.IsStatic &&
                                 !identifierName.IsParentKind(SyntaxKind.SimpleMemberAccessExpression))
                        {
                            INamedTypeSymbol containingType = symbol.ContainingType;

                            if (!InvocationEnclosingType
                                .BaseTypesAndSelf()
                                .Any(f => f.Equals(containingType)))
                            {
                                replacementMap.Add(identifierName, CSharpFactory.SimpleMemberAccessExpression(containingType.ToTypeSyntax().WithSimplifierAnnotation(), identifierName));
                            }
                        }
                        if (symbolMap != null)
                        {
                            string name;
                            if (symbolMap.TryGetValue(symbol, out name))
                            {
                                replacementMap.Add(identifierName, SyntaxFactory.IdentifierName(name));
                            }
                        }
                    }
                }
                else if (symbolMap != null &&
                         kind == SyntaxKind.VariableDeclarator)
                {
                    var variableDeclarator = (VariableDeclaratorSyntax)descendant;

                    ISymbol symbol = DeclarationSemanticModel.GetDeclaredSymbol(variableDeclarator, CancellationToken);

                    string name;
                    if (symbolMap.TryGetValue(symbol, out name))
                    {
                        replacementMap.Add(variableDeclarator, name);
                    }
                }
            }

            var rewriter = new InlineMethodRewriter(replacementMap);

            return((TNode)rewriter.Visit(node));
        }
Ejemplo n.º 5
0
        private Dictionary <SyntaxNode, object> GetReplacementMap(SyntaxNode node, Dictionary <ISymbol, string> symbolMap)
        {
            var replacementMap = new Dictionary <SyntaxNode, object>();

            foreach (SyntaxNode descendant in node.DescendantNodesAndSelf(node.Span))
            {
                SyntaxKind kind = descendant.Kind();

                if (kind == SyntaxKind.IdentifierName)
                {
                    var identifierName = (IdentifierNameSyntax)descendant;

                    ISymbol symbol = DeclarationSemanticModel.GetSymbol(identifierName, CancellationToken);

                    if (symbol != null)
                    {
                        if (symbol is IParameterSymbol parameterSymbol)
                        {
                            foreach (ParameterInfo parameterInfo in ParameterInfos)
                            {
                                if (ParameterEquals(parameterInfo, parameterSymbol))
                                {
                                    ExpressionSyntax expression = parameterInfo.Expression;

                                    if (expression == null &&
                                        parameterInfo.ParameterSymbol.HasExplicitDefaultValue)
                                    {
                                        expression = parameterInfo.ParameterSymbol.GetDefaultValueMinimalSyntax(InvocationSemanticModel, Node.SpanStart);
                                    }

                                    replacementMap.Add(identifierName, expression);
                                    break;
                                }
                            }
                        }
                        else if (symbol.Kind == SymbolKind.TypeParameter)
                        {
                            var typeParameter = (ITypeParameterSymbol)symbol;

                            ImmutableArray <ITypeSymbol> typeArguments = TypeArguments;

                            if (typeArguments.Length > typeParameter.Ordinal)
                            {
                                replacementMap.Add(identifierName, typeArguments[typeParameter.Ordinal].ToMinimalTypeSyntax(DeclarationSemanticModel, identifierName.SpanStart));
                            }
                        }
                        else if (symbol.IsStatic &&
                                 !identifierName.IsParentKind(SyntaxKind.SimpleMemberAccessExpression, SyntaxKind.QualifiedName))
                        {
                            INamedTypeSymbol containingType = symbol.ContainingType;

                            if (containingType != null)
                            {
                                if (!NodeEnclosingType
                                    .BaseTypesAndSelf()
                                    .Any(f => f.Equals(containingType)))
                                {
                                    replacementMap.Add(identifierName, CSharpFactory.SimpleMemberAccessExpression(containingType.ToTypeSyntax().WithSimplifierAnnotation(), identifierName));
                                }
                            }
                            else if (symbol is ITypeSymbol typeSymbol)
                            {
                                replacementMap.Add(identifierName, typeSymbol.ToMinimalTypeSyntax(InvocationSemanticModel, Node.SpanStart));
                            }
                        }

                        if (symbolMap != null &&
                            symbolMap.TryGetValue(symbol, out string name))
                        {
                            replacementMap.Add(identifierName, IdentifierName(name));
                        }
                    }
                }
                else if (symbolMap != null)
                {
                    if (kind.Is(
                            SyntaxKind.VariableDeclarator,
                            SyntaxKind.SingleVariableDesignation,
                            SyntaxKind.Parameter,
                            SyntaxKind.TypeParameter,
                            SyntaxKind.ForEachStatement,
                            SyntaxKind.ForEachVariableStatement))
                    {
                        ISymbol symbol = DeclarationSemanticModel.GetDeclaredSymbol(descendant, CancellationToken);

                        Debug.Assert(symbol != null || (descendant as ForEachVariableStatementSyntax)?.Variable?.Kind() == SyntaxKind.TupleExpression, kind.ToString());

                        if (symbol != null &&
                            symbolMap.TryGetValue(symbol, out string name))
                        {
                            replacementMap.Add(descendant, name);
                        }
                    }
                }
            }

            return(replacementMap);

            bool ParameterEquals(in ParameterInfo parameterInfo, IParameterSymbol parameterSymbol2)
            {
                IParameterSymbol parameterSymbol = parameterInfo.ParameterSymbol;

                if (parameterSymbol.ContainingSymbol is IMethodSymbol methodSymbol)
                {
                    if (parameterInfo.IsThis ||
                        methodSymbol.MethodKind == MethodKind.ReducedExtension)
                    {
                        int ordinal = parameterSymbol.Ordinal;

                        if (methodSymbol.MethodKind == MethodKind.ReducedExtension)
                        {
                            ordinal++;
                        }

                        return(ordinal == parameterSymbol2.Ordinal &&
                               string.Equals(parameterSymbol.Name, parameterSymbol2.Name, StringComparison.Ordinal));
                    }
                }

                return(parameterSymbol.OriginalDefinition.Equals(parameterSymbol2));
            }
        }