Skip to content

Commit 99ea1bb

Browse files
committed
ClangImporter: Sink getEffectiveClangNode() down from IDE to the importer and clean up related code
1 parent c07023e commit 99ea1bb

File tree

6 files changed

+30
-83
lines changed

6 files changed

+30
-83
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,9 @@ class ClangImporter final : public ClangModuleLoader {
215215
StringRef relatedEntityKind,
216216
llvm::function_ref<void(TypeDecl *)> receiver) override;
217217

218-
/// Look up the nested 'Code' enum for an error wrapper struct.
219-
EnumDecl *lookupErrorCodeEnum(const StructDecl *errorWrapper) const;
218+
/// Just like Decl::getClangNode() except we look through to the 'Code'
219+
/// enum of an error wrapper struct.
220+
ClangNode getEffectiveClangNode(const Decl *decl) const;
220221

221222
/// Look for textually included declarations from the bridging header.
222223
///

lib/ClangImporter/ClangImporter.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,26 +2167,9 @@ static bool isVisibleFromModule(const ClangModuleUnit *ModuleFilter,
21672167
if (!Wrapper)
21682168
return false;
21692169

2170-
auto ClangNode = VD->getClangNode();
2171-
if (!ClangNode) {
2172-
// If we synthesized a ValueDecl, it won't have a Clang node. Find the
2173-
// associated declaration that /does/ have a Clang node, and use that.
2174-
auto *SynthesizedTypeAttr =
2175-
VD->getAttrs().getAttribute<ClangImporterSynthesizedTypeAttr>();
2176-
assert(SynthesizedTypeAttr);
2177-
2178-
switch (SynthesizedTypeAttr->getKind()) {
2179-
case ClangImporterSynthesizedTypeAttr::Kind::NSErrorWrapper:
2180-
case ClangImporterSynthesizedTypeAttr::Kind::NSErrorWrapperAnon: {
2181-
ASTContext &Ctx = ContainingUnit->getASTContext();
2182-
auto *Importer = static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
2183-
auto WrapperStruct = cast<StructDecl>(VD);
2184-
auto *CodeEnum = Importer->lookupErrorCodeEnum(WrapperStruct);
2185-
ClangNode = CodeEnum->getClangNode();
2186-
break;
2187-
}
2188-
}
2189-
}
2170+
ASTContext &Ctx = ContainingUnit->getASTContext();
2171+
auto *Importer = static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
2172+
auto ClangNode = Importer->getEffectiveClangNode(VD);
21902173

21912174
// Macros can be "redeclared" by putting an equivalent definition in two
21922175
// different modules. (We don't actually check the equivalence.)
@@ -2493,8 +2476,19 @@ void ClangImporter::lookupValue(DeclName name, VisibleDeclConsumer &consumer) {
24932476
});
24942477
}
24952478

2496-
EnumDecl *ClangImporter::lookupErrorCodeEnum(const StructDecl *errorWrapper) const {
2497-
return Impl.lookupErrorCodeEnum(errorWrapper);
2479+
ClangNode ClangImporter::getEffectiveClangNode(const Decl *decl) const {
2480+
// Directly...
2481+
if (auto clangNode = decl->getClangNode())
2482+
return clangNode;
2483+
2484+
// Or via the nested "Code" enum.
2485+
if (auto *errorWrapper = dyn_cast<StructDecl>(decl)) {
2486+
if (auto *code = Impl.lookupErrorCodeEnum(errorWrapper))
2487+
if (auto clangNode = code->getClangNode())
2488+
return clangNode;
2489+
}
2490+
2491+
return ClangNode();
24982492
}
24992493

25002494
void ClangImporter::lookupTypeDecl(

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,7 +2942,7 @@ namespace {
29422942
AccessLevel::Public, loc, SourceLoc(),
29432943
C.Id_ErrorType, loc,
29442944
/*genericparams=*/nullptr, enumDecl);
2945-
alias->setUnderlyingType(Impl.getSugaredTypeReference(errorWrapper));
2945+
alias->setUnderlyingType(errorWrapper->getDeclaredInterfaceType());
29462946
enumDecl->addMember(alias);
29472947

29482948
// Add the 'Code' enum to the error wrapper.
@@ -5161,7 +5161,7 @@ namespace {
51615161
typealias->setGenericParams(GTD->getGenericParams()->clone(typealias));
51625162
}
51635163

5164-
typealias->setUnderlyingType(Impl.getSugaredTypeReference(typeDecl));
5164+
typealias->setUnderlyingType(typeDecl->getDeclaredInterfaceType());
51655165
return typealias;
51665166
}
51675167

@@ -5393,7 +5393,7 @@ Decl *SwiftDeclConverter::importCompatibilityTypeAlias(
53935393
alias->setGenericParams(GTD->getGenericParams()->clone(alias));
53945394
}
53955395

5396-
alias->setUnderlyingType(Impl.getSugaredTypeReference(typeDecl));
5396+
alias->setUnderlyingType(typeDecl->getDeclaredInterfaceType());
53975397

53985398
// Record that this is the official version of this declaration.
53995399
Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}] = alias;

