Skip to content

Commit

Permalink
function_registered_name => func_name
Browse files Browse the repository at this point in the history
  • Loading branch information
0x53A committed Jan 27, 2025
1 parent 74fab79 commit d005e7e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 39 deletions.
11 changes: 5 additions & 6 deletions godot-macros/src/class/data_models/field_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::class::{
into_signature_info, make_existence_check, make_method_registration, Field, FieldHint,
FuncDefinition,
};
use crate::util::make_function_registered_name_constant;
use crate::util::make_func_name_constant;
use crate::util::KvParser;
use crate::{util, ParseResult};

Expand Down Expand Up @@ -167,7 +167,7 @@ pub struct GetterSetterImpl {
pub function_name: Ident,
pub function_impl: TokenStream,
pub export_token: TokenStream,
pub func_registered_name_const: TokenStream,
pub func_name_const: TokenStream,
}

impl GetterSetterImpl {
Expand Down Expand Up @@ -208,8 +208,7 @@ impl GetterSetterImpl {
}
};

let func_registered_name_const =
make_function_registered_name_constant(class_name, &function_name, None, &[]);
let func_name_const = make_func_name_constant(class_name, &function_name, None, &[]);

let signature = util::parse_signature(signature);
let export_token = make_method_registration(
Expand All @@ -234,7 +233,7 @@ impl GetterSetterImpl {
function_name,
function_impl,
export_token,
func_registered_name_const,
func_name_const,
}
}

