Skip to content

fix(parse): make template-argument-list optional #473

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

Merged
merged 3 commits into from
Jun 17, 2023
Merged
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,2 @@
plus: const std::plus<> = ();
main: () -> int = std::plus<>()(0, 0);
5 changes: 5 additions & 0 deletions regression-tests/test-results/gcc-13/gcc-version.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
c++ (GCC) 13.1.1 20230429
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#define CPP2_USE_MODULES Yes

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


#include "cpp2util.h"



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

#line 1 "pure2-bugfix-for-optional-template-argument-list.cpp2"
extern std::plus<> const plus;
[[nodiscard]] auto main() -> int;


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

#line 1 "pure2-bugfix-for-optional-template-argument-list.cpp2"
std::plus<> const plus {};
[[nodiscard]] auto main() -> int { return std::plus<>()(0, 0); }

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

2 changes: 1 addition & 1 deletion source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ class cppfront
assert(n.identifier);
emit(*n.identifier, is_qualified); // inform the identifier if we know this is qualified

if (!n.template_args.empty()) {
if (n.open_angle != source_position{}) {
printer.print_cpp2("<", n.open_angle);
auto first = true;
for (auto& a : n.template_args) {
Expand Down
29 changes: 12 additions & 17 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ struct unqualified_id_node
auto get_token() const
-> token const*
{
if (template_args.empty()) {
if (open_angle == source_position{}) {
assert (identifier);
return identifier;
}
Expand All @@ -718,12 +718,13 @@ struct unqualified_id_node
assert (identifier);
v.start(*identifier, depth+1);

if (!template_args.empty()) {
if (open_angle != source_position{}) {
// Inform the visitor that this is a template args list
v.start(template_args_tag{}, depth);
assert(open_angle != source_position{});
assert(close_angle != source_position{});
assert(template_args.front().comma == source_position{});
assert(template_args.empty()
|| template_args.front().comma == source_position{});
for (auto& a : template_args) {
try_visit<expression>(a.arg, v, depth+1);
try_visit<type_id >(a.arg, v, depth+1);
Expand Down Expand Up @@ -925,7 +926,7 @@ auto unqualified_id_node::to_string() const
{
assert(identifier);
auto ret = identifier->to_string(true);
if (!template_args.empty()) {
if (open_angle != source_position{}) {
auto separator = std::string{"<"};
for (auto& t : template_args) {
ret += separator;
Expand Down Expand Up @@ -4454,18 +4455,9 @@ class parser
// Remember current position, in case this < is isn't a template argument list
auto start_pos = pos;

// And since we'll do this in two places, factor it into a local function
auto back_out_template_arg_list = [&]{
// Aha, this wasn't a template argument list after all,
// so back out just that part and return the identifier
n->open_angle = source_position{};
n->template_args.clear();
pos = start_pos;
};

n->open_angle = curr().position();
next();

auto term = unqualified_id_node::term{};

do {
Expand All @@ -4477,8 +4469,7 @@ class parser
term.arg = std::move(i);
}
else {
back_out_template_arg_list();
return n;
break;
}
n->template_args.push_back( std::move(term) );
}
Expand All @@ -4492,7 +4483,11 @@ class parser
// next term.comma = curr().position();

if (curr().type() != lexeme::Greater) {
back_out_template_arg_list();
// Aha, this wasn't a template argument list after all,
// so back out just that part and return the identifier
n->open_angle = source_position{};
n->template_args.clear();
pos = start_pos;
return n;
}
n->close_angle = curr().position();
Expand Down