Skip to content

Commit 511b67f

Browse files
committed
fix(to_cpp1): emit template template parameters
1 parent 61550a5 commit 511b67f

7 files changed

+65
-7
lines changed
Lines changed: 5 additions & 0 deletions
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> = (); }

regression-tests/test-results/gcc-13/pure2-bugfix-for-template-template-parameter.cpp.execution

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-bugfix-for-template-template-parameter.cpp.output

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
#define CPP2_IMPORT_STD 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+
Lines changed: 2 additions & 0 deletions
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/parse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,6 +2822,8 @@ struct declaration_node
28222822

28232823
auto is_function_expression () const -> bool
28242824
{ return is_function() && !identifier; }
2825+
auto is_template() const -> bool
2826+
{ return template_parameters != nullptr; }
28252827

28262828
auto is_polymorphic() const // has base types or virtual functions
28272829
-> bool

source/to_cpp1.h

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3892,7 +3892,8 @@ class cppfront
38923892
auto emit(
38933893
parameter_declaration_node const& n,
38943894
bool is_returns = false,
3895-
bool is_template_parameter = false
3895+
bool is_template_parameter = false,
3896+
bool emit_identifier = true
38963897
)
38973898
-> void
38983899
{
@@ -3975,15 +3976,32 @@ class cppfront
39753976
// Handle type parameters
39763977

39773978
if (n.declaration->is_type()) {
3979+
assert (n.declaration->identifier);
39783980
printer.print_cpp2("typename ", n.declaration->identifier->position());
39793981
if (n.declaration->is_variadic) {
39803982
printer.print_cpp2(
39813983
"...",
39823984
n.declaration->identifier->position()
39833985
);
39843986
}
3987+
if (emit_identifier) {
3988+
emit(*n.declaration->identifier);
3989+
}
3990+
return;
3991+
}
3992+
3993+
//-----------------------------------------------------------------------
3994+
// Handle template parameters
3995+
3996+
if (n.declaration->is_template()) {
39853997
assert (n.declaration->identifier);
3986-
emit(*n.declaration->identifier);
3998+
printer.print_cpp2("template ", n.declaration->identifier->position());
3999+
emit(*n.declaration->template_parameters, is_returns, true, false);
4000+
printer.print_cpp2(" class", n.declaration->identifier->position());
4001+
if (emit_identifier) {
4002+
printer.print_cpp2(" ", n.declaration->identifier->position());
4003+
emit(*n.declaration->identifier);
4004+
}
39874005
return;
39884006
}
39894007

@@ -3995,9 +4013,11 @@ class cppfront
39954013

39964014
if (is_template_parameter) {
39974015
emit( type_id );
3998-
printer.print_cpp2(" ", type_id.position());
3999-
assert (n.declaration->identifier);
4000-
emit(*n.declaration->identifier);
4016+
if (emit_identifier) {
4017+
printer.print_cpp2(" ", type_id.position());
4018+
assert (n.declaration->identifier);
4019+
emit(*n.declaration->identifier);
4020+
}
40014021
return;
40024022
}
40034023

@@ -4211,7 +4231,8 @@ class cppfront
42114231
auto emit(
42124232
parameter_declaration_list_node const& n,
42134233
bool is_returns = false,
4214-
bool is_template_parameter = false
4234+
bool is_template_parameter = false,
4235+
bool emit_identifier = true
42154236
)
42164237
-> void
42174238
{
@@ -4239,7 +4260,7 @@ class cppfront
42394260
}
42404261
prev_pos = x->position();
42414262
assert(x);
4242-
emit(*x, is_returns, is_template_parameter);
4263+
emit(*x, is_returns, is_template_parameter, emit_identifier);
42434264
if (!x->declaration->has_name("this")) {
42444265
first = false;
42454266
}

0 commit comments

Comments
 (0)