Expand All @@ -243,7 +242,7 @@ impl GetterSetterImpl {
function_name: function_name.clone(),
function_impl: TokenStream::new(),
export_token: make_existence_check(function_name),
func_registered_name_const: TokenStream::new(),
func_name_const: TokenStream::new(),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions godot-macros/src/class/data_models/inherent_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::class::{
SignatureInfo, TransferMode,
};
use crate::util::{
bail, c_str, error_fn, format_function_registered_name_struct_name, ident,
make_function_registered_name_constants, replace_class_in_path, require_api_version, KvParser,
bail, c_str, error_fn, format_func_name_struct_name, ident, make_func_name_constants,
replace_class_in_path, require_api_version, KvParser,
};
use crate::{handle_mutually_exclusive_keys, util, ParseResult};

Expand Down Expand Up @@ -95,7 +95,7 @@ pub fn transform_inherent_impl(

// This is the container struct that holds the names of all registered #[func]s.
// (The struct is declared by the macro derive_godot_class.)
let class_functions_name = format_function_registered_name_struct_name(&class_name);
let class_functions_name = format_func_name_struct_name(&class_name);
// As the impl block could be of the form "path::class", and we add a second impl block below, we need the full path, not just the class name.
let this_class_full_path = impl_block.self_ty.as_path().ok_or(error_fn(
"unexpected: the function already checked 'as_path' above in validate_impl",
Expand All @@ -104,7 +104,7 @@ pub fn transform_inherent_impl(
let class_functions_path: Path =
replace_class_in_path(this_class_full_path, class_functions_name);
// For each #[func] in this impl block, we create one constant.
let func_name_constants = make_function_registered_name_constants(&funcs, &class_name);
let func_name_constants = make_func_name_constants(&funcs, &class_name);

let signal_registrations = make_signal_registrations(signals, &class_name_obj);

Expand Down
25 changes: 11 additions & 14 deletions godot-macros/src/class/data_models/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
//! Parsing the `var` and `export` attributes on fields.
use crate::class::{Field, FieldVar, Fields, GetSet, GetterSetterImpl, UsageFlags};
use crate::util::{
format_function_registered_name_constant_name, format_function_registered_name_struct_name,
ident,
};
use crate::util::{format_func_name_constant_name, format_func_name_struct_name, ident};
use proc_macro2::{Ident, TokenStream};
use quote::quote;

Expand Down Expand Up @@ -42,7 +39,7 @@ impl FieldHint {

pub fn make_property_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
let mut getter_setter_impls = Vec::new();
let mut func_registered_name_consts = Vec::new();
let mut func_name_consts = Vec::new();
let mut export_tokens = Vec::new();

for field in &fields.all_fields {
Expand Down Expand Up @@ -144,14 +141,14 @@ pub fn make_property_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
let getter_tokens = make_getter_setter(
getter.to_impl(class_name, GetSet::Get, field),
&mut getter_setter_impls,
&mut func_registered_name_consts,
&mut func_name_consts,
&mut export_tokens,
class_name,
);
let setter_tokens = make_getter_setter(
setter.to_impl(class_name, GetSet::Set, field),
&mut getter_setter_impls,
&mut func_registered_name_consts,
&mut func_name_consts,
&mut export_tokens,
class_name,
);
Expand All @@ -169,15 +166,15 @@ pub fn make_property_impl(class_name: &Ident, fields: &Fields) -> TokenStream {

// For each generated #[func], add a const.
// This is the name of the container struct, which is declared by the derive macro GodotClass.
let class_functions_name = format_function_registered_name_struct_name(class_name);
let class_functions_name = format_func_name_struct_name(class_name);

quote! {
impl #class_name {
#(#getter_setter_impls)*
}

impl #class_functions_name {
#(#func_registered_name_consts)*
#(#func_name_consts)*
}

impl ::godot::obj::cap::ImplementsGodotExports for #class_name {
Expand All @@ -195,7 +192,7 @@ pub fn make_property_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
fn make_getter_setter(
getter_setter_impl: Option<GetterSetterImpl>,
getter_setter_impls: &mut Vec<TokenStream>,
func_registered_name_consts: &mut Vec<TokenStream>,
func_name_consts: &mut Vec<TokenStream>,
export_tokens: &mut Vec<TokenStream>,
class_name: &Ident,
) -> TokenStream {
Expand All @@ -204,19 +201,19 @@ fn make_getter_setter(
function_name,
function_impl,
export_token,
func_registered_name_const,
func_name_const,
} = getter_impl;

getter_setter_impls.push(function_impl);
func_registered_name_consts.push(func_registered_name_const);
func_name_consts.push(func_name_const);
export_tokens.push(export_token);

let getter_setter_name = function_name.to_string();

let class_functions_name = format_function_registered_name_struct_name(class_name);
let class_functions_name = format_func_name_struct_name(class_name);

let getter_setter_fn_const =
format_function_registered_name_constant_name(class_name, &ident(&getter_setter_name));
format_func_name_constant_name(class_name, &ident(&getter_setter_name));
quote! { #class_functions_name::#getter_setter_fn_const }
} else {
quote! { "" }
Expand Down
6 changes: 3 additions & 3 deletions godot-macros/src/class/derive_godot_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::class::{
FieldVar, Fields, SignatureInfo,
};
use crate::util::{
bail, error, format_function_registered_name_struct_name, ident, path_ends_with_complex,
require_api_version, KvParser,
bail, error, format_func_name_struct_name, ident, path_ends_with_complex, require_api_version,
KvParser,
};
use crate::{handle_mutually_exclusive_keys, util, ParseResult};

Expand Down Expand Up @@ -138,7 +138,7 @@ pub fn derive_godot_class(item: venial::Item) -> ParseResult<TokenStream> {
}

// Declares a dummy struct that, for each #[func], holds a constant that maps the rust name to the name under which it is registered in godot.
let class_functions_name = format_function_registered_name_struct_name(class_name);
let class_functions_name = format_func_name_struct_name(class_name);
let class_functions_struct = quote! {
#[doc(hidden)]
pub struct #class_functions_name { }
Expand Down
18 changes: 6 additions & 12 deletions godot-macros/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,7 @@ pub fn venial_parse_meta(

// util functions for handling #[func]s and #[var(get=f, set=f)]

pub fn make_function_registered_name_constants(
funcs: &[FuncDefinition],
class_name: &Ident,
) -> Vec<TokenStream> {
pub fn make_func_name_constants(funcs: &[FuncDefinition], class_name: &Ident) -> Vec<TokenStream> {
funcs
.iter()
.map(|func| {
Expand All @@ -332,7 +329,7 @@ pub fn make_function_registered_name_constants(
.into_iter()
.collect::<Vec<_>>();

make_function_registered_name_constant(
make_func_name_constant(
class_name,
&func.signature_info.method_name,
func.registered_name.as_ref(),
Expand All @@ -344,13 +341,13 @@ pub fn make_function_registered_name_constants(

/// Funcs can be renamed with `#[func(rename=new_name) fn f();`.
/// To be able to access the renamed function name at a later point, it is saved in a string constant.
pub fn make_function_registered_name_constant(
pub fn make_func_name_constant(
class_name: &Ident,
func_name: &Ident,
registered_name: Option<&String>,
attributes: &[&Attribute],
) -> TokenStream {
let const_name = format_function_registered_name_constant_name(class_name, func_name);
let const_name = format_func_name_constant_name(class_name, func_name);
let const_value = match &registered_name {
Some(renamed) => renamed.to_string(),
None => func_name.to_string(),
Expand Down Expand Up @@ -399,14 +396,11 @@ pub fn replace_class_in_path(path: Path, new_class: Ident) -> Path {
}

/// Returns the name of the constant that will be autogenerated.
pub fn format_function_registered_name_constant_name(
_class_name: &Ident,
func_name: &Ident,
) -> Ident {
pub fn format_func_name_constant_name(_class_name: &Ident, func_name: &Ident) -> Ident {
format_ident!("{func_name}")
}

/// Returns the name of the dummy struct that's used as container for all function name constants.
pub fn format_function_registered_name_struct_name(class_name: &Ident) -> Ident {
pub fn format_func_name_struct_name(class_name: &Ident) -> Ident {
format_ident!("__gdext_{class_name}_Functions")
}

0 comments on commit d005e7e

Please sign in to comment.