Coverage Report

Created: 2022-08-24 06:55

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