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
// Take a look at the license at the top of the repository in the LICENSE file.

use proc_macro2::TokenStream;
use proc_macro_error::abort_call_site;
use quote::quote;

pub const WRONG_PLACE_MSG: &str =
    "This macro should be used on `impl` block for `glib::ObjectSubclass` trait";

pub fn impl_object_subclass(input: &syn::ItemImpl) -> TokenStream {
    let mut has_new = false;
    let mut has_interfaces = false;
    let mut has_instance = false;
    let mut has_class = false;
    for item in &input.items {
        match item {
            syn::ImplItem::Method(method) => {
                let name = method.sig.ident.to_string();
                if name == "new" || name == "with_class" {
                    has_new = true;
                }
            }
            syn::ImplItem::Type(type_) => {
                let name = type_.ident.to_string();
                if name == "Interfaces" {
                    has_interfaces = true;
                } else if name == "Instance" {
                    has_instance = true;
                } else if name == "Class" {
                    has_class = true;
                }
            }
            _ => {}
        }
    }

    let syn::ItemImpl {
        attrs,
        generics,
        trait_,
        self_ty,
        items,
        ..
    } = &input;

    let interfaces_opt = if has_interfaces {
        None
    } else {
        Some(quote!(
            type Interfaces = ();
        ))
    };

    let new_opt = if has_new {
        None
    } else {
        Some(quote! {
            fn new() -> Self {
                std::default::Default::default()
            }
        })
    };

    let crate_ident = crate::utils::crate_ident_new();

    let class_opt = if has_class {
        None
    } else {
        Some(quote!(type Class = #crate_ident::subclass::basic::ClassStruct<Self>;))
    };

    let instance_opt = if has_instance {
        None
    } else {
        Some(quote!(type Instance = #crate_ident::subclass::basic::InstanceStruct<Self>;))
    };

    let trait_path = match &trait_ {
        Some(path) => &path.1,
        None => abort_call_site!(WRONG_PLACE_MSG),
    };

    quote! {
        #(#attrs)*
        impl#generics #trait_path for #self_ty {
            #interfaces_opt
            #class_opt
            #instance_opt
            #new_opt
            #(#items)*
        }

        unsafe impl #crate_ident::subclass::types::ObjectSubclassType for #self_ty {
            fn type_data() -> std::ptr::NonNull<#crate_ident::subclass::TypeData> {
                static mut DATA: #crate_ident::subclass::TypeData = #crate_ident::subclass::TypeData {
                    type_: #crate_ident::Type::INVALID,
                    parent_class: std::ptr::null_mut(),
                    parent_ifaces: None,
                    class_data: None,
                    private_offset: 0,
                    private_imp_offset: 0,
                };

                unsafe { std::ptr::NonNull::new_unchecked(&mut DATA) }
            }

            fn type_() -> #crate_ident::Type {
                static ONCE: std::sync::Once = std::sync::Once::new();

                ONCE.call_once(|| {
                    #crate_ident::subclass::register_type::<Self>();
                });

                unsafe {
                    let data = Self::type_data();
                    let type_ = data.as_ref().type_();
                    assert!(type_.is_valid());

                    type_
                }
            }
        }
    }
}