diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 0ec94c72df2..a09c689839a 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -2055,14 +2055,19 @@ bool GDScriptInstance::execute_access_restriction(const StringName &p_member_nam if (p_member_access_restriction.access_restriction == GDScriptParser::Node::ACCESS_RESTRICTION_PUBLIC) { return true; } + + ERR_FAIL_NULL_V_EDMSG(p_current_script, false, R"(Trying to execute access protection on a null script...)"); + if (p_current_script->local_name == p_member_access_restriction.access_member_owner || p_member_access_restriction.access_member_owner == p_current_script->local_name) { return true; } else if (p_member_access_restriction.access_restriction == GDScriptParser::Node::ACCESS_RESTRICTION_PRIVATE) { - print_line(vformat(R"(Error private)")); - ERR_FAIL_V_MSG(false, vformat("Invalid access to %s (access level: private, owner: %s)", p_member_name, p_member_access_restriction.access_member_owner)); + String err = vformat("Invalid access to %s (access level: private, owner: %s)", p_member_name, p_member_access_restriction.access_member_owner); + print_error(err); + ERR_FAIL_V_MSG(false, err); } else if (p_member_access_restriction.access_restriction == GDScriptParser::Node::ACCESS_RESTRICTION_PROTECTED && !ClassDB::is_parent_class(p_current_script->local_name, p_member_access_restriction.access_member_owner)) { - print_line(vformat(R"(Error protected)")); - ERR_FAIL_V_MSG(false, vformat("Invalid access to %s (access level: protected, owner: %s)", p_member_name, p_member_access_restriction.access_member_owner)); + String err = vformat("Invalid access to %s (access level: protected, owner: %s)", p_member_name, p_member_access_restriction.access_member_owner); + print_error(err); + ERR_FAIL_V_MSG(false, err); } return true; diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index 64a5ce8a439..adcce3fabc2 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -296,7 +296,7 @@ Error GDScriptAnalyzer::check_class_member_name_conflict(const GDScriptParser::C return OK; } -bool GDScriptAnalyzer::execute_access_protection(const GDScriptParser::ClassNode *p_derived_class, const GDScriptParser::Node *p_protected_member, const Vector &p_super_classes, const GDScriptParser::Node *p_node, const bool p_is_call) { +bool GDScriptAnalyzer::execute_access_protection(const GDScriptParser::ClassNode *p_derived_class, const GDScriptParser::Node *p_protected_member, const GDScriptParser::Node *p_node, const bool p_is_call) { if (p_protected_member->access_restriction == GDScriptParser::Node::ACCESS_RESTRICTION_PUBLIC) { return true; } @@ -307,12 +307,12 @@ bool GDScriptAnalyzer::execute_access_protection(const GDScriptParser::ClassNode const String member_type = p_protected_member->type == GDScriptParser::Node::FUNCTION ? "method" : (p_protected_member->type == GDScriptParser::Node::Type::SIGNAL ? "signal" : "property"); const String action = p_is_call ? "call" : "access"; - const bool is_from_non_derived = !p_super_classes.has(p_derived_class->identifier->name); + const bool is_from_non_derived = !ClassDB::is_parent_class(p_derived_class->identifier->name, p_protected_member->access_member_owner); const GDScriptParser::AssignableNode *member_assignable = static_cast(p_protected_member); const StringName member_name = member_assignable ? member_assignable->identifier->name : ""; - switch (execute_access_protection_global(p_derived_class->identifier->name, p_protected_member, p_super_classes, p_node)) { + switch (execute_access_protection_global(p_derived_class->identifier->name, p_protected_member, p_node)) { case GDScriptAnalyzer::AccessRestrictionError::ACCESS_PRIVATE: push_error(vformat(R"*(Could not %s %s "%s%s" in %s class, because it is private.)*", action, member_type, member_name, p_is_call ? "()" : "", is_from_non_derived ? "external" : "super"), p_node); return false; @@ -326,7 +326,7 @@ bool GDScriptAnalyzer::execute_access_protection(const GDScriptParser::ClassNode return true; } -GDScriptAnalyzer::AccessRestrictionError GDScriptAnalyzer::execute_access_protection_global(const StringName &p_derived_class, const GDScriptParser::Node *p_protected_member, const Vector &p_super_classes, const GDScriptParser::Node *p_node) { +GDScriptAnalyzer::AccessRestrictionError GDScriptAnalyzer::execute_access_protection_global(const StringName &p_derived_class, const GDScriptParser::Node *p_protected_member, const GDScriptParser::Node *p_node) { if (p_protected_member->access_restriction == GDScriptParser::Node::ACCESS_RESTRICTION_PUBLIC) { return GDScriptAnalyzer::AccessRestrictionError::ACCESS_OK; } @@ -338,7 +338,7 @@ GDScriptAnalyzer::AccessRestrictionError GDScriptAnalyzer::execute_access_protec return GDScriptAnalyzer::AccessRestrictionError::ACCESS_OK; } else if (p_protected_member->access_restriction == GDScriptParser::Node::ACCESS_RESTRICTION_PRIVATE) { return GDScriptAnalyzer::AccessRestrictionError::ACCESS_PRIVATE; - } else if (p_protected_member->access_restriction == GDScriptParser::Node::ACCESS_RESTRICTION_PROTECTED && !p_super_classes.has(p_derived_class)) { + } else if (p_protected_member->access_restriction == GDScriptParser::Node::ACCESS_RESTRICTION_PROTECTED && !ClassDB::is_parent_class(p_derived_class, p_protected_member->access_member_owner)) { return GDScriptAnalyzer::AccessRestrictionError::ACCESS_PROTECTED; } diff --git a/modules/gdscript/gdscript_analyzer.h b/modules/gdscript/gdscript_analyzer.h index a2bd21e7e81..e0850f53000 100644 --- a/modules/gdscript/gdscript_analyzer.h +++ b/modules/gdscript/gdscript_analyzer.h @@ -67,7 +67,7 @@ class GDScriptAnalyzer { Error check_native_member_name_conflict(const StringName &p_member_name, const GDScriptParser::Node *p_member_node, const StringName &p_native_type_string); Error check_class_member_name_conflict(const GDScriptParser::ClassNode *p_class_node, const StringName &p_member_name, const GDScriptParser::Node *p_member_node); - bool execute_access_protection(const GDScriptParser::ClassNode *p_derived_class, const GDScriptParser::Node *p_protected_member, const Vector &p_super_classes, const GDScriptParser::Node *p_node, const bool p_is_call = false); + bool execute_access_protection(const GDScriptParser::ClassNode *p_derived_class, const GDScriptParser::Node *p_protected_member, const GDScriptParser::Node *p_node, const bool p_is_call = false); void get_class_node_current_scope_classes(GDScriptParser::ClassNode *p_node, List *p_list, GDScriptParser::Node *p_source); @@ -176,7 +176,7 @@ class GDScriptAnalyzer { Variant make_variable_default_value(GDScriptParser::VariableNode *p_variable); static bool check_type_compatibility(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion = false, const GDScriptParser::Node *p_source_node = nullptr); - static AccessRestrictionError execute_access_protection_global(const StringName &p_derived_class, const GDScriptParser::Node *p_protected_member, const Vector &p_super_classes, const GDScriptParser::Node *p_node); + static AccessRestrictionError execute_access_protection_global(const StringName &p_derived_class, const GDScriptParser::Node *p_protected_member, const GDScriptParser::Node *p_node); GDScriptAnalyzer(GDScriptParser *p_parser); };