-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[cxx-interop] Support templated constructors. #34424
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
zoecarver
merged 2 commits into
swiftlang:main
from
zoecarver:cxx/templated-constructors
Nov 11, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,12 +107,19 @@ Solution::computeSubstitutions(GenericSignature sig, | |
| } | ||
|
|
||
| static ConcreteDeclRef generateDeclRefForSpecializedCXXFunctionTemplate( | ||
| ASTContext &ctx, FuncDecl *oldDecl, SubstitutionMap subst, | ||
| ASTContext &ctx, AbstractFunctionDecl *oldDecl, SubstitutionMap subst, | ||
| clang::FunctionDecl *specialized) { | ||
| // Create a new ParameterList with the substituted type. | ||
| auto oldFnType = | ||
| cast<GenericFunctionType>(oldDecl->getInterfaceType().getPointer()); | ||
| auto newFnType = oldFnType->substGenericArgs(subst); | ||
| // The constructor type is a function type as follows: | ||
| // (CType.Type) -> (Generic) -> CType | ||
| // But we only want the result of that function type because that is the | ||
| // function type with the generic params that need to be substituted: | ||
| // (Generic) -> CType | ||
| if (isa<ConstructorDecl>(oldDecl)) | ||
| newFnType = cast<FunctionType>(newFnType->getResult().getPointer()); | ||
| SmallVector<ParamDecl *, 4> newParams; | ||
| unsigned i = 0; | ||
| for (auto paramTy : newFnType->getParams()) { | ||
|
|
@@ -126,6 +133,16 @@ static ConcreteDeclRef generateDeclRefForSpecializedCXXFunctionTemplate( | |
| auto *newParamList = | ||
| ParameterList::create(ctx, SourceLoc(), newParams, SourceLoc()); | ||
|
|
||
| if (isa<ConstructorDecl>(oldDecl)) { | ||
| DeclName ctorName(ctx, DeclBaseName::createConstructor(), newParamList); | ||
| auto newCtorDecl = ConstructorDecl::createImported( | ||
| ctx, specialized, ctorName, oldDecl->getLoc(), /*failable=*/false, | ||
| /*failabilityLoc=*/SourceLoc(), /*throws=*/false, | ||
| /*throwsLoc=*/SourceLoc(), newParamList, /*genericParams=*/nullptr, | ||
| oldDecl->getDeclContext()); | ||
| return ConcreteDeclRef(newCtorDecl); | ||
| } | ||
|
|
||
| // Generate a name for the specialized function. | ||
| std::string newNameStr; | ||
| llvm::raw_string_ostream buffer(newNameStr); | ||
|
||
|
|
@@ -139,8 +156,8 @@ static ConcreteDeclRef generateDeclRefForSpecializedCXXFunctionTemplate( | |
|
|
||
| auto newFnDecl = FuncDecl::createImported( | ||
| ctx, oldDecl->getLoc(), newName, oldDecl->getNameLoc(), | ||
| /*Async*/ false, oldDecl->hasThrows(), newParamList, | ||
| newFnType->getResult(), /*GenericParams*/ nullptr, | ||
| /*Async=*/false, oldDecl->hasThrows(), newParamList, | ||
| newFnType->getResult(), /*GenericParams=*/nullptr, | ||
| oldDecl->getDeclContext(), specialized); | ||
| return ConcreteDeclRef(newFnDecl); | ||
| } | ||
|
|
@@ -168,7 +185,7 @@ Solution::resolveConcreteDeclRef(ValueDecl *decl, | |
| cast<clang::FunctionTemplateDecl>(decl->getClangDecl())), | ||
| subst); | ||
| return generateDeclRefForSpecializedCXXFunctionTemplate( | ||
| decl->getASTContext(), cast<FuncDecl>(decl), subst, newFn); | ||
| decl->getASTContext(), cast<AbstractFunctionDecl>(decl), subst, newFn); | ||
| } | ||
|
|
||
| return ConcreteDeclRef(decl, subst); | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.