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:
parent
8a82dcd977
commit
4d3ca7f132
|
@ -41,7 +41,7 @@ StructMember::~StructMember() = default;
|
||||||
|
|
||||||
bool StructMember::has_offset_decoration() const {
|
bool StructMember::has_offset_decoration() const {
|
||||||
for (auto* deco : decorations_) {
|
for (auto* deco : decorations_) {
|
||||||
if (deco->IsOffset()) {
|
if (deco->Is<ast::StructMemberOffsetDecoration>()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,8 +50,8 @@ bool StructMember::has_offset_decoration() const {
|
||||||
|
|
||||||
uint32_t StructMember::offset() const {
|
uint32_t StructMember::offset() const {
|
||||||
for (auto* deco : decorations_) {
|
for (auto* deco : decorations_) {
|
||||||
if (deco->IsOffset()) {
|
if (auto* offset = deco->As<ast::StructMemberOffsetDecoration>()) {
|
||||||
return deco->AsOffset()->offset();
|
return offset->offset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -32,14 +32,5 @@ DecorationKind StructMemberDecoration::GetKind() const {
|
||||||
return Kind;
|
return Kind;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StructMemberDecoration::IsOffset() const {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
StructMemberOffsetDecoration* StructMemberDecoration::AsOffset() {
|
|
||||||
assert(IsOffset());
|
|
||||||
return static_cast<StructMemberOffsetDecoration*>(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
} // namespace tint
|
} // namespace tint
|
||||||
|
|
|
@ -24,8 +24,6 @@
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace ast {
|
namespace ast {
|
||||||
|
|
||||||
class StructMemberOffsetDecoration;
|
|
||||||
|
|
||||||
/// A decoration attached to a struct member
|
/// A decoration attached to a struct member
|
||||||
class StructMemberDecoration
|
class StructMemberDecoration
|
||||||
: public Castable<StructMemberDecoration, Decoration> {
|
: public Castable<StructMemberDecoration, Decoration> {
|
||||||
|
@ -38,12 +36,6 @@ class StructMemberDecoration
|
||||||
/// @return the decoration kind
|
/// @return the decoration kind
|
||||||
DecorationKind GetKind() const override;
|
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:
|
protected:
|
||||||
/// Constructor
|
/// Constructor
|
||||||
/// @param source the source of this decoration
|
/// @param source the source of this decoration
|
||||||
|
|
|
@ -21,10 +21,6 @@ StructMemberOffsetDecoration::StructMemberOffsetDecoration(uint32_t offset,
|
||||||
const Source& source)
|
const Source& source)
|
||||||
: Base(source), offset_(offset) {}
|
: Base(source), offset_(offset) {}
|
||||||
|
|
||||||
bool StructMemberOffsetDecoration::IsOffset() const {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
StructMemberOffsetDecoration::~StructMemberOffsetDecoration() = default;
|
StructMemberOffsetDecoration::~StructMemberOffsetDecoration() = default;
|
||||||
|
|
||||||
void StructMemberOffsetDecoration::to_str(std::ostream& out,
|
void StructMemberOffsetDecoration::to_str(std::ostream& out,
|
||||||
|
|
|
@ -34,9 +34,6 @@ class StructMemberOffsetDecoration
|
||||||
StructMemberOffsetDecoration(uint32_t offset, const Source& source);
|
StructMemberOffsetDecoration(uint32_t offset, const Source& source);
|
||||||
~StructMemberOffsetDecoration() override;
|
~StructMemberOffsetDecoration() override;
|
||||||
|
|
||||||
/// @returns true if this is an offset decoration
|
|
||||||
bool IsOffset() const override;
|
|
||||||
|
|
||||||
/// @returns the offset value
|
/// @returns the offset value
|
||||||
uint32_t offset() const { return offset_; }
|
uint32_t offset() const { return offset_; }
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ TEST_F(StructMemberOffsetDecorationTest, Creation) {
|
||||||
|
|
||||||
TEST_F(StructMemberOffsetDecorationTest, Is) {
|
TEST_F(StructMemberOffsetDecorationTest, Is) {
|
||||||
StructMemberOffsetDecoration d{2, Source{}};
|
StructMemberOffsetDecoration d{2, Source{}};
|
||||||
EXPECT_TRUE(d.IsOffset());
|
EXPECT_TRUE(d.Is<ast::StructMemberOffsetDecoration>());
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
@ -36,7 +36,7 @@ TEST_F(StructMemberTest, Creation) {
|
||||||
EXPECT_EQ(st.name(), "a");
|
EXPECT_EQ(st.name(), "a");
|
||||||
EXPECT_EQ(st.type(), &i32);
|
EXPECT_EQ(st.type(), &i32);
|
||||||
EXPECT_EQ(st.decorations().size(), 1u);
|
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.line, 0u);
|
||||||
EXPECT_EQ(st.source().range.begin.column, 0u);
|
EXPECT_EQ(st.source().range.begin.column, 0u);
|
||||||
EXPECT_EQ(st.source().range.end.line, 0u);
|
EXPECT_EQ(st.source().range.end.line, 0u);
|
||||||
|
|
|
@ -63,8 +63,8 @@ TEST_F(SpvParserTest, ConvertMemberDecoration_Offset) {
|
||||||
|
|
||||||
auto* result = p->ConvertMemberDecoration(1, 1, {SpvDecorationOffset, 8});
|
auto* result = p->ConvertMemberDecoration(1, 1, {SpvDecorationOffset, 8});
|
||||||
ASSERT_NE(result, nullptr);
|
ASSERT_NE(result, nullptr);
|
||||||
EXPECT_TRUE(result->IsOffset());
|
EXPECT_TRUE(result->Is<ast::StructMemberOffsetDecoration>());
|
||||||
auto* offset_deco = result->AsOffset();
|
auto* offset_deco = result->As<ast::StructMemberOffsetDecoration>();
|
||||||
ASSERT_NE(offset_deco, nullptr);
|
ASSERT_NE(offset_deco, nullptr);
|
||||||
EXPECT_EQ(offset_deco->offset(), 8u);
|
EXPECT_EQ(offset_deco->offset(), 8u);
|
||||||
EXPECT_TRUE(p->error().empty());
|
EXPECT_TRUE(p->error().empty());
|
||||||
|
|
|
@ -50,7 +50,7 @@ TEST_F(ParserImplTest, StructMemberDecorationDecl_Single) {
|
||||||
ASSERT_EQ(decos.value.size(), 1u);
|
ASSERT_EQ(decos.value.size(), 1u);
|
||||||
auto* deco = decos.value[0]->As<ast::StructMemberDecoration>();
|
auto* deco = decos.value[0]->As<ast::StructMemberDecoration>();
|
||||||
ASSERT_NE(deco, nullptr);
|
ASSERT_NE(deco, nullptr);
|
||||||
EXPECT_TRUE(deco->IsOffset());
|
EXPECT_TRUE(deco->Is<ast::StructMemberOffsetDecoration>());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ParserImplTest, StructMemberDecorationDecl_InvalidDecoration) {
|
TEST_F(ParserImplTest, StructMemberDecorationDecl_InvalidDecoration) {
|
||||||
|
|
|
@ -32,9 +32,9 @@ TEST_F(ParserImplTest, StructMemberDecoration_Offset) {
|
||||||
|
|
||||||
auto* member_deco = deco.value->As<ast::StructMemberDecoration>();
|
auto* member_deco = deco.value->As<ast::StructMemberDecoration>();
|
||||||
ASSERT_NE(member_deco, nullptr);
|
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);
|
EXPECT_EQ(o->offset(), 4u);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -68,8 +68,10 @@ TEST_F(ParserImplTest, StructMember_ParsesWithDecoration) {
|
||||||
EXPECT_EQ(m->name(), "a");
|
EXPECT_EQ(m->name(), "a");
|
||||||
EXPECT_EQ(m->type(), i32);
|
EXPECT_EQ(m->type(), i32);
|
||||||
EXPECT_EQ(m->decorations().size(), 1u);
|
EXPECT_EQ(m->decorations().size(), 1u);
|
||||||
EXPECT_TRUE(m->decorations()[0]->IsOffset());
|
EXPECT_TRUE(m->decorations()[0]->Is<ast::StructMemberOffsetDecoration>());
|
||||||
EXPECT_EQ(m->decorations()[0]->AsOffset()->offset(), 2u);
|
EXPECT_EQ(
|
||||||
|
m->decorations()[0]->As<ast::StructMemberOffsetDecoration>()->offset(),
|
||||||
|
2u);
|
||||||
|
|
||||||
ASSERT_EQ(m->source().range.begin.line, 1u);
|
ASSERT_EQ(m->source().range.begin.line, 1u);
|
||||||
ASSERT_EQ(m->source().range.begin.column, 15u);
|
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->name(), "a");
|
||||||
EXPECT_EQ(m->type(), i32);
|
EXPECT_EQ(m->type(), i32);
|
||||||
EXPECT_EQ(m->decorations().size(), 2u);
|
EXPECT_EQ(m->decorations().size(), 2u);
|
||||||
EXPECT_TRUE(m->decorations()[0]->IsOffset());
|
EXPECT_TRUE(m->decorations()[0]->Is<ast::StructMemberOffsetDecoration>());
|
||||||
EXPECT_EQ(m->decorations()[0]->AsOffset()->offset(), 2u);
|
EXPECT_EQ(
|
||||||
EXPECT_TRUE(m->decorations()[1]->IsOffset());
|
m->decorations()[0]->As<ast::StructMemberOffsetDecoration>()->offset(),
|
||||||
EXPECT_EQ(m->decorations()[1]->AsOffset()->offset(), 4u);
|
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.line, 2u);
|
||||||
ASSERT_EQ(m->source().range.begin.column, 15u);
|
ASSERT_EQ(m->source().range.begin.column, 15u);
|
||||||
|
|
|
@ -218,8 +218,8 @@ uint32_t GeneratorImpl::calculate_alignment_size(ast::type::Type* type) {
|
||||||
// Offset decorations in WGSL must be in increasing order.
|
// Offset decorations in WGSL must be in increasing order.
|
||||||
for (auto* mem : stct->members()) {
|
for (auto* mem : stct->members()) {
|
||||||
for (auto* deco : mem->decorations()) {
|
for (auto* deco : mem->decorations()) {
|
||||||
if (deco->IsOffset()) {
|
if (auto* offset = deco->As<ast::StructMemberOffsetDecoration>()) {
|
||||||
count = deco->AsOffset()->offset();
|
count = offset->offset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto align = calculate_alignment_size(mem->type());
|
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()) {
|
for (auto* mem : str->impl()->members()) {
|
||||||
make_indent();
|
make_indent();
|
||||||
for (auto* deco : mem->decorations()) {
|
for (auto* deco : mem->decorations()) {
|
||||||
if (deco->IsOffset()) {
|
if (auto* o = deco->As<ast::StructMemberOffsetDecoration>()) {
|
||||||
uint32_t offset = deco->AsOffset()->offset();
|
uint32_t offset = o->offset();
|
||||||
if (offset != current_offset) {
|
if (offset != current_offset) {
|
||||||
out_ << "int8_t pad_" << pad_count << "[" << (offset - current_offset)
|
out_ << "int8_t pad_" << pad_count << "[" << (offset - current_offset)
|
||||||
<< "];" << std::endl;
|
<< "];" << std::endl;
|
||||||
|
|
|
@ -2696,11 +2696,11 @@ uint32_t Builder::GenerateStructMember(uint32_t struct_id,
|
||||||
|
|
||||||
bool has_layout = false;
|
bool has_layout = false;
|
||||||
for (auto* deco : member->decorations()) {
|
for (auto* deco : member->decorations()) {
|
||||||
if (deco->IsOffset()) {
|
if (auto* offset = deco->As<ast::StructMemberOffsetDecoration>()) {
|
||||||
push_annot(spv::Op::OpMemberDecorate,
|
push_annot(spv::Op::OpMemberDecorate,
|
||||||
{Operand::Int(struct_id), Operand::Int(idx),
|
{Operand::Int(struct_id), Operand::Int(idx),
|
||||||
Operand::Int(SpvDecorationOffset),
|
Operand::Int(SpvDecorationOffset),
|
||||||
Operand::Int(deco->AsOffset()->offset())});
|
Operand::Int(offset->offset())});
|
||||||
has_layout = true;
|
has_layout = true;
|
||||||
} else {
|
} else {
|
||||||
error_ = "unknown struct member decoration";
|
error_ = "unknown struct member decoration";
|
||||||
|
|
|
@ -580,8 +580,10 @@ bool GeneratorImpl::EmitStructType(const ast::type::StructType* str) {
|
||||||
make_indent();
|
make_indent();
|
||||||
|
|
||||||
// TODO(dsinclair): Split this out when we have more then one
|
// TODO(dsinclair): Split this out when we have more then one
|
||||||
assert(deco->IsOffset());
|
assert(deco->Is<ast::StructMemberOffsetDecoration>());
|
||||||
out_ << "[[offset(" << deco->AsOffset()->offset() << ")]]" << std::endl;
|
out_ << "[[offset("
|
||||||
|
<< deco->As<ast::StructMemberOffsetDecoration>()->offset() << ")]]"
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
make_indent();
|
make_indent();
|
||||||
out_ << mem->name() << " : ";
|
out_ << mem->name() << " : ";
|
||||||
|
|
Loading…
Reference in New Issue