Determine types for module scoped variable constructors.

This CL updates the type determination code to determine the types of
module scoped variable constructors.

Bug: tint:89
Change-Id: Icd5d65409fcf17a0cbf0b34b9919f8d9e1577354
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23220
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-06-15 20:55:09 +00:00
parent c7d1ac3361
commit 7be237aa8f
2 changed files with 21 additions and 0 deletions

View File

@ -65,6 +65,12 @@ void TypeDeterminer::set_error(const Source& src, const std::string& msg) {
bool TypeDeterminer::Determine() {
for (const auto& var : mod_->global_variables()) {
variable_stack_.set_global(var->name(), var.get());
if (var->has_constructor()) {
if (!DetermineResultType(var->constructor())) {
return false;
}
}
}
for (const auto& func : mod_->functions()) {

View File

@ -353,6 +353,21 @@ TEST_F(TypeDeterminerTest, Stmt_VariableDecl) {
EXPECT_TRUE(init_ptr->result_type()->IsI32());
}
TEST_F(TypeDeterminerTest, Stmt_VariableDecl_ModuleScope) {
ast::type::I32Type i32;
auto var =
std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, &i32);
var->set_constructor(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 2)));
auto* init_ptr = var->constructor();
mod()->AddGlobalVariable(std::move(var));
EXPECT_TRUE(td()->Determine());
ASSERT_NE(init_ptr->result_type(), nullptr);
EXPECT_TRUE(init_ptr->result_type()->IsI32());
}
TEST_F(TypeDeterminerTest, Expr_Error_Unknown) {
FakeExpr e;
e.set_source(Source{2, 30});