@@ -21,18 +21,25 @@ pub fn define_datatype(names: &Names, namedtype: &witx::NamedType) -> TokenStrea
21
21
witx:: Type :: Union ( _) => unimplemented ! ( "union types" ) ,
22
22
witx:: Type :: Handle ( _h) => unimplemented ! ( "handle types" ) ,
23
23
witx:: Type :: Builtin ( b) => define_builtin ( names, & namedtype. name , * b) ,
24
- witx:: Type :: Pointer { .. } => unimplemented ! ( "pointer types" ) ,
25
- witx:: Type :: ConstPointer { .. } => unimplemented ! ( "constpointer types" ) ,
24
+ witx:: Type :: Pointer ( p) => {
25
+ define_witx_pointer ( names, & namedtype. name , quote ! ( :: memory:: GuestPtrMut ) , p)
26
+ }
27
+ witx:: Type :: ConstPointer ( p) => {
28
+ define_witx_pointer ( names, & namedtype. name , quote ! ( :: memory:: GuestPtr ) , p)
29
+ }
26
30
witx:: Type :: Array { .. } => unimplemented ! ( "array types" ) ,
27
31
} ,
28
32
}
29
33
}
30
34
31
35
fn define_alias ( names : & Names , name : & witx:: Id , to : & witx:: NamedType ) -> TokenStream {
32
36
let ident = names. type_ ( name) ;
33
- let to = names. type_ ( & to. name ) ;
34
-
35
- quote ! ( pub type #ident = #to; )
37
+ let rhs = names. type_ ( & to. name ) ;
38
+ if type_needs_lifetime ( & to. tref ) {
39
+ quote ! ( pub type #ident<' a> = #rhs<' a>; )
40
+ } else {
41
+ quote ! ( pub type #ident = #rhs; )
42
+ }
36
43
}
37
44
38
45
fn define_enum ( names : & Names , name : & witx:: Id , e : & witx:: EnumDatatype ) -> TokenStream {
@@ -133,6 +140,23 @@ fn define_builtin(names: &Names, name: &witx::Id, builtin: witx::BuiltinType) ->
133
140
quote ! ( pub type #ident = #built; )
134
141
}
135
142
143
+ pub fn type_needs_lifetime ( tref : & witx:: TypeRef ) -> bool {
144
+ match & * tref. type_ ( ) {
145
+ witx:: Type :: Builtin ( b) => match b {
146
+ witx:: BuiltinType :: String => unimplemented ! ( ) ,
147
+ _ => false ,
148
+ } ,
149
+ witx:: Type :: Enum { .. }
150
+ | witx:: Type :: Flags { .. }
151
+ | witx:: Type :: Int { .. }
152
+ | witx:: Type :: Handle { .. } => false ,
153
+ witx:: Type :: Struct ( s) => !struct_is_copy ( & s) ,
154
+ witx:: Type :: Union { .. } => true ,
155
+ witx:: Type :: Pointer { .. } | witx:: Type :: ConstPointer { .. } => true ,
156
+ witx:: Type :: Array { .. } => unimplemented ! ( ) ,
157
+ }
158
+ }
159
+
136
160
pub fn struct_is_copy ( s : & witx:: StructDatatype ) -> bool {
137
161
s. members . iter ( ) . all ( |m| match & * m. tref . type_ ( ) {
138
162
witx:: Type :: Struct ( s) => struct_is_copy ( & s) ,
@@ -158,11 +182,11 @@ fn define_copy_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype)
158
182
159
183
let member_decls = s. members . iter ( ) . map ( |m| {
160
184
let name = names. struct_member ( & m. name ) ;
161
- let type_ = names. type_ref ( & m. tref ) ;
185
+ let type_ = names. type_ref ( & m. tref , anon_lifetime ( ) ) ;
162
186
quote ! ( pub #name: #type_)
163
187
} ) ;
164
188
let member_valids = s. member_layout ( ) . into_iter ( ) . map ( |ml| {
165
- let type_ = names. type_ref ( & ml. member . tref ) ;
189
+ let type_ = names. type_ref ( & ml. member . tref , anon_lifetime ( ) ) ;
166
190
let offset = ml. offset as u32 ;
167
191
let fieldname = names. struct_member ( & ml. member . name ) ;
168
192
quote ! {
@@ -221,11 +245,11 @@ fn define_ptr_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) -
221
245
witx:: TypeRef :: Value ( ty) => match & * * ty {
222
246
witx:: Type :: Builtin ( builtin) => names. builtin_type ( * builtin) ,
223
247
witx:: Type :: Pointer ( pointee) => {
224
- let pointee_type = names. type_ref ( & pointee) ;
248
+ let pointee_type = names. type_ref ( & pointee, quote ! ( ' a ) ) ;
225
249
quote ! ( :: memory:: GuestPtrMut <' a, #pointee_type>)
226
250
}
227
251
witx:: Type :: ConstPointer ( pointee) => {
228
- let pointee_type = names. type_ref ( & pointee) ;
252
+ let pointee_type = names. type_ref ( & pointee, quote ! ( ' a ) ) ;
229
253
quote ! ( :: memory:: GuestPtr <' a, #pointee_type>)
230
254
}
231
255
_ => unimplemented ! ( "other anonymous struct members" ) ,
@@ -239,11 +263,11 @@ fn define_ptr_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) -
239
263
witx:: TypeRef :: Value ( ty) => match & * * ty {
240
264
witx:: Type :: Builtin ( builtin) => names. builtin_type ( * builtin) ,
241
265
witx:: Type :: Pointer ( pointee) => {
242
- let pointee_type = names. type_ref ( & pointee) ;
266
+ let pointee_type = names. type_ref ( & pointee, anon_lifetime ( ) ) ;
243
267
quote ! ( :: memory:: GuestPtrMut :: <#pointee_type>)
244
268
}
245
269
witx:: Type :: ConstPointer ( pointee) => {
246
- let pointee_type = names. type_ref ( & pointee) ;
270
+ let pointee_type = names. type_ref ( & pointee, anon_lifetime ( ) ) ;
247
271
quote ! ( :: memory:: GuestPtr :: <#pointee_type>)
248
272
}
249
273
_ => unimplemented ! ( "other anonymous struct members" ) ,
@@ -286,13 +310,13 @@ fn define_ptr_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) -
286
310
}
287
311
}
288
312
witx:: Type :: Pointer ( pointee) => {
289
- let pointee_type = names. type_ref ( & pointee) ;
313
+ let pointee_type = names. type_ref ( & pointee, anon_lifetime ( ) ) ;
290
314
quote ! {
291
315
let #name = :: memory:: GuestPtrMut :: <#pointee_type>:: read_from_guest( & location. cast( #offset) ?) ?;
292
316
}
293
317
}
294
318
witx:: Type :: ConstPointer ( pointee) => {
295
- let pointee_type = names. type_ref ( & pointee) ;
319
+ let pointee_type = names. type_ref ( & pointee, anon_lifetime ( ) ) ;
296
320
quote ! {
297
321
let #name = :: memory:: GuestPtr :: <#pointee_type>:: read_from_guest( & location. cast( #offset) ?) ?;
298
322
}
@@ -340,6 +364,19 @@ fn define_ptr_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) -
340
364
}
341
365
}
342
366
}
367
+
368
+ fn define_witx_pointer (
369
+ names : & Names ,
370
+ name : & witx:: Id ,
371
+ pointer_type : TokenStream ,
372
+ pointee : & witx:: TypeRef ,
373
+ ) -> TokenStream {
374
+ let ident = names. type_ ( name) ;
375
+ let pointee_type = names. type_ref ( pointee, quote ! ( ' a) ) ;
376
+
377
+ quote ! ( pub type #ident<' a> = #pointer_type<' a, #pointee_type>; )
378
+ }
379
+
343
380
fn int_repr_tokens ( int_repr : witx:: IntRepr ) -> TokenStream {
344
381
match int_repr {
345
382
witx:: IntRepr :: U8 => quote ! ( u8 ) ,
@@ -356,3 +393,7 @@ fn atom_token(atom: witx::AtomType) -> TokenStream {
356
393
witx:: AtomType :: F64 => quote ! ( f64 ) ,
357
394
}
358
395
}
396
+
397
+ pub fn anon_lifetime ( ) -> TokenStream {
398
+ quote ! ( ' _)
399
+ }
0 commit comments