Skip to content

[Import Decl] Refactoring ObjCMethod importing #5991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 9 additions & 137 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,131 +812,6 @@ bool ClangImporter::addSearchPath(StringRef newSearchPath, bool isFramework) {
return false;
}

void importer::addEntryToLookupTable(SwiftLookupTable &table,
clang::NamedDecl *named,
NameImporter &nameImporter) {
// Determine whether this declaration is suppressed in Swift.
if (shouldSuppressDeclImport(named)) return;

// Leave incomplete struct/enum/union types out of the table; Swift only
// handles pointers to them.
// FIXME: At some point we probably want to be importing incomplete types,
// so that pointers to different incomplete types themselves have distinct
// types. At that time it will be necessary to make the decision of whether
// or not to import an incomplete type declaration based on whether it's
// actually the struct backing a CF type:
//
// typedef struct CGColor *CGColorRef;
//
// The best way to do this is probably to change CFDatabase.def to include
// struct names when relevant, not just pointer names. That way we can check
// both CFDatabase.def and the objc_bridge attribute and cover all our bases.
if (auto *tagDecl = dyn_cast<clang::TagDecl>(named)) {
if (!tagDecl->getDefinition())
return;
}

// If we have a name to import as, add this entry to the table.
if (auto importedName = nameImporter.importName(named, None)) {
table.addEntry(importedName.Imported, named, importedName.EffectiveContext);

// Also add the subscript entry, if needed.
if (importedName.isSubscriptAccessor())
table.addEntry(DeclName(nameImporter.getContext(),
nameImporter.getContext().Id_subscript,
ArrayRef<Identifier>()),
named, importedName.EffectiveContext);

// Import the Swift 2 name of this entity, and record it as well if it is
// different.
if (auto swift2Name =
nameImporter.importName(named, ImportNameFlags::Swift2Name)) {
if (swift2Name.Imported != importedName.Imported)
table.addEntry(swift2Name.Imported, named, swift2Name.EffectiveContext);
}
} else if (auto category = dyn_cast<clang::ObjCCategoryDecl>(named)) {
// If the category is invalid, don't add it.
if (category->isInvalidDecl()) return;

table.addCategory(category);
}

// Walk the members of any context that can have nested members.
if (isa<clang::TagDecl>(named) ||
isa<clang::ObjCInterfaceDecl>(named) ||
isa<clang::ObjCProtocolDecl>(named) ||
isa<clang::ObjCCategoryDecl>(named)) {
clang::DeclContext *dc = cast<clang::DeclContext>(named);
for (auto member : dc->decls()) {
if (auto namedMember = dyn_cast<clang::NamedDecl>(member))
addEntryToLookupTable(table, namedMember, nameImporter);
}
}
}

void importer::addMacrosToLookupTable(clang::ASTContext &clangCtx,
clang::Preprocessor &pp,
SwiftLookupTable &table,
ASTContext &SwiftContext) {
for (const auto &macro : pp.macros(false)) {
// Find the local history of this macro directive.
clang::MacroDirective *MD = pp.getLocalMacroDirectiveHistory(macro.first);

// Walk the history.
for (; MD; MD = MD->getPrevious()) {
// Don't look at any definitions that are followed by undefs.
// FIXME: This isn't quite correct across explicit submodules -- one
// submodule might define a macro, while another defines and then
// undefines the same macro. If they are processed in that order, the
// history will have the undef at the end, and we'll miss the first
// definition.
if (isa<clang::UndefMacroDirective>(MD))
break;

// Only interested in macro definitions.
auto *defMD = dyn_cast<clang::DefMacroDirective>(MD);
if (!defMD) continue;

// Is this definition from this module?
auto info = defMD->getInfo();
if (!info || info->isFromASTFile()) continue;

// If we hit a builtin macro, we're done.
if (info->isBuiltinMacro()) break;

// If we hit a macro with invalid or predefined location, we're done.
auto loc = defMD->getLocation();
if (loc.isInvalid()) break;
if (pp.getSourceManager().getFileID(loc) == pp.getPredefinesFileID())
break;

// Add this entry.
auto name = importMacroName(macro.first, info, clangCtx, SwiftContext);
if (name.empty()) continue;
table.addEntry(name, info, clangCtx.getTranslationUnitDecl(), &pp);
}
}
}

