gd_compiler.cpp 64 KB

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