diff --git a/gcc/rust/ast/rust-ast-fragment.cc b/gcc/rust/ast/rust-ast-fragment.cc index 16bcbb3c24dd..80f10d54dc55 100644 --- a/gcc/rust/ast/rust-ast-fragment.cc +++ b/gcc/rust/ast/rust-ast-fragment.cc @@ -55,6 +55,12 @@ Fragment::create_error () return Fragment (FragmentKind::Error, {}, {}); } +Fragment +Fragment::create_empty () +{ + return Fragment (FragmentKind::Complete, {}, {}); +} + Fragment::Fragment (std::vector nodes, std::vector> tokens) : kind (FragmentKind::Complete), nodes (std::move (nodes)), diff --git a/gcc/rust/ast/rust-ast-fragment.h b/gcc/rust/ast/rust-ast-fragment.h index 41f5a287756c..e05d0bf455a0 100644 --- a/gcc/rust/ast/rust-ast-fragment.h +++ b/gcc/rust/ast/rust-ast-fragment.h @@ -60,6 +60,11 @@ class Fragment */ static Fragment create_error (); + /** + * Create an empty fragment + */ + static Fragment create_empty (); + /** * Create a complete AST fragment */ @@ -117,6 +122,13 @@ class Fragment bool is_single_fragment_of_kind (SingleASTNode::NodeType expected) const; void assert_single_fragment (SingleASTNode::NodeType expected) const; }; + +/** + * This is the type for transcriber functions found in + * rust-macro-builtins.{h,cc}. + */ + using transcriber_t = std::function (location_t, MacroInvocData &)>; + } // namespace AST } // namespace Rust diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index d63ea3309306..72fbdd25ebc7 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -468,7 +468,8 @@ class MacroRulesDefinition : public VisItem std::vector rules; // inlined form location_t locus; - std::function associated_transcriber; + transcriber_t associated_transcriber; + // Since we can't compare std::functions, we need to use an extra boolean bool is_builtin_rule; MacroKind kind; @@ -503,9 +504,8 @@ class MacroRulesDefinition : public VisItem {} MacroRulesDefinition (Identifier builtin_name, DelimType delim_type, - std::function - associated_transcriber, - MacroKind kind, Visibility vis) + transcriber_t associated_transcriber, MacroKind kind, + Visibility vis) : VisItem (std::move (vis), std::vector ()), outer_attrs (std::vector ()), rule_name (builtin_name), delim_type (delim_type), rules (std::vector ()), @@ -560,14 +560,12 @@ class MacroRulesDefinition : public VisItem const std::vector &get_rules () const { return rules; } bool is_builtin () const { return is_builtin_rule; } - const std::function & - get_builtin_transcriber () const + const transcriber_t &get_builtin_transcriber () const { rust_assert (is_builtin ()); return associated_transcriber; } - void set_builtin_transcriber ( - std::function transcriber) + void set_builtin_transcriber (transcriber_t transcriber) { associated_transcriber = transcriber; is_builtin_rule = true; diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc index 2d32904eb923..1079ea9978d1 100644 --- a/gcc/rust/expand/rust-macro-builtins.cc +++ b/gcc/rust/expand/rust-macro-builtins.cc @@ -74,8 +74,7 @@ const BiMap MacroBuiltin::builtins = {{ }}; -std::unordered_map< - std::string, std::function> +std::unordered_map MacroBuiltin::builtin_transcribers = { {"assert", MacroBuiltin::assert_handler}, {"file", MacroBuiltin::file_handler}, @@ -398,7 +397,7 @@ load_file_bytes (location_t invoc_locus, const char *filename) } } // namespace -AST::Fragment +tl::optional MacroBuiltin::assert_handler (location_t invoc_locus, AST::MacroInvocData &invoc) { @@ -407,7 +406,7 @@ MacroBuiltin::assert_handler (location_t invoc_locus, return AST::Fragment::create_error (); } -AST::Fragment +tl::optional MacroBuiltin::file_handler (location_t invoc_locus, AST::MacroInvocData &) { auto current_file = LOCATION_FILE (invoc_locus); @@ -418,7 +417,7 @@ MacroBuiltin::file_handler (location_t invoc_locus, AST::MacroInvocData &) return AST::Fragment ({file_str}, std::move (str_token)); } -AST::Fragment +tl::optional MacroBuiltin::column_handler (location_t invoc_locus, AST::MacroInvocData &) { auto current_column = LOCATION_COLUMN (invoc_locus); @@ -436,7 +435,7 @@ MacroBuiltin::column_handler (location_t invoc_locus, AST::MacroInvocData &) of the given file as reference to a byte array. Yields an expression of type &'static [u8; N]. */ -AST::Fragment +tl::optional MacroBuiltin::include_bytes_handler (location_t invoc_locus, AST::MacroInvocData &invoc) { @@ -496,7 +495,7 @@ MacroBuiltin::include_bytes_handler (location_t invoc_locus, of the given file as a string. The file must be UTF-8 encoded. Yields an expression of type &'static str. */ -AST::Fragment +tl::optional MacroBuiltin::include_str_handler (location_t invoc_locus, AST::MacroInvocData &invoc) { @@ -581,7 +580,7 @@ MacroBuiltin::include_str_handler (location_t invoc_locus, /* Expand builtin macro compile_error!("error"), which forces a compile error during the compile time. */ -AST::Fragment +tl::optional MacroBuiltin::compile_error_handler (location_t invoc_locus, AST::MacroInvocData &invoc) { @@ -642,7 +641,7 @@ MacroBuiltin::compile_error_handler (location_t invoc_locus, // invocation? // Do we split the two passes of parsing the token tree and then expanding it? // Can we do that easily? -AST::Fragment +tl::optional MacroBuiltin::concat_handler (location_t invoc_locus, AST::MacroInvocData &invoc) { @@ -707,7 +706,7 @@ MacroBuiltin::concat_handler (location_t invoc_locus, /* Expand builtin macro env!(), which inspects an environment variable at compile time. */ -AST::Fragment +tl::optional MacroBuiltin::env_handler (location_t invoc_locus, AST::MacroInvocData &invoc) { auto invoc_token_tree = invoc.get_delim_tok_tree (); @@ -781,7 +780,7 @@ MacroBuiltin::env_handler (location_t invoc_locus, AST::MacroInvocData &invoc) return AST::Fragment ({node}, std::move (tok)); } -AST::Fragment +tl::optional MacroBuiltin::cfg_handler (location_t invoc_locus, AST::MacroInvocData &invoc) { // only parse if not already parsed @@ -823,7 +822,7 @@ MacroBuiltin::cfg_handler (location_t invoc_locus, AST::MacroInvocData &invoc) /* Expand builtin macro include!(), which includes a source file at the current scope compile time. */ -AST::Fragment +tl::optional MacroBuiltin::include_handler (location_t invoc_locus, AST::MacroInvocData &invoc) { @@ -893,7 +892,7 @@ MacroBuiltin::include_handler (location_t invoc_locus, return AST::Fragment (nodes, std::vector> ()); } -AST::Fragment +tl::optional MacroBuiltin::line_handler (location_t invoc_locus, AST::MacroInvocData &) { auto current_line = LOCATION_LINE (invoc_locus); @@ -908,7 +907,7 @@ MacroBuiltin::line_handler (location_t invoc_locus, AST::MacroInvocData &) return AST::Fragment ({line_no}, std::move (tok)); } -AST::Fragment +tl::optional MacroBuiltin::stringify_handler (location_t invoc_locus, AST::MacroInvocData &invoc) { @@ -938,7 +937,7 @@ MacroBuiltin::stringify_handler (location_t invoc_locus, return AST::Fragment ({node}, std::move (token)); } -AST::Fragment +tl::optional MacroBuiltin::sorry (location_t invoc_locus, AST::MacroInvocData &invoc) { rust_sorry_at (invoc_locus, "unimplemented builtin macro: %qs", @@ -947,7 +946,7 @@ MacroBuiltin::sorry (location_t invoc_locus, AST::MacroInvocData &invoc) return AST::Fragment::create_error (); } -AST::Fragment +tl::optional MacroBuiltin::proc_macro_builtin (location_t invoc_locus, AST::MacroInvocData &invoc) { diff --git a/gcc/rust/expand/rust-macro-builtins.h b/gcc/rust/expand/rust-macro-builtins.h index fee9dc0cc07a..1700fea56920 100644 --- a/gcc/rust/expand/rust-macro-builtins.h +++ b/gcc/rust/expand/rust-macro-builtins.h @@ -118,52 +118,51 @@ class MacroBuiltin { public: static const BiMap builtins; - static std::unordered_map> + static std::unordered_map builtin_transcribers; - static AST::Fragment assert_handler (location_t invoc_locus, + static tl::optional assert_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment file_handler (location_t invoc_locus, + static tl::optional file_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment column_handler (location_t invoc_locus, + static tl::optional column_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment include_bytes_handler (location_t invoc_locus, + static tl::optional include_bytes_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment include_str_handler (location_t invoc_locus, + static tl::optional include_str_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment stringify_handler (location_t invoc_locus, + static tl::optional stringify_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment compile_error_handler (location_t invoc_locus, + static tl::optional compile_error_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment concat_handler (location_t invoc_locus, + static tl::optional concat_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment env_handler (location_t invoc_locus, + static tl::optional env_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment cfg_handler (location_t invoc_locus, + static tl::optional cfg_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment include_handler (location_t invoc_locus, + static tl::optional include_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment line_handler (location_t invoc_locus, + static tl::optional line_handler (location_t invoc_locus, AST::MacroInvocData &invoc); - static AST::Fragment sorry (location_t invoc_locus, + static tl::optional sorry (location_t invoc_locus, AST::MacroInvocData &invoc); /* Builtin procedural macros do not work directly on tokens, but still need a * builtin transcriber to be considered proper builtin macros */ - static AST::Fragment proc_macro_builtin (location_t, AST::MacroInvocData &); + static tl::optional proc_macro_builtin (location_t, AST::MacroInvocData &); }; } // namespace Rust diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index eb505f158708..d1be9529262d 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -286,7 +286,9 @@ MacroExpander::expand_invoc (AST::MacroInvocation &invoc, bool has_semicolon) if (rules_def->is_builtin ()) fragment - = rules_def->get_builtin_transcriber () (invoc.get_locus (), invoc_data); + = rules_def->get_builtin_transcriber () (invoc.get_locus (), + invoc_data) + .value_or(AST::Fragment::create_empty()); else fragment = expand_decl_macro (invoc.get_locus (), invoc_data, *rules_def, has_semicolon);