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