summaryrefslogtreecommitdiff
path: root/src/expr/node_builder.cpp
blob: 042e4442d9d23781b9ab7507c1b32d40cde6ff4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/******************************************************************************
 * Top contributors (to current version):
 *   Andres Noetzli, Morgan Deters, Mathias Preiner
 *
 * This file is part of the cvc5 project.
 *
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 * in the top-level source directory and their institutional affiliations.
 * All rights reserved.  See the file COPYING in the top-level source
 * directory for licensing information.
 * ****************************************************************************
 *
 * A builder interface for Nodes.
 */

#include "expr/node_builder.h"

#include <memory>

namespace cvc5 {

NodeBuilder::NodeBuilder()
    : d_nv(&d_inlineNv),
      d_nm(NodeManager::currentNM()),
      d_nvMaxChildren(default_nchild_thresh)
{
  d_inlineNv.d_id = 0;
  d_inlineNv.d_rc = 0;
  d_inlineNv.d_kind = expr::NodeValue::kindToDKind(kind::UNDEFINED_KIND);
  d_inlineNv.d_nchildren = 0;
}

NodeBuilder::NodeBuilder(Kind k)
    : d_nv(&d_inlineNv),
      d_nm(NodeManager::currentNM()),
      d_nvMaxChildren(default_nchild_thresh)
{
  Assert(k != kind::NULL_EXPR && k != kind::UNDEFINED_KIND)
      << "illegal Node-building kind";

  d_inlineNv.d_id = 1;  // have a kind already
  d_inlineNv.d_rc = 0;
  d_inlineNv.d_kind = expr::NodeValue::kindToDKind(k);
  d_inlineNv.d_nchildren = 0;
}

NodeBuilder::NodeBuilder(NodeManager* nm)
    : d_nv(&d_inlineNv), d_nm(nm), d_nvMaxChildren(default_nchild_thresh)
{
  d_inlineNv.d_id = 0;
  d_inlineNv.d_rc = 0;
  d_inlineNv.d_kind = expr::NodeValue::kindToDKind(kind::UNDEFINED_KIND);
  d_inlineNv.d_nchildren = 0;
}

NodeBuilder::NodeBuilder(NodeManager* nm, Kind k)
    : d_nv(&d_inlineNv), d_nm(nm), d_nvMaxChildren(default_nchild_thresh)
{
  Assert(k != kind::NULL_EXPR && k != kind::UNDEFINED_KIND)
      << "illegal Node-building kind";

  d_inlineNv.d_id = 1;  // have a kind already
  d_inlineNv.d_rc = 0;
  d_inlineNv.d_kind = expr::NodeValue::kindToDKind(k);
  d_inlineNv.d_nchildren = 0;
}

NodeBuilder::NodeBuilder(const NodeBuilder& nb)
    : d_nv(&d_inlineNv), d_nm(nb.d_nm), d_nvMaxChildren(default_nchild_thresh)
{
  d_inlineNv.d_id = nb.d_nv->d_id;
  d_inlineNv.d_rc = 0;
  d_inlineNv.d_kind = nb.d_nv->d_kind;
  d_inlineNv.d_nchildren = 0;

  internalCopy(nb);
}

NodeBuilder::~NodeBuilder()
{
  if (CVC5_PREDICT_FALSE(nvIsAllocated()))
  {
    dealloc();
  }
  else if (CVC5_PREDICT_FALSE(!isUsed()))
  {
    decrRefCounts();
  }
}

Kind NodeBuilder::getKind() const
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  return d_nv->getKind();
}

kind::MetaKind NodeBuilder::getMetaKind() const
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  Assert(getKind() != kind::UNDEFINED_KIND)
      << "The metakind of a NodeBuilder is undefined "
         "until a Kind is set";
  return d_nv->getMetaKind();
}

unsigned NodeBuilder::getNumChildren() const
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  Assert(getKind() != kind::UNDEFINED_KIND)
      << "The number of children of a NodeBuilder is undefined "
         "until a Kind is set";
  return d_nv->getNumChildren();
}

