Skip to content

Commit

Permalink
WIP stuff on hir refactor
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* backend/rust-compile-base.cc:
	* backend/rust-compile-expr.cc (CompileExpr::visit):
	* backend/rust-compile-intrinsic.cc (compile_fn_params):
	(transmute_handler):
	(assume_handler):
	* backend/rust-compile-type.cc (TyTyResolveCompile::visit):
	* checks/errors/borrowck/rust-bir-builder-expr-stmt.cc (ExprStmtBuilder::visit):
	* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit):
	* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit):
	* hir/tree/rust-hir-expr.h:
	* hir/tree/rust-hir-item.h:
	* hir/tree/rust-hir.h:
	* typecheck/rust-hir-dot-operator.cc (MethodResolver::Select):
	* typecheck/rust-hir-type-check-expr.cc:
	* typecheck/rust-hir-type-check-implitem.cc (TypeCheckTopLevelExternItem::visit):
	(TypeCheckImplItem::visit):
	* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit):
	* typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn):
	* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit):
	(TypeCheckMethodCallExpr::check):
	* typecheck/rust-tyty-cmp.h:
	* typecheck/rust-tyty.cc (BaseType::monomorphized_clone):
	(BaseType::is_concrete):
	(FnType::as_string):
	(FnType::is_equal):
	(FnType::clone):
	(FnType::handle_substitions):
	* typecheck/rust-tyty.h (class FnParam):
	* typecheck/rust-unify.cc (UnifyRules::expect_fndef):
	(UnifyRules::expect_fnptr):

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
  • Loading branch information
P-E-P committed Mar 18, 2024
1 parent 6116b17 commit 8765a9c
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 115 deletions.
2 changes: 1 addition & 1 deletion gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ HIRCompileBase::compile_function (
for (auto &referenced_param : function_params)
{
auto &tyty_param = fntype->param_at (i++);
auto param_tyty = tyty_param.second;
auto param_tyty = tyty_param.get_type ();
auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty);

location_t param_locus = referenced_param.get_locus ();
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ CompileExpr::visit (HIR::CallExpr &expr)

const TyTy::FnType *fn = static_cast<const TyTy::FnType *> (base);
auto &param = fn->param_at (index);
*result = param.second;
*result = param.get_type ();

return true;
};
Expand Down Expand Up @@ -1338,7 +1338,7 @@ CompileExpr::visit (HIR::MethodCallExpr &expr)
// assignments are coercion sites so lets convert the rvalue if
// necessary, offset from the already adjusted implicit self
bool ok;
TyTy::BaseType *expected = fntype->param_at (i + 1).second;
TyTy::BaseType *expected = fntype->param_at (i + 1).get_type ();

