From patchwork Tue Feb 21 12:01:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 59984 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1830813wrn; Tue, 21 Feb 2023 04:22:28 -0800 (PST) X-Google-Smtp-Source: AK7set+kGTZ5kouaUd/6vrQJF32hwHgmS0H04SvviYmgTh8kGQm3aRcQ97QwdBDFInb5URjstXbl X-Received: by 2002:a17:906:c04d:b0:8a4:e2aa:6cd9 with SMTP id bm13-20020a170906c04d00b008a4e2aa6cd9mr12459920ejb.19.1676982148199; Tue, 21 Feb 2023 04:22:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676982148; cv=none; d=google.com; s=arc-20160816; b=OuSHJ1nQuWgB5nyV4WqbuengQbvtvFccT5x8UQ+TaM3jqF03q+KpjuFcsD4dHTmAp1 j3yiMdHtCdpbf5aa5a30zDPb7vzAtG3XZVemWj4MPlJwWdb2YOmWZse37M7AALAzBhN/ hw6BIgterp4zutf6xYNCHLni75aGKX8Drzup+0z8xnAyFP4xfud3vAGzQFZ7UcmI+IID +bzRjC1+L3+hSfyP3rMl9nSZhMYlLgQOLP4hrvzrKLNBhs53AyRFCmLkwyu8k4S9ZPaP azed4U0fOrfJfhdmo50RPFOdq+qAkJJUjw8clQ7vu2ILt9e78rjrvfzu9gCmXzMMVpcA tTcA== 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=O8zwOMN0JVeRFxhdQL7TtArc9HsLUZ8u3Trm17NZxoc=; b=xAXT5ijYkA3llx6gGWXMNO0/duCT93gATFac/xZrlYPw5FgP4rpO1WiBxNKxLIJrLk 03EV0z1klbMo9d/w6yoe1t5Es7qnus+Gmu9bl0WhKy9LKbvvLXomV1ByVTO3vfdv+1z1 /8Ka4jeRRhsrJ+FpKSs5M1gXogAC5k4/ocmG/BU6DJq3a2/w6UilV3ON0sR7x02Xr7xv ryB1JOL6vluSIDWEvIwh9az1icO9e0ar3x6CiX74Fz9iPGV3hI0RM4cq/HnI50NP/Yn/ NehApYcm7afCBZqpCXw8dV2u/bwPdP2uTSwvExgSkKPM6XbA8ZVJsifORKD/f9kP2i82 kL8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=XMZBPYwz; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id 12-20020a170906010c00b008b17662c9dasi19794349eje.713.2023.02.21.04.22.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:22:28 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=XMZBPYwz; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 E1B08384D562 for ; Tue, 21 Feb 2023 12:09:26 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id D1DE5384F48B for ; Tue, 21 Feb 2023 12:03:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D1DE5384F48B 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-x433.google.com with SMTP id o4so4164560wrs.4 for ; Tue, 21 Feb 2023 04:03:53 -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=O8zwOMN0JVeRFxhdQL7TtArc9HsLUZ8u3Trm17NZxoc=; b=XMZBPYwzEEt/S2PgJsdaYseFAQFM8PHXKqaHkZtMIu3lpNZXLXY8iUj4XaxPdqZXIV aEDThZtEtBFv1Y9xVgnwCA+hv0Y2XliwAME5tIwfI6COV+pHgDYVA0GP3Y6EKwDtBQpU z46wG/oI41j8ABrbFp5X5ahV3iR7c4Cx+CYNRYKS7ig5rsDV5cEe+1bqlo3bRacC+F0d F+TyALA52z5bdijpqHKjCON/U5PXIsukAgzO8EaAqqATSjx9kAdiuPXoaaDkRhKDayGw L+wDVSoUvZ5TmdRGY/H9mVnoPwilZ/X6lRQTzp2U0Nf/WzYYB2pjminK3MIBxGCYkL9b Mhqw== 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=O8zwOMN0JVeRFxhdQL7TtArc9HsLUZ8u3Trm17NZxoc=; b=RfugntYCFl5Xc60Ygm754iDYvA7IjLSbrrEppl7IUr3pDflPMMjxMZolIkHrogr7FT RAQ27vhAkcPECE+dhGMTtvwd/B8p1gzyZfLwFjDPK/S1HrBuPLEXKM8J+URQNXjTx/Mz OzDjuCX8Q/JNsd+ey0H3HbOwGg39oOfVBuzVQd5wH7u1WtDEmdmYXGHE0Yw16LUHG4TO wwU8SGLYugKZvhyozhRTp7V5JPtZIcltz0L1CBanrf+RjZgDXHeygH15lB/jlGd+IVQq 2npOa3GFaGCtLVLOVLTGX4NSztQ4L3X0+DKNY+Nwc0W7FBqpw0EATKAzrYQyShCbdACG N+DA== X-Gm-Message-State: AO0yUKU4vsxY1buuO4yJumJo3G3LQhKFUH2HJS0A4l7QXkOJARaclbiE ulk5Qoi0FUCE7AZJnXiBQhAnfAaHVjbI+cIToA== X-Received: by 2002:a5d:6686:0:b0:2c5:58fd:a965 with SMTP id l6-20020a5d6686000000b002c558fda965mr2523691wru.35.1676981033231; Tue, 21 Feb 2023 04:03:53 -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.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:03:52 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [committed 038/103] gccrs: dump: Dump macro rules definition Date: Tue, 21 Feb 2023 13:01:28 +0100 Message-Id: <20230221120230.596966-39-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.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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?1758443233292816493?= X-GMAIL-MSGID: =?utf-8?q?1758443233292816493?= From: Arthur Cohen gcc/rust/ChangeLog: * ast/rust-ast-dump.cc (Dump::visit): Add missing visitors for macro definition dumping. (get_delimiters): New function. * ast/rust-ast-dump.h: Declare `get_delimiters` and add documentation. * ast/rust-macro.h: Add `get_token_tree` method. --- gcc/rust/ast/rust-ast-dump.cc | 126 +++++++++++++++++++++++++++++++--- gcc/rust/ast/rust-ast-dump.h | 16 +++-- gcc/rust/ast/rust-macro.h | 1 + 3 files changed, 131 insertions(+), 12 deletions(-) diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index a12c3922c47..4817962f767 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -175,11 +175,27 @@ Dump::format_struct_field (StructField &field) void Dump::visit (Token &tok) -{} +{ + stream << tok.as_string (); +} void Dump::visit (DelimTokenTree &delim_tok_tree) -{} +{ + auto tokens = delim_tok_tree.to_token_stream (); + + indentation.increment (); + stream << '\n' << indentation; + + for (const auto &tok : tokens) + { + stream << ' '; + tok->accept_vis (*this); + } + + indentation.decrement (); + stream << '\n' << indentation; +} void Dump::visit (AttrInputMetaItemContainer &input) @@ -1308,22 +1324,116 @@ Dump::visit (ExternBlock &block) stream << "\n" << indentation << "}\n"; } -// rust-macro.h +static std::pair +get_delimiters (DelimType delim) +{ + auto start_delim = '\0'; + auto end_delim = '\0'; + + switch (delim) + { + case PARENS: + start_delim = '('; + end_delim = ')'; + break; + case SQUARE: + start_delim = '['; + end_delim = ']'; + break; + case CURLY: + start_delim = '{'; + end_delim = '}'; + break; + } + + return {start_delim, end_delim}; +} + void Dump::visit (MacroMatchFragment &match) -{} +{ + stream << '$' << match.get_ident () << ':' + << match.get_frag_spec ().as_string (); +} void -Dump::visit (MacroMatchRepetition &match) -{} +Dump::visit (MacroMatchRepetition &repetition) +{ + stream << "$("; + + for (auto &match : repetition.get_matches ()) + { + match->accept_vis (*this); + stream << ' '; + } + + auto op_char = '\0'; + switch (repetition.get_op ()) + { + case MacroMatchRepetition::ANY: + op_char = '*'; + break; + case MacroMatchRepetition::ONE_OR_MORE: + op_char = '+'; + break; + case MacroMatchRepetition::ZERO_OR_ONE: + op_char = '?'; + break; + case MacroMatchRepetition::NONE: + break; + } + + stream << ')'; + + if (repetition.has_sep ()) + stream << repetition.get_sep ()->as_string (); + + stream << op_char; +} void Dump::visit (MacroMatcher &matcher) -{} +{ + auto delimiters = get_delimiters (matcher.get_delim_type ()); + + stream << delimiters.first; + + for (auto &match : matcher.get_matches ()) + { + match->accept_vis (*this); + stream << ' '; + } + + stream << delimiters.second; +} void Dump::visit (MacroRulesDefinition &rules_def) -{} +{ + for (auto &outer_attr : rules_def.get_outer_attrs ()) + emit_attrib (outer_attr); + + stream << "macro_rules! " << rules_def.get_rule_name () << " {\n"; + + indentation.increment (); + + for (auto &rule : rules_def.get_rules ()) + { + stream << indentation; + + rule.get_matcher ().accept_vis (*this); + + stream << " => "; + + rule.get_transcriber ().get_token_tree ().accept_vis (*this); + + stream << ";\n"; + } + + indentation.decrement (); + + stream << "}\n"; +} void Dump::visit (MacroInvocation ¯o_invoc) diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 2da2736c95b..9fe8ee95493 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -72,7 +72,9 @@ private: std::ostream &stream; Indent indentation; - // Format together common items of functions: Parameters, return type, block + /** + * Format together common items of functions: Parameters, return type, block + */ void format_function_common (std::unique_ptr &return_type, std::unique_ptr &block); @@ -97,13 +99,19 @@ private: std::ostream &emit_indented_string (const std::string &value, const std::string &comment = ""); - // Emit formatted string for generic parameters. + /** + * Emit formatted string for generic parameters + */ void emit_generic_params (std::vector> ¶ms); - // Format a single field of a tuple. + /** + * Format a single field of a tuple + */ void format_tuple_field (TupleField &field); - // Format a single field of a struct. + /** + * Format a single field of a struct + */ void format_struct_field (StructField &field); // rust-ast.h diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 4f980b0b506..798bc974fbb 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -407,6 +407,7 @@ public: Location get_locus () const { return locus; } DelimTokenTree &get_token_tree () { return token_tree; } + const DelimTokenTree &get_token_tree () const { return token_tree; } }; // A macro rule? Matcher and transcriber pair?