Skip to content

Add HIR::OffsetOf node#4010

Merged
CohenArthur merged 1 commit into
Rust-GCC:masterfrom
CohenArthur:offset-of-hir
Jul 31, 2025
Merged

Add HIR::OffsetOf node#4010
CohenArthur merged 1 commit into
Rust-GCC:masterfrom
CohenArthur:offset-of-hir

Conversation

@CohenArthur

Copy link
Copy Markdown
Member
  • hir: Add OffsetOf node

void
CompileExpr::visit (HIR::OffsetOf &expr)
{
rust_unreachable ();

@philberty philberty Jul 29, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are maybe two ways to do this but i would probably do it like this:

  1. lookup the tyty type on the expr.get_type () HirId;

  2. Then call ``tree offset_type = TyTyResolveCompile::compile (ctx, tyty_of_the_type);```

  3. Then you will need to assert its a RECORD_TYPE iirc

  4. You will need to iterate the record to make sure this field exists we probably should do that check on the ADTType in the type check pass upfront

  5. call type_field_offset from rust-gcc.cc to get the offset and turn it into a tree using build_int_cst (size_node_type, value)

will probably do this for you

@philberty philberty left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All LGTM to me i gave you a suggestion on how to fill out the code-gen pass for it but that can be a separate pr if you want

@philberty

philberty commented Jul 29, 2025

Copy link
Copy Markdown
Member

just noticed gcc provides both:

tree
byte_position (const_tree field)
{
  return byte_from_pos (DECL_FIELD_OFFSET (field),
			DECL_FIELD_BIT_OFFSET (field));
}

/* Likewise, but return as an integer.  It must be representable in
   that way (since it could be a signed value, we don't have the
   option of returning -1 like int_size_in_byte can.  */

HOST_WIDE_INT
int_byte_position (const_tree field)
{
  return tree_to_shwi (byte_position (field));
}

You could call the byte_position on the field index instead so you dont have to convert back to a tree again

You'll see what i mean when you read rust-gcc.cc:type_field_offset

@CohenArthur CohenArthur added this pull request to the merge queue Jul 31, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Jul 31, 2025
@CohenArthur CohenArthur enabled auto-merge July 31, 2025 10:05
gcc/rust/ChangeLog:

	* hir/tree/rust-hir-expr.h (class OffsetOf): New.
	* hir/tree/rust-hir-expr.cc: Define its methods.
	* hir/tree/rust-hir-expr-abstract.h: Add ExprType::OffsetOf.
	* hir/tree/rust-hir-full-decls.h (class OffsetOf): Declare it.
	* backend/rust-compile-block.h: Add handling for OffsetOf.
	* backend/rust-compile-expr.cc (CompileExpr::visit): Likewise.
	* backend/rust-compile-expr.h: Likewise.
	* checks/errors/borrowck/rust-bir-builder-expr-stmt.cc (ExprStmtBuilder::visit): Likewise.
	* checks/errors/borrowck/rust-bir-builder-expr-stmt.h (RUST_BIR_BUILDER_EXPR_H): Likewise.
	* checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h: Likewise.
	* checks/errors/borrowck/rust-bir-builder-struct.h: Likewise.
	* checks/errors/borrowck/rust-function-collector.h: Likewise.
	* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit): Likewise.
	* checks/errors/privacy/rust-privacy-reporter.h (RUST_PRIVACY_REPORTER_H): Likewise.
	* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
	* checks/errors/rust-const-checker.h: Likewise.
	* checks/errors/rust-hir-pattern-analysis.cc (PatternChecker::visit): Likewise.
	* checks/errors/rust-hir-pattern-analysis.h: Likewise.
	* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
	* checks/errors/rust-unsafe-checker.h: Likewise.
	* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Likewise.
	* hir/rust-hir-dump.cc (Dump::visit): Likewise.
	* hir/rust-hir-dump.h: Likewise.
	* hir/tree/rust-hir-visitor.cc (DefaultHIRVisitor::walk): Likewise.
	* hir/tree/rust-hir-visitor.h: Likewise.
	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise.
	* typecheck/rust-hir-type-check-expr.h (RUST_HIR_TYPE_CHECK_EXPR): Likewise.

gcc/testsuite/ChangeLog:

	* rust/compile/offset_of2.rs: New test.
@CohenArthur CohenArthur added this pull request to the merge queue Jul 31, 2025
Merged via the queue into Rust-GCC:master with commit 3449cb9 Jul 31, 2025
13 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in libcore 1.49 Jul 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants