sem: Fold together sem::Struct and sem::StructType

There's now no need to have both.
Removes a whole bunch of Sem().Get() smell, and simplifies the resolver.

Bug: tint:724
Fixed: tint:761
Change-Id: I756a32680ac52441fd6eebf6fc53dd507ef5e538
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/49961
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2021-05-07 14:49:34 +00:00
committed by Commit Bot service account
parent 33d0f6aa08
commit ba6ab5e6bd
86 changed files with 962 additions and 1313 deletions

View File

@@ -44,7 +44,7 @@ TEST_F(AccessControlTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -40,7 +40,7 @@ TEST_F(AliasTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -54,7 +54,7 @@ TEST_F(ArrayTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -34,7 +34,7 @@ TEST_F(BoolTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -39,7 +39,7 @@ TEST_F(DepthTextureTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -40,7 +40,7 @@ TEST_F(ExternalTextureTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -34,7 +34,7 @@ TEST_F(F32Test, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -34,7 +34,7 @@ TEST_F(I32Test, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -45,7 +45,7 @@ TEST_F(MatrixTest, Is) {
EXPECT_TRUE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -40,7 +40,7 @@ TEST_F(MultisampledTextureTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -42,7 +42,7 @@ TEST_F(PointerTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_TRUE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -39,7 +39,7 @@ TEST_F(SampledTextureTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -45,7 +45,7 @@ TEST_F(SamplerTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_TRUE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -13,6 +13,7 @@
// limitations under the License.
#include "src/sem/access_control_type.h"
#include "src/sem/struct.h"
#include "src/sem/test_helper.h"
#include "src/sem/texture_type.h"
@@ -20,22 +21,27 @@ namespace tint {
namespace sem {
namespace {
using StructTypeTest = TestHelper;
using StructTest = TestHelper;
TEST_F(StructTypeTest, Creation) {
TEST_F(StructTest, Creation) {
auto name = Sym("S");
auto* impl =
create<ast::Struct>(name, ast::StructMemberList{}, ast::DecorationList{});
auto* ptr = impl;
auto s = ty.struct_(impl);
EXPECT_EQ(s->impl(), ptr);
auto* s = create<sem::Struct>(impl, StructMemberList{}, 4 /* align */,
8 /* size */, 16 /* size_no_padding */);
EXPECT_EQ(s->Declaration(), ptr);
EXPECT_EQ(s->Align(), 4u);
EXPECT_EQ(s->Size(), 8u);
EXPECT_EQ(s->SizeNoPadding(), 16u);
}
TEST_F(StructTypeTest, Is) {
TEST_F(StructTest, Is) {
auto name = Sym("S");
auto* impl =
create<ast::Struct>(name, ast::StructMemberList{}, ast::DecorationList{});
auto s = ty.struct_(impl);
auto* s = create<sem::Struct>(impl, StructMemberList{}, 4 /* align */,
4 /* size */, 4 /* size_no_padding */);
sem::Type* ty = s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
@@ -46,25 +52,27 @@ TEST_F(StructTypeTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_TRUE(ty->Is<StructType>());
EXPECT_TRUE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(StructTypeTest, TypeName) {
TEST_F(StructTest, TypeName) {
auto name = Sym("my_struct");
auto* impl =
create<ast::Struct>(name, ast::StructMemberList{}, ast::DecorationList{});
auto s = ty.struct_(impl);
auto* s = create<sem::Struct>(impl, StructMemberList{}, 4 /* align */,
4 /* size */, 4 /* size_no_padding */);
EXPECT_EQ(s->type_name(), "__struct_$1");
}
TEST_F(StructTypeTest, FriendlyName) {
TEST_F(StructTest, FriendlyName) {
auto name = Sym("my_struct");
auto* impl =
create<ast::Struct>(name, ast::StructMemberList{}, ast::DecorationList{});
auto s = ty.struct_(impl);
auto* s = create<sem::Struct>(impl, StructMemberList{}, 4 /* align */,
4 /* size */, 4 /* size_no_padding */);
EXPECT_EQ(s->FriendlyName(Symbols()), "my_struct");
}

View File

@@ -41,7 +41,7 @@ TEST_F(StorageTextureTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -15,6 +15,7 @@
#include "src/sem/struct.h"
#include "src/ast/struct_member.h"
#include <string>
#include <utility>
TINT_INSTANTIATE_TYPEINFO(tint::sem::Struct);
@@ -23,20 +24,16 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::StructMember);
namespace tint {
namespace sem {
Struct::Struct(sem::StructType* type,
Struct::Struct(const ast::Struct* declaration,
StructMemberList members,
uint32_t align,
uint32_t size,
uint32_t size_no_padding,
std::unordered_set<ast::StorageClass> storage_class_usage,
std::unordered_set<PipelineStageUsage> pipeline_stage_uses)
: type_(type),
uint32_t size_no_padding)
: declaration_(declaration),
members_(std::move(members)),
align_(align),
size_(size),
size_no_padding_(size_no_padding),
storage_class_usage_(std::move(storage_class_usage)),
pipeline_stage_uses_(std::move(pipeline_stage_uses)) {}
size_no_padding_(size_no_padding) {}
Struct::~Struct() = default;
@@ -49,6 +46,14 @@ const StructMember* Struct::FindMember(Symbol name) const {
return nullptr;
}
std::string Struct::type_name() const {
return declaration_->type_name();
}
std::string Struct::FriendlyName(const SymbolTable& symbols) const {
return declaration_->FriendlyName(symbols);
}
StructMember::StructMember(ast::StructMember* declaration,
sem::Type* type,
uint32_t offset,

View File

@@ -17,11 +17,14 @@
#include <stdint.h>
#include <string>
#include <unordered_set>
#include <vector>
#include "src/ast/storage_class.h"
#include "src/ast/struct.h"
#include "src/sem/node.h"
#include "src/sem/type.h"
#include "src/symbol.h"
namespace tint {
@@ -34,7 +37,6 @@ class StructMember;
namespace sem {
// Forward declarations
class StructType;
class StructMember;
class Type;
@@ -52,30 +54,26 @@ enum class PipelineStageUsage {
};
/// Struct holds the semantic information for structures.
class Struct : public Castable<Struct, Node> {
class Struct : public Castable<Struct, Type> {
public:
/// Constructor
/// @param type the structure type
/// @param declaration the AST structure declaration
/// @param members the structure members
/// @param align the byte alignment of the structure
/// @param size the byte size of the structure
/// @param size_no_padding size of the members without the end of structure
/// alignment padding
/// @param storage_class_usage a set of all the storage class usages
/// @param pipeline_stage_uses a set of all the pipeline stage uses
Struct(sem::StructType* type,
Struct(const ast::Struct* declaration,
StructMemberList members,
uint32_t align,
uint32_t size,
uint32_t size_no_padding,
std::unordered_set<ast::StorageClass> storage_class_usage,
std::unordered_set<PipelineStageUsage> pipeline_stage_uses);
uint32_t size_no_padding);
/// Destructor
~Struct() override;
/// @returns the structure type
sem::StructType* Type() const { return type_; }
/// @returns the struct
const ast::Struct* Declaration() const { return declaration_; }
/// @returns the members of the structure
const StructMemberList& Members() const { return members_; }
@@ -100,6 +98,12 @@ class Struct : public Castable<Struct, Node> {
/// alignment padding
uint32_t SizeNoPadding() const { return size_no_padding_; }
/// Adds the StorageClass usage to the structure.
/// @param usage the storage usage
void AddUsage(ast::StorageClass usage) {
storage_class_usage_.emplace(usage);
}
/// @returns the set of storage class uses of this structure
const std::unordered_set<ast::StorageClass>& StorageClassUsage() const {
return storage_class_usage_;
@@ -122,19 +126,38 @@ class Struct : public Castable<Struct, Node> {
return false;
}
/// Adds the pipeline stage usage to the structure.
/// @param usage the storage usage
void AddUsage(PipelineStageUsage usage) {
pipeline_stage_uses_.emplace(usage);
}
/// @returns the set of entry point uses of this structure
const std::unordered_set<PipelineStageUsage>& PipelineStageUses() const {
return pipeline_stage_uses_;
}
/// @returns true if the struct has a block decoration
bool IsBlockDecorated() const { return declaration_->IsBlockDecorated(); }
/// @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;
private:
sem::StructType* const type_;
uint64_t LargestMemberBaseAlignment(MemoryLayout mem_layout) const;
ast::Struct const* const declaration_;
StructMemberList const members_;
uint32_t const align_;
uint32_t const size_;
uint32_t const size_no_padding_;
std::unordered_set<ast::StorageClass> const storage_class_usage_;
std::unordered_set<PipelineStageUsage> const pipeline_stage_uses_;
std::unordered_set<ast::StorageClass> storage_class_usage_;
std::unordered_set<PipelineStageUsage> pipeline_stage_uses_;
};
/// StructMember holds the semantic information for structure members.

View File

@@ -1,41 +0,0 @@
// 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(ast::Struct* impl) : struct_(impl) {}
StructType::StructType(StructType&&) = default;
StructType::~StructType() = default;
std::string StructType::type_name() const {
return impl()->type_name();
}
std::string StructType::FriendlyName(const SymbolTable& symbols) const {
return impl()->FriendlyName(symbols);
}
} // namespace sem
} // namespace tint

View File

@@ -1,59 +0,0 @@
// 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 impl the struct data
explicit StructType(ast::Struct* impl);
/// Move constructor
StructType(StructType&&);
~StructType() override;
/// @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;
private:
ast::Struct* const struct_;
uint64_t LargestMemberBaseAlignment(MemoryLayout mem_layout) const;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_STRUCT_TYPE_H_

View File

@@ -41,7 +41,6 @@ class Function;
class MemberAccessorExpression;
class Statement;
class Struct;
class StructType;
class StructMember;
class Type;
class Variable;
@@ -58,7 +57,6 @@ struct TypeMappings {
Function* operator()(ast::Function*);
MemberAccessorExpression* operator()(ast::MemberAccessorExpression*);
Statement* operator()(ast::Statement*);
Struct* operator()(sem::StructType*);
StructMember* operator()(ast::StructMember*);
Type* operator()(ast::Type*);
Variable* operator()(ast::Variable*);

View File

@@ -34,7 +34,7 @@ TEST_F(U32Test, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_TRUE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());

View File

@@ -42,7 +42,7 @@ TEST_F(VectorTest, Is) {
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_TRUE(ty->Is<Vector>());