Skip to content

Commit 177660e

Browse files
braw-leeCohenArthur
authored andcommitted
Simplify construction of BIR::Statement
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 <[email protected]>
1 parent 65bf72f commit 177660e

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ class AbstractBuilder
239239
protected: // Helpers to add BIR statements
240240
void push_assignment (PlaceId lhs, AbstractExpr *rhs, location_t location)
241241
{
242-
ctx.get_current_bb ().statements.emplace_back (lhs, rhs, location);
242+
ctx.get_current_bb ().statements.push_back (
243+
Statement::make_assignment (lhs, rhs, location));
243244
translated = lhs;
244245
}
245246

@@ -266,47 +267,46 @@ class AbstractBuilder
266267
std::initializer_list<BasicBlockId> destinations = {})
267268
{
268269
auto copy = move_place (switch_val, location);
269-
ctx.get_current_bb ().statements.emplace_back (Statement::Kind::SWITCH,
270-
copy);
270+
ctx.get_current_bb ().statements.push_back (Statement::make_switch (copy));
271271
ctx.get_current_bb ().successors.insert (
272272
ctx.get_current_bb ().successors.end (), destinations);
273273
}
274274

275275
void push_goto (BasicBlockId bb)
276276
{
277-
ctx.get_current_bb ().statements.emplace_back (Statement::Kind::GOTO);
277+
ctx.get_current_bb ().statements.push_back (Statement::make_goto ());
278278
if (bb != INVALID_BB) // INVALID_BB means the goto will be resolved later.
279279
ctx.get_current_bb ().successors.push_back (bb);
280280
}
281281

282282
void push_storage_live (PlaceId place)
283283
{
284-
ctx.get_current_bb ().statements.emplace_back (
285-
Statement::Kind::STORAGE_LIVE, place);
284+
ctx.get_current_bb ().statements.push_back (
285+
Statement::make_storage_live (place));
286286
}
287287

288288
void push_storage_dead (PlaceId place)
289289
{
290-
ctx.get_current_bb ().statements.emplace_back (
291-
Statement::Kind::STORAGE_DEAD, place);
290+
ctx.get_current_bb ().statements.push_back (
291+
Statement::make_storage_dead (place));
292292
}
293293

294294
void push_user_type_ascription (PlaceId place, TyTy::BaseType *ty)
295295
{
296-
ctx.get_current_bb ().statements.emplace_back (
297-
Statement::Kind::USER_TYPE_ASCRIPTION, place, ty);
296+
ctx.get_current_bb ().statements.push_back (
297+
Statement::make_user_type_ascription (place, ty));
298298
}
299299

300300
void push_fake_read (PlaceId place)
301301
{
302-
ctx.get_current_bb ().statements.emplace_back (Statement::Kind::FAKE_READ,
303-
place);
302+
ctx.get_current_bb ().statements.push_back (
303+
Statement::make_fake_read (place));
304304
}
305305

306306
void push_return (location_t location)
307307
{
308-
ctx.get_current_bb ().statements.emplace_back (Statement::Kind::RETURN,
309-
INVALID_PLACE, location);
308+
ctx.get_current_bb ().statements.push_back (
309+
Statement::make_return (location));
310310
}
311311

312312
PlaceId borrow_place (PlaceId place_id, TyTy::BaseType *ty,

gcc/rust/checks/errors/borrowck/rust-bir.h

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,44 @@ class Statement
8383
location_t location;
8484

8585
public:
86-
Statement (PlaceId lhs, AbstractExpr *rhs, location_t location)
87-
: kind (Kind::ASSIGNMENT), place (lhs), expr (rhs), location (location)
88-
{}
89-
90-
explicit Statement (Kind kind, PlaceId place = INVALID_PLACE,
91-
location_t location = UNKNOWN_LOCATION)
92-
: kind (kind), place (place), location (location)
93-
{}
86+
static Statement make_assignment (PlaceId place, AbstractExpr *rhs,
87+
location_t location)
88+
{
89+
return Statement (Kind::ASSIGNMENT, place, rhs, nullptr, location);
90+
}
91+
static Statement make_switch (PlaceId place)
92+
{
93+
return Statement (Kind::SWITCH, place);
94+
}
95+
static Statement make_return (location_t location)
96+
{
97+
return Statement (Kind::RETURN, INVALID_PLACE, nullptr, nullptr, location);
98+
}
99+
static Statement make_goto () { return Statement (Kind::GOTO); }
100+
static Statement make_storage_dead (PlaceId place)
101+
{
102+
return Statement (Kind::STORAGE_DEAD, place);
103+
}
104+
static Statement make_storage_live (PlaceId place)
105+
{
106+
return Statement (Kind::STORAGE_LIVE, place);
107+
}
108+
static Statement make_user_type_ascription (PlaceId place,
109+
TyTy::BaseType *type)
110+
{
111+
return Statement (Kind::USER_TYPE_ASCRIPTION, place, nullptr, type);
112+
}
113+
static Statement make_fake_read (PlaceId place)
114+
{
115+
return Statement (Kind::FAKE_READ, place);
116+
}
94117

95-
explicit Statement (Kind kind, PlaceId place, TyTy::BaseType *type,
96-
location_t location = UNKNOWN_LOCATION)
97-
: kind (kind), place (place), type (type), location (location)
118+
private:
119+
// compelete constructor, used by make_* functions
120+
Statement (Kind kind, PlaceId place = INVALID_PLACE,
121+
AbstractExpr *rhs = nullptr, TyTy::BaseType *type = nullptr,
122+
location_t location = UNKNOWN_LOCATION)
123+
: kind (kind), place (place), expr (rhs), type (type), location (location)
98124
{}
99125

100126
public:

0 commit comments

Comments
 (0)