TyTy::BaseType *actual = nullptr;
ok = ctx->get_tyctx ()->lookup_type (
Expand Down
17 changes: 9 additions & 8 deletions gcc/rust/backend/rust-compile-intrinsic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,13 @@ compile_fn_params (Context *ctx, TyTy::FnType *fntype, tree fndecl,
{
for (auto &parm : fntype->get_params ())
{
auto &referenced_param = parm.first;
auto &param_tyty = parm.second;
auto &referenced_param = parm.get_pattern ();
auto param_tyty = parm.get_type ();
auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty);

location_t param_locus = referenced_param->get_locus ();
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);

compiled_param_variables->push_back (compiled_param_var);
Expand Down Expand Up @@ -496,9 +496,10 @@ transmute_handler (Context *ctx, TyTy::FnType *fntype)
rust_error_at (fntype->get_locus (),
"cannot transmute between types of different sizes, or "
"dependently-sized types");
rust_inform (fntype->get_ident ().locus, "source type: %qs (%lu bits)",
fntype->get_params ().at (0).second->as_string ().c_str (),
(unsigned long) source_size);
rust_inform (
fntype->get_ident ().locus, "source type: %qs (%lu bits)",
fntype->get_params ().at (0).get_type ()->as_string ().c_str (),
(unsigned long) source_size);
rust_inform (fntype->get_ident ().locus, "target type: %qs (%lu bits)",
fntype->get_return_type ()->as_string ().c_str (),
(unsigned long) target_size);
Expand Down Expand Up @@ -1226,7 +1227,7 @@ assume_handler (Context *ctx, TyTy::FnType *fntype)
// TODO: make sure this is actually helping the compiler optimize

rust_assert (fntype->get_params ().size () == 1);
rust_assert (fntype->param_at (0).second->get_kind ()
rust_assert (fntype->param_at (0).get_type ()->get_kind ()
== TyTy::TypeKind::BOOL);

tree lookup = NULL_TREE;
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/backend/rust-compile-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ TyTyResolveCompile::visit (const TyTy::FnType &type)

for (auto &param_pair : type.get_params ())
{
auto param_tyty = param_pair.second;
auto param_tyty = param_pair.get_type ();
auto compiled_param_type
= TyTyResolveCompile::compile (ctx, param_tyty, trait_object_mode);

auto compiled_param = Backend::typed_identifier (
param_pair.first->as_string (), compiled_param_type,
param_pair.get_pattern ().as_string (), compiled_param_type,
ctx->get_mappings ()->lookup_location (param_tyty->get_ref ()));

parameters.push_back (compiled_param);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ ExprStmtBuilder::visit (HIR::CallExpr &expr)
{
for (size_t i = 0; i < fn_type->get_params ().size (); ++i)
{
coercion_site (arguments[i], fn_type->get_params ()[i].second);
coercion_site (arguments[i], fn_type->get_params ()[i].get_type ());
}
}
else if (auto fn_ptr_type = call_type->try_as<TyTy::FnPtr> ())
Expand Down
11 changes: 6 additions & 5 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ PrivacyReporter::visit (HIR::BlockExpr &expr)
for (auto &stmt : expr.get_statements ())
stmt->accept_vis (*this);

auto &last_expr = expr.get_final_expr ();
last_expr.accept_vis (*this);
if (expr.has_final_expr ())
expr.get_final_expr ().accept_vis (*this);
}

void
Expand All @@ -499,8 +499,8 @@ PrivacyReporter::visit (HIR::ContinueExpr &)
void
PrivacyReporter::visit (HIR::BreakExpr &expr)
{
auto &break_expr = expr.get_expr ();
break_expr.accept_vis (*this);
if (expr.has_break_expr ())
expr.get_expr ().accept_vis (*this);
}

void
Expand Down Expand Up @@ -542,7 +542,8 @@ PrivacyReporter::visit (HIR::RangeToInclExpr &)
void
PrivacyReporter::visit (HIR::ReturnExpr &expr)
{
expr.get_expr ().accept_vis (*this);
if (expr.has_expr ())
expr.get_expr ().accept_vis (*this);
}

void
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/checks/errors/rust-unsafe-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ UnsafeChecker::visit (MethodCallExpr &expr)
context.lookup_type (expr.get_method_name ().get_mappings ().get_hirid (),
&method_type);

auto fn = *static_cast<TyTy::FnType *> (method_type);
auto &fn = static_cast<TyTy::FnType &> (*method_type);
auto method = mappings.lookup_hir_implitem (fn.get_ref (), nullptr);

if (!unsafe_context.is_in_context () && method)
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/hir/tree/rust-hir-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2225,6 +2225,7 @@ class BlockExpr : public ExprWithBlock, public WithInnerAttrs

bool is_final_stmt (Stmt *stmt) { return statements.back ().get () == stmt; }

bool has_final_expr () { return expr != nullptr; }
Expr &get_final_expr () { return *expr; }

std::vector<std::unique_ptr<Stmt> > &get_statements () { return statements; }
Expand Down Expand Up @@ -2772,6 +2773,7 @@ class ReturnExpr : public ExprWithoutBlock
void accept_vis (HIRFullVisitor &vis) override;
void accept_vis (HIRExpressionVisitor &vis) override;

bool has_expr () { return return_expr != nullptr; }
Expr &get_expr () { return *return_expr; }

ExprType get_expression_type () const override final
Expand Down
2 changes: 0 additions & 2 deletions gcc/rust/hir/tree/rust-hir-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,6 @@ struct FunctionParam

Pattern &get_param_name () { return *param_name; }

std::unique_ptr<Pattern> take_param_name () { return std::move (param_name); }

Type &get_type () { return *type; }

const Analysis::NodeMapping &get_mappings () const { return mappings; }
Expand Down
5 changes: 4 additions & 1 deletion gcc/rust/hir/tree/rust-hir.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,10 @@ class Type : public Node, public FullVisitable

virtual void accept_vis (HIRTypeVisitor &vis) = 0;

virtual Analysis::NodeMapping get_mappings () const { return mappings; }
virtual const Analysis::NodeMapping &get_mappings () const
{
return mappings;
}
virtual location_t get_locus () const { return locus; }

protected:
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/typecheck/rust-hir-dot-operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ MethodResolver::Select (std::set<MethodCandidate> &candidates,
for (size_t i = 0; i < arguments.size (); i++)
{
TyTy::BaseType *arg = arguments.at (i);
TyTy::BaseType *param = fn.get_params ().at (i + 1).second;
TyTy::BaseType *param = fn.get_params ().at (i + 1).get_type ();
TyTy::BaseType *coerced
= try_coercion (0, TyTy::TyWithLocation (param),
TyTy::TyWithLocation (arg), UNDEF_LOCATION);
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,7 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type,

// typecheck the self
unify_site (expr.get_mappings ().get_hirid (),
TyTy::TyWithLocation (fnparam.second),
TyTy::TyWithLocation (fnparam.get_type ()),
TyTy::TyWithLocation (adjusted_self), expr.get_locus ());
if (rhs == nullptr)
{
Expand All @@ -1800,7 +1800,7 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type,
rust_assert (type->num_params () == 2);
auto &fnparam = type->param_at (1);
unify_site (expr.get_mappings ().get_hirid (),
TyTy::TyWithLocation (fnparam.second),
TyTy::TyWithLocation (fnparam.get_type ()),
TyTy::TyWithLocation (rhs), expr.get_locus ());
}

Expand Down
28 changes: 11 additions & 17 deletions gcc/rust/typecheck/rust-hir-type-check-implitem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function)
function.get_return_type ().get_mappings ().get_hirid ());
}

std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params;
std::unique_ptr<HIR::IdentifierPattern> param_pattern = nullptr;
std::vector<TyTy::FnParam> params;
for (auto &param : function.get_function_params ())
{
// get the name as well required for later on
Expand All @@ -140,14 +139,12 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function)
mappings->get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

param_pattern = Rust::make_unique<HIR::IdentifierPattern> (
auto param_pattern = Rust::make_unique<HIR::IdentifierPattern> (
HIR::IdentifierPattern (mapping, param.get_param_name (),
UNDEF_LOCATION, false, Mutability::Imm,
std::unique_ptr<HIR::Pattern> (nullptr)));

params.push_back (
std::pair<HIR::Pattern *, TyTy::BaseType *> (param_pattern.get (),
param_tyty));
params.push_back (TyTy::FnParam (std::move (param_pattern), param_tyty));

context->insert_type (param.get_mappings (), param_tyty);

Expand Down Expand Up @@ -373,8 +370,7 @@ TypeCheckImplItem::visit (HIR::Function &function)
function.get_return_type ().get_mappings ().get_hirid ());
}

std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params;
std::unique_ptr<HIR::IdentifierPattern> self_pattern = nullptr;
std::vector<TyTy::FnParam> params;
if (function.is_method ())
{
// these are implicit mappings and not used
Expand All @@ -388,10 +384,11 @@ TypeCheckImplItem::visit (HIR::Function &function)
// reuse the HIR identifier pattern which requires it
HIR::SelfParam &self_param = function.get_self_param ();
// FIXME: which location should be used for Rust::Identifier for `self`?
self_pattern = Rust::make_unique<HIR::IdentifierPattern> (
HIR::IdentifierPattern (mapping, {"self"}, self_param.get_locus (),
self_param.is_ref (), self_param.get_mut (),
std::unique_ptr<HIR::Pattern> (nullptr)));
std::unique_ptr<HIR::Pattern> self_pattern
= Rust::make_unique<HIR::IdentifierPattern> (
HIR::IdentifierPattern (mapping, {"self"}, self_param.get_locus (),
self_param.is_ref (), self_param.get_mut (),
std::unique_ptr<HIR::Pattern> (nullptr)));

// might have a specified type
TyTy::BaseType *self_type = nullptr;
Expand Down Expand Up @@ -448,9 +445,7 @@ TypeCheckImplItem::visit (HIR::Function &function)
}

context->insert_type (self_param.get_mappings (), self_type);
params.push_back (
std::pair<HIR::Pattern *, TyTy::BaseType *> (self_pattern.get (),
self_type));
params.push_back (TyTy::FnParam (std::move (self_pattern), self_type));
}

