From 6c498fc0f8bd669bcde54edb3781a662a79e5fcd Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 7 Apr 2020 12:47:23 +0000 Subject: [PATCH] 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 --- src/type_determiner.cc | 8 +++++++- src/type_determiner_test.cc | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/type_determiner.cc b/src/type_determiner.cc index c911281991..87bf07a54c 100644 --- a/src/type_determiner.cc +++ b/src/type_determiner.cc @@ -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; } diff --git a/src/type_determiner_test.cc b/src/type_determiner_test.cc index 49a9c89d1d..471b9a1b1e 100644 --- a/src/type_determiner_test.cc +++ b/src/type_determiner_test.cc @@ -18,10 +18,12 @@ #include #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 td_; }; +TEST_F(TypeDeterminerTest, Stmt_Assign) { + ast::type::F32Type f32; + ast::type::I32Type i32; + + auto lhs = std::make_unique( + std::make_unique(&i32, 2)); + auto lhs_ptr = lhs.get(); + + auto rhs = std::make_unique( + std::make_unique(&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(