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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use std::collections::BTreeMap;
use crate::attrs::ExportScope;
use crate::function::{
flatten_type_groups, print_type, ExportedFn, FnNamespaceAccess, FnSpecialAccess, FN_GET,
FN_IDX_GET, FN_IDX_SET, FN_SET,
};
use crate::module::Module;
#[derive(Debug)]
pub struct ExportedConst {
pub name: String,
pub typ: Box<syn::Type>,
pub expr: syn::Expr,
pub cfg_attrs: Vec<syn::Attribute>,
}
#[derive(Debug)]
pub struct ExportedType {
pub name: String,
pub typ: Box<syn::Type>,
pub cfg_attrs: Vec<syn::Attribute>,
}
pub fn generate_body(
doc: &str,
fns: &mut [ExportedFn],
consts: &[ExportedConst],
custom_types: &[ExportedType],
sub_modules: &mut [Module],
parent_scope: &ExportScope,
) -> 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();
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
let string_type_path = syn::parse2::<syn::Path>(quote! { String }).unwrap();
for ExportedConst {
name: const_name,
cfg_attrs,
..
} in consts
{
let const_literal = syn::LitStr::new(const_name, Span::call_site());
let const_ref = syn::Ident::new(const_name, Span::call_site());
let cfg_attrs: Vec<_> = cfg_attrs
.iter()
.map(syn::Attribute::to_token_stream)
.collect();
set_const_statements.push(
syn::parse2::<syn::Stmt>(quote! {
#(#cfg_attrs)*
m.set_var(#const_literal, #const_ref);
})
.unwrap(),
);
}
for ExportedType {
name,
typ,
cfg_attrs,
..
} in custom_types
{
let const_literal = syn::LitStr::new(name, Span::call_site());
let cfg_attrs: Vec<_> = cfg_attrs
.iter()
.map(syn::Attribute::to_token_stream)
.collect();
set_const_statements.push(
syn::parse2::<syn::Stmt>(quote! {
#(#cfg_attrs)*
m.set_custom_type::<#typ>(#const_literal);
})
.unwrap(),
);
}
for item_mod in sub_modules {
item_mod.update_scope(parent_scope);
if item_mod.skipped() {
continue;
}
let module_name = item_mod.module_name();
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());
add_mod_blocks.push(
syn::parse2::<syn::ExprBlock>(quote! {
{
#(#cfg_attrs)*
m.set_sub_module(#exported_name, self::#module_name::rhai_module_generate());
}
})
.unwrap(),
);
set_flattened_mod_blocks.push(
syn::parse2::<syn::ExprBlock>(quote! {
{
#(#cfg_attrs)*
self::#module_name::rhai_generate_into_module(m, flatten);
}
})
.unwrap(),
);
}
let mut gen_fn_tokens = Vec::new();
for function in fns {
function.update_scope(parent_scope);
if function.skipped() {
continue;
}
let fn_token_name = syn::Ident::new(
&format!("{}_token", function.name()),
function.name().span(),
);
let reg_names = function.exported_names();
let fn_input_types: Vec<_> = function
.arg_list()
.map(|fn_arg| match fn_arg {
syn::FnArg::Receiver(..) => panic!("internal error: receiver fn outside impl!?"),
syn::FnArg::Typed(syn::PatType { ref ty, .. }) => {
let arg_type = match flatten_type_groups(ty.as_ref()) {
syn::Type::Reference(syn::TypeReference {
mutability: None,
ref elem,
..
}) => match flatten_type_groups(elem.as_ref()) {
syn::Type::Path(ref p) if p.path == str_type_path => {
syn::parse2::<syn::Type>(quote! {
ImmutableString })
.unwrap()
}
_ => panic!("internal error: non-string shared reference!?"),
},
syn::Type::Path(ref p) if p.path == string_type_path => {
syn::parse2::<syn::Type>(quote! {
ImmutableString })
.unwrap()
}
syn::Type::Reference(syn::TypeReference {
mutability: Some(_),
ref elem,
..
}) => match flatten_type_groups(elem.as_ref()) {
syn::Type::Path(ref p) => syn::parse2::<syn::Type>(quote! {
#p })
.unwrap(),
_ => panic!("internal error: invalid mutable reference!?"),
},
t => t.clone(),
};
syn::parse2::<syn::Expr>(quote! {
TypeId::of::<#arg_type>()})
.unwrap()
}
})
.collect();
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;
}
}
}
match function.params().namespace {
FnNamespaceAccess::Unset => (),
ns => namespace = ns,
}
let ns_str = syn::Ident::new(
match namespace {
FnNamespaceAccess::Unset => unreachable!("`namespace` should be set"),
FnNamespaceAccess::Global => "Global",
FnNamespaceAccess::Internal => "Internal",
},
fn_literal.span(),
);
#[cfg(feature = "metadata")]
let (param_names, comments) = (
quote! { Some(#fn_token_name::PARAM_NAMES) },
function
.comments()
.iter()
.map(|s| syn::LitStr::new(s, Span::call_site()))
.collect::<Vec<_>>(),
);
#[cfg(not(feature = "metadata"))]
let (param_names, comments) = (quote! { None }, Vec::<syn::LitStr>::new());
set_fn_statements.push(if comments.is_empty() {
syn::parse2::<syn::Stmt>(quote! {
#(#cfg_attrs)*
m.set_fn(#fn_literal, FnNamespace::#ns_str, FnAccess::Public,
#param_names, &[#(#fn_input_types),*], #fn_token_name().into());
})
.unwrap()
} else {
syn::parse2::<syn::Stmt>(quote! {
#(#cfg_attrs)*
m.set_fn_with_comments(#fn_literal, FnNamespace::#ns_str, FnAccess::Public,
#param_names, [#(#fn_input_types),*], [#(#comments),*], #fn_token_name().into());
})
.unwrap()
});
}
gen_fn_tokens.push(quote! {
#(#cfg_attrs)*
#[allow(non_camel_case_types)]
#[doc(hidden)]
pub struct #fn_token_name();
});
gen_fn_tokens.push(function.generate_impl(&fn_token_name.to_string()));
}
let module_docs = if doc.is_empty() {
None
} else {
Some(
syn::parse2::<syn::Stmt>(quote! {
m.set_doc(#doc);
})
.unwrap(),
)
};
let mut generate_fn_call = syn::parse2::<syn::ItemMod>(quote! {
pub mod generate_info {
#[allow(unused_imports)]
use super::*;
#[doc(hidden)]
pub fn rhai_module_generate() -> Module {
let mut m = Module::new();
#module_docs
rhai_generate_into_module(&mut m, false);
m.build_index();
m
}
#[allow(unused_mut)]
#[doc(hidden)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
#(#set_fn_statements)*
#(#set_const_statements)*
if flatten {
#(#set_flattened_mod_blocks)*
} else {
#(#add_mod_blocks)*
}
}
}
})
.unwrap();
let (.., generate_call_content) = generate_fn_call.content.take().unwrap();
quote! {
#(#generate_call_content)*
#(#gen_fn_tokens)*
}
}
pub fn check_rename_collisions(fns: &[ExportedFn]) -> Result<(), syn::Error> {
fn make_key(name: impl ToString, item_fn: &ExportedFn) -> String {
item_fn
.arg_list()
.fold(name.to_string(), |mut arg_str, fn_arg| {
let type_string: String = match fn_arg {
syn::FnArg::Receiver(..) => unimplemented!("receiver rhai_fns not implemented"),
syn::FnArg::Typed(syn::PatType { ref ty, .. }) => print_type(ty),
};
arg_str.push('.');
arg_str.push_str(&type_string);
arg_str
})
}
let mut renames = BTreeMap::new();
let mut fn_defs = BTreeMap::new();
for item_fn in fns {
if !item_fn.params().name.is_empty() || item_fn.params().special != FnSpecialAccess::None {
let mut names: Vec<_> = item_fn
.params()
.name
.iter()
.map(|n| (n.clone(), n.clone()))
.collect();
if let Some((s, n, ..)) = item_fn.params().special.get_fn_name() {
names.push((s, n));
}
for (name, fn_name) in names {
let current_span = item_fn.params().span.unwrap();
let key = make_key(name, item_fn);
if let Some(other_span) = renames.insert(key, current_span) {
let mut err = syn::Error::new(
current_span,
format!("duplicate Rhai signature for '{fn_name}'"),
);
err.combine(syn::Error::new(
other_span,
format!("duplicated function renamed '{fn_name}'"),
));
return Err(err);
}
}
} else {
let ident = item_fn.name();
if let Some(other_span) = fn_defs.insert(ident.to_string(), ident.span()) {
let mut err =
syn::Error::new(ident.span(), format!("duplicate function '{ident}'"));
err.combine(syn::Error::new(
other_span,
format!("duplicated function '{ident}'"),
));
return Err(err);
}
let key = make_key(ident, item_fn);
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);
}
}
}
Ok(())
}