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

Change-Id: I9808e31f3842b70b3df9ed0398583e09e4f2ca42
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34310
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-30 23:30:58 +00:00
parent 2c1d7d5be7
commit c24ac2720d
13 changed files with 13 additions and 39 deletions

View File

@ -35,7 +35,7 @@ TEST_F(BindingDecorationTest, Is) {
EXPECT_FALSE(d->Is<BuiltinDecoration>()); EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>()); EXPECT_FALSE(d->Is<ConstantIdDecoration>());
EXPECT_FALSE(d->Is<LocationDecoration>()); EXPECT_FALSE(d->Is<LocationDecoration>());
EXPECT_FALSE(bd.IsSet()); EXPECT_FALSE(d->Is<SetDecoration>());
} }
TEST_F(BindingDecorationTest, ToStr) { TEST_F(BindingDecorationTest, ToStr) {

View File

@ -35,7 +35,7 @@ TEST_F(BuiltinDecorationTest, Is) {
EXPECT_TRUE(d->Is<BuiltinDecoration>()); EXPECT_TRUE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>()); EXPECT_FALSE(d->Is<ConstantIdDecoration>());
EXPECT_FALSE(d->Is<LocationDecoration>()); EXPECT_FALSE(d->Is<LocationDecoration>());
EXPECT_FALSE(bd.IsSet()); EXPECT_FALSE(d->Is<SetDecoration>());
} }
TEST_F(BuiltinDecorationTest, ToStr) { TEST_F(BuiltinDecorationTest, ToStr) {

View File

@ -34,7 +34,7 @@ TEST_F(ConstantIdDecorationTest, Is) {
EXPECT_FALSE(d->Is<BuiltinDecoration>()); EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_TRUE(d->Is<ConstantIdDecoration>()); EXPECT_TRUE(d->Is<ConstantIdDecoration>());
EXPECT_FALSE(d->Is<LocationDecoration>()); EXPECT_FALSE(d->Is<LocationDecoration>());
EXPECT_FALSE(cd.IsSet()); EXPECT_FALSE(d->Is<SetDecoration>());
} }
TEST_F(ConstantIdDecorationTest, ToStr) { TEST_F(ConstantIdDecorationTest, ToStr) {

View File

@ -37,7 +37,7 @@ TEST_F(LocationDecorationTest, Is) {
EXPECT_FALSE(d->Is<BuiltinDecoration>()); EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>()); EXPECT_FALSE(d->Is<ConstantIdDecoration>());
EXPECT_TRUE(d->Is<LocationDecoration>()); EXPECT_TRUE(d->Is<LocationDecoration>());
EXPECT_FALSE(ld.IsSet()); EXPECT_FALSE(d->Is<SetDecoration>());
} }
TEST_F(LocationDecorationTest, ToStr) { TEST_F(LocationDecorationTest, ToStr) {

View File

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

View File

@ -31,9 +31,6 @@ class SetDecoration : public Castable<SetDecoration, VariableDecoration> {
SetDecoration(uint32_t value, const Source& source); SetDecoration(uint32_t value, const Source& source);
~SetDecoration() override; ~SetDecoration() override;
/// @returns true if this is a set decoration
bool IsSet() const override;
/// @returns the set value /// @returns the set value
uint32_t value() const { return value_; } uint32_t value() const { return value_; }

View File

@ -35,7 +35,7 @@ TEST_F(SetDecorationTest, Is) {
EXPECT_FALSE(d->Is<BuiltinDecoration>()); EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>()); EXPECT_FALSE(d->Is<ConstantIdDecoration>());
EXPECT_FALSE(d->Is<LocationDecoration>()); EXPECT_FALSE(d->Is<LocationDecoration>());
EXPECT_TRUE(sd.IsSet()); EXPECT_TRUE(d->Is<SetDecoration>());
} }
TEST_F(SetDecorationTest, ToStr) { TEST_F(SetDecorationTest, ToStr) {

View File

@ -16,8 +16,6 @@
#include <assert.h> #include <assert.h>
#include "src/ast/set_decoration.h"
namespace tint { namespace tint {
namespace ast { namespace ast {
@ -31,14 +29,5 @@ DecorationKind VariableDecoration::GetKind() const {
return Kind; return Kind;
} }
bool VariableDecoration::IsSet() const {
return false;
}
SetDecoration* VariableDecoration::AsSet() {
assert(IsSet());
return static_cast<SetDecoration*>(this);
}
} // namespace ast } // namespace ast
} // namespace tint } // namespace tint

View File

@ -25,8 +25,6 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
class SetDecoration;
/// A decoration attached to a variable /// A decoration attached to a variable
class VariableDecoration : public Castable<VariableDecoration, Decoration> { class VariableDecoration : public Castable<VariableDecoration, Decoration> {
public: public:
@ -38,12 +36,6 @@ class VariableDecoration : public Castable<VariableDecoration, Decoration> {
/// @return the decoration kind /// @return the decoration kind
DecorationKind GetKind() const override; DecorationKind GetKind() const override;
/// @returns true if this is a set decoration
virtual bool IsSet() const;
/// @returns the decoration as a set decoration
SetDecoration* AsSet();
protected: protected:
/// Constructor /// Constructor
/// @param source the source of this decoration /// @param source the source of this decoration

View File

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

View File

@ -240,9 +240,9 @@ TEST_F(ParserImplTest, VariableDecoration_set) {
auto* var_deco = deco.value->As<ast::VariableDecoration>(); auto* var_deco = deco.value->As<ast::VariableDecoration>();
ASSERT_FALSE(p->has_error()); ASSERT_FALSE(p->has_error());
ASSERT_NE(var_deco, nullptr); ASSERT_NE(var_deco, nullptr);
ASSERT_TRUE(var_deco->IsSet()); ASSERT_TRUE(var_deco->Is<ast::SetDecoration>());
auto* set = var_deco->AsSet(); auto* set = var_deco->As<ast::SetDecoration>();
EXPECT_EQ(set->value(), 4u); EXPECT_EQ(set->value(), 4u);
} }

View File

@ -792,11 +792,11 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) {
push_annot(spv::Op::OpDecorate, push_annot(spv::Op::OpDecorate,
{Operand::Int(var_id), Operand::Int(SpvDecorationBinding), {Operand::Int(var_id), Operand::Int(SpvDecorationBinding),
Operand::Int(binding->value())}); Operand::Int(binding->value())});
} else if (deco->IsSet()) { } else if (auto* set = deco->As<ast::SetDecoration>()) {
push_annot( push_annot(
spv::Op::OpDecorate, spv::Op::OpDecorate,
{Operand::Int(var_id), Operand::Int(SpvDecorationDescriptorSet), {Operand::Int(var_id), Operand::Int(SpvDecorationDescriptorSet),
Operand::Int(deco->AsSet()->value())}); Operand::Int(set->value())});
} else if (deco->Is<ast::ConstantIdDecoration>()) { } else if (deco->Is<ast::ConstantIdDecoration>()) {
// Spec constants are handled elsewhere // Spec constants are handled elsewhere
} else { } else {

View File

@ -643,8 +643,8 @@ bool GeneratorImpl::EmitVariableDecorations(ast::DecoratedVariable* var) {
if (auto* binding = deco->As<ast::BindingDecoration>()) { if (auto* binding = deco->As<ast::BindingDecoration>()) {
out_ << "binding(" << binding->value() << ")"; out_ << "binding(" << binding->value() << ")";
} else if (deco->IsSet()) { } else if (auto* set = deco->As<ast::SetDecoration>()) {
out_ << "set(" << deco->AsSet()->value() << ")"; out_ << "set(" << set->value() << ")";
} else if (auto* location = deco->As<ast::LocationDecoration>()) { } else if (auto* location = deco->As<ast::LocationDecoration>()) {
out_ << "location(" << location->value() << ")"; out_ << "location(" << location->value() << ")";
} else if (auto* builtin = deco->As<ast::BuiltinDecoration>()) { } else if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {