1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#![recursion_limit="192"]
#![deny(unreachable_patterns)]
#![deny(unused_doc_comments)]
#![deny(unconditional_recursion)]
extern crate proc_macro;
#[proc_macro_derive(StableAbi, attributes(sabi))]
pub fn derive_stable_abi(input: TokenStream1) -> TokenStream1 {
parse_or_compile_err( input, stable_abi::derive ).into()
}
#[doc(hidden)]
#[proc_macro]
#[allow(non_snake_case)]
pub fn impl_InterfaceType(input: TokenStream1) -> TokenStream1 {
parse_or_compile_err( input, impl_interfacetype::the_macro ).into()
}
#[proc_macro_attribute]
pub fn export_root_module(attr: TokenStream1, item: TokenStream1) -> TokenStream1 {
crate::mangle_library_getter::mangle_library_getter_attr(attr,item)
}
#[proc_macro_attribute]
pub fn sabi_extern_fn(attr: TokenStream1, item: TokenStream1) -> TokenStream1 {
crate::sabi_extern_fn_impl::sabi_extern_fn(attr,item)
}
#[proc_macro_attribute]
pub fn sabi_trait(_attr: TokenStream1, item: TokenStream1) -> TokenStream1 {
parse_or_compile_err( item, sabi_trait::derive_sabi_trait ).into()
}
#[doc(hidden)]
#[proc_macro]
pub fn concatenated_and_ranges( input: TokenStream1) -> TokenStream1 {
parse_or_compile_err( input, concat_and_ranges::macro_impl ).into()
}
#[proc_macro_derive(GetStaticEquivalent, attributes(sabi))]
pub fn derive_get_static_equivalent(input: TokenStream1) -> TokenStream1 {
parse_or_compile_err( input, get_static_equivalent::derive ).into()
}
#[doc(hidden)]
#[proc_macro]
pub fn get_string_length(input: TokenStream1) -> TokenStream1 {
parse_or_compile_err(input,|lit:syn::LitStr|{
let len=lit.value().len();
Ok(quote!( pub(super) const LEN:usize=#len; ))
}).into()
}
#[doc(hidden)]
#[proc_macro]
pub fn construct_abi_header(_: TokenStream1) -> TokenStream1 {
let abi_major=env!("CARGO_PKG_VERSION_MAJOR").parse::<u32>().unwrap();
let abi_minor=env!("CARGO_PKG_VERSION_MINOR").parse::<u32>().unwrap();
quote!(
pub const ABI_HEADER:AbiHeader=AbiHeader{
magic_string:*b"abi stable library for Rust ",
abi_major:#abi_major,
abi_minor:#abi_minor,
_priv:(),
};
).into()
}
#[doc(hidden)]
#[proc_macro]
pub fn get_root_module_static(_: TokenStream1) -> TokenStream1 {
let export_name=syn::Ident::new(
&abi_stable_shared::mangled_root_module_loader_name(),
proc_macro2::Span::call_site(),
);
quote!( crate::#export_name ).into()
}
#[macro_use]
mod utils;
mod arenas;
mod attribute_parsing;
mod concat_and_ranges;
mod common_tokens;
mod composite_collections;
mod constants;
mod fn_pointer_extractor;
mod get_static_equivalent;
mod ignored_wrapper;
mod impl_interfacetype;
mod lifetimes;
mod literals_constructors;
mod mangle_library_getter;
mod my_visibility;
mod parse_utils;
mod sabi_extern_fn_impl;
mod set_span_visitor;
mod workaround;
#[cfg(test)]
mod input_code_range_tests;
#[cfg(test)]
mod test_framework;
#[doc(hidden)]
pub(crate) mod stable_abi;
#[doc(hidden)]
pub(crate) mod sabi_trait;
use proc_macro::TokenStream as TokenStream1;
use proc_macro2::TokenStream as TokenStream2;
use syn::{DeriveInput,ItemFn};
use quote::{quote, ToTokens, quote_spanned};
#[allow(unused_imports)]
use core_extensions::prelude::*;
#[allow(unused_imports)]
use crate::{
arenas::{AllocMethods, Arenas},
utils::PrintDurationOnDrop,
};
#[cfg(test)]
pub(crate) fn derive_stable_abi_from_str(s: &str) -> Result<TokenStream2,syn::Error> {
syn::parse_str(s)
.and_then(stable_abi::derive)
}
#[cfg(test)]
pub(crate) fn derive_sabi_trait_str(item: &str) -> Result<TokenStream2,syn::Error> {
syn::parse_str(item)
.and_then(sabi_trait::derive_sabi_trait)
}
fn parse_or_compile_err<P,F>(input:TokenStream1,f:F)->TokenStream2
where
P:syn::parse::Parse,
F:FnOnce(P)->Result<TokenStream2,syn::Error>
{
syn::parse::<P>(input)
.and_then(f)
.unwrap_or_else(|e| e.to_compile_error() )
}