Skip to content

Commit

Permalink
trying doing access protection for method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazy-Rabbit-2001 committed Oct 17, 2024
1 parent dba9aae commit 8c028ad
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
21 changes: 21 additions & 0 deletions modules/gdscript/gdscript_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,25 @@ bool GDScriptAnalyzer::execute_access_protection(const GDScriptParser::ClassNode
return true;
}

bool GDScriptAnalyzer::execute_access_protection(const GDScriptParser::CallNode *p_call_node, const GDScriptParser::ClassNode *p_derived_class, const StringName &p_super_class_name) {
ERR_FAIL_COND_V_MSG(!p_derived_class || p_super_class_name.is_empty(), false, R"(Could not resolve the derived or super class node...)");

if (p_call_node) {
const bool are_different_classes = !p_derived_class->is_same_as(p_super_class_name);
const bool is_from_non_derived = !p_derived_class->is_derived_from(p_super_class_name);
if (p_call_node->access_restriction == GDScriptParser::Node::ACCESS_RESTRICTION_PRIVATE && are_different_classes) {
push_error(vformat(R"*(Could not call method "%s()" in %s class, because it is private.)*", p_call_node->function_name, is_from_non_derived ? "external" : "super"), p_call_node);
return false;
}
if (p_call_node->access_restriction == GDScriptParser::Node::ACCESS_RESTRICTION_PROTECTED && is_from_non_derived) {
push_error(vformat(R"*(Could not call method "%s()" in external class, because it is protected by class "%s".)*", p_call_node->function_name, p_super_class_name), p_call_node);
return false;
}
}

return true;
}

void GDScriptAnalyzer::get_class_node_current_scope_classes(GDScriptParser::ClassNode *p_node, List<GDScriptParser::ClassNode *> *p_list, GDScriptParser::Node *p_source) {
ERR_FAIL_NULL(p_node);
ERR_FAIL_NULL(p_list);
Expand Down Expand Up @@ -3492,6 +3511,8 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
case Callable::CallError::CALL_OK:
p_call->is_constant = true;
p_call->reduced_value = value;
print_line(vformat(R"(Current call: %s; owner class: %s)", p_call->function_name, p_call->accessible_class_name));
execute_access_protection(p_call, parser->current_class, p_call->accessible_class_name);
break;
}
} else {
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript_analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class GDScriptAnalyzer {
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::Member &p_member, const GDScriptParser::ClassNode *p_derived_class, const GDScriptParser::ClassNode *p_super_class, const GDScriptParser::Node *p_source);
bool execute_access_protection(const GDScriptParser::CallNode *p_call_node, const GDScriptParser::ClassNode *p_derived_class, const StringName &p_super_class_name);

void get_class_node_current_scope_classes(GDScriptParser::ClassNode *p_node, List<GDScriptParser::ClassNode *> *p_list, GDScriptParser::Node *p_source);

Expand Down
2 changes: 2 additions & 0 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4209,6 +4209,7 @@ bool GDScriptParser::access_private_annotation(AnnotationNode *p_annotation, Nod
}

member->access_restriction = Node::ACCESS_RESTRICTION_PRIVATE;
member->accessible_class_name = current_class->identifier->name;
return true;
}

Expand All @@ -4234,6 +4235,7 @@ bool GDScriptParser::access_protected_annotation(AnnotationNode *p_annotation, N
}

member->access_restriction = Node::ACCESS_RESTRICTION_PROTECTED;
member->accessible_class_name = current_class->identifier->name;
return true;
}

Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ class GDScriptParser {
ACCESS_RESTRICTION_PROTECTED,
};
AccessRestriction access_restriction = ACCESS_RESTRICTION_PUBLIC;
StringName accessible_class_name = "";

int start_line = 0, end_line = 0;
int start_column = 0, end_column = 0;
Expand Down

0 comments on commit 8c028ad

Please sign in to comment.