Replace VariableDecoration::(Is|As)Location with Castable

Change-Id: I3096045fc093f3782401ce8623a18e0bca49b22c
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34309
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-30 23:30:58 +00:00
parent 0482b54782
commit 2c1d7d5be7
18 changed files with 39 additions and 66 deletions

View File

@ -14,8 +14,8 @@
#include "src/ast/binding_decoration.h"
#include "src/ast/test_helper.h"
#include "src/ast/constant_id_decoration.h"
#include "src/ast/test_helper.h"
namespace tint {
namespace ast {
@ -34,7 +34,7 @@ TEST_F(BindingDecorationTest, Is) {
EXPECT_TRUE(d->Is<BindingDecoration>());
EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>());
EXPECT_FALSE(bd.IsLocation());
EXPECT_FALSE(d->Is<LocationDecoration>());
EXPECT_FALSE(bd.IsSet());
}

View File

@ -34,7 +34,7 @@ TEST_F(BuiltinDecorationTest, Is) {
EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_TRUE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>());
EXPECT_FALSE(bd.IsLocation());
EXPECT_FALSE(d->Is<LocationDecoration>());
EXPECT_FALSE(bd.IsSet());
}

View File

@ -33,7 +33,7 @@ TEST_F(ConstantIdDecorationTest, Is) {
EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_TRUE(d->Is<ConstantIdDecoration>());
EXPECT_FALSE(cd.IsLocation());
EXPECT_FALSE(d->Is<LocationDecoration>());
EXPECT_FALSE(cd.IsSet());
}

View File

