[ast] Add DiscardStatement AST node.
This CL adds DiscardStatement to the AST.
Bug: tint:162
Change-Id: I1905023eb8297d9a983884e77c6a2267fd43a076
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25601
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 24c587f..d2807b9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -70,6 +70,8 @@
ast/continue_statement.h
ast/decorated_variable.cc
ast/decorated_variable.h
+ ast/discard_statement.cc
+ ast/discard_statement.h
ast/else_statement.cc
ast/else_statement.h
ast/entry_point.cc
@@ -286,6 +288,7 @@
ast/case_statement_test.cc
ast/cast_expression_test.cc
ast/continue_statement_test.cc
+ ast/discard_statement_test.cc
ast/decorated_variable_test.cc
ast/else_statement_test.cc
ast/entry_point_test.cc
diff --git a/src/ast/discard_statement.cc b/src/ast/discard_statement.cc
new file mode 100644
index 0000000..6864aa0
--- /dev/null
+++ b/src/ast/discard_statement.cc
@@ -0,0 +1,40 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/ast/discard_statement.h"
+
+namespace tint {
+namespace ast {
+
+DiscardStatement::DiscardStatement() : Statement() {}
+
+DiscardStatement::DiscardStatement(const Source& source) : Statement(source) {}
+
+DiscardStatement::~DiscardStatement() = default;
+
+bool DiscardStatement::IsDiscard() const {
+ return true;
+}
+
+bool DiscardStatement::IsValid() const {
+ return true;
+}
+
+void DiscardStatement::to_str(std::ostream& out, size_t indent) const {
+ make_indent(out, indent);
+ out << "Discard{}" << std::endl;
+}
+
+} // namespace ast
+} // namespace tint
diff --git a/src/ast/discard_statement.h b/src/ast/discard_statement.h
new file mode 100644
index 0000000..af0f3fd
--- /dev/null
+++ b/src/ast/discard_statement.h
@@ -0,0 +1,53 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef SRC_AST_DISCARD_STATEMENT_H_
+#define SRC_AST_DISCARD_STATEMENT_H_
+
+#include "src/ast/statement.h"
+
+namespace tint {
+namespace ast {
+
+/// A discard statement
+class DiscardStatement : public Statement {
+ public:
+ /// Constructor
+ DiscardStatement();
+ /// Constructor
+ /// @param source the discard statement source
+ explicit DiscardStatement(const Source& source);
+ /// Move constructor
+ DiscardStatement(DiscardStatement&&) = default;
+ ~DiscardStatement() override;
+
+ /// @returns true if this is a discard statement
+ bool IsDiscard() const override;
+
+ /// @returns true if the node is valid
+ bool IsValid() const override;
+
+ /// Writes a representation of the node to the output stream
+ /// @param out the stream to write to
+ /// @param indent number of spaces to indent the node when writing
+ void to_str(std::ostream& out, size_t indent) const override;
+
+ private:
+ DiscardStatement(const DiscardStatement&) = delete;
+};
+
+} // namespace ast
+} // namespace tint
+
+#endif // SRC_AST_DISCARD_STATEMENT_H_
diff --git a/src/ast/discard_statement_test.cc b/src/ast/discard_statement_test.cc
new file mode 100644
index 0000000..c6bcd49
--- /dev/null
+++ b/src/ast/discard_statement_test.cc
@@ -0,0 +1,59 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/ast/discard_statement.h"
+
+#include <sstream>
+
+#include "gtest/gtest.h"
+
+namespace tint {
+namespace ast {
+namespace {
+
+using DiscardStatementTest = testing::Test;
+
+TEST_F(DiscardStatementTest, Creation) {
+ DiscardStatement stmt;
+ EXPECT_EQ(stmt.line(), 0u);
+ EXPECT_EQ(stmt.column(), 0u);
+}
+
+TEST_F(DiscardStatementTest, Creation_WithSource) {
+ DiscardStatement stmt(Source{20, 2});
+ EXPECT_EQ(stmt.line(), 20u);
+ EXPECT_EQ(stmt.column(), 2u);
+}
+
+TEST_F(DiscardStatementTest, IsDiscard) {
+ DiscardStatement stmt;
+ EXPECT_TRUE(stmt.IsDiscard());
+}
+
+TEST_F(DiscardStatementTest, IsValid) {
+ DiscardStatement stmt;
+ EXPECT_TRUE(stmt.IsValid());
+}
+
+TEST_F(DiscardStatementTest, ToStr) {
+ DiscardStatement stmt;
+ std::ostringstream out;
+ stmt.to_str(out, 2);
+ EXPECT_EQ(out.str(), R"( Discard{}
+)");
+}
+
+} // namespace
+} // namespace ast
+} // namespace tint
diff --git a/src/ast/statement.cc b/src/ast/statement.cc
index 884c954..af944ae 100644
--- a/src/ast/statement.cc
+++ b/src/ast/statement.cc
@@ -21,6 +21,7 @@
#include "src/ast/call_statement.h"
#include "src/ast/case_statement.h"
#include "src/ast/continue_statement.h"
+#include "src/ast/discard_statement.h"
#include "src/ast/else_statement.h"
#include "src/ast/fallthrough_statement.h"
#include "src/ast/if_statement.h"
@@ -61,6 +62,10 @@
return false;
}
+bool Statement::IsDiscard() const {
+ return false;
+}
+
bool Statement::IsElse() const {
return false;
}
@@ -118,6 +123,11 @@
return static_cast<const ContinueStatement*>(this);
}
+const DiscardStatement* Statement::AsDiscard() const {
+ assert(IsDiscard());
+ return static_cast<const DiscardStatement*>(this);
+}
+
const ElseStatement* Statement::AsElse() const {
assert(IsElse());
return static_cast<const ElseStatement*>(this);
@@ -183,6 +193,11 @@
return static_cast<ContinueStatement*>(this);
}
+DiscardStatement* Statement::AsDiscard() {
+ assert(IsDiscard());
+ return static_cast<DiscardStatement*>(this);
+}
+
ElseStatement* Statement::AsElse() {
assert(IsElse());
return static_cast<ElseStatement*>(this);
diff --git a/src/ast/statement.h b/src/ast/statement.h
index f887ffb..b1511ab 100644
--- a/src/ast/statement.h
+++ b/src/ast/statement.h
@@ -28,6 +28,7 @@
class CallStatement;
class CaseStatement;
class ContinueStatement;
+class DiscardStatement;
class ElseStatement;
class FallthroughStatement;
class IfStatement;
@@ -52,6 +53,8 @@
virtual bool IsCase() const;
/// @returns true if this is a continue statement
virtual bool IsContinue() const;
+ /// @returns true if this is a discard statement
+ virtual bool IsDiscard() const;
/// @returns true if this is an else statement
virtual bool IsElse() const;
/// @returns true if this is a fallthrough statement
@@ -79,6 +82,8 @@
const CaseStatement* AsCase() const;
/// @returns the statement as a const continue statement
const ContinueStatement* AsContinue() const;
+ /// @returns the statement as a const discard statement
+ const DiscardStatement* AsDiscard() const;
/// @returns the statement as a const else statement
const ElseStatement* AsElse() const;
/// @returns the statement as a const fallthrough statement
@@ -106,6 +111,8 @@
CaseStatement* AsCase();
/// @returns the statement as a continue statement
ContinueStatement* AsContinue();
+ /// @returns the statement as a discard statement
+ DiscardStatement* AsDiscard();
/// @returns the statement as a else statement
ElseStatement* AsElse();
/// @returns the statement as a fallthrough statement