Add Source to sem::Struct

This CL adds a Source to the sem::Struct. The uses of
struct->Declaration()->source now use the source stored directly on the
struct.

Bug: tint:1718
Change-Id: I860c67764f85d98b3f655247e18f93fa0fef9436
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/112447
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair 2022-12-01 16:25:25 +00:00 committed by Dawn LUCI CQ
parent 8954189545
commit f745e4e2bf
9 changed files with 34 additions and 25 deletions

View File

@ -151,7 +151,7 @@ TEST_F(ResolverInferredTypeTest, InferStruct_Pass) {
auto* str = Structure("S", utils::Vector{member});
auto* expected_type =
create<sem::Struct>(str, str->name,
create<sem::Struct>(str, str->source, str->name,
sem::StructMemberList{create<sem::StructMember>(
member, member->source, member->symbol, create<sem::I32>(), 0u, 0u,
0u, 4u, std::nullopt)},

View File

@ -823,6 +823,7 @@ sem::Struct* build_struct(ProgramBuilder& b,
uint32_t size_with_padding = utils::RoundUp(max_align, offset);
return b.create<sem::Struct>(
/* declaration */ nullptr,
/* source */ Source{},
/* name */ b.Sym(name),
/* members */ members,
/* align */ max_align,

View File

@ -3299,7 +3299,7 @@ sem::Struct* Resolver::Structure(const ast::Struct* str) {
}
auto* out = builder_->create<sem::Struct>(
str, str->name, sem_members, static_cast<uint32_t>(struct_align),
str, str->source, str->name, sem_members, static_cast<uint32_t>(struct_align),
static_cast<uint32_t>(struct_size), static_cast<uint32_t>(size_no_padding));
for (size_t i = 0; i < sem_members.size(); i++) {

View File

@ -446,8 +446,7 @@ bool Validator::AddressSpaceLayout(const sem::Type* store_ty,
// Recurse into the member type.
if (!AddressSpaceLayout(m->Type(), address_space, m->Declaration()->type->source)) {
AddNote("see layout of struct:\n" + str->Layout(symbols_),
str->Declaration()->source);
AddNote("see layout of struct:\n" + str->Layout(symbols_), str->Source());
note_usage();
return false;
}
@ -463,12 +462,11 @@ bool Validator::AddressSpaceLayout(const sem::Type* store_ty,
std::to_string(required_align) + ") on this member",
m->Source());
AddNote("see layout of struct:\n" + str->Layout(symbols_),
str->Declaration()->source);
AddNote("see layout of struct:\n" + str->Layout(symbols_), str->Source());
if (auto* member_str = m->Type()->As<sem::Struct>()) {
AddNote("and layout of struct member:\n" + member_str->Layout(symbols_),
member_str->Declaration()->source);
member_str->Source());
}
note_usage();
@ -490,13 +488,12 @@ bool Validator::AddressSpaceLayout(const sem::Type* store_ty,
"'. Consider setting @align(16) on this member",
m->Source());
AddNote("see layout of struct:\n" + str->Layout(symbols_),
str->Declaration()->source);
AddNote("see layout of struct:\n" + str->Layout(symbols_), str->Source());
auto* prev_member_str = prev_member->Type()->As<sem::Struct>();
AddNote("and layout of previous member struct:\n" +
prev_member_str->Layout(symbols_),
prev_member_str->Declaration()->source);
prev_member_str->Source());
note_usage();
return false;
}
@ -2017,7 +2014,7 @@ bool Validator::Alias(const ast::Alias*) const {
bool Validator::Structure(const sem::Struct* str, ast::PipelineStage stage) const {
if (str->Members().empty()) {
AddError("structures must have at least one member", str->Declaration()->source);
AddError("structures must have at least one member", str->Source());
return false;
}

View File

@ -52,6 +52,7 @@ TypeFlags FlagsFrom(const StructMemberList& members) {
} // namespace
Struct::Struct(const ast::Struct* declaration,
tint::Source source,
Symbol name,
StructMemberList members,
uint32_t align,
@ -59,6 +60,7 @@ Struct::Struct(const ast::Struct* declaration,
uint32_t size_no_padding)
: Base(FlagsFrom(members)),
declaration_(declaration),
source_(source),
name_(name),
members_(std::move(members)),
align_(align),

View File

@ -57,6 +57,7 @@ class Struct final : public Castable<Struct, Type> {
public:
/// Constructor
/// @param declaration the AST structure declaration
/// @param source the source of the structure
/// @param name the name of the structure
/// @param members the structure members
/// @param align the byte alignment of the structure
@ -64,6 +65,7 @@ class Struct final : public Castable<Struct, Type> {
/// @param size_no_padding size of the members without the end of structure
/// alignment padding
Struct(const ast::Struct* declaration,
tint::Source source,
Symbol name,
StructMemberList members,
uint32_t align,
@ -83,6 +85,9 @@ class Struct final : public Castable<Struct, Type> {
/// @returns the struct
const ast::Struct* Declaration() const { return declaration_; }
/// @returns the source of the structure
tint::Source Source() const { return source_; }
/// @returns the name of the structure
Symbol Name() const { return name_; }
@ -162,6 +167,7 @@ class Struct final : public Castable<Struct, Type> {
private:
ast::Struct const* const declaration_;
const tint::Source source_;
const Symbol name_;
const StructMemberList members_;
const uint32_t align_;

View File

@ -26,8 +26,8 @@ TEST_F(StructTest, Creation) {
auto name = Sym("S");
auto* impl = create<ast::Struct>(name, utils::Empty, utils::Empty);
auto* ptr = impl;
auto* s = create<sem::Struct>(impl, impl->name, StructMemberList{}, 4u /* align */,
8u /* size */, 16u /* size_no_padding */);
auto* s = create<sem::Struct>(impl, impl->source, impl->name, StructMemberList{},
4u /* align */, 8u /* size */, 16u /* size_no_padding */);
EXPECT_EQ(s->Declaration(), ptr);
EXPECT_EQ(s->Align(), 4u);
EXPECT_EQ(s->Size(), 8u);
@ -36,22 +36,22 @@ TEST_F(StructTest, Creation) {
TEST_F(StructTest, Hash) {
auto* a_impl = create<ast::Struct>(Sym("a"), utils::Empty, utils::Empty);
auto* a = create<sem::Struct>(a_impl, a_impl->name, StructMemberList{}, 4u /* align */,
4u /* size */, 4u /* size_no_padding */);
auto* a = create<sem::Struct>(a_impl, a_impl->source, a_impl->name, StructMemberList{},
4u /* align */, 4u /* size */, 4u /* size_no_padding */);
auto* b_impl = create<ast::Struct>(Sym("b"), utils::Empty, utils::Empty);
auto* b = create<sem::Struct>(b_impl, b_impl->name, StructMemberList{}, 4u /* align */,
4u /* size */, 4u /* size_no_padding */);
auto* b = create<sem::Struct>(b_impl, b_impl->source, b_impl->name, StructMemberList{},
4u /* align */, 4u /* size */, 4u /* size_no_padding */);
EXPECT_NE(a->Hash(), b->Hash());
}
TEST_F(StructTest, Equals) {
auto* a_impl = create<ast::Struct>(Sym("a"), utils::Empty, utils::Empty);
auto* a = create<sem::Struct>(a_impl, a_impl->name, StructMemberList{}, 4u /* align */,
4u /* size */, 4u /* size_no_padding */);
auto* a = create<sem::Struct>(a_impl, a_impl->source, a_impl->name, StructMemberList{},
4u /* align */, 4u /* size */, 4u /* size_no_padding */);
auto* b_impl = create<ast::Struct>(Sym("b"), utils::Empty, utils::Empty);
auto* b = create<sem::Struct>(b_impl, b_impl->name, StructMemberList{}, 4u /* align */,
4u /* size */, 4u /* size_no_padding */);
auto* b = create<sem::Struct>(b_impl, b_impl->source, b_impl->name, StructMemberList{},
4u /* align */, 4u /* size */, 4u /* size_no_padding */);
EXPECT_TRUE(a->Equals(*a));
EXPECT_FALSE(a->Equals(*b));
@ -61,8 +61,8 @@ TEST_F(StructTest, Equals) {
TEST_F(StructTest, FriendlyName) {
auto name = Sym("my_struct");
auto* impl = create<ast::Struct>(name, utils::Empty, utils::Empty);
auto* s = create<sem::Struct>(impl, impl->name, StructMemberList{}, 4u /* align */,
4u /* size */, 4u /* size_no_padding */);
auto* s = create<sem::Struct>(impl, impl->source, impl->name, StructMemberList{},
4u /* align */, 4u /* size */, 4u /* size_no_padding */);
EXPECT_EQ(s->FriendlyName(Symbols()), "my_struct");
}

View File

@ -45,6 +45,7 @@ struct TypeTest : public TestHelper {
const sem::Reference* ref_u32 =
create<Reference>(u32, ast::AddressSpace::kPrivate, ast::Access::kReadWrite);
const sem::Struct* str_f32 = create<Struct>(nullptr,
Source{},
Sym("str_f32"),
StructMemberList{
create<StructMember>(
@ -62,6 +63,7 @@ struct TypeTest : public TestHelper {
/* size*/ 4u,
/* size_no_padding*/ 4u);
const sem::Struct* str_f16 = create<Struct>(nullptr,
Source{},
Sym("str_f16"),
StructMemberList{
create<StructMember>(
@ -79,6 +81,7 @@ struct TypeTest : public TestHelper {
/* size*/ 4u,
/* size_no_padding*/ 4u);
sem::Struct* str_af = create<Struct>(nullptr,
Source{},
Sym("str_af"),
StructMemberList{
create<StructMember>(

View File

@ -122,8 +122,8 @@ TEST_F(CreateASTTypeForTest, AliasedArrayWithComplexOverrideLength) {
TEST_F(CreateASTTypeForTest, Struct) {
auto* str = create([](ProgramBuilder& b) {
auto* decl = b.Structure("S", {});
return b.create<sem::Struct>(decl, decl->name, sem::StructMemberList{}, 4u /* align */,
4u /* size */, 4u /* size_no_padding */);
return b.create<sem::Struct>(decl, decl->source, decl->name, sem::StructMemberList{},
4u /* align */, 4u /* size */, 4u /* size_no_padding */);
});
ASSERT_TRUE(str->Is<ast::TypeName>());
EXPECT_EQ(ast_type_builder.Symbols().NameFor(str->As<ast::TypeName>()->name), "S");