mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #1810 from ethereum/compactJson
Compact format for AST-Json.
This commit is contained in:
@@ -1238,13 +1238,17 @@ bool TypeChecker::visit(FunctionCall const& _functionCall)
|
||||
|
||||
if (auto const* typeType = dynamic_cast<TypeType const*>(expressionType.get()))
|
||||
{
|
||||
_functionCall.annotation().isStructConstructorCall = (typeType->actualType()->category() == Type::Category::Struct);
|
||||
_functionCall.annotation().isTypeConversion = !_functionCall.annotation().isStructConstructorCall;
|
||||
if (typeType->actualType()->category() == Type::Category::Struct)
|
||||
_functionCall.annotation().kind = FunctionCallKind::StructConstructorCall;
|
||||
else
|
||||
_functionCall.annotation().kind = FunctionCallKind::TypeConversion;
|
||||
|
||||
}
|
||||
else
|
||||
_functionCall.annotation().isStructConstructorCall = _functionCall.annotation().isTypeConversion = false;
|
||||
_functionCall.annotation().kind = FunctionCallKind::FunctionCall;
|
||||
solAssert(_functionCall.annotation().kind != FunctionCallKind::Unset, "");
|
||||
|
||||
if (_functionCall.annotation().isTypeConversion)
|
||||
if (_functionCall.annotation().kind == FunctionCallKind::TypeConversion)
|
||||
{
|
||||
TypeType const& t = dynamic_cast<TypeType const&>(*expressionType);
|
||||
TypePointer resultType = t.actualType();
|
||||
@@ -1274,7 +1278,7 @@ bool TypeChecker::visit(FunctionCall const& _functionCall)
|
||||
|
||||
/// For error message: Struct members that were removed during conversion to memory.
|
||||
set<string> membersRemovedForStructConstructor;
|
||||
if (_functionCall.annotation().isStructConstructorCall)
|
||||
if (_functionCall.annotation().kind == FunctionCallKind::StructConstructorCall)
|
||||
{
|
||||
TypeType const& t = dynamic_cast<TypeType const&>(*expressionType);
|
||||
auto const& structType = dynamic_cast<StructType const&>(*t.actualType());
|
||||
@@ -1312,7 +1316,7 @@ bool TypeChecker::visit(FunctionCall const& _functionCall)
|
||||
toString(parameterTypes.size()) +
|
||||
".";
|
||||
// Extend error message in case we try to construct a struct with mapping member.
|
||||
if (_functionCall.annotation().isStructConstructorCall && !membersRemovedForStructConstructor.empty())
|
||||
if (_functionCall.annotation().kind == FunctionCallKind::StructConstructorCall && !membersRemovedForStructConstructor.empty())
|
||||
{
|
||||
msg += " Members that have to be skipped in memory:";
|
||||
for (auto const& member: membersRemovedForStructConstructor)
|
||||
|
||||
@@ -583,8 +583,7 @@ public:
|
||||
bool isPayable() const { return m_isPayable; }
|
||||
std::vector<ASTPointer<ModifierInvocation>> const& modifiers() const { return m_functionModifiers; }
|
||||
std::vector<ASTPointer<VariableDeclaration>> const& returnParameters() const { return m_returnParameters->parameters(); }
|
||||
Block const& body() const { return *m_body; }
|
||||
|
||||
Block const& body() const { solAssert(m_body, ""); return *m_body; }
|
||||
virtual bool isVisibleInContract() const override
|
||||
{
|
||||
return Declaration::isVisibleInContract() && !isConstructor() && !name().empty();
|
||||
@@ -1314,7 +1313,7 @@ private:
|
||||
|
||||
/**
|
||||
* Tuple, parenthesized expression, or bracketed expression.
|
||||
* Examples: (1, 2), (x,), (x), (), [1, 2],
|
||||
* Examples: (1, 2), (x,), (x), (), [1, 2],
|
||||
* Individual components might be empty shared pointers (as in the second example).
|
||||
* The respective types in lvalue context are: 2-tuple, 2-tuple (with wildcard), type of x, 0-tuple
|
||||
* Not in lvalue context: 2-tuple, _1_-tuple, type of x, 0-tuple.
|
||||
@@ -1327,8 +1326,8 @@ public:
|
||||
std::vector<ASTPointer<Expression>> const& _components,
|
||||
bool _isArray
|
||||
):
|
||||
Expression(_location),
|
||||
m_components(_components),
|
||||
Expression(_location),
|
||||
m_components(_components),
|
||||
m_isArray(_isArray) {}
|
||||
virtual void accept(ASTVisitor& _visitor) override;
|
||||
virtual void accept(ASTConstVisitor& _visitor) const override;
|
||||
|
||||
@@ -200,12 +200,17 @@ struct BinaryOperationAnnotation: ExpressionAnnotation
|
||||
TypePointer commonType;
|
||||
};
|
||||
|
||||
enum class FunctionCallKind
|
||||
{
|
||||
Unset,
|
||||
FunctionCall,
|
||||
TypeConversion,
|
||||
StructConstructorCall
|
||||
};
|
||||
|
||||
struct FunctionCallAnnotation: ExpressionAnnotation
|
||||
{
|
||||
/// Whether this is an explicit type conversion.
|
||||
bool isTypeConversion = false;
|
||||
/// Whether this is a struct constructor call.
|
||||
bool isStructConstructorCall = false;
|
||||
FunctionCallKind kind = FunctionCallKind::Unset;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -42,15 +42,23 @@ class ASTJsonConverter: public ASTConstVisitor
|
||||
{
|
||||
public:
|
||||
/// Create a converter to JSON for the given abstract syntax tree.
|
||||
/// @a _legacy if true, use legacy format
|
||||
/// @a _sourceIndices is used to abbreviate source names in source locations.
|
||||
explicit ASTJsonConverter(
|
||||
ASTNode const& _ast,
|
||||
bool _legacy,
|
||||
std::map<std::string, unsigned> _sourceIndices = std::map<std::string, unsigned>()
|
||||
);
|
||||
/// Output the json representation of the AST to _stream.
|
||||
void print(std::ostream& _stream);
|
||||
Json::Value const& json();
|
||||
|
||||
void print(std::ostream& _stream, ASTNode const& _node);
|
||||
Json::Value toJson(ASTNode const& _node);
|
||||
template <class T>
|
||||
Json::Value toJson(std::vector<ASTPointer<T>> const& _nodes)
|
||||
{
|
||||
Json::Value ret(Json::arrayValue);
|
||||
for (auto const& n: _nodes)
|
||||
ret.append(n ? toJson(*n) : Json::nullValue);
|
||||
return ret;
|
||||
}
|
||||
bool visit(SourceUnit const& _node) override;
|
||||
bool visit(PragmaDirective const& _node) override;
|
||||
bool visit(ImportDirective const& _node) override;
|
||||
@@ -97,82 +105,60 @@ public:
|
||||
bool visit(ElementaryTypeNameExpression const& _node) override;
|
||||
bool visit(Literal const& _node) override;
|
||||
|
||||
void endVisit(SourceUnit const&) override;
|
||||
void endVisit(PragmaDirective const&) override;
|
||||
void endVisit(ImportDirective const&) override;
|
||||
void endVisit(ContractDefinition const&) override;
|
||||
void endVisit(InheritanceSpecifier const&) override;
|
||||
void endVisit(UsingForDirective const&) override;
|
||||
void endVisit(StructDefinition const&) override;
|
||||
void endVisit(EnumDefinition const&) override;
|
||||
void endVisit(EnumValue const&) override;
|
||||
void endVisit(ParameterList const&) override;
|
||||
void endVisit(FunctionDefinition const&) override;
|
||||
void endVisit(VariableDeclaration const&) override;
|
||||
void endVisit(ModifierDefinition const&) override;
|
||||
void endVisit(ModifierInvocation const&) override;
|
||||
void endVisit(EventDefinition const&) override;
|
||||
void endVisit(TypeName const&) override;
|
||||
void endVisit(ElementaryTypeName const&) override;
|
||||
void endVisit(UserDefinedTypeName const&) override;
|
||||
void endVisit(FunctionTypeName const&) override;
|
||||
void endVisit(Mapping const&) override;
|
||||
void endVisit(ArrayTypeName const&) override;
|
||||
void endVisit(InlineAssembly const&) override;
|
||||
void endVisit(Block const&) override;
|
||||
void endVisit(PlaceholderStatement const&) override;
|
||||
void endVisit(IfStatement const&) override;
|
||||
void endVisit(WhileStatement const&) override;
|
||||
void endVisit(ForStatement const&) override;
|
||||
void endVisit(Continue const&) override;
|
||||
void endVisit(Break const&) override;
|
||||
void endVisit(Return const&) override;
|
||||
void endVisit(Throw const&) override;
|
||||
void endVisit(VariableDeclarationStatement const&) override;
|
||||
void endVisit(ExpressionStatement const&) override;
|
||||
void endVisit(Conditional const&) override;
|
||||
void endVisit(Assignment const&) override;
|
||||
void endVisit(TupleExpression const&) override;
|
||||
void endVisit(UnaryOperation const&) override;
|
||||
void endVisit(BinaryOperation const&) override;
|
||||
void endVisit(FunctionCall const&) override;
|
||||
void endVisit(NewExpression const&) override;
|
||||
void endVisit(MemberAccess const&) override;
|
||||
void endVisit(IndexAccess const&) override;
|
||||
void endVisit(Identifier const&) override;
|
||||
void endVisit(ElementaryTypeNameExpression const&) override;
|
||||
void endVisit(Literal const&) override;
|
||||
|
||||
private:
|
||||
void process();
|
||||
void addJsonNode(
|
||||
void setJsonNode(
|
||||
ASTNode const& _node,
|
||||
std::string const& _nodeName,
|
||||
std::initializer_list<std::pair<std::string const, Json::Value const>> _attributes,
|
||||
bool _hasChildren
|
||||
std::initializer_list<std::pair<std::string, Json::Value>>&& _attributes
|
||||
);
|
||||
void addJsonNode(
|
||||
void setJsonNode(
|
||||
ASTNode const& _node,
|
||||
std::string const& _nodeName,
|
||||
std::vector<std::pair<std::string const, Json::Value const>> const& _attributes,
|
||||
bool _hasChildren
|
||||
std::vector<std::pair<std::string, Json::Value>>&& _attributes
|
||||
);
|
||||
std::string sourceLocationToString(SourceLocation const& _location) const;
|
||||
std::string namePathToString(std::vector<ASTString> const& _namePath) const;
|
||||
Json::Value idOrNull(ASTNode const* _pt)
|
||||
{
|
||||
return _pt ? Json::Value(nodeId(*_pt)) : Json::nullValue;
|
||||
}
|
||||
Json::Value toJsonOrNull(ASTNode const* _node)
|
||||
{
|
||||
return _node ? toJson(*_node) : Json::nullValue;
|
||||
}
|
||||
Json::Value inlineAssemblyIdentifierToJson(std::pair<assembly::Identifier const* , InlineAssemblyAnnotation::ExternalIdentifierInfo> _info);
|
||||
std::string visibility(Declaration::Visibility const& _visibility);
|
||||
std::string location(VariableDeclaration::Location _location);
|
||||
std::string contractKind(ContractDefinition::ContractKind _kind);
|
||||
std::string functionCallKind(FunctionCallKind _kind);
|
||||
std::string type(Expression const& _expression);
|
||||
std::string type(VariableDeclaration const& _varDecl);
|
||||
inline void goUp()
|
||||
int nodeId(ASTNode const& _node)
|
||||
{
|
||||
solAssert(!m_jsonNodePtrs.empty(), "Uneven json nodes stack. Internal error.");
|
||||
m_jsonNodePtrs.pop();
|
||||
return _node.id();
|
||||
}
|
||||
|
||||
template<class Container>
|
||||
Json::Value getContainerIds(Container const& container)
|
||||
{
|
||||
Json::Value tmp(Json::arrayValue);
|
||||
for (auto const& element: container)
|
||||
{
|
||||
solAssert(element, "");
|
||||
tmp.append(nodeId(*element));
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
Json::Value typePointerToJson(TypePointer _tp);
|
||||
Json::Value typePointerToJson(std::shared_ptr<std::vector<TypePointer>> _tps);
|
||||
void appendExpressionAttributes(
|
||||
std::vector<std::pair<std::string, Json::Value>> &_attributes,
|
||||
ExpressionAnnotation const& _annotation
|
||||
);
|
||||
bool m_legacy = false; ///< if true, use legacy format
|
||||
bool m_inEvent = false; ///< whether we are currently inside an event or not
|
||||
bool processed = false;
|
||||
Json::Value m_astJson;
|
||||
std::stack<Json::Value*> m_jsonNodePtrs;
|
||||
ASTNode const* m_ast;
|
||||
Json::Value m_currentValue;
|
||||
std::map<std::string, unsigned> m_sourceIndices;
|
||||
};
|
||||
|
||||
|
||||
@@ -2183,6 +2183,8 @@ string FunctionType::identifier() const
|
||||
case Kind::ArrayPush: id += "arraypush"; break;
|
||||
case Kind::ByteArrayPush: id += "bytearraypush"; break;
|
||||
case Kind::ObjectCreation: id += "objectcreation"; break;
|
||||
case Kind::Assert: id += "assert"; break;
|
||||
case Kind::Require: id += "require";break;
|
||||
default: solAssert(false, "Unknown function location."); break;
|
||||
}
|
||||
if (isConstant())
|
||||
|
||||
@@ -434,7 +434,7 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
|
||||
bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
||||
{
|
||||
CompilerContext::LocationSetter locationSetter(m_context, _functionCall);
|
||||
if (_functionCall.annotation().isTypeConversion)
|
||||
if (_functionCall.annotation().kind == FunctionCallKind::TypeConversion)
|
||||
{
|
||||
solAssert(_functionCall.arguments().size() == 1, "");
|
||||
solAssert(_functionCall.names().empty(), "");
|
||||
@@ -445,7 +445,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
||||
}
|
||||
|
||||
FunctionTypePointer functionType;
|
||||
if (_functionCall.annotation().isStructConstructorCall)
|
||||
if (_functionCall.annotation().kind == FunctionCallKind::StructConstructorCall)
|
||||
{
|
||||
auto const& type = dynamic_cast<TypeType const&>(*_functionCall.expression().annotation().type);
|
||||
auto const& structType = dynamic_cast<StructType const&>(*type.actualType());
|
||||
@@ -476,7 +476,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
||||
solAssert(found, "");
|
||||
}
|
||||
|
||||
if (_functionCall.annotation().isStructConstructorCall)
|
||||
if (_functionCall.annotation().kind == FunctionCallKind::StructConstructorCall)
|
||||
{
|
||||
TypeType const& type = dynamic_cast<TypeType const&>(*_functionCall.expression().annotation().type);
|
||||
auto const& structType = dynamic_cast<StructType const&>(*type.actualType());
|
||||
|
||||
@@ -582,7 +582,7 @@ bool Why3Translator::visit(BinaryOperation const& _binaryOperation)
|
||||
|
||||
bool Why3Translator::visit(FunctionCall const& _node)
|
||||
{
|
||||
if (_node.annotation().isTypeConversion || _node.annotation().isStructConstructorCall)
|
||||
if (_node.annotation().kind == FunctionCallKind::TypeConversion || _node.annotation().kind == FunctionCallKind::StructConstructorCall)
|
||||
{
|
||||
error(_node, "Only ordinary function calls supported.");
|
||||
return true;
|
||||
|
||||
@@ -377,7 +377,8 @@ Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
|
||||
{
|
||||
Json::Value sourceResult = Json::objectValue;
|
||||
sourceResult["id"] = sourceIndex++;
|
||||
sourceResult["legacyAST"] = ASTJsonConverter(m_compilerStack.ast(source), m_compilerStack.sourceIndices()).json();
|
||||
sourceResult["ast"] = ASTJsonConverter(false, m_compilerStack.sourceIndices()).toJson(m_compilerStack.ast(source));
|
||||
sourceResult["legacyAST"] = ASTJsonConverter(true, m_compilerStack.sourceIndices()).toJson(m_compilerStack.ast(source));
|
||||
output["sources"][source] = sourceResult;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user