rhai/codegen/src/rhai_module.rs

314 lines
11 KiB
Rust
Raw Normal View History

2021-10-20 07:36:40 +02:00
use proc_macro2::{Span, TokenStream};
2021-10-20 09:30:11 +02:00
use quote::{quote, ToTokens};
2020-08-01 18:52:26 +02:00
2021-10-20 07:36:40 +02:00
use std::collections::BTreeMap;
use crate::attrs::ExportScope;
2020-11-22 10:21:34 +01:00
use crate::function::{
flatten_type_groups, print_type, ExportedFn, FnNamespaceAccess, FnSpecialAccess, FN_GET,
FN_IDX_GET, FN_IDX_SET, FN_SET,
2020-11-22 10:21:34 +01:00
};
use crate::module::Module;
2020-08-01 18:52:26 +02:00
2021-10-20 09:30:11 +02:00
pub type ExportedConst = (String, Box<syn::Type>, syn::Expr, Vec<syn::Attribute>);
2020-08-01 18:52:26 +02:00
2021-02-21 07:11:19 +01:00
pub fn generate_body(
fns: &mut [ExportedFn],
2020-08-24 00:53:30 +02:00
consts: &[ExportedConst],
2021-02-18 10:42:49 +01:00
sub_modules: &mut [Module],
parent_scope: &ExportScope,
2021-10-20 07:36:40 +02:00
) -> TokenStream {
let mut set_fn_statements = Vec::new();
let mut set_const_statements = Vec::new();
let mut add_mod_blocks = Vec::new();
let mut set_flattened_mod_blocks = 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
2021-10-20 09:30:11 +02:00
for (const_name, _, _, cfg_attrs) in consts {
2021-10-20 07:36:40 +02:00
let const_literal = syn::LitStr::new(&const_name, Span::call_site());
let const_ref = syn::Ident::new(&const_name, Span::call_site());
2021-10-20 09:30:11 +02:00
let cfg_attrs: Vec<_> = cfg_attrs
.iter()
.map(syn::Attribute::to_token_stream)
.collect();
2021-02-18 10:42:49 +01:00
set_const_statements.push(
2020-08-02 09:39:08 +02:00
syn::parse2::<syn::Stmt>(quote! {
2021-10-20 09:30:11 +02:00
#(#cfg_attrs)*
m.set_var(#const_literal, #const_ref);
2020-08-02 09:39:08 +02:00
})
.unwrap(),
);
2020-08-01 18:52:26 +02:00
}
2021-02-18 10:42:49 +01:00
for item_mod in sub_modules {
item_mod.update_scope(&parent_scope);
if item_mod.skipped() {
2020-08-24 00:53:30 +02:00
continue;
}
2021-02-18 10:42:49 +01:00
let module_name = item_mod.module_name();
2021-10-20 09:30:11 +02:00
let exported_name = syn::LitStr::new(item_mod.exported_name().as_ref(), Span::call_site());
let cfg_attrs = crate::attrs::collect_cfg_attr(item_mod.attrs());
2020-08-22 06:05:18 +02:00
add_mod_blocks.push(
syn::parse2::<syn::ExprBlock>(quote! {
2021-03-29 06:46:46 +02:00
{
#(#cfg_attrs)*
2020-08-22 06:05:18 +02:00
m.set_sub_module(#exported_name, self::#module_name::rhai_module_generate());
}
})
.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! {
2021-03-29 06:46:46 +02:00
{
#(#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(),
);
}
// NB: these are token streams, because re-parsing messes up "> >" vs ">>"
2021-10-20 07:36:40 +02:00
let mut gen_fn_tokens = Vec::new();
2020-08-01 18:52:26 +02:00
for function in fns {
function.update_scope(&parent_scope);
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(
2021-10-23 06:28:42 +02:00
&format!("{}_token", function.name()),
2020-08-02 09:39:08 +02:00
function.name().span(),
);
let reg_names = function.exported_names();
2020-09-04 05:57:40 +02:00
2021-10-20 07:36:40 +02:00
let fn_input_types: Vec<_> = function
2020-08-02 09:39:08 +02:00
.arg_list()
2021-02-18 10:42:49 +01:00
.map(|fn_arg| match fn_arg {
2020-08-01 18:52:26 +02:00
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(),
_ => panic!("internal error: invalid mutable reference!?"),
2020-08-01 18:52:26 +02:00
},
t => t.clone(),
};
2020-11-22 10:21:34 +01:00
2020-08-01 18:52:26 +02:00
syn::parse2::<syn::Expr>(quote! {
TypeId::of::<#arg_type>()})
2020-08-02 09:39:08 +02:00
.unwrap()
}
})
.collect();
2020-08-01 18:52:26 +02:00
2021-10-20 09:30:11 +02:00
let cfg_attrs: Vec<_> = function
.cfg_attrs()
.iter()
.map(syn::Attribute::to_token_stream)
.collect();
for fn_literal in reg_names {
let mut namespace = FnNamespaceAccess::Internal;
match function.params().special {
FnSpecialAccess::None => (),
FnSpecialAccess::Index(_) | FnSpecialAccess::Property(_) => {
let reg_name = fn_literal.value();
if reg_name.starts_with(FN_GET)
|| reg_name.starts_with(FN_SET)
|| reg_name == FN_IDX_GET
|| reg_name == FN_IDX_SET
{
namespace = FnNamespaceAccess::Global;
}
}
}
2020-12-24 14:28:40 +01:00
match function.params().namespace {
FnNamespaceAccess::Unset => (),
ns => namespace = ns,
}
2020-11-17 07:29:28 +01:00
let ns_str = syn::Ident::new(
match namespace {
2020-12-24 14:28:40 +01:00
FnNamespaceAccess::Unset => unreachable!(),
2020-11-17 07:29:28 +01:00
FnNamespaceAccess::Global => "Global",
FnNamespaceAccess::Internal => "Internal",
},
fn_literal.span(),
);
#[cfg(feature = "metadata")]
2021-12-21 15:16:03 +01:00
let (param_names, comments) = (
quote! { Some(#fn_token_name::PARAM_NAMES) },
2021-12-21 15:16:03 +01:00
function
.comments()
.iter()
.map(|s| syn::LitStr::new(s, Span::call_site()))
.collect::<Vec<_>>(),
);
#[cfg(not(feature = "metadata"))]
2021-12-21 15:16:03 +01:00
let (param_names, comments) = (quote! { None }, Vec::<syn::LitStr>::new());
2021-12-21 15:16:03 +01:00
set_fn_statements.push(if comments.is_empty() {
2020-11-17 07:29:28 +01:00
syn::parse2::<syn::Stmt>(quote! {
2021-10-20 09:30:11 +02:00
#(#cfg_attrs)*
2021-03-26 11:41:28 +01:00
m.set_fn(#fn_literal, FnNamespace::#ns_str, FnAccess::Public,
#param_names, &[#(#fn_input_types),*], #fn_token_name().into());
2020-11-17 07:29:28 +01:00
})
.unwrap()
} else {
syn::parse2::<syn::Stmt>(quote! {
#(#cfg_attrs)*
2021-12-21 15:16:03 +01:00
m.set_fn_with_comments(#fn_literal, FnNamespace::#ns_str, FnAccess::Public,
#param_names, &[#(#fn_input_types),*], &[#(#comments),*], #fn_token_name().into());
})
.unwrap()
});
2020-09-04 05:57:40 +02:00
}
2020-08-01 18:52:26 +02:00
gen_fn_tokens.push(quote! {
2021-10-20 09:30:11 +02:00
#(#cfg_attrs)*
2020-08-01 18:52:26 +02:00
#[allow(non_camel_case_types)]
pub struct #fn_token_name();
2020-08-01 18:52:26 +02:00
});
2021-10-20 09:30:11 +02:00
2020-08-01 18:52:26 +02:00
gen_fn_tokens.push(function.generate_impl(&fn_token_name.to_string()));
}
2021-02-18 10:42:49 +01:00
let mut generate_fn_call = syn::parse2::<syn::ItemMod>(quote! {
2020-08-01 18:52:26 +02:00
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
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-11-21 15:18:32 +01:00
m.build_index();
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) {
2021-02-18 10:42:49 +01:00
#(#set_fn_statements)*
#(#set_const_statements)*
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
2021-02-18 10:42:49 +01:00
let (_, generate_call_content) = generate_fn_call.content.take().unwrap();
2020-08-01 18:52:26 +02:00
quote! {
#(#generate_call_content)*
#(#gen_fn_tokens)*
}
}
2020-08-24 00:53:30 +02:00
2021-07-24 08:11:16 +02:00
pub fn check_rename_collisions(fns: &[ExportedFn]) -> Result<(), syn::Error> {
2021-02-18 10:42:49 +01:00
fn make_key(name: impl ToString, item_fn: &ExportedFn) -> String {
item_fn
2020-09-26 06:08:15 +02:00
.arg_list()
2021-02-18 10:42:49 +01:00
.fold(name.to_string(), |mut arg_str, fn_arg| {
let type_string: String = match fn_arg {
2020-09-26 06:08:15 +02:00
syn::FnArg::Receiver(_) => unimplemented!("receiver rhai_fns not implemented"),
2020-11-22 10:21:34 +01:00
syn::FnArg::Typed(syn::PatType { ref ty, .. }) => print_type(ty),
2020-09-26 06:08:15 +02:00
};
2021-02-18 10:42:49 +01:00
arg_str.push('.');
arg_str.push_str(&type_string);
arg_str
2020-09-26 06:08:15 +02:00
})
}
2021-10-20 07:36:40 +02:00
let mut renames = BTreeMap::new();
let mut fn_defs = BTreeMap::new();
2021-02-18 10:42:49 +01:00
for item_fn in fns.iter() {
if !item_fn.params().name.is_empty() || item_fn.params().special != FnSpecialAccess::None {
let mut names: Vec<_> = item_fn
.params()
.name
2020-12-23 16:29:19 +01:00
.iter()
.map(|n| (n.clone(), n.clone()))
.collect();
2021-02-18 10:42:49 +01:00
if let Some((s, n, _)) = item_fn.params().special.get_fn_name() {
names.push((s, n));
}
for (name, fn_name) in names {
2021-02-18 10:42:49 +01:00
let current_span = item_fn.params().span.unwrap();
let key = make_key(&name, item_fn);
2020-12-24 14:28:40 +01:00
if let Some(other_span) = renames.insert(key, current_span) {
2020-09-04 05:57:40 +02:00
let mut err = syn::Error::new(
2020-12-24 14:28:40 +01:00
current_span,
2021-10-23 06:28:42 +02:00
format!("duplicate Rhai signature for '{}'", fn_name),
2020-09-04 05:57:40 +02:00
);
err.combine(syn::Error::new(
other_span,
2021-10-23 06:28:42 +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 {
2021-02-18 10:42:49 +01:00
let ident = item_fn.name();
2020-09-26 06:08:15 +02:00
if let Some(other_span) = fn_defs.insert(ident.to_string(), ident.span()) {
2021-10-23 06:28:42 +02:00
let mut err =
syn::Error::new(ident.span(), format!("duplicate function '{}'", ident));
err.combine(syn::Error::new(
other_span,
2021-10-23 06:28:42 +02:00
format!("duplicated function '{}'", ident),
));
return Err(err);
}
2021-02-18 10:42:49 +01:00
let key = make_key(ident, item_fn);
2020-09-26 06:08:15 +02:00
if let Some(fn_span) = renames.get(&key) {
let mut err = syn::Error::new(
ident.span(),
2021-10-23 06:28:42 +02:00
format!("duplicate Rhai signature for '{}'", ident),
2020-09-26 06:08:15 +02:00
);
err.combine(syn::Error::new(
*fn_span,
2021-10-23 06:28:42 +02:00
format!("duplicated function '{}'", ident),
2020-09-26 06:08:15 +02:00
));
return Err(err);
}
2020-08-24 00:53:30 +02:00
}
}
2020-08-24 00:53:30 +02:00
Ok(())
}