Replace VariableDecoration::(Is|As)Binding with Castable

Change-Id: Ia13ce42054e5f514eb34f6549da10b22129f9026
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34306
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-30 23:30:58 +00:00
parent 864579db3d
commit 62d81485dd
14 changed files with 53 additions and 70 deletions

View File

@ -22,10 +22,6 @@ BindingDecoration::BindingDecoration(uint32_t val, const Source& source)
BindingDecoration::~BindingDecoration() = default;
bool BindingDecoration::IsBinding() const {
return true;
}
void BindingDecoration::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << "BindingDecoration{" << value_ << "}" << std::endl;

View File

@ -32,9 +32,6 @@ class BindingDecoration
BindingDecoration(uint32_t value, const Source& source);
~BindingDecoration() override;
/// @returns true if this is a binding decoration
bool IsBinding() const override;
/// @returns the binding value
uint32_t value() const { return value_; }

View File

@ -29,7 +29,7 @@ TEST_F(BindingDecorationTest, Creation) {
TEST_F(BindingDecorationTest, Is) {
BindingDecoration d{2, Source{}};
EXPECT_TRUE(d.IsBinding());
EXPECT_TRUE(d.Is<BindingDecoration>());
EXPECT_FALSE(d.IsBuiltin());
EXPECT_FALSE(d.IsConstantId());
EXPECT_FALSE(d.IsLocation());

View File

@ -28,12 +28,13 @@ TEST_F(BuiltinDecorationTest, Creation) {
}
TEST_F(BuiltinDecorationTest, Is) {
BuiltinDecoration d{Builtin::kFragDepth, Source{}};
EXPECT_FALSE(d.IsBinding());
EXPECT_TRUE(d.IsBuiltin());
EXPECT_FALSE(d.IsConstantId());
EXPECT_FALSE(d.IsLocation());
EXPECT_FALSE(d.IsSet());
BuiltinDecoration bd{Builtin::kFragDepth, Source{}};
Decoration* d = &bd;
EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_TRUE(bd.IsBuiltin());
EXPECT_FALSE(bd.IsConstantId());
EXPECT_FALSE(bd.IsLocation());
EXPECT_FALSE(bd.IsSet());
}
TEST_F(BuiltinDecorationTest, ToStr) {

View File

@ -28,12 +28,13 @@ TEST_F(ConstantIdDecorationTest, Creation) {
}
TEST_F(ConstantIdDecorationTest, Is) {
ConstantIdDecoration d{27, Source{}};
EXPECT_FALSE(d.IsBinding());
EXPECT_FALSE(d.IsBuiltin());
EXPECT_TRUE(d.IsConstantId());
EXPECT_FALSE(d.IsLocation());
EXPECT_FALSE(d.IsSet());
ConstantIdDecoration cd{27, Source{}};
Decoration* d = &cd;
EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_FALSE(cd.IsBuiltin());
EXPECT_TRUE(cd.IsConstantId());
EXPECT_FALSE(cd.IsLocation());
EXPECT_FALSE(cd.IsSet());
}
TEST_F(ConstantIdDecorationTest, ToStr) {

View File

@ -111,10 +111,10 @@ Function::referenced_uniform_variables() const {
BindingDecoration* binding = nullptr;
SetDecoration* set = nullptr;
for (auto* deco : var->AsDecorated()->decorations()) {
if (deco->IsBinding()) {
binding = deco->AsBinding();
} else if (deco->IsSet()) {
set = deco->AsSet();
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
} else if (auto* s = deco->As<ast::SetDecoration>()) {
set = s;
}
}
if (binding == nullptr || set == nullptr) {
@ -139,10 +139,10 @@ Function::referenced_storagebuffer_variables() const {
BindingDecoration* binding = nullptr;
SetDecoration* set = nullptr;
for (auto* deco : var->AsDecorated()->decorations()) {
if (deco->IsBinding()) {
binding = deco->AsBinding();
} else if (deco->IsSet()) {
set = deco->AsSet();
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
} else if (auto* s = deco->As<ast::SetDecoration>()) {
set = s;
}
}
if (binding == nullptr || set == nullptr) {
@ -291,10 +291,10 @@ Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const {
BindingDecoration* binding = nullptr;
SetDecoration* set = nullptr;
for (auto* deco : var->AsDecorated()->decorations()) {
if (deco->IsBinding()) {
binding = deco->AsBinding();
} else if (deco->IsSet()) {
set = deco->AsSet();
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
} else if (auto* s = deco->As<ast::SetDecoration>()) {
set = s;
}
}
if (binding == nullptr || set == nullptr) {
@ -326,10 +326,10 @@ Function::ReferencedSampledTextureVariablesImpl(bool multisampled) const {
BindingDecoration* binding = nullptr;
SetDecoration* set = nullptr;
for (auto* deco : var->AsDecorated()->decorations()) {
if (deco->IsBinding()) {
binding = deco->AsBinding();
} else if (deco->IsSet()) {
set = deco->AsSet();
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
} else if (auto* s = deco->As<ast::SetDecoration>()) {
set = s;
}
}
if (binding == nullptr || set == nullptr) {

View File

@ -30,12 +30,13 @@ TEST_F(LocationDecorationTest, Creation) {
}
TEST_F(LocationDecorationTest, Is) {
LocationDecoration d{2, Source{}};
EXPECT_FALSE(d.IsBinding());
EXPECT_FALSE(d.IsBuiltin());
EXPECT_FALSE(d.IsConstantId());
EXPECT_TRUE(d.IsLocation());
EXPECT_FALSE(d.IsSet());
LocationDecoration ld{2, Source{}};
Decoration* d = &ld;
EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_FALSE(ld.IsBuiltin());
EXPECT_FALSE(ld.IsConstantId());
EXPECT_TRUE(ld.IsLocation());
EXPECT_FALSE(ld.IsSet());
}
TEST_F(LocationDecorationTest, ToStr) {

View File

@ -28,12 +28,13 @@ TEST_F(SetDecorationTest, Creation) {
}
TEST_F(SetDecorationTest, Is) {
SetDecoration d{2, Source{}};
EXPECT_FALSE(d.IsBinding());
EXPECT_FALSE(d.IsBuiltin());
EXPECT_FALSE(d.IsConstantId());
EXPECT_FALSE(d.IsLocation());
EXPECT_TRUE(d.IsSet());
SetDecoration sd{2, Source{}};
Decoration* d = &sd;
EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_FALSE(sd.IsBuiltin());
EXPECT_FALSE(sd.IsConstantId());
EXPECT_FALSE(sd.IsLocation());
EXPECT_TRUE(sd.IsSet());
}
TEST_F(SetDecorationTest, ToStr) {

View File

@ -35,10 +35,6 @@ DecorationKind VariableDecoration::GetKind() const {
return Kind;
}
bool VariableDecoration::IsBinding() const {
return false;
}
bool VariableDecoration::IsBuiltin() const {
return false;
}
@ -55,11 +51,6 @@ bool VariableDecoration::IsSet() const {
return false;
}
BindingDecoration* VariableDecoration::AsBinding() {
assert(IsBinding());
return static_cast<BindingDecoration*>(this);
}
BuiltinDecoration* VariableDecoration::AsBuiltin() {
assert(IsBuiltin());
return static_cast<BuiltinDecoration*>(this);

View File

@ -25,7 +25,6 @@
namespace tint {
namespace ast {
class BindingDecoration;
class BuiltinDecoration;
class ConstantIdDecoration;
class LocationDecoration;
@ -42,8 +41,6 @@ class VariableDecoration : public Castable<VariableDecoration, Decoration> {
/// @return the decoration kind
DecorationKind GetKind() const override;
/// @returns true if this is a binding decoration
virtual bool IsBinding() const;
/// @returns true if this is a builtin decoration
virtual bool IsBuiltin() const;
/// @returns true if this is a constant id decoration
@ -53,8 +50,6 @@ class VariableDecoration : public Castable<VariableDecoration, Decoration> {
/// @returns true if this is a set decoration
virtual bool IsSet() const;
/// @returns the decoration as a binding decoration
BindingDecoration* AsBinding();
/// @returns the decoration as a builtin decoration
BuiltinDecoration* AsBuiltin();
/// @returns the decoration as a constant id decoration

View File

@ -104,7 +104,7 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithDecoration) {
auto& decorations = v->decorations();
ASSERT_EQ(decorations.size(), 2u);
ASSERT_TRUE(decorations[0]->IsBinding());
ASSERT_TRUE(decorations[0]->Is<ast::BindingDecoration>());
ASSERT_TRUE(decorations[1]->IsSet());
}
@ -138,7 +138,7 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithDecoration_MulitpleGroups) {
auto& decorations = v->decorations();
ASSERT_EQ(decorations.size(), 2u);
ASSERT_TRUE(decorations[0]->IsBinding());
ASSERT_TRUE(decorations[0]->Is<ast::BindingDecoration>());
ASSERT_TRUE(decorations[1]->IsSet());
}

View File

@ -183,9 +183,9 @@ TEST_F(ParserImplTest, VariableDecoration_Binding) {
auto* var_deco = deco.value->As<ast::VariableDecoration>();
ASSERT_NE(var_deco, nullptr);
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(var_deco->IsBinding());
ASSERT_TRUE(var_deco->Is<ast::BindingDecoration>());
auto* binding = var_deco->AsBinding();
auto* binding = var_deco->As<ast::BindingDecoration>();
EXPECT_EQ(binding->value(), 4u);
}

View File

@ -787,10 +787,10 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) {
push_annot(spv::Op::OpDecorate,
{Operand::Int(var_id), Operand::Int(SpvDecorationLocation),
Operand::Int(deco->AsLocation()->value())});
} else if (deco->IsBinding()) {
} else if (auto* binding = deco->As<ast::BindingDecoration>()) {
push_annot(spv::Op::OpDecorate,
{Operand::Int(var_id), Operand::Int(SpvDecorationBinding),
Operand::Int(deco->AsBinding()->value())});
Operand::Int(binding->value())});
} else if (deco->IsSet()) {
push_annot(
spv::Op::OpDecorate,

View File

@ -641,8 +641,8 @@ bool GeneratorImpl::EmitVariableDecorations(ast::DecoratedVariable* var) {
}
first = false;
if (deco->IsBinding()) {
out_ << "binding(" << deco->AsBinding()->value() << ")";
if (auto* binding = deco->As<ast::BindingDecoration>()) {
out_ << "binding(" << binding->value() << ")";
} else if (deco->IsSet()) {
out_ << "set(" << deco->AsSet()->value() << ")";
} else if (deco->IsLocation()) {