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
use crate::ir::{Identifier, Path, Attributes};
use proc_macro2::TokenStream;
use quote::quote;
use std::convert::TryFrom;
fn get_parameters(attributes: TokenStream) -> (Identifier, Path) {
let attributes = Attributes::try_from(attributes).expect("Couldn't parse attributes.");
let function_identifier = attributes.get_named("name").expect("Procedural macro name not present. e.g.: name = \"ligen_cpp\"");
let function_identifier = Identifier::new(function_identifier.to_string());
let generator_path = attributes.get_named("generator").expect("Generator path not present. e.g.: generator = \"ligen_c_core::Generator\"");
let generator_path: Path = generator_path.to_string().into();
(function_identifier, generator_path)
}
pub fn define_binding_generator(attributes: TokenStream) -> TokenStream {
let (function_identifier, generator_path) = get_parameters(attributes);
let function_signature = quote! {
pub fn #function_identifier(attributes: proc_macro::TokenStream, input: proc_macro::TokenStream) -> proc_macro::TokenStream
};
quote! {
#[cfg(cargo_ligen)]
#[proc_macro_attribute]
#function_signature {
use ligen::proc_macro::prelude::*;
let context = Context::current().expect("Couldn't get context.");
let attributes = attributes.try_into().expect("Failed to parse attributes.");
let implementation = input.clone().try_into().ok();
let mut output: TokenStream = input.into();
let generator = #generator_path::new(&context, &attributes);
let generated = generator.generate(&context, implementation.as_ref()).expect("Generator failed.");
output.append_all(generated);
output.into()
}
#[cfg(not(cargo_ligen))]
#[proc_macro_attribute]
#function_signature {
input
}
}
}
pub fn define_project_generator(attributes: TokenStream) -> TokenStream {
let (function_identifier, generator_path) = get_parameters(attributes);
let function_signature = quote! {
pub fn #function_identifier(attributes: proc_macro::TokenStream) -> proc_macro::TokenStream
};
quote! {
#[cfg(cargo_ligen)]
#[proc_macro]
#function_signature {
use ligen::proc_macro::prelude::*;
let context = Context::current().expect("Couldn't get context.");
let attributes = attributes.try_into().expect("Failed to parse attributes.");
let implementation = None;
let generator = #generator_path::new(&context, &attributes);
let generated = generator.generate(&context, implementation.as_ref()).expect("Generator failed.");
let output = generated;
output.into()
}
#[cfg(not(cargo_ligen))]
#[proc_macro]
#function_signature {
Default::default()
}
}
}