From bce6f1792ebce5de31700e72f57830e5d87b419a Mon Sep 17 00:00:00 2001 From: Rune Date: Tue, 8 Nov 2022 03:51:20 -0800 Subject: GDScript compiler subclass bugfixes --- modules/gdscript/gdscript.cpp | 140 +++++---- modules/gdscript/gdscript.h | 7 +- modules/gdscript/gdscript_analyzer.cpp | 10 - modules/gdscript/gdscript_cache.cpp | 40 ++- modules/gdscript/gdscript_cache.h | 3 +- modules/gdscript/gdscript_compiler.cpp | 316 +++++++++------------ modules/gdscript/gdscript_compiler.h | 8 +- modules/gdscript/gdscript_editor.cpp | 5 +- modules/gdscript/gdscript_parser.cpp | 5 + modules/gdscript/tests/gdscript_test_runner.cpp | 1 - .../analyzer/features/external_inner_base.gd | 4 + .../analyzer/features/external_inner_base.out | 3 + .../tests/scripts/analyzer/features/inner_base.gd | 18 ++ .../tests/scripts/analyzer/features/inner_base.out | 4 + 14 files changed, 283 insertions(+), 281 deletions(-) create mode 100644 modules/gdscript/tests/scripts/analyzer/features/external_inner_base.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/features/external_inner_base.out create mode 100644 modules/gdscript/tests/scripts/analyzer/features/inner_base.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/features/inner_base.out diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index b4da9c1224..bd6cef0b6e 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -629,10 +629,6 @@ void GDScript::_update_doc() { } } - for (KeyValue> &E : subclasses) { - E.value->_update_doc(); - } - _add_doc(doc); } #endif @@ -674,36 +670,14 @@ bool GDScript::_update_exports(bool *r_err, bool p_recursive_call, PlaceHolderSc base_cache = Ref(); } - if (c->extends_used) { - String ext_path = ""; - if (String(c->extends_path) != "" && String(c->extends_path) != get_path()) { - ext_path = c->extends_path; - if (ext_path.is_relative_path()) { - String base_path = get_path(); - if (base_path.is_empty() || base_path.is_relative_path()) { - ERR_PRINT(("Could not resolve relative path for parent class: " + ext_path).utf8().get_data()); - } else { - ext_path = base_path.get_base_dir().path_join(ext_path); - } - } - } else if (c->extends.size() != 0) { - const StringName &base_class = c->extends[0]; - - if (ScriptServer::is_global_class(base_class)) { - ext_path = ScriptServer::get_global_class_path(base_class); - } - } - - if (!ext_path.is_empty()) { - if (ext_path != get_path()) { - Ref bf = ResourceLoader::load(ext_path); - - if (bf.is_valid()) { - base_cache = bf; - bf->inheriters_cache.insert(get_instance_id()); - } - } else { - ERR_PRINT(("Path extending itself in " + ext_path).utf8().get_data()); + GDScriptParser::DataType base_type = parser.get_tree()->base_type; + if (base_type.kind == GDScriptParser::DataType::CLASS) { + Ref bf = GDScriptCache::get_full_script(base_type.script_path, err, path); + if (err == OK) { + bf = Ref(bf->find_class(base_type.class_type->fqcn)); + if (bf.is_valid()) { + base_cache = bf; + bf->inheriters_cache.insert(get_instance_id()); } } } @@ -825,13 +799,6 @@ void GDScript::update_exports() { #endif } -void GDScript::_set_subclass_path(Ref &p_sc, const String &p_path) { - p_sc->path = p_path; - for (KeyValue> &E : p_sc->subclasses) { - _set_subclass_path(E.value, p_path); - } -} - String GDScript::_get_debug_path() const { if (is_built_in() && !get_name().is_empty()) { return get_name() + " (" + get_path() + ")"; @@ -860,7 +827,7 @@ Error GDScript::reload(bool p_keep_state) { basedir = basedir.get_base_dir(); } -// Loading a template, don't parse. + // Loading a template, don't parse. #ifdef TOOLS_ENABLED if (EditorPaths::get_singleton() && basedir.begins_with(EditorPaths::get_singleton()->get_project_script_templates_dir())) { return OK; @@ -913,10 +880,6 @@ Error GDScript::reload(bool p_keep_state) { GDScriptCompiler compiler; err = compiler.compile(&parser, this, p_keep_state); -#ifdef TOOLS_ENABLED - _update_doc(); -#endif - if (err) { if (can_run) { if (EngineDebugger::is_active()) { @@ -937,14 +900,6 @@ Error GDScript::reload(bool p_keep_state) { } #endif - valid = true; - - for (KeyValue> &E : subclasses) { - _set_subclass_path(E.value, path); - } - - _init_rpc_methods_properties(); - return OK; } @@ -1051,8 +1006,13 @@ Error GDScript::load_byte_code(const String &p_path) { } void GDScript::set_path(const String &p_path, bool p_take_over) { - Script::set_path(p_path, p_take_over); + if (is_root_script()) { + Script::set_path(p_path, p_take_over); + } this->path = p_path; + for (KeyValue> &kv : subclasses) { + kv.value->set_path(p_path, p_take_over); + } } Error GDScript::load_source_code(const String &p_path) { @@ -1127,6 +1087,52 @@ bool GDScript::inherits_script(const Ref