@@ -23,14 +23,14 @@ impl TypeRef {
23
23
return * hit;
24
24
}
25
25
let layout = match & * self . type_ ( ) {
26
- Type :: Enum ( e) => e. repr . layout ( ) ,
27
- Type :: Flags ( f) => f. repr . layout ( ) ,
26
+ Type :: Enum ( e) => e. repr . mem_size_align ( ) ,
27
+ Type :: Flags ( f) => f. repr . mem_size_align ( ) ,
28
28
Type :: Struct ( s) => s. layout ( cache) ,
29
29
Type :: Union ( u) => u. layout ( cache) ,
30
- Type :: Handle { .. } => BuiltinType :: U32 . layout ( ) ,
31
- Type :: Array { .. } => BuiltinType :: String . layout ( ) ,
32
- Type :: Pointer { .. } | Type :: ConstPointer { .. } => BuiltinType :: U32 . layout ( ) ,
33
- Type :: Builtin ( b) => b. layout ( ) ,
30
+ Type :: Handle { .. } => BuiltinType :: U32 . mem_size_align ( ) ,
31
+ Type :: Array { .. } => BuiltinType :: String . mem_size_align ( ) ,
32
+ Type :: Pointer { .. } | Type :: ConstPointer { .. } => BuiltinType :: U32 . mem_size_align ( ) ,
33
+ Type :: Builtin ( b) => b. mem_size_align ( ) ,
34
34
} ;
35
35
cache. insert ( self . clone ( ) , layout) ;
36
36
layout
@@ -43,28 +43,28 @@ impl Layout for TypeRef {
43
43
self . layout ( & mut cache)
44
44
}
45
45
}
46
-
47
- impl IntRepr {
48
- pub fn layout ( & self ) -> SizeAlign {
46
+ impl Layout for IntRepr {
47
+ fn mem_size_align ( & self ) -> SizeAlign {
49
48
match self {
50
- IntRepr :: U8 => BuiltinType :: U8 . layout ( ) ,
51
- IntRepr :: U16 => BuiltinType :: U16 . layout ( ) ,
52
- IntRepr :: U32 => BuiltinType :: U32 . layout ( ) ,
53
- IntRepr :: U64 => BuiltinType :: U64 . layout ( ) ,
49
+ IntRepr :: U8 => BuiltinType :: U8 . mem_size_align ( ) ,
50
+ IntRepr :: U16 => BuiltinType :: U16 . mem_size_align ( ) ,
51
+ IntRepr :: U32 => BuiltinType :: U32 . mem_size_align ( ) ,
52
+ IntRepr :: U64 => BuiltinType :: U64 . mem_size_align ( ) ,
54
53
}
55
54
}
56
55
}
57
56
58
57
pub struct StructMemberLayout < ' a > {
59
- member : & ' a StructMember ,
60
- offset : usize ,
58
+ pub member : & ' a StructMember ,
59
+ pub offset : usize ,
61
60
}
62
61
63
62
impl StructDatatype {
64
- pub fn member_layout (
65
- & self ,
66
- cache : & mut HashMap < TypeRef , SizeAlign > ,
67
- ) -> Vec < StructMemberLayout > {
63
+ pub fn member_layout ( & self ) -> Vec < StructMemberLayout > {
64
+ self . member_layout_ ( & mut HashMap :: new ( ) )
65
+ }
66
+
67
+ fn member_layout_ ( & self , cache : & mut HashMap < TypeRef , SizeAlign > ) -> Vec < StructMemberLayout > {
68
68
let mut members = Vec :: new ( ) ;
69
69
let mut offset = 0 ;
70
70
for m in self . members . iter ( ) {
@@ -76,19 +76,27 @@ impl StructDatatype {
76
76
members
77
77
}
78
78
79
- pub fn layout ( & self , cache : & mut HashMap < TypeRef , SizeAlign > ) -> SizeAlign {
80
- let members = self . member_layout ( cache) ;
79
+ fn layout ( & self , cache : & mut HashMap < TypeRef , SizeAlign > ) -> SizeAlign {
80
+ let members = self . member_layout_ ( cache) ;
81
81
let align = members
82
82
. iter ( )
83
83
. map ( |m| m. member . tref . layout ( cache) . align )
84
84
. max ( )
85
85
. expect ( "nonzero struct members" ) ;
86
- let last_offset = members. last ( ) . expect ( "nonzero struct members" ) . offset ;
87
- let size = align_to ( last_offset, align) ;
86
+ let last = members. last ( ) . expect ( "nonzero struct members" ) ;
87
+ let size = last. offset + last. member . tref . layout ( cache) . size ;
88
+ let size = align_to ( size, align) ;
88
89
SizeAlign { size, align }
89
90
}
90
91
}
91
92
93
+ impl Layout for StructDatatype {
94
+ fn mem_size_align ( & self ) -> SizeAlign {
95
+ let mut cache = HashMap :: new ( ) ;
96
+ self . layout ( & mut cache)
97
+ }
98
+ }
99
+
92
100
/// If the next free byte in the struct is `offs`, and the next
93
101
/// element has alignment `alignment`, determine the offset at
94
102
/// which to place that element.
@@ -124,7 +132,7 @@ mod test {
124
132
}
125
133
126
134
impl UnionDatatype {
127
- pub fn layout ( & self , cache : & mut HashMap < TypeRef , SizeAlign > ) -> SizeAlign {
135
+ fn layout ( & self , cache : & mut HashMap < TypeRef , SizeAlign > ) -> SizeAlign {
128
136
let sas = self
129
137
. variants
130
138
. iter ( )
@@ -144,8 +152,15 @@ impl UnionDatatype {
144
152
}
145
153
}
146
154
147
- impl BuiltinType {
148
- pub fn layout ( & self ) -> SizeAlign {
155
+ impl Layout for UnionDatatype {
156
+ fn mem_size_align ( & self ) -> SizeAlign {
157
+ let mut cache = HashMap :: new ( ) ;
158
+ self . layout ( & mut cache)
159
+ }
160
+ }
161
+
162
+ impl Layout for BuiltinType {
163
+ fn mem_size_align ( & self ) -> SizeAlign {
149
164
match self {
150
165
BuiltinType :: String => SizeAlign { size : 8 , align : 4 } , // Pointer and Length
151
166
BuiltinType :: U8 | BuiltinType :: S8 => SizeAlign { size : 1 , align : 1 } ,
0 commit comments