summaryrefslogtreecommitdiff
path: root/engine-macros/src/reflection/enum_impl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine-macros/src/reflection/enum_impl.rs')
-rw-r--r--engine-macros/src/reflection/enum_impl.rs485
1 files changed, 485 insertions, 0 deletions
diff --git a/engine-macros/src/reflection/enum_impl.rs b/engine-macros/src/reflection/enum_impl.rs
new file mode 100644
index 0000000..14d91a7
--- /dev/null
+++ b/engine-macros/src/reflection/enum_impl.rs
@@ -0,0 +1,485 @@
+use quote::{format_ident, quote};
+
+use crate::reflection::default_value::gen_get_default_value_fn;
+use crate::reflection::options_attr::OptionsAttr;
+use crate::util::find_engine_crate_path;
+
+pub fn generate(input: syn::ItemEnum, options: OptionsAttr) -> proc_macro2::TokenStream
+{
+ let engine_crate_path = find_engine_crate_path().unwrap();
+
+ let variant_lookup_match_arms = input
+ .variants
+ .iter()
+ .enumerate()
+ .map(|(index, variant)| {
+ let variant_ident = &variant.ident;
+
+ let pattern = match variant.fields {
+ syn::Fields::Unit => quote! { Self::#variant_ident },
+ syn::Fields::Named(_) => quote! { Self::#variant_ident { .. } },
+ syn::Fields::Unnamed(_) => quote! { Self::#variant_ident(..) },
+ };
+
+ quote! {
+ #pattern => &enum_reflection.variants[#index]
+ }
+ })
+ .collect::<Vec<_>>();
+
+ let is_unit_only = input
+ .variants
+ .iter()
+ .all(|variant| matches!(variant.fields, syn::Fields::Unit));
+
+ let impls: &mut dyn Iterator<Item = proc_macro2::TokenStream> =
+ if input.generics.params.is_empty() {
+ &mut [generate_impls(
+ &input,
+ None,
+ is_unit_only,
+ &variant_lookup_match_arms,
+ &engine_crate_path,
+ )]
+ .into_iter()
+ } else {
+ &mut options.impl_with_generics.iter().map(|generic_args| {
+ generate_impls(
+ &input,
+ Some(generic_args),
+ is_unit_only,
+ &variant_lookup_match_arms,
+ &engine_crate_path,
+ )
+ })
+ };
+
+ quote! {
+ #(#impls)*
+ }
+}
+
+fn generate_impls(
+ input: &syn::ItemEnum,
+ generic_args: Option<&syn::AngleBracketedGenericArguments>,
+ is_unit_only: bool,
+ variant_lookup_match_arms: &[proc_macro2::TokenStream],
+ engine_crate_path: &syn::Path,
+) -> proc_macro2::TokenStream
+{
+ let get_default_value_fn = gen_get_default_value_fn(&input.ident, generic_args);
+
+ let variants = generate_variants(&input.variants, &engine_crate_path);
+
+ let generics_type_aliases = input
+ .generics
+ .type_params()
+ .zip(
+ generic_args
+ .iter()
+ .map(|generic_args| &generic_args.args)
+ .flatten()
+ .flat_map(|generic_arg| match generic_arg {
+ syn::GenericArgument::Type(ty) => Some(ty),
+ _ => None,
+ }),
+ )
+ .map(|(type_param, generic_arg_type)| {
+ let type_param_ident = &type_param.ident;
+
+ quote! {
+ type #type_param_ident = #generic_arg_type;
+ }
+ });
+
+ let input_ident = &input.ident;
+
+ quote! {
+ unsafe impl #engine_crate_path::reflection::Reflection for
+ #input_ident #generic_args
+ {
+ const TYPE_REFLECTION: &#engine_crate_path::reflection::Type =
+ &const {
+ #(#generics_type_aliases)*
+
+ #engine_crate_path::reflection::Type::Enum(
+ #engine_crate_path::reflection::Enum {
+ variants: &[#(#variants),*],
+ is_unit_only: #is_unit_only,
+ get_default_value: || {
+ #get_default_value_fn
+ },
+ cast_dyn_any: |ptr| ptr.cast::<Self>(),
+ get_variant_index: |target| {
+ use std::any::Any;
+ use #engine_crate_path::reflection::Reflection;
+ use #engine_crate_path::reflection::EnumReflectionExt;
+
+ let target = target.downcast_ref::<Self>()?;
+
+ let enum_reflection = unsafe {
+ <Self as Reflection>::TYPE_REFLECTION
+ .as_enum()
+ .unwrap_unchecked()
+ };
+
+ Some(enum_reflection.variants
+ .element_offset(
+ match target {
+ #(#variant_lookup_match_arms),*
+ }
+ )
+ .unwrap())
+ }
+ }
+ )
+ };
+ }
+
+ unsafe impl #engine_crate_path::reflection::EnumReflectionExt for
+ #input_ident #generic_args
+ {
+ fn get_variant_reflection(&self)
+ -> &'static #engine_crate_path::reflection::EnumVariant
+ {
+ let enum_reflection = unsafe {
+ <Self as #engine_crate_path::reflection::Reflection>::TYPE_REFLECTION
+ .as_enum()
+ .unwrap_unchecked()
+ };
+
+ match self {
+ #(#variant_lookup_match_arms),*
+ }
+ }
+ }
+ }
+}
+
+fn generate_variants<'a>(
+ input_variants: &'a syn::punctuated::Punctuated<syn::Variant, syn::Token![,]>,
+ engine_crate_path: &'a syn::Path,
+) -> impl Iterator<Item = proc_macro2::TokenStream> + use<'a>
+{
+ input_variants.iter().map(move |variant| {
+ let variant_name =
+ syn::LitStr::new(&variant.ident.to_string(), variant.ident.span());
+
+ let fields = match &variant.fields {
+ syn::Fields::Unit => quote! { None },
+ syn::Fields::Named(named_fields) => {
+ let fields = named_fields.named.iter().enumerate().map(
+ |(variant_field_index, variant_field)| {
+ generate_enum_variant_field(
+ variant_field,
+ variant_field_index,
+ &engine_crate_path,
+ )
+ },
+ );
+
+ quote! {
+ Some(#engine_crate_path::reflection::EnumVariantFields::Named {
+ fields: &[#(#fields),*]
+ })
+ }
+ }
+ syn::Fields::Unnamed(unnamed_fields) => {
+ let fields = unnamed_fields.unnamed.iter().enumerate().map(
+ |(variant_field_index, variant_field)| {
+ generate_enum_variant_field(
+ variant_field,
+ variant_field_index,
+ &engine_crate_path,
+ )
+ },
+ );
+
+ quote! {
+ Some(#engine_crate_path::reflection::EnumVariantFields::Unnamed {
+ fields: &[#(#fields),*]
+ })
+ }
+ }
+ };
+
+ let variant_field_vars = variant.fields.iter().enumerate().map(
+ |(variant_field_index, variant_field)| {
+ let var_ident = match variant_field.ident.as_ref() {
+ Some(field_ident) => format_ident!("field_{field_ident}"),
+ None => format_ident!("field_{}", variant_field_index),
+ };
+
+ let field_type = &variant_field.ty;
+
+ quote! {
+ let #var_ident = fields
+ .next()
+ .ok_or(EnumVariantWriteNewToError::TooFewFields)?
+ .downcast::<#field_type>()
+ .map_err(|_| EnumVariantWriteNewToError::WrongFieldType)?;
+ }
+ },
+ );
+
+ let variant_construction =
+ match &variant.fields {
+ syn::Fields::Unit => {
+ let variant_ident = &variant.ident;
+
+ quote! {
+ Self::#variant_ident
+ }
+ }
+ syn::Fields::Named(named_fields) => {
+ let variant_ident = &variant.ident;
+
+ let fields = named_fields.named.iter().map(|field| {
+ let Some(field_ident) = field.ident.as_ref() else {
+ unreachable!();
+ };
+
+ let field_var_ident = format_ident!("field_{field_ident}");
+
+ quote! {
+ #field_ident: *#field_var_ident
+ }
+ });
+
+ quote! {
+ Self::#variant_ident {
+ #(#fields),*
+ }
+ }
+ }
+ syn::Fields::Unnamed(unnamed_fields) => {
+ let variant_ident = &variant.ident;
+
+ let fields = unnamed_fields.unnamed.iter().enumerate().map(
+ |(field_index, _)| {
+ let field_var_ident = format_ident!("field_{field_index}");
+
+ quote! {
+ *#field_var_ident
+ }
+ },
+ );
+
+ quote! {
+ Self::#variant_ident(
+ #(#fields),*
+ )
+ }
+ }
+ };
+
+ let variant_ident = &variant.ident;
+
+ let variant_pattern = match &variant.fields {
+ syn::Fields::Unit => quote! { Self::#variant_ident },
+ syn::Fields::Named(syn::FieldsNamed { named: named_fields, .. }) => {
+ let field_idents = named_fields.iter().map(|field| {
+ let Some(field_ident) = &field.ident else {
+ unreachable!();
+ };
+
+ field_ident
+ });
+
+ quote! { Self::#variant_ident { #(#field_idents),* } }
+ }
+ syn::Fields::Unnamed(syn::FieldsUnnamed {
+ unnamed: unnamed_fields, ..
+ }) => {
+ let field_idents = unnamed_fields
+ .iter()
+ .enumerate()
+ .map(|(field_index, _)| format_ident!("field_{field_index}"));
+
+ quote! { Self::#variant_ident(#(#field_idents),*) }
+ }
+ };
+
+ let field_index_match_arms = match &variant.fields {
+ syn::Fields::Unit => quote! {},
+ syn::Fields::Named(syn::FieldsNamed { named: named_fields, .. }) => {
+ let match_arms =
+ named_fields.iter().enumerate().map(|(field_index, field)| {
+ let Some(field_ident) = &field.ident else {
+ unreachable!();
+ };
+
+ quote! { #field_index => { #field_ident } }
+ });
+
+ quote! { #(#match_arms)* }
+ }
+ syn::Fields::Unnamed(syn::FieldsUnnamed {
+ unnamed: unnamed_fields, ..
+ }) => {
+ let match_arms =
+ unnamed_fields.iter().enumerate().map(|(field_index, _)| {
+ let field_ident = format_ident!("field_{field_index}");
+
+ quote! { #field_index => { #field_ident } }
+ });
+
+ quote! { #(#match_arms)* }
+ }
+ };
+
+ quote! {
+ #engine_crate_path::reflection::EnumVariant {
+ name: #variant_name,
+ fields: #fields,
+ try_write_new_to: |dst, fields| {
+ use std::any::Any;
+ use #engine_crate_path::reflection::EnumVariantWriteNewToError;
+
+ let dst = dst
+ .downcast_mut()
+ .ok_or(EnumVariantWriteNewToError::WrongDstType)?;
+
+ #(#variant_field_vars)*;
+
+ let created = #variant_construction;
+
+ if fields.next().is_some() {
+ return Err(EnumVariantWriteNewToError::TooManyFields);
+ }
+
+ *dst = created;
+
+ Ok(())
+ },
+ try_get_field: |target, field_index| { {
+ #![allow(unreachable_code)]
+
+ use std::any::Any;
+ use #engine_crate_path::reflection::GetError;
+
+ let target = target
+ .downcast_ref::<Self>()
+ .ok_or(GetError::WrongTargetType)?;
+
+ let #variant_pattern = target else {
+ return Err(GetError::WrongTargetEnumVariant);
+ };
+
+ let field: &dyn Any = match field_index {
+ #field_index_match_arms
+ _ => {
+ return Err(GetError::IndexOutOfBounds);
+ }
+ };
+
+ Ok(field)
+ } },
+ try_get_field_mut: |target, field_index| { {
+ #![allow(unreachable_code)]
+
+ use std::any::Any;
+ use #engine_crate_path::reflection::GetError;
+
+ let target = target
+ .downcast_mut::<Self>()
+ .ok_or(GetError::WrongTargetType)?;
+
+ let #variant_pattern = target else {
+ return Err(GetError::WrongTargetEnumVariant);
+ };
+
+ let field: &mut dyn Any = match field_index {
+ #field_index_match_arms
+ _ => {
+ return Err(GetError::IndexOutOfBounds);
+ }
+ };
+
+ Ok(field)
+ } },
+ }
+ }
+ })
+}
+
+pub fn generate_enum_variant_field(
+ field: &syn::Field,
+ field_index: usize,
+ engine_crate_path: &syn::Path,
+) -> 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 get_type_fn_body =
+ { gen_get_optional_type_reflection(field_type, engine_crate_path) };
+
+ quote! {
+ #engine_crate_path::reflection::EnumVariantField {
+ name: #field_name,
+ index: #field_index,
+ 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(|| {
+ #get_type_fn_body
+ }),
+ }
+ }
+}
+
+fn gen_get_optional_type_reflection(
+ field_type: &syn::Type,
+ engine_crate_path: &syn::Path,
+) -> proc_macro2::TokenStream
+{
+ quote! {
+ 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()
+ }
+}