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 |
73962451 |
bool NodeTemplate<ref_count>::isConst() const { |
82 |
73962451 |
assertTNodeNotExpired(); |
83 |
73962451 |
Debug("isConst") << "Node::isConst() for: " << *this << std::endl; |
84 |
73962451 |
if(isNull()) { |
85 |
46531 |
return false; |
86 |
|
} |
87 |
73915920 |
switch(getMetaKind()) { |
88 |
19551249 |
case kind::metakind::CONSTANT: |
89 |
19551249 |
Debug("isConst") << "Node::isConst() returning true, it's a CONSTANT" << std::endl; |
90 |
19551249 |
return true; |
91 |
9252251 |
case kind::metakind::VARIABLE: |
92 |
9252251 |
Debug("isConst") << "Node::isConst() returning false, it's a VARIABLE" << std::endl; |
93 |
9252251 |
return false; |
94 |
45112420 |
default: |
95 |
45112420 |
if(getAttribute(IsConstComputedAttr())) { |
96 |
40018142 |
bool bval = getAttribute(IsConstAttr()); |
97 |
40018142 |
Debug("isConst") << "Node::isConst() returning cached value " << (bval ? "true" : "false") << " for: " << *this << std::endl; |
98 |
40018142 |
return bval; |
99 |
|
} else { |
100 |
5094278 |
bool bval = expr::TypeChecker::computeIsConst(NodeManager::currentNM(), *this); |
101 |
5094278 |
Debug("isConst") << "Node::isConst() computed value " << (bval ? "true" : "false") << " for: " << *this << std::endl; |
102 |
5094278 |
const_cast< NodeTemplate<ref_count>* >(this)->setAttribute(IsConstAttr(), bval); |
103 |
5094278 |
const_cast< NodeTemplate<ref_count>* >(this)->setAttribute(IsConstComputedAttr(), true); |
104 |
5094278 |
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 |
1134595291 |
size_t hash<cvc5::Node>::operator()(const cvc5::Node& node) const |
117 |
|
{ |
118 |
1134595291 |
return node.getId(); |
119 |
|
} |
120 |
|
|
121 |
1209928490 |
size_t hash<cvc5::TNode>::operator()(const cvc5::TNode& node) const |
122 |
|
{ |
123 |
1209928490 |
return node.getId(); |
124 |
|
} |
125 |
|
|
126 |
29337 |
} // namespace std |