Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gcc/rust/backend/rust-compile-block.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define RUST_COMPILE_BLOCK

#include "rust-compile-base.h"
#include "rust-hir-expr.h"
#include "rust-hir-visitor.h"

namespace Rust {
Expand Down Expand Up @@ -103,6 +104,7 @@ class CompileConditionalBlocks : public HIRCompileBase,
void visit (HIR::AsyncBlockExpr &) override {}
void visit (HIR::InlineAsm &) override {}
void visit (HIR::LlvmInlineAsm &) override {}
void visit (HIR::OffsetOf &) override {}

private:
CompileConditionalBlocks (Context *ctx, Bvariable *result)
Expand Down Expand Up @@ -192,6 +194,7 @@ class CompileExprWithBlock : public HIRCompileBase,
void visit (HIR::AsyncBlockExpr &) override {}
void visit (HIR::InlineAsm &) override {}
void visit (HIR::LlvmInlineAsm &) override {}
void visit (HIR::OffsetOf &) override {}
void visit (HIR::AnonConst &) override {}

private:
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ CompileExpr::visit (HIR::LlvmInlineAsm &expr)
ctx->add_statement (asm_codegen.tree_codegen_asm (expr));
}

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

}

void
CompileExpr::visit (HIR::IfExprConseqElse &expr)
{
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/backend/rust-compile-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class CompileExpr : private HIRCompileBase, protected HIR::HIRExpressionVisitor
void visit (HIR::ClosureExpr &expr) override;
void visit (HIR::InlineAsm &expr) override;
void visit (HIR::LlvmInlineAsm &expr) override;
void visit (HIR::OffsetOf &expr) override;

// TODO
void visit (HIR::ErrorPropagationExpr &) override {}
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ void
ExprStmtBuilder::visit (HIR::LlvmInlineAsm &expr)
{}

void
ExprStmtBuilder::visit (HIR::OffsetOf &expr)
{}

void
ExprStmtBuilder::visit (HIR::MethodCallExpr &expr)
{}
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef RUST_BIR_BUILDER_EXPR_H
#define RUST_BIR_BUILDER_EXPR_H

#include "rust-hir-expr.h"
#include "rust-hir-visitor.h"
#include "rust-bir-builder-internal.h"

Expand Down Expand Up @@ -103,6 +104,7 @@ class ExprStmtBuilder final : public AbstractExprBuilder,
void visit (HIR::IfExprConseqElse &expr) override;
void visit (HIR::InlineAsm &expr) override;
void visit (HIR::LlvmInlineAsm &expr) override;
void visit (HIR::OffsetOf &expr) override;

void visit (HIR::MatchExpr &expr) override;
void visit (HIR::AwaitExpr &expr) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class LazyBooleanExprBuilder : public AbstractExprBuilder

void visit (HIR::InlineAsm &expr) override {}
void visit (HIR::LlvmInlineAsm &expr) override {}
void visit (HIR::OffsetOf &expr) override {}

protected: // Illegal at this position.
void visit (HIR::StructExprFieldIdentifier &field) override
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "rust-bir-builder-internal.h"
#include "rust-bir-builder-expr-stmt.h"
#include "rust-hir-expr.h"

namespace Rust {
namespace BIR {
Expand Down Expand Up @@ -156,6 +157,7 @@ class StructBuilder : public AbstractBuilder, public HIR::HIRFullVisitor
void visit (HIR::AsyncBlockExpr &expr) override { rust_unreachable (); }
void visit (HIR::InlineAsm &expr) override { rust_unreachable (); }
void visit (HIR::LlvmInlineAsm &expr) override { rust_unreachable (); }
void visit (HIR::OffsetOf &expr) override { rust_unreachable (); }
void visit (HIR::TypeParam &param) override { rust_unreachable (); }
void visit (HIR::ConstGenericParam &param) override { rust_unreachable (); }
void visit (HIR::LifetimeWhereClauseItem &item) override
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/checks/errors/borrowck/rust-function-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef RUST_HIR_FUNCTION_COLLECTOR_H
#define RUST_HIR_FUNCTION_COLLECTOR_H

#include "rust-hir-expr.h"
#include "rust-hir-item.h"
#include "rust-hir-visitor.h"
#include "rust-hir.h"
Expand Down Expand Up @@ -126,6 +127,7 @@ class FunctionCollector : public HIR::HIRFullVisitor
void visit (HIR::AsyncBlockExpr &expr) override {}
void visit (HIR::InlineAsm &expr) override {}
void visit (HIR::LlvmInlineAsm &expr) override {}
void visit (HIR::OffsetOf &expr) override {}
void visit (HIR::TypeParam &param) override {}
void visit (HIR::ConstGenericParam &param) override {}
void visit (HIR::LifetimeWhereClauseItem &item) override {}
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ void
PrivacyReporter::visit (HIR::LlvmInlineAsm &)
{}

void
PrivacyReporter::visit (HIR::OffsetOf &expr)
{
// TODO: Do we have to do anything?
}

void
PrivacyReporter::visit (HIR::TypePath &path)
{
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef RUST_PRIVACY_REPORTER_H
#define RUST_PRIVACY_REPORTER_H

#include "rust-hir-expr.h"
#include "rust-hir-map.h"
#include "rust-hir-visitor.h"
#include "rust-mapping-common.h"
Expand Down Expand Up @@ -128,6 +129,7 @@ types
virtual void visit (HIR::AsyncBlockExpr &expr);
virtual void visit (HIR::InlineAsm &expr);
virtual void visit (HIR::LlvmInlineAsm &expr);
virtual void visit (HIR::OffsetOf &expr);

virtual void visit (HIR::EnumItemTuple &);
virtual void visit (HIR::EnumItemStruct &);
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/checks/errors/rust-const-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,10 @@ void
ConstChecker::visit (LlvmInlineAsm &)
{}

void
ConstChecker::visit (OffsetOf &)
{}

void
ConstChecker::visit (TypeParam &)
{}
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/checks/errors/rust-const-checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class ConstChecker : public HIRFullVisitor
virtual void visit (AsyncBlockExpr &expr) override;
virtual void visit (InlineAsm &expr) override;
virtual void visit (LlvmInlineAsm &expr) override;
virtual void visit (OffsetOf &expr) override;

virtual void visit (TypeParam &param) override;
virtual void visit (ConstGenericParam &param) override;
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/checks/errors/rust-hir-pattern-analysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ void
PatternChecker::visit (LlvmInlineAsm &expr)
{}

void
PatternChecker::visit (OffsetOf &expr)
{}

void
PatternChecker::visit (TypeParam &)
{}
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/checks/errors/rust-hir-pattern-analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class PatternChecker : public HIR::HIRFullVisitor
virtual void visit (AsyncBlockExpr &expr) override;
virtual void visit (InlineAsm &expr) override;
virtual void visit (LlvmInlineAsm &expr) override;
virtual void visit (OffsetOf &expr) override;
virtual void visit (TypeParam &param) override;
virtual void visit (ConstGenericParam &param) override;
virtual void visit (LifetimeWhereClauseItem &item) override;
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/checks/errors/rust-unsafe-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,12 @@ UnsafeChecker::visit (LlvmInlineAsm &expr)
"use of inline assembly is unsafe and requires unsafe function or block");
}

void
UnsafeChecker::visit (OffsetOf &expr)
{
// nothing to do, offset_of!() is safe
}

void
UnsafeChecker::visit (TypeParam &)
{}
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/checks/errors/rust-unsafe-checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class UnsafeChecker : public HIRFullVisitor
virtual void visit (AsyncBlockExpr &expr) override;
virtual void visit (InlineAsm &expr) override;
virtual void visit (LlvmInlineAsm &expr) override;
virtual void visit (OffsetOf &expr) override;
virtual void visit (TypeParam &param) override;
virtual void visit (ConstGenericParam &param) override;
virtual void visit (LifetimeWhereClauseItem &item) override;
Expand Down
12 changes: 10 additions & 2 deletions gcc/rust/hir/rust-ast-lower-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1059,8 +1059,16 @@ ASTLoweringExpr::visit (AST::FormatArgs &fmt)
void
ASTLoweringExpr::visit (AST::OffsetOf &offset_of)
{
// FIXME: Implement HIR::OffsetOf node and const evaluation
rust_unreachable ();
auto type = std::unique_ptr<Type> (
ASTLoweringType::translate (offset_of.get_type ()));

auto crate_num = mappings.get_current_crate ();
Analysis::NodeMapping mapping (crate_num, offset_of.get_node_id (),
mappings.get_next_hir_id (crate_num),
mappings.get_next_localdef_id (crate_num));

translated = new HIR::OffsetOf (std::move (type), offset_of.get_field (),
mapping, offset_of.get_locus ());
}

} // namespace HIR
Expand Down
11 changes: 11 additions & 0 deletions gcc/rust/hir/rust-hir-dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,17 @@ void
Dump::visit (LlvmInlineAsm &e)
{}

void
Dump::visit (OffsetOf &e)
{
begin ("OffsetOf");

put_field ("type", e.get_type ().as_string ());
put_field ("field", e.get_field ());

end ("OffsetOf");
}

void
Dump::visit (TypeParam &e)
{
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/hir/rust-hir-dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class Dump : public HIRFullVisitor
virtual void visit (AsyncBlockExpr &) override;
virtual void visit (InlineAsm &) override;
virtual void visit (LlvmInlineAsm &) override;
virtual void visit (OffsetOf &) override;

virtual void visit (TypeParam &) override;
virtual void visit (ConstGenericParam &) override;
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/hir/tree/rust-hir-expr-abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Expr : public Node, virtual public FullVisitable
Path,
InlineAsm,
LlvmInlineAsm,
OffsetOf,
};

BaseKind get_hir_kind () override final { return Node::BaseKind::EXPR; }
Expand Down
36 changes: 36 additions & 0 deletions gcc/rust/hir/tree/rust-hir-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1526,5 +1526,41 @@ InlineAsm::InlineAsm (location_t locus, bool is_global_asm,
clobber_abi (std::move (clobber_abi)), options (std::move (options))
{}

OffsetOf &
OffsetOf::operator= (const OffsetOf &other)
{
ExprWithoutBlock::operator= (other);

type = other.type->clone_type ();
field = other.field;
loc = other.loc;

return *this;
}

ExprWithoutBlock *
OffsetOf::clone_expr_without_block_impl () const
{
return new OffsetOf (*this);
}

std::string
OffsetOf::as_string () const
{
return "OffsetOf(" + type->as_string () + ", " + field.as_string () + ")";
}

void
OffsetOf::accept_vis (HIRExpressionVisitor &vis)
{
vis.visit (*this);
}

void
OffsetOf::accept_vis (HIRFullVisitor &vis)
{
vis.visit (*this);
}

} // namespace HIR
} // namespace Rust
36 changes: 36 additions & 0 deletions gcc/rust/hir/tree/rust-hir-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3236,6 +3236,42 @@ class InlineAsm : public ExprWithoutBlock
AST::AttrVec outer_attribs = AST::AttrVec ());
};