lib/ClangImporter/ImportType.cpp

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -819,33 +819,17 @@ namespace {
819819
return getAdjustedTypeDeclReferenceType(decl);
820820
}
821821

822-
/// Retrieve the 'Code' type for a bridged NSError, or nullptr if
823-
/// this is not a bridged NSError type.
824-
TypeDecl *getBridgedNSErrorCode(TypeDecl *decl) {
825-
auto errorWrapper = dyn_cast<StructDecl>(decl);
826-
if (!errorWrapper) return nullptr;
827-
828-
const DeclAttributes &allAttrs = errorWrapper->getAttrs();
829-
for (auto attr : allAttrs.getAttributes<SynthesizedProtocolAttr>()) {
830-
if (attr->getProtocolKind() ==
831-
KnownProtocolKind::BridgedStoredNSError) {
832-
return Impl.lookupErrorCodeEnum(errorWrapper);
833-
}
834-
}
835-
836-
return nullptr;
837-
}
838-
839822
/// Retrieve the adjusted type of a reference to the given type declaration.
840-
Type getAdjustedTypeDeclReferenceType(TypeDecl *type) {
823+
Type getAdjustedTypeDeclReferenceType(TypeDecl *decl) {
841824
// If the imported declaration is a bridged NSError, dig out
842825
// the Code nested type. References to the enum type from C
843826
// code need to map to the code type (which is ABI compatible with C),
844827
// and the bridged error type is used elsewhere.
845-
if (auto codeDecl = getBridgedNSErrorCode(type))
846-
return Impl.getSugaredTypeReference(codeDecl);
828+
if (auto *structDecl = dyn_cast<StructDecl>(decl))
829+
if (auto *codeEnum = Impl.lookupErrorCodeEnum(structDecl))
830+
return codeEnum->getDeclaredInterfaceType();
847831

848-
return Impl.getSugaredTypeReference(type);
832+
return decl->getDeclaredInterfaceType();
849833
}
850834

851835
ImportResult VisitEnumType(const clang::EnumType *type) {
@@ -2514,23 +2498,6 @@ static Type getNamedProtocolType(ClangImporter::Implementation &impl,
25142498
return Type();
25152499
}
25162500

2517-
Type ClangImporter::Implementation::getSugaredTypeReference(TypeDecl *type) {
2518-
// For typealiases, build a sugared type.
2519-
if (auto typealias = dyn_cast<TypeAliasDecl>(type)) {
2520-
// If this typealias is nested, retrieve the parent type.
2521-
Type parentType;
2522-
if (auto nominal = typealias->getDeclContext()->getSelfNominalTypeDecl()) {
2523-
if (!nominal->getGenericSignature())
2524-
parentType = nominal->getDeclaredInterfaceType();
2525-
}
2526-
2527-
return TypeAliasType::get(typealias, parentType, SubstitutionMap(),
2528-
typealias->getUnderlyingTypeLoc().getType());
2529-
}
2530-
2531-
return type->getDeclaredInterfaceType();
2532-
}
2533-
25342501
Type ClangImporter::Implementation::getNSCopyingType() {
25352502
return getNamedProtocolType(*this, "NSCopying");
25362503
}

lib/ClangImporter/ImporterImpl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -950,9 +950,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
950950
/// Retrieve the NSCopying protocol type.
951951
Type getNSCopyingType();
952952

953-
/// Retrieve a sugared referenece to the given (imported) type.
954-
Type getSugaredTypeReference(TypeDecl *type);
955-
956953
/// Determines whether the given type matches an implicit type
957954
/// bound of "Hashable", which is used to validate NSDictionary/NSSet.
958955
bool matchesHashableBound(Type type);

lib/IDE/Utils.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -954,21 +954,9 @@ bool swift::ide::isFromClang(const Decl *D) {
954954
}
955955

956956
ClangNode swift::ide::getEffectiveClangNode(const Decl *decl) {
957-
// Directly...
958-
if (auto clangNode = decl->getClangNode())
959-
return clangNode;
960-
961-
// Or via the nested "Code" enum.
962-
if (auto *errorWrapper = dyn_cast<StructDecl>(decl)) {
963-
auto &ctx = errorWrapper->getASTContext();
964-
auto *importer = static_cast<ClangImporter *>(ctx.getClangModuleLoader());
965-
if (importer)
966-
if (auto *code = importer->lookupErrorCodeEnum(errorWrapper))
967-
if (auto clangDecl = code->getClangDecl())
968-
return clangDecl;
969-
}
970-
971-
return ClangNode();
957+
auto &ctx = decl->getASTContext();
958+
auto *importer = static_cast<ClangImporter *>(ctx.getClangModuleLoader());
959+
return importer->getEffectiveClangNode(decl);
972960
}
973961

974962
/// Retrieve the Clang node for the given extension, if it has one.

0 commit comments

Comments
 (0)