gdscript_compiler.cpp 75 KB

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