Skip to content

Commit 8b27914

Browse files
committed
fix(to_cpp1): emit template template parameters
1 parent c5feb42 commit 8b27914

5 files changed

+71
-14
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
@@ -3282,6 +3282,8 @@ struct declaration_node
32823282

32833283
auto is_function_expression () const -> bool
32843284
{ return is_function() && !identifier; }
3285+
auto is_template() const -> bool
3286+
{ return template_parameters != nullptr; }
32853287

32863288
auto is_polymorphic() const // has base types or virtual functions
32873289
-> bool

source/to_cpp1.h

Lines changed: 30 additions & 14 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()) {
@@ -3428,12 +3428,12 @@ class cppfront
34283428
last_was_prefixed = true;
34293429
}
34303430

3431-
// Handle the other Cpp2 postfix operators that stay postfix in Cpp1
3431+
// Handle the other Cpp2 postfix operators that stay postfix in Cpp1
34323432
// (currently '...' for expansion, not when used as a range operator)
34333433
else if (
34343434
is_postfix_operator(i->op->type())
34353435
&& !i->last_expr // not being used as a range operator
3436-
)
3436+
)
34373437
{
34383438
flush_args();
34393439
suffix.emplace_back( i->op->to_string(), i->op->position());
@@ -3474,7 +3474,7 @@ class cppfront
34743474
}
34753475

34763476
auto print = print_to_string(
3477-
*i->id_expr,
3477+
*i->id_expr,
34783478
false, // not a local name
34793479
i->op->type() == lexeme::Dot || i->op->type() == lexeme::DotDot // member access
34803480
);
@@ -4362,7 +4362,8 @@ class cppfront
43624362
parameter_declaration_node const& n,
43634363
bool is_returns = false,
43644364
bool is_template_parameter = false,
4365-
bool is_statement = false
4365+
bool is_statement = false,
4366+
bool emit_identifier = true
43664367
)
43674368
-> void
43684369
{ STACKINSTR
@@ -4406,8 +4407,8 @@ class cppfront
44064407
{
44074408
assert(n.declaration);
44084409
auto is_param_to_namespace_scope_type =
4409-
n.declaration->parent_is_type()
4410-
&& n.declaration->parent_declaration->parent_is_namespace()
4410+
n.declaration->parent_is_type()
4411+
&& n.declaration->parent_declaration->parent_is_namespace()
44114412
;
44124413

44134414
auto emit_in_phase_0 =
@@ -4524,7 +4525,7 @@ class cppfront
45244525
printer.print_cpp2( unnamed_type_param_name(n.ordinal, n.declaration->identifier->get_token()),
45254526
identifier_pos );
45264527
}
4527-
else {
4528+
else if (emit_identifier) {
45284529
printer.print_cpp2( identifier, identifier_pos );
45294530
}
45304531
};
@@ -4537,6 +4538,20 @@ class cppfront
45374538
return;
45384539
}
45394540

4541+
//-----------------------------------------------------------------------
4542+
// Handle template parameters
4543+
4544+
if (n.declaration->is_template()) {
4545+
printer.print_cpp2("template ", identifier_pos);
4546+
emit(*n.declaration->template_parameters, is_returns, true, false, false);
4547+
printer.print_cpp2(" class", identifier_pos);
4548+
if (emit_identifier) {
4549+
printer.print_cpp2(" ", identifier_pos);
4550+
printer.print_cpp2( identifier, identifier_pos );
4551+
}
4552+
return;
4553+
}
4554+
45404555
//-----------------------------------------------------------------------
45414556
// Else handle template non-type parameters
45424557

@@ -4760,7 +4775,8 @@ class cppfront
47604775
parameter_declaration_list_node const& n,
47614776
bool is_returns = false,
47624777
bool is_template_parameter = false,
4763-
bool generating_postfix_inc_dec = false
4778+
bool generating_postfix_inc_dec = false,
4779+
bool emit_identifier = true
47644780
)
47654781
-> void
47664782
{ STACKINSTR
@@ -4789,7 +4805,7 @@ class cppfront
47894805
}
47904806
prev_pos = x->position();
47914807
assert(x);
4792-
emit(*x, is_returns, is_template_parameter);
4808+
emit(*x, is_returns, is_template_parameter, false, emit_identifier);
47934809
if (!x->declaration->has_name("this")) {
47944810
first = false;
47954811
}
@@ -5033,7 +5049,7 @@ class cppfront
50335049
|| n.is_swap()
50345050
|| n.is_destructor()
50355051
|| (
5036-
n.my_decl
5052+
n.my_decl
50375053
&& generating_move_from == n.my_decl
50385054
)
50395055
)
@@ -5047,7 +5063,7 @@ class cppfront
50475063
if (
50485064
n.is_assignment()
50495065
|| (
5050-
n.my_decl
5066+
n.my_decl
50515067
&& generating_assignment_from == n.my_decl
50525068
)
50535069
)
@@ -6935,8 +6951,8 @@ class cppfront
69356951
return;
69366952
}
69376953
}
6938-
printer.preempt_position_push(n.position());
6939-
emit( *type, {}, print_to_string(*n.identifier) );
6954+
printer.preempt_position_push(n.position());
6955+
emit( *type, {}, print_to_string(*n.identifier) );
69406956
printer.preempt_position_pop();
69416957

69426958
if (

0 commit comments

Comments
 (0)