-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Support nested templates for conditional escapability #77678
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 |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
#include "swift/AST/NameLookupRequests.h" | ||
#include "swift/AST/PrettyStackTrace.h" | ||
#include "swift/AST/SourceFile.h" | ||
#include "swift/AST/Type.h" | ||
#include "swift/AST/TypeCheckRequests.h" | ||
#include "swift/AST/Types.h" | ||
#include "swift/Basic/Assertions.h" | ||
|
@@ -51,6 +52,7 @@ | |
#include "swift/Strings.h" | ||
#include "swift/Subsystems.h" | ||
#include "clang/AST/ASTContext.h" | ||
#include "clang/AST/DeclBase.h" | ||
#include "clang/AST/DeclCXX.h" | ||
#include "clang/AST/DeclTemplate.h" | ||
#include "clang/AST/Mangle.h" | ||
|
@@ -5080,31 +5082,41 @@ ClangTypeEscapability::evaluate(Evaluator &evaluator, | |
importer::getConditionalEscapableAttrParams(recordDecl); | ||
if (!conditionalParams.empty()) { | ||
auto specDecl = cast<clang::ClassTemplateSpecializationDecl>(recordDecl); | ||
auto templateDecl = specDecl->getSpecializedTemplate(); | ||
SmallVector<std::pair<unsigned, StringRef>, 4> argumentsToCheck; | ||
for (auto [idx, param] : | ||
llvm::enumerate(*templateDecl->getTemplateParameters())) { | ||
if (conditionalParams.erase(param->getName())) | ||
argumentsToCheck.push_back(std::make_pair(idx, param->getName())); | ||
} | ||
HeaderLoc loc{recordDecl->getLocation()}; | ||
for (auto name : conditionalParams) | ||
desc.impl.diagnose(loc, diag::unknown_template_parameter, name); | ||
|
||
auto &argList = specDecl->getTemplateArgs(); | ||
for (auto argToCheck : argumentsToCheck) { | ||
auto arg = argList[argToCheck.first]; | ||
if (arg.getKind() != clang::TemplateArgument::Type) { | ||
desc.impl.diagnose(loc, diag::type_template_parameter_expected, | ||
argToCheck.second); | ||
return CxxEscapability::Unknown; | ||
while (specDecl) { | ||
auto templateDecl = specDecl->getSpecializedTemplate(); | ||
for (auto [idx, param] : | ||
llvm::enumerate(*templateDecl->getTemplateParameters())) { | ||
if (conditionalParams.erase(param->getName())) | ||
argumentsToCheck.push_back(std::make_pair(idx, param->getName())); | ||
} | ||
auto &argList = specDecl->getTemplateArgs(); | ||
for (auto argToCheck : argumentsToCheck) { | ||
auto arg = argList[argToCheck.first]; | ||
if (arg.getKind() != clang::TemplateArgument::Type) { | ||
desc.impl.diagnose(loc, diag::type_template_parameter_expected, | ||
argToCheck.second); | ||
return CxxEscapability::Unknown; | ||
} | ||
|
||
auto argEscapability = evaluateEscapability( | ||
arg.getAsType()->getUnqualifiedDesugaredType()); | ||
if (argEscapability == CxxEscapability::NonEscapable) | ||
return CxxEscapability::NonEscapable; | ||
auto argEscapability = evaluateEscapability( | ||
arg.getAsType()->getUnqualifiedDesugaredType()); | ||
if (argEscapability == CxxEscapability::NonEscapable) | ||
return CxxEscapability::NonEscapable; | ||
} | ||
clang::DeclContext *rec = specDecl; | ||
specDecl = nullptr; | ||
while ((rec = rec->getParent())) { | ||
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. Should this loop stop once 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. When 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 see, that makes sense. |
||
specDecl = dyn_cast<clang::ClassTemplateSpecializationDecl>(rec); | ||
if (specDecl) | ||
break; | ||
} | ||
} | ||
|
||
for (auto name : conditionalParams) | ||
desc.impl.diagnose(loc, diag::unknown_template_parameter, name); | ||
|
||
return hadUnknown ? CxxEscapability::Unknown : CxxEscapability::Escapable; | ||
} | ||
if (desc.annotationOnly) | ||
|
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'm wondering if this would work with variadic templates, e.g.
std::tuple
? But that's out of scope of this patch 🙂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.
Good point! I think this would not work for that. I'd address that in a follow-up PR.