Skip to content

Commit 2542a6d

Browse files
committed
fix(to_cpp1): emit template template parameters
1 parent 210b2a0 commit 2542a6d

7 files changed

+65
-6
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
@@ -2853,6 +2853,8 @@ struct declaration_node
28532853

28542854
auto is_function_expression () const -> bool
28552855
{ return is_function() && !identifier; }
2856+
auto is_template() const -> bool
2857+
{ return template_parameters != nullptr; }
28562858

28572859
auto is_polymorphic() const // has base types or virtual functions
28582860
-> bool

source/to_cpp1.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4129,7 +4129,8 @@ class cppfront
41294129
auto emit(
41304130
parameter_declaration_node const& n,
41314131
bool is_returns = false,
4132-
bool is_template_parameter = false
4132+
bool is_template_parameter = false,
4133+
bool emit_identifier = true
41334134
)
41344135
-> void
41354136
{ STACKINSTR
@@ -4234,13 +4235,27 @@ class cppfront
42344235
if (identifier == "_") {
42354236
printer.print_cpp2( "UnnamedTypeParam" + std::to_string(n.ordinal), identifier_pos );
42364237
}
4237-
else {
4238+
else if (emit_identifier) {
42384239
printer.print_cpp2( identifier, identifier_pos );
42394240
}
42404241

42414242
return;
42424243
}
42434244

4245+
//-----------------------------------------------------------------------
4246+
// Handle template parameters
4247+
4248+
if (n.declaration->is_template()) {
4249+
printer.print_cpp2("template ", identifier_pos);
4250+
emit(*n.declaration->template_parameters, is_returns, true, false, false);
4251+
printer.print_cpp2(" class", identifier_pos);
4252+
if (emit_identifier) {
4253+
printer.print_cpp2(" ", identifier_pos);
4254+
printer.print_cpp2( identifier, identifier_pos );
4255+
}
4256+
return;
4257+
}
4258+
42444259
//-----------------------------------------------------------------------
42454260
// Else handle template non-type parameters
42464261

@@ -4249,8 +4264,10 @@ class cppfront
42494264

42504265
if (is_template_parameter) {
42514266
emit( type_id );
4252-
printer.print_cpp2(" ", type_id.position());
4267+
if (emit_identifier) {
4268+
printer.print_cpp2(" ", type_id.position());
42534269
printer.print_cpp2( identifier, identifier_pos );
4270+
}
42544271
return;
42554272
}
42564273

@@ -4463,7 +4480,8 @@ class cppfront
44634480
parameter_declaration_list_node const& n,
44644481
bool is_returns = false,
44654482
bool is_template_parameter = false,
4466-
bool generating_postfix_inc_dec = false
4483+
bool generating_postfix_inc_dec = false,
4484+
bool emit_identifier = true
44674485
)
44684486
-> void
44694487
{ STACKINSTR
@@ -4492,7 +4510,7 @@ class cppfront
44924510
}
44934511
prev_pos = x->position();
44944512
assert(x);
4495-
emit(*x, is_returns, is_template_parameter);
4513+
emit(*x, is_returns, is_template_parameter, emit_identifier);
44964514
if (!x->declaration->has_name("this")) {
44974515
first = false;
44984516
}
@@ -4639,7 +4657,7 @@ class cppfront
46394657
);
46404658
}
46414659
else {
4642-
emit(*n.parameters, false, false, generating_postfix_inc_dec);
4660+
emit(*n.parameters, false, false, generating_postfix_inc_dec);
46434661
}
46444662

46454663
// For an anonymous function, the emitted lambda is 'constexpr' or 'mutable'

0 commit comments

Comments
 (0)