Skip to content

Commit 15f5367

Browse files
committed
[Clang Importer] Simplify and move more lookup table APIs
1 parent f7d61a1 commit 15f5367

File tree

7 files changed

+175
-169
lines changed

7 files changed

+175
-169
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 6 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -812,131 +812,6 @@ bool ClangImporter::addSearchPath(StringRef newSearchPath, bool isFramework) {
812812
return false;
813813
}
814814

815-
void importer::addEntryToLookupTable(SwiftLookupTable &table,
816-
clang::NamedDecl *named,
817-
NameImporter &nameImporter) {
818-
// Determine whether this declaration is suppressed in Swift.
819-
if (shouldSuppressDeclImport(named)) return;
820-
821-
// Leave incomplete struct/enum/union types out of the table; Swift only
822-
// handles pointers to them.
823-
// FIXME: At some point we probably want to be importing incomplete types,
824-
// so that pointers to different incomplete types themselves have distinct
825-
// types. At that time it will be necessary to make the decision of whether
826-
// or not to import an incomplete type declaration based on whether it's
827-
// actually the struct backing a CF type:
828-
//
829-
// typedef struct CGColor *CGColorRef;
830-
//
831-
// The best way to do this is probably to change CFDatabase.def to include
832-
// struct names when relevant, not just pointer names. That way we can check
833-
// both CFDatabase.def and the objc_bridge attribute and cover all our bases.
834-
if (auto *tagDecl = dyn_cast<clang::TagDecl>(named)) {
835-
if (!tagDecl->getDefinition())
836-
return;
837-
}
838-
839-
// If we have a name to import as, add this entry to the table.
840-
if (auto importedName = nameImporter.importName(named, None)) {
841-
table.addEntry(importedName.Imported, named, importedName.EffectiveContext);
842-
843-
// Also add the subscript entry, if needed.
844-
if (importedName.isSubscriptAccessor())
845-
table.addEntry(DeclName(nameImporter.getContext(),
846-
nameImporter.getContext().Id_subscript,
847-
ArrayRef<Identifier>()),
848-
named, importedName.EffectiveContext);
849-
850-
// Import the Swift 2 name of this entity, and record it as well if it is
851-
// different.
852-
if (auto swift2Name =
853-
nameImporter.importName(named, ImportNameFlags::Swift2Name)) {
854-
if (swift2Name.Imported != importedName.Imported)
855-
table.addEntry(swift2Name.Imported, named, swift2Name.EffectiveContext);
856-
}
857-
} else if (auto category = dyn_cast<clang::ObjCCategoryDecl>(named)) {
858-
// If the category is invalid, don't add it.
859-
if (category->isInvalidDecl()) return;
860-
861-
table.addCategory(category);
862-
}
863-
864-
// Walk the members of any context that can have nested members.
865-
if (isa<clang::TagDecl>(named) ||
866-
isa<clang::ObjCInterfaceDecl>(named) ||
867-
isa<clang::ObjCProtocolDecl>(named) ||
868-
isa<clang::ObjCCategoryDecl>(named)) {
869-
clang::DeclContext *dc = cast<clang::DeclContext>(named);
870-
for (auto member : dc->decls()) {
871-
if (auto namedMember = dyn_cast<clang::NamedDecl>(member))
872-
addEntryToLookupTable(table, namedMember, nameImporter);
873-
}
874-
}
875-
}
876-
877-
void importer::addMacrosToLookupTable(clang::ASTContext &clangCtx,
878-
clang::Preprocessor &pp,
879-
SwiftLookupTable &table,
880-
ASTContext &SwiftContext) {
881-
for (const auto &macro : pp.macros(false)) {
882-
// Find the local history of this macro directive.
883-
clang::MacroDirective *MD = pp.getLocalMacroDirectiveHistory(macro.first);
884-
885-
// Walk the history.
886-
for (; MD; MD = MD->getPrevious()) {
887-
// Don't look at any definitions that are followed by undefs.
888-
// FIXME: This isn't quite correct across explicit submodules -- one
889-
// submodule might define a macro, while another defines and then
890-
// undefines the same macro. If they are processed in that order, the
891-
// history will have the undef at the end, and we'll miss the first
892-
// definition.
893-
if (isa<clang::UndefMacroDirective>(MD))
894-
break;
895-
896-
// Only interested in macro definitions.
897-
auto *defMD = dyn_cast<clang::DefMacroDirective>(MD);
898-
if (!defMD) continue;
899-
900-
// Is this definition from this module?
901-
auto info = defMD->getInfo();
902-
if (!info || info->isFromASTFile()) continue;
903-
904-
// If we hit a builtin macro, we're done.
905-
if (info->isBuiltinMacro()) break;
906-
907-
// If we hit a macro with invalid or predefined location, we're done.
908-
auto loc = defMD->getLocation();
909-
if (loc.isInvalid()) break;
910-
if (pp.getSourceManager().getFileID(loc) == pp.getPredefinesFileID())
911-
break;
912-
913-
// Add this entry.
914-
auto name = importMacroName(macro.first, info, clangCtx, SwiftContext);
915-
if (name.empty()) continue;
916-
table.addEntry(name, info, clangCtx.getTranslationUnitDecl(), &pp);
917-
}
918-
}
919-
}
920-
921-
void importer::finalizeLookupTable(clang::ASTContext &clangCtx,
922-
clang::Preprocessor &pp,
923-
SwiftLookupTable &table,
924-
ASTContext &SwiftContext) {
925-
// Resolve any unresolved entries.
926-
SmallVector<SwiftLookupTable::SingleEntry, 4> unresolved;
927-
if (table.resolveUnresolvedEntries(unresolved)) {
928-
// Complain about unresolved entries that remain.
929-
for (auto entry : unresolved) {
930-
auto decl = entry.get<clang::NamedDecl *>();
931-
auto swiftName = decl->getAttr<clang::SwiftNameAttr>();
932-
933-
SwiftContext.Diags.diagnose(SourceLoc(), diag::unresolvable_clang_decl,
934-
decl->getNameAsString(),
935-
swiftName->getName());
936-
}
937-
}
938-
}
939-
940815
bool ClangImporter::Implementation::importHeader(
941816
Module *adapter, StringRef headerName, SourceLoc diagLoc,
942817
bool trackParsedSymbols,
@@ -997,14 +872,14 @@ bool ClangImporter::Implementation::importHeader(
997872
for (auto group : allParsedDecls)
998873
for (auto *D : group)
999874
if (auto named = dyn_cast<clang::NamedDecl>(D))
1000-
addEntryToLookupTable(BridgingHeaderLookupTable, named, *nameImporter);
875+
addEntryToLookupTable(BridgingHeaderLookupTable, named,
876+
getNameImporter());
1001877

1002878
pp.EndSourceFile();
1003879
bumpGeneration();
1004880

1005881
// Add any defined macros to the bridging header lookup table.
1006-
addMacrosToLookupTable(getClangASTContext(), getClangPreprocessor(),
1007-
BridgingHeaderLookupTable, SwiftContext);
882+
addMacrosToLookupTable(BridgingHeaderLookupTable, getNameImporter());
1008883

1009884
// Wrap all Clang imports under a Swift import decl.
1010885
for (auto &Import : BridgeHeaderTopLevelImports) {
@@ -1014,8 +889,7 @@ bool ClangImporter::Implementation::importHeader(
1014889
}
1015890

1016891
// Finalize the lookup table, which may fail.
1017-
finalizeLookupTable(getClangASTContext(), getClangPreprocessor(),
1018-
BridgingHeaderLookupTable, SwiftContext);
892+
finalizeLookupTable(BridgingHeaderLookupTable, getNameImporter());
1019893

1020894
// FIXME: What do we do if there was already an error?
1021895
if (!hadError && clangDiags.hasErrorOccurred()) {
@@ -1854,12 +1728,11 @@ void ClangImporter::lookupBridgingHeaderDecls(
18541728
}
18551729
}
18561730

1857-
auto &ClangCtx = Impl.getClangASTContext();
18581731
auto &ClangPP = Impl.getClangPreprocessor();
18591732
for (clang::IdentifierInfo *II : Impl.BridgeHeaderMacros) {
18601733
if (auto *MI = ClangPP.getMacroInfo(II)) {
18611734
if (filter(MI)) {
1862-
Identifier Name = importMacroName(II, MI, ClangCtx, Impl.SwiftContext);
1735+
Identifier Name = Impl.getNameImporter().importMacroName(II, MI);
18631736
if (Decl *imported = Impl.importMacro(Name, MI))
18641737
receiver(imported);
18651738
}
@@ -1936,8 +1809,7 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
19361809
auto *II = const_cast<clang::IdentifierInfo*>(MD->getName());
19371810
if (auto *MI = ClangPP.getMacroInfo(II)) {
19381811
if (filter(MI)) {
1939-
Identifier Name =
1940-
importMacroName(II, MI, ClangCtx, Impl.SwiftContext);
1812+
Identifier Name = Impl.getNameImporter().importMacroName(II, MI);
19411813
if (Decl *imported = Impl.importMacro(Name, MI))
19421814
receiver(imported);
19431815
}

lib/ClangImporter/ImportMacro.cpp

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

lib/ClangImporter/ImportName.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,16 +1622,16 @@ bool ClangImporter::shouldIgnoreMacro(StringRef Name,
16221622
return ::shouldIgnoreMacro(Name, Macro);
16231623
}
16241624

1625-
Identifier importer::importMacroName(
1626-
const clang::IdentifierInfo *clangIdentifier, const clang::MacroInfo *macro,
1627-
clang::ASTContext &clangCtx, ASTContext &SwiftContext) {
1625+
Identifier
1626+
NameImporter::importMacroName(const clang::IdentifierInfo *clangIdentifier,
1627+
const clang::MacroInfo *macro) {
16281628
// If we're supposed to ignore this macro, return an empty identifier.
16291629
if (::shouldIgnoreMacro(clangIdentifier->getName(), macro))
16301630
return Identifier();
16311631

16321632
// No transformation is applied to the name.
16331633
StringRef name = clangIdentifier->getName();
1634-
return SwiftContext.getIdentifier(name);
1634+
return swiftCtx.getIdentifier(name);
16351635
}
16361636

16371637
/// Determine the Swift name for a clang decl

lib/ClangImporter/ImportName.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,12 @@ struct ImportedName {
139139
/// in "Notification", or it there would be nothing left.
140140
StringRef stripNotification(StringRef name);
141141

142-
/// Imports the name of the given Clang macro into Swift.
143-
Identifier importMacroName(const clang::IdentifierInfo *clangIdentifier,
144-
const clang::MacroInfo *macro,
145-
clang::ASTContext &clangCtx,
146-
ASTContext &SwiftContext);
147-
148142
// TODO: I'd like to remove the following
149143
/// Flags that control the import of names in importFullName.
150144
enum class ImportNameFlags {
151145
/// Suppress the factory-method-as-initializer transformation.
152146
SuppressFactoryMethodAsInit = 0x01,
147+
153148
/// Produce the Swift 2 name of the given entity.
154149
Swift2Name = 0x02,
155150
};
@@ -189,6 +184,10 @@ class NameImporter {
189184
ImportedName importName(const clang::NamedDecl *decl,
190185
ImportNameOptions options);
191186

187+
/// Imports the name of the given Clang macro into Swift.
188+
Identifier importMacroName(const clang::IdentifierInfo *clangIdentifier,
189+
const clang::MacroInfo *macro);
190+
192191
ASTContext &getContext() { return swiftCtx; }
193192
const LangOptions &getLangOpts() const { return swiftCtx.LangOpts; }
194193

@@ -208,7 +207,12 @@ class NameImporter {
208207
}
209208

210209
clang::Sema &getClangSema() { return clangSema; }
211-
clang::ASTContext &getClangContext() { return getClangSema().getASTContext(); }
210+
clang::ASTContext &getClangContext() {
211+
return getClangSema().getASTContext();
212+
}
213+
clang::Preprocessor &getClangPreprocessor() {
214+
return getClangSema().getPreprocessor();
215+
}
212216

213217
private:
214218
bool enableObjCInterop() const { return swiftCtx.LangOpts.EnableObjCInterop; }

lib/ClangImporter/ImporterImpl.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,22 +1145,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
11451145

11461146
namespace importer {
11471147

1148-
/// Add the given named declaration as an entry to the given Swift name
1149-
/// lookup table, including any of its child entries.
1150-
void addEntryToLookupTable(SwiftLookupTable &table, clang::NamedDecl *named,
1151-
importer::NameImporter &);
1152-
1153-
/// Add the macros from the given Clang preprocessor to the given
1154-
/// Swift name lookup table.
1155-
void addMacrosToLookupTable(clang::ASTContext &clangCtx,
1156-
clang::Preprocessor &pp, SwiftLookupTable &table,
1157-
ASTContext &SwiftContext);
1158-
1159-
/// Finalize a lookup table, handling any as-yet-unresolved entries
1160-
/// and emitting diagnostics if necessary.
1161-
void finalizeLookupTable(clang::ASTContext &clangCtx, clang::Preprocessor &pp,
1162-
SwiftLookupTable &table, ASTContext &SwiftContext);
1163-
11641148
/// Whether we should suppress the import of the given Clang declaration.
11651149
bool shouldSuppressDeclImport(const clang::Decl *decl);
11661150

0 commit comments

Comments
 (0)