Skip to content

Commit 5805c67

Browse files
committed
fix(to_cpp1): emit template template parameters
1 parent ccf7011 commit 5805c67

5 files changed

+65
-10
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> = (); }
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_1, auto UnnamedTypeParam2_2> 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_1, auto UnnamedTypeParam2_2> 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
@@ -3347,6 +3347,8 @@ struct declaration_node
33473347

33483348
auto is_function_expression () const -> bool
33493349
{ return is_function() && !identifier; }
3350+
auto is_template() const -> bool
3351+
{ return template_parameters != nullptr; }
33503352

33513353
auto is_polymorphic() const // has base types or virtual functions
33523354
-> bool

source/to_cpp1.h

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ class cppfront
12551255

12561256
// Now we'll open the Cpp1 file
12571257
auto cpp1_filename = sourcefile.substr(0, std::ssize(sourcefile) - 1);
1258-
1258+
12591259
// Use explicit filename override if present,
12601260
// otherwise strip leading path
12611261
if (!flag_cpp1_filename.empty()) {
@@ -3458,12 +3458,12 @@ class cppfront
34583458
last_was_prefixed = true;
34593459
}
34603460

3461-
// Handle the other Cpp2 postfix operators that stay postfix in Cpp1
3461+
// Handle the other Cpp2 postfix operators that stay postfix in Cpp1
34623462
// (currently '...' for expansion, not when used as a range operator)
34633463
else if (
34643464
is_postfix_operator(i->op->type())
34653465
&& !i->last_expr // not being used as a range operator
3466-
)
3466+
)
34673467
{
34683468
flush_args();
34693469
suffix.emplace_back( i->op->to_string(), i->op->position());
@@ -3504,7 +3504,7 @@ class cppfront
35043504
}
35053505

35063506
auto print = print_to_string(
3507-
*i->id_expr,
3507+
*i->id_expr,
35083508
false, // not a local name
35093509
i->op->type() == lexeme::Dot || i->op->type() == lexeme::DotDot // member access
35103510
);
@@ -4453,8 +4453,8 @@ class cppfront
44534453
{
44544454
assert(n.declaration);
44554455
auto is_param_to_namespace_scope_type =
4456-
n.declaration->parent_is_type()
4457-
&& n.declaration->parent_declaration->parent_is_namespace()
4456+
n.declaration->parent_is_type()
4457+
&& n.declaration->parent_declaration->parent_is_namespace()
44584458
;
44594459

44604460
auto emit_in_phase_0 =
@@ -4584,6 +4584,20 @@ class cppfront
45844584
return;
45854585
}
45864586

4587+
//-----------------------------------------------------------------------
4588+
// Handle template parameters
4589+
4590+
if (n.declaration->is_template()) {
4591+
assert( n.declaration->is_template() );
4592+
printer.print_cpp2("template ", identifier_pos);
4593+
emit(*n.declaration->template_parameters, is_returns, true);
4594+
printer.print_cpp2(" class ", identifier_pos);
4595+
4596+
emit_template_name();
4597+
emit_initializer();
4598+
return;
4599+
}
4600+
45874601
//-----------------------------------------------------------------------
45884602
// Else handle template non-type parameters
45894603

@@ -5091,7 +5105,7 @@ class cppfront
50915105
|| n.is_swap()
50925106
|| n.is_destructor()
50935107
|| (
5094-
n.my_decl
5108+
n.my_decl
50955109
&& generating_move_from == n.my_decl
50965110
)
50975111
)
@@ -5105,7 +5119,7 @@ class cppfront
51055119
if (
51065120
n.is_assignment()
51075121
|| (
5108-
n.my_decl
5122+
n.my_decl
51095123
&& generating_assignment_from == n.my_decl
51105124
)
51115125
)
@@ -6995,8 +7009,8 @@ class cppfront
69957009
return;
69967010
}
69977011
}
6998-
printer.preempt_position_push(n.position());
6999-
emit( *type, {}, print_to_string(*n.identifier) );
7012+
printer.preempt_position_push(n.position());
7013+
emit( *type, {}, print_to_string(*n.identifier) );
70007014
printer.preempt_position_pop();
70017015

70027016
if (

0 commit comments

Comments
 (0)