|
| 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 "RefactoringActions.h" |
| 14 | +#include "Utils.h" |
| 15 | + |
| 16 | +using namespace swift::refactoring; |
| 17 | + |
| 18 | +namespace { |
| 19 | +class AddCodableContext { |
| 20 | + |
| 21 | + /// Declaration context |
| 22 | + DeclContext *DC; |
| 23 | + |
| 24 | + /// Start location of declaration context brace |
| 25 | + SourceLoc StartLoc; |
| 26 | + |
| 27 | + /// Array of all conformed protocols |
| 28 | + SmallVector<swift::ProtocolDecl *, 2> Protocols; |
| 29 | + |
| 30 | + /// Range of internal members in declaration |
| 31 | + DeclRange Range; |
| 32 | + |
| 33 | + bool conformsToCodableProtocol() { |
| 34 | + for (ProtocolDecl *Protocol : Protocols) { |
| 35 | + if (Protocol->getKnownProtocolKind() == KnownProtocolKind::Encodable || |
| 36 | + Protocol->getKnownProtocolKind() == KnownProtocolKind::Decodable) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + } |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | +public: |
| 44 | + AddCodableContext(NominalTypeDecl *Decl) |
| 45 | + : DC(Decl), StartLoc(Decl->getBraces().Start), |
| 46 | + Protocols(getAllProtocols(Decl)), Range(Decl->getMembers()){}; |
| 47 | + |
| 48 | + AddCodableContext(ExtensionDecl *Decl) |
| 49 | + : DC(Decl), StartLoc(Decl->getBraces().Start), |
| 50 | + Protocols(getAllProtocols(Decl->getExtendedNominal())), |
| 51 | + Range(Decl->getMembers()){}; |
| 52 | + |
| 53 | + AddCodableContext() : DC(nullptr), Protocols(), Range(nullptr, nullptr){}; |
| 54 | + |
| 55 | + static AddCodableContext |
| 56 | + getDeclarationContextFromInfo(ResolvedCursorInfoPtr Info); |
| 57 | + |
| 58 | + void printInsertionText(ResolvedCursorInfoPtr CursorInfo, SourceManager &SM, |
| 59 | + llvm::raw_ostream &OS); |
| 60 | + |
| 61 | + bool isValid() { return StartLoc.isValid() && conformsToCodableProtocol(); } |
| 62 | + |
| 63 | + SourceLoc getInsertStartLoc(); |
| 64 | +}; |
| 65 | + |
| 66 | +SourceLoc AddCodableContext::getInsertStartLoc() { |
| 67 | + SourceLoc MaxLoc = StartLoc; |
| 68 | + for (auto Mem : Range) { |
| 69 | + if (Mem->getEndLoc().getOpaquePointerValue() > |
| 70 | + MaxLoc.getOpaquePointerValue()) { |
| 71 | + MaxLoc = Mem->getEndLoc(); |
| 72 | + } |
| 73 | + } |
| 74 | + return MaxLoc; |
| 75 | +} |
| 76 | + |
| 77 | +/// Walks an AST and prints the synthesized Codable implementation. |
| 78 | +class SynthesizedCodablePrinter : public ASTWalker { |
| 79 | +private: |
| 80 | + ASTPrinter &Printer; |
| 81 | + |
| 82 | +public: |
| 83 | + SynthesizedCodablePrinter(ASTPrinter &Printer) : Printer(Printer) {} |
| 84 | + |
| 85 | + MacroWalking getMacroWalkingBehavior() const override { |
| 86 | + return MacroWalking::Arguments; |
| 87 | + } |
| 88 | + |
| 89 | + PreWalkAction walkToDeclPre(Decl *D) override { |
| 90 | + auto *VD = dyn_cast<ValueDecl>(D); |
| 91 | + if (!VD) |
| 92 | + return Action::SkipChildren(); |
| 93 | + |
| 94 | + if (!VD->isSynthesized()) { |
| 95 | + return Action::Continue(); |
| 96 | + } |
| 97 | + SmallString<32> Scratch; |
| 98 | + auto name = VD->getName().getString(Scratch); |
| 99 | + // Print all synthesized enums, |
| 100 | + // since Codable can synthesize multiple enums (for associated values). |
| 101 | + auto shouldPrint = |
| 102 | + isa<EnumDecl>(VD) || name == "init(from:)" || name == "encode(to:)"; |
| 103 | + if (!shouldPrint) { |
| 104 | + // Some other synthesized decl that we don't want to print. |
| 105 | + return Action::SkipChildren(); |
| 106 | + } |
| 107 | + |
| 108 | + Printer.printNewline(); |
| 109 | + |
| 110 | + if (auto enumDecl = dyn_cast<EnumDecl>(D)) { |
| 111 | + // Manually print enum here, since we don't want to print synthesized |
| 112 | + // functions. |
| 113 | + Printer << "enum " << enumDecl->getNameStr(); |
| 114 | + PrintOptions Options; |
| 115 | + Options.PrintSpaceBeforeInheritance = false; |
| 116 | + enumDecl->printInherited(Printer, Options); |
| 117 | + Printer << " {"; |
| 118 | + for (Decl *EC : enumDecl->getAllElements()) { |
| 119 | + Printer.printNewline(); |
| 120 | + Printer << " "; |
| 121 | + EC->print(Printer, Options); |
| 122 | + } |
| 123 | + Printer.printNewline(); |
| 124 | + Printer << "}"; |
| 125 | + return Action::SkipChildren(); |
| 126 | + } |
| 127 | + |
| 128 | + PrintOptions Options; |
| 129 | + Options.SynthesizeSugarOnTypes = true; |
| 130 | + Options.FunctionDefinitions = true; |
| 131 | + Options.VarInitializers = true; |
| 132 | + Options.PrintExprs = true; |
| 133 | + Options.TypeDefinitions = true; |
| 134 | + Options.ExcludeAttrList.push_back(DAK_HasInitialValue); |
| 135 | + |
| 136 | + Printer.printNewline(); |
| 137 | + D->print(Printer, Options); |
| 138 | + |
| 139 | + return Action::SkipChildren(); |
| 140 | + } |
| 141 | +}; |
| 142 | + |
| 143 | +void AddCodableContext::printInsertionText(ResolvedCursorInfoPtr CursorInfo, |
| 144 | + SourceManager &SM, |
| 145 | + llvm::raw_ostream &OS) { |
| 146 | + StringRef ExtraIndent; |
| 147 | + StringRef CurrentIndent = |
| 148 | + Lexer::getIndentationForLine(SM, getInsertStartLoc(), &ExtraIndent); |
| 149 | + std::string Indent; |
| 150 | + if (getInsertStartLoc() == StartLoc) { |
| 151 | + Indent = (CurrentIndent + ExtraIndent).str(); |
| 152 | + } else { |
| 153 | + Indent = CurrentIndent.str(); |
| 154 | + } |
| 155 | + |
| 156 | + ExtraIndentStreamPrinter Printer(OS, Indent); |
| 157 | + Printer.printNewline(); |
| 158 | + SynthesizedCodablePrinter Walker(Printer); |
| 159 | + DC->getAsDecl()->walk(Walker); |
| 160 | +} |
| 161 | + |
| 162 | +AddCodableContext |
| 163 | +AddCodableContext::getDeclarationContextFromInfo(ResolvedCursorInfoPtr Info) { |
| 164 | + auto ValueRefInfo = dyn_cast<ResolvedValueRefCursorInfo>(Info); |
| 165 | + if (!ValueRefInfo) { |
| 166 | + return AddCodableContext(); |
| 167 | + } |
| 168 | + if (!ValueRefInfo->isRef()) { |
| 169 | + if (auto *NomDecl = dyn_cast<NominalTypeDecl>(ValueRefInfo->getValueD())) { |
| 170 | + return AddCodableContext(NomDecl); |
| 171 | + } |
| 172 | + } |
| 173 | + // TODO: support extensions |
| 174 | + // (would need to get synthesized nodes from the main decl, |
| 175 | + // and only if it's in the same file?) |
| 176 | + return AddCodableContext(); |
| 177 | +} |
| 178 | +} // namespace |
| 179 | + |
| 180 | +bool RefactoringActionAddExplicitCodableImplementation::isApplicable( |
| 181 | + ResolvedCursorInfoPtr Tok, DiagnosticEngine &Diag) { |
| 182 | + return AddCodableContext::getDeclarationContextFromInfo(Tok).isValid(); |
| 183 | +} |
| 184 | + |
| 185 | +bool RefactoringActionAddExplicitCodableImplementation::performChange() { |
| 186 | + auto Context = AddCodableContext::getDeclarationContextFromInfo(CursorInfo); |
| 187 | + |
| 188 | + SmallString<64> Buffer; |
| 189 | + llvm::raw_svector_ostream OS(Buffer); |
| 190 | + Context.printInsertionText(CursorInfo, SM, OS); |
| 191 | + |
| 192 | + EditConsumer.insertAfter(SM, Context.getInsertStartLoc(), OS.str()); |
| 193 | + return false; |
| 194 | +} |
0 commit comments