CBMC
irep.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_UTIL_IREP_H
11 #define CPROVER_UTIL_IREP_H
12 
13 #include <string>
14 #include <vector>
15 
16 #include "invariant.h"
17 #include "irep_ids.h"
18 
19 #define SHARING
20 #ifndef HASH_CODE
21 # define HASH_CODE 1
22 #endif
23 // use forward_list by default, unless _GLIBCXX_DEBUG is set as the debug
24 // overhead is noticeably higher with the regression test suite taking four
25 // times as long.
26 #if !defined(NAMED_SUB_IS_FORWARD_LIST) && !defined(_GLIBCXX_DEBUG)
27 # define NAMED_SUB_IS_FORWARD_LIST 1
28 #endif
29 
30 #if NAMED_SUB_IS_FORWARD_LIST
31 # include "forward_list_as_map.h"
32 #else
33 #include <map>
34 #endif
35 
36 #ifdef USE_DSTRING
38 // NOLINTNEXTLINE(readability/identifiers)
40 #else
41 #include "string_hash.h"
42 typedef std::string irep_idt;
43 // NOLINTNEXTLINE(readability/identifiers)
45 #endif
46 
47 inline const std::string &id2string(const irep_idt &d)
48 {
49  #ifdef USE_DSTRING
50  return as_string(d);
51  #else
52  return d;
53  #endif
54 }
55 
56 #ifdef IREP_DEBUG
57 #include <iostream>
58 #endif
59 
60 class irept;
61 const irept &get_nil_irep();
62 
66 template <bool enabled>
68 {
69 };
70 
71 template <>
72 struct ref_count_ift<true>
73 {
74  unsigned ref_count = 1;
75 };
76 
96 template <typename treet, typename named_subtreest, bool sharing = true>
97 class tree_nodet : public ref_count_ift<sharing>
98 {
99 public:
100  // These are not stable.
101  typedef std::vector<treet> subt;
102 
103  // named_subt has to provide stable references; we can
104  // use std::forward_list or std::vector< unique_ptr<T> > to save
105  // memory and increase efficiency.
106  using named_subt = named_subtreest;
107 
108  friend treet;
109 
112 
115 
116 #if HASH_CODE
117  mutable std::size_t hash_code = 0;
118 #endif
119 
120  void clear()
121  {
122  data.clear();
123  sub.clear();
124  named_sub.clear();
125 #if HASH_CODE
126  hash_code = 0;
127 #endif
128  }
129 
130  void swap(tree_nodet &d)
131  {
132  d.data.swap(data);
133  d.sub.swap(sub);
134  d.named_sub.swap(named_sub);
135 #if HASH_CODE
136  std::swap(d.hash_code, hash_code);
137 #endif
138  }
139 
140  tree_nodet() = default;
141 
142  explicit tree_nodet(irep_idt _data) : data(std::move(_data))
143  {
144  }
145 
146  tree_nodet(irep_idt _data, named_subt _named_sub, subt _sub)
147  : data(std::move(_data)),
148  named_sub(std::move(_named_sub)),
149  sub(std::move(_sub))
150  {
151  }
152 };
153 
155 template <typename derivedt, typename named_subtreest>
157 {
158 public:
160  using subt = typename dt::subt;
161  using named_subt = typename dt::named_subt;
162 
165 
166  explicit sharing_treet(irep_idt _id) : data(new dt(std::move(_id)))
167  {
168  }
169 
170  sharing_treet(irep_idt _id, named_subt _named_sub, subt _sub)
171  : data(new dt(std::move(_id), std::move(_named_sub), std::move(_sub)))
172  {
173  }
174 
175  // constructor for blank irep
177  {
178  }
179 
180  // copy constructor
181  sharing_treet(const sharing_treet &irep) : data(irep.data)
182  {
183  if(data!=&empty_d)
184  {
185  PRECONDITION(data->ref_count != 0);
186  data->ref_count++;
187 #ifdef IREP_DEBUG
188  std::cout << "COPY " << data << " " << data->ref_count << '\n';
189 #endif
190  }
191  }
192 
193  // Copy from rvalue reference.
194  // Note that this does avoid a branch compared to the
195  // standard copy constructor above.
197  {
198 #ifdef IREP_DEBUG
199  std::cout << "COPY MOVE\n";
200 #endif
201  irep.data=&empty_d;
202  }
203 
205  {
206 #ifdef IREP_DEBUG
207  std::cout << "ASSIGN\n";
208 #endif
209 
210  // Ordering is very important here!
211  // Consider self-assignment, which may destroy 'irep'
212  dt *irep_data=irep.data;
213  if(irep_data!=&empty_d)
214  irep_data->ref_count++;
215 
216  remove_ref(data); // this may kill 'irep'
217  data=irep_data;
218 
219  return *this;
220  }
221 
222  // Note that the move assignment operator does avoid
223  // three branches compared to standard operator above.
225  {
226 #ifdef IREP_DEBUG
227  std::cout << "ASSIGN MOVE\n";
228 #endif
229  // we simply swap two pointers
230  std::swap(data, irep.data);
231  return *this;
232  }
233 
235  {
236  remove_ref(data);
237  }
238 
239 protected:
241  static dt empty_d;
242 
243  static void remove_ref(dt *old_data);
244  static void nonrecursive_destructor(dt *old_data);
245  void detach();
246 
247 public:
248  const dt &read() const
249  {
250  return *data;
251  }
252 
254  {
255  detach();
256 #if HASH_CODE
257  data->hash_code = 0;
258 #endif
259  return *data;
260  }
261 };
262 
263 // Static field initialization
264 template <typename derivedt, typename named_subtreest>
267 
269 template <typename derivedt, typename named_subtreest>
271 {
272 public:
274  using subt = typename dt::subt;
275  using named_subt = typename dt::named_subt;
276 
279 
280  explicit non_sharing_treet(irep_idt _id) : data(std::move(_id))
281  {
282  }
283 
284  non_sharing_treet(irep_idt _id, named_subt _named_sub, subt _sub)
285  : data(std::move(_id), std::move(_named_sub), std::move(_sub))
286  {
287  }
288 
289  non_sharing_treet() = default;
290 
291  const dt &read() const
292  {
293  return data;
294  }
295 
297  {
298 #if HASH_CODE
299  data.hash_code = 0;
300 #endif
301  return data;
302  }
303 
304 protected:
306 };
307 
359 class irept
360 #ifdef SHARING
361  : public sharing_treet<
362  irept,
363 #else
364  : public non_sharing_treet<
365  irept,
366 #endif
367 #if NAMED_SUB_IS_FORWARD_LIST
368  forward_list_as_mapt<irep_idt, irept>>
369 #else
370  std::map<irep_idt, irept>>
371 #endif
372 {
373 public:
375 
376  bool is_nil() const
377  {
378  return id() == ID_nil;
379  }
380  bool is_not_nil() const
381  {
382  return id() != ID_nil;
383  }
384 
385  explicit irept(const irep_idt &_id) : baset(_id)
386  {
387  }
388 
389  irept(const irep_idt &_id, const named_subt &_named_sub, const subt &_sub)
390  : baset(_id, _named_sub, _sub)
391  {
392  }
393 
394  irept() = default;
395 
396  const irep_idt &id() const
397  { return read().data; }
398 
399  const std::string &id_string() const
400  { return id2string(read().data); }
401 
402  void id(const irep_idt &_data)
403  { write().data=_data; }
404 
405  const irept &find(const irep_idt &name) const;
406  irept &add(const irep_idt &name);
407  irept &add(const irep_idt &name, irept irep);
408 
409  const std::string &get_string(const irep_idt &name) const
410  {
411  return id2string(get(name));
412  }
413 
414  const irep_idt &get(const irep_idt &name) const;
415  bool get_bool(const irep_idt &name) const;
416  signed int get_int(const irep_idt &name) const;
417  std::size_t get_size_t(const irep_idt &name) const;
418  long long get_long_long(const irep_idt &name) const;
419 
420  void set(const irep_idt &name, const irep_idt &value)
421  {
422  add(name, irept(value));
423  }
424  void set(const irep_idt &name, irept irep)
425  {
426  add(name, std::move(irep));
427  }
428  void set(const irep_idt &name, const long long value);
429  void set_size_t(const irep_idt &name, const std::size_t value);
430 
431  void remove(const irep_idt &name);
432  void move_to_sub(irept &irep);
433  void move_to_named_sub(const irep_idt &name, irept &irep);
434 
435  bool operator==(const irept &other) const;
436 
437  bool operator!=(const irept &other) const
438  {
439  return !(*this==other);
440  }
441 
442  void swap(irept &irep)
443  {
444  std::swap(irep.data, data);
445  }
446 
447  bool operator<(const irept &other) const;
448  bool ordering(const irept &other) const;
449 
450  int compare(const irept &i) const;
451 
452  void clear() { *this=irept(); }
453 
454  void make_nil() { *this=get_nil_irep(); }
455 
456  subt &get_sub() { return write().sub; } // DANGEROUS
457  const subt &get_sub() const { return read().sub; }
458  named_subt &get_named_sub() { return write().named_sub; } // DANGEROUS
459  const named_subt &get_named_sub() const { return read().named_sub; }
460 
461  std::size_t hash() const;
462  std::size_t full_hash() const;
463 
464  bool full_eq(const irept &other) const;
465 
466  std::string pretty(unsigned indent=0, unsigned max_indent=0) const;
467 
468  static bool is_comment(const irep_idt &name)
469  { return !name.empty() && name[0]=='#'; }
470 
472  static std::size_t number_of_non_comments(const named_subt &);
473 };
474 
475 // NOLINTNEXTLINE(readability/identifiers)
476 struct irep_hash
477 {
478  std::size_t operator()(const irept &irep) const
479  {
480  return irep.hash();
481  }
482 };
483 
484 // NOLINTNEXTLINE(readability/identifiers)
486 {
487  std::size_t operator()(const irept &irep) const
488  {
489  return irep.full_hash();
490  }
491 };
492 
493 // NOLINTNEXTLINE(readability/identifiers)
495 {
496  bool operator()(const irept &i1, const irept &i2) const
497  {
498  return i1.full_eq(i2);
499  }
500 };
501 
503 {
504  const irept &irep;
505  explicit irep_pretty_diagnosticst(const irept &irep);
506 };
507 
508 template <>
510 {
511  static std::string diagnostics_as_string(const irep_pretty_diagnosticst &irep)
512  {
513  return irep.irep.pretty();
514  }
515 };
516 
517 template <typename derivedt, typename named_subtreest>
519 {
520 #ifdef IREP_DEBUG
521  std::cout << "DETACH1: " << data << '\n';
522 #endif
523 
524  if(data == &empty_d)
525  {
526  data = new dt;
527 
528 #ifdef IREP_DEBUG
529  std::cout << "ALLOCATED " << data << '\n';
530 #endif
531  }
532  else if(data->ref_count > 1)
533  {
534  dt *old_data(data);
535  data = new dt(*old_data);
536 
537 #ifdef IREP_DEBUG
538  std::cout << "ALLOCATED " << data << '\n';
539 #endif
540 
541  data->ref_count = 1;
542  remove_ref(old_data);
543  }
544 
545  POSTCONDITION(data->ref_count == 1);
546 
547 #ifdef IREP_DEBUG
548  std::cout << "DETACH2: " << data << '\n';
549 #endif
550 }
551 
552 template <typename derivedt, typename named_subtreest>
554 {
555  if(old_data == &empty_d)
556  return;
557 
558 #if 0
559  nonrecursive_destructor(old_data);
560 #else
561 
562  PRECONDITION(old_data->ref_count != 0);
563 
564 #ifdef IREP_DEBUG
565  std::cout << "R: " << old_data << " " << old_data->ref_count << '\n';
566 #endif
567 
568  old_data->ref_count--;
569  if(old_data->ref_count == 0)
570  {
571 #ifdef IREP_DEBUG
572  std::cout << "D: " << pretty() << '\n';
573  std::cout << "DELETING " << old_data->data << " " << old_data << '\n';
574  old_data->clear();
575  std::cout << "DEALLOCATING " << old_data << "\n";
576 #endif
577 
578  // may cause recursive call
579  delete old_data;
580 
581 #ifdef IREP_DEBUG
582  std::cout << "DONE\n";
583 #endif
584  }
585 #endif
586 }
587 
590 template <typename derivedt, typename named_subtreest>
592  dt *old_data)
593 {
594  std::vector<dt *> stack(1, old_data);
595 
596  while(!stack.empty())
597  {
598  dt *d = stack.back();
599  stack.erase(--stack.end());
600  if(d == &empty_d)
601  continue;
602 
603  INVARIANT(d->ref_count != 0, "All contents of the stack must be in use");
604  d->ref_count--;
605 
606  if(d->ref_count == 0)
607  {
608  stack.reserve(
609  stack.size() + std::distance(d->named_sub.begin(), d->named_sub.end()) +
610  d->sub.size());
611 
612  for(typename named_subt::iterator it = d->named_sub.begin();
613  it != d->named_sub.end();
614  it++)
615  {
616  stack.push_back(it->second.data);
617  it->second.data = &empty_d;
618  }
619 
620  for(typename subt::iterator it = d->sub.begin(); it != d->sub.end(); it++)
621  {
622  stack.push_back(it->data);
623  it->data = &empty_d;
624  }
625 
626  // now delete, won't do recursion
627  delete d;
628  }
629  }
630 }
631 
632 #endif // CPROVER_UTIL_IREP_H
non_sharing_treet::data
dt data
Definition: irep.h:305
sharing_treet::data
dt * data
Definition: irep.h:240
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
tree_nodet::subt
std::vector< treet > subt
Definition: irep.h:101
irept::get_size_t
std::size_t get_size_t(const irep_idt &name) const
Definition: irep.cpp:68
sharing_treet::remove_ref
static void remove_ref(dt *old_data)
Definition: irep.h:553
irept::clear
void clear()
Definition: irep.h:452
irept::get_long_long
long long get_long_long(const irep_idt &name) const
Definition: irep.cpp:73
irept::move_to_sub
void move_to_sub(irept &irep)
Definition: irep.cpp:36
irept::make_nil
void make_nil()
Definition: irep.h:454
irep_full_eq::operator()
bool operator()(const irept &i1, const irept &i2) const
Definition: irep.h:496
irept::full_eq
bool full_eq(const irept &other) const
Definition: irep.cpp:205
irept::pretty
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:495
sharing_treet::~sharing_treet
~sharing_treet()
Definition: irep.h:234
sharing_treet< irept, forward_list_as_mapt< irep_idt, irept > >::tree_implementationt
sharing_treet tree_implementationt
Used to refer to this class from derived classes.
Definition: irep.h:164
irept::find
const irept & find(const irep_idt &name) const
Definition: irep.cpp:106
non_sharing_treet::write
dt & write()
Definition: irep.h:296
irept::hash
std::size_t hash() const
Definition: irep.cpp:428
tree_nodet::tree_nodet
tree_nodet()=default
irept::irept
irept(const irep_idt &_id, const named_subt &_named_sub, const subt &_sub)
Definition: irep.h:389
invariant.h
tree_nodet::swap
void swap(tree_nodet &d)
Definition: irep.h:130
forward_list_as_map.h
irept::set
void set(const irep_idt &name, irept irep)
Definition: irep.h:424
string_hash
Definition: string_hash.h:21
sharing_treet< irept, forward_list_as_mapt< irep_idt, irept > >::named_subt
typename dt::named_subt named_subt
Definition: irep.h:161
tree_nodet::named_sub
named_subt named_sub
Definition: irep.h:113
tree_nodet::treet
friend treet
Definition: irep.h:108
sharing_treet::sharing_treet
sharing_treet()
Definition: irep.h:176
irep_idt
dstringt irep_idt
Definition: irep.h:37
non_sharing_treet::read
const dt & read() const
Definition: irep.h:291
sharing_treet::write
dt & write()
Definition: irep.h:253
diagnostics_helpert< irep_pretty_diagnosticst >::diagnostics_as_string
static std::string diagnostics_as_string(const irep_pretty_diagnosticst &irep)
Definition: irep.h:511
irept::irept
irept()=default
irept::get
const irep_idt & get(const irep_idt &name) const
Definition: irep.cpp:45
sharing_treet::sharing_treet
sharing_treet(const sharing_treet &irep)
Definition: irep.h:181
irept::number_of_non_comments
static std::size_t number_of_non_comments(const named_subt &)
count the number of named_sub elements that are not comments
Definition: irep.cpp:417
non_sharing_treet::subt
typename dt::subt subt
Definition: irep.h:274
irept::move_to_named_sub
void move_to_named_sub(const irep_idt &name, irept &irep)
Definition: irep.cpp:27
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:380
ref_count_ift
Used in tree_nodet for activating or not reference counting.
Definition: irep.h:67
non_sharing_treet::non_sharing_treet
non_sharing_treet(irep_idt _id)
Definition: irep.h:280
dstringt::swap
void swap(dstringt &b)
Definition: dstring.h:161
irep_full_hash::operator()
std::size_t operator()(const irept &irep) const
Definition: irep.h:487
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
irept::get_named_sub
named_subt & get_named_sub()
Definition: irep.h:458
irep_hash::operator()
std::size_t operator()(const irept &irep) const
Definition: irep.h:478
irept::id
void id(const irep_idt &_data)
Definition: irep.h:402
tree_nodet::tree_nodet
tree_nodet(irep_idt _data, named_subt _named_sub, subt _sub)
Definition: irep.h:146
irept::set
void set(const irep_idt &name, const irep_idt &value)
Definition: irep.h:420
sharing_treet::nonrecursive_destructor
static void nonrecursive_destructor(dt *old_data)
Does the same as remove_ref, but using an explicit stack instead of recursion.
Definition: irep.h:591
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
sharing_treet::read
const dt & read() const
Definition: irep.h:248
tree_nodet::clear
void clear()
Definition: irep.h:120
irept::operator!=
bool operator!=(const irept &other) const
Definition: irep.h:437
dstring_hash
Definition: dstring.h:222
string_hash.h
irept::id_string
const std::string & id_string() const
Definition: irep.h:399
get_nil_irep
const irept & get_nil_irep()
Definition: irep.cpp:20
tree_nodet::tree_nodet
tree_nodet(irep_idt _data)
Definition: irep.h:142
irept::get_string
const std::string & get_string(const irep_idt &name) const
Definition: irep.h:409
sharing_treet::operator=
sharing_treet & operator=(const sharing_treet &irep)
Definition: irep.h:204
irept::swap
void swap(irept &irep)
Definition: irep.h:442
irept::is_nil
bool is_nil() const
Definition: irep.h:376
sharing_treet::sharing_treet
sharing_treet(sharing_treet &&irep)
Definition: irep.h:196
irept::id
const irep_idt & id() const
Definition: irep.h:396
irept::get_named_sub
const named_subt & get_named_sub() const
Definition: irep.h:459
sharing_treet::operator=
sharing_treet & operator=(sharing_treet &&irep)
Definition: irep.h:224
dstringt::empty
bool empty() const
Definition: dstring.h:88
irep_hash
Definition: irep.h:476
irep_pretty_diagnosticst::irep
const irept & irep
Definition: irep.h:504
irept::set_size_t
void set_size_t(const irep_idt &name, const std::size_t value)
Definition: irep.cpp:87
sharing_treet::empty_d
static dt empty_d
Definition: irep.h:241
diagnostics_helpert
Helper to give us some diagnostic in a usable form on assertion failure.
Definition: invariant.h:298
dstringt::clear
void clear()
Definition: dstring.h:158
sharing_treet< irept, forward_list_as_mapt< irep_idt, irept > >::subt
typename dt::subt subt
Definition: irep.h:160
irep_id_hash
dstring_hash irep_id_hash
Definition: irep.h:39
irept::operator==
bool operator==(const irept &other) const
Definition: irep.cpp:146
tree_nodet::hash_code
std::size_t hash_code
Definition: irep.h:117
irep_pretty_diagnosticst
Definition: irep.h:502
irept::irept
irept(const irep_idt &_id)
Definition: irep.h:385
non_sharing_treet
Base class for tree-like data structures without sharing.
Definition: irep.h:270
sharing_treet::sharing_treet
sharing_treet(irep_idt _id, named_subt _named_sub, subt _sub)
Definition: irep.h:170
POSTCONDITION
#define POSTCONDITION(CONDITION)
Definition: invariant.h:479
irept::ordering
bool ordering(const irept &other) const
defines ordering on the internal representation
Definition: irep.cpp:245
irept::add
irept & add(const irep_idt &name)
Definition: irep.cpp:116
irep_ids.h
irept::get_sub
const subt & get_sub() const
Definition: irep.h:457
sharing_treet::sharing_treet
sharing_treet(irep_idt _id)
Definition: irep.h:166
tree_nodet
A node with data in a tree, it contains:
Definition: irep.h:97
irept::compare
int compare(const irept &i) const
defines ordering on the internal representation comments are ignored
Definition: irep.cpp:314
ref_count_ift< true >::ref_count
unsigned ref_count
Definition: irep.h:74
irept::get_int
signed int get_int(const irep_idt &name) const
Definition: irep.cpp:63
irept::get_sub
subt & get_sub()
Definition: irep.h:456
tree_nodet< derivedt, named_subtreest, false >::named_subt
named_subtreest named_subt
Definition: irep.h:106
SHARING
#define SHARING
Definition: irep.h:19
as_string
std::string as_string(const ai_verifier_statust &status)
Makes a status message string from a status.
Definition: static_verifier.cpp:23
irep_full_eq
Definition: irep.h:494
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:359
irep_pretty_diagnosticst::irep_pretty_diagnosticst
irep_pretty_diagnosticst(const irept &irep)
Definition: irep.cpp:536
irept::remove
void remove(const irep_idt &name)
Definition: irep.cpp:96
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
tree_nodet::data
irep_idt data
This irep_idt is the only place to store data in an tree node.
Definition: irep.h:111
irept::operator<
bool operator<(const irept &other) const
defines ordering on the internal representation
Definition: irep.cpp:408
irept::is_comment
static bool is_comment(const irep_idt &name)
Definition: irep.h:468
non_sharing_treet::non_sharing_treet
non_sharing_treet()=default
tree_nodet::sub
subt sub
Definition: irep.h:114
non_sharing_treet::named_subt
typename dt::named_subt named_subt
Definition: irep.h:275
irept::get_bool
bool get_bool(const irep_idt &name) const
Definition: irep.cpp:58
irep_full_hash
Definition: irep.h:485
non_sharing_treet::non_sharing_treet
non_sharing_treet(irep_idt _id, named_subt _named_sub, subt _sub)
Definition: irep.h:284
sharing_treet::detach
void detach()
Definition: irep.h:518
sharing_treet
Base class for tree-like data structures with sharing.
Definition: irep.h:156
irept::full_hash
std::size_t full_hash() const
Definition: irep.cpp:466