diff --git a/core/script_language.h b/core/script_language.h index 314b047027..6ba38399a1 100644 --- a/core/script_language.h +++ b/core/script_language.h @@ -294,7 +294,8 @@ public: /* EDITOR FUNCTIONS */ struct Warning { - int line; + int start_line = -1, end_line = -1; + int leftmost_column = -1, rightmost_column = -1; int code; String string_code; String message; diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index d8648310b6..9a87d6d38c 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1778,6 +1778,7 @@ CodeTextEditor::CodeTextEditor() { cs.push_back("("); cs.push_back("="); cs.push_back("$"); + cs.push_back("@"); text_editor->set_completion(true, cs); idle->connect("timeout", callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout)); diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 1e03d9dfab..f4fdf8ccb0 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -494,7 +494,7 @@ void ScriptTextEditor::_validate_script() { ScriptLanguage::Warning w = E->get(); Dictionary ignore_meta; - ignore_meta["line"] = w.line; + ignore_meta["line"] = w.start_line; ignore_meta["code"] = w.string_code.to_lower(); warnings_panel->push_cell(); warnings_panel->push_meta(ignore_meta); @@ -506,9 +506,9 @@ void ScriptTextEditor::_validate_script() { warnings_panel->pop(); // Cell. warnings_panel->push_cell(); - warnings_panel->push_meta(w.line - 1); + warnings_panel->push_meta(w.start_line - 1); warnings_panel->push_color(warnings_panel->get_theme_color("warning_color", "Editor")); - warnings_panel->add_text(TTR("Line") + " " + itos(w.line)); + warnings_panel->add_text(TTR("Line") + " " + itos(w.start_line)); warnings_panel->add_text(" (" + w.string_code + "):"); warnings_panel->pop(); // Color. warnings_panel->pop(); // Meta goto. diff --git a/main/tests/test_gdscript.cpp b/main/tests/test_gdscript.cpp index 10586c6495..a50311972f 100644 --- a/main/tests/test_gdscript.cpp +++ b/main/tests/test_gdscript.cpp @@ -33,854 +33,93 @@ #include "core/os/file_access.h" #include "core/os/main_loop.h" #include "core/os/os.h" +#include "core/string_builder.h" #include "modules/modules_enabled.gen.h" #ifdef MODULE_GDSCRIPT_ENABLED -#include "modules/gdscript/gdscript.h" -#include "modules/gdscript/gdscript_compiler.h" #include "modules/gdscript/gdscript_parser.h" #include "modules/gdscript/gdscript_tokenizer.h" +#ifdef TOOLS_ENABLED +#include "editor/editor_settings.h" +#endif + namespace TestGDScript { -static void _print_indent(int p_ident, const String &p_text) { - String txt; - for (int i = 0; i < p_ident; i++) { - txt += '\t'; +static void test_tokenizer(const String &p_code, const Vector &p_lines) { + GDScriptTokenizer tokenizer; + tokenizer.set_source_code(p_code); + + int tab_size = 4; +#ifdef TOOLS_ENABLED + if (EditorSettings::get_singleton()) { + tab_size = EditorSettings::get_singleton()->get_setting("text_editor/indent/size"); } +#endif // TOOLS_ENABLED + String tab = String(" ").repeat(tab_size); - print_line(txt + p_text); -} + GDScriptTokenizer::Token current = tokenizer.scan(); + while (current.type != GDScriptTokenizer::Token::TK_EOF) { + StringBuilder token; + token += " --> "; // Padding for line number. -static String _parser_extends(const GDScriptParser::ClassNode *p_class) { - String txt = "extends "; - if (String(p_class->extends_file) != "") { - txt += "\"" + p_class->extends_file + "\""; - if (p_class->extends_class.size()) { - txt += "."; - } - } - - for (int i = 0; i < p_class->extends_class.size(); i++) { - if (i != 0) { - txt += "."; + for (int l = current.start_line; l <= current.end_line; l++) { + print_line(vformat("%04d %s", l, p_lines[l - 1]).replace("\t", tab)); } - txt += p_class->extends_class[i]; - } - - return txt; -} - -static String _parser_expr(const GDScriptParser::Node *p_expr) { - String txt; - switch (p_expr->type) { - case GDScriptParser::Node::TYPE_IDENTIFIER: { - const GDScriptParser::IdentifierNode *id_node = static_cast(p_expr); - txt = id_node->name; - } break; - case GDScriptParser::Node::TYPE_CONSTANT: { - const GDScriptParser::ConstantNode *c_node = static_cast(p_expr); - if (c_node->value.get_type() == Variant::STRING) { - txt = "\"" + String(c_node->value) + "\""; - } else { - txt = c_node->value; + { + // Print carets to point at the token. + StringBuilder pointer; + pointer += " "; // Padding for line number. + int rightmost_column = current.rightmost_column; + if (current.end_line > current.start_line) { + rightmost_column--; // Don't point to the newline as a column. } - - } break; - case GDScriptParser::Node::TYPE_SELF: { - txt = "self"; - } break; - case GDScriptParser::Node::TYPE_ARRAY: { - const GDScriptParser::ArrayNode *arr_node = static_cast(p_expr); - txt += "["; - for (int i = 0; i < arr_node->elements.size(); i++) { - if (i > 0) { - txt += ", "; - } - txt += _parser_expr(arr_node->elements[i]); - } - txt += "]"; - } break; - case GDScriptParser::Node::TYPE_DICTIONARY: { - const GDScriptParser::DictionaryNode *dict_node = static_cast(p_expr); - txt += "{"; - for (int i = 0; i < dict_node->elements.size(); i++) { - if (i > 0) { - txt += ", "; - } - - const GDScriptParser::DictionaryNode::Pair &p = dict_node->elements[i]; - txt += _parser_expr(p.key); - txt += ":"; - txt += _parser_expr(p.value); - } - txt += "}"; - } break; - case GDScriptParser::Node::TYPE_OPERATOR: { - const GDScriptParser::OperatorNode *c_node = static_cast(p_expr); - switch (c_node->op) { - case GDScriptParser::OperatorNode::OP_PARENT_CALL: - txt += "."; - [[fallthrough]]; - case GDScriptParser::OperatorNode::OP_CALL: { - ERR_FAIL_COND_V(c_node->arguments.size() < 1, ""); - String func_name; - const GDScriptParser::Node *nfunc = c_node->arguments[0]; - int arg_ofs = 0; - if (nfunc->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) { - const GDScriptParser::BuiltInFunctionNode *bif_node = static_cast(nfunc); - func_name = GDScriptFunctions::get_func_name(bif_node->function); - arg_ofs = 1; - } else if (nfunc->type == GDScriptParser::Node::TYPE_TYPE) { - const GDScriptParser::TypeNode *t_node = static_cast(nfunc); - func_name = Variant::get_type_name(t_node->vtype); - arg_ofs = 1; - } else { - ERR_FAIL_COND_V(c_node->arguments.size() < 2, ""); - nfunc = c_node->arguments[1]; - ERR_FAIL_COND_V(nfunc->type != GDScriptParser::Node::TYPE_IDENTIFIER, ""); - - if (c_node->arguments[0]->type != GDScriptParser::Node::TYPE_SELF) { - func_name = _parser_expr(c_node->arguments[0]) + "."; - } - - func_name += _parser_expr(nfunc); - arg_ofs = 2; - } - - txt += func_name + "("; - - for (int i = arg_ofs; i < c_node->arguments.size(); i++) { - const GDScriptParser::Node *arg = c_node->arguments[i]; - if (i > arg_ofs) { - txt += ", "; - } - txt += _parser_expr(arg); - } - - txt += ")"; - - } break; - case GDScriptParser::OperatorNode::OP_INDEX: { - ERR_FAIL_COND_V(c_node->arguments.size() != 2, ""); - - //index with [] - txt = _parser_expr(c_node->arguments[0]) + "[" + _parser_expr(c_node->arguments[1]) + "]"; - - } break; - case GDScriptParser::OperatorNode::OP_INDEX_NAMED: { - ERR_FAIL_COND_V(c_node->arguments.size() != 2, ""); - - txt = _parser_expr(c_node->arguments[0]) + "." + _parser_expr(c_node->arguments[1]); - - } break; - case GDScriptParser::OperatorNode::OP_NEG: { - txt = "-" + _parser_expr(c_node->arguments[0]); - } break; - case GDScriptParser::OperatorNode::OP_NOT: { - txt = "not " + _parser_expr(c_node->arguments[0]); - } break; - case GDScriptParser::OperatorNode::OP_BIT_INVERT: { - txt = "~" + _parser_expr(c_node->arguments[0]); - } break; - case GDScriptParser::OperatorNode::OP_IN: { - txt = _parser_expr(c_node->arguments[0]) + " in " + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_EQUAL: { - txt = _parser_expr(c_node->arguments[0]) + "==" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_NOT_EQUAL: { - txt = _parser_expr(c_node->arguments[0]) + "!=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_LESS: { - txt = _parser_expr(c_node->arguments[0]) + "<" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_LESS_EQUAL: { - txt = _parser_expr(c_node->arguments[0]) + "<=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_GREATER: { - txt = _parser_expr(c_node->arguments[0]) + ">" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_GREATER_EQUAL: { - txt = _parser_expr(c_node->arguments[0]) + ">=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_AND: { - txt = _parser_expr(c_node->arguments[0]) + " and " + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_OR: { - txt = _parser_expr(c_node->arguments[0]) + " or " + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ADD: { - txt = _parser_expr(c_node->arguments[0]) + "+" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_SUB: { - txt = _parser_expr(c_node->arguments[0]) + "-" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_MUL: { - txt = _parser_expr(c_node->arguments[0]) + "*" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_DIV: { - txt = _parser_expr(c_node->arguments[0]) + "/" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_MOD: { - txt = _parser_expr(c_node->arguments[0]) + "%" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: { - txt = _parser_expr(c_node->arguments[0]) + "<<" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: { - txt = _parser_expr(c_node->arguments[0]) + ">>" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN: { - txt = _parser_expr(c_node->arguments[0]) + "=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_ADD: { - txt = _parser_expr(c_node->arguments[0]) + "+=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_SUB: { - txt = _parser_expr(c_node->arguments[0]) + "-=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_MUL: { - txt = _parser_expr(c_node->arguments[0]) + "*=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_DIV: { - txt = _parser_expr(c_node->arguments[0]) + "/=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_MOD: { - txt = _parser_expr(c_node->arguments[0]) + "%=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT: { - txt = _parser_expr(c_node->arguments[0]) + "<<=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: { - txt = _parser_expr(c_node->arguments[0]) + ">>=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_AND: { - txt = _parser_expr(c_node->arguments[0]) + "&=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_OR: { - txt = _parser_expr(c_node->arguments[0]) + "|=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR: { - txt = _parser_expr(c_node->arguments[0]) + "^=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_BIT_AND: { - txt = _parser_expr(c_node->arguments[0]) + "&" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_BIT_OR: { - txt = _parser_expr(c_node->arguments[0]) + "|" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_BIT_XOR: { - txt = _parser_expr(c_node->arguments[0]) + "^" + _parser_expr(c_node->arguments[1]); - } break; - default: { + for (int col = 1; col < rightmost_column; col++) { + if (col < current.leftmost_column) { + pointer += " "; + } else { + pointer += "^"; } } - - } break; - case GDScriptParser::Node::TYPE_CAST: { - const GDScriptParser::CastNode *cast_node = static_cast(p_expr); - txt = _parser_expr(cast_node->source_node) + " as " + cast_node->cast_type.to_string(); - - } break; - case GDScriptParser::Node::TYPE_NEWLINE: { - //skippie - } break; - default: { - ERR_FAIL_V_MSG("", "Parser bug at " + itos(p_expr->line) + ", invalid expression type: " + itos(p_expr->type)); + print_line(pointer.as_string()); } + + token += current.get_name(); + + if (current.type == GDScriptTokenizer::Token::ERROR || current.type == GDScriptTokenizer::Token::LITERAL || current.type == GDScriptTokenizer::Token::IDENTIFIER || current.type == GDScriptTokenizer::Token::ANNOTATION) { + token += "("; + token += Variant::get_type_name(current.literal.get_type()); + token += ") "; + token += current.literal; + } + + print_line(token.as_string()); + + print_line("-------------------------------------------------------"); + + current = tokenizer.scan(); } - return txt; + print_line(current.get_name()); // Should be EOF } -static void _parser_show_block(const GDScriptParser::BlockNode *p_block, int p_indent) { - for (int i = 0; i < p_block->statements.size(); i++) { - const GDScriptParser::Node *statement = p_block->statements[i]; +static void test_parser(const String &p_code, const String &p_script_path, const Vector &p_lines) { + GDScriptParser parser; + Error err = parser.parse(p_code, p_script_path, false); - switch (statement->type) { - case GDScriptParser::Node::TYPE_CONTROL_FLOW: { - const GDScriptParser::ControlFlowNode *cf_node = static_cast(statement); - switch (cf_node->cf_type) { - case GDScriptParser::ControlFlowNode::CF_IF: { - ERR_FAIL_COND(cf_node->arguments.size() != 1); - String txt; - txt += "if "; - txt += _parser_expr(cf_node->arguments[0]); - txt += ":"; - _print_indent(p_indent, txt); - ERR_FAIL_COND(!cf_node->body); - _parser_show_block(cf_node->body, p_indent + 1); - if (cf_node->body_else) { - _print_indent(p_indent, "else:"); - _parser_show_block(cf_node->body_else, p_indent + 1); - } - - } break; - case GDScriptParser::ControlFlowNode::CF_FOR: { - ERR_FAIL_COND(cf_node->arguments.size() != 2); - String txt; - txt += "for "; - txt += _parser_expr(cf_node->arguments[0]); - txt += " in "; - txt += _parser_expr(cf_node->arguments[1]); - txt += ":"; - _print_indent(p_indent, txt); - ERR_FAIL_COND(!cf_node->body); - _parser_show_block(cf_node->body, p_indent + 1); - - } break; - case GDScriptParser::ControlFlowNode::CF_WHILE: { - ERR_FAIL_COND(cf_node->arguments.size() != 1); - String txt; - txt += "while "; - txt += _parser_expr(cf_node->arguments[0]); - txt += ":"; - _print_indent(p_indent, txt); - ERR_FAIL_COND(!cf_node->body); - _parser_show_block(cf_node->body, p_indent + 1); - - } break; - case GDScriptParser::ControlFlowNode::CF_MATCH: { - // FIXME: Implement - } break; - case GDScriptParser::ControlFlowNode::CF_CONTINUE: { - _print_indent(p_indent, "continue"); - } break; - case GDScriptParser::ControlFlowNode::CF_BREAK: { - _print_indent(p_indent, "break"); - } break; - case GDScriptParser::ControlFlowNode::CF_RETURN: { - if (cf_node->arguments.size()) { - _print_indent(p_indent, "return " + _parser_expr(cf_node->arguments[0])); - } else { - _print_indent(p_indent, "return "); - } - } break; - } - - } break; - case GDScriptParser::Node::TYPE_LOCAL_VAR: { - const GDScriptParser::LocalVarNode *lv_node = static_cast(statement); - _print_indent(p_indent, "var " + String(lv_node->name)); - } break; - default: { - //expression i guess - _print_indent(p_indent, _parser_expr(statement)); - } - } - } -} - -static void _parser_show_function(const GDScriptParser::FunctionNode *p_func, int p_indent, GDScriptParser::BlockNode *p_initializer = nullptr) { - String txt; - if (p_func->_static) { - txt = "static "; - } - txt += "func "; - if (p_func->name == "") { // initializer - txt += "[built-in-initializer]"; - } else { - txt += String(p_func->name); - } - txt += "("; - - for (int i = 0; i < p_func->arguments.size(); i++) { - if (i != 0) { - txt += ", "; - } - txt += "var " + String(p_func->arguments[i]); - if (i >= (p_func->arguments.size() - p_func->default_values.size())) { - int defarg = i - (p_func->arguments.size() - p_func->default_values.size()); - txt += "="; - txt += _parser_expr(p_func->default_values[defarg]); + if (err != OK) { + const List &errors = parser.get_errors(); + for (const List::Element *E = errors.front(); E != nullptr; E = E->next()) { + const GDScriptParser::ParserError &error = E->get(); + print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message)); } } - txt += ")"; + GDScriptParser::TreePrinter printer; - //todo constructor check! - - txt += ":"; - - _print_indent(p_indent, txt); - if (p_initializer) { - _parser_show_block(p_initializer, p_indent + 1); - } - _parser_show_block(p_func->body, p_indent + 1); -} - -static void _parser_show_class(const GDScriptParser::ClassNode *p_class, int p_indent, const Vector &p_code) { - if (p_indent == 0 && (String(p_class->extends_file) != "" || p_class->extends_class.size())) { - _print_indent(p_indent, _parser_extends(p_class)); - print_line("\n"); - } - - for (int i = 0; i < p_class->subclasses.size(); i++) { - const GDScriptParser::ClassNode *subclass = p_class->subclasses[i]; - String line = "class " + subclass->name; - if (String(subclass->extends_file) != "" || subclass->extends_class.size()) { - line += " " + _parser_extends(subclass); - } - line += ":"; - _print_indent(p_indent, line); - _parser_show_class(subclass, p_indent + 1, p_code); - print_line("\n"); - } - - for (Map::Element *E = p_class->constant_expressions.front(); E; E = E->next()) { - const GDScriptParser::ClassNode::Constant &constant = E->get(); - _print_indent(p_indent, "const " + String(E->key()) + "=" + _parser_expr(constant.expression)); - } - - for (int i = 0; i < p_class->variables.size(); i++) { - const GDScriptParser::ClassNode::Member &m = p_class->variables[i]; - - _print_indent(p_indent, "var " + String(m.identifier)); - } - - print_line("\n"); - - for (int i = 0; i < p_class->static_functions.size(); i++) { - _parser_show_function(p_class->static_functions[i], p_indent); - print_line("\n"); - } - - for (int i = 0; i < p_class->functions.size(); i++) { - if (String(p_class->functions[i]->name) == "_init") { - _parser_show_function(p_class->functions[i], p_indent, p_class->initializer); - } else { - _parser_show_function(p_class->functions[i], p_indent); - } - print_line("\n"); - } - //_parser_show_function(p_class->initializer,p_indent); - print_line("\n"); -} - -static String _disassemble_addr(const Ref &p_script, const GDScriptFunction &func, int p_addr) { - int addr = p_addr & GDScriptFunction::ADDR_MASK; - - switch (p_addr >> GDScriptFunction::ADDR_BITS) { - case GDScriptFunction::ADDR_TYPE_SELF: { - return "self"; - } break; - case GDScriptFunction::ADDR_TYPE_CLASS: { - return "class"; - } break; - case GDScriptFunction::ADDR_TYPE_MEMBER: { - return "member(" + p_script->debug_get_member_by_index(addr) + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT: { - return "class_const(" + func.get_global_name(addr) + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT: { - Variant v = func.get_constant(addr); - String txt; - if (v.get_type() == Variant::STRING || v.get_type() == Variant::NODE_PATH) { - txt = "\"" + String(v) + "\""; - } else { - txt = v; - } - return "const(" + txt + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_STACK: { - return "stack(" + itos(addr) + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_STACK_VARIABLE: { - return "var_stack(" + itos(addr) + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_GLOBAL: { - return "global(" + func.get_global_name(addr) + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_NIL: { - return "nil"; - } break; - } - - return ""; -} - -static void _disassemble_class(const Ref &p_class, const Vector &p_code) { - const Map &mf = p_class->debug_get_member_functions(); - - for (const Map::Element *E = mf.front(); E; E = E->next()) { - const GDScriptFunction &func = *E->get(); - const int *code = func.get_code(); - int codelen = func.get_code_size(); - String defargs; - if (func.get_default_argument_count()) { - defargs = "defarg at: "; - for (int i = 0; i < func.get_default_argument_count(); i++) { - if (i > 0) { - defargs += ","; - } - defargs += itos(func.get_default_argument_addr(i)); - } - defargs += " "; - } - print_line("== function " + String(func.get_name()) + "() :: stack size: " + itos(func.get_max_stack_size()) + " " + defargs + "=="); - -#define DADDR(m_ip) (_disassemble_addr(p_class, func, code[ip + m_ip])) - - for (int ip = 0; ip < codelen;) { - int incr = 0; - String txt = itos(ip) + " "; - - switch (code[ip]) { - case GDScriptFunction::OPCODE_OPERATOR: { - int op = code[ip + 1]; - txt += " op "; - - String opname = Variant::get_operator_name(Variant::Operator(op)); - - txt += DADDR(4); - txt += " = "; - txt += DADDR(2); - txt += " " + opname + " "; - txt += DADDR(3); - incr += 5; - - } break; - case GDScriptFunction::OPCODE_SET: { - txt += "set "; - txt += DADDR(1); - txt += "["; - txt += DADDR(2); - txt += "]="; - txt += DADDR(3); - incr += 4; - - } break; - case GDScriptFunction::OPCODE_GET: { - txt += " get "; - txt += DADDR(3); - txt += "="; - txt += DADDR(1); - txt += "["; - txt += DADDR(2); - txt += "]"; - incr += 4; - - } break; - case GDScriptFunction::OPCODE_SET_NAMED: { - txt += " set_named "; - txt += DADDR(1); - txt += "[\""; - txt += func.get_global_name(code[ip + 2]); - txt += "\"]="; - txt += DADDR(3); - incr += 4; - - } break; - case GDScriptFunction::OPCODE_GET_NAMED: { - txt += " get_named "; - txt += DADDR(3); - txt += "="; - txt += DADDR(1); - txt += "[\""; - txt += func.get_global_name(code[ip + 2]); - txt += "\"]"; - incr += 4; - - } break; - case GDScriptFunction::OPCODE_SET_MEMBER: { - txt += " set_member "; - txt += "[\""; - txt += func.get_global_name(code[ip + 1]); - txt += "\"]="; - txt += DADDR(2); - incr += 3; - - } break; - case GDScriptFunction::OPCODE_GET_MEMBER: { - txt += " get_member "; - txt += DADDR(2); - txt += "="; - txt += "[\""; - txt += func.get_global_name(code[ip + 1]); - txt += "\"]"; - incr += 3; - - } break; - case GDScriptFunction::OPCODE_ASSIGN: { - txt += " assign "; - txt += DADDR(1); - txt += "="; - txt += DADDR(2); - incr += 3; - - } break; - case GDScriptFunction::OPCODE_ASSIGN_TRUE: { - txt += " assign "; - txt += DADDR(1); - txt += "= true"; - incr += 2; - - } break; - case GDScriptFunction::OPCODE_ASSIGN_FALSE: { - txt += " assign "; - txt += DADDR(1); - txt += "= false"; - incr += 2; - - } break; - case GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN: { - txt += " assign typed builtin ("; - txt += Variant::get_type_name((Variant::Type)code[ip + 1]); - txt += ") "; - txt += DADDR(2); - txt += " = "; - txt += DADDR(3); - incr += 4; - - } break; - case GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE: { - Variant className = func.get_constant(code[ip + 1]); - GDScriptNativeClass *nc = Object::cast_to(className.operator Object *()); - - txt += " assign typed native ("; - txt += nc->get_name().operator String(); - txt += ") "; - txt += DADDR(2); - txt += " = "; - txt += DADDR(3); - incr += 4; - - } break; - case GDScriptFunction::OPCODE_CAST_TO_SCRIPT: { - txt += " cast "; - txt += DADDR(3); - txt += "="; - txt += DADDR(1); - txt += " as "; - txt += DADDR(2); - incr += 4; - - } break; - case GDScriptFunction::OPCODE_CONSTRUCT: { - Variant::Type t = Variant::Type(code[ip + 1]); - int argc = code[ip + 2]; - - txt += " construct "; - txt += DADDR(3 + argc); - txt += " = "; - - txt += Variant::get_type_name(t) + "("; - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(i + 3); - } - txt += ")"; - - incr = 4 + argc; - - } break; - case GDScriptFunction::OPCODE_CONSTRUCT_ARRAY: { - int argc = code[ip + 1]; - txt += " make_array "; - txt += DADDR(2 + argc); - txt += " = [ "; - - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(2 + i); - } - - txt += "]"; - - incr += 3 + argc; - - } break; - case GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY: { - int argc = code[ip + 1]; - txt += " make_dict "; - txt += DADDR(2 + argc * 2); - txt += " = { "; - - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(2 + i * 2 + 0); - txt += ":"; - txt += DADDR(2 + i * 2 + 1); - } - - txt += "}"; - - incr += 3 + argc * 2; - - } break; - - case GDScriptFunction::OPCODE_CALL: - case GDScriptFunction::OPCODE_CALL_RETURN: { - bool ret = code[ip] == GDScriptFunction::OPCODE_CALL_RETURN; - - if (ret) { - txt += " call-ret "; - } else { - txt += " call "; - } - - int argc = code[ip + 1]; - if (ret) { - txt += DADDR(4 + argc) + "="; - } - - txt += DADDR(2) + "."; - txt += String(func.get_global_name(code[ip + 3])); - txt += "("; - - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(4 + i); - } - txt += ")"; - - incr = 5 + argc; - - } break; - case GDScriptFunction::OPCODE_CALL_BUILT_IN: { - txt += " call-built-in "; - - int argc = code[ip + 2]; - txt += DADDR(3 + argc) + "="; - - txt += GDScriptFunctions::get_func_name(GDScriptFunctions::Function(code[ip + 1])); - txt += "("; - - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(3 + i); - } - txt += ")"; - - incr = 4 + argc; - - } break; - case GDScriptFunction::OPCODE_CALL_SELF_BASE: { - txt += " call-self-base "; - - int argc = code[ip + 2]; - txt += DADDR(3 + argc) + "="; - - txt += func.get_global_name(code[ip + 1]); - txt += "("; - - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(3 + i); - } - txt += ")"; - - incr = 4 + argc; - - } break; - case GDScriptFunction::OPCODE_YIELD: { - txt += " yield "; - incr = 1; - - } break; - case GDScriptFunction::OPCODE_YIELD_SIGNAL: { - txt += " yield_signal "; - txt += DADDR(1); - txt += ","; - txt += DADDR(2); - incr = 3; - } break; - case GDScriptFunction::OPCODE_YIELD_RESUME: { - txt += " yield resume: "; - txt += DADDR(1); - incr = 2; - } break; - case GDScriptFunction::OPCODE_JUMP: { - txt += " jump "; - txt += itos(code[ip + 1]); - - incr = 2; - - } break; - case GDScriptFunction::OPCODE_JUMP_IF: { - txt += " jump-if "; - txt += DADDR(1); - txt += " to "; - txt += itos(code[ip + 2]); - - incr = 3; - } break; - case GDScriptFunction::OPCODE_JUMP_IF_NOT: { - txt += " jump-if-not "; - txt += DADDR(1); - txt += " to "; - txt += itos(code[ip + 2]); - - incr = 3; - } break; - case GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT: { - txt += " jump-to-default-argument "; - incr = 1; - } break; - case GDScriptFunction::OPCODE_RETURN: { - txt += " return "; - txt += DADDR(1); - - incr = 2; - - } break; - case GDScriptFunction::OPCODE_ITERATE_BEGIN: { - txt += " for-init " + DADDR(4) + " in " + DADDR(2) + " counter " + DADDR(1) + " end " + itos(code[ip + 3]); - incr += 5; - - } break; - case GDScriptFunction::OPCODE_ITERATE: { - txt += " for-loop " + DADDR(4) + " in " + DADDR(2) + " counter " + DADDR(1) + " end " + itos(code[ip + 3]); - incr += 5; - - } break; - case GDScriptFunction::OPCODE_LINE: { - int line = code[ip + 1] - 1; - if (line >= 0 && line < p_code.size()) { - txt = "\n" + itos(line + 1) + ": " + p_code[line] + "\n"; - } else { - txt = ""; - } - incr += 2; - } break; - case GDScriptFunction::OPCODE_END: { - txt += " end"; - incr += 1; - } break; - case GDScriptFunction::OPCODE_ASSERT: { - txt += " assert "; - txt += DADDR(1); - incr += 2; - - } break; - } - - if (incr == 0) { - ERR_BREAK_MSG(true, "Unhandled opcode: " + itos(code[ip])); - } - - ip += incr; - if (txt != "") { - print_line(txt); - } - } - } + printer.print_tree(parser); } MainLoop *test(TestType p_type) { @@ -891,12 +130,12 @@ MainLoop *test(TestType p_type) { } String test = cmdlargs.back()->get(); - if (!test.ends_with(".gd") && !test.ends_with(".gdc")) { + if (!test.ends_with(".gd")) { print_line("This test expects a path to a GDScript file as its last parameter. Got: " + test); return nullptr; } - FileAccess *fa = FileAccess::open(test, FileAccess::READ); + FileAccessRef fa = FileAccess::open(test, FileAccess::READ); ERR_FAIL_COND_V_MSG(!fa, nullptr, "Could not open file: " + test); Vector buf; @@ -910,7 +149,6 @@ MainLoop *test(TestType p_type) { Vector lines; int last = 0; - for (int i = 0; i <= code.length(); i++) { if (code[i] == '\n' || code[i] == 0) { lines.push_back(code.substr(last, i - last)); @@ -918,104 +156,18 @@ MainLoop *test(TestType p_type) { } } - if (p_type == TEST_TOKENIZER) { - GDScriptTokenizerText tk; - tk.set_code(code); - int line = -1; - while (tk.get_token() != GDScriptTokenizer::TK_EOF) { - String text; - if (tk.get_token() == GDScriptTokenizer::TK_IDENTIFIER) { - text = "'" + tk.get_token_identifier() + "' (identifier)"; - } else if (tk.get_token() == GDScriptTokenizer::TK_CONSTANT) { - const Variant &c = tk.get_token_constant(); - if (c.get_type() == Variant::STRING) { - text = "\"" + String(c) + "\""; - } else { - text = c; - } - - text = text + " (" + Variant::get_type_name(c.get_type()) + " constant)"; - } else if (tk.get_token() == GDScriptTokenizer::TK_ERROR) { - text = "ERROR: " + tk.get_token_error(); - } else if (tk.get_token() == GDScriptTokenizer::TK_NEWLINE) { - text = "newline (" + itos(tk.get_token_line()) + ") + indent: " + itos(tk.get_token_line_indent()); - } else if (tk.get_token() == GDScriptTokenizer::TK_BUILT_IN_FUNC) { - text = "'" + String(GDScriptFunctions::get_func_name(tk.get_token_built_in_func())) + "' (built-in function)"; - } else { - text = tk.get_token_name(tk.get_token()); - } - - if (tk.get_token_line() != line) { - int from = line + 1; - line = tk.get_token_line(); - - for (int i = from; i <= line; i++) { - int l = i - 1; - if (l >= 0 && l < lines.size()) { - print_line("\n" + itos(i) + ": " + lines[l] + "\n"); - } - } - } - print_line("\t(" + itos(tk.get_token_column()) + "): " + text); - tk.advance(); - } + switch (p_type) { + case TEST_TOKENIZER: + test_tokenizer(code, lines); + break; + case TEST_PARSER: + test_parser(code, test, lines); + break; + case TEST_COMPILER: + case TEST_BYTECODE: + print_line("Not implemented."); } - if (p_type == TEST_PARSER) { - GDScriptParser parser; - Error err = parser.parse(code); - if (err) { - print_line("Parse Error:\n" + itos(parser.get_error_line()) + ":" + itos(parser.get_error_column()) + ":" + parser.get_error()); - memdelete(fa); - return nullptr; - } - - const GDScriptParser::Node *root = parser.get_parse_tree(); - ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, nullptr); - const GDScriptParser::ClassNode *cnode = static_cast(root); - - _parser_show_class(cnode, 0, lines); - } - - if (p_type == TEST_COMPILER) { - GDScriptParser parser; - - Error err = parser.parse(code); - if (err) { - print_line("Parse Error:\n" + itos(parser.get_error_line()) + ":" + itos(parser.get_error_column()) + ":" + parser.get_error()); - memdelete(fa); - return nullptr; - } - - Ref gds; - gds.instance(); - - GDScriptCompiler gdc; - err = gdc.compile(&parser, gds.ptr()); - if (err) { - print_line("Compile Error:\n" + itos(gdc.get_error_line()) + ":" + itos(gdc.get_error_column()) + ":" + gdc.get_error()); - return nullptr; - } - - Ref current = gds; - - while (current.is_valid()) { - print_line("** CLASS **"); - _disassemble_class(current, lines); - - current = current->get_base(); - } - - } else if (p_type == TEST_BYTECODE) { - Vector buf2 = GDScriptTokenizerBuffer::parse_code_string(code); - String dst = test.get_basename() + ".gdc"; - FileAccess *fw = FileAccess::open(dst, FileAccess::WRITE); - fw->store_buffer(buf2.ptr(), buf2.size()); - memdelete(fw); - } - - memdelete(fa); - return nullptr; } diff --git a/modules/gdscript/editor/gdscript_highlighter.cpp b/modules/gdscript/editor/gdscript_highlighter.cpp index 43d0116125..aba3e07134 100644 --- a/modules/gdscript/editor/gdscript_highlighter.cpp +++ b/modules/gdscript/editor/gdscript_highlighter.cpp @@ -288,7 +288,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) if (str[k] == '(') { in_function_name = true; - } else if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::TK_PR_VAR)) { + } else if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR)) { in_variable_declaration = true; } } @@ -357,7 +357,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) } else if (in_function_name) { next_type = FUNCTION; - if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::TK_PR_FUNCTION)) { + if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) { color = function_definition_color; } else { color = function_color; diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 632407c61f..40ef0aeec6 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -39,7 +39,11 @@ #include "core/os/file_access.h" #include "core/os/os.h" #include "core/project_settings.h" +#include "gdscript_analyzer.h" +#include "gdscript_cache.h" #include "gdscript_compiler.h" +#include "gdscript_parser.h" +#include "gdscript_warning.h" /////////////////////////// @@ -79,6 +83,17 @@ Object *GDScriptNativeClass::instance() { return ClassDB::instance(name); } +void GDScript::_super_implicit_constructor(GDScript *p_script, GDScriptInstance *p_instance, Callable::CallError &r_error) { + GDScript *base = p_script->_base; + if (base != nullptr) { + _super_implicit_constructor(base, p_instance, r_error); + if (r_error.error != Callable::CallError::CALL_OK) { + return; + } + } + p_script->implicit_initializer->call(p_instance, nullptr, 0, r_error); +} + GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Callable::CallError &r_error) { /* STEP 1, CREATE */ @@ -101,10 +116,8 @@ GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argco MutexLock lock(GDScriptLanguage::singleton->lock); instances.insert(instance->owner); } - if (p_argcount < 0) { - return instance; - } - initializer->call(instance, p_args, p_argcount, r_error); + + _super_implicit_constructor(this, instance, r_error); if (r_error.error != Callable::CallError::CALL_OK) { instance->script = Ref(); instance->owner->set_script_instance(nullptr); @@ -114,6 +127,22 @@ GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argco } ERR_FAIL_V_MSG(nullptr, "Error constructing a GDScriptInstance."); } + + if (p_argcount < 0) { + return instance; + } + if (initializer != nullptr) { + initializer->call(instance, p_args, p_argcount, r_error); + if (r_error.error != Callable::CallError::CALL_OK) { + instance->script = Ref(); + instance->owner->set_script_instance(nullptr); + { + MutexLock lock(GDScriptLanguage::singleton->lock); + instances.erase(p_owner); + } + ERR_FAIL_V_MSG(nullptr, "Error constructing a GDScriptInstance."); + } + } //@TODO make thread safe return instance; } @@ -382,13 +411,11 @@ bool GDScript::_update_exports(bool *r_err, bool p_recursive_call) { } GDScriptParser parser; - Error err = parser.parse(source, basedir, true, path); + GDScriptAnalyzer analyzer(&parser); + Error err = parser.parse(source, path, false); - if (err == OK) { - const GDScriptParser::Node *root = parser.get_parse_tree(); - ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, false); - - const GDScriptParser::ClassNode *c = static_cast(root); + if (err == OK && analyzer.analyze() == OK) { + const GDScriptParser::ClassNode *c = parser.get_tree(); if (base_cache.is_valid()) { base_cache->inheriters_cache.erase(get_instance_id()); @@ -397,8 +424,8 @@ bool GDScript::_update_exports(bool *r_err, bool p_recursive_call) { if (c->extends_used) { String path = ""; - if (String(c->extends_file) != "" && String(c->extends_file) != get_path()) { - path = c->extends_file; + if (String(c->extends_path) != "" && String(c->extends_path) != get_path()) { + path = c->extends_path; if (path.is_rel_path()) { String base = get_path(); if (base == "" || base.is_rel_path()) { @@ -407,8 +434,8 @@ bool GDScript::_update_exports(bool *r_err, bool p_recursive_call) { path = base.get_base_dir().plus_file(path); } } - } else if (c->extends_class.size() != 0) { - String base = c->extends_class[0]; + } else if (c->extends.size() != 0) { + const StringName &base = c->extends[0]; if (ScriptServer::is_global_class(base)) { path = ScriptServer::get_global_class_path(base); @@ -431,20 +458,36 @@ bool GDScript::_update_exports(bool *r_err, bool p_recursive_call) { members_cache.clear(); member_default_values_cache.clear(); - - for (int i = 0; i < c->variables.size(); i++) { - if (c->variables[i]._export.type == Variant::NIL) { - continue; - } - - members_cache.push_back(c->variables[i]._export); - member_default_values_cache[c->variables[i].identifier] = c->variables[i].default_value; - } - _signals.clear(); - for (int i = 0; i < c->_signals.size(); i++) { - _signals[c->_signals[i].name] = c->_signals[i].arguments; + for (int i = 0; i < c->members.size(); i++) { + const GDScriptParser::ClassNode::Member &member = c->members[i]; + + switch (member.type) { + case GDScriptParser::ClassNode::Member::VARIABLE: { + if (!member.variable->exported) { + continue; + } + + members_cache.push_back(member.variable->export_info); + Variant default_value; + if (member.variable->initializer && member.variable->initializer->is_constant) { + default_value = member.variable->initializer->reduced_value; + } + member_default_values_cache[member.variable->identifier->name] = default_value; + } break; + case GDScriptParser::ClassNode::Member::SIGNAL: { + // TODO: Cache this in parser to avoid loops like this. + Vector parameters_names; + parameters_names.resize(member.signal->parameters.size()); + for (int j = 0; j < member.signal->parameters.size(); j++) { + parameters_names.write[j] = member.signal->parameters[j]->identifier->name; + } + _signals[member.signal->identifier->name] = parameters_names; + } break; + default: + break; // Nothing. + } } } else { placeholder_fallback_enabled = true; @@ -555,16 +598,29 @@ Error GDScript::reload(bool p_keep_state) { valid = false; GDScriptParser parser; - Error err = parser.parse(source, basedir, false, path); + Error err = parser.parse(source, path, false); if (err) { if (EngineDebugger::is_active()) { - GDScriptLanguage::get_singleton()->debug_break_parse(get_path(), parser.get_error_line(), "Parser Error: " + parser.get_error()); + GDScriptLanguage::get_singleton()->debug_break_parse(get_path(), parser.get_errors().front()->get().line, "Parser Error: " + parser.get_errors().front()->get().message); } - _err_print_error("GDScript::reload", path.empty() ? "built-in" : (const char *)path.utf8().get_data(), parser.get_error_line(), ("Parse Error: " + parser.get_error()).utf8().get_data(), ERR_HANDLER_SCRIPT); + // TODO: Show all error messages. + _err_print_error("GDScript::reload", path.empty() ? "built-in" : (const char *)path.utf8().get_data(), parser.get_errors().front()->get().line, ("Parse Error: " + parser.get_errors().front()->get().message).utf8().get_data(), ERR_HANDLER_SCRIPT); ERR_FAIL_V(ERR_PARSE_ERROR); } - bool can_run = ScriptServer::is_scripting_enabled() || parser.is_tool_script(); + GDScriptAnalyzer analyzer(&parser); + err = analyzer.analyze(); + + if (err) { + if (EngineDebugger::is_active()) { + GDScriptLanguage::get_singleton()->debug_break_parse(get_path(), parser.get_errors().front()->get().line, "Parser Error: " + parser.get_errors().front()->get().message); + } + // TODO: Show all error messages. + _err_print_error("GDScript::reload", path.empty() ? "built-in" : (const char *)path.utf8().get_data(), parser.get_errors().front()->get().line, ("Parse Error: " + parser.get_errors().front()->get().message).utf8().get_data(), ERR_HANDLER_SCRIPT); + ERR_FAIL_V(ERR_PARSE_ERROR); + } + + bool can_run = ScriptServer::is_scripting_enabled() || parser.is_tool(); GDScriptCompiler compiler; err = compiler.compile(&parser, this, p_keep_state); @@ -585,7 +641,7 @@ Error GDScript::reload(bool p_keep_state) { const GDScriptWarning &warning = E->get(); if (EngineDebugger::is_active()) { Vector si; - EngineDebugger::get_script_debugger()->send_error("", get_path(), warning.line, warning.get_name(), warning.get_message(), ERR_HANDLER_WARNING, si); + EngineDebugger::get_script_debugger()->send_error("", get_path(), warning.start_line, warning.get_name(), warning.get_message(), ERR_HANDLER_WARNING, si); } } #endif @@ -753,83 +809,12 @@ void GDScript::_bind_methods() { } Vector GDScript::get_as_byte_code() const { - GDScriptTokenizerBuffer tokenizer; - return tokenizer.parse_code_string(source); + return Vector(); }; +// TODO: Fully remove this. There's not this kind of "bytecode" anymore. Error GDScript::load_byte_code(const String &p_path) { - Vector bytecode; - - if (p_path.ends_with("gde")) { - FileAccess *fa = FileAccess::open(p_path, FileAccess::READ); - ERR_FAIL_COND_V(!fa, ERR_CANT_OPEN); - - FileAccessEncrypted *fae = memnew(FileAccessEncrypted); - ERR_FAIL_COND_V(!fae, ERR_CANT_OPEN); - - Vector key; - key.resize(32); - for (int i = 0; i < key.size(); i++) { - key.write[i] = script_encryption_key[i]; - } - - Error err = fae->open_and_parse(fa, key, FileAccessEncrypted::MODE_READ); - - if (err) { - fa->close(); - memdelete(fa); - memdelete(fae); - - ERR_FAIL_COND_V(err, err); - } - - bytecode.resize(fae->get_len()); - fae->get_buffer(bytecode.ptrw(), bytecode.size()); - fae->close(); - memdelete(fae); - - } else { - bytecode = FileAccess::get_file_as_array(p_path); - } - - ERR_FAIL_COND_V(bytecode.size() == 0, ERR_PARSE_ERROR); - path = p_path; - - String basedir = path; - - if (basedir == "") { - basedir = get_path(); - } - - if (basedir != "") { - basedir = basedir.get_base_dir(); - } - - valid = false; - GDScriptParser parser; - Error err = parser.parse_bytecode(bytecode, basedir, get_path()); - if (err) { - _err_print_error("GDScript::load_byte_code", path.empty() ? "built-in" : (const char *)path.utf8().get_data(), parser.get_error_line(), ("Parse Error: " + parser.get_error()).utf8().get_data(), ERR_HANDLER_SCRIPT); - ERR_FAIL_V(ERR_PARSE_ERROR); - } - - GDScriptCompiler compiler; - err = compiler.compile(&parser, this); - - if (err) { - _err_print_error("GDScript::load_byte_code", path.empty() ? "built-in" : (const char *)path.utf8().get_data(), compiler.get_error_line(), ("Compile Error: " + compiler.get_error()).utf8().get_data(), ERR_HANDLER_SCRIPT); - ERR_FAIL_V(ERR_COMPILATION_FAILED); - } - - valid = true; - - for (Map>::Element *E = subclasses.front(); E; E = E->next()) { - _set_subclass_path(E->get(), path); - } - - _init_rpc_methods_properties(); - - return OK; + return ERR_COMPILATION_FAILED; } Error GDScript::load_source_code(const String &p_path) { @@ -1055,6 +1040,8 @@ GDScript::~GDScript() { memdelete(E->get()); } + GDScriptCache::remove_script(get_path()); + _save_orphaned_subclasses(); #ifdef DEBUG_ENABLED @@ -1152,6 +1139,32 @@ bool GDScriptInstance::get(const StringName &p_name, Variant &r_ret) const { } } + { + // Signals. + const GDScript *sl = sptr; + while (sl) { + const Map>::Element *E = sl->_signals.find(p_name); + if (E) { + r_ret = Signal(this->owner, E->key()); + return true; //index found + } + sl = sl->_base; + } + } + + { + // Methods. + const GDScript *sl = sptr; + while (sl) { + const Map::Element *E = sl->member_functions.find(p_name); + if (E) { + r_ret = Callable(this->owner, E->key()); + return true; //index found + } + sl = sl->_base; + } + } + { const Map::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get); if (E) { @@ -1304,6 +1317,7 @@ void GDScriptInstance::call_multilevel(const StringName &p_method, const Variant Map::Element *E = sptr->member_functions.find(p_method); if (E) { E->get()->call(this, p_args, p_argcount, ce); + return; } sptr = sptr->_base; } @@ -1827,6 +1841,7 @@ void GDScriptLanguage::frame() { /* EDITOR FUNCTIONS */ void GDScriptLanguage::get_reserved_words(List *p_words) const { + // TODO: Add annotations here? static const char *_reserved_words[] = { // operators "and", @@ -1849,6 +1864,7 @@ void GDScriptLanguage::get_reserved_words(List *p_words) const { // functions "as", "assert", + "await", "breakpoint", "class", "class_name", @@ -1856,15 +1872,13 @@ void GDScriptLanguage::get_reserved_words(List *p_words) const { "is", "func", "preload", - "setget", "signal", - "tool", + "super", + "trait", "yield", // var "const", "enum", - "export", - "onready", "static", "var", // control flow @@ -1878,12 +1892,6 @@ void GDScriptLanguage::get_reserved_words(List *p_words) const { "return", "match", "while", - "remote", - "master", - "puppet", - "remotesync", - "mastersync", - "puppetsync", nullptr }; @@ -1914,10 +1922,11 @@ String GDScriptLanguage::get_global_class_name(const String &p_path, String *r_b String source = f->get_as_utf8_string(); GDScriptParser parser; - parser.parse(source, p_path.get_base_dir(), true, p_path, false, nullptr, true); + err = parser.parse(source, p_path, false); - if (parser.get_parse_tree() && parser.get_parse_tree()->type == GDScriptParser::Node::TYPE_CLASS) { - const GDScriptParser::ClassNode *c = static_cast(parser.get_parse_tree()); + // TODO: Simplify this code by using the analyzer to get full inheritance. + if (err == OK) { + const GDScriptParser::ClassNode *c = parser.get_tree(); if (r_icon_path) { if (c->icon_path.empty() || c->icon_path.is_abs_path()) { *r_icon_path = c->icon_path; @@ -1931,15 +1940,15 @@ String GDScriptLanguage::get_global_class_name(const String &p_path, String *r_b GDScriptParser subparser; while (subclass) { if (subclass->extends_used) { - if (subclass->extends_file) { - if (subclass->extends_class.size() == 0) { - get_global_class_name(subclass->extends_file, r_base_type); + if (!subclass->extends_path.empty()) { + if (subclass->extends.size() == 0) { + get_global_class_name(subclass->extends_path, r_base_type); subclass = nullptr; break; } else { - Vector extend_classes = subclass->extends_class; + Vector extend_classes = subclass->extends; - FileAccessRef subfile = FileAccess::open(subclass->extends_file, FileAccess::READ); + FileAccessRef subfile = FileAccess::open(subclass->extends_path, FileAccess::READ); if (!subfile) { break; } @@ -1948,25 +1957,26 @@ String GDScriptLanguage::get_global_class_name(const String &p_path, String *r_b if (subsource.empty()) { break; } - String subpath = subclass->extends_file; + String subpath = subclass->extends_path; if (subpath.is_rel_path()) { subpath = path.get_base_dir().plus_file(subpath).simplify_path(); } - if (OK != subparser.parse(subsource, subpath.get_base_dir(), true, subpath, false, nullptr, true)) { + if (OK != subparser.parse(subsource, subpath, false)) { break; } path = subpath; - if (!subparser.get_parse_tree() || subparser.get_parse_tree()->type != GDScriptParser::Node::TYPE_CLASS) { - break; - } - subclass = static_cast(subparser.get_parse_tree()); + subclass = subparser.get_tree(); while (extend_classes.size() > 0) { bool found = false; - for (int i = 0; i < subclass->subclasses.size(); i++) { - const GDScriptParser::ClassNode *inner_class = subclass->subclasses[i]; - if (inner_class->name == extend_classes[0]) { + for (int i = 0; i < subclass->members.size(); i++) { + if (subclass->members[i].type != GDScriptParser::ClassNode::Member::CLASS) { + continue; + } + + const GDScriptParser::ClassNode *inner_class = subclass->members[i].m_class; + if (inner_class->identifier->name == extend_classes[0]) { extend_classes.remove(0); found = true; subclass = inner_class; @@ -1979,8 +1989,8 @@ String GDScriptLanguage::get_global_class_name(const String &p_path, String *r_b } } } - } else if (subclass->extends_class.size() == 1) { - *r_base_type = subclass->extends_class[0]; + } else if (subclass->extends.size() == 1) { + *r_base_type = subclass->extends[0]; subclass = nullptr; } else { break; @@ -1991,181 +2001,12 @@ String GDScriptLanguage::get_global_class_name(const String &p_path, String *r_b } } } - return c->name; + return c->identifier != nullptr ? String(c->identifier->name) : String(); } return String(); } -#ifdef DEBUG_ENABLED -String GDScriptWarning::get_message() const { -#define CHECK_SYMBOLS(m_amount) ERR_FAIL_COND_V(symbols.size() < m_amount, String()); - - switch (code) { - case UNASSIGNED_VARIABLE_OP_ASSIGN: { - CHECK_SYMBOLS(1); - return "Using assignment with operation but the variable '" + symbols[0] + "' was not previously assigned a value."; - } break; - case UNASSIGNED_VARIABLE: { - CHECK_SYMBOLS(1); - return "The variable '" + symbols[0] + "' was used but never assigned a value."; - } break; - case UNUSED_VARIABLE: { - CHECK_SYMBOLS(1); - return "The local variable '" + symbols[0] + "' is declared but never used in the block. If this is intended, prefix it with an underscore: '_" + symbols[0] + "'"; - } break; - case SHADOWED_VARIABLE: { - CHECK_SYMBOLS(2); - return "The local variable '" + symbols[0] + "' is shadowing an already-defined variable at line " + symbols[1] + "."; - } break; - case UNUSED_CLASS_VARIABLE: { - CHECK_SYMBOLS(1); - return "The class variable '" + symbols[0] + "' is declared but never used in the script."; - } break; - case UNUSED_ARGUMENT: { - CHECK_SYMBOLS(2); - return "The argument '" + symbols[1] + "' is never used in the function '" + symbols[0] + "'. If this is intended, prefix it with an underscore: '_" + symbols[1] + "'"; - } break; - case UNREACHABLE_CODE: { - CHECK_SYMBOLS(1); - return "Unreachable code (statement after return) in function '" + symbols[0] + "()'."; - } break; - case STANDALONE_EXPRESSION: { - return "Standalone expression (the line has no effect)."; - } break; - case VOID_ASSIGNMENT: { - CHECK_SYMBOLS(1); - return "Assignment operation, but the function '" + symbols[0] + "()' returns void."; - } break; - case NARROWING_CONVERSION: { - return "Narrowing conversion (float is converted to int and loses precision)."; - } break; - case FUNCTION_MAY_YIELD: { - CHECK_SYMBOLS(1); - return "Assigned variable is typed but the function '" + symbols[0] + "()' may yield and return a GDScriptFunctionState instead."; - } break; - case VARIABLE_CONFLICTS_FUNCTION: { - CHECK_SYMBOLS(1); - return "Variable declaration of '" + symbols[0] + "' conflicts with a function of the same name."; - } break; - case FUNCTION_CONFLICTS_VARIABLE: { - CHECK_SYMBOLS(1); - return "Function declaration of '" + symbols[0] + "()' conflicts with a variable of the same name."; - } break; - case FUNCTION_CONFLICTS_CONSTANT: { - CHECK_SYMBOLS(1); - return "Function declaration of '" + symbols[0] + "()' conflicts with a constant of the same name."; - } break; - case INCOMPATIBLE_TERNARY: { - return "Values of the ternary conditional are not mutually compatible."; - } break; - case UNUSED_SIGNAL: { - CHECK_SYMBOLS(1); - return "The signal '" + symbols[0] + "' is declared but never emitted."; - } break; - case RETURN_VALUE_DISCARDED: { - CHECK_SYMBOLS(1); - return "The function '" + symbols[0] + "()' returns a value, but this value is never used."; - } break; - case PROPERTY_USED_AS_FUNCTION: { - CHECK_SYMBOLS(2); - return "The method '" + symbols[0] + "()' was not found in base '" + symbols[1] + "' but there's a property with the same name. Did you mean to access it?"; - } break; - case CONSTANT_USED_AS_FUNCTION: { - CHECK_SYMBOLS(2); - return "The method '" + symbols[0] + "()' was not found in base '" + symbols[1] + "' but there's a constant with the same name. Did you mean to access it?"; - } break; - case FUNCTION_USED_AS_PROPERTY: { - CHECK_SYMBOLS(2); - return "The property '" + symbols[0] + "' was not found in base '" + symbols[1] + "' but there's a method with the same name. Did you mean to call it?"; - } break; - case INTEGER_DIVISION: { - return "Integer division, decimal part will be discarded."; - } break; - case UNSAFE_PROPERTY_ACCESS: { - CHECK_SYMBOLS(2); - return "The property '" + symbols[0] + "' is not present on the inferred type '" + symbols[1] + "' (but may be present on a subtype)."; - } break; - case UNSAFE_METHOD_ACCESS: { - CHECK_SYMBOLS(2); - return "The method '" + symbols[0] + "' is not present on the inferred type '" + symbols[1] + "' (but may be present on a subtype)."; - } break; - case UNSAFE_CAST: { - CHECK_SYMBOLS(1); - return "The value is cast to '" + symbols[0] + "' but has an unknown type."; - } break; - case UNSAFE_CALL_ARGUMENT: { - CHECK_SYMBOLS(4); - return "The argument '" + symbols[0] + "' of the function '" + symbols[1] + "' requires a the subtype '" + symbols[2] + "' but the supertype '" + symbols[3] + "' was provided"; - } break; - case DEPRECATED_KEYWORD: { - CHECK_SYMBOLS(2); - return "The '" + symbols[0] + "' keyword is deprecated and will be removed in a future release, please replace its uses by '" + symbols[1] + "'."; - } break; - case STANDALONE_TERNARY: { - return "Standalone ternary conditional operator: the return value is being discarded."; - } - case WARNING_MAX: - break; // Can't happen, but silences warning - } - ERR_FAIL_V_MSG(String(), "Invalid GDScript warning code: " + get_name_from_code(code) + "."); - -#undef CHECK_SYMBOLS -} - -String GDScriptWarning::get_name() const { - return get_name_from_code(code); -} - -String GDScriptWarning::get_name_from_code(Code p_code) { - ERR_FAIL_COND_V(p_code < 0 || p_code >= WARNING_MAX, String()); - - static const char *names[] = { - "UNASSIGNED_VARIABLE", - "UNASSIGNED_VARIABLE_OP_ASSIGN", - "UNUSED_VARIABLE", - "SHADOWED_VARIABLE", - "UNUSED_CLASS_VARIABLE", - "UNUSED_ARGUMENT", - "UNREACHABLE_CODE", - "STANDALONE_EXPRESSION", - "VOID_ASSIGNMENT", - "NARROWING_CONVERSION", - "FUNCTION_MAY_YIELD", - "VARIABLE_CONFLICTS_FUNCTION", - "FUNCTION_CONFLICTS_VARIABLE", - "FUNCTION_CONFLICTS_CONSTANT", - "INCOMPATIBLE_TERNARY", - "UNUSED_SIGNAL", - "RETURN_VALUE_DISCARDED", - "PROPERTY_USED_AS_FUNCTION", - "CONSTANT_USED_AS_FUNCTION", - "FUNCTION_USED_AS_PROPERTY", - "INTEGER_DIVISION", - "UNSAFE_PROPERTY_ACCESS", - "UNSAFE_METHOD_ACCESS", - "UNSAFE_CAST", - "UNSAFE_CALL_ARGUMENT", - "DEPRECATED_KEYWORD", - "STANDALONE_TERNARY", - nullptr - }; - - return names[(int)p_code]; -} - -GDScriptWarning::Code GDScriptWarning::get_code_from_name(const String &p_name) { - for (int i = 0; i < WARNING_MAX; i++) { - if (get_name_from_code((Code)i) == p_name) { - return (Code)i; - } - } - - ERR_FAIL_V_MSG(WARNING_MAX, "Invalid GDScript warning name: " + p_name); -} - -#endif // DEBUG_ENABLED - GDScriptLanguage::GDScriptLanguage() { calls = 0; ERR_FAIL_COND(singleton); @@ -2204,7 +2045,7 @@ GDScriptLanguage::GDScriptLanguage() { GLOBAL_DEF("debug/gdscript/completion/autocomplete_setters_and_getters", false); for (int i = 0; i < (int)GDScriptWarning::WARNING_MAX; i++) { String warning = GDScriptWarning::get_name_from_code((GDScriptWarning::Code)i).to_lower(); - bool default_enabled = !warning.begins_with("unsafe_") && i != GDScriptWarning::UNUSED_CLASS_VARIABLE; + bool default_enabled = !warning.begins_with("unsafe_"); GLOBAL_DEF("debug/gdscript/warnings/" + warning, default_enabled); } #endif // DEBUG_ENABLED @@ -2242,36 +2083,28 @@ RES ResourceFormatLoaderGDScript::load(const String &p_path, const String &p_ori *r_error = ERR_FILE_CANT_OPEN; } - GDScript *script = memnew(GDScript); + Error err; + Ref script = GDScriptCache::get_full_script(p_path, err); - Ref scriptres(script); + // TODO: Reintroduce binary and encrypted scripts. - if (p_path.ends_with(".gde") || p_path.ends_with(".gdc")) { - script->set_script_path(p_original_path); // script needs this. - script->set_path(p_original_path); - Error err = script->load_byte_code(p_path); - ERR_FAIL_COND_V_MSG(err != OK, RES(), "Cannot load byte code from file '" + p_path + "'."); - - } else { - Error err = script->load_source_code(p_path); - ERR_FAIL_COND_V_MSG(err != OK, RES(), "Cannot load source code from file '" + p_path + "'."); - - script->set_script_path(p_original_path); // script needs this. - script->set_path(p_original_path); - - script->reload(); + if (script.is_null()) { + // Don't fail loading because of parsing error. + script.instance(); } + if (r_error) { *r_error = OK; } - return scriptres; + return script; } void ResourceFormatLoaderGDScript::get_recognized_extensions(List *p_extensions) const { p_extensions->push_back("gd"); - p_extensions->push_back("gdc"); - p_extensions->push_back("gde"); + // TODO: Reintroduce binary and encrypted scripts. + // p_extensions->push_back("gdc"); + // p_extensions->push_back("gde"); } bool ResourceFormatLoaderGDScript::handles_type(const String &p_type) const { @@ -2280,7 +2113,8 @@ bool ResourceFormatLoaderGDScript::handles_type(const String &p_type) const { String ResourceFormatLoaderGDScript::get_resource_type(const String &p_path) const { String el = p_path.get_extension().to_lower(); - if (el == "gd" || el == "gdc" || el == "gde") { + // TODO: Reintroduce binary and encrypted scripts. + if (el == "gd" /*|| el == "gdc" || el == "gde"*/) { return "GDScript"; } return ""; @@ -2296,7 +2130,7 @@ void ResourceFormatLoaderGDScript::get_dependencies(const String &p_path, List member_info; - GDScriptFunction *initializer; //direct pointer to _init , faster to locate + GDScriptFunction *implicit_initializer = nullptr; + GDScriptFunction *initializer = nullptr; //direct pointer to new , faster to locate int subclass_count; Set instances; @@ -117,6 +118,7 @@ class GDScript : public Script { SelfList::List pending_func_states; + void _super_implicit_constructor(GDScript *p_script, GDScriptInstance *p_instance, Callable::CallError &r_error); GDScriptInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Callable::CallError &r_error); void _set_subclass_path(Ref &p_sc, const String &p_path); @@ -301,52 +303,6 @@ public: ~GDScriptInstance(); }; -#ifdef DEBUG_ENABLED -struct GDScriptWarning { - enum Code { - UNASSIGNED_VARIABLE, // Variable used but never assigned - UNASSIGNED_VARIABLE_OP_ASSIGN, // Variable never assigned but used in an assignment operation (+=, *=, etc) - UNUSED_VARIABLE, // Local variable is declared but never used - SHADOWED_VARIABLE, // Variable name shadowed by other variable - UNUSED_CLASS_VARIABLE, // Class variable is declared but never used in the file - UNUSED_ARGUMENT, // Function argument is never used - UNREACHABLE_CODE, // Code after a return statement - STANDALONE_EXPRESSION, // Expression not assigned to a variable - VOID_ASSIGNMENT, // Function returns void but it's assigned to a variable - NARROWING_CONVERSION, // Float value into an integer slot, precision is lost - FUNCTION_MAY_YIELD, // Typed assign of function call that yields (it may return a function state) - VARIABLE_CONFLICTS_FUNCTION, // Variable has the same name of a function - FUNCTION_CONFLICTS_VARIABLE, // Function has the same name of a variable - FUNCTION_CONFLICTS_CONSTANT, // Function has the same name of a constant - INCOMPATIBLE_TERNARY, // Possible values of a ternary if are not mutually compatible - UNUSED_SIGNAL, // Signal is defined but never emitted - RETURN_VALUE_DISCARDED, // Function call returns something but the value isn't used - PROPERTY_USED_AS_FUNCTION, // Function not found, but there's a property with the same name - CONSTANT_USED_AS_FUNCTION, // Function not found, but there's a constant with the same name - FUNCTION_USED_AS_PROPERTY, // Property not found, but there's a function with the same name - INTEGER_DIVISION, // Integer divide by integer, decimal part is discarded - UNSAFE_PROPERTY_ACCESS, // Property not found in the detected type (but can be in subtypes) - UNSAFE_METHOD_ACCESS, // Function not found in the detected type (but can be in subtypes) - UNSAFE_CAST, // Cast used in an unknown type - UNSAFE_CALL_ARGUMENT, // Function call argument is of a supertype of the require argument - DEPRECATED_KEYWORD, // The keyword is deprecated and should be replaced - STANDALONE_TERNARY, // Return value of ternary expression is discarded - WARNING_MAX, - }; - - Code code = WARNING_MAX; - Vector symbols; - int line = -1; - - String get_name() const; - String get_message() const; - static String get_name_from_code(Code p_code); - static Code get_code_from_name(const String &p_name); - - GDScriptWarning() {} -}; -#endif // DEBUG_ENABLED - class GDScriptLanguage : public ScriptLanguage { friend class GDScriptFunctionState; diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp new file mode 100644 index 0000000000..51d93481b6 --- /dev/null +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -0,0 +1,3062 @@ +/*************************************************************************/ +/* gdscript_analyzer.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "gdscript_analyzer.h" + +#include "core/class_db.h" +#include "core/hash_map.h" +#include "core/io/resource_loader.h" +#include "core/project_settings.h" +#include "core/script_language.h" +#include "gdscript.h" + +// TODO: Move this to a central location (maybe core?). +static HashMap underscore_map; +static const char *underscore_classes[] = { + "ClassDB", + "Directory", + "Engine", + "File", + "Geometry", + "GodotSharp", + "JSON", + "Marshalls", + "Mutex", + "OS", + "ResourceLoader", + "ResourceSaver", + "Semaphore", + "Thread", + "VisualScriptEditor", + nullptr, +}; +static StringName get_real_class_name(const StringName &p_source) { + if (underscore_map.empty()) { + const char **class_name = underscore_classes; + while (*class_name != nullptr) { + underscore_map[*class_name] = String("_") + *class_name; + class_name++; + } + } + if (underscore_map.has(p_source)) { + return underscore_map[p_source]; + } + return p_source; +} + +static GDScriptParser::DataType make_callable_type(const MethodInfo &p_info) { + GDScriptParser::DataType type; + type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + type.kind = GDScriptParser::DataType::BUILTIN; + type.builtin_type = Variant::CALLABLE; + type.is_constant = true; + type.method_info = p_info; + return type; +} + +static GDScriptParser::DataType make_signal_type(const MethodInfo &p_info) { + GDScriptParser::DataType type; + type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + type.kind = GDScriptParser::DataType::BUILTIN; + type.builtin_type = Variant::SIGNAL; + type.is_constant = true; + type.method_info = p_info; + return type; +} + +static GDScriptParser::DataType make_native_meta_type(const StringName &p_class_name) { + GDScriptParser::DataType type; + type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + type.kind = GDScriptParser::DataType::NATIVE; + type.builtin_type = Variant::OBJECT; + type.is_constant = true; + type.native_type = p_class_name; + type.is_meta_type = true; + return type; +} + +static GDScriptParser::DataType make_native_enum_type(const StringName &p_native_class, const StringName &p_enum_name) { + GDScriptParser::DataType type; + type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + type.kind = GDScriptParser::DataType::ENUM; + type.builtin_type = Variant::OBJECT; + type.is_constant = true; + type.is_meta_type = true; + + List enum_values; + StringName real_native_name = get_real_class_name(p_native_class); + ClassDB::get_enum_constants(real_native_name, p_enum_name, &enum_values); + + for (const List::Element *E = enum_values.front(); E != nullptr; E = E->next()) { + type.enum_values[E->get()] = ClassDB::get_integer_constant(real_native_name, E->get()); + } + + return type; +} + +static GDScriptParser::DataType make_builtin_meta_type(Variant::Type p_type) { + GDScriptParser::DataType type; + type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + type.kind = GDScriptParser::DataType::BUILTIN; + type.builtin_type = p_type; + type.is_constant = true; + type.is_meta_type = true; + return type; +} + +Error GDScriptAnalyzer::resolve_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive) { + if (p_class->base_type.is_set()) { + // Already resolved + return OK; + } + + if (p_class == parser->head) { + if (p_class->identifier) { + p_class->fqcn = p_class->identifier->name; + } else { + p_class->fqcn = parser->script_path; + } + } else { + p_class->fqcn = p_class->outer->fqcn + "::" + String(p_class->identifier->name); + } + + GDScriptParser::DataType result; + + // Set datatype for class. + GDScriptParser::DataType class_type; + class_type.is_constant = true; + class_type.is_meta_type = true; + class_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + class_type.kind = GDScriptParser::DataType::CLASS; + class_type.class_type = p_class; + class_type.script_path = parser->script_path; + p_class->set_datatype(class_type); + + if (!p_class->extends_used) { + result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED; + result.kind = GDScriptParser::DataType::NATIVE; + result.native_type = "Reference"; + } else { + result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + + GDScriptParser::DataType base; + + int extends_index = 0; + + if (!p_class->extends_path.empty()) { + Ref parser = get_parser_for(p_class->extends_path); + if (parser.is_null()) { + push_error(vformat(R"(Could not resolve super class path "%s".)", p_class->extends_path), p_class); + return ERR_PARSE_ERROR; + } + + Error err = parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED); + if (err != OK) { + push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", p_class->extends_path), p_class); + return err; + } + + base = parser->get_parser()->head->get_datatype(); + } else { + if (p_class->extends.empty()) { + return ERR_PARSE_ERROR; + } + const StringName &name = p_class->extends[extends_index++]; + base.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + + if (ScriptServer::is_global_class(name)) { + String base_path = ScriptServer::get_global_class_path(name); + + if (base_path == parser->script_path) { + base = parser->head->get_datatype(); + } else { + Ref parser = get_parser_for(base_path); + if (parser.is_null()) { + push_error(vformat(R"(Could not resolve super class "%s".)", name), p_class); + return ERR_PARSE_ERROR; + } + + Error err = parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED); + if (err != OK) { + push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", name), p_class); + return err; + } + } + } else if (ProjectSettings::get_singleton()->has_autoload(name) && ProjectSettings::get_singleton()->get_autoload(name).is_singleton) { + const ProjectSettings::AutoloadInfo &info = ProjectSettings::get_singleton()->get_autoload(name); + if (info.path.get_extension().to_lower() != ".gd") { + push_error(vformat(R"(Singleton %s is not a GDScript.)", info.name), p_class); + return ERR_PARSE_ERROR; + } + + Ref parser = get_parser_for(info.path); + if (parser.is_null()) { + push_error(vformat(R"(Could not parse singleton from "%s".)", info.path), p_class); + return ERR_PARSE_ERROR; + } + + Error err = parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED); + if (err != OK) { + push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", name), p_class); + return err; + } + } else if (class_exists(name) && ClassDB::can_instance(get_real_class_name(name))) { + base.kind = GDScriptParser::DataType::NATIVE; + base.native_type = name; + } else { + // Look for other classes in script. + GDScriptParser::ClassNode *look_class = p_class; + bool found = false; + while (look_class != nullptr) { + if (look_class->identifier && look_class->identifier->name == name) { + if (!look_class->get_datatype().is_set()) { + Error err = resolve_inheritance(look_class, false); + if (err) { + return err; + } + } + base = look_class->get_datatype(); + found = true; + break; + } + if (look_class->members_indices.has(name) && look_class->get_member(name).type == GDScriptParser::ClassNode::Member::CLASS) { + GDScriptParser::ClassNode::Member member = look_class->get_member(name); + if (!member.m_class->get_datatype().is_set()) { + Error err = resolve_inheritance(member.m_class, false); + if (err) { + return err; + } + } + base = member.m_class->get_datatype(); + found = true; + break; + } + look_class = look_class->outer; + } + + if (!found) { + push_error(vformat(R"(Could not find base class "%s".)", name), p_class); + return ERR_PARSE_ERROR; + } + } + } + + for (int index = extends_index; index < p_class->extends.size(); index++) { + if (base.kind != GDScriptParser::DataType::CLASS) { + push_error(R"(Super type "%s" is not a GDScript. Cannot get nested types.)", p_class); + return ERR_PARSE_ERROR; + } + + // TODO: Extends could use identifier nodes. That way errors can be pointed out properly and it can be used here. + GDScriptParser::IdentifierNode *id = parser->alloc_node(); + id->name = p_class->extends[index]; + + reduce_identifier_from_base(id, &base); + + GDScriptParser::DataType id_type = id->get_datatype(); + if (!id_type.is_set()) { + push_error(vformat(R"(Could not find type "%s" under base "%s".)", id->name, base.to_string()), p_class); + } + + base = id_type; + } + + result = base; + } + + if (!result.is_set()) { + // TODO: More specific error messages. + push_error(vformat(R"(Could not resolve inheritance for class "%s".)", p_class->identifier == nullptr ? "
" : p_class->identifier->name), p_class); + return ERR_PARSE_ERROR; + } + + p_class->base_type = result; + class_type.native_type = result.native_type; + p_class->set_datatype(class_type); + + if (p_recursive) { + for (int i = 0; i < p_class->members.size(); i++) { + if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) { + resolve_inheritance(p_class->members[i].m_class, true); + } + } + } + + return OK; +} + +GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::TypeNode *p_type) { + GDScriptParser::DataType result; + + if (p_type == nullptr) { + result.kind = GDScriptParser::DataType::VARIANT; + return result; + } + + result.type_source = result.ANNOTATED_EXPLICIT; + result.builtin_type = Variant::OBJECT; + + if (p_type->type_chain.empty()) { + // void. + result.kind = GDScriptParser::DataType::BUILTIN; + result.builtin_type = Variant::NIL; + p_type->set_datatype(result); + return result; + } + + StringName first = p_type->type_chain[0]->name; + + if (first == "Variant") { + result.kind = GDScriptParser::DataType::VARIANT; + if (p_type->type_chain.size() > 1) { + push_error(R"("Variant" type don't contain nested types.)", p_type->type_chain[1]); + return GDScriptParser::DataType(); + } + return result; + } + + if (GDScriptParser::get_builtin_type(first) < Variant::VARIANT_MAX) { + // Built-in types. + if (p_type->type_chain.size() > 1) { + push_error(R"(Built-in types don't contain nested types.)", p_type->type_chain[1]); + return GDScriptParser::DataType(); + } + result.kind = GDScriptParser::DataType::BUILTIN; + result.builtin_type = GDScriptParser::get_builtin_type(first); + } else if (class_exists(first)) { + // Native engine classes. + result.kind = GDScriptParser::DataType::NATIVE; + result.native_type = first; + } else if (ScriptServer::is_global_class(first)) { + if (parser->script_path == ScriptServer::get_global_class_path(first)) { + result = parser->head->get_datatype(); + } else { + Ref ref = get_parser_for(ScriptServer::get_global_class_path(first)); + if (ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED) != OK) { + push_error(vformat(R"(Could not parse global class "%s" from "%s".)", first, ScriptServer::get_global_class_path(first)), p_type); + return GDScriptParser::DataType(); + } + result = ref->get_parser()->head->get_datatype(); + } + } else if (ProjectSettings::get_singleton()->has_autoload(first) && ProjectSettings::get_singleton()->get_autoload(first).is_singleton) { + const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(first); + Ref ref = get_parser_for(autoload.path); + if (ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED) != OK) { + push_error(vformat(R"(Could not parse singleton "%s" from "%s".)", first, autoload.path), p_type); + return GDScriptParser::DataType(); + } + result = ref->get_parser()->head->get_datatype(); + } else if (ClassDB::has_enum(get_real_class_name(parser->current_class->base_type.native_type), first)) { + // Native enum in current class. + result = make_native_enum_type(parser->current_class->base_type.native_type, first); + } else { + // Classes in current scope. + GDScriptParser::ClassNode *script_class = parser->current_class; + bool found = false; + while (!found && script_class != nullptr) { + if (script_class->identifier && script_class->identifier->name == first) { + result = script_class->get_datatype(); + found = true; + break; + } + if (script_class->members_indices.has(first)) { + GDScriptParser::ClassNode::Member member = script_class->members[script_class->members_indices[first]]; + switch (member.type) { + case GDScriptParser::ClassNode::Member::CLASS: + result = member.m_class->get_datatype(); + found = true; + break; + case GDScriptParser::ClassNode::Member::ENUM: + result = member.m_enum->get_datatype(); + found = true; + break; + case GDScriptParser::ClassNode::Member::CONSTANT: + if (member.constant->get_datatype().is_meta_type) { + result = member.constant->get_datatype(); + found = true; + break; + } + [[fallthrough]]; + default: + push_error(vformat(R"("%s" is a %s but does not contain a type.)", first, member.get_type_name()), p_type); + return GDScriptParser::DataType(); + } + } + script_class = script_class->outer; + } + } + if (!result.is_set()) { + push_error(vformat(R"("%s" was not found in the current scope.)", first), p_type); + result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type. + return result; + } + + if (p_type->type_chain.size() > 1) { + if (result.kind == GDScriptParser::DataType::CLASS) { + for (int i = 1; i < p_type->type_chain.size(); i++) { + GDScriptParser::DataType base = result; + reduce_identifier_from_base(p_type->type_chain[i], &base); + result = p_type->type_chain[i]->get_datatype(); + if (!result.is_set()) { + push_error(vformat(R"(Could not find type "%s" under base "%s".)", p_type->type_chain[i]->name, base.to_string()), p_type->type_chain[1]); + result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type. + return result; + } else if (!result.is_meta_type) { + push_error(vformat(R"(Member "%s" under base "%s" is not a valid type.)", p_type->type_chain[i]->name, base.to_string()), p_type->type_chain[1]); + result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type. + return result; + } + } + } else if (result.kind == GDScriptParser::DataType::NATIVE) { + // Only enums allowed for native. + if (ClassDB::has_enum(get_real_class_name(result.native_type), p_type->type_chain[1]->name)) { + if (p_type->type_chain.size() > 2) { + push_error(R"(Enums cannot contain nested types.)", p_type->type_chain[2]); + } else { + result = make_native_enum_type(result.native_type, p_type->type_chain[1]->name); + } + } + } else { + push_error(vformat(R"(Could not find nested type "%s" under base "%s".)", p_type->type_chain[1]->name, result.to_string()), p_type->type_chain[1]); + result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type. + return result; + } + } + + p_type->set_datatype(result); + return result; +} + +void GDScriptAnalyzer::resolve_class_interface(GDScriptParser::ClassNode *p_class) { + if (p_class->resolved_interface) { + return; + } + p_class->resolved_interface = true; + + GDScriptParser::ClassNode *previous_class = parser->current_class; + parser->current_class = p_class; + + for (int i = 0; i < p_class->members.size(); i++) { + GDScriptParser::ClassNode::Member member = p_class->members[i]; + + switch (member.type) { + case GDScriptParser::ClassNode::Member::VARIABLE: { + GDScriptParser::DataType datatype; + datatype.kind = GDScriptParser::DataType::VARIANT; + datatype.type_source = GDScriptParser::DataType::UNDETECTED; + + if (member.variable->initializer != nullptr) { + member.variable->set_datatype(datatype); // Allow recursive usage. + reduce_expression(member.variable->initializer); + datatype = member.variable->initializer->get_datatype(); + } + + if (member.variable->datatype_specifier != nullptr) { + datatype = resolve_datatype(member.variable->datatype_specifier); + datatype.is_meta_type = false; + + if (member.variable->initializer != nullptr) { + if (!is_type_compatible(datatype, member.variable->initializer->get_datatype(), true)) { + push_error(vformat(R"(Value of type "%s" cannot be assigned to variable of type "%s".)", member.variable->initializer->get_datatype().to_string(), datatype.to_string()), member.variable->initializer); + } else if (datatype.builtin_type == Variant::INT && member.variable->initializer->get_datatype().builtin_type == Variant::FLOAT) { +#ifdef DEBUG_ENABLED + parser->push_warning(member.variable->initializer, GDScriptWarning::NARROWING_CONVERSION); +#endif + } + if (member.variable->initializer->get_datatype().is_variant()) { + // TODO: Warn unsafe assign. + mark_node_unsafe(member.variable->initializer); + } + } + } else if (member.variable->infer_datatype) { + if (member.variable->initializer == nullptr) { + push_error(vformat(R"(Cannot infer the type of "%s" variable because there's no default value.)", member.variable->identifier->name), member.variable->identifier); + } else if (!datatype.is_set() || datatype.has_no_type()) { + push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value doesn't have a set type.)", member.variable->identifier->name), member.variable->initializer); + } else if (datatype.is_variant()) { + push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is Variant. Use explicit "Variant" type if this is intended.)", member.variable->identifier->name), member.variable->initializer); + } else if (datatype.builtin_type == Variant::NIL) { + push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is "null".)", member.variable->identifier->name), member.variable->initializer); + } + } + + datatype.is_constant = false; + member.variable->set_datatype(datatype); + if (!datatype.has_no_type()) { + // TODO: Move this out into a routine specific to validate annotations. + if (member.variable->export_info.hint == PROPERTY_HINT_TYPE_STRING) { + // @export annotation. + switch (datatype.kind) { + case GDScriptParser::DataType::BUILTIN: + member.variable->export_info.hint_string = Variant::get_type_name(datatype.builtin_type); + break; + case GDScriptParser::DataType::NATIVE: + if (ClassDB::is_parent_class(get_real_class_name(datatype.native_type), "Resource")) { + member.variable->export_info.hint_string = get_real_class_name(datatype.native_type); + } else { + push_error(R"(Export type can only be built-in or a resource.)", member.variable); + } + break; + default: + // TODO: Allow custom user resources. + push_error(R"(Export type can only be built-in or a resource.)", member.variable); + break; + } + } + } + } break; + case GDScriptParser::ClassNode::Member::CONSTANT: { + reduce_expression(member.constant->initializer); + + GDScriptParser::DataType datatype = member.constant->get_datatype(); + if (member.constant->initializer) { + if (!member.constant->initializer->is_constant) { + push_error(R"(Initializer for a constant must be a constant expression.)", member.constant->initializer); + } + + if (member.constant->datatype_specifier != nullptr) { + datatype = resolve_datatype(member.constant->datatype_specifier); + datatype.is_meta_type = false; + + if (!is_type_compatible(datatype, member.constant->initializer->get_datatype(), true)) { + push_error(vformat(R"(Value of type "%s" cannot be initialized to constant of type "%s".)", member.constant->initializer->get_datatype().to_string(), datatype.to_string()), member.constant->initializer); + } else if (datatype.builtin_type == Variant::INT && member.constant->initializer->get_datatype().builtin_type == Variant::FLOAT) { +#ifdef DEBUG_ENABLED + parser->push_warning(member.constant->initializer, GDScriptWarning::NARROWING_CONVERSION); +#endif + } + } + } + datatype.is_constant = true; + + member.constant->set_datatype(datatype); + } break; + case GDScriptParser::ClassNode::Member::SIGNAL: { + for (int j = 0; j < member.signal->parameters.size(); j++) { + GDScriptParser::DataType signal_type = resolve_datatype(member.signal->parameters[j]->datatype_specifier); + signal_type.is_meta_type = false; + member.signal->parameters[j]->set_datatype(signal_type); + } + // TODO: Make MethodInfo from signal. + GDScriptParser::DataType signal_type; + signal_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + signal_type.kind = GDScriptParser::DataType::BUILTIN; + signal_type.builtin_type = Variant::SIGNAL; + + member.signal->set_datatype(signal_type); + } break; + case GDScriptParser::ClassNode::Member::ENUM: { + GDScriptParser::DataType enum_type; + enum_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + enum_type.kind = GDScriptParser::DataType::ENUM; + enum_type.builtin_type = Variant::DICTIONARY; + enum_type.enum_type = member.m_enum->identifier->name; + enum_type.native_type = p_class->fqcn + "." + member.m_enum->identifier->name; + enum_type.is_meta_type = true; + enum_type.is_constant = true; + + for (int j = 0; j < member.m_enum->values.size(); j++) { + enum_type.enum_values[member.m_enum->values[j].identifier->name] = member.m_enum->values[j].value; + } + + member.m_enum->set_datatype(enum_type); + } break; + case GDScriptParser::ClassNode::Member::FUNCTION: + resolve_function_signature(member.function); + break; + case GDScriptParser::ClassNode::Member::ENUM_VALUE: + break; // Nothing to do, type and value set in parser. + case GDScriptParser::ClassNode::Member::CLASS: + break; // Done later. + case GDScriptParser::ClassNode::Member::UNDEFINED: + ERR_PRINT("Trying to resolve undefined member."); + break; + } + } + + // Recurse nested classes. + for (int i = 0; i < p_class->members.size(); i++) { + GDScriptParser::ClassNode::Member member = p_class->members[i]; + if (member.type != GDScriptParser::ClassNode::Member::CLASS) { + continue; + } + + resolve_class_interface(member.m_class); + } + + parser->current_class = previous_class; +} + +void GDScriptAnalyzer::resolve_class_body(GDScriptParser::ClassNode *p_class) { + if (p_class->resolved_body) { + return; + } + p_class->resolved_body = true; + + GDScriptParser::ClassNode *previous_class = parser->current_class; + parser->current_class = p_class; + + // Do functions now. + for (int i = 0; i < p_class->members.size(); i++) { + GDScriptParser::ClassNode::Member member = p_class->members[i]; + if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) { + continue; + } + + resolve_function_body(member.function); + } + + parser->current_class = previous_class; + + // Recurse nested classes. + for (int i = 0; i < p_class->members.size(); i++) { + GDScriptParser::ClassNode::Member member = p_class->members[i]; + if (member.type != GDScriptParser::ClassNode::Member::CLASS) { + continue; + } + + resolve_class_body(member.m_class); + } + + // Check unused variables. + for (int i = 0; i < p_class->members.size(); i++) { + GDScriptParser::ClassNode::Member member = p_class->members[i]; + if (member.type != GDScriptParser::ClassNode::Member::VARIABLE) { + continue; + } +#ifdef DEBUG_ENABLED + if (member.variable->usages == 0 && String(member.variable->identifier->name).begins_with("_")) { + parser->push_warning(member.variable->identifier, GDScriptWarning::UNUSED_PRIVATE_CLASS_VARIABLE, member.variable->identifier->name); + } +#endif + } +} + +void GDScriptAnalyzer::resolve_node(GDScriptParser::Node *p_node) { + ERR_FAIL_COND_MSG(p_node == nullptr, "Trying to resolve type of a null node."); + + switch (p_node->type) { + case GDScriptParser::Node::NONE: + break; // Unreachable. + case GDScriptParser::Node::CLASS: + resolve_class_interface(static_cast(p_node)); + resolve_class_body(static_cast(p_node)); + break; + case GDScriptParser::Node::CONSTANT: + resolve_constant(static_cast(p_node)); + break; + case GDScriptParser::Node::FOR: + resolve_for(static_cast(p_node)); + break; + case GDScriptParser::Node::FUNCTION: + resolve_function_signature(static_cast(p_node)); + resolve_function_body(static_cast(p_node)); + break; + case GDScriptParser::Node::IF: + resolve_if(static_cast(p_node)); + break; + case GDScriptParser::Node::SUITE: + resolve_suite(static_cast(p_node)); + break; + case GDScriptParser::Node::VARIABLE: + resolve_variable(static_cast(p_node)); + break; + case GDScriptParser::Node::WHILE: + resolve_while(static_cast(p_node)); + break; + case GDScriptParser::Node::ANNOTATION: + resolve_annotation(static_cast(p_node)); + break; + case GDScriptParser::Node::ASSERT: + resolve_assert(static_cast(p_node)); + break; + case GDScriptParser::Node::MATCH: + resolve_match(static_cast(p_node)); + break; + case GDScriptParser::Node::MATCH_BRANCH: + resolve_match_branch(static_cast(p_node), nullptr); + break; + case GDScriptParser::Node::PARAMETER: + resolve_pararameter(static_cast(p_node)); + break; + case GDScriptParser::Node::PATTERN: + resolve_match_pattern(static_cast(p_node), nullptr); + break; + case GDScriptParser::Node::RETURN: + resolve_return(static_cast(p_node)); + break; + case GDScriptParser::Node::TYPE: + resolve_datatype(static_cast(p_node)); + break; + // Resolving expression is the same as reducing them. + case GDScriptParser::Node::ARRAY: + case GDScriptParser::Node::ASSIGNMENT: + case GDScriptParser::Node::AWAIT: + case GDScriptParser::Node::BINARY_OPERATOR: + case GDScriptParser::Node::CALL: + case GDScriptParser::Node::CAST: + case GDScriptParser::Node::DICTIONARY: + case GDScriptParser::Node::GET_NODE: + case GDScriptParser::Node::IDENTIFIER: + case GDScriptParser::Node::LITERAL: + case GDScriptParser::Node::PRELOAD: + case GDScriptParser::Node::SELF: + case GDScriptParser::Node::SUBSCRIPT: + case GDScriptParser::Node::TERNARY_OPERATOR: + case GDScriptParser::Node::UNARY_OPERATOR: + reduce_expression(static_cast(p_node)); + break; + case GDScriptParser::Node::BREAK: + case GDScriptParser::Node::BREAKPOINT: + case GDScriptParser::Node::CONTINUE: + case GDScriptParser::Node::ENUM: + case GDScriptParser::Node::PASS: + case GDScriptParser::Node::SIGNAL: + // Nothing to do. + break; + } +} + +void GDScriptAnalyzer::resolve_annotation(GDScriptParser::AnnotationNode *p_annotation) { + // TODO: Add second validation function for annotations, so they can use checked types. +} + +void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode *p_function) { + if (p_function->resolved_signature) { + return; + } + p_function->resolved_signature = true; + + GDScriptParser::FunctionNode *previous_function = parser->current_function; + parser->current_function = p_function; + + for (int i = 0; i < p_function->parameters.size(); i++) { + resolve_pararameter(p_function->parameters[i]); +#ifdef DEBUG_ENABLED + if (p_function->parameters[i]->usages == 0 && !String(p_function->parameters[i]->identifier->name).begins_with("_")) { + parser->push_warning(p_function->parameters[i]->identifier, GDScriptWarning::UNUSED_PARAMETER, p_function->identifier->name, p_function->parameters[i]->identifier->name); + } + is_shadowing(p_function->parameters[i]->identifier, "function parameter"); +#endif + } + + if (p_function->identifier->name == "_init") { + // Constructor. + GDScriptParser::DataType return_type = parser->current_class->get_datatype(); + return_type.is_meta_type = false; + p_function->set_datatype(return_type); + if (p_function->return_type) { + push_error("Constructor cannot have an explicit return type.", p_function->return_type); + } + } else { + GDScriptParser::DataType return_type = resolve_datatype(p_function->return_type); + p_function->set_datatype(return_type); + } + + parser->current_function = previous_function; +} + +void GDScriptAnalyzer::resolve_function_body(GDScriptParser::FunctionNode *p_function) { + if (p_function->resolved_body) { + return; + } + p_function->resolved_body = true; + + GDScriptParser::FunctionNode *previous_function = parser->current_function; + parser->current_function = p_function; + + resolve_suite(p_function->body); + + GDScriptParser::DataType return_type = p_function->body->get_datatype(); + + if (p_function->get_datatype().has_no_type() && return_type.is_set()) { + // Use the suite inferred type if return isn't explicitly set. + return_type.type_source = GDScriptParser::DataType::INFERRED; + p_function->set_datatype(p_function->body->get_datatype()); + } else if (p_function->get_datatype().is_hard_type() && (p_function->get_datatype().kind != GDScriptParser::DataType::BUILTIN || p_function->get_datatype().builtin_type != Variant::NIL)) { + if (!p_function->body->has_return && p_function->identifier->name != GDScriptLanguage::get_singleton()->strings._init) { + push_error(R"(Not all code paths return a value.)", p_function); + } + } + + parser->current_function = previous_function; +} + +void GDScriptAnalyzer::decide_suite_type(GDScriptParser::Node *p_suite, GDScriptParser::Node *p_statement) { + if (p_statement == nullptr) { + return; + } + switch (p_statement->type) { + case GDScriptParser::Node::IF: + case GDScriptParser::Node::FOR: + case GDScriptParser::Node::MATCH: + case GDScriptParser::Node::PATTERN: + case GDScriptParser::Node::RETURN: + case GDScriptParser::Node::WHILE: + // Use return or nested suite type as this suite type. + if (p_suite->get_datatype().is_set() && (p_suite->get_datatype() != p_statement->get_datatype())) { + // Mixed types. + // TODO: This could use the common supertype instead. + p_suite->datatype.kind = GDScriptParser::DataType::VARIANT; + p_suite->datatype.type_source = GDScriptParser::DataType::UNDETECTED; + } else { + p_suite->set_datatype(p_statement->get_datatype()); + } + break; + default: + break; + } +} + +void GDScriptAnalyzer::resolve_suite(GDScriptParser::SuiteNode *p_suite) { + for (int i = 0; i < p_suite->statements.size(); i++) { + GDScriptParser::Node *stmt = p_suite->statements[i]; + resolve_node(stmt); + decide_suite_type(p_suite, stmt); + } +} + +void GDScriptAnalyzer::resolve_if(GDScriptParser::IfNode *p_if) { + reduce_expression(p_if->condition); + + resolve_suite(p_if->true_block); + p_if->set_datatype(p_if->true_block->get_datatype()); + + if (p_if->false_block != nullptr) { + resolve_suite(p_if->false_block); + decide_suite_type(p_if, p_if->false_block); + } +} + +void GDScriptAnalyzer::resolve_for(GDScriptParser::ForNode *p_for) { + bool list_resolved = false; + + // Optimize constant range() call to not allocate an array. + // Use int, Vector2, Vector3 instead, which also can be used as range iterators. + if (p_for->list && p_for->list->type == GDScriptParser::Node::CALL) { + GDScriptParser::CallNode *call = static_cast(p_for->list); + if (call->callee->type == GDScriptParser::Node::IDENTIFIER) { + GDScriptParser::IdentifierNode *callee = static_cast(call->callee); + if (callee->name == "range") { + list_resolved = true; + if (call->arguments.size() < 1) { + push_error(R"*(Invalid call for "range()" function. Expected at least 1 argument, none given.)*", call->callee); + } else if (call->arguments.size() > 3) { + push_error(vformat(R"*(Invalid call for "range()" function. Expected at most 3 arguments, %d given.)*", call->arguments.size()), call->callee); + } else { + // Now we can optimize it. + bool all_is_constant = true; + Vector args; + args.resize(call->arguments.size()); + for (int i = 0; i < call->arguments.size(); i++) { + reduce_expression(call->arguments[i]); + + if (!call->arguments[i]->is_constant) { + all_is_constant = false; + } else { + args.write[i] = call->arguments[i]->reduced_value; + } + + GDScriptParser::DataType arg_type = call->arguments[i]->get_datatype(); + if (arg_type.kind != GDScriptParser::DataType::BUILTIN) { + all_is_constant = false; + push_error(vformat(R"*(Invalid argument for "range()" call. Argument %d should be int or float but "%s" was given.)*", i + 1, arg_type.to_string()), call->arguments[i]); + } else if (arg_type.builtin_type != Variant::INT && arg_type.builtin_type != Variant::FLOAT) { + all_is_constant = false; + push_error(vformat(R"*(Invalid argument for "range()" call. Argument %d should be int or float but "%s" was given.)*", i + 1, arg_type.to_string()), call->arguments[i]); + } + } + + Variant reduced; + + if (all_is_constant) { + switch (args.size()) { + case 1: + reduced = args[0]; + break; + case 2: + reduced = Vector2i(args[0], args[1]); + break; + case 3: + reduced = Vector3i(args[0], args[1], args[2]); + break; + } + p_for->list->is_constant = true; + p_for->list->reduced_value = reduced; + } + } + + GDScriptParser::DataType list_type; + list_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + list_type.kind = GDScriptParser::DataType::BUILTIN; + list_type.builtin_type = Variant::ARRAY; + p_for->list->set_datatype(list_type); + } + } + } + + if (!list_resolved) { + resolve_node(p_for->list); + } + + // TODO: If list is a typed array, the variable should be an element. + // Also applicable for constant range() (so variable is int or float). + + resolve_suite(p_for->loop); + p_for->set_datatype(p_for->loop->get_datatype()); +#ifdef DEBUG_ENABLED + if (p_for->variable) { + is_shadowing(p_for->variable, R"("for" iterator variable)"); + } +#endif +} + +void GDScriptAnalyzer::resolve_while(GDScriptParser::WhileNode *p_while) { + resolve_node(p_while->condition); + + resolve_suite(p_while->loop); + p_while->set_datatype(p_while->loop->get_datatype()); +} + +void GDScriptAnalyzer::resolve_variable(GDScriptParser::VariableNode *p_variable) { + GDScriptParser::DataType type; + type.kind = GDScriptParser::DataType::VARIANT; // By default. + + if (p_variable->initializer != nullptr) { + reduce_expression(p_variable->initializer); + type = p_variable->initializer->get_datatype(); + + if (p_variable->infer_datatype) { + type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED; + + if (type.has_no_type()) { + push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value does not have a set type.)", p_variable->identifier->name), p_variable->initializer); + } else if (type.is_variant()) { + push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value is a variant. Use explicit "Variant" type if this is intended.)", p_variable->identifier->name), p_variable->initializer); + } else if (type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) { + push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value is "null".)", p_variable->identifier->name), p_variable->initializer); + } + } else { + type.type_source = GDScriptParser::DataType::INFERRED; + } +#ifdef DEBUG_ENABLED + if (p_variable->initializer->type == GDScriptParser::Node::CALL && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) { + parser->push_warning(p_variable->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast(p_variable->initializer)->function_name); + } +#endif + } + + if (p_variable->datatype_specifier != nullptr) { + type = resolve_datatype(p_variable->datatype_specifier); + type.is_meta_type = false; + + if (p_variable->initializer != nullptr) { + if (!is_type_compatible(type, p_variable->initializer->get_datatype(), true)) { + push_error(vformat(R"(Value of type "%s" cannot be assigned to variable of type "%s".)", p_variable->initializer->get_datatype().to_string(), type.to_string()), p_variable->initializer); +#ifdef DEBUG_ENABLED + } else if (type.builtin_type == Variant::INT && p_variable->initializer->get_datatype().builtin_type == Variant::FLOAT) { + parser->push_warning(p_variable->initializer, GDScriptWarning::NARROWING_CONVERSION); +#endif + } + if (p_variable->initializer->get_datatype().is_variant()) { + // TODO: Warn unsafe assign. + mark_node_unsafe(p_variable->initializer); + } + } + } else if (p_variable->infer_datatype) { + if (type.has_no_type()) { + push_error(vformat(R"(Cannot infer the type of variable "%s" because the initial value doesn't have a set type.)", p_variable->identifier->name), p_variable->identifier); + } + type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED; + } + + type.is_constant = false; + p_variable->set_datatype(type); + +#ifdef DEBUG_ENABLED + if (p_variable->usages == 0 && !String(p_variable->identifier->name).begins_with("_")) { + parser->push_warning(p_variable, GDScriptWarning::UNUSED_VARIABLE, p_variable->identifier->name); + } else if (p_variable->assignments == 0) { + parser->push_warning(p_variable, GDScriptWarning::UNASSIGNED_VARIABLE, p_variable->identifier->name); + } + + is_shadowing(p_variable->identifier, "variable"); +#endif +} + +void GDScriptAnalyzer::resolve_constant(GDScriptParser::ConstantNode *p_constant) { + GDScriptParser::DataType type; + + reduce_expression(p_constant->initializer); + + if (!p_constant->initializer->is_constant) { + push_error(vformat(R"(Assigned value for constant "%s" isn't a constant expression.)", p_constant->identifier->name), p_constant->initializer); + } + + type = p_constant->initializer->get_datatype(); + +#ifdef DEBUG_ENABLED + if (p_constant->initializer->type == GDScriptParser::Node::CALL && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) { + parser->push_warning(p_constant->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast(p_constant->initializer)->function_name); + } +#endif + + if (p_constant->datatype_specifier != nullptr) { + GDScriptParser::DataType explicit_type = resolve_datatype(p_constant->datatype_specifier); + explicit_type.is_meta_type = false; + if (!is_type_compatible(explicit_type, type)) { + push_error(vformat(R"(Assigned value for constant "%s" has type %s which is not compatible with defined type %s.)", p_constant->identifier->name, type.to_string(), explicit_type.to_string()), p_constant->initializer); +#ifdef DEBUG_ENABLED + } else if (explicit_type.builtin_type == Variant::INT && type.builtin_type == Variant::FLOAT) { + parser->push_warning(p_constant->initializer, GDScriptWarning::NARROWING_CONVERSION); +#endif + } + type = explicit_type; + } else if (p_constant->infer_datatype) { + if (type.has_no_type()) { + push_error(vformat(R"(Cannot infer the type of constant "%s" because the initial value doesn't have a set type.)", p_constant->identifier->name), p_constant->identifier); + } + type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED; + } + + type.is_constant = true; + p_constant->set_datatype(type); + +#ifdef DEBUG_ENABLED + if (p_constant->usages == 0) { + parser->push_warning(p_constant, GDScriptWarning::UNUSED_LOCAL_CONSTANT, p_constant->identifier->name); + } + + is_shadowing(p_constant->identifier, "constant"); +#endif +} + +void GDScriptAnalyzer::resolve_assert(GDScriptParser::AssertNode *p_assert) { + reduce_expression(p_assert->condition); + if (p_assert->message != nullptr) { + reduce_literal(p_assert->message); + } + + p_assert->set_datatype(p_assert->condition->get_datatype()); + +#ifdef DEBUG_ENABLED + if (p_assert->condition->is_constant) { + if (p_assert->condition->reduced_value.booleanize()) { + parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_TRUE); + } else { + parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_FALSE); + } + } +#endif +} + +void GDScriptAnalyzer::resolve_match(GDScriptParser::MatchNode *p_match) { + reduce_expression(p_match->test); + + for (int i = 0; i < p_match->branches.size(); i++) { + resolve_match_branch(p_match->branches[i], p_match->test); + + decide_suite_type(p_match, p_match->branches[i]); + } +} + +void GDScriptAnalyzer::resolve_match_branch(GDScriptParser::MatchBranchNode *p_match_branch, GDScriptParser::ExpressionNode *p_match_test) { + for (int i = 0; i < p_match_branch->patterns.size(); i++) { + resolve_match_pattern(p_match_branch->patterns[i], p_match_test); + } + + resolve_suite(p_match_branch->block); + + decide_suite_type(p_match_branch, p_match_branch->block); +} + +void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_match_pattern, GDScriptParser::ExpressionNode *p_match_test) { + if (p_match_pattern == nullptr) { + return; + } + + GDScriptParser::DataType result; + + switch (p_match_pattern->pattern_type) { + case GDScriptParser::PatternNode::PT_LITERAL: + if (p_match_pattern->literal) { + reduce_literal(p_match_pattern->literal); + result = p_match_pattern->literal->get_datatype(); + } + break; + case GDScriptParser::PatternNode::PT_EXPRESSION: + if (p_match_pattern->expression) { + reduce_expression(p_match_pattern->expression); + if (!p_match_pattern->expression->is_constant) { + push_error(R"(Expression in match pattern must be a constant.)", p_match_pattern->expression); + } + result = p_match_pattern->expression->get_datatype(); + } + break; + case GDScriptParser::PatternNode::PT_BIND: + if (p_match_test != nullptr) { + result = p_match_test->get_datatype(); + } else { + result.kind = GDScriptParser::DataType::VARIANT; + } + p_match_pattern->bind->set_datatype(result); +#ifdef DEBUG_ENABLED + is_shadowing(p_match_pattern->bind, "pattern bind"); + if (p_match_pattern->bind->usages == 0) { + parser->push_warning(p_match_pattern->bind, GDScriptWarning::UNASSIGNED_VARIABLE, p_match_pattern->bind->name); + } +#endif + break; + case GDScriptParser::PatternNode::PT_ARRAY: + for (int i = 0; i < p_match_pattern->array.size(); i++) { + resolve_match_pattern(p_match_pattern->array[i], nullptr); + decide_suite_type(p_match_pattern, p_match_pattern->array[i]); + } + result = p_match_pattern->get_datatype(); + break; + case GDScriptParser::PatternNode::PT_DICTIONARY: + for (int i = 0; i < p_match_pattern->dictionary.size(); i++) { + if (p_match_pattern->dictionary[i].key) { + reduce_expression(p_match_pattern->dictionary[i].key); + if (!p_match_pattern->dictionary[i].key->is_constant) { + push_error(R"(Expression in dictionary pattern key must be a constant.)", p_match_pattern->expression); + } + } + + if (p_match_pattern->dictionary[i].value_pattern) { + resolve_match_pattern(p_match_pattern->dictionary[i].value_pattern, nullptr); + decide_suite_type(p_match_pattern, p_match_pattern->dictionary[i].value_pattern); + } + } + result = p_match_pattern->get_datatype(); + break; + case GDScriptParser::PatternNode::PT_WILDCARD: + case GDScriptParser::PatternNode::PT_REST: + result.kind = GDScriptParser::DataType::VARIANT; + break; + } + + p_match_pattern->set_datatype(result); +} + +void GDScriptAnalyzer::resolve_pararameter(GDScriptParser::ParameterNode *p_parameter) { + GDScriptParser::DataType result; + result.kind = GDScriptParser::DataType::VARIANT; + + if (p_parameter->default_value != nullptr) { + reduce_expression(p_parameter->default_value); + result = p_parameter->default_value->get_datatype(); + result.type_source = GDScriptParser::DataType::INFERRED; + } + + if (p_parameter->datatype_specifier != nullptr) { + resolve_datatype(p_parameter->datatype_specifier); + result = p_parameter->datatype_specifier->get_datatype(); + result.is_meta_type = false; + + if (p_parameter->default_value != nullptr) { + if (!is_type_compatible(result, p_parameter->default_value->get_datatype())) { + push_error(vformat(R"(Type of default value for parameter "%s" (%s) is not compatible with paremeter type (%s).)", p_parameter->identifier->name, p_parameter->default_value->get_datatype().to_string(), p_parameter->datatype_specifier->get_datatype().to_string()), p_parameter->default_value); + } else if (p_parameter->default_value->get_datatype().is_variant()) { + mark_node_unsafe(p_parameter); + } + } + } + + p_parameter->set_datatype(result); +} + +void GDScriptAnalyzer::resolve_return(GDScriptParser::ReturnNode *p_return) { + GDScriptParser::DataType result; + + if (p_return->return_value != nullptr) { + reduce_expression(p_return->return_value); + result = p_return->return_value->get_datatype(); + } else { + // Return type is null by default. + result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + result.kind = GDScriptParser::DataType::BUILTIN; + result.builtin_type = Variant::NIL; + result.is_constant = true; + } + + GDScriptParser::DataType function_type = parser->current_function->get_datatype(); + function_type.is_meta_type = false; + if (function_type.is_hard_type()) { + if (!is_type_compatible(function_type, result)) { + // Try other way. Okay but not safe. + if (!is_type_compatible(result, function_type)) { + push_error(vformat(R"(Cannot return value of type "%s" because the function return type is "%s".)", result.to_string(), function_type.to_string()), p_return); + } else { + // TODO: Add warning. + mark_node_unsafe(p_return); + } +#ifdef DEBUG_ENABLED + } else if (function_type.builtin_type == Variant::INT && result.builtin_type == Variant::FLOAT) { + parser->push_warning(p_return, GDScriptWarning::NARROWING_CONVERSION); + } else if (result.is_variant()) { + mark_node_unsafe(p_return); +#endif + } + } + + p_return->set_datatype(result); +} + +void GDScriptAnalyzer::reduce_expression(GDScriptParser::ExpressionNode *p_expression) { + // This one makes some magic happen. + + if (p_expression == nullptr) { + return; + } + + if (p_expression->reduced) { + // Don't do this more than once. + return; + } + + p_expression->reduced = true; + + switch (p_expression->type) { + case GDScriptParser::Node::ARRAY: + reduce_array(static_cast(p_expression)); + break; + case GDScriptParser::Node::ASSIGNMENT: + reduce_assignment(static_cast(p_expression)); + break; + case GDScriptParser::Node::AWAIT: + reduce_await(static_cast(p_expression)); + break; + case GDScriptParser::Node::BINARY_OPERATOR: + reduce_binary_op(static_cast(p_expression)); + break; + case GDScriptParser::Node::CALL: + reduce_call(static_cast(p_expression)); + break; + case GDScriptParser::Node::CAST: + reduce_cast(static_cast(p_expression)); + break; + case GDScriptParser::Node::DICTIONARY: + reduce_dictionary(static_cast(p_expression)); + break; + case GDScriptParser::Node::GET_NODE: + reduce_get_node(static_cast(p_expression)); + break; + case GDScriptParser::Node::IDENTIFIER: + reduce_identifier(static_cast(p_expression)); + break; + case GDScriptParser::Node::LITERAL: + reduce_literal(static_cast(p_expression)); + break; + case GDScriptParser::Node::PRELOAD: + reduce_preload(static_cast(p_expression)); + break; + case GDScriptParser::Node::SELF: + reduce_self(static_cast(p_expression)); + break; + case GDScriptParser::Node::SUBSCRIPT: + reduce_subscript(static_cast(p_expression)); + break; + case GDScriptParser::Node::TERNARY_OPERATOR: + reduce_ternary_op(static_cast(p_expression)); + break; + case GDScriptParser::Node::UNARY_OPERATOR: + reduce_unary_op(static_cast(p_expression)); + break; + // Non-expressions. Here only to make sure new nodes aren't forgotten. + case GDScriptParser::Node::NONE: + case GDScriptParser::Node::ANNOTATION: + case GDScriptParser::Node::ASSERT: + case GDScriptParser::Node::BREAK: + case GDScriptParser::Node::BREAKPOINT: + case GDScriptParser::Node::CLASS: + case GDScriptParser::Node::CONSTANT: + case GDScriptParser::Node::CONTINUE: + case GDScriptParser::Node::ENUM: + case GDScriptParser::Node::FOR: + case GDScriptParser::Node::FUNCTION: + case GDScriptParser::Node::IF: + case GDScriptParser::Node::MATCH: + case GDScriptParser::Node::MATCH_BRANCH: + case GDScriptParser::Node::PARAMETER: + case GDScriptParser::Node::PASS: + case GDScriptParser::Node::PATTERN: + case GDScriptParser::Node::RETURN: + case GDScriptParser::Node::SIGNAL: + case GDScriptParser::Node::SUITE: + case GDScriptParser::Node::TYPE: + case GDScriptParser::Node::VARIABLE: + case GDScriptParser::Node::WHILE: + ERR_FAIL_MSG("Reaching unreachable case"); + } +} + +void GDScriptAnalyzer::reduce_array(GDScriptParser::ArrayNode *p_array) { + bool all_is_constant = true; + + for (int i = 0; i < p_array->elements.size(); i++) { + GDScriptParser::ExpressionNode *element = p_array->elements[i]; + reduce_expression(element); + all_is_constant = all_is_constant && element->is_constant; + } + + if (all_is_constant) { + Array array; + array.resize(p_array->elements.size()); + for (int i = 0; i < p_array->elements.size(); i++) { + array[i] = p_array->elements[i]->reduced_value; + } + p_array->is_constant = true; + p_array->reduced_value = array; + } + + // It's array in any case. + GDScriptParser::DataType arr_type; + arr_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + arr_type.kind = GDScriptParser::DataType::BUILTIN; + arr_type.builtin_type = Variant::ARRAY; + arr_type.is_constant = true; + + p_array->set_datatype(arr_type); +} + +void GDScriptAnalyzer::reduce_assignment(GDScriptParser::AssignmentNode *p_assignment) { + reduce_expression(p_assignment->assignee); + reduce_expression(p_assignment->assigned_value); + + if (p_assignment->assigned_value == nullptr || p_assignment->assignee == nullptr) { + return; + } + + if (p_assignment->assignee->get_datatype().is_constant) { + push_error("Cannot assign a new value to a constant.", p_assignment->assignee); + } + + if (!is_type_compatible(p_assignment->assignee->get_datatype(), p_assignment->assigned_value->get_datatype(), true)) { + if (p_assignment->assignee->get_datatype().is_hard_type()) { + push_error(vformat(R"(Cannot assign a value of type "%s" to a target of type "%s".)", p_assignment->assigned_value->get_datatype().to_string(), p_assignment->assignee->get_datatype().to_string()), p_assignment->assigned_value); + } else { + // TODO: Warning in this case. + } + } + + if (p_assignment->assignee->get_datatype().has_no_type() || p_assignment->assigned_value->get_datatype().is_variant()) { + mark_node_unsafe(p_assignment); + } + + if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) { + // Change source type so it's not wrongly detected later. + GDScriptParser::IdentifierNode *identifier = static_cast(p_assignment->assignee); + + switch (identifier->source) { + case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: { + GDScriptParser::DataType id_type = identifier->variable_source->get_datatype(); + if (!id_type.is_hard_type()) { + id_type.kind = GDScriptParser::DataType::VARIANT; + id_type.type_source = GDScriptParser::DataType::UNDETECTED; + identifier->variable_source->set_datatype(id_type); + } + } break; + case GDScriptParser::IdentifierNode::LOCAL_VARIABLE: { + GDScriptParser::DataType id_type = identifier->variable_source->get_datatype(); + if (!id_type.is_hard_type()) { + id_type = p_assignment->assigned_value->get_datatype(); + id_type.type_source = GDScriptParser::DataType::INFERRED; + id_type.is_constant = false; + identifier->variable_source->set_datatype(id_type); + } + } break; + case GDScriptParser::IdentifierNode::LOCAL_ITERATOR: { + GDScriptParser::DataType id_type = identifier->bind_source->get_datatype(); + if (!id_type.is_hard_type()) { + id_type = p_assignment->assigned_value->get_datatype(); + id_type.type_source = GDScriptParser::DataType::INFERRED; + id_type.is_constant = false; + identifier->variable_source->set_datatype(id_type); + } + } break; + default: + // Nothing to do. + break; + } + } + + GDScriptParser::DataType assignee_type = p_assignment->assignee->get_datatype(); + GDScriptParser::DataType assigned_type = p_assignment->assigned_value->get_datatype(); +#ifdef DEBUG_ENABLED + if (p_assignment->assigned_value->type == GDScriptParser::Node::CALL && assigned_type.kind == GDScriptParser::DataType::BUILTIN && assigned_type.builtin_type == Variant::NIL) { + parser->push_warning(p_assignment->assigned_value, GDScriptWarning::VOID_ASSIGNMENT, static_cast(p_assignment->assigned_value)->function_name); + } else if (assignee_type.is_hard_type() && assignee_type.builtin_type == Variant::INT && assigned_type.builtin_type == Variant::FLOAT) { + parser->push_warning(p_assignment->assigned_value, GDScriptWarning::NARROWING_CONVERSION); + } +#endif +} + +void GDScriptAnalyzer::reduce_await(GDScriptParser::AwaitNode *p_await) { + if (p_await->to_await->type == GDScriptParser::Node::CALL) { + reduce_call(static_cast(p_await->to_await), true); + } else { + reduce_expression(p_await->to_await); + } + + p_await->is_constant = p_await->to_await->is_constant; + p_await->reduced_value = p_await->to_await->reduced_value; + + GDScriptParser::DataType awaiting_type = p_await->to_await->get_datatype(); + + p_await->set_datatype(awaiting_type); + +#ifdef DEBUG_ENABLED + if (!awaiting_type.is_coroutine && awaiting_type.builtin_type != Variant::SIGNAL) { + parser->push_warning(p_await, GDScriptWarning::REDUNDANT_AWAIT); + } +#endif +} + +void GDScriptAnalyzer::reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op) { + reduce_expression(p_binary_op->left_operand); + + if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST && p_binary_op->right_operand && p_binary_op->right_operand->type == GDScriptParser::Node::IDENTIFIER) { + reduce_identifier(static_cast(p_binary_op->right_operand), true); + } else { + reduce_expression(p_binary_op->right_operand); + } + // TODO: Right operand must be a valid type with the `is` operator. Need to check here. + + GDScriptParser::DataType left_type; + if (p_binary_op->left_operand) { + left_type = p_binary_op->left_operand->get_datatype(); + } + GDScriptParser::DataType right_type; + if (p_binary_op->right_operand) { + right_type = p_binary_op->right_operand->get_datatype(); + } + + if (!left_type.is_set() || !right_type.is_set()) { + return; + } + +#ifdef DEBUG_ENABLED + if (p_binary_op->variant_op == Variant::OP_DIVIDE && left_type.builtin_type == Variant::INT && right_type.builtin_type == Variant::INT) { + parser->push_warning(p_binary_op, GDScriptWarning::INTEGER_DIVISION); + } +#endif + + if (p_binary_op->left_operand->is_constant && p_binary_op->right_operand->is_constant) { + p_binary_op->is_constant = true; + if (p_binary_op->variant_op < Variant::OP_MAX) { + p_binary_op->reduced_value = Variant::evaluate(p_binary_op->variant_op, p_binary_op->left_operand->reduced_value, p_binary_op->right_operand->reduced_value); + } else { + if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) { + GDScriptParser::DataType test_type = right_type; + test_type.is_meta_type = false; + + if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) { + push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)"), p_binary_op->left_operand); + p_binary_op->reduced_value = false; + } else { + p_binary_op->reduced_value = true; + } + } else { + ERR_PRINT("Parser bug: unknown binary operation."); + } + } + p_binary_op->set_datatype(type_from_variant(p_binary_op->reduced_value)); + + return; + } + + GDScriptParser::DataType result; + + if (left_type.is_variant() || right_type.is_variant()) { + // Cannot infer type because one operand can be anything. + result.kind = GDScriptParser::DataType::VARIANT; + mark_node_unsafe(p_binary_op); + } else { + if (p_binary_op->variant_op < Variant::OP_MAX) { + bool valid = false; + result = get_operation_type(p_binary_op->variant_op, p_binary_op->left_operand->get_datatype(), right_type, valid); + + if (!valid) { + push_error(vformat(R"(Invalid operands "%s" and "%s" for "%s" operator.)", p_binary_op->left_operand->get_datatype().to_string(), right_type.to_string(), Variant::get_operator_name(p_binary_op->variant_op)), p_binary_op); + } + } else { + if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) { + GDScriptParser::DataType test_type = right_type; + test_type.is_meta_type = false; + + if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) { + // Test reverse as well to consider for subtypes. + if (!is_type_compatible(p_binary_op->left_operand->get_datatype(), test_type, false)) { + if (p_binary_op->left_operand->get_datatype().is_hard_type()) { + push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)", p_binary_op->left_operand->get_datatype().to_string(), test_type.to_string()), p_binary_op->left_operand); + } else { + // TODO: Warning. + mark_node_unsafe(p_binary_op); + } + } + } + + // "is" operator is always a boolean anyway. + result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + result.kind = GDScriptParser::DataType::BUILTIN; + result.builtin_type = Variant::BOOL; + } else { + ERR_PRINT("Parser bug: unknown binary operation."); + } + } + } + + p_binary_op->set_datatype(result); +} + +void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool is_await) { + bool all_is_constant = true; + for (int i = 0; i < p_call->arguments.size(); i++) { + reduce_expression(p_call->arguments[i]); + all_is_constant = all_is_constant && p_call->arguments[i]->is_constant; + } + + GDScriptParser::DataType call_type; + + if (!p_call->is_super && p_call->callee->type == GDScriptParser::Node::IDENTIFIER) { + // Call to name directly. + StringName function_name = p_call->function_name; + Variant::Type builtin_type = GDScriptParser::get_builtin_type(function_name); + GDScriptFunctions::Function builtin_function = GDScriptParser::get_builtin_function(function_name); + + if (builtin_type < Variant::VARIANT_MAX) { + // Is a builtin constructor. + call_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + call_type.kind = GDScriptParser::DataType::BUILTIN; + call_type.builtin_type = builtin_type; + + if (builtin_type == Variant::OBJECT) { + call_type.kind = GDScriptParser::DataType::NATIVE; + call_type.native_type = function_name; // "Object". + } + + if (all_is_constant) { + // Construct here. + Vector args; + for (int i = 0; i < p_call->arguments.size(); i++) { + args.push_back(&(p_call->arguments[i]->reduced_value)); + } + + Callable::CallError err; + Variant value = Variant::construct(builtin_type, (const Variant **)args.ptr(), args.size(), err); + + switch (err.error) { + case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: + push_error(vformat(R"(Invalid argument for %s constructor: argument %d should be %s but is %s.)", Variant::get_type_name(builtin_type), err.argument + 1, + Variant::get_type_name(Variant::Type(err.expected)), p_call->arguments[err.argument]->get_datatype().to_string()), + p_call->arguments[err.argument]); + break; + case Callable::CallError::CALL_ERROR_INVALID_METHOD: { + String signature = Variant::get_type_name(builtin_type) + "("; + for (int i = 0; i < p_call->arguments.size(); i++) { + if (i > 0) { + signature += ", "; + } + signature += p_call->arguments[i]->get_datatype().to_string(); + } + push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call->callee); + } break; + case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: + push_error(vformat(R"(Too many arguments for %s constructor. Received %d but expected %d.)", Variant::get_type_name(builtin_type), p_call->arguments.size(), err.expected), p_call); + break; + case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: + push_error(vformat(R"(Too few arguments for %s constructor. Received %d but expected %d.)", Variant::get_type_name(builtin_type), p_call->arguments.size(), err.expected), p_call); + break; + case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: + break; // Can't happen in a builtin constructor. + case Callable::CallError::CALL_OK: + p_call->is_constant = true; + p_call->reduced_value = value; + break; + } + } else { + // TODO: Check constructors without constants. + + // If there's one argument, try to use copy constructor (those aren't explicitly defined). + if (p_call->arguments.size() == 1) { + GDScriptParser::DataType arg_type = p_call->arguments[0]->get_datatype(); + if (arg_type.is_variant()) { + mark_node_unsafe(p_call->arguments[0]); + } else { + if (arg_type.kind == GDScriptParser::DataType::BUILTIN && arg_type.builtin_type == builtin_type) { + // Okay. + p_call->set_datatype(call_type); + return; + } + } + } + List constructors; + Variant::get_constructor_list(builtin_type, &constructors); + bool match = false; + + for (const List::Element *E = constructors.front(); E != nullptr; E = E->next()) { + const MethodInfo &info = E->get(); + + if (p_call->arguments.size() < info.arguments.size() - info.default_arguments.size()) { + continue; + } + if (p_call->arguments.size() > info.arguments.size()) { + continue; + } + + bool types_match = true; + + for (int i = 0; i < p_call->arguments.size(); i++) { + GDScriptParser::DataType par_type = type_from_property(info.arguments[i]); + + if (!is_type_compatible(par_type, p_call->arguments[i]->get_datatype())) { + types_match = false; + break; +#ifdef DEBUG_ENABLED + } else { + if (par_type.builtin_type == Variant::INT && p_call->arguments[i]->get_datatype().builtin_type == Variant::FLOAT) { + parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name); + } +#endif + } + } + + if (types_match) { + match = true; + call_type = type_from_property(info.return_val); + break; + } + } + + if (!match) { + String signature = Variant::get_type_name(builtin_type) + "("; + for (int i = 0; i < p_call->arguments.size(); i++) { + if (i > 0) { + signature += ", "; + } + signature += p_call->arguments[i]->get_datatype().to_string(); + } + push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call); + } + } + p_call->set_datatype(call_type); + return; + } else if (builtin_function < GDScriptFunctions::FUNC_MAX) { + MethodInfo function_info = GDScriptFunctions::get_info(builtin_function); + + if (all_is_constant && GDScriptFunctions::is_deterministic(builtin_function)) { + // Can call on compilation. + Vector args; + for (int i = 0; i < p_call->arguments.size(); i++) { + args.push_back(&(p_call->arguments[i]->reduced_value)); + } + + Variant value; + Callable::CallError err; + GDScriptFunctions::call(builtin_function, (const Variant **)args.ptr(), args.size(), value, err); + + switch (err.error) { + case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: { + PropertyInfo wrong_arg = function_info.arguments[err.argument]; + push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", GDScriptFunctions::get_func_name(builtin_function), err.argument + 1, + type_from_property(wrong_arg).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()), + p_call->arguments[err.argument]); + } break; + case Callable::CallError::CALL_ERROR_INVALID_METHOD: + push_error(vformat(R"(Invalid call for function "%s".)", GDScriptFunctions::get_func_name(builtin_function)), p_call); + break; + case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: + push_error(vformat(R"*(Too many arguments for "%s()" call. Expected at most %d but received %d.)*", GDScriptFunctions::get_func_name(builtin_function), err.expected, p_call->arguments.size()), p_call); + break; + case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: + push_error(vformat(R"*(Too few arguments for "%s()" call. Expected at least %d but received %d.)*", GDScriptFunctions::get_func_name(builtin_function), err.expected, p_call->arguments.size()), p_call); + break; + case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: + break; // Can't happen in a builtin constructor. + case Callable::CallError::CALL_OK: + p_call->is_constant = true; + p_call->reduced_value = value; + break; + } + } else { + validate_call_arg(function_info, p_call); + } + p_call->set_datatype(type_from_property(function_info.return_val)); + return; + } + } + + GDScriptParser::DataType base_type; + call_type.kind = GDScriptParser::DataType::VARIANT; + bool is_self = false; + + if (p_call->is_super) { + base_type = parser->current_class->base_type; + is_self = true; + } else if (p_call->callee->type == GDScriptParser::Node::IDENTIFIER) { + base_type = parser->current_class->get_datatype(); + is_self = true; + } else if (p_call->callee->type == GDScriptParser::Node::SUBSCRIPT) { + GDScriptParser::SubscriptNode *subscript = static_cast(p_call->callee); + if (!subscript->is_attribute) { + // Invalid call. Error already sent in parser. + // TODO: Could check if Callable here. + p_call->set_datatype(call_type); + mark_node_unsafe(p_call); + return; + } + reduce_expression(subscript->base); + + base_type = subscript->base->get_datatype(); + } else { + // Invalid call. Error already sent in parser. + // TODO: Could check if Callable here too. + p_call->set_datatype(call_type); + mark_node_unsafe(p_call); + return; + } + + bool is_static = false; + bool is_vararg = false; + int default_arg_count = 0; + GDScriptParser::DataType return_type; + List par_types; + + if (get_function_signature(p_call, base_type, p_call->function_name, return_type, par_types, default_arg_count, is_static, is_vararg)) { + validate_call_arg(par_types, default_arg_count, is_vararg, p_call); + + if (is_self && parser->current_function != nullptr && parser->current_function->is_static && !is_static) { + push_error(vformat(R"*(Cannot call non-static function "%s()" from static function "%s()".)*", p_call->function_name, parser->current_function->identifier->name), p_call->callee); + } + + call_type = return_type; + } else { + // Check if the name exists as something else. + bool found = false; + if (!p_call->is_super) { + GDScriptParser::IdentifierNode *callee_id; + if (p_call->callee->type == GDScriptParser::Node::IDENTIFIER) { + callee_id = static_cast(p_call->callee); + } else { + // Can only be attribute. + callee_id = static_cast(p_call->callee)->attribute; + } + if (callee_id) { + reduce_identifier_from_base(callee_id, &base_type); + GDScriptParser::DataType callee_type = callee_id->get_datatype(); + if (callee_type.is_set() && !callee_type.is_variant()) { + found = true; + if (callee_type.builtin_type == Variant::CALLABLE) { + push_error(vformat(R"*(Name "%s" is a Callable. You can call it with "%s.call()" instead.)*", p_call->function_name, p_call->function_name), p_call->callee); + } else { + push_error(vformat(R"*(Name "%s" called as a function but is a "%s".)*", p_call->function_name, callee_type.to_string()), p_call->callee); + } +#ifdef DEBUG_ENABLED + } else if (!is_self) { + parser->push_warning(p_call, GDScriptWarning::UNSAFE_METHOD_ACCESS, p_call->function_name, base_type.to_string()); + mark_node_unsafe(p_call); +#endif + } + } + } + if (!found && is_self) { + String base_name = is_self && !p_call->is_super ? "self" : base_type.to_string(); + push_error(vformat(R"*(Function "%s()" not found in base %s.)*", p_call->function_name, base_name), p_call->is_super ? p_call : p_call->callee); + } + } + + if (call_type.is_coroutine && !is_await) { + push_error(vformat(R"*(Function "%s()" is a coroutine, so it must be called with "await".)*", p_call->function_name), p_call->callee); + } + + p_call->set_datatype(call_type); +} + +void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) { + reduce_expression(p_cast->operand); + + GDScriptParser::DataType cast_type = resolve_datatype(p_cast->cast_type); + + if (!cast_type.is_set()) { + return; + } + + cast_type.is_meta_type = false; // The casted value won't be a type name. + p_cast->set_datatype(cast_type); + + if (!cast_type.is_variant()) { + GDScriptParser::DataType op_type = p_cast->operand->get_datatype(); + if (!op_type.is_variant()) { + bool valid = false; + if (op_type.kind == GDScriptParser::DataType::BUILTIN && cast_type.kind == GDScriptParser::DataType::BUILTIN) { + valid = Variant::can_convert(op_type.builtin_type, cast_type.builtin_type); + } else if (op_type.kind != GDScriptParser::DataType::BUILTIN && cast_type.kind != GDScriptParser::DataType::BUILTIN) { + valid = is_type_compatible(cast_type, op_type) || is_type_compatible(op_type, cast_type); + } + + if (!valid) { + push_error(vformat(R"(Invalid cast. Cannot convert from "%s" to "%s".)", op_type.to_string(), cast_type.to_string()), p_cast->cast_type); + } + } + } else { + mark_node_unsafe(p_cast); + } +#ifdef DEBUG_ENABLED + if (p_cast->operand->get_datatype().is_variant()) { + parser->push_warning(p_cast, GDScriptWarning::UNSAFE_CAST, cast_type.to_string()); + mark_node_unsafe(p_cast); + } +#endif + + // TODO: Perform cast on constants. +} + +void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) { + bool all_is_constant = true; + + for (int i = 0; i < p_dictionary->elements.size(); i++) { + const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i]; + if (p_dictionary->style == GDScriptParser::DictionaryNode::PYTHON_DICT) { + reduce_expression(element.key); + } + reduce_expression(element.value); + all_is_constant = all_is_constant && element.key->is_constant && element.value->is_constant; + } + + if (all_is_constant) { + Dictionary dict; + for (int i = 0; i < p_dictionary->elements.size(); i++) { + const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i]; + dict[element.key->reduced_value] = element.value->reduced_value; + } + p_dictionary->is_constant = true; + p_dictionary->reduced_value = dict; + } + + // It's dictionary in any case. + GDScriptParser::DataType dict_type; + dict_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + dict_type.kind = GDScriptParser::DataType::BUILTIN; + dict_type.builtin_type = Variant::DICTIONARY; + dict_type.is_constant = true; + + p_dictionary->set_datatype(dict_type); +} + +void GDScriptAnalyzer::reduce_get_node(GDScriptParser::GetNodeNode *p_get_node) { + GDScriptParser::DataType result; + result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + result.kind = GDScriptParser::DataType::NATIVE; + result.native_type = "Node"; + result.builtin_type = Variant::OBJECT; + + if (!ClassDB::is_parent_class(get_real_class_name(parser->current_class->base_type.native_type), result.native_type)) { + push_error(R"*(Cannot use shorthand "get_node()" notation ("$") on a class that isn't a node.)*", p_get_node); + } + + p_get_node->set_datatype(result); +} + +GDScriptParser::DataType GDScriptAnalyzer::make_global_class_meta_type(const StringName &p_class_name) { + Ref ref = get_parser_for(ScriptServer::get_global_class_path(p_class_name)); + ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED); + + GDScriptParser::DataType type; + type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + type.kind = GDScriptParser::DataType::CLASS; + type.builtin_type = Variant::OBJECT; + type.native_type = ScriptServer::get_global_class_native_base(p_class_name); + type.class_type = ref->get_parser()->head; + type.script_path = ref->get_parser()->script_path; + type.is_constant = true; + type.is_meta_type = true; + return type; +} + +void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base) { + GDScriptParser::DataType base; + if (p_base == nullptr) { + base = type_from_metatype(parser->current_class->get_datatype()); + } else { + base = *p_base; + } + + const StringName &name = p_identifier->name; + + if (base.kind == GDScriptParser::DataType::BUILTIN) { + if (base.is_meta_type) { + bool valid = true; + Variant result = Variant::get_constant_value(base.builtin_type, name, &valid); + if (valid) { + p_identifier->is_constant = true; + p_identifier->reduced_value = result; + p_identifier->set_datatype(type_from_variant(result)); + } else { + push_error(vformat(R"(Cannot find constant "%s" on type "%s".)", name, base.to_string()), p_identifier); + } + } else { + Callable::CallError temp; + Variant dummy = Variant::construct(base.builtin_type, nullptr, 0, temp); + List properties; + dummy.get_property_list(&properties); + for (const List::Element *E = properties.front(); E != nullptr; E = E->next()) { + const PropertyInfo &prop = E->get(); + if (prop.name == name) { + p_identifier->set_datatype(type_from_property(prop)); + return; + } + } + push_error(vformat(R"(Cannot find property "%s" on base "%s".)", name, base.to_string()), p_identifier); + } + return; + } + + if (base.kind == GDScriptParser::DataType::ENUM) { + if (base.is_meta_type) { + if (base.enum_values.has(name)) { + p_identifier->is_constant = true; + p_identifier->reduced_value = base.enum_values[name]; + + GDScriptParser::DataType result; + result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + result.kind = GDScriptParser::DataType::ENUM_VALUE; + result.native_type = base.native_type; + result.enum_type = name; + p_identifier->set_datatype(result); + } else { + push_error(vformat(R"(Cannot find value "%s" in "%s".)", name, base.to_string()), p_identifier); + } + } else { + push_error(R"(Cannot get property from enum value.)", p_identifier); + } + return; + } + + GDScriptParser::ClassNode *base_class = base.class_type; + + // TODO: Switch current class/function/suite here to avoid misrepresenting identifiers (in recursive reduce calls). + while (base_class != nullptr) { + if (base_class->identifier && base_class->identifier->name == name) { + p_identifier->set_datatype(base_class->get_datatype()); + return; + } + if (base_class->has_member(name)) { + const GDScriptParser::ClassNode::Member &member = base_class->get_member(name); + p_identifier->set_datatype(member.get_datatype()); + switch (member.type) { + case GDScriptParser::ClassNode::Member::CONSTANT: + // For out-of-order resolution: + reduce_expression(member.constant->initializer); + p_identifier->is_constant = true; + p_identifier->reduced_value = member.constant->initializer->reduced_value; + p_identifier->set_datatype(member.constant->initializer->get_datatype()); + p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT; + p_identifier->constant_source = member.constant; + break; + case GDScriptParser::ClassNode::Member::ENUM_VALUE: + p_identifier->is_constant = true; + p_identifier->reduced_value = member.enum_value.value; + break; + case GDScriptParser::ClassNode::Member::VARIABLE: + p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_VARIABLE; + p_identifier->variable_source = member.variable; + break; + case GDScriptParser::ClassNode::Member::FUNCTION: + resolve_function_signature(member.function); + p_identifier->set_datatype(make_callable_type(member.function->info)); + break; + default: + break; // Type already set. + } + return; + } + // Check outer constants. + // TODO: Allow outer static functions. + GDScriptParser::ClassNode *outer = base_class->outer; + while (outer != nullptr) { + if (outer->has_member(name)) { + const GDScriptParser::ClassNode::Member &member = base_class->get_member(name); + if (member.type == GDScriptParser::ClassNode::Member::CONSTANT) { + // TODO: Make sure loops won't cause problem. And make special error message for those. + // For out-of-order resolution: + reduce_expression(member.constant->initializer); + p_identifier->set_datatype(member.get_datatype()); + p_identifier->is_constant = true; + p_identifier->reduced_value = member.constant->initializer->reduced_value; + return; + } + } + outer = outer->outer; + } + + base_class = base_class->base_type.class_type; + } + + // Check native members. + const StringName &native = get_real_class_name(base.native_type); + + if (class_exists(native)) { + PropertyInfo prop_info; + MethodInfo method_info; + if (ClassDB::get_property_info(native, name, &prop_info)) { + p_identifier->set_datatype(type_from_property(prop_info)); + return; + } + if (ClassDB::get_method_info(native, name, &method_info)) { + // Method is callable. + p_identifier->set_datatype(make_callable_type(method_info)); + return; + } + if (ClassDB::get_signal(native, name, &method_info)) { + // Signal is a type too. + p_identifier->set_datatype(make_signal_type(method_info)); + return; + } + if (ClassDB::has_enum(native, name)) { + p_identifier->set_datatype(make_native_enum_type(native, name)); + return; + } + bool valid = false; + int int_constant = ClassDB::get_integer_constant(native, name, &valid); + if (valid) { + p_identifier->is_constant = true; + p_identifier->reduced_value = int_constant; + p_identifier->set_datatype(type_from_variant(int_constant)); + return; + } + } +} + +void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin) { + // TODO: This is opportunity to further infer types. + // Check if identifier is local. + // If that's the case, the declaration already was solved before. + switch (p_identifier->source) { + case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER: + p_identifier->set_datatype(p_identifier->parameter_source->get_datatype()); + return; + case GDScriptParser::IdentifierNode::LOCAL_CONSTANT: + case GDScriptParser::IdentifierNode::MEMBER_CONSTANT: + p_identifier->set_datatype(p_identifier->constant_source->get_datatype()); + p_identifier->is_constant = true; + // TODO: Constant should have a value on the node itself. + p_identifier->reduced_value = p_identifier->constant_source->initializer->reduced_value; + return; + case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: + p_identifier->variable_source->usages++; + [[fallthrough]]; + case GDScriptParser::IdentifierNode::LOCAL_VARIABLE: + p_identifier->set_datatype(p_identifier->variable_source->get_datatype()); + return; + case GDScriptParser::IdentifierNode::LOCAL_ITERATOR: + p_identifier->set_datatype(p_identifier->bind_source->get_datatype()); + return; + case GDScriptParser::IdentifierNode::LOCAL_BIND: { + GDScriptParser::DataType result = p_identifier->bind_source->get_datatype(); + result.is_constant = true; + p_identifier->set_datatype(result); + return; + } + case GDScriptParser::IdentifierNode::UNDEFINED_SOURCE: + break; + } + + // Not a local, so check members. + reduce_identifier_from_base(p_identifier); + if (p_identifier->get_datatype().is_set()) { + // Found. + return; + } + + StringName name = p_identifier->name; + p_identifier->source = GDScriptParser::IdentifierNode::UNDEFINED_SOURCE; + + // Check globals. + if (GDScriptParser::get_builtin_type(name) < Variant::VARIANT_MAX) { + if (can_be_builtin) { + p_identifier->set_datatype(make_builtin_meta_type(GDScriptParser::get_builtin_type(name))); + return; + } else { + push_error(R"(Builtin type cannot be used as a name on its own.)", p_identifier); + } + } + + if (class_exists(name)) { + p_identifier->set_datatype(make_native_meta_type(name)); + return; + } + + if (ScriptServer::is_global_class(name)) { + p_identifier->set_datatype(make_global_class_meta_type(name)); + return; + } + + if (GDScriptLanguage::get_singleton()->get_global_map().has(name)) { + int idx = GDScriptLanguage::get_singleton()->get_global_map()[name]; + Variant constant = GDScriptLanguage::get_singleton()->get_global_array()[idx]; + p_identifier->set_datatype(type_from_variant(constant)); + p_identifier->is_constant = true; + p_identifier->reduced_value = constant; + return; + } + + if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(name)) { + Variant constant = GDScriptLanguage::get_singleton()->get_named_globals_map()[name]; + p_identifier->set_datatype(type_from_variant(constant)); + p_identifier->is_constant = true; + p_identifier->reduced_value = constant; + return; + } + + // Not found. + // Check if it's a builtin function. + if (parser->get_builtin_function(name) < GDScriptFunctions::FUNC_MAX) { + push_error(vformat(R"(Built-in function "%s" cannot be used as an identifier.)", name), p_identifier); + } else { + push_error(vformat(R"(Identifier "%s" not declared in the current scope.)", name), p_identifier); + } + GDScriptParser::DataType dummy; + dummy.kind = GDScriptParser::DataType::VARIANT; + p_identifier->set_datatype(dummy); // Just so type is set to something. +} + +void GDScriptAnalyzer::reduce_literal(GDScriptParser::LiteralNode *p_literal) { + p_literal->reduced_value = p_literal->value; + p_literal->is_constant = true; + + p_literal->set_datatype(type_from_variant(p_literal->reduced_value)); +} + +void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) { + p_preload->is_constant = true; + p_preload->reduced_value = p_preload->resource; + p_preload->set_datatype(type_from_variant(p_preload->reduced_value)); +} + +void GDScriptAnalyzer::reduce_self(GDScriptParser::SelfNode *p_self) { + p_self->is_constant = false; + p_self->set_datatype(type_from_metatype(parser->current_class->get_datatype())); +} + +void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscript) { + if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER) { + reduce_identifier(static_cast(p_subscript->base), true); + } else { + reduce_expression(p_subscript->base); + } + + GDScriptParser::DataType result_type; + + // Reduce index first. If it's a constant StringName, use attribute instead. + if (!p_subscript->is_attribute) { + if (p_subscript->index == nullptr) { + return; + } + reduce_expression(p_subscript->index); + + if (p_subscript->index->is_constant && p_subscript->index->reduced_value.get_type() == Variant::STRING_NAME) { + GDScriptParser::IdentifierNode *attribute = parser->alloc_node(); + // Copy location for better error message. + attribute->start_line = p_subscript->index->start_line; + attribute->end_line = p_subscript->index->end_line; + attribute->leftmost_column = p_subscript->index->leftmost_column; + attribute->rightmost_column = p_subscript->index->rightmost_column; + p_subscript->is_attribute = true; + p_subscript->attribute = attribute; + } + } + + if (p_subscript->is_attribute) { + if (p_subscript->attribute == nullptr) { + return; + } + if (p_subscript->base->is_constant) { + // Just try to get it. + bool valid = false; + Variant value = p_subscript->base->reduced_value.get_named(p_subscript->attribute->name, &valid); + if (!valid) { + push_error(vformat(R"(Cannot get member "%s" from "%s".)", p_subscript->attribute->name, p_subscript->base->reduced_value), p_subscript->index); + } else { + p_subscript->is_constant = true; + p_subscript->reduced_value = value; + result_type = type_from_variant(value); + } + result_type.kind = GDScriptParser::DataType::VARIANT; + } else { + GDScriptParser::DataType base_type = p_subscript->base->get_datatype(); + + if (base_type.is_variant()) { + result_type.kind = GDScriptParser::DataType::VARIANT; + mark_node_unsafe(p_subscript); + } else { + reduce_identifier_from_base(p_subscript->attribute, &base_type); + GDScriptParser::DataType attr_type = p_subscript->attribute->get_datatype(); + if (attr_type.is_set()) { + result_type = attr_type; + p_subscript->is_constant = p_subscript->attribute->is_constant; + p_subscript->reduced_value = p_subscript->attribute->reduced_value; + } else { + if (base_type.kind == GDScriptParser::DataType::BUILTIN) { + push_error(vformat(R"(Cannot find member "%s" in base "%s".)", p_subscript->attribute->name, base_type.to_string()), p_subscript->attribute); +#ifdef DEBUG_ENABLED + } else { + parser->push_warning(p_subscript, GDScriptWarning::UNSAFE_PROPERTY_ACCESS, p_subscript->attribute->name, base_type.to_string()); +#endif + } + result_type.kind = GDScriptParser::DataType::VARIANT; + } + } + } + } else { + // Index was already reduced before. + + if (p_subscript->base->is_constant && p_subscript->index->is_constant) { + // Just try to get it. + bool valid = false; + Variant value = p_subscript->base->reduced_value.get(p_subscript->index->reduced_value, &valid); + if (!valid) { + push_error(vformat(R"(Cannot get index "%s" from "%s".)", p_subscript->index->reduced_value, p_subscript->base->reduced_value), p_subscript->index); + } else { + p_subscript->is_constant = true; + p_subscript->reduced_value = value; + result_type = type_from_variant(value); + } + result_type.kind = GDScriptParser::DataType::VARIANT; + } else { + GDScriptParser::DataType base_type = p_subscript->base->get_datatype(); + GDScriptParser::DataType index_type = p_subscript->index->get_datatype(); + + if (base_type.is_variant()) { + result_type.kind = GDScriptParser::DataType::VARIANT; + mark_node_unsafe(p_subscript); + } else { + if (base_type.kind == GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) { + // Check if indexing is valid. + bool error = index_type.kind != GDScriptParser::DataType::BUILTIN && base_type.builtin_type != Variant::DICTIONARY; + if (!error) { + switch (base_type.builtin_type) { + // Expect int or real as index. + case Variant::PACKED_BYTE_ARRAY: + case Variant::PACKED_COLOR_ARRAY: + case Variant::PACKED_FLOAT32_ARRAY: + case Variant::PACKED_FLOAT64_ARRAY: + case Variant::PACKED_INT32_ARRAY: + case Variant::PACKED_INT64_ARRAY: + case Variant::PACKED_STRING_ARRAY: + case Variant::PACKED_VECTOR2_ARRAY: + case Variant::PACKED_VECTOR3_ARRAY: + case Variant::ARRAY: + case Variant::STRING: + error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT; + break; + // Expect String only. + case Variant::RECT2: + case Variant::RECT2I: + case Variant::PLANE: + case Variant::QUAT: + case Variant::AABB: + case Variant::OBJECT: + error = index_type.builtin_type != Variant::STRING; + break; + // Expect String or number. + case Variant::BASIS: + case Variant::VECTOR2: + case Variant::VECTOR2I: + case Variant::VECTOR3: + case Variant::VECTOR3I: + case Variant::TRANSFORM: + case Variant::TRANSFORM2D: + error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT && + index_type.builtin_type != Variant::STRING; + break; + // Expect String or int. + case Variant::COLOR: + error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING; + break; + // Don't support indexing, but we will check it later. + case Variant::_RID: + case Variant::BOOL: + case Variant::CALLABLE: + case Variant::FLOAT: + case Variant::INT: + case Variant::NIL: + case Variant::NODE_PATH: + case Variant::SIGNAL: + case Variant::STRING_NAME: + break; + // Here for completeness. + case Variant::DICTIONARY: + case Variant::VARIANT_MAX: + break; + } + + if (error) { + push_error(vformat(R"(Invalid index type "%s" for a base of type "%s".)", index_type.to_string(), base_type.to_string()), p_subscript->index); + } + } + } else if (base_type.kind != GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) { + if (index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME) { + push_error(vformat(R"(Only String or StringName can be used as index for type "%s", but received a "%s".)", base_type.to_string(), index_type.to_string()), p_subscript->index); + } + } + + // Check resulting type if possible. + result_type.builtin_type = Variant::NIL; + result_type.kind = GDScriptParser::DataType::BUILTIN; + result_type.type_source = GDScriptParser::DataType::INFERRED; + + switch (base_type.builtin_type) { + // Can't index at all. + case Variant::_RID: + case Variant::BOOL: + case Variant::CALLABLE: + case Variant::FLOAT: + case Variant::INT: + case Variant::NIL: + case Variant::NODE_PATH: + case Variant::SIGNAL: + case Variant::STRING_NAME: + result_type.kind = GDScriptParser::DataType::VARIANT; + push_error(vformat(R"(Cannot use subscript operator on a base of type "%s".)", base_type.to_string()), p_subscript->base); + break; + // Return int. + case Variant::PACKED_BYTE_ARRAY: + case Variant::PACKED_INT32_ARRAY: + case Variant::PACKED_INT64_ARRAY: + case Variant::VECTOR2I: + case Variant::VECTOR3I: + result_type.builtin_type = Variant::INT; + break; + // Return float. + case Variant::PACKED_FLOAT32_ARRAY: + case Variant::PACKED_FLOAT64_ARRAY: + case Variant::VECTOR2: + case Variant::VECTOR3: + case Variant::QUAT: + result_type.builtin_type = Variant::FLOAT; + break; + // Return Color. + case Variant::PACKED_COLOR_ARRAY: + result_type.builtin_type = Variant::COLOR; + break; + // Return String. + case Variant::PACKED_STRING_ARRAY: + case Variant::STRING: + result_type.builtin_type = Variant::STRING; + break; + // Return Vector2. + case Variant::PACKED_VECTOR2_ARRAY: + case Variant::TRANSFORM2D: + case Variant::RECT2: + result_type.builtin_type = Variant::VECTOR2; + break; + // Return Vector2I. + case Variant::RECT2I: + result_type.builtin_type = Variant::VECTOR2I; + break; + // Return Vector3. + case Variant::PACKED_VECTOR3_ARRAY: + case Variant::AABB: + case Variant::BASIS: + result_type.builtin_type = Variant::VECTOR3; + break; + // Depends on the index. + case Variant::TRANSFORM: + case Variant::PLANE: + case Variant::COLOR: + case Variant::ARRAY: + case Variant::DICTIONARY: + result_type.kind = GDScriptParser::DataType::VARIANT; + result_type.type_source = GDScriptParser::DataType::UNDETECTED; + break; + // Here for completeness. + case Variant::OBJECT: + case Variant::VARIANT_MAX: + break; + } + } + } + } + + p_subscript->set_datatype(result_type); +} + +void GDScriptAnalyzer::reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op) { + reduce_expression(p_ternary_op->condition); + reduce_expression(p_ternary_op->true_expr); + reduce_expression(p_ternary_op->false_expr); + + GDScriptParser::DataType result; + + if (p_ternary_op->condition && p_ternary_op->condition->is_constant && p_ternary_op->true_expr->is_constant && p_ternary_op->false_expr && p_ternary_op->false_expr->is_constant) { + p_ternary_op->is_constant = true; + if (p_ternary_op->condition->reduced_value.booleanize()) { + p_ternary_op->reduced_value = p_ternary_op->true_expr->reduced_value; + } else { + p_ternary_op->reduced_value = p_ternary_op->false_expr->reduced_value; + } + } + + GDScriptParser::DataType true_type; + if (p_ternary_op->true_expr) { + true_type = p_ternary_op->true_expr->get_datatype(); + } else { + true_type.kind = GDScriptParser::DataType::VARIANT; + } + GDScriptParser::DataType false_type; + if (p_ternary_op->false_expr) { + false_type = p_ternary_op->false_expr->get_datatype(); + } else { + false_type.kind = GDScriptParser::DataType::VARIANT; + } + + if (true_type.is_variant() || false_type.is_variant()) { + result.kind = GDScriptParser::DataType::VARIANT; + } else { + result = true_type; + if (!is_type_compatible(true_type, false_type)) { + result = false_type; + if (!is_type_compatible(false_type, true_type)) { + result.type_source = GDScriptParser::DataType::UNDETECTED; + result.kind = GDScriptParser::DataType::VARIANT; +#ifdef DEBUG_ENABLED + parser->push_warning(p_ternary_op, GDScriptWarning::INCOMPATIBLE_TERNARY); +#endif + } + } + } + + p_ternary_op->set_datatype(result); +} + +void GDScriptAnalyzer::reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op) { + reduce_expression(p_unary_op->operand); + + GDScriptParser::DataType result; + + if (p_unary_op->operand->is_constant) { + p_unary_op->is_constant = true; + p_unary_op->reduced_value = Variant::evaluate(p_unary_op->variant_op, p_unary_op->operand->reduced_value, Variant()); + result = type_from_variant(p_unary_op->reduced_value); + } else if (p_unary_op->operand->get_datatype().is_variant()) { + result.kind = GDScriptParser::DataType::VARIANT; + mark_node_unsafe(p_unary_op); + } else { + bool valid = false; + result = get_operation_type(p_unary_op->variant_op, p_unary_op->operand->get_datatype(), p_unary_op->operand->get_datatype(), valid); + + if (!valid) { + push_error(vformat(R"(Invalid operand of type "%s" for unary operator "%s".)", p_unary_op->operand->get_datatype().to_string(), Variant::get_operator_name(p_unary_op->variant_op)), p_unary_op->operand); + } + } + + p_unary_op->set_datatype(result); +} + +GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_value) { + GDScriptParser::DataType result; + result.is_constant = true; + result.kind = GDScriptParser::DataType::BUILTIN; + result.builtin_type = p_value.get_type(); + result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; // Constant has explicit type. + + if (p_value.get_type() == Variant::OBJECT) { + Object *obj = p_value; + if (!obj) { + return GDScriptParser::DataType(); + } + result.native_type = obj->get_class_name(); + + Ref