|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "RefactoringActions.h" |
| 14 | + |
| 15 | +using namespace swift::refactoring; |
| 16 | + |
| 17 | +static std::unique_ptr<llvm::SetVector<Expr *>> |
| 18 | +findConcatenatedExpressions(const ResolvedRangeInfo &Info, ASTContext &Ctx) { |
| 19 | + Expr *E = nullptr; |
| 20 | + |
| 21 | + switch (Info.Kind) { |
| 22 | + case RangeKind::SingleExpression: |
| 23 | + E = Info.ContainedNodes[0].get<Expr *>(); |
| 24 | + break; |
| 25 | + case RangeKind::PartOfExpression: |
| 26 | + E = Info.CommonExprParent; |
| 27 | + break; |
| 28 | + default: |
| 29 | + return nullptr; |
| 30 | + } |
| 31 | + |
| 32 | + assert(E); |
| 33 | + |
| 34 | + struct StringInterpolationExprFinder : public SourceEntityWalker { |
| 35 | + std::unique_ptr<llvm::SetVector<Expr *>> Bucket = |
| 36 | + std::make_unique<llvm::SetVector<Expr *>>(); |
| 37 | + ASTContext &Ctx; |
| 38 | + |
| 39 | + bool IsValidInterpolation = true; |
| 40 | + StringInterpolationExprFinder(ASTContext &Ctx) : Ctx(Ctx) {} |
| 41 | + |
| 42 | + bool isConcatenationExpr(DeclRefExpr *Expr) { |
| 43 | + if (!Expr) |
| 44 | + return false; |
| 45 | + auto *FD = dyn_cast<FuncDecl>(Expr->getDecl()); |
| 46 | + if (FD == nullptr || |
| 47 | + (FD != Ctx.getPlusFunctionOnString() && |
| 48 | + FD != Ctx.getPlusFunctionOnRangeReplaceableCollection())) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + bool walkToExprPre(Expr *E) override { |
| 55 | + if (E->isImplicit()) |
| 56 | + return true; |
| 57 | + // FIXME: we should have ErrorType instead of null. |
| 58 | + if (E->getType().isNull()) |
| 59 | + return true; |
| 60 | + |
| 61 | + // Only binary concatenation operators should exist in expression |
| 62 | + if (E->getKind() == ExprKind::Binary) { |
| 63 | + auto *BE = dyn_cast<BinaryExpr>(E); |
| 64 | + auto *OperatorDeclRef = BE->getSemanticFn()->getMemberOperatorRef(); |
| 65 | + if (!(isConcatenationExpr(OperatorDeclRef) && |
| 66 | + E->getType()->isString())) { |
| 67 | + IsValidInterpolation = false; |
| 68 | + return false; |
| 69 | + } |
| 70 | + return true; |
| 71 | + } |
| 72 | + // Everything that evaluates to string should be gathered. |
| 73 | + if (E->getType()->isString()) { |
| 74 | + Bucket->insert(E); |
| 75 | + return false; |
| 76 | + } |
| 77 | + if (auto *DR = dyn_cast<DeclRefExpr>(E)) { |
| 78 | + // Checks whether all function references in expression are |
| 79 | + // concatenations. |
| 80 | + auto *FD = dyn_cast<FuncDecl>(DR->getDecl()); |
| 81 | + auto IsConcatenation = isConcatenationExpr(DR); |
| 82 | + if (FD && IsConcatenation) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | + // There was non-expected expression, it's not valid interpolation then. |
| 87 | + IsValidInterpolation = false; |
| 88 | + return false; |
| 89 | + } |
| 90 | + } Walker(Ctx); |
| 91 | + Walker.walk(E); |
| 92 | + |
| 93 | + // There should be two or more expressions to convert. |
| 94 | + if (!Walker.IsValidInterpolation || Walker.Bucket->size() < 2) |
| 95 | + return nullptr; |
| 96 | + |
| 97 | + return std::move(Walker.Bucket); |
| 98 | +} |
| 99 | + |
| 100 | +static void interpolatedExpressionForm(Expr *E, SourceManager &SM, |
| 101 | + llvm::raw_ostream &OS) { |
| 102 | + if (auto *Literal = dyn_cast<StringLiteralExpr>(E)) { |
| 103 | + OS << Literal->getValue(); |
| 104 | + return; |
| 105 | + } |
| 106 | + auto ExpStr = |
| 107 | + Lexer::getCharSourceRangeFromSourceRange(SM, E->getSourceRange()) |
| 108 | + .str() |
| 109 | + .str(); |
| 110 | + if (isa<InterpolatedStringLiteralExpr>(E)) { |
| 111 | + ExpStr.erase(0, 1); |
| 112 | + ExpStr.pop_back(); |
| 113 | + OS << ExpStr; |
| 114 | + return; |
| 115 | + } |
| 116 | + OS << "\\(" << ExpStr << ")"; |
| 117 | +} |
| 118 | + |
| 119 | +bool RefactoringActionConvertStringsConcatenationToInterpolation::isApplicable( |
| 120 | + const ResolvedRangeInfo &Info, DiagnosticEngine &Diag) { |
| 121 | + auto RangeContext = Info.RangeContext; |
| 122 | + if (RangeContext) { |
| 123 | + auto &Ctx = Info.RangeContext->getASTContext(); |
| 124 | + return findConcatenatedExpressions(Info, Ctx) != nullptr; |
| 125 | + } |
| 126 | + return false; |
| 127 | +} |
| 128 | + |
| 129 | +bool RefactoringActionConvertStringsConcatenationToInterpolation:: |
| 130 | + performChange() { |
| 131 | + auto Expressions = findConcatenatedExpressions(RangeInfo, Ctx); |
| 132 | + if (!Expressions) |
| 133 | + return true; |
| 134 | + EditorConsumerInsertStream OS(EditConsumer, SM, RangeInfo.ContentRange); |
| 135 | + OS << "\""; |
| 136 | + for (auto It = Expressions->begin(); It != Expressions->end(); ++It) { |
| 137 | + interpolatedExpressionForm(*It, SM, OS); |
| 138 | + } |
| 139 | + OS << "\""; |
| 140 | + return false; |
| 141 | +} |
0 commit comments