Coverage Report

Created: 2022-08-24 06:55

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