Coverage Report

Created: 2022-08-24 06:28

/src/solidity/liblangutil/Token.cpp
Line
Count
Source (jump to first uncovered line)
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
50
using namespace std;
51
52
namespace solidity::langutil
53
{
54
55
Token TokenTraits::AssignmentToBinaryOp(Token op)
56
0
{
57
0
  solAssert(isAssignmentOp(op) && op != Token::Assign, "");
58
0
  return static_cast<Token>(static_cast<int>(op) + (static_cast<int>(Token::BitOr) - static_cast<int>(Token::AssignBitOr)));
59
0
}
60
61
std::string ElementaryTypeNameToken::toString(bool const& tokenValue) const
62
0
{
63
0
  std::string name = TokenTraits::toString(m_token);
64
0
  if (tokenValue || (firstNumber() == 0 && secondNumber() == 0))
65
0
    return name;
66
0
  solAssert(name.size() >= 3, "Token name size should be greater than 3. Should not reach here.");
67
0
  if (m_token == Token::FixedMxN || m_token == Token::UFixedMxN)
68
0
    return name.substr(0, name.size() - 3) + std::to_string(m_firstNumber) + "x" + std::to_string(m_secondNumber);
69
0
  else
70
0
    return name.substr(0, name.size() - 1) + std::to_string(m_firstNumber);
71
0
}
72
73
void ElementaryTypeNameToken::assertDetails(Token _baseType, unsigned const& _first, unsigned const& _second)
74
11.4k
{
75
11.4k
  solAssert(TokenTraits::isElementaryTypeName(_baseType), "Expected elementary type name: " + string(TokenTraits::toString(_baseType)));
76
11.4k
  if (_baseType == Token::BytesM)
77
2.86k
  {
78
2.86k
    solAssert(_second == 0, "There should not be a second size argument to type bytesM.");
79
2.86k
    solAssert(_first <= 32, "No elementary type bytes" + to_string(_first) + ".");
80
2.86k
  }
81
8.59k
  else if (_baseType == Token::UIntM || _baseType == Token::IntM)
82
3.93k
  {
83
3.93k
    solAssert(_second == 0, "There should not be a second size argument to type " + string(TokenTraits::toString(_baseType)) + ".");
84
3.93k
    solAssert(
85
3.93k
      _first <= 256 && _first % 8 == 0,
86
3.93k
      "No elementary type " + string(TokenTraits::toString(_baseType)) + to_string(_first) + "."
87
3.93k
    );
88
3.93k
  }
89
4.65k
  else if (_baseType == Token::UFixedMxN || _baseType == Token::FixedMxN)
90
0
  {
91
0
    solAssert(
92
0
      _first >= 8 && _first <= 256 && _first % 8 == 0 && _second <= 80,
93
0
      "No elementary type " + string(TokenTraits::toString(_baseType)) + to_string(_first) + "x" + to_string(_second) + "."
94
0
    );
95
0
  }
96
4.65k
  else
97
4.65k
    solAssert(_first == 0 && _second == 0, "Unexpected size arguments");
98
99
11.4k
  m_token = _baseType;
100
11.4k
  m_firstNumber = _first;
101
11.4k
  m_secondNumber = _second;
102
11.4k
}
103
104
namespace TokenTraits
105
{
106
char const* toString(Token tok)
107
716
{
108
716
  switch (tok)
109
716
  {
110
716
#define T(name, string, precedence) case Token::name: return string;
111
716
    TOKEN_LIST(T, T)
112
0
#undef T
113
0
    default: // Token::NUM_TOKENS:
114
0
      return "";
115
716
  }
116
716
}
117
118
char const* name(Token tok)
119
0
{
120
0
#define T(name, string, precedence) #name,
121
0
  static char const* const names[TokenTraits::count()] = { TOKEN_LIST(T, T) };
122
0
#undef T
123
124
0
  solAssert(static_cast<size_t>(tok) < TokenTraits::count(), "");
125
0
  return names[static_cast<size_t>(tok)];
126
0
}
127
128
std::string friendlyName(Token tok)
129
0
{
130
0
  char const* ret = toString(tok);
131
0
  if (ret)
132
0
    return std::string(ret);
133
134
0
  ret = name(tok);
135
0
  solAssert(ret != nullptr, "");
136
0
  return std::string(ret);
137
0
}
138
139
140
static Token keywordByName(string const& _name)
141
28.9k
{
142
  // The following macros are used inside TOKEN_LIST and cause non-keyword tokens to be ignored
143
  // and keywords to be put inside the keywords variable.
144
3.01M
#define KEYWORD(name, string, precedence) {string, Token::name},
145
28.9k
#define TOKEN(name, string, precedence)
146
3.01M
  static map<string, Token> const keywords({TOKEN_LIST(TOKEN, KEYWORD)});
147
28.9k
#undef KEYWORD
148
28.9k
#undef TOKEN
149
28.9k
  auto it = keywords.find(_name);
150
28.9k
  return it == keywords.end() ? Token::Identifier : it->second;
151
28.9k
}
152
153
bool isYulKeyword(string const& _literal)
154
0
{
155
0
  return _literal == "leave" || isYulKeyword(keywordByName(_literal));
156
0
}
157
158
tuple<Token, unsigned int, unsigned int> fromIdentifierOrKeyword(string const& _literal)
159
28.9k
{
160
  // Used for `bytesM`, `uintM`, `intM`, `fixedMxN`, `ufixedMxN`.
161
  // M/N must be shortest representation. M can never be 0. N can be zero.
162
28.9k
  auto parseSize = [](string::const_iterator _begin, string::const_iterator _end) -> int
163
28.9k
  {
164
    // No number.
165
9.66k
    if (distance(_begin, _end) == 0)
166
0
      return -1;
167
168
    // Disallow leading zero.
169
9.66k
    if (distance(_begin, _end) > 1 && *_begin == '0')
170
0
      return -1;
171
172
9.66k
    int ret = 0;
173
33.6k
    for (auto it = _begin; it != _end; it++)
174
23.9k
    {
175
23.9k
      if (*it < '0' || *it > '9')
176
0
        return -1;
177
      //  Overflow check. The largest acceptable value is 256 in the callers.
178
23.9k
      if (ret >= 256)
179
0
        return -1;
180
23.9k
      ret *= 10;
181
23.9k
      ret += *it - '0';
182
23.9k
    }
183
9.66k
    return ret;
184
9.66k
  };
185
186
28.9k
  auto positionM = find_if(_literal.begin(), _literal.end(), util::isDigit);
187
28.9k
  if (positionM != _literal.end())
188
9.66k
  {
189
9.66k
    string baseType(_literal.begin(), positionM);
190
9.66k
    auto positionX = find_if_not(positionM, _literal.end(), util::isDigit);
191
9.66k
    int m = parseSize(positionM, positionX);
192
9.66k
    Token keyword = keywordByName(baseType);
193
9.66k
    if (keyword == Token::Bytes)
194
2.86k
    {
195
2.86k
      if (0 < m && m <= 32 && positionX == _literal.end())
196
2.86k
        return make_tuple(Token::BytesM, m, 0);
197
2.86k
    }
198
6.80k
    else if (keyword == Token::UInt || keyword == Token::Int)
199
3.93k
    {
200
3.93k
      if (0 < m && m <= 256 && m % 8 == 0 && positionX == _literal.end())
201
3.93k
      {
202
3.93k
        if (keyword == Token::UInt)
203
3.93k
          return make_tuple(Token::UIntM, m, 0);
204
0
        else
205
0
          return make_tuple(Token::IntM, m, 0);
206
3.93k
      }
207
3.93k
    }
208
2.86k
    else if (keyword == Token::UFixed || keyword == Token::Fixed)
209
0
    {
210
0
      if (
211
0
        positionM < positionX &&
212
0
        positionX < _literal.end() &&
213
0
        *positionX == 'x' &&
214
0
        all_of(positionX + 1, _literal.end(), util::isDigit)
215
0
      ) {
216
0
        int n = parseSize(positionX + 1, _literal.end());
217
0
        if (
218
0
          8 <= m && m <= 256 && m % 8 == 0 &&
219
0
          0 <= n && n <= 80
220
0
        ) {
221
0
          if (keyword == Token::UFixed)
222
0
            return make_tuple(Token::UFixedMxN, m, n);
223
0
          else
224
0
            return make_tuple(Token::FixedMxN, m, n);
225
0
        }
226
0
      }
227
0
    }
228
2.86k
    return make_tuple(Token::Identifier, 0, 0);
229
9.66k
  }
230
231
19.3k
  return make_tuple(keywordByName(_literal), 0, 0);
232
28.9k
}
233
234
}
235
}