gdscript_compiler.cpp 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695
  1. /*************************************************************************/
  2. /* gdscript_compiler.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "gdscript_compiler.h"
  31. #include "gdscript.h"
  32. #include "gdscript_byte_codegen.h"
  33. #include "gdscript_cache.h"
  34. #include "gdscript_utility_functions.h"
  35. #include "core/config/engine.h"
  36. #include "core/config/project_settings.h"
  37. bool GDScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringName &p_name) {
  38. if (codegen.function_node && codegen.function_node->is_static) {
  39. return false;
  40. }
  41. if (codegen.parameters.has(p_name) || codegen.locals.has(p_name)) {
  42. return false; //shadowed
  43. }
  44. return _is_class_member_property(codegen.script, p_name);
  45. }
  46. bool GDScriptCompiler::_is_class_member_property(GDScript *owner, const StringName &p_name) {
  47. GDScript *scr = owner;
  48. GDScriptNativeClass *nc = nullptr;
  49. while (scr) {
  50. if (scr->native.is_valid()) {
  51. nc = scr->native.ptr();
  52. }
  53. scr = scr->_base;
  54. }
  55. ERR_FAIL_COND_V(!nc, false);
  56. return ClassDB::has_property(nc->get_name(), p_name);
  57. }
  58. void GDScriptCompiler::_set_error(const String &p_error, const GDScriptParser::Node *p_node) {
  59. if (!error.is_empty()) {
  60. return;
  61. }
  62. error = p_error;
  63. if (p_node) {
  64. err_line = p_node->start_line;
  65. err_column = p_node->leftmost_column;
  66. } else {
  67. err_line = 0;
  68. err_column = 0;
  69. }
  70. }
  71. GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::DataType &p_datatype, GDScript *p_owner) {
  72. if (!p_datatype.is_set() || !p_datatype.is_hard_type()) {
  73. return GDScriptDataType();
  74. }
  75. GDScriptDataType result;
  76. result.has_type = true;
  77. switch (p_datatype.kind) {
  78. case GDScriptParser::DataType::VARIANT: {
  79. result.has_type = false;
  80. } break;
  81. case GDScriptParser::DataType::BUILTIN: {
  82. result.kind = GDScriptDataType::BUILTIN;
  83. result.builtin_type = p_datatype.builtin_type;
  84. } break;
  85. case GDScriptParser::DataType::NATIVE: {
  86. result.kind = GDScriptDataType::NATIVE;
  87. result.native_type = p_datatype.native_type;
  88. result.builtin_type = p_datatype.builtin_type;
  89. } break;
  90. case GDScriptParser::DataType::SCRIPT: {
  91. result.kind = GDScriptDataType::SCRIPT;
  92. result.builtin_type = p_datatype.builtin_type;
  93. result.script_type_ref = p_datatype.script_type;
  94. result.script_type = result.script_type_ref.ptr();
  95. result.native_type = p_datatype.native_type;
  96. } break;
  97. case GDScriptParser::DataType::CLASS: {
  98. result.kind = GDScriptDataType::GDSCRIPT;
  99. result.builtin_type = p_datatype.builtin_type;
  100. result.native_type = p_datatype.native_type;
  101. String root_name = p_datatype.class_type->fqcn.get_slice("::", 0);
  102. bool is_local_class = !root_name.is_empty() && root_name == main_script->fully_qualified_name;
  103. Ref<GDScript> script;
  104. if (is_local_class) {
  105. script = Ref<GDScript>(main_script);
  106. } else {
  107. Error err = OK;
  108. script = GDScriptCache::get_shallow_script(p_datatype.script_path, err, p_owner->path);
  109. if (err) {
  110. _set_error(vformat(R"(Could not find script "%s": %s)", p_datatype.script_path, error_names[err]), nullptr);
  111. }
  112. }
  113. if (script.is_valid()) {
  114. script = Ref<GDScript>(script->find_class(p_datatype.class_type->fqcn));
  115. }
  116. if (script.is_null()) {
  117. _set_error(vformat(R"(Could not find class "%s" in "%s".)", p_datatype.class_type->fqcn, p_datatype.script_path), nullptr);
  118. return GDScriptDataType();
  119. } else {
  120. // Only hold a strong reference if the owner of the element qualified with this type is not local, to avoid cyclic references (leaks).
  121. // TODO: Might lead to use after free if script_type is a subclass and is used after its parent is freed.
  122. if (!is_local_class) {
  123. result.script_type_ref = script;
  124. }
  125. result.script_type = script.ptr();
  126. result.native_type = p_datatype.native_type;
  127. }
  128. } break;
  129. case GDScriptParser::DataType::ENUM:
  130. result.has_type = true;
  131. result.kind = GDScriptDataType::BUILTIN;
  132. if (p_datatype.is_meta_type) {
  133. result.builtin_type = Variant::DICTIONARY;
  134. } else {
  135. result.builtin_type = Variant::INT;
  136. }
  137. break;
  138. case GDScriptParser::DataType::UNRESOLVED: {
  139. ERR_PRINT("Parser bug: converting unresolved type.");
  140. return GDScriptDataType();
  141. }
  142. }
  143. if (p_datatype.has_container_element_type()) {
  144. result.set_container_element_type(_gdtype_from_datatype(p_datatype.get_container_element_type(), p_owner));
  145. }
  146. return result;
  147. }
  148. static bool _is_exact_type(const PropertyInfo &p_par_type, const GDScriptDataType &p_arg_type) {
  149. if (!p_arg_type.has_type) {
  150. return false;
  151. }
  152. if (p_par_type.type == Variant::NIL) {
  153. return false;
  154. }
  155. if (p_par_type.type == Variant::OBJECT) {
  156. if (p_arg_type.kind == GDScriptDataType::BUILTIN) {
  157. return false;
  158. }
  159. StringName class_name;
  160. if (p_arg_type.kind == GDScriptDataType::NATIVE) {
  161. class_name = p_arg_type.native_type;
  162. } else {
  163. class_name = p_arg_type.native_type == StringName() ? p_arg_type.script_type->get_instance_base_type() : p_arg_type.native_type;
  164. }
  165. return p_par_type.class_name == class_name || ClassDB::is_parent_class(class_name, p_par_type.class_name);
  166. } else {
  167. if (p_arg_type.kind != GDScriptDataType::BUILTIN) {
  168. return false;
  169. }
  170. return p_par_type.type == p_arg_type.builtin_type;
  171. }
  172. }
  173. static bool _have_exact_arguments(const MethodBind *p_method, const Vector<GDScriptCodeGenerator::Address> &p_arguments) {
  174. if (p_method->get_argument_count() != p_arguments.size()) {
  175. // ptrcall won't work with default arguments.
  176. return false;
  177. }
  178. MethodInfo info;
  179. ClassDB::get_method_info(p_method->get_instance_class(), p_method->get_name(), &info);
  180. for (int i = 0; i < p_arguments.size(); i++) {
  181. const PropertyInfo &prop = info.arguments[i];
  182. if (!_is_exact_type(prop, p_arguments[i].type)) {
  183. return false;
  184. }
  185. }
  186. return true;
  187. }
  188. GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::ExpressionNode *p_expression, bool p_root, bool p_initializer, const GDScriptCodeGenerator::Address &p_index_addr) {
  189. if (p_expression->is_constant) {
  190. return codegen.add_constant(p_expression->reduced_value);
  191. }
  192. GDScriptCodeGenerator *gen = codegen.generator;
  193. switch (p_expression->type) {
  194. case GDScriptParser::Node::IDENTIFIER: {
  195. // Look for identifiers in current scope.
  196. const GDScriptParser::IdentifierNode *in = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  197. StringName identifier = in->name;
  198. // Try function parameters.
  199. if (codegen.parameters.has(identifier)) {
  200. return codegen.parameters[identifier];
  201. }
  202. // Try local variables and constants.
  203. if (!p_initializer && codegen.locals.has(identifier)) {
  204. return codegen.locals[identifier];
  205. }
  206. // Try class members.
  207. if (_is_class_member_property(codegen, identifier)) {
  208. // Get property.
  209. GDScriptCodeGenerator::Address temp = codegen.add_temporary(); // TODO: Could get the type of the class member here.
  210. gen->write_get_member(temp, identifier);
  211. return temp;
  212. }
  213. // Try members.
  214. if (!codegen.function_node || !codegen.function_node->is_static) {
  215. // Try member variables.
  216. if (codegen.script->member_indices.has(identifier)) {
  217. if (codegen.script->member_indices[identifier].getter != StringName() && codegen.script->member_indices[identifier].getter != codegen.function_name) {
  218. // Perform getter.
  219. GDScriptCodeGenerator::Address temp = codegen.add_temporary();
  220. Vector<GDScriptCodeGenerator::Address> args; // No argument needed.
  221. gen->write_call_self(temp, codegen.script->member_indices[identifier].getter, args);
  222. return temp;
  223. } else {
  224. // No getter or inside getter: direct member access.,
  225. int idx = codegen.script->member_indices[identifier].index;
  226. return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::MEMBER, idx, codegen.script->get_member_type(identifier));
  227. }
  228. }
  229. }
  230. // Try class constants.
  231. {
  232. GDScript *owner = codegen.script;
  233. while (owner) {
  234. GDScript *scr = owner;
  235. GDScriptNativeClass *nc = nullptr;
  236. while (scr) {
  237. if (scr->constants.has(identifier)) {
  238. return codegen.add_constant(scr->constants[identifier]); // TODO: Get type here.
  239. }
  240. if (scr->native.is_valid()) {
  241. nc = scr->native.ptr();
  242. }
  243. scr = scr->_base;
  244. }
  245. // Class C++ integer constant.
  246. if (nc) {
  247. bool success = false;
  248. int64_t constant = ClassDB::get_integer_constant(nc->get_name(), identifier, &success);
  249. if (success) {
  250. return codegen.add_constant(constant);
  251. }
  252. }
  253. owner = owner->_owner;
  254. }
  255. }
  256. // Try signals and methods (can be made callables).
  257. {
  258. // Search upwards through parent classes:
  259. const GDScriptParser::ClassNode *base_class = codegen.class_node;
  260. while (base_class != nullptr) {
  261. if (base_class->has_member(identifier)) {
  262. const GDScriptParser::ClassNode::Member &member = base_class->get_member(identifier);
  263. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION || member.type == GDScriptParser::ClassNode::Member::SIGNAL) {
  264. // Get like it was a property.
  265. GDScriptCodeGenerator::Address temp = codegen.add_temporary(); // TODO: Get type here.
  266. GDScriptCodeGenerator::Address self(GDScriptCodeGenerator::Address::SELF);
  267. gen->write_get_named(temp, identifier, self);
  268. return temp;
  269. }
  270. }
  271. base_class = base_class->base_type.class_type;
  272. }
  273. // Try in native base.
  274. GDScript *scr = codegen.script;
  275. GDScriptNativeClass *nc = nullptr;
  276. while (scr) {
  277. if (scr->native.is_valid()) {
  278. nc = scr->native.ptr();
  279. }
  280. scr = scr->_base;
  281. }
  282. if (nc && (ClassDB::has_signal(nc->get_name(), identifier) || ClassDB::has_method(nc->get_name(), identifier))) {
  283. // Get like it was a property.
  284. GDScriptCodeGenerator::Address temp = codegen.add_temporary(); // TODO: Get type here.
  285. GDScriptCodeGenerator::Address self(GDScriptCodeGenerator::Address::SELF);
  286. gen->write_get_named(temp, identifier, self);
  287. return temp;
  288. }
  289. }
  290. // Try globals.
  291. if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
  292. // If it's an autoload singleton, we postpone to load it at runtime.
  293. // This is so one autoload doesn't try to load another before it's compiled.
  294. HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  295. if (autoloads.has(identifier) && autoloads[identifier].is_singleton) {
  296. GDScriptCodeGenerator::Address global = codegen.add_temporary(_gdtype_from_datatype(in->get_datatype(), codegen.script));
  297. int idx = GDScriptLanguage::get_singleton()->get_global_map()[identifier];
  298. gen->write_store_global(global, idx);
  299. return global;
  300. } else {
  301. int idx = GDScriptLanguage::get_singleton()->get_global_map()[identifier];
  302. Variant global = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  303. return codegen.add_constant(global);
  304. }
  305. }
  306. // Try global classes.
  307. if (ScriptServer::is_global_class(identifier)) {
  308. const GDScriptParser::ClassNode *class_node = codegen.class_node;
  309. while (class_node->outer) {
  310. class_node = class_node->outer;
  311. }
  312. Ref<Resource> res;
  313. if (class_node->identifier && class_node->identifier->name == identifier) {
  314. res = Ref<GDScript>(main_script);
  315. } else {
  316. String global_class_path = ScriptServer::get_global_class_path(identifier);
  317. if (ResourceLoader::get_resource_type(global_class_path) == "GDScript") {
  318. Error err = OK;
  319. res = GDScriptCache::get_full_script(global_class_path, err);
  320. if (err != OK) {
  321. _set_error("Can't load global class " + String(identifier), p_expression);
  322. r_error = ERR_COMPILATION_FAILED;
  323. return GDScriptCodeGenerator::Address();
  324. }
  325. } else {
  326. res = ResourceLoader::load(global_class_path);
  327. if (res.is_null()) {
  328. _set_error("Can't load global class " + String(identifier) + ", cyclic reference?", p_expression);
  329. r_error = ERR_COMPILATION_FAILED;
  330. return GDScriptCodeGenerator::Address();
  331. }
  332. }
  333. }
  334. return codegen.add_constant(res);
  335. }
  336. #ifdef TOOLS_ENABLED
  337. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(identifier)) {
  338. GDScriptCodeGenerator::Address global = codegen.add_temporary(); // TODO: Get type.
  339. gen->write_store_named_global(global, identifier);
  340. return global;
  341. }
  342. #endif
  343. // Not found, error.
  344. _set_error("Identifier not found: " + String(identifier), p_expression);
  345. r_error = ERR_COMPILATION_FAILED;
  346. return GDScriptCodeGenerator::Address();
  347. } break;
  348. case GDScriptParser::Node::LITERAL: {
  349. // Return constant.
  350. const GDScriptParser::LiteralNode *cn = static_cast<const GDScriptParser::LiteralNode *>(p_expression);
  351. return codegen.add_constant(cn->value);
  352. } break;
  353. case GDScriptParser::Node::SELF: {
  354. //return constant
  355. if (codegen.function_node && codegen.function_node->is_static) {
  356. _set_error("'self' not present in static function!", p_expression);
  357. r_error = ERR_COMPILATION_FAILED;
  358. return GDScriptCodeGenerator::Address();
  359. }
  360. return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::SELF);
  361. } break;
  362. case GDScriptParser::Node::ARRAY: {
  363. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  364. Vector<GDScriptCodeGenerator::Address> values;
  365. // Create the result temporary first since it's the last to be killed.
  366. GDScriptDataType array_type = _gdtype_from_datatype(an->get_datatype(), codegen.script);
  367. GDScriptCodeGenerator::Address result = codegen.add_temporary(array_type);
  368. for (int i = 0; i < an->elements.size(); i++) {
  369. GDScriptCodeGenerator::Address val = _parse_expression(codegen, r_error, an->elements[i]);
  370. if (r_error) {
  371. return GDScriptCodeGenerator::Address();
  372. }
  373. values.push_back(val);
  374. }
  375. if (array_type.has_container_element_type()) {
  376. gen->write_construct_typed_array(result, array_type.get_container_element_type(), values);
  377. } else {
  378. gen->write_construct_array(result, values);
  379. }
  380. for (int i = 0; i < values.size(); i++) {
  381. if (values[i].mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  382. gen->pop_temporary();
  383. }
  384. }
  385. return result;
  386. } break;
  387. case GDScriptParser::Node::DICTIONARY: {
  388. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  389. Vector<GDScriptCodeGenerator::Address> elements;
  390. // Create the result temporary first since it's the last to be killed.
  391. GDScriptDataType dict_type;
  392. dict_type.has_type = true;
  393. dict_type.kind = GDScriptDataType::BUILTIN;
  394. dict_type.builtin_type = Variant::DICTIONARY;
  395. GDScriptCodeGenerator::Address result = codegen.add_temporary(dict_type);
  396. for (int i = 0; i < dn->elements.size(); i++) {
  397. // Key.
  398. GDScriptCodeGenerator::Address element;
  399. switch (dn->style) {
  400. case GDScriptParser::DictionaryNode::PYTHON_DICT:
  401. // Python-style: key is any expression.
  402. element = _parse_expression(codegen, r_error, dn->elements[i].key);
  403. if (r_error) {
  404. return GDScriptCodeGenerator::Address();
  405. }
  406. break;
  407. case GDScriptParser::DictionaryNode::LUA_TABLE:
  408. // Lua-style: key is an identifier interpreted as StringName.
  409. StringName key = dn->elements[i].key->reduced_value.operator StringName();
  410. element = codegen.add_constant(key);
  411. break;
  412. }
  413. elements.push_back(element);
  414. element = _parse_expression(codegen, r_error, dn->elements[i].value);
  415. if (r_error) {
  416. return GDScriptCodeGenerator::Address();
  417. }
  418. elements.push_back(element);
  419. }
  420. gen->write_construct_dictionary(result, elements);
  421. for (int i = 0; i < elements.size(); i++) {
  422. if (elements[i].mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  423. gen->pop_temporary();
  424. }
  425. }
  426. return result;
  427. } break;
  428. case GDScriptParser::Node::CAST: {
  429. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  430. GDScriptParser::DataType og_cast_type = cn->cast_type->get_datatype();
  431. GDScriptDataType cast_type = _gdtype_from_datatype(og_cast_type, codegen.script);
  432. if (og_cast_type.kind == GDScriptParser::DataType::ENUM) {
  433. // Enum types are usually treated as dictionaries, but in this case we want to cast to an integer.
  434. cast_type.kind = GDScriptDataType::BUILTIN;
  435. cast_type.builtin_type = Variant::INT;
  436. }
  437. // Create temporary for result first since it will be deleted last.
  438. GDScriptCodeGenerator::Address result = codegen.add_temporary(cast_type);
  439. GDScriptCodeGenerator::Address src = _parse_expression(codegen, r_error, cn->operand);
  440. gen->write_cast(result, src, cast_type);
  441. if (src.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  442. gen->pop_temporary();
  443. }
  444. return result;
  445. } break;
  446. case GDScriptParser::Node::CALL: {
  447. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression);
  448. GDScriptDataType type = _gdtype_from_datatype(call->get_datatype(), codegen.script);
  449. GDScriptCodeGenerator::Address result = codegen.add_temporary(type);
  450. GDScriptCodeGenerator::Address nil = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::NIL);
  451. GDScriptCodeGenerator::Address return_addr = p_root ? nil : result;
  452. Vector<GDScriptCodeGenerator::Address> arguments;
  453. for (int i = 0; i < call->arguments.size(); i++) {
  454. GDScriptCodeGenerator::Address arg = _parse_expression(codegen, r_error, call->arguments[i]);
  455. if (r_error) {
  456. return GDScriptCodeGenerator::Address();
  457. }
  458. arguments.push_back(arg);
  459. }
  460. if (!call->is_super && call->callee->type == GDScriptParser::Node::IDENTIFIER && GDScriptParser::get_builtin_type(call->function_name) != Variant::VARIANT_MAX) {
  461. // Construct a built-in type.
  462. Variant::Type vtype = GDScriptParser::get_builtin_type(static_cast<GDScriptParser::IdentifierNode *>(call->callee)->name);
  463. gen->write_construct(result, vtype, arguments);
  464. } else if (!call->is_super && call->callee->type == GDScriptParser::Node::IDENTIFIER && Variant::has_utility_function(call->function_name)) {
  465. // Variant utility function.
  466. gen->write_call_utility(result, call->function_name, arguments);
  467. } else if (!call->is_super && call->callee->type == GDScriptParser::Node::IDENTIFIER && GDScriptUtilityFunctions::function_exists(call->function_name)) {
  468. // GDScript utility function.
  469. gen->write_call_gdscript_utility(result, GDScriptUtilityFunctions::get_function(call->function_name), arguments);
  470. } else {
  471. // Regular function.
  472. const GDScriptParser::ExpressionNode *callee = call->callee;
  473. if (call->is_super) {
  474. // Super call.
  475. gen->write_super_call(result, call->function_name, arguments);
  476. } else {
  477. if (callee->type == GDScriptParser::Node::IDENTIFIER) {
  478. // Self function call.
  479. if (ClassDB::has_method(codegen.script->native->get_name(), call->function_name)) {
  480. // Native method, use faster path.
  481. GDScriptCodeGenerator::Address self;
  482. self.mode = GDScriptCodeGenerator::Address::SELF;
  483. MethodBind *method = ClassDB::get_method(codegen.script->native->get_name(), call->function_name);
  484. if (_have_exact_arguments(method, arguments)) {
  485. // Exact arguments, use ptrcall.
  486. gen->write_call_ptrcall(result, self, method, arguments);
  487. } else {
  488. // Not exact arguments, but still can use method bind call.
  489. gen->write_call_method_bind(result, self, method, arguments);
  490. }
  491. } else if ((codegen.function_node && codegen.function_node->is_static) || call->function_name == "new") {
  492. GDScriptCodeGenerator::Address self;
  493. self.mode = GDScriptCodeGenerator::Address::CLASS;
  494. if (within_await) {
  495. gen->write_call_async(result, self, call->function_name, arguments);
  496. } else {
  497. gen->write_call(return_addr, self, call->function_name, arguments);
  498. }
  499. } else {
  500. if (within_await) {
  501. gen->write_call_self_async(result, call->function_name, arguments);
  502. } else {
  503. gen->write_call_self(return_addr, call->function_name, arguments);
  504. }
  505. }
  506. } else if (callee->type == GDScriptParser::Node::SUBSCRIPT) {
  507. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee);
  508. if (subscript->is_attribute) {
  509. // May be static built-in method call.
  510. if (!call->is_super && subscript->base->type == GDScriptParser::Node::IDENTIFIER && GDScriptParser::get_builtin_type(static_cast<GDScriptParser::IdentifierNode *>(subscript->base)->name) < Variant::VARIANT_MAX) {
  511. gen->write_call_builtin_type_static(result, GDScriptParser::get_builtin_type(static_cast<GDScriptParser::IdentifierNode *>(subscript->base)->name), subscript->attribute->name, arguments);
  512. } else if (!call->is_super && subscript->base->type == GDScriptParser::Node::IDENTIFIER && call->function_name != SNAME("new") &&
  513. ClassDB::class_exists(static_cast<GDScriptParser::IdentifierNode *>(subscript->base)->name) && !Engine::get_singleton()->has_singleton(static_cast<GDScriptParser::IdentifierNode *>(subscript->base)->name)) {
  514. // It's a static native method call.
  515. gen->write_call_native_static(result, static_cast<GDScriptParser::IdentifierNode *>(subscript->base)->name, subscript->attribute->name, arguments);
  516. } else {
  517. GDScriptCodeGenerator::Address base = _parse_expression(codegen, r_error, subscript->base);
  518. if (r_error) {
  519. return GDScriptCodeGenerator::Address();
  520. }
  521. if (within_await) {
  522. gen->write_call_async(result, base, call->function_name, arguments);
  523. } else if (base.type.has_type && base.type.kind != GDScriptDataType::BUILTIN) {
  524. // Native method, use faster path.
  525. StringName class_name;
  526. if (base.type.kind == GDScriptDataType::NATIVE) {
  527. class_name = base.type.native_type;
  528. } else {
  529. class_name = base.type.native_type == StringName() ? base.type.script_type->get_instance_base_type() : base.type.native_type;
  530. }
  531. if (ClassDB::class_exists(class_name) && ClassDB::has_method(class_name, call->function_name)) {
  532. MethodBind *method = ClassDB::get_method(class_name, call->function_name);
  533. if (_have_exact_arguments(method, arguments)) {
  534. // Exact arguments, use ptrcall.
  535. gen->write_call_ptrcall(result, base, method, arguments);
  536. } else {
  537. // Not exact arguments, but still can use method bind call.
  538. gen->write_call_method_bind(result, base, method, arguments);
  539. }
  540. } else {
  541. gen->write_call(return_addr, base, call->function_name, arguments);
  542. }
  543. } else if (base.type.has_type && base.type.kind == GDScriptDataType::BUILTIN) {
  544. gen->write_call_builtin_type(result, base, base.type.builtin_type, call->function_name, arguments);
  545. } else {
  546. gen->write_call(return_addr, base, call->function_name, arguments);
  547. }
  548. if (base.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  549. gen->pop_temporary();
  550. }
  551. }
  552. } else {
  553. _set_error("Cannot call something that isn't a function.", call->callee);
  554. r_error = ERR_COMPILATION_FAILED;
  555. return GDScriptCodeGenerator::Address();
  556. }
  557. } else {
  558. r_error = ERR_COMPILATION_FAILED;
  559. return GDScriptCodeGenerator::Address();
  560. }
  561. }
  562. }
  563. for (int i = 0; i < arguments.size(); i++) {
  564. if (arguments[i].mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  565. gen->pop_temporary();
  566. }
  567. }
  568. return result;
  569. } break;
  570. case GDScriptParser::Node::GET_NODE: {
  571. const GDScriptParser::GetNodeNode *get_node = static_cast<const GDScriptParser::GetNodeNode *>(p_expression);
  572. Vector<GDScriptCodeGenerator::Address> args;
  573. args.push_back(codegen.add_constant(NodePath(get_node->full_path)));
  574. GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(get_node->get_datatype(), codegen.script));
  575. MethodBind *get_node_method = ClassDB::get_method("Node", "get_node");
  576. gen->write_call_ptrcall(result, GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::SELF), get_node_method, args);
  577. return result;
  578. } break;
  579. case GDScriptParser::Node::PRELOAD: {
  580. const GDScriptParser::PreloadNode *preload = static_cast<const GDScriptParser::PreloadNode *>(p_expression);
  581. // Add resource as constant.
  582. return codegen.add_constant(preload->resource);
  583. } break;
  584. case GDScriptParser::Node::AWAIT: {
  585. const GDScriptParser::AwaitNode *await = static_cast<const GDScriptParser::AwaitNode *>(p_expression);
  586. GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(p_expression->get_datatype(), codegen.script));
  587. within_await = true;
  588. GDScriptCodeGenerator::Address argument = _parse_expression(codegen, r_error, await->to_await);
  589. within_await = false;
  590. if (r_error) {
  591. return GDScriptCodeGenerator::Address();
  592. }
  593. gen->write_await(result, argument);
  594. if (argument.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  595. gen->pop_temporary();
  596. }
  597. return result;
  598. } break;
  599. // Indexing operator.
  600. case GDScriptParser::Node::SUBSCRIPT: {
  601. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
  602. GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(subscript->get_datatype(), codegen.script));
  603. GDScriptCodeGenerator::Address base = _parse_expression(codegen, r_error, subscript->base);
  604. if (r_error) {
  605. return GDScriptCodeGenerator::Address();
  606. }
  607. bool named = subscript->is_attribute;
  608. StringName name;
  609. GDScriptCodeGenerator::Address index;
  610. if (p_index_addr.mode != GDScriptCodeGenerator::Address::NIL) {
  611. index = p_index_addr;
  612. } else if (subscript->is_attribute) {
  613. if (subscript->base->type == GDScriptParser::Node::SELF && codegen.script) {
  614. GDScriptParser::IdentifierNode *identifier = subscript->attribute;
  615. HashMap<StringName, GDScript::MemberInfo>::Iterator MI = codegen.script->member_indices.find(identifier->name);
  616. #ifdef DEBUG_ENABLED
  617. if (MI && MI->value.getter == codegen.function_name) {
  618. String n = identifier->name;
  619. _set_error("Must use '" + n + "' instead of 'self." + n + "' in getter.", identifier);
  620. r_error = ERR_COMPILATION_FAILED;
  621. return GDScriptCodeGenerator::Address();
  622. }
  623. #endif
  624. if (MI && MI->value.getter == "") {
  625. // Remove result temp as we don't need it.
  626. gen->pop_temporary();
  627. // Faster than indexing self (as if no self. had been used).
  628. return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::MEMBER, MI->value.index, _gdtype_from_datatype(subscript->get_datatype(), codegen.script));
  629. }
  630. }
  631. name = subscript->attribute->name;
  632. named = true;
  633. } else {
  634. if (subscript->index->is_constant && subscript->index->reduced_value.get_type() == Variant::STRING_NAME) {
  635. // Also, somehow, named (speed up anyway).
  636. name = subscript->index->reduced_value;
  637. named = true;
  638. } else {
  639. // Regular indexing.
  640. index = _parse_expression(codegen, r_error, subscript->index);
  641. if (r_error) {
  642. return GDScriptCodeGenerator::Address();
  643. }
  644. }
  645. }
  646. if (named) {
  647. gen->write_get_named(result, name, base);
  648. } else {
  649. gen->write_get(result, index, base);
  650. }
  651. if (index.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  652. gen->pop_temporary();
  653. }
  654. if (base.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  655. gen->pop_temporary();
  656. }
  657. return result;
  658. } break;
  659. case GDScriptParser::Node::UNARY_OPERATOR: {
  660. const GDScriptParser::UnaryOpNode *unary = static_cast<const GDScriptParser::UnaryOpNode *>(p_expression);
  661. GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(unary->get_datatype(), codegen.script));
  662. GDScriptCodeGenerator::Address operand = _parse_expression(codegen, r_error, unary->operand);
  663. if (r_error) {
  664. return GDScriptCodeGenerator::Address();
  665. }
  666. gen->write_unary_operator(result, unary->variant_op, operand);
  667. if (operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  668. gen->pop_temporary();
  669. }
  670. return result;
  671. }
  672. case GDScriptParser::Node::BINARY_OPERATOR: {
  673. const GDScriptParser::BinaryOpNode *binary = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
  674. GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(binary->get_datatype(), codegen.script));
  675. switch (binary->operation) {
  676. case GDScriptParser::BinaryOpNode::OP_LOGIC_AND: {
  677. // AND operator with early out on failure.
  678. GDScriptCodeGenerator::Address left_operand = _parse_expression(codegen, r_error, binary->left_operand);
  679. gen->write_and_left_operand(left_operand);
  680. GDScriptCodeGenerator::Address right_operand = _parse_expression(codegen, r_error, binary->right_operand);
  681. gen->write_and_right_operand(right_operand);
  682. gen->write_end_and(result);
  683. if (right_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  684. gen->pop_temporary();
  685. }
  686. if (left_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  687. gen->pop_temporary();
  688. }
  689. } break;
  690. case GDScriptParser::BinaryOpNode::OP_LOGIC_OR: {
  691. // OR operator with early out on success.
  692. GDScriptCodeGenerator::Address left_operand = _parse_expression(codegen, r_error, binary->left_operand);
  693. gen->write_or_left_operand(left_operand);
  694. GDScriptCodeGenerator::Address right_operand = _parse_expression(codegen, r_error, binary->right_operand);
  695. gen->write_or_right_operand(right_operand);
  696. gen->write_end_or(result);
  697. if (right_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  698. gen->pop_temporary();
  699. }
  700. if (left_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  701. gen->pop_temporary();
  702. }
  703. } break;
  704. case GDScriptParser::BinaryOpNode::OP_TYPE_TEST: {
  705. GDScriptCodeGenerator::Address operand = _parse_expression(codegen, r_error, binary->left_operand);
  706. if (binary->right_operand->type == GDScriptParser::Node::IDENTIFIER && GDScriptParser::get_builtin_type(static_cast<const GDScriptParser::IdentifierNode *>(binary->right_operand)->name) != Variant::VARIANT_MAX) {
  707. // `is` with builtin type)
  708. Variant::Type type = GDScriptParser::get_builtin_type(static_cast<const GDScriptParser::IdentifierNode *>(binary->right_operand)->name);
  709. gen->write_type_test_builtin(result, operand, type);
  710. } else {
  711. GDScriptCodeGenerator::Address type = _parse_expression(codegen, r_error, binary->right_operand);
  712. if (r_error) {
  713. return GDScriptCodeGenerator::Address();
  714. }
  715. gen->write_type_test(result, operand, type);
  716. if (type.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  717. gen->pop_temporary();
  718. }
  719. }
  720. if (operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  721. gen->pop_temporary();
  722. }
  723. } break;
  724. default: {
  725. GDScriptCodeGenerator::Address left_operand = _parse_expression(codegen, r_error, binary->left_operand);
  726. GDScriptCodeGenerator::Address right_operand = _parse_expression(codegen, r_error, binary->right_operand);
  727. gen->write_binary_operator(result, binary->variant_op, left_operand, right_operand);
  728. if (right_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  729. gen->pop_temporary();
  730. }
  731. if (left_operand.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  732. gen->pop_temporary();
  733. }
  734. }
  735. }
  736. return result;
  737. } break;
  738. case GDScriptParser::Node::TERNARY_OPERATOR: {
  739. // x IF a ELSE y operator with early out on failure.
  740. const GDScriptParser::TernaryOpNode *ternary = static_cast<const GDScriptParser::TernaryOpNode *>(p_expression);
  741. GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(ternary->get_datatype(), codegen.script));
  742. gen->write_start_ternary(result);
  743. GDScriptCodeGenerator::Address condition = _parse_expression(codegen, r_error, ternary->condition);
  744. if (r_error) {
  745. return GDScriptCodeGenerator::Address();
  746. }
  747. gen->write_ternary_condition(condition);
  748. if (condition.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  749. gen->pop_temporary();
  750. }
  751. GDScriptCodeGenerator::Address true_expr = _parse_expression(codegen, r_error, ternary->true_expr);
  752. if (r_error) {
  753. return GDScriptCodeGenerator::Address();
  754. }
  755. gen->write_ternary_true_expr(true_expr);
  756. if (true_expr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  757. gen->pop_temporary();
  758. }
  759. GDScriptCodeGenerator::Address false_expr = _parse_expression(codegen, r_error, ternary->false_expr);
  760. if (r_error) {
  761. return GDScriptCodeGenerator::Address();
  762. }
  763. gen->write_ternary_false_expr(false_expr);
  764. if (false_expr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  765. gen->pop_temporary();
  766. }
  767. gen->write_end_ternary();
  768. return result;
  769. } break;
  770. case GDScriptParser::Node::ASSIGNMENT: {
  771. const GDScriptParser::AssignmentNode *assignment = static_cast<const GDScriptParser::AssignmentNode *>(p_expression);
  772. if (assignment->assignee->type == GDScriptParser::Node::SUBSCRIPT) {
  773. // SET (chained) MODE!
  774. const GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(assignment->assignee);
  775. #ifdef DEBUG_ENABLED
  776. if (subscript->is_attribute && subscript->base->type == GDScriptParser::Node::SELF && codegen.script) {
  777. HashMap<StringName, GDScript::MemberInfo>::Iterator MI = codegen.script->member_indices.find(subscript->attribute->name);
  778. if (MI && MI->value.setter == codegen.function_name) {
  779. String n = subscript->attribute->name;
  780. _set_error("Must use '" + n + "' instead of 'self." + n + "' in setter.", subscript);
  781. r_error = ERR_COMPILATION_FAILED;
  782. return GDScriptCodeGenerator::Address();
  783. }
  784. }
  785. #endif
  786. /* Find chain of sets */
  787. StringName assign_class_member_property;
  788. GDScriptCodeGenerator::Address target_member_property;
  789. bool is_member_property = false;
  790. bool member_property_has_setter = false;
  791. bool member_property_is_in_setter = false;
  792. StringName member_property_setter_function;
  793. List<const GDScriptParser::SubscriptNode *> chain;
  794. {
  795. // Create get/set chain.
  796. const GDScriptParser::SubscriptNode *n = subscript;
  797. while (true) {
  798. chain.push_back(n);
  799. if (n->base->type != GDScriptParser::Node::SUBSCRIPT) {
  800. // Check for a property.
  801. if (n->base->type == GDScriptParser::Node::IDENTIFIER) {
  802. GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(n->base);
  803. StringName var_name = identifier->name;
  804. if (_is_class_member_property(codegen, var_name)) {
  805. assign_class_member_property = var_name;
  806. } else if (!codegen.locals.has(var_name) && codegen.script->member_indices.has(var_name)) {
  807. is_member_property = true;
  808. member_property_setter_function = codegen.script->member_indices[var_name].setter;
  809. member_property_has_setter = member_property_setter_function != StringName();
  810. member_property_is_in_setter = member_property_has_setter && member_property_setter_function == codegen.function_name;
  811. target_member_property.mode = GDScriptCodeGenerator::Address::MEMBER;
  812. target_member_property.address = codegen.script->member_indices[var_name].index;
  813. target_member_property.type = codegen.script->member_indices[var_name].data_type;
  814. }
  815. }
  816. break;
  817. }
  818. n = static_cast<const GDScriptParser::SubscriptNode *>(n->base);
  819. }
  820. }
  821. /* Chain of gets */
  822. // Get at (potential) root stack pos, so it can be returned.
  823. GDScriptCodeGenerator::Address base = _parse_expression(codegen, r_error, chain.back()->get()->base);
  824. if (r_error) {
  825. return GDScriptCodeGenerator::Address();
  826. }
  827. GDScriptCodeGenerator::Address prev_base = base;
  828. struct ChainInfo {
  829. bool is_named = false;
  830. GDScriptCodeGenerator::Address base;
  831. GDScriptCodeGenerator::Address key;
  832. StringName name;
  833. };
  834. List<ChainInfo> set_chain;
  835. for (List<const GDScriptParser::SubscriptNode *>::Element *E = chain.back(); E; E = E->prev()) {
  836. if (E == chain.front()) {
  837. // Skip the main subscript, since we'll assign to that.
  838. break;
  839. }
  840. const GDScriptParser::SubscriptNode *subscript_elem = E->get();
  841. GDScriptCodeGenerator::Address value = codegen.add_temporary(_gdtype_from_datatype(subscript_elem->get_datatype(), codegen.script));
  842. GDScriptCodeGenerator::Address key;
  843. StringName name;
  844. if (subscript_elem->is_attribute) {
  845. name = subscript_elem->attribute->name;
  846. gen->write_get_named(value, name, prev_base);
  847. } else {
  848. key = _parse_expression(codegen, r_error, subscript_elem->index);
  849. if (r_error) {
  850. return GDScriptCodeGenerator::Address();
  851. }
  852. gen->write_get(value, key, prev_base);
  853. }
  854. // Store base and key for setting it back later.
  855. set_chain.push_front({ subscript_elem->is_attribute, prev_base, key, name }); // Push to front to invert the list.
  856. prev_base = value;
  857. }
  858. // Get value to assign.
  859. GDScriptCodeGenerator::Address assigned = _parse_expression(codegen, r_error, assignment->assigned_value);
  860. if (r_error) {
  861. return GDScriptCodeGenerator::Address();
  862. }
  863. // Get the key if needed.
  864. GDScriptCodeGenerator::Address key;
  865. StringName name;
  866. if (subscript->is_attribute) {
  867. name = subscript->attribute->name;
  868. } else {
  869. key = _parse_expression(codegen, r_error, subscript->index);
  870. if (r_error) {
  871. return GDScriptCodeGenerator::Address();
  872. }
  873. }
  874. // Perform operator if any.
  875. if (assignment->operation != GDScriptParser::AssignmentNode::OP_NONE) {
  876. GDScriptCodeGenerator::Address op_result = codegen.add_temporary(_gdtype_from_datatype(assignment->get_datatype(), codegen.script));
  877. GDScriptCodeGenerator::Address value = codegen.add_temporary(_gdtype_from_datatype(subscript->get_datatype(), codegen.script));
  878. if (subscript->is_attribute) {
  879. gen->write_get_named(value, name, prev_base);
  880. } else {
  881. gen->write_get(value, key, prev_base);
  882. }
  883. gen->write_binary_operator(op_result, assignment->variant_op, value, assigned);
  884. gen->pop_temporary();
  885. if (assigned.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  886. gen->pop_temporary();
  887. }
  888. assigned = op_result;
  889. }
  890. // Perform assignment.
  891. if (subscript->is_attribute) {
  892. gen->write_set_named(prev_base, name, assigned);
  893. } else {
  894. gen->write_set(prev_base, key, assigned);
  895. }
  896. if (key.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  897. gen->pop_temporary();
  898. }
  899. if (assigned.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  900. gen->pop_temporary();
  901. }
  902. assigned = prev_base;
  903. // Set back the values into their bases.
  904. for (const ChainInfo &info : set_chain) {
  905. bool known_type = assigned.type.has_type;
  906. bool is_shared = Variant::is_type_shared(assigned.type.builtin_type);
  907. if (!known_type || !is_shared) {
  908. if (!known_type) {
  909. // Jump shared values since they are already updated in-place.
  910. gen->write_jump_if_shared(assigned);
  911. }
  912. if (!info.is_named) {
  913. gen->write_set(info.base, info.key, assigned);
  914. } else {
  915. gen->write_set_named(info.base, info.name, assigned);
  916. }
  917. if (!known_type) {
  918. gen->write_end_jump_if_shared();
  919. }
  920. }
  921. if (!info.is_named && info.key.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  922. gen->pop_temporary();
  923. }
  924. if (assigned.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  925. gen->pop_temporary();
  926. }
  927. assigned = info.base;
  928. }
  929. bool known_type = assigned.type.has_type;
  930. bool is_shared = Variant::is_type_shared(assigned.type.builtin_type);
  931. if (!known_type || !is_shared) {
  932. // If this is a class member property, also assign to it.
  933. // This allow things like: position.x += 2.0
  934. if (assign_class_member_property != StringName()) {
  935. if (!known_type) {
  936. gen->write_jump_if_shared(assigned);
  937. }
  938. gen->write_set_member(assigned, assign_class_member_property);
  939. if (!known_type) {
  940. gen->write_end_jump_if_shared();
  941. }
  942. } else if (is_member_property) {
  943. // Same as above but for script members.
  944. if (!known_type) {
  945. gen->write_jump_if_shared(assigned);
  946. }
  947. if (member_property_has_setter && !member_property_is_in_setter) {
  948. Vector<GDScriptCodeGenerator::Address> args;
  949. args.push_back(assigned);
  950. gen->write_call(GDScriptCodeGenerator::Address(), GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::SELF), member_property_setter_function, args);
  951. } else {
  952. gen->write_assign(target_member_property, assigned);
  953. }
  954. if (!known_type) {
  955. gen->write_end_jump_if_shared();
  956. }
  957. }
  958. }
  959. if (assigned.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  960. gen->pop_temporary();
  961. }
  962. } else if (assignment->assignee->type == GDScriptParser::Node::IDENTIFIER && _is_class_member_property(codegen, static_cast<GDScriptParser::IdentifierNode *>(assignment->assignee)->name)) {
  963. // Assignment to member property.
  964. GDScriptCodeGenerator::Address assigned_value = _parse_expression(codegen, r_error, assignment->assigned_value);
  965. if (r_error) {
  966. return GDScriptCodeGenerator::Address();
  967. }
  968. GDScriptCodeGenerator::Address to_assign = assigned_value;
  969. bool has_operation = assignment->operation != GDScriptParser::AssignmentNode::OP_NONE;
  970. StringName name = static_cast<GDScriptParser::IdentifierNode *>(assignment->assignee)->name;
  971. if (has_operation) {
  972. GDScriptCodeGenerator::Address op_result = codegen.add_temporary(_gdtype_from_datatype(assignment->get_datatype(), codegen.script));
  973. GDScriptCodeGenerator::Address member = codegen.add_temporary(_gdtype_from_datatype(assignment->assignee->get_datatype(), codegen.script));
  974. gen->write_get_member(member, name);
  975. gen->write_binary_operator(op_result, assignment->variant_op, member, assigned_value);
  976. gen->pop_temporary(); // Pop member temp.
  977. to_assign = op_result;
  978. }
  979. gen->write_set_member(to_assign, name);
  980. if (to_assign.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  981. gen->pop_temporary(); // Pop the assigned expression or the temp result if it has operation.
  982. }
  983. if (has_operation && assigned_value.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  984. gen->pop_temporary(); // Pop the assigned expression if not done before.
  985. }
  986. } else {
  987. // Regular assignment.
  988. ERR_FAIL_COND_V_MSG(assignment->assignee->type != GDScriptParser::Node::IDENTIFIER, GDScriptCodeGenerator::Address(), "Expected the assignee to be an identifier here.");
  989. GDScriptCodeGenerator::Address member;
  990. bool is_member = false;
  991. bool has_setter = false;
  992. bool is_in_setter = false;
  993. StringName setter_function;
  994. StringName var_name = static_cast<const GDScriptParser::IdentifierNode *>(assignment->assignee)->name;
  995. if (!codegen.locals.has(var_name) && codegen.script->member_indices.has(var_name)) {
  996. is_member = true;
  997. setter_function = codegen.script->member_indices[var_name].setter;
  998. has_setter = setter_function != StringName();
  999. is_in_setter = has_setter && setter_function == codegen.function_name;
  1000. member.mode = GDScriptCodeGenerator::Address::MEMBER;
  1001. member.address = codegen.script->member_indices[var_name].index;
  1002. member.type = codegen.script->member_indices[var_name].data_type;
  1003. }
  1004. GDScriptCodeGenerator::Address target;
  1005. if (is_member) {
  1006. target = member; // _parse_expression could call its getter, but we want to know the actual address
  1007. } else {
  1008. target = _parse_expression(codegen, r_error, assignment->assignee);
  1009. if (r_error) {
  1010. return GDScriptCodeGenerator::Address();
  1011. }
  1012. }
  1013. GDScriptCodeGenerator::Address assigned_value = _parse_expression(codegen, r_error, assignment->assigned_value);
  1014. if (r_error) {
  1015. return GDScriptCodeGenerator::Address();
  1016. }
  1017. GDScriptCodeGenerator::Address to_assign;
  1018. bool has_operation = assignment->operation != GDScriptParser::AssignmentNode::OP_NONE;
  1019. if (has_operation) {
  1020. // Perform operation.
  1021. GDScriptCodeGenerator::Address op_result = codegen.add_temporary(_gdtype_from_datatype(assignment->get_datatype(), codegen.script));
  1022. GDScriptCodeGenerator::Address og_value = _parse_expression(codegen, r_error, assignment->assignee);
  1023. gen->write_binary_operator(op_result, assignment->variant_op, og_value, assigned_value);
  1024. to_assign = op_result;
  1025. if (og_value.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1026. gen->pop_temporary();
  1027. }
  1028. } else {
  1029. to_assign = assigned_value;
  1030. }
  1031. GDScriptDataType assign_type = _gdtype_from_datatype(assignment->assignee->get_datatype(), codegen.script);
  1032. if (has_setter && !is_in_setter) {
  1033. // Call setter.
  1034. Vector<GDScriptCodeGenerator::Address> args;
  1035. args.push_back(to_assign);
  1036. gen->write_call(GDScriptCodeGenerator::Address(), GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::SELF), setter_function, args);
  1037. } else {
  1038. // Just assign.
  1039. if (assignment->use_conversion_assign) {
  1040. gen->write_assign_with_conversion(target, to_assign);
  1041. } else {
  1042. gen->write_assign(target, to_assign);
  1043. }
  1044. }
  1045. if (to_assign.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1046. gen->pop_temporary(); // Pop assigned value or temp operation result.
  1047. }
  1048. if (has_operation && assigned_value.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1049. gen->pop_temporary(); // Pop assigned value if not done before.
  1050. }
  1051. if (target.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1052. gen->pop_temporary(); // Pop the target to assignment.
  1053. }
  1054. }
  1055. return GDScriptCodeGenerator::Address(); // Assignment does not return a value.
  1056. } break;
  1057. case GDScriptParser::Node::LAMBDA: {
  1058. const GDScriptParser::LambdaNode *lambda = static_cast<const GDScriptParser::LambdaNode *>(p_expression);
  1059. GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(lambda->get_datatype(), codegen.script));
  1060. Vector<GDScriptCodeGenerator::Address> captures;
  1061. captures.resize(lambda->captures.size());
  1062. for (int i = 0; i < lambda->captures.size(); i++) {
  1063. captures.write[i] = _parse_expression(codegen, r_error, lambda->captures[i]);
  1064. if (r_error) {
  1065. return GDScriptCodeGenerator::Address();
  1066. }
  1067. }
  1068. GDScriptFunction *function = _parse_function(r_error, codegen.script, codegen.class_node, lambda->function, false, true);
  1069. if (r_error) {
  1070. return GDScriptCodeGenerator::Address();
  1071. }
  1072. gen->write_lambda(result, function, captures, lambda->use_self);
  1073. for (int i = 0; i < captures.size(); i++) {
  1074. if (captures[i].mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1075. gen->pop_temporary();
  1076. }
  1077. }
  1078. return result;
  1079. } break;
  1080. default: {
  1081. ERR_FAIL_V_MSG(GDScriptCodeGenerator::Address(), "Bug in bytecode compiler, unexpected node in parse tree while parsing expression."); // Unreachable code.
  1082. } break;
  1083. }
  1084. }
  1085. 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) {
  1086. switch (p_pattern->pattern_type) {
  1087. case GDScriptParser::PatternNode::PT_LITERAL: {
  1088. if (p_is_nested) {
  1089. codegen.generator->write_and_left_operand(p_previous_test);
  1090. } else if (!p_is_first) {
  1091. codegen.generator->write_or_left_operand(p_previous_test);
  1092. }
  1093. // Get literal type into constant map.
  1094. GDScriptCodeGenerator::Address literal_type_addr = codegen.add_constant((int)p_pattern->literal->value.get_type());
  1095. // Equality is always a boolean.
  1096. GDScriptDataType equality_type;
  1097. equality_type.has_type = true;
  1098. equality_type.kind = GDScriptDataType::BUILTIN;
  1099. equality_type.builtin_type = Variant::BOOL;
  1100. // Check type equality.
  1101. GDScriptCodeGenerator::Address type_equality_addr = codegen.add_temporary(equality_type);
  1102. codegen.generator->write_binary_operator(type_equality_addr, Variant::OP_EQUAL, p_type_addr, literal_type_addr);
  1103. codegen.generator->write_and_left_operand(type_equality_addr);
  1104. // Get literal.
  1105. GDScriptCodeGenerator::Address literal_addr = _parse_expression(codegen, r_error, p_pattern->literal);
  1106. if (r_error) {
  1107. return GDScriptCodeGenerator::Address();
  1108. }
  1109. // Check value equality.
  1110. GDScriptCodeGenerator::Address equality_addr = codegen.add_temporary(equality_type);
  1111. codegen.generator->write_binary_operator(equality_addr, Variant::OP_EQUAL, p_value_addr, literal_addr);
  1112. codegen.generator->write_and_right_operand(equality_addr);
  1113. // AND both together (reuse temporary location).
  1114. codegen.generator->write_end_and(type_equality_addr);
  1115. codegen.generator->pop_temporary(); // Remove equality_addr from stack.
  1116. if (literal_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1117. codegen.generator->pop_temporary();
  1118. }
  1119. // If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead.
  1120. if (p_is_nested) {
  1121. // Use the previous value as target, since we only need one temporary variable.
  1122. codegen.generator->write_and_right_operand(type_equality_addr);
  1123. codegen.generator->write_end_and(p_previous_test);
  1124. } else if (!p_is_first) {
  1125. // Use the previous value as target, since we only need one temporary variable.
  1126. codegen.generator->write_or_right_operand(type_equality_addr);
  1127. codegen.generator->write_end_or(p_previous_test);
  1128. } else {
  1129. // Just assign this value to the accumulator temporary.
  1130. codegen.generator->write_assign(p_previous_test, type_equality_addr);
  1131. }
  1132. codegen.generator->pop_temporary(); // Remove type_equality_addr.
  1133. return p_previous_test;
  1134. } break;
  1135. case GDScriptParser::PatternNode::PT_EXPRESSION: {
  1136. if (p_is_nested) {
  1137. codegen.generator->write_and_left_operand(p_previous_test);
  1138. } else if (!p_is_first) {
  1139. codegen.generator->write_or_left_operand(p_previous_test);
  1140. }
  1141. // Create the result temps first since it's the last to go away.
  1142. GDScriptCodeGenerator::Address result_addr = codegen.add_temporary();
  1143. GDScriptCodeGenerator::Address equality_test_addr = codegen.add_temporary();
  1144. // Evaluate expression.
  1145. GDScriptCodeGenerator::Address expr_addr;
  1146. expr_addr = _parse_expression(codegen, r_error, p_pattern->expression);
  1147. if (r_error) {
  1148. return GDScriptCodeGenerator::Address();
  1149. }
  1150. // Evaluate expression type.
  1151. Vector<GDScriptCodeGenerator::Address> typeof_args;
  1152. typeof_args.push_back(expr_addr);
  1153. codegen.generator->write_call_utility(result_addr, "typeof", typeof_args);
  1154. // Check type equality.
  1155. codegen.generator->write_binary_operator(result_addr, Variant::OP_EQUAL, p_type_addr, result_addr);
  1156. codegen.generator->write_and_left_operand(result_addr);
  1157. // Check value equality.
  1158. codegen.generator->write_binary_operator(equality_test_addr, Variant::OP_EQUAL, p_value_addr, expr_addr);
  1159. codegen.generator->write_and_right_operand(equality_test_addr);
  1160. // AND both type and value equality.
  1161. codegen.generator->write_end_and(result_addr);
  1162. // We don't need the expression temporary anymore.
  1163. if (expr_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1164. codegen.generator->pop_temporary();
  1165. }
  1166. codegen.generator->pop_temporary(); // Remove type equality temporary.
  1167. // If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead.
  1168. if (p_is_nested) {
  1169. // Use the previous value as target, since we only need one temporary variable.
  1170. codegen.generator->write_and_right_operand(result_addr);
  1171. codegen.generator->write_end_and(p_previous_test);
  1172. } else if (!p_is_first) {
  1173. // Use the previous value as target, since we only need one temporary variable.
  1174. codegen.generator->write_or_right_operand(result_addr);
  1175. codegen.generator->write_end_or(p_previous_test);
  1176. } else {
  1177. // Just assign this value to the accumulator temporary.
  1178. codegen.generator->write_assign(p_previous_test, result_addr);
  1179. }
  1180. codegen.generator->pop_temporary(); // Remove temp result addr.
  1181. return p_previous_test;
  1182. } break;
  1183. case GDScriptParser::PatternNode::PT_ARRAY: {
  1184. if (p_is_nested) {
  1185. codegen.generator->write_and_left_operand(p_previous_test);
  1186. } else if (!p_is_first) {
  1187. codegen.generator->write_or_left_operand(p_previous_test);
  1188. }
  1189. // Get array type into constant map.
  1190. GDScriptCodeGenerator::Address array_type_addr = codegen.add_constant((int)Variant::ARRAY);
  1191. // Equality is always a boolean.
  1192. GDScriptDataType temp_type;
  1193. temp_type.has_type = true;
  1194. temp_type.kind = GDScriptDataType::BUILTIN;
  1195. temp_type.builtin_type = Variant::BOOL;
  1196. // Check type equality.
  1197. GDScriptCodeGenerator::Address result_addr = codegen.add_temporary(temp_type);
  1198. codegen.generator->write_binary_operator(result_addr, Variant::OP_EQUAL, p_type_addr, array_type_addr);
  1199. codegen.generator->write_and_left_operand(result_addr);
  1200. // Store pattern length in constant map.
  1201. GDScriptCodeGenerator::Address array_length_addr = codegen.add_constant(p_pattern->rest_used ? p_pattern->array.size() - 1 : p_pattern->array.size());
  1202. // Get value length.
  1203. temp_type.builtin_type = Variant::INT;
  1204. GDScriptCodeGenerator::Address value_length_addr = codegen.add_temporary(temp_type);
  1205. Vector<GDScriptCodeGenerator::Address> len_args;
  1206. len_args.push_back(p_value_addr);
  1207. codegen.generator->write_call_gdscript_utility(value_length_addr, GDScriptUtilityFunctions::get_function("len"), len_args);
  1208. // Test length compatibility.
  1209. temp_type.builtin_type = Variant::BOOL;
  1210. GDScriptCodeGenerator::Address length_compat_addr = codegen.add_temporary(temp_type);
  1211. 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);
  1212. codegen.generator->write_and_right_operand(length_compat_addr);
  1213. // AND type and length check.
  1214. codegen.generator->write_end_and(result_addr);
  1215. // Remove length temporaries.
  1216. codegen.generator->pop_temporary();
  1217. codegen.generator->pop_temporary();
  1218. // Create temporaries outside the loop so they can be reused.
  1219. GDScriptCodeGenerator::Address element_addr = codegen.add_temporary();
  1220. GDScriptCodeGenerator::Address element_type_addr = codegen.add_temporary();
  1221. // Evaluate element by element.
  1222. for (int i = 0; i < p_pattern->array.size(); i++) {
  1223. if (p_pattern->array[i]->pattern_type == GDScriptParser::PatternNode::PT_REST) {
  1224. // Don't want to access an extra element of the user array.
  1225. break;
  1226. }
  1227. // 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).
  1228. codegen.generator->write_and_left_operand(result_addr);
  1229. // Add index to constant map.
  1230. GDScriptCodeGenerator::Address index_addr = codegen.add_constant(i);
  1231. // Get the actual element from the user-sent array.
  1232. codegen.generator->write_get(element_addr, index_addr, p_value_addr);
  1233. // Also get type of element.
  1234. Vector<GDScriptCodeGenerator::Address> typeof_args;
  1235. typeof_args.push_back(element_addr);
  1236. codegen.generator->write_call_utility(element_type_addr, "typeof", typeof_args);
  1237. // Try the pattern inside the element.
  1238. result_addr = _parse_match_pattern(codegen, r_error, p_pattern->array[i], element_addr, element_type_addr, result_addr, false, true);
  1239. if (r_error != OK) {
  1240. return GDScriptCodeGenerator::Address();
  1241. }
  1242. codegen.generator->write_and_right_operand(result_addr);
  1243. codegen.generator->write_end_and(result_addr);
  1244. }
  1245. // Remove element temporaries.
  1246. codegen.generator->pop_temporary();
  1247. codegen.generator->pop_temporary();
  1248. // If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead.
  1249. if (p_is_nested) {
  1250. // Use the previous value as target, since we only need one temporary variable.
  1251. codegen.generator->write_and_right_operand(result_addr);
  1252. codegen.generator->write_end_and(p_previous_test);
  1253. } else if (!p_is_first) {
  1254. // Use the previous value as target, since we only need one temporary variable.
  1255. codegen.generator->write_or_right_operand(result_addr);
  1256. codegen.generator->write_end_or(p_previous_test);
  1257. } else {
  1258. // Just assign this value to the accumulator temporary.
  1259. codegen.generator->write_assign(p_previous_test, result_addr);
  1260. }
  1261. codegen.generator->pop_temporary(); // Remove temp result addr.
  1262. return p_previous_test;
  1263. } break;
  1264. case GDScriptParser::PatternNode::PT_DICTIONARY: {
  1265. if (p_is_nested) {
  1266. codegen.generator->write_and_left_operand(p_previous_test);
  1267. } else if (!p_is_first) {
  1268. codegen.generator->write_or_left_operand(p_previous_test);
  1269. }
  1270. // Get dictionary type into constant map.
  1271. GDScriptCodeGenerator::Address dict_type_addr = codegen.add_constant((int)Variant::DICTIONARY);
  1272. // Equality is always a boolean.
  1273. GDScriptDataType temp_type;
  1274. temp_type.has_type = true;
  1275. temp_type.kind = GDScriptDataType::BUILTIN;
  1276. temp_type.builtin_type = Variant::BOOL;
  1277. // Check type equality.
  1278. GDScriptCodeGenerator::Address result_addr = codegen.add_temporary(temp_type);
  1279. codegen.generator->write_binary_operator(result_addr, Variant::OP_EQUAL, p_type_addr, dict_type_addr);
  1280. codegen.generator->write_and_left_operand(result_addr);
  1281. // Store pattern length in constant map.
  1282. GDScriptCodeGenerator::Address dict_length_addr = codegen.add_constant(p_pattern->rest_used ? p_pattern->dictionary.size() - 1 : p_pattern->dictionary.size());
  1283. // Get user's dictionary length.
  1284. temp_type.builtin_type = Variant::INT;
  1285. GDScriptCodeGenerator::Address value_length_addr = codegen.add_temporary(temp_type);
  1286. Vector<GDScriptCodeGenerator::Address> func_args;
  1287. func_args.push_back(p_value_addr);
  1288. codegen.generator->write_call_gdscript_utility(value_length_addr, GDScriptUtilityFunctions::get_function("len"), func_args);
  1289. // Test length compatibility.
  1290. temp_type.builtin_type = Variant::BOOL;
  1291. GDScriptCodeGenerator::Address length_compat_addr = codegen.add_temporary(temp_type);
  1292. 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);
  1293. codegen.generator->write_and_right_operand(length_compat_addr);
  1294. // AND type and length check.
  1295. codegen.generator->write_end_and(result_addr);
  1296. // Remove length temporaries.
  1297. codegen.generator->pop_temporary();
  1298. codegen.generator->pop_temporary();
  1299. // Create temporaries outside the loop so they can be reused.
  1300. GDScriptCodeGenerator::Address element_addr = codegen.add_temporary();
  1301. GDScriptCodeGenerator::Address element_type_addr = codegen.add_temporary();
  1302. // Evaluate element by element.
  1303. for (int i = 0; i < p_pattern->dictionary.size(); i++) {
  1304. const GDScriptParser::PatternNode::Pair &element = p_pattern->dictionary[i];
  1305. if (element.value_pattern && element.value_pattern->pattern_type == GDScriptParser::PatternNode::PT_REST) {
  1306. // Ignore rest pattern.
  1307. break;
  1308. }
  1309. // 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).
  1310. codegen.generator->write_and_left_operand(result_addr);
  1311. // Get the pattern key.
  1312. GDScriptCodeGenerator::Address pattern_key_addr = _parse_expression(codegen, r_error, element.key);
  1313. if (r_error) {
  1314. return GDScriptCodeGenerator::Address();
  1315. }
  1316. // Check if pattern key exists in user's dictionary. This will be AND-ed with next result.
  1317. func_args.clear();
  1318. func_args.push_back(pattern_key_addr);
  1319. codegen.generator->write_call(result_addr, p_value_addr, "has", func_args);
  1320. if (element.value_pattern != nullptr) {
  1321. // 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).
  1322. codegen.generator->write_and_left_operand(result_addr);
  1323. // Get actual value from user dictionary.
  1324. codegen.generator->write_get(element_addr, pattern_key_addr, p_value_addr);
  1325. // Also get type of value.
  1326. func_args.clear();
  1327. func_args.push_back(element_addr);
  1328. codegen.generator->write_call_utility(element_type_addr, "typeof", func_args);
  1329. // Try the pattern inside the value.
  1330. result_addr = _parse_match_pattern(codegen, r_error, element.value_pattern, element_addr, element_type_addr, result_addr, false, true);
  1331. if (r_error != OK) {
  1332. return GDScriptCodeGenerator::Address();
  1333. }
  1334. codegen.generator->write_and_right_operand(result_addr);
  1335. codegen.generator->write_end_and(result_addr);
  1336. }
  1337. codegen.generator->write_and_right_operand(result_addr);
  1338. codegen.generator->write_end_and(result_addr);
  1339. // Remove pattern key temporary.
  1340. if (pattern_key_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1341. codegen.generator->pop_temporary();
  1342. }
  1343. }
  1344. // Remove element temporaries.
  1345. codegen.generator->pop_temporary();
  1346. codegen.generator->pop_temporary();
  1347. // If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead.
  1348. if (p_is_nested) {
  1349. // Use the previous value as target, since we only need one temporary variable.
  1350. codegen.generator->write_and_right_operand(result_addr);
  1351. codegen.generator->write_end_and(p_previous_test);
  1352. } else if (!p_is_first) {
  1353. // Use the previous value as target, since we only need one temporary variable.
  1354. codegen.generator->write_or_right_operand(result_addr);
  1355. codegen.generator->write_end_or(p_previous_test);
  1356. } else {
  1357. // Just assign this value to the accumulator temporary.
  1358. codegen.generator->write_assign(p_previous_test, result_addr);
  1359. }
  1360. codegen.generator->pop_temporary(); // Remove temp result addr.
  1361. return p_previous_test;
  1362. } break;
  1363. case GDScriptParser::PatternNode::PT_REST:
  1364. // Do nothing.
  1365. return p_previous_test;
  1366. break;
  1367. case GDScriptParser::PatternNode::PT_BIND: {
  1368. if (p_is_nested) {
  1369. codegen.generator->write_and_left_operand(p_previous_test);
  1370. } else if (!p_is_first) {
  1371. codegen.generator->write_or_left_operand(p_previous_test);
  1372. }
  1373. // Get the bind address.
  1374. GDScriptCodeGenerator::Address bind = codegen.locals[p_pattern->bind->name];
  1375. // Assign value to bound variable.
  1376. codegen.generator->write_assign(bind, p_value_addr);
  1377. }
  1378. [[fallthrough]]; // Act like matching anything too.
  1379. case GDScriptParser::PatternNode::PT_WILDCARD:
  1380. // If this is a fall through we don't want to do this again.
  1381. if (p_pattern->pattern_type != GDScriptParser::PatternNode::PT_BIND) {
  1382. if (p_is_nested) {
  1383. codegen.generator->write_and_left_operand(p_previous_test);
  1384. } else if (!p_is_first) {
  1385. codegen.generator->write_or_left_operand(p_previous_test);
  1386. }
  1387. }
  1388. // This matches anything so just do the same as `if(true)`.
  1389. // If this isn't the first, we need to OR with the previous pattern. If it's nested, we use AND instead.
  1390. if (p_is_nested) {
  1391. // Use the operator with the `true` constant so it works as always matching.
  1392. GDScriptCodeGenerator::Address constant = codegen.add_constant(true);
  1393. codegen.generator->write_and_right_operand(constant);
  1394. codegen.generator->write_end_and(p_previous_test);
  1395. } else if (!p_is_first) {
  1396. // Use the operator with the `true` constant so it works as always matching.
  1397. GDScriptCodeGenerator::Address constant = codegen.add_constant(true);
  1398. codegen.generator->write_or_right_operand(constant);
  1399. codegen.generator->write_end_or(p_previous_test);
  1400. } else {
  1401. // Just assign this value to the accumulator temporary.
  1402. codegen.generator->write_assign_true(p_previous_test);
  1403. }
  1404. return p_previous_test;
  1405. }
  1406. ERR_FAIL_V_MSG(p_previous_test, "Reaching the end of pattern compilation without matching a pattern.");
  1407. }
  1408. void GDScriptCompiler::_add_locals_in_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block) {
  1409. for (int i = 0; i < p_block->locals.size(); i++) {
  1410. if (p_block->locals[i].type == GDScriptParser::SuiteNode::Local::PARAMETER || p_block->locals[i].type == GDScriptParser::SuiteNode::Local::FOR_VARIABLE) {
  1411. // Parameters are added directly from function and loop variables are declared explicitly.
  1412. continue;
  1413. }
  1414. codegen.add_local(p_block->locals[i].name, _gdtype_from_datatype(p_block->locals[i].get_datatype(), codegen.script));
  1415. }
  1416. }
  1417. Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, bool p_add_locals) {
  1418. Error err = OK;
  1419. GDScriptCodeGenerator *gen = codegen.generator;
  1420. codegen.start_block();
  1421. if (p_add_locals) {
  1422. _add_locals_in_block(codegen, p_block);
  1423. }
  1424. for (int i = 0; i < p_block->statements.size(); i++) {
  1425. const GDScriptParser::Node *s = p_block->statements[i];
  1426. #ifdef DEBUG_ENABLED
  1427. // Add a newline before each statement, since the debugger needs those.
  1428. gen->write_newline(s->start_line);
  1429. #endif
  1430. switch (s->type) {
  1431. case GDScriptParser::Node::MATCH: {
  1432. const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(s);
  1433. gen->start_match();
  1434. codegen.start_block();
  1435. // Evaluate the match expression.
  1436. GDScriptCodeGenerator::Address value = codegen.add_local("@match_value", _gdtype_from_datatype(match->test->get_datatype(), codegen.script));
  1437. GDScriptCodeGenerator::Address value_expr = _parse_expression(codegen, err, match->test);
  1438. if (err) {
  1439. return err;
  1440. }
  1441. // Assign to local.
  1442. // TODO: This can be improved by passing the target to parse_expression().
  1443. gen->write_assign(value, value_expr);
  1444. if (value_expr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1445. codegen.generator->pop_temporary();
  1446. }
  1447. // Then, let's save the type of the value in the stack too, so we can reuse for later comparisons.
  1448. GDScriptDataType typeof_type;
  1449. typeof_type.has_type = true;
  1450. typeof_type.kind = GDScriptDataType::BUILTIN;
  1451. typeof_type.builtin_type = Variant::INT;
  1452. GDScriptCodeGenerator::Address type = codegen.add_local("@match_type", typeof_type);
  1453. Vector<GDScriptCodeGenerator::Address> typeof_args;
  1454. typeof_args.push_back(value);
  1455. gen->write_call_utility(type, "typeof", typeof_args);
  1456. // Now we can actually start testing.
  1457. // For each branch.
  1458. for (int j = 0; j < match->branches.size(); j++) {
  1459. if (j > 0) {
  1460. // Use `else` to not check the next branch after matching.
  1461. gen->write_else();
  1462. }
  1463. const GDScriptParser::MatchBranchNode *branch = match->branches[j];
  1464. gen->start_match_branch(); // Need so lower level code can patch 'continue' jumps.
  1465. codegen.start_block(); // Create an extra block around for binds.
  1466. // Add locals in block before patterns, so temporaries don't use the stack address for binds.
  1467. _add_locals_in_block(codegen, branch->block);
  1468. #ifdef DEBUG_ENABLED
  1469. // Add a newline before each branch, since the debugger needs those.
  1470. gen->write_newline(branch->start_line);
  1471. #endif
  1472. // For each pattern in branch.
  1473. GDScriptCodeGenerator::Address pattern_result = codegen.add_temporary();
  1474. for (int k = 0; k < branch->patterns.size(); k++) {
  1475. pattern_result = _parse_match_pattern(codegen, err, branch->patterns[k], value, type, pattern_result, k == 0, false);
  1476. if (err != OK) {
  1477. return err;
  1478. }
  1479. }
  1480. // Check if pattern did match.
  1481. gen->write_if(pattern_result);
  1482. // Remove the result from stack.
  1483. gen->pop_temporary();
  1484. // Parse the branch block.
  1485. err = _parse_block(codegen, branch->block, false); // Don't add locals again.
  1486. if (err) {
  1487. return err;
  1488. }
  1489. codegen.end_block(); // Get out of extra block.
  1490. }
  1491. // End all nested `if`s.
  1492. for (int j = 0; j < match->branches.size(); j++) {
  1493. gen->write_endif();
  1494. }
  1495. gen->end_match();
  1496. } break;
  1497. case GDScriptParser::Node::IF: {
  1498. const GDScriptParser::IfNode *if_n = static_cast<const GDScriptParser::IfNode *>(s);
  1499. GDScriptCodeGenerator::Address condition = _parse_expression(codegen, err, if_n->condition);
  1500. if (err) {
  1501. return err;
  1502. }
  1503. gen->write_if(condition);
  1504. if (condition.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1505. codegen.generator->pop_temporary();
  1506. }
  1507. err = _parse_block(codegen, if_n->true_block);
  1508. if (err) {
  1509. return err;
  1510. }
  1511. if (if_n->false_block) {
  1512. gen->write_else();
  1513. err = _parse_block(codegen, if_n->false_block);
  1514. if (err) {
  1515. return err;
  1516. }
  1517. }
  1518. gen->write_endif();
  1519. } break;
  1520. case GDScriptParser::Node::FOR: {
  1521. const GDScriptParser::ForNode *for_n = static_cast<const GDScriptParser::ForNode *>(s);
  1522. codegen.start_block();
  1523. GDScriptCodeGenerator::Address iterator = codegen.add_local(for_n->variable->name, _gdtype_from_datatype(for_n->variable->get_datatype(), codegen.script));
  1524. gen->start_for(iterator.type, _gdtype_from_datatype(for_n->list->get_datatype(), codegen.script));
  1525. GDScriptCodeGenerator::Address list = _parse_expression(codegen, err, for_n->list);
  1526. if (err) {
  1527. return err;
  1528. }
  1529. gen->write_for_assignment(iterator, list);
  1530. if (list.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1531. codegen.generator->pop_temporary();
  1532. }
  1533. gen->write_for();
  1534. err = _parse_block(codegen, for_n->loop);
  1535. if (err) {
  1536. return err;
  1537. }
  1538. gen->write_endfor();
  1539. codegen.end_block();
  1540. } break;
  1541. case GDScriptParser::Node::WHILE: {
  1542. const GDScriptParser::WhileNode *while_n = static_cast<const GDScriptParser::WhileNode *>(s);
  1543. gen->start_while_condition();
  1544. GDScriptCodeGenerator::Address condition = _parse_expression(codegen, err, while_n->condition);
  1545. if (err) {
  1546. return err;
  1547. }
  1548. gen->write_while(condition);
  1549. if (condition.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1550. codegen.generator->pop_temporary();
  1551. }
  1552. err = _parse_block(codegen, while_n->loop);
  1553. if (err) {
  1554. return err;
  1555. }
  1556. gen->write_endwhile();
  1557. } break;
  1558. case GDScriptParser::Node::BREAK: {
  1559. gen->write_break();
  1560. } break;
  1561. case GDScriptParser::Node::CONTINUE: {
  1562. const GDScriptParser::ContinueNode *cont = static_cast<const GDScriptParser::ContinueNode *>(s);
  1563. if (cont->is_for_match) {
  1564. gen->write_continue_match();
  1565. } else {
  1566. gen->write_continue();
  1567. }
  1568. } break;
  1569. case GDScriptParser::Node::RETURN: {
  1570. const GDScriptParser::ReturnNode *return_n = static_cast<const GDScriptParser::ReturnNode *>(s);
  1571. GDScriptCodeGenerator::Address return_value;
  1572. if (return_n->return_value != nullptr) {
  1573. return_value = _parse_expression(codegen, err, return_n->return_value);
  1574. if (err) {
  1575. return err;
  1576. }
  1577. }
  1578. gen->write_return(return_value);
  1579. if (return_value.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1580. codegen.generator->pop_temporary();
  1581. }
  1582. } break;
  1583. case GDScriptParser::Node::ASSERT: {
  1584. #ifdef DEBUG_ENABLED
  1585. const GDScriptParser::AssertNode *as = static_cast<const GDScriptParser::AssertNode *>(s);
  1586. GDScriptCodeGenerator::Address condition = _parse_expression(codegen, err, as->condition);
  1587. if (err) {
  1588. return err;
  1589. }
  1590. GDScriptCodeGenerator::Address message;
  1591. if (as->message) {
  1592. message = _parse_expression(codegen, err, as->message);
  1593. if (err) {
  1594. return err;
  1595. }
  1596. }
  1597. gen->write_assert(condition, message);
  1598. if (condition.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1599. codegen.generator->pop_temporary();
  1600. }
  1601. if (message.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1602. codegen.generator->pop_temporary();
  1603. }
  1604. #endif
  1605. } break;
  1606. case GDScriptParser::Node::BREAKPOINT: {
  1607. #ifdef DEBUG_ENABLED
  1608. gen->write_breakpoint();
  1609. #endif
  1610. } break;
  1611. case GDScriptParser::Node::VARIABLE: {
  1612. const GDScriptParser::VariableNode *lv = static_cast<const GDScriptParser::VariableNode *>(s);
  1613. // Should be already in stack when the block began.
  1614. GDScriptCodeGenerator::Address local = codegen.locals[lv->identifier->name];
  1615. GDScriptDataType local_type = _gdtype_from_datatype(lv->get_datatype(), codegen.script);
  1616. if (lv->initializer != nullptr) {
  1617. // For typed arrays we need to make sure this is already initialized correctly so typed assignment work.
  1618. if (local_type.has_type && local_type.builtin_type == Variant::ARRAY) {
  1619. if (local_type.has_container_element_type()) {
  1620. codegen.generator->write_construct_typed_array(local, local_type.get_container_element_type(), Vector<GDScriptCodeGenerator::Address>());
  1621. } else {
  1622. codegen.generator->write_construct_array(local, Vector<GDScriptCodeGenerator::Address>());
  1623. }
  1624. }
  1625. GDScriptCodeGenerator::Address src_address = _parse_expression(codegen, err, lv->initializer);
  1626. if (err) {
  1627. return err;
  1628. }
  1629. if (lv->use_conversion_assign) {
  1630. gen->write_assign_with_conversion(local, src_address);
  1631. } else {
  1632. gen->write_assign(local, src_address);
  1633. }
  1634. if (src_address.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1635. codegen.generator->pop_temporary();
  1636. }
  1637. } else if (local_type.has_type) {
  1638. // Initialize with default for type.
  1639. if (local_type.has_container_element_type()) {
  1640. codegen.generator->write_construct_typed_array(local, local_type.get_container_element_type(), Vector<GDScriptCodeGenerator::Address>());
  1641. } else if (local_type.kind == GDScriptDataType::BUILTIN) {
  1642. codegen.generator->write_construct(local, local_type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
  1643. }
  1644. // The `else` branch is for objects, in such case we leave it as `null`.
  1645. }
  1646. } break;
  1647. case GDScriptParser::Node::CONSTANT: {
  1648. // Local constants.
  1649. const GDScriptParser::ConstantNode *lc = static_cast<const GDScriptParser::ConstantNode *>(s);
  1650. if (!lc->initializer->is_constant) {
  1651. _set_error("Local constant must have a constant value as initializer.", lc->initializer);
  1652. return ERR_PARSE_ERROR;
  1653. }
  1654. codegen.add_local_constant(lc->identifier->name, lc->initializer->reduced_value);
  1655. } break;
  1656. case GDScriptParser::Node::PASS:
  1657. // Nothing to do.
  1658. break;
  1659. default: {
  1660. // Expression.
  1661. if (s->is_expression()) {
  1662. GDScriptCodeGenerator::Address expr = _parse_expression(codegen, err, static_cast<const GDScriptParser::ExpressionNode *>(s), true);
  1663. if (err) {
  1664. return err;
  1665. }
  1666. if (expr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1667. codegen.generator->pop_temporary();
  1668. }
  1669. } else {
  1670. ERR_FAIL_V_MSG(ERR_INVALID_DATA, "Bug in bytecode compiler, unexpected node in parse tree while parsing statement."); // Unreachable code.
  1671. }
  1672. } break;
  1673. }
  1674. }
  1675. codegen.end_block();
  1676. return OK;
  1677. }
  1678. 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) {
  1679. r_error = OK;
  1680. CodeGen codegen;
  1681. codegen.generator = memnew(GDScriptByteCodeGenerator);
  1682. codegen.class_node = p_class;
  1683. codegen.script = p_script;
  1684. codegen.function_node = p_func;
  1685. StringName func_name;
  1686. bool is_static = false;
  1687. Variant rpc_config;
  1688. GDScriptDataType return_type;
  1689. return_type.has_type = true;
  1690. return_type.kind = GDScriptDataType::BUILTIN;
  1691. return_type.builtin_type = Variant::NIL;
  1692. if (p_func) {
  1693. if (p_func->identifier) {
  1694. func_name = p_func->identifier->name;
  1695. } else {
  1696. func_name = "<anonymous lambda>";
  1697. }
  1698. is_static = p_func->is_static;
  1699. rpc_config = p_func->rpc_config;
  1700. return_type = _gdtype_from_datatype(p_func->get_datatype(), p_script);
  1701. } else {
  1702. if (p_for_ready) {
  1703. func_name = "_ready";
  1704. } else {
  1705. func_name = "@implicit_new";
  1706. }
  1707. }
  1708. codegen.function_name = func_name;
  1709. codegen.generator->write_start(p_script, func_name, is_static, rpc_config, return_type);
  1710. int optional_parameters = 0;
  1711. if (p_func) {
  1712. for (int i = 0; i < p_func->parameters.size(); i++) {
  1713. const GDScriptParser::ParameterNode *parameter = p_func->parameters[i];
  1714. GDScriptDataType par_type = _gdtype_from_datatype(parameter->get_datatype(), p_script);
  1715. uint32_t par_addr = codegen.generator->add_parameter(parameter->identifier->name, parameter->default_value != nullptr, par_type);
  1716. codegen.parameters[parameter->identifier->name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::FUNCTION_PARAMETER, par_addr, par_type);
  1717. if (p_func->parameters[i]->default_value != nullptr) {
  1718. optional_parameters++;
  1719. }
  1720. }
  1721. }
  1722. // Parse initializer if applies.
  1723. bool is_implicit_initializer = !p_for_ready && !p_func && !p_for_lambda;
  1724. bool is_initializer = p_func && !p_for_lambda && p_func->identifier->name == GDScriptLanguage::get_singleton()->strings._init;
  1725. bool is_implicit_ready = !p_func && p_for_ready;
  1726. if (!p_for_lambda && (is_implicit_initializer || is_implicit_ready)) {
  1727. // Initialize class fields.
  1728. for (int i = 0; i < p_class->members.size(); i++) {
  1729. if (p_class->members[i].type != GDScriptParser::ClassNode::Member::VARIABLE) {
  1730. continue;
  1731. }
  1732. const GDScriptParser::VariableNode *field = p_class->members[i].variable;
  1733. if (field->onready != is_implicit_ready) {
  1734. // Only initialize in @implicit_ready.
  1735. continue;
  1736. }
  1737. GDScriptDataType field_type = _gdtype_from_datatype(field->get_datatype(), codegen.script);
  1738. GDScriptCodeGenerator::Address dst_address(GDScriptCodeGenerator::Address::MEMBER, codegen.script->member_indices[field->identifier->name].index, field_type);
  1739. if (field->initializer) {
  1740. // Emit proper line change.
  1741. codegen.generator->write_newline(field->initializer->start_line);
  1742. // For typed arrays we need to make sure this is already initialized correctly so typed assignment work.
  1743. if (field_type.has_type && field_type.builtin_type == Variant::ARRAY) {
  1744. if (field_type.has_container_element_type()) {
  1745. codegen.generator->write_construct_typed_array(dst_address, field_type.get_container_element_type(), Vector<GDScriptCodeGenerator::Address>());
  1746. } else {
  1747. codegen.generator->write_construct_array(dst_address, Vector<GDScriptCodeGenerator::Address>());
  1748. }
  1749. }
  1750. GDScriptCodeGenerator::Address src_address = _parse_expression(codegen, r_error, field->initializer, false, true);
  1751. if (r_error) {
  1752. memdelete(codegen.generator);
  1753. return nullptr;
  1754. }
  1755. if (field->use_conversion_assign) {
  1756. codegen.generator->write_assign_with_conversion(dst_address, src_address);
  1757. } else {
  1758. codegen.generator->write_assign(dst_address, src_address);
  1759. }
  1760. if (src_address.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1761. codegen.generator->pop_temporary();
  1762. }
  1763. } else if (field_type.has_type) {
  1764. codegen.generator->write_newline(field->start_line);
  1765. // Initialize with default for type.
  1766. if (field_type.has_container_element_type()) {
  1767. codegen.generator->write_construct_typed_array(dst_address, field_type.get_container_element_type(), Vector<GDScriptCodeGenerator::Address>());
  1768. } else if (field_type.kind == GDScriptDataType::BUILTIN) {
  1769. codegen.generator->write_construct(dst_address, field_type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
  1770. }
  1771. // The `else` branch is for objects, in such case we leave it as `null`.
  1772. }
  1773. }
  1774. }
  1775. // Parse default argument code if applies.
  1776. if (p_func) {
  1777. if (optional_parameters > 0) {
  1778. codegen.generator->start_parameters();
  1779. for (int i = p_func->parameters.size() - optional_parameters; i < p_func->parameters.size(); i++) {
  1780. const GDScriptParser::ParameterNode *parameter = p_func->parameters[i];
  1781. GDScriptCodeGenerator::Address src_addr = _parse_expression(codegen, r_error, parameter->default_value);
  1782. if (r_error) {
  1783. memdelete(codegen.generator);
  1784. return nullptr;
  1785. }
  1786. GDScriptCodeGenerator::Address dst_addr = codegen.parameters[parameter->identifier->name];
  1787. codegen.generator->write_assign_default_parameter(dst_addr, src_addr);
  1788. if (src_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
  1789. codegen.generator->pop_temporary();
  1790. }
  1791. }
  1792. codegen.generator->end_parameters();
  1793. }
  1794. r_error = _parse_block(codegen, p_func->body);
  1795. if (r_error) {
  1796. memdelete(codegen.generator);
  1797. return nullptr;
  1798. }
  1799. }
  1800. #ifdef DEBUG_ENABLED
  1801. if (EngineDebugger::is_active()) {
  1802. String signature;
  1803. // Path.
  1804. if (!p_script->get_path().is_empty()) {
  1805. signature += p_script->get_path();
  1806. }
  1807. // Location.
  1808. if (p_func) {
  1809. signature += "::" + itos(p_func->body->start_line);
  1810. } else {
  1811. signature += "::0";
  1812. }
  1813. // Function and class.
  1814. if (p_class->identifier) {
  1815. signature += "::" + String(p_class->identifier->name) + "." + String(func_name);
  1816. } else {
  1817. signature += "::" + String(func_name);
  1818. }
  1819. if (p_for_lambda) {
  1820. signature += "(lambda)";
  1821. }
  1822. codegen.generator->set_signature(signature);
  1823. }
  1824. #endif
  1825. if (p_func) {
  1826. codegen.generator->set_initial_line(p_func->start_line);
  1827. #ifdef TOOLS_ENABLED
  1828. if (!p_for_lambda) {
  1829. p_script->member_lines[func_name] = p_func->start_line;
  1830. p_script->doc_functions[func_name] = p_func->doc_description;
  1831. }
  1832. #endif
  1833. } else {
  1834. codegen.generator->set_initial_line(0);
  1835. }
  1836. GDScriptFunction *gd_function = codegen.generator->write_end();
  1837. if (is_initializer) {
  1838. p_script->initializer = gd_function;
  1839. } else if (is_implicit_initializer) {
  1840. p_script->implicit_initializer = gd_function;
  1841. } else if (is_implicit_ready) {
  1842. p_script->implicit_ready = gd_function;
  1843. }
  1844. if (p_func) {
  1845. // if no return statement -> return type is void not unresolved Variant
  1846. if (p_func->body->has_return) {
  1847. gd_function->return_type = _gdtype_from_datatype(p_func->get_datatype(), p_script);
  1848. } else {
  1849. gd_function->return_type = GDScriptDataType();
  1850. gd_function->return_type.has_type = true;
  1851. gd_function->return_type.kind = GDScriptDataType::BUILTIN;
  1852. gd_function->return_type.builtin_type = Variant::NIL;
  1853. }
  1854. #ifdef TOOLS_ENABLED
  1855. gd_function->default_arg_values = p_func->default_arg_values;
  1856. #endif
  1857. }
  1858. if (!is_implicit_initializer && !is_implicit_ready && !p_for_lambda) {
  1859. p_script->member_functions[func_name] = gd_function;
  1860. }
  1861. memdelete(codegen.generator);
  1862. return gd_function;
  1863. }
  1864. Error GDScriptCompiler::_parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter) {
  1865. Error err = OK;
  1866. GDScriptParser::FunctionNode *function;
  1867. if (p_is_setter) {
  1868. function = p_variable->setter;
  1869. } else {
  1870. function = p_variable->getter;
  1871. }
  1872. _parse_function(err, p_script, p_class, function);
  1873. return err;
  1874. }
  1875. Error GDScriptCompiler::_populate_class_members(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  1876. if (parsed_classes.has(p_script)) {
  1877. return OK;
  1878. }
  1879. if (parsing_classes.has(p_script)) {
  1880. String class_name = p_class->identifier ? String(p_class->identifier->name) : p_class->fqcn;
  1881. _set_error(vformat(R"(Cyclic class reference for "%s".)", class_name), p_class);
  1882. return ERR_PARSE_ERROR;
  1883. }
  1884. parsing_classes.insert(p_script);
  1885. p_script->clearing = true;
  1886. #ifdef TOOLS_ENABLED
  1887. p_script->doc_functions.clear();
  1888. p_script->doc_variables.clear();
  1889. p_script->doc_constants.clear();
  1890. p_script->doc_enums.clear();
  1891. p_script->doc_signals.clear();
  1892. p_script->doc_tutorials.clear();
  1893. p_script->doc_brief_description = p_class->doc_brief_description;
  1894. p_script->doc_description = p_class->doc_description;
  1895. for (int i = 0; i < p_class->doc_tutorials.size(); i++) {
  1896. DocData::TutorialDoc td;
  1897. td.title = p_class->doc_tutorials[i].first;
  1898. td.link = p_class->doc_tutorials[i].second;
  1899. p_script->doc_tutorials.append(td);
  1900. }
  1901. #endif
  1902. p_script->native = Ref<GDScriptNativeClass>();
  1903. p_script->base = Ref<GDScript>();
  1904. p_script->_base = nullptr;
  1905. p_script->members.clear();
  1906. // This makes possible to clear script constants and member_functions without heap-use-after-free errors.
  1907. HashMap<StringName, Variant> constants;
  1908. for (const KeyValue<StringName, Variant> &E : p_script->constants) {
  1909. constants.insert(E.key, E.value);
  1910. }
  1911. p_script->constants.clear();
  1912. constants.clear();
  1913. HashMap<StringName, GDScriptFunction *> member_functions;
  1914. for (const KeyValue<StringName, GDScriptFunction *> &E : p_script->member_functions) {
  1915. member_functions.insert(E.key, E.value);
  1916. }
  1917. p_script->member_functions.clear();
  1918. for (const KeyValue<StringName, GDScriptFunction *> &E : member_functions) {
  1919. memdelete(E.value);
  1920. }
  1921. member_functions.clear();
  1922. if (p_script->implicit_initializer) {
  1923. memdelete(p_script->implicit_initializer);
  1924. }
  1925. if (p_script->implicit_ready) {
  1926. memdelete(p_script->implicit_ready);
  1927. }
  1928. p_script->member_functions.clear();
  1929. p_script->member_indices.clear();
  1930. p_script->member_info.clear();
  1931. p_script->_signals.clear();
  1932. p_script->initializer = nullptr;
  1933. p_script->implicit_initializer = nullptr;
  1934. p_script->implicit_ready = nullptr;
  1935. p_script->clearing = false;
  1936. p_script->tool = parser->is_tool();
  1937. if (!p_script->name.is_empty()) {
  1938. if (ClassDB::class_exists(p_script->name) && ClassDB::is_class_exposed(p_script->name)) {
  1939. _set_error("The class '" + p_script->name + "' shadows a native class", p_class);
  1940. return ERR_ALREADY_EXISTS;
  1941. }
  1942. }
  1943. GDScriptDataType base_type = _gdtype_from_datatype(p_class->base_type, p_script);
  1944. // Inheritance
  1945. switch (base_type.kind) {
  1946. case GDScriptDataType::NATIVE: {
  1947. int native_idx = GDScriptLanguage::get_singleton()->get_global_map()[base_type.native_type];
  1948. p_script->native = GDScriptLanguage::get_singleton()->get_global_array()[native_idx];
  1949. ERR_FAIL_COND_V(p_script->native.is_null(), ERR_BUG);
  1950. } break;
  1951. case GDScriptDataType::GDSCRIPT: {
  1952. Ref<GDScript> base = Ref<GDScript>(base_type.script_type);
  1953. if (base.is_null()) {
  1954. return ERR_COMPILATION_FAILED;
  1955. }
  1956. if (base.ptr() == main_script || main_script->is_subclass(base.ptr())) {
  1957. Error err = _populate_class_members(base.ptr(), p_class->base_type.class_type, p_keep_state);
  1958. if (err) {
  1959. return err;
  1960. }
  1961. } else if (!base->is_valid()) {
  1962. Error err = OK;
  1963. Ref<GDScript> base_root = GDScriptCache::get_full_script(base->path, err, p_script->path);
  1964. if (err) {
  1965. _set_error(vformat(R"(Could not compile base class "%s" from "%s": %s)", base->fully_qualified_name, base->path, error_names[err]), nullptr);
  1966. return err;
  1967. }
  1968. if (base_root.is_valid()) {
  1969. base = Ref<GDScript>(base_root->find_class(base->fully_qualified_name));
  1970. }
  1971. if (base.is_null()) {
  1972. _set_error(vformat(R"(Could not find class "%s" in "%s".)", base->fully_qualified_name, base->path), nullptr);
  1973. return ERR_COMPILATION_FAILED;
  1974. }
  1975. ERR_FAIL_COND_V(!base->is_valid(), ERR_BUG);
  1976. }
  1977. p_script->base = base;
  1978. p_script->_base = base.ptr();
  1979. p_script->member_indices = base->member_indices;
  1980. p_script->native = base->native;
  1981. } break;
  1982. default: {
  1983. _set_error("Parser bug: invalid inheritance.", nullptr);
  1984. return ERR_BUG;
  1985. } break;
  1986. }
  1987. for (int i = 0; i < p_class->members.size(); i++) {
  1988. const GDScriptParser::ClassNode::Member &member = p_class->members[i];
  1989. switch (member.type) {
  1990. case GDScriptParser::ClassNode::Member::VARIABLE: {
  1991. const GDScriptParser::VariableNode *variable = member.variable;
  1992. StringName name = variable->identifier->name;
  1993. GDScript::MemberInfo minfo;
  1994. minfo.index = p_script->member_indices.size();
  1995. switch (variable->property) {
  1996. case GDScriptParser::VariableNode::PROP_NONE:
  1997. break; // Nothing to do.
  1998. case GDScriptParser::VariableNode::PROP_SETGET:
  1999. if (variable->setter_pointer != nullptr) {
  2000. minfo.setter = variable->setter_pointer->name;
  2001. }
  2002. if (variable->getter_pointer != nullptr) {
  2003. minfo.getter = variable->getter_pointer->name;
  2004. }
  2005. break;
  2006. case GDScriptParser::VariableNode::PROP_INLINE:
  2007. if (variable->setter != nullptr) {
  2008. minfo.setter = "@" + variable->identifier->name + "_setter";
  2009. }
  2010. if (variable->getter != nullptr) {
  2011. minfo.getter = "@" + variable->identifier->name + "_getter";
  2012. }
  2013. break;
  2014. }
  2015. minfo.data_type = _gdtype_from_datatype(variable->get_datatype(), p_script);
  2016. PropertyInfo prop_info = minfo.data_type;
  2017. prop_info.name = name;
  2018. PropertyInfo export_info = variable->export_info;
  2019. if (variable->exported) {
  2020. if (!minfo.data_type.has_type) {
  2021. prop_info.type = export_info.type;
  2022. prop_info.class_name = export_info.class_name;
  2023. }
  2024. prop_info.hint = export_info.hint;
  2025. prop_info.hint_string = export_info.hint_string;
  2026. prop_info.usage = export_info.usage | PROPERTY_USAGE_SCRIPT_VARIABLE;
  2027. } else {
  2028. prop_info.usage = PROPERTY_USAGE_SCRIPT_VARIABLE;
  2029. }
  2030. #ifdef TOOLS_ENABLED
  2031. p_script->doc_variables[name] = variable->doc_description;
  2032. #endif
  2033. p_script->member_info[name] = prop_info;
  2034. p_script->member_indices[name] = minfo;
  2035. p_script->members.insert(name);
  2036. #ifdef TOOLS_ENABLED
  2037. if (variable->initializer != nullptr && variable->initializer->is_constant) {
  2038. p_script->member_default_values[name] = variable->initializer->reduced_value;
  2039. } else {
  2040. p_script->member_default_values.erase(name);
  2041. }
  2042. p_script->member_lines[name] = variable->start_line;
  2043. #endif
  2044. } break;
  2045. case GDScriptParser::ClassNode::Member::CONSTANT: {
  2046. const GDScriptParser::ConstantNode *constant = member.constant;
  2047. StringName name = constant->identifier->name;
  2048. p_script->constants.insert(name, constant->initializer->reduced_value);
  2049. #ifdef TOOLS_ENABLED
  2050. p_script->member_lines[name] = constant->start_line;
  2051. if (!constant->doc_description.is_empty()) {
  2052. p_script->doc_constants[name] = constant->doc_description;
  2053. }
  2054. #endif
  2055. } break;
  2056. case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
  2057. const GDScriptParser::EnumNode::Value &enum_value = member.enum_value;
  2058. StringName name = enum_value.identifier->name;
  2059. p_script->constants.insert(name, enum_value.value);
  2060. #ifdef TOOLS_ENABLED
  2061. p_script->member_lines[name] = enum_value.identifier->start_line;
  2062. if (!p_script->doc_enums.has("@unnamed_enums")) {
  2063. p_script->doc_enums["@unnamed_enums"] = DocData::EnumDoc();
  2064. p_script->doc_enums["@unnamed_enums"].name = "@unnamed_enums";
  2065. }
  2066. DocData::ConstantDoc const_doc;
  2067. const_doc.name = enum_value.identifier->name;
  2068. const_doc.value = Variant(enum_value.value).operator String(); // TODO-DOC: enum value currently is int.
  2069. const_doc.description = enum_value.doc_description;
  2070. p_script->doc_enums["@unnamed_enums"].values.push_back(const_doc);
  2071. #endif
  2072. } break;
  2073. case GDScriptParser::ClassNode::Member::SIGNAL: {
  2074. const GDScriptParser::SignalNode *signal = member.signal;
  2075. StringName name = signal->identifier->name;
  2076. Vector<StringName> parameters_names;
  2077. parameters_names.resize(signal->parameters.size());
  2078. for (int j = 0; j < signal->parameters.size(); j++) {
  2079. parameters_names.write[j] = signal->parameters[j]->identifier->name;
  2080. }
  2081. p_script->_signals[name] = parameters_names;
  2082. #ifdef TOOLS_ENABLED
  2083. if (!signal->doc_description.is_empty()) {
  2084. p_script->doc_signals[name] = signal->doc_description;
  2085. }
  2086. #endif
  2087. } break;
  2088. case GDScriptParser::ClassNode::Member::ENUM: {
  2089. const GDScriptParser::EnumNode *enum_n = member.m_enum;
  2090. // TODO: Make enums not be just a dictionary?
  2091. Dictionary new_enum;
  2092. for (int j = 0; j < enum_n->values.size(); j++) {
  2093. // Needs to be string because Variant::get will convert to String.
  2094. new_enum[String(enum_n->values[j].identifier->name)] = enum_n->values[j].value;
  2095. }
  2096. p_script->constants.insert(enum_n->identifier->name, new_enum);
  2097. #ifdef TOOLS_ENABLED
  2098. p_script->member_lines[enum_n->identifier->name] = enum_n->start_line;
  2099. p_script->doc_enums[enum_n->identifier->name] = DocData::EnumDoc();
  2100. p_script->doc_enums[enum_n->identifier->name].name = enum_n->identifier->name;
  2101. p_script->doc_enums[enum_n->identifier->name].description = enum_n->doc_description;
  2102. for (int j = 0; j < enum_n->values.size(); j++) {
  2103. DocData::ConstantDoc const_doc;
  2104. const_doc.name = enum_n->values[j].identifier->name;
  2105. const_doc.value = Variant(enum_n->values[j].value).operator String();
  2106. const_doc.description = enum_n->values[j].doc_description;
  2107. p_script->doc_enums[enum_n->identifier->name].values.push_back(const_doc);
  2108. }
  2109. #endif
  2110. } break;
  2111. case GDScriptParser::ClassNode::Member::GROUP: {
  2112. const GDScriptParser::AnnotationNode *annotation = member.annotation;
  2113. StringName name = annotation->export_info.name;
  2114. // This is not a normal member, but we need this to keep indices in order.
  2115. GDScript::MemberInfo minfo;
  2116. minfo.index = p_script->member_indices.size();
  2117. PropertyInfo prop_info;
  2118. prop_info.name = name;
  2119. prop_info.usage = annotation->export_info.usage;
  2120. prop_info.hint_string = annotation->export_info.hint_string;
  2121. p_script->member_info[name] = prop_info;
  2122. p_script->member_indices[name] = minfo;
  2123. p_script->members.insert(name);
  2124. } break;
  2125. default:
  2126. break; // Nothing to do here.
  2127. }
  2128. }
  2129. parsed_classes.insert(p_script);
  2130. parsing_classes.erase(p_script);
  2131. // Populate sub-classes.
  2132. for (int i = 0; i < p_class->members.size(); i++) {
  2133. const GDScriptParser::ClassNode::Member &member = p_class->members[i];
  2134. if (member.type != member.CLASS) {
  2135. continue;
  2136. }
  2137. const GDScriptParser::ClassNode *inner_class = member.m_class;
  2138. StringName name = inner_class->identifier->name;
  2139. Ref<GDScript> &subclass = p_script->subclasses[name];
  2140. GDScript *subclass_ptr = subclass.ptr();
  2141. // Subclass might still be parsing, just skip it
  2142. if (!parsing_classes.has(subclass_ptr)) {
  2143. Error err = _populate_class_members(subclass_ptr, inner_class, p_keep_state);
  2144. if (err) {
  2145. return err;
  2146. }
  2147. }
  2148. #ifdef TOOLS_ENABLED
  2149. p_script->member_lines[name] = inner_class->start_line;
  2150. #endif
  2151. p_script->constants.insert(name, subclass); //once parsed, goes to the list of constants
  2152. }
  2153. return OK;
  2154. }
  2155. Error GDScriptCompiler::_compile_class(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  2156. // Compile member functions, getters, and setters.
  2157. for (int i = 0; i < p_class->members.size(); i++) {
  2158. const GDScriptParser::ClassNode::Member &member = p_class->members[i];
  2159. if (member.type == member.FUNCTION) {
  2160. const GDScriptParser::FunctionNode *function = member.function;
  2161. Error err = OK;
  2162. _parse_function(err, p_script, p_class, function);
  2163. if (err) {
  2164. return err;
  2165. }
  2166. } else if (member.type == member.VARIABLE) {
  2167. const GDScriptParser::VariableNode *variable = member.variable;
  2168. if (variable->property == GDScriptParser::VariableNode::PROP_INLINE) {
  2169. if (variable->setter != nullptr) {
  2170. Error err = _parse_setter_getter(p_script, p_class, variable, true);
  2171. if (err) {
  2172. return err;
  2173. }
  2174. }
  2175. if (variable->getter != nullptr) {
  2176. Error err = _parse_setter_getter(p_script, p_class, variable, false);
  2177. if (err) {
  2178. return err;
  2179. }
  2180. }
  2181. }
  2182. }
  2183. }
  2184. {
  2185. // Create an implicit constructor in any case.
  2186. Error err = OK;
  2187. _parse_function(err, p_script, p_class, nullptr);
  2188. if (err) {
  2189. return err;
  2190. }
  2191. }
  2192. if (p_class->onready_used) {
  2193. // Create an implicit_ready constructor.
  2194. Error err = OK;
  2195. _parse_function(err, p_script, p_class, nullptr, true);
  2196. if (err) {
  2197. return err;
  2198. }
  2199. }
  2200. #ifdef DEBUG_ENABLED
  2201. //validate instances if keeping state
  2202. if (p_keep_state) {
  2203. for (RBSet<Object *>::Element *E = p_script->instances.front(); E;) {
  2204. RBSet<Object *>::Element *N = E->next();
  2205. ScriptInstance *si = E->get()->get_script_instance();
  2206. if (si->is_placeholder()) {
  2207. #ifdef TOOLS_ENABLED
  2208. PlaceHolderScriptInstance *psi = static_cast<PlaceHolderScriptInstance *>(si);
  2209. if (p_script->is_tool()) {
  2210. //re-create as an instance
  2211. p_script->placeholders.erase(psi); //remove placeholder
  2212. GDScriptInstance *instance = memnew(GDScriptInstance);
  2213. instance->base_ref_counted = Object::cast_to<RefCounted>(E->get());
  2214. instance->members.resize(p_script->member_indices.size());
  2215. instance->script = Ref<GDScript>(p_script);
  2216. instance->owner = E->get();
  2217. //needed for hot reloading
  2218. for (const KeyValue<StringName, GDScript::MemberInfo> &F : p_script->member_indices) {
  2219. instance->member_indices_cache[F.key] = F.value.index;
  2220. }
  2221. instance->owner->set_script_instance(instance);
  2222. /* STEP 2, INITIALIZE AND CONSTRUCT */
  2223. Callable::CallError ce;
  2224. p_script->initializer->call(instance, nullptr, 0, ce);
  2225. if (ce.error != Callable::CallError::CALL_OK) {
  2226. //well, tough luck, not gonna do anything here
  2227. }
  2228. }
  2229. #endif
  2230. } else {
  2231. GDScriptInstance *gi = static_cast<GDScriptInstance *>(si);
  2232. gi->reload_members();
  2233. }
  2234. E = N;
  2235. }
  2236. }
  2237. #endif
  2238. for (int i = 0; i < p_class->members.size(); i++) {
  2239. if (p_class->members[i].type != GDScriptParser::ClassNode::Member::CLASS) {
  2240. continue;
  2241. }
  2242. const GDScriptParser::ClassNode *inner_class = p_class->members[i].m_class;
  2243. StringName name = inner_class->identifier->name;
  2244. GDScript *subclass = p_script->subclasses[name].ptr();
  2245. Error err = _compile_class(subclass, inner_class, p_keep_state);
  2246. if (err) {
  2247. return err;
  2248. }
  2249. }
  2250. #ifdef TOOLS_ENABLED
  2251. p_script->_update_doc();
  2252. #endif
  2253. p_script->_init_rpc_methods_properties();
  2254. p_script->valid = true;
  2255. return OK;
  2256. }
  2257. void GDScriptCompiler::make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  2258. p_script->fully_qualified_name = p_class->fqcn;
  2259. p_script->name = p_class->identifier ? p_class->identifier->name : "";
  2260. HashMap<StringName, Ref<GDScript>> old_subclasses;
  2261. if (p_keep_state) {
  2262. old_subclasses = p_script->subclasses;
  2263. }
  2264. p_script->subclasses.clear();
  2265. for (int i = 0; i < p_class->members.size(); i++) {
  2266. if (p_class->members[i].type != GDScriptParser::ClassNode::Member::CLASS) {
  2267. continue;
  2268. }
  2269. const GDScriptParser::ClassNode *inner_class = p_class->members[i].m_class;
  2270. StringName name = inner_class->identifier->name;
  2271. Ref<GDScript> subclass;
  2272. if (old_subclasses.has(name)) {
  2273. subclass = old_subclasses[name];
  2274. } else {
  2275. subclass = GDScriptLanguage::get_singleton()->get_orphan_subclass(inner_class->fqcn);
  2276. }
  2277. if (subclass.is_null()) {
  2278. subclass.instantiate();
  2279. }
  2280. subclass->_owner = p_script;
  2281. subclass->path = p_script->path;
  2282. p_script->subclasses.insert(name, subclass);
  2283. make_scripts(subclass.ptr(), inner_class, p_keep_state);
  2284. }
  2285. }
  2286. Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state) {
  2287. err_line = -1;
  2288. err_column = -1;
  2289. error = "";
  2290. parser = p_parser;
  2291. main_script = p_script;
  2292. const GDScriptParser::ClassNode *root = parser->get_tree();
  2293. source = p_script->get_path();
  2294. // Create scripts for subclasses beforehand so they can be referenced
  2295. make_scripts(p_script, root, p_keep_state);
  2296. main_script->_owner = nullptr;
  2297. Error err = _populate_class_members(main_script, parser->get_tree(), p_keep_state);
  2298. if (err) {
  2299. return err;
  2300. }
  2301. err = _compile_class(main_script, root, p_keep_state);
  2302. if (err) {
  2303. return err;
  2304. }
  2305. return GDScriptCache::finish_compiling(main_script->get_path());
  2306. }
  2307. String GDScriptCompiler::get_error() const {
  2308. return error;
  2309. }
  2310. int GDScriptCompiler::get_error_line() const {
  2311. return err_line;
  2312. }
  2313. int GDScriptCompiler::get_error_column() const {
  2314. return err_column;
  2315. }
  2316. GDScriptCompiler::GDScriptCompiler() {
  2317. }