gdscript_compiler.cpp 77 KB

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