/src/solidity/libsolidity/codegen/ir/IRGenerator.h
Line | Count | Source |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | /** |
19 | | * @author Alex Beregszaszi |
20 | | * @date 2017 |
21 | | * Component that translates Solidity code into Yul. |
22 | | */ |
23 | | |
24 | | #pragma once |
25 | | |
26 | | #include <libsolutil/JSON.h> |
27 | | |
28 | | #include <libsolidity/ast/ASTForward.h> |
29 | | #include <libsolidity/ast/CallGraph.h> |
30 | | #include <libsolidity/codegen/ir/IRGenerationContext.h> |
31 | | #include <libsolidity/codegen/YulUtilFunctions.h> |
32 | | #include <libsolidity/interface/OptimiserSettings.h> |
33 | | |
34 | | #include <liblangutil/CharStreamProvider.h> |
35 | | #include <liblangutil/EVMVersion.h> |
36 | | |
37 | | #include <string> |
38 | | |
39 | | namespace solidity::frontend |
40 | | { |
41 | | |
42 | | class SourceUnit; |
43 | | |
44 | | class IRGenerator |
45 | | { |
46 | | public: |
47 | | using ExecutionContext = IRGenerationContext::ExecutionContext; |
48 | | |
49 | | IRGenerator( |
50 | | langutil::EVMVersion _evmVersion, |
51 | | RevertStrings _revertStrings, |
52 | | std::map<std::string, unsigned> _sourceIndices, |
53 | | langutil::DebugInfoSelection const& _debugInfoSelection, |
54 | | langutil::CharStreamProvider const* _soliditySourceProvider, |
55 | | OptimiserSettings& _optimiserSettings |
56 | | ): |
57 | 4.26k | m_evmVersion(_evmVersion), |
58 | 4.26k | m_context( |
59 | 4.26k | _evmVersion, |
60 | 4.26k | ExecutionContext::Creation, |
61 | 4.26k | _revertStrings, |
62 | 4.26k | std::move(_sourceIndices), |
63 | 4.26k | _debugInfoSelection, |
64 | 4.26k | _soliditySourceProvider |
65 | 4.26k | ), |
66 | 4.26k | m_utils(_evmVersion, m_context.revertStrings(), m_context.functionCollector()), |
67 | 4.26k | m_optimiserSettings(_optimiserSettings) |
68 | 4.26k | {} |
69 | | |
70 | | /// Generates and returns (unoptimized) IR code. |
71 | | std::string run( |
72 | | ContractDefinition const& _contract, |
73 | | bytes const& _cborMetadata, |
74 | | std::map<ContractDefinition const*, std::string_view const> const& _otherYulSources |
75 | | ); |
76 | | |
77 | | private: |
78 | | std::string generate( |
79 | | ContractDefinition const& _contract, |
80 | | bytes const& _cborMetadata, |
81 | | std::map<ContractDefinition const*, std::string_view const> const& _otherYulSources |
82 | | ); |
83 | | std::string generate(Block const& _block); |
84 | | |
85 | | /// Generates code for all the functions from the function generation queue. |
86 | | /// The resulting code is stored in the function collector in IRGenerationContext. |
87 | | /// @returns A set of ast nodes of the generated functions. |
88 | | std::set<FunctionDefinition const*> generateQueuedFunctions(); |
89 | | /// Generates all the internal dispatch functions necessary to handle any function that could |
90 | | /// possibly be called via a pointer. |
91 | | /// @return The content of the dispatch for reuse in runtime code. Reuse is necessary because |
92 | | /// pointers to functions can be passed from the creation code in storage variables. |
93 | | InternalDispatchMap generateInternalDispatchFunctions(ContractDefinition const& _contract); |
94 | | /// Generates code for and returns the name of the function. |
95 | | std::string generateFunction(FunctionDefinition const& _function); |
96 | | std::string generateModifier( |
97 | | ModifierInvocation const& _modifierInvocation, |
98 | | FunctionDefinition const& _function, |
99 | | std::string const& _nextFunction |
100 | | ); |
101 | | std::string generateFunctionWithModifierInner(FunctionDefinition const& _function); |
102 | | /// Generates a getter for the given declaration and returns its name |
103 | | std::string generateGetter(VariableDeclaration const& _varDecl); |
104 | | |
105 | | /// Generates the external part (ABI decoding and encoding) of a function or getter. |
106 | | std::string generateExternalFunction(ContractDefinition const& _contract, FunctionType const& _functionType); |
107 | | |
108 | | /// Generates code that assigns the initial value of the respective type. |
109 | | std::string generateInitialAssignment(VariableDeclaration const& _varDecl); |
110 | | |
111 | | /// Generates constructors for all contracts in the inheritance hierarchy of |
112 | | /// @a _contract |
113 | | /// If there are user defined constructors, their body will be included in the implicit constructor's body. |
114 | | void generateConstructors(ContractDefinition const& _contract); |
115 | | |
116 | | /// Evaluates constructor's arguments for all base contracts (listed in inheritance specifiers) of |
117 | | /// @a _contract |
118 | | /// @returns Pair of expressions needed to evaluate params and list of parameters in a map contract -> params |
119 | | std::pair<std::string, std::map<ContractDefinition const*, std::vector<std::string>>> |
120 | | evaluateConstructorArguments(ContractDefinition const& _contract); |
121 | | |
122 | | /// Initializes state variables of |
123 | | /// @a _contract |
124 | | /// @returns Source code to initialize state variables |
125 | | std::string initStateVariables(ContractDefinition const& _contract); |
126 | | |
127 | | std::string deployCode(ContractDefinition const& _contract); |
128 | | std::string callValueCheck(); |
129 | | |
130 | | std::string dispatchRoutine(ContractDefinition const& _contract); |
131 | | |
132 | | /// @a _useMemoryGuard If true, use a memory guard, allowing the optimiser |
133 | | /// to perform memory optimizations. |
134 | | std::string memoryInit(bool _useMemoryGuard); |
135 | | |
136 | | void resetContext(ContractDefinition const& _contract, ExecutionContext _context); |
137 | | |
138 | | std::string dispenseLocationComment(ASTNode const& _node); |
139 | | |
140 | | langutil::EVMVersion const m_evmVersion; |
141 | | |
142 | | IRGenerationContext m_context; |
143 | | YulUtilFunctions m_utils; |
144 | | OptimiserSettings m_optimiserSettings; |
145 | | }; |
146 | | |
147 | | } |