From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id B2EA13810AC8; Wed, 8 Jun 2022 12:52:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B2EA13810AC8 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] ast: Dump Traits properly X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 2ad8ec6da7a871debda2e4ce94a63696b7d43422 X-Git-Newrev: 4d021b8efb6cdddd6fc860738f0b6c7ab2022fe7 Message-Id: <20220608125231.B2EA13810AC8@sourceware.org> Date: Wed, 8 Jun 2022 12:52:31 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:52:31 -0000 https://gcc.gnu.org/g:4d021b8efb6cdddd6fc860738f0b6c7ab2022fe7 commit 4d021b8efb6cdddd6fc860738f0b6c7ab2022fe7 Author: Arthur Cohen Date: Thu Jun 2 17:53:30 2022 +0200 ast: Dump Traits properly Diff: --- gcc/rust/ast/rust-ast-dump.cc | 76 ++++++++++++++++++++++++++++++++++++++++--- gcc/rust/ast/rust-ast-dump.h | 4 +++ gcc/rust/ast/rust-item.h | 24 +++----------- 3 files changed, 79 insertions(+), 25 deletions(-) diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index c23720b59d1..cdcb5632cc7 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -514,25 +514,91 @@ void Dump::visit (StaticItem &static_item) {} +void +Dump::format_function_common (std::unique_ptr &return_type, + std::unique_ptr &block) +{ + if (return_type) + { + stream << "-> "; + return_type->accept_vis (*this); + } + + if (block) + { + if (return_type) + stream << ' '; + block->accept_vis (*this); + } + else + stream << ";\n"; +} + void Dump::visit (TraitItemFunc &item) -{} +{ + auto func = item.get_trait_function_decl (); + stream << indentation << "fn " << func.get_identifier () << '('; + + auto ¶ms = func.get_function_params (); + for (auto ¶m : params) + { + stream << ", "; + format_function_param (param); + } + + stream << ") "; + + format_function_common (func.get_return_type (), item.get_definition ()); +} void Dump::visit (TraitItemMethod &item) -{} +{ + auto method = item.get_trait_method_decl (); + stream << indentation << "fn " << method.get_identifier () << '('; + + auto &self = method.get_self_param (); + stream << self.as_string (); + + auto ¶ms = method.get_function_params (); + for (auto ¶m : params) + { + stream << ", "; + format_function_param (param); + } + + stream << ") "; + + format_function_common (method.get_return_type (), item.get_definition ()); +} void Dump::visit (TraitItemConst &item) -{} +{ + stream << indentation << "const " << item.get_identifier () << ": "; + item.get_type ()->accept_vis (*this); + stream << ";\n"; +} void Dump::visit (TraitItemType &item) -{} +{ + stream << indentation << "type " << item.get_identifier () << ";\n"; +} void Dump::visit (Trait &trait) -{} +{ + stream << "trait " << trait.get_identifier () << " {\n"; + indentation.increment (); + + for (auto &item : trait.get_trait_items ()) + item->accept_vis (*this); + + indentation.decrement (); + stream << "\n}\n"; +} void Dump::visit (InherentImpl &impl) diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 436c2b523dd..e6c6ca4bc6f 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -55,6 +55,10 @@ private: std::ostream &stream; Indent indentation; + // Format together common items of functions: Parameters, return type, block + void format_function_common (std::unique_ptr &return_type, + std::unique_ptr &block); + /** * Format a function's definition parameter */ diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 4bed5af51ee..6d953fb128c 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -2872,11 +2872,7 @@ public: } // TODO: is this better? Or is a "vis_block" better? - std::unique_ptr &get_return_type () - { - rust_assert (has_return_type ()); - return return_type; - } + std::unique_ptr &get_return_type () { return return_type; } // TODO: is this better? Or is a "vis_block" better? WhereClause &get_where_clause () { return where_clause; } @@ -2954,11 +2950,7 @@ public: const std::vector &get_outer_attrs () const { return outer_attrs; } // TODO: is this better? Or is a "vis_block" better? - std::unique_ptr &get_definition () - { - rust_assert (has_definition ()); - return block_expr; - } + std::unique_ptr &get_definition () { return block_expr; } // TODO: is this better? Or is a "vis_block" better? TraitFunctionDecl &get_trait_function_decl () @@ -3097,11 +3089,7 @@ public: } // TODO: is this better? Or is a "vis_block" better? - std::unique_ptr &get_return_type () - { - rust_assert (has_return_type ()); - return return_type; - } + std::unique_ptr &get_return_type () { return return_type; } // TODO: is this better? Or is a "vis_block" better? WhereClause &get_where_clause () { return where_clause; } @@ -3189,11 +3177,7 @@ public: } // TODO: is this better? Or is a "vis_block" better? - std::unique_ptr &get_definition () - { - rust_assert (has_definition ()); - return block_expr; - } + std::unique_ptr &get_definition () { return block_expr; } protected: // Clone function implementation as (not pure) virtual method