class OffsetOf : public ExprWithoutBlock
{
public:
OffsetOf (std::unique_ptr<Type> &&type, Identifier field,
Analysis::NodeMapping mappings, location_t loc)
: ExprWithoutBlock (mappings), type (std::move (type)), field (field),
loc (loc)
{}

OffsetOf (const OffsetOf &other)
: ExprWithoutBlock (other), type (other.type->clone_type ()),
field (other.field), loc (other.loc)
{}

OffsetOf &operator= (const OffsetOf &other);

ExprWithoutBlock *clone_expr_without_block_impl () const override;
std::string as_string () const override;

void accept_vis (HIRExpressionVisitor &vis) override;
void accept_vis (HIRFullVisitor &vis) override;

ExprType get_expression_type () const override { return ExprType::OffsetOf; }

location_t get_locus () const override { return loc; }

Type &get_type () { return *type; }
const Type &get_type () const { return *type; }
const Identifier &get_field () const { return field; }

private:
std::unique_ptr<Type> type;
Identifier field;
location_t loc;
};

struct LlvmOperand
{
std::string constraint;
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/hir/tree/rust-hir-full-decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class InlineAsmRegClass;
class InlineAsmOperand;
class InlineAsm;
class LlvmInlineAsm;
class OffsetOf;

// rust-stmt.h
class EmptyStmt;
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/hir/tree/rust-hir-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,12 @@ DefaultHIRVisitor::walk (LlvmInlineAsm &expr)
input.expr->accept_vis (*this);
}

