From 0482b547827cbc0c4e9b29727cef8521a64aa9a4 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 30 Nov 2020 23:30:58 +0000 Subject: [PATCH] Replace VariableDecoration::(Is|As)ConstantId with Castable Change-Id: I3f402fe8925ef7d5c9b3d5b21f2556d7af59a0cd Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34308 Reviewed-by: dan sinclair --- src/ast/binding_decoration_test.cc | 3 ++- src/ast/builtin_decoration_test.cc | 3 ++- src/ast/constant_id_decoration.cc | 4 ---- src/ast/constant_id_decoration.h | 3 --- src/ast/constant_id_decoration_test.cc | 2 +- src/ast/decorated_variable.cc | 6 +++--- src/ast/location_decoration_test.cc | 3 ++- src/ast/set_decoration_test.cc | 3 ++- src/ast/variable_decoration.cc | 12 ------------ src/ast/variable_decoration.h | 4 ---- src/writer/spirv/builder.cc | 3 ++- src/writer/wgsl/generator_impl.cc | 4 ++-- 12 files changed, 16 insertions(+), 34 deletions(-) diff --git a/src/ast/binding_decoration_test.cc b/src/ast/binding_decoration_test.cc index 7bd236f137..e3889e263e 100644 --- a/src/ast/binding_decoration_test.cc +++ b/src/ast/binding_decoration_test.cc @@ -15,6 +15,7 @@ #include "src/ast/binding_decoration.h" #include "src/ast/test_helper.h" +#include "src/ast/constant_id_decoration.h" namespace tint { namespace ast { @@ -32,7 +33,7 @@ TEST_F(BindingDecorationTest, Is) { Decoration* d = &bd; EXPECT_TRUE(d->Is()); EXPECT_FALSE(d->Is()); - EXPECT_FALSE(bd.IsConstantId()); + EXPECT_FALSE(d->Is()); EXPECT_FALSE(bd.IsLocation()); EXPECT_FALSE(bd.IsSet()); } diff --git a/src/ast/builtin_decoration_test.cc b/src/ast/builtin_decoration_test.cc index d3e3702424..b0fa0ffa56 100644 --- a/src/ast/builtin_decoration_test.cc +++ b/src/ast/builtin_decoration_test.cc @@ -14,6 +14,7 @@ #include "src/ast/builtin_decoration.h" +#include "src/ast/constant_id_decoration.h" #include "src/ast/test_helper.h" namespace tint { @@ -32,7 +33,7 @@ TEST_F(BuiltinDecorationTest, Is) { Decoration* d = &bd; EXPECT_FALSE(d->Is()); EXPECT_TRUE(d->Is()); - EXPECT_FALSE(bd.IsConstantId()); + EXPECT_FALSE(d->Is()); EXPECT_FALSE(bd.IsLocation()); EXPECT_FALSE(bd.IsSet()); } diff --git a/src/ast/constant_id_decoration.cc b/src/ast/constant_id_decoration.cc index 86eaa7af74..6b10150f50 100644 --- a/src/ast/constant_id_decoration.cc +++ b/src/ast/constant_id_decoration.cc @@ -22,10 +22,6 @@ ConstantIdDecoration::ConstantIdDecoration(uint32_t val, const Source& source) ConstantIdDecoration::~ConstantIdDecoration() = default; -bool ConstantIdDecoration::IsConstantId() const { - return true; -} - void ConstantIdDecoration::to_str(std::ostream& out, size_t indent) const { make_indent(out, indent); out << "ConstantIdDecoration{" << value_ << "}" << std::endl; diff --git a/src/ast/constant_id_decoration.h b/src/ast/constant_id_decoration.h index 0a1192f103..d7a02561b5 100644 --- a/src/ast/constant_id_decoration.h +++ b/src/ast/constant_id_decoration.h @@ -31,9 +31,6 @@ class ConstantIdDecoration ConstantIdDecoration(uint32_t val, const Source& source); ~ConstantIdDecoration() override; - /// @returns true if this is a constant_id decoration - bool IsConstantId() const override; - /// @returns the constant id value uint32_t value() const { return value_; } diff --git a/src/ast/constant_id_decoration_test.cc b/src/ast/constant_id_decoration_test.cc index 6df2495cce..d784b44f2c 100644 --- a/src/ast/constant_id_decoration_test.cc +++ b/src/ast/constant_id_decoration_test.cc @@ -32,7 +32,7 @@ TEST_F(ConstantIdDecorationTest, Is) { Decoration* d = &cd; EXPECT_FALSE(d->Is()); EXPECT_FALSE(d->Is()); - EXPECT_TRUE(cd.IsConstantId()); + EXPECT_TRUE(d->Is()); EXPECT_FALSE(cd.IsLocation()); EXPECT_FALSE(cd.IsSet()); } diff --git a/src/ast/decorated_variable.cc b/src/ast/decorated_variable.cc index dcf6da375b..2e704b12c4 100644 --- a/src/ast/decorated_variable.cc +++ b/src/ast/decorated_variable.cc @@ -51,7 +51,7 @@ bool DecoratedVariable::HasBuiltinDecoration() const { bool DecoratedVariable::HasConstantIdDecoration() const { for (auto* deco : decorations_) { - if (deco->IsConstantId()) { + if (deco->Is()) { return true; } } @@ -61,8 +61,8 @@ bool DecoratedVariable::HasConstantIdDecoration() const { uint32_t DecoratedVariable::constant_id() const { assert(HasConstantIdDecoration()); for (auto* deco : decorations_) { - if (deco->IsConstantId()) { - return deco->AsConstantId()->value(); + if (auto* cid = deco->As()) { + return cid->value(); } } return 0; diff --git a/src/ast/location_decoration_test.cc b/src/ast/location_decoration_test.cc index e2c1b7cd35..2876899987 100644 --- a/src/ast/location_decoration_test.cc +++ b/src/ast/location_decoration_test.cc @@ -16,6 +16,7 @@ #include +#include "src/ast/constant_id_decoration.h" #include "src/ast/test_helper.h" namespace tint { @@ -34,7 +35,7 @@ TEST_F(LocationDecorationTest, Is) { Decoration* d = &ld; EXPECT_FALSE(d->Is()); EXPECT_FALSE(d->Is()); - EXPECT_FALSE(ld.IsConstantId()); + EXPECT_FALSE(d->Is()); EXPECT_TRUE(ld.IsLocation()); EXPECT_FALSE(ld.IsSet()); } diff --git a/src/ast/set_decoration_test.cc b/src/ast/set_decoration_test.cc index df5afa1836..ceac9a7c62 100644 --- a/src/ast/set_decoration_test.cc +++ b/src/ast/set_decoration_test.cc @@ -14,6 +14,7 @@ #include "src/ast/set_decoration.h" +#include "src/ast/constant_id_decoration.h" #include "src/ast/test_helper.h" namespace tint { @@ -32,7 +33,7 @@ TEST_F(SetDecorationTest, Is) { Decoration* d = &sd; EXPECT_FALSE(d->Is()); EXPECT_FALSE(d->Is()); - EXPECT_FALSE(sd.IsConstantId()); + EXPECT_FALSE(d->Is()); EXPECT_FALSE(sd.IsLocation()); EXPECT_TRUE(sd.IsSet()); } diff --git a/src/ast/variable_decoration.cc b/src/ast/variable_decoration.cc index 3dfb875bd2..79e956295c 100644 --- a/src/ast/variable_decoration.cc +++ b/src/ast/variable_decoration.cc @@ -16,9 +16,6 @@ #include -#include "src/ast/binding_decoration.h" -#include "src/ast/builtin_decoration.h" -#include "src/ast/constant_id_decoration.h" #include "src/ast/location_decoration.h" #include "src/ast/set_decoration.h" @@ -39,19 +36,10 @@ bool VariableDecoration::IsLocation() const { return false; } -bool VariableDecoration::IsConstantId() const { - return false; -} - bool VariableDecoration::IsSet() const { return false; } -ConstantIdDecoration* VariableDecoration::AsConstantId() { - assert(IsConstantId()); - return static_cast(this); -} - LocationDecoration* VariableDecoration::AsLocation() { assert(IsLocation()); return static_cast(this); diff --git a/src/ast/variable_decoration.h b/src/ast/variable_decoration.h index c83d22be3a..8e6ae1693a 100644 --- a/src/ast/variable_decoration.h +++ b/src/ast/variable_decoration.h @@ -40,15 +40,11 @@ class VariableDecoration : public Castable { /// @return the decoration kind DecorationKind GetKind() const override; - /// @returns true if this is a constant id decoration - virtual bool IsConstantId() const; /// @returns true if this is a location decoration virtual bool IsLocation() const; /// @returns true if this is a set decoration virtual bool IsSet() const; - /// @returns the decoration as a constant id decoration - ConstantIdDecoration* AsConstantId(); /// @returns the decoration as a location decoration LocationDecoration* AsLocation(); /// @returns the decoration as a set decoration diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index c712310037..43b0507b6e 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -31,6 +31,7 @@ #include "src/ast/call_expression.h" #include "src/ast/call_statement.h" #include "src/ast/case_statement.h" +#include "src/ast/constant_id_decoration.h" #include "src/ast/constructor_expression.h" #include "src/ast/decorated_variable.h" #include "src/ast/else_statement.h" @@ -796,7 +797,7 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) { spv::Op::OpDecorate, {Operand::Int(var_id), Operand::Int(SpvDecorationDescriptorSet), Operand::Int(deco->AsSet()->value())}); - } else if (deco->IsConstantId()) { + } else if (deco->Is()) { // Spec constants are handled elsewhere } else { error_ = "unknown decoration"; diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index b0d3fa5b81..b22a529e5c 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -649,8 +649,8 @@ bool GeneratorImpl::EmitVariableDecorations(ast::DecoratedVariable* var) { out_ << "location(" << deco->AsLocation()->value() << ")"; } else if (auto* builtin = deco->As()) { out_ << "builtin(" << builtin->value() << ")"; - } else if (deco->IsConstantId()) { - out_ << "constant_id(" << deco->AsConstantId()->value() << ")"; + } else if (auto* cid = deco->As()) { + out_ << "constant_id(" << cid->value() << ")"; } else { error_ = "unknown variable decoration"; return false;