2020-08-24 00:53:30 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use quote::{quote, ToTokens};
|
2020-08-01 18:52:26 +02:00
|
|
|
|
2020-09-02 06:15:22 +02:00
|
|
|
use crate::attrs::ExportScope;
|
2020-09-25 18:30:30 +02:00
|
|
|
use crate::function::{ExportedFn, FnSpecialAccess};
|
2020-08-21 05:20:12 +02:00
|
|
|
use crate::module::Module;
|
2020-08-01 18:52:26 +02:00
|
|
|
|
2020-09-13 16:36:24 +02:00
|
|
|
pub(crate) type ExportedConst = (String, Box<syn::Type>, syn::Expr);
|
2020-08-01 18:52:26 +02:00
|
|
|
|
|
|
|
pub(crate) fn generate_body(
|
2020-09-02 06:15:22 +02:00
|
|
|
fns: &mut [ExportedFn],
|
2020-08-24 00:53:30 +02:00
|
|
|
consts: &[ExportedConst],
|
2020-09-02 06:15:22 +02:00
|
|
|
submodules: &mut [Module],
|
|
|
|
parent_scope: &ExportScope,
|
2020-08-01 18:52:26 +02:00
|
|
|
) -> proc_macro2::TokenStream {
|
|
|
|
let mut set_fn_stmts: Vec<syn::Stmt> = Vec::new();
|
|
|
|
let mut set_const_stmts: Vec<syn::Stmt> = Vec::new();
|
2020-08-22 06:05:18 +02:00
|
|
|
let mut add_mod_blocks: Vec<syn::ExprBlock> = Vec::new();
|
2020-09-14 05:40:15 +02:00
|
|
|
let mut set_flattened_mod_blocks: Vec<syn::ExprBlock> = Vec::new();
|
2020-08-01 18:52:26 +02:00
|
|
|
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
|
2020-09-19 12:18:40 +02:00
|
|
|
let string_type_path = syn::parse2::<syn::Path>(quote! { String }).unwrap();
|
2020-08-01 18:52:26 +02:00
|
|
|
|
2020-09-17 05:56:10 +02:00
|
|
|
for (const_name, _, _) in consts {
|
2020-08-01 18:52:26 +02:00
|
|
|
let const_literal = syn::LitStr::new(&const_name, proc_macro2::Span::call_site());
|
2020-09-17 05:56:10 +02:00
|
|
|
let const_ref = syn::Ident::new(&const_name, proc_macro2::Span::call_site());
|
2020-08-02 09:39:08 +02:00
|
|
|
set_const_stmts.push(
|
|
|
|
syn::parse2::<syn::Stmt>(quote! {
|
2020-09-17 05:56:10 +02:00
|
|
|
m.set_var(#const_literal, #const_ref);
|
2020-08-02 09:39:08 +02:00
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 05:20:12 +02:00
|
|
|
for itemmod in submodules {
|
2020-09-02 06:15:22 +02:00
|
|
|
itemmod.update_scope(&parent_scope);
|
2020-08-24 00:53:30 +02:00
|
|
|
if itemmod.skipped() {
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-21 05:20:12 +02:00
|
|
|
let module_name: &syn::Ident = itemmod.module_name().unwrap();
|
|
|
|
let exported_name: syn::LitStr = if let Some(name) = itemmod.exported_name() {
|
|
|
|
syn::LitStr::new(&name, proc_macro2::Span::call_site())
|
|
|
|
} else {
|
|
|
|
syn::LitStr::new(&module_name.to_string(), proc_macro2::Span::call_site())
|
|
|
|
};
|
2020-08-24 00:53:30 +02:00
|
|
|
let cfg_attrs: Vec<&syn::Attribute> = itemmod
|
|
|
|
.attrs()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.filter(|&a| a.path.get_ident().map(|i| *i == "cfg").unwrap_or(false))
|
|
|
|
.collect();
|
2020-08-22 06:05:18 +02:00
|
|
|
add_mod_blocks.push(
|
|
|
|
syn::parse2::<syn::ExprBlock>(quote! {
|
|
|
|
#(#cfg_attrs)* {
|
|
|
|
m.set_sub_module(#exported_name, self::#module_name::rhai_module_generate());
|
|
|
|
}
|
2020-08-21 05:20:12 +02:00
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2020-09-14 05:40:15 +02:00
|
|
|
set_flattened_mod_blocks.push(
|
2020-09-13 16:12:11 +02:00
|
|
|
syn::parse2::<syn::ExprBlock>(quote! {
|
|
|
|
#(#cfg_attrs)* {
|
2020-09-14 05:40:15 +02:00
|
|
|
self::#module_name::rhai_generate_into_module(m, flatten);
|
2020-09-13 16:12:11 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2020-08-21 05:20:12 +02:00
|
|
|
}
|
|
|
|
|
2020-08-01 18:52:26 +02:00
|
|
|
// NB: these are token streams, because reparsing messes up "> >" vs ">>"
|
|
|
|
let mut gen_fn_tokens: Vec<proc_macro2::TokenStream> = Vec::new();
|
|
|
|
for function in fns {
|
2020-09-02 06:15:22 +02:00
|
|
|
function.update_scope(&parent_scope);
|
2020-08-28 06:08:34 +02:00
|
|
|
if function.skipped() {
|
2020-08-24 00:53:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-02 09:39:08 +02:00
|
|
|
let fn_token_name = syn::Ident::new(
|
2020-08-07 01:36:15 +02:00
|
|
|
&format!("{}_token", function.name().to_string()),
|
2020-08-02 09:39:08 +02:00
|
|
|
function.name().span(),
|
|
|
|
);
|
2020-09-13 05:27:54 +02:00
|
|
|
let reg_names = function.exported_names();
|
2020-09-04 05:57:40 +02:00
|
|
|
|
2020-08-02 09:39:08 +02:00
|
|
|
let fn_input_types: Vec<syn::Expr> = function
|
|
|
|
.arg_list()
|
2020-08-01 18:52:26 +02:00
|
|
|
.map(|fnarg| match fnarg {
|
|
|
|
syn::FnArg::Receiver(_) => panic!("internal error: receiver fn outside impl!?"),
|
|
|
|
syn::FnArg::Typed(syn::PatType { ref ty, .. }) => {
|
2020-09-22 16:19:21 +02:00
|
|
|
let arg_type = match flatten_type_groups(ty.as_ref()) {
|
2020-08-24 00:53:30 +02:00
|
|
|
syn::Type::Reference(syn::TypeReference {
|
2020-08-02 09:39:08 +02:00
|
|
|
mutability: None,
|
|
|
|
ref elem,
|
|
|
|
..
|
2020-09-22 16:19:21 +02:00
|
|
|
}) => match flatten_type_groups(elem.as_ref()) {
|
2020-08-24 00:53:30 +02:00
|
|
|
syn::Type::Path(ref p) if p.path == str_type_path => {
|
2020-08-02 09:39:08 +02:00
|
|
|
syn::parse2::<syn::Type>(quote! {
|
2020-08-03 02:27:19 +02:00
|
|
|
ImmutableString })
|
2020-08-02 09:39:08 +02:00
|
|
|
.unwrap()
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
2020-08-02 09:39:08 +02:00
|
|
|
_ => panic!("internal error: non-string shared reference!?"),
|
2020-08-01 18:52:26 +02:00
|
|
|
},
|
2020-09-19 12:18:40 +02:00
|
|
|
syn::Type::Path(ref p) if p.path == string_type_path => {
|
|
|
|
syn::parse2::<syn::Type>(quote! {
|
|
|
|
ImmutableString })
|
|
|
|
.unwrap()
|
|
|
|
}
|
2020-08-24 00:53:30 +02:00
|
|
|
syn::Type::Reference(syn::TypeReference {
|
2020-08-02 09:39:08 +02:00
|
|
|
mutability: Some(_),
|
|
|
|
ref elem,
|
|
|
|
..
|
2020-09-22 16:19:21 +02:00
|
|
|
}) => match flatten_type_groups(elem.as_ref()) {
|
2020-08-24 00:53:30 +02:00
|
|
|
syn::Type::Path(ref p) => syn::parse2::<syn::Type>(quote! {
|
2020-08-02 09:39:08 +02:00
|
|
|
#p })
|
|
|
|
.unwrap(),
|
2020-09-22 15:29:44 +02:00
|
|
|
_ => panic!("internal error: invalid mutable reference!?"),
|
2020-08-01 18:52:26 +02:00
|
|
|
},
|
|
|
|
t => t.clone(),
|
|
|
|
};
|
|
|
|
syn::parse2::<syn::Expr>(quote! {
|
2020-08-02 09:39:08 +02:00
|
|
|
core::any::TypeId::of::<#arg_type>()})
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2020-08-01 18:52:26 +02:00
|
|
|
|
2020-09-13 05:27:54 +02:00
|
|
|
for fn_literal in reg_names {
|
2020-09-04 05:57:40 +02:00
|
|
|
set_fn_stmts.push(
|
|
|
|
syn::parse2::<syn::Stmt>(quote! {
|
|
|
|
m.set_fn(#fn_literal, FnAccess::Public, &[#(#fn_input_types),*],
|
2020-09-29 07:06:48 +02:00
|
|
|
#fn_token_name().into());
|
2020-09-04 05:57:40 +02:00
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
}
|
2020-08-01 18:52:26 +02:00
|
|
|
|
|
|
|
gen_fn_tokens.push(quote! {
|
|
|
|
#[allow(non_camel_case_types)]
|
2020-08-03 02:27:19 +02:00
|
|
|
struct #fn_token_name();
|
2020-08-01 18:52:26 +02:00
|
|
|
});
|
|
|
|
gen_fn_tokens.push(function.generate_impl(&fn_token_name.to_string()));
|
2020-08-03 02:27:19 +02:00
|
|
|
gen_fn_tokens.push(function.generate_callable(&fn_token_name.to_string()));
|
|
|
|
gen_fn_tokens.push(function.generate_input_types(&fn_token_name.to_string()));
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut generate_fncall = syn::parse2::<syn::ItemMod>(quote! {
|
|
|
|
pub mod generate_info {
|
|
|
|
#[allow(unused_imports)]
|
2020-08-03 02:27:19 +02:00
|
|
|
use super::*;
|
2020-09-14 05:40:15 +02:00
|
|
|
|
2020-08-06 08:10:27 +02:00
|
|
|
pub fn rhai_module_generate() -> Module {
|
2020-08-01 18:52:26 +02:00
|
|
|
let mut m = Module::new();
|
2020-09-14 05:40:15 +02:00
|
|
|
rhai_generate_into_module(&mut m, false);
|
2020-08-01 18:52:26 +02:00
|
|
|
m
|
|
|
|
}
|
2020-09-14 05:40:15 +02:00
|
|
|
#[allow(unused_mut)]
|
|
|
|
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
2020-09-13 16:12:11 +02:00
|
|
|
#(#set_fn_stmts)*
|
|
|
|
#(#set_const_stmts)*
|
2020-09-14 05:40:15 +02:00
|
|
|
|
|
|
|
if flatten {
|
|
|
|
#(#set_flattened_mod_blocks)*
|
|
|
|
} else {
|
|
|
|
#(#add_mod_blocks)*
|
|
|
|
}
|
2020-09-13 16:12:11 +02:00
|
|
|
}
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
2020-08-02 09:39:08 +02:00
|
|
|
})
|
|
|
|
.unwrap();
|
2020-08-01 18:52:26 +02:00
|
|
|
|
|
|
|
let (_, generate_call_content) = generate_fncall.content.take().unwrap();
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
#(#generate_call_content)*
|
|
|
|
#(#gen_fn_tokens)*
|
|
|
|
}
|
|
|
|
}
|
2020-08-24 00:53:30 +02:00
|
|
|
|
2020-09-22 16:19:21 +02:00
|
|
|
pub(crate) fn flatten_type_groups(ty: &syn::Type) -> &syn::Type {
|
|
|
|
match ty {
|
|
|
|
syn::Type::Group(syn::TypeGroup { ref elem, .. })
|
|
|
|
| syn::Type::Paren(syn::TypeParen { ref elem, .. }) => flatten_type_groups(elem.as_ref()),
|
|
|
|
_ => ty,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
pub(crate) fn check_rename_collisions(fns: &Vec<ExportedFn>) -> Result<(), syn::Error> {
|
2020-09-26 06:08:15 +02:00
|
|
|
fn make_key(name: impl ToString, itemfn: &ExportedFn) -> String {
|
|
|
|
itemfn
|
|
|
|
.arg_list()
|
|
|
|
.fold(name.to_string(), |mut argstr, fnarg| {
|
|
|
|
let type_string: String = match fnarg {
|
|
|
|
syn::FnArg::Receiver(_) => unimplemented!("receiver rhai_fns not implemented"),
|
|
|
|
syn::FnArg::Typed(syn::PatType { ref ty, .. }) => {
|
|
|
|
ty.as_ref().to_token_stream().to_string()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
argstr.push('.');
|
|
|
|
argstr.push_str(&type_string);
|
|
|
|
argstr
|
|
|
|
})
|
2020-09-25 17:07:18 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
let mut renames = HashMap::<String, proc_macro2::Span>::new();
|
2020-09-26 06:08:15 +02:00
|
|
|
let mut fn_defs = HashMap::<String, proc_macro2::Span>::new();
|
2020-09-25 17:07:18 +02:00
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
for itemfn in fns.iter() {
|
2020-09-25 18:30:30 +02:00
|
|
|
if itemfn.params().name.is_some() || itemfn.params().special != FnSpecialAccess::None {
|
|
|
|
let mut names = itemfn
|
|
|
|
.params()
|
|
|
|
.name
|
|
|
|
.as_ref()
|
|
|
|
.map(|v| v.iter().map(|n| (n.clone(), n.clone())).collect())
|
|
|
|
.unwrap_or_else(|| Vec::new());
|
|
|
|
|
|
|
|
if let Some((s, n, _)) = itemfn.params().special.get_fn_name() {
|
|
|
|
names.push((s, n));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (name, fn_name) in names {
|
2020-09-04 05:57:40 +02:00
|
|
|
let current_span = itemfn.params().span.as_ref().unwrap();
|
2020-09-26 06:08:15 +02:00
|
|
|
let key = make_key(&name, itemfn);
|
2020-09-04 05:57:40 +02:00
|
|
|
if let Some(other_span) = renames.insert(key, *current_span) {
|
|
|
|
let mut err = syn::Error::new(
|
|
|
|
*current_span,
|
2020-09-25 18:30:30 +02:00
|
|
|
format!("duplicate Rhai signature for '{}'", &fn_name),
|
2020-09-04 05:57:40 +02:00
|
|
|
);
|
|
|
|
err.combine(syn::Error::new(
|
|
|
|
other_span,
|
2020-09-25 18:30:30 +02:00
|
|
|
format!("duplicated function renamed '{}'", &fn_name),
|
2020-09-04 05:57:40 +02:00
|
|
|
));
|
|
|
|
return Err(err);
|
|
|
|
}
|
2020-08-24 00:53:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let ident = itemfn.name();
|
2020-09-26 06:08:15 +02:00
|
|
|
if let Some(other_span) = fn_defs.insert(ident.to_string(), ident.span()) {
|
2020-08-28 05:26:05 +02:00
|
|
|
let mut err = syn::Error::new(
|
|
|
|
ident.span(),
|
|
|
|
format!("duplicate function '{}'", ident.to_string()),
|
|
|
|
);
|
|
|
|
err.combine(syn::Error::new(
|
|
|
|
other_span,
|
|
|
|
format!("duplicated function '{}'", ident.to_string()),
|
|
|
|
));
|
|
|
|
return Err(err);
|
|
|
|
}
|
2020-09-26 06:08:15 +02:00
|
|
|
let key = make_key(ident, itemfn);
|
|
|
|
if let Some(fn_span) = renames.get(&key) {
|
|
|
|
let mut err = syn::Error::new(
|
|
|
|
ident.span(),
|
|
|
|
format!("duplicate Rhai signature for '{}'", &ident),
|
|
|
|
);
|
|
|
|
err.combine(syn::Error::new(
|
|
|
|
*fn_span,
|
|
|
|
format!("duplicated function '{}'", &ident),
|
|
|
|
));
|
|
|
|
return Err(err);
|
|
|
|
}
|
2020-08-24 00:53:30 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-25 17:07:18 +02:00
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
Ok(())
|
|
|
|
}
|