]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
hir: Add ExportedMacro node and handling.
authorArthur Cohen <arthur.cohen@embecosm.com>
Wed, 1 Mar 2023 11:35:18 +0000 (12:35 +0100)
committerCohenArthur <arthur.cohen@embecosm.com>
Tue, 14 Mar 2023 16:15:50 +0000 (16:15 +0000)
This HIR node represents macros which should be exported into the final
Rust metadata files. Because our metadata exporter operates on the HIR,
while macros are inherently tied to the AST, we need a way to propagate
exported macros up until the metadata export pass on the HIR. Hence the
existence of this class, whose sole purpose is to keep enough information
for the metadata exporter to retrieve the original AST::MacroRulesDefinition.
Handling for actually exporting these macros will come later.

gcc/rust/ChangeLog:

* hir/tree/rust-hir-item.h (class ExportedMacro): Add new ExportedMacro class.
* hir/tree/rust-hir.cc (ExportedMacro::accept_vis): New function.
(ExportedMacro::get_locus): Likewise.
(ExportedMacro::get_item_kind): Likewise.
(ExportedMacro::clone_item_impl): Likewise.
* hir/tree/rust-hir-full-decls.h (class ExportedMacro): Forward declare class.
* backend/rust-compile-item.h: Add visitor for ExportedMacro.
* backend/rust-compile-stmt.h: Likewise.
* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit): Likewise.
* checks/errors/privacy/rust-privacy-reporter.h: Likewise.
* checks/errors/privacy/rust-pub-restricted-visitor.cc (PubRestrictedVisitor::visit):
Likewise.
* checks/errors/privacy/rust-pub-restricted-visitor.h: Likewise.
* checks/errors/privacy/rust-reachability.cc (ReachabilityVisitor::visit): Likewise.
* checks/errors/privacy/rust-reachability.h: Likewise.
* checks/errors/privacy/rust-visibility-resolver.cc (VisibilityResolver::visit): Likewise.
* checks/errors/privacy/rust-visibility-resolver.h: Likewise.
* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
* checks/errors/rust-const-checker.h: Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
* checks/errors/rust-unsafe-checker.h: Likewise.
* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Likewise.
* hir/rust-ast-lower-item.h: Likewise.
* hir/rust-hir-dump.cc (Dump::visit): Likewise.
* hir/rust-hir-dump.h: Likewise.
* hir/tree/rust-hir-visitor.h: Likewise.
* metadata/rust-export-metadata.cc: Likewise.
* typecheck/rust-hir-type-check-item.h: Likewise.
* typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::visit): Likewise.
* typecheck/rust-hir-type-check-stmt.h: Likewise.
* typecheck/rust-tycheck-dump.h: Likewise.
* hir/tree/rust-hir.h: Add new ItemKind::MacroExport variant.
* util/rust-attributes.cc: Add #[macro_export] attribute.

29 files changed:
gcc/rust/backend/rust-compile-item.h
gcc/rust/backend/rust-compile-stmt.h
gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
gcc/rust/checks/errors/privacy/rust-privacy-reporter.h
gcc/rust/checks/errors/privacy/rust-pub-restricted-visitor.cc
gcc/rust/checks/errors/privacy/rust-pub-restricted-visitor.h
gcc/rust/checks/errors/privacy/rust-reachability.cc
gcc/rust/checks/errors/privacy/rust-reachability.h
gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc
gcc/rust/checks/errors/privacy/rust-visibility-resolver.h
gcc/rust/checks/errors/rust-const-checker.cc
gcc/rust/checks/errors/rust-const-checker.h
gcc/rust/checks/errors/rust-unsafe-checker.cc
gcc/rust/checks/errors/rust-unsafe-checker.h
gcc/rust/hir/rust-ast-lower-item.cc
gcc/rust/hir/rust-ast-lower-item.h
gcc/rust/hir/rust-hir-dump.cc
gcc/rust/hir/rust-hir-dump.h
gcc/rust/hir/tree/rust-hir-full-decls.h
gcc/rust/hir/tree/rust-hir-item.h
gcc/rust/hir/tree/rust-hir-visitor.h
gcc/rust/hir/tree/rust-hir.cc
gcc/rust/hir/tree/rust-hir.h
gcc/rust/metadata/rust-export-metadata.cc
gcc/rust/typecheck/rust-hir-type-check-item.h
gcc/rust/typecheck/rust-hir-type-check-stmt.cc
gcc/rust/typecheck/rust-hir-type-check-stmt.h
gcc/rust/typecheck/rust-tycheck-dump.h
gcc/rust/util/rust-attributes.cc

