2
0

gdscript_compiler.cpp 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984
  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->is_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->start_line;
  60. err_column = p_node->leftmost_column;
  61. } else {
  62. err_line = 0;
  63. err_column = 0;
  64. }
  65. }
  66. bool GDScriptCompiler::_create_unary_operator(CodeGen &codegen, const GDScriptParser::UnaryOpNode *on, Variant::Operator op, int p_stack_level) {
  67. int src_address_a = _parse_expression(codegen, on->operand, p_stack_level);
  68. if (src_address_a < 0) {
  69. return false;
  70. }
  71. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); // perform operator
  72. codegen.opcodes.push_back(op); //which operator
  73. codegen.opcodes.push_back(src_address_a); // argument 1
  74. codegen.opcodes.push_back(src_address_a); // argument 2 (repeated)
  75. //codegen.opcodes.push_back(GDScriptFunction::ADDR_TYPE_NIL); // argument 2 (unary only takes one parameter)
  76. return true;
  77. }
  78. bool GDScriptCompiler::_create_binary_operator(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_left_operand, const GDScriptParser::ExpressionNode *p_right_operand, Variant::Operator op, int p_stack_level, bool p_initializer, int p_index_addr) {
  79. int src_address_a = _parse_expression(codegen, p_left_operand, p_stack_level, false, p_initializer, p_index_addr);
  80. if (src_address_a < 0) {
  81. return false;
  82. }
  83. if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  84. p_stack_level++; //uses stack for return, increase stack
  85. }
  86. int src_address_b = _parse_expression(codegen, p_right_operand, p_stack_level, false, p_initializer);
  87. if (src_address_b < 0) {
  88. return false;
  89. }
  90. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); // perform operator
  91. codegen.opcodes.push_back(op); //which operator
  92. codegen.opcodes.push_back(src_address_a); // argument 1
  93. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  94. return true;
  95. }
  96. bool GDScriptCompiler::_create_binary_operator(CodeGen &codegen, const GDScriptParser::BinaryOpNode *on, Variant::Operator op, int p_stack_level, bool p_initializer, int p_index_addr) {
  97. return _create_binary_operator(codegen, on->left_operand, on->right_operand, op, p_stack_level, p_initializer, p_index_addr);
  98. }
  99. GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::DataType &p_datatype) const {
  100. if (!p_datatype.is_set()) {
  101. return GDScriptDataType();
  102. }
  103. GDScriptDataType result;
  104. result.has_type = true;
  105. switch (p_datatype.kind) {
  106. case GDScriptParser::DataType::BUILTIN: {
  107. result.kind = GDScriptDataType::BUILTIN;
  108. result.builtin_type = p_datatype.builtin_type;
  109. } break;
  110. case GDScriptParser::DataType::NATIVE: {
  111. result.kind = GDScriptDataType::NATIVE;
  112. result.native_type = p_datatype.native_type;
  113. } break;
  114. case GDScriptParser::DataType::SCRIPT: {
  115. result.kind = GDScriptDataType::SCRIPT;
  116. result.script_type = p_datatype.script_type;
  117. result.native_type = result.script_type->get_instance_base_type();
  118. } break;
  119. case GDScriptParser::DataType::CLASS: {
  120. // Locate class by constructing the path to it and following that path
  121. GDScriptParser::ClassNode *class_type = p_datatype.class_type;
  122. if (class_type) {
  123. List<StringName> names;
  124. while (class_type->outer) {
  125. names.push_back(class_type->identifier->name);
  126. class_type = class_type->outer;
  127. }
  128. Ref<GDScript> script = Ref<GDScript>(main_script);
  129. while (names.back()) {
  130. if (!script->subclasses.has(names.back()->get())) {
  131. ERR_PRINT("Parser bug: Cannot locate datatype class.");
  132. result.has_type = false;
  133. return GDScriptDataType();
  134. }
  135. script = script->subclasses[names.back()->get()];
  136. names.pop_back();
  137. }
  138. result.kind = GDScriptDataType::GDSCRIPT;
  139. result.script_type = script;
  140. result.native_type = script->get_instance_base_type();
  141. } else {
  142. result.kind = GDScriptDataType::GDSCRIPT;
  143. result.script_type = p_datatype.script_type;
  144. result.native_type = result.script_type->get_instance_base_type();
  145. }
  146. } break;
  147. case GDScriptParser::DataType::UNRESOLVED: {
  148. ERR_PRINT("Parser bug: converting unresolved type.");
  149. return GDScriptDataType();
  150. }
  151. default:
  152. break; // FIXME
  153. }
  154. return result;
  155. }
  156. int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::AssignmentNode *p_assignment, int p_stack_level, int p_index_addr) {
  157. Variant::Operator var_op = Variant::OP_MAX;
  158. switch (p_assignment->operation) {
  159. case GDScriptParser::AssignmentNode::OP_ADDITION:
  160. var_op = Variant::OP_ADD;
  161. break;
  162. case GDScriptParser::AssignmentNode::OP_SUBTRACTION:
  163. var_op = Variant::OP_SUBTRACT;
  164. break;
  165. case GDScriptParser::AssignmentNode::OP_MULTIPLICATION:
  166. var_op = Variant::OP_MULTIPLY;
  167. break;
  168. case GDScriptParser::AssignmentNode::OP_DIVISION:
  169. var_op = Variant::OP_DIVIDE;
  170. break;
  171. case GDScriptParser::AssignmentNode::OP_MODULO:
  172. var_op = Variant::OP_MODULE;
  173. break;
  174. case GDScriptParser::AssignmentNode::OP_BIT_SHIFT_LEFT:
  175. var_op = Variant::OP_SHIFT_LEFT;
  176. break;
  177. case GDScriptParser::AssignmentNode::OP_BIT_SHIFT_RIGHT:
  178. var_op = Variant::OP_SHIFT_RIGHT;
  179. break;
  180. case GDScriptParser::AssignmentNode::OP_BIT_AND:
  181. var_op = Variant::OP_BIT_AND;
  182. break;
  183. case GDScriptParser::AssignmentNode::OP_BIT_OR:
  184. var_op = Variant::OP_BIT_OR;
  185. break;
  186. case GDScriptParser::AssignmentNode::OP_BIT_XOR:
  187. var_op = Variant::OP_BIT_XOR;
  188. break;
  189. case GDScriptParser::AssignmentNode::OP_NONE: {
  190. //none
  191. } break;
  192. default: {
  193. ERR_FAIL_V(-1);
  194. }
  195. }
  196. // bool initializer = p_expression->op == GDScriptParser::OperatorNode::OP_INIT_ASSIGN;
  197. if (var_op == Variant::OP_MAX) {
  198. return _parse_expression(codegen, p_assignment->assigned_value, p_stack_level, false, false);
  199. }
  200. if (!_create_binary_operator(codegen, p_assignment->assignee, p_assignment->assigned_value, var_op, p_stack_level, false, p_index_addr)) {
  201. return -1;
  202. }
  203. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  204. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  205. codegen.alloc_stack(p_stack_level);
  206. return dst_addr;
  207. }
  208. int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_expression, int p_stack_level, bool p_root, bool p_initializer, int p_index_addr) {
  209. if (p_expression->is_constant) {
  210. return codegen.get_constant_pos(p_expression->reduced_value);
  211. }
  212. switch (p_expression->type) {
  213. //should parse variable declaration and adjust stack accordingly...
  214. case GDScriptParser::Node::IDENTIFIER: {
  215. //return identifier
  216. //wait, identifier could be a local variable or something else... careful here, must reference properly
  217. //as stack may be more interesting to work with
  218. //This could be made much simpler by just indexing "self", but done this way (with custom self-addressing modes) increases performance a lot.
  219. const GDScriptParser::IdentifierNode *in = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  220. StringName identifier = in->name;
  221. // TRY STACK!
  222. if (!p_initializer && codegen.stack_identifiers.has(identifier)) {
  223. int pos = codegen.stack_identifiers[identifier];
  224. return pos | (GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS);
  225. }
  226. // TRY LOCAL CONSTANTS!
  227. if (codegen.local_named_constants.has(identifier)) {
  228. return codegen.local_named_constants[identifier] | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  229. }
  230. // TRY CLASS MEMBER
  231. if (_is_class_member_property(codegen, identifier)) {
  232. //get property
  233. codegen.opcodes.push_back(GDScriptFunction::OPCODE_GET_MEMBER); // perform operator
  234. codegen.opcodes.push_back(codegen.get_name_map_pos(identifier)); // argument 2 (unary only takes one parameter)
  235. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  236. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  237. codegen.alloc_stack(p_stack_level);
  238. return dst_addr;
  239. }
  240. //TRY MEMBERS!
  241. if (!codegen.function_node || !codegen.function_node->is_static) {
  242. // TRY MEMBER VARIABLES!
  243. //static function
  244. if (codegen.script->member_indices.has(identifier)) {
  245. if (codegen.script->member_indices[identifier].getter != StringName() && codegen.script->member_indices[identifier].getter != codegen.function_name) {
  246. // Perform getter.
  247. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_RETURN);
  248. codegen.opcodes.push_back(0); // Argument count.
  249. codegen.opcodes.push_back(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS); // Base (self).
  250. codegen.opcodes.push_back(codegen.get_name_map_pos(codegen.script->member_indices[identifier].getter)); // Method name.
  251. // Destination.
  252. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  253. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  254. codegen.alloc_stack(p_stack_level);
  255. return dst_addr;
  256. } else {
  257. // No getter or inside getter: direct member access.
  258. int idx = codegen.script->member_indices[identifier].index;
  259. return idx | (GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS); //argument (stack root)
  260. }
  261. }
  262. }
  263. //TRY CLASS CONSTANTS
  264. GDScript *owner = codegen.script;
  265. while (owner) {
  266. GDScript *scr = owner;
  267. GDScriptNativeClass *nc = nullptr;
  268. while (scr) {
  269. if (scr->constants.has(identifier)) {
  270. //int idx=scr->constants[identifier];
  271. int idx = codegen.get_name_map_pos(identifier);
  272. return idx | (GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT << GDScriptFunction::ADDR_BITS); //argument (stack root)
  273. }
  274. if (scr->native.is_valid()) {
  275. nc = scr->native.ptr();
  276. }
  277. scr = scr->_base;
  278. }
  279. // CLASS C++ Integer Constant
  280. if (nc) {
  281. bool success = false;
  282. int constant = ClassDB::get_integer_constant(nc->get_name(), identifier, &success);
  283. if (success) {
  284. Variant key = constant;
  285. int idx;
  286. if (!codegen.constant_map.has(key)) {
  287. idx = codegen.constant_map.size();
  288. codegen.constant_map[key] = idx;
  289. } else {
  290. idx = codegen.constant_map[key];
  291. }
  292. return idx | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS); //make it a local constant (faster access)
  293. }
  294. }
  295. owner = owner->_owner;
  296. }
  297. // TRY SIGNALS AND METHODS (can be made callables)
  298. if (codegen.class_node->members_indices.has(identifier)) {
  299. const GDScriptParser::ClassNode::Member &member = codegen.class_node->members[codegen.class_node->members_indices[identifier]];
  300. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION || member.type == GDScriptParser::ClassNode::Member::SIGNAL) {
  301. // Get like it was a property.
  302. codegen.opcodes.push_back(GDScriptFunction::OPCODE_GET_NAMED); // perform operator
  303. codegen.opcodes.push_back(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS); // Self.
  304. codegen.opcodes.push_back(codegen.get_name_map_pos(identifier)); // argument 2 (unary only takes one parameter)
  305. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  306. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  307. codegen.alloc_stack(p_stack_level);
  308. return dst_addr;
  309. }
  310. }
  311. if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
  312. int idx = GDScriptLanguage::get_singleton()->get_global_map()[identifier];
  313. return idx | (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root)
  314. }
  315. /* TRY GLOBAL CLASSES */
  316. if (ScriptServer::is_global_class(identifier)) {
  317. const GDScriptParser::ClassNode *class_node = codegen.class_node;
  318. while (class_node->outer) {
  319. class_node = class_node->outer;
  320. }
  321. if (class_node->identifier && class_node->identifier->name == identifier) {
  322. _set_error("Using own name in class file is not allowed (creates a cyclic reference)", p_expression);
  323. return -1;
  324. }
  325. RES res = ResourceLoader::load(ScriptServer::get_global_class_path(identifier));
  326. if (res.is_null()) {
  327. _set_error("Can't load global class " + String(identifier) + ", cyclic reference?", p_expression);
  328. return -1;
  329. }
  330. Variant key = res;
  331. int idx;
  332. if (!codegen.constant_map.has(key)) {
  333. idx = codegen.constant_map.size();
  334. codegen.constant_map[key] = idx;
  335. } else {
  336. idx = codegen.constant_map[key];
  337. }
  338. return idx | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS); //make it a local constant (faster access)
  339. }
  340. #ifdef TOOLS_ENABLED
  341. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(identifier)) {
  342. int idx = codegen.named_globals.find(identifier);
  343. if (idx == -1) {
  344. idx = codegen.named_globals.size();
  345. codegen.named_globals.push_back(identifier);
  346. }
  347. return idx | (GDScriptFunction::ADDR_TYPE_NAMED_GLOBAL << GDScriptFunction::ADDR_BITS);
  348. }
  349. #endif
  350. //not found, error
  351. _set_error("Identifier not found: " + String(identifier), p_expression);
  352. return -1;
  353. } break;
  354. case GDScriptParser::Node::LITERAL: {
  355. //return constant
  356. const GDScriptParser::LiteralNode *cn = static_cast<const GDScriptParser::LiteralNode *>(p_expression);
  357. int idx;
  358. if (!codegen.constant_map.has(cn->value)) {
  359. idx = codegen.constant_map.size();
  360. codegen.constant_map[cn->value] = idx;
  361. } else {
  362. idx = codegen.constant_map[cn->value];
  363. }
  364. return idx | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS); //argument (stack root)
  365. } break;
  366. case GDScriptParser::Node::SELF: {
  367. //return constant
  368. if (codegen.function_node && codegen.function_node->is_static) {
  369. _set_error("'self' not present in static function!", p_expression);
  370. return -1;
  371. }
  372. return (GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
  373. } break;
  374. case GDScriptParser::Node::ARRAY: {
  375. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  376. Vector<int> values;
  377. int slevel = p_stack_level;
  378. for (int i = 0; i < an->elements.size(); i++) {
  379. int ret = _parse_expression(codegen, an->elements[i], slevel);
  380. if (ret < 0) {
  381. return ret;
  382. }
  383. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  384. slevel++;
  385. codegen.alloc_stack(slevel);
  386. }
  387. values.push_back(ret);
  388. }
  389. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY);
  390. codegen.opcodes.push_back(values.size());
  391. for (int i = 0; i < values.size(); i++) {
  392. codegen.opcodes.push_back(values[i]);
  393. }
  394. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  395. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  396. codegen.alloc_stack(p_stack_level);
  397. return dst_addr;
  398. } break;
  399. case GDScriptParser::Node::DICTIONARY: {
  400. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  401. Vector<int> elements;
  402. int slevel = p_stack_level;
  403. for (int i = 0; i < dn->elements.size(); i++) {
  404. // Key.
  405. int ret = -1;
  406. switch (dn->style) {
  407. case GDScriptParser::DictionaryNode::PYTHON_DICT:
  408. // Python-style: key is any expression.
  409. ret = _parse_expression(codegen, dn->elements[i].key, slevel);
  410. if (ret < 0) {
  411. return ret;
  412. }
  413. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  414. slevel++;
  415. codegen.alloc_stack(slevel);
  416. }
  417. break;
  418. case GDScriptParser::DictionaryNode::LUA_TABLE:
  419. // Lua-style: key is an identifier interpreted as string.
  420. String key = static_cast<const GDScriptParser::IdentifierNode *>(dn->elements[i].key)->name;
  421. ret = codegen.get_constant_pos(key);
  422. break;
  423. }
  424. elements.push_back(ret);
  425. ret = _parse_expression(codegen, dn->elements[i].value, slevel);
  426. if (ret < 0) {
  427. return ret;
  428. }
  429. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  430. slevel++;
  431. codegen.alloc_stack(slevel);
  432. }
  433. elements.push_back(ret);
  434. }
  435. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY);
  436. codegen.opcodes.push_back(dn->elements.size());
  437. for (int i = 0; i < elements.size(); i++) {
  438. codegen.opcodes.push_back(elements[i]);
  439. }
  440. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  441. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  442. codegen.alloc_stack(p_stack_level);
  443. return dst_addr;
  444. } break;
  445. case GDScriptParser::Node::CAST: {
  446. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  447. int slevel = p_stack_level;
  448. int src_addr = _parse_expression(codegen, cn->operand, slevel);
  449. if (src_addr < 0) {
  450. return src_addr;
  451. }
  452. if (src_addr & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  453. slevel++;
  454. codegen.alloc_stack(slevel);
  455. }
  456. // FIXME: Allow actual cast.
  457. GDScriptDataType cast_type; // = _gdtype_from_datatype(cn->cast_type);
  458. switch (cast_type.kind) {
  459. case GDScriptDataType::BUILTIN: {
  460. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CAST_TO_BUILTIN);
  461. codegen.opcodes.push_back(cast_type.builtin_type);
  462. } break;
  463. case GDScriptDataType::NATIVE: {
  464. int class_idx;
  465. if (GDScriptLanguage::get_singleton()->get_global_map().has(cast_type.native_type)) {
  466. class_idx = GDScriptLanguage::get_singleton()->get_global_map()[cast_type.native_type];
  467. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root)
  468. } else {
  469. _set_error("Invalid native class type '" + String(cast_type.native_type) + "'.", cn);
  470. return -1;
  471. }
  472. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CAST_TO_NATIVE); // perform operator
  473. codegen.opcodes.push_back(class_idx); // variable type
  474. } break;
  475. case GDScriptDataType::SCRIPT:
  476. case GDScriptDataType::GDSCRIPT: {
  477. Variant script = cast_type.script_type;
  478. int idx = codegen.get_constant_pos(script); //make it a local constant (faster access)
  479. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CAST_TO_SCRIPT); // perform operator
  480. codegen.opcodes.push_back(idx); // variable type
  481. } break;
  482. default: {
  483. _set_error("Parser bug: unresolved data type.", cn);
  484. return -1;
  485. }
  486. }
  487. codegen.opcodes.push_back(src_addr); // source address
  488. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  489. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  490. codegen.alloc_stack(p_stack_level);
  491. return dst_addr;
  492. } break;
  493. //hell breaks loose
  494. #define OPERATOR_RETURN \
  495. int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); \
  496. codegen.opcodes.push_back(dst_addr); \
  497. codegen.alloc_stack(p_stack_level); \
  498. return dst_addr
  499. case GDScriptParser::Node::CALL: {
  500. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression);
  501. if (!call->is_super && call->callee->type == GDScriptParser::Node::IDENTIFIER && GDScriptParser::get_builtin_type(static_cast<GDScriptParser::IdentifierNode *>(call->callee)->name) != Variant::VARIANT_MAX) {
  502. //construct a basic type
  503. Variant::Type vtype = GDScriptParser::get_builtin_type(static_cast<GDScriptParser::IdentifierNode *>(call->callee)->name);
  504. Vector<int> arguments;
  505. int slevel = p_stack_level;
  506. for (int i = 0; i < call->arguments.size(); i++) {
  507. int ret = _parse_expression(codegen, call->arguments[i], slevel);
  508. if (ret < 0) {
  509. return ret;
  510. }
  511. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  512. slevel++;
  513. codegen.alloc_stack(slevel);
  514. }
  515. arguments.push_back(ret);
  516. }
  517. //push call bytecode
  518. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CONSTRUCT); // basic type constructor
  519. codegen.opcodes.push_back(vtype); //instance
  520. codegen.opcodes.push_back(arguments.size()); //argument count
  521. codegen.alloc_call(arguments.size());
  522. for (int i = 0; i < arguments.size(); i++) {
  523. codegen.opcodes.push_back(arguments[i]); //arguments
  524. }
  525. } else if (!call->is_super && call->callee->type == GDScriptParser::Node::IDENTIFIER && GDScriptParser::get_builtin_function(static_cast<GDScriptParser::IdentifierNode *>(call->callee)->name) != GDScriptFunctions::FUNC_MAX) {
  526. //built in function
  527. Vector<int> arguments;
  528. int slevel = p_stack_level;
  529. for (int i = 0; i < call->arguments.size(); i++) {
  530. int ret = _parse_expression(codegen, call->arguments[i], slevel);
  531. if (ret < 0) {
  532. return ret;
  533. }
  534. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  535. slevel++;
  536. codegen.alloc_stack(slevel);
  537. }
  538. arguments.push_back(ret);
  539. }
  540. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN);
  541. codegen.opcodes.push_back(GDScriptParser::get_builtin_function(static_cast<GDScriptParser::IdentifierNode *>(call->callee)->name));
  542. codegen.opcodes.push_back(arguments.size());
  543. codegen.alloc_call(arguments.size());
  544. for (int i = 0; i < arguments.size(); i++) {
  545. codegen.opcodes.push_back(arguments[i]);
  546. }
  547. } else {
  548. //regular function
  549. const GDScriptParser::ExpressionNode *callee = call->callee;
  550. Vector<int> arguments;
  551. int slevel = p_stack_level;
  552. // TODO: Use callables when possible if needed.
  553. int ret = -1;
  554. int super_address = -1;
  555. if (call->is_super) {
  556. // Super call.
  557. if (call->callee == nullptr) {
  558. // Implicit super function call.
  559. super_address = codegen.get_name_map_pos(codegen.function_node->identifier->name);
  560. } else {
  561. super_address = codegen.get_name_map_pos(static_cast<GDScriptParser::IdentifierNode *>(call->callee)->name);
  562. }
  563. } else {
  564. if (callee->type == GDScriptParser::Node::IDENTIFIER) {
  565. // Self function call.
  566. if (codegen.function_node && codegen.function_node->is_static) {
  567. ret = (GDScriptFunction::ADDR_TYPE_CLASS << GDScriptFunction::ADDR_BITS);
  568. } else {
  569. ret = (GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
  570. }
  571. arguments.push_back(ret);
  572. ret = codegen.get_name_map_pos(static_cast<GDScriptParser::IdentifierNode *>(call->callee)->name);
  573. arguments.push_back(ret);
  574. } else if (callee->type == GDScriptParser::Node::SUBSCRIPT) {
  575. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee);
  576. if (subscript->is_attribute) {
  577. ret = _parse_expression(codegen, subscript->base, 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. arguments.push_back(codegen.get_name_map_pos(subscript->attribute->name));
  587. } else {
  588. // TODO: Validate this at parse time.
  589. // TODO: Though might not be the case if callables are a thing.
  590. _set_error("Cannot call something that isn't a function.", call->callee);
  591. return -1;
  592. }
  593. } else {
  594. _set_error("Cannot call something that isn't a function.", call->callee);
  595. return -1;
  596. }
  597. }
  598. for (int i = 0; i < call->arguments.size(); i++) {
  599. ret = _parse_expression(codegen, call->arguments[i], slevel);
  600. if (ret < 0) {
  601. return ret;
  602. }
  603. if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  604. slevel++;
  605. codegen.alloc_stack(slevel);
  606. }
  607. arguments.push_back(ret);
  608. }
  609. int opcode = GDScriptFunction::OPCODE_CALL_RETURN;
  610. if (call->is_super) {
  611. opcode = GDScriptFunction::OPCODE_CALL_SELF_BASE;
  612. } else if (within_await) {
  613. opcode = GDScriptFunction::OPCODE_CALL_ASYNC;
  614. } else if (p_root) {
  615. opcode = GDScriptFunction::OPCODE_CALL;
  616. }
  617. codegen.opcodes.push_back(opcode); // perform operator
  618. if (call->is_super) {
  619. codegen.opcodes.push_back(super_address);
  620. }
  621. codegen.opcodes.push_back(call->arguments.size());
  622. codegen.alloc_call(call->arguments.size());
  623. for (int i = 0; i < arguments.size(); i++) {
  624. codegen.opcodes.push_back(arguments[i]);
  625. }
  626. }
  627. OPERATOR_RETURN;
  628. } break;
  629. case GDScriptParser::Node::GET_NODE: {
  630. const GDScriptParser::GetNodeNode *get_node = static_cast<const GDScriptParser::GetNodeNode *>(p_expression);
  631. String node_name;
  632. if (get_node->string != nullptr) {
  633. node_name += String(get_node->string->value);
  634. } else {
  635. for (int i = 0; i < get_node->chain.size(); i++) {
  636. if (i > 0) {
  637. node_name += "/";
  638. }
  639. node_name += get_node->chain[i]->name;
  640. }
  641. }
  642. int arg_address = codegen.get_constant_pos(NodePath(node_name));
  643. codegen.opcodes.push_back(p_root ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN);
  644. codegen.opcodes.push_back(1); // number of arguments.
  645. codegen.alloc_call(1);
  646. codegen.opcodes.push_back(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS); // self.
  647. codegen.opcodes.push_back(codegen.get_name_map_pos("get_node")); // function.
  648. codegen.opcodes.push_back(arg_address); // argument (NodePath).
  649. OPERATOR_RETURN;
  650. } break;
  651. case GDScriptParser::Node::PRELOAD: {
  652. const GDScriptParser::PreloadNode *preload = static_cast<const GDScriptParser::PreloadNode *>(p_expression);
  653. // Add resource as constant.
  654. return codegen.get_constant_pos(preload->resource);
  655. } break;
  656. case GDScriptParser::Node::AWAIT: {
  657. const GDScriptParser::AwaitNode *await = static_cast<const GDScriptParser::AwaitNode *>(p_expression);
  658. int slevel = p_stack_level;
  659. within_await = true;
  660. int argument = _parse_expression(codegen, await->to_await, slevel);
  661. within_await = false;
  662. if (argument < 0) {
  663. return argument;
  664. }
  665. if ((argument >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  666. slevel++;
  667. codegen.alloc_stack(slevel);
  668. }
  669. //push call bytecode
  670. codegen.opcodes.push_back(GDScriptFunction::OPCODE_AWAIT);
  671. codegen.opcodes.push_back(argument);
  672. codegen.opcodes.push_back(GDScriptFunction::OPCODE_AWAIT_RESUME);
  673. //next will be where to place the result :)
  674. OPERATOR_RETURN;
  675. } break;
  676. //indexing operator
  677. case GDScriptParser::Node::SUBSCRIPT: {
  678. int slevel = p_stack_level;
  679. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
  680. int from = _parse_expression(codegen, subscript->base, slevel);
  681. if (from < 0) {
  682. return from;
  683. }
  684. bool named = subscript->is_attribute;
  685. int index;
  686. if (p_index_addr != 0) {
  687. index = p_index_addr;
  688. } else if (subscript->is_attribute) {
  689. if (subscript->base->type == GDScriptParser::Node::SELF && codegen.script) {
  690. GDScriptParser::IdentifierNode *identifier = subscript->attribute;
  691. const Map<StringName, GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(identifier->name);
  692. #ifdef DEBUG_ENABLED
  693. if (MI && MI->get().getter == codegen.function_name) {
  694. String n = identifier->name;
  695. _set_error("Must use '" + n + "' instead of 'self." + n + "' in getter.", identifier);
  696. return -1;
  697. }
  698. #endif
  699. if (MI && MI->get().getter == "") {
  700. // Faster than indexing self (as if no self. had been used)
  701. return (MI->get().index) | (GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS);
  702. }
  703. }
  704. index = codegen.get_name_map_pos(subscript->attribute->name);
  705. } else {
  706. if (subscript->index->type == GDScriptParser::Node::LITERAL && static_cast<const GDScriptParser::LiteralNode *>(subscript->index)->value.get_type() == Variant::STRING) {
  707. //also, somehow, named (speed up anyway)
  708. StringName name = static_cast<const GDScriptParser::LiteralNode *>(subscript->index)->value;
  709. index = codegen.get_name_map_pos(name);
  710. named = true;
  711. } else {
  712. //regular indexing
  713. if (from & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  714. slevel++;
  715. codegen.alloc_stack(slevel);
  716. }
  717. index = _parse_expression(codegen, subscript->index, slevel);
  718. if (index < 0) {
  719. return index;
  720. }
  721. }
  722. }
  723. codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_GET_NAMED : GDScriptFunction::OPCODE_GET); // perform operator
  724. codegen.opcodes.push_back(from); // argument 1
  725. codegen.opcodes.push_back(index); // argument 2 (unary only takes one parameter)
  726. OPERATOR_RETURN;
  727. } break;
  728. case GDScriptParser::Node::UNARY_OPERATOR: {
  729. //unary operators
  730. const GDScriptParser::UnaryOpNode *unary = static_cast<const GDScriptParser::UnaryOpNode *>(p_expression);
  731. switch (unary->operation) {
  732. case GDScriptParser::UnaryOpNode::OP_NEGATIVE: {
  733. if (!_create_unary_operator(codegen, unary, Variant::OP_NEGATE, p_stack_level)) {
  734. return -1;
  735. }
  736. } break;
  737. case GDScriptParser::UnaryOpNode::OP_POSITIVE: {
  738. if (!_create_unary_operator(codegen, unary, Variant::OP_POSITIVE, p_stack_level)) {
  739. return -1;
  740. }
  741. } break;
  742. case GDScriptParser::UnaryOpNode::OP_LOGIC_NOT: {
  743. if (!_create_unary_operator(codegen, unary, Variant::OP_NOT, p_stack_level)) {
  744. return -1;
  745. }
  746. } break;
  747. case GDScriptParser::UnaryOpNode::OP_COMPLEMENT: {
  748. if (!_create_unary_operator(codegen, unary, Variant::OP_BIT_NEGATE, p_stack_level)) {
  749. return -1;
  750. }
  751. } break;
  752. }
  753. OPERATOR_RETURN;
  754. }
  755. case GDScriptParser::Node::BINARY_OPERATOR: {
  756. //binary operators (in precedence order)
  757. const GDScriptParser::BinaryOpNode *binary = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
  758. switch (binary->operation) {
  759. case GDScriptParser::BinaryOpNode::OP_LOGIC_AND: {
  760. // AND operator with early out on failure
  761. int res = _parse_expression(codegen, binary->left_operand, p_stack_level);
  762. if (res < 0) {
  763. return res;
  764. }
  765. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  766. codegen.opcodes.push_back(res);
  767. int jump_fail_pos = codegen.opcodes.size();
  768. codegen.opcodes.push_back(0);
  769. res = _parse_expression(codegen, binary->right_operand, p_stack_level);
  770. if (res < 0) {
  771. return res;
  772. }
  773. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  774. codegen.opcodes.push_back(res);
  775. int jump_fail_pos2 = codegen.opcodes.size();
  776. codegen.opcodes.push_back(0);
  777. codegen.alloc_stack(p_stack_level); //it will be used..
  778. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  779. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  780. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  781. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  782. codegen.opcodes.write[jump_fail_pos] = codegen.opcodes.size();
  783. codegen.opcodes.write[jump_fail_pos2] = codegen.opcodes.size();
  784. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  785. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  786. return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  787. } break;
  788. case GDScriptParser::BinaryOpNode::OP_LOGIC_OR: {
  789. // OR operator with early out on success
  790. int res = _parse_expression(codegen, binary->left_operand, p_stack_level);
  791. if (res < 0) {
  792. return res;
  793. }
  794. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF);
  795. codegen.opcodes.push_back(res);
  796. int jump_success_pos = codegen.opcodes.size();
  797. codegen.opcodes.push_back(0);
  798. res = _parse_expression(codegen, binary->right_operand, p_stack_level);
  799. if (res < 0) {
  800. return res;
  801. }
  802. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF);
  803. codegen.opcodes.push_back(res);
  804. int jump_success_pos2 = codegen.opcodes.size();
  805. codegen.opcodes.push_back(0);
  806. codegen.alloc_stack(p_stack_level); //it will be used..
  807. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  808. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  809. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  810. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  811. codegen.opcodes.write[jump_success_pos] = codegen.opcodes.size();
  812. codegen.opcodes.write[jump_success_pos2] = codegen.opcodes.size();
  813. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  814. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  815. return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  816. } break;
  817. case GDScriptParser::BinaryOpNode::OP_TYPE_TEST: {
  818. int slevel = p_stack_level;
  819. int src_address_a = _parse_expression(codegen, binary->left_operand, slevel);
  820. if (src_address_a < 0) {
  821. return -1;
  822. }
  823. if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  824. slevel++; //uses stack for return, increase stack
  825. }
  826. int src_address_b = -1;
  827. bool builtin = false;
  828. if (binary->right_operand->type == GDScriptParser::Node::IDENTIFIER && GDScriptParser::get_builtin_type(static_cast<const GDScriptParser::IdentifierNode *>(binary->right_operand)->name) != Variant::VARIANT_MAX) {
  829. // `is` with builtin type
  830. builtin = true;
  831. src_address_b = (int)GDScriptParser::get_builtin_type(static_cast<const GDScriptParser::IdentifierNode *>(binary->right_operand)->name);
  832. } else {
  833. src_address_b = _parse_expression(codegen, binary->right_operand, slevel);
  834. if (src_address_b < 0) {
  835. return -1;
  836. }
  837. }
  838. codegen.opcodes.push_back(builtin ? GDScriptFunction::OPCODE_IS_BUILTIN : GDScriptFunction::OPCODE_EXTENDS_TEST); // perform operator
  839. codegen.opcodes.push_back(src_address_a); // argument 1
  840. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  841. } break;
  842. case GDScriptParser::BinaryOpNode::OP_CONTENT_TEST: {
  843. if (!_create_binary_operator(codegen, binary, Variant::OP_IN, p_stack_level)) {
  844. return -1;
  845. }
  846. } break;
  847. case GDScriptParser::BinaryOpNode::OP_COMP_EQUAL: {
  848. if (!_create_binary_operator(codegen, binary, Variant::OP_EQUAL, p_stack_level)) {
  849. return -1;
  850. }
  851. } break;
  852. case GDScriptParser::BinaryOpNode::OP_COMP_NOT_EQUAL: {
  853. if (!_create_binary_operator(codegen, binary, Variant::OP_NOT_EQUAL, p_stack_level)) {
  854. return -1;
  855. }
  856. } break;
  857. case GDScriptParser::BinaryOpNode::OP_COMP_LESS: {
  858. if (!_create_binary_operator(codegen, binary, Variant::OP_LESS, p_stack_level)) {
  859. return -1;
  860. }
  861. } break;
  862. case GDScriptParser::BinaryOpNode::OP_COMP_LESS_EQUAL: {
  863. if (!_create_binary_operator(codegen, binary, Variant::OP_LESS_EQUAL, p_stack_level)) {
  864. return -1;
  865. }
  866. } break;
  867. case GDScriptParser::BinaryOpNode::OP_COMP_GREATER: {
  868. if (!_create_binary_operator(codegen, binary, Variant::OP_GREATER, p_stack_level)) {
  869. return -1;
  870. }
  871. } break;
  872. case GDScriptParser::BinaryOpNode::OP_COMP_GREATER_EQUAL: {
  873. if (!_create_binary_operator(codegen, binary, Variant::OP_GREATER_EQUAL, p_stack_level)) {
  874. return -1;
  875. }
  876. } break;
  877. case GDScriptParser::BinaryOpNode::OP_ADDITION: {
  878. if (!_create_binary_operator(codegen, binary, Variant::OP_ADD, p_stack_level)) {
  879. return -1;
  880. }
  881. } break;
  882. case GDScriptParser::BinaryOpNode::OP_SUBTRACTION: {
  883. if (!_create_binary_operator(codegen, binary, Variant::OP_SUBTRACT, p_stack_level)) {
  884. return -1;
  885. }
  886. } break;
  887. case GDScriptParser::BinaryOpNode::OP_MULTIPLICATION: {
  888. if (!_create_binary_operator(codegen, binary, Variant::OP_MULTIPLY, p_stack_level)) {
  889. return -1;
  890. }
  891. } break;
  892. case GDScriptParser::BinaryOpNode::OP_DIVISION: {
  893. if (!_create_binary_operator(codegen, binary, Variant::OP_DIVIDE, p_stack_level)) {
  894. return -1;
  895. }
  896. } break;
  897. case GDScriptParser::BinaryOpNode::OP_MODULO: {
  898. if (!_create_binary_operator(codegen, binary, Variant::OP_MODULE, p_stack_level)) {
  899. return -1;
  900. }
  901. } break;
  902. case GDScriptParser::BinaryOpNode::OP_BIT_AND: {
  903. if (!_create_binary_operator(codegen, binary, Variant::OP_BIT_AND, p_stack_level)) {
  904. return -1;
  905. }
  906. } break;
  907. case GDScriptParser::BinaryOpNode::OP_BIT_OR: {
  908. if (!_create_binary_operator(codegen, binary, Variant::OP_BIT_OR, p_stack_level)) {
  909. return -1;
  910. }
  911. } break;
  912. case GDScriptParser::BinaryOpNode::OP_BIT_XOR: {
  913. if (!_create_binary_operator(codegen, binary, Variant::OP_BIT_XOR, p_stack_level)) {
  914. return -1;
  915. }
  916. } break;
  917. //shift
  918. case GDScriptParser::BinaryOpNode::OP_BIT_LEFT_SHIFT: {
  919. if (!_create_binary_operator(codegen, binary, Variant::OP_SHIFT_LEFT, p_stack_level)) {
  920. return -1;
  921. }
  922. } break;
  923. case GDScriptParser::BinaryOpNode::OP_BIT_RIGHT_SHIFT: {
  924. if (!_create_binary_operator(codegen, binary, Variant::OP_SHIFT_RIGHT, p_stack_level)) {
  925. return -1;
  926. }
  927. } break;
  928. }
  929. OPERATOR_RETURN;
  930. } break;
  931. // ternary operators
  932. case GDScriptParser::Node::TERNARY_OPERATOR: {
  933. // x IF a ELSE y operator with early out on failure
  934. const GDScriptParser::TernaryOpNode *ternary = static_cast<const GDScriptParser::TernaryOpNode *>(p_expression);
  935. int res = _parse_expression(codegen, ternary->condition, p_stack_level);
  936. if (res < 0) {
  937. return res;
  938. }
  939. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  940. codegen.opcodes.push_back(res);
  941. int jump_fail_pos = codegen.opcodes.size();
  942. codegen.opcodes.push_back(0);
  943. res = _parse_expression(codegen, ternary->true_expr, p_stack_level);
  944. if (res < 0) {
  945. return res;
  946. }
  947. codegen.alloc_stack(p_stack_level); //it will be used..
  948. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  949. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  950. codegen.opcodes.push_back(res);
  951. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  952. int jump_past_pos = codegen.opcodes.size();
  953. codegen.opcodes.push_back(0);
  954. codegen.opcodes.write[jump_fail_pos] = codegen.opcodes.size();
  955. res = _parse_expression(codegen, ternary->false_expr, p_stack_level);
  956. if (res < 0) {
  957. return res;
  958. }
  959. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  960. codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  961. codegen.opcodes.push_back(res);
  962. codegen.opcodes.write[jump_past_pos] = codegen.opcodes.size();
  963. return p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  964. } break;
  965. //assignment operators
  966. case GDScriptParser::Node::ASSIGNMENT: {
  967. const GDScriptParser::AssignmentNode *assignment = static_cast<const GDScriptParser::AssignmentNode *>(p_expression);
  968. if (assignment->assignee->type == GDScriptParser::Node::SUBSCRIPT) {
  969. // SET (chained) MODE!
  970. const GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(assignment->assignee);
  971. #ifdef DEBUG_ENABLED
  972. if (subscript->is_attribute) {
  973. if (subscript->base->type == GDScriptParser::Node::SELF && codegen.script) {
  974. const Map<StringName, GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(subscript->attribute->name);
  975. if (MI && MI->get().setter == codegen.function_name) {
  976. String n = subscript->attribute->name;
  977. _set_error("Must use '" + n + "' instead of 'self." + n + "' in setter.", subscript);
  978. return -1;
  979. }
  980. }
  981. }
  982. #endif
  983. int slevel = p_stack_level;
  984. /* Find chain of sets */
  985. StringName assign_property;
  986. List<const GDScriptParser::SubscriptNode *> chain;
  987. {
  988. //create get/set chain
  989. const GDScriptParser::SubscriptNode *n = subscript;
  990. while (true) {
  991. chain.push_back(n);
  992. if (n->base->type != GDScriptParser::Node::SUBSCRIPT) {
  993. //check for a built-in property
  994. if (n->base->type == GDScriptParser::Node::IDENTIFIER) {
  995. GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(n->base);
  996. if (_is_class_member_property(codegen, identifier->name)) {
  997. assign_property = identifier->name;
  998. }
  999. }
  1000. break;
  1001. }
  1002. n = static_cast<const GDScriptParser::SubscriptNode *>(n->base);
  1003. }
  1004. }
  1005. /* Chain of gets */
  1006. //get at (potential) root stack pos, so it can be returned
  1007. int prev_pos = _parse_expression(codegen, chain.back()->get()->base, slevel);
  1008. if (prev_pos < 0) {
  1009. return prev_pos;
  1010. }
  1011. int retval = prev_pos;
  1012. if (retval & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  1013. slevel++;
  1014. codegen.alloc_stack(slevel);
  1015. }
  1016. Vector<int> setchain;
  1017. if (assign_property != StringName()) {
  1018. // recover and assign at the end, this allows stuff like
  1019. // position.x+=2.0
  1020. // in Node2D
  1021. setchain.push_back(prev_pos);
  1022. setchain.push_back(codegen.get_name_map_pos(assign_property));
  1023. setchain.push_back(GDScriptFunction::OPCODE_SET_MEMBER);
  1024. }
  1025. for (List<const GDScriptParser::SubscriptNode *>::Element *E = chain.back(); E; E = E->prev()) {
  1026. if (E == chain.front()) { //ignore first
  1027. break;
  1028. }
  1029. const GDScriptParser::SubscriptNode *subscript_elem = E->get();
  1030. int key_idx;
  1031. if (subscript_elem->is_attribute) {
  1032. key_idx = codegen.get_name_map_pos(subscript_elem->attribute->name);
  1033. //printf("named key %x\n",key_idx);
  1034. } else {
  1035. if (prev_pos & (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS)) {
  1036. slevel++;
  1037. codegen.alloc_stack(slevel);
  1038. }
  1039. GDScriptParser::ExpressionNode *key = subscript_elem->index;
  1040. key_idx = _parse_expression(codegen, key, slevel);
  1041. //printf("expr key %x\n",key_idx);
  1042. //stack was raised here if retval was stack but..
  1043. }
  1044. if (key_idx < 0) { //error
  1045. return key_idx;
  1046. }
  1047. codegen.opcodes.push_back(subscript_elem->is_attribute ? GDScriptFunction::OPCODE_GET_NAMED : GDScriptFunction::OPCODE_GET);
  1048. codegen.opcodes.push_back(prev_pos);
  1049. codegen.opcodes.push_back(key_idx);
  1050. slevel++;
  1051. codegen.alloc_stack(slevel);
  1052. int dst_pos = (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) | slevel;
  1053. codegen.opcodes.push_back(dst_pos);
  1054. //add in reverse order, since it will be reverted
  1055. setchain.push_back(dst_pos);
  1056. setchain.push_back(key_idx);
  1057. setchain.push_back(prev_pos);
  1058. setchain.push_back(subscript_elem->is_attribute ? GDScriptFunction::OPCODE_SET_NAMED : GDScriptFunction::OPCODE_SET);
  1059. prev_pos = dst_pos;
  1060. }
  1061. setchain.invert();
  1062. int set_index;
  1063. if (subscript->is_attribute) {
  1064. set_index = codegen.get_name_map_pos(subscript->attribute->name);
  1065. } else {
  1066. set_index = _parse_expression(codegen, subscript->index, slevel + 1);
  1067. }
  1068. if (set_index < 0) { //error
  1069. return set_index;
  1070. }
  1071. if (set_index & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  1072. slevel++;
  1073. codegen.alloc_stack(slevel);
  1074. }
  1075. int set_value = _parse_assign_right_expression(codegen, assignment, slevel + 1, subscript->is_attribute ? 0 : set_index);
  1076. if (set_value < 0) { //error
  1077. return set_value;
  1078. }
  1079. codegen.opcodes.push_back(subscript->is_attribute ? GDScriptFunction::OPCODE_SET_NAMED : GDScriptFunction::OPCODE_SET);
  1080. codegen.opcodes.push_back(prev_pos);
  1081. codegen.opcodes.push_back(set_index);
  1082. codegen.opcodes.push_back(set_value);
  1083. for (int i = 0; i < setchain.size(); i++) {
  1084. codegen.opcodes.push_back(setchain[i]);
  1085. }
  1086. return retval;
  1087. } else if (assignment->assignee->type == GDScriptParser::Node::IDENTIFIER && _is_class_member_property(codegen, static_cast<GDScriptParser::IdentifierNode *>(assignment->assignee)->name)) {
  1088. //assignment to member property
  1089. int slevel = p_stack_level;
  1090. int src_address = _parse_assign_right_expression(codegen, assignment, slevel);
  1091. if (src_address < 0) {
  1092. return -1;
  1093. }
  1094. StringName name = static_cast<GDScriptParser::IdentifierNode *>(assignment->assignee)->name;
  1095. codegen.opcodes.push_back(GDScriptFunction::OPCODE_SET_MEMBER);
  1096. codegen.opcodes.push_back(codegen.get_name_map_pos(name));
  1097. codegen.opcodes.push_back(src_address);
  1098. return GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS;
  1099. } else {
  1100. //REGULAR ASSIGNMENT MODE!!
  1101. int slevel = p_stack_level;
  1102. int dst_address_a = -1;
  1103. bool has_setter = false;
  1104. bool is_in_setter = false;
  1105. StringName setter_function;
  1106. if (assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1107. StringName var_name = static_cast<const GDScriptParser::IdentifierNode *>(assignment->assignee)->name;
  1108. if (!codegen.stack_identifiers.has(var_name) && codegen.script->member_indices.has(var_name)) {
  1109. setter_function = codegen.script->member_indices[var_name].setter;
  1110. if (setter_function != StringName()) {
  1111. has_setter = true;
  1112. is_in_setter = setter_function == codegen.function_name;
  1113. dst_address_a = codegen.script->member_indices[var_name].index;
  1114. }
  1115. }
  1116. }
  1117. if (has_setter) {
  1118. if (is_in_setter) {
  1119. // Use direct member access.
  1120. dst_address_a |= GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS;
  1121. } else {
  1122. // Store stack slot for the temp value.
  1123. dst_address_a = slevel++;
  1124. codegen.alloc_stack(slevel);
  1125. dst_address_a |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  1126. }
  1127. } else {
  1128. dst_address_a = _parse_expression(codegen, assignment->assignee, slevel);
  1129. if (dst_address_a < 0) {
  1130. return -1;
  1131. }
  1132. if (dst_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
  1133. slevel++;
  1134. codegen.alloc_stack(slevel);
  1135. }
  1136. }
  1137. int src_address_b = _parse_assign_right_expression(codegen, assignment, slevel);
  1138. if (src_address_b < 0) {
  1139. return -1;
  1140. }
  1141. GDScriptDataType assign_type = _gdtype_from_datatype(assignment->assignee->get_datatype());
  1142. if (assign_type.has_type && !assignment->assigned_value->get_datatype().is_variant()) {
  1143. // Typed assignment
  1144. switch (assign_type.kind) {
  1145. case GDScriptDataType::BUILTIN: {
  1146. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN); // perform operator
  1147. codegen.opcodes.push_back(assign_type.builtin_type); // variable type
  1148. codegen.opcodes.push_back(dst_address_a); // argument 1
  1149. codegen.opcodes.push_back(src_address_b); // argument 2
  1150. } break;
  1151. case GDScriptDataType::NATIVE: {
  1152. int class_idx;
  1153. if (GDScriptLanguage::get_singleton()->get_global_map().has(assign_type.native_type)) {
  1154. class_idx = GDScriptLanguage::get_singleton()->get_global_map()[assign_type.native_type];
  1155. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root)
  1156. } else {
  1157. // _set_error("Invalid native class type '" + String(assign_type.native_type) + "'.", on->arguments[0]);
  1158. return -1;
  1159. }
  1160. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE); // perform operator
  1161. codegen.opcodes.push_back(class_idx); // variable type
  1162. codegen.opcodes.push_back(dst_address_a); // argument 1
  1163. codegen.opcodes.push_back(src_address_b); // argument 2
  1164. } break;
  1165. case GDScriptDataType::SCRIPT:
  1166. case GDScriptDataType::GDSCRIPT: {
  1167. Variant script = assign_type.script_type;
  1168. int idx = codegen.get_constant_pos(script); //make it a local constant (faster access)
  1169. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT); // perform operator
  1170. codegen.opcodes.push_back(idx); // variable type
  1171. codegen.opcodes.push_back(dst_address_a); // argument 1
  1172. codegen.opcodes.push_back(src_address_b); // argument 2
  1173. } break;
  1174. default: {
  1175. ERR_PRINT("Compiler bug: unresolved assign.");
  1176. // Shouldn't get here, but fail-safe to a regular assignment
  1177. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); // perform operator
  1178. codegen.opcodes.push_back(dst_address_a); // argument 1
  1179. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  1180. }
  1181. }
  1182. } else {
  1183. // Either untyped assignment or already type-checked by the parser
  1184. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN); // perform operator
  1185. codegen.opcodes.push_back(dst_address_a); // argument 1
  1186. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  1187. }
  1188. if (has_setter && !is_in_setter) {
  1189. // Call setter.
  1190. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL);
  1191. codegen.opcodes.push_back(1); // Argument count.
  1192. codegen.opcodes.push_back(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS); // Base (self).
  1193. codegen.opcodes.push_back(codegen.get_name_map_pos(setter_function)); // Method name.
  1194. codegen.opcodes.push_back(dst_address_a); // Argument.
  1195. codegen.opcodes.push_back(dst_address_a); // Result address (won't be used here).
  1196. codegen.alloc_call(1);
  1197. }
  1198. return dst_address_a; //if anything, returns wathever was assigned or correct stack position
  1199. }
  1200. } break;
  1201. #undef OPERATOR_RETURN
  1202. //TYPE_TYPE,
  1203. default: {
  1204. ERR_FAIL_V_MSG(-1, "Bug in bytecode compiler, unexpected node in parse tree while parsing expression."); //unreachable code
  1205. } break;
  1206. }
  1207. }
  1208. Error GDScriptCompiler::_parse_match_pattern(CodeGen &codegen, const GDScriptParser::PatternNode *p_pattern, int p_stack_level, int p_value_addr, int p_type_addr, int &r_bound_variables, Vector<int> &r_patch_addresses, Vector<int> &r_block_patch_address) {
  1209. // TODO: Many "repeated" code here that could be abstracted. This compiler is going away when new VM arrives though, so...
  1210. switch (p_pattern->pattern_type) {
  1211. case GDScriptParser::PatternNode::PT_LITERAL: {
  1212. // Get literal type into constant map.
  1213. int literal_type_addr = -1;
  1214. if (!codegen.constant_map.has((int)p_pattern->literal->value.get_type())) {
  1215. literal_type_addr = codegen.constant_map.size();
  1216. codegen.constant_map[(int)p_pattern->literal->value.get_type()] = literal_type_addr;
  1217. } else {
  1218. literal_type_addr = codegen.constant_map[(int)p_pattern->literal->value.get_type()];
  1219. }
  1220. literal_type_addr |= GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS;
  1221. // Check type equality.
  1222. int equality_addr = p_stack_level++;
  1223. equality_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  1224. codegen.alloc_stack(p_stack_level);
  1225. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR);
  1226. codegen.opcodes.push_back(Variant::OP_EQUAL);
  1227. codegen.opcodes.push_back(p_type_addr);
  1228. codegen.opcodes.push_back(literal_type_addr);
  1229. codegen.opcodes.push_back(equality_addr); // Address to result.
  1230. // Jump away if not the same type.
  1231. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1232. codegen.opcodes.push_back(equality_addr);
  1233. r_patch_addresses.push_back(codegen.opcodes.size());
  1234. codegen.opcodes.push_back(0); // Will be replaced.
  1235. // Get literal.
  1236. int literal_addr = _parse_expression(codegen, p_pattern->literal, p_stack_level);
  1237. // Check value equality.
  1238. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR);
  1239. codegen.opcodes.push_back(Variant::OP_EQUAL);
  1240. codegen.opcodes.push_back(p_value_addr);
  1241. codegen.opcodes.push_back(literal_addr);
  1242. codegen.opcodes.push_back(equality_addr); // Address to result.
  1243. // Jump away if doesn't match.
  1244. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1245. codegen.opcodes.push_back(equality_addr);
  1246. r_patch_addresses.push_back(codegen.opcodes.size());
  1247. codegen.opcodes.push_back(0); // Will be replaced.
  1248. // Jump to the actual block since it matches. This is needed to take multi-pattern into account.
  1249. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1250. r_block_patch_address.push_back(codegen.opcodes.size());
  1251. codegen.opcodes.push_back(0); // Will be replaced.
  1252. } break;
  1253. case GDScriptParser::PatternNode::PT_EXPRESSION: {
  1254. // Evaluate expression.
  1255. int expr_addr = _parse_expression(codegen, p_pattern->expression, p_stack_level);
  1256. if ((expr_addr >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  1257. p_stack_level++;
  1258. codegen.alloc_stack(p_stack_level);
  1259. }
  1260. // Evaluate expression type.
  1261. int expr_type_addr = p_stack_level++;
  1262. expr_type_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  1263. codegen.alloc_stack(p_stack_level);
  1264. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN);
  1265. codegen.opcodes.push_back(GDScriptFunctions::TYPE_OF);
  1266. codegen.opcodes.push_back(1); // One argument.
  1267. codegen.opcodes.push_back(expr_addr); // Argument is the value we want to test.
  1268. codegen.opcodes.push_back(expr_type_addr); // Address to result.
  1269. // Check type equality.
  1270. int equality_addr = p_stack_level++;
  1271. equality_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  1272. codegen.alloc_stack(p_stack_level);
  1273. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR);
  1274. codegen.opcodes.push_back(Variant::OP_EQUAL);
  1275. codegen.opcodes.push_back(p_type_addr);
  1276. codegen.opcodes.push_back(expr_type_addr);
  1277. codegen.opcodes.push_back(equality_addr); // Address to result.
  1278. // Jump away if not the same type.
  1279. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1280. codegen.opcodes.push_back(equality_addr);
  1281. r_patch_addresses.push_back(codegen.opcodes.size());
  1282. codegen.opcodes.push_back(0); // Will be replaced.
  1283. // Check value equality.
  1284. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR);
  1285. codegen.opcodes.push_back(Variant::OP_EQUAL);
  1286. codegen.opcodes.push_back(p_value_addr);
  1287. codegen.opcodes.push_back(expr_addr);
  1288. codegen.opcodes.push_back(equality_addr); // Address to result.
  1289. // Jump away if doesn't match.
  1290. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1291. codegen.opcodes.push_back(equality_addr);
  1292. r_patch_addresses.push_back(codegen.opcodes.size());
  1293. codegen.opcodes.push_back(0); // Will be replaced.
  1294. // Jump to the actual block since it matches. This is needed to take multi-pattern into account.
  1295. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1296. r_block_patch_address.push_back(codegen.opcodes.size());
  1297. codegen.opcodes.push_back(0); // Will be replaced.
  1298. } break;
  1299. case GDScriptParser::PatternNode::PT_BIND: {
  1300. // Create new stack variable.
  1301. int bind_addr = p_stack_level | (GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS);
  1302. codegen.add_stack_identifier(p_pattern->bind->name, p_stack_level++);
  1303. codegen.alloc_stack(p_stack_level);
  1304. r_bound_variables++;
  1305. // Assign value to bound variable.
  1306. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  1307. codegen.opcodes.push_back(bind_addr); // Destination.
  1308. codegen.opcodes.push_back(p_value_addr); // Source.
  1309. // Not need to block jump because bind happens only once.
  1310. } break;
  1311. case GDScriptParser::PatternNode::PT_ARRAY: {
  1312. int slevel = p_stack_level;
  1313. // Get array type into constant map.
  1314. int array_type_addr = codegen.get_constant_pos(Variant::ARRAY);
  1315. // Check type equality.
  1316. int equality_addr = slevel++;
  1317. equality_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  1318. codegen.alloc_stack(slevel);
  1319. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR);
  1320. codegen.opcodes.push_back(Variant::OP_EQUAL);
  1321. codegen.opcodes.push_back(p_type_addr);
  1322. codegen.opcodes.push_back(array_type_addr);
  1323. codegen.opcodes.push_back(equality_addr); // Address to result.
  1324. // Jump away if not the same type.
  1325. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1326. codegen.opcodes.push_back(equality_addr);
  1327. r_patch_addresses.push_back(codegen.opcodes.size());
  1328. codegen.opcodes.push_back(0); // Will be replaced.
  1329. // Store pattern length in constant map.
  1330. int array_length_addr = codegen.get_constant_pos(p_pattern->rest_used ? p_pattern->array.size() - 1 : p_pattern->array.size());
  1331. // Get value length.
  1332. int value_length_addr = slevel++;
  1333. codegen.alloc_stack(slevel);
  1334. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN);
  1335. codegen.opcodes.push_back(GDScriptFunctions::LEN);
  1336. codegen.opcodes.push_back(1); // One argument.
  1337. codegen.opcodes.push_back(p_value_addr); // Argument is the value we want to test.
  1338. codegen.opcodes.push_back(value_length_addr); // Address to result.
  1339. // Test length compatibility.
  1340. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR);
  1341. codegen.opcodes.push_back(p_pattern->rest_used ? Variant::OP_GREATER_EQUAL : Variant::OP_EQUAL);
  1342. codegen.opcodes.push_back(value_length_addr);
  1343. codegen.opcodes.push_back(array_length_addr);
  1344. codegen.opcodes.push_back(equality_addr); // Address to result.
  1345. // Jump away if length is not compatible.
  1346. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1347. codegen.opcodes.push_back(equality_addr);
  1348. r_patch_addresses.push_back(codegen.opcodes.size());
  1349. codegen.opcodes.push_back(0); // Will be replaced.
  1350. // Evaluate element by element.
  1351. for (int i = 0; i < p_pattern->array.size(); i++) {
  1352. if (p_pattern->array[i]->pattern_type == GDScriptParser::PatternNode::PT_REST) {
  1353. // Don't want to access an extra element of the user array.
  1354. break;
  1355. }
  1356. int stlevel = p_stack_level;
  1357. Vector<int> element_block_patches; // I want to internal patterns try the next element instead of going to the block.
  1358. // Add index to constant map.
  1359. int index_addr = codegen.get_constant_pos(i);
  1360. // Get the actual element from the user-sent array.
  1361. int element_addr = stlevel++;
  1362. codegen.alloc_stack(stlevel);
  1363. codegen.opcodes.push_back(GDScriptFunction::OPCODE_GET);
  1364. codegen.opcodes.push_back(p_value_addr); // Source.
  1365. codegen.opcodes.push_back(index_addr); // Index.
  1366. codegen.opcodes.push_back(element_addr); // Destination.
  1367. // Also get type of element.
  1368. int element_type_addr = stlevel++;
  1369. element_type_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  1370. codegen.alloc_stack(stlevel);
  1371. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN);
  1372. codegen.opcodes.push_back(GDScriptFunctions::TYPE_OF);
  1373. codegen.opcodes.push_back(1); // One argument.
  1374. codegen.opcodes.push_back(element_addr); // Argument is the value we want to test.
  1375. codegen.opcodes.push_back(element_type_addr); // Address to result.
  1376. // Try the pattern inside the element.
  1377. Error err = _parse_match_pattern(codegen, p_pattern->array[i], stlevel, element_addr, element_type_addr, r_bound_variables, r_patch_addresses, element_block_patches);
  1378. if (err != OK) {
  1379. return err;
  1380. }
  1381. // Patch jumps to block to try the next element.
  1382. for (int j = 0; j < element_block_patches.size(); j++) {
  1383. codegen.opcodes.write[element_block_patches[j]] = codegen.opcodes.size();
  1384. }
  1385. }
  1386. // Jump to the actual block since it matches. This is needed to take multi-pattern into account.
  1387. // Also here for the case of empty arrays.
  1388. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1389. r_block_patch_address.push_back(codegen.opcodes.size());
  1390. codegen.opcodes.push_back(0); // Will be replaced.
  1391. } break;
  1392. case GDScriptParser::PatternNode::PT_DICTIONARY: {
  1393. int slevel = p_stack_level;
  1394. // Get dictionary type into constant map.
  1395. int dict_type_addr = codegen.get_constant_pos(Variant::DICTIONARY);
  1396. // Check type equality.
  1397. int equality_addr = slevel++;
  1398. equality_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  1399. codegen.alloc_stack(slevel);
  1400. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR);
  1401. codegen.opcodes.push_back(Variant::OP_EQUAL);
  1402. codegen.opcodes.push_back(p_type_addr);
  1403. codegen.opcodes.push_back(dict_type_addr);
  1404. codegen.opcodes.push_back(equality_addr); // Address to result.
  1405. // Jump away if not the same type.
  1406. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1407. codegen.opcodes.push_back(equality_addr);
  1408. r_patch_addresses.push_back(codegen.opcodes.size());
  1409. codegen.opcodes.push_back(0); // Will be replaced.
  1410. // Store pattern length in constant map.
  1411. int dict_length_addr = codegen.get_constant_pos(p_pattern->rest_used ? p_pattern->dictionary.size() - 1 : p_pattern->dictionary.size());
  1412. // Get user's dictionary length.
  1413. int value_length_addr = slevel++;
  1414. codegen.alloc_stack(slevel);
  1415. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN);
  1416. codegen.opcodes.push_back(GDScriptFunctions::LEN);
  1417. codegen.opcodes.push_back(1); // One argument.
  1418. codegen.opcodes.push_back(p_value_addr); // Argument is the value we want to test.
  1419. codegen.opcodes.push_back(value_length_addr); // Address to result.
  1420. // Test length compatibility.
  1421. codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR);
  1422. codegen.opcodes.push_back(p_pattern->rest_used ? Variant::OP_GREATER_EQUAL : Variant::OP_EQUAL);
  1423. codegen.opcodes.push_back(value_length_addr);
  1424. codegen.opcodes.push_back(dict_length_addr);
  1425. codegen.opcodes.push_back(equality_addr); // Address to result.
  1426. // Jump away if length is not compatible.
  1427. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1428. codegen.opcodes.push_back(equality_addr);
  1429. r_patch_addresses.push_back(codegen.opcodes.size());
  1430. codegen.opcodes.push_back(0); // Will be replaced.
  1431. // Evaluate element by element.
  1432. for (int i = 0; i < p_pattern->dictionary.size(); i++) {
  1433. const GDScriptParser::PatternNode::Pair &element = p_pattern->dictionary[i];
  1434. if (element.value_pattern->pattern_type == GDScriptParser::PatternNode::PT_REST) {
  1435. // Ignore rest pattern.
  1436. continue;
  1437. }
  1438. int stlevel = p_stack_level;
  1439. Vector<int> element_block_patches; // I want to internal patterns try the next element instead of going to the block.
  1440. // Get the pattern key.
  1441. int pattern_key_addr = _parse_expression(codegen, element.key, stlevel);
  1442. if (pattern_key_addr < 0) {
  1443. return ERR_PARSE_ERROR;
  1444. }
  1445. if ((pattern_key_addr >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  1446. stlevel++;
  1447. codegen.alloc_stack(stlevel);
  1448. }
  1449. // Create stack slot for test result.
  1450. int test_result = stlevel++;
  1451. test_result |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  1452. codegen.alloc_stack(stlevel);
  1453. // Check if pattern key exists in user's dictionary.
  1454. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_RETURN);
  1455. codegen.opcodes.push_back(1); // Argument count.
  1456. codegen.opcodes.push_back(p_value_addr); // Base (user dictionary).
  1457. codegen.opcodes.push_back(codegen.get_name_map_pos("has")); // Function name.
  1458. codegen.opcodes.push_back(pattern_key_addr); // Argument (pattern key).
  1459. codegen.opcodes.push_back(test_result); // Return address.
  1460. // Jump away if key doesn't exist.
  1461. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1462. codegen.opcodes.push_back(test_result);
  1463. r_patch_addresses.push_back(codegen.opcodes.size());
  1464. codegen.opcodes.push_back(0); // Will be replaced.
  1465. // Get actual value from user dictionary.
  1466. int value_addr = stlevel++;
  1467. codegen.alloc_stack(stlevel);
  1468. codegen.opcodes.push_back(GDScriptFunction::OPCODE_GET);
  1469. codegen.opcodes.push_back(p_value_addr); // Source.
  1470. codegen.opcodes.push_back(pattern_key_addr); // Index.
  1471. codegen.opcodes.push_back(value_addr); // Destination.
  1472. // Also get type of value.
  1473. int value_type_addr = stlevel++;
  1474. value_type_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  1475. codegen.alloc_stack(stlevel);
  1476. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN);
  1477. codegen.opcodes.push_back(GDScriptFunctions::TYPE_OF);
  1478. codegen.opcodes.push_back(1); // One argument.
  1479. codegen.opcodes.push_back(value_addr); // Argument is the value we want to test.
  1480. codegen.opcodes.push_back(value_type_addr); // Address to result.
  1481. // Try the pattern inside the value.
  1482. Error err = _parse_match_pattern(codegen, element.value_pattern, stlevel, value_addr, value_type_addr, r_bound_variables, r_patch_addresses, element_block_patches);
  1483. if (err != OK) {
  1484. return err;
  1485. }
  1486. // Patch jumps to block to try the next element.
  1487. for (int j = 0; j < element_block_patches.size(); j++) {
  1488. codegen.opcodes.write[element_block_patches[j]] = codegen.opcodes.size();
  1489. }
  1490. }
  1491. // Jump to the actual block since it matches. This is needed to take multi-pattern into account.
  1492. // Also here for the case of empty dictionaries.
  1493. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1494. r_block_patch_address.push_back(codegen.opcodes.size());
  1495. codegen.opcodes.push_back(0); // Will be replaced.
  1496. } break;
  1497. case GDScriptParser::PatternNode::PT_REST:
  1498. // Do nothing.
  1499. break;
  1500. case GDScriptParser::PatternNode::PT_WILDCARD:
  1501. // This matches anything so just do the jump.
  1502. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1503. r_block_patch_address.push_back(codegen.opcodes.size());
  1504. codegen.opcodes.push_back(0); // Will be replaced.
  1505. }
  1506. return OK;
  1507. }
  1508. Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, int p_stack_level, int p_break_addr, int p_continue_addr) {
  1509. codegen.push_stack_identifiers();
  1510. int new_identifiers = 0;
  1511. codegen.current_line = p_block->start_line;
  1512. for (int i = 0; i < p_block->statements.size(); i++) {
  1513. const GDScriptParser::Node *s = p_block->statements[i];
  1514. #ifdef DEBUG_ENABLED
  1515. // Add a newline before each statement, since the debugger needs those.
  1516. codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE);
  1517. codegen.opcodes.push_back(s->start_line);
  1518. codegen.current_line = s->start_line;
  1519. #endif
  1520. switch (s->type) {
  1521. case GDScriptParser::Node::MATCH: {
  1522. const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(s);
  1523. int slevel = p_stack_level;
  1524. // First, let's save the addres of the value match.
  1525. int temp_addr = _parse_expression(codegen, match->test, slevel);
  1526. if (temp_addr < 0) {
  1527. return ERR_PARSE_ERROR;
  1528. }
  1529. if ((temp_addr >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
  1530. slevel++;
  1531. codegen.alloc_stack(slevel);
  1532. }
  1533. // Then, let's save the type of the value in the stack too, so we can reuse for later comparisons.
  1534. int type_addr = slevel++;
  1535. type_addr |= GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS;
  1536. codegen.alloc_stack(slevel);
  1537. codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_BUILT_IN);
  1538. codegen.opcodes.push_back(GDScriptFunctions::TYPE_OF);
  1539. codegen.opcodes.push_back(1); // One argument.
  1540. codegen.opcodes.push_back(temp_addr); // Argument is the value we want to test.
  1541. codegen.opcodes.push_back(type_addr); // Address to result.
  1542. Vector<int> patch_match_end; // Will patch the jump to the end of match.
  1543. // Now we can actually start testing.
  1544. // For each branch.
  1545. for (int j = 0; j < match->branches.size(); j++) {
  1546. const GDScriptParser::MatchBranchNode *branch = match->branches[j];
  1547. int bound_variables = 0;
  1548. codegen.push_stack_identifiers(); // Create an extra block around for binds.
  1549. #ifdef DEBUG_ENABLED
  1550. // Add a newline before each branch, since the debugger needs those.
  1551. codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE);
  1552. codegen.opcodes.push_back(s->start_line);
  1553. codegen.current_line = s->start_line;
  1554. #endif
  1555. Vector<int> patch_addrs; // Will patch with end of pattern to jump.
  1556. Vector<int> block_patch_addrs; // Will patch with start of block to jump.
  1557. // For each pattern in branch.
  1558. for (int k = 0; k < branch->patterns.size(); k++) {
  1559. if (k > 0) {
  1560. // Patch jumps per pattern to allow for multipattern. If a pattern fails it just tries the next.
  1561. for (int l = 0; l < patch_addrs.size(); l++) {
  1562. codegen.opcodes.write[patch_addrs[l]] = codegen.opcodes.size();
  1563. }
  1564. patch_addrs.clear();
  1565. }
  1566. Error err = _parse_match_pattern(codegen, branch->patterns[k], slevel, temp_addr, type_addr, bound_variables, patch_addrs, block_patch_addrs);
  1567. if (err != OK) {
  1568. return err;
  1569. }
  1570. }
  1571. // Patch jumps to the block.
  1572. for (int k = 0; k < block_patch_addrs.size(); k++) {
  1573. codegen.opcodes.write[block_patch_addrs[k]] = codegen.opcodes.size();
  1574. }
  1575. // Leave space for bound variables.
  1576. slevel += bound_variables;
  1577. codegen.alloc_stack(slevel);
  1578. // Parse the branch block.
  1579. _parse_block(codegen, branch->block, slevel, p_break_addr, p_continue_addr);
  1580. // Jump to end of match.
  1581. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1582. patch_match_end.push_back(codegen.opcodes.size());
  1583. codegen.opcodes.push_back(0); // Will be patched.
  1584. // Patch the addresses of last pattern to jump to the end of the branch, into the next one.
  1585. for (int k = 0; k < patch_addrs.size(); k++) {
  1586. codegen.opcodes.write[patch_addrs[k]] = codegen.opcodes.size();
  1587. }
  1588. codegen.pop_stack_identifiers(); // Get out of extra block.
  1589. }
  1590. // Patch the addresses to jump to the end of the match statement.
  1591. for (int j = 0; j < patch_match_end.size(); j++) {
  1592. codegen.opcodes.write[patch_match_end[j]] = codegen.opcodes.size();
  1593. }
  1594. } break;
  1595. case GDScriptParser::Node::IF: {
  1596. const GDScriptParser::IfNode *if_n = static_cast<const GDScriptParser::IfNode *>(s);
  1597. int ret2 = _parse_expression(codegen, if_n->condition, p_stack_level, false);
  1598. if (ret2 < 0) {
  1599. return ERR_PARSE_ERROR;
  1600. }
  1601. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1602. codegen.opcodes.push_back(ret2);
  1603. int else_addr = codegen.opcodes.size();
  1604. codegen.opcodes.push_back(0); //temporary
  1605. Error err = _parse_block(codegen, if_n->true_block, p_stack_level, p_break_addr, p_continue_addr);
  1606. if (err) {
  1607. return err;
  1608. }
  1609. if (if_n->false_block) {
  1610. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1611. int end_addr = codegen.opcodes.size();
  1612. codegen.opcodes.push_back(0);
  1613. codegen.opcodes.write[else_addr] = codegen.opcodes.size();
  1614. Error err2 = _parse_block(codegen, if_n->false_block, p_stack_level, p_break_addr, p_continue_addr);
  1615. if (err2) {
  1616. return err2;
  1617. }
  1618. codegen.opcodes.write[end_addr] = codegen.opcodes.size();
  1619. } else {
  1620. //end without else
  1621. codegen.opcodes.write[else_addr] = codegen.opcodes.size();
  1622. }
  1623. } break;
  1624. case GDScriptParser::Node::FOR: {
  1625. const GDScriptParser::ForNode *for_n = static_cast<const GDScriptParser::ForNode *>(s);
  1626. int slevel = p_stack_level;
  1627. int iter_stack_pos = slevel;
  1628. int iterator_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1629. int counter_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1630. int container_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1631. codegen.alloc_stack(slevel);
  1632. codegen.push_stack_identifiers();
  1633. codegen.add_stack_identifier(for_n->variable->name, iter_stack_pos);
  1634. int ret2 = _parse_expression(codegen, for_n->list, slevel, false);
  1635. if (ret2 < 0) {
  1636. return ERR_COMPILATION_FAILED;
  1637. }
  1638. //assign container
  1639. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  1640. codegen.opcodes.push_back(container_pos);
  1641. codegen.opcodes.push_back(ret2);
  1642. //begin loop
  1643. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ITERATE_BEGIN);
  1644. codegen.opcodes.push_back(counter_pos);
  1645. codegen.opcodes.push_back(container_pos);
  1646. codegen.opcodes.push_back(codegen.opcodes.size() + 4);
  1647. codegen.opcodes.push_back(iterator_pos);
  1648. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); //skip code for next
  1649. codegen.opcodes.push_back(codegen.opcodes.size() + 8);
  1650. //break loop
  1651. int break_pos = codegen.opcodes.size();
  1652. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP); //skip code for next
  1653. codegen.opcodes.push_back(0); //skip code for next
  1654. //next loop
  1655. int continue_pos = codegen.opcodes.size();
  1656. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ITERATE);
  1657. codegen.opcodes.push_back(counter_pos);
  1658. codegen.opcodes.push_back(container_pos);
  1659. codegen.opcodes.push_back(break_pos);
  1660. codegen.opcodes.push_back(iterator_pos);
  1661. Error err = _parse_block(codegen, for_n->loop, slevel, break_pos, continue_pos);
  1662. if (err) {
  1663. return err;
  1664. }
  1665. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1666. codegen.opcodes.push_back(continue_pos);
  1667. codegen.opcodes.write[break_pos + 1] = codegen.opcodes.size();
  1668. codegen.pop_stack_identifiers();
  1669. } break;
  1670. case GDScriptParser::Node::WHILE: {
  1671. const GDScriptParser::WhileNode *while_n = static_cast<const GDScriptParser::WhileNode *>(s);
  1672. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1673. codegen.opcodes.push_back(codegen.opcodes.size() + 3);
  1674. int break_addr = codegen.opcodes.size();
  1675. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1676. codegen.opcodes.push_back(0);
  1677. int continue_addr = codegen.opcodes.size();
  1678. int ret2 = _parse_expression(codegen, while_n->condition, p_stack_level, false);
  1679. if (ret2 < 0) {
  1680. return ERR_PARSE_ERROR;
  1681. }
  1682. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1683. codegen.opcodes.push_back(ret2);
  1684. codegen.opcodes.push_back(break_addr);
  1685. Error err = _parse_block(codegen, while_n->loop, p_stack_level, break_addr, continue_addr);
  1686. if (err) {
  1687. return err;
  1688. }
  1689. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1690. codegen.opcodes.push_back(continue_addr);
  1691. codegen.opcodes.write[break_addr + 1] = codegen.opcodes.size();
  1692. } break;
  1693. case GDScriptParser::Node::BREAK: {
  1694. if (p_break_addr < 0) {
  1695. _set_error("'break'' not within loop", s);
  1696. return ERR_COMPILATION_FAILED;
  1697. }
  1698. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1699. codegen.opcodes.push_back(p_break_addr);
  1700. } break;
  1701. case GDScriptParser::Node::CONTINUE: {
  1702. if (p_continue_addr < 0) {
  1703. _set_error("'continue' not within loop", s);
  1704. return ERR_COMPILATION_FAILED;
  1705. }
  1706. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
  1707. codegen.opcodes.push_back(p_continue_addr);
  1708. } break;
  1709. case GDScriptParser::Node::RETURN: {
  1710. const GDScriptParser::ReturnNode *return_n = static_cast<const GDScriptParser::ReturnNode *>(s);
  1711. int ret2;
  1712. if (return_n->return_value != nullptr) {
  1713. ret2 = _parse_expression(codegen, return_n->return_value, p_stack_level, false);
  1714. if (ret2 < 0) {
  1715. return ERR_PARSE_ERROR;
  1716. }
  1717. } else {
  1718. ret2 = GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS;
  1719. }
  1720. codegen.opcodes.push_back(GDScriptFunction::OPCODE_RETURN);
  1721. codegen.opcodes.push_back(ret2);
  1722. } break;
  1723. case GDScriptParser::Node::ASSERT: {
  1724. #ifdef DEBUG_ENABLED
  1725. // try subblocks
  1726. const GDScriptParser::AssertNode *as = static_cast<const GDScriptParser::AssertNode *>(s);
  1727. int ret2 = _parse_expression(codegen, as->condition, p_stack_level, false);
  1728. if (ret2 < 0) {
  1729. return ERR_PARSE_ERROR;
  1730. }
  1731. int message_ret = 0;
  1732. if (as->message) {
  1733. message_ret = _parse_expression(codegen, as->message, p_stack_level + 1, false);
  1734. if (message_ret < 0) {
  1735. return ERR_PARSE_ERROR;
  1736. }
  1737. }
  1738. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSERT);
  1739. codegen.opcodes.push_back(ret2);
  1740. codegen.opcodes.push_back(message_ret);
  1741. #endif
  1742. } break;
  1743. case GDScriptParser::Node::BREAKPOINT: {
  1744. #ifdef DEBUG_ENABLED
  1745. // try subblocks
  1746. codegen.opcodes.push_back(GDScriptFunction::OPCODE_BREAKPOINT);
  1747. #endif
  1748. } break;
  1749. case GDScriptParser::Node::VARIABLE: {
  1750. const GDScriptParser::VariableNode *lv = static_cast<const GDScriptParser::VariableNode *>(s);
  1751. // since we are using properties now for most class access, allow shadowing of class members to make user's life easier.
  1752. //
  1753. //if (_is_class_member_property(codegen, lv->name)) {
  1754. // _set_error("Name for local variable '" + String(lv->name) + "' can't shadow class property of the same name.", lv);
  1755. // return ERR_ALREADY_EXISTS;
  1756. //}
  1757. codegen.add_stack_identifier(lv->identifier->name, p_stack_level++);
  1758. codegen.alloc_stack(p_stack_level);
  1759. new_identifiers++;
  1760. if (lv->initializer != nullptr) {
  1761. int dst_address = codegen.stack_identifiers[lv->identifier->name];
  1762. dst_address |= GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS;
  1763. int src_address = _parse_expression(codegen, lv->initializer, p_stack_level);
  1764. if (src_address < 0) {
  1765. return ERR_PARSE_ERROR;
  1766. }
  1767. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  1768. codegen.opcodes.push_back(dst_address);
  1769. codegen.opcodes.push_back(src_address);
  1770. }
  1771. } break;
  1772. case GDScriptParser::Node::CONSTANT: {
  1773. // Local constants.
  1774. const GDScriptParser::ConstantNode *lc = static_cast<const GDScriptParser::ConstantNode *>(s);
  1775. // FIXME: Need to properly reduce expressions to avoid limiting this so much.
  1776. if (lc->initializer->type != GDScriptParser::Node::LITERAL) {
  1777. _set_error("Local constant must have a literal as initializer.", lc->initializer);
  1778. return ERR_PARSE_ERROR;
  1779. }
  1780. codegen.local_named_constants[lc->identifier->name] = codegen.get_constant_pos(static_cast<const GDScriptParser::LiteralNode *>(lc->initializer)->value);
  1781. } break;
  1782. case GDScriptParser::Node::PASS:
  1783. // Nothing to do.
  1784. break;
  1785. default: {
  1786. //expression
  1787. if (s->is_expression()) {
  1788. int ret2 = _parse_expression(codegen, static_cast<const GDScriptParser::ExpressionNode *>(s), p_stack_level, true);
  1789. if (ret2 < 0) {
  1790. return ERR_PARSE_ERROR;
  1791. }
  1792. } else {
  1793. ERR_FAIL_V_MSG(ERR_INVALID_DATA, "Bug in bytecode compiler, unexpected node in parse tree while parsing statement."); //unreachable code
  1794. }
  1795. } break;
  1796. }
  1797. }
  1798. codegen.pop_stack_identifiers();
  1799. return OK;
  1800. }
  1801. Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready) {
  1802. Vector<int> bytecode;
  1803. CodeGen codegen;
  1804. codegen.class_node = p_class;
  1805. codegen.script = p_script;
  1806. codegen.function_node = p_func;
  1807. codegen.stack_max = 0;
  1808. codegen.current_line = 0;
  1809. codegen.call_max = 0;
  1810. codegen.debug_stack = EngineDebugger::is_active();
  1811. Vector<StringName> argnames;
  1812. int stack_level = 0;
  1813. int optional_parameters = 0;
  1814. if (p_func) {
  1815. for (int i = 0; i < p_func->parameters.size(); i++) {
  1816. // since we are using properties now for most class access, allow shadowing of class members to make user's life easier.
  1817. //
  1818. //if (_is_class_member_property(p_script, p_func->arguments[i])) {
  1819. // _set_error("Name for argument '" + String(p_func->arguments[i]) + "' can't shadow class property of the same name.", p_func);
  1820. // return ERR_ALREADY_EXISTS;
  1821. //}
  1822. codegen.add_stack_identifier(p_func->parameters[i]->identifier->name, i);
  1823. #ifdef TOOLS_ENABLED
  1824. argnames.push_back(p_func->parameters[i]->identifier->name);
  1825. #endif
  1826. if (p_func->parameters[i]->default_value != nullptr) {
  1827. optional_parameters++;
  1828. }
  1829. }
  1830. stack_level = p_func->parameters.size();
  1831. }
  1832. codegen.alloc_stack(stack_level);
  1833. /* Parse initializer -if applies- */
  1834. bool is_implicit_initializer = !p_for_ready && !p_func;
  1835. bool is_initializer = p_func && String(p_func->identifier->name) == GDScriptLanguage::get_singleton()->strings._init;
  1836. if (is_implicit_initializer) {
  1837. // Initialize class fields.
  1838. for (int i = 0; i < p_class->members.size(); i++) {
  1839. if (p_class->members[i].type != GDScriptParser::ClassNode::Member::VARIABLE) {
  1840. continue;
  1841. }
  1842. const GDScriptParser::VariableNode *field = p_class->members[i].variable;
  1843. if (field->onready) {
  1844. // Only initialize in _ready.
  1845. continue;
  1846. }
  1847. if (field->initializer) {
  1848. // Emit proper line change.
  1849. codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE);
  1850. codegen.opcodes.push_back(field->initializer->start_line);
  1851. int src_address = _parse_expression(codegen, field->initializer, stack_level, false, true);
  1852. if (src_address < 0) {
  1853. return ERR_PARSE_ERROR;
  1854. }
  1855. int dst_address = codegen.script->member_indices[field->identifier->name].index;
  1856. dst_address |= GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS;
  1857. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  1858. codegen.opcodes.push_back(dst_address);
  1859. codegen.opcodes.push_back(src_address);
  1860. }
  1861. }
  1862. }
  1863. if (p_for_ready || (p_func && String(p_func->identifier->name) == "_ready")) {
  1864. // Initialize class fields on ready.
  1865. for (int i = 0; i < p_class->members.size(); i++) {
  1866. if (p_class->members[i].type != GDScriptParser::ClassNode::Member::VARIABLE) {
  1867. continue;
  1868. }
  1869. const GDScriptParser::VariableNode *field = p_class->members[i].variable;
  1870. if (!field->onready) {
  1871. continue;
  1872. }
  1873. if (field->initializer) {
  1874. // Emit proper line change.
  1875. codegen.opcodes.push_back(GDScriptFunction::OPCODE_LINE);
  1876. codegen.opcodes.push_back(field->initializer->start_line);
  1877. int src_address = _parse_expression(codegen, field->initializer, stack_level, false, true);
  1878. if (src_address < 0) {
  1879. return ERR_PARSE_ERROR;
  1880. }
  1881. int dst_address = codegen.script->member_indices[field->identifier->name].index;
  1882. dst_address |= GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS;
  1883. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  1884. codegen.opcodes.push_back(dst_address);
  1885. codegen.opcodes.push_back(src_address);
  1886. }
  1887. }
  1888. }
  1889. /* Parse default argument code -if applies- */
  1890. Vector<int> defarg_addr;
  1891. StringName func_name;
  1892. if (p_func) {
  1893. if (optional_parameters > 0) {
  1894. codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  1895. defarg_addr.push_back(codegen.opcodes.size());
  1896. for (int i = p_func->parameters.size() - optional_parameters; i < p_func->parameters.size(); i++) {
  1897. int src_addr = _parse_expression(codegen, p_func->parameters[i]->default_value, stack_level, true);
  1898. if (src_addr < 0) {
  1899. return ERR_PARSE_ERROR;
  1900. }
  1901. codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
  1902. codegen.opcodes.push_back(codegen.stack_identifiers[p_func->parameters[i]->identifier->name] | (GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS));
  1903. codegen.opcodes.push_back(src_addr);
  1904. defarg_addr.push_back(codegen.opcodes.size());
  1905. }
  1906. defarg_addr.invert();
  1907. }
  1908. func_name = p_func->identifier->name;
  1909. codegen.function_name = func_name;
  1910. Error err = _parse_block(codegen, p_func->body, stack_level);
  1911. if (err) {
  1912. return err;
  1913. }
  1914. } else {
  1915. if (p_for_ready) {
  1916. func_name = "_ready";
  1917. } else {
  1918. func_name = "@implicit_new";
  1919. }
  1920. }
  1921. codegen.function_name = func_name;
  1922. codegen.opcodes.push_back(GDScriptFunction::OPCODE_END);
  1923. /*
  1924. if (String(p_func->name)=="") { //initializer func
  1925. gdfunc = &p_script->initializer;
  1926. */
  1927. //} else { //regular func
  1928. p_script->member_functions[func_name] = memnew(GDScriptFunction);
  1929. GDScriptFunction *gdfunc = p_script->member_functions[func_name];
  1930. //}
  1931. if (p_func) {
  1932. gdfunc->_static = p_func->is_static;
  1933. gdfunc->rpc_mode = p_func->rpc_mode;
  1934. // FIXME: Add actual parameters types;
  1935. gdfunc->argument_types.resize(p_func->parameters.size());
  1936. for (int i = 0; i < p_func->parameters.size(); i++) {
  1937. gdfunc->argument_types.write[i] = GDScriptDataType(); //_gdtype_from_datatype(p_func->argument_types[i]);
  1938. }
  1939. gdfunc->return_type = GDScriptDataType(); //_gdtype_from_datatype(p_func->return_type);
  1940. } else {
  1941. gdfunc->_static = false;
  1942. gdfunc->rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1943. gdfunc->return_type = GDScriptDataType();
  1944. gdfunc->return_type.has_type = true;
  1945. gdfunc->return_type.kind = GDScriptDataType::BUILTIN;
  1946. gdfunc->return_type.builtin_type = Variant::NIL;
  1947. }
  1948. #ifdef TOOLS_ENABLED
  1949. gdfunc->arg_names = argnames;
  1950. #endif
  1951. //constants
  1952. if (codegen.constant_map.size()) {
  1953. gdfunc->_constant_count = codegen.constant_map.size();
  1954. gdfunc->constants.resize(codegen.constant_map.size());
  1955. gdfunc->_constants_ptr = gdfunc->constants.ptrw();
  1956. const Variant *K = nullptr;
  1957. while ((K = codegen.constant_map.next(K))) {
  1958. int idx = codegen.constant_map[*K];
  1959. gdfunc->constants.write[idx] = *K;
  1960. }
  1961. } else {
  1962. gdfunc->_constants_ptr = nullptr;
  1963. gdfunc->_constant_count = 0;
  1964. }
  1965. //global names
  1966. if (codegen.name_map.size()) {
  1967. gdfunc->global_names.resize(codegen.name_map.size());
  1968. gdfunc->_global_names_ptr = &gdfunc->global_names[0];
  1969. for (Map<StringName, int>::Element *E = codegen.name_map.front(); E; E = E->next()) {
  1970. gdfunc->global_names.write[E->get()] = E->key();
  1971. }
  1972. gdfunc->_global_names_count = gdfunc->global_names.size();
  1973. } else {
  1974. gdfunc->_global_names_ptr = nullptr;
  1975. gdfunc->_global_names_count = 0;
  1976. }
  1977. #ifdef TOOLS_ENABLED
  1978. // Named globals
  1979. if (codegen.named_globals.size()) {
  1980. gdfunc->named_globals.resize(codegen.named_globals.size());
  1981. gdfunc->_named_globals_ptr = gdfunc->named_globals.ptr();
  1982. for (int i = 0; i < codegen.named_globals.size(); i++) {
  1983. gdfunc->named_globals.write[i] = codegen.named_globals[i];
  1984. }
  1985. gdfunc->_named_globals_count = gdfunc->named_globals.size();
  1986. }
  1987. #endif
  1988. if (codegen.opcodes.size()) {
  1989. gdfunc->code = codegen.opcodes;
  1990. gdfunc->_code_ptr = &gdfunc->code[0];
  1991. gdfunc->_code_size = codegen.opcodes.size();
  1992. } else {
  1993. gdfunc->_code_ptr = nullptr;
  1994. gdfunc->_code_size = 0;
  1995. }
  1996. if (defarg_addr.size()) {
  1997. gdfunc->default_arguments = defarg_addr;
  1998. gdfunc->_default_arg_count = defarg_addr.size() - 1;
  1999. gdfunc->_default_arg_ptr = &gdfunc->default_arguments[0];
  2000. } else {
  2001. gdfunc->_default_arg_count = 0;
  2002. gdfunc->_default_arg_ptr = nullptr;
  2003. }
  2004. gdfunc->_argument_count = p_func ? p_func->parameters.size() : 0;
  2005. gdfunc->_stack_size = codegen.stack_max;
  2006. gdfunc->_call_size = codegen.call_max;
  2007. gdfunc->name = func_name;
  2008. #ifdef DEBUG_ENABLED
  2009. if (EngineDebugger::is_active()) {
  2010. String signature;
  2011. //path
  2012. if (p_script->get_path() != String()) {
  2013. signature += p_script->get_path();
  2014. }
  2015. //loc
  2016. if (p_func) {
  2017. signature += "::" + itos(p_func->body->start_line);
  2018. } else {
  2019. signature += "::0";
  2020. }
  2021. //function and class
  2022. if (p_class->identifier) {
  2023. signature += "::" + String(p_class->identifier->name) + "." + String(func_name);
  2024. } else {
  2025. signature += "::" + String(func_name);
  2026. }
  2027. gdfunc->profile.signature = signature;
  2028. }
  2029. #endif
  2030. gdfunc->_script = p_script;
  2031. gdfunc->source = source;
  2032. #ifdef DEBUG_ENABLED
  2033. {
  2034. gdfunc->func_cname = (String(source) + " - " + String(func_name)).utf8();
  2035. gdfunc->_func_cname = gdfunc->func_cname.get_data();
  2036. }
  2037. #endif
  2038. if (p_func) {
  2039. gdfunc->_initial_line = p_func->start_line;
  2040. #ifdef TOOLS_ENABLED
  2041. p_script->member_lines[func_name] = p_func->start_line;
  2042. #endif
  2043. } else {
  2044. gdfunc->_initial_line = 0;
  2045. }
  2046. if (codegen.debug_stack) {
  2047. gdfunc->stack_debug = codegen.stack_debug;
  2048. }
  2049. if (is_initializer) {
  2050. p_script->initializer = gdfunc;
  2051. }
  2052. if (is_implicit_initializer) {
  2053. p_script->implicit_initializer = gdfunc;
  2054. }
  2055. return OK;
  2056. }
  2057. Error GDScriptCompiler::_parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter) {
  2058. Vector<int> bytecode;
  2059. CodeGen codegen;
  2060. codegen.class_node = p_class;
  2061. codegen.script = p_script;
  2062. codegen.function_node = nullptr;
  2063. codegen.stack_max = 0;
  2064. codegen.current_line = 0;
  2065. codegen.call_max = 0;
  2066. codegen.debug_stack = EngineDebugger::is_active();
  2067. Vector<StringName> argnames;
  2068. int stack_level = 0;
  2069. if (p_is_setter) {
  2070. codegen.add_stack_identifier(p_variable->setter_parameter->name, stack_level++);
  2071. argnames.push_back(p_variable->setter_parameter->name);
  2072. }
  2073. codegen.alloc_stack(stack_level);
  2074. StringName func_name;
  2075. if (p_is_setter) {
  2076. func_name = "@" + p_variable->identifier->name + "_setter";
  2077. } else {
  2078. func_name = "@" + p_variable->identifier->name + "_getter";
  2079. }
  2080. codegen.function_name = func_name;
  2081. Error err = _parse_block(codegen, p_is_setter ? p_variable->setter : p_variable->getter, stack_level);
  2082. if (err != OK) {
  2083. return err;
  2084. }
  2085. codegen.opcodes.push_back(GDScriptFunction::OPCODE_END);
  2086. p_script->member_functions[func_name] = memnew(GDScriptFunction);
  2087. GDScriptFunction *gdfunc = p_script->member_functions[func_name];
  2088. gdfunc->_static = false;
  2089. gdfunc->rpc_mode = p_variable->rpc_mode;
  2090. gdfunc->argument_types.resize(p_is_setter ? 1 : 0);
  2091. gdfunc->return_type = GDScriptDataType(); // TODO
  2092. #ifdef TOOLS_ENABLED
  2093. gdfunc->arg_names = argnames;
  2094. #endif
  2095. // TODO: Unify this with function compiler.
  2096. //constants
  2097. if (codegen.constant_map.size()) {
  2098. gdfunc->_constant_count = codegen.constant_map.size();
  2099. gdfunc->constants.resize(codegen.constant_map.size());
  2100. gdfunc->_constants_ptr = gdfunc->constants.ptrw();
  2101. const Variant *K = nullptr;
  2102. while ((K = codegen.constant_map.next(K))) {
  2103. int idx = codegen.constant_map[*K];
  2104. gdfunc->constants.write[idx] = *K;
  2105. }
  2106. } else {
  2107. gdfunc->_constants_ptr = nullptr;
  2108. gdfunc->_constant_count = 0;
  2109. }
  2110. //global names
  2111. if (codegen.name_map.size()) {
  2112. gdfunc->global_names.resize(codegen.name_map.size());
  2113. gdfunc->_global_names_ptr = &gdfunc->global_names[0];
  2114. for (Map<StringName, int>::Element *E = codegen.name_map.front(); E; E = E->next()) {
  2115. gdfunc->global_names.write[E->get()] = E->key();
  2116. }
  2117. gdfunc->_global_names_count = gdfunc->global_names.size();
  2118. } else {
  2119. gdfunc->_global_names_ptr = nullptr;
  2120. gdfunc->_global_names_count = 0;
  2121. }
  2122. #ifdef TOOLS_ENABLED
  2123. // Named globals
  2124. if (codegen.named_globals.size()) {
  2125. gdfunc->named_globals.resize(codegen.named_globals.size());
  2126. gdfunc->_named_globals_ptr = gdfunc->named_globals.ptr();
  2127. for (int i = 0; i < codegen.named_globals.size(); i++) {
  2128. gdfunc->named_globals.write[i] = codegen.named_globals[i];
  2129. }
  2130. gdfunc->_named_globals_count = gdfunc->named_globals.size();
  2131. }
  2132. #endif
  2133. gdfunc->code = codegen.opcodes;
  2134. gdfunc->_code_ptr = &gdfunc->code[0];
  2135. gdfunc->_code_size = codegen.opcodes.size();
  2136. gdfunc->_default_arg_count = 0;
  2137. gdfunc->_default_arg_ptr = nullptr;
  2138. gdfunc->_argument_count = argnames.size();
  2139. gdfunc->_stack_size = codegen.stack_max;
  2140. gdfunc->_call_size = codegen.call_max;
  2141. gdfunc->name = func_name;
  2142. #ifdef DEBUG_ENABLED
  2143. if (EngineDebugger::is_active()) {
  2144. String signature;
  2145. //path
  2146. if (p_script->get_path() != String()) {
  2147. signature += p_script->get_path();
  2148. }
  2149. //loc
  2150. signature += "::" + itos(p_is_setter ? p_variable->setter->start_line : p_variable->getter->start_line);
  2151. //function and class
  2152. if (p_class->identifier) {
  2153. signature += "::" + String(p_class->identifier->name) + "." + String(func_name);
  2154. } else {
  2155. signature += "::" + String(func_name);
  2156. }
  2157. gdfunc->profile.signature = signature;
  2158. }
  2159. #endif
  2160. gdfunc->_script = p_script;
  2161. gdfunc->source = source;
  2162. #ifdef DEBUG_ENABLED
  2163. {
  2164. gdfunc->func_cname = (String(source) + " - " + String(func_name)).utf8();
  2165. gdfunc->_func_cname = gdfunc->func_cname.get_data();
  2166. }
  2167. #endif
  2168. gdfunc->_initial_line = p_is_setter ? p_variable->setter->start_line : p_variable->getter->start_line;
  2169. #ifdef TOOLS_ENABLED
  2170. p_script->member_lines[func_name] = gdfunc->_initial_line;
  2171. #endif
  2172. if (codegen.debug_stack) {
  2173. gdfunc->stack_debug = codegen.stack_debug;
  2174. }
  2175. return OK;
  2176. }
  2177. Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  2178. parsing_classes.insert(p_script);
  2179. if (p_class->outer && p_class->outer->outer) {
  2180. // Owner is not root
  2181. if (!parsed_classes.has(p_script->_owner)) {
  2182. if (parsing_classes.has(p_script->_owner)) {
  2183. _set_error("Cyclic class reference for '" + String(p_class->identifier->name) + "'.", p_class);
  2184. return ERR_PARSE_ERROR;
  2185. }
  2186. Error err = _parse_class_level(p_script->_owner, p_class->outer, p_keep_state);
  2187. if (err) {
  2188. return err;
  2189. }
  2190. }
  2191. }
  2192. p_script->native = Ref<GDScriptNativeClass>();
  2193. p_script->base = Ref<GDScript>();
  2194. p_script->_base = nullptr;
  2195. p_script->members.clear();
  2196. p_script->constants.clear();
  2197. for (Map<StringName, GDScriptFunction *>::Element *E = p_script->member_functions.front(); E; E = E->next()) {
  2198. memdelete(E->get());
  2199. }
  2200. p_script->member_functions.clear();
  2201. p_script->member_indices.clear();
  2202. p_script->member_info.clear();
  2203. p_script->_signals.clear();
  2204. p_script->initializer = nullptr;
  2205. p_script->tool = parser->is_tool();
  2206. p_script->name = p_class->identifier ? p_class->identifier->name : "";
  2207. Ref<GDScriptNativeClass> native;
  2208. GDScriptDataType base_type = _gdtype_from_datatype(p_class->base_type);
  2209. // Inheritance
  2210. switch (base_type.kind) {
  2211. case GDScriptDataType::NATIVE: {
  2212. int native_idx = GDScriptLanguage::get_singleton()->get_global_map()[base_type.native_type];
  2213. native = GDScriptLanguage::get_singleton()->get_global_array()[native_idx];
  2214. ERR_FAIL_COND_V(native.is_null(), ERR_BUG);
  2215. p_script->native = native;
  2216. } break;
  2217. case GDScriptDataType::GDSCRIPT: {
  2218. Ref<GDScript> base = base_type.script_type;
  2219. p_script->base = base;
  2220. p_script->_base = base.ptr();
  2221. p_script->member_indices = base->member_indices;
  2222. if (p_class->base_type.kind == GDScriptParser::DataType::CLASS && p_class->base_type.class_type != nullptr) {
  2223. if (!parsed_classes.has(p_script->_base)) {
  2224. if (parsing_classes.has(p_script->_base)) {
  2225. String class_name = p_class->identifier ? p_class->identifier->name : "<main>";
  2226. _set_error("Cyclic class reference for '" + class_name + "'.", p_class);
  2227. return ERR_PARSE_ERROR;
  2228. }
  2229. Error err = _parse_class_level(p_script->_base, p_class->base_type.class_type, p_keep_state);
  2230. if (err) {
  2231. return err;
  2232. }
  2233. }
  2234. }
  2235. } break;
  2236. default: {
  2237. _set_error("Parser bug: invalid inheritance.", p_class);
  2238. return ERR_BUG;
  2239. } break;
  2240. }
  2241. for (int i = 0; i < p_class->members.size(); i++) {
  2242. const GDScriptParser::ClassNode::Member &member = p_class->members[i];
  2243. switch (member.type) {
  2244. case GDScriptParser::ClassNode::Member::VARIABLE: {
  2245. const GDScriptParser::VariableNode *variable = member.variable;
  2246. StringName name = variable->identifier->name;
  2247. GDScript::MemberInfo minfo;
  2248. minfo.index = p_script->member_indices.size();
  2249. switch (variable->property) {
  2250. case GDScriptParser::VariableNode::PROP_NONE:
  2251. break; // Nothing to do.
  2252. case GDScriptParser::VariableNode::PROP_SETGET:
  2253. if (variable->setter_pointer != nullptr) {
  2254. minfo.setter = variable->setter_pointer->name;
  2255. }
  2256. if (variable->getter_pointer != nullptr) {
  2257. minfo.getter = variable->getter_pointer->name;
  2258. }
  2259. break;
  2260. case GDScriptParser::VariableNode::PROP_INLINE:
  2261. if (variable->setter != nullptr) {
  2262. minfo.setter = "@" + variable->identifier->name + "_setter";
  2263. }
  2264. if (variable->getter != nullptr) {
  2265. minfo.getter = "@" + variable->identifier->name + "_getter";
  2266. }
  2267. break;
  2268. }
  2269. minfo.rpc_mode = variable->rpc_mode;
  2270. // FIXME: Types.
  2271. // minfo.data_type = _gdtype_from_datatype(p_class->variables[i].data_type);
  2272. PropertyInfo prop_info = minfo.data_type;
  2273. prop_info.name = name;
  2274. PropertyInfo export_info = variable->export_info;
  2275. if (variable->exported) {
  2276. if (!minfo.data_type.has_type) {
  2277. prop_info.type = export_info.type;
  2278. prop_info.class_name = export_info.class_name;
  2279. }
  2280. prop_info.hint = export_info.hint;
  2281. prop_info.hint_string = export_info.hint_string;
  2282. prop_info.usage = export_info.usage;
  2283. #ifdef TOOLS_ENABLED
  2284. if (variable->initializer != nullptr && variable->initializer->type == GDScriptParser::Node::LITERAL) {
  2285. p_script->member_default_values[name] = static_cast<const GDScriptParser::LiteralNode *>(variable->initializer)->value;
  2286. }
  2287. #endif
  2288. } else {
  2289. prop_info.usage = PROPERTY_USAGE_SCRIPT_VARIABLE;
  2290. }
  2291. p_script->member_info[name] = prop_info;
  2292. p_script->member_indices[name] = minfo;
  2293. p_script->members.insert(name);
  2294. #ifdef TOOLS_ENABLED
  2295. p_script->member_lines[name] = variable->start_line;
  2296. #endif
  2297. } break;
  2298. case GDScriptParser::ClassNode::Member::CONSTANT: {
  2299. const GDScriptParser::ConstantNode *constant = member.constant;
  2300. StringName name = constant->identifier->name;
  2301. ERR_CONTINUE(constant->initializer->type != GDScriptParser::Node::LITERAL);
  2302. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(constant->initializer);
  2303. p_script->constants.insert(name, literal->value);
  2304. #ifdef TOOLS_ENABLED
  2305. p_script->member_lines[name] = constant->start_line;
  2306. #endif
  2307. } break;
  2308. case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
  2309. const GDScriptParser::EnumNode::Value &enum_value = member.enum_value;
  2310. StringName name = enum_value.identifier->name;
  2311. p_script->constants.insert(name, enum_value.value);
  2312. #ifdef TOOLS_ENABLED
  2313. p_script->member_lines[name] = enum_value.identifier->start_line;
  2314. #endif
  2315. } break;
  2316. case GDScriptParser::ClassNode::Member::SIGNAL: {
  2317. const GDScriptParser::SignalNode *signal = member.signal;
  2318. StringName name = signal->identifier->name;
  2319. GDScript *c = p_script;
  2320. while (c) {
  2321. if (c->_signals.has(name)) {
  2322. _set_error("Signal '" + name + "' redefined (in current or parent class)", p_class);
  2323. return ERR_ALREADY_EXISTS;
  2324. }
  2325. if (c->base.is_valid()) {
  2326. c = c->base.ptr();
  2327. } else {
  2328. c = nullptr;
  2329. }
  2330. }
  2331. if (native.is_valid()) {
  2332. if (ClassDB::has_signal(native->get_name(), name)) {
  2333. _set_error("Signal '" + name + "' redefined (original in native class '" + String(native->get_name()) + "')", p_class);
  2334. return ERR_ALREADY_EXISTS;
  2335. }
  2336. }
  2337. Vector<StringName> parameters_names;
  2338. parameters_names.resize(signal->parameters.size());
  2339. for (int j = 0; j < signal->parameters.size(); j++) {
  2340. parameters_names.write[j] = signal->parameters[j]->identifier->name;
  2341. }
  2342. p_script->_signals[name] = parameters_names;
  2343. } break;
  2344. case GDScriptParser::ClassNode::Member::ENUM: {
  2345. const GDScriptParser::EnumNode *enum_n = member.m_enum;
  2346. // TODO: Make enums not be just a dictionary?
  2347. Dictionary new_enum;
  2348. for (int j = 0; j < enum_n->values.size(); j++) {
  2349. int value = enum_n->values[j].value;
  2350. // Needs to be string because Variant::get will convert to String.
  2351. new_enum[String(enum_n->values[j].identifier->name)] = value;
  2352. }
  2353. p_script->constants.insert(enum_n->identifier->name, new_enum);
  2354. #ifdef TOOLS_ENABLED
  2355. p_script->member_lines[enum_n->identifier->name] = enum_n->start_line;
  2356. #endif
  2357. } break;
  2358. default:
  2359. break; // Nothing to do here.
  2360. }
  2361. }
  2362. parsed_classes.insert(p_script);
  2363. parsing_classes.erase(p_script);
  2364. //parse sub-classes
  2365. for (int i = 0; i < p_class->members.size(); i++) {
  2366. const GDScriptParser::ClassNode::Member &member = p_class->members[i];
  2367. if (member.type != member.CLASS) {
  2368. continue;
  2369. }
  2370. const GDScriptParser::ClassNode *inner_class = member.m_class;
  2371. StringName name = inner_class->identifier->name;
  2372. Ref<GDScript> &subclass = p_script->subclasses[name];
  2373. GDScript *subclass_ptr = subclass.ptr();
  2374. // Subclass might still be parsing, just skip it
  2375. if (!parsed_classes.has(subclass_ptr) && !parsing_classes.has(subclass_ptr)) {
  2376. Error err = _parse_class_level(subclass_ptr, inner_class, p_keep_state);
  2377. if (err) {
  2378. return err;
  2379. }
  2380. }
  2381. #ifdef TOOLS_ENABLED
  2382. p_script->member_lines[name] = inner_class->start_line;
  2383. #endif
  2384. p_script->constants.insert(name, subclass); //once parsed, goes to the list of constants
  2385. }
  2386. return OK;
  2387. }
  2388. Error GDScriptCompiler::_parse_class_blocks(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  2389. //parse methods
  2390. bool has_ready = false;
  2391. for (int i = 0; i < p_class->members.size(); i++) {
  2392. const GDScriptParser::ClassNode::Member &member = p_class->members[i];
  2393. if (member.type == member.FUNCTION) {
  2394. const GDScriptParser::FunctionNode *function = member.function;
  2395. if (!has_ready && function->identifier->name == "_ready") {
  2396. has_ready = true;
  2397. }
  2398. Error err = _parse_function(p_script, p_class, function);
  2399. if (err) {
  2400. return err;
  2401. }
  2402. } else if (member.type == member.VARIABLE) {
  2403. const GDScriptParser::VariableNode *variable = member.variable;
  2404. if (variable->property == GDScriptParser::VariableNode::PROP_INLINE) {
  2405. if (variable->setter != nullptr) {
  2406. Error err = _parse_setter_getter(p_script, p_class, variable, true);
  2407. if (err) {
  2408. return err;
  2409. }
  2410. }
  2411. if (variable->getter != nullptr) {
  2412. Error err = _parse_setter_getter(p_script, p_class, variable, false);
  2413. if (err) {
  2414. return err;
  2415. }
  2416. }
  2417. }
  2418. }
  2419. }
  2420. {
  2421. // Create an implicit constructor in any case.
  2422. Error err = _parse_function(p_script, p_class, nullptr);
  2423. if (err) {
  2424. return err;
  2425. }
  2426. }
  2427. if (!has_ready && p_class->onready_used) {
  2428. //create a _ready constructor
  2429. Error err = _parse_function(p_script, p_class, nullptr, true);
  2430. if (err) {
  2431. return err;
  2432. }
  2433. }
  2434. #ifdef DEBUG_ENABLED
  2435. //validate instances if keeping state
  2436. if (p_keep_state) {
  2437. for (Set<Object *>::Element *E = p_script->instances.front(); E;) {
  2438. Set<Object *>::Element *N = E->next();
  2439. ScriptInstance *si = E->get()->get_script_instance();
  2440. if (si->is_placeholder()) {
  2441. #ifdef TOOLS_ENABLED
  2442. PlaceHolderScriptInstance *psi = static_cast<PlaceHolderScriptInstance *>(si);
  2443. if (p_script->is_tool()) {
  2444. //re-create as an instance
  2445. p_script->placeholders.erase(psi); //remove placeholder
  2446. GDScriptInstance *instance = memnew(GDScriptInstance);
  2447. instance->base_ref = Object::cast_to<Reference>(E->get());
  2448. instance->members.resize(p_script->member_indices.size());
  2449. instance->script = Ref<GDScript>(p_script);
  2450. instance->owner = E->get();
  2451. //needed for hot reloading
  2452. for (Map<StringName, GDScript::MemberInfo>::Element *F = p_script->member_indices.front(); F; F = F->next()) {
  2453. instance->member_indices_cache[F->key()] = F->get().index;
  2454. }
  2455. instance->owner->set_script_instance(instance);
  2456. /* STEP 2, INITIALIZE AND CONSTRUCT */
  2457. Callable::CallError ce;
  2458. p_script->initializer->call(instance, nullptr, 0, ce);
  2459. if (ce.error != Callable::CallError::CALL_OK) {
  2460. //well, tough luck, not goinna do anything here
  2461. }
  2462. }
  2463. #endif
  2464. } else {
  2465. GDScriptInstance *gi = static_cast<GDScriptInstance *>(si);
  2466. gi->reload_members();
  2467. }
  2468. E = N;
  2469. }
  2470. }
  2471. #endif
  2472. for (int i = 0; i < p_class->members.size(); i++) {
  2473. if (p_class->members[i].type != GDScriptParser::ClassNode::Member::CLASS) {
  2474. continue;
  2475. }
  2476. const GDScriptParser::ClassNode *inner_class = p_class->members[i].m_class;
  2477. StringName name = inner_class->identifier->name;
  2478. GDScript *subclass = p_script->subclasses[name].ptr();
  2479. Error err = _parse_class_blocks(subclass, inner_class, p_keep_state);
  2480. if (err) {
  2481. return err;
  2482. }
  2483. }
  2484. p_script->valid = true;
  2485. return OK;
  2486. }
  2487. void GDScriptCompiler::_make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
  2488. Map<StringName, Ref<GDScript>> old_subclasses;
  2489. if (p_keep_state) {
  2490. old_subclasses = p_script->subclasses;
  2491. }
  2492. p_script->subclasses.clear();
  2493. for (int i = 0; i < p_class->members.size(); i++) {
  2494. if (p_class->members[i].type != GDScriptParser::ClassNode::Member::CLASS) {
  2495. continue;
  2496. }
  2497. const GDScriptParser::ClassNode *inner_class = p_class->members[i].m_class;
  2498. StringName name = inner_class->identifier->name;
  2499. Ref<GDScript> subclass;
  2500. String fully_qualified_name = p_script->fully_qualified_name + "::" + name;
  2501. if (old_subclasses.has(name)) {
  2502. subclass = old_subclasses[name];
  2503. } else {
  2504. Ref<GDScript> orphan_subclass = GDScriptLanguage::get_singleton()->get_orphan_subclass(fully_qualified_name);
  2505. if (orphan_subclass.is_valid()) {
  2506. subclass = orphan_subclass;
  2507. } else {
  2508. subclass.instance();
  2509. }
  2510. }
  2511. subclass->_owner = p_script;
  2512. subclass->fully_qualified_name = fully_qualified_name;
  2513. p_script->subclasses.insert(name, subclass);
  2514. _make_scripts(subclass.ptr(), inner_class, false);
  2515. }
  2516. }
  2517. Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state) {
  2518. err_line = -1;
  2519. err_column = -1;
  2520. error = "";
  2521. parser = p_parser;
  2522. main_script = p_script;
  2523. const GDScriptParser::ClassNode *root = parser->get_tree();
  2524. source = p_script->get_path();
  2525. // The best fully qualified name for a base level script is its file path
  2526. p_script->fully_qualified_name = p_script->path;
  2527. // Create scripts for subclasses beforehand so they can be referenced
  2528. _make_scripts(p_script, root, p_keep_state);
  2529. p_script->_owner = nullptr;
  2530. Error err = _parse_class_level(p_script, root, p_keep_state);
  2531. if (err) {
  2532. return err;
  2533. }
  2534. err = _parse_class_blocks(p_script, root, p_keep_state);
  2535. if (err) {
  2536. return err;
  2537. }
  2538. return OK;
  2539. }
  2540. String GDScriptCompiler::get_error() const {
  2541. return error;
  2542. }
  2543. int GDScriptCompiler::get_error_line() const {
  2544. return err_line;
  2545. }
  2546. int GDScriptCompiler::get_error_column() const {
  2547. return err_column;
  2548. }
  2549. GDScriptCompiler::GDScriptCompiler() {
  2550. }