gdscript_compiler.cpp 98 KB

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