/src/solidity/libsmtutil/SMTLib2Context.h
Line | Count | Source |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | |
19 | | #pragma once |
20 | | |
21 | | #include <libsmtutil/SolverInterface.h> |
22 | | #include <libsmtutil/Sorts.h> |
23 | | |
24 | | #include <string> |
25 | | #include <unordered_map> |
26 | | #include <unordered_set> |
27 | | |
28 | | namespace solidity::smtutil |
29 | | { |
30 | | |
31 | | using SortId = uint32_t; |
32 | | struct SortPairHash |
33 | | { |
34 | | std::size_t operator()(std::pair<SortId, SortId> const& _pair) const; |
35 | | }; |
36 | | |
37 | | struct SMTLibType { |
38 | | Kind const kind; |
39 | | SortId const id; |
40 | 436k | SMTLibType(Kind _kind, SortId _id): kind(_kind), id(_id) {} |
41 | 436k | virtual ~SMTLibType() = default; |
42 | | }; |
43 | | |
44 | | struct SMTLibSort : public SMTLibType |
45 | | { |
46 | | std::string const name; |
47 | | std::vector<SortId> const args; |
48 | | |
49 | | SMTLibSort( |
50 | | Kind _kind, |
51 | | std::string_view _name, |
52 | | std::vector<SortId> _args, |
53 | | SortId _id |
54 | 242k | ): SMTLibType(_kind, _id), name(_name), args(std::move(_args)) {} |
55 | | }; |
56 | | |
57 | | struct TupleType : public SMTLibType |
58 | | { |
59 | | std::string const name; |
60 | | std::vector<std::pair<std::string,SortId>> accessors; |
61 | | |
62 | | TupleType(std::string_view _name, std::vector<std::pair<std::string,SortId>> _accessors, SortId _id) |
63 | 193k | : SMTLibType(Kind::Tuple, _id), name(_name), accessors(std::move(_accessors)) {} |
64 | | }; |
65 | | |
66 | | class SMTLib2Context |
67 | | { |
68 | | public: |
69 | | using TupleDeclarationCallback = std::function<void(TupleSort const&)>; |
70 | | |
71 | | SMTLib2Context(); |
72 | | void clear(); |
73 | | |
74 | | bool isDeclared(std::string const& _name) const; |
75 | | void declare(std::string const& _name, SortPointer const& _sort); |
76 | | SortPointer getDeclaredSort(std::string const& _name) const; |
77 | | |
78 | | SortId resolve(SortPointer const& _sort); |
79 | | SortPointer unresolve(SortId _sortId) const; |
80 | | |
81 | | std::string toString(SortId _id); |
82 | | |
83 | | std::string toSExpr(Expression const& _expr); |
84 | | std::string toSmtLibSort(SortPointer const& _sort); |
85 | | |
86 | | std::optional<SortPointer> getTupleType(std::string const& _name) const; |
87 | | std::optional<std::pair<std::string, SortPointer>> getTupleAccessor(std::string const& _name) const; |
88 | | |
89 | | void setTupleDeclarationCallback(TupleDeclarationCallback _callback); |
90 | | private: |
91 | | SortId resolveBitVectorSort(BitVectorSort const& _sort); |
92 | | SortId resolveArraySort(ArraySort const& _sort); |
93 | | SortId resolveTupleSort(TupleSort const& _sort); |
94 | | |
95 | | using functions_t = std::map<std::string, SortPointer>; |
96 | | functions_t m_functions; // Variables are uninterpreted constants = nullary functions |
97 | | |
98 | | SortId const m_boolSort{0u}; |
99 | | SortId const m_intSort{1u}; |
100 | | std::vector<std::unique_ptr<SMTLibType>> m_knownTypes; |
101 | | std::unordered_map<std::pair<SortId, SortId>, SortId, SortPairHash> m_arraySorts; |
102 | | std::unordered_map<std::size_t, SortId> m_bitVectorSorts; |
103 | | std::unordered_map<std::string, SortId> m_tupleSorts; |
104 | | |
105 | | TupleDeclarationCallback m_callback; |
106 | | }; |
107 | | |
108 | | } |
109 | | |