for (auto &param : function.get_function_params ())
Expand All @@ -462,8 +457,7 @@ TypeCheckImplItem::visit (HIR::Function &function)
TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);

params.push_back (
std::pair<HIR::Pattern *, TyTy::BaseType *> (&param.get_param_name (),
param_tyty));
TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty));
}

const CanonicalPath *canonical_path = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions gcc/rust/typecheck/rust-hir-type-check-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -487,15 +487,15 @@ TypeCheckItem::visit (HIR::Function &function)
function.get_return_type ().get_mappings ().get_hirid ());
}

std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *>> params;
std::vector<TyTy::FnParam> params;
for (auto &param : function.get_function_params ())
{
// get the name as well required for later on
auto param_tyty = TypeCheckType::Resolve (param.get_type ());
params.emplace_back (&param.get_param_name (), param_tyty);

context->insert_type (param.get_mappings (), param_tyty);
TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);
params.push_back (
TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty));
}

const CanonicalPath *canonical_path = nullptr;
Expand Down
18 changes: 7 additions & 11 deletions gcc/rust/typecheck/rust-hir-type-check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,8 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const
function.get_return_type ().get_mappings ().get_hirid ());
}

std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params;
std::vector<TyTy::FnParam> params;

std::unique_ptr<HIR::IdentifierPattern> self_pattern = nullptr;
if (function.is_method ())
{
// these are implicit mappings and not used
Expand All @@ -234,7 +233,7 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const
// for compilation to know parameter names. The types are ignored
// but we reuse the HIR identifier pattern which requires it
HIR::SelfParam &self_param = function.get_self ();
self_pattern
std::unique_ptr<HIR::Pattern> self_pattern
= Rust::make_unique<HIR::IdentifierPattern> (HIR::IdentifierPattern (
mapping, {"self"}, self_param.get_locus (), self_param.is_ref (),
self_param.is_mut () ? Mutability::Mut : Mutability::Imm,
Expand Down Expand Up @@ -286,21 +285,18 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const
}

context->insert_type (self_param.get_mappings (), self_type);
params.push_back (
std::pair<HIR::Pattern *, TyTy::BaseType *> (self_pattern.get (),
self_type));
params.push_back (TyTy::FnParam (std::move (self_pattern), self_type));
}

for (auto &param : function.get_function_params ())
{
// get the name as well required for later on
auto param_tyty = TypeCheckType::Resolve (param.get_type ());
params.push_back (
std::pair<HIR::Pattern *, TyTy::BaseType *> (&param.get_param_name (),
param_tyty));

context->insert_type (param.get_mappings (), param_tyty);
TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);
// FIXME: Should we take the name ? Use a shared pointer instead ?
params.push_back (
TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty));
}

