gdscript_compiler.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. /*************************************************************************/
  2. /* gdscript_compiler.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_compiler.h"
  31. #include "gdscript.h"
  32. bool GDScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringName &p_name) {
  33. if (codegen.function_node && codegen.function_node->_static) {
  34. return false;
  35. }
  36. if (codegen.stack_identifiers.has(p_name)) {
  37. return false; //shadowed
  38. }
  39. return _is_class_member_property(codegen.script, p_name);
  40. }
  41. bool GDScriptCompiler::_is_class_member_property(GDScript *owner, const StringName &p_name) {
  42. GDScript *scr = owner;
  43. GDScriptNativeClass *nc = nullptr;
  44. while (scr) {
  45. if (scr->native.is_valid()) {
  46. nc = scr->native.ptr();
  47. }
  48. scr = scr->_base;
  49. }
  50. ERR_FAIL_COND_V(!nc, false);
  51. return ClassDB::has_property(nc->get_name(), p_name);
  52. }
  53. void GDScriptCompiler::_set_error(const String &p_error, const GDScriptParser::Node *p_node) {
  54. if (error != "") {
  55. return;
  56. }
  57. error = p_error;
  58. if (p_node) {
  59. err_line = p_node->line;
  60. err_column = p_node->column;
  61. } else {
  62. err_line = 0;
  63. err_column = 0;
  64. }
  65. }
  66. bool GDScriptCompiler::_create_unary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level) {
  67. ERR_FAIL_COND_V(on->arguments.size() != 1, false);
  68. int src_address_a = _parse_expression(codegen, on->arguments[0], p_stack_level);
  69. if (src_address_a < 0) {
  70. return false;
  71. }
  72. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); // perform operator
  73. codegen.opcodes.push_back(op); //which operator
  74. codegen.opcodes.push_back(src_address_a); // argument 1
  75. codegen.opcodes.push_back(src_address_a); // argument 2 (repeated)
  76. //codegen.opcodes.push_back(GDScriptFunction::ADDR_TYPE_NIL); // argument 2 (unary only takes one parameter)
  77. return true;
  78. }
  79. bool GDScriptCompiler::_create_binary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer, int p_index_addr) {
  80. ERR_FAIL_COND_V(on->arguments.size() != 2, false);
  81. int src_address_a = _parse_expression(codegen, on->arguments[0], p_stack_level, false, p_initializer, p_index_addr);
  82. if (src_address_a < 0) {
  83. return false;
  84. }
  85. if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  86. p_stack_level++; //uses stack for return, increase stack
  87. }
  88. int src_address_b = _parse_expression(codegen, on->arguments[1], p_stack_level, false, p_initializer);
  89. if (src_address_b < 0) {
  90. return false;
  91. }
  92. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); // perform operator
  93. codegen.opcodes.push_back(op); //which operator
  94. codegen.opcodes.push_back(src_address_a); // argument 1
  95. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  96. return true;
  97. }
  98. GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::DataType &p_datatype) const {
  99. if (!p_datatype.has_type) {
  100. return GDScriptDataType();
  101. }
  102. GDScriptDataType result;
  103. result.has_type = true;
  104. switch (p_datatype.kind) {
  105. case GDScriptParser::DataType::BUILTIN: {
  106. result.kind = GDScriptDataType::BUILTIN;
  107. result.builtin_type = p_datatype.builtin_type;
  108. } break;
  109. case GDScriptParser::DataType::NATIVE: {
  110. result.kind = GDScriptDataType::NATIVE;
  111. result.native_type = p_datatype.native_type;
  112. } break;
  113. case GDScriptParser::DataType::SCRIPT: {
  114. result.kind = GDScriptDataType::SCRIPT;
  115. result.script_type = p_datatype.script_type;
  116. result.native_type = result.script_type->get_instance_base_type();
  117. } break;
  118. case GDScriptParser::DataType::GDSCRIPT: {
  119. result.kind = GDScriptDataType::GDSCRIPT;
  120. result.script_type = p_datatype.script_type;
  121. result.native_type = result.script_type->get_instance_base_type();
  122. } break;
  123. case GDScriptParser::DataType::CLASS: {
  124. // Locate class by constructing the path to it and following that path
  125. GDScriptParser::ClassNode *class_type = p_datatype.class_type;
  126. List<StringName> names;
  127. while (class_type->owner) {
  128. names.push_back(class_type->name);
  129. class_type = class_type->owner;
  130. }
  131. Ref<GDScript> script = Ref<GDScript>(main_script);
  132. while (names.back()) {
  133. if (!script->subclasses.has(names.back()->get())) {
  134. ERR_PRINT("Parser bug: Cannot locate datatype class.");
  135. result.has_type = false;
  136. return GDScriptDataType();
  137. }
  138. script = script->subclasses[names.back()->get()];
  139. names.pop_back();
  140. }
  141. result.kind = GDScriptDataType::GDSCRIPT;
  142. result.script_type = script;
  143. result.native_type = script->get_instance_base_type();
  144. } break;
  145. default: {
  146. ERR_PRINT("Parser bug: converting unresolved type.");
  147. return GDScriptDataType();
  148. }
  149. }
  150. return result;
  151. }
  152. int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level, int p_index_addr) {
  153. Variant::Operator var_op = Variant::OP_MAX;
  154. switch (p_expression->op) {
  155. case GDScriptParser::OperatorNode::OP_ASSIGN_ADD:
  156. var_op = Variant::OP_ADD;
  157. break;
  158. case GDScriptParser::OperatorNode::OP_ASSIGN_SUB:
  159. var_op = Variant::OP_SUBTRACT;
  160. break;
  161. case GDScriptParser::OperatorNode::OP_ASSIGN_MUL:
  162. var_op = Variant::OP_MULTIPLY;
  163. break;
  164. case GDScriptParser::OperatorNode::OP_ASSIGN_DIV:
  165. var_op = Variant::OP_DIVIDE;
  166. break;
  167. case GDScriptParser::OperatorNode::OP_ASSIGN_MOD:
  168. var_op = Variant::OP_MODULE;
  169. break;
  170. case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  171. var_op = Variant::OP_SHIFT_LEFT;
  172. break;
  173. case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  174. var_op = Variant::OP_SHIFT_RIGHT;
  175. break;
  176. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_AND:
  177. var_op = Variant::OP_BIT_AND;
  178. break;
  179. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_OR:
  180. var_op = Variant::OP_BIT_OR;
  181. break;
  182. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR:
  183. var_op = Variant::OP_BIT_XOR;
  184. break;
  185. case GDScriptParser::OperatorNode::OP_INIT_ASSIGN:
  186. case GDScriptParser::OperatorNode::OP_ASSIGN: {
  187. //none
  188. } break;
  189. default: {
  190. ERR_FAIL_V(-1);
  191. }
  192. }
  193. bool initializer = p_expression->op == GDScriptParser::OperatorNode::OP_INIT_ASSIGN;
  194. if (var_op == Variant::OP_MAX) {
  195. return _parse_expression(codegen, p_expression->arguments[1], p_stack_level, false, initializer);
  196. }
  197. if (!_create_binary_operator(codegen, p_expression, var_op, p_stack_level, initializer, p_index_addr)) {
  198. return -1;
  199. }
  200. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  201. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  202. codegen.alloc_stack(p_stack_level);
  203. return dst_addr;
  204. }
  205. int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root, bool p_initializer, int p_index_addr) {
  206. switch (p_expression->type) {
  207. //should parse variable declaration and adjust stack accordingly...
  208. case GDScriptParser::Node::TYPE_IDENTIFIER: {
  209. //return identifier
  210. //wait, identifier could be a local variable or something else... careful here, must reference properly
  211. //as stack may be more interesting to work with
  212. //This could be made much simpler by just indexing "self", but done this way (with custom self-addressing modes) increases performance a lot.
  213. const GDScriptParser::IdentifierNode *in = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  214. StringName identifier = in->name;
  215. // TRY STACK!
  216. if (!p_initializer && codegen.stack_identifiers.has(identifier)) {
  217. int pos = codegen.stack_identifiers[identifier];
  218. return pos | (GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS);
  219. }
  220. // TRY CLASS MEMBER
  221. if (_is_class_member_property(codegen, identifier)) {
  222. //get property
  223. codegen.opcodes.push_back(GDScriptFunction::OPCODE_GET_MEMBER); // perform operator
  224. codegen.opcodes.push_back(codegen.get_name_map_pos(identifier)); // argument 2 (unary only takes one parameter)
  225. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  226. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  227. codegen.alloc_stack(p_stack_level);
  228. return dst_addr;
  229. }
  230. //TRY MEMBERS!
  231. if (!codegen.function_node || !codegen.function_node->_static) {
  232. // TRY MEMBER VARIABLES!
  233. //static function
  234. if (codegen.script->member_indices.has(identifier)) {
  235. int idx = codegen.script->member_indices[identifier].index;
  236. return idx | (GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS); //argument (stack root)
  237. }
  238. }
  239. //TRY CLASS CONSTANTS
  240. GDScript *owner = codegen.script;
  241. while (owner) {
  242. GDScript *scr = owner;
  243. GDScriptNativeClass *nc = nullptr;
  244. while (scr) {
  245. if (scr->constants.has(identifier)) {
  246. //int idx=scr->constants[identifier];
  247. int idx = codegen.get_name_map_pos(identifier);
  248. return idx | (GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT << GDScriptFunction::ADDR_BITS); //argument (stack root)
  249. }
  250. if (scr->native.is_valid()) {
  251. nc = scr->native.ptr();
  252. }
  253. scr = scr->_base;
  254. }
  255. // CLASS C++ Integer Constant
  256. if (nc) {
  257. bool success = false;
  258. int constant = ClassDB::get_integer_constant(nc->get_name(), identifier, &success);
  259. if (success) {
  260. Variant key = constant;
  261. int idx;
  262. if (!codegen.constant_map.has(key)) {
  263. idx = codegen.constant_map.size();
  264. codegen.constant_map[key] = idx;
  265. } else {
  266. idx = codegen.constant_map[key];
  267. }
  268. return idx | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS); //make it a local constant (faster access)
  269. }
  270. }
  271. owner = owner->_owner;
  272. }
  273. if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
  274. int idx = GDScriptLanguage::get_singleton()->get_global_map()[identifier];
  275. return idx | (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root)
  276. }
  277. /* TRY GLOBAL CLASSES */
  278. if (ScriptServer::is_global_class(identifier)) {
  279. const GDScriptParser::ClassNode *class_node = codegen.class_node;
  280. while (class_node->owner) {
  281. class_node = class_node->owner;
  282. }
  283. if (class_node->name == identifier) {
  284. _set_error("Using own name in class file is not allowed (creates a cyclic reference)", p_expression);
  285. return -1;
  286. }
  287. RES res = ResourceLoader::load(ScriptServer::get_global_class_path(identifier));
  288. if (res.is_null()) {
  289. _set_error("Can't load global class " + String(identifier) + ", cyclic reference?", p_expression);
  290. return -1;
  291. }
  292. Variant key = res;
  293. int idx;
  294. if (!codegen.constant_map.has(key)) {
  295. idx = codegen.constant_map.size();
  296. codegen.constant_map[key] = idx;
  297. } else {
  298. idx = codegen.constant_map[key];
  299. }
  300. return idx | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS); //make it a local constant (faster access)
  301. }
  302. #ifdef TOOLS_ENABLED
  303. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(identifier)) {
  304. int idx = codegen.named_globals.find(identifier);
  305. if (idx == -1) {
  306. idx = codegen.named_globals.size();
  307. codegen.named_globals.push_back(identifier);
  308. }
  309. return idx | (GDScriptFunction::ADDR_TYPE_NAMED_GLOBAL << GDScriptFunction::ADDR_BITS);
  310. }
  311. #endif
  312. //not found, error
  313. _set_error("Identifier not found: " + String(identifier), p_expression);
  314. return -1;
  315. } break;
  316. case GDScriptParser::Node::TYPE_CONSTANT: {
  317. //return constant
  318. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(p_expression);
  319. int idx;
  320. if (!codegen.constant_map.has(cn->value)) {
  321. idx = codegen.constant_map.size();
  322. codegen.constant_map[cn->value] = idx;
  323. } else {
  324. idx = codegen.constant_map[cn->value];
  325. }
  326. return idx | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS); //argument (stack root)
  327. } break;
  328. case GDScriptParser::Node::TYPE_SELF: {
  329. //return constant
  330. if (codegen.function_node && codegen.function_node->_static) {
  331. _set_error("'self' not present in static function!", p_expression);
  332. return -1;
  333. }
  334. return (GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
  335. } break;
  336. case GDScriptParser::Node::TYPE_ARRAY: {
  337. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  338. Vector<int> values;
  339. int slevel = p_stack_level;
  340. for (int i = 0; i < an->elements.size(); i++) {
  341. int ret = _parse_expression(codegen, an->elements[i], slevel);
  342. if (ret < 0) {
  343. return ret;
  344. }
  345. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  346. slevel++;
  347. codegen.alloc_stack(slevel);
  348. }
  349. values.push_back(ret);
  350. }
  351. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY);
  352. codegen.opcodes.push_back(values.size());
  353. for (int i = 0; i < values.size(); i++) {
  354. codegen.opcodes.push_back(values[i]);
  355. }
  356. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  357. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  358. codegen.alloc_stack(p_stack_level);
  359. return dst_addr;
  360. } break;
  361. case GDScriptParser::Node::TYPE_DICTIONARY: {
  362. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  363. Vector<int> values;
  364. int slevel = p_stack_level;
  365. for (int i = 0; i < dn->elements.size(); i++) {
  366. int ret = _parse_expression(codegen, dn->elements[i].key, slevel);
  367. if (ret < 0) {
  368. return ret;
  369. }
  370. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  371. slevel++;
  372. codegen.alloc_stack(slevel);
  373. }
  374. values.push_back(ret);
  375. ret = _parse_expression(codegen, dn->elements[i].value, slevel);
  376. if (ret < 0) {
  377. return ret;
  378. }
  379. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  380. slevel++;
  381. codegen.alloc_stack(slevel);
  382. }
  383. values.push_back(ret);
  384. }
  385. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY);
  386. codegen.opcodes.push_back(dn->elements.size());
  387. for (int i = 0; i < values.size(); i++) {
  388. codegen.opcodes.push_back(values[i]);
  389. }
  390. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  391. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  392. codegen.alloc_stack(p_stack_level);
  393. return dst_addr;
  394. } break;
  395. case GDScriptParser::Node::TYPE_CAST: {
  396. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  397. int slevel = p_stack_level;
  398. int src_addr = _parse_expression(codegen, cn->source_node, slevel);
  399. if (src_addr < 0) {
  400. return src_addr;
  401. }
  402. if (src_addr & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  403. slevel++;
  404. codegen.alloc_stack(slevel);
  405. }
  406. GDScriptDataType cast_type = _gdtype_from_datatype(cn->cast_type);
  407. switch (cast_type.kind) {
  408. case GDScriptDataType::BUILTIN: {
  409. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CAST_TO_BUILTIN);
  410. codegen.opcodes.push_back(cast_type.builtin_type);
  411. } break;
  412. case GDScriptDataType::NATIVE: {
  413. int class_idx;
  414. if (GDScriptLanguage::get_singleton()->get_global_map().has(cast_type.native_type)) {
  415. class_idx = GDScriptLanguage::get_singleton()->get_global_map()[cast_type.native_type];
  416. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root)
  417. } else {
  418. _set_error("Invalid native class type '" + String(cast_type.native_type) + "'.", cn);
  419. return -1;
  420. }
  421. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CAST_TO_NATIVE); // perform operator
  422. codegen.opcodes.push_back(class_idx); // variable type
  423. } break;
  424. case GDScriptDataType::SCRIPT:
  425. case GDScriptDataType::GDSCRIPT: {
  426. Variant script = cast_type.script_type;
  427. int idx = codegen.get_constant_pos(script);
  428. idx |= GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS; //make it a local constant (faster access)
  429. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CAST_TO_SCRIPT); // perform operator
  430. codegen.opcodes.push_back(idx); // variable type
  431. } break;
  432. default: {
  433. _set_error("Parser bug: unresolved data type.", cn);
  434. return -1;
  435. }
  436. }
  437. codegen.opcodes.push_back(src_addr); // source address
  438. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  439. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  440. codegen.alloc_stack(p_stack_level);
  441. return dst_addr;
  442. } break;
  443. case GDScriptParser::Node::TYPE_OPERATOR: {
  444. //hell breaks loose
  445. const GDScriptParser::OperatorNode *on = static_cast<const GDScriptParser::OperatorNode *>(p_expression);
  446. switch (on->op) {
  447. //call/constructor operator
  448. case GDScriptParser::OperatorNode::OP_PARENT_CALL: {
  449. ERR_FAIL_COND_V(on->arguments.size() < 1, -1);
  450. const GDScriptParser::IdentifierNode *in = (const GDScriptParser::IdentifierNode *)on->arguments[0];
  451. Vector<int> arguments;
  452. int slevel = p_stack_level;
  453. for (int i = 1; i < on->arguments.size(); i++) {
  454. int ret = _parse_expression(codegen, on->arguments[i], slevel);
  455. if (ret < 0) {
  456. return ret;
  457. }
  458. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  459. slevel++;
  460. codegen.alloc_stack(slevel);
  461. }
  462. arguments.push_back(ret);
  463. }
  464. //push call bytecode
  465. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_SELF_BASE); // basic type constructor
  466. codegen.opcodes.push_back(codegen.get_name_map_pos(in->name)); //instance
  467. codegen.opcodes.push_back(arguments.size()); //argument count
  468. codegen.alloc_call(arguments.size());
  469. for (int i = 0; i < arguments.size(); i++) {
  470. codegen.opcodes.push_back(arguments[i]); //arguments
  471. }
  472. } break;
  473. case GDScriptParser::OperatorNode::OP_CALL: {
  474. if (on->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  475. //construct a basic type
  476. ERR_FAIL_COND_V(on->arguments.size() < 1, -1);
  477. const GDScriptParser::TypeNode *tn = (const GDScriptParser::TypeNode *)on->arguments[0];
  478. int vtype = tn->vtype;
  479. Vector<int> arguments;
  480. int slevel = p_stack_level;
  481. for (int i = 1; i < on->arguments.size(); i++) {
  482. int ret = _parse_expression(codegen, on->arguments[i], slevel);
  483. if (ret < 0) {
  484. return ret;
  485. }
  486. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  487. slevel++;
  488. codegen.alloc_stack(slevel);
  489. }
  490. arguments.push_back(ret);
  491. }
  492. //push call bytecode
  493. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CONSTRUCT); // basic type constructor
  494. codegen.opcodes.push_back(vtype); //instance
  495. codegen.opcodes.push_back(arguments.size()); //argument count
  496. codegen.alloc_call(arguments.size());
  497. for (int i = 0; i < arguments.size(); i++) {
  498. codegen.opcodes.push_back(arguments[i]); //arguments
  499. }
  500. } else if (on->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  501. //built in function
  502. ERR_FAIL_COND_V(on->arguments.size() < 1, -1);
  503. Vector<int> arguments;
  504. int slevel = p_stack_level;
  505. for (int i = 1; i < on->arguments.size(); i++) {
  506. int ret = _parse_expression(codegen, on->arguments[i], slevel);
  507. if (ret < 0) {
  508. return ret;
  509. }
  510. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  511. slevel++;
  512. codegen.alloc_stack(slevel);
  513. }
  514. arguments.push_back(ret);
  515. }
  516. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN);
  517. codegen.opcodes.push_back(static_cast<const GDScriptParser::BuiltInFunctionNode *>(on->arguments[0])->function);
  518. codegen.opcodes.push_back(on->arguments.size() - 1);
  519. codegen.alloc_call(on->arguments.size() - 1);
  520. for (int i = 0; i < arguments.size(); i++) {
  521. codegen.opcodes.push_back(arguments[i]);
  522. }
  523. } else {
  524. //regular function
  525. ERR_FAIL_COND_V(on->arguments.size() < 2, -1);
  526. const GDScriptParser::Node *instance = on->arguments[0];
  527. if (instance->type == GDScriptParser::Node::TYPE_SELF) {
  528. //room for optimization
  529. }
  530. Vector<int> arguments;
  531. int slevel = p_stack_level;
  532. for (int i = 0; i < on->arguments.size(); i++) {
  533. int ret;
  534. if (i == 0 && on->arguments[i]->type == GDScriptParser::Node::TYPE_SELF && codegen.function_node && codegen.function_node->_static) {
  535. //static call to self
  536. ret = (GDScriptFunction::ADDR_TYPE_CLASS << GDScriptFunction::ADDR_BITS);
  537. } else if (i == 1) {
  538. if (on->arguments[i]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  539. _set_error("Attempt to call a non-identifier.", on);
  540. return -1;
  541. }
  542. GDScriptParser::IdentifierNode *id = static_cast<GDScriptParser::IdentifierNode *>(on->arguments[i]);
  543. ret = codegen.get_name_map_pos(id->name);
  544. } else {
  545. ret = _parse_expression(codegen, on->arguments[i], slevel);
  546. if (ret < 0) {
  547. return ret;
  548. }
  549. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  550. slevel++;
  551. codegen.alloc_stack(slevel);
  552. }
  553. }
  554. arguments.push_back(ret);
  555. }
  556. codegen.opcodes.push_back(p_root ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN); // perform operator
  557. codegen.opcodes.push_back(on->arguments.size() - 2);
  558. codegen.alloc_call(on->arguments.size() - 2);
  559. for (int i = 0; i < arguments.size(); i++) {
  560. codegen.opcodes.push_back(arguments[i]);
  561. }
  562. }
  563. } break;
  564. case GDScriptParser::OperatorNode::OP_YIELD: {
  565. ERR_FAIL_COND_V(on->arguments.size() && on->arguments.size() != 2, -1);
  566. Vector<int> arguments;
  567. int slevel = p_stack_level;
  568. for (int i = 0; i < on->arguments.size(); i++) {
  569. int ret = _parse_expression(codegen, on->arguments[i], slevel);
  570. if (ret < 0) {
  571. return ret;
  572. }
  573. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  574. slevel++;
  575. codegen.alloc_stack(slevel);
  576. }
  577. arguments.push_back(ret);
  578. }
  579. //push call bytecode
  580. codegen.opcodes.push_back(arguments.size() == 0 ? GDScriptFunction::OPCODE_YIELD : GDScriptFunction::OPCODE_YIELD_SIGNAL); // basic type constructor
  581. for (int i = 0; i < arguments.size(); i++) {
  582. codegen.opcodes.push_back(arguments[i]); //arguments
  583. }
  584. codegen.opcodes.push_back(GDScriptFunction::OPCODE_YIELD_RESUME);
  585. //next will be where to place the result :)
  586. } break;
  587. //indexing operator
  588. case GDScriptParser::OperatorNode::OP_INDEX:
  589. case GDScriptParser::OperatorNode::OP_INDEX_NAMED: {
  590. ERR_FAIL_COND_V(on->arguments.size() != 2, -1);
  591. int slevel = p_stack_level;
  592. bool named = (on->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED);
  593. int from = _parse_expression(codegen, on->arguments[0], slevel);
  594. if (from < 0) {
  595. return from;
  596. }
  597. int index;
  598. if (p_index_addr != 0) {
  599. index = p_index_addr;
  600. } else if (named) {
  601. if (on->arguments[0]->type == GDScriptParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) {
  602. GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(on->arguments[1]);
  603. const Map<StringName, GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(identifier->name);
  604. #ifdef DEBUG_ENABLED
  605. if (MI && MI->get().getter == codegen.function_node->name) {
  606. String n = static_cast<GDScriptParser::IdentifierNode *>(on->arguments[1])->name;
  607. _set_error("Must use '" + n + "' instead of 'self." + n + "' in getter.", on);
  608. return -1;
  609. }
  610. #endif
  611. if (MI && MI->get().getter == "") {
  612. // Faster than indexing self (as if no self. had been used)
  613. return (MI->get().index) | (GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS);
  614. }
  615. }
  616. index = codegen.get_name_map_pos(static_cast<GDScriptParser::IdentifierNode *>(on->arguments[1])->name);
  617. } else {
  618. if (on->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT && static_cast<const GDScriptParser::ConstantNode *>(on->arguments[1])->value.get_type() == Variant::STRING) {
  619. //also, somehow, named (speed up anyway)
  620. StringName name = static_cast<const GDScriptParser::ConstantNode *>(on->arguments[1])->value;
  621. index = codegen.get_name_map_pos(name);
  622. named = true;
  623. } else {
  624. //regular indexing
  625. if (from & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  626. slevel++;
  627. codegen.alloc_stack(slevel);
  628. }
  629. index = _parse_expression(codegen, on->arguments[1], slevel);
  630. if (index < 0) {
  631. return index;
  632. }
  633. }
  634. }
  635. codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_GET_NAMED : GDScriptFunction::OPCODE_GET); // perform operator
  636. codegen.opcodes.push_back(from); // argument 1
  637. codegen.opcodes.push_back(index); // argument 2 (unary only takes one parameter)
  638. } break;
  639. case GDScriptParser::OperatorNode::OP_AND: {
  640. // AND operator with early out on failure
  641. int res = _parse_expression(codegen, on->arguments[0], p_stack_level);
  642. if (res < 0) {
  643. return res;
  644. }
  645. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  646. codegen.opcodes.push_back(res);
  647. int jump_fail_pos = codegen.opcodes.size();
  648. codegen.opcodes.push_back(0);
  649. res = _parse_expression(codegen, on->arguments[1], p_stack_level);
  650. if (res < 0) {
  651. return res;
  652. }
  653. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  654. codegen.opcodes.push_back(res);
  655. int jump_fail_pos2 = codegen.opcodes.size();
  656. codegen.opcodes.push_back(0);
  657. codegen.alloc_stack(p_stack_level); //it will be used..
  658. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  659. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  660. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  661. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  662. codegen.opcodes.write[jump_fail_pos] = codegen.opcodes.size();
  663. codegen.opcodes.write[jump_fail_pos2] = codegen.opcodes.size();
  664. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  665. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  666. return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  667. } break;
  668. case GDScriptParser::OperatorNode::OP_OR: {
  669. // OR operator with early out on success
  670. int res = _parse_expression(codegen, on->arguments[0], p_stack_level);
  671. if (res < 0) {
  672. return res;
  673. }
  674. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF);
  675. codegen.opcodes.push_back(res);
  676. int jump_success_pos = codegen.opcodes.size();
  677. codegen.opcodes.push_back(0);
  678. res = _parse_expression(codegen, on->arguments[1], p_stack_level);
  679. if (res < 0) {
  680. return res;
  681. }
  682. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF);
  683. codegen.opcodes.push_back(res);
  684. int jump_success_pos2 = codegen.opcodes.size();
  685. codegen.opcodes.push_back(0);
  686. codegen.alloc_stack(p_stack_level); //it will be used..
  687. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  688. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  689. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  690. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  691. codegen.opcodes.write[jump_success_pos] = codegen.opcodes.size();
  692. codegen.opcodes.write[jump_success_pos2] = codegen.opcodes.size();
  693. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  694. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  695. return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  696. } break;
  697. // ternary operators
  698. case GDScriptParser::OperatorNode::OP_TERNARY_IF: {
  699. // x IF a ELSE y operator with early out on failure
  700. int res = _parse_expression(codegen, on->arguments[0], p_stack_level);
  701. if (res < 0) {
  702. return res;
  703. }
  704. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  705. codegen.opcodes.push_back(res);
  706. int jump_fail_pos = codegen.opcodes.size();
  707. codegen.opcodes.push_back(0);
  708. res = _parse_expression(codegen, on->arguments[1], p_stack_level);
  709. if (res < 0) {
  710. return res;
  711. }
  712. codegen.alloc_stack(p_stack_level); //it will be used..
  713. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  714. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  715. codegen.opcodes.push_back(res);
  716. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  717. int jump_past_pos = codegen.opcodes.size();
  718. codegen.opcodes.push_back(0);
  719. codegen.opcodes.write[jump_fail_pos] = codegen.opcodes.size();
  720. res = _parse_expression(codegen, on->arguments[2], p_stack_level);
  721. if (res < 0) {
  722. return res;
  723. }
  724. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  725. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  726. codegen.opcodes.push_back(res);
  727. codegen.opcodes.write[jump_past_pos] = codegen.opcodes.size();
  728. return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  729. } break;
  730. //unary operators
  731. case GDScriptParser::OperatorNode::OP_NEG: {
  732. if (!_create_unary_operator(codegen, on, Variant::OP_NEGATE, p_stack_level)) {
  733. return -1;
  734. }
  735. } break;
  736. case GDScriptParser::OperatorNode::OP_POS: {
  737. if (!_create_unary_operator(codegen, on, Variant::OP_POSITIVE, p_stack_level)) {
  738. return -1;
  739. }
  740. } break;
  741. case GDScriptParser::OperatorNode::OP_NOT: {
  742. if (!_create_unary_operator(codegen, on, Variant::OP_NOT, p_stack_level)) {
  743. return -1;
  744. }
  745. } break;
  746. case GDScriptParser::OperatorNode::OP_BIT_INVERT: {
  747. if (!_create_unary_operator(codegen, on, Variant::OP_BIT_NEGATE, p_stack_level)) {
  748. return -1;
  749. }
  750. } break;
  751. //binary operators (in precedence order)
  752. case GDScriptParser::OperatorNode::OP_IN: {
  753. if (!_create_binary_operator(codegen, on, Variant::OP_IN, p_stack_level)) {
  754. return -1;
  755. }
  756. } break;
  757. case GDScriptParser::OperatorNode::OP_EQUAL: {
  758. if (!_create_binary_operator(codegen, on, Variant::OP_EQUAL, p_stack_level)) {
  759. return -1;
  760. }
  761. } break;
  762. case GDScriptParser::OperatorNode::OP_NOT_EQUAL: {
  763. if (!_create_binary_operator(codegen, on, Variant::OP_NOT_EQUAL, p_stack_level)) {
  764. return -1;
  765. }
  766. } break;
  767. case GDScriptParser::OperatorNode::OP_LESS: {
  768. if (!_create_binary_operator(codegen, on, Variant::OP_LESS, p_stack_level)) {
  769. return -1;
  770. }
  771. } break;
  772. case GDScriptParser::OperatorNode::OP_LESS_EQUAL: {
  773. if (!_create_binary_operator(codegen, on, Variant::OP_LESS_EQUAL, p_stack_level)) {
  774. return -1;
  775. }
  776. } break;
  777. case GDScriptParser::OperatorNode::OP_GREATER: {
  778. if (!_create_binary_operator(codegen, on, Variant::OP_GREATER, p_stack_level)) {
  779. return -1;
  780. }
  781. } break;
  782. case GDScriptParser::OperatorNode::OP_GREATER_EQUAL: {
  783. if (!_create_binary_operator(codegen, on, Variant::OP_GREATER_EQUAL, p_stack_level)) {
  784. return -1;
  785. }
  786. } break;
  787. case GDScriptParser::OperatorNode::OP_ADD: {
  788. if (!_create_binary_operator(codegen, on, Variant::OP_ADD, p_stack_level)) {
  789. return -1;
  790. }
  791. } break;
  792. case GDScriptParser::OperatorNode::OP_SUB: {
  793. if (!_create_binary_operator(codegen, on, Variant::OP_SUBTRACT, p_stack_level)) {
  794. return -1;
  795. }
  796. } break;
  797. case GDScriptParser::OperatorNode::OP_MUL: {
  798. if (!_create_binary_operator(codegen, on, Variant::OP_MULTIPLY, p_stack_level)) {
  799. return -1;
  800. }
  801. } break;
  802. case GDScriptParser::OperatorNode::OP_DIV: {
  803. if (!_create_binary_operator(codegen, on, Variant::OP_DIVIDE, p_stack_level)) {
  804. return -1;
  805. }
  806. } break;
  807. case GDScriptParser::OperatorNode::OP_MOD: {
  808. if (!_create_binary_operator(codegen, on, Variant::OP_MODULE, p_stack_level)) {
  809. return -1;
  810. }
  811. } break;
  812. //case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
  813. //case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
  814. case GDScriptParser::OperatorNode::OP_BIT_AND: {
  815. if (!_create_binary_operator(codegen, on, Variant::OP_BIT_AND, p_stack_level)) {
  816. return -1;
  817. }
  818. } break;
  819. case GDScriptParser::OperatorNode::OP_BIT_OR: {
  820. if (!_create_binary_operator(codegen, on, Variant::OP_BIT_OR, p_stack_level)) {
  821. return -1;
  822. }
  823. } break;
  824. case GDScriptParser::OperatorNode::OP_BIT_XOR: {
  825. if (!_create_binary_operator(codegen, on, Variant::OP_BIT_XOR, p_stack_level)) {
  826. return -1;
  827. }
  828. } break;
  829. //shift
  830. case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: {
  831. if (!_create_binary_operator(codegen, on, Variant::OP_SHIFT_LEFT, p_stack_level)) {
  832. return -1;
  833. }
  834. } break;
  835. case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: {
  836. if (!_create_binary_operator(codegen, on, Variant::OP_SHIFT_RIGHT, p_stack_level)) {
  837. return -1;
  838. }
  839. } break;
  840. //assignment operators
  841. case GDScriptParser::OperatorNode::OP_ASSIGN_ADD:
  842. case GDScriptParser::OperatorNode::OP_ASSIGN_SUB:
  843. case GDScriptParser::OperatorNode::OP_ASSIGN_MUL:
  844. case GDScriptParser::OperatorNode::OP_ASSIGN_DIV:
  845. case GDScriptParser::OperatorNode::OP_ASSIGN_MOD:
  846. case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  847. case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  848. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_AND:
  849. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_OR:
  850. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR:
  851. case GDScriptParser::OperatorNode::OP_INIT_ASSIGN:
  852. case GDScriptParser::OperatorNode::OP_ASSIGN: {
  853. ERR_FAIL_COND_V(on->arguments.size() != 2, -1);
  854. if (on->arguments[0]->type == GDScriptParser::Node::TYPE_OPERATOR && (static_cast<GDScriptParser::OperatorNode *>(on->arguments[0])->op == GDScriptParser::OperatorNode::OP_INDEX || static_cast<GDScriptParser::OperatorNode *>(on->arguments[0])->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED)) {
  855. // SET (chained) MODE!
  856. #ifdef DEBUG_ENABLED
  857. if (static_cast<GDScriptParser::OperatorNode *>(on->arguments[0])->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  858. const GDScriptParser::OperatorNode *inon = static_cast<GDScriptParser::OperatorNode *>(on->arguments[0]);
  859. if (inon->arguments[0]->type == GDScriptParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) {
  860. const Map<StringName, GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(static_cast<GDScriptParser::IdentifierNode *>(inon->arguments[1])->name);
  861. if (MI && MI->get().setter == codegen.function_node->name) {
  862. String n = static_cast<GDScriptParser::IdentifierNode *>(inon->arguments[1])->name;
  863. _set_error("Must use '" + n + "' instead of 'self." + n + "' in setter.", inon);
  864. return -1;
  865. }
  866. }
  867. }
  868. #endif
  869. int slevel = p_stack_level;
  870. GDScriptParser::OperatorNode *op = static_cast<GDScriptParser::OperatorNode *>(on->arguments[0]);
  871. /* Find chain of sets */
  872. StringName assign_property;
  873. List<GDScriptParser::OperatorNode *> chain;
  874. {
  875. //create get/set chain
  876. GDScriptParser::OperatorNode *n = op;
  877. while (true) {
  878. chain.push_back(n);
  879. if (n->arguments[0]->type != GDScriptParser::Node::TYPE_OPERATOR) {
  880. //check for a built-in property
  881. if (n->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  882. GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(n->arguments[0]);
  883. if (_is_class_member_property(codegen, identifier->name)) {
  884. assign_property = identifier->name;
  885. }
  886. }
  887. break;
  888. }
  889. n = static_cast<GDScriptParser::OperatorNode *>(n->arguments[0]);
  890. if (n->op != GDScriptParser::OperatorNode::OP_INDEX && n->op != GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  891. break;
  892. }
  893. }
  894. }
  895. /* Chain of gets */
  896. //get at (potential) root stack pos, so it can be returned
  897. int prev_pos = _parse_expression(codegen, chain.back()->get()->arguments[0], slevel);
  898. if (prev_pos < 0) {
  899. return prev_pos;
  900. }
  901. int retval = prev_pos;
  902. if (retval & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  903. slevel++;
  904. codegen.alloc_stack(slevel);
  905. }
  906. Vector<int> setchain;
  907. if (assign_property != StringName()) {
  908. // recover and assign at the end, this allows stuff like
  909. // position.x+=2.0
  910. // in Node2D
  911. setchain.push_back(prev_pos);
  912. setchain.push_back(codegen.get_name_map_pos(assign_property));
  913. setchain.push_back(GDScriptFunction::OPCODE_SET_MEMBER);
  914. }
  915. for (List<GDScriptParser::OperatorNode *>::Element *E = chain.back(); E; E = E->prev()) {
  916. if (E == chain.front()) { //ignore first
  917. break;
  918. }
  919. bool named = E->get()->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED;
  920. int key_idx;
  921. if (named) {
  922. key_idx = codegen.get_name_map_pos(static_cast<const GDScriptParser::IdentifierNode *>(E->get()->arguments[1])->name);
  923. //printf("named key %x\n",key_idx);
  924. } else {
  925. if (prev_pos & (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS)) {
  926. slevel++;
  927. codegen.alloc_stack(slevel);
  928. }
  929. GDScriptParser::Node *key = E->get()->arguments[1];
  930. key_idx = _parse_expression(codegen, key, slevel);
  931. //printf("expr key %x\n",key_idx);
  932. //stack was raised here if retval was stack but..
  933. }
  934. if (key_idx < 0) { //error
  935. return key_idx;
  936. }
  937. codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_GET_NAMED : GDScriptFunction::OPCODE_GET);
  938. codegen.opcodes.push_back(prev_pos);
  939. codegen.opcodes.push_back(key_idx);
  940. slevel++;
  941. codegen.alloc_stack(slevel);
  942. int dst_pos = (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) | slevel;
  943. codegen.opcodes.push_back(dst_pos);
  944. //add in reverse order, since it will be reverted
  945. setchain.push_back(dst_pos);
  946. setchain.push_back(key_idx);
  947. setchain.push_back(prev_pos);
  948. setchain.push_back(named ? GDScriptFunction::OPCODE_SET_NAMED : GDScriptFunction::OPCODE_SET);
  949. prev_pos = dst_pos;
  950. }
  951. setchain.invert();
  952. int set_index;
  953. bool named = false;
  954. if (op->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  955. set_index = codegen.get_name_map_pos(static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name);
  956. named = true;
  957. } else {
  958. set_index = _parse_expression(codegen, op->arguments[1], slevel + 1);
  959. named = false;
  960. }
  961. if (set_index < 0) { //error
  962. return set_index;
  963. }
  964. if (set_index & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  965. slevel++;
  966. codegen.alloc_stack(slevel);
  967. }
  968. int set_value = _parse_assign_right_expression(codegen, on, slevel + 1, named ? 0 : set_index);
  969. if (set_value < 0) { //error
  970. return set_value;
  971. }
  972. codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_SET_NAMED : GDScriptFunction::OPCODE_SET);
  973. codegen.opcodes.push_back(prev_pos);
  974. codegen.opcodes.push_back(set_index);
  975. codegen.opcodes.push_back(set_value);
  976. for (int i = 0; i < setchain.size(); i++) {
  977. codegen.opcodes.push_back(setchain[i]);
  978. }
  979. return retval;
  980. } else if (on->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && _is_class_member_property(codegen, static_cast<GDScriptParser::IdentifierNode *>(on->arguments[0])->name)) {
  981. //assignment to member property
  982. int slevel = p_stack_level;
  983. int src_address = _parse_assign_right_expression(codegen, on, slevel);
  984. if (src_address < 0) {
  985. return -1;
  986. }
  987. StringName name = static_cast<GDScriptParser::IdentifierNode *>(on->arguments[0])->name;
  988. codegen.opcodes.push_back(GDScriptFunction::OPCODE_SET_MEMBER);
  989. codegen.opcodes.push_back(codegen.get_name_map_pos(name));
  990. codegen.opcodes.push_back(src_address);
  991. return GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS;
  992. } else {
  993. //REGULAR ASSIGNMENT MODE!!
  994. int slevel = p_stack_level;
  995. int dst_address_a = _parse_expression(codegen, on->arguments[0], slevel, false, on->op == GDScriptParser::OperatorNode::OP_INIT_ASSIGN);
  996. if (dst_address_a < 0) {
  997. return -1;
  998. }
  999. if (dst_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  1000. slevel++;
  1001. codegen.alloc_stack(slevel);
  1002. }
  1003. int src_address_b = _parse_assign_right_expression(codegen, on, slevel);
  1004. if (src_address_b < 0) {
  1005. return -1;
  1006. }
  1007. GDScriptDataType assign_type = _gdtype_from_datatype(on->arguments[0]->get_datatype());
  1008. if (assign_type.has_type && !on->datatype.has_type) {
  1009. // Typed assignment
  1010. switch (assign_type.kind) {
  1011. case GDScriptDataType::BUILTIN: {
  1012. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN); // perform operator
  1013. codegen.opcodes.push_back(assign_type.builtin_type); // variable type
  1014. codegen.opcodes.push_back(dst_address_a); // argument 1
  1015. codegen.opcodes.push_back(src_address_b); // argument 2
  1016. } break;
  1017. case GDScriptDataType::NATIVE: {
  1018. int class_idx;
  1019. if (GDScriptLanguage::get_singleton()->get_global_map().has(assign_type.native_type)) {
  1020. class_idx = GDScriptLanguage::get_singleton()->get_global_map()[assign_type.native_type];
  1021. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root)
  1022. } else {
  1023. _set_error("Invalid native class type '" + String(assign_type.native_type) + "'.", on->arguments[0]);
  1024. return -1;
  1025. }
  1026. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE); // perform operator
  1027. codegen.opcodes.push_back(class_idx); // variable type
  1028. codegen.opcodes.push_back(dst_address_a); // argument 1
  1029. codegen.opcodes.push_back(src_address_b); // argument 2
  1030. } break;
  1031. case GDScriptDataType::SCRIPT:
  1032. case GDScriptDataType::GDSCRIPT: {
  1033. Variant script = assign_type.script_type;
  1034. int idx = codegen.get_constant_pos(script);
  1035. idx |= GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS; //make it a local constant (faster access)
  1036. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT); // perform operator
  1037. codegen.opcodes.push_back(idx); // variable type
  1038. codegen.opcodes.push_back(dst_address_a); // argument 1
  1039. codegen.opcodes.push_back(src_address_b); // argument 2
  1040. } break;
  1041. default: {
  1042. ERR_PRINT("Compiler bug: unresolved assign.");
  1043. // Shouldn't get here, but fail-safe to a regular assignment
  1044. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); // perform operator
  1045. codegen.opcodes.push_back(dst_address_a); // argument 1
  1046. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  1047. }
  1048. }
  1049. } else {
  1050. // Either untyped assignment or already type-checked by the parser
  1051. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); // perform operator
  1052. codegen.opcodes.push_back(dst_address_a); // argument 1
  1053. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  1054. }
  1055. return dst_address_a; //if anything, returns wathever was assigned or correct stack position
  1056. }
  1057. } break;
  1058. case GDScriptParser::OperatorNode::OP_IS: {
  1059. ERR_FAIL_COND_V(on->arguments.size() != 2, false);
  1060. int slevel = p_stack_level;
  1061. int src_address_a = _parse_expression(codegen, on->arguments[0], slevel);
  1062. if (src_address_a < 0) {
  1063. return -1;
  1064. }
  1065. if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  1066. slevel++; //uses stack for return, increase stack
  1067. }
  1068. int src_address_b = _parse_expression(codegen, on->arguments[1], slevel);
  1069. if (src_address_b < 0) {
  1070. return -1;
  1071. }
  1072. codegen.opcodes.push_back(GDScriptFunction::OPCODE_EXTENDS_TEST); // perform operator
  1073. codegen.opcodes.push_back(src_address_a); // argument 1
  1074. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  1075. } break;
  1076. case GDScriptParser::OperatorNode::OP_IS_BUILTIN: {
  1077. ERR_FAIL_COND_V(on->arguments.size() != 2, false);
  1078. ERR_FAIL_COND_V(on->arguments[1]->type != GDScriptParser::Node::TYPE_TYPE, false);
  1079. int slevel = p_stack_level;
  1080. int src_address_a = _parse_expression(codegen, on->arguments[0], slevel);
  1081. if (src_address_a < 0) {
  1082. return -1;
  1083. }
  1084. if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  1085. slevel++; //uses stack for return, increase stack
  1086. }
  1087. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(on->arguments[1]);
  1088. codegen.opcodes.push_back(GDScriptFunction::OPCODE_IS_BUILTIN); // perform operator
  1089. codegen.opcodes.push_back(src_address_a); // argument 1
  1090. codegen.opcodes.push_back((int)tn->vtype); // argument 2 (unary only takes one parameter)
  1091. } break;
  1092. default: {
  1093. ERR_FAIL_V_MSG(0, "Bug in bytecode compiler, unexpected operator #" + itos(on->op) + " in parse tree while parsing expression."); //unreachable code
  1094. } break;
  1095. }
  1096. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1097. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  1098. codegen.alloc_stack(p_stack_level);
  1099. return dst_addr;
  1100. } break;
  1101. //TYPE_TYPE,
  1102. default: {
  1103. ERR_FAIL_V_MSG(-1, "Bug in bytecode compiler, unexpected node in parse tree while parsing expression."); //unreachable code
  1104. } break;
  1105. }
  1106. }
  1107. Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::BlockNode *p_block, int p_stack_level, int p_break_addr, int p_continue_addr) {
  1108. codegen.push_stack_identifiers();
  1109. int new_identifiers = 0;
  1110. codegen.current_line = p_block->line;
  1111. for (int i = 0; i < p_block->statements.size(); i++) {
  1112. const GDScriptParser::Node *s = p_block->statements[i];
  1113. switch (s->type) {
  1114. case GDScriptParser::Node::TYPE_NEWLINE: {
  1115. #ifdef DEBUG_ENABLED
  1116. const GDScriptParser::NewLineNode *nl = static_cast<const GDScriptParser::NewLineNode *>(s);
  1117. codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE);
  1118. codegen.opcodes.push_back(nl->line);
  1119. codegen.current_line = nl->line;
  1120. #endif
  1121. } break;
  1122. case GDScriptParser::Node::TYPE_CONTROL_FLOW: {
  1123. // try subblocks
  1124. const GDScriptParser::ControlFlowNode *cf = static_cast<const GDScriptParser::ControlFlowNode *>(s);
  1125. switch (cf->cf_type) {
  1126. case GDScriptParser::ControlFlowNode::CF_MATCH: {
  1127. GDScriptParser::MatchNode *match = cf->match;
  1128. GDScriptParser::IdentifierNode *id = memnew(GDScriptParser::IdentifierNode);
  1129. id->name = "#match_value";
  1130. // var #match_value
  1131. // copied because there is no _parse_statement :(
  1132. codegen.add_stack_identifier(id->name, p_stack_level++);
  1133. codegen.alloc_stack(p_stack_level);
  1134. new_identifiers++;
  1135. GDScriptParser::OperatorNode *op = memnew(GDScriptParser::OperatorNode);
  1136. op->op = GDScriptParser::OperatorNode::OP_ASSIGN;
  1137. op->arguments.push_back(id);
  1138. op->arguments.push_back(match->val_to_match);
  1139. int ret = _parse_expression(codegen, op, p_stack_level);
  1140. if (ret < 0) {
  1141. memdelete(id);
  1142. memdelete(op);
  1143. return ERR_PARSE_ERROR;
  1144. }
  1145. // break address
  1146. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1147. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  1148. int break_addr = codegen.opcodes.size();
  1149. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1150. codegen.opcodes.push_back(0); // break addr
  1151. for (int j = 0; j < match->compiled_pattern_branches.size(); j++) {
  1152. GDScriptParser::MatchNode::CompiledPatternBranch branch = match->compiled_pattern_branches[j];
  1153. // jump over continue
  1154. // jump unconditionally
  1155. // continue address
  1156. // compile the condition
  1157. int ret2 = _parse_expression(codegen, branch.compiled_pattern, p_stack_level);
  1158. if (ret2 < 0) {
  1159. memdelete(id);
  1160. memdelete(op);
  1161. return ERR_PARSE_ERROR;
  1162. }
  1163. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF);
  1164. codegen.opcodes.push_back(ret2);
  1165. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  1166. int continue_addr = codegen.opcodes.size();
  1167. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1168. codegen.opcodes.push_back(0);
  1169. Error err = _parse_block(codegen, branch.body, p_stack_level, p_break_addr, continue_addr);
  1170. if (err) {
  1171. memdelete(id);
  1172. memdelete(op);
  1173. return ERR_PARSE_ERROR;
  1174. }
  1175. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1176. codegen.opcodes.push_back(break_addr);
  1177. codegen.opcodes.write[continue_addr + 1] = codegen.opcodes.size();
  1178. }
  1179. codegen.opcodes.write[break_addr + 1] = codegen.opcodes.size();
  1180. memdelete(id);
  1181. memdelete(op);
  1182. } break;
  1183. case GDScriptParser::ControlFlowNode::CF_IF: {
  1184. int ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false);
  1185. if (ret2 < 0) {
  1186. return ERR_PARSE_ERROR;
  1187. }
  1188. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1189. codegen.opcodes.push_back(ret2);
  1190. int else_addr = codegen.opcodes.size();
  1191. codegen.opcodes.push_back(0); //temporary
  1192. Error err = _parse_block(codegen, cf->body, p_stack_level, p_break_addr, p_continue_addr);
  1193. if (err) {
  1194. return err;
  1195. }
  1196. if (cf->body_else) {
  1197. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1198. int end_addr = codegen.opcodes.size();
  1199. codegen.opcodes.push_back(0);
  1200. codegen.opcodes.write[else_addr] = codegen.opcodes.size();
  1201. codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE);
  1202. codegen.opcodes.push_back(cf->body_else->line);
  1203. codegen.current_line = cf->body_else->line;
  1204. Error err2 = _parse_block(codegen, cf->body_else, p_stack_level, p_break_addr, p_continue_addr);
  1205. if (err2) {
  1206. return err2;
  1207. }
  1208. codegen.opcodes.write[end_addr] = codegen.opcodes.size();
  1209. } else {
  1210. //end without else
  1211. codegen.opcodes.write[else_addr] = codegen.opcodes.size();
  1212. }
  1213. } break;
  1214. case GDScriptParser::ControlFlowNode::CF_FOR: {
  1215. int slevel = p_stack_level;
  1216. int iter_stack_pos = slevel;
  1217. int iterator_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1218. int counter_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1219. int container_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1220. codegen.alloc_stack(slevel);
  1221. codegen.push_stack_identifiers();
  1222. codegen.add_stack_identifier(static_cast<const GDScriptParser::IdentifierNode *>(cf->arguments[0])->name, iter_stack_pos);
  1223. int ret2 = _parse_expression(codegen, cf->arguments[1], slevel, false);
  1224. if (ret2 < 0) {
  1225. return ERR_COMPILATION_FAILED;
  1226. }
  1227. //assign container
  1228. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  1229. codegen.opcodes.push_back(container_pos);
  1230. codegen.opcodes.push_back(ret2);
  1231. //begin loop
  1232. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ITERATE_BEGIN);
  1233. codegen.opcodes.push_back(counter_pos);
  1234. codegen.opcodes.push_back(container_pos);
  1235. codegen.opcodes.push_back(codegen.opcodes.size() + 4);
  1236. codegen.opcodes.push_back(iterator_pos);
  1237. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); //skip code for next
  1238. codegen.opcodes.push_back(codegen.opcodes.size() + 8);
  1239. //break loop
  1240. int break_pos = codegen.opcodes.size();
  1241. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); //skip code for next
  1242. codegen.opcodes.push_back(0); //skip code for next
  1243. //next loop
  1244. int continue_pos = codegen.opcodes.size();
  1245. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ITERATE);
  1246. codegen.opcodes.push_back(counter_pos);
  1247. codegen.opcodes.push_back(container_pos);
  1248. codegen.opcodes.push_back(break_pos);
  1249. codegen.opcodes.push_back(iterator_pos);
  1250. Error err = _parse_block(codegen, cf->body, slevel, break_pos, continue_pos);
  1251. if (err) {
  1252. return err;
  1253. }
  1254. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1255. codegen.opcodes.push_back(continue_pos);
  1256. codegen.opcodes.write[break_pos + 1] = codegen.opcodes.size();
  1257. codegen.pop_stack_identifiers();
  1258. } break;
  1259. case GDScriptParser::ControlFlowNode::CF_WHILE: {
  1260. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1261. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  1262. int break_addr = codegen.opcodes.size();
  1263. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1264. codegen.opcodes.push_back(0);
  1265. int continue_addr = codegen.opcodes.size();
  1266. int ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false);
  1267. if (ret2 < 0) {
  1268. return ERR_PARSE_ERROR;
  1269. }
  1270. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1271. codegen.opcodes.push_back(ret2);
  1272. codegen.opcodes.push_back(break_addr);
  1273. Error err = _parse_block(codegen, cf->body, p_stack_level, break_addr, continue_addr);
  1274. if (err) {
  1275. return err;
  1276. }
  1277. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1278. codegen.opcodes.push_back(continue_addr);
  1279. codegen.opcodes.write[break_addr + 1] = codegen.opcodes.size();
  1280. } break;
  1281. case GDScriptParser::ControlFlowNode::CF_BREAK: {
  1282. if (p_break_addr < 0) {
  1283. _set_error("'break'' not within loop", cf);
  1284. return ERR_COMPILATION_FAILED;
  1285. }
  1286. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1287. codegen.opcodes.push_back(p_break_addr);
  1288. } break;
  1289. case GDScriptParser::ControlFlowNode::CF_CONTINUE: {
  1290. if (p_continue_addr < 0) {
  1291. _set_error("'continue' not within loop", cf);
  1292. return ERR_COMPILATION_FAILED;
  1293. }
  1294. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1295. codegen.opcodes.push_back(p_continue_addr);
  1296. } break;
  1297. case GDScriptParser::ControlFlowNode::CF_RETURN: {
  1298. int ret2;
  1299. if (cf->arguments.size()) {
  1300. ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false);
  1301. if (ret2 < 0) {
  1302. return ERR_PARSE_ERROR;
  1303. }
  1304. } else {
  1305. ret2 = GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS;
  1306. }
  1307. codegen.opcodes.push_back(GDScriptFunction::OPCODE_RETURN);
  1308. codegen.opcodes.push_back(ret2);
  1309. } break;
  1310. }
  1311. } break;
  1312. case GDScriptParser::Node::TYPE_ASSERT: {
  1313. #ifdef DEBUG_ENABLED
  1314. // try subblocks
  1315. const GDScriptParser::AssertNode *as = static_cast<const GDScriptParser::AssertNode *>(s);
  1316. int ret2 = _parse_expression(codegen, as->condition, p_stack_level, false);
  1317. if (ret2 < 0) {
  1318. return ERR_PARSE_ERROR;
  1319. }
  1320. int message_ret = 0;
  1321. if (as->message) {
  1322. message_ret = _parse_expression(codegen, as->message, p_stack_level + 1, false);
  1323. if (message_ret < 0) {
  1324. return ERR_PARSE_ERROR;
  1325. }
  1326. }
  1327. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSERT);
  1328. codegen.opcodes.push_back(ret2);
  1329. codegen.opcodes.push_back(message_ret);
  1330. #endif
  1331. } break;
  1332. case GDScriptParser::Node::TYPE_BREAKPOINT: {
  1333. #ifdef DEBUG_ENABLED
  1334. // try subblocks
  1335. codegen.opcodes.push_back(GDScriptFunction::OPCODE_BREAKPOINT);
  1336. #endif
  1337. } break;
  1338. case GDScriptParser::Node::TYPE_LOCAL_VAR: {
  1339. const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(s);
  1340. // since we are using properties now for most class access, allow shadowing of class members to make user's life easier.
  1341. //
  1342. //if (_is_class_member_property(codegen, lv->name)) {
  1343. // _set_error("Name for local variable '" + String(lv->name) + "' can't shadow class property of the same name.", lv);
  1344. // return ERR_ALREADY_EXISTS;
  1345. //}
  1346. codegen.add_stack_identifier(lv->name, p_stack_level++);
  1347. codegen.alloc_stack(p_stack_level);
  1348. new_identifiers++;
  1349. } break;
  1350. default: {
  1351. //expression
  1352. int ret2 = _parse_expression(codegen, s, p_stack_level, true);
  1353. if (ret2 < 0) {
  1354. return ERR_PARSE_ERROR;
  1355. }
  1356. } break;
  1357. }
  1358. }
  1359. codegen.pop_stack_identifiers();
  1360. return OK;
  1361. }
  1362. Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready) {
  1363. Vector<int> bytecode;
  1364. CodeGen codegen;
  1365. codegen.class_node = p_class;
  1366. codegen.script = p_script;
  1367. codegen.function_node = p_func;
  1368. codegen.stack_max = 0;
  1369. codegen.current_line = 0;
  1370. codegen.call_max = 0;
  1371. codegen.debug_stack = EngineDebugger::is_active();
  1372. Vector<StringName> argnames;
  1373. int stack_level = 0;
  1374. if (p_func) {
  1375. for (int i = 0; i < p_func->arguments.size(); i++) {
  1376. // since we are using properties now for most class access, allow shadowing of class members to make user's life easier.
  1377. //
  1378. //if (_is_class_member_property(p_script, p_func->arguments[i])) {
  1379. // _set_error("Name for argument '" + String(p_func->arguments[i]) + "' can't shadow class property of the same name.", p_func);
  1380. // return ERR_ALREADY_EXISTS;
  1381. //}
  1382. codegen.add_stack_identifier(p_func->arguments[i], i);
  1383. #ifdef TOOLS_ENABLED
  1384. argnames.push_back(p_func->arguments[i]);
  1385. #endif
  1386. }
  1387. stack_level = p_func->arguments.size();
  1388. }
  1389. codegen.alloc_stack(stack_level);
  1390. /* Parse initializer -if applies- */
  1391. bool is_initializer = !p_for_ready && !p_func;
  1392. if (is_initializer || (p_func && String(p_func->name) == "_init")) {
  1393. //parse initializer for class members
  1394. if (!p_func && p_class->extends_used && p_script->native.is_null()) {
  1395. //call implicit parent constructor
  1396. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_SELF_BASE);
  1397. codegen.opcodes.push_back(codegen.get_name_map_pos("_init"));
  1398. codegen.opcodes.push_back(0);
  1399. codegen.opcodes.push_back((GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) | 0);
  1400. }
  1401. Error err = _parse_block(codegen, p_class->initializer, stack_level);
  1402. if (err) {
  1403. return err;
  1404. }
  1405. is_initializer = true;
  1406. }
  1407. if (p_for_ready || (p_func && String(p_func->name) == "_ready")) {
  1408. //parse initializer for class members
  1409. if (p_class->ready->statements.size()) {
  1410. Error err = _parse_block(codegen, p_class->ready, stack_level);
  1411. if (err) {
  1412. return err;
  1413. }
  1414. }
  1415. }
  1416. /* Parse default argument code -if applies- */
  1417. Vector<int> defarg_addr;
  1418. StringName func_name;
  1419. if (p_func) {
  1420. if (p_func->default_values.size()) {
  1421. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  1422. defarg_addr.push_back(codegen.opcodes.size());
  1423. for (int i = 0; i < p_func->default_values.size(); i++) {
  1424. _parse_expression(codegen, p_func->default_values[i], stack_level, true);
  1425. defarg_addr.push_back(codegen.opcodes.size());
  1426. }
  1427. defarg_addr.invert();
  1428. }
  1429. Error err = _parse_block(codegen, p_func->body, stack_level);
  1430. if (err) {
  1431. return err;
  1432. }
  1433. func_name = p_func->name;
  1434. } else {
  1435. if (p_for_ready) {
  1436. func_name = "_ready";
  1437. } else {
  1438. func_name = "_init";
  1439. }
  1440. }
  1441. codegen.opcodes.push_back(GDScriptFunction::OPCODE_END);
  1442. /*
  1443. if (String(p_func->name)=="") { //initializer func
  1444. gdfunc = &p_script->initializer;
  1445. */
  1446. //} else { //regular func
  1447. p_script->member_functions[func_name] = memnew(GDScriptFunction);
  1448. GDScriptFunction *gdfunc = p_script->member_functions[func_name];
  1449. //}
  1450. if (p_func) {
  1451. gdfunc->_static = p_func->_static;
  1452. gdfunc->rpc_mode = p_func->rpc_mode;
  1453. gdfunc->argument_types.resize(p_func->argument_types.size());
  1454. for (int i = 0; i < p_func->argument_types.size(); i++) {
  1455. gdfunc->argument_types.write[i] = _gdtype_from_datatype(p_func->argument_types[i]);
  1456. }
  1457. gdfunc->return_type = _gdtype_from_datatype(p_func->return_type);
  1458. } else {
  1459. gdfunc->_static = false;
  1460. gdfunc->rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1461. gdfunc->return_type = GDScriptDataType();
  1462. gdfunc->return_type.has_type = true;
  1463. gdfunc->return_type.kind = GDScriptDataType::BUILTIN;
  1464. gdfunc->return_type.builtin_type = Variant::NIL;
  1465. }
  1466. #ifdef TOOLS_ENABLED
  1467. gdfunc->arg_names = argnames;
  1468. #endif
  1469. //constants
  1470. if (codegen.constant_map.size()) {
  1471. gdfunc->_constant_count = codegen.constant_map.size();
  1472. gdfunc->constants.resize(codegen.constant_map.size());
  1473. gdfunc->_constants_ptr = gdfunc->constants.ptrw();
  1474. const Variant *K = nullptr;
  1475. while ((K = codegen.constant_map.next(K))) {
  1476. int idx = codegen.constant_map[*K];
  1477. gdfunc->constants.write[idx] = *K;
  1478. }
  1479. } else {
  1480. gdfunc->_constants_ptr = nullptr;
  1481. gdfunc->_constant_count = 0;
  1482. }
  1483. //global names
  1484. if (codegen.name_map.size()) {
  1485. gdfunc->global_names.resize(codegen.name_map.size());
  1486. gdfunc->_global_names_ptr = &gdfunc->global_names[0];
  1487. for (Map<StringName, int>::Element *E = codegen.name_map.front(); E; E = E->next()) {
  1488. gdfunc->global_names.write[E->get()] = E->key();
  1489. }
  1490. gdfunc->_global_names_count = gdfunc->global_names.size();
  1491. } else {
  1492. gdfunc->_global_names_ptr = nullptr;
  1493. gdfunc->_global_names_count = 0;
  1494. }
  1495. #ifdef TOOLS_ENABLED
  1496. // Named globals
  1497. if (codegen.named_globals.size()) {
  1498. gdfunc->named_globals.resize(codegen.named_globals.size());
  1499. gdfunc->_named_globals_ptr = gdfunc->named_globals.ptr();
  1500. for (int i = 0; i < codegen.named_globals.size(); i++) {
  1501. gdfunc->named_globals.write[i] = codegen.named_globals[i];
  1502. }
  1503. gdfunc->_named_globals_count = gdfunc->named_globals.size();
  1504. }
  1505. #endif
  1506. if (codegen.opcodes.size()) {
  1507. gdfunc->code = codegen.opcodes;
  1508. gdfunc->_code_ptr = &gdfunc->code[0];
  1509. gdfunc->_code_size = codegen.opcodes.size();
  1510. } else {
  1511. gdfunc->_code_ptr = nullptr;
  1512. gdfunc->_code_size = 0;
  1513. }
  1514. if (defarg_addr.size()) {
  1515. gdfunc->default_arguments = defarg_addr;
  1516. gdfunc->_default_arg_count = defarg_addr.size() - 1;
  1517. gdfunc->_default_arg_ptr = &gdfunc->default_arguments[0];
  1518. } else {
  1519. gdfunc->_default_arg_count = 0;
  1520. gdfunc->_default_arg_ptr = nullptr;
  1521. }
  1522. gdfunc->_argument_count = p_func ? p_func->arguments.size() : 0;
  1523. gdfunc->_stack_size = codegen.stack_max;
  1524. gdfunc->_call_size = codegen.call_max;
  1525. gdfunc->name = func_name;
  1526. #ifdef DEBUG_ENABLED
  1527. if (EngineDebugger::is_active()) {
  1528. String signature;
  1529. //path
  1530. if (p_script->get_path() != String()) {
  1531. signature += p_script->get_path();
  1532. }
  1533. //loc
  1534. if (p_func) {
  1535. signature += "::" + itos(p_func->body->line);
  1536. } else {
  1537. signature += "::0";
  1538. }
  1539. //function and class
  1540. if (p_class->name) {
  1541. signature += "::" + String(p_class->name) + "." + String(func_name);
  1542. } else {
  1543. signature += "::" + String(func_name);
  1544. }
  1545. gdfunc->profile.signature = signature;
  1546. }
  1547. #endif
  1548. gdfunc->_script = p_script;
  1549. gdfunc->source = source;
  1550. #ifdef DEBUG_ENABLED
  1551. {
  1552. gdfunc->func_cname = (String(source) + " - " + String(func_name)).utf8();
  1553. gdfunc->_func_cname = gdfunc->func_cname.get_data();
  1554. }
  1555. #endif
  1556. if (p_func) {
  1557. gdfunc->_initial_line = p_func->line;
  1558. #ifdef TOOLS_ENABLED
  1559. p_script->member_lines[func_name] = p_func->line;
  1560. #endif
  1561. } else {
  1562. gdfunc->_initial_line = 0;
  1563. }
  1564. if (codegen.debug_stack) {
  1565. gdfunc->stack_debug = codegen.stack_debug;
  1566. }
  1567. if (is_initializer) {
  1568. p_script->initializer = gdfunc;
  1569. }
  1570. return OK;
  1571. }
  1572. Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  1573. parsing_classes.insert(p_script);
  1574. if (p_class->owner && p_class->owner->owner) {
  1575. // Owner is not root
  1576. if (!parsed_classes.has(p_script->_owner)) {
  1577. if (parsing_classes.has(p_script->_owner)) {
  1578. _set_error("Cyclic class reference for '" + String(p_class->name) + "'.", p_class);
  1579. return ERR_PARSE_ERROR;
  1580. }
  1581. Error err = _parse_class_level(p_script->_owner, p_class->owner, p_keep_state);
  1582. if (err) {
  1583. return err;
  1584. }
  1585. }
  1586. }
  1587. p_script->native = Ref<GDScriptNativeClass>();
  1588. p_script->base = Ref<GDScript>();
  1589. p_script->_base = nullptr;
  1590. p_script->members.clear();
  1591. p_script->constants.clear();
  1592. for (Map<StringName, GDScriptFunction *>::Element *E = p_script->member_functions.front(); E; E = E->next()) {
  1593. memdelete(E->get());
  1594. }
  1595. p_script->member_functions.clear();
  1596. p_script->member_indices.clear();
  1597. p_script->member_info.clear();
  1598. p_script->_signals.clear();
  1599. p_script->initializer = nullptr;
  1600. p_script->tool = p_class->tool;
  1601. p_script->name = p_class->name;
  1602. Ref<GDScriptNativeClass> native;
  1603. GDScriptDataType base_type = _gdtype_from_datatype(p_class->base_type);
  1604. // Inheritance
  1605. switch (base_type.kind) {
  1606. case GDScriptDataType::NATIVE: {
  1607. int native_idx = GDScriptLanguage::get_singleton()->get_global_map()[base_type.native_type];
  1608. native = GDScriptLanguage::get_singleton()->get_global_array()[native_idx];
  1609. ERR_FAIL_COND_V(native.is_null(), ERR_BUG);
  1610. p_script->native = native;
  1611. } break;
  1612. case GDScriptDataType::GDSCRIPT: {
  1613. Ref<GDScript> base = base_type.script_type;
  1614. p_script->base = base;
  1615. p_script->_base = base.ptr();
  1616. p_script->member_indices = base->member_indices;
  1617. if (p_class->base_type.kind == GDScriptParser::DataType::CLASS) {
  1618. if (!parsed_classes.has(p_script->_base)) {
  1619. if (parsing_classes.has(p_script->_base)) {
  1620. _set_error("Cyclic class reference for '" + String(p_class->name) + "'.", p_class);
  1621. return ERR_PARSE_ERROR;
  1622. }
  1623. Error err = _parse_class_level(p_script->_base, p_class->base_type.class_type, p_keep_state);
  1624. if (err) {
  1625. return err;
  1626. }
  1627. }
  1628. }
  1629. } break;
  1630. default: {
  1631. _set_error("Parser bug: invalid inheritance.", p_class);
  1632. return ERR_BUG;
  1633. } break;
  1634. }
  1635. for (int i = 0; i < p_class->variables.size(); i++) {
  1636. StringName name = p_class->variables[i].identifier;
  1637. GDScript::MemberInfo minfo;
  1638. minfo.index = p_script->member_indices.size();
  1639. minfo.setter = p_class->variables[i].setter;
  1640. minfo.getter = p_class->variables[i].getter;
  1641. minfo.rpc_mode = p_class->variables[i].rpc_mode;
  1642. minfo.data_type = _gdtype_from_datatype(p_class->variables[i].data_type);
  1643. PropertyInfo prop_info = minfo.data_type;
  1644. prop_info.name = name;
  1645. PropertyInfo export_info = p_class->variables[i]._export;
  1646. if (export_info.type != Variant::NIL) {
  1647. if (!minfo.data_type.has_type) {
  1648. prop_info.type = export_info.type;
  1649. prop_info.class_name = export_info.class_name;
  1650. }
  1651. prop_info.hint = export_info.hint;
  1652. prop_info.hint_string = export_info.hint_string;
  1653. prop_info.usage = export_info.usage;
  1654. #ifdef TOOLS_ENABLED
  1655. if (p_class->variables[i].default_value.get_type() != Variant::NIL) {
  1656. p_script->member_default_values[name] = p_class->variables[i].default_value;
  1657. }
  1658. #endif
  1659. } else {
  1660. prop_info.usage = PROPERTY_USAGE_SCRIPT_VARIABLE;
  1661. }
  1662. p_script->member_info[name] = prop_info;
  1663. p_script->member_indices[name] = minfo;
  1664. p_script->members.insert(name);
  1665. #ifdef TOOLS_ENABLED
  1666. p_script->member_lines[name] = p_class->variables[i].line;
  1667. #endif
  1668. }
  1669. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
  1670. StringName name = E->key();
  1671. ERR_CONTINUE(E->get().expression->type != GDScriptParser::Node::TYPE_CONSTANT);
  1672. GDScriptParser::ConstantNode *constant = static_cast<GDScriptParser::ConstantNode *>(E->get().expression);
  1673. p_script->constants.insert(name, constant->value);
  1674. #ifdef TOOLS_ENABLED
  1675. p_script->member_lines[name] = E->get().expression->line;
  1676. #endif
  1677. }
  1678. for (int i = 0; i < p_class->_signals.size(); i++) {
  1679. StringName name = p_class->_signals[i].name;
  1680. GDScript *c = p_script;
  1681. while (c) {
  1682. if (c->_signals.has(name)) {
  1683. _set_error("Signal '" + name + "' redefined (in current or parent class)", p_class);
  1684. return ERR_ALREADY_EXISTS;
  1685. }
  1686. if (c->base.is_valid()) {
  1687. c = c->base.ptr();
  1688. } else {
  1689. c = nullptr;
  1690. }
  1691. }
  1692. if (native.is_valid()) {
  1693. if (ClassDB::has_signal(native->get_name(), name)) {
  1694. _set_error("Signal '" + name + "' redefined (original in native class '" + String(native->get_name()) + "')", p_class);
  1695. return ERR_ALREADY_EXISTS;
  1696. }
  1697. }
  1698. p_script->_signals[name] = p_class->_signals[i].arguments;
  1699. }
  1700. parsed_classes.insert(p_script);
  1701. parsing_classes.erase(p_script);
  1702. //parse sub-classes
  1703. for (int i = 0; i < p_class->subclasses.size(); i++) {
  1704. StringName name = p_class->subclasses[i]->name;
  1705. Ref<GDScript> &subclass = p_script->subclasses[name];
  1706. GDScript *subclass_ptr = subclass.ptr();
  1707. // Subclass might still be parsing, just skip it
  1708. if (!parsed_classes.has(subclass_ptr) && !parsing_classes.has(subclass_ptr)) {
  1709. Error err = _parse_class_level(subclass_ptr, p_class->subclasses[i], p_keep_state);
  1710. if (err) {
  1711. return err;
  1712. }
  1713. }
  1714. #ifdef TOOLS_ENABLED
  1715. p_script->member_lines[name] = p_class->subclasses[i]->line;
  1716. #endif
  1717. p_script->constants.insert(name, subclass); //once parsed, goes to the list of constants
  1718. }
  1719. return OK;
  1720. }
  1721. Error GDScriptCompiler::_parse_class_blocks(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  1722. //parse methods
  1723. bool has_initializer = false;
  1724. bool has_ready = false;
  1725. for (int i = 0; i < p_class->functions.size(); i++) {
  1726. if (!has_initializer && p_class->functions[i]->name == "_init") {
  1727. has_initializer = true;
  1728. }
  1729. if (!has_ready && p_class->functions[i]->name == "_ready") {
  1730. has_ready = true;
  1731. }
  1732. Error err = _parse_function(p_script, p_class, p_class->functions[i]);
  1733. if (err) {
  1734. return err;
  1735. }
  1736. }
  1737. //parse static methods
  1738. for (int i = 0; i < p_class->static_functions.size(); i++) {
  1739. Error err = _parse_function(p_script, p_class, p_class->static_functions[i]);
  1740. if (err) {
  1741. return err;
  1742. }
  1743. }
  1744. if (!has_initializer) {
  1745. //create a constructor
  1746. Error err = _parse_function(p_script, p_class, nullptr);
  1747. if (err) {
  1748. return err;
  1749. }
  1750. }
  1751. if (!has_ready && p_class->ready->statements.size()) {
  1752. //create a constructor
  1753. Error err = _parse_function(p_script, p_class, nullptr, true);
  1754. if (err) {
  1755. return err;
  1756. }
  1757. }
  1758. #ifdef DEBUG_ENABLED
  1759. //validate instances if keeping state
  1760. if (p_keep_state) {
  1761. for (Set<Object *>::Element *E = p_script->instances.front(); E;) {
  1762. Set<Object *>::Element *N = E->next();
  1763. ScriptInstance *si = E->get()->get_script_instance();
  1764. if (si->is_placeholder()) {
  1765. #ifdef TOOLS_ENABLED
  1766. PlaceHolderScriptInstance *psi = static_cast<PlaceHolderScriptInstance *>(si);
  1767. if (p_script->is_tool()) {
  1768. //re-create as an instance
  1769. p_script->placeholders.erase(psi); //remove placeholder
  1770. GDScriptInstance *instance = memnew(GDScriptInstance);
  1771. instance->base_ref = Object::cast_to<Reference>(E->get());
  1772. instance->members.resize(p_script->member_indices.size());
  1773. instance->script = Ref<GDScript>(p_script);
  1774. instance->owner = E->get();
  1775. //needed for hot reloading
  1776. for (Map<StringName, GDScript::MemberInfo>::Element *F = p_script->member_indices.front(); F; F = F->next()) {
  1777. instance->member_indices_cache[F->key()] = F->get().index;
  1778. }
  1779. instance->owner->set_script_instance(instance);
  1780. /* STEP 2, INITIALIZE AND CONSTRUCT */
  1781. Callable::CallError ce;
  1782. p_script->initializer->call(instance, nullptr, 0, ce);
  1783. if (ce.error != Callable::CallError::CALL_OK) {
  1784. //well, tough luck, not goinna do anything here
  1785. }
  1786. }
  1787. #endif
  1788. } else {
  1789. GDScriptInstance *gi = static_cast<GDScriptInstance *>(si);
  1790. gi->reload_members();
  1791. }
  1792. E = N;
  1793. }
  1794. }
  1795. #endif
  1796. for (int i = 0; i < p_class->subclasses.size(); i++) {
  1797. StringName name = p_class->subclasses[i]->name;
  1798. GDScript *subclass = p_script->subclasses[name].ptr();
  1799. Error err = _parse_class_blocks(subclass, p_class->subclasses[i], p_keep_state);
  1800. if (err) {
  1801. return err;
  1802. }
  1803. }
  1804. p_script->valid = true;
  1805. return OK;
  1806. }
  1807. void GDScriptCompiler::_make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  1808. Map<StringName, Ref<GDScript>> old_subclasses;
  1809. if (p_keep_state) {
  1810. old_subclasses = p_script->subclasses;
  1811. }
  1812. p_script->subclasses.clear();
  1813. for (int i = 0; i < p_class->subclasses.size(); i++) {
  1814. StringName name = p_class->subclasses[i]->name;
  1815. Ref<GDScript> subclass;
  1816. String fully_qualified_name = p_script->fully_qualified_name + "::" + name;
  1817. if (old_subclasses.has(name)) {
  1818. subclass = old_subclasses[name];
  1819. } else {
  1820. Ref<GDScript> orphan_subclass = GDScriptLanguage::get_singleton()->get_orphan_subclass(fully_qualified_name);
  1821. if (orphan_subclass.is_valid()) {
  1822. subclass = orphan_subclass;
  1823. } else {
  1824. subclass.instance();
  1825. }
  1826. }
  1827. subclass->_owner = p_script;
  1828. subclass->fully_qualified_name = fully_qualified_name;
  1829. p_script->subclasses.insert(name, subclass);
  1830. _make_scripts(subclass.ptr(), p_class->subclasses[i], false);
  1831. }
  1832. }
  1833. Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state) {
  1834. err_line = -1;
  1835. err_column = -1;
  1836. error = "";
  1837. parser = p_parser;
  1838. main_script = p_script;
  1839. const GDScriptParser::Node *root = parser->get_parse_tree();
  1840. ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, ERR_INVALID_DATA);
  1841. source = p_script->get_path();
  1842. // The best fully qualified name for a base level script is its file path
  1843. p_script->fully_qualified_name = p_script->path;
  1844. // Create scripts for subclasses beforehand so they can be referenced
  1845. _make_scripts(p_script, static_cast<const GDScriptParser::ClassNode *>(root), p_keep_state);
  1846. p_script->_owner = nullptr;
  1847. Error err = _parse_class_level(p_script, static_cast<const GDScriptParser::ClassNode *>(root), p_keep_state);
  1848. if (err) {
  1849. return err;
  1850. }
  1851. err = _parse_class_blocks(p_script, static_cast<const GDScriptParser::ClassNode *>(root), p_keep_state);
  1852. if (err) {
  1853. return err;
  1854. }
  1855. return OK;
  1856. }
  1857. String GDScriptCompiler::get_error() const {
  1858. return error;
  1859. }
  1860. int GDScriptCompiler::get_error_line() const {
  1861. return err_line;
  1862. }
  1863. int GDScriptCompiler::get_error_column() const {
  1864. return err_column;
  1865. }
  1866. GDScriptCompiler::GDScriptCompiler() {
  1867. }