gdscript_compiler.cpp 83 KB

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