index ae3fdf6647e24c0882e72728a5d8e41db1df58f9..333e55490cedd086c693a6f79a4aa437cc76c47d 100644 (file)
@@ -70,6 +70,7 @@ public:
   void visit (HIR::LetStmt &) override {}
   void visit (HIR::ExprStmtWithoutBlock &) override {}
   void visit (HIR::ExprStmtWithBlock &) override {}
+  void visit (HIR::ExportedMacro &) override {}
 
 protected:
   CompileItem (Context *ctx, TyTy::BaseType *concrete, Location ref_locus)
index 1f06d54f4980b0ab7216678a24e0cfa365ffe16d..3d2f3ddb7460ab16b3d38ee5ec38a175c898128e 100644 (file)
@@ -56,6 +56,7 @@ public:
   void visit (HIR::ImplBlock &) override {}
   void visit (HIR::ExternBlock &) override {}
   void visit (HIR::EmptyStmt &) override {}
+  void visit (HIR::ExportedMacro &) override {}
 
 private:
   CompileStmt (Context *ctx);
index 7417f31b510054f4cd8ce98fb5cc0cecbdb2d3cc..a435d6dcade1802c1c775ae05a6d50608c65711c 100644 (file)
@@ -761,5 +761,9 @@ PrivacyReporter::visit (HIR::ExprStmtWithBlock &stmt)
   stmt.get_expr ()->accept_vis (*this);
 }
 
+void
+PrivacyReporter::visit (HIR::ExportedMacro &)
+{}
+
 } // namespace Privacy
 } // namespace Rust
index c9307787e9e77784d4d1ac89848cbc8cd46fd93e..695a7e88df0315d42fdad37a000c18a7b95b1b3d 100644 (file)
@@ -157,6 +157,7 @@ types
   virtual void visit (HIR::LetStmt &stmt);
   virtual void visit (HIR::ExprStmtWithoutBlock &stmt);
   virtual void visit (HIR::ExprStmtWithBlock &stmt);
+  virtual void visit (HIR::ExportedMacro &macro);
 
   Analysis::Mappings &mappings;
   Rust::Resolver::Resolver &resolver;
index 851fc7cd6ac1b168bad0be26364c663d60bbaf60..4dbd44ebb92d7d605acb8979cd3fa688d4dccad3 100644 (file)
@@ -178,5 +178,12 @@ PubRestrictedVisitor::visit (HIR::ExternBlock &block)
                        block.get_locus ());
 }
 
+void
+PubRestrictedVisitor::visit (HIR::ExportedMacro &macro)
+{
+  is_restriction_valid (macro.get_mappings ().get_nodeid (),
+                       macro.get_locus ());
+}
+
 } // namespace Privacy
 } // namespace Rust
index c77c6d86903a4fe5e786ce6927a985b1a6cfb112..d8419e2921b7480d18983674cfc13d94f4225035 100644 (file)
@@ -103,6 +103,7 @@ public:
   virtual void visit (HIR::Trait &trait);
   virtual void visit (HIR::ImplBlock &impl);
   virtual void visit (HIR::ExternBlock &block);
+  virtual void visit (HIR::ExportedMacro &macro);
 
 private:
   /* Stack of ancestor modules visited by this visitor */
index b9a2b363708d3b2922f427b8cc9d2739eaf8765e..d75e2a1f49a5f323f48d9c5372029622e638cbb8 100644 (file)
@@ -230,6 +230,10 @@ void
 ReachabilityVisitor::visit (HIR::ExternBlock &)
 {}
 
+void
+ReachabilityVisitor::visit (HIR::ExportedMacro &macro)
+{}
+
 // FIXME: How can we visit Blocks in the current configuration? Have a full
 // visitor?
 } // namespace Privacy
index bfc44eb8af9b6290c50abe69f72a30892bf29d54..80fb1d4899c42105e17ddc786eacb6fc03810ab2 100644 (file)
@@ -75,6 +75,7 @@ public:
   virtual void visit (HIR::Trait &trait);
   virtual void visit (HIR::ImplBlock &impl);
   virtual void visit (HIR::ExternBlock &block);
