[ast] Require StructType to have a name

This CL moves the StructType name into the constructor of the struct
type instead of receiving through an accessor. The |set_name| accessor
is removed as it should not be needed anymore. All call sites have been
updated.

The vertex pulling transform was fixed to correctly register an alias
for the structure being created so it will be emitted.

Bug: tint:175
Change-Id: I8802931d9bdbc6c2f12982eea9032931939d195c
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/30280
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair
2020-10-15 17:54:13 +00:00
committed by Commit Bot service account
parent 53380f9ed7
commit 481ecff293
27 changed files with 190 additions and 174 deletions

View File

@@ -864,12 +864,11 @@ ast::type::Type* ParserImpl::ConvertType(
// Now make the struct.
auto ast_struct = std::make_unique<ast::Struct>(
std::move(ast_struct_decorations), std::move(ast_members));
// The struct type will be assigned a name during EmitAliasTypes.
auto ast_struct_type =
std::make_unique<ast::type::StructType>(std::move(ast_struct));
// Set the struct name before registering it.
namer_.SuggestSanitizedName(type_id, "S");
ast_struct_type->set_name(namer_.GetName(type_id));
auto ast_struct_type = std::make_unique<ast::type::StructType>(
namer_.GetName(type_id), std::move(ast_struct));
auto* result = ctx_.type_mgr().Get(std::move(ast_struct_type));
return result;
}

View File

@@ -1083,7 +1083,7 @@ ast::type::AliasType* ParserImpl::type_alias() {
if (has_error())
return nullptr;
if (type == nullptr) {
auto str = struct_decl();
auto str = struct_decl(name);
if (has_error())
return nullptr;
if (str == nullptr) {
@@ -1091,7 +1091,6 @@ ast::type::AliasType* ParserImpl::type_alias() {
return nullptr;
}
str->set_name(name);
type = ctx_.type_mgr().Get(std::move(str));
}
if (type == nullptr) {
@@ -1479,7 +1478,8 @@ ast::StorageClass ParserImpl::storage_class() {
// struct_decl
// : struct_decoration_decl* STRUCT struct_body_decl
std::unique_ptr<ast::type::StructType> ParserImpl::struct_decl() {
std::unique_ptr<ast::type::StructType> ParserImpl::struct_decl(
const std::string& name) {
auto t = peek();
auto source = t.source();
@@ -1506,6 +1506,7 @@ std::unique_ptr<ast::type::StructType> ParserImpl::struct_decl() {
}
return std::make_unique<ast::type::StructType>(
name,
std::make_unique<ast::Struct>(source, std::move(decos), std::move(body)));
}

View File

@@ -156,8 +156,9 @@ class ParserImpl {
/// @returns the storage class or StorageClass::kNone if none matched
ast::StorageClass storage_class();
/// Parses a `struct_decl` grammar element
/// @param name the name of the struct
/// @returns the struct type or nullptr on error
std::unique_ptr<ast::type::StructType> struct_decl();
std::unique_ptr<ast::type::StructType> struct_decl(const std::string& name);
/// Parses a `struct_decoration_decl` grammar element, appending newly
/// parsed decorations to the end of |decos|.
/// @param decos list to store the parsed decorations

View File

@@ -28,9 +28,10 @@ struct {
a : i32;
[[offset(4)]] b : f32;
})");
auto s = p->struct_decl();
auto s = p->struct_decl("S");
ASSERT_FALSE(p->has_error());
ASSERT_NE(s, nullptr);
ASSERT_EQ(s->name(), "S");
ASSERT_EQ(s->impl()->members().size(), 2u);
EXPECT_EQ(s->impl()->members()[0]->name(), "a");
EXPECT_EQ(s->impl()->members()[1]->name(), "b");
@@ -42,9 +43,10 @@ TEST_F(ParserImplTest, StructDecl_ParsesWithDecoration) {
a : f32;
b : f32;
})");
auto s = p->struct_decl();
auto s = p->struct_decl("B");
ASSERT_FALSE(p->has_error());
ASSERT_NE(s, nullptr);
ASSERT_EQ(s->name(), "B");
ASSERT_EQ(s->impl()->members().size(), 2u);
EXPECT_EQ(s->impl()->members()[0]->name(), "a");
EXPECT_EQ(s->impl()->members()[1]->name(), "b");
@@ -59,9 +61,10 @@ TEST_F(ParserImplTest, StructDecl_ParsesWithMultipleDecoration) {
a : f32;
b : f32;
})");
auto s = p->struct_decl();
auto s = p->struct_decl("S");
ASSERT_FALSE(p->has_error());
ASSERT_NE(s, nullptr);
ASSERT_EQ(s->name(), "S");
ASSERT_EQ(s->impl()->members().size(), 2u);
EXPECT_EQ(s->impl()->members()[0]->name(), "a");
EXPECT_EQ(s->impl()->members()[1]->name(), "b");
@@ -72,7 +75,7 @@ TEST_F(ParserImplTest, StructDecl_ParsesWithMultipleDecoration) {
TEST_F(ParserImplTest, StructDecl_EmptyMembers) {
auto* p = parser("struct {}");
auto s = p->struct_decl();
auto s = p->struct_decl("S");
ASSERT_FALSE(p->has_error());
ASSERT_NE(s, nullptr);
ASSERT_EQ(s->impl()->members().size(), 0u);
@@ -80,7 +83,7 @@ TEST_F(ParserImplTest, StructDecl_EmptyMembers) {
TEST_F(ParserImplTest, StructDecl_MissingBracketLeft) {
auto* p = parser("struct }");
auto s = p->struct_decl();
auto s = p->struct_decl("S");
ASSERT_TRUE(p->has_error());
ASSERT_EQ(s, nullptr);
EXPECT_EQ(p->error(), "1:8: missing { for struct declaration");
@@ -88,7 +91,7 @@ TEST_F(ParserImplTest, StructDecl_MissingBracketLeft) {
TEST_F(ParserImplTest, StructDecl_InvalidStructBody) {
auto* p = parser("struct { a : B; }");
auto s = p->struct_decl();
auto s = p->struct_decl("S");
ASSERT_TRUE(p->has_error());
ASSERT_EQ(s, nullptr);
EXPECT_EQ(p->error(), "1:14: unknown type alias 'B'");
@@ -96,7 +99,7 @@ TEST_F(ParserImplTest, StructDecl_InvalidStructBody) {
TEST_F(ParserImplTest, StructDecl_InvalidStructDecorationDecl) {
auto* p = parser("[[block struct { a : i32; }");
auto s = p->struct_decl();
auto s = p->struct_decl("S");
ASSERT_TRUE(p->has_error());
ASSERT_EQ(s, nullptr);
EXPECT_EQ(p->error(), "1:9: missing ]] for struct decoration");
@@ -104,7 +107,7 @@ TEST_F(ParserImplTest, StructDecl_InvalidStructDecorationDecl) {
TEST_F(ParserImplTest, StructDecl_MissingStruct) {
auto* p = parser("[[block]] {}");
auto s = p->struct_decl();
auto s = p->struct_decl("S");
ASSERT_TRUE(p->has_error());
ASSERT_EQ(s, nullptr);
EXPECT_EQ(p->error(), "1:11: missing struct declaration");