gdscript_compiler.cpp 76 KB

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