diff options
Diffstat (limited to 'modules/gdscript/gdscript_analyzer.cpp')
-rw-r--r-- | modules/gdscript/gdscript_analyzer.cpp | 1273 |
1 files changed, 591 insertions, 682 deletions
diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index fc2e6e94f3..1b488d560c 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -1,32 +1,32 @@ -/*************************************************************************/ -/* gdscript_analyzer.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 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. */ -/*************************************************************************/ +/**************************************************************************/ +/* gdscript_analyzer.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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" @@ -42,6 +42,9 @@ #include "gdscript_utility_functions.h" #include "scene/resources/packed_scene.h" +#define UNNAMED_ENUM "<anonymous enum>" +#define ENUM_SEPARATOR "::" + static MethodInfo info_from_utility_func(const StringName &p_function) { ERR_FAIL_COND_V(!Variant::has_utility_function(p_function), MethodInfo()); @@ -65,9 +68,6 @@ static MethodInfo info_from_utility_func(const StringName &p_function) { pi.name = "arg" + itos(i + 1); #endif pi.type = Variant::get_utility_function_argument_type(p_function, i); - if (pi.type == Variant::NIL) { - pi.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; - } info.arguments.push_back(pi); } } @@ -100,19 +100,45 @@ static GDScriptParser::DataType make_native_meta_type(const StringName &p_class_ type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; type.kind = GDScriptParser::DataType::NATIVE; type.builtin_type = Variant::OBJECT; - type.is_constant = true; type.native_type = p_class_name; + type.is_constant = true; type.is_meta_type = true; return type; } -static GDScriptParser::DataType make_native_enum_type(const StringName &p_native_class, const StringName &p_enum_name) { +static GDScriptParser::DataType make_script_meta_type(const Ref<Script> &p_script) { GDScriptParser::DataType type; type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; - type.kind = GDScriptParser::DataType::ENUM; - type.builtin_type = Variant::INT; + type.kind = GDScriptParser::DataType::SCRIPT; + type.builtin_type = Variant::OBJECT; + type.native_type = p_script->get_instance_base_type(); + type.script_type = p_script; + type.script_path = p_script->get_path(); type.is_constant = true; type.is_meta_type = true; + return type; +} + +// In enum types, native_type is used to store the class (native or otherwise) that the enum belongs to. +// This disambiguates between similarly named enums in base classes or outer classes +static GDScriptParser::DataType make_enum_type(const StringName &p_enum_name, const String &p_base_name, const bool p_meta = false) { + GDScriptParser::DataType type; + type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + type.kind = GDScriptParser::DataType::ENUM; + type.builtin_type = p_meta ? Variant::DICTIONARY : Variant::INT; + type.enum_type = p_enum_name; + type.is_constant = true; + type.is_meta_type = p_meta; + + // For enums, native_type is only used to check compatibility in is_type_compatible() + // We can set anything readable here for error messages, as long as it uniquely identifies the type of the enum + type.native_type = p_base_name + ENUM_SEPARATOR + p_enum_name; + + return type; +} + +static GDScriptParser::DataType make_native_enum_type(const StringName &p_enum_name, const StringName &p_native_class, const bool p_meta = true) { + GDScriptParser::DataType type = make_enum_type(p_enum_name, p_native_class, p_meta); List<StringName> enum_values; ClassDB::get_enum_constants(p_native_class, p_enum_name, &enum_values); @@ -134,6 +160,19 @@ static GDScriptParser::DataType make_builtin_meta_type(Variant::Type p_type) { return type; } +static StringName enum_get_value_name(const GDScriptParser::DataType p_type, int64_t p_val) { + // Check that an enum has a given value, not key. + // Make sure that implicit conversion to int64_t is sensible before calling! + HashMap<StringName, int64_t>::ConstIterator i = p_type.enum_values.begin(); + while (i) { + if (i->value == p_val) { + return i->key; + } + ++i; + } + return StringName(); +} + bool GDScriptAnalyzer::has_member_name_conflict_in_script_class(const StringName &p_member_name, const GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_member) { if (p_class->members_indices.has(p_member_name)) { int index = p_class->members_indices[p_member_name]; @@ -192,6 +231,7 @@ Error GDScriptAnalyzer::check_native_member_name_conflict(const StringName &p_me } Error GDScriptAnalyzer::check_class_member_name_conflict(const GDScriptParser::ClassNode *p_class_node, const StringName &p_member_name, const GDScriptParser::Node *p_member_node) { + // TODO check outer classes for static members only const GDScriptParser::DataType *current_data_type = &p_class_node->base_type; while (current_data_type && current_data_type->kind == GDScriptParser::DataType::Kind::CLASS) { GDScriptParser::ClassNode *current_class_node = current_data_type->class_type; @@ -220,9 +260,13 @@ Error GDScriptAnalyzer::check_class_member_name_conflict(const GDScriptParser::C } void GDScriptAnalyzer::get_class_node_current_scope_classes(GDScriptParser::ClassNode *p_node, List<GDScriptParser::ClassNode *> *p_list) { + ERR_FAIL_NULL(p_node); + ERR_FAIL_NULL(p_list); + if (p_list->find(p_node) != nullptr) { return; } + p_list->push_back(p_node); // TODO: Try to solve class inheritance if not yet resolving. @@ -387,7 +431,7 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c return err; } base = info_parser->get_parser()->head->get_datatype(); - } else if (class_exists(name) && ClassDB::can_instantiate(name)) { + } else if (class_exists(name)) { base.kind = GDScriptParser::DataType::NATIVE; base.native_type = name; } else { @@ -547,11 +591,8 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type result.builtin_type = GDScriptParser::get_builtin_type(first); if (result.builtin_type == Variant::ARRAY) { - GDScriptParser::DataType container_type = resolve_datatype(p_type->container_type); - + GDScriptParser::DataType container_type = type_from_metatype(resolve_datatype(p_type->container_type)); if (container_type.kind != GDScriptParser::DataType::VARIANT) { - container_type.is_meta_type = false; - container_type.is_constant = false; result.set_container_element_type(container_type); } } @@ -573,12 +614,7 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type } result = ref->get_parser()->head->get_datatype(); } else { - result.kind = GDScriptParser::DataType::SCRIPT; - result.script_type = ResourceLoader::load(path, "Script"); - result.native_type = result.script_type->get_instance_base_type(); - result.script_path = path; - result.is_constant = true; - result.is_meta_type = false; + result = make_script_meta_type(ResourceLoader::load(path, "Script")); } } } else if (ProjectSettings::get_singleton()->has_autoload(first) && ProjectSettings::get_singleton()->get_autoload(first).is_singleton) { @@ -591,12 +627,17 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type result = ref->get_parser()->head->get_datatype(); } else if (ClassDB::has_enum(parser->current_class->base_type.native_type, first)) { // Native enum in current class. - result = make_native_enum_type(parser->current_class->base_type.native_type, first); + result = make_native_enum_type(first, parser->current_class->base_type.native_type); } else { // Classes in current scope. List<GDScriptParser::ClassNode *> script_classes; + bool found = false; get_class_node_current_scope_classes(parser->current_class, &script_classes); for (GDScriptParser::ClassNode *script_class : script_classes) { + if (found) { + break; + } + if (script_class->identifier && script_class->identifier->name == first) { result = script_class->get_datatype(); break; @@ -608,14 +649,16 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type switch (member.type) { case GDScriptParser::ClassNode::Member::CLASS: result = member.get_datatype(); + found = true; break; case GDScriptParser::ClassNode::Member::ENUM: result = member.get_datatype(); + found = true; break; case GDScriptParser::ClassNode::Member::CONSTANT: if (member.get_datatype().is_meta_type) { result = member.get_datatype(); - result.is_meta_type = false; + found = true; break; } else if (Ref<Script>(member.constant->initializer->reduced_value).is_valid()) { Ref<GDScript> gdscript = member.constant->initializer->reduced_value; @@ -626,16 +669,10 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type return bad_type; } result = ref->get_parser()->head->get_datatype(); - result.is_meta_type = false; } else { - Ref<Script> script = member.constant->initializer->reduced_value; - result.kind = GDScriptParser::DataType::SCRIPT; - result.builtin_type = Variant::OBJECT; - result.script_type = script; - result.script_path = script->get_path(); - result.native_type = script->get_instance_base_type(); - result.is_meta_type = false; + result = make_script_meta_type(member.constant->initializer->reduced_value); } + found = true; break; } [[fallthrough]]; @@ -667,15 +704,17 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type } } else if (result.kind == GDScriptParser::DataType::NATIVE) { // Only enums allowed for native. - if (!ClassDB::has_enum(result.native_type, p_type->type_chain[1]->name)) { - push_error(vformat(R"(Could not find nested type "%s" under base "%s".)", p_type->type_chain[1]->name, result.to_string()), p_type->type_chain[1]); - return bad_type; - } - if (p_type->type_chain.size() > 2) { - push_error(R"(Enums cannot contain nested types.)", p_type->type_chain[2]); + if (ClassDB::has_enum(result.native_type, p_type->type_chain[1]->name)) { + if (p_type->type_chain.size() > 2) { + push_error(R"(Enums cannot contain nested types.)", p_type->type_chain[2]); + return bad_type; + } else { + result = make_native_enum_type(p_type->type_chain[1]->name, result.native_type); + } + } else { + push_error(vformat(R"(Could not find type "%s" in "%s".)", p_type->type_chain[1]->name, first), p_type->type_chain[1]); return bad_type; } - result = make_native_enum_type(result.native_type, p_type->type_chain[1]->name); } else { push_error(vformat(R"(Could not find nested type "%s" under base "%s".)", p_type->type_chain[1]->name, result.to_string()), p_type->type_chain[1]); return bad_type; @@ -758,80 +797,8 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class, switch (member.type) { case GDScriptParser::ClassNode::Member::VARIABLE: { check_class_member_name_conflict(p_class, member.variable->identifier->name, member.variable); - member.variable->set_datatype(resolving_datatype); - - GDScriptParser::DataType datatype; - datatype.kind = GDScriptParser::DataType::VARIANT; - datatype.type_source = GDScriptParser::DataType::UNDETECTED; - - GDScriptParser::DataType specified_type; - if (member.variable->datatype_specifier != nullptr) { - specified_type = resolve_datatype(member.variable->datatype_specifier); - specified_type.is_meta_type = false; - } - - if (member.variable->initializer != nullptr) { - reduce_expression(member.variable->initializer); - if ((member.variable->infer_datatype || (member.variable->datatype_specifier != nullptr && specified_type.has_container_element_type())) && member.variable->initializer->type == GDScriptParser::Node::ARRAY) { - // Typed array. - GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(member.variable->initializer); - // Can only infer typed array if it has elements. - if ((member.variable->infer_datatype && array->elements.size() > 0) || member.variable->datatype_specifier != nullptr) { - update_array_literal_element_type(specified_type, array); - } - } - datatype = member.variable->initializer->get_datatype(); - - if (datatype.type_source != GDScriptParser::DataType::UNDETECTED) { - datatype.type_source = GDScriptParser::DataType::INFERRED; - } - - if (!datatype.is_set()) { - push_error(vformat(R"(Could not resolve initializer for member "%s".)", member.variable->identifier->name), member.variable->initializer); - datatype.kind = GDScriptParser::DataType::VARIANT; - } - } - - if (member.variable->datatype_specifier != nullptr) { - datatype = specified_type; - - if (member.variable->initializer != nullptr) { - if (!is_type_compatible(datatype, member.variable->initializer->get_datatype(), true, member.variable->initializer)) { - // Try reverse test since it can be a masked subtype. - if (!is_type_compatible(member.variable->initializer->get_datatype(), datatype, true, member.variable->initializer)) { - push_error(vformat(R"(Value of type "%s" cannot be assigned to a variable of type "%s".)", member.variable->initializer->get_datatype().to_string(), datatype.to_string()), member.variable->initializer); - } else { - // TODO: Add warning. - mark_node_unsafe(member.variable->initializer); - member.variable->use_conversion_assign = true; - } - } else if (datatype.builtin_type == Variant::INT && member.variable->initializer->get_datatype().builtin_type == Variant::FLOAT) { -#ifdef DEBUG_ENABLED - parser->push_warning(member.variable->initializer, GDScriptWarning::NARROWING_CONVERSION); -#endif - } - if (member.variable->initializer->get_datatype().is_variant()) { - // TODO: Warn unsafe assign. - mark_node_unsafe(member.variable->initializer); - member.variable->use_conversion_assign = true; - } - } - } else if (member.variable->infer_datatype) { - if (member.variable->initializer == nullptr) { - push_error(vformat(R"(Cannot infer the type of "%s" variable because there's no default value.)", member.variable->identifier->name), member.variable->identifier); - } else if (!datatype.is_set() || datatype.has_no_type()) { - push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value doesn't have a set type.)", member.variable->identifier->name), member.variable->initializer); - } else if (datatype.is_variant()) { - push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is Variant. Use explicit "Variant" type if this is intended.)", member.variable->identifier->name), member.variable->initializer); - } else if (datatype.builtin_type == Variant::NIL) { - push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is "null".)", member.variable->identifier->name), member.variable->initializer); - } - datatype.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED; - } - - datatype.is_constant = false; - member.variable->set_datatype(datatype); + resolve_variable(member.variable, false); // Apply annotations. for (GDScriptParser::AnnotationNode *&E : member.variable->annotations) { @@ -840,56 +807,8 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class, } break; case GDScriptParser::ClassNode::Member::CONSTANT: { check_class_member_name_conflict(p_class, member.constant->identifier->name, member.constant); - member.constant->set_datatype(resolving_datatype); - - GDScriptParser::DataType specified_type; - if (member.constant->datatype_specifier != nullptr) { - specified_type = resolve_datatype(member.constant->datatype_specifier); - specified_type.is_meta_type = false; - } - - GDScriptParser::DataType datatype; - if (member.constant->initializer) { - reduce_expression(member.constant->initializer); - datatype = member.constant->initializer->get_datatype(); - - if (member.constant->initializer->type == GDScriptParser::Node::ARRAY) { - GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(member.constant->initializer); - const_fold_array(array); - - // Can only infer typed array if it has elements. - if (array->elements.size() > 0 || (member.constant->datatype_specifier != nullptr && specified_type.has_container_element_type())) { - update_array_literal_element_type(specified_type, array); - } - } else if (member.constant->initializer->type == GDScriptParser::Node::DICTIONARY) { - const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(member.constant->initializer)); - } - - if (!datatype.is_set()) { - push_error(vformat(R"(Could not resolve initializer for member "%s".)", member.constant->identifier->name), member.constant->initializer); - datatype.kind = GDScriptParser::DataType::VARIANT; - } - - if (!member.constant->initializer->is_constant) { - push_error(R"(Initializer for a constant must be a constant expression.)", member.constant->initializer); - } - - if (member.constant->datatype_specifier != nullptr) { - datatype = specified_type; - - if (!is_type_compatible(datatype, member.constant->initializer->get_datatype(), true)) { - push_error(vformat(R"(Value of type "%s" cannot be initialized to constant of type "%s".)", member.constant->initializer->get_datatype().to_string(), datatype.to_string()), member.constant->initializer); - } else if (datatype.builtin_type == Variant::INT && member.constant->initializer->get_datatype().builtin_type == Variant::FLOAT) { -#ifdef DEBUG_ENABLED - parser->push_warning(member.constant->initializer, GDScriptWarning::NARROWING_CONVERSION); -#endif - } - } - } - datatype.is_constant = true; - - member.constant->set_datatype(datatype); + resolve_constant(member.constant, false); // Apply annotations. for (GDScriptParser::AnnotationNode *&E : member.constant->annotations) { @@ -907,8 +826,7 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class, for (int j = 0; j < member.signal->parameters.size(); j++) { GDScriptParser::ParameterNode *param = member.signal->parameters[j]; - GDScriptParser::DataType param_type = resolve_datatype(param->datatype_specifier); - param_type.is_meta_type = false; + GDScriptParser::DataType param_type = type_from_metatype(resolve_datatype(param->datatype_specifier)); param->set_datatype(param_type); mi.arguments.push_back(PropertyInfo(param_type.builtin_type, param->identifier->name)); // TODO: add signal parameter default values @@ -924,19 +842,12 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class, check_class_member_name_conflict(p_class, member.m_enum->identifier->name, member.m_enum); member.m_enum->set_datatype(resolving_datatype); - - GDScriptParser::DataType enum_type; - enum_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; - enum_type.kind = GDScriptParser::DataType::ENUM; - enum_type.builtin_type = Variant::DICTIONARY; - enum_type.enum_type = member.m_enum->identifier->name; - enum_type.native_type = p_class->fqcn + "." + member.m_enum->identifier->name; - enum_type.is_meta_type = true; - enum_type.is_constant = true; + GDScriptParser::DataType enum_type = make_enum_type(member.m_enum->identifier->name, p_class->fqcn, true); const GDScriptParser::EnumNode *prev_enum = current_enum; current_enum = member.m_enum; + Dictionary dictionary; for (int j = 0; j < member.m_enum->values.size(); j++) { GDScriptParser::EnumNode::Value &element = member.m_enum->values.write[j]; @@ -960,11 +871,14 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class, } enum_type.enum_values[element.identifier->name] = element.value; + dictionary[String(element.identifier->name)] = element.value; } current_enum = prev_enum; + dictionary.set_read_only(true); member.m_enum->set_datatype(enum_type); + member.m_enum->dictionary = dictionary; // Apply annotations. for (GDScriptParser::AnnotationNode *&E : member.m_enum->annotations) { @@ -975,11 +889,11 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class, resolve_function_signature(member.function, p_source); break; case GDScriptParser::ClassNode::Member::ENUM_VALUE: { + member.enum_value.identifier->set_datatype(resolving_datatype); + if (member.enum_value.custom_value) { check_class_member_name_conflict(p_class, member.enum_value.identifier->name, member.enum_value.custom_value); - member.enum_value.identifier->set_datatype(resolving_datatype); - const GDScriptParser::EnumNode *prev_enum = current_enum; current_enum = member.enum_value.parent_enum; reduce_expression(member.enum_value.custom_value); @@ -1009,11 +923,7 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class, // Also update the original references. member.enum_value.parent_enum->values.set(member.enum_value.index, member.enum_value); - GDScriptParser::DataType datatype; - datatype.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; - datatype.kind = GDScriptParser::DataType::BUILTIN; - datatype.builtin_type = Variant::INT; - member.enum_value.identifier->set_datatype(datatype); + member.enum_value.identifier->set_datatype(make_enum_type(UNNAMED_ENUM, p_class->fqcn, false)); } break; case GDScriptParser::ClassNode::Member::CLASS: check_class_member_name_conflict(p_class, member.m_class->identifier->name, member.m_class); @@ -1307,15 +1217,11 @@ void GDScriptAnalyzer::resolve_node(GDScriptParser::Node *p_node, bool p_is_root } break; case GDScriptParser::Node::CONSTANT: - resolve_constant(static_cast<GDScriptParser::ConstantNode *>(p_node)); + resolve_constant(static_cast<GDScriptParser::ConstantNode *>(p_node), true); break; case GDScriptParser::Node::FOR: resolve_for(static_cast<GDScriptParser::ForNode *>(p_node)); break; - case GDScriptParser::Node::FUNCTION: - resolve_function_signature(static_cast<GDScriptParser::FunctionNode *>(p_node)); - resolve_function_body(static_cast<GDScriptParser::FunctionNode *>(p_node)); - break; case GDScriptParser::Node::IF: resolve_if(static_cast<GDScriptParser::IfNode *>(p_node)); break; @@ -1323,7 +1229,7 @@ void GDScriptAnalyzer::resolve_node(GDScriptParser::Node *p_node, bool p_is_root resolve_suite(static_cast<GDScriptParser::SuiteNode *>(p_node)); break; case GDScriptParser::Node::VARIABLE: - resolve_variable(static_cast<GDScriptParser::VariableNode *>(p_node)); + resolve_variable(static_cast<GDScriptParser::VariableNode *>(p_node), true); break; case GDScriptParser::Node::WHILE: resolve_while(static_cast<GDScriptParser::WhileNode *>(p_node)); @@ -1375,6 +1281,7 @@ void GDScriptAnalyzer::resolve_node(GDScriptParser::Node *p_node, bool p_is_root case GDScriptParser::Node::BREAKPOINT: case GDScriptParser::Node::CONTINUE: case GDScriptParser::Node::ENUM: + case GDScriptParser::Node::FUNCTION: case GDScriptParser::Node::PASS: case GDScriptParser::Node::SIGNAL: // Nothing to do. @@ -1386,13 +1293,15 @@ void GDScriptAnalyzer::resolve_annotation(GDScriptParser::AnnotationNode *p_anno // TODO: Add second validation function for annotations, so they can use checked types. } -void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode *p_function, const GDScriptParser::Node *p_source) { +void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode *p_function, const GDScriptParser::Node *p_source, bool p_is_lambda) { if (p_source == nullptr) { p_source = p_function; } + StringName function_name = p_function->identifier != nullptr ? p_function->identifier->name : StringName(); + if (p_function->get_datatype().is_resolving()) { - push_error(vformat(R"(Could not resolve function "%s": Cyclic reference.)", p_function->identifier->name), p_source); + push_error(vformat(R"(Could not resolve function "%s": Cyclic reference.)", function_name), p_source); return; } @@ -1418,16 +1327,16 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode * resolve_parameter(p_function->parameters[i]); #ifdef DEBUG_ENABLED if (p_function->parameters[i]->usages == 0 && !String(p_function->parameters[i]->identifier->name).begins_with("_")) { - parser->push_warning(p_function->parameters[i]->identifier, GDScriptWarning::UNUSED_PARAMETER, p_function->identifier->name, p_function->parameters[i]->identifier->name); + parser->push_warning(p_function->parameters[i]->identifier, GDScriptWarning::UNUSED_PARAMETER, function_name, p_function->parameters[i]->identifier->name); } is_shadowing(p_function->parameters[i]->identifier, "function parameter"); #endif // DEBUG_ENABLED #ifdef TOOLS_ENABLED - if (p_function->parameters[i]->default_value) { + if (p_function->parameters[i]->initializer) { default_value_count++; - if (p_function->parameters[i]->default_value->is_constant) { - p_function->default_arg_values.push_back(p_function->parameters[i]->default_value->reduced_value); + if (p_function->parameters[i]->initializer->is_constant) { + p_function->default_arg_values.push_back(p_function->parameters[i]->initializer->reduced_value); } else { p_function->default_arg_values.push_back(Variant()); // Prevent shift. } @@ -1435,7 +1344,7 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode * #endif // TOOLS_ENABLED } - if (p_function->identifier->name == GDScriptLanguage::get_singleton()->strings._init) { + if (!p_is_lambda && function_name == GDScriptLanguage::get_singleton()->strings._init) { // Constructor. GDScriptParser::DataType return_type = parser->current_class->get_datatype(); return_type.is_meta_type = false; @@ -1448,7 +1357,7 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode * } } else { if (p_function->return_type != nullptr) { - p_function->set_datatype(resolve_datatype(p_function->return_type)); + p_function->set_datatype(type_from_metatype(resolve_datatype(p_function->return_type))); } else { // In case the function is not typed, we can safely assume it's a Variant, so it's okay to mark as "inferred" here. // It's not "undetected" to not mix up with unknown functions. @@ -1467,7 +1376,7 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode * int default_par_count = 0; bool is_static = false; bool is_vararg = false; - if (get_function_signature(p_function, false, base_type, p_function->identifier->name, parent_return_type, parameters_types, default_par_count, is_static, is_vararg)) { + if (!p_is_lambda && get_function_signature(p_function, false, base_type, function_name, parent_return_type, parameters_types, default_par_count, is_static, is_vararg)) { bool valid = p_function->is_static == is_static; valid = valid && parent_return_type == p_function->get_datatype(); @@ -1482,7 +1391,7 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode * if (!valid) { // Compute parent signature as a string to show in the error message. - String parent_signature = p_function->identifier->name.operator String() + "("; + String parent_signature = String(function_name) + "("; int j = 0; for (const GDScriptParser::DataType &par_type : parameters_types) { if (j > 0) { @@ -1521,7 +1430,7 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode * parser->current_function = previous_function; } -void GDScriptAnalyzer::resolve_function_body(GDScriptParser::FunctionNode *p_function) { +void GDScriptAnalyzer::resolve_function_body(GDScriptParser::FunctionNode *p_function, bool p_is_lambda) { if (p_function->resolved_body) { return; } @@ -1539,7 +1448,7 @@ void GDScriptAnalyzer::resolve_function_body(GDScriptParser::FunctionNode *p_fun return_type.type_source = GDScriptParser::DataType::INFERRED; p_function->set_datatype(p_function->body->get_datatype()); } else if (p_function->get_datatype().is_hard_type() && (p_function->get_datatype().kind != GDScriptParser::DataType::BUILTIN || p_function->get_datatype().builtin_type != Variant::NIL)) { - if (!p_function->body->has_return && p_function->identifier->name != GDScriptLanguage::get_singleton()->strings._init) { + if (!p_function->body->has_return && (p_is_lambda || p_function->identifier->name != GDScriptLanguage::get_singleton()->strings._init)) { push_error(R"(Not all code paths return a value.)", p_function); } } @@ -1598,6 +1507,131 @@ void GDScriptAnalyzer::resolve_suite(GDScriptParser::SuiteNode *p_suite) { } } +void GDScriptAnalyzer::resolve_assignable(GDScriptParser::AssignableNode *p_assignable, const char *p_kind) { + GDScriptParser::DataType type; + type.kind = GDScriptParser::DataType::VARIANT; + + bool is_constant = p_assignable->type == GDScriptParser::Node::CONSTANT; + + GDScriptParser::DataType specified_type; + bool has_specified_type = p_assignable->datatype_specifier != nullptr; + if (has_specified_type) { + specified_type = type_from_metatype(resolve_datatype(p_assignable->datatype_specifier)); + type = specified_type; + } + + if (p_assignable->initializer != nullptr) { + reduce_expression(p_assignable->initializer); + + if (p_assignable->initializer->type == GDScriptParser::Node::ARRAY) { + GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(p_assignable->initializer); + if ((p_assignable->infer_datatype && array->elements.size() > 0) || (has_specified_type && specified_type.has_container_element_type())) { + update_array_literal_element_type(specified_type, array); + } + } + + if (is_constant) { + if (p_assignable->initializer->type == GDScriptParser::Node::ARRAY) { + const_fold_array(static_cast<GDScriptParser::ArrayNode *>(p_assignable->initializer), true); + } else if (p_assignable->initializer->type == GDScriptParser::Node::DICTIONARY) { + const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_assignable->initializer), true); + } + if (!p_assignable->initializer->is_constant) { + push_error(vformat(R"(Assigned value for %s "%s" isn't a constant expression.)", p_kind, p_assignable->identifier->name), p_assignable->initializer); + } + } + + GDScriptParser::DataType initializer_type = p_assignable->initializer->get_datatype(); + + if (p_assignable->infer_datatype) { + if (!initializer_type.is_set() || initializer_type.has_no_type()) { + push_error(vformat(R"(Cannot infer the type of "%s" %s because the value doesn't have a set type.)", p_assignable->identifier->name, p_kind), p_assignable->initializer); + } else if (initializer_type.is_variant() && !initializer_type.is_hard_type()) { + push_error(vformat(R"(Cannot infer the type of "%s" %s because the value is Variant. Use explicit "Variant" type if this is intended.)", p_assignable->identifier->name, p_kind), p_assignable->initializer); + } else if (initializer_type.kind == GDScriptParser::DataType::BUILTIN && initializer_type.builtin_type == Variant::NIL && !is_constant) { + push_error(vformat(R"(Cannot infer the type of "%s" %s because the value is "null".)", p_assignable->identifier->name, p_kind), p_assignable->initializer); + } + } else { + if (!initializer_type.is_set()) { + push_error(vformat(R"(Could not resolve type for %s "%s".)", p_kind, p_assignable->identifier->name), p_assignable->initializer); + } + } + + if (!has_specified_type) { + type = initializer_type; + + if (!type.is_set() || (type.is_hard_type() && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL && !is_constant)) { + type.kind = GDScriptParser::DataType::VARIANT; + } + + if (p_assignable->infer_datatype || is_constant) { + type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED; + } else { + type.type_source = GDScriptParser::DataType::INFERRED; + } + } else if (!specified_type.is_variant()) { + if (initializer_type.is_variant() || !initializer_type.is_hard_type()) { + mark_node_unsafe(p_assignable->initializer); + p_assignable->use_conversion_assign = true; + if (!initializer_type.is_variant() && !is_type_compatible(specified_type, initializer_type, true, p_assignable->initializer)) { + downgrade_node_type_source(p_assignable->initializer); + } + } else if (!is_type_compatible(specified_type, initializer_type, true, p_assignable->initializer)) { + if (!is_constant && is_type_compatible(initializer_type, specified_type, true, p_assignable->initializer)) { + mark_node_unsafe(p_assignable->initializer); + p_assignable->use_conversion_assign = true; + } else { + push_error(vformat(R"(Cannot assign a value of type %s to %s "%s" with specified type %s.)", initializer_type.to_string(), p_kind, p_assignable->identifier->name, specified_type.to_string()), p_assignable->initializer); + } +#ifdef DEBUG_ENABLED + } else if (specified_type.builtin_type == Variant::INT && initializer_type.builtin_type == Variant::FLOAT) { + parser->push_warning(p_assignable->initializer, GDScriptWarning::NARROWING_CONVERSION); +#endif + } + } + } + + type.is_constant = is_constant; + p_assignable->set_datatype(type); +} + +void GDScriptAnalyzer::resolve_variable(GDScriptParser::VariableNode *p_variable, bool p_is_local) { + static constexpr const char *kind = "variable"; + resolve_assignable(p_variable, kind); + +#ifdef DEBUG_ENABLED + if (p_is_local) { + if (p_variable->usages == 0 && !String(p_variable->identifier->name).begins_with("_")) { + parser->push_warning(p_variable, GDScriptWarning::UNUSED_VARIABLE, p_variable->identifier->name); + } else if (p_variable->assignments == 0) { + parser->push_warning(p_variable, GDScriptWarning::UNASSIGNED_VARIABLE, p_variable->identifier->name); + } + + is_shadowing(p_variable->identifier, kind); + } +#endif +} + +void GDScriptAnalyzer::resolve_constant(GDScriptParser::ConstantNode *p_constant, bool p_is_local) { + static constexpr const char *kind = "constant"; + resolve_assignable(p_constant, kind); + +#ifdef DEBUG_ENABLED + if (p_is_local) { + if (p_constant->usages == 0) { + parser->push_warning(p_constant, GDScriptWarning::UNUSED_LOCAL_CONSTANT, p_constant->identifier->name); + } + + is_shadowing(p_constant->identifier, kind); + } +#endif +} + +void GDScriptAnalyzer::resolve_parameter(GDScriptParser::ParameterNode *p_parameter) { + static constexpr const char *kind = "parameter"; + resolve_assignable(p_parameter, kind); +} + void GDScriptAnalyzer::resolve_if(GDScriptParser::IfNode *p_if) { reduce_expression(p_if->condition); @@ -1725,160 +1759,6 @@ void GDScriptAnalyzer::resolve_while(GDScriptParser::WhileNode *p_while) { p_while->set_datatype(p_while->loop->get_datatype()); } -void GDScriptAnalyzer::resolve_variable(GDScriptParser::VariableNode *p_variable) { - GDScriptParser::DataType type; - type.kind = GDScriptParser::DataType::VARIANT; // By default. - - GDScriptParser::DataType specified_type; - if (p_variable->datatype_specifier != nullptr) { - specified_type = resolve_datatype(p_variable->datatype_specifier); - specified_type.is_meta_type = false; - } - - if (p_variable->initializer != nullptr) { - reduce_expression(p_variable->initializer); - if ((p_variable->infer_datatype || (p_variable->datatype_specifier != nullptr && specified_type.has_container_element_type())) && p_variable->initializer->type == GDScriptParser::Node::ARRAY) { - // Typed array. - GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(p_variable->initializer); - // Can only infer typed array if it has elements. - if ((p_variable->infer_datatype && array->elements.size() > 0) || p_variable->datatype_specifier != nullptr) { - update_array_literal_element_type(specified_type, array); - } - } - - type = p_variable->initializer->get_datatype(); - -#ifdef DEBUG_ENABLED - if (p_variable->initializer->type == GDScriptParser::Node::CALL && type.is_hard_type() && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) { - parser->push_warning(p_variable->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_variable->initializer)->function_name); - } -#endif - - if (p_variable->infer_datatype) { - type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED; - - if (type.has_no_type()) { - push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value does not have a set type.)", p_variable->identifier->name), p_variable->initializer); - } else if (type.is_variant()) { - push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value is a variant. Use explicit "Variant" type if this is intended.)", p_variable->identifier->name), p_variable->initializer); - } else if (type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) { - push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value is "null".)", p_variable->identifier->name), p_variable->initializer); - } - } else { - type.type_source = GDScriptParser::DataType::INFERRED; - } - } - - if (p_variable->datatype_specifier != nullptr) { - type = specified_type; - type.is_meta_type = false; - - if (p_variable->initializer != nullptr) { - if (!is_type_compatible(type, p_variable->initializer->get_datatype(), true, p_variable->initializer)) { - // Try reverse test since it can be a masked subtype. - if (!is_type_compatible(p_variable->initializer->get_datatype(), type, true, p_variable->initializer)) { - push_error(vformat(R"(Value of type "%s" cannot be assigned to a variable of type "%s".)", p_variable->initializer->get_datatype().to_string(), type.to_string()), p_variable->initializer); - } else { - // TODO: Add warning. - mark_node_unsafe(p_variable->initializer); - p_variable->use_conversion_assign = true; - } -#ifdef DEBUG_ENABLED - } else if (type.builtin_type == Variant::INT && p_variable->initializer->get_datatype().builtin_type == Variant::FLOAT) { - parser->push_warning(p_variable->initializer, GDScriptWarning::NARROWING_CONVERSION); -#endif - } - if (p_variable->initializer->get_datatype().is_variant() && !type.is_variant()) { - // TODO: Warn unsafe assign. - mark_node_unsafe(p_variable->initializer); - p_variable->use_conversion_assign = true; - } - } - } else if (p_variable->infer_datatype) { - if (type.has_no_type()) { - push_error(vformat(R"(Cannot infer the type of variable "%s" because the initial value doesn't have a set type.)", p_variable->identifier->name), p_variable->identifier); - } - type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED; - } - - type.is_constant = false; - p_variable->set_datatype(type); - -#ifdef DEBUG_ENABLED - if (p_variable->usages == 0 && !String(p_variable->identifier->name).begins_with("_")) { - parser->push_warning(p_variable, GDScriptWarning::UNUSED_VARIABLE, p_variable->identifier->name); - } else if (p_variable->assignments == 0) { - parser->push_warning(p_variable, GDScriptWarning::UNASSIGNED_VARIABLE, p_variable->identifier->name); - } - - is_shadowing(p_variable->identifier, "variable"); -#endif -} - -void GDScriptAnalyzer::resolve_constant(GDScriptParser::ConstantNode *p_constant) { - GDScriptParser::DataType type; - - GDScriptParser::DataType explicit_type; - if (p_constant->datatype_specifier != nullptr) { - explicit_type = resolve_datatype(p_constant->datatype_specifier); - explicit_type.is_meta_type = false; - } - - if (p_constant->initializer != nullptr) { - reduce_expression(p_constant->initializer); - if (p_constant->initializer->type == GDScriptParser::Node::ARRAY) { - GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(p_constant->initializer); - const_fold_array(array); - - // Can only infer typed array if it has elements. - if (array->elements.size() > 0 || (p_constant->datatype_specifier != nullptr && explicit_type.has_container_element_type())) { - update_array_literal_element_type(explicit_type, array); - } - } else if (p_constant->initializer->type == GDScriptParser::Node::DICTIONARY) { - const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_constant->initializer)); - } - - if (!p_constant->initializer->is_constant) { - push_error(vformat(R"(Assigned value for constant "%s" isn't a constant expression.)", p_constant->identifier->name), p_constant->initializer); - } - - type = p_constant->initializer->get_datatype(); - -#ifdef DEBUG_ENABLED - if (p_constant->initializer->type == GDScriptParser::Node::CALL && type.is_hard_type() && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) { - parser->push_warning(p_constant->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_constant->initializer)->function_name); - } -#endif - } - - if (p_constant->datatype_specifier != nullptr) { - if (!is_type_compatible(explicit_type, type, true)) { - push_error(vformat(R"(Assigned value for constant "%s" has type %s which is not compatible with defined type %s.)", p_constant->identifier->name, type.to_string(), explicit_type.to_string()), p_constant->initializer); -#ifdef DEBUG_ENABLED - } else if (explicit_type.builtin_type == Variant::INT && type.builtin_type == Variant::FLOAT) { - parser->push_warning(p_constant->initializer, GDScriptWarning::NARROWING_CONVERSION); -#endif - } - type = explicit_type; - } else if (p_constant->infer_datatype) { - if (type.has_no_type()) { - push_error(vformat(R"(Cannot infer the type of constant "%s" because the initial value doesn't have a set type.)", p_constant->identifier->name), p_constant->identifier); - } - type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED; - } - - type.is_constant = true; - p_constant->set_datatype(type); - -#ifdef DEBUG_ENABLED - if (p_constant->usages == 0) { - parser->push_warning(p_constant, GDScriptWarning::UNUSED_LOCAL_CONSTANT, p_constant->identifier->name); - } - - is_shadowing(p_constant->identifier, "constant"); -#endif -} - void GDScriptAnalyzer::resolve_assert(GDScriptParser::AssertNode *p_assert) { reduce_expression(p_assert->condition); if (p_assert->message != nullptr) { @@ -1990,41 +1870,6 @@ void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_matc p_match_pattern->set_datatype(result); } -void GDScriptAnalyzer::resolve_parameter(GDScriptParser::ParameterNode *p_parameter) { - GDScriptParser::DataType result; - result.kind = GDScriptParser::DataType::VARIANT; - - if (p_parameter->default_value != nullptr) { - reduce_expression(p_parameter->default_value); - result = p_parameter->default_value->get_datatype(); - if (p_parameter->infer_datatype) { - result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED; - } else { - result.type_source = GDScriptParser::DataType::INFERRED; - } - } - - if (p_parameter->datatype_specifier != nullptr) { - result = resolve_datatype(p_parameter->datatype_specifier); - result.is_meta_type = false; - - if (p_parameter->default_value != nullptr) { - if (!is_type_compatible(result, p_parameter->default_value->get_datatype())) { - push_error(vformat(R"(Type of default value for parameter "%s" (%s) is not compatible with parameter type (%s).)", p_parameter->identifier->name, p_parameter->default_value->get_datatype().to_string(), p_parameter->datatype_specifier->get_datatype().to_string()), p_parameter->default_value); - } else if (p_parameter->default_value->get_datatype().is_variant()) { - mark_node_unsafe(p_parameter); - } - } - } - - if (result.builtin_type == Variant::Type::NIL && result.type_source == GDScriptParser::DataType::ANNOTATED_INFERRED && p_parameter->datatype_specifier == nullptr) { - push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value is "null".)", p_parameter->identifier->name), p_parameter->default_value); - } - - result.is_constant = false; - p_parameter->set_datatype(result); -} - void GDScriptAnalyzer::resolve_return(GDScriptParser::ReturnNode *p_return) { GDScriptParser::DataType result; @@ -2044,6 +1889,9 @@ void GDScriptAnalyzer::resolve_return(GDScriptParser::ReturnNode *p_return) { update_array_literal_element_type(expected_type, static_cast<GDScriptParser::ArrayNode *>(p_return->return_value)); } } + if (has_expected_type && expected_type.is_hard_type() && expected_type.kind == GDScriptParser::DataType::BUILTIN && expected_type.builtin_type == Variant::NIL) { + push_error("A void function cannot return a value.", p_return); + } result = p_return->return_value->get_datatype(); } else { // Return type is null by default. @@ -2241,6 +2089,10 @@ void GDScriptAnalyzer::reduce_assignment(GDScriptParser::AssignmentNode *p_assig GDScriptParser::DataType assignee_type = p_assignment->assignee->get_datatype(); + if (assignee_type.is_constant || (p_assignment->assignee->type == GDScriptParser::Node::SUBSCRIPT && static_cast<GDScriptParser::SubscriptNode *>(p_assignment->assignee)->base->is_constant)) { + push_error("Cannot assign a new value to a constant.", p_assignment->assignee); + } + // Check if assigned value is an array literal, so we can make it a typed array too if appropriate. if (assignee_type.has_container_element_type() && p_assignment->assigned_value->type == GDScriptParser::Node::ARRAY) { update_array_literal_element_type(assignee_type, static_cast<GDScriptParser::ArrayNode *>(p_assignment->assigned_value)); @@ -2248,94 +2100,88 @@ void GDScriptAnalyzer::reduce_assignment(GDScriptParser::AssignmentNode *p_assig GDScriptParser::DataType assigned_value_type = p_assignment->assigned_value->get_datatype(); - if (assignee_type.is_constant) { - push_error("Cannot assign a new value to a constant.", p_assignment->assignee); - } - + bool assignee_is_variant = assignee_type.is_variant(); + bool assignee_is_hard = assignee_type.is_hard_type(); + bool assigned_is_variant = assigned_value_type.is_variant(); + bool assigned_is_hard = assigned_value_type.is_hard_type(); bool compatible = true; + bool downgrades_assignee = false; + bool downgrades_assigned = false; GDScriptParser::DataType op_type = assigned_value_type; - if (p_assignment->operation != GDScriptParser::AssignmentNode::OP_NONE) { + if (p_assignment->operation != GDScriptParser::AssignmentNode::OP_NONE && !op_type.is_variant()) { op_type = get_operation_type(p_assignment->variant_op, assignee_type, assigned_value_type, compatible, p_assignment->assigned_value); + + if (assignee_is_variant) { + // variant assignee + mark_node_unsafe(p_assignment); + } else if (!compatible) { + // incompatible hard types and non-variant assignee + mark_node_unsafe(p_assignment); + if (assigned_is_variant) { + // incompatible hard non-variant assignee and hard variant assigned + p_assignment->use_conversion_assign = true; + } else { + // incompatible hard non-variant types + push_error(vformat(R"(Invalid operands "%s" and "%s" for assignment operator.)", assignee_type.to_string(), assigned_value_type.to_string()), p_assignment); + } + } else if (op_type.type_source == GDScriptParser::DataType::UNDETECTED && !assigned_is_variant) { + // incompatible non-variant types (at least one weak) + downgrades_assignee = !assignee_is_hard; + downgrades_assigned = !assigned_is_hard; + } } p_assignment->set_datatype(op_type); - if (!assignee_type.is_variant() && assigned_value_type.is_hard_type()) { - if (compatible) { - compatible = is_type_compatible(assignee_type, op_type, true, p_assignment->assigned_value); - if (!compatible) { - if (assignee_type.is_hard_type()) { - // Try reverse test since it can be a masked subtype. - if (!is_type_compatible(op_type, assignee_type, true, p_assignment->assigned_value)) { - push_error(vformat(R"(Cannot assign a value of type "%s" to a target of type "%s".)", assigned_value_type.to_string(), assignee_type.to_string()), p_assignment->assigned_value); - } else { - // TODO: Add warning. - mark_node_unsafe(p_assignment); + if (assignee_is_variant) { + if (!assignee_is_hard) { + // weak variant assignee + mark_node_unsafe(p_assignment); + } + } else { + if (assignee_is_hard && !assigned_is_hard) { + // hard non-variant assignee and weak assigned + mark_node_unsafe(p_assignment); + p_assignment->use_conversion_assign = true; + downgrades_assigned = downgrades_assigned || (!assigned_is_variant && !is_type_compatible(assignee_type, op_type, true, p_assignment->assigned_value)); + } else if (compatible) { + if (op_type.is_variant()) { + // non-variant assignee and variant result + mark_node_unsafe(p_assignment); + if (assignee_is_hard) { + // hard non-variant assignee and variant result + p_assignment->use_conversion_assign = true; + } else { + // weak non-variant assignee and variant result + downgrades_assignee = true; + } + } else if (!is_type_compatible(assignee_type, op_type, assignee_is_hard, p_assignment->assigned_value)) { + // non-variant assignee and incompatible result + mark_node_unsafe(p_assignment); + if (assignee_is_hard) { + if (is_type_compatible(op_type, assignee_type, true, p_assignment->assigned_value)) { + // hard non-variant assignee and maybe compatible result p_assignment->use_conversion_assign = true; + } else { + // hard non-variant assignee and incompatible result + push_error(vformat(R"(Value of type "%s" cannot be assigned to a variable of type "%s".)", assigned_value_type.to_string(), assignee_type.to_string()), p_assignment->assigned_value); } } else { - // TODO: Warning in this case. - mark_node_unsafe(p_assignment); + // weak non-variant assignee and incompatible result + downgrades_assignee = true; } } - } else { - push_error(vformat(R"(Invalid operands "%s" and "%s" for assignment operator.)", assignee_type.to_string(), assigned_value_type.to_string()), p_assignment); } } - if (assignee_type.has_no_type() || assigned_value_type.is_variant()) { - mark_node_unsafe(p_assignment); - if (assignee_type.is_hard_type() && !assignee_type.is_variant()) { - p_assignment->use_conversion_assign = true; - } + if (downgrades_assignee) { + downgrade_node_type_source(p_assignment->assignee); } - - if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) { - // Change source type so it's not wrongly detected later. - GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(p_assignment->assignee); - - switch (identifier->source) { - case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: { - GDScriptParser::DataType id_type = identifier->variable_source->get_datatype(); - if (!id_type.is_hard_type()) { - id_type.kind = GDScriptParser::DataType::VARIANT; - id_type.type_source = GDScriptParser::DataType::UNDETECTED; - identifier->variable_source->set_datatype(id_type); - } - } break; - case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER: { - GDScriptParser::DataType id_type = identifier->parameter_source->get_datatype(); - if (!id_type.is_hard_type()) { - id_type.kind = GDScriptParser::DataType::VARIANT; - id_type.type_source = GDScriptParser::DataType::UNDETECTED; - identifier->parameter_source->set_datatype(id_type); - } - } break; - case GDScriptParser::IdentifierNode::LOCAL_VARIABLE: { - GDScriptParser::DataType id_type = identifier->variable_source->get_datatype(); - if (!id_type.is_hard_type()) { - id_type.kind = GDScriptParser::DataType::VARIANT; - id_type.type_source = GDScriptParser::DataType::UNDETECTED; - identifier->variable_source->set_datatype(id_type); - } - } break; - case GDScriptParser::IdentifierNode::LOCAL_ITERATOR: { - GDScriptParser::DataType id_type = identifier->bind_source->get_datatype(); - if (!id_type.is_hard_type()) { - id_type.kind = GDScriptParser::DataType::VARIANT; - id_type.type_source = GDScriptParser::DataType::UNDETECTED; - identifier->variable_source->set_datatype(id_type); - } - } break; - default: - // Nothing to do. - break; - } + if (downgrades_assigned) { + downgrade_node_type_source(p_assignment->assigned_value); } #ifdef DEBUG_ENABLED - if (p_assignment->assigned_value->type == GDScriptParser::Node::CALL && assigned_value_type.is_hard_type() && assigned_value_type.kind == GDScriptParser::DataType::BUILTIN && assigned_value_type.builtin_type == Variant::NIL) { - parser->push_warning(p_assignment->assigned_value, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_assignment->assigned_value)->function_name); - } else if (assignee_type.is_hard_type() && assignee_type.builtin_type == Variant::INT && assigned_value_type.builtin_type == Variant::FLOAT) { + if (assignee_type.is_hard_type() && assignee_type.builtin_type == Variant::INT && assigned_value_type.builtin_type == Variant::FLOAT) { parser->push_warning(p_assignment->assigned_value, GDScriptWarning::NARROWING_CONVERSION); } #endif @@ -2552,7 +2398,7 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a switch (err.error) { case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: - push_error(vformat(R"(Invalid argument for %s constructor: argument %d should be %s but is %s.)", Variant::get_type_name(builtin_type), err.argument + 1, + push_error(vformat(R"(Invalid argument for %s constructor: argument %d should be "%s" but is "%s".)", Variant::get_type_name(builtin_type), err.argument + 1, Variant::get_type_name(Variant::Type(err.expected)), p_call->arguments[err.argument]->get_datatype().to_string()), p_call->arguments[err.argument]); break; @@ -2612,7 +2458,7 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a bool types_match = true; for (int i = 0; i < p_call->arguments.size(); i++) { - GDScriptParser::DataType par_type = type_from_property(info.arguments[i]); + GDScriptParser::DataType par_type = type_from_property(info.arguments[i], true); if (!is_type_compatible(par_type, p_call->arguments[i]->get_datatype(), true)) { types_match = false; @@ -2650,6 +2496,10 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a } else if (GDScriptUtilityFunctions::function_exists(function_name)) { MethodInfo function_info = GDScriptUtilityFunctions::get_function_info(function_name); + if (!p_is_root && !p_is_await && function_info.return_val.type == Variant::NIL && ((function_info.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) == 0)) { + push_error(vformat(R"*(Cannot get return value of call to "%s()" because it returns "void".)*", function_name), p_call); + } + if (all_is_constant && GDScriptUtilityFunctions::is_function_constant(function_name)) { // Can call on compilation. Vector<const Variant *> args; @@ -2664,8 +2514,8 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a switch (err.error) { case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: { PropertyInfo wrong_arg = function_info.arguments[err.argument]; - push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1, - type_from_property(wrong_arg).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()), + push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be "%s" but is "%s".)*", function_name, err.argument + 1, + type_from_property(wrong_arg, true).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()), p_call->arguments[err.argument]); } break; case Callable::CallError::CALL_ERROR_INVALID_METHOD: @@ -2693,6 +2543,10 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a } else if (Variant::has_utility_function(function_name)) { MethodInfo function_info = info_from_utility_func(function_name); + if (!p_is_root && !p_is_await && function_info.return_val.type == Variant::NIL && ((function_info.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) == 0)) { + push_error(vformat(R"*(Cannot get return value of call to "%s()" because it returns "void".)*", function_name), p_call); + } + if (all_is_constant && Variant::get_utility_function_type(function_name) == Variant::UTILITY_FUNC_TYPE_MATH) { // Can call on compilation. Vector<const Variant *> args; @@ -2708,12 +2562,12 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: { String expected_type_name; if (err.argument < function_info.arguments.size()) { - expected_type_name = type_from_property(function_info.arguments[err.argument]).to_string(); + expected_type_name = type_from_property(function_info.arguments[err.argument], true).to_string(); } else { expected_type_name = Variant::get_type_name((Variant::Type)err.expected); } - push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1, + push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be "%s" but is "%s".)*", function_name, err.argument + 1, expected_type_name, p_call->arguments[err.argument]->get_datatype().to_string()), p_call->arguments[err.argument]); } break; @@ -2789,6 +2643,7 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a } else { reduce_expression(subscript->base); base_type = subscript->base->get_datatype(); + is_self = subscript->base->type == GDScriptParser::Node::SELF; } } else { // Invalid call. Error already sent in parser. @@ -2835,6 +2690,10 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a mark_lambda_use_self(); } + if (!p_is_root && !p_is_await && return_type.is_hard_type() && return_type.kind == GDScriptParser::DataType::BUILTIN && return_type.builtin_type == Variant::NIL) { + push_error(vformat(R"*(Cannot get return value of call to "%s()" because it returns "void".)*", p_call->function_name), p_call); + } + #ifdef DEBUG_ENABLED if (p_is_root && return_type.kind != GDScriptParser::DataType::UNRESOLVED && return_type.builtin_type != Variant::NIL) { parser->push_warning(p_call, GDScriptWarning::RETURN_VALUE_DISCARDED, p_call->function_name); @@ -2855,8 +2714,10 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a } else { bool found = false; - // Check if the name exists as something else. - if (!p_call->is_super && callee_type != GDScriptParser::Node::NONE) { + // Enums do not have functions other than the built-in dictionary ones. + if (base_type.kind == GDScriptParser::DataType::ENUM && base_type.is_meta_type) { + push_error(vformat(R"*(Enums only have Dictionary built-in methods. Function "%s()" does not exist for enum "%s".)*", p_call->function_name, base_type.enum_type), p_call->callee); + } else if (!p_call->is_super && callee_type != GDScriptParser::Node::NONE) { // Check if the name exists as something else. GDScriptParser::IdentifierNode *callee_id; if (callee_type == GDScriptParser::Node::IDENTIFIER) { callee_id = static_cast<GDScriptParser::IdentifierNode *>(p_call->callee); @@ -2886,7 +2747,7 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a String base_name = is_self && !p_call->is_super ? "self" : base_type.to_string(); push_error(vformat(R"*(Function "%s()" not found in base %s.)*", p_call->function_name, base_name), p_call->is_super ? p_call : p_call->callee); } else if (!found && (!p_call->is_super && base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::NATIVE && base_type.is_meta_type)) { - push_error(vformat(R"*(Static function "%s()" not found in base "%s".)*", p_call->function_name, base_type.native_type.operator String()), p_call); + push_error(vformat(R"*(Static function "%s()" not found in base "%s".)*", p_call->function_name, base_type.native_type), p_call); } } @@ -2900,33 +2761,61 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) { reduce_expression(p_cast->operand); - GDScriptParser::DataType cast_type = resolve_datatype(p_cast->cast_type); + GDScriptParser::DataType cast_type = type_from_metatype(resolve_datatype(p_cast->cast_type)); if (!cast_type.is_set()) { mark_node_unsafe(p_cast); return; } - cast_type = type_from_metatype(cast_type); // The casted value won't be a type name. p_cast->set_datatype(cast_type); if (!cast_type.is_variant()) { GDScriptParser::DataType op_type = p_cast->operand->get_datatype(); if (!op_type.is_variant()) { bool valid = false; + bool more_informative_error = false; if (op_type.kind == GDScriptParser::DataType::ENUM && cast_type.kind == GDScriptParser::DataType::ENUM) { - // Enum types are compatible between each other, so it's a safe cast. - valid = true; + // Enum casts are compatible when value from operand exists in target enum + if (p_cast->operand->is_constant && p_cast->operand->reduced) { + if (enum_get_value_name(cast_type, p_cast->operand->reduced_value) != StringName()) { + valid = true; + } else { + valid = false; + more_informative_error = true; + push_error(vformat(R"(Invalid cast. Enum "%s" does not have value corresponding to "%s.%s" (%d).)", + cast_type.to_string(), op_type.enum_type, + enum_get_value_name(op_type, p_cast->operand->reduced_value), // Can never be null + p_cast->operand->reduced_value.operator uint64_t()), + p_cast->cast_type); + } + } else { + // Can't statically tell whether int has a corresponding enum value. Valid but dangerous! + mark_node_unsafe(p_cast); + valid = true; + } } else if (op_type.kind == GDScriptParser::DataType::BUILTIN && op_type.builtin_type == Variant::INT && cast_type.kind == GDScriptParser::DataType::ENUM) { - // Convertint int to enum is always valid. - valid = true; + // Int assignment to enum not valid when exact int assigned is known but is not an enum value + if (p_cast->operand->is_constant && p_cast->operand->reduced) { + if (enum_get_value_name(cast_type, p_cast->operand->reduced_value) != StringName()) { + valid = true; + } else { + valid = false; + more_informative_error = true; + push_error(vformat(R"(Invalid cast. Enum "%s" does not have enum value %d.)", cast_type.to_string(), p_cast->operand->reduced_value.operator uint64_t()), p_cast->cast_type); + } + } else { + // Can't statically tell whether int has a corresponding enum value. Valid but dangerous! + mark_node_unsafe(p_cast); + valid = true; + } } else if (op_type.kind == GDScriptParser::DataType::BUILTIN && cast_type.kind == GDScriptParser::DataType::BUILTIN) { valid = Variant::can_convert(op_type.builtin_type, cast_type.builtin_type); } else if (op_type.kind != GDScriptParser::DataType::BUILTIN && cast_type.kind != GDScriptParser::DataType::BUILTIN) { valid = is_type_compatible(cast_type, op_type) || is_type_compatible(op_type, cast_type); } - if (!valid) { + if (!valid && !more_informative_error) { push_error(vformat(R"(Invalid cast. Cannot convert from "%s" to "%s".)", op_type.to_string(), cast_type.to_string()), p_cast->cast_type); } } @@ -3012,18 +2901,22 @@ GDScriptParser::DataType GDScriptAnalyzer::make_global_class_meta_type(const Str return ref->get_parser()->head->get_datatype(); } else { - type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; - type.kind = GDScriptParser::DataType::SCRIPT; - type.builtin_type = Variant::OBJECT; - type.script_type = ResourceLoader::load(path, "Script"); - type.native_type = type.script_type->get_instance_base_type(); - type.script_path = path; - type.is_constant = true; - type.is_meta_type = true; - return type; + return make_script_meta_type(ResourceLoader::load(path, "Script")); } } +void GDScriptAnalyzer::reduce_identifier_from_base_set_class(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType p_identifier_datatype) { + ERR_FAIL_NULL(p_identifier); + + p_identifier->set_datatype(p_identifier_datatype); + Error err = OK; + GDScript *scr = GDScriptCache::get_full_script(p_identifier_datatype.script_path, err).ptr(); + ERR_FAIL_COND_MSG(err != OK, "Error while getting full script."); + scr = scr->find_class(p_identifier_datatype.class_type->fqcn); + p_identifier->reduced_value = scr; + p_identifier->is_constant = true; +} + void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base) { if (!p_identifier->get_datatype().has_no_type()) { return; @@ -3041,26 +2934,14 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod if (base.kind == GDScriptParser::DataType::ENUM) { if (base.is_meta_type) { if (base.enum_values.has(name)) { + p_identifier->set_datatype(type_from_metatype(base)); p_identifier->is_constant = true; p_identifier->reduced_value = base.enum_values[name]; - - GDScriptParser::DataType result; - result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; - result.kind = GDScriptParser::DataType::ENUM; - result.is_constant = true; - result.builtin_type = Variant::INT; - result.native_type = base.native_type; - result.enum_type = base.enum_type; - result.enum_values = base.enum_values; - p_identifier->set_datatype(result); return; - } else { - // Consider as a Dictionary, so it can be anything. - // This will be evaluated in the next if block. - base.kind = GDScriptParser::DataType::BUILTIN; - base.builtin_type = Variant::DICTIONARY; - base.is_meta_type = false; } + + // Enum does not have this value, return. + return; } else { push_error(R"(Cannot get property from enum value.)", p_identifier); return; @@ -3114,102 +2995,91 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod } GDScriptParser::ClassNode *base_class = base.class_type; + List<GDScriptParser::ClassNode *> script_classes; + bool is_base = true; - // TODO: Switch current class/function/suite here to avoid misrepresenting identifiers (in recursive reduce calls). - while (base_class != nullptr) { - if (base_class->identifier && base_class->identifier->name == name) { - p_identifier->set_datatype(base_class->get_datatype()); + if (base_class != nullptr) { + get_class_node_current_scope_classes(base_class, &script_classes); + } + + for (GDScriptParser::ClassNode *script_class : script_classes) { + if (p_base == nullptr && script_class->identifier && script_class->identifier->name == name) { + reduce_identifier_from_base_set_class(p_identifier, script_class->get_datatype()); return; } - if (base_class->has_member(name)) { - resolve_class_member(base_class, name, p_identifier); + if (script_class->has_member(name)) { + resolve_class_member(script_class, name, p_identifier); - GDScriptParser::ClassNode::Member member = base_class->get_member(name); - p_identifier->set_datatype(member.get_datatype()); + GDScriptParser::ClassNode::Member member = script_class->get_member(name); switch (member.type) { - case GDScriptParser::ClassNode::Member::CONSTANT: + case GDScriptParser::ClassNode::Member::CONSTANT: { + p_identifier->set_datatype(member.get_datatype()); p_identifier->is_constant = true; p_identifier->reduced_value = member.constant->initializer->reduced_value; p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT; p_identifier->constant_source = member.constant; - break; - case GDScriptParser::ClassNode::Member::ENUM_VALUE: + return; + } + + case GDScriptParser::ClassNode::Member::ENUM_VALUE: { + p_identifier->set_datatype(member.get_datatype()); p_identifier->is_constant = true; p_identifier->reduced_value = member.enum_value.value; p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT; - break; - case GDScriptParser::ClassNode::Member::ENUM: - if (p_base != nullptr && p_base->is_constant) { - p_identifier->is_constant = true; - p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT; + return; + } + + case GDScriptParser::ClassNode::Member::ENUM: { + p_identifier->set_datatype(member.get_datatype()); + p_identifier->is_constant = true; + p_identifier->reduced_value = member.m_enum->dictionary; + p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT; + return; + } + + case GDScriptParser::ClassNode::Member::VARIABLE: { + if (is_base && !base.is_meta_type) { + p_identifier->set_datatype(member.get_datatype()); + p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_VARIABLE; + p_identifier->variable_source = member.variable; + member.variable->usages += 1; + return; } - break; - case GDScriptParser::ClassNode::Member::VARIABLE: - p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_VARIABLE; - p_identifier->variable_source = member.variable; - member.variable->usages += 1; - break; - case GDScriptParser::ClassNode::Member::SIGNAL: - p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_SIGNAL; - break; - case GDScriptParser::ClassNode::Member::FUNCTION: - p_identifier->set_datatype(make_callable_type(member.function->info)); - break; - case GDScriptParser::ClassNode::Member::CLASS: - if (p_base != nullptr && p_base->is_constant) { - p_identifier->is_constant = true; - p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT; - - Error err = OK; - GDScript *scr = GDScriptCache::get_full_script(base.script_path, err).ptr(); - ERR_FAIL_COND_MSG(err != OK, "Error while getting subscript full script."); - scr = scr->find_class(p_identifier->get_datatype().class_type->fqcn); - p_identifier->reduced_value = scr; + } break; + + case GDScriptParser::ClassNode::Member::SIGNAL: { + if (is_base && !base.is_meta_type) { + p_identifier->set_datatype(member.get_datatype()); + p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_SIGNAL; + return; } - break; - default: - break; // Type already set. - } - return; - } - // Check outer constants. - // TODO: Allow outer static functions. - if (base_class->outer != nullptr) { - List<GDScriptParser::ClassNode *> script_classes; - get_class_node_current_scope_classes(base_class->outer, &script_classes); - for (GDScriptParser::ClassNode *script_class : script_classes) { - if (script_class->has_member(name)) { - resolve_class_member(script_class, name, p_identifier); - - GDScriptParser::ClassNode::Member member = script_class->get_member(name); - switch (member.type) { - case GDScriptParser::ClassNode::Member::CONSTANT: - // TODO: Make sure loops won't cause problem. And make special error message for those. - p_identifier->set_datatype(member.get_datatype()); - p_identifier->is_constant = true; - p_identifier->reduced_value = member.constant->initializer->reduced_value; - return; - case GDScriptParser::ClassNode::Member::ENUM_VALUE: - p_identifier->set_datatype(member.get_datatype()); - p_identifier->is_constant = true; - p_identifier->reduced_value = member.enum_value.value; - return; - case GDScriptParser::ClassNode::Member::ENUM: - p_identifier->set_datatype(member.get_datatype()); - p_identifier->is_constant = false; - return; - case GDScriptParser::ClassNode::Member::CLASS: - p_identifier->set_datatype(member.get_datatype()); - return; - default: - break; + } break; + + case GDScriptParser::ClassNode::Member::FUNCTION: { + if (is_base && !base.is_meta_type) { + p_identifier->set_datatype(make_callable_type(member.function->info)); + return; } + } break; + + case GDScriptParser::ClassNode::Member::CLASS: { + reduce_identifier_from_base_set_class(p_identifier, member.get_datatype()); + return; + } + + default: { + // Do nothing } } } - base_class = base_class->base_type.class_type; + if (is_base) { + is_base = script_class->base_type.class_type != nullptr; + if (!is_base && p_base != nullptr) { + break; + } + } } // Check native members. No need for native class recursion because Node exposes all Object's properties. @@ -3239,35 +3109,39 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod return; } if (ClassDB::has_enum(native, name)) { - p_identifier->set_datatype(make_native_enum_type(native, name)); + p_identifier->set_datatype(make_native_enum_type(name, native)); p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT; return; } bool valid = false; + int64_t int_constant = ClassDB::get_integer_constant(native, name, &valid); if (valid) { p_identifier->is_constant = true; p_identifier->reduced_value = int_constant; - p_identifier->set_datatype(type_from_variant(int_constant, p_identifier)); p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT; - return; + + // Check whether this constant, which exists, belongs to an enum + StringName enum_name = ClassDB::get_integer_constant_enum(native, name); + if (enum_name != StringName()) { + p_identifier->set_datatype(make_native_enum_type(enum_name, native, false)); + } else { + p_identifier->set_datatype(type_from_variant(int_constant, p_identifier)); + } } } } void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin) { - // TODO: This is opportunity to further infer types. + // TODO: This is an opportunity to further infer types. - // Check if we are inside and enum. This allows enum values to access other elements of the same enum. + // Check if we are inside an enum. This allows enum values to access other elements of the same enum. if (current_enum) { for (int i = 0; i < current_enum->values.size(); i++) { const GDScriptParser::EnumNode::Value &element = current_enum->values[i]; if (element.identifier->name == p_identifier->name) { - GDScriptParser::DataType type; - type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; - type.kind = element.parent_enum->identifier ? GDScriptParser::DataType::ENUM : GDScriptParser::DataType::BUILTIN; - type.builtin_type = Variant::INT; - type.is_constant = true; + StringName enum_name = current_enum->identifier->name ? current_enum->identifier->name : UNNAMED_ENUM; + GDScriptParser::DataType type = make_enum_type(enum_name, parser->current_class->fqcn, false); if (element.parent_enum->identifier) { type.enum_type = element.parent_enum->identifier->name; } @@ -3336,18 +3210,20 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident } if (found_source) { - if ((p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_VARIABLE || p_identifier->source == GDScriptParser::IdentifierNode::INHERITED_VARIABLE) && parser->current_function && parser->current_function->is_static) { + bool source_is_variable = p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_VARIABLE || p_identifier->source == GDScriptParser::IdentifierNode::INHERITED_VARIABLE; + bool source_is_signal = p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_SIGNAL; + if ((source_is_variable || source_is_signal) && parser->current_function && parser->current_function->is_static) { // Get the parent function above any lambda. GDScriptParser::FunctionNode *parent_function = parser->current_function; while (parent_function->source_lambda) { parent_function = parent_function->source_lambda->parent_function; } - push_error(vformat(R"*(Cannot access instance variable "%s" from the static function "%s()".)*", p_identifier->name, parent_function->identifier->name), p_identifier); + push_error(vformat(R"*(Cannot access %s "%s" from the static function "%s()".)*", source_is_signal ? "signal" : "instance variable", p_identifier->name, parent_function->identifier->name), p_identifier); } if (!lambda_stack.is_empty()) { - // If the identifier is a member variable (including the native class properties), we consider the lambda to be using `self`, so we keep a reference to the current instance. - if (p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_VARIABLE || p_identifier->source == GDScriptParser::IdentifierNode::INHERITED_VARIABLE) { + // If the identifier is a member variable (including the native class properties) or a signal, we consider the lambda to be using `self`, so we keep a reference to the current instance. + if (source_is_variable || source_is_signal) { mark_lambda_use_self(); return; // No need to capture. } @@ -3468,16 +3344,10 @@ void GDScriptAnalyzer::reduce_lambda(GDScriptParser::LambdaNode *p_lambda) { return; } - GDScriptParser::FunctionNode *previous_function = parser->current_function; - parser->current_function = p_lambda->function; - lambda_stack.push_back(p_lambda); - - for (int i = 0; i < p_lambda->function->parameters.size(); i++) { - resolve_parameter(p_lambda->function->parameters[i]); - } - - resolve_suite(p_lambda->function->body); + resolve_function_signature(p_lambda->function, p_lambda, true); + resolve_function_body(p_lambda->function, true); + lambda_stack.pop_back(); int captures_amount = p_lambda->captures.size(); if (captures_amount > 0) { @@ -3502,9 +3372,6 @@ void GDScriptAnalyzer::reduce_lambda(GDScriptParser::LambdaNode *p_lambda) { p_lambda->function->parameters_indices[capture->name] = i; } } - - lambda_stack.pop_back(); - parser->current_function = previous_function; } void GDScriptAnalyzer::reduce_literal(GDScriptParser::LiteralNode *p_literal) { @@ -3592,9 +3459,9 @@ void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscri reduce_expression(p_subscript->base); if (p_subscript->base->type == GDScriptParser::Node::ARRAY) { - const_fold_array(static_cast<GDScriptParser::ArrayNode *>(p_subscript->base)); + const_fold_array(static_cast<GDScriptParser::ArrayNode *>(p_subscript->base), false); } else if (p_subscript->base->type == GDScriptParser::Node::DICTIONARY) { - const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_subscript->base)); + const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_subscript->base), false); } } @@ -3654,12 +3521,12 @@ void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscri Variant value = p_subscript->base->reduced_value.get(p_subscript->index->reduced_value, &valid); if (!valid) { push_error(vformat(R"(Cannot get index "%s" from "%s".)", p_subscript->index->reduced_value, p_subscript->base->reduced_value), p_subscript->index); + result_type.kind = GDScriptParser::DataType::VARIANT; } else { p_subscript->is_constant = true; p_subscript->reduced_value = value; result_type = type_from_variant(value, p_subscript); } - result_type.kind = GDScriptParser::DataType::VARIANT; } else { GDScriptParser::DataType base_type = p_subscript->base->get_datatype(); GDScriptParser::DataType index_type = p_subscript->index->get_datatype(); @@ -3919,20 +3786,17 @@ void GDScriptAnalyzer::reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op) p_unary_op->set_datatype(result); } -void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array) { - bool all_is_constant = true; - +void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array, bool p_is_const) { for (int i = 0; i < p_array->elements.size(); i++) { GDScriptParser::ExpressionNode *element = p_array->elements[i]; if (element->type == GDScriptParser::Node::ARRAY) { - const_fold_array(static_cast<GDScriptParser::ArrayNode *>(element)); + const_fold_array(static_cast<GDScriptParser::ArrayNode *>(element), p_is_const); } else if (element->type == GDScriptParser::Node::DICTIONARY) { - const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element)); + const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element), p_is_const); } - all_is_constant = all_is_constant && element->is_constant; - if (!all_is_constant) { + if (!element->is_constant) { return; } } @@ -3942,24 +3806,24 @@ void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array) { for (int i = 0; i < p_array->elements.size(); i++) { array[i] = p_array->elements[i]->reduced_value; } + if (p_is_const) { + array.set_read_only(true); + } p_array->is_constant = true; p_array->reduced_value = array; } -void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_dictionary) { - bool all_is_constant = true; - +void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_dictionary, bool p_is_const) { for (int i = 0; i < p_dictionary->elements.size(); i++) { const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i]; if (element.value->type == GDScriptParser::Node::ARRAY) { - const_fold_array(static_cast<GDScriptParser::ArrayNode *>(element.value)); + const_fold_array(static_cast<GDScriptParser::ArrayNode *>(element.value), p_is_const); } else if (element.value->type == GDScriptParser::Node::DICTIONARY) { - const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element.value)); + const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element.value), p_is_const); } - all_is_constant = all_is_constant && element.key->is_constant && element.value->is_constant; - if (!all_is_constant) { + if (!element.key->is_constant || !element.value->is_constant) { return; } } @@ -3969,6 +3833,9 @@ void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_d const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i]; dict[element.key->reduced_value] = element.value->reduced_value; } + if (p_is_const) { + dict.set_read_only(true); + } p_dictionary->is_constant = true; p_dictionary->reduced_value = dict; } @@ -3998,10 +3865,8 @@ GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_va scr = obj->get_script(); } if (scr.is_valid()) { - result.script_path = scr->get_path(); Ref<GDScript> gds = scr; if (gds.is_valid()) { - result.kind = GDScriptParser::DataType::CLASS; // 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. String script_path = gds->get_script_path(); @@ -4027,11 +3892,14 @@ GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_va return error_type; } + result.kind = GDScriptParser::DataType::CLASS; + result.native_type = found->get_datatype().native_type; result.class_type = found; result.script_path = ref->get_parser()->script_path; } else { result.kind = GDScriptParser::DataType::SCRIPT; result.native_type = scr->get_instance_base_type(); + result.script_path = scr->get_path(); } result.script_type = scr; } else { @@ -4045,20 +3913,21 @@ GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_va return result; } -GDScriptParser::DataType GDScriptAnalyzer::type_from_metatype(const GDScriptParser::DataType &p_meta_type) const { +GDScriptParser::DataType GDScriptAnalyzer::type_from_metatype(const GDScriptParser::DataType &p_meta_type) { GDScriptParser::DataType result = p_meta_type; result.is_meta_type = false; - result.is_constant = false; if (p_meta_type.kind == GDScriptParser::DataType::ENUM) { result.builtin_type = Variant::INT; + } else { + result.is_constant = false; } return result; } -GDScriptParser::DataType GDScriptAnalyzer::type_from_property(const PropertyInfo &p_property) const { +GDScriptParser::DataType GDScriptAnalyzer::type_from_property(const PropertyInfo &p_property, bool p_is_arg) const { GDScriptParser::DataType result; result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; - if (p_property.type == Variant::NIL && (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) { + if (p_property.type == Variant::NIL && (p_is_arg || (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) { // Variant result.kind = GDScriptParser::DataType::VARIANT; return result; @@ -4108,11 +3977,12 @@ bool GDScriptAnalyzer::get_function_signature(GDScriptParser::Node *p_source, bo r_default_arg_count = 0; StringName function_name = p_function; + bool was_enum = false; if (p_base_type.kind == GDScriptParser::DataType::ENUM) { + was_enum = true; if (p_base_type.is_meta_type) { // Enum type can be treated as a dictionary value. p_base_type.kind = GDScriptParser::DataType::BUILTIN; - p_base_type.builtin_type = Variant::DICTIONARY; p_base_type.is_meta_type = false; } else { push_error("Cannot call function on enum value.", p_source); @@ -4135,6 +4005,10 @@ bool GDScriptAnalyzer::get_function_signature(GDScriptParser::Node *p_source, bo if (E.name == p_function) { function_signature_from_info(E, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg); r_static = Variant::is_builtin_method_static(p_base_type.builtin_type, function_name); + // Cannot use non-const methods on enums. + if (!r_static && was_enum && !(E.flags & METHOD_FLAG_CONST)) { + push_error(vformat(R"*(Cannot call non-const Dictionary function "%s()" on enum "%s".)*", p_function, p_base_type.enum_type), p_source); + } return true; } } @@ -4142,6 +4016,25 @@ bool GDScriptAnalyzer::get_function_signature(GDScriptParser::Node *p_source, bo return false; } + StringName base_native = p_base_type.native_type; + if (base_native != StringName()) { + // Empty native class might happen in some Script implementations. + // Just ignore it. + if (!class_exists(base_native)) { + push_error(vformat("Native class %s used in script doesn't exist or isn't exposed.", base_native), p_source); + return false; + } else if (p_is_constructor && !ClassDB::can_instantiate(base_native)) { + if (p_base_type.kind == GDScriptParser::DataType::CLASS) { + push_error(vformat(R"(Class "%s" cannot be constructed as it is based on abstract native class "%s".)", p_base_type.class_type->fqcn.get_file(), base_native), p_source); + } else if (p_base_type.kind == GDScriptParser::DataType::SCRIPT) { + push_error(vformat(R"(Script "%s" cannot be constructed as it is based on abstract native class "%s".)", p_base_type.script_path.get_file(), base_native), p_source); + } else { + push_error(vformat(R"(Native class "%s" cannot be constructed as it is abstract.)", base_native), p_source); + } + return false; + } + } + if (p_is_constructor) { function_name = "_init"; r_static = true; @@ -4170,11 +4063,11 @@ bool GDScriptAnalyzer::get_function_signature(GDScriptParser::Node *p_source, bo r_static = p_is_constructor || found_function->is_static; for (int i = 0; i < found_function->parameters.size(); i++) { r_par_types.push_back(found_function->parameters[i]->get_datatype()); - if (found_function->parameters[i]->default_value != nullptr) { + if (found_function->parameters[i]->initializer != nullptr) { r_default_arg_count++; } } - r_return_type = found_function->get_datatype(); + r_return_type = p_is_constructor ? p_base_type : found_function->get_datatype(); r_return_type.is_meta_type = false; r_return_type.is_coroutine = found_function->is_coroutine; @@ -4202,17 +4095,6 @@ bool GDScriptAnalyzer::get_function_signature(GDScriptParser::Node *p_source, bo } } - StringName base_native = p_base_type.native_type; -#ifdef DEBUG_ENABLED - if (base_native != StringName()) { - // Empty native class might happen in some Script implementations. - // Just ignore it. - if (!class_exists(base_native)) { - ERR_FAIL_V_MSG(false, vformat("Native class %s used in script doesn't exist or isn't exposed.", base_native)); - } - } -#endif - if (p_is_constructor) { // Native types always have a default constructor. r_return_type = p_base_type; @@ -4240,7 +4122,7 @@ bool GDScriptAnalyzer::function_signature_from_info(const MethodInfo &p_info, GD r_static = (p_info.flags & METHOD_FLAG_STATIC) != 0; for (const PropertyInfo &E : p_info.arguments) { - r_par_types.push_back(type_from_property(E)); + r_par_types.push_back(type_from_property(E, true)); } return true; } @@ -4249,7 +4131,7 @@ bool GDScriptAnalyzer::validate_call_arg(const MethodInfo &p_method, const GDScr List<GDScriptParser::DataType> arg_types; for (const PropertyInfo &E : p_method.arguments) { - arg_types.push_back(type_from_property(E)); + arg_types.push_back(type_from_property(E, true)); } return validate_call_arg(arg_types, p_method.default_arguments.size(), (p_method.flags & METHOD_FLAG_VARARG) != 0, p_call); @@ -4282,7 +4164,7 @@ bool GDScriptAnalyzer::validate_call_arg(const List<GDScriptParser::DataType> &p // Supertypes are acceptable for dynamic compliance, but it's unsafe. mark_node_unsafe(p_call); if (!is_type_compatible(arg_type, par_type)) { - push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", + push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be "%s" but is "%s".)*", p_call->function_name, i + 1, par_type.to_string(), arg_type.to_string()), p_call->arguments[i]); valid = false; @@ -4367,9 +4249,6 @@ GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator } GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, const GDScriptParser::DataType &p_b, bool &r_valid, const GDScriptParser::Node *p_source) { - GDScriptParser::DataType result; - result.kind = GDScriptParser::DataType::VARIANT; - Variant::Type a_type = p_a.builtin_type; Variant::Type b_type = p_b.builtin_type; @@ -4389,28 +4268,18 @@ GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator } Variant::ValidatedOperatorEvaluator op_eval = Variant::get_validated_operator_evaluator(p_operation, a_type, b_type); - bool hard_operation = p_a.is_hard_type() && p_b.is_hard_type(); bool validated = op_eval != nullptr; - if (hard_operation && !validated) { - r_valid = false; - return result; - } else if (hard_operation && validated) { + GDScriptParser::DataType result; + if (validated) { r_valid = true; - result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED; + result.type_source = hard_operation ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED; result.kind = GDScriptParser::DataType::BUILTIN; result.builtin_type = Variant::get_operator_return_type(p_operation, a_type, b_type); - } else if (!hard_operation && !validated) { - r_valid = true; - result.type_source = GDScriptParser::DataType::UNDETECTED; + } else { + r_valid = !hard_operation; result.kind = GDScriptParser::DataType::VARIANT; - result.builtin_type = Variant::NIL; - } else if (!hard_operation && validated) { - r_valid = true; - result.type_source = GDScriptParser::DataType::INFERRED; - result.kind = GDScriptParser::DataType::BUILTIN; - result.builtin_type = Variant::get_operator_return_type(p_operation, a_type, b_type); } return result; @@ -4586,6 +4455,46 @@ void GDScriptAnalyzer::mark_node_unsafe(const GDScriptParser::Node *p_node) { #endif } +void GDScriptAnalyzer::downgrade_node_type_source(GDScriptParser::Node *p_node) { + GDScriptParser::IdentifierNode *identifier = nullptr; + if (p_node->type == GDScriptParser::Node::IDENTIFIER) { + identifier = static_cast<GDScriptParser::IdentifierNode *>(p_node); + } else if (p_node->type == GDScriptParser::Node::SUBSCRIPT) { + GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(p_node); + if (subscript->is_attribute) { + identifier = subscript->attribute; + } + } + if (identifier == nullptr) { + return; + } + + GDScriptParser::Node *source = nullptr; + switch (identifier->source) { + case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: { + source = identifier->variable_source; + } break; + case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER: { + source = identifier->parameter_source; + } break; + case GDScriptParser::IdentifierNode::LOCAL_VARIABLE: { + source = identifier->variable_source; + } break; + case GDScriptParser::IdentifierNode::LOCAL_ITERATOR: { + source = identifier->bind_source; + } break; + default: + break; + } + if (source == nullptr) { + return; + } + + GDScriptParser::DataType datatype; + datatype.kind = GDScriptParser::DataType::VARIANT; + source->set_datatype(datatype); +} + void GDScriptAnalyzer::mark_lambda_use_self() { for (GDScriptParser::LambdaNode *lambda : lambda_stack) { lambda->use_self = true; |