1 /********************* */
2 /*! \file node_manager.h
4 ** Top contributors (to current version):
5 ** Morgan Deters, Christopher L. Conway, Dejan Jovanovic
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2017 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** \brief A manager for Nodes
14 ** A manager for Nodes.
16 ** Reviewed by Chris Conway, Apr 5 2010 (bug #65).
19 #include "cvc4_private.h"
21 /* circular dependency; force node.h first */
22 //#include "expr/attribute.h"
23 #include "expr/node.h"
24 #include "expr/type_node.h"
25 #include "expr/expr.h"
26 #include "expr/expr_manager.h"
28 #ifndef __CVC4__NODE_MANAGER_H
29 #define __CVC4__NODE_MANAGER_H
33 #include <unordered_set>
36 #include "expr/kind.h"
37 #include "expr/metakind.h"
38 #include "expr/node_value.h"
39 #include "options/options.h"
43 class StatisticsRegistry
;
44 class ResourceManager
;
48 class AttributeUniqueId
;
49 class AttributeManager
;
50 }/* CVC4::expr::attr namespace */
53 }/* CVC4::expr namespace */
56 * An interface that an interested party can implement and then subscribe
57 * to NodeManager events via NodeManager::subscribeEvents(this).
59 class NodeManagerListener
{
61 virtual ~NodeManagerListener() {}
62 virtual void nmNotifyNewSort(TypeNode tn
, uint32_t flags
) {}
63 virtual void nmNotifyNewSortConstructor(TypeNode tn
) {}
64 virtual void nmNotifyInstantiateSortConstructor(TypeNode ctor
, TypeNode sort
,
66 virtual void nmNotifyNewDatatypes(
67 const std::vector
<DatatypeType
>& datatypes
) {}
68 virtual void nmNotifyNewVar(TNode n
, uint32_t flags
) {}
69 virtual void nmNotifyNewSkolem(TNode n
, const std::string
& comment
,
72 * Notify a listener of a Node that's being GCed. If this function stores a
74 * to the Node somewhere, very bad things will happen.
76 virtual void nmNotifyDeleteNode(TNode n
) {}
77 }; /* class NodeManagerListener */
80 template <unsigned nchild_thresh
> friend class CVC4::NodeBuilder
;
81 friend class NodeManagerScope
;
82 friend class expr::NodeValue
;
83 friend class expr::TypeChecker
;
85 // friends so they can access mkVar() here, which is private
86 friend Expr
ExprManager::mkVar(const std::string
&, Type
, uint32_t flags
);
87 friend Expr
ExprManager::mkVar(Type
, uint32_t flags
);
89 // friend so it can access NodeManager's d_listeners and notify clients
90 friend std::vector
<DatatypeType
> ExprManager::mkMutualDatatypeTypes(std::vector
<Datatype
>&, std::set
<Type
>&);
92 /** Predicate for use with STL algorithms */
93 struct NodeValueReferenceCountNonZero
{
94 bool operator()(expr::NodeValue
* nv
) { return nv
->d_rc
> 0; }
97 typedef std::unordered_set
<expr::NodeValue
*,
98 expr::NodeValuePoolHashFunction
,
99 expr::NodeValuePoolEq
> NodeValuePool
;
100 typedef std::unordered_set
<expr::NodeValue
*,
101 expr::NodeValueIDHashFunction
,
102 expr::NodeValueIDEquality
> NodeValueIDSet
;
104 static CVC4_THREAD_LOCAL NodeManager
* s_current
;
107 StatisticsRegistry
* d_statisticsRegistry
;
109 ResourceManager
* d_resourceManager
;
112 * A list of registrations on d_options to that call into d_resourceManager.
113 * These must be garbage collected before d_options and d_resourceManager.
115 ListenerRegistrationList
* d_registrations
;
117 NodeValuePool d_nodeValuePool
;
121 expr::attr::AttributeManager
* d_attrManager
;
123 /** The associated ExprManager */
124 ExprManager
* d_exprManager
;
127 * The node value we're currently freeing. This unique node value
128 * is permitted to have outstanding TNodes to it (in "soft"
129 * contexts, like as a key in attribute tables), even though
130 * normally it's an error to have a TNode to a node value with a
131 * reference count of 0. Being "under deletion" also enables
132 * assertions that inc() is not called on it. (A poorly-behaving
133 * attribute cleanup function could otherwise create a "Node" that
134 * points to the node value that is in the process of being deleted,
135 * springing it back to life.)
137 expr::NodeValue
* d_nodeUnderDeletion
;
140 * True iff we are in reclaimZombies(). This avoids unnecessary
141 * recursion; a NodeValue being deleted might zombify other
142 * NodeValues, but these shouldn't trigger a (recursive) call to
145 bool d_inReclaimZombies
;
148 * The set of zombie nodes. We may want to revisit this design, as
149 * we might like to delete nodes in least-recently-used order. But
150 * we also need to avoid processing a zombie twice.
152 NodeValueIDSet d_zombies
;
155 * NodeValues with maxed out reference counts. These live as long as the
156 * NodeManager. They have a custom deallocation procedure at the very end.
158 std::vector
<expr::NodeValue
*> d_maxedOut
;
161 * A set of operator singletons (w.r.t. to this NodeManager
162 * instance) for operators. Conceptually, Nodes with kind, say,
163 * PLUS, are APPLYs of a PLUS operator to arguments. This array
164 * holds the set of operators for these things. A PLUS operator is
165 * a Node with kind "BUILTIN", and if you call
166 * plusOperator->getConst<CVC4::Kind>(), you get kind::PLUS back.
168 Node d_operators
[kind::LAST_KIND
];
170 /** unique vars per (Kind,Type) */
171 std::map
< Kind
, std::map
< TypeNode
, Node
> > d_unique_vars
;
174 * A list of subscribers for NodeManager events.
176 std::vector
<NodeManagerListener
*> d_listeners
;
178 /** A list of datatypes owned by this node manager. */
179 std::vector
<Datatype
*> d_ownedDatatypes
;
182 * A map of tuple and record types to their corresponding datatype.
184 class TupleTypeCache
{
186 std::map
< TypeNode
, TupleTypeCache
> d_children
;
188 TypeNode
getTupleType( NodeManager
* nm
, std::vector
< TypeNode
>& types
, unsigned index
= 0 );
192 std::map
< TypeNode
, std::map
< std::string
, RecTypeCache
> > d_children
;
194 TypeNode
getRecordType( NodeManager
* nm
, const Record
& rec
, unsigned index
= 0 );
196 TupleTypeCache d_tt_cache
;
197 RecTypeCache d_rt_cache
;
200 * Keep a count of all abstract values produced by this NodeManager.
201 * Abstract values have a type attribute, so if multiple SmtEngines
202 * are attached to this NodeManager, we don't want their abstract
205 unsigned d_abstractValueCount
;
208 * A counter used to produce unique skolem names.
210 * Note that it is NOT incremented when skolems are created using
211 * SKOLEM_EXACT_NAME, so it is NOT a count of the skolems produced
212 * by this node manager.
214 unsigned d_skolemCounter
;
217 * Look up a NodeValue in the pool associated to this NodeManager.
218 * The NodeValue argument need not be a "completely-constructed"
219 * NodeValue. In particular, "non-inlined" constants are permitted
222 * For non-CONSTANT metakinds, nv's d_kind and d_nchildren should be
223 * correctly set, and d_children[0..n-1] should be valid (extant)
224 * NodeValues for lookup.
226 * For CONSTANT metakinds, nv's d_kind should be set correctly.
227 * Normally a CONSTANT would have d_nchildren == 0 and the constant
228 * value inlined in the d_children space. However, here we permit
229 * "non-inlined" NodeValues to avoid unnecessary copying. For
230 * these, d_nchildren == 1, and d_nchildren is a pointer to the
233 * The point of this complex design is to permit efficient lookups
234 * (without fully constructing a NodeValue). In the case that the
235 * argument is not fully constructed, and this function returns
236 * NULL, the caller should fully construct an equivalent one before
237 * calling poolInsert(). NON-FULLY-CONSTRUCTED NODEVALUES are not
238 * permitted in the pool!
240 inline expr::NodeValue
* poolLookup(expr::NodeValue
* nv
) const;
243 * Insert a NodeValue into the NodeManager's pool.
245 * It is an error to insert a NodeValue already in the pool.
246 * Enquire first with poolLookup().
248 inline void poolInsert(expr::NodeValue
* nv
);
251 * Remove a NodeValue from the NodeManager's pool.
253 * It is an error to request the removal of a NodeValue from the
254 * pool that is not in the pool.
256 inline void poolRemove(expr::NodeValue
* nv
);
259 * Determine if nv is currently being deleted by the NodeManager.
261 inline bool isCurrentlyDeleting(const expr::NodeValue
* nv
) const {
262 return d_nodeUnderDeletion
== nv
;
266 * Register a NodeValue as a zombie.
268 inline void markForDeletion(expr::NodeValue
* nv
) {
269 Assert(nv
->d_rc
== 0);
271 // if d_reclaiming is set, make sure we don't call
272 // reclaimZombies(), because it's already running.
273 if(Debug
.isOn("gc")) {
274 Debug("gc") << "zombifying node value " << nv
275 << " [" << nv
->d_id
<< "]: ";
276 nv
->printAst(Debug("gc"));
277 Debug("gc") << (d_inReclaimZombies
? " [CURRENTLY-RECLAIMING]" : "")
280 d_zombies
.insert(nv
); // FIXME multithreading
282 if(safeToReclaimZombies()) {
283 if(d_zombies
.size() > 5000) {
290 * Register a NodeValue as having a maxed out reference count. This NodeValue
291 * will live as long as its containing NodeManager.
293 inline void markRefCountMaxedOut(expr::NodeValue
* nv
) {
294 Assert(nv
->HasMaximizedReferenceCount());
295 if(Debug
.isOn("gc")) {
296 Debug("gc") << "marking node value " << nv
297 << " [" << nv
->d_id
<< "]: as maxed out" << std::endl
;
299 d_maxedOut
.push_back(nv
);
303 * Reclaim all zombies.
305 void reclaimZombies();
308 * It is safe to collect zombies.
310 bool safeToReclaimZombies() const;
313 * Returns a reverse topological sort of a list of NodeValues. The NodeValues
314 * must be valid and have ids. The NodeValues are not modified (including ref
317 static std::vector
<expr::NodeValue
*> TopologicalSort(
318 const std::vector
<expr::NodeValue
*>& roots
);
321 * This template gives a mechanism to stack-allocate a NodeValue
322 * with enough space for N children (where N is a compile-time
323 * constant). You use it like this:
325 * NVStorage<4> nvStorage;
326 * NodeValue& nvStack = reinterpret_cast<NodeValue&>(nvStorage);
328 * ...and then you can use nvStack as a NodeValue that you know has
329 * room for 4 children.
334 expr::NodeValue
* child
[N
];
335 };/* struct NodeManager::NVStorage<N> */
337 /* A note on isAtomic() and isAtomicFormula() (in CVC3 parlance)..
339 * It has been decided for now to hold off on implementations of
340 * these functions, as they may only be needed in CNF conversion,
341 * where it's pointless to do a lazy isAtomic determination by
342 * searching through the DAG, and storing it, since the result will
343 * only be used once. For more details see the 4/27/2010 CVC4
344 * developer's meeting notes at:
346 * http://goedel.cims.nyu.edu/wiki/Meeting_Minutes_-_April_27,_2010#isAtomic.28.29_and_isAtomicFormula.28.29
348 // bool containsDecision(TNode); // is "atomic"
349 // bool properlyContainsDecision(TNode); // all children are atomic
351 // undefined private copy constructor (disallow copy)
352 NodeManager(const NodeManager
&) CVC4_UNDEFINED
;
354 NodeManager
& operator=(const NodeManager
&) CVC4_UNDEFINED
;
359 * Create a variable with the given name and type. NOTE that no
360 * lookup is done on the name. If you mkVar("a", type) and then
361 * mkVar("a", type) again, you have two variables. The NodeManager
362 * version of this is private to avoid internal uses of mkVar() from
363 * within CVC4. Such uses should employ mkSkolem() instead.
365 Node
mkVar(const std::string
& name
, const TypeNode
& type
, uint32_t flags
= ExprManager::VAR_FLAG_NONE
);
366 Node
* mkVarPtr(const std::string
& name
, const TypeNode
& type
, uint32_t flags
= ExprManager::VAR_FLAG_NONE
);
368 /** Create a variable with the given type. */
369 Node
mkVar(const TypeNode
& type
, uint32_t flags
= ExprManager::VAR_FLAG_NONE
);
370 Node
* mkVarPtr(const TypeNode
& type
, uint32_t flags
= ExprManager::VAR_FLAG_NONE
);
374 explicit NodeManager(ExprManager
* exprManager
);
375 explicit NodeManager(ExprManager
* exprManager
, const Options
& options
);
378 /** The node manager in the current public-facing CVC4 library context */
379 static NodeManager
* currentNM() { return s_current
; }
380 /** The resource manager associated with the current node manager */
381 static ResourceManager
* currentResourceManager() { return s_current
->d_resourceManager
; }
383 /** Get this node manager's options (const version) */
384 const Options
& getOptions() const {
388 /** Get this node manager's options (non-const version) */
389 Options
& getOptions() {
393 /** Get this node manager's resource manager */
394 ResourceManager
* getResourceManager() throw() { return d_resourceManager
; }
396 /** Get this node manager's statistics registry */
397 StatisticsRegistry
* getStatisticsRegistry() const throw() {
398 return d_statisticsRegistry
;
401 /** Subscribe to NodeManager events */
402 void subscribeEvents(NodeManagerListener
* listener
) {
403 Assert(std::find(d_listeners
.begin(), d_listeners
.end(), listener
) == d_listeners
.end(), "listener already subscribed");
404 d_listeners
.push_back(listener
);
407 /** Unsubscribe from NodeManager events */
408 void unsubscribeEvents(NodeManagerListener
* listener
) {
409 std::vector
<NodeManagerListener
*>::iterator elt
= std::find(d_listeners
.begin(), d_listeners
.end(), listener
);
410 Assert(elt
!= d_listeners
.end(), "listener not subscribed");
411 d_listeners
.erase(elt
);
414 /** register datatype */
415 unsigned registerDatatype(Datatype
* dt
);
416 /** get datatype for index */
417 const Datatype
& getDatatypeForIndex( unsigned index
) const;
419 /** Get a Kind from an operator expression */
420 static inline Kind
operatorToKind(TNode n
);
422 // general expression-builders
424 /** Create a node with one child. */
425 Node
mkNode(Kind kind
, TNode child1
);
426 Node
* mkNodePtr(Kind kind
, TNode child1
);
428 /** Create a node with two children. */
429 Node
mkNode(Kind kind
, TNode child1
, TNode child2
);
430 Node
* mkNodePtr(Kind kind
, TNode child1
, TNode child2
);
432 /** Create a node with three children. */
433 Node
mkNode(Kind kind
, TNode child1
, TNode child2
, TNode child3
);
434 Node
* mkNodePtr(Kind kind
, TNode child1
, TNode child2
, TNode child3
);
436 /** Create a node with four children. */
437 Node
mkNode(Kind kind
, TNode child1
, TNode child2
, TNode child3
,
439 Node
* mkNodePtr(Kind kind
, TNode child1
, TNode child2
, TNode child3
,
442 /** Create a node with five children. */
443 Node
mkNode(Kind kind
, TNode child1
, TNode child2
, TNode child3
,
444 TNode child4
, TNode child5
);
445 Node
* mkNodePtr(Kind kind
, TNode child1
, TNode child2
, TNode child3
,
446 TNode child4
, TNode child5
);
448 /** Create a node with an arbitrary number of children. */
449 template <bool ref_count
>
450 Node
mkNode(Kind kind
, const std::vector
<NodeTemplate
<ref_count
> >& children
);
451 template <bool ref_count
>
452 Node
* mkNodePtr(Kind kind
, const std::vector
<NodeTemplate
<ref_count
> >& children
);
454 /** Create a node (with no children) by operator. */
455 Node
mkNode(TNode opNode
);
456 Node
* mkNodePtr(TNode opNode
);
458 /** Create a node with one child by operator. */
459 Node
mkNode(TNode opNode
, TNode child1
);
460 Node
* mkNodePtr(TNode opNode
, TNode child1
);
462 /** Create a node with two children by operator. */
463 Node
mkNode(TNode opNode
, TNode child1
, TNode child2
);
464 Node
* mkNodePtr(TNode opNode
, TNode child1
, TNode child2
);
466 /** Create a node with three children by operator. */
467 Node
mkNode(TNode opNode
, TNode child1
, TNode child2
, TNode child3
);
468 Node
* mkNodePtr(TNode opNode
, TNode child1
, TNode child2
, TNode child3
);
470 /** Create a node with four children by operator. */
471 Node
mkNode(TNode opNode
, TNode child1
, TNode child2
, TNode child3
,
473 Node
* mkNodePtr(TNode opNode
, TNode child1
, TNode child2
, TNode child3
,
476 /** Create a node with five children by operator. */
477 Node
mkNode(TNode opNode
, TNode child1
, TNode child2
, TNode child3
,
478 TNode child4
, TNode child5
);
479 Node
* mkNodePtr(TNode opNode
, TNode child1
, TNode child2
, TNode child3
,
480 TNode child4
, TNode child5
);
482 /** Create a node by applying an operator to the children. */
483 template <bool ref_count
>
484 Node
mkNode(TNode opNode
, const std::vector
<NodeTemplate
<ref_count
> >& children
);
485 template <bool ref_count
>
486 Node
* mkNodePtr(TNode opNode
, const std::vector
<NodeTemplate
<ref_count
> >& children
);
488 Node
mkBoundVar(const std::string
& name
, const TypeNode
& type
);
489 Node
* mkBoundVarPtr(const std::string
& name
, const TypeNode
& type
);
491 Node
mkBoundVar(const TypeNode
& type
);
492 Node
* mkBoundVarPtr(const TypeNode
& type
);
494 /** get the canonical bound variable list for function type tn */
495 static Node
getBoundVarListForFunctionType( TypeNode tn
);
498 * Optional flags used to control behavior of NodeManager::mkSkolem().
499 * They should be composed with a bitwise OR (e.g.,
500 * "SKOLEM_NO_NOTIFY | SKOLEM_EXACT_NAME"). Of course, SKOLEM_DEFAULT
501 * cannot be composed in such a manner.
504 SKOLEM_DEFAULT
= 0, /**< default behavior */
505 SKOLEM_NO_NOTIFY
= 1, /**< do not notify subscribers */
506 SKOLEM_EXACT_NAME
= 2,/**< do not make the name unique by adding the id */
507 SKOLEM_IS_GLOBAL
= 4 /**< global vars appear in models even after a pop */
508 };/* enum SkolemFlags */
511 * Create a skolem constant with the given name, type, and comment.
513 * @param prefix the name of the new skolem variable is the prefix
514 * appended with a unique ID. This way a family of skolem variables
515 * can be made with unique identifiers, used in dump, tracing, and
516 * debugging output. Use SKOLEM_EXECT_NAME flag if you don't want
517 * a unique ID appended and use prefix as the name.
519 * @param type the type of the skolem variable to create
521 * @param comment a comment for dumping output; if declarations are
522 * being dumped, this is included in a comment before the declaration
523 * and can be quite useful for debugging
525 * @param flags an optional mask of bits from SkolemFlags to control
526 * mkSkolem() behavior
528 Node
mkSkolem(const std::string
& prefix
, const TypeNode
& type
,
529 const std::string
& comment
= "", int flags
= SKOLEM_DEFAULT
);
531 /** Create a instantiation constant with the given type. */
532 Node
mkInstConstant(const TypeNode
& type
);
534 /** Create a boolean term variable. */
535 Node
mkBooleanTermVariable();
537 /** Make a new abstract value with the given type. */
538 Node
mkAbstractValue(const TypeNode
& type
);
540 /** make unique (per Type,Kind) variable. */
541 Node
mkNullaryOperator(const TypeNode
& type
, Kind k
);
544 * Create a constant of type T. It will have the appropriate
545 * CONST_* kind defined for T.
548 Node
mkConst(const T
&);
551 TypeNode
mkTypeConst(const T
&);
553 template <class NodeClass
, class T
>
554 NodeClass
mkConstInternal(const T
&);
556 /** Create a node with children. */
557 TypeNode
mkTypeNode(Kind kind
, TypeNode child1
);
558 TypeNode
mkTypeNode(Kind kind
, TypeNode child1
, TypeNode child2
);
559 TypeNode
mkTypeNode(Kind kind
, TypeNode child1
, TypeNode child2
,
561 TypeNode
mkTypeNode(Kind kind
, const std::vector
<TypeNode
>& children
);
564 * Determine whether Nodes of a particular Kind have operators.
565 * @returns true if Nodes of Kind k have operators.
567 static inline bool hasOperator(Kind k
);
570 * Get the (singleton) operator of an OPERATOR-kinded kind. The
571 * returned node n will have kind BUILTIN, and calling
572 * n.getConst<CVC4::Kind>() will yield k.
574 inline TNode
operatorOf(Kind k
) {
575 AssertArgument( kind::metaKindOf(k
) == kind::metakind::OPERATOR
, k
,
576 "Kind is not an OPERATOR-kinded kind "
577 "in NodeManager::operatorOf()" );
578 return d_operators
[k
];
582 * Retrieve an attribute for a node.
584 * @param nv the node value
585 * @param attr an instance of the attribute kind to retrieve.
586 * @returns the attribute, if set, or a default-constructed
587 * <code>AttrKind::value_type</code> if not.
589 template <class AttrKind
>
590 inline typename
AttrKind::value_type
getAttribute(expr::NodeValue
* nv
,
591 const AttrKind
& attr
) const;
594 * Check whether an attribute is set for a node.
596 * @param nv the node value
597 * @param attr an instance of the attribute kind to check
598 * @returns <code>true</code> iff <code>attr</code> is set for
601 template <class AttrKind
>
602 inline bool hasAttribute(expr::NodeValue
* nv
,
603 const AttrKind
& attr
) const;
606 * Check whether an attribute is set for a node, and, if so,
609 * @param nv the node value
610 * @param attr an instance of the attribute kind to check
611 * @param value a reference to an object of the attribute's value type.
612 * <code>value</code> will be set to the value of the attribute, if it is
613 * set for <code>nv</code>; otherwise, it will be set to the default
614 * value of the attribute.
615 * @returns <code>true</code> iff <code>attr</code> is set for
618 template <class AttrKind
>
619 inline bool getAttribute(expr::NodeValue
* nv
,
620 const AttrKind
& attr
,
621 typename
AttrKind::value_type
& value
) const;
624 * Set an attribute for a node. If the node doesn't have the
625 * attribute, this function assigns one. If the node has one, this
628 * @param nv the node value
629 * @param attr an instance of the attribute kind to set
630 * @param value the value of <code>attr</code> for <code>nv</code>
632 template <class AttrKind
>
633 inline void setAttribute(expr::NodeValue
* nv
,
634 const AttrKind
& attr
,
635 const typename
AttrKind::value_type
& value
);
638 * Retrieve an attribute for a TNode.
641 * @param attr an instance of the attribute kind to retrieve.
642 * @returns the attribute, if set, or a default-constructed
643 * <code>AttrKind::value_type</code> if not.
645 template <class AttrKind
>
646 inline typename
AttrKind::value_type
647 getAttribute(TNode n
, const AttrKind
& attr
) const;
650 * Check whether an attribute is set for a TNode.
653 * @param attr an instance of the attribute kind to check
654 * @returns <code>true</code> iff <code>attr</code> is set for <code>n</code>.
656 template <class AttrKind
>
657 inline bool hasAttribute(TNode n
,
658 const AttrKind
& attr
) const;
661 * Check whether an attribute is set for a TNode and, if so, retieve
665 * @param attr an instance of the attribute kind to check
666 * @param value a reference to an object of the attribute's value type.
667 * <code>value</code> will be set to the value of the attribute, if it is
668 * set for <code>nv</code>; otherwise, it will be set to the default value of
670 * @returns <code>true</code> iff <code>attr</code> is set for <code>n</code>.
672 template <class AttrKind
>
673 inline bool getAttribute(TNode n
,
674 const AttrKind
& attr
,
675 typename
AttrKind::value_type
& value
) const;
678 * Set an attribute for a node. If the node doesn't have the
679 * attribute, this function assigns one. If the node has one, this
683 * @param attr an instance of the attribute kind to set
684 * @param value the value of <code>attr</code> for <code>n</code>
686 template <class AttrKind
>
687 inline void setAttribute(TNode n
,
688 const AttrKind
& attr
,
689 const typename
AttrKind::value_type
& value
);
692 * Retrieve an attribute for a TypeNode.
694 * @param n the type node
695 * @param attr an instance of the attribute kind to retrieve.
696 * @returns the attribute, if set, or a default-constructed
697 * <code>AttrKind::value_type</code> if not.
699 template <class AttrKind
>
700 inline typename
AttrKind::value_type
701 getAttribute(TypeNode n
, const AttrKind
& attr
) const;
704 * Check whether an attribute is set for a TypeNode.
706 * @param n the type node
707 * @param attr an instance of the attribute kind to check
708 * @returns <code>true</code> iff <code>attr</code> is set for <code>n</code>.
710 template <class AttrKind
>
711 inline bool hasAttribute(TypeNode n
,
712 const AttrKind
& attr
) const;
715 * Check whether an attribute is set for a TypeNode and, if so, retieve
718 * @param n the type node
719 * @param attr an instance of the attribute kind to check
720 * @param value a reference to an object of the attribute's value type.
721 * <code>value</code> will be set to the value of the attribute, if it is
722 * set for <code>nv</code>; otherwise, it will be set to the default value of
724 * @returns <code>true</code> iff <code>attr</code> is set for <code>n</code>.
726 template <class AttrKind
>
727 inline bool getAttribute(TypeNode n
,
728 const AttrKind
& attr
,
729 typename
AttrKind::value_type
& value
) const;
732 * Set an attribute for a type node. If the node doesn't have the
733 * attribute, this function assigns one. If the type node has one,
734 * this overwrites it.
736 * @param n the type node
737 * @param attr an instance of the attribute kind to set
738 * @param value the value of <code>attr</code> for <code>n</code>
740 template <class AttrKind
>
741 inline void setAttribute(TypeNode n
,
742 const AttrKind
& attr
,
743 const typename
AttrKind::value_type
& value
);
745 /** Get the (singleton) type for Booleans. */
746 inline TypeNode
booleanType();
748 /** Get the (singleton) type for integers. */
749 inline TypeNode
integerType();
751 /** Get the (singleton) type for reals. */
752 inline TypeNode
realType();
754 /** Get the (singleton) type for strings. */
755 inline TypeNode
stringType();
757 /** Get the (singleton) type for RegExp. */
758 inline TypeNode
regExpType();
760 /** Get the (singleton) type for rounding modes. */
761 inline TypeNode
roundingModeType();
763 /** Get the bound var list type. */
764 inline TypeNode
boundVarListType();
766 /** Get the instantiation pattern type. */
767 inline TypeNode
instPatternType();
769 /** Get the instantiation pattern type. */
770 inline TypeNode
instPatternListType();
773 * Get the (singleton) type for builtin operators (that is, the type
774 * of the Node returned from Node::getOperator() when the operator
775 * is built-in, like EQUAL). */
776 inline TypeNode
builtinOperatorType();
779 * Make a function type from domain to range.
781 * @param domain the domain type
782 * @param range the range type
783 * @returns the functional type domain -> range
785 inline TypeNode
mkFunctionType(const TypeNode
& domain
, const TypeNode
& range
);
788 * Make a function type with input types from
789 * argTypes. <code>argTypes</code> must have at least one element.
791 * @param argTypes the domain is a tuple (argTypes[0], ..., argTypes[n])
792 * @param range the range type
793 * @returns the functional type (argTypes[0], ..., argTypes[n]) -> range
795 inline TypeNode
mkFunctionType(const std::vector
<TypeNode
>& argTypes
,
796 const TypeNode
& range
);
799 * Make a function type with input types from
800 * <code>sorts[0..sorts.size()-2]</code> and result type
801 * <code>sorts[sorts.size()-1]</code>. <code>sorts</code> must have
802 * at least 2 elements.
804 inline TypeNode
mkFunctionType(const std::vector
<TypeNode
>& sorts
);
807 * Make a predicate type with input types from
808 * <code>sorts</code>. The result with be a function type with range
809 * <code>BOOLEAN</code>. <code>sorts</code> must have at least one
812 inline TypeNode
mkPredicateType(const std::vector
<TypeNode
>& sorts
);
815 * Make a tuple type with types from
816 * <code>types</code>. <code>types</code> must have at least one
819 * @param types a vector of types
820 * @returns the tuple type (types[0], ..., types[n])
822 TypeNode
mkTupleType(const std::vector
<TypeNode
>& types
);
825 * Make a record type with the description from rec.
827 * @param rec a description of the record
828 * @returns the record type
830 TypeNode
mkRecordType(const Record
& rec
);
833 * Make a symbolic expression type with types from
834 * <code>types</code>. <code>types</code> may have any number of
837 * @param types a vector of types
838 * @returns the symbolic expression type (types[0], ..., types[n])
840 inline TypeNode
mkSExprType(const std::vector
<TypeNode
>& types
);
842 /** Make the type of floating-point with <code>exp</code> bit exponent and
843 <code>sig</code> bit significand */
844 inline TypeNode
mkFloatingPointType(unsigned exp
, unsigned sig
);
845 inline TypeNode
mkFloatingPointType(FloatingPointSize fs
);
847 /** Make the type of bitvectors of size <code>size</code> */
848 inline TypeNode
mkBitVectorType(unsigned size
);
850 /** Make the type of arrays with the given parameterization */
851 inline TypeNode
mkArrayType(TypeNode indexType
, TypeNode constituentType
);
853 /** Make the type of arrays with the given parameterization */
854 inline TypeNode
mkSetType(TypeNode elementType
);
856 /** Make a type representing a constructor with the given parameterization */
857 TypeNode
mkConstructorType(const DatatypeConstructor
& constructor
, TypeNode range
);
859 /** Make a type representing a selector with the given parameterization */
860 inline TypeNode
mkSelectorType(TypeNode domain
, TypeNode range
);
862 /** Make a type representing a tester with given parameterization */
863 inline TypeNode
mkTesterType(TypeNode domain
);
865 /** Make a new (anonymous) sort of arity 0. */
866 TypeNode
mkSort(uint32_t flags
= ExprManager::SORT_FLAG_NONE
);
868 /** Make a new sort with the given name of arity 0. */
869 TypeNode
mkSort(const std::string
& name
, uint32_t flags
= ExprManager::SORT_FLAG_NONE
);
871 /** Make a new sort by parameterizing the given sort constructor. */
872 TypeNode
mkSort(TypeNode constructor
,
873 const std::vector
<TypeNode
>& children
,
874 uint32_t flags
= ExprManager::SORT_FLAG_NONE
);
876 /** Make a new sort with the given name and arity. */
877 TypeNode
mkSortConstructor(const std::string
& name
, size_t arity
);
880 * Get the type for the given node and optionally do type checking.
882 * Initial type computation will be near-constant time if
883 * type checking is not requested. Results are memoized, so that
884 * subsequent calls to getType() without type checking will be
887 * Initial type checking is linear in the size of the expression.
888 * Again, the results are memoized, so that subsequent calls to
889 * getType(), with or without type checking, will be constant
892 * NOTE: A TypeCheckingException can be thrown even when type
893 * checking is not requested. getType() will always return a
894 * valid and correct type and, thus, an exception will be thrown
895 * when no valid or correct type can be computed (e.g., if the
896 * arguments to a bit-vector operation aren't bit-vectors). When
897 * type checking is not requested, getType() will do the minimum
898 * amount of checking required to return a valid result.
900 * @param n the Node for which we want a type
901 * @param check whether we should check the type as we compute it
904 TypeNode
getType(TNode n
, bool check
= false)
905 throw(TypeCheckingExceptionPrivate
, AssertionException
);
908 * Convert a node to an expression. Uses the ExprManager
909 * associated to this NodeManager.
911 inline Expr
toExpr(TNode n
);
914 * Convert an expression to a node.
916 static inline Node
fromExpr(const Expr
& e
);
919 * Convert a node manager to an expression manager.
921 inline ExprManager
* toExprManager();
924 * Convert an expression manager to a node manager.
926 static inline NodeManager
* fromExprManager(ExprManager
* exprManager
);
929 * Convert a type node to a type.
931 inline Type
toType(TypeNode tn
);
934 * Convert a type to a type node.
936 static inline TypeNode
fromType(Type t
);
938 /** Reclaim zombies while there are more than k nodes in the pool (if possible).*/
939 void reclaimZombiesUntil(uint32_t k
);
941 /** Reclaims all zombies (if possible).*/
942 void reclaimAllZombies();
944 /** Size of the node pool. */
945 size_t poolSize() const;
947 /** Deletes a list of attributes from the NM's AttributeManager.*/
948 void deleteAttributes(const std::vector
< const expr::attr::AttributeUniqueId
* >& ids
);
951 * This function gives developers a hook into the NodeManager.
952 * This can be changed in node_manager.cpp without recompiling most of cvc4.
954 * debugHook is a debugging only function, and should not be present in
955 * any published code!
957 void debugHook(int debugFlag
);
958 };/* class NodeManager */
961 * This class changes the "current" thread-global
962 * <code>NodeManager</code> when it is created and reinstates the
963 * previous thread-global <code>NodeManager</code> when it is
964 * destroyed, effectively maintaining a set of nested
965 * <code>NodeManager</code> scopes. This is especially useful on
966 * public-interface calls into the CVC4 library, where CVC4's notion
967 * of the "current" <code>NodeManager</code> should be set to match
968 * the calling context. See, for example, the implementations of
969 * public calls in the <code>ExprManager</code> and
970 * <code>SmtEngine</code> classes.
972 * The client must be careful to create and destroy
973 * <code>NodeManagerScope</code> objects in a well-nested manner (such
974 * as on the stack). You may create a <code>NodeManagerScope</code>
975 * with <code>new</code> and destroy it with <code>delete</code>, or
976 * place it as a data member of an object that is, but if the scope of
977 * these <code>new</code>/<code>delete</code> pairs isn't properly
978 * maintained, the incorrect "current" <code>NodeManager</code>
979 * pointer may be restored after a delete.
981 class NodeManagerScope
{
982 /** The old NodeManager, to be restored on destruction. */
983 NodeManager
* d_oldNodeManager
;
984 Options::OptionsScope d_optionsScope
;
987 NodeManagerScope(NodeManager
* nm
)
988 : d_oldNodeManager(NodeManager::s_current
)
989 , d_optionsScope(nm
? nm
->d_options
: NULL
) {
990 // There are corner cases where nm can be NULL and it's ok.
991 // For example, if you write { Expr e; }, then when the null
992 // Expr is destructed, there's no active node manager.
993 //Assert(nm != NULL);
994 NodeManager::s_current
= nm
;
995 //Options::s_current = nm ? nm->d_options : NULL;
996 Debug("current") << "node manager scope: "
997 << NodeManager::s_current
<< "\n";
1000 ~NodeManagerScope() {
1001 NodeManager::s_current
= d_oldNodeManager
;
1002 //Options::s_current = d_oldNodeManager ? d_oldNodeManager->d_options : NULL;
1003 Debug("current") << "node manager scope: "
1004 << "returning to " << NodeManager::s_current
<< "\n";
1006 };/* class NodeManagerScope */
1008 /** Get the (singleton) type for booleans. */
1009 inline TypeNode
NodeManager::booleanType() {
1010 return TypeNode(mkTypeConst
<TypeConstant
>(BOOLEAN_TYPE
));
1013 /** Get the (singleton) type for integers. */
1014 inline TypeNode
NodeManager::integerType() {
1015 return TypeNode(mkTypeConst
<TypeConstant
>(INTEGER_TYPE
));
1018 /** Get the (singleton) type for reals. */
1019 inline TypeNode
NodeManager::realType() {
1020 return TypeNode(mkTypeConst
<TypeConstant
>(REAL_TYPE
));
1023 /** Get the (singleton) type for strings. */
1024 inline TypeNode
NodeManager::stringType() {
1025 return TypeNode(mkTypeConst
<TypeConstant
>(STRING_TYPE
));
1028 /** Get the (singleton) type for regexps. */
1029 inline TypeNode
NodeManager::regExpType() {
1030 return TypeNode(mkTypeConst
<TypeConstant
>(REGEXP_TYPE
));
1033 /** Get the (singleton) type for rounding modes. */
1034 inline TypeNode
NodeManager::roundingModeType() {
1035 return TypeNode(mkTypeConst
<TypeConstant
>(ROUNDINGMODE_TYPE
));
1038 /** Get the bound var list type. */
1039 inline TypeNode
NodeManager::boundVarListType() {
1040 return TypeNode(mkTypeConst
<TypeConstant
>(BOUND_VAR_LIST_TYPE
));
1043 /** Get the instantiation pattern type. */
1044 inline TypeNode
NodeManager::instPatternType() {
1045 return TypeNode(mkTypeConst
<TypeConstant
>(INST_PATTERN_TYPE
));
1048 /** Get the instantiation pattern type. */
1049 inline TypeNode
NodeManager::instPatternListType() {
1050 return TypeNode(mkTypeConst
<TypeConstant
>(INST_PATTERN_LIST_TYPE
));
1053 /** Get the (singleton) type for builtin operators. */
1054 inline TypeNode
NodeManager::builtinOperatorType() {
1055 return TypeNode(mkTypeConst
<TypeConstant
>(BUILTIN_OPERATOR_TYPE
));
1058 /** Make a function type from domain to range. */
1059 inline TypeNode
NodeManager::mkFunctionType(const TypeNode
& domain
, const TypeNode
& range
) {
1060 std::vector
<TypeNode
> sorts
;
1061 sorts
.push_back(domain
);
1062 sorts
.push_back(range
);
1063 return mkFunctionType(sorts
);
1066 inline TypeNode
NodeManager::mkFunctionType(const std::vector
<TypeNode
>& argTypes
, const TypeNode
& range
) {
1067 Assert(argTypes
.size() >= 1);
1068 std::vector
<TypeNode
> sorts(argTypes
);
1069 sorts
.push_back(range
);
1070 return mkFunctionType(sorts
);
1074 NodeManager::mkFunctionType(const std::vector
<TypeNode
>& sorts
) {
1075 Assert(sorts
.size() >= 2);
1076 std::vector
<TypeNode
> sortNodes
;
1077 for (unsigned i
= 0; i
< sorts
.size(); ++ i
) {
1078 CheckArgument(!sorts
[i
].isFunctionLike(), sorts
,
1079 "cannot create higher-order function types");
1080 sortNodes
.push_back(sorts
[i
]);
1082 return mkTypeNode(kind::FUNCTION_TYPE
, sortNodes
);
1086 NodeManager::mkPredicateType(const std::vector
<TypeNode
>& sorts
) {
1087 Assert(sorts
.size() >= 1);
1088 std::vector
<TypeNode
> sortNodes
;
1089 for (unsigned i
= 0; i
< sorts
.size(); ++ i
) {
1090 CheckArgument(!sorts
[i
].isFunctionLike(), sorts
,
1091 "cannot create higher-order function types");
1092 sortNodes
.push_back(sorts
[i
]);
1094 sortNodes
.push_back(booleanType());
1095 return mkTypeNode(kind::FUNCTION_TYPE
, sortNodes
);
1098 inline TypeNode
NodeManager::mkSExprType(const std::vector
<TypeNode
>& types
) {
1099 std::vector
<TypeNode
> typeNodes
;
1100 for (unsigned i
= 0; i
< types
.size(); ++ i
) {
1101 typeNodes
.push_back(types
[i
]);
1103 return mkTypeNode(kind::SEXPR_TYPE
, typeNodes
);
1106 inline TypeNode
NodeManager::mkBitVectorType(unsigned size
) {
1107 return TypeNode(mkTypeConst
<BitVectorSize
>(BitVectorSize(size
)));
1110 inline TypeNode
NodeManager::mkFloatingPointType(unsigned exp
, unsigned sig
) {
1111 return TypeNode(mkTypeConst
<FloatingPointSize
>(FloatingPointSize(exp
,sig
)));
1114 inline TypeNode
NodeManager::mkFloatingPointType(FloatingPointSize fs
) {
1115 return TypeNode(mkTypeConst
<FloatingPointSize
>(fs
));
1118 inline TypeNode
NodeManager::mkArrayType(TypeNode indexType
,
1119 TypeNode constituentType
) {
1120 CheckArgument(!indexType
.isNull(), indexType
,
1121 "unexpected NULL index type");
1122 CheckArgument(!constituentType
.isNull(), constituentType
,
1123 "unexpected NULL constituent type");
1124 CheckArgument(!indexType
.isFunctionLike(), indexType
,
1125 "cannot index arrays by a function-like type");
1126 CheckArgument(!constituentType
.isFunctionLike(), constituentType
,
1127 "cannot store function-like types in arrays");
1128 Debug("arrays") << "making array type " << indexType
<< " "
1129 << constituentType
<< std::endl
;
1130 return mkTypeNode(kind::ARRAY_TYPE
, indexType
, constituentType
);
1133 inline TypeNode
NodeManager::mkSetType(TypeNode elementType
) {
1134 CheckArgument(!elementType
.isNull(), elementType
,
1135 "unexpected NULL element type");
1136 // TODO: Confirm meaning of isFunctionLike(). --K
1137 CheckArgument(!elementType
.isFunctionLike(), elementType
,
1138 "cannot store function-like types in sets");
1139 Debug("sets") << "making sets type " << elementType
<< std::endl
;
1140 return mkTypeNode(kind::SET_TYPE
, elementType
);
1143 inline TypeNode
NodeManager::mkSelectorType(TypeNode domain
, TypeNode range
) {
1144 CheckArgument(!domain
.isFunctionLike(), domain
,
1145 "cannot create higher-order function types");
1146 CheckArgument(!range
.isFunctionLike(), range
,
1147 "cannot create higher-order function types");
1148 return mkTypeNode(kind::SELECTOR_TYPE
, domain
, range
);
1151 inline TypeNode
NodeManager::mkTesterType(TypeNode domain
) {
1152 CheckArgument(!domain
.isFunctionLike(), domain
,
1153 "cannot create higher-order function types");
1154 return mkTypeNode(kind::TESTER_TYPE
, domain
);
1157 inline expr::NodeValue
* NodeManager::poolLookup(expr::NodeValue
* nv
) const {
1158 NodeValuePool::const_iterator find
= d_nodeValuePool
.find(nv
);
1159 if(find
== d_nodeValuePool
.end()) {
1166 inline void NodeManager::poolInsert(expr::NodeValue
* nv
) {
1167 Assert(d_nodeValuePool
.find(nv
) == d_nodeValuePool
.end(),
1168 "NodeValue already in the pool!");
1169 d_nodeValuePool
.insert(nv
);// FIXME multithreading
1172 inline void NodeManager::poolRemove(expr::NodeValue
* nv
) {
1173 Assert(d_nodeValuePool
.find(nv
) != d_nodeValuePool
.end(),
1174 "NodeValue is not in the pool!");
1176 d_nodeValuePool
.erase(nv
);// FIXME multithreading
1179 inline Expr
NodeManager::toExpr(TNode n
) {
1180 return Expr(d_exprManager
, new Node(n
));
1183 inline Node
NodeManager::fromExpr(const Expr
& e
) {
1187 inline ExprManager
* NodeManager::toExprManager() {
1188 return d_exprManager
;
1191 inline NodeManager
* NodeManager::fromExprManager(ExprManager
* exprManager
) {
1192 return exprManager
->getNodeManager();
1195 inline Type
NodeManager::toType(TypeNode tn
) {
1196 return Type(this, new TypeNode(tn
));
1199 inline TypeNode
NodeManager::fromType(Type t
) {
1200 return *Type::getTypeNode(t
);
1203 }/* CVC4 namespace */
1205 #define __CVC4__NODE_MANAGER_NEEDS_CONSTANT_MAP
1206 #include "expr/metakind.h"
1207 #undef __CVC4__NODE_MANAGER_NEEDS_CONSTANT_MAP
1209 #include "expr/node_builder.h"
1213 // general expression-builders
1215 inline bool NodeManager::hasOperator(Kind k
) {
1216 switch(kind::MetaKind mk
= kind::metaKindOf(k
)) {
1218 case kind::metakind::INVALID
:
1219 case kind::metakind::VARIABLE
:
1220 case kind::metakind::NULLARY_OPERATOR
:
1223 case kind::metakind::OPERATOR
:
1224 case kind::metakind::PARAMETERIZED
:
1227 case kind::metakind::CONSTANT
:
1235 inline Kind
NodeManager::operatorToKind(TNode n
) {
1236 return kind::operatorToKind(n
.d_nv
);
1239 inline Node
NodeManager::mkNode(Kind kind
, TNode child1
) {
1240 NodeBuilder
<1> nb(this, kind
);
1242 return nb
.constructNode();
1245 inline Node
* NodeManager::mkNodePtr(Kind kind
, TNode child1
) {
1246 NodeBuilder
<1> nb(this, kind
);
1248 return nb
.constructNodePtr();
1251 inline Node
NodeManager::mkNode(Kind kind
, TNode child1
, TNode child2
) {
1252 NodeBuilder
<2> nb(this, kind
);
1253 nb
<< child1
<< child2
;
1254 return nb
.constructNode();
1257 inline Node
* NodeManager::mkNodePtr(Kind kind
, TNode child1
, TNode child2
) {
1258 NodeBuilder
<2> nb(this, kind
);
1259 nb
<< child1
<< child2
;
1260 return nb
.constructNodePtr();
1263 inline Node
NodeManager::mkNode(Kind kind
, TNode child1
, TNode child2
,
1265 NodeBuilder
<3> nb(this, kind
);
1266 nb
<< child1
<< child2
<< child3
;
1267 return nb
.constructNode();
1270 inline Node
* NodeManager::mkNodePtr(Kind kind
, TNode child1
, TNode child2
,
1272 NodeBuilder
<3> nb(this, kind
);
1273 nb
<< child1
<< child2
<< child3
;
1274 return nb
.constructNodePtr();
1277 inline Node
NodeManager::mkNode(Kind kind
, TNode child1
, TNode child2
,
1278 TNode child3
, TNode child4
) {
1279 NodeBuilder
<4> nb(this, kind
);
1280 nb
<< child1
<< child2
<< child3
<< child4
;
1281 return nb
.constructNode();
1284 inline Node
* NodeManager::mkNodePtr(Kind kind
, TNode child1
, TNode child2
,
1285 TNode child3
, TNode child4
) {
1286 NodeBuilder
<4> nb(this, kind
);
1287 nb
<< child1
<< child2
<< child3
<< child4
;
1288 return nb
.constructNodePtr();
1291 inline Node
NodeManager::mkNode(Kind kind
, TNode child1
, TNode child2
,
1292 TNode child3
, TNode child4
, TNode child5
) {
1293 NodeBuilder
<5> nb(this, kind
);
1294 nb
<< child1
<< child2
<< child3
<< child4
<< child5
;
1295 return nb
.constructNode();
1298 inline Node
* NodeManager::mkNodePtr(Kind kind
, TNode child1
, TNode child2
,
1299 TNode child3
, TNode child4
, TNode child5
) {
1300 NodeBuilder
<5> nb(this, kind
);
1301 nb
<< child1
<< child2
<< child3
<< child4
<< child5
;
1302 return nb
.constructNodePtr();
1306 template <bool ref_count
>
1307 inline Node
NodeManager::mkNode(Kind kind
,
1308 const std::vector
<NodeTemplate
<ref_count
> >&
1310 NodeBuilder
<> nb(this, kind
);
1311 nb
.append(children
);
1312 return nb
.constructNode();
1315 template <bool ref_count
>
1316 inline Node
* NodeManager::mkNodePtr(Kind kind
,
1317 const std::vector
<NodeTemplate
<ref_count
> >&
1319 NodeBuilder
<> nb(this, kind
);
1320 nb
.append(children
);
1321 return nb
.constructNodePtr();
1325 inline Node
NodeManager::mkNode(TNode opNode
) {
1326 NodeBuilder
<1> nb(this, operatorToKind(opNode
));
1327 if(opNode
.getKind() != kind::BUILTIN
) {
1330 return nb
.constructNode();
1333 inline Node
* NodeManager::mkNodePtr(TNode opNode
) {
1334 NodeBuilder
<1> nb(this, operatorToKind(opNode
));
1335 if(opNode
.getKind() != kind::BUILTIN
) {
1338 return nb
.constructNodePtr();
1341 inline Node
NodeManager::mkNode(TNode opNode
, TNode child1
) {
1342 NodeBuilder
<2> nb(this, operatorToKind(opNode
));
1343 if(opNode
.getKind() != kind::BUILTIN
) {
1347 return nb
.constructNode();
1350 inline Node
* NodeManager::mkNodePtr(TNode opNode
, TNode child1
) {
1351 NodeBuilder
<2> nb(this, operatorToKind(opNode
));
1352 if(opNode
.getKind() != kind::BUILTIN
) {
1356 return nb
.constructNodePtr();
1359 inline Node
NodeManager::mkNode(TNode opNode
, TNode child1
, TNode child2
) {
1360 NodeBuilder
<3> nb(this, operatorToKind(opNode
));
1361 if(opNode
.getKind() != kind::BUILTIN
) {
1364 nb
<< child1
<< child2
;
1365 return nb
.constructNode();
1368 inline Node
* NodeManager::mkNodePtr(TNode opNode
, TNode child1
, TNode child2
) {
1369 NodeBuilder
<3> nb(this, operatorToKind(opNode
));
1370 if(opNode
.getKind() != kind::BUILTIN
) {
1373 nb
<< child1
<< child2
;
1374 return nb
.constructNodePtr();
1377 inline Node
NodeManager::mkNode(TNode opNode
, TNode child1
, TNode child2
,
1379 NodeBuilder
<4> nb(this, operatorToKind(opNode
));
1380 if(opNode
.getKind() != kind::BUILTIN
) {
1383 nb
<< child1
<< child2
<< child3
;
1384 return nb
.constructNode();
1387 inline Node
* NodeManager::mkNodePtr(TNode opNode
, TNode child1
, TNode child2
,
1389 NodeBuilder
<4> nb(this, operatorToKind(opNode
));
1390 if(opNode
.getKind() != kind::BUILTIN
) {
1393 nb
<< child1
<< child2
<< child3
;
1394 return nb
.constructNodePtr();
1397 inline Node
NodeManager::mkNode(TNode opNode
, TNode child1
, TNode child2
,
1398 TNode child3
, TNode child4
) {
1399 NodeBuilder
<5> nb(this, operatorToKind(opNode
));
1400 if(opNode
.getKind() != kind::BUILTIN
) {
1403 nb
<< child1
<< child2
<< child3
<< child4
;
1404 return nb
.constructNode();
1407 inline Node
* NodeManager::mkNodePtr(TNode opNode
, TNode child1
, TNode child2
,
1408 TNode child3
, TNode child4
) {
1409 NodeBuilder
<5> nb(this, operatorToKind(opNode
));
1410 if(opNode
.getKind() != kind::BUILTIN
) {
1413 nb
<< child1
<< child2
<< child3
<< child4
;
1414 return nb
.constructNodePtr();
1417 inline Node
NodeManager::mkNode(TNode opNode
, TNode child1
, TNode child2
,
1418 TNode child3
, TNode child4
, TNode child5
) {
1419 NodeBuilder
<6> nb(this, operatorToKind(opNode
));
1420 if(opNode
.getKind() != kind::BUILTIN
) {
1423 nb
<< child1
<< child2
<< child3
<< child4
<< child5
;
1424 return nb
.constructNode();
1427 inline Node
* NodeManager::mkNodePtr(TNode opNode
, TNode child1
, TNode child2
,
1428 TNode child3
, TNode child4
, TNode child5
) {
1429 NodeBuilder
<6> nb(this, operatorToKind(opNode
));
1430 if(opNode
.getKind() != kind::BUILTIN
) {
1433 nb
<< child1
<< child2
<< child3
<< child4
<< child5
;
1434 return nb
.constructNodePtr();
1437 // N-ary version for operators
1438 template <bool ref_count
>
1439 inline Node
NodeManager::mkNode(TNode opNode
,
1440 const std::vector
<NodeTemplate
<ref_count
> >&
1442 NodeBuilder
<> nb(this, operatorToKind(opNode
));
1443 if(opNode
.getKind() != kind::BUILTIN
) {
1446 nb
.append(children
);
1447 return nb
.constructNode();
1450 template <bool ref_count
>
1451 inline Node
* NodeManager::mkNodePtr(TNode opNode
,
1452 const std::vector
<NodeTemplate
<ref_count
> >&
1454 NodeBuilder
<> nb(this, operatorToKind(opNode
));
1455 if(opNode
.getKind() != kind::BUILTIN
) {
1458 nb
.append(children
);
1459 return nb
.constructNodePtr();
1463 inline TypeNode
NodeManager::mkTypeNode(Kind kind
, TypeNode child1
) {
1464 return (NodeBuilder
<1>(this, kind
) << child1
).constructTypeNode();
1467 inline TypeNode
NodeManager::mkTypeNode(Kind kind
, TypeNode child1
,
1469 return (NodeBuilder
<2>(this, kind
) << child1
<< child2
).constructTypeNode();
1472 inline TypeNode
NodeManager::mkTypeNode(Kind kind
, TypeNode child1
,
1473 TypeNode child2
, TypeNode child3
) {
1474 return (NodeBuilder
<3>(this, kind
) << child1
<< child2
<< child3
).constructTypeNode();
1477 // N-ary version for types
1478 inline TypeNode
NodeManager::mkTypeNode(Kind kind
,
1479 const std::vector
<TypeNode
>& children
) {
1480 return NodeBuilder
<>(this, kind
).append(children
).constructTypeNode();
1484 Node
NodeManager::mkConst(const T
& val
) {
1485 return mkConstInternal
<Node
, T
>(val
);
1489 TypeNode
NodeManager::mkTypeConst(const T
& val
) {
1490 return mkConstInternal
<TypeNode
, T
>(val
);
1493 template <class NodeClass
, class T
>
1494 NodeClass
NodeManager::mkConstInternal(const T
& val
) {
1496 // typedef typename kind::metakind::constantMap<T>::OwningTheory theory_t;
1497 NVStorage
<1> nvStorage
;
1498 expr::NodeValue
& nvStack
= reinterpret_cast<expr::NodeValue
&>(nvStorage
);
1501 nvStack
.d_kind
= kind::metakind::ConstantMap
<T
>::kind
;
1503 nvStack
.d_nchildren
= 1;
1505 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
1506 #pragma GCC diagnostic push
1507 #pragma GCC diagnostic ignored "-Warray-bounds"
1510 nvStack
.d_children
[0] =
1511 const_cast<expr::NodeValue
*>(reinterpret_cast<const expr::NodeValue
*>(&val
));
1512 expr::NodeValue
* nv
= poolLookup(&nvStack
);
1514 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
1515 #pragma GCC diagnostic pop
1519 return NodeClass(nv
);
1522 nv
= (expr::NodeValue
*)
1523 std::malloc(sizeof(expr::NodeValue
) + sizeof(T
));
1525 throw std::bad_alloc();
1528 nv
->d_nchildren
= 0;
1529 nv
->d_kind
= kind::metakind::ConstantMap
<T
>::kind
;
1530 nv
->d_id
= next_id
++;// FIXME multithreading
1533 //OwningTheory::mkConst(val);
1534 new (&nv
->d_children
) T(val
);
1537 if(Debug
.isOn("gc")) {
1538 Debug("gc") << "creating node value " << nv
1539 << " [" << nv
->d_id
<< "]: ";
1540 nv
->printAst(Debug("gc"));
1541 Debug("gc") << std::endl
;
1544 return NodeClass(nv
);
1547 }/* CVC4 namespace */
1549 #endif /* __CVC4__NODE_MANAGER_H */