public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Move cfg!() macro to builtins
@ 2022-06-08 12:31 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:31 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:a9c0649503f19bb2399bb8d4fe2f2d45db65727d

commit a9c0649503f19bb2399bb8d4fe2f2d45db65727d
Author: antego <antego@users.noreply.github.com>
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<AST::AttrInputMetaItemContainer> 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<std::unique_ptr<AST::MetaItemInner>> 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<AST::Expr> (
+    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<AST::AttrInputMetaItemContainer> 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<std::unique_ptr<AST::MetaItemInner>> 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 ());


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:31 [gcc/devel/rust/master] Move cfg!() macro to builtins Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).