From 9e7f9dc96c47464d359565bd420a107afbd46cbc Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 29 Sep 2020 18:07:50 +0000 Subject: [PATCH] [ast] Add constant_id decoration This CL adds AST support for the constant_id decoration. Bug: tint:150 Change-Id: Ifde5a5325f770567ea24129e786953d89f3f514b Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/29100 Commit-Queue: Ryan Harrison Reviewed-by: Ryan Harrison --- BUILD.gn | 3 ++ src/CMakeLists.txt | 3 ++ src/ast/binding_decoration_test.cc | 1 + src/ast/builtin_decoration_test.cc | 1 + src/ast/constant_id_decoration.cc | 33 +++++++++++++++++ src/ast/constant_id_decoration.h | 49 ++++++++++++++++++++++++++ src/ast/constant_id_decoration_test.cc | 49 ++++++++++++++++++++++++++ src/ast/location_decoration_test.cc | 1 + src/ast/set_decoration_test.cc | 1 + src/ast/variable_decoration.cc | 10 ++++++ src/ast/variable_decoration.h | 5 +++ 11 files changed, 156 insertions(+) create mode 100644 src/ast/constant_id_decoration.cc create mode 100644 src/ast/constant_id_decoration.h create mode 100644 src/ast/constant_id_decoration_test.cc diff --git a/BUILD.gn b/BUILD.gn index ea3b9799a1..707cab3dcd 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -243,6 +243,8 @@ source_set("libtint_core_src") { "src/ast/call_statement.h", "src/ast/case_statement.cc", "src/ast/case_statement.h", + "src/ast/constant_id_decoration.cc", + "src/ast/constant_id_decoration.h", "src/ast/constructor_expression.cc", "src/ast/constructor_expression.h", "src/ast/continue_statement.cc", @@ -695,6 +697,7 @@ source_set("tint_unittests_core_src") { "src/ast/call_expression_test.cc", "src/ast/call_statement_test.cc", "src/ast/case_statement_test.cc", + "src/ast/constant_id_decoration_test.cc", "src/ast/continue_statement_test.cc", "src/ast/decorated_variable_test.cc", "src/ast/discard_statement_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b2d4885eab..798bc6ab36 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -64,6 +64,8 @@ set(TINT_LIB_SRCS ast/call_statement.h ast/case_statement.cc ast/case_statement.h + ast/constant_id_decoration.cc + ast/constant_id_decoration.h ast/constructor_expression.cc ast/constructor_expression.h ast/continue_statement.cc @@ -304,6 +306,7 @@ set(TINT_TEST_SRCS ast/call_expression_test.cc ast/call_statement_test.cc ast/case_statement_test.cc + ast/constant_id_decoration_test.cc ast/continue_statement_test.cc ast/discard_statement_test.cc ast/decorated_variable_test.cc diff --git a/src/ast/binding_decoration_test.cc b/src/ast/binding_decoration_test.cc index 677fd69a12..740fde30f1 100644 --- a/src/ast/binding_decoration_test.cc +++ b/src/ast/binding_decoration_test.cc @@ -31,6 +31,7 @@ TEST_F(BindingDecorationTest, Is) { BindingDecoration d{2}; EXPECT_TRUE(d.IsBinding()); EXPECT_FALSE(d.IsBuiltin()); + EXPECT_FALSE(d.IsConstantId()); EXPECT_FALSE(d.IsLocation()); EXPECT_FALSE(d.IsSet()); } diff --git a/src/ast/builtin_decoration_test.cc b/src/ast/builtin_decoration_test.cc index 114874b9db..21c26598f9 100644 --- a/src/ast/builtin_decoration_test.cc +++ b/src/ast/builtin_decoration_test.cc @@ -31,6 +31,7 @@ TEST_F(BuiltinDecorationTest, Is) { BuiltinDecoration d{Builtin::kFragDepth}; EXPECT_FALSE(d.IsBinding()); EXPECT_TRUE(d.IsBuiltin()); + EXPECT_FALSE(d.IsConstantId()); EXPECT_FALSE(d.IsLocation()); EXPECT_FALSE(d.IsSet()); } diff --git a/src/ast/constant_id_decoration.cc b/src/ast/constant_id_decoration.cc new file mode 100644 index 0000000000..a5befb6ee1 --- /dev/null +++ b/src/ast/constant_id_decoration.cc @@ -0,0 +1,33 @@ +// 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/constant_id_decoration.h" + +namespace tint { +namespace ast { + +ConstantIdDecoration::ConstantIdDecoration(uint32_t val) : value_(val) {} + +ConstantIdDecoration::~ConstantIdDecoration() = default; + +bool ConstantIdDecoration::IsConstantId() const { + return true; +} + +void ConstantIdDecoration::to_str(std::ostream& out) const { + out << "ConstantIdDecoration{" << value_ << "}" << std::endl; +} + +} // namespace ast +} // namespace tint diff --git a/src/ast/constant_id_decoration.h b/src/ast/constant_id_decoration.h new file mode 100644 index 0000000000..c7b2d9a834 --- /dev/null +++ b/src/ast/constant_id_decoration.h @@ -0,0 +1,49 @@ +// 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_CONSTANT_ID_DECORATION_H_ +#define SRC_AST_CONSTANT_ID_DECORATION_H_ + +#include "src/ast/builtin.h" +#include "src/ast/variable_decoration.h" + +namespace tint { +namespace ast { + +/// A constant id decoration +class ConstantIdDecoration : public VariableDecoration { + public: + /// constructor + /// @param val the constant_id value + explicit ConstantIdDecoration(uint32_t val); + ~ConstantIdDecoration() override; + + /// @returns true if this is a constant_id decoration + bool IsConstantId() const override; + + /// @returns the constant id value + uint32_t value() const { return value_; } + + /// Outputs the decoration to the given stream + /// @param out the stream to output too + void to_str(std::ostream& out) const override; + + private: + uint32_t value_ = 0; +}; + +} // namespace ast +} // namespace tint + +#endif // SRC_AST_CONSTANT_ID_DECORATION_H_ diff --git a/src/ast/constant_id_decoration_test.cc b/src/ast/constant_id_decoration_test.cc new file mode 100644 index 0000000000..85eac218b2 --- /dev/null +++ b/src/ast/constant_id_decoration_test.cc @@ -0,0 +1,49 @@ +// 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/constant_id_decoration.h" + +#include "gtest/gtest.h" + +namespace tint { +namespace ast { +namespace { + +using ConstantIdDecorationTest = testing::Test; + +TEST_F(ConstantIdDecorationTest, Creation) { + ConstantIdDecoration d{12}; + EXPECT_EQ(12u, d.value()); +} + +TEST_F(ConstantIdDecorationTest, Is) { + ConstantIdDecoration d{27}; + EXPECT_FALSE(d.IsBinding()); + EXPECT_FALSE(d.IsBuiltin()); + EXPECT_TRUE(d.IsConstantId()); + EXPECT_FALSE(d.IsLocation()); + EXPECT_FALSE(d.IsSet()); +} + +TEST_F(ConstantIdDecorationTest, ToStr) { + ConstantIdDecoration d{1200}; + std::ostringstream out; + d.to_str(out); + EXPECT_EQ(out.str(), R"(ConstantIdDecoration{1200} +)"); +} + +} // namespace +} // namespace ast +} // namespace tint diff --git a/src/ast/location_decoration_test.cc b/src/ast/location_decoration_test.cc index c0ed6b17bb..7063e326b0 100644 --- a/src/ast/location_decoration_test.cc +++ b/src/ast/location_decoration_test.cc @@ -33,6 +33,7 @@ TEST_F(LocationDecorationTest, Is) { LocationDecoration d{2}; EXPECT_FALSE(d.IsBinding()); EXPECT_FALSE(d.IsBuiltin()); + EXPECT_FALSE(d.IsConstantId()); EXPECT_TRUE(d.IsLocation()); EXPECT_FALSE(d.IsSet()); } diff --git a/src/ast/set_decoration_test.cc b/src/ast/set_decoration_test.cc index 5a90c9d181..1d3d3726d3 100644 --- a/src/ast/set_decoration_test.cc +++ b/src/ast/set_decoration_test.cc @@ -31,6 +31,7 @@ TEST_F(SetDecorationTest, Is) { SetDecoration d{2}; EXPECT_FALSE(d.IsBinding()); EXPECT_FALSE(d.IsBuiltin()); + EXPECT_FALSE(d.IsConstantId()); EXPECT_FALSE(d.IsLocation()); EXPECT_TRUE(d.IsSet()); } diff --git a/src/ast/variable_decoration.cc b/src/ast/variable_decoration.cc index bdfa81a0a7..fff88bee38 100644 --- a/src/ast/variable_decoration.cc +++ b/src/ast/variable_decoration.cc @@ -18,6 +18,7 @@ #include "src/ast/binding_decoration.h" #include "src/ast/builtin_decoration.h" +#include "src/ast/constant_id_decoration.h" #include "src/ast/location_decoration.h" #include "src/ast/set_decoration.h" @@ -40,6 +41,10 @@ bool VariableDecoration::IsLocation() const { return false; } +bool VariableDecoration::IsConstantId() const { + return false; +} + bool VariableDecoration::IsSet() const { return false; } @@ -54,6 +59,11 @@ BuiltinDecoration* VariableDecoration::AsBuiltin() { return static_cast(this); } +ConstantIdDecoration* VariableDecoration::AsConstantId() { + assert(IsConstantId()); + return static_cast(this); +} + LocationDecoration* VariableDecoration::AsLocation() { assert(IsLocation()); return static_cast(this); diff --git a/src/ast/variable_decoration.h b/src/ast/variable_decoration.h index 95d9148022..fe62123c1a 100644 --- a/src/ast/variable_decoration.h +++ b/src/ast/variable_decoration.h @@ -25,6 +25,7 @@ namespace ast { class BindingDecoration; class BuiltinDecoration; +class ConstantIdDecoration; class LocationDecoration; class SetDecoration; @@ -37,6 +38,8 @@ class VariableDecoration { virtual bool IsBinding() const; /// @returns true if this is a builtin decoration virtual bool IsBuiltin() const; + /// @returns true if this is a constant id decoration + virtual bool IsConstantId() const; /// @returns true if this is a location decoration virtual bool IsLocation() const; /// @returns true if this is a set decoration @@ -46,6 +49,8 @@ class VariableDecoration { BindingDecoration* AsBinding(); /// @returns the decoration as a builtin decoration BuiltinDecoration* AsBuiltin(); + /// @returns the decoration as a constant id decoration + ConstantIdDecoration* AsConstantId(); /// @returns the decoration as a location decoration LocationDecoration* AsLocation(); /// @returns the decoration as a set decoration