| 368 | } |
| 369 | |
| 370 | std::string YulUtilFunctions::leftAlignFunction(Type const& _type) |
| 371 | { |
| 372 | std::string functionName = std::string("leftAlign_") + _type.identifier(); |
| 373 | return m_functionCollector.createFunction(functionName, [&]() { |
| 374 | Whiskers templ(R"( |
| 375 | function <functionName>(value) -> aligned { |
| 376 | <body> |
| 377 | } |
| 378 | )"); |
| 379 | templ("functionName", functionName); |
| 380 | switch (_type.category()) |
| 381 | { |
| 382 | case Type::Category::Address: |
| 383 | templ("body", "aligned := " + leftAlignFunction(IntegerType(160)) + "(value)"); |
| 384 | break; |
| 385 | case Type::Category::Integer: |
| 386 | { |
| 387 | IntegerType const& type = dynamic_cast<IntegerType const&>(_type); |
| 388 | if (type.numBits() == 256) |
| 389 | templ("body", "aligned := value"); |
| 390 | else |
| 391 | templ("body", "aligned := " + shiftLeftFunction(256 - type.numBits()) + "(value)"); |
| 392 | break; |
| 393 | } |
| 394 | case Type::Category::RationalNumber: |
| 395 | solAssert(false, "Left align requested for rational number."); |
| 396 | break; |
| 397 | case Type::Category::Bool: |
| 398 | templ("body", "aligned := " + leftAlignFunction(IntegerType(8)) + "(value)"); |
| 399 | break; |
| 400 | case Type::Category::FixedPoint: |
| 401 | solUnimplemented("Fixed point types not implemented."); |
| 402 | break; |
| 403 | case Type::Category::Array: |
| 404 | case Type::Category::Struct: |
| 405 | solAssert(false, "Left align requested for non-value type."); |
| 406 | break; |
| 407 | case Type::Category::FixedBytes: |
| 408 | templ("body", "aligned := value"); |
| 409 | break; |
| 410 | case Type::Category::Contract: |
| 411 | templ("body", "aligned := " + leftAlignFunction(*TypeProvider::address()) + "(value)"); |
| 412 | break; |
| 413 | case Type::Category::Enum: |
| 414 | { |
| 415 | solAssert(dynamic_cast<EnumType const&>(_type).storageBytes() == 1, ""); |
| 416 | templ("body", "aligned := " + leftAlignFunction(IntegerType(8)) + "(value)"); |
| 417 | break; |
| 418 | } |
| 419 | case Type::Category::InaccessibleDynamic: |
| 420 | solAssert(false, "Left align requested for inaccessible dynamic type."); |
| 421 | break; |
| 422 | default: |
| 423 | solAssert(false, "Left align of type " + _type.identifier() + " requested."); |
| 424 | } |
| 425 | |
| 426 | return templ.render(); |
| 427 | }); |
no test coverage detected