ast: Support decoration kind hierarchies

Instead of just having a single `DecorationKind` for the first
derivation from `Decoration`, have a `DecorationKind` for every
decoration type.

Add `Decoration::IsKind()` to test whether the decoration is of, or
derives from the given kind.

Note, this change is originally by bclayton@ from
https://dawn-review.googlesource.com/c/tint/+/33201

R=bclayton@google.com

Bug: tint:287
Change-Id: I69b51dfaa3f82ef4d61cda383b2f98f401013429
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33280
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
Auto-Submit: dan sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
dan sinclair 2020-11-18 19:33:30 +00:00 committed by Commit Bot service account
parent 038f6d2f37
commit c7f51b7d3c
31 changed files with 368 additions and 36 deletions

View File

@ -21,11 +21,17 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
ArrayDecoration::ArrayDecoration(const Source& source) constexpr const DecorationKind ArrayDecoration::Kind;
: Decoration(Kind, source) {}
ArrayDecoration::ArrayDecoration(DecorationKind kind, const Source& source)
: Decoration(kind, source) {}
ArrayDecoration::~ArrayDecoration() = default; ArrayDecoration::~ArrayDecoration() = default;
bool ArrayDecoration::IsKind(DecorationKind kind) const {
return kind == Kind;
}
bool ArrayDecoration::IsStride() const { bool ArrayDecoration::IsStride() const {
return false; return false;
} }

View File

