gdscript_compiler.cpp 78 KB

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