diff options
author | Lu Guanqun <guanqun.lu@gmail.com> | 2015-12-23 00:50:06 +0800 |
---|---|---|
committer | Lu Guanqun <guanqun.lu@gmail.com> | 2016-01-23 01:14:00 +0800 |
commit | 047172eb9a49df200b3a046751e90f52fc64999f (patch) | |
tree | 99006f4a58d722d65aa444bcd354bf3472d87be8 /libsolidity | |
parent | f1d21552fcf9b3f29c9d70b767810da05adeb011 (diff) | |
download | dexon-solidity-047172eb9a49df200b3a046751e90f52fc64999f.tar.gz dexon-solidity-047172eb9a49df200b3a046751e90f52fc64999f.tar.zst dexon-solidity-047172eb9a49df200b3a046751e90f52fc64999f.zip |
[cond-expr] add type checker
Diffstat (limited to 'libsolidity')
-rw-r--r-- | libsolidity/analysis/TypeChecker.cpp | 27 | ||||
-rw-r--r-- | libsolidity/analysis/TypeChecker.h | 1 |
2 files changed, 28 insertions, 0 deletions
diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 69357043..058e879e 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -744,6 +744,33 @@ void TypeChecker::endVisit(ExpressionStatement const& _statement) typeError(_statement.expression().location(), "Invalid integer constant."); } +void TypeChecker::endVisit(Conditional const& _conditional) +{ + TypePointer const& conditionType = type(_conditional.condition()); + if (!conditionType->isImplicitlyConvertibleTo(BoolType())) + typeError( + _conditional.location(), + "Conditional expression's type " + + conditionType->toString() + + " doesn't match bool type." + ); + + TypePointer const& trueType = type(_conditional.trueExpression()); + TypePointer const& falseType = type(_conditional.falseExpression()); + // we fake it as an equal operator, but any other comparison operator can work. + TypePointer commonType = trueType->binaryOperatorResult(Token::Equal, falseType); + if (!commonType) + typeError( + _conditional.location(), + "True expression's type " + + trueType->toString() + + " doesn't match false expression's type " + + falseType->toString() + + "." + ); + _conditional.annotation().type = commonType; +} + bool TypeChecker::visit(Assignment const& _assignment) { requireLValue(_assignment.leftHandSide()); diff --git a/libsolidity/analysis/TypeChecker.h b/libsolidity/analysis/TypeChecker.h index 7829a23d..ae96229b 100644 --- a/libsolidity/analysis/TypeChecker.h +++ b/libsolidity/analysis/TypeChecker.h @@ -90,6 +90,7 @@ private: virtual void endVisit(Return const& _return) override; virtual bool visit(VariableDeclarationStatement const& _variable) override; virtual void endVisit(ExpressionStatement const& _statement) override; + virtual void endVisit(Conditional const& _conditional) override; virtual bool visit(Assignment const& _assignment) override; virtual bool visit(TupleExpression const& _tuple) override; virtual void endVisit(BinaryOperation const& _operation) override; |