aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorantego <antego@users.noreply.github.com>2022-04-13 20:00:54 +1000
committerantego <antego@users.noreply.github.com>2022-04-15 07:31:32 +1000
commita9c0649503f19bb2399bb8d4fe2f2d45db65727d (patch)
treedead9ef51792ed2b75af442b67d983c77da733bf /gcc
parentd36a3c5752cbffab5bc107bb3cf7710442a29f9e (diff)
downloadgcc-a9c0649503f19bb2399bb8d4fe2f2d45db65727d.zip
gcc-a9c0649503f19bb2399bb8d4fe2f2d45db65727d.tar.gz
gcc-a9c0649503f19bb2399bb8d4fe2f2d45db65727d.tar.bz2
Move cfg!() macro to builtins
Fixes #1039
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/expand/rust-macro-builtins.cc36
-rw-r--r--gcc/rust/expand/rust-macro-builtins.h3
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc44
-rw-r--r--gcc/rust/expand/rust-macro-expand.h5
-rw-r--r--gcc/rust/util/rust-hir-map.cc1
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 d0f7302..8c68a7d 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 0471811..e284c03 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 20bbbc0..6224b0c 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 f3ca7fc8..3c53d8d 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 23b78ef..e9ad87c 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 ());