Merge pull request #94888 from rune-scape/no-const-cast-gdscript-parser

GDScriptParser: Avoid `const_cast` of `AnnotationNode`
This commit is contained in:
Rémi Verschelde 2024-08-28 00:12:35 +02:00
commit 6eb1d3cf98
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 30 additions and 32 deletions

View file

@ -4092,7 +4092,7 @@ bool GDScriptParser::validate_annotation_arguments(AnnotationNode *p_annotation)
return true; return true;
} }
bool GDScriptParser::tool_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { bool GDScriptParser::tool_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
if (_is_tool) { if (_is_tool) {
push_error(R"("@tool" annotation can only be used once.)", p_annotation); push_error(R"("@tool" annotation can only be used once.)", p_annotation);
@ -4103,7 +4103,7 @@ bool GDScriptParser::tool_annotation(const AnnotationNode *p_annotation, Node *p
return true; return true;
} }
bool GDScriptParser::icon_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { bool GDScriptParser::icon_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
ERR_FAIL_COND_V_MSG(p_target->type != Node::CLASS, false, R"("@icon" annotation can only be applied to classes.)"); ERR_FAIL_COND_V_MSG(p_target->type != Node::CLASS, false, R"("@icon" annotation can only be applied to classes.)");
ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false); ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false);
@ -4134,7 +4134,7 @@ bool GDScriptParser::icon_annotation(const AnnotationNode *p_annotation, Node *p
return true; return true;
} }
bool GDScriptParser::onready_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { bool GDScriptParser::onready_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, R"("@onready" annotation can only be applied to class variables.)"); ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, R"("@onready" annotation can only be applied to class variables.)");
if (current_class && !ClassDB::is_parent_class(current_class->get_datatype().native_type, SNAME("Node"))) { if (current_class && !ClassDB::is_parent_class(current_class->get_datatype().native_type, SNAME("Node"))) {
@ -4267,7 +4267,7 @@ static StringName _find_narrowest_native_or_global_class(const GDScriptParser::D
} }
template <PropertyHint t_hint, Variant::Type t_type> template <PropertyHint t_hint, Variant::Type t_type>
bool GDScriptParser::export_annotations(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { bool GDScriptParser::export_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name)); ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));
ERR_FAIL_NULL_V(p_class, false); ERR_FAIL_NULL_V(p_class, false);
@ -4506,7 +4506,7 @@ bool GDScriptParser::export_annotations(const AnnotationNode *p_annotation, Node
// For `@export_storage` and `@export_custom`, there is no need to check the variable type, argument values, // For `@export_storage` and `@export_custom`, there is no need to check the variable type, argument values,
// or handle array exports in a special way, so they are implemented as separate methods. // or handle array exports in a special way, so they are implemented as separate methods.
bool GDScriptParser::export_storage_annotation(const AnnotationNode *p_annotation, Node *p_node, ClassNode *p_class) { bool GDScriptParser::export_storage_annotation(AnnotationNode *p_annotation, Node *p_node, ClassNode *p_class) {
ERR_FAIL_COND_V_MSG(p_node->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name)); ERR_FAIL_COND_V_MSG(p_node->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));
VariableNode *variable = static_cast<VariableNode *>(p_node); VariableNode *variable = static_cast<VariableNode *>(p_node);
@ -4528,7 +4528,7 @@ bool GDScriptParser::export_storage_annotation(const AnnotationNode *p_annotatio
return true; return true;
} }
bool GDScriptParser::export_custom_annotation(const AnnotationNode *p_annotation, Node *p_node, ClassNode *p_class) { bool GDScriptParser::export_custom_annotation(AnnotationNode *p_annotation, Node *p_node, ClassNode *p_class) {
ERR_FAIL_COND_V_MSG(p_node->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name)); ERR_FAIL_COND_V_MSG(p_node->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));
ERR_FAIL_COND_V_MSG(p_annotation->resolved_arguments.size() < 2, false, R"(Annotation "@export_custom" requires 2 arguments.)"); ERR_FAIL_COND_V_MSG(p_annotation->resolved_arguments.size() < 2, false, R"(Annotation "@export_custom" requires 2 arguments.)");
@ -4557,31 +4557,29 @@ bool GDScriptParser::export_custom_annotation(const AnnotationNode *p_annotation
} }
template <PropertyUsageFlags t_usage> template <PropertyUsageFlags t_usage>
bool GDScriptParser::export_group_annotations(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { bool GDScriptParser::export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
AnnotationNode *annotation = const_cast<AnnotationNode *>(p_annotation); if (p_annotation->resolved_arguments.is_empty()) {
if (annotation->resolved_arguments.is_empty()) {
return false; return false;
} }
annotation->export_info.name = annotation->resolved_arguments[0]; p_annotation->export_info.name = p_annotation->resolved_arguments[0];
switch (t_usage) { switch (t_usage) {
case PROPERTY_USAGE_CATEGORY: { case PROPERTY_USAGE_CATEGORY: {
annotation->export_info.usage = t_usage; p_annotation->export_info.usage = t_usage;
} break; } break;
case PROPERTY_USAGE_GROUP: { case PROPERTY_USAGE_GROUP: {
annotation->export_info.usage = t_usage; p_annotation->export_info.usage = t_usage;
if (annotation->resolved_arguments.size() == 2) { if (p_annotation->resolved_arguments.size() == 2) {
annotation->export_info.hint_string = annotation->resolved_arguments[1]; p_annotation->export_info.hint_string = p_annotation->resolved_arguments[1];
} }
} break; } break;
case PROPERTY_USAGE_SUBGROUP: { case PROPERTY_USAGE_SUBGROUP: {
annotation->export_info.usage = t_usage; p_annotation->export_info.usage = t_usage;
if (annotation->resolved_arguments.size() == 2) { if (p_annotation->resolved_arguments.size() == 2) {
annotation->export_info.hint_string = annotation->resolved_arguments[1]; p_annotation->export_info.hint_string = p_annotation->resolved_arguments[1];
} }
} break; } break;
} }
@ -4589,7 +4587,7 @@ bool GDScriptParser::export_group_annotations(const AnnotationNode *p_annotation
return true; return true;
} }
bool GDScriptParser::warning_annotations(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { bool GDScriptParser::warning_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
#ifndef DEBUG_ENABLED #ifndef DEBUG_ENABLED
// Only available in debug builds. // Only available in debug builds.
return true; return true;
@ -4664,7 +4662,7 @@ bool GDScriptParser::warning_annotations(const AnnotationNode *p_annotation, Nod
#endif // DEBUG_ENABLED #endif // DEBUG_ENABLED
} }
bool GDScriptParser::rpc_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { bool GDScriptParser::rpc_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
ERR_FAIL_COND_V_MSG(p_target->type != Node::FUNCTION, false, vformat(R"("%s" annotation can only be applied to functions.)", p_annotation->name)); ERR_FAIL_COND_V_MSG(p_target->type != Node::FUNCTION, false, vformat(R"("%s" annotation can only be applied to functions.)", p_annotation->name));
FunctionNode *function = static_cast<FunctionNode *>(p_target); FunctionNode *function = static_cast<FunctionNode *>(p_target);
@ -4725,7 +4723,7 @@ bool GDScriptParser::rpc_annotation(const AnnotationNode *p_annotation, Node *p_
return true; return true;
} }
bool GDScriptParser::static_unload_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { bool GDScriptParser::static_unload_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
ERR_FAIL_COND_V_MSG(p_target->type != Node::CLASS, false, vformat(R"("%s" annotation can only be applied to classes.)", p_annotation->name)); ERR_FAIL_COND_V_MSG(p_target->type != Node::CLASS, false, vformat(R"("%s" annotation can only be applied to classes.)", p_annotation->name));
ClassNode *class_node = static_cast<ClassNode *>(p_target); ClassNode *class_node = static_cast<ClassNode *>(p_target);
if (class_node->annotated_static_unload) { if (class_node->annotated_static_unload) {

View file

@ -1371,7 +1371,7 @@ private:
bool in_lambda = false; bool in_lambda = false;
bool lambda_ended = false; // Marker for when a lambda ends, to apply an end of statement if needed. bool lambda_ended = false; // Marker for when a lambda ends, to apply an end of statement if needed.
typedef bool (GDScriptParser::*AnnotationAction)(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class); typedef bool (GDScriptParser::*AnnotationAction)(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
struct AnnotationInfo { struct AnnotationInfo {
enum TargetKind { enum TargetKind {
NONE = 0, NONE = 0,
@ -1495,18 +1495,18 @@ private:
static bool register_annotation(const MethodInfo &p_info, uint32_t p_target_kinds, AnnotationAction p_apply, const Vector<Variant> &p_default_arguments = Vector<Variant>(), bool p_is_vararg = false); static bool register_annotation(const MethodInfo &p_info, uint32_t p_target_kinds, AnnotationAction p_apply, const Vector<Variant> &p_default_arguments = Vector<Variant>(), bool p_is_vararg = false);
bool validate_annotation_arguments(AnnotationNode *p_annotation); bool validate_annotation_arguments(AnnotationNode *p_annotation);
void clear_unused_annotations(); void clear_unused_annotations();
bool tool_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class); bool tool_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
bool icon_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class); bool icon_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
bool onready_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class); bool onready_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
template <PropertyHint t_hint, Variant::Type t_type> template <PropertyHint t_hint, Variant::Type t_type>
bool export_annotations(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class); bool export_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
bool export_storage_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class); bool export_storage_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
bool export_custom_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class); bool export_custom_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
template <PropertyUsageFlags t_usage> template <PropertyUsageFlags t_usage>
bool export_group_annotations(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class); bool export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
bool warning_annotations(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class); bool warning_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
bool rpc_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class); bool rpc_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
bool static_unload_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class); bool static_unload_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
// Statements. // Statements.
Node *parse_statement(); Node *parse_statement();
VariableNode *parse_variable(bool p_is_static); VariableNode *parse_variable(bool p_is_static);