Skip to content

Make objcImpl classes derive inherited inits #63534

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 3 commits into from
Feb 9, 2023
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
5 changes: 3 additions & 2 deletions lib/SILGen/SILGenConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,8 @@ emitMemberInit(SILGenFunction &SGF, VarDecl *selfDecl, Pattern *pattern) {
slot = SGF.B.createStructElementAddr(pattern, self.forward(SGF), field,
fieldTy.getAddressType());
} else {
assert(isa<ClassDecl>(field->getDeclContext()));
assert(isa<ClassDecl>(field->getDeclContext()->
getImplementedObjCContext()));
slot = SGF.B.createRefElementAddr(pattern, self.forward(SGF), field,
fieldTy.getAddressType());
}
Expand Down Expand Up @@ -1154,7 +1155,7 @@ void SILGenFunction::emitMemberInitializers(DeclContext *dc,
NominalTypeDecl *nominal) {
auto subs = getSubstitutionsForPropertyInitializer(dc, nominal);

for (auto member : nominal->getMembers()) {
for (auto member : nominal->getImplementationContext()->getMembers()) {
// Find instance pattern binding declarations that have initializers.
if (auto pbd = dyn_cast<PatternBindingDecl>(member)) {
if (pbd->isStatic()) continue;
Expand Down
40 changes: 27 additions & 13 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,26 @@ createDesignatedInitOverrideGenericParams(ASTContext &ctx,
ArrayRef<RequirementRepr>(), SourceLoc());
}

/// True if the type has an opaque clang implementation, meaning it is imported
/// and doesn't have an \c \@objcImplementation extension.
static bool hasClangImplementation(const NominalTypeDecl *decl) {
return decl->hasClangNode() && !decl->getObjCImplementationDecl();
}

/// True if \p member is in the main body of \p ty, where the "main body" is
/// either the type itself (the usual case) or its \c \@objcImplementation
/// extension (if one is present).
static bool isInMainBody(ValueDecl *member, NominalTypeDecl *ty) {
return member->getDeclContext() ==
ty->getImplementationContext()->getAsGenericContext();
}

static void
configureInheritedDesignatedInitAttributes(ClassDecl *classDecl,
ConstructorDecl *ctor,
ConstructorDecl *superclassCtor,
ASTContext &ctx) {
assert(ctor->getDeclContext() == classDecl);
assert(isInMainBody(ctor, classDecl));

AccessLevel access = classDecl->getFormalAccess();
access = std::max(access, AccessLevel::Internal);
Expand Down Expand Up @@ -856,6 +870,7 @@ createDesignatedInitOverride(ClassDecl *classDecl,

// Create the initializer declaration, inheriting the name,
// failability, and throws from the superclass initializer.
auto implCtx = classDecl->getImplementationContext()->getAsGenericContext();
auto ctor =
new (ctx) ConstructorDecl(superclassCtor->getName(),
classDecl->getBraces().Start,
Expand All @@ -865,8 +880,7 @@ createDesignatedInitOverride(ClassDecl *classDecl,
/*AsyncLoc=*/SourceLoc(),
/*Throws=*/superclassCtor->hasThrows(),
/*ThrowsLoc=*/SourceLoc(),
bodyParams, genericParams,
classDecl);
bodyParams, genericParams, implCtx);

ctor->setImplicit();

Expand Down Expand Up @@ -988,9 +1002,9 @@ static void diagnoseMissingRequiredInitializer(

bool AreAllStoredPropertiesDefaultInitableRequest::evaluate(
Evaluator &evaluator, NominalTypeDecl *decl) const {
assert(!decl->hasClangNode());
assert(!hasClangImplementation(decl));

for (auto member : decl->getMembers()) {
for (auto member : decl->getImplementationContext()->getMembers()) {
// If a stored property lacks an initial value and if there is no way to
// synthesize an initial value (e.g. for an optional) then we suppress
// generation of the default initializer.
Expand Down Expand Up @@ -1031,7 +1045,7 @@ bool AreAllStoredPropertiesDefaultInitableRequest::evaluate(

static bool areAllStoredPropertiesDefaultInitializable(Evaluator &eval,
NominalTypeDecl *decl) {
if (decl->hasClangNode())
if (hasClangImplementation(decl))
return true;

return evaluateOrDefault(
Expand All @@ -1041,11 +1055,11 @@ static bool areAllStoredPropertiesDefaultInitializable(Evaluator &eval,
bool
HasUserDefinedDesignatedInitRequest::evaluate(Evaluator &evaluator,
NominalTypeDecl *decl) const {
assert(!decl->hasClangNode());
assert(!hasClangImplementation(decl));

auto results = decl->lookupDirect(DeclBaseName::createConstructor());
for (auto *member : results) {
if (isa<ExtensionDecl>(member->getDeclContext()))
if (!isInMainBody(member, decl))
continue;

auto *ctor = cast<ConstructorDecl>(member);
Expand All @@ -1059,7 +1073,7 @@ HasUserDefinedDesignatedInitRequest::evaluate(Evaluator &evaluator,
static bool hasUserDefinedDesignatedInit(Evaluator &eval,
NominalTypeDecl *decl) {
// Imported decls don't have a designated initializer defined by the user.
if (decl->hasClangNode())
if (hasClangImplementation(decl))
return false;

return evaluateOrDefault(eval, HasUserDefinedDesignatedInitRequest{decl},
Expand All @@ -1086,7 +1100,7 @@ static void collectNonOveriddenSuperclassInits(

auto ctors = subclass->lookupDirect(DeclBaseName::createConstructor());
for (auto *member : ctors) {
if (isa<ExtensionDecl>(member->getDeclContext()))
if (!isInMainBody(member, subclass))
continue;

auto *ctor = cast<ConstructorDecl>(member);
Expand Down Expand Up @@ -1189,7 +1203,7 @@ static void addImplicitInheritedConstructorsToClass(ClassDecl *decl) {

auto results = decl->lookupDirect(DeclBaseName::createConstructor());
for (auto *member : results) {
if (isa<ExtensionDecl>(member->getDeclContext()))
if (!isInMainBody(member, decl))
continue;

auto *ctor = cast<ConstructorDecl>(member);
Expand Down Expand Up @@ -1217,7 +1231,7 @@ static void addImplicitInheritedConstructorsToClass(ClassDecl *decl) {

if (auto ctor = createDesignatedInitOverride(
decl, superclassCtor, kind, ctx)) {
decl->addMember(ctor);
decl->getImplementationContext()->addMember(ctor);
}
}
}
Expand Down Expand Up @@ -1258,7 +1272,7 @@ InheritsSuperclassInitializersRequest::evaluate(Evaluator &eval,

static bool shouldAttemptInitializerSynthesis(const NominalTypeDecl *decl) {
// Don't synthesize initializers for imported decls.
if (decl->hasClangNode())
if (hasClangImplementation(decl))
return false;

// Don't add implicit constructors in module interfaces.
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2737,7 +2737,7 @@ static ArrayRef<Decl *> evaluateMembersRequest(
return ctx.AllocateCopy(result);
}

auto nominal = dyn_cast<NominalTypeDecl>(idc);
auto nominal = dyn_cast<NominalTypeDecl>(dc->getImplementedObjCContext());

if (nominal) {
// We need to add implicit initializers because they
Expand Down
9 changes: 9 additions & 0 deletions test/IRGen/Inputs/objc_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@
- (void)noImplMethod:(int)param;

@end

@interface NoInitImplClass: NSObject

@property (readonly, strong, nonnull) NSString *s1;
@property (strong, nonnull) NSString *s2;
@property (readonly, strong, nonnull) NSString *s3;
@property (strong, nonnull) NSString *s4;

@end
Loading