/src/solidity/libsolidity/ast/ASTJsonImporter.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 julius <djudju@protonmail.com> |
20 | | * @date 2019 |
21 | | * Converts the AST from JSON format to ASTNode |
22 | | */ |
23 | | |
24 | | #pragma once |
25 | | |
26 | | #include <vector> |
27 | | #include <libsolutil/JSON.h> |
28 | | #include <libsolidity/ast/AST.h> |
29 | | #include <libsolidity/ast/ASTAnnotations.h> |
30 | | #include <liblangutil/EVMVersion.h> |
31 | | #include <liblangutil/Exceptions.h> |
32 | | #include <liblangutil/SourceLocation.h> |
33 | | |
34 | | namespace solidity::frontend |
35 | | { |
36 | | |
37 | | /** |
38 | | * Component that imports an AST from json format to the internal format |
39 | | */ |
40 | | class ASTJsonImporter |
41 | | { |
42 | | public: |
43 | | ASTJsonImporter(langutil::EVMVersion _evmVersion) |
44 | 0 | :m_evmVersion(_evmVersion) |
45 | 0 | {} |
46 | | |
47 | | /// Converts the AST from JSON-format to ASTPointer |
48 | | /// @a _sourceList used to provide source names for the ASTs |
49 | | /// @returns map of sourcenames to their respective ASTs |
50 | | std::map<std::string, ASTPointer<SourceUnit>> jsonToSourceUnit(std::map<std::string, Json> const& _sourceList); |
51 | | |
52 | | private: |
53 | | |
54 | | // =========== general creation functions ============== |
55 | | |
56 | | /// Sets the source location and nodeID |
57 | | /// @returns the ASTNode Object class of the respective JSON node, |
58 | | template <typename T, typename... Args> |
59 | | ASTPointer<T> createASTNode(Json const& _node, Args&&... _args); |
60 | | /// @returns the sourceLocation-object created from the string in the JSON node |
61 | | langutil::SourceLocation const createSourceLocation(Json const& _node); |
62 | | std::optional<std::vector<langutil::SourceLocation>> createSourceLocations(Json const& _node) const; |
63 | | /// Creates an ASTNode for a given JSON-ast of unknown type |
64 | | /// @returns Pointer to a new created ASTNode |
65 | | ASTPointer<ASTNode> convertJsonToASTNode(Json const& _ast); |
66 | | /// @returns a pointer to the more specific subclass of ASTNode |
67 | | /// as indicated by the nodeType field of the json |
68 | | template<class T> |
69 | | ASTPointer<T> convertJsonToASTNode(Json const& _node); |
70 | | |
71 | | langutil::SourceLocation createNameSourceLocation(Json const& _node); |
72 | | /// @returns source location of a mapping key name |
73 | | langutil::SourceLocation createKeyNameSourceLocation(Json const& _node); |
74 | | /// @returns source location of a mapping value name |
75 | | langutil::SourceLocation createValueNameSourceLocation(Json const& _node); |
76 | | |
77 | | /// \defgroup nodeCreators JSON to AST-Nodes |
78 | | ///@{ |
79 | | ASTPointer<SourceUnit> createSourceUnit(Json const& _node, std::string const& _srcName); |
80 | | ASTPointer<PragmaDirective> createPragmaDirective(Json const& _node); |
81 | | ASTPointer<ImportDirective> createImportDirective(Json const& _node); |
82 | | ASTPointer<ContractDefinition> createContractDefinition(Json const& _node); |
83 | | ASTPointer<IdentifierPath> createIdentifierPath(Json const& _node); |
84 | | ASTPointer<InheritanceSpecifier> createInheritanceSpecifier(Json const& _node); |
85 | | ASTPointer<UsingForDirective> createUsingForDirective(Json const& _node); |
86 | | ASTPointer<ASTNode> createStructDefinition(Json const& _node); |
87 | | ASTPointer<EnumDefinition> createEnumDefinition(Json const& _node); |
88 | | ASTPointer<EnumValue> createEnumValue(Json const& _node); |
89 | | ASTPointer<UserDefinedValueTypeDefinition> createUserDefinedValueTypeDefinition(Json const& _node); |
90 | | ASTPointer<ParameterList> createParameterList(Json const& _node); |
91 | | ASTPointer<OverrideSpecifier> createOverrideSpecifier(Json const& _node); |
92 | | ASTPointer<FunctionDefinition> createFunctionDefinition(Json const& _node); |
93 | | ASTPointer<VariableDeclaration> createVariableDeclaration(Json const& _node); |
94 | | ASTPointer<ModifierDefinition> createModifierDefinition(Json const& _node); |
95 | | ASTPointer<ModifierInvocation> createModifierInvocation(Json const& _node); |
96 | | ASTPointer<EventDefinition> createEventDefinition(Json const& _node); |
97 | | ASTPointer<ErrorDefinition> createErrorDefinition(Json const& _node); |
98 | | ASTPointer<ElementaryTypeName> createElementaryTypeName(Json const& _node); |
99 | | ASTPointer<UserDefinedTypeName> createUserDefinedTypeName(Json const& _node); |
100 | | ASTPointer<FunctionTypeName> createFunctionTypeName(Json const& _node); |
101 | | ASTPointer<Mapping> createMapping(Json const& _node); |
102 | | ASTPointer<ArrayTypeName> createArrayTypeName(Json const& _node); |
103 | | ASTPointer<InlineAssembly> createInlineAssembly(Json const& _node); |
104 | | ASTPointer<Block> createBlock(Json const& _node, bool _unchecked); |
105 | | ASTPointer<PlaceholderStatement> createPlaceholderStatement(Json const& _node); |
106 | | ASTPointer<IfStatement> createIfStatement(Json const& _node); |
107 | | ASTPointer<TryCatchClause> createTryCatchClause(Json const& _node); |
108 | | ASTPointer<TryStatement> createTryStatement(Json const& _node); |
109 | | ASTPointer<WhileStatement> createWhileStatement(Json const& _node, bool _isDoWhile); |
110 | | ASTPointer<ForStatement> createForStatement(Json const& _node); |
111 | | ASTPointer<Continue> createContinue(Json const& _node); |
112 | | ASTPointer<Break> createBreak(Json const& _node); |
113 | | ASTPointer<Return> createReturn(Json const& _node); |
114 | | ASTPointer<Throw> createThrow(Json const& _node); |
115 | | ASTPointer<EmitStatement> createEmitStatement(Json const& _node); |
116 | | ASTPointer<RevertStatement> createRevertStatement(Json const& _node); |
117 | | ASTPointer<VariableDeclarationStatement> createVariableDeclarationStatement(Json const& _node); |
118 | | ASTPointer<ExpressionStatement> createExpressionStatement(Json const& _node); |
119 | | ASTPointer<Conditional> createConditional(Json const& _node); |
120 | | ASTPointer<Assignment> createAssignment(Json const& _node); |
121 | | ASTPointer<TupleExpression> createTupleExpression(Json const& _node); |
122 | | ASTPointer<UnaryOperation> createUnaryOperation(Json const& _node); |
123 | | ASTPointer<BinaryOperation> createBinaryOperation(Json const& _node); |
124 | | ASTPointer<FunctionCall> createFunctionCall(Json const& _node); |
125 | | ASTPointer<FunctionCallOptions> createFunctionCallOptions(Json const& _node); |
126 | | ASTPointer<NewExpression> createNewExpression(Json const& _node); |
127 | | ASTPointer<MemberAccess> createMemberAccess(Json const& _node); |
128 | | ASTPointer<IndexAccess> createIndexAccess(Json const& _node); |
129 | | ASTPointer<IndexRangeAccess> createIndexRangeAccess(Json const& _node); |
130 | | ASTPointer<Identifier> createIdentifier(Json const& _node); |
131 | | ASTPointer<ElementaryTypeNameExpression> createElementaryTypeNameExpression(Json const& _node); |
132 | | ASTPointer<ASTNode> createLiteral(Json const& _node); |
133 | | ASTPointer<StructuredDocumentation> createDocumentation(Json const& _node); |
134 | | ASTPointer<StorageLayoutSpecifier> createStorageLayoutSpecifier(Json const& _node); |
135 | | ///@} |
136 | | |
137 | | // =============== general helper functions =================== |
138 | | /// @returns the member of a given JSON object or null if member does not exist |
139 | | Json member(Json const& _node, std::string const& _name); |
140 | | /// @returns the appropriate TokenObject used in parsed Strings (pragma directive or operator) |
141 | | Token scanSingleToken(Json const& _node); |
142 | | template<class T> |
143 | | ///@returns nullptr or an ASTPointer cast to a specific Class |
144 | | ASTPointer<T> nullOrCast(Json const& _json); |
145 | | /// @returns nullptr or ASTString, given an JSON string or an empty field |
146 | | ASTPointer<ASTString> nullOrASTString(Json const& _json, std::string const& _name); |
147 | | |
148 | | // ============== JSON to definition helpers =============== |
149 | | /// \defgroup typeHelpers Json to ast-datatype helpers |
150 | | /// {@ |
151 | | ASTPointer<ASTString> memberAsASTString(Json const& _node, std::string const& _name); |
152 | | bool memberAsBool(Json const& _node, std::string const& _name); |
153 | | Visibility visibility(Json const& _node); |
154 | | StateMutability stateMutability(Json const& _node); |
155 | | VariableDeclaration::Location location(Json const& _node); |
156 | | ContractKind contractKind(Json const& _node); |
157 | | Token literalTokenKind(Json const& _node); |
158 | | Literal::SubDenomination subdenomination(Json const& _node); |
159 | | ///@} |
160 | | |
161 | | // =========== member variables =============== |
162 | | /// list of source names, order by source index |
163 | | std::vector<std::shared_ptr<std::string const>> m_sourceNames; |
164 | | /// filepath to AST |
165 | | std::map<std::string, ASTPointer<SourceUnit>> m_sourceUnits; |
166 | | /// IDs already used by the nodes |
167 | | std::set<int64_t> m_usedIDs; |
168 | | /// Configured EVM version |
169 | | langutil::EVMVersion m_evmVersion; |
170 | | }; |
171 | | |
172 | | } |