From 616a9854e96b0892f3f0c881d87185b163cf3b87 Mon Sep 17 00:00:00 2001 From: djudjuu Date: Mon, 13 Mar 2017 15:19:41 +0100 Subject: [PATCH 1/2] AST entry for VariableDeclaration --- libsolidity/ast/ASTJsonConverter.cpp | 45 ++++++++++++++++++++++++++-- libsolidity/ast/ASTJsonConverter.h | 8 +++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp index 69c10c8d3..c57fd6b26 100644 --- a/libsolidity/ast/ASTJsonConverter.cpp +++ b/libsolidity/ast/ASTJsonConverter.cpp @@ -39,6 +39,21 @@ void ASTJsonConverter::addJsonNode( initializer_list> _attributes, bool _hasChildren = false ) +{ + ASTJsonConverter::addJsonNode( + _node, + _nodeName, + std::vector>(_attributes), + _hasChildren + ); +} + +void ASTJsonConverter::addJsonNode( + ASTNode const& _node, + string const& _nodeName, + std::vector> const& _attributes, + bool _hasChildren = false +) { Json::Value node; @@ -183,11 +198,18 @@ bool ASTJsonConverter::visit(FunctionDefinition const& _node) bool ASTJsonConverter::visit(VariableDeclaration const& _node) { - addJsonNode(_node, "VariableDeclaration", { + std::vector> attributes = { make_pair("name", _node.name()), - make_pair("type", type(_node)) - }, true); + make_pair("type", type(_node)), + make_pair("constant", _node.isConstant()), + make_pair("storageLocation", location(_node.referenceLocation())), + make_pair("visibility", visibility(_node.visibility())) + }; + if (m_inEvent) + attributes.push_back(make_pair("indexed", _node.isIndexed())); + addJsonNode(_node, "VariableDeclaration", attributes, true); return true; + } bool ASTJsonConverter::visit(ModifierDefinition const& _node) @@ -209,6 +231,7 @@ bool ASTJsonConverter::visit(TypeName const&) bool ASTJsonConverter::visit(EventDefinition const& _node) { + m_inEvent = true; addJsonNode(_node, "EventDefinition", { make_pair("name", _node.name()) }, true); return true; } @@ -502,6 +525,7 @@ void ASTJsonConverter::endVisit(ModifierInvocation const&) void ASTJsonConverter::endVisit(EventDefinition const&) { + m_inEvent = false; goUp(); } @@ -670,6 +694,21 @@ string ASTJsonConverter::visibility(Declaration::Visibility const& _visibility) } } +string ASTJsonConverter::location( VariableDeclaration::Location _location) +{ + switch (_location) + { + case VariableDeclaration::Location::Default: + return "default"; + case VariableDeclaration::Location::Storage: + return "storage"; + case VariableDeclaration::Location::Memory: + return "memory"; + default: + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown declaration location.")); + } +} + string ASTJsonConverter::type(Expression const& _expression) { return _expression.annotation().type ? _expression.annotation().type->toString() : "Unknown"; diff --git a/libsolidity/ast/ASTJsonConverter.h b/libsolidity/ast/ASTJsonConverter.h index 49f23f999..603f2ab15 100644 --- a/libsolidity/ast/ASTJsonConverter.h +++ b/libsolidity/ast/ASTJsonConverter.h @@ -151,8 +151,15 @@ private: std::initializer_list> _attributes, bool _hasChildren ); + void addJsonNode( + ASTNode const& _node, + std::string const& _nodeName, + std::vector> const& _attributes, + bool _hasChildren + ); std::string sourceLocationToString(SourceLocation const& _location) const; std::string visibility(Declaration::Visibility const& _visibility); + std::string location(VariableDeclaration::Location _location); std::string type(Expression const& _expression); std::string type(VariableDeclaration const& _varDecl); inline void goUp() @@ -161,6 +168,7 @@ private: m_jsonNodePtrs.pop(); } + bool m_inEvent = false; bool processed = false; Json::Value m_astJson; std::stack m_jsonNodePtrs; From 44a032992144222aa604e94c1a2b6a63f9f34b4d Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 17 Mar 2017 14:44:44 +0100 Subject: [PATCH 2/2] Comment. --- libsolidity/ast/ASTJsonConverter.cpp | 2 +- libsolidity/ast/ASTJsonConverter.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp index c57fd6b26..9ea23687d 100644 --- a/libsolidity/ast/ASTJsonConverter.cpp +++ b/libsolidity/ast/ASTJsonConverter.cpp @@ -694,7 +694,7 @@ string ASTJsonConverter::visibility(Declaration::Visibility const& _visibility) } } -string ASTJsonConverter::location( VariableDeclaration::Location _location) +string ASTJsonConverter::location(VariableDeclaration::Location _location) { switch (_location) { diff --git a/libsolidity/ast/ASTJsonConverter.h b/libsolidity/ast/ASTJsonConverter.h index 603f2ab15..bd5e65604 100644 --- a/libsolidity/ast/ASTJsonConverter.h +++ b/libsolidity/ast/ASTJsonConverter.h @@ -168,7 +168,7 @@ private: m_jsonNodePtrs.pop(); } - bool m_inEvent = false; + bool m_inEvent = false; ///< whether we are currently inside an event or not bool processed = false; Json::Value m_astJson; std::stack m_jsonNodePtrs;