gdscript_compiler.cpp 75 KB

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