diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-06-02 17:53:30 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-06-04 09:37:38 +0200 |
commit | 4d021b8efb6cdddd6fc860738f0b6c7ab2022fe7 (patch) | |
tree | d306b2eb73c06ddc15193e359426bf6139891d48 /gcc | |
parent | 2ad8ec6da7a871debda2e4ce94a63696b7d43422 (diff) | |
download | gcc-4d021b8efb6cdddd6fc860738f0b6c7ab2022fe7.zip gcc-4d021b8efb6cdddd6fc860738f0b6c7ab2022fe7.tar.gz gcc-4d021b8efb6cdddd6fc860738f0b6c7ab2022fe7.tar.bz2 |
ast: Dump Traits properly
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-ast-dump.cc | 76 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast-dump.h | 4 | ||||
-rw-r--r-- | 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 c23720b..cdcb563 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -515,24 +515,90 @@ Dump::visit (StaticItem &static_item) {} void +Dump::format_function_common (std::unique_ptr<Type> &return_type, + std::unique_ptr<BlockExpr> &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 436c2b5..e6c6ca4 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<Type> &return_type, + std::unique_ptr<BlockExpr> &block); + /** * Format a function's definition parameter */ diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 4bed5af..6d953fb 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<Type> &get_return_type () - { - rust_assert (has_return_type ()); - return return_type; - } + std::unique_ptr<Type> &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<Attribute> &get_outer_attrs () const { return outer_attrs; } // TODO: is this better? Or is a "vis_block" better? - std::unique_ptr<BlockExpr> &get_definition () - { - rust_assert (has_definition ()); - return block_expr; - } + std::unique_ptr<BlockExpr> &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<Type> &get_return_type () - { - rust_assert (has_return_type ()); - return return_type; - } + std::unique_ptr<Type> &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<BlockExpr> &get_definition () - { - rust_assert (has_definition ()); - return block_expr; - } + std::unique_ptr<BlockExpr> &get_definition () { return block_expr; } protected: // Clone function implementation as (not pure) virtual method |