Skip to content

[ClangImporter] Avoid calling into ObjCSelector lookup #31937

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 1 commit into from
May 22, 2020
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
12 changes: 7 additions & 5 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -748,11 +748,13 @@ class ASTContext final {
/// \param methods The list of @objc methods in this class that have this
/// selector and are instance/class methods as requested. This list will be
/// extended with any methods found in subsequent generations.
void loadObjCMethods(ClassDecl *classDecl,
ObjCSelector selector,
bool isInstanceMethod,
unsigned previousGeneration,
llvm::TinyPtrVector<AbstractFunctionDecl *> &methods);
///
/// \param swiftOnly If true, only loads methods from imported Swift modules,
/// skipping the Clang importer.
void loadObjCMethods(ClassDecl *classDecl, ObjCSelector selector,
bool isInstanceMethod, unsigned previousGeneration,
llvm::TinyPtrVector<AbstractFunctionDecl *> &methods,
bool swiftOnly = false);

/// Load derivative function configurations for the given
/// AbstractFunctionDecl.
Expand Down
12 changes: 7 additions & 5 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1484,14 +1484,16 @@ void ASTContext::loadExtensions(NominalTypeDecl *nominal,
}

void ASTContext::loadObjCMethods(
ClassDecl *classDecl,
ObjCSelector selector,
bool isInstanceMethod,
unsigned previousGeneration,
llvm::TinyPtrVector<AbstractFunctionDecl *> &methods) {
ClassDecl *classDecl, ObjCSelector selector, bool isInstanceMethod,
unsigned previousGeneration,
llvm::TinyPtrVector<AbstractFunctionDecl *> &methods, bool swiftOnly) {
PrettyStackTraceSelector stackTraceSelector("looking for", selector);
PrettyStackTraceDecl stackTraceDecl("...in", classDecl);
for (auto &loader : getImpl().ModuleLoaders) {
// Ignore the Clang importer if we've been asked for Swift-only results.
if (swiftOnly && loader.get() == getClangModuleLoader())
continue;

loader->loadObjCMethods(classDecl, selector, isInstanceMethod,
previousGeneration, methods);
}
Expand Down
10 changes: 1 addition & 9 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3045,11 +3045,6 @@ void ClangImporter::loadObjCMethods(
bool isInstanceMethod,
unsigned previousGeneration,
llvm::TinyPtrVector<AbstractFunctionDecl *> &methods) {
// If we're currently looking for this selector, don't load any Objective-C
// methods.
if (Impl.ActiveSelectors.count({selector, isInstanceMethod}))
return;

const auto *objcClass =
dyn_cast_or_null<clang::ObjCInterfaceDecl>(classDecl->getClangDecl());
if (!objcClass)
Expand Down Expand Up @@ -3082,10 +3077,7 @@ void ClangImporter::loadObjCMethods(
// earlier, because we aren't tracking generation counts for Clang modules.
// Filter out the duplicates.
// FIXME: We shouldn't need to do this.
llvm::SmallPtrSet<AbstractFunctionDecl *, 4> known;
known.insert(methods.begin(), methods.end());

if (known.insert(method).second)
if (!llvm::is_contained(methods, method))
methods.push_back(method);
}

Expand Down
105 changes: 66 additions & 39 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2191,10 +2191,13 @@ namespace {
if (fd->isInstanceMember() != decl->isInstanceProperty())
continue;

assert(fd->getName().getArgumentNames().empty());
// We only care about methods with no arguments, because they can
// shadow imported properties.
if (!fd->getName().getArgumentNames().empty())
continue;

foundMethod = true;
} else {
auto *var = cast<VarDecl>(result);
} else if (auto *var = dyn_cast<VarDecl>(result)) {
if (var->isInstanceMember() != decl->isInstanceProperty())
continue;

Expand Down Expand Up @@ -2280,7 +2283,7 @@ namespace {
return getVersion() == getActiveSwiftVersion();
}

void recordMemberInContext(DeclContext *dc, ValueDecl *member) {
void recordMemberInContext(const DeclContext *dc, ValueDecl *member) {
assert(member && "Attempted to record null member!");
auto *nominal = dc->getSelfNominalTypeDecl();
auto name = member->getBaseName();
Expand Down Expand Up @@ -4170,33 +4173,51 @@ namespace {
bool isInstance, const DeclContext *dc,
llvm::function_ref<bool(AbstractFunctionDecl *fn)> filter) {
// We only need to perform this check for classes.
auto classDecl
= dc->getDeclaredInterfaceType()->getClassOrBoundGenericClass();
auto *classDecl = dc->getSelfClassDecl();
if (!classDecl)
return false;

// Make sure we don't search in Clang modules for this method.
++Impl.ActiveSelectors[{selector, isInstance}];

// Look for a matching imported or deserialized member.
bool result = false;
for (auto decl : classDecl->lookupDirect(selector, isInstance)) {
if ((decl->getClangDecl()
|| !decl->getDeclContext()->getParentSourceFile())
&& importedName.getDeclName() == decl->getName()
&& filter(decl)) {
result = true;
break;
auto matchesImportedDecl = [&](Decl *member) -> bool {
auto *afd = dyn_cast<AbstractFunctionDecl>(member);
if (!afd)
return false;

// Instance-ness must match.
if (afd->isObjCInstanceMethod() != isInstance)
return false;

// Both the selector and imported name must match.
if (afd->getObjCSelector() != selector ||
importedName.getDeclName() != afd->getName()) {
return false;
}
}

// Restore the previous active count in the active-selector mapping.
auto activeCount = Impl.ActiveSelectors.find({selector, isInstance});
--activeCount->second;
if (activeCount->second == 0)
Impl.ActiveSelectors.erase(activeCount);
// Finally, the provided filter must match.
return filter(afd);
};

return result;
// First check to see if we've already imported a method with the same
// selector.
auto importedMembers = Impl.MembersForNominal.find(classDecl);
if (importedMembers != Impl.MembersForNominal.end()) {
auto baseName = importedName.getDeclName().getBaseName();
auto membersForName = importedMembers->second.find(baseName);
if (membersForName != importedMembers->second.end()) {
return llvm::any_of(membersForName->second, matchesImportedDecl);
}
}

// Then, for a deserialized Swift class, check to see if it has brought in
// any matching @objc methods.
if (classDecl->wasDeserialized()) {
auto &ctx = Impl.SwiftContext;
TinyPtrVector<AbstractFunctionDecl *> deserializedMethods;
ctx.loadObjCMethods(classDecl, selector, isInstance,
/*prevGeneration*/ 0, deserializedMethods,
/*swiftOnly*/ true);
return llvm::any_of(deserializedMethods, matchesImportedDecl);
}
return false;
}

Decl *importObjCMethodDecl(const clang::ObjCMethodDecl *decl,
Expand Down Expand Up @@ -4448,6 +4469,9 @@ namespace {
// If this method overrides another method, mark it as such.
recordObjCOverride(result);

// Make a note that we've imported this method into this context.
recordMemberInContext(dc, result);

// Record the error convention.
if (errorConvention) {
result->setForeignErrorConvention(*errorConvention);
Expand Down Expand Up @@ -4484,14 +4508,6 @@ namespace {
Impl.addAlternateDecl(result, cast<ValueDecl>(imported));
}
}

// We only care about recording methods with no arguments here, because
// they can shadow imported properties.
if (!isa<AccessorDecl>(result) &&
result->getName().getArgumentNames().empty()) {
recordMemberInContext(dc, result);
}

return result;
}

Expand Down Expand Up @@ -6461,6 +6477,7 @@ ConstructorDecl *SwiftDeclConverter::importConstructor(
/*GenericParams=*/nullptr, const_cast<DeclContext *>(dc));

addObjCAttribute(result, selector);
recordMemberInContext(dc, result);

Impl.recordImplicitUnwrapForDecl(result,
importedType.isImplicitlyUnwrapped());
Expand Down Expand Up @@ -6668,14 +6685,24 @@ SwiftDeclConverter::importSubscript(Decl *decl,
};

auto findCounterpart = [&](clang::Selector sel) -> FuncDecl * {
// If the declaration we're starting from is in a class, first
// look for a class member with the appropriate selector.
// If the declaration we're starting from is in a class, first check to see
// if we've already imported an instance method with a matching selector.
if (auto classDecl = decl->getDeclContext()->getSelfClassDecl()) {
auto swiftSel = Impl.importSelector(sel);
for (auto found : classDecl->lookupDirect(swiftSel, true)) {
if (auto foundFunc = dyn_cast<FuncDecl>(found))
if (foundFunc->hasClangNode())
return foundFunc;
auto importedMembers = Impl.MembersForNominal.find(classDecl);
if (importedMembers != Impl.MembersForNominal.end()) {
for (auto membersForName : importedMembers->second) {
for (auto *member : membersForName.second) {
// Must be an instance method.
auto *afd = dyn_cast<FuncDecl>(member);
if (!afd || !afd->isInstanceMember())
continue;

// Selector must match.
if (afd->getObjCSelector() == swiftSel)
return afd;
}
}
}
}

Expand Down
6 changes: 0 additions & 6 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
llvm::DenseMap<const clang::MacroInfo *, std::pair<clang::APValue, Type>>
ImportedMacroConstants;

/// Keeps track of active selector-based lookups, so that we don't infinitely
/// recurse when checking whether a method with a given selector has already
/// been imported.
llvm::DenseMap<std::pair<ObjCSelector, char>, unsigned>
ActiveSelectors;

// Mapping from imported types to their raw value types.
llvm::DenseMap<const NominalTypeDecl *, Type> RawTypes;

Expand Down