Replace StructMemberDecoration::(Is|As) with Castable

Change-Id: I158194c60a9fe0ea2126ca31a92ad536c92a6388
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34314
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-30 23:30:58 +00:00
parent 8a82dcd977
commit 4d3ca7f132
14 changed files with 32 additions and 48 deletions

View File

@ -41,7 +41,7 @@ StructMember::~StructMember() = default;
bool StructMember::has_offset_decoration() const {
for (auto* deco : decorations_) {
if (deco->IsOffset()) {
if (deco->Is<ast::StructMemberOffsetDecoration>()) {
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<ast::StructMemberOffsetDecoration>()) {
return offset->offset();
}
}
return 0;

View File

@ -32,14 +32,5 @@ DecorationKind StructMemberDecoration::GetKind() const {
return Kind;
}
bool StructMemberDecoration::IsOffset() const {
return false;
}
StructMemberOffsetDecoration* StructMemberDecoration::AsOffset() {
assert(IsOffset());
return static_cast<StructMemberOffsetDecoration*>(this);
}
} // namespace ast
} // namespace tint

View File

@ -24,8 +24,6 @@
namespace tint {
namespace ast {
class StructMemberOffsetDecoration;
/// A decoration attached to a struct member
class StructMemberDecoration
: public Castable<StructMemberDecoration, Decoration> {
@ -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

View File

@ -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,

View File

@ -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_; }

View File

@ -29,7 +29,7 @@ TEST_F(StructMemberOffsetDecorationTest, Creation) {
TEST_F(StructMemberOffsetDecorationTest, Is) {
StructMemberOffsetDecoration d{2, Source{}};
EXPECT_TRUE(d.IsOffset());
EXPECT_TRUE(d.Is<ast::StructMemberOffsetDecoration>());
}
} // namespace

View File

@ -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<ast::StructMemberOffsetDecoration>());
EXPECT_EQ(st.source().range.begin.line, 0u);
EXPECT_EQ(st.source().range.begin.column, 0u);
EXPECT_EQ(st.source().range.end.line, 0u);

View File

@ -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<ast::StructMemberOffsetDecoration>());
auto* offset_deco = result->As<ast::StructMemberOffsetDecoration>();
ASSERT_NE(offset_deco, nullptr);
EXPECT_EQ(offset_deco->offset(), 8u);
EXPECT_TRUE(p->error().empty());

View File

@ -50,7 +50,7 @@ TEST_F(ParserImplTest, StructMemberDecorationDecl_Single) {
ASSERT_EQ(decos.value.size(), 1u);
auto* deco = decos.value[0]->As<ast::StructMemberDecoration>();
ASSERT_NE(deco, nullptr);
EXPECT_TRUE(deco->IsOffset());
EXPECT_TRUE(deco->Is<ast::StructMemberOffsetDecoration>());
}
TEST_F(ParserImplTest, StructMemberDecorationDecl_InvalidDecoration) {

View File

@ -32,9 +32,9 @@ TEST_F(ParserImplTest, StructMemberDecoration_Offset) {
auto* member_deco = deco.value->As<ast::StructMemberDecoration>();
ASSERT_NE(member_deco, nullptr);
ASSERT_TRUE(member_deco->IsOffset());
ASSERT_TRUE(member_deco->Is<ast::StructMemberOffsetDecoration>());
auto* o = member_deco->AsOffset();
auto* o = member_deco->As<ast::StructMemberOffsetDecoration>();
EXPECT_EQ(o->offset(), 4u);
}

View File

@ -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<ast::StructMemberOffsetDecoration>());
EXPECT_EQ(
m->decorations()[0]->As<ast::StructMemberOffsetDecoration>()->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<ast::StructMemberOffsetDecoration>());
EXPECT_EQ(
m->decorations()[0]->As<ast::StructMemberOffsetDecoration>()->offset(),
2u);
EXPECT_TRUE(m->decorations()[1]->Is<ast::StructMemberOffsetDecoration>());
EXPECT_EQ(
m->decorations()[1]->As<ast::StructMemberOffsetDecoration>()->offset(),
4u);
ASSERT_EQ(m->source().range.begin.line, 2u);
ASSERT_EQ(m->source().range.begin.column, 15u);

View File

@ -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<ast::StructMemberOffsetDecoration>()) {
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<ast::StructMemberOffsetDecoration>()) {
uint32_t offset = o->offset();
if (offset != current_offset) {
out_ << "int8_t pad_" << pad_count << "[" << (offset - current_offset)
<< "];" << std::endl;

View File

@ -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<ast::StructMemberOffsetDecoration>()) {
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";

View File

@ -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<ast::StructMemberOffsetDecoration>());
out_ << "[[offset("
<< deco->As<ast::StructMemberOffsetDecoration>()->offset() << ")]]"
<< std::endl;
}
make_indent();
out_ << mem->name() << " : ";