gd_compiler.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  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-2016 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. const GDParser::NewLineNode *nl = static_cast<const GDParser::NewLineNode*>(s);
  739. codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
  740. codegen.opcodes.push_back(nl->line);
  741. codegen.current_line=nl->line;
  742. } break;
  743. case GDParser::Node::TYPE_CONTROL_FLOW: {
  744. // try subblocks
  745. const GDParser::ControlFlowNode *cf = static_cast<const GDParser::ControlFlowNode*>(s);
  746. switch(cf->cf_type) {
  747. case GDParser::ControlFlowNode::CF_IF: {
  748. #ifdef DEBUG_ENABLED
  749. codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
  750. codegen.opcodes.push_back(cf->line);
  751. codegen.current_line=cf->line;
  752. #endif
  753. int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  754. if (ret<0)
  755. return ERR_PARSE_ERROR;
  756. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  757. codegen.opcodes.push_back(ret);
  758. int else_addr=codegen.opcodes.size();
  759. codegen.opcodes.push_back(0); //temporary
  760. Error err = _parse_block(codegen,cf->body,p_stack_level,p_break_addr,p_continue_addr);
  761. if (err)
  762. return err;
  763. if (cf->body_else) {
  764. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  765. int end_addr=codegen.opcodes.size();
  766. codegen.opcodes.push_back(0);
  767. codegen.opcodes[else_addr]=codegen.opcodes.size();
  768. Error err = _parse_block(codegen,cf->body_else,p_stack_level,p_break_addr,p_continue_addr);
  769. if (err)
  770. return err;
  771. codegen.opcodes[end_addr]=codegen.opcodes.size();
  772. } else {
  773. //end without else
  774. codegen.opcodes[else_addr]=codegen.opcodes.size();
  775. }
  776. } break;
  777. case GDParser::ControlFlowNode::CF_FOR: {
  778. int slevel=p_stack_level;
  779. int iter_stack_pos=slevel;
  780. int iterator_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  781. int counter_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  782. int container_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  783. codegen.alloc_stack(slevel);
  784. codegen.push_stack_identifiers();
  785. codegen.add_stack_identifier(static_cast<const GDParser::IdentifierNode*>(cf->arguments[0])->name,iter_stack_pos);
  786. int ret = _parse_expression(codegen,cf->arguments[1],slevel,false);
  787. if (ret<0)
  788. return ERR_COMPILATION_FAILED;
  789. //assign container
  790. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN);
  791. codegen.opcodes.push_back(container_pos);
  792. codegen.opcodes.push_back(ret);
  793. //begin loop
  794. codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE_BEGIN);
  795. codegen.opcodes.push_back(counter_pos);
  796. codegen.opcodes.push_back(container_pos);
  797. codegen.opcodes.push_back(codegen.opcodes.size()+4);
  798. codegen.opcodes.push_back(iterator_pos);
  799. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
  800. codegen.opcodes.push_back(codegen.opcodes.size()+8);
  801. //break loop
  802. int break_pos=codegen.opcodes.size();
  803. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
  804. codegen.opcodes.push_back(0); //skip code for next
  805. //next loop
  806. int continue_pos=codegen.opcodes.size();
  807. codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE);
  808. codegen.opcodes.push_back(counter_pos);
  809. codegen.opcodes.push_back(container_pos);
  810. codegen.opcodes.push_back(break_pos);
  811. codegen.opcodes.push_back(iterator_pos);
  812. Error err = _parse_block(codegen,cf->body,slevel,break_pos,continue_pos);
  813. if (err)
  814. return err;
  815. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  816. codegen.opcodes.push_back(continue_pos);
  817. codegen.opcodes[break_pos+1]=codegen.opcodes.size();
  818. codegen.pop_stack_identifiers();
  819. } break;
  820. case GDParser::ControlFlowNode::CF_WHILE: {
  821. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  822. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  823. int break_addr=codegen.opcodes.size();
  824. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  825. codegen.opcodes.push_back(0);
  826. int continue_addr=codegen.opcodes.size();
  827. int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  828. if (ret<0)
  829. return ERR_PARSE_ERROR;
  830. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  831. codegen.opcodes.push_back(ret);
  832. codegen.opcodes.push_back(break_addr);
  833. Error err = _parse_block(codegen,cf->body,p_stack_level,break_addr,continue_addr);
  834. if (err)
  835. return err;
  836. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  837. codegen.opcodes.push_back(continue_addr);
  838. codegen.opcodes[break_addr+1]=codegen.opcodes.size();
  839. } break;
  840. case GDParser::ControlFlowNode::CF_SWITCH: {
  841. } break;
  842. case GDParser::ControlFlowNode::CF_BREAK: {
  843. if (p_break_addr<0) {
  844. _set_error("'break'' not within loop",cf);
  845. return ERR_COMPILATION_FAILED;
  846. }
  847. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  848. codegen.opcodes.push_back(p_break_addr);
  849. } break;
  850. case GDParser::ControlFlowNode::CF_CONTINUE: {
  851. if (p_continue_addr<0) {
  852. _set_error("'continue' not within loop",cf);
  853. return ERR_COMPILATION_FAILED;
  854. }
  855. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  856. codegen.opcodes.push_back(p_continue_addr);
  857. } break;
  858. case GDParser::ControlFlowNode::CF_RETURN: {
  859. int ret;
  860. if (cf->arguments.size()) {
  861. ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  862. if (ret<0)
  863. return ERR_PARSE_ERROR;
  864. } else {
  865. ret=GDFunction::ADDR_TYPE_NIL << GDFunction::ADDR_BITS;
  866. }
  867. codegen.opcodes.push_back(GDFunction::OPCODE_RETURN);
  868. codegen.opcodes.push_back(ret);
  869. } break;
  870. }
  871. } break;
  872. case GDParser::Node::TYPE_ASSERT: {
  873. // try subblocks
  874. const GDParser::AssertNode *as = static_cast<const GDParser::AssertNode*>(s);
  875. int ret = _parse_expression(codegen,as->condition,p_stack_level,false);
  876. if (ret<0)
  877. return ERR_PARSE_ERROR;
  878. codegen.opcodes.push_back(GDFunction::OPCODE_ASSERT);
  879. codegen.opcodes.push_back(ret);
  880. } break;
  881. case GDParser::Node::TYPE_BREAKPOINT: {
  882. // try subblocks
  883. codegen.opcodes.push_back(GDFunction::OPCODE_BREAKPOINT);
  884. } break;
  885. case GDParser::Node::TYPE_LOCAL_VAR: {
  886. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode*>(s);
  887. codegen.add_stack_identifier(lv->name,p_stack_level++);
  888. codegen.alloc_stack(p_stack_level);
  889. new_identifiers++;
  890. } break;
  891. default: {
  892. //expression
  893. int ret = _parse_expression(codegen,s,p_stack_level,true);
  894. if (ret<0)
  895. return ERR_PARSE_ERROR;
  896. } break;
  897. }
  898. }
  899. codegen.pop_stack_identifiers();
  900. return OK;
  901. }
  902. Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *p_class,const GDParser::FunctionNode *p_func,bool p_for_ready) {
  903. Vector<int> bytecode;
  904. CodeGen codegen;
  905. codegen.class_node=p_class;
  906. codegen.script=p_script;
  907. codegen.function_node=p_func;
  908. codegen.stack_max=0;
  909. codegen.current_line=0;
  910. codegen.call_max=0;
  911. codegen.debug_stack=ScriptDebugger::get_singleton()!=NULL;
  912. Vector<StringName> argnames;
  913. int stack_level=0;
  914. if (p_func) {
  915. for(int i=0;i<p_func->arguments.size();i++) {
  916. codegen.add_stack_identifier(p_func->arguments[i],i);
  917. #ifdef TOOLS_ENABLED
  918. argnames.push_back(p_func->arguments[i]);
  919. #endif
  920. }
  921. stack_level=p_func->arguments.size();
  922. }
  923. codegen.alloc_stack(stack_level);
  924. /* Parse initializer -if applies- */
  925. bool is_initializer=!p_for_ready && !p_func;
  926. if (is_initializer || (p_func && String(p_func->name)=="_init")) {
  927. //parse initializer for class members
  928. if (!p_func && p_class->extends_used && p_script->native.is_null()){
  929. //call implicit parent constructor
  930. codegen.opcodes.push_back(GDFunction::OPCODE_CALL_SELF_BASE);
  931. codegen.opcodes.push_back(codegen.get_name_map_pos("_init"));
  932. codegen.opcodes.push_back(0);
  933. codegen.opcodes.push_back((GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)|0);
  934. }
  935. Error err = _parse_block(codegen,p_class->initializer,stack_level);
  936. if (err)
  937. return err;
  938. is_initializer=true;
  939. }
  940. if (p_for_ready || (p_func && String(p_func->name)=="_ready")) {
  941. //parse initializer for class members
  942. if (p_class->ready->statements.size()) {
  943. Error err = _parse_block(codegen,p_class->ready,stack_level);
  944. if (err)
  945. return err;
  946. }
  947. }
  948. /* Parse default argument code -if applies- */
  949. Vector<int> defarg_addr;
  950. StringName func_name;
  951. if (p_func) {
  952. if (p_func->default_values.size()) {
  953. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  954. defarg_addr.push_back(codegen.opcodes.size());
  955. for(int i=0;i<p_func->default_values.size();i++) {
  956. _parse_expression(codegen,p_func->default_values[i],stack_level,true);
  957. defarg_addr.push_back(codegen.opcodes.size());
  958. }
  959. defarg_addr.invert();
  960. }
  961. Error err = _parse_block(codegen,p_func->body,stack_level);
  962. if (err)
  963. return err;
  964. func_name=p_func->name;
  965. } else {
  966. if (p_for_ready)
  967. func_name="_ready";
  968. else
  969. func_name="_init";
  970. }
  971. codegen.opcodes.push_back(GDFunction::OPCODE_END);
  972. GDFunction *gdfunc=NULL;
  973. //if (String(p_func->name)=="") { //initializer func
  974. // gdfunc = &p_script->initializer;
  975. //} else { //regular func
  976. p_script->member_functions[func_name]=memnew(GDFunction);
  977. gdfunc = p_script->member_functions[func_name];
  978. //}
  979. if (p_func) {
  980. gdfunc->_static=p_func->_static;
  981. gdfunc->rpc_mode=p_func->rpc_mode;
  982. }
  983. #ifdef TOOLS_ENABLED
  984. gdfunc->arg_names=argnames;
  985. #endif
  986. //constants
  987. if (codegen.constant_map.size()) {
  988. gdfunc->_constant_count=codegen.constant_map.size();
  989. gdfunc->constants.resize(codegen.constant_map.size());
  990. gdfunc->_constants_ptr=&gdfunc->constants[0];
  991. const Variant *K=NULL;
  992. while((K=codegen.constant_map.next(K))) {
  993. int idx = codegen.constant_map[*K];
  994. gdfunc->constants[idx]=*K;
  995. }
  996. } else {
  997. gdfunc->_constants_ptr=NULL;
  998. gdfunc->_constant_count=0;
  999. }
  1000. //global names
  1001. if (codegen.name_map.size()) {
  1002. gdfunc->global_names.resize(codegen.name_map.size());
  1003. gdfunc->_global_names_ptr = &gdfunc->global_names[0];
  1004. for(Map<StringName,int>::Element *E=codegen.name_map.front();E;E=E->next()) {
  1005. gdfunc->global_names[E->get()]=E->key();
  1006. }
  1007. gdfunc->_global_names_count=gdfunc->global_names.size();
  1008. } else {
  1009. gdfunc->_global_names_ptr = NULL;
  1010. gdfunc->_global_names_count =0;
  1011. }
  1012. if (codegen.opcodes.size()) {
  1013. gdfunc->code=codegen.opcodes;
  1014. gdfunc->_code_ptr=&gdfunc->code[0];
  1015. gdfunc->_code_size=codegen.opcodes.size();
  1016. } else {
  1017. gdfunc->_code_ptr=NULL;
  1018. gdfunc->_code_size=0;
  1019. }
  1020. if (defarg_addr.size()) {
  1021. gdfunc->default_arguments=defarg_addr;
  1022. gdfunc->_default_arg_count=defarg_addr.size()-1;
  1023. gdfunc->_default_arg_ptr=&gdfunc->default_arguments[0];
  1024. } else {
  1025. gdfunc->_default_arg_count=0;
  1026. gdfunc->_default_arg_ptr=NULL;
  1027. }
  1028. gdfunc->_argument_count=p_func ? p_func->arguments.size() : 0;
  1029. gdfunc->_stack_size=codegen.stack_max;
  1030. gdfunc->_call_size=codegen.call_max;
  1031. gdfunc->name=func_name;
  1032. #ifdef DEBUG_ENABLED
  1033. if (ScriptDebugger::get_singleton()){
  1034. String signature;
  1035. //path
  1036. if (p_script->get_path()!=String())
  1037. signature+=p_script->get_path();
  1038. //loc
  1039. if (p_func) {
  1040. signature+="::"+itos(p_func->body->line);
  1041. } else {
  1042. signature+="::0";
  1043. }
  1044. //funciton and class
  1045. if (p_class->name) {
  1046. signature+="::"+String(p_class->name)+"."+String(func_name);;
  1047. } else {
  1048. signature+="::"+String(func_name);
  1049. }
  1050. gdfunc->profile.signature=signature;
  1051. }
  1052. #endif
  1053. gdfunc->_script=p_script;
  1054. gdfunc->source=source;
  1055. #ifdef DEBUG_ENABLED
  1056. {
  1057. gdfunc->func_cname=(String(source)+" - "+String(func_name)).utf8();
  1058. gdfunc->_func_cname=gdfunc->func_cname.get_data();
  1059. }
  1060. #endif
  1061. if (p_func) {
  1062. gdfunc->_initial_line=p_func->line;
  1063. #ifdef TOOLS_ENABLED
  1064. p_script->member_lines[func_name]=p_func->line;
  1065. #endif
  1066. } else {
  1067. gdfunc->_initial_line=0;
  1068. }
  1069. if (codegen.debug_stack)
  1070. gdfunc->stack_debug=codegen.stack_debug;
  1071. if (is_initializer)
  1072. p_script->initializer=gdfunc;
  1073. return OK;
  1074. }
  1075. Error GDCompiler::_parse_class(GDScript *p_script, GDScript *p_owner, const GDParser::ClassNode *p_class, bool p_keep_state) {
  1076. Map<StringName,Ref<GDScript> > old_subclasses;
  1077. if (p_keep_state) {
  1078. old_subclasses=p_script->subclasses;
  1079. }
  1080. p_script->native=Ref<GDNativeClass>();
  1081. p_script->base=Ref<GDScript>();
  1082. p_script->_base=NULL;
  1083. p_script->members.clear();
  1084. p_script->constants.clear();
  1085. for (Map<StringName,GDFunction*>::Element *E=p_script->member_functions.front();E;E=E->next()) {
  1086. memdelete(E->get());
  1087. }
  1088. p_script->member_functions.clear();
  1089. p_script->member_indices.clear();
  1090. p_script->member_info.clear();
  1091. p_script->_signals.clear();
  1092. p_script->initializer=NULL;
  1093. p_script->subclasses.clear();
  1094. p_script->_owner=p_owner;
  1095. p_script->tool=p_class->tool;
  1096. p_script->name=p_class->name;
  1097. Ref<GDNativeClass> native;
  1098. if (p_class->extends_used) {
  1099. //do inheritance
  1100. String path = p_class->extends_file;
  1101. Ref<GDScript> script;
  1102. if (path!="") {
  1103. //path (and optionally subclasses)
  1104. if (path.is_rel_path()) {
  1105. String base;
  1106. if (p_owner) {
  1107. GDScript *current_class = p_owner;
  1108. while (current_class != NULL) {
  1109. base=current_class->get_path();
  1110. if (base=="")
  1111. current_class = current_class->_owner;
  1112. else
  1113. break;
  1114. }
  1115. }
  1116. else {
  1117. base = p_script->get_path();
  1118. }
  1119. if (base=="" || base.is_rel_path()) {
  1120. _set_error("Could not resolve relative path for parent class: "+path,p_class);
  1121. return ERR_FILE_NOT_FOUND;
  1122. }
  1123. path=base.get_base_dir().plus_file(path).simplify_path();
  1124. }
  1125. script = ResourceLoader::load(path);
  1126. if (script.is_null()) {
  1127. _set_error("Could not load base class: "+path,p_class);
  1128. return ERR_FILE_NOT_FOUND;
  1129. }
  1130. if (!script->valid) {
  1131. _set_error("Script not fully loaded (cyclic preload?): "+path,p_class);
  1132. return ERR_BUSY;
  1133. }
  1134. //print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
  1135. if (p_class->extends_class.size()) {
  1136. for(int i=0;i<p_class->extends_class.size();i++) {
  1137. String sub = p_class->extends_class[i];
  1138. if (script->subclasses.has(sub)) {
  1139. Ref<Script> subclass = script->subclasses[sub]; //avoid reference from dissapearing
  1140. script=subclass;
  1141. } else {
  1142. _set_error("Could not find subclass: "+sub,p_class);
  1143. return ERR_FILE_NOT_FOUND;
  1144. }
  1145. }
  1146. }
  1147. } else {
  1148. ERR_FAIL_COND_V(p_class->extends_class.size()==0,ERR_BUG);
  1149. //look around for the subclasses
  1150. String base=p_class->extends_class[0];
  1151. GDScript *p = p_owner;
  1152. Ref<GDScript> base_class;
  1153. while(p) {
  1154. if (p->subclasses.has(base)) {
  1155. base_class=p->subclasses[base];
  1156. break;
  1157. }
  1158. p=p->_owner;
  1159. }
  1160. if (base_class.is_valid()) {
  1161. for(int i=1;i<p_class->extends_class.size();i++) {
  1162. String subclass=p_class->extends_class[i];
  1163. if (base_class->subclasses.has(subclass)) {
  1164. base_class=base_class->subclasses[subclass];
  1165. } else {
  1166. _set_error("Could not find subclass: "+subclass,p_class);
  1167. return ERR_FILE_NOT_FOUND;
  1168. }
  1169. }
  1170. script=base_class;
  1171. } else {
  1172. if (p_class->extends_class.size()>1) {
  1173. _set_error("Invalid inheritance (unknown class+subclasses)",p_class);
  1174. return ERR_FILE_NOT_FOUND;
  1175. }
  1176. //if not found, try engine classes
  1177. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  1178. _set_error("Unknown class: '"+base+"'",p_class);
  1179. return ERR_FILE_NOT_FOUND;
  1180. }
  1181. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  1182. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  1183. if (!native.is_valid()) {
  1184. _set_error("Global not a class: '"+base+"'",p_class);
  1185. return ERR_FILE_NOT_FOUND;
  1186. }
  1187. }
  1188. }
  1189. if (script.is_valid()) {
  1190. p_script->base=script;
  1191. p_script->_base=p_script->base.ptr();
  1192. p_script->member_indices=script->member_indices;
  1193. } else if (native.is_valid()) {
  1194. p_script->native=native;
  1195. } else {
  1196. _set_error("Could not determine inheritance",p_class);
  1197. return ERR_FILE_NOT_FOUND;
  1198. }
  1199. }
  1200. //print_line("Script: "+p_script->get_path()+" indices: "+itos(p_script->member_indices.size()));
  1201. for(int i=0;i<p_class->variables.size();i++) {
  1202. StringName name = p_class->variables[i].identifier;
  1203. if (p_script->member_indices.has(name)) {
  1204. _set_error("Member '"+name+"' already exists (in current or parent class)",p_class);
  1205. return ERR_ALREADY_EXISTS;
  1206. }
  1207. if (p_class->variables[i]._export.type!=Variant::NIL) {
  1208. p_script->member_info[name]=p_class->variables[i]._export;
  1209. #ifdef TOOLS_ENABLED
  1210. if (p_class->variables[i].default_value.get_type()!=Variant::NIL) {
  1211. p_script->member_default_values[name]=p_class->variables[i].default_value;
  1212. }
  1213. #endif
  1214. } else {
  1215. p_script->member_info[name]=PropertyInfo(Variant::NIL,name,PROPERTY_HINT_NONE,"",PROPERTY_USAGE_SCRIPT_VARIABLE);
  1216. }
  1217. //int new_idx = p_script->member_indices.size();
  1218. GDScript::MemberInfo minfo;
  1219. minfo.index = p_script->member_indices.size();
  1220. minfo.setter = p_class->variables[i].setter;
  1221. minfo.getter = p_class->variables[i].getter;
  1222. minfo.rpc_mode=p_class->variables[i].rpc_mode;
  1223. p_script->member_indices[name]=minfo;
  1224. p_script->members.insert(name);
  1225. #ifdef TOOLS_ENABLED
  1226. p_script->member_lines[name]=p_class->variables[i].line;
  1227. #endif
  1228. }
  1229. for(int i=0;i<p_class->constant_expressions.size();i++) {
  1230. StringName name = p_class->constant_expressions[i].identifier;
  1231. ERR_CONTINUE( p_class->constant_expressions[i].expression->type!=GDParser::Node::TYPE_CONSTANT );
  1232. GDParser::ConstantNode *constant = static_cast<GDParser::ConstantNode*>(p_class->constant_expressions[i].expression);
  1233. p_script->constants.insert(name,constant->value);
  1234. //p_script->constants[constant->value].make_const();
  1235. #ifdef TOOLS_ENABLED
  1236. p_script->member_lines[name]=p_class->constant_expressions[i].expression->line;
  1237. #endif
  1238. }
  1239. for(int i=0;i<p_class->_signals.size();i++) {
  1240. StringName name = p_class->_signals[i].name;
  1241. GDScript *c = p_script;
  1242. while(c) {
  1243. if (c->_signals.has(name)) {
  1244. _set_error("Signal '"+name+"' redefined (in current or parent class)",p_class);
  1245. return ERR_ALREADY_EXISTS;
  1246. }
  1247. if (c->base.is_valid()) {
  1248. c=c->base.ptr();
  1249. } else {
  1250. c=NULL;
  1251. }
  1252. }
  1253. if (native.is_valid()) {
  1254. if (ObjectTypeDB::has_signal(native->get_name(),name)) {
  1255. _set_error("Signal '"+name+"' redefined (original in native class '"+String(native->get_name())+"')",p_class);
  1256. return ERR_ALREADY_EXISTS;
  1257. }
  1258. }
  1259. p_script->_signals[name]=p_class->_signals[i].arguments;
  1260. }
  1261. //parse sub-classes
  1262. for(int i=0;i<p_class->subclasses.size();i++) {
  1263. StringName name = p_class->subclasses[i]->name;
  1264. Ref<GDScript> subclass;
  1265. if (old_subclasses.has(name)) {
  1266. subclass=old_subclasses[name];
  1267. } else {
  1268. subclass.instance();
  1269. }
  1270. Error err = _parse_class(subclass.ptr(),p_script,p_class->subclasses[i],p_keep_state);
  1271. if (err)
  1272. return err;
  1273. #ifdef TOOLS_ENABLED
  1274. p_script->member_lines[name]=p_class->subclasses[i]->line;
  1275. #endif
  1276. p_script->constants.insert(name,subclass); //once parsed, goes to the list of constants
  1277. p_script->subclasses.insert(name,subclass);
  1278. }
  1279. //parse methods
  1280. bool has_initializer=false;
  1281. bool has_ready=false;
  1282. for(int i=0;i<p_class->functions.size();i++) {
  1283. if (!has_initializer && p_class->functions[i]->name=="_init")
  1284. has_initializer=true;
  1285. if (!has_ready && p_class->functions[i]->name=="_ready")
  1286. has_ready=true;
  1287. Error err = _parse_function(p_script,p_class,p_class->functions[i]);
  1288. if (err)
  1289. return err;
  1290. }
  1291. //parse static methods
  1292. for(int i=0;i<p_class->static_functions.size();i++) {
  1293. Error err = _parse_function(p_script,p_class,p_class->static_functions[i]);
  1294. if (err)
  1295. return err;
  1296. }
  1297. if (!has_initializer) {
  1298. //create a constructor
  1299. Error err = _parse_function(p_script,p_class,NULL);
  1300. if (err)
  1301. return err;
  1302. }
  1303. if (!has_ready && p_class->ready->statements.size()) {
  1304. //create a constructor
  1305. Error err = _parse_function(p_script,p_class,NULL,true);
  1306. if (err)
  1307. return err;
  1308. }
  1309. #ifdef DEBUG_ENABLED
  1310. //validate setters/getters if debug is enabled
  1311. for(int i=0;i<p_class->variables.size();i++) {
  1312. if (p_class->variables[i].setter) {
  1313. const Map<StringName,GDFunction*>::Element *E=p_script->get_member_functions().find(p_class->variables[i].setter);
  1314. if (!E) {
  1315. _set_error("Setter function '"+String(p_class->variables[i].setter)+"' not found in class.",NULL);
  1316. err_line=p_class->variables[i].line;
  1317. err_column=0;
  1318. return ERR_PARSE_ERROR;
  1319. }
  1320. if (E->get()->is_static()) {
  1321. _set_error("Setter function '"+String(p_class->variables[i].setter)+"' is static.",NULL);
  1322. err_line=p_class->variables[i].line;
  1323. err_column=0;
  1324. return ERR_PARSE_ERROR;
  1325. }
  1326. }
  1327. if (p_class->variables[i].getter) {
  1328. const Map<StringName,GDFunction*>::Element *E=p_script->get_member_functions().find(p_class->variables[i].getter);
  1329. if (!E) {
  1330. _set_error("Getter function '"+String(p_class->variables[i].getter)+"' not found in class.",NULL);
  1331. err_line=p_class->variables[i].line;
  1332. err_column=0;
  1333. return ERR_PARSE_ERROR;
  1334. }
  1335. if (E->get()->is_static()) {
  1336. _set_error("Getter function '"+String(p_class->variables[i].getter)+"' is static.",NULL);
  1337. err_line=p_class->variables[i].line;
  1338. err_column=0;
  1339. return ERR_PARSE_ERROR;
  1340. }
  1341. }
  1342. }
  1343. //validate instances if keeping state
  1344. if (p_keep_state) {
  1345. print_line("RELOAD KEEP "+p_script->path);
  1346. for (Set<Object*>::Element *E=p_script->instances.front();E;) {
  1347. Set<Object*>::Element *N = E->next();
  1348. ScriptInstance *si = E->get()->get_script_instance();
  1349. if (si->is_placeholder()) {
  1350. #ifdef TOOLS_ENABLED
  1351. PlaceHolderScriptInstance *psi = static_cast<PlaceHolderScriptInstance*>(si);
  1352. if (p_script->is_tool()) {
  1353. //re-create as an instance
  1354. p_script->placeholders.erase(psi); //remove placeholder
  1355. GDInstance* instance = memnew( GDInstance );
  1356. instance->base_ref=E->get()->cast_to<Reference>();
  1357. instance->members.resize(p_script->member_indices.size());
  1358. instance->script=Ref<GDScript>(p_script);
  1359. instance->owner=E->get();
  1360. //needed for hot reloading
  1361. for(Map<StringName,GDScript::MemberInfo>::Element *E=p_script->member_indices.front();E;E=E->next()) {
  1362. instance->member_indices_cache[E->key()]=E->get().index;
  1363. }
  1364. instance->owner->set_script_instance(instance);
  1365. /* STEP 2, INITIALIZE AND CONSRTUCT */
  1366. Variant::CallError ce;
  1367. p_script->initializer->call(instance,NULL,0,ce);
  1368. if (ce.error!=Variant::CallError::CALL_OK) {
  1369. //well, tough luck, not goinna do anything here
  1370. }
  1371. }
  1372. #endif
  1373. } else {
  1374. GDInstance *gi = static_cast<GDInstance*>(si);
  1375. gi->reload_members();
  1376. }
  1377. E=N;
  1378. }
  1379. }
  1380. #endif
  1381. p_script->valid=true;
  1382. return OK;
  1383. }
  1384. Error GDCompiler::compile(const GDParser *p_parser,GDScript *p_script,bool p_keep_state) {
  1385. err_line=-1;
  1386. err_column=-1;
  1387. error="";
  1388. parser=p_parser;
  1389. const GDParser::Node* root = parser->get_parse_tree();
  1390. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,ERR_INVALID_DATA);
  1391. source=p_script->get_path();
  1392. Error err = _parse_class(p_script,NULL,static_cast<const GDParser::ClassNode*>(root),p_keep_state);
  1393. if (err)
  1394. return err;
  1395. return OK;
  1396. }
  1397. String GDCompiler::get_error() const {
  1398. return error;
  1399. }
  1400. int GDCompiler::get_error_line() const{
  1401. return err_line;
  1402. }
  1403. int GDCompiler::get_error_column() const{
  1404. return err_column;
  1405. }
  1406. GDCompiler::GDCompiler()
  1407. {
  1408. }