@@ -500,21 +500,28 @@ fn writeln_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name:
500
500
write_cpp_wrapper ( cpp_headers, & format ! ( "{}" , ident) , true ) ;
501
501
}
502
502
503
- /// Writes out all the relevant mappings for a Rust struct, deferring to writeln_opaque to generate
504
- /// the struct itself, and then writing getters and setters for public, understood-type fields and
505
- /// a constructor if every field is public.
506
- fn writeln_struct < ' a , ' b , W : std:: io:: Write > ( w : & mut W , s : & ' a syn:: ItemStruct , types : & mut TypeResolver < ' b , ' a > , extra_headers : & mut File , cpp_headers : & mut File ) {
507
- let struct_name = & format ! ( "{}" , s. ident) ;
503
+ fn declare_struct < ' a , ' b > ( s : & ' a syn:: ItemStruct , types : & mut TypeResolver < ' b , ' a > ) -> bool {
508
504
let export = export_status ( & s. attrs ) ;
509
505
match export {
510
506
ExportStatus :: Export => { } ,
511
- ExportStatus :: TestOnly => return ,
507
+ ExportStatus :: TestOnly => return false ,
512
508
ExportStatus :: NoExport => {
513
509
types. struct_ignored ( & s. ident ) ;
514
- return ;
510
+ return false ;
515
511
}
516
512
}
517
513
514
+ types. struct_imported ( & s. ident , format ! ( "{}" , s. ident) ) ;
515
+ true
516
+ }
517
+
518
+ /// Writes out all the relevant mappings for a Rust struct, deferring to writeln_opaque to generate
519
+ /// the struct itself, and then writing getters and setters for public, understood-type fields and
520
+ /// a constructor if every field is public.
521
+ fn writeln_struct < ' a , ' b , W : std:: io:: Write > ( w : & mut W , s : & ' a syn:: ItemStruct , types : & mut TypeResolver < ' b , ' a > , extra_headers : & mut File , cpp_headers : & mut File ) {
522
+ if !declare_struct ( s, types) { return ; }
523
+
524
+ let struct_name = & format ! ( "{}" , s. ident) ;
518
525
writeln_opaque ( w, & s. ident , struct_name, & s. generics , & s. attrs , types, extra_headers, cpp_headers) ;
519
526
520
527
eprintln ! ( "exporting fields for {}" , struct_name) ;
@@ -598,8 +605,6 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
598
605
writeln ! ( w, "\t }})), is_owned: true }}\n }}" ) . unwrap ( ) ;
599
606
}
600
607
}
601
-
602
- types. struct_imported ( & s. ident , struct_name. clone ( ) ) ;
603
608
}
604
609
605
610
/// Prints a relevant conversion for impl *
@@ -916,6 +921,19 @@ fn is_enum_opaque(e: &syn::ItemEnum) -> bool {
916
921
false
917
922
}
918
923
924
+ fn declare_enum < ' a , ' b > ( e : & ' a syn:: ItemEnum , types : & mut TypeResolver < ' b , ' a > ) {
925
+ match export_status ( & e. attrs ) {
926
+ ExportStatus :: Export => { } ,
927
+ ExportStatus :: NoExport |ExportStatus :: TestOnly => return ,
928
+ }
929
+
930
+ if is_enum_opaque ( e) {
931
+ types. enum_ignored ( & e. ident ) ;
932
+ } else {
933
+ types. mirrored_enum_declared ( & e. ident ) ;
934
+ }
935
+ }
936
+
919
937
/// Print a mapping of an enum. If all of the enum's fields are C-mapped in some form (or the enum
920
938
/// is unitary), we generate an equivalent enum with all types replaced with their C mapped
921
939
/// versions followed by conversion functions which map between the Rust version and the C mapped
@@ -929,15 +947,13 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
929
947
if is_enum_opaque ( e) {
930
948
eprintln ! ( "Skipping enum {} as it contains non-unit fields" , e. ident) ;
931
949
writeln_opaque ( w, & e. ident , & format ! ( "{}" , e. ident) , & e. generics , & e. attrs , types, extra_headers, cpp_headers) ;
932
- types. enum_ignored ( & e. ident ) ;
933
950
return ;
934
951
}
935
952
writeln_docs ( w, & e. attrs , "" ) ;
936
953
937
954
if e. generics . lt_token . is_some ( ) {
938
955
unimplemented ! ( ) ;
939
956
}
940
- types. mirrored_enum_declared ( & e. ident ) ;
941
957
942
958
let mut needs_free = false ;
943
959
@@ -1166,9 +1182,27 @@ fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &mut CrateTypes
1166
1182
1167
1183
let mut type_resolver = TypeResolver :: new ( orig_crate, module, crate_types) ;
1168
1184
1185
+ // First pass over the items and fill in imports and file-declared objects in the type resolver
1169
1186
for item in syntax. items . iter ( ) {
1170
1187
match item {
1171
1188
syn:: Item :: Use ( u) => type_resolver. process_use ( & mut out, & u) ,
1189
+ syn:: Item :: Struct ( s) => {
1190
+ if let syn:: Visibility :: Public ( _) = s. vis {
1191
+ declare_struct ( & s, & mut type_resolver) ;
1192
+ }
1193
+ } ,
1194
+ syn:: Item :: Enum ( e) => {
1195
+ if let syn:: Visibility :: Public ( _) = e. vis {
1196
+ declare_enum ( & e, & mut type_resolver) ;
1197
+ }
1198
+ } ,
1199
+ _ => { } ,
1200
+ }
1201
+ }
1202
+
1203
+ for item in syntax. items . iter ( ) {
1204
+ match item {
1205
+ syn:: Item :: Use ( _) => { } , // Handled above
1172
1206
syn:: Item :: Static ( _) => { } ,
1173
1207
syn:: Item :: Enum ( e) => {
1174
1208
if let syn:: Visibility :: Public ( _) = e. vis {
0 commit comments