public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Add support for ast with generic traits Date: Fri, 15 Jul 2022 21:38:07 +0000 (GMT) [thread overview] Message-ID: <20220715213807.28D6D3858D32@sourceware.org> (raw) https://gcc.gnu.org/g:ecd1908fc20a18631725087abe3c48383bdc992a commit ecd1908fc20a18631725087abe3c48383bdc992a Author: Philip Herron <philip.herron@embecosm.com> 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<AST::AttrInputLiteral &> (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);
reply other threads:[~2022-07-15 21:38 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220715213807.28D6D3858D32@sourceware.org \ --to=tschwinge@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).