auto mappings = Analysis::Mappings::get ();
Expand All @@ -315,7 +311,7 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const
function.get_function_name ().as_string (), ident,
function.is_method () ? TyTy::FnType::FNTYPE_IS_METHOD_FLAG
: TyTy::FnType::FNTYPE_DEFAULT_FLAGS,
ABI::RUST, params, ret_type, substitutions,
ABI::RUST, std::move (params), ret_type, substitutions,
TyTy::SubstitutionArgumentMappings::empty (
context->get_lifetime_resolver ().get_num_bound_regions ()),
region_constraints);
Expand Down
20 changes: 10 additions & 10 deletions gcc/rust/typecheck/rust-tyty-call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ TypeCheckCallExpr::visit (FnType &type)
if (i < type.num_params ())
{
auto &fnparam = type.param_at (i);
auto &fn_param_pattern = fnparam.first;
BaseType *param_ty = fnparam.second;
auto &fn_param_pattern = fnparam.get_pattern ();
BaseType *param_ty = fnparam.get_type ();
location_t param_locus
= fn_param_pattern == nullptr
? mappings->lookup_location (param_ty->get_ref ())
: fn_param_pattern->get_locus ();
= fnparam.has_pattern ()
? fn_param_pattern.get_locus ()
: mappings->lookup_location (param_ty->get_ref ());

HirId coercion_side_id = argument->get_mappings ().get_hirid ();
auto resolved_argument_type
Expand Down Expand Up @@ -375,12 +375,12 @@ TypeCheckMethodCallExpr::check (FnType &type)
location_t arg_locus = argument.get_locus ();

auto &fnparam = type.param_at (i);
HIR::Pattern *fn_param_pattern = fnparam.first;
BaseType *param_ty = fnparam.second;
HIR::Pattern &fn_param_pattern = fnparam.get_pattern ();
BaseType *param_ty = fnparam.get_type ();
location_t param_locus
= fn_param_pattern == nullptr
? mappings->lookup_location (param_ty->get_ref ())
: fn_param_pattern->get_locus ();
= fnparam.has_pattern ()
? fn_param_pattern.get_locus ()
: mappings->lookup_location (param_ty->get_ref ());

auto argument_expr_tyty = argument.get_argument_type ();
HirId coercion_side_id = argument.get_mappings ().get_hirid ();
Expand Down
Loading

0 comments on commit 8765a9c

Please sign in to comment.