Skip to content

Commit 8334ac8

Browse files
authored
Merge pull request #3502 from tautschnig/irept-rvalue-add-set
Enable perfect forwarding with irept/exprt/typet constructors
2 parents df04421 + 83ed9a5 commit 8334ac8

File tree

12 files changed

+433
-435
lines changed

12 files changed

+433
-435
lines changed

src/util/dstring.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ class dstringt final
7171
{
7272
}
7373

74+
dstringt(const dstringt &) = default;
75+
76+
/// Move constructor. There is no need and no point in actually destroying the
77+
/// source object \p other, this is effectively just a copy constructor.
78+
#ifdef __GNUC__
79+
constexpr
80+
#endif
81+
dstringt(dstringt &&other)
82+
: no(other.no)
83+
{
84+
}
85+
7486
// access
7587

7688
bool empty() const
@@ -136,6 +148,14 @@ class dstringt final
136148
dstringt &operator=(const dstringt &b)
137149
{ no=b.no; return *this; }
138150

151+
/// Move assignment. There is no need and no point in actually destroying the
152+
/// source object \p other, this is effectively just an assignment.
153+
dstringt &operator=(dstringt &&other)
154+
{
155+
no = other.no;
156+
return *this;
157+
}
158+
139159
// output
140160

141161
std::ostream &operator<<(std::ostream &out) const;

src/util/expr.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,18 @@ class exprt:public irept
5757
// constructors
5858
exprt() { }
5959
explicit exprt(const irep_idt &_id):irept(_id) { }
60-
exprt(const irep_idt &_id, const typet &_type):irept(_id)
60+
61+
exprt(irep_idt _id, typet _type)
62+
: irept(std::move(_id), {{ID_type, std::move(_type)}}, {})
63+
{
64+
}
65+
66+
exprt(irep_idt _id, typet _type, operandst &&_operands)
67+
: irept(
68+
std::move(_id),
69+
{{ID_type, std::move(_type)}},
70+
std::move((irept::subt &&) _operands))
6171
{
62-
add(ID_type, _type);
6372
}
6473

6574
/// Return the type of the expression
@@ -324,7 +333,13 @@ class expr_protectedt : public exprt
324333
{
325334
protected:
326335
// constructors
327-
expr_protectedt(const irep_idt &_id, const typet &_type) : exprt(_id, _type)
336+
expr_protectedt(irep_idt _id, typet _type)
337+
: exprt(std::move(_id), std::move(_type))
338+
{
339+
}
340+
341+
expr_protectedt(irep_idt _id, typet _type, operandst _operands)
342+
: exprt(std::move(_id), std::move(_type), std::move(_operands))
328343
{
329344
}
330345

src/util/irep.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ irept &irept::add(const irep_namet &name)
189189
#endif
190190
}
191191

192-
irept &irept::add(const irep_namet &name, const irept &irep)
192+
irept &irept::add(const irep_namet &name, irept irep)
193193
{
194194
named_subt &s = get_named_sub();
195195

@@ -202,18 +202,20 @@ irept &irept::add(const irep_namet &name, const irept &irep)
202202
named_subt::iterator before = s.before_begin();
203203
while(std::next(before) != it)
204204
++before;
205-
it = s.emplace_after(before, name, irep);
205+
it = s.emplace_after(before, name, std::move(irep));
206206
}
207207
else
208-
it->second=irep;
208+
it->second = std::move(irep);
209209

210210
return it->second;
211211
#else
212-
std::pair<named_subt::iterator, bool> entry=
213-
s.insert(std::make_pair(name, irep));
212+
std::pair<named_subt::iterator, bool> entry = s.emplace(
213+
std::piecewise_construct,
214+
std::forward_as_tuple(name),
215+
std::forward_as_tuple(irep));
214216

215217
if(!entry.second)
216-
entry.first->second=irep;
218+
entry.first->second = std::move(irep);
217219

218220
return entry.first->second;
219221
#endif

src/util/irep.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ class irept
421421

422422
const irept &find(const irep_namet &name) const;
423423
irept &add(const irep_namet &name);
424-
irept &add(const irep_namet &name, const irept &irep);
424+
irept &add(const irep_namet &name, irept irep);
425425

426426
const std::string &get_string(const irep_namet &name) const
427427
{
@@ -435,9 +435,13 @@ class irept
435435
long long get_long_long(const irep_namet &name) const;
436436

437437
void set(const irep_namet &name, const irep_idt &value)
438-
{ add(name).id(value); }
439-
void set(const irep_namet &name, const irept &irep)
440-
{ add(name, irep); }
438+
{
439+
add(name, irept(value));
440+
}
441+
void set(const irep_namet &name, irept irep)
442+
{
443+
add(name, std::move(irep));
444+
}
441445
void set(const irep_namet &name, const long long value);
442446

443447
void remove(const irep_namet &name);

src/util/ssa_expr.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ssa_exprt:public symbol_exprt
1818
public:
1919
/// Constructor
2020
/// \param expr: Expression to be converted to SSA symbol
21-
explicit ssa_exprt(const exprt &expr) : symbol_exprt(irep_idt(), expr.type())
21+
explicit ssa_exprt(const exprt &expr) : symbol_exprt(expr.type())
2222
{
2323
set(ID_C_SSA_symbol, true);
2424
add(ID_expression, expr);
@@ -42,15 +42,13 @@ class ssa_exprt:public symbol_exprt
4242
if(original_expr.id() == ID_symbol)
4343
return to_symbol_expr(original_expr).get_identifier();
4444

45-
object_descriptor_exprt ode;
46-
ode.object() = original_expr;
45+
object_descriptor_exprt ode(original_expr);
4746
return to_symbol_expr(ode.root_object()).get_identifier();
4847
}
4948

5049
const ssa_exprt get_l1_object() const
5150
{
52-
object_descriptor_exprt ode;
53-
ode.object()=get_original_expr();
51+
object_descriptor_exprt ode(get_original_expr());
5452

5553
ssa_exprt root(ode.root_object());
5654
root.set(ID_L0, get(ID_L0));

0 commit comments

Comments
 (0)