Skip to content

Commit 82f0a19

Browse files
authored
Merge pull request #5991 from milseman/import_name_refactor_wip
[Import Decl] Refactoring ObjCMethod importing
2 parents 8be4df4 + d1efc80 commit 82f0a19

17 files changed

+434
-509
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 9 additions & 137 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
}
@@ -2324,7 +2196,7 @@ void ClangModuleUnit::lookupObjCMethods(
23242196
results.push_back(func);
23252197

23262198
// If there is an alternate declaration, also look at it.
2327-
if (auto alternate = owner.Impl.getAlternateDecl(imported)) {
2199+
for (auto alternate : owner.Impl.getAlternateDecls(imported)) {
23282200
if (auto func = dyn_cast<AbstractFunctionDecl>(alternate))
23292201
results.push_back(func);
23302202
}
@@ -2713,7 +2585,7 @@ void ClangImporter::Implementation::lookupValue(
27132585

27142586
// If there is an alternate declaration and the name matches,
27152587
// report this result.
2716-
if (auto alternate = getAlternateDecl(decl)) {
2588+
for (auto alternate : getAlternateDecls(decl)) {
27172589
if (alternate->getFullName().matchesRef(name) &&
27182590
alternate->getDeclContext()->isModuleScopeContext()) {
27192591
consumer.foundDecl(alternate, DeclVisibilityKind::VisibleAtTopLevel);
@@ -2777,7 +2649,7 @@ void ClangImporter::Implementation::lookupObjCMembers(
27772649

27782650
// Check for an alternate declaration; if it's name matches,
27792651
// report it.
2780-
if (auto alternate = getAlternateDecl(decl)) {
2652+
for (auto alternate : getAlternateDecls(decl)) {
27812653
if (alternate->getFullName().matchesRef(name)) {
27822654
consumer.foundDecl(alternate, DeclVisibilityKind::DynamicLookup);
27832655
matchedAny = true;

0 commit comments

Comments
 (0)