]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: ast: Add OffsetOf node
authorArthur Cohen <arthur.cohen@embecosm.com>
Thu, 24 Jul 2025 09:03:35 +0000 (11:03 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:59 +0000 (16:36 +0200)
gcc/rust/ChangeLog:

* ast/rust-ast.h: Add OffsetOf expression kind.
* ast/rust-builtin-ast-nodes.h (class OffsetOf): Add node.
* ast/rust-ast.cc: Define it.
* ast/rust-ast-collector.cc: Add visitor for OffsetOf.
* ast/rust-ast-collector.h: Likewise.
* ast/rust-ast-visitor.cc: Likewise.
* ast/rust-ast-visitor.h: Likewise.
* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise.
* hir/rust-ast-lower-base.h: Likewise.
* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Likewise.
* hir/rust-ast-lower-expr.h: Likewise.
* resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise.
* resolve/rust-ast-resolve-base.h: Likewise.
* resolve/rust-early-name-resolver-2.0.cc: Likewise.
* expand/rust-derive.h:

15 files changed:
gcc/rust/ast/rust-ast-collector.cc
gcc/rust/ast/rust-ast-collector.h
gcc/rust/ast/rust-ast-visitor.cc
gcc/rust/ast/rust-ast-visitor.h
gcc/rust/ast/rust-ast.cc
gcc/rust/ast/rust-ast.h
gcc/rust/ast/rust-builtin-ast-nodes.h
gcc/rust/expand/rust-derive.h
gcc/rust/hir/rust-ast-lower-base.cc
gcc/rust/hir/rust-ast-lower-base.h
gcc/rust/hir/rust-ast-lower-expr.cc
gcc/rust/hir/rust-ast-lower-expr.h
gcc/rust/resolve/rust-ast-resolve-base.cc
gcc/rust/resolve/rust-ast-resolve-base.h
gcc/rust/resolve/rust-early-name-resolver-2.0.cc

index 4fe246d7a3ada05e7e611db0c0b8c47a7df5acaa..f4033ad4aaefb090ae9e0678761b6b8b9859bb67 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "rust-ast-collector.h"
 #include "rust-ast.h"
+#include "rust-builtin-ast-nodes.h"
 #include "rust-diagnostics.h"
 #include "rust-expr.h"
 #include "rust-item.h"
@@ -2982,5 +2983,23 @@ TokenCollector::visit (AST::FormatArgs &fmt)
                 __FILE__, __LINE__);
 }
 
+void
+TokenCollector::visit (AST::OffsetOf &offset_of)
+{
+  auto loc = offset_of.get_locus ();
+
+  push (Rust::Token::make_identifier (loc, "offset_of"));
+  push (Rust::Token::make (EXCLAM, loc));
+  push (Rust::Token::make (LEFT_PAREN, loc));
+
+  visit (offset_of.get_type ());
+
+  push (Rust::Token::make (COMMA, loc));
+
+  push (Rust::Token::make_identifier (offset_of.get_field ()));
+
+  push (Rust::Token::make (RIGHT_PAREN, loc));
+}
+
 } // namespace AST
 } // namespace Rust
index e8af5878a2cd337d52ac00b170496ad54d3e17d0..767d211ddf3ad922bb1729dfa3ebf93da80e5040 100644 (file)
@@ -403,6 +403,7 @@ public:
   void visit (BareFunctionType &type);
 
   void visit (FormatArgs &fmt);
+  void visit (OffsetOf &offset_of);
 };
 } // namespace AST
 
index 7be2ecc1b48dc3aac5a2508fd863dcf9b0c9a450..f752b3a8f1583d86de80efbccab74c6dd8f44dfd 100644 (file)
@@ -19,6 +19,7 @@
 #include "rust-ast-visitor.h"
 #include "rust-ast-full-decls.h"
 #include "rust-ast.h"
+#include "rust-builtin-ast-nodes.h"
 #include "rust-path.h"
 #include "rust-token.h"
 #include "rust-expr.h"
@@ -1489,6 +1490,12 @@ DefaultASTVisitor::visit (AST::FormatArgs &)
   // FIXME: Do we have anything to do? any subnodes to visit? Probably, right?
 }
 
+void
+DefaultASTVisitor::visit (AST::OffsetOf &offset_of)
+{
+  visit (offset_of.get_type ());
+}
+
 void
 DefaultASTVisitor::visit (AST::VariadicParam &param)
 {
index 6111b0548edc7d89119407fc2a14f5190c1b47ce..020400dde3fe1e6eca3167e138ddf7701c639345 100644 (file)
@@ -238,6 +238,7 @@ public:
 
   // special AST nodes for certain builtin macros such as `asm!()`
   virtual void visit (FormatArgs &fmt) = 0;
+  virtual void visit (OffsetOf &fmt) = 0;
 
   // TODO: rust-cond-compilation.h visiting? not currently used
 };
