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

Method storageSetToZeroFunction

libsolidity/codegen/YulUtilFunctions.cpp:4375–4432  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4373}
4374
4375std::string YulUtilFunctions::storageSetToZeroFunction(Type const& _type, VariableDeclaration::Location _location)
4376{
4377 solAssert(
4378 _location == VariableDeclaration::Location::Transient ||
4379 _location == VariableDeclaration::Location::Unspecified,
4380 "Invalid location for the storage_set_to_zero function"
4381 );
4382
4383 if (dynamic_cast<ReferenceType const*>(&_type))
4384 solAssert(
4385 _location == VariableDeclaration::Location::Unspecified &&
4386 _type.dataStoredIn(DataLocation::Storage)
4387 );
4388
4389 std::string const functionName =
4390 (_location == VariableDeclaration::Location::Transient ? "transient_"s : "") +
4391 "storage_set_to_zero_" +
4392 _type.identifier();
4393
4394 return m_functionCollector.createFunction(functionName, [&]() {
4395 if (_type.isValueType())
4396 return Whiskers(R"(
4397 function <functionName>(slot, offset) {
4398 let <values> := <zeroValue>()
4399 <store>(slot, offset, <values>)
4400 }
4401 )")
4402 ("functionName", functionName)
4403 ("store", updateStorageValueFunction(_type, _type, _location))
4404 ("values", suffixedVariableNameList("zero_", 0, _type.sizeOnStack()))
4405 ("zeroValue", zeroValueFunction(_type))
4406 .render();
4407 else if (_type.category() == Type::Category::Array)
4408 return Whiskers(R"(
4409 function <functionName>(slot, offset) {
4410 if iszero(eq(offset, 0)) { <panic>() }
4411 <clearArray>(slot)
4412 }
4413 )")
4414 ("functionName", functionName)
4415 ("clearArray", clearStorageArrayFunction(dynamic_cast<ArrayType const&>(_type)))
4416 ("panic", panicFunction(PanicCode::Generic))
4417 .render();
4418 else if (_type.category() == Type::Category::Struct)
4419 return Whiskers(R"(
4420 function <functionName>(slot, offset) {
4421 if iszero(eq(offset, 0)) { <panic>() }
4422 <clearStruct>(slot)
4423 }
4424 )")
4425 ("functionName", functionName)
4426 ("clearStruct", clearStorageStructFunction(dynamic_cast<StructType const&>(_type)))
4427 ("panic", panicFunction(PanicCode::Generic))
4428 .render();
4429 else
4430 solUnimplemented("setToZero for type " + _type.identifier() + " not yet implemented!");
4431 });
4432}

Callers 1

visitMethod · 0.80

Calls 8

WhiskersClass · 0.85
identifierMethod · 0.80
renderMethod · 0.80
dataStoredInMethod · 0.45
createFunctionMethod · 0.45
isValueTypeMethod · 0.45
sizeOnStackMethod · 0.45
categoryMethod · 0.45

Tested by

no test coverage detected