Skip to content

Commit 779166d

Browse files
authored
Merge pull request swiftlang#12823 from DougGregor/clang-name-importer-all-properties
2 parents bef1e6d + a49455e commit 779166d

File tree

5 files changed

+84
-167
lines changed

5 files changed

+84
-167
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -884,15 +884,6 @@ class ASTContext {
884884
GenericSignatureBuilder *builder,
885885
GenericSignature *sig);
886886

887-
/// Retrieve the inherited name set for the given class.
888-
const InheritedNameSet *getAllPropertyNames(ClassDecl *classDecl,
889-
bool forInstance);
890-
891-
/// Retrieve the inherited name set for the given Objective-C class.
892-
const InheritedNameSet *getAllPropertyNames(
893-
clang::ObjCInterfaceDecl *classDecl,
894-
bool forInstance);
895-
896887
/// Retrieve a generic signature with a single unconstrained type parameter,
897888
/// like `<T>`.
898889
CanGenericSignature getSingleGenericParameterSignature() const;

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,6 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
266266
/// The keys are the generic signature builders in \c GenericSignatureBuilders.
267267
llvm::DenseMap<GenericSignatureBuilder *, GenericEnvironment *>
268268
CanonicalGenericEnvironments;
269-
270-
/// The set of property names that show up in the defining module of a
271-
/// class.
272-
llvm::DenseMap<std::pair<const ClassDecl *, char>,
273-
std::unique_ptr<InheritedNameSet>> AllProperties;
274-
275-
/// The set of property names that show up in the defining module of
276-
/// an Objective-C class.
277-
llvm::DenseMap<std::pair<const clang::ObjCInterfaceDecl *, char>,
278-
std::unique_ptr<InheritedNameSet>> AllPropertiesObjC;
279269

280270
/// The single-parameter generic signature with no constraints, <T>.
281271
CanGenericSignature SingleGenericParameterSignature;
@@ -4713,129 +4703,6 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
47134703
return Type();
47144704
}
47154705

