summaryrefslogtreecommitdiff
path: root/src/expr/attribute.h
blob: 95433688ec289b952cc4bad3f419b4116b062a6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*********************                                                        */
/** attribute.h
 ** Original author: mdeters
 ** Major contributors: none
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009, 2010  The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.
 **
 ** Node attributes.
 **
 ** Attribute structures:
 **
 ** An attribute structure looks like the following:
 **
 ** struct VarNameAttr {
 **
 **   // the value type for this attribute
 **   typedef std::string value_type;
 **
 **   // an extra hash value (to avoid same-value-type collisions)
 **   enum { hash_value = 1 };
 **
 **   // cleanup routine when the Node goes away
 **   static inline void cleanup(const std::string&) {
 **   }
 ** }
 **/

/* There are strong constraints on ordering of declarations of
 * attributes and nodes due to template use */
#include "expr/node.h"

#ifndef __CVC4__EXPR__ATTRIBUTE_H
#define __CVC4__EXPR__ATTRIBUTE_H

#include <stdint.h>

#include <string>
#include <ext/hash_map>

#include "config.h"
#include "context/context.h"
#include "expr/soft_node.h"
#include "expr/type.h"

#include "util/output.h"

namespace CVC4 {
namespace expr {

// ATTRIBUTE HASH FUNCTIONS ====================================================

struct AttrHashFcn {
  enum { LARGE_PRIME = 1 };
  std::size_t operator()(const std::pair<uint64_t, SoftNode>& p) const {
    return p.first * LARGE_PRIME + p.second.hash();
  }
};

struct AttrHashBoolFcn {
  std::size_t operator()(const SoftNode& n) const {
    return n.hash();
  }
};

// ATTRIBUTE TYPE MAPPINGS =====================================================

template <class T>
struct KindValueToTableValueMapping {
  typedef T table_value_type;
  inline static T convert(const T& t) { return t; }
  inline static T convertBack(const T& t) { return t; }
};

template <>
struct KindValueToTableValueMapping<bool> {
  typedef uint64_t table_value_type;
  inline static uint64_t convert(const bool& t) { return t; }
  inline static bool convertBack(const uint64_t& t) { return t; }
};

template <class T>
struct KindValueToTableValueMapping<T*> {
  typedef void* table_value_type;
  inline static void* convert(const T*& t) {
    return reinterpret_cast<void*>(t);
  }
  inline static T* convertBack(void*& t) {
    return reinterpret_cast<T*>(t);
  }
};

template <class T>
struct KindValueToTableValueMapping<const T*> {
  typedef void* table_value_type;
  inline static void* convert(const T* const& t) {
    return reinterpret_cast<void*>(const_cast<T*>(t));
  }
  inline static const T* convertBack(const void* const& t) {
    return reinterpret_cast<const T*>(t);
  }
};

template <class AttrKind, class T>
struct OwnTable;

template <class AttrKind, class T>
struct KindValueToTableValueMapping<OwnTable<AttrKind, T> > {
  typedef typename KindValueToTableValueMapping<T>::table_value_type table_value_type;
};

template <class AttrKind>
struct KindTableMapping {
  typedef typename AttrKind::value_type table_identifier;
};

// ATTRIBUTE HASH TABLES =======================================================

// use a TAG to indicate which table it should be in
template <class value_type>
struct AttrHash : public __gnu_cxx::hash_map<std::pair<uint64_t, SoftNode>, value_type, AttrHashFcn> {};

template <>
class AttrHash<bool> : protected __gnu_cxx::hash_map<SoftNode, uint64_t, AttrHashBoolFcn> {

  typedef __gnu_cxx::hash_map<SoftNode, uint64_t, AttrHashBoolFcn> super;

  class BitAccessor {

    uint64_t& d_word;

    unsigned d_bit;

  public:

    BitAccessor(uint64_t& word, unsigned bit) :
      d_word(word),
      d_bit(bit) {
    }

    BitAccessor& operator=(bool b) {
      if(b) {
        // set the bit
        d_word |= (1 << d_bit);
      } else {
        // clear the bit
        d_word &= ~(1 << d_bit);
      }

      return *this;
    }

