-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Import default public ctor of C++ foreign ref types as Swift Initializer #79986
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2529,3 +2529,131 @@ SwiftDeclSynthesizer::makeDefaultArgument(const clang::ParmVarDecl *param, | |
|
||
return callExpr; | ||
} | ||
|
||
// MARK: C++ foreign reference type constructors | ||
|
||
clang::CXXMethodDecl * | ||
SwiftDeclSynthesizer::synthesizeStaticFactoryForCXXForeignRef( | ||
const clang::CXXRecordDecl *cxxRecordDecl) { | ||
|
||
clang::ASTContext &clangCtx = cxxRecordDecl->getASTContext(); | ||
clang::Sema &clangSema = ImporterImpl.getClangSema(); | ||
|
||
clang::QualType cxxRecordTy = clangCtx.getRecordType(cxxRecordDecl); | ||
|
||
clang::CXXConstructorDecl *defaultCtorDecl = nullptr; | ||
for (clang::CXXConstructorDecl *ctor : cxxRecordDecl->ctors()) { | ||
if (ctor->parameters().empty() && !ctor->isDeleted() && | ||
ctor->getAccess() != clang::AS_private && | ||
ctor->getAccess() != clang::AS_protected) { | ||
defaultCtorDecl = ctor; | ||
break; | ||
} | ||
} | ||
if (!defaultCtorDecl) | ||
return nullptr; | ||
|
||
clang::FunctionDecl *operatorNew = nullptr; | ||
clang::FunctionDecl *operatorDelete = nullptr; | ||
bool passAlignment = false; | ||
bool findingAllocFuncFailed = clangSema.FindAllocationFunctions( | ||
cxxRecordDecl->getLocation(), clang::SourceRange(), clang::Sema::AFS_Both, | ||
clang::Sema::AFS_Both, cxxRecordTy, | ||
/*IsArray*/ false, passAlignment, clang::MultiExprArg(), operatorNew, | ||
operatorDelete, /*Diagnose*/ false); | ||
if (findingAllocFuncFailed || !operatorNew || operatorNew->isDeleted() || | ||
operatorNew->getAccess() == clang::AS_private || | ||
operatorNew->getAccess() == clang::AS_protected) | ||
return nullptr; | ||
|
||
clang::QualType cxxRecordPtrTy = clangCtx.getPointerType(cxxRecordTy); | ||
// Adding `_Nonnull` to the return type of synthesized static factory | ||
bool nullabilityCannotBeAdded = | ||
clangSema.CheckImplicitNullabilityTypeSpecifier( | ||
cxxRecordPtrTy, clang::NullabilityKind::NonNull, | ||
cxxRecordDecl->getLocation(), | ||
/*isParam=*/false, | ||
/*OverrideExisting=*/true); | ||
assert(!nullabilityCannotBeAdded && | ||
"Failed to add _Nonnull specifier to synthesized " | ||
"static factory's return type"); | ||
|
||
clang::IdentifierTable &clangIdents = clangCtx.Idents; | ||
clang::IdentifierInfo *funcNameToSynthesize = &clangIdents.get( | ||
("__returns_" + cxxRecordDecl->getNameAsString()).c_str()); | ||
clang::FunctionProtoType::ExtProtoInfo EPI; | ||
clang::QualType funcTypeToSynthesize = | ||
clangCtx.getFunctionType(cxxRecordPtrTy, {}, EPI); | ||
|
||
clang::CXXMethodDecl *synthesizedCxxMethodDecl = clang::CXXMethodDecl::Create( | ||
clangCtx, const_cast<clang::CXXRecordDecl *>(cxxRecordDecl), | ||
cxxRecordDecl->getLocation(), | ||
clang::DeclarationNameInfo(funcNameToSynthesize, | ||
cxxRecordDecl->getLocation()), | ||
funcTypeToSynthesize, | ||
clangCtx.getTrivialTypeSourceInfo(funcTypeToSynthesize), clang::SC_Static, | ||
/*UsesFPIntrin=*/false, /*isInline=*/true, | ||
clang::ConstexprSpecKind::Unspecified, cxxRecordDecl->getLocation()); | ||
assert(synthesizedCxxMethodDecl && | ||
"Unable to synthesize static factory for c++ foreign reference type"); | ||
synthesizedCxxMethodDecl->setAccess(clang::AccessSpecifier::AS_public); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above comment about not assuming (also, this is usually spelled |
||
|
||
if (!hasImmortalAttrs(cxxRecordDecl)) { | ||
clang::SwiftAttrAttr *returnsRetainedAttrForSynthesizedCxxMethodDecl = | ||
clang::SwiftAttrAttr::Create(clangCtx, "returns_retained"); | ||
synthesizedCxxMethodDecl->addAttr( | ||
returnsRetainedAttrForSynthesizedCxxMethodDecl); | ||
} | ||
|
||
clang::SwiftNameAttr *swiftNameInitAttrForSynthesizedCxxMethodDecl = | ||
clang::SwiftNameAttr::Create(clangCtx, "init()"); | ||
synthesizedCxxMethodDecl->addAttr( | ||
swiftNameInitAttrForSynthesizedCxxMethodDecl); | ||
|
||
clang::ExprResult synthesizedConstructExprResult = | ||
clangSema.BuildCXXConstructExpr( | ||
clang::SourceLocation(), cxxRecordTy, defaultCtorDecl, | ||
/*Elidable=*/false, clang::MultiExprArg(), | ||
/*HadMultipleCandidates=*/false, | ||
/*IsListInitialization=*/false, | ||
/*IsStdInitListInitialization=*/false, | ||
/*RequiresZeroInit=*/false, clang::CXXConstructionKind::Complete, | ||
clang::SourceRange()); | ||
assert(!synthesizedConstructExprResult.isInvalid() && | ||
"Unable to synthesize constructor expression for c++ foreign " | ||
"reference type"); | ||
clang::CXXConstructExpr *synthesizedConstructExpr = | ||
cast<clang::CXXConstructExpr>(synthesizedConstructExprResult.get()); | ||
|
||
clang::ExprResult synthesizedNewExprResult = clangSema.BuildCXXNew( | ||
clang::SourceRange(), /*UseGlobal=*/false, clang::SourceLocation(), {}, | ||
clang::SourceLocation(), clang::SourceRange(), cxxRecordTy, | ||
clangCtx.getTrivialTypeSourceInfo(cxxRecordTy), std::nullopt, | ||
clang::SourceRange(), synthesizedConstructExpr); | ||
assert( | ||
!synthesizedNewExprResult.isInvalid() && | ||
"Unable to synthesize `new` expression for c++ foreign reference type"); | ||
clang::CXXNewExpr *synthesizedNewExpr = | ||
cast<clang::CXXNewExpr>(synthesizedNewExprResult.get()); | ||
|
||
clang::ReturnStmt *synthesizedRetStmt = | ||
clang::ReturnStmt::Create(clangCtx, clang::SourceLocation(), | ||
synthesizedNewExpr, /*VarDecl=*/nullptr); | ||
assert(synthesizedRetStmt && "Unable to synthesize return statement for " | ||
"static factory of c++ foreign reference type"); | ||
|
||
clang::CompoundStmt *synthesizedFuncBody = clang::CompoundStmt::Create( | ||
clangCtx, {synthesizedRetStmt}, clang::FPOptionsOverride(), | ||
clang::SourceLocation(), clang::SourceLocation()); | ||
assert(synthesizedRetStmt && "Unable to synthesize function body for static " | ||
"factory of c++ foreign reference type"); | ||
|
||
synthesizedCxxMethodDecl->setBody(synthesizedFuncBody); | ||
synthesizedCxxMethodDecl->addAttr( | ||
clang::NoDebugAttr::CreateImplicit(clangCtx)); | ||
|
||
synthesizedCxxMethodDecl->setImplicit(); | ||
synthesizedCxxMethodDecl->setImplicitlyInline(); | ||
|
||
return synthesizedCxxMethodDecl; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that all the major concerns are addressed in this PR, I would even be OK to not have the feature flag and have this feature on by default for default constructors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per some discussions offline, we might want to invert this flag so users can opt out of this feature if it causes any trouble.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I seem like we can use the same feature flag to either enable or disable a feature.
Disable:
-disable-experimental-feature CXXForeignReferenceTypeInitializers
Enable:
-enable-experimental-feature CXXForeignReferenceTypeInitializers
I tested this at least for the features which are marked as
EXPERIMENTAL_FEATURE(<feature-name>, true)
I am not sure what would be the semantics of
EXPERIMENTAL_FEATURE(<feature-name>, false)
I am not sure what is the best thing to do to call this feature as ready. My inclination is towards completely removing the feature flag if it feels stable enough and we are happy with the test coverage.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the compilation error with operator new and immortal references is a blocker to turn this on by default. Until those are resolved there will be user code that is broken by this patch. I.e., it was compiling fine before but stopped compiling after.