Add HIR::OffsetOf node#4010
Conversation
CohenArthur
commented
Jul 28, 2025
- hir: Add OffsetOf node
| void | ||
| CompileExpr::visit (HIR::OffsetOf &expr) | ||
| { | ||
| rust_unreachable (); |
There was a problem hiding this comment.
I think there are maybe two ways to do this but i would probably do it like this:
-
lookup the tyty type on the expr.get_type () HirId;
-
Then call ``tree offset_type = TyTyResolveCompile::compile (ctx, tyty_of_the_type);```
-
Then you will need to assert its a RECORD_TYPE iirc
-
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
-
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
left a comment
There was a problem hiding this comment.
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
|
just noticed gcc provides both: 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 |
4a39fa9 to
d2294f8
Compare
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.