GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/expr/node.cpp Lines: 42 50 84.0 %
Date: 2021-08-06 Branches: 115 226 50.9 %

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
442
TypeCheckingExceptionPrivate::TypeCheckingExceptionPrivate(TNode node,
31
442
                                                           std::string message)
32
442
    : Exception(message), d_node(new Node(node))
33
{
34
#ifdef CVC5_DEBUG
35
884
  std::stringstream ss;
36
442
  LastExceptionBuffer* current = LastExceptionBuffer::getCurrent();
37
442
  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
442
}
52
53
442
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
74698412
bool NodeTemplate<ref_count>::isConst() const {
82
74698412
  assertTNodeNotExpired();
83
74698412
  Debug("isConst") << "Node::isConst() for: " << *this << std::endl;
84
74698412
  if(isNull()) {
85
46531
    return false;
86
  }
87
74651881
  switch(getMetaKind()) {
88
19578506
  case kind::metakind::CONSTANT:
89
19578506
    Debug("isConst") << "Node::isConst() returning true, it's a CONSTANT" << std::endl;
90
19578506
    return true;
91
9249477
  case kind::metakind::VARIABLE:
92
9249477
    Debug("isConst") << "Node::isConst() returning false, it's a VARIABLE" << std::endl;
93
9249477
    return false;
94
45823898
  default:
95
45823898
    if(getAttribute(IsConstComputedAttr())) {
96
40726947
      bool bval = getAttribute(IsConstAttr());
97
40726947
      Debug("isConst") << "Node::isConst() returning cached value " << (bval ? "true" : "false") << " for: " << *this << std::endl;
98
40726947
      return bval;
99
    } else {
100
5096951
      bool bval = expr::TypeChecker::computeIsConst(NodeManager::currentNM(), *this);
101
5096951
      Debug("isConst") << "Node::isConst() computed value " << (bval ? "true" : "false") << " for: " << *this << std::endl;
102
5096951
      const_cast< NodeTemplate<ref_count>* >(this)->setAttribute(IsConstAttr(), bval);
103
5096951
      const_cast< NodeTemplate<ref_count>* >(this)->setAttribute(IsConstComputedAttr(), true);
104
5096951
      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
1162583728
size_t hash<cvc5::Node>::operator()(const cvc5::Node& node) const
117
{
118
1162583728
  return node.getId();
119
}
120
121
1247749301
size_t hash<cvc5::TNode>::operator()(const cvc5::TNode& node) const
122
{
123
1247749301
  return node.getId();
124
}
125
126
29322
}  // namespace std