Skip to content

Commit 980ca13

Browse files
committed
irept: use single map for all named sub-nodes
1 parent f5f870b commit 980ca13

File tree

14 files changed

+89
-184
lines changed

14 files changed

+89
-184
lines changed

src/cpp/cpp_type2name.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,29 @@ static void irep2name(const irept &irep, std::string &result)
3636
if(irep.id()!="")
3737
result+=do_prefix(irep.id_string());
3838

39-
if(irep.get_named_sub().empty() &&
40-
irep.get_sub().empty() &&
41-
irep.get_comments().empty())
39+
if(irep.get_named_sub().empty() && irep.get_sub().empty())
4240
return;
4341

4442
result+='(';
4543
bool first=true;
4644

4745
forall_named_irep(it, irep.get_named_sub())
48-
{
49-
if(first)
50-
first=false;
51-
else
52-
result+=',';
46+
if(!irept::is_comment(it->first))
47+
{
48+
if(first)
49+
first = false;
50+
else
51+
result += ',';
5352

54-
result+=do_prefix(name2string(it->first));
53+
result += do_prefix(name2string(it->first));
5554

56-
result+='=';
57-
std::string tmp;
58-
irep2name(it->second, tmp);
59-
result+=tmp;
60-
}
55+
result += '=';
56+
std::string tmp;
57+
irep2name(it->second, tmp);
58+
result += tmp;
59+
}
6160

62-
forall_named_irep(it, irep.get_comments())
61+
forall_named_irep(it, irep.get_named_sub())
6362
if(it->first==ID_C_constant ||
6463
it->first==ID_C_volatile ||
6564
it->first==ID_C_restricted)

src/goto-programs/json_goto_trace.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void convert_decl(
155155
if(trace_options.json_full_lhs)
156156
{
157157
// Not language specific, still mangled, fully-qualified name of lhs
158-
json_assignment["rawLhs"] = json_irept(true).convert_from_irep(simplified);
158+
json_assignment["rawLhs"] = json_irept().convert_from_irep(simplified);
159159
}
160160
json_assignment["hidden"] = jsont::json_boolean(step.hidden);
161161
json_assignment["internal"] = jsont::json_boolean(step.internal);

src/goto-programs/show_goto_functions_json.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ json_objectt show_goto_functions_jsont::convert(
4040
const goto_functionst &goto_functions)
4141
{
4242
json_arrayt json_functions;
43-
const json_irept no_comments_irep_converter(false);
43+
const json_irept no_comments_irep_converter;
4444

4545
const auto sorted = goto_functions.sorted();
4646

src/goto-programs/show_symbol_table.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static void show_symbol_table_json_ui(
166166
result_wrapper.push_back_stream_object("symbolTable");
167167

168168
const namespacet ns(symbol_table);
169-
json_irept irep_converter(true);
169+
json_irept irep_converter;
170170

171171
for(const auto &id_and_symbol : symbol_table.symbols)
172172
{
@@ -241,7 +241,7 @@ static void show_symbol_table_brief_json_ui(
241241
result_wrapper.push_back_stream_object("symbolTable");
242242

243243
const namespacet ns(symbol_table);
244-
json_irept irep_converter(true);
244+
json_irept irep_converter;
245245

246246
for(const auto &id_and_symbol : symbol_table.symbols)
247247
{

src/json-symtab-language/json_symbol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ symbolt symbol_from_json(const jsont &in)
4747
if(!in.is_object())
4848
throw deserialization_exceptiont("symbol_from_json takes an object");
4949
symbolt result;
50-
json_irept json2irep(true);
50+
json_irept json2irep;
5151
for(const auto &kv : in.object)
5252
{
5353
if(kv.first == "type")

src/util/irep.cpp

Lines changed: 62 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ void irept::nonrecursive_destructor(dt *old_data)
154154
{
155155
stack.reserve(stack.size()+
156156
d->named_sub.size()+
157-
d->comments.size()+
158157
d->sub.size());
159158

160159
for(named_subt::iterator
@@ -166,15 +165,6 @@ void irept::nonrecursive_destructor(dt *old_data)
166165
it->second.data=&empty_d;
167166
}
168167

169-
for(named_subt::iterator
170-
it=d->comments.begin();
171-
it!=d->comments.end();
172-
it++)
173-
{
174-
stack.push_back(it->second.data);
175-
it->second.data=&empty_d;
176-
}
177-
178168
for(subt::iterator
179169
it=d->sub.begin();
180170
it!=d->sub.end();
@@ -211,10 +201,9 @@ void irept::move_to_sub(irept &irep)
211201

212202
const irep_idt &irept::get(const irep_namet &name) const
213203
{
214-
const named_subt &s=
215-
is_comment(name)?get_comments():get_named_sub();
204+
const named_subt &s = get_named_sub();
216205

217-
#ifdef SUB_IS_LIST
206+
#ifdef SUB_IS_LIST
218207
named_subt::const_iterator it=named_subt_lower_bound(s, name);
219208

220209
if(it==s.end() ||
@@ -223,15 +212,15 @@ const irep_idt &irept::get(const irep_namet &name) const
223212
static const irep_idt empty;
224213
return empty;
225214
}
226-
#else
215+
#else
227216
named_subt::const_iterator it=s.find(name);
228217

229218
if(it==s.end())
230219
{
231220
static const irep_idt empty;
232221
return empty;
233222
}
234-
#endif
223+
#endif
235224

236225
return it->second.id();
237226
}
@@ -268,64 +257,60 @@ void irept::set(const irep_namet &name, const long long value)
268257

269258
void irept::remove(const irep_namet &name)
270259
{
271-
named_subt &s=
272-
is_comment(name)?get_comments():get_named_sub();
260+
named_subt &s = get_named_sub();
273261

274-
#ifdef SUB_IS_LIST
262+
#ifdef SUB_IS_LIST
275263
named_subt::iterator it=named_subt_lower_bound(s, name);
276264

277265
if(it!=s.end() && it->first==name)
278266
s.erase(it);
279-
#else
267+
#else
280268
s.erase(name);
281-
#endif
269+
#endif
282270
}
283271

284272
const irept &irept::find(const irep_namet &name) const
285273
{
286-
const named_subt &s=
287-
is_comment(name)?get_comments():get_named_sub();
274+
const named_subt &s = get_named_sub();
288275

289-
#ifdef SUB_IS_LIST
276+
#ifdef SUB_IS_LIST
290277
named_subt::const_iterator it=named_subt_lower_bound(s, name);
291278

292279
if(it==s.end() ||
293280
it->first!=name)
294281
return get_nil_irep();
295-
#else
282+
#else
296283
named_subt::const_iterator it=s.find(name);
297284

298285
if(it==s.end())
299286
return get_nil_irep();
300-
#endif
287+
#endif
301288

302289
return it->second;
303290
}
304291

305292
irept &irept::add(const irep_namet &name)
306293
{
307-
named_subt &s=
308-
is_comment(name)?get_comments():get_named_sub();
294+
named_subt &s = get_named_sub();
309295

310-
#ifdef SUB_IS_LIST
296+
#ifdef SUB_IS_LIST
311297
named_subt::iterator it=named_subt_lower_bound(s, name);
312298

313299
if(it==s.end() ||
314300
it->first!=name)
315301
it=s.insert(it, std::make_pair(name, irept()));
316302

317303
return it->second;
318-
#else
304+
#else
319305
return s[name];
320-
#endif
306+
#endif
321307
}
322308

323309
irept &irept::add(const irep_namet &name, const irept &irep)
324310
{
325-
named_subt &s=
326-
is_comment(name)?get_comments():get_named_sub();
311+
named_subt &s = get_named_sub();
327312

328-
#ifdef SUB_IS_LIST
313+
#ifdef SUB_IS_LIST
329314
named_subt::iterator it=named_subt_lower_bound(s, name);
330315

331316
if(it==s.end() ||
@@ -335,15 +320,15 @@ irept &irept::add(const irep_namet &name, const irept &irep)
335320
it->second=irep;
336321

337322
return it->second;
338-
#else
323+
#else
339324
std::pair<named_subt::iterator, bool> entry=
340325
s.insert(std::make_pair(name, irep));
341326

342327
if(!entry.second)
343328
entry.first->second=irep;
344329

345330
return entry.first->second;
346-
#endif
331+
#endif
347332
}
348333

349334
#ifdef IREP_HASH_STATS
@@ -361,18 +346,52 @@ bool irept::operator==(const irept &other) const
361346
return true;
362347
#endif
363348

364-
if(id()!=other.id() ||
365-
get_sub()!=other.get_sub() || // recursive call
366-
get_named_sub()!=other.get_named_sub()) // recursive call
349+
if(id() != other.id() || get_sub() != other.get_sub()) // recursive call
367350
{
368351
#ifdef IREP_HASH_STATS
369352
++irep_cmp_ne_cnt;
370353
#endif
371354
return false;
372355
}
373356

374-
// comments are NOT checked
357+
const auto &this_named_sub = get_named_sub();
358+
const auto &other_named_sub = other.get_named_sub();
359+
360+
// walk in sync, ignoring comments, until end of both maps
361+
named_subt::const_iterator this_it = this_named_sub.begin();
362+
named_subt::const_iterator other_it = other_named_sub.begin();
363+
364+
while(this_it != this_named_sub.end() || other_it != other_named_sub.end())
365+
{
366+
if(this_it != this_named_sub.end() && is_comment(this_it->first))
367+
{
368+
this_it++;
369+
continue;
370+
}
371+
372+
if(other_it != other_named_sub.end() && is_comment(other_it->first))
373+
{
374+
other_it++;
375+
continue;
376+
}
377+
378+
if(
379+
this_it == this_named_sub.end() || // reached end of 'this'
380+
other_it == other_named_sub.end() || // reached the end of 'other'
381+
this_it->first != other_it->first ||
382+
this_it->second != other_it->second) // recursive call
383+
{
384+
#ifdef IREP_HASH_STATS
385+
++irep_cmp_ne_cnt;
386+
#endif
387+
return false;
388+
}
375389

390+
this_it++;
391+
other_it++;
392+
}
393+
394+
// reached the end of both
376395
return true;
377396
}
378397

@@ -390,12 +409,10 @@ bool irept::full_eq(const irept &other) const
390409
const irept::subt &i2_sub=other.get_sub();
391410
const irept::named_subt &i1_named_sub=get_named_sub();
392411
const irept::named_subt &i2_named_sub=other.get_named_sub();
393-
const irept::named_subt &i1_comments=get_comments();
394-
const irept::named_subt &i2_comments=other.get_comments();
395412

396-
if(i1_sub.size()!=i2_sub.size() ||
397-
i1_named_sub.size()!=i2_named_sub.size() ||
398-
i1_comments.size()!=i2_comments.size())
413+
if(
414+
i1_sub.size() != i2_sub.size() ||
415+
i1_named_sub.size() != i2_named_sub.size())
399416
return false;
400417

401418
for(std::size_t i=0; i<i1_sub.size(); i++)
@@ -412,16 +429,6 @@ bool irept::full_eq(const irept &other) const
412429
return false;
413430
}
414431

415-
{
416-
irept::named_subt::const_iterator i1_it=i1_comments.begin();
417-
irept::named_subt::const_iterator i2_it=i2_comments.begin();
418-
419-
for(; i1_it!=i1_comments.end(); i1_it++, i2_it++)
420-
if(i1_it->first!=i2_it->first ||
421-
!i1_it->second.full_eq(i2_it->second))
422-
return false;
423-
}
424-
425432
return true;
426433
}
427434

@@ -606,7 +613,6 @@ std::size_t irept::full_hash() const
606613
{
607614
const irept::subt &sub=get_sub();
608615
const irept::named_subt &named_sub=get_named_sub();
609-
const irept::named_subt &comments=get_comments();
610616

611617
std::size_t result=hash_string(id());
612618

@@ -618,15 +624,7 @@ std::size_t irept::full_hash() const
618624
result=hash_combine(result, it->second.full_hash());
619625
}
620626

621-
forall_named_irep(it, comments)
622-
{
623-
result=hash_combine(result, hash_string(it->first));
624-
result=hash_combine(result, it->second.full_hash());
625-
}
626-
627-
result=hash_finalize(
628-
result,
629-
named_sub.size()+sub.size()+comments.size());
627+
result = hash_finalize(result, named_sub.size() + sub.size());
630628

631629
return result;
632630
}
@@ -662,18 +660,6 @@ std::string irept::pretty(unsigned indent, unsigned max_indent) const
662660
result+=it->second.pretty(indent+2, max_indent);
663661
}
664662

665-
forall_named_irep(it, get_comments())
666-
{
667-
result+="\n";
668-
indent_str(result, indent);
669-
670-
result+="* ";
671-
result+=id2string(it->first);
672-
result+=": ";
673-
674-
result+=it->second.pretty(indent+2, max_indent);
675-
}
676-
677663
std::size_t count=0;
678664

679665
forall_irep(it, get_sub())

0 commit comments

Comments
 (0)