ClangImporter: Look up availability domains defined in bridging headers.

This is very brittle in this first iteration. For now we require the
declaration representing the availability domain be deserialized before it can
be looked up by name since Clang does not have a lookup table for availabilty
domains in its module representation. As a result, it only works for bridging
headers that are not precompiled.

Part of rdar://138441266.
This commit is contained in:
Allan Shortlidge
2025-03-12 18:50:10 -07:00
parent 275a6792d1
commit 017dae382e
12 changed files with 153 additions and 32 deletions

View File

@@ -350,6 +350,11 @@ void SwiftLookupTable::addCategory(clang::ObjCCategoryDecl *category) {
Categories.push_back(category);
}
void SwiftLookupTable::addAvailabilityDomainDecl(StringRef name,
clang::VarDecl *decl) {
AvailabilityDomains.insert({name, StoredSingleEntry(decl)});
}
bool SwiftLookupTable::resolveUnresolvedEntries(
SmallVectorImpl<SingleEntry> &unresolved) {
// Common case: nothing left to resolve.
@@ -712,6 +717,17 @@ SwiftLookupTable::lookupGlobalsAsMembers(SerializedSwiftName baseName,
return lookupGlobalsAsMembersImpl(baseName, *storedContext);
}
clang::VarDecl *SwiftLookupTable::lookupAvailabilityDomainDecl(StringRef name) {
// FIXME: [availability] Remove this once Clang has a lookup table.
auto result = AvailabilityDomains.find(name);
if (result == AvailabilityDomains.end())
return nullptr;
auto &entry = result->second;
DEBUG_ASSERT(entry.isASTNodeEntry());
return static_cast<clang::VarDecl *>(entry.getASTNode());
}
SmallVector<SwiftLookupTable::SingleEntry, 4>
SwiftLookupTable::allGlobalsAsMembersInContext(EffectiveClangContext context) {
if (!context) return { };
@@ -1868,9 +1884,9 @@ SwiftNameLookupExtension::hashExtension(ExtensionHashBuilder &HBuilder) const {
void importer::addEntryToLookupTable(SwiftLookupTable &table,
clang::NamedDecl *named,
NameImporter &nameImporter) {
auto &clangContext = nameImporter.getClangContext();
clang::PrettyStackTraceDecl trace(
named, named->getLocation(),
nameImporter.getClangContext().getSourceManager(),
named, named->getLocation(), clangContext.getSourceManager(),
"while adding SwiftName lookup table entries for clang declaration");
// Determine whether this declaration is suppressed in Swift.
@@ -2009,6 +2025,16 @@ void importer::addEntryToLookupTable(SwiftLookupTable &table,
addEntryToLookupTable(table, usingShadowDecl, nameImporter);
}
}
// If this decl represents an availability domain, add it to the lookup table
// as one.
// FIXME: [availability] Remove this once Clang has a lookup table.
if (auto varDecl = dyn_cast<clang::VarDecl>(named)) {
auto mutableVar = const_cast<clang::VarDecl *>(varDecl);
auto domainInfo = clangContext.getFeatureAvailInfo(mutableVar);
if (!domainInfo.first.empty())
table.addAvailabilityDomainDecl(domainInfo.first, mutableVar);
}
}
/// Returns the nearest parent of \p module that is marked \c explicit in its