[cond-expr] add type checker

This commit is contained in:
Lu Guanqun 2015-12-22 16:50:06 +00:00
parent f1d21552fc
commit 047172eb9a
2 changed files with 28 additions and 0 deletions

View File

@ -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());

View File

@ -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;