Skip to content

Commit

Permalink
Enter the function parameter mangling scope for a function encoding
Browse files Browse the repository at this point in the history
before mangling the template argument list.

With an abbreviated function template, the template parameters can
contain constraints that refer to the function parameters, so we need to
bring the function parameters into scope earlier.

Fixes #67356.
  • Loading branch information
zygoloid committed Sep 25, 2023
1 parent 0d7c340 commit c743986
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 15 additions & 1 deletion clang/lib/AST/ItaniumMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,17 @@ void CXXNameMangler::mangleFunctionEncoding(GlobalDecl GD) {

AbiTagList ReturnTypeAbiTags = makeFunctionReturnTypeTags(FD);
if (ReturnTypeAbiTags.empty()) {
// There are no tags for return type, the simplest case.
// There are no tags for return type, the simplest case. Enter the function
// parameter scope before mangling the name, because a template using
// constrained `auto` can have references to its parameters within its
// template argument list:
//
// template<typename T> void f(T x, C<decltype(x)> auto)
// ... is mangled as ...
// template<typename T, C<decltype(param 1)> U> void f(T, U)
FunctionTypeDepthState Saved = FunctionTypeDepth.push();
mangleName(GD);
FunctionTypeDepth.pop(Saved);
mangleFunctionEncodingBareType(FD);
return;
}
Expand All @@ -855,7 +864,10 @@ void CXXNameMangler::mangleFunctionEncoding(GlobalDecl GD) {
CXXNameMangler FunctionEncodingMangler(*this, FunctionEncodingStream);
// Output name of the function.
FunctionEncodingMangler.disableDerivedAbiTags();

FunctionTypeDepthState Saved = FunctionTypeDepth.push();
FunctionEncodingMangler.mangleNameWithAbiTags(FD, nullptr);
FunctionTypeDepth.pop(Saved);

// Remember length of the function name in the buffer.
size_t EncodingPositionStart = FunctionEncodingStream.str().size();
Expand All @@ -873,7 +885,9 @@ void CXXNameMangler::mangleFunctionEncoding(GlobalDecl GD) {
AdditionalAbiTags.end());

// Output name with implicit tags and function encoding from temporary buffer.
Saved = FunctionTypeDepth.push();
mangleNameWithAbiTags(FD, &AdditionalAbiTags);
FunctionTypeDepth.pop(Saved);
Out << FunctionEncodingStream.str().substr(EncodingPositionStart);

// Function encoding could create new substitutions so we have to add
Expand Down
12 changes: 12 additions & 0 deletions clang/test/CodeGenCXX/mangle-concept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,15 @@ namespace gh67244 {
// CHECK: define {{.*}} @_ZN7gh672441fITkNS_1CIifEEiEEvT_(
template void f(int);
}

namespace gh67356 {
template<typename, typename T> concept C = true;
template<typename T> void f(T t, C<decltype(t)> auto) {}
// CHECK: define {{.*}} @_ZN7gh673561fIiTkNS_1CIDtfL0p_EEEiEEvT_T0_(
template void f(int, int);

// Note, we use `fL0p` not `fp` above because:
template<typename T> void g(T t, C<auto (T u) -> decltype(f(t, u))> auto) {}
// CHECK: define {{.*}} @_ZN7gh673561gIiTkNS_1CIFDTcl1ffL0p_fp_EET_EEEiEEvS3_T0_(
template void g(int, int);
}

0 comments on commit c743986

Please sign in to comment.