1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/sem/struct.h"
16
17 #include <string>
18 #include <utility>
19
20 #include "src/ast/struct_member.h"
21 #include "src/symbol_table.h"
22
23 TINT_INSTANTIATE_TYPEINFO(tint::sem::Struct);
24 TINT_INSTANTIATE_TYPEINFO(tint::sem::StructMember);
25
26 namespace tint {
27 namespace sem {
28
Struct(const ast::Struct * declaration,Symbol name,StructMemberList members,uint32_t align,uint32_t size,uint32_t size_no_padding)29 Struct::Struct(const ast::Struct* declaration,
30 Symbol name,
31 StructMemberList members,
32 uint32_t align,
33 uint32_t size,
34 uint32_t size_no_padding)
35 : declaration_(declaration),
36 name_(name),
37 members_(std::move(members)),
38 align_(align),
39 size_(size),
40 size_no_padding_(size_no_padding) {
41 constructible_ = true;
42 for (auto* member : members_) {
43 if (!member->Type()->IsConstructible()) {
44 constructible_ = false;
45 break;
46 }
47 }
48 }
49
50 Struct::~Struct() = default;
51
FindMember(Symbol name) const52 const StructMember* Struct::FindMember(Symbol name) const {
53 for (auto* member : members_) {
54 if (member->Declaration()->symbol == name) {
55 return member;
56 }
57 }
58 return nullptr;
59 }
60
type_name() const61 std::string Struct::type_name() const {
62 return "__struct_" + name_.to_str();
63 }
64
Align() const65 uint32_t Struct::Align() const {
66 return align_;
67 }
68
Size() const69 uint32_t Struct::Size() const {
70 return size_;
71 }
72
FriendlyName(const SymbolTable & symbols) const73 std::string Struct::FriendlyName(const SymbolTable& symbols) const {
74 return symbols.NameFor(name_);
75 }
76
IsConstructible() const77 bool Struct::IsConstructible() const {
78 return constructible_;
79 }
80
StructMember(const ast::StructMember * declaration,Symbol name,sem::Type * type,uint32_t index,uint32_t offset,uint32_t align,uint32_t size)81 StructMember::StructMember(const ast::StructMember* declaration,
82 Symbol name,
83 sem::Type* type,
84 uint32_t index,
85 uint32_t offset,
86 uint32_t align,
87 uint32_t size)
88 : declaration_(declaration),
89 name_(name),
90 type_(type),
91 index_(index),
92 offset_(offset),
93 align_(align),
94 size_(size) {}
95
96 StructMember::~StructMember() = default;
97
98 } // namespace sem
99 } // namespace tint
100