Skip to content

Commit 03b6778

Browse files
committed
fix(to_cpp1): emit template template parameters
1 parent 05ce45a commit 03b6778

7 files changed

+64
-5
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
#define CPP2_IMPORT_STD Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
#line 1 "pure2-bugfix-for-template-template-parameter.cpp2"
10+
template<template <typename UnnamedTypeParam1, auto> class T> class t;
11+
#line 2 "pure2-bugfix-for-template-template-parameter.cpp2"
12+
13+
template<typename T, auto V> class u;
14+
15+
16+
//=== Cpp2 type definitions and function declarations ===========================
17+
18+
#line 1 "pure2-bugfix-for-template-template-parameter.cpp2"
19+
template<template <typename UnnamedTypeParam1, auto> class T> class t {};
20+
#line 2 "pure2-bugfix-for-template-template-parameter.cpp2"
21+
22+
template<typename T, auto V> class u {};
23+
24+
auto main() -> int;
25+
26+
//=== Cpp2 function definitions =================================================
27+
28+
#line 1 "pure2-bugfix-for-template-template-parameter.cpp2"
29+
30+
#line 5 "pure2-bugfix-for-template-template-parameter.cpp2"
31+
auto main() -> int{static_cast<void>(t<u>{}); }
32+
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
@@ -2841,6 +2841,8 @@ struct declaration_node
28412841

28422842
auto is_function_expression () const -> bool
28432843
{ return is_function() && !identifier; }
2844+
auto is_template() const -> bool
2845+
{ return template_parameters != nullptr; }
28442846

28452847
auto is_polymorphic() const // has base types or virtual functions
28462848
-> bool

source/to_cpp1.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,7 +4122,8 @@ class cppfront
41224122
auto emit(
41234123
parameter_declaration_node const& n,
41244124
bool is_returns = false,
4125-
bool is_template_parameter = false
4125+
bool is_template_parameter = false,
4126+
bool emit_identifier = true
41264127
)
41274128
-> void
41284129
{ STACKINSTR
@@ -4227,13 +4228,27 @@ class cppfront
42274228
if (identifier == "_") {
42284229
printer.print_cpp2( "UnnamedTypeParam" + std::to_string(n.ordinal), identifier_pos );
42294230
}
4230-
else {
4231+
else if (emit_identifier) {
42314232
printer.print_cpp2( identifier, identifier_pos );
42324233
}
42334234

42344235
return;
42354236
}
42364237

4238+
//-----------------------------------------------------------------------
4239+
// Handle template parameters
4240+
4241+
if (n.declaration->is_template()) {
4242+
printer.print_cpp2("template ", identifier_pos);
4243+
emit(*n.declaration->template_parameters, is_returns, true, false);
4244+
printer.print_cpp2(" class", identifier_pos);
4245+
if (emit_identifier) {
4246+
printer.print_cpp2(" ", identifier_pos);
4247+
printer.print_cpp2( identifier, identifier_pos );
4248+
}
4249+
return;
4250+
}
4251+
42374252
//-----------------------------------------------------------------------
42384253
// Else handle template non-type parameters
42394254

@@ -4242,8 +4257,10 @@ class cppfront
42424257

42434258
if (is_template_parameter) {
42444259
emit( type_id );
4245-
printer.print_cpp2(" ", type_id.position());
4260+
if (emit_identifier) {
4261+
printer.print_cpp2(" ", type_id.position());
42464262
printer.print_cpp2( identifier, identifier_pos );
4263+
}
42474264
return;
42484265
}
42494266

@@ -4455,7 +4472,8 @@ class cppfront
44554472
auto emit(
44564473
parameter_declaration_list_node const& n,
44574474
bool is_returns = false,
4458-
bool is_template_parameter = false
4475+
bool is_template_parameter = false,
4476+
bool emit_identifier = true
44594477
)
44604478
-> void
44614479
{ STACKINSTR
@@ -4483,7 +4501,7 @@ class cppfront
44834501
}
44844502
prev_pos = x->position();
44854503
assert(x);
4486-
emit(*x, is_returns, is_template_parameter);
4504+
emit(*x, is_returns, is_template_parameter, emit_identifier);
44874505
if (!x->declaration->has_name("this")) {
44884506
first = false;
44894507
}

0 commit comments

Comments
 (0)