[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 <rharrison@chromium.org>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index ea3b979..707cab3 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -243,6 +243,8 @@
"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 @@
"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 b2d4885..798bc6a 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -64,6 +64,8 @@
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 @@
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 677fd69..740fde3 100644
--- a/src/ast/binding_decoration_test.cc
+++ b/src/ast/binding_decoration_test.cc
@@ -31,6 +31,7 @@
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 114874b..21c2659 100644
--- a/src/ast/builtin_decoration_test.cc
+++ b/src/ast/builtin_decoration_test.cc
@@ -31,6 +31,7 @@
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 0000000..a5befb6
--- /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 0000000..c7b2d9a
--- /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 0000000..85eac21
--- /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 c0ed6b1..7063e32 100644
--- a/src/ast/location_decoration_test.cc
+++ b/src/ast/location_decoration_test.cc
@@ -33,6 +33,7 @@
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 5a90c9d..1d3d372 100644
--- a/src/ast/set_decoration_test.cc
+++ b/src/ast/set_decoration_test.cc
@@ -31,6 +31,7 @@
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 bdfa81a..fff88be 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 @@
return false;
}
+bool VariableDecoration::IsConstantId() const {
+ return false;
+}
+
bool VariableDecoration::IsSet() const {
return false;
}
@@ -54,6 +59,11 @@
return static_cast<BuiltinDecoration*>(this);
}
+ConstantIdDecoration* VariableDecoration::AsConstantId() {
+ assert(IsConstantId());
+ return static_cast<ConstantIdDecoration*>(this);
+}
+
LocationDecoration* VariableDecoration::AsLocation() {
assert(IsLocation());
return static_cast<LocationDecoration*>(this);
diff --git a/src/ast/variable_decoration.h b/src/ast/variable_decoration.h
index 95d9148..fe62123 100644
--- a/src/ast/variable_decoration.h
+++ b/src/ast/variable_decoration.h
@@ -25,6 +25,7 @@
class BindingDecoration;
class BuiltinDecoration;
+class ConstantIdDecoration;
class LocationDecoration;
class SetDecoration;
@@ -37,6 +38,8 @@
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 @@
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