void
DefaultHIRVisitor::walk (OffsetOf &expr)
{
expr.get_type ().accept_vis (*this);
}

void
DefaultHIRVisitor::walk (TypeParam &param)
{
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/hir/tree/rust-hir-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class HIRFullVisitor
virtual void visit (AsyncBlockExpr &expr) = 0;
virtual void visit (InlineAsm &expr) = 0;
virtual void visit (LlvmInlineAsm &expr) = 0;
virtual void visit (OffsetOf &expr) = 0;
virtual void visit (TypeParam &param) = 0;
virtual void visit (ConstGenericParam &param) = 0;
virtual void visit (LifetimeWhereClauseItem &item) = 0;
Expand Down Expand Up @@ -254,6 +255,7 @@ class DefaultHIRVisitor : public HIRFullVisitor
virtual void visit (AsyncBlockExpr &node) override { walk (node); }
virtual void visit (InlineAsm &node) override { walk (node); }
virtual void visit (LlvmInlineAsm &node) override { walk (node); }
virtual void visit (OffsetOf &node) override { walk (node); }
virtual void visit (TypeParam &node) override { walk (node); }
virtual void visit (ConstGenericParam &node) override { walk (node); }
virtual void visit (LifetimeWhereClauseItem &node) override { walk (node); }
Expand Down Expand Up @@ -392,6 +394,7 @@ class DefaultHIRVisitor : public HIRFullVisitor
virtual void walk (AsyncBlockExpr &) final;
virtual void walk (InlineAsm &) final;
virtual void walk (LlvmInlineAsm &) final;
virtual void walk (OffsetOf &) final;
virtual void walk (TypeParam &) final;
virtual void walk (ConstGenericParam &) final;
virtual void walk (LifetimeWhereClauseItem &) final;
Expand Down Expand Up @@ -531,6 +534,7 @@ class HIRFullVisitorBase : public HIRFullVisitor
virtual void visit (AsyncBlockExpr &) override {}
virtual void visit (InlineAsm &) override {}
virtual void visit (LlvmInlineAsm &) override {}
virtual void visit (OffsetOf &) override {}

virtual void visit (TypeParam &) override {}
virtual void visit (ConstGenericParam &) override {}
Expand Down Expand Up @@ -754,6 +758,7 @@ class HIRExpressionVisitor
virtual void visit (IfExpr &expr) = 0;
virtual void visit (IfExprConseqElse &expr) = 0;
virtual void visit (InlineAsm &expr) = 0;
virtual void visit (OffsetOf &expr) = 0;
virtual void visit (LlvmInlineAsm &expr) = 0;
virtual void visit (MatchExpr &expr) = 0;
virtual void visit (AwaitExpr &expr) = 0;
Expand Down
Loading
Loading