Skip to content

Commit e092774

Browse files
committed
[Import Name] Ruin SwiftNameLookupExtension and Impl's friendship
SwiftNameLookupExtension and ClangImporter::Implementation were friends, but as time goes on they have drifted apart. As part of the ImportName refactoring, these are being decoupled to facilitate multiple-name importing, and fight the existing false encapsulation present in the Impl. SwiftNameLookupExtension is now spun off into its own entity, and can evolve to have and use its own de-coupled NameImporter.
1 parent e1b2a27 commit e092774

File tree

7 files changed

+135
-151
lines changed

7 files changed

+135
-151
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,6 @@ class ClangImporter final : public ClangModuleLoader {
231231
/// Otherwise, return nullptr.
232232
Decl *importDeclCached(const clang::NamedDecl *ClangDecl);
233233

234-
// Returns true if it is expected that the macro is ignored.
235-
bool shouldIgnoreMacro(StringRef Name, const clang::MacroInfo *Macro);
236-
237234
/// Returns the name of the given enum element as it would be imported into
238235
/// Swift.
239236
///

lib/ClangImporter/ClangImporter.cpp

Lines changed: 28 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ ClangImporter::create(ASTContext &ctx,
654654

655655
// Install a Clang module file extension to build Swift name lookup tables.
656656
invocation->getFrontendOpts().ModuleFileExtensions.push_back(
657-
new Implementation::SwiftNameLookupExtension(importer->Impl));
657+
new SwiftNameLookupExtension(importer->Impl));
658658

659659
// Create a compiler instance.
660660
auto PCHContainerOperations =
@@ -739,10 +739,9 @@ ClangImporter::create(ASTContext &ctx,
739739
importer->Impl.addBridgeHeaderTopLevelDecls(D);
740740

741741
if (auto named = dyn_cast<clang::NamedDecl>(D)) {
742-
importer->Impl.addEntryToLookupTable(
743-
instance.getSema(),
744-
importer->Impl.BridgingHeaderLookupTable,
745-
named);
742+
addEntryToLookupTable(instance.getSema(),
743+
importer->Impl.BridgingHeaderLookupTable, named,
744+
importer->Impl.nameImporter);
746745
}
747746
}
748747
}
@@ -800,9 +799,10 @@ bool ClangImporter::addSearchPath(StringRef newSearchPath, bool isFramework) {
800799
return false;
801800
}
802801

