Merge pull request #90601 from rune-scape/rune-gdscript-dependant-parser-ref-errors

GDScript: Fix out of date errors in depended scripts
This commit is contained in:
Rémi Verschelde 2024-04-22 22:18:15 +02:00
commit e8eca0b3f0
No known key found for this signature in database
GPG Key ID: C3336907360768E1
8 changed files with 139 additions and 106 deletions

View File

@ -739,10 +739,18 @@ Error GDScript::reload(bool p_keep_state) {
if (source_path.is_empty()) { if (source_path.is_empty()) {
source_path = get_path(); source_path = get_path();
} }
Ref<GDScript> cached_script = GDScriptCache::get_cached_script(source_path); if (!source_path.is_empty()) {
if (!source_path.is_empty() && cached_script.is_null()) { if (GDScriptCache::get_cached_script(source_path).is_null()) {
MutexLock lock(GDScriptCache::singleton->mutex); MutexLock lock(GDScriptCache::singleton->mutex);
GDScriptCache::singleton->shallow_gdscript_cache[source_path] = Ref<GDScript>(this); GDScriptCache::singleton->shallow_gdscript_cache[source_path] = Ref<GDScript>(this);
}
if (GDScriptCache::has_parser(source_path)) {
Error err = OK;
Ref<GDScriptParserRef> parser_ref = GDScriptCache::get_parser(source_path, GDScriptParserRef::EMPTY, err);
if (parser_ref.is_valid() && parser_ref->get_source_hash() != source.hash()) {
GDScriptCache::remove_parser(source_path);
}
}
} }
} }

View File

