From patchwork Wed Apr 5 14:03:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 79647 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp344154vqo; Wed, 5 Apr 2023 07:20:00 -0700 (PDT) X-Google-Smtp-Source: AKy350bLTAHkXgC0GcwZjaatHjKUxI8AyBud5u8L1C3kFftYKV9hG3bdPxBOZvAzPxZk0nDe4Vec X-Received: by 2002:a17:906:f51:b0:88a:1ea9:a5ea with SMTP id h17-20020a1709060f5100b0088a1ea9a5eamr3030317ejj.65.1680704400418; Wed, 05 Apr 2023 07:20:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680704400; cv=none; d=google.com; s=arc-20160816; b=iEO75q9Jhsg8dW5OImqXrIoDoy0iWOnfd0IIvUP905rfGh9s8EPK6QC0tgw/C5Q9KU DZLMGdWXe+dh6lU+ECKamiuTIRJp9Wsyz5ayE6wRq7gtbjCcxSvHUnm5aRjd5YPBPS5l nlUjRgGPM6SPKDo/AbLwRJXexWWviGwJ2hJbBv8KFjj+TTrDb8k9W0igud+/lB3Eu7YQ zj3Vkm6TajwYkyoaIlHpjfoMd4omSf1yjZLZvJrLii590++gekuJ21k7yuE5/qWeMpEm SaLeFbj26y8avOWT959143LZyDP7GcGvXjkkFmRauDnWOXEIqEBQr4/v2nlqAD6P4+Eo 7irw== 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=XASHR5fWYWYoKYBrrl1/4rFLy0iFDUR8UCc7yuPK6aM=; b=G/RoZjMR9g9ifaLsi8hUoY0KVTj5ME2ykk6czMASr/Xjbkd/+yiB/hQHN9970uLNcX xdM1WRxcD+su8pVdzbJFxb1fMQP9rLF/nuBTqgjF15xUbVQNEf6s8qRGOfwtP6i8qVDc c3GFRo2lg3WfaeIiapN/UQ3RJBlirMbLILURrWBPatbCQmvsLs6LCaHUawnCgpqrKtjO sdvYDarPoqkL1x8SCPV04Ywz2IeR4CNOw69mFr6FEFfGptjvFtxTow+QmJNZzB5HdXtU wtuh7Y/ieT/rZEbNw17R6BQ1NvX1Cx9aL434+zMjgQbnfKi24dmsxgYW353/S7xjRh7f BNUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=UvTn39J1; 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 lh19-20020a170906f8d300b009263490cbeasi4789290ejb.92.2023.04.05.07.20.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:20:00 -0700 (PDT) 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=UvTn39J1; 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 EE1413ACBCA6 for ; Wed, 5 Apr 2023 14:09:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by sourceware.org (Postfix) with ESMTPS id 7FE353857344 for ; Wed, 5 Apr 2023 14:05:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7FE353857344 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-wm1-x32a.google.com with SMTP id l10-20020a05600c1d0a00b003f04bd3691eso7160271wms.5 for ; Wed, 05 Apr 2023 07:05:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703544; 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=XASHR5fWYWYoKYBrrl1/4rFLy0iFDUR8UCc7yuPK6aM=; b=UvTn39J1+0LYyKjS6qgW/yb/KznNaD7RdB6HerdW/Ru+h5eoePmHA3xzVTef5+aE7b M0AY/p3BWE/DV+UCpqkROHn1DpLMG//vd8itx/It3ztLa/iH69j++dr7P2NFTWlYwATs G/7rQYYsu852LUzuIdQLJ0WSlubIq3osNnHS/Wkx6uu8ek3dQQAhrO39akfV8ofDTLlh A+Zh3tnyp78VK5yjehca7YFCQaIs5oq1RREHvQ7PfZSgTe8ImWppm/j5Af1GrctQh+vh wfuGyoYZBsx4rffKChR1C41/MZBc69HUqoUhkHMh5C6cc6V7oGdDJQ0/jKgPwxISHcRA YupA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703544; 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=XASHR5fWYWYoKYBrrl1/4rFLy0iFDUR8UCc7yuPK6aM=; b=BchcLdfziHRjJV0Z+yc//wpu2yYstrYQ8navWZ/r+mLMLqiGsEEDXQLBE5Sz03e/mW tDkeuXntmJPGJeDIga2rF7Bp64OQw2zquBcO0Nt4I0tAuR4Oyt7x3oWE99Eh8zUwT43S Id7/h1fCjKOU5jJ0TwTwjCh061Mzsicq6T5WBnZEBKa7y72CYcywU89K0blwGajvMgn2 6q8uhJfm963YCWpUYIONdjp02CCqAVzWynkeUHVvny9cvz0EzO0byyfz8G7xqRMwRdyo RI5Uezzv5Jbppqu1yZu56DVJgygcuGnWHuuZ4Une6e9ubruC3LKL3GjxrZNzrs1fa0+k Autg== X-Gm-Message-State: AAQBX9cjnvMlmy4NiTCcY3FF/qUdfATiy3T6rPkHN3tfqK75yqQKOhFp sQ7pBZ9fGct4Hn98xzH0j1cePjIV5MtOnbA74g== X-Received: by 2002:a7b:cc98:0:b0:3ed:8079:27d7 with SMTP id p24-20020a7bcc98000000b003ed807927d7mr4689779wma.40.1680703543977; Wed, 05 Apr 2023 07:05:43 -0700 (PDT) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id ay8-20020a05600c1e0800b003edddae1068sm2330150wmb.9.2023.04.05.07.05.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:05:43 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [committed 23/88] gccrs: macro: Allow builtin `MacroInvocation`s within the AST Date: Wed, 5 Apr 2023 16:03:07 +0200 Message-Id: <20230405140411.3016563-24-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230405140411.3016563-1-arthur.cohen@embecosm.com> References: <20230405140411.3016563-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, 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?1762346297455959923?= X-GMAIL-MSGID: =?utf-8?q?1762346297455959923?= From: Arthur Cohen gcc/rust/ChangeLog: * ast/rust-macro.h (enum class): Add `BuiltinMacro` enum class. * expand/rust-attribute-visitor.cc (AttrVisitor::visit): Mention switching on `macro.kind` once builtin macro invocations are properly handled. * parse/rust-parse-impl.h (Parser::parse_macro_invocation): Switch to new MacroInvocation API. (Parser::parse_type): Likewise. (Parser::parse_type_no_bounds): Likewise. --- gcc/rust/ast/rust-macro.h | 110 ++++++++++++++++++---- gcc/rust/expand/rust-attribute-visitor.cc | 2 + gcc/rust/parse/rust-parse-impl.h | 70 +++++++------- 3 files changed, 126 insertions(+), 56 deletions(-) diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 4226c6919ff..68dd7d660d3 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -578,8 +578,30 @@ protected: } }; +/** + * All builtin macros possible + */ +enum class BuiltinMacro +{ + Assert, + File, + Line, + Column, + IncludeBytes, + IncludeStr, + CompileError, + Concat, + Env, + Cfg, + Include +}; + /* AST node of a macro invocation, which is replaced by the macro result at - * compile time */ + * compile time. This is technically a sum-type/tagged-union, which represents + * both classic macro invocations and builtin macro invocations. Regular macro + * invocations are expanded lazily, but builtin macro invocations need to be + * expanded eagerly, hence the differentiation. + */ class MacroInvocation : public TypeNoBounds, public Pattern, public Item, @@ -589,26 +611,47 @@ class MacroInvocation : public TypeNoBounds, public ExternalItem, public ExprWithoutBlock { - std::vector outer_attrs; - MacroInvocData invoc_data; - Location locus; - - // Important for when we actually expand the macro - bool is_semi_coloned; - - NodeId node_id; - public: + enum class InvocKind + { + Regular, + Builtin, + }; + std::string as_string () const override; - MacroInvocation (MacroInvocData invoc_data, - std::vector outer_attrs, Location locus, - bool is_semi_coloned = false) - : outer_attrs (std::move (outer_attrs)), - invoc_data (std::move (invoc_data)), locus (locus), - is_semi_coloned (is_semi_coloned), - node_id (Analysis::Mappings::get ()->get_next_node_id ()) - {} + /** + * The default constructor you should use. Whenever we parse a macro call, we + * cannot possibly know whether or not this call refers to a builtin macro or + * a regular macro. With name resolution and scopes and nested macro calls, + * this is literally impossible. Hence, always start by creating a `Regular` + * MacroInvocation which will then (maybe!) become a `Builtin` macro + * invocation in the expander. + */ + static std::unique_ptr + Regular (MacroInvocData invoc_data, std::vector outer_attrs, + Location locus, bool is_semi_coloned = false) + { + return std::unique_ptr ( + new MacroInvocation (InvocKind::Regular, Optional::none (), + invoc_data, outer_attrs, locus, is_semi_coloned)); + } + + /** + * Create a builtin macro invocation. This can only be done after macro + * name-resolution and within the macro expander, so unless you're modifying + * these visitors, you probably do not want to use this function. + */ + static std::unique_ptr + Builtin (BuiltinMacro kind, MacroInvocData invoc_data, + std::vector outer_attrs, Location locus, + bool is_semi_coloned = false) + { + return std::unique_ptr ( + new MacroInvocation (InvocKind::Builtin, + Optional::some (kind), invoc_data, + outer_attrs, locus, is_semi_coloned)); + } Location get_locus () const override final { return locus; } @@ -642,6 +685,37 @@ public: bool has_semicolon () const { return is_semi_coloned; } + InvocKind get_kind () const { return kind; } + Optional get_builtin_kind () const { return builtin_kind; } + +private: + /* Full constructor */ + MacroInvocation (InvocKind kind, Optional builtin_kind, + MacroInvocData invoc_data, + std::vector outer_attrs, Location locus, + bool is_semi_coloned) + : outer_attrs (std::move (outer_attrs)), locus (locus), + node_id (Analysis::Mappings::get ()->get_next_node_id ()), + invoc_data (std::move (invoc_data)), is_semi_coloned (is_semi_coloned), + kind (kind), builtin_kind (builtin_kind) + {} + + std::vector outer_attrs; + Location locus; + NodeId node_id; + + /* The data given to the macro invocation */ + MacroInvocData invoc_data; + + /* Important for when we actually expand the macro */ + bool is_semi_coloned; + + /* Is this a builtin macro or a regular macro */ + InvocKind kind; + + /* If it is a builtin macro, which one */ + Optional builtin_kind = Optional::none (); + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/expand/rust-attribute-visitor.cc b/gcc/rust/expand/rust-attribute-visitor.cc index a1895749b82..6bceed62096 100644 --- a/gcc/rust/expand/rust-attribute-visitor.cc +++ b/gcc/rust/expand/rust-attribute-visitor.cc @@ -389,6 +389,8 @@ AttrVisitor::visit (AST::ConstGenericParam &) void AttrVisitor::visit (AST::MacroInvocation ¯o_invoc) { + // FIXME: Probably need to check macro_invoc.kind + // initial strip test based on outer attrs expander.expand_cfg_attrs (macro_invoc.get_outer_attrs ()); if (expander.fails_cfg_with_expand (macro_invoc.get_outer_attrs ())) diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index af749ca9200..a0bc2e10d2f 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -1752,10 +1752,9 @@ Parser::parse_macro_invocation_semi ( { // as this is the end, allow recovery (probably) - may change - return std::unique_ptr ( - new AST::MacroInvocation (std::move (invoc_data), - std::move (outer_attrs), macro_locus, - true)); + return AST::MacroInvocation::Regular (std::move (invoc_data), + std::move (outer_attrs), + macro_locus, true); } } @@ -1764,9 +1763,9 @@ Parser::parse_macro_invocation_semi ( t->get_token_description (), lexer.peek_token ()->get_token_description ()); - return std::unique_ptr ( - new AST::MacroInvocation (std::move (invoc_data), - std::move (outer_attrs), macro_locus, true)); + return AST::MacroInvocation::Regular (std::move (invoc_data), + std::move (outer_attrs), + macro_locus, true); } else { @@ -1814,10 +1813,9 @@ Parser::parse_macro_invocation (AST::AttrVec outer_attrs) Location macro_locus = macro_path.get_locus (); - return std::unique_ptr ( - new AST::MacroInvocation (AST::MacroInvocData (std::move (macro_path), - std::move (delim_tok_tree)), - std::move (outer_attrs), macro_locus)); + return AST::MacroInvocation::Regular ( + AST::MacroInvocData (std::move (macro_path), std::move (delim_tok_tree)), + std::move (outer_attrs), macro_locus); } // Parses a macro rule definition - does not parse semicolons. @@ -9308,11 +9306,10 @@ Parser::parse_type (bool save_errors) AST::DelimTokenTree tok_tree = parse_delim_token_tree (); - return std::unique_ptr ( - new AST::MacroInvocation ( - AST::MacroInvocData (std::move (macro_path), - std::move (tok_tree)), - {}, locus)); + return AST::MacroInvocation::Regular ( + AST::MacroInvocData (std::move (macro_path), + std::move (tok_tree)), + {}, locus); } case PLUS: { // type param bounds @@ -10146,11 +10143,10 @@ Parser::parse_type_no_bounds () AST::DelimTokenTree tok_tree = parse_delim_token_tree (); - return std::unique_ptr ( - new AST::MacroInvocation ( - AST::MacroInvocData (std::move (macro_path), - std::move (tok_tree)), - {}, locus)); + return AST::MacroInvocation::Regular ( + AST::MacroInvocData (std::move (macro_path), + std::move (tok_tree)), + {}, locus); } default: // assume that this is a type path and not an error @@ -12010,18 +12006,17 @@ Parser::parse_path_based_stmt_or_expr ( { lexer.skip_token (); - std::unique_ptr stmt ( - new AST::MacroInvocation (std::move (invoc_data), - std::move (outer_attrs), - stmt_or_expr_loc, true)); + auto stmt + = AST::MacroInvocation::Regular (std::move (invoc_data), + std::move (outer_attrs), + stmt_or_expr_loc, true); return ExprOrStmt (std::move (stmt)); } // otherwise, create macro invocation - std::unique_ptr expr ( - new AST::MacroInvocation (std::move (invoc_data), - std::move (outer_attrs), - stmt_or_expr_loc, false)); + auto expr = AST::MacroInvocation::Regular (std::move (invoc_data), + std::move (outer_attrs), + stmt_or_expr_loc, false); return ExprOrStmt (std::move (expr)); } else @@ -12330,17 +12325,16 @@ Parser::parse_macro_invocation_maybe_semi ( { lexer.skip_token (); - std::unique_ptr stmt ( - new AST::MacroInvocation (std::move (invoc_data), - std::move (outer_attrs), macro_locus, - true)); + auto stmt = AST::MacroInvocation::Regular (std::move (invoc_data), + std::move (outer_attrs), + macro_locus, true); return ExprOrStmt (std::move (stmt)); } // otherwise, create macro invocation - std::unique_ptr expr ( - new AST::MacroInvocation (std::move (invoc_data), - std::move (outer_attrs), macro_locus)); + auto expr + = AST::MacroInvocation::Regular (std::move (invoc_data), + std::move (outer_attrs), macro_locus); return ExprOrStmt (std::move (expr)); } else @@ -14552,9 +14546,9 @@ Parser::parse_macro_invocation_partial ( Location macro_locus = converted_path.get_locus (); - return std::unique_ptr (new AST::MacroInvocation ( + return AST::MacroInvocation::Regular ( AST::MacroInvocData (std::move (converted_path), std::move (tok_tree)), - std::move (outer_attrs), macro_locus, restrictions.expr_can_be_stmt)); + std::move (outer_attrs), macro_locus, restrictions.expr_can_be_stmt); } /* Parses a struct expr struct with a path in expression already parsed (but