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