1
1
use std:: { env, fs, io, path:: Path , process} ;
2
2
3
3
use quote:: ToTokens ;
4
- use syn:: { ForeignItem , Item , Type } ;
4
+ use syn:: { self , visit_mut :: VisitMut } ;
5
5
6
- use crate :: link_visitor:: {
7
- syn:: { self , visit_mut:: VisitMut } ,
8
- Link ,
9
- } ;
6
+ use crate :: visitors:: { Link , Sort } ;
10
7
11
8
pub fn generate (
12
9
vita_headers_include : & Path ,
@@ -25,11 +22,12 @@ pub fn generate(
25
22
"Loading vita-headers metadata yaml files from \" {}\" " ,
26
23
db. to_string_lossy( )
27
24
) ;
28
- let mut link = Link :: load ( db, vitasdk_sys_manifest) ;
29
- link. visit_file_mut ( & mut bindings) ;
25
+ let link = Link :: load ( db, vitasdk_sys_manifest) ;
26
+
27
+ link. visit ( & mut bindings) ;
30
28
31
29
// We sort items here so generated bindings don't depend on the included order.
32
- sort_items ( & mut bindings. items ) ;
30
+ Sort . visit_file_mut ( & mut bindings) ;
33
31
34
32
let bindings = bindings. into_token_stream ( ) ;
35
33
@@ -97,97 +95,3 @@ fn generate_preprocessed_bindings(
97
95
98
96
builder. generate ( ) . expect ( "bindgen failed" ) . to_string ( )
99
97
}
100
-
101
- /// Sorts items on alphabetical order based on normalized identifier.
102
- /// Bindgen items will be moved to the start.
103
- fn sort_items ( items : & mut [ Item ] ) {
104
- items. sort_by_cached_key ( |item| {
105
- let ( precedence, ident) =
106
- match item {
107
- Item :: ExternCrate ( i) => ( 0 , i. ident . to_string ( ) ) ,
108
- Item :: Use ( _i) => ( 1 , String :: new ( ) ) ,
109
- Item :: Mod ( i) => ( 2 , i. ident . to_string ( ) ) ,
110
- Item :: Macro ( i) => (
111
- 3 ,
112
- i. ident
113
- . as_ref ( )
114
- . map ( |i| i. to_string ( ) )
115
- . unwrap_or_else ( String :: new) ,
116
- ) ,
117
- Item :: Static ( i) => ( 4 , i. ident . to_string ( ) ) ,
118
- Item :: Const ( i) => ( 4 , i. ident . to_string ( ) ) ,
119
- Item :: Trait ( i) => ( 4 , i. ident . to_string ( ) ) ,
120
- Item :: TraitAlias ( i) => ( 4 , i. ident . to_string ( ) ) ,
121
- Item :: Type ( i) => ( 4 , i. ident . to_string ( ) ) ,
122
- Item :: Enum ( i) => ( 4 , i. ident . to_string ( ) ) ,
123
- Item :: Struct ( i) => ( 4 , i. ident . to_string ( ) ) ,
124
- Item :: Impl ( i) => {
125
- let ident =
126
- match & * i. self_ty {
127
- Type :: Path ( path_type) => path_type. path . segments . iter ( ) . fold (
128
- String :: new ( ) ,
129
- |acc, segment| {
130
- let ident_string = segment. ident . to_string ( ) ;
131
- if acc. is_empty ( ) {
132
- ident_string
133
- } else {
134
- acc + "::" + & ident_string
135
- }
136
- } ,
137
- ) ,
138
- ty => {
139
- log:: warn!( "impl on unexpected type {ty:?}" ) ;
140
- String :: new ( )
141
- }
142
- } ;
143
- ( 4 , ident)
144
- }
145
- Item :: Fn ( i) => ( 4 , i. sig . ident . to_string ( ) ) ,
146
- Item :: ForeignMod ( i) => {
147
- // For `extern` blocks, we use the first item's identifier.
148
- let ident = match i. items . len ( ) {
149
- // Could use link name here. But it's not strictly necessary as they're
150
- // already sorted.
151
- 0 => String :: new ( ) ,
152
- len => {
153
- if len > 1 {
154
- log:: warn!( "Unexpected ForeignMod with multiple items: {i:?}" ) ;
155
- }
156
-
157
- match & i. items [ 0 ] {
158
- ForeignItem :: Fn ( i) => i. sig . ident . to_string ( ) ,
159
- ForeignItem :: Static ( i) => i. ident . to_string ( ) ,
160
- ForeignItem :: Type ( i) => i. ident . to_string ( ) ,
161
- i => {
162
- log:: warn!( "Unexpected item in foreign mod: {i:?}" ) ;
163
- String :: new ( )
164
- }
165
- }
166
- }
167
- } ;
168
- ( 4 , ident)
169
- }
170
- Item :: Union ( i) => ( 4 , i. ident . to_string ( ) ) ,
171
- i => {
172
- log:: warn!( "Unexpected item: {i:?}" ) ;
173
- ( 10 , String :: new ( ) )
174
- }
175
- } ;
176
- consider_bindgen ( ( precedence, normalize_str ( & ident) ) )
177
- } ) ;
178
- }
179
-
180
- fn normalize_str ( input : & str ) -> String {
181
- input. to_lowercase ( ) . replace ( '_' , "" )
182
- }
183
-
184
- fn consider_bindgen ( keys : ( i32 , String ) ) -> ( i32 , String ) {
185
- let ( precedence, ident) = keys;
186
- let new_precedence = if ident. starts_with ( "bindgen" ) {
187
- // Move bindgen items to the start of the file
188
- precedence - 16
189
- } else {
190
- precedence
191
- } ;
192
- ( new_precedence, ident)
193
- }
0 commit comments