| 1 | /**************************************************************************/ |
| 2 | /* gdscript_compiler.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "gdscript_compiler.h" |
| 32 | |
| 33 | #include "gdscript.h" |
| 34 | #include "gdscript_analyzer.h" |
| 35 | #include "gdscript_byte_codegen.h" |
| 36 | #include "gdscript_cache.h" |
| 37 | #include "gdscript_utility_functions.h" |
| 38 | |
| 39 | #include "core/config/engine.h" |
| 40 | #include "core/config/project_settings.h" |
| 41 | |
| 42 | #include "scene/scene_string_names.h" |
| 43 | |
| 44 | bool GDScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringName &p_name) { |
| 45 | if (codegen.function_node && codegen.function_node->is_static) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | if (_is_local_or_parameter(codegen, p_name)) { |
| 50 | return false; //shadowed |
| 51 | } |
| 52 | |
| 53 | return _is_class_member_property(codegen.script, p_name); |
| 54 | } |
| 55 | |
| 56 | bool GDScriptCompiler::_is_class_member_property(GDScript *owner, const StringName &p_name) { |
| 57 | GDScript *scr = owner; |
| 58 | GDScriptNativeClass *nc = nullptr; |
| 59 | while (scr) { |
| 60 | if (scr->native.is_valid()) { |
| 61 | nc = scr->native.ptr(); |
| 62 | } |
| 63 | scr = scr->base.ptr(); |
| 64 | } |
| 65 | |
| 66 | ERR_FAIL_NULL_V(nc, false); |
| 67 | |
| 68 | return ClassDB::has_property(nc->get_name(), p_name); |
| 69 | } |
| 70 | |
| 71 | bool GDScriptCompiler::_is_local_or_parameter(CodeGen &codegen, const StringName &p_name) { |
| 72 | return codegen.parameters.has(p_name) || codegen.locals.has(p_name); |
| 73 | } |
| 74 | |
| 75 | void GDScriptCompiler::_set_error(const String &p_error, const GDScriptParser::Node *p_node) { |
| 76 | if (!error.is_empty()) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | error = p_error; |
| 81 | if (p_node) { |
| 82 | err_line = p_node->start_line; |
| 83 | err_column = p_node->start_column; |
| 84 | } else { |
| 85 | err_line = 0; |
| 86 | err_column = 0; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::DataType &p_datatype, GDScript *p_owner, bool p_handle_metatype) { |
| 91 | if (!p_datatype.is_set() || !p_datatype.is_hard_type() || p_datatype.is_coroutine) { |
| 92 | return GDScriptDataType(); |
| 93 | } |
| 94 | |
| 95 | GDScriptDataType result; |
| 96 | |
| 97 | switch (p_datatype.kind) { |
| 98 | case GDScriptParser::DataType::VARIANT: { |
| 99 | result.kind = GDScriptDataType::VARIANT; |
| 100 | } break; |
| 101 | case GDScriptParser::DataType::BUILTIN: { |
| 102 | result.kind = GDScriptDataType::BUILTIN; |
| 103 | result.builtin_type = p_datatype.builtin_type; |
| 104 | } break; |
| 105 | case GDScriptParser::DataType::NATIVE: { |
| 106 | if (p_handle_metatype && p_datatype.is_meta_type) { |
| 107 | result.kind = GDScriptDataType::NATIVE; |
| 108 | result.builtin_type = Variant::OBJECT; |
| 109 | // Fixes GH-82255. `GDScriptNativeClass` is obtainable in GDScript, |
| 110 | // but is not a registered and exposed class, so `GDScriptNativeClass` |
| 111 | // is missing from `GDScriptLanguage::get_singleton()->get_global_map()`. |
| 112 | //result.native_type = GDScriptNativeClass::get_class_static(); |
| 113 | result.native_type = Object::get_class_static(); |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | result.kind = GDScriptDataType::NATIVE; |
| 118 | result.builtin_type = p_datatype.builtin_type; |
| 119 | result.native_type = p_datatype.native_type; |
| 120 | |
| 121 | #ifdef DEBUG_ENABLED |
| 122 | if (unlikely(!GDScriptLanguage::get_singleton()->get_global_map().has(result.native_type))) { |
| 123 | _set_error(vformat(R"(GDScript bug (please report): Native class "%s" not found.)", result.native_type), nullptr); |
| 124 | return GDScriptDataType(); |
| 125 | } |
| 126 | #endif |
| 127 | } break; |
| 128 | case GDScriptParser::DataType::SCRIPT: { |
| 129 | if (p_handle_metatype && p_datatype.is_meta_type) { |
| 130 | result.kind = GDScriptDataType::NATIVE; |
| 131 | result.builtin_type = Variant::OBJECT; |
| 132 | result.native_type = p_datatype.script_type.is_valid() ? p_datatype.script_type->get_class_name() : Script::get_class_static(); |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | result.kind = GDScriptDataType::SCRIPT; |
| 137 | result.builtin_type = p_datatype.builtin_type; |
| 138 | result.script_type_ref = p_datatype.script_type; |
| 139 | result.script_type = result.script_type_ref.ptr(); |
| 140 | result.native_type = p_datatype.native_type; |
| 141 | } break; |
| 142 | case GDScriptParser::DataType::CLASS: { |
| 143 | if (p_handle_metatype && p_datatype.is_meta_type) { |
| 144 | result.kind = GDScriptDataType::NATIVE; |
| 145 | result.builtin_type = Variant::OBJECT; |
| 146 | result.native_type = GDScript::get_class_static(); |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | result.kind = GDScriptDataType::GDSCRIPT; |
| 151 | result.builtin_type = p_datatype.builtin_type; |
| 152 | result.native_type = p_datatype.native_type; |
| 153 | |
| 154 | bool is_local_class = parser->has_class(p_datatype.class_type); |
| 155 | |
| 156 | Ref<GDScript> script; |
| 157 | if (is_local_class) { |
| 158 | script = Ref<GDScript>(main_script); |
| 159 | } else { |
| 160 | Error err = OK; |
| 161 | script = GDScriptCache::get_shallow_script(p_datatype.script_path, err, p_owner->path); |
| 162 | if (err) { |
| 163 | _set_error(vformat(R"(Could not find script "%s": %s)", p_datatype.script_path, error_names[err]), nullptr); |
| 164 | return GDScriptDataType(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (script.is_valid()) { |
| 169 | script = Ref<GDScript>(script->find_class(p_datatype.class_type->fqcn)); |
| 170 | } |
| 171 | |
| 172 | if (script.is_null()) { |
| 173 | _set_error(vformat(R"(Could not find class "%s" in "%s".)", p_datatype.class_type->fqcn, p_datatype.script_path), nullptr); |
| 174 | return GDScriptDataType(); |
| 175 | } else { |
| 176 | // Only hold a strong reference if the owner of the element qualified with this type is not local, to avoid cyclic references (leaks). |
| 177 | // TODO: Might lead to use after free if script_type is a subclass and is used after its parent is freed. |
| 178 | if (!is_local_class) { |
| 179 | result.script_type_ref = script; |
| 180 | } |
| 181 | result.script_type = script.ptr(); |
| 182 | result.native_type = p_datatype.native_type; |
| 183 | } |
| 184 | } break; |
| 185 | case GDScriptParser::DataType::ENUM: |
| 186 | if (p_handle_metatype && p_datatype.is_meta_type) { |
| 187 | result.kind = GDScriptDataType::BUILTIN; |
| 188 | result.builtin_type = Variant::DICTIONARY; |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | result.kind = GDScriptDataType::BUILTIN; |
| 193 | result.builtin_type = p_datatype.builtin_type; |
| 194 | break; |
| 195 | case GDScriptParser::DataType::RESOLVING: |
| 196 | case GDScriptParser::DataType::UNRESOLVED: { |
| 197 | _set_error("Parser bug (please report): converting unresolved type.", nullptr); |
| 198 | return GDScriptDataType(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | for (int i = 0; i < p_datatype.container_element_types.size(); i++) { |
| 203 | result.set_container_element_type(i, _gdtype_from_datatype(p_datatype.get_container_element_type_or_variant(i), p_owner, false)); |
| 204 | } |
| 205 | |
| 206 | return result; |
| 207 | } |
| 208 | |
| 209 | static bool _is_exact_type(const PropertyInfo &p_par_type, const GDScriptDataType &p_arg_type) { |
| 210 | if (!p_arg_type.has_type()) { |
| 211 | return false; |
| 212 | } |
| 213 | if (p_par_type.type == Variant::NIL) { |
| 214 | return false; |
| 215 | } |
| 216 | if (p_par_type.type == Variant::OBJECT) { |
| 217 | if (p_arg_type.kind == GDScriptDataType::BUILTIN) { |
| 218 | return false; |
| 219 | } |
| 220 | StringName class_name; |
| 221 | if (p_arg_type.kind == GDScriptDataType::NATIVE) { |
| 222 | class_name = p_arg_type.native_type; |
| 223 | } else { |
| 224 | class_name = p_arg_type.native_type == StringName() ? p_arg_type.script_type->get_instance_base_type() : p_arg_type.native_type; |
| 225 | } |
| 226 | return p_par_type.class_name == class_name || ClassDB::is_parent_class(class_name, p_par_type.class_name); |
| 227 | } else { |
| 228 | if (p_arg_type.kind != GDScriptDataType::BUILTIN) { |
| 229 | return false; |
| 230 | } |
| 231 | return p_par_type.type == p_arg_type.builtin_type; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static bool _can_use_validate_call(const MethodBind *p_method, const Vector<GDScriptCodeGenerator::Address> &p_arguments) { |
| 236 | if (p_method->is_vararg()) { |
| 237 | // Validated call won't work with vararg methods. |
| 238 | return false; |
| 239 | } |
| 240 | if (p_method->get_argument_count() != p_arguments.size()) { |
| 241 | // Validated call won't work with default arguments. |
| 242 | return false; |
| 243 | } |
| 244 | MethodInfo info; |
| 245 | ClassDB::get_method_info(p_method->get_instance_class(), p_method->get_name(), &info); |
| 246 | for (int64_t i = 0; i < info.arguments.size(); ++i) { |
| 247 | if (!_is_exact_type(info.arguments[i], p_arguments[i].type)) { |
| 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::ExpressionNode *p_expression, bool p_root, bool p_initializer) { |
| 255 | if (p_expression->is_constant && !(p_expression->get_datatype().is_meta_type && p_expression->get_datatype().kind == GDScriptParser::DataType::CLASS)) { |
| 256 | return codegen.add_constant(p_expression->reduced_value); |
| 257 | } |
| 258 | |
| 259 | GDScriptCodeGenerator *gen = codegen.generator; |
| 260 | |
| 261 | switch (p_expression->type) { |
| 262 | case GDScriptParser::Node::IDENTIFIER: { |
| 263 | // Look for identifiers in current scope. |
| 264 | const GDScriptParser::IdentifierNode *in = static_cast<const GDScriptParser::IdentifierNode *>(p_expression); |
| 265 | |
| 266 | StringName identifier = in->name; |
| 267 | |
| 268 | switch (in->source) { |
| 269 | // LOCALS. |
| 270 | case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER: |
| 271 | case GDScriptParser::IdentifierNode::LOCAL_VARIABLE: |
| 272 | case GDScriptParser::IdentifierNode::LOCAL_CONSTANT: |
| 273 | case GDScriptParser::IdentifierNode::LOCAL_ITERATOR: |
| 274 | case GDScriptParser::IdentifierNode::LOCAL_BIND: { |
| 275 | // Try function parameters. |
| 276 | if (codegen.parameters.has(identifier)) { |
| 277 | return codegen.parameters[identifier]; |
| 278 | } |
| 279 | |
| 280 | // Try local variables and constants. |
| 281 | if (!p_initializer && codegen.locals.has(identifier)) { |
| 282 | return codegen.locals[identifier]; |
| 283 | } |
| 284 | } break; |
| 285 | |
| 286 | // MEMBERS. |
| 287 | case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: |
| 288 | case GDScriptParser::IdentifierNode::MEMBER_FUNCTION: |
| 289 | case GDScriptParser::IdentifierNode::MEMBER_SIGNAL: |
| 290 | case GDScriptParser::IdentifierNode::INHERITED_VARIABLE: { |
| 291 | // Try class members. |
| 292 | if (_is_class_member_property(codegen, identifier)) { |
| 293 | // Get property. |
| 294 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(_gdtype_from_datatype(p_expression->get_datatype(), codegen.script)); |
| 295 | gen->write_get_member(temp, identifier); |
| 296 | return temp; |
| 297 | } |
| 298 | |
| 299 | // Try members. |
| 300 | if (!codegen.function_node || !codegen.function_node->is_static) { |
| 301 | // Try member variables. |
| 302 | if (codegen.script->member_indices.has(identifier)) { |
| 303 | if (codegen.script->member_indices[identifier].getter != StringName() && codegen.script->member_indices[identifier].getter != codegen.function_name) { |
| 304 | // Perform getter. |
| 305 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(codegen.script->member_indices[identifier].data_type); |
| 306 | Vector<GDScriptCodeGenerator::Address> args; // No argument needed. |
| 307 | gen->write_call_self(temp, codegen.script->member_indices[identifier].getter, args); |
| 308 | return temp; |
| 309 | } else { |
| 310 | // No getter or inside getter: direct member access. |
| 311 | int idx = codegen.script->member_indices[identifier].index; |
| 312 | return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::MEMBER, idx, codegen.script->get_member_type(identifier)); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // Try methods and signals (can be Callable and Signal). |
| 318 | { |
| 319 | // Search upwards through parent classes: |
| 320 | const GDScriptParser::ClassNode *base_class = codegen.class_node; |
| 321 | while (base_class != nullptr) { |
| 322 | if (base_class->has_member(identifier)) { |
| 323 | const GDScriptParser::ClassNode::Member &member = base_class->get_member(identifier); |
| 324 | if (member.type == GDScriptParser::ClassNode::Member::FUNCTION || member.type == GDScriptParser::ClassNode::Member::SIGNAL) { |
| 325 | // Get like it was a property. |
| 326 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(); // TODO: Get type here. |
| 327 | |
| 328 | GDScriptCodeGenerator::Address base(GDScriptCodeGenerator::Address::SELF); |
| 329 | if (member.type == GDScriptParser::ClassNode::Member::FUNCTION && member.function->is_static) { |
| 330 | base = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CLASS); |
| 331 | } |
| 332 | |
| 333 | gen->write_get_named(temp, identifier, base); |
| 334 | return temp; |
| 335 | } |
| 336 | } |
| 337 | base_class = base_class->base_type.class_type; |
| 338 | } |
| 339 | |
| 340 | // Try in native base. |
| 341 | GDScript *scr = codegen.script; |
| 342 | GDScriptNativeClass *nc = nullptr; |
| 343 | while (scr) { |
| 344 | if (scr->native.is_valid()) { |
| 345 | nc = scr->native.ptr(); |
| 346 | } |
| 347 | scr = scr->base.ptr(); |
| 348 | } |
| 349 | |
| 350 | if (nc && (identifier == CoreStringName(free_) || ClassDB::has_signal(nc->get_name(), identifier) || ClassDB::has_method(nc->get_name(), identifier))) { |
| 351 | // Get like it was a property. |
| 352 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(); // TODO: Get type here. |
| 353 | GDScriptCodeGenerator::Address self(GDScriptCodeGenerator::Address::SELF); |
| 354 | |
| 355 | gen->write_get_named(temp, identifier, self); |
| 356 | return temp; |
| 357 | } |
| 358 | } |
| 359 | } break; |
| 360 | case GDScriptParser::IdentifierNode::MEMBER_CONSTANT: |
| 361 | case GDScriptParser::IdentifierNode::MEMBER_CLASS: { |
| 362 | // Try class constants. |
| 363 | GDScript *owner = codegen.script; |
| 364 | while (owner) { |
| 365 | GDScript *scr = owner; |
| 366 | GDScriptNativeClass *nc = nullptr; |
| 367 | |
| 368 | while (scr) { |
| 369 | if (scr->constants.has(identifier)) { |
| 370 | return codegen.add_constant(scr->constants[identifier]); // TODO: Get type here. |
| 371 | } |
| 372 | if (scr->native.is_valid()) { |
| 373 | nc = scr->native.ptr(); |
| 374 | } |
| 375 | scr = scr->base.ptr(); |
| 376 | } |
| 377 | |
| 378 | // Class C++ integer constant. |
| 379 | if (nc) { |
| 380 | bool success = false; |
| 381 | int64_t constant = ClassDB::get_integer_constant(nc->get_name(), identifier, &success); |
| 382 | if (success) { |
| 383 | return codegen.add_constant(constant); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | owner = owner->_owner; |
| 388 | } |
| 389 | } break; |
| 390 | case GDScriptParser::IdentifierNode::STATIC_VARIABLE: { |
| 391 | // Try static variables. |
| 392 | GDScript *scr = codegen.script; |
| 393 | while (scr) { |
| 394 | if (scr->static_variables_indices.has(identifier)) { |
| 395 | if (scr->static_variables_indices[identifier].getter != StringName() && scr->static_variables_indices[identifier].getter != codegen.function_name) { |
| 396 | // Perform getter. |
| 397 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(scr->static_variables_indices[identifier].data_type); |
| 398 | GDScriptCodeGenerator::Address class_addr(GDScriptCodeGenerator::Address::CLASS); |
| 399 | Vector<GDScriptCodeGenerator::Address> args; // No argument needed. |
| 400 | gen->write_call(temp, class_addr, scr->static_variables_indices[identifier].getter, args); |
| 401 | return temp; |
| 402 | } else { |
| 403 | // No getter or inside getter: direct variable access. |
| 404 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(scr->static_variables_indices[identifier].data_type); |
| 405 | GDScriptCodeGenerator::Address _class = codegen.add_constant(scr); |
| 406 | int index = scr->static_variables_indices[identifier].index; |
| 407 | gen->write_get_static_variable(temp, _class, index); |
| 408 | return temp; |
| 409 | } |
| 410 | } |
| 411 | scr = scr->base.ptr(); |
| 412 | } |
| 413 | } break; |
| 414 | |
| 415 | // GLOBALS. |
| 416 | case GDScriptParser::IdentifierNode::NATIVE_CLASS: |
| 417 | case GDScriptParser::IdentifierNode::UNDEFINED_SOURCE: { |
| 418 | // Try globals. |
| 419 | if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) { |
| 420 | // If it's an autoload singleton, we postpone to load it at runtime. |
| 421 | // This is so one autoload doesn't try to load another before it's compiled. |
| 422 | HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list(); |
| 423 | if (autoloads.has(identifier) && autoloads[identifier].is_singleton) { |
| 424 | GDScriptCodeGenerator::Address global = codegen.add_temporary(_gdtype_from_datatype(in->get_datatype(), codegen.script)); |
| 425 | int idx = GDScriptLanguage::get_singleton()->get_global_map()[identifier]; |
| 426 | gen->write_store_global(global, idx); |
| 427 | return global; |
| 428 | } else { |
| 429 | int idx = GDScriptLanguage::get_singleton()->get_global_map()[identifier]; |
| 430 | Variant global = GDScriptLanguage::get_singleton()->get_global_array()[idx]; |
| 431 | return codegen.add_constant(global); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Try global classes. |
| 436 | if (ScriptServer::is_global_class(identifier)) { |
| 437 | const GDScriptParser::ClassNode *class_node = codegen.class_node; |
| 438 | while (class_node->outer) { |
| 439 | class_node = class_node->outer; |
| 440 | } |
| 441 | |
| 442 | Ref<Resource> res; |
| 443 | |
| 444 | if (class_node->identifier && class_node->identifier->name == identifier) { |
| 445 | res = Ref<GDScript>(main_script); |
| 446 | } else { |
| 447 | String global_class_path = ScriptServer::get_global_class_path(identifier); |
| 448 | if (ResourceLoader::get_resource_type(global_class_path) == "GDScript") { |
| 449 | Error err = OK; |
| 450 | // Should not need to pass p_owner since analyzer will already have done it. |
| 451 | res = GDScriptCache::get_shallow_script(global_class_path, err); |
| 452 | if (err != OK) { |
| 453 | _set_error("Can't load global class " + String(identifier), p_expression); |
| 454 | r_error = ERR_COMPILATION_FAILED; |
| 455 | return GDScriptCodeGenerator::Address(); |
| 456 | } |
| 457 | } else { |
| 458 | res = ResourceLoader::load(global_class_path); |
| 459 | if (res.is_null()) { |
| 460 | _set_error("Can't load global class " + String(identifier) + ", cyclic reference?", p_expression); |
| 461 | r_error = ERR_COMPILATION_FAILED; |
| 462 | return GDScriptCodeGenerator::Address(); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return codegen.add_constant(res); |
| 468 | } |
| 469 | |
| 470 | #ifdef TOOLS_ENABLED |
| 471 | if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(identifier)) { |
| 472 | GDScriptCodeGenerator::Address global = codegen.add_temporary(); // TODO: Get type. |
| 473 | gen->write_store_named_global(global, identifier); |
| 474 | return global; |
| 475 | } |
| 476 | #endif |
| 477 | |
| 478 | } break; |
| 479 | } |
| 480 | |
| 481 | // Not found, error. |
| 482 | _set_error("Identifier not found: " + String(identifier), p_expression); |
| 483 | r_error = ERR_COMPILATION_FAILED; |
| 484 | return GDScriptCodeGenerator::Address(); |
| 485 | } break; |
| 486 | case GDScriptParser::Node::LITERAL: { |
| 487 | // Return constant. |
| 488 | const GDScriptParser::LiteralNode *cn = static_cast<const GDScriptParser::LiteralNode *>(p_expression); |
| 489 | |
| 490 | return codegen.add_constant(cn->value); |
| 491 | } break; |
| 492 | case GDScriptParser::Node::SELF: { |
| 493 | //return constant |
| 494 | if (codegen.function_node && codegen.function_node->is_static) { |
| 495 | _set_error("'self' not present in static function.", p_expression); |
| 496 | r_error = ERR_COMPILATION_FAILED; |
| 497 | return GDScriptCodeGenerator::Address(); |
| 498 | } |
| 499 | return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::SELF); |
| 500 | } break; |
| 501 | case GDScriptParser::Node::ARRAY: { |
| 502 | const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression); |
| 503 | Vector<GDScriptCodeGenerator::Address> values; |
| 504 | |
| 505 | // Create the result temporary first since it's the last to be killed. |
| 506 | GDScriptDataType array_type = _gdtype_from_datatype(an->get_datatype(), codegen.script); |
| 507 | GDScriptCodeGenerator::Address result = codegen.add_temporary(array_type); |
| 508 | |
| 509 | for (int i = 0; i < an->elements.size(); i++) { |
| 510 | GDScriptCodeGenerator::Address val = _parse_expression(codegen, r_error, an->elements[i]); |
| 511 | if (r_error) { |
| 512 | return GDScriptCodeGenerator::Address(); |
| 513 | } |
| 514 | values.push_back(val); |
| 515 | } |
| 516 | |
| 517 | if (array_type.has_container_element_type(0)) { |
| 518 | gen->write_construct_typed_array(result, array_type.get_container_element_type(0), values); |
| 519 | } else { |
| 520 | gen->write_construct_array(result, values); |
| 521 | } |
| 522 | |
| 523 | for (int i = 0; i < values.size(); i++) { |
| 524 | if (values[i].mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 525 | gen->pop_temporary(); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | return result; |
| 530 | } break; |
| 531 | case GDScriptParser::Node::DICTIONARY: { |
| 532 | const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression); |
| 533 | Vector<GDScriptCodeGenerator::Address> elements; |
| 534 | |
| 535 | // Create the result temporary first since it's the last to be killed. |
| 536 | GDScriptDataType dict_type = _gdtype_from_datatype(dn->get_datatype(), codegen.script); |
| 537 | GDScriptCodeGenerator::Address result = codegen.add_temporary(dict_type); |
| 538 | |
| 539 | for (int i = 0; i < dn->elements.size(); i++) { |
| 540 | // Key. |
| 541 | GDScriptCodeGenerator::Address element; |
| 542 | switch (dn->style) { |
| 543 | case GDScriptParser::DictionaryNode::PYTHON_DICT: |
| 544 | // Python-style: key is any expression. |
| 545 | element = _parse_expression(codegen, r_error, dn->elements[i].key); |
| 546 | if (r_error) { |
| 547 | return GDScriptCodeGenerator::Address(); |
| 548 | } |
| 549 | break; |
| 550 | case GDScriptParser::DictionaryNode::LUA_TABLE: |
| 551 | // Lua-style: key is an identifier interpreted as StringName. |
| 552 | StringName key = dn->elements[i].key->reduced_value.operator StringName(); |
| 553 | element = codegen.add_constant(key); |
| 554 | break; |
| 555 | } |
| 556 | |
| 557 | elements.push_back(element); |
| 558 | |
| 559 | element = _parse_expression(codegen, r_error, dn->elements[i].value); |
| 560 | if (r_error) { |
| 561 | return GDScriptCodeGenerator::Address(); |
| 562 | } |
| 563 | |
| 564 | elements.push_back(element); |
| 565 | } |
| 566 | |
| 567 | if (dict_type.has_container_element_types()) { |
| 568 | gen->write_construct_typed_dictionary(result, dict_type.get_container_element_type_or_variant(0), dict_type.get_container_element_type_or_variant(1), elements); |
| 569 | } else { |
| 570 | gen->write_construct_dictionary(result, elements); |
| 571 | } |
| 572 | |
| 573 | for (int i = 0; i < elements.size(); i++) { |
| 574 | if (elements[i].mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 575 | gen->pop_temporary(); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | return result; |
| 580 | } break; |
| 581 | case GDScriptParser::Node::CAST: { |
| 582 | const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression); |
| 583 | GDScriptDataType cast_type = _gdtype_from_datatype(cn->get_datatype(), codegen.script, false); |
| 584 | |
| 585 | GDScriptCodeGenerator::Address result; |
| 586 | if (cast_type.has_type()) { |
| 587 | // Create temporary for result first since it will be deleted last. |
| 588 | result = codegen.add_temporary(cast_type); |
| 589 | |
| 590 | GDScriptCodeGenerator::Address src = _parse_expression(codegen, r_error, cn->operand); |
| 591 | |
| 592 | gen->write_cast(result, src, cast_type); |
| 593 | |
| 594 | if (src.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 595 | gen->pop_temporary(); |
| 596 | } |
| 597 | } else { |
| 598 | result = _parse_expression(codegen, r_error, cn->operand); |
| 599 | } |
| 600 | |
| 601 | return result; |
| 602 | } break; |
| 603 | case GDScriptParser::Node::CALL: { |
| 604 | const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression); |
| 605 | bool is_awaited = p_expression == awaited_node; |
| 606 | GDScriptDataType type = _gdtype_from_datatype(call->get_datatype(), codegen.script); |
| 607 | GDScriptCodeGenerator::Address result; |
| 608 | if (p_root) { |
| 609 | result = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::NIL); |
| 610 | } else { |
| 611 | result = codegen.add_temporary(type); |
| 612 | } |
| 613 | |
| 614 | Vector<GDScriptCodeGenerator::Address> arguments; |
| 615 | for (int i = 0; i < call->arguments.size(); i++) { |
| 616 | GDScriptCodeGenerator::Address arg = _parse_expression(codegen, r_error, call->arguments[i]); |
| 617 | if (r_error) { |
| 618 | return GDScriptCodeGenerator::Address(); |
| 619 | } |
| 620 | arguments.push_back(arg); |
| 621 | } |
| 622 | |
| 623 | if (!call->is_super && call->callee->type == GDScriptParser::Node::IDENTIFIER && GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) { |
| 624 | gen->write_construct(result, GDScriptParser::get_builtin_type(call->function_name), arguments); |
| 625 | } else if (!call->is_super && call->callee->type == GDScriptParser::Node::IDENTIFIER && Variant::has_utility_function(call->function_name)) { |
| 626 | // Variant utility function. |
| 627 | gen->write_call_utility(result, call->function_name, arguments); |
| 628 | } else if (!call->is_super && call->callee->type == GDScriptParser::Node::IDENTIFIER && GDScriptUtilityFunctions::function_exists(call->function_name)) { |
| 629 | // GDScript utility function. |
| 630 | gen->write_call_gdscript_utility(result, call->function_name, arguments); |
| 631 | } else { |
| 632 | // Regular function. |
| 633 | const GDScriptParser::ExpressionNode *callee = call->callee; |
| 634 | |
| 635 | if (call->is_super) { |
| 636 | // Super call. |
| 637 | gen->write_super_call(result, call->function_name, arguments); |
| 638 | } else { |
| 639 | if (callee->type == GDScriptParser::Node::IDENTIFIER) { |
| 640 | // Self function call. |
| 641 | if (ClassDB::has_method(codegen.script->native->get_name(), call->function_name)) { |
| 642 | // Native method, use faster path. |
| 643 | GDScriptCodeGenerator::Address self; |
| 644 | self.mode = GDScriptCodeGenerator::Address::SELF; |
| 645 | MethodBind *method = ClassDB::get_method(codegen.script->native->get_name(), call->function_name); |
| 646 | |
| 647 | if (_can_use_validate_call(method, arguments)) { |
| 648 | // Exact arguments, use validated call. |
| 649 | gen->write_call_method_bind_validated(result, self, method, arguments); |
| 650 | } else { |
| 651 | // Not exact arguments, but still can use method bind call. |
| 652 | gen->write_call_method_bind(result, self, method, arguments); |
| 653 | } |
| 654 | } else if (call->is_static || codegen.is_static || (codegen.function_node && codegen.function_node->is_static) || call->function_name == "new") { |
| 655 | GDScriptCodeGenerator::Address self; |
| 656 | self.mode = GDScriptCodeGenerator::Address::CLASS; |
| 657 | if (is_awaited) { |
| 658 | gen->write_call_async(result, self, call->function_name, arguments); |
| 659 | } else { |
| 660 | gen->write_call(result, self, call->function_name, arguments); |
| 661 | } |
| 662 | } else { |
| 663 | if (is_awaited) { |
| 664 | gen->write_call_self_async(result, call->function_name, arguments); |
| 665 | } else { |
| 666 | gen->write_call_self(result, call->function_name, arguments); |
| 667 | } |
| 668 | } |
| 669 | } else if (callee->type == GDScriptParser::Node::SUBSCRIPT) { |
| 670 | const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee); |
| 671 | |
| 672 | if (subscript->is_attribute) { |
| 673 | // May be static built-in method call. |
| 674 | if (!call->is_super && subscript->base->type == GDScriptParser::Node::IDENTIFIER && GDScriptParser::get_builtin_type(static_cast<GDScriptParser::IdentifierNode *>(subscript->base)->name) < Variant::VARIANT_MAX) { |
| 675 | gen->write_call_builtin_type_static(result, GDScriptParser::get_builtin_type(static_cast<GDScriptParser::IdentifierNode *>(subscript->base)->name), subscript->attribute->name, arguments); |
| 676 | } else if (!call->is_super && subscript->base->type == GDScriptParser::Node::IDENTIFIER && call->function_name != SNAME("new") && |
| 677 | static_cast<GDScriptParser::IdentifierNode *>(subscript->base)->source == GDScriptParser::IdentifierNode::NATIVE_CLASS && !Engine::get_singleton()->has_singleton(static_cast<GDScriptParser::IdentifierNode *>(subscript->base)->name)) { |
| 678 | // It's a static native method call. |
| 679 | StringName class_name = static_cast<GDScriptParser::IdentifierNode *>(subscript->base)->name; |
| 680 | MethodBind *method = ClassDB::get_method(class_name, subscript->attribute->name); |
| 681 | if (_can_use_validate_call(method, arguments)) { |
| 682 | // Exact arguments, use validated call. |
| 683 | gen->write_call_native_static_validated(result, method, arguments); |
| 684 | } else { |
| 685 | // Not exact arguments, use regular static call |
| 686 | gen->write_call_native_static(result, class_name, subscript->attribute->name, arguments); |
| 687 | } |
| 688 | } else { |
| 689 | GDScriptCodeGenerator::Address base = _parse_expression(codegen, r_error, subscript->base); |
| 690 | if (r_error) { |
| 691 | return GDScriptCodeGenerator::Address(); |
| 692 | } |
| 693 | if (is_awaited) { |
| 694 | gen->write_call_async(result, base, call->function_name, arguments); |
| 695 | } else if (base.type.kind != GDScriptDataType::VARIANT && base.type.kind != GDScriptDataType::BUILTIN) { |
| 696 | // Native method, use faster path. |
| 697 | StringName class_name; |
| 698 | if (base.type.kind == GDScriptDataType::NATIVE) { |
| 699 | class_name = base.type.native_type; |
| 700 | } else { |
| 701 | class_name = base.type.native_type == StringName() ? base.type.script_type->get_instance_base_type() : base.type.native_type; |
| 702 | } |
| 703 | if (GDScriptAnalyzer::class_exists(class_name) && ClassDB::has_method(class_name, call->function_name)) { |
| 704 | MethodBind *method = ClassDB::get_method(class_name, call->function_name); |
| 705 | if (_can_use_validate_call(method, arguments)) { |
| 706 | // Exact arguments, use validated call. |
| 707 | gen->write_call_method_bind_validated(result, base, method, arguments); |
| 708 | } else { |
| 709 | // Not exact arguments, but still can use method bind call. |
| 710 | gen->write_call_method_bind(result, base, method, arguments); |
| 711 | } |
| 712 | } else { |
| 713 | gen->write_call(result, base, call->function_name, arguments); |
| 714 | } |
| 715 | } else if (base.type.kind == GDScriptDataType::BUILTIN) { |
| 716 | gen->write_call_builtin_type(result, base, base.type.builtin_type, call->function_name, arguments); |
| 717 | } else { |
| 718 | gen->write_call(result, base, call->function_name, arguments); |
| 719 | } |
| 720 | if (base.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 721 | gen->pop_temporary(); |
| 722 | } |
| 723 | } |
| 724 | } else { |
| 725 | _set_error("Cannot call something that isn't a function.", call->callee); |
| 726 | r_error = ERR_COMPILATION_FAILED; |
| 727 | return GDScriptCodeGenerator::Address(); |
| 728 | } |
| 729 | } else { |
| 730 | _set_error("Compiler bug (please report): incorrect callee type in call node.", call->callee); |
| 731 | r_error = ERR_COMPILATION_FAILED; |
| 732 | return GDScriptCodeGenerator::Address(); |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | for (int i = 0; i < arguments.size(); i++) { |
| 738 | if (arguments[i].mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 739 | gen->pop_temporary(); |
| 740 | } |
| 741 | } |
| 742 | return result; |
| 743 | } break; |
| 744 | case GDScriptParser::Node::GET_NODE: { |
| 745 | const GDScriptParser::GetNodeNode *get_node = static_cast<const GDScriptParser::GetNodeNode *>(p_expression); |
| 746 | |
| 747 | Vector<GDScriptCodeGenerator::Address> args; |
| 748 | args.push_back(codegen.add_constant(NodePath(get_node->full_path))); |
| 749 | |
| 750 | GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(get_node->get_datatype(), codegen.script)); |
| 751 | |
| 752 | MethodBind *get_node_method = ClassDB::get_method("Node", "get_node"); |
| 753 | gen->write_call_method_bind_validated(result, GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::SELF), get_node_method, args); |
| 754 | |
| 755 | return result; |
| 756 | } break; |
| 757 | case GDScriptParser::Node::PRELOAD: { |
| 758 | const GDScriptParser::PreloadNode *preload = static_cast<const GDScriptParser::PreloadNode *>(p_expression); |
| 759 | |
| 760 | // Add resource as constant. |
| 761 | return codegen.add_constant(preload->resource); |
| 762 | } break; |
| 763 | case GDScriptParser::Node::AWAIT: { |
| 764 | const GDScriptParser::AwaitNode *await = static_cast<const GDScriptParser::AwaitNode *>(p_expression); |
| 765 | |
| 766 | GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(p_expression->get_datatype(), codegen.script)); |
| 767 | GDScriptParser::ExpressionNode *previous_awaited_node = awaited_node; |
| 768 | awaited_node = await->to_await; |
| 769 | GDScriptCodeGenerator::Address argument = _parse_expression(codegen, r_error, await->to_await); |
| 770 | awaited_node = previous_awaited_node; |
| 771 | if (r_error) { |
| 772 | return GDScriptCodeGenerator::Address(); |
| 773 | } |
| 774 | |
| 775 | gen->write_await(result, argument); |
| 776 | |
| 777 | if (argument.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 778 | gen->pop_temporary(); |
| 779 | } |
| 780 | |
| 781 | return result; |
| 782 | } break; |
| 783 | // Indexing operator. |
| 784 | case GDScriptParser::Node::SUBSCRIPT: { |
| 785 | const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression); |
| 786 | GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(subscript->get_datatype(), codegen.script)); |
| 787 | |
| 788 | GDScriptCodeGenerator::Address base = _parse_expression(codegen, r_error, subscript->base); |
| 789 | if (r_error) { |
| 790 | return GDScriptCodeGenerator::Address(); |
| 791 | } |
| 792 | |
| 793 | bool named = subscript->is_attribute; |
| 794 | StringName name; |
| 795 | GDScriptCodeGenerator::Address index; |
| 796 | if (subscript->is_attribute) { |
| 797 | if (subscript->base->type == GDScriptParser::Node::SELF && codegen.script) { |
| 798 | GDScriptParser::IdentifierNode *identifier = subscript->attribute; |
| 799 | HashMap<StringName, GDScript::MemberInfo>::Iterator MI = codegen.script->member_indices.find(identifier->name); |
| 800 | |
| 801 | #ifdef DEBUG_ENABLED |
| 802 | if (MI && MI->value.getter == codegen.function_name) { |
| 803 | String n = identifier->name; |
| 804 | _set_error("Must use '" + n + "' instead of 'self." + n + "' in getter.", identifier); |
| 805 | r_error = ERR_COMPILATION_FAILED; |
| 806 | return GDScriptCodeGenerator::Address(); |
| 807 | } |
| 808 | #endif |
| 809 | |
| 810 | if (MI && MI->value.getter == "") { |
| 811 | // Remove result temp as we don't need it. |
| 812 | gen->pop_temporary(); |
| 813 | // Faster than indexing self (as if no self. had been used). |
| 814 | return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::MEMBER, MI->value.index, _gdtype_from_datatype(subscript->get_datatype(), codegen.script)); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | name = subscript->attribute->name; |
| 819 | named = true; |
| 820 | } else { |
| 821 | if (subscript->index->is_constant && subscript->index->reduced_value.get_type() == Variant::STRING_NAME) { |
| 822 | // Also, somehow, named (speed up anyway). |
| 823 | name = subscript->index->reduced_value; |
| 824 | named = true; |
| 825 | } else { |
| 826 | // Regular indexing. |
| 827 | index = _parse_expression(codegen, r_error, subscript->index); |
| 828 | if (r_error) { |
| 829 | return GDScriptCodeGenerator::Address(); |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | if (named) { |
| 835 | gen->write_get_named(result, name, base); |
| 836 | } else { |
| 837 | gen->write_get(result, index, base); |
| 838 | } |
| 839 | |
| 840 | if (index.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 841 | gen->pop_temporary(); |
| 842 | } |
| 843 | if (base.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 844 | gen->pop_temporary(); |
| 845 | } |
| 846 | |
| 847 | return result; |
| 848 | } break; |
| 849 | case GDScriptParser::Node::UNARY_OPERATOR: { |
| 850 | const GDScriptParser::UnaryOpNode *unary = static_cast<const GDScriptParser::UnaryOpNode *>(p_expression); |
| 851 | |
| 852 | GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(unary->get_datatype(), codegen.script)); |
| 853 | |
| 854 | GDScriptCodeGenerator::Address operand = _parse_expression(codegen, r_error, unary->operand); |
| 855 | if (r_error) { |
| 856 | return GDScriptCodeGenerator::Address(); |
| 857 | } |
| 858 | |
| 859 | gen->write_unary_operator(result, unary->variant_op, operand); |
| 860 | |
| 861 | if (operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 862 | gen->pop_temporary(); |
| 863 | } |
| 864 | |
| 865 | return result; |
| 866 | } |
| 867 | case GDScriptParser::Node::BINARY_OPERATOR: { |
| 868 | const GDScriptParser::BinaryOpNode *binary = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression); |
| 869 | |
| 870 | GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(binary->get_datatype(), codegen.script)); |
| 871 | |
| 872 | switch (binary->operation) { |
| 873 | case GDScriptParser::BinaryOpNode::OP_LOGIC_AND: { |
| 874 | // AND operator with early out on failure. |
| 875 | GDScriptCodeGenerator::Address left_operand = _parse_expression(codegen, r_error, binary->left_operand); |
| 876 | gen->write_and_left_operand(left_operand); |
| 877 | GDScriptCodeGenerator::Address right_operand = _parse_expression(codegen, r_error, binary->right_operand); |
| 878 | gen->write_and_right_operand(right_operand); |
| 879 | |
| 880 | gen->write_end_and(result); |
| 881 | |
| 882 | if (right_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 883 | gen->pop_temporary(); |
| 884 | } |
| 885 | if (left_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 886 | gen->pop_temporary(); |
| 887 | } |
| 888 | } break; |
| 889 | case GDScriptParser::BinaryOpNode::OP_LOGIC_OR: { |
| 890 | // OR operator with early out on success. |
| 891 | GDScriptCodeGenerator::Address left_operand = _parse_expression(codegen, r_error, binary->left_operand); |
| 892 | gen->write_or_left_operand(left_operand); |
| 893 | GDScriptCodeGenerator::Address right_operand = _parse_expression(codegen, r_error, binary->right_operand); |
| 894 | gen->write_or_right_operand(right_operand); |
| 895 | |
| 896 | gen->write_end_or(result); |
| 897 | |
| 898 | if (right_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 899 | gen->pop_temporary(); |
| 900 | } |
| 901 | if (left_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 902 | gen->pop_temporary(); |
| 903 | } |
| 904 | } break; |
| 905 | default: { |
| 906 | GDScriptCodeGenerator::Address left_operand = _parse_expression(codegen, r_error, binary->left_operand); |
| 907 | GDScriptCodeGenerator::Address right_operand = _parse_expression(codegen, r_error, binary->right_operand); |
| 908 | |
| 909 | gen->write_binary_operator(result, binary->variant_op, left_operand, right_operand); |
| 910 | |
| 911 | if (right_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 912 | gen->pop_temporary(); |
| 913 | } |
| 914 | if (left_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 915 | gen->pop_temporary(); |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | return result; |
| 920 | } break; |
| 921 | case GDScriptParser::Node::TERNARY_OPERATOR: { |
| 922 | // x IF a ELSE y operator with early out on failure. |
| 923 | const GDScriptParser::TernaryOpNode *ternary = static_cast<const GDScriptParser::TernaryOpNode *>(p_expression); |
| 924 | GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(ternary->get_datatype(), codegen.script)); |
| 925 | |
| 926 | gen->write_start_ternary(result); |
| 927 | |
| 928 | GDScriptCodeGenerator::Address condition = _parse_expression(codegen, r_error, ternary->condition); |
| 929 | if (r_error) { |
| 930 | return GDScriptCodeGenerator::Address(); |
| 931 | } |
| 932 | gen->write_ternary_condition(condition); |
| 933 | |
| 934 | if (condition.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 935 | gen->pop_temporary(); |
| 936 | } |
| 937 | |
| 938 | GDScriptCodeGenerator::Address true_expr = _parse_expression(codegen, r_error, ternary->true_expr); |
| 939 | if (r_error) { |
| 940 | return GDScriptCodeGenerator::Address(); |
| 941 | } |
| 942 | gen->write_ternary_true_expr(true_expr); |
| 943 | if (true_expr.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 944 | gen->pop_temporary(); |
| 945 | } |
| 946 | |
| 947 | GDScriptCodeGenerator::Address false_expr = _parse_expression(codegen, r_error, ternary->false_expr); |
| 948 | if (r_error) { |
| 949 | return GDScriptCodeGenerator::Address(); |
| 950 | } |
| 951 | gen->write_ternary_false_expr(false_expr); |
| 952 | if (false_expr.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 953 | gen->pop_temporary(); |
| 954 | } |
| 955 | |
| 956 | gen->write_end_ternary(); |
| 957 | |
| 958 | return result; |
| 959 | } break; |
| 960 | case GDScriptParser::Node::TYPE_TEST: { |
| 961 | const GDScriptParser::TypeTestNode *type_test = static_cast<const GDScriptParser::TypeTestNode *>(p_expression); |
| 962 | GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(type_test->get_datatype(), codegen.script)); |
| 963 | |
| 964 | GDScriptCodeGenerator::Address operand = _parse_expression(codegen, r_error, type_test->operand); |
| 965 | GDScriptDataType test_type = _gdtype_from_datatype(type_test->test_datatype, codegen.script, false); |
| 966 | if (r_error) { |
| 967 | return GDScriptCodeGenerator::Address(); |
| 968 | } |
| 969 | |
| 970 | if (test_type.has_type()) { |
| 971 | gen->write_type_test(result, operand, test_type); |
| 972 | } else { |
| 973 | gen->write_assign_true(result); |
| 974 | } |
| 975 | |
| 976 | if (operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 977 | gen->pop_temporary(); |
| 978 | } |
| 979 | |
| 980 | return result; |
| 981 | } break; |
| 982 | case GDScriptParser::Node::ASSIGNMENT: { |
| 983 | const GDScriptParser::AssignmentNode *assignment = static_cast<const GDScriptParser::AssignmentNode *>(p_expression); |
| 984 | |
| 985 | if (assignment->assignee->type == GDScriptParser::Node::SUBSCRIPT) { |
| 986 | // SET (chained) MODE! |
| 987 | const GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(assignment->assignee); |
| 988 | #ifdef DEBUG_ENABLED |
| 989 | if (subscript->is_attribute && subscript->base->type == GDScriptParser::Node::SELF && codegen.script) { |
| 990 | HashMap<StringName, GDScript::MemberInfo>::Iterator MI = codegen.script->member_indices.find(subscript->attribute->name); |
| 991 | if (MI && MI->value.setter == codegen.function_name) { |
| 992 | String n = subscript->attribute->name; |
| 993 | _set_error("Must use '" + n + "' instead of 'self." + n + "' in setter.", subscript); |
| 994 | r_error = ERR_COMPILATION_FAILED; |
| 995 | return GDScriptCodeGenerator::Address(); |
| 996 | } |
| 997 | } |
| 998 | #endif |
| 999 | /* Find chain of sets */ |
| 1000 | |
| 1001 | StringName assign_class_member_property; |
| 1002 | |
| 1003 | GDScriptCodeGenerator::Address target_member_property; |
| 1004 | bool is_member_property = false; |
| 1005 | bool member_property_has_setter = false; |
| 1006 | bool member_property_is_in_setter = false; |
| 1007 | bool is_static = false; |
| 1008 | GDScriptCodeGenerator::Address static_var_class; |
| 1009 | int static_var_index = 0; |
| 1010 | GDScriptDataType static_var_data_type; |
| 1011 | StringName var_name; |
| 1012 | StringName member_property_setter_function; |
| 1013 | |
| 1014 | List<const GDScriptParser::SubscriptNode *> chain; |
| 1015 | |
| 1016 | { |
| 1017 | // Create get/set chain. |
| 1018 | const GDScriptParser::SubscriptNode *n = subscript; |
| 1019 | while (true) { |
| 1020 | chain.push_back(n); |
| 1021 | if (n->base->type != GDScriptParser::Node::SUBSCRIPT) { |
| 1022 | // Check for a property. |
| 1023 | if (n->base->type == GDScriptParser::Node::IDENTIFIER) { |
| 1024 | GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(n->base); |
| 1025 | var_name = identifier->name; |
| 1026 | if (_is_class_member_property(codegen, var_name)) { |
| 1027 | assign_class_member_property = var_name; |
| 1028 | } else if (!_is_local_or_parameter(codegen, var_name)) { |
| 1029 | if (codegen.script->member_indices.has(var_name)) { |
| 1030 | is_member_property = true; |
| 1031 | is_static = false; |
| 1032 | const GDScript::MemberInfo &minfo = codegen.script->member_indices[var_name]; |
| 1033 | member_property_setter_function = minfo.setter; |
| 1034 | member_property_has_setter = member_property_setter_function != StringName(); |
| 1035 | member_property_is_in_setter = member_property_has_setter && member_property_setter_function == codegen.function_name; |
| 1036 | target_member_property.mode = GDScriptCodeGenerator::Address::MEMBER; |
| 1037 | target_member_property.address = minfo.index; |
| 1038 | target_member_property.type = minfo.data_type; |
| 1039 | } else { |
| 1040 | // Try static variables. |
| 1041 | GDScript *scr = codegen.script; |
| 1042 | while (scr) { |
| 1043 | if (scr->static_variables_indices.has(var_name)) { |
| 1044 | is_member_property = true; |
| 1045 | is_static = true; |
| 1046 | const GDScript::MemberInfo &minfo = scr->static_variables_indices[var_name]; |
| 1047 | member_property_setter_function = minfo.setter; |
| 1048 | member_property_has_setter = member_property_setter_function != StringName(); |
| 1049 | member_property_is_in_setter = member_property_has_setter && member_property_setter_function == codegen.function_name; |
| 1050 | static_var_class = codegen.add_constant(scr); |
| 1051 | static_var_index = minfo.index; |
| 1052 | static_var_data_type = minfo.data_type; |
| 1053 | break; |
| 1054 | } |
| 1055 | scr = scr->base.ptr(); |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | break; |
| 1061 | } |
| 1062 | n = static_cast<const GDScriptParser::SubscriptNode *>(n->base); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | /* Chain of gets */ |
| 1067 | |
| 1068 | // Get at (potential) root stack pos, so it can be returned. |
| 1069 | GDScriptCodeGenerator::Address base = _parse_expression(codegen, r_error, chain.back()->get()->base); |
| 1070 | const bool base_known_type = base.type.has_type(); |
| 1071 | const bool base_is_shared = Variant::is_type_shared(base.type.builtin_type); |
| 1072 | |
| 1073 | if (r_error) { |
| 1074 | return GDScriptCodeGenerator::Address(); |
| 1075 | } |
| 1076 | |
| 1077 | GDScriptCodeGenerator::Address prev_base = base; |
| 1078 | |
| 1079 | // In case the base has a setter, don't use the address directly, as we want to call that setter. |
| 1080 | // So use a temp value instead and call the setter at the end. |
| 1081 | GDScriptCodeGenerator::Address base_temp; |
| 1082 | if ((!base_known_type || !base_is_shared) && base.mode == GDScriptCodeGenerator::Address::MEMBER && member_property_has_setter && !member_property_is_in_setter) { |
| 1083 | base_temp = codegen.add_temporary(base.type); |
| 1084 | gen->write_assign(base_temp, base); |
| 1085 | prev_base = base_temp; |
| 1086 | } |
| 1087 | |
| 1088 | struct ChainInfo { |
| 1089 | bool is_named = false; |
| 1090 | GDScriptCodeGenerator::Address base; |
| 1091 | GDScriptCodeGenerator::Address key; |
| 1092 | StringName name; |
| 1093 | }; |
| 1094 | |
| 1095 | List<ChainInfo> set_chain; |
| 1096 | |
| 1097 | for (List<const GDScriptParser::SubscriptNode *>::Element *E = chain.back(); E; E = E->prev()) { |
| 1098 | if (E == chain.front()) { |
| 1099 | // Skip the main subscript, since we'll assign to that. |
| 1100 | break; |
| 1101 | } |
| 1102 | const GDScriptParser::SubscriptNode *subscript_elem = E->get(); |
| 1103 | GDScriptCodeGenerator::Address value = codegen.add_temporary(_gdtype_from_datatype(subscript_elem->get_datatype(), codegen.script)); |
| 1104 | GDScriptCodeGenerator::Address key; |
| 1105 | StringName name; |
| 1106 | |
| 1107 | if (subscript_elem->is_attribute) { |
| 1108 | name = subscript_elem->attribute->name; |
| 1109 | gen->write_get_named(value, name, prev_base); |
| 1110 | } else { |
| 1111 | key = _parse_expression(codegen, r_error, subscript_elem->index); |
| 1112 | if (r_error) { |
| 1113 | return GDScriptCodeGenerator::Address(); |
| 1114 | } |
| 1115 | gen->write_get(value, key, prev_base); |
| 1116 | } |
| 1117 | |
| 1118 | // Store base and key for setting it back later. |
| 1119 | set_chain.push_front({ subscript_elem->is_attribute, prev_base, key, name }); // Push to front to invert the list. |
| 1120 | prev_base = value; |
| 1121 | } |
| 1122 | |
| 1123 | // Get value to assign. |
| 1124 | GDScriptCodeGenerator::Address assigned = _parse_expression(codegen, r_error, assignment->assigned_value); |
| 1125 | if (r_error) { |
| 1126 | return GDScriptCodeGenerator::Address(); |
| 1127 | } |
| 1128 | // Get the key if needed. |
| 1129 | GDScriptCodeGenerator::Address key; |
| 1130 | StringName name; |
| 1131 | if (subscript->is_attribute) { |
| 1132 | name = subscript->attribute->name; |
| 1133 | } else { |
| 1134 | key = _parse_expression(codegen, r_error, subscript->index); |
| 1135 | if (r_error) { |
| 1136 | return GDScriptCodeGenerator::Address(); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | // Perform operator if any. |
| 1141 | if (assignment->operation != GDScriptParser::AssignmentNode::OP_NONE) { |
| 1142 | GDScriptCodeGenerator::Address op_result = codegen.add_temporary(_gdtype_from_datatype(assignment->get_datatype(), codegen.script)); |
| 1143 | GDScriptCodeGenerator::Address value = codegen.add_temporary(_gdtype_from_datatype(subscript->get_datatype(), codegen.script)); |
| 1144 | if (subscript->is_attribute) { |
| 1145 | gen->write_get_named(value, name, prev_base); |
| 1146 | } else { |
| 1147 | gen->write_get(value, key, prev_base); |
| 1148 | } |
| 1149 | gen->write_binary_operator(op_result, assignment->variant_op, value, assigned); |
| 1150 | gen->pop_temporary(); |
| 1151 | if (assigned.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1152 | gen->pop_temporary(); |
| 1153 | } |
| 1154 | assigned = op_result; |
| 1155 | } |
| 1156 | |
| 1157 | // Perform assignment. |
| 1158 | if (subscript->is_attribute) { |
| 1159 | gen->write_set_named(prev_base, name, assigned); |
| 1160 | } else { |
| 1161 | gen->write_set(prev_base, key, assigned); |
| 1162 | } |
| 1163 | if (key.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1164 | gen->pop_temporary(); |
| 1165 | } |
| 1166 | if (assigned.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1167 | gen->pop_temporary(); |
| 1168 | } |
| 1169 | |
| 1170 | assigned = prev_base; |
| 1171 | |
| 1172 | // Set back the values into their bases. |
| 1173 | for (const ChainInfo &info : set_chain) { |
| 1174 | bool known_type = assigned.type.has_type(); |
| 1175 | bool is_shared = Variant::is_type_shared(assigned.type.builtin_type); |
| 1176 | |
| 1177 | if (!known_type || !is_shared) { |
| 1178 | if (!known_type) { |
| 1179 | // Jump shared values since they are already updated in-place. |
| 1180 | gen->write_jump_if_shared(assigned); |
| 1181 | } |
| 1182 | if (!info.is_named) { |
| 1183 | gen->write_set(info.base, info.key, assigned); |
| 1184 | } else { |
| 1185 | gen->write_set_named(info.base, info.name, assigned); |
| 1186 | } |
| 1187 | if (!known_type) { |
| 1188 | gen->write_end_jump_if_shared(); |
| 1189 | } |
| 1190 | } |
| 1191 | if (!info.is_named && info.key.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1192 | gen->pop_temporary(); |
| 1193 | } |
| 1194 | if (assigned.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1195 | gen->pop_temporary(); |
| 1196 | } |
| 1197 | assigned = info.base; |
| 1198 | } |
| 1199 | |
| 1200 | bool known_type = assigned.type.has_type(); |
| 1201 | bool is_shared = Variant::is_type_shared(assigned.type.builtin_type); |
| 1202 | |
| 1203 | if (!known_type || !is_shared) { |
| 1204 | // If this is a class member property, also assign to it. |
| 1205 | // This allow things like: position.x += 2.0 |
| 1206 | if (assign_class_member_property != StringName()) { |
| 1207 | if (!known_type) { |
| 1208 | gen->write_jump_if_shared(assigned); |
| 1209 | } |
| 1210 | gen->write_set_member(assigned, assign_class_member_property); |
| 1211 | if (!known_type) { |
| 1212 | gen->write_end_jump_if_shared(); |
| 1213 | } |
| 1214 | } else if (is_member_property) { |
| 1215 | // Same as above but for script members. |
| 1216 | if (!known_type) { |
| 1217 | gen->write_jump_if_shared(assigned); |
| 1218 | } |
| 1219 | if (member_property_has_setter && !member_property_is_in_setter) { |
| 1220 | Vector<GDScriptCodeGenerator::Address> args; |
| 1221 | args.push_back(assigned); |
| 1222 | GDScriptCodeGenerator::Address call_base = is_static ? GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CLASS) : GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::SELF); |
| 1223 | gen->write_call(GDScriptCodeGenerator::Address(), call_base, member_property_setter_function, args); |
| 1224 | } else if (is_static) { |
| 1225 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(static_var_data_type); |
| 1226 | gen->write_assign(temp, assigned); |
| 1227 | gen->write_set_static_variable(temp, static_var_class, static_var_index); |
| 1228 | gen->pop_temporary(); |
| 1229 | } else { |
| 1230 | gen->write_assign(target_member_property, assigned); |
| 1231 | } |
| 1232 | if (!known_type) { |
| 1233 | gen->write_end_jump_if_shared(); |
| 1234 | } |
| 1235 | } |
| 1236 | } else if (base_temp.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1237 | if (!base_known_type) { |
| 1238 | gen->write_jump_if_shared(base); |
| 1239 | } |
| 1240 | // Save the temp value back to the base by calling its setter. |
| 1241 | gen->write_call(GDScriptCodeGenerator::Address(), base, member_property_setter_function, { assigned }); |
| 1242 | if (!base_known_type) { |
| 1243 | gen->write_end_jump_if_shared(); |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | if (assigned.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1248 | gen->pop_temporary(); |
| 1249 | } |
| 1250 | } else if (assignment->assignee->type == GDScriptParser::Node::IDENTIFIER && _is_class_member_property(codegen, static_cast<GDScriptParser::IdentifierNode *>(assignment->assignee)->name)) { |
| 1251 | // Assignment to member property. |
| 1252 | GDScriptCodeGenerator::Address assigned_value = _parse_expression(codegen, r_error, assignment->assigned_value); |
| 1253 | if (r_error) { |
| 1254 | return GDScriptCodeGenerator::Address(); |
| 1255 | } |
| 1256 | |
| 1257 | GDScriptCodeGenerator::Address to_assign = assigned_value; |
| 1258 | bool has_operation = assignment->operation != GDScriptParser::AssignmentNode::OP_NONE; |
| 1259 | |
| 1260 | StringName name = static_cast<GDScriptParser::IdentifierNode *>(assignment->assignee)->name; |
| 1261 | |
| 1262 | if (has_operation) { |
| 1263 | GDScriptCodeGenerator::Address op_result = codegen.add_temporary(_gdtype_from_datatype(assignment->get_datatype(), codegen.script)); |
| 1264 | GDScriptCodeGenerator::Address member = codegen.add_temporary(_gdtype_from_datatype(assignment->assignee->get_datatype(), codegen.script)); |
| 1265 | gen->write_get_member(member, name); |
| 1266 | gen->write_binary_operator(op_result, assignment->variant_op, member, assigned_value); |
| 1267 | gen->pop_temporary(); // Pop member temp. |
| 1268 | to_assign = op_result; |
| 1269 | } |
| 1270 | |
| 1271 | gen->write_set_member(to_assign, name); |
| 1272 | |
| 1273 | if (to_assign.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1274 | gen->pop_temporary(); // Pop the assigned expression or the temp result if it has operation. |
| 1275 | } |
| 1276 | if (has_operation && assigned_value.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1277 | gen->pop_temporary(); // Pop the assigned expression if not done before. |
| 1278 | } |
| 1279 | } else { |
| 1280 | // Regular assignment. |
| 1281 | if (assignment->assignee->type != GDScriptParser::Node::IDENTIFIER) { |
| 1282 | _set_error("Compiler bug (please report): Expected the assignee to be an identifier here.", assignment->assignee); |
| 1283 | r_error = ERR_COMPILATION_FAILED; |
| 1284 | return GDScriptCodeGenerator::Address(); |
| 1285 | } |
| 1286 | GDScriptCodeGenerator::Address member; |
| 1287 | bool is_member = false; |
| 1288 | bool has_setter = false; |
| 1289 | bool is_in_setter = false; |
| 1290 | bool is_static = false; |
| 1291 | GDScriptCodeGenerator::Address static_var_class; |
| 1292 | int static_var_index = 0; |
| 1293 | GDScriptDataType static_var_data_type; |
| 1294 | StringName var_name; |
| 1295 | StringName setter_function; |
| 1296 | var_name = static_cast<const GDScriptParser::IdentifierNode *>(assignment->assignee)->name; |
| 1297 | if (!_is_local_or_parameter(codegen, var_name)) { |
| 1298 | if (codegen.script->member_indices.has(var_name)) { |
| 1299 | is_member = true; |
| 1300 | is_static = false; |
| 1301 | GDScript::MemberInfo &minfo = codegen.script->member_indices[var_name]; |
| 1302 | setter_function = minfo.setter; |
| 1303 | has_setter = setter_function != StringName(); |
| 1304 | is_in_setter = has_setter && setter_function == codegen.function_name; |
| 1305 | member.mode = GDScriptCodeGenerator::Address::MEMBER; |
| 1306 | member.address = minfo.index; |
| 1307 | member.type = minfo.data_type; |
| 1308 | } else { |
| 1309 | // Try static variables. |
| 1310 | GDScript *scr = codegen.script; |
| 1311 | while (scr) { |
| 1312 | if (scr->static_variables_indices.has(var_name)) { |
| 1313 | is_member = true; |
| 1314 | is_static = true; |
| 1315 | GDScript::MemberInfo &minfo = scr->static_variables_indices[var_name]; |
| 1316 | setter_function = minfo.setter; |
| 1317 | has_setter = setter_function != StringName(); |
| 1318 | is_in_setter = has_setter && setter_function == codegen.function_name; |
| 1319 | static_var_class = codegen.add_constant(scr); |
| 1320 | static_var_index = minfo.index; |
| 1321 | static_var_data_type = minfo.data_type; |
| 1322 | break; |
| 1323 | } |
| 1324 | scr = scr->base.ptr(); |
| 1325 | } |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | GDScriptCodeGenerator::Address target; |
| 1330 | if (is_member) { |
| 1331 | target = member; // _parse_expression could call its getter, but we want to know the actual address |
| 1332 | } else { |
| 1333 | target = _parse_expression(codegen, r_error, assignment->assignee); |
| 1334 | if (r_error) { |
| 1335 | return GDScriptCodeGenerator::Address(); |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | GDScriptCodeGenerator::Address assigned_value = _parse_expression(codegen, r_error, assignment->assigned_value); |
| 1340 | if (r_error) { |
| 1341 | return GDScriptCodeGenerator::Address(); |
| 1342 | } |
| 1343 | |
| 1344 | GDScriptCodeGenerator::Address to_assign; |
| 1345 | bool has_operation = assignment->operation != GDScriptParser::AssignmentNode::OP_NONE; |
| 1346 | if (has_operation) { |
| 1347 | // Perform operation. |
| 1348 | GDScriptCodeGenerator::Address op_result = codegen.add_temporary(_gdtype_from_datatype(assignment->get_datatype(), codegen.script)); |
| 1349 | GDScriptCodeGenerator::Address og_value = _parse_expression(codegen, r_error, assignment->assignee); |
| 1350 | gen->write_binary_operator(op_result, assignment->variant_op, og_value, assigned_value); |
| 1351 | to_assign = op_result; |
| 1352 | |
| 1353 | if (og_value.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1354 | gen->pop_temporary(); |
| 1355 | } |
| 1356 | } else { |
| 1357 | to_assign = assigned_value; |
| 1358 | } |
| 1359 | |
| 1360 | if (has_setter && !is_in_setter) { |
| 1361 | // Call setter. |
| 1362 | Vector<GDScriptCodeGenerator::Address> args; |
| 1363 | args.push_back(to_assign); |
| 1364 | GDScriptCodeGenerator::Address call_base = is_static ? GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CLASS) : GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::SELF); |
| 1365 | gen->write_call(GDScriptCodeGenerator::Address(), call_base, setter_function, args); |
| 1366 | } else if (is_static) { |
| 1367 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(static_var_data_type); |
| 1368 | if (assignment->use_conversion_assign) { |
| 1369 | gen->write_assign_with_conversion(temp, to_assign); |
| 1370 | } else { |
| 1371 | gen->write_assign(temp, to_assign); |
| 1372 | } |
| 1373 | gen->write_set_static_variable(temp, static_var_class, static_var_index); |
| 1374 | gen->pop_temporary(); |
| 1375 | } else { |
| 1376 | // Just assign. |
| 1377 | if (assignment->use_conversion_assign) { |
| 1378 | gen->write_assign_with_conversion(target, to_assign); |
| 1379 | } else { |
| 1380 | gen->write_assign(target, to_assign); |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | if (to_assign.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1385 | gen->pop_temporary(); // Pop assigned value or temp operation result. |
| 1386 | } |
| 1387 | if (has_operation && assigned_value.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1388 | gen->pop_temporary(); // Pop assigned value if not done before. |
| 1389 | } |
| 1390 | if (target.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1391 | gen->pop_temporary(); // Pop the target to assignment. |
| 1392 | } |
| 1393 | } |
| 1394 | return GDScriptCodeGenerator::Address(); // Assignment does not return a value. |
| 1395 | } break; |
| 1396 | case GDScriptParser::Node::LAMBDA: { |
| 1397 | const GDScriptParser::LambdaNode *lambda = static_cast<const GDScriptParser::LambdaNode *>(p_expression); |
| 1398 | GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(lambda->get_datatype(), codegen.script)); |
| 1399 | |
| 1400 | Vector<GDScriptCodeGenerator::Address> captures; |
| 1401 | captures.resize(lambda->captures.size()); |
| 1402 | for (int i = 0; i < lambda->captures.size(); i++) { |
| 1403 | captures.write[i] = _parse_expression(codegen, r_error, lambda->captures[i]); |
| 1404 | if (r_error) { |
| 1405 | return GDScriptCodeGenerator::Address(); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | GDScriptFunction *function = _parse_function(r_error, codegen.script, codegen.class_node, lambda->function, false, true); |
| 1410 | if (r_error) { |
| 1411 | return GDScriptCodeGenerator::Address(); |
| 1412 | } |
| 1413 | |
| 1414 | codegen.script->lambda_info.insert(function, { (int)lambda->captures.size(), lambda->use_self }); |
| 1415 | gen->write_lambda(result, function, captures, lambda->use_self); |
| 1416 | |
| 1417 | for (int i = 0; i < captures.size(); i++) { |
| 1418 | if (captures[i].mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1419 | gen->pop_temporary(); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | return result; |
| 1424 | } break; |
| 1425 | default: { |
| 1426 | _set_error("Compiler bug (please report): Unexpected node in parse tree while parsing expression.", p_expression); // Unreachable code. |
| 1427 | r_error = ERR_COMPILATION_FAILED; |
| 1428 | return GDScriptCodeGenerator::Address(); |
| 1429 | } break; |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | GDScriptCodeGenerator::Address GDScriptCompiler::_parse_match_pattern(CodeGen &codegen, Error &r_error, const GDScriptParser::PatternNode *p_pattern, const GDScriptCodeGenerator::Address &p_value_addr, const GDScriptCodeGenerator::Address &p_type_addr, const GDScriptCodeGenerator::Address &p_previous_test, bool p_is_first, bool p_is_nested) { |
| 1434 | switch (p_pattern->pattern_type) { |
| 1435 | case GDScriptParser::PatternNode::PT_LITERAL: { |
| 1436 | if (p_is_nested) { |
| 1437 | codegen.generator->write_and_left_operand(p_previous_test); |
| 1438 | } else if (!p_is_first) { |
| 1439 | codegen.generator->write_or_left_operand(p_previous_test); |
| 1440 | } |
| 1441 | |
| 1442 | // Get literal type into constant map. |
| 1443 | Variant::Type literal_type = p_pattern->literal->value.get_type(); |
| 1444 | GDScriptCodeGenerator::Address literal_type_addr = codegen.add_constant(literal_type); |
| 1445 | |
| 1446 | // Equality is always a boolean. |
| 1447 | GDScriptDataType equality_type; |
| 1448 | equality_type.kind = GDScriptDataType::BUILTIN; |
| 1449 | equality_type.builtin_type = Variant::BOOL; |
| 1450 | |
| 1451 | // Check type equality. |
| 1452 | GDScriptCodeGenerator::Address type_equality_addr = codegen.add_temporary(equality_type); |
| 1453 | codegen.generator->write_binary_operator(type_equality_addr, Variant::OP_EQUAL, p_type_addr, literal_type_addr); |
| 1454 | |
| 1455 | if (literal_type == Variant::STRING) { |
| 1456 | GDScriptCodeGenerator::Address type_stringname_addr = codegen.add_constant(Variant::STRING_NAME); |
| 1457 | |
| 1458 | // Check StringName <-> String type equality. |
| 1459 | GDScriptCodeGenerator::Address tmp_comp_addr = codegen.add_temporary(equality_type); |
| 1460 | |
| 1461 | codegen.generator->write_binary_operator(tmp_comp_addr, Variant::OP_EQUAL, p_type_addr, type_stringname_addr); |
| 1462 | codegen.generator->write_binary_operator(type_equality_addr, Variant::OP_OR, type_equality_addr, tmp_comp_addr); |
| 1463 | |
| 1464 | codegen.generator->pop_temporary(); // Remove tmp_comp_addr from stack. |
| 1465 | } else if (literal_type == Variant::STRING_NAME) { |
| 1466 | GDScriptCodeGenerator::Address type_string_addr = codegen.add_constant(Variant::STRING); |
| 1467 | |
| 1468 | // Check String <-> StringName type equality. |
| 1469 | GDScriptCodeGenerator::Address tmp_comp_addr = codegen.add_temporary(equality_type); |
| 1470 | |
| 1471 | codegen.generator->write_binary_operator(tmp_comp_addr, Variant::OP_EQUAL, p_type_addr, type_string_addr); |
| 1472 | codegen.generator->write_binary_operator(type_equality_addr, Variant::OP_OR, type_equality_addr, tmp_comp_addr); |
| 1473 | |
| 1474 | codegen.generator->pop_temporary(); // Remove tmp_comp_addr from stack. |
| 1475 | } |
| 1476 | |
| 1477 | codegen.generator->write_and_left_operand(type_equality_addr); |
| 1478 | |
| 1479 | // Get literal. |
| 1480 | GDScriptCodeGenerator::Address literal_addr = _parse_expression(codegen, r_error, p_pattern->literal); |
| 1481 | if (r_error) { |
| 1482 | return GDScriptCodeGenerator::Address(); |
| 1483 | } |
| 1484 | |
| 1485 | // Check value equality. |
| 1486 | GDScriptCodeGenerator::Address equality_addr = codegen.add_temporary(equality_type); |
| 1487 | codegen.generator->write_binary_operator(equality_addr, Variant::OP_EQUAL, p_value_addr, literal_addr); |
| 1488 | codegen.generator->write_and_right_operand(equality_addr); |
| 1489 | |
| 1490 | // AND both together (reuse temporary location). |
| 1491 | codegen.generator->write_end_and(type_equality_addr); |
| 1492 | |
| 1493 | codegen.generator->pop_temporary(); // Remove equality_addr from stack. |
| 1494 | |
| 1495 | if (literal_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1496 | codegen.generator->pop_temporary(); |
| 1497 | } |
| 1498 | |
| 1499 | // If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead. |
| 1500 | if (p_is_nested) { |
| 1501 | // Use the previous value as target, since we only need one temporary variable. |
| 1502 | codegen.generator->write_and_right_operand(type_equality_addr); |
| 1503 | codegen.generator->write_end_and(p_previous_test); |
| 1504 | } else if (!p_is_first) { |
| 1505 | // Use the previous value as target, since we only need one temporary variable. |
| 1506 | codegen.generator->write_or_right_operand(type_equality_addr); |
| 1507 | codegen.generator->write_end_or(p_previous_test); |
| 1508 | } else { |
| 1509 | // Just assign this value to the accumulator temporary. |
| 1510 | codegen.generator->write_assign(p_previous_test, type_equality_addr); |
| 1511 | } |
| 1512 | codegen.generator->pop_temporary(); // Remove type_equality_addr. |
| 1513 | |
| 1514 | return p_previous_test; |
| 1515 | } break; |
| 1516 | case GDScriptParser::PatternNode::PT_EXPRESSION: { |
| 1517 | if (p_is_nested) { |
| 1518 | codegen.generator->write_and_left_operand(p_previous_test); |
| 1519 | } else if (!p_is_first) { |
| 1520 | codegen.generator->write_or_left_operand(p_previous_test); |
| 1521 | } |
| 1522 | |
| 1523 | GDScriptCodeGenerator::Address type_string_addr = codegen.add_constant(Variant::STRING); |
| 1524 | GDScriptCodeGenerator::Address type_stringname_addr = codegen.add_constant(Variant::STRING_NAME); |
| 1525 | |
| 1526 | // Equality is always a boolean. |
| 1527 | GDScriptDataType equality_type; |
| 1528 | equality_type.kind = GDScriptDataType::BUILTIN; |
| 1529 | equality_type.builtin_type = Variant::BOOL; |
| 1530 | |
| 1531 | // Create the result temps first since it's the last to go away. |
| 1532 | GDScriptCodeGenerator::Address result_addr = codegen.add_temporary(equality_type); |
| 1533 | GDScriptCodeGenerator::Address equality_test_addr = codegen.add_temporary(equality_type); |
| 1534 | GDScriptCodeGenerator::Address stringy_comp_addr = codegen.add_temporary(equality_type); |
| 1535 | GDScriptCodeGenerator::Address stringy_comp_addr_2 = codegen.add_temporary(equality_type); |
| 1536 | GDScriptCodeGenerator::Address expr_type_addr = codegen.add_temporary(); |
| 1537 | |
| 1538 | // Evaluate expression. |
| 1539 | GDScriptCodeGenerator::Address expr_addr; |
| 1540 | expr_addr = _parse_expression(codegen, r_error, p_pattern->expression); |
| 1541 | if (r_error) { |
| 1542 | return GDScriptCodeGenerator::Address(); |
| 1543 | } |
| 1544 | |
| 1545 | // Evaluate expression type. |
| 1546 | Vector<GDScriptCodeGenerator::Address> typeof_args; |
| 1547 | typeof_args.push_back(expr_addr); |
| 1548 | codegen.generator->write_call_utility(expr_type_addr, "typeof", typeof_args); |
| 1549 | |
| 1550 | // Check type equality. |
| 1551 | codegen.generator->write_binary_operator(result_addr, Variant::OP_EQUAL, p_type_addr, expr_type_addr); |
| 1552 | |
| 1553 | // Check for String <-> StringName comparison. |
| 1554 | codegen.generator->write_binary_operator(stringy_comp_addr, Variant::OP_EQUAL, p_type_addr, type_string_addr); |
| 1555 | codegen.generator->write_binary_operator(stringy_comp_addr_2, Variant::OP_EQUAL, expr_type_addr, type_stringname_addr); |
| 1556 | codegen.generator->write_binary_operator(stringy_comp_addr, Variant::OP_AND, stringy_comp_addr, stringy_comp_addr_2); |
| 1557 | codegen.generator->write_binary_operator(result_addr, Variant::OP_OR, result_addr, stringy_comp_addr); |
| 1558 | |
| 1559 | // Check for StringName <-> String comparison. |
| 1560 | codegen.generator->write_binary_operator(stringy_comp_addr, Variant::OP_EQUAL, p_type_addr, type_stringname_addr); |
| 1561 | codegen.generator->write_binary_operator(stringy_comp_addr_2, Variant::OP_EQUAL, expr_type_addr, type_string_addr); |
| 1562 | codegen.generator->write_binary_operator(stringy_comp_addr, Variant::OP_AND, stringy_comp_addr, stringy_comp_addr_2); |
| 1563 | codegen.generator->write_binary_operator(result_addr, Variant::OP_OR, result_addr, stringy_comp_addr); |
| 1564 | |
| 1565 | codegen.generator->pop_temporary(); // Remove expr_type_addr from stack. |
| 1566 | codegen.generator->pop_temporary(); // Remove stringy_comp_addr_2 from stack. |
| 1567 | codegen.generator->pop_temporary(); // Remove stringy_comp_addr from stack. |
| 1568 | |
| 1569 | codegen.generator->write_and_left_operand(result_addr); |
| 1570 | |
| 1571 | // Check value equality. |
| 1572 | codegen.generator->write_binary_operator(equality_test_addr, Variant::OP_EQUAL, p_value_addr, expr_addr); |
| 1573 | codegen.generator->write_and_right_operand(equality_test_addr); |
| 1574 | |
| 1575 | // AND both type and value equality. |
| 1576 | codegen.generator->write_end_and(result_addr); |
| 1577 | |
| 1578 | // We don't need the expression temporary anymore. |
| 1579 | if (expr_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1580 | codegen.generator->pop_temporary(); |
| 1581 | } |
| 1582 | codegen.generator->pop_temporary(); // Remove equality_test_addr from stack. |
| 1583 | |
| 1584 | // If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead. |
| 1585 | if (p_is_nested) { |
| 1586 | // Use the previous value as target, since we only need one temporary variable. |
| 1587 | codegen.generator->write_and_right_operand(result_addr); |
| 1588 | codegen.generator->write_end_and(p_previous_test); |
| 1589 | } else if (!p_is_first) { |
| 1590 | // Use the previous value as target, since we only need one temporary variable. |
| 1591 | codegen.generator->write_or_right_operand(result_addr); |
| 1592 | codegen.generator->write_end_or(p_previous_test); |
| 1593 | } else { |
| 1594 | // Just assign this value to the accumulator temporary. |
| 1595 | codegen.generator->write_assign(p_previous_test, result_addr); |
| 1596 | } |
| 1597 | codegen.generator->pop_temporary(); // Remove temp result addr. |
| 1598 | |
| 1599 | return p_previous_test; |
| 1600 | } break; |
| 1601 | case GDScriptParser::PatternNode::PT_ARRAY: { |
| 1602 | if (p_is_nested) { |
| 1603 | codegen.generator->write_and_left_operand(p_previous_test); |
| 1604 | } else if (!p_is_first) { |
| 1605 | codegen.generator->write_or_left_operand(p_previous_test); |
| 1606 | } |
| 1607 | // Get array type into constant map. |
| 1608 | GDScriptCodeGenerator::Address array_type_addr = codegen.add_constant((int)Variant::ARRAY); |
| 1609 | |
| 1610 | // Equality is always a boolean. |
| 1611 | GDScriptDataType temp_type; |
| 1612 | temp_type.kind = GDScriptDataType::BUILTIN; |
| 1613 | temp_type.builtin_type = Variant::BOOL; |
| 1614 | |
| 1615 | // Check type equality. |
| 1616 | GDScriptCodeGenerator::Address result_addr = codegen.add_temporary(temp_type); |
| 1617 | codegen.generator->write_binary_operator(result_addr, Variant::OP_EQUAL, p_type_addr, array_type_addr); |
| 1618 | codegen.generator->write_and_left_operand(result_addr); |
| 1619 | |
| 1620 | // Store pattern length in constant map. |
| 1621 | GDScriptCodeGenerator::Address array_length_addr = codegen.add_constant(p_pattern->rest_used ? p_pattern->array.size() - 1 : p_pattern->array.size()); |
| 1622 | |
| 1623 | // Get value length. |
| 1624 | temp_type.builtin_type = Variant::INT; |
| 1625 | GDScriptCodeGenerator::Address value_length_addr = codegen.add_temporary(temp_type); |
| 1626 | Vector<GDScriptCodeGenerator::Address> len_args; |
| 1627 | len_args.push_back(p_value_addr); |
| 1628 | codegen.generator->write_call_gdscript_utility(value_length_addr, "len", len_args); |
| 1629 | |
| 1630 | // Test length compatibility. |
| 1631 | temp_type.builtin_type = Variant::BOOL; |
| 1632 | GDScriptCodeGenerator::Address length_compat_addr = codegen.add_temporary(temp_type); |
| 1633 | codegen.generator->write_binary_operator(length_compat_addr, p_pattern->rest_used ? Variant::OP_GREATER_EQUAL : Variant::OP_EQUAL, value_length_addr, array_length_addr); |
| 1634 | codegen.generator->write_and_right_operand(length_compat_addr); |
| 1635 | |
| 1636 | // AND type and length check. |
| 1637 | codegen.generator->write_end_and(result_addr); |
| 1638 | |
| 1639 | // Remove length temporaries. |
| 1640 | codegen.generator->pop_temporary(); |
| 1641 | codegen.generator->pop_temporary(); |
| 1642 | |
| 1643 | // Create temporaries outside the loop so they can be reused. |
| 1644 | GDScriptCodeGenerator::Address element_addr = codegen.add_temporary(); |
| 1645 | GDScriptCodeGenerator::Address element_type_addr = codegen.add_temporary(); |
| 1646 | |
| 1647 | // Evaluate element by element. |
| 1648 | for (int i = 0; i < p_pattern->array.size(); i++) { |
| 1649 | if (p_pattern->array[i]->pattern_type == GDScriptParser::PatternNode::PT_REST) { |
| 1650 | // Don't want to access an extra element of the user array. |
| 1651 | break; |
| 1652 | } |
| 1653 | |
| 1654 | // Use AND here too, as we don't want to be checking elements if previous test failed (which means this might be an invalid get). |
| 1655 | codegen.generator->write_and_left_operand(result_addr); |
| 1656 | |
| 1657 | // Add index to constant map. |
| 1658 | GDScriptCodeGenerator::Address index_addr = codegen.add_constant(i); |
| 1659 | |
| 1660 | // Get the actual element from the user-sent array. |
| 1661 | codegen.generator->write_get(element_addr, index_addr, p_value_addr); |
| 1662 | |
| 1663 | // Also get type of element. |
| 1664 | Vector<GDScriptCodeGenerator::Address> typeof_args; |
| 1665 | typeof_args.push_back(element_addr); |
| 1666 | codegen.generator->write_call_utility(element_type_addr, "typeof", typeof_args); |
| 1667 | |
| 1668 | // Try the pattern inside the element. |
| 1669 | result_addr = _parse_match_pattern(codegen, r_error, p_pattern->array[i], element_addr, element_type_addr, result_addr, false, true); |
| 1670 | if (r_error != OK) { |
| 1671 | return GDScriptCodeGenerator::Address(); |
| 1672 | } |
| 1673 | |
| 1674 | codegen.generator->write_and_right_operand(result_addr); |
| 1675 | codegen.generator->write_end_and(result_addr); |
| 1676 | } |
| 1677 | // Remove element temporaries. |
| 1678 | codegen.generator->pop_temporary(); |
| 1679 | codegen.generator->pop_temporary(); |
| 1680 | |
| 1681 | // If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead. |
| 1682 | if (p_is_nested) { |
| 1683 | // Use the previous value as target, since we only need one temporary variable. |
| 1684 | codegen.generator->write_and_right_operand(result_addr); |
| 1685 | codegen.generator->write_end_and(p_previous_test); |
| 1686 | } else if (!p_is_first) { |
| 1687 | // Use the previous value as target, since we only need one temporary variable. |
| 1688 | codegen.generator->write_or_right_operand(result_addr); |
| 1689 | codegen.generator->write_end_or(p_previous_test); |
| 1690 | } else { |
| 1691 | // Just assign this value to the accumulator temporary. |
| 1692 | codegen.generator->write_assign(p_previous_test, result_addr); |
| 1693 | } |
| 1694 | codegen.generator->pop_temporary(); // Remove temp result addr. |
| 1695 | |
| 1696 | return p_previous_test; |
| 1697 | } break; |
| 1698 | case GDScriptParser::PatternNode::PT_DICTIONARY: { |
| 1699 | if (p_is_nested) { |
| 1700 | codegen.generator->write_and_left_operand(p_previous_test); |
| 1701 | } else if (!p_is_first) { |
| 1702 | codegen.generator->write_or_left_operand(p_previous_test); |
| 1703 | } |
| 1704 | // Get dictionary type into constant map. |
| 1705 | GDScriptCodeGenerator::Address dict_type_addr = codegen.add_constant((int)Variant::DICTIONARY); |
| 1706 | |
| 1707 | // Equality is always a boolean. |
| 1708 | GDScriptDataType temp_type; |
| 1709 | temp_type.kind = GDScriptDataType::BUILTIN; |
| 1710 | temp_type.builtin_type = Variant::BOOL; |
| 1711 | |
| 1712 | // Check type equality. |
| 1713 | GDScriptCodeGenerator::Address result_addr = codegen.add_temporary(temp_type); |
| 1714 | codegen.generator->write_binary_operator(result_addr, Variant::OP_EQUAL, p_type_addr, dict_type_addr); |
| 1715 | codegen.generator->write_and_left_operand(result_addr); |
| 1716 | |
| 1717 | // Store pattern length in constant map. |
| 1718 | GDScriptCodeGenerator::Address dict_length_addr = codegen.add_constant(p_pattern->rest_used ? p_pattern->dictionary.size() - 1 : p_pattern->dictionary.size()); |
| 1719 | |
| 1720 | // Get user's dictionary length. |
| 1721 | temp_type.builtin_type = Variant::INT; |
| 1722 | GDScriptCodeGenerator::Address value_length_addr = codegen.add_temporary(temp_type); |
| 1723 | Vector<GDScriptCodeGenerator::Address> func_args; |
| 1724 | func_args.push_back(p_value_addr); |
| 1725 | codegen.generator->write_call_gdscript_utility(value_length_addr, "len", func_args); |
| 1726 | |
| 1727 | // Test length compatibility. |
| 1728 | temp_type.builtin_type = Variant::BOOL; |
| 1729 | GDScriptCodeGenerator::Address length_compat_addr = codegen.add_temporary(temp_type); |
| 1730 | codegen.generator->write_binary_operator(length_compat_addr, p_pattern->rest_used ? Variant::OP_GREATER_EQUAL : Variant::OP_EQUAL, value_length_addr, dict_length_addr); |
| 1731 | codegen.generator->write_and_right_operand(length_compat_addr); |
| 1732 | |
| 1733 | // AND type and length check. |
| 1734 | codegen.generator->write_end_and(result_addr); |
| 1735 | |
| 1736 | // Remove length temporaries. |
| 1737 | codegen.generator->pop_temporary(); |
| 1738 | codegen.generator->pop_temporary(); |
| 1739 | |
| 1740 | // Create temporaries outside the loop so they can be reused. |
| 1741 | GDScriptCodeGenerator::Address element_addr = codegen.add_temporary(); |
| 1742 | GDScriptCodeGenerator::Address element_type_addr = codegen.add_temporary(); |
| 1743 | |
| 1744 | // Evaluate element by element. |
| 1745 | for (int i = 0; i < p_pattern->dictionary.size(); i++) { |
| 1746 | const GDScriptParser::PatternNode::Pair &element = p_pattern->dictionary[i]; |
| 1747 | if (element.value_pattern && element.value_pattern->pattern_type == GDScriptParser::PatternNode::PT_REST) { |
| 1748 | // Ignore rest pattern. |
| 1749 | break; |
| 1750 | } |
| 1751 | |
| 1752 | // Use AND here too, as we don't want to be checking elements if previous test failed (which means this might be an invalid get). |
| 1753 | codegen.generator->write_and_left_operand(result_addr); |
| 1754 | |
| 1755 | // Get the pattern key. |
| 1756 | GDScriptCodeGenerator::Address pattern_key_addr = _parse_expression(codegen, r_error, element.key); |
| 1757 | if (r_error) { |
| 1758 | return GDScriptCodeGenerator::Address(); |
| 1759 | } |
| 1760 | |
| 1761 | // Check if pattern key exists in user's dictionary. This will be AND-ed with next result. |
| 1762 | func_args.clear(); |
| 1763 | func_args.push_back(pattern_key_addr); |
| 1764 | codegen.generator->write_call(result_addr, p_value_addr, "has", func_args); |
| 1765 | |
| 1766 | if (element.value_pattern != nullptr) { |
| 1767 | // Use AND here too, as we don't want to be checking elements if previous test failed (which means this might be an invalid get). |
| 1768 | codegen.generator->write_and_left_operand(result_addr); |
| 1769 | |
| 1770 | // Get actual value from user dictionary. |
| 1771 | codegen.generator->write_get(element_addr, pattern_key_addr, p_value_addr); |
| 1772 | |
| 1773 | // Also get type of value. |
| 1774 | func_args.clear(); |
| 1775 | func_args.push_back(element_addr); |
| 1776 | codegen.generator->write_call_utility(element_type_addr, "typeof", func_args); |
| 1777 | |
| 1778 | // Try the pattern inside the value. |
| 1779 | result_addr = _parse_match_pattern(codegen, r_error, element.value_pattern, element_addr, element_type_addr, result_addr, false, true); |
| 1780 | if (r_error != OK) { |
| 1781 | return GDScriptCodeGenerator::Address(); |
| 1782 | } |
| 1783 | codegen.generator->write_and_right_operand(result_addr); |
| 1784 | codegen.generator->write_end_and(result_addr); |
| 1785 | } |
| 1786 | |
| 1787 | codegen.generator->write_and_right_operand(result_addr); |
| 1788 | codegen.generator->write_end_and(result_addr); |
| 1789 | |
| 1790 | // Remove pattern key temporary. |
| 1791 | if (pattern_key_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1792 | codegen.generator->pop_temporary(); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | // Remove element temporaries. |
| 1797 | codegen.generator->pop_temporary(); |
| 1798 | codegen.generator->pop_temporary(); |
| 1799 | |
| 1800 | // If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead. |
| 1801 | if (p_is_nested) { |
| 1802 | // Use the previous value as target, since we only need one temporary variable. |
| 1803 | codegen.generator->write_and_right_operand(result_addr); |
| 1804 | codegen.generator->write_end_and(p_previous_test); |
| 1805 | } else if (!p_is_first) { |
| 1806 | // Use the previous value as target, since we only need one temporary variable. |
| 1807 | codegen.generator->write_or_right_operand(result_addr); |
| 1808 | codegen.generator->write_end_or(p_previous_test); |
| 1809 | } else { |
| 1810 | // Just assign this value to the accumulator temporary. |
| 1811 | codegen.generator->write_assign(p_previous_test, result_addr); |
| 1812 | } |
| 1813 | codegen.generator->pop_temporary(); // Remove temp result addr. |
| 1814 | |
| 1815 | return p_previous_test; |
| 1816 | } break; |
| 1817 | case GDScriptParser::PatternNode::PT_REST: |
| 1818 | // Do nothing. |
| 1819 | return p_previous_test; |
| 1820 | break; |
| 1821 | case GDScriptParser::PatternNode::PT_BIND: { |
| 1822 | if (p_is_nested) { |
| 1823 | codegen.generator->write_and_left_operand(p_previous_test); |
| 1824 | } else if (!p_is_first) { |
| 1825 | codegen.generator->write_or_left_operand(p_previous_test); |
| 1826 | } |
| 1827 | // Get the bind address. |
| 1828 | GDScriptCodeGenerator::Address bind = codegen.locals[p_pattern->bind->name]; |
| 1829 | |
| 1830 | // Assign value to bound variable. |
| 1831 | codegen.generator->write_assign(bind, p_value_addr); |
| 1832 | } |
| 1833 | [[fallthrough]]; // Act like matching anything too. |
| 1834 | case GDScriptParser::PatternNode::PT_WILDCARD: |
| 1835 | // If this is a fall through we don't want to do this again. |
| 1836 | if (p_pattern->pattern_type != GDScriptParser::PatternNode::PT_BIND) { |
| 1837 | if (p_is_nested) { |
| 1838 | codegen.generator->write_and_left_operand(p_previous_test); |
| 1839 | } else if (!p_is_first) { |
| 1840 | codegen.generator->write_or_left_operand(p_previous_test); |
| 1841 | } |
| 1842 | } |
| 1843 | // This matches anything so just do the same as `if(true)`. |
| 1844 | // If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead. |
| 1845 | if (p_is_nested) { |
| 1846 | // Use the operator with the `true` constant so it works as always matching. |
| 1847 | GDScriptCodeGenerator::Address constant = codegen.add_constant(true); |
| 1848 | codegen.generator->write_and_right_operand(constant); |
| 1849 | codegen.generator->write_end_and(p_previous_test); |
| 1850 | } else if (!p_is_first) { |
| 1851 | // Use the operator with the `true` constant so it works as always matching. |
| 1852 | GDScriptCodeGenerator::Address constant = codegen.add_constant(true); |
| 1853 | codegen.generator->write_or_right_operand(constant); |
| 1854 | codegen.generator->write_end_or(p_previous_test); |
| 1855 | } else { |
| 1856 | // Just assign this value to the accumulator temporary. |
| 1857 | codegen.generator->write_assign_true(p_previous_test); |
| 1858 | } |
| 1859 | return p_previous_test; |
| 1860 | } |
| 1861 | |
| 1862 | _set_error("Compiler bug (please report): Reaching the end of pattern compilation without matching a pattern.", p_pattern); |
| 1863 | r_error = ERR_COMPILATION_FAILED; |
| 1864 | return p_previous_test; |
| 1865 | } |
| 1866 | |
| 1867 | List<GDScriptCodeGenerator::Address> GDScriptCompiler::_add_block_locals(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block) { |
| 1868 | List<GDScriptCodeGenerator::Address> addresses; |
| 1869 | for (int i = 0; i < p_block->locals.size(); i++) { |
| 1870 | if (p_block->locals[i].type == GDScriptParser::SuiteNode::Local::PARAMETER || p_block->locals[i].type == GDScriptParser::SuiteNode::Local::FOR_VARIABLE) { |
| 1871 | // Parameters are added directly from function and loop variables are declared explicitly. |
| 1872 | continue; |
| 1873 | } |
| 1874 | addresses.push_back(codegen.add_local(p_block->locals[i].name, _gdtype_from_datatype(p_block->locals[i].get_datatype(), codegen.script))); |
| 1875 | } |
| 1876 | return addresses; |
| 1877 | } |
| 1878 | |
| 1879 | // Avoid keeping in the stack long-lived references to objects, which may prevent `RefCounted` objects from being freed. |
| 1880 | void GDScriptCompiler::_clear_block_locals(CodeGen &codegen, const List<GDScriptCodeGenerator::Address> &p_locals) { |
| 1881 | for (const GDScriptCodeGenerator::Address &local : p_locals) { |
| 1882 | if (local.type.can_contain_object()) { |
| 1883 | codegen.generator->clear_address(local); |
| 1884 | } |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, bool p_add_locals, bool p_clear_locals) { |
| 1889 | Error err = OK; |
| 1890 | GDScriptCodeGenerator *gen = codegen.generator; |
| 1891 | List<GDScriptCodeGenerator::Address> block_locals; |
| 1892 | |
| 1893 | gen->clear_temporaries(); |
| 1894 | codegen.start_block(); |
| 1895 | |
| 1896 | if (p_add_locals) { |
| 1897 | block_locals = _add_block_locals(codegen, p_block); |
| 1898 | } |
| 1899 | |
| 1900 | for (int i = 0; i < p_block->statements.size(); i++) { |
| 1901 | const GDScriptParser::Node *s = p_block->statements[i]; |
| 1902 | |
| 1903 | gen->write_newline(s->start_line); |
| 1904 | |
| 1905 | switch (s->type) { |
| 1906 | case GDScriptParser::Node::MATCH: { |
| 1907 | const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(s); |
| 1908 | |
| 1909 | codegen.start_block(); // Add an extra block, since @special locals belong to the match scope. |
| 1910 | |
| 1911 | // Evaluate the match expression. |
| 1912 | GDScriptCodeGenerator::Address value = codegen.add_local("@match_value", _gdtype_from_datatype(match->test->get_datatype(), codegen.script)); |
| 1913 | GDScriptCodeGenerator::Address value_expr = _parse_expression(codegen, err, match->test); |
| 1914 | if (err) { |
| 1915 | return err; |
| 1916 | } |
| 1917 | |
| 1918 | // Assign to local. |
| 1919 | // TODO: This can be improved by passing the target to parse_expression(). |
| 1920 | gen->write_assign(value, value_expr); |
| 1921 | |
| 1922 | if (value_expr.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1923 | codegen.generator->pop_temporary(); |
| 1924 | } |
| 1925 | |
| 1926 | // Then, let's save the type of the value in the stack too, so we can reuse for later comparisons. |
| 1927 | GDScriptDataType typeof_type; |
| 1928 | typeof_type.kind = GDScriptDataType::BUILTIN; |
| 1929 | typeof_type.builtin_type = Variant::INT; |
| 1930 | GDScriptCodeGenerator::Address type = codegen.add_local("@match_type", typeof_type); |
| 1931 | |
| 1932 | Vector<GDScriptCodeGenerator::Address> typeof_args; |
| 1933 | typeof_args.push_back(value); |
| 1934 | gen->write_call_utility(type, "typeof", typeof_args); |
| 1935 | |
| 1936 | // Now we can actually start testing. |
| 1937 | // For each branch. |
| 1938 | for (int j = 0; j < match->branches.size(); j++) { |
| 1939 | if (j > 0) { |
| 1940 | // Use `else` to not check the next branch after matching. |
| 1941 | gen->write_else(); |
| 1942 | } |
| 1943 | |
| 1944 | const GDScriptParser::MatchBranchNode *branch = match->branches[j]; |
| 1945 | |
| 1946 | codegen.start_block(); // Add an extra block, since binds belong to the match branch scope. |
| 1947 | |
| 1948 | // Add locals in block before patterns, so temporaries don't use the stack address for binds. |
| 1949 | List<GDScriptCodeGenerator::Address> branch_locals = _add_block_locals(codegen, branch->block); |
| 1950 | |
| 1951 | gen->write_newline(branch->start_line); |
| 1952 | |
| 1953 | // For each pattern in branch. |
| 1954 | GDScriptCodeGenerator::Address pattern_result = codegen.add_temporary(); |
| 1955 | for (int k = 0; k < branch->patterns.size(); k++) { |
| 1956 | pattern_result = _parse_match_pattern(codegen, err, branch->patterns[k], value, type, pattern_result, k == 0, false); |
| 1957 | if (err != OK) { |
| 1958 | return err; |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | // If there's a guard, check its condition too. |
| 1963 | if (branch->guard_body != nullptr) { |
| 1964 | // Do this first so the guard does not run unless the pattern matched. |
| 1965 | gen->write_and_left_operand(pattern_result); |
| 1966 | |
| 1967 | // Don't actually use the block for the guard. |
| 1968 | // The binds are already in the locals and we don't want to clear the result of the guard condition before we check the actual match. |
| 1969 | GDScriptCodeGenerator::Address guard_result = _parse_expression(codegen, err, static_cast<GDScriptParser::ExpressionNode *>(branch->guard_body->statements[0])); |
| 1970 | if (err) { |
| 1971 | return err; |
| 1972 | } |
| 1973 | |
| 1974 | gen->write_and_right_operand(guard_result); |
| 1975 | gen->write_end_and(pattern_result); |
| 1976 | |
| 1977 | if (guard_result.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 1978 | codegen.generator->pop_temporary(); |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | // Check if pattern did match. |
| 1983 | gen->write_if(pattern_result); |
| 1984 | |
| 1985 | // Remove the result from stack. |
| 1986 | gen->pop_temporary(); |
| 1987 | |
| 1988 | // Parse the branch block. |
| 1989 | err = _parse_block(codegen, branch->block, false); // Don't add locals again. |
| 1990 | if (err) { |
| 1991 | return err; |
| 1992 | } |
| 1993 | |
| 1994 | _clear_block_locals(codegen, branch_locals); |
| 1995 | |
| 1996 | codegen.end_block(); // Get out of extra block for binds. |
| 1997 | } |
| 1998 | |
| 1999 | // End all nested `if`s. |
| 2000 | for (int j = 0; j < match->branches.size(); j++) { |
| 2001 | gen->write_endif(); |
| 2002 | } |
| 2003 | |
| 2004 | codegen.end_block(); // Get out of extra block for match's @special locals. |
| 2005 | } break; |
| 2006 | case GDScriptParser::Node::IF: { |
| 2007 | const GDScriptParser::IfNode *if_n = static_cast<const GDScriptParser::IfNode *>(s); |
| 2008 | GDScriptCodeGenerator::Address condition = _parse_expression(codegen, err, if_n->condition); |
| 2009 | if (err) { |
| 2010 | return err; |
| 2011 | } |
| 2012 | |
| 2013 | gen->write_if(condition); |
| 2014 | |
| 2015 | if (condition.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2016 | codegen.generator->pop_temporary(); |
| 2017 | } |
| 2018 | |
| 2019 | err = _parse_block(codegen, if_n->true_block); |
| 2020 | if (err) { |
| 2021 | return err; |
| 2022 | } |
| 2023 | |
| 2024 | if (if_n->false_block) { |
| 2025 | gen->write_else(); |
| 2026 | |
| 2027 | err = _parse_block(codegen, if_n->false_block); |
| 2028 | if (err) { |
| 2029 | return err; |
| 2030 | } |
| 2031 | } |
| 2032 | |
| 2033 | gen->write_endif(); |
| 2034 | } break; |
| 2035 | case GDScriptParser::Node::FOR: { |
| 2036 | const GDScriptParser::ForNode *for_n = static_cast<const GDScriptParser::ForNode *>(s); |
| 2037 | |
| 2038 | // Add an extra block, since the iterator and @special locals belong to the loop scope. |
| 2039 | // Also we use custom logic to clear block locals. |
| 2040 | codegen.start_block(); |
| 2041 | |
| 2042 | GDScriptCodeGenerator::Address iterator = codegen.add_local(for_n->variable->name, _gdtype_from_datatype(for_n->variable->get_datatype(), codegen.script)); |
| 2043 | |
| 2044 | // Optimize `range()` call to not allocate an array. |
| 2045 | GDScriptParser::CallNode *range_call = nullptr; |
| 2046 | if (for_n->list && for_n->list->type == GDScriptParser::Node::CALL) { |
| 2047 | GDScriptParser::CallNode *call = static_cast<GDScriptParser::CallNode *>(for_n->list); |
| 2048 | if (call->get_callee_type() == GDScriptParser::Node::IDENTIFIER) { |
| 2049 | if (static_cast<GDScriptParser::IdentifierNode *>(call->callee)->name == "range") { |
| 2050 | range_call = call; |
| 2051 | } |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | gen->start_for(iterator.type, _gdtype_from_datatype(for_n->list->get_datatype(), codegen.script), range_call != nullptr); |
| 2056 | |
| 2057 | if (range_call != nullptr) { |
| 2058 | Vector<GDScriptCodeGenerator::Address> args; |
| 2059 | args.resize(range_call->arguments.size()); |
| 2060 | |
| 2061 | for (int j = 0; j < args.size(); j++) { |
| 2062 | args.write[j] = _parse_expression(codegen, err, range_call->arguments[j]); |
| 2063 | if (err) { |
| 2064 | return err; |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | switch (args.size()) { |
| 2069 | case 1: |
| 2070 | gen->write_for_range_assignment(codegen.add_constant(0), args[0], codegen.add_constant(1)); |
| 2071 | break; |
| 2072 | case 2: |
| 2073 | gen->write_for_range_assignment(args[0], args[1], codegen.add_constant(1)); |
| 2074 | break; |
| 2075 | case 3: |
| 2076 | gen->write_for_range_assignment(args[0], args[1], args[2]); |
| 2077 | break; |
| 2078 | default: |
| 2079 | _set_error(R"*(Analyzer bug: Wrong "range()" argument count.)*", range_call); |
| 2080 | return ERR_BUG; |
| 2081 | } |
| 2082 | |
| 2083 | for (int j = 0; j < args.size(); j++) { |
| 2084 | if (args[j].mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2085 | codegen.generator->pop_temporary(); |
| 2086 | } |
| 2087 | } |
| 2088 | } else { |
| 2089 | GDScriptCodeGenerator::Address list = _parse_expression(codegen, err, for_n->list); |
| 2090 | if (err) { |
| 2091 | return err; |
| 2092 | } |
| 2093 | |
| 2094 | gen->write_for_list_assignment(list); |
| 2095 | |
| 2096 | if (list.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2097 | codegen.generator->pop_temporary(); |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | gen->write_for(iterator, for_n->use_conversion_assign, range_call != nullptr); |
| 2102 | |
| 2103 | // Loop variables must be cleared even when `break`/`continue` is used. |
| 2104 | List<GDScriptCodeGenerator::Address> loop_locals = _add_block_locals(codegen, for_n->loop); |
| 2105 | |
| 2106 | //_clear_block_locals(codegen, loop_locals); // Inside loop, before block - for `continue`. // TODO |
| 2107 | |
| 2108 | err = _parse_block(codegen, for_n->loop, false); // Don't add locals again. |
| 2109 | if (err) { |
| 2110 | return err; |
| 2111 | } |
| 2112 | |
| 2113 | gen->write_endfor(range_call != nullptr); |
| 2114 | |
| 2115 | _clear_block_locals(codegen, loop_locals); // Outside loop, after block - for `break` and normal exit. |
| 2116 | |
| 2117 | codegen.end_block(); // Get out of extra block for loop iterator, @special locals, and custom locals clearing. |
| 2118 | } break; |
| 2119 | case GDScriptParser::Node::WHILE: { |
| 2120 | const GDScriptParser::WhileNode *while_n = static_cast<const GDScriptParser::WhileNode *>(s); |
| 2121 | |
| 2122 | codegen.start_block(); // Add an extra block, since we use custom logic to clear block locals. |
| 2123 | |
| 2124 | gen->start_while_condition(); |
| 2125 | |
| 2126 | GDScriptCodeGenerator::Address condition = _parse_expression(codegen, err, while_n->condition); |
| 2127 | if (err) { |
| 2128 | return err; |
| 2129 | } |
| 2130 | |
| 2131 | gen->write_while(condition); |
| 2132 | |
| 2133 | if (condition.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2134 | codegen.generator->pop_temporary(); |
| 2135 | } |
| 2136 | |
| 2137 | // Loop variables must be cleared even when `break`/`continue` is used. |
| 2138 | List<GDScriptCodeGenerator::Address> loop_locals = _add_block_locals(codegen, while_n->loop); |
| 2139 | |
| 2140 | //_clear_block_locals(codegen, loop_locals); // Inside loop, before block - for `continue`. // TODO |
| 2141 | |
| 2142 | err = _parse_block(codegen, while_n->loop, false); // Don't add locals again. |
| 2143 | if (err) { |
| 2144 | return err; |
| 2145 | } |
| 2146 | |
| 2147 | gen->write_endwhile(); |
| 2148 | |
| 2149 | _clear_block_locals(codegen, loop_locals); // Outside loop, after block - for `break` and normal exit. |
| 2150 | |
| 2151 | codegen.end_block(); // Get out of extra block for custom locals clearing. |
| 2152 | } break; |
| 2153 | case GDScriptParser::Node::BREAK: { |
| 2154 | gen->write_break(); |
| 2155 | } break; |
| 2156 | case GDScriptParser::Node::CONTINUE: { |
| 2157 | gen->write_continue(); |
| 2158 | } break; |
| 2159 | case GDScriptParser::Node::RETURN: { |
| 2160 | const GDScriptParser::ReturnNode *return_n = static_cast<const GDScriptParser::ReturnNode *>(s); |
| 2161 | |
| 2162 | GDScriptCodeGenerator::Address return_value; |
| 2163 | |
| 2164 | if (return_n->return_value != nullptr) { |
| 2165 | return_value = _parse_expression(codegen, err, return_n->return_value); |
| 2166 | if (err) { |
| 2167 | return err; |
| 2168 | } |
| 2169 | } |
| 2170 | |
| 2171 | if (return_n->void_return) { |
| 2172 | // Always return "null", even if the expression is a call to a void function. |
| 2173 | gen->write_return(codegen.add_constant(Variant())); |
| 2174 | } else { |
| 2175 | gen->write_return(return_value); |
| 2176 | } |
| 2177 | if (return_value.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2178 | codegen.generator->pop_temporary(); |
| 2179 | } |
| 2180 | } break; |
| 2181 | case GDScriptParser::Node::ASSERT: { |
| 2182 | #ifdef DEBUG_ENABLED |
| 2183 | const GDScriptParser::AssertNode *as = static_cast<const GDScriptParser::AssertNode *>(s); |
| 2184 | |
| 2185 | GDScriptCodeGenerator::Address condition = _parse_expression(codegen, err, as->condition); |
| 2186 | if (err) { |
| 2187 | return err; |
| 2188 | } |
| 2189 | |
| 2190 | GDScriptCodeGenerator::Address message; |
| 2191 | |
| 2192 | if (as->message) { |
| 2193 | message = _parse_expression(codegen, err, as->message); |
| 2194 | if (err) { |
| 2195 | return err; |
| 2196 | } |
| 2197 | } |
| 2198 | gen->write_assert(condition, message); |
| 2199 | |
| 2200 | if (condition.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2201 | codegen.generator->pop_temporary(); |
| 2202 | } |
| 2203 | if (message.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2204 | codegen.generator->pop_temporary(); |
| 2205 | } |
| 2206 | #endif |
| 2207 | } break; |
| 2208 | case GDScriptParser::Node::BREAKPOINT: { |
| 2209 | #ifdef DEBUG_ENABLED |
| 2210 | gen->write_breakpoint(); |
| 2211 | #endif |
| 2212 | } break; |
| 2213 | case GDScriptParser::Node::VARIABLE: { |
| 2214 | const GDScriptParser::VariableNode *lv = static_cast<const GDScriptParser::VariableNode *>(s); |
| 2215 | // Should be already in stack when the block began. |
| 2216 | GDScriptCodeGenerator::Address local = codegen.locals[lv->identifier->name]; |
| 2217 | GDScriptDataType local_type = _gdtype_from_datatype(lv->get_datatype(), codegen.script); |
| 2218 | |
| 2219 | bool initialized = false; |
| 2220 | if (lv->initializer != nullptr) { |
| 2221 | GDScriptCodeGenerator::Address src_address = _parse_expression(codegen, err, lv->initializer); |
| 2222 | if (err) { |
| 2223 | return err; |
| 2224 | } |
| 2225 | if (lv->use_conversion_assign) { |
| 2226 | gen->write_assign_with_conversion(local, src_address); |
| 2227 | } else { |
| 2228 | gen->write_assign(local, src_address); |
| 2229 | } |
| 2230 | if (src_address.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2231 | codegen.generator->pop_temporary(); |
| 2232 | } |
| 2233 | initialized = true; |
| 2234 | } else if (local_type.kind == GDScriptDataType::BUILTIN || codegen.generator->is_local_dirty(local)) { |
| 2235 | // Initialize with default for the type. Built-in types must always be cleared (they cannot be `null`). |
| 2236 | // Objects and untyped variables are assigned to `null` only if the stack address has been reused and not cleared. |
| 2237 | codegen.generator->clear_address(local); |
| 2238 | initialized = true; |
| 2239 | } |
| 2240 | |
| 2241 | // Don't check `is_local_dirty()` since the variable must be assigned to `null` **on each iteration**. |
| 2242 | if (!initialized && p_block->is_in_loop) { |
| 2243 | codegen.generator->clear_address(local); |
| 2244 | } |
| 2245 | } break; |
| 2246 | case GDScriptParser::Node::CONSTANT: { |
| 2247 | // Local constants. |
| 2248 | const GDScriptParser::ConstantNode *lc = static_cast<const GDScriptParser::ConstantNode *>(s); |
| 2249 | if (!lc->initializer->is_constant) { |
| 2250 | _set_error("Local constant must have a constant value as initializer.", lc->initializer); |
| 2251 | return ERR_PARSE_ERROR; |
| 2252 | } |
| 2253 | |
| 2254 | codegen.add_local_constant(lc->identifier->name, lc->initializer->reduced_value); |
| 2255 | } break; |
| 2256 | case GDScriptParser::Node::PASS: |
| 2257 | // Nothing to do. |
| 2258 | break; |
| 2259 | default: { |
| 2260 | // Expression. |
| 2261 | if (s->is_expression()) { |
| 2262 | GDScriptCodeGenerator::Address expr = _parse_expression(codegen, err, static_cast<const GDScriptParser::ExpressionNode *>(s), true); |
| 2263 | if (err) { |
| 2264 | return err; |
| 2265 | } |
| 2266 | if (expr.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2267 | codegen.generator->pop_temporary(); |
| 2268 | } |
| 2269 | } else { |
| 2270 | _set_error("Compiler bug (please report): unexpected node in parse tree while parsing statement.", s); // Unreachable code. |
| 2271 | return ERR_INVALID_DATA; |
| 2272 | } |
| 2273 | } break; |
| 2274 | } |
| 2275 | |
| 2276 | gen->clear_temporaries(); |
| 2277 | } |
| 2278 | |
| 2279 | if (p_add_locals && p_clear_locals) { |
| 2280 | _clear_block_locals(codegen, block_locals); |
| 2281 | } |
| 2282 | |
| 2283 | codegen.end_block(); |
| 2284 | return OK; |
| 2285 | } |
| 2286 | |
| 2287 | GDScriptFunction *GDScriptCompiler::_parse_function(Error &r_error, GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready, bool p_for_lambda) { |
| 2288 | r_error = OK; |
| 2289 | CodeGen codegen; |
| 2290 | codegen.generator = memnew(GDScriptByteCodeGenerator); |
| 2291 | |
| 2292 | codegen.class_node = p_class; |
| 2293 | codegen.script = p_script; |
| 2294 | codegen.function_node = p_func; |
| 2295 | |
| 2296 | StringName func_name; |
| 2297 | bool is_abstract = false; |
| 2298 | bool is_static = false; |
| 2299 | Variant rpc_config; |
| 2300 | GDScriptDataType return_type; |
| 2301 | return_type.kind = GDScriptDataType::BUILTIN; |
| 2302 | return_type.builtin_type = Variant::NIL; |
| 2303 | |
| 2304 | if (p_func) { |
| 2305 | if (p_func->identifier) { |
| 2306 | func_name = p_func->identifier->name; |
| 2307 | } else { |
| 2308 | func_name = "<anonymous lambda>"; |
| 2309 | } |
| 2310 | is_abstract = p_func->is_abstract; |
| 2311 | is_static = p_func->is_static; |
| 2312 | rpc_config = p_func->rpc_config; |
| 2313 | return_type = _gdtype_from_datatype(p_func->get_datatype(), p_script); |
| 2314 | } else { |
| 2315 | if (p_for_ready) { |
| 2316 | func_name = "@implicit_ready"; |
| 2317 | } else { |
| 2318 | func_name = "@implicit_new"; |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | MethodInfo method_info; |
| 2323 | |
| 2324 | codegen.function_name = func_name; |
| 2325 | method_info.name = func_name; |
| 2326 | codegen.is_static = is_static; |
| 2327 | if (is_abstract) { |
| 2328 | method_info.flags |= METHOD_FLAG_VIRTUAL_REQUIRED; |
| 2329 | } |
| 2330 | if (is_static) { |
| 2331 | method_info.flags |= METHOD_FLAG_STATIC; |
| 2332 | } |
| 2333 | codegen.generator->write_start(p_script, func_name, is_static, rpc_config, return_type); |
| 2334 | |
| 2335 | int optional_parameters = 0; |
| 2336 | GDScriptCodeGenerator::Address vararg_addr; |
| 2337 | |
| 2338 | if (p_func) { |
| 2339 | for (int i = 0; i < p_func->parameters.size(); i++) { |
| 2340 | const GDScriptParser::ParameterNode *parameter = p_func->parameters[i]; |
| 2341 | GDScriptDataType par_type = _gdtype_from_datatype(parameter->get_datatype(), p_script); |
| 2342 | uint32_t par_addr = codegen.generator->add_parameter(parameter->identifier->name, parameter->initializer != nullptr, par_type); |
| 2343 | codegen.parameters[parameter->identifier->name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::FUNCTION_PARAMETER, par_addr, par_type); |
| 2344 | |
| 2345 | method_info.arguments.push_back(parameter->get_datatype().to_property_info(parameter->identifier->name)); |
| 2346 | |
| 2347 | if (parameter->initializer != nullptr) { |
| 2348 | optional_parameters++; |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | if (p_func->is_vararg()) { |
| 2353 | vararg_addr = codegen.add_local(p_func->rest_parameter->identifier->name, _gdtype_from_datatype(p_func->rest_parameter->get_datatype(), codegen.script)); |
| 2354 | method_info.flags |= METHOD_FLAG_VARARG; |
| 2355 | } |
| 2356 | |
| 2357 | method_info.default_arguments.append_array(p_func->default_arg_values); |
| 2358 | } |
| 2359 | |
| 2360 | // Parse initializer if applies. |
| 2361 | bool is_implicit_initializer = !p_for_ready && !p_func && !p_for_lambda; |
| 2362 | bool is_initializer = p_func && !p_for_lambda && p_func->identifier->name == GDScriptLanguage::get_singleton()->strings._init; |
| 2363 | bool is_implicit_ready = !p_func && p_for_ready; |
| 2364 | |
| 2365 | if (!p_for_lambda && is_implicit_initializer) { |
| 2366 | // Initialize the default values for typed variables before anything. |
| 2367 | // This avoids crashes if they are accessed with validated calls before being properly initialized. |
| 2368 | // It may happen with out-of-order access or with `@onready` variables. |
| 2369 | for (const GDScriptParser::ClassNode::Member &member : p_class->members) { |
| 2370 | if (member.type != GDScriptParser::ClassNode::Member::VARIABLE) { |
| 2371 | continue; |
| 2372 | } |
| 2373 | |
| 2374 | const GDScriptParser::VariableNode *field = member.variable; |
| 2375 | if (field->is_static) { |
| 2376 | continue; |
| 2377 | } |
| 2378 | |
| 2379 | GDScriptDataType field_type = _gdtype_from_datatype(field->get_datatype(), codegen.script); |
| 2380 | if (field_type.has_type()) { |
| 2381 | codegen.generator->write_newline(field->start_line); |
| 2382 | |
| 2383 | GDScriptCodeGenerator::Address dst_address(GDScriptCodeGenerator::Address::MEMBER, codegen.script->member_indices[field->identifier->name].index, field_type); |
| 2384 | |
| 2385 | if (field_type.builtin_type == Variant::ARRAY && field_type.has_container_element_type(0)) { |
| 2386 | codegen.generator->write_construct_typed_array(dst_address, field_type.get_container_element_type(0), Vector<GDScriptCodeGenerator::Address>()); |
| 2387 | } else if (field_type.builtin_type == Variant::DICTIONARY && field_type.has_container_element_types()) { |
| 2388 | codegen.generator->write_construct_typed_dictionary(dst_address, field_type.get_container_element_type_or_variant(0), |
| 2389 | field_type.get_container_element_type_or_variant(1), Vector<GDScriptCodeGenerator::Address>()); |
| 2390 | } else if (field_type.kind == GDScriptDataType::BUILTIN) { |
| 2391 | codegen.generator->write_construct(dst_address, field_type.builtin_type, Vector<GDScriptCodeGenerator::Address>()); |
| 2392 | } |
| 2393 | // The `else` branch is for objects, in such case we leave it as `null`. |
| 2394 | } |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | if (!p_for_lambda && (is_implicit_initializer || is_implicit_ready)) { |
| 2399 | // Initialize class fields. |
| 2400 | for (int i = 0; i < p_class->members.size(); i++) { |
| 2401 | if (p_class->members[i].type != GDScriptParser::ClassNode::Member::VARIABLE) { |
| 2402 | continue; |
| 2403 | } |
| 2404 | const GDScriptParser::VariableNode *field = p_class->members[i].variable; |
| 2405 | if (field->is_static) { |
| 2406 | continue; |
| 2407 | } |
| 2408 | |
| 2409 | if (field->onready != is_implicit_ready) { |
| 2410 | // Only initialize in `@implicit_ready()`. |
| 2411 | continue; |
| 2412 | } |
| 2413 | |
| 2414 | if (field->initializer) { |
| 2415 | codegen.generator->write_newline(field->initializer->start_line); |
| 2416 | |
| 2417 | GDScriptCodeGenerator::Address src_address = _parse_expression(codegen, r_error, field->initializer, false, true); |
| 2418 | if (r_error) { |
| 2419 | memdelete(codegen.generator); |
| 2420 | return nullptr; |
| 2421 | } |
| 2422 | |
| 2423 | GDScriptDataType field_type = _gdtype_from_datatype(field->get_datatype(), codegen.script); |
| 2424 | GDScriptCodeGenerator::Address dst_address(GDScriptCodeGenerator::Address::MEMBER, codegen.script->member_indices[field->identifier->name].index, field_type); |
| 2425 | |
| 2426 | if (field->use_conversion_assign) { |
| 2427 | codegen.generator->write_assign_with_conversion(dst_address, src_address); |
| 2428 | } else { |
| 2429 | codegen.generator->write_assign(dst_address, src_address); |
| 2430 | } |
| 2431 | if (src_address.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2432 | codegen.generator->pop_temporary(); |
| 2433 | } |
| 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | // Parse default argument code if applies. |
| 2439 | if (p_func) { |
| 2440 | if (optional_parameters > 0) { |
| 2441 | codegen.generator->start_parameters(); |
| 2442 | for (int i = p_func->parameters.size() - optional_parameters; i < p_func->parameters.size(); i++) { |
| 2443 | const GDScriptParser::ParameterNode *parameter = p_func->parameters[i]; |
| 2444 | GDScriptCodeGenerator::Address src_addr = _parse_expression(codegen, r_error, parameter->initializer); |
| 2445 | if (r_error) { |
| 2446 | memdelete(codegen.generator); |
| 2447 | return nullptr; |
| 2448 | } |
| 2449 | GDScriptCodeGenerator::Address dst_addr = codegen.parameters[parameter->identifier->name]; |
| 2450 | codegen.generator->write_assign_default_parameter(dst_addr, src_addr, parameter->use_conversion_assign); |
| 2451 | if (src_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2452 | codegen.generator->pop_temporary(); |
| 2453 | } |
| 2454 | } |
| 2455 | codegen.generator->end_parameters(); |
| 2456 | } |
| 2457 | |
| 2458 | // No need to reset locals at the end of the function, the stack will be cleared anyway. |
| 2459 | r_error = _parse_block(codegen, p_func->body, true, false); |
| 2460 | if (r_error) { |
| 2461 | memdelete(codegen.generator); |
| 2462 | return nullptr; |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | #ifdef DEBUG_ENABLED |
| 2467 | if (EngineDebugger::is_active()) { |
| 2468 | String signature; |
| 2469 | // Path. |
| 2470 | if (!p_script->get_script_path().is_empty()) { |
| 2471 | signature += p_script->get_script_path(); |
| 2472 | } |
| 2473 | // Location. |
| 2474 | if (p_func) { |
| 2475 | signature += "::" + itos(p_func->body->start_line); |
| 2476 | } else { |
| 2477 | signature += "::0"; |
| 2478 | } |
| 2479 | |
| 2480 | // Function and class. |
| 2481 | |
| 2482 | if (p_class->identifier) { |
| 2483 | signature += "::" + String(p_class->identifier->name) + "." + String(func_name); |
| 2484 | } else { |
| 2485 | signature += "::" + String(func_name); |
| 2486 | } |
| 2487 | |
| 2488 | if (p_for_lambda) { |
| 2489 | signature += "(lambda)"; |
| 2490 | } |
| 2491 | |
| 2492 | codegen.generator->set_signature(signature); |
| 2493 | } |
| 2494 | #endif |
| 2495 | |
| 2496 | if (p_func) { |
| 2497 | codegen.generator->set_initial_line(p_func->start_line); |
| 2498 | } else { |
| 2499 | codegen.generator->set_initial_line(0); |
| 2500 | } |
| 2501 | |
| 2502 | GDScriptFunction *gd_function = codegen.generator->write_end(); |
| 2503 | |
| 2504 | if (is_initializer) { |
| 2505 | p_script->initializer = gd_function; |
| 2506 | } else if (is_implicit_initializer) { |
| 2507 | p_script->implicit_initializer = gd_function; |
| 2508 | } else if (is_implicit_ready) { |
| 2509 | p_script->implicit_ready = gd_function; |
| 2510 | } |
| 2511 | |
| 2512 | if (p_func) { |
| 2513 | // If no `return` statement, then return type is `void`, not `Variant`. |
| 2514 | if (p_func->body->has_return) { |
| 2515 | gd_function->return_type = _gdtype_from_datatype(p_func->get_datatype(), p_script); |
| 2516 | method_info.return_val = p_func->get_datatype().to_property_info(String()); |
| 2517 | } else { |
| 2518 | gd_function->return_type = GDScriptDataType(); |
| 2519 | gd_function->return_type.kind = GDScriptDataType::BUILTIN; |
| 2520 | gd_function->return_type.builtin_type = Variant::NIL; |
| 2521 | } |
| 2522 | |
| 2523 | if (p_func->is_vararg()) { |
| 2524 | gd_function->_vararg_index = vararg_addr.address; |
| 2525 | } |
| 2526 | } |
| 2527 | |
| 2528 | gd_function->method_info = method_info; |
| 2529 | |
| 2530 | if (!is_implicit_initializer && !is_implicit_ready && !p_for_lambda) { |
| 2531 | p_script->member_functions[func_name] = gd_function; |
| 2532 | } |
| 2533 | |
| 2534 | memdelete(codegen.generator); |
| 2535 | |
| 2536 | return gd_function; |
| 2537 | } |
| 2538 | |
| 2539 | GDScriptFunction *GDScriptCompiler::_make_static_initializer(Error &r_error, GDScript *p_script, const GDScriptParser::ClassNode *p_class) { |
| 2540 | r_error = OK; |
| 2541 | CodeGen codegen; |
| 2542 | codegen.generator = memnew(GDScriptByteCodeGenerator); |
| 2543 | |
| 2544 | codegen.class_node = p_class; |
| 2545 | codegen.script = p_script; |
| 2546 | |
| 2547 | StringName func_name = SNAME("@static_initializer"); |
| 2548 | bool is_static = true; |
| 2549 | Variant rpc_config; |
| 2550 | GDScriptDataType return_type; |
| 2551 | return_type.kind = GDScriptDataType::BUILTIN; |
| 2552 | return_type.builtin_type = Variant::NIL; |
| 2553 | |
| 2554 | codegen.function_name = func_name; |
| 2555 | codegen.is_static = is_static; |
| 2556 | codegen.generator->write_start(p_script, func_name, is_static, rpc_config, return_type); |
| 2557 | |
| 2558 | // The static initializer is always called on the same class where the static variables are defined, |
| 2559 | // so the CLASS address (current class) can be used instead of `codegen.add_constant(p_script)`. |
| 2560 | GDScriptCodeGenerator::Address class_addr(GDScriptCodeGenerator::Address::CLASS); |
| 2561 | |
| 2562 | // Initialize the default values for typed variables before anything. |
| 2563 | // This avoids crashes if they are accessed with validated calls before being properly initialized. |
| 2564 | // It may happen with out-of-order access or with `@onready` variables. |
| 2565 | for (const GDScriptParser::ClassNode::Member &member : p_class->members) { |
| 2566 | if (member.type != GDScriptParser::ClassNode::Member::VARIABLE) { |
| 2567 | continue; |
| 2568 | } |
| 2569 | |
| 2570 | const GDScriptParser::VariableNode *field = member.variable; |
| 2571 | if (!field->is_static) { |
| 2572 | continue; |
| 2573 | } |
| 2574 | |
| 2575 | GDScriptDataType field_type = _gdtype_from_datatype(field->get_datatype(), codegen.script); |
| 2576 | if (field_type.has_type()) { |
| 2577 | codegen.generator->write_newline(field->start_line); |
| 2578 | |
| 2579 | if (field_type.builtin_type == Variant::ARRAY && field_type.has_container_element_type(0)) { |
| 2580 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(field_type); |
| 2581 | codegen.generator->write_construct_typed_array(temp, field_type.get_container_element_type(0), Vector<GDScriptCodeGenerator::Address>()); |
| 2582 | codegen.generator->write_set_static_variable(temp, class_addr, p_script->static_variables_indices[field->identifier->name].index); |
| 2583 | codegen.generator->pop_temporary(); |
| 2584 | } else if (field_type.builtin_type == Variant::DICTIONARY && field_type.has_container_element_types()) { |
| 2585 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(field_type); |
| 2586 | codegen.generator->write_construct_typed_dictionary(temp, field_type.get_container_element_type_or_variant(0), |
| 2587 | field_type.get_container_element_type_or_variant(1), Vector<GDScriptCodeGenerator::Address>()); |
| 2588 | codegen.generator->write_set_static_variable(temp, class_addr, p_script->static_variables_indices[field->identifier->name].index); |
| 2589 | codegen.generator->pop_temporary(); |
| 2590 | } else if (field_type.kind == GDScriptDataType::BUILTIN) { |
| 2591 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(field_type); |
| 2592 | codegen.generator->write_construct(temp, field_type.builtin_type, Vector<GDScriptCodeGenerator::Address>()); |
| 2593 | codegen.generator->write_set_static_variable(temp, class_addr, p_script->static_variables_indices[field->identifier->name].index); |
| 2594 | codegen.generator->pop_temporary(); |
| 2595 | } |
| 2596 | // The `else` branch is for objects, in such case we leave it as `null`. |
| 2597 | } |
| 2598 | } |
| 2599 | |
| 2600 | for (int i = 0; i < p_class->members.size(); i++) { |
| 2601 | // Initialize static fields. |
| 2602 | if (p_class->members[i].type != GDScriptParser::ClassNode::Member::VARIABLE) { |
| 2603 | continue; |
| 2604 | } |
| 2605 | const GDScriptParser::VariableNode *field = p_class->members[i].variable; |
| 2606 | if (!field->is_static) { |
| 2607 | continue; |
| 2608 | } |
| 2609 | |
| 2610 | if (field->initializer) { |
| 2611 | codegen.generator->write_newline(field->initializer->start_line); |
| 2612 | |
| 2613 | GDScriptCodeGenerator::Address src_address = _parse_expression(codegen, r_error, field->initializer, false, true); |
| 2614 | if (r_error) { |
| 2615 | memdelete(codegen.generator); |
| 2616 | return nullptr; |
| 2617 | } |
| 2618 | |
| 2619 | GDScriptDataType field_type = _gdtype_from_datatype(field->get_datatype(), codegen.script); |
| 2620 | GDScriptCodeGenerator::Address temp = codegen.add_temporary(field_type); |
| 2621 | |
| 2622 | if (field->use_conversion_assign) { |
| 2623 | codegen.generator->write_assign_with_conversion(temp, src_address); |
| 2624 | } else { |
| 2625 | codegen.generator->write_assign(temp, src_address); |
| 2626 | } |
| 2627 | if (src_address.mode == GDScriptCodeGenerator::Address::TEMPORARY) { |
| 2628 | codegen.generator->pop_temporary(); |
| 2629 | } |
| 2630 | |
| 2631 | codegen.generator->write_set_static_variable(temp, class_addr, p_script->static_variables_indices[field->identifier->name].index); |
| 2632 | codegen.generator->pop_temporary(); |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | if (p_script->has_method(GDScriptLanguage::get_singleton()->strings._static_init)) { |
| 2637 | codegen.generator->write_newline(p_class->start_line); |
| 2638 | codegen.generator->write_call(GDScriptCodeGenerator::Address(), class_addr, GDScriptLanguage::get_singleton()->strings._static_init, Vector<GDScriptCodeGenerator::Address>()); |
| 2639 | } |
| 2640 | |
| 2641 | #ifdef DEBUG_ENABLED |
| 2642 | if (EngineDebugger::is_active()) { |
| 2643 | String signature; |
| 2644 | // Path. |
| 2645 | if (!p_script->get_script_path().is_empty()) { |
| 2646 | signature += p_script->get_script_path(); |
| 2647 | } |
| 2648 | // Location. |
| 2649 | signature += "::0"; |
| 2650 | |
| 2651 | // Function and class. |
| 2652 | |
| 2653 | if (p_class->identifier) { |
| 2654 | signature += "::" + String(p_class->identifier->name) + "." + String(func_name); |
| 2655 | } else { |
| 2656 | signature += "::" + String(func_name); |
| 2657 | } |
| 2658 | |
| 2659 | codegen.generator->set_signature(signature); |
| 2660 | } |
| 2661 | #endif |
| 2662 | |
| 2663 | codegen.generator->set_initial_line(p_class->start_line); |
| 2664 | |
| 2665 | GDScriptFunction *gd_function = codegen.generator->write_end(); |
| 2666 | |
| 2667 | memdelete(codegen.generator); |
| 2668 | |
| 2669 | return gd_function; |
| 2670 | } |
| 2671 | |
| 2672 | Error GDScriptCompiler::_parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter) { |
| 2673 | Error err = OK; |
| 2674 | |
| 2675 | GDScriptParser::FunctionNode *function; |
| 2676 | |
| 2677 | if (p_is_setter) { |
| 2678 | function = p_variable->setter; |
| 2679 | } else { |
| 2680 | function = p_variable->getter; |
| 2681 | } |
| 2682 | |
| 2683 | _parse_function(err, p_script, p_class, function); |
| 2684 | |
| 2685 | return err; |
| 2686 | } |
| 2687 | |
| 2688 | // Prepares given script, and inner class scripts, for compilation. It populates class members and |
| 2689 | // initializes method RPC info for its base classes first, then for itself, then for inner classes. |
| 2690 | // WARNING: This function cannot initiate compilation of other classes, or it will result in |
| 2691 | // cyclic dependency issues. |
| 2692 | Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) { |
| 2693 | if (parsed_classes.has(p_script)) { |
| 2694 | return OK; |
| 2695 | } |
| 2696 | |
| 2697 | if (parsing_classes.has(p_script)) { |
| 2698 | String class_name = p_class->identifier ? String(p_class->identifier->name) : p_class->fqcn; |
| 2699 | _set_error(vformat(R"(Cyclic class reference for "%s".)", class_name), p_class); |
| 2700 | return ERR_PARSE_ERROR; |
| 2701 | } |
| 2702 | |
| 2703 | parsing_classes.insert(p_script); |
| 2704 | |
| 2705 | p_script->clearing = true; |
| 2706 | |
| 2707 | p_script->cancel_pending_functions(true); |
| 2708 | |
| 2709 | p_script->native = Ref<GDScriptNativeClass>(); |
| 2710 | p_script->base = Ref<GDScript>(); |
| 2711 | p_script->members.clear(); |
| 2712 | |
| 2713 | // This makes possible to clear script constants and member_functions without heap-use-after-free errors. |
| 2714 | HashMap<StringName, Variant> constants; |
| 2715 | for (const KeyValue<StringName, Variant> &E : p_script->constants) { |
| 2716 | constants.insert(E.key, E.value); |
| 2717 | } |
| 2718 | p_script->constants.clear(); |
| 2719 | constants.clear(); |
| 2720 | HashMap<StringName, GDScriptFunction *> member_functions; |
| 2721 | for (const KeyValue<StringName, GDScriptFunction *> &E : p_script->member_functions) { |
| 2722 | member_functions.insert(E.key, E.value); |
| 2723 | } |
| 2724 | p_script->member_functions.clear(); |
| 2725 | for (const KeyValue<StringName, GDScriptFunction *> &E : member_functions) { |
| 2726 | memdelete(E.value); |
| 2727 | } |
| 2728 | member_functions.clear(); |
| 2729 | |
| 2730 | p_script->static_variables.clear(); |
| 2731 | |
| 2732 | if (p_script->implicit_initializer) { |
| 2733 | memdelete(p_script->implicit_initializer); |
| 2734 | } |
| 2735 | if (p_script->implicit_ready) { |
| 2736 | memdelete(p_script->implicit_ready); |
| 2737 | } |
| 2738 | if (p_script->static_initializer) { |
| 2739 | memdelete(p_script->static_initializer); |
| 2740 | } |
| 2741 | |
| 2742 | p_script->member_functions.clear(); |
| 2743 | p_script->member_indices.clear(); |
| 2744 | p_script->static_variables_indices.clear(); |
| 2745 | p_script->static_variables.clear(); |
| 2746 | p_script->_signals.clear(); |
| 2747 | p_script->initializer = nullptr; |
| 2748 | p_script->implicit_initializer = nullptr; |
| 2749 | p_script->implicit_ready = nullptr; |
| 2750 | p_script->static_initializer = nullptr; |
| 2751 | p_script->rpc_config.clear(); |
| 2752 | p_script->lambda_info.clear(); |
| 2753 | |
| 2754 | p_script->clearing = false; |
| 2755 | |
| 2756 | p_script->tool = parser->is_tool(); |
| 2757 | p_script->_is_abstract = p_class->is_abstract; |
| 2758 | |
| 2759 | if (p_script->local_name != StringName()) { |
| 2760 | if (GDScriptAnalyzer::class_exists(p_script->local_name)) { |
| 2761 | _set_error(vformat(R"(The class "%s" shadows a native class)", p_script->local_name), p_class); |
| 2762 | return ERR_ALREADY_EXISTS; |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | GDScriptDataType base_type = _gdtype_from_datatype(p_class->base_type, p_script, false); |
| 2767 | |
| 2768 | if (base_type.native_type == StringName()) { |
| 2769 | _set_error(vformat(R"(Parser bug (please report): Empty native type in base class "%s")", p_script->path), p_class); |
| 2770 | return ERR_BUG; |
| 2771 | } |
| 2772 | |
| 2773 | int native_idx = GDScriptLanguage::get_singleton()->get_global_map()[base_type.native_type]; |
| 2774 | |
| 2775 | p_script->native = GDScriptLanguage::get_singleton()->get_global_array()[native_idx]; |
| 2776 | if (p_script->native.is_null()) { |
| 2777 | _set_error("Compiler bug (please report): script native type is null.", nullptr); |
| 2778 | return ERR_BUG; |
| 2779 | } |
| 2780 | |
| 2781 | // Inheritance |
| 2782 | switch (base_type.kind) { |
| 2783 | case GDScriptDataType::NATIVE: |
| 2784 | // Nothing more to do. |
| 2785 | break; |
| 2786 | case GDScriptDataType::GDSCRIPT: { |
| 2787 | Ref<GDScript> base = Ref<GDScript>(base_type.script_type); |
| 2788 | if (base.is_null()) { |
| 2789 | _set_error("Compiler bug (please report): base script type is null.", nullptr); |
| 2790 | return ERR_BUG; |
| 2791 | } |
| 2792 | |
| 2793 | if (main_script->has_class(base.ptr())) { |
| 2794 | Error err = _prepare_compilation(base.ptr(), p_class->base_type.class_type, p_keep_state); |
| 2795 | if (err) { |
| 2796 | return err; |
| 2797 | } |
| 2798 | } else if (!base->is_valid()) { |
| 2799 | Error err = OK; |
| 2800 | Ref<GDScript> base_root = GDScriptCache::get_shallow_script(base->path, err, p_script->path); |
| 2801 | if (err) { |
| 2802 | _set_error(vformat(R"(Could not parse base class "%s" from "%s": %s)", base->fully_qualified_name, base->path, error_names[err]), nullptr); |
| 2803 | return err; |
| 2804 | } |
| 2805 | if (base_root.is_valid()) { |
| 2806 | base = Ref<GDScript>(base_root->find_class(base->fully_qualified_name)); |
| 2807 | } |
| 2808 | if (base.is_null()) { |
| 2809 | _set_error(vformat(R"(Could not find class "%s" in "%s".)", base->fully_qualified_name, base->path), nullptr); |
| 2810 | return ERR_COMPILATION_FAILED; |
| 2811 | } |
| 2812 | |
| 2813 | err = _prepare_compilation(base.ptr(), p_class->base_type.class_type, p_keep_state); |
| 2814 | if (err) { |
| 2815 | _set_error(vformat(R"(Could not populate class members of base class "%s" in "%s".)", base->fully_qualified_name, base->path), nullptr); |
| 2816 | return err; |
| 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | p_script->base = base; |
| 2821 | p_script->member_indices = base->member_indices; |
| 2822 | } break; |
| 2823 | default: { |
| 2824 | _set_error("Parser bug (please report): invalid inheritance.", nullptr); |
| 2825 | return ERR_BUG; |
| 2826 | } break; |
| 2827 | } |
| 2828 | |
| 2829 | // Duplicate RPC information from base GDScript |
| 2830 | // Base script isn't valid because it should not have been compiled yet, but the reference contains relevant info. |
| 2831 | if (base_type.kind == GDScriptDataType::GDSCRIPT && p_script->base.is_valid()) { |
| 2832 | p_script->rpc_config = p_script->base->rpc_config.duplicate(); |
| 2833 | } |
| 2834 | |
| 2835 | for (int i = 0; i < p_class->members.size(); i++) { |
| 2836 | const GDScriptParser::ClassNode::Member &member = p_class->members[i]; |
| 2837 | switch (member.type) { |
| 2838 | case GDScriptParser::ClassNode::Member::VARIABLE: { |
| 2839 | const GDScriptParser::VariableNode *variable = member.variable; |
| 2840 | StringName name = variable->identifier->name; |
| 2841 | |
| 2842 | GDScript::MemberInfo minfo; |
| 2843 | switch (variable->property) { |
| 2844 | case GDScriptParser::VariableNode::PROP_NONE: |
| 2845 | break; // Nothing to do. |
| 2846 | case GDScriptParser::VariableNode::PROP_SETGET: |
| 2847 | if (variable->setter_pointer != nullptr) { |
| 2848 | minfo.setter = variable->setter_pointer->name; |
| 2849 | } |
| 2850 | if (variable->getter_pointer != nullptr) { |
| 2851 | minfo.getter = variable->getter_pointer->name; |
| 2852 | } |
| 2853 | break; |
| 2854 | case GDScriptParser::VariableNode::PROP_INLINE: |
| 2855 | if (variable->setter != nullptr) { |
| 2856 | minfo.setter = "@" + variable->identifier->name + "_setter"; |
| 2857 | } |
| 2858 | if (variable->getter != nullptr) { |
| 2859 | minfo.getter = "@" + variable->identifier->name + "_getter"; |
| 2860 | } |
| 2861 | break; |
| 2862 | } |
| 2863 | minfo.data_type = _gdtype_from_datatype(variable->get_datatype(), p_script); |
| 2864 | |
| 2865 | PropertyInfo prop_info = variable->get_datatype().to_property_info(name); |
| 2866 | PropertyInfo export_info = variable->export_info; |
| 2867 | |
| 2868 | if (variable->exported) { |
| 2869 | if (!minfo.data_type.has_type()) { |
| 2870 | prop_info.type = export_info.type; |
| 2871 | prop_info.class_name = export_info.class_name; |
| 2872 | } |
| 2873 | prop_info.hint = export_info.hint; |
| 2874 | prop_info.hint_string = export_info.hint_string; |
| 2875 | prop_info.usage = export_info.usage; |
| 2876 | } |
| 2877 | prop_info.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE; |
| 2878 | minfo.property_info = prop_info; |
| 2879 | |
| 2880 | if (variable->is_static) { |
| 2881 | minfo.index = p_script->static_variables_indices.size(); |
| 2882 | p_script->static_variables_indices[name] = minfo; |
| 2883 | } else { |
| 2884 | minfo.index = p_script->member_indices.size(); |
| 2885 | p_script->member_indices[name] = minfo; |
| 2886 | p_script->members.insert(name); |
| 2887 | } |
| 2888 | |
| 2889 | #ifdef TOOLS_ENABLED |
| 2890 | if (variable->initializer != nullptr && variable->initializer->is_constant) { |
| 2891 | p_script->member_default_values[name] = variable->initializer->reduced_value; |
| 2892 | GDScriptCompiler::convert_to_initializer_type(p_script->member_default_values[name], variable); |
| 2893 | } else { |
| 2894 | p_script->member_default_values.erase(name); |
| 2895 | } |
| 2896 | #endif |
| 2897 | } break; |
| 2898 | |
| 2899 | case GDScriptParser::ClassNode::Member::CONSTANT: { |
| 2900 | const GDScriptParser::ConstantNode *constant = member.constant; |
| 2901 | StringName name = constant->identifier->name; |
| 2902 | |
| 2903 | p_script->constants.insert(name, constant->initializer->reduced_value); |
| 2904 | } break; |
| 2905 | |
| 2906 | case GDScriptParser::ClassNode::Member::ENUM_VALUE: { |
| 2907 | const GDScriptParser::EnumNode::Value &enum_value = member.enum_value; |
| 2908 | StringName name = enum_value.identifier->name; |
| 2909 | |
| 2910 | p_script->constants.insert(name, enum_value.value); |
| 2911 | } break; |
| 2912 | |
| 2913 | case GDScriptParser::ClassNode::Member::SIGNAL: { |
| 2914 | const GDScriptParser::SignalNode *signal = member.signal; |
| 2915 | StringName name = signal->identifier->name; |
| 2916 | |
| 2917 | p_script->_signals[name] = signal->method_info; |
| 2918 | } break; |
| 2919 | |
| 2920 | case GDScriptParser::ClassNode::Member::ENUM: { |
| 2921 | const GDScriptParser::EnumNode *enum_n = member.m_enum; |
| 2922 | StringName name = enum_n->identifier->name; |
| 2923 | |
| 2924 | p_script->constants.insert(name, enum_n->dictionary); |
| 2925 | } break; |
| 2926 | |
| 2927 | case GDScriptParser::ClassNode::Member::GROUP: { |
| 2928 | const GDScriptParser::AnnotationNode *annotation = member.annotation; |
| 2929 | // Avoid name conflict. See GH-78252. |
| 2930 | StringName name = vformat("@group_%d_%s", p_script->members.size(), annotation->export_info.name); |
| 2931 | |
| 2932 | // This is not a normal member, but we need this to keep indices in order. |
| 2933 | GDScript::MemberInfo minfo; |
| 2934 | minfo.index = p_script->member_indices.size(); |
| 2935 | |
| 2936 | PropertyInfo prop_info; |
| 2937 | prop_info.name = annotation->export_info.name; |
| 2938 | prop_info.usage = annotation->export_info.usage; |
| 2939 | prop_info.hint_string = annotation->export_info.hint_string; |
| 2940 | minfo.property_info = prop_info; |
| 2941 | |
| 2942 | p_script->member_indices[name] = minfo; |
| 2943 | p_script->members.insert(name); |
| 2944 | } break; |
| 2945 | |
| 2946 | case GDScriptParser::ClassNode::Member::FUNCTION: { |
| 2947 | const GDScriptParser::FunctionNode *function_n = member.function; |
| 2948 | |
| 2949 | Variant config = function_n->rpc_config; |
| 2950 | if (config.get_type() != Variant::NIL) { |
| 2951 | p_script->rpc_config[function_n->identifier->name] = config; |
| 2952 | } |
| 2953 | } break; |
| 2954 | default: |
| 2955 | break; // Nothing to do here. |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | p_script->static_variables.resize(p_script->static_variables_indices.size()); |
| 2960 | |
| 2961 | parsed_classes.insert(p_script); |
| 2962 | parsing_classes.erase(p_script); |
| 2963 | |
| 2964 | // Populate inner classes. |
| 2965 | for (int i = 0; i < p_class->members.size(); i++) { |
| 2966 | const GDScriptParser::ClassNode::Member &member = p_class->members[i]; |
| 2967 | if (member.type != member.CLASS) { |
| 2968 | continue; |
| 2969 | } |
| 2970 | const GDScriptParser::ClassNode *inner_class = member.m_class; |
| 2971 | StringName name = inner_class->identifier->name; |
| 2972 | Ref<GDScript> &subclass = p_script->subclasses[name]; |
| 2973 | GDScript *subclass_ptr = subclass.ptr(); |
| 2974 | |
| 2975 | // Subclass might still be parsing, just skip it |
| 2976 | if (!parsing_classes.has(subclass_ptr)) { |
| 2977 | Error err = _prepare_compilation(subclass_ptr, inner_class, p_keep_state); |
| 2978 | if (err) { |
| 2979 | return err; |
| 2980 | } |
| 2981 | } |
| 2982 | |
| 2983 | p_script->constants.insert(name, subclass); //once parsed, goes to the list of constants |
| 2984 | } |
| 2985 | |
| 2986 | return OK; |
| 2987 | } |
| 2988 | |
| 2989 | Error GDScriptCompiler::_compile_class(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) { |
| 2990 | // Compile member functions, getters, and setters. |
| 2991 | for (int i = 0; i < p_class->members.size(); i++) { |
| 2992 | const GDScriptParser::ClassNode::Member &member = p_class->members[i]; |
| 2993 | if (member.type == member.FUNCTION) { |
| 2994 | const GDScriptParser::FunctionNode *function = member.function; |
| 2995 | Error err = OK; |
| 2996 | _parse_function(err, p_script, p_class, function); |
| 2997 | if (err) { |
| 2998 | return err; |
| 2999 | } |
| 3000 | } else if (member.type == member.VARIABLE) { |
| 3001 | const GDScriptParser::VariableNode *variable = member.variable; |
| 3002 | if (variable->property == GDScriptParser::VariableNode::PROP_INLINE) { |
| 3003 | if (variable->setter != nullptr) { |
| 3004 | Error err = _parse_setter_getter(p_script, p_class, variable, true); |
| 3005 | if (err) { |
| 3006 | return err; |
| 3007 | } |
| 3008 | } |
| 3009 | if (variable->getter != nullptr) { |
| 3010 | Error err = _parse_setter_getter(p_script, p_class, variable, false); |
| 3011 | if (err) { |
| 3012 | return err; |
| 3013 | } |
| 3014 | } |
| 3015 | } |
| 3016 | } |
| 3017 | } |
| 3018 | |
| 3019 | { |
| 3020 | // Create `@implicit_new()` special function in any case. |
| 3021 | Error err = OK; |
| 3022 | _parse_function(err, p_script, p_class, nullptr); |
| 3023 | if (err) { |
| 3024 | return err; |
| 3025 | } |
| 3026 | } |
| 3027 | |
| 3028 | if (p_class->onready_used) { |
| 3029 | // Create `@implicit_ready()` special function. |
| 3030 | Error err = OK; |
| 3031 | _parse_function(err, p_script, p_class, nullptr, true); |
| 3032 | if (err) { |
| 3033 | return err; |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | if (p_class->has_static_data) { |
| 3038 | Error err = OK; |
| 3039 | GDScriptFunction *func = _make_static_initializer(err, p_script, p_class); |
| 3040 | p_script->static_initializer = func; |
| 3041 | if (err) { |
| 3042 | return err; |
| 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | #ifdef DEBUG_ENABLED |
| 3047 | |
| 3048 | //validate instances if keeping state |
| 3049 | |
| 3050 | if (p_keep_state) { |
| 3051 | for (RBSet<Object *>::Element *E = p_script->instances.front(); E;) { |
| 3052 | RBSet<Object *>::Element *N = E->next(); |
| 3053 | |
| 3054 | ScriptInstance *si = E->get()->get_script_instance(); |
| 3055 | if (si->is_placeholder()) { |
| 3056 | #ifdef TOOLS_ENABLED |
| 3057 | PlaceHolderScriptInstance *psi = static_cast<PlaceHolderScriptInstance *>(si); |
| 3058 | |
| 3059 | if (p_script->is_tool()) { |
| 3060 | //re-create as an instance |
| 3061 | p_script->placeholders.erase(psi); //remove placeholder |
| 3062 | |
| 3063 | GDScriptInstance *instance = memnew(GDScriptInstance); |
| 3064 | instance->members.resize(p_script->member_indices.size()); |
| 3065 | instance->script = Ref<GDScript>(p_script); |
| 3066 | instance->owner = E->get(); |
| 3067 | |
| 3068 | //needed for hot reloading |
| 3069 | for (const KeyValue<StringName, GDScript::MemberInfo> &F : p_script->member_indices) { |
| 3070 | instance->member_indices_cache[F.key] = F.value.index; |
| 3071 | } |
| 3072 | instance->owner->set_script_instance(instance); |
| 3073 | |
| 3074 | /* STEP 2, INITIALIZE AND CONSTRUCT */ |
| 3075 | |
| 3076 | Callable::CallError ce; |
| 3077 | p_script->initializer->call(instance, nullptr, 0, ce); |
| 3078 | |
| 3079 | if (ce.error != Callable::CallError::CALL_OK) { |
| 3080 | //well, tough luck, not gonna do anything here |
| 3081 | } |
| 3082 | } |
| 3083 | #endif // TOOLS_ENABLED |
| 3084 | } else { |
| 3085 | GDScriptInstance *gi = static_cast<GDScriptInstance *>(si); |
| 3086 | gi->reload_members(); |
| 3087 | } |
| 3088 | |
| 3089 | E = N; |
| 3090 | } |
| 3091 | } |
| 3092 | #endif //DEBUG_ENABLED |
| 3093 | |
| 3094 | has_static_data = p_class->has_static_data; |
| 3095 | |
| 3096 | for (int i = 0; i < p_class->members.size(); i++) { |
| 3097 | if (p_class->members[i].type != GDScriptParser::ClassNode::Member::CLASS) { |
| 3098 | continue; |
| 3099 | } |
| 3100 | const GDScriptParser::ClassNode *inner_class = p_class->members[i].m_class; |
| 3101 | StringName name = inner_class->identifier->name; |
| 3102 | GDScript *subclass = p_script->subclasses[name].ptr(); |
| 3103 | |
| 3104 | Error err = _compile_class(subclass, inner_class, p_keep_state); |
| 3105 | if (err) { |
| 3106 | return err; |
| 3107 | } |
| 3108 | |
| 3109 | has_static_data = has_static_data || inner_class->has_static_data; |
| 3110 | } |
| 3111 | |
| 3112 | p_script->_static_default_init(); |
| 3113 | |
| 3114 | p_script->valid = true; |
| 3115 | return OK; |
| 3116 | } |
| 3117 | |
| 3118 | void GDScriptCompiler::convert_to_initializer_type(Variant &p_variant, const GDScriptParser::VariableNode *p_node) { |
| 3119 | // Set p_variant to the value of p_node's initializer, with the type of p_node's variable. |
| 3120 | GDScriptParser::DataType member_t = p_node->datatype; |
| 3121 | GDScriptParser::DataType init_t = p_node->initializer->datatype; |
| 3122 | if (member_t.is_hard_type() && init_t.is_hard_type() && |
| 3123 | member_t.kind == GDScriptParser::DataType::BUILTIN && init_t.kind == GDScriptParser::DataType::BUILTIN) { |
| 3124 | if (Variant::can_convert_strict(init_t.builtin_type, member_t.builtin_type)) { |
| 3125 | const Variant *v = &p_node->initializer->reduced_value; |
| 3126 | Callable::CallError ce; |
| 3127 | Variant::construct(member_t.builtin_type, p_variant, &v, 1, ce); |
| 3128 | } |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | void GDScriptCompiler::make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) { |
| 3133 | p_script->fully_qualified_name = p_class->fqcn; |
| 3134 | p_script->local_name = p_class->identifier ? p_class->identifier->name : StringName(); |
| 3135 | p_script->global_name = p_class->get_global_name(); |
| 3136 | p_script->simplified_icon_path = p_class->simplified_icon_path; |
| 3137 | |
| 3138 | HashMap<StringName, Ref<GDScript>> old_subclasses; |
| 3139 | |
| 3140 | if (p_keep_state) { |
| 3141 | old_subclasses = p_script->subclasses; |
| 3142 | } |
| 3143 | |
| 3144 | p_script->subclasses.clear(); |
| 3145 | |
| 3146 | for (int i = 0; i < p_class->members.size(); i++) { |
| 3147 | if (p_class->members[i].type != GDScriptParser::ClassNode::Member::CLASS) { |
| 3148 | continue; |
| 3149 | } |
| 3150 | const GDScriptParser::ClassNode *inner_class = p_class->members[i].m_class; |
| 3151 | StringName name = inner_class->identifier->name; |
| 3152 | |
| 3153 | Ref<GDScript> subclass; |
| 3154 | |
| 3155 | if (old_subclasses.has(name)) { |
| 3156 | subclass = old_subclasses[name]; |
| 3157 | } else { |
| 3158 | subclass = GDScriptLanguage::get_singleton()->get_orphan_subclass(inner_class->fqcn); |
| 3159 | } |
| 3160 | |
| 3161 | if (subclass.is_null()) { |
| 3162 | subclass.instantiate(); |
| 3163 | } |
| 3164 | |
| 3165 | subclass->_owner = p_script; |
| 3166 | subclass->path = p_script->path; |
| 3167 | p_script->subclasses.insert(name, subclass); |
| 3168 | |
| 3169 | make_scripts(subclass.ptr(), inner_class, p_keep_state); |
| 3170 | } |
| 3171 | } |
| 3172 | |
| 3173 | GDScriptCompiler::FunctionLambdaInfo GDScriptCompiler::_get_function_replacement_info(GDScriptFunction *p_func, int p_index, int p_depth, GDScriptFunction *p_parent_func) { |
| 3174 | FunctionLambdaInfo info; |
| 3175 | info.function = p_func; |
| 3176 | info.parent = p_parent_func; |
| 3177 | info.script = p_func->get_script(); |
| 3178 | info.name = p_func->get_name(); |
| 3179 | info.line = p_func->_initial_line; |
| 3180 | info.index = p_index; |
| 3181 | info.depth = p_depth; |
| 3182 | info.capture_count = 0; |
| 3183 | info.use_self = false; |
| 3184 | info.arg_count = p_func->_argument_count; |
| 3185 | info.default_arg_count = p_func->_default_arg_count; |
| 3186 | info.sublambdas = _get_function_lambda_replacement_info(p_func, p_depth, p_parent_func); |
| 3187 | |
| 3188 | ERR_FAIL_NULL_V(info.script, info); |
| 3189 | GDScript::LambdaInfo *extra_info = info.script->lambda_info.getptr(p_func); |
| 3190 | if (extra_info != nullptr) { |
| 3191 | info.capture_count = extra_info->capture_count; |
| 3192 | info.use_self = extra_info->use_self; |
| 3193 | } else { |
| 3194 | info.capture_count = 0; |
| 3195 | info.use_self = false; |
| 3196 | } |
| 3197 | |
| 3198 | return info; |
| 3199 | } |
| 3200 | |
| 3201 | Vector<GDScriptCompiler::FunctionLambdaInfo> GDScriptCompiler::_get_function_lambda_replacement_info(GDScriptFunction *p_func, int p_depth, GDScriptFunction *p_parent_func) { |
| 3202 | Vector<FunctionLambdaInfo> result; |
| 3203 | // Only scrape the lambdas inside p_func. |
| 3204 | for (int i = 0; i < p_func->lambdas.size(); ++i) { |
| 3205 | result.push_back(_get_function_replacement_info(p_func->lambdas[i], i, p_depth + 1, p_func)); |
| 3206 | } |
| 3207 | return result; |
| 3208 | } |
| 3209 | |
| 3210 | GDScriptCompiler::ScriptLambdaInfo GDScriptCompiler::_get_script_lambda_replacement_info(GDScript *p_script) { |
| 3211 | ScriptLambdaInfo info; |
| 3212 | |
| 3213 | if (p_script->implicit_initializer) { |
| 3214 | info.implicit_initializer_info = _get_function_lambda_replacement_info(p_script->implicit_initializer); |
| 3215 | } |
| 3216 | if (p_script->implicit_ready) { |
| 3217 | info.implicit_ready_info = _get_function_lambda_replacement_info(p_script->implicit_ready); |
| 3218 | } |
| 3219 | if (p_script->static_initializer) { |
| 3220 | info.static_initializer_info = _get_function_lambda_replacement_info(p_script->static_initializer); |
| 3221 | } |
| 3222 | |
| 3223 | for (const KeyValue<StringName, GDScriptFunction *> &E : p_script->member_functions) { |
| 3224 | info.member_function_infos.insert(E.key, _get_function_lambda_replacement_info(E.value)); |
| 3225 | } |
| 3226 | |
| 3227 | for (const KeyValue<StringName, Ref<GDScript>> &KV : p_script->get_subclasses()) { |
| 3228 | info.subclass_info.insert(KV.key, _get_script_lambda_replacement_info(KV.value.ptr())); |
| 3229 | } |
| 3230 | |
| 3231 | return info; |
| 3232 | } |
| 3233 | |
| 3234 | bool GDScriptCompiler::_do_function_infos_match(const FunctionLambdaInfo &p_old_info, const FunctionLambdaInfo *p_new_info) { |
| 3235 | if (p_new_info == nullptr) { |
| 3236 | return false; |
| 3237 | } |
| 3238 | |
| 3239 | if (p_new_info->capture_count != p_old_info.capture_count || p_new_info->use_self != p_old_info.use_self) { |
| 3240 | return false; |
| 3241 | } |
| 3242 | |
| 3243 | int old_required_arg_count = p_old_info.arg_count - p_old_info.default_arg_count; |
| 3244 | int new_required_arg_count = p_new_info->arg_count - p_new_info->default_arg_count; |
| 3245 | if (new_required_arg_count > old_required_arg_count || p_new_info->arg_count < old_required_arg_count) { |
| 3246 | return false; |
| 3247 | } |
| 3248 | |
| 3249 | return true; |
| 3250 | } |
| 3251 | |
| 3252 | void GDScriptCompiler::_get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const FunctionLambdaInfo &p_old_info, const FunctionLambdaInfo *p_new_info) { |
| 3253 | ERR_FAIL_COND(r_replacements.has(p_old_info.function)); |
| 3254 | if (!_do_function_infos_match(p_old_info, p_new_info)) { |
| 3255 | p_new_info = nullptr; |
| 3256 | } |
| 3257 | |
| 3258 | r_replacements.insert(p_old_info.function, p_new_info != nullptr ? p_new_info->function : nullptr); |
| 3259 | _get_function_ptr_replacements(r_replacements, p_old_info.sublambdas, p_new_info != nullptr ? &p_new_info->sublambdas : nullptr); |
| 3260 | } |
| 3261 | |
| 3262 | void GDScriptCompiler::_get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const Vector<FunctionLambdaInfo> &p_old_infos, const Vector<FunctionLambdaInfo> *p_new_infos) { |
| 3263 | for (int i = 0; i < p_old_infos.size(); ++i) { |
| 3264 | const FunctionLambdaInfo &old_info = p_old_infos[i]; |
| 3265 | const FunctionLambdaInfo *new_info = nullptr; |
| 3266 | if (p_new_infos != nullptr && p_new_infos->size() == p_old_infos.size()) { |
| 3267 | // For now only attempt if the size is the same. |
| 3268 | new_info = &p_new_infos->get(i); |
| 3269 | } |
| 3270 | _get_function_ptr_replacements(r_replacements, old_info, new_info); |
| 3271 | } |
| 3272 | } |
| 3273 | |
| 3274 | void GDScriptCompiler::_get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const ScriptLambdaInfo &p_old_info, const ScriptLambdaInfo *p_new_info) { |
| 3275 | _get_function_ptr_replacements(r_replacements, p_old_info.implicit_initializer_info, p_new_info != nullptr ? &p_new_info->implicit_initializer_info : nullptr); |
| 3276 | _get_function_ptr_replacements(r_replacements, p_old_info.implicit_ready_info, p_new_info != nullptr ? &p_new_info->implicit_ready_info : nullptr); |
| 3277 | _get_function_ptr_replacements(r_replacements, p_old_info.static_initializer_info, p_new_info != nullptr ? &p_new_info->static_initializer_info : nullptr); |
| 3278 | |
| 3279 | for (const KeyValue<StringName, Vector<FunctionLambdaInfo>> &old_kv : p_old_info.member_function_infos) { |
| 3280 | _get_function_ptr_replacements(r_replacements, old_kv.value, p_new_info != nullptr ? p_new_info->member_function_infos.getptr(old_kv.key) : nullptr); |
| 3281 | } |
| 3282 | for (int i = 0; i < p_old_info.other_function_infos.size(); ++i) { |
| 3283 | const FunctionLambdaInfo &old_other_info = p_old_info.other_function_infos[i]; |
| 3284 | const FunctionLambdaInfo *new_other_info = nullptr; |
| 3285 | if (p_new_info != nullptr && p_new_info->other_function_infos.size() == p_old_info.other_function_infos.size()) { |
| 3286 | // For now only attempt if the size is the same. |
| 3287 | new_other_info = &p_new_info->other_function_infos[i]; |
| 3288 | } |
| 3289 | // Needs to be called on all old lambdas, even if there's no replacement. |
| 3290 | _get_function_ptr_replacements(r_replacements, old_other_info, new_other_info); |
| 3291 | } |
| 3292 | for (const KeyValue<StringName, ScriptLambdaInfo> &old_kv : p_old_info.subclass_info) { |
| 3293 | const ScriptLambdaInfo &old_subinfo = old_kv.value; |
| 3294 | const ScriptLambdaInfo *new_subinfo = p_new_info != nullptr ? p_new_info->subclass_info.getptr(old_kv.key) : nullptr; |
| 3295 | _get_function_ptr_replacements(r_replacements, old_subinfo, new_subinfo); |
| 3296 | } |
| 3297 | } |
| 3298 | |
| 3299 | Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state) { |
| 3300 | err_line = -1; |
| 3301 | err_column = -1; |
| 3302 | error = ""; |
| 3303 | parser = p_parser; |
| 3304 | main_script = p_script; |
| 3305 | const GDScriptParser::ClassNode *root = parser->get_tree(); |
| 3306 | |
| 3307 | source = p_script->get_path(); |
| 3308 | |
| 3309 | ScriptLambdaInfo old_lambda_info = _get_script_lambda_replacement_info(p_script); |
| 3310 | |
| 3311 | // Create scripts for subclasses beforehand so they can be referenced |
| 3312 | make_scripts(p_script, root, p_keep_state); |
| 3313 | |
| 3314 | main_script->_owner = nullptr; |
| 3315 | Error err = _prepare_compilation(main_script, parser->get_tree(), p_keep_state); |
| 3316 | |
| 3317 | if (err) { |
| 3318 | return err; |
| 3319 | } |
| 3320 | |
| 3321 | err = _compile_class(main_script, root, p_keep_state); |
| 3322 | if (err) { |
| 3323 | return err; |
| 3324 | } |
| 3325 | |
| 3326 | ScriptLambdaInfo new_lambda_info = _get_script_lambda_replacement_info(p_script); |
| 3327 | |
| 3328 | HashMap<GDScriptFunction *, GDScriptFunction *> func_ptr_replacements; |
| 3329 | _get_function_ptr_replacements(func_ptr_replacements, old_lambda_info, &new_lambda_info); |
| 3330 | main_script->_recurse_replace_function_ptrs(func_ptr_replacements); |
| 3331 | |
| 3332 | if (has_static_data && !root->annotated_static_unload) { |
| 3333 | GDScriptCache::add_static_script(p_script); |
| 3334 | } |
| 3335 | |
| 3336 | err = GDScriptCache::finish_compiling(main_script->path); |
| 3337 | if (err) { |
| 3338 | _set_error(R"(Failed to compile depended scripts.)", nullptr); |
| 3339 | } |
| 3340 | return err; |
| 3341 | } |
| 3342 | |
| 3343 | String GDScriptCompiler::get_error() const { |
| 3344 | return error; |
| 3345 | } |
| 3346 | |
| 3347 | int GDScriptCompiler::get_error_line() const { |
| 3348 | return err_line; |
| 3349 | } |
| 3350 | |
| 3351 | int GDScriptCompiler::get_error_column() const { |
| 3352 | return err_column; |
| 3353 | } |
| 3354 | |
| 3355 | GDScriptCompiler::GDScriptCompiler() { |
| 3356 | } |