예제 #1
0
        private static BlockSyntax WithDeclarationMoved(BlockSyntax scope, LocalDeclarationStatementSyntax declaration, VariableDeclaratorSyntax v, ISemanticModel model, Assumptions assume, CancellationToken cancellationToken)
        {
            Func<SyntaxNode, bool> accesses = s => s.DescendantNodes()
                                                    .OfType<IdentifierNameSyntax>()
                                                    .Where(e => e.Identifier.ValueText == v.Identifier.ValueText)
                                                    .Where(e => model.GetSymbolInfo(e).Symbol == model.GetDeclaredSymbol(v))
                                                    .Any();

            var isOutermostScope = scope.Statements.Contains(declaration);
            var scopeStatements = isOutermostScope
                                ? scope.Statements.SkipWhile(e => e != declaration).Skip(1).ToArray()
                                : scope.Statements.ToArray();

            var statementAccesses = scope.Statements.Where(s => accesses(s)).ToArray();
            if (statementAccesses.Length == 0) return null; //unused local, no change
            var firstAccess = statementAccesses.FirstOrDefault();
            var insertBeforeTarget = firstAccess;
            if (v.Initializer != null && v.Initializer.Value.IsConst(model) != true) {
                insertBeforeTarget = scopeStatements
                                    .TakeWhile(e => e != firstAccess)
                                    .SkipWhile(e => e.HasSideEffects(model, assume) == false)
                                    .Append(firstAccess)
                                    .FirstOrDefault();
            }

            if (statementAccesses.Length == 1 && insertBeforeTarget == firstAccess) {
                if (firstAccess is BlockSyntax) {
                    var reducedScope = WithDeclarationMoved((BlockSyntax)firstAccess, declaration, v, model, assume, cancellationToken);
                    return scope.WithStatements(scope.Statements.Replace(firstAccess, reducedScope));
                }
                if (firstAccess is IfStatementSyntax) {
                    var s = (IfStatementSyntax)firstAccess;
                    if (!accesses(s.Condition)) {
                        if (!accesses(s.Statement) && s.Else.Statement is BlockSyntax) {
                            return scope.WithStatements(
                                scope.Statements.Replace(
                                    s,
                                    s.WithElse(
                                        s.Else.WithStatement(
                                            WithDeclarationMoved(
                                                (BlockSyntax)s.Else.Statement,
                                                declaration,
                                                v,
                                                model,
                                                assume,
                                                cancellationToken)))));
                        } else if (s.Statement is BlockSyntax && (s.Else == null || !accesses(s.Else.Statement))) {
                            return scope.WithStatements(
                                scope.Statements.Replace(
                                    s,
                                    s.WithStatement(
                                        WithDeclarationMoved(
                                            (BlockSyntax)s.Statement,
                                            declaration,
                                            v,
                                            model,
                                            assume,
                                            cancellationToken))));
                        }
                    }
                }
            }
            if (isOutermostScope && scopeStatements.TakeWhile(e => e != insertBeforeTarget).Where(e => !(e is LocalDeclarationStatementSyntax)).None())
                return null; //minimal scope already

            var newDeclaration = declaration.WithDeclaration(declaration.Declaration.WithVariables(v.SepList1()));
            return scope.WithStatements(scope.Statements.InsertBefore(insertBeforeTarget, newDeclaration).List());
        }