Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_compiler.cpp
10277 views
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_byte_codegen.h"
35
#include "gdscript_cache.h"
36
#include "gdscript_utility_functions.h"
37
38
#include "core/config/engine.h"
39
#include "core/config/project_settings.h"
40
41
#include "scene/scene_string_names.h"
42
43
bool GDScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringName &p_name) {
44
if (codegen.function_node && codegen.function_node->is_static) {
45
return false;
46
}
47
48
if (_is_local_or_parameter(codegen, p_name)) {
49
return false; //shadowed
50
}
51
52
return _is_class_member_property(codegen.script, p_name);
53
}
54
55
bool GDScriptCompiler::_is_class_member_property(GDScript *owner, const StringName &p_name) {
56
GDScript *scr = owner;
57
GDScriptNativeClass *nc = nullptr;
58
while (scr) {
59
if (scr->native.is_valid()) {
60
nc = scr->native.ptr();
61
}
62
scr = scr->_base;
63
}
64
65
ERR_FAIL_NULL_V(nc, false);
66
67
return ClassDB::has_property(nc->get_name(), p_name);
68
}
69
70
bool GDScriptCompiler::_is_local_or_parameter(CodeGen &codegen, const StringName &p_name) {
71
return codegen.parameters.has(p_name) || codegen.locals.has(p_name);
72
}
73
74
void GDScriptCompiler::_set_error(const String &p_error, const GDScriptParser::Node *p_node) {
75
if (!error.is_empty()) {
76
return;
77
}
78
79
error = p_error;
80
if (p_node) {
81
err_line = p_node->start_line;
82
err_column = p_node->start_column;
83
} else {
84
err_line = 0;
85
err_column = 0;
86
}
87
}
88
89
GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::DataType &p_datatype, GDScript *p_owner, bool p_handle_metatype) {
90
if (!p_datatype.is_set() || !p_datatype.is_hard_type() || p_datatype.is_coroutine) {
91
return GDScriptDataType();
92
}
93
94
GDScriptDataType result;
95
result.has_type = true;
96
97
switch (p_datatype.kind) {
98
case GDScriptParser::DataType::VARIANT: {
99
result.has_type = false;
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;
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;
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;
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.has_type && 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 (ClassDB::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.has_type && 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;
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;
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.has_type = true;
1449
equality_type.kind = GDScriptDataType::BUILTIN;
1450
equality_type.builtin_type = Variant::BOOL;
1451
1452
// Check type equality.
1453
GDScriptCodeGenerator::Address type_equality_addr = codegen.add_temporary(equality_type);
1454
codegen.generator->write_binary_operator(type_equality_addr, Variant::OP_EQUAL, p_type_addr, literal_type_addr);
1455
1456
if (literal_type == Variant::STRING) {
1457
GDScriptCodeGenerator::Address type_stringname_addr = codegen.add_constant(Variant::STRING_NAME);
1458
1459
// Check StringName <-> String type equality.
1460
GDScriptCodeGenerator::Address tmp_comp_addr = codegen.add_temporary(equality_type);
1461
1462
codegen.generator->write_binary_operator(tmp_comp_addr, Variant::OP_EQUAL, p_type_addr, type_stringname_addr);
1463
codegen.generator->write_binary_operator(type_equality_addr, Variant::OP_OR, type_equality_addr, tmp_comp_addr);
1464
1465
codegen.generator->pop_temporary(); // Remove tmp_comp_addr from stack.
1466
} else if (literal_type == Variant::STRING_NAME) {
1467
GDScriptCodeGenerator::Address type_string_addr = codegen.add_constant(Variant::STRING);
1468
1469
// Check String <-> StringName type equality.
1470
GDScriptCodeGenerator::Address tmp_comp_addr = codegen.add_temporary(equality_type);
1471
1472
codegen.generator->write_binary_operator(tmp_comp_addr, Variant::OP_EQUAL, p_type_addr, type_string_addr);
1473
codegen.generator->write_binary_operator(type_equality_addr, Variant::OP_OR, type_equality_addr, tmp_comp_addr);
1474
1475
codegen.generator->pop_temporary(); // Remove tmp_comp_addr from stack.
1476
}
1477
1478
codegen.generator->write_and_left_operand(type_equality_addr);
1479
1480
// Get literal.
1481
GDScriptCodeGenerator::Address literal_addr = _parse_expression(codegen, r_error, p_pattern->literal);
1482
if (r_error) {
1483
return GDScriptCodeGenerator::Address();
1484
}
1485
1486
// Check value equality.
1487
GDScriptCodeGenerator::Address equality_addr = codegen.add_temporary(equality_type);
1488
codegen.generator->write_binary_operator(equality_addr, Variant::OP_EQUAL, p_value_addr, literal_addr);
1489
codegen.generator->write_and_right_operand(equality_addr);
1490
1491
// AND both together (reuse temporary location).
1492
codegen.generator->write_end_and(type_equality_addr);
1493
1494
codegen.generator->pop_temporary(); // Remove equality_addr from stack.
1495
1496
if (literal_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
1497
codegen.generator->pop_temporary();
1498
}
1499
1500
// If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead.
1501
if (p_is_nested) {
1502
// Use the previous value as target, since we only need one temporary variable.
1503
codegen.generator->write_and_right_operand(type_equality_addr);
1504
codegen.generator->write_end_and(p_previous_test);
1505
} else if (!p_is_first) {
1506
// Use the previous value as target, since we only need one temporary variable.
1507
codegen.generator->write_or_right_operand(type_equality_addr);
1508
codegen.generator->write_end_or(p_previous_test);
1509
} else {
1510
// Just assign this value to the accumulator temporary.
1511
codegen.generator->write_assign(p_previous_test, type_equality_addr);
1512
}
1513
codegen.generator->pop_temporary(); // Remove type_equality_addr.
1514
1515
return p_previous_test;
1516
} break;
1517
case GDScriptParser::PatternNode::PT_EXPRESSION: {
1518
if (p_is_nested) {
1519
codegen.generator->write_and_left_operand(p_previous_test);
1520
} else if (!p_is_first) {
1521
codegen.generator->write_or_left_operand(p_previous_test);
1522
}
1523
1524
GDScriptCodeGenerator::Address type_string_addr = codegen.add_constant(Variant::STRING);
1525
GDScriptCodeGenerator::Address type_stringname_addr = codegen.add_constant(Variant::STRING_NAME);
1526
1527
// Equality is always a boolean.
1528
GDScriptDataType equality_type;
1529
equality_type.has_type = true;
1530
equality_type.kind = GDScriptDataType::BUILTIN;
1531
equality_type.builtin_type = Variant::BOOL;
1532
1533
// Create the result temps first since it's the last to go away.
1534
GDScriptCodeGenerator::Address result_addr = codegen.add_temporary(equality_type);
1535
GDScriptCodeGenerator::Address equality_test_addr = codegen.add_temporary(equality_type);
1536
GDScriptCodeGenerator::Address stringy_comp_addr = codegen.add_temporary(equality_type);
1537
GDScriptCodeGenerator::Address stringy_comp_addr_2 = codegen.add_temporary(equality_type);
1538
GDScriptCodeGenerator::Address expr_type_addr = codegen.add_temporary();
1539
1540
// Evaluate expression.
1541
GDScriptCodeGenerator::Address expr_addr;
1542
expr_addr = _parse_expression(codegen, r_error, p_pattern->expression);
1543
if (r_error) {
1544
return GDScriptCodeGenerator::Address();
1545
}
1546
1547
// Evaluate expression type.
1548
Vector<GDScriptCodeGenerator::Address> typeof_args;
1549
typeof_args.push_back(expr_addr);
1550
codegen.generator->write_call_utility(expr_type_addr, "typeof", typeof_args);
1551
1552
// Check type equality.
1553
codegen.generator->write_binary_operator(result_addr, Variant::OP_EQUAL, p_type_addr, expr_type_addr);
1554
1555
// Check for String <-> StringName comparison.
1556
codegen.generator->write_binary_operator(stringy_comp_addr, Variant::OP_EQUAL, p_type_addr, type_string_addr);
1557
codegen.generator->write_binary_operator(stringy_comp_addr_2, Variant::OP_EQUAL, expr_type_addr, type_stringname_addr);
1558
codegen.generator->write_binary_operator(stringy_comp_addr, Variant::OP_AND, stringy_comp_addr, stringy_comp_addr_2);
1559
codegen.generator->write_binary_operator(result_addr, Variant::OP_OR, result_addr, stringy_comp_addr);
1560
1561
// Check for StringName <-> String comparison.
1562
codegen.generator->write_binary_operator(stringy_comp_addr, Variant::OP_EQUAL, p_type_addr, type_stringname_addr);
1563
codegen.generator->write_binary_operator(stringy_comp_addr_2, Variant::OP_EQUAL, expr_type_addr, type_string_addr);
1564
codegen.generator->write_binary_operator(stringy_comp_addr, Variant::OP_AND, stringy_comp_addr, stringy_comp_addr_2);
1565
codegen.generator->write_binary_operator(result_addr, Variant::OP_OR, result_addr, stringy_comp_addr);
1566
1567
codegen.generator->pop_temporary(); // Remove expr_type_addr from stack.
1568
codegen.generator->pop_temporary(); // Remove stringy_comp_addr_2 from stack.
1569
codegen.generator->pop_temporary(); // Remove stringy_comp_addr from stack.
1570
1571
codegen.generator->write_and_left_operand(result_addr);
1572
1573
// Check value equality.
1574
codegen.generator->write_binary_operator(equality_test_addr, Variant::OP_EQUAL, p_value_addr, expr_addr);
1575
codegen.generator->write_and_right_operand(equality_test_addr);
1576
1577
// AND both type and value equality.
1578
codegen.generator->write_end_and(result_addr);
1579
1580
// We don't need the expression temporary anymore.
1581
if (expr_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
1582
codegen.generator->pop_temporary();
1583
}
1584
codegen.generator->pop_temporary(); // Remove equality_test_addr from stack.
1585
1586
// If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead.
1587
if (p_is_nested) {
1588
// Use the previous value as target, since we only need one temporary variable.
1589
codegen.generator->write_and_right_operand(result_addr);
1590
codegen.generator->write_end_and(p_previous_test);
1591
} else if (!p_is_first) {
1592
// Use the previous value as target, since we only need one temporary variable.
1593
codegen.generator->write_or_right_operand(result_addr);
1594
codegen.generator->write_end_or(p_previous_test);
1595
} else {
1596
// Just assign this value to the accumulator temporary.
1597
codegen.generator->write_assign(p_previous_test, result_addr);
1598
}
1599
codegen.generator->pop_temporary(); // Remove temp result addr.
1600
1601
return p_previous_test;
1602
} break;
1603
case GDScriptParser::PatternNode::PT_ARRAY: {
1604
if (p_is_nested) {
1605
codegen.generator->write_and_left_operand(p_previous_test);
1606
} else if (!p_is_first) {
1607
codegen.generator->write_or_left_operand(p_previous_test);
1608
}
1609
// Get array type into constant map.
1610
GDScriptCodeGenerator::Address array_type_addr = codegen.add_constant((int)Variant::ARRAY);
1611
1612
// Equality is always a boolean.
1613
GDScriptDataType temp_type;
1614
temp_type.has_type = true;
1615
temp_type.kind = GDScriptDataType::BUILTIN;
1616
temp_type.builtin_type = Variant::BOOL;
1617
1618
// Check type equality.
1619
GDScriptCodeGenerator::Address result_addr = codegen.add_temporary(temp_type);
1620
codegen.generator->write_binary_operator(result_addr, Variant::OP_EQUAL, p_type_addr, array_type_addr);
1621
codegen.generator->write_and_left_operand(result_addr);
1622
1623
// Store pattern length in constant map.
1624
GDScriptCodeGenerator::Address array_length_addr = codegen.add_constant(p_pattern->rest_used ? p_pattern->array.size() - 1 : p_pattern->array.size());
1625
1626
// Get value length.
1627
temp_type.builtin_type = Variant::INT;
1628
GDScriptCodeGenerator::Address value_length_addr = codegen.add_temporary(temp_type);
1629
Vector<GDScriptCodeGenerator::Address> len_args;
1630
len_args.push_back(p_value_addr);
1631
codegen.generator->write_call_gdscript_utility(value_length_addr, "len", len_args);
1632
1633
// Test length compatibility.
1634
temp_type.builtin_type = Variant::BOOL;
1635
GDScriptCodeGenerator::Address length_compat_addr = codegen.add_temporary(temp_type);
1636
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);
1637
codegen.generator->write_and_right_operand(length_compat_addr);
1638
1639
// AND type and length check.
1640
codegen.generator->write_end_and(result_addr);
1641
1642
// Remove length temporaries.
1643
codegen.generator->pop_temporary();
1644
codegen.generator->pop_temporary();
1645
1646
// Create temporaries outside the loop so they can be reused.
1647
GDScriptCodeGenerator::Address element_addr = codegen.add_temporary();
1648
GDScriptCodeGenerator::Address element_type_addr = codegen.add_temporary();
1649
1650
// Evaluate element by element.
1651
for (int i = 0; i < p_pattern->array.size(); i++) {
1652
if (p_pattern->array[i]->pattern_type == GDScriptParser::PatternNode::PT_REST) {
1653
// Don't want to access an extra element of the user array.
1654
break;
1655
}
1656
1657
// 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).
1658
codegen.generator->write_and_left_operand(result_addr);
1659
1660
// Add index to constant map.
1661
GDScriptCodeGenerator::Address index_addr = codegen.add_constant(i);
1662
1663
// Get the actual element from the user-sent array.
1664
codegen.generator->write_get(element_addr, index_addr, p_value_addr);
1665
1666
// Also get type of element.
1667
Vector<GDScriptCodeGenerator::Address> typeof_args;
1668
typeof_args.push_back(element_addr);
1669
codegen.generator->write_call_utility(element_type_addr, "typeof", typeof_args);
1670
1671
// Try the pattern inside the element.
1672
result_addr = _parse_match_pattern(codegen, r_error, p_pattern->array[i], element_addr, element_type_addr, result_addr, false, true);
1673
if (r_error != OK) {
1674
return GDScriptCodeGenerator::Address();
1675
}
1676
1677
codegen.generator->write_and_right_operand(result_addr);
1678
codegen.generator->write_end_and(result_addr);
1679
}
1680
// Remove element temporaries.
1681
codegen.generator->pop_temporary();
1682
codegen.generator->pop_temporary();
1683
1684
// If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead.
1685
if (p_is_nested) {
1686
// Use the previous value as target, since we only need one temporary variable.
1687
codegen.generator->write_and_right_operand(result_addr);
1688
codegen.generator->write_end_and(p_previous_test);
1689
} else if (!p_is_first) {
1690
// Use the previous value as target, since we only need one temporary variable.
1691
codegen.generator->write_or_right_operand(result_addr);
1692
codegen.generator->write_end_or(p_previous_test);
1693
} else {
1694
// Just assign this value to the accumulator temporary.
1695
codegen.generator->write_assign(p_previous_test, result_addr);
1696
}
1697
codegen.generator->pop_temporary(); // Remove temp result addr.
1698
1699
return p_previous_test;
1700
} break;
1701
case GDScriptParser::PatternNode::PT_DICTIONARY: {
1702
if (p_is_nested) {
1703
codegen.generator->write_and_left_operand(p_previous_test);
1704
} else if (!p_is_first) {
1705
codegen.generator->write_or_left_operand(p_previous_test);
1706
}
1707
// Get dictionary type into constant map.
1708
GDScriptCodeGenerator::Address dict_type_addr = codegen.add_constant((int)Variant::DICTIONARY);
1709
1710
// Equality is always a boolean.
1711
GDScriptDataType temp_type;
1712
temp_type.has_type = true;
1713
temp_type.kind = GDScriptDataType::BUILTIN;
1714
temp_type.builtin_type = Variant::BOOL;
1715
1716
// Check type equality.
1717
GDScriptCodeGenerator::Address result_addr = codegen.add_temporary(temp_type);
1718
codegen.generator->write_binary_operator(result_addr, Variant::OP_EQUAL, p_type_addr, dict_type_addr);
1719
codegen.generator->write_and_left_operand(result_addr);
1720
1721
// Store pattern length in constant map.
1722
GDScriptCodeGenerator::Address dict_length_addr = codegen.add_constant(p_pattern->rest_used ? p_pattern->dictionary.size() - 1 : p_pattern->dictionary.size());
1723
1724
// Get user's dictionary length.
1725
temp_type.builtin_type = Variant::INT;
1726
GDScriptCodeGenerator::Address value_length_addr = codegen.add_temporary(temp_type);
1727
Vector<GDScriptCodeGenerator::Address> func_args;
1728
func_args.push_back(p_value_addr);
1729
codegen.generator->write_call_gdscript_utility(value_length_addr, "len", func_args);
1730
1731
// Test length compatibility.
1732
temp_type.builtin_type = Variant::BOOL;
1733
GDScriptCodeGenerator::Address length_compat_addr = codegen.add_temporary(temp_type);
1734
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);
1735
codegen.generator->write_and_right_operand(length_compat_addr);
1736
1737
// AND type and length check.
1738
codegen.generator->write_end_and(result_addr);
1739
1740
// Remove length temporaries.
1741
codegen.generator->pop_temporary();
1742
codegen.generator->pop_temporary();
1743
1744
// Create temporaries outside the loop so they can be reused.
1745
GDScriptCodeGenerator::Address element_addr = codegen.add_temporary();
1746
GDScriptCodeGenerator::Address element_type_addr = codegen.add_temporary();
1747
1748
// Evaluate element by element.
1749
for (int i = 0; i < p_pattern->dictionary.size(); i++) {
1750
const GDScriptParser::PatternNode::Pair &element = p_pattern->dictionary[i];
1751
if (element.value_pattern && element.value_pattern->pattern_type == GDScriptParser::PatternNode::PT_REST) {
1752
// Ignore rest pattern.
1753
break;
1754
}
1755
1756
// 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).
1757
codegen.generator->write_and_left_operand(result_addr);
1758
1759
// Get the pattern key.
1760
GDScriptCodeGenerator::Address pattern_key_addr = _parse_expression(codegen, r_error, element.key);
1761
if (r_error) {
1762
return GDScriptCodeGenerator::Address();
1763
}
1764
1765
// Check if pattern key exists in user's dictionary. This will be AND-ed with next result.
1766
func_args.clear();
1767
func_args.push_back(pattern_key_addr);
1768
codegen.generator->write_call(result_addr, p_value_addr, "has", func_args);
1769
1770
if (element.value_pattern != nullptr) {
1771
// 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).
1772
codegen.generator->write_and_left_operand(result_addr);
1773
1774
// Get actual value from user dictionary.
1775
codegen.generator->write_get(element_addr, pattern_key_addr, p_value_addr);
1776
1777
// Also get type of value.
1778
func_args.clear();
1779
func_args.push_back(element_addr);
1780
codegen.generator->write_call_utility(element_type_addr, "typeof", func_args);
1781
1782
// Try the pattern inside the value.
1783
result_addr = _parse_match_pattern(codegen, r_error, element.value_pattern, element_addr, element_type_addr, result_addr, false, true);
1784
if (r_error != OK) {
1785
return GDScriptCodeGenerator::Address();
1786
}
1787
codegen.generator->write_and_right_operand(result_addr);
1788
codegen.generator->write_end_and(result_addr);
1789
}
1790
1791
codegen.generator->write_and_right_operand(result_addr);
1792
codegen.generator->write_end_and(result_addr);
1793
1794
// Remove pattern key temporary.
1795
if (pattern_key_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
1796
codegen.generator->pop_temporary();
1797
}
1798
}
1799
1800
// Remove element temporaries.
1801
codegen.generator->pop_temporary();
1802
codegen.generator->pop_temporary();
1803
1804
// If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead.
1805
if (p_is_nested) {
1806
// Use the previous value as target, since we only need one temporary variable.
1807
codegen.generator->write_and_right_operand(result_addr);
1808
codegen.generator->write_end_and(p_previous_test);
1809
} else if (!p_is_first) {
1810
// Use the previous value as target, since we only need one temporary variable.
1811
codegen.generator->write_or_right_operand(result_addr);
1812
codegen.generator->write_end_or(p_previous_test);
1813
} else {
1814
// Just assign this value to the accumulator temporary.
1815
codegen.generator->write_assign(p_previous_test, result_addr);
1816
}
1817
codegen.generator->pop_temporary(); // Remove temp result addr.
1818
1819
return p_previous_test;
1820
} break;
1821
case GDScriptParser::PatternNode::PT_REST:
1822
// Do nothing.
1823
return p_previous_test;
1824
break;
1825
case GDScriptParser::PatternNode::PT_BIND: {
1826
if (p_is_nested) {
1827
codegen.generator->write_and_left_operand(p_previous_test);
1828
} else if (!p_is_first) {
1829
codegen.generator->write_or_left_operand(p_previous_test);
1830
}
1831
// Get the bind address.
1832
GDScriptCodeGenerator::Address bind = codegen.locals[p_pattern->bind->name];
1833
1834
// Assign value to bound variable.
1835
codegen.generator->write_assign(bind, p_value_addr);
1836
}
1837
[[fallthrough]]; // Act like matching anything too.
1838
case GDScriptParser::PatternNode::PT_WILDCARD:
1839
// If this is a fall through we don't want to do this again.
1840
if (p_pattern->pattern_type != GDScriptParser::PatternNode::PT_BIND) {
1841
if (p_is_nested) {
1842
codegen.generator->write_and_left_operand(p_previous_test);
1843
} else if (!p_is_first) {
1844
codegen.generator->write_or_left_operand(p_previous_test);
1845
}
1846
}
1847
// This matches anything so just do the same as `if(true)`.
1848
// If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead.
1849
if (p_is_nested) {
1850
// Use the operator with the `true` constant so it works as always matching.
1851
GDScriptCodeGenerator::Address constant = codegen.add_constant(true);
1852
codegen.generator->write_and_right_operand(constant);
1853
codegen.generator->write_end_and(p_previous_test);
1854
} else if (!p_is_first) {
1855
// Use the operator with the `true` constant so it works as always matching.
1856
GDScriptCodeGenerator::Address constant = codegen.add_constant(true);
1857
codegen.generator->write_or_right_operand(constant);
1858
codegen.generator->write_end_or(p_previous_test);
1859
} else {
1860
// Just assign this value to the accumulator temporary.
1861
codegen.generator->write_assign_true(p_previous_test);
1862
}
1863
return p_previous_test;
1864
}
1865
1866
_set_error("Compiler bug (please report): Reaching the end of pattern compilation without matching a pattern.", p_pattern);
1867
r_error = ERR_COMPILATION_FAILED;
1868
return p_previous_test;
1869
}
1870
1871
List<GDScriptCodeGenerator::Address> GDScriptCompiler::_add_block_locals(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block) {
1872
List<GDScriptCodeGenerator::Address> addresses;
1873
for (int i = 0; i < p_block->locals.size(); i++) {
1874
if (p_block->locals[i].type == GDScriptParser::SuiteNode::Local::PARAMETER || p_block->locals[i].type == GDScriptParser::SuiteNode::Local::FOR_VARIABLE) {
1875
// Parameters are added directly from function and loop variables are declared explicitly.
1876
continue;
1877
}
1878
addresses.push_back(codegen.add_local(p_block->locals[i].name, _gdtype_from_datatype(p_block->locals[i].get_datatype(), codegen.script)));
1879
}
1880
return addresses;
1881
}
1882
1883
// Avoid keeping in the stack long-lived references to objects, which may prevent `RefCounted` objects from being freed.
1884
void GDScriptCompiler::_clear_block_locals(CodeGen &codegen, const List<GDScriptCodeGenerator::Address> &p_locals) {
1885
for (const GDScriptCodeGenerator::Address &local : p_locals) {
1886
if (local.type.can_contain_object()) {
1887
codegen.generator->clear_address(local);
1888
}
1889
}
1890
}
1891
1892
Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, bool p_add_locals, bool p_clear_locals) {
1893
Error err = OK;
1894
GDScriptCodeGenerator *gen = codegen.generator;
1895
List<GDScriptCodeGenerator::Address> block_locals;
1896
1897
gen->clear_temporaries();
1898
codegen.start_block();
1899
1900
if (p_add_locals) {
1901
block_locals = _add_block_locals(codegen, p_block);
1902
}
1903
1904
for (int i = 0; i < p_block->statements.size(); i++) {
1905
const GDScriptParser::Node *s = p_block->statements[i];
1906
1907
gen->write_newline(s->start_line);
1908
1909
switch (s->type) {
1910
case GDScriptParser::Node::MATCH: {
1911
const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(s);
1912
1913
codegen.start_block(); // Add an extra block, since @special locals belong to the match scope.
1914
1915
// Evaluate the match expression.
1916
GDScriptCodeGenerator::Address value = codegen.add_local("@match_value", _gdtype_from_datatype(match->test->get_datatype(), codegen.script));
1917
GDScriptCodeGenerator::Address value_expr = _parse_expression(codegen, err, match->test);
1918
if (err) {
1919
return err;
1920
}
1921
1922
// Assign to local.
1923
// TODO: This can be improved by passing the target to parse_expression().
1924
gen->write_assign(value, value_expr);
1925
1926
if (value_expr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
1927
codegen.generator->pop_temporary();
1928
}
1929
1930
// Then, let's save the type of the value in the stack too, so we can reuse for later comparisons.
1931
GDScriptDataType typeof_type;
1932
typeof_type.has_type = true;
1933
typeof_type.kind = GDScriptDataType::BUILTIN;
1934
typeof_type.builtin_type = Variant::INT;
1935
GDScriptCodeGenerator::Address type = codegen.add_local("@match_type", typeof_type);
1936
1937
Vector<GDScriptCodeGenerator::Address> typeof_args;
1938
typeof_args.push_back(value);
1939
gen->write_call_utility(type, "typeof", typeof_args);
1940
1941
// Now we can actually start testing.
1942
// For each branch.
1943
for (int j = 0; j < match->branches.size(); j++) {
1944
if (j > 0) {
1945
// Use `else` to not check the next branch after matching.
1946
gen->write_else();
1947
}
1948
1949
const GDScriptParser::MatchBranchNode *branch = match->branches[j];
1950
1951
codegen.start_block(); // Add an extra block, since binds belong to the match branch scope.
1952
1953
// Add locals in block before patterns, so temporaries don't use the stack address for binds.
1954
List<GDScriptCodeGenerator::Address> branch_locals = _add_block_locals(codegen, branch->block);
1955
1956
gen->write_newline(branch->start_line);
1957
1958
// For each pattern in branch.
1959
GDScriptCodeGenerator::Address pattern_result = codegen.add_temporary();
1960
for (int k = 0; k < branch->patterns.size(); k++) {
1961
pattern_result = _parse_match_pattern(codegen, err, branch->patterns[k], value, type, pattern_result, k == 0, false);
1962
if (err != OK) {
1963
return err;
1964
}
1965
}
1966
1967
// If there's a guard, check its condition too.
1968
if (branch->guard_body != nullptr) {
1969
// Do this first so the guard does not run unless the pattern matched.
1970
gen->write_and_left_operand(pattern_result);
1971
1972
// Don't actually use the block for the guard.
1973
// 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.
1974
GDScriptCodeGenerator::Address guard_result = _parse_expression(codegen, err, static_cast<GDScriptParser::ExpressionNode *>(branch->guard_body->statements[0]));
1975
if (err) {
1976
return err;
1977
}
1978
1979
gen->write_and_right_operand(guard_result);
1980
gen->write_end_and(pattern_result);
1981
1982
if (guard_result.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
1983
codegen.generator->pop_temporary();
1984
}
1985
}
1986
1987
// Check if pattern did match.
1988
gen->write_if(pattern_result);
1989
1990
// Remove the result from stack.
1991
gen->pop_temporary();
1992
1993
// Parse the branch block.
1994
err = _parse_block(codegen, branch->block, false); // Don't add locals again.
1995
if (err) {
1996
return err;
1997
}
1998
1999
_clear_block_locals(codegen, branch_locals);
2000
2001
codegen.end_block(); // Get out of extra block for binds.
2002
}
2003
2004
// End all nested `if`s.
2005
for (int j = 0; j < match->branches.size(); j++) {
2006
gen->write_endif();
2007
}
2008
2009
codegen.end_block(); // Get out of extra block for match's @special locals.
2010
} break;
2011
case GDScriptParser::Node::IF: {
2012
const GDScriptParser::IfNode *if_n = static_cast<const GDScriptParser::IfNode *>(s);
2013
GDScriptCodeGenerator::Address condition = _parse_expression(codegen, err, if_n->condition);
2014
if (err) {
2015
return err;
2016
}
2017
2018
gen->write_if(condition);
2019
2020
if (condition.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2021
codegen.generator->pop_temporary();
2022
}
2023
2024
err = _parse_block(codegen, if_n->true_block);
2025
if (err) {
2026
return err;
2027
}
2028
2029
if (if_n->false_block) {
2030
gen->write_else();
2031
2032
err = _parse_block(codegen, if_n->false_block);
2033
if (err) {
2034
return err;
2035
}
2036
}
2037
2038
gen->write_endif();
2039
} break;
2040
case GDScriptParser::Node::FOR: {
2041
const GDScriptParser::ForNode *for_n = static_cast<const GDScriptParser::ForNode *>(s);
2042
2043
// Add an extra block, since the iterator and @special locals belong to the loop scope.
2044
// Also we use custom logic to clear block locals.
2045
codegen.start_block();
2046
2047
GDScriptCodeGenerator::Address iterator = codegen.add_local(for_n->variable->name, _gdtype_from_datatype(for_n->variable->get_datatype(), codegen.script));
2048
2049
// Optimize `range()` call to not allocate an array.
2050
GDScriptParser::CallNode *range_call = nullptr;
2051
if (for_n->list && for_n->list->type == GDScriptParser::Node::CALL) {
2052
GDScriptParser::CallNode *call = static_cast<GDScriptParser::CallNode *>(for_n->list);
2053
if (call->get_callee_type() == GDScriptParser::Node::IDENTIFIER) {
2054
if (static_cast<GDScriptParser::IdentifierNode *>(call->callee)->name == "range") {
2055
range_call = call;
2056
}
2057
}
2058
}
2059
2060
gen->start_for(iterator.type, _gdtype_from_datatype(for_n->list->get_datatype(), codegen.script), range_call != nullptr);
2061
2062
if (range_call != nullptr) {
2063
Vector<GDScriptCodeGenerator::Address> args;
2064
args.resize(range_call->arguments.size());
2065
2066
for (int j = 0; j < args.size(); j++) {
2067
args.write[j] = _parse_expression(codegen, err, range_call->arguments[j]);
2068
if (err) {
2069
return err;
2070
}
2071
}
2072
2073
switch (args.size()) {
2074
case 1:
2075
gen->write_for_range_assignment(codegen.add_constant(0), args[0], codegen.add_constant(1));
2076
break;
2077
case 2:
2078
gen->write_for_range_assignment(args[0], args[1], codegen.add_constant(1));
2079
break;
2080
case 3:
2081
gen->write_for_range_assignment(args[0], args[1], args[2]);
2082
break;
2083
default:
2084
_set_error(R"*(Analyzer bug: Wrong "range()" argument count.)*", range_call);
2085
return ERR_BUG;
2086
}
2087
2088
for (int j = 0; j < args.size(); j++) {
2089
if (args[j].mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2090
codegen.generator->pop_temporary();
2091
}
2092
}
2093
} else {
2094
GDScriptCodeGenerator::Address list = _parse_expression(codegen, err, for_n->list);
2095
if (err) {
2096
return err;
2097
}
2098
2099
gen->write_for_list_assignment(list);
2100
2101
if (list.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2102
codegen.generator->pop_temporary();
2103
}
2104
}
2105
2106
gen->write_for(iterator, for_n->use_conversion_assign, range_call != nullptr);
2107
2108
// Loop variables must be cleared even when `break`/`continue` is used.
2109
List<GDScriptCodeGenerator::Address> loop_locals = _add_block_locals(codegen, for_n->loop);
2110
2111
//_clear_block_locals(codegen, loop_locals); // Inside loop, before block - for `continue`. // TODO
2112
2113
err = _parse_block(codegen, for_n->loop, false); // Don't add locals again.
2114
if (err) {
2115
return err;
2116
}
2117
2118
gen->write_endfor(range_call != nullptr);
2119
2120
_clear_block_locals(codegen, loop_locals); // Outside loop, after block - for `break` and normal exit.
2121
2122
codegen.end_block(); // Get out of extra block for loop iterator, @special locals, and custom locals clearing.
2123
} break;
2124
case GDScriptParser::Node::WHILE: {
2125
const GDScriptParser::WhileNode *while_n = static_cast<const GDScriptParser::WhileNode *>(s);
2126
2127
codegen.start_block(); // Add an extra block, since we use custom logic to clear block locals.
2128
2129
gen->start_while_condition();
2130
2131
GDScriptCodeGenerator::Address condition = _parse_expression(codegen, err, while_n->condition);
2132
if (err) {
2133
return err;
2134
}
2135
2136
gen->write_while(condition);
2137
2138
if (condition.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2139
codegen.generator->pop_temporary();
2140
}
2141
2142
// Loop variables must be cleared even when `break`/`continue` is used.
2143
List<GDScriptCodeGenerator::Address> loop_locals = _add_block_locals(codegen, while_n->loop);
2144
2145
//_clear_block_locals(codegen, loop_locals); // Inside loop, before block - for `continue`. // TODO
2146
2147
err = _parse_block(codegen, while_n->loop, false); // Don't add locals again.
2148
if (err) {
2149
return err;
2150
}
2151
2152
gen->write_endwhile();
2153
2154
_clear_block_locals(codegen, loop_locals); // Outside loop, after block - for `break` and normal exit.
2155
2156
codegen.end_block(); // Get out of extra block for custom locals clearing.
2157
} break;
2158
case GDScriptParser::Node::BREAK: {
2159
gen->write_break();
2160
} break;
2161
case GDScriptParser::Node::CONTINUE: {
2162
gen->write_continue();
2163
} break;
2164
case GDScriptParser::Node::RETURN: {
2165
const GDScriptParser::ReturnNode *return_n = static_cast<const GDScriptParser::ReturnNode *>(s);
2166
2167
GDScriptCodeGenerator::Address return_value;
2168
2169
if (return_n->return_value != nullptr) {
2170
return_value = _parse_expression(codegen, err, return_n->return_value);
2171
if (err) {
2172
return err;
2173
}
2174
}
2175
2176
if (return_n->void_return) {
2177
// Always return "null", even if the expression is a call to a void function.
2178
gen->write_return(codegen.add_constant(Variant()));
2179
} else {
2180
gen->write_return(return_value);
2181
}
2182
if (return_value.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2183
codegen.generator->pop_temporary();
2184
}
2185
} break;
2186
case GDScriptParser::Node::ASSERT: {
2187
#ifdef DEBUG_ENABLED
2188
const GDScriptParser::AssertNode *as = static_cast<const GDScriptParser::AssertNode *>(s);
2189
2190
GDScriptCodeGenerator::Address condition = _parse_expression(codegen, err, as->condition);
2191
if (err) {
2192
return err;
2193
}
2194
2195
GDScriptCodeGenerator::Address message;
2196
2197
if (as->message) {
2198
message = _parse_expression(codegen, err, as->message);
2199
if (err) {
2200
return err;
2201
}
2202
}
2203
gen->write_assert(condition, message);
2204
2205
if (condition.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2206
codegen.generator->pop_temporary();
2207
}
2208
if (message.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2209
codegen.generator->pop_temporary();
2210
}
2211
#endif
2212
} break;
2213
case GDScriptParser::Node::BREAKPOINT: {
2214
#ifdef DEBUG_ENABLED
2215
gen->write_breakpoint();
2216
#endif
2217
} break;
2218
case GDScriptParser::Node::VARIABLE: {
2219
const GDScriptParser::VariableNode *lv = static_cast<const GDScriptParser::VariableNode *>(s);
2220
// Should be already in stack when the block began.
2221
GDScriptCodeGenerator::Address local = codegen.locals[lv->identifier->name];
2222
GDScriptDataType local_type = _gdtype_from_datatype(lv->get_datatype(), codegen.script);
2223
2224
bool initialized = false;
2225
if (lv->initializer != nullptr) {
2226
GDScriptCodeGenerator::Address src_address = _parse_expression(codegen, err, lv->initializer);
2227
if (err) {
2228
return err;
2229
}
2230
if (lv->use_conversion_assign) {
2231
gen->write_assign_with_conversion(local, src_address);
2232
} else {
2233
gen->write_assign(local, src_address);
2234
}
2235
if (src_address.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2236
codegen.generator->pop_temporary();
2237
}
2238
initialized = true;
2239
} else if ((local_type.has_type && local_type.kind == GDScriptDataType::BUILTIN) || codegen.generator->is_local_dirty(local)) {
2240
// Initialize with default for the type. Built-in types must always be cleared (they cannot be `null`).
2241
// Objects and untyped variables are assigned to `null` only if the stack address has been reused and not cleared.
2242
codegen.generator->clear_address(local);
2243
initialized = true;
2244
}
2245
2246
// Don't check `is_local_dirty()` since the variable must be assigned to `null` **on each iteration**.
2247
if (!initialized && p_block->is_in_loop) {
2248
codegen.generator->clear_address(local);
2249
}
2250
} break;
2251
case GDScriptParser::Node::CONSTANT: {
2252
// Local constants.
2253
const GDScriptParser::ConstantNode *lc = static_cast<const GDScriptParser::ConstantNode *>(s);
2254
if (!lc->initializer->is_constant) {
2255
_set_error("Local constant must have a constant value as initializer.", lc->initializer);
2256
return ERR_PARSE_ERROR;
2257
}
2258
2259
codegen.add_local_constant(lc->identifier->name, lc->initializer->reduced_value);
2260
} break;
2261
case GDScriptParser::Node::PASS:
2262
// Nothing to do.
2263
break;
2264
default: {
2265
// Expression.
2266
if (s->is_expression()) {
2267
GDScriptCodeGenerator::Address expr = _parse_expression(codegen, err, static_cast<const GDScriptParser::ExpressionNode *>(s), true);
2268
if (err) {
2269
return err;
2270
}
2271
if (expr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2272
codegen.generator->pop_temporary();
2273
}
2274
} else {
2275
_set_error("Compiler bug (please report): unexpected node in parse tree while parsing statement.", s); // Unreachable code.
2276
return ERR_INVALID_DATA;
2277
}
2278
} break;
2279
}
2280
2281
gen->clear_temporaries();
2282
}
2283
2284
if (p_add_locals && p_clear_locals) {
2285
_clear_block_locals(codegen, block_locals);
2286
}
2287
2288
codegen.end_block();
2289
return OK;
2290
}
2291
2292
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) {
2293
r_error = OK;
2294
CodeGen codegen;
2295
codegen.generator = memnew(GDScriptByteCodeGenerator);
2296
2297
codegen.class_node = p_class;
2298
codegen.script = p_script;
2299
codegen.function_node = p_func;
2300
2301
StringName func_name;
2302
bool is_abstract = false;
2303
bool is_static = false;
2304
Variant rpc_config;
2305
GDScriptDataType return_type;
2306
return_type.has_type = true;
2307
return_type.kind = GDScriptDataType::BUILTIN;
2308
return_type.builtin_type = Variant::NIL;
2309
2310
if (p_func) {
2311
if (p_func->identifier) {
2312
func_name = p_func->identifier->name;
2313
} else {
2314
func_name = "<anonymous lambda>";
2315
}
2316
is_abstract = p_func->is_abstract;
2317
is_static = p_func->is_static;
2318
rpc_config = p_func->rpc_config;
2319
return_type = _gdtype_from_datatype(p_func->get_datatype(), p_script);
2320
} else {
2321
if (p_for_ready) {
2322
func_name = "@implicit_ready";
2323
} else {
2324
func_name = "@implicit_new";
2325
}
2326
}
2327
2328
MethodInfo method_info;
2329
2330
codegen.function_name = func_name;
2331
method_info.name = func_name;
2332
codegen.is_static = is_static;
2333
if (is_abstract) {
2334
method_info.flags |= METHOD_FLAG_VIRTUAL_REQUIRED;
2335
}
2336
if (is_static) {
2337
method_info.flags |= METHOD_FLAG_STATIC;
2338
}
2339
codegen.generator->write_start(p_script, func_name, is_static, rpc_config, return_type);
2340
2341
int optional_parameters = 0;
2342
GDScriptCodeGenerator::Address vararg_addr;
2343
2344
if (p_func) {
2345
for (int i = 0; i < p_func->parameters.size(); i++) {
2346
const GDScriptParser::ParameterNode *parameter = p_func->parameters[i];
2347
GDScriptDataType par_type = _gdtype_from_datatype(parameter->get_datatype(), p_script);
2348
uint32_t par_addr = codegen.generator->add_parameter(parameter->identifier->name, parameter->initializer != nullptr, par_type);
2349
codegen.parameters[parameter->identifier->name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::FUNCTION_PARAMETER, par_addr, par_type);
2350
2351
method_info.arguments.push_back(parameter->get_datatype().to_property_info(parameter->identifier->name));
2352
2353
if (parameter->initializer != nullptr) {
2354
optional_parameters++;
2355
}
2356
}
2357
2358
if (p_func->is_vararg()) {
2359
vararg_addr = codegen.add_local(p_func->rest_parameter->identifier->name, _gdtype_from_datatype(p_func->rest_parameter->get_datatype(), codegen.script));
2360
method_info.flags |= METHOD_FLAG_VARARG;
2361
}
2362
2363
method_info.default_arguments.append_array(p_func->default_arg_values);
2364
}
2365
2366
// Parse initializer if applies.
2367
bool is_implicit_initializer = !p_for_ready && !p_func && !p_for_lambda;
2368
bool is_initializer = p_func && !p_for_lambda && p_func->identifier->name == GDScriptLanguage::get_singleton()->strings._init;
2369
bool is_implicit_ready = !p_func && p_for_ready;
2370
2371
if (!p_for_lambda && is_implicit_initializer) {
2372
// Initialize the default values for typed variables before anything.
2373
// This avoids crashes if they are accessed with validated calls before being properly initialized.
2374
// It may happen with out-of-order access or with `@onready` variables.
2375
for (const GDScriptParser::ClassNode::Member &member : p_class->members) {
2376
if (member.type != GDScriptParser::ClassNode::Member::VARIABLE) {
2377
continue;
2378
}
2379
2380
const GDScriptParser::VariableNode *field = member.variable;
2381
if (field->is_static) {
2382
continue;
2383
}
2384
2385
GDScriptDataType field_type = _gdtype_from_datatype(field->get_datatype(), codegen.script);
2386
if (field_type.has_type) {
2387
codegen.generator->write_newline(field->start_line);
2388
2389
GDScriptCodeGenerator::Address dst_address(GDScriptCodeGenerator::Address::MEMBER, codegen.script->member_indices[field->identifier->name].index, field_type);
2390
2391
if (field_type.builtin_type == Variant::ARRAY && field_type.has_container_element_type(0)) {
2392
codegen.generator->write_construct_typed_array(dst_address, field_type.get_container_element_type(0), Vector<GDScriptCodeGenerator::Address>());
2393
} else if (field_type.builtin_type == Variant::DICTIONARY && field_type.has_container_element_types()) {
2394
codegen.generator->write_construct_typed_dictionary(dst_address, field_type.get_container_element_type_or_variant(0),
2395
field_type.get_container_element_type_or_variant(1), Vector<GDScriptCodeGenerator::Address>());
2396
} else if (field_type.kind == GDScriptDataType::BUILTIN) {
2397
codegen.generator->write_construct(dst_address, field_type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
2398
}
2399
// The `else` branch is for objects, in such case we leave it as `null`.
2400
}
2401
}
2402
}
2403
2404
if (!p_for_lambda && (is_implicit_initializer || is_implicit_ready)) {
2405
// Initialize class fields.
2406
for (int i = 0; i < p_class->members.size(); i++) {
2407
if (p_class->members[i].type != GDScriptParser::ClassNode::Member::VARIABLE) {
2408
continue;
2409
}
2410
const GDScriptParser::VariableNode *field = p_class->members[i].variable;
2411
if (field->is_static) {
2412
continue;
2413
}
2414
2415
if (field->onready != is_implicit_ready) {
2416
// Only initialize in `@implicit_ready()`.
2417
continue;
2418
}
2419
2420
if (field->initializer) {
2421
codegen.generator->write_newline(field->initializer->start_line);
2422
2423
GDScriptCodeGenerator::Address src_address = _parse_expression(codegen, r_error, field->initializer, false, true);
2424
if (r_error) {
2425
memdelete(codegen.generator);
2426
return nullptr;
2427
}
2428
2429
GDScriptDataType field_type = _gdtype_from_datatype(field->get_datatype(), codegen.script);
2430
GDScriptCodeGenerator::Address dst_address(GDScriptCodeGenerator::Address::MEMBER, codegen.script->member_indices[field->identifier->name].index, field_type);
2431
2432
if (field->use_conversion_assign) {
2433
codegen.generator->write_assign_with_conversion(dst_address, src_address);
2434
} else {
2435
codegen.generator->write_assign(dst_address, src_address);
2436
}
2437
if (src_address.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2438
codegen.generator->pop_temporary();
2439
}
2440
}
2441
}
2442
}
2443
2444
// Parse default argument code if applies.
2445
if (p_func) {
2446
if (optional_parameters > 0) {
2447
codegen.generator->start_parameters();
2448
for (int i = p_func->parameters.size() - optional_parameters; i < p_func->parameters.size(); i++) {
2449
const GDScriptParser::ParameterNode *parameter = p_func->parameters[i];
2450
GDScriptCodeGenerator::Address src_addr = _parse_expression(codegen, r_error, parameter->initializer);
2451
if (r_error) {
2452
memdelete(codegen.generator);
2453
return nullptr;
2454
}
2455
GDScriptCodeGenerator::Address dst_addr = codegen.parameters[parameter->identifier->name];
2456
codegen.generator->write_assign_default_parameter(dst_addr, src_addr, parameter->use_conversion_assign);
2457
if (src_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2458
codegen.generator->pop_temporary();
2459
}
2460
}
2461
codegen.generator->end_parameters();
2462
}
2463
2464
// No need to reset locals at the end of the function, the stack will be cleared anyway.
2465
r_error = _parse_block(codegen, p_func->body, true, false);
2466
if (r_error) {
2467
memdelete(codegen.generator);
2468
return nullptr;
2469
}
2470
}
2471
2472
#ifdef DEBUG_ENABLED
2473
if (EngineDebugger::is_active()) {
2474
String signature;
2475
// Path.
2476
if (!p_script->get_script_path().is_empty()) {
2477
signature += p_script->get_script_path();
2478
}
2479
// Location.
2480
if (p_func) {
2481
signature += "::" + itos(p_func->body->start_line);
2482
} else {
2483
signature += "::0";
2484
}
2485
2486
// Function and class.
2487
2488
if (p_class->identifier) {
2489
signature += "::" + String(p_class->identifier->name) + "." + String(func_name);
2490
} else {
2491
signature += "::" + String(func_name);
2492
}
2493
2494
if (p_for_lambda) {
2495
signature += "(lambda)";
2496
}
2497
2498
codegen.generator->set_signature(signature);
2499
}
2500
#endif
2501
2502
if (p_func) {
2503
codegen.generator->set_initial_line(p_func->start_line);
2504
} else {
2505
codegen.generator->set_initial_line(0);
2506
}
2507
2508
GDScriptFunction *gd_function = codegen.generator->write_end();
2509
2510
if (is_initializer) {
2511
p_script->initializer = gd_function;
2512
} else if (is_implicit_initializer) {
2513
p_script->implicit_initializer = gd_function;
2514
} else if (is_implicit_ready) {
2515
p_script->implicit_ready = gd_function;
2516
}
2517
2518
if (p_func) {
2519
// If no `return` statement, then return type is `void`, not `Variant`.
2520
if (p_func->body->has_return) {
2521
gd_function->return_type = _gdtype_from_datatype(p_func->get_datatype(), p_script);
2522
method_info.return_val = p_func->get_datatype().to_property_info(String());
2523
} else {
2524
gd_function->return_type = GDScriptDataType();
2525
gd_function->return_type.has_type = true;
2526
gd_function->return_type.kind = GDScriptDataType::BUILTIN;
2527
gd_function->return_type.builtin_type = Variant::NIL;
2528
}
2529
2530
if (p_func->is_vararg()) {
2531
gd_function->_vararg_index = vararg_addr.address;
2532
}
2533
}
2534
2535
gd_function->method_info = method_info;
2536
2537
if (!is_implicit_initializer && !is_implicit_ready && !p_for_lambda) {
2538
p_script->member_functions[func_name] = gd_function;
2539
}
2540
2541
memdelete(codegen.generator);
2542
2543
return gd_function;
2544
}
2545
2546
GDScriptFunction *GDScriptCompiler::_make_static_initializer(Error &r_error, GDScript *p_script, const GDScriptParser::ClassNode *p_class) {
2547
r_error = OK;
2548
CodeGen codegen;
2549
codegen.generator = memnew(GDScriptByteCodeGenerator);
2550
2551
codegen.class_node = p_class;
2552
codegen.script = p_script;
2553
2554
StringName func_name = SNAME("@static_initializer");
2555
bool is_static = true;
2556
Variant rpc_config;
2557
GDScriptDataType return_type;
2558
return_type.has_type = true;
2559
return_type.kind = GDScriptDataType::BUILTIN;
2560
return_type.builtin_type = Variant::NIL;
2561
2562
codegen.function_name = func_name;
2563
codegen.is_static = is_static;
2564
codegen.generator->write_start(p_script, func_name, is_static, rpc_config, return_type);
2565
2566
// The static initializer is always called on the same class where the static variables are defined,
2567
// so the CLASS address (current class) can be used instead of `codegen.add_constant(p_script)`.
2568
GDScriptCodeGenerator::Address class_addr(GDScriptCodeGenerator::Address::CLASS);
2569
2570
// Initialize the default values for typed variables before anything.
2571
// This avoids crashes if they are accessed with validated calls before being properly initialized.
2572
// It may happen with out-of-order access or with `@onready` variables.
2573
for (const GDScriptParser::ClassNode::Member &member : p_class->members) {
2574
if (member.type != GDScriptParser::ClassNode::Member::VARIABLE) {
2575
continue;
2576
}
2577
2578
const GDScriptParser::VariableNode *field = member.variable;
2579
if (!field->is_static) {
2580
continue;
2581
}
2582
2583
GDScriptDataType field_type = _gdtype_from_datatype(field->get_datatype(), codegen.script);
2584
if (field_type.has_type) {
2585
codegen.generator->write_newline(field->start_line);
2586
2587
if (field_type.builtin_type == Variant::ARRAY && field_type.has_container_element_type(0)) {
2588
GDScriptCodeGenerator::Address temp = codegen.add_temporary(field_type);
2589
codegen.generator->write_construct_typed_array(temp, field_type.get_container_element_type(0), Vector<GDScriptCodeGenerator::Address>());
2590
codegen.generator->write_set_static_variable(temp, class_addr, p_script->static_variables_indices[field->identifier->name].index);
2591
codegen.generator->pop_temporary();
2592
} else if (field_type.builtin_type == Variant::DICTIONARY && field_type.has_container_element_types()) {
2593
GDScriptCodeGenerator::Address temp = codegen.add_temporary(field_type);
2594
codegen.generator->write_construct_typed_dictionary(temp, field_type.get_container_element_type_or_variant(0),
2595
field_type.get_container_element_type_or_variant(1), Vector<GDScriptCodeGenerator::Address>());
2596
codegen.generator->write_set_static_variable(temp, class_addr, p_script->static_variables_indices[field->identifier->name].index);
2597
codegen.generator->pop_temporary();
2598
} else if (field_type.kind == GDScriptDataType::BUILTIN) {
2599
GDScriptCodeGenerator::Address temp = codegen.add_temporary(field_type);
2600
codegen.generator->write_construct(temp, field_type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
2601
codegen.generator->write_set_static_variable(temp, class_addr, p_script->static_variables_indices[field->identifier->name].index);
2602
codegen.generator->pop_temporary();
2603
}
2604
// The `else` branch is for objects, in such case we leave it as `null`.
2605
}
2606
}
2607
2608
for (int i = 0; i < p_class->members.size(); i++) {
2609
// Initialize static fields.
2610
if (p_class->members[i].type != GDScriptParser::ClassNode::Member::VARIABLE) {
2611
continue;
2612
}
2613
const GDScriptParser::VariableNode *field = p_class->members[i].variable;
2614
if (!field->is_static) {
2615
continue;
2616
}
2617
2618
if (field->initializer) {
2619
codegen.generator->write_newline(field->initializer->start_line);
2620
2621
GDScriptCodeGenerator::Address src_address = _parse_expression(codegen, r_error, field->initializer, false, true);
2622
if (r_error) {
2623
memdelete(codegen.generator);
2624
return nullptr;
2625
}
2626
2627
GDScriptDataType field_type = _gdtype_from_datatype(field->get_datatype(), codegen.script);
2628
GDScriptCodeGenerator::Address temp = codegen.add_temporary(field_type);
2629
2630
if (field->use_conversion_assign) {
2631
codegen.generator->write_assign_with_conversion(temp, src_address);
2632
} else {
2633
codegen.generator->write_assign(temp, src_address);
2634
}
2635
if (src_address.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
2636
codegen.generator->pop_temporary();
2637
}
2638
2639
codegen.generator->write_set_static_variable(temp, class_addr, p_script->static_variables_indices[field->identifier->name].index);
2640
codegen.generator->pop_temporary();
2641
}
2642
}
2643
2644
if (p_script->has_method(GDScriptLanguage::get_singleton()->strings._static_init)) {
2645
codegen.generator->write_newline(p_class->start_line);
2646
codegen.generator->write_call(GDScriptCodeGenerator::Address(), class_addr, GDScriptLanguage::get_singleton()->strings._static_init, Vector<GDScriptCodeGenerator::Address>());
2647
}
2648
2649
#ifdef DEBUG_ENABLED
2650
if (EngineDebugger::is_active()) {
2651
String signature;
2652
// Path.
2653
if (!p_script->get_script_path().is_empty()) {
2654
signature += p_script->get_script_path();
2655
}
2656
// Location.
2657
signature += "::0";
2658
2659
// Function and class.
2660
2661
if (p_class->identifier) {
2662
signature += "::" + String(p_class->identifier->name) + "." + String(func_name);
2663
} else {
2664
signature += "::" + String(func_name);
2665
}
2666
2667
codegen.generator->set_signature(signature);
2668
}
2669
#endif
2670
2671
codegen.generator->set_initial_line(p_class->start_line);
2672
2673
GDScriptFunction *gd_function = codegen.generator->write_end();
2674
2675
memdelete(codegen.generator);
2676
2677
return gd_function;
2678
}
2679
2680
Error GDScriptCompiler::_parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter) {
2681
Error err = OK;
2682
2683
GDScriptParser::FunctionNode *function;
2684
2685
if (p_is_setter) {
2686
function = p_variable->setter;
2687
} else {
2688
function = p_variable->getter;
2689
}
2690
2691
_parse_function(err, p_script, p_class, function);
2692
2693
return err;
2694
}
2695
2696
// Prepares given script, and inner class scripts, for compilation. It populates class members and
2697
// initializes method RPC info for its base classes first, then for itself, then for inner classes.
2698
// WARNING: This function cannot initiate compilation of other classes, or it will result in
2699
// cyclic dependency issues.
2700
Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
2701
if (parsed_classes.has(p_script)) {
2702
return OK;
2703
}
2704
2705
if (parsing_classes.has(p_script)) {
2706
String class_name = p_class->identifier ? String(p_class->identifier->name) : p_class->fqcn;
2707
_set_error(vformat(R"(Cyclic class reference for "%s".)", class_name), p_class);
2708
return ERR_PARSE_ERROR;
2709
}
2710
2711
parsing_classes.insert(p_script);
2712
2713
p_script->clearing = true;
2714
2715
p_script->cancel_pending_functions(true);
2716
2717
p_script->native = Ref<GDScriptNativeClass>();
2718
p_script->base = Ref<GDScript>();
2719
p_script->_base = nullptr;
2720
p_script->members.clear();
2721
2722
// This makes possible to clear script constants and member_functions without heap-use-after-free errors.
2723
HashMap<StringName, Variant> constants;
2724
for (const KeyValue<StringName, Variant> &E : p_script->constants) {
2725
constants.insert(E.key, E.value);
2726
}
2727
p_script->constants.clear();
2728
constants.clear();
2729
HashMap<StringName, GDScriptFunction *> member_functions;
2730
for (const KeyValue<StringName, GDScriptFunction *> &E : p_script->member_functions) {
2731
member_functions.insert(E.key, E.value);
2732
}
2733
p_script->member_functions.clear();
2734
for (const KeyValue<StringName, GDScriptFunction *> &E : member_functions) {
2735
memdelete(E.value);
2736
}
2737
member_functions.clear();
2738
2739
p_script->static_variables.clear();
2740
2741
if (p_script->implicit_initializer) {
2742
memdelete(p_script->implicit_initializer);
2743
}
2744
if (p_script->implicit_ready) {
2745
memdelete(p_script->implicit_ready);
2746
}
2747
if (p_script->static_initializer) {
2748
memdelete(p_script->static_initializer);
2749
}
2750
2751
p_script->member_functions.clear();
2752
p_script->member_indices.clear();
2753
p_script->static_variables_indices.clear();
2754
p_script->static_variables.clear();
2755
p_script->_signals.clear();
2756
p_script->initializer = nullptr;
2757
p_script->implicit_initializer = nullptr;
2758
p_script->implicit_ready = nullptr;
2759
p_script->static_initializer = nullptr;
2760
p_script->rpc_config.clear();
2761
p_script->lambda_info.clear();
2762
2763
p_script->clearing = false;
2764
2765
p_script->tool = parser->is_tool();
2766
p_script->_is_abstract = p_class->is_abstract;
2767
2768
if (p_script->local_name != StringName()) {
2769
if (ClassDB::class_exists(p_script->local_name) && ClassDB::is_class_exposed(p_script->local_name)) {
2770
_set_error(vformat(R"(The class "%s" shadows a native class)", p_script->local_name), p_class);
2771
return ERR_ALREADY_EXISTS;
2772
}
2773
}
2774
2775
GDScriptDataType base_type = _gdtype_from_datatype(p_class->base_type, p_script, false);
2776
2777
if (base_type.native_type == StringName()) {
2778
_set_error(vformat(R"(Parser bug (please report): Empty native type in base class "%s")", p_script->path), p_class);
2779
return ERR_BUG;
2780
}
2781
2782
int native_idx = GDScriptLanguage::get_singleton()->get_global_map()[base_type.native_type];
2783
2784
p_script->native = GDScriptLanguage::get_singleton()->get_global_array()[native_idx];
2785
if (p_script->native.is_null()) {
2786
_set_error("Compiler bug (please report): script native type is null.", nullptr);
2787
return ERR_BUG;
2788
}
2789
2790
// Inheritance
2791
switch (base_type.kind) {
2792
case GDScriptDataType::NATIVE:
2793
// Nothing more to do.
2794
break;
2795
case GDScriptDataType::GDSCRIPT: {
2796
Ref<GDScript> base = Ref<GDScript>(base_type.script_type);
2797
if (base.is_null()) {
2798
_set_error("Compiler bug (please report): base script type is null.", nullptr);
2799
return ERR_BUG;
2800
}
2801
2802
if (main_script->has_class(base.ptr())) {
2803
Error err = _prepare_compilation(base.ptr(), p_class->base_type.class_type, p_keep_state);
2804
if (err) {
2805
return err;
2806
}
2807
} else if (!base->is_valid()) {
2808
Error err = OK;
2809
Ref<GDScript> base_root = GDScriptCache::get_shallow_script(base->path, err, p_script->path);
2810
if (err) {
2811
_set_error(vformat(R"(Could not parse base class "%s" from "%s": %s)", base->fully_qualified_name, base->path, error_names[err]), nullptr);
2812
return err;
2813
}
2814
if (base_root.is_valid()) {
2815
base = Ref<GDScript>(base_root->find_class(base->fully_qualified_name));
2816
}
2817
if (base.is_null()) {
2818
_set_error(vformat(R"(Could not find class "%s" in "%s".)", base->fully_qualified_name, base->path), nullptr);
2819
return ERR_COMPILATION_FAILED;
2820
}
2821
2822
err = _prepare_compilation(base.ptr(), p_class->base_type.class_type, p_keep_state);
2823
if (err) {
2824
_set_error(vformat(R"(Could not populate class members of base class "%s" in "%s".)", base->fully_qualified_name, base->path), nullptr);
2825
return err;
2826
}
2827
}
2828
2829
p_script->base = base;
2830
p_script->_base = base.ptr();
2831
p_script->member_indices = base->member_indices;
2832
} break;
2833
default: {
2834
_set_error("Parser bug (please report): invalid inheritance.", nullptr);
2835
return ERR_BUG;
2836
} break;
2837
}
2838
2839
// Duplicate RPC information from base GDScript
2840
// Base script isn't valid because it should not have been compiled yet, but the reference contains relevant info.
2841
if (base_type.kind == GDScriptDataType::GDSCRIPT && p_script->base.is_valid()) {
2842
p_script->rpc_config = p_script->base->rpc_config.duplicate();
2843
}
2844
2845
for (int i = 0; i < p_class->members.size(); i++) {
2846
const GDScriptParser::ClassNode::Member &member = p_class->members[i];
2847
switch (member.type) {
2848
case GDScriptParser::ClassNode::Member::VARIABLE: {
2849
const GDScriptParser::VariableNode *variable = member.variable;
2850
StringName name = variable->identifier->name;
2851
2852
GDScript::MemberInfo minfo;
2853
switch (variable->property) {
2854
case GDScriptParser::VariableNode::PROP_NONE:
2855
break; // Nothing to do.
2856
case GDScriptParser::VariableNode::PROP_SETGET:
2857
if (variable->setter_pointer != nullptr) {
2858
minfo.setter = variable->setter_pointer->name;
2859
}
2860
if (variable->getter_pointer != nullptr) {
2861
minfo.getter = variable->getter_pointer->name;
2862
}
2863
break;
2864
case GDScriptParser::VariableNode::PROP_INLINE:
2865
if (variable->setter != nullptr) {
2866
minfo.setter = "@" + variable->identifier->name + "_setter";
2867
}
2868
if (variable->getter != nullptr) {
2869
minfo.getter = "@" + variable->identifier->name + "_getter";
2870
}
2871
break;
2872
}
2873
minfo.data_type = _gdtype_from_datatype(variable->get_datatype(), p_script);
2874
2875
PropertyInfo prop_info = variable->get_datatype().to_property_info(name);
2876
PropertyInfo export_info = variable->export_info;
2877
2878
if (variable->exported) {
2879
if (!minfo.data_type.has_type) {
2880
prop_info.type = export_info.type;
2881
prop_info.class_name = export_info.class_name;
2882
}
2883
prop_info.hint = export_info.hint;
2884
prop_info.hint_string = export_info.hint_string;
2885
prop_info.usage = export_info.usage;
2886
}
2887
prop_info.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
2888
minfo.property_info = prop_info;
2889
2890
if (variable->is_static) {
2891
minfo.index = p_script->static_variables_indices.size();
2892
p_script->static_variables_indices[name] = minfo;
2893
} else {
2894
minfo.index = p_script->member_indices.size();
2895
p_script->member_indices[name] = minfo;
2896
p_script->members.insert(name);
2897
}
2898
2899
#ifdef TOOLS_ENABLED
2900
if (variable->initializer != nullptr && variable->initializer->is_constant) {
2901
p_script->member_default_values[name] = variable->initializer->reduced_value;
2902
GDScriptCompiler::convert_to_initializer_type(p_script->member_default_values[name], variable);
2903
} else {
2904
p_script->member_default_values.erase(name);
2905
}
2906
#endif
2907
} break;
2908
2909
case GDScriptParser::ClassNode::Member::CONSTANT: {
2910
const GDScriptParser::ConstantNode *constant = member.constant;
2911
StringName name = constant->identifier->name;
2912
2913
p_script->constants.insert(name, constant->initializer->reduced_value);
2914
} break;
2915
2916
case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
2917
const GDScriptParser::EnumNode::Value &enum_value = member.enum_value;
2918
StringName name = enum_value.identifier->name;
2919
2920
p_script->constants.insert(name, enum_value.value);
2921
} break;
2922
2923
case GDScriptParser::ClassNode::Member::SIGNAL: {
2924
const GDScriptParser::SignalNode *signal = member.signal;
2925
StringName name = signal->identifier->name;
2926
2927
p_script->_signals[name] = signal->method_info;
2928
} break;
2929
2930
case GDScriptParser::ClassNode::Member::ENUM: {
2931
const GDScriptParser::EnumNode *enum_n = member.m_enum;
2932
StringName name = enum_n->identifier->name;
2933
2934
p_script->constants.insert(name, enum_n->dictionary);
2935
} break;
2936
2937
case GDScriptParser::ClassNode::Member::GROUP: {
2938
const GDScriptParser::AnnotationNode *annotation = member.annotation;
2939
// Avoid name conflict. See GH-78252.
2940
StringName name = vformat("@group_%d_%s", p_script->members.size(), annotation->export_info.name);
2941
2942
// This is not a normal member, but we need this to keep indices in order.
2943
GDScript::MemberInfo minfo;
2944
minfo.index = p_script->member_indices.size();
2945
2946
PropertyInfo prop_info;
2947
prop_info.name = annotation->export_info.name;
2948
prop_info.usage = annotation->export_info.usage;
2949
prop_info.hint_string = annotation->export_info.hint_string;
2950
minfo.property_info = prop_info;
2951
2952
p_script->member_indices[name] = minfo;
2953
p_script->members.insert(name);
2954
} break;
2955
2956
case GDScriptParser::ClassNode::Member::FUNCTION: {
2957
const GDScriptParser::FunctionNode *function_n = member.function;
2958
2959
Variant config = function_n->rpc_config;
2960
if (config.get_type() != Variant::NIL) {
2961
p_script->rpc_config[function_n->identifier->name] = config;
2962
}
2963
} break;
2964
default:
2965
break; // Nothing to do here.
2966
}
2967
}
2968
2969
p_script->static_variables.resize(p_script->static_variables_indices.size());
2970
2971
parsed_classes.insert(p_script);
2972
parsing_classes.erase(p_script);
2973
2974
// Populate inner classes.
2975
for (int i = 0; i < p_class->members.size(); i++) {
2976
const GDScriptParser::ClassNode::Member &member = p_class->members[i];
2977
if (member.type != member.CLASS) {
2978
continue;
2979
}
2980
const GDScriptParser::ClassNode *inner_class = member.m_class;
2981
StringName name = inner_class->identifier->name;
2982
Ref<GDScript> &subclass = p_script->subclasses[name];
2983
GDScript *subclass_ptr = subclass.ptr();
2984
2985
// Subclass might still be parsing, just skip it
2986
if (!parsing_classes.has(subclass_ptr)) {
2987
Error err = _prepare_compilation(subclass_ptr, inner_class, p_keep_state);
2988
if (err) {
2989
return err;
2990
}
2991
}
2992
2993
p_script->constants.insert(name, subclass); //once parsed, goes to the list of constants
2994
}
2995
2996
return OK;
2997
}
2998
2999
Error GDScriptCompiler::_compile_class(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
3000
// Compile member functions, getters, and setters.
3001
for (int i = 0; i < p_class->members.size(); i++) {
3002
const GDScriptParser::ClassNode::Member &member = p_class->members[i];
3003
if (member.type == member.FUNCTION) {
3004
const GDScriptParser::FunctionNode *function = member.function;
3005
Error err = OK;
3006
_parse_function(err, p_script, p_class, function);
3007
if (err) {
3008
return err;
3009
}
3010
} else if (member.type == member.VARIABLE) {
3011
const GDScriptParser::VariableNode *variable = member.variable;
3012
if (variable->property == GDScriptParser::VariableNode::PROP_INLINE) {
3013
if (variable->setter != nullptr) {
3014
Error err = _parse_setter_getter(p_script, p_class, variable, true);
3015
if (err) {
3016
return err;
3017
}
3018
}
3019
if (variable->getter != nullptr) {
3020
Error err = _parse_setter_getter(p_script, p_class, variable, false);
3021
if (err) {
3022
return err;
3023
}
3024
}
3025
}
3026
}
3027
}
3028
3029
{
3030
// Create `@implicit_new()` special function in any case.
3031
Error err = OK;
3032
_parse_function(err, p_script, p_class, nullptr);
3033
if (err) {
3034
return err;
3035
}
3036
}
3037
3038
if (p_class->onready_used) {
3039
// Create `@implicit_ready()` special function.
3040
Error err = OK;
3041
_parse_function(err, p_script, p_class, nullptr, true);
3042
if (err) {
3043
return err;
3044
}
3045
}
3046
3047
if (p_class->has_static_data) {
3048
Error err = OK;
3049
GDScriptFunction *func = _make_static_initializer(err, p_script, p_class);
3050
p_script->static_initializer = func;
3051
if (err) {
3052
return err;
3053
}
3054
}
3055
3056
#ifdef DEBUG_ENABLED
3057
3058
//validate instances if keeping state
3059
3060
if (p_keep_state) {
3061
for (RBSet<Object *>::Element *E = p_script->instances.front(); E;) {
3062
RBSet<Object *>::Element *N = E->next();
3063
3064
ScriptInstance *si = E->get()->get_script_instance();
3065
if (si->is_placeholder()) {
3066
#ifdef TOOLS_ENABLED
3067
PlaceHolderScriptInstance *psi = static_cast<PlaceHolderScriptInstance *>(si);
3068
3069
if (p_script->is_tool()) {
3070
//re-create as an instance
3071
p_script->placeholders.erase(psi); //remove placeholder
3072
3073
GDScriptInstance *instance = memnew(GDScriptInstance);
3074
instance->base_ref_counted = Object::cast_to<RefCounted>(E->get());
3075
instance->members.resize(p_script->member_indices.size());
3076
instance->script = Ref<GDScript>(p_script);
3077
instance->owner = E->get();
3078
3079
//needed for hot reloading
3080
for (const KeyValue<StringName, GDScript::MemberInfo> &F : p_script->member_indices) {
3081
instance->member_indices_cache[F.key] = F.value.index;
3082
}
3083
instance->owner->set_script_instance(instance);
3084
3085
/* STEP 2, INITIALIZE AND CONSTRUCT */
3086
3087
Callable::CallError ce;
3088
p_script->initializer->call(instance, nullptr, 0, ce);
3089
3090
if (ce.error != Callable::CallError::CALL_OK) {
3091
//well, tough luck, not gonna do anything here
3092
}
3093
}
3094
#endif // TOOLS_ENABLED
3095
} else {
3096
GDScriptInstance *gi = static_cast<GDScriptInstance *>(si);
3097
gi->reload_members();
3098
}
3099
3100
E = N;
3101
}
3102
}
3103
#endif //DEBUG_ENABLED
3104
3105
has_static_data = p_class->has_static_data;
3106
3107
for (int i = 0; i < p_class->members.size(); i++) {
3108
if (p_class->members[i].type != GDScriptParser::ClassNode::Member::CLASS) {
3109
continue;
3110
}
3111
const GDScriptParser::ClassNode *inner_class = p_class->members[i].m_class;
3112
StringName name = inner_class->identifier->name;
3113
GDScript *subclass = p_script->subclasses[name].ptr();
3114
3115
Error err = _compile_class(subclass, inner_class, p_keep_state);
3116
if (err) {
3117
return err;
3118
}
3119
3120
has_static_data = has_static_data || inner_class->has_static_data;
3121
}
3122
3123
p_script->_static_default_init();
3124
3125
p_script->valid = true;
3126
return OK;
3127
}
3128
3129
void GDScriptCompiler::convert_to_initializer_type(Variant &p_variant, const GDScriptParser::VariableNode *p_node) {
3130
// Set p_variant to the value of p_node's initializer, with the type of p_node's variable.
3131
GDScriptParser::DataType member_t = p_node->datatype;
3132
GDScriptParser::DataType init_t = p_node->initializer->datatype;
3133
if (member_t.is_hard_type() && init_t.is_hard_type() &&
3134
member_t.kind == GDScriptParser::DataType::BUILTIN && init_t.kind == GDScriptParser::DataType::BUILTIN) {
3135
if (Variant::can_convert_strict(init_t.builtin_type, member_t.builtin_type)) {
3136
const Variant *v = &p_node->initializer->reduced_value;
3137
Callable::CallError ce;
3138
Variant::construct(member_t.builtin_type, p_variant, &v, 1, ce);
3139
}
3140
}
3141
}
3142
3143
void GDScriptCompiler::make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
3144
p_script->fully_qualified_name = p_class->fqcn;
3145
p_script->local_name = p_class->identifier ? p_class->identifier->name : StringName();
3146
p_script->global_name = p_class->get_global_name();
3147
p_script->simplified_icon_path = p_class->simplified_icon_path;
3148
3149
HashMap<StringName, Ref<GDScript>> old_subclasses;
3150
3151
if (p_keep_state) {
3152
old_subclasses = p_script->subclasses;
3153
}
3154
3155
p_script->subclasses.clear();
3156
3157
for (int i = 0; i < p_class->members.size(); i++) {
3158
if (p_class->members[i].type != GDScriptParser::ClassNode::Member::CLASS) {
3159
continue;
3160
}
3161
const GDScriptParser::ClassNode *inner_class = p_class->members[i].m_class;
3162
StringName name = inner_class->identifier->name;
3163
3164
Ref<GDScript> subclass;
3165
3166
if (old_subclasses.has(name)) {
3167
subclass = old_subclasses[name];
3168
} else {
3169
subclass = GDScriptLanguage::get_singleton()->get_orphan_subclass(inner_class->fqcn);
3170
}
3171
3172
if (subclass.is_null()) {
3173
subclass.instantiate();
3174
}
3175
3176
subclass->_owner = p_script;
3177
subclass->path = p_script->path;
3178
p_script->subclasses.insert(name, subclass);
3179
3180
make_scripts(subclass.ptr(), inner_class, p_keep_state);
3181
}
3182
}
3183
3184
GDScriptCompiler::FunctionLambdaInfo GDScriptCompiler::_get_function_replacement_info(GDScriptFunction *p_func, int p_index, int p_depth, GDScriptFunction *p_parent_func) {
3185
FunctionLambdaInfo info;
3186
info.function = p_func;
3187
info.parent = p_parent_func;
3188
info.script = p_func->get_script();
3189
info.name = p_func->get_name();
3190
info.line = p_func->_initial_line;
3191
info.index = p_index;
3192
info.depth = p_depth;
3193
info.capture_count = 0;
3194
info.use_self = false;
3195
info.arg_count = p_func->_argument_count;
3196
info.default_arg_count = p_func->_default_arg_count;
3197
info.sublambdas = _get_function_lambda_replacement_info(p_func, p_depth, p_parent_func);
3198
3199
ERR_FAIL_NULL_V(info.script, info);
3200
GDScript::LambdaInfo *extra_info = info.script->lambda_info.getptr(p_func);
3201
if (extra_info != nullptr) {
3202
info.capture_count = extra_info->capture_count;
3203
info.use_self = extra_info->use_self;
3204
} else {
3205
info.capture_count = 0;
3206
info.use_self = false;
3207
}
3208
3209
return info;
3210
}
3211
3212
Vector<GDScriptCompiler::FunctionLambdaInfo> GDScriptCompiler::_get_function_lambda_replacement_info(GDScriptFunction *p_func, int p_depth, GDScriptFunction *p_parent_func) {
3213
Vector<FunctionLambdaInfo> result;
3214
// Only scrape the lambdas inside p_func.
3215
for (int i = 0; i < p_func->lambdas.size(); ++i) {
3216
result.push_back(_get_function_replacement_info(p_func->lambdas[i], i, p_depth + 1, p_func));
3217
}
3218
return result;
3219
}
3220
3221
GDScriptCompiler::ScriptLambdaInfo GDScriptCompiler::_get_script_lambda_replacement_info(GDScript *p_script) {
3222
ScriptLambdaInfo info;
3223
3224
if (p_script->implicit_initializer) {
3225
info.implicit_initializer_info = _get_function_lambda_replacement_info(p_script->implicit_initializer);
3226
}
3227
if (p_script->implicit_ready) {
3228
info.implicit_ready_info = _get_function_lambda_replacement_info(p_script->implicit_ready);
3229
}
3230
if (p_script->static_initializer) {
3231
info.static_initializer_info = _get_function_lambda_replacement_info(p_script->static_initializer);
3232
}
3233
3234
for (const KeyValue<StringName, GDScriptFunction *> &E : p_script->member_functions) {
3235
info.member_function_infos.insert(E.key, _get_function_lambda_replacement_info(E.value));
3236
}
3237
3238
for (const KeyValue<StringName, Ref<GDScript>> &KV : p_script->get_subclasses()) {
3239
info.subclass_info.insert(KV.key, _get_script_lambda_replacement_info(KV.value.ptr()));
3240
}
3241
3242
return info;
3243
}
3244
3245
bool GDScriptCompiler::_do_function_infos_match(const FunctionLambdaInfo &p_old_info, const FunctionLambdaInfo *p_new_info) {
3246
if (p_new_info == nullptr) {
3247
return false;
3248
}
3249
3250
if (p_new_info->capture_count != p_old_info.capture_count || p_new_info->use_self != p_old_info.use_self) {
3251
return false;
3252
}
3253
3254
int old_required_arg_count = p_old_info.arg_count - p_old_info.default_arg_count;
3255
int new_required_arg_count = p_new_info->arg_count - p_new_info->default_arg_count;
3256
if (new_required_arg_count > old_required_arg_count || p_new_info->arg_count < old_required_arg_count) {
3257
return false;
3258
}
3259
3260
return true;
3261
}
3262
3263
void GDScriptCompiler::_get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const FunctionLambdaInfo &p_old_info, const FunctionLambdaInfo *p_new_info) {
3264
ERR_FAIL_COND(r_replacements.has(p_old_info.function));
3265
if (!_do_function_infos_match(p_old_info, p_new_info)) {
3266
p_new_info = nullptr;
3267
}
3268
3269
r_replacements.insert(p_old_info.function, p_new_info != nullptr ? p_new_info->function : nullptr);
3270
_get_function_ptr_replacements(r_replacements, p_old_info.sublambdas, p_new_info != nullptr ? &p_new_info->sublambdas : nullptr);
3271
}
3272
3273
void GDScriptCompiler::_get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const Vector<FunctionLambdaInfo> &p_old_infos, const Vector<FunctionLambdaInfo> *p_new_infos) {
3274
for (int i = 0; i < p_old_infos.size(); ++i) {
3275
const FunctionLambdaInfo &old_info = p_old_infos[i];
3276
const FunctionLambdaInfo *new_info = nullptr;
3277
if (p_new_infos != nullptr && p_new_infos->size() == p_old_infos.size()) {
3278
// For now only attempt if the size is the same.
3279
new_info = &p_new_infos->get(i);
3280
}
3281
_get_function_ptr_replacements(r_replacements, old_info, new_info);
3282
}
3283
}
3284
3285
void GDScriptCompiler::_get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const ScriptLambdaInfo &p_old_info, const ScriptLambdaInfo *p_new_info) {
3286
_get_function_ptr_replacements(r_replacements, p_old_info.implicit_initializer_info, p_new_info != nullptr ? &p_new_info->implicit_initializer_info : nullptr);
3287
_get_function_ptr_replacements(r_replacements, p_old_info.implicit_ready_info, p_new_info != nullptr ? &p_new_info->implicit_ready_info : nullptr);
3288
_get_function_ptr_replacements(r_replacements, p_old_info.static_initializer_info, p_new_info != nullptr ? &p_new_info->static_initializer_info : nullptr);
3289
3290
for (const KeyValue<StringName, Vector<FunctionLambdaInfo>> &old_kv : p_old_info.member_function_infos) {
3291
_get_function_ptr_replacements(r_replacements, old_kv.value, p_new_info != nullptr ? p_new_info->member_function_infos.getptr(old_kv.key) : nullptr);
3292
}
3293
for (int i = 0; i < p_old_info.other_function_infos.size(); ++i) {
3294
const FunctionLambdaInfo &old_other_info = p_old_info.other_function_infos[i];
3295
const FunctionLambdaInfo *new_other_info = nullptr;
3296
if (p_new_info != nullptr && p_new_info->other_function_infos.size() == p_old_info.other_function_infos.size()) {
3297
// For now only attempt if the size is the same.
3298
new_other_info = &p_new_info->other_function_infos[i];
3299
}
3300
// Needs to be called on all old lambdas, even if there's no replacement.
3301
_get_function_ptr_replacements(r_replacements, old_other_info, new_other_info);
3302
}
3303
for (const KeyValue<StringName, ScriptLambdaInfo> &old_kv : p_old_info.subclass_info) {
3304
const ScriptLambdaInfo &old_subinfo = old_kv.value;
3305
const ScriptLambdaInfo *new_subinfo = p_new_info != nullptr ? p_new_info->subclass_info.getptr(old_kv.key) : nullptr;
3306
_get_function_ptr_replacements(r_replacements, old_subinfo, new_subinfo);
3307
}
3308
}
3309
3310
Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state) {
3311
err_line = -1;
3312
err_column = -1;
3313
error = "";
3314
parser = p_parser;
3315
main_script = p_script;
3316
const GDScriptParser::ClassNode *root = parser->get_tree();
3317
3318
source = p_script->get_path();
3319
3320
ScriptLambdaInfo old_lambda_info = _get_script_lambda_replacement_info(p_script);
3321
3322
// Create scripts for subclasses beforehand so they can be referenced
3323
make_scripts(p_script, root, p_keep_state);
3324
3325
main_script->_owner = nullptr;
3326
Error err = _prepare_compilation(main_script, parser->get_tree(), p_keep_state);
3327
3328
if (err) {
3329
return err;
3330
}
3331
3332
err = _compile_class(main_script, root, p_keep_state);
3333
if (err) {
3334
return err;
3335
}
3336
3337
ScriptLambdaInfo new_lambda_info = _get_script_lambda_replacement_info(p_script);
3338
3339
HashMap<GDScriptFunction *, GDScriptFunction *> func_ptr_replacements;
3340
_get_function_ptr_replacements(func_ptr_replacements, old_lambda_info, &new_lambda_info);
3341
main_script->_recurse_replace_function_ptrs(func_ptr_replacements);
3342
3343
if (has_static_data && !root->annotated_static_unload) {
3344
GDScriptCache::add_static_script(p_script);
3345
}
3346
3347
err = GDScriptCache::finish_compiling(main_script->path);
3348
if (err) {
3349
_set_error(R"(Failed to compile depended scripts.)", nullptr);
3350
}
3351
return err;
3352
}
3353
3354
String GDScriptCompiler::get_error() const {
3355
return error;
3356
}
3357
3358
int GDScriptCompiler::get_error_line() const {
3359
return err_line;
3360
}
3361
3362
int GDScriptCompiler::get_error_column() const {
3363
return err_column;
3364
}
3365
3366
GDScriptCompiler::GDScriptCompiler() {
3367
}
3368
3369