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  * String abstraction that avoids copies.
20  */
21 
22 #pragma once
23 
24 #include <fmt/format.h>
25 
26 #include <unordered_map>
27 #include <memory>
28 #include <vector>
29 #include <string>
30 #include <functional>
31 
32 namespace solidity::yul
33 {
34 
35 /// Repository for YulStrings.
36 /// Owns the string data for all YulStrings, which can be referenced by a Handle.
37 /// A Handle consists of an ID (that depends on the insertion order of YulStrings and is potentially
38 /// non-deterministic) and a deterministic string hash.
39 class YulStringRepository
40 {
41 public:
42 	struct Handle
43 	{
44 		size_t id;
45 		std::uint64_t hash;
46 	};
47 
instance()48 	static YulStringRepository& instance()
49 	{
50 		static YulStringRepository inst;
51 		return inst;
52 	}
53 
stringToHandle(std::string const & _string)54 	Handle stringToHandle(std::string const& _string)
55 	{
56 		if (_string.empty())
57 			return { 0, emptyHash() };
58 		std::uint64_t h = hash(_string);
59 		auto range = m_hashToID.equal_range(h);
60 		for (auto it = range.first; it != range.second; ++it)
61 			if (*m_strings[it->second] == _string)
62 				return Handle{it->second, h};
63 		m_strings.emplace_back(std::make_shared<std::string>(_string));
64 		size_t id = m_strings.size() - 1;
65 		m_hashToID.emplace_hint(range.second, std::make_pair(h, id));
66 
67 		return Handle{id, h};
68 	}
idToString(size_t _id)69 	std::string const& idToString(size_t _id) const	{ return *m_strings.at(_id); }
70 
hash(std::string const & v)71 	static std::uint64_t hash(std::string const& v)
72 	{
73 		// FNV hash - can be replaced by a better one, e.g. xxhash64
74 		std::uint64_t hash = emptyHash();
75 		for (char c: v)
76 		{
77 			hash *= 1099511628211u;
78 			hash ^= static_cast<uint64_t>(c);
79 		}
80 
81 		return hash;
82 	}
emptyHash()83 	static constexpr std::uint64_t emptyHash() { return 14695981039346656037u; }
84 	/// Clear the repository.
85 	/// Use with care - there cannot be any dangling YulString references.
86 	/// If references need to be cleared manually, register the callback via
87 	/// resetCallback.
reset()88 	static void reset()
89 	{
90 		for (auto const& cb: resetCallbacks())
91 			cb();
92 		instance() = YulStringRepository{};
93 	}
94 	/// Struct that registers a reset callback as a side-effect of its construction.
95 	/// Useful as static local variable to register a reset callback once.
96 	struct ResetCallback
97 	{
ResetCallbackResetCallback98 		ResetCallback(std::function<void()> _fun)
99 		{
100 			YulStringRepository::resetCallbacks().emplace_back(std::move(_fun));
101 		}
102 	};
103 
104 private:
105 	YulStringRepository() = default;
106 	YulStringRepository(YulStringRepository const&) = delete;
107 	YulStringRepository(YulStringRepository&&) = default;
108 	YulStringRepository& operator=(YulStringRepository const& _rhs) = delete;
109 	YulStringRepository& operator=(YulStringRepository&& _rhs) = default;
110 
resetCallbacks()111 	static std::vector<std::function<void()>>& resetCallbacks()
112 	{
113 		static std::vector<std::function<void()>> callbacks;
114 		return callbacks;
115 	}
116 
117 	std::vector<std::shared_ptr<std::string>> m_strings = {std::make_shared<std::string>()};
118 	std::unordered_multimap<std::uint64_t, size_t> m_hashToID = {{emptyHash(), 0}};
119 };
120 
121 /// Wrapper around handles into the YulString repository.
122 /// Equality of two YulStrings is determined by comparing their ID.
123 /// The <-operator depends on the string hash and is not consistent
124 /// with string comparisons (however, it is still deterministic).
125 class YulString
126 {
127 public:
128 	YulString() = default;
YulString(std::string const & _s)129 	explicit YulString(std::string const& _s): m_handle(YulStringRepository::instance().stringToHandle(_s)) {}
130 	YulString(YulString const&) = default;
131 	YulString(YulString&&) = default;
132 	YulString& operator=(YulString const&) = default;
133 	YulString& operator=(YulString&&) = default;
134 
135 	/// This is not consistent with the string <-operator!
136 	/// First compares the string hashes. If they are equal
137 	/// it checks for identical IDs (only identical strings have
138 	/// identical IDs and identical strings do not compare as "less").
139 	/// If the hashes are identical and the strings are distinct, it
140 	/// falls back to string comparison.
141 	bool operator<(YulString const& _other) const
142 	{
143 		if (m_handle.hash < _other.m_handle.hash) return true;
144 		if (_other.m_handle.hash < m_handle.hash) return false;
145 		if (m_handle.id == _other.m_handle.id) return false;
146 		return str() < _other.str();
147 	}
148 	/// Equality is determined based on the string ID.
149 	bool operator==(YulString const& _other) const { return m_handle.id == _other.m_handle.id; }
150 	bool operator!=(YulString const& _other) const { return m_handle.id != _other.m_handle.id; }
151 
empty()152 	bool empty() const { return m_handle.id == 0; }
str()153 	std::string const& str() const
154 	{
155 		return YulStringRepository::instance().idToString(m_handle.id);
156 	}
157 
hash()158 	uint64_t hash() const { return m_handle.hash; }
159 
160 private:
161 	/// Handle of the string. Assumes that the empty string has ID zero.
162 	YulStringRepository::Handle m_handle{ 0, YulStringRepository::emptyHash() };
163 };
164 
165 inline YulString operator "" _yulstring(char const* _string, std::size_t _size)
166 {
167 	return YulString(std::string(_string, _size));
168 }
169 
170 }
171 
172 namespace fmt
173 {
174 template <>
175 struct formatter<solidity::yul::YulString>
176 {
177 	template <typename ParseContext>
178 	constexpr auto parse(ParseContext& _context)
179 	{
180 		return _context.begin();
181 	}
182 
183 	template <typename FormatContext>
184 	auto format(solidity::yul::YulString _value, FormatContext& _context)
185 	{
186 		return format_to(_context.out(), "{}", _value.str());
187 	}
188 };
189 }
190 
191 namespace std
192 {
193 template<> struct hash<solidity::yul::YulString>
194 {
195 	size_t operator()(solidity::yul::YulString const& x) const
196 	{
197 		return static_cast<size_t>(x.hash());
198 	}
199 };
200 }
201