Type information for AST printer.

This commit is contained in:
Christian 2014-10-16 17:57:27 +02:00
parent fd046d7c90
commit a09e4c559d
4 changed files with 53 additions and 7 deletions

View File

@ -172,6 +172,7 @@ bool ASTPrinter::visit(VariableDefinition& _node)
bool ASTPrinter::visit(Expression& _node) bool ASTPrinter::visit(Expression& _node)
{ {
writeLine("Expression"); writeLine("Expression");
printType(_node);
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }
@ -179,6 +180,7 @@ bool ASTPrinter::visit(Expression& _node)
bool ASTPrinter::visit(Assignment& _node) bool ASTPrinter::visit(Assignment& _node)
{ {
writeLine(std::string("Assignment using operator ") + Token::toString(_node.getAssignmentOperator())); writeLine(std::string("Assignment using operator ") + Token::toString(_node.getAssignmentOperator()));
printType(_node);
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }
@ -187,6 +189,7 @@ bool ASTPrinter::visit(UnaryOperation& _node)
{ {
writeLine(std::string("UnaryOperation (") + (_node.isPrefixOperation() ? "prefix" : "postfix") + writeLine(std::string("UnaryOperation (") + (_node.isPrefixOperation() ? "prefix" : "postfix") +
") " + Token::toString(_node.getOperator())); ") " + Token::toString(_node.getOperator()));
printType(_node);
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }
@ -194,6 +197,7 @@ bool ASTPrinter::visit(UnaryOperation& _node)
bool ASTPrinter::visit(BinaryOperation& _node) bool ASTPrinter::visit(BinaryOperation& _node)
{ {
writeLine(std::string("BinaryOperation using operator ") + Token::toString(_node.getOperator())); writeLine(std::string("BinaryOperation using operator ") + Token::toString(_node.getOperator()));
printType(_node);
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }
@ -201,6 +205,7 @@ bool ASTPrinter::visit(BinaryOperation& _node)
bool ASTPrinter::visit(FunctionCall& _node) bool ASTPrinter::visit(FunctionCall& _node)
{ {
writeLine("FunctionCall"); writeLine("FunctionCall");
printType(_node);
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }
@ -208,6 +213,7 @@ bool ASTPrinter::visit(FunctionCall& _node)
bool ASTPrinter::visit(MemberAccess& _node) bool ASTPrinter::visit(MemberAccess& _node)
{ {
writeLine("MemberAccess to member " + _node.getMemberName()); writeLine("MemberAccess to member " + _node.getMemberName());
printType(_node);
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }
@ -215,6 +221,7 @@ bool ASTPrinter::visit(MemberAccess& _node)
bool ASTPrinter::visit(IndexAccess& _node) bool ASTPrinter::visit(IndexAccess& _node)
{ {
writeLine("IndexAccess"); writeLine("IndexAccess");
printType(_node);
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }
@ -222,6 +229,7 @@ bool ASTPrinter::visit(IndexAccess& _node)
bool ASTPrinter::visit(PrimaryExpression& _node) bool ASTPrinter::visit(PrimaryExpression& _node)
{ {
writeLine("PrimaryExpression"); writeLine("PrimaryExpression");
printType(_node);
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }
@ -229,6 +237,7 @@ bool ASTPrinter::visit(PrimaryExpression& _node)
bool ASTPrinter::visit(Identifier& _node) bool ASTPrinter::visit(Identifier& _node)
{ {
writeLine(std::string("Identifier ") + _node.getName()); writeLine(std::string("Identifier ") + _node.getName());
printType(_node);
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }
@ -236,6 +245,7 @@ bool ASTPrinter::visit(Identifier& _node)
bool ASTPrinter::visit(ElementaryTypeNameExpression& _node) bool ASTPrinter::visit(ElementaryTypeNameExpression& _node)
{ {
writeLine(std::string("ElementaryTypeNameExpression ") + Token::toString(_node.getTypeToken())); writeLine(std::string("ElementaryTypeNameExpression ") + Token::toString(_node.getTypeToken()));
printType(_node);
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }
@ -246,6 +256,7 @@ bool ASTPrinter::visit(Literal& _node)
if (tokenString == nullptr) if (tokenString == nullptr)
tokenString = "[no token]"; tokenString = "[no token]";
writeLine(std::string("Literal, token: ") + tokenString + " value: " + _node.getValue()); writeLine(std::string("Literal, token: ") + tokenString + " value: " + _node.getValue());
printType(_node);
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }
@ -410,6 +421,14 @@ void ASTPrinter::printSourcePart(ASTNode const& _node)
} }
} }
void ASTPrinter::printType(Expression const& _expression)
{
if (_expression.getType())
*m_ostream << getIndentation() << " Type: " << _expression.getType()->toString() << "\n";
else
*m_ostream << getIndentation() << " Type unknown.\n";
}
std::string ASTPrinter::getIndentation() const std::string ASTPrinter::getIndentation() const
{ {
return std::string(m_indentation * 2, ' '); return std::string(m_indentation * 2, ' ');

View File

@ -102,6 +102,7 @@ public:
private: private:
void printSourcePart(ASTNode const& _node); void printSourcePart(ASTNode const& _node);
void printType(Expression const& _expression);
std::string getIndentation() const; std::string getIndentation() const;
void writeLine(std::string const& _line); void writeLine(std::string const& _line);
bool goDeeper() { m_indentation++; return true; } bool goDeeper() { m_indentation++; return true; }

View File

@ -22,6 +22,7 @@
#include <libsolidity/Types.h> #include <libsolidity/Types.h>
#include <libsolidity/AST.h> #include <libsolidity/AST.h>
#include <libdevcore/CommonIO.h>
namespace dev namespace dev
{ {
@ -130,6 +131,14 @@ bool IntegerType::acceptsUnaryOperator(Token::Value _operator) const
return _operator == Token::DELETE || (!isAddress() && _operator == Token::BIT_NOT); return _operator == Token::DELETE || (!isAddress() && _operator == Token::BIT_NOT);
} }
std::string IntegerType::toString() const
{
if (isAddress())
return "address";
std::string prefix = isHash() ? "hash" : (isSigned() ? "int" : "uint");
return prefix + dev::toString(m_bits);
}
bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const
{ {
// conversion to integer is fine, but not to address // conversion to integer is fine, but not to address

31
Types.h
View File

@ -59,6 +59,8 @@ public:
} }
virtual bool acceptsBinaryOperator(Token::Value) const { return false; } virtual bool acceptsBinaryOperator(Token::Value) const { return false; }
virtual bool acceptsUnaryOperator(Token::Value) const { return false; } virtual bool acceptsUnaryOperator(Token::Value) const { return false; }
virtual std::string toString() const = 0;
}; };
class IntegerType: public Type class IntegerType: public Type
@ -68,7 +70,7 @@ public:
{ {
UNSIGNED, SIGNED, HASH, ADDRESS UNSIGNED, SIGNED, HASH, ADDRESS
}; };
virtual Category getCategory() const { return Category::INTEGER; } virtual Category getCategory() const override { return Category::INTEGER; }
static std::shared_ptr<IntegerType> smallestTypeForLiteral(std::string const& _literal); static std::shared_ptr<IntegerType> smallestTypeForLiteral(std::string const& _literal);
@ -79,6 +81,8 @@ public:
virtual bool acceptsBinaryOperator(Token::Value _operator) const override; virtual bool acceptsBinaryOperator(Token::Value _operator) const override;
virtual bool acceptsUnaryOperator(Token::Value _operator) const override; virtual bool acceptsUnaryOperator(Token::Value _operator) const override;
virtual std::string toString() const override;
int getNumBits() const { return m_bits; } int getNumBits() const { return m_bits; }
bool isHash() const { return m_modifier == Modifier::HASH || m_modifier == Modifier::ADDRESS; } bool isHash() const { return m_modifier == Modifier::HASH || m_modifier == Modifier::ADDRESS; }
bool isAddress() const { return m_modifier == Modifier::ADDRESS; } bool isAddress() const { return m_modifier == Modifier::ADDRESS; }
@ -106,15 +110,18 @@ public:
{ {
return _operator == Token::NOT || _operator == Token::DELETE; return _operator == Token::NOT || _operator == Token::DELETE;
} }
virtual std::string toString() const override { return "bool"; }
}; };
class ContractType: public Type class ContractType: public Type
{ {
public: public:
virtual Category getCategory() const { return Category::CONTRACT; } virtual Category getCategory() const override { return Category::CONTRACT; }
ContractType(ContractDefinition const& _contract): m_contract(_contract) {} ContractType(ContractDefinition const& _contract): m_contract(_contract) {}
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const; virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const;
virtual std::string toString() const override { return "contract{...}"; }
private: private:
ContractDefinition const& m_contract; ContractDefinition const& m_contract;
}; };
@ -122,7 +129,7 @@ private:
class StructType: public Type class StructType: public Type
{ {
public: public:
virtual Category getCategory() const { return Category::STRUCT; } virtual Category getCategory() const override { return Category::STRUCT; }
StructType(StructDefinition const& _struct): m_struct(_struct) {} StructType(StructDefinition const& _struct): m_struct(_struct) {}
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const; virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const;
virtual bool acceptsUnaryOperator(Token::Value _operator) const override virtual bool acceptsUnaryOperator(Token::Value _operator) const override
@ -130,6 +137,9 @@ public:
return _operator == Token::DELETE; return _operator == Token::DELETE;
} }
virtual std::string toString() const override { return "struct{...}"; }
private: private:
StructDefinition const& m_struct; StructDefinition const& m_struct;
}; };
@ -137,11 +147,13 @@ private:
class FunctionType: public Type class FunctionType: public Type
{ {
public: public:
virtual Category getCategory() const { return Category::FUNCTION; } virtual Category getCategory() const override { return Category::FUNCTION; }
FunctionType(FunctionDefinition const& _function): m_function(_function) {} FunctionType(FunctionDefinition const& _function): m_function(_function) {}
FunctionDefinition const& getFunction() const { return m_function; } FunctionDefinition const& getFunction() const { return m_function; }
virtual std::string toString() const override { return "function(...)returns(...)"; }
private: private:
FunctionDefinition const& m_function; FunctionDefinition const& m_function;
}; };
@ -149,8 +161,10 @@ private:
class MappingType: public Type class MappingType: public Type
{ {
public: public:
virtual Category getCategory() const { return Category::MAPPING; } virtual Category getCategory() const override { return Category::MAPPING; }
MappingType() {} MappingType() {}
virtual std::string toString() const override { return "mapping(...=>...)"; }
private: private:
//@todo //@todo
}; };
@ -159,18 +173,21 @@ private:
class VoidType: public Type class VoidType: public Type
{ {
public: public:
virtual Category getCategory() const { return Category::VOID; } virtual Category getCategory() const override { return Category::VOID; }
VoidType() {} VoidType() {}
virtual std::string toString() const override { return "void"; }
}; };
class TypeType: public Type class TypeType: public Type
{ {
public: public:
virtual Category getCategory() const { return Category::TYPE; } virtual Category getCategory() const override { return Category::TYPE; }
TypeType(std::shared_ptr<Type const> const& _actualType): m_actualType(_actualType) {} TypeType(std::shared_ptr<Type const> const& _actualType): m_actualType(_actualType) {}
std::shared_ptr<Type const> const& getActualType() const { return m_actualType; } std::shared_ptr<Type const> const& getActualType() const { return m_actualType; }
virtual std::string toString() const override { return "type(" + m_actualType->toString() + ")"; }
private: private:
std::shared_ptr<Type const> m_actualType; std::shared_ptr<Type const> m_actualType;
}; };