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
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use syn::{parse, Result};

pub(crate) fn imp(args: TokenStream, input: TokenStream) -> Result<TokenStream> {
    let args = syn::parse2::<AttrArgs>(args)?;

    let input = syn::parse2::<syn::ItemFn>(input)?;
    let name = &input.sig.ident;
    let body = &input.block;

    let mut attrs: Vec<_> = input.attrs.iter().collect();
    let thread_local = attrs.drain_filter(|attr| attr.path.is_ident("thread_local")).count() > 0;
    let setup_method = if thread_local { quote!(system_local) } else { quote!(system) };

    let system_name = format_ident!("{}_system", name);
    let setup_name = format_ident!("{}_setup", name);

    let mut out_args = Vec::new();
    let mut state_values = Vec::new();
    let mut assigns = Vec::new();
    let mut var_adapters = Vec::new();

    let mut perf_name = None;

    let mut has_debug_entries_resource = false;

    'param_loop: for arg in &input.sig.inputs {
        let syn::PatType { pat, ty, attrs: arg_attrs, .. } = match arg {
            syn::FnArg::Receiver(recv) => {
                return Err(syn::Error::new_spanned(recv, "receiver not allowed"))
            }
            syn::FnArg::Typed(typed) => typed,
        };

        let mut arg_attrs: Vec<_> = arg_attrs.iter().collect();

        if let Some(attr) = arg_attrs.iter().find(|attr| attr.path.is_ident("resource")) {
            let mut is_perf = false;
            if let syn::Type::Reference(ty) = &**ty {
                if let syn::Type::Path(path) = &*ty.elem {
                    if path.path.segments.iter().map(|segment| &segment.ident).collect::<Vec<_>>()
                        == ["codegen", "Perf"]
                    {
                        is_perf = true;
                    }
                }
            }
            if is_perf {
                perf_name = Some(quote!(#pat));
            } else {
                let mut no_init = false;
                let args = attr.parse_meta()?;
                if let syn::Meta::List(list) = &args {
                    for arg in &list.nested {
                        if let syn::NestedMeta::Meta(meta) = arg {
                            no_init =
                                matches!(meta, syn::Meta::Path(path) if path.is_ident("no_init"));
                        }
                    }
                }
                if !no_init {
                    if let syn::Type::Reference(ty) = &**ty {
                        let elem = &*ty.elem;
                        assigns.push(quote! {
                            setup = setup.resource_default::<#elem>();
                        });
                    }
                }
            }
        }

        if let Some(attr) = arg_attrs.drain_filter(|attr| attr.path.is_ident("state")).next() {
            let expr = attr.parse_args::<syn::Expr>()?;
            state_values.push(quote!(#expr));
            out_args.push((quote!(#[state]), quote!(#pat), quote!(#ty)));
            continue;
        }

        for attr in &arg_attrs {
            if attr.path.is_ident("debug") {
                if cfg!(feature = "render-debug") {
                    let DebugName { category, name } =
                        syn::parse2::<DebugName>(attr.tokens.clone())?;
                    if !has_debug_entries_resource {
                        assigns.push(quote! {
                            let mut __debug_entries = setup.resources.get_mut_or_default::<codegen::DebugEntries>();
                        });
                        has_debug_entries_resource = true;
                    }

                    let entry_name = format_ident!(
                        "__debug_entry_for_{}",
                        match &**pat {
                            syn::Pat::Ident(ident) => &ident.ident,
                            span => {
                                return Err(syn::Error::new_spanned(
                                    span,
                                    "debug entry must have simple variable name",
                                ));
                            }
                        }
                    );
                    assigns.push(quote! {
                        let #entry_name = __debug_entries.entry(#category, #name).clone();
                    });
                    state_values.push(quote!(#entry_name));
                    out_args.push((quote!(#[state]), quote!(#pat), quote!(#ty)));
                } else {
                    let raw = match &**ty {
                        syn::Type::Reference(ty) => &ty.elem,
                        ty => {
                            return Err(syn::Error::new_spanned(
                                ty,
                                "debug entry must have type &mut DebugEntry",
                            ))
                        }
                    };
                    state_values.push(quote!(#raw(())));
                    out_args.push((quote!(#[state]), quote!(#pat), quote!(#ty)))
                }
                continue 'param_loop;
            }
        }

        if arg_attrs.drain_filter(|attr| attr.path.is_ident("subscriber")).next().is_some() {
            let event = match &**ty {
                syn::Type::ImplTrait(it) if it.bounds.len() == 1 => &it.bounds[0],
                span => {
                    return Err(syn::Error::new_spanned(
                        span,
                        "subscriber must have type `impl Iterator<Item = EventType>`",
                    ))
                }
            };
            let event = match event {
                syn::TypeParamBound::Trait(tb) => {
                    &tb.path.segments.last().expect("empty path").arguments
                }
                span => {
                    return Err(syn::Error::new_spanned(
                        span,
                        "subscriber must have type `impl Iterator<Item = EventType>`",
                    ))
                }
            };
            let event = match event {
                syn::PathArguments::AngleBracketed(ab) => ab.args.first().expect("empty <>"),
                span => {
                    return Err(syn::Error::new_spanned(
                        span,
                        "subscriber must have type `impl Iterator<Item = EventType>`",
                    ))
                }
            };
            let event = match event {
                syn::GenericArgument::Binding(bind) => bind,
                span => {
                    return Err(syn::Error::new_spanned(
                        span,
                        "subscriber must have type `impl Iterator<Item = EventType>`",
                    ))
                }
            };
            if event.ident != "Item" {
                return Err(syn::Error::new_spanned(
                    event,
                    "subscriber must have type `impl Iterator<Item = EventType>`",
                ));
            }
            let event = &event.ty;

            let pat = match &**pat {
                syn::Pat::Ident(ident) => &ident.ident,
                span => {
                    return Err(syn::Error::new_spanned(
                        span,
                        "subscriber must have simple variable name",
                    ))
                }
            };
            let reader_var_name = format_ident!("__reader_id_for_{}", &pat);
            let channel_var_name = format_ident!("__channel_for_{}", &pat);
            out_args.push((
                quote!(#[state]),
                quote!(#reader_var_name),
                quote!(&mut ::shrev::ReaderId<#event>),
            ));
            out_args.push((
                quote!(#[resource]),
                quote!(#channel_var_name),
                quote!(&::shrev::EventChannel<#event>),
            ));
            state_values.push(quote!(#reader_var_name));
            assigns.push(quote! {
                let #reader_var_name = setup.subscriber::<#event>();
            });
            var_adapters.push(quote! {
                let mut #pat = #channel_var_name.read(#reader_var_name);
            });
            continue;
        }

        if arg_attrs.drain_filter(|attr| attr.path.is_ident("publisher")).next().is_some() {
            let event = match &**ty {
                syn::Type::ImplTrait(it) if it.bounds.len() == 1 => &it.bounds[0],
                span => {
                    return Err(syn::Error::new_spanned(
                        span,
                        "publisher must have type `impl FnMut(EventType)`",
                    ))
                }
            };
            let event = match event {
                syn::TypeParamBound::Trait(tb) => {
                    &tb.path.segments.last().expect("empty path").arguments
                }
                span => {
                    return Err(syn::Error::new_spanned(
                        span,
                        "publisher must have type `impl FnMut(EventType)`",
                    ))
                }
            };
            let event = match event {
                syn::PathArguments::Parenthesized(ab) => &ab.inputs,
                span => {
                    return Err(syn::Error::new_spanned(
                        span,
                        "publisher must have type `impl FnMut(EventType)`",
                    ))
                }
            };
            let event = match event.first() {
                Some(event) => event,
                None => {
                    return Err(syn::Error::new_spanned(
                        event,
                        "publisher must have type `impl FnMut(EventType)`",
                    ))
                }
            };

            let pat = match &**pat {
                syn::Pat::Ident(ident) => &ident.ident,
                span => {
                    return Err(syn::Error::new_spanned(
                        span,
                        "publisher must have simple variable name",
                    ))
                }
            };
            out_args.push((
                quote!(#[resource]),
                quote!(#pat),
                quote!(&mut ::shrev::EventChannel<#event>),
            ));
            assigns.push(quote! {
                setup = setup.resource_default::<::shrev::EventChannel<#event>>();
            });
            var_adapters.push(quote! {
                let mut #pat = move |event| #pat.single_write(event);
            });
            continue;
        }

        out_args.push((quote!(#(#arg_attrs)*), quote!(#pat), quote!(#ty)));
    }

    if has_debug_entries_resource {
        assigns.push(quote! {
            drop(__debug_entries);
        });
    }

    let perf_name = match perf_name {
        Some(name) => name,
        None => {
            out_args.push((
                quote!(#[resource]),
                quote!(__traffloat_codegen_perf),
                quote!(&::codegen::Perf),
            ));
            quote!(__traffloat_codegen_perf)
        }
    };

    let out_arg_attrs: Vec<_> = out_args.iter().map(|tuple| &tuple.0).collect();
    let out_arg_names: Vec<_> = out_args.iter().map(|tuple| &tuple.1).collect();
    let out_arg_types: Vec<_> = out_args.iter().map(|tuple| &tuple.2).collect();

    let class = &args.class;

    let output = quote! {
        #[::legion::system]
        #[allow(clippy::too_many_arguments)]
        #(#attrs)*
        fn #name(
            #(#out_arg_attrs #out_arg_names: #out_arg_types),*
        ) {
            fn imp(#(#out_arg_names: #out_arg_types),*) {
                #(#var_adapters)*
                #body
            }

            let __traffloat_codegen_perf_start = ::codegen::hrtime();
            imp(#(#out_arg_names),*);
            let __traffloat_codegen_perf_end = ::codegen::hrtime();
            #perf_name.push(
                concat!(module_path!(), "::", stringify!(#name)),
                __traffloat_codegen_perf_end - __traffloat_codegen_perf_start,
            )
        }

        fn #setup_name(mut setup: ::codegen::SetupEcs) -> ::codegen::SetupEcs {
            #(#assigns)*
            setup.#setup_method(#system_name(#(#state_values),*), ::codegen::SystemClass::#class)
        }
    };
    Ok(output)
}

struct DebugName {
    category: String,
    name:     String,
}

impl parse::Parse for DebugName {
    fn parse(buf: parse::ParseStream) -> parse::Result<Self> {
        let inner;
        syn::parenthesized!(inner in buf);
        let category = inner.parse::<syn::LitStr>()?;
        inner.parse::<syn::Token![,]>()?;
        let name = inner.parse::<syn::LitStr>()?;
        Ok(Self { category: category.value(), name: name.value() })
    }
}

struct AttrArgs {
    class: Ident,
}

impl parse::Parse for AttrArgs {
    fn parse(buf: parse::ParseStream) -> parse::Result<Self> {
        let class = buf.parse::<Ident>()?;
        Ok(Self { class })
    }
}