diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-06-07 09:19:25 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-07 09:19:25 +0000 |
commit | 9d81164aa0447c738fe0435de14ec9666a03d5da (patch) | |
tree | d306b2eb73c06ddc15193e359426bf6139891d48 /gcc | |
parent | 957914b4284213b1d72c9c4210892367acaf1419 (diff) | |
parent | 4d021b8efb6cdddd6fc860738f0b6c7ab2022fe7 (diff) | |
download | gcc-9d81164aa0447c738fe0435de14ec9666a03d5da.zip gcc-9d81164aa0447c738fe0435de14ec9666a03d5da.tar.gz gcc-9d81164aa0447c738fe0435de14ec9666a03d5da.tar.bz2 |
Merge #1295
1295: AST Dump impl traits r=CohenArthur a=CohenArthur
This adds proper AST formatting for inherent impl blocks and traits.
This does not handle trait impls yet (`impl Trait for Type`) but it should be really easy to refactor and add.
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-ast-dump.cc | 159 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast-dump.h | 4 | ||||
-rw-r--r-- | gcc/rust/ast/rust-item.h | 24 |
3 files changed, 151 insertions, 36 deletions
diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 64966c9..cdcb563 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -102,7 +102,9 @@ Dump::visit (TypePathSegmentFunction &segment) void Dump::visit (TypePath &path) -{} +{ + stream << path.as_string (); +} void Dump::visit (QualifiedPathInExpression &path) @@ -253,8 +255,8 @@ Dump::visit (BlockExpr &expr) if (expr.has_tail_expr ()) expr.get_tail_expr ()->accept_vis (*this); - stream << "\n}\n"; - indentation.increment (); + indentation.decrement (); + stream << "\n" << indentation << "}\n"; } void @@ -364,7 +366,10 @@ Dump::visit (AsyncBlockExpr &expr) // rust-item.h void Dump::visit (TypeParam ¶m) -{} +{ + // Is it possible to have a null type here? + param.get_type ()->accept_vis (*this); +} void Dump::visit (LifetimeWhereClauseItem &item) @@ -376,7 +381,36 @@ Dump::visit (TypeBoundWhereClauseItem &item) void Dump::visit (Method &method) -{} +{ + stream << indentation << "fn " << method.get_method_name () << '('; + + 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 << ") "; + + if (method.has_return_type ()) + { + stream << "-> "; + method.get_return_type ()->accept_vis (*this); + stream << " "; + } + + auto &block = method.get_definition (); + if (!block) + stream << ';'; + else + block->accept_vis (*this); + + stream << '\n'; +} void Dump::visit (Module &module) @@ -405,7 +439,7 @@ Dump::visit (UseDeclaration &use_decl) void Dump::visit (Function &function) { - stream << "fn " << function.get_function_name () << '('; + stream << indentation << "fn " << function.get_function_name () << '('; auto ¶ms = function.get_function_params (); if (params.size () >= 1) @@ -424,6 +458,7 @@ Dump::visit (Function &function) { stream << "-> "; function.get_return_type ()->accept_vis (*this); + stream << " "; } auto &block = function.get_definition (); @@ -480,28 +515,112 @@ 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) -{} +{ + stream << "impl "; + + // FIXME: Handle generics + + impl.get_type ()->accept_vis (*this); + + // FIXME: Handle where-clause + // FIXME: Handle inner attributes + + stream << " {\n"; + indentation.increment (); + + for (auto &item : impl.get_impl_items ()) + item->accept_vis (*this); + + indentation.decrement (); + stream << "\n}\n"; +} void Dump::visit (TraitImpl &impl) @@ -728,19 +847,27 @@ Dump::visit (RawPointerType &type) void Dump::visit (ReferenceType &type) -{} +{ + type.get_type_referenced ()->accept_vis (*this); +} void Dump::visit (ArrayType &type) -{} +{ + type.get_elem_type ()->accept_vis (*this); +} void Dump::visit (SliceType &type) -{} +{ + type.get_elem_type ()->accept_vis (*this); +} void Dump::visit (InferredType &type) -{} +{ + stream << "_"; +} void Dump::visit (BareFunctionType &type) 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 |