From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id 40808384FB58; Tue, 21 Feb 2023 12:02:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 40808384FB58 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676980922; bh=WK4KtlYhcmxOwzx+W2Nw294Ej2PRuZdICCldenziPZM=; h=From:To:Subject:Date:From; b=Q4rY5U62yPKsQyhQa5VX79e9+TvB4X9VU0/GhPovP0TXpupSSaI4SGe2RjKOogq/l GnxZttfAUmJhJFB2WYoR3Qsw1H0YF2Q6VXacANdAGFlPvdtpKuzFb334NpFavKaktE SOA6K25+CK6qhUx1K+DV3EBtE7Pipq5arn4Y///4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-6233] gccrs: ast: Dump bare function type X-Act-Checkin: gcc X-Git-Author: Jakub Dupak X-Git-Refname: refs/heads/master X-Git-Oldrev: ee0166fa1b88e2c28a61f8755c0cb192e7fd0b05 X-Git-Newrev: ae1f6b3a96dd8cfae914ab55411b769d245e48f9 Message-Id: <20230221120202.40808384FB58@sourceware.org> Date: Tue, 21 Feb 2023 12:02:02 +0000 (GMT) List-Id: https://gcc.gnu.org/g:ae1f6b3a96dd8cfae914ab55411b769d245e48f9 commit r13-6233-gae1f6b3a96dd8cfae914ab55411b769d245e48f9 Author: Jakub Dupak Date: Mon Nov 7 17:09:33 2022 +0100 gccrs: ast: Dump bare function type + Return FunctionQualifiers as ref to work in ast dump gcc/rust/ChangeLog: * ast/rust-ast-dump.cc (Dump::visit): Add missing visitor. * ast/rust-ast-dump.h: Add missing getter declaration. * ast/rust-ast-full-test.cc (BareFunctionType::as_string): Fix bare function string representation. * ast/rust-type.h (class BareFunctionType): Declare said getter. Signed-off-by: Jakub Dupak Diff: --- gcc/rust/ast/rust-ast-dump.cc | 94 +++++++++++++++++++++++++++++++++++++- gcc/rust/ast/rust-ast-dump.h | 2 + gcc/rust/ast/rust-ast-full-test.cc | 2 +- gcc/rust/ast/rust-type.h | 22 ++++++--- 4 files changed, 111 insertions(+), 9 deletions(-) diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index a37d5addbc4..5dae38ccef9 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -249,6 +249,56 @@ Dump::visit (std::vector &for_lifetimes) stream << "> "; } +void +Dump::visit (FunctionQualifiers &qualifiers) +{ + // Syntax: + // `const`? `async`? `unsafe`? (`extern` Abi?)? + // unsafe? (extern Abi?)? + + switch (qualifiers.get_const_status ()) + { + case NONE: + break; + case CONST_FN: + stream << "const "; + break; + case ASYNC_FN: + stream << "async "; + break; + } + + if (qualifiers.is_unsafe ()) + stream << "unsafe "; + if (qualifiers.is_extern ()) + { + stream << "extern "; + if (qualifiers.has_abi ()) + stream << "\"" << qualifiers.get_extern_abi () << "\" "; + } +} // namespace AST + +void +Dump::visit (MaybeNamedParam ¶m) +{ + // Syntax: + // OuterAttribute* ( ( IDENTIFIER | _ ) : )? Type + + visit_items_joined_by_separator (param.get_outer_attrs (), " "); + switch (param.get_param_kind ()) + { + case MaybeNamedParam::UNNAMED: + break; + case MaybeNamedParam::IDENTIFIER: + stream << " " << param.get_name () << ": "; + break; + case MaybeNamedParam::WILDCARD: + stream << " _: "; + break; + } + visit (param.get_type ()); +} + void Dump::visit (Token &tok) { @@ -1668,8 +1718,48 @@ Dump::visit (InferredType &) } void -Dump::visit (BareFunctionType &) -{} +Dump::visit (BareFunctionType &type) +{ + // Syntax: + // ForLifetimes? FunctionTypeQualifiers fn + // ( FunctionParametersMaybeNamedVariadic? ) BareFunctionReturnType? + // + // BareFunctionReturnType: + // -> TypeNoBounds + // + // FunctionParametersMaybeNamedVariadic : + // MaybeNamedFunctionParameters | MaybeNamedFunctionParametersVariadic + // + // MaybeNamedFunctionParameters : + // MaybeNamedParam ( , MaybeNamedParam )* ,? + // + // MaybeNamedFunctionParametersVariadic : + // ( MaybeNamedParam , )* MaybeNamedParam , OuterAttribute* ... + + if (type.has_for_lifetimes ()) + visit (type.get_for_lifetimes ()); + + visit (type.get_function_qualifiers ()); + + stream << "fn ("; + + visit_items_joined_by_separator (type.get_function_params (), ", "); + + if (type.is_variadic ()) + { + stream << ", "; + visit_items_joined_by_separator (type.get_variadic_attr (), " "); + stream << "..."; + } + + stream << ')'; + + if (type.has_return_type ()) + { + stream << " -> "; + visit (type.get_return_type ()); + } +} } // namespace AST } // namespace Rust diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 2bd3b31d09a..6c2f13c8c01 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -137,6 +137,8 @@ private: void visit (MacroRule &rule); void visit (WhereClause &rule); void visit (std::vector &for_lifetimes); + void visit (FunctionQualifiers &qualifiers); + void visit (MaybeNamedParam ¶m); // rust-ast.h void visit (Token &tok); diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index a4e059f1c0c..4f593dcbd49 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -3071,7 +3071,7 @@ BareFunctionType::as_string () const } str += "\n Is variadic: "; - if (is_variadic) + if (_is_variadic) str += "true"; else str += "false"; diff --git a/gcc/rust/ast/rust-type.h b/gcc/rust/ast/rust-type.h index 05a78956b57..efe59bd6f79 100644 --- a/gcc/rust/ast/rust-type.h +++ b/gcc/rust/ast/rust-type.h @@ -835,7 +835,7 @@ public: }; /* A function pointer type - can be created via coercion from function items and - * non- capturing closures. */ + * non-capturing closures. */ class BareFunctionType : public TypeNoBounds { // bool has_for_lifetimes; @@ -844,7 +844,7 @@ class BareFunctionType : public TypeNoBounds FunctionQualifiers function_qualifiers; std::vector params; - bool is_variadic; + bool _is_variadic; std::vector variadic_attrs; // bool has_return_type; @@ -860,6 +860,16 @@ public: // Whether the function has ForLifetimes. bool has_for_lifetimes () const { return !for_lifetimes.empty (); } + std::vector &get_for_lifetimes () { return for_lifetimes; } + + bool is_variadic () const { return _is_variadic; } + + std::vector &get_variadic_attr () { return variadic_attrs; }; + const std::vector &get_variadic_attr () const + { + return variadic_attrs; + }; + BareFunctionType (std::vector lifetime_params, FunctionQualifiers qualifiers, std::vector named_params, bool is_variadic, @@ -867,7 +877,7 @@ public: std::unique_ptr type, Location locus) : for_lifetimes (std::move (lifetime_params)), function_qualifiers (std::move (qualifiers)), - params (std::move (named_params)), is_variadic (is_variadic), + params (std::move (named_params)), _is_variadic (is_variadic), variadic_attrs (std::move (variadic_attrs)), return_type (std::move (type)), locus (locus) { @@ -879,7 +889,7 @@ public: BareFunctionType (BareFunctionType const &other) : for_lifetimes (other.for_lifetimes), function_qualifiers (other.function_qualifiers), params (other.params), - is_variadic (other.is_variadic), variadic_attrs (other.variadic_attrs), + _is_variadic (other._is_variadic), variadic_attrs (other.variadic_attrs), locus (other.locus) { // guard to prevent null dereference @@ -893,7 +903,7 @@ public: for_lifetimes = other.for_lifetimes; function_qualifiers = other.function_qualifiers; params = other.params; - is_variadic = other.is_variadic; + _is_variadic = other._is_variadic; variadic_attrs = other.variadic_attrs; locus = other.locus; @@ -930,7 +940,7 @@ public: return return_type; } - FunctionQualifiers get_function_qualifiers () { return function_qualifiers; } + FunctionQualifiers &get_function_qualifiers () { return function_qualifiers; } protected: /* Use covariance to implement clone function as returning this object rather