|
| 1 | +/* |
| 2 | + * The Certora Prover |
| 3 | + * Copyright (C) 2025 Certora Ltd. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, version 3 of the License. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package prefixgen |
| 19 | + |
| 20 | +import bridge.types.SolidityTypeDescription |
| 21 | + |
| 22 | +/** |
| 23 | + * Utilities for the code generation process. |
| 24 | + */ |
| 25 | +object CodeGen { |
| 26 | + |
| 27 | + /** |
| 28 | + * Bound some expression given by [syntax] to be between `[0,maxValueInclusive]`. |
| 29 | + * This is done by simply applying a modulus. |
| 30 | + */ |
| 31 | + fun bound(syntax: String, maxValueInclusive: Int) = "$syntax % ${maxValueInclusive + 1}" |
| 32 | + |
| 33 | + /** |
| 34 | + * Pretty print the given soliidty type for use in a *local* variable declaration. |
| 35 | + */ |
| 36 | + fun SolidityTypeDescription.pp() = when(this) { |
| 37 | + is SolidityTypeDescription.Contract -> { |
| 38 | + this.contractName |
| 39 | + } |
| 40 | + is SolidityTypeDescription.Array, |
| 41 | + is SolidityTypeDescription.Function, |
| 42 | + is SolidityTypeDescription.StaticArray, |
| 43 | + is SolidityTypeDescription.UserDefined.Struct, |
| 44 | + is SolidityTypeDescription.Mapping -> throw UnsupportedOperationException("nope") |
| 45 | + is SolidityTypeDescription.PackedBytes -> "bytes memory" |
| 46 | + is SolidityTypeDescription.Primitive -> this.primitiveName.name |
| 47 | + is SolidityTypeDescription.StringType -> "string memory" |
| 48 | + is SolidityTypeDescription.UserDefined.Enum -> this.containingContract?.let { "$it." }.orEmpty() + this.enumName |
| 49 | + is SolidityTypeDescription.UserDefined.ValueType -> throw UnsupportedOperationException("not yet") |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Pretty print the given solidity type for use in *storage* (or struct definition) declaration. |
| 54 | + * For most types, this is just [pp], with the exception of `string` and `bytes`, which do not have the `memory` |
| 55 | + * location. |
| 56 | + */ |
| 57 | + fun SolidityTypeDescription.storagePP() = when(this) { |
| 58 | + is SolidityTypeDescription.PackedBytes -> "bytes" |
| 59 | + is SolidityTypeDescription.StringType -> "string" |
| 60 | + else -> this.pp() |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Indicates whether the type described by `this` is something we know how to build via fuzzing. |
| 66 | + * These are (smallish) static arrays of buildable types, dynamic arrays of primitive types, |
| 67 | + * primitive types, bytes and strings, enums, and structs of buildable types. |
| 68 | + */ |
| 69 | + fun SolidityTypeDescription.buildable() : Boolean = when(this) { |
| 70 | + is SolidityTypeDescription.Array -> this.dynamicArrayBaseType is SolidityTypeDescription.Primitive |
| 71 | + is SolidityTypeDescription.Contract -> true |
| 72 | + is SolidityTypeDescription.Function, |
| 73 | + is SolidityTypeDescription.Mapping -> false |
| 74 | + is SolidityTypeDescription.PackedBytes, |
| 75 | + is SolidityTypeDescription.StringType, |
| 76 | + is SolidityTypeDescription.Primitive -> true |
| 77 | + is SolidityTypeDescription.StaticArray -> this.staticArrayBaseType.buildable() && this.staticArraySize <= 4.toBigInteger() |
| 78 | + is SolidityTypeDescription.UserDefined.ValueType -> false |
| 79 | + is SolidityTypeDescription.UserDefined.Enum -> true |
| 80 | + is SolidityTypeDescription.UserDefined.Struct -> this.structMembers.all { |
| 81 | + it.type.buildable() |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments