gdscript_compiler.cpp 76 KB

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