gd_compiler.cpp 60 KB

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