Skip to content

Commit

Permalink
Get rid of token pointer arithmetic
Browse files Browse the repository at this point in the history
Tokens aren't always contiguous anyway, because of generated/merged tokens
  • Loading branch information
hsutter authored and bluetarpmedia committed Feb 7, 2024
1 parent 7b4928f commit 1a24ab1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
49 changes: 49 additions & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ struct primary_expression_node
auto is_id_expression() const
-> bool;

auto is_unqualified_id() const
-> bool;

auto is_expression_list() const
-> bool;

Expand Down Expand Up @@ -246,6 +249,9 @@ struct prefix_expression_node
auto is_id_expression() const
-> bool;

auto is_unqualified_id() const
-> bool;

auto is_expression_list() const
-> bool;

Expand Down Expand Up @@ -340,6 +346,12 @@ struct binary_expression_node
return terms.empty() && expr->is_id_expression();
}

auto is_unqualified_id() const
-> bool
{
return terms.empty() && expr->is_unqualified_id();
}

auto is_expression_list() const
-> bool
{
Expand Down Expand Up @@ -533,6 +545,12 @@ struct expression_node
return expr->is_id_expression();
}

auto is_unqualified_id() const
-> bool
{
return expr->is_unqualified_id();
}

auto is_expression_list() const
-> bool
{
Expand Down Expand Up @@ -858,6 +876,12 @@ struct postfix_expression_node
return ops.empty() && expr->is_id_expression();
}

auto is_unqualified_id() const
-> bool
{
return ops.empty() && expr->is_unqualified_id();
}

auto is_expression_list() const
-> bool
{
Expand Down Expand Up @@ -935,6 +959,12 @@ auto prefix_expression_node::is_id_expression() const
return ops.empty() && expr->is_id_expression();
}

auto prefix_expression_node::is_unqualified_id() const
-> bool
{
return ops.empty() && expr->is_unqualified_id();
}

auto prefix_expression_node::is_expression_list() const
-> bool
{
Expand Down Expand Up @@ -1409,6 +1439,12 @@ struct is_as_expression_node
return ops.empty() && expr->is_id_expression();
}

auto is_unqualified_id() const
-> bool
{
return ops.empty() && expr->is_unqualified_id();
}

auto is_expression_list() const
-> bool
{
Expand Down Expand Up @@ -1608,6 +1644,19 @@ postfix_expression_node::~postfix_expression_node()
}


auto primary_expression_node::is_unqualified_id() const
-> bool
{
if (is_identifier()) {
return true;
}
if (is_id_expression()) {
return std::get<id_expression>(expr)->is_unqualified();
}
return false;
}


auto primary_expression_node::is_fold_expression() const
-> bool
{
Expand Down
11 changes: 9 additions & 2 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,7 @@ class cppfront
std::vector<iter_info> iteration_statements;

std::vector<bool> in_non_rvalue_context = { false };
std::vector<bool> in_single_unqualified_id_return = { false };
std::vector<bool> need_expression_list_parens = { true };
auto push_need_expression_list_parens( bool b ) -> void { need_expression_list_parens.push_back(b); }
auto pop_need_expression_list_parens() -> void { assert(std::ssize(need_expression_list_parens) > 1);
Expand Down Expand Up @@ -1795,8 +1796,8 @@ class cppfront

if (
add_move
&& *(n.identifier - 1) == "return"
&& *(n.identifier + 1) == ";"
&& in_single_unqualified_id_return.size() > 0
&& in_single_unqualified_id_return.back()
&& (
!decl
|| decl->initializer
Expand Down Expand Up @@ -2388,6 +2389,12 @@ class cppfront
assert(*n.identifier == "return");
printer.print_cpp2("return ", n.position());

in_single_unqualified_id_return.push_back(
n.expression
&& n.expression->is_unqualified_id()
);
auto guard = finally([&]{ in_single_unqualified_id_return.pop_back(); });

// Return with expression == single anonymous return type
//
if (n.expression)
Expand Down

0 comments on commit 1a24ab1

Please sign in to comment.