4716-
const InheritedNameSet *ASTContext::getAllPropertyNames(ClassDecl *classDecl,
4717-
bool forInstance) {
4718-
// If this class was defined in Objective-C, perform the lookup based on
4719-
// the Objective-C class.
4720-
if (auto objcClass = dyn_cast_or_null<clang::ObjCInterfaceDecl>(
4721-
classDecl->getClangDecl())) {
4722-
return getAllPropertyNames(
4723-
const_cast<clang::ObjCInterfaceDecl *>(objcClass),
4724-
forInstance);
4725-
}
4726-
4727-
// If we already have this information, return it.
4728-
auto known = Impl.AllProperties.find({classDecl, forInstance});
4729-
if (known != Impl.AllProperties.end()) return known->second.get();
4730-
4731-
// Otherwise, get information from our superclass first.
4732-
if (auto resolver = getLazyResolver())
4733-
resolver->resolveSuperclass(classDecl);
4734-
4735-
const InheritedNameSet *parentSet = nullptr;
4736-
if (auto superclass = classDecl->getSuperclass()) {
4737-
if (auto superclassDecl = superclass->getClassOrBoundGenericClass()) {
4738-
parentSet = getAllPropertyNames(superclassDecl, forInstance);
4739-
}
4740-
}
4741-
4742-
// Create the set of properties.
4743-
known = Impl.AllProperties.insert(
4744-
{ std::pair<const ClassDecl *, char>(classDecl, forInstance),
4745-
llvm::make_unique<InheritedNameSet>(parentSet) }).first;
4746-
4747-
// Local function to add properties from the given set.
4748-
auto addProperties = [&](DeclRange members) {
4749-
for (auto member : members) {
4750-
auto var = dyn_cast<VarDecl>(member);
4751-
if (!var || var->getName().empty()) continue;
4752-
if (var->isInstanceMember() != forInstance) continue;
4753-
4754-
known->second->add(var->getName().str());
4755-
}
4756-
};
4757-
4758-
// Collect property names from the class.
4759-
addProperties(classDecl->getMembers());
4760-
4761-
// Collect property names from all extensions in the same module as the class.
4762-
auto module = classDecl->getParentModule();
4763-
for (auto ext : classDecl->getExtensions()) {
4764-
if (ext->getParentModule() != module) continue;
4765-
addProperties(ext->getMembers());
4766-
}
4767-
4768-
return known->second.get();
4769-
}
4770-
4771-
const InheritedNameSet *ASTContext::getAllPropertyNames(
4772-
clang::ObjCInterfaceDecl *classDecl,
4773-
bool forInstance) {
4774-
classDecl = classDecl->getCanonicalDecl();
4775-
4776-
// If we already have this information, return it.
4777-
auto known = Impl.AllPropertiesObjC.find({classDecl, forInstance});
4778-
if (known != Impl.AllPropertiesObjC.end()) return known->second.get();
4779-
4780-
// Otherwise, get information from our superclass first.
4781-
const InheritedNameSet *parentSet = nullptr;
4782-
if (auto superclassDecl = classDecl->getSuperClass()) {
4783-
parentSet = getAllPropertyNames(superclassDecl, forInstance);
4784-
}
4785-
4786-
// Create the set of properties.
4787-
known = Impl.AllPropertiesObjC.insert(
4788-
{ std::pair<const clang::ObjCInterfaceDecl *, char>(classDecl,
4789-
forInstance),
4790-
llvm::make_unique<InheritedNameSet>(parentSet) }).first;
4791-
4792-
// Local function to add properties from the given set.
4793-
auto addProperties = [&](clang::DeclContext::decl_range members) {
4794-
for (auto member : members) {
4795-
// Add Objective-C property names.
4796-
if (auto property = dyn_cast<clang::ObjCPropertyDecl>(member)) {
4797-
if (forInstance)
4798-
known->second->add(property->getName());
4799-
continue;
4800-
}
4801-
4802-
// Add no-parameter, non-void method names.
4803-
if (auto method = dyn_cast<clang::ObjCMethodDecl>(member)) {
4804-
if (method->getSelector().isUnarySelector() &&
4805-
!method->getReturnType()->isVoidType() &&
4806-
!method->hasRelatedResultType() &&
4807-
method->isInstanceMethod() == forInstance) {
4808-
known->second->add(method->getSelector().getNameForSlot(0));
4809-
continue;
4810-
}
4811-
}
4812-
}
4813-
};
4814-
4815-
// Dig out the class definition.
4816-
auto classDef = classDecl->getDefinition();
4817-
if (!classDef) return known->second.get();
4818-
4819-
// Collect property names from the class definition.
4820-
addProperties(classDef->decls());
4821-
4822-
// Dig out the module that owns the class definition.
4823-
auto module = classDef->getImportedOwningModule();
4824-
if (module) module = module->getTopLevelModule();
4825-
4826-
// Collect property names from all categories and extensions in the same
4827-
// module as the class.
4828-
for (auto category : classDef->known_categories()) {
4829-
auto categoryModule = category->getImportedOwningModule();
4830-
if (categoryModule) categoryModule = categoryModule->getTopLevelModule();
4831-
if (module != categoryModule) continue;
4832-
4833-
addProperties(category->decls());
4834-
}
4835-
4836-
return known->second.get();
4837-
}
4838-
48394706
CanGenericSignature ASTContext::getSingleGenericParameterSignature() const {
48404707
if (auto theSig = Impl.SingleGenericParameterSignature)
48414708
return theSig;

lib/ClangImporter/ImportName.cpp

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ static bool omitNeedlessWordsInFunctionName(
839839
if (!contextType.isNull()) {
840840
if (auto objcPtrType = contextType->getAsObjCInterfacePointerType())
841841
if (auto objcClassDecl = objcPtrType->getInterfaceDecl())
842-
allPropertyNames = nameImporter.getContext().getAllPropertyNames(
842+
allPropertyNames = nameImporter.getAllPropertyNames(
843843
objcClassDecl, isInstanceMethod);
844844
}
845845

@@ -1655,8 +1655,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
16551655
if (auto objcPtrType = contextType->getAsObjCInterfacePointerType())
16561656
if (auto objcClassDecl = objcPtrType->getInterfaceDecl())
16571657
allPropertyNames =
1658-
swiftCtx.getAllPropertyNames(objcClassDecl,
1659-
/*forInstance=*/true);
1658+
getAllPropertyNames(objcClassDecl, /*forInstance=*/true);
16601659
}
16611660

16621661
(void)omitNeedlessWords(baseName, {}, "", propertyTypeName,
@@ -1774,3 +1773,72 @@ ImportedName NameImporter::importName(const clang::NamedDecl *decl,
17741773
importNameCache[key] = res;
17751774
return res;
17761775
}
1776+
1777+
const InheritedNameSet *NameImporter::getAllPropertyNames(
1778+
clang::ObjCInterfaceDecl *classDecl,
1779+
bool forInstance) {
1780+
classDecl = classDecl->getCanonicalDecl();
1781+
1782+
// If we already have this information, return it.
1783+
auto known = allProperties.find({classDecl, forInstance});
1784+
if (known != allProperties.end()) return known->second.get();
1785+
1786+
// Otherwise, get information from our superclass first.
1787+
const InheritedNameSet *parentSet = nullptr;
1788+
if (auto superclassDecl = classDecl->getSuperClass()) {
1789+
parentSet = getAllPropertyNames(superclassDecl, forInstance);
1790+
}
1791+
1792+
// Create the set of properties.
1793+
known = allProperties.insert(
1794+
{ std::pair<const clang::ObjCInterfaceDecl *, char>(classDecl,
1795+
forInstance),
1796+
llvm::make_unique<InheritedNameSet>(parentSet) }).first;
1797+
1798+
// Local function to add properties from the given set.
1799+
auto addProperties = [&](clang::DeclContext::decl_range members) {
1800+
for (auto member : members) {
1801+
// Add Objective-C property names.
1802+
if (auto property = dyn_cast<clang::ObjCPropertyDecl>(member)) {
1803+
if (forInstance)
1804+
known->second->add(property->getName());
1805+
continue;
1806+
}
1807+
1808+
// Add no-parameter, non-void method names.
1809+
if (auto method = dyn_cast<clang::ObjCMethodDecl>(member)) {
1810+
if (method->getSelector().isUnarySelector() &&
1811+
!method->getReturnType()->isVoidType() &&
1812+
!method->hasRelatedResultType() &&
1813+
method->isInstanceMethod() == forInstance) {
1814+
known->second->add(method->getSelector().getNameForSlot(0));
1815+
continue;
1816+
}
1817+
}
1818+
}
1819+
};
1820+
1821+
// Dig out the class definition.
1822+
auto classDef = classDecl->getDefinition();
1823+
if (!classDef) return known->second.get();
1824+
1825+
// Collect property names from the class definition.
1826+
addProperties(classDef->decls());
1827+
1828+
// Dig out the module that owns the class definition.
1829+
auto module = classDef->getImportedOwningModule();
1830+
if (module) module = module->getTopLevelModule();
1831+
1832+
// Collect property names from all categories and extensions in the same
1833+
// module as the class.
1834+
for (auto category : classDef->known_categories()) {
1835+
auto categoryModule = category->getImportedOwningModule();
1836+
if (categoryModule) categoryModule = categoryModule->getTopLevelModule();
1837+
if (module != categoryModule) continue;
1838+
1839+
addProperties(category->decls());
1840+
}
1841+
1842+
return known->second.get();
1843+
}
1844+

lib/ClangImporter/ImportName.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ class NameImporter {
288288
/// Cache for repeated calls
289289
llvm::DenseMap<CacheKeyType, ImportedName> importNameCache;
290290

291+
/// The set of property names that show up in the defining module of
292+
/// an Objective-C class.
293+
llvm::DenseMap<std::pair<const clang::ObjCInterfaceDecl *, char>,
294+
std::unique_ptr<InheritedNameSet>> allProperties;
295+
291296
public:
292297
NameImporter(ASTContext &ctx, const PlatformAvailability &avail,
293298
clang::Sema &cSema, bool inferIAM)
@@ -331,6 +336,11 @@ class NameImporter {
331336
return getClangSema().getPreprocessor();
332337
}
333338

339+
/// Retrieve the inherited name set for the given Objective-C class.
340+
const InheritedNameSet *getAllPropertyNames(
341+
clang::ObjCInterfaceDecl *classDecl,
342+
bool forInstance);
343+
334344
private:
335345
bool enableObjCInterop() const { return swiftCtx.LangOpts.EnableObjCInterop; }
336346

lib/Sema/MiscDiagnostics.cpp

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3724,21 +3724,12 @@ Optional<DeclName> TypeChecker::omitNeedlessWords(AbstractFunctionDecl *afd) {
37243724
if (params->size() != 0 && !params->get(0)->getName().empty())
37253725
firstParamName = params->get(0)->getName().str();
37263726

3727-
// Find the set of property names.
3728-
const InheritedNameSet *allPropertyNames = nullptr;
3729-
if (contextType) {
3730-
if (auto classDecl = contextType->getClassOrBoundGenericClass()) {
3731-
allPropertyNames = Context.getAllPropertyNames(classDecl,
3732-
afd->isInstanceMember());
3733-
}
3734-
}
3735-
37363727
StringScratchSpace scratch;
37373728
if (!swift::omitNeedlessWords(baseNameStr, argNameStrs, firstParamName,
37383729
getTypeNameForOmission(resultType),
37393730
getTypeNameForOmission(contextType),
37403731
paramTypes, returnsSelf, false,
3741-
allPropertyNames, scratch))
3732+
/*allPropertyNames=*/nullptr, scratch))
37423733
return None;
37433734

37443735
/// Retrieve a replacement identifier.
@@ -3790,23 +3781,13 @@ Optional<Identifier> TypeChecker::omitNeedlessWords(VarDecl *var) {
37903781
while (auto optObjectTy = type->getAnyOptionalObjectType())
37913782
type = optObjectTy;
37923783

3793-
// Find the set of property names.
3794-
const InheritedNameSet *allPropertyNames = nullptr;
3795-
if (contextType) {
3796-
if (auto classDecl = contextType->getClassOrBoundGenericClass()) {
3797-
allPropertyNames = Context.getAllPropertyNames(classDecl,
3798-
var->isInstanceMember());
3799-
}
3800-
}
3801-
3802-
38033784
// Omit needless words.
38043785
StringScratchSpace scratch;
38053786
OmissionTypeName typeName = getTypeNameForOmission(var->getInterfaceType());
38063787
OmissionTypeName contextTypeName = getTypeNameForOmission(contextType);
38073788
if (::omitNeedlessWords(name, { }, "", typeName, contextTypeName, { },
3808-
/*returnsSelf=*/false, true, allPropertyNames,
3809-
scratch)) {
3789+
/*returnsSelf=*/false, true,
3790+
/*allPropertyNames=*/nullptr, scratch)) {
38103791
return Context.getIdentifier(name);
38113792
}
38123793

0 commit comments

Comments
 (0)