@ -30,10 +30,15 @@ class StrideDecoration;
class ArrayDecoration : public Decoration { class ArrayDecoration : public Decoration {
public: public:
/// The kind of decoration that this type represents /// The kind of decoration that this type represents
static constexpr DecorationKind Kind = DecorationKind::kArray; static constexpr const DecorationKind Kind = DecorationKind::kArray;
~ArrayDecoration() override; ~ArrayDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is a stride decoration /// @returns true if this is a stride decoration
virtual bool IsStride() const; virtual bool IsStride() const;
@ -42,8 +47,9 @@ class ArrayDecoration : public Decoration {
protected: protected:
/// Constructor /// Constructor
/// @param kind the decoration kind
/// @param source the source of this decoration /// @param source the source of this decoration
explicit ArrayDecoration(const Source& source); ArrayDecoration(DecorationKind kind, const Source& source);
}; };
/// A list of array decorations /// A list of array decorations

View File

@ -17,11 +17,17 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
constexpr const DecorationKind BindingDecoration::Kind;
BindingDecoration::BindingDecoration(uint32_t val, const Source& source) BindingDecoration::BindingDecoration(uint32_t val, const Source& source)
: VariableDecoration(source), value_(val) {} : VariableDecoration(Kind, source), value_(val) {}
BindingDecoration::~BindingDecoration() = default; BindingDecoration::~BindingDecoration() = default;
bool BindingDecoration::IsKind(DecorationKind kind) const {
return kind == Kind || VariableDecoration::IsKind(kind);
}
bool BindingDecoration::IsBinding() const { bool BindingDecoration::IsBinding() const {
return true; return true;
} }

View File

@ -25,12 +25,20 @@ namespace ast {
/// A binding decoration /// A binding decoration
class BindingDecoration : public VariableDecoration { class BindingDecoration : public VariableDecoration {
public: public:
/// The kind of decoration that this type represents
static constexpr const DecorationKind Kind = DecorationKind::kBinding;
/// constructor /// constructor
/// @param value the binding value /// @param value the binding value
/// @param source the source of this decoration /// @param source the source of this decoration
BindingDecoration(uint32_t value, const Source& source); BindingDecoration(uint32_t value, const Source& source);
~BindingDecoration() override; ~BindingDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is a binding decoration /// @returns true if this is a binding decoration
bool IsBinding() const override; bool IsBinding() const override;

View File

@ -17,11 +17,17 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
constexpr const DecorationKind BuiltinDecoration::Kind;
BuiltinDecoration::BuiltinDecoration(Builtin builtin, const Source& source) BuiltinDecoration::BuiltinDecoration(Builtin builtin, const Source& source)
: VariableDecoration(source), builtin_(builtin) {} : VariableDecoration(Kind, source), builtin_(builtin) {}
BuiltinDecoration::~BuiltinDecoration() = default; BuiltinDecoration::~BuiltinDecoration() = default;
bool BuiltinDecoration::IsKind(DecorationKind kind) const {
return kind == Kind || VariableDecoration::IsKind(kind);
}
bool BuiltinDecoration::IsBuiltin() const { bool BuiltinDecoration::IsBuiltin() const {
return true; return true;
} }

View File

@ -24,12 +24,20 @@ namespace ast {
/// A builtin decoration /// A builtin decoration
class BuiltinDecoration : public VariableDecoration { class BuiltinDecoration : public VariableDecoration {
public: public:
/// The kind of decoration that this type represents
static constexpr const DecorationKind Kind = DecorationKind::kBuiltin;
/// constructor /// constructor
/// @param builtin the builtin value /// @param builtin the builtin value
/// @param source the source of this decoration /// @param source the source of this decoration
BuiltinDecoration(Builtin builtin, const Source& source); BuiltinDecoration(Builtin builtin, const Source& source);
~BuiltinDecoration() override; ~BuiltinDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is a builtin decoration /// @returns true if this is a builtin decoration
bool IsBuiltin() const override; bool IsBuiltin() const override;

View File

@ -17,11 +17,17 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
constexpr const DecorationKind ConstantIdDecoration::Kind;
ConstantIdDecoration::ConstantIdDecoration(uint32_t val, const Source& source) ConstantIdDecoration::ConstantIdDecoration(uint32_t val, const Source& source)
: VariableDecoration(source), value_(val) {} : VariableDecoration(Kind, source), value_(val) {}
ConstantIdDecoration::~ConstantIdDecoration() = default; ConstantIdDecoration::~ConstantIdDecoration() = default;
bool ConstantIdDecoration::IsKind(DecorationKind kind) const {
return kind == Kind || VariableDecoration::IsKind(kind);
}
bool ConstantIdDecoration::IsConstantId() const { bool ConstantIdDecoration::IsConstantId() const {
return true; return true;
} }

View File

@ -24,12 +24,20 @@ namespace ast {
/// A constant id decoration /// A constant id decoration
class ConstantIdDecoration : public VariableDecoration { class ConstantIdDecoration : public VariableDecoration {
public: public:
/// The kind of decoration that this type represents
static constexpr const DecorationKind Kind = DecorationKind::kConstantId;
/// constructor /// constructor
/// @param val the constant_id value /// @param val the constant_id value
/// @param source the source of this decoration /// @param source the source of this decoration
ConstantIdDecoration(uint32_t val, const Source& source); ConstantIdDecoration(uint32_t val, const Source& source);
~ConstantIdDecoration() override; ~ConstantIdDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is a constant_id decoration /// @returns true if this is a constant_id decoration
bool IsConstantId() const override; bool IsConstantId() const override;

View File

@ -23,14 +23,30 @@ std::ostream& operator<<(std::ostream& out, DecorationKind data) {
switch (data) { switch (data) {
case DecorationKind::kArray: case DecorationKind::kArray:
return out << "array"; return out << "array";
case DecorationKind::kStride:
return out << "stride";
case DecorationKind::kFunction: case DecorationKind::kFunction:
return out << "function"; return out << "function";
case DecorationKind::kStage:
return out << "stage";
case DecorationKind::kWorkgroup:
return out << "workgroup";
case DecorationKind::kStruct: case DecorationKind::kStruct:
return out << "struct"; return out << "struct";
case DecorationKind::kStructMember: case DecorationKind::kStructMember:
return out << "struct member"; return out << "struct member";
case DecorationKind::kStructMemberOffset:
return out << "offset";
case DecorationKind::kVariable: case DecorationKind::kVariable:
return out << "variable"; return out << "variable";
case DecorationKind::kBinding:
return out << "binding";
case DecorationKind::kBuiltin:
return out << "builtin";
case DecorationKind::kConstantId:
return out << "constant_id";
case DecorationKind::kLocation:
return out << "location";
} }
return out << "<unknown>"; return out << "<unknown>";
} }

View File

@ -28,10 +28,18 @@ namespace ast {
/// The decoration kind enumerator /// The decoration kind enumerator
enum class DecorationKind { enum class DecorationKind {
kArray, kArray,
/*|*/ kStride,
kFunction, kFunction,
/*|*/ kStage,
/*|*/ kWorkgroup,
kStruct, kStruct,
kStructMember, kStructMember,
kVariable /*|*/ kStructMemberOffset,
kVariable,
/*|*/ kBinding,
/*|*/ kBuiltin,
/*|*/ kConstantId,
/*|*/ kLocation,
}; };
std::ostream& operator<<(std::ostream& out, DecorationKind data); std::ostream& operator<<(std::ostream& out, DecorationKind data);
@ -41,15 +49,20 @@ class Decoration : public Node {
public: public:
~Decoration() override; ~Decoration() override;
/// @return the decoration kind /// @param kind the decoration kind
DecorationKind GetKind() const { return kind_; } /// @return true if this Decoration is of the (or derives from) the given
/// kind.
virtual bool IsKind(DecorationKind kind) const = 0;
/// @return true if this decoration is of (or derives from) type |TO| /// @return true if this decoration is of (or derives from) type |TO|
template <typename TO> template <typename TO>
bool Is() const { bool Is() const {
return GetKind() == TO::Kind; return IsKind(TO::Kind);
} }
/// @return the decoration kind
DecorationKind GetKind() const { return kind_; }
/// @returns true if the node is valid /// @returns true if the node is valid
bool IsValid() const override; bool IsValid() const override;

View File

@ -15,11 +15,24 @@
#include "src/ast/decoration.h" #include "src/ast/decoration.h"
#include <sstream> #include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <utility> #include <utility>
#include "src/ast/array_decoration.h" #include "src/ast/array_decoration.h"
#include "src/ast/binding_decoration.h"
#include "src/ast/builtin_decoration.h"
#include "src/ast/constant_id_decoration.h" #include "src/ast/constant_id_decoration.h"
#include "src/ast/function_decoration.h"
#include "src/ast/location_decoration.h"
#include "src/ast/stage_decoration.h"
#include "src/ast/stride_decoration.h"
#include "src/ast/struct_decoration.h"
#include "src/ast/struct_member_decoration.h"
#include "src/ast/struct_member_offset_decoration.h"
#include "src/ast/test_helper.h" #include "src/ast/test_helper.h"
#include "src/ast/variable_decoration.h"
#include "src/ast/workgroup_decoration.h"
namespace tint { namespace tint {
namespace ast { namespace ast {
@ -47,6 +60,120 @@ TEST_F(DecorationTest, Is) {
EXPECT_FALSE(decoration->Is<ArrayDecoration>()); EXPECT_FALSE(decoration->Is<ArrayDecoration>());
} }
TEST_F(DecorationTest, Kinds) {
EXPECT_EQ(ArrayDecoration::Kind, DecorationKind::kArray);
EXPECT_EQ(StrideDecoration::Kind, DecorationKind::kStride);
EXPECT_EQ(FunctionDecoration::Kind, DecorationKind::kFunction);
EXPECT_EQ(StageDecoration::Kind, DecorationKind::kStage);
EXPECT_EQ(WorkgroupDecoration::Kind, DecorationKind::kWorkgroup);
EXPECT_EQ(StructDecoration::Kind, DecorationKind::kStruct);
EXPECT_EQ(StructMemberDecoration::Kind, DecorationKind::kStructMember);
EXPECT_EQ(StructMemberOffsetDecoration::Kind,
DecorationKind::kStructMemberOffset);
EXPECT_EQ(VariableDecoration::Kind, DecorationKind::kVariable);
EXPECT_EQ(BindingDecoration::Kind, DecorationKind::kBinding);
EXPECT_EQ(BuiltinDecoration::Kind, DecorationKind::kBuiltin);
EXPECT_EQ(ConstantIdDecoration::Kind, DecorationKind::kConstantId);
EXPECT_EQ(LocationDecoration::Kind, DecorationKind::kLocation);
}
TEST_F(DecorationTest, IsKind) {
std::vector<DecorationKind> all_kinds{
DecorationKind::kArray, DecorationKind::kStride,
DecorationKind::kFunction, DecorationKind::kStage,
DecorationKind::kWorkgroup, DecorationKind::kStruct,
DecorationKind::kStructMember, DecorationKind::kStructMemberOffset,
DecorationKind::kVariable, DecorationKind::kBinding,
DecorationKind::kBuiltin, DecorationKind::kConstantId,
DecorationKind::kLocation,
};
struct ExpectedKinds {
DecorationKind kind;
std::unordered_set<DecorationKind> expect_true;
};
// kArray
// | kStride
// kFunction
// | kStage
// | kWorkgroup
// kStruct
// kStructMember
// | kStructMemberOffset
// kVariable
// | kBinding
// | kBuiltin
// | kConstantId
// | kLocation
std::unordered_map<DecorationKind, std::unordered_set<DecorationKind>>
kind_is{
{
DecorationKind::kStride,
{DecorationKind::kArray, DecorationKind::kStride},
},
{
DecorationKind::kStage,
{DecorationKind::kFunction, DecorationKind::kStage},
},
{
DecorationKind::kWorkgroup,
{DecorationKind::kFunction, DecorationKind::kWorkgroup},
},
{
DecorationKind::kStruct,
{DecorationKind::kStruct},
},
{
DecorationKind::kStructMemberOffset,
{DecorationKind::kStructMember,
DecorationKind::kStructMemberOffset},
},
{
DecorationKind::kBinding,
{DecorationKind::kVariable, DecorationKind::kBinding},
},
{
DecorationKind::kBuiltin,
{DecorationKind::kVariable, DecorationKind::kBuiltin},
},
{
DecorationKind::kConstantId,
{DecorationKind::kVariable, DecorationKind::kConstantId},
},
{
DecorationKind::kLocation,
{DecorationKind::kVariable, DecorationKind::kLocation},
},
};
auto check = [&](Decoration* d) {
auto& is_set = kind_is[d->GetKind()];
for (auto test : all_kinds) {
bool is_kind = is_set.find(test) != is_set.end();
EXPECT_EQ(d->IsKind(test), is_kind)
<< "decoration: " << d->GetKind() << " IsKind(" << test << ")";
}
};
StrideDecoration stride(0, {});
StageDecoration stage(PipelineStage::kNone, {});
WorkgroupDecoration workgroup(0, {});
StructMemberOffsetDecoration struct_member_offset(0, {});
BindingDecoration binding(0, {});
BuiltinDecoration builtin(Builtin::kNone, {});
ConstantIdDecoration constant_id(0, {});
LocationDecoration location(0, {});
check(&stride);
check(&stage);
check(&workgroup);
check(&struct_member_offset);
check(&binding);
check(&builtin);
check(&constant_id);
check(&location);
}
} // namespace } // namespace
} // namespace ast } // namespace ast
} // namespace tint } // namespace tint

View File

@ -22,11 +22,18 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
FunctionDecoration::FunctionDecoration(const Source& source) constexpr const DecorationKind FunctionDecoration::Kind;
: Decoration(Kind, source) {}
FunctionDecoration::FunctionDecoration(DecorationKind kind,
const Source& source)
: Decoration(kind, source) {}
FunctionDecoration::~FunctionDecoration() = default; FunctionDecoration::~FunctionDecoration() = default;
bool FunctionDecoration::IsKind(DecorationKind kind) const {
return kind == Kind;
}
bool FunctionDecoration::IsStage() const { bool FunctionDecoration::IsStage() const {
return false; return false;
} }

View File

@ -31,10 +31,15 @@ class WorkgroupDecoration;
class FunctionDecoration : public Decoration { class FunctionDecoration : public Decoration {
public: public:
/// The kind of decoration that this type represents /// The kind of decoration that this type represents
static constexpr DecorationKind Kind = DecorationKind::kFunction; static constexpr const DecorationKind Kind = DecorationKind::kFunction;
~FunctionDecoration() override; ~FunctionDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is a stage decoration /// @returns true if this is a stage decoration
virtual bool IsStage() const; virtual bool IsStage() const;
/// @returns true if this is a workgroup decoration /// @returns true if this is a workgroup decoration
@ -47,8 +52,9 @@ class FunctionDecoration : public Decoration {
protected: protected:
/// Constructor /// Constructor
/// @param kind the decoration kind
/// @param source the source of this decoration /// @param source the source of this decoration
explicit FunctionDecoration(const Source& source); FunctionDecoration(DecorationKind kind, const Source& source);
}; };
/// A list of function decorations /// A list of function decorations

View File

@ -17,11 +17,17 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
constexpr const DecorationKind LocationDecoration::Kind;
LocationDecoration::LocationDecoration(uint32_t val, const Source& source) LocationDecoration::LocationDecoration(uint32_t val, const Source& source)
: VariableDecoration(source), value_(val) {} : VariableDecoration(Kind, source), value_(val) {}
LocationDecoration::~LocationDecoration() = default; LocationDecoration::~LocationDecoration() = default;
bool LocationDecoration::IsKind(DecorationKind kind) const {
return kind == Kind || VariableDecoration::IsKind(kind);
}
bool LocationDecoration::IsLocation() const { bool LocationDecoration::IsLocation() const {
return true; return true;
} }

View File

@ -25,12 +25,20 @@ namespace ast {
/// A location decoration /// A location decoration
class LocationDecoration : public VariableDecoration { class LocationDecoration : public VariableDecoration {
public: public:
/// The kind of decoration that this type represents
static constexpr const DecorationKind Kind = DecorationKind::kLocation;
/// constructor /// constructor
/// @param value the location value /// @param value the location value
/// @param source the source of this decoration /// @param source the source of this decoration
explicit LocationDecoration(uint32_t value, const Source& source); explicit LocationDecoration(uint32_t value, const Source& source);
~LocationDecoration() override; ~LocationDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is a location decoration /// @returns true if this is a location decoration
bool IsLocation() const override; bool IsLocation() const override;

View File

@ -18,7 +18,7 @@ namespace tint {
namespace ast { namespace ast {
SetDecoration::SetDecoration(uint32_t val, const Source& source) SetDecoration::SetDecoration(uint32_t val, const Source& source)
: VariableDecoration(source), value_(val) {} : VariableDecoration(Kind, source), value_(val) {}
SetDecoration::~SetDecoration() = default; SetDecoration::~SetDecoration() = default;

View File

@ -17,11 +17,17 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
constexpr const DecorationKind StageDecoration::Kind;
StageDecoration::StageDecoration(ast::PipelineStage stage, const Source& source) StageDecoration::StageDecoration(ast::PipelineStage stage, const Source& source)
: FunctionDecoration(source), stage_(stage) {} : FunctionDecoration(Kind, source), stage_(stage) {}
StageDecoration::~StageDecoration() = default; StageDecoration::~StageDecoration() = default;
bool StageDecoration::IsKind(DecorationKind kind) const {
return kind == Kind || FunctionDecoration::IsKind(kind);
}
bool StageDecoration::IsStage() const { bool StageDecoration::IsStage() const {
return true; return true;
} }

View File

@ -24,12 +24,20 @@ namespace ast {
/// A workgroup decoration /// A workgroup decoration
class StageDecoration : public FunctionDecoration { class StageDecoration : public FunctionDecoration {
public: public:
/// The kind of decoration that this type represents
static constexpr const DecorationKind Kind = DecorationKind::kStage;
/// constructor /// constructor
/// @param stage the pipeline stage /// @param stage the pipeline stage
/// @param source the source of this decoration /// @param source the source of this decoration
StageDecoration(ast::PipelineStage stage, const Source& source); StageDecoration(ast::PipelineStage stage, const Source& source);
~StageDecoration() override; ~StageDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is a stage decoration /// @returns true if this is a stage decoration
bool IsStage() const override; bool IsStage() const override;

View File

@ -17,8 +17,14 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
constexpr const DecorationKind StrideDecoration::Kind;
StrideDecoration::StrideDecoration(uint32_t stride, const Source& source) StrideDecoration::StrideDecoration(uint32_t stride, const Source& source)
: ArrayDecoration(source), stride_(stride) {} : ArrayDecoration(Kind, source), stride_(stride) {}
bool StrideDecoration::IsKind(DecorationKind kind) const {
return kind == Kind || ArrayDecoration::IsKind(kind);
}
bool StrideDecoration::IsStride() const { bool StrideDecoration::IsStride() const {
return true; return true;

View File

@ -27,12 +27,20 @@ namespace ast {
/// A stride decoration /// A stride decoration
class StrideDecoration : public ArrayDecoration { class StrideDecoration : public ArrayDecoration {
public: public:
/// The kind of decoration that this type represents
static constexpr const DecorationKind Kind = DecorationKind::kStride;
/// constructor /// constructor
/// @param stride the stride value /// @param stride the stride value
/// @param source the source of this decoration /// @param source the source of this decoration
StrideDecoration(uint32_t stride, const Source& source); StrideDecoration(uint32_t stride, const Source& source);
~StrideDecoration() override; ~StrideDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is a stride decoration /// @returns true if this is a stride decoration
bool IsStride() const override; bool IsStride() const override;

View File

@ -18,7 +18,7 @@ namespace tint {
namespace ast { namespace ast {
StructBlockDecoration::StructBlockDecoration(const Source& source) StructBlockDecoration::StructBlockDecoration(const Source& source)
: StructDecoration(source) {} : StructDecoration(Kind, source) {}
StructBlockDecoration::~StructBlockDecoration() = default; StructBlockDecoration::~StructBlockDecoration() = default;

View File

@ -17,10 +17,16 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
StructDecoration::StructDecoration(const Source& source) constexpr const DecorationKind StructDecoration::Kind;
: Decoration(Kind, source) {}
StructDecoration::StructDecoration(DecorationKind kind, const Source& source)
: Decoration(kind, source) {}
StructDecoration::~StructDecoration() = default; StructDecoration::~StructDecoration() = default;
bool StructDecoration::IsKind(DecorationKind kind) const {
return kind == Kind;
}
} // namespace ast } // namespace ast
} // namespace tint } // namespace tint

View File

@ -28,17 +28,23 @@ namespace ast {
class StructDecoration : public Decoration { class StructDecoration : public Decoration {
public: public:
/// The kind of decoration that this type represents /// The kind of decoration that this type represents
static constexpr DecorationKind Kind = DecorationKind::kStruct; static constexpr const DecorationKind Kind = DecorationKind::kStruct;
~StructDecoration() override; ~StructDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is a block struct /// @returns true if this is a block struct
virtual bool IsBlock() const = 0; virtual bool IsBlock() const = 0;
protected: protected:
/// Constructor /// Constructor
/// @param kind the decoration kind
/// @param source the source of this decoration /// @param source the source of this decoration
explicit StructDecoration(const Source& source); explicit StructDecoration(DecorationKind kind, const Source& source);
}; };
/// List of struct decorations /// List of struct decorations

View File

@ -21,11 +21,18 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
StructMemberDecoration::StructMemberDecoration(const Source& source) constexpr const DecorationKind StructMemberDecoration::Kind;
: Decoration(DecorationKind::kStructMember, source) {}
StructMemberDecoration::StructMemberDecoration(DecorationKind kind,
const Source& source)
: Decoration(kind, source) {}
StructMemberDecoration::~StructMemberDecoration() = default; StructMemberDecoration::~StructMemberDecoration() = default;
bool StructMemberDecoration::IsKind(DecorationKind kind) const {
return kind == Kind;
}
bool StructMemberDecoration::IsOffset() const { bool StructMemberDecoration::IsOffset() const {
return false; return false;
} }

View File

@ -30,10 +30,15 @@ class StructMemberOffsetDecoration;
class StructMemberDecoration : public Decoration { class StructMemberDecoration : public Decoration {
public: public:
/// The kind of decoration that this type represents /// The kind of decoration that this type represents
static constexpr DecorationKind Kind = DecorationKind::kStructMember; static constexpr const DecorationKind Kind = DecorationKind::kStructMember;
~StructMemberDecoration() override; ~StructMemberDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is an offset decoration /// @returns true if this is an offset decoration
virtual bool IsOffset() const; virtual bool IsOffset() const;
@ -42,8 +47,9 @@ class StructMemberDecoration : public Decoration {
protected: protected:
/// Constructor /// Constructor
/// @param kind the decoration kind
/// @param source the source of this decoration /// @param source the source of this decoration
explicit StructMemberDecoration(const Source& source); explicit StructMemberDecoration(DecorationKind kind, const Source& source);
}; };
/// A list of struct member decorations /// A list of struct member decorations

View File

@ -17,9 +17,15 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
constexpr const DecorationKind StructMemberOffsetDecoration::Kind;
StructMemberOffsetDecoration::StructMemberOffsetDecoration(uint32_t offset, StructMemberOffsetDecoration::StructMemberOffsetDecoration(uint32_t offset,
const Source& source) const Source& source)
: StructMemberDecoration(source), offset_(offset) {} : StructMemberDecoration(Kind, source), offset_(offset) {}
bool StructMemberOffsetDecoration::IsKind(DecorationKind kind) const {
return kind == Kind || StructMemberDecoration::IsKind(kind);
}
bool StructMemberOffsetDecoration::IsOffset() const { bool StructMemberOffsetDecoration::IsOffset() const {
return true; return true;

View File

@ -27,12 +27,21 @@ namespace ast {
/// A struct member offset decoration /// A struct member offset decoration
class StructMemberOffsetDecoration : public StructMemberDecoration { class StructMemberOffsetDecoration : public StructMemberDecoration {
public: public:
/// The kind of decoration that this type represents
static constexpr const DecorationKind Kind =
DecorationKind::kStructMemberOffset;
/// constructor /// constructor
/// @param offset the offset value /// @param offset the offset value
/// @param source the source of this decoration /// @param source the source of this decoration
StructMemberOffsetDecoration(uint32_t offset, const Source& source); StructMemberOffsetDecoration(uint32_t offset, const Source& source);
~StructMemberOffsetDecoration() override; ~StructMemberOffsetDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is an offset decoration /// @returns true if this is an offset decoration
bool IsOffset() const override; bool IsOffset() const override;

View File

@ -25,11 +25,18 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
VariableDecoration::VariableDecoration(const Source& source) constexpr const DecorationKind VariableDecoration::Kind;
: Decoration(Kind, source) {}
VariableDecoration::VariableDecoration(DecorationKind kind,
const Source& source)
: Decoration(kind, source) {}
VariableDecoration::~VariableDecoration() = default; VariableDecoration::~VariableDecoration() = default;
bool VariableDecoration::IsKind(DecorationKind kind) const {
return kind == Kind;
}
bool VariableDecoration::IsBinding() const { bool VariableDecoration::IsBinding() const {
return false; return false;
} }

View File

@ -35,10 +35,15 @@ class SetDecoration;
class VariableDecoration : public Decoration { class VariableDecoration : public Decoration {
public: public:
/// The kind of decoration that this type represents /// The kind of decoration that this type represents
static constexpr DecorationKind Kind = DecorationKind::kVariable; static constexpr const DecorationKind Kind = DecorationKind::kVariable;
~VariableDecoration() override; ~VariableDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is a binding decoration /// @returns true if this is a binding decoration
virtual bool IsBinding() const; virtual bool IsBinding() const;
/// @returns true if this is a builtin decoration /// @returns true if this is a builtin decoration
@ -63,8 +68,9 @@ class VariableDecoration : public Decoration {
protected: protected:
/// Constructor /// Constructor
/// @param kind the decoration kind
/// @param source the source of this decoration /// @param source the source of this decoration
explicit VariableDecoration(const Source& source); VariableDecoration(DecorationKind kind, const Source& source);
}; };
/// A list of variable decorations /// A list of variable decorations

View File

@ -17,22 +17,28 @@
namespace tint { namespace tint {
namespace ast { namespace ast {
constexpr const DecorationKind WorkgroupDecoration::Kind;
WorkgroupDecoration::WorkgroupDecoration(uint32_t x, const Source& source) WorkgroupDecoration::WorkgroupDecoration(uint32_t x, const Source& source)
: FunctionDecoration(source), x_(x) {} : FunctionDecoration(Kind, source), x_(x) {}
WorkgroupDecoration::WorkgroupDecoration(uint32_t x, WorkgroupDecoration::WorkgroupDecoration(uint32_t x,
uint32_t y, uint32_t y,
const Source& source) const Source& source)
: FunctionDecoration(source), x_(x), y_(y) {} : FunctionDecoration(Kind, source), x_(x), y_(y) {}
WorkgroupDecoration::WorkgroupDecoration(uint32_t x, WorkgroupDecoration::WorkgroupDecoration(uint32_t x,
uint32_t y, uint32_t y,
uint32_t z, uint32_t z,
const Source& source) const Source& source)
: FunctionDecoration(source), x_(x), y_(y), z_(z) {} : FunctionDecoration(Kind, source), x_(x), y_(y), z_(z) {}
WorkgroupDecoration::~WorkgroupDecoration() = default; WorkgroupDecoration::~WorkgroupDecoration() = default;
bool WorkgroupDecoration::IsKind(DecorationKind kind) const {
return kind == Kind || FunctionDecoration::IsKind(kind);
}
bool WorkgroupDecoration::IsWorkgroup() const { bool WorkgroupDecoration::IsWorkgroup() const {
return true; return true;
} }

View File

@ -27,6 +27,9 @@ namespace ast {
/// A workgroup decoration /// A workgroup decoration
class WorkgroupDecoration : public FunctionDecoration { class WorkgroupDecoration : public FunctionDecoration {
public: public:
/// The kind of decoration that this type represents
static constexpr const DecorationKind Kind = DecorationKind::kWorkgroup;
/// constructor /// constructor
/// @param x the workgroup x dimension size /// @param x the workgroup x dimension size
/// @param source the source of this decoration /// @param source the source of this decoration
@ -44,6 +47,11 @@ class WorkgroupDecoration : public FunctionDecoration {
WorkgroupDecoration(uint32_t x, uint32_t y, uint32_t z, const Source& source); WorkgroupDecoration(uint32_t x, uint32_t y, uint32_t z, const Source& source);
~WorkgroupDecoration() override; ~WorkgroupDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is a workgroup decoration /// @returns true if this is a workgroup decoration
bool IsWorkgroup() const override; bool IsWorkgroup() const override;