-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Support function templates. #33053
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,9 @@ | |
#include "swift/AST/ClangModuleLoader.h" | ||
#include "swift/AST/DiagnosticEngine.h" | ||
#include "swift/AST/DiagnosticsClangImporter.h" | ||
#include "swift/AST/ImportCache.h" | ||
#include "swift/AST/DiagnosticsSema.h" | ||
#include "swift/AST/IRGenOptions.h" | ||
#include "swift/AST/ImportCache.h" | ||
#include "swift/AST/LinkLibrary.h" | ||
#include "swift/AST/Module.h" | ||
#include "swift/AST/NameLookup.h" | ||
|
@@ -35,8 +36,6 @@ | |
#include "swift/ClangImporter/ClangModule.h" | ||
#include "swift/Config.h" | ||
#include "swift/Demangling/Demangle.h" | ||
#include "swift/ClangImporter/ClangModule.h" | ||
#include "swift/Config.h" | ||
#include "swift/Parse/Lexer.h" | ||
#include "swift/Parse/Parser.h" | ||
#include "swift/Strings.h" | ||
|
@@ -4082,3 +4081,32 @@ swift::getModuleCachePathFromClang(const clang::CompilerInstance &Clang) { | |
return llvm::sys::path::parent_path(SpecificModuleCachePath).str(); | ||
} | ||
|
||
clang::FunctionDecl *ClangImporter::instantiateCXXFunctionTemplate( | ||
ASTContext &ctx, clang::FunctionTemplateDecl *func, SubstitutionMap subst) { | ||
SmallVector<clang::TemplateArgument, 4> templateSubst; | ||
std::unique_ptr<TemplateInstantiationError> error = | ||
ctx.getClangTemplateArguments(func->getTemplateParameters(), | ||
subst.getReplacementTypes(), templateSubst); | ||
if (error) { | ||
std::string failedTypesStr; | ||
llvm::raw_string_ostream failedTypesStrStream(failedTypesStr); | ||
llvm::interleaveComma(error->failedTypes, failedTypesStrStream); | ||
// TODO: Use the location of the apply here. | ||
// TODO: This error message should not reference implementation details. | ||
// See: https://github.com/apple/swift/pull/33053#discussion_r477003350 | ||
ctx.Diags.diagnose(SourceLoc(), | ||
diag::unable_to_convert_generic_swift_types.ID, | ||
{func->getName(), StringRef(failedTypesStr)}); | ||
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. @varungandhi-apple can I get your opinion on error handling again here? The issue is, on Windows, clang will crash if we don't exit here (because of an assertion:
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. [I only briefly skimmed the Clang code...] The assertion you pointed out is in 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. Thanks, your comment made me realize, I think the issue is completely my fault. What we need to do is return I think name mangling probably happens when we try to instantiate the function template. 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. Ah. That's not what I was talking about (and I totally missed that we didn't return here in case of an error)... but glad that it helped you figured out the issue. 😅 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. All is well that ends well :) Now let's just hope the build succeeds on Windows. |
||
// Return a valid FunctionDecl but, we'll never use it. | ||
return func->getAsFunction(); | ||
} | ||
|
||
// Instanciate a specialization of this template using the substitution map. | ||
auto *templateArgList = clang::TemplateArgumentList::CreateCopy( | ||
func->getASTContext(), templateSubst); | ||
auto &sema = getClangInstance().getSema(); | ||
auto *spec = sema.InstantiateFunctionDeclaration(func, templateArgList, | ||
clang::SourceLocation()); | ||
sema.InstantiateFunctionDefinition(clang::SourceLocation(), spec); | ||
return spec; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.