803-
void ClangImporter::Implementation::addEntryToLookupTable(
804-
clang::Sema &clangSema, SwiftLookupTable &table, clang::NamedDecl *named,
805-
NameImporter &nameImporter) {
802+
void importer::addEntryToLookupTable(clang::Sema &clangSema,
803+
SwiftLookupTable &table,
804+
clang::NamedDecl *named,
805+
NameImporter &nameImporter) {
806806
// Determine whether this declaration is suppressed in Swift.
807807
if (shouldSuppressDeclImport(named)) return;
808808

@@ -863,9 +863,10 @@ void ClangImporter::Implementation::addEntryToLookupTable(
863863
}
864864
}
865865

866-
void ClangImporter::Implementation::addMacrosToLookupTable(
867-
clang::ASTContext &clangCtx, clang::Preprocessor &pp,
868-
SwiftLookupTable &table, ASTContext &SwiftContext) {
866+
void importer::addMacrosToLookupTable(clang::ASTContext &clangCtx,
867+
clang::Preprocessor &pp,
868+
SwiftLookupTable &table,
869+
ASTContext &SwiftContext) {
869870
for (const auto &macro : pp.macros(false)) {
870871
// Find the local history of this macro directive.
871872
clang::MacroDirective *MD = pp.getLocalMacroDirectiveHistory(macro.first);
@@ -906,11 +907,10 @@ void ClangImporter::Implementation::addMacrosToLookupTable(
906907
}
907908
}
908909

909-
void ClangImporter::Implementation::finalizeLookupTable(
910-
clang::ASTContext &clangCtx,
911-
clang::Preprocessor &pp,
912-
SwiftLookupTable &table,
913-
ASTContext &SwiftContext) {
910+
void importer::finalizeLookupTable(clang::ASTContext &clangCtx,
911+
clang::Preprocessor &pp,
912+
SwiftLookupTable &table,
913+
ASTContext &SwiftContext) {
914914
// Resolve any unresolved entries.
915915
SmallVector<SwiftLookupTable::SingleEntry, 4> unresolved;
916916
if (table.resolveUnresolvedEntries(unresolved)) {
@@ -986,7 +986,8 @@ bool ClangImporter::Implementation::importHeader(
986986
for (auto group : allParsedDecls)
987987
for (auto *D : group)
988988
if (auto named = dyn_cast<clang::NamedDecl>(D))
989-
addEntryToLookupTable(getClangSema(), BridgingHeaderLookupTable, named);
989+
addEntryToLookupTable(getClangSema(), BridgingHeaderLookupTable, named,
990+
nameImporter);
990991

991992
pp.EndSourceFile();
992993
bumpGeneration();
@@ -1418,50 +1419,6 @@ ClangImporter::Implementation::exportName(Identifier name) {
14181419
return ident;
14191420
}
14201421

1421-
/// Returns true if it is expected that the macro is ignored.
1422-
static bool shouldIgnoreMacro(StringRef name, const clang::MacroInfo *macro) {
1423-
// Ignore include guards.
1424-
if (macro->isUsedForHeaderGuard())
1425-
return true;
1426-
1427-
// If there are no tokens, there is nothing to convert.
1428-
if (macro->tokens_empty())
1429-
return true;
1430-
1431-
// Currently we only convert non-function-like macros.
1432-
if (macro->isFunctionLike())
1433-
return true;
1434-
1435-
// Consult the blacklist of macros to suppress.
1436-
auto suppressMacro =
1437-
llvm::StringSwitch<bool>(name)
1438-
#define SUPPRESS_MACRO(NAME) .Case(#NAME, true)
1439-
#include "MacroTable.def"
1440-
.Default(false);
1441-
1442-
if (suppressMacro)
1443-
return true;
1444-
1445-
return false;
1446-
}
1447-
1448-
bool ClangImporter::shouldIgnoreMacro(StringRef Name,
1449-
const clang::MacroInfo *Macro) {
1450-
return ::shouldIgnoreMacro(Name, Macro);
1451-
}
1452-
1453-
Identifier ClangImporter::Implementation::importMacroName(
1454-
const clang::IdentifierInfo *clangIdentifier, const clang::MacroInfo *macro,
1455-
clang::ASTContext &clangCtx, ASTContext &SwiftContext) {
1456-
// If we're supposed to ignore this macro, return an empty identifier.
1457-
if (::shouldIgnoreMacro(clangIdentifier->getName(), macro))
1458-
return Identifier();
1459-
1460-
// No transformation is applied to the name.
1461-
StringRef name = clangIdentifier->getName();
1462-
return SwiftContext.getIdentifier(name);
1463-
}
1464-
14651422
namespace llvm {
14661423
// An Identifier is "pointer like".
14671424
template<typename T> class PointerLikeTypeTraits;
@@ -1575,8 +1532,7 @@ isPotentiallyConflictingSetter(const clang::ObjCProtocolDecl *proto,
15751532
return false;
15761533
}
15771534

1578-
bool ClangImporter::Implementation::shouldSuppressDeclImport(
1579-
const clang::Decl *decl) {
1535+
bool importer::shouldSuppressDeclImport(const clang::Decl *decl) {
15801536
if (auto objcMethod = dyn_cast<clang::ObjCMethodDecl>(decl)) {
15811537
// First check if we're actually in a Swift class.
15821538
auto dc = decl->getDeclContext();
@@ -1894,8 +1850,7 @@ void ClangImporter::lookupBridgingHeaderDecls(
18941850
for (clang::IdentifierInfo *II : Impl.BridgeHeaderMacros) {
18951851
if (auto *MI = ClangPP.getMacroInfo(II)) {
18961852
if (filter(MI)) {
1897-
Identifier Name =
1898-
Impl.importMacroName(II, MI, ClangCtx, Impl.SwiftContext);
1853+
Identifier Name = importMacroName(II, MI, ClangCtx, Impl.SwiftContext);
18991854
if (Decl *imported = Impl.importMacro(Name, MI))
19001855
receiver(imported);
19011856
}
@@ -1973,7 +1928,7 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
19731928
if (auto *MI = ClangPP.getMacroInfo(II)) {
19741929
if (filter(MI)) {
19751930
Identifier Name =
1976-
Impl.importMacroName(II, MI, ClangCtx, Impl.SwiftContext);
1931+
importMacroName(II, MI, ClangCtx, Impl.SwiftContext);
19771932
if (Decl *imported = Impl.importMacro(Name, MI))
19781933
receiver(imported);
19791934
}
@@ -2673,8 +2628,7 @@ void ClangImporter::getMangledName(raw_ostream &os,
26732628
// ---------------------------------------------------------------------------
26742629

26752630
clang::ModuleFileExtensionMetadata
2676-
ClangImporter::Implementation::SwiftNameLookupExtension::
2677-
getExtensionMetadata() const {
2631+
SwiftNameLookupExtension::getExtensionMetadata() const {
26782632
clang::ModuleFileExtensionMetadata metadata;
26792633
metadata.BlockName = "swift.lookup";
26802634
metadata.MajorVersion = SWIFT_LOOKUP_TABLE_VERSION_MAJOR;
@@ -2685,17 +2639,15 @@ getExtensionMetadata() const {
26852639
}
26862640

26872641
llvm::hash_code
2688-
ClangImporter::Implementation::SwiftNameLookupExtension::hashExtension(
2689-
llvm::hash_code code) const {
2642+
SwiftNameLookupExtension::hashExtension(llvm::hash_code code) const {
26902643
return llvm::hash_combine(code, StringRef("swift.lookup"),
26912644
SWIFT_LOOKUP_TABLE_VERSION_MAJOR,
26922645
SWIFT_LOOKUP_TABLE_VERSION_MINOR,
26932646
nameImporter.isInferImportAsMember());
26942647
}
26952648

26962649
std::unique_ptr<clang::ModuleFileExtensionWriter>
2697-
ClangImporter::Implementation::SwiftNameLookupExtension::createExtensionWriter(
2698-
clang::ASTWriter &writer) {
2650+
SwiftNameLookupExtension::createExtensionWriter(clang::ASTWriter &writer) {
26992651
// Local function to populate the lookup table.
27002652
auto populateTable = [this](clang::Sema &sema, SwiftLookupTable &table) {
27012653
auto &swiftCtx = nameImporter.getContext();
@@ -2725,12 +2677,10 @@ ClangImporter::Implementation::SwiftNameLookupExtension::createExtensionWriter(
27252677
}
27262678

27272679
std::unique_ptr<clang::ModuleFileExtensionReader>
2728-
ClangImporter::Implementation::SwiftNameLookupExtension::createExtensionReader(
2729-
const clang::ModuleFileExtensionMetadata &metadata,
2730-
clang::ASTReader &reader,
2731-
clang::serialization::ModuleFile &mod,
2732-
const llvm::BitstreamCursor &stream)
2733-
{
2680+
SwiftNameLookupExtension::createExtensionReader(
2681+
const clang::ModuleFileExtensionMetadata &metadata,
2682+
clang::ASTReader &reader, clang::serialization::ModuleFile &mod,
2683+
const llvm::BitstreamCursor &stream) {
27342684
// Make sure we have a compatible block. Since these values are part
27352685
// of the hash, it should never be wrong.
27362686
assert(metadata.BlockName == "swift.lookup");

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5954,7 +5954,7 @@ void SwiftDeclConverter::importObjCMembers(
59545954

59555955
// If this declaration shouldn't be visible, don't add it to
59565956
// the list.
5957-
if (Impl.shouldSuppressDeclImport(objcMethod))
5957+
if (shouldSuppressDeclImport(objcMethod))
59585958
continue;
59595959
}
59605960

lib/ClangImporter/ImportMacro.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,12 @@ static ValueDecl *importMacro(ClangImporter::Implementation &impl,
463463
auto firstMacroInfo = impl.getClangPreprocessor().getMacroInfo(firstID);
464464
auto secondMacroInfo = impl.getClangPreprocessor().getMacroInfo(
465465
secondID);
466-
auto firstIdentifier = impl.importMacroName(firstID, firstMacroInfo,
467-
impl.getClangASTContext(),
468-
impl.SwiftContext);
469-
auto secondIdentifier = impl.importMacroName(secondID, secondMacroInfo,
470-
impl.getClangASTContext(),
471-
impl.SwiftContext);
466+
auto firstIdentifier = importMacroName(firstID, firstMacroInfo,
467+
impl.getClangASTContext(),
468+
impl.SwiftContext);
469+
auto secondIdentifier = importMacroName(secondID, secondMacroInfo,
470+
impl.getClangASTContext(),
471+
impl.SwiftContext);
472472
impl.importMacro(firstIdentifier, firstMacroInfo);
473473
impl.importMacro(secondIdentifier, secondMacroInfo);
474474
auto firstIterator = impl.ImportedMacroConstants.find(firstMacroInfo);

lib/ClangImporter/ImportName.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,3 +1594,41 @@ ImportedName NameImporter::importFullName(const clang::NamedDecl *D,
15941594
result.Imported = formDeclName(swiftCtx, baseName, argumentNames, isFunction);
15951595
return result;
15961596
}
1597+
1598+
/// Returns true if it is expected that the macro is ignored.
1599+
static bool shouldIgnoreMacro(StringRef name, const clang::MacroInfo *macro) {
1600+
// Ignore include guards.
1601+
if (macro->isUsedForHeaderGuard())
1602+
return true;
1603+
1604+
// If there are no tokens, there is nothing to convert.
1605+
if (macro->tokens_empty())
1606+
return true;
1607+
1608+
// Currently we only convert non-function-like macros.
1609+
if (macro->isFunctionLike())
1610+
return true;
1611+
1612+
// Consult the blacklist of macros to suppress.
1613+
auto suppressMacro = llvm::StringSwitch<bool>(name)
1614+
#define SUPPRESS_MACRO(NAME) .Case(#NAME, true)
1615+
#include "MacroTable.def"
1616+
.Default(false);
1617+
1618+
if (suppressMacro)
1619+
return true;
1620+
1621+
return false;
1622+
}
1623+
1624+
Identifier importer::importMacroName(
1625+
const clang::IdentifierInfo *clangIdentifier, const clang::MacroInfo *macro,
1626+
clang::ASTContext &clangCtx, ASTContext &SwiftContext) {
1627+
// If we're supposed to ignore this macro, return an empty identifier.
1628+
if (::shouldIgnoreMacro(clangIdentifier->getName(), macro))
1629+
return Identifier();
1630+
1631+
// No transformation is applied to the name.
1632+
StringRef name = clangIdentifier->getName();
1633+
return SwiftContext.getIdentifier(name);
1634+
}

lib/ClangImporter/ImportName.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ struct ImportedName {
132132
/// in "Notification", or it there would be nothing left.
133133
StringRef stripNotification(StringRef name);
134134

135+
/// Imports the name of the given Clang macro into Swift.
136+
Identifier importMacroName(const clang::IdentifierInfo *clangIdentifier,
137+
const clang::MacroInfo *macro,
138+
clang::ASTContext &clangCtx,
139+
ASTContext &SwiftContext);
140+
135141
// TODO: I'd like to remove the following
136142
/// Flags that control the import of names in importFullName.
137143
enum class ImportNameFlags {

0 commit comments

Comments
 (0)