dom_struct/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use proc_macro::TokenStream;
6use quote::quote;
7use syn::*;
8
9#[proc_macro_attribute]
10pub fn dom_struct(args: TokenStream, input: TokenStream) -> TokenStream {
11    if !args.is_empty() {
12        panic!("#[dom_struct] takes no arguments");
13    }
14    let attributes = quote! {
15        #[derive(deny_public_fields::DenyPublicFields, domobject_derive::DomObject, JSTraceable, MallocSizeOf)]
16        #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
17        #[repr(C)]
18    };
19
20    // Work around https://github.com/rust-lang/rust/issues/46489
21    let attributes: TokenStream = attributes.to_string().parse().unwrap();
22
23    let output: TokenStream = attributes.into_iter().chain(input).collect();
24
25    let item: Item = syn::parse(output).unwrap();
26
27    if let Item::Struct(s) = item {
28        let s2 = s.clone();
29        if !s.generics.params.is_empty() {
30            return quote!(#s2).into();
31        }
32        if let Fields::Named(ref f) = s.fields {
33            let f = f.named.first().expect("Must have at least one field");
34            let ident = f.ident.as_ref().expect("Must have named fields");
35            let name = &s.ident;
36            let ty = &f.ty;
37
38            quote! (
39                #s2
40
41                impl crate::HasParent for #name {
42                    type Parent = #ty;
43                    /// This is used in a type assertion to ensure that
44                    /// the source and webidls agree as to what the parent type is
45                    fn as_parent(&self) -> &#ty {
46                        &self.#ident
47                    }
48                }
49            )
50            .into()
51        } else {
52            panic!("#[dom_struct] only applies to structs with named fields");
53        }
54    } else {
55        panic!("#[dom_struct] only applies to structs");
56    }
57}