Skip to content

Commit 95d9aa0

Browse files
committed
ClangImporter: Refactor importInheritedConstructors()
The lambda here was completely unnecessary.
1 parent c8f990b commit 95d9aa0

File tree

1 file changed

+88
-92
lines changed

1 file changed

+88
-92
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 88 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -7175,112 +7175,108 @@ void SwiftDeclConverter::importNonOverriddenMirroredMethods(DeclContext *dc,
71757175

71767176
void SwiftDeclConverter::importInheritedConstructors(
71777177
const ClassDecl *classDecl, SmallVectorImpl<Decl *> &newMembers) {
7178-
if (!classDecl->hasSuperclass())
7178+
auto superclassDecl = classDecl->getSuperclassDecl();
7179+
if (!superclassDecl)
7180+
return;
7181+
7182+
auto superclassClangDecl = superclassDecl->getClangDecl();
7183+
if (!superclassClangDecl ||
7184+
!isa<clang::ObjCInterfaceDecl>(superclassClangDecl))
71797185
return;
71807186

71817187
auto curObjCClass = cast<clang::ObjCInterfaceDecl>(classDecl->getClangDecl());
71827188

7183-
auto inheritConstructors = [&](TinyPtrVector<ValueDecl *> members,
7184-
Optional<CtorInitializerKind> kind) {
7185-
const auto &languageVersion =
7186-
Impl.SwiftContext.LangOpts.EffectiveLanguageVersion;
7189+
// The kind of initializer to import. If this class has designated
7190+
// initializers, everything it inherits is a convenience initializer.
7191+
Optional<CtorInitializerKind> kind;
7192+
if (curObjCClass->hasDesignatedInitializers())
7193+
kind = CtorInitializerKind::Convenience;
71877194

7188-
for (auto member : members) {
7189-
auto ctor = dyn_cast<ConstructorDecl>(member);
7190-
if (!ctor)
7191-
continue;
7195+
const auto &languageVersion =
7196+
Impl.SwiftContext.LangOpts.EffectiveLanguageVersion;
71927197

7193-
// Don't inherit compatibility stubs.
7194-
if (ctor->getAttrs().isUnavailableInSwiftVersion(languageVersion))
7195-
continue;
7198+
auto members = superclassDecl->lookupDirect(
7199+
DeclBaseName::createConstructor());
71967200

7197-
// Don't inherit (non-convenience) factory initializers.
7198-
// Note that convenience factories return instancetype and can be
7199-
// inherited.
7200-
switch (ctor->getInitKind()) {
7201-
case CtorInitializerKind::Factory:
7202-
continue;
7203-
case CtorInitializerKind::ConvenienceFactory:
7204-
case CtorInitializerKind::Convenience:
7205-
case CtorInitializerKind::Designated:
7206-
break;
7207-
}
7201+
for (auto member : members) {
7202+
auto ctor = dyn_cast<ConstructorDecl>(member);
7203+
if (!ctor)
7204+
continue;
72087205

7209-
auto objcMethod =
7210-
dyn_cast_or_null<clang::ObjCMethodDecl>(ctor->getClangDecl());
7211-
if (!objcMethod)
7212-
continue;
7206+
// Don't inherit compatibility stubs.
7207+
if (ctor->getAttrs().isUnavailableInSwiftVersion(languageVersion))
7208+
continue;
72137209

7214-
auto &clangSourceMgr = Impl.getClangASTContext().getSourceManager();
7215-
clang::PrettyStackTraceDecl trace(objcMethod, clang::SourceLocation(),
7216-
clangSourceMgr,
7217-
"importing (inherited)");
7218-
7219-
// If this initializer came from a factory method, inherit
7220-
// it as an initializer.
7221-
if (objcMethod->isClassMethod()) {
7222-
assert(ctor->getInitKind() == CtorInitializerKind::ConvenienceFactory);
7223-
7224-
Optional<ImportedName> correctSwiftName;
7225-
ImportedName importedName =
7226-
importFullName(objcMethod, correctSwiftName);
7227-
assert(
7228-
!correctSwiftName &&
7229-
"Import inherited initializers never references correctSwiftName");
7230-
importedName.setHasCustomName();
7231-
bool redundant;
7232-
if (auto newCtor =
7233-
importConstructor(objcMethod, classDecl,
7234-
/*implicit=*/true, ctor->getInitKind(),
7235-
/*required=*/false, ctor->getObjCSelector(),
7236-
importedName, objcMethod->parameters(),
7237-
objcMethod->isVariadic(), redundant)) {
7238-
// If this is a compatibility stub, mark it as such.
7239-
if (correctSwiftName)
7240-
markAsVariant(newCtor, *correctSwiftName);
7241-
7242-
Impl.importAttributes(objcMethod, newCtor, curObjCClass);
7243-
newMembers.push_back(newCtor);
7244-
}
7245-
continue;
7246-
}
7210+
// Don't inherit (non-convenience) factory initializers.
7211+
// Note that convenience factories return instancetype and can be
7212+
// inherited.
7213+
switch (ctor->getInitKind()) {
7214+
case CtorInitializerKind::Factory:
7215+
continue;
7216+
case CtorInitializerKind::ConvenienceFactory:
7217+
case CtorInitializerKind::Convenience:
7218+
case CtorInitializerKind::Designated:
7219+
break;
7220+
}
72477221

7248-
// Figure out what kind of constructor this will be.
7249-
CtorInitializerKind myKind;
7250-
bool isRequired = false;
7251-
if (ctor->isRequired()) {
7252-
// Required initializers are always considered designated.
7253-
isRequired = true;
7254-
myKind = CtorInitializerKind::Designated;
7255-
} else if (kind) {
7256-
myKind = *kind;
7257-
} else {
7258-
myKind = ctor->getInitKind();
7259-
}
7222+
auto objcMethod =
7223+
dyn_cast_or_null<clang::ObjCMethodDecl>(ctor->getClangDecl());
7224+
if (!objcMethod)
7225+
continue;
72607226

7261-
// Import the constructor into this context.
7227+
auto &clangSourceMgr = Impl.getClangASTContext().getSourceManager();
7228+
clang::PrettyStackTraceDecl trace(objcMethod, clang::SourceLocation(),
7229+
clangSourceMgr,
7230+
"importing (inherited)");
7231+
7232+
// If this initializer came from a factory method, inherit
7233+
// it as an initializer.
7234+
if (objcMethod->isClassMethod()) {
7235+
assert(ctor->getInitKind() == CtorInitializerKind::ConvenienceFactory);
7236+
7237+
Optional<ImportedName> correctSwiftName;
7238+
ImportedName importedName =
7239+
importFullName(objcMethod, correctSwiftName);
7240+
assert(
7241+
!correctSwiftName &&
7242+
"Import inherited initializers never references correctSwiftName");
7243+
importedName.setHasCustomName();
7244+
bool redundant;
72627245
if (auto newCtor =
72637246
importConstructor(objcMethod, classDecl,
7264-
/*implicit=*/true, myKind, isRequired)) {
7247+
/*implicit=*/true, ctor->getInitKind(),
7248+
/*required=*/false, ctor->getObjCSelector(),
7249+
importedName, objcMethod->parameters(),
7250+
objcMethod->isVariadic(), redundant)) {
7251+
// If this is a compatibility stub, mark it as such.
7252+
if (correctSwiftName)
7253+
markAsVariant(newCtor, *correctSwiftName);
7254+
72657255
Impl.importAttributes(objcMethod, newCtor, curObjCClass);
72667256
newMembers.push_back(newCtor);
72677257
}
7258+
continue;
72687259
}
7269-
};
7270-
7271-
// The kind of initializer to import. If this class has designated
7272-
// initializers, everything it inherits is a convenience initializer.
7273-
Optional<CtorInitializerKind> kind;
7274-
if (curObjCClass->hasDesignatedInitializers())
7275-
kind = CtorInitializerKind::Convenience;
72767260

7261+
// Figure out what kind of constructor this will be.
7262+
CtorInitializerKind myKind;
7263+
bool isRequired = false;
7264+
if (ctor->isRequired()) {
7265+
// Required initializers are always considered designated.
7266+
isRequired = true;
7267+
myKind = CtorInitializerKind::Designated;
7268+
} else if (kind) {
7269+
myKind = *kind;
7270+
} else {
7271+
myKind = ctor->getInitKind();
7272+
}
72777273

7278-
// If we have a superclass, import from it.
7279-
auto superclass = classDecl->getSuperclassDecl();
7280-
if (auto superclassClangDecl = superclass->getClangDecl()) {
7281-
if (isa<clang::ObjCInterfaceDecl>(superclassClangDecl)) {
7282-
inheritConstructors(superclass->lookupDirect(DeclBaseName::createConstructor()),
7283-
kind);
7274+
// Import the constructor into this context.
7275+
if (auto newCtor =
7276+
importConstructor(objcMethod, classDecl,
7277+
/*implicit=*/true, myKind, isRequired)) {
7278+
Impl.importAttributes(objcMethod, newCtor, curObjCClass);
7279+
newMembers.push_back(newCtor);
72847280
}
72857281
}
72867282
}
@@ -8660,11 +8656,11 @@ void ClangImporter::Implementation::insertMembersAndAlternates(
86608656
void ClangImporter::Implementation::importInheritedConstructors(
86618657
const clang::ObjCInterfaceDecl *curObjCClass,
86628658
const ClassDecl *classDecl, SmallVectorImpl<Decl *> &newMembers) {
8663-
if (curObjCClass->getName() != "Protocol") {
8664-
SwiftDeclConverter converter(*this, CurrentVersion);
8665-
converter.importInheritedConstructors(classDecl, newMembers);
8666-
}
8667-
}
8659+
if (curObjCClass->getName() != "Protocol") {
8660+
SwiftDeclConverter converter(*this, CurrentVersion);
8661+
converter.importInheritedConstructors(classDecl, newMembers);
8662+
}
8663+
}
86688664

86698665
void ClangImporter::Implementation::collectMembersToAdd(
86708666
const clang::ObjCContainerDecl *objcContainer, Decl *D, DeclContext *DC,

0 commit comments

Comments
 (0)