Node NodeBuilder::getOperator() const
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  Assert(getKind() != kind::UNDEFINED_KIND)
      << "NodeBuilder operator access is not permitted "
         "until a Kind is set";
  Assert(getMetaKind() == kind::metakind::PARAMETERIZED)
      << "NodeBuilder operator access is only permitted "
         "on parameterized kinds, not `"
      << getKind() << "'";
  return Node(d_nv->getOperator());
}

Node NodeBuilder::getChild(int i) const
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  Assert(getKind() != kind::UNDEFINED_KIND)
      << "NodeBuilder child access is not permitted "
         "until a Kind is set";
  Assert(i >= 0 && unsigned(i) < d_nv->getNumChildren())
      << "index out of range for NodeBuilder::getChild()";
  return Node(d_nv->getChild(i));
}

Node NodeBuilder::operator[](int i) const { return getChild(i); }

void NodeBuilder::clear(Kind k)
{
  Assert(k != kind::NULL_EXPR) << "illegal Node-building clear kind";

  if (CVC5_PREDICT_FALSE(nvIsAllocated()))
  {
    dealloc();
  }
  else if (CVC5_PREDICT_FALSE(!isUsed()))
  {
    decrRefCounts();
  }
  else
  {
    setUnused();
  }

  d_inlineNv.d_kind = expr::NodeValue::kindToDKind(k);
  for (expr::NodeValue::nv_iterator i = d_inlineNv.nv_begin();
       i != d_inlineNv.nv_end();
       ++i)
  {
    (*i)->dec();
  }
  d_inlineNv.d_nchildren = 0;
  // keep track of whether or not we hvae a kind already
  d_inlineNv.d_id = (k == kind::UNDEFINED_KIND) ? 0 : 1;
}

NodeBuilder& NodeBuilder::operator<<(const Kind& k)
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  Assert(getKind() == kind::UNDEFINED_KIND || d_nv->d_id == 0)
      << "can't redefine the Kind of a NodeBuilder";
  Assert(d_nv->d_id == 0)
      << "internal inconsistency with NodeBuilder: d_id != 0";
  AssertArgument(
      k != kind::UNDEFINED_KIND && k != kind::NULL_EXPR && k < kind::LAST_KIND,
      k,
      "illegal node-building kind");
  // This test means: we didn't have a Kind at the beginning (on
  // NodeBuilder construction or at the last clear()), but we do
  // now.  That means we appended a Kind with operator<<(Kind),
  // which now (lazily) we'll collapse.
  if (CVC5_PREDICT_FALSE(d_nv->d_id == 0 && getKind() != kind::UNDEFINED_KIND))
  {
    Node n2 = operator Node();
    clear();
    append(n2);
  }
  else if (d_nv->d_nchildren == 0)
  {
    d_nv->d_id = 1;  // remember that we had a kind from the start
  }
  d_nv->d_kind = expr::NodeValue::kindToDKind(k);
  return *this;
}

NodeBuilder& NodeBuilder::operator<<(TNode n)
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  // This test means: we didn't have a Kind at the beginning (on
  // NodeBuilder construction or at the last clear()), but we do
  // now.  That means we appended a Kind with operator<<(Kind),
  // which now (lazily) we'll collapse.
  if (CVC5_PREDICT_FALSE(d_nv->d_id == 0 && getKind() != kind::UNDEFINED_KIND))
  {
    Node n2 = operator Node();
    clear();
    append(n2);
  }
  return append(n);
}

NodeBuilder& NodeBuilder::operator<<(TypeNode n)
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  // This test means: we didn't have a Kind at the beginning (on
  // NodeBuilder construction or at the last clear()), but we do
  // now.  That means we appended a Kind with operator<<(Kind),
  // which now (lazily) we'll collapse.
  if (CVC5_PREDICT_FALSE(d_nv->d_id == 0 && getKind() != kind::UNDEFINED_KIND))
  {
    Node n2 = operator Node();
    clear();
    append(n2);
  }
  return append(n);
}

NodeBuilder& NodeBuilder::append(const std::vector<TypeNode>& children)
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  return append(children.begin(), children.end());
}

