Replace use of std::unique_ptr<T> with T* for AST nodes

This is a minimal effort to fix up the code. There's substantial code
cleanup which can now be done, which is done in the next change.

Bug: tint:322
Change-Id: Iafcf5e814837d9534889e8c21333de4931a19cfa
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32864
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2020-11-16 16:31:07 +00:00
committed by Commit Bot service account
parent 0613890eed
commit b053acf796
209 changed files with 3469 additions and 3608 deletions

View File

@@ -57,7 +57,7 @@ bool BoundArrayAccessorsTransform::Run() {
// constant expression. There can't be any array accessors as per the current
// grammar.
for (auto& func : mod_->functions()) {
for (auto* func : mod_->functions()) {
scope_stack_.push_scope();
if (!ProcessStatement(func->body())) {
return false;
@@ -72,8 +72,8 @@ bool BoundArrayAccessorsTransform::ProcessStatement(ast::Statement* stmt) {
auto* as = stmt->AsAssign();
return ProcessExpression(as->lhs()) && ProcessExpression(as->rhs());
} else if (stmt->IsBlock()) {
for (auto& s : *(stmt->AsBlock())) {
if (!ProcessStatement(s.get())) {
for (auto* s : *(stmt->AsBlock())) {
if (!ProcessStatement(s)) {
return false;
}
}
@@ -97,8 +97,8 @@ bool BoundArrayAccessorsTransform::ProcessStatement(ast::Statement* stmt) {
if (!ProcessExpression(e->condition()) || !ProcessStatement(e->body())) {
return false;
}
for (auto& s : e->else_statements()) {
if (!ProcessStatement(s.get())) {
for (auto* s : e->else_statements()) {
if (!ProcessStatement(s)) {
return false;
}
}
@@ -118,8 +118,8 @@ bool BoundArrayAccessorsTransform::ProcessStatement(ast::Statement* stmt) {
return false;
}
for (auto& c : s->body()) {
if (!ProcessStatement(c.get())) {
for (auto* c : s->body()) {
if (!ProcessStatement(c)) {
return false;
}
}
@@ -146,8 +146,8 @@ bool BoundArrayAccessorsTransform::ProcessExpression(ast::Expression* expr) {
if (!ProcessExpression(c->func())) {
return false;
}
for (auto& e : c->params()) {
if (!ProcessExpression(e.get())) {
for (auto* e : c->params()) {
if (!ProcessExpression(e)) {
return false;
}
}
@@ -156,8 +156,8 @@ bool BoundArrayAccessorsTransform::ProcessExpression(ast::Expression* expr) {
} else if (expr->IsConstructor()) {
auto* c = expr->AsConstructor();
if (c->IsTypeConstructor()) {
for (auto& e : c->AsTypeConstructor()->values()) {
if (!ProcessExpression(e.get())) {
for (auto* e : c->AsTypeConstructor()->values()) {
if (!ProcessExpression(e)) {
return false;
}
}
@@ -241,7 +241,7 @@ bool BoundArrayAccessorsTransform::ProcessAccessExpression(
auto* u32 = ctx_->type_mgr().Get(std::make_unique<ast::type::U32Type>());
ast::ExpressionList cast_expr;
cast_expr.push_back(expr->take_idx_expr());
cast_expr.push_back(expr->idx_expr());
ast::ExpressionList params;
params.push_back(
@@ -249,7 +249,7 @@ bool BoundArrayAccessorsTransform::ProcessAccessExpression(
params.push_back(create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(u32, size - 1)));
auto call_expr = create<ast::CallExpression>(
auto* call_expr = create<ast::CallExpression>(
create<ast::IdentifierExpression>("min"), std::move(params));
call_expr->set_result_type(u32);

View File

@@ -52,15 +52,15 @@ class BoundArrayAccessorsTest : public testing::Test {
BoundArrayAccessorsTest() : td_(&ctx_, &mod_), transform_(&ctx_, &mod_) {}
ast::BlockStatement* SetupFunctionAndBody() {
auto block = create<ast::BlockStatement>();
body_ = block.get();
auto func = create<ast::Function>("func", ast::VariableList{}, &void_type_,
auto* block = create<ast::BlockStatement>();
body_ = block;
auto* func = create<ast::Function>("func", ast::VariableList{}, &void_type_,
std::move(block));
mod_.AddFunction(std::move(func));
return body_;
}
void DeclareVariable(std::unique_ptr<ast::Variable> var) {
void DeclareVariable(ast::Variable* var) {
ASSERT_NE(body_, nullptr);
body_->append(create<ast::VariableDeclStatement>(std::move(var)));
}
@@ -69,11 +69,13 @@ class BoundArrayAccessorsTest : public testing::Test {
BoundArrayAccessorsTransform* transform() { return &transform_; }
/// @return a `std::unique_ptr` to a new `T` constructed with `args`
/// @param args the arguments to forward to the constructor for `T`
/// Creates a new `ast::Node` owned by the Context. When the Context is
/// destructed, the `ast::Node` will also be destructed.
/// @param args the arguments to pass to the type constructor
/// @returns the node pointer
template <typename T, typename... ARGS>
std::unique_ptr<T> create(ARGS&&... args) {
return std::make_unique<T>(std::forward<ARGS>(args)...);
T* create(ARGS&&... args) {
return ctx_.create<T>(std::forward<ARGS>(args)...);
}
private:
@@ -101,18 +103,18 @@ TEST_F(BoundArrayAccessorsTest, Ptrs_Clamp) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &ary));
auto c_var = create<ast::Variable>("c", ast::StorageClass::kFunction, &u32);
auto* c_var = create<ast::Variable>("c", ast::StorageClass::kFunction, &u32);
c_var->set_is_const(true);
DeclareVariable(std::move(c_var));
auto access_idx = create<ast::IdentifierExpression>("c");
auto* access_ptr = access_idx.get();
auto* access_idx = create<ast::IdentifierExpression>("c");
auto* access_ptr = access_idx;
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"), std::move(access_idx));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &ptr_type);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &ptr_type);
b->set_constructor(std::move(accessor));
b->set_is_const(true);
DeclareVariable(std::move(b));
@@ -134,7 +136,7 @@ TEST_F(BoundArrayAccessorsTest, Ptrs_Clamp) {
auto* tc = idx->params()[0]->AsConstructor()->AsTypeConstructor();
EXPECT_TRUE(tc->type()->IsU32());
ASSERT_EQ(tc->values().size(), 1u);
ASSERT_EQ(tc->values()[0].get(), access_ptr);
ASSERT_EQ(tc->values()[0], access_ptr);
ASSERT_TRUE(idx->params()[1]->IsConstructor());
ASSERT_TRUE(idx->params()[1]->AsConstructor()->IsScalarConstructor());
@@ -167,17 +169,17 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Nested_Scalar) {
DeclareVariable(
create<ast::Variable>("i", ast::StorageClass::kFunction, &u32));
auto b_access_idx = create<ast::IdentifierExpression>("i");
auto* b_access_ptr = b_access_idx.get();
auto* b_access_idx = create<ast::IdentifierExpression>("i");
auto* b_access_ptr = b_access_idx;
auto a_access_idx = create<ast::ArrayAccessorExpression>(
auto* a_access_idx = create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("b"), std::move(b_access_idx));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"), std::move(a_access_idx));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("c", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("c", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -199,7 +201,7 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Nested_Scalar) {
EXPECT_TRUE(tc->type()->IsU32());
ASSERT_EQ(tc->values().size(), 1u);
auto* sub = tc->values()[0].get();
auto* sub = tc->values()[0];
ASSERT_TRUE(sub->IsArrayAccessor());
ASSERT_TRUE(sub->AsArrayAccessor()->idx_expr()->IsCall());
@@ -212,7 +214,7 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Nested_Scalar) {
tc = sub_idx->params()[0]->AsConstructor()->AsTypeConstructor();
EXPECT_TRUE(tc->type()->IsU32());
ASSERT_EQ(tc->values().size(), 1u);
ASSERT_EQ(tc->values()[0].get(), b_access_ptr);
ASSERT_EQ(tc->values()[0], b_access_ptr);
ASSERT_TRUE(sub_idx->params()[1]->IsConstructor());
ASSERT_TRUE(sub_idx->params()[1]->AsConstructor()->IsScalarConstructor());
@@ -244,13 +246,13 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Scalar) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &ary));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 1u)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -286,20 +288,20 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Expr) {
DeclareVariable(
create<ast::Variable>("c", ast::StorageClass::kFunction, &u32));
auto access_idx = create<ast::BinaryExpression>(
auto* access_idx = create<ast::BinaryExpression>(
ast::BinaryOp::kAdd, create<ast::IdentifierExpression>("c"),
create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 2)),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 3))));
auto* access_ptr = access_idx.get();
auto* access_ptr = access_idx;
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"), std::move(access_idx));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -320,7 +322,7 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Expr) {
auto* tc = idx->params()[0]->AsConstructor()->AsTypeConstructor();
EXPECT_TRUE(tc->type()->IsU32());
ASSERT_EQ(tc->values().size(), 1u);
ASSERT_EQ(tc->values()[0].get(), access_ptr);
ASSERT_EQ(tc->values()[0], access_ptr);
ASSERT_TRUE(idx->params()[1]->IsConstructor());
ASSERT_TRUE(idx->params()[1]->AsConstructor()->IsScalarConstructor());
@@ -346,13 +348,13 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Negative) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &ary));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, -1)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -385,13 +387,13 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_OutOfBounds) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &ary));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 3u)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -424,13 +426,13 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Scalar) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &vec));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 1u)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -466,20 +468,20 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Expr) {
DeclareVariable(
create<ast::Variable>("c", ast::StorageClass::kFunction, &u32));
auto access_idx = create<ast::BinaryExpression>(
auto* access_idx = create<ast::BinaryExpression>(
ast::BinaryOp::kAdd, create<ast::IdentifierExpression>("c"),
create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 2)),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 3))));
auto* access_ptr = access_idx.get();
auto* access_ptr = access_idx;
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"), std::move(access_idx));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -499,7 +501,7 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Expr) {
auto* tc = idx->params()[0]->AsConstructor()->AsTypeConstructor();
EXPECT_TRUE(tc->type()->IsU32());
ASSERT_EQ(tc->values().size(), 1u);
ASSERT_EQ(tc->values()[0].get(), access_ptr);
ASSERT_EQ(tc->values()[0], access_ptr);
ASSERT_TRUE(idx->params()[1]->IsConstructor());
ASSERT_TRUE(idx->params()[1]->AsConstructor()->IsScalarConstructor());
@@ -525,13 +527,13 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Negative) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &vec));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, -1)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -564,13 +566,13 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_OutOfBounds) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &vec));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 3u)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -603,16 +605,16 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Scalar) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &mat));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 2u))),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 1u)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -661,23 +663,23 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Column) {
DeclareVariable(
create<ast::Variable>("c", ast::StorageClass::kFunction, &u32));
auto access_idx = create<ast::BinaryExpression>(
auto* access_idx = create<ast::BinaryExpression>(
ast::BinaryOp::kAdd, create<ast::IdentifierExpression>("c"),
create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 2)),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 3))));
auto* access_ptr = access_idx.get();
auto* access_ptr = access_idx;
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"), std::move(access_idx)),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 1u)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -701,7 +703,7 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Column) {
auto* tc = idx->params()[0]->AsConstructor()->AsTypeConstructor();
EXPECT_TRUE(tc->type()->IsU32());
ASSERT_EQ(tc->values().size(), 1u);
ASSERT_EQ(tc->values()[0].get(), access_ptr);
ASSERT_EQ(tc->values()[0], access_ptr);
ASSERT_TRUE(idx->params()[1]->IsConstructor());
ASSERT_TRUE(idx->params()[1]->AsConstructor()->IsScalarConstructor());
@@ -740,24 +742,24 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Row) {
DeclareVariable(
create<ast::Variable>("c", ast::StorageClass::kFunction, &u32));
auto access_idx = create<ast::BinaryExpression>(
auto* access_idx = create<ast::BinaryExpression>(
ast::BinaryOp::kAdd, create<ast::IdentifierExpression>("c"),
create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 2)),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 3))));
auto* access_ptr = access_idx.get();
auto* access_ptr = access_idx;
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 1u))),
std::move(access_idx));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -788,7 +790,7 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Row) {
auto* tc = idx->params()[0]->AsConstructor()->AsTypeConstructor();
EXPECT_TRUE(tc->type()->IsU32());
ASSERT_EQ(tc->values().size(), 1u);
ASSERT_EQ(tc->values()[0].get(), access_ptr);
ASSERT_EQ(tc->values()[0], access_ptr);
ASSERT_TRUE(idx->params()[1]->IsConstructor());
ASSERT_TRUE(idx->params()[1]->AsConstructor()->IsScalarConstructor());
@@ -816,16 +818,16 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Negative_Column) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &mat));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, -1))),
create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, 1)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -870,16 +872,16 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Negative_Row) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &mat));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, 2))),
create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, -1)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -925,16 +927,16 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_OutOfBounds_Column) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &mat));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 5u))),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 1u)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));
@@ -980,16 +982,16 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_OutOfBounds_Row) {
DeclareVariable(
create<ast::Variable>("a", ast::StorageClass::kFunction, &mat));
auto accessor = create<ast::ArrayAccessorExpression>(
auto* accessor = create<ast::ArrayAccessorExpression>(
create<ast::ArrayAccessorExpression>(
create<ast::IdentifierExpression>("a"),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 2u))),
create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(&u32, 5u)));
auto* ptr = accessor.get();
auto* ptr = accessor;
auto b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
auto* b = create<ast::Variable>("b", ast::StorageClass::kFunction, &f32);
b->set_constructor(std::move(accessor));
DeclareVariable(std::move(b));

View File

@@ -41,11 +41,13 @@ class Transformer {
const std::string& error() { return error_; }
protected:
/// @return a `std::unique_ptr` to a new `T` constructed with `args`
/// @param args the arguments to forward to the constructor for `T`
/// Creates a new `ast::Node` owned by the Context. When the Context is
/// destructed, the `ast::Node` will also be destructed.
/// @param args the arguments to pass to the type constructor
/// @returns the node pointer
template <typename T, typename... ARGS>
std::unique_ptr<T> create(ARGS&&... args) const {
return std::make_unique<T>(std::forward<ARGS>(args)...);
T* create(ARGS&&... args) {
return ctx_->create<T>(std::forward<ARGS>(args)...);
}
/// The context

View File

@@ -126,12 +126,12 @@ void VertexPullingTransform::FindOrInsertVertexIndexIfUsed() {
}
// Look for an existing vertex index builtin
for (auto& v : mod_->global_variables()) {
for (auto* v : mod_->global_variables()) {
if (!v->IsDecorated() || v->storage_class() != ast::StorageClass::kInput) {
continue;
}
for (auto& d : v->AsDecorated()->decorations()) {
for (auto* d : v->AsDecorated()->decorations()) {
if (d->IsBuiltin() &&
d->AsBuiltin()->value() == ast::Builtin::kVertexIdx) {
vertex_index_name_ = v->name();
@@ -143,7 +143,7 @@ void VertexPullingTransform::FindOrInsertVertexIndexIfUsed() {
// We didn't find a vertex index builtin, so create one
vertex_index_name_ = kDefaultVertexIndexName;
auto var = create<ast::DecoratedVariable>(create<ast::Variable>(
auto* var = create<ast::DecoratedVariable>(create<ast::Variable>(
vertex_index_name_, ast::StorageClass::kInput, GetI32Type()));
ast::VariableDecorationList decorations;
@@ -168,12 +168,12 @@ void VertexPullingTransform::FindOrInsertInstanceIndexIfUsed() {
}
// Look for an existing instance index builtin
for (auto& v : mod_->global_variables()) {
for (auto* v : mod_->global_variables()) {
if (!v->IsDecorated() || v->storage_class() != ast::StorageClass::kInput) {
continue;
}
for (auto& d : v->AsDecorated()->decorations()) {
for (auto* d : v->AsDecorated()->decorations()) {
if (d->IsBuiltin() &&
d->AsBuiltin()->value() == ast::Builtin::kInstanceIdx) {
instance_index_name_ = v->name();
@@ -185,7 +185,7 @@ void VertexPullingTransform::FindOrInsertInstanceIndexIfUsed() {
// We didn't find an instance index builtin, so create one
instance_index_name_ = kDefaultInstanceIndexName;
auto var = create<ast::DecoratedVariable>(create<ast::Variable>(
auto* var = create<ast::DecoratedVariable>(create<ast::Variable>(
instance_index_name_, ast::StorageClass::kInput, GetI32Type()));
ast::VariableDecorationList decorations;
@@ -197,12 +197,12 @@ void VertexPullingTransform::FindOrInsertInstanceIndexIfUsed() {
}
void VertexPullingTransform::ConvertVertexInputVariablesToPrivate() {
for (auto& v : mod_->global_variables()) {
for (auto*& v : mod_->global_variables()) {
if (!v->IsDecorated() || v->storage_class() != ast::StorageClass::kInput) {
continue;
}
for (auto& d : v->AsDecorated()->decorations()) {
for (auto* d : v->AsDecorated()->decorations()) {
if (!d->IsLocation()) {
continue;
}
@@ -213,7 +213,7 @@ void VertexPullingTransform::ConvertVertexInputVariablesToPrivate() {
// in the AST.
v = create<ast::Variable>(v->name(), ast::StorageClass::kPrivate,
v->type());
location_to_var_[location] = v.get();
location_to_var_[location] = v;
break;
}
}
@@ -247,7 +247,7 @@ void VertexPullingTransform::AddVertexStorageBuffers() {
for (uint32_t i = 0; i < vertex_state_->vertex_buffers.size(); ++i) {
// The decorated variable with struct type
auto var = create<ast::DecoratedVariable>(
auto* var = create<ast::DecoratedVariable>(
create<ast::Variable>(GetVertexBufferName(i),
ast::StorageClass::kStorageBuffer, struct_type));
@@ -268,10 +268,10 @@ void VertexPullingTransform::AddVertexPullingPreamble(
// location.
// A block statement allowing us to use append instead of insert
auto block = create<ast::BlockStatement>();
auto* block = create<ast::BlockStatement>();
// Declare the |kPullingPosVarName| variable in the shader
auto pos_declaration =
auto* pos_declaration =
create<ast::VariableDeclStatement>(create<ast::Variable>(
kPullingPosVarName, ast::StorageClass::kFunction, GetI32Type()));
@@ -293,13 +293,13 @@ void VertexPullingTransform::AddVertexPullingPreamble(
auto* v = it->second;
// Identifier to index by
auto index_identifier = create<ast::IdentifierExpression>(
auto* index_identifier = create<ast::IdentifierExpression>(
buffer_layout.step_mode == InputStepMode::kVertex
? vertex_index_name_
: instance_index_name_);
// An expression for the start of the read in the buffer in bytes
auto pos_value = create<ast::BinaryExpression>(
auto* pos_value = create<ast::BinaryExpression>(
ast::BinaryOp::kAdd,
create<ast::BinaryExpression>(
ast::BinaryOp::kMultiply, std::move(index_identifier),
@@ -307,7 +307,7 @@ void VertexPullingTransform::AddVertexPullingPreamble(
GenUint(static_cast<uint32_t>(attribute_desc.offset)));
// Update position of the read
auto set_pos_expr = create<ast::AssignmentStatement>(
auto* set_pos_expr = create<ast::AssignmentStatement>(
CreatePullingPositionIdent(), std::move(pos_value));
block->append(std::move(set_pos_expr));
@@ -320,20 +320,17 @@ void VertexPullingTransform::AddVertexPullingPreamble(
vertex_func->body()->insert(0, std::move(block));
}
std::unique_ptr<ast::Expression> VertexPullingTransform::GenUint(
uint32_t value) {
ast::Expression* VertexPullingTransform::GenUint(uint32_t value) {
return create<ast::ScalarConstructorExpression>(
create<ast::UintLiteral>(GetU32Type(), value));
}
std::unique_ptr<ast::Expression>
VertexPullingTransform::CreatePullingPositionIdent() {
ast::Expression* VertexPullingTransform::CreatePullingPositionIdent() {
return create<ast::IdentifierExpression>(kPullingPosVarName);
}
std::unique_ptr<ast::Expression> VertexPullingTransform::AccessByFormat(
uint32_t buffer,
VertexFormat format) {
ast::Expression* VertexPullingTransform::AccessByFormat(uint32_t buffer,
VertexFormat format) {
// TODO(idanr): this doesn't account for the format of the attribute in the
// shader. ex: vec<u32> in shader, and attribute claims VertexFormat::Float4
// right now, we would try to assign a vec4<f32> to this attribute, but we
@@ -359,9 +356,8 @@ std::unique_ptr<ast::Expression> VertexPullingTransform::AccessByFormat(
}
}
std::unique_ptr<ast::Expression> VertexPullingTransform::AccessU32(
uint32_t buffer,
std::unique_ptr<ast::Expression> pos) {
ast::Expression* VertexPullingTransform::AccessU32(uint32_t buffer,
ast::Expression* pos) {
// Here we divide by 4, since the buffer is uint32 not uint8. The input buffer
// has byte offsets for each attribute, and we will convert it to u32 indexes
// by dividing. Then, that element is going to be read, and if needed,
@@ -375,26 +371,23 @@ std::unique_ptr<ast::Expression> VertexPullingTransform::AccessU32(
GenUint(4)));
}
std::unique_ptr<ast::Expression> VertexPullingTransform::AccessI32(
uint32_t buffer,
std::unique_ptr<ast::Expression> pos) {
ast::Expression* VertexPullingTransform::AccessI32(uint32_t buffer,
ast::Expression* pos) {
// as<T> reinterprets bits
return create<ast::BitcastExpression>(GetI32Type(),
AccessU32(buffer, std::move(pos)));
}
std::unique_ptr<ast::Expression> VertexPullingTransform::AccessF32(
uint32_t buffer,
std::unique_ptr<ast::Expression> pos) {
ast::Expression* VertexPullingTransform::AccessF32(uint32_t buffer,
ast::Expression* pos) {
// as<T> reinterprets bits
return create<ast::BitcastExpression>(GetF32Type(),
AccessU32(buffer, std::move(pos)));
}
std::unique_ptr<ast::Expression> VertexPullingTransform::AccessPrimitive(
uint32_t buffer,
std::unique_ptr<ast::Expression> pos,
VertexFormat format) {
ast::Expression* VertexPullingTransform::AccessPrimitive(uint32_t buffer,
ast::Expression* pos,
VertexFormat format) {
// This function uses a position expression to read, rather than using the
// position variable. This allows us to read from offset positions relative to
// |kPullingPosVarName|. We can't call AccessByFormat because it reads only
@@ -411,18 +404,17 @@ std::unique_ptr<ast::Expression> VertexPullingTransform::AccessPrimitive(
}
}
std::unique_ptr<ast::Expression> VertexPullingTransform::AccessVec(
uint32_t buffer,
uint32_t element_stride,
ast::type::Type* base_type,
VertexFormat base_format,
uint32_t count) {
ast::Expression* VertexPullingTransform::AccessVec(uint32_t buffer,
uint32_t element_stride,
ast::type::Type* base_type,
VertexFormat base_format,
uint32_t count) {
ast::ExpressionList expr_list;
for (uint32_t i = 0; i < count; ++i) {
// Offset read position by element_stride for each component
auto cur_pos = create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
CreatePullingPositionIdent(),
GenUint(element_stride * i));
auto* cur_pos = create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
CreatePullingPositionIdent(),
GenUint(element_stride * i));
expr_list.push_back(
AccessPrimitive(buffer, std::move(cur_pos), base_format));
}

View File

@@ -189,49 +189,41 @@ class VertexPullingTransform : public Transformer {
/// Generates an expression holding a constant uint
/// @param value uint value
std::unique_ptr<ast::Expression> GenUint(uint32_t value);
ast::Expression* GenUint(uint32_t value);
/// Generates an expression to read the shader value |kPullingPosVarName|
std::unique_ptr<ast::Expression> CreatePullingPositionIdent();
ast::Expression* CreatePullingPositionIdent();
/// Generates an expression reading from a buffer a specific format.
/// This reads the value wherever |kPullingPosVarName| points to at the time
/// of the read.
/// @param buffer the index of the vertex buffer
/// @param format the format to read
std::unique_ptr<ast::Expression> AccessByFormat(uint32_t buffer,
VertexFormat format);
ast::Expression* AccessByFormat(uint32_t buffer, VertexFormat format);
/// Generates an expression reading a uint32 from a vertex buffer
/// @param buffer the index of the vertex buffer
/// @param pos an expression for the position of the access, in bytes
std::unique_ptr<ast::Expression> AccessU32(
uint32_t buffer,
std::unique_ptr<ast::Expression> pos);
ast::Expression* AccessU32(uint32_t buffer, ast::Expression* pos);
/// Generates an expression reading an int32 from a vertex buffer
/// @param buffer the index of the vertex buffer
/// @param pos an expression for the position of the access, in bytes
std::unique_ptr<ast::Expression> AccessI32(
uint32_t buffer,
std::unique_ptr<ast::Expression> pos);
ast::Expression* AccessI32(uint32_t buffer, ast::Expression* pos);
/// Generates an expression reading a float from a vertex buffer
/// @param buffer the index of the vertex buffer
/// @param pos an expression for the position of the access, in bytes
std::unique_ptr<ast::Expression> AccessF32(
uint32_t buffer,
std::unique_ptr<ast::Expression> pos);
ast::Expression* AccessF32(uint32_t buffer, ast::Expression* pos);
/// Generates an expression reading a basic type (u32, i32, f32) from a vertex
/// buffer
/// @param buffer the index of the vertex buffer
/// @param pos an expression for the position of the access, in bytes
/// @param format the underlying vertex format
std::unique_ptr<ast::Expression> AccessPrimitive(
uint32_t buffer,
std::unique_ptr<ast::Expression> pos,
VertexFormat format);
ast::Expression* AccessPrimitive(uint32_t buffer,
ast::Expression* pos,
VertexFormat format);
/// Generates an expression reading a vec2/3/4 from a vertex buffer.
/// This reads the value wherever |kPullingPosVarName| points to at the time
@@ -241,11 +233,11 @@ class VertexPullingTransform : public Transformer {
/// @param base_type underlying AST type
/// @param base_format underlying vertex format
/// @param count how many elements the vector has
std::unique_ptr<ast::Expression> AccessVec(uint32_t buffer,
uint32_t element_stride,
ast::type::Type* base_type,
VertexFormat base_format,
uint32_t count);
ast::Expression* AccessVec(uint32_t buffer,
uint32_t element_stride,
ast::type::Type* base_type,
VertexFormat base_format,
uint32_t count);
// Used to grab corresponding types from the type manager
ast::type::Type* GetU32Type();

View File

@@ -35,18 +35,18 @@ namespace {
class VertexPullingTransformHelper {
public:
VertexPullingTransformHelper() {
mod_ = create<ast::Module>();
mod_ = std::make_unique<ast::Module>();
transform_ = std::make_unique<VertexPullingTransform>(&ctx_, mod_.get());
}
// Create basic module with an entry point and vertex function
void InitBasicModule() {
auto func = create<ast::Function>(
auto* func = create<ast::Function>(
"main", ast::VariableList{},
ctx_.type_mgr().Get(std::make_unique<ast::type::VoidType>()),
create<ast::BlockStatement>());
func->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage ::kVertex, Source{}));
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
mod()->AddFunction(std::move(func));
}
@@ -66,7 +66,7 @@ class VertexPullingTransformHelper {
void AddVertexInputVariable(uint32_t location,
std::string name,
ast::type::Type* type) {
auto var = create<ast::DecoratedVariable>(
auto* var = create<ast::DecoratedVariable>(
create<ast::Variable>(name, ast::StorageClass::kInput, type));
ast::VariableDecorationList decorations;
@@ -80,11 +80,13 @@ class VertexPullingTransformHelper {
ast::Module* mod() { return mod_.get(); }
VertexPullingTransform* transform() { return transform_.get(); }
/// @return a `std::unique_ptr` to a new `T` constructed with `args`
/// @param args the arguments to forward to the constructor for `T`
/// Creates a new `ast::Node` owned by the Context. When the Context is
/// destructed, the `ast::Node` will also be destructed.
/// @param args the arguments to pass to the type constructor
/// @returns the node pointer
template <typename T, typename... ARGS>
std::unique_ptr<T> create(ARGS&&... args) {
return std::make_unique<T>(std::forward<ARGS>(args)...);
T* create(ARGS&&... args) {
return ctx_.create<T>(std::forward<ARGS>(args)...);
}
private:
@@ -117,7 +119,7 @@ TEST_F(VertexPullingTransformTest, Error_InvalidEntryPoint) {
}
TEST_F(VertexPullingTransformTest, Error_EntryPointWrongStage) {
auto func = create<ast::Function>(
auto* func = create<ast::Function>(
"main", ast::VariableList{},
ctx()->type_mgr().Get(std::make_unique<ast::type::VoidType>()),
create<ast::BlockStatement>());
@@ -400,7 +402,7 @@ TEST_F(VertexPullingTransformTest, ExistingVertexIndexAndInstanceIndex) {
ast::type::I32Type i32;
{
auto vertex_index_var =
auto* vertex_index_var =
create<ast::DecoratedVariable>(create<ast::Variable>(
"custom_vertex_index", ast::StorageClass::kInput, &i32));
@@ -413,7 +415,7 @@ TEST_F(VertexPullingTransformTest, ExistingVertexIndexAndInstanceIndex) {
}
{
auto instance_index_var =
auto* instance_index_var =
create<ast::DecoratedVariable>(create<ast::Variable>(
"custom_instance_index", ast::StorageClass::kInput, &i32));