From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 28D6D3858D32; Fri, 15 Jul 2022 21:38:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 28D6D3858D32 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] Add support for ast with generic traits X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 67f9b173b9061ceef0fd96413578b0e5ddcac061 X-Git-Newrev: ecd1908fc20a18631725087abe3c48383bdc992a Message-Id: <20220715213807.28D6D3858D32@sourceware.org> Date: Fri, 15 Jul 2022 21:38:07 +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: Fri, 15 Jul 2022 21:38:07 -0000 https://gcc.gnu.org/g:ecd1908fc20a18631725087abe3c48383bdc992a commit ecd1908fc20a18631725087abe3c48383bdc992a Author: Philip Herron Date: Fri Jul 15 15:57:03 2022 +0100 Add support for ast with generic traits This allows AST dump of attributes on Traits this code needs to be applied for all attribute dumps. Traits also have an implicit Self generic param this ensure we ignore this during an AST dump. Diff: --- gcc/rust/ast/rust-ast-dump.cc | 69 +++++++++++++++++++++++++++++++++++++++++-- gcc/rust/ast/rust-ast-dump.h | 1 + 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 3b02d84c362..ad9ad0b7de7 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -69,6 +69,45 @@ Dump::format_function_param (FunctionParam ¶m) param.get_type ()->accept_vis (*this); } +void +Dump::emit_attrib (const Attribute &attrib) +{ + stream << "#"; + stream << "["; + + for (size_t i = 0; i < attrib.get_path ().get_segments ().size (); i++) + { + const auto &seg = attrib.get_path ().get_segments ().at (i); + bool has_next = (i + 1) < attrib.get_path ().get_segments ().size (); + + stream << seg.get_segment_name (); + if (has_next) + stream << "::"; + } + + if (attrib.has_attr_input ()) + { + stream << " = "; + + bool is_literal = attrib.get_attr_input ().get_attr_input_type () + == AST::AttrInput::AttrInputType::LITERAL; + if (is_literal) + { + auto &literal + = static_cast (attrib.get_attr_input ()); + const auto &value = literal.get_literal ().as_string (); + + stream << "\"" << value << "\""; + } + else + { + stream << "FIXME"; + } + } + + stream << "]"; +} + void Dump::visit (Token &tok) {} @@ -440,7 +479,7 @@ Dump::visit (TypeParam ¶m) stream << param.get_type_representation (); if (param.has_type ()) { - stream << ": "; + stream << " = "; param.get_type ()->accept_vis (*this); } } @@ -680,7 +719,33 @@ Dump::visit (TraitItemType &item) void Dump::visit (Trait &trait) { - stream << "trait " << trait.get_identifier () << " {\n"; + for (const auto &attr : trait.get_outer_attrs ()) + { + emit_attrib (attr); + stream << "\n" << indentation; + } + + stream << "trait " << trait.get_identifier (); + + // Traits actually have an implicit Self thrown at the start so we must expect + // the number of generic params to be > 1 + if (trait.get_generic_params ().size () > 1) + { + stream << "<"; + for (size_t i = 1; i < trait.get_generic_params ().size (); i++) + { + auto ¶m = trait.get_generic_params ().at (i); + param->accept_vis (*this); + + bool has_next = (i + 1) < trait.get_generic_params ().size (); + if (has_next) + stream << ", "; + } + stream << ">"; + } + + stream << " {\n"; + indentation.increment (); for (auto &item : trait.get_trait_items ()) diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index d74d88759b0..c3854e8287d 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -64,6 +64,7 @@ private: * Format a function's definition parameter */ void format_function_param (FunctionParam ¶m); + void emit_attrib (const Attribute &attrib); // rust-ast.h void visit (Token &tok);