NodeBuilder& NodeBuilder::append(TNode n)
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  Assert(!n.isNull()) << "Cannot use NULL Node as a child of a Node";
  if (n.getKind() == kind::BUILTIN)
  {
    return *this << NodeManager::operatorToKind(n);
  }
  allocateNvIfNecessaryForAppend();
  expr::NodeValue* nv = n.d_nv;
  nv->inc();
  d_nv->d_children[d_nv->d_nchildren++] = nv;
  Assert(d_nv->d_nchildren <= d_nvMaxChildren);
  return *this;
}

NodeBuilder& NodeBuilder::append(const TypeNode& typeNode)
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  Assert(!typeNode.isNull()) << "Cannot use NULL Node as a child of a Node";
  allocateNvIfNecessaryForAppend();
  expr::NodeValue* nv = typeNode.d_nv;
  nv->inc();
  d_nv->d_children[d_nv->d_nchildren++] = nv;
  Assert(d_nv->d_nchildren <= d_nvMaxChildren);
  return *this;
}

void NodeBuilder::realloc(size_t toSize)
{
  AlwaysAssert(toSize > d_nvMaxChildren)
      << "attempt to realloc() a NodeBuilder to a smaller/equal size!";
  Assert(toSize < (static_cast<size_t>(1) << expr::NodeValue::NBITS_NCHILDREN))
      << "attempt to realloc() a NodeBuilder to size " << toSize
      << " (beyond hard limit of " << expr::NodeValue::MAX_CHILDREN << ")";

  if (CVC5_PREDICT_FALSE(nvIsAllocated()))
  {
    // Ensure d_nv is not modified on allocation failure
    expr::NodeValue* newBlock = (expr::NodeValue*)std::realloc(
        d_nv, sizeof(expr::NodeValue) + (sizeof(expr::NodeValue*) * toSize));
    if (newBlock == nullptr)
    {
      // In this case, d_nv was NOT freed.  If we throw, the
      // deallocation should occur on destruction of the NodeBuilder.
      throw std::bad_alloc();
    }
    d_nvMaxChildren = toSize;
    Assert(d_nvMaxChildren == toSize);  // overflow check
    // Here, the copy (between two heap-allocated buffers) has already
    // been done for us by the std::realloc().
    d_nv = newBlock;
  }
  else
  {
    // Ensure d_nv is not modified on allocation failure
    expr::NodeValue* newBlock = (expr::NodeValue*)std::malloc(
        sizeof(expr::NodeValue) + (sizeof(expr::NodeValue*) * toSize));
    if (newBlock == nullptr)
    {
      throw std::bad_alloc();
    }
    d_nvMaxChildren = toSize;
    Assert(d_nvMaxChildren == toSize);  // overflow check

    d_nv = newBlock;
    d_nv->d_id = d_inlineNv.d_id;
    d_nv->d_rc = 0;
    d_nv->d_kind = d_inlineNv.d_kind;
    d_nv->d_nchildren = d_inlineNv.d_nchildren;

    std::copy(d_inlineNv.d_children,
              d_inlineNv.d_children + d_inlineNv.d_nchildren,
              d_nv->d_children);

    // ensure "inline" children don't get decremented in dtor
    d_inlineNv.d_nchildren = 0;
  }
}

void NodeBuilder::dealloc()
{
  Assert(nvIsAllocated())
      << "Internal error: NodeBuilder: dealloc() called without a "
         "private NodeBuilder-allocated buffer";

  for (expr::NodeValue::nv_iterator i = d_nv->nv_begin(); i != d_nv->nv_end();
       ++i)
  {
    (*i)->dec();
  }

  free(d_nv);
  d_nv = &d_inlineNv;
  d_nvMaxChildren = default_nchild_thresh;
}

void NodeBuilder::decrRefCounts()
{
  Assert(!nvIsAllocated())
      << "Internal error: NodeBuilder: decrRefCounts() called with a "
         "private NodeBuilder-allocated buffer";

  for (expr::NodeValue::nv_iterator i = d_inlineNv.nv_begin();
       i != d_inlineNv.nv_end();
       ++i)
  {
    (*i)->dec();
  }

  d_inlineNv.d_nchildren = 0;
}

TypeNode NodeBuilder::constructTypeNode() { return TypeNode(constructNV()); }

Node NodeBuilder::constructNode()
{
  Node n(constructNV());
  maybeCheckType(n);
  return n;
}

