gd_compiler.cpp 58 KB

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