Coverage Report

Created: 2025-09-04 07:34

/src/solidity/libyul/Utilities.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
 * Small useful snippets for the optimiser.
20
 */
21
22
#pragma once
23
24
#include <libsolutil/Common.h>
25
#include <libsolutil/Numeric.h>
26
#include <libyul/ASTForward.h>
27
28
#include <string_view>
29
30
namespace solidity::yul
31
{
32
33
class Dialect;
34
class EVMDialect;
35
struct BuiltinFunction;
36
struct BuiltinFunctionForEVM;
37
38
std::string reindent(std::string const& _code);
39
40
LiteralValue valueOfNumberLiteral(std::string_view _literal);
41
LiteralValue valueOfStringLiteral(std::string_view _literal);
42
LiteralValue valueOfBuiltinStringLiteralArgument(std::string_view _literal);
43
LiteralValue valueOfBoolLiteral(std::string_view _literal);
44
LiteralValue valueOfLiteral(std::string_view _literal, LiteralKind const& _kind, bool _unlimitedLiteralArgument = false);
45
bool validLiteral(Literal const& _literal);
46
bool validStringLiteral(Literal const& _literal);
47
bool validNumberLiteral(Literal const& _literal);
48
bool validBoolLiteral(Literal const& _literal);
49
50
/// Produces a string representation of a Literal instance.
51
/// @param _literal the Literal to be formatted
52
/// @param _validated whether the Literal was already validated, i.e., assumptions are asserted in the method
53
/// @returns the literal's string representation
54
std::string formatLiteral(Literal const& _literal, bool _validated = true);
55
56
/**
57
 * Linear order on Yul AST nodes.
58
 *
59
 * Defines a linear order on Yul AST nodes to be used in maps and sets.
60
 * Note: the order is total and deterministic, but independent of the semantics, e.g.
61
 * it is not guaranteed that the false Literal is "less" than the true Literal.
62
 */
63
template<typename T>
64
struct Less
65
{
66
  bool operator()(T const& _lhs, T const& _rhs) const;
67
};
68
69
template<typename T>
70
struct Less<T*>
71
{
72
  bool operator()(T const* _lhs, T const* _rhs) const
73
734k
  {
74
734k
    if (_lhs && _rhs)
75
650k
      return Less<T>{}(*_lhs, *_rhs);
76
83.8k
    else
77
83.8k
      return _lhs < _rhs;
78
734k
  }
79
};
80
81
template<> bool Less<Literal>::operator()(Literal const& _lhs, Literal const& _rhs) const;
82
extern template struct Less<Literal>;
83
84
// This can only be used for cases within one switch statement and
85
// relies on the fact that there are no duplicate cases.
86
struct SwitchCaseCompareByLiteralValue
87
{
88
  bool operator()(Case const* _lhsCase, Case const* _rhsCase) const;
89
};
90
91
std::string_view resolveFunctionName(FunctionName const& _functionName, Dialect const& _dialect);
92
93
BuiltinFunction const* resolveBuiltinFunction(FunctionName const& _functionName, Dialect const& _dialect);
94
BuiltinFunctionForEVM const* resolveBuiltinFunctionForEVM(FunctionName const& _functionName, EVMDialect const& _dialect);
95
FunctionHandle functionNameToHandle(FunctionName const& _functionName);
96
97
}