Skip to content

Commit ecbd668

Browse files
committed
Merge pull request godotengine#90223 from clubby789/optimize-static
Don't pass `self` when calling a static function from a non-static context
2 parents 0ff056e + 164b34a commit ecbd668

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

modules/gdscript/gdscript_analyzer.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3293,6 +3293,7 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
32933293
if (get_function_signature(p_call, is_constructor, base_type, p_call->function_name, return_type, par_types, default_arg_count, method_flags)) {
32943294
// If the method is implemented in the class hierarchy, the virtual flag will not be set for that MethodInfo and the search stops there.
32953295
// Virtual check only possible for super() calls because class hierarchy is known. Node/Objects may have scripts attached we don't know of at compile-time.
3296+
p_call->is_static = method_flags.has_flag(METHOD_FLAG_STATIC);
32963297
if (p_call->is_super && method_flags.has_flag(METHOD_FLAG_VIRTUAL)) {
32973298
push_error(vformat(R"*(Cannot call the parent class' virtual function "%s()" because it hasn't been defined.)*", p_call->function_name), p_call);
32983299
}
@@ -3311,7 +3312,7 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
33113312
base_type.is_meta_type = false;
33123313
}
33133314

3314-
if (is_self && static_context && !method_flags.has_flag(METHOD_FLAG_STATIC)) {
3315+
if (is_self && static_context && !p_call->is_static) {
33153316
// Get the parent function above any lambda.
33163317
GDScriptParser::FunctionNode *parent_function = parser->current_function;
33173318
while (parent_function && parent_function->source_lambda) {
@@ -3323,10 +3324,10 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
33233324
} else {
33243325
push_error(vformat(R"*(Cannot call non-static function "%s()" from a static variable initializer.)*", p_call->function_name), p_call);
33253326
}
3326-
} else if (!is_self && base_type.is_meta_type && !method_flags.has_flag(METHOD_FLAG_STATIC)) {
3327+
} else if (!is_self && base_type.is_meta_type && !p_call->is_static) {
33273328
base_type.is_meta_type = false; // For `to_string()`.
33283329
push_error(vformat(R"*(Cannot call non-static function "%s()" on the class "%s" directly. Make an instance instead.)*", p_call->function_name, base_type.to_string()), p_call);
3329-
} else if (is_self && !method_flags.has_flag(METHOD_FLAG_STATIC)) {
3330+
} else if (is_self && !p_call->is_static) {
33303331
mark_lambda_use_self();
33313332
}
33323333

modules/gdscript/gdscript_compiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
648648
// Not exact arguments, but still can use method bind call.
649649
gen->write_call_method_bind(result, self, method, arguments);
650650
}
651-
} else if (codegen.is_static || (codegen.function_node && codegen.function_node->is_static) || call->function_name == "new") {
651+
} else if (call->is_static || codegen.is_static || (codegen.function_node && codegen.function_node->is_static) || call->function_name == "new") {
652652
GDScriptCodeGenerator::Address self;
653653
self.mode = GDScriptCodeGenerator::Address::CLASS;
654654
if (is_awaited) {

modules/gdscript/gdscript_parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ class GDScriptParser {
498498
Vector<ExpressionNode *> arguments;
499499
StringName function_name;
500500
bool is_super = false;
501+
bool is_static = false;
501502

502503
CallNode() {
503504
type = CALL;

0 commit comments

Comments
 (0)