From patchwork Tue Feb 21 12:00:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 59950 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1823764wrn; Tue, 21 Feb 2023 04:05:23 -0800 (PST) X-Google-Smtp-Source: AK7set8nxCgrxG6i50eTbFaDe91t7CkdozHl1fShPTLHDUZ46nepbeagdXzJlOdk6w1Ev7/VWpBs X-Received: by 2002:a17:907:cc97:b0:88f:13f0:4565 with SMTP id up23-20020a170907cc9700b0088f13f04565mr8653916ejc.69.1676981122973; Tue, 21 Feb 2023 04:05:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676981122; cv=none; d=google.com; s=arc-20160816; b=FLR+qQJ/H5YXNvCNa/O1JbHk8vgtfFo6cSNR7yWyd3NembEBtMV+/9FggRB/XPOB5z SunOfKLbLX+XDmCKH2m1x3K2FajLdc+9Fb0p+FryN5c1fS9W2VxzvcQzc7tsnbG3Wir1 ro2YxLsJkQn9WUWwazInHPXgvQa4O+84XjHrxMg7S5+itKmFx8tvCXvXSZ4+DdWXbn0P ObNQCDXgtLJwJqsaJJSSVIFIKh6erQm58RLcYld+Lmz6HjMdWOUfm87qLZaV/jBSULXy 4K/MOqAjeobdPqurhT/Z+qZYl1CMLJ0bNy8xWX9+zOQOA4pP/LqQ2+4bSaO2lViRFYCV CBmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dmarc-filter :delivered-to; bh=zknrg5IxglaK3KByVVKC5dlYvyS0JUps+E2x3OUOq98=; b=LWjH5SAZ/oMu9g4pOpb5EIEHcD9nVFZRRoaJHCQ5PgboQ4ysoWjLKDOIh+wZnJajUb 2Sc3ULRZaMrFU/r/CFehii0q8WY4cKKtUk8TAFFG4f+QEanNqIiXehL2XY2llSJhuuz+ zmkYgfBwMvJ4YaZvvKte85dv/Z2Hy1KjSVkCyDm2huSvNHBn+Rxsotb6YY+EyhE8Dxqb 81vN1UJpS12CtS6rMsgRO6MRmAC2jF296e5bdE1TgD62Y7rihrHienZPO9J4E1kR3Vos pWP30VRvJEOE8hCj4t5/NsiQsA9aqfhu4Hawp+7o56i6Pch6muZULSmkLmAxrwMPOb8N VEVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=WSBDzgIM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bf22-20020a170906c51600b008b1816e7564si13513333ejb.100.2023.02.21.04.05.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:05:22 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=WSBDzgIM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4C51E384F493 for ; Tue, 21 Feb 2023 12:03:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 48F85384F4AF for ; Tue, 21 Feb 2023 12:03:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 48F85384F4AF 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 r7so3989830wrz.6 for ; Tue, 21 Feb 2023 04:03:28 -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=aTlY8Ypc9bm4LeeIZ6oLOckSb/bb4mSYuBYoSKEesUL4fKLmtAdzflpmT5IKkUJSXE IS6QVHblecet8rkQ7W2i0Go4JD/KG4/tAubn6PqSKS+Di+Zb//xV3IS1sEINPn4MXN2t tBq4Z6wbTNQX+eKPGwPiocaZxE3QWYs0qPAfmJli+Q3Pi8GbG9zppv+vBhNmDypPEbpm aFVBrr/9wMsnJ4YIECrXOg/zEh+woAlmJci3Cpnlza7tUFh4Gp1Lbvu3GmUSChGhw337 jA7Hd/4/thGDuCqIMMCHPOWBquhZmUiTE6azZn8omMJe1fTkFRR81r/V3s9HveczBYi4 nSjA== X-Gm-Message-State: AO0yUKVhWKEVHgHyT8kNezHONkWQ/6c+jE+nVQo8h5WzlW1TQHN1Jvqx Zo+hbH4hL3fNtw3G+GrLQToDYGioxUHbS5v8YA== 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> MIME-Version: 1.0 X-Spam-Status: No, score=-14.3 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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: arthur.cohen@embecosm.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758442157789204990?= X-GMAIL-MSGID: =?utf-8?q?1758442157789204990?= 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);