|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 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 "ContextFinder.h" |
| 14 | +#include "RefactoringActions.h" |
| 15 | +#include "swift/AST/DiagnosticsRefactoring.h" |
| 16 | +#include "swift/AST/Pattern.h" |
| 17 | +#include "swift/AST/Stmt.h" |
| 18 | + |
| 19 | +using namespace swift::refactoring; |
| 20 | + |
| 21 | +static EnumDecl *getEnumDeclFromSwitchStmt(SwitchStmt *SwitchS) { |
| 22 | + if (auto SubjectTy = SwitchS->getSubjectExpr()->getType()) { |
| 23 | + // FIXME: Support more complex subject like '(Enum1, Enum2)'. |
| 24 | + return dyn_cast_or_null<EnumDecl>(SubjectTy->getAnyNominal()); |
| 25 | + } |
| 26 | + return nullptr; |
| 27 | +} |
| 28 | + |
| 29 | +static bool performCasesExpansionInSwitchStmt(SwitchStmt *SwitchS, |
| 30 | + DiagnosticEngine &DiagEngine, |
| 31 | + SourceLoc ExpandedStmtLoc, |
| 32 | + EditorConsumerInsertStream &OS) { |
| 33 | + // Assume enum elements are not handled in the switch statement. |
| 34 | + auto EnumDecl = getEnumDeclFromSwitchStmt(SwitchS); |
| 35 | + assert(EnumDecl); |
| 36 | + llvm::DenseSet<EnumElementDecl *> UnhandledElements; |
| 37 | + EnumDecl->getAllElements(UnhandledElements); |
| 38 | + for (auto Current : SwitchS->getCases()) { |
| 39 | + if (Current->isDefault()) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + // For each handled enum element, remove it from the bucket. |
| 43 | + for (auto Item : Current->getCaseLabelItems()) { |
| 44 | + if (auto *EEP = dyn_cast_or_null<EnumElementPattern>(Item.getPattern())) { |
| 45 | + UnhandledElements.erase(EEP->getElementDecl()); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // If all enum elements are handled in the switch statement, issue error. |
| 51 | + if (UnhandledElements.empty()) { |
| 52 | + DiagEngine.diagnose(ExpandedStmtLoc, diag::no_remaining_cases); |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + printEnumElementsAsCases(UnhandledElements, OS); |
| 57 | + return false; |
| 58 | +} |
| 59 | + |
| 60 | +// Finds SwitchStmt that contains given CaseStmt. |
| 61 | +static SwitchStmt *findEnclosingSwitchStmt(CaseStmt *CS, SourceFile *SF, |
| 62 | + DiagnosticEngine &DiagEngine) { |
| 63 | + auto IsSwitch = [](ASTNode Node) { |
| 64 | + return Node.is<Stmt *>() && |
| 65 | + Node.get<Stmt *>()->getKind() == StmtKind::Switch; |
| 66 | + }; |
| 67 | + ContextFinder Finder(*SF, CS, IsSwitch); |
| 68 | + Finder.resolve(); |
| 69 | + |
| 70 | + // If failed to find the switch statement, issue error. |
| 71 | + if (Finder.getContexts().empty()) { |
| 72 | + DiagEngine.diagnose(CS->getStartLoc(), diag::no_parent_switch); |
| 73 | + return nullptr; |
| 74 | + } |
| 75 | + auto *SwitchS = |
| 76 | + static_cast<SwitchStmt *>(Finder.getContexts().back().get<Stmt *>()); |
| 77 | + // Make sure that CaseStmt is included in switch that was found. |
| 78 | + auto Cases = SwitchS->getCases(); |
| 79 | + auto Default = std::find(Cases.begin(), Cases.end(), CS); |
| 80 | + if (Default == Cases.end()) { |
| 81 | + DiagEngine.diagnose(CS->getStartLoc(), diag::no_parent_switch); |
| 82 | + return nullptr; |
| 83 | + } |
| 84 | + return SwitchS; |
| 85 | +} |
| 86 | + |
| 87 | +bool RefactoringActionExpandDefault::isApplicable( |
| 88 | + ResolvedCursorInfoPtr CursorInfo, DiagnosticEngine &Diag) { |
| 89 | + auto Exit = [&](bool Applicable) { |
| 90 | + if (!Applicable) |
| 91 | + Diag.diagnose(SourceLoc(), diag::invalid_default_location); |
| 92 | + return Applicable; |
| 93 | + }; |
| 94 | + auto StmtStartInfo = dyn_cast<ResolvedStmtStartCursorInfo>(CursorInfo); |
| 95 | + if (!StmtStartInfo) |
| 96 | + return Exit(false); |
| 97 | + if (auto *CS = dyn_cast<CaseStmt>(StmtStartInfo->getTrailingStmt())) { |
| 98 | + auto EnclosingSwitchStmt = |
| 99 | + findEnclosingSwitchStmt(CS, CursorInfo->getSourceFile(), Diag); |
| 100 | + if (!EnclosingSwitchStmt) |
| 101 | + return false; |
| 102 | + auto EnumD = getEnumDeclFromSwitchStmt(EnclosingSwitchStmt); |
| 103 | + auto IsApplicable = CS->isDefault() && EnumD != nullptr; |
| 104 | + return IsApplicable; |
| 105 | + } |
| 106 | + return Exit(false); |
| 107 | +} |
| 108 | + |
| 109 | +bool RefactoringActionExpandDefault::performChange() { |
| 110 | + // If we've not seen the default statement inside the switch statement, issue |
| 111 | + // error. |
| 112 | + auto StmtStartInfo = cast<ResolvedStmtStartCursorInfo>(CursorInfo); |
| 113 | + auto *CS = static_cast<CaseStmt *>(StmtStartInfo->getTrailingStmt()); |
| 114 | + auto *SwitchS = findEnclosingSwitchStmt(CS, TheFile, DiagEngine); |
| 115 | + assert(SwitchS); |
| 116 | + EditorConsumerInsertStream OS( |
| 117 | + EditConsumer, SM, |
| 118 | + Lexer::getCharSourceRangeFromSourceRange(SM, CS->getLabelItemsRange())); |
| 119 | + return performCasesExpansionInSwitchStmt(SwitchS, DiagEngine, |
| 120 | + CS->getStartLoc(), OS); |
| 121 | +} |
| 122 | + |
| 123 | +bool RefactoringActionExpandSwitchCases::isApplicable( |
| 124 | + ResolvedCursorInfoPtr CursorInfo, DiagnosticEngine &DiagEngine) { |
| 125 | + auto StmtStartInfo = dyn_cast<ResolvedStmtStartCursorInfo>(CursorInfo); |
| 126 | + if (!StmtStartInfo || !StmtStartInfo->getTrailingStmt()) |
| 127 | + return false; |
| 128 | + if (auto *Switch = dyn_cast<SwitchStmt>(StmtStartInfo->getTrailingStmt())) { |
| 129 | + return getEnumDeclFromSwitchStmt(Switch); |
| 130 | + } |
| 131 | + return false; |
| 132 | +} |
| 133 | + |
| 134 | +bool RefactoringActionExpandSwitchCases::performChange() { |
| 135 | + auto StmtStartInfo = cast<ResolvedStmtStartCursorInfo>(CursorInfo); |
| 136 | + auto *SwitchS = dyn_cast<SwitchStmt>(StmtStartInfo->getTrailingStmt()); |
| 137 | + assert(SwitchS); |
| 138 | + |
| 139 | + auto InsertRange = CharSourceRange(); |
| 140 | + auto Cases = SwitchS->getCases(); |
| 141 | + auto Default = std::find_if(Cases.begin(), Cases.end(), |
| 142 | + [](CaseStmt *Stmt) { return Stmt->isDefault(); }); |
| 143 | + if (Default != Cases.end()) { |
| 144 | + auto DefaultRange = (*Default)->getLabelItemsRange(); |
| 145 | + InsertRange = Lexer::getCharSourceRangeFromSourceRange(SM, DefaultRange); |
| 146 | + } else { |
| 147 | + auto RBraceLoc = SwitchS->getRBraceLoc(); |
| 148 | + InsertRange = CharSourceRange(SM, RBraceLoc, RBraceLoc); |
| 149 | + } |
| 150 | + EditorConsumerInsertStream OS(EditConsumer, SM, InsertRange); |
| 151 | + if (SM.getLineAndColumnInBuffer(SwitchS->getLBraceLoc()).first == |
| 152 | + SM.getLineAndColumnInBuffer(SwitchS->getRBraceLoc()).first) { |
| 153 | + OS << "\n"; |
| 154 | + } |
| 155 | + auto Result = performCasesExpansionInSwitchStmt(SwitchS, DiagEngine, |
| 156 | + SwitchS->getStartLoc(), OS); |
| 157 | + return Result; |
| 158 | +} |
0 commit comments