From patchwork Tue Feb 21 12:01:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 59992 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1832320wrn; Tue, 21 Feb 2023 04:26:27 -0800 (PST) X-Google-Smtp-Source: AK7set/3d0OhW5W1L6XLUYL2296EuQ5ZOXz1RhLL7e1WMvLG0gtL7UG48hWW7TRNcIYX/eRX+uHu X-Received: by 2002:a17:906:bfc5:b0:879:bff:55c with SMTP id us5-20020a170906bfc500b008790bff055cmr14080542ejb.1.1676982387850; Tue, 21 Feb 2023 04:26:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676982387; cv=none; d=google.com; s=arc-20160816; b=CiGW7xcSRvC9EhzW6cSfEz7Nn27wvx6ABZQOTIFWBKXbEaagUsxtdm9E6dZvo38LtO nbpOeO77eek1JcNUF/Ejng85b4+THWq3WCZzwiBxCn80hfFR65VYTEjmGsDG3QYQhr/Z 16odhzkgnFIPGqmd0STum7+UbffILjuVZaLLVyCjgctZTLlVlZkVyEVin5AG6Z3CobcF WKtWnDnaDY1hUnbmhq/vwCijaeQNfwyYxFgYExRJGbfbWC3VTH5F7XxlCudARAXbN6pv ctOW8ZdwW2xRlCrpV4oTpJLhai3riuVi2B3UW69FYc3DRCtwa/JgwqEcu0ZiqtZ94+hb eN0g== 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=PReMTRp2tbf6gGkZGNgpfxe+TfV6I29DrgLnP3Mm9Po=; b=DZSpfxxUqhmfrLE0VikR/450YetZ13YVTotHEvPP1OC2vomEt0q1jDpmNyrK7rmreA VcdePT8QiKNx57VkQqloLevXN0m2FeebiLwQ7eEEiyYVkXjjBoEa/wdyvyOEpEN48eIO w5OeokFAqkmLGTRhsAajNfjm1sbmcyQ1c9MbRcxhYfGHKNQSJsNL3zryX4PIzXXW4NRs CMoSJztL+RBs0w9BmgVVwtY0Wxo+wBXLk+0Ke1HoeWcMcU0FhfmJsbM1Ngodn8Ztx2ey W1BlR4ffLAgEYscbc1j1TCL4QDMXumhdrIOIUwJwQv2ojXIGchPbXijCoGjHsXt4hf/v L3nA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=bn03QUuz; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d19-20020aa7d693000000b004ad05b994c3si19057731edr.544.2023.02.21.04.26.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:26:27 -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=bn03QUuz; 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 D2C58393A426 for ; Tue, 21 Feb 2023 12:10:29 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id D83513839DFC for ; Tue, 21 Feb 2023 12:03:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D83513839DFC 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-x434.google.com with SMTP id t13so4089371wrv.13 for ; Tue, 21 Feb 2023 04:03:58 -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=PReMTRp2tbf6gGkZGNgpfxe+TfV6I29DrgLnP3Mm9Po=; b=bn03QUuzzAV0p5+d/EWhcooEpcMUtF7sFCC0l5AdBav/NFPGNmr0NTXgPHqcTuB4Az FQkW37bK22O/2C+YHUOuMbRdJnyoOSotRhf5/prTVMk9z53iF9KS9wN6pHgZqkZK7JED KcHlPuHYc/ZhPVFnyIAsAPEp9aRiQTJo03K0I1JpjCMObSkTVjXKOw4qWqj2F2GpH4zT lxVhfqf0IDq5cSKnt/x11tjRTzvwRoc9UhiC2PBw0neELHfDm5kyJRWpWNMn842TUdE7 8XTp5E2rQoYESEIW3VYEMzcM0IoVIKOOAKJTmKCTCA0hYrb5bFSzYFQGtj31vwOwFIzV /XMw== 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=PReMTRp2tbf6gGkZGNgpfxe+TfV6I29DrgLnP3Mm9Po=; b=jpmTXiFO2T58w6k5huQ9ackHUzmwO73ghEzYjFb1L/xVEtrHBtwDXEtT/YqetvVEwW arTFkOwlkpT1qnv0mD64RYPwNOkQc9FSt1Q97PRLagrON1ZuDofzLr7cvTtbrXGLl6/x SaNPICDe4bcvNIlTkPipillRm9Ljrpk1MLPcTXiG9nrhkRFZEjLo7M+mT975pa7WBGXw yYUVluOBBgZiKfMnRsYlWvMFT9pALmMIwNF1cljy3qSuZG6pR5dc+Knhkum5p8FYfoIO Ulki7pp1Tfvd7TOuv6L2V7+uKu/rD56eeBg8CtGnDuAwzKl/E6JmO/J0TQTbK/FiFGLt dU5g== X-Gm-Message-State: AO0yUKVn1A28MigvlHLM8H3BM49Bzv+WEAW0YATOUGH110cbsWYmruOI TOe0G5H5j5CjdDu0l95wHhnUOXBzmmcL2SnRbQ== X-Received: by 2002:adf:e301:0:b0:2c5:46d1:69e1 with SMTP id b1-20020adfe301000000b002c546d169e1mr4955390wrj.36.1676981037272; Tue, 21 Feb 2023 04:03:57 -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.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:03:56 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [committed 042/103] gccrs: ast: Improve Fragment API Date: Tue, 21 Feb 2023 13:01:32 +0100 Message-Id: <20230221120230.596966-43-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?1758443484333993602?= X-GMAIL-MSGID: =?utf-8?q?1758443484333993602?= From: Arthur Cohen gcc/rust/ChangeLog: * ast/rust-ast-fragment.cc (Fragment::Fragment): Add better APIs. (Fragment::complete): New function. (Fragment::unexpanded): New function. * ast/rust-ast-fragment.h: Declare new APIs and add documentation. * expand/rust-attribute-visitor.h: Use new Fragment API. * expand/rust-macro-builtins.cc (MacroBuiltin::file): Likewise. (MacroBuiltin::column): Likewise. (MacroBuiltin::include_bytes): Likewise. (MacroBuiltin::include_str): Likewise. (MacroBuiltin::concat): Likewise. (MacroBuiltin::env): Likewise. (MacroBuiltin::cfg): Likewise. (MacroBuiltin::include): Likewise. (MacroBuiltin::line): Likewise. * expand/rust-macro-expand.cc (parse_many): Likewise. (transcribe_expression): Likewise. (transcribe_type): Likewise. * expand/rust-macro-expand.h (struct MacroExpander): Likewise. --- gcc/rust/ast/rust-ast-fragment.cc | 31 +++++++++++++----------- gcc/rust/ast/rust-ast-fragment.h | 18 ++++++++++++-- gcc/rust/expand/rust-attribute-visitor.h | 2 +- gcc/rust/expand/rust-macro-builtins.cc | 18 +++++++------- gcc/rust/expand/rust-macro-expand.cc | 6 ++--- gcc/rust/expand/rust-macro-expand.h | 2 +- 6 files changed, 47 insertions(+), 30 deletions(-) diff --git a/gcc/rust/ast/rust-ast-fragment.cc b/gcc/rust/ast/rust-ast-fragment.cc index 1a2dd99835a..c4916093fc6 100644 --- a/gcc/rust/ast/rust-ast-fragment.cc +++ b/gcc/rust/ast/rust-ast-fragment.cc @@ -21,22 +21,13 @@ namespace Rust { namespace AST { -Fragment::Fragment (std::vector nodes, bool fragment_is_error) - : kind (fragment_is_error ? FragmentKind::Error : FragmentKind::Complete), - nodes (std::move (nodes)) -{ - if (fragment_is_error) - rust_assert (nodes.empty ()); -} +Fragment::Fragment (FragmentKind kind, std::vector nodes) + : kind (kind), nodes (std::move (nodes)) +{} Fragment::Fragment (Fragment const &other) : kind (other.get_kind ()) { - nodes.clear (); - nodes.reserve (other.nodes.size ()); - for (auto &n : other.nodes) - { - nodes.push_back (n); - } + *this = other; } Fragment & @@ -56,7 +47,19 @@ Fragment::operator= (Fragment const &other) Fragment Fragment::create_error () { - return Fragment ({}, true); + return Fragment (FragmentKind::Error, {}); +} + +Fragment +Fragment::complete (std::vector nodes) +{ + return Fragment (FragmentKind::Complete, std::move (nodes)); +} + +Fragment +Fragment::unexpanded () +{ + return Fragment (FragmentKind::Unexpanded, {}); } std::vector & diff --git a/gcc/rust/ast/rust-ast-fragment.h b/gcc/rust/ast/rust-ast-fragment.h index ee6ab0d8433..3ef4ba16dce 100644 --- a/gcc/rust/ast/rust-ast-fragment.h +++ b/gcc/rust/ast/rust-ast-fragment.h @@ -57,11 +57,23 @@ enum class FragmentKind class Fragment { public: - Fragment (std::vector nodes, bool fragment_is_error = false); Fragment (Fragment const &other); + Fragment &operator= (Fragment const &other); + + /** + * Create an error fragment + */ static Fragment create_error (); - Fragment &operator= (Fragment const &other); + /** + * Create a complete AST fragment + */ + static Fragment complete (std::vector nodes); + + /** + * Create a fragment which contains unexpanded nodes + */ + static Fragment unexpanded (); FragmentKind get_kind () const; std::vector &get_nodes (); @@ -78,6 +90,8 @@ public: void accept_vis (ASTVisitor &vis); private: + Fragment (FragmentKind kind, std::vector nodes); + FragmentKind kind; /** diff --git a/gcc/rust/expand/rust-attribute-visitor.h b/gcc/rust/expand/rust-attribute-visitor.h index 6b562bd49b1..cbddc163da7 100644 --- a/gcc/rust/expand/rust-attribute-visitor.h +++ b/gcc/rust/expand/rust-attribute-visitor.h @@ -60,7 +60,7 @@ public: { auto fragment = expander.take_expanded_fragment (*this); unsigned int original_depth = expander.expansion_depth; - auto final_fragment = AST::Fragment ({}, true); + auto final_fragment = AST::Fragment::create_error (); while (fragment.should_expand ()) { diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc index 2a8a3f752f4..a2129e84a46 100644 --- a/gcc/rust/expand/rust-macro-builtins.cc +++ b/gcc/rust/expand/rust-macro-builtins.cc @@ -279,7 +279,7 @@ MacroBuiltin::file (Location invoc_locus, AST::MacroInvocData &invoc) = Session::get_instance ().linemap->location_file (invoc_locus); auto file_str = AST::SingleASTNode (make_string (invoc_locus, current_file)); - return AST::Fragment ({file_str}); + return AST::Fragment::complete ({file_str}); } AST::Fragment @@ -292,7 +292,7 @@ MacroBuiltin::column (Location invoc_locus, AST::MacroInvocData &invoc) new AST::LiteralExpr (std::to_string (current_column), AST::Literal::INT, PrimitiveCoreType::CORETYPE_U32, {}, invoc_locus))); - return AST::Fragment ({column_no}); + return AST::Fragment::complete ({column_no}); } /* Expand builtin macro include_bytes!("filename"), which includes the contents @@ -335,7 +335,7 @@ MacroBuiltin::include_bytes (Location invoc_locus, AST::MacroInvocData &invoc) new AST::BorrowExpr (std::move (array), false, false, {}, invoc_locus)); auto node = AST::SingleASTNode (std::move (borrow)); - return AST::Fragment ({node}); + return AST::Fragment::complete ({node}); } /* Expand builtin macro include_str!("filename"), which includes the contents @@ -362,7 +362,7 @@ MacroBuiltin::include_str (Location invoc_locus, AST::MacroInvocData &invoc) std::string str ((const char *) &bytes[0], bytes.size ()); auto node = AST::SingleASTNode (make_string (invoc_locus, str)); - return AST::Fragment ({node}); + return AST::Fragment::complete ({node}); } /* Expand builtin macro compile_error!("error"), which forces a compile error @@ -430,7 +430,7 @@ MacroBuiltin::concat (Location invoc_locus, AST::MacroInvocData &invoc) return AST::Fragment::create_error (); auto node = AST::SingleASTNode (make_string (invoc_locus, str)); - return AST::Fragment ({node}); + return AST::Fragment::complete ({node}); } /* Expand builtin macro env!(), which inspects an environment variable at @@ -491,7 +491,7 @@ MacroBuiltin::env (Location invoc_locus, AST::MacroInvocData &invoc) } auto node = AST::SingleASTNode (make_string (invoc_locus, env_value)); - return AST::Fragment ({node}); + return AST::Fragment::complete ({node}); } AST::Fragment @@ -527,7 +527,7 @@ MacroBuiltin::cfg (Location invoc_locus, AST::MacroInvocData &invoc) new AST::LiteralExpr (result ? "true" : "false", AST::Literal::BOOL, PrimitiveCoreType::CORETYPE_BOOL, {}, invoc_locus))); - return AST::Fragment ({literal_exp}); + return AST::Fragment::complete ({literal_exp}); } /* Expand builtin macro include!(), which includes a source file at the current @@ -584,7 +584,7 @@ MacroBuiltin::include (Location invoc_locus, AST::MacroInvocData &invoc) nodes.push_back (node); } - return AST::Fragment (nodes); + return AST::Fragment::complete (nodes); } AST::Fragment @@ -597,7 +597,7 @@ MacroBuiltin::line (Location invoc_locus, AST::MacroInvocData &invoc) new AST::LiteralExpr (std::to_string (current_line), AST::Literal::INT, PrimitiveCoreType::CORETYPE_U32, {}, invoc_locus))); - return AST::Fragment ({line_no}); + return AST::Fragment::complete ({line_no}); } } // namespace Rust diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 0684a28787f..9c3523e0515 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -729,7 +729,7 @@ parse_many (Parser &parser, TokenId &delimiter, nodes.emplace_back (std::move (node)); } - return AST::Fragment (std::move (nodes)); + return AST::Fragment::complete (std::move (nodes)); } /** @@ -840,7 +840,7 @@ transcribe_expression (Parser &parser) { auto expr = parser.parse_expr (); - return AST::Fragment ({std::move (expr)}); + return AST::Fragment::complete ({std::move (expr)}); } /** @@ -855,7 +855,7 @@ transcribe_type (Parser &parser) for (auto err : parser.get_errors ()) err.emit_error (); - return AST::Fragment ({std::move (type)}); + return AST::Fragment::complete ({std::move (type)}); } static AST::Fragment diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index b0c0c23a0c3..bf761c1b199 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -344,7 +344,7 @@ struct MacroExpander auto new_nodes = expanded_fragment.get_nodes (); std::move (new_nodes.begin (), new_nodes.end (), std::back_inserter (accumulator)); - expanded_fragment = AST::Fragment (accumulator); + expanded_fragment = AST::Fragment::complete (accumulator); } expansion_depth--; }