gdscript_compiler.cpp 70 KB

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