Skip to content

Commit

Permalink
fix(parse): recognize const and pointer template arguments (#521)
Browse files Browse the repository at this point in the history
* fix(parse): recognize const and pointer template arguments

* Minor: Massage the new code's format and comment

Signed-off-by: Herb Sutter <[email protected]>

* Update parse.h

Signed-off-by: Herb Sutter <[email protected]>

---------

Signed-off-by: Herb Sutter <[email protected]>
Co-authored-by: Herb Sutter <[email protected]>
  • Loading branch information
JohelEGP and hsutter authored Aug 11, 2023
1 parent 9ec94ca commit 706ebc8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
3 changes: 3 additions & 0 deletions regression-tests/pure2-bugfix-for-template-argument.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
main: () = //
std::is_void_v<* i32> //
&& std::is_void_v<const i32>;
Empty file.
Empty file.
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-template-argument.cpp2"
auto main() -> int;


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

#line 1 "pure2-bugfix-for-template-argument.cpp2"
auto main() -> int { //
std::is_void_v<cpp2::i32*> //
&& std::is_void_v<cpp2::i32 const>; }

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

21 changes: 19 additions & 2 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -4822,6 +4822,7 @@ class parser
//G
//G template-argument:
//G # note: < > << >> are not allowed in expressions until new ( is opened
//G 'const' type-id
//G expression
//G type-id
//G
Expand Down Expand Up @@ -4855,16 +4856,32 @@ class parser
auto term = unqualified_id_node::term{};

do {
// disallow unparenthesized relational comparisons in template args
if (auto e = expression(false)) {
// If it doesn't start with * or const (which can only be a type id),
// try parsing it as an expression
if (auto e = [&]{
if (
curr().type() == lexeme::Multiply // '*'
|| curr() == "const" // 'const'
)
{
return decltype(expression()){};
}
return expression(false); // false == disallow unparenthesized relational comparisons in template args
}()
)
{
term.arg = std::move(e);
}

// Else try parsing it as a type id
else if (auto i = type_id()) {
term.arg = std::move(i);
}

else {
break;
}

n->template_args.push_back( std::move(term) );
}
// Use the lambda trick to jam in a "next" clause
Expand Down

0 comments on commit 706ebc8

Please sign in to comment.