Skip to content

Commit

Permalink
fix(cpp1): emit template template parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed Sep 22, 2023
1 parent efd185f commit 279ca08
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 7 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> = (); }
4 changes: 4 additions & 0 deletions regression-tests/test-results/clang-18/clang-version.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 3723ede3cf5324827f8fbbe7f484c2ee4d7a7204)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /home/johel/root/clang-main/bin
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sizeof(x) is 25
(not a name)
xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#define CPP2_USE_MODULES Yes

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


#include "cpp2util.h"

template<template <typename , auto> class T> class t;

template<typename T, auto V> class u;


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

template<template <typename , auto> class T> class t {};

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

auto main() -> int;


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


#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)

35 changes: 28 additions & 7 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3842,7 +3842,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
{
Expand Down Expand Up @@ -3925,15 +3926,32 @@ class cppfront
// Handle type parameters

if (n.declaration->is_type()) {
assert (n.declaration->identifier);
printer.print_cpp2("typename ", n.declaration->identifier->position());
if (n.declaration->is_variadic) {
printer.print_cpp2(
"...",
n.declaration->identifier->position()
);
}
if (emit_identifier) {
emit(*n.declaration->identifier);
}
return;
}

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

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

Expand All @@ -3945,9 +3963,11 @@ class cppfront

if (is_template_parameter) {
emit( type_id );
printer.print_cpp2(" ", type_id.position());
assert (n.declaration->identifier);
emit(*n.declaration->identifier);
if (emit_identifier) {
printer.print_cpp2(" ", type_id.position());
assert (n.declaration->identifier);
emit(*n.declaration->identifier);
}
return;
}

Expand Down Expand Up @@ -4149,7 +4169,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
{
Expand Down Expand Up @@ -4177,7 +4198,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
2 changes: 2 additions & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -2740,6 +2740,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

0 comments on commit 279ca08

Please sign in to comment.