forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.cpp
160 lines (132 loc) · 4.78 KB
/
Object.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Yul code and data object container.
*/
#include <libyul/Object.h>
#include <libyul/AsmPrinter.h>
#include <libyul/AsmJsonConverter.h>
#include <libyul/AST.h>
#include <libyul/Exceptions.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/StringUtils.h>
#include <boost/algorithm/string.hpp>
#include <range/v3/view/transform.hpp>
using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::util;
using namespace solidity::yul;
std::string Data::toString(Dialect const*, DebugInfoSelection const&, CharStreamProvider const*) const
{
return "data \"" + name + "\" hex\"" + util::toHex(data) + "\"";
}
std::string Object::toString(
Dialect const* _dialect,
DebugInfoSelection const& _debugInfoSelection,
CharStreamProvider const* _soliditySourceProvider
) const
{
yulAssert(code, "No code");
yulAssert(debugData, "No debug data");
std::string useSrcComment;
if (debugData->sourceNames)
useSrcComment =
"/// @use-src " +
joinHumanReadable(ranges::views::transform(*debugData->sourceNames, [](auto&& _pair) {
return std::to_string(_pair.first) + ":" + util::escapeAndQuoteString(*_pair.second);
})) +
"\n";
std::string inner = "code " + AsmPrinter(
_dialect,
debugData->sourceNames,
_debugInfoSelection,
_soliditySourceProvider
)(*code);
for (auto const& obj: subObjects)
inner += "\n" + obj->toString(_dialect, _debugInfoSelection, _soliditySourceProvider);
return useSrcComment + "object \"" + name + "\" {\n" + indent(inner) + "\n}";
}
Json Data::toJson() const
{
Json ret;
ret["nodeType"] = "YulData";
ret["value"] = util::toHex(data);
return ret;
}
Json Object::toJson() const
{
yulAssert(code, "No code");
Json codeJson;
codeJson["nodeType"] = "YulCode";
codeJson["block"] = AsmJsonConverter(0 /* sourceIndex */)(*code);
Json subObjectsJson = Json::array();
for (std::shared_ptr<ObjectNode> const& subObject: subObjects)
subObjectsJson.emplace_back(subObject->toJson());
Json ret;
ret["nodeType"] = "YulObject";
ret["name"] = name;
ret["code"] = codeJson;
ret["subObjects"] = subObjectsJson;
return ret;
}
std::set<std::string> Object::qualifiedDataNames() const
{
std::set<std::string> qualifiedNames =
name.empty() || util::contains(name, '.') ?
std::set<std::string>{} :
std::set<std::string>{name};
for (std::shared_ptr<ObjectNode> const& subObjectNode: subObjects)
{
yulAssert(qualifiedNames.count(subObjectNode->name) == 0, "");
if (util::contains(subObjectNode->name, '.'))
continue;
qualifiedNames.insert(subObjectNode->name);
if (auto const* subObject = dynamic_cast<Object const*>(subObjectNode.get()))
for (auto const& subSubObj: subObject->qualifiedDataNames())
if (subObject->name != subSubObj)
{
yulAssert(qualifiedNames.count(subObject->name + "." + subSubObj) == 0, "");
qualifiedNames.insert(subObject->name + "." + subSubObj);
}
}
yulAssert(qualifiedNames.count("") == 0, "");
return qualifiedNames;
}
std::vector<size_t> Object::pathToSubObject(std::string_view _qualifiedName) const
{
yulAssert(_qualifiedName != name, "");
yulAssert(subIndexByName.count(name) == 0, "");
if (boost::algorithm::starts_with(_qualifiedName, name + "."))
_qualifiedName = _qualifiedName.substr(name.length() + 1);
yulAssert(!_qualifiedName.empty(), "");
std::vector<std::string> subObjectPathComponents;
boost::algorithm::split(subObjectPathComponents, _qualifiedName, boost::is_any_of("."));
std::vector<size_t> path;
Object const* object = this;
for (std::string const& currentSubObjectName: subObjectPathComponents)
{
yulAssert(!currentSubObjectName.empty(), "");
auto subIndexIt = object->subIndexByName.find(currentSubObjectName);
yulAssert(
subIndexIt != object->subIndexByName.end(),
"Assembly object <" + std::string(_qualifiedName) + "> not found or does not contain code."
);
object = dynamic_cast<Object const*>(object->subObjects[subIndexIt->second].get());
yulAssert(object, "Assembly object <" + std::string(_qualifiedName) + "> not found or does not contain code.");
yulAssert(object->subId != std::numeric_limits<size_t>::max(), "");
path.push_back({object->subId});
}
return path;
}