gd_compiler.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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. bool in_static=false;
  341. if (instance->type==GDParser::Node::TYPE_SELF) {
  342. //room for optimization
  343. }
  344. Vector<int> arguments;
  345. int slevel = p_stack_level;
  346. for(int i=0;i<on->arguments.size();i++) {
  347. int ret;
  348. if (i==0 && on->arguments[i]->type==GDParser::Node::TYPE_SELF && codegen.function_node && codegen.function_node->_static) {
  349. //static call to self
  350. ret=(GDFunction::ADDR_TYPE_CLASS<<GDFunction::ADDR_BITS);
  351. } else if (i==1) {
  352. if (on->arguments[i]->type!=GDParser::Node::TYPE_IDENTIFIER) {
  353. _set_error("Attempt to call a non-identifier.",on);
  354. return -1;
  355. }
  356. GDParser::IdentifierNode *id = static_cast<GDParser::IdentifierNode*>(on->arguments[i]);
  357. ret=codegen.get_name_map_pos(id->name);
  358. } else {
  359. ret = _parse_expression(codegen,on->arguments[i],slevel);
  360. if (ret<0)
  361. return ret;
  362. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  363. slevel++;
  364. codegen.alloc_stack(slevel);
  365. }
  366. }
  367. arguments.push_back(ret);
  368. }
  369. codegen.opcodes.push_back(p_root?GDFunction::OPCODE_CALL:GDFunction::OPCODE_CALL_RETURN); // perform operator
  370. codegen.opcodes.push_back(on->arguments.size()-2);
  371. codegen.alloc_call(on->arguments.size()-2);
  372. for(int i=0;i<arguments.size();i++)
  373. codegen.opcodes.push_back(arguments[i]);
  374. }
  375. } break;
  376. case GDParser::OperatorNode::OP_YIELD: {
  377. ERR_FAIL_COND_V(on->arguments.size() && on->arguments.size()!=2,-1);
  378. Vector<int> arguments;
  379. int slevel = p_stack_level;
  380. for(int i=0;i<on->arguments.size();i++) {
  381. int ret = _parse_expression(codegen,on->arguments[i],slevel);
  382. if (ret<0)
  383. return ret;
  384. if (ret&(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)) {
  385. slevel++;
  386. codegen.alloc_stack(slevel);
  387. }
  388. arguments.push_back(ret);
  389. }
  390. //push call bytecode
  391. codegen.opcodes.push_back(arguments.size()==0?GDFunction::OPCODE_YIELD:GDFunction::OPCODE_YIELD_SIGNAL); // basic type constructor
  392. for(int i=0;i<arguments.size();i++)
  393. codegen.opcodes.push_back(arguments[i]); //arguments
  394. codegen.opcodes.push_back(GDFunction::OPCODE_YIELD_RESUME);
  395. //next will be where to place the result :)
  396. } break;
  397. //indexing operator
  398. case GDParser::OperatorNode::OP_INDEX:
  399. case GDParser::OperatorNode::OP_INDEX_NAMED: {
  400. ERR_FAIL_COND_V(on->arguments.size()!=2,-1);
  401. int slevel = p_stack_level;
  402. bool named=(on->op==GDParser::OperatorNode::OP_INDEX_NAMED);
  403. int from = _parse_expression(codegen,on->arguments[0],slevel);
  404. if (from<0)
  405. return from;
  406. int index;
  407. if (named) {
  408. #ifdef DEBUG_ENABLED
  409. if (on->arguments[0]->type==GDParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) {
  410. const Map<StringName,GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(static_cast<GDParser::IdentifierNode*>(on->arguments[1])->name);
  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. }
  417. #endif
  418. index=codegen.get_name_map_pos(static_cast<GDParser::IdentifierNode*>(on->arguments[1])->name);
  419. } else {
  420. if (on->arguments[1]->type==GDParser::Node::TYPE_CONSTANT && static_cast<const GDParser::ConstantNode*>(on->arguments[1])->value.get_type()==Variant::STRING) {
  421. //also, somehow, named (speed up anyway)
  422. StringName name = static_cast<const GDParser::ConstantNode*>(on->arguments[1])->value;
  423. index=codegen.get_name_map_pos(name);
  424. named=true;
  425. } else {
  426. //regular indexing
  427. if (from&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  428. slevel++;
  429. codegen.alloc_stack(slevel);
  430. }
  431. index = _parse_expression(codegen,on->arguments[1],slevel);
  432. if (index<0)
  433. return index;
  434. }
  435. }
  436. codegen.opcodes.push_back(named?GDFunction::OPCODE_GET_NAMED:GDFunction::OPCODE_GET); // perform operator
  437. codegen.opcodes.push_back(from); // argument 1
  438. codegen.opcodes.push_back(index); // argument 2 (unary only takes one parameter)
  439. } break;
  440. case GDParser::OperatorNode::OP_AND: {
  441. // AND operator with early out on failure
  442. int res = _parse_expression(codegen,on->arguments[0],p_stack_level);
  443. if (res<0)
  444. return res;
  445. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  446. codegen.opcodes.push_back(res);
  447. int jump_fail_pos=codegen.opcodes.size();
  448. codegen.opcodes.push_back(0);
  449. res = _parse_expression(codegen,on->arguments[1],p_stack_level);
  450. if (res<0)
  451. return res;
  452. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  453. codegen.opcodes.push_back(res);
  454. int jump_fail_pos2=codegen.opcodes.size();
  455. codegen.opcodes.push_back(0);
  456. codegen.alloc_stack(p_stack_level); //it will be used..
  457. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_TRUE);
  458. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  459. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  460. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  461. codegen.opcodes[jump_fail_pos]=codegen.opcodes.size();
  462. codegen.opcodes[jump_fail_pos2]=codegen.opcodes.size();
  463. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_FALSE);
  464. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  465. return p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS;
  466. } break;
  467. case GDParser::OperatorNode::OP_OR: {
  468. // OR operator with early out on success
  469. int res = _parse_expression(codegen,on->arguments[0],p_stack_level);
  470. if (res<0)
  471. return res;
  472. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF);
  473. codegen.opcodes.push_back(res);
  474. int jump_success_pos=codegen.opcodes.size();
  475. codegen.opcodes.push_back(0);
  476. res = _parse_expression(codegen,on->arguments[1],p_stack_level);
  477. if (res<0)
  478. return res;
  479. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF);
  480. codegen.opcodes.push_back(res);
  481. int jump_success_pos2=codegen.opcodes.size();
  482. codegen.opcodes.push_back(0);
  483. codegen.alloc_stack(p_stack_level); //it will be used..
  484. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_FALSE);
  485. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  486. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  487. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  488. codegen.opcodes[jump_success_pos]=codegen.opcodes.size();
  489. codegen.opcodes[jump_success_pos2]=codegen.opcodes.size();
  490. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_TRUE);
  491. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  492. return p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS;
  493. } break;
  494. //unary operators
  495. case GDParser::OperatorNode::OP_NEG: { if (!_create_unary_operator(codegen,on,Variant::OP_NEGATE,p_stack_level)) return -1;} break;
  496. case GDParser::OperatorNode::OP_NOT: { if (!_create_unary_operator(codegen,on,Variant::OP_NOT,p_stack_level)) return -1;} break;
  497. case GDParser::OperatorNode::OP_BIT_INVERT: { if (!_create_unary_operator(codegen,on,Variant::OP_BIT_NEGATE,p_stack_level)) return -1;} break;
  498. case GDParser::OperatorNode::OP_PREINC: { } break; //?
  499. case GDParser::OperatorNode::OP_PREDEC: { } break;
  500. case GDParser::OperatorNode::OP_INC: { } break;
  501. case GDParser::OperatorNode::OP_DEC: { } break;
  502. //binary operators (in precedence order)
  503. case GDParser::OperatorNode::OP_IN: { if (!_create_binary_operator(codegen,on,Variant::OP_IN,p_stack_level)) return -1;} break;
  504. case GDParser::OperatorNode::OP_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_EQUAL,p_stack_level)) return -1;} break;
  505. case GDParser::OperatorNode::OP_NOT_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_NOT_EQUAL,p_stack_level)) return -1;} break;
  506. case GDParser::OperatorNode::OP_LESS: { if (!_create_binary_operator(codegen,on,Variant::OP_LESS,p_stack_level)) return -1;} break;
  507. case GDParser::OperatorNode::OP_LESS_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_LESS_EQUAL,p_stack_level)) return -1;} break;
  508. case GDParser::OperatorNode::OP_GREATER: { if (!_create_binary_operator(codegen,on,Variant::OP_GREATER,p_stack_level)) return -1;} break;
  509. case GDParser::OperatorNode::OP_GREATER_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_GREATER_EQUAL,p_stack_level)) return -1;} break;
  510. case GDParser::OperatorNode::OP_ADD: { if (!_create_binary_operator(codegen,on,Variant::OP_ADD,p_stack_level)) return -1;} break;
  511. case GDParser::OperatorNode::OP_SUB: { if (!_create_binary_operator(codegen,on,Variant::OP_SUBSTRACT,p_stack_level)) return -1;} break;
  512. case GDParser::OperatorNode::OP_MUL: { if (!_create_binary_operator(codegen,on,Variant::OP_MULTIPLY,p_stack_level)) return -1;} break;
  513. case GDParser::OperatorNode::OP_DIV: { if (!_create_binary_operator(codegen,on,Variant::OP_DIVIDE,p_stack_level)) return -1;} break;
  514. case GDParser::OperatorNode::OP_MOD: { if (!_create_binary_operator(codegen,on,Variant::OP_MODULE,p_stack_level)) return -1;} break;
  515. //case GDParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
  516. //case GDParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
  517. case GDParser::OperatorNode::OP_BIT_AND: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_AND,p_stack_level)) return -1;} break;
  518. case GDParser::OperatorNode::OP_BIT_OR: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_OR,p_stack_level)) return -1;} break;
  519. case GDParser::OperatorNode::OP_BIT_XOR: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_XOR,p_stack_level)) return -1;} break;
  520. //shift
  521. case GDParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
  522. case GDParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
  523. //assignment operators
  524. case GDParser::OperatorNode::OP_ASSIGN_ADD:
  525. case GDParser::OperatorNode::OP_ASSIGN_SUB:
  526. case GDParser::OperatorNode::OP_ASSIGN_MUL:
  527. case GDParser::OperatorNode::OP_ASSIGN_DIV:
  528. case GDParser::OperatorNode::OP_ASSIGN_MOD:
  529. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  530. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  531. case GDParser::OperatorNode::OP_ASSIGN_BIT_AND:
  532. case GDParser::OperatorNode::OP_ASSIGN_BIT_OR:
  533. case GDParser::OperatorNode::OP_ASSIGN_BIT_XOR:
  534. case GDParser::OperatorNode::OP_INIT_ASSIGN:
  535. case GDParser::OperatorNode::OP_ASSIGN: {
  536. ERR_FAIL_COND_V(on->arguments.size()!=2,-1);
  537. 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)) {
  538. //SET (chained) MODE!!
  539. #ifdef DEBUG_ENABLED
  540. if (static_cast<GDParser::OperatorNode*>(on->arguments[0])->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  541. const GDParser::OperatorNode* inon = static_cast<GDParser::OperatorNode*>(on->arguments[0]);
  542. if (inon->arguments[0]->type==GDParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) {
  543. const Map<StringName,GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(static_cast<GDParser::IdentifierNode*>(inon->arguments[1])->name);
  544. if (MI && MI->get().setter==codegen.function_node->name) {
  545. String n = static_cast<GDParser::IdentifierNode*>(inon->arguments[1])->name;
  546. _set_error("Must use '"+n+"' instead of 'self."+n+"' in setter.",inon);
  547. return -1;
  548. }
  549. }
  550. }
  551. #endif
  552. int slevel=p_stack_level;
  553. GDParser::OperatorNode* op = static_cast<GDParser::OperatorNode*>(on->arguments[0]);
  554. /* Find chain of sets */
  555. List<GDParser::OperatorNode*> chain;
  556. {
  557. //create get/set chain
  558. GDParser::OperatorNode* n=op;
  559. while(true) {
  560. chain.push_back(n);
  561. if (n->arguments[0]->type!=GDParser::Node::TYPE_OPERATOR)
  562. break;
  563. n = static_cast<GDParser::OperatorNode*>(n->arguments[0]);
  564. if (n->op!=GDParser::OperatorNode::OP_INDEX && n->op!=GDParser::OperatorNode::OP_INDEX_NAMED)
  565. break;
  566. }
  567. }
  568. /* Chain of gets */
  569. //get at (potential) root stack pos, so it can be returned
  570. int prev_pos = _parse_expression(codegen,chain.back()->get()->arguments[0],slevel);
  571. if (prev_pos<0)
  572. return prev_pos;
  573. int retval=prev_pos;
  574. //print_line("retval: "+itos(retval));
  575. if (retval&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  576. slevel++;
  577. codegen.alloc_stack(slevel);
  578. }
  579. Vector<int> setchain;
  580. int prev_key_idx=-1;
  581. for(List<GDParser::OperatorNode*>::Element *E=chain.back();E;E=E->prev()) {
  582. if (E==chain.front()) //ignore first
  583. break;
  584. bool named = E->get()->op==GDParser::OperatorNode::OP_INDEX_NAMED;
  585. int key_idx;
  586. if (named) {
  587. key_idx = codegen.get_name_map_pos(static_cast<const GDParser::IdentifierNode*>(E->get()->arguments[1])->name);
  588. //printf("named key %x\n",key_idx);
  589. } else {
  590. if (prev_pos&(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)) {
  591. slevel++;
  592. codegen.alloc_stack(slevel);
  593. }
  594. GDParser::Node *key = E->get()->arguments[1];
  595. key_idx = _parse_expression(codegen,key,slevel);
  596. //printf("expr key %x\n",key_idx);
  597. //stack was raised here if retval was stack but..
  598. }
  599. if (key_idx<0)
  600. return key_idx;
  601. codegen.opcodes.push_back(named ? GDFunction::OPCODE_GET_NAMED : GDFunction::OPCODE_GET);
  602. codegen.opcodes.push_back(prev_pos);
  603. codegen.opcodes.push_back(key_idx);
  604. slevel++;
  605. codegen.alloc_stack(slevel);
  606. int dst_pos = (GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)|slevel;
  607. codegen.opcodes.push_back(dst_pos);
  608. //add in reverse order, since it will be reverted
  609. setchain.push_back(dst_pos);
  610. setchain.push_back(key_idx);
  611. setchain.push_back(prev_pos);
  612. setchain.push_back(named ? GDFunction::OPCODE_SET_NAMED : GDFunction::OPCODE_SET);
  613. prev_pos=dst_pos;
  614. prev_key_idx=key_idx;
  615. }
  616. setchain.invert();
  617. int set_index;
  618. bool named=false;
  619. if (static_cast<const GDParser::OperatorNode*>(op)->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  620. set_index=codegen.get_name_map_pos(static_cast<const GDParser::IdentifierNode*>(op->arguments[1])->name);
  621. named=true;
  622. } else {
  623. set_index = _parse_expression(codegen,op->arguments[1],slevel+1);
  624. named=false;
  625. }
  626. if (set_index<0)
  627. return set_index;
  628. if (set_index&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  629. slevel++;
  630. codegen.alloc_stack(slevel);
  631. }
  632. int set_value = _parse_assign_right_expression(codegen,on,slevel+1);
  633. if (set_value<0)
  634. return set_value;
  635. codegen.opcodes.push_back(named?GDFunction::OPCODE_SET_NAMED:GDFunction::OPCODE_SET);
  636. codegen.opcodes.push_back(prev_pos);
  637. codegen.opcodes.push_back(set_index);
  638. codegen.opcodes.push_back(set_value);
  639. for(int i=0;i<setchain.size();i+=4) {
  640. codegen.opcodes.push_back(setchain[i+0]);
  641. codegen.opcodes.push_back(setchain[i+1]);
  642. codegen.opcodes.push_back(setchain[i+2]);
  643. codegen.opcodes.push_back(setchain[i+3]);
  644. }
  645. return retval;
  646. } else {
  647. //ASSIGNMENT MODE!!
  648. int slevel = p_stack_level;
  649. int dst_address_a = _parse_expression(codegen,on->arguments[0],slevel,false,on->op==GDParser::OperatorNode::OP_INIT_ASSIGN);
  650. if (dst_address_a<0)
  651. return -1;
  652. if (dst_address_a&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  653. slevel++;
  654. codegen.alloc_stack(slevel);
  655. }
  656. int src_address_b = _parse_assign_right_expression(codegen,on,slevel);
  657. if (src_address_b<0)
  658. return -1;
  659. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN); // perform operator
  660. codegen.opcodes.push_back(dst_address_a); // argument 1
  661. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  662. return dst_address_a; //if anything, returns wathever was assigned or correct stack position
  663. }
  664. } break;
  665. case GDParser::OperatorNode::OP_EXTENDS: {
  666. ERR_FAIL_COND_V(on->arguments.size()!=2,false);
  667. int slevel = p_stack_level;
  668. int src_address_a = _parse_expression(codegen,on->arguments[0],slevel);
  669. if (src_address_a<0)
  670. return -1;
  671. if (src_address_a&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)
  672. slevel++; //uses stack for return, increase stack
  673. int src_address_b = _parse_expression(codegen,on->arguments[1],slevel);
  674. if (src_address_b<0)
  675. return -1;
  676. codegen.opcodes.push_back(GDFunction::OPCODE_EXTENDS_TEST); // perform operator
  677. codegen.opcodes.push_back(src_address_a); // argument 1
  678. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  679. } break;
  680. default: {
  681. ERR_EXPLAIN("Bug in bytecode compiler, unexpected operator #"+itos(on->op)+" in parse tree while parsing expression.");
  682. ERR_FAIL_V(0); //unreachable code
  683. } break;
  684. }
  685. int dst_addr=(p_stack_level)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  686. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  687. codegen.alloc_stack(p_stack_level);
  688. return dst_addr;
  689. } break;
  690. //TYPE_TYPE,
  691. default: {
  692. ERR_EXPLAIN("Bug in bytecode compiler, unexpected node in parse tree while parsing expression.");
  693. ERR_FAIL_V(-1); //unreachable code
  694. } break;
  695. }
  696. ERR_FAIL_V(-1); //unreachable code
  697. }
  698. Error GDCompiler::_parse_block(CodeGen& codegen,const GDParser::BlockNode *p_block,int p_stack_level,int p_break_addr,int p_continue_addr) {
  699. codegen.push_stack_identifiers();
  700. int new_identifiers=0;
  701. codegen.current_line=p_block->line;
  702. for(int i=0;i<p_block->statements.size();i++) {
  703. const GDParser::Node *s = p_block->statements[i];
  704. switch(s->type) {
  705. case GDParser::Node::TYPE_NEWLINE: {
  706. const GDParser::NewLineNode *nl = static_cast<const GDParser::NewLineNode*>(s);
  707. codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
  708. codegen.opcodes.push_back(nl->line);
  709. codegen.current_line=nl->line;
  710. } break;
  711. case GDParser::Node::TYPE_CONTROL_FLOW: {
  712. // try subblocks
  713. const GDParser::ControlFlowNode *cf = static_cast<const GDParser::ControlFlowNode*>(s);
  714. switch(cf->cf_type) {
  715. case GDParser::ControlFlowNode::CF_IF: {
  716. #ifdef DEBUG_ENABLED
  717. codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
  718. codegen.opcodes.push_back(cf->line);
  719. codegen.current_line=cf->line;
  720. #endif
  721. int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  722. if (ret<0)
  723. return ERR_PARSE_ERROR;
  724. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  725. codegen.opcodes.push_back(ret);
  726. int else_addr=codegen.opcodes.size();
  727. codegen.opcodes.push_back(0); //temporary
  728. Error err = _parse_block(codegen,cf->body,p_stack_level,p_break_addr,p_continue_addr);
  729. if (err)
  730. return err;
  731. if (cf->body_else) {
  732. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  733. int end_addr=codegen.opcodes.size();
  734. codegen.opcodes.push_back(0);
  735. codegen.opcodes[else_addr]=codegen.opcodes.size();
  736. Error err = _parse_block(codegen,cf->body_else,p_stack_level,p_break_addr,p_continue_addr);
  737. if (err)
  738. return err;
  739. codegen.opcodes[end_addr]=codegen.opcodes.size();
  740. } else {
  741. //end without else
  742. codegen.opcodes[else_addr]=codegen.opcodes.size();
  743. }
  744. } break;
  745. case GDParser::ControlFlowNode::CF_FOR: {
  746. int slevel=p_stack_level;
  747. int iter_stack_pos=slevel;
  748. int iterator_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  749. int counter_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  750. int container_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  751. codegen.alloc_stack(slevel);
  752. codegen.push_stack_identifiers();
  753. codegen.add_stack_identifier(static_cast<const GDParser::IdentifierNode*>(cf->arguments[0])->name,iter_stack_pos);
  754. int ret = _parse_expression(codegen,cf->arguments[1],slevel,false);
  755. if (ret<0)
  756. return ERR_COMPILATION_FAILED;
  757. //assign container
  758. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN);
  759. codegen.opcodes.push_back(container_pos);
  760. codegen.opcodes.push_back(ret);
  761. //begin loop
  762. codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE_BEGIN);
  763. codegen.opcodes.push_back(counter_pos);
  764. codegen.opcodes.push_back(container_pos);
  765. codegen.opcodes.push_back(codegen.opcodes.size()+4);
  766. codegen.opcodes.push_back(iterator_pos);
  767. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
  768. codegen.opcodes.push_back(codegen.opcodes.size()+8);
  769. //break loop
  770. int break_pos=codegen.opcodes.size();
  771. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
  772. codegen.opcodes.push_back(0); //skip code for next
  773. //next loop
  774. int continue_pos=codegen.opcodes.size();
  775. codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE);
  776. codegen.opcodes.push_back(counter_pos);
  777. codegen.opcodes.push_back(container_pos);
  778. codegen.opcodes.push_back(break_pos);
  779. codegen.opcodes.push_back(iterator_pos);
  780. Error err = _parse_block(codegen,cf->body,slevel,break_pos,continue_pos);
  781. if (err)
  782. return err;
  783. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  784. codegen.opcodes.push_back(continue_pos);
  785. codegen.opcodes[break_pos+1]=codegen.opcodes.size();
  786. codegen.pop_stack_identifiers();
  787. } break;
  788. case GDParser::ControlFlowNode::CF_WHILE: {
  789. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  790. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  791. int break_addr=codegen.opcodes.size();
  792. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  793. codegen.opcodes.push_back(0);
  794. int continue_addr=codegen.opcodes.size();
  795. int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  796. if (ret<0)
  797. return ERR_PARSE_ERROR;
  798. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  799. codegen.opcodes.push_back(ret);
  800. codegen.opcodes.push_back(break_addr);
  801. Error err = _parse_block(codegen,cf->body,p_stack_level,break_addr,continue_addr);
  802. if (err)
  803. return err;
  804. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  805. codegen.opcodes.push_back(continue_addr);
  806. codegen.opcodes[break_addr+1]=codegen.opcodes.size();
  807. } break;
  808. case GDParser::ControlFlowNode::CF_SWITCH: {
  809. } break;
  810. case GDParser::ControlFlowNode::CF_BREAK: {
  811. if (p_break_addr<0) {
  812. _set_error("'break'' not within loop",cf);
  813. return ERR_COMPILATION_FAILED;
  814. }
  815. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  816. codegen.opcodes.push_back(p_break_addr);
  817. } break;
  818. case GDParser::ControlFlowNode::CF_CONTINUE: {
  819. if (p_continue_addr<0) {
  820. _set_error("'continue' not within loop",cf);
  821. return ERR_COMPILATION_FAILED;
  822. }
  823. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  824. codegen.opcodes.push_back(p_continue_addr);
  825. } break;
  826. case GDParser::ControlFlowNode::CF_RETURN: {
  827. int ret;
  828. if (cf->arguments.size()) {
  829. ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  830. if (ret<0)
  831. return ERR_PARSE_ERROR;
  832. } else {
  833. ret=GDFunction::ADDR_TYPE_NIL << GDFunction::ADDR_BITS;
  834. }
  835. codegen.opcodes.push_back(GDFunction::OPCODE_RETURN);
  836. codegen.opcodes.push_back(ret);
  837. } break;
  838. }
  839. } break;
  840. case GDParser::Node::TYPE_ASSERT: {
  841. // try subblocks
  842. const GDParser::AssertNode *as = static_cast<const GDParser::AssertNode*>(s);
  843. int ret = _parse_expression(codegen,as->condition,p_stack_level,false);
  844. if (ret<0)
  845. return ERR_PARSE_ERROR;
  846. codegen.opcodes.push_back(GDFunction::OPCODE_ASSERT);
  847. codegen.opcodes.push_back(ret);
  848. } break;
  849. case GDParser::Node::TYPE_BREAKPOINT: {
  850. // try subblocks
  851. codegen.opcodes.push_back(GDFunction::OPCODE_BREAKPOINT);
  852. } break;
  853. case GDParser::Node::TYPE_LOCAL_VAR: {
  854. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode*>(s);
  855. codegen.add_stack_identifier(lv->name,p_stack_level++);
  856. codegen.alloc_stack(p_stack_level);
  857. new_identifiers++;
  858. } break;
  859. default: {
  860. //expression
  861. int ret = _parse_expression(codegen,s,p_stack_level,true);
  862. if (ret<0)
  863. return ERR_PARSE_ERROR;
  864. } break;
  865. }
  866. }
  867. codegen.pop_stack_identifiers();
  868. return OK;
  869. }
  870. Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *p_class,const GDParser::FunctionNode *p_func,bool p_for_ready) {
  871. Vector<int> bytecode;
  872. CodeGen codegen;
  873. codegen.class_node=p_class;
  874. codegen.script=p_script;
  875. codegen.function_node=p_func;
  876. codegen.stack_max=0;
  877. codegen.current_line=0;
  878. codegen.call_max=0;
  879. codegen.debug_stack=ScriptDebugger::get_singleton()!=NULL;
  880. Vector<StringName> argnames;
  881. int stack_level=0;
  882. if (p_func) {
  883. for(int i=0;i<p_func->arguments.size();i++) {
  884. int idx = i;
  885. codegen.add_stack_identifier(p_func->arguments[i],i);
  886. #ifdef TOOLS_ENABLED
  887. argnames.push_back(p_func->arguments[i]);
  888. #endif
  889. }
  890. stack_level=p_func->arguments.size();
  891. }
  892. codegen.alloc_stack(stack_level);
  893. /* Parse initializer -if applies- */
  894. bool is_initializer=!p_for_ready && !p_func;
  895. if (is_initializer || (p_func && String(p_func->name)=="_init")) {
  896. //parse initializer for class members
  897. if (!p_func && p_class->extends_used && p_script->native.is_null()){
  898. //call implicit parent constructor
  899. codegen.opcodes.push_back(GDFunction::OPCODE_CALL_SELF_BASE);
  900. codegen.opcodes.push_back(codegen.get_name_map_pos("_init"));
  901. codegen.opcodes.push_back(0);
  902. codegen.opcodes.push_back((GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)|0);
  903. }
  904. Error err = _parse_block(codegen,p_class->initializer,stack_level);
  905. if (err)
  906. return err;
  907. is_initializer=true;
  908. }
  909. if (p_for_ready || (p_func && String(p_func->name)=="_ready")) {
  910. //parse initializer for class members
  911. if (p_class->ready->statements.size()) {
  912. Error err = _parse_block(codegen,p_class->ready,stack_level);
  913. if (err)
  914. return err;
  915. }
  916. }
  917. /* Parse default argument code -if applies- */
  918. Vector<int> defarg_addr;
  919. StringName func_name;
  920. if (p_func) {
  921. if (p_func->default_values.size()) {
  922. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  923. defarg_addr.push_back(codegen.opcodes.size());
  924. for(int i=0;i<p_func->default_values.size();i++) {
  925. _parse_expression(codegen,p_func->default_values[i],stack_level,true);
  926. defarg_addr.push_back(codegen.opcodes.size());
  927. }
  928. defarg_addr.invert();
  929. }
  930. Error err = _parse_block(codegen,p_func->body,stack_level);
  931. if (err)
  932. return err;
  933. func_name=p_func->name;
  934. } else {
  935. if (p_for_ready)
  936. func_name="_ready";
  937. else
  938. func_name="_init";
  939. }
  940. codegen.opcodes.push_back(GDFunction::OPCODE_END);
  941. GDFunction *gdfunc=NULL;
  942. //if (String(p_func->name)=="") { //initializer func
  943. // gdfunc = &p_script->initializer;
  944. //} else { //regular func
  945. p_script->member_functions[func_name]=memnew(GDFunction);
  946. gdfunc = p_script->member_functions[func_name];
  947. //}
  948. if (p_func)
  949. gdfunc->_static=p_func->_static;
  950. #ifdef TOOLS_ENABLED
  951. gdfunc->arg_names=argnames;
  952. #endif
  953. //constants
  954. if (codegen.constant_map.size()) {
  955. gdfunc->_constant_count=codegen.constant_map.size();
  956. gdfunc->constants.resize(codegen.constant_map.size());
  957. gdfunc->_constants_ptr=&gdfunc->constants[0];
  958. const Variant *K=NULL;
  959. while((K=codegen.constant_map.next(K))) {
  960. int idx = codegen.constant_map[*K];
  961. gdfunc->constants[idx]=*K;
  962. }
  963. } else {
  964. gdfunc->_constants_ptr=NULL;
  965. gdfunc->_constant_count=0;
  966. }
  967. //global names
  968. if (codegen.name_map.size()) {
  969. gdfunc->global_names.resize(codegen.name_map.size());
  970. gdfunc->_global_names_ptr = &gdfunc->global_names[0];
  971. for(Map<StringName,int>::Element *E=codegen.name_map.front();E;E=E->next()) {
  972. gdfunc->global_names[E->get()]=E->key();
  973. }
  974. gdfunc->_global_names_count=gdfunc->global_names.size();
  975. } else {
  976. gdfunc->_global_names_ptr = NULL;
  977. gdfunc->_global_names_count =0;
  978. }
  979. if (codegen.opcodes.size()) {
  980. gdfunc->code=codegen.opcodes;
  981. gdfunc->_code_ptr=&gdfunc->code[0];
  982. gdfunc->_code_size=codegen.opcodes.size();
  983. } else {
  984. gdfunc->_code_ptr=NULL;
  985. gdfunc->_code_size=0;
  986. }
  987. if (defarg_addr.size()) {
  988. gdfunc->default_arguments=defarg_addr;
  989. gdfunc->_default_arg_count=defarg_addr.size()-1;
  990. gdfunc->_default_arg_ptr=&gdfunc->default_arguments[0];
  991. } else {
  992. gdfunc->_default_arg_count=0;
  993. gdfunc->_default_arg_ptr=NULL;
  994. }
  995. gdfunc->_argument_count=p_func ? p_func->arguments.size() : 0;
  996. gdfunc->_stack_size=codegen.stack_max;
  997. gdfunc->_call_size=codegen.call_max;
  998. gdfunc->name=func_name;
  999. #ifdef DEBUG_ENABLED
  1000. if (ScriptDebugger::get_singleton()){
  1001. String signature;
  1002. //path
  1003. if (p_script->get_path()!=String())
  1004. signature+=p_script->get_path();
  1005. //loc
  1006. if (p_func) {
  1007. signature+="::"+itos(p_func->body->line);
  1008. } else {
  1009. signature+="::0";
  1010. }
  1011. //funciton and class
  1012. if (p_class->name) {
  1013. signature+="::"+String(p_class->name)+"."+String(func_name);;
  1014. } else {
  1015. signature+="::"+String(func_name);
  1016. }
  1017. gdfunc->profile.signature=signature;
  1018. }
  1019. #endif
  1020. gdfunc->_script=p_script;
  1021. gdfunc->source=source;
  1022. #ifdef DEBUG_ENABLED
  1023. {
  1024. gdfunc->func_cname=(String(source)+" - "+String(func_name)).utf8();
  1025. gdfunc->_func_cname=gdfunc->func_cname.get_data();
  1026. }
  1027. #endif
  1028. if (p_func) {
  1029. gdfunc->_initial_line=p_func->line;
  1030. } else {
  1031. gdfunc->_initial_line=0;
  1032. }
  1033. if (codegen.debug_stack)
  1034. gdfunc->stack_debug=codegen.stack_debug;
  1035. if (is_initializer)
  1036. p_script->initializer=gdfunc;
  1037. return OK;
  1038. }
  1039. Error GDCompiler::_parse_class(GDScript *p_script, GDScript *p_owner, const GDParser::ClassNode *p_class, bool p_keep_state) {
  1040. Map<StringName,Ref<GDScript> > old_subclasses;
  1041. if (p_keep_state) {
  1042. old_subclasses=p_script->subclasses;
  1043. }
  1044. p_script->native=Ref<GDNativeClass>();
  1045. p_script->base=Ref<GDScript>();
  1046. p_script->_base=NULL;
  1047. p_script->members.clear();
  1048. p_script->constants.clear();
  1049. for (Map<StringName,GDFunction*>::Element *E=p_script->member_functions.front();E;E=E->next()) {
  1050. memdelete(E->get());
  1051. }
  1052. p_script->member_functions.clear();
  1053. p_script->member_indices.clear();
  1054. p_script->member_info.clear();
  1055. p_script->initializer=NULL;
  1056. p_script->subclasses.clear();
  1057. p_script->_owner=p_owner;
  1058. p_script->tool=p_class->tool;
  1059. p_script->name=p_class->name;
  1060. int index_from=0;
  1061. Ref<GDNativeClass> native;
  1062. if (p_class->extends_used) {
  1063. //do inheritance
  1064. String path = p_class->extends_file;
  1065. Ref<GDScript> script;
  1066. if (path!="") {
  1067. //path (and optionally subclasses)
  1068. if (path.is_rel_path()) {
  1069. String base;
  1070. if (p_owner) {
  1071. GDScript *current_class = p_owner;
  1072. while (current_class != NULL) {
  1073. base=current_class->get_path();
  1074. if (base=="")
  1075. current_class = current_class->_owner;
  1076. else
  1077. break;
  1078. }
  1079. }
  1080. else {
  1081. base = p_script->get_path();
  1082. }
  1083. if (base=="" || base.is_rel_path()) {
  1084. _set_error("Could not resolve relative path for parent class: "+path,p_class);
  1085. return ERR_FILE_NOT_FOUND;
  1086. }
  1087. path=base.get_base_dir().plus_file(path).simplify_path();
  1088. }
  1089. script = ResourceLoader::load(path);
  1090. if (script.is_null()) {
  1091. _set_error("Could not load base class: "+path,p_class);
  1092. return ERR_FILE_NOT_FOUND;
  1093. }
  1094. if (!script->valid) {
  1095. _set_error("Script not fully loaded (cyclic preload?): "+path,p_class);
  1096. return ERR_BUSY;
  1097. }
  1098. //print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
  1099. if (p_class->extends_class.size()) {
  1100. for(int i=0;i<p_class->extends_class.size();i++) {
  1101. String sub = p_class->extends_class[i];
  1102. if (script->subclasses.has(sub)) {
  1103. script=script->subclasses[sub];
  1104. } else {
  1105. _set_error("Could not find subclass: "+sub,p_class);
  1106. return ERR_FILE_NOT_FOUND;
  1107. }
  1108. }
  1109. }
  1110. } else {
  1111. ERR_FAIL_COND_V(p_class->extends_class.size()==0,ERR_BUG);
  1112. //look around for the subclasses
  1113. String base=p_class->extends_class[0];
  1114. GDScript *p = p_owner;
  1115. Ref<GDScript> base_class;
  1116. while(p) {
  1117. if (p->subclasses.has(base)) {
  1118. base_class=p->subclasses[base];
  1119. break;
  1120. }
  1121. p=p->_owner;
  1122. }
  1123. if (base_class.is_valid()) {
  1124. for(int i=1;i<p_class->extends_class.size();i++) {
  1125. String subclass=p_class->extends_class[i];
  1126. if (base_class->subclasses.has(subclass)) {
  1127. base_class=base_class->subclasses[subclass];
  1128. } else {
  1129. _set_error("Could not find subclass: "+subclass,p_class);
  1130. return ERR_FILE_NOT_FOUND;
  1131. }
  1132. }
  1133. script=base_class;
  1134. } else {
  1135. if (p_class->extends_class.size()>1) {
  1136. _set_error("Invalid inheritance (unknown class+subclasses)",p_class);
  1137. return ERR_FILE_NOT_FOUND;
  1138. }
  1139. //if not found, try engine classes
  1140. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  1141. _set_error("Unknown class: '"+base+"'",p_class);
  1142. return ERR_FILE_NOT_FOUND;
  1143. }
  1144. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  1145. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  1146. if (!native.is_valid()) {
  1147. _set_error("Global not a class: '"+base+"'",p_class);
  1148. return ERR_FILE_NOT_FOUND;
  1149. }
  1150. }
  1151. }
  1152. if (script.is_valid()) {
  1153. p_script->base=script;
  1154. p_script->_base=p_script->base.ptr();
  1155. p_script->member_indices=script->member_indices;
  1156. } else if (native.is_valid()) {
  1157. p_script->native=native;
  1158. } else {
  1159. _set_error("Could not determine inheritance",p_class);
  1160. return ERR_FILE_NOT_FOUND;
  1161. }
  1162. }
  1163. //print_line("Script: "+p_script->get_path()+" indices: "+itos(p_script->member_indices.size()));
  1164. for(int i=0;i<p_class->variables.size();i++) {
  1165. StringName name = p_class->variables[i].identifier;
  1166. if (p_script->member_indices.has(name)) {
  1167. _set_error("Member '"+name+"' already exists (in current or parent class)",p_class);
  1168. return ERR_ALREADY_EXISTS;
  1169. }
  1170. if (p_class->variables[i]._export.type!=Variant::NIL) {
  1171. p_script->member_info[name]=p_class->variables[i]._export;
  1172. #ifdef TOOLS_ENABLED
  1173. if (p_class->variables[i].default_value.get_type()!=Variant::NIL) {
  1174. p_script->member_default_values[name]=p_class->variables[i].default_value;
  1175. }
  1176. #endif
  1177. }
  1178. //int new_idx = p_script->member_indices.size();
  1179. GDScript::MemberInfo minfo;
  1180. minfo.index = p_script->member_indices.size();
  1181. minfo.setter = p_class->variables[i].setter;
  1182. minfo.getter = p_class->variables[i].getter;
  1183. p_script->member_indices[name]=minfo;
  1184. p_script->members.insert(name);
  1185. }
  1186. for(int i=0;i<p_class->constant_expressions.size();i++) {
  1187. StringName name = p_class->constant_expressions[i].identifier;
  1188. ERR_CONTINUE( p_class->constant_expressions[i].expression->type!=GDParser::Node::TYPE_CONSTANT );
  1189. GDParser::ConstantNode *constant = static_cast<GDParser::ConstantNode*>(p_class->constant_expressions[i].expression);
  1190. p_script->constants.insert(name,constant->value);
  1191. //p_script->constants[constant->value].make_const();
  1192. }
  1193. for(int i=0;i<p_class->_signals.size();i++) {
  1194. StringName name = p_class->_signals[i].name;
  1195. GDScript *c = p_script;
  1196. while(c) {
  1197. if (c->_signals.has(name)) {
  1198. _set_error("Signal '"+name+"' redefined (in current or parent class)",p_class);
  1199. return ERR_ALREADY_EXISTS;
  1200. }
  1201. if (c->base.is_valid()) {
  1202. c=c->base.ptr();
  1203. } else {
  1204. c=NULL;
  1205. }
  1206. }
  1207. if (native.is_valid()) {
  1208. if (ObjectTypeDB::has_signal(native->get_name(),name)) {
  1209. _set_error("Signal '"+name+"' redefined (original in native class '"+String(native->get_name())+"')",p_class);
  1210. return ERR_ALREADY_EXISTS;
  1211. }
  1212. }
  1213. p_script->_signals[name]=p_class->_signals[i].arguments;
  1214. }
  1215. //parse sub-classes
  1216. for(int i=0;i<p_class->subclasses.size();i++) {
  1217. StringName name = p_class->subclasses[i]->name;
  1218. Ref<GDScript> subclass;
  1219. if (old_subclasses.has(name)) {
  1220. subclass=old_subclasses[name];
  1221. } else {
  1222. subclass.instance();
  1223. }
  1224. Error err = _parse_class(subclass.ptr(),p_script,p_class->subclasses[i],p_keep_state);
  1225. if (err)
  1226. return err;
  1227. p_script->constants.insert(name,subclass); //once parsed, goes to the list of constants
  1228. p_script->subclasses.insert(name,subclass);
  1229. }
  1230. //parse methods
  1231. bool has_initializer=false;
  1232. bool has_ready=false;
  1233. for(int i=0;i<p_class->functions.size();i++) {
  1234. if (!has_initializer && p_class->functions[i]->name=="_init")
  1235. has_initializer=true;
  1236. if (!has_ready && p_class->functions[i]->name=="_ready")
  1237. has_ready=true;
  1238. Error err = _parse_function(p_script,p_class,p_class->functions[i]);
  1239. if (err)
  1240. return err;
  1241. }
  1242. //parse static methods
  1243. for(int i=0;i<p_class->static_functions.size();i++) {
  1244. Error err = _parse_function(p_script,p_class,p_class->static_functions[i]);
  1245. if (err)
  1246. return err;
  1247. }
  1248. if (!has_initializer) {
  1249. //create a constructor
  1250. Error err = _parse_function(p_script,p_class,NULL);
  1251. if (err)
  1252. return err;
  1253. }
  1254. if (!has_ready && p_class->ready->statements.size()) {
  1255. //create a constructor
  1256. Error err = _parse_function(p_script,p_class,NULL,true);
  1257. if (err)
  1258. return err;
  1259. }
  1260. #ifdef DEBUG_ENABLED
  1261. //validate setters/getters if debug is enabled
  1262. for(int i=0;i<p_class->variables.size();i++) {
  1263. if (p_class->variables[i].setter) {
  1264. const Map<StringName,GDFunction*>::Element *E=p_script->get_member_functions().find(p_class->variables[i].setter);
  1265. if (!E) {
  1266. _set_error("Setter function '"+String(p_class->variables[i].setter)+"' not found in class.",NULL);
  1267. err_line=p_class->variables[i].line;
  1268. err_column=0;
  1269. return ERR_PARSE_ERROR;
  1270. }
  1271. if (E->get()->is_static()) {
  1272. _set_error("Setter function '"+String(p_class->variables[i].setter)+"' is static.",NULL);
  1273. err_line=p_class->variables[i].line;
  1274. err_column=0;
  1275. return ERR_PARSE_ERROR;
  1276. }
  1277. }
  1278. if (p_class->variables[i].getter) {
  1279. const Map<StringName,GDFunction*>::Element *E=p_script->get_member_functions().find(p_class->variables[i].getter);
  1280. if (!E) {
  1281. _set_error("Getter function '"+String(p_class->variables[i].getter)+"' not found in class.",NULL);
  1282. err_line=p_class->variables[i].line;
  1283. err_column=0;
  1284. return ERR_PARSE_ERROR;
  1285. }
  1286. if (E->get()->is_static()) {
  1287. _set_error("Getter function '"+String(p_class->variables[i].getter)+"' is static.",NULL);
  1288. err_line=p_class->variables[i].line;
  1289. err_column=0;
  1290. return ERR_PARSE_ERROR;
  1291. }
  1292. }
  1293. }
  1294. //validate instances if keeping state
  1295. if (p_keep_state) {
  1296. print_line("RELOAD KEEP "+p_script->path);
  1297. for (Set<Object*>::Element *E=p_script->instances.front();E;) {
  1298. Set<Object*>::Element *N = E->next();
  1299. ScriptInstance *si = E->get()->get_script_instance();
  1300. if (si->is_placeholder()) {
  1301. #ifdef TOOLS_ENABLED
  1302. PlaceHolderScriptInstance *psi = static_cast<PlaceHolderScriptInstance*>(si);
  1303. if (p_script->is_tool()) {
  1304. //re-create as an instance
  1305. p_script->placeholders.erase(psi); //remove placeholder
  1306. GDInstance* instance = memnew( GDInstance );
  1307. instance->base_ref=E->get()->cast_to<Reference>();
  1308. instance->members.resize(p_script->member_indices.size());
  1309. instance->script=Ref<GDScript>(p_script);
  1310. instance->owner=E->get();
  1311. //needed for hot reloading
  1312. for(Map<StringName,GDScript::MemberInfo>::Element *E=p_script->member_indices.front();E;E=E->next()) {
  1313. instance->member_indices_cache[E->key()]=E->get().index;
  1314. }
  1315. instance->owner->set_script_instance(instance);
  1316. /* STEP 2, INITIALIZE AND CONSRTUCT */
  1317. Variant::CallError ce;
  1318. p_script->initializer->call(instance,NULL,0,ce);
  1319. if (ce.error!=Variant::CallError::CALL_OK) {
  1320. //well, tough luck, not goinna do anything here
  1321. }
  1322. }
  1323. #endif
  1324. } else {
  1325. GDInstance *gi = static_cast<GDInstance*>(si);
  1326. gi->reload_members();
  1327. }
  1328. E=N;
  1329. }
  1330. }
  1331. #endif
  1332. p_script->valid=true;
  1333. return OK;
  1334. }
  1335. Error GDCompiler::compile(const GDParser *p_parser,GDScript *p_script,bool p_keep_state) {
  1336. err_line=-1;
  1337. err_column=-1;
  1338. error="";
  1339. parser=p_parser;
  1340. const GDParser::Node* root = parser->get_parse_tree();
  1341. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,ERR_INVALID_DATA);
  1342. source=p_script->get_path();
  1343. Error err = _parse_class(p_script,NULL,static_cast<const GDParser::ClassNode*>(root),p_keep_state);
  1344. if (err)
  1345. return err;
  1346. return OK;
  1347. }
  1348. String GDCompiler::get_error() const {
  1349. return error;
  1350. }
  1351. int GDCompiler::get_error_line() const{
  1352. return err_line;
  1353. }
  1354. int GDCompiler::get_error_column() const{
  1355. return err_column;
  1356. }
  1357. GDCompiler::GDCompiler()
  1358. {
  1359. }