@@ -78,19 +78,20 @@ fn print_datatype(ret: &mut String, nt: &NamedType) {
7878 ret. push_str ( " */\n " ) ;
7979 }
8080
81- match & nt. dt {
81+ match & nt. tref {
8282 TypeRef :: Value ( v) => match & * * v {
8383 Type :: Enum ( e) => print_enum ( ret, & nt. name , e) ,
84+ Type :: Int ( i) => print_int ( ret, & nt. name , i) ,
8485 Type :: Flags ( f) => print_flags ( ret, & nt. name , f) ,
8586 Type :: Struct ( s) => print_struct ( ret, & nt. name , s) ,
8687 Type :: Union ( u) => print_union ( ret, & nt. name , u) ,
8788 Type :: Handle ( h) => print_handle ( ret, & nt. name , h) ,
8889 Type :: Builtin { .. }
8990 | Type :: Array { .. }
9091 | Type :: Pointer { .. }
91- | Type :: ConstPointer { .. } => print_alias ( ret, & nt. name , & nt. dt ) ,
92+ | Type :: ConstPointer { .. } => print_alias ( ret, & nt. name , & nt. tref ) ,
9293 } ,
93- TypeRef :: Name ( _) => print_alias ( ret, & nt. name , & nt. dt ) ,
94+ TypeRef :: Name ( _) => print_alias ( ret, & nt. name , & nt. tref ) ,
9495 }
9596}
9697
@@ -147,6 +148,46 @@ fn print_enum(ret: &mut String, name: &Id, e: &EnumDatatype) {
147148 }
148149}
149150
151+ fn print_int ( ret : & mut String , name : & Id , i : & IntDatatype ) {
152+ ret. push_str ( & format ! (
153+ "typedef {} __wasi_{}_t;\n " ,
154+ intrepr_name( i. repr) ,
155+ ident_name( name)
156+ ) ) ;
157+ ret. push_str ( "\n " ) ;
158+
159+ for ( index, const_) in i. consts . iter ( ) . enumerate ( ) {
160+ if !const_. docs . is_empty ( ) {
161+ ret. push_str ( "/**\n " ) ;
162+ for line in const_. docs . lines ( ) {
163+ ret. push_str ( & format ! ( " * {}\n " , line) ) ;
164+ }
165+ ret. push_str ( " */\n " ) ;
166+ }
167+ ret. push_str ( & format ! (
168+ "#define __WASI_{}_{} ({}({}))\n " ,
169+ ident_name( & name) . to_shouty_snake_case( ) ,
170+ ident_name( & const_. name) . to_shouty_snake_case( ) ,
171+ intrepr_const( i. repr) ,
172+ index
173+ ) ) ;
174+ ret. push_str ( "\n " ) ;
175+ }
176+
177+ ret. push_str ( & format ! (
178+ "_Static_assert(sizeof(__wasi_{}_t) == {}, \" witx calculated size\" );\n " ,
179+ ident_name( name) ,
180+ i. repr. mem_size( )
181+ ) ) ;
182+ ret. push_str ( & format ! (
183+ "_Static_assert(_Alignof(__wasi_{}_t) == {}, \" witx calculated align\" );\n " ,
184+ ident_name( name) ,
185+ i. repr. mem_align( )
186+ ) ) ;
187+
188+ ret. push_str ( "\n " ) ;
189+ }
190+
150191fn print_flags ( ret : & mut String , name : & Id , f : & FlagsDatatype ) {
151192 ret. push_str ( & format ! (
152193 "typedef {} __wasi_{}_t;\n " ,
@@ -377,7 +418,10 @@ fn ident_name(i: &Id) -> String {
377418
378419fn builtin_type_name ( b : BuiltinType ) -> & ' static str {
379420 match b {
380- BuiltinType :: String => "string" ,
421+ BuiltinType :: String | BuiltinType :: Char8 => {
422+ panic ! ( "no type name for string or char8 builtins" )
423+ }
424+ BuiltinType :: USize => "size_t" ,
381425 BuiltinType :: U8 => "uint8_t" ,
382426 BuiltinType :: U16 => "uint16_t" ,
383427 BuiltinType :: U32 => "uint32_t" ,
@@ -392,13 +436,26 @@ fn builtin_type_name(b: BuiltinType) -> &'static str {
392436}
393437
394438fn typeref_name ( tref : & TypeRef ) -> String {
439+ match & * tref. type_ ( ) {
440+ Type :: Builtin ( BuiltinType :: String ) | Type :: Builtin ( BuiltinType :: Char8 ) | Type :: Array ( _) => {
441+ panic ! ( "unsupported grammar: cannot construct name of string or array" , )
442+ }
443+ _ => { }
444+ }
445+
395446 match tref {
396- TypeRef :: Name ( named_type) => format ! ( "__wasi_{}_t" , named_type. name. as_str( ) ) ,
447+ TypeRef :: Name ( named_type) => match & * named_type. type_ ( ) {
448+ Type :: Pointer ( p) => format ! ( "{} *" , typeref_name( & * p) ) ,
449+ Type :: ConstPointer ( p) => format ! ( "const {} *" , typeref_name( & * p) ) ,
450+ Type :: Array ( _) => unreachable ! ( "arrays excluded above" ) ,
451+ _ => format ! ( "__wasi_{}_t" , named_type. name. as_str( ) ) ,
452+ } ,
397453 TypeRef :: Value ( anon_type) => match & * * anon_type {
454+ Type :: Array ( _) => unreachable ! ( "arrays excluded above" ) ,
398455 Type :: Builtin ( b) => builtin_type_name ( * b) . to_string ( ) ,
399- Type :: Array ( _) => unreachable ! ( "arrays should be special-cased" ) ,
400456 Type :: Pointer ( p) => format ! ( "{} *" , typeref_name( & * p) ) ,
401457 Type :: ConstPointer ( p) => format ! ( "const {} *" , typeref_name( & * p) ) ,
458+ Type :: Int ( i) => format ! ( "{}" , intrepr_name( i. repr) ) ,
402459 Type :: Struct { .. }
403460 | Type :: Union { .. }
404461 | Type :: Enum { .. }
0 commit comments