gdscript_compiler.cpp 77 KB

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