gdscript_compiler.cpp 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197
  1. /*************************************************************************/
  2. /* gdscript_compiler.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_compiler.h"
  31. #include "gdscript.h"
  32. bool GDScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringName &p_name) {
  33. if (codegen.function_node && codegen.function_node->_static)
  34. return false;
  35. 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 = nullptr;
  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, int p_index_addr) {
  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, p_index_addr);
  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, int p_index_addr) {
  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, p_index_addr))
  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, int p_index_addr) {
  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 = nullptr;
  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 (p_index_addr != 0) {
  552. index = p_index_addr;
  553. } else if (named) {
  554. if (on->arguments[0]->type == GDScriptParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) {
  555. GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(on->arguments[1]);
  556. const Map<StringName, GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(identifier->name);
  557. #ifdef DEBUG_ENABLED
  558. if (MI && MI->get().getter == codegen.function_node->name) {
  559. String n = static_cast<GDScriptParser::IdentifierNode *>(on->arguments[1])->name;
  560. _set_error("Must use '" + n + "' instead of 'self." + n + "' in getter.", on);
  561. return -1;
  562. }
  563. #endif
  564. if (MI && MI->get().getter == "") {
  565. // Faster than indexing self (as if no self. had been used)
  566. return (MI->get().index) | (GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS);
  567. }
  568. }
  569. index = codegen.get_name_map_pos(static_cast<GDScriptParser::IdentifierNode *>(on->arguments[1])->name);
  570. } else {
  571. if (on->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT && static_cast<const GDScriptParser::ConstantNode *>(on->arguments[1])->value.get_type() == Variant::STRING) {
  572. //also, somehow, named (speed up anyway)
  573. StringName name = static_cast<const GDScriptParser::ConstantNode *>(on->arguments[1])->value;
  574. index = codegen.get_name_map_pos(name);
  575. named = true;
  576. } else {
  577. //regular indexing
  578. if (from & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  579. slevel++;
  580. codegen.alloc_stack(slevel);
  581. }
  582. index = _parse_expression(codegen, on->arguments[1], slevel);
  583. if (index < 0)
  584. return index;
  585. }
  586. }
  587. codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_GET_NAMED : GDScriptFunction::OPCODE_GET); // perform operator
  588. codegen.opcodes.push_back(from); // argument 1
  589. codegen.opcodes.push_back(index); // argument 2 (unary only takes one parameter)
  590. } break;
  591. case GDScriptParser::OperatorNode::OP_AND: {
  592. // AND operator with early out on failure
  593. int res = _parse_expression(codegen, on->arguments[0], p_stack_level);
  594. if (res < 0)
  595. return res;
  596. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  597. codegen.opcodes.push_back(res);
  598. int jump_fail_pos = codegen.opcodes.size();
  599. codegen.opcodes.push_back(0);
  600. res = _parse_expression(codegen, on->arguments[1], p_stack_level);
  601. if (res < 0)
  602. return res;
  603. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  604. codegen.opcodes.push_back(res);
  605. int jump_fail_pos2 = codegen.opcodes.size();
  606. codegen.opcodes.push_back(0);
  607. codegen.alloc_stack(p_stack_level); //it will be used..
  608. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  609. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  610. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  611. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  612. codegen.opcodes.write[jump_fail_pos] = codegen.opcodes.size();
  613. codegen.opcodes.write[jump_fail_pos2] = codegen.opcodes.size();
  614. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  615. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  616. return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  617. } break;
  618. case GDScriptParser::OperatorNode::OP_OR: {
  619. // OR operator with early out on success
  620. int res = _parse_expression(codegen, on->arguments[0], p_stack_level);
  621. if (res < 0)
  622. return res;
  623. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF);
  624. codegen.opcodes.push_back(res);
  625. int jump_success_pos = codegen.opcodes.size();
  626. codegen.opcodes.push_back(0);
  627. res = _parse_expression(codegen, on->arguments[1], p_stack_level);
  628. if (res < 0)
  629. return res;
  630. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF);
  631. codegen.opcodes.push_back(res);
  632. int jump_success_pos2 = codegen.opcodes.size();
  633. codegen.opcodes.push_back(0);
  634. codegen.alloc_stack(p_stack_level); //it will be used..
  635. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  636. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  637. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  638. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  639. codegen.opcodes.write[jump_success_pos] = codegen.opcodes.size();
  640. codegen.opcodes.write[jump_success_pos2] = codegen.opcodes.size();
  641. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  642. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  643. return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  644. } break;
  645. // ternary operators
  646. case GDScriptParser::OperatorNode::OP_TERNARY_IF: {
  647. // x IF a ELSE y operator with early out on failure
  648. int res = _parse_expression(codegen, on->arguments[0], p_stack_level);
  649. if (res < 0)
  650. return res;
  651. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  652. codegen.opcodes.push_back(res);
  653. int jump_fail_pos = codegen.opcodes.size();
  654. codegen.opcodes.push_back(0);
  655. res = _parse_expression(codegen, on->arguments[1], p_stack_level);
  656. if (res < 0)
  657. return res;
  658. codegen.alloc_stack(p_stack_level); //it will be used..
  659. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  660. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  661. codegen.opcodes.push_back(res);
  662. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  663. int jump_past_pos = codegen.opcodes.size();
  664. codegen.opcodes.push_back(0);
  665. codegen.opcodes.write[jump_fail_pos] = codegen.opcodes.size();
  666. res = _parse_expression(codegen, on->arguments[2], p_stack_level);
  667. if (res < 0)
  668. return res;
  669. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  670. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  671. codegen.opcodes.push_back(res);
  672. codegen.opcodes.write[jump_past_pos] = codegen.opcodes.size();
  673. return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  674. } break;
  675. //unary operators
  676. case GDScriptParser::OperatorNode::OP_NEG: {
  677. if (!_create_unary_operator(codegen, on, Variant::OP_NEGATE, p_stack_level)) return -1;
  678. } break;
  679. case GDScriptParser::OperatorNode::OP_POS: {
  680. if (!_create_unary_operator(codegen, on, Variant::OP_POSITIVE, p_stack_level)) return -1;
  681. } break;
  682. case GDScriptParser::OperatorNode::OP_NOT: {
  683. if (!_create_unary_operator(codegen, on, Variant::OP_NOT, p_stack_level)) return -1;
  684. } break;
  685. case GDScriptParser::OperatorNode::OP_BIT_INVERT: {
  686. if (!_create_unary_operator(codegen, on, Variant::OP_BIT_NEGATE, p_stack_level)) return -1;
  687. } break;
  688. //binary operators (in precedence order)
  689. case GDScriptParser::OperatorNode::OP_IN: {
  690. if (!_create_binary_operator(codegen, on, Variant::OP_IN, p_stack_level)) return -1;
  691. } break;
  692. case GDScriptParser::OperatorNode::OP_EQUAL: {
  693. if (!_create_binary_operator(codegen, on, Variant::OP_EQUAL, p_stack_level)) return -1;
  694. } break;
  695. case GDScriptParser::OperatorNode::OP_NOT_EQUAL: {
  696. if (!_create_binary_operator(codegen, on, Variant::OP_NOT_EQUAL, p_stack_level)) return -1;
  697. } break;
  698. case GDScriptParser::OperatorNode::OP_LESS: {
  699. if (!_create_binary_operator(codegen, on, Variant::OP_LESS, p_stack_level)) return -1;
  700. } break;
  701. case GDScriptParser::OperatorNode::OP_LESS_EQUAL: {
  702. if (!_create_binary_operator(codegen, on, Variant::OP_LESS_EQUAL, p_stack_level)) return -1;
  703. } break;
  704. case GDScriptParser::OperatorNode::OP_GREATER: {
  705. if (!_create_binary_operator(codegen, on, Variant::OP_GREATER, p_stack_level)) return -1;
  706. } break;
  707. case GDScriptParser::OperatorNode::OP_GREATER_EQUAL: {
  708. if (!_create_binary_operator(codegen, on, Variant::OP_GREATER_EQUAL, p_stack_level)) return -1;
  709. } break;
  710. case GDScriptParser::OperatorNode::OP_ADD: {
  711. if (!_create_binary_operator(codegen, on, Variant::OP_ADD, p_stack_level)) return -1;
  712. } break;
  713. case GDScriptParser::OperatorNode::OP_SUB: {
  714. if (!_create_binary_operator(codegen, on, Variant::OP_SUBTRACT, p_stack_level)) return -1;
  715. } break;
  716. case GDScriptParser::OperatorNode::OP_MUL: {
  717. if (!_create_binary_operator(codegen, on, Variant::OP_MULTIPLY, p_stack_level)) return -1;
  718. } break;
  719. case GDScriptParser::OperatorNode::OP_DIV: {
  720. if (!_create_binary_operator(codegen, on, Variant::OP_DIVIDE, p_stack_level)) return -1;
  721. } break;
  722. case GDScriptParser::OperatorNode::OP_MOD: {
  723. if (!_create_binary_operator(codegen, on, Variant::OP_MODULE, p_stack_level)) return -1;
  724. } break;
  725. //case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
  726. //case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
  727. case GDScriptParser::OperatorNode::OP_BIT_AND: {
  728. if (!_create_binary_operator(codegen, on, Variant::OP_BIT_AND, p_stack_level)) return -1;
  729. } break;
  730. case GDScriptParser::OperatorNode::OP_BIT_OR: {
  731. if (!_create_binary_operator(codegen, on, Variant::OP_BIT_OR, p_stack_level)) return -1;
  732. } break;
  733. case GDScriptParser::OperatorNode::OP_BIT_XOR: {
  734. if (!_create_binary_operator(codegen, on, Variant::OP_BIT_XOR, p_stack_level)) return -1;
  735. } break;
  736. //shift
  737. case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: {
  738. if (!_create_binary_operator(codegen, on, Variant::OP_SHIFT_LEFT, p_stack_level)) return -1;
  739. } break;
  740. case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: {
  741. if (!_create_binary_operator(codegen, on, Variant::OP_SHIFT_RIGHT, p_stack_level)) return -1;
  742. } break;
  743. //assignment operators
  744. case GDScriptParser::OperatorNode::OP_ASSIGN_ADD:
  745. case GDScriptParser::OperatorNode::OP_ASSIGN_SUB:
  746. case GDScriptParser::OperatorNode::OP_ASSIGN_MUL:
  747. case GDScriptParser::OperatorNode::OP_ASSIGN_DIV:
  748. case GDScriptParser::OperatorNode::OP_ASSIGN_MOD:
  749. case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  750. case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  751. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_AND:
  752. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_OR:
  753. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR:
  754. case GDScriptParser::OperatorNode::OP_INIT_ASSIGN:
  755. case GDScriptParser::OperatorNode::OP_ASSIGN: {
  756. ERR_FAIL_COND_V(on->arguments.size() != 2, -1);
  757. 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)) {
  758. // SET (chained) MODE!
  759. #ifdef DEBUG_ENABLED
  760. if (static_cast<GDScriptParser::OperatorNode *>(on->arguments[0])->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  761. const GDScriptParser::OperatorNode *inon = static_cast<GDScriptParser::OperatorNode *>(on->arguments[0]);
  762. if (inon->arguments[0]->type == GDScriptParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) {
  763. const Map<StringName, GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(static_cast<GDScriptParser::IdentifierNode *>(inon->arguments[1])->name);
  764. if (MI && MI->get().setter == codegen.function_node->name) {
  765. String n = static_cast<GDScriptParser::IdentifierNode *>(inon->arguments[1])->name;
  766. _set_error("Must use '" + n + "' instead of 'self." + n + "' in setter.", inon);
  767. return -1;
  768. }
  769. }
  770. }
  771. #endif
  772. int slevel = p_stack_level;
  773. GDScriptParser::OperatorNode *op = static_cast<GDScriptParser::OperatorNode *>(on->arguments[0]);
  774. /* Find chain of sets */
  775. StringName assign_property;
  776. List<GDScriptParser::OperatorNode *> chain;
  777. {
  778. //create get/set chain
  779. GDScriptParser::OperatorNode *n = op;
  780. while (true) {
  781. chain.push_back(n);
  782. if (n->arguments[0]->type != GDScriptParser::Node::TYPE_OPERATOR) {
  783. //check for a built-in property
  784. if (n->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  785. GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(n->arguments[0]);
  786. if (_is_class_member_property(codegen, identifier->name)) {
  787. assign_property = identifier->name;
  788. }
  789. }
  790. break;
  791. }
  792. n = static_cast<GDScriptParser::OperatorNode *>(n->arguments[0]);
  793. if (n->op != GDScriptParser::OperatorNode::OP_INDEX && n->op != GDScriptParser::OperatorNode::OP_INDEX_NAMED)
  794. break;
  795. }
  796. }
  797. /* Chain of gets */
  798. //get at (potential) root stack pos, so it can be returned
  799. int prev_pos = _parse_expression(codegen, chain.back()->get()->arguments[0], slevel);
  800. if (prev_pos < 0)
  801. return prev_pos;
  802. int retval = prev_pos;
  803. if (retval & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  804. slevel++;
  805. codegen.alloc_stack(slevel);
  806. }
  807. Vector<int> setchain;
  808. if (assign_property != StringName()) {
  809. // recover and assign at the end, this allows stuff like
  810. // position.x+=2.0
  811. // in Node2D
  812. setchain.push_back(prev_pos);
  813. setchain.push_back(codegen.get_name_map_pos(assign_property));
  814. setchain.push_back(GDScriptFunction::OPCODE_SET_MEMBER);
  815. }
  816. for (List<GDScriptParser::OperatorNode *>::Element *E = chain.back(); E; E = E->prev()) {
  817. if (E == chain.front()) //ignore first
  818. break;
  819. bool named = E->get()->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED;
  820. int key_idx;
  821. if (named) {
  822. key_idx = codegen.get_name_map_pos(static_cast<const GDScriptParser::IdentifierNode *>(E->get()->arguments[1])->name);
  823. //printf("named key %x\n",key_idx);
  824. } else {
  825. if (prev_pos & (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS)) {
  826. slevel++;
  827. codegen.alloc_stack(slevel);
  828. }
  829. GDScriptParser::Node *key = E->get()->arguments[1];
  830. key_idx = _parse_expression(codegen, key, slevel);
  831. //printf("expr key %x\n",key_idx);
  832. //stack was raised here if retval was stack but..
  833. }
  834. if (key_idx < 0) //error
  835. return key_idx;
  836. codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_GET_NAMED : GDScriptFunction::OPCODE_GET);
  837. codegen.opcodes.push_back(prev_pos);
  838. codegen.opcodes.push_back(key_idx);
  839. slevel++;
  840. codegen.alloc_stack(slevel);
  841. int dst_pos = (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) | slevel;
  842. codegen.opcodes.push_back(dst_pos);
  843. //add in reverse order, since it will be reverted
  844. setchain.push_back(dst_pos);
  845. setchain.push_back(key_idx);
  846. setchain.push_back(prev_pos);
  847. setchain.push_back(named ? GDScriptFunction::OPCODE_SET_NAMED : GDScriptFunction::OPCODE_SET);
  848. prev_pos = dst_pos;
  849. }
  850. setchain.invert();
  851. int set_index;
  852. bool named = false;
  853. if (op->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  854. set_index = codegen.get_name_map_pos(static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name);
  855. named = true;
  856. } else {
  857. set_index = _parse_expression(codegen, op->arguments[1], slevel + 1);
  858. named = false;
  859. }
  860. if (set_index < 0) //error
  861. return set_index;
  862. if (set_index & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  863. slevel++;
  864. codegen.alloc_stack(slevel);
  865. }
  866. int set_value = _parse_assign_right_expression(codegen, on, slevel + 1, named ? 0 : set_index);
  867. if (set_value < 0) //error
  868. return set_value;
  869. codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_SET_NAMED : GDScriptFunction::OPCODE_SET);
  870. codegen.opcodes.push_back(prev_pos);
  871. codegen.opcodes.push_back(set_index);
  872. codegen.opcodes.push_back(set_value);
  873. for (int i = 0; i < setchain.size(); i++) {
  874. codegen.opcodes.push_back(setchain[i]);
  875. }
  876. return retval;
  877. } else if (on->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && _is_class_member_property(codegen, static_cast<GDScriptParser::IdentifierNode *>(on->arguments[0])->name)) {
  878. //assignment to member property
  879. int slevel = p_stack_level;
  880. int src_address = _parse_assign_right_expression(codegen, on, slevel);
  881. if (src_address < 0)
  882. return -1;
  883. StringName name = static_cast<GDScriptParser::IdentifierNode *>(on->arguments[0])->name;
  884. codegen.opcodes.push_back(GDScriptFunction::OPCODE_SET_MEMBER);
  885. codegen.opcodes.push_back(codegen.get_name_map_pos(name));
  886. codegen.opcodes.push_back(src_address);
  887. return GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS;
  888. } else {
  889. //REGULAR ASSIGNMENT MODE!!
  890. int slevel = p_stack_level;
  891. int dst_address_a = _parse_expression(codegen, on->arguments[0], slevel, false, on->op == GDScriptParser::OperatorNode::OP_INIT_ASSIGN);
  892. if (dst_address_a < 0)
  893. return -1;
  894. if (dst_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  895. slevel++;
  896. codegen.alloc_stack(slevel);
  897. }
  898. int src_address_b = _parse_assign_right_expression(codegen, on, slevel);
  899. if (src_address_b < 0)
  900. return -1;
  901. GDScriptDataType assign_type = _gdtype_from_datatype(on->arguments[0]->get_datatype());
  902. if (assign_type.has_type && !on->datatype.has_type) {
  903. // Typed assignment
  904. switch (assign_type.kind) {
  905. case GDScriptDataType::BUILTIN: {
  906. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN); // perform operator
  907. codegen.opcodes.push_back(assign_type.builtin_type); // variable type
  908. codegen.opcodes.push_back(dst_address_a); // argument 1
  909. codegen.opcodes.push_back(src_address_b); // argument 2
  910. } break;
  911. case GDScriptDataType::NATIVE: {
  912. int class_idx;
  913. if (GDScriptLanguage::get_singleton()->get_global_map().has(assign_type.native_type)) {
  914. class_idx = GDScriptLanguage::get_singleton()->get_global_map()[assign_type.native_type];
  915. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root)
  916. } else {
  917. _set_error("Invalid native class type '" + String(assign_type.native_type) + "'.", on->arguments[0]);
  918. return -1;
  919. }
  920. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE); // perform operator
  921. codegen.opcodes.push_back(class_idx); // variable type
  922. codegen.opcodes.push_back(dst_address_a); // argument 1
  923. codegen.opcodes.push_back(src_address_b); // argument 2
  924. } break;
  925. case GDScriptDataType::SCRIPT:
  926. case GDScriptDataType::GDSCRIPT: {
  927. Variant script = assign_type.script_type;
  928. int idx = codegen.get_constant_pos(script);
  929. idx |= GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS; //make it a local constant (faster access)
  930. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT); // perform operator
  931. codegen.opcodes.push_back(idx); // variable type
  932. codegen.opcodes.push_back(dst_address_a); // argument 1
  933. codegen.opcodes.push_back(src_address_b); // argument 2
  934. } break;
  935. default: {
  936. ERR_PRINT("Compiler bug: unresolved assign.");
  937. // Shouldn't get here, but fail-safe to a regular assignment
  938. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); // perform operator
  939. codegen.opcodes.push_back(dst_address_a); // argument 1
  940. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  941. }
  942. }
  943. } else {
  944. // Either untyped assignment or already type-checked by the parser
  945. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); // perform operator
  946. codegen.opcodes.push_back(dst_address_a); // argument 1
  947. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  948. }
  949. return dst_address_a; //if anything, returns wathever was assigned or correct stack position
  950. }
  951. } break;
  952. case GDScriptParser::OperatorNode::OP_IS: {
  953. ERR_FAIL_COND_V(on->arguments.size() != 2, false);
  954. int slevel = p_stack_level;
  955. int src_address_a = _parse_expression(codegen, on->arguments[0], slevel);
  956. if (src_address_a < 0)
  957. return -1;
  958. if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS)
  959. slevel++; //uses stack for return, increase stack
  960. int src_address_b = _parse_expression(codegen, on->arguments[1], slevel);
  961. if (src_address_b < 0)
  962. return -1;
  963. codegen.opcodes.push_back(GDScriptFunction::OPCODE_EXTENDS_TEST); // perform operator
  964. codegen.opcodes.push_back(src_address_a); // argument 1
  965. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  966. } break;
  967. case GDScriptParser::OperatorNode::OP_IS_BUILTIN: {
  968. ERR_FAIL_COND_V(on->arguments.size() != 2, false);
  969. ERR_FAIL_COND_V(on->arguments[1]->type != GDScriptParser::Node::TYPE_TYPE, false);
  970. int slevel = p_stack_level;
  971. int src_address_a = _parse_expression(codegen, on->arguments[0], slevel);
  972. if (src_address_a < 0)
  973. return -1;
  974. if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS)
  975. slevel++; //uses stack for return, increase stack
  976. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(on->arguments[1]);
  977. codegen.opcodes.push_back(GDScriptFunction::OPCODE_IS_BUILTIN); // perform operator
  978. codegen.opcodes.push_back(src_address_a); // argument 1
  979. codegen.opcodes.push_back((int)tn->vtype); // argument 2 (unary only takes one parameter)
  980. } break;
  981. default: {
  982. ERR_FAIL_V_MSG(0, "Bug in bytecode compiler, unexpected operator #" + itos(on->op) + " in parse tree while parsing expression."); //unreachable code
  983. } break;
  984. }
  985. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  986. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  987. codegen.alloc_stack(p_stack_level);
  988. return dst_addr;
  989. } break;
  990. //TYPE_TYPE,
  991. default: {
  992. ERR_FAIL_V_MSG(-1, "Bug in bytecode compiler, unexpected node in parse tree while parsing expression."); //unreachable code
  993. } break;
  994. }
  995. }
  996. Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::BlockNode *p_block, int p_stack_level, int p_break_addr, int p_continue_addr) {
  997. codegen.push_stack_identifiers();
  998. int new_identifiers = 0;
  999. codegen.current_line = p_block->line;
  1000. for (int i = 0; i < p_block->statements.size(); i++) {
  1001. const GDScriptParser::Node *s = p_block->statements[i];
  1002. switch (s->type) {
  1003. case GDScriptParser::Node::TYPE_NEWLINE: {
  1004. #ifdef DEBUG_ENABLED
  1005. const GDScriptParser::NewLineNode *nl = static_cast<const GDScriptParser::NewLineNode *>(s);
  1006. codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE);
  1007. codegen.opcodes.push_back(nl->line);
  1008. codegen.current_line = nl->line;
  1009. #endif
  1010. } break;
  1011. case GDScriptParser::Node::TYPE_CONTROL_FLOW: {
  1012. // try subblocks
  1013. const GDScriptParser::ControlFlowNode *cf = static_cast<const GDScriptParser::ControlFlowNode *>(s);
  1014. switch (cf->cf_type) {
  1015. case GDScriptParser::ControlFlowNode::CF_MATCH: {
  1016. GDScriptParser::MatchNode *match = cf->match;
  1017. GDScriptParser::IdentifierNode *id = memnew(GDScriptParser::IdentifierNode);
  1018. id->name = "#match_value";
  1019. // var #match_value
  1020. // copied because there is no _parse_statement :(
  1021. codegen.add_stack_identifier(id->name, p_stack_level++);
  1022. codegen.alloc_stack(p_stack_level);
  1023. new_identifiers++;
  1024. GDScriptParser::OperatorNode *op = memnew(GDScriptParser::OperatorNode);
  1025. op->op = GDScriptParser::OperatorNode::OP_ASSIGN;
  1026. op->arguments.push_back(id);
  1027. op->arguments.push_back(match->val_to_match);
  1028. int ret = _parse_expression(codegen, op, p_stack_level);
  1029. if (ret < 0) {
  1030. memdelete(id);
  1031. memdelete(op);
  1032. return ERR_PARSE_ERROR;
  1033. }
  1034. // break address
  1035. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1036. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  1037. int break_addr = codegen.opcodes.size();
  1038. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1039. codegen.opcodes.push_back(0); // break addr
  1040. for (int j = 0; j < match->compiled_pattern_branches.size(); j++) {
  1041. GDScriptParser::MatchNode::CompiledPatternBranch branch = match->compiled_pattern_branches[j];
  1042. // jump over continue
  1043. // jump unconditionally
  1044. // continue address
  1045. // compile the condition
  1046. int ret2 = _parse_expression(codegen, branch.compiled_pattern, p_stack_level);
  1047. if (ret2 < 0) {
  1048. memdelete(id);
  1049. memdelete(op);
  1050. return ERR_PARSE_ERROR;
  1051. }
  1052. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF);
  1053. codegen.opcodes.push_back(ret2);
  1054. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  1055. int continue_addr = codegen.opcodes.size();
  1056. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1057. codegen.opcodes.push_back(0);
  1058. Error err = _parse_block(codegen, branch.body, p_stack_level, p_break_addr, continue_addr);
  1059. if (err) {
  1060. memdelete(id);
  1061. memdelete(op);
  1062. return ERR_PARSE_ERROR;
  1063. }
  1064. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1065. codegen.opcodes.push_back(break_addr);
  1066. codegen.opcodes.write[continue_addr + 1] = codegen.opcodes.size();
  1067. }
  1068. codegen.opcodes.write[break_addr + 1] = codegen.opcodes.size();
  1069. memdelete(id);
  1070. memdelete(op);
  1071. } break;
  1072. case GDScriptParser::ControlFlowNode::CF_IF: {
  1073. int ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false);
  1074. if (ret2 < 0)
  1075. return ERR_PARSE_ERROR;
  1076. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1077. codegen.opcodes.push_back(ret2);
  1078. int else_addr = codegen.opcodes.size();
  1079. codegen.opcodes.push_back(0); //temporary
  1080. Error err = _parse_block(codegen, cf->body, p_stack_level, p_break_addr, p_continue_addr);
  1081. if (err)
  1082. return err;
  1083. if (cf->body_else) {
  1084. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1085. int end_addr = codegen.opcodes.size();
  1086. codegen.opcodes.push_back(0);
  1087. codegen.opcodes.write[else_addr] = codegen.opcodes.size();
  1088. codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE);
  1089. codegen.opcodes.push_back(cf->body_else->line);
  1090. codegen.current_line = cf->body_else->line;
  1091. Error err2 = _parse_block(codegen, cf->body_else, p_stack_level, p_break_addr, p_continue_addr);
  1092. if (err2)
  1093. return err2;
  1094. codegen.opcodes.write[end_addr] = codegen.opcodes.size();
  1095. } else {
  1096. //end without else
  1097. codegen.opcodes.write[else_addr] = codegen.opcodes.size();
  1098. }
  1099. } break;
  1100. case GDScriptParser::ControlFlowNode::CF_FOR: {
  1101. int slevel = p_stack_level;
  1102. int iter_stack_pos = slevel;
  1103. int iterator_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1104. int counter_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1105. int container_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1106. codegen.alloc_stack(slevel);
  1107. codegen.push_stack_identifiers();
  1108. codegen.add_stack_identifier(static_cast<const GDScriptParser::IdentifierNode *>(cf->arguments[0])->name, iter_stack_pos);
  1109. int ret2 = _parse_expression(codegen, cf->arguments[1], slevel, false);
  1110. if (ret2 < 0)
  1111. return ERR_COMPILATION_FAILED;
  1112. //assign container
  1113. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  1114. codegen.opcodes.push_back(container_pos);
  1115. codegen.opcodes.push_back(ret2);
  1116. //begin loop
  1117. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ITERATE_BEGIN);
  1118. codegen.opcodes.push_back(counter_pos);
  1119. codegen.opcodes.push_back(container_pos);
  1120. codegen.opcodes.push_back(codegen.opcodes.size() + 4);
  1121. codegen.opcodes.push_back(iterator_pos);
  1122. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); //skip code for next
  1123. codegen.opcodes.push_back(codegen.opcodes.size() + 8);
  1124. //break loop
  1125. int break_pos = codegen.opcodes.size();
  1126. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); //skip code for next
  1127. codegen.opcodes.push_back(0); //skip code for next
  1128. //next loop
  1129. int continue_pos = codegen.opcodes.size();
  1130. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ITERATE);
  1131. codegen.opcodes.push_back(counter_pos);
  1132. codegen.opcodes.push_back(container_pos);
  1133. codegen.opcodes.push_back(break_pos);
  1134. codegen.opcodes.push_back(iterator_pos);
  1135. Error err = _parse_block(codegen, cf->body, slevel, break_pos, continue_pos);
  1136. if (err)
  1137. return err;
  1138. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1139. codegen.opcodes.push_back(continue_pos);
  1140. codegen.opcodes.write[break_pos + 1] = codegen.opcodes.size();
  1141. codegen.pop_stack_identifiers();
  1142. } break;
  1143. case GDScriptParser::ControlFlowNode::CF_WHILE: {
  1144. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1145. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  1146. int break_addr = codegen.opcodes.size();
  1147. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1148. codegen.opcodes.push_back(0);
  1149. int continue_addr = codegen.opcodes.size();
  1150. int ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false);
  1151. if (ret2 < 0)
  1152. return ERR_PARSE_ERROR;
  1153. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1154. codegen.opcodes.push_back(ret2);
  1155. codegen.opcodes.push_back(break_addr);
  1156. Error err = _parse_block(codegen, cf->body, p_stack_level, break_addr, continue_addr);
  1157. if (err)
  1158. return err;
  1159. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1160. codegen.opcodes.push_back(continue_addr);
  1161. codegen.opcodes.write[break_addr + 1] = codegen.opcodes.size();
  1162. } break;
  1163. case GDScriptParser::ControlFlowNode::CF_BREAK: {
  1164. if (p_break_addr < 0) {
  1165. _set_error("'break'' not within loop", cf);
  1166. return ERR_COMPILATION_FAILED;
  1167. }
  1168. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1169. codegen.opcodes.push_back(p_break_addr);
  1170. } break;
  1171. case GDScriptParser::ControlFlowNode::CF_CONTINUE: {
  1172. if (p_continue_addr < 0) {
  1173. _set_error("'continue' not within loop", cf);
  1174. return ERR_COMPILATION_FAILED;
  1175. }
  1176. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1177. codegen.opcodes.push_back(p_continue_addr);
  1178. } break;
  1179. case GDScriptParser::ControlFlowNode::CF_RETURN: {
  1180. int ret2;
  1181. if (cf->arguments.size()) {
  1182. ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false);
  1183. if (ret2 < 0)
  1184. return ERR_PARSE_ERROR;
  1185. } else {
  1186. ret2 = GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS;
  1187. }
  1188. codegen.opcodes.push_back(GDScriptFunction::OPCODE_RETURN);
  1189. codegen.opcodes.push_back(ret2);
  1190. } break;
  1191. }
  1192. } break;
  1193. case GDScriptParser::Node::TYPE_ASSERT: {
  1194. #ifdef DEBUG_ENABLED
  1195. // try subblocks
  1196. const GDScriptParser::AssertNode *as = static_cast<const GDScriptParser::AssertNode *>(s);
  1197. int ret2 = _parse_expression(codegen, as->condition, p_stack_level, false);
  1198. if (ret2 < 0)
  1199. return ERR_PARSE_ERROR;
  1200. int message_ret = 0;
  1201. if (as->message) {
  1202. message_ret = _parse_expression(codegen, as->message, p_stack_level + 1, false);
  1203. if (message_ret < 0)
  1204. return ERR_PARSE_ERROR;
  1205. }
  1206. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSERT);
  1207. codegen.opcodes.push_back(ret2);
  1208. codegen.opcodes.push_back(message_ret);
  1209. #endif
  1210. } break;
  1211. case GDScriptParser::Node::TYPE_BREAKPOINT: {
  1212. #ifdef DEBUG_ENABLED
  1213. // try subblocks
  1214. codegen.opcodes.push_back(GDScriptFunction::OPCODE_BREAKPOINT);
  1215. #endif
  1216. } break;
  1217. case GDScriptParser::Node::TYPE_LOCAL_VAR: {
  1218. const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(s);
  1219. // since we are using properties now for most class access, allow shadowing of class members to make user's life easier.
  1220. //
  1221. //if (_is_class_member_property(codegen, lv->name)) {
  1222. // _set_error("Name for local variable '" + String(lv->name) + "' can't shadow class property of the same name.", lv);
  1223. // return ERR_ALREADY_EXISTS;
  1224. //}
  1225. codegen.add_stack_identifier(lv->name, p_stack_level++);
  1226. codegen.alloc_stack(p_stack_level);
  1227. new_identifiers++;
  1228. } break;
  1229. default: {
  1230. //expression
  1231. int ret2 = _parse_expression(codegen, s, p_stack_level, true);
  1232. if (ret2 < 0)
  1233. return ERR_PARSE_ERROR;
  1234. } break;
  1235. }
  1236. }
  1237. codegen.pop_stack_identifiers();
  1238. return OK;
  1239. }
  1240. Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready) {
  1241. Vector<int> bytecode;
  1242. CodeGen codegen;
  1243. codegen.class_node = p_class;
  1244. codegen.script = p_script;
  1245. codegen.function_node = p_func;
  1246. codegen.stack_max = 0;
  1247. codegen.current_line = 0;
  1248. codegen.call_max = 0;
  1249. codegen.debug_stack = EngineDebugger::is_active();
  1250. Vector<StringName> argnames;
  1251. int stack_level = 0;
  1252. if (p_func) {
  1253. for (int i = 0; i < p_func->arguments.size(); i++) {
  1254. // since we are using properties now for most class access, allow shadowing of class members to make user's life easier.
  1255. //
  1256. //if (_is_class_member_property(p_script, p_func->arguments[i])) {
  1257. // _set_error("Name for argument '" + String(p_func->arguments[i]) + "' can't shadow class property of the same name.", p_func);
  1258. // return ERR_ALREADY_EXISTS;
  1259. //}
  1260. codegen.add_stack_identifier(p_func->arguments[i], i);
  1261. #ifdef TOOLS_ENABLED
  1262. argnames.push_back(p_func->arguments[i]);
  1263. #endif
  1264. }
  1265. stack_level = p_func->arguments.size();
  1266. }
  1267. codegen.alloc_stack(stack_level);
  1268. /* Parse initializer -if applies- */
  1269. bool is_initializer = !p_for_ready && !p_func;
  1270. if (is_initializer || (p_func && String(p_func->name) == "_init")) {
  1271. //parse initializer for class members
  1272. if (!p_func && p_class->extends_used && p_script->native.is_null()) {
  1273. //call implicit parent constructor
  1274. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_SELF_BASE);
  1275. codegen.opcodes.push_back(codegen.get_name_map_pos("_init"));
  1276. codegen.opcodes.push_back(0);
  1277. codegen.opcodes.push_back((GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) | 0);
  1278. }
  1279. Error err = _parse_block(codegen, p_class->initializer, stack_level);
  1280. if (err)
  1281. return err;
  1282. is_initializer = true;
  1283. }
  1284. if (p_for_ready || (p_func && String(p_func->name) == "_ready")) {
  1285. //parse initializer for class members
  1286. if (p_class->ready->statements.size()) {
  1287. Error err = _parse_block(codegen, p_class->ready, stack_level);
  1288. if (err)
  1289. return err;
  1290. }
  1291. }
  1292. /* Parse default argument code -if applies- */
  1293. Vector<int> defarg_addr;
  1294. StringName func_name;
  1295. if (p_func) {
  1296. if (p_func->default_values.size()) {
  1297. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  1298. defarg_addr.push_back(codegen.opcodes.size());
  1299. for (int i = 0; i < p_func->default_values.size(); i++) {
  1300. _parse_expression(codegen, p_func->default_values[i], stack_level, true);
  1301. defarg_addr.push_back(codegen.opcodes.size());
  1302. }
  1303. defarg_addr.invert();
  1304. }
  1305. Error err = _parse_block(codegen, p_func->body, stack_level);
  1306. if (err)
  1307. return err;
  1308. func_name = p_func->name;
  1309. } else {
  1310. if (p_for_ready)
  1311. func_name = "_ready";
  1312. else
  1313. func_name = "_init";
  1314. }
  1315. codegen.opcodes.push_back(GDScriptFunction::OPCODE_END);
  1316. /*
  1317. if (String(p_func->name)=="") { //initializer func
  1318. gdfunc = &p_script->initializer;
  1319. */
  1320. //} else { //regular func
  1321. p_script->member_functions[func_name] = memnew(GDScriptFunction);
  1322. GDScriptFunction *gdfunc = p_script->member_functions[func_name];
  1323. //}
  1324. if (p_func) {
  1325. gdfunc->_static = p_func->_static;
  1326. gdfunc->rpc_mode = p_func->rpc_mode;
  1327. gdfunc->argument_types.resize(p_func->argument_types.size());
  1328. for (int i = 0; i < p_func->argument_types.size(); i++) {
  1329. gdfunc->argument_types.write[i] = _gdtype_from_datatype(p_func->argument_types[i]);
  1330. }
  1331. gdfunc->return_type = _gdtype_from_datatype(p_func->return_type);
  1332. } else {
  1333. gdfunc->_static = false;
  1334. gdfunc->rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1335. gdfunc->return_type = GDScriptDataType();
  1336. gdfunc->return_type.has_type = true;
  1337. gdfunc->return_type.kind = GDScriptDataType::BUILTIN;
  1338. gdfunc->return_type.builtin_type = Variant::NIL;
  1339. }
  1340. #ifdef TOOLS_ENABLED
  1341. gdfunc->arg_names = argnames;
  1342. #endif
  1343. //constants
  1344. if (codegen.constant_map.size()) {
  1345. gdfunc->_constant_count = codegen.constant_map.size();
  1346. gdfunc->constants.resize(codegen.constant_map.size());
  1347. gdfunc->_constants_ptr = gdfunc->constants.ptrw();
  1348. const Variant *K = nullptr;
  1349. while ((K = codegen.constant_map.next(K))) {
  1350. int idx = codegen.constant_map[*K];
  1351. gdfunc->constants.write[idx] = *K;
  1352. }
  1353. } else {
  1354. gdfunc->_constants_ptr = nullptr;
  1355. gdfunc->_constant_count = 0;
  1356. }
  1357. //global names
  1358. if (codegen.name_map.size()) {
  1359. gdfunc->global_names.resize(codegen.name_map.size());
  1360. gdfunc->_global_names_ptr = &gdfunc->global_names[0];
  1361. for (Map<StringName, int>::Element *E = codegen.name_map.front(); E; E = E->next()) {
  1362. gdfunc->global_names.write[E->get()] = E->key();
  1363. }
  1364. gdfunc->_global_names_count = gdfunc->global_names.size();
  1365. } else {
  1366. gdfunc->_global_names_ptr = nullptr;
  1367. gdfunc->_global_names_count = 0;
  1368. }
  1369. #ifdef TOOLS_ENABLED
  1370. // Named globals
  1371. if (codegen.named_globals.size()) {
  1372. gdfunc->named_globals.resize(codegen.named_globals.size());
  1373. gdfunc->_named_globals_ptr = gdfunc->named_globals.ptr();
  1374. for (int i = 0; i < codegen.named_globals.size(); i++) {
  1375. gdfunc->named_globals.write[i] = codegen.named_globals[i];
  1376. }
  1377. gdfunc->_named_globals_count = gdfunc->named_globals.size();
  1378. }
  1379. #endif
  1380. if (codegen.opcodes.size()) {
  1381. gdfunc->code = codegen.opcodes;
  1382. gdfunc->_code_ptr = &gdfunc->code[0];
  1383. gdfunc->_code_size = codegen.opcodes.size();
  1384. } else {
  1385. gdfunc->_code_ptr = nullptr;
  1386. gdfunc->_code_size = 0;
  1387. }
  1388. if (defarg_addr.size()) {
  1389. gdfunc->default_arguments = defarg_addr;
  1390. gdfunc->_default_arg_count = defarg_addr.size() - 1;
  1391. gdfunc->_default_arg_ptr = &gdfunc->default_arguments[0];
  1392. } else {
  1393. gdfunc->_default_arg_count = 0;
  1394. gdfunc->_default_arg_ptr = nullptr;
  1395. }
  1396. gdfunc->_argument_count = p_func ? p_func->arguments.size() : 0;
  1397. gdfunc->_stack_size = codegen.stack_max;
  1398. gdfunc->_call_size = codegen.call_max;
  1399. gdfunc->name = func_name;
  1400. #ifdef DEBUG_ENABLED
  1401. if (EngineDebugger::is_active()) {
  1402. String signature;
  1403. //path
  1404. if (p_script->get_path() != String())
  1405. signature += p_script->get_path();
  1406. //loc
  1407. if (p_func) {
  1408. signature += "::" + itos(p_func->body->line);
  1409. } else {
  1410. signature += "::0";
  1411. }
  1412. //function and class
  1413. if (p_class->name) {
  1414. signature += "::" + String(p_class->name) + "." + String(func_name);
  1415. } else {
  1416. signature += "::" + String(func_name);
  1417. }
  1418. gdfunc->profile.signature = signature;
  1419. }
  1420. #endif
  1421. gdfunc->_script = p_script;
  1422. gdfunc->source = source;
  1423. #ifdef DEBUG_ENABLED
  1424. {
  1425. gdfunc->func_cname = (String(source) + " - " + String(func_name)).utf8();
  1426. gdfunc->_func_cname = gdfunc->func_cname.get_data();
  1427. }
  1428. #endif
  1429. if (p_func) {
  1430. gdfunc->_initial_line = p_func->line;
  1431. #ifdef TOOLS_ENABLED
  1432. p_script->member_lines[func_name] = p_func->line;
  1433. #endif
  1434. } else {
  1435. gdfunc->_initial_line = 0;
  1436. }
  1437. if (codegen.debug_stack)
  1438. gdfunc->stack_debug = codegen.stack_debug;
  1439. if (is_initializer)
  1440. p_script->initializer = gdfunc;
  1441. return OK;
  1442. }
  1443. Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  1444. parsing_classes.insert(p_script);
  1445. if (p_class->owner && p_class->owner->owner) {
  1446. // Owner is not root
  1447. if (!parsed_classes.has(p_script->_owner)) {
  1448. if (parsing_classes.has(p_script->_owner)) {
  1449. _set_error("Cyclic class reference for '" + String(p_class->name) + "'.", p_class);
  1450. return ERR_PARSE_ERROR;
  1451. }
  1452. Error err = _parse_class_level(p_script->_owner, p_class->owner, p_keep_state);
  1453. if (err) {
  1454. return err;
  1455. }
  1456. }
  1457. }
  1458. p_script->native = Ref<GDScriptNativeClass>();
  1459. p_script->base = Ref<GDScript>();
  1460. p_script->_base = nullptr;
  1461. p_script->members.clear();
  1462. p_script->constants.clear();
  1463. for (Map<StringName, GDScriptFunction *>::Element *E = p_script->member_functions.front(); E; E = E->next()) {
  1464. memdelete(E->get());
  1465. }
  1466. p_script->member_functions.clear();
  1467. p_script->member_indices.clear();
  1468. p_script->member_info.clear();
  1469. p_script->_signals.clear();
  1470. p_script->initializer = nullptr;
  1471. p_script->tool = p_class->tool;
  1472. p_script->name = p_class->name;
  1473. Ref<GDScriptNativeClass> native;
  1474. GDScriptDataType base_type = _gdtype_from_datatype(p_class->base_type);
  1475. // Inheritance
  1476. switch (base_type.kind) {
  1477. case GDScriptDataType::NATIVE: {
  1478. int native_idx = GDScriptLanguage::get_singleton()->get_global_map()[base_type.native_type];
  1479. native = GDScriptLanguage::get_singleton()->get_global_array()[native_idx];
  1480. ERR_FAIL_COND_V(native.is_null(), ERR_BUG);
  1481. p_script->native = native;
  1482. } break;
  1483. case GDScriptDataType::GDSCRIPT: {
  1484. Ref<GDScript> base = base_type.script_type;
  1485. p_script->base = base;
  1486. p_script->_base = base.ptr();
  1487. p_script->member_indices = base->member_indices;
  1488. if (p_class->base_type.kind == GDScriptParser::DataType::CLASS) {
  1489. if (!parsed_classes.has(p_script->_base)) {
  1490. if (parsing_classes.has(p_script->_base)) {
  1491. _set_error("Cyclic class reference for '" + String(p_class->name) + "'.", p_class);
  1492. return ERR_PARSE_ERROR;
  1493. }
  1494. Error err = _parse_class_level(p_script->_base, p_class->base_type.class_type, p_keep_state);
  1495. if (err) {
  1496. return err;
  1497. }
  1498. }
  1499. }
  1500. } break;
  1501. default: {
  1502. _set_error("Parser bug: invalid inheritance.", p_class);
  1503. return ERR_BUG;
  1504. } break;
  1505. }
  1506. for (int i = 0; i < p_class->variables.size(); i++) {
  1507. StringName name = p_class->variables[i].identifier;
  1508. GDScript::MemberInfo minfo;
  1509. minfo.index = p_script->member_indices.size();
  1510. minfo.setter = p_class->variables[i].setter;
  1511. minfo.getter = p_class->variables[i].getter;
  1512. minfo.rpc_mode = p_class->variables[i].rpc_mode;
  1513. minfo.data_type = _gdtype_from_datatype(p_class->variables[i].data_type);
  1514. PropertyInfo prop_info = minfo.data_type;
  1515. prop_info.name = name;
  1516. PropertyInfo export_info = p_class->variables[i]._export;
  1517. if (export_info.type != Variant::NIL) {
  1518. if (!minfo.data_type.has_type) {
  1519. prop_info.type = export_info.type;
  1520. prop_info.class_name = export_info.class_name;
  1521. }
  1522. prop_info.hint = export_info.hint;
  1523. prop_info.hint_string = export_info.hint_string;
  1524. prop_info.usage = export_info.usage;
  1525. #ifdef TOOLS_ENABLED
  1526. if (p_class->variables[i].default_value.get_type() != Variant::NIL) {
  1527. p_script->member_default_values[name] = p_class->variables[i].default_value;
  1528. }
  1529. #endif
  1530. } else {
  1531. prop_info.usage = PROPERTY_USAGE_SCRIPT_VARIABLE;
  1532. }
  1533. p_script->member_info[name] = prop_info;
  1534. p_script->member_indices[name] = minfo;
  1535. p_script->members.insert(name);
  1536. #ifdef TOOLS_ENABLED
  1537. p_script->member_lines[name] = p_class->variables[i].line;
  1538. #endif
  1539. }
  1540. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
  1541. StringName name = E->key();
  1542. ERR_CONTINUE(E->get().expression->type != GDScriptParser::Node::TYPE_CONSTANT);
  1543. GDScriptParser::ConstantNode *constant = static_cast<GDScriptParser::ConstantNode *>(E->get().expression);
  1544. p_script->constants.insert(name, constant->value);
  1545. #ifdef TOOLS_ENABLED
  1546. p_script->member_lines[name] = E->get().expression->line;
  1547. #endif
  1548. }
  1549. for (int i = 0; i < p_class->_signals.size(); i++) {
  1550. StringName name = p_class->_signals[i].name;
  1551. GDScript *c = p_script;
  1552. while (c) {
  1553. if (c->_signals.has(name)) {
  1554. _set_error("Signal '" + name + "' redefined (in current or parent class)", p_class);
  1555. return ERR_ALREADY_EXISTS;
  1556. }
  1557. if (c->base.is_valid()) {
  1558. c = c->base.ptr();
  1559. } else {
  1560. c = nullptr;
  1561. }
  1562. }
  1563. if (native.is_valid()) {
  1564. if (ClassDB::has_signal(native->get_name(), name)) {
  1565. _set_error("Signal '" + name + "' redefined (original in native class '" + String(native->get_name()) + "')", p_class);
  1566. return ERR_ALREADY_EXISTS;
  1567. }
  1568. }
  1569. p_script->_signals[name] = p_class->_signals[i].arguments;
  1570. }
  1571. parsed_classes.insert(p_script);
  1572. parsing_classes.erase(p_script);
  1573. //parse sub-classes
  1574. for (int i = 0; i < p_class->subclasses.size(); i++) {
  1575. StringName name = p_class->subclasses[i]->name;
  1576. Ref<GDScript> &subclass = p_script->subclasses[name];
  1577. GDScript *subclass_ptr = subclass.ptr();
  1578. // Subclass might still be parsing, just skip it
  1579. if (!parsed_classes.has(subclass_ptr) && !parsing_classes.has(subclass_ptr)) {
  1580. Error err = _parse_class_level(subclass_ptr, p_class->subclasses[i], p_keep_state);
  1581. if (err)
  1582. return err;
  1583. }
  1584. #ifdef TOOLS_ENABLED
  1585. p_script->member_lines[name] = p_class->subclasses[i]->line;
  1586. #endif
  1587. p_script->constants.insert(name, subclass); //once parsed, goes to the list of constants
  1588. }
  1589. return OK;
  1590. }
  1591. Error GDScriptCompiler::_parse_class_blocks(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  1592. //parse methods
  1593. bool has_initializer = false;
  1594. bool has_ready = false;
  1595. for (int i = 0; i < p_class->functions.size(); i++) {
  1596. if (!has_initializer && p_class->functions[i]->name == "_init")
  1597. has_initializer = true;
  1598. if (!has_ready && p_class->functions[i]->name == "_ready")
  1599. has_ready = true;
  1600. Error err = _parse_function(p_script, p_class, p_class->functions[i]);
  1601. if (err)
  1602. return err;
  1603. }
  1604. //parse static methods
  1605. for (int i = 0; i < p_class->static_functions.size(); i++) {
  1606. Error err = _parse_function(p_script, p_class, p_class->static_functions[i]);
  1607. if (err)
  1608. return err;
  1609. }
  1610. if (!has_initializer) {
  1611. //create a constructor
  1612. Error err = _parse_function(p_script, p_class, nullptr);
  1613. if (err)
  1614. return err;
  1615. }
  1616. if (!has_ready && p_class->ready->statements.size()) {
  1617. //create a constructor
  1618. Error err = _parse_function(p_script, p_class, nullptr, true);
  1619. if (err)
  1620. return err;
  1621. }
  1622. #ifdef DEBUG_ENABLED
  1623. //validate instances if keeping state
  1624. if (p_keep_state) {
  1625. for (Set<Object *>::Element *E = p_script->instances.front(); E;) {
  1626. Set<Object *>::Element *N = E->next();
  1627. ScriptInstance *si = E->get()->get_script_instance();
  1628. if (si->is_placeholder()) {
  1629. #ifdef TOOLS_ENABLED
  1630. PlaceHolderScriptInstance *psi = static_cast<PlaceHolderScriptInstance *>(si);
  1631. if (p_script->is_tool()) {
  1632. //re-create as an instance
  1633. p_script->placeholders.erase(psi); //remove placeholder
  1634. GDScriptInstance *instance = memnew(GDScriptInstance);
  1635. instance->base_ref = Object::cast_to<Reference>(E->get());
  1636. instance->members.resize(p_script->member_indices.size());
  1637. instance->script = Ref<GDScript>(p_script);
  1638. instance->owner = E->get();
  1639. //needed for hot reloading
  1640. for (Map<StringName, GDScript::MemberInfo>::Element *F = p_script->member_indices.front(); F; F = F->next()) {
  1641. instance->member_indices_cache[F->key()] = F->get().index;
  1642. }
  1643. instance->owner->set_script_instance(instance);
  1644. /* STEP 2, INITIALIZE AND CONSTRUCT */
  1645. Callable::CallError ce;
  1646. p_script->initializer->call(instance, nullptr, 0, ce);
  1647. if (ce.error != Callable::CallError::CALL_OK) {
  1648. //well, tough luck, not goinna do anything here
  1649. }
  1650. }
  1651. #endif
  1652. } else {
  1653. GDScriptInstance *gi = static_cast<GDScriptInstance *>(si);
  1654. gi->reload_members();
  1655. }
  1656. E = N;
  1657. }
  1658. }
  1659. #endif
  1660. for (int i = 0; i < p_class->subclasses.size(); i++) {
  1661. StringName name = p_class->subclasses[i]->name;
  1662. GDScript *subclass = p_script->subclasses[name].ptr();
  1663. Error err = _parse_class_blocks(subclass, p_class->subclasses[i], p_keep_state);
  1664. if (err) {
  1665. return err;
  1666. }
  1667. }
  1668. p_script->valid = true;
  1669. return OK;
  1670. }
  1671. void GDScriptCompiler::_make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  1672. Map<StringName, Ref<GDScript>> old_subclasses;
  1673. if (p_keep_state) {
  1674. old_subclasses = p_script->subclasses;
  1675. }
  1676. p_script->subclasses.clear();
  1677. for (int i = 0; i < p_class->subclasses.size(); i++) {
  1678. StringName name = p_class->subclasses[i]->name;
  1679. Ref<GDScript> subclass;
  1680. String fully_qualified_name = p_script->fully_qualified_name + "::" + name;
  1681. if (old_subclasses.has(name)) {
  1682. subclass = old_subclasses[name];
  1683. } else {
  1684. Ref<GDScript> orphan_subclass = GDScriptLanguage::get_singleton()->get_orphan_subclass(fully_qualified_name);
  1685. if (orphan_subclass.is_valid()) {
  1686. subclass = orphan_subclass;
  1687. } else {
  1688. subclass.instance();
  1689. }
  1690. }
  1691. subclass->_owner = p_script;
  1692. subclass->fully_qualified_name = fully_qualified_name;
  1693. p_script->subclasses.insert(name, subclass);
  1694. _make_scripts(subclass.ptr(), p_class->subclasses[i], false);
  1695. }
  1696. }
  1697. Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state) {
  1698. err_line = -1;
  1699. err_column = -1;
  1700. error = "";
  1701. parser = p_parser;
  1702. main_script = p_script;
  1703. const GDScriptParser::Node *root = parser->get_parse_tree();
  1704. ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, ERR_INVALID_DATA);
  1705. source = p_script->get_path();
  1706. // The best fully qualified name for a base level script is its file path
  1707. p_script->fully_qualified_name = p_script->path;
  1708. // Create scripts for subclasses beforehand so they can be referenced
  1709. _make_scripts(p_script, static_cast<const GDScriptParser::ClassNode *>(root), p_keep_state);
  1710. p_script->_owner = nullptr;
  1711. Error err = _parse_class_level(p_script, static_cast<const GDScriptParser::ClassNode *>(root), p_keep_state);
  1712. if (err)
  1713. return err;
  1714. err = _parse_class_blocks(p_script, static_cast<const GDScriptParser::ClassNode *>(root), p_keep_state);
  1715. if (err)
  1716. return err;
  1717. return OK;
  1718. }
  1719. String GDScriptCompiler::get_error() const {
  1720. return error;
  1721. }
  1722. int GDScriptCompiler::get_error_line() const {
  1723. return err_line;
  1724. }
  1725. int GDScriptCompiler::get_error_column() const {
  1726. return err_column;
  1727. }
  1728. GDScriptCompiler::GDScriptCompiler() {
  1729. }