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

commit r16-3006-geb54ab61fd5532d08be99721ffdbb03590cc3fff
Author: Arthur Cohen <arthur.co...@embecosm.com>
Date:   Wed Jul 30 11:57:45 2025 +0200

    gccrs: offset_of: Compile the offset properly
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile-expr.cc (CompileExpr::visit): Add proper 
handling
            of the node.
            * rust-backend.h (lookup_field): Declare it.
            * rust-gcc.cc (lookup_field): Add forked implementation from gcc/c/.
    
    gcc/testsuite/ChangeLog:
    
            * rust/execute/torture/offset_of1.rs: New test.

Diff:
---
 gcc/rust/backend/rust-compile-expr.cc            | 25 ++++++++++++++++++++++-
 gcc/rust/rust-backend.h                          |  6 ++++++
 gcc/rust/rust-gcc.cc                             | 26 ++++++++++++++++++++++++
 gcc/testsuite/rust/execute/torture/offset_of1.rs | 16 +++++++++++++++
 4 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/backend/rust-compile-expr.cc 
b/gcc/rust/backend/rust-compile-expr.cc
index 8deb55ab77d6..64339236f89a 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -17,6 +17,8 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-compile-expr.h"
+#include "rust-backend.h"
+#include "rust-compile-type.h"
 #include "rust-compile-struct-field-expr.h"
 #include "rust-compile-pattern.h"
 #include "rust-compile-resolve-path.h"
@@ -32,7 +34,9 @@
 #include "print-tree.h"
 #include "rust-hir-expr.h"
 #include "rust-system.h"
+#include "rust-tree.h"
 #include "rust-tyty.h"
+#include "tree-core.h"
 
 namespace Rust {
 namespace Compile {
@@ -378,7 +382,26 @@ CompileExpr::visit (HIR::LlvmInlineAsm &expr)
 void
 CompileExpr::visit (HIR::OffsetOf &expr)
 {
-  rust_unreachable ();
+  TyTy::BaseType *type = nullptr;
+  if (!ctx->get_tyctx ()->lookup_type (
+       expr.get_type ().get_mappings ().get_hirid (), &type))
+    {
+      translated = error_mark_node;
+      return;
+    }
+
+  auto compiled_ty = TyTyResolveCompile::compile (ctx, type);
+
+  rust_assert (TREE_CODE (compiled_ty) == RECORD_TYPE);
+
+  // Create an identifier node for the field
+  auto field_id = Backend::get_identifier_node (expr.get_field ().as_string 
());
+
+  // And now look it up and get its value for `byte_position`
+  auto field = Backend::lookup_field (compiled_ty, field_id);
+  auto field_value = TREE_VALUE (field);
+
+  translated = byte_position (field_value);
 }
 
 void
diff --git a/gcc/rust/rust-backend.h b/gcc/rust/rust-backend.h
index 205eb2b71465..95ca7a9fe46d 100644
--- a/gcc/rust/rust-backend.h
+++ b/gcc/rust/rust-backend.h
@@ -397,6 +397,12 @@ tree goto_statement (tree, location_t);
 // recover.
 tree label_address (tree, location_t);
 
+// Lookup a field from a type given its name.
+// Build the `component` tree with `Backend::get_identifier_node`.
+//
+// Forked from the C frontend.
+tree lookup_field (const_tree, tree);
+
 // Functions.
 
 // Bit flags to pass to the function method.
diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc
index 0f9ccfb9c81a..398dea17d14c 100644
--- a/gcc/rust/rust-gcc.cc
+++ b/gcc/rust/rust-gcc.cc
@@ -2366,4 +2366,30 @@ write_global_definitions (const std::vector<tree> 
&type_decls,
   delete[] defs;
 }
 
+tree
+lookup_field (const_tree type, tree component)
+{
+  tree field;
+
+  for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
+    {
+      if (DECL_NAME (field) == NULL_TREE
+         && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
+       {
+         tree anon = lookup_field (TREE_TYPE (field), component);
+
+         if (anon)
+           return tree_cons (NULL_TREE, field, anon);
+       }
+
+      if (DECL_NAME (field) == component)
+       break;
+    }
+
+  if (field == NULL_TREE)
+    return NULL_TREE;
+
+  return tree_cons (NULL_TREE, field, NULL_TREE);
+}
+
 } // namespace Backend
diff --git a/gcc/testsuite/rust/execute/torture/offset_of1.rs 
b/gcc/testsuite/rust/execute/torture/offset_of1.rs
new file mode 100644
index 000000000000..7d39483f25f0
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/offset_of1.rs
@@ -0,0 +1,16 @@
+// { dg-do run { target x86_64*-*-* } }
+// { dg-additional-options "-frust-assume-builtin-offset-of" }
+
+pub struct Foo {
+    pub a: i32,
+    pub b: i32,
+}
+
+fn main() -> i32 {
+    let a = offset_of!(Foo, a); // valid
+    let b = offset_of!(Foo, b); // valid
+
+    let res = a + b - 4;
+
+    res as i32
+}

Reply via email to