Remove type alias struct variant.

This CL removes the `type IDENT = struct` format in favour of the
`struct IDENT` variant.

Bug: tint:175
Change-Id: I4fde8012fd07f811cd0bd80445198f6bbc92b720
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/30661
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
This commit is contained in:
dan sinclair
2020-10-21 19:15:20 +00:00
committed by Commit Bot service account
parent 4f79c84050
commit 0ce0704741
14 changed files with 61 additions and 222 deletions

View File

@@ -261,7 +261,7 @@ void ParserImpl::global_decl() {
return;
}
auto str = struct_decl("");
auto str = struct_decl();
if (has_error()) {
return;
}
@@ -1080,7 +1080,6 @@ ast::StorageClass ParserImpl::variable_storage_decoration() {
// type_alias
// : TYPE IDENT EQUAL type_decl
// | TYPE IDENT EQUAL struct_decl
ast::type::Type* ParserImpl::type_alias() {
auto t = peek();
if (!t.IsType())
@@ -1105,17 +1104,8 @@ ast::type::Type* ParserImpl::type_alias() {
if (has_error())
return nullptr;
if (type == nullptr) {
auto str = struct_decl(name);
if (has_error())
return nullptr;
if (str == nullptr) {
set_error(peek(), "invalid type alias");
return nullptr;
}
type = ctx_.type_mgr().Get(std::move(str));
register_constructed(name, type);
return type;
set_error(peek(), "invalid type alias");
return nullptr;
}
if (type == nullptr) {
set_error(peek(), "invalid type for alias");
@@ -1501,9 +1491,8 @@ ast::StorageClass ParserImpl::storage_class() {
}
// struct_decl
// : struct_decoration_decl* STRUCT struct_body_decl
std::unique_ptr<ast::type::StructType> ParserImpl::struct_decl(
const std::string& name) {
// : struct_decoration_decl* STRUCT IDENT struct_body_decl
std::unique_ptr<ast::type::StructType> ParserImpl::struct_decl() {
auto t = peek();
auto source = t.source();
@@ -1527,17 +1516,12 @@ std::unique_ptr<ast::type::StructType> ParserImpl::struct_decl(
}
next(); // Consume the peek
// If there is no name this is a global struct call. This check will go
// away when the type_alias struct entry is removed.
std::string str_name = name;
if (name.empty()) {
t = next();
if (!t.IsIdentifier()) {
set_error(t, "missing identifier for struct declaration");
return nullptr;
}
str_name = t.to_str();
t = next();
if (!t.IsIdentifier()) {
set_error(t, "missing identifier for struct declaration");
return nullptr;
}
auto name = t.to_str();
auto body = struct_body_decl();
if (has_error()) {
@@ -1545,7 +1529,7 @@ std::unique_ptr<ast::type::StructType> ParserImpl::struct_decl(
}
return std::make_unique<ast::type::StructType>(
str_name,
name,
std::make_unique<ast::Struct>(source, std::move(decos), std::move(body)));
}

View File

@@ -156,9 +156,8 @@ 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(const std::string& name);
std::unique_ptr<ast::type::StructType> struct_decl();
/// 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

@@ -92,17 +92,6 @@ TEST_F(ParserImplTest, GlobalDecl_TypeAlias) {
EXPECT_EQ(m.constructed_types()[0]->AsAlias()->name(), "A");
}
TEST_F(ParserImplTest, GlobalDecl_TypeAlias_Struct) {
auto* p = parser("type A = struct { a : f32; };");
p->global_decl();
ASSERT_FALSE(p->has_error()) << p->error();
auto m = p->module();
ASSERT_EQ(m.constructed_types().size(), 1u);
ASSERT_TRUE(m.constructed_types()[0]->IsStruct());
EXPECT_EQ(m.constructed_types()[0]->AsStruct()->name(), "A");
}
TEST_F(ParserImplTest, GlobalDecl_TypeAlias_StructIdent) {
auto* p = parser(R"(struct A {
a : f32;
@@ -239,21 +228,6 @@ TEST_F(ParserImplTest, GlobalDecl_StructMissing_Semi) {
EXPECT_EQ(p->error(), "1:22: missing ';' for struct declaration");
}
// This was failing due to not finding the missing ;. https://crbug.com/tint/218
TEST_F(ParserImplTest, TypeDecl_Struct_Empty) {
auto* p = parser("type str = struct {};");
p->global_decl();
ASSERT_FALSE(p->has_error()) << p->error();
auto module = p->module();
ASSERT_EQ(module.constructed_types().size(), 1u);
ASSERT_TRUE(module.constructed_types()[0]->IsStruct());
auto* str = module.constructed_types()[0]->AsStruct();
EXPECT_EQ(str->name(), "str");
EXPECT_EQ(str->impl()->members().size(), 0u);
}
} // namespace
} // namespace wgsl
} // namespace reader

View File

@@ -28,22 +28,7 @@ struct S {
a : i32;
[[offset(4)]] b : f32;
})");
auto s = p->struct_decl("");
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");
}
TEST_F(ParserImplTest, StructDecl_Parses_WithoutName) {
auto* p = parser(R"(
struct {
a : i32;
[[offset(4)]] b : f32;
})");
auto s = p->struct_decl("S");
auto s = p->struct_decl();
ASSERT_FALSE(p->has_error());
ASSERT_NE(s, nullptr);
ASSERT_EQ(s->name(), "S");
@@ -58,7 +43,7 @@ TEST_F(ParserImplTest, StructDecl_ParsesWithDecoration) {
a : f32;
b : f32;
})");
auto s = p->struct_decl("");
auto s = p->struct_decl();
ASSERT_FALSE(p->has_error());
ASSERT_NE(s, nullptr);
ASSERT_EQ(s->name(), "B");
@@ -76,7 +61,7 @@ TEST_F(ParserImplTest, StructDecl_ParsesWithMultipleDecoration) {
a : f32;
b : f32;
})");
auto s = p->struct_decl("");
auto s = p->struct_decl();
ASSERT_FALSE(p->has_error());
ASSERT_NE(s, nullptr);
ASSERT_EQ(s->name(), "S");
@@ -90,7 +75,7 @@ TEST_F(ParserImplTest, StructDecl_ParsesWithMultipleDecoration) {
TEST_F(ParserImplTest, StructDecl_EmptyMembers) {
auto* p = parser("struct S {}");
auto s = p->struct_decl("");
auto s = p->struct_decl();
ASSERT_FALSE(p->has_error());
ASSERT_NE(s, nullptr);
ASSERT_EQ(s->impl()->members().size(), 0u);
@@ -98,7 +83,7 @@ TEST_F(ParserImplTest, StructDecl_EmptyMembers) {
TEST_F(ParserImplTest, StructDecl_MissingIdent) {
auto* p = parser("struct {}");
auto s = p->struct_decl("");
auto s = p->struct_decl();
ASSERT_TRUE(p->has_error());
ASSERT_EQ(s, nullptr);
EXPECT_EQ(p->error(), "1:8: missing identifier for struct declaration");
@@ -106,7 +91,7 @@ TEST_F(ParserImplTest, StructDecl_MissingIdent) {
TEST_F(ParserImplTest, StructDecl_MissingBracketLeft) {
auto* p = parser("struct S }");
auto s = p->struct_decl("");
auto s = p->struct_decl();
ASSERT_TRUE(p->has_error());
ASSERT_EQ(s, nullptr);
EXPECT_EQ(p->error(), "1:10: missing { for struct declaration");
@@ -114,7 +99,7 @@ TEST_F(ParserImplTest, StructDecl_MissingBracketLeft) {
TEST_F(ParserImplTest, StructDecl_InvalidStructBody) {
auto* p = parser("struct S { a : B; }");
auto s = p->struct_decl("");
auto s = p->struct_decl();
ASSERT_TRUE(p->has_error());
ASSERT_EQ(s, nullptr);
EXPECT_EQ(p->error(), "1:16: unknown constructed type 'B'");
@@ -122,7 +107,7 @@ TEST_F(ParserImplTest, StructDecl_InvalidStructBody) {
TEST_F(ParserImplTest, StructDecl_InvalidStructDecorationDecl) {
auto* p = parser("[[block struct S { a : i32; }");
auto s = p->struct_decl("");
auto s = p->struct_decl();
ASSERT_TRUE(p->has_error());
ASSERT_EQ(s, nullptr);
EXPECT_EQ(p->error(), "1:9: missing ]] for struct decoration");
@@ -130,7 +115,7 @@ TEST_F(ParserImplTest, StructDecl_InvalidStructDecorationDecl) {
TEST_F(ParserImplTest, StructDecl_MissingStruct) {
auto* p = parser("[[block]] S {}");
auto s = p->struct_decl("");
auto s = p->struct_decl();
ASSERT_TRUE(p->has_error());
ASSERT_EQ(s, nullptr);
EXPECT_EQ(p->error(), "1:11: missing struct declaration");

View File

@@ -89,59 +89,6 @@ TEST_F(ParserImplTest, TypeDecl_InvalidType) {
EXPECT_EQ(p->error(), "1:10: unknown constructed type 'B'");
}
TEST_F(ParserImplTest, TypeDecl_ParsesStruct) {
auto* p = parser("type a = struct { b: i32; c: f32;}");
auto* t = p->type_alias();
ASSERT_FALSE(p->has_error());
ASSERT_NE(t, nullptr);
ASSERT_TRUE(t->IsStruct());
auto* str = t->AsStruct();
EXPECT_EQ(str->name(), "a");
EXPECT_EQ(str->impl()->members().size(), 2u);
}
TEST_F(ParserImplTest, TypeDecl_InvalidStruct) {
auto* p = parser("type a = [[block]] {}");
auto* t = p->type_alias();
ASSERT_TRUE(p->has_error());
ASSERT_EQ(t, nullptr);
EXPECT_EQ(p->error(), "1:20: missing struct declaration");
}
TEST_F(ParserImplTest, TypeDecl_Struct_WithStride) {
auto* p = parser(
"type a = [[block]] struct { [[offset(0)]] data: [[stride(4)]] "
"array<f32>; "
"}");
auto* t = p->type_alias();
ASSERT_FALSE(p->has_error());
ASSERT_NE(t, nullptr);
ASSERT_TRUE(t->IsStruct());
auto* str = t->AsStruct();
EXPECT_EQ(str->name(), "a");
EXPECT_EQ(str->impl()->members().size(), 1u);
const auto* ty = str->impl()->members()[0]->type();
ASSERT_TRUE(ty->IsArray());
const auto* arr = ty->AsArray();
EXPECT_TRUE(arr->has_array_stride());
EXPECT_EQ(arr->array_stride(), 4u);
}
TEST_F(ParserImplTest, TypeDecl_Struct_Empty) {
auto* p = parser("type str = struct {};");
p->global_decl();
ASSERT_FALSE(p->has_error()) << p->error();
auto module = p->module();
ASSERT_EQ(module.constructed_types().size(), 1u);
ASSERT_TRUE(module.constructed_types()[0]->IsStruct());
auto* str = module.constructed_types()[0]->AsStruct();
EXPECT_EQ(str->name(), "str");
EXPECT_EQ(str->impl()->members().size(), 0u);
}
} // namespace
} // namespace wgsl
} // namespace reader