gdscript_compiler.cpp 76 KB

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