|
| 1 | +#include <test/libsolidity/util/compiler/InternalCompiler.h> |
| 2 | + |
| 3 | +using namespace solidity; |
| 4 | +using namespace solidity::evmasm; |
| 5 | +using namespace solidity::frontend::test; |
| 6 | + |
| 7 | +CompilerOutput InternalCompiler::compile(CompilerInput const& _input) |
| 8 | +{ |
| 9 | + configure(_input); |
| 10 | + |
| 11 | + auto [contracts, errors] = internalCompile(_input); |
| 12 | + bool success = m_stack.compilationSuccessful(); |
| 13 | + |
| 14 | + return CompilerOutput{ |
| 15 | + contracts, |
| 16 | + success, |
| 17 | + errors, |
| 18 | + formatErrorInformation() |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +void InternalCompiler::configure(CompilerInput const& _input) |
| 23 | +{ |
| 24 | + m_stack.reset(); |
| 25 | + |
| 26 | + m_stack.setSources(_input.sourceCode); |
| 27 | + m_stack.setLibraries(_input.libraryAddresses); |
| 28 | + |
| 29 | + if (_input.evmVersion.has_value()) |
| 30 | + m_stack.setEVMVersion(_input.evmVersion.value()); |
| 31 | + if (_input.optimise.has_value()) |
| 32 | + m_stack.setOptimiserSettings(_input.optimise.value()); |
| 33 | + if (_input.optimiserSettings.has_value()) |
| 34 | + m_stack.setOptimiserSettings(_input.optimiserSettings.value()); |
| 35 | + if (_input.revertStrings.has_value()) |
| 36 | + m_stack.setRevertStringBehaviour(_input.revertStrings.value()); |
| 37 | + if (_input.metadataFormat.has_value()) |
| 38 | + m_stack.setMetadataFormat(_input.metadataFormat.value()); |
| 39 | + if (_input.metadataHash.has_value()) |
| 40 | + m_stack.setMetadataHash(_input.metadataHash.value()); |
| 41 | + if (_input.viaIR.has_value()) |
| 42 | + m_stack.setViaIR(_input.viaIR.value()); |
| 43 | + m_stack.setEOFVersion(_input.eofVersion); |
| 44 | +} |
| 45 | + |
| 46 | +std::pair<MappedContracts, Errors> InternalCompiler::internalCompile( |
| 47 | + CompilerInput const& _input |
| 48 | +) |
| 49 | +{ |
| 50 | + m_stack.compile(); |
| 51 | + |
| 52 | + MappedContracts mappedContracts{}; |
| 53 | + for (auto sourceName: m_stack.sourceNames()) |
| 54 | + { |
| 55 | + std::vector<CompiledContract> compiled; |
| 56 | + for (auto const& contract: m_stack.contractDefinitions(sourceName)) |
| 57 | + { |
| 58 | + std::string contractName = contract->fullyQualifiedName(); |
| 59 | + LinkerObject object = m_stack.object(contractName); |
| 60 | + LinkerObject runtimeObject = m_stack.runtimeObject(contractName); |
| 61 | + bool hasUnlinkedReferences = !object.linkReferences.empty(); |
| 62 | + bytes cborMetadata = m_stack.cborMetadata(contractName); |
| 63 | + |
| 64 | + std::optional<AssemblyItems> assemblyItems; |
| 65 | + std::optional<AssemblyItems> runtimeAssemblyItems; |
| 66 | + |
| 67 | + if (!_input.eofVersion.has_value()) |
| 68 | + { |
| 69 | + if (auto items = m_stack.assemblyItems(contractName); items != nullptr) |
| 70 | + assemblyItems = std::make_optional(*items); |
| 71 | + |
| 72 | + if (auto items = m_stack.runtimeAssemblyItems(contractName); items != nullptr) |
| 73 | + runtimeAssemblyItems = std::make_optional(*items); |
| 74 | + } |
| 75 | + |
| 76 | + if (!m_stack.isExperimentalSolidity()) |
| 77 | + compiled.emplace_back(CompiledContract{ |
| 78 | + contractName, |
| 79 | + object.bytecode, |
| 80 | + runtimeObject.bytecode, |
| 81 | + hasUnlinkedReferences, |
| 82 | + cborMetadata, |
| 83 | + assemblyItems, |
| 84 | + runtimeAssemblyItems, |
| 85 | + m_stack.metadata(contractName), |
| 86 | + m_stack.contractABI(contractName), |
| 87 | + m_stack.interfaceSymbols(contractName), |
| 88 | + generateEventSignatures(contractName) |
| 89 | + }); |
| 90 | + else |
| 91 | + compiled.emplace_back(CompiledContract{ |
| 92 | + contractName, |
| 93 | + object.bytecode, |
| 94 | + runtimeObject.bytecode, |
| 95 | + hasUnlinkedReferences, |
| 96 | + cborMetadata, |
| 97 | + assemblyItems, |
| 98 | + runtimeAssemblyItems, |
| 99 | + std::nullopt, |
| 100 | + std::nullopt, |
| 101 | + std::nullopt, |
| 102 | + std::vector<EventSignature>{} |
| 103 | + }); |
| 104 | + } |
| 105 | + mappedContracts.insert(std::make_pair(sourceName, compiled)); |
| 106 | + } |
| 107 | + |
| 108 | + return std::make_pair(mappedContracts, m_stack.errors()); |
| 109 | +} |
| 110 | + |
| 111 | +std::vector<EventSignature> InternalCompiler::generateEventSignatures( |
| 112 | + std::string const& _contractName |
| 113 | +) const |
| 114 | +{ |
| 115 | + auto const& contract = m_stack.contractDefinition(_contractName); |
| 116 | + auto const& events = contract.events(); |
| 117 | + auto const& interfaceEvents = contract.usedInterfaceEvents(); |
| 118 | + |
| 119 | + auto toSignature = [](EventDefinition const* _event) |
| 120 | + { |
| 121 | + soltestAssert(_event); |
| 122 | + return EventSignature::fromEvent(*_event); |
| 123 | + }; |
| 124 | + |
| 125 | + auto signatures = |
| 126 | + ranges::views::concat(events, interfaceEvents) | |
| 127 | + ranges::views::transform(toSignature) | |
| 128 | + ranges::to<std::vector>(); |
| 129 | + |
| 130 | + return signatures; |
| 131 | +} |
| 132 | + |
| 133 | +std::string InternalCompiler::formatErrorInformation() const |
| 134 | +{ |
| 135 | + std::string errorInformation; |
| 136 | + for (auto const& error: m_stack.errors()) |
| 137 | + { |
| 138 | + auto formatted = SourceReferenceFormatter::formatErrorInformation( |
| 139 | + *error, |
| 140 | + m_stack, |
| 141 | + true, |
| 142 | + false |
| 143 | + ); |
| 144 | + errorInformation.append(formatted); |
| 145 | + } |
| 146 | + |
| 147 | + return errorInformation; |
| 148 | +} |
0 commit comments