@@ -408,6 +409,7 @@ public:
   virtual void visit (AST::FunctionParam &param) override;
   virtual void visit (AST::VariadicParam &param) override;
   virtual void visit (AST::FormatArgs &fmt) override;
+  virtual void visit (AST::OffsetOf &fmt) override;
 
   template <typename T> void visit (T &node) { node.accept_vis (*this); }
 
index 2b7ee5cb8bf01aa835773e6ea46e7f3765ab1df6..ec1596bfb978cbe97a7156ed80c9d11ce44b8d54 100644 (file)
@@ -5088,6 +5088,12 @@ FormatArgs::accept_vis (ASTVisitor &vis)
   vis.visit (*this);
 }
 
+void
+OffsetOf::accept_vis (ASTVisitor &vis)
+{
+  vis.visit (*this);
+}
+
 std::string
 FormatArgs::as_string () const
 {
@@ -5095,6 +5101,12 @@ FormatArgs::as_string () const
   return "FormatArgs";
 }
 
+std::string
+OffsetOf::as_string () const
+{
+  return "OffsetOf(" + type->as_string () + ", " + field.as_string () + ")";
+}
+
 location_t
 FormatArgs::get_locus () const
 {
@@ -5139,6 +5151,24 @@ FormatArgs::clone_expr_impl () const
   return new FormatArgs (*this);
 }
 
+std::vector<Attribute> &
+OffsetOf::get_outer_attrs ()
+{
+  rust_unreachable ();
+}
+
+void
+OffsetOf::set_outer_attrs (std::vector<Attribute>)
+{
+  rust_unreachable ();
+}
+
+Expr *
+OffsetOf::clone_expr_impl () const
+{
+  return new OffsetOf (*this);
+}
+
 } // namespace AST
 
 std::ostream &
index 33c50b71d38d216e7b578251dec1ccff59a656d2..93d4ff132e505716441066519c8e48a1cf896d10 100644 (file)
@@ -1307,6 +1307,7 @@ public:
     LlvmInlineAsm,
     Identifier,
     FormatArgs,
+    OffsetOf,
     MacroInvocation,
     Borrow,
     Dereference,
index 3684092aca22de6236e25fb5f086680b75a3c25f..2893e7b46760a7f63b5a56043813a9353f2d37f2 100644 (file)
@@ -225,6 +225,59 @@ protected:
   virtual Expr *clone_expr_impl () const override;
 };
 
+/**
+ * The node associated with the builtin offset_of!() macro
+ */
+class OffsetOf : public Expr
+{
+public:
+  OffsetOf (std::unique_ptr<Type> &&type, Identifier field, location_t loc)
+    : type (std::move (type)), field (field), loc (loc)
+  {}
+
+  OffsetOf (const OffsetOf &other)
+    : type (other.type->clone_type ()), field (other.field), loc (other.loc),
+      marked_for_strip (other.marked_for_strip)
+  {}
+
+  OffsetOf &operator= (const OffsetOf &other)
+  {
+    type = other.type->clone_type ();
+    field = other.field;
+    loc = other.loc;
+    marked_for_strip = other.marked_for_strip;
+
+    return *this;
+  }
+
+  void accept_vis (AST::ASTVisitor &vis) override;
+
+  virtual location_t get_locus () const override { return loc; }
+  const Type &get_type () const { return *type; }
+  Type &get_type () { return *type; }
+  const Identifier &get_field () const { return field; }
+
+  bool is_expr_without_block () const override { return false; }
+
+  void mark_for_strip () override { marked_for_strip = true; }
+  bool is_marked_for_strip () const override { return marked_for_strip; }
+
+  std::string as_string () const override;
+
+  std::vector<Attribute> &get_outer_attrs () override;
+  void set_outer_attrs (std::vector<Attribute>) override;
+  Expr *clone_expr_impl () const override;
+
+  Expr::Kind get_expr_kind () const override { return Expr::Kind::OffsetOf; }
+
+private:
+  std::unique_ptr<Type> type;
+  Identifier field;
+
+  location_t loc;
+  bool marked_for_strip = false;
+};
+
 } // namespace AST
 } // namespace Rust
 
index 61c7355e7ab2753a29c65a4b7c48122490a63d64..297d0799122ec17f1a7e166b19461ef608cbda59 100644 (file)
@@ -254,6 +254,7 @@ private:
   virtual void visit (FunctionParam &param) override final{};
   virtual void visit (VariadicParam &param) override final{};
   virtual void visit (FormatArgs &param) override final{};
+  virtual void visit (OffsetOf &param) override final{};
 };
 
 } // namespace AST
index b07ac0c07500b5f86a88750fa1b69f774441a312..8e8f3b649d0f0ed91b96ae6b80e2ea82558cc003 100644 (file)
@@ -560,6 +560,10 @@ void
 ASTLoweringBase::visit (AST::FormatArgs &fmt)
 {}
 
+void
+ASTLoweringBase::visit (AST::OffsetOf &offset_of)
+{}
+
 HIR::Lifetime
 ASTLoweringBase::lower_lifetime (AST::Lifetime &lifetime,
                                 bool default_to_static_lifetime)
index 0284ff0c82bbc7930c6de52b650ce0028feb1a54..d9428d61be9ac4cd288d0cb74ce70b2b31dbcb5f 100644 (file)
@@ -20,6 +20,8 @@
 #define RUST_AST_LOWER_BASE
 
 #include "rust-ast.h"
+#include "rust-builtin-ast-nodes.h"
+#include "rust-expr.h"
 #include "rust-system.h"
 #include "rust-ast-full.h"
 #include "rust-ast-visitor.h"
@@ -262,6 +264,7 @@ public:
   virtual void visit (AST::SelfParam &param) override;
 
   virtual void visit (AST::FormatArgs &fmt) override;
+  virtual void visit (AST::OffsetOf &offset_of) override;
 
 protected:
   ASTLoweringBase ()
index 96820fd8c18d9d328e04e0a958cd808687d230d1..31126a8ad35df139e4a6ac8c9faec32cb54a45d4 100644 (file)
@@ -24,6 +24,7 @@
 #include "rust-ast-lower-pattern.h"
 #include "rust-ast-lower-type.h"
 #include "rust-ast.h"
+#include "rust-builtin-ast-nodes.h"
 #include "rust-diagnostics.h"
 #include "rust-hir-map.h"
 #include "rust-system.h"
@@ -1055,5 +1056,12 @@ ASTLoweringExpr::visit (AST::FormatArgs &fmt)
                 "FormatArgs lowering is not implemented yet");
 }
 
+void
+ASTLoweringExpr::visit (AST::OffsetOf &offset_of)
+{
+  // FIXME: Implement HIR::OffsetOf node and const evaluation
+  rust_unreachable ();
+}
+
 } // namespace HIR
 } // namespace Rust
index 9d1bf68e87621679b1eb806bd230c575940a639d..eb822c9f4de6d81bac875fc010ce4a4241eb6cd7 100644 (file)
@@ -126,8 +126,9 @@ public:
   void visit (AST::InlineAsm &expr) override;
   void visit (AST::LlvmInlineAsm &expr) override;
 
-  // Extra visitor for FormatArgs nodes
+  // Extra visitor for builtin macro nodes
   void visit (AST::FormatArgs &fmt) override;
+  void visit (AST::OffsetOf &offset_of) override;
 
 private:
   ASTLoweringExpr ();
index 05f34bc3f87428b19ba3dd77a5c322c1d32acc24..4890d8e519c149f0c17f18545987542bc64c9342 100644 (file)
@@ -675,5 +675,9 @@ void
 ResolverBase::visit (AST::FormatArgs &fmt)
 {}
 
+void
+ResolverBase::visit (AST::OffsetOf &offset_of)
+{}
+
 } // namespace Resolver
 } // namespace Rust
index 0cbf78e39abc9e9b2fba12a300a723c14a89845d..dce8e7bfc7505c9e3bf5e1da163c07ead61c249b 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "rust-ast-visitor.h"
 #include "rust-ast.h"
+#include "rust-builtin-ast-nodes.h"
 #include "rust-expr.h"
 #include "rust-name-resolver.h"
 #include "rust-diagnostics.h"
@@ -211,6 +212,7 @@ public:
   void visit (AST::SelfParam &param);
 
   void visit (AST::FormatArgs &fmt);
+  void visit (AST::OffsetOf &offset_of);
 
 protected:
   ResolverBase ()
index fa746d770fba8c517779d3bcc815f72a1e142cfd..bb93d95fe8e7db0b55f40e1af4985ec234f7bd74 100644 (file)
@@ -18,7 +18,7 @@
 
 #include "rust-early-name-resolver-2.0.h"
 #include "optional.h"
-#include "rust-ast-full.h"
+#include "options.h"
 #include "rust-diagnostics.h"
 #include "rust-hir-map.h"
 #include "rust-item.h"