    operator bool() const {
      return (d_word & (1 << d_bit)) ? true : false;
    }
  };

  class BitIterator {

    std::pair<const SoftNode, uint64_t>* d_entry;

    unsigned d_bit;

  public:

    BitIterator() :
      d_entry(NULL),
      d_bit(0) {
    }

    BitIterator(std::pair<const SoftNode, uint64_t>& entry, unsigned bit) :
      d_entry(&entry),
      d_bit(bit) {
    }

    std::pair<const SoftNode, BitAccessor> operator*() {
      return std::make_pair(d_entry->first, BitAccessor(d_entry->second, d_bit));
    }

    bool operator==(const BitIterator& b) {
      return d_entry == b.d_entry && d_bit == b.d_bit;
    }
  };

  class ConstBitIterator {

    const std::pair<const SoftNode, uint64_t>* d_entry;

    unsigned d_bit;

  public:

    ConstBitIterator() :
      d_entry(NULL),
      d_bit(0) {
    }

    ConstBitIterator(const std::pair<const SoftNode, uint64_t>& entry, unsigned bit) :
      d_entry(&entry),
      d_bit(bit) {
    }

    std::pair<const SoftNode, bool> operator*() {
      return std::make_pair(d_entry->first, (d_entry->second & (1 << d_bit)) ? true : false);
    }

    bool operator==(const ConstBitIterator& b) {
      return d_entry == b.d_entry && d_bit == b.d_bit;
    }
  };

public:

  typedef std::pair<uint64_t, SoftNode> key_type;
  typedef bool data_type;
  typedef std::pair<const key_type, data_type> value_type;

  typedef BitIterator iterator;
  typedef ConstBitIterator const_iterator;

  BitIterator find(const std::pair<uint64_t, SoftNode>& k) {
    super::iterator i = super::find(k.second);
    if(i == super::end()) {
      return BitIterator();
    }
    Debug.printf("boolattr",
                 "underlying word at 0x%p looks like 0x%016llx, bit is %u\n",
                 &(*i).second,
                 (unsigned long long)((*i).second),
                 unsigned(k.first));
    return BitIterator(*i, k.first);
  }

  BitIterator end() {
    return BitIterator();
  }

  ConstBitIterator find(const std::pair<uint64_t, SoftNode>& k) const {
    super::const_iterator i = super::find(k.second);
    if(i == super::end()) {
      return ConstBitIterator();
    }
    Debug.printf("boolattr",
                 "underlying word at 0x%p looks like 0x%016llx, bit is %u\n",
                 &(*i).second,
                 (unsigned long long)((*i).second),
                 unsigned(k.first));
    return ConstBitIterator(*i, k.first);
  }

  ConstBitIterator end() const {
    return ConstBitIterator();
  }

  BitAccessor operator[](const std::pair<uint64_t, SoftNode>& k) {
    uint64_t& word = super::operator[](k.second);
    return BitAccessor(word, k.first);
  }
};/* class AttrHash<bool> */

// ATTRIBUTE PATTERN ===========================================================

/**
 * An "attribute type" structure.
 */
template <class T, class value_t>
struct Attribute {

  /** the value type for this attribute */
  typedef value_t value_type;

  /** cleanup routine when the Node goes away */
  static inline void cleanup(const value_t&) {}

  static inline uint64_t getId() { return s_id; }
  static inline uint64_t getHashValue() { return s_hashValue; }

  static const bool has_default_value = false;

private:

  /** an id */
  static const uint64_t s_id;

  /** an extra hash value (to avoid same-value-type collisions) */
  static const uint64_t s_hashValue;
};

/**
 * An "attribute type" structure for boolean flags (special).
 */
template <class T>
struct Attribute<T, bool> {

  /** the value type for this attribute */
  typedef bool value_type;

  /** cleanup routine when the Node goes away */
  static inline void cleanup(const bool&) {}

  static inline uint64_t getId() { return s_id; }
  static inline uint64_t getHashValue() { return s_hashValue; }

  static const bool has_default_value = true;
  static const bool default_value = false;

