From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 4B497382B258; Wed, 8 Jun 2022 12:31:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4B497382B258 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Move cfg!() macro to builtins X-Act-Checkin: gcc X-Git-Author: antego X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: d36a3c5752cbffab5bc107bb3cf7710442a29f9e X-Git-Newrev: a9c0649503f19bb2399bb8d4fe2f2d45db65727d Message-Id: <20220608123157.4B497382B258@sourceware.org> Date: Wed, 8 Jun 2022 12:31:57 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:31:57 -0000 https://gcc.gnu.org/g:a9c0649503f19bb2399bb8d4fe2f2d45db65727d commit a9c0649503f19bb2399bb8d4fe2f2d45db65727d Author: antego Date: Wed Apr 13 20:00:54 2022 +1000 Move cfg!() macro to builtins Fixes #1039 Diff: --- gcc/rust/expand/rust-macro-builtins.cc | 36 ++++++++++++++++++++++++++++ gcc/rust/expand/rust-macro-builtins.h | 3 +++ gcc/rust/expand/rust-macro-expand.cc | 44 ---------------------------------- gcc/rust/expand/rust-macro-expand.h | 5 ---- gcc/rust/util/rust-hir-map.cc | 1 + 5 files changed, 40 insertions(+), 49 deletions(-) diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc index d0f73024ca9..8c68a7d41ca 100644 --- a/gcc/rust/expand/rust-macro-builtins.cc +++ b/gcc/rust/expand/rust-macro-builtins.cc @@ -376,4 +376,40 @@ MacroBuiltin::env (Location invoc_locus, AST::MacroInvocData &invoc) return AST::ASTFragment ({node}); } +AST::ASTFragment +MacroBuiltin::cfg (Location invoc_locus, AST::MacroInvocData &invoc) +{ + // only parse if not already parsed + if (!invoc.is_parsed ()) + { + std::unique_ptr converted_input ( + invoc.get_delim_tok_tree ().parse_to_meta_item ()); + + if (converted_input == nullptr) + { + rust_debug ("DEBUG: failed to parse macro to meta item"); + // TODO: do something now? is this an actual error? + } + else + { + std::vector> meta_items ( + std::move (converted_input->get_items ())); + invoc.set_meta_item_output (std::move (meta_items)); + } + } + + /* TODO: assuming that cfg! macros can only have one meta item inner, like cfg + * attributes */ + if (invoc.get_meta_items ().size () != 1) + return AST::ASTFragment::create_error (); + + bool result = invoc.get_meta_items ()[0]->check_cfg_predicate ( + Session::get_instance ()); + auto literal_exp = AST::SingleASTNode (std::unique_ptr ( + new AST::LiteralExpr (result ? "true" : "false", AST::Literal::BOOL, + PrimitiveCoreType::CORETYPE_BOOL, {}, invoc_locus))); + + return AST::ASTFragment ({literal_exp}); +} + } // namespace Rust diff --git a/gcc/rust/expand/rust-macro-builtins.h b/gcc/rust/expand/rust-macro-builtins.h index 047181146c1..e284c032e1d 100644 --- a/gcc/rust/expand/rust-macro-builtins.h +++ b/gcc/rust/expand/rust-macro-builtins.h @@ -92,6 +92,9 @@ public: static AST::ASTFragment env (Location invoc_locus, AST::MacroInvocData &invoc); + + static AST::ASTFragment cfg (Location invoc_locus, + AST::MacroInvocData &invoc); }; } // namespace Rust diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 20bbbc07ffb..6224b0c5e17 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -25,50 +25,6 @@ #include "rust-attribute-visitor.h" namespace Rust { -void -MacroExpander::parse_macro_to_meta_item (AST::MacroInvocData &invoc) -{ - // only parse if not already parsed - if (invoc.is_parsed ()) - return; - - std::unique_ptr converted_input ( - invoc.get_delim_tok_tree ().parse_to_meta_item ()); - - if (converted_input == nullptr) - { - rust_debug ("DEBUG: failed to parse macro to meta item"); - // TODO: do something now? is this an actual error? - } - else - { - std::vector> meta_items ( - std::move (converted_input->get_items ())); - invoc.set_meta_item_output (std::move (meta_items)); - } -} - -AST::Literal -MacroExpander::expand_cfg_macro (AST::MacroInvocData &invoc) -{ - // only allow on cfg macros - if (invoc.get_path () != "cfg") - return AST::Literal::create_error (); - - parse_macro_to_meta_item (invoc); - - /* TODO: assuming that cfg! macros can only have one meta item inner, like cfg - * attributes */ - if (invoc.get_meta_items ().size () != 1) - return AST::Literal::create_error (); - - bool result = invoc.get_meta_items ()[0]->check_cfg_predicate (session); - if (result) - return AST::Literal ("true", AST::Literal::BOOL, CORETYPE_BOOL); - else - return AST::Literal ("false", AST::Literal::BOOL, CORETYPE_BOOL); -} - AST::ASTFragment MacroExpander::expand_decl_macro (Location invoc_locus, AST::MacroInvocData &invoc, diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index f3ca7fc876a..3c53d8d099f 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -226,11 +226,6 @@ struct MacroExpander bool fails_cfg (const AST::AttrVec &attr) const; bool fails_cfg_with_expand (AST::AttrVec &attrs) const; - // Expand the data of a cfg! macro. - void parse_macro_to_meta_item (AST::MacroInvocData &invoc); - // Get the literal representation of a cfg! macro. - AST::Literal expand_cfg_macro (AST::MacroInvocData &invoc); - bool depth_exceeds_recursion_limit () const; bool try_match_rule (AST::MacroRule &match_rule, diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 23b78efcf07..e9ad87c5560 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -756,6 +756,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro) {"compile_error", MacroBuiltin::compile_error}, {"concat", MacroBuiltin::concat}, {"env", MacroBuiltin::env}, + {"cfg", MacroBuiltin::cfg}, }; auto builtin = builtin_macros.find (macro->get_rule_name ());