Skip to content

fix(cpp1): recognize concept definitions #561

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 2 commits into from
Aug 11, 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
5 changes: 5 additions & 0 deletions regression-tests/pure2-concept-definition.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
arithmetic: <T> concept = std::integral<T> || std::floating_point<T>;
main: () = {
[[assert Testing: arithmetic<i32>]]
[[assert Testing: arithmetic<float>]]
}
26 changes: 26 additions & 0 deletions regression-tests/test-results/pure2-concept-definition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

#define CPP2_USE_MODULES Yes

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


#include "cpp2util.h"



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

#line 1 "pure2-concept-definition.cpp2"
template<typename T> concept arithmetic = std::integral<T> || std::floating_point<T>;
auto main() -> int;


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


#line 2 "pure2-concept-definition.cpp2"
auto main() -> int {
cpp2::Testing.expects(arithmetic<cpp2::i32>, "");
cpp2::Testing.expects(arithmetic<float>, "");
}

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

45 changes: 32 additions & 13 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class positional_printer
std::vector<comment> const* pcomments = {}; // Cpp2 comments data
source const* psource = {};
parser const* pparser = {};

source_position curr_pos = {}; // current (line,col) in output
lineno_t generated_pos_line = {}; // current line in generated output
int last_line_indentation = {};
Expand Down Expand Up @@ -1226,7 +1226,7 @@ class cppfront

//---------------------------------------------------------------------
// Do lowered file prolog
//
//
// Only emit extra lines if we actually have Cpp2, because
// we want pure-Cpp1 files to pass through with zero changes
if (source.has_cpp2())
Expand Down Expand Up @@ -1284,7 +1284,7 @@ class cppfront
}
}


//---------------------------------------------------------------------
// Do phase1_type_defs_func_decls
//
Expand Down Expand Up @@ -1641,7 +1641,7 @@ class cppfront
{
add_move = false;
}

