From 84768b8f0ce05e2ca0e2649510bfdf71002dd7d5 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 2 Oct 2023 19:22:04 +0200 Subject: [PATCH] GDScript LSP compile fix pt2. --- .../gdscript_extend_parser.cpp | 55 +++++++++++++------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp index 1fea04b80..06c66ba16 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.cpp +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -83,7 +83,11 @@ void ExtendGDScriptParser::update_symbols() { members.clear(); const GDScriptParser::Node *head = get_parse_tree(); - if (const GDScriptParser::ClassNode *gdclass = dynamic_cast(head)) { + + +if (head && head->type == Node::TYPE_CLASS) { + const GDScriptParser::ClassNode *gdclass = static_cast(head); + parse_class_symbol(gdclass, class_symbol); for (int i = 0; i < class_symbol.children.size(); i++) { @@ -220,8 +224,11 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p for (RBMap::Element *E = p_class->constant_expressions.front(); E; E = E->next()) { lsp::DocumentSymbol symbol; const GDScriptParser::ClassNode::Constant &c = E->value(); - const GDScriptParser::ConstantNode *node = dynamic_cast(c.expression); - ERR_FAIL_COND(!node); + + ERR_FAIL_COND(c.expression->type != Node::TYPE_CONSTANT); + + const GDScriptParser::ConstantNode *node = static_cast(c.expression); + symbol.name = E->key(); symbol.kind = lsp::SymbolKind::Constant; symbol.deprecated = false; @@ -322,12 +329,17 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN } int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size()); if (default_value_idx >= 0) { - const GDScriptParser::ConstantNode *const_node = dynamic_cast(p_func->default_values[default_value_idx]); - if (const_node == nullptr) { - const GDScriptParser::OperatorNode *operator_node = dynamic_cast(p_func->default_values[default_value_idx]); - if (operator_node) { - const_node = dynamic_cast(operator_node->next); + const GDScriptParser::Node *current_defval_node = p_func->default_values[default_value_idx]; + const GDScriptParser::ConstantNode *const_node = NULL; + + if (current_defval_node->type == Node::TYPE_OPERATOR) { + const GDScriptParser::OperatorNode *operator_node = static_cast(current_defval_node); + + if (operator_node->next->type == Node::TYPE_CONSTANT) { + const_node = static_cast(operator_node->next); } + } else if (current_defval_node->type == Node::TYPE_CONSTANT) { + const_node = static_cast(current_defval_node); } if (const_node) { @@ -657,18 +669,26 @@ Dictionary ExtendGDScriptParser::dump_function_api(const GDScriptParser::Functio arg["name"] = p_func->arguments[i]; arg["type"] = p_func->argument_types[i].to_string(); int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size()); + if (default_value_idx >= 0) { - const GDScriptParser::ConstantNode *const_node = dynamic_cast(p_func->default_values[default_value_idx]); - if (const_node == nullptr) { - const GDScriptParser::OperatorNode *operator_node = dynamic_cast(p_func->default_values[default_value_idx]); - if (operator_node) { - const_node = dynamic_cast(operator_node->next); + const GDScriptParser::Node *current_defval_node = p_func->default_values[default_value_idx]; + const GDScriptParser::ConstantNode *const_node = NULL; + + if (current_defval_node->type == Node::TYPE_OPERATOR) { + const GDScriptParser::OperatorNode *operator_node = static_cast(current_defval_node); + + if (operator_node->next->type == Node::TYPE_CONSTANT) { + const_node = static_cast(operator_node->next); } + } else if (current_defval_node->type == Node::TYPE_CONSTANT) { + const_node = static_cast(current_defval_node); } + if (const_node) { arg["default_value"] = const_node->value; } } + arguments.push_back(arg); } if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(p_func->line))) { @@ -708,8 +728,10 @@ Dictionary ExtendGDScriptParser::dump_class_api(const GDScriptParser::ClassNode Array constants; for (RBMap::Element *E = p_class->constant_expressions.front(); E; E = E->next()) { const GDScriptParser::ClassNode::Constant &c = E->value(); - const GDScriptParser::ConstantNode *node = dynamic_cast(c.expression); - ERR_FAIL_COND_V(!node, class_api); + + ERR_FAIL_COND_V(c.expression->type != Node::TYPE_CONSTANT, class_api); + + const GDScriptParser::ConstantNode *node = static_cast(c.expression); Dictionary api; api["name"] = E->key(); @@ -777,7 +799,8 @@ Dictionary ExtendGDScriptParser::dump_class_api(const GDScriptParser::ClassNode Dictionary ExtendGDScriptParser::generate_api() const { Dictionary api; const GDScriptParser::Node *head = get_parse_tree(); - if (const GDScriptParser::ClassNode *gdclass = dynamic_cast(head)) { + if (head && head->type == Node::TYPE_CLASS) { + const GDScriptParser::ClassNode *gdclass = static_cast(head); api = dump_class_api(gdclass); } return api;