/src/solidity/liblangutil/Token.cpp
Line | Count | Source |
1 | | // Copyright 2006-2012, the V8 project authors. All rights reserved. |
2 | | // Redistribution and use in source and binary forms, with or without |
3 | | // modification, are permitted provided that the following conditions are |
4 | | // met: |
5 | | // |
6 | | // * Redistributions of source code must retain the above copyright |
7 | | // notice, this list of conditions and the following disclaimer. |
8 | | // * Redistributions in binary form must reproduce the above |
9 | | // copyright notice, this list of conditions and the following |
10 | | // disclaimer in the documentation and/or other materials provided |
11 | | // with the distribution. |
12 | | // * Neither the name of Google Inc. nor the names of its |
13 | | // contributors may be used to endorse or promote products derived |
14 | | // from this software without specific prior written permission. |
15 | | // |
16 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | | // |
28 | | // Modifications as part of solidity under the following license: |
29 | | // |
30 | | // solidity is free software: you can redistribute it and/or modify |
31 | | // it under the terms of the GNU General Public License as published by |
32 | | // the Free Software Foundation, either version 3 of the License, or |
33 | | // (at your option) any later version. |
34 | | // |
35 | | // solidity is distributed in the hope that it will be useful, |
36 | | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
37 | | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
38 | | // GNU General Public License for more details. |
39 | | // |
40 | | // You should have received a copy of the GNU General Public License |
41 | | // along with solidity. If not, see <http://www.gnu.org/licenses/>. |
42 | | |
43 | | #include <liblangutil/Exceptions.h> |
44 | | #include <liblangutil/Token.h> |
45 | | #include <libsolutil/StringUtils.h> |
46 | | |
47 | | #include <map> |
48 | | |
49 | | namespace solidity::langutil |
50 | | { |
51 | | |
52 | | Token TokenTraits::AssignmentToBinaryOp(Token op) |
53 | 2.26k | { |
54 | 2.26k | solAssert(isAssignmentOp(op) && op != Token::Assign, ""); |
55 | 2.26k | return static_cast<Token>(static_cast<int>(op) + (static_cast<int>(Token::BitOr) - static_cast<int>(Token::AssignBitOr))); |
56 | 2.26k | } |
57 | | |
58 | | std::string ElementaryTypeNameToken::toString(bool const& tokenValue) const |
59 | 57 | { |
60 | 57 | std::string name = TokenTraits::toString(m_token); |
61 | 57 | if (tokenValue || (firstNumber() == 0 && secondNumber() == 0)) |
62 | 23 | return name; |
63 | 34 | solAssert(name.size() >= 3, "Token name size should be greater than 3. Should not reach here."); |
64 | 34 | if (m_token == Token::FixedMxN || m_token == Token::UFixedMxN) |
65 | 17 | return name.substr(0, name.size() - 3) + std::to_string(m_firstNumber) + "x" + std::to_string(m_secondNumber); |
66 | 17 | else |
67 | 17 | return name.substr(0, name.size() - 1) + std::to_string(m_firstNumber); |
68 | 34 | } |
69 | | |
70 | | void ElementaryTypeNameToken::assertDetails(Token _baseType, unsigned const& _first, unsigned const& _second) |
71 | 941k | { |
72 | 941k | solAssert(TokenTraits::isElementaryTypeName(_baseType), "Expected elementary type name: " + std::string(TokenTraits::toString(_baseType))); |
73 | 941k | if (_baseType == Token::BytesM) |
74 | 214k | { |
75 | 214k | solAssert(_second == 0, "There should not be a second size argument to type bytesM."); |
76 | 214k | solAssert(_first <= 32, "No elementary type bytes" + std::to_string(_first) + "."); |
77 | 214k | } |
78 | 726k | else if (_baseType == Token::UIntM || _baseType == Token::IntM) |
79 | 320k | { |
80 | 320k | solAssert(_second == 0, "There should not be a second size argument to type " + std::string(TokenTraits::toString(_baseType)) + "."); |
81 | 320k | solAssert( |
82 | 320k | _first <= 256 && _first % 8 == 0, |
83 | 320k | "No elementary type " + std::string(TokenTraits::toString(_baseType)) + std::to_string(_first) + "." |
84 | 320k | ); |
85 | 320k | } |
86 | 406k | else if (_baseType == Token::UFixedMxN || _baseType == Token::FixedMxN) |
87 | 197 | { |
88 | 197 | solAssert( |
89 | 197 | _first >= 8 && _first <= 256 && _first % 8 == 0 && _second <= 80, |
90 | 197 | "No elementary type " + std::string(TokenTraits::toString(_baseType)) + std::to_string(_first) + "x" + std::to_string(_second) + "." |
91 | 197 | ); |
92 | 197 | } |
93 | 406k | else |
94 | 406k | solAssert(_first == 0 && _second == 0, "Unexpected size arguments"); |
95 | | |
96 | 941k | m_token = _baseType; |
97 | 941k | m_firstNumber = _first; |
98 | 941k | m_secondNumber = _second; |
99 | 941k | } |
100 | | |
101 | | namespace TokenTraits |
102 | | { |
103 | | char const* toString(Token tok) |
104 | 163k | { |
105 | 163k | switch (tok) |
106 | 163k | { |
107 | 163k | #define T(name, string, precedence) case Token::name: return string; |
108 | 100k | TOKEN_LIST(T, T) |
109 | 0 | #undef T |
110 | 0 | default: // Token::NUM_TOKENS: |
111 | 0 | return ""; |
112 | 163k | } |
113 | 163k | } |
114 | | |
115 | | char const* name(Token tok) |
116 | 358 | { |
117 | 64.0k | #define T(name, string, precedence) #name, |
118 | 39.7k | static char const* const names[TokenTraits::count()] = { TOKEN_LIST(T, T) }; |
119 | 358 | #undef T |
120 | | |
121 | 358 | solAssert(static_cast<size_t>(tok) < TokenTraits::count(), ""); |
122 | 358 | return names[static_cast<size_t>(tok)]; |
123 | 358 | } |
124 | | |
125 | | std::string friendlyName(Token tok) |
126 | 21.4k | { |
127 | 21.4k | char const* ret = toString(tok); |
128 | 21.4k | if (ret) |
129 | 21.1k | return std::string(ret); |
130 | | |
131 | 358 | ret = name(tok); |
132 | 358 | solAssert(ret != nullptr, ""); |
133 | 358 | return std::string(ret); |
134 | 21.4k | } |
135 | | |
136 | | |
137 | | static Token keywordByName(std::string_view const _name) |
138 | 107M | { |
139 | | // The following macros are used inside TOKEN_LIST and cause non-keyword tokens to be ignored |
140 | | // and keywords to be put inside the keywords variable. |
141 | 11.9G | #define KEYWORD(name, string, precedence) {string, Token::name}, |
142 | 107M | #define TOKEN(name, string, precedence) |
143 | 11.9G | static std::map<std::string, Token, std::less<>> const keywords({TOKEN_LIST(TOKEN, KEYWORD)}); |
144 | 107M | #undef KEYWORD |
145 | 107M | #undef TOKEN |
146 | 107M | auto it = keywords.find(_name); |
147 | 107M | return it == keywords.end() ? Token::Identifier : it->second; |
148 | 107M | } |
149 | | |
150 | | bool isYulKeyword(std::string_view const _literal) |
151 | 84.6M | { |
152 | 84.6M | return isYulKeyword(keywordByName(_literal)); |
153 | 84.6M | } |
154 | | |
155 | | bool isFutureSolidityKeyword(std::string_view const _literal) |
156 | 170k | { |
157 | 170k | return |
158 | 170k | _literal == "transient" || |
159 | 170k | _literal == "layout" || |
160 | 170k | _literal == "at" || |
161 | 170k | _literal == "error" || |
162 | 170k | _literal == "super" || |
163 | 170k | _literal == "this" || |
164 | 169k | isFutureYulKeyword(_literal); |
165 | 170k | } |
166 | | |
167 | | bool isFutureYulKeyword(std::string_view const _literal) |
168 | 94.0M | { |
169 | 94.0M | return _literal == "leave"; |
170 | 94.0M | } |
171 | | |
172 | | bool isFutureYulReservedIdentifier(std::string_view const _literal) |
173 | 9.28M | { |
174 | 9.28M | return |
175 | 9.28M | _literal == "basefee" || |
176 | 9.28M | _literal == "blobbasefee" || |
177 | 9.28M | _literal == "blobhash" || |
178 | 9.28M | _literal == "clz" || |
179 | 9.28M | _literal == "mcopy" || |
180 | 9.28M | _literal == "memoryguard" || |
181 | 9.28M | _literal == "prevrandao" || |
182 | 9.28M | _literal == "tload" || |
183 | 9.28M | _literal == "tstore"; |
184 | 9.28M | } |
185 | | |
186 | | std::tuple<Token, unsigned int, unsigned int> fromIdentifierOrKeyword(std::string const& _literal) |
187 | 22.9M | { |
188 | | // Used for `bytesM`, `uintM`, `intM`, `fixedMxN`, `ufixedMxN`. |
189 | | // M/N must be shortest representation. M can never be 0. N can be zero. |
190 | 22.9M | auto parseSize = [](std::string::const_iterator _begin, std::string::const_iterator _end) -> int |
191 | 22.9M | { |
192 | | // No number. |
193 | 7.07M | if (distance(_begin, _end) == 0) |
194 | 833 | return -1; |
195 | | |
196 | | // Disallow leading zero. |
197 | 7.07M | if (distance(_begin, _end) > 1 && *_begin == '0') |
198 | 4.05k | return -1; |
199 | | |
200 | 7.07M | int ret = 0; |
201 | 21.6M | for (auto it = _begin; it != _end; it++) |
202 | 14.8M | { |
203 | 14.8M | if (*it < '0' || *it > '9') |
204 | 0 | return -1; |
205 | | // Overflow check. The largest acceptable value is 256 in the callers. |
206 | 14.8M | if (ret >= 256) |
207 | 231k | return -1; |
208 | 14.6M | ret *= 10; |
209 | 14.6M | ret += *it - '0'; |
210 | 14.6M | } |
211 | 6.84M | return ret; |
212 | 7.07M | }; |
213 | | |
214 | 22.9M | auto positionM = find_if(_literal.begin(), _literal.end(), util::isDigit); |
215 | 22.9M | if (positionM != _literal.end()) |
216 | 7.07M | { |
217 | 7.07M | std::string baseType(_literal.begin(), positionM); |
218 | 7.07M | auto positionX = find_if_not(positionM, _literal.end(), util::isDigit); |
219 | 7.07M | int m = parseSize(positionM, positionX); |
220 | 7.07M | Token keyword = keywordByName(baseType); |
221 | 7.07M | if (keyword == Token::Bytes) |
222 | 215k | { |
223 | 215k | if (0 < m && m <= 32 && positionX == _literal.end()) |
224 | 215k | return std::make_tuple(Token::BytesM, m, 0); |
225 | 215k | } |
226 | 6.86M | else if (keyword == Token::UInt || keyword == Token::Int) |
227 | 322k | { |
228 | 322k | if (0 < m && m <= 256 && m % 8 == 0 && positionX == _literal.end()) |
229 | 320k | { |
230 | 320k | if (keyword == Token::UInt) |
231 | 317k | return std::make_tuple(Token::UIntM, m, 0); |
232 | 3.58k | else |
233 | 3.58k | return std::make_tuple(Token::IntM, m, 0); |
234 | 320k | } |
235 | 322k | } |
236 | 6.53M | else if (keyword == Token::UFixed || keyword == Token::Fixed) |
237 | 3.63k | { |
238 | 3.63k | if ( |
239 | 3.63k | positionM < positionX && |
240 | 3.63k | positionX < _literal.end() && |
241 | 3.15k | *positionX == 'x' && |
242 | 2.80k | all_of(positionX + 1, _literal.end(), util::isDigit) |
243 | 3.63k | ) { |
244 | 2.40k | int n = parseSize(positionX + 1, _literal.end()); |
245 | 2.40k | if ( |
246 | 2.40k | 8 <= m && m <= 256 && m % 8 == 0 && |
247 | 1.13k | 0 <= n && n <= 80 |
248 | 2.40k | ) { |
249 | 482 | if (keyword == Token::UFixed) |
250 | 223 | return std::make_tuple(Token::UFixedMxN, m, n); |
251 | 259 | else |
252 | 259 | return std::make_tuple(Token::FixedMxN, m, n); |
253 | 482 | } |
254 | 2.40k | } |
255 | 3.63k | } |
256 | 6.54M | return std::make_tuple(Token::Identifier, 0, 0); |
257 | 7.07M | } |
258 | | |
259 | 15.8M | return std::make_tuple(keywordByName(_literal), 0, 0); |
260 | 22.9M | } |
261 | | |
262 | | } |
263 | | } |