sem: Rename methods of sem::Pointer and Reference

Rename:
* type() to StoreType()
* storage_class() to StorageClass()

Move away from snake_case methods in the semantic namespace.
Try to avoid generic 'type()' method names.

Also add an assertion to detect doubly nested references (these are
invalid).

Bug: tint:727
Change-Id: I975a3f1e5fbed7947cc2fc156fee892b282c63de
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51220
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2021-05-17 20:38:17 +00:00 committed by Commit Bot service account
parent 7b25769aed
commit c5eebaf7d6
12 changed files with 40 additions and 34 deletions

View File

@ -370,7 +370,7 @@ class PtrBuilder : public Builder {
bool Match(MatchState& state, const sem::Type* ty) const override {
if (auto* ptr = ty->As<sem::Pointer>()) {
return element_builder_->Match(state, ptr->type());
return element_builder_->Match(state, ptr->StoreType());
}
return false;
}
@ -1396,7 +1396,7 @@ sem::Intrinsic* Impl::Overload::Match(ProgramBuilder& builder,
if (!parameters[i].matcher->ExpectsPointer()) {
// Argument is a pointer, but the matcher isn't expecting one.
// Perform an implicit dereference.
arg_ty = ptr->type();
arg_ty = ptr->StoreType();
}
}
if (parameters[i].matcher->Match(matcher_state, arg_ty)) {

View File

@ -1542,7 +1542,7 @@ bool Resolver::ArrayAccessor(ast::ArrayAccessorExpression* expr) {
// If we're extracting from a pointer, we return a pointer.
if (auto* ptr = res->As<sem::Pointer>()) {
ret = builder_->create<sem::Pointer>(ret, ptr->storage_class());
ret = builder_->create<sem::Pointer>(ret, ptr->StorageClass());
} else if (auto* arr = parent_type->As<sem::Array>()) {
if (!arr->ElemType()->is_scalar()) {
// If we extract a non-scalar from an array then we also get a pointer. We
@ -1906,7 +1906,7 @@ bool Resolver::MemberAccessor(ast::MemberAccessorExpression* expr) {
// If we're extracting from a pointer, we return a pointer.
if (auto* ptr = res->As<sem::Pointer>()) {
ret = builder_->create<sem::Pointer>(ret, ptr->storage_class());
ret = builder_->create<sem::Pointer>(ret, ptr->StorageClass());
}
builder_->Sem().Add(expr, builder_->create<sem::StructMemberAccess>(
@ -1969,7 +1969,7 @@ bool Resolver::MemberAccessor(ast::MemberAccessorExpression* expr) {
ret = vec->type();
// If we're extracting from a pointer, we return a pointer.
if (auto* ptr = res->As<sem::Pointer>()) {
ret = builder_->create<sem::Pointer>(ret, ptr->storage_class());
ret = builder_->create<sem::Pointer>(ret, ptr->StorageClass());
}
} else {
// The vector will have a number of components equal to the length of

View File

@ -400,7 +400,7 @@ TEST_F(ResolverTest, Expr_ArrayAccessor_Array) {
ASSERT_TRUE(TypeOf(acc)->Is<sem::Pointer>());
auto* ptr = TypeOf(acc)->As<sem::Pointer>();
EXPECT_TRUE(ptr->type()->Is<sem::F32>());
EXPECT_TRUE(ptr->StoreType()->Is<sem::F32>());
}
TEST_F(ResolverTest, Expr_ArrayAccessor_Alias_Array) {
@ -418,7 +418,7 @@ TEST_F(ResolverTest, Expr_ArrayAccessor_Alias_Array) {
ASSERT_TRUE(TypeOf(acc)->Is<sem::Pointer>());
auto* ptr = TypeOf(acc)->As<sem::Pointer>();
EXPECT_TRUE(ptr->type()->Is<sem::F32>());
EXPECT_TRUE(ptr->StoreType()->Is<sem::F32>());
}
TEST_F(ResolverTest, Expr_ArrayAccessor_Array_Constant) {
@ -445,8 +445,8 @@ TEST_F(ResolverTest, Expr_ArrayAccessor_Matrix) {
ASSERT_TRUE(TypeOf(acc)->Is<sem::Pointer>());
auto* ptr = TypeOf(acc)->As<sem::Pointer>();
ASSERT_TRUE(ptr->type()->Is<sem::Vector>());
EXPECT_EQ(ptr->type()->As<sem::Vector>()->size(), 3u);
ASSERT_TRUE(ptr->StoreType()->Is<sem::Vector>());
EXPECT_EQ(ptr->StoreType()->As<sem::Vector>()->size(), 3u);
}
TEST_F(ResolverTest, Expr_ArrayAccessor_Matrix_BothDimensions) {
@ -461,7 +461,7 @@ TEST_F(ResolverTest, Expr_ArrayAccessor_Matrix_BothDimensions) {
ASSERT_TRUE(TypeOf(acc)->Is<sem::Pointer>());
auto* ptr = TypeOf(acc)->As<sem::Pointer>();
EXPECT_TRUE(ptr->type()->Is<sem::F32>());
EXPECT_TRUE(ptr->StoreType()->Is<sem::F32>());
}
TEST_F(ResolverTest, Expr_ArrayAccessor_Vector) {
@ -476,7 +476,7 @@ TEST_F(ResolverTest, Expr_ArrayAccessor_Vector) {
ASSERT_TRUE(TypeOf(acc)->Is<sem::Pointer>());
auto* ptr = TypeOf(acc)->As<sem::Pointer>();
EXPECT_TRUE(ptr->type()->Is<sem::F32>());
EXPECT_TRUE(ptr->StoreType()->Is<sem::F32>());
}
TEST_F(ResolverTest, Expr_Bitcast) {
@ -610,7 +610,7 @@ TEST_F(ResolverTest, Expr_Identifier_GlobalVariable) {
ASSERT_NE(TypeOf(ident), nullptr);
EXPECT_TRUE(TypeOf(ident)->Is<sem::Pointer>());
EXPECT_TRUE(TypeOf(ident)->As<sem::Pointer>()->type()->Is<sem::F32>());
EXPECT_TRUE(TypeOf(ident)->As<sem::Pointer>()->StoreType()->Is<sem::F32>());
EXPECT_TRUE(CheckVarUsers(my_var, {ident}));
ASSERT_NE(VarOf(ident), nullptr);
EXPECT_EQ(VarOf(ident)->Declaration(), my_var);
@ -675,11 +675,13 @@ TEST_F(ResolverTest, Expr_Identifier_FunctionVariable) {
ASSERT_NE(TypeOf(my_var_a), nullptr);
EXPECT_TRUE(TypeOf(my_var_a)->Is<sem::Pointer>());
EXPECT_TRUE(TypeOf(my_var_a)->As<sem::Pointer>()->type()->Is<sem::F32>());
EXPECT_TRUE(
TypeOf(my_var_a)->As<sem::Pointer>()->StoreType()->Is<sem::F32>());
EXPECT_EQ(StmtOf(my_var_a), assign);
ASSERT_NE(TypeOf(my_var_b), nullptr);
EXPECT_TRUE(TypeOf(my_var_b)->Is<sem::Pointer>());
EXPECT_TRUE(TypeOf(my_var_b)->As<sem::Pointer>()->type()->Is<sem::F32>());
EXPECT_TRUE(
TypeOf(my_var_b)->As<sem::Pointer>()->StoreType()->Is<sem::F32>());
EXPECT_EQ(StmtOf(my_var_b), assign);
EXPECT_TRUE(CheckVarUsers(var, {my_var_a, my_var_b}));
ASSERT_NE(VarOf(my_var_a), nullptr);
@ -704,11 +706,13 @@ TEST_F(ResolverTest, Expr_Identifier_Function_Ptr) {
ASSERT_NE(TypeOf(my_var_a), nullptr);
EXPECT_TRUE(TypeOf(my_var_a)->Is<sem::Pointer>());
EXPECT_TRUE(TypeOf(my_var_a)->As<sem::Pointer>()->type()->Is<sem::F32>());
EXPECT_TRUE(
TypeOf(my_var_a)->As<sem::Pointer>()->StoreType()->Is<sem::F32>());
EXPECT_EQ(StmtOf(my_var_a), assign);
ASSERT_NE(TypeOf(my_var_b), nullptr);
EXPECT_TRUE(TypeOf(my_var_b)->Is<sem::Pointer>());
EXPECT_TRUE(TypeOf(my_var_b)->As<sem::Pointer>()->type()->Is<sem::F32>());
EXPECT_TRUE(
TypeOf(my_var_b)->As<sem::Pointer>()->StoreType()->Is<sem::F32>());
EXPECT_EQ(StmtOf(my_var_b), assign);
}
@ -894,7 +898,7 @@ TEST_F(ResolverTest, Expr_MemberAccessor_Struct) {
ASSERT_TRUE(TypeOf(mem)->Is<sem::Pointer>());
auto* ptr = TypeOf(mem)->As<sem::Pointer>();
EXPECT_TRUE(ptr->type()->Is<sem::F32>());
EXPECT_TRUE(ptr->StoreType()->Is<sem::F32>());
auto* sma = Sem().Get(mem)->As<sem::StructMemberAccess>();
ASSERT_NE(sma, nullptr);
EXPECT_EQ(sma->Member()->Type(), ty.f32());
@ -919,7 +923,7 @@ TEST_F(ResolverTest, Expr_MemberAccessor_Struct_Alias) {
ASSERT_TRUE(TypeOf(mem)->Is<sem::Pointer>());
auto* ptr = TypeOf(mem)->As<sem::Pointer>();
EXPECT_TRUE(ptr->type()->Is<sem::F32>());
EXPECT_TRUE(ptr->StoreType()->Is<sem::F32>());
auto* sma = Sem().Get(mem)->As<sem::StructMemberAccess>();
ASSERT_NE(sma, nullptr);
EXPECT_EQ(sma->Member()->Type(), ty.f32());
@ -955,7 +959,7 @@ TEST_F(ResolverTest, Expr_MemberAccessor_VectorSwizzle_SingleElement) {
ASSERT_TRUE(TypeOf(mem)->Is<sem::Pointer>());
auto* ptr = TypeOf(mem)->As<sem::Pointer>();
ASSERT_TRUE(ptr->type()->Is<sem::F32>());
ASSERT_TRUE(ptr->StoreType()->Is<sem::F32>());
ASSERT_TRUE(Sem().Get(mem)->Is<sem::Swizzle>());
EXPECT_THAT(Sem().Get(mem)->As<sem::Swizzle>()->Indices(), ElementsAre(2));
}

View File

@ -35,9 +35,9 @@ class Pointer : public Castable<Pointer, Type> {
~Pointer() override;
/// @returns the pointee type
const Type* type() const { return subtype_; }
const Type* StoreType() const { return subtype_; }
/// @returns the storage class of the pointer
ast::StorageClass storage_class() const { return storage_class_; }
ast::StorageClass StorageClass() const { return storage_class_; }
/// @returns the name for this type
std::string type_name() const override;

View File

@ -23,8 +23,8 @@ using PointerTest = TestHelper;
TEST_F(PointerTest, Creation) {
auto* r = create<Pointer>(create<I32>(), ast::StorageClass::kStorage);
EXPECT_TRUE(r->type()->Is<sem::I32>());
EXPECT_EQ(r->storage_class(), ast::StorageClass::kStorage);
EXPECT_TRUE(r->StoreType()->Is<sem::I32>());
EXPECT_EQ(r->StorageClass(), ast::StorageClass::kStorage);
}
TEST_F(PointerTest, TypeName) {

View File

@ -22,7 +22,9 @@ namespace tint {
namespace sem {
Reference::Reference(const Type* subtype, ast::StorageClass storage_class)
: subtype_(subtype), storage_class_(storage_class) {}
: subtype_(subtype), storage_class_(storage_class) {
TINT_ASSERT(!subtype->Is<Reference>());
}
std::string Reference::type_name() const {
std::ostringstream out;

View File

@ -35,9 +35,9 @@ class Reference : public Castable<Reference, Type> {
~Reference() override;
/// @returns the pointee type
const Type* type() const { return subtype_; }
const Type* StoreType() const { return subtype_; }
/// @returns the storage class of the reference
ast::StorageClass storage_class() const { return storage_class_; }
ast::StorageClass StorageClass() const { return storage_class_; }
/// @returns the name for this type
std::string type_name() const override;

View File

@ -23,8 +23,8 @@ using ReferenceTest = TestHelper;
TEST_F(ReferenceTest, Creation) {
auto* r = create<Reference>(create<I32>(), ast::StorageClass::kStorage);
EXPECT_TRUE(r->type()->Is<sem::I32>());
EXPECT_EQ(r->storage_class(), ast::StorageClass::kStorage);
EXPECT_TRUE(r->StoreType()->Is<sem::I32>());
EXPECT_EQ(r->StorageClass(), ast::StorageClass::kStorage);
}
TEST_F(ReferenceTest, TypeName) {

View File

@ -38,7 +38,7 @@ Type::~Type() = default;
const Type* Type::UnwrapPtr() const {
auto* type = this;
while (auto* ptr = type->As<sem::Pointer>()) {
type = ptr->type();
type = ptr->StoreType();
}
return type;
}
@ -53,7 +53,7 @@ const Type* Type::UnwrapAll() const {
auto* type = this;
while (true) {
if (auto* ptr = type->As<sem::Pointer>()) {
type = ptr->type();
type = ptr->StoreType();
} else {
break;
}

View File

@ -334,7 +334,7 @@ void InsertGlobal(CloneContext& ctx,
const ast::NamedType* ConstructedTypeOf(const sem::Type* ty) {
while (true) {
if (auto* ptr = ty->As<sem::Pointer>()) {
ty = ptr->type();
ty = ptr->StoreType();
continue;
}
if (auto* str = ty->As<sem::Struct>()) {

View File

@ -1912,7 +1912,7 @@ bool GeneratorImpl::EmitType(const sem::Type* type, const std::string& name) {
out_ << mat->columns() << "x" << mat->rows();
} else if (auto* ptr = type->As<sem::Pointer>()) {
// TODO(dsinclair): Storage class?
if (!EmitType(ptr->type(), "")) {
if (!EmitType(ptr->StoreType(), "")) {
return false;
}
out_ << "*";

View File

@ -3131,12 +3131,12 @@ bool Builder::GenerateMatrixType(const sem::Matrix* mat,
bool Builder::GeneratePointerType(const sem::Pointer* ptr,
const Operand& result) {
auto pointee_id = GenerateTypeIfNeeded(ptr->type());
auto pointee_id = GenerateTypeIfNeeded(ptr->StoreType());
if (pointee_id == 0) {
return false;
}
auto stg_class = ConvertStorageClass(ptr->storage_class());
auto stg_class = ConvertStorageClass(ptr->StorageClass());
if (stg_class == SpvStorageClassMax) {
error_ = "invalid storage class for pointer";
return false;