Skip to content

Commit 279ca08

Browse files
committed
fix(cpp1): emit template template parameters
1 parent efd185f commit 279ca08

12 files changed

+72
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
t: @struct <T: <_, _: _>> type = { }
2+
3+
u: @struct <T, V: _> type = { }
4+
5+
main: () = { _ = :t<u> = (); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 3723ede3cf5324827f8fbbe7f484c2ee4d7a7204)
2+
Target: x86_64-pc-linux-gnu
3+
Thread model: posix
4+
InstalledDir: /home/johel/root/clang-main/bin

regression-tests/test-results/clang-18/pure2-bugfix-for-non-local-function-expression.cpp.execution

Whitespace-only changes.

regression-tests/test-results/clang-18/pure2-bugfix-for-non-local-function-expression.cpp.output

Whitespace-only changes.

regression-tests/test-results/clang-18/pure2-bugfix-for-template-template-parameter.cpp.execution

Whitespace-only changes.

regression-tests/test-results/clang-18/pure2-bugfix-for-template-template-parameter.cpp.output

Whitespace-only changes.

regression-tests/test-results/clang-18/pure2-print.cpp.output

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sizeof(x) is 25
2+
(not a name)
3+
xyz
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
template<template <typename , auto> class T> class t;
10+
11+
template<typename T, auto V> class u;
12+
13+
14+
//=== Cpp2 type definitions and function declarations ===========================
15+
16+
template<template <typename , auto> class T> class t {};
17+
18+
template<typename T, auto V> class u {};
19+
20+
auto main() -> int;
21+
22+
23+
//=== Cpp2 function definitions =================================================
24+
25+
26+
#line 5 "pure2-bugfix-for-template-template-parameter.cpp2"
27+
auto main() -> int{static_cast<void>(t<u>{}); }
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-bugfix-for-template-template-parameter.cpp2... ok (all Cpp2, passes safety checks)
2+

source/cppfront.cpp

+28-7
Original file line numberDiff line numberDiff line change
@@ -3842,7 +3842,8 @@ class cppfront
38423842
auto emit(
38433843
parameter_declaration_node const& n,
38443844
bool is_returns = false,
3845-
bool is_template_parameter = false
3845+
bool is_template_parameter = false,
3846+
bool emit_identifier = true
38463847
)
38473848
-> void
38483849
{
@@ -3925,15 +3926,32 @@ class cppfront
39253926
// Handle type parameters
39263927

39273928
if (n.declaration->is_type()) {
3929+
assert (n.declaration->identifier);
39283930
printer.print_cpp2("typename ", n.declaration->identifier->position());
39293931
if (n.declaration->is_variadic) {
39303932
printer.print_cpp2(
39313933
"...",
39323934
n.declaration->identifier->position()
39333935
);
39343936
}
3937+
if (emit_identifier) {
3938+
emit(*n.declaration->identifier);
3939+
}
3940+
return;
3941+
}
3942+
3943+
//-----------------------------------------------------------------------
3944+
// Handle template parameters
3945+
3946+
if (n.declaration->is_template()) {
39353947
assert (n.declaration->identifier);
3936-
emit(*n.declaration->identifier);
3948+
printer.print_cpp2("template ", n.declaration->identifier->position());
3949+
emit(*n.declaration->template_parameters, is_returns, true, false);
3950+
printer.print_cpp2(" class", n.declaration->identifier->position());
3951+
if (emit_identifier) {
3952+
printer.print_cpp2(" ", n.declaration->identifier->position());
3953+
emit(*n.declaration->identifier);
3954+
}
39373955
return;
39383956
}
39393957

@@ -3945,9 +3963,11 @@ class cppfront
39453963

39463964
if (is_template_parameter) {
39473965
emit( type_id );
3948-
printer.print_cpp2(" ", type_id.position());
3949-
assert (n.declaration->identifier);
3950-
emit(*n.declaration->identifier);
3966+
if (emit_identifier) {
3967+
printer.print_cpp2(" ", type_id.position());
3968+
assert (n.declaration->identifier);
3969+
emit(*n.declaration->identifier);
3970+
}
39513971
return;
39523972
}
39533973

@@ -4149,7 +4169,8 @@ class cppfront
41494169
auto emit(
41504170
parameter_declaration_list_node const& n,
41514171
bool is_returns = false,
4152-
bool is_template_parameter = false
4172+
bool is_template_parameter = false,
4173+
bool emit_identifier = true
41534174
)
41544175
-> void
41554176
{
@@ -4177,7 +4198,7 @@ class cppfront
41774198
}
41784199
prev_pos = x->position();
41794200
assert(x);
4180-
emit(*x, is_returns, is_template_parameter);
4201+
emit(*x, is_returns, is_template_parameter, emit_identifier);
41814202
if (!x->declaration->has_name("this")) {
41824203
first = false;
41834204
}

source/parse.h

+2
Original file line numberDiff line numberDiff line change
@@ -2740,6 +2740,8 @@ struct declaration_node
27402740

27412741
auto is_function_expression () const -> bool
27422742
{ return is_function() && !identifier; }
2743+
auto is_template() const -> bool
2744+
{ return template_parameters != nullptr; }
27432745

27442746
auto is_polymorphic() const // has base types or virtual functions
27452747
-> bool

0 commit comments

Comments
 (0)