void importer::finalizeLookupTable(clang::ASTContext &clangCtx,
clang::Preprocessor &pp,
SwiftLookupTable &table,
ASTContext &SwiftContext) {
// Resolve any unresolved entries.
SmallVector<SwiftLookupTable::SingleEntry, 4> unresolved;
if (table.resolveUnresolvedEntries(unresolved)) {
// Complain about unresolved entries that remain.
for (auto entry : unresolved) {
auto decl = entry.get<clang::NamedDecl *>();
auto swiftName = decl->getAttr<clang::SwiftNameAttr>();

SwiftContext.Diags.diagnose(SourceLoc(), diag::unresolvable_clang_decl,
decl->getNameAsString(),
swiftName->getName());
}
}
}

bool ClangImporter::Implementation::importHeader(
Module *adapter, StringRef headerName, SourceLoc diagLoc,
bool trackParsedSymbols,
Expand Down Expand Up @@ -997,14 +872,14 @@ bool ClangImporter::Implementation::importHeader(
for (auto group : allParsedDecls)
for (auto *D : group)
if (auto named = dyn_cast<clang::NamedDecl>(D))
addEntryToLookupTable(BridgingHeaderLookupTable, named, *nameImporter);
addEntryToLookupTable(BridgingHeaderLookupTable, named,
getNameImporter());

pp.EndSourceFile();
bumpGeneration();

// Add any defined macros to the bridging header lookup table.
addMacrosToLookupTable(getClangASTContext(), getClangPreprocessor(),
BridgingHeaderLookupTable, SwiftContext);
addMacrosToLookupTable(BridgingHeaderLookupTable, getNameImporter());

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

// Finalize the lookup table, which may fail.
finalizeLookupTable(getClangASTContext(), getClangPreprocessor(),
BridgingHeaderLookupTable, SwiftContext);
finalizeLookupTable(BridgingHeaderLookupTable, getNameImporter());

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

auto &ClangCtx = Impl.getClangASTContext();
auto &ClangPP = Impl.getClangPreprocessor();
for (clang::IdentifierInfo *II : Impl.BridgeHeaderMacros) {
if (auto *MI = ClangPP.getMacroInfo(II)) {
if (filter(MI)) {
Identifier Name = importMacroName(II, MI, ClangCtx, Impl.SwiftContext);
Identifier Name = Impl.getNameImporter().importMacroName(II, MI);
if (Decl *imported = Impl.importMacro(Name, MI))
receiver(imported);
}
Expand Down Expand Up @@ -1936,8 +1809,7 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
auto *II = const_cast<clang::IdentifierInfo*>(MD->getName());
if (auto *MI = ClangPP.getMacroInfo(II)) {
if (filter(MI)) {
Identifier Name =
importMacroName(II, MI, ClangCtx, Impl.SwiftContext);
Identifier Name = Impl.getNameImporter().importMacroName(II, MI);
if (Decl *imported = Impl.importMacro(Name, MI))
receiver(imported);
}
Expand Down Expand Up @@ -2324,7 +2196,7 @@ void ClangModuleUnit::lookupObjCMethods(
results.push_back(func);

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

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

// Check for an alternate declaration; if it's name matches,
// report it.
if (auto alternate = getAlternateDecl(decl)) {
for (auto alternate : getAlternateDecls(decl)) {
if (alternate->getFullName().matchesRef(name)) {
consumer.foundDecl(alternate, DeclVisibilityKind::DynamicLookup);
matchedAny = true;
Expand Down
Loading