Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the amount of raw pointer the HIR API #2878

Merged
merged 11 commits into from
Nov 20, 2024
7 changes: 7 additions & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ GRS_OBJS = \
rust/rust-polonius.o\
rust/rust-hir-dot-operator.o \
rust/rust-hir-path-probe.o \
rust/rust-hir-path.o \
rust/rust-hir-type.o \
rust/rust-hir-expr.o \
rust/rust-hir-type-abstract.o \
rust/rust-hir-item.o \
rust/rust-hir-stmt.o \
rust/rust-hir-generic-param.o \
rust/rust-type-util.o \
rust/rust-coercion.o \
rust/rust-casts.o \
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/backend/rust-compile-asm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ CompileAsm::asm_construct_outputs (HIR::InlineAsm &expr)
{
auto out = output.get_out ();

tree out_tree = CompileExpr::Compile (out.expr.get (), this->ctx);
tree out_tree = CompileExpr::Compile (*out.expr, this->ctx);
// expects a tree list
// TODO: This assumes that the output is a register
std::string expr_name = "=r";
Expand All @@ -112,7 +112,7 @@ CompileAsm::asm_construct_inputs (HIR::InlineAsm &expr)
{
auto in = input.get_in ();

tree in_tree = CompileExpr::Compile (in.expr.get (), this->ctx);
tree in_tree = CompileExpr::Compile (*in.expr, this->ctx);
// expects a tree list
// TODO: This assumes that the input is a register
std::string expr_name = "r";
Expand Down
37 changes: 19 additions & 18 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,9 @@ HIRCompileBase::compile_function_body (tree fndecl,

if (function_body.has_expr ())
{
location_t locus = function_body.get_final_expr ()->get_locus ();
tree return_value = CompileExpr::Compile (function_body.expr.get (), ctx);
location_t locus = function_body.get_final_expr ().get_locus ();
tree return_value
= CompileExpr::Compile (function_body.get_final_expr (), ctx);

// we can only return this if non unit value return type
if (!fn_return_ty->is_unit ())
Expand Down Expand Up @@ -709,18 +710,18 @@ HIRCompileBase::compile_function (
size_t i = is_method ? 1 : 0;
for (auto &referenced_param : function_params)
{
auto tyty_param = fntype->param_at (i++);
auto param_tyty = tyty_param.second;
auto &tyty_param = fntype->param_at (i++);
auto param_tyty = tyty_param.get_type ();
auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty);

location_t param_locus = referenced_param.get_locus ();
Bvariable *compiled_param_var
= CompileFnParam::compile (ctx, fndecl, &referenced_param,
= CompileFnParam::compile (ctx, fndecl, referenced_param,
compiled_param_type, param_locus);

param_vars.push_back (compiled_param_var);

const HIR::Pattern &param_pattern = *referenced_param.get_param_name ();
const HIR::Pattern &param_pattern = referenced_param.get_param_name ();
ctx->insert_var_decl (param_pattern.get_mappings ().get_hirid (),
compiled_param_var);
}
Expand Down Expand Up @@ -768,14 +769,14 @@ HIRCompileBase::compile_function (
tree
HIRCompileBase::compile_constant_item (
TyTy::BaseType *resolved_type, const Resolver::CanonicalPath &canonical_path,
HIR::Expr *const_value_expr, location_t locus)
HIR::Expr &const_value_expr, location_t locus)
{
const std::string &ident = canonical_path.get ();

tree type = TyTyResolveCompile::compile (ctx, resolved_type);
tree const_type = build_qualified_type (type, TYPE_QUAL_CONST);
bool is_block_expr
= const_value_expr->get_expression_type () == HIR::Expr::ExprType::Block;
= const_value_expr.get_expression_type () == HIR::Expr::ExprType::Block;

// in order to compile a block expr we want to reuse as much existing
// machineary that we already have. This means the best approach is to
Expand All @@ -789,14 +790,14 @@ HIRCompileBase::compile_constant_item (
TREE_READONLY (fndecl) = 1;

tree enclosing_scope = NULL_TREE;
location_t start_location = const_value_expr->get_locus ();
location_t end_location = const_value_expr->get_locus ();
location_t start_location = const_value_expr.get_locus ();
location_t end_location = const_value_expr.get_locus ();
if (is_block_expr)
{
HIR::BlockExpr *function_body
= static_cast<HIR::BlockExpr *> (const_value_expr);
start_location = function_body->get_locus ();
end_location = function_body->get_end_locus ();
HIR::BlockExpr &function_body
= static_cast<HIR::BlockExpr &> (const_value_expr);
start_location = function_body.get_locus ();
end_location = function_body.get_end_locus ();
}

tree code_block = Backend::block (fndecl, enclosing_scope, {} /*locals*/,
Expand All @@ -814,17 +815,17 @@ HIRCompileBase::compile_constant_item (

if (is_block_expr)
{
HIR::BlockExpr *function_body
= static_cast<HIR::BlockExpr *> (const_value_expr);
compile_function_body (fndecl, *function_body, resolved_type);
HIR::BlockExpr &function_body
= static_cast<HIR::BlockExpr &> (const_value_expr);
compile_function_body (fndecl, function_body, resolved_type);
}
else
{
tree value = CompileExpr::Compile (const_value_expr, ctx);

tree return_expr
= Backend::return_statement (fndecl, value,
const_value_expr->get_locus ());
const_value_expr.get_locus ());
ctx->add_statement (return_expr);
}

Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/backend/rust-compile-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class HIRCompileBase

tree compile_constant_item (TyTy::BaseType *resolved_type,
const Resolver::CanonicalPath &canonical_path,
HIR::Expr *const_value_expr, location_t locus);
HIR::Expr &const_value_expr, location_t locus);

tree compile_function (const std::string &fn_name, HIR::SelfParam &self_param,
std::vector<HIR::FunctionParam> &function_params,
Expand Down
27 changes: 11 additions & 16 deletions gcc/rust/backend/rust-compile-block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ CompileBlock::CompileBlock (Context *ctx, Bvariable *result)
{}

tree
CompileBlock::compile (HIR::BlockExpr *expr, Context *ctx, Bvariable *result)
CompileBlock::compile (HIR::BlockExpr &expr, Context *ctx, Bvariable *result)
{
CompileBlock compiler (ctx, result);
compiler.visit (*expr);
compiler.visit (expr);
return compiler.translated;
}

Expand Down Expand Up @@ -60,10 +60,10 @@ CompileBlock::visit (HIR::BlockExpr &expr)

if (expr.has_expr ())
{
tree compiled_expr = CompileExpr::Compile (expr.expr.get (), ctx);
tree compiled_expr = CompileExpr::Compile (expr.get_final_expr (), ctx);
if (result != nullptr)
{
location_t locus = expr.get_final_expr ()->get_locus ();
location_t locus = expr.get_final_expr ().get_locus ();
tree result_reference = Backend::var_expression (result, locus);

tree assignment
Expand Down Expand Up @@ -93,10 +93,8 @@ CompileConditionalBlocks::visit (HIR::IfExpr &expr)
{
fncontext fnctx = ctx->peek_fn ();
tree fndecl = fnctx.fndecl;
tree condition_expr
= CompileExpr::Compile (expr.get_if_condition ().get (), ctx);
tree then_block
= CompileBlock::compile (expr.get_if_block ().get (), ctx, result);
tree condition_expr = CompileExpr::Compile (expr.get_if_condition (), ctx);
tree then_block = CompileBlock::compile (expr.get_if_block (), ctx, result);

translated = Backend::if_statement (fndecl, condition_expr, then_block, NULL,
expr.get_locus ());
Expand All @@ -107,23 +105,20 @@ CompileConditionalBlocks::visit (HIR::IfExprConseqElse &expr)
{
fncontext fnctx = ctx->peek_fn ();
tree fndecl = fnctx.fndecl;
tree condition_expr
= CompileExpr::Compile (expr.get_if_condition ().get (), ctx);
tree then_block
= CompileBlock::compile (expr.get_if_block ().get (), ctx, result);
tree condition_expr = CompileExpr::Compile (expr.get_if_condition (), ctx);
tree then_block = CompileBlock::compile (expr.get_if_block (), ctx, result);

// else block
std::vector<Bvariable *> locals;
location_t start_location = expr.get_else_block ()->get_locus ();
location_t end_location = expr.get_else_block ()->get_locus (); // FIXME
location_t start_location = expr.get_else_block ().get_locus ();
location_t end_location = expr.get_else_block ().get_locus (); // FIXME
tree enclosing_scope = ctx->peek_enclosing_scope ();
tree else_block = Backend::block (fndecl, enclosing_scope, locals,
start_location, end_location);
ctx->push_block (else_block);

tree else_stmt_decl
= CompileExprWithBlock::compile (expr.get_else_block ().get (), ctx,
result);
= CompileExprWithBlock::compile (&expr.get_else_block (), ctx, result);

ctx->add_statement (else_stmt_decl);

Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/backend/rust-compile-block.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Compile {
class CompileBlock : private HIRCompileBase
{
public:
static tree compile (HIR::BlockExpr *expr, Context *ctx, Bvariable *result);
static tree compile (HIR::BlockExpr &expr, Context *ctx, Bvariable *result);

protected:
void visit (HIR::BlockExpr &expr);
Expand Down Expand Up @@ -134,7 +134,7 @@ class CompileExprWithBlock : public HIRCompileBase,

void visit (HIR::BlockExpr &expr) override
{
translated = CompileBlock::compile (&expr, ctx, result);
translated = CompileBlock::compile (expr, ctx, result);
}

// Empty visit for unused Expression HIR nodes.
Expand Down
Loading
Loading