Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(to_cpp1): emit template template parameters #603

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3347,6 +3347,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
34 changes: 24 additions & 10 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 @@ -3458,12 +3458,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 @@ -3504,7 +3504,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 @@ -4453,8 +4453,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 @@ -4584,6 +4584,20 @@ class cppfront
return;
}

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

if (n.declaration->is_template()) {
assert( n.declaration->is_template() );
printer.print_cpp2("template ", identifier_pos);
emit(*n.declaration->template_parameters, is_returns, true);
printer.print_cpp2(" class ", identifier_pos);

emit_template_name();
emit_initializer();
return;
}

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

Expand Down Expand Up @@ -5091,7 +5105,7 @@ class cppfront
|| n.is_swap()
|| n.is_destructor()
|| (
n.my_decl
n.my_decl
&& generating_move_from == n.my_decl
)
)
Expand All @@ -5105,7 +5119,7 @@ class cppfront
if (
n.is_assignment()
|| (
n.my_decl
n.my_decl
&& generating_assignment_from == n.my_decl
)
)
Expand Down Expand Up @@ -6995,8 +7009,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
Loading