+  virtual void visit (HIR::ExportedMacro &macro);
 
 private:
   ReachLevel current_level;
index 30afaf9174e7289adcdc2dfa2f43825a3fe406ae..72a3a5f88290a1231306306e122edac9f3cd5449 100644 (file)
@@ -241,5 +241,11 @@ void
 VisibilityResolver::visit (HIR::ExternBlock &)
 {}
 
+void
+VisibilityResolver::visit (HIR::ExportedMacro &macro)
+{
+  resolve_and_update (&macro);
+}
+
 } // namespace Privacy
 } // namespace Rust
index 1c453d1b11cfdefcf80b138693ef2ccc2c68d04c..9ea8ebd5c4f6a789c3a9be92df261001189e5f1f 100644 (file)
@@ -90,6 +90,7 @@ public:
   virtual void visit (HIR::Trait &trait);
   virtual void visit (HIR::ImplBlock &impl);
   virtual void visit (HIR::ExternBlock &block);
+  virtual void visit (HIR::ExportedMacro &macro);
 
 private:
   Analysis::Mappings &mappings;
index d0ca5cf1c8a0a37413df0edb0f1faba25852f5f5..12bb62a5c4c26659be33876ec556542100e8572e 100644 (file)
@@ -927,5 +927,9 @@ void
 ConstChecker::visit (BareFunctionType &)
 {}
 
+void
+ConstChecker::visit (ExportedMacro &)
+{}
+
 } // namespace HIR
 } // namespace Rust
index 2183992b7d374b5b9731cf6290594a26d0e270b8..69628411d4cdc063b7a5e6c0f08351915f2e8e3e 100644 (file)
@@ -205,6 +205,7 @@ private:
   virtual void visit (SliceType &type) override;
   virtual void visit (InferredType &type) override;
   virtual void visit (BareFunctionType &type) override;
+  virtual void visit (ExportedMacro &macro) override;
 };
 
 } // namespace HIR
index be85c90c7ad05680ae1d9313f74cb867d86132b0..4374a0e2c1f857996667f5effafa2ca59f475d4d 100644 (file)
@@ -984,5 +984,9 @@ void
 UnsafeChecker::visit (BareFunctionType &)
 {}
 
+void
+UnsafeChecker::visit (ExportedMacro &)
+{}
+
 } // namespace HIR
 } // namespace Rust
index 2e6719a5d6e5fcdabbdbd95c7eec2422b8373c24..8f3a87f339594dc3201c82d0fbe85f8ca27040cc 100644 (file)
@@ -187,6 +187,7 @@ private:
   virtual void visit (SliceType &type) override;
   virtual void visit (InferredType &type) override;
   virtual void visit (BareFunctionType &type) override;
+  virtual void visit (ExportedMacro &macro) override;
 };
 
 } // namespace HIR
index 411cc4be855532817c9b7cf115fa466dab02f052..8c265e7f31c259080a001dbf2216ca775dd251b5 100644 (file)
@@ -691,6 +691,28 @@ ASTLoweringItem::visit (AST::ExternBlock &extern_block)
   translated = lower_extern_block (extern_block);
 }
 
+void
+ASTLoweringItem::visit (AST::MacroRulesDefinition &def)
+{
+  bool is_export = false;
+  for (const auto &attr : def.get_outer_attrs ())
+    if (attr.get_path ().as_string () == "macro_export")
+      is_export = true;
+
+  if (is_export)
+    {
+      auto crate_num = mappings->get_current_crate ();
+      Analysis::NodeMapping mapping (crate_num, def.get_node_id (),
+                                    mappings->get_next_hir_id (crate_num),
+                                    mappings->get_next_localdef_id (
+                                      crate_num));
+      auto locus = def.get_locus ();
+
+      translated
+       = new HIR::ExportedMacro (mapping, def.get_outer_attrs (), locus);
+    }
+}
+
 HIR::SimplePath
 ASTLoweringSimplePath::translate (const AST::SimplePath &path)
 {
index 07623f38e8411b6084c3cac59d19337d30ac8014..f059c0568a7e927bc785188d3e68fe9d8b82f10a 100644 (file)
@@ -56,6 +56,7 @@ public:
   void visit (AST::Trait &trait) override;
   void visit (AST::TraitImpl &impl_block) override;
   void visit (AST::ExternBlock &extern_block) override;
+  void visit (AST::MacroRulesDefinition &macro) override;
 
 private:
   ASTLoweringItem () : translated (nullptr) {}
index cfa6a3a46dadc495bd8e828e4bc4af0c30948432..a7aa16a3b0d540f45eaef29cd603407d0a970a56 100644 (file)
@@ -657,5 +657,8 @@ Dump::visit (InferredType &)
 void
 Dump::visit (BareFunctionType &)
 {}
+void
+Dump::visit (ExportedMacro &)
+{}
 } // namespace HIR
 } // namespace Rust
index 3f42d0481351a61522991d4a1f48e017888761ad..145a6a1ee72086a5e0eca8cb4f9eb9ca3375662d 100644 (file)
@@ -184,6 +184,7 @@ private:
   virtual void visit (SliceType &) override;
   virtual void visit (InferredType &) override;
   virtual void visit (BareFunctionType &) override;
+  virtual void visit (ExportedMacro &) override;
 };
 
 } // namespace HIR
index d486d91818639893434be91c96254c5102f930aa..e49888c67c392a3e44e03d31f2229e8c9fbd7dcc 100644 (file)
@@ -35,6 +35,9 @@ class Lifetime;
 class GenericParam;
 class LifetimeParam;
 
+// FIXME: ARTHUR: Move this somewhere else
+class ExportedMacro;
+
 class TraitItem;
 class ImplItem;
 struct Crate;
index 03e1f71abb53777ce75ae1a8d421758fd06f961c..7a2a39fe771e5c6e06b76afe04cb07e8f1a6cc12 100644 (file)
@@ -3224,6 +3224,27 @@ protected:
   }*/
 };
 
+class ExportedMacro : public VisItem
+{
+  Location locus;
+
+public:
+  ExportedMacro (Analysis::NodeMapping mapping, AST::AttrVec outer_attrs,
+                Location locus)
+    : VisItem (mapping, Visibility (Visibility::PUBLIC),
+              std::move (outer_attrs)),
+      locus (locus)
+  {}
+
+  virtual Location get_locus () const override;
+  virtual ItemKind get_item_kind () const override;
+  virtual ExportedMacro *clone_item_impl () const override;
+
+  void accept_vis (HIRFullVisitor &vis) override;
+  void accept_vis (HIRStmtVisitor &vis) override;
+  void accept_vis (HIRVisItemVisitor &vis) override;
+};
+
 } // namespace HIR
 } // namespace Rust
 
index fa6f5923f2fbf184d55b9d3343cd9f26cc2b5008..037aa01ecab64cdedad3e7eb4de71b04a5d63e84 100644 (file)
@@ -157,6 +157,7 @@ public:
   virtual void visit (SliceType &type) = 0;
   virtual void visit (InferredType &type) = 0;
   virtual void visit (BareFunctionType &type) = 0;
+  virtual void visit (ExportedMacro &macro) = 0;
 };
 
 class HIRFullVisitorBase : public HIRFullVisitor
@@ -311,6 +312,7 @@ public:
   virtual void visit (SliceType &) override {}
   virtual void visit (InferredType &) override {}
   virtual void visit (BareFunctionType &) override {}
+  virtual void visit (ExportedMacro &) override {}
 };
 
 class HIRExternalItemVisitor
@@ -345,6 +347,7 @@ public:
   virtual void visit (Trait &trait) = 0;
   virtual void visit (ImplBlock &impl) = 0;
   virtual void visit (ExternBlock &block) = 0;
+  virtual void visit (ExportedMacro &macro) = 0;
 };
 
 class HIRImplVisitor
@@ -404,6 +407,7 @@ public:
   virtual void visit (LetStmt &stmt) = 0;
   virtual void visit (ExprStmtWithoutBlock &stmt) = 0;
   virtual void visit (ExprStmtWithBlock &stmt) = 0;
+  virtual void visit (ExportedMacro &macro) = 0;
 };
 
 class HIRExpressionVisitor
index 3a4362f92b4769e3721ebbe368e367ef2191b8ee..4aa9d7e822765c42ef2904357d4aae0ca5a9a466 100644 (file)
@@ -5243,5 +5243,41 @@ void
 ConstGenericParam::accept_vis (HIRFullVisitor &)
 {}
 
