Skip to content

Commit 8456825

Browse files
committed
fix(to_cpp1): emit template template parameters
1 parent 0b333f3 commit 8456825

7 files changed

+60
-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: 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
@@ -2824,6 +2824,8 @@ struct declaration_node
28242824

28252825
auto is_function_expression () const -> bool
28262826
{ return is_function() && !identifier; }
2827+
auto is_template() const -> bool
2828+
{ return template_parameters != nullptr; }
28272829

28282830
auto is_polymorphic() const // has base types or virtual functions
28292831
-> bool

source/to_cpp1.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3933,7 +3933,8 @@ class cppfront
39333933
auto emit(
39343934
parameter_declaration_node const& n,
39353935
bool is_returns = false,
3936-
bool is_template_parameter = false
3936+
bool is_template_parameter = false,
3937+
bool emit_identifier = true
39373938
)
39383939
-> void
39393940
{
@@ -4036,13 +4037,27 @@ class cppfront
40364037
if (identifier == "_") {
40374038
printer.print_cpp2( "UnnamedTypeParam" + std::to_string(n.ordinal), identifier_pos );
40384039
}
4039-
else {
4040+
else if (emit_identifier) {
40404041
printer.print_cpp2( identifier, identifier_pos );
40414042
}
40424043

40434044
return;
40444045
}
40454046

4047+
//-----------------------------------------------------------------------
4048+
// Handle template parameters
4049+
4050+
if (n.declaration->is_template()) {
4051+
printer.print_cpp2("template ", identifier_pos);
4052+
emit(*n.declaration->template_parameters, is_returns, true, false);
4053+
printer.print_cpp2(" class", identifier_pos);
4054+
if (emit_identifier) {
4055+
printer.print_cpp2(" ", identifier_pos);
4056+
printer.print_cpp2( identifier, identifier_pos );
4057+
}
4058+
return;
4059+
}
4060+
40464061
//-----------------------------------------------------------------------
40474062
// Else handle template non-type parameters
40484063

@@ -4051,8 +4066,10 @@ class cppfront
40514066

40524067
if (is_template_parameter) {
40534068
emit( type_id );
4054-
printer.print_cpp2(" ", type_id.position());
4069+
if (emit_identifier) {
4070+
printer.print_cpp2(" ", type_id.position());
40554071
printer.print_cpp2( identifier, identifier_pos );
4072+
}
40564073
return;
40574074
}
40584075

@@ -4263,7 +4280,8 @@ class cppfront
42634280
auto emit(
42644281
parameter_declaration_list_node const& n,
42654282
bool is_returns = false,
4266-
bool is_template_parameter = false
4283+
bool is_template_parameter = false,
4284+
bool emit_identifier = true
42674285
)
42684286
-> void
42694287
{
@@ -4291,7 +4309,7 @@ class cppfront
42914309
}
42924310
prev_pos = x->position();
42934311
assert(x);
4294-
emit(*x, is_returns, is_template_parameter);
4312+
emit(*x, is_returns, is_template_parameter, emit_identifier);
42954313
if (!x->declaration->has_name("this")) {
42964314
first = false;
42974315
}

0 commit comments

Comments
 (0)