Add type determination for as expression.

This CL adds the as expression type determination.

Bug: tint:5
Change-Id: I84a06df67844c75112c745f65a2c8102f8677f62
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18843
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-04-07 12:57:52 +00:00 committed by dan sinclair
parent 973bd6a7b4
commit a01777c2d9
3 changed files with 22 additions and 0 deletions

View File

@ -17,6 +17,7 @@
#include <memory> #include <memory>
#include "src/ast/array_accessor_expression.h" #include "src/ast/array_accessor_expression.h"
#include "src/ast/as_expression.h"
#include "src/ast/assignment_statement.h" #include "src/ast/assignment_statement.h"
#include "src/ast/break_statement.h" #include "src/ast/break_statement.h"
#include "src/ast/case_statement.h" #include "src/ast/case_statement.h"
@ -183,6 +184,9 @@ bool TypeDeterminer::DetermineResultType(ast::Expression* expr) {
if (expr->IsArrayAccessor()) { if (expr->IsArrayAccessor()) {
return DetermineArrayAccessor(expr->AsArrayAccessor()); return DetermineArrayAccessor(expr->AsArrayAccessor());
} }
if (expr->IsAs()) {
return DetermineAs(expr->AsAs());
}
if (expr->IsConstructor()) { if (expr->IsConstructor()) {
return DetermineConstructor(expr->AsConstructor()); return DetermineConstructor(expr->AsConstructor());
} }
@ -215,6 +219,11 @@ bool TypeDeterminer::DetermineArrayAccessor(
return true; return true;
} }
bool TypeDeterminer::DetermineAs(ast::AsExpression* expr) {
expr->set_result_type(expr->type());
return true;
}
bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) { bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) {
if (expr->IsTypeConstructor()) { if (expr->IsTypeConstructor()) {
expr->set_result_type(expr->AsTypeConstructor()->type()); expr->set_result_type(expr->AsTypeConstructor()->type());

View File

@ -26,6 +26,7 @@ namespace tint {
namespace ast { namespace ast {
class ArrayAccessorExpression; class ArrayAccessorExpression;
class AsExpression;
class ConstructorExpression; class ConstructorExpression;
class IdentifierExpression; class IdentifierExpression;
class Function; class Function;
@ -71,6 +72,7 @@ class TypeDeterminer {
private: private:
bool DetermineArrayAccessor(ast::ArrayAccessorExpression* expr); bool DetermineArrayAccessor(ast::ArrayAccessorExpression* expr);
bool DetermineAs(ast::AsExpression* expr);
bool DetermineConstructor(ast::ConstructorExpression* expr); bool DetermineConstructor(ast::ConstructorExpression* expr);
bool DetermineIdentifier(ast::IdentifierExpression* expr); bool DetermineIdentifier(ast::IdentifierExpression* expr);
Context& ctx_; Context& ctx_;

View File

@ -19,6 +19,7 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "src/ast/array_accessor_expression.h" #include "src/ast/array_accessor_expression.h"
#include "src/ast/as_expression.h"
#include "src/ast/assignment_statement.h" #include "src/ast/assignment_statement.h"
#include "src/ast/break_statement.h" #include "src/ast/break_statement.h"
#include "src/ast/case_statement.h" #include "src/ast/case_statement.h"
@ -478,6 +479,16 @@ TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Vector) {
EXPECT_TRUE(acc.result_type()->IsF32()); EXPECT_TRUE(acc.result_type()->IsF32());
} }
TEST_F(TypeDeterminerTest, Expr_As) {
ast::type::F32Type f32;
ast::AsExpression as(&f32,
std::make_unique<ast::IdentifierExpression>("name"));
EXPECT_TRUE(td()->DetermineResultType(&as));
ASSERT_NE(as.result_type(), nullptr);
EXPECT_TRUE(as.result_type()->IsF32());
}
TEST_F(TypeDeterminerTest, Expr_Constructor_Scalar) { TEST_F(TypeDeterminerTest, Expr_Constructor_Scalar) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::ScalarConstructorExpression s( ast::ScalarConstructorExpression s(