tint/transform: Handle 'const' for Unshadow.

Bug: tint:1580
Change-Id: I0af473ac13abde1f1890851aeb92812be6a52933
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94682
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Ben Clayton 2022-06-26 14:38:30 +00:00 committed by Dawn LUCI CQ
parent fcf9fdcad8
commit 54b3da95f8
2 changed files with 279 additions and 3 deletions

View File

@ -63,6 +63,9 @@ struct Unshadow::State {
[&](const ast::Let*) { [&](const ast::Let*) {
return ctx.dst->Let(source, symbol, type, constructor, attributes); return ctx.dst->Let(source, symbol, type, constructor, attributes);
}, },
[&](const ast::Const*) {
return ctx.dst->Const(source, symbol, type, constructor, attributes);
},
[&](const ast::Parameter*) { [&](const ast::Parameter*) {
return ctx.dst->Param(source, symbol, type, attributes); return ctx.dst->Param(source, symbol, type, attributes);
}, },

View File

@ -34,14 +34,16 @@ TEST_F(UnshadowTest, Noop) {
auto* src = R"( auto* src = R"(
var<private> a : i32; var<private> a : i32;
let b : i32 = 1; const b : i32 = 1;
fn F(c : i32) { fn F(c : i32) {
var d : i32; var d : i32;
let e : i32 = 1; let e : i32 = 1;
const f : i32 = 2;
{ {
var f : i32; var g : i32;
let g : i32 = 1; let h : i32 = 1;
const i : i32 = 2;
} }
} }
)"; )";
@ -64,6 +66,10 @@ fn X() {
fn Y() { fn Y() {
let a = true; let a = true;
} }
fn Z() {
const a = true;
}
)"; )";
auto* expect = R"( auto* expect = R"(
@ -76,6 +82,10 @@ fn X() {
fn Y() { fn Y() {
let a_2 = true; let a_2 = true;
} }
fn Z() {
const a_3 = true;
}
)"; )";
auto got = Run<Unshadow>(src); auto got = Run<Unshadow>(src);
@ -93,6 +103,10 @@ fn Y() {
let a = true; let a = true;
} }
fn Z() {
const a = true;
}
type a = i32; type a = i32;
)"; )";
@ -105,6 +119,10 @@ fn Y() {
let a_2 = true; let a_2 = true;
} }
fn Z() {
const a_3 = true;
}
type a = i32; type a = i32;
)"; )";
@ -126,6 +144,10 @@ fn X() {
fn Y() { fn Y() {
let a = false; let a = false;
} }
fn Z() {
const a = false;
}
)"; )";
auto* expect = R"( auto* expect = R"(
@ -140,6 +162,10 @@ fn X() {
fn Y() { fn Y() {
let a_2 = false; let a_2 = false;
} }
fn Z() {
const a_3 = false;
}
)"; )";
auto got = Run<Unshadow>(src); auto got = Run<Unshadow>(src);
@ -157,6 +183,10 @@ fn Y() {
let a = false; let a = false;
} }
fn Z() {
const a = false;
}
struct a { struct a {
m : i32, m : i32,
}; };
@ -172,6 +202,10 @@ fn Y() {
let a_2 = false; let a_2 = false;
} }
fn Z() {
const a_3 = false;
}
struct a { struct a {
m : i32, m : i32,
} }
@ -187,11 +221,19 @@ TEST_F(UnshadowTest, LocalShadowsFunction) {
fn a() { fn a() {
var a = true; var a = true;
var b = false; var b = false;
var c = true;
} }
fn b() { fn b() {
let a = true; let a = true;
let b = false; let b = false;
let c = true;
}
fn c() {
const a = true;
const b = false;
const c = true;
} }
)"; )";
@ -199,11 +241,19 @@ fn b() {
fn a() { fn a() {
var a_1 = true; var a_1 = true;
var b_1 = false; var b_1 = false;
var c_1 = true;
} }
fn b() { fn b() {
let a_2 = true; let a_2 = true;
let b_2 = false; let b_2 = false;
let c_2 = true;
}
fn c() {
const a_3 = true;
const b_3 = false;
const c_3 = true;
} }
)"; )";
@ -217,24 +267,39 @@ TEST_F(UnshadowTest, LocalShadowsFunction_OutOfOrder) {
fn b() { fn b() {
let a = true; let a = true;
let b = false; let b = false;
let c = true;
} }
fn a() { fn a() {
var a = true; var a = true;
var b = false; var b = false;
var c = true;
} }
fn c() {
const a = true;
const b = false;
const c = true;
}
)"; )";
auto* expect = R"( auto* expect = R"(
fn b() { fn b() {
let a_1 = true; let a_1 = true;
let b_1 = false; let b_1 = false;
let c_1 = true;
} }
fn a() { fn a() {
var a_2 = true; var a_2 = true;
var b_2 = false; var b_2 = false;
var c_2 = true;
}
fn c() {
const a_3 = true;
const b_3 = false;
const c_3 = true;
} }
)"; )";
@ -254,6 +319,10 @@ fn X() {
fn Y() { fn Y() {
let a = (a == 321); let a = (a == 321);
} }
fn Z() {
const a = 321;
}
)"; )";
auto* expect = R"( auto* expect = R"(
@ -266,6 +335,10 @@ fn X() {
fn Y() { fn Y() {
let a_2 = (a == 321); let a_2 = (a == 321);
} }
fn Z() {
const a_3 = 321;
}
)"; )";
auto got = Run<Unshadow>(src); auto got = Run<Unshadow>(src);
@ -283,6 +356,10 @@ fn Y() {
let a = (a == 321); let a = (a == 321);
} }
fn Z() {
const a = 321;
}
var<private> a : i32; var<private> a : i32;
)"; )";
@ -295,6 +372,10 @@ fn Y() {
let a_2 = (a == 321); let a_2 = (a == 321);
} }
fn Z() {
const a_3 = 321;
}
var<private> a : i32; var<private> a : i32;
)"; )";
@ -314,6 +395,10 @@ fn X() {
fn Y() { fn Y() {
let a = (a == 321); let a = (a == 321);
} }
fn Z() {
const a = 321;
}
)"; )";
auto* expect = R"( auto* expect = R"(
@ -326,6 +411,10 @@ fn X() {
fn Y() { fn Y() {
let a_2 = (a == 321); let a_2 = (a == 321);
} }
fn Z() {
const a_3 = 321;
}
)"; )";
auto got = Run<Unshadow>(src); auto got = Run<Unshadow>(src);
@ -343,6 +432,10 @@ fn Y() {
let a = (a == 321); let a = (a == 321);
} }
fn Z() {
const a = 321;
}
let a : i32 = 1; let a : i32 = 1;
)"; )";
@ -355,6 +448,10 @@ fn Y() {
let a_2 = (a == 321); let a_2 = (a == 321);
} }
fn Z() {
const a_3 = 321;
}
let a : i32 = 1; let a : i32 = 1;
)"; )";
@ -363,6 +460,82 @@ let a : i32 = 1;
EXPECT_EQ(expect, str(got)); EXPECT_EQ(expect, str(got));
} }
TEST_F(UnshadowTest, LocalShadowsGlobalConst) {
auto* src = R"(
const a : i32 = 1;
fn X() {
var a = (a == 123);
}
fn Y() {
let a = (a == 321);
}
fn Z() {
const a = a;
}
)";
auto* expect = R"(
const a : i32 = 1;
fn X() {
var a_1 = (a == 123);
}
fn Y() {
let a_2 = (a == 321);
}
fn Z() {
const a_3 = a;
}
)";
auto got = Run<Unshadow>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(UnshadowTest, LocalShadowsGlobalConst_OutOfOrder) {
auto* src = R"(
fn X() {
var a = (a == 123);
}
fn Y() {
let a = (a == 321);
}
fn Z() {
const a = a;
}
const a : i32 = 1;
)";
auto* expect = R"(
fn X() {
var a_1 = (a == 123);
}
fn Y() {
let a_2 = (a == 321);
}
fn Z() {
const a_3 = a;
}
const a : i32 = 1;
)";
auto got = Run<Unshadow>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(UnshadowTest, LocalShadowsLocalVar) { TEST_F(UnshadowTest, LocalShadowsLocalVar) {
auto* src = R"( auto* src = R"(
fn X() { fn X() {
@ -373,6 +546,9 @@ fn X() {
{ {
let a = (a == 321); let a = (a == 321);
} }
{
const a = 321;
}
} }
)"; )";
@ -385,6 +561,9 @@ fn X() {
{ {
let a_2 = (a == 321); let a_2 = (a == 321);
} }
{
const a_3 = 321;
}
} }
)"; )";
@ -403,6 +582,9 @@ fn X() {
{ {
let a = (a == 321); let a = (a == 321);
} }
{
const a = 321;
}
} }
)"; )";
@ -415,6 +597,45 @@ fn X() {
{ {
let a_2 = (a == 321); let a_2 = (a == 321);
} }
{
const a_3 = 321;
}
}
)";
auto got = Run<Unshadow>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(UnshadowTest, LocalShadowsLocalConst) {
auto* src = R"(
fn X() {
const a = 1;
{
var a = (a == 123);
}
{
let a = (a == 321);
}
{
const a = a;
}
}
)";
auto* expect = R"(
fn X() {
const a = 1;
{
var a_1 = (a == 123);
}
{
let a_2 = (a == 321);
}
{
const a_3 = a;
}
} }
)"; )";
@ -432,6 +653,9 @@ fn F(a : i32) {
{ {
let a = (a == 321); let a = (a == 321);
} }
{
const a = 321;
}
} }
)"; )";
@ -443,6 +667,9 @@ fn F(a : i32) {
{ {
let a_2 = (a == 321); let a_2 = (a == 321);
} }
{
const a_3 = 321;
}
} }
)"; )";
@ -460,6 +687,9 @@ fn a(a : i32) {
{ {
let a = (a == 321); let a = (a == 321);
} }
{
const a = 321;
}
} }
)"; )";
@ -471,6 +701,9 @@ fn a(a_1 : i32) {
{ {
let a_3 = (a_1 == 321); let a_3 = (a_1 == 321);
} }
{
const a_4 = 321;
}
} }
)"; )";
@ -539,6 +772,46 @@ let a : i32 = 1;
EXPECT_EQ(expect, str(got)); EXPECT_EQ(expect, str(got));
} }
TEST_F(UnshadowTest, ParamShadowsGlobalConst) {
auto* src = R"(
const a : i32 = 1;
fn F(a : bool) {
}
)";
auto* expect = R"(
const a : i32 = 1;
fn F(a_1 : bool) {
}
)";
auto got = Run<Unshadow>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(UnshadowTest, ParamShadowsGlobalConst_OutOfOrder) {
auto* src = R"(
fn F(a : bool) {
}
const a : i32 = 1;
)";
auto* expect = R"(
fn F(a_1 : bool) {
}
const a : i32 = 1;
)";
auto got = Run<Unshadow>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(UnshadowTest, ParamShadowsAlias) { TEST_F(UnshadowTest, ParamShadowsAlias) {
auto* src = R"( auto* src = R"(
type a = i32; type a = i32;