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 Dec 11, 2023
1 parent 05ce45a commit 03b6778
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 5 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, auto> 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, auto> 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 @@ -2841,6 +2841,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
28 changes: 23 additions & 5 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -4122,7 +4122,8 @@ class cppfront
auto emit(
parameter_declaration_node const& n,
bool is_returns = false,
bool is_template_parameter = false
bool is_template_parameter = false,
bool emit_identifier = true
)
-> void
{ STACKINSTR
Expand Down Expand Up @@ -4227,13 +4228,27 @@ class cppfront
if (identifier == "_") {
printer.print_cpp2( "UnnamedTypeParam" + std::to_string(n.ordinal), identifier_pos );
}
else {
else if (emit_identifier) {
printer.print_cpp2( identifier, identifier_pos );
}

return;
}

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

if (n.declaration->is_template()) {
printer.print_cpp2("template ", identifier_pos);
emit(*n.declaration->template_parameters, is_returns, true, 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 All @@ -4242,8 +4257,10 @@ class cppfront

if (is_template_parameter) {
emit( type_id );
printer.print_cpp2(" ", type_id.position());
if (emit_identifier) {
printer.print_cpp2(" ", type_id.position());
printer.print_cpp2( identifier, identifier_pos );
}
return;
}

Expand Down Expand Up @@ -4455,7 +4472,8 @@ class cppfront
auto emit(
parameter_declaration_list_node const& n,
bool is_returns = false,
bool is_template_parameter = false
bool is_template_parameter = false,
bool emit_identifier = true
)
-> void
{ STACKINSTR
Expand Down Expand Up @@ -4483,7 +4501,7 @@ class cppfront
}
prev_pos = x->position();
assert(x);
emit(*x, is_returns, is_template_parameter);
emit(*x, is_returns, is_template_parameter, emit_identifier);
if (!x->declaration->has_name("this")) {
first = false;
}
Expand Down

0 comments on commit 03b6778

Please sign in to comment.