From f751501c357db67a2c95970aacf74ecf4beb91eb Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Mon, 27 Jul 2020 15:25:00 +0000 Subject: [PATCH] [wgsl-reader] Update to create BlockStatements This CL updates the WGSL Reader to create BlockStatements instead of StatementLists. Bug: tint:135 Change-Id: Ifda394023553a625dad67be7d4e0dc815292282a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25724 Reviewed-by: Sarah Mashayekhi --- src/reader/wgsl/parser_impl.cc | 23 ++++++++++--------- src/reader/wgsl/parser_impl.h | 8 +++---- src/reader/wgsl/parser_impl_body_stmt_test.cc | 8 +++---- src/reader/wgsl/parser_impl_case_body_test.cc | 17 +++++++------- .../wgsl/parser_impl_continuing_stmt_test.cc | 6 ++--- .../wgsl/parser_impl_statements_test.cc | 8 +++---- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index b61d721cea..2a9b4c9214 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -1351,7 +1351,7 @@ ast::PipelineStage ParserImpl::pipeline_stage() { // body_stmt // : BRACKET_LEFT statements BRACKET_RIGHT -ast::StatementList ParserImpl::body_stmt() { +std::unique_ptr ParserImpl::body_stmt() { auto t = peek(); if (!t.IsBraceLeft()) return {}; @@ -1400,8 +1400,8 @@ std::unique_ptr ParserImpl::paren_rhs_stmt() { // statements // : statement* -ast::StatementList ParserImpl::statements() { - ast::StatementList ret; +std::unique_ptr ParserImpl::statements() { + auto ret = std::make_unique(); for (;;) { auto stmt = statement(); @@ -1410,7 +1410,7 @@ ast::StatementList ParserImpl::statements() { if (stmt == nullptr) break; - ret.push_back(std::move(stmt)); + ret->append(std::move(stmt)); } return ret; @@ -1857,8 +1857,8 @@ ast::CaseSelectorList ParserImpl::case_selectors() { // : // | statement case_body // | FALLTHROUGH SEMICOLON -ast::StatementList ParserImpl::case_body() { - ast::StatementList ret; +std::unique_ptr ParserImpl::case_body() { + auto ret = std::make_unique(); for (;;) { auto t = peek(); if (t.IsFallthrough()) { @@ -1871,7 +1871,7 @@ ast::StatementList ParserImpl::case_body() { return {}; } - ret.push_back(std::make_unique(source)); + ret->append(std::make_unique(source)); break; } @@ -1881,7 +1881,7 @@ ast::StatementList ParserImpl::case_body() { if (stmt == nullptr) break; - ret.push_back(std::move(stmt)); + ret->append(std::move(stmt)); } return ret; @@ -1980,10 +1980,11 @@ std::unique_ptr ParserImpl::continue_stmt() { // continuing_stmt // : CONTINUING body_stmt -ast::StatementList ParserImpl::continuing_stmt() { +std::unique_ptr ParserImpl::continuing_stmt() { auto t = peek(); - if (!t.IsContinuing()) - return {}; + if (!t.IsContinuing()) { + return std::make_unique(); + } next(); // Consume the peek return body_stmt(); diff --git a/src/reader/wgsl/parser_impl.h b/src/reader/wgsl/parser_impl.h index af76bbbdc1..f31f0b941f 100644 --- a/src/reader/wgsl/parser_impl.h +++ b/src/reader/wgsl/parser_impl.h @@ -177,13 +177,13 @@ class ParserImpl { ast::PipelineStage pipeline_stage(); /// Parses a `body_stmt` grammar element /// @returns the parsed statements - ast::StatementList body_stmt(); + std::unique_ptr body_stmt(); /// Parses a `paren_rhs_stmt` grammar element /// @returns the parsed element or nullptr std::unique_ptr paren_rhs_stmt(); /// Parses a `statements` grammar element /// @returns the statements parsed - ast::StatementList statements(); + std::unique_ptr statements(); /// Parses a `statement` grammar element /// @returns the parsed statement or nullptr std::unique_ptr statement(); @@ -219,7 +219,7 @@ class ParserImpl { ast::CaseSelectorList case_selectors(); /// Parses a `case_body` grammar element /// @returns the parsed statements - ast::StatementList case_body(); + std::unique_ptr case_body(); /// Parses a `func_call_stmt` grammar element /// @returns the parsed function call or nullptr std::unique_ptr func_call_stmt(); @@ -228,7 +228,7 @@ class ParserImpl { std::unique_ptr loop_stmt(); /// Parses a `continuing_stmt` grammar element /// @returns the parsed statements - ast::StatementList continuing_stmt(); + std::unique_ptr continuing_stmt(); /// Parses a `const_literal` grammar element /// @returns the const literal parsed or nullptr if none found std::unique_ptr const_literal(); diff --git a/src/reader/wgsl/parser_impl_body_stmt_test.cc b/src/reader/wgsl/parser_impl_body_stmt_test.cc index ae0952b695..1bd1fbeb59 100644 --- a/src/reader/wgsl/parser_impl_body_stmt_test.cc +++ b/src/reader/wgsl/parser_impl_body_stmt_test.cc @@ -28,16 +28,16 @@ TEST_F(ParserImplTest, BodyStmt) { })"); auto e = p->body_stmt(); ASSERT_FALSE(p->has_error()) << p->error(); - ASSERT_EQ(e.size(), 2u); - EXPECT_TRUE(e[0]->IsDiscard()); - EXPECT_TRUE(e[1]->IsReturn()); + ASSERT_EQ(e->size(), 2u); + EXPECT_TRUE(e->get(0)->IsDiscard()); + EXPECT_TRUE(e->get(1)->IsReturn()); } TEST_F(ParserImplTest, BodyStmt_Empty) { auto* p = parser("{}"); auto e = p->body_stmt(); ASSERT_FALSE(p->has_error()) << p->error(); - EXPECT_EQ(e.size(), 0u); + EXPECT_EQ(e->size(), 0u); } TEST_F(ParserImplTest, BodyStmt_InvalidStmt) { diff --git a/src/reader/wgsl/parser_impl_case_body_test.cc b/src/reader/wgsl/parser_impl_case_body_test.cc index 8196c5bba0..61c6ec06e2 100644 --- a/src/reader/wgsl/parser_impl_case_body_test.cc +++ b/src/reader/wgsl/parser_impl_case_body_test.cc @@ -25,7 +25,7 @@ TEST_F(ParserImplTest, CaseBody_Empty) { auto* p = parser(""); auto e = p->case_body(); ASSERT_FALSE(p->has_error()) << p->error(); - EXPECT_EQ(e.size(), 0u); + EXPECT_EQ(e->size(), 0u); } TEST_F(ParserImplTest, CaseBody_Statements) { @@ -35,32 +35,31 @@ TEST_F(ParserImplTest, CaseBody_Statements) { auto e = p->case_body(); ASSERT_FALSE(p->has_error()) << p->error(); - ASSERT_EQ(e.size(), 2u); - EXPECT_TRUE(e[0]->IsVariableDecl()); - EXPECT_TRUE(e[1]->IsAssign()); + ASSERT_EQ(e->size(), 2u); + EXPECT_TRUE(e->get(0)->IsVariableDecl()); + EXPECT_TRUE(e->get(1)->IsAssign()); } TEST_F(ParserImplTest, CaseBody_InvalidStatement) { auto* p = parser("a ="); auto e = p->case_body(); ASSERT_TRUE(p->has_error()); - EXPECT_EQ(e.size(), 0u); - EXPECT_EQ(p->error(), "1:4: unable to parse right side of assignment"); + EXPECT_EQ(e, nullptr); } TEST_F(ParserImplTest, CaseBody_Fallthrough) { auto* p = parser("fallthrough;"); auto e = p->case_body(); ASSERT_FALSE(p->has_error()) << p->error(); - ASSERT_EQ(e.size(), 1u); - EXPECT_TRUE(e[0]->IsFallthrough()); + ASSERT_EQ(e->size(), 1u); + EXPECT_TRUE(e->get(0)->IsFallthrough()); } TEST_F(ParserImplTest, CaseBody_Fallthrough_MissingSemicolon) { auto* p = parser("fallthrough"); auto e = p->case_body(); ASSERT_TRUE(p->has_error()); - EXPECT_EQ(e.size(), 0u); + EXPECT_EQ(e, nullptr); EXPECT_EQ(p->error(), "1:12: missing ;"); } diff --git a/src/reader/wgsl/parser_impl_continuing_stmt_test.cc b/src/reader/wgsl/parser_impl_continuing_stmt_test.cc index d67b326899..c570d84d8b 100644 --- a/src/reader/wgsl/parser_impl_continuing_stmt_test.cc +++ b/src/reader/wgsl/parser_impl_continuing_stmt_test.cc @@ -25,15 +25,15 @@ TEST_F(ParserImplTest, ContinuingStmt) { auto* p = parser("continuing { discard; }"); auto e = p->continuing_stmt(); ASSERT_FALSE(p->has_error()) << p->error(); - ASSERT_EQ(e.size(), 1u); - ASSERT_TRUE(e[0]->IsDiscard()); + ASSERT_EQ(e->size(), 1u); + ASSERT_TRUE(e->get(0)->IsDiscard()); } TEST_F(ParserImplTest, ContinuingStmt_InvalidBody) { auto* p = parser("continuing { discard }"); auto e = p->continuing_stmt(); ASSERT_TRUE(p->has_error()); - ASSERT_EQ(e.size(), 0u); + ASSERT_EQ(e, nullptr); EXPECT_EQ(p->error(), "1:22: missing ;"); } diff --git a/src/reader/wgsl/parser_impl_statements_test.cc b/src/reader/wgsl/parser_impl_statements_test.cc index 23082d8181..a22e6566ef 100644 --- a/src/reader/wgsl/parser_impl_statements_test.cc +++ b/src/reader/wgsl/parser_impl_statements_test.cc @@ -26,16 +26,16 @@ TEST_F(ParserImplTest, Statements) { auto* p = parser("discard; return;"); auto e = p->statements(); ASSERT_FALSE(p->has_error()) << p->error(); - ASSERT_EQ(e.size(), 2u); - EXPECT_TRUE(e[0]->IsDiscard()); - EXPECT_TRUE(e[1]->IsReturn()); + ASSERT_EQ(e->size(), 2u); + EXPECT_TRUE(e->get(0)->IsDiscard()); + EXPECT_TRUE(e->get(1)->IsReturn()); } TEST_F(ParserImplTest, Statements_Empty) { auto* p = parser(""); auto e = p->statements(); ASSERT_FALSE(p->has_error()) << p->error(); - ASSERT_EQ(e.size(), 0u); + ASSERT_EQ(e->size(), 0u); } } // namespace