mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-08 13:14:56 +00:00
sem: Fold together sem::Array and sem::ArrayType
There's now no need to have both. Removes a whole bunch of Sem().Get() smell, and simplifies the resolver. Also fixes a long-standing issue where an array with an explicit, but equal-to-implicit-stride attribute would result in a different type to an array without the decoration. Bug: tint:724 Fixed: tint:782 Change-Id: I0202459009cd45be427cdb621993a5a3b07ff51e Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50301 Reviewed-by: Antonio Maiorano <amaiorano@google.com> Kokoro: Kokoro <noreply+kokoro@google.com> Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
committed by
Commit Bot service account
parent
6732e8561c
commit
4cd5eea87e
@@ -37,7 +37,7 @@ TEST_F(AccessControlTest, Is) {
|
||||
Type* ty = &at;
|
||||
EXPECT_TRUE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -33,7 +33,7 @@ TEST_F(AliasTest, Is) {
|
||||
sem::Type* ty = at;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_TRUE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -14,16 +14,53 @@
|
||||
|
||||
#include "src/sem/array.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/debug.h"
|
||||
|
||||
TINT_INSTANTIATE_TYPEINFO(tint::sem::Array);
|
||||
|
||||
namespace tint {
|
||||
namespace sem {
|
||||
|
||||
Array::Array(sem::ArrayType* type,
|
||||
Array::Array(const Type* element,
|
||||
uint32_t count,
|
||||
uint32_t align,
|
||||
uint32_t size,
|
||||
uint32_t stride)
|
||||
: type_(type), align_(align), size_(size), stride_(stride) {}
|
||||
uint32_t stride,
|
||||
bool stride_implicit)
|
||||
: element_(element),
|
||||
count_(count),
|
||||
align_(align),
|
||||
size_(size),
|
||||
stride_(stride),
|
||||
stride_implicit_(stride_implicit) {
|
||||
TINT_ASSERT(element_);
|
||||
}
|
||||
|
||||
std::string Array::type_name() const {
|
||||
std::string type_name = "__array" + element_->type_name();
|
||||
type_name += "_count_" + std::to_string(count_);
|
||||
type_name += "_align_" + std::to_string(align_);
|
||||
type_name += "_size_" + std::to_string(size_);
|
||||
type_name += "_stride_" + std::to_string(stride_);
|
||||
// Note: stride_implicit is not part of the type_name string as this is a
|
||||
// property derived from the other fields.
|
||||
return type_name;
|
||||
}
|
||||
|
||||
std::string Array::FriendlyName(const SymbolTable& symbols) const {
|
||||
std::ostringstream out;
|
||||
if (!stride_implicit_) {
|
||||
out << "[[stride(" << stride_ << ")]] ";
|
||||
}
|
||||
out << "array<" << element_->FriendlyName(symbols);
|
||||
if (!IsRuntimeSized()) {
|
||||
out << ", " << count_;
|
||||
}
|
||||
out << ">";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
} // namespace sem
|
||||
} // namespace tint
|
||||
|
||||
@@ -16,28 +16,47 @@
|
||||
#define SRC_SEM_ARRAY_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
#include "src/sem/node.h"
|
||||
#include "src/sem/type.h"
|
||||
|
||||
// Forward declarations
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
class Array;
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
namespace tint {
|
||||
|
||||
namespace sem {
|
||||
// Forward declarations
|
||||
class ArrayType;
|
||||
|
||||
/// Array holds the semantic information for Array nodes.
|
||||
class Array : public Castable<Array, Node> {
|
||||
class Array : public Castable<Array, Type> {
|
||||
public:
|
||||
/// Constructor
|
||||
/// @param type the Array type
|
||||
/// @param align the byte alignment of the structure
|
||||
/// @param size the byte size of the structure
|
||||
/// @param element the array element type
|
||||
/// @param count the number of elements in the array. 0 represents a
|
||||
/// runtime-sized array.
|
||||
/// @param align the byte alignment of the array
|
||||
/// @param size the byte size of the array
|
||||
/// @param stride the number of bytes from the start of one element of the
|
||||
/// array to the start of the next element
|
||||
Array(sem::ArrayType* type, uint32_t align, uint32_t size, uint32_t stride);
|
||||
/// @param stride_implicit is true if the value of `stride` matches the
|
||||
/// element's natural stride.
|
||||
Array(Type const* element,
|
||||
uint32_t count,
|
||||
uint32_t align,
|
||||
uint32_t size,
|
||||
uint32_t stride,
|
||||
bool stride_implicit);
|
||||
|
||||
/// @return the resolved type of the Array
|
||||
sem::ArrayType* Type() const { return type_; }
|
||||
/// @return the array element type
|
||||
Type const* ElemType() const { return element_; }
|
||||
|
||||
/// @returns the number of elements in the array. 0 represents a runtime-sized
|
||||
/// array.
|
||||
uint32_t Count() const { return count_; }
|
||||
|
||||
/// @returns the byte alignment of the array
|
||||
/// @note this may differ from the alignment of a structure member of this
|
||||
@@ -47,17 +66,34 @@ class Array : public Castable<Array, Node> {
|
||||
/// @returns the byte size of the array
|
||||
/// @note this may differ from the size of a structure member of this array
|
||||
/// type, if the member is annotated with the `[[size(n)]]` decoration.
|
||||
uint32_t Size() const { return size_; }
|
||||
uint32_t SizeInBytes() const { return size_; }
|
||||
|
||||
/// @returns the number of bytes from the start of one element of the
|
||||
/// array to the start of the next element
|
||||
uint32_t Stride() const { return stride_; }
|
||||
|
||||
/// @returns true if the value returned by Stride() does matches the
|
||||
/// element's natural stride
|
||||
bool IsStrideImplicit() const { return stride_implicit_; }
|
||||
|
||||
/// @returns true if this array is runtime sized
|
||||
bool IsRuntimeSized() const { return count_ == 0; }
|
||||
|
||||
/// @returns the name for the type
|
||||
std::string type_name() const override;
|
||||
|
||||
/// @param symbols the program's symbol table
|
||||
/// @returns the name for this type that closely resembles how it would be
|
||||
/// declared in WGSL.
|
||||
std::string FriendlyName(const SymbolTable& symbols) const override;
|
||||
|
||||
private:
|
||||
sem::ArrayType* const type_;
|
||||
Type const* const element_;
|
||||
uint32_t const count_;
|
||||
uint32_t const align_;
|
||||
uint32_t const size_;
|
||||
uint32_t const stride_;
|
||||
bool const stride_implicit_;
|
||||
};
|
||||
|
||||
} // namespace sem
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
// Copyright 2020 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "src/sem/array_type.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "src/program_builder.h"
|
||||
|
||||
TINT_INSTANTIATE_TYPEINFO(tint::sem::ArrayType);
|
||||
|
||||
namespace tint {
|
||||
namespace sem {
|
||||
|
||||
ArrayType::ArrayType(Type* subtype,
|
||||
uint32_t size,
|
||||
ast::DecorationList decorations)
|
||||
: subtype_(subtype), size_(size), decos_(decorations) {}
|
||||
|
||||
ArrayType::ArrayType(ArrayType&&) = default;
|
||||
|
||||
ArrayType::~ArrayType() = default;
|
||||
|
||||
std::string ArrayType::type_name() const {
|
||||
TINT_ASSERT(subtype_);
|
||||
|
||||
std::string type_name = "__array" + subtype_->type_name();
|
||||
if (!IsRuntimeArray()) {
|
||||
type_name += "_" + std::to_string(size_);
|
||||
}
|
||||
for (auto* deco : decos_) {
|
||||
if (auto* stride = deco->As<ast::StrideDecoration>()) {
|
||||
type_name += "_stride_" + std::to_string(stride->stride());
|
||||
}
|
||||
}
|
||||
|
||||
return type_name;
|
||||
}
|
||||
|
||||
std::string ArrayType::FriendlyName(const SymbolTable& symbols) const {
|
||||
std::ostringstream out;
|
||||
for (auto* deco : decos_) {
|
||||
if (auto* stride = deco->As<ast::StrideDecoration>()) {
|
||||
out << "[[stride(" << stride->stride() << ")]] ";
|
||||
}
|
||||
}
|
||||
out << "array<" << subtype_->FriendlyName(symbols);
|
||||
if (!IsRuntimeArray()) {
|
||||
out << ", " << size_;
|
||||
}
|
||||
out << ">";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
} // namespace sem
|
||||
} // namespace tint
|
||||
@@ -1,70 +0,0 @@
|
||||
// Copyright 2020 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef SRC_SEM_ARRAY_TYPE_H_
|
||||
#define SRC_SEM_ARRAY_TYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/decoration.h"
|
||||
#include "src/sem/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace sem {
|
||||
|
||||
/// An array type. If size is zero then it is a runtime array.
|
||||
// TODO(amaiorano): https://crbug.com/tint/724 Fold into sem::Array once parsers
|
||||
// don't create this anymore.
|
||||
class ArrayType : public Castable<ArrayType, Type> {
|
||||
public:
|
||||
/// Constructor
|
||||
/// @param subtype the type of the array elements
|
||||
/// @param size the number of elements in the array. `0` represents a
|
||||
/// runtime-sized array.
|
||||
/// @param decorations the array decorations
|
||||
ArrayType(Type* subtype, uint32_t size, ast::DecorationList decorations);
|
||||
/// Move constructor
|
||||
ArrayType(ArrayType&&);
|
||||
~ArrayType() override;
|
||||
|
||||
/// @returns true if this is a runtime array.
|
||||
/// i.e. the size is determined at runtime
|
||||
bool IsRuntimeArray() const { return size_ == 0; }
|
||||
|
||||
/// @returns the array decorations
|
||||
const ast::DecorationList& decorations() const { return decos_; }
|
||||
|
||||
/// @returns the array type
|
||||
Type* type() const { return subtype_; }
|
||||
/// @returns the array size. Size is 0 for a runtime array
|
||||
uint32_t size() const { return size_; }
|
||||
|
||||
/// @returns the name for the type
|
||||
std::string type_name() const override;
|
||||
|
||||
/// @param symbols the program's symbol table
|
||||
/// @returns the name for this type that closely resembles how it would be
|
||||
/// declared in WGSL.
|
||||
std::string FriendlyName(const SymbolTable& symbols) const override;
|
||||
|
||||
private:
|
||||
Type* const subtype_;
|
||||
uint32_t const size_;
|
||||
ast::DecorationList const decos_;
|
||||
};
|
||||
|
||||
} // namespace sem
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_SEM_ARRAY_TYPE_H_
|
||||
@@ -27,7 +27,7 @@ TEST_F(BoolTest, Is) {
|
||||
Type* ty = &b;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_TRUE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -32,7 +32,7 @@ TEST_F(DepthTextureTest, Is) {
|
||||
Type* ty = &d;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -33,7 +33,7 @@ TEST_F(ExternalTextureTest, Is) {
|
||||
Type* ty = &s;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -27,7 +27,7 @@ TEST_F(F32Test, Is) {
|
||||
Type* ty = &f;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_TRUE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -27,7 +27,7 @@ TEST_F(I32Test, Is) {
|
||||
Type* ty = &i;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_TRUE(ty->Is<I32>());
|
||||
|
||||
@@ -38,7 +38,7 @@ TEST_F(MatrixTest, Is) {
|
||||
Type* ty = &m;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -33,7 +33,7 @@ TEST_F(MultisampledTextureTest, Is) {
|
||||
Type* ty = &s;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -21,7 +21,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::Pointer);
|
||||
namespace tint {
|
||||
namespace sem {
|
||||
|
||||
Pointer::Pointer(Type* subtype, ast::StorageClass storage_class)
|
||||
Pointer::Pointer(const Type* subtype, ast::StorageClass storage_class)
|
||||
: subtype_(subtype), storage_class_(storage_class) {}
|
||||
|
||||
std::string Pointer::type_name() const {
|
||||
|
||||
@@ -26,16 +26,16 @@ namespace sem {
|
||||
/// A pointer type.
|
||||
class Pointer : public Castable<Pointer, Type> {
|
||||
public:
|
||||
/// Construtor
|
||||
/// Constructor
|
||||
/// @param subtype the pointee type
|
||||
/// @param storage_class the storage class of the pointer
|
||||
Pointer(Type* subtype, ast::StorageClass storage_class);
|
||||
Pointer(const Type* subtype, ast::StorageClass storage_class);
|
||||
/// Move constructor
|
||||
Pointer(Pointer&&);
|
||||
~Pointer() override;
|
||||
|
||||
/// @returns the pointee type
|
||||
Type* type() const { return subtype_; }
|
||||
const Type* type() const { return subtype_; }
|
||||
/// @returns the storage class of the pointer
|
||||
ast::StorageClass storage_class() const { return storage_class_; }
|
||||
|
||||
@@ -48,7 +48,7 @@ class Pointer : public Castable<Pointer, Type> {
|
||||
std::string FriendlyName(const SymbolTable& symbols) const override;
|
||||
|
||||
private:
|
||||
Type* const subtype_;
|
||||
Type const* const subtype_;
|
||||
ast::StorageClass const storage_class_;
|
||||
};
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ TEST_F(PointerTest, Is) {
|
||||
Type* ty = &p;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -32,7 +32,7 @@ TEST_F(SampledTextureTest, Is) {
|
||||
Type* ty = &s;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -38,7 +38,7 @@ TEST_F(SamplerTest, Is) {
|
||||
Type* ty = &s;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -24,30 +24,35 @@ using ArrayTest = TestHelper;
|
||||
|
||||
TEST_F(ArrayTest, CreateSizedArray) {
|
||||
U32 u32;
|
||||
ArrayType arr{&u32, 3, ast::DecorationList{}};
|
||||
EXPECT_EQ(arr.type(), &u32);
|
||||
EXPECT_EQ(arr.size(), 3u);
|
||||
EXPECT_TRUE(arr.Is<ArrayType>());
|
||||
EXPECT_FALSE(arr.IsRuntimeArray());
|
||||
auto* arr = create<Array>(&u32, 2, 4, 8, 16, true);
|
||||
EXPECT_EQ(arr->ElemType(), &u32);
|
||||
EXPECT_EQ(arr->Count(), 2u);
|
||||
EXPECT_EQ(arr->Align(), 4u);
|
||||
EXPECT_EQ(arr->SizeInBytes(), 8u);
|
||||
EXPECT_EQ(arr->Stride(), 16u);
|
||||
EXPECT_TRUE(arr->IsStrideImplicit());
|
||||
EXPECT_FALSE(arr->IsRuntimeSized());
|
||||
}
|
||||
|
||||
TEST_F(ArrayTest, CreateRuntimeArray) {
|
||||
U32 u32;
|
||||
ArrayType arr{&u32, 0, ast::DecorationList{}};
|
||||
EXPECT_EQ(arr.type(), &u32);
|
||||
EXPECT_EQ(arr.size(), 0u);
|
||||
EXPECT_TRUE(arr.Is<ArrayType>());
|
||||
EXPECT_TRUE(arr.IsRuntimeArray());
|
||||
auto* arr = create<Array>(&u32, 0, 4, 8, 16, true);
|
||||
EXPECT_EQ(arr->ElemType(), &u32);
|
||||
EXPECT_EQ(arr->Count(), 0u);
|
||||
EXPECT_EQ(arr->Align(), 4u);
|
||||
EXPECT_EQ(arr->SizeInBytes(), 8u);
|
||||
EXPECT_EQ(arr->Stride(), 16u);
|
||||
EXPECT_TRUE(arr->IsStrideImplicit());
|
||||
EXPECT_TRUE(arr->IsRuntimeSized());
|
||||
}
|
||||
|
||||
TEST_F(ArrayTest, Is) {
|
||||
I32 i32;
|
||||
|
||||
ArrayType arr{&i32, 3, ast::DecorationList{}};
|
||||
Type* ty = &arr;
|
||||
Type* ty = create<Array>(&i32, 2, 4, 8, 4, true);
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_TRUE(ty->Is<ArrayType>());
|
||||
EXPECT_TRUE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
@@ -62,37 +67,34 @@ TEST_F(ArrayTest, Is) {
|
||||
|
||||
TEST_F(ArrayTest, TypeName) {
|
||||
I32 i32;
|
||||
ArrayType arr{&i32, 0, ast::DecorationList{}};
|
||||
EXPECT_EQ(arr.type_name(), "__array__i32");
|
||||
auto* arr = create<Array>(&i32, 2, 0, 4, 4, true);
|
||||
EXPECT_EQ(arr->type_name(), "__array__i32_count_2_align_0_size_4_stride_4");
|
||||
}
|
||||
|
||||
TEST_F(ArrayTest, FriendlyNameRuntimeSized) {
|
||||
ArrayType arr{ty.i32(), 0, ast::DecorationList{}};
|
||||
EXPECT_EQ(arr.FriendlyName(Symbols()), "array<i32>");
|
||||
auto* arr = create<Array>(ty.i32(), 0, 0, 4, 4, true);
|
||||
EXPECT_EQ(arr->FriendlyName(Symbols()), "array<i32>");
|
||||
}
|
||||
|
||||
TEST_F(ArrayTest, FriendlyNameStaticSized) {
|
||||
ArrayType arr{ty.i32(), 5, ast::DecorationList{}};
|
||||
EXPECT_EQ(arr.FriendlyName(Symbols()), "array<i32, 5>");
|
||||
auto* arr = create<Array>(ty.i32(), 5, 4, 20, 4, true);
|
||||
EXPECT_EQ(arr->FriendlyName(Symbols()), "array<i32, 5>");
|
||||
}
|
||||
|
||||
TEST_F(ArrayTest, FriendlyNameWithStride) {
|
||||
ArrayType arr{ty.i32(), 5,
|
||||
ast::DecorationList{create<ast::StrideDecoration>(32)}};
|
||||
EXPECT_EQ(arr.FriendlyName(Symbols()), "[[stride(32)]] array<i32, 5>");
|
||||
TEST_F(ArrayTest, FriendlyNameRuntimeSizedNonImplicitStride) {
|
||||
auto* arr = create<Array>(ty.i32(), 0, 0, 4, 4, false);
|
||||
EXPECT_EQ(arr->FriendlyName(Symbols()), "[[stride(4)]] array<i32>");
|
||||
}
|
||||
|
||||
TEST_F(ArrayTest, FriendlyNameStaticSizedNonImplicitStride) {
|
||||
auto* arr = create<Array>(ty.i32(), 5, 4, 20, 4, false);
|
||||
EXPECT_EQ(arr->FriendlyName(Symbols()), "[[stride(4)]] array<i32, 5>");
|
||||
}
|
||||
|
||||
TEST_F(ArrayTest, TypeName_RuntimeArray) {
|
||||
I32 i32;
|
||||
ArrayType arr{&i32, 3, ast::DecorationList{}};
|
||||
EXPECT_EQ(arr.type_name(), "__array__i32_3");
|
||||
}
|
||||
|
||||
TEST_F(ArrayTest, TypeName_WithStride) {
|
||||
I32 i32;
|
||||
ArrayType arr{&i32, 3,
|
||||
ast::DecorationList{create<ast::StrideDecoration>(16)}};
|
||||
EXPECT_EQ(arr.type_name(), "__array__i32_3_stride_16");
|
||||
auto* arr = create<Array>(&i32, 2, 4, 8, 16, true);
|
||||
EXPECT_EQ(arr->type_name(), "__array__i32_count_2_align_4_size_8_stride_16");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -45,7 +45,7 @@ TEST_F(StructTest, Is) {
|
||||
sem::Type* ty = s;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -34,7 +34,7 @@ TEST_F(StorageTextureTest, Is) {
|
||||
Type* ty = s;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -37,28 +37,28 @@ Type::Type(Type&&) = default;
|
||||
|
||||
Type::~Type() = default;
|
||||
|
||||
Type* Type::UnwrapPtrIfNeeded() {
|
||||
const Type* Type::UnwrapPtrIfNeeded() const {
|
||||
if (auto* ptr = As<sem::Pointer>()) {
|
||||
return ptr->type();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
Type* Type::UnwrapAliasIfNeeded() {
|
||||
Type* unwrapped = this;
|
||||
const Type* Type::UnwrapAliasIfNeeded() const {
|
||||
const Type* unwrapped = this;
|
||||
while (auto* ptr = unwrapped->As<sem::Alias>()) {
|
||||
unwrapped = ptr->type();
|
||||
}
|
||||
return unwrapped;
|
||||
}
|
||||
|
||||
Type* Type::UnwrapIfNeeded() {
|
||||
const Type* Type::UnwrapIfNeeded() const {
|
||||
auto* where = this;
|
||||
while (true) {
|
||||
if (auto* alias = where->As<sem::Alias>()) {
|
||||
where = alias->type();
|
||||
where = alias->type();
|
||||
} else if (auto* access = where->As<sem::AccessControl>()) {
|
||||
where = access->type();
|
||||
where = access->type();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -66,7 +66,7 @@ Type* Type::UnwrapIfNeeded() {
|
||||
return where;
|
||||
}
|
||||
|
||||
Type* Type::UnwrapAll() {
|
||||
const Type* Type::UnwrapAll() const {
|
||||
return UnwrapIfNeeded()->UnwrapPtrIfNeeded()->UnwrapIfNeeded();
|
||||
}
|
||||
|
||||
|
||||
@@ -46,22 +46,11 @@ class Type : public Castable<Type, Node> {
|
||||
virtual std::string FriendlyName(const SymbolTable& symbols) const = 0;
|
||||
|
||||
/// @returns the pointee type if this is a pointer, `this` otherwise
|
||||
Type* UnwrapPtrIfNeeded();
|
||||
|
||||
/// @returns the pointee type if this is a pointer, `this` otherwise
|
||||
const Type* UnwrapPtrIfNeeded() const {
|
||||
return const_cast<Type*>(this)->UnwrapPtrIfNeeded();
|
||||
}
|
||||
const Type* UnwrapPtrIfNeeded() const;
|
||||
|
||||
/// @returns the most deeply nested aliased type if this is an alias, `this`
|
||||
/// otherwise
|
||||
Type* UnwrapAliasIfNeeded();
|
||||
|
||||
/// @returns the most deeply nested aliased type if this is an alias, `this`
|
||||
/// otherwise
|
||||
const Type* UnwrapAliasIfNeeded() const {
|
||||
return const_cast<Type*>(this)->UnwrapAliasIfNeeded();
|
||||
}
|
||||
const Type* UnwrapAliasIfNeeded() const;
|
||||
|
||||
/// Removes all levels of aliasing and access control.
|
||||
/// This is just enough to assist with WGSL translation
|
||||
@@ -69,31 +58,14 @@ class Type : public Castable<Type, Node> {
|
||||
/// identifier-like expression as an l-value to its corresponding r-value,
|
||||
/// plus see through the wrappers on either side.
|
||||
/// @returns the completely unaliased type.
|
||||
Type* UnwrapIfNeeded();
|
||||
|
||||
/// Removes all levels of aliasing and access control.
|
||||
/// This is just enough to assist with WGSL translation
|
||||
/// in that you want see through one level of pointer to get from an
|
||||
/// identifier-like expression as an l-value to its corresponding r-value,
|
||||
/// plus see through the wrappers on either side.
|
||||
/// @returns the completely unaliased type.
|
||||
const Type* UnwrapIfNeeded() const {
|
||||
return const_cast<Type*>(this)->UnwrapIfNeeded();
|
||||
}
|
||||
const Type* UnwrapIfNeeded() const;
|
||||
|
||||
/// Returns the type found after:
|
||||
/// - removing all layers of aliasing and access control if they exist, then
|
||||
/// - removing the pointer, if it exists, then
|
||||
/// - removing all further layers of aliasing or access control, if they exist
|
||||
/// @returns the unwrapped type
|
||||
Type* UnwrapAll();
|
||||
|
||||
/// Returns the type found after:
|
||||
/// - removing all layers of aliasing and access control if they exist, then
|
||||
/// - removing the pointer, if it exists, then
|
||||
/// - removing all further layers of aliasing or access control, if they exist
|
||||
/// @returns the unwrapped type
|
||||
const Type* UnwrapAll() const { return const_cast<Type*>(this)->UnwrapAll(); }
|
||||
const Type* UnwrapAll() const;
|
||||
|
||||
/// @returns true if this type is a scalar
|
||||
bool is_scalar() const;
|
||||
|
||||
@@ -34,7 +34,6 @@ class Variable;
|
||||
namespace sem {
|
||||
// Forward declarations
|
||||
class Array;
|
||||
class ArrayType;
|
||||
class Call;
|
||||
class Expression;
|
||||
class Function;
|
||||
@@ -51,7 +50,6 @@ class Variable;
|
||||
/// rules will be used to infer the return type based on the argument type.
|
||||
struct TypeMappings {
|
||||
//! @cond Doxygen_Suppress
|
||||
Array* operator()(sem::ArrayType*);
|
||||
Call* operator()(ast::CallExpression*);
|
||||
Expression* operator()(ast::Expression*);
|
||||
Function* operator()(ast::Function*);
|
||||
|
||||
@@ -27,7 +27,7 @@ TEST_F(U32Test, Is) {
|
||||
Type* ty = &u;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
@@ -35,7 +35,7 @@ TEST_F(VectorTest, Is) {
|
||||
Type* ty = &v;
|
||||
EXPECT_FALSE(ty->Is<AccessControl>());
|
||||
EXPECT_FALSE(ty->Is<Alias>());
|
||||
EXPECT_FALSE(ty->Is<ArrayType>());
|
||||
EXPECT_FALSE(ty->Is<Array>());
|
||||
EXPECT_FALSE(ty->Is<Bool>());
|
||||
EXPECT_FALSE(ty->Is<F32>());
|
||||
EXPECT_FALSE(ty->Is<I32>());
|
||||
|
||||
Reference in New Issue
Block a user