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