/* This file is part of solidity. solidity is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. solidity is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with solidity. If not, see . */ // SPDX-License-Identifier: GPL-3.0 /** * @author Christian * @date 2016 * Parsed inline assembly to be used by the AST */ #pragma once #include #include #include #include #include namespace solidity::yul { using Type = YulString; struct DebugData { explicit DebugData( langutil::SourceLocation _nativeLocation, langutil::SourceLocation _originLocation = {}, std::optional _astID = {} ): nativeLocation(std::move(_nativeLocation)), originLocation(std::move(_originLocation)), astID(std::move(_astID)) {} static std::shared_ptr create( langutil::SourceLocation _nativeLocation = {}, langutil::SourceLocation _originLocation = {}, std::optional _astID = {} ) { return std::make_shared( std::move(_nativeLocation), std::move(_originLocation), std::move(_astID) ); } /// Location in the Yul code. langutil::SourceLocation nativeLocation; /// Location in the original source that the Yul code was produced from. /// Optional. Only present if the Yul source contains location annotations. langutil::SourceLocation originLocation; /// ID in the (Solidity) source AST. std::optional astID; }; struct TypedName { std::shared_ptr debugData; YulString name; Type type; }; using TypedNameList = std::vector; /// Literal number or string (up to 32 bytes) enum class LiteralKind { Number, Boolean, String }; struct Literal { std::shared_ptr debugData; LiteralKind kind; YulString value; Type type; }; /// External / internal identifier or label reference struct Identifier { std::shared_ptr debugData; YulString name; }; /// Assignment ("x := mload(20:u256)", expects push-1-expression on the right hand /// side and requires x to occupy exactly one stack slot. /// /// Multiple assignment ("x, y := f()"), where the left hand side variables each occupy /// a single stack slot and expects a single expression on the right hand returning /// the same amount of items as the number of variables. struct Assignment { std::shared_ptr debugData; std::vector variableNames; std::unique_ptr value; }; struct FunctionCall { std::shared_ptr debugData; Identifier functionName; std::vector arguments; }; /// Statement that contains only a single expression struct ExpressionStatement { std::shared_ptr debugData; Expression expression; }; /// Block-scope variable declaration ("let x:u256 := mload(20:u256)"), non-hoisted struct VariableDeclaration { std::shared_ptr debugData; TypedNameList variables; std::unique_ptr value; }; /// Block that creates a scope (frees declared stack variables) struct Block { std::shared_ptr debugData; std::vector statements; }; /// Function definition ("function f(a, b) -> (d, e) { ... }") struct FunctionDefinition { std::shared_ptr debugData; YulString name; TypedNameList parameters; TypedNameList returnVariables; Block body; }; /// Conditional execution without "else" part. struct If { std::shared_ptr debugData; std::unique_ptr condition; Block body; }; /// Switch case or default case struct Case { std::shared_ptr debugData; std::unique_ptr value; Block body; }; /// Switch statement struct Switch { std::shared_ptr debugData; std::unique_ptr expression; std::vector cases; }; struct ForLoop { std::shared_ptr debugData; Block pre; std::unique_ptr condition; Block post; Block body; }; /// Break statement (valid within for loop) struct Break { std::shared_ptr debugData; }; /// Continue statement (valid within for loop) struct Continue { std::shared_ptr debugData; }; /// Leave statement (valid within function) struct Leave { std::shared_ptr debugData; }; /// Extracts the IR source location from a Yul node. template inline langutil::SourceLocation nativeLocationOf(T const& _node) { return _node.debugData ? _node.debugData->nativeLocation : langutil::SourceLocation{}; } /// Extracts the IR source location from a Yul node. template inline langutil::SourceLocation nativeLocationOf(std::variant const& _node) { return std::visit([](auto const& _arg) { return nativeLocationOf(_arg); }, _node); } /// Extracts the original source location from a Yul node. template inline langutil::SourceLocation originLocationOf(T const& _node) { return _node.debugData ? _node.debugData->originLocation : langutil::SourceLocation{}; } /// Extracts the original source location from a Yul node. template inline langutil::SourceLocation originLocationOf(std::variant const& _node) { return std::visit([](auto const& _arg) { return originLocationOf(_arg); }, _node); } /// Extracts the debug data from a Yul node. template inline std::shared_ptr debugDataOf(T const& _node) { return _node.debugData; } /// Extracts the debug data from a Yul node. template inline std::shared_ptr debugDataOf(std::variant const& _node) { return std::visit([](auto const& _arg) { return debugDataOf(_arg); }, _node); } }