gdscript_compiler.cpp 77 KB

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