1 /********************* */
3 ** Original author: mdeters
4 ** Major contributors: dejan
5 ** Minor contributors (to current version): none
6 ** This file is part of the CVC4 prototype.
7 ** Copyright (c) 2009, 2010 The Analysis of Computer Systems Group (ACSys)
8 ** Courant Institute of Mathematical Sciences
10 ** See the file COPYING in the top-level source directory for licensing
13 ** An expression node.
15 ** Instances of this class are generally referenced through
16 ** cvc4::Node rather than by pointer; cvc4::Node maintains the
17 ** reference count on NodeValue instances and
20 /* this must be above the check for __CVC4__EXPR__NODE_VALUE_H */
21 /* to resolve a circular dependency */
22 //#include "expr/node.h"
24 #ifndef __CVC4__EXPR__NODE_VALUE_H
25 #define __CVC4__EXPR__NODE_VALUE_H
27 #include "cvc4_config.h"
37 template <unsigned> class NodeBuilder
;
43 * This is an NodeValue.
47 /** A convenient null-valued expression value */
48 static NodeValue s_null
;
50 /** Maximum reference count possible. Used for sticky
51 * reference-counting. Should be (1 << num_bits(d_rc)) - 1 */
52 static const unsigned MAX_RC
= 255;
54 /** Number of bits assigned to the kind in the NodeValue header */
55 static const unsigned KINDBITS
= 8;
57 /** A mask for d_kind */
58 static const unsigned kindMask
= (1u << KINDBITS
) - 1;
60 // this header fits into one 64-bit word
62 /** The ID (0 is reserved for the null value) */
65 /** The expression's reference count. @see cvc4::Node. */
68 /** Kind of the expression */
69 unsigned d_kind
: KINDBITS
;
71 /** Number of children */
72 unsigned d_nchildren
: 16;
74 /** Variable number of child nodes */
75 NodeValue
*d_children
[0];
77 // todo add exprMgr ref in debug case
79 friend class CVC4::Node
;
80 template <unsigned> friend class CVC4::NodeBuilder
;
81 friend class CVC4::NodeManager
;
86 static size_t next_id
;
88 /** Private default constructor for the null value. */
91 /** Private default constructor for the NodeBuilder. */
94 /** Destructor decrements the ref counts of its children */
97 typedef NodeValue
** ev_iterator
;
98 typedef NodeValue
const* const* const_ev_iterator
;
100 ev_iterator
ev_begin();
101 ev_iterator
ev_end();
103 const_ev_iterator
ev_begin() const;
104 const_ev_iterator
ev_end() const;
106 class node_iterator
{
107 const_ev_iterator d_i
;
109 explicit node_iterator(const_ev_iterator i
) : d_i(i
) {}
111 inline Node
operator*();
113 bool operator==(const node_iterator
& i
) {
117 bool operator!=(const node_iterator
& i
) {
121 node_iterator
operator++() {
126 node_iterator
operator++(int) {
127 return node_iterator(d_i
++);
130 typedef std::input_iterator_tag iterator_category
;
131 typedef Node value_type
;
132 typedef ptrdiff_t difference_type
;
133 typedef Node
* pointer
;
134 typedef Node
& reference
;
136 typedef node_iterator const_node_iterator
;
141 typedef node_iterator iterator
;
142 typedef node_iterator const_iterator
;
147 const_iterator
begin() const;
148 const_iterator
end() const;
151 * Hash this expression.
152 * @return the hash value of this expression.
154 size_t hash() const {
155 size_t hash
= d_kind
;
156 const_ev_iterator i
= ev_begin();
157 const_ev_iterator i_end
= ev_end();
159 hash
^= (*i
)->d_id
+ 0x9e3779b9 + (hash
<< 6) + (hash
>> 2);
165 inline bool compareTo(const NodeValue
* nv
) const {
166 if(d_kind
!= nv
->d_kind
)
168 if(d_nchildren
!= nv
->d_nchildren
)
170 const_ev_iterator i
= ev_begin();
171 const_ev_iterator j
= nv
->ev_begin();
172 const_ev_iterator i_end
= ev_end();
174 if ((*i
) != (*j
)) return false;
180 // Comparison predicate
182 bool operator()(const NodeValue
* nv1
, const NodeValue
* nv2
) const {
183 return nv1
->compareTo(nv2
);
187 unsigned getId() const { return d_id
; }
188 Kind
getKind() const { return dKindToKind(d_kind
); }
189 unsigned getNumChildren() const { return d_nchildren
; }
191 std::string
toString() const;
192 void toStream(std::ostream
& out
) const;
194 static inline unsigned kindToDKind(Kind k
) {
195 return ((unsigned) k
) & kindMask
;
197 static inline Kind
dKindToKind(unsigned d
) {
198 return (d
== kindMask
) ? UNDEFINED_KIND
: Kind(d
);
202 }/* CVC4::expr namespace */
203 }/* CVC4 namespace */
205 #include "expr/node.h"
210 inline Node
NodeValue::node_iterator::operator*() {
214 }/* CVC4::expr namespace */
215 }/* CVC4 namespace */
217 #endif /* __CVC4__EXPR__NODE_VALUE_H */