summaryrefslogtreecommitdiff
path: root/engine-macros/src/reflection/field.rs
blob: 7d4e301f72194c03e74f45c475e011c8673a60dd (plain)
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
use quote::quote;

use crate::reflection::visibility::generate as generate_visibility;

pub struct ReflectionFieldGenOptions<'a>
{
    pub field_vis_override: Option<syn::Visibility>,
    pub gen_get_byte_offset: &'a dyn Fn(&syn::Field) -> proc_macro2::TokenStream,
}

pub fn generate(
    field: &syn::Field,
    field_index: usize,
    engine_crate_path: &syn::Path,
    options: ReflectionFieldGenOptions<'_>,
) -> proc_macro2::TokenStream
{
    let field_ident = &field.ident;

    let field_type = &field.ty;

    let field_name = if let Some(field_ident) = field_ident {
        let field_name = syn::LitStr::new(&field_ident.to_string(), field_ident.span());

        quote! { Some(#field_name) }
    } else {
        quote! { None }
    };

    let field_byte_offset = (options.gen_get_byte_offset)(field);

    let field_vis = options.field_vis_override.as_ref().unwrap_or(&field.vis);

    let field_reflection_vis = generate_visibility(field_vis, &engine_crate_path);

    quote! {
        #engine_crate_path::reflection::Field {
            name: #field_name,
            index: #field_index,
            layout: std::alloc::Layout::new::<#field_type>(),
            byte_offset: #field_byte_offset,
            type_id: std::any::TypeId::of::<#field_type>(),
            get_type_name: #engine_crate_path::reflection::FnWithDebug::new(|| {
                std::any::type_name::<#field_type>()
            }),
            get_type: #engine_crate_path::reflection::FnWithDebug::new(|| {
                struct SpecializationTarget<Field>(std::marker::PhantomData<Field>);

                trait FieldHasReflection
                {
                    fn field_type_reflection(&self)
                        -> Option<&'static #engine_crate_path::reflection::Type>;
                }

                trait FieldDoesNotHaveReflection
                {
                    fn field_type_reflection(&self)
                        -> Option<&'static #engine_crate_path::reflection::Type>;
                }

                impl<Field> FieldDoesNotHaveReflection for &SpecializationTarget<Field>
                {
                    fn field_type_reflection(&self)
                        -> Option<&'static #engine_crate_path::reflection::Type>
                    {
                        None
                    }
                }

                impl<Field> FieldHasReflection for SpecializationTarget<Field>
                where
                    Field: #engine_crate_path::reflection::Reflection
                {
                    fn field_type_reflection(&self)
                        -> Option<&'static #engine_crate_path::reflection::Type>
                    {
                        Some(Field::type_reflection())
                    }
                }

                (&SpecializationTarget::<#field_type>(std::marker::PhantomData))
                    .field_type_reflection()
            }),
            visibility: #field_reflection_vis
        }
    }
}