Add type determination for return statement.

This CL adds the type determination code for the return statements.

Bug: tint:5
Change-Id: I0300fe0c66e7b8c25d1dc0ed22aef8cc469567be
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18837
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-04-07 12:56:24 +00:00 committed by dan sinclair
parent 45540b9dbd
commit bf0fff8438
2 changed files with 20 additions and 0 deletions

View File

@ -22,6 +22,7 @@
#include "src/ast/if_statement.h"
#include "src/ast/loop_statement.h"
#include "src/ast/regardless_statement.h"
#include "src/ast/return_statement.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/type_constructor_expression.h"
@ -129,6 +130,10 @@ bool TypeDeterminer::DetermineResultType(ast::Statement* stmt) {
return DetermineResultType(r->condition()) &&
DetermineResultType(r->body());
}
if (stmt->IsReturn()) {
auto r = stmt->AsReturn();
return DetermineResultType(r->value());
}
if (stmt->IsNop()) {
return true;
}

View File

@ -28,6 +28,7 @@
#include "src/ast/int_literal.h"
#include "src/ast/loop_statement.h"
#include "src/ast/regardless_statement.h"
#include "src/ast/return_statement.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
@ -278,6 +279,20 @@ TEST_F(TypeDeterminerTest, Stmt_Regardless) {
EXPECT_TRUE(rhs_ptr->result_type()->IsF32());
}
TEST_F(TypeDeterminerTest, Stmt_Return) {
ast::type::I32Type i32;
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::IntLiteral>(&i32, 2));
auto cond_ptr = cond.get();
ast::ReturnStatement ret(std::move(cond));
EXPECT_TRUE(td()->DetermineResultType(&ret));
ASSERT_NE(cond_ptr->result_type(), nullptr);
EXPECT_TRUE(cond_ptr->result_type()->IsI32());
}
TEST_F(TypeDeterminerTest, Expr_Constructor_Scalar) {
ast::type::F32Type f32;
ast::ScalarConstructorExpression s(