From 8650247ddb433bd6cee26b27c338395331dba2f2 Mon Sep 17 00:00:00 2001 From: James Price Date: Thu, 13 May 2021 20:11:22 +0000 Subject: [PATCH] ast: Support override decorations without IDs Bug: tint:755 Change-Id: I33f6535b4f3f03826d6f4dd531dc6967f476402b Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50840 Reviewed-by: Ben Clayton Commit-Queue: James Price --- src/ast/override_decoration.cc | 18 +++++++++++++++--- src/ast/override_decoration.h | 10 +++++++++- src/ast/override_decoration_test.cc | 8 +++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/ast/override_decoration.cc b/src/ast/override_decoration.cc index c50a993ebb..bf32a487cd 100644 --- a/src/ast/override_decoration.cc +++ b/src/ast/override_decoration.cc @@ -21,10 +21,14 @@ TINT_INSTANTIATE_TYPEINFO(tint::ast::OverrideDecoration); namespace tint { namespace ast { +OverrideDecoration::OverrideDecoration(ProgramID program_id, + const Source& source) + : Base(program_id, source), has_value_(false), value_(0) {} + OverrideDecoration::OverrideDecoration(ProgramID program_id, const Source& source, uint32_t val) - : Base(program_id, source), value_(val) {} + : Base(program_id, source), has_value_(true), value_(val) {} OverrideDecoration::~OverrideDecoration() = default; @@ -32,13 +36,21 @@ void OverrideDecoration::to_str(const sem::Info&, std::ostream& out, size_t indent) const { make_indent(out, indent); - out << "OverrideDecoration{" << value_ << "}" << std::endl; + out << "OverrideDecoration"; + if (has_value_) { + out << "{" << value_ << "}"; + } + out << std::endl; } OverrideDecoration* OverrideDecoration::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source()); - return ctx->dst->create(src, value_); + if (has_value_) { + return ctx->dst->create(src, value_); + } else { + return ctx->dst->create(src); + } } } // namespace ast diff --git a/src/ast/override_decoration.h b/src/ast/override_decoration.h index b2f8b3b70f..f5c6090bf3 100644 --- a/src/ast/override_decoration.h +++ b/src/ast/override_decoration.h @@ -23,13 +23,20 @@ namespace ast { /// An override decoration class OverrideDecoration : public Castable { public: - /// constructor + /// Create an override decoration with no specified id. + /// @param program_id the identifier of the program that owns this node + /// @param source the source of this decoration + OverrideDecoration(ProgramID program_id, const Source& source); + /// Create an override decoration with a specific id value. /// @param program_id the identifier of the program that owns this node /// @param source the source of this decoration /// @param val the override value OverrideDecoration(ProgramID program_id, const Source& source, uint32_t val); ~OverrideDecoration() override; + /// @returns true if an override id was specified + uint32_t HasValue() const { return has_value_; } + /// @returns the override id value uint32_t value() const { return value_; } @@ -48,6 +55,7 @@ class OverrideDecoration : public Castable { OverrideDecoration* Clone(CloneContext* ctx) const override; private: + bool has_value_; uint32_t const value_; }; diff --git a/src/ast/override_decoration_test.cc b/src/ast/override_decoration_test.cc index 078563c7d8..fe1d151599 100644 --- a/src/ast/override_decoration_test.cc +++ b/src/ast/override_decoration_test.cc @@ -22,11 +22,17 @@ namespace { using OverrideDecorationTest = TestHelper; -TEST_F(OverrideDecorationTest, Creation) { +TEST_F(OverrideDecorationTest, Creation_WithValue) { auto* d = create(12); + EXPECT_TRUE(d->HasValue()); EXPECT_EQ(12u, d->value()); } +TEST_F(OverrideDecorationTest, Creation_WithoutValue) { + auto* d = create(); + EXPECT_FALSE(d->HasValue()); +} + TEST_F(OverrideDecorationTest, ToStr) { auto* d = create(1200); EXPECT_EQ(str(d), R"(OverrideDecoration{1200}