Coverage Report

Created: 2022-08-24 06:43

/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
367k
{
35
367k
  return ExpressionStatement{ _statement.debugData, translate(_statement.expression) };
36
367k
}
37
38
Statement ASTCopier::operator()(VariableDeclaration const& _varDecl)
39
2.54M
{
40
2.54M
  return VariableDeclaration{
41
2.54M
    _varDecl.debugData,
42
2.54M
    translateVector(_varDecl.variables),
43
2.54M
    translate(_varDecl.value)
44
2.54M
  };
45
2.54M
}
46
47
Statement ASTCopier::operator()(Assignment const& _assignment)
48
356k
{
49
356k
  return Assignment{
50
356k
    _assignment.debugData,
51
356k
    translateVector(_assignment.variableNames),
52
356k
    translate(_assignment.value)
53
356k
  };
54
356k
}
55
56
Expression ASTCopier::operator()(FunctionCall const& _call)
57
1.31M
{
58
1.31M
  return FunctionCall{
59
1.31M
    _call.debugData,
60
1.31M
    translate(_call.functionName),
61
1.31M
    translateVector(_call.arguments)
62
1.31M
  };
63
1.31M
}
64
65
Expression ASTCopier::operator()(Identifier const& _identifier)
66
3.48M
{
67
3.48M
  return translate(_identifier);
68
3.48M
}
69
70
Expression ASTCopier::operator()(Literal const& _literal)
71
1.67M
{
72
1.67M
  return translate(_literal);
73
1.67M
}
74
75
Statement ASTCopier::operator()(If const& _if)
76
143k
{
77
143k
  return If{_if.debugData, translate(_if.condition), translate(_if.body)};
78
143k
}
79
80
Statement ASTCopier::operator()(Switch const& _switch)
81
6.40k
{
82
6.40k
  return Switch{_switch.debugData, translate(_switch.expression), translateVector(_switch.cases)};
83
6.40k
}
84
85
Statement ASTCopier::operator()(FunctionDefinition const& _function)
86
137k
{
87
137k
  YulString translatedName = translateIdentifier(_function.name);
88
89
137k
  enterFunction(_function);
90
137k
  ScopeGuard g([&]() { this->leaveFunction(_function); });
91
92
137k
  return FunctionDefinition{
93
137k
    _function.debugData,
94
137k
    translatedName,
95
137k
    translateVector(_function.parameters),
96
137k
    translateVector(_function.returnVariables),
97
137k
    translate(_function.body)
98
137k
  };
99
137k
}
100
101
Statement ASTCopier::operator()(ForLoop const& _forLoop)
102
27.7k
{
103
27.7k
  enterScope(_forLoop.pre);
104
27.7k
  ScopeGuard g([&]() { this->leaveScope(_forLoop.pre); });
105
106
27.7k
  return ForLoop{
107
27.7k
    _forLoop.debugData,
108
27.7k
    translate(_forLoop.pre),
109
27.7k
    translate(_forLoop.condition),
110
27.7k
    translate(_forLoop.post),
111
27.7k
    translate(_forLoop.body)
112
27.7k
  };
113
27.7k
}
114
Statement ASTCopier::operator()(Break const& _break)
115
22.2k
{
116
22.2k
  return Break{ _break };
117
22.2k
}
118
119
Statement ASTCopier::operator()(Continue const& _continue)
120
4
{
121
4
  return Continue{ _continue };
122
4
}
123
124
Statement ASTCopier::operator()(Leave const& _leaveStatement)
125
25.2k
{
126
25.2k
  return Leave{_leaveStatement};
127
25.2k
}
128
129
Statement ASTCopier::operator ()(Block const& _block)
130
68.3k
{
131
68.3k
  return translate(_block);
132
68.3k
}
133
134
Expression ASTCopier::translate(Expression const& _expression)
135
6.47M
{
136
6.47M
  return std::visit(static_cast<ExpressionCopier&>(*this), _expression);
137
6.47M
}
138
139
Statement ASTCopier::translate(Statement const& _statement)
140
3.60M
{
141
3.60M
  return std::visit(static_cast<StatementCopier&>(*this), _statement);
142
3.60M
}
143
144
Block ASTCopier::translate(Block const& _block)
145
446k
{
146
446k
  enterScope(_block);
147
446k
  ScopeGuard g([&]() { this->leaveScope(_block); });
148
149
446k
  return Block{_block.debugData, translateVector(_block.statements)};
150
446k
}
151
152
Case ASTCopier::translate(Case const& _case)
153
12.9k
{
154
12.9k
  return Case{_case.debugData, translate(_case.value), translate(_case.body)};
155
12.9k
}
156
157
Identifier ASTCopier::translate(Identifier const& _identifier)
158
5.15M
{
159
5.15M
  return Identifier{_identifier.debugData, translateIdentifier(_identifier.name)};
160
5.15M
}
161
162
Literal ASTCopier::translate(Literal const& _literal)
163
1.68M
{
164
1.68M
  return _literal;
165
1.68M
}
166
167
TypedName ASTCopier::translate(TypedName const& _typedName)
168
2.83M
{
169
2.83M
  return TypedName{_typedName.debugData, translateIdentifier(_typedName.name), _typedName.type};
170
2.83M
}
171
172
YulString FunctionCopier::translateIdentifier(YulString _name)
173
2.63M
{
174
2.63M
  if (m_translations.count(_name))
175
2.20M
    return m_translations.at(_name);
176
435k
  return _name;
177
2.63M
}