Skip to content

Commit 6498382

Browse files
committed
[NFC] Refactor getRenameDecl to use the request evaluator
Move `getRenameDecl` from `PrintAsObjC` into its own request so that other components can re-use this functionality.
1 parent 5a5b1af commit 6498382

File tree

14 files changed

+185
-139
lines changed

14 files changed

+185
-139
lines changed

include/swift/AST/Module.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,6 @@ class ModuleDecl : public DeclContext, public TypeDecl {
758758
void collectBasicSourceFileInfo(
759759
llvm::function_ref<void(const BasicSourceFileInfo &)> callback) const;
760760

761-
public:
762761
/// Retrieve a fingerprint value that summarizes the contents of this module.
763762
///
764763
/// This interface hash a of a module is guaranteed to change if the interface
@@ -771,6 +770,15 @@ class ModuleDecl : public DeclContext, public TypeDecl {
771770
/// contents have been made.
772771
Fingerprint getFingerprint() const;
773772

773+
/// Returns an approximation of whether the given module could be
774+
/// redistributed and consumed by external clients.
775+
///
776+
/// FIXME: The scope of this computation should be limited entirely to
777+
/// RenamedDeclRequest. Unfortunately, it has been co-opted to support the
778+
/// \c SerializeOptionsForDebugging hack. Once this information can be
779+
/// transferred from module files to the dSYMs, remove this.
780+
bool isExternallyConsumed() const;
781+
774782
SourceRange getSourceRange() const { return SourceRange(); }
775783

776784
static bool classof(const DeclContext *DC) {

include/swift/AST/TypeCheckRequests.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,6 +2986,23 @@ class AsyncAlternativeRequest
29862986
bool isCached() const { return true; }
29872987
};
29882988

2989+
class RenamedDeclRequest
2990+
: public SimpleRequest<RenamedDeclRequest,
2991+
ValueDecl *(const ValueDecl *, const AvailableAttr *),
2992+
RequestFlags::Cached> {
2993+
public:
2994+
using SimpleRequest::SimpleRequest;
2995+
2996+
private:
2997+
friend SimpleRequest;
2998+
2999+
ValueDecl *evaluate(Evaluator &evaluator, const ValueDecl *attached,
3000+
const AvailableAttr *attr) const;
3001+
3002+
public:
3003+
bool isCached() const { return true; }
3004+
};
3005+
29893006
void simple_display(llvm::raw_ostream &out, Type value);
29903007
void simple_display(llvm::raw_ostream &out, const TypeRepr *TyR);
29913008
void simple_display(llvm::raw_ostream &out, ImplicitMemberAction action);

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,6 @@ SWIFT_REQUEST(TypeChecker, GetImplicitSendableRequest,
332332
SWIFT_REQUEST(TypeChecker, AsyncAlternativeRequest,
333333
AbstractFunctionDecl *(AbstractFunctionDecl *),
334334
Cached, NoLocationInfo)
335+
SWIFT_REQUEST(TypeChecker, RenamedDeclRequest,
336+
ValueDecl *(const ValueDecl *),
337+
Cached, NoLocationInfo)

include/swift/Frontend/Frontend.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,6 @@ class CompilerInvocation {
400400
SerializationOptions
401401
computeSerializationOptions(const SupplementaryOutputPaths &outs,
402402
const ModuleDecl *module) const;
403-
404-
/// Returns an approximation of whether the given module could be
405-
/// redistributed and consumed by external clients.
406-
///
407-
/// FIXME: The scope of this computation should be limited entirely to
408-
/// PrintAsObjC. Unfortunately, it has been co-opted to support the
409-
/// \c SerializeOptionsForDebugging hack. Once this information can be
410-
/// transferred from module files to the dSYMs, remove this.
411-
bool isModuleExternallyConsumed(const ModuleDecl *mod) const;
412403
};
413404

414405
/// A class which manages the state and execution of the compiler.

include/swift/PrintAsObjC/PrintAsObjC.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ namespace swift {
2525
/// header.
2626
///
2727
/// Returns true on error.
28-
bool printAsObjC(raw_ostream &out, ModuleDecl *M, StringRef bridgingHeader,
29-
AccessLevel minRequiredAccess);
28+
bool printAsObjC(raw_ostream &out, ModuleDecl *M, StringRef bridgingHeader);
3029
}
3130

3231
#endif

lib/AST/Module.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,32 @@ Fingerprint ModuleDecl::getFingerprint() const {
16471647
return Fingerprint{std::move(hasher)};
16481648
}
16491649

1650+
bool ModuleDecl::isExternallyConsumed() const {
1651+
// Modules for executables aren't expected to be consumed by other modules.
1652+
// This picks up all kinds of entrypoints, including script mode,
1653+
// @UIApplicationMain and @NSApplicationMain.
1654+
if (hasEntryPoint()) {
1655+
return false;
1656+
}
1657+
1658+
// If an implicit Objective-C header was needed to construct this module, it
1659+
// must be the product of a library target.
1660+
if (!getImplicitImportInfo().BridgingHeaderPath.empty()) {
1661+
return false;
1662+
}
1663+
1664+
// App extensions are special beasts because they build without entrypoints
1665+
// like library targets, but they behave like executable targets because
1666+
// their associated modules are not suitable for distribution.
1667+
if (getASTContext().LangOpts.EnableAppExtensionRestrictions) {
1668+
return false;
1669+
}
1670+
1671+
// FIXME: This is still a lousy approximation of whether the module file will
1672+
// be externally consumed.
1673+
return true;
1674+
}
1675+
16501676
//===----------------------------------------------------------------------===//
16511677
// Cross-Import Overlays
16521678
//===----------------------------------------------------------------------===//

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,30 +2013,3 @@ CompilerInvocation::setUpInputForSILTool(
20132013
}
20142014
return fileBufOrErr;
20152015
}
2016-
2017-
bool CompilerInvocation::isModuleExternallyConsumed(
2018-
const ModuleDecl *mod) const {
2019-
// Modules for executables aren't expected to be consumed by other modules.
2020-
// This picks up all kinds of entrypoints, including script mode,
2021-
// @UIApplicationMain and @NSApplicationMain.
2022-
if (mod->hasEntryPoint()) {
2023-
return false;
2024-
}
2025-
2026-
// If an implicit Objective-C header was needed to construct this module, it
2027-
// must be the product of a library target.
2028-
if (!getFrontendOptions().ImplicitObjCHeaderPath.empty()) {
2029-
return false;
2030-
}
2031-
2032-
// App extensions are special beasts because they build without entrypoints
2033-
// like library targets, but they behave like executable targets because
2034-
// their associated modules are not suitable for distribution.
2035-
if (mod->getASTContext().LangOpts.EnableAppExtensionRestrictions) {
2036-
return false;
2037-
}
2038-
2039-
// FIXME: This is still a lousy approximation of whether the module file will
2040-
// be externally consumed.
2041-
return true;
2042-
}

lib/Frontend/Frontend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
177177
// the public.
178178
serializationOpts.SerializeOptionsForDebugging =
179179
opts.SerializeOptionsForDebugging.getValueOr(
180-
!isModuleExternallyConsumed(module));
180+
!module->isExternallyConsumed());
181181

182182
serializationOpts.DisableCrossModuleIncrementalInfo =
183183
opts.DisableCrossModuleIncrementalBuild;

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,12 @@ static bool writeSIL(SILModule &SM, const PrimarySpecificPaths &PSPs,
174174
///
175175
/// \see swift::printAsObjC
176176
static bool printAsObjCIfNeeded(StringRef outputPath, ModuleDecl *M,
177-
StringRef bridgingHeader, bool moduleIsPublic) {
177+
StringRef bridgingHeader) {
178178
if (outputPath.empty())
179179
return false;
180180
return withOutputFile(M->getDiags(), outputPath,
181181
[&](raw_ostream &out) -> bool {
182-
auto requiredAccess = moduleIsPublic ? AccessLevel::Public
183-
: AccessLevel::Internal;
184-
return printAsObjC(out, M, bridgingHeader, requiredAccess);
182+
return printAsObjC(out, M, bridgingHeader);
185183
});
186184
}
187185

@@ -852,8 +850,7 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
852850
}
853851
hadAnyError |= printAsObjCIfNeeded(
854852
Invocation.getObjCHeaderOutputPathForAtMostOnePrimary(),
855-
Instance.getMainModule(), BridgingHeaderPathForPrint,
856-
Invocation.isModuleExternallyConsumed(Instance.getMainModule()));
853+
Instance.getMainModule(), BridgingHeaderPathForPrint);
857854
}
858855

859856
// Only want the header if there's been any errors, ie. there's not much

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 5 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/GenericEnvironment.h"
2424
#include "swift/AST/PrettyStackTrace.h"
2525
#include "swift/AST/SwiftNameTranslation.h"
26+
#include "swift/AST/TypeCheckRequests.h"
2627
#include "swift/AST/TypeVisitor.h"
2728
#include "swift/IDE/CommentConversion.h"
2829
#include "swift/Parse/Lexer.h"
@@ -930,96 +931,15 @@ class DeclAndTypePrinter::Implementation
930931
return hasPrintedAnything;
931932
}
932933

933-
const ValueDecl *getRenameDecl(const ValueDecl *D,
934-
const ParsedDeclName renamedParsedDeclName) {
935-
auto declContext = D->getDeclContext();
936-
ASTContext &astContext = D->getASTContext();
937-
auto renamedDeclName = renamedParsedDeclName.formDeclNameRef(astContext);
938-
939-
if (isa<ClassDecl>(D) || isa<ProtocolDecl>(D)) {
940-
if (!renamedParsedDeclName.ContextName.empty()) {
941-
return nullptr;
942-
}
943-
SmallVector<ValueDecl *, 1> decls;
944-
declContext->lookupQualified(declContext->getParentModule(),
945-
renamedDeclName.withoutArgumentLabels(),
946-
NL_OnlyTypes,
947-
decls);
948-
if (decls.size() == 1)
949-
return decls[0];
950-
return nullptr;
951-
}
952-
953-
TypeDecl *typeDecl = declContext->getSelfNominalTypeDecl();
954-
955-
const ValueDecl *renamedDecl = nullptr;
956-
SmallVector<ValueDecl *, 4> lookupResults;
957-
declContext->lookupQualified(typeDecl->getDeclaredInterfaceType(),
958-
renamedDeclName, NL_QualifiedDefault,
959-
lookupResults);
960-
961-
if (lookupResults.size() == 1) {
962-
auto candidate = lookupResults[0];
963-
if (!shouldInclude(candidate))
964-
return nullptr;
965-
if (candidate->getKind() != D->getKind() ||
966-
(candidate->isInstanceMember() !=
967-
cast<ValueDecl>(D)->isInstanceMember()))
968-
return nullptr;
969-
970-
renamedDecl = candidate;
971-
} else {
972-
for (auto candidate : lookupResults) {
973-
if (!shouldInclude(candidate))
974-
continue;
975-
976-
if (candidate->getKind() != D->getKind() ||
977-
(candidate->isInstanceMember() !=
978-
cast<ValueDecl>(D)->isInstanceMember()))
979-
continue;
980-
981-
if (isa<AbstractFunctionDecl>(candidate)) {
982-
auto cParams = cast<AbstractFunctionDecl>(candidate)->getParameters();
983-
auto dParams = cast<AbstractFunctionDecl>(D)->getParameters();
984-
985-
if (cParams->size() != dParams->size())
986-
continue;
987-
988-
bool hasSameParameterTypes = true;
989-
for (auto index : indices(*cParams)) {
990-
auto cParamsType = cParams->get(index)->getType();
991-
auto dParamsType = dParams->get(index)->getType();
992-
if (!cParamsType->matchesParameter(dParamsType,
993-
TypeMatchOptions())) {
994-
hasSameParameterTypes = false;
995-
break;
996-
}
997-
}
998-
999-
if (!hasSameParameterTypes) {
1000-
continue;
1001-
}
1002-
}
1003-
1004-
if (renamedDecl) {
1005-
// If we found a duplicated candidate then we would silently fail.
1006-
renamedDecl = nullptr;
1007-
break;
1008-
}
1009-
renamedDecl = candidate;
1010-
}
1011-
}
1012-
return renamedDecl;
1013-
}
1014-
1015934
void printRenameForDecl(const AvailableAttr *AvAttr, const ValueDecl *D,
1016935
bool includeQuotes) {
1017936
assert(!AvAttr->Rename.empty());
1018937

1019-
const ValueDecl *renamedDecl =
1020-
getRenameDecl(D, parseDeclName(AvAttr->Rename));
1021-
938+
auto *renamedDecl = evaluateOrDefault(
939+
getASTContext().evaluator, RenamedDeclRequest{D, AvAttr}, nullptr);
1022940
if (renamedDecl) {
941+
assert(shouldInclude(renamedDecl) &&
942+
"ObjC printer logic mismatch with renamed decl");
1023943
SmallString<128> scratch;
1024944
auto renamedObjCRuntimeName =
1025945
renamedDecl->getObjCRuntimeName()->getString(scratch);

0 commit comments

Comments
 (0)