2015-01-05 14:46:40 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-01-05 14:46:40 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-01-05 14:46:40 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-01-05 14:46:40 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-01-05 14:46:40 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Lefteris <lefteris@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Converts the AST into json format
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <ostream>
|
2015-01-06 15:50:04 +00:00
|
|
|
#include <stack>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/ASTVisitor.h>
|
|
|
|
#include <libsolidity/interface/Exceptions.h>
|
|
|
|
#include <libsolidity/ast/ASTAnnotations.h>
|
2015-02-10 11:15:44 +00:00
|
|
|
#include <json/json.h>
|
2015-01-05 14:46:40 +00:00
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converter of the AST into JSON format
|
|
|
|
*/
|
|
|
|
class ASTJsonConverter: public ASTConstVisitor
|
|
|
|
{
|
|
|
|
public:
|
2015-01-16 10:44:55 +00:00
|
|
|
/// Create a converter to JSON for the given abstract syntax tree.
|
2017-03-20 18:06:17 +00:00
|
|
|
/// @a _legacy if true, use legacy format
|
2016-07-01 08:14:50 +00:00
|
|
|
/// @a _sourceIndices is used to abbreviate source names in source locations.
|
|
|
|
explicit ASTJsonConverter(
|
2017-03-20 18:06:17 +00:00
|
|
|
bool _legacy,
|
2016-08-01 22:57:58 +00:00
|
|
|
std::map<std::string, unsigned> _sourceIndices = std::map<std::string, unsigned>()
|
2016-07-01 08:14:50 +00:00
|
|
|
);
|
2015-01-05 14:46:40 +00:00
|
|
|
/// Output the json representation of the AST to _stream.
|
2017-03-20 18:06:17 +00:00
|
|
|
void print(std::ostream& _stream, ASTNode const& _node);
|
2017-07-27 14:40:01 +00:00
|
|
|
Json::Value&& toJson(ASTNode const& _node);
|
2017-03-20 18:06:17 +00:00
|
|
|
template <class T>
|
|
|
|
Json::Value toJson(std::vector<ASTPointer<T>> const& _nodes)
|
|
|
|
{
|
|
|
|
Json::Value ret(Json::arrayValue);
|
|
|
|
for (auto const& n: _nodes)
|
2017-07-27 14:40:01 +00:00
|
|
|
if (n)
|
|
|
|
appendMove(ret, toJson(*n));
|
|
|
|
else
|
|
|
|
ret.append(Json::nullValue);
|
2017-03-20 18:06:17 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2016-08-18 12:51:17 +00:00
|
|
|
bool visit(SourceUnit const& _node) override;
|
2016-08-19 17:57:21 +00:00
|
|
|
bool visit(PragmaDirective const& _node) override;
|
2015-01-05 14:46:40 +00:00
|
|
|
bool visit(ImportDirective const& _node) override;
|
|
|
|
bool visit(ContractDefinition const& _node) override;
|
2016-08-16 13:25:25 +00:00
|
|
|
bool visit(InheritanceSpecifier const& _node) override;
|
2016-08-16 14:28:28 +00:00
|
|
|
bool visit(UsingForDirective const& _node) override;
|
2015-01-05 14:46:40 +00:00
|
|
|
bool visit(StructDefinition const& _node) override;
|
2016-08-16 15:18:57 +00:00
|
|
|
bool visit(EnumDefinition const& _node) override;
|
2016-08-16 15:38:24 +00:00
|
|
|
bool visit(EnumValue const& _node) override;
|
2015-01-05 14:46:40 +00:00
|
|
|
bool visit(ParameterList const& _node) override;
|
|
|
|
bool visit(FunctionDefinition const& _node) override;
|
|
|
|
bool visit(VariableDeclaration const& _node) override;
|
2016-08-17 14:23:20 +00:00
|
|
|
bool visit(ModifierDefinition const& _node) override;
|
2016-08-17 14:52:14 +00:00
|
|
|
bool visit(ModifierInvocation const& _node) override;
|
|
|
|
bool visit(EventDefinition const& _node) override;
|
2015-01-05 14:46:40 +00:00
|
|
|
bool visit(ElementaryTypeName const& _node) override;
|
|
|
|
bool visit(UserDefinedTypeName const& _node) override;
|
2016-09-27 19:37:32 +00:00
|
|
|
bool visit(FunctionTypeName const& _node) override;
|
2015-01-05 14:46:40 +00:00
|
|
|
bool visit(Mapping const& _node) override;
|
2016-08-17 15:09:21 +00:00
|
|
|
bool visit(ArrayTypeName const& _node) override;
|
2016-02-22 01:13:41 +00:00
|
|
|
bool visit(InlineAssembly const& _node) override;
|
2015-01-05 14:46:40 +00:00
|
|
|
bool visit(Block const& _node) override;
|
2016-08-17 15:20:17 +00:00
|
|
|
bool visit(PlaceholderStatement const& _node) override;
|
2015-01-05 14:46:40 +00:00
|
|
|
bool visit(IfStatement const& _node) override;
|
|
|
|
bool visit(WhileStatement const& _node) override;
|
|
|
|
bool visit(ForStatement const& _node) override;
|
|
|
|
bool visit(Continue const& _node) override;
|
|
|
|
bool visit(Break const& _node) override;
|
|
|
|
bool visit(Return const& _node) override;
|
2015-09-15 14:33:14 +00:00
|
|
|
bool visit(Throw const& _node) override;
|
2018-02-16 15:55:21 +00:00
|
|
|
bool visit(EmitStatement const& _node) override;
|
2015-02-17 15:21:38 +00:00
|
|
|
bool visit(VariableDeclarationStatement const& _node) override;
|
2015-01-05 14:46:40 +00:00
|
|
|
bool visit(ExpressionStatement const& _node) override;
|
2015-12-22 16:47:00 +00:00
|
|
|
bool visit(Conditional const& _node) override;
|
2015-01-05 14:46:40 +00:00
|
|
|
bool visit(Assignment const& _node) override;
|
2015-10-12 21:02:35 +00:00
|
|
|
bool visit(TupleExpression const& _node) override;
|
2015-01-05 14:46:40 +00:00
|
|
|
bool visit(UnaryOperation const& _node) override;
|
|
|
|
bool visit(BinaryOperation const& _node) override;
|
|
|
|
bool visit(FunctionCall const& _node) override;
|
|
|
|
bool visit(NewExpression const& _node) override;
|
|
|
|
bool visit(MemberAccess const& _node) override;
|
|
|
|
bool visit(IndexAccess const& _node) override;
|
|
|
|
bool visit(Identifier const& _node) override;
|
|
|
|
bool visit(ElementaryTypeNameExpression const& _node) override;
|
|
|
|
bool visit(Literal const& _node) override;
|
|
|
|
|
2016-08-17 14:52:14 +00:00
|
|
|
void endVisit(EventDefinition const&) override;
|
2015-01-05 14:46:40 +00:00
|
|
|
|
|
|
|
private:
|
2017-03-20 18:06:17 +00:00
|
|
|
void setJsonNode(
|
2016-07-01 08:14:50 +00:00
|
|
|
ASTNode const& _node,
|
|
|
|
std::string const& _nodeName,
|
2017-03-20 18:06:17 +00:00
|
|
|
std::initializer_list<std::pair<std::string, Json::Value>>&& _attributes
|
2016-07-01 08:14:50 +00:00
|
|
|
);
|
2017-03-20 18:06:17 +00:00
|
|
|
void setJsonNode(
|
2017-03-13 14:19:41 +00:00
|
|
|
ASTNode const& _node,
|
|
|
|
std::string const& _nodeName,
|
2017-03-20 18:06:17 +00:00
|
|
|
std::vector<std::pair<std::string, Json::Value>>&& _attributes
|
2017-03-13 14:19:41 +00:00
|
|
|
);
|
2016-07-01 08:14:50 +00:00
|
|
|
std::string sourceLocationToString(SourceLocation const& _location) const;
|
2017-08-30 21:44:46 +00:00
|
|
|
static std::string namePathToString(std::vector<ASTString> const& _namePath);
|
2017-08-21 21:05:24 +00:00
|
|
|
static Json::Value idOrNull(ASTNode const* _pt)
|
2017-03-20 18:06:17 +00:00
|
|
|
{
|
|
|
|
return _pt ? Json::Value(nodeId(*_pt)) : Json::nullValue;
|
|
|
|
}
|
|
|
|
Json::Value toJsonOrNull(ASTNode const* _node)
|
|
|
|
{
|
|
|
|
return _node ? toJson(*_node) : Json::nullValue;
|
|
|
|
}
|
2017-08-30 21:44:46 +00:00
|
|
|
Json::Value inlineAssemblyIdentifierToJson(std::pair<assembly::Identifier const* , InlineAssemblyAnnotation::ExternalIdentifierInfo> _info) const;
|
|
|
|
static std::string location(VariableDeclaration::Location _location);
|
|
|
|
static std::string contractKind(ContractDefinition::ContractKind _kind);
|
|
|
|
static std::string functionCallKind(FunctionCallKind _kind);
|
|
|
|
static std::string literalTokenKind(Token::Value _token);
|
|
|
|
static std::string type(Expression const& _expression);
|
|
|
|
static std::string type(VariableDeclaration const& _varDecl);
|
2017-08-21 21:05:24 +00:00
|
|
|
static int nodeId(ASTNode const& _node)
|
2015-01-06 15:50:04 +00:00
|
|
|
{
|
2017-03-20 18:06:17 +00:00
|
|
|
return _node.id();
|
2015-01-28 07:50:53 +00:00
|
|
|
}
|
2017-04-25 16:47:11 +00:00
|
|
|
template<class Container>
|
2017-08-21 21:05:24 +00:00
|
|
|
static Json::Value getContainerIds(Container const& container)
|
2017-04-25 16:47:11 +00:00
|
|
|
{
|
|
|
|
Json::Value tmp(Json::arrayValue);
|
2017-05-17 16:22:39 +00:00
|
|
|
for (auto const& element: container)
|
2017-04-25 16:47:11 +00:00
|
|
|
{
|
2017-05-17 16:22:39 +00:00
|
|
|
solAssert(element, "");
|
|
|
|
tmp.append(nodeId(*element));
|
2017-04-25 16:47:11 +00:00
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
2017-08-30 21:44:46 +00:00
|
|
|
static Json::Value typePointerToJson(TypePointer _tp);
|
|
|
|
static Json::Value typePointerToJson(std::shared_ptr<std::vector<TypePointer>> _tps);
|
2017-03-20 18:06:17 +00:00
|
|
|
void appendExpressionAttributes(
|
2017-04-25 16:47:11 +00:00
|
|
|
std::vector<std::pair<std::string, Json::Value>> &_attributes,
|
2017-03-20 18:06:17 +00:00
|
|
|
ExpressionAnnotation const& _annotation
|
|
|
|
);
|
2017-07-27 14:40:01 +00:00
|
|
|
static void appendMove(Json::Value& _array, Json::Value&& _value)
|
|
|
|
{
|
|
|
|
solAssert(_array.isArray(), "");
|
|
|
|
_array.append(std::move(_value));
|
|
|
|
}
|
|
|
|
|
2017-03-20 18:06:17 +00:00
|
|
|
bool m_legacy = false; ///< if true, use legacy format
|
2017-03-17 13:44:44 +00:00
|
|
|
bool m_inEvent = false; ///< whether we are currently inside an event or not
|
2017-03-20 18:06:17 +00:00
|
|
|
Json::Value m_currentValue;
|
2016-08-01 22:57:58 +00:00
|
|
|
std::map<std::string, unsigned> m_sourceIndices;
|
2015-01-05 14:46:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|