if (
emitting_move_that_function
&& *n.identifier == "that"
Expand Down Expand Up @@ -2253,7 +2253,7 @@ class cppfront
return;
} else if (
is_literal(tok->type()) || n.expression->expr->is_result_a_temporary_variable()
)
)
{
errors.emplace_back(
n.position(),
Expand Down Expand Up @@ -2545,7 +2545,7 @@ class cppfront
)
-> bool
{
if (!fun_node) {
if (!fun_node) {
return false;
}
if (addr_cnt > deref_cnt) {
Expand All @@ -2572,11 +2572,11 @@ class cppfront
)
-> bool
{
if (!type_id_node) {
if (!type_id_node) {
return false;
}
if (addr_cnt > deref_cnt) {
return true;
return true;
}

if ( type_id_node->dereference_of ) {
Expand Down Expand Up @@ -2753,7 +2753,7 @@ class cppfront
{
auto& unqual = std::get<id_expression_node::unqualified>(id->id);
assert(unqual);
// TODO: Generalize this:
// TODO: Generalize this:
// - we don't recognize pointer types from Cpp1
// - we don't deduce pointer types from parameter_declaration_list_node
if ( is_pointer_declaration(unqual->identifier) ) {
Expand Down Expand Up @@ -4817,7 +4817,7 @@ class cppfront
}

// If this is a generated declaration (negative source line number),
// add a line break before
// add a line break before
if (
printer.get_phase() == printer.phase2_func_defs
&& n.position().lineno < 1
Expand Down Expand Up @@ -5076,6 +5076,10 @@ class cppfront
&& printer.get_phase() == printer.phase2_func_defs
)
)
&& (
!n.is_concept()
|| printer.get_phase() == printer.phase1_type_defs_func_decls
)
)
{
printer.print_cpp2("template", n.position());
Expand Down Expand Up @@ -5453,7 +5457,7 @@ class cppfront
// A2) This is '(out this, move that)'
// and no '(inout this, move that)' was written by the user
// (*) and no '(inout this, that)' was written by the user (*)
//
//
// (*) This third test is to tie-break M2 and A2 in favor of M2. Both M2 and A2
// can generate a missing '(inout this, move that)', and if we have both
// options then we should prefer to use M2 (generate move assignment from
Expand Down Expand Up @@ -5746,10 +5750,18 @@ class cppfront
)
{
auto& type = std::get<declaration_node::an_object>(n.type);
if (
printer.get_phase() == printer.phase2_func_defs
&& type->is_concept()
)
{
return;
}

if (
printer.get_phase() != printer.phase2_func_defs
&& n.parent_is_namespace()
&& !type->is_concept()
)
{
printer.print_cpp2( "extern ", n.position() );
Expand Down Expand Up @@ -5813,6 +5825,7 @@ class cppfront
if (
n.parent_is_namespace()
&& printer.get_phase() != printer.phase2_func_defs
&& !type->is_concept()
)
{
printer.print_cpp2( ";", n.position());
Expand All @@ -5824,14 +5837,20 @@ class cppfront
{
in_non_rvalue_context.push_back(true);
printer.add_pad_in_this_line(-100);
printer.print_cpp2( " {", n.position() );
if (type->is_concept()) {
printer.print_cpp2( " = ", n.position() );
} else {
printer.print_cpp2( " {", n.position() );
}

push_need_expression_list_parens(false);
assert( n.initializer );
emit( *n.initializer, false );
pop_need_expression_list_parens();

printer.print_cpp2( "}", n.position() );
if (!type->is_concept()) {
printer.print_cpp2( "}", n.position() );
}
in_non_rvalue_context.pop_back();
}

Expand Down
47 changes: 28 additions & 19 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ struct postfix_expression_node
if (ops.empty()) {
return false;
} else {
return (ops.front().op->type() == lexeme::Ampersand
return (ops.front().op->type() == lexeme::Ampersand
|| ops.front().op->type() == lexeme::Tilde);
}
}
Expand Down Expand Up @@ -1046,6 +1046,13 @@ struct type_id_node
return false;
}

auto is_concept() const
-> bool
{
auto tok = get_token();
return tok && *tok == "concept";
}

auto template_args_count() const
-> int
{
Expand Down Expand Up @@ -2488,6 +2495,8 @@ struct declaration_node
{ return type.index() == a_function; }
auto is_object () const -> bool
{ return type.index() == an_object; }
auto is_concept () const -> bool
{ return type.index() == an_object && get<an_object>(type)->is_concept(); }
auto is_type () const -> bool
{ return type.index() == a_type; }
auto is_namespace() const -> bool
Expand Down Expand Up @@ -3993,7 +4002,7 @@ class parser
// || curr().type() == lexeme::LeftBrace
)
{
bool inside_initializer = (
bool inside_initializer = (
peek(-1) && peek(-1)->type() == lexeme::Assignment
);
auto open_paren = &curr();
Expand All @@ -4015,12 +4024,12 @@ class parser
next();
if (
curr().type() != lexeme::Semicolon
&& curr().type() != lexeme::RightParen
&& curr().type() != lexeme::RightBracket
&& curr().type() != lexeme::RightParen
&& curr().type() != lexeme::RightBracket
&& curr().type() != lexeme::Comma
) {
expr_list->inside_initializer = false;
}
}
n->expr = std::move(expr_list);
return n;
}
Expand Down Expand Up @@ -4374,7 +4383,7 @@ class parser
//G shift-expression '<<' additive-expression
//G shift-expression '>>' additive-expression
//G
auto shift_expression(bool allow_angle_operators = true)
auto shift_expression(bool allow_angle_operators = true)
-> auto
{
if (allow_angle_operators) {
Expand Down Expand Up @@ -4409,7 +4418,7 @@ class parser
//G shift-expression
//G compare-expression '<=>' shift-expression
//G
auto compare_expression(bool allow_angle_operators = true)
auto compare_expression(bool allow_angle_operators = true)
-> auto
{
return binary_expression<compare_expression_node> (
Expand All @@ -4425,7 +4434,7 @@ class parser
//G relational-expression '<=' compare-expression
//G relational-expression '>=' compare-expression
//G
auto relational_expression(bool allow_angle_operators = true)
auto relational_expression(bool allow_angle_operators = true)
-> auto
{
if (allow_angle_operators) {
Expand Down Expand Up @@ -4457,7 +4466,7 @@ class parser
//G equality-expression '==' relational-expression
//G equality-expression '!=' relational-expression
//G
auto equality_expression(bool allow_angle_operators = true)
auto equality_expression(bool allow_angle_operators = true)
-> auto
{
return binary_expression<equality_expression_node> (
Expand All @@ -4470,7 +4479,7 @@ class parser
//G equality-expression
//G bit-and-expression '&' equality-expression
//G
auto bit_and_expression(bool allow_angle_operators = true)
auto bit_and_expression(bool allow_angle_operators = true)
-> auto
{
return binary_expression<bit_and_expression_node> (
Expand All @@ -4483,7 +4492,7 @@ class parser
//G bit-and-expression
//G bit-xor-expression '^' bit-and-expression
//G
auto bit_xor_expression(bool allow_angle_operators = true)
auto bit_xor_expression(bool allow_angle_operators = true)
-> auto
{
return binary_expression<bit_xor_expression_node> (
Expand All @@ -4496,7 +4505,7 @@ class parser
//G bit-xor-expression
//G bit-or-expression '|' bit-xor-expression
//G
auto bit_or_expression(bool allow_angle_operators = true)
auto bit_or_expression(bool allow_angle_operators = true)
-> auto
{
return binary_expression<bit_or_expression_node> (
Expand All @@ -4509,7 +4518,7 @@ class parser
//G bit-or-expression
//G logical-and-expression '&&' bit-or-expression
//G
auto logical_and_expression(bool allow_angle_operators = true)
auto logical_and_expression(bool allow_angle_operators = true)
-> auto
{
return binary_expression<logical_and_expression_node> (
Expand All @@ -4524,7 +4533,7 @@ class parser
//G logical-and-expression
//G logical-or-expression '||' logical-and-expression
//G
auto logical_or_expression(bool allow_angle_operators = true)
auto logical_or_expression(bool allow_angle_operators = true)
-> auto
{
return binary_expression<logical_or_expression_node> (
Expand Down Expand Up @@ -4842,7 +4851,7 @@ class parser

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

auto term = unqualified_id_node::term{};

do {
Expand Down Expand Up @@ -6413,7 +6422,7 @@ class parser
}
assert (n->is_type());
}

// Or a function type, declaring a function - and tell the function whether it's in a user-defined type
else if (auto t = function_type(n.get(), named))
{
Expand Down Expand Up @@ -6561,11 +6570,11 @@ class parser
)
{
auto& type = std::get<declaration_node::an_object>(n->type);
// object initialized by the address of the curr() object
// object initialized by the address of the curr() object
if (peek(1)->type() == lexeme::Ampersand) {
type->address_of = &curr();
}
// object initialized by (potentially multiple) dereference of the curr() object
// object initialized by (potentially multiple) dereference of the curr() object
else if (peek(1)->type() == lexeme::Multiply) {
type->dereference_of = &curr();
for (int i = 1; peek(i)->type() == lexeme::Multiply; ++i)
Expand Down Expand Up @@ -6770,7 +6779,7 @@ class parser
return {};
}
if (
t->is_wildcard()
t->is_wildcard()
|| ( t->get_token() && t->get_token()->to_string(true) == "auto" )
) {
errors.emplace_back(
Expand Down