  static inline uint64_t checkID(uint64_t id) {
    AlwaysAssert(id <= 63,
                 "Too many boolean node attributes registered during initialization !");
    return id;
  }

private:

  /** a bit assignment */
  static const uint64_t s_id;

  /** an extra hash value (to avoid same-value-type collisions) */
  static const uint64_t s_hashValue;
};

// SPECIFIC, GLOBAL ATTRIBUTE DEFINITIONS ======================================

namespace attr {
  struct VarName {};
  struct Type {};

  template <class T>
  struct LastAttributeId {
    static uint64_t s_id;
  };

  template <class T>
  uint64_t LastAttributeId<T>::s_id = 0;
}/* CVC4::expr::attr namespace */

typedef Attribute<attr::VarName, std::string> VarNameAttr;
typedef Attribute<attr::Type, const CVC4::Type*> TypeAttr;

// ATTRIBUTE IDENTIFIER ASSIGNMENT =============================================

template <class T, class value_t>
const uint64_t Attribute<T, value_t>::s_id =
  attr::LastAttributeId<typename KindValueToTableValueMapping<value_t>::table_value_type>::s_id++;
template <class T, class value_t>
const uint64_t Attribute<T, value_t>::s_hashValue = Attribute<T, value_t>::s_id;

template <class T>
const uint64_t Attribute<T, bool>::s_id =
  Attribute<T, bool>::checkID(attr::LastAttributeId<bool>::s_id++);
template <class T>
const uint64_t Attribute<T, bool>::s_hashValue = Attribute<T, bool>::s_id;

class AttributeManager;

template <class T>
struct getTable {
  //inline AttrHash<KindTableValueMapping<T> >& get(AttributeManager& am);
};

// ATTRIBUTE MANAGER ===========================================================

class AttributeManager {
  NodeManager* d_nm;

  AttrHash<bool>    d_bools;
  AttrHash<uint64_t>    d_ints;
  AttrHash<SoftNode>    d_exprs;
  AttrHash<std::string> d_strings;
  AttrHash<void*>       d_ptrs;

  template <class T>
  friend struct getTable;

public:
  AttributeManager(NodeManager* nm) : d_nm(nm) {}

  template <class AttrKind>
  typename AttrKind::value_type getAttribute(const Node& n,
                                             const AttrKind&) const;

  template <class AttrKind>
  bool hasAttribute(const Node& n,
                    const AttrKind&) const;

  template <class AttrKind>
  bool hasAttribute(const Node& n,
                    const AttrKind&,
                    typename AttrKind::value_type*) const;

  template <class AttrKind>
  void setAttribute(const Node& n,
                    const AttrKind&,
                    const typename AttrKind::value_type& value);
};

// MAPPING OF ATTRIBUTE KINDS TO TABLES IN THE ATTRIBUTE MANAGER ===============

template <>
struct getTable<bool> {
  typedef AttrHash<bool> table_type;
  static inline table_type& get(AttributeManager& am) {
    return am.d_bools;
  }
  static inline const table_type& get(const AttributeManager& am) {
    return am.d_bools;
  }
};

template <>
struct getTable<uint64_t> {
  typedef AttrHash<uint64_t> table_type;
  static inline table_type& get(AttributeManager& am) {
    return am.d_ints;
  }
  static inline const table_type& get(const AttributeManager& am) {
    return am.d_ints;
  }
};

template <>
struct getTable<Node> {
  typedef AttrHash<SoftNode> table_type;
  static inline table_type& get(AttributeManager& am) {
    return am.d_exprs;
  }
  static inline const table_type& get(const AttributeManager& am) {
    return am.d_exprs;
  }
};

template <>
struct getTable<std::string> {
  typedef AttrHash<std::string> table_type;
  static inline table_type& get(AttributeManager& am) {
    return am.d_strings;
  }
  static inline const table_type& get(const AttributeManager& am) {
    return am.d_strings;
  }
};

template <class T>
struct getTable<const T*> {
  typedef AttrHash<void*> table_type;
  static inline table_type& get(AttributeManager& am) {
    return am.d_ptrs;
  }
  static inline const table_type& get(const AttributeManager& am) {
    return am.d_ptrs;
  }
};

// ATTRIBUTE MANAGER IMPLEMENTATIONS ===========================================

template <class AttrKind>
typename AttrKind::value_type AttributeManager::getAttribute(const Node& n,
                                                             const AttrKind&) const {

  typedef typename AttrKind::value_type value_type;
  typedef KindValueToTableValueMapping<value_type> mapping;
  typedef typename getTable<value_type>::table_type table_type;

  const table_type& ah = getTable<value_type>::get(*this);
  typename table_type::const_iterator i = ah.find(std::make_pair(AttrKind::getId(), n));

  if(i == ah.end()) {
    return typename AttrKind::value_type();
  }

  return mapping::convertBack((*i).second);
}

/* helper template class for hasAttribute(), specialized based on
 * whether AttrKind has a "default value" that all Nodes implicitly
 * have or not. */
template <bool has_default, class AttrKind>
struct HasAttribute;

template <class AttrKind>
struct HasAttribute<true, AttrKind> {
  static inline bool hasAttribute(const AttributeManager* am,
                                  const Node& n) {
    return true;
  }

