Skip to content

Commit 1a0b770

Browse files
authored
Merge pull request #3912 from tautschnig/irept-opt-constructors
irept constructors to avoid immediate detach() calls
2 parents 05360c4 + befaff7 commit 1a0b770

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

src/util/irep.h

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ class irept
169169
bool is_nil() const { return id()==ID_nil; }
170170
bool is_not_nil() const { return id()!=ID_nil; }
171171

172+
#if !defined(USE_MOVE) || !defined(SHARING)
172173
explicit irept(const irep_idt &_id)
173174
#ifdef SHARING
174175
:data(&empty_d)
@@ -177,7 +178,27 @@ class irept
177178
id(_id);
178179
}
179180

180-
#ifdef SHARING
181+
irept(const irep_idt &_id, const named_subt &_named_sub, const subt &_sub)
182+
#ifdef SHARING
183+
: data(&empty_d)
184+
#endif
185+
{
186+
id(_id);
187+
get_named_sub() = _named_sub;
188+
get_sub() = _sub;
189+
}
190+
#else
191+
explicit irept(irep_idt _id) : data(new dt(std::move(_id)))
192+
{
193+
}
194+
195+
irept(irep_idt _id, named_subt _named_sub, subt _sub)
196+
: data(new dt(std::move(_id), std::move(_named_sub), std::move(_sub)))
197+
{
198+
}
199+
#endif
200+
201+
#ifdef SHARING
181202
// constructor for blank irep
182203
irept():data(&empty_d)
183204
{
@@ -335,9 +356,9 @@ class irept
335356
private:
336357
friend class irept;
337358

338-
#ifdef SHARING
339-
unsigned ref_count;
340-
#endif
359+
#ifdef SHARING
360+
unsigned ref_count = 1;
361+
#endif
341362

342363
/// This irep_idt is the only place to store data in an irep, other than
343364
/// the mere nesting structure
@@ -346,9 +367,9 @@ class irept
346367
named_subt named_sub;
347368
subt sub;
348369

349-
#ifdef HASH_CODE
350-
mutable std::size_t hash_code;
351-
#endif
370+
#ifdef HASH_CODE
371+
mutable std::size_t hash_code = 0;
372+
#endif
352373

353374
void clear()
354375
{
@@ -370,21 +391,20 @@ class irept
370391
#endif
371392
}
372393

373-
#ifdef SHARING
374-
dt():ref_count(1)
375-
#ifdef HASH_CODE
376-
, hash_code(0)
377-
#endif
394+
dt() = default;
395+
396+
#ifdef USE_MOVE
397+
explicit dt(irep_idt _data) : data(std::move(_data))
378398
{
379399
}
380-
#else
381-
dt()
382-
#ifdef HASH_CODE
383-
:hash_code(0)
384-
#endif
400+
401+
dt(irep_idt _data, named_subt _named_sub, subt _sub)
402+
: data(std::move(_data)),
403+
named_sub(std::move(_named_sub)),
404+
sub(std::move(_sub))
385405
{
386406
}
387-
#endif
407+
#endif
388408
};
389409

390410
protected:

unit/util/irep.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ SCENARIO("irept_memory", "[core][utils][irept]")
7272
}
7373
}
7474

75+
GIVEN("Parts of an irep")
76+
{
77+
irept irep("some_id", {{"some_member", irept("other")}}, {irept("op")});
78+
79+
THEN("It is properly initialized")
80+
{
81+
REQUIRE(irep.id() == "some_id");
82+
REQUIRE(irep.find("some_member") == irept("other"));
83+
REQUIRE(irep.get_sub().size() == 1);
84+
REQUIRE(irep.get_sub().front() == irept("op"));
85+
}
86+
}
87+
7588
GIVEN("An initialized irep")
7689
{
7790
irept irep("some_id");

0 commit comments

Comments
 (0)