sem: Add atomic type

Bug: tint:892
Change-Id: I4826b71e5911c7f66019a956685675e9504551cf
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/54646
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-06-17 15:48:39 +00:00 committed by Ben Clayton
parent 989c3a1811
commit cf1613ee35
6 changed files with 148 additions and 0 deletions

View File

@ -487,6 +487,8 @@ libtint_source_set("libtint_core_all_src") {
"resolver/resolver.h", "resolver/resolver.h",
"scope_stack.h", "scope_stack.h",
"sem/array.h", "sem/array.h",
"sem/atomic_type.cc",
"sem/atomic_type.h",
"sem/binding_point.h", "sem/binding_point.h",
"sem/bool_type.cc", "sem/bool_type.cc",
"sem/bool_type.h", "sem/bool_type.h",

View File

@ -235,6 +235,8 @@ set(TINT_LIB_SRCS
scope_stack.h scope_stack.h
sem/array.cc sem/array.cc
sem/array.h sem/array.h
sem/atomic_type.cc
sem/atomic_type.h
sem/binding_point.h sem/binding_point.h
sem/block_statement.cc sem/block_statement.cc
sem/block_statement.h sem/block_statement.h
@ -624,6 +626,7 @@ if(${TINT_BUILD_TESTS})
symbol_test.cc symbol_test.cc
transform/transform_test.cc transform/transform_test.cc
test_main.cc test_main.cc
sem/atomic_type_test.cc
sem/bool_type_test.cc sem/bool_type_test.cc
sem/depth_texture_type_test.cc sem/depth_texture_type_test.cc
sem/external_texture_type_test.cc sem/external_texture_type_test.cc

46
src/sem/atomic_type.cc Normal file
View File

@ -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<Reference>());
}
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

54
src/sem/atomic_type.h Normal file
View File

@ -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 <string>
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// A atomic type.
class Atomic : public Castable<Atomic, Type> {
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_

View File

@ -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<Atomic>(create<I32>());
EXPECT_TRUE(a->Type()->Is<sem::I32>());
}
TEST_F(AtomicTest, TypeName) {
auto* a = create<Atomic>(create<I32>());
EXPECT_EQ(a->type_name(), "__atomic__i32");
}
TEST_F(AtomicTest, FriendlyName) {
auto* a = create<Atomic>(create<I32>());
EXPECT_EQ(a->FriendlyName(Symbols()), "atomic<i32>");
}
} // namespace
} // namespace sem
} // namespace tint

View File

@ -251,6 +251,7 @@ tint_unittests_source_set("tint_unittests_core_src") {
"../src/resolver/var_let_test.cc", "../src/resolver/var_let_test.cc",
"../src/resolver/var_let_validation_test.cc", "../src/resolver/var_let_validation_test.cc",
"../src/scope_stack_test.cc", "../src/scope_stack_test.cc",
"../src/sem/atomic_type_test.cc",
"../src/sem/bool_type_test.cc", "../src/sem/bool_type_test.cc",
"../src/sem/depth_texture_type_test.cc", "../src/sem/depth_texture_type_test.cc",
"../src/sem/external_texture_type_test.cc", "../src/sem/external_texture_type_test.cc",