From cf1613ee35ed18862f2d05faa72efb30a581575d Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 17 Jun 2021 15:48:39 +0000 Subject: [PATCH] sem: Add atomic type Bug: tint:892 Change-Id: I4826b71e5911c7f66019a956685675e9504551cf Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/54646 Kokoro: Kokoro Reviewed-by: James Price --- src/BUILD.gn | 2 ++ src/CMakeLists.txt | 3 +++ src/sem/atomic_type.cc | 46 +++++++++++++++++++++++++++++++ src/sem/atomic_type.h | 54 +++++++++++++++++++++++++++++++++++++ src/sem/atomic_type_test.cc | 42 +++++++++++++++++++++++++++++ test/BUILD.gn | 1 + 6 files changed, 148 insertions(+) create mode 100644 src/sem/atomic_type.cc create mode 100644 src/sem/atomic_type.h create mode 100644 src/sem/atomic_type_test.cc diff --git a/src/BUILD.gn b/src/BUILD.gn index 992f5fa730..9b7a2b2de7 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -487,6 +487,8 @@ libtint_source_set("libtint_core_all_src") { "resolver/resolver.h", "scope_stack.h", "sem/array.h", + "sem/atomic_type.cc", + "sem/atomic_type.h", "sem/binding_point.h", "sem/bool_type.cc", "sem/bool_type.h", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bf41f5a7db..43e6c980d2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -235,6 +235,8 @@ set(TINT_LIB_SRCS scope_stack.h sem/array.cc sem/array.h + sem/atomic_type.cc + sem/atomic_type.h sem/binding_point.h sem/block_statement.cc sem/block_statement.h @@ -624,6 +626,7 @@ if(${TINT_BUILD_TESTS}) symbol_test.cc transform/transform_test.cc test_main.cc + sem/atomic_type_test.cc sem/bool_type_test.cc sem/depth_texture_type_test.cc sem/external_texture_type_test.cc diff --git a/src/sem/atomic_type.cc b/src/sem/atomic_type.cc new file mode 100644 index 0000000000..e632bd8bad --- /dev/null +++ b/src/sem/atomic_type.cc @@ -0,0 +1,46 @@ +// 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/atomic_type.h" + +#include "src/program_builder.h" +#include "src/sem/reference_type.h" + +TINT_INSTANTIATE_TYPEINFO(tint::sem::Atomic); + +namespace tint { +namespace sem { + +Atomic::Atomic(const sem::Type* subtype) : subtype_(subtype) { + TINT_ASSERT(!subtype->Is()); +} + +std::string Atomic::type_name() const { + std::ostringstream out; + out << "__atomic" << subtype_->type_name(); + return out.str(); +} + +std::string Atomic::FriendlyName(const SymbolTable& symbols) const { + std::ostringstream out; + out << "atomic<" << subtype_->FriendlyName(symbols) << ">"; + return out.str(); +} + +Atomic::Atomic(Atomic&&) = default; + +Atomic::~Atomic() = default; + +} // namespace sem +} // namespace tint diff --git a/src/sem/atomic_type.h b/src/sem/atomic_type.h new file mode 100644 index 0000000000..8c4a4ff5dd --- /dev/null +++ b/src/sem/atomic_type.h @@ -0,0 +1,54 @@ +// 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_ATOMIC_TYPE_H_ +#define SRC_SEM_ATOMIC_TYPE_H_ + +#include + +#include "src/sem/type.h" + +namespace tint { +namespace sem { + +/// A atomic type. +class Atomic : public Castable { + public: + /// Constructor + /// @param subtype the atomic type + explicit Atomic(const sem::Type* subtype); + + /// Move constructor + Atomic(Atomic&&); + ~Atomic() override; + + /// @returns the atomic type + const sem::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; + + private: + sem::Type const* const subtype_; +}; + +} // namespace sem +} // namespace tint + +#endif // SRC_SEM_ATOMIC_TYPE_H_ diff --git a/src/sem/atomic_type_test.cc b/src/sem/atomic_type_test.cc new file mode 100644 index 0000000000..121f82de05 --- /dev/null +++ b/src/sem/atomic_type_test.cc @@ -0,0 +1,42 @@ +// 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/atomic_type.h" + +#include "src/sem/test_helper.h" + +namespace tint { +namespace sem { +namespace { + +using AtomicTest = TestHelper; + +TEST_F(AtomicTest, Creation) { + auto* a = create(create()); + EXPECT_TRUE(a->Type()->Is()); +} + +TEST_F(AtomicTest, TypeName) { + auto* a = create(create()); + EXPECT_EQ(a->type_name(), "__atomic__i32"); +} + +TEST_F(AtomicTest, FriendlyName) { + auto* a = create(create()); + EXPECT_EQ(a->FriendlyName(Symbols()), "atomic"); +} + +} // namespace +} // namespace sem +} // namespace tint diff --git a/test/BUILD.gn b/test/BUILD.gn index 63fceec982..3ec89bb1bd 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -251,6 +251,7 @@ tint_unittests_source_set("tint_unittests_core_src") { "../src/resolver/var_let_test.cc", "../src/resolver/var_let_validation_test.cc", "../src/scope_stack_test.cc", + "../src/sem/atomic_type_test.cc", "../src/sem/bool_type_test.cc", "../src/sem/depth_texture_type_test.cc", "../src/sem/external_texture_type_test.cc",