Coverage Report

Created: 2022-08-24 06:38

/src/solidity/libyul/optimiser/ASTCopier.cpp
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
 * Creates an independent copy of an AST, renaming identifiers to be unique.
20
 */
21
22
#include <libyul/optimiser/ASTCopier.h>
23
24
#include <libyul/AST.h>
25
26
#include <libsolutil/Common.h>
27
28
using namespace std;
29
using namespace solidity;
30
using namespace solidity::yul;
31
using namespace solidity::util;
32
33
Statement ASTCopier::operator()(ExpressionStatement const& _statement)
34
349k
{
35
349k
  return ExpressionStatement{ _statement.debugData, translate(_statement.expression) };
36
349k
}
37
38
Statement ASTCopier::operator()(VariableDeclaration const& _varDecl)
39
1.26M
{
40
1.26M
  return VariableDeclaration{
41
1.26M
    _varDecl.debugData,
42
1.26M
    translateVector(_varDecl.variables),
43
1.26M
    translate(_varDecl.value)
44
1.26M
  };
45
1.26M
}
46
47
Statement ASTCopier::operator()(Assignment const& _assignment)
48
62.4k
{
49
62.4k
  return Assignment{
50
62.4k
    _assignment.debugData,
51
62.4k
    translateVector(_assignment.variableNames),
52
62.4k
    translate(_assignment.value)
53
62.4k
  };
54
62.4k
}
55
56
Expression ASTCopier::operator()(FunctionCall const& _call)
57
818k
{
58
818k
  return FunctionCall{
59
818k
    _call.debugData,
60
818k
    translate(_call.functionName),
61
818k
    translateVector(_call.arguments)
62
818k
  };
63
818k
}
64
65
Expression ASTCopier::operator()(Identifier const& _identifier)
66
1.35M
{
67
1.35M
  return translate(_identifier);
68
1.35M
}
69
70
Expression ASTCopier::operator()(Literal const& _literal)
71
2.01M
{
72
2.01M
  return translate(_literal);
73
2.01M
}
74
75
Statement ASTCopier::operator()(If const& _if)
76
31.7k
{
77
31.7k
  return If{_if.debugData, translate(_if.condition), translate(_if.body)};
78
31.7k
}
79
80
Statement ASTCopier::operator()(Switch const& _switch)
81
4.50k
{
82
4.50k
  return Switch{_switch.debugData, translate(_switch.expression), translateVector(_switch.cases)};
83
4.50k
}
84
85
Statement ASTCopier::operator()(FunctionDefinition const& _function)
86
63.4k
{
87
63.4k
  YulString translatedName = translateIdentifier(_function.name);
88
89
63.4k
  enterFunction(_function);
90
63.4k
  ScopeGuard g([&]() { this->leaveFunction(_function); });
91
92
63.4k
  return FunctionDefinition{
93
63.4k
    _function.debugData,
94
63.4k
    translatedName,
95
63.4k
    translateVector(_function.parameters),
96
63.4k
    translateVector(_function.returnVariables),
97
63.4k
    translate(_function.body)
98
63.4k
  };
99
63.4k
}
100
101
Statement ASTCopier::operator()(ForLoop const& _forLoop)
102
38.5k
{
103
38.5k
  enterScope(_forLoop.pre);
104
38.5k
  ScopeGuard g([&]() { this->leaveScope(_forLoop.pre); });
105
106
38.5k
  return ForLoop{
107
38.5k
    _forLoop.debugData,
108
38.5k
    translate(_forLoop.pre),
109
38.5k
    translate(_forLoop.condition),
110
38.5k
    translate(_forLoop.post),
111
38.5k
    translate(_forLoop.body)
112
38.5k
  };
113
38.5k
}
114
Statement ASTCopier::operator()(Break const& _break)
115
6.48k
{
116
6.48k
  return Break{ _break };
117
6.48k
}
118
119
Statement ASTCopier::operator()(Continue const& _continue)
120
2.68k
{
121
2.68k
  return Continue{ _continue };
122
2.68k
}
123
124
Statement ASTCopier::operator()(Leave const& _leaveStatement)
125
5.37k
{
126
5.37k
  return Leave{_leaveStatement};
127
5.37k
}
128
129
Statement ASTCopier::operator ()(Block const& _block)
130
108k
{
131
108k
  return translate(_block);
132
108k
}
133
134
Expression ASTCopier::translate(Expression const& _expression)
135
4.18M
{
136
4.18M
  return std::visit(static_cast<ExpressionCopier&>(*this), _expression);
137
4.18M
}
138
139
Statement ASTCopier::translate(Statement const& _statement)
140
1.80M
{
141
1.80M
  return std::visit(static_cast<StatementCopier&>(*this), _statement);
142
1.80M
}
143
144
Block ASTCopier::translate(Block const& _block)
145
329k
{
146
329k
  enterScope(_block);
147
329k
  ScopeGuard g([&]() { this->leaveScope(_block); });
148
149
329k
  return Block{_block.debugData, translateVector(_block.statements)};
150
329k
}
151
152
Case ASTCopier::translate(Case const& _case)
153
9.40k
{
154
9.40k
  return Case{_case.debugData, translate(_case.value), translate(_case.body)};
155
9.40k
}
156
157
Identifier ASTCopier::translate(Identifier const& _identifier)
158
2.23M
{
159
2.23M
  return Identifier{_identifier.debugData, translateIdentifier(_identifier.name)};
160
2.23M
}
161
162
Literal ASTCopier::translate(Literal const& _literal)
163
2.01M
{
164
2.01M
  return _literal;
165
2.01M
}
166
167
TypedName ASTCopier::translate(TypedName const& _typedName)
168
1.33M
{
169
1.33M
  return TypedName{_typedName.debugData, translateIdentifier(_typedName.name), _typedName.type};
170
1.33M
}
171
172
YulString FunctionCopier::translateIdentifier(YulString _name)
173
1.80M
{
174
1.80M
  if (m_translations.count(_name))
175
1.40M
    return m_translations.at(_name);
176
394k
  return _name;
177
1.80M
}