gdscript_compiler.cpp 76 KB

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