Node* NodeBuilder::constructNodePtr()
{
  std::unique_ptr<Node> np(new Node(constructNV()));
  maybeCheckType(*np.get());
  return np.release();
}

NodeBuilder::operator Node() { return constructNode(); }

NodeBuilder::operator TypeNode() { return constructTypeNode(); }

expr::NodeValue* NodeBuilder::constructNV()
{
  Assert(!isUsed()) << "NodeBuilder is one-shot only; "
                       "attempt to access it after conversion";
  Assert(getKind() != kind::UNDEFINED_KIND)
      << "Can't make an expression of an undefined kind!";

  // NOTE: The comments in this function refer to the cases in the
  // file comments at the top of this file.

  // Case 0: If a VARIABLE
  if (getMetaKind() == kind::metakind::VARIABLE
      || getMetaKind() == kind::metakind::NULLARY_OPERATOR)
  {
    /* 0. If a VARIABLE, treat similarly to 1(b), except that we know
     * there are no children (no reference counts to reason about),
     * and we don't keep VARIABLE-kinded Nodes in the NodeManager
     * pool. */

    Assert(!nvIsAllocated())
        << "internal NodeBuilder error: "
           "VARIABLE-kinded NodeBuilder is heap-allocated !?";
    Assert(d_inlineNv.d_nchildren == 0)
        << "improperly-formed VARIABLE-kinded NodeBuilder: "
           "no children permitted";

    // we have to copy the inline NodeValue out
    expr::NodeValue* nv =
        (expr::NodeValue*)std::malloc(sizeof(expr::NodeValue));
    if (nv == nullptr)
    {
      throw std::bad_alloc();
    }
    // there are no children, so we don't have to worry about
    // reference counts in this case.
    nv->d_nchildren = 0;
    nv->d_kind = d_nv->d_kind;
    nv->d_id = d_nm->next_id++;  // FIXME multithreading
    nv->d_rc = 0;
    setUsed();
    if (Debug.isOn("gc"))
    {
      Debug("gc") << "creating node value " << nv << " [" << nv->d_id << "]: ";
      nv->printAst(Debug("gc"));
      Debug("gc") << std::endl;
    }
    return nv;
  }

  // check that there are the right # of children for this kind
  Assert(getMetaKind() != kind::metakind::CONSTANT)
      << "Cannot make Nodes with NodeBuilder that have CONSTANT-kinded kinds";
  Assert(getNumChildren() >= kind::metakind::getMinArityForKind(getKind()))
      << "Nodes with kind " << getKind() << " must have at least "
      << kind::metakind::getMinArityForKind(getKind())
      << " children (the one under "
         "construction has "
      << getNumChildren() << ")";
  Assert(getNumChildren() <= kind::metakind::getMaxArityForKind(getKind()))
      << "Nodes with kind " << getKind() << " must have at most "
      << kind::metakind::getMaxArityForKind(getKind())
      << " children (the one under "
         "construction has "
      << getNumChildren() << ")";

  // Implementation differs depending on whether the NodeValue was
  // malloc'ed or not and whether or not it's in the already-been-seen
  // NodeManager pool of Nodes.  See implementation notes at the top
  // of this file.

  if (CVC5_PREDICT_TRUE(!nvIsAllocated()))
  {
    /** Case 1.  d_nv points to d_inlineNv: it is the backing store
     ** allocated "inline" in this NodeBuilder. **/

    // Lookup the expression value in the pool we already have
    expr::NodeValue* poolNv = d_nm->poolLookup(&d_inlineNv);
    // If something else is there, we reuse it
    if (poolNv != nullptr)
    {
      /* Subcase (a): The Node under construction already exists in
       * the NodeManager's pool. */

      /* 1(a). Reference-counts for all children in d_inlineNv must be
       * decremented, and the NodeBuilder must be marked as "used" and
       * the number of children set to zero so that we don't decrement
       * them again on destruction.  The existing NodeManager pool
       * entry is returned.
       */
      decrRefCounts();
      d_inlineNv.d_nchildren = 0;
      setUsed();
      return poolNv;
    }
    else
    {
      /* Subcase (b): The Node under construction is NOT already in
       * the NodeManager's pool. */

      /* 1(b). A new heap-allocated NodeValue must be constructed and
       * all settings and children from d_inlineNv copied into it.
       * This new NodeValue is put into the NodeManager's pool.  The
       * NodeBuilder is marked as "used" and the number of children in
       * d_inlineNv set to zero so that we don't decrement child
       * reference counts on destruction (the child reference counts
       * have been "taken over" by the new NodeValue).  We return a
       * Node wrapper for this new NodeValue, which increments its
       * reference count. */

      // create the canonical expression value for this node
      expr::NodeValue* nv = (expr::NodeValue*)std::malloc(
          sizeof(expr::NodeValue)
          + (sizeof(expr::NodeValue*) * d_inlineNv.d_nchildren));
      if (nv == nullptr)
      {
        throw std::bad_alloc();
      }
      nv->d_nchildren = d_inlineNv.d_nchildren;
      nv->d_kind = d_inlineNv.d_kind;
      nv->d_id = d_nm->next_id++;  // FIXME multithreading
      nv->d_rc = 0;

      std::copy(d_inlineNv.d_children,
                d_inlineNv.d_children + d_inlineNv.d_nchildren,
                nv->d_children);

      d_inlineNv.d_nchildren = 0;
      setUsed();

      // poolNv = nv;
      d_nm->poolInsert(nv);
      if (Debug.isOn("gc"))
      {
        Debug("gc") << "creating node value " << nv << " [" << nv->d_id
                    << "]: ";
        nv->printAst(Debug("gc"));
        Debug("gc") << std::endl;
      }
      return nv;
    }
  }
  else
  {
    /** Case 2. d_nv does NOT point to d_inlineNv: it is a new, larger
     ** buffer that was heap-allocated by this NodeBuilder. **/

    // Lookup the expression value in the pool we already have (with insert)
    expr::NodeValue* poolNv = d_nm->poolLookup(d_nv);
    // If something else is there, we reuse it
    if (poolNv != nullptr)
    {
      /* Subcase (a): The Node under construction already exists in
       * the NodeManager's pool. */

      /* 2(a). Reference-counts for all children in d_nv must be
       * decremented.  The NodeBuilder is marked as "used" and the
       * heap-allocated d_nv deleted.  d_nv is repointed to d_inlineNv
       * so that destruction of the NodeBuilder doesn't cause any
       * problems.  The existing NodeManager pool entry is
       * returned. */

      dealloc();
      setUsed();
      return poolNv;
    }
    else
    {
      /* Subcase (b) The Node under construction is NOT already in the
       * NodeManager's pool. */

      /* 2(b). The heap-allocated d_nv is "cropped" to the correct
       * size (based on the number of children it _actually_ has).
       * d_nv is repointed to d_inlineNv so that destruction of the
       * NodeBuilder doesn't cause any problems, and the (old) value
       * it had is placed into the NodeManager's pool and returned in
       * a Node wrapper. */

      crop();
      expr::NodeValue* nv = d_nv;
      nv->d_id = d_nm->next_id++;  // FIXME multithreading
      d_nv = &d_inlineNv;
      d_nvMaxChildren = default_nchild_thresh;
      setUsed();

      // poolNv = nv;
      d_nm->poolInsert(nv);
      Debug("gc") << "creating node value " << nv << " [" << nv->d_id
                  << "]: " << *nv << "\n";
      return nv;
    }
  }
}

