Add cast expression type determination.
This CL adds the type determination for cast expressions.
Bug: tint:5
Change-Id: I35ae28e3b70c554cd48c6abcd78d95c2ba7211fa
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18845
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/type_determiner.cc b/src/type_determiner.cc
index f974bb0..f1da09c 100644
--- a/src/type_determiner.cc
+++ b/src/type_determiner.cc
@@ -22,6 +22,7 @@
#include "src/ast/break_statement.h"
#include "src/ast/call_expression.h"
#include "src/ast/case_statement.h"
+#include "src/ast/cast_expression.h"
#include "src/ast/continue_statement.h"
#include "src/ast/else_statement.h"
#include "src/ast/identifier_expression.h"
@@ -197,6 +198,9 @@
if (expr->IsCall()) {
return DetermineCall(expr->AsCall());
}
+ if (expr->IsCast()) {
+ return DetermineCast(expr->AsCast());
+ }
if (expr->IsConstructor()) {
return DetermineConstructor(expr->AsConstructor());
}
@@ -245,6 +249,11 @@
return true;
}
+bool TypeDeterminer::DetermineCast(ast::CastExpression* expr) {
+ expr->set_result_type(expr->type());
+ return true;
+}
+
bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) {
if (expr->IsTypeConstructor()) {
expr->set_result_type(expr->AsTypeConstructor()->type());
diff --git a/src/type_determiner.h b/src/type_determiner.h
index 79862ec..692947e 100644
--- a/src/type_determiner.h
+++ b/src/type_determiner.h
@@ -28,6 +28,7 @@
class ArrayAccessorExpression;
class AsExpression;
class CallExpression;
+class CastExpression;
class ConstructorExpression;
class IdentifierExpression;
class Function;
@@ -79,6 +80,7 @@
bool DetermineArrayAccessor(ast::ArrayAccessorExpression* expr);
bool DetermineAs(ast::AsExpression* expr);
bool DetermineCall(ast::CallExpression* expr);
+ bool DetermineCast(ast::CastExpression* expr);
bool DetermineConstructor(ast::ConstructorExpression* expr);
bool DetermineIdentifier(ast::IdentifierExpression* expr);
Context& ctx_;
diff --git a/src/type_determiner_test.cc b/src/type_determiner_test.cc
index cd38670..7796ca7 100644
--- a/src/type_determiner_test.cc
+++ b/src/type_determiner_test.cc
@@ -24,6 +24,7 @@
#include "src/ast/break_statement.h"
#include "src/ast/call_expression.h"
#include "src/ast/case_statement.h"
+#include "src/ast/cast_expression.h"
#include "src/ast/continue_statement.h"
#include "src/ast/else_statement.h"
#include "src/ast/float_literal.h"
@@ -550,6 +551,16 @@
EXPECT_TRUE(b_ptr->result_type()->IsI32());
}
+TEST_F(TypeDeterminerTest, Expr_Cast) {
+ ast::type::F32Type f32;
+ ast::CastExpression cast(&f32,
+ std::make_unique<ast::IdentifierExpression>("name"));
+
+ EXPECT_TRUE(td()->DetermineResultType(&cast));
+ ASSERT_NE(cast.result_type(), nullptr);
+ EXPECT_TRUE(cast.result_type()->IsF32());
+}
+
TEST_F(TypeDeterminerTest, Expr_Constructor_Scalar) {
ast::type::F32Type f32;
ast::ScalarConstructorExpression s(