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

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

commit e753fa4e3633c6e6158ca26d930f0d1aaca15a6a
Author: liushuyu <liushuyu011@gmail.com>
Date:   Sat Apr 9 02:23:16 2022 -0600

    macros: add include! macro

Diff:
---
 gcc/rust/expand/rust-macro-builtins.cc | 54 ++++++++++++++++++++++++++++++++++
 gcc/rust/expand/rust-macro-builtins.h  |  3 ++
 gcc/rust/util/rust-hir-map.cc          |  1 +
 3 files changed, 58 insertions(+)

diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc
index 8c68a7d41ca..e4beb953b5f 100644
--- a/gcc/rust/expand/rust-macro-builtins.cc
+++ b/gcc/rust/expand/rust-macro-builtins.cc
@@ -412,4 +412,58 @@ MacroBuiltin::cfg (Location invoc_locus, AST::MacroInvocData &invoc)
   return AST::ASTFragment ({literal_exp});
 }
 
+/* Expand builtin macro include!(), which includes a source file at the current
+ scope compile time. */
+
+AST::ASTFragment
+MacroBuiltin::include (Location invoc_locus, AST::MacroInvocData &invoc)
+{
+  /* Get target filename from the macro invocation, which is treated as a path
+     relative to the include!-ing file (currently being compiled).  */
+  auto lit_expr
+    = parse_single_string_literal (invoc.get_delim_tok_tree (), invoc_locus);
+  if (lit_expr == nullptr)
+    return AST::ASTFragment::create_error ();
+
+  std::string target_filename
+    = source_relative_path (lit_expr->as_string (), invoc_locus);
+
+  RAIIFile target_file (target_filename.c_str ());
+  Linemap *linemap = Session::get_instance ().linemap;
+
+  if (target_file.get_raw () == nullptr)
+    {
+      rust_error_at (lit_expr->get_locus (), "cannot open included file %s: %m",
+		     target_filename.c_str ());
+      return AST::ASTFragment::create_error ();
+    }
+
+  rust_debug ("Attempting to parse included file %s", target_filename.c_str ());
+
+  Lexer lex (target_filename.c_str (), std::move (target_file), linemap);
+  Parser<Lexer> parser (std::move (lex));
+
+  auto parsed_items = parser.parse_items ();
+  bool has_error = !parser.get_errors ().empty ();
+
+  for (const auto &error : parser.get_errors ())
+    error.emit_error ();
+
+  if (has_error)
+    {
+      // inform the user that the errors above are from a included file
+      rust_inform (invoc_locus, "included from here");
+      return AST::ASTFragment::create_error ();
+    }
+
+  std::vector<AST::SingleASTNode> nodes{};
+  for (auto &item : parsed_items)
+    {
+      AST::SingleASTNode node (std::move (item));
+      nodes.push_back (node);
+    }
+
+  return AST::ASTFragment (nodes);
+}
+
 } // namespace Rust
diff --git a/gcc/rust/expand/rust-macro-builtins.h b/gcc/rust/expand/rust-macro-builtins.h
index e284c032e1d..f1d10df1ba2 100644
--- a/gcc/rust/expand/rust-macro-builtins.h
+++ b/gcc/rust/expand/rust-macro-builtins.h
@@ -95,6 +95,9 @@ public:
 
   static AST::ASTFragment cfg (Location invoc_locus,
 			       AST::MacroInvocData &invoc);
+
+  static AST::ASTFragment include (Location invoc_locus,
+				   AST::MacroInvocData &invoc);
 };
 } // namespace Rust
 
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index e9ad87c5560..b08258c269c 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -757,6 +757,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
       {"concat", MacroBuiltin::concat},
       {"env", MacroBuiltin::env},
       {"cfg", MacroBuiltin::cfg},
+      {"include", MacroBuiltin::include},
     };
 
   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:32 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:32 [gcc/devel/rust/master] macros: add include! macro 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).