Add sem::Reference

Bug: tint:727
Change-Id: I904d33b6c0678f478b4687fcc08589dc3cfd379d
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50541
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-05-10 19:45:46 +00:00 committed by Commit Bot service account
parent 3f968e7b05
commit ad19a63c54
7 changed files with 168 additions and 12 deletions

View File

@ -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",

View File

@ -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

View File

@ -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<Pointer>(create<I32>(), ast::StorageClass::kStorage);
EXPECT_TRUE(r->type()->Is<sem::I32>());
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<Pointer>(create<I32>(), 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<workgroup, i32>");
auto* r = create<Pointer>(ty.i32(), ast::StorageClass::kWorkgroup);
EXPECT_EQ(r->FriendlyName(Symbols()), "ptr<workgroup, i32>");
}
TEST_F(PointerTest, FriendlyNameWithoutStorageClass) {
Pointer p{ty.i32(), ast::StorageClass::kNone};
EXPECT_EQ(p.FriendlyName(Symbols()), "ptr<i32>");
auto* r = create<Pointer>(ty.i32(), ast::StorageClass::kNone);
EXPECT_EQ(r->FriendlyName(Symbols()), "ptr<i32>");
}
} // namespace

48
src/sem/reference_type.cc Normal file
View File

@ -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

58
src/sem/reference_type.h Normal file
View File

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

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/reference_type.h"
#include "src/sem/test_helper.h"
namespace tint {
namespace sem {
namespace {
using ReferenceTest = TestHelper;
TEST_F(ReferenceTest, Creation) {
auto* r = create<Reference>(create<I32>(), ast::StorageClass::kStorage);
EXPECT_TRUE(r->type()->Is<sem::I32>());
EXPECT_EQ(r->storage_class(), ast::StorageClass::kStorage);
}
TEST_F(ReferenceTest, TypeName) {
auto* r = create<Reference>(create<I32>(), ast::StorageClass::kWorkgroup);
EXPECT_EQ(r->type_name(), "__ref_workgroup__i32");
}
TEST_F(ReferenceTest, FriendlyNameWithStorageClass) {
auto* r = create<Reference>(ty.i32(), ast::StorageClass::kWorkgroup);
EXPECT_EQ(r->FriendlyName(Symbols()), "ref<workgroup, i32>");
}
TEST_F(ReferenceTest, FriendlyNameWithoutStorageClass) {
auto* r = create<Reference>(ty.i32(), ast::StorageClass::kNone);
EXPECT_EQ(r->FriendlyName(Symbols()), "ref<i32>");
}
} // namespace
} // namespace sem
} // namespace tint

View File

@ -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",