@ -325,7 +325,7 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c
if (!parser->has_class(p_class)) { if (!parser->has_class(p_class)) {
String script_path = p_class->get_datatype().script_path; String script_path = p_class->get_datatype().script_path;
Ref<GDScriptParserRef> parser_ref = get_parser_for(script_path); Ref<GDScriptParserRef> parser_ref = parser->get_depended_parser_for(script_path);
if (parser_ref.is_null()) { if (parser_ref.is_null()) {
push_error(vformat(R"(Could not find script "%s".)", script_path), p_source); push_error(vformat(R"(Could not find script "%s".)", script_path), p_source);
return ERR_PARSE_ERROR; return ERR_PARSE_ERROR;
@ -400,7 +400,7 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c
if (p_class->extends_path.is_relative_path()) { if (p_class->extends_path.is_relative_path()) {
p_class->extends_path = class_type.script_path.get_base_dir().path_join(p_class->extends_path).simplify_path(); p_class->extends_path = class_type.script_path.get_base_dir().path_join(p_class->extends_path).simplify_path();
} }
Ref<GDScriptParserRef> ext_parser = get_parser_for(p_class->extends_path); Ref<GDScriptParserRef> ext_parser = parser->get_depended_parser_for(p_class->extends_path);
if (ext_parser.is_null()) { if (ext_parser.is_null()) {
push_error(vformat(R"(Could not resolve super class path "%s".)", p_class->extends_path), p_class); push_error(vformat(R"(Could not resolve super class path "%s".)", p_class->extends_path), p_class);
return ERR_PARSE_ERROR; return ERR_PARSE_ERROR;
@ -428,7 +428,7 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c
if (GDScript::is_canonically_equal_paths(base_path, parser->script_path)) { if (GDScript::is_canonically_equal_paths(base_path, parser->script_path)) {
base = parser->head->get_datatype(); base = parser->head->get_datatype();
} else { } else {
Ref<GDScriptParserRef> base_parser = get_parser_for(base_path); Ref<GDScriptParserRef> base_parser = parser->get_depended_parser_for(base_path);
if (base_parser.is_null()) { if (base_parser.is_null()) {
push_error(vformat(R"(Could not resolve super class "%s".)", name), id); push_error(vformat(R"(Could not resolve super class "%s".)", name), id);
return ERR_PARSE_ERROR; return ERR_PARSE_ERROR;
@ -448,7 +448,7 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c
return ERR_PARSE_ERROR; return ERR_PARSE_ERROR;
} }
Ref<GDScriptParserRef> info_parser = get_parser_for(info.path); Ref<GDScriptParserRef> info_parser = parser->get_depended_parser_for(info.path);
if (info_parser.is_null()) { if (info_parser.is_null()) {
push_error(vformat(R"(Could not parse singleton from "%s".)", info.path), id); push_error(vformat(R"(Could not parse singleton from "%s".)", info.path), id);
return ERR_PARSE_ERROR; return ERR_PARSE_ERROR;
@ -644,7 +644,7 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type
} else if (Ref<Script>(local.constant->initializer->reduced_value).is_valid()) { } else if (Ref<Script>(local.constant->initializer->reduced_value).is_valid()) {
Ref<GDScript> gdscript = local.constant->initializer->reduced_value; Ref<GDScript> gdscript = local.constant->initializer->reduced_value;
if (gdscript.is_valid()) { if (gdscript.is_valid()) {
Ref<GDScriptParserRef> ref = get_parser_for(gdscript->get_script_path()); Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(gdscript->get_script_path());
if (ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED) != OK) { if (ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED) != OK) {
push_error(vformat(R"(Could not parse script from "%s".)", gdscript->get_script_path()), first_id); push_error(vformat(R"(Could not parse script from "%s".)", gdscript->get_script_path()), first_id);
return bad_type; return bad_type;
@ -710,7 +710,7 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type
String path = ScriptServer::get_global_class_path(first); String path = ScriptServer::get_global_class_path(first);
String ext = path.get_extension(); String ext = path.get_extension();
if (ext == GDScriptLanguage::get_singleton()->get_extension()) { if (ext == GDScriptLanguage::get_singleton()->get_extension()) {
Ref<GDScriptParserRef> ref = get_parser_for(path); Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(path);
if (!ref.is_valid() || ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED) != OK) { if (!ref.is_valid() || ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED) != OK) {
push_error(vformat(R"(Could not parse global class "%s" from "%s".)", first, ScriptServer::get_global_class_path(first)), p_type); push_error(vformat(R"(Could not parse global class "%s" from "%s".)", first, ScriptServer::get_global_class_path(first)), p_type);
return bad_type; return bad_type;
@ -722,7 +722,7 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type
} }
} else if (ProjectSettings::get_singleton()->has_autoload(first) && ProjectSettings::get_singleton()->get_autoload(first).is_singleton) { } else if (ProjectSettings::get_singleton()->has_autoload(first) && ProjectSettings::get_singleton()->get_autoload(first).is_singleton) {
const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(first); const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(first);
Ref<GDScriptParserRef> ref = get_parser_for(autoload.path); Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(autoload.path);
if (ref.is_null()) { if (ref.is_null()) {
push_error(vformat(R"(The referenced autoload "%s" (from "%s") could not be loaded.)", first, autoload.path), p_type); push_error(vformat(R"(The referenced autoload "%s" (from "%s") could not be loaded.)", first, autoload.path), p_type);
return bad_type; return bad_type;
@ -776,7 +776,7 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type
} else if (Ref<Script>(member.constant->initializer->reduced_value).is_valid()) { } else if (Ref<Script>(member.constant->initializer->reduced_value).is_valid()) {
Ref<GDScript> gdscript = member.constant->initializer->reduced_value; Ref<GDScript> gdscript = member.constant->initializer->reduced_value;
if (gdscript.is_valid()) { if (gdscript.is_valid()) {
Ref<GDScriptParserRef> ref = get_parser_for(gdscript->get_script_path()); Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(gdscript->get_script_path());
if (ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED) != OK) { if (ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED) != OK) {
push_error(vformat(R"(Could not parse script from "%s".)", gdscript->get_script_path()), p_type); push_error(vformat(R"(Could not parse script from "%s".)", gdscript->get_script_path()), p_type);
return bad_type; return bad_type;
@ -876,7 +876,7 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class,
if (!parser->has_class(p_class)) { if (!parser->has_class(p_class)) {
String script_path = p_class->get_datatype().script_path; String script_path = p_class->get_datatype().script_path;
Ref<GDScriptParserRef> parser_ref = get_parser_for(script_path); Ref<GDScriptParserRef> parser_ref = parser->get_depended_parser_for(script_path);
if (parser_ref.is_null()) { if (parser_ref.is_null()) {
push_error(vformat(R"(Could not find script "%s" (While resolving "%s").)", script_path, member.get_name()), p_source); push_error(vformat(R"(Could not find script "%s" (While resolving "%s").)", script_path, member.get_name()), p_source);
return; return;
@ -1159,7 +1159,7 @@ void GDScriptAnalyzer::resolve_class_interface(GDScriptParser::ClassNode *p_clas
if (!parser->has_class(p_class)) { if (!parser->has_class(p_class)) {
String script_path = p_class->get_datatype().script_path; String script_path = p_class->get_datatype().script_path;
Ref<GDScriptParserRef> parser_ref = get_parser_for(script_path); Ref<GDScriptParserRef> parser_ref = parser->get_depended_parser_for(script_path);
if (parser_ref.is_null()) { if (parser_ref.is_null()) {
push_error(vformat(R"(Could not find script "%s".)", script_path), p_source); push_error(vformat(R"(Could not find script "%s".)", script_path), p_source);
return; return;
@ -1249,7 +1249,7 @@ void GDScriptAnalyzer::resolve_class_body(GDScriptParser::ClassNode *p_class, co
if (!parser->has_class(p_class)) { if (!parser->has_class(p_class)) {
String script_path = p_class->get_datatype().script_path; String script_path = p_class->get_datatype().script_path;
Ref<GDScriptParserRef> parser_ref = get_parser_for(script_path); Ref<GDScriptParserRef> parser_ref = parser->get_depended_parser_for(script_path);
if (parser_ref.is_null()) { if (parser_ref.is_null()) {
push_error(vformat(R"(Could not find script "%s".)", script_path), p_source); push_error(vformat(R"(Could not find script "%s".)", script_path), p_source);
return; return;
@ -3556,7 +3556,7 @@ GDScriptParser::DataType GDScriptAnalyzer::make_global_class_meta_type(const Str
String path = ScriptServer::get_global_class_path(p_class_name); String path = ScriptServer::get_global_class_path(p_class_name);
String ext = path.get_extension(); String ext = path.get_extension();
if (ext == GDScriptLanguage::get_singleton()->get_extension()) { if (ext == GDScriptLanguage::get_singleton()->get_extension()) {
Ref<GDScriptParserRef> ref = get_parser_for(path); Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(path);
if (ref.is_null()) { if (ref.is_null()) {
push_error(vformat(R"(Could not find script for class "%s".)", p_class_name), p_source); push_error(vformat(R"(Could not find script for class "%s".)", p_class_name), p_source);
type.type_source = GDScriptParser::DataType::UNDETECTED; type.type_source = GDScriptParser::DataType::UNDETECTED;
@ -4078,7 +4078,7 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident
result.builtin_type = Variant::OBJECT; result.builtin_type = Variant::OBJECT;
result.native_type = SNAME("Node"); result.native_type = SNAME("Node");
if (ResourceLoader::get_resource_type(autoload.path) == "GDScript") { if (ResourceLoader::get_resource_type(autoload.path) == "GDScript") {
Ref<GDScriptParserRef> singl_parser = get_parser_for(autoload.path); Ref<GDScriptParserRef> singl_parser = parser->get_depended_parser_for(autoload.path);
if (singl_parser.is_valid()) { if (singl_parser.is_valid()) {
Error err = singl_parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED); Error err = singl_parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
if (err == OK) { if (err == OK) {
@ -4092,7 +4092,7 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident
if (node != nullptr) { if (node != nullptr) {
Ref<GDScript> scr = node->get_script(); Ref<GDScript> scr = node->get_script();
if (scr.is_valid()) { if (scr.is_valid()) {
Ref<GDScriptParserRef> singl_parser = get_parser_for(scr->get_script_path()); Ref<GDScriptParserRef> singl_parser = parser->get_depended_parser_for(scr->get_script_path());
if (singl_parser.is_valid()) { if (singl_parser.is_valid()) {
Error err = singl_parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED); Error err = singl_parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
if (err == OK) { if (err == OK) {
@ -4822,10 +4822,6 @@ Variant GDScriptAnalyzer::make_variable_default_value(GDScriptParser::VariableNo
return result; return result;
} }
const HashMap<String, Ref<GDScriptParserRef>> &GDScriptAnalyzer::get_depended_parsers() {
return depended_parsers;
}
GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source) { GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source) {
GDScriptParser::DataType result; GDScriptParser::DataType result;
result.is_constant = true; result.is_constant = true;
@ -4865,7 +4861,7 @@ GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_va
// This might be an inner class, so we want to get the parser for the root. // This might be an inner class, so we want to get the parser for the root.
// But still get the inner class from that tree. // But still get the inner class from that tree.
String script_path = gds->get_script_path(); String script_path = gds->get_script_path();
Ref<GDScriptParserRef> ref = get_parser_for(script_path); Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(script_path);
if (ref.is_null()) { if (ref.is_null()) {
push_error(vformat(R"(Could not find script "%s".)", script_path), p_source); push_error(vformat(R"(Could not find script "%s".)", script_path), p_source);
GDScriptParser::DataType error_type; GDScriptParser::DataType error_type;
@ -5619,21 +5615,6 @@ bool GDScriptAnalyzer::class_exists(const StringName &p_class) const {
return ClassDB::class_exists(p_class) && ClassDB::is_class_exposed(p_class); return ClassDB::class_exists(p_class) && ClassDB::is_class_exposed(p_class);
} }
Ref<GDScriptParserRef> GDScriptAnalyzer::get_parser_for(const String &p_path) {
Ref<GDScriptParserRef> ref;
if (depended_parsers.has(p_path)) {
ref = depended_parsers[p_path];
} else {
Error err = OK;
ref = GDScriptCache::get_parser(p_path, GDScriptParserRef::EMPTY, err, parser->script_path);
if (ref.is_valid()) {
depended_parsers[p_path] = ref;
}
}
return ref;
}
Error GDScriptAnalyzer::resolve_inheritance() { Error GDScriptAnalyzer::resolve_inheritance() {
return resolve_class_inheritance(parser->head, true); return resolve_class_inheritance(parser->head, true);
} }
@ -5645,11 +5626,17 @@ Error GDScriptAnalyzer::resolve_interface() {
Error GDScriptAnalyzer::resolve_body() { Error GDScriptAnalyzer::resolve_body() {
resolve_class_body(parser->head, true); resolve_class_body(parser->head, true);
#ifdef DEBUG_ENABLED
// Apply here, after all `@warning_ignore`s have been resolved and applied.
parser->apply_pending_warnings();
#endif
return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR; return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
} }
Error GDScriptAnalyzer::resolve_dependencies() { Error GDScriptAnalyzer::resolve_dependencies() {
for (KeyValue<String, Ref<GDScriptParserRef>> &K : depended_parsers) { for (KeyValue<String, Ref<GDScriptParserRef>> &K : parser->depended_parsers) {
if (K.value.is_null()) { if (K.value.is_null()) {
return ERR_PARSE_ERROR; return ERR_PARSE_ERROR;
} }
@ -5668,15 +5655,9 @@ Error GDScriptAnalyzer::analyze() {
} }
resolve_interface(); resolve_interface();
resolve_body(); err = resolve_body();
if (err) {
#ifdef DEBUG_ENABLED return err;
// Apply here, after all `@warning_ignore`s have been resolved and applied.
parser->apply_pending_warnings();
#endif
if (!parser->errors.is_empty()) {
return ERR_PARSE_ERROR;
} }
return resolve_dependencies(); return resolve_dependencies();

View File

@ -40,7 +40,6 @@
class GDScriptAnalyzer { class GDScriptAnalyzer {
GDScriptParser *parser = nullptr; GDScriptParser *parser = nullptr;
HashMap<String, Ref<GDScriptParserRef>> depended_parsers;
const GDScriptParser::EnumNode *current_enum = nullptr; const GDScriptParser::EnumNode *current_enum = nullptr;
GDScriptParser::LambdaNode *current_lambda = nullptr; GDScriptParser::LambdaNode *current_lambda = nullptr;
@ -132,7 +131,6 @@ class GDScriptAnalyzer {
void mark_lambda_use_self(); void mark_lambda_use_self();
void resolve_pending_lambda_bodies(); void resolve_pending_lambda_bodies();
bool class_exists(const StringName &p_class) const; bool class_exists(const StringName &p_class) const;
Ref<GDScriptParserRef> get_parser_for(const String &p_path);
void reduce_identifier_from_base_set_class(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType p_identifier_datatype); void reduce_identifier_from_base_set_class(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType p_identifier_datatype);
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
void is_shadowing(GDScriptParser::IdentifierNode *p_identifier, const String &p_context, const bool p_in_local_scope); void is_shadowing(GDScriptParser::IdentifierNode *p_identifier, const String &p_context, const bool p_in_local_scope);
@ -146,7 +144,6 @@ public:
Error analyze(); Error analyze();
Variant make_variable_default_value(GDScriptParser::VariableNode *p_variable); Variant make_variable_default_value(GDScriptParser::VariableNode *p_variable);
const HashMap<String, Ref<GDScriptParserRef>> &get_depended_parsers();
static bool check_type_compatibility(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion = false, const GDScriptParser::Node *p_source_node = nullptr); static bool check_type_compatibility(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion = false, const GDScriptParser::Node *p_source_node = nullptr);
GDScriptAnalyzer(GDScriptParser *p_parser); GDScriptAnalyzer(GDScriptParser *p_parser);

View File

@ -38,88 +38,98 @@
#include "core/io/file_access.h" #include "core/io/file_access.h"
#include "core/templates/vector.h" #include "core/templates/vector.h"
bool GDScriptParserRef::is_valid() const {
return parser != nullptr;
}
GDScriptParserRef::Status GDScriptParserRef::get_status() const { GDScriptParserRef::Status GDScriptParserRef::get_status() const {
return status; return status;
} }
GDScriptParser *GDScriptParserRef::get_parser() const { uint32_t GDScriptParserRef::get_source_hash() const {
return source_hash;
}
GDScriptParser *GDScriptParserRef::get_parser() {
if (parser == nullptr) {
parser = memnew(GDScriptParser);
}
return parser; return parser;
} }
GDScriptAnalyzer *GDScriptParserRef::get_analyzer() { GDScriptAnalyzer *GDScriptParserRef::get_analyzer() {
if (analyzer == nullptr) { if (analyzer == nullptr) {
analyzer = memnew(GDScriptAnalyzer(parser)); analyzer = memnew(GDScriptAnalyzer(get_parser()));
} }
return analyzer; return analyzer;
} }
Error GDScriptParserRef::raise_status(Status p_new_status) { Error GDScriptParserRef::raise_status(Status p_new_status) {
ERR_FAIL_NULL_V(parser, ERR_INVALID_DATA); ERR_FAIL_COND_V(clearing, ERR_BUG);
ERR_FAIL_COND_V(parser == nullptr && status != EMPTY, ERR_BUG);
if (result != OK) { while (result == OK && p_new_status > status) {
return result;
}
while (p_new_status > status) {
switch (status) { switch (status) {
case EMPTY: { case EMPTY: {
// Calling parse will clear the parser, which can destruct another GDScriptParserRef which can clear the last reference to the script with this path, calling remove_script, which clears this GDScriptParserRef.
// It's ok if its the first thing done here.
get_parser()->clear();
status = PARSED; status = PARSED;
String remapped_path = ResourceLoader::path_remap(path); String remapped_path = ResourceLoader::path_remap(path);
if (remapped_path.get_extension().to_lower() == "gdc") { if (remapped_path.get_extension().to_lower() == "gdc") {
result = parser->parse_binary(GDScriptCache::get_binary_tokens(remapped_path), path); Vector<uint8_t> tokens = GDScriptCache::get_binary_tokens(remapped_path);
source_hash = hash_djb2_buffer(tokens.ptr(), tokens.size());
result = get_parser()->parse_binary(tokens, path);
} else { } else {
result = parser->parse(GDScriptCache::get_source_code(remapped_path), path, false); String source = GDScriptCache::get_source_code(remapped_path);
source_hash = source.hash();
result = get_parser()->parse(source, path, false);
} }
} break; } break;
case PARSED: { case PARSED: {
status = INHERITANCE_SOLVED; status = INHERITANCE_SOLVED;
Error inheritance_result = get_analyzer()->resolve_inheritance(); result = get_analyzer()->resolve_inheritance();
if (result == OK) {
result = inheritance_result;
}
} break; } break;
case INHERITANCE_SOLVED: { case INHERITANCE_SOLVED: {
status = INTERFACE_SOLVED; status = INTERFACE_SOLVED;
Error interface_result = get_analyzer()->resolve_interface(); result = get_analyzer()->resolve_interface();
if (result == OK) {
result = interface_result;
}
} break; } break;
case INTERFACE_SOLVED: { case INTERFACE_SOLVED: {
status = BODY_SOLVED;
result = get_analyzer()->resolve_body();
} break;
case BODY_SOLVED: {
status = FULLY_SOLVED; status = FULLY_SOLVED;
Error body_result = get_analyzer()->resolve_body(); result = get_analyzer()->resolve_dependencies();
if (result == OK) {
result = body_result;
}
} break; } break;
case FULLY_SOLVED: { case FULLY_SOLVED: {
return result; return result;
} }
} }
if (result != OK) {
return result;
}
} }
return result; return result;
} }
void GDScriptParserRef::clear() { void GDScriptParserRef::clear() {
if (cleared) { if (clearing) {
return; return;
} }
cleared = true; clearing = true;
if (parser != nullptr) { GDScriptParser *lparser = parser;
memdelete(parser); GDScriptAnalyzer *lanalyzer = analyzer;
parser = nullptr;
analyzer = nullptr;
status = EMPTY;
result = OK;
source_hash = 0;
clearing = false;
if (lanalyzer != nullptr) {
memdelete(lanalyzer);
} }
if (analyzer != nullptr) { if (lparser != nullptr) {
memdelete(analyzer); memdelete(lparser);
} }
} }
@ -171,8 +181,11 @@ void GDScriptCache::remove_script(const String &p_path) {
} }
if (singleton->parser_map.has(p_path)) { if (singleton->parser_map.has(p_path)) {
singleton->parser_map[p_path]->clear(); // Keep a local reference until it goes out of scope.
// Clearing it can trigger a reference to itself to go out of scope, destructing it before clear finishes.
Ref<GDScriptParserRef> parser_ref = singleton->parser_map[p_path];
singleton->parser_map.erase(p_path); singleton->parser_map.erase(p_path);
parser_ref->clear();
} }
singleton->dependencies.erase(p_path); singleton->dependencies.erase(p_path);
@ -198,9 +211,7 @@ Ref<GDScriptParserRef> GDScriptCache::get_parser(const String &p_path, GDScriptP
r_error = ERR_FILE_NOT_FOUND; r_error = ERR_FILE_NOT_FOUND;
return ref; return ref;
} }
GDScriptParser *parser = memnew(GDScriptParser);
ref.instantiate(); ref.instantiate();
ref->parser = parser;
ref->path = p_path; ref->path = p_path;
singleton->parser_map[p_path] = ref.ptr(); singleton->parser_map[p_path] = ref.ptr();
} }
@ -209,6 +220,17 @@ Ref<GDScriptParserRef> GDScriptCache::get_parser(const String &p_path, GDScriptP
return ref; return ref;
} }
bool GDScriptCache::has_parser(const String &p_path) {
MutexLock lock(singleton->mutex);
return singleton->parser_map.has(p_path);
}
void GDScriptCache::remove_parser(const String &p_path) {
MutexLock lock(singleton->mutex);
// Can't clear the parser because some other parser might be currently using it in the chain of calls.
singleton->parser_map.erase(p_path);
}
String GDScriptCache::get_source_code(const String &p_path) { String GDScriptCache::get_source_code(const String &p_path) {
Vector<uint8_t> source_file; Vector<uint8_t> source_file;
Error err; Error err;
@ -400,13 +422,15 @@ void GDScriptCache::clear() {
parser_map_refs.insert(E.value); parser_map_refs.insert(E.value);
} }
singleton->parser_map.clear();
for (Ref<GDScriptParserRef> &E : parser_map_refs) { for (Ref<GDScriptParserRef> &E : parser_map_refs) {
if (E.is_valid()) if (E.is_valid()) {
E->clear(); E->clear();
}
} }
parser_map_refs.clear(); parser_map_refs.clear();
singleton->parser_map.clear();
singleton->shallow_gdscript_cache.clear(); singleton->shallow_gdscript_cache.clear();
singleton->full_gdscript_cache.clear(); singleton->full_gdscript_cache.clear();
} }

View File

@ -48,6 +48,7 @@ public:
PARSED, PARSED,
INHERITANCE_SOLVED, INHERITANCE_SOLVED,
INTERFACE_SOLVED, INTERFACE_SOLVED,
BODY_SOLVED,
FULLY_SOLVED, FULLY_SOLVED,
}; };
@ -57,14 +58,16 @@ private:
Status status = EMPTY; Status status = EMPTY;
Error result = OK; Error result = OK;
String path; String path;
bool cleared = false; uint32_t source_hash = 0;
bool clearing = false;
friend class GDScriptCache; friend class GDScriptCache;
friend class GDScript;
public: public:
bool is_valid() const;
Status get_status() const; Status get_status() const;
GDScriptParser *get_parser() const; uint32_t get_source_hash() const;
GDScriptParser *get_parser();
GDScriptAnalyzer *get_analyzer(); GDScriptAnalyzer *get_analyzer();
Error raise_status(Status p_new_status); Error raise_status(Status p_new_status);
void clear(); void clear();
@ -95,6 +98,8 @@ public:
static void move_script(const String &p_from, const String &p_to); static void move_script(const String &p_from, const String &p_to);
static void remove_script(const String &p_path); static void remove_script(const String &p_path);
static Ref<GDScriptParserRef> get_parser(const String &p_path, GDScriptParserRef::Status status, Error &r_error, const String &p_owner = String()); static Ref<GDScriptParserRef> get_parser(const String &p_path, GDScriptParserRef::Status status, Error &r_error, const String &p_owner = String());
static bool has_parser(const String &p_path);
static void remove_parser(const String &p_path);
static String get_source_code(const String &p_path); static String get_source_code(const String &p_path);
static Vector<uint8_t> get_binary_tokens(const String &p_path); static Vector<uint8_t> get_binary_tokens(const String &p_path);
static Ref<GDScript> get_shallow_script(const String &p_path, Error &r_error, const String &p_owner = String()); static Ref<GDScript> get_shallow_script(const String &p_path, Error &r_error, const String &p_owner = String());

View File

@ -163,7 +163,7 @@ bool GDScriptLanguage::validate(const String &p_script, const String &p_path, Li
r_errors->push_back(e); r_errors->push_back(e);
} }
for (KeyValue<String, Ref<GDScriptParserRef>> E : analyzer.get_depended_parsers()) { for (KeyValue<String, Ref<GDScriptParserRef>> E : parser.get_depended_parsers()) {
GDScriptParser *depended_parser = E.value->get_parser(); GDScriptParser *depended_parser = E.value->get_parser();
for (const GDScriptParser::ParserError &pe : depended_parser->get_errors()) { for (const GDScriptParser::ParserError &pe : depended_parser->get_errors()) {
ScriptLanguage::ScriptError e; ScriptLanguage::ScriptError e;

View File

@ -147,23 +147,17 @@ GDScriptParser::GDScriptParser() {
} }
GDScriptParser::~GDScriptParser() { GDScriptParser::~GDScriptParser() {
clear();
}
void GDScriptParser::clear() {
while (list != nullptr) { while (list != nullptr) {
Node *element = list; Node *element = list;
list = list->next; list = list->next;
memdelete(element); memdelete(element);
} }
}
head = nullptr; void GDScriptParser::clear() {
list = nullptr; GDScriptParser tmp;
_is_tool = false; tmp = *this;
for_completion = false; *this = GDScriptParser();
errors.clear();
multiline_stack.clear();
nodes_in_progress.clear();
} }
void GDScriptParser::push_error(const String &p_message, const Node *p_origin) { void GDScriptParser::push_error(const String &p_message, const Node *p_origin) {
@ -709,6 +703,25 @@ void GDScriptParser::parse_program() {
clear_unused_annotations(); clear_unused_annotations();
} }
Ref<GDScriptParserRef> GDScriptParser::get_depended_parser_for(const String &p_path) {
Ref<GDScriptParserRef> ref;
if (depended_parsers.has(p_path)) {
ref = depended_parsers[p_path];
} else {
Error err = OK;
ref = GDScriptCache::get_parser(p_path, GDScriptParserRef::EMPTY, err, script_path);
if (ref.is_valid()) {
depended_parsers[p_path] = ref;
}
}
return ref;
}
const HashMap<String, Ref<GDScriptParserRef>> &GDScriptParser::get_depended_parsers() {
return depended_parsers;
}
GDScriptParser::ClassNode *GDScriptParser::find_class(const String &p_qualified_name) const { GDScriptParser::ClassNode *GDScriptParser::find_class(const String &p_qualified_name) const {
String first = p_qualified_name.get_slice("::", 0); String first = p_qualified_name.get_slice("::", 0);
@ -4071,6 +4084,7 @@ bool GDScriptParser::onready_annotation(const AnnotationNode *p_annotation, Node
if (current_class && !ClassDB::is_parent_class(current_class->get_datatype().native_type, SNAME("Node"))) { if (current_class && !ClassDB::is_parent_class(current_class->get_datatype().native_type, SNAME("Node"))) {
push_error(R"("@onready" can only be used in classes that inherit "Node".)", p_annotation); push_error(R"("@onready" can only be used in classes that inherit "Node".)", p_annotation);
return false;
} }
VariableNode *variable = static_cast<VariableNode *>(p_target); VariableNode *variable = static_cast<VariableNode *>(p_target);

View File

@ -1321,6 +1321,7 @@ public:
private: private:
friend class GDScriptAnalyzer; friend class GDScriptAnalyzer;
friend class GDScriptParserRef;
bool _is_tool = false; bool _is_tool = false;
String script_path; String script_path;
@ -1329,6 +1330,7 @@ private:
bool can_break = false; bool can_break = false;
bool can_continue = false; bool can_continue = false;
List<bool> multiline_stack; List<bool> multiline_stack;
HashMap<String, Ref<GDScriptParserRef>> depended_parsers;
ClassNode *head = nullptr; ClassNode *head = nullptr;
Node *list = nullptr; Node *list = nullptr;
@ -1558,6 +1560,8 @@ public:
Error parse_binary(const Vector<uint8_t> &p_binary, const String &p_script_path); Error parse_binary(const Vector<uint8_t> &p_binary, const String &p_script_path);
ClassNode *get_tree() const { return head; } ClassNode *get_tree() const { return head; }
bool is_tool() const { return _is_tool; } bool is_tool() const { return _is_tool; }
Ref<GDScriptParserRef> get_depended_parser_for(const String &p_path);
const HashMap<String, Ref<GDScriptParserRef>> &get_depended_parsers();
ClassNode *find_class(const String &p_qualified_name) const; ClassNode *find_class(const String &p_qualified_name) const;
bool has_class(const GDScriptParser::ClassNode *p_class) const; bool has_class(const GDScriptParser::ClassNode *p_class) const;
static Variant::Type get_builtin_type(const StringName &p_type); // Excluding `Variant::NIL` and `Variant::OBJECT`. static Variant::Type get_builtin_type(const StringName &p_type); // Excluding `Variant::NIL` and `Variant::OBJECT`.