Coverage Report

Created: 2022-08-24 06:43

/src/solidity/libsolidity/codegen/ir/Common.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
#include <libsolidity/ast/TypeProvider.h>
20
#include <libsolidity/codegen/ir/Common.h>
21
#include <libsolidity/codegen/ir/IRGenerationContext.h>
22
23
#include <libsolutil/CommonIO.h>
24
25
#include <libyul/AsmPrinter.h>
26
27
using namespace std;
28
using namespace solidity::langutil;
29
using namespace solidity::frontend;
30
using namespace solidity::util;
31
using namespace solidity::yul;
32
33
namespace solidity::frontend
34
{
35
36
YulArity YulArity::fromType(FunctionType const& _functionType)
37
172
{
38
172
  return YulArity{
39
172
    TupleType(_functionType.parameterTypesIncludingSelf()).sizeOnStack(),
40
172
    TupleType(_functionType.returnParameterTypes()).sizeOnStack()
41
172
  };
42
172
}
43
44
string IRNames::externalFunctionABIWrapper(Declaration const& _functionOrVarDecl)
45
3.26k
{
46
3.26k
  if (auto const* function = dynamic_cast<FunctionDefinition const*>(&_functionOrVarDecl))
47
3.26k
    solAssert(!function->isConstructor());
48
49
3.26k
  return "external_fun_" + _functionOrVarDecl.name() + "_" + to_string(_functionOrVarDecl.id());
50
3.26k
}
51
52
string IRNames::function(FunctionDefinition const& _function)
53
6.52k
{
54
6.52k
  if (_function.isConstructor())
55
0
    return constructor(*_function.annotation().contract);
56
57
6.52k
  return "fun_" + _function.name() + "_" + to_string(_function.id());
58
6.52k
}
59
60
string IRNames::function(VariableDeclaration const& _varDecl)
61
444
{
62
444
  return "getter_fun_" + _varDecl.name() + "_" + to_string(_varDecl.id());
63
444
}
64
65
string IRNames::modifierInvocation(ModifierInvocation const& _modifierInvocation)
66
152
{
67
  // This uses the ID of the modifier invocation because it has to be unique
68
  // for each invocation.
69
152
  solAssert(!_modifierInvocation.name().path().empty(), "");
70
152
  string const& modifierName = _modifierInvocation.name().path().back();
71
152
  solAssert(!modifierName.empty(), "");
72
152
  return "modifier_" + modifierName + "_" + to_string(_modifierInvocation.id());
73
152
}
74
75
string IRNames::functionWithModifierInner(FunctionDefinition const& _function)
76
114
{
77
114
  return "fun_" + _function.name() + "_" + to_string(_function.id()) + "_inner";
78
114
}
79
80
string IRNames::creationObject(ContractDefinition const& _contract)
81
3.01k
{
82
3.01k
  return _contract.name() + "_" + toString(_contract.id());
83
3.01k
}
84
85
string IRNames::deployedObject(ContractDefinition const& _contract)
86
5.61k
{
87
5.61k
  return _contract.name() + "_" + toString(_contract.id()) + "_deployed";
88
5.61k
}
89
90
string IRNames::internalDispatch(YulArity const& _arity)
91
111
{
92
111
  return "dispatch_internal"
93
111
    "_in_" + to_string(_arity.in) +
94
111
    "_out_" + to_string(_arity.out);
95
111
}
96
97
string IRNames::constructor(ContractDefinition const& _contract)
98
9.18k
{
99
9.18k
  return "constructor_" + _contract.name() + "_" + to_string(_contract.id());
100
9.18k
}
101
102
string IRNames::libraryAddressImmutable()
103
2.87k
{
104
2.87k
  return "library_deploy_address";
105
2.87k
}
106
107
string IRNames::constantValueFunction(VariableDeclaration const& _constant)
108
43
{
109
43
  solAssert(_constant.isConstant(), "");
110
43
  return "constant_" + _constant.name() + "_" + to_string(_constant.id());
111
43
}
112
113
string IRNames::localVariable(VariableDeclaration const& _declaration)
114
6.12k
{
115
6.12k
  return "var_" + _declaration.name() + '_' + std::to_string(_declaration.id());
116
6.12k
}
117
118
string IRNames::localVariable(Expression const& _expression)
119
86.2k
{
120
86.2k
  return "expr_" + to_string(_expression.id());
121
86.2k
}
122
123
string IRNames::trySuccessConditionVariable(Expression const& _expression)
124
42
{
125
42
  auto annotation = dynamic_cast<FunctionCallAnnotation const*>(&_expression.annotation());
126
42
  solAssert(annotation, "");
127
42
  solAssert(annotation->tryCall, "Parameter must be a FunctionCall with tryCall-annotation set.");
128
129
42
  return "trySuccessCondition_" + to_string(_expression.id());
130
42
}
131
132
string IRNames::tupleComponent(size_t _i)
133
1.13k
{
134
1.13k
  return "component_" + to_string(_i + 1);
135
1.13k
}
136
137
string IRNames::zeroValue(Type const& _type, string const& _variableName)
138
2.70k
{
139
2.70k
  return "zero_" + _type.identifier() + _variableName;
140
2.70k
}
141
142
string dispenseLocationComment(langutil::SourceLocation const& _location, IRGenerationContext& _context)
143
71.1k
{
144
71.1k
  solAssert(_location.sourceName, "");
145
71.1k
  _context.markSourceUsed(*_location.sourceName);
146
147
71.1k
  string debugInfo = AsmPrinter::formatSourceLocation(
148
71.1k
    _location,
149
71.1k
    _context.sourceIndices(),
150
71.1k
    _context.debugInfoSelection(),
151
71.1k
    _context.soliditySourceProvider()
152
71.1k
  );
153
154
71.1k
  return debugInfo.empty() ? "" : "/// " + debugInfo;
155
71.1k
}
156
157
string dispenseLocationComment(ASTNode const& _node, IRGenerationContext& _context)
158
19.1k
{
159
19.1k
  return dispenseLocationComment(_node.location(), _context);
160
19.1k
}
161
162
}