Skip to content

Commit

Permalink
Refactor HIR to reduce the amount of raw pointers
Browse files Browse the repository at this point in the history
Attempt to use references and smart pointers whenever possible.

gcc/rust/ChangeLog:

	* backend/rust-compile-base.cc (HIRCompileBase::compile_function_body):
	Remove usage of get function to retrieve a raw pointer.
	* backend/rust-compile-base.h:
	Change API usage from raw pointer to a reference.
	* backend/rust-compile-block.cc (CompileBlock::compile): Likewise.
	(CompileBlock::visit): Likewise.
	(CompileConditionalBlocks::visit): Likewise.
	* backend/rust-compile-block.h: Likewise.
	* backend/rust-compile-expr.cc (CompileExpr::Compile): Likewise.
	(CompileExpr::visit): Likewise.
	(check_match_scrutinee): Likewise.
	(CompileExpr::array_value_expr): Likewise.
	(CompileExpr::array_copied_expr): Likewise.
	(CompileExpr::generate_closure_function): Likewise.
	(CompileExpr::generate_possible_fn_trait_call): Likewise.
	* backend/rust-compile-expr.h: Likewise.
	* backend/rust-compile-fnparam.cc (CompileFnParam::compile): Likewise.
	(CompileFnParam::visit): Likewise.
	* backend/rust-compile-fnparam.h: Likewise.
	* backend/rust-compile-implitem.cc (CompileTraitItem::visit): Likewise.
	* backend/rust-compile-intrinsic.cc (compile_fn_params): Likewise.
	* backend/rust-compile-item.cc (CompileItem::visit): Likewise.
	* backend/rust-compile-pattern.cc (CompilePatternCheckExpr::visit):
	Likewise.
	(compile_range_pattern_bound): Likewise.
	(CompilePatternBindings::visit): Likewise.
	(CompilePatternLet::visit): Likewise.
	* backend/rust-compile-pattern.h: Likewise.
	* backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve):
	Likewise.
	(HIRCompileBase::query_compile): Likewise.
	* backend/rust-compile-stmt.cc (CompileStmt::visit): Likewise.
	* backend/rust-compile-struct-field-expr.cc (CompileStructExprField::Compile):
	Likewise.
	(CompileStructExprField::visit): Likewise.
	* backend/rust-compile-struct-field-expr.h: Likewise.
	* backend/rust-compile-type.cc (TyTyResolveCompile::visit): Likewise.
	* backend/rust-compile-var-decl.h: Likewise.
	* backend/rust-compile.cc: Likewise.
	* backend/rust-mangle-v0.cc (v0_inherent_or_trait_impl_path): Likewise.
	* checks/errors/borrowck/rust-bir-builder-expr-stmt.cc (ExprStmtBuilder::visit):
	Likewise.
	* checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h: Likewise.
	* checks/errors/borrowck/rust-bir-builder-pattern.h: Likewise.
	* checks/errors/borrowck/rust-bir-builder-struct.h: Likewise.
	* checks/errors/borrowck/rust-bir-builder.h: Likewise.
	* checks/errors/borrowck/rust-function-collector.h: Likewise.
	* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::check_type_privacy):
	Likewise.
	(PrivacyReporter::visit): Likewise.
	* checks/errors/privacy/rust-privacy-reporter.h: Likewise.
	* checks/errors/privacy/rust-reachability.cc (ReachabilityVisitor::visit):
	Likewise.
	* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
	* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit):
	Likewise.
	* checks/lints/rust-lint-marklive.cc (MarkLive::visit): Likewise.
	* checks/lints/rust-lint-marklive.h: Likewise.
	* hir/rust-hir-dump.cc (Dump::visit): Likewise.
	* hir/tree/rust-hir-expr.h: Likewise.
	* hir/tree/rust-hir-item.h: Likewise.
	* hir/tree/rust-hir-path.h: Likewise.
	* hir/tree/rust-hir-pattern.h: Likewise.
	* hir/tree/rust-hir-stmt.h: Likewise.
	* hir/tree/rust-hir-type.h: Likewise.
	* hir/tree/rust-hir.h: Likewise.
	* typecheck/rust-autoderef.cc: Likewise.
	* typecheck/rust-hir-dot-operator.cc (MethodResolver::select):
	Likewise.
	* typecheck/rust-hir-inherent-impl-overlap.h: Likewise.
	* typecheck/rust-hir-path-probe.cc (PathProbeType::process_impl_item_candidate):
	Likewise.
	(PathProbeImplTrait::process_trait_impl_items_for_candidates): Likewise.
	* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_trait):
	Likewise.
	(TraitItemReference::resolve_item): Likewise.
	* typecheck/rust-hir-type-check-base.cc: Likewise.
	* typecheck/rust-hir-type-check-base.h: Likewise.
	* typecheck/rust-hir-type-check-enumitem.cc (TypeCheckEnumItem::Resolve):
	Likewise.
	(TypeCheckEnumItem::visit): Likewise.
	* typecheck/rust-hir-type-check-enumitem.h: Likewise.
	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::Resolve):
	Likewise.
	(TypeCheckExpr::visit): Likewise.
	(TypeCheckExpr::resolve_fn_trait_call): Likewise.
	* typecheck/rust-hir-type-check-expr.h: Likewise.
	* typecheck/rust-hir-type-check-implitem.cc (TypeCheckTopLevelExternItem::Resolve):
	Likewise.
	(TypeCheckTopLevelExternItem::visit): Likewise.
	(TypeCheckImplItem::visit): Likewise.
	(TypeCheckImplItemWithTrait::visit): Likewise.
	* typecheck/rust-hir-type-check-implitem.h: Likewise.
	* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Likewise.
	(TypeCheckItem::resolve_impl_item): Likewise.
	(TypeCheckItem::resolve_impl_block_substitutions): Likewise.
	(TypeCheckItem::resolve_impl_block_self): Likewise.
	* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): Likewise.
	(TypeCheckExpr::resolve_segments): Likewise.
	* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::Resolve):
	Likewise.
	(TypeCheckPattern::visit): Likewise.
	(ClosureParamInfer::Resolve): Likewise.
	(ClosureParamInfer::visit): Likewise.
	* typecheck/rust-hir-type-check-pattern.h: Likewise.
	* typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::Resolve):
	Likewise.
	(TypeCheckStmt::visit): Likewise.
	* typecheck/rust-hir-type-check-stmt.h: Likewise.
	* typecheck/rust-hir-type-check-struct-field.h: Likewise.
	* typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::TypeCheckStructExpr):
	Likewise.
	(TypeCheckStructExpr::Resolve): Likewise.
	(TypeCheckStructExpr::resolve): Likewise.
	(TypeCheckStructExpr::visit): Likewise.
	* typecheck/rust-hir-type-check-type.cc (TypeCheckResolveGenericArguments::resolve):
	Likewise.
	(TypeCheckType::Resolve): Likewise.
	(TypeCheckType::visit): Likewise.
	(TypeCheckType::resolve_root_path): Likewise.
	(TypeResolveGenericParam::Resolve): Likewise.
	(TypeResolveGenericParam::visit): Likewise.
	(ResolveWhereClauseItem::visit): Likewise.
	* typecheck/rust-hir-type-check-type.h: Likewise.
	* typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn):
	Likewise.
	* typecheck/rust-hir-type-check.h: Likewise.
	* typecheck/rust-type-util.cc (query_type): Likewise.
	* typecheck/rust-typecheck-context.cc (TypeCheckContextItem::TypeCheckContextItem):
	Likewise.
	* typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::scan): Likewise.
	(TypeCheckBase::get_predicate_from_bound): Likewise.
	* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): Likewise.
	(TypeCheckMethodCallExpr::go): Likewise.
	(TypeCheckMethodCallExpr::check): Likewise.
	* typecheck/rust-tyty-subst.cc: Likewise.
	* typecheck/rust-tyty.cc (BaseType::monomorphized_clone): Likewise.
	(VariantDef::VariantDef): Remove copy constructor.
	(VariantDef::operator=): Change to move operator.
	(VariantDef::get_discriminant): Replace return type to a reference
	instead of a reference to a unique pointer.
	(VariantDef::clone): Change to references.
	(VariantDef::monomorphized_clone): Likewise.
	(FnType::as_string): Likewise.
	(FnType::clone): Likewise.
	* typecheck/rust-tyty.h: Likewise.
	* util/rust-hir-map.cc (Mappings::insert_hir_impl_block): Likewise.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
  • Loading branch information
P-E-P committed Feb 27, 2024
1 parent c4a39be commit bd0cd09
Show file tree
Hide file tree
Showing 75 changed files with 1,267 additions and 1,360 deletions.
35 changes: 18 additions & 17 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,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 @@ -702,18 +703,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 &tyty_param = fntype->param_at (i++);
auto param_tyty = tyty_param.second;
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 @@ -761,14 +762,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 @@ -782,14 +783,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 @@ -807,17 +808,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 @@ -135,7 +135,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

0 comments on commit bd0cd09

Please sign in to comment.