-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrecord.go
More file actions
90 lines (81 loc) · 1.93 KB
/
record.go
File metadata and controls
90 lines (81 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package wit
// Record represents a WIT [record type], akin to a struct.
// It implements the [Node], [ABI], and [TypeDefKind] interfaces.
//
// [record type]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#item-record-bag-of-named-fields
type Record struct {
_typeDefKind
Fields []Field
}
// Size returns the [ABI byte size] for [Record] r.
//
// [ABI byte size]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#element-size
func (r *Record) Size() uintptr {
var s uintptr
for _, f := range r.Fields {
s = Align(s, f.Type.Align())
s += f.Type.Size()
}
return Align(s, r.Align())
}
// Align returns the [ABI byte alignment] for [Record] r.
//
// [ABI byte alignment]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#alignment
func (r *Record) Align() uintptr {
var a uintptr = 1
for _, f := range r.Fields {
a = max(a, f.Type.Align())
}
return a
}
// Flat returns the [flattened] ABI representation of [Record] r.
//
// [flattened]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#flattening
func (r *Record) Flat() []Type {
flat := make([]Type, 0, len(r.Fields))
for _, f := range r.Fields {
flat = append(flat, f.Type.Flat()...)
}
return flat
}
func (r *Record) hasPointer() bool {
for _, f := range r.Fields {
if HasPointer(f.Type) {
return true
}
}
return false
}
func (r *Record) hasBorrow() bool {
for _, f := range r.Fields {
if HasBorrow(f.Type) {
return true
}
}
return false
}
func (r *Record) hasResource() bool {
for _, f := range r.Fields {
if HasResource(f.Type) {
return true
}
}
return false
}
func (r *Record) dependsOn(dep Node) bool {
if dep == r {
return true
}
for _, f := range r.Fields {
if DependsOn(f.Type, dep) {
return true
}
}
return false
}
// Field represents a field in a [Record].
type Field struct {
Name string
Type Type
Docs Docs
}