blob: 862e1a2b85d854dcd3958ad945ae6ab75e7ddfab [file] [log] [blame]
// Copyright 2021 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "gmock/gmock.h"
#include "src/tint/lang/core/type/reference.h"
#include "src/tint/lang/core/type/texture_dimension.h"
#include "src/tint/lang/wgsl/resolver/resolver.h"
#include "src/tint/lang/wgsl/resolver/resolver_helper_test.h"
namespace tint::resolver {
namespace {
using namespace tint::core::fluent_types; // NOLINT
using namespace tint::core::number_suffixes; // NOLINT
struct ResolverVariableTest : public resolver::TestHelper, public testing::Test {};
////////////////////////////////////////////////////////////////////////////////////////////////////
// Function-scope 'var'
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(ResolverVariableTest, LocalVar_NoInitializer) {
// struct S { i : i32; }
// alias A = S;
// fn F(){
// var i : i32;
// var u : u32;
// var f : f32;
// var h : f16;
// var b : bool;
// var s : S;
// var a : A;
// }
Enable(wgsl::Extension::kF16);
auto* S = Structure("S", Vector{Member("i", ty.i32())});
auto* A = Alias("A", ty.Of(S));
auto* i = Var("i", ty.i32());
auto* u = Var("u", ty.u32());
auto* f = Var("f", ty.f32());
auto* h = Var("h", ty.f16());
auto* b = Var("b", ty.bool_());
auto* s = Var("s", ty.Of(S));
auto* a = Var("a", ty.Of(A));
Func("F", tint::Empty, ty.void_(),
Vector{
Decl(i),
Decl(u),
Decl(f),
Decl(h),
Decl(b),
Decl(s),
Decl(a),
});
ASSERT_TRUE(r()->Resolve()) << r()->error();
// `var` declarations are always of reference type
ASSERT_TRUE(TypeOf(i)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(u)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(f)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(h)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(b)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(s)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(a)->Is<core::type::Reference>());
EXPECT_TRUE(TypeOf(i)->As<core::type::Reference>()->StoreType()->Is<core::type::I32>());
EXPECT_TRUE(TypeOf(u)->As<core::type::Reference>()->StoreType()->Is<core::type::U32>());
EXPECT_TRUE(TypeOf(f)->As<core::type::Reference>()->StoreType()->Is<core::type::F32>());
EXPECT_TRUE(TypeOf(h)->As<core::type::Reference>()->StoreType()->Is<core::type::F16>());
EXPECT_TRUE(TypeOf(b)->As<core::type::Reference>()->StoreType()->Is<core::type::Bool>());
EXPECT_TRUE(TypeOf(s)->As<core::type::Reference>()->StoreType()->Is<core::type::Struct>());
EXPECT_TRUE(TypeOf(a)->As<core::type::Reference>()->StoreType()->Is<core::type::Struct>());
EXPECT_EQ(Sem().Get(i)->Initializer(), nullptr);
EXPECT_EQ(Sem().Get(u)->Initializer(), nullptr);
EXPECT_EQ(Sem().Get(f)->Initializer(), nullptr);
EXPECT_EQ(Sem().Get(h)->Initializer(), nullptr);
EXPECT_EQ(Sem().Get(b)->Initializer(), nullptr);
EXPECT_EQ(Sem().Get(s)->Initializer(), nullptr);
EXPECT_EQ(Sem().Get(a)->Initializer(), nullptr);
}
TEST_F(ResolverVariableTest, LocalVar_WithInitializer) {
// struct S { i : i32; }
// alias A = S;
// fn F(){
// var i : i32 = 1i;
// var u : u32 = 1u;
// var f : f32 = 1.f;
// var h : f16 = 1.h;
// var b : bool = true;
// var s : S = S(1);
// var a : A = A(1);
// }
Enable(wgsl::Extension::kF16);
auto* S = Structure("S", Vector{Member("i", ty.i32())});
auto* A = Alias("A", ty.Of(S));
auto* i_c = Expr(1_i);
auto* u_c = Expr(1_u);
auto* f_c = Expr(1_f);
auto* h_c = Expr(1_h);
auto* b_c = Expr(true);
auto* s_c = Call(ty.Of(S), Expr(1_i));
auto* a_c = Call(ty.Of(A), Expr(1_i));
auto* i = Var("i", ty.i32(), i_c);
auto* u = Var("u", ty.u32(), u_c);
auto* f = Var("f", ty.f32(), f_c);
auto* h = Var("h", ty.f16(), h_c);
auto* b = Var("b", ty.bool_(), b_c);
auto* s = Var("s", ty.Of(S), s_c);
auto* a = Var("a", ty.Of(A), a_c);
Func("F", tint::Empty, ty.void_(),
Vector{
Decl(i),
Decl(u),
Decl(f),
Decl(h),
Decl(b),
Decl(s),
Decl(a),
});
ASSERT_TRUE(r()->Resolve()) << r()->error();
// `var` declarations are always of reference type
ASSERT_TRUE(TypeOf(i)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(u)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(f)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(h)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(b)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(s)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(a)->Is<core::type::Reference>());
EXPECT_EQ(TypeOf(i)->As<core::type::Reference>()->Access(), core::Access::kReadWrite);
EXPECT_EQ(TypeOf(u)->As<core::type::Reference>()->Access(), core::Access::kReadWrite);
EXPECT_EQ(TypeOf(f)->As<core::type::Reference>()->Access(), core::Access::kReadWrite);
EXPECT_EQ(TypeOf(b)->As<core::type::Reference>()->Access(), core::Access::kReadWrite);
EXPECT_EQ(TypeOf(s)->As<core::type::Reference>()->Access(), core::Access::kReadWrite);
EXPECT_EQ(TypeOf(a)->As<core::type::Reference>()->Access(), core::Access::kReadWrite);
EXPECT_TRUE(TypeOf(i)->As<core::type::Reference>()->StoreType()->Is<core::type::I32>());
EXPECT_TRUE(TypeOf(u)->As<core::type::Reference>()->StoreType()->Is<core::type::U32>());
EXPECT_TRUE(TypeOf(f)->As<core::type::Reference>()->StoreType()->Is<core::type::F32>());
EXPECT_TRUE(TypeOf(h)->As<core::type::Reference>()->StoreType()->Is<core::type::F16>());
EXPECT_TRUE(TypeOf(b)->As<core::type::Reference>()->StoreType()->Is<core::type::Bool>());
EXPECT_TRUE(TypeOf(s)->As<core::type::Reference>()->StoreType()->Is<core::type::Struct>());
EXPECT_TRUE(TypeOf(a)->As<core::type::Reference>()->StoreType()->Is<core::type::Struct>());
EXPECT_EQ(Sem().Get(i)->Initializer()->Declaration(), i_c);
EXPECT_EQ(Sem().Get(u)->Initializer()->Declaration(), u_c);
EXPECT_EQ(Sem().Get(f)->Initializer()->Declaration(), f_c);
EXPECT_EQ(Sem().Get(h)->Initializer()->Declaration(), h_c);
EXPECT_EQ(Sem().Get(b)->Initializer()->Declaration(), b_c);
EXPECT_EQ(Sem().Get(s)->Initializer()->Declaration(), s_c);
EXPECT_EQ(Sem().Get(a)->Initializer()->Declaration(), a_c);
}
TEST_F(ResolverVariableTest, LocalVar_ShadowsAlias) {
// type a = i32;
//
// fn F() {
// var a = false;
// }
auto* t = Alias("a", ty.i32());
auto* v = Var("a", Expr(false));
Func("F", tint::Empty, ty.void_(), Vector{Decl(v)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* type_t = Sem().Get(t);
auto* local = Sem().Get<sem::LocalVariable>(v);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), type_t);
}
TEST_F(ResolverVariableTest, LocalVar_ShadowsStruct) {
// struct a {
// m : i32;
// };
//
// fn F() {
// var a = true;
// }
auto* t = Structure("a", Vector{Member("m", ty.i32())});
auto* v = Var("a", Expr(false));
Func("F", tint::Empty, ty.void_(), Vector{Decl(v)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* type_t = Sem().Get(t);
auto* local = Sem().Get<sem::LocalVariable>(v);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), type_t);
}
TEST_F(ResolverVariableTest, LocalVar_ShadowsFunction) {
// fn a() {
// var a = true;
// }
auto* v = Var("a", Expr(false));
auto* f = Func("a", tint::Empty, ty.void_(), Vector{Decl(v)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* func = Sem().Get(f);
ASSERT_NE(func, nullptr);
auto* local = Sem().Get<sem::LocalVariable>(v);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), func);
}
TEST_F(ResolverVariableTest, LocalVar_ShadowsGlobalVar) {
// var<private> a : i32;
//
// fn F() {
// var a = a;
// }
auto* g = GlobalVar("a", ty.i32(), core::AddressSpace::kPrivate);
auto* v = Var("a", Expr("a"));
Func("F", tint::Empty, ty.void_(), Vector{Decl(v)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* global = Sem().Get(g);
auto* local = Sem().Get<sem::LocalVariable>(v);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), global);
auto* user_v =
Sem().GetVal(local->Declaration()->initializer)->UnwrapLoad()->As<sem::VariableUser>();
ASSERT_NE(user_v, nullptr);
EXPECT_EQ(user_v->Variable(), global);
}
TEST_F(ResolverVariableTest, LocalVar_ShadowsGlobalConst) {
// const a : i32 = 1i;
//
// fn X() {
// var a = (a == 123);
// }
auto* g = GlobalConst("a", ty.i32(), Expr(1_i));
auto* v = Var("a", Expr("a"));
Func("F", tint::Empty, ty.void_(), Vector{Decl(v)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* global = Sem().Get(g);
auto* local = Sem().Get<sem::LocalVariable>(v);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), global);
auto* user_v = Sem().Get<sem::VariableUser>(local->Declaration()->initializer);
ASSERT_NE(user_v, nullptr);
EXPECT_EQ(user_v->Variable(), global);
}
TEST_F(ResolverVariableTest, LocalVar_ShadowsLocalVar) {
// fn F() {
// var a : i32 = 1i; // x
// {
// var a = a; // y
// }
// }
auto* x = Var("a", ty.i32(), Expr(1_i));
auto* y = Var("a", Expr("a"));
Func("F", tint::Empty, ty.void_(), Vector{Decl(x), Block(Decl(y))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* local_x = Sem().Get<sem::LocalVariable>(x);
auto* local_y = Sem().Get<sem::LocalVariable>(y);
ASSERT_NE(local_x, nullptr);
ASSERT_NE(local_y, nullptr);
EXPECT_EQ(local_y->Shadows(), local_x);
auto* user_y =
Sem().GetVal(local_y->Declaration()->initializer)->UnwrapLoad()->As<sem::VariableUser>();
ASSERT_NE(user_y, nullptr);
EXPECT_EQ(user_y->Variable(), local_x);
}
TEST_F(ResolverVariableTest, LocalVar_ShadowsLocalConst) {
// fn F() {
// const a : i32 = 1i;
// {
// var a = (a == 123);
// }
// }
auto* c = Const("a", ty.i32(), Expr(1_i));
auto* v = Var("a", Expr("a"));
Func("X", tint::Empty, ty.void_(), Vector{Decl(c), Block(Decl(v))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* local_c = Sem().Get<sem::LocalVariable>(c);
auto* local_v = Sem().Get<sem::LocalVariable>(v);
ASSERT_NE(local_c, nullptr);
ASSERT_NE(local_v, nullptr);
EXPECT_EQ(local_v->Shadows(), local_c);
auto* user_v = Sem().Get<sem::VariableUser>(local_v->Declaration()->initializer);
ASSERT_NE(user_v, nullptr);
EXPECT_EQ(user_v->Variable(), local_c);
}
TEST_F(ResolverVariableTest, LocalVar_ShadowsLocalLet) {
// fn F() {
// let a : i32 = 1i;
// {
// var a = (a == 123);
// }
// }
auto* l = Let("a", ty.i32(), Expr(1_i));
auto* v = Var("a", Expr("a"));
Func("X", tint::Empty, ty.void_(), Vector{Decl(l), Block(Decl(v))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* local_l = Sem().Get<sem::LocalVariable>(l);
auto* local_v = Sem().Get<sem::LocalVariable>(v);
ASSERT_NE(local_l, nullptr);
ASSERT_NE(local_v, nullptr);
EXPECT_EQ(local_v->Shadows(), local_l);
auto* user_v = Sem().Get<sem::VariableUser>(local_v->Declaration()->initializer);
ASSERT_NE(user_v, nullptr);
EXPECT_EQ(user_v->Variable(), local_l);
}
TEST_F(ResolverVariableTest, LocalVar_ShadowsParam) {
// fn F(a : i32) {
// {
// var a = a;
// }
// }
auto* p = Param("a", ty.i32());
auto* v = Var("a", Expr("a"));
Func("X", Vector{p}, ty.void_(), Vector{Block(Decl(v))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* param = Sem().Get(p);
auto* local = Sem().Get<sem::LocalVariable>(v);
ASSERT_NE(param, nullptr);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), param);
auto* user_v = Sem().Get<sem::VariableUser>(local->Declaration()->initializer);
ASSERT_NE(user_v, nullptr);
EXPECT_EQ(user_v->Variable(), param);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// 'let' declaration
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(ResolverVariableTest, LocalLet) {
// struct S { i : i32; }
// fn F(){
// var v : i32;
// let i : i32 = 1i;
// let u : u32 = 1u;
// let f : f32 = 1.f;
// let h : h32 = 1.h;
// let b : bool = true;
// let s : S = S(1);
// let a : A = A(1);
// let p : pointer<function, i32> = &v;
// }
Enable(wgsl::Extension::kF16);
auto* S = Structure("S", Vector{Member("i", ty.i32())});
auto* A = Alias("A", ty.Of(S));
auto* v = Var("v", ty.i32());
auto* i_c = Expr(1_i);
auto* u_c = Expr(1_u);
auto* f_c = Expr(1_f);
auto* h_c = Expr(1_h);
auto* b_c = Expr(true);
auto* s_c = Call(ty.Of(S), Expr(1_i));
auto* a_c = Call(ty.Of(A), Expr(1_i));
auto* p_c = AddressOf(v);
auto* i = Let("i", ty.i32(), i_c);
auto* u = Let("u", ty.u32(), u_c);
auto* f = Let("f", ty.f32(), f_c);
auto* h = Let("h", ty.f16(), h_c);
auto* b = Let("b", ty.bool_(), b_c);
auto* s = Let("s", ty.Of(S), s_c);
auto* a = Let("a", ty.Of(A), a_c);
auto* p = Let("p", ty.ptr<function, i32>(), p_c);
Func("F", tint::Empty, ty.void_(),
Vector{
Decl(v),
Decl(i),
Decl(u),
Decl(f),
Decl(h),
Decl(b),
Decl(s),
Decl(a),
Decl(p),
});
ASSERT_TRUE(r()->Resolve()) << r()->error();
// `let` declarations are always of the storage type
ASSERT_TRUE(TypeOf(i)->Is<core::type::I32>());
ASSERT_TRUE(TypeOf(u)->Is<core::type::U32>());
ASSERT_TRUE(TypeOf(f)->Is<core::type::F32>());
ASSERT_TRUE(TypeOf(h)->Is<core::type::F16>());
ASSERT_TRUE(TypeOf(b)->Is<core::type::Bool>());
ASSERT_TRUE(TypeOf(s)->Is<core::type::Struct>());
ASSERT_TRUE(TypeOf(a)->Is<core::type::Struct>());
ASSERT_TRUE(TypeOf(p)->Is<core::type::Pointer>());
ASSERT_TRUE(TypeOf(p)->As<core::type::Pointer>()->StoreType()->Is<core::type::I32>());
EXPECT_EQ(Sem().Get(i)->Initializer()->Declaration(), i_c);
EXPECT_EQ(Sem().Get(u)->Initializer()->Declaration(), u_c);
EXPECT_EQ(Sem().Get(f)->Initializer()->Declaration(), f_c);
EXPECT_EQ(Sem().Get(h)->Initializer()->Declaration(), h_c);
EXPECT_EQ(Sem().Get(b)->Initializer()->Declaration(), b_c);
EXPECT_EQ(Sem().Get(s)->Initializer()->Declaration(), s_c);
EXPECT_EQ(Sem().Get(a)->Initializer()->Declaration(), a_c);
EXPECT_EQ(Sem().Get(p)->Initializer()->Declaration(), p_c);
}
TEST_F(ResolverVariableTest, LocalLet_InheritsAccessFromOriginatingVariable) {
// struct Inner {
// arr: array<i32, 4>;
// }
// struct S {
// inner: Inner;
// }
// @group(0) @binding(0) var<storage, read_write> s : S;
// fn f() {
// let p = &s.inner.arr[3];
// }
auto* inner = Structure("Inner", Vector{Member("arr", ty.array<i32, 4>())});
auto* buf = Structure("S", Vector{Member("inner", ty.Of(inner))});
auto* storage = GlobalVar("s", ty.Of(buf), core::AddressSpace::kStorage,
core::Access::kReadWrite, Binding(0_a), Group(0_a));
auto* expr = IndexAccessor(MemberAccessor(MemberAccessor(storage, "inner"), "arr"), 3_i);
auto* ptr = Let("p", AddressOf(expr));
WrapInFunction(ptr);
ASSERT_TRUE(r()->Resolve()) << r()->error();
ASSERT_TRUE(TypeOf(expr)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(ptr)->Is<core::type::Pointer>());
EXPECT_EQ(TypeOf(expr)->As<core::type::Reference>()->Access(), core::Access::kReadWrite);
EXPECT_EQ(TypeOf(ptr)->As<core::type::Pointer>()->Access(), core::Access::kReadWrite);
}
TEST_F(ResolverVariableTest, LocalLet_ShadowsAlias) {
// type a = i32;
//
// fn F() {
// let a = true;
// }
auto* t = Alias("a", ty.i32());
auto* l = Let("a", Expr(false));
Func("F", tint::Empty, ty.void_(), Vector{Decl(l)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* type_t = Sem().Get(t);
auto* local = Sem().Get<sem::LocalVariable>(l);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), type_t);
}
TEST_F(ResolverVariableTest, LocalLet_ShadowsStruct) {
// struct a {
// m : i32;
// };
//
// fn F() {
// let a = false;
// }
auto* t = Structure("a", Vector{Member("m", ty.i32())});
auto* l = Let("a", Expr(false));
Func("F", tint::Empty, ty.void_(), Vector{Decl(l)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* type_t = Sem().Get(t);
auto* local = Sem().Get<sem::LocalVariable>(l);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), type_t);
}
TEST_F(ResolverVariableTest, LocalLet_ShadowsFunction) {
// fn a() {
// let a = false;
// }
auto* l = Let("a", Expr(false));
auto* fb = Func("a", tint::Empty, ty.void_(), Vector{Decl(l)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* func = Sem().Get(fb);
ASSERT_NE(func, nullptr);
auto* local = Sem().Get<sem::LocalVariable>(l);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), func);
}
TEST_F(ResolverVariableTest, LocalLet_ShadowsGlobalVar) {
// var<private> a : i32;
//
// fn F() {
// let a = a;
// }
auto* g = GlobalVar("a", ty.i32(), core::AddressSpace::kPrivate);
auto* l = Let("a", Expr("a"));
Func("F", tint::Empty, ty.void_(), Vector{Decl(l)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* global = Sem().Get(g);
auto* local = Sem().Get<sem::LocalVariable>(l);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), global);
auto* user =
Sem().GetVal(local->Declaration()->initializer)->UnwrapLoad()->As<sem::VariableUser>();
ASSERT_NE(user, nullptr);
EXPECT_EQ(user->Variable(), global);
}
TEST_F(ResolverVariableTest, LocalLet_ShadowsGlobalConst) {
// const a : i32 = 1i;
//
// fn F() {
// let a = a;
// }
auto* g = GlobalConst("a", ty.i32(), Expr(1_i));
auto* l = Let("a", Expr("a"));
Func("F", tint::Empty, ty.void_(), Vector{Decl(l)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* global = Sem().Get(g);
auto* local = Sem().Get<sem::LocalVariable>(l);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), global);
auto* user = Sem().Get<sem::VariableUser>(local->Declaration()->initializer);
ASSERT_NE(user, nullptr);
EXPECT_EQ(user->Variable(), global);
}
TEST_F(ResolverVariableTest, LocalLet_ShadowsLocalVar) {
// fn F() {
// var a : i32 = 1i;
// {
// let a = a;
// }
// }
auto* v = Var("a", ty.i32(), Expr(1_i));
auto* l = Let("a", Expr("a"));
Func("F", tint::Empty, ty.void_(), Vector{Decl(v), Block(Decl(l))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* local_v = Sem().Get<sem::LocalVariable>(v);
auto* local_l = Sem().Get<sem::LocalVariable>(l);
ASSERT_NE(local_v, nullptr);
ASSERT_NE(local_l, nullptr);
EXPECT_EQ(local_l->Shadows(), local_v);
auto* user =
Sem().GetVal(local_l->Declaration()->initializer)->UnwrapLoad()->As<sem::VariableUser>();
ASSERT_NE(user, nullptr);
EXPECT_EQ(user->Variable(), local_v);
}
TEST_F(ResolverVariableTest, LocalLet_ShadowsLocalConst) {
// fn X() {
// const a : i32 = 1i; // x
// {
// let a = a; // y
// }
// }
auto* x = Const("a", ty.i32(), Expr(1_i));
auto* y = Let("a", Expr("a"));
Func("X", tint::Empty, ty.void_(), Vector{Decl(x), Block(Decl(y))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* local_x = Sem().Get<sem::LocalVariable>(x);
auto* local_y = Sem().Get<sem::LocalVariable>(y);
ASSERT_NE(local_x, nullptr);
ASSERT_NE(local_y, nullptr);
EXPECT_EQ(local_y->Shadows(), local_x);
auto* user = Sem().Get<sem::VariableUser>(local_y->Declaration()->initializer);
ASSERT_NE(user, nullptr);
EXPECT_EQ(user->Variable(), local_x);
}
TEST_F(ResolverVariableTest, LocalLet_ShadowsLocalLet) {
// fn X() {
// let a : i32 = 1i; // x
// {
// let a = a; // y
// }
// }
auto* x = Let("a", ty.i32(), Expr(1_i));
auto* y = Let("a", Expr("a"));
Func("X", tint::Empty, ty.void_(), Vector{Decl(x), Block(Decl(y))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* local_x = Sem().Get<sem::LocalVariable>(x);
auto* local_y = Sem().Get<sem::LocalVariable>(y);
ASSERT_NE(local_x, nullptr);
ASSERT_NE(local_y, nullptr);
EXPECT_EQ(local_y->Shadows(), local_x);
auto* user = Sem().Get<sem::VariableUser>(local_y->Declaration()->initializer);
ASSERT_NE(user, nullptr);
EXPECT_EQ(user->Variable(), local_x);
}
TEST_F(ResolverVariableTest, LocalLet_ShadowsParam) {
// fn F(a : i32) {
// {
// let a = a;
// }
// }
auto* p = Param("a", ty.i32());
auto* l = Let("a", Expr("a"));
Func("X", Vector{p}, ty.void_(), Vector{Block(Decl(l))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* param = Sem().Get(p);
auto* local = Sem().Get<sem::LocalVariable>(l);
ASSERT_NE(param, nullptr);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), param);
auto* user = Sem().Get<sem::VariableUser>(local->Declaration()->initializer);
ASSERT_NE(user, nullptr);
EXPECT_EQ(user->Variable(), param);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Function-scope const
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(ResolverVariableTest, LocalConst_ShadowsAlias) {
// type a = i32;
//
// fn F() {
// const a = true;
// }
auto* t = Alias("a", ty.i32());
auto* c = Const("a", Expr(false));
Func("F", tint::Empty, ty.void_(), Vector{Decl(c)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* type_t = Sem().Get(t);
auto* local = Sem().Get<sem::LocalVariable>(c);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), type_t);
}
TEST_F(ResolverVariableTest, LocalConst_ShadowsStruct) {
// struct a {
// m : i32;
// };
//
// fn F() {
// const a = false;
// }
auto* t = Structure("a", Vector{Member("m", ty.i32())});
auto* c = Const("a", Expr(false));
Func("F", tint::Empty, ty.void_(), Vector{Decl(c)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* type_t = Sem().Get(t);
auto* local = Sem().Get<sem::LocalVariable>(c);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), type_t);
}
TEST_F(ResolverVariableTest, LocalConst_ShadowsFunction) {
// fn a() {
// const a = false;
// }
auto* c = Const("a", Expr(false));
auto* fb = Func("a", tint::Empty, ty.void_(), Vector{Decl(c)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* func = Sem().Get(fb);
ASSERT_NE(func, nullptr);
auto* local = Sem().Get<sem::LocalVariable>(c);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), func);
}
TEST_F(ResolverVariableTest, LocalConst_ShadowsGlobalVar) {
// var<private> a : i32;
//
// fn F() {
// const a = 1i;
// }
auto* g = GlobalVar("a", ty.i32(), core::AddressSpace::kPrivate);
auto* c = Const("a", Expr(1_i));
Func("F", tint::Empty, ty.void_(), Vector{Decl(c)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* global = Sem().Get(g);
auto* local = Sem().Get<sem::LocalVariable>(c);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), global);
}
TEST_F(ResolverVariableTest, LocalConst_ShadowsGlobalConst) {
// const a : i32 = 1i;
//
// fn F() {
// const a = a;
// }
auto* g = GlobalConst("a", ty.i32(), Expr(1_i));
auto* c = Const("a", Expr("a"));
Func("F", tint::Empty, ty.void_(), Vector{Decl(c)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* global = Sem().Get(g);
auto* local = Sem().Get<sem::LocalVariable>(c);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), global);
auto* user = Sem().Get<sem::VariableUser>(local->Declaration()->initializer);
ASSERT_NE(user, nullptr);
EXPECT_EQ(user->Variable(), global);
}
TEST_F(ResolverVariableTest, LocalConst_ShadowsLocalVar) {
// fn F() {
// var a = 1i;
// {
// const a = 1i;
// }
// }
auto* v = Var("a", ty.i32(), Expr(1_i));
auto* c = Const("a", Expr(1_i));
Func("F", tint::Empty, ty.void_(), Vector{Decl(v), Block(Decl(c))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* local_v = Sem().Get<sem::LocalVariable>(v);
auto* local_c = Sem().Get<sem::LocalVariable>(c);
ASSERT_NE(local_v, nullptr);
ASSERT_NE(local_c, nullptr);
EXPECT_EQ(local_c->Shadows(), local_v);
}
TEST_F(ResolverVariableTest, LocalConst_ShadowsLocalConst) {
// fn X() {
// const a = 1i; // x
// {
// const a = a; // y
// }
// }
auto* x = Const("a", ty.i32(), Expr(1_i));
auto* y = Const("a", Expr("a"));
Func("X", tint::Empty, ty.void_(), Vector{Decl(x), Block(Decl(y))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* local_x = Sem().Get<sem::LocalVariable>(x);
auto* local_y = Sem().Get<sem::LocalVariable>(y);
ASSERT_NE(local_x, nullptr);
ASSERT_NE(local_y, nullptr);
EXPECT_EQ(local_y->Shadows(), local_x);
auto* user = Sem().Get<sem::VariableUser>(local_y->Declaration()->initializer);
ASSERT_NE(user, nullptr);
EXPECT_EQ(user->Variable(), local_x);
}
TEST_F(ResolverVariableTest, LocalConst_ShadowsLocalLet) {
// fn X() {
// let a = 1i; // x
// {
// const a = 1i; // y
// }
// }
auto* l = Let("a", ty.i32(), Expr(1_i));
auto* c = Const("a", Expr(1_i));
Func("X", tint::Empty, ty.void_(), Vector{Decl(l), Block(Decl(c))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* local_l = Sem().Get<sem::LocalVariable>(l);
auto* local_c = Sem().Get<sem::LocalVariable>(c);
ASSERT_NE(local_l, nullptr);
ASSERT_NE(local_c, nullptr);
EXPECT_EQ(local_c->Shadows(), local_l);
}
TEST_F(ResolverVariableTest, LocalConst_ShadowsParam) {
// fn F(a : i32) {
// {
// const a = 1i;
// }
// }
auto* p = Param("a", ty.i32());
auto* c = Const("a", Expr(1_i));
Func("X", Vector{p}, ty.void_(), Vector{Block(Decl(c))});
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* param = Sem().Get(p);
auto* local = Sem().Get<sem::LocalVariable>(c);
ASSERT_NE(param, nullptr);
ASSERT_NE(local, nullptr);
EXPECT_EQ(local->Shadows(), param);
}
TEST_F(ResolverVariableTest, LocalConst_ExplicitType_Decls) {
Structure("S", Vector{Member("m", ty.u32())});
auto* c_i32 = Const("a", ty.i32(), Expr(0_i));
auto* c_u32 = Const("b", ty.u32(), Expr(0_u));
auto* c_f32 = Const("c", ty.f32(), Expr(0_f));
auto* c_vi32 = Const("d", ty.vec3<i32>(), Call<vec3<i32>>());
auto* c_vu32 = Const("e", ty.vec3<u32>(), Call<vec3<u32>>());
auto* c_vf32 = Const("f", ty.vec3<f32>(), Call<vec3<f32>>());
auto* c_mf32 = Const("g", ty.mat3x3<f32>(), Call<mat3x3<f32>>());
auto* c_s = Const("h", ty("S"), Call("S"));
WrapInFunction(c_i32, c_u32, c_f32, c_vi32, c_vu32, c_vf32, c_mf32, c_s);
ASSERT_TRUE(r()->Resolve()) << r()->error();
EXPECT_EQ(Sem().Get(c_i32)->Declaration(), c_i32);
EXPECT_EQ(Sem().Get(c_u32)->Declaration(), c_u32);
EXPECT_EQ(Sem().Get(c_f32)->Declaration(), c_f32);
EXPECT_EQ(Sem().Get(c_vi32)->Declaration(), c_vi32);
EXPECT_EQ(Sem().Get(c_vu32)->Declaration(), c_vu32);
EXPECT_EQ(Sem().Get(c_vf32)->Declaration(), c_vf32);
EXPECT_EQ(Sem().Get(c_mf32)->Declaration(), c_mf32);
EXPECT_EQ(Sem().Get(c_s)->Declaration(), c_s);
ASSERT_TRUE(TypeOf(c_i32)->Is<core::type::I32>());
ASSERT_TRUE(TypeOf(c_u32)->Is<core::type::U32>());
ASSERT_TRUE(TypeOf(c_f32)->Is<core::type::F32>());
ASSERT_TRUE(TypeOf(c_vi32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vu32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vf32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_mf32)->Is<core::type::Matrix>());
ASSERT_TRUE(TypeOf(c_s)->Is<core::type::Struct>());
EXPECT_TRUE(Sem().Get(c_i32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_u32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_f32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vi32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vu32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vf32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_mf32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_s)->ConstantValue()->AllZero());
}
TEST_F(ResolverVariableTest, LocalConst_ImplicitType_Decls) {
Structure("S", Vector{Member("m", ty.u32())});
auto* c_i32 = Const("a", Expr(0_i));
auto* c_u32 = Const("b", Expr(0_u));
auto* c_f32 = Const("c", Expr(0_f));
auto* c_ai = Const("d", Expr(0_a));
auto* c_af = Const("e", Expr(0._a));
auto* c_vi32 = Const("f", Call<vec3<i32>>());
auto* c_vu32 = Const("g", Call<vec3<u32>>());
auto* c_vf32 = Const("h", Call<vec3<f32>>());
auto* c_vai = Const("i", Call<vec3<Infer>>(0_a));
auto* c_vaf = Const("j", Call<vec3<Infer>>(0._a));
auto* c_mf32 = Const("k", Call<mat3x3<f32>>());
auto* c_maf32 = Const("l", Call<mat3x3<Infer>>( //
Call<vec3<Infer>>(0._a), //
Call<vec3<Infer>>(0._a), //
Call<vec3<Infer>>(0._a)));
auto* c_s = Const("m", Call("S"));
WrapInFunction(c_i32, c_u32, c_f32, c_ai, c_af, c_vi32, c_vu32, c_vf32, c_vai, c_vaf, c_mf32,
c_maf32, c_s);
ASSERT_TRUE(r()->Resolve()) << r()->error();
EXPECT_EQ(Sem().Get(c_i32)->Declaration(), c_i32);
EXPECT_EQ(Sem().Get(c_u32)->Declaration(), c_u32);
EXPECT_EQ(Sem().Get(c_f32)->Declaration(), c_f32);
EXPECT_EQ(Sem().Get(c_ai)->Declaration(), c_ai);
EXPECT_EQ(Sem().Get(c_af)->Declaration(), c_af);
EXPECT_EQ(Sem().Get(c_vi32)->Declaration(), c_vi32);
EXPECT_EQ(Sem().Get(c_vu32)->Declaration(), c_vu32);
EXPECT_EQ(Sem().Get(c_vf32)->Declaration(), c_vf32);
EXPECT_EQ(Sem().Get(c_vai)->Declaration(), c_vai);
EXPECT_EQ(Sem().Get(c_vaf)->Declaration(), c_vaf);
EXPECT_EQ(Sem().Get(c_mf32)->Declaration(), c_mf32);
EXPECT_EQ(Sem().Get(c_maf32)->Declaration(), c_maf32);
EXPECT_EQ(Sem().Get(c_s)->Declaration(), c_s);
ASSERT_TRUE(TypeOf(c_i32)->Is<core::type::I32>());
ASSERT_TRUE(TypeOf(c_u32)->Is<core::type::U32>());
ASSERT_TRUE(TypeOf(c_f32)->Is<core::type::F32>());
ASSERT_TRUE(TypeOf(c_ai)->Is<core::type::AbstractInt>());
ASSERT_TRUE(TypeOf(c_af)->Is<core::type::AbstractFloat>());
ASSERT_TRUE(TypeOf(c_vi32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vu32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vf32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vai)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vaf)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_mf32)->Is<core::type::Matrix>());
ASSERT_TRUE(TypeOf(c_maf32)->Is<core::type::Matrix>());
ASSERT_TRUE(TypeOf(c_s)->Is<core::type::Struct>());
EXPECT_TRUE(Sem().Get(c_i32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_u32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_f32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_ai)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_af)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vi32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vu32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vf32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vai)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vaf)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_mf32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_maf32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_s)->ConstantValue()->AllZero());
}
TEST_F(ResolverVariableTest, LocalConst_PropagateConstValue) {
auto* a = Const("a", Expr(42_i));
auto* b = Const("b", Expr("a"));
auto* c = Const("c", Expr("b"));
WrapInFunction(a, b, c);
ASSERT_TRUE(r()->Resolve()) << r()->error();
ASSERT_TRUE(TypeOf(c)->Is<core::type::I32>());
EXPECT_EQ(Sem().Get(c)->ConstantValue()->ValueAs<i32>(), 42_i);
}
TEST_F(ResolverVariableTest, LocalConst_ConstEval) {
auto* c = Const("c", Div(Mul(Add(1_i, 2_i), 3_i), 3_i));
WrapInFunction(c);
ASSERT_TRUE(r()->Resolve()) << r()->error();
ASSERT_TRUE(TypeOf(c)->Is<core::type::I32>());
EXPECT_EQ(Sem().Get(c)->ConstantValue()->ValueAs<i32>(), 3_i);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Module-scope 'var'
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(ResolverVariableTest, GlobalVar_AddressSpace) {
// https://gpuweb.github.io/gpuweb/wgsl/#storage-class
Enable(wgsl::Extension::kChromiumExperimentalPushConstant);
auto* buf = Structure("S", Vector{Member("m", ty.i32())});
auto* private_ = GlobalVar("p", ty.i32(), core::AddressSpace::kPrivate);
auto* workgroup = GlobalVar("w", ty.i32(), core::AddressSpace::kWorkgroup);
auto* push_constant = GlobalVar("pc", ty.i32(), core::AddressSpace::kPushConstant);
auto* uniform =
GlobalVar("ub", ty.Of(buf), core::AddressSpace::kUniform, Binding(0_a), Group(0_a));
auto* storage =
GlobalVar("sb", ty.Of(buf), core::AddressSpace::kStorage, Binding(1_a), Group(0_a));
auto* handle = GlobalVar("h", ty.depth_texture(core::type::TextureDimension::k2d), Binding(2_a),
Group(0_a));
ASSERT_TRUE(r()->Resolve()) << r()->error();
ASSERT_TRUE(TypeOf(private_)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(workgroup)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(push_constant)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(uniform)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(storage)->Is<core::type::Reference>());
ASSERT_TRUE(TypeOf(handle)->Is<core::type::Reference>());
EXPECT_EQ(TypeOf(private_)->As<core::type::Reference>()->Access(), core::Access::kReadWrite);
EXPECT_EQ(TypeOf(workgroup)->As<core::type::Reference>()->Access(), core::Access::kReadWrite);
EXPECT_EQ(TypeOf(push_constant)->As<core::type::Reference>()->Access(), core::Access::kRead);
EXPECT_EQ(TypeOf(uniform)->As<core::type::Reference>()->Access(), core::Access::kRead);
EXPECT_EQ(TypeOf(storage)->As<core::type::Reference>()->Access(), core::Access::kRead);
EXPECT_EQ(TypeOf(handle)->As<core::type::Reference>()->Access(), core::Access::kRead);
}
TEST_F(ResolverVariableTest, GlobalVar_ExplicitAddressSpace) {
// https://gpuweb.github.io/gpuweb/wgsl/#storage-class
auto* buf = Structure("S", Vector{Member("m", ty.i32())});
auto* storage = GlobalVar("sb", ty.Of(buf), core::AddressSpace::kStorage,
core::Access::kReadWrite, Binding(1_a), Group(0_a));
ASSERT_TRUE(r()->Resolve()) << r()->error();
ASSERT_TRUE(TypeOf(storage)->Is<core::type::Reference>());
EXPECT_EQ(TypeOf(storage)->As<core::type::Reference>()->Access(), core::Access::kReadWrite);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Module-scope const
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(ResolverVariableTest, GlobalConst_ExplicitType_Decls) {
auto* c_i32 = GlobalConst("a", ty.i32(), Expr(0_i));
auto* c_u32 = GlobalConst("b", ty.u32(), Expr(0_u));
auto* c_f32 = GlobalConst("c", ty.f32(), Expr(0_f));
auto* c_vi32 = GlobalConst("d", ty.vec3<i32>(), Call<vec3<i32>>());
auto* c_vu32 = GlobalConst("e", ty.vec3<u32>(), Call<vec3<u32>>());
auto* c_vf32 = GlobalConst("f", ty.vec3<f32>(), Call<vec3<f32>>());
auto* c_mf32 = GlobalConst("g", ty.mat3x3<f32>(), Call<mat3x3<f32>>());
ASSERT_TRUE(r()->Resolve()) << r()->error();
EXPECT_EQ(Sem().Get(c_i32)->Declaration(), c_i32);
EXPECT_EQ(Sem().Get(c_u32)->Declaration(), c_u32);
EXPECT_EQ(Sem().Get(c_f32)->Declaration(), c_f32);
EXPECT_EQ(Sem().Get(c_vi32)->Declaration(), c_vi32);
EXPECT_EQ(Sem().Get(c_vu32)->Declaration(), c_vu32);
EXPECT_EQ(Sem().Get(c_vf32)->Declaration(), c_vf32);
EXPECT_EQ(Sem().Get(c_mf32)->Declaration(), c_mf32);
ASSERT_TRUE(TypeOf(c_i32)->Is<core::type::I32>());
ASSERT_TRUE(TypeOf(c_u32)->Is<core::type::U32>());
ASSERT_TRUE(TypeOf(c_f32)->Is<core::type::F32>());
ASSERT_TRUE(TypeOf(c_vi32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vu32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vf32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_mf32)->Is<core::type::Matrix>());
EXPECT_TRUE(Sem().Get(c_i32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_u32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_f32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vi32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vu32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vf32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_mf32)->ConstantValue()->AllZero());
}
TEST_F(ResolverVariableTest, GlobalConst_ImplicitType_Decls) {
auto* c_i32 = GlobalConst("a", Expr(0_i));
auto* c_u32 = GlobalConst("b", Expr(0_u));
auto* c_f32 = GlobalConst("c", Expr(0_f));
auto* c_ai = GlobalConst("d", Expr(0_a));
auto* c_af = GlobalConst("e", Expr(0._a));
auto* c_vi32 = GlobalConst("f", Call<vec3<i32>>());
auto* c_vu32 = GlobalConst("g", Call<vec3<u32>>());
auto* c_vf32 = GlobalConst("h", Call<vec3<f32>>());
auto* c_vai = GlobalConst("i", Call<vec3<Infer>>(0_a));
auto* c_vaf = GlobalConst("j", Call<vec3<Infer>>(0._a));
auto* c_mf32 = GlobalConst("k", Call<mat3x3<f32>>());
auto* c_maf32 = GlobalConst("l", Call<mat3x3<Infer>>( //
Call<vec3<Infer>>(0._a), //
Call<vec3<Infer>>(0._a), //
Call<vec3<Infer>>(0._a)));
ASSERT_TRUE(r()->Resolve()) << r()->error();
EXPECT_EQ(Sem().Get(c_i32)->Declaration(), c_i32);
EXPECT_EQ(Sem().Get(c_u32)->Declaration(), c_u32);
EXPECT_EQ(Sem().Get(c_f32)->Declaration(), c_f32);
EXPECT_EQ(Sem().Get(c_ai)->Declaration(), c_ai);
EXPECT_EQ(Sem().Get(c_af)->Declaration(), c_af);
EXPECT_EQ(Sem().Get(c_vi32)->Declaration(), c_vi32);
EXPECT_EQ(Sem().Get(c_vu32)->Declaration(), c_vu32);
EXPECT_EQ(Sem().Get(c_vf32)->Declaration(), c_vf32);
EXPECT_EQ(Sem().Get(c_vai)->Declaration(), c_vai);
EXPECT_EQ(Sem().Get(c_vaf)->Declaration(), c_vaf);
EXPECT_EQ(Sem().Get(c_mf32)->Declaration(), c_mf32);
EXPECT_EQ(Sem().Get(c_maf32)->Declaration(), c_maf32);
ASSERT_TRUE(TypeOf(c_i32)->Is<core::type::I32>());
ASSERT_TRUE(TypeOf(c_u32)->Is<core::type::U32>());
ASSERT_TRUE(TypeOf(c_f32)->Is<core::type::F32>());
ASSERT_TRUE(TypeOf(c_ai)->Is<core::type::AbstractInt>());
ASSERT_TRUE(TypeOf(c_af)->Is<core::type::AbstractFloat>());
ASSERT_TRUE(TypeOf(c_vi32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vu32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vf32)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vai)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_vaf)->Is<core::type::Vector>());
ASSERT_TRUE(TypeOf(c_mf32)->Is<core::type::Matrix>());
ASSERT_TRUE(TypeOf(c_maf32)->Is<core::type::Matrix>());
EXPECT_TRUE(Sem().Get(c_i32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_u32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_f32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_ai)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_af)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vi32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vu32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vf32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vai)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_vaf)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_mf32)->ConstantValue()->AllZero());
EXPECT_TRUE(Sem().Get(c_maf32)->ConstantValue()->AllZero());
}
TEST_F(ResolverVariableTest, GlobalConst_PropagateConstValue) {
GlobalConst("b", Expr("a"));
auto* c = GlobalConst("c", Expr("b"));
GlobalConst("a", Expr(42_i));
ASSERT_TRUE(r()->Resolve()) << r()->error();
ASSERT_TRUE(TypeOf(c)->Is<core::type::I32>());
EXPECT_EQ(Sem().Get(c)->ConstantValue()->ValueAs<i32>(), 42_i);
}
TEST_F(ResolverVariableTest, GlobalConst_ConstEval) {
auto* c = GlobalConst("c", Div(Mul(Add(1_i, 2_i), 3_i), 3_i));
ASSERT_TRUE(r()->Resolve()) << r()->error();
ASSERT_TRUE(TypeOf(c)->Is<core::type::I32>());
EXPECT_EQ(Sem().Get(c)->ConstantValue()->ValueAs<i32>(), 3_i);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Function parameter
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(ResolverVariableTest, Param_ShadowsFunction) {
// fn a(a : bool) {
// }
auto* p = Param("a", ty.bool_());
auto* f = Func("a", Vector{p}, ty.void_(), tint::Empty);
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* func = Sem().Get(f);
auto* param = Sem().Get(p);
ASSERT_NE(func, nullptr);
ASSERT_NE(param, nullptr);
EXPECT_EQ(param->Shadows(), func);
}
TEST_F(ResolverVariableTest, Param_ShadowsGlobalVar) {
// var<private> a : i32;
//
// fn F(a : bool) {
// }
auto* g = GlobalVar("a", ty.i32(), core::AddressSpace::kPrivate);
auto* p = Param("a", ty.bool_());
Func("F", Vector{p}, ty.void_(), tint::Empty);
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* global = Sem().Get(g);
auto* param = Sem().Get(p);
ASSERT_NE(global, nullptr);
ASSERT_NE(param, nullptr);
EXPECT_EQ(param->Shadows(), global);
}
TEST_F(ResolverVariableTest, Param_ShadowsGlobalConst) {
// const a : i32 = 1i;
//
// fn F(a : bool) {
// }
auto* g = GlobalConst("a", ty.i32(), Expr(1_i));
auto* p = Param("a", ty.bool_());
Func("F", Vector{p}, ty.void_(), tint::Empty);
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* global = Sem().Get(g);
auto* param = Sem().Get(p);
ASSERT_NE(global, nullptr);
ASSERT_NE(param, nullptr);
EXPECT_EQ(param->Shadows(), global);
}
TEST_F(ResolverVariableTest, Param_ShadowsAlias) {
// type a = i32;
//
// fn F(a : a) {
// }
auto* a = Alias("a", ty.i32());
auto* p = Param("a", ty("a"));
Func("F", Vector{p}, ty.void_(), tint::Empty);
ASSERT_TRUE(r()->Resolve()) << r()->error();
auto* alias = Sem().Get(a);
auto* param = Sem().Get(p);
ASSERT_NE(alias, nullptr);
ASSERT_NE(param, nullptr);
EXPECT_EQ(param->Shadows(), alias);
EXPECT_EQ(param->Type(), alias);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Templated identifier uses
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(ResolverVariableTest, GlobalVar_UseTemplatedIdent) {
// var<private> a : i32;
//
// fn F() {
// let l = a<i32>;
// }
GlobalVar(Source{{56, 78}}, "a", ty.i32(), core::AddressSpace::kPrivate);
Func("F", tint::Empty, ty.void_(),
Vector{
Decl(Let("l", Expr(Ident(Source{{12, 34}}, "a", "i32")))),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: variable 'a' does not take template arguments
56:78 note: 'var a' declared here)");
}
TEST_F(ResolverVariableTest, GlobalConst_UseTemplatedIdent) {
// const a = 1i;
//
// fn F() {
// let l = a<i32>;
// }
GlobalConst(Source{{56, 78}}, "a", Expr(1_i));
Func("F", tint::Empty, ty.void_(),
Vector{
Decl(Let("l", Expr(Ident(Source{{12, 34}}, "a", "i32")))),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: variable 'a' does not take template arguments
56:78 note: 'const a' declared here)");
}
TEST_F(ResolverVariableTest, GlobalOverride_UseTemplatedIdent) {
// override a = 1i;
//
// fn F() {
// let l = a<i32>;
// }
Override(Source{{56, 78}}, "a", Expr(1_i));
Func("F", tint::Empty, ty.void_(),
Vector{
Decl(Let("l", Expr(Ident(Source{{12, 34}}, "a", "i32")))),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: variable 'a' does not take template arguments
56:78 note: 'override a' declared here)");
}
TEST_F(ResolverVariableTest, Param_UseTemplatedIdent) {
// fn F(a : i32) {
// let l = a<i32>;
// }
Func("F", Vector{Param(Source{{56, 78}}, "a", ty.i32())}, ty.void_(),
Vector{
Decl(Let("l", Expr(Ident(Source{{12, 34}}, "a", "i32")))),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: variable 'a' does not take template arguments
56:78 note: parameter 'a' declared here)");
}
TEST_F(ResolverVariableTest, LocalVar_UseTemplatedIdent) {
// fn F() {
// var a : i32;
// let l = a<i32>;
// }
Func("F", tint::Empty, ty.void_(),
Vector{
Decl(Var(Source{{56, 78}}, "a", ty.i32())),
Decl(Let("l", Expr(Ident(Source{{12, 34}}, "a", "i32")))),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: variable 'a' does not take template arguments
56:78 note: 'var a' declared here)");
}
TEST_F(ResolverVariableTest, Let_UseTemplatedIdent) {
// fn F() {
// let a = 1i;
// let l = a<i32>;
// }
Func("F", tint::Empty, ty.void_(),
Vector{
Decl(Let(Source{{56, 78}}, "a", Expr(1_i))),
Decl(Let("l", Expr(Ident(Source{{12, 34}}, "a", "i32")))),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: variable 'a' does not take template arguments
56:78 note: 'let a' declared here)");
}
} // namespace
} // namespace tint::resolver