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 <bclayton@google.com>
Commit-Queue: James Price <jrprice@google.com>
This commit is contained in:
James Price 2021-05-13 20:11:22 +00:00 committed by Commit Bot service account
parent 09587e1d86
commit 8650247ddb
3 changed files with 31 additions and 5 deletions

View File

@ -21,10 +21,14 @@ TINT_INSTANTIATE_TYPEINFO(tint::ast::OverrideDecoration);
namespace tint { namespace tint {
namespace ast { namespace ast {
OverrideDecoration::OverrideDecoration(ProgramID program_id,
const Source& source)
: Base(program_id, source), has_value_(false), value_(0) {}
OverrideDecoration::OverrideDecoration(ProgramID program_id, OverrideDecoration::OverrideDecoration(ProgramID program_id,
const Source& source, const Source& source,
uint32_t val) uint32_t val)
: Base(program_id, source), value_(val) {} : Base(program_id, source), has_value_(true), value_(val) {}
OverrideDecoration::~OverrideDecoration() = default; OverrideDecoration::~OverrideDecoration() = default;
@ -32,13 +36,21 @@ void OverrideDecoration::to_str(const sem::Info&,
std::ostream& out, std::ostream& out,
size_t indent) const { size_t indent) const {
make_indent(out, indent); 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 { OverrideDecoration* OverrideDecoration::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering // Clone arguments outside of create() call to have deterministic ordering
auto src = ctx->Clone(source()); auto src = ctx->Clone(source());
if (has_value_) {
return ctx->dst->create<OverrideDecoration>(src, value_); return ctx->dst->create<OverrideDecoration>(src, value_);
} else {
return ctx->dst->create<OverrideDecoration>(src);
}
} }
} // namespace ast } // namespace ast

View File

@ -23,13 +23,20 @@ namespace ast {
/// An override decoration /// An override decoration
class OverrideDecoration : public Castable<OverrideDecoration, Decoration> { class OverrideDecoration : public Castable<OverrideDecoration, Decoration> {
public: 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 program_id the identifier of the program that owns this node
/// @param source the source of this decoration /// @param source the source of this decoration
/// @param val the override value /// @param val the override value
OverrideDecoration(ProgramID program_id, const Source& source, uint32_t val); OverrideDecoration(ProgramID program_id, const Source& source, uint32_t val);
~OverrideDecoration() override; ~OverrideDecoration() override;
/// @returns true if an override id was specified
uint32_t HasValue() const { return has_value_; }
/// @returns the override id value /// @returns the override id value
uint32_t value() const { return value_; } uint32_t value() const { return value_; }
@ -48,6 +55,7 @@ class OverrideDecoration : public Castable<OverrideDecoration, Decoration> {
OverrideDecoration* Clone(CloneContext* ctx) const override; OverrideDecoration* Clone(CloneContext* ctx) const override;
private: private:
bool has_value_;
uint32_t const value_; uint32_t const value_;
}; };

View File

@ -22,11 +22,17 @@ namespace {
using OverrideDecorationTest = TestHelper; using OverrideDecorationTest = TestHelper;
TEST_F(OverrideDecorationTest, Creation) { TEST_F(OverrideDecorationTest, Creation_WithValue) {
auto* d = create<OverrideDecoration>(12); auto* d = create<OverrideDecoration>(12);
EXPECT_TRUE(d->HasValue());
EXPECT_EQ(12u, d->value()); EXPECT_EQ(12u, d->value());
} }
TEST_F(OverrideDecorationTest, Creation_WithoutValue) {
auto* d = create<OverrideDecoration>();
EXPECT_FALSE(d->HasValue());
}
TEST_F(OverrideDecorationTest, ToStr) { TEST_F(OverrideDecorationTest, ToStr) {
auto* d = create<OverrideDecoration>(1200); auto* d = create<OverrideDecoration>(1200);
EXPECT_EQ(str(d), R"(OverrideDecoration{1200} EXPECT_EQ(str(d), R"(OverrideDecoration{1200}