Add assignment statement type determination.

This CL adds type determination for the assignment statements.

Bug: tint:5
Change-Id: Ica0e59a459bfedc0c649d279f93a31b188c27736
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18828
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-04-07 12:47:23 +00:00 committed by dan sinclair
parent b7edc4c765
commit 6c498fc0f8
2 changed files with 31 additions and 1 deletions

View File

@ -14,6 +14,7 @@
#include "src/type_determiner.h"
#include "src/ast/assignment_statement.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/type_constructor_expression.h"
@ -69,7 +70,12 @@ bool TypeDeterminer::DetermineResultType(const ast::StatementList& stmts) {
return true;
}
bool TypeDeterminer::DetermineResultType(ast::Statement*) {
bool TypeDeterminer::DetermineResultType(ast::Statement* stmt) {
if (stmt->IsAssign()) {
auto a = stmt->AsAssign();
return DetermineResultType(a->lhs()) && DetermineResultType(a->rhs());
}
error_ = "unknown statement type for type determination";
return false;
}

View File

@ -18,10 +18,12 @@
#include <utility>
#include "gtest/gtest.h"
#include "src/ast/assignment_statement.h"
#include "src/ast/float_literal.h"
#include "src/ast/int_literal.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/vector_type.h"
#include "src/ast/type_constructor_expression.h"
@ -39,6 +41,28 @@ class TypeDeterminerTest : public testing::Test {
std::unique_ptr<TypeDeterminer> td_;
};
TEST_F(TypeDeterminerTest, Stmt_Assign) {
ast::type::F32Type f32;
ast::type::I32Type i32;
auto lhs = std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::IntLiteral>(&i32, 2));
auto lhs_ptr = lhs.get();
auto rhs = std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
auto rhs_ptr = rhs.get();
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
EXPECT_TRUE(td()->DetermineResultType(&assign));
ASSERT_NE(lhs_ptr->result_type(), nullptr);
ASSERT_NE(rhs_ptr->result_type(), nullptr);
EXPECT_TRUE(lhs_ptr->result_type()->IsI32());
EXPECT_TRUE(rhs_ptr->result_type()->IsF32());
}
TEST_F(TypeDeterminerTest, Expr_Constructor_Scalar) {
ast::type::F32Type f32;
ast::ScalarConstructorExpression s(