+void
+ExportedMacro::accept_vis (HIRVisItemVisitor &vis)
+{
+  vis.visit (*this);
+}
+
+void
+ExportedMacro::accept_vis (HIRFullVisitor &vis)
+{
+  vis.visit (*this);
+}
+
+void
+ExportedMacro::accept_vis (HIRStmtVisitor &vis)
+{
+  vis.visit (*this);
+}
+
+Location
+ExportedMacro::get_locus () const
+{
+  return locus;
+}
+
+Item::ItemKind
+ExportedMacro::get_item_kind () const
+{
+  return ItemKind::MacroExport;
+}
+
+ExportedMacro *
+ExportedMacro::clone_item_impl () const
+{
+  return new ExportedMacro (*this);
+}
+
 } // namespace HIR
 } // namespace Rust
index 6ed74428356465cb24511c2c42a3ad685b0b27b7..6d5b755029fb5d87a8edb0abf9c3d6547e168dfe 100644 (file)
@@ -191,6 +191,7 @@ public:
     Trait,
     Impl,
     Module,
+    MacroExport,
   };
 
   virtual ItemKind get_item_kind () const = 0;
index 49a7226476d752ce22fea74fde05af4987131570..dc6e559ce1eea3b4e09cb4343f449804b28b4f3a 100644 (file)
@@ -169,6 +169,7 @@ public:
   void visit (HIR::StaticItem &) override {}
   void visit (HIR::ImplBlock &) override {}
   void visit (HIR::ExternBlock &) override {}
+  void visit (HIR::ExportedMacro &) override {}
 
   void visit (HIR::Trait &trait) override { ctx.emit_trait (trait); }
 
index 2268fdb6e6ccdc73b659ba98f2826811083398bd..437a3d377e4235d1a53317742a12d712b4ae1097 100644 (file)
@@ -50,6 +50,7 @@ public:
   // nothing to do
   void visit (HIR::ExternCrate &) override {}
   void visit (HIR::UseDeclaration &) override {}
+  void visit (HIR::ExportedMacro &) override {}
 
 protected:
   std::vector<TyTy::SubstitutionParamMapping>
index 956249a76070a05a86424fa60b5b92929f55cf67..c0a7f09486a3973ac71b1ef7600aecb1a299ae93 100644 (file)
@@ -148,6 +148,10 @@ TypeCheckStmt::visit (HIR::QualifiedPathInType &path)
   infered = TypeCheckType::Resolve (&path);
 }
 
+void
+TypeCheckStmt::visit (HIR::ExportedMacro &path)
+{}
+
 void
 TypeCheckStmt::visit (HIR::TupleStruct &struct_decl)
 {
index 04878b8c5314c0fff7b748a603603991d06b3505..7331b2a92fc70b8238759039418a041ffed0b491 100644 (file)
@@ -47,6 +47,7 @@ public:
   void visit (HIR::ImplBlock &impl) override;
   void visit (HIR::TypePath &path) override;
   void visit (HIR::QualifiedPathInType &path) override;
+  void visit (HIR::ExportedMacro &path) override;
 
   // FIXME
   // this seems like it should not be part of this visitor
index 0076fe9e3b7644099cdccb51aa71810acb9b074c..19a52dd0022ae817dfb0e54c8e8a4bd549cc507d 100644 (file)
@@ -196,6 +196,8 @@ public:
     dump += "ctor: " + type_string (expr.get_mappings ());
   }
 
+  void visit (HIR::ExportedMacro &) override {}
+
 protected:
   std::string type_string (const Analysis::NodeMapping &mappings)
   {
index 6df00ad95beea06734a25d06e2f6e04164a482b2..2573ec9b10f36bb00e4804743d754ee4ab4b9d5a 100644 (file)
@@ -41,6 +41,7 @@ static const BuiltinAttrDefinition __definitions[]
      {"repr", CODE_GENERATION},
      {"path", EXPANSION},
      {"macro_use", NAME_RESOLUTION},
+     {"macro_export", CODE_GENERATION}, // FIXME: And NAME_RESOLUTION as well
      // FIXME: This is not implemented yet, see
      // https://github.com/Rust-GCC/gccrs/issues/1475
      {"target_feature", CODE_GENERATION},