gdscript_compiler.cpp 76 KB

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