From eb54ab61fd5532d08be99721ffdbb03590cc3fff Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Wed, 30 Jul 2025 11:57:45 +0200 Subject: 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. --- gcc/rust/backend/rust-compile-expr.cc | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 8deb55a..6433923 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -17,6 +17,8 @@ // . #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 -- cgit v1.1