Replace VariableDecoration::(Is|As)Builtin with Castable
Change-Id: I49d970301c46cfe29d7b22e18abb443daa0c8073 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34307 Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
parent
62d81485dd
commit
c0eb9aeafb
|
@ -28,12 +28,13 @@ TEST_F(BindingDecorationTest, Creation) {
|
|||
}
|
||||
|
||||
TEST_F(BindingDecorationTest, Is) {
|
||||
BindingDecoration d{2, Source{}};
|
||||
EXPECT_TRUE(d.Is<BindingDecoration>());
|
||||
EXPECT_FALSE(d.IsBuiltin());
|
||||
EXPECT_FALSE(d.IsConstantId());
|
||||
EXPECT_FALSE(d.IsLocation());
|
||||
EXPECT_FALSE(d.IsSet());
|
||||
BindingDecoration bd{2, Source{}};
|
||||
Decoration* d = &bd;
|
||||
EXPECT_TRUE(d->Is<BindingDecoration>());
|
||||
EXPECT_FALSE(d->Is<BuiltinDecoration>());
|
||||
EXPECT_FALSE(bd.IsConstantId());
|
||||
EXPECT_FALSE(bd.IsLocation());
|
||||
EXPECT_FALSE(bd.IsSet());
|
||||
}
|
||||
|
||||
TEST_F(BindingDecorationTest, ToStr) {
|
||||
|
|
|
@ -22,10 +22,6 @@ BuiltinDecoration::BuiltinDecoration(Builtin builtin, const Source& source)
|
|||
|
||||
BuiltinDecoration::~BuiltinDecoration() = default;
|
||||
|
||||
bool BuiltinDecoration::IsBuiltin() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void BuiltinDecoration::to_str(std::ostream& out, size_t indent) const {
|
||||
make_indent(out, indent);
|
||||
out << "BuiltinDecoration{" << builtin_ << "}" << std::endl;
|
||||
|
|
|
@ -31,9 +31,6 @@ class BuiltinDecoration
|
|||
BuiltinDecoration(Builtin builtin, const Source& source);
|
||||
~BuiltinDecoration() override;
|
||||
|
||||
/// @returns true if this is a builtin decoration
|
||||
bool IsBuiltin() const override;
|
||||
|
||||
/// @returns the builtin value
|
||||
Builtin value() const { return builtin_; }
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ TEST_F(BuiltinDecorationTest, Is) {
|
|||
BuiltinDecoration bd{Builtin::kFragDepth, Source{}};
|
||||
Decoration* d = &bd;
|
||||
EXPECT_FALSE(d->Is<BindingDecoration>());
|
||||
EXPECT_TRUE(bd.IsBuiltin());
|
||||
EXPECT_TRUE(d->Is<BuiltinDecoration>());
|
||||
EXPECT_FALSE(bd.IsConstantId());
|
||||
EXPECT_FALSE(bd.IsLocation());
|
||||
EXPECT_FALSE(bd.IsSet());
|
||||
|
|
|
@ -31,7 +31,7 @@ TEST_F(ConstantIdDecorationTest, Is) {
|
|||
ConstantIdDecoration cd{27, Source{}};
|
||||
Decoration* d = &cd;
|
||||
EXPECT_FALSE(d->Is<BindingDecoration>());
|
||||
EXPECT_FALSE(cd.IsBuiltin());
|
||||
EXPECT_FALSE(d->Is<BuiltinDecoration>());
|
||||
EXPECT_TRUE(cd.IsConstantId());
|
||||
EXPECT_FALSE(cd.IsLocation());
|
||||
EXPECT_FALSE(cd.IsSet());
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
#include <cassert>
|
||||
|
||||
#include "src/ast/builtin_decoration.h"
|
||||
#include "src/ast/constant_id_decoration.h"
|
||||
|
||||
namespace tint {
|
||||
|
@ -41,7 +42,7 @@ bool DecoratedVariable::HasLocationDecoration() const {
|
|||
|
||||
bool DecoratedVariable::HasBuiltinDecoration() const {
|
||||
for (auto* deco : decorations_) {
|
||||
if (deco->IsBuiltin()) {
|
||||
if (deco->Is<BuiltinDecoration>()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -163,8 +163,8 @@ Function::referenced_builtin_variables() const {
|
|||
continue;
|
||||
}
|
||||
for (auto* deco : var->AsDecorated()->decorations()) {
|
||||
if (deco->IsBuiltin()) {
|
||||
ret.push_back({var, deco->AsBuiltin()});
|
||||
if (auto* builtin = deco->As<BuiltinDecoration>()) {
|
||||
ret.push_back({var, builtin});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ TEST_F(LocationDecorationTest, Is) {
|
|||
LocationDecoration ld{2, Source{}};
|
||||
Decoration* d = &ld;
|
||||
EXPECT_FALSE(d->Is<BindingDecoration>());
|
||||
EXPECT_FALSE(ld.IsBuiltin());
|
||||
EXPECT_FALSE(d->Is<BuiltinDecoration>());
|
||||
EXPECT_FALSE(ld.IsConstantId());
|
||||
EXPECT_TRUE(ld.IsLocation());
|
||||
EXPECT_FALSE(ld.IsSet());
|
||||
|
|
|
@ -31,7 +31,7 @@ TEST_F(SetDecorationTest, Is) {
|
|||
SetDecoration sd{2, Source{}};
|
||||
Decoration* d = &sd;
|
||||
EXPECT_FALSE(d->Is<BindingDecoration>());
|
||||
EXPECT_FALSE(sd.IsBuiltin());
|
||||
EXPECT_FALSE(d->Is<BuiltinDecoration>());
|
||||
EXPECT_FALSE(sd.IsConstantId());
|
||||
EXPECT_FALSE(sd.IsLocation());
|
||||
EXPECT_TRUE(sd.IsSet());
|
||||
|
|
|
@ -35,10 +35,6 @@ DecorationKind VariableDecoration::GetKind() const {
|
|||
return Kind;
|
||||
}
|
||||
|
||||
bool VariableDecoration::IsBuiltin() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool VariableDecoration::IsLocation() const {
|
||||
return false;
|
||||
}
|
||||
|
@ -51,11 +47,6 @@ bool VariableDecoration::IsSet() const {
|
|||
return false;
|
||||
}
|
||||
|
||||
BuiltinDecoration* VariableDecoration::AsBuiltin() {
|
||||
assert(IsBuiltin());
|
||||
return static_cast<BuiltinDecoration*>(this);
|
||||
}
|
||||
|
||||
ConstantIdDecoration* VariableDecoration::AsConstantId() {
|
||||
assert(IsConstantId());
|
||||
return static_cast<ConstantIdDecoration*>(this);
|
||||
|
|
|
@ -25,7 +25,6 @@
|
|||
namespace tint {
|
||||
namespace ast {
|
||||
|
||||
class BuiltinDecoration;
|
||||
class ConstantIdDecoration;
|
||||
class LocationDecoration;
|
||||
class SetDecoration;
|
||||
|
@ -41,8 +40,6 @@ class VariableDecoration : public Castable<VariableDecoration, Decoration> {
|
|||
/// @return the decoration kind
|
||||
DecorationKind GetKind() const override;
|
||||
|
||||
/// @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
|
||||
|
@ -50,8 +47,6 @@ class VariableDecoration : public Castable<VariableDecoration, Decoration> {
|
|||
/// @returns true if this is a set decoration
|
||||
virtual bool IsSet() const;
|
||||
|
||||
/// @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
|
||||
|
|
|
@ -38,8 +38,9 @@ TEST_F(ParserImplTest, VariableDecorationList_Parses) {
|
|||
|
||||
ASSERT_TRUE(deco_0->IsLocation());
|
||||
EXPECT_EQ(deco_0->AsLocation()->value(), 4u);
|
||||
ASSERT_TRUE(deco_1->IsBuiltin());
|
||||
EXPECT_EQ(deco_1->AsBuiltin()->value(), ast::Builtin::kPosition);
|
||||
ASSERT_TRUE(deco_1->Is<ast::BuiltinDecoration>());
|
||||
EXPECT_EQ(deco_1->As<ast::BuiltinDecoration>()->value(),
|
||||
ast::Builtin::kPosition);
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, VariableDecorationList_Empty) {
|
||||
|
|
|
@ -104,9 +104,9 @@ TEST_P(BuiltinTest, VariableDecoration_Builtin) {
|
|||
auto* var_deco = deco.value->As<ast::VariableDecoration>();
|
||||
ASSERT_FALSE(p->has_error()) << p->error();
|
||||
ASSERT_NE(var_deco, nullptr);
|
||||
ASSERT_TRUE(var_deco->IsBuiltin());
|
||||
ASSERT_TRUE(var_deco->Is<ast::BuiltinDecoration>());
|
||||
|
||||
auto* builtin = var_deco->AsBuiltin();
|
||||
auto* builtin = var_deco->As<ast::BuiltinDecoration>();
|
||||
EXPECT_EQ(builtin->value(), params.result);
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
|
|
|
@ -127,8 +127,9 @@ void VertexPullingTransform::FindOrInsertVertexIndexIfUsed() {
|
|||
}
|
||||
|
||||
for (auto* d : v->AsDecorated()->decorations()) {
|
||||
if (d->IsBuiltin() &&
|
||||
d->AsBuiltin()->value() == ast::Builtin::kVertexIdx) {
|
||||
if (d->Is<ast::BuiltinDecoration>() &&
|
||||
d->As<ast::BuiltinDecoration>()->value() ==
|
||||
ast::Builtin::kVertexIdx) {
|
||||
vertex_index_name_ = v->name();
|
||||
return;
|
||||
}
|
||||
|
@ -169,8 +170,9 @@ void VertexPullingTransform::FindOrInsertInstanceIndexIfUsed() {
|
|||
}
|
||||
|
||||
for (auto* d : v->AsDecorated()->decorations()) {
|
||||
if (d->IsBuiltin() &&
|
||||
d->AsBuiltin()->value() == ast::Builtin::kInstanceIdx) {
|
||||
if (d->Is<ast::BuiltinDecoration>() &&
|
||||
d->As<ast::BuiltinDecoration>()->value() ==
|
||||
ast::Builtin::kInstanceIdx) {
|
||||
instance_index_name_ = v->name();
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1383,8 +1383,9 @@ bool GeneratorImpl::EmitEntryPointData(
|
|||
return false;
|
||||
}
|
||||
out << "TEXCOORD" << deco->AsLocation()->value();
|
||||
} else if (deco->IsBuiltin()) {
|
||||
auto attr = builtin_to_attribute(deco->AsBuiltin()->value());
|
||||
} else if (deco->Is<ast::BuiltinDecoration>()) {
|
||||
auto attr =
|
||||
builtin_to_attribute(deco->As<ast::BuiltinDecoration>()->value());
|
||||
if (attr.empty()) {
|
||||
error_ = "unsupported builtin";
|
||||
return false;
|
||||
|
@ -1433,8 +1434,9 @@ bool GeneratorImpl::EmitEntryPointData(
|
|||
error_ = "invalid location variable for pipeline stage";
|
||||
return false;
|
||||
}
|
||||
} else if (deco->IsBuiltin()) {
|
||||
auto attr = builtin_to_attribute(deco->AsBuiltin()->value());
|
||||
} else if (deco->Is<ast::BuiltinDecoration>()) {
|
||||
auto attr =
|
||||
builtin_to_attribute(deco->As<ast::BuiltinDecoration>()->value());
|
||||
if (attr.empty()) {
|
||||
error_ = "unsupported builtin";
|
||||
return false;
|
||||
|
|
|
@ -1070,8 +1070,9 @@ bool GeneratorImpl::EmitEntryPointData(ast::Function* func) {
|
|||
error_ = "invalid location variable for pipeline stage";
|
||||
return false;
|
||||
}
|
||||
} else if (deco->IsBuiltin()) {
|
||||
auto attr = builtin_to_attribute(deco->AsBuiltin()->value());
|
||||
} else if (deco->Is<ast::BuiltinDecoration>()) {
|
||||
auto attr =
|
||||
builtin_to_attribute(deco->As<ast::BuiltinDecoration>()->value());
|
||||
if (attr.empty()) {
|
||||
error_ = "unsupported builtin";
|
||||
return false;
|
||||
|
|
|
@ -779,10 +779,10 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) {
|
|||
|
||||
if (var->IsDecorated()) {
|
||||
for (auto* deco : var->AsDecorated()->decorations()) {
|
||||
if (deco->IsBuiltin()) {
|
||||
if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
|
||||
push_annot(spv::Op::OpDecorate,
|
||||
{Operand::Int(var_id), Operand::Int(SpvDecorationBuiltIn),
|
||||
Operand::Int(ConvertBuiltin(deco->AsBuiltin()->value()))});
|
||||
Operand::Int(ConvertBuiltin(builtin->value()))});
|
||||
} else if (deco->IsLocation()) {
|
||||
push_annot(spv::Op::OpDecorate,
|
||||
{Operand::Int(var_id), Operand::Int(SpvDecorationLocation),
|
||||
|
|
|
@ -647,8 +647,8 @@ bool GeneratorImpl::EmitVariableDecorations(ast::DecoratedVariable* var) {
|
|||
out_ << "set(" << deco->AsSet()->value() << ")";
|
||||
} else if (deco->IsLocation()) {
|
||||
out_ << "location(" << deco->AsLocation()->value() << ")";
|
||||
} else if (deco->IsBuiltin()) {
|
||||
out_ << "builtin(" << deco->AsBuiltin()->value() << ")";
|
||||
} else if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
|
||||
out_ << "builtin(" << builtin->value() << ")";
|
||||
} else if (deco->IsConstantId()) {
|
||||
out_ << "constant_id(" << deco->AsConstantId()->value() << ")";
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue