Skip to content

Commit 512ccba

Browse files
committed
Remove lots of unused symbols in generated code (function tables)
1 parent 48f6e2d commit 512ccba

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

godot-codegen/src/central_generator.rs

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,18 @@ struct MethodInitGroup {
7979
}
8080

8181
impl MethodInitGroup {
82-
fn new(class_godot_name: &str, class_var: Ident, method_inits: Vec<MethodInit>) -> Self {
82+
fn new(
83+
godot_class_name: &str,
84+
class_var: Option<Ident>,
85+
method_inits: Vec<MethodInit>,
86+
) -> Self {
8387
Self {
84-
class_name: ident(class_godot_name),
88+
class_name: ident(godot_class_name),
8589
// Only create class variable if any methods have been added.
86-
class_var_init: if method_inits.is_empty() {
90+
class_var_init: if class_var.is_none() || method_inits.is_empty() {
8791
None
8892
} else {
89-
let initializer_expr = util::make_sname_ptr(class_godot_name);
93+
let initializer_expr = util::make_sname_ptr(godot_class_name);
9094
Some(quote! {
9195
let #class_var = #initializer_expr;
9296
})
@@ -111,7 +115,7 @@ struct AccessorMethod {
111115

112116
// ----------------------------------------------------------------------------------------------------------------------------------------------
113117

114-
pub struct TypeNames {
118+
pub struct BuiltinName {
115119
/// Name in JSON: "int" or "PackedVector2Array"
116120
pub json_builtin_name: String,
117121

@@ -125,15 +129,15 @@ pub struct TypeNames {
125129
pub sys_variant_type: Ident,
126130
}
127131

128-
impl Eq for TypeNames {}
132+
impl Eq for BuiltinName {}
129133

130-
impl PartialEq for TypeNames {
134+
impl PartialEq for BuiltinName {
131135
fn eq(&self, other: &Self) -> bool {
132136
self.json_builtin_name == other.json_builtin_name
133137
}
134138
}
135139

136-
impl std::hash::Hash for TypeNames {
140+
impl std::hash::Hash for BuiltinName {
137141
fn hash<H: Hasher>(&self, state: &mut H) {
138142
self.json_builtin_name.hash(state);
139143
}
@@ -144,7 +148,7 @@ impl std::hash::Hash for TypeNames {
144148
/// Allows collecting all builtin TypeNames before generating methods
145149
pub(crate) struct BuiltinTypeInfo<'a> {
146150
pub value: i32,
147-
pub type_names: TypeNames,
151+
pub type_names: BuiltinName,
148152

149153
/// If `variant_get_ptr_destructor` returns a non-null function pointer for this type.
150154
/// List is directly sourced from extension_api.json (information would also be in variant_destruct.cpp).
@@ -1010,6 +1014,8 @@ fn populate_class_methods(
10101014
class_ty: &TyName,
10111015
ctx: &mut Context,
10121016
) {
1017+
// Note: already checked outside whether class is active in codegen.
1018+
10131019
let class_var = format_ident!("sname_{}", &class.name);
10141020
let mut method_inits = vec![];
10151021

@@ -1049,23 +1055,32 @@ fn populate_class_methods(
10491055
}
10501056
}
10511057

1052-
table.method_init_groups.push(MethodInitGroup::new(
1053-
&class_ty.godot_ty,
1054-
class_var,
1055-
method_inits,
1056-
));
1057-
table.class_count += 1;
1058+
// No methods available, or all excluded (e.g. virtual ones) -> no group needed.
1059+
if !method_inits.is_empty() {
1060+
table.method_init_groups.push(MethodInitGroup::new(
1061+
&class_ty.godot_ty,
1062+
Some(class_var),
1063+
method_inits,
1064+
));
1065+
1066+
table.class_count += 1;
1067+
}
10581068
}
10591069

10601070
fn populate_builtin_methods(
10611071
table: &mut IndexedMethodTable,
10621072
builtin_class: &BuiltinClass,
1063-
builtin_name: &TypeNames,
1073+
builtin_name: &BuiltinName,
10641074
ctx: &mut Context,
10651075
) {
1066-
let class_var = format_ident!("sname_{}", &builtin_class.name);
10671076
let mut method_inits = vec![];
10681077

1078+
// Skip types such as int, float, bool.
1079+
// TODO separate BuiltinName + TyName needed?
1080+
if special_cases::is_builtin_type_deleted(&TyName::from_godot(&builtin_class.name)) {
1081+
return;
1082+
}
1083+
10691084
for method in option_as_slice(&builtin_class.methods) {
10701085
let builtin_ty = TyName::from_godot(&builtin_class.name);
10711086
if special_cases::is_builtin_deleted(&builtin_ty, method) {
@@ -1105,7 +1120,7 @@ fn populate_builtin_methods(
11051120

11061121
table.method_init_groups.push(MethodInitGroup::new(
11071122
&builtin_class.name,
1108-
class_var,
1123+
None, // load_builtin_method() doesn't need a StringName for the class, as it accepts the VariantType enum.
11091124
method_inits,
11101125
));
11111126
table.class_count += 1;
@@ -1141,7 +1156,7 @@ fn make_class_method_init(
11411156

11421157
fn make_builtin_method_init(
11431158
method: &BuiltinClassMethod,
1144-
type_name: &TypeNames,
1159+
type_name: &BuiltinName,
11451160
index: usize,
11461161
) -> TokenStream {
11471162
let method_name_str = method.name.as_str();
@@ -1229,7 +1244,7 @@ pub(crate) fn collect_builtin_types(api: &ExtensionApi) -> HashMap<String, Built
12291244
operators = None;
12301245
}
12311246

1232-
let type_names = TypeNames {
1247+
let type_names = BuiltinName {
12331248
json_builtin_name: class_name.clone(),
12341249
snake_case: to_snake_case(&class_name),
12351250
//shout_case: shout_case.to_string(),
@@ -1263,7 +1278,7 @@ fn collect_variant_operators(api: &ExtensionApi) -> Vec<&EnumConstant> {
12631278
}
12641279

12651280
fn make_enumerator(
1266-
type_names: &TypeNames,
1281+
type_names: &BuiltinName,
12671282
value: i32,
12681283
ctx: &mut Context,
12691284
) -> (Ident, TokenStream, Literal) {
@@ -1287,7 +1302,7 @@ fn make_opaque_type(name: &str, size: usize) -> TokenStream {
12871302
}
12881303

12891304
fn make_variant_fns(
1290-
type_names: &TypeNames,
1305+
type_names: &BuiltinName,
12911306
has_destructor: bool,
12921307
constructors: Option<&Vec<Constructor>>,
12931308
operators: Option<&Vec<Operator>>,
@@ -1341,7 +1356,7 @@ fn make_variant_fns(
13411356
}
13421357

13431358
fn make_construct_fns(
1344-
type_names: &TypeNames,
1359+
type_names: &BuiltinName,
13451360
constructors: Option<&Vec<Constructor>>,
13461361
builtin_types: &HashMap<String, BuiltinTypeInfo>,
13471362
) -> (TokenStream, TokenStream) {
@@ -1420,7 +1435,7 @@ fn make_construct_fns(
14201435

14211436
/// Lists special cases for useful constructors
14221437
fn make_extra_constructors(
1423-
type_names: &TypeNames,
1438+
type_names: &BuiltinName,
14241439
constructors: &Vec<Constructor>,
14251440
builtin_types: &HashMap<String, BuiltinTypeInfo>,
14261441
) -> (Vec<TokenStream>, Vec<TokenStream>) {
@@ -1464,7 +1479,7 @@ fn make_extra_constructors(
14641479
(extra_decls, extra_inits)
14651480
}
14661481

1467-
fn make_destroy_fns(type_names: &TypeNames, has_destructor: bool) -> (TokenStream, TokenStream) {
1482+
fn make_destroy_fns(type_names: &BuiltinName, has_destructor: bool) -> (TokenStream, TokenStream) {
14681483
if !has_destructor || is_trivial(type_names) {
14691484
return (TokenStream::new(), TokenStream::new());
14701485
}
@@ -1488,7 +1503,7 @@ fn make_destroy_fns(type_names: &TypeNames, has_destructor: bool) -> (TokenStrea
14881503
}
14891504

14901505
fn make_operator_fns(
1491-
type_names: &TypeNames,
1506+
type_names: &BuiltinName,
14921507
operators: Option<&Vec<Operator>>,
14931508
json_name: &str,
14941509
sys_name: &str,
@@ -1529,7 +1544,7 @@ fn make_operator_fns(
15291544

15301545
/// Returns true if the type is so trivial that most of its operations are directly provided by Rust, and there is no need
15311546
/// to expose the construct/destruct/operator methods from Godot
1532-
fn is_trivial(type_names: &TypeNames) -> bool {
1547+
fn is_trivial(type_names: &BuiltinName) -> bool {
15331548
let list = ["bool", "int", "float"];
15341549

15351550
list.contains(&type_names.json_builtin_name.as_str())

godot-codegen/src/codegen_special_cases.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::context::Context;
1111
use crate::{special_cases, TyName};
1212

1313
pub(crate) fn is_builtin_method_excluded(method: &BuiltinClassMethod) -> bool {
14-
// Builtin class methods that need varcall are not currently available in GDExtension.
14+
// TODO Fall back to varcall (recent addition in GDExtension API).
1515
// See https://github.com/godot-rust/gdext/issues/382.
1616
method.is_vararg
1717
}

0 commit comments

Comments
 (0)