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

Method storageArrayIndexAccessFunction

libsolidity/codegen/YulUtilFunctions.cpp:2422–2465  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2420}
2421
2422std::string YulUtilFunctions::storageArrayIndexAccessFunction(ArrayType const& _type)
2423{
2424 std::string functionName = "storage_array_index_access_" + _type.identifier();
2425 return m_functionCollector.createFunction(functionName, [&]() {
2426 return Whiskers(R"(
2427 function <functionName>(array, index) -> slot, offset {
2428 let arrayLength := <arrayLen>(array)
2429 if iszero(lt(index, arrayLength)) { <panic>() }
2430
2431 <?multipleItemsPerSlot>
2432 <?isBytesArray>
2433 switch lt(arrayLength, 0x20)
2434 case 0 {
2435 slot, offset := <indexAccessNoChecks>(array, index)
2436 }
2437 default {
2438 offset := sub(31, mod(index, 0x20))
2439 slot := array
2440 }
2441 <!isBytesArray>
2442 let dataArea := <dataAreaFunc>(array)
2443 slot := add(dataArea, div(index, <itemsPerSlot>))
2444 offset := mul(mod(index, <itemsPerSlot>), <storageBytes>)
2445 </isBytesArray>
2446 <!multipleItemsPerSlot>
2447 let dataArea := <dataAreaFunc>(array)
2448 slot := add(dataArea, mul(index, <storageSize>))
2449 offset := 0
2450 </multipleItemsPerSlot>
2451 }
2452 )")
2453 ("functionName", functionName)
2454 ("panic", panicFunction(PanicCode::ArrayOutOfBounds))
2455 ("arrayLen", arrayLengthFunction(_type))
2456 ("dataAreaFunc", arrayDataAreaFunction(_type))
2457 ("indexAccessNoChecks", longByteArrayStorageIndexAccessNoCheckFunction())
2458 ("multipleItemsPerSlot", _type.baseType()->storageBytes() <= 16)
2459 ("isBytesArray", _type.isByteArrayOrString())
2460 ("storageSize", _type.baseType()->storageSize().str())
2461 ("storageBytes", toString(_type.baseType()->storageBytes()))
2462 ("itemsPerSlot", std::to_string(32 / _type.baseType()->storageBytes()))
2463 .render();
2464 });
2465}
2466
2467std::string YulUtilFunctions::memoryArrayIndexAccessFunction(ArrayType const& _type)
2468{

Callers 2

endVisitMethod · 0.80
generateGetterMethod · 0.80

Calls 11

WhiskersClass · 0.85
identifierMethod · 0.80
renderMethod · 0.80
isByteArrayOrStringMethod · 0.80
strMethod · 0.80
toStringFunction · 0.50
to_stringFunction · 0.50
createFunctionMethod · 0.45
storageBytesMethod · 0.45
baseTypeMethod · 0.45
storageSizeMethod · 0.45

Tested by

no test coverage detected