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
extern crate proc_macro; extern crate proc_macro2; #[macro_use] extern crate syn; #[macro_use] extern crate quote; use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use syn::{AttributeArgs, DeriveInput, ItemFn, ItemImpl}; mod methods; mod native_script; mod profiled; mod variant; #[proc_macro_attribute] pub fn methods(meta: TokenStream, input: TokenStream) -> TokenStream { if syn::parse::<syn::parse::Nothing>(meta.clone()).is_err() { let err = syn::Error::new_spanned( TokenStream2::from(meta), "#[methods] does not take parameters.", ); return error_with_input(input, err); } let impl_block = match syn::parse::<ItemImpl>(input.clone()) { Ok(impl_block) => impl_block, Err(err) => return error_with_input(input, err), }; fn error_with_input(input: TokenStream, err: syn::Error) -> TokenStream { let mut err = TokenStream::from(err.to_compile_error()); err.extend(std::iter::once(input)); err } TokenStream::from(methods::derive_methods(impl_block)) } /// Makes a function profiled in Godot's built-in profiler. This macro automatically /// creates a tag using the name of the current module and the function by default. /// /// This attribute may also be used on non-exported functions. If the GDNative API isn't /// initialized when the function is called, the data will be ignored silently. /// /// A custom tag can also be provided using the `tag` option. /// /// See the `gdnative::nativescript::profiling` for a lower-level API to the profiler with /// more control. /// /// # Examples /// /// ```ignore /// mod foo { /// // This function will show up as `foo/bar` under Script Functions. /// #[gdnative::profiled] /// fn bar() { /// std::thread::sleep(std::time::Duration::from_millis(1)); /// } /// } /// ``` /// /// ```ignore /// // This function will show up as `my_custom_tag` under Script Functions. /// #[gdnative::profiled(tag = "my_custom_tag")] /// fn baz() { /// std::thread::sleep(std::time::Duration::from_millis(1)); /// } /// ``` #[proc_macro_attribute] pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream { let args = parse_macro_input!(meta as AttributeArgs); let item_fn = parse_macro_input!(input as ItemFn); match profiled::derive_profiled(args, item_fn) { Ok(tokens) => tokens.into(), Err(err) => err.to_compile_error().into(), } } /// Makes it possible to use a type as a NativeScript. /// /// ## Required attributes /// /// The following attributes are required on the type deriving `NativeClass`: /// /// ### `#[inherit(gdnative::api::BaseClass)]` /// /// Sets `gdnative::api::BaseClass` as the base class for the script. This *must* be /// a type from the generated Godot API (that implements `GodotObject`). All `owner` /// arguments of exported methods must be references (`TRef`, `Ref`, or `&`) to this /// type. /// /// Inheritance from other scripts, either in Rust or other languages, is /// not supported. /// /// ## Optional type attributes /// /// Behavior of the derive macro can be customized using attribute on the type: /// /// ### `#[user_data(gdnative::user_data::SomeWrapper<Self>)]` /// /// Use the given type as the user-data wrapper. See the module-level docs on /// `gdnative::user_data` for more information. /// /// ### `#[register_with(path::to::function)]` /// /// Use a custom function to register signals, properties or methods, in addition /// to the one generated by `#[methods]`: /// /// ```ignore /// #[derive(NativeClass)] /// #[inherit(Reference)] /// #[register_with(my_register_function)] /// struct Foo; /// /// fn my_register_function(builder: &ClassBuilder<Foo>) { /// builder.add_signal(Signal { name: "foo", args: &[] }); /// builder.add_property::<f32>("bar") /// .with_getter(|_, _| 42.0) /// .with_hint(FloatHint::Range(RangeHint::new(0.0, 100.0))) /// .done(); /// } /// ``` /// /// ### `#[no_constructor]` /// /// Indicates that this type has no zero-argument constructor. Instances of such /// scripts can only be created from Rust using `Instance::emplace`. `Instance::new` /// or `ScriptName.new` from GDScript will result in panics at runtime. /// /// See documentation on `Instance::emplace` for an example on how this can be used. /// /// ## Optional field attributes /// /// ### `#[property]` /// /// Convenience attribute to register a field as a property. Possible arguments for /// the attribute are: /// /// - `path = "my_category/my_property_name"` /// /// Puts the property under the `my_category` category and renames it to /// `my_property_name` in the inspector and for GDScript. /// /// - `default = 42.0` /// /// Sets the default value *in the inspector* for this property. The setter is *not* /// guaranteed to be called by the engine with the value. /// /// - `before_get` / `after_get` / `before_set` / `after_set` `= "Self::hook_method"` /// /// Call hook methods with `self` and `owner` before and/or after the generated property /// accessors. /// /// - `no_editor` /// /// Hides the property from the editor. Does not prevent it from being sent over network or saved in storage. #[proc_macro_derive( NativeClass, attributes( inherit, export, opt, user_data, property, register_with, no_constructor ) )] pub fn derive_native_class(input: TokenStream) -> TokenStream { // Converting the proc_macro::TokenStream into non proc_macro types so that tests // can be written against the inner functions. let derive_input = syn::parse_macro_input!(input as DeriveInput); // Implement NativeClass for the input native_script::derive_native_class(&derive_input).map_or_else( |err| { // Silence the other errors that happen because NativeClass is not implemented let empty_nativeclass = native_script::impl_empty_nativeclass(&derive_input); let err = err.to_compile_error(); TokenStream::from(quote! { #empty_nativeclass #err }) }, std::convert::identity, ) } #[proc_macro_derive(ToVariant, attributes(variant))] pub fn derive_to_variant(input: TokenStream) -> TokenStream { match variant::derive_to_variant(variant::ToVariantTrait::ToVariant, input) { Ok(stream) => stream.into(), Err(err) => err.to_compile_error().into(), } } #[proc_macro_derive(OwnedToVariant, attributes(variant))] pub fn derive_owned_to_variant(input: TokenStream) -> TokenStream { match variant::derive_to_variant(variant::ToVariantTrait::OwnedToVariant, input) { Ok(stream) => stream.into(), Err(err) => err.to_compile_error().into(), } } #[proc_macro_derive(FromVariant, attributes(variant))] pub fn derive_from_variant(input: TokenStream) -> TokenStream { let derive_input = syn::parse_macro_input!(input as syn::DeriveInput); match variant::derive_from_variant(derive_input) { Ok(stream) => stream.into(), Err(err) => err.to_compile_error().into(), } }