Skip to content

Commit

Permalink
fix(cpp1): emit template for dependent _template-id_
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed Jul 4, 2023
1 parent 4569d75 commit 0c30349
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions regression-tests/pure2-optional-typename.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ f: <T, V: T::value_type> (x: T::value_type) -> T::value_type = {
_ = :identity<T>::value_type = (); // First identifier.
_ = :std::optional<T>::value_type = (); // Non-first identifier.

// Emitted `template`.
ptr: type == * T; // Needed, pending #502.
type: type == std::pointer_traits<ptr>::rebind<ptr>; // Type-only context.
_ = :std::pointer_traits<ptr>::rebind<ptr> = (); // Non type-only context.

// Aliases.
w: type == T;
_ = :w::value_type = x;
Expand Down
7 changes: 6 additions & 1 deletion regression-tests/test-results/pure2-optional-typename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ template<typename T> using identity = T;
template<typename T, typename T::value_type V> [[nodiscard]] auto f(cpp2::in<typename T::value_type> x) -> typename T::value_type;


#line 31 "pure2-optional-typename.cpp2"
#line 36 "pure2-optional-typename.cpp2"
auto main() -> int;


Expand All @@ -34,6 +34,11 @@ template<typename T, typename T::value_type V> [[nodiscard]] auto f(cpp2::in<typ
(void) typename identity<T>::value_type{};// First identifier.
(void) typename std::optional<T>::value_type{};// Non-first identifier.

// Emitted `template`.
using ptr = T*; // Needed, pending #502.
using type = typename std::pointer_traits<ptr>::template rebind<ptr>;// Type-only context.
(void) typename std::pointer_traits<ptr>::template rebind<ptr>{};// Non type-only context.

// Aliases.
using w = T;
(void) typename w::value_type{x};
Expand Down
17 changes: 13 additions & 4 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,8 @@ class cppfront
unqualified_id_node const& n,
bool in_synthesized_multi_return = false,
bool is_local_name = true,
bool is_qualified = false
bool is_qualified = false,
bool can_emit_template_keyword = false
)
-> void
{
Expand Down Expand Up @@ -1663,6 +1664,10 @@ class cppfront
printer.print_cpp2("CPP2_FORWARD(", {n.position().lineno, n.position().colno - 8});
}

if (can_emit_template_keyword && n.open_angle != source_position{}) {
printer.print_cpp2("template ", n.position());
}

assert(n.identifier);
emit(*n.identifier, is_qualified); // inform the identifier if we know this is qualified

Expand Down Expand Up @@ -1755,7 +1760,7 @@ class cppfront
auto guard = finally([&]{ looking_up.pop_back(); });

// If any parent declaration
return std::any_of(current_declarations.begin() + 1, current_declarations.end(), [&](declaration_node const* decl) {
return std::any_of(current_declarations.rbegin(), current_declarations.rend() - 1, [&](declaration_node const* decl) {
// that can have aliases
if ((decl->is_function()
|| decl->is_type()
Expand All @@ -1765,7 +1770,7 @@ class cppfront
{
auto& stmts = decl->initializer->get_if<compound_statement_node>()->statements;
// among its statements
return std::any_of(stmts.begin(), stmts.end(), [&](decltype(stmts.front())& stmt) {
return std::any_of(stmts.rbegin(), stmts.rend(), [&](decltype(stmts.front())& stmt) {
if (auto decl = stmt->get_if<declaration_node>();
decl
&& decl->is_alias()) {
Expand Down Expand Up @@ -1886,12 +1891,16 @@ class cppfront
printer.print_cpp2("typename ", n.position());
}

auto can_emit_template_keyword = false;
for (auto const& id : n.ids)
{
if (id.scope_op) {
emit(*id.scope_op);
}
emit(*id.id, false, true, true); // inform the unqualified-id that it's qualified
emit(*id.id, false, true, true, can_emit_template_keyword); // inform the unqualified-id that it's qualified
if (!can_emit_template_keyword && this->is_dependent(*id.id)) {
can_emit_template_keyword = true;
}
}

printer.emit_to_string();
Expand Down

0 comments on commit 0c30349

Please sign in to comment.