diff --git a/src/ast/struct_member.cc b/src/ast/struct_member.cc index 629323482c..ea5223ce36 100644 --- a/src/ast/struct_member.cc +++ b/src/ast/struct_member.cc @@ -41,7 +41,7 @@ StructMember::~StructMember() = default; bool StructMember::has_offset_decoration() const { for (auto* deco : decorations_) { - if (deco->IsOffset()) { + if (deco->Is()) { return true; } } @@ -50,8 +50,8 @@ bool StructMember::has_offset_decoration() const { uint32_t StructMember::offset() const { for (auto* deco : decorations_) { - if (deco->IsOffset()) { - return deco->AsOffset()->offset(); + if (auto* offset = deco->As()) { + return offset->offset(); } } return 0; diff --git a/src/ast/struct_member_decoration.cc b/src/ast/struct_member_decoration.cc index 9c3118d9ce..353f1ca5d3 100644 --- a/src/ast/struct_member_decoration.cc +++ b/src/ast/struct_member_decoration.cc @@ -32,14 +32,5 @@ DecorationKind StructMemberDecoration::GetKind() const { return Kind; } -bool StructMemberDecoration::IsOffset() const { - return false; -} - -StructMemberOffsetDecoration* StructMemberDecoration::AsOffset() { - assert(IsOffset()); - return static_cast(this); -} - } // namespace ast } // namespace tint diff --git a/src/ast/struct_member_decoration.h b/src/ast/struct_member_decoration.h index 837ecc0756..6a0bf18dd0 100644 --- a/src/ast/struct_member_decoration.h +++ b/src/ast/struct_member_decoration.h @@ -24,8 +24,6 @@ namespace tint { namespace ast { -class StructMemberOffsetDecoration; - /// A decoration attached to a struct member class StructMemberDecoration : public Castable { @@ -38,12 +36,6 @@ class StructMemberDecoration /// @return the decoration kind DecorationKind GetKind() const override; - /// @returns true if this is an offset decoration - virtual bool IsOffset() const; - - /// @returns the decoration as an offset decoration - StructMemberOffsetDecoration* AsOffset(); - protected: /// Constructor /// @param source the source of this decoration diff --git a/src/ast/struct_member_offset_decoration.cc b/src/ast/struct_member_offset_decoration.cc index f1ceb43b98..91ed01b9d6 100644 --- a/src/ast/struct_member_offset_decoration.cc +++ b/src/ast/struct_member_offset_decoration.cc @@ -21,10 +21,6 @@ StructMemberOffsetDecoration::StructMemberOffsetDecoration(uint32_t offset, const Source& source) : Base(source), offset_(offset) {} -bool StructMemberOffsetDecoration::IsOffset() const { - return true; -} - StructMemberOffsetDecoration::~StructMemberOffsetDecoration() = default; void StructMemberOffsetDecoration::to_str(std::ostream& out, diff --git a/src/ast/struct_member_offset_decoration.h b/src/ast/struct_member_offset_decoration.h index 21c2cf4758..1f442715ec 100644 --- a/src/ast/struct_member_offset_decoration.h +++ b/src/ast/struct_member_offset_decoration.h @@ -34,9 +34,6 @@ class StructMemberOffsetDecoration StructMemberOffsetDecoration(uint32_t offset, const Source& source); ~StructMemberOffsetDecoration() override; - /// @returns true if this is an offset decoration - bool IsOffset() const override; - /// @returns the offset value uint32_t offset() const { return offset_; } diff --git a/src/ast/struct_member_offset_decoration_test.cc b/src/ast/struct_member_offset_decoration_test.cc index fb96a3b64c..51fab732fe 100644 --- a/src/ast/struct_member_offset_decoration_test.cc +++ b/src/ast/struct_member_offset_decoration_test.cc @@ -29,7 +29,7 @@ TEST_F(StructMemberOffsetDecorationTest, Creation) { TEST_F(StructMemberOffsetDecorationTest, Is) { StructMemberOffsetDecoration d{2, Source{}}; - EXPECT_TRUE(d.IsOffset()); + EXPECT_TRUE(d.Is()); } } // namespace diff --git a/src/ast/struct_member_test.cc b/src/ast/struct_member_test.cc index a7fe388713..729c11afb4 100644 --- a/src/ast/struct_member_test.cc +++ b/src/ast/struct_member_test.cc @@ -36,7 +36,7 @@ TEST_F(StructMemberTest, Creation) { EXPECT_EQ(st.name(), "a"); EXPECT_EQ(st.type(), &i32); EXPECT_EQ(st.decorations().size(), 1u); - EXPECT_TRUE(st.decorations()[0]->IsOffset()); + EXPECT_TRUE(st.decorations()[0]->Is()); EXPECT_EQ(st.source().range.begin.line, 0u); EXPECT_EQ(st.source().range.begin.column, 0u); EXPECT_EQ(st.source().range.end.line, 0u); diff --git a/src/reader/spirv/parser_impl_convert_member_decoration_test.cc b/src/reader/spirv/parser_impl_convert_member_decoration_test.cc index 9f95d0a394..0551710772 100644 --- a/src/reader/spirv/parser_impl_convert_member_decoration_test.cc +++ b/src/reader/spirv/parser_impl_convert_member_decoration_test.cc @@ -63,8 +63,8 @@ TEST_F(SpvParserTest, ConvertMemberDecoration_Offset) { auto* result = p->ConvertMemberDecoration(1, 1, {SpvDecorationOffset, 8}); ASSERT_NE(result, nullptr); - EXPECT_TRUE(result->IsOffset()); - auto* offset_deco = result->AsOffset(); + EXPECT_TRUE(result->Is()); + auto* offset_deco = result->As(); ASSERT_NE(offset_deco, nullptr); EXPECT_EQ(offset_deco->offset(), 8u); EXPECT_TRUE(p->error().empty()); diff --git a/src/reader/wgsl/parser_impl_struct_member_decoration_decl_test.cc b/src/reader/wgsl/parser_impl_struct_member_decoration_decl_test.cc index 6d0a3174e6..9d9081be28 100644 --- a/src/reader/wgsl/parser_impl_struct_member_decoration_decl_test.cc +++ b/src/reader/wgsl/parser_impl_struct_member_decoration_decl_test.cc @@ -50,7 +50,7 @@ TEST_F(ParserImplTest, StructMemberDecorationDecl_Single) { ASSERT_EQ(decos.value.size(), 1u); auto* deco = decos.value[0]->As(); ASSERT_NE(deco, nullptr); - EXPECT_TRUE(deco->IsOffset()); + EXPECT_TRUE(deco->Is()); } TEST_F(ParserImplTest, StructMemberDecorationDecl_InvalidDecoration) { diff --git a/src/reader/wgsl/parser_impl_struct_member_decoration_test.cc b/src/reader/wgsl/parser_impl_struct_member_decoration_test.cc index c9f235784f..450c3a2fbd 100644 --- a/src/reader/wgsl/parser_impl_struct_member_decoration_test.cc +++ b/src/reader/wgsl/parser_impl_struct_member_decoration_test.cc @@ -32,9 +32,9 @@ TEST_F(ParserImplTest, StructMemberDecoration_Offset) { auto* member_deco = deco.value->As(); ASSERT_NE(member_deco, nullptr); - ASSERT_TRUE(member_deco->IsOffset()); + ASSERT_TRUE(member_deco->Is()); - auto* o = member_deco->AsOffset(); + auto* o = member_deco->As(); EXPECT_EQ(o->offset(), 4u); } diff --git a/src/reader/wgsl/parser_impl_struct_member_test.cc b/src/reader/wgsl/parser_impl_struct_member_test.cc index 4e9898047c..f4643d3bfd 100644 --- a/src/reader/wgsl/parser_impl_struct_member_test.cc +++ b/src/reader/wgsl/parser_impl_struct_member_test.cc @@ -68,8 +68,10 @@ TEST_F(ParserImplTest, StructMember_ParsesWithDecoration) { EXPECT_EQ(m->name(), "a"); EXPECT_EQ(m->type(), i32); EXPECT_EQ(m->decorations().size(), 1u); - EXPECT_TRUE(m->decorations()[0]->IsOffset()); - EXPECT_EQ(m->decorations()[0]->AsOffset()->offset(), 2u); + EXPECT_TRUE(m->decorations()[0]->Is()); + EXPECT_EQ( + m->decorations()[0]->As()->offset(), + 2u); ASSERT_EQ(m->source().range.begin.line, 1u); ASSERT_EQ(m->source().range.begin.column, 15u); @@ -97,10 +99,14 @@ TEST_F(ParserImplTest, StructMember_ParsesWithMultipleDecorations) { EXPECT_EQ(m->name(), "a"); EXPECT_EQ(m->type(), i32); EXPECT_EQ(m->decorations().size(), 2u); - EXPECT_TRUE(m->decorations()[0]->IsOffset()); - EXPECT_EQ(m->decorations()[0]->AsOffset()->offset(), 2u); - EXPECT_TRUE(m->decorations()[1]->IsOffset()); - EXPECT_EQ(m->decorations()[1]->AsOffset()->offset(), 4u); + EXPECT_TRUE(m->decorations()[0]->Is()); + EXPECT_EQ( + m->decorations()[0]->As()->offset(), + 2u); + EXPECT_TRUE(m->decorations()[1]->Is()); + EXPECT_EQ( + m->decorations()[1]->As()->offset(), + 4u); ASSERT_EQ(m->source().range.begin.line, 2u); ASSERT_EQ(m->source().range.begin.column, 15u); diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index d7d2fb29c5..5fb958cafd 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -218,8 +218,8 @@ uint32_t GeneratorImpl::calculate_alignment_size(ast::type::Type* type) { // Offset decorations in WGSL must be in increasing order. for (auto* mem : stct->members()) { for (auto* deco : mem->decorations()) { - if (deco->IsOffset()) { - count = deco->AsOffset()->offset(); + if (auto* offset = deco->As()) { + count = offset->offset(); } } auto align = calculate_alignment_size(mem->type()); @@ -1941,8 +1941,8 @@ bool GeneratorImpl::EmitStructType(const ast::type::StructType* str) { for (auto* mem : str->impl()->members()) { make_indent(); for (auto* deco : mem->decorations()) { - if (deco->IsOffset()) { - uint32_t offset = deco->AsOffset()->offset(); + if (auto* o = deco->As()) { + uint32_t offset = o->offset(); if (offset != current_offset) { out_ << "int8_t pad_" << pad_count << "[" << (offset - current_offset) << "];" << std::endl; diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index 0c1c1d5688..54871205c9 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -2696,11 +2696,11 @@ uint32_t Builder::GenerateStructMember(uint32_t struct_id, bool has_layout = false; for (auto* deco : member->decorations()) { - if (deco->IsOffset()) { + if (auto* offset = deco->As()) { push_annot(spv::Op::OpMemberDecorate, {Operand::Int(struct_id), Operand::Int(idx), Operand::Int(SpvDecorationOffset), - Operand::Int(deco->AsOffset()->offset())}); + Operand::Int(offset->offset())}); has_layout = true; } else { error_ = "unknown struct member decoration"; diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index a417837e20..bcff24f0cf 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -580,8 +580,10 @@ bool GeneratorImpl::EmitStructType(const ast::type::StructType* str) { make_indent(); // TODO(dsinclair): Split this out when we have more then one - assert(deco->IsOffset()); - out_ << "[[offset(" << deco->AsOffset()->offset() << ")]]" << std::endl; + assert(deco->Is()); + out_ << "[[offset(" + << deco->As()->offset() << ")]]" + << std::endl; } make_indent(); out_ << mem->name() << " : ";