Skip to content

Commit 35194d1

Browse files
committed
Compute is_unsafe property at JSON mapping time
1 parent bb39acd commit 35194d1

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

godot-codegen/src/generator/functions_common.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub fn make_function_definition(
122122
// Thus, let's keep things simple and more conservative.
123123
let (maybe_unsafe, maybe_safety_doc) = if let Some(safety_doc) = safety_doc {
124124
(quote! { unsafe }, safety_doc)
125-
} else if function_uses_pointers(sig) {
125+
} else if sig.common().is_unsafe {
126126
(
127127
quote! { unsafe },
128128
quote! {
@@ -637,15 +637,3 @@ pub(crate) fn make_params_exprs_virtual<'a>(
637637

638638
ret
639639
}
640-
641-
fn function_uses_pointers(sig: &dyn Function) -> bool {
642-
let has_pointer_params = sig
643-
.params()
644-
.iter()
645-
.any(|param| matches!(param.type_, RustTy::RawPointer { .. }));
646-
647-
let has_pointer_return = matches!(sig.return_value().type_, Some(RustTy::RawPointer { .. }));
648-
649-
// No short-circuiting due to variable decls, but that's fine.
650-
has_pointer_params || has_pointer_return
651-
}

godot-codegen/src/models/domain.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ pub struct FunctionCommon {
284284
pub is_vararg: bool,
285285
pub is_private: bool,
286286
pub is_virtual_required: bool,
287+
/// Whether raw pointers appear in signature. Affects safety, and in case of virtual methods, the name.
288+
pub is_unsafe: bool,
287289
pub direction: FnDirection,
288290
}
289291

godot-codegen/src/models/domain_mapping.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::models::domain::{
1010
BuildConfiguration, BuiltinClass, BuiltinMethod, BuiltinSize, BuiltinVariant, Class,
1111
ClassCommons, ClassConstant, ClassConstantValue, ClassMethod, ClassSignal, Constructor, Enum,
1212
Enumerator, EnumeratorValue, ExtensionApi, FnDirection, FnParam, FnQualifier, FnReturn,
13-
FunctionCommon, GodotApiVersion, ModName, NativeStructure, Operator, Singleton, TyName,
13+
FunctionCommon, GodotApiVersion, ModName, NativeStructure, Operator, RustTy, Singleton, TyName,
1414
UtilityFunction,
1515
};
1616
use crate::models::json::{
@@ -374,6 +374,7 @@ impl BuiltinMethod {
374374
is_vararg: method.is_vararg,
375375
is_private: false, // See 'exposed' below. Could be special_cases::is_method_private(builtin_name, &method.name),
376376
is_virtual_required: false,
377+
is_unsafe: false, // Builtin methods don't use raw pointers.
377378
direction: FnDirection::Outbound {
378379
hash: method.hash.expect("hash absent for builtin method"),
379380
},
@@ -507,15 +508,20 @@ impl ClassMethod {
507508
is_required_in_json
508509
};
509510

511+
let parameters = FnParam::new_range(&method.arguments, ctx);
512+
let return_value = FnReturn::new(&method.return_value, ctx);
513+
let is_unsafe = Self::function_uses_pointers(&parameters, &return_value);
514+
510515
Some(Self {
511516
common: FunctionCommon {
512517
name: rust_method_name.to_string(),
513518
godot_name: godot_method_name,
514-
parameters: FnParam::new_range(&method.arguments, ctx),
515-
return_value: FnReturn::new(&method.return_value, ctx),
519+
parameters,
520+
return_value,
516521
is_vararg: method.is_vararg,
517522
is_private,
518523
is_virtual_required,
524+
is_unsafe,
519525
direction,
520526
},
521527
qualifier,
@@ -531,6 +537,17 @@ impl ClassMethod {
531537

532538
special_cases::maybe_rename_virtual_method(class_name, method_name)
533539
}
540+
541+
fn function_uses_pointers(parameters: &[FnParam], return_value: &FnReturn) -> bool {
542+
let has_pointer_params = parameters
543+
.iter()
544+
.any(|param| matches!(param.type_, RustTy::RawPointer { .. }));
545+
546+
let has_pointer_return = matches!(return_value.type_, Some(RustTy::RawPointer { .. }));
547+
548+
// No short-circuiting due to variable decls, but that's fine.
549+
has_pointer_params || has_pointer_return
550+
}
534551
}
535552

536553
impl ClassSignal {
@@ -584,6 +601,7 @@ impl UtilityFunction {
584601
is_vararg: function.is_vararg,
585602
is_private,
586603
is_virtual_required: false,
604+
is_unsafe: false, // Utility functions don't use raw pointers.
587605
direction: FnDirection::Outbound {
588606
hash: function.hash,
589607
},

0 commit comments

Comments
 (0)