From 5d6e8538065050d5f5579ec03cfa9e241811e062 Mon Sep 17 00:00:00 2001 From: George Marques Date: Fri, 1 May 2020 19:14:56 -0300 Subject: [PATCH] New GDScript tokenizer and parser Sometimes to fix something you have to break it first. This get GDScript mostly working with the new tokenizer and parser but a lot of things isn't working yet. It compiles and it's usable, and that should be enough for now. Don't worry: other huge commits will come after this. --- main/tests/test_gdscript.cpp | 992 +- .../gdscript/editor/gdscript_highlighter.cpp | 4 +- modules/gdscript/gdscript.cpp | 459 +- modules/gdscript/gdscript.h | 4 +- modules/gdscript/gdscript_analyzer.cpp | 283 + modules/gdscript/gdscript_analyzer.h | 52 + modules/gdscript/gdscript_compiler.cpp | 2742 ++-- modules/gdscript/gdscript_compiler.h | 19 +- modules/gdscript/gdscript_editor.cpp | 3036 +--- modules/gdscript/gdscript_function.cpp | 200 +- modules/gdscript/gdscript_function.h | 6 +- modules/gdscript/gdscript_parser.cpp | 11853 ++++------------ modules/gdscript/gdscript_parser.h | 1471 +- modules/gdscript/gdscript_tokenizer.cpp | 2436 ++-- modules/gdscript/gdscript_tokenizer.h | 439 +- modules/gdscript/gdscript_warning.cpp | 201 + modules/gdscript/gdscript_warning.h | 84 + .../gdscript_extend_parser.cpp | 5 + .../language_server/gdscript_extend_parser.h | 5 + .../gdscript_language_protocol.cpp | 5 + .../gdscript_language_protocol.h | 5 + .../gdscript_language_server.cpp | 5 + .../gdscript_language_server.h | 5 + .../gdscript_text_document.cpp | 5 + .../language_server/gdscript_text_document.h | 5 + .../language_server/gdscript_workspace.cpp | 5 + .../language_server/gdscript_workspace.h | 5 + modules/gdscript/language_server/lsp.hpp | 5 + modules/gdscript/register_types.cpp | 66 +- 29 files changed, 7906 insertions(+), 16496 deletions(-) create mode 100644 modules/gdscript/gdscript_analyzer.cpp create mode 100644 modules/gdscript/gdscript_analyzer.h create mode 100644 modules/gdscript/gdscript_warning.cpp create mode 100644 modules/gdscript/gdscript_warning.h diff --git a/main/tests/test_gdscript.cpp b/main/tests/test_gdscript.cpp index 10586c64959..a50311972f2 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 43d0116125f..aba3e071347 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 632407c61fe..f2752a75397 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -39,7 +39,9 @@ #include "core/os/file_access.h" #include "core/os/os.h" #include "core/project_settings.h" +#include "gdscript_analyzer.h" #include "gdscript_compiler.h" +#include "gdscript_parser.h" /////////////////////////// @@ -79,6 +81,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 +114,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 +125,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 +409,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 +422,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 +432,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 +456,37 @@ 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); + // FIXME: Get variable's default value in non-literal cases. + Variant default_value; + if (member.variable->initializer != nullptr && member.variable->initializer->type == GDScriptParser::Node::LITERAL) { + default_value = static_cast(member.variable->initializer)->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 +597,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); @@ -581,13 +636,14 @@ Error GDScript::reload(bool p_keep_state) { } } #ifdef DEBUG_ENABLED - for (const List::Element *E = parser.get_warnings().front(); E; E = E->next()) { - 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); - } - } + // FIXME: Add warnings. + // for (const List::Element *E = parser.get_warnings().front(); E; E = E->next()) { + // 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); + // } + // } #endif valid = true; @@ -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) { @@ -1152,6 +1137,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 +1315,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 +1839,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 +1862,7 @@ void GDScriptLanguage::get_reserved_words(List *p_words) const { // functions "as", "assert", + "await", "breakpoint", "class", "class_name", @@ -1856,15 +1870,11 @@ void GDScriptLanguage::get_reserved_words(List *p_words) const { "is", "func", "preload", - "setget", "signal", - "tool", "yield", // var "const", "enum", - "export", - "onready", "static", "var", // control flow @@ -1878,12 +1888,6 @@ void GDScriptLanguage::get_reserved_words(List *p_words) const { "return", "match", "while", - "remote", - "master", - "puppet", - "remotesync", - "mastersync", - "puppetsync", nullptr }; @@ -1914,10 +1918,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 +1936,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 +1953,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 +1985,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 +1997,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); @@ -2296,7 +2133,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); diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp new file mode 100644 index 00000000000..efc2ecd9c8c --- /dev/null +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -0,0 +1,283 @@ +/*************************************************************************/ +/* 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/script_language.h" + +Error GDScriptAnalyzer::resolve_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive) { + GDScriptParser::DataType result; + + if (p_class->base_type.is_set()) { + // Already resolved + return OK; + } + + 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; + + if (!p_class->extends_path.empty()) { + base.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + base.kind = GDScriptParser::DataType::CLASS; + // TODO: Don't load the script here to avoid the issue with cycles. + base.script_type = ResourceLoader::load(p_class->extends_path); + if (base.script_type.is_null() || !base.script_type->is_valid()) { + parser->push_error(vformat(R"(Could not load the parent script at "%s".)", p_class->extends_path)); + } + // TODO: Get this from cache singleton. + base.gdscript_type = nullptr; + } else { + const StringName &name = p_class->extends[0]; + base.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + + if (ScriptServer::is_global_class(name)) { + base.kind = GDScriptParser::DataType::CLASS; + // TODO: Get this from cache singleton. + base.gdscript_type = nullptr; + // TODO: Try singletons (create main unified source for those). + } else if (p_class->members_indices.has(name)) { + GDScriptParser::ClassNode::Member member = p_class->get_member(name); + + if (member.type == member.CLASS) { + base.kind = GDScriptParser::DataType::CLASS; + base.gdscript_type = member.m_class; + } else if (member.type == member.CONSTANT) { + // FIXME: This could also be a native type or preloaded GDScript. + base.kind = GDScriptParser::DataType::CLASS; + base.gdscript_type = nullptr; + } + } else { + if (ClassDB::class_exists(name)) { + base.kind = GDScriptParser::DataType::NATIVE; + base.native_type = name; + } + } + } + + // TODO: Extends with attributes (A.B.C). + result = base; + } + + if (!result.is_set()) { + // TODO: More specific error messages. + parser->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->set_datatype(result); + + 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; +} + +Error GDScriptAnalyzer::resolve_inheritance() { + return resolve_inheritance(parser->head); +} + +// 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; +} + +GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(const GDScriptParser::TypeNode *p_type) { + GDScriptParser::DataType result; + + if (p_type == nullptr) { + return result; + } + + result.type_source = result.ANNOTATED_EXPLICIT; + if (p_type->type_base == nullptr) { + // void. + result.kind = GDScriptParser::DataType::BUILTIN; + result.builtin_type = Variant::NIL; + return result; + } + + StringName first = p_type->type_base->name; + + if (GDScriptParser::get_builtin_type(first) != Variant::NIL) { + // Built-in types. + // FIXME: I'm probably using this wrong here (well, I'm not really using it). Specifier *includes* the base the type. + if (p_type->type_specifier != nullptr) { + parser->push_error(R"(Built-in types don't contain subtypes.)", p_type->type_specifier); + return GDScriptParser::DataType(); + } + result.kind = GDScriptParser::DataType::BUILTIN; + result.builtin_type = GDScriptParser::get_builtin_type(first); + } else if (ClassDB::class_exists(get_real_class_name(first))) { + // Native engine classes. + if (p_type->type_specifier != nullptr) { + parser->push_error(R"(Engine classes don't contain subtypes.)", p_type->type_specifier); + return GDScriptParser::DataType(); + } + result.kind = GDScriptParser::DataType::NATIVE; + result.native_type = first; + } else if (ScriptServer::is_global_class(first)) { + // Global class_named classes. + // TODO: Global classes and singletons. + parser->push_error("GDScript analyzer: global class type not implemented.", p_type); + ERR_FAIL_V_MSG(GDScriptParser::DataType(), "GDScript analyzer: global class type not implemented."); + } else { + // Classes in current scope. + GDScriptParser::ClassNode *script_class = parser->current_class; + bool found = false; + while (!found && script_class != nullptr) { + 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.kind = GDScriptParser::DataType::CLASS; + result.gdscript_type = member.m_class; + found = true; + break; + default: + // TODO: Get constants as types, disallow others explicitly. + parser->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; + } + + parser->push_error(vformat(R"("%s" is not a valid type.)", first), p_type); + return GDScriptParser::DataType(); + } + + // TODO: Allow subtypes. + if (p_type->type_specifier != nullptr) { + parser->push_error(R"(Subtypes are not yet supported.)", p_type->type_specifier); + return GDScriptParser::DataType(); + } + + return result; +} + +Error GDScriptAnalyzer::resolve_datatypes(GDScriptParser::ClassNode *p_class) { + 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 = resolve_datatype(member.variable->datatype_specifier); + if (datatype.is_set()) { + member.variable->set_datatype(datatype); + 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 { + parser->push_error(R"(Export type can only be built-in or a resource.)", member.variable); + } + break; + default: + // TODO: Allow custom user resources. + parser->push_error(R"(Export type can only be built-in or a resource.)", member.variable); + break; + } + } + } + break; + } + default: + // TODO + break; + } + } + parser->current_class = previous_class; + + return parser->errors.size() > 0 ? ERR_PARSE_ERROR : OK; +} + +Error GDScriptAnalyzer::analyze() { + parser->errors.clear(); + Error err = resolve_inheritance(parser->head); + if (err) { + return err; + } + return resolve_datatypes(parser->head); +} + +GDScriptAnalyzer::GDScriptAnalyzer(GDScriptParser *p_parser) { + parser = p_parser; +} diff --git a/modules/gdscript/gdscript_analyzer.h b/modules/gdscript/gdscript_analyzer.h new file mode 100644 index 00000000000..33f036f11c7 --- /dev/null +++ b/modules/gdscript/gdscript_analyzer.h @@ -0,0 +1,52 @@ +/*************************************************************************/ +/* gdscript_analyzer.h */ +/*************************************************************************/ +/* 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. */ +/*************************************************************************/ + +#ifndef GDSCRIPT_ANALYZER_H +#define GDSCRIPT_ANALYZER_H + +#include "gdscript_parser.h" + +class GDScriptAnalyzer { + GDScriptParser *parser = nullptr; + + Error resolve_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive = true); + GDScriptParser::DataType resolve_datatype(const GDScriptParser::TypeNode *p_type); + + // This traverses the tree to resolve all TypeNodes. + Error resolve_datatypes(GDScriptParser::ClassNode *p_class); + +public: + Error resolve_inheritance(); + Error analyze(); + + GDScriptAnalyzer(GDScriptParser *p_parser); +}; + +#endif // GDSCRIPT_ANALYZER_H diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp index 5bc9003c292..def2bed714b 100644 --- a/modules/gdscript/gdscript_compiler.cpp +++ b/modules/gdscript/gdscript_compiler.cpp @@ -33,7 +33,7 @@ #include "gdscript.h" bool GDScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringName &p_name) { - if (codegen.function_node && codegen.function_node->_static) { + if (codegen.function_node && codegen.function_node->is_static) { return false; } @@ -66,18 +66,16 @@ void GDScriptCompiler::_set_error(const String &p_error, const GDScriptParser::N error = p_error; if (p_node) { - err_line = p_node->line; - err_column = p_node->column; + err_line = p_node->start_line; + err_column = p_node->leftmost_column; } else { err_line = 0; err_column = 0; } } -bool GDScriptCompiler::_create_unary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level) { - ERR_FAIL_COND_V(on->arguments.size() != 1, false); - - int src_address_a = _parse_expression(codegen, on->arguments[0], p_stack_level); +bool GDScriptCompiler::_create_unary_operator(CodeGen &codegen, const GDScriptParser::UnaryOpNode *on, Variant::Operator op, int p_stack_level) { + int src_address_a = _parse_expression(codegen, on->operand, p_stack_level); if (src_address_a < 0) { return false; } @@ -90,10 +88,8 @@ bool GDScriptCompiler::_create_unary_operator(CodeGen &codegen, const GDScriptPa return true; } -bool GDScriptCompiler::_create_binary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer, int p_index_addr) { - ERR_FAIL_COND_V(on->arguments.size() != 2, false); - - int src_address_a = _parse_expression(codegen, on->arguments[0], p_stack_level, false, p_initializer, p_index_addr); +bool GDScriptCompiler::_create_binary_operator(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_left_operand, const GDScriptParser::ExpressionNode *p_right_operand, Variant::Operator op, int p_stack_level, bool p_initializer, int p_index_addr) { + int src_address_a = _parse_expression(codegen, p_left_operand, p_stack_level, false, p_initializer, p_index_addr); if (src_address_a < 0) { return false; } @@ -101,7 +97,7 @@ bool GDScriptCompiler::_create_binary_operator(CodeGen &codegen, const GDScriptP p_stack_level++; //uses stack for return, increase stack } - int src_address_b = _parse_expression(codegen, on->arguments[1], p_stack_level, false, p_initializer); + int src_address_b = _parse_expression(codegen, p_right_operand, p_stack_level, false, p_initializer); if (src_address_b < 0) { return false; } @@ -113,8 +109,12 @@ bool GDScriptCompiler::_create_binary_operator(CodeGen &codegen, const GDScriptP return true; } +bool GDScriptCompiler::_create_binary_operator(CodeGen &codegen, const GDScriptParser::BinaryOpNode *on, Variant::Operator op, int p_stack_level, bool p_initializer, int p_index_addr) { + return _create_binary_operator(codegen, on->left_operand, on->right_operand, op, p_stack_level, p_initializer, p_index_addr); +} + GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::DataType &p_datatype) const { - if (!p_datatype.has_type) { + if (!p_datatype.is_set()) { return GDScriptDataType(); } @@ -135,34 +135,35 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D result.script_type = p_datatype.script_type; result.native_type = result.script_type->get_instance_base_type(); } break; - case GDScriptParser::DataType::GDSCRIPT: { - result.kind = GDScriptDataType::GDSCRIPT; - result.script_type = p_datatype.script_type; - result.native_type = result.script_type->get_instance_base_type(); - } break; case GDScriptParser::DataType::CLASS: { // Locate class by constructing the path to it and following that path - GDScriptParser::ClassNode *class_type = p_datatype.class_type; - List names; - while (class_type->owner) { - names.push_back(class_type->name); - class_type = class_type->owner; - } - - Ref script = Ref(main_script); - while (names.back()) { - if (!script->subclasses.has(names.back()->get())) { - ERR_PRINT("Parser bug: Cannot locate datatype class."); - result.has_type = false; - return GDScriptDataType(); + GDScriptParser::ClassNode *class_type = p_datatype.gdscript_type; + if (class_type) { + List names; + while (class_type->outer) { + names.push_back(class_type->identifier->name); + class_type = class_type->outer; } - script = script->subclasses[names.back()->get()]; - names.pop_back(); + + Ref script = Ref(main_script); + while (names.back()) { + if (!script->subclasses.has(names.back()->get())) { + ERR_PRINT("Parser bug: Cannot locate datatype class."); + result.has_type = false; + return GDScriptDataType(); + } + script = script->subclasses[names.back()->get()]; + names.pop_back(); + } + result.kind = GDScriptDataType::GDSCRIPT; + result.script_type = script; + result.native_type = script->get_instance_base_type(); + } else { + result.kind = GDScriptDataType::GDSCRIPT; + result.script_type = p_datatype.script_type; + result.native_type = result.script_type->get_instance_base_type(); } - result.kind = GDScriptDataType::GDSCRIPT; - result.script_type = script; - result.native_type = script->get_instance_base_type(); } break; default: { ERR_PRINT("Parser bug: converting unresolved type."); @@ -173,42 +174,41 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D return result; } -int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level, int p_index_addr) { +int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::AssignmentNode *p_assignment, int p_stack_level, int p_index_addr) { Variant::Operator var_op = Variant::OP_MAX; - switch (p_expression->op) { - case GDScriptParser::OperatorNode::OP_ASSIGN_ADD: + switch (p_assignment->operation) { + case GDScriptParser::AssignmentNode::OP_ADDITION: var_op = Variant::OP_ADD; break; - case GDScriptParser::OperatorNode::OP_ASSIGN_SUB: + case GDScriptParser::AssignmentNode::OP_SUBTRACTION: var_op = Variant::OP_SUBTRACT; break; - case GDScriptParser::OperatorNode::OP_ASSIGN_MUL: + case GDScriptParser::AssignmentNode::OP_MULTIPLICATION: var_op = Variant::OP_MULTIPLY; break; - case GDScriptParser::OperatorNode::OP_ASSIGN_DIV: + case GDScriptParser::AssignmentNode::OP_DIVISION: var_op = Variant::OP_DIVIDE; break; - case GDScriptParser::OperatorNode::OP_ASSIGN_MOD: + case GDScriptParser::AssignmentNode::OP_MODULO: var_op = Variant::OP_MODULE; break; - case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT: + case GDScriptParser::AssignmentNode::OP_BIT_SHIFT_LEFT: var_op = Variant::OP_SHIFT_LEFT; break; - case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: + case GDScriptParser::AssignmentNode::OP_BIT_SHIFT_RIGHT: var_op = Variant::OP_SHIFT_RIGHT; break; - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_AND: + case GDScriptParser::AssignmentNode::OP_BIT_AND: var_op = Variant::OP_BIT_AND; break; - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_OR: + case GDScriptParser::AssignmentNode::OP_BIT_OR: var_op = Variant::OP_BIT_OR; break; - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR: + case GDScriptParser::AssignmentNode::OP_BIT_XOR: var_op = Variant::OP_BIT_XOR; break; - case GDScriptParser::OperatorNode::OP_INIT_ASSIGN: - case GDScriptParser::OperatorNode::OP_ASSIGN: { + case GDScriptParser::AssignmentNode::OP_NONE: { //none } break; default: { @@ -216,13 +216,13 @@ int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDS } } - bool initializer = p_expression->op == GDScriptParser::OperatorNode::OP_INIT_ASSIGN; + // bool initializer = p_expression->op == GDScriptParser::OperatorNode::OP_INIT_ASSIGN; if (var_op == Variant::OP_MAX) { - return _parse_expression(codegen, p_expression->arguments[1], p_stack_level, false, initializer); + return _parse_expression(codegen, p_assignment->assigned_value, p_stack_level, false, false); } - if (!_create_binary_operator(codegen, p_expression, var_op, p_stack_level, initializer, p_index_addr)) { + if (!_create_binary_operator(codegen, p_assignment->assignee, p_assignment->assigned_value, var_op, p_stack_level, false, p_index_addr)) { return -1; } @@ -232,10 +232,10 @@ int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDS return dst_addr; } -int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root, bool p_initializer, int p_index_addr) { +int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_expression, int p_stack_level, bool p_root, bool p_initializer, int p_index_addr) { switch (p_expression->type) { //should parse variable declaration and adjust stack accordingly... - case GDScriptParser::Node::TYPE_IDENTIFIER: { + case GDScriptParser::Node::IDENTIFIER: { //return identifier //wait, identifier could be a local variable or something else... careful here, must reference properly //as stack may be more interesting to work with @@ -252,6 +252,11 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: return pos | (GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS); } + // TRY LOCAL CONSTANTS! + if (codegen.local_named_constants.has(identifier)) { + return codegen.local_named_constants[identifier] | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS); + } + // TRY CLASS MEMBER if (_is_class_member_property(codegen, identifier)) { //get property @@ -264,7 +269,7 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: } //TRY MEMBERS! - if (!codegen.function_node || !codegen.function_node->_static) { + if (!codegen.function_node || !codegen.function_node->is_static) { // TRY MEMBER VARIABLES! //static function if (codegen.script->member_indices.has(identifier)) { @@ -315,6 +320,21 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: owner = owner->_owner; } + // TRY SIGNALS AND METHODS (can be made callables) + if (codegen.class_node->members_indices.has(identifier)) { + const GDScriptParser::ClassNode::Member &member = codegen.class_node->members[codegen.class_node->members_indices[identifier]]; + if (member.type == GDScriptParser::ClassNode::Member::FUNCTION || member.type == GDScriptParser::ClassNode::Member::SIGNAL) { + // Get like it was a property. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_GET_NAMED); // perform operator + codegen.opcodes.push_back(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS); // Self. + codegen.opcodes.push_back(codegen.get_name_map_pos(identifier)); // argument 2 (unary only takes one parameter) + int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); + codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode + codegen.alloc_stack(p_stack_level); + return dst_addr; + } + } + if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) { int idx = GDScriptLanguage::get_singleton()->get_global_map()[identifier]; return idx | (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root) @@ -324,11 +344,11 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: if (ScriptServer::is_global_class(identifier)) { const GDScriptParser::ClassNode *class_node = codegen.class_node; - while (class_node->owner) { - class_node = class_node->owner; + while (class_node->outer) { + class_node = class_node->outer; } - if (class_node->name == identifier) { + if (class_node->identifier && class_node->identifier->name == identifier) { _set_error("Using own name in class file is not allowed (creates a cyclic reference)", p_expression); return -1; } @@ -371,9 +391,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: return -1; } break; - case GDScriptParser::Node::TYPE_CONSTANT: { + case GDScriptParser::Node::LITERAL: { //return constant - const GDScriptParser::ConstantNode *cn = static_cast(p_expression); + const GDScriptParser::LiteralNode *cn = static_cast(p_expression); int idx; @@ -388,15 +408,15 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: return idx | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS); //argument (stack root) } break; - case GDScriptParser::Node::TYPE_SELF: { + case GDScriptParser::Node::SELF: { //return constant - if (codegen.function_node && codegen.function_node->_static) { + if (codegen.function_node && codegen.function_node->is_static) { _set_error("'self' not present in static function!", p_expression); return -1; } return (GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS); } break; - case GDScriptParser::Node::TYPE_ARRAY: { + case GDScriptParser::Node::ARRAY: { const GDScriptParser::ArrayNode *an = static_cast(p_expression); Vector values; @@ -427,23 +447,35 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: return dst_addr; } break; - case GDScriptParser::Node::TYPE_DICTIONARY: { + case GDScriptParser::Node::DICTIONARY: { const GDScriptParser::DictionaryNode *dn = static_cast(p_expression); - Vector values; + Vector elements; int slevel = p_stack_level; for (int i = 0; i < dn->elements.size(); i++) { - int ret = _parse_expression(codegen, dn->elements[i].key, slevel); - if (ret < 0) { - return ret; - } - if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { - slevel++; - codegen.alloc_stack(slevel); + // Key. + int ret = -1; + switch (dn->style) { + case GDScriptParser::DictionaryNode::PYTHON_DICT: + // Python-style: key is any expression. + ret = _parse_expression(codegen, dn->elements[i].key, slevel); + if (ret < 0) { + return ret; + } + if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { + slevel++; + codegen.alloc_stack(slevel); + } + break; + case GDScriptParser::DictionaryNode::LUA_TABLE: + // Lua-style: key is an identifier interpreted as string. + String key = static_cast(dn->elements[i].key)->name; + ret = codegen.get_constant_pos(key); + break; } - values.push_back(ret); + elements.push_back(ret); ret = _parse_expression(codegen, dn->elements[i].value, slevel); if (ret < 0) { @@ -454,13 +486,13 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: codegen.alloc_stack(slevel); } - values.push_back(ret); + elements.push_back(ret); } codegen.opcodes.push_back(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY); codegen.opcodes.push_back(dn->elements.size()); - for (int i = 0; i < values.size(); i++) { - codegen.opcodes.push_back(values[i]); + for (int i = 0; i < elements.size(); i++) { + codegen.opcodes.push_back(elements[i]); } int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); @@ -469,11 +501,11 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: return dst_addr; } break; - case GDScriptParser::Node::TYPE_CAST: { + case GDScriptParser::Node::CAST: { const GDScriptParser::CastNode *cn = static_cast(p_expression); int slevel = p_stack_level; - int src_addr = _parse_expression(codegen, cn->source_node, slevel); + int src_addr = _parse_expression(codegen, cn->operand, slevel); if (src_addr < 0) { return src_addr; } @@ -482,7 +514,8 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: codegen.alloc_stack(slevel); } - GDScriptDataType cast_type = _gdtype_from_datatype(cn->cast_type); + // FIXME: Allow actual cast. + GDScriptDataType cast_type; // = _gdtype_from_datatype(cn->cast_type); switch (cast_type.kind) { case GDScriptDataType::BUILTIN: { @@ -504,8 +537,7 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: case GDScriptDataType::SCRIPT: case GDScriptDataType::GDSCRIPT: { Variant script = cast_type.script_type; - int idx = codegen.get_constant_pos(script); - idx |= GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS; //make it a local constant (faster access) + int idx = codegen.get_constant_pos(script); //make it a local constant (faster access) codegen.opcodes.push_back(GDScriptFunction::OPCODE_CAST_TO_SCRIPT); // perform operator codegen.opcodes.push_back(idx); // variable type @@ -523,244 +555,312 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: return dst_addr; } break; - case GDScriptParser::Node::TYPE_OPERATOR: { - //hell breaks loose + //hell breaks loose - const GDScriptParser::OperatorNode *on = static_cast(p_expression); - switch (on->op) { - //call/constructor operator - case GDScriptParser::OperatorNode::OP_PARENT_CALL: { - ERR_FAIL_COND_V(on->arguments.size() < 1, -1); +#define OPERATOR_RETURN \ + int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); \ + codegen.opcodes.push_back(dst_addr); \ + codegen.alloc_stack(p_stack_level); \ + return dst_addr - const GDScriptParser::IdentifierNode *in = (const GDScriptParser::IdentifierNode *)on->arguments[0]; + case GDScriptParser::Node::CALL: { + const GDScriptParser::CallNode *call = static_cast(p_expression); + if (!call->is_super && call->callee->type == GDScriptParser::Node::IDENTIFIER && GDScriptParser::get_builtin_type(static_cast(call->callee)->name) != Variant::VARIANT_MAX) { + //construct a basic type - Vector arguments; - int slevel = p_stack_level; - for (int i = 1; i < on->arguments.size(); i++) { - int ret = _parse_expression(codegen, on->arguments[i], slevel); - if (ret < 0) { - return ret; - } - if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { - slevel++; - codegen.alloc_stack(slevel); - } - arguments.push_back(ret); + Variant::Type vtype = GDScriptParser::get_builtin_type(static_cast(call->callee)->name); + + Vector arguments; + int slevel = p_stack_level; + for (int i = 0; i < call->arguments.size(); i++) { + int ret = _parse_expression(codegen, call->arguments[i], slevel); + if (ret < 0) { + return ret; + } + if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { + slevel++; + codegen.alloc_stack(slevel); + } + arguments.push_back(ret); + } + + //push call bytecode + codegen.opcodes.push_back(GDScriptFunction::OPCODE_CONSTRUCT); // basic type constructor + codegen.opcodes.push_back(vtype); //instance + codegen.opcodes.push_back(arguments.size()); //argument count + codegen.alloc_call(arguments.size()); + for (int i = 0; i < arguments.size(); i++) { + codegen.opcodes.push_back(arguments[i]); //arguments + } + + } else if (!call->is_super && call->callee->type == GDScriptParser::Node::IDENTIFIER && GDScriptParser::get_builtin_function(static_cast(call->callee)->name) != GDScriptFunctions::FUNC_MAX) { + //built in function + + Vector arguments; + int slevel = p_stack_level; + for (int i = 0; i < call->arguments.size(); i++) { + int ret = _parse_expression(codegen, call->arguments[i], slevel); + if (ret < 0) { + return ret; } - //push call bytecode - codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_SELF_BASE); // basic type constructor - - codegen.opcodes.push_back(codegen.get_name_map_pos(in->name)); //instance - codegen.opcodes.push_back(arguments.size()); //argument count - codegen.alloc_call(arguments.size()); - for (int i = 0; i < arguments.size(); i++) { - codegen.opcodes.push_back(arguments[i]); //arguments + if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { + slevel++; + codegen.alloc_stack(slevel); } - } break; - case GDScriptParser::OperatorNode::OP_CALL: { - if (on->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) { - //construct a basic type - ERR_FAIL_COND_V(on->arguments.size() < 1, -1); + arguments.push_back(ret); + } - const GDScriptParser::TypeNode *tn = (const GDScriptParser::TypeNode *)on->arguments[0]; - int vtype = tn->vtype; + codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN); + codegen.opcodes.push_back(GDScriptParser::get_builtin_function(static_cast(call->callee)->name)); + codegen.opcodes.push_back(arguments.size()); + codegen.alloc_call(arguments.size()); + for (int i = 0; i < arguments.size(); i++) { + codegen.opcodes.push_back(arguments[i]); + } - Vector arguments; - int slevel = p_stack_level; - for (int i = 1; i < on->arguments.size(); i++) { - int ret = _parse_expression(codegen, on->arguments[i], slevel); - if (ret < 0) { - return ret; - } - if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { - slevel++; - codegen.alloc_stack(slevel); - } - arguments.push_back(ret); - } + } else { + //regular function - //push call bytecode - codegen.opcodes.push_back(GDScriptFunction::OPCODE_CONSTRUCT); // basic type constructor - codegen.opcodes.push_back(vtype); //instance - codegen.opcodes.push_back(arguments.size()); //argument count - codegen.alloc_call(arguments.size()); - for (int i = 0; i < arguments.size(); i++) { - codegen.opcodes.push_back(arguments[i]); //arguments - } + const GDScriptParser::ExpressionNode *callee = call->callee; - } else if (on->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) { - //built in function - - ERR_FAIL_COND_V(on->arguments.size() < 1, -1); - - Vector arguments; - int slevel = p_stack_level; - for (int i = 1; i < on->arguments.size(); i++) { - int ret = _parse_expression(codegen, on->arguments[i], slevel); - if (ret < 0) { - return ret; - } - - if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { - slevel++; - codegen.alloc_stack(slevel); - } - - arguments.push_back(ret); - } - - codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN); - codegen.opcodes.push_back(static_cast(on->arguments[0])->function); - codegen.opcodes.push_back(on->arguments.size() - 1); - codegen.alloc_call(on->arguments.size() - 1); - for (int i = 0; i < arguments.size(); i++) { - codegen.opcodes.push_back(arguments[i]); - } + Vector arguments; + int slevel = p_stack_level; + // TODO: Use callables when possible if needed. + int ret = -1; + int super_address = -1; + if (call->is_super) { + // Super call. + if (call->callee == nullptr) { + // Implicit super function call. + super_address = codegen.get_name_map_pos(codegen.function_node->identifier->name); } else { - //regular function - ERR_FAIL_COND_V(on->arguments.size() < 2, -1); - - const GDScriptParser::Node *instance = on->arguments[0]; - - if (instance->type == GDScriptParser::Node::TYPE_SELF) { - //room for optimization - } - - Vector arguments; - int slevel = p_stack_level; - - for (int i = 0; i < on->arguments.size(); i++) { - int ret; - - if (i == 0 && on->arguments[i]->type == GDScriptParser::Node::TYPE_SELF && codegen.function_node && codegen.function_node->_static) { - //static call to self - ret = (GDScriptFunction::ADDR_TYPE_CLASS << GDScriptFunction::ADDR_BITS); - } else if (i == 1) { - if (on->arguments[i]->type != GDScriptParser::Node::TYPE_IDENTIFIER) { - _set_error("Attempt to call a non-identifier.", on); - return -1; - } - GDScriptParser::IdentifierNode *id = static_cast(on->arguments[i]); - ret = codegen.get_name_map_pos(id->name); - - } else { - ret = _parse_expression(codegen, on->arguments[i], slevel); - if (ret < 0) { - return ret; - } - if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { - slevel++; - codegen.alloc_stack(slevel); - } - } - arguments.push_back(ret); - } - - codegen.opcodes.push_back(p_root ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN); // perform operator - codegen.opcodes.push_back(on->arguments.size() - 2); - codegen.alloc_call(on->arguments.size() - 2); - for (int i = 0; i < arguments.size(); i++) { - codegen.opcodes.push_back(arguments[i]); - } + super_address = codegen.get_name_map_pos(static_cast(call->callee)->name); } - } break; - case GDScriptParser::OperatorNode::OP_YIELD: { - ERR_FAIL_COND_V(on->arguments.size() && on->arguments.size() != 2, -1); - - Vector arguments; - int slevel = p_stack_level; - for (int i = 0; i < on->arguments.size(); i++) { - int ret = _parse_expression(codegen, on->arguments[i], slevel); - if (ret < 0) { - return ret; - } - if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { - slevel++; - codegen.alloc_stack(slevel); + } else { + if (callee->type == GDScriptParser::Node::IDENTIFIER) { + // Self function call. + if (codegen.function_node && codegen.function_node->is_static) { + ret = (GDScriptFunction::ADDR_TYPE_CLASS << GDScriptFunction::ADDR_BITS); + } else { + ret = (GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS); } arguments.push_back(ret); + ret = codegen.get_name_map_pos(static_cast(call->callee)->name); + arguments.push_back(ret); + } else if (callee->type == GDScriptParser::Node::SUBSCRIPT) { + const GDScriptParser::SubscriptNode *subscript = static_cast(call->callee); + + if (subscript->is_attribute) { + ret = _parse_expression(codegen, subscript->base, slevel); + if (ret < 0) { + return ret; + } + if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { + slevel++; + codegen.alloc_stack(slevel); + } + arguments.push_back(ret); + arguments.push_back(codegen.get_name_map_pos(subscript->attribute->name)); + } else { + // TODO: Validate this at parse time. + // TODO: Though might not be the case if callables are a thing. + _set_error("Cannot call something that isn't a function.", call->callee); + return -1; + } + } else { + _set_error("Cannot call something that isn't a function.", call->callee); + return -1; } + } - //push call bytecode - codegen.opcodes.push_back(arguments.size() == 0 ? GDScriptFunction::OPCODE_YIELD : GDScriptFunction::OPCODE_YIELD_SIGNAL); // basic type constructor - for (int i = 0; i < arguments.size(); i++) { - codegen.opcodes.push_back(arguments[i]); //arguments + for (int i = 0; i < call->arguments.size(); i++) { + ret = _parse_expression(codegen, call->arguments[i], slevel); + if (ret < 0) { + return ret; } - codegen.opcodes.push_back(GDScriptFunction::OPCODE_YIELD_RESUME); - //next will be where to place the result :) - - } break; - - //indexing operator - case GDScriptParser::OperatorNode::OP_INDEX: - case GDScriptParser::OperatorNode::OP_INDEX_NAMED: { - ERR_FAIL_COND_V(on->arguments.size() != 2, -1); - - int slevel = p_stack_level; - bool named = (on->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED); - - int from = _parse_expression(codegen, on->arguments[0], slevel); - if (from < 0) { - return from; + if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { + slevel++; + codegen.alloc_stack(slevel); } + arguments.push_back(ret); + } - int index; - if (p_index_addr != 0) { - index = p_index_addr; - } else if (named) { - if (on->arguments[0]->type == GDScriptParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) { - GDScriptParser::IdentifierNode *identifier = static_cast(on->arguments[1]); - const Map::Element *MI = codegen.script->member_indices.find(identifier->name); + int opcode = GDScriptFunction::OPCODE_CALL_RETURN; + if (call->is_super) { + opcode = GDScriptFunction::OPCODE_CALL_SELF_BASE; + } else if (within_await) { + opcode = GDScriptFunction::OPCODE_CALL_ASYNC; + } else if (p_root) { + opcode = GDScriptFunction::OPCODE_CALL; + } + + codegen.opcodes.push_back(opcode); // perform operator + if (call->is_super) { + codegen.opcodes.push_back(super_address); + } + codegen.opcodes.push_back(call->arguments.size()); + codegen.alloc_call(call->arguments.size()); + for (int i = 0; i < arguments.size(); i++) { + codegen.opcodes.push_back(arguments[i]); + } + } + OPERATOR_RETURN; + } break; + case GDScriptParser::Node::GET_NODE: { + const GDScriptParser::GetNodeNode *get_node = static_cast(p_expression); + + String node_name; + if (get_node->string != nullptr) { + node_name += String(get_node->string->value); + } else { + for (int i = 0; i < get_node->chain.size(); i++) { + if (i > 0) { + node_name += "/"; + } + node_name += get_node->chain[i]->name; + } + } + + int arg_address = codegen.get_constant_pos(NodePath(node_name)); + + codegen.opcodes.push_back(p_root ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN); + codegen.opcodes.push_back(1); // number of arguments. + codegen.alloc_call(1); + codegen.opcodes.push_back(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS); // self. + codegen.opcodes.push_back(codegen.get_name_map_pos("get_node")); // function. + codegen.opcodes.push_back(arg_address); // argument (NodePath). + OPERATOR_RETURN; + } break; + case GDScriptParser::Node::PRELOAD: { + const GDScriptParser::PreloadNode *preload = static_cast(p_expression); + + // Add resource as constant. + return codegen.get_constant_pos(preload->resource); + } break; + case GDScriptParser::Node::AWAIT: { + const GDScriptParser::AwaitNode *await = static_cast(p_expression); + + int slevel = p_stack_level; + within_await = true; + int argument = _parse_expression(codegen, await->to_await, slevel); + within_await = false; + if (argument < 0) { + return argument; + } + if ((argument >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { + slevel++; + codegen.alloc_stack(slevel); + } + //push call bytecode + codegen.opcodes.push_back(GDScriptFunction::OPCODE_AWAIT); + codegen.opcodes.push_back(argument); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_AWAIT_RESUME); + //next will be where to place the result :) + + OPERATOR_RETURN; + } break; + + //indexing operator + case GDScriptParser::Node::SUBSCRIPT: { + int slevel = p_stack_level; + + const GDScriptParser::SubscriptNode *subscript = static_cast(p_expression); + + int from = _parse_expression(codegen, subscript->base, slevel); + if (from < 0) { + return from; + } + + bool named = subscript->is_attribute; + int index; + if (p_index_addr != 0) { + index = p_index_addr; + } else if (subscript->is_attribute) { + if (subscript->base->type == GDScriptParser::Node::SELF && codegen.script && codegen.function_node && !codegen.function_node->is_static) { + GDScriptParser::IdentifierNode *identifier = subscript->attribute; + const Map::Element *MI = codegen.script->member_indices.find(identifier->name); #ifdef DEBUG_ENABLED - if (MI && MI->get().getter == codegen.function_node->name) { - String n = static_cast(on->arguments[1])->name; - _set_error("Must use '" + n + "' instead of 'self." + n + "' in getter.", on); - return -1; - } + if (MI && MI->get().getter == codegen.function_node->identifier->name) { + String n = identifier->name; + _set_error("Must use '" + n + "' instead of 'self." + n + "' in getter.", identifier); + return -1; + } #endif - if (MI && MI->get().getter == "") { - // Faster than indexing self (as if no self. had been used) - return (MI->get().index) | (GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS); - } - } + if (MI && MI->get().getter == "") { + // Faster than indexing self (as if no self. had been used) + return (MI->get().index) | (GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS); + } + } - index = codegen.get_name_map_pos(static_cast(on->arguments[1])->name); + index = codegen.get_name_map_pos(subscript->attribute->name); - } else { - if (on->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT && static_cast(on->arguments[1])->value.get_type() == Variant::STRING) { - //also, somehow, named (speed up anyway) - StringName name = static_cast(on->arguments[1])->value; - index = codegen.get_name_map_pos(name); - named = true; + } else { + if (subscript->index->type == GDScriptParser::Node::LITERAL && static_cast(subscript->index)->value.get_type() == Variant::STRING) { + //also, somehow, named (speed up anyway) + StringName name = static_cast(subscript->index)->value; + index = codegen.get_name_map_pos(name); + named = true; - } else { - //regular indexing - if (from & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) { - slevel++; - codegen.alloc_stack(slevel); - } - - index = _parse_expression(codegen, on->arguments[1], slevel); - if (index < 0) { - return index; - } - } + } else { + //regular indexing + if (from & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) { + slevel++; + codegen.alloc_stack(slevel); } - codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_GET_NAMED : GDScriptFunction::OPCODE_GET); // perform operator - codegen.opcodes.push_back(from); // argument 1 - codegen.opcodes.push_back(index); // argument 2 (unary only takes one parameter) + index = _parse_expression(codegen, subscript->index, slevel); + if (index < 0) { + return index; + } + } + } + codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_GET_NAMED : GDScriptFunction::OPCODE_GET); // perform operator + codegen.opcodes.push_back(from); // argument 1 + codegen.opcodes.push_back(index); // argument 2 (unary only takes one parameter) + OPERATOR_RETURN; + } break; + case GDScriptParser::Node::UNARY_OPERATOR: { + //unary operators + const GDScriptParser::UnaryOpNode *unary = static_cast(p_expression); + switch (unary->operation) { + case GDScriptParser::UnaryOpNode::OP_NEGATIVE: { + if (!_create_unary_operator(codegen, unary, Variant::OP_NEGATE, p_stack_level)) { + return -1; + } } break; - case GDScriptParser::OperatorNode::OP_AND: { + case GDScriptParser::UnaryOpNode::OP_POSITIVE: { + if (!_create_unary_operator(codegen, unary, Variant::OP_POSITIVE, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::UnaryOpNode::OP_LOGIC_NOT: { + if (!_create_unary_operator(codegen, unary, Variant::OP_NOT, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::UnaryOpNode::OP_COMPLEMENT: { + if (!_create_unary_operator(codegen, unary, Variant::OP_BIT_NEGATE, p_stack_level)) { + return -1; + } + } break; + } + OPERATOR_RETURN; + } + case GDScriptParser::Node::BINARY_OPERATOR: { + //binary operators (in precedence order) + const GDScriptParser::BinaryOpNode *binary = static_cast(p_expression); + + switch (binary->operation) { + case GDScriptParser::BinaryOpNode::OP_LOGIC_AND: { // AND operator with early out on failure - int res = _parse_expression(codegen, on->arguments[0], p_stack_level); + int res = _parse_expression(codegen, binary->left_operand, p_stack_level); if (res < 0) { return res; } @@ -769,7 +869,7 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: int jump_fail_pos = codegen.opcodes.size(); codegen.opcodes.push_back(0); - res = _parse_expression(codegen, on->arguments[1], p_stack_level); + res = _parse_expression(codegen, binary->right_operand, p_stack_level); if (res < 0) { return res; } @@ -791,10 +891,10 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; } break; - case GDScriptParser::OperatorNode::OP_OR: { + case GDScriptParser::BinaryOpNode::OP_LOGIC_OR: { // OR operator with early out on success - int res = _parse_expression(codegen, on->arguments[0], p_stack_level); + int res = _parse_expression(codegen, binary->left_operand, p_stack_level); if (res < 0) { return res; } @@ -803,7 +903,7 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: int jump_success_pos = codegen.opcodes.size(); codegen.opcodes.push_back(0); - res = _parse_expression(codegen, on->arguments[1], p_stack_level); + res = _parse_expression(codegen, binary->right_operand, p_stack_level); if (res < 0) { return res; } @@ -825,429 +925,10 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; } break; - // ternary operators - case GDScriptParser::OperatorNode::OP_TERNARY_IF: { - // x IF a ELSE y operator with early out on failure - - int res = _parse_expression(codegen, on->arguments[0], p_stack_level); - if (res < 0) { - return res; - } - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); - codegen.opcodes.push_back(res); - int jump_fail_pos = codegen.opcodes.size(); - codegen.opcodes.push_back(0); - - res = _parse_expression(codegen, on->arguments[1], p_stack_level); - if (res < 0) { - return res; - } - - codegen.alloc_stack(p_stack_level); //it will be used.. - codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); - codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); - codegen.opcodes.push_back(res); - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - int jump_past_pos = codegen.opcodes.size(); - codegen.opcodes.push_back(0); - - codegen.opcodes.write[jump_fail_pos] = codegen.opcodes.size(); - res = _parse_expression(codegen, on->arguments[2], p_stack_level); - if (res < 0) { - return res; - } - - codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); - codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); - codegen.opcodes.push_back(res); - - codegen.opcodes.write[jump_past_pos] = codegen.opcodes.size(); - - return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; - - } break; - //unary operators - case GDScriptParser::OperatorNode::OP_NEG: { - if (!_create_unary_operator(codegen, on, Variant::OP_NEGATE, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_POS: { - if (!_create_unary_operator(codegen, on, Variant::OP_POSITIVE, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_NOT: { - if (!_create_unary_operator(codegen, on, Variant::OP_NOT, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_BIT_INVERT: { - if (!_create_unary_operator(codegen, on, Variant::OP_BIT_NEGATE, p_stack_level)) { - return -1; - } - } break; - //binary operators (in precedence order) - case GDScriptParser::OperatorNode::OP_IN: { - if (!_create_binary_operator(codegen, on, Variant::OP_IN, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_EQUAL: { - if (!_create_binary_operator(codegen, on, Variant::OP_EQUAL, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_NOT_EQUAL: { - if (!_create_binary_operator(codegen, on, Variant::OP_NOT_EQUAL, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_LESS: { - if (!_create_binary_operator(codegen, on, Variant::OP_LESS, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_LESS_EQUAL: { - if (!_create_binary_operator(codegen, on, Variant::OP_LESS_EQUAL, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_GREATER: { - if (!_create_binary_operator(codegen, on, Variant::OP_GREATER, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_GREATER_EQUAL: { - if (!_create_binary_operator(codegen, on, Variant::OP_GREATER_EQUAL, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_ADD: { - if (!_create_binary_operator(codegen, on, Variant::OP_ADD, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_SUB: { - if (!_create_binary_operator(codegen, on, Variant::OP_SUBTRACT, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_MUL: { - if (!_create_binary_operator(codegen, on, Variant::OP_MULTIPLY, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_DIV: { - if (!_create_binary_operator(codegen, on, Variant::OP_DIVIDE, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_MOD: { - if (!_create_binary_operator(codegen, on, Variant::OP_MODULE, p_stack_level)) { - return -1; - } - } break; - //case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break; - //case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break; - case GDScriptParser::OperatorNode::OP_BIT_AND: { - if (!_create_binary_operator(codegen, on, Variant::OP_BIT_AND, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_BIT_OR: { - if (!_create_binary_operator(codegen, on, Variant::OP_BIT_OR, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_BIT_XOR: { - if (!_create_binary_operator(codegen, on, Variant::OP_BIT_XOR, p_stack_level)) { - return -1; - } - } break; - //shift - case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: { - if (!_create_binary_operator(codegen, on, Variant::OP_SHIFT_LEFT, p_stack_level)) { - return -1; - } - } break; - case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: { - if (!_create_binary_operator(codegen, on, Variant::OP_SHIFT_RIGHT, p_stack_level)) { - return -1; - } - } break; - //assignment operators - case GDScriptParser::OperatorNode::OP_ASSIGN_ADD: - case GDScriptParser::OperatorNode::OP_ASSIGN_SUB: - case GDScriptParser::OperatorNode::OP_ASSIGN_MUL: - case GDScriptParser::OperatorNode::OP_ASSIGN_DIV: - case GDScriptParser::OperatorNode::OP_ASSIGN_MOD: - case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT: - case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_AND: - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_OR: - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR: - case GDScriptParser::OperatorNode::OP_INIT_ASSIGN: - case GDScriptParser::OperatorNode::OP_ASSIGN: { - ERR_FAIL_COND_V(on->arguments.size() != 2, -1); - - if (on->arguments[0]->type == GDScriptParser::Node::TYPE_OPERATOR && (static_cast(on->arguments[0])->op == GDScriptParser::OperatorNode::OP_INDEX || static_cast(on->arguments[0])->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED)) { - // SET (chained) MODE! -#ifdef DEBUG_ENABLED - if (static_cast(on->arguments[0])->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) { - const GDScriptParser::OperatorNode *inon = static_cast(on->arguments[0]); - - if (inon->arguments[0]->type == GDScriptParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) { - const Map::Element *MI = codegen.script->member_indices.find(static_cast(inon->arguments[1])->name); - if (MI && MI->get().setter == codegen.function_node->name) { - String n = static_cast(inon->arguments[1])->name; - _set_error("Must use '" + n + "' instead of 'self." + n + "' in setter.", inon); - return -1; - } - } - } -#endif - - int slevel = p_stack_level; - - GDScriptParser::OperatorNode *op = static_cast(on->arguments[0]); - - /* Find chain of sets */ - - StringName assign_property; - - List chain; - - { - //create get/set chain - GDScriptParser::OperatorNode *n = op; - while (true) { - chain.push_back(n); - if (n->arguments[0]->type != GDScriptParser::Node::TYPE_OPERATOR) { - //check for a built-in property - if (n->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) { - GDScriptParser::IdentifierNode *identifier = static_cast(n->arguments[0]); - if (_is_class_member_property(codegen, identifier->name)) { - assign_property = identifier->name; - } - } - break; - } - n = static_cast(n->arguments[0]); - if (n->op != GDScriptParser::OperatorNode::OP_INDEX && n->op != GDScriptParser::OperatorNode::OP_INDEX_NAMED) { - break; - } - } - } - - /* Chain of gets */ - - //get at (potential) root stack pos, so it can be returned - int prev_pos = _parse_expression(codegen, chain.back()->get()->arguments[0], slevel); - if (prev_pos < 0) { - return prev_pos; - } - int retval = prev_pos; - - if (retval & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) { - slevel++; - codegen.alloc_stack(slevel); - } - - Vector setchain; - - if (assign_property != StringName()) { - // recover and assign at the end, this allows stuff like - // position.x+=2.0 - // in Node2D - setchain.push_back(prev_pos); - setchain.push_back(codegen.get_name_map_pos(assign_property)); - setchain.push_back(GDScriptFunction::OPCODE_SET_MEMBER); - } - - for (List::Element *E = chain.back(); E; E = E->prev()) { - if (E == chain.front()) { //ignore first - break; - } - - bool named = E->get()->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED; - int key_idx; - - if (named) { - key_idx = codegen.get_name_map_pos(static_cast(E->get()->arguments[1])->name); - //printf("named key %x\n",key_idx); - - } else { - if (prev_pos & (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS)) { - slevel++; - codegen.alloc_stack(slevel); - } - - GDScriptParser::Node *key = E->get()->arguments[1]; - key_idx = _parse_expression(codegen, key, slevel); - //printf("expr key %x\n",key_idx); - - //stack was raised here if retval was stack but.. - } - - if (key_idx < 0) { //error - return key_idx; - } - - codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_GET_NAMED : GDScriptFunction::OPCODE_GET); - codegen.opcodes.push_back(prev_pos); - codegen.opcodes.push_back(key_idx); - slevel++; - codegen.alloc_stack(slevel); - int dst_pos = (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) | slevel; - - codegen.opcodes.push_back(dst_pos); - - //add in reverse order, since it will be reverted - - setchain.push_back(dst_pos); - setchain.push_back(key_idx); - setchain.push_back(prev_pos); - setchain.push_back(named ? GDScriptFunction::OPCODE_SET_NAMED : GDScriptFunction::OPCODE_SET); - - prev_pos = dst_pos; - } - - setchain.invert(); - - int set_index; - bool named = false; - - if (op->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) { - set_index = codegen.get_name_map_pos(static_cast(op->arguments[1])->name); - named = true; - } else { - set_index = _parse_expression(codegen, op->arguments[1], slevel + 1); - named = false; - } - - if (set_index < 0) { //error - return set_index; - } - - if (set_index & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) { - slevel++; - codegen.alloc_stack(slevel); - } - - int set_value = _parse_assign_right_expression(codegen, on, slevel + 1, named ? 0 : set_index); - if (set_value < 0) { //error - return set_value; - } - - codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_SET_NAMED : GDScriptFunction::OPCODE_SET); - codegen.opcodes.push_back(prev_pos); - codegen.opcodes.push_back(set_index); - codegen.opcodes.push_back(set_value); - - for (int i = 0; i < setchain.size(); i++) { - codegen.opcodes.push_back(setchain[i]); - } - - return retval; - - } else if (on->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && _is_class_member_property(codegen, static_cast(on->arguments[0])->name)) { - //assignment to member property - - int slevel = p_stack_level; - - int src_address = _parse_assign_right_expression(codegen, on, slevel); - if (src_address < 0) { - return -1; - } - - StringName name = static_cast(on->arguments[0])->name; - - codegen.opcodes.push_back(GDScriptFunction::OPCODE_SET_MEMBER); - codegen.opcodes.push_back(codegen.get_name_map_pos(name)); - codegen.opcodes.push_back(src_address); - - return GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS; - } else { - //REGULAR ASSIGNMENT MODE!! - - int slevel = p_stack_level; - - int dst_address_a = _parse_expression(codegen, on->arguments[0], slevel, false, on->op == GDScriptParser::OperatorNode::OP_INIT_ASSIGN); - if (dst_address_a < 0) { - return -1; - } - - if (dst_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) { - slevel++; - codegen.alloc_stack(slevel); - } - - int src_address_b = _parse_assign_right_expression(codegen, on, slevel); - if (src_address_b < 0) { - return -1; - } - - GDScriptDataType assign_type = _gdtype_from_datatype(on->arguments[0]->get_datatype()); - - if (assign_type.has_type && !on->datatype.has_type) { - // Typed assignment - switch (assign_type.kind) { - case GDScriptDataType::BUILTIN: { - codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN); // perform operator - codegen.opcodes.push_back(assign_type.builtin_type); // variable type - codegen.opcodes.push_back(dst_address_a); // argument 1 - codegen.opcodes.push_back(src_address_b); // argument 2 - } break; - case GDScriptDataType::NATIVE: { - int class_idx; - if (GDScriptLanguage::get_singleton()->get_global_map().has(assign_type.native_type)) { - class_idx = GDScriptLanguage::get_singleton()->get_global_map()[assign_type.native_type]; - class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root) - } else { - _set_error("Invalid native class type '" + String(assign_type.native_type) + "'.", on->arguments[0]); - return -1; - } - codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE); // perform operator - codegen.opcodes.push_back(class_idx); // variable type - codegen.opcodes.push_back(dst_address_a); // argument 1 - codegen.opcodes.push_back(src_address_b); // argument 2 - } break; - case GDScriptDataType::SCRIPT: - case GDScriptDataType::GDSCRIPT: { - Variant script = assign_type.script_type; - int idx = codegen.get_constant_pos(script); - idx |= GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS; //make it a local constant (faster access) - - codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT); // perform operator - codegen.opcodes.push_back(idx); // variable type - codegen.opcodes.push_back(dst_address_a); // argument 1 - codegen.opcodes.push_back(src_address_b); // argument 2 - } break; - default: { - ERR_PRINT("Compiler bug: unresolved assign."); - - // Shouldn't get here, but fail-safe to a regular assignment - codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); // perform operator - codegen.opcodes.push_back(dst_address_a); // argument 1 - codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter) - } - } - } else { - // Either untyped assignment or already type-checked by the parser - codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); // perform operator - codegen.opcodes.push_back(dst_address_a); // argument 1 - codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter) - } - return dst_address_a; //if anything, returns wathever was assigned or correct stack position - } - } break; - case GDScriptParser::OperatorNode::OP_IS: { - ERR_FAIL_COND_V(on->arguments.size() != 2, false); - + case GDScriptParser::BinaryOpNode::OP_TYPE_TEST: { int slevel = p_stack_level; - int src_address_a = _parse_expression(codegen, on->arguments[0], slevel); + int src_address_a = _parse_expression(codegen, binary->left_operand, slevel); if (src_address_a < 0) { return -1; } @@ -1256,48 +937,403 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: slevel++; //uses stack for return, increase stack } - int src_address_b = _parse_expression(codegen, on->arguments[1], slevel); - if (src_address_b < 0) { - return -1; + int src_address_b = -1; + bool builtin = false; + if (binary->right_operand->type == GDScriptParser::Node::IDENTIFIER && GDScriptParser::get_builtin_type(static_cast(binary->right_operand)->name) != Variant::VARIANT_MAX) { + // `is` with builtin type + builtin = true; + src_address_b = (int)GDScriptParser::get_builtin_type(static_cast(binary->right_operand)->name); + } else { + src_address_b = _parse_expression(codegen, binary->right_operand, slevel); + if (src_address_b < 0) { + return -1; + } } - codegen.opcodes.push_back(GDScriptFunction::OPCODE_EXTENDS_TEST); // perform operator + codegen.opcodes.push_back(builtin ? GDScriptFunction::OPCODE_IS_BUILTIN : GDScriptFunction::OPCODE_EXTENDS_TEST); // perform operator codegen.opcodes.push_back(src_address_a); // argument 1 codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter) } break; - case GDScriptParser::OperatorNode::OP_IS_BUILTIN: { - ERR_FAIL_COND_V(on->arguments.size() != 2, false); - ERR_FAIL_COND_V(on->arguments[1]->type != GDScriptParser::Node::TYPE_TYPE, false); - - int slevel = p_stack_level; - - int src_address_a = _parse_expression(codegen, on->arguments[0], slevel); - if (src_address_a < 0) { + case GDScriptParser::BinaryOpNode::OP_CONTENT_TEST: { + if (!_create_binary_operator(codegen, binary, Variant::OP_IN, p_stack_level)) { return -1; } - - if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) { - slevel++; //uses stack for return, increase stack - } - - const GDScriptParser::TypeNode *tn = static_cast(on->arguments[1]); - - codegen.opcodes.push_back(GDScriptFunction::OPCODE_IS_BUILTIN); // perform operator - codegen.opcodes.push_back(src_address_a); // argument 1 - codegen.opcodes.push_back((int)tn->vtype); // argument 2 (unary only takes one parameter) } break; - default: { - ERR_FAIL_V_MSG(0, "Bug in bytecode compiler, unexpected operator #" + itos(on->op) + " in parse tree while parsing expression."); //unreachable code - + case GDScriptParser::BinaryOpNode::OP_COMP_EQUAL: { + if (!_create_binary_operator(codegen, binary, Variant::OP_EQUAL, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_COMP_NOT_EQUAL: { + if (!_create_binary_operator(codegen, binary, Variant::OP_NOT_EQUAL, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_COMP_LESS: { + if (!_create_binary_operator(codegen, binary, Variant::OP_LESS, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_COMP_LESS_EQUAL: { + if (!_create_binary_operator(codegen, binary, Variant::OP_LESS_EQUAL, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_COMP_GREATER: { + if (!_create_binary_operator(codegen, binary, Variant::OP_GREATER, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_COMP_GREATER_EQUAL: { + if (!_create_binary_operator(codegen, binary, Variant::OP_GREATER_EQUAL, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_ADDITION: { + if (!_create_binary_operator(codegen, binary, Variant::OP_ADD, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_SUBTRACTION: { + if (!_create_binary_operator(codegen, binary, Variant::OP_SUBTRACT, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_MULTIPLICATION: { + if (!_create_binary_operator(codegen, binary, Variant::OP_MULTIPLY, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_DIVISION: { + if (!_create_binary_operator(codegen, binary, Variant::OP_DIVIDE, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_MODULO: { + if (!_create_binary_operator(codegen, binary, Variant::OP_MODULE, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_BIT_AND: { + if (!_create_binary_operator(codegen, binary, Variant::OP_BIT_AND, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_BIT_OR: { + if (!_create_binary_operator(codegen, binary, Variant::OP_BIT_OR, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_BIT_XOR: { + if (!_create_binary_operator(codegen, binary, Variant::OP_BIT_XOR, p_stack_level)) { + return -1; + } + } break; + //shift + case GDScriptParser::BinaryOpNode::OP_BIT_LEFT_SHIFT: { + if (!_create_binary_operator(codegen, binary, Variant::OP_SHIFT_LEFT, p_stack_level)) { + return -1; + } + } break; + case GDScriptParser::BinaryOpNode::OP_BIT_RIGHT_SHIFT: { + if (!_create_binary_operator(codegen, binary, Variant::OP_SHIFT_RIGHT, p_stack_level)) { + return -1; + } } break; } - - int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); - codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode - codegen.alloc_stack(p_stack_level); - return dst_addr; + OPERATOR_RETURN; } break; + // ternary operators + case GDScriptParser::Node::TERNARY_OPERATOR: { + // x IF a ELSE y operator with early out on failure + + const GDScriptParser::TernaryOpNode *ternary = static_cast(p_expression); + int res = _parse_expression(codegen, ternary->condition, p_stack_level); + if (res < 0) { + return res; + } + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(res); + int jump_fail_pos = codegen.opcodes.size(); + codegen.opcodes.push_back(0); + + res = _parse_expression(codegen, ternary->true_expr, p_stack_level); + if (res < 0) { + return res; + } + + codegen.alloc_stack(p_stack_level); //it will be used.. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); + codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); + codegen.opcodes.push_back(res); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + int jump_past_pos = codegen.opcodes.size(); + codegen.opcodes.push_back(0); + + codegen.opcodes.write[jump_fail_pos] = codegen.opcodes.size(); + res = _parse_expression(codegen, ternary->false_expr, p_stack_level); + if (res < 0) { + return res; + } + + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); + codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); + codegen.opcodes.push_back(res); + + codegen.opcodes.write[jump_past_pos] = codegen.opcodes.size(); + + return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; + + } break; + //assignment operators + case GDScriptParser::Node::ASSIGNMENT: { + const GDScriptParser::AssignmentNode *assignment = static_cast(p_expression); + + if (assignment->assignee->type == GDScriptParser::Node::SUBSCRIPT) { + // SET (chained) MODE! + const GDScriptParser::SubscriptNode *subscript = static_cast(assignment->assignee); +#ifdef DEBUG_ENABLED + if (subscript->is_attribute) { + if (subscript->base->type == GDScriptParser::Node::SELF && codegen.script && codegen.function_node && !codegen.function_node->is_static) { + const Map::Element *MI = codegen.script->member_indices.find(subscript->attribute->name); + if (MI && MI->get().setter == codegen.function_node->identifier->name) { + String n = subscript->attribute->name; + _set_error("Must use '" + n + "' instead of 'self." + n + "' in setter.", subscript); + return -1; + } + } + } +#endif + + int slevel = p_stack_level; + + /* Find chain of sets */ + + StringName assign_property; + + List chain; + + { + //create get/set chain + const GDScriptParser::SubscriptNode *n = subscript; + while (true) { + chain.push_back(n); + + if (n->base->type != GDScriptParser::Node::SUBSCRIPT) { + //check for a built-in property + if (n->base->type == GDScriptParser::Node::IDENTIFIER) { + GDScriptParser::IdentifierNode *identifier = static_cast(n->base); + if (_is_class_member_property(codegen, identifier->name)) { + assign_property = identifier->name; + } + } + break; + } + n = static_cast(n->base); + } + } + + /* Chain of gets */ + + //get at (potential) root stack pos, so it can be returned + int prev_pos = _parse_expression(codegen, chain.back()->get()->base, slevel); + if (prev_pos < 0) { + return prev_pos; + } + int retval = prev_pos; + + if (retval & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) { + slevel++; + codegen.alloc_stack(slevel); + } + + Vector setchain; + + if (assign_property != StringName()) { + // recover and assign at the end, this allows stuff like + // position.x+=2.0 + // in Node2D + setchain.push_back(prev_pos); + setchain.push_back(codegen.get_name_map_pos(assign_property)); + setchain.push_back(GDScriptFunction::OPCODE_SET_MEMBER); + } + + for (List::Element *E = chain.back(); E; E = E->prev()) { + if (E == chain.front()) { //ignore first + break; + } + + const GDScriptParser::SubscriptNode *subscript_elem = E->get(); + int key_idx; + + if (subscript_elem->is_attribute) { + key_idx = codegen.get_name_map_pos(subscript_elem->attribute->name); + //printf("named key %x\n",key_idx); + + } else { + if (prev_pos & (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS)) { + slevel++; + codegen.alloc_stack(slevel); + } + + GDScriptParser::ExpressionNode *key = subscript_elem->index; + key_idx = _parse_expression(codegen, key, slevel); + //printf("expr key %x\n",key_idx); + + //stack was raised here if retval was stack but.. + } + + if (key_idx < 0) { //error + return key_idx; + } + + codegen.opcodes.push_back(subscript_elem->is_attribute ? GDScriptFunction::OPCODE_GET_NAMED : GDScriptFunction::OPCODE_GET); + codegen.opcodes.push_back(prev_pos); + codegen.opcodes.push_back(key_idx); + slevel++; + codegen.alloc_stack(slevel); + int dst_pos = (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) | slevel; + + codegen.opcodes.push_back(dst_pos); + + //add in reverse order, since it will be reverted + + setchain.push_back(dst_pos); + setchain.push_back(key_idx); + setchain.push_back(prev_pos); + setchain.push_back(subscript_elem->is_attribute ? GDScriptFunction::OPCODE_SET_NAMED : GDScriptFunction::OPCODE_SET); + + prev_pos = dst_pos; + } + + setchain.invert(); + + int set_index; + + if (subscript->is_attribute) { + set_index = codegen.get_name_map_pos(subscript->attribute->name); + } else { + set_index = _parse_expression(codegen, subscript->index, slevel + 1); + } + + if (set_index < 0) { //error + return set_index; + } + + if (set_index & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) { + slevel++; + codegen.alloc_stack(slevel); + } + + int set_value = _parse_assign_right_expression(codegen, assignment, slevel + 1, subscript->is_attribute ? 0 : set_index); + if (set_value < 0) { //error + return set_value; + } + + codegen.opcodes.push_back(subscript->is_attribute ? GDScriptFunction::OPCODE_SET_NAMED : GDScriptFunction::OPCODE_SET); + codegen.opcodes.push_back(prev_pos); + codegen.opcodes.push_back(set_index); + codegen.opcodes.push_back(set_value); + + for (int i = 0; i < setchain.size(); i++) { + codegen.opcodes.push_back(setchain[i]); + } + + return retval; + + } else if (assignment->assignee->type == GDScriptParser::Node::IDENTIFIER && _is_class_member_property(codegen, static_cast(assignment->assignee)->name)) { + //assignment to member property + + int slevel = p_stack_level; + + int src_address = _parse_assign_right_expression(codegen, assignment, slevel); + if (src_address < 0) { + return -1; + } + + StringName name = static_cast(assignment->assignee)->name; + + codegen.opcodes.push_back(GDScriptFunction::OPCODE_SET_MEMBER); + codegen.opcodes.push_back(codegen.get_name_map_pos(name)); + codegen.opcodes.push_back(src_address); + + return GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS; + } else { + //REGULAR ASSIGNMENT MODE!! + + int slevel = p_stack_level; + + int dst_address_a = _parse_expression(codegen, assignment->assignee, slevel); + if (dst_address_a < 0) { + return -1; + } + + if (dst_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) { + slevel++; + codegen.alloc_stack(slevel); + } + + int src_address_b = _parse_assign_right_expression(codegen, assignment, slevel); + if (src_address_b < 0) { + return -1; + } + + // FIXME: Actually check type. + GDScriptDataType assign_type; // = _gdtype_from_datatype(on->arguments[0]->get_datatype()); + + if (assign_type.has_type && !assignment->get_datatype().is_set()) { + // Typed assignment + switch (assign_type.kind) { + case GDScriptDataType::BUILTIN: { + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN); // perform operator + codegen.opcodes.push_back(assign_type.builtin_type); // variable type + codegen.opcodes.push_back(dst_address_a); // argument 1 + codegen.opcodes.push_back(src_address_b); // argument 2 + } break; + case GDScriptDataType::NATIVE: { + int class_idx; + if (GDScriptLanguage::get_singleton()->get_global_map().has(assign_type.native_type)) { + class_idx = GDScriptLanguage::get_singleton()->get_global_map()[assign_type.native_type]; + class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root) + } else { + // _set_error("Invalid native class type '" + String(assign_type.native_type) + "'.", on->arguments[0]); + return -1; + } + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE); // perform operator + codegen.opcodes.push_back(class_idx); // variable type + codegen.opcodes.push_back(dst_address_a); // argument 1 + codegen.opcodes.push_back(src_address_b); // argument 2 + } break; + case GDScriptDataType::SCRIPT: + case GDScriptDataType::GDSCRIPT: { + Variant script = assign_type.script_type; + int idx = codegen.get_constant_pos(script); //make it a local constant (faster access) + + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT); // perform operator + codegen.opcodes.push_back(idx); // variable type + codegen.opcodes.push_back(dst_address_a); // argument 1 + codegen.opcodes.push_back(src_address_b); // argument 2 + } break; + default: { + ERR_PRINT("Compiler bug: unresolved assign."); + + // Shouldn't get here, but fail-safe to a regular assignment + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); // perform operator + codegen.opcodes.push_back(dst_address_a); // argument 1 + codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter) + } + } + } else { + // Either untyped assignment or already type-checked by the parser + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); // perform operator + codegen.opcodes.push_back(dst_address_a); // argument 1 + codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter) + } + return dst_address_a; //if anything, returns wathever was assigned or correct stack position + } + } break; +#undef OPERATOR_RETURN //TYPE_TYPE, default: { ERR_FAIL_V_MSG(-1, "Bug in bytecode compiler, unexpected node in parse tree while parsing expression."); //unreachable code @@ -1305,256 +1341,610 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: } } -Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::BlockNode *p_block, int p_stack_level, int p_break_addr, int p_continue_addr) { +Error GDScriptCompiler::_parse_match_pattern(CodeGen &codegen, const GDScriptParser::PatternNode *p_pattern, int p_stack_level, int p_value_addr, int p_type_addr, int &r_bound_variables, Vector &r_patch_addresses, Vector &r_block_patch_address) { + // TODO: Many "repeated" code here that could be abstracted. This compiler is going away when new VM arrives though, so... + switch (p_pattern->pattern_type) { + case GDScriptParser::PatternNode::PT_LITERAL: { + // Get literal type into constant map. + int literal_type_addr = -1; + if (!codegen.constant_map.has((int)p_pattern->literal->value.get_type())) { + literal_type_addr = codegen.constant_map.size(); + codegen.constant_map[(int)p_pattern->literal->value.get_type()] = literal_type_addr; + + } else { + literal_type_addr = codegen.constant_map[(int)p_pattern->literal->value.get_type()]; + } + literal_type_addr |= GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS; + + // Check type equality. + int equality_addr = p_stack_level++; + equality_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; + codegen.alloc_stack(p_stack_level); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); + codegen.opcodes.push_back(Variant::OP_EQUAL); + codegen.opcodes.push_back(p_type_addr); + codegen.opcodes.push_back(literal_type_addr); + codegen.opcodes.push_back(equality_addr); // Address to result. + + // Jump away if not the same type. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(equality_addr); + r_patch_addresses.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + + // Get literal. + int literal_addr = _parse_expression(codegen, p_pattern->literal, p_stack_level); + + // Check value equality. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); + codegen.opcodes.push_back(Variant::OP_EQUAL); + codegen.opcodes.push_back(p_value_addr); + codegen.opcodes.push_back(literal_addr); + codegen.opcodes.push_back(equality_addr); // Address to result. + + // Jump away if doesn't match. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(equality_addr); + r_patch_addresses.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + + // Jump to the actual block since it matches. This is needed to take multi-pattern into account. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + r_block_patch_address.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + } break; + case GDScriptParser::PatternNode::PT_EXPRESSION: { + // Evaluate expression. + int expr_addr = _parse_expression(codegen, p_pattern->expression, p_stack_level); + if ((expr_addr >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { + p_stack_level++; + codegen.alloc_stack(p_stack_level); + } + + // Evaluate expression type. + int expr_type_addr = p_stack_level++; + expr_type_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; + codegen.alloc_stack(p_stack_level); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN); + codegen.opcodes.push_back(GDScriptFunctions::TYPE_OF); + codegen.opcodes.push_back(1); // One argument. + codegen.opcodes.push_back(expr_addr); // Argument is the value we want to test. + codegen.opcodes.push_back(expr_type_addr); // Address to result. + + // Check type equality. + int equality_addr = p_stack_level++; + equality_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; + codegen.alloc_stack(p_stack_level); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); + codegen.opcodes.push_back(Variant::OP_EQUAL); + codegen.opcodes.push_back(p_type_addr); + codegen.opcodes.push_back(expr_type_addr); + codegen.opcodes.push_back(equality_addr); // Address to result. + + // Jump away if not the same type. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(equality_addr); + r_patch_addresses.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + + // Check value equality. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); + codegen.opcodes.push_back(Variant::OP_EQUAL); + codegen.opcodes.push_back(p_value_addr); + codegen.opcodes.push_back(expr_addr); + codegen.opcodes.push_back(equality_addr); // Address to result. + + // Jump away if doesn't match. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(equality_addr); + r_patch_addresses.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + + // Jump to the actual block since it matches. This is needed to take multi-pattern into account. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + r_block_patch_address.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + } break; + case GDScriptParser::PatternNode::PT_BIND: { + // Create new stack variable. + int bind_addr = p_stack_level | (GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS); + codegen.add_stack_identifier(p_pattern->bind->name, p_stack_level++); + codegen.alloc_stack(p_stack_level); + r_bound_variables++; + + // Assign value to bound variable. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); + codegen.opcodes.push_back(bind_addr); // Destination. + codegen.opcodes.push_back(p_value_addr); // Source. + // Not need to block jump because bind happens only once. + } break; + case GDScriptParser::PatternNode::PT_ARRAY: { + int slevel = p_stack_level; + + // Get array type into constant map. + int array_type_addr = codegen.get_constant_pos(Variant::ARRAY); + + // Check type equality. + int equality_addr = slevel++; + equality_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; + codegen.alloc_stack(slevel); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); + codegen.opcodes.push_back(Variant::OP_EQUAL); + codegen.opcodes.push_back(p_type_addr); + codegen.opcodes.push_back(array_type_addr); + codegen.opcodes.push_back(equality_addr); // Address to result. + + // Jump away if not the same type. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(equality_addr); + r_patch_addresses.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + + // Store pattern length in constant map. + int array_length_addr = codegen.get_constant_pos(p_pattern->rest_used ? p_pattern->array.size() - 1 : p_pattern->array.size()); + + // Get value length. + int value_length_addr = slevel++; + codegen.alloc_stack(slevel); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN); + codegen.opcodes.push_back(GDScriptFunctions::LEN); + codegen.opcodes.push_back(1); // One argument. + codegen.opcodes.push_back(p_value_addr); // Argument is the value we want to test. + codegen.opcodes.push_back(value_length_addr); // Address to result. + + // Test length compatibility. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); + codegen.opcodes.push_back(p_pattern->rest_used ? Variant::OP_GREATER_EQUAL : Variant::OP_EQUAL); + codegen.opcodes.push_back(value_length_addr); + codegen.opcodes.push_back(array_length_addr); + codegen.opcodes.push_back(equality_addr); // Address to result. + + // Jump away if length is not compatible. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(equality_addr); + r_patch_addresses.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + + // Evaluate element by element. + for (int i = 0; i < p_pattern->array.size(); i++) { + if (p_pattern->array[i]->pattern_type == GDScriptParser::PatternNode::PT_REST) { + // Don't want to access an extra element of the user array. + break; + } + + int stlevel = p_stack_level; + Vector element_block_patches; // I want to internal patterns try the next element instead of going to the block. + // Add index to constant map. + int index_addr = codegen.get_constant_pos(i); + + // Get the actual element from the user-sent array. + int element_addr = stlevel++; + codegen.alloc_stack(stlevel); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_GET); + codegen.opcodes.push_back(p_value_addr); // Source. + codegen.opcodes.push_back(index_addr); // Index. + codegen.opcodes.push_back(element_addr); // Destination. + + // Also get type of element. + int element_type_addr = stlevel++; + element_type_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; + codegen.alloc_stack(stlevel); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN); + codegen.opcodes.push_back(GDScriptFunctions::TYPE_OF); + codegen.opcodes.push_back(1); // One argument. + codegen.opcodes.push_back(element_addr); // Argument is the value we want to test. + codegen.opcodes.push_back(element_type_addr); // Address to result. + + // Try the pattern inside the element. + Error err = _parse_match_pattern(codegen, p_pattern->array[i], stlevel, element_addr, element_type_addr, r_bound_variables, r_patch_addresses, element_block_patches); + if (err != OK) { + return err; + } + + // Patch jumps to block to try the next element. + for (int j = 0; j < element_block_patches.size(); j++) { + codegen.opcodes.write[element_block_patches[j]] = codegen.opcodes.size(); + } + } + + // Jump to the actual block since it matches. This is needed to take multi-pattern into account. + // Also here for the case of empty arrays. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + r_block_patch_address.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + } break; + case GDScriptParser::PatternNode::PT_DICTIONARY: { + int slevel = p_stack_level; + + // Get dictionary type into constant map. + int dict_type_addr = codegen.get_constant_pos(Variant::DICTIONARY); + + // Check type equality. + int equality_addr = slevel++; + equality_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; + codegen.alloc_stack(slevel); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); + codegen.opcodes.push_back(Variant::OP_EQUAL); + codegen.opcodes.push_back(p_type_addr); + codegen.opcodes.push_back(dict_type_addr); + codegen.opcodes.push_back(equality_addr); // Address to result. + + // Jump away if not the same type. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(equality_addr); + r_patch_addresses.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + + // Store pattern length in constant map. + int dict_length_addr = codegen.get_constant_pos(p_pattern->rest_used ? p_pattern->dictionary.size() - 1 : p_pattern->dictionary.size()); + + // Get user's dictionary length. + int value_length_addr = slevel++; + codegen.alloc_stack(slevel); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN); + codegen.opcodes.push_back(GDScriptFunctions::LEN); + codegen.opcodes.push_back(1); // One argument. + codegen.opcodes.push_back(p_value_addr); // Argument is the value we want to test. + codegen.opcodes.push_back(value_length_addr); // Address to result. + + // Test length compatibility. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); + codegen.opcodes.push_back(p_pattern->rest_used ? Variant::OP_GREATER_EQUAL : Variant::OP_EQUAL); + codegen.opcodes.push_back(value_length_addr); + codegen.opcodes.push_back(dict_length_addr); + codegen.opcodes.push_back(equality_addr); // Address to result. + + // Jump away if length is not compatible. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(equality_addr); + r_patch_addresses.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + + // Evaluate element by element. + for (int i = 0; i < p_pattern->dictionary.size(); i++) { + const GDScriptParser::PatternNode::Pair &element = p_pattern->dictionary[i]; + if (element.value_pattern->pattern_type == GDScriptParser::PatternNode::PT_REST) { + // Ignore rest pattern. + continue; + } + int stlevel = p_stack_level; + Vector element_block_patches; // I want to internal patterns try the next element instead of going to the block. + + // Get the pattern key. + int pattern_key_addr = _parse_expression(codegen, element.key, stlevel); + if (pattern_key_addr < 0) { + return ERR_PARSE_ERROR; + } + if ((pattern_key_addr >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { + stlevel++; + codegen.alloc_stack(stlevel); + } + + // Create stack slot for test result. + int test_result = stlevel++; + test_result |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; + codegen.alloc_stack(stlevel); + + // Check if pattern key exists in user's dictionary. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_RETURN); + codegen.opcodes.push_back(1); // Argument count. + codegen.opcodes.push_back(p_value_addr); // Base (user dictionary). + codegen.opcodes.push_back(codegen.get_name_map_pos("has")); // Function name. + codegen.opcodes.push_back(pattern_key_addr); // Argument (pattern key). + codegen.opcodes.push_back(test_result); // Return address. + + // Jump away if key doesn't exist. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(test_result); + r_patch_addresses.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + + // Get actual value from user dictionary. + int value_addr = stlevel++; + codegen.alloc_stack(stlevel); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_GET); + codegen.opcodes.push_back(p_value_addr); // Source. + codegen.opcodes.push_back(pattern_key_addr); // Index. + codegen.opcodes.push_back(value_addr); // Destination. + + // Also get type of value. + int value_type_addr = stlevel++; + value_type_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; + codegen.alloc_stack(stlevel); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN); + codegen.opcodes.push_back(GDScriptFunctions::TYPE_OF); + codegen.opcodes.push_back(1); // One argument. + codegen.opcodes.push_back(value_addr); // Argument is the value we want to test. + codegen.opcodes.push_back(value_type_addr); // Address to result. + + // Try the pattern inside the value. + Error err = _parse_match_pattern(codegen, element.value_pattern, stlevel, value_addr, value_type_addr, r_bound_variables, r_patch_addresses, element_block_patches); + if (err != OK) { + return err; + } + + // Patch jumps to block to try the next element. + for (int j = 0; j < element_block_patches.size(); j++) { + codegen.opcodes.write[element_block_patches[j]] = codegen.opcodes.size(); + } + } + + // Jump to the actual block since it matches. This is needed to take multi-pattern into account. + // Also here for the case of empty dictionaries. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + r_block_patch_address.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + + } break; + case GDScriptParser::PatternNode::PT_REST: + // Do nothing. + break; + case GDScriptParser::PatternNode::PT_WILDCARD: + // This matches anything so just do the jump. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + r_block_patch_address.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be replaced. + } + return OK; +} + +Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, int p_stack_level, int p_break_addr, int p_continue_addr) { codegen.push_stack_identifiers(); int new_identifiers = 0; - codegen.current_line = p_block->line; + codegen.current_line = p_block->start_line; for (int i = 0; i < p_block->statements.size(); i++) { const GDScriptParser::Node *s = p_block->statements[i]; - switch (s->type) { - case GDScriptParser::Node::TYPE_NEWLINE: { #ifdef DEBUG_ENABLED - const GDScriptParser::NewLineNode *nl = static_cast(s); - codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE); - codegen.opcodes.push_back(nl->line); - codegen.current_line = nl->line; + // Add a newline before each statement, since the debugger needs those. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE); + codegen.opcodes.push_back(s->start_line); + codegen.current_line = s->start_line; #endif - } break; - case GDScriptParser::Node::TYPE_CONTROL_FLOW: { - // try subblocks - const GDScriptParser::ControlFlowNode *cf = static_cast(s); + switch (s->type) { + case GDScriptParser::Node::MATCH: { + const GDScriptParser::MatchNode *match = static_cast(s); - switch (cf->cf_type) { - case GDScriptParser::ControlFlowNode::CF_MATCH: { - GDScriptParser::MatchNode *match = cf->match; + int slevel = p_stack_level; - GDScriptParser::IdentifierNode *id = memnew(GDScriptParser::IdentifierNode); - id->name = "#match_value"; + // First, let's save the addres of the value match. + int temp_addr = _parse_expression(codegen, match->test, slevel); + if (temp_addr < 0) { + return ERR_PARSE_ERROR; + } + if ((temp_addr >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) { + slevel++; + codegen.alloc_stack(slevel); + } - // var #match_value - // copied because there is no _parse_statement :( - codegen.add_stack_identifier(id->name, p_stack_level++); - codegen.alloc_stack(p_stack_level); - new_identifiers++; + // Then, let's save the type of the value in the stack too, so we can reuse for later comparisons. + int type_addr = slevel++; + type_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS; + codegen.alloc_stack(slevel); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN); + codegen.opcodes.push_back(GDScriptFunctions::TYPE_OF); + codegen.opcodes.push_back(1); // One argument. + codegen.opcodes.push_back(temp_addr); // Argument is the value we want to test. + codegen.opcodes.push_back(type_addr); // Address to result. - GDScriptParser::OperatorNode *op = memnew(GDScriptParser::OperatorNode); - op->op = GDScriptParser::OperatorNode::OP_ASSIGN; - op->arguments.push_back(id); - op->arguments.push_back(match->val_to_match); + Vector patch_match_end; // Will patch the jump to the end of match. - int ret = _parse_expression(codegen, op, p_stack_level); - if (ret < 0) { - memdelete(id); - memdelete(op); - return ERR_PARSE_ERROR; - } + // Now we can actually start testing. + // For each branch. + for (int j = 0; j < match->branches.size(); j++) { + const GDScriptParser::MatchBranchNode *branch = match->branches[j]; - // break address - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - codegen.opcodes.push_back(codegen.opcodes.size() + 3); - int break_addr = codegen.opcodes.size(); - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - codegen.opcodes.push_back(0); // break addr + int bound_variables = 0; + codegen.push_stack_identifiers(); // Create an extra block around for binds. - for (int j = 0; j < match->compiled_pattern_branches.size(); j++) { - GDScriptParser::MatchNode::CompiledPatternBranch branch = match->compiled_pattern_branches[j]; +#ifdef DEBUG_ENABLED + // Add a newline before each branch, since the debugger needs those. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE); + codegen.opcodes.push_back(s->start_line); + codegen.current_line = s->start_line; +#endif + Vector patch_addrs; // Will patch with end of pattern to jump. + Vector block_patch_addrs; // Will patch with start of block to jump. - // jump over continue - // jump unconditionally - // continue address - // compile the condition - int ret2 = _parse_expression(codegen, branch.compiled_pattern, p_stack_level); - if (ret2 < 0) { - memdelete(id); - memdelete(op); - return ERR_PARSE_ERROR; + // For each pattern in branch. + for (int k = 0; k < branch->patterns.size(); k++) { + if (k > 0) { + // Patch jumps per pattern to allow for multipattern. If a pattern fails it just tries the next. + for (int l = 0; l < patch_addrs.size(); l++) { + codegen.opcodes.write[patch_addrs[l]] = codegen.opcodes.size(); } - - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF); - codegen.opcodes.push_back(ret2); - codegen.opcodes.push_back(codegen.opcodes.size() + 3); - int continue_addr = codegen.opcodes.size(); - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - codegen.opcodes.push_back(0); - - Error err = _parse_block(codegen, branch.body, p_stack_level, p_break_addr, continue_addr); - if (err) { - memdelete(id); - memdelete(op); - return ERR_PARSE_ERROR; - } - - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - codegen.opcodes.push_back(break_addr); - - codegen.opcodes.write[continue_addr + 1] = codegen.opcodes.size(); + patch_addrs.clear(); } - - codegen.opcodes.write[break_addr + 1] = codegen.opcodes.size(); - - memdelete(id); - memdelete(op); - - } break; - - case GDScriptParser::ControlFlowNode::CF_IF: { - int ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false); - if (ret2 < 0) { - return ERR_PARSE_ERROR; - } - - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); - codegen.opcodes.push_back(ret2); - int else_addr = codegen.opcodes.size(); - codegen.opcodes.push_back(0); //temporary - - Error err = _parse_block(codegen, cf->body, p_stack_level, p_break_addr, p_continue_addr); - if (err) { + Error err = _parse_match_pattern(codegen, branch->patterns[k], slevel, temp_addr, type_addr, bound_variables, patch_addrs, block_patch_addrs); + if (err != OK) { return err; } + } + // Patch jumps to the block. + for (int k = 0; k < block_patch_addrs.size(); k++) { + codegen.opcodes.write[block_patch_addrs[k]] = codegen.opcodes.size(); + } - if (cf->body_else) { - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - int end_addr = codegen.opcodes.size(); - codegen.opcodes.push_back(0); - codegen.opcodes.write[else_addr] = codegen.opcodes.size(); + // Leave space for bound variables. + slevel += bound_variables; + codegen.alloc_stack(slevel); - codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE); - codegen.opcodes.push_back(cf->body_else->line); - codegen.current_line = cf->body_else->line; + // Parse the branch block. + _parse_block(codegen, branch->block, slevel, p_break_addr, p_continue_addr); - Error err2 = _parse_block(codegen, cf->body_else, p_stack_level, p_break_addr, p_continue_addr); - if (err2) { - return err2; - } + // Jump to end of match. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + patch_match_end.push_back(codegen.opcodes.size()); + codegen.opcodes.push_back(0); // Will be patched. - codegen.opcodes.write[end_addr] = codegen.opcodes.size(); - } else { - //end without else - codegen.opcodes.write[else_addr] = codegen.opcodes.size(); - } + // Patch the addresses of last pattern to jump to the end of the branch, into the next one. + for (int k = 0; k < patch_addrs.size(); k++) { + codegen.opcodes.write[patch_addrs[k]] = codegen.opcodes.size(); + } - } break; - case GDScriptParser::ControlFlowNode::CF_FOR: { - int slevel = p_stack_level; - int iter_stack_pos = slevel; - int iterator_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); - int counter_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); - int container_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); - codegen.alloc_stack(slevel); - - codegen.push_stack_identifiers(); - codegen.add_stack_identifier(static_cast(cf->arguments[0])->name, iter_stack_pos); - - int ret2 = _parse_expression(codegen, cf->arguments[1], slevel, false); - if (ret2 < 0) { - return ERR_COMPILATION_FAILED; - } - - //assign container - codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); - codegen.opcodes.push_back(container_pos); - codegen.opcodes.push_back(ret2); - - //begin loop - codegen.opcodes.push_back(GDScriptFunction::OPCODE_ITERATE_BEGIN); - codegen.opcodes.push_back(counter_pos); - codegen.opcodes.push_back(container_pos); - codegen.opcodes.push_back(codegen.opcodes.size() + 4); - codegen.opcodes.push_back(iterator_pos); - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); //skip code for next - codegen.opcodes.push_back(codegen.opcodes.size() + 8); - //break loop - int break_pos = codegen.opcodes.size(); - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); //skip code for next - codegen.opcodes.push_back(0); //skip code for next - //next loop - int continue_pos = codegen.opcodes.size(); - codegen.opcodes.push_back(GDScriptFunction::OPCODE_ITERATE); - codegen.opcodes.push_back(counter_pos); - codegen.opcodes.push_back(container_pos); - codegen.opcodes.push_back(break_pos); - codegen.opcodes.push_back(iterator_pos); - - Error err = _parse_block(codegen, cf->body, slevel, break_pos, continue_pos); - if (err) { - return err; - } - - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - codegen.opcodes.push_back(continue_pos); - codegen.opcodes.write[break_pos + 1] = codegen.opcodes.size(); - - codegen.pop_stack_identifiers(); - - } break; - case GDScriptParser::ControlFlowNode::CF_WHILE: { - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - codegen.opcodes.push_back(codegen.opcodes.size() + 3); - int break_addr = codegen.opcodes.size(); - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - codegen.opcodes.push_back(0); - int continue_addr = codegen.opcodes.size(); - - int ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false); - if (ret2 < 0) { - return ERR_PARSE_ERROR; - } - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); - codegen.opcodes.push_back(ret2); - codegen.opcodes.push_back(break_addr); - Error err = _parse_block(codegen, cf->body, p_stack_level, break_addr, continue_addr); - if (err) { - return err; - } - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - codegen.opcodes.push_back(continue_addr); - - codegen.opcodes.write[break_addr + 1] = codegen.opcodes.size(); - - } break; - case GDScriptParser::ControlFlowNode::CF_BREAK: { - if (p_break_addr < 0) { - _set_error("'break'' not within loop", cf); - return ERR_COMPILATION_FAILED; - } - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - codegen.opcodes.push_back(p_break_addr); - - } break; - case GDScriptParser::ControlFlowNode::CF_CONTINUE: { - if (p_continue_addr < 0) { - _set_error("'continue' not within loop", cf); - return ERR_COMPILATION_FAILED; - } - - codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); - codegen.opcodes.push_back(p_continue_addr); - - } break; - case GDScriptParser::ControlFlowNode::CF_RETURN: { - int ret2; - - if (cf->arguments.size()) { - ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false); - if (ret2 < 0) { - return ERR_PARSE_ERROR; - } - - } else { - ret2 = GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS; - } - - codegen.opcodes.push_back(GDScriptFunction::OPCODE_RETURN); - codegen.opcodes.push_back(ret2); - - } break; + codegen.pop_stack_identifiers(); // Get out of extra block. + } + // Patch the addresses to jump to the end of the match statement. + for (int j = 0; j < patch_match_end.size(); j++) { + codegen.opcodes.write[patch_match_end[j]] = codegen.opcodes.size(); } } break; - case GDScriptParser::Node::TYPE_ASSERT: { + + case GDScriptParser::Node::IF: { + const GDScriptParser::IfNode *if_n = static_cast(s); + int ret2 = _parse_expression(codegen, if_n->condition, p_stack_level, false); + if (ret2 < 0) { + return ERR_PARSE_ERROR; + } + + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(ret2); + int else_addr = codegen.opcodes.size(); + codegen.opcodes.push_back(0); //temporary + + Error err = _parse_block(codegen, if_n->true_block, p_stack_level, p_break_addr, p_continue_addr); + if (err) { + return err; + } + + if (if_n->false_block) { + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + int end_addr = codegen.opcodes.size(); + codegen.opcodes.push_back(0); + codegen.opcodes.write[else_addr] = codegen.opcodes.size(); + + Error err2 = _parse_block(codegen, if_n->false_block, p_stack_level, p_break_addr, p_continue_addr); + if (err2) { + return err2; + } + + codegen.opcodes.write[end_addr] = codegen.opcodes.size(); + } else { + //end without else + codegen.opcodes.write[else_addr] = codegen.opcodes.size(); + } + + } break; + case GDScriptParser::Node::FOR: { + const GDScriptParser::ForNode *for_n = static_cast(s); + int slevel = p_stack_level; + int iter_stack_pos = slevel; + int iterator_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); + int counter_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); + int container_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); + codegen.alloc_stack(slevel); + + codegen.push_stack_identifiers(); + codegen.add_stack_identifier(for_n->variable->name, iter_stack_pos); + + int ret2 = _parse_expression(codegen, for_n->list, slevel, false); + if (ret2 < 0) { + return ERR_COMPILATION_FAILED; + } + + //assign container + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); + codegen.opcodes.push_back(container_pos); + codegen.opcodes.push_back(ret2); + + //begin loop + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ITERATE_BEGIN); + codegen.opcodes.push_back(counter_pos); + codegen.opcodes.push_back(container_pos); + codegen.opcodes.push_back(codegen.opcodes.size() + 4); + codegen.opcodes.push_back(iterator_pos); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); //skip code for next + codegen.opcodes.push_back(codegen.opcodes.size() + 8); + //break loop + int break_pos = codegen.opcodes.size(); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); //skip code for next + codegen.opcodes.push_back(0); //skip code for next + //next loop + int continue_pos = codegen.opcodes.size(); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ITERATE); + codegen.opcodes.push_back(counter_pos); + codegen.opcodes.push_back(container_pos); + codegen.opcodes.push_back(break_pos); + codegen.opcodes.push_back(iterator_pos); + + Error err = _parse_block(codegen, for_n->loop, slevel, break_pos, continue_pos); + if (err) { + return err; + } + + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + codegen.opcodes.push_back(continue_pos); + codegen.opcodes.write[break_pos + 1] = codegen.opcodes.size(); + + codegen.pop_stack_identifiers(); + + } break; + case GDScriptParser::Node::WHILE: { + const GDScriptParser::WhileNode *while_n = static_cast(s); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + codegen.opcodes.push_back(codegen.opcodes.size() + 3); + int break_addr = codegen.opcodes.size(); + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + codegen.opcodes.push_back(0); + int continue_addr = codegen.opcodes.size(); + + int ret2 = _parse_expression(codegen, while_n->condition, p_stack_level, false); + if (ret2 < 0) { + return ERR_PARSE_ERROR; + } + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT); + codegen.opcodes.push_back(ret2); + codegen.opcodes.push_back(break_addr); + Error err = _parse_block(codegen, while_n->loop, p_stack_level, break_addr, continue_addr); + if (err) { + return err; + } + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + codegen.opcodes.push_back(continue_addr); + + codegen.opcodes.write[break_addr + 1] = codegen.opcodes.size(); + + } break; + case GDScriptParser::Node::BREAK: { + if (p_break_addr < 0) { + _set_error("'break'' not within loop", s); + return ERR_COMPILATION_FAILED; + } + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + codegen.opcodes.push_back(p_break_addr); + + } break; + case GDScriptParser::Node::CONTINUE: { + if (p_continue_addr < 0) { + _set_error("'continue' not within loop", s); + return ERR_COMPILATION_FAILED; + } + + codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); + codegen.opcodes.push_back(p_continue_addr); + + } break; + case GDScriptParser::Node::RETURN: { + const GDScriptParser::ReturnNode *return_n = static_cast(s); + int ret2; + + if (return_n->return_value != nullptr) { + ret2 = _parse_expression(codegen, return_n->return_value, p_stack_level, false); + if (ret2 < 0) { + return ERR_PARSE_ERROR; + } + + } else { + ret2 = GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS; + } + + codegen.opcodes.push_back(GDScriptFunction::OPCODE_RETURN); + codegen.opcodes.push_back(ret2); + + } break; + case GDScriptParser::Node::ASSERT: { #ifdef DEBUG_ENABLED // try subblocks @@ -1578,14 +1968,14 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo codegen.opcodes.push_back(message_ret); #endif } break; - case GDScriptParser::Node::TYPE_BREAKPOINT: { + case GDScriptParser::Node::BREAKPOINT: { #ifdef DEBUG_ENABLED // try subblocks codegen.opcodes.push_back(GDScriptFunction::OPCODE_BREAKPOINT); #endif } break; - case GDScriptParser::Node::TYPE_LOCAL_VAR: { - const GDScriptParser::LocalVarNode *lv = static_cast(s); + case GDScriptParser::Node::VARIABLE: { + const GDScriptParser::VariableNode *lv = static_cast(s); // since we are using properties now for most class access, allow shadowing of class members to make user's life easier. // @@ -1594,20 +1984,50 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo // return ERR_ALREADY_EXISTS; //} - codegen.add_stack_identifier(lv->name, p_stack_level++); + codegen.add_stack_identifier(lv->identifier->name, p_stack_level++); codegen.alloc_stack(p_stack_level); new_identifiers++; + if (lv->initializer != nullptr) { + int dst_address = codegen.stack_identifiers[lv->identifier->name]; + dst_address |= GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS; + + int src_address = _parse_expression(codegen, lv->initializer, p_stack_level); + if (src_address < 0) { + return ERR_PARSE_ERROR; + } + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); + codegen.opcodes.push_back(dst_address); + codegen.opcodes.push_back(src_address); + } } break; + case GDScriptParser::Node::CONSTANT: { + // Local constants. + const GDScriptParser::ConstantNode *lc = static_cast(s); + // FIXME: Need to properly reduce expressions to avoid limiting this so much. + if (lc->initializer->type != GDScriptParser::Node::LITERAL) { + _set_error("Local constant must have a literal as initializer.", lc->initializer); + return ERR_PARSE_ERROR; + } + codegen.local_named_constants[lc->identifier->name] = codegen.get_constant_pos(static_cast(lc->initializer)->value); + } break; + case GDScriptParser::Node::PASS: + // Nothing to do. + break; default: { //expression - int ret2 = _parse_expression(codegen, s, p_stack_level, true); - if (ret2 < 0) { - return ERR_PARSE_ERROR; + if (s->is_expression()) { + int ret2 = _parse_expression(codegen, static_cast(s), p_stack_level, true); + if (ret2 < 0) { + return ERR_PARSE_ERROR; + } + } else { + ERR_FAIL_V_MSG(ERR_INVALID_DATA, "Bug in bytecode compiler, unexpected node in parse tree while parsing statement."); //unreachable code } } break; } } + codegen.pop_stack_identifiers(); return OK; } @@ -1626,9 +2046,10 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser Vector argnames; int stack_level = 0; + int optional_parameters = 0; if (p_func) { - for (int i = 0; i < p_func->arguments.size(); i++) { + for (int i = 0; i < p_func->parameters.size(); i++) { // since we are using properties now for most class access, allow shadowing of class members to make user's life easier. // //if (_is_class_member_property(p_script, p_func->arguments[i])) { @@ -1636,42 +2057,81 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser // return ERR_ALREADY_EXISTS; //} - codegen.add_stack_identifier(p_func->arguments[i], i); + codegen.add_stack_identifier(p_func->parameters[i]->identifier->name, i); #ifdef TOOLS_ENABLED - argnames.push_back(p_func->arguments[i]); + argnames.push_back(p_func->parameters[i]->identifier->name); #endif + if (p_func->parameters[i]->default_value != nullptr) { + optional_parameters++; + } } - stack_level = p_func->arguments.size(); + stack_level = p_func->parameters.size(); } codegen.alloc_stack(stack_level); /* Parse initializer -if applies- */ - bool is_initializer = !p_for_ready && !p_func; + bool is_implicit_initializer = !p_for_ready && !p_func; + bool is_initializer = p_func && String(p_func->identifier->name) == GDScriptLanguage::get_singleton()->strings._init; - if (is_initializer || (p_func && String(p_func->name) == "_init")) { - //parse initializer for class members - if (!p_func && p_class->extends_used && p_script->native.is_null()) { - //call implicit parent constructor - codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_SELF_BASE); - codegen.opcodes.push_back(codegen.get_name_map_pos("_init")); - codegen.opcodes.push_back(0); - codegen.opcodes.push_back((GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) | 0); + if (is_implicit_initializer) { + // Initialize class fields. + for (int i = 0; i < p_class->members.size(); i++) { + if (p_class->members[i].type != GDScriptParser::ClassNode::Member::VARIABLE) { + continue; + } + const GDScriptParser::VariableNode *field = p_class->members[i].variable; + if (field->onready) { + // Only initialize in _ready. + continue; + } + + if (field->initializer) { + // Emit proper line change. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE); + codegen.opcodes.push_back(field->initializer->start_line); + + int src_address = _parse_expression(codegen, field->initializer, stack_level, false, true); + if (src_address < 0) { + return ERR_PARSE_ERROR; + } + int dst_address = codegen.script->member_indices[field->identifier->name].index; + dst_address |= GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS; + + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); + codegen.opcodes.push_back(dst_address); + codegen.opcodes.push_back(src_address); + } } - Error err = _parse_block(codegen, p_class->initializer, stack_level); - if (err) { - return err; - } - is_initializer = true; } - if (p_for_ready || (p_func && String(p_func->name) == "_ready")) { - //parse initializer for class members - if (p_class->ready->statements.size()) { - Error err = _parse_block(codegen, p_class->ready, stack_level); - if (err) { - return err; + if (p_for_ready || (p_func && String(p_func->identifier->name) == "_ready")) { + // Initialize class fields on ready. + for (int i = 0; i < p_class->members.size(); i++) { + if (p_class->members[i].type != GDScriptParser::ClassNode::Member::VARIABLE) { + continue; + } + const GDScriptParser::VariableNode *field = p_class->members[i].variable; + if (!field->onready) { + continue; + } + + if (field->initializer) { + // Emit proper line change. + codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE); + codegen.opcodes.push_back(field->initializer->start_line); + + int src_address = _parse_expression(codegen, field->initializer, stack_level, false, true); + if (src_address < 0) { + return ERR_PARSE_ERROR; + } + int dst_address = codegen.script->member_indices[field->identifier->name].index; + dst_address |= GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS; + + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); + codegen.opcodes.push_back(dst_address); + codegen.opcodes.push_back(src_address); } } } @@ -1682,14 +2142,19 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser StringName func_name; if (p_func) { - if (p_func->default_values.size()) { + if (optional_parameters > 0) { codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT); defarg_addr.push_back(codegen.opcodes.size()); - for (int i = 0; i < p_func->default_values.size(); i++) { - _parse_expression(codegen, p_func->default_values[i], stack_level, true); + for (int i = p_func->parameters.size() - optional_parameters; i < p_func->parameters.size(); i++) { + int src_addr = _parse_expression(codegen, p_func->parameters[i]->default_value, stack_level, true); + if (src_addr < 0) { + return ERR_PARSE_ERROR; + } + codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); + codegen.opcodes.push_back(codegen.stack_identifiers[p_func->parameters[i]->identifier->name] | (GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS)); + codegen.opcodes.push_back(src_addr); defarg_addr.push_back(codegen.opcodes.size()); } - defarg_addr.invert(); } @@ -1698,12 +2163,12 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser return err; } - func_name = p_func->name; + func_name = p_func->identifier->name; } else { if (p_for_ready) { func_name = "_ready"; } else { - func_name = "_init"; + func_name = "@implicit_new"; } } @@ -1719,13 +2184,14 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser //} if (p_func) { - gdfunc->_static = p_func->_static; + gdfunc->_static = p_func->is_static; gdfunc->rpc_mode = p_func->rpc_mode; - gdfunc->argument_types.resize(p_func->argument_types.size()); - for (int i = 0; i < p_func->argument_types.size(); i++) { - gdfunc->argument_types.write[i] = _gdtype_from_datatype(p_func->argument_types[i]); + // FIXME: Add actual parameters types; + gdfunc->argument_types.resize(p_func->parameters.size()); + for (int i = 0; i < p_func->parameters.size(); i++) { + gdfunc->argument_types.write[i] = GDScriptDataType(); //_gdtype_from_datatype(p_func->argument_types[i]); } - gdfunc->return_type = _gdtype_from_datatype(p_func->return_type); + gdfunc->return_type = GDScriptDataType(); //_gdtype_from_datatype(p_func->return_type); } else { gdfunc->_static = false; gdfunc->rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED; @@ -1797,7 +2263,7 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser gdfunc->_default_arg_ptr = nullptr; } - gdfunc->_argument_count = p_func ? p_func->arguments.size() : 0; + gdfunc->_argument_count = p_func ? p_func->parameters.size() : 0; gdfunc->_stack_size = codegen.stack_max; gdfunc->_call_size = codegen.call_max; gdfunc->name = func_name; @@ -1810,15 +2276,15 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser } //loc if (p_func) { - signature += "::" + itos(p_func->body->line); + signature += "::" + itos(p_func->body->start_line); } else { signature += "::0"; } //function and class - if (p_class->name) { - signature += "::" + String(p_class->name) + "." + String(func_name); + if (p_class->identifier) { + signature += "::" + String(p_class->identifier->name) + "." + String(func_name); } else { signature += "::" + String(func_name); } @@ -1838,10 +2304,10 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser #endif if (p_func) { - gdfunc->_initial_line = p_func->line; + gdfunc->_initial_line = p_func->start_line; #ifdef TOOLS_ENABLED - p_script->member_lines[func_name] = p_func->line; + p_script->member_lines[func_name] = p_func->start_line; #endif } else { gdfunc->_initial_line = 0; @@ -1854,6 +2320,9 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser if (is_initializer) { p_script->initializer = gdfunc; } + if (is_implicit_initializer) { + p_script->implicit_initializer = gdfunc; + } return OK; } @@ -1861,14 +2330,14 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) { parsing_classes.insert(p_script); - if (p_class->owner && p_class->owner->owner) { + if (p_class->outer && p_class->outer->outer) { // Owner is not root if (!parsed_classes.has(p_script->_owner)) { if (parsing_classes.has(p_script->_owner)) { - _set_error("Cyclic class reference for '" + String(p_class->name) + "'.", p_class); + _set_error("Cyclic class reference for '" + String(p_class->identifier->name) + "'.", p_class); return ERR_PARSE_ERROR; } - Error err = _parse_class_level(p_script->_owner, p_class->owner, p_keep_state); + Error err = _parse_class_level(p_script->_owner, p_class->outer, p_keep_state); if (err) { return err; } @@ -1889,8 +2358,8 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar p_script->_signals.clear(); p_script->initializer = nullptr; - p_script->tool = p_class->tool; - p_script->name = p_class->name; + p_script->tool = parser->is_tool(); + p_script->name = p_class->identifier ? p_class->identifier->name : ""; Ref native; @@ -1909,13 +2378,14 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar p_script->_base = base.ptr(); p_script->member_indices = base->member_indices; - if (p_class->base_type.kind == GDScriptParser::DataType::CLASS) { + if (p_class->base_type.kind == GDScriptParser::DataType::CLASS && p_class->base_type.gdscript_type != nullptr) { if (!parsed_classes.has(p_script->_base)) { if (parsing_classes.has(p_script->_base)) { - _set_error("Cyclic class reference for '" + String(p_class->name) + "'.", p_class); + String class_name = p_class->identifier ? p_class->identifier->name : "
"; + _set_error("Cyclic class reference for '" + class_name + "'.", p_class); return ERR_PARSE_ERROR; } - Error err = _parse_class_level(p_script->_base, p_class->base_type.class_type, p_keep_state); + Error err = _parse_class_level(p_script->_base, p_class->base_type.gdscript_type, p_keep_state); if (err) { return err; } @@ -1928,86 +2398,130 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar } break; } - for (int i = 0; i < p_class->variables.size(); i++) { - StringName name = p_class->variables[i].identifier; + for (int i = 0; i < p_class->members.size(); i++) { + const GDScriptParser::ClassNode::Member &member = p_class->members[i]; + switch (member.type) { + case GDScriptParser::ClassNode::Member::VARIABLE: { + const GDScriptParser::VariableNode *variable = member.variable; + StringName name = variable->identifier->name; - GDScript::MemberInfo minfo; - minfo.index = p_script->member_indices.size(); - minfo.setter = p_class->variables[i].setter; - minfo.getter = p_class->variables[i].getter; - minfo.rpc_mode = p_class->variables[i].rpc_mode; - minfo.data_type = _gdtype_from_datatype(p_class->variables[i].data_type); + GDScript::MemberInfo minfo; + minfo.index = p_script->member_indices.size(); + // FIXME: Getter and setter. + // minfo.setter = p_class->variables[i].setter; + // minfo.getter = p_class->variables[i].getter; + minfo.rpc_mode = variable->rpc_mode; + // FIXME: Types. + // minfo.data_type = _gdtype_from_datatype(p_class->variables[i].data_type); - PropertyInfo prop_info = minfo.data_type; - prop_info.name = name; - PropertyInfo export_info = p_class->variables[i]._export; + PropertyInfo prop_info = minfo.data_type; + prop_info.name = name; + PropertyInfo export_info = variable->export_info; - if (export_info.type != Variant::NIL) { - if (!minfo.data_type.has_type) { - prop_info.type = export_info.type; - prop_info.class_name = export_info.class_name; - } - prop_info.hint = export_info.hint; - prop_info.hint_string = export_info.hint_string; - prop_info.usage = export_info.usage; + if (variable->exported) { + if (!minfo.data_type.has_type) { + prop_info.type = export_info.type; + prop_info.class_name = export_info.class_name; + } + prop_info.hint = export_info.hint; + prop_info.hint_string = export_info.hint_string; + prop_info.usage = export_info.usage; #ifdef TOOLS_ENABLED - if (p_class->variables[i].default_value.get_type() != Variant::NIL) { - p_script->member_default_values[name] = p_class->variables[i].default_value; - } + if (variable->initializer != nullptr && variable->initializer->type == GDScriptParser::Node::LITERAL) { + p_script->member_default_values[name] = static_cast(variable->initializer)->value; + } #endif - } else { - prop_info.usage = PROPERTY_USAGE_SCRIPT_VARIABLE; - } + } else { + prop_info.usage = PROPERTY_USAGE_SCRIPT_VARIABLE; + } - p_script->member_info[name] = prop_info; - p_script->member_indices[name] = minfo; - p_script->members.insert(name); + p_script->member_info[name] = prop_info; + p_script->member_indices[name] = minfo; + p_script->members.insert(name); #ifdef TOOLS_ENABLED - p_script->member_lines[name] = p_class->variables[i].line; + p_script->member_lines[name] = variable->start_line; #endif - } + } break; - for (Map::Element *E = p_class->constant_expressions.front(); E; E = E->next()) { - StringName name = E->key(); + case GDScriptParser::ClassNode::Member::CONSTANT: { + const GDScriptParser::ConstantNode *constant = member.constant; + StringName name = constant->identifier->name; - ERR_CONTINUE(E->get().expression->type != GDScriptParser::Node::TYPE_CONSTANT); + ERR_CONTINUE(constant->initializer->type != GDScriptParser::Node::LITERAL); - GDScriptParser::ConstantNode *constant = static_cast(E->get().expression); + const GDScriptParser::LiteralNode *literal = static_cast(constant->initializer); - p_script->constants.insert(name, constant->value); + p_script->constants.insert(name, literal->value); #ifdef TOOLS_ENABLED - p_script->member_lines[name] = E->get().expression->line; + p_script->member_lines[name] = constant->start_line; #endif - } + } break; - for (int i = 0; i < p_class->_signals.size(); i++) { - StringName name = p_class->_signals[i].name; + case GDScriptParser::ClassNode::Member::ENUM_VALUE: { + const GDScriptParser::EnumNode::Value &enum_value = member.enum_value; + StringName name = enum_value.identifier->name; - GDScript *c = p_script; + p_script->constants.insert(name, enum_value.value); +#ifdef TOOLS_ENABLED + p_script->member_lines[name] = enum_value.identifier->start_line; +#endif + } break; - while (c) { - if (c->_signals.has(name)) { - _set_error("Signal '" + name + "' redefined (in current or parent class)", p_class); - return ERR_ALREADY_EXISTS; - } + case GDScriptParser::ClassNode::Member::SIGNAL: { + const GDScriptParser::SignalNode *signal = member.signal; + StringName name = signal->identifier->name; - if (c->base.is_valid()) { - c = c->base.ptr(); - } else { - c = nullptr; - } + GDScript *c = p_script; + + while (c) { + if (c->_signals.has(name)) { + _set_error("Signal '" + name + "' redefined (in current or parent class)", p_class); + return ERR_ALREADY_EXISTS; + } + + if (c->base.is_valid()) { + c = c->base.ptr(); + } else { + c = nullptr; + } + } + + if (native.is_valid()) { + if (ClassDB::has_signal(native->get_name(), name)) { + _set_error("Signal '" + name + "' redefined (original in native class '" + String(native->get_name()) + "')", p_class); + return ERR_ALREADY_EXISTS; + } + } + + Vector parameters_names; + parameters_names.resize(signal->parameters.size()); + for (int j = 0; j < signal->parameters.size(); j++) { + parameters_names.write[j] = signal->parameters[j]->identifier->name; + } + p_script->_signals[name] = parameters_names; + } break; + + case GDScriptParser::ClassNode::Member::ENUM: { + const GDScriptParser::EnumNode *enum_n = member.m_enum; + + // TODO: Make enums not be just a dictionary? + Dictionary new_enum; + for (int j = 0; j < enum_n->values.size(); j++) { + int value = enum_n->values[j].value; + // Needs to be string because Variant::get will convert to String. + new_enum[String(enum_n->values[j].identifier->name)] = value; + } + + p_script->constants.insert(enum_n->identifier->name, new_enum); +#ifdef TOOLS_ENABLED + p_script->member_lines[enum_n->identifier->name] = enum_n->start_line; +#endif + } break; + default: + break; // Nothing to do here. } - - if (native.is_valid()) { - if (ClassDB::has_signal(native->get_name(), name)) { - _set_error("Signal '" + name + "' redefined (original in native class '" + String(native->get_name()) + "')", p_class); - return ERR_ALREADY_EXISTS; - } - } - - p_script->_signals[name] = p_class->_signals[i].arguments; } parsed_classes.insert(p_script); @@ -2015,14 +2529,19 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar //parse sub-classes - for (int i = 0; i < p_class->subclasses.size(); i++) { - StringName name = p_class->subclasses[i]->name; + for (int i = 0; i < p_class->members.size(); i++) { + const GDScriptParser::ClassNode::Member &member = p_class->members[i]; + if (member.type != member.CLASS) { + continue; + } + const GDScriptParser::ClassNode *inner_class = member.m_class; + StringName name = inner_class->identifier->name; Ref &subclass = p_script->subclasses[name]; GDScript *subclass_ptr = subclass.ptr(); // Subclass might still be parsing, just skip it if (!parsed_classes.has(subclass_ptr) && !parsing_classes.has(subclass_ptr)) { - Error err = _parse_class_level(subclass_ptr, p_class->subclasses[i], p_keep_state); + Error err = _parse_class_level(subclass_ptr, inner_class, p_keep_state); if (err) { return err; } @@ -2030,7 +2549,7 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar #ifdef TOOLS_ENABLED - p_script->member_lines[name] = p_class->subclasses[i]->line; + p_script->member_lines[name] = inner_class->start_line; #endif p_script->constants.insert(name, subclass); //once parsed, goes to the list of constants @@ -2042,41 +2561,33 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar Error GDScriptCompiler::_parse_class_blocks(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) { //parse methods - bool has_initializer = false; bool has_ready = false; - for (int i = 0; i < p_class->functions.size(); i++) { - if (!has_initializer && p_class->functions[i]->name == "_init") { - has_initializer = true; + for (int i = 0; i < p_class->members.size(); i++) { + const GDScriptParser::ClassNode::Member &member = p_class->members[i]; + if (member.type != member.FUNCTION) { + continue; } - if (!has_ready && p_class->functions[i]->name == "_ready") { + const GDScriptParser::FunctionNode *function = member.function; + if (!has_ready && function->identifier->name == "_ready") { has_ready = true; } - Error err = _parse_function(p_script, p_class, p_class->functions[i]); + Error err = _parse_function(p_script, p_class, function); if (err) { return err; } } - //parse static methods - - for (int i = 0; i < p_class->static_functions.size(); i++) { - Error err = _parse_function(p_script, p_class, p_class->static_functions[i]); - if (err) { - return err; - } - } - - if (!has_initializer) { - //create a constructor + { + // Create an implicit constructor in any case. Error err = _parse_function(p_script, p_class, nullptr); if (err) { return err; } } - if (!has_ready && p_class->ready->statements.size()) { - //create a constructor + if (!has_ready && p_class->onready_used) { + //create a _ready constructor Error err = _parse_function(p_script, p_class, nullptr, true); if (err) { return err; @@ -2132,11 +2643,15 @@ Error GDScriptCompiler::_parse_class_blocks(GDScript *p_script, const GDScriptPa } #endif - for (int i = 0; i < p_class->subclasses.size(); i++) { - StringName name = p_class->subclasses[i]->name; + for (int i = 0; i < p_class->members.size(); i++) { + if (p_class->members[i].type != GDScriptParser::ClassNode::Member::CLASS) { + continue; + } + const GDScriptParser::ClassNode *inner_class = p_class->members[i].m_class; + StringName name = inner_class->identifier->name; GDScript *subclass = p_script->subclasses[name].ptr(); - Error err = _parse_class_blocks(subclass, p_class->subclasses[i], p_keep_state); + Error err = _parse_class_blocks(subclass, inner_class, p_keep_state); if (err) { return err; } @@ -2155,8 +2670,12 @@ void GDScriptCompiler::_make_scripts(GDScript *p_script, const GDScriptParser::C p_script->subclasses.clear(); - for (int i = 0; i < p_class->subclasses.size(); i++) { - StringName name = p_class->subclasses[i]->name; + for (int i = 0; i < p_class->members.size(); i++) { + if (p_class->members[i].type != GDScriptParser::ClassNode::Member::CLASS) { + continue; + } + const GDScriptParser::ClassNode *inner_class = p_class->members[i].m_class; + StringName name = inner_class->identifier->name; Ref subclass; String fully_qualified_name = p_script->fully_qualified_name + "::" + name; @@ -2176,7 +2695,7 @@ void GDScriptCompiler::_make_scripts(GDScript *p_script, const GDScriptParser::C subclass->fully_qualified_name = fully_qualified_name; p_script->subclasses.insert(name, subclass); - _make_scripts(subclass.ptr(), p_class->subclasses[i], false); + _make_scripts(subclass.ptr(), inner_class, false); } } @@ -2186,8 +2705,7 @@ Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_scri error = ""; parser = p_parser; main_script = p_script; - const GDScriptParser::Node *root = parser->get_parse_tree(); - ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, ERR_INVALID_DATA); + const GDScriptParser::ClassNode *root = parser->get_tree(); source = p_script->get_path(); @@ -2195,16 +2713,16 @@ Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_scri p_script->fully_qualified_name = p_script->path; // Create scripts for subclasses beforehand so they can be referenced - _make_scripts(p_script, static_cast(root), p_keep_state); + _make_scripts(p_script, root, p_keep_state); p_script->_owner = nullptr; - Error err = _parse_class_level(p_script, static_cast(root), p_keep_state); + Error err = _parse_class_level(p_script, root, p_keep_state); if (err) { return err; } - err = _parse_class_blocks(p_script, static_cast(root), p_keep_state); + err = _parse_class_blocks(p_script, root, p_keep_state); if (err) { return err; diff --git a/modules/gdscript/gdscript_compiler.h b/modules/gdscript/gdscript_compiler.h index 315d4f1842c..674982659f2 100644 --- a/modules/gdscript/gdscript_compiler.h +++ b/modules/gdscript/gdscript_compiler.h @@ -33,6 +33,7 @@ #include "core/set.h" #include "gdscript.h" +#include "gdscript_function.h" #include "gdscript_parser.h" class GDScriptCompiler { @@ -52,6 +53,7 @@ class GDScriptCompiler { List stack_debug; List> block_identifier_stack; Map block_identifiers; + Map local_named_constants; void add_stack_identifier(const StringName &p_id, int p_stackpos) { stack_identifiers[p_id] = p_stackpos; @@ -111,11 +113,11 @@ class GDScriptCompiler { int get_constant_pos(const Variant &p_constant) { if (constant_map.has(p_constant)) { - return constant_map[p_constant]; + return constant_map[p_constant] | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS); } int pos = constant_map.size(); constant_map[p_constant] = pos; - return pos; + return pos | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS); } Vector opcodes; @@ -140,14 +142,16 @@ class GDScriptCompiler { void _set_error(const String &p_error, const GDScriptParser::Node *p_node); - bool _create_unary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level); - bool _create_binary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer = false, int p_index_addr = 0); + bool _create_unary_operator(CodeGen &codegen, const GDScriptParser::UnaryOpNode *on, Variant::Operator op, int p_stack_level); + bool _create_binary_operator(CodeGen &codegen, const GDScriptParser::BinaryOpNode *on, Variant::Operator op, int p_stack_level, bool p_initializer = false, int p_index_addr = 0); + bool _create_binary_operator(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_left_operand, const GDScriptParser::ExpressionNode *p_right_operand, Variant::Operator op, int p_stack_level, bool p_initializer = false, int p_index_addr = 0); GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype) const; - int _parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level, int p_index_addr = 0); - int _parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root = false, bool p_initializer = false, int p_index_addr = 0); - Error _parse_block(CodeGen &codegen, const GDScriptParser::BlockNode *p_block, int p_stack_level = 0, int p_break_addr = -1, int p_continue_addr = -1); + int _parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::AssignmentNode *p_assignment, int p_stack_level, int p_index_addr = 0); + int _parse_expression(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_expression, int p_stack_level, bool p_root = false, bool p_initializer = false, int p_index_addr = 0); + Error _parse_match_pattern(CodeGen &codegen, const GDScriptParser::PatternNode *p_pattern, int p_stack_level, int p_value_addr, int p_type_addr, int &r_bound_variables, Vector &r_patch_addresses, Vector &r_block_patch_address); + Error _parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, int p_stack_level = 0, int p_break_addr = -1, int p_continue_addr = -1); Error _parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false); Error _parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state); Error _parse_class_blocks(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state); @@ -156,6 +160,7 @@ class GDScriptCompiler { int err_column; StringName source; String error; + bool within_await = false; public: Error compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state = false); diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index 3a5db3687bf..5cbd8afc07a 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -34,6 +34,8 @@ #include "core/global_constants.h" #include "core/os/file_access.h" #include "gdscript_compiler.h" +#include "gdscript_parser.h" +#include "gdscript_tokenizer.h" #ifdef TOOLS_ENABLED #include "editor/editor_file_system.h" @@ -114,50 +116,47 @@ void GDScriptLanguage::make_template(const String &p_class_name, const String &p p_script->set_source_code(_template); } +static void get_function_names_recursively(const GDScriptParser::ClassNode *p_class, const String &p_prefix, Map &r_funcs) { + for (int i = 0; i < p_class->members.size(); i++) { + if (p_class->members[i].type == GDScriptParser::ClassNode::Member::FUNCTION) { + const GDScriptParser::FunctionNode *function = p_class->members[i].function; + r_funcs[function->start_line] = p_prefix.empty() ? String(function->identifier->name) : p_prefix + "." + String(function->identifier->name); + } else if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) { + String new_prefix = p_class->members[i].m_class->identifier->name; + get_function_names_recursively(p_class->members[i].m_class, p_prefix.empty() ? new_prefix : p_prefix + "." + new_prefix, r_funcs); + } + } +} + bool GDScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List *r_functions, List *r_warnings, Set *r_safe_lines) const { GDScriptParser parser; - Error err = parser.parse(p_script, p_path.get_base_dir(), true, p_path, false, r_safe_lines); + Error err = parser.parse(p_script, p_path, false); #ifdef DEBUG_ENABLED - if (r_warnings) { - for (const List::Element *E = parser.get_warnings().front(); E; E = E->next()) { - const GDScriptWarning &warn = E->get(); - ScriptLanguage::Warning w; - w.line = warn.line; - w.code = (int)warn.code; - w.string_code = GDScriptWarning::get_name_from_code(warn.code); - w.message = warn.get_message(); - r_warnings->push_back(w); - } - } + // FIXME: Warnings. + // if (r_warnings) { + // for (const List::Element *E = parser.get_warnings().front(); E; E = E->next()) { + // const GDScriptWarning &warn = E->get(); + // ScriptLanguage::Warning w; + // w.line = warn.line; + // w.code = (int)warn.code; + // w.string_code = GDScriptWarning::get_name_from_code(warn.code); + // w.message = warn.get_message(); + // r_warnings->push_back(w); + // } + // } #endif if (err) { - r_line_error = parser.get_error_line(); - r_col_error = parser.get_error_column(); - r_test_error = parser.get_error(); + GDScriptParser::ParserError parse_error = parser.get_errors().front()->get(); + r_line_error = parse_error.line; + r_col_error = parse_error.column; + r_test_error = parse_error.message; return false; } else { - const GDScriptParser::Node *root = parser.get_parse_tree(); - ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, false); - - const GDScriptParser::ClassNode *cl = static_cast(root); + const GDScriptParser::ClassNode *cl = parser.get_tree(); Map funcs; - for (int i = 0; i < cl->functions.size(); i++) { - funcs[cl->functions[i]->line] = cl->functions[i]->name; - } - for (int i = 0; i < cl->static_functions.size(); i++) { - funcs[cl->static_functions[i]->line] = cl->static_functions[i]->name; - } - - for (int i = 0; i < cl->subclasses.size(); i++) { - for (int j = 0; j < cl->subclasses[i]->functions.size(); j++) { - funcs[cl->subclasses[i]->functions[j]->line] = String(cl->subclasses[i]->name) + "." + cl->subclasses[i]->functions[j]->name; - } - for (int j = 0; j < cl->subclasses[i]->static_functions.size(); j++) { - funcs[cl->subclasses[i]->static_functions[j]->line] = String(cl->subclasses[i]->name) + "." + cl->subclasses[i]->static_functions[j]->name; - } - } + get_function_names_recursively(cl, "", funcs); for (Map::Element *E = funcs.front(); E; E = E->next()) { r_functions->push_back(E->get() + ":" + itos(E->key())); @@ -176,20 +175,26 @@ bool GDScriptLanguage::supports_builtin_mode() const { } int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const { - GDScriptTokenizerText tokenizer; - tokenizer.set_code(p_code); + GDScriptTokenizer tokenizer; + tokenizer.set_source_code(p_code); int indent = 0; - while (tokenizer.get_token() != GDScriptTokenizer::TK_EOF && tokenizer.get_token() != GDScriptTokenizer::TK_ERROR) { - if (tokenizer.get_token() == GDScriptTokenizer::TK_NEWLINE) { - indent = tokenizer.get_token_line_indent(); + GDScriptTokenizer::Token current = tokenizer.scan(); + while (current.type != GDScriptTokenizer::Token::TK_EOF && current.type != GDScriptTokenizer::Token::ERROR) { + if (current.type == GDScriptTokenizer::Token::INDENT) { + indent++; + } else if (current.type == GDScriptTokenizer::Token::DEDENT) { + indent--; } - if (indent == 0 && tokenizer.get_token() == GDScriptTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1) == GDScriptTokenizer::TK_IDENTIFIER) { - String identifier = tokenizer.get_token_identifier(1); - if (identifier == p_function) { - return tokenizer.get_token_line(); + if (indent == 0 && current.type == GDScriptTokenizer::Token::FUNC) { + current = tokenizer.scan(); + if (current.is_identifier()) { + String identifier = current.get_identifier(); + if (identifier == p_function) { + return current.start_line; + } } } - tokenizer.advance(); + current = tokenizer.scan(); } return -1; } @@ -395,16 +400,6 @@ void GDScriptLanguage::get_public_functions(List *p_functions) const mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource"); p_functions->push_back(mi); } - { - MethodInfo mi; - mi.name = "yield"; - mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object")); - mi.arguments.push_back(PropertyInfo(Variant::STRING, "signal")); - mi.default_arguments.push_back(Variant()); - mi.default_arguments.push_back(String()); - mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "GDScriptFunctionState"); - p_functions->push_back(mi); - } { MethodInfo mi; mi.name = "assert"; @@ -467,2511 +462,11 @@ String GDScriptLanguage::make_function(const String &p_class, const String &p_na //////// COMPLETION ////////// -#if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED) - -struct GDScriptCompletionContext { - const GDScriptParser::ClassNode *_class = nullptr; - const GDScriptParser::FunctionNode *function = nullptr; - const GDScriptParser::BlockNode *block = nullptr; - Object *base = nullptr; - String base_path; - int line = 0; - uint32_t depth = 0; - - GDScriptCompletionContext() {} -}; - -struct GDScriptCompletionIdentifier { - GDScriptParser::DataType type; - String enumeration; - Variant value; - const GDScriptParser::Node *assigned_expression = nullptr; - - GDScriptCompletionIdentifier() {} -}; - -static void _get_directory_contents(EditorFileSystemDirectory *p_dir, Map &r_list) { - const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\""; - - for (int i = 0; i < p_dir->get_file_count(); i++) { - ScriptCodeCompletionOption option(p_dir->get_file_path(i), ScriptCodeCompletionOption::KIND_FILE_PATH); - option.insert_text = quote_style + option.display + quote_style; - r_list.insert(option.display, option); - } - - for (int i = 0; i < p_dir->get_subdir_count(); i++) { - _get_directory_contents(p_dir->get_subdir(i), r_list); - } -} - -static String _get_visual_datatype(const PropertyInfo &p_info, bool p_isarg = true) { - if (p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { - String enum_name = p_info.class_name; - if (enum_name.find(".") == -1) { - return enum_name; - } - return enum_name.get_slice(".", 1); - } - - String n = p_info.name; - int idx = n.find(":"); - if (idx != -1) { - return n.substr(idx + 1, n.length()); - } - - if (p_info.type == Variant::OBJECT) { - if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { - return p_info.hint_string; - } else { - return p_info.class_name.operator String(); - } - } - if (p_info.type == Variant::NIL) { - if (p_isarg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) { - return "Variant"; - } else { - return "void"; - } - } - - return Variant::get_type_name(p_info.type); -} - -static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value) { - GDScriptCompletionIdentifier ci; - ci.value = p_value; - ci.type.is_constant = true; - ci.type.has_type = true; - ci.type.kind = GDScriptParser::DataType::BUILTIN; - ci.type.builtin_type = p_value.get_type(); - - if (ci.type.builtin_type == Variant::OBJECT) { - Object *obj = p_value.operator Object *(); - if (!obj) { - return ci; - } - ci.type.native_type = obj->get_class_name(); - Ref