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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use std::collections::{hash_map::DefaultHasher, HashMap};
use std::fs;
use std::hash::{Hash, Hasher};

use proc_macro2::Span;
use quote::{quote, ToTokens};
use syn::parse::{Parse, ParseStream, Result as ParseResult};
use syn::spanned::Spanned as _;
use syn::{
    self, parse_quote, token::Brace, Attribute, Expr, FnArg, ForeignItem, ForeignItemFn, Ident,
    Item, ItemForeignMod, ItemMod, Lit, LitByteStr, LitStr, Meta, MetaNameValue, Pat, Path, Token,
    Type,
};

use args::{Args, Input};

mod args;

fn fnarg_type(arg: &FnArg) -> Option<&Type> {
    match arg {
        FnArg::Typed(ty) => Some(&ty.ty),
        _ => None,
    }
}

fn fnarg_pat(arg: &FnArg) -> Option<&Pat> {
    match arg {
        FnArg::Typed(ty) => Some(&ty.pat),
        _ => None,
    }
}

fn sym(fun: &ForeignItemFn) -> String {
    let attrs = &fun.attrs;
    for attr in attrs {
        match attr.parse_meta() {
            Ok(Meta::NameValue(MetaNameValue {
                path,
                lit: Lit::Str(s),
                ..
            })) if path.is_ident("link_name") => return s.value(),
            _ => {}
        }
    }
    fun.sig.ident.to_string()
}

fn inheritable_attrs(attrs: &[Attribute]) -> Vec<Attribute> {
    attrs
        .iter()
        .filter(|attr| attr.path.is_ident("cfg") || attr.path.is_ident("doc"))
        .cloned()
        .collect()
}

fn convert_foreign_fn(input: &Input, uniq: u64, funs: &[ForeignItemFn]) -> Vec<Item> {
    let krate: Ident = parse_quote! { lazylink };

    let libloading: Path = parse_quote! {
        #krate::libloading
    };

    let libname: Expr = match input {
        Input::Empty => {
            parse_quote!(compile_error!("no name specified")) // FIXME
        }
        Input::Name(name) => {
            let name = LitStr::new(&name, Span::call_site());
            parse_quote! { #libloading::library_filename(#name) }
        }
        Input::FullName(name) => {
            let name = LitStr::new(&name, Span::call_site());
            parse_quote! { #name }
        }
    };

    let struct_name = Ident::new(&format!("__LazyLink{:x}", uniq), Span::call_site());

    let mut idents = vec![];
    let mut tys = vec![] as Vec<Type>;
    let mut syms = vec![];
    let mut attrs = vec![];

    for fun in funs {
        let sig = &fun.sig;
        let ident = &sig.ident;
        let argtys = sig.inputs.iter().filter_map(fnarg_type);
        let output = &sig.output;

        idents.push(ident);
        tys.push(parse_quote! {
            #libloading::Symbol<'a, unsafe extern "C" fn(#(#argtys),*) #output>
        });
        syms.push(LitByteStr::new(
            format!("{}\0", sym(fun)).as_ref(),
            Span::call_site(),
        ));
        attrs.push(
            fun.attrs
                .iter()
                .filter(|attr| !attr.path.is_ident("link_name"))
                .collect::<Vec<_>>(),
        );
    }

    let mut result = vec![
        parse_quote! {
            struct #struct_name<'a> {
                #(#(#attrs)* #idents: #tys,)*
                _phantom: std::marker::PhantomData<fn() -> &'a ()>,
            }
        },
        parse_quote! {
            impl<'a> #struct_name<'a> {
                unsafe fn new(lib: &'a #libloading::Library)
                -> Result<Self, #libloading::Error> {
                    Ok(Self {
                        #(#(#attrs)* #idents: lib.get(#syms)?,)*
                        _phantom: std::marker::PhantomData,
                    })
                }

                fn get() -> &'static #struct_name<'static> {
                    static mut LIB: Option<#libloading::Library> = None;
                    static mut FNS: Option<#struct_name<'static>> = None;
                    static ONCE: std::sync::Once = std::sync::Once::new();

                    ONCE.call_once(|| {
                        unsafe {
                            LIB = Some(#libloading::Library::new(#libname).unwrap());
                            FNS = Some(#struct_name::new(LIB.as_ref().unwrap()).unwrap());
                        }
                    });
                    unsafe {
                        FNS.as_ref().unwrap()
                    }
                }
            }
        },
    ];

    result.extend(funs.iter().map(|fun| {
        let attrs = fun
            .attrs
            .iter()
            .filter(|attr| !attr.path.is_ident("link_name"))
            .collect::<Vec<_>>();
        let vis = &fun.vis;
        let mut sig = fun.sig.clone();
        let ident = &sig.ident;
        let args = sig.inputs.iter().filter_map(fnarg_pat);
        sig.unsafety = Some(parse_quote! {unsafe});

        parse_quote! {
            #(#attrs)*
            #vis #sig {
                (#struct_name::get().#ident)(#(#args),*)
            }
        }
    }));

    result
}

fn take_foreign_items(
    iter: impl IntoIterator<Item = Item>,
    input: &Input,
    foreign_items: &mut HashMap<Input, Vec<ForeignItemFn>>,
) -> syn::Result<Vec<Item>> {
    let mut result = vec![];

    for item in iter {
        match item {
            Item::ForeignMod(foreign_mod) => {
                let input = input.with_link_attr(&foreign_mod.attrs);
                let attrs = foreign_mod.attrs;
                for item in foreign_mod.items {
                    match item {
                        ForeignItem::Fn(mut fun) => {
                            let mut attrs = inheritable_attrs(&attrs);
                            attrs.extend(fun.attrs);
                            fun.attrs = attrs;
                            foreign_items.entry(input.clone()).or_default().push(fun)
                        }
                        e => {
                            return Err(syn::Error::new(
                                e.span(),
                                "currently supported extern fn only",
                            ))
                        }
                    }
                }
            }
            item => result.push(item),
        }
    }

    Ok(result)
}

#[derive(Debug)]
enum Target {
    Mod(ItemMod),
    ForeignMod(ItemForeignMod),
}

impl Parse for Target {
    fn parse(input: ParseStream) -> ParseResult<Self> {
        let attrs = input.call(Attribute::parse_outer)?;

        let lookahead = input.lookahead1();
        if lookahead.peek(Token![mod]) {
            input.parse::<ItemMod>().map(|mut item| {
                item.attrs = attrs;
                Self::Mod(item)
            })
        } else if lookahead.peek(Token![extern]) {
            input.parse::<ItemForeignMod>().map(|mut item| {
                item.attrs = attrs;
                Self::ForeignMod(item)
            })
        } else {
            Err(lookahead.error())
        }
    }
}

impl Target {
    fn proc(self, args: &Args) -> syn::Result<proc_macro2::TokenStream> {
        match self {
            Self::Mod(item) => proc_mod(item, args),
            Self::ForeignMod(item) => proc_foreign_mod(item, args),
        }
    }
}

fn proc_mod(mut target: ItemMod, args: &Args) -> syn::Result<proc_macro2::TokenStream> {
    let Args { input, include } = args;

    match (include, target.content.take()) {
        (Some((path, span)), Some((brace, ref items))) => {
            if !items.is_empty() {
                return Err(syn::Error::new(
                    items.iter().next().span(),
                    "include but item already exists.",
                ));
            }

            let code = fs::read_to_string(&path)
                .map_err(|e| syn::Error::new(*span, format!("{} {:?}", e, path)))?;
            let file = syn::parse_file(&code).map_err(|e| syn::Error::new(*span, e))?;
            target.content = Some((brace, file.items));
        }

        (Some((path, span)), None) => {
            let code = fs::read_to_string(&path).map_err(|e| syn::Error::new(*span, e))?;
            let file = syn::parse_file(&code).map_err(|e| syn::Error::new(*span, e))?;
            target.content = Some((Brace(Span::call_site()), file.items));
        }

        (None, items) => target.content = items,
    }

    if let Some((brace, items)) = target.content.take() {
        let mut foreign_items = HashMap::new();
        let mut items = take_foreign_items(items, &input, &mut foreign_items)?;
        for (input, funs) in foreign_items {
            let mut hasher = DefaultHasher::default();
            input.hash(&mut hasher);
            items.extend(convert_foreign_fn(&input, hasher.finish(), &funs))
        }
        target.content = Some((brace, items));
    }
    Ok(target.into_token_stream())
}

fn proc_foreign_mod(
    foreign_mod: ItemForeignMod,
    args: &Args,
) -> syn::Result<proc_macro2::TokenStream> {
    let Args { input, include, .. } = args;

    if let Some((_, span)) = include {
        return Err(syn::Error::new(*span, "can not include extern block."));
    }

    let mut foreign_items = HashMap::new();
    let mut items = take_foreign_items(vec![foreign_mod.into()], &input, &mut foreign_items)?;
    for (input, funs) in foreign_items {
        let mut hasher = DefaultHasher::default();
        input.hash(&mut hasher);
        items.extend(convert_foreign_fn(&input, hasher.finish(), &funs))
    }
    Ok(quote! {
        #(#items)*
    })
}

/// Convert extern fn to libdl function call.
///
/// # Parameters
///
/// - name (or omit attr name) ... Calling library short name. e.g.) z for libz.so
/// - fullname ... Calling library full name. e.g.) libz.so
/// - include ... module including item source code location. relative by CARGO_MANIFEST_DIR.
/// - include_outdir ... module including item source code location. relative by OUT_DIR.
/// (typically for bindgen)
#[proc_macro_attribute]
pub fn lazylink(
    attr: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let original = proc_macro2::TokenStream::from(item.clone());
    let args = syn::parse_macro_input!(attr as Args);
    let target = syn::parse_macro_input!(item as Target);

    match target.proc(&args) {
        Ok(tokens) => tokens.into(),
        Err(e) => {
            let compile_error = e.to_compile_error();
            (quote! {
                #compile_error
                #original
            })
            .into()
        }
    }
}