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-2018 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>
35 #include "expr/kind.h"
36 #include "expr/metakind.h"
37 #include "expr/node_value.h"
38 #include "options/options.h"
42 class StatisticsRegistry
;
43 class ResourceManager
;
47 class AttributeUniqueId
;
48 class AttributeManager
;
49 }/* CVC4::expr::attr namespace */
52 }/* CVC4::expr namespace */
55 * An interface that an interested party can implement and then subscribe
56 * to NodeManager events via NodeManager::subscribeEvents(this).
58 class NodeManagerListener
{
60 virtual ~NodeManagerListener() {}
61 virtual void nmNotifyNewSort(TypeNode tn
, uint32_t flags
) {}
62 virtual void nmNotifyNewSortConstructor(TypeNode tn
, uint32_t flags
) {}
63 virtual void nmNotifyInstantiateSortConstructor(TypeNode ctor
, TypeNode sort
,
65 virtual void nmNotifyNewDatatypes(
66 const std::vector
<DatatypeType
>& datatypes
) {}
67 virtual void nmNotifyNewVar(TNode n
, uint32_t flags
) {}
68 virtual void nmNotifyNewSkolem(TNode n
, const std::string
& comment
,
71 * Notify a listener of a Node that's being GCed. If this function stores a
73 * to the Node somewhere, very bad things will happen.
75 virtual void nmNotifyDeleteNode(TNode n
) {}
76 }; /* class NodeManagerListener */
79 template <unsigned nchild_thresh
> friend class CVC4::NodeBuilder
;
80 friend class NodeManagerScope
;
81 friend class expr::NodeValue
;
82 friend class expr::TypeChecker
;
84 // friends so they can access mkVar() here, which is private
85 friend Expr
ExprManager::mkVar(const std::string
&, Type
, uint32_t flags
);
86 friend Expr
ExprManager::mkVar(Type
, uint32_t flags
);
88 // friend so it can access NodeManager's d_listeners and notify clients
89 friend std::vector
<DatatypeType
> ExprManager::mkMutualDatatypeTypes(std::vector
<Datatype
>&, std::set
<Type
>&);
91 /** Predicate for use with STL algorithms */
92 struct NodeValueReferenceCountNonZero
{
93 bool operator()(expr::NodeValue
* nv
) { return nv
->d_rc
> 0; }
96 typedef std::unordered_set
<expr::NodeValue
*,
97 expr::NodeValuePoolHashFunction
,
98 expr::NodeValuePoolEq
> NodeValuePool
;
99 typedef std::unordered_set
<expr::NodeValue
*,
100 expr::NodeValueIDHashFunction
,
101 expr::NodeValueIDEquality
> NodeValueIDSet
;
103 static thread_local NodeManager
* s_current
;
106 StatisticsRegistry
* d_statisticsRegistry
;
108 ResourceManager
* d_resourceManager
;
111 * A list of registrations on d_options to that call into d_resourceManager.
112 * These must be garbage collected before d_options and d_resourceManager.
114 ListenerRegistrationList
* d_registrations
;
116 NodeValuePool d_nodeValuePool
;
120 expr::attr::AttributeManager
* d_attrManager
;
122 /** The associated ExprManager */
123 ExprManager
* d_exprManager
;
126 * The node value we're currently freeing. This unique node value
127 * is permitted to have outstanding TNodes to it (in "soft"
128 * contexts, like as a key in attribute tables), even though
129 * normally it's an error to have a TNode to a node value with a
130 * reference count of 0. Being "under deletion" also enables
131 * assertions that inc() is not called on it.
133 expr::NodeValue
* d_nodeUnderDeletion
;
136 * True iff we are in reclaimZombies(). This avoids unnecessary
137 * recursion; a NodeValue being deleted might zombify other
138 * NodeValues, but these shouldn't trigger a (recursive) call to
141 bool d_inReclaimZombies
;
144 * The set of zombie nodes. We may want to revisit this design, as
145 * we might like to delete nodes in least-recently-used order. But
146 * we also need to avoid processing a zombie twice.
148 NodeValueIDSet d_zombies
;
151 * NodeValues with maxed out reference counts. These live as long as the
152 * NodeManager. They have a custom deallocation procedure at the very end.
154 std::vector
<expr::NodeValue
*> d_maxedOut
;
157 * A set of operator singletons (w.r.t. to this NodeManager
158 * instance) for operators. Conceptually, Nodes with kind, say,
159 * PLUS, are APPLYs of a PLUS operator to arguments. This array
160 * holds the set of operators for these things. A PLUS operator is
161 * a Node with kind "BUILTIN", and if you call
162 * plusOperator->getConst<CVC4::Kind>(), you get kind::PLUS back.
164 Node d_operators
[kind::LAST_KIND
];
166 /** unique vars per (Kind,Type) */
167 std::map
< Kind
, std::map
< TypeNode
, Node
> > d_unique_vars
;
170 * A list of subscribers for NodeManager events.
172 std::vector
<NodeManagerListener
*> d_listeners
;
174 /** A list of datatypes owned by this node manager. */
175 std::vector
<Datatype
*> d_ownedDatatypes
;
178 * A map of tuple and record types to their corresponding datatype.
180 class TupleTypeCache
{
182 std::map
< TypeNode
, TupleTypeCache
> d_children
;
184 TypeNode
getTupleType( NodeManager
* nm
, std::vector
< TypeNode
>& types
, unsigned index
= 0 );
188 std::map
< TypeNode
, std::map
< std::string
, RecTypeCache
> > d_children
;
190 TypeNode
getRecordType( NodeManager
* nm
, const Record
& rec
, unsigned index
= 0 );
192 TupleTypeCache d_tt_cache
;
193 RecTypeCache d_rt_cache
;
196 * Keep a count of all abstract values produced by this NodeManager.
197 * Abstract values have a type attribute, so if multiple SmtEngines
198 * are attached to this NodeManager, we don't want their abstract
201 unsigned d_abstractValueCount
;
204 * A counter used to produce unique skolem names.
206 * Note that it is NOT incremented when skolems are created using
207 * SKOLEM_EXACT_NAME, so it is NOT a count of the skolems produced
208 * by this node manager.
210 unsigned d_skolemCounter
;
213 * Look up a NodeValue in the pool associated to this NodeManager.
214 * The NodeValue argument need not be a "completely-constructed"
215 * NodeValue. In particular, "non-inlined" constants are permitted
218 * For non-CONSTANT metakinds, nv's d_kind and d_nchildren should be
219 * correctly set, and d_children[0..n-1] should be valid (extant)
220 * NodeValues for lookup.
222 * For CONSTANT metakinds, nv's d_kind should be set correctly.
223 * Normally a CONSTANT would have d_nchildren == 0 and the constant
224 * value inlined in the d_children space. However, here we permit
225 * "non-inlined" NodeValues to avoid unnecessary copying. For
226 * these, d_nchildren == 1, and d_nchildren is a pointer to the
229 * The point of this complex design is to permit efficient lookups
230 * (without fully constructing a NodeValue). In the case that the
231 * argument is not fully constructed, and this function returns
232 * NULL, the caller should fully construct an equivalent one before
233 * calling poolInsert(). NON-FULLY-CONSTRUCTED NODEVALUES are not
234 * permitted in the pool!
236 inline expr::NodeValue
* poolLookup(expr::NodeValue
* nv
) const;
239 * Insert a NodeValue into the NodeManager's pool.
241 * It is an error to insert a NodeValue already in the pool.
242 * Enquire first with poolLookup().
244 inline void poolInsert(expr::NodeValue
* nv
);
247 * Remove a NodeValue from the NodeManager's pool.
249 * It is an error to request the removal of a NodeValue from the
250 * pool that is not in the pool.
252 inline void poolRemove(expr::NodeValue
* nv
);
255 * Determine if nv is currently being deleted by the NodeManager.
257 inline bool isCurrentlyDeleting(const expr::NodeValue
* nv
) const {
258 return d_nodeUnderDeletion
== nv
;
262 * Register a NodeValue as a zombie.
264 inline void markForDeletion(expr::NodeValue
* nv
) {
265 Assert(nv
->d_rc
== 0);
267 // if d_reclaiming is set, make sure we don't call
268 // reclaimZombies(), because it's already running.
269 if(Debug
.isOn("gc")) {
270 Debug("gc") << "zombifying node value " << nv
271 << " [" << nv
->d_id
<< "]: ";
272 nv
->printAst(Debug("gc"));
273 Debug("gc") << (d_inReclaimZombies
? " [CURRENTLY-RECLAIMING]" : "")
276 d_zombies
.insert(nv
); // FIXME multithreading
278 if(safeToReclaimZombies()) {
279 if(d_zombies
.size() > 5000) {
286 * Register a NodeValue as having a maxed out reference count. This NodeValue
287 * will live as long as its containing NodeManager.
289 inline void markRefCountMaxedOut(expr::NodeValue
* nv
) {
290 Assert(nv
->HasMaximizedReferenceCount());
291 if(Debug
.isOn("gc")) {
292 Debug("gc") << "marking node value " << nv
293 << " [" << nv
->d_id
<< "]: as maxed out" << std::endl
;
295 d_maxedOut
.push_back(nv
);
299 * Reclaim all zombies.
301 void reclaimZombies();
304 * It is safe to collect zombies.
306 bool safeToReclaimZombies() const;
309 * Returns a reverse topological sort of a list of NodeValues. The NodeValues
310 * must be valid and have ids. The NodeValues are not modified (including ref
313 static std::vector
<expr::NodeValue
*> TopologicalSort(
314 const std::vector
<expr::NodeValue
*>& roots
);
317 * This template gives a mechanism to stack-allocate a NodeValue
318 * with enough space for N children (where N is a compile-time
319 * constant). You use it like this:
321 * NVStorage<4> nvStorage;
322 * NodeValue& nvStack = reinterpret_cast<NodeValue&>(nvStorage);
324 * ...and then you can use nvStack as a NodeValue that you know has
325 * room for 4 children.
330 expr::NodeValue
* child
[N
];
331 };/* struct NodeManager::NVStorage<N> */
333 /* A note on isAtomic() and isAtomicFormula() (in CVC3 parlance)..
335 * It has been decided for now to hold off on implementations of
336 * these functions, as they may only be needed in CNF conversion,
337 * where it's pointless to do a lazy isAtomic determination by
338 * searching through the DAG, and storing it, since the result will
339 * only be used once. For more details see the 4/27/2010 CVC4
340 * developer's meeting notes at:
342 * http://goedel.cims.nyu.edu/wiki/Meeting_Minutes_-_April_27,_2010#isAtomic.28.29_and_isAtomicFormula.28.29
344 // bool containsDecision(TNode); // is "atomic"
345 // bool properlyContainsDecision(TNode); // all children are atomic
347 // undefined private copy constructor (disallow copy)
348 NodeManager(const NodeManager
&) = delete;
350 NodeManager
& operator=(const NodeManager
&) = delete;
355 * Create a variable with the given name and type. NOTE that no
356 * lookup is done on the name. If you mkVar("a", type) and then
357 * mkVar("a", type) again, you have two variables. The NodeManager
358 * version of this is private to avoid internal uses of mkVar() from
359 * within CVC4. Such uses should employ mkSkolem() instead.
361 Node
mkVar(const std::string
& name
, const TypeNode
& type
, uint32_t flags
= ExprManager::VAR_FLAG_NONE
);
362 Node
* mkVarPtr(const std::string
& name
, const TypeNode
& type
, uint32_t flags
= ExprManager::VAR_FLAG_NONE
);
364 /** Create a variable with the given type. */
365 Node
mkVar(const TypeNode
& type
, uint32_t flags
= ExprManager::VAR_FLAG_NONE
);
366 Node
* mkVarPtr(const TypeNode
& type
, uint32_t flags
= ExprManager::VAR_FLAG_NONE
);
370 explicit NodeManager(ExprManager
* exprManager
);
371 explicit NodeManager(ExprManager
* exprManager
, const Options
& options
);
374 /** The node manager in the current public-facing CVC4 library context */
375 static NodeManager
* currentNM() { return s_current
; }
376 /** The resource manager associated with the current node manager */
377 static ResourceManager
* currentResourceManager() { return s_current
->d_resourceManager
; }
379 /** Get this node manager's options (const version) */
380 const Options
& getOptions() const {
384 /** Get this node manager's options (non-const version) */
385 Options
& getOptions() {
389 /** Get this node manager's resource manager */
390 ResourceManager
* getResourceManager() { return d_resourceManager
; }
392 /** Get this node manager's statistics registry */
393 StatisticsRegistry
* getStatisticsRegistry() const
395 return d_statisticsRegistry
;
398 /** Subscribe to NodeManager events */
399 void subscribeEvents(NodeManagerListener
* listener
) {
400 Assert(std::find(d_listeners
.begin(), d_listeners
.end(), listener
) == d_listeners
.end(), "listener already subscribed");
401 d_listeners
.push_back(listener
);
404 /** Unsubscribe from NodeManager events */
405 void unsubscribeEvents(NodeManagerListener
* listener
) {
406 std::vector
<NodeManagerListener
*>::iterator elt
= std::find(d_listeners
.begin(), d_listeners
.end(), listener
);
407 Assert(elt
!= d_listeners
.end(), "listener not subscribed");
408 d_listeners
.erase(elt
);
411 /** register datatype */
412 unsigned registerDatatype(Datatype
* dt
);
413 /** get datatype for index */
414 const Datatype
& getDatatypeForIndex( unsigned index
) const;
416 /** Get a Kind from an operator expression */
417 static inline Kind
operatorToKind(TNode n
);
419 // general expression-builders
421 /** Create a node with one child. */
422 Node
mkNode(Kind kind
, TNode child1
);
423 Node
* mkNodePtr(Kind kind
, TNode child1
);
425 /** Create a node with two children. */
426 Node
mkNode(Kind kind
, TNode child1
, TNode child2
);
427 Node
* mkNodePtr(Kind kind
, TNode child1
, TNode child2
);
429 /** Create a node with three children. */
430 Node
mkNode(Kind kind
, TNode child1
, TNode child2
, TNode child3
);
431 Node
* mkNodePtr(Kind kind
, TNode child1
, TNode child2
, TNode child3
);
433 /** Create a node with four children. */
434 Node
mkNode(Kind kind
, TNode child1
, TNode child2
, TNode child3
,
436 Node
* mkNodePtr(Kind kind
, TNode child1
, TNode child2
, TNode child3
,
439 /** Create a node with five children. */
440 Node
mkNode(Kind kind
, TNode child1
, TNode child2
, TNode child3
,
441 TNode child4
, TNode child5
);
442 Node
* mkNodePtr(Kind kind
, TNode child1
, TNode child2
, TNode child3
,
443 TNode child4
, TNode child5
);
445 /** Create a node with an arbitrary number of children. */
446 template <bool ref_count
>
447 Node
mkNode(Kind kind
, const std::vector
<NodeTemplate
<ref_count
> >& children
);
448 template <bool ref_count
>
449 Node
* mkNodePtr(Kind kind
, const std::vector
<NodeTemplate
<ref_count
> >& children
);
451 /** Create a node (with no children) by operator. */
452 Node
mkNode(TNode opNode
);
453 Node
* mkNodePtr(TNode opNode
);
455 /** Create a node with one child by operator. */
456 Node
mkNode(TNode opNode
, TNode child1
);
457 Node
* mkNodePtr(TNode opNode
, TNode child1
);
459 /** Create a node with two children by operator. */
460 Node
mkNode(TNode opNode
, TNode child1
, TNode child2
);
461 Node
* mkNodePtr(TNode opNode
, TNode child1
, TNode child2
);
463 /** Create a node with three children by operator. */
464 Node
mkNode(TNode opNode
, TNode child1
, TNode child2
, TNode child3
);
465 Node
* mkNodePtr(TNode opNode
, TNode child1
, TNode child2
, TNode child3
);
467 /** Create a node with four children by operator. */
468 Node
mkNode(TNode opNode
, TNode child1
, TNode child2
, TNode child3
,
470 Node
* mkNodePtr(TNode opNode
, TNode child1
, TNode child2
, TNode child3
,
473 /** Create a node with five children by operator. */
474 Node
mkNode(TNode opNode
, TNode child1
, TNode child2
, TNode child3
,
475 TNode child4
, TNode child5
);
476 Node
* mkNodePtr(TNode opNode
, TNode child1
, TNode child2
, TNode child3
,
477 TNode child4
, TNode child5
);
479 /** Create a node by applying an operator to the children. */
480 template <bool ref_count
>
481 Node
mkNode(TNode opNode
, const std::vector
<NodeTemplate
<ref_count
> >& children
);
482 template <bool ref_count
>
483 Node
* mkNodePtr(TNode opNode
, const std::vector
<NodeTemplate
<ref_count
> >& children
);
485 Node
mkBoundVar(const std::string
& name
, const TypeNode
& type
);
486 Node
* mkBoundVarPtr(const std::string
& name
, const TypeNode
& type
);
488 Node
mkBoundVar(const TypeNode
& type
);
489 Node
* mkBoundVarPtr(const TypeNode
& type
);
491 /** get the canonical bound variable list for function type tn */
492 static Node
getBoundVarListForFunctionType( TypeNode tn
);
495 * Optional flags used to control behavior of NodeManager::mkSkolem().
496 * They should be composed with a bitwise OR (e.g.,
497 * "SKOLEM_NO_NOTIFY | SKOLEM_EXACT_NAME"). Of course, SKOLEM_DEFAULT
498 * cannot be composed in such a manner.
501 SKOLEM_DEFAULT
= 0, /**< default behavior */
502 SKOLEM_NO_NOTIFY
= 1, /**< do not notify subscribers */
503 SKOLEM_EXACT_NAME
= 2,/**< do not make the name unique by adding the id */
504 SKOLEM_IS_GLOBAL
= 4 /**< global vars appear in models even after a pop */
505 };/* enum SkolemFlags */
508 * Create a skolem constant with the given name, type, and comment.
510 * @param prefix the name of the new skolem variable is the prefix
511 * appended with a unique ID. This way a family of skolem variables
512 * can be made with unique identifiers, used in dump, tracing, and
513 * debugging output. Use SKOLEM_EXECT_NAME flag if you don't want
514 * a unique ID appended and use prefix as the name.
516 * @param type the type of the skolem variable to create
518 * @param comment a comment for dumping output; if declarations are
519 * being dumped, this is included in a comment before the declaration
520 * and can be quite useful for debugging
522 * @param flags an optional mask of bits from SkolemFlags to control
523 * mkSkolem() behavior
525 Node
mkSkolem(const std::string
& prefix
, const TypeNode
& type
,
526 const std::string
& comment
= "", int flags
= SKOLEM_DEFAULT
);
528 /** Create a instantiation constant with the given type. */
529 Node
mkInstConstant(const TypeNode
& type
);
531 /** Create a boolean term variable. */
532 Node
mkBooleanTermVariable();
534 /** Make a new abstract value with the given type. */
535 Node
mkAbstractValue(const TypeNode
& type
);
537 /** make unique (per Type,Kind) variable. */
538 Node
mkNullaryOperator(const TypeNode
& type
, Kind k
);
541 * Create a constant of type T. It will have the appropriate
542 * CONST_* kind defined for T.
545 Node
mkConst(const T
&);
548 TypeNode
mkTypeConst(const T
&);
550 template <class NodeClass
, class T
>
551 NodeClass
mkConstInternal(const T
&);
553 /** Create a node with children. */
554 TypeNode
mkTypeNode(Kind kind
, TypeNode child1
);
555 TypeNode
mkTypeNode(Kind kind
, TypeNode child1
, TypeNode child2
);
556 TypeNode
mkTypeNode(Kind kind
, TypeNode child1
, TypeNode child2
,
558 TypeNode
mkTypeNode(Kind kind
, const std::vector
<TypeNode
>& children
);
561 * Determine whether Nodes of a particular Kind have operators.
562 * @returns true if Nodes of Kind k have operators.
564 static inline bool hasOperator(Kind k
);
567 * Get the (singleton) operator of an OPERATOR-kinded kind. The
568 * returned node n will have kind BUILTIN, and calling
569 * n.getConst<CVC4::Kind>() will yield k.
571 inline TNode
operatorOf(Kind k
) {
572 AssertArgument( kind::metaKindOf(k
) == kind::metakind::OPERATOR
, k
,
573 "Kind is not an OPERATOR-kinded kind "
574 "in NodeManager::operatorOf()" );
575 return d_operators
[k
];
579 * Retrieve an attribute for a node.
581 * @param nv the node value
582 * @param attr an instance of the attribute kind to retrieve.
583 * @returns the attribute, if set, or a default-constructed
584 * <code>AttrKind::value_type</code> if not.
586 template <class AttrKind
>
587 inline typename
AttrKind::value_type
getAttribute(expr::NodeValue
* nv
,
588 const AttrKind
& attr
) const;
591 * Check whether an attribute is set for a node.
593 * @param nv the node value
594 * @param attr an instance of the attribute kind to check
595 * @returns <code>true</code> iff <code>attr</code> is set for
598 template <class AttrKind
>
599 inline bool hasAttribute(expr::NodeValue
* nv
,
600 const AttrKind
& attr
) const;
603 * Check whether an attribute is set for a node, and, if so,
606 * @param nv the node value
607 * @param attr an instance of the attribute kind to check
608 * @param value a reference to an object of the attribute's value type.
609 * <code>value</code> will be set to the value of the attribute, if it is
610 * set for <code>nv</code>; otherwise, it will be set to the default
611 * value of the attribute.
612 * @returns <code>true</code> iff <code>attr</code> is set for
615 template <class AttrKind
>
616 inline bool getAttribute(expr::NodeValue
* nv
,
617 const AttrKind
& attr
,
618 typename
AttrKind::value_type
& value
) const;
621 * Set an attribute for a node. If the node doesn't have the
622 * attribute, this function assigns one. If the node has one, this
625 * @param nv the node value
626 * @param attr an instance of the attribute kind to set
627 * @param value the value of <code>attr</code> for <code>nv</code>
629 template <class AttrKind
>
630 inline void setAttribute(expr::NodeValue
* nv
,
631 const AttrKind
& attr
,
632 const typename
AttrKind::value_type
& value
);
635 * Retrieve an attribute for a TNode.
638 * @param attr an instance of the attribute kind to retrieve.
639 * @returns the attribute, if set, or a default-constructed
640 * <code>AttrKind::value_type</code> if not.
642 template <class AttrKind
>
643 inline typename
AttrKind::value_type
644 getAttribute(TNode n
, const AttrKind
& attr
) const;
647 * Check whether an attribute is set for a TNode.
650 * @param attr an instance of the attribute kind to check
651 * @returns <code>true</code> iff <code>attr</code> is set for <code>n</code>.
653 template <class AttrKind
>
654 inline bool hasAttribute(TNode n
,
655 const AttrKind
& attr
) const;
658 * Check whether an attribute is set for a TNode and, if so, retieve
662 * @param attr an instance of the attribute kind to check
663 * @param value a reference to an object of the attribute's value type.
664 * <code>value</code> will be set to the value of the attribute, if it is
665 * set for <code>nv</code>; otherwise, it will be set to the default value of
667 * @returns <code>true</code> iff <code>attr</code> is set for <code>n</code>.
669 template <class AttrKind
>
670 inline bool getAttribute(TNode n
,
671 const AttrKind
& attr
,
672 typename
AttrKind::value_type
& value
) const;
675 * Set an attribute for a node. If the node doesn't have the
676 * attribute, this function assigns one. If the node has one, this
680 * @param attr an instance of the attribute kind to set
681 * @param value the value of <code>attr</code> for <code>n</code>
683 template <class AttrKind
>
684 inline void setAttribute(TNode n
,
685 const AttrKind
& attr
,
686 const typename
AttrKind::value_type
& value
);
689 * Retrieve an attribute for a TypeNode.
691 * @param n the type node
692 * @param attr an instance of the attribute kind to retrieve.
693 * @returns the attribute, if set, or a default-constructed
694 * <code>AttrKind::value_type</code> if not.
696 template <class AttrKind
>
697 inline typename
AttrKind::value_type
698 getAttribute(TypeNode n
, const AttrKind
& attr
) const;
701 * Check whether an attribute is set for a TypeNode.
703 * @param n the type node
704 * @param attr an instance of the attribute kind to check
705 * @returns <code>true</code> iff <code>attr</code> is set for <code>n</code>.
707 template <class AttrKind
>
708 inline bool hasAttribute(TypeNode n
,
709 const AttrKind
& attr
) const;
712 * Check whether an attribute is set for a TypeNode and, if so, retieve
715 * @param n the type node
716 * @param attr an instance of the attribute kind to check
717 * @param value a reference to an object of the attribute's value type.
718 * <code>value</code> will be set to the value of the attribute, if it is
719 * set for <code>nv</code>; otherwise, it will be set to the default value of
721 * @returns <code>true</code> iff <code>attr</code> is set for <code>n</code>.
723 template <class AttrKind
>
724 inline bool getAttribute(TypeNode n
,
725 const AttrKind
& attr
,
726 typename
AttrKind::value_type
& value
) const;
729 * Set an attribute for a type node. If the node doesn't have the
730 * attribute, this function assigns one. If the type node has one,
731 * this overwrites it.
733 * @param n the type node
734 * @param attr an instance of the attribute kind to set
735 * @param value the value of <code>attr</code> for <code>n</code>
737 template <class AttrKind
>
738 inline void setAttribute(TypeNode n
,
739 const AttrKind
& attr
,
740 const typename
AttrKind::value_type
& value
);
742 /** Get the (singleton) type for Booleans. */
743 inline TypeNode
booleanType();
745 /** Get the (singleton) type for integers. */
746 inline TypeNode
integerType();
748 /** Get the (singleton) type for reals. */
749 inline TypeNode
realType();
751 /** Get the (singleton) type for strings. */
752 inline TypeNode
stringType();
754 /** Get the (singleton) type for RegExp. */
755 inline TypeNode
regExpType();
757 /** Get the (singleton) type for rounding modes. */
758 inline TypeNode
roundingModeType();
760 /** Get the bound var list type. */
761 inline TypeNode
boundVarListType();
763 /** Get the instantiation pattern type. */
764 inline TypeNode
instPatternType();
766 /** Get the instantiation pattern type. */
767 inline TypeNode
instPatternListType();
770 * Get the (singleton) type for builtin operators (that is, the type
771 * of the Node returned from Node::getOperator() when the operator
772 * is built-in, like EQUAL). */
773 inline TypeNode
builtinOperatorType();
776 * Make a function type from domain to range.
778 * @param domain the domain type
779 * @param range the range type
780 * @returns the functional type domain -> range
782 inline TypeNode
mkFunctionType(const TypeNode
& domain
, const TypeNode
& range
);
785 * Make a function type with input types from
786 * argTypes. <code>argTypes</code> must have at least one element.
788 * @param argTypes the domain is a tuple (argTypes[0], ..., argTypes[n])
789 * @param range the range type
790 * @returns the functional type (argTypes[0], ..., argTypes[n]) -> range
792 inline TypeNode
mkFunctionType(const std::vector
<TypeNode
>& argTypes
,
793 const TypeNode
& range
);
796 * Make a function type with input types from
797 * <code>sorts[0..sorts.size()-2]</code> and result type
798 * <code>sorts[sorts.size()-1]</code>. <code>sorts</code> must have
799 * at least 2 elements.
801 inline TypeNode
mkFunctionType(const std::vector
<TypeNode
>& sorts
);
804 * Make a predicate type with input types from
805 * <code>sorts</code>. The result with be a function type with range
806 * <code>BOOLEAN</code>. <code>sorts</code> must have at least one
809 inline TypeNode
mkPredicateType(const std::vector
<TypeNode
>& sorts
);
812 * Make a tuple type with types from
813 * <code>types</code>. <code>types</code> must have at least one
816 * @param types a vector of types
817 * @returns the tuple type (types[0], ..., types[n])
819 TypeNode
mkTupleType(const std::vector
<TypeNode
>& types
);
822 * Make a record type with the description from rec.
824 * @param rec a description of the record
825 * @returns the record type
827 TypeNode
mkRecordType(const Record
& rec
);
830 * Make a symbolic expression type with types from
831 * <code>types</code>. <code>types</code> may have any number of
834 * @param types a vector of types
835 * @returns the symbolic expression type (types[0], ..., types[n])
837 inline TypeNode
mkSExprType(const std::vector
<TypeNode
>& types
);
839 /** Make the type of floating-point with <code>exp</code> bit exponent and
840 <code>sig</code> bit significand */
841 inline TypeNode
mkFloatingPointType(unsigned exp
, unsigned sig
);
842 inline TypeNode
mkFloatingPointType(FloatingPointSize fs
);
844 /** Make the type of bitvectors of size <code>size</code> */
845 inline TypeNode
mkBitVectorType(unsigned size
);
847 /** Make the type of arrays with the given parameterization */
848 inline TypeNode
mkArrayType(TypeNode indexType
, TypeNode constituentType
);
850 /** Make the type of arrays with the given parameterization */
851 inline TypeNode
mkSetType(TypeNode elementType
);
853 /** Make a type representing a constructor with the given parameterization */
854 TypeNode
mkConstructorType(const DatatypeConstructor
& constructor
, TypeNode range
);
856 /** Make a type representing a selector with the given parameterization */
857 inline TypeNode
mkSelectorType(TypeNode domain
, TypeNode range
);
859 /** Make a type representing a tester with given parameterization */
860 inline TypeNode
mkTesterType(TypeNode domain
);
862 /** Make a new (anonymous) sort of arity 0. */
863 TypeNode
mkSort(uint32_t flags
= ExprManager::SORT_FLAG_NONE
);
865 /** Make a new sort with the given name of arity 0. */
866 TypeNode
mkSort(const std::string
& name
, uint32_t flags
= ExprManager::SORT_FLAG_NONE
);
868 /** Make a new sort by parameterizing the given sort constructor. */
869 TypeNode
mkSort(TypeNode constructor
,
870 const std::vector
<TypeNode
>& children
,
871 uint32_t flags
= ExprManager::SORT_FLAG_NONE
);
873 /** Make a new sort with the given name and arity. */
874 TypeNode
mkSortConstructor(const std::string
& name
,
876 uint32_t flags
= ExprManager::SORT_FLAG_NONE
);
879 * Get the type for the given node and optionally do type checking.
881 * Initial type computation will be near-constant time if
882 * type checking is not requested. Results are memoized, so that
883 * subsequent calls to getType() without type checking will be
886 * Initial type checking is linear in the size of the expression.
887 * Again, the results are memoized, so that subsequent calls to
888 * getType(), with or without type checking, will be constant
891 * NOTE: A TypeCheckingException can be thrown even when type
892 * checking is not requested. getType() will always return a
893 * valid and correct type and, thus, an exception will be thrown
894 * when no valid or correct type can be computed (e.g., if the
895 * arguments to a bit-vector operation aren't bit-vectors). When
896 * type checking is not requested, getType() will do the minimum
897 * amount of checking required to return a valid result.
899 * @param n the Node for which we want a type
900 * @param check whether we should check the type as we compute it
903 TypeNode
getType(TNode n
, bool check
= false);
906 * Convert a node to an expression. Uses the ExprManager
907 * associated to this NodeManager.
909 inline Expr
toExpr(TNode n
);
912 * Convert an expression to a node.
914 static inline Node
fromExpr(const Expr
& e
);
917 * Convert a node manager to an expression manager.
919 inline ExprManager
* toExprManager();
922 * Convert an expression manager to a node manager.
924 static inline NodeManager
* fromExprManager(ExprManager
* exprManager
);
927 * Convert a type node to a type.
929 inline Type
toType(TypeNode tn
);
932 * Convert a type to a type node.
934 static inline TypeNode
fromType(Type t
);
936 /** Reclaim zombies while there are more than k nodes in the pool (if possible).*/
937 void reclaimZombiesUntil(uint32_t k
);
939 /** Reclaims all zombies (if possible).*/
940 void reclaimAllZombies();
942 /** Size of the node pool. */
943 size_t poolSize() const;
945 /** Deletes a list of attributes from the NM's AttributeManager.*/
946 void deleteAttributes(const std::vector
< const expr::attr::AttributeUniqueId
* >& ids
);
949 * This function gives developers a hook into the NodeManager.
950 * This can be changed in node_manager.cpp without recompiling most of cvc4.
952 * debugHook is a debugging only function, and should not be present in
953 * any published code!
955 void debugHook(int debugFlag
);
956 };/* class NodeManager */
959 * This class changes the "current" thread-global
960 * <code>NodeManager</code> when it is created and reinstates the
961 * previous thread-global <code>NodeManager</code> when it is
962 * destroyed, effectively maintaining a set of nested
963 * <code>NodeManager</code> scopes. This is especially useful on
964 * public-interface calls into the CVC4 library, where CVC4's notion
965 * of the "current" <code>NodeManager</code> should be set to match
966 * the calling context. See, for example, the implementations of
967 * public calls in the <code>ExprManager</code> and
968 * <code>SmtEngine</code> classes.
970 * The client must be careful to create and destroy
971 * <code>NodeManagerScope</code> objects in a well-nested manner (such
972 * as on the stack). You may create a <code>NodeManagerScope</code>
973 * with <code>new</code> and destroy it with <code>delete</code>, or
974 * place it as a data member of an object that is, but if the scope of
975 * these <code>new</code>/<code>delete</code> pairs isn't properly
976 * maintained, the incorrect "current" <code>NodeManager</code>
977 * pointer may be restored after a delete.
979 class NodeManagerScope
{
980 /** The old NodeManager, to be restored on destruction. */
981 NodeManager
* d_oldNodeManager
;
982 Options::OptionsScope d_optionsScope
;
985 NodeManagerScope(NodeManager
* nm
)
986 : d_oldNodeManager(NodeManager::s_current
)
987 , d_optionsScope(nm
? nm
->d_options
: NULL
) {
988 // There are corner cases where nm can be NULL and it's ok.
989 // For example, if you write { Expr e; }, then when the null
990 // Expr is destructed, there's no active node manager.
991 //Assert(nm != NULL);
992 NodeManager::s_current
= nm
;
993 //Options::s_current = nm ? nm->d_options : NULL;
994 Debug("current") << "node manager scope: "
995 << NodeManager::s_current
<< "\n";
998 ~NodeManagerScope() {
999 NodeManager::s_current
= d_oldNodeManager
;
1000 //Options::s_current = d_oldNodeManager ? d_oldNodeManager->d_options : NULL;
1001 Debug("current") << "node manager scope: "
1002 << "returning to " << NodeManager::s_current
<< "\n";
1004 };/* class NodeManagerScope */
1006 /** Get the (singleton) type for booleans. */
1007 inline TypeNode
NodeManager::booleanType() {
1008 return TypeNode(mkTypeConst
<TypeConstant
>(BOOLEAN_TYPE
));
1011 /** Get the (singleton) type for integers. */
1012 inline TypeNode
NodeManager::integerType() {
1013 return TypeNode(mkTypeConst
<TypeConstant
>(INTEGER_TYPE
));
1016 /** Get the (singleton) type for reals. */
1017 inline TypeNode
NodeManager::realType() {
1018 return TypeNode(mkTypeConst
<TypeConstant
>(REAL_TYPE
));
1021 /** Get the (singleton) type for strings. */
1022 inline TypeNode
NodeManager::stringType() {
1023 return TypeNode(mkTypeConst
<TypeConstant
>(STRING_TYPE
));
1026 /** Get the (singleton) type for regexps. */
1027 inline TypeNode
NodeManager::regExpType() {
1028 return TypeNode(mkTypeConst
<TypeConstant
>(REGEXP_TYPE
));
1031 /** Get the (singleton) type for rounding modes. */
1032 inline TypeNode
NodeManager::roundingModeType() {
1033 return TypeNode(mkTypeConst
<TypeConstant
>(ROUNDINGMODE_TYPE
));
1036 /** Get the bound var list type. */
1037 inline TypeNode
NodeManager::boundVarListType() {
1038 return TypeNode(mkTypeConst
<TypeConstant
>(BOUND_VAR_LIST_TYPE
));
1041 /** Get the instantiation pattern type. */
1042 inline TypeNode
NodeManager::instPatternType() {
1043 return TypeNode(mkTypeConst
<TypeConstant
>(INST_PATTERN_TYPE
));
1046 /** Get the instantiation pattern type. */
1047 inline TypeNode
NodeManager::instPatternListType() {
1048 return TypeNode(mkTypeConst
<TypeConstant
>(INST_PATTERN_LIST_TYPE
));
1051 /** Get the (singleton) type for builtin operators. */
1052 inline TypeNode
NodeManager::builtinOperatorType() {
1053 return TypeNode(mkTypeConst
<TypeConstant
>(BUILTIN_OPERATOR_TYPE
));
1056 /** Make a function type from domain to range. */
1057 inline TypeNode
NodeManager::mkFunctionType(const TypeNode
& domain
, const TypeNode
& range
) {
1058 std::vector
<TypeNode
> sorts
;
1059 sorts
.push_back(domain
);
1060 sorts
.push_back(range
);
1061 return mkFunctionType(sorts
);
1064 inline TypeNode
NodeManager::mkFunctionType(const std::vector
<TypeNode
>& argTypes
, const TypeNode
& range
) {
1065 Assert(argTypes
.size() >= 1);
1066 std::vector
<TypeNode
> sorts(argTypes
);
1067 sorts
.push_back(range
);
1068 return mkFunctionType(sorts
);
1072 NodeManager::mkFunctionType(const std::vector
<TypeNode
>& sorts
) {
1073 Assert(sorts
.size() >= 2);
1074 std::vector
<TypeNode
> sortNodes
;
1075 for (unsigned i
= 0; i
< sorts
.size(); ++ i
) {
1076 CheckArgument(sorts
[i
].isFirstClass(), sorts
,
1077 "cannot create function types for argument types that are not first-class");
1078 sortNodes
.push_back(sorts
[i
]);
1080 CheckArgument(!sorts
[sorts
.size()-1].isFunction(), sorts
[sorts
.size()-1],
1081 "must flatten function types");
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
].isFirstClass(), sorts
,
1091 "cannot create predicate types for argument types that are not first-class");
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
.isFirstClass(), indexType
,
1125 "cannot index arrays by types that are not first-class");
1126 CheckArgument(constituentType
.isFirstClass(), constituentType
,
1127 "cannot store types that are not first-class 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 CheckArgument(elementType
.isFirstClass(), elementType
,
1137 "cannot store types that are not first-class in sets");
1138 Debug("sets") << "making sets type " << elementType
<< std::endl
;
1139 return mkTypeNode(kind::SET_TYPE
, elementType
);
1142 inline TypeNode
NodeManager::mkSelectorType(TypeNode domain
, TypeNode range
) {
1143 CheckArgument(domain
.isDatatype(), domain
,
1144 "cannot create non-datatype selector type");
1145 CheckArgument(range
.isFirstClass(), range
,
1146 "cannot have selector fields that are not first-class types");
1147 return mkTypeNode(kind::SELECTOR_TYPE
, domain
, range
);
1150 inline TypeNode
NodeManager::mkTesterType(TypeNode domain
) {
1151 CheckArgument(domain
.isDatatype(), domain
,
1152 "cannot create non-datatype tester");
1153 return mkTypeNode(kind::TESTER_TYPE
, domain
);
1156 inline expr::NodeValue
* NodeManager::poolLookup(expr::NodeValue
* nv
) const {
1157 NodeValuePool::const_iterator find
= d_nodeValuePool
.find(nv
);
1158 if(find
== d_nodeValuePool
.end()) {
1165 inline void NodeManager::poolInsert(expr::NodeValue
* nv
) {
1166 Assert(d_nodeValuePool
.find(nv
) == d_nodeValuePool
.end(),
1167 "NodeValue already in the pool!");
1168 d_nodeValuePool
.insert(nv
);// FIXME multithreading
1171 inline void NodeManager::poolRemove(expr::NodeValue
* nv
) {
1172 Assert(d_nodeValuePool
.find(nv
) != d_nodeValuePool
.end(),
1173 "NodeValue is not in the pool!");
1175 d_nodeValuePool
.erase(nv
);// FIXME multithreading
1178 inline Expr
NodeManager::toExpr(TNode n
) {
1179 return Expr(d_exprManager
, new Node(n
));
1182 inline Node
NodeManager::fromExpr(const Expr
& e
) {
1186 inline ExprManager
* NodeManager::toExprManager() {
1187 return d_exprManager
;
1190 inline NodeManager
* NodeManager::fromExprManager(ExprManager
* exprManager
) {
1191 return exprManager
->getNodeManager();
1194 inline Type
NodeManager::toType(TypeNode tn
) {
1195 return Type(this, new TypeNode(tn
));
1198 inline TypeNode
NodeManager::fromType(Type t
) {
1199 return *Type::getTypeNode(t
);
1202 }/* CVC4 namespace */
1204 #define __CVC4__NODE_MANAGER_NEEDS_CONSTANT_MAP
1205 #include "expr/metakind.h"
1206 #undef __CVC4__NODE_MANAGER_NEEDS_CONSTANT_MAP
1208 #include "expr/node_builder.h"
1212 // general expression-builders
1214 inline bool NodeManager::hasOperator(Kind k
) {
1215 switch(kind::MetaKind mk
= kind::metaKindOf(k
)) {
1217 case kind::metakind::INVALID
:
1218 case kind::metakind::VARIABLE
:
1219 case kind::metakind::NULLARY_OPERATOR
:
1222 case kind::metakind::OPERATOR
:
1223 case kind::metakind::PARAMETERIZED
:
1226 case kind::metakind::CONSTANT
:
1234 inline Kind
NodeManager::operatorToKind(TNode n
) {
1235 return kind::operatorToKind(n
.d_nv
);
1238 inline Node
NodeManager::mkNode(Kind kind
, TNode child1
) {
1239 NodeBuilder
<1> nb(this, kind
);
1241 return nb
.constructNode();
1244 inline Node
* NodeManager::mkNodePtr(Kind kind
, TNode child1
) {
1245 NodeBuilder
<1> nb(this, kind
);
1247 return nb
.constructNodePtr();
1250 inline Node
NodeManager::mkNode(Kind kind
, TNode child1
, TNode child2
) {
1251 NodeBuilder
<2> nb(this, kind
);
1252 nb
<< child1
<< child2
;
1253 return nb
.constructNode();
1256 inline Node
* NodeManager::mkNodePtr(Kind kind
, TNode child1
, TNode child2
) {
1257 NodeBuilder
<2> nb(this, kind
);
1258 nb
<< child1
<< child2
;
1259 return nb
.constructNodePtr();
1262 inline Node
NodeManager::mkNode(Kind kind
, TNode child1
, TNode child2
,
1264 NodeBuilder
<3> nb(this, kind
);
1265 nb
<< child1
<< child2
<< child3
;
1266 return nb
.constructNode();
1269 inline Node
* NodeManager::mkNodePtr(Kind kind
, TNode child1
, TNode child2
,
1271 NodeBuilder
<3> nb(this, kind
);
1272 nb
<< child1
<< child2
<< child3
;
1273 return nb
.constructNodePtr();
1276 inline Node
NodeManager::mkNode(Kind kind
, TNode child1
, TNode child2
,
1277 TNode child3
, TNode child4
) {
1278 NodeBuilder
<4> nb(this, kind
);
1279 nb
<< child1
<< child2
<< child3
<< child4
;
1280 return nb
.constructNode();
1283 inline Node
* NodeManager::mkNodePtr(Kind kind
, TNode child1
, TNode child2
,
1284 TNode child3
, TNode child4
) {
1285 NodeBuilder
<4> nb(this, kind
);
1286 nb
<< child1
<< child2
<< child3
<< child4
;
1287 return nb
.constructNodePtr();
1290 inline Node
NodeManager::mkNode(Kind kind
, TNode child1
, TNode child2
,
1291 TNode child3
, TNode child4
, TNode child5
) {
1292 NodeBuilder
<5> nb(this, kind
);
1293 nb
<< child1
<< child2
<< child3
<< child4
<< child5
;
1294 return nb
.constructNode();
1297 inline Node
* NodeManager::mkNodePtr(Kind kind
, TNode child1
, TNode child2
,
1298 TNode child3
, TNode child4
, TNode child5
) {
1299 NodeBuilder
<5> nb(this, kind
);
1300 nb
<< child1
<< child2
<< child3
<< child4
<< child5
;
1301 return nb
.constructNodePtr();
1305 template <bool ref_count
>
1306 inline Node
NodeManager::mkNode(Kind kind
,
1307 const std::vector
<NodeTemplate
<ref_count
> >&
1309 NodeBuilder
<> nb(this, kind
);
1310 nb
.append(children
);
1311 return nb
.constructNode();
1314 template <bool ref_count
>
1315 inline Node
* NodeManager::mkNodePtr(Kind kind
,
1316 const std::vector
<NodeTemplate
<ref_count
> >&
1318 NodeBuilder
<> nb(this, kind
);
1319 nb
.append(children
);
1320 return nb
.constructNodePtr();
1324 inline Node
NodeManager::mkNode(TNode opNode
) {
1325 NodeBuilder
<1> nb(this, operatorToKind(opNode
));
1326 if(opNode
.getKind() != kind::BUILTIN
) {
1329 return nb
.constructNode();
1332 inline Node
* NodeManager::mkNodePtr(TNode opNode
) {
1333 NodeBuilder
<1> nb(this, operatorToKind(opNode
));
1334 if(opNode
.getKind() != kind::BUILTIN
) {
1337 return nb
.constructNodePtr();
1340 inline Node
NodeManager::mkNode(TNode opNode
, TNode child1
) {
1341 NodeBuilder
<2> nb(this, operatorToKind(opNode
));
1342 if(opNode
.getKind() != kind::BUILTIN
) {
1346 return nb
.constructNode();
1349 inline Node
* NodeManager::mkNodePtr(TNode opNode
, TNode child1
) {
1350 NodeBuilder
<2> nb(this, operatorToKind(opNode
));
1351 if(opNode
.getKind() != kind::BUILTIN
) {
1355 return nb
.constructNodePtr();
1358 inline Node
NodeManager::mkNode(TNode opNode
, TNode child1
, TNode child2
) {
1359 NodeBuilder
<3> nb(this, operatorToKind(opNode
));
1360 if(opNode
.getKind() != kind::BUILTIN
) {
1363 nb
<< child1
<< child2
;
1364 return nb
.constructNode();
1367 inline Node
* NodeManager::mkNodePtr(TNode opNode
, TNode child1
, TNode child2
) {
1368 NodeBuilder
<3> nb(this, operatorToKind(opNode
));
1369 if(opNode
.getKind() != kind::BUILTIN
) {
1372 nb
<< child1
<< child2
;
1373 return nb
.constructNodePtr();
1376 inline Node
NodeManager::mkNode(TNode opNode
, TNode child1
, TNode child2
,
1378 NodeBuilder
<4> nb(this, operatorToKind(opNode
));
1379 if(opNode
.getKind() != kind::BUILTIN
) {
1382 nb
<< child1
<< child2
<< child3
;
1383 return nb
.constructNode();
1386 inline Node
* NodeManager::mkNodePtr(TNode opNode
, TNode child1
, TNode child2
,
1388 NodeBuilder
<4> nb(this, operatorToKind(opNode
));
1389 if(opNode
.getKind() != kind::BUILTIN
) {
1392 nb
<< child1
<< child2
<< child3
;
1393 return nb
.constructNodePtr();
1396 inline Node
NodeManager::mkNode(TNode opNode
, TNode child1
, TNode child2
,
1397 TNode child3
, TNode child4
) {
1398 NodeBuilder
<5> nb(this, operatorToKind(opNode
));
1399 if(opNode
.getKind() != kind::BUILTIN
) {
1402 nb
<< child1
<< child2
<< child3
<< child4
;
1403 return nb
.constructNode();
1406 inline Node
* NodeManager::mkNodePtr(TNode opNode
, TNode child1
, TNode child2
,
1407 TNode child3
, TNode child4
) {
1408 NodeBuilder
<5> nb(this, operatorToKind(opNode
));
1409 if(opNode
.getKind() != kind::BUILTIN
) {
1412 nb
<< child1
<< child2
<< child3
<< child4
;
1413 return nb
.constructNodePtr();
1416 inline Node
NodeManager::mkNode(TNode opNode
, TNode child1
, TNode child2
,
1417 TNode child3
, TNode child4
, TNode child5
) {
1418 NodeBuilder
<6> nb(this, operatorToKind(opNode
));
1419 if(opNode
.getKind() != kind::BUILTIN
) {
1422 nb
<< child1
<< child2
<< child3
<< child4
<< child5
;
1423 return nb
.constructNode();
1426 inline Node
* NodeManager::mkNodePtr(TNode opNode
, TNode child1
, TNode child2
,
1427 TNode child3
, TNode child4
, TNode child5
) {
1428 NodeBuilder
<6> nb(this, operatorToKind(opNode
));
1429 if(opNode
.getKind() != kind::BUILTIN
) {
1432 nb
<< child1
<< child2
<< child3
<< child4
<< child5
;
1433 return nb
.constructNodePtr();
1436 // N-ary version for operators
1437 template <bool ref_count
>
1438 inline Node
NodeManager::mkNode(TNode opNode
,
1439 const std::vector
<NodeTemplate
<ref_count
> >&
1441 NodeBuilder
<> nb(this, operatorToKind(opNode
));
1442 if(opNode
.getKind() != kind::BUILTIN
) {
1445 nb
.append(children
);
1446 return nb
.constructNode();
1449 template <bool ref_count
>
1450 inline Node
* NodeManager::mkNodePtr(TNode opNode
,
1451 const std::vector
<NodeTemplate
<ref_count
> >&
1453 NodeBuilder
<> nb(this, operatorToKind(opNode
));
1454 if(opNode
.getKind() != kind::BUILTIN
) {
1457 nb
.append(children
);
1458 return nb
.constructNodePtr();
1462 inline TypeNode
NodeManager::mkTypeNode(Kind kind
, TypeNode child1
) {
1463 return (NodeBuilder
<1>(this, kind
) << child1
).constructTypeNode();
1466 inline TypeNode
NodeManager::mkTypeNode(Kind kind
, TypeNode child1
,
1468 return (NodeBuilder
<2>(this, kind
) << child1
<< child2
).constructTypeNode();
1471 inline TypeNode
NodeManager::mkTypeNode(Kind kind
, TypeNode child1
,
1472 TypeNode child2
, TypeNode child3
) {
1473 return (NodeBuilder
<3>(this, kind
) << child1
<< child2
<< child3
).constructTypeNode();
1476 // N-ary version for types
1477 inline TypeNode
NodeManager::mkTypeNode(Kind kind
,
1478 const std::vector
<TypeNode
>& children
) {
1479 return NodeBuilder
<>(this, kind
).append(children
).constructTypeNode();
1483 Node
NodeManager::mkConst(const T
& val
) {
1484 return mkConstInternal
<Node
, T
>(val
);
1488 TypeNode
NodeManager::mkTypeConst(const T
& val
) {
1489 return mkConstInternal
<TypeNode
, T
>(val
);
1492 template <class NodeClass
, class T
>
1493 NodeClass
NodeManager::mkConstInternal(const T
& val
) {
1495 // typedef typename kind::metakind::constantMap<T>::OwningTheory theory_t;
1496 NVStorage
<1> nvStorage
;
1497 expr::NodeValue
& nvStack
= reinterpret_cast<expr::NodeValue
&>(nvStorage
);
1500 nvStack
.d_kind
= kind::metakind::ConstantMap
<T
>::kind
;
1502 nvStack
.d_nchildren
= 1;
1504 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
1505 #pragma GCC diagnostic push
1506 #pragma GCC diagnostic ignored "-Warray-bounds"
1509 nvStack
.d_children
[0] =
1510 const_cast<expr::NodeValue
*>(reinterpret_cast<const expr::NodeValue
*>(&val
));
1511 expr::NodeValue
* nv
= poolLookup(&nvStack
);
1513 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
1514 #pragma GCC diagnostic pop
1518 return NodeClass(nv
);
1521 nv
= (expr::NodeValue
*)
1522 std::malloc(sizeof(expr::NodeValue
) + sizeof(T
));
1524 throw std::bad_alloc();
1527 nv
->d_nchildren
= 0;
1528 nv
->d_kind
= kind::metakind::ConstantMap
<T
>::kind
;
1529 nv
->d_id
= next_id
++;// FIXME multithreading
1532 //OwningTheory::mkConst(val);
1533 new (&nv
->d_children
) T(val
);
1536 if(Debug
.isOn("gc")) {
1537 Debug("gc") << "creating node value " << nv
1538 << " [" << nv
->d_id
<< "]: ";
1539 nv
->printAst(Debug("gc"));
1540 Debug("gc") << std::endl
;
1543 return NodeClass(nv
);
1546 }/* CVC4 namespace */
1548 #endif /* __CVC4__NODE_MANAGER_H */