From ad19a63c5431710377c426957c672692ffdabb5e Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 10 May 2021 19:45:46 +0000 Subject: [PATCH] Add sem::Reference Bug: tint:727 Change-Id: I904d33b6c0678f478b4687fcc08589dc3cfd379d Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50541 Commit-Queue: Ben Clayton Reviewed-by: James Price --- src/BUILD.gn | 2 ++ src/CMakeLists.txt | 3 ++ src/sem/pointer_type_test.cc | 21 ++++++------ src/sem/reference_type.cc | 48 ++++++++++++++++++++++++++++ src/sem/reference_type.h | 58 ++++++++++++++++++++++++++++++++++ src/sem/reference_type_test.cc | 47 +++++++++++++++++++++++++++ test/BUILD.gn | 1 + 7 files changed, 168 insertions(+), 12 deletions(-) create mode 100644 src/sem/reference_type.cc create mode 100644 src/sem/reference_type.h create mode 100644 src/sem/reference_type_test.cc diff --git a/src/BUILD.gn b/src/BUILD.gn index 5d0ae2a8a6..fc6835f4d2 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -471,6 +471,8 @@ libtint_source_set("libtint_core_all_src") { "sem/node.h", "sem/pointer_type.cc", "sem/pointer_type.h", + "sem/reference_type.cc", + "sem/reference_type.h", "sem/sampled_texture_type.cc", "sem/sampled_texture_type.h", "sem/sampler_type.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dc55ff0b9e..70b691df6f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -303,6 +303,8 @@ set(TINT_LIB_SRCS sem/multisampled_texture_type.h sem/pointer_type.cc sem/pointer_type.h + sem/reference_type.cc + sem/reference_type.h sem/sampled_texture_type.cc sem/sampled_texture_type.h sem/sampler_type.cc @@ -573,6 +575,7 @@ if(${TINT_BUILD_TESTS}) sem/matrix_type_test.cc sem/multisampled_texture_type_test.cc sem/pointer_type_test.cc + sem/reference_type_test.cc sem/sampled_texture_type_test.cc sem/sampler_type_test.cc sem/sem_array_test.cc diff --git a/src/sem/pointer_type_test.cc b/src/sem/pointer_type_test.cc index 6c75fa2a00..afa3a0aeff 100644 --- a/src/sem/pointer_type_test.cc +++ b/src/sem/pointer_type_test.cc @@ -23,27 +23,24 @@ 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); + auto* r = create(create(), ast::StorageClass::kStorage); + EXPECT_TRUE(r->type()->Is()); + EXPECT_EQ(r->storage_class(), ast::StorageClass::kStorage); } - TEST_F(PointerTest, TypeName) { - I32 i32; - Pointer p{&i32, ast::StorageClass::kWorkgroup}; - EXPECT_EQ(p.type_name(), "__ptr_workgroup__i32"); + auto* r = create(create(), ast::StorageClass::kWorkgroup); + EXPECT_EQ(r->type_name(), "__ptr_workgroup__i32"); } TEST_F(PointerTest, FriendlyNameWithStorageClass) { - Pointer p{ty.i32(), ast::StorageClass::kWorkgroup}; - EXPECT_EQ(p.FriendlyName(Symbols()), "ptr"); + auto* r = create(ty.i32(), ast::StorageClass::kWorkgroup); + EXPECT_EQ(r->FriendlyName(Symbols()), "ptr"); } TEST_F(PointerTest, FriendlyNameWithoutStorageClass) { - Pointer p{ty.i32(), ast::StorageClass::kNone}; - EXPECT_EQ(p.FriendlyName(Symbols()), "ptr"); + auto* r = create(ty.i32(), ast::StorageClass::kNone); + EXPECT_EQ(r->FriendlyName(Symbols()), "ptr"); } } // namespace diff --git a/src/sem/reference_type.cc b/src/sem/reference_type.cc new file mode 100644 index 0000000000..8e4c405e71 --- /dev/null +++ b/src/sem/reference_type.cc @@ -0,0 +1,48 @@ +// 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/reference_type.h" + +#include "src/program_builder.h" + +TINT_INSTANTIATE_TYPEINFO(tint::sem::Reference); + +namespace tint { +namespace sem { + +Reference::Reference(const Type* subtype, ast::StorageClass storage_class) + : subtype_(subtype), storage_class_(storage_class) {} + +std::string Reference::type_name() const { + std::ostringstream out; + out << "__ref_" << storage_class_ << subtype_->type_name(); + return out.str(); +} + +std::string Reference::FriendlyName(const SymbolTable& symbols) const { + std::ostringstream out; + out << "ref<"; + if (storage_class_ != ast::StorageClass::kNone) { + out << storage_class_ << ", "; + } + out << subtype_->FriendlyName(symbols) << ">"; + return out.str(); +} + +Reference::Reference(Reference&&) = default; + +Reference::~Reference() = default; + +} // namespace sem +} // namespace tint diff --git a/src/sem/reference_type.h b/src/sem/reference_type.h new file mode 100644 index 0000000000..b86112afc8 --- /dev/null +++ b/src/sem/reference_type.h @@ -0,0 +1,58 @@ +// 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_REFERENCE_TYPE_H_ +#define SRC_SEM_REFERENCE_TYPE_H_ + +#include + +#include "src/ast/storage_class.h" +#include "src/sem/type.h" + +namespace tint { +namespace sem { + +/// A reference type. +class Reference : public Castable { + public: + /// Constructor + /// @param subtype the pointee type + /// @param storage_class the storage class of the reference + Reference(const Type* subtype, ast::StorageClass storage_class); + /// Move constructor + Reference(Reference&&); + ~Reference() override; + + /// @returns the pointee type + const Type* type() const { return subtype_; } + /// @returns the storage class of the reference + 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; + + private: + Type const* const subtype_; + ast::StorageClass const storage_class_; +}; + +} // namespace sem +} // namespace tint + +#endif // SRC_SEM_REFERENCE_TYPE_H_ diff --git a/src/sem/reference_type_test.cc b/src/sem/reference_type_test.cc new file mode 100644 index 0000000000..bc890fa7c0 --- /dev/null +++ b/src/sem/reference_type_test.cc @@ -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/reference_type.h" +#include "src/sem/test_helper.h" + +namespace tint { +namespace sem { +namespace { + +using ReferenceTest = TestHelper; + +TEST_F(ReferenceTest, Creation) { + auto* r = create(create(), ast::StorageClass::kStorage); + EXPECT_TRUE(r->type()->Is()); + EXPECT_EQ(r->storage_class(), ast::StorageClass::kStorage); +} + +TEST_F(ReferenceTest, TypeName) { + auto* r = create(create(), ast::StorageClass::kWorkgroup); + EXPECT_EQ(r->type_name(), "__ref_workgroup__i32"); +} + +TEST_F(ReferenceTest, FriendlyNameWithStorageClass) { + auto* r = create(ty.i32(), ast::StorageClass::kWorkgroup); + EXPECT_EQ(r->FriendlyName(Symbols()), "ref"); +} + +TEST_F(ReferenceTest, FriendlyNameWithoutStorageClass) { + auto* r = create(ty.i32(), ast::StorageClass::kNone); + EXPECT_EQ(r->FriendlyName(Symbols()), "ref"); +} + +} // namespace +} // namespace sem +} // namespace tint diff --git a/test/BUILD.gn b/test/BUILD.gn index c5cd555dc1..ca6b446428 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -277,6 +277,7 @@ tint_unittests_source_set("tint_unittests_core_src") { "../src/sem/matrix_type_test.cc", "../src/sem/multisampled_texture_type_test.cc", "../src/sem/pointer_type_test.cc", + "../src/sem/reference_type_test.cc", "../src/sem/sampled_texture_type_test.cc", "../src/sem/sampler_type_test.cc", "../src/sem/sem_array_test.cc",