Skip to content

Commit

Permalink
Simplify construction of BIR::Statement
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* checks/errors/borrowck/rust-bir-builder-internal.h:
	Use `make_*` functions to create BIR::Statements.
	* checks/errors/borrowck/rust-bir.h: Make a complete constructor
	and introduce `make_*` functions to create various
	BIR::Statements.

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
  • Loading branch information
braw-lee authored and CohenArthur committed Aug 2, 2024
1 parent 65bf72f commit 177660e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
28 changes: 14 additions & 14 deletions gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ class AbstractBuilder
protected: // Helpers to add BIR statements
void push_assignment (PlaceId lhs, AbstractExpr *rhs, location_t location)
{
ctx.get_current_bb ().statements.emplace_back (lhs, rhs, location);
ctx.get_current_bb ().statements.push_back (
Statement::make_assignment (lhs, rhs, location));
translated = lhs;
}

Expand All @@ -266,47 +267,46 @@ class AbstractBuilder
std::initializer_list<BasicBlockId> destinations = {})
{
auto copy = move_place (switch_val, location);
ctx.get_current_bb ().statements.emplace_back (Statement::Kind::SWITCH,
copy);
ctx.get_current_bb ().statements.push_back (Statement::make_switch (copy));
ctx.get_current_bb ().successors.insert (
ctx.get_current_bb ().successors.end (), destinations);
}

void push_goto (BasicBlockId bb)
{
ctx.get_current_bb ().statements.emplace_back (Statement::Kind::GOTO);
ctx.get_current_bb ().statements.push_back (Statement::make_goto ());
if (bb != INVALID_BB) // INVALID_BB means the goto will be resolved later.
ctx.get_current_bb ().successors.push_back (bb);
}

void push_storage_live (PlaceId place)
{
ctx.get_current_bb ().statements.emplace_back (
Statement::Kind::STORAGE_LIVE, place);
ctx.get_current_bb ().statements.push_back (
Statement::make_storage_live (place));
}

void push_storage_dead (PlaceId place)
{
ctx.get_current_bb ().statements.emplace_back (
Statement::Kind::STORAGE_DEAD, place);
ctx.get_current_bb ().statements.push_back (
Statement::make_storage_dead (place));
}

void push_user_type_ascription (PlaceId place, TyTy::BaseType *ty)
{
ctx.get_current_bb ().statements.emplace_back (
Statement::Kind::USER_TYPE_ASCRIPTION, place, ty);
ctx.get_current_bb ().statements.push_back (
Statement::make_user_type_ascription (place, ty));
}

void push_fake_read (PlaceId place)
{
ctx.get_current_bb ().statements.emplace_back (Statement::Kind::FAKE_READ,
place);
ctx.get_current_bb ().statements.push_back (
Statement::make_fake_read (place));
}

void push_return (location_t location)
{
ctx.get_current_bb ().statements.emplace_back (Statement::Kind::RETURN,
INVALID_PLACE, location);
ctx.get_current_bb ().statements.push_back (
Statement::make_return (location));
}

PlaceId borrow_place (PlaceId place_id, TyTy::BaseType *ty,
Expand Down
48 changes: 37 additions & 11 deletions gcc/rust/checks/errors/borrowck/rust-bir.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,44 @@ class Statement
location_t location;

public:
Statement (PlaceId lhs, AbstractExpr *rhs, location_t location)
: kind (Kind::ASSIGNMENT), place (lhs), expr (rhs), location (location)
{}

explicit Statement (Kind kind, PlaceId place = INVALID_PLACE,
location_t location = UNKNOWN_LOCATION)
: kind (kind), place (place), location (location)
{}
static Statement make_assignment (PlaceId place, AbstractExpr *rhs,
location_t location)
{
return Statement (Kind::ASSIGNMENT, place, rhs, nullptr, location);
}
static Statement make_switch (PlaceId place)
{
return Statement (Kind::SWITCH, place);
}
static Statement make_return (location_t location)
{
return Statement (Kind::RETURN, INVALID_PLACE, nullptr, nullptr, location);
}
static Statement make_goto () { return Statement (Kind::GOTO); }
static Statement make_storage_dead (PlaceId place)
{
return Statement (Kind::STORAGE_DEAD, place);
}
static Statement make_storage_live (PlaceId place)
{
return Statement (Kind::STORAGE_LIVE, place);
}
static Statement make_user_type_ascription (PlaceId place,
TyTy::BaseType *type)
{
return Statement (Kind::USER_TYPE_ASCRIPTION, place, nullptr, type);
}
static Statement make_fake_read (PlaceId place)
{
return Statement (Kind::FAKE_READ, place);
}

explicit Statement (Kind kind, PlaceId place, TyTy::BaseType *type,
location_t location = UNKNOWN_LOCATION)
: kind (kind), place (place), type (type), location (location)
private:
// compelete constructor, used by make_* functions
Statement (Kind kind, PlaceId place = INVALID_PLACE,
AbstractExpr *rhs = nullptr, TyTy::BaseType *type = nullptr,
location_t location = UNKNOWN_LOCATION)
: kind (kind), place (place), expr (rhs), type (type), location (location)
{}

public:
Expand Down

0 comments on commit 177660e

Please sign in to comment.