Skip to content

Commit

Permalink
Detect whether virtual functions are required to override
Browse files Browse the repository at this point in the history
From Godot 4.4 onward, we can use the GDExtension-provided information, which states whether
virtual functions have a default implementation or are required to be overridden.
  • Loading branch information
Bromeon committed Sep 28, 2024
1 parent 6e46fa4 commit b1db236
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
21 changes: 17 additions & 4 deletions godot-codegen/src/models/domain_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,22 @@ impl ClassMethod {
FnQualifier::from_const_static(is_actually_const, method.is_static)
};

// Since Godot 4.4, GDExtension advertises whether virtual methods have a default implementation or are required to be overridden.
#[cfg(before_api = "4.4")]
let is_virtual_required = special_cases::is_virtual_method_required(
&class_name.rust_ty.to_string(),
rust_method_name,
);

#[cfg(since_api = "4.4")]
let is_virtual_required = method.is_virtual
&& method.is_required.unwrap_or_else(|| {
panic!(
"virtual method {}::{} lacks field `is_required`",
class_name.rust_ty, rust_method_name
);
});

Some(Self {
common: FunctionCommon {
name: rust_method_name.to_string(),
Expand All @@ -464,10 +480,7 @@ impl ClassMethod {
return_value: FnReturn::new(&method.return_value, ctx),
is_vararg: method.is_vararg,
is_private,
is_virtual_required: special_cases::is_virtual_method_required(
&class_name.rust_ty.to_string(),
rust_method_name,
),
is_virtual_required,
direction,
},
qualifier,
Expand Down
2 changes: 2 additions & 0 deletions godot-codegen/src/models/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ pub struct JsonClassMethod {
pub is_vararg: bool,
pub is_static: bool,
pub is_virtual: bool,
#[cfg(since_api = "4.4")]
pub is_required: Option<bool>, // Only virtual functions have this field.
pub hash: Option<i64>,
pub return_value: Option<JsonMethodReturn>,
pub arguments: Option<Vec<JsonMethodArg>>,
Expand Down
1 change: 1 addition & 0 deletions godot-codegen/src/special_cases/special_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ pub fn get_interface_extra_docs(trait_name: &str) -> Option<&'static str> {
}
}

#[cfg(before_api = "4.4")]
pub fn is_virtual_method_required(class_name: &str, method: &str) -> bool {
match (class_name, method) {
("ScriptLanguageExtension", _) => method != "get_doc_comment_delimiters",
Expand Down

0 comments on commit b1db236

Please sign in to comment.