GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/expr/node.cpp Lines: 42 50 84.0 %
Date: 2021-09-07 Branches: 115 222 51.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Morgan Deters, Tim King, Yoni Zohar
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 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.
11
 * ****************************************************************************
12
 *
13
 * Reference-counted encapsulation of a pointer to node information.
14
 */
15
#include "expr/node.h"
16
17
#include <cstring>
18
#include <iostream>
19
#include <sstream>
20
21
#include "base/exception.h"
22
#include "base/output.h"
23
#include "expr/attribute.h"
24
#include "expr/type_checker.h"
25
26
using namespace std;
27
28
namespace cvc5 {
29
30
448
TypeCheckingExceptionPrivate::TypeCheckingExceptionPrivate(TNode node,
31
448
                                                           std::string message)
32
448
    : Exception(message), d_node(new Node(node))
33
{
34
#ifdef CVC5_DEBUG
35
896
  std::stringstream ss;
36
448
  LastExceptionBuffer* current = LastExceptionBuffer::getCurrent();
37
448
  if(current != NULL){
38
    // Since this node is malformed, we cannot use toString().
39
    // Instead, we print the kind and the children.
40
4
    ss << "node kind: " << node.getKind() << ". children: ";
41
4
    int i = 0;
42
7
    for (const TNode& child : node)
43
    {
44
3
      ss << "child[" << i << "]: " << child << ". ";
45
3
      i++;
46
    }
47
8
    string ssstring = ss.str();
48
4
    current->setContents(ssstring.c_str());
49
  }
50
#endif /* CVC5_DEBUG */
51
448
}
52
53
448
TypeCheckingExceptionPrivate::~TypeCheckingExceptionPrivate() { delete d_node; }
54
55
void TypeCheckingExceptionPrivate::toStream(std::ostream& os) const
56
{
57
  os << "Error during type checking: " << d_msg << std::endl << *d_node << endl << "The ill-typed expression: " << *d_node;
58
}
59
60
NodeTemplate<true> TypeCheckingExceptionPrivate::getNode() const
61
{
62
  return *d_node;
63
}
64
65
UnknownTypeException::UnknownTypeException(TNode n)
66
    : TypeCheckingExceptionPrivate(
67
          n,
68
          "this expression contains an element of unknown type (such as an "
69
          "abstract value);"
70
          " its type cannot be computed until it is substituted away")
71
{
72
}
73
74
/** Is this node constant? (and has that been computed yet?) */
75
struct IsConstTag { };
76
struct IsConstComputedTag { };
77
typedef expr::Attribute<IsConstTag, bool> IsConstAttr;
78
typedef expr::Attribute<IsConstComputedTag, bool> IsConstComputedAttr;
79
80
template <bool ref_count>
81
80865343
bool NodeTemplate<ref_count>::isConst() const {
82
80865343
  assertTNodeNotExpired();
83
80865343
  Debug("isConst") << "Node::isConst() for: " << *this << std::endl;
84
80865343
  if(isNull()) {
85
67362
    return false;
86
  }
87
80797981
  switch(getMetaKind()) {
88
20548603
  case kind::metakind::CONSTANT:
89
20548603
    Debug("isConst") << "Node::isConst() returning true, it's a CONSTANT" << std::endl;
90
20548603
    return true;
91
10433662
  case kind::metakind::VARIABLE:
92
10433662
    Debug("isConst") << "Node::isConst() returning false, it's a VARIABLE" << std::endl;
93
10433662
    return false;
94
49815716
  default:
95
49815716
    if(getAttribute(IsConstComputedAttr())) {
96
44478274
      bool bval = getAttribute(IsConstAttr());
97
44478274
      Debug("isConst") << "Node::isConst() returning cached value " << (bval ? "true" : "false") << " for: " << *this << std::endl;
98
44478274
      return bval;
99
    } else {
100
5337442
      bool bval = expr::TypeChecker::computeIsConst(NodeManager::currentNM(), *this);
101
5337442
      Debug("isConst") << "Node::isConst() computed value " << (bval ? "true" : "false") << " for: " << *this << std::endl;
102
5337442
      const_cast< NodeTemplate<ref_count>* >(this)->setAttribute(IsConstAttr(), bval);
103
5337442
      const_cast< NodeTemplate<ref_count>* >(this)->setAttribute(IsConstComputedAttr(), true);
104
5337442
      return bval;
105
    }
106
  }
107
}
108
109
template bool NodeTemplate<true>::isConst() const;
110
template bool NodeTemplate<false>::isConst() const;
111
112
}  // namespace cvc5
113
114
namespace std {
115
116
1092479849
size_t hash<cvc5::Node>::operator()(const cvc5::Node& node) const
117
{
118
1092479849
  return node.getId();
119
}
120
121
1339558347
size_t hash<cvc5::TNode>::operator()(const cvc5::TNode& node) const
122
{
123
1339558347
  return node.getId();
124
}
125
126
29502
}  // namespace std