Skip to content

Commit f1c45de

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 35aa862 commit f1c45de

14 files changed

+185
-139
lines changed

Diff for: include/swift/AST/Module.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,6 @@ class ModuleDecl : public DeclContext, public TypeDecl {
774774
void collectBasicSourceFileInfo(
775775
llvm::function_ref<void(const BasicSourceFileInfo &)> callback) const;
776776

777-
public:
778777
/// Retrieve a fingerprint value that summarizes the contents of this module.
779778
///
780779
/// This interface hash a of a module is guaranteed to change if the interface
@@ -787,6 +786,15 @@ class ModuleDecl : public DeclContext, public TypeDecl {
787786
/// contents have been made.
788787
Fingerprint getFingerprint() const;
789788

789+
/// Returns an approximation of whether the given module could be
790+
/// redistributed and consumed by external clients.
791+
///
792+
/// FIXME: The scope of this computation should be limited entirely to
793+
/// RenamedDeclRequest. Unfortunately, it has been co-opted to support the
794+
/// \c SerializeOptionsForDebugging hack. Once this information can be
795+
/// transferred from module files to the dSYMs, remove this.
796+
bool isExternallyConsumed() const;
797+
790798
SourceRange getSourceRange() const { return SourceRange(); }
791799

792800
static bool classof(const DeclContext *DC) {

Diff for: include/swift/AST/TypeCheckRequests.h

+17
Original file line numberDiff line numberDiff line change
@@ -3003,6 +3003,23 @@ class AsyncAlternativeRequest
30033003
bool isCached() const { return true; }
30043004
};
30053005

3006+
class RenamedDeclRequest
3007+
: public SimpleRequest<RenamedDeclRequest,
3008+
ValueDecl *(const ValueDecl *, const AvailableAttr *),
3009+
RequestFlags::Cached> {
3010+
public:
3011+
using SimpleRequest::SimpleRequest;
3012+
3013+
private:
3014+
friend SimpleRequest;
3015+
3016+
ValueDecl *evaluate(Evaluator &evaluator, const ValueDecl *attached,
3017+
const AvailableAttr *attr) const;
3018+
3019+
public:
3020+
bool isCached() const { return true; }
3021+
};
3022+
30063023
void simple_display(llvm::raw_ostream &out, Type value);
30073024
void simple_display(llvm::raw_ostream &out, const TypeRepr *TyR);
30083025
void simple_display(llvm::raw_ostream &out, ImplicitMemberAction action);

Diff for: include/swift/AST/TypeCheckerTypeIDZone.def

+3
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,6 @@ SWIFT_REQUEST(TypeChecker, GetImplicitSendableRequest,
334334
SWIFT_REQUEST(TypeChecker, AsyncAlternativeRequest,
335335
AbstractFunctionDecl *(AbstractFunctionDecl *),
336336
Cached, NoLocationInfo)
337+
SWIFT_REQUEST(TypeChecker, RenamedDeclRequest,
338+
ValueDecl *(const ValueDecl *),
339+
Cached, NoLocationInfo)

Diff for: include/swift/Frontend/Frontend.h

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

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

Diff for: include/swift/PrintAsObjC/PrintAsObjC.h

+1-2
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

Diff for: lib/AST/Module.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,32 @@ Fingerprint ModuleDecl::getFingerprint() const {
16381638
return Fingerprint{std::move(hasher)};
16391639
}
16401640

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

Diff for: lib/Frontend/CompilerInvocation.cpp

-27
Original file line numberDiff line numberDiff line change
@@ -2118,30 +2118,3 @@ CompilerInvocation::setUpInputForSILTool(
21182118
}
21192119
return fileBufOrErr;
21202120
}
2121-
2122-
bool CompilerInvocation::isModuleExternallyConsumed(
2123-
const ModuleDecl *mod) const {
2124-
// Modules for executables aren't expected to be consumed by other modules.
2125-
// This picks up all kinds of entrypoints, including script mode,
2126-
// @UIApplicationMain and @NSApplicationMain.
2127-
if (mod->hasEntryPoint()) {
2128-
return false;
2129-
}
2130-
2131-
// If an implicit Objective-C header was needed to construct this module, it
2132-
// must be the product of a library target.
2133-
if (!getFrontendOptions().ImplicitObjCHeaderPath.empty()) {
2134-
return false;
2135-
}
2136-
2137-
// App extensions are special beasts because they build without entrypoints
2138-
// like library targets, but they behave like executable targets because
2139-
// their associated modules are not suitable for distribution.
2140-
if (mod->getASTContext().LangOpts.EnableAppExtensionRestrictions) {
2141-
return false;
2142-
}
2143-
2144-
// FIXME: This is still a lousy approximation of whether the module file will
2145-
// be externally consumed.
2146-
return true;
2147-
}

Diff for: lib/Frontend/Frontend.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
180180
// the public.
181181
serializationOpts.SerializeOptionsForDebugging =
182182
opts.SerializeOptionsForDebugging.getValueOr(
183-
!isModuleExternallyConsumed(module));
183+
!module->isExternallyConsumed());
184184

185185
serializationOpts.DisableCrossModuleIncrementalInfo =
186186
opts.DisableCrossModuleIncrementalBuild;

Diff for: lib/FrontendTool/FrontendTool.cpp

+3-6
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

@@ -853,8 +851,7 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
853851
}
854852
hadAnyError |= printAsObjCIfNeeded(
855853
Invocation.getObjCHeaderOutputPathForAtMostOnePrimary(),
856-
Instance.getMainModule(), BridgingHeaderPathForPrint,
857-
Invocation.isModuleExternallyConsumed(Instance.getMainModule()));
854+
Instance.getMainModule(), BridgingHeaderPathForPrint);
858855
}
859856

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

Diff for: lib/PrintAsObjC/DeclAndTypePrinter.cpp

+5-85
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"
@@ -927,96 +928,15 @@ class DeclAndTypePrinter::Implementation
927928
return hasPrintedAnything;
928929
}
929930

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

1016-
const ValueDecl *renamedDecl =
1017-
getRenameDecl(D, parseDeclName(AvAttr->Rename));
1018-
935+
auto *renamedDecl = evaluateOrDefault(
936+
getASTContext().evaluator, RenamedDeclRequest{D, AvAttr}, nullptr);
1019937
if (renamedDecl) {
938+
assert(shouldInclude(renamedDecl) &&
939+
"ObjC printer logic mismatch with renamed decl");
1020940
SmallString<128> scratch;
1021941
auto renamedObjCRuntimeName =
1022942
renamedDecl->getObjCRuntimeName()->getString(scratch);

Diff for: lib/PrintAsObjC/ModuleContentsWriter.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ class ModuleWriter {
598598
void
599599
swift::printModuleContentsAsObjC(raw_ostream &os,
600600
llvm::SmallPtrSetImpl<ImportModuleTy> &imports,
601-
ModuleDecl &M, AccessLevel minRequiredAccess) {
602-
ModuleWriter(os, imports, M, minRequiredAccess).write();
601+
ModuleDecl &M) {
602+
auto requiredAccess = M.isExternallyConsumed() ? AccessLevel::Public
603+
: AccessLevel::Internal;
604+
ModuleWriter(os, imports, M, requiredAccess).write();
603605
}

Diff for: lib/PrintAsObjC/ModuleContentsWriter.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class ModuleDecl;
2727

2828
using ImportModuleTy = PointerUnion<ModuleDecl*, const clang::Module*>;
2929

30-
/// Prints the declarations of \p M to \p os, filtering by \p minRequiredAccess
31-
/// and collecting imports in \p imports along the way.
30+
/// Prints the declarations of \p M to \p os and collecting imports in
31+
/// \p imports along the way.
3232
void printModuleContentsAsObjC(raw_ostream &os,
3333
llvm::SmallPtrSetImpl<ImportModuleTy> &imports,
34-
ModuleDecl &M, AccessLevel minRequiredAccess);
34+
ModuleDecl &M);
3535

3636
} // end namespace swift
3737

Diff for: lib/PrintAsObjC/PrintAsObjC.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,13 @@ static void writeEpilogue(raw_ostream &os) {
383383
}
384384

385385
bool swift::printAsObjC(raw_ostream &os, ModuleDecl *M,
386-
StringRef bridgingHeader,
387-
AccessLevel minRequiredAccess) {
386+
StringRef bridgingHeader) {
388387
llvm::PrettyStackTraceString trace("While generating Objective-C header");
389388

390389
SmallPtrSet<ImportModuleTy, 8> imports;
391390
std::string moduleContentsBuf;
392391
llvm::raw_string_ostream moduleContents{moduleContentsBuf};
393-
printModuleContentsAsObjC(moduleContents, imports, *M, minRequiredAccess);
392+
printModuleContentsAsObjC(moduleContents, imports, *M);
394393
std::string macroGuard = (llvm::Twine(M->getNameStr().upper()) + "_SWIFT_H").str();
395394
writePrologue(os, M->getASTContext(), macroGuard);
396395
writeImports(os, imports, *M, bridgingHeader);

0 commit comments

Comments
 (0)