void NodeBuilder::internalCopy(const NodeBuilder& nb)
{
  if (nb.isUsed())
  {
    setUsed();
    return;
  }

  bool realloced CVC5_UNUSED = false;
  if (nb.d_nvMaxChildren > d_nvMaxChildren)
  {
    realloced = true;
    realloc(nb.d_nvMaxChildren);
  }

  Assert(nb.d_nvMaxChildren <= d_nvMaxChildren);
  Assert(nb.d_nv->nv_end() >= nb.d_nv->nv_begin());
  Assert((size_t)(nb.d_nv->nv_end() - nb.d_nv->nv_begin()) <= d_nvMaxChildren)
      << "realloced:" << (realloced ? "true" : "false")
      << ", d_nvMax:" << d_nvMaxChildren
      << ", size:" << nb.d_nv->nv_end() - nb.d_nv->nv_begin()
      << ", nc:" << nb.d_nv->getNumChildren();
  std::copy(nb.d_nv->nv_begin(), nb.d_nv->nv_end(), d_nv->nv_begin());

  d_nv->d_nchildren = nb.d_nv->d_nchildren;

  for (expr::NodeValue::nv_iterator i = d_nv->nv_begin(); i != d_nv->nv_end();
       ++i)
  {
    (*i)->inc();
  }
}

#ifdef CVC5_DEBUG
inline void NodeBuilder::maybeCheckType(const TNode n) const
{
  /* force an immediate type check, if early type checking is
     enabled and the current node isn't a variable or constant */
  kind::MetaKind mk = n.getMetaKind();
  if (mk != kind::metakind::VARIABLE && mk != kind::metakind::NULLARY_OPERATOR
      && mk != kind::metakind::CONSTANT)
  {
    d_nm->getType(n, true);
  }
}
#endif /* CVC5_DEBUG */

bool NodeBuilder::isUsed() const { return CVC5_PREDICT_FALSE(d_nv == nullptr); }

void NodeBuilder::setUsed()
{
  Assert(!isUsed()) << "Internal error: bad `used' state in NodeBuilder!";
  Assert(d_inlineNv.d_nchildren == 0
         && d_nvMaxChildren == default_nchild_thresh)
      << "Internal error: bad `inline' state in NodeBuilder!";
  d_nv = nullptr;
}

void NodeBuilder::setUnused()
{
  Assert(isUsed()) << "Internal error: bad `used' state in NodeBuilder!";
  Assert(d_inlineNv.d_nchildren == 0
         && d_nvMaxChildren == default_nchild_thresh)
      << "Internal error: bad `inline' state in NodeBuilder!";
  d_nv = &d_inlineNv;
}

bool NodeBuilder::nvIsAllocated() const
{
  return CVC5_PREDICT_FALSE(d_nv != &d_inlineNv)
         && CVC5_PREDICT_TRUE(d_nv != nullptr);
}

bool NodeBuilder::nvNeedsToBeAllocated() const
{
  return CVC5_PREDICT_FALSE(d_nv->d_nchildren == d_nvMaxChildren);
}

void NodeBuilder::realloc()
{
  size_t newSize = 2 * size_t(d_nvMaxChildren);
  size_t hardLimit = expr::NodeValue::MAX_CHILDREN;
  realloc(CVC5_PREDICT_FALSE(newSize > hardLimit) ? hardLimit : newSize);
}

void NodeBuilder::allocateNvIfNecessaryForAppend()
{
  if (CVC5_PREDICT_FALSE(nvNeedsToBeAllocated()))
  {
    realloc();
  }
}

void NodeBuilder::crop()
{
  if (CVC5_PREDICT_FALSE(nvIsAllocated())
      && CVC5_PREDICT_TRUE(d_nvMaxChildren > d_nv->d_nchildren))
  {
    // Ensure d_nv is not modified on allocation failure
    expr::NodeValue* newBlock = (expr::NodeValue*)std::realloc(
        d_nv,
        sizeof(expr::NodeValue)
            + (sizeof(expr::NodeValue*) * d_nv->d_nchildren));
    if (newBlock == nullptr)
    {
      // In this case, d_nv was NOT freed.  If we throw, the
      // deallocation should occur on destruction of the
      // NodeBuilder.
      throw std::bad_alloc();
    }
    d_nv = newBlock;
    d_nvMaxChildren = d_nv->d_nchildren;
  }
}

NodeBuilder& NodeBuilder::collapseTo(Kind k)
{
  AssertArgument(
      k != kind::UNDEFINED_KIND && k != kind::NULL_EXPR && k < kind::LAST_KIND,
      k,
      "illegal collapsing kind");

  if (getKind() != k)
  {
    Node n = operator Node();
    clear();
    d_nv->d_kind = expr::NodeValue::kindToDKind(k);
    d_nv->d_id = 1;  // have a kind already
    return append(n);
  }
  return *this;
}

std::ostream& operator<<(std::ostream& out, const NodeBuilder& nb)
{
  return out << *nb.d_nv;
}

}  // namespace cvc5
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback