Skip to content

Commit

Permalink
fix(to_cpp1): emit template template parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed Oct 1, 2024
1 parent c5feb42 commit 8b27914
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
t: @struct <T: <_, _: _>> type = { }

u: @struct <T, V: _> type = { }

main: () = { _ = :t<u> = (); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

#define CPP2_IMPORT_STD Yes

//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"

#line 1 "pure2-bugfix-for-template-template-parameter.cpp2"
template<template <typename UnnamedTypeParam1_1, auto UnnamedTypeParam2_2> class T> class t;
#line 2 "pure2-bugfix-for-template-template-parameter.cpp2"

template<typename T, auto V> class u;


//=== Cpp2 type definitions and function declarations ===========================

#line 1 "pure2-bugfix-for-template-template-parameter.cpp2"
template<template <typename UnnamedTypeParam1_1, auto UnnamedTypeParam2_2> class T> class t {};
#line 2 "pure2-bugfix-for-template-template-parameter.cpp2"

template<typename T, auto V> class u {};

auto main() -> int;

//=== Cpp2 function definitions =================================================

#line 1 "pure2-bugfix-for-template-template-parameter.cpp2"

#line 5 "pure2-bugfix-for-template-template-parameter.cpp2"
auto main() -> int{static_cast<void>(t<u>{}); }

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-bugfix-for-template-template-parameter.cpp2... ok (all Cpp2, passes safety checks)

2 changes: 2 additions & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -3282,6 +3282,8 @@ struct declaration_node

auto is_function_expression () const -> bool
{ return is_function() && !identifier; }
auto is_template() const -> bool
{ return template_parameters != nullptr; }

auto is_polymorphic() const // has base types or virtual functions
-> bool
Expand Down
44 changes: 30 additions & 14 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ class cppfront

// Now we'll open the Cpp1 file
auto cpp1_filename = sourcefile.substr(0, std::ssize(sourcefile) - 1);

// Use explicit filename override if present,
// otherwise strip leading path
if (!flag_cpp1_filename.empty()) {
Expand Down Expand Up @@ -3428,12 +3428,12 @@ class cppfront
last_was_prefixed = true;
}

// Handle the other Cpp2 postfix operators that stay postfix in Cpp1
// Handle the other Cpp2 postfix operators that stay postfix in Cpp1
// (currently '...' for expansion, not when used as a range operator)
else if (
is_postfix_operator(i->op->type())
&& !i->last_expr // not being used as a range operator
)
)
{
flush_args();
suffix.emplace_back( i->op->to_string(), i->op->position());
Expand Down Expand Up @@ -3474,7 +3474,7 @@ class cppfront
}

auto print = print_to_string(
*i->id_expr,
*i->id_expr,
false, // not a local name
i->op->type() == lexeme::Dot || i->op->type() == lexeme::DotDot // member access
);
Expand Down Expand Up @@ -4362,7 +4362,8 @@ class cppfront
parameter_declaration_node const& n,
bool is_returns = false,
bool is_template_parameter = false,
bool is_statement = false
bool is_statement = false,
bool emit_identifier = true
)
-> void
{ STACKINSTR
Expand Down Expand Up @@ -4406,8 +4407,8 @@ class cppfront
{
assert(n.declaration);
auto is_param_to_namespace_scope_type =
n.declaration->parent_is_type()
&& n.declaration->parent_declaration->parent_is_namespace()
n.declaration->parent_is_type()
&& n.declaration->parent_declaration->parent_is_namespace()
;

auto emit_in_phase_0 =
Expand Down Expand Up @@ -4524,7 +4525,7 @@ class cppfront
printer.print_cpp2( unnamed_type_param_name(n.ordinal, n.declaration->identifier->get_token()),
identifier_pos );
}
else {
else if (emit_identifier) {
printer.print_cpp2( identifier, identifier_pos );
}
};
Expand All @@ -4537,6 +4538,20 @@ class cppfront
return;
}

//-----------------------------------------------------------------------
// Handle template parameters

if (n.declaration->is_template()) {
printer.print_cpp2("template ", identifier_pos);
emit(*n.declaration->template_parameters, is_returns, true, false, false);
printer.print_cpp2(" class", identifier_pos);
if (emit_identifier) {
printer.print_cpp2(" ", identifier_pos);
printer.print_cpp2( identifier, identifier_pos );
}
return;
}

//-----------------------------------------------------------------------
// Else handle template non-type parameters

Expand Down Expand Up @@ -4760,7 +4775,8 @@ class cppfront
parameter_declaration_list_node const& n,
bool is_returns = false,
bool is_template_parameter = false,
bool generating_postfix_inc_dec = false
bool generating_postfix_inc_dec = false,
bool emit_identifier = true
)
-> void
{ STACKINSTR
Expand Down Expand Up @@ -4789,7 +4805,7 @@ class cppfront
}
prev_pos = x->position();
assert(x);
emit(*x, is_returns, is_template_parameter);
emit(*x, is_returns, is_template_parameter, false, emit_identifier);
if (!x->declaration->has_name("this")) {
first = false;
}
Expand Down Expand Up @@ -5033,7 +5049,7 @@ class cppfront
|| n.is_swap()
|| n.is_destructor()
|| (
n.my_decl
n.my_decl
&& generating_move_from == n.my_decl
)
)
Expand All @@ -5047,7 +5063,7 @@ class cppfront
if (
n.is_assignment()
|| (
n.my_decl
n.my_decl
&& generating_assignment_from == n.my_decl
)
)
Expand Down Expand Up @@ -6935,8 +6951,8 @@ class cppfront
return;
}
}
printer.preempt_position_push(n.position());
emit( *type, {}, print_to_string(*n.identifier) );
printer.preempt_position_push(n.position());
emit( *type, {}, print_to_string(*n.identifier) );
printer.preempt_position_pop();

if (
Expand Down

0 comments on commit 8b27914

Please sign in to comment.