From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id 08524384F4AA for ; Tue, 21 Feb 2023 12:03:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 08524384F4AA Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wr1-x42e.google.com with SMTP id l25so3756612wrb.3 for ; Tue, 21 Feb 2023 04:03:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=zknrg5IxglaK3KByVVKC5dlYvyS0JUps+E2x3OUOq98=; b=WSBDzgIMG48rPqNfaXYM7djMbx272ZPTwHHEtvVnN96zchJD95ETG8Wtod6jQlNR2V njrYSSnT7JFsicf47W8qC4sXTngNXmWRIjCi+SamVVN3R4ASKBWmxT1EWuHWuG5/ysm+ r3AypLee/olZPjOne8rJQ8aW7/wj0eCRqeOJNJ0nb4SMefpxQTyCEBlaFRWe1Mwji13q HAyPcRl8SWf3iyaBeEvJMyZTFNCqq1qdvL/irLDg50HPUDPWkFjaA+PoZzQ78soaEhgF ZuZ/ci3mUHHI/44vzEqOMf5iCJ/kpthA42tF/5cf7UYyU2ci/8aNCiU2lQdD0lP0bxve DjbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=zknrg5IxglaK3KByVVKC5dlYvyS0JUps+E2x3OUOq98=; b=WBAwOTjigcy6fuIZS1MzYhtoWU21GzgcM65tpX9O/mE9G6LVyclhO0dK6H+Ybz76BY owJNr5sJF5Ecpkek2RJNBOHjtfmk02hagJRYiYaWs/p857qPGqL2ZpRFCmVX+pPKi/FZ DOYCn7+jMbKBxhN3VJuD+dOblliEFv7ojq0uVJjaLvxcEu1m1KJGSQoQjhIVh+S8j9NA oYr9lxTileUafJ4/9rSCur4/Nc9rgXrgCaMmrgeG2a1phFMF9jV6/YC5cwAeWDG40IRv wZ+WyBwj3URGCRHOa59BacD0MlTypoBSBTwrq0xOe7WGFO5uqyJqDJhqBGr4kEjLEgDs sdRQ== X-Gm-Message-State: AO0yUKVMjjMPkREPzyyZol8/Ju2qww/B2m999KIy4riRNLPS1cLXsh4Z Pd0d3AJ9RavKsaNoXkzdDPD+ X-Google-Smtp-Source: AK7set9xmmMzlCfqhVLqQnX5cqcI+0Rrto7OBG3YyiotewS+R/tSxBQBeRhjrGiWQqSVaZ7CA7Ftig== X-Received: by 2002:a5d:65c5:0:b0:2c5:5a65:799f with SMTP id e5-20020a5d65c5000000b002c55a65799fmr4349916wrw.34.1676981006649; Tue, 21 Feb 2023 04:03:26 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id c15-20020adffb4f000000b002c55b0e6ef1sm5013811wrs.4.2023.02.21.04.03.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:03:26 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [committed 003/103] gccrs: dump: Emit visibility when dumping items Date: Tue, 21 Feb 2023 13:00:53 +0100 Message-Id: <20230221120230.596966-4-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230221120230.596966-1-arthur.cohen@embecosm.com> References: <20230221120230.596966-1-arthur.cohen@embecosm.com> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Arthur Cohen gcc/rust/ChangeLog: * ast/rust-ast-dump.cc (Dump::emit_visibility): New function. (Dump::visit): Call into `emit_visibility`. (Dump::format_function_common): Likewise. * ast/rust-ast-dump.h: Declare `emit_visibility`. --- gcc/rust/ast/rust-ast-dump.cc | 142 +++++++++++++++++++++++++--------- gcc/rust/ast/rust-ast-dump.h | 17 +++- 2 files changed, 123 insertions(+), 36 deletions(-) diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 0b2dac9b2ff..3d1b42d70e3 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2023 Free Software Foundation, Inc. +// Copyright (C) 2020-2022 Free Software Foundation, Inc. // This file is part of GCC. @@ -17,6 +17,7 @@ // . #include "rust-ast-dump.h" +#include "rust-diagnostics.h" namespace Rust { namespace AST { @@ -51,7 +52,7 @@ Dump::go (AST::Crate &crate) { stream << indentation; item->accept_vis (*this); - stream << "\n"; + stream << '\n'; } } @@ -72,8 +73,7 @@ Dump::format_function_param (FunctionParam ¶m) void Dump::emit_attrib (const Attribute &attrib) { - stream << "#"; - stream << "["; + stream << "#["; for (size_t i = 0; i < attrib.get_path ().get_segments ().size (); i++) { @@ -108,6 +108,38 @@ Dump::emit_attrib (const Attribute &attrib) stream << "]"; } +void +Dump::emit_visibility (const Visibility &vis) +{ + switch (vis.get_vis_type ()) + { + case Visibility::PUB: + stream << "pub "; + break; + case Visibility::PUB_CRATE: + stream << "pub(crate) "; + break; + case Visibility::PUB_SELF: + stream << "pub(self) "; + break; + case Visibility::PUB_SUPER: + stream << "pub(super) "; + break; + case Visibility::PUB_IN_PATH: + stream << "pub(in " << vis.get_path ().as_string () << ") "; + break; + case Visibility::PRIV: + break; + } +} + +std::ostream & +Dump::emit_indented_string (const std::string &value, + const std::string &comment) +{ + return stream << indentation << value << comment; +} + void Dump::visit (Token &tok) {} @@ -141,7 +173,9 @@ Dump::visit (ConstGenericParam &lifetime_param) // rust-path.h void Dump::visit (PathInExpression &path) -{} +{ + stream << path.as_string (); +} void Dump::visit (TypePathSegment &segment) @@ -163,7 +197,9 @@ Dump::visit (TypePath &path) void Dump::visit (QualifiedPathInExpression &path) -{} +{ + stream << path.as_string (); +} void Dump::visit (QualifiedPathInType &path) @@ -207,53 +243,52 @@ Dump::visit (NegationExpr &expr) void Dump::visit (ArithmeticOrLogicalExpr &expr) { - expr.get_left_expr ()->accept_vis (*this); - stream << " "; - + auto op = ""; switch (expr.get_expr_type ()) { case ArithmeticOrLogicalOperator::ADD: - stream << "+"; + op = "+"; break; case ArithmeticOrLogicalOperator::SUBTRACT: - stream << "-"; + op = "-"; break; case ArithmeticOrLogicalOperator::MULTIPLY: - stream << "*"; + op = "*"; break; case ArithmeticOrLogicalOperator::DIVIDE: - stream << "/"; + op = "/"; break; case ArithmeticOrLogicalOperator::MODULUS: - stream << "%"; + op = "%"; break; case ArithmeticOrLogicalOperator::BITWISE_AND: - stream << "&"; + op = "&"; break; case ArithmeticOrLogicalOperator::BITWISE_OR: - stream << "|"; + op = "|"; break; case ArithmeticOrLogicalOperator::BITWISE_XOR: - stream << "^"; + op = "^"; break; case ArithmeticOrLogicalOperator::LEFT_SHIFT: - stream << "<<"; + op = "<<"; break; case ArithmeticOrLogicalOperator::RIGHT_SHIFT: - stream << ">>"; + op = ">>"; break; } - stream << " "; + expr.get_left_expr ()->accept_vis (*this); + stream << " " << op << " "; expr.get_right_expr ()->accept_vis (*this); } @@ -331,7 +366,23 @@ Dump::visit (StructExprStructBase &expr) void Dump::visit (CallExpr &expr) -{} +{ + expr.get_function_expr ()->accept_vis (*this); + stream << '('; + + indentation.increment (); + + for (auto &arg : expr.get_params ()) + { + stream << '\n' << indentation; + arg->accept_vis (*this); + stream << ','; + } + + indentation.decrement (); + + stream << '\n' << indentation << ')'; +} void Dump::visit (MethodCallExpr &expr) @@ -355,13 +406,14 @@ Dump::visit (BlockExpr &expr) { stream << indentation; stmt->accept_vis (*this); - stream << ";\n"; + stream << "; /* stmt */\n"; } if (expr.has_tail_expr ()) { stream << indentation; expr.get_tail_expr ()->accept_vis (*this); + stream << " /* tail expr */"; } indentation.decrement (); @@ -495,7 +547,10 @@ Dump::visit (TypeBoundWhereClauseItem &item) void Dump::visit (Method &method) { - stream << indentation << "fn " << method.get_method_name () << '('; + // FIXME: Do we really need to dump the indentation here? + stream << indentation; + emit_visibility (method.get_visibility ()); + stream << "fn " << method.get_method_name () << '('; auto &self = method.get_self_param (); stream << self.as_string (); @@ -552,6 +607,7 @@ Dump::visit (UseDeclaration &use_decl) void Dump::visit (Function &function) { + emit_visibility (function.get_visibility ()); stream << "fn " << function.get_function_name (); if (function.has_generics ()) @@ -647,6 +703,7 @@ void Dump::format_function_common (std::unique_ptr &return_type, std::unique_ptr &block) { + // FIXME: This should format the ` fn ( [args] )` as well if (return_type) { stream << "-> "; @@ -656,8 +713,10 @@ Dump::format_function_common (std::unique_ptr &return_type, if (block) { if (return_type) - stream << ' '; - block->accept_vis (*this); + { + stream << ' '; + block->accept_vis (*this); + } } else stream << ";\n"; @@ -685,7 +744,13 @@ void Dump::visit (TraitItemMethod &item) { auto method = item.get_trait_method_decl (); - stream << indentation << "fn " << method.get_identifier () << '('; + + // FIXME: Do we really need to dump the indentation here? + stream << indentation; + + // FIXME: Can we have visibility here? + // emit_visibility (method.get_visibility ()); + stream << "fn " << method.get_identifier () << '('; auto &self = method.get_self_param (); stream << self.as_string (); @@ -725,6 +790,8 @@ Dump::visit (Trait &trait) stream << "\n" << indentation; } + emit_visibility (trait.get_visibility ()); + stream << "trait " << trait.get_identifier (); // Traits actually have an implicit Self thrown at the start so we must expect @@ -784,12 +851,15 @@ Dump::visit (TraitImpl &impl) impl.get_trait_path ().accept_vis (*this); stream << " for "; impl.get_type ()->accept_vis (*this); - stream << " {\n"; + indentation.increment (); for (auto &item : impl.get_impl_items ()) - item->accept_vis (*this); + { + stream << indentation; + item->accept_vis (*this); + } indentation.decrement (); stream << "\n}\n"; @@ -802,6 +872,8 @@ Dump::visit (ExternalStaticItem &item) void Dump::visit (ExternalFunctionItem &function) { + emit_visibility (function.get_visibility ()); + stream << "fn " << function.get_identifier () << '('; for (size_t i = 0; i < function.get_function_params ().size (); i++) @@ -830,11 +902,7 @@ Dump::visit (ExternBlock &block) stream << "extern "; if (block.has_abi ()) - { - stream << "\""; - stream << block.get_abi (); - stream << "\" "; - } + stream << "\"" << block.get_abi () << "\" "; stream << "{\n"; indentation.increment (); @@ -1014,11 +1082,15 @@ Dump::visit (LetStmt &stmt) void Dump::visit (ExprStmtWithoutBlock &stmt) -{} +{ + stmt.get_expr ()->accept_vis (*this); +} void Dump::visit (ExprStmtWithBlock &stmt) -{} +{ + stmt.get_expr ()->accept_vis (*this); +} // rust-type.h void diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 955dbc0bebc..a5a99f2b03e 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2023 Free Software Foundation, Inc. +// Copyright (C) 2020-2022 Free Software Foundation, Inc. // This file is part of GCC. @@ -80,8 +80,23 @@ private: * Format a function's definition parameter */ void format_function_param (FunctionParam ¶m); + + /** + * Emit an attribute + */ void emit_attrib (const Attribute &attrib); + /** + * Emit an item's visibility + */ + void emit_visibility (const Visibility &vis); + + /** + * Emit an indented string with an optional extra comment + */ + std::ostream &emit_indented_string (const std::string &value, + const std::string &comment = ""); + // rust-ast.h void visit (Token &tok); void visit (DelimTokenTree &delim_tok_tree); -- 2.39.1