Skip to content

Commit e6a6ecd

Browse files
committed
fix(cpp1): emit template template parameters
1 parent 083c8a0 commit e6a6ecd

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/cppfront.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,7 +3889,8 @@ class cppfront
38893889
auto emit(
38903890
parameter_declaration_node const& n,
38913891
bool is_returns = false,
3892-
bool is_template_parameter = false
3892+
bool is_template_parameter = false,
3893+
bool emit_identifier = true
38933894
)
38943895
-> void
38953896
{
@@ -3972,15 +3973,32 @@ class cppfront
39723973
// Handle type parameters
39733974

39743975
if (n.declaration->is_type()) {
3976+
assert (n.declaration->identifier);
39753977
printer.print_cpp2("typename ", n.declaration->identifier->position());
39763978
if (n.declaration->is_variadic) {
39773979
printer.print_cpp2(
39783980
"...",
39793981
n.declaration->identifier->position()
39803982
);
39813983
}
3984+
if (emit_identifier) {
3985+
emit(*n.declaration->identifier);
3986+
}
3987+
return;
3988+
}
3989+
3990+
//-----------------------------------------------------------------------
3991+
// Handle template parameters
3992+
3993+
if (n.declaration->is_template()) {
39823994
assert (n.declaration->identifier);
3983-
emit(*n.declaration->identifier);
3995+
printer.print_cpp2("template ", n.declaration->identifier->position());
3996+
emit(*n.declaration->template_parameters, is_returns, true, false);
3997+
printer.print_cpp2(" class", n.declaration->identifier->position());
3998+
if (emit_identifier) {
3999+
printer.print_cpp2(" ", n.declaration->identifier->position());
4000+
emit(*n.declaration->identifier);
4001+
}
39844002
return;
39854003
}
39864004

@@ -3992,9 +4010,11 @@ class cppfront
39924010

39934011
if (is_template_parameter) {
39944012
emit( type_id );
3995-
printer.print_cpp2(" ", type_id.position());
3996-
assert (n.declaration->identifier);
3997-
emit(*n.declaration->identifier);
4013+
if (emit_identifier) {
4014+
printer.print_cpp2(" ", type_id.position());
4015+
assert (n.declaration->identifier);
4016+
emit(*n.declaration->identifier);
4017+
}
39984018
return;
39994019
}
40004020

@@ -4208,7 +4228,8 @@ class cppfront
42084228
auto emit(
42094229
parameter_declaration_list_node const& n,
42104230
bool is_returns = false,
4211-
bool is_template_parameter = false
4231+
bool is_template_parameter = false,
4232+
bool emit_identifier = true
42124233
)
42134234
-> void
42144235
{
@@ -4236,7 +4257,7 @@ class cppfront
42364257
}
42374258
prev_pos = x->position();
42384259
assert(x);
4239-
emit(*x, is_returns, is_template_parameter);
4260+
emit(*x, is_returns, is_template_parameter, emit_identifier);
42404261
if (!x->declaration->has_name("this")) {
42414262
first = false;
42424263
}

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

0 commit comments

Comments
 (0)