Move type/* files to sem/ directory

Bug: tint:724
Change-Id: I45d83d32dbce1fbee265810615c18219ef879b20
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/48363
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Antonio Maiorano
2021-04-19 22:54:43 +00:00
committed by Commit Bot service account
parent 3751fd2290
commit aea9c68de9
116 changed files with 468 additions and 471 deletions

View File

@@ -0,0 +1,75 @@
// 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/access_control_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::AccessControl);
namespace tint {
namespace sem {
AccessControl::AccessControl(ast::AccessControl access, Type* subtype)
: access_(access), subtype_(subtype) {
TINT_ASSERT(subtype_);
TINT_ASSERT(!subtype_->Is<AccessControl>());
}
AccessControl::AccessControl(AccessControl&&) = default;
AccessControl::~AccessControl() = default;
std::string AccessControl::type_name() const {
std::string name = "__access_control_";
switch (access_) {
case ast::AccessControl::kReadOnly:
name += "read_only";
break;
case ast::AccessControl::kWriteOnly:
name += "write_only";
break;
case ast::AccessControl::kReadWrite:
name += "read_write";
break;
}
return name + subtype_->type_name();
}
std::string AccessControl::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out;
out << "[[access(";
switch (access_) {
case ast::AccessControl::kReadOnly:
out << "read";
break;
case ast::AccessControl::kWriteOnly:
out << "write";
break;
case ast::AccessControl::kReadWrite:
out << "read_write";
break;
}
out << ")]] " << subtype_->FriendlyName(symbols);
return out.str();
}
AccessControl* AccessControl::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto* ty = ctx->Clone(type());
return ctx->dst->create<AccessControl>(access_, ty);
}
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,70 @@
// 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_ACCESS_CONTROL_TYPE_H_
#define SRC_SEM_ACCESS_CONTROL_TYPE_H_
#include <string>
#include "src/ast/access_control.h"
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// An access control type. Holds an access setting and pointer to another type.
class AccessControl : public Castable<AccessControl, Type> {
public:
/// Constructor
/// @param access the access control setting
/// @param subtype the access controlled type
AccessControl(ast::AccessControl access, Type* subtype);
/// Move constructor
AccessControl(AccessControl&&);
~AccessControl() override;
/// @returns true if the access control is read only
bool IsReadOnly() const { return access_ == ast::AccessControl::kReadOnly; }
/// @returns true if the access control is write only
bool IsWriteOnly() const { return access_ == ast::AccessControl::kWriteOnly; }
/// @returns true if the access control is read/write
bool IsReadWrite() const { return access_ == ast::AccessControl::kReadWrite; }
/// @returns the access control value
ast::AccessControl access_control() const { return access_; }
/// @returns the subtype type
Type* type() const { return subtype_; }
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
AccessControl* Clone(CloneContext* ctx) const override;
private:
ast::AccessControl const access_;
Type* const subtype_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_ACCESS_CONTROL_TYPE_H_

View File

@@ -0,0 +1,100 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
using AccessControlTest = TestHelper;
TEST_F(AccessControlTest, Create) {
U32 u32;
AccessControl a{ast::AccessControl::kReadWrite, &u32};
EXPECT_TRUE(a.IsReadWrite());
EXPECT_EQ(a.type(), &u32);
}
TEST_F(AccessControlTest, Is) {
I32 i32;
AccessControl at{ast::AccessControl::kReadOnly, &i32};
Type* ty = &at;
EXPECT_TRUE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(AccessControlTest, AccessRead) {
I32 i32;
AccessControl at{ast::AccessControl::kReadOnly, &i32};
EXPECT_TRUE(at.IsReadOnly());
EXPECT_FALSE(at.IsWriteOnly());
EXPECT_FALSE(at.IsReadWrite());
EXPECT_EQ(at.type_name(), "__access_control_read_only__i32");
}
TEST_F(AccessControlTest, AccessWrite) {
I32 i32;
AccessControl at{ast::AccessControl::kWriteOnly, &i32};
EXPECT_FALSE(at.IsReadOnly());
EXPECT_TRUE(at.IsWriteOnly());
EXPECT_FALSE(at.IsReadWrite());
EXPECT_EQ(at.type_name(), "__access_control_write_only__i32");
}
TEST_F(AccessControlTest, AccessReadWrite) {
I32 i32;
AccessControl at{ast::AccessControl::kReadWrite, &i32};
EXPECT_FALSE(at.IsReadOnly());
EXPECT_FALSE(at.IsWriteOnly());
EXPECT_TRUE(at.IsReadWrite());
EXPECT_EQ(at.type_name(), "__access_control_read_write__i32");
}
TEST_F(AccessControlTest, FriendlyNameReadOnly) {
AccessControl at{ast::AccessControl::kReadOnly, ty.i32()};
EXPECT_EQ(at.FriendlyName(Symbols()), "[[access(read)]] i32");
}
TEST_F(AccessControlTest, FriendlyNameWriteOnly) {
AccessControl at{ast::AccessControl::kWriteOnly, ty.i32()};
EXPECT_EQ(at.FriendlyName(Symbols()), "[[access(write)]] i32");
}
TEST_F(AccessControlTest, FriendlyNameReadWrite) {
AccessControl at{ast::AccessControl::kReadWrite, ty.i32()};
EXPECT_EQ(at.FriendlyName(Symbols()), "[[access(read_write)]] i32");
}
} // namespace
} // namespace sem
} // namespace tint

49
src/sem/alias_type.cc Normal file
View File

@@ -0,0 +1,49 @@
// 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/alias_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Alias);
namespace tint {
namespace sem {
Alias::Alias(const Symbol& sym, Type* subtype)
: symbol_(sym), subtype_(subtype) {
TINT_ASSERT(subtype_);
}
Alias::Alias(Alias&&) = default;
Alias::~Alias() = default;
std::string Alias::type_name() const {
return "__alias_" + symbol_.to_str() + subtype_->type_name();
}
std::string Alias::FriendlyName(const SymbolTable& symbols) const {
return symbols.NameFor(symbol_);
}
Alias* Alias::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto sym = ctx->Clone(symbol());
auto* ty = ctx->Clone(type());
return ctx->dst->create<Alias>(sym, ty);
}
} // namespace sem
} // namespace tint

63
src/sem/alias_type.h Normal file
View File

@@ -0,0 +1,63 @@
// 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_ALIAS_TYPE_H_
#define SRC_SEM_ALIAS_TYPE_H_
#include <string>
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// A type alias type. Holds a name and pointer to another type.
class Alias : public Castable<Alias, Type> {
public:
/// Constructor
/// @param sym the symbol for the alias
/// @param subtype the alias'd type
Alias(const Symbol& sym, Type* subtype);
/// Move constructor
Alias(Alias&&);
/// Destructor
~Alias() override;
/// @returns the alias symbol
Symbol symbol() const { return symbol_; }
/// @returns the alias type
Type* type() const { return subtype_; }
/// @returns the type_name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Alias* Clone(CloneContext* ctx) const override;
private:
Symbol const symbol_;
Type* const subtype_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_ALIAS_TYPE_H_

149
src/sem/alias_type_test.cc Normal file
View File

@@ -0,0 +1,149 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
using AliasTest = TestHelper;
TEST_F(AliasTest, Create) {
auto* a = ty.alias("a_type", ty.u32());
EXPECT_EQ(a->symbol(), Symbol(1, ID()));
EXPECT_EQ(a->type(), ty.u32());
}
TEST_F(AliasTest, Is) {
auto* at = ty.alias("a", ty.i32());
sem::Type* ty = at;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_TRUE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(AliasTest, TypeName) {
auto* at = ty.alias("Particle", ty.i32());
EXPECT_EQ(at->type_name(), "__alias_$1__i32");
}
TEST_F(AliasTest, FriendlyName) {
auto* at = ty.alias("Particle", ty.i32());
EXPECT_EQ(at->FriendlyName(Symbols()), "Particle");
}
TEST_F(AliasTest, UnwrapIfNeeded_Alias) {
auto* a = ty.alias("a_type", ty.u32());
EXPECT_EQ(a->symbol(), Symbol(1, ID()));
EXPECT_EQ(a->type(), ty.u32());
EXPECT_EQ(a->UnwrapIfNeeded(), ty.u32());
EXPECT_EQ(ty.u32()->UnwrapIfNeeded(), ty.u32());
}
TEST_F(AliasTest, UnwrapIfNeeded_AccessControl) {
AccessControl a{ast::AccessControl::kReadOnly, ty.u32()};
EXPECT_EQ(a.type(), ty.u32());
EXPECT_EQ(a.UnwrapIfNeeded(), ty.u32());
}
TEST_F(AliasTest, UnwrapIfNeeded_MultiLevel) {
auto* a = ty.alias("a_type", ty.u32());
auto* aa = ty.alias("aa_type", a);
EXPECT_EQ(aa->symbol(), Symbol(2, ID()));
EXPECT_EQ(aa->type(), a);
EXPECT_EQ(aa->UnwrapIfNeeded(), ty.u32());
}
TEST_F(AliasTest, UnwrapIfNeeded_MultiLevel_AliasAccessControl) {
auto* a = ty.alias("a_type", ty.u32());
AccessControl aa{ast::AccessControl::kReadWrite, a};
EXPECT_EQ(aa.type(), a);
EXPECT_EQ(aa.UnwrapIfNeeded(), ty.u32());
}
TEST_F(AliasTest, UnwrapAll_TwiceAliasPointerTwiceAlias) {
auto* a = ty.alias("a_type", ty.u32());
auto* aa = ty.alias("aa_type", a);
Pointer paa{aa, ast::StorageClass::kUniform};
auto* apaa = ty.alias("paa_type", &paa);
auto* aapaa = ty.alias("aapaa_type", apaa);
EXPECT_EQ(aapaa->symbol(), Symbol(4, ID()));
EXPECT_EQ(aapaa->type(), apaa);
EXPECT_EQ(aapaa->UnwrapAll(), ty.u32());
}
TEST_F(AliasTest, UnwrapAll_SecondConsecutivePointerBlocksUnrapping) {
auto* a = ty.alias("a_type", ty.u32());
auto* aa = ty.alias("aa_type", a);
Pointer paa{aa, ast::StorageClass::kUniform};
Pointer ppaa{&paa, ast::StorageClass::kUniform};
auto* appaa = ty.alias("appaa_type", &ppaa);
EXPECT_EQ(appaa->UnwrapAll(), &paa);
}
TEST_F(AliasTest, UnwrapAll_SecondNonConsecutivePointerBlocksUnrapping) {
auto* a = ty.alias("a_type", ty.u32());
auto* aa = ty.alias("aa_type", a);
Pointer paa{aa, ast::StorageClass::kUniform};
auto* apaa = ty.alias("apaa_type", &paa);
auto* aapaa = ty.alias("aapaa_type", apaa);
Pointer paapaa{aapaa, ast::StorageClass::kUniform};
auto* apaapaa = ty.alias("apaapaa_type", &paapaa);
EXPECT_EQ(apaapaa->UnwrapAll(), &paa);
}
TEST_F(AliasTest, UnwrapAll_AccessControlPointer) {
AccessControl a{ast::AccessControl::kReadOnly, ty.u32()};
Pointer pa{&a, ast::StorageClass::kUniform};
EXPECT_EQ(pa.type(), &a);
EXPECT_EQ(pa.UnwrapAll(), ty.u32());
}
TEST_F(AliasTest, UnwrapAll_PointerAccessControl) {
Pointer p{ty.u32(), ast::StorageClass::kUniform};
AccessControl a{ast::AccessControl::kReadOnly, &p};
EXPECT_EQ(a.type(), &p);
EXPECT_EQ(a.UnwrapAll(), ty.u32());
}
TEST_F(AliasTest, UnwrapAliasIfNeeded) {
auto* alias1 = ty.alias("alias1", ty.f32());
auto* alias2 = ty.alias("alias2", alias1);
auto* alias3 = ty.alias("alias3", alias2);
EXPECT_EQ(alias3->UnwrapAliasIfNeeded(), ty.f32());
}
} // namespace
} // namespace sem
} // namespace tint

74
src/sem/array_type.cc Normal file
View File

@@ -0,0 +1,74 @@
// 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();
}
ArrayType* ArrayType::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto* ty = ctx->Clone(type());
auto decos = ctx->Clone(decorations());
return ctx->dst->create<ArrayType>(ty, size_, decos);
}
} // namespace sem
} // namespace tint

75
src/sem/array_type.h Normal file
View File

@@ -0,0 +1,75 @@
// 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
ArrayType* Clone(CloneContext* ctx) const override;
private:
Type* const subtype_;
uint32_t const size_;
ast::DecorationList const decos_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_ARRAY_TYPE_H_

100
src/sem/array_type_test.cc Normal file
View File

@@ -0,0 +1,100 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
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());
}
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());
}
TEST_F(ArrayTest, Is) {
I32 i32;
ArrayType arr{&i32, 3, ast::DecorationList{}};
Type* ty = &arr;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_TRUE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(ArrayTest, TypeName) {
I32 i32;
ArrayType arr{&i32, 0, ast::DecorationList{}};
EXPECT_EQ(arr.type_name(), "__array__i32");
}
TEST_F(ArrayTest, FriendlyNameRuntimeSized) {
ArrayType arr{ty.i32(), 0, ast::DecorationList{}};
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>");
}
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, 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");
}
} // namespace
} // namespace sem
} // namespace tint

43
src/sem/bool_type.cc Normal file
View File

@@ -0,0 +1,43 @@
// 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/bool_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Bool);
namespace tint {
namespace sem {
Bool::Bool() = default;
Bool::Bool(Bool&&) = default;
Bool::~Bool() = default;
std::string Bool::type_name() const {
return "__bool";
}
std::string Bool::FriendlyName(const SymbolTable&) const {
return "bool";
}
Bool* Bool::Clone(CloneContext* ctx) const {
return ctx->dst->create<Bool>();
}
} // namespace sem
} // namespace tint

57
src/sem/bool_type.h Normal file
View File

@@ -0,0 +1,57 @@
// 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_BOOL_TYPE_H_
#define SRC_SEM_BOOL_TYPE_H_
#include <string>
#include "src/sem/type.h"
// X11 likes to #define Bool leading to confusing error messages.
// If its defined, undefine it.
#ifdef Bool
#undef Bool
#endif
namespace tint {
namespace sem {
/// A boolean type
class Bool : public Castable<Bool, Type> {
public:
/// Constructor
Bool();
/// Move constructor
Bool(Bool&&);
~Bool() override;
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Bool* Clone(CloneContext* ctx) const override;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_BOOL_TYPE_H_

55
src/sem/bool_type_test.cc Normal file
View File

@@ -0,0 +1,55 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
using BoolTest = TestHelper;
TEST_F(BoolTest, Is) {
Bool b;
Type* ty = &b;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_TRUE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(BoolTest, TypeName) {
Bool b;
EXPECT_EQ(b.type_name(), "__bool");
}
TEST_F(BoolTest, FriendlyName) {
Bool b;
EXPECT_EQ(b.FriendlyName(Symbols()), "bool");
}
} // namespace
} // namespace sem
} // namespace tint

View File

@@ -18,16 +18,13 @@
#include <vector>
#include "src/sem/node.h"
#include "src/type/sampler_type.h"
#include "src/sem/sampler_type.h"
namespace tint {
namespace sem {
// Forward declarations
namespace sem {
class Type;
} // namespace sem
namespace sem {
/// Parameter describes a single parameter of a call target
struct Parameter {

View File

@@ -0,0 +1,57 @@
// 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/depth_texture_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::DepthTexture);
namespace tint {
namespace sem {
namespace {
bool IsValidDepthDimension(TextureDimension dim) {
return dim == TextureDimension::k2d || dim == TextureDimension::k2dArray ||
dim == TextureDimension::kCube || dim == TextureDimension::kCubeArray;
}
} // namespace
DepthTexture::DepthTexture(TextureDimension dim) : Base(dim) {
TINT_ASSERT(IsValidDepthDimension(dim));
}
DepthTexture::DepthTexture(DepthTexture&&) = default;
DepthTexture::~DepthTexture() = default;
std::string DepthTexture::type_name() const {
std::ostringstream out;
out << "__depth_texture_" << dim();
return out.str();
}
std::string DepthTexture::FriendlyName(const SymbolTable&) const {
std::ostringstream out;
out << "texture_depth_" << dim();
return out.str();
}
DepthTexture* DepthTexture::Clone(CloneContext* ctx) const {
return ctx->dst->create<DepthTexture>(dim());
}
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,52 @@
// 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_DEPTH_TEXTURE_TYPE_H_
#define SRC_SEM_DEPTH_TEXTURE_TYPE_H_
#include <string>
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
/// A depth texture type.
class DepthTexture : public Castable<DepthTexture, Texture> {
public:
/// Constructor
/// @param dim the dimensionality of the texture
explicit DepthTexture(TextureDimension dim);
/// Move constructor
DepthTexture(DepthTexture&&);
~DepthTexture() override;
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
DepthTexture* Clone(CloneContext* ctx) const override;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_DEPTH_TEXTURE_TYPE_H_

View File

@@ -0,0 +1,74 @@
// 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/depth_texture_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/access_control_type.h"
#include "src/sem/external_texture_type.h"
#include "src/sem/sampled_texture_type.h"
#include "src/sem/storage_texture_type.h"
namespace tint {
namespace sem {
namespace {
using DepthTextureTest = TestHelper;
TEST_F(DepthTextureTest, Is) {
DepthTexture d(TextureDimension::kCube);
Type* ty = &d;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(DepthTextureTest, IsTexture) {
DepthTexture d(TextureDimension::kCube);
Texture* ty = &d;
EXPECT_TRUE(ty->Is<DepthTexture>());
EXPECT_FALSE(ty->Is<ExternalTexture>());
EXPECT_FALSE(ty->Is<SampledTexture>());
EXPECT_FALSE(ty->Is<StorageTexture>());
}
TEST_F(DepthTextureTest, Dim) {
DepthTexture d(TextureDimension::kCube);
EXPECT_EQ(d.dim(), TextureDimension::kCube);
}
TEST_F(DepthTextureTest, TypeName) {
DepthTexture d(TextureDimension::kCube);
EXPECT_EQ(d.type_name(), "__depth_texture_cube");
}
TEST_F(DepthTextureTest, FriendlyName) {
DepthTexture d(TextureDimension::kCube);
EXPECT_EQ(d.FriendlyName(Symbols()), "texture_depth_cube");
}
} // namespace
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,47 @@
// Copyright 2021 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/external_texture_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::ExternalTexture);
namespace tint {
namespace sem {
ExternalTexture::ExternalTexture() : Base(TextureDimension::k2d) {}
ExternalTexture::ExternalTexture(ExternalTexture&&) = default;
ExternalTexture::~ExternalTexture() = default;
std::string ExternalTexture::type_name() const {
std::ostringstream out;
out << "__external_texture";
return out.str();
}
std::string ExternalTexture::FriendlyName(const SymbolTable&) const {
std::ostringstream out;
out << "texture_external";
return out.str();
}
ExternalTexture* ExternalTexture::Clone(CloneContext* ctx) const {
return ctx->dst->create<ExternalTexture>();
}
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,49 @@
// Copyright 2021 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_EXTERNAL_TEXTURE_TYPE_H_
#define SRC_SEM_EXTERNAL_TEXTURE_TYPE_H_
#include <string>
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
class ExternalTexture : public Castable<ExternalTexture, Texture> {
public:
/// Constructor
ExternalTexture();
/// Move constructor
ExternalTexture(ExternalTexture&&);
~ExternalTexture() override;
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
ExternalTexture* Clone(CloneContext* ctx) const override;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_EXTERNAL_TEXTURE_TYPE_H_

View File

@@ -0,0 +1,79 @@
// Copyright 2021 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/external_texture_type.h"
#include "src/sem/access_control_type.h"
#include "src/sem/depth_texture_type.h"
#include "src/sem/multisampled_texture_type.h"
#include "src/sem/sampled_texture_type.h"
#include "src/sem/storage_texture_type.h"
#include "src/sem/test_helper.h"
namespace tint {
namespace sem {
namespace {
using ExternalTextureTest = TestHelper;
TEST_F(ExternalTextureTest, Is) {
F32 f32;
ExternalTexture s;
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(ExternalTextureTest, IsTexture) {
F32 f32;
ExternalTexture s;
Texture* ty = &s;
EXPECT_FALSE(ty->Is<DepthTexture>());
EXPECT_TRUE(ty->Is<ExternalTexture>());
EXPECT_FALSE(ty->Is<MultisampledTexture>());
EXPECT_FALSE(ty->Is<SampledTexture>());
EXPECT_FALSE(ty->Is<StorageTexture>());
}
TEST_F(ExternalTextureTest, Dim) {
F32 f32;
ExternalTexture s;
EXPECT_EQ(s.dim(), TextureDimension::k2d);
}
TEST_F(ExternalTextureTest, TypeName) {
F32 f32;
ExternalTexture s;
EXPECT_EQ(s.type_name(), "__external_texture");
}
TEST_F(ExternalTextureTest, FriendlyName) {
ExternalTexture s;
EXPECT_EQ(s.FriendlyName(Symbols()), "texture_external");
}
} // namespace
} // namespace sem
} // namespace tint

43
src/sem/f32_type.cc Normal file
View File

@@ -0,0 +1,43 @@
// 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/f32_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::F32);
namespace tint {
namespace sem {
F32::F32() = default;
F32::F32(F32&&) = default;
F32::~F32() = default;
std::string F32::type_name() const {
return "__f32";
}
std::string F32::FriendlyName(const SymbolTable&) const {
return "f32";
}
F32* F32::Clone(CloneContext* ctx) const {
return ctx->dst->create<F32>();
}
} // namespace sem
} // namespace tint

51
src/sem/f32_type.h Normal file
View File

@@ -0,0 +1,51 @@
// 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_F32_TYPE_H_
#define SRC_SEM_F32_TYPE_H_
#include <string>
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// A float 32 type
class F32 : public Castable<F32, Type> {
public:
/// Constructor
F32();
/// Move constructor
F32(F32&&);
~F32() override;
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
F32* Clone(CloneContext* ctx) const override;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_F32_TYPE_H_

55
src/sem/f32_type_test.cc Normal file
View File

@@ -0,0 +1,55 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
using F32Test = TestHelper;
TEST_F(F32Test, Is) {
F32 f;
Type* ty = &f;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_TRUE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(F32Test, TypeName) {
F32 f;
EXPECT_EQ(f.type_name(), "__f32");
}
TEST_F(F32Test, FriendlyName) {
F32 f;
EXPECT_EQ(f.FriendlyName(Symbols()), "f32");
}
} // namespace
} // namespace sem
} // namespace tint

View File

@@ -15,11 +15,11 @@
#include "src/sem/function.h"
#include "src/ast/function.h"
#include "src/sem/depth_texture_type.h"
#include "src/sem/multisampled_texture_type.h"
#include "src/sem/sampled_texture_type.h"
#include "src/sem/storage_texture_type.h"
#include "src/sem/variable.h"
#include "src/type/depth_texture_type.h"
#include "src/type/multisampled_texture_type.h"
#include "src/type/sampled_texture_type.h"
#include "src/type/storage_texture_type.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Function);

43
src/sem/i32_type.cc Normal file
View File

@@ -0,0 +1,43 @@
// 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/i32_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::I32);
namespace tint {
namespace sem {
I32::I32() = default;
I32::I32(I32&&) = default;
I32::~I32() = default;
std::string I32::type_name() const {
return "__i32";
}
std::string I32::FriendlyName(const SymbolTable&) const {
return "i32";
}
I32* I32::Clone(CloneContext* ctx) const {
return ctx->dst->create<I32>();
}
} // namespace sem
} // namespace tint

51
src/sem/i32_type.h Normal file
View File

@@ -0,0 +1,51 @@
// 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_I32_TYPE_H_
#define SRC_SEM_I32_TYPE_H_
#include <string>
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// A signed int 32 type.
class I32 : public Castable<I32, Type> {
public:
/// Constructor
I32();
/// Move constructor
I32(I32&&);
~I32() override;
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
I32* Clone(CloneContext* ctx) const override;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_I32_TYPE_H_

55
src/sem/i32_type_test.cc Normal file
View File

@@ -0,0 +1,55 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
using I32Test = TestHelper;
TEST_F(I32Test, Is) {
I32 i;
Type* ty = &i;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_TRUE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(I32Test, TypeName) {
I32 i;
EXPECT_EQ(i.type_name(), "__i32");
}
TEST_F(I32Test, FriendlyName) {
I32 i;
EXPECT_EQ(i.FriendlyName(Symbols()), "i32");
}
} // namespace
} // namespace sem
} // namespace tint

55
src/sem/matrix_type.cc Normal file
View File

@@ -0,0 +1,55 @@
// 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/matrix_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Matrix);
namespace tint {
namespace sem {
Matrix::Matrix(Type* subtype, uint32_t rows, uint32_t columns)
: subtype_(subtype), rows_(rows), columns_(columns) {
TINT_ASSERT(rows > 1);
TINT_ASSERT(rows < 5);
TINT_ASSERT(columns > 1);
TINT_ASSERT(columns < 5);
}
Matrix::Matrix(Matrix&&) = default;
Matrix::~Matrix() = default;
std::string Matrix::type_name() const {
return "__mat_" + std::to_string(rows_) + "_" + std::to_string(columns_) +
subtype_->type_name();
}
std::string Matrix::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out;
out << "mat" << columns_ << "x" << rows_ << "<"
<< subtype_->FriendlyName(symbols) << ">";
return out.str();
}
Matrix* Matrix::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto* ty = ctx->Clone(type());
return ctx->dst->create<Matrix>(ty, rows_, columns_);
}
} // namespace sem
} // namespace tint

66
src/sem/matrix_type.h Normal file
View File

@@ -0,0 +1,66 @@
// 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_MATRIX_TYPE_H_
#define SRC_SEM_MATRIX_TYPE_H_
#include <string>
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// A matrix type
class Matrix : public Castable<Matrix, Type> {
public:
/// Constructor
/// @param subtype type matrix type
/// @param rows the number of rows in the matrix
/// @param columns the number of columns in the matrix
Matrix(Type* subtype, uint32_t rows, uint32_t columns);
/// Move constructor
Matrix(Matrix&&);
~Matrix() override;
/// @returns the type of the matrix
Type* type() const { return subtype_; }
/// @returns the number of rows in the matrix
uint32_t rows() const { return rows_; }
/// @returns the number of columns in the matrix
uint32_t columns() const { return columns_; }
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Matrix* Clone(CloneContext* ctx) const override;
private:
Type* const subtype_;
uint32_t const rows_;
uint32_t const columns_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_MATRIX_TYPE_H_

View File

@@ -0,0 +1,65 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
using MatrixTest = TestHelper;
TEST_F(MatrixTest, Creation) {
I32 i32;
Matrix m{&i32, 2, 4};
EXPECT_EQ(m.type(), &i32);
EXPECT_EQ(m.rows(), 2u);
EXPECT_EQ(m.columns(), 4u);
}
TEST_F(MatrixTest, Is) {
I32 i32;
Matrix m{&i32, 2, 3};
Type* ty = &m;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_TRUE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(MatrixTest, TypeName) {
I32 i32;
Matrix m{&i32, 2, 3};
EXPECT_EQ(m.type_name(), "__mat_2_3__i32");
}
TEST_F(MatrixTest, FriendlyName) {
Matrix m{ty.i32(), 3, 2};
EXPECT_EQ(m.FriendlyName(Symbols()), "mat2x3<i32>");
}
} // namespace
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,54 @@
// 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/multisampled_texture_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::MultisampledTexture);
namespace tint {
namespace sem {
MultisampledTexture::MultisampledTexture(TextureDimension dim, Type* type)
: Base(dim), type_(type) {
TINT_ASSERT(type_);
}
MultisampledTexture::MultisampledTexture(MultisampledTexture&&) = default;
MultisampledTexture::~MultisampledTexture() = default;
std::string MultisampledTexture::type_name() const {
std::ostringstream out;
out << "__multisampled_texture_" << dim() << type_->type_name();
return out.str();
}
std::string MultisampledTexture::FriendlyName(
const SymbolTable& symbols) const {
std::ostringstream out;
out << "texture_multisampled_" << dim() << "<" << type_->FriendlyName(symbols)
<< ">";
return out.str();
}
MultisampledTexture* MultisampledTexture::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto* ty = ctx->Clone(type());
return ctx->dst->create<MultisampledTexture>(dim(), ty);
}
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,59 @@
// 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_MULTISAMPLED_TEXTURE_TYPE_H_
#define SRC_SEM_MULTISAMPLED_TEXTURE_TYPE_H_
#include <string>
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
/// A multisampled texture type.
class MultisampledTexture : public Castable<MultisampledTexture, Texture> {
public:
/// Constructor
/// @param dim the dimensionality of the texture
/// @param type the data type of the multisampled texture
MultisampledTexture(TextureDimension dim, Type* type);
/// Move constructor
MultisampledTexture(MultisampledTexture&&);
~MultisampledTexture() override;
/// @returns the subtype of the sampled texture
Type* type() const { return type_; }
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
MultisampledTexture* Clone(CloneContext* ctx) const override;
private:
Type* const type_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_MULTISAMPLED_TEXTURE_TYPE_H_

View File

@@ -0,0 +1,85 @@
// 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/multisampled_texture_type.h"
#include "src/sem/access_control_type.h"
#include "src/sem/depth_texture_type.h"
#include "src/sem/external_texture_type.h"
#include "src/sem/sampled_texture_type.h"
#include "src/sem/storage_texture_type.h"
#include "src/sem/test_helper.h"
namespace tint {
namespace sem {
namespace {
using MultisampledTextureTest = TestHelper;
TEST_F(MultisampledTextureTest, Is) {
F32 f32;
MultisampledTexture s(TextureDimension::kCube, &f32);
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(MultisampledTextureTest, IsTexture) {
F32 f32;
MultisampledTexture s(TextureDimension::kCube, &f32);
Texture* ty = &s;
EXPECT_FALSE(ty->Is<DepthTexture>());
EXPECT_FALSE(ty->Is<ExternalTexture>());
EXPECT_TRUE(ty->Is<MultisampledTexture>());
EXPECT_FALSE(ty->Is<SampledTexture>());
EXPECT_FALSE(ty->Is<StorageTexture>());
}
TEST_F(MultisampledTextureTest, Dim) {
F32 f32;
MultisampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.dim(), TextureDimension::k3d);
}
TEST_F(MultisampledTextureTest, Type) {
F32 f32;
MultisampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.type(), &f32);
}
TEST_F(MultisampledTextureTest, TypeName) {
F32 f32;
MultisampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.type_name(), "__multisampled_texture_3d__f32");
}
TEST_F(MultisampledTextureTest, FriendlyName) {
MultisampledTexture s(TextureDimension::k3d, ty.f32());
EXPECT_EQ(s.FriendlyName(Symbols()), "texture_multisampled_3d<f32>");
}
} // namespace
} // namespace sem
} // namespace tint

54
src/sem/pointer_type.cc Normal file
View File

@@ -0,0 +1,54 @@
// 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/pointer_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Pointer);
namespace tint {
namespace sem {
Pointer::Pointer(Type* subtype, ast::StorageClass storage_class)
: subtype_(subtype), storage_class_(storage_class) {}
std::string Pointer::type_name() const {
std::ostringstream out;
out << "__ptr_" << storage_class_ << subtype_->type_name();
return out.str();
}
std::string Pointer::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out;
out << "ptr<";
if (storage_class_ != ast::StorageClass::kNone) {
out << storage_class_ << ", ";
}
out << subtype_->FriendlyName(symbols) << ">";
return out.str();
}
Pointer::Pointer(Pointer&&) = default;
Pointer::~Pointer() = default;
Pointer* Pointer::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto* ty = ctx->Clone(type());
return ctx->dst->create<Pointer>(ty, storage_class_);
}
} // namespace sem
} // namespace tint

63
src/sem/pointer_type.h Normal file
View File

@@ -0,0 +1,63 @@
// 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_POINTER_TYPE_H_
#define SRC_SEM_POINTER_TYPE_H_
#include <string>
#include "src/ast/storage_class.h"
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// A pointer type.
class Pointer : public Castable<Pointer, Type> {
public:
/// Construtor
/// @param subtype the pointee type
/// @param storage_class the storage class of the pointer
explicit Pointer(Type* subtype, ast::StorageClass storage_class);
/// Move constructor
Pointer(Pointer&&);
~Pointer() override;
/// @returns the pointee type
Type* type() const { return subtype_; }
/// @returns the storage class of the pointer
ast::StorageClass storage_class() const { return storage_class_; }
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Pointer* Clone(CloneContext* ctx) const override;
private:
Type* const subtype_;
ast::StorageClass const storage_class_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_POINTER_TYPE_H_

View File

@@ -0,0 +1,69 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
using PointerTest = TestHelper;
TEST_F(PointerTest, Creation) {
I32 i32;
Pointer p{&i32, ast::StorageClass::kStorage};
EXPECT_EQ(p.type(), &i32);
EXPECT_EQ(p.storage_class(), ast::StorageClass::kStorage);
}
TEST_F(PointerTest, Is) {
I32 i32;
Pointer p{&i32, ast::StorageClass::kFunction};
Type* ty = &p;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_TRUE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(PointerTest, TypeName) {
I32 i32;
Pointer p{&i32, ast::StorageClass::kWorkgroup};
EXPECT_EQ(p.type_name(), "__ptr_workgroup__i32");
}
TEST_F(PointerTest, FriendlyNameWithStorageClass) {
Pointer p{ty.i32(), ast::StorageClass::kWorkgroup};
EXPECT_EQ(p.FriendlyName(Symbols()), "ptr<workgroup, i32>");
}
TEST_F(PointerTest, FriendlyNameWithoutStorageClass) {
Pointer p{ty.i32(), ast::StorageClass::kNone};
EXPECT_EQ(p.FriendlyName(Symbols()), "ptr<i32>");
}
} // namespace
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,52 @@
// 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/sampled_texture_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::SampledTexture);
namespace tint {
namespace sem {
SampledTexture::SampledTexture(TextureDimension dim, Type* type)
: Base(dim), type_(type) {
TINT_ASSERT(type_);
}
SampledTexture::SampledTexture(SampledTexture&&) = default;
SampledTexture::~SampledTexture() = default;
std::string SampledTexture::type_name() const {
std::ostringstream out;
out << "__sampled_texture_" << dim() << type_->type_name();
return out.str();
}
std::string SampledTexture::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out;
out << "texture_" << dim() << "<" << type_->FriendlyName(symbols) << ">";
return out.str();
}
SampledTexture* SampledTexture::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto* ty = ctx->Clone(type());
return ctx->dst->create<SampledTexture>(dim(), ty);
}
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,59 @@
// 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_SAMPLED_TEXTURE_TYPE_H_
#define SRC_SEM_SAMPLED_TEXTURE_TYPE_H_
#include <string>
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
/// A sampled texture type.
class SampledTexture : public Castable<SampledTexture, Texture> {
public:
/// Constructor
/// @param dim the dimensionality of the texture
/// @param type the data type of the sampled texture
SampledTexture(TextureDimension dim, Type* type);
/// Move constructor
SampledTexture(SampledTexture&&);
~SampledTexture() override;
/// @returns the subtype of the sampled texture
Type* type() const { return type_; }
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
SampledTexture* Clone(CloneContext* ctx) const override;
private:
Type* const type_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_SAMPLED_TEXTURE_TYPE_H_

View File

@@ -0,0 +1,83 @@
// 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/sampled_texture_type.h"
#include "src/sem/access_control_type.h"
#include "src/sem/depth_texture_type.h"
#include "src/sem/external_texture_type.h"
#include "src/sem/storage_texture_type.h"
#include "src/sem/test_helper.h"
namespace tint {
namespace sem {
namespace {
using SampledTextureTest = TestHelper;
TEST_F(SampledTextureTest, Is) {
F32 f32;
SampledTexture s(TextureDimension::kCube, &f32);
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(SampledTextureTest, IsTexture) {
F32 f32;
SampledTexture s(TextureDimension::kCube, &f32);
Texture* ty = &s;
EXPECT_FALSE(ty->Is<DepthTexture>());
EXPECT_FALSE(ty->Is<ExternalTexture>());
EXPECT_TRUE(ty->Is<SampledTexture>());
EXPECT_FALSE(ty->Is<StorageTexture>());
}
TEST_F(SampledTextureTest, Dim) {
F32 f32;
SampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.dim(), TextureDimension::k3d);
}
TEST_F(SampledTextureTest, Type) {
F32 f32;
SampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.type(), &f32);
}
TEST_F(SampledTextureTest, TypeName) {
F32 f32;
SampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.type_name(), "__sampled_texture_3d__f32");
}
TEST_F(SampledTextureTest, FriendlyName) {
SampledTexture s(TextureDimension::k3d, ty.f32());
EXPECT_EQ(s.FriendlyName(Symbols()), "texture_3d<f32>");
}
} // namespace
} // namespace sem
} // namespace tint

56
src/sem/sampler_type.cc Normal file
View File

@@ -0,0 +1,56 @@
// 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/sampler_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Sampler);
namespace tint {
namespace sem {
std::ostream& operator<<(std::ostream& out, SamplerKind kind) {
switch (kind) {
case SamplerKind::kSampler:
out << "sampler";
break;
case SamplerKind::kComparisonSampler:
out << "comparison_sampler";
break;
}
return out;
}
Sampler::Sampler(SamplerKind kind) : kind_(kind) {}
Sampler::Sampler(Sampler&&) = default;
Sampler::~Sampler() = default;
std::string Sampler::type_name() const {
return std::string("__sampler_") +
(kind_ == SamplerKind::kSampler ? "sampler" : "comparison");
}
std::string Sampler::FriendlyName(const SymbolTable&) const {
return kind_ == SamplerKind::kSampler ? "sampler" : "sampler_comparison";
}
Sampler* Sampler::Clone(CloneContext* ctx) const {
return ctx->dst->create<Sampler>(kind_);
}
} // namespace sem
} // namespace tint

70
src/sem/sampler_type.h Normal file
View File

@@ -0,0 +1,70 @@
// 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_SAMPLER_TYPE_H_
#define SRC_SEM_SAMPLER_TYPE_H_
#include <string>
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// The different kinds of samplers
enum class SamplerKind {
/// A regular sampler
kSampler,
/// A comparison sampler
kComparisonSampler
};
std::ostream& operator<<(std::ostream& out, SamplerKind kind);
/// A sampler type.
class Sampler : public Castable<Sampler, Type> {
public:
/// Constructor
/// @param kind the kind of sampler
explicit Sampler(SamplerKind kind);
/// Move constructor
Sampler(Sampler&&);
~Sampler() override;
/// @returns the sampler type
SamplerKind kind() const { return kind_; }
/// @returns true if this is a comparison sampler
bool IsComparison() const { return kind_ == SamplerKind::kComparisonSampler; }
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Sampler* Clone(CloneContext* ctx) const override;
private:
SamplerKind const kind_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_SAMPLER_TYPE_H_

View File

@@ -0,0 +1,76 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
using SamplerTest = TestHelper;
TEST_F(SamplerTest, Creation) {
Sampler s{SamplerKind::kSampler};
EXPECT_EQ(s.kind(), SamplerKind::kSampler);
}
TEST_F(SamplerTest, Creation_ComparisonSampler) {
Sampler s{SamplerKind::kComparisonSampler};
EXPECT_EQ(s.kind(), SamplerKind::kComparisonSampler);
EXPECT_TRUE(s.IsComparison());
}
TEST_F(SamplerTest, Is) {
Sampler s{SamplerKind::kSampler};
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_TRUE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(SamplerTest, TypeName_Sampler) {
Sampler s{SamplerKind::kSampler};
EXPECT_EQ(s.type_name(), "__sampler_sampler");
}
TEST_F(SamplerTest, TypeName_Comparison) {
Sampler s{SamplerKind::kComparisonSampler};
EXPECT_EQ(s.type_name(), "__sampler_comparison");
}
TEST_F(SamplerTest, FriendlyNameSampler) {
Sampler s{SamplerKind::kSampler};
EXPECT_EQ(s.FriendlyName(Symbols()), "sampler");
}
TEST_F(SamplerTest, FriendlyNameComparisonSampler) {
Sampler s{SamplerKind::kComparisonSampler};
EXPECT_EQ(s.FriendlyName(Symbols()), "sampler_comparison");
}
} // namespace
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,222 @@
// 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/storage_texture_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::StorageTexture);
namespace tint {
namespace sem {
// Note, these names match the names in the WGSL spec. This behaviour is used
// in the WGSL writer to emit the texture format names.
std::ostream& operator<<(std::ostream& out, ImageFormat format) {
switch (format) {
case ImageFormat::kNone:
out << "none";
break;
case ImageFormat::kR8Unorm:
out << "r8unorm";
break;
case ImageFormat::kR8Snorm:
out << "r8snorm";
break;
case ImageFormat::kR8Uint:
out << "r8uint";
break;
case ImageFormat::kR8Sint:
out << "r8sint";
break;
case ImageFormat::kR16Uint:
out << "r16uint";
break;
case ImageFormat::kR16Sint:
out << "r16sint";
break;
case ImageFormat::kR16Float:
out << "r16float";
break;
case ImageFormat::kRg8Unorm:
out << "rg8unorm";
break;
case ImageFormat::kRg8Snorm:
out << "rg8snorm";
break;
case ImageFormat::kRg8Uint:
out << "rg8uint";
break;
case ImageFormat::kRg8Sint:
out << "rg8sint";
break;
case ImageFormat::kR32Uint:
out << "r32uint";
break;
case ImageFormat::kR32Sint:
out << "r32sint";
break;
case ImageFormat::kR32Float:
out << "r32float";
break;
case ImageFormat::kRg16Uint:
out << "rg16uint";
break;
case ImageFormat::kRg16Sint:
out << "rg16sint";
break;
case ImageFormat::kRg16Float:
out << "rg16float";
break;
case ImageFormat::kRgba8Unorm:
out << "rgba8unorm";
break;
case ImageFormat::kRgba8UnormSrgb:
out << "rgba8unorm_srgb";
break;
case ImageFormat::kRgba8Snorm:
out << "rgba8snorm";
break;
case ImageFormat::kRgba8Uint:
out << "rgba8uint";
break;
case ImageFormat::kRgba8Sint:
out << "rgba8sint";
break;
case ImageFormat::kBgra8Unorm:
out << "bgra8unorm";
break;
case ImageFormat::kBgra8UnormSrgb:
out << "bgra8unorm_srgb";
break;
case ImageFormat::kRgb10A2Unorm:
out << "rgb10a2unorm";
break;
case ImageFormat::kRg11B10Float:
out << "rg11b10float";
break;
case ImageFormat::kRg32Uint:
out << "rg32uint";
break;
case ImageFormat::kRg32Sint:
out << "rg32sint";
break;
case ImageFormat::kRg32Float:
out << "rg32float";
break;
case ImageFormat::kRgba16Uint:
out << "rgba16uint";
break;
case ImageFormat::kRgba16Sint:
out << "rgba16sint";
break;
case ImageFormat::kRgba16Float:
out << "rgba16float";
break;
case ImageFormat::kRgba32Uint:
out << "rgba32uint";
break;
case ImageFormat::kRgba32Sint:
out << "rgba32sint";
break;
case ImageFormat::kRgba32Float:
out << "rgba32float";
break;
}
return out;
}
StorageTexture::StorageTexture(TextureDimension dim,
ImageFormat format,
sem::Type* subtype)
: Base(dim), image_format_(format), subtype_(subtype) {}
StorageTexture::StorageTexture(StorageTexture&&) = default;
StorageTexture::~StorageTexture() = default;
std::string StorageTexture::type_name() const {
std::ostringstream out;
out << "__storage_texture_" << dim() << "_" << image_format_;
return out.str();
}
std::string StorageTexture::FriendlyName(const SymbolTable&) const {
std::ostringstream out;
out << "texture_storage_" << dim() << "<" << image_format_ << ">";
return out.str();
}
StorageTexture* StorageTexture::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto* ty = ctx->Clone(type());
return ctx->dst->create<StorageTexture>(dim(), image_format_, ty);
}
sem::Type* StorageTexture::SubtypeFor(sem::ImageFormat format,
sem::Manager& type_mgr) {
switch (format) {
case sem::ImageFormat::kR8Uint:
case sem::ImageFormat::kR16Uint:
case sem::ImageFormat::kRg8Uint:
case sem::ImageFormat::kR32Uint:
case sem::ImageFormat::kRg16Uint:
case sem::ImageFormat::kRgba8Uint:
case sem::ImageFormat::kRg32Uint:
case sem::ImageFormat::kRgba16Uint:
case sem::ImageFormat::kRgba32Uint: {
return type_mgr.Get<sem::U32>();
}
case sem::ImageFormat::kR8Sint:
case sem::ImageFormat::kR16Sint:
case sem::ImageFormat::kRg8Sint:
case sem::ImageFormat::kR32Sint:
case sem::ImageFormat::kRg16Sint:
case sem::ImageFormat::kRgba8Sint:
case sem::ImageFormat::kRg32Sint:
case sem::ImageFormat::kRgba16Sint:
case sem::ImageFormat::kRgba32Sint: {
return type_mgr.Get<sem::I32>();
}
case sem::ImageFormat::kR8Unorm:
case sem::ImageFormat::kRg8Unorm:
case sem::ImageFormat::kRgba8Unorm:
case sem::ImageFormat::kRgba8UnormSrgb:
case sem::ImageFormat::kBgra8Unorm:
case sem::ImageFormat::kBgra8UnormSrgb:
case sem::ImageFormat::kRgb10A2Unorm:
case sem::ImageFormat::kR8Snorm:
case sem::ImageFormat::kRg8Snorm:
case sem::ImageFormat::kRgba8Snorm:
case sem::ImageFormat::kR16Float:
case sem::ImageFormat::kR32Float:
case sem::ImageFormat::kRg16Float:
case sem::ImageFormat::kRg11B10Float:
case sem::ImageFormat::kRg32Float:
case sem::ImageFormat::kRgba16Float:
case sem::ImageFormat::kRgba32Float: {
return type_mgr.Get<sem::F32>();
}
case sem::ImageFormat::kNone:
break;
}
return nullptr;
}
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,113 @@
// 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_STORAGE_TEXTURE_TYPE_H_
#define SRC_SEM_STORAGE_TEXTURE_TYPE_H_
#include <string>
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
class Manager;
/// The image format in the storage texture
enum class ImageFormat {
kNone = -1,
kR8Unorm,
kR8Snorm,
kR8Uint,
kR8Sint,
kR16Uint,
kR16Sint,
kR16Float,
kRg8Unorm,
kRg8Snorm,
kRg8Uint,
kRg8Sint,
kR32Uint,
kR32Sint,
kR32Float,
kRg16Uint,
kRg16Sint,
kRg16Float,
kRgba8Unorm,
kRgba8UnormSrgb,
kRgba8Snorm,
kRgba8Uint,
kRgba8Sint,
kBgra8Unorm,
kBgra8UnormSrgb,
kRgb10A2Unorm,
kRg11B10Float,
kRg32Uint,
kRg32Sint,
kRg32Float,
kRgba16Uint,
kRgba16Sint,
kRgba16Float,
kRgba32Uint,
kRgba32Sint,
kRgba32Float,
};
std::ostream& operator<<(std::ostream& out, ImageFormat dim);
/// A storage texture type.
class StorageTexture : public Castable<StorageTexture, Texture> {
public:
/// Constructor
/// @param dim the dimensionality of the texture
/// @param format the image format of the texture
/// @param subtype the storage subtype. Use SubtypeFor() to calculate this.
StorageTexture(TextureDimension dim, ImageFormat format, sem::Type* subtype);
/// Move constructor
StorageTexture(StorageTexture&&);
~StorageTexture() override;
/// @returns the storage subtype
Type* type() const { return subtype_; }
/// @returns the image format
ImageFormat image_format() const { return image_format_; }
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
StorageTexture* Clone(CloneContext* ctx) const override;
/// @param format the storage texture image format
/// @param type_mgr the sem::Manager used to build the returned type
/// @returns the storage texture subtype for the given ImageFormat
static sem::Type* SubtypeFor(sem::ImageFormat format, sem::Manager& type_mgr);
private:
ImageFormat const image_format_;
Type* const subtype_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_STORAGE_TEXTURE_TYPE_H_

View File

@@ -0,0 +1,139 @@
// 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/storage_texture_type.h"
#include "src/sem/access_control_type.h"
#include "src/sem/depth_texture_type.h"
#include "src/sem/external_texture_type.h"
#include "src/sem/sampled_texture_type.h"
#include "src/sem/test_helper.h"
namespace tint {
namespace sem {
namespace {
using StorageTextureTest = TestHelper;
TEST_F(StorageTextureTest, Is) {
auto* subtype =
StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, Types());
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
Type* ty = s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(StorageTextureTest, IsTexture) {
auto* subtype =
StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, Types());
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
Texture* ty = s;
EXPECT_FALSE(ty->Is<DepthTexture>());
EXPECT_FALSE(ty->Is<ExternalTexture>());
EXPECT_FALSE(ty->Is<SampledTexture>());
EXPECT_TRUE(ty->Is<StorageTexture>());
}
TEST_F(StorageTextureTest, Dim) {
auto* subtype =
StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, Types());
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
EXPECT_EQ(s->dim(), TextureDimension::k2dArray);
}
TEST_F(StorageTextureTest, Format) {
auto* subtype =
StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, Types());
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
EXPECT_EQ(s->image_format(), ImageFormat::kRgba32Float);
}
TEST_F(StorageTextureTest, TypeName) {
auto* subtype =
StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, Types());
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
EXPECT_EQ(s->type_name(), "__storage_texture_2d_array_rgba32float");
}
TEST_F(StorageTextureTest, FriendlyName) {
auto* subtype =
StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, Types());
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
EXPECT_EQ(s->FriendlyName(Symbols()),
"texture_storage_2d_array<rgba32float>");
}
TEST_F(StorageTextureTest, F32) {
auto* subtype =
sem::StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, Types());
Type* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
auto program = Build();
ASSERT_TRUE(program.IsValid()) << program.Diagnostics().str();
ASSERT_TRUE(s->Is<Texture>());
ASSERT_TRUE(s->Is<StorageTexture>());
EXPECT_TRUE(s->As<StorageTexture>()->type()->Is<F32>());
}
TEST_F(StorageTextureTest, U32) {
auto* subtype =
sem::StorageTexture::SubtypeFor(ImageFormat::kRg32Uint, Types());
Type* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRg32Uint, subtype);
auto program = Build();
ASSERT_TRUE(program.IsValid()) << program.Diagnostics().str();
ASSERT_TRUE(s->Is<Texture>());
ASSERT_TRUE(s->Is<StorageTexture>());
EXPECT_TRUE(s->As<StorageTexture>()->type()->Is<U32>());
}
TEST_F(StorageTextureTest, I32) {
auto* subtype =
sem::StorageTexture::SubtypeFor(ImageFormat::kRgba32Sint, Types());
Type* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Sint, subtype);
auto program = Build();
ASSERT_TRUE(program.IsValid()) << program.Diagnostics().str();
ASSERT_TRUE(s->Is<Texture>());
ASSERT_TRUE(s->Is<StorageTexture>());
EXPECT_TRUE(s->As<StorageTexture>()->type()->Is<I32>());
}
} // namespace
} // namespace sem
} // namespace tint

49
src/sem/struct_type.cc Normal file
View File

@@ -0,0 +1,49 @@
// 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/struct_type.h"
#include <cmath>
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::StructType);
namespace tint {
namespace sem {
StructType::StructType(const Symbol& sym, ast::Struct* impl)
: symbol_(sym), struct_(impl) {}
StructType::StructType(StructType&&) = default;
StructType::~StructType() = default;
std::string StructType::type_name() const {
return "__struct_" + symbol_.to_str();
}
std::string StructType::FriendlyName(const SymbolTable& symbols) const {
return symbols.NameFor(symbol_);
}
StructType* StructType::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto sym = ctx->Clone(symbol());
auto* str = ctx->Clone(impl());
return ctx->dst->create<StructType>(sym, str);
}
} // namespace sem
} // namespace tint

69
src/sem/struct_type.h Normal file
View File

@@ -0,0 +1,69 @@
// 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_STRUCT_TYPE_H_
#define SRC_SEM_STRUCT_TYPE_H_
#include <string>
#include "src/ast/struct.h"
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// A structure type
class StructType : public Castable<StructType, Type> {
public:
/// Constructor
/// @param sym the symbol representing the struct
/// @param impl the struct data
StructType(const Symbol& sym, ast::Struct* impl);
/// Move constructor
StructType(StructType&&);
~StructType() override;
/// @returns the struct symbol
const Symbol& symbol() const { return symbol_; }
/// @returns true if the struct has a block decoration
bool IsBlockDecorated() const { return struct_->IsBlockDecorated(); }
/// @returns the struct
ast::Struct* impl() const { return struct_; }
/// @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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
StructType* Clone(CloneContext* ctx) const override;
private:
Symbol const symbol_;
ast::Struct* const struct_;
uint64_t LargestMemberBaseAlignment(MemoryLayout mem_layout) const;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_STRUCT_TYPE_H_

View File

@@ -0,0 +1,69 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
using StructTypeTest = TestHelper;
TEST_F(StructTypeTest, Creation) {
auto* impl =
create<ast::Struct>(ast::StructMemberList{}, ast::DecorationList{});
auto* ptr = impl;
auto* s = ty.struct_("S", impl);
EXPECT_EQ(s->impl(), ptr);
}
TEST_F(StructTypeTest, Is) {
auto* impl =
create<ast::Struct>(ast::StructMemberList{}, ast::DecorationList{});
auto* s = ty.struct_("S", impl);
sem::Type* ty = s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_TRUE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(StructTypeTest, TypeName) {
auto* impl =
create<ast::Struct>(ast::StructMemberList{}, ast::DecorationList{});
auto* s = ty.struct_("my_struct", impl);
EXPECT_EQ(s->type_name(), "__struct_$1");
}
TEST_F(StructTypeTest, FriendlyName) {
auto* impl =
create<ast::Struct>(ast::StructMemberList{}, ast::DecorationList{});
auto* s = ty.struct_("my_struct", impl);
EXPECT_EQ(s->FriendlyName(Symbols()), "my_struct");
}
} // namespace
} // namespace sem
} // namespace tint

49
src/sem/test_helper.h Normal file
View File

@@ -0,0 +1,49 @@
// 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_TEST_HELPER_H_
#define SRC_SEM_TEST_HELPER_H_
#include <utility>
#include "gtest/gtest.h"
#include "src/program_builder.h"
namespace tint {
namespace sem {
/// Helper class for testing
template <typename BASE>
class TestHelperBase : public BASE, public ProgramBuilder {
public:
/// Builds and returns the program. Must only be called once per test
/// @return the built program
Program Build() {
diag::Formatter formatter;
[&]() {
ASSERT_TRUE(IsValid()) << "Builder program is not valid\n"
<< formatter.format(Diagnostics());
}();
return Program(std::move(*this));
}
};
using TestHelper = TestHelperBase<testing::Test>;
template <typename T>
using TestParamHelper = TestHelperBase<testing::TestWithParam<T>>;
} // namespace sem
} // namespace tint
#endif // SRC_SEM_TEST_HELPER_H_

88
src/sem/texture_type.cc Normal file
View File

@@ -0,0 +1,88 @@
// 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/texture_type.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Texture);
namespace tint {
namespace sem {
std::ostream& operator<<(std::ostream& out, TextureDimension dim) {
switch (dim) {
case TextureDimension::kNone:
out << "None";
break;
case TextureDimension::k1d:
out << "1d";
break;
case TextureDimension::k2d:
out << "2d";
break;
case TextureDimension::k2dArray:
out << "2d_array";
break;
case TextureDimension::k3d:
out << "3d";
break;
case TextureDimension::kCube:
out << "cube";
break;
case TextureDimension::kCubeArray:
out << "cube_array";
break;
}
return out;
}
bool IsTextureArray(TextureDimension dim) {
switch (dim) {
case TextureDimension::k2dArray:
case TextureDimension::kCubeArray:
return true;
case TextureDimension::k2d:
case TextureDimension::kNone:
case TextureDimension::k1d:
case TextureDimension::k3d:
case TextureDimension::kCube:
return false;
}
return false;
}
int NumCoordinateAxes(TextureDimension dim) {
switch (dim) {
case TextureDimension::kNone:
return 0;
case TextureDimension::k1d:
return 1;
case TextureDimension::k2d:
case TextureDimension::k2dArray:
return 2;
case TextureDimension::k3d:
case TextureDimension::kCube:
case TextureDimension::kCubeArray:
return 3;
}
return 0;
}
Texture::Texture(TextureDimension dim) : dim_(dim) {}
Texture::Texture(Texture&&) = default;
Texture::~Texture() = default;
} // namespace sem
} // namespace tint

75
src/sem/texture_type.h Normal file
View File

@@ -0,0 +1,75 @@
// 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_TEXTURE_TYPE_H_
#define SRC_SEM_TEXTURE_TYPE_H_
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// The dimensionality of the texture
enum class TextureDimension {
/// Invalid texture
kNone = -1,
/// 1 dimensional texture
k1d,
/// 2 dimensional texture
k2d,
/// 2 dimensional array texture
k2dArray,
/// 3 dimensional texture
k3d,
/// cube texture
kCube,
/// cube array texture
kCubeArray,
};
std::ostream& operator<<(std::ostream& out, TextureDimension dim);
/// @param dim the TextureDimension to query
/// @return true if the given TextureDimension is an array texture
bool IsTextureArray(TextureDimension dim);
/// Returns the number of axes in the coordinate for a dimensionality.
/// None -> 0
/// 1D -> 1
/// 2D, 2DArray -> 2
/// 3D, Cube, CubeArray -> 3
/// @param dim the TextureDimension to query
/// @return number of dimensions in a coordinate for the dimensionality
int NumCoordinateAxes(TextureDimension dim);
/// A texture type.
class Texture : public Castable<Texture, Type> {
public:
/// Constructor
/// @param dim the dimensionality of the texture
explicit Texture(TextureDimension dim);
/// Move constructor
Texture(Texture&&);
~Texture() override;
/// @returns the texture dimension
TextureDimension dim() const { return dim_; }
private:
TextureDimension const dim_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_TEXTURE_TYPE_H_

View File

@@ -0,0 +1,47 @@
// Copyright 2021 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/texture_type.h"
#include "src/sem/test_helper.h"
namespace tint {
namespace sem {
namespace {
using TextureTypeTest = TestHelper;
TEST_F(TextureTypeTest, IsTextureArray) {
EXPECT_EQ(false, IsTextureArray(TextureDimension::kNone));
EXPECT_EQ(false, IsTextureArray(TextureDimension::k1d));
EXPECT_EQ(false, IsTextureArray(TextureDimension::k2d));
EXPECT_EQ(true, IsTextureArray(TextureDimension::k2dArray));
EXPECT_EQ(false, IsTextureArray(TextureDimension::k3d));
EXPECT_EQ(false, IsTextureArray(TextureDimension::kCube));
EXPECT_EQ(true, IsTextureArray(TextureDimension::kCubeArray));
}
TEST_F(TextureTypeTest, NumCoordinateAxes) {
EXPECT_EQ(0, NumCoordinateAxes(TextureDimension::kNone));
EXPECT_EQ(1, NumCoordinateAxes(TextureDimension::k1d));
EXPECT_EQ(2, NumCoordinateAxes(TextureDimension::k2d));
EXPECT_EQ(2, NumCoordinateAxes(TextureDimension::k2dArray));
EXPECT_EQ(3, NumCoordinateAxes(TextureDimension::k3d));
EXPECT_EQ(3, NumCoordinateAxes(TextureDimension::kCube));
EXPECT_EQ(3, NumCoordinateAxes(TextureDimension::kCubeArray));
}
} // namespace
} // namespace sem
} // namespace tint

136
src/sem/type.cc Normal file
View File

@@ -0,0 +1,136 @@
// 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/type.h"
#include "src/sem/access_control_type.h"
#include "src/sem/alias_type.h"
#include "src/sem/bool_type.h"
#include "src/sem/f32_type.h"
#include "src/sem/i32_type.h"
#include "src/sem/matrix_type.h"
#include "src/sem/pointer_type.h"
#include "src/sem/sampler_type.h"
#include "src/sem/texture_type.h"
#include "src/sem/u32_type.h"
#include "src/sem/vector_type.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Type);
namespace tint {
namespace sem {
Type::Type() = default;
Type::Type(Type&&) = default;
Type::~Type() = default;
Type* Type::UnwrapPtrIfNeeded() {
if (auto* ptr = As<sem::Pointer>()) {
return ptr->type();
}
return this;
}
Type* Type::UnwrapAliasIfNeeded() {
Type* unwrapped = this;
while (auto* ptr = unwrapped->As<sem::Alias>()) {
unwrapped = ptr->type();
}
return unwrapped;
}
Type* Type::UnwrapIfNeeded() {
auto* where = this;
while (true) {
if (auto* alias = where->As<sem::Alias>()) {
where = alias->type();
} else if (auto* access = where->As<sem::AccessControl>()) {
where = access->type();
} else {
break;
}
}
return where;
}
Type* Type::UnwrapAll() {
return UnwrapIfNeeded()->UnwrapPtrIfNeeded()->UnwrapIfNeeded();
}
bool Type::is_scalar() const {
return IsAnyOf<F32, U32, I32, Bool>();
}
bool Type::is_float_scalar() const {
return Is<F32>();
}
bool Type::is_float_matrix() const {
return Is<Matrix>(
[](const Matrix* m) { return m->type()->is_float_scalar(); });
}
bool Type::is_float_vector() const {
return Is<Vector>(
[](const Vector* v) { return v->type()->is_float_scalar(); });
}
bool Type::is_float_scalar_or_vector() const {
return is_float_scalar() || is_float_vector();
}
bool Type::is_float_scalar_or_vector_or_matrix() const {
return is_float_scalar() || is_float_vector() || is_float_matrix();
}
bool Type::is_integer_scalar() const {
return IsAnyOf<U32, I32>();
}
bool Type::is_unsigned_integer_vector() const {
return Is<Vector>([](const Vector* v) { return v->type()->Is<U32>(); });
}
bool Type::is_signed_integer_vector() const {
return Is<Vector>([](const Vector* v) { return v->type()->Is<I32>(); });
}
bool Type::is_unsigned_scalar_or_vector() const {
return Is<U32>() || is_unsigned_integer_vector();
}
bool Type::is_signed_scalar_or_vector() const {
return Is<I32>() || is_signed_integer_vector();
}
bool Type::is_integer_scalar_or_vector() const {
return is_unsigned_scalar_or_vector() || is_signed_scalar_or_vector();
}
bool Type::is_bool_vector() const {
return Is<Vector>([](const Vector* v) { return v->type()->Is<Bool>(); });
}
bool Type::is_bool_scalar_or_vector() const {
return Is<Bool>() || is_bool_vector();
}
bool Type::is_handle() const {
return IsAnyOf<Sampler, Texture>();
}
} // namespace sem
} // namespace tint

115
src/sem/type.h Normal file
View File

@@ -0,0 +1,115 @@
// 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_TYPE_H_
#define SRC_SEM_TYPE_H_
#include <string>
#include "src/clone_context.h"
namespace tint {
// Forward declarations
class ProgramBuilder;
class SymbolTable;
namespace sem {
/// Supported memory layouts for calculating sizes
enum class MemoryLayout { kUniformBuffer, kStorageBuffer };
/// Base class for a type in the system
class Type : public Castable<Type, ShareableCloneable> {
public:
/// Move constructor
Type(Type&&);
~Type() override;
/// @returns the name for this type. The type name is unique over all types.
virtual std::string type_name() const = 0;
/// @param symbols the program's symbol table
/// @returns the name for this type that closely resembles how it would be
/// declared in WGSL.
virtual std::string FriendlyName(const SymbolTable& symbols) const = 0;
/// @returns the pointee type if this is a pointer, `this` otherwise
Type* UnwrapPtrIfNeeded();
/// @returns the most deeply nested aliased type if this is an alias, `this`
/// otherwise
Type* UnwrapAliasIfNeeded();
/// 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.
Type* UnwrapIfNeeded();
/// 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 true if this type is a scalar
bool is_scalar() const;
/// @returns true if this type is a float scalar
bool is_float_scalar() const;
/// @returns true if this type is a float matrix
bool is_float_matrix() const;
/// @returns true if this type is a float vector
bool is_float_vector() const;
/// @returns true if this type is a float scalar or vector
bool is_float_scalar_or_vector() const;
/// @returns true if this type is a float scalar or vector or matrix
bool is_float_scalar_or_vector_or_matrix() const;
/// @returns true if this type is an integer scalar
bool is_integer_scalar() const;
/// @returns true if this type is a signed integer vector
bool is_signed_integer_vector() const;
/// @returns true if this type is an unsigned vector
bool is_unsigned_integer_vector() const;
/// @returns true if this type is an unsigned scalar or vector
bool is_unsigned_scalar_or_vector() const;
/// @returns true if this type is a signed scalar or vector
bool is_signed_scalar_or_vector() const;
/// @returns true if this type is an integer scalar or vector
bool is_integer_scalar_or_vector() const;
/// @returns true if this type is a boolean vector
bool is_bool_vector() const;
/// @returns true if this type is boolean scalar or vector
bool is_bool_scalar_or_vector() const;
/// @returns true if this type is a handle type
bool is_handle() const;
protected:
Type();
};
/// @returns the ProgramID of the given type.
inline ProgramID ProgramIDOf(const Type*) {
/// TODO(crbug.com/tint/724): Actually implement this once we split the `type`
/// namespace into ast::Type and sem::Type.
return ProgramID();
}
} // namespace sem
} // namespace tint
#endif // SRC_SEM_TYPE_H_

26
src/sem/type_manager.cc Normal file
View File

@@ -0,0 +1,26 @@
// 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/type_manager.h"
namespace tint {
namespace sem {
Manager::Manager() = default;
Manager::Manager(Manager&&) = default;
Manager& Manager::operator=(Manager&& rhs) = default;
Manager::~Manager() = default;
} // namespace sem
} // namespace tint

100
src/sem/type_manager.h Normal file
View File

@@ -0,0 +1,100 @@
// 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_TYPE_MANAGER_H_
#define SRC_SEM_TYPE_MANAGER_H_
#include <string>
#include <unordered_map>
#include <utility>
#include "src/block_allocator.h"
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// The type manager holds all the pointers to the known types.
class Manager {
public:
/// Iterator is the type returned by begin() and end()
using Iterator = BlockAllocator<sem::Type>::ConstIterator;
/// Constructor
Manager();
/// Move constructor
Manager(Manager&&);
/// Move assignment operator
/// @param rhs the Manager to move
/// @return this Manager
Manager& operator=(Manager&& rhs);
/// Destructor
~Manager();
/// Get the given type `T` from the type manager
/// @param args the arguments to pass to the type constructor
/// @return the pointer to the registered type
template <typename T, typename... ARGS>
T* Get(ARGS&&... args) {
// Note: We do not use std::forward here, as we may need to use the
// arguments again for the call to Create<T>() below.
auto name = T(args...).type_name();
auto it = by_name_.find(name);
if (it != by_name_.end()) {
return static_cast<T*>(it->second);
}
auto* type = types_.Create<T>(std::forward<ARGS>(args)...);
by_name_.emplace(name, type);
return type;
}
/// Wrap returns a new Manager created with the types of `inner`.
/// The Manager returned by Wrap is intended to temporarily extend the types
/// of an existing immutable Manager.
/// As the copied types are owned by `inner`, `inner` must not be destructed
/// or assigned while using the returned Manager.
/// TODO(bclayton) - Evaluate whether there are safer alternatives to this
/// function. See crbug.com/tint/460.
/// @param inner the immutable Manager to extend
/// @return the Manager that wraps `inner`
static Manager Wrap(const Manager& inner) {
Manager out;
out.by_name_ = inner.by_name_;
return out;
}
/// Returns the type map
/// @returns the mapping from name string to type.
const std::unordered_map<std::string, sem::Type*>& types() const {
return by_name_;
}
/// @returns an iterator to the beginning of the types
Iterator begin() const { return types_.Objects().begin(); }
/// @returns an iterator to the end of the types
Iterator end() const { return types_.Objects().end(); }
private:
std::unordered_map<std::string, sem::Type*> by_name_;
BlockAllocator<sem::Type> types_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_TYPE_MANAGER_H_

View File

@@ -0,0 +1,83 @@
// 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/type_manager.h"
#include "gtest/gtest.h"
#include "src/sem/i32_type.h"
#include "src/sem/u32_type.h"
namespace tint {
namespace sem {
namespace {
template <typename T>
size_t count(const T& range_loopable) {
size_t n = 0;
for (auto it : range_loopable) {
(void)it;
n++;
}
return n;
}
using TypeManagerTest = testing::Test;
TEST_F(TypeManagerTest, GetUnregistered) {
Manager tm;
auto* t = tm.Get<I32>();
ASSERT_NE(t, nullptr);
EXPECT_TRUE(t->Is<I32>());
}
TEST_F(TypeManagerTest, GetSameTypeReturnsSamePtr) {
Manager tm;
auto* t = tm.Get<I32>();
ASSERT_NE(t, nullptr);
EXPECT_TRUE(t->Is<I32>());
auto* t2 = tm.Get<I32>();
EXPECT_EQ(t, t2);
}
TEST_F(TypeManagerTest, GetDifferentTypeReturnsDifferentPtr) {
Manager tm;
Type* t = tm.Get<I32>();
ASSERT_NE(t, nullptr);
EXPECT_TRUE(t->Is<I32>());
Type* t2 = tm.Get<U32>();
ASSERT_NE(t2, nullptr);
EXPECT_NE(t, t2);
EXPECT_TRUE(t2->Is<U32>());
}
TEST_F(TypeManagerTest, WrapDoesntAffectInner) {
Manager inner;
Manager outer = Manager::Wrap(inner);
inner.Get<I32>();
EXPECT_EQ(count(inner), 1u);
EXPECT_EQ(count(outer), 0u);
outer.Get<U32>();
EXPECT_EQ(count(inner), 1u);
EXPECT_EQ(count(outer), 1u);
}
} // namespace
} // namespace sem
} // namespace tint

43
src/sem/u32_type.cc Normal file
View File

@@ -0,0 +1,43 @@
// 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/u32_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::U32);
namespace tint {
namespace sem {
U32::U32() = default;
U32::~U32() = default;
U32::U32(U32&&) = default;
std::string U32::type_name() const {
return "__u32";
}
std::string U32::FriendlyName(const SymbolTable&) const {
return "u32";
}
U32* U32::Clone(CloneContext* ctx) const {
return ctx->dst->create<U32>();
}
} // namespace sem
} // namespace tint

51
src/sem/u32_type.h Normal file
View File

@@ -0,0 +1,51 @@
// 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_U32_TYPE_H_
#define SRC_SEM_U32_TYPE_H_
#include <string>
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// A unsigned int 32 type.
class U32 : public Castable<U32, Type> {
public:
/// Constructor
U32();
/// Move constructor
U32(U32&&);
~U32() override;
/// @returns the name for th 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
U32* Clone(CloneContext* ctx) const override;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_U32_TYPE_H_

55
src/sem/u32_type_test.cc Normal file
View File

@@ -0,0 +1,55 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
using U32Test = TestHelper;
TEST_F(U32Test, Is) {
U32 u;
Type* ty = &u;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_TRUE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(U32Test, TypeName) {
U32 u;
EXPECT_EQ(u.type_name(), "__u32");
}
TEST_F(U32Test, FriendlyName) {
U32 u;
EXPECT_EQ(u.FriendlyName(Symbols()), "u32");
}
} // namespace
} // namespace sem
} // namespace tint

50
src/sem/vector_type.cc Normal file
View File

@@ -0,0 +1,50 @@
// 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/vector_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Vector);
namespace tint {
namespace sem {
Vector::Vector(Type* subtype, uint32_t size) : subtype_(subtype), size_(size) {
TINT_ASSERT(size_ > 1);
TINT_ASSERT(size_ < 5);
}
Vector::Vector(Vector&&) = default;
Vector::~Vector() = default;
std::string Vector::type_name() const {
return "__vec_" + std::to_string(size_) + subtype_->type_name();
}
std::string Vector::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out;
out << "vec" << size_ << "<" << subtype_->FriendlyName(symbols) << ">";
return out.str();
}
Vector* Vector::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto* ty = ctx->Clone(type());
return ctx->dst->create<Vector>(ty, size_);
}
} // namespace sem
} // namespace tint

62
src/sem/vector_type.h Normal file
View File

@@ -0,0 +1,62 @@
// 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_VECTOR_TYPE_H_
#define SRC_SEM_VECTOR_TYPE_H_
#include <string>
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// A vector type.
class Vector : public Castable<Vector, Type> {
public:
/// Constructor
/// @param subtype the vector element type
/// @param size the number of elements in the vector
Vector(Type* subtype, uint32_t size);
/// Move constructor
Vector(Vector&&);
~Vector() override;
/// @returns the type of the vector elements
Type* type() const { return subtype_; }
/// @returns the size of the vector
uint32_t size() const { return size_; }
/// @returns the name for th 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Vector* Clone(CloneContext* ctx) const override;
private:
Type* const subtype_;
uint32_t const size_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_VECTOR_TYPE_H_

View File

@@ -0,0 +1,64 @@
// 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/access_control_type.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
namespace tint {
namespace sem {
namespace {
using VectorTest = TestHelper;
TEST_F(VectorTest, Creation) {
I32 i32;
Vector v{&i32, 2};
EXPECT_EQ(v.type(), &i32);
EXPECT_EQ(v.size(), 2u);
}
TEST_F(VectorTest, Is) {
I32 i32;
Vector v{&i32, 4};
Type* ty = &v;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<ArrayType>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_TRUE(ty->Is<Vector>());
}
TEST_F(VectorTest, TypeName) {
I32 i32;
Vector v{&i32, 3};
EXPECT_EQ(v.type_name(), "__vec_3__i32");
}
TEST_F(VectorTest, FriendlyName) {
auto* v = ty.vec3<f32>();
EXPECT_EQ(v->FriendlyName(Symbols()), "vec3<f32>");
}
} // namespace
} // namespace sem
} // namespace tint

43
src/sem/void_type.cc Normal file
View File

@@ -0,0 +1,43 @@
// 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/void_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Void);
namespace tint {
namespace sem {
Void::Void() = default;
Void::Void(Void&&) = default;
Void::~Void() = default;
std::string Void::type_name() const {
return "__void";
}
std::string Void::FriendlyName(const SymbolTable&) const {
return "void";
}
Void* Void::Clone(CloneContext* ctx) const {
return ctx->dst->create<Void>();
}
} // namespace sem
} // namespace tint

51
src/sem/void_type.h Normal file
View File

@@ -0,0 +1,51 @@
// 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_VOID_TYPE_H_
#define SRC_SEM_VOID_TYPE_H_
#include <string>
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// A void type
class Void : public Castable<Void, Type> {
public:
/// Constructor
Void();
/// Move constructor
Void(Void&&);
~Void() override;
/// @returns the name for this 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;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Void* Clone(CloneContext* ctx) const override;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_VOID_TYPE_H_