MCPcopy Create free account
hub / github.com/argotorg/solidity / copyLiteralToStorageFunction

Method copyLiteralToStorageFunction

libsolidity/codegen/YulUtilFunctions.cpp:190–236  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

188}
189
190std::string YulUtilFunctions::copyLiteralToStorageFunction(std::string const& _literal)
191{
192 std::string functionName = "copy_literal_to_storage_" + util::toHex(util::keccak256(_literal).asBytes());
193
194 return m_functionCollector.createFunction(functionName, [&](std::vector<std::string>& _args, std::vector<std::string>&) {
195 _args = {"slot"};
196
197 if (_literal.size() >= 32)
198 {
199 size_t words = (_literal.length() + 31) / 32;
200 std::vector<std::map<std::string, std::string>> wordParams(words);
201 for (size_t i = 0; i < words; ++i)
202 {
203 wordParams[i]["offset"] = std::to_string(i);
204 wordParams[i]["wordValue"] = formatAsStringOrNumber(_literal.substr(32 * i, 32));
205 }
206 return Whiskers(R"(
207 let oldLen := <byteArrayLength>(sload(slot))
208 <cleanUpArrayEnd>(slot, oldLen, <length>)
209 sstore(slot, <encodedLen>)
210 let dstPtr := <dataArea>(slot)
211 <#word>
212 sstore(add(dstPtr, <offset>), <wordValue>)
213 </word>
214 )")
215 ("byteArrayLength", extractByteArrayLengthFunction())
216 ("cleanUpArrayEnd", cleanUpDynamicByteArrayEndSlotsFunction(*TypeProvider::bytesStorage()))
217 ("dataArea", arrayDataAreaFunction(*TypeProvider::bytesStorage()))
218 ("word", wordParams)
219 ("length", std::to_string(_literal.size()))
220 ("encodedLen", std::to_string(2 * _literal.size() + 1))
221 .render();
222 }
223 else
224 return Whiskers(R"(
225 let oldLen := <byteArrayLength>(sload(slot))
226 <cleanUpArrayEnd>(slot, oldLen, <length>)
227 sstore(slot, add(<wordValue>, <encodedLen>))
228 )")
229 ("byteArrayLength", extractByteArrayLengthFunction())
230 ("cleanUpArrayEnd", cleanUpDynamicByteArrayEndSlotsFunction(*TypeProvider::bytesStorage()))
231 ("wordValue", formatAsStringOrNumber(_literal))
232 ("length", std::to_string(_literal.size()))
233 ("encodedLen", std::to_string(2 * _literal.size()))
234 .render();
235 });
236}
237
238std::string YulUtilFunctions::revertWithError(
239 std::string const& _signature,

Callers

nothing calls this directly

Calls 9

toHexFunction · 0.85
WhiskersClass · 0.85
asBytesMethod · 0.80
renderMethod · 0.80
keccak256Function · 0.50
to_stringFunction · 0.50
createFunctionMethod · 0.45
sizeMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected