@@ -3,7 +3,7 @@ use std::ops::Deref;
3
3
use syn:: punctuated:: Punctuated ;
4
4
use syn:: visit:: Visit ;
5
5
6
- use crate :: { Abi , Const , Field , Fn , Parameter , Static , Struct , Type , Union } ;
6
+ use crate :: { Abi , BoxStr , Const , Field , Fn , Parameter , Static , Struct , Type , Union } ;
7
7
8
8
/// Represents a collected set of top-level Rust items relevant to FFI generation or analysis.
9
9
///
@@ -94,6 +94,30 @@ fn collect_fields(fields: &Punctuated<syn::Field, syn::Token![,]>) -> Vec<Field>
94
94
. collect ( )
95
95
}
96
96
97
+ fn extract_single_link_name ( attrs : & [ syn:: Attribute ] ) -> Option < BoxStr > {
98
+ let mut link_name_iter = attrs
99
+ . iter ( )
100
+ . filter ( |attr| attr. path ( ) . is_ident ( "link_name" ) ) ;
101
+
102
+ let link_name = link_name_iter. next ( ) . and_then ( |attr| match & attr. meta {
103
+ syn:: Meta :: NameValue ( nv) => {
104
+ if let syn:: Expr :: Lit ( expr_lit) = & nv. value {
105
+ if let syn:: Lit :: Str ( lit_str) = & expr_lit. lit {
106
+ return Some ( lit_str. value ( ) . into_boxed_str ( ) ) ;
107
+ }
108
+ }
109
+ None
110
+ }
111
+ _ => None ,
112
+ } ) ;
113
+
114
+ if let Some ( attr) = link_name_iter. next ( ) {
115
+ panic ! ( "multiple `#[link_name = ...]` attributes found: {attr:?}" ) ;
116
+ }
117
+
118
+ link_name
119
+ }
120
+
97
121
fn visit_foreign_item_fn ( table : & mut FfiItems , i : & syn:: ForeignItemFn , abi : & Abi ) {
98
122
let public = is_visible ( & i. vis ) ;
99
123
let abi = abi. clone ( ) ;
@@ -121,11 +145,13 @@ fn visit_foreign_item_fn(table: &mut FfiItems, i: &syn::ForeignItemFn, abi: &Abi
121
145
syn:: ReturnType :: Default => None ,
122
146
syn:: ReturnType :: Type ( _, ty) => Some ( ty. deref ( ) . clone ( ) ) ,
123
147
} ;
148
+ let link_name = extract_single_link_name ( & i. attrs ) ;
124
149
125
150
table. foreign_functions . push ( Fn {
126
151
public,
127
152
abi,
128
153
ident,
154
+ link_name,
129
155
parameters,
130
156
return_type,
131
157
} ) ;
@@ -136,11 +162,13 @@ fn visit_foreign_item_static(table: &mut FfiItems, i: &syn::ForeignItemStatic, a
136
162
let abi = abi. clone ( ) ;
137
163
let ident = i. ident . to_string ( ) . into_boxed_str ( ) ;
138
164
let ty = i. ty . deref ( ) . clone ( ) ;
165
+ let link_name = extract_single_link_name ( & i. attrs ) ;
139
166
140
167
table. foreign_statics . push ( Static {
141
168
public,
142
169
abi,
143
170
ident,
171
+ link_name,
144
172
ty,
145
173
} ) ;
146
174
}
0 commit comments