1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Mathias Preiner, Morgan Deters |
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 |
|
* Implementation of a class representing a type matcher. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "type_matcher.h" |
17 |
|
|
18 |
|
namespace cvc5 { |
19 |
|
|
20 |
1700 |
TypeMatcher::TypeMatcher(TypeNode dt) |
21 |
|
{ |
22 |
1700 |
Assert(dt.isDatatype()); |
23 |
1700 |
addTypesFromDatatype(dt); |
24 |
1700 |
} |
25 |
|
|
26 |
1795 |
void TypeMatcher::addTypesFromDatatype(TypeNode dt) |
27 |
|
{ |
28 |
3590 |
std::vector<TypeNode> argTypes = dt.getParamTypes(); |
29 |
1795 |
addTypes(argTypes); |
30 |
1795 |
Debug("typecheck-idt") << "instantiating matcher for " << dt << std::endl; |
31 |
4006 |
for (unsigned i = 0, narg = argTypes.size(); i < narg; ++i) |
32 |
|
{ |
33 |
2211 |
if (dt.isParameterInstantiatedDatatype(i)) |
34 |
|
{ |
35 |
1442 |
Debug("typecheck-idt") |
36 |
721 |
<< "++ instantiate param " << i << " : " << d_types[i] << std::endl; |
37 |
721 |
d_match[i] = d_types[i]; |
38 |
|
} |
39 |
|
} |
40 |
1795 |
} |
41 |
|
|
42 |
2211 |
void TypeMatcher::addType(TypeNode t) |
43 |
|
{ |
44 |
2211 |
d_types.push_back(t); |
45 |
2211 |
d_match.push_back(TypeNode::null()); |
46 |
2211 |
} |
47 |
|
|
48 |
1795 |
void TypeMatcher::addTypes(const std::vector<TypeNode>& types) |
49 |
|
{ |
50 |
4006 |
for (const TypeNode& t : types) |
51 |
|
{ |
52 |
2211 |
addType(t); |
53 |
|
} |
54 |
1795 |
} |
55 |
|
|
56 |
4814 |
bool TypeMatcher::doMatching(TypeNode pattern, TypeNode tn) |
57 |
|
{ |
58 |
9628 |
Debug("typecheck-idt") << "doMatching() : " << pattern << " : " << tn |
59 |
4814 |
<< std::endl; |
60 |
|
std::vector<TypeNode>::iterator i = |
61 |
4814 |
std::find(d_types.begin(), d_types.end(), pattern); |
62 |
4814 |
if (i != d_types.end()) |
63 |
|
{ |
64 |
1843 |
size_t index = i - d_types.begin(); |
65 |
1843 |
if (!d_match[index].isNull()) |
66 |
|
{ |
67 |
846 |
Debug("typecheck-idt") |
68 |
423 |
<< "check subtype " << tn << " " << d_match[index] << std::endl; |
69 |
846 |
TypeNode tnn = TypeNode::leastCommonTypeNode(tn, d_match[index]); |
70 |
|
// recognize subtype relation |
71 |
423 |
if (!tnn.isNull()) |
72 |
|
{ |
73 |
423 |
d_match[index] = tnn; |
74 |
423 |
return true; |
75 |
|
} |
76 |
|
return false; |
77 |
|
} |
78 |
1420 |
d_match[index] = tn; |
79 |
1420 |
return true; |
80 |
|
} |
81 |
2971 |
else if (pattern == tn) |
82 |
|
{ |
83 |
1662 |
return true; |
84 |
|
} |
85 |
2618 |
else if (pattern.getKind() != tn.getKind() |
86 |
1309 |
|| pattern.getNumChildren() != tn.getNumChildren()) |
87 |
|
{ |
88 |
31 |
return false; |
89 |
|
} |
90 |
1278 |
else if (pattern.getNumChildren() == 0) |
91 |
|
{ |
92 |
|
// fail if the type parameter or type constructors are different |
93 |
|
return pattern == tn; |
94 |
|
} |
95 |
4061 |
for (size_t j = 0, nchild = pattern.getNumChildren(); j < nchild; j++) |
96 |
|
{ |
97 |
2783 |
if (!doMatching(pattern[j], tn[j])) |
98 |
|
{ |
99 |
|
return false; |
100 |
|
} |
101 |
|
} |
102 |
1278 |
return true; |
103 |
|
} |
104 |
|
|
105 |
500 |
void TypeMatcher::getTypes(std::vector<TypeNode>& types) const |
106 |
|
{ |
107 |
500 |
types.clear(); |
108 |
1162 |
for (const TypeNode& t : d_types) |
109 |
|
{ |
110 |
662 |
types.push_back(t); |
111 |
|
} |
112 |
500 |
} |
113 |
1556 |
void TypeMatcher::getMatches(std::vector<TypeNode>& types) const |
114 |
|
{ |
115 |
1556 |
types.clear(); |
116 |
3470 |
for (size_t i = 0, nmatch = d_match.size(); i < nmatch; i++) |
117 |
|
{ |
118 |
1914 |
if (d_match[i].isNull()) |
119 |
|
{ |
120 |
33 |
types.push_back(d_types[i]); |
121 |
|
} |
122 |
|
else |
123 |
|
{ |
124 |
1881 |
types.push_back(d_match[i]); |
125 |
|
} |
126 |
|
} |
127 |
1556 |
} |
128 |
|
|
129 |
29340 |
} // namespace cvc5 |