  static inline bool hasAttribute(const AttributeManager* am,
                                  const Node& n,
                                  typename AttrKind::value_type* ret) {
    if(ret != NULL) {
      typedef typename AttrKind::value_type value_type;
      typedef KindValueToTableValueMapping<value_type> mapping;
      typedef typename getTable<value_type>::table_type table_type;

      const table_type& ah = getTable<value_type>::get(*am);
      typename table_type::const_iterator i = ah.find(std::make_pair(AttrKind::getId(), n));

      if(i == ah.end()) {
        *ret = AttrKind::default_value;
      } else {
        *ret = mapping::convertBack((*i).second);
      }
    }

    return true;
  }
};

template <class AttrKind>
struct HasAttribute<false, AttrKind> {
  static inline bool hasAttribute(const AttributeManager* am,
                                  const Node& n) {
    typedef typename AttrKind::value_type value_type;
    typedef KindValueToTableValueMapping<value_type> mapping;
    typedef typename getTable<value_type>::table_type table_type;

    const table_type& ah = getTable<value_type>::get(*am);
    typename table_type::const_iterator i = ah.find(std::make_pair(AttrKind::getId(), n));

    if(i == ah.end()) {
      return false;
    }

    return true;
  }

  static inline bool hasAttribute(const AttributeManager* am,
                                  const Node& n,
                                  typename AttrKind::value_type* ret) {
    typedef typename AttrKind::value_type value_type;
    typedef KindValueToTableValueMapping<value_type> mapping;
    typedef typename getTable<value_type>::table_type table_type;

    const table_type& ah = getTable<value_type>::get(*am);
    typename table_type::const_iterator i = ah.find(std::make_pair(AttrKind::getId(), n));

    if(i == ah.end()) {
      return false;
    }

    if(ret != NULL) {
      *ret = mapping::convertBack((*i).second);
    }

    return true;
  }
};

template <class AttrKind>
bool AttributeManager::hasAttribute(const Node& n,
                                    const AttrKind&) const {
  return HasAttribute<AttrKind::has_default_value, AttrKind>::hasAttribute(this, n);
}

template <class AttrKind>
bool AttributeManager::hasAttribute(const Node& n,
                                    const AttrKind&,
                                    typename AttrKind::value_type* ret) const {
  return HasAttribute<AttrKind::has_default_value, AttrKind>::hasAttribute(this, n, ret);
}

template <class AttrKind>
inline void AttributeManager::setAttribute(const Node& n,
                                           const AttrKind&,
                                           const typename AttrKind::value_type& value) {

  typedef typename AttrKind::value_type value_type;
  typedef KindValueToTableValueMapping<value_type> mapping;
  typedef typename getTable<value_type>::table_type table_type;

  table_type& ah = getTable<value_type>::get(*this);
  ah[std::make_pair(AttrKind::getId(), n)] = mapping::convert(value);
}

}/* CVC4::expr namespace */
}/* CVC4 namespace */

#endif /* __CVC4__EXPR__ATTRIBUTE_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback