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
extern crate proc_macro;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{parse_quote, Error, Result};

const MAX_TYPES: usize = 10;

#[allow(clippy::or_fun_call)]
#[proc_macro_attribute]
pub fn system(
    attr: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let name = syn::parse_macro_input!(attr as syn::Ident);
    let run = syn::parse_macro_input!(item as syn::ItemFn);
    expand_system(name, run)
        .unwrap_or_else(|err| err.to_compile_error())
        .into()
}

#[allow(clippy::cognitive_complexity)]
fn expand_system(name: syn::Ident, mut run: syn::ItemFn) -> Result<TokenStream> {
    if run.sig.ident != "run" {
        return Err(Error::new(
            Span::call_site(),
            "Systems have only one method: run",
        ));
    }
    if !run.sig.generics.params.is_empty() {
        return Err(Error::new_spanned(
            run.sig.generics,
            "run should not take generic arguments",
        ));
    }
    if run.sig.generics.where_clause.is_some() {
        return Err(Error::new_spanned(
            run.sig.generics.where_clause,
            "run should not take a where clause",
        ));
    }

    // checks if run returns a type other than ()
    let returns_something = match run.sig.output {
        syn::ReturnType::Type(_, ref type_info) => match **type_info {
            syn::Type::Tuple(ref tuple) => !tuple.elems.is_empty(),
            _ => true,
        },
        syn::ReturnType::Default => false,
    };
    if returns_something {
        return Err(Error::new_spanned(
            run.sig.output,
            "run should not return anything",
        ));
    }

    let body = &*run.block;
    let vis = run.vis;

    let mut data = Vec::with_capacity(run.sig.inputs.len());
    let mut binding = Vec::with_capacity(run.sig.inputs.len());

    run.sig.inputs.iter_mut().try_for_each(|arg| {
        if let syn::FnArg::Typed(syn::PatType { pat, ty, .. }) = arg {
            match **ty {
                syn::Type::Reference(ref mut reference) => {
                    // references are added a 'sys lifetime if they don't have one
                    // if they have another lifetime, make it 'sys
                    if let syn::Type::Path(path) = &*reference.elem {
                        // transform &Entities into Entites and &mut Entities into EntitiesMut
                        if path.path.segments.last().unwrap().ident == "Entities" {
                            if reference.mutability.is_none() {
                                **ty = parse_quote!(::shipyard::prelude::Entities);
                            } else {
                                **ty = parse_quote!(::shipyard::prelude::EntitiesMut);
                            }
                        } else if path.path.segments.last().unwrap().ident == "AllStorages" {
                            if reference.mutability.is_none() {
                                return Err(Error::new_spanned(
                                    path,
                                    "You probably forgot a mut, &AllStorages isn't a valid storage access"
                                ));
                            } else {
                                **ty = parse_quote!(::shipyard::prelude::AllStorages);
                            }
                        } else if path.path.segments.last().unwrap().ident == "ThreadPool" {
                            if reference.mutability.is_none() {
                                **ty = parse_quote!(::shipyard::prelude::ThreadPool);
                            } else {
                                return Err(Error::new_spanned(
                                    path,
                                    "ThreadPool can't be accessed mutably but there's no need to, it's Sync and works perfectly with a shared access"
                                ));
                            }
                        } else {
                            reference.lifetime = parse_quote!('sys);
                        }
                    } else {
                        reference.lifetime = parse_quote!('sys);
                    }
                }
                syn::Type::Path(ref mut path) => {
                    let last = path.path.segments.last_mut().unwrap();
                    // Unique has to be handled separatly because the lifetime is inside it
                    if last.ident == "Unique" {
                        if let syn::PathArguments::AngleBracketed(inner_type) = &mut last.arguments
                        {
                            let arg = inner_type.args.iter_mut().next().unwrap();
                            if let syn::GenericArgument::Type(inner_type) = arg {
                                match inner_type {
                                    syn::Type::Reference(reference) => {
                                        reference.lifetime = parse_quote!('sys);
                                    },
                                    syn::Type::Path(ref mut path) => {
                                        let last = path.path.segments.last_mut().unwrap();
                                        if last.ident == "NonSend" {
                                            if let syn::PathArguments::AngleBracketed(inner_type) = &mut last.arguments
                                            {
                                                let arg = inner_type.args.iter_mut().next().unwrap();
                                                if let syn::GenericArgument::Type(inner_type) = arg {
                                                    if let syn::Type::Reference(reference) = inner_type {
                                                        reference.lifetime = parse_quote!('sys);
                                                    } else {
                                                        return Err(Error::new_spanned(
                                                            inner_type,
                                                            "NonSend will only work with component storages reffered by &T or &mut T",
                                                        ));
                                                    }
                                                } else {
                                                    unreachable!()
                                                }
                                            }
                                        }
                                        else if last.ident == "NonSync" {
                                            if let syn::PathArguments::AngleBracketed(inner_type) = &mut last.arguments
                                            {
                                                let arg = inner_type.args.iter_mut().next().unwrap();
                                                if let syn::GenericArgument::Type(inner_type) = arg {
                                                    if let syn::Type::Reference(reference) = inner_type {
                                                        reference.lifetime = parse_quote!('sys);
                                                    } else {
                                                        return Err(Error::new_spanned(
                                                            inner_type,
                                                            "NonSync will only work with component storages reffered by &T or &mut T",
                                                        ));
                                                    }
                                                } else {
                                                    unreachable!()
                                                }
                                            }
                                        }
                                        else if last.ident == "NonSendSync" {
                                            if let syn::PathArguments::AngleBracketed(inner_type) = &mut last.arguments
                                            {
                                                let arg = inner_type.args.iter_mut().next().unwrap();
                                                if let syn::GenericArgument::Type(inner_type) = arg {
                                                    if let syn::Type::Reference(reference) = inner_type {
                                                        reference.lifetime = parse_quote!('sys);
                                                    } else {
                                                        return Err(Error::new_spanned(
                                                            inner_type,
                                                            "NonSendSync will only work with component storages reffered by &T or &mut T",
                                                        ));
                                                    }
                                                } else {
                                                    unreachable!()
                                                }
                                            }
                                        } else {
                                            return Err(Error::new_spanned(
                                                inner_type,
                                                "Unique will only work with component storages referred by &T or &mut T",
                                            ));
                                        }
                                    },
                                    _ => {
                                        return Err(Error::new_spanned(
                                            inner_type,
                                            "Unique will only work with component storages referred by &T or &mut T",
                                        ));
                                    }
                                }
                            } else {
                                unreachable!()
                            }
                        }
                    }
                    else if last.ident == "NonSend" {
                        if let syn::PathArguments::AngleBracketed(inner_type) = &mut last.arguments
                        {
                            let arg = inner_type.args.iter_mut().next().unwrap();
                            if let syn::GenericArgument::Type(inner_type) = arg {
                                if let syn::Type::Reference(reference) = inner_type {
                                    reference.lifetime = parse_quote!('sys);
                                } else {
                                    return Err(Error::new_spanned(
                                        inner_type,
                                        "NonSend will only work with component storages referred by &T or &mut T",
                                    ));
                                }
                            } else {
                                unreachable!()
                            }
                        }
                    }
                    else if last.ident == "NonSync" {
                        if let syn::PathArguments::AngleBracketed(inner_type) = &mut last.arguments
                        {
                            let arg = inner_type.args.iter_mut().next().unwrap();
                            if let syn::GenericArgument::Type(inner_type) = arg {
                                if let syn::Type::Reference(reference) = inner_type {
                                    reference.lifetime = parse_quote!('sys);
                                } else {
                                    return Err(Error::new_spanned(
                                        inner_type,
                                        "NonSync will only work with component storages referred by &T or &mut T",
                                    ));
                                }
                            } else {
                                unreachable!()
                            }
                        }
                    }
                    else if last.ident == "NonSendSync" {
                        if let syn::PathArguments::AngleBracketed(inner_type) = &mut last.arguments
                        {
                            let arg = inner_type.args.iter_mut().next().unwrap();
                            if let syn::GenericArgument::Type(inner_type) = arg {
                                if let syn::Type::Reference(reference) = inner_type {
                                    reference.lifetime = parse_quote!('sys);
                                } else {
                                    return Err(Error::new_spanned(
                                        inner_type,
                                        "NonSendSync will only work with component storages referred by &T or &mut T",
                                    ));
                                }
                            } else {
                                unreachable!()
                            }
                        }
                    }
                }
                _ => {
                    return Err(Error::new_spanned(
                        ty,
                        "A system will only accept a type of this list:\n\
                            \t\t- &T for an immutable reference to T storage\n\
                            \t\t- &mut T for a mutable reference to T storage\n\
                            \t\t- &Entities for an immutable reference to the entity storage\n\
                            \t\t- &mut EntitiesMut for a mutable reference to the entity storage\n\
                            \t\t- &mut AllStorages for a mutable reference to the storage of all components\n\
                            \t\t- &ThreadPool for an immutable reference to the rayon::ThreadPool used by the World",
                    ));
                }
            }

            data.push((*ty).clone());
            binding.push((**pat).clone());
            Ok(())
        } else {
            unreachable!()
        }
    })?;

    // make tuples MAX_TYPES len maximum to allow users to pass an infinite number of types
    while data.len() > MAX_TYPES {
        for i in 0..(data.len() / MAX_TYPES) {
            let ten = &data[(i * MAX_TYPES)..((i + 1) * MAX_TYPES)];
            *data[i] = parse_quote!((#(#ten,)*));
            data.drain((i + 1)..((i + 1) * MAX_TYPES));

            let ten = &binding[i..(i + 10)];
            binding[i] = parse_quote!((#(#ten,)*));
            binding.drain((i + 1)..((i + 1) * MAX_TYPES));
        }
    }

    Ok(quote! {
        #vis struct #name;
        impl<'sys> ::shipyard::prelude::System<'sys> for #name {
            type Data = (#(#data,)*);
            fn run((#(#binding,)*): (#(<#data as SystemData<'sys>>::View,)*)) #body
        }
    })
}