@ -18,6 +18,7 @@
#include "src/ast/builtin_decoration.h"
#include "src/ast/constant_id_decoration.h"
#include "src/ast/location_decoration.h"
namespace tint {
namespace ast {
@ -33,7 +34,7 @@ DecoratedVariable::~DecoratedVariable() = default;
bool DecoratedVariable::HasLocationDecoration() const {
for (auto* deco : decorations_) {
if (deco->IsLocation()) {
if (deco->Is<LocationDecoration>()) {
return true;
}
}

View File

@ -89,8 +89,8 @@ Function::referenced_location_variables() const {
continue;
}
for (auto* deco : var->AsDecorated()->decorations()) {
if (deco->IsLocation()) {
ret.push_back({var, deco->AsLocation()});
if (auto* location = deco->As<LocationDecoration>()) {
ret.push_back({var, location});
break;
}
}

View File

@ -22,10 +22,6 @@ LocationDecoration::LocationDecoration(uint32_t val, const Source& source)
LocationDecoration::~LocationDecoration() = default;
bool LocationDecoration::IsLocation() const {
return true;
}
void LocationDecoration::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << "LocationDecoration{" << value_ << "}" << std::endl;

View File

@ -32,9 +32,6 @@ class LocationDecoration
explicit LocationDecoration(uint32_t value, const Source& source);
~LocationDecoration() override;
/// @returns true if this is a location decoration
bool IsLocation() const override;
/// @returns the location value
uint32_t value() const { return value_; }

View File

@ -36,7 +36,7 @@ TEST_F(LocationDecorationTest, Is) {
EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>());
EXPECT_TRUE(ld.IsLocation());
EXPECT_TRUE(d->Is<LocationDecoration>());
EXPECT_FALSE(ld.IsSet());
}

View File

@ -34,7 +34,7 @@ TEST_F(SetDecorationTest, Is) {
EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>());
EXPECT_FALSE(sd.IsLocation());
EXPECT_FALSE(d->Is<LocationDecoration>());
EXPECT_TRUE(sd.IsSet());
}

View File

@ -16,7 +16,6 @@
#include <assert.h>
#include "src/ast/location_decoration.h"
#include "src/ast/set_decoration.h"
namespace tint {
@ -32,19 +31,10 @@ DecorationKind VariableDecoration::GetKind() const {
return Kind;
}
bool VariableDecoration::IsLocation() const {
return false;
}
bool VariableDecoration::IsSet() const {
return false;
}
LocationDecoration* VariableDecoration::AsLocation() {
assert(IsLocation());
return static_cast<LocationDecoration*>(this);
}
SetDecoration* VariableDecoration::AsSet() {
assert(IsSet());
return static_cast<SetDecoration*>(this);

View File

@ -25,8 +25,6 @@
namespace tint {
namespace ast {
class ConstantIdDecoration;
class LocationDecoration;
class SetDecoration;
/// A decoration attached to a variable
@ -40,13 +38,9 @@ class VariableDecoration : public Castable<VariableDecoration, Decoration> {
/// @return the decoration kind
DecorationKind GetKind() const override;
/// @returns true if this is a location decoration
virtual bool IsLocation() const;
/// @returns true if this is a set decoration
virtual bool IsSet() const;
/// @returns the decoration as a location decoration
LocationDecoration* AsLocation();
/// @returns the decoration as a set decoration
SetDecoration* AsSet();

View File

@ -36,8 +36,8 @@ TEST_F(ParserImplTest, VariableDecorationList_Parses) {
ASSERT_NE(deco_0, nullptr);
ASSERT_NE(deco_1, nullptr);
ASSERT_TRUE(deco_0->IsLocation());
EXPECT_EQ(deco_0->AsLocation()->value(), 4u);
ASSERT_TRUE(deco_0->Is<ast::LocationDecoration>());
EXPECT_EQ(deco_0->As<ast::LocationDecoration>()->value(), 4u);
ASSERT_TRUE(deco_1->Is<ast::BuiltinDecoration>());
EXPECT_EQ(deco_1->As<ast::BuiltinDecoration>()->value(),
ast::Builtin::kPosition);

View File

@ -34,9 +34,9 @@ TEST_F(ParserImplTest, VariableDecoration_Location) {
auto* var_deco = deco.value->As<ast::VariableDecoration>();
ASSERT_NE(var_deco, nullptr);
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(var_deco->IsLocation());
ASSERT_TRUE(var_deco->Is<ast::LocationDecoration>());
auto* loc = var_deco->AsLocation();
auto* loc = var_deco->As<ast::LocationDecoration>();
EXPECT_EQ(loc->value(), 4u);
}

View File

@ -200,18 +200,16 @@ void VertexPullingTransform::ConvertVertexInputVariablesToPrivate() {
}
for (auto* d : v->AsDecorated()->decorations()) {
if (!d->IsLocation()) {
continue;
if (auto* l = d->As<ast::LocationDecoration>()) {
uint32_t location = l->value();
// This is where the replacement happens. Expressions use identifier
// strings instead of pointers, so we don't need to update any other
// place in the AST.
v = create<ast::Variable>(v->name(), ast::StorageClass::kPrivate,
v->type());
location_to_var_[location] = v;
break;
}
uint32_t location = d->AsLocation()->value();
// This is where the replacement happens. Expressions use identifier
// strings instead of pointers, so we don't need to update any other place
// in the AST.
v = create<ast::Variable>(v->name(), ast::StorageClass::kPrivate,
v->type());
location_to_var_[location] = v;
break;
}
}
}

View File

@ -1377,15 +1377,14 @@ bool GeneratorImpl::EmitEntryPointData(
}
out << " " << var->name() << " : ";
if (deco->IsLocation()) {
if (auto* location = deco->As<ast::LocationDecoration>()) {
if (func->pipeline_stage() == ast::PipelineStage::kCompute) {
error_ = "invalid location variable for pipeline stage";
return false;
}
out << "TEXCOORD" << deco->AsLocation()->value();
} else if (deco->Is<ast::BuiltinDecoration>()) {
auto attr =
builtin_to_attribute(deco->As<ast::BuiltinDecoration>()->value());
out << "TEXCOORD" << location->value();
} else if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
auto attr = builtin_to_attribute(builtin->value());
if (attr.empty()) {
error_ = "unsupported builtin";
return false;
@ -1424,8 +1423,8 @@ bool GeneratorImpl::EmitEntryPointData(
out << " " << var->name() << " : ";
if (deco->IsLocation()) {
auto loc = deco->AsLocation()->value();
if (auto* location = deco->As<ast::LocationDecoration>()) {
auto loc = location->value();
if (func->pipeline_stage() == ast::PipelineStage::kVertex) {
out << "TEXCOORD" << loc;
} else if (func->pipeline_stage() == ast::PipelineStage::kFragment) {
@ -1434,9 +1433,8 @@ bool GeneratorImpl::EmitEntryPointData(
error_ = "invalid location variable for pipeline stage";
return false;
}
} else if (deco->Is<ast::BuiltinDecoration>()) {
auto attr =
builtin_to_attribute(deco->As<ast::BuiltinDecoration>()->value());
} else if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
auto attr = builtin_to_attribute(builtin->value());
if (attr.empty()) {
error_ = "unsupported builtin";
return false;

View File

@ -1060,8 +1060,8 @@ bool GeneratorImpl::EmitEntryPointData(ast::Function* func) {
out_ << " " << var->name() << " [[";
if (deco->IsLocation()) {
auto loc = deco->AsLocation()->value();
if (auto* location = deco->As<ast::LocationDecoration>()) {
auto loc = location->value();
if (func->pipeline_stage() == ast::PipelineStage::kVertex) {
out_ << "user(locn" << loc << ")";
} else if (func->pipeline_stage() == ast::PipelineStage::kFragment) {
@ -1070,9 +1070,8 @@ bool GeneratorImpl::EmitEntryPointData(ast::Function* func) {
error_ = "invalid location variable for pipeline stage";
return false;
}
} else if (deco->Is<ast::BuiltinDecoration>()) {
auto attr =
builtin_to_attribute(deco->As<ast::BuiltinDecoration>()->value());
} else if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
auto attr = builtin_to_attribute(builtin->value());
if (attr.empty()) {
error_ = "unsupported builtin";
return false;

View File

@ -784,10 +784,10 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) {
push_annot(spv::Op::OpDecorate,
{Operand::Int(var_id), Operand::Int(SpvDecorationBuiltIn),
Operand::Int(ConvertBuiltin(builtin->value()))});
} else if (deco->IsLocation()) {
} else if (auto* location = deco->As<ast::LocationDecoration>()) {
push_annot(spv::Op::OpDecorate,
{Operand::Int(var_id), Operand::Int(SpvDecorationLocation),
Operand::Int(deco->AsLocation()->value())});
Operand::Int(location->value())});
} else if (auto* binding = deco->As<ast::BindingDecoration>()) {
push_annot(spv::Op::OpDecorate,
{Operand::Int(var_id), Operand::Int(SpvDecorationBinding),

View File

@ -645,8 +645,8 @@ bool GeneratorImpl::EmitVariableDecorations(ast::DecoratedVariable* var) {
out_ << "binding(" << binding->value() << ")";
} else if (deco->IsSet()) {
out_ << "set(" << deco->AsSet()->value() << ")";
} else if (deco->IsLocation()) {
out_ << "location(" << deco->AsLocation()->value() << ")";
} else if (auto* location = deco->As<ast::LocationDecoration>()) {
out_ << "location(" << location->value() << ")";
} else if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
out_ << "builtin(" << builtin->value() << ")";
} else if (auto* cid = deco->As<ast::ConstantIdDecoration>()) {