|
| 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 | + |
| 15 | +using namespace swift::refactoring; |
| 16 | + |
| 17 | +namespace { |
| 18 | +struct MemberwiseParameter { |
| 19 | + CharSourceRange NameRange; |
| 20 | + Type MemberType; |
| 21 | + Expr *DefaultExpr; |
| 22 | + |
| 23 | + MemberwiseParameter(CharSourceRange nameRange, Type type, Expr *initialExpr) |
| 24 | + : NameRange(nameRange), MemberType(type), DefaultExpr(initialExpr) {} |
| 25 | +}; |
| 26 | +} // namespace |
| 27 | + |
| 28 | +static void generateMemberwiseInit(SourceEditConsumer &EditConsumer, |
| 29 | + SourceManager &SM, |
| 30 | + ArrayRef<MemberwiseParameter> memberVector, |
| 31 | + SourceLoc targetLocation) { |
| 32 | + |
| 33 | + EditConsumer.accept(SM, targetLocation, "\ninternal init("); |
| 34 | + auto insertMember = [&SM](const MemberwiseParameter &memberData, |
| 35 | + raw_ostream &OS, bool wantsSeparator) { |
| 36 | + { |
| 37 | + OS << SM.extractText(memberData.NameRange) << ": "; |
| 38 | + // Unconditionally print '@escaping' if we print out a function type - |
| 39 | + // the assignments we generate below will escape this parameter. |
| 40 | + if (isa<AnyFunctionType>(memberData.MemberType->getCanonicalType())) { |
| 41 | + OS << "@" << TypeAttributes::getAttrName(TAK_escaping) << " "; |
| 42 | + } |
| 43 | + OS << memberData.MemberType.getString(); |
| 44 | + } |
| 45 | + |
| 46 | + bool HasAddedDefault = false; |
| 47 | + if (auto *expr = memberData.DefaultExpr) { |
| 48 | + if (expr->getSourceRange().isValid()) { |
| 49 | + auto range = Lexer::getCharSourceRangeFromSourceRange( |
| 50 | + SM, expr->getSourceRange()); |
| 51 | + OS << " = " << SM.extractText(range); |
| 52 | + HasAddedDefault = true; |
| 53 | + } |
| 54 | + } |
| 55 | + if (!HasAddedDefault && memberData.MemberType->isOptional()) { |
| 56 | + OS << " = nil"; |
| 57 | + } |
| 58 | + |
| 59 | + if (wantsSeparator) { |
| 60 | + OS << ", "; |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + // Process the initial list of members, inserting commas as appropriate. |
| 65 | + std::string Buffer; |
| 66 | + llvm::raw_string_ostream OS(Buffer); |
| 67 | + for (const auto &memberData : llvm::enumerate(memberVector)) { |
| 68 | + bool wantsSeparator = (memberData.index() != memberVector.size() - 1); |
| 69 | + insertMember(memberData.value(), OS, wantsSeparator); |
| 70 | + } |
| 71 | + |
| 72 | + // Synthesize the body. |
| 73 | + OS << ") {\n"; |
| 74 | + for (auto &member : memberVector) { |
| 75 | + // self.<property> = <property> |
| 76 | + auto name = SM.extractText(member.NameRange); |
| 77 | + OS << "self." << name << " = " << name << "\n"; |
| 78 | + } |
| 79 | + OS << "}\n"; |
| 80 | + |
| 81 | + // Accept the entire edit. |
| 82 | + EditConsumer.accept(SM, targetLocation, OS.str()); |
| 83 | +} |
| 84 | + |
| 85 | +static SourceLoc |
| 86 | +collectMembersForInit(ResolvedCursorInfoPtr CursorInfo, |
| 87 | + SmallVectorImpl<MemberwiseParameter> &memberVector) { |
| 88 | + auto ValueRefInfo = dyn_cast<ResolvedValueRefCursorInfo>(CursorInfo); |
| 89 | + if (!ValueRefInfo || !ValueRefInfo->getValueD()) |
| 90 | + return SourceLoc(); |
| 91 | + |
| 92 | + NominalTypeDecl *nominalDecl = |
| 93 | + dyn_cast<NominalTypeDecl>(ValueRefInfo->getValueD()); |
| 94 | + if (!nominalDecl || nominalDecl->getStoredProperties().empty() || |
| 95 | + ValueRefInfo->isRef()) { |
| 96 | + return SourceLoc(); |
| 97 | + } |
| 98 | + |
| 99 | + SourceLoc bracesStart = nominalDecl->getBraces().Start; |
| 100 | + if (!bracesStart.isValid()) |
| 101 | + return SourceLoc(); |
| 102 | + |
| 103 | + SourceLoc targetLocation = bracesStart.getAdvancedLoc(1); |
| 104 | + if (!targetLocation.isValid()) |
| 105 | + return SourceLoc(); |
| 106 | + |
| 107 | + SourceManager &SM = nominalDecl->getASTContext().SourceMgr; |
| 108 | + |
| 109 | + for (auto member : nominalDecl->getMemberwiseInitProperties()) { |
| 110 | + auto varDecl = dyn_cast<VarDecl>(member); |
| 111 | + if (!varDecl) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + if (varDecl->getAttrs().hasAttribute<LazyAttr>()) { |
| 115 | + // Exclude lazy members from the memberwise initializer. This is |
| 116 | + // inconsistent with the implicitly synthesized memberwise initializer but |
| 117 | + // we think it makes more sense because otherwise the lazy variable's |
| 118 | + // initializer gets evaluated eagerly. |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + auto patternBinding = varDecl->getParentPatternBinding(); |
| 123 | + if (!patternBinding) |
| 124 | + continue; |
| 125 | + |
| 126 | + const auto i = patternBinding->getPatternEntryIndexForVarDecl(varDecl); |
| 127 | + Expr *defaultInit = nullptr; |
| 128 | + if (patternBinding->isExplicitlyInitialized(i) || |
| 129 | + patternBinding->isDefaultInitializable()) { |
| 130 | + defaultInit = patternBinding->getOriginalInit(i); |
| 131 | + } |
| 132 | + |
| 133 | + auto NameRange = |
| 134 | + Lexer::getCharSourceRangeFromSourceRange(SM, varDecl->getNameLoc()); |
| 135 | + memberVector.emplace_back(NameRange, varDecl->getTypeInContext(), |
| 136 | + defaultInit); |
| 137 | + } |
| 138 | + |
| 139 | + return targetLocation; |
| 140 | +} |
| 141 | + |
| 142 | +bool RefactoringActionMemberwiseInitLocalRefactoring::isApplicable( |
| 143 | + ResolvedCursorInfoPtr Tok, DiagnosticEngine &Diag) { |
| 144 | + |
| 145 | + SmallVector<MemberwiseParameter, 8> memberVector; |
| 146 | + return collectMembersForInit(Tok, memberVector).isValid(); |
| 147 | +} |
| 148 | + |
| 149 | +bool RefactoringActionMemberwiseInitLocalRefactoring::performChange() { |
| 150 | + |
| 151 | + SmallVector<MemberwiseParameter, 8> memberVector; |
| 152 | + SourceLoc targetLocation = collectMembersForInit(CursorInfo, memberVector); |
| 153 | + if (targetLocation.isInvalid()) |
| 154 | + return true; |
| 155 | + |
| 156 | + generateMemberwiseInit(EditConsumer, SM, memberVector, targetLocation); |
| 157 | + |
| 158 | + return false; |
| 159 | +} |
0 commit comments