gdscript_analyzer.cpp 171 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352
  1. /*************************************************************************/
  2. /* gdscript_analyzer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_analyzer.h"
  31. #include "core/config/engine.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/core_string_names.h"
  34. #include "core/io/file_access.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/object/class_db.h"
  37. #include "core/object/script_language.h"
  38. #include "core/templates/hash_map.h"
  39. #include "gdscript.h"
  40. #include "gdscript_utility_functions.h"
  41. #include "scene/resources/packed_scene.h"
  42. static MethodInfo info_from_utility_func(const StringName &p_function) {
  43. ERR_FAIL_COND_V(!Variant::has_utility_function(p_function), MethodInfo());
  44. MethodInfo info(p_function);
  45. if (Variant::has_utility_function_return_value(p_function)) {
  46. info.return_val.type = Variant::get_utility_function_return_type(p_function);
  47. if (info.return_val.type == Variant::NIL) {
  48. info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  49. }
  50. }
  51. if (Variant::is_utility_function_vararg(p_function)) {
  52. info.flags |= METHOD_FLAG_VARARG;
  53. } else {
  54. for (int i = 0; i < Variant::get_utility_function_argument_count(p_function); i++) {
  55. PropertyInfo pi;
  56. #ifdef DEBUG_METHODS_ENABLED
  57. pi.name = Variant::get_utility_function_argument_name(p_function, i);
  58. #else
  59. pi.name = "arg" + itos(i + 1);
  60. #endif
  61. pi.type = Variant::get_utility_function_argument_type(p_function, i);
  62. if (pi.type == Variant::NIL) {
  63. pi.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  64. }
  65. info.arguments.push_back(pi);
  66. }
  67. }
  68. return info;
  69. }
  70. static GDScriptParser::DataType make_callable_type(const MethodInfo &p_info) {
  71. GDScriptParser::DataType type;
  72. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  73. type.kind = GDScriptParser::DataType::BUILTIN;
  74. type.builtin_type = Variant::CALLABLE;
  75. type.is_constant = true;
  76. type.method_info = p_info;
  77. return type;
  78. }
  79. static GDScriptParser::DataType make_signal_type(const MethodInfo &p_info) {
  80. GDScriptParser::DataType type;
  81. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  82. type.kind = GDScriptParser::DataType::BUILTIN;
  83. type.builtin_type = Variant::SIGNAL;
  84. type.is_constant = true;
  85. type.method_info = p_info;
  86. return type;
  87. }
  88. static GDScriptParser::DataType make_native_meta_type(const StringName &p_class_name) {
  89. GDScriptParser::DataType type;
  90. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  91. type.kind = GDScriptParser::DataType::NATIVE;
  92. type.builtin_type = Variant::OBJECT;
  93. type.is_constant = true;
  94. type.native_type = p_class_name;
  95. type.is_meta_type = true;
  96. return type;
  97. }
  98. static GDScriptParser::DataType make_native_enum_type(const StringName &p_native_class, const StringName &p_enum_name) {
  99. GDScriptParser::DataType type;
  100. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  101. type.kind = GDScriptParser::DataType::ENUM;
  102. type.builtin_type = Variant::INT;
  103. type.is_constant = true;
  104. type.is_meta_type = true;
  105. List<StringName> enum_values;
  106. ClassDB::get_enum_constants(p_native_class, p_enum_name, &enum_values);
  107. for (const StringName &E : enum_values) {
  108. type.enum_values[E] = ClassDB::get_integer_constant(p_native_class, E);
  109. }
  110. return type;
  111. }
  112. static GDScriptParser::DataType make_builtin_meta_type(Variant::Type p_type) {
  113. GDScriptParser::DataType type;
  114. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  115. type.kind = GDScriptParser::DataType::BUILTIN;
  116. type.builtin_type = p_type;
  117. type.is_constant = true;
  118. type.is_meta_type = true;
  119. return type;
  120. }
  121. bool GDScriptAnalyzer::has_member_name_conflict_in_script_class(const StringName &p_member_name, const GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_member) {
  122. if (p_class->members_indices.has(p_member_name)) {
  123. int index = p_class->members_indices[p_member_name];
  124. const GDScriptParser::ClassNode::Member *member = &p_class->members[index];
  125. if (member->type == GDScriptParser::ClassNode::Member::VARIABLE ||
  126. member->type == GDScriptParser::ClassNode::Member::CONSTANT ||
  127. member->type == GDScriptParser::ClassNode::Member::ENUM ||
  128. member->type == GDScriptParser::ClassNode::Member::ENUM_VALUE ||
  129. member->type == GDScriptParser::ClassNode::Member::CLASS ||
  130. member->type == GDScriptParser::ClassNode::Member::SIGNAL) {
  131. return true;
  132. }
  133. if (p_member->type != GDScriptParser::Node::FUNCTION && member->type == GDScriptParser::ClassNode::Member::FUNCTION) {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. bool GDScriptAnalyzer::has_member_name_conflict_in_native_type(const StringName &p_member_name, const StringName &p_native_type_string) {
  140. if (ClassDB::has_signal(p_native_type_string, p_member_name)) {
  141. return true;
  142. }
  143. if (ClassDB::has_property(p_native_type_string, p_member_name)) {
  144. return true;
  145. }
  146. if (ClassDB::has_integer_constant(p_native_type_string, p_member_name)) {
  147. return true;
  148. }
  149. if (p_member_name == CoreStringNames::get_singleton()->_script) {
  150. return true;
  151. }
  152. return false;
  153. }
  154. Error GDScriptAnalyzer::check_native_member_name_conflict(const StringName &p_member_name, const GDScriptParser::Node *p_member_node, const StringName &p_native_type_string) {
  155. if (has_member_name_conflict_in_native_type(p_member_name, p_native_type_string)) {
  156. push_error(vformat(R"(Member "%s" redefined (original in native class '%s'))", p_member_name, p_native_type_string), p_member_node);
  157. return ERR_PARSE_ERROR;
  158. }
  159. if (class_exists(p_member_name)) {
  160. push_error(vformat(R"(The member "%s" shadows a native class.)", p_member_name), p_member_node);
  161. return ERR_PARSE_ERROR;
  162. }
  163. if (GDScriptParser::get_builtin_type(p_member_name) != Variant::VARIANT_MAX) {
  164. push_error(vformat(R"(The member "%s" cannot have the same name as a builtin type.)", p_member_name), p_member_node);
  165. return ERR_PARSE_ERROR;
  166. }
  167. return OK;
  168. }
  169. Error GDScriptAnalyzer::check_class_member_name_conflict(const GDScriptParser::ClassNode *p_class_node, const StringName &p_member_name, const GDScriptParser::Node *p_member_node) {
  170. const GDScriptParser::DataType *current_data_type = &p_class_node->base_type;
  171. while (current_data_type && current_data_type->kind == GDScriptParser::DataType::Kind::CLASS) {
  172. GDScriptParser::ClassNode *current_class_node = current_data_type->class_type;
  173. if (has_member_name_conflict_in_script_class(p_member_name, current_class_node, p_member_node)) {
  174. push_error(vformat(R"(The member "%s" already exists in parent class %s.)", p_member_name, current_class_node->identifier->name),
  175. p_member_node);
  176. return ERR_PARSE_ERROR;
  177. }
  178. current_data_type = &current_class_node->base_type;
  179. }
  180. // No need for native class recursion because Node exposes all Object's properties.
  181. if (current_data_type && current_data_type->kind == GDScriptParser::DataType::Kind::NATIVE) {
  182. if (current_data_type->native_type != StringName()) {
  183. return check_native_member_name_conflict(
  184. p_member_name,
  185. p_member_node,
  186. current_data_type->native_type);
  187. }
  188. }
  189. return OK;
  190. }
  191. Error GDScriptAnalyzer::resolve_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive) {
  192. if (p_class->base_type.is_set()) {
  193. // Already resolved
  194. return OK;
  195. }
  196. if (p_class->identifier) {
  197. StringName class_name = p_class->identifier->name;
  198. if (class_exists(class_name)) {
  199. push_error(vformat(R"(Class "%s" hides a native class.)", class_name), p_class->identifier);
  200. } else if (ScriptServer::is_global_class(class_name) && (ScriptServer::get_global_class_path(class_name) != parser->script_path || p_class != parser->head)) {
  201. push_error(vformat(R"(Class "%s" hides a global script class.)", class_name), p_class->identifier);
  202. } else if (ProjectSettings::get_singleton()->has_autoload(class_name) && ProjectSettings::get_singleton()->get_autoload(class_name).is_singleton) {
  203. push_error(vformat(R"(Class "%s" hides an autoload singleton.)", class_name), p_class->identifier);
  204. }
  205. }
  206. GDScriptParser::DataType result;
  207. // Set datatype for class.
  208. GDScriptParser::DataType class_type;
  209. class_type.is_constant = true;
  210. class_type.is_meta_type = true;
  211. class_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  212. class_type.kind = GDScriptParser::DataType::CLASS;
  213. class_type.class_type = p_class;
  214. class_type.script_path = parser->script_path;
  215. class_type.builtin_type = Variant::OBJECT;
  216. p_class->set_datatype(class_type);
  217. if (!p_class->extends_used) {
  218. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  219. result.kind = GDScriptParser::DataType::NATIVE;
  220. result.native_type = SNAME("RefCounted");
  221. } else {
  222. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  223. GDScriptParser::DataType base;
  224. int extends_index = 0;
  225. if (!p_class->extends_path.is_empty()) {
  226. if (p_class->extends_path.is_relative_path()) {
  227. p_class->extends_path = class_type.script_path.get_base_dir().path_join(p_class->extends_path).simplify_path();
  228. }
  229. Ref<GDScriptParserRef> ext_parser = get_parser_for(p_class->extends_path);
  230. if (ext_parser.is_null()) {
  231. push_error(vformat(R"(Could not resolve super class path "%s".)", p_class->extends_path), p_class);
  232. return ERR_PARSE_ERROR;
  233. }
  234. Error err = ext_parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  235. if (err != OK) {
  236. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", p_class->extends_path), p_class);
  237. return err;
  238. }
  239. base = ext_parser->get_parser()->head->get_datatype();
  240. } else {
  241. if (p_class->extends.is_empty()) {
  242. push_error("Could not resolve an empty super class path.", p_class);
  243. return ERR_PARSE_ERROR;
  244. }
  245. const StringName &name = p_class->extends[extends_index++];
  246. base.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  247. if (ScriptServer::is_global_class(name)) {
  248. String base_path = ScriptServer::get_global_class_path(name);
  249. if (base_path == parser->script_path) {
  250. base = parser->head->get_datatype();
  251. } else {
  252. Ref<GDScriptParserRef> base_parser = get_parser_for(base_path);
  253. if (base_parser.is_null()) {
  254. push_error(vformat(R"(Could not resolve super class "%s".)", name), p_class);
  255. return ERR_PARSE_ERROR;
  256. }
  257. Error err = base_parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  258. if (err != OK) {
  259. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", name), p_class);
  260. return err;
  261. }
  262. base = base_parser->get_parser()->head->get_datatype();
  263. }
  264. } else if (ProjectSettings::get_singleton()->has_autoload(name) && ProjectSettings::get_singleton()->get_autoload(name).is_singleton) {
  265. const ProjectSettings::AutoloadInfo &info = ProjectSettings::get_singleton()->get_autoload(name);
  266. if (info.path.get_extension().to_lower() != ".gd") {
  267. push_error(vformat(R"(Singleton %s is not a GDScript.)", info.name), p_class);
  268. return ERR_PARSE_ERROR;
  269. }
  270. Ref<GDScriptParserRef> info_parser = get_parser_for(info.path);
  271. if (info_parser.is_null()) {
  272. push_error(vformat(R"(Could not parse singleton from "%s".)", info.path), p_class);
  273. return ERR_PARSE_ERROR;
  274. }
  275. Error err = info_parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  276. if (err != OK) {
  277. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", name), p_class);
  278. return err;
  279. }
  280. } else if (class_exists(name) && ClassDB::can_instantiate(name)) {
  281. base.kind = GDScriptParser::DataType::NATIVE;
  282. base.native_type = name;
  283. } else {
  284. // Look for other classes in script.
  285. GDScriptParser::ClassNode *look_class = p_class;
  286. bool found = false;
  287. while (look_class != nullptr) {
  288. if (look_class->identifier && look_class->identifier->name == name) {
  289. if (!look_class->get_datatype().is_set()) {
  290. Error err = resolve_inheritance(look_class, false);
  291. if (err) {
  292. return err;
  293. }
  294. }
  295. base = look_class->get_datatype();
  296. found = true;
  297. break;
  298. }
  299. if (look_class->members_indices.has(name) && look_class->get_member(name).type == GDScriptParser::ClassNode::Member::CLASS) {
  300. GDScriptParser::ClassNode::Member member = look_class->get_member(name);
  301. if (!member.m_class->get_datatype().is_set()) {
  302. Error err = resolve_inheritance(member.m_class, false);
  303. if (err) {
  304. return err;
  305. }
  306. }
  307. base = member.m_class->get_datatype();
  308. found = true;
  309. break;
  310. }
  311. look_class = look_class->outer;
  312. }
  313. if (!found) {
  314. push_error(vformat(R"(Could not find base class "%s".)", name), p_class);
  315. return ERR_PARSE_ERROR;
  316. }
  317. }
  318. }
  319. for (int index = extends_index; index < p_class->extends.size(); index++) {
  320. if (base.kind != GDScriptParser::DataType::CLASS) {
  321. push_error(R"(Super type "%s" is not a GDScript. Cannot get nested types.)", p_class);
  322. return ERR_PARSE_ERROR;
  323. }
  324. // TODO: Extends could use identifier nodes. That way errors can be pointed out properly and it can be used here.
  325. GDScriptParser::IdentifierNode *id = parser->alloc_node<GDScriptParser::IdentifierNode>();
  326. id->name = p_class->extends[index];
  327. reduce_identifier_from_base(id, &base);
  328. GDScriptParser::DataType id_type = id->get_datatype();
  329. if (!id_type.is_set()) {
  330. push_error(vformat(R"(Could not find type "%s" under base "%s".)", id->name, base.to_string()), p_class);
  331. }
  332. base = id_type;
  333. }
  334. result = base;
  335. }
  336. if (!result.is_set()) {
  337. // TODO: More specific error messages.
  338. push_error(vformat(R"(Could not resolve inheritance for class "%s".)", p_class->identifier == nullptr ? "<main>" : p_class->identifier->name), p_class);
  339. return ERR_PARSE_ERROR;
  340. }
  341. // Check for cyclic inheritance.
  342. const GDScriptParser::ClassNode *base_class = result.class_type;
  343. while (base_class) {
  344. if (base_class->fqcn == p_class->fqcn) {
  345. push_error("Cyclic inheritance.", p_class);
  346. return ERR_PARSE_ERROR;
  347. }
  348. base_class = base_class->base_type.class_type;
  349. }
  350. p_class->base_type = result;
  351. class_type.native_type = result.native_type;
  352. p_class->set_datatype(class_type);
  353. if (p_recursive) {
  354. for (int i = 0; i < p_class->members.size(); i++) {
  355. if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
  356. Error err = resolve_inheritance(p_class->members[i].m_class, true);
  357. if (err) {
  358. return err;
  359. }
  360. }
  361. }
  362. }
  363. return OK;
  364. }
  365. GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::TypeNode *p_type) {
  366. GDScriptParser::DataType result;
  367. if (p_type == nullptr) {
  368. result.kind = GDScriptParser::DataType::VARIANT;
  369. return result;
  370. }
  371. result.type_source = result.ANNOTATED_EXPLICIT;
  372. result.builtin_type = Variant::OBJECT;
  373. if (p_type->type_chain.is_empty()) {
  374. // void.
  375. result.kind = GDScriptParser::DataType::BUILTIN;
  376. result.builtin_type = Variant::NIL;
  377. p_type->set_datatype(result);
  378. return result;
  379. }
  380. StringName first = p_type->type_chain[0]->name;
  381. if (first == SNAME("Variant")) {
  382. result.kind = GDScriptParser::DataType::VARIANT;
  383. if (p_type->type_chain.size() > 1) {
  384. push_error(R"("Variant" type don't contain nested types.)", p_type->type_chain[1]);
  385. return GDScriptParser::DataType();
  386. }
  387. return result;
  388. }
  389. if (first == SNAME("Object")) {
  390. result.kind = GDScriptParser::DataType::NATIVE;
  391. result.native_type = SNAME("Object");
  392. if (p_type->type_chain.size() > 1) {
  393. push_error(R"("Object" type don't contain nested types.)", p_type->type_chain[1]);
  394. return GDScriptParser::DataType();
  395. }
  396. return result;
  397. }
  398. if (GDScriptParser::get_builtin_type(first) < Variant::VARIANT_MAX) {
  399. // Built-in types.
  400. if (p_type->type_chain.size() > 1) {
  401. push_error(R"(Built-in types don't contain nested types.)", p_type->type_chain[1]);
  402. return GDScriptParser::DataType();
  403. }
  404. result.kind = GDScriptParser::DataType::BUILTIN;
  405. result.builtin_type = GDScriptParser::get_builtin_type(first);
  406. if (result.builtin_type == Variant::ARRAY) {
  407. GDScriptParser::DataType container_type = resolve_datatype(p_type->container_type);
  408. if (container_type.kind != GDScriptParser::DataType::VARIANT) {
  409. container_type.is_meta_type = false;
  410. container_type.is_constant = false;
  411. result.set_container_element_type(container_type);
  412. }
  413. }
  414. } else if (class_exists(first)) {
  415. // Native engine classes.
  416. result.kind = GDScriptParser::DataType::NATIVE;
  417. result.native_type = first;
  418. } else if (ScriptServer::is_global_class(first)) {
  419. if (parser->script_path == ScriptServer::get_global_class_path(first)) {
  420. result = parser->head->get_datatype();
  421. } else {
  422. String path = ScriptServer::get_global_class_path(first);
  423. String ext = path.get_extension();
  424. if (ext == GDScriptLanguage::get_singleton()->get_extension()) {
  425. Ref<GDScriptParserRef> ref = get_parser_for(path);
  426. if (!ref.is_valid() || ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED) != OK) {
  427. push_error(vformat(R"(Could not parse global class "%s" from "%s".)", first, ScriptServer::get_global_class_path(first)), p_type);
  428. return GDScriptParser::DataType();
  429. }
  430. result = ref->get_parser()->head->get_datatype();
  431. } else {
  432. result.kind = GDScriptParser::DataType::SCRIPT;
  433. result.native_type = ScriptServer::get_global_class_native_base(first);
  434. result.script_type = ResourceLoader::load(path, "Script");
  435. result.script_path = path;
  436. result.is_constant = true;
  437. result.is_meta_type = false;
  438. }
  439. }
  440. } else if (ProjectSettings::get_singleton()->has_autoload(first) && ProjectSettings::get_singleton()->get_autoload(first).is_singleton) {
  441. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(first);
  442. Ref<GDScriptParserRef> ref = get_parser_for(autoload.path);
  443. if (ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED) != OK) {
  444. push_error(vformat(R"(Could not parse singleton "%s" from "%s".)", first, autoload.path), p_type);
  445. return GDScriptParser::DataType();
  446. }
  447. result = ref->get_parser()->head->get_datatype();
  448. } else if (ClassDB::has_enum(parser->current_class->base_type.native_type, first)) {
  449. // Native enum in current class.
  450. result = make_native_enum_type(parser->current_class->base_type.native_type, first);
  451. } else {
  452. // Classes in current scope.
  453. GDScriptParser::ClassNode *script_class = parser->current_class;
  454. bool found = false;
  455. while (!found && script_class != nullptr) {
  456. if (script_class->identifier && script_class->identifier->name == first) {
  457. result = script_class->get_datatype();
  458. found = true;
  459. break;
  460. }
  461. if (script_class->members_indices.has(first)) {
  462. GDScriptParser::ClassNode::Member member = script_class->members[script_class->members_indices[first]];
  463. switch (member.type) {
  464. case GDScriptParser::ClassNode::Member::CLASS:
  465. result = member.m_class->get_datatype();
  466. found = true;
  467. break;
  468. case GDScriptParser::ClassNode::Member::ENUM:
  469. result = member.m_enum->get_datatype();
  470. found = true;
  471. break;
  472. case GDScriptParser::ClassNode::Member::CONSTANT:
  473. if (member.constant->get_datatype().is_meta_type) {
  474. result = member.constant->get_datatype();
  475. result.is_meta_type = false;
  476. found = true;
  477. break;
  478. } else if (Ref<Script>(member.constant->initializer->reduced_value).is_valid()) {
  479. Ref<GDScript> gdscript = member.constant->initializer->reduced_value;
  480. if (gdscript.is_valid()) {
  481. Ref<GDScriptParserRef> ref = get_parser_for(gdscript->get_path());
  482. if (ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED) != OK) {
  483. push_error(vformat(R"(Could not parse script from "%s".)", gdscript->get_path()), p_type);
  484. return GDScriptParser::DataType();
  485. }
  486. result = ref->get_parser()->head->get_datatype();
  487. result.is_meta_type = false;
  488. } else {
  489. Ref<Script> script = member.constant->initializer->reduced_value;
  490. result.kind = GDScriptParser::DataType::SCRIPT;
  491. result.builtin_type = Variant::OBJECT;
  492. result.script_type = script;
  493. result.script_path = script->get_path();
  494. result.native_type = script->get_instance_base_type();
  495. result.is_meta_type = false;
  496. }
  497. break;
  498. }
  499. [[fallthrough]];
  500. default:
  501. push_error(vformat(R"("%s" is a %s but does not contain a type.)", first, member.get_type_name()), p_type);
  502. return GDScriptParser::DataType();
  503. }
  504. }
  505. script_class = script_class->outer;
  506. }
  507. }
  508. if (!result.is_set()) {
  509. push_error(vformat(R"("%s" was not found in the current scope.)", first), p_type);
  510. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  511. return result;
  512. }
  513. if (p_type->type_chain.size() > 1) {
  514. if (result.kind == GDScriptParser::DataType::CLASS) {
  515. for (int i = 1; i < p_type->type_chain.size(); i++) {
  516. GDScriptParser::DataType base = result;
  517. reduce_identifier_from_base(p_type->type_chain[i], &base);
  518. result = p_type->type_chain[i]->get_datatype();
  519. if (!result.is_set()) {
  520. push_error(vformat(R"(Could not find type "%s" under base "%s".)", p_type->type_chain[i]->name, base.to_string()), p_type->type_chain[1]);
  521. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  522. return result;
  523. } else if (!result.is_meta_type) {
  524. push_error(vformat(R"(Member "%s" under base "%s" is not a valid type.)", p_type->type_chain[i]->name, base.to_string()), p_type->type_chain[1]);
  525. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  526. return result;
  527. }
  528. }
  529. } else if (result.kind == GDScriptParser::DataType::NATIVE) {
  530. // Only enums allowed for native.
  531. if (ClassDB::has_enum(result.native_type, p_type->type_chain[1]->name)) {
  532. if (p_type->type_chain.size() > 2) {
  533. push_error(R"(Enums cannot contain nested types.)", p_type->type_chain[2]);
  534. } else {
  535. result = make_native_enum_type(result.native_type, p_type->type_chain[1]->name);
  536. }
  537. }
  538. } else {
  539. push_error(vformat(R"(Could not find nested type "%s" under base "%s".)", p_type->type_chain[1]->name, result.to_string()), p_type->type_chain[1]);
  540. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  541. return result;
  542. }
  543. }
  544. if (result.builtin_type != Variant::ARRAY && p_type->container_type != nullptr) {
  545. push_error("Only arrays can specify the collection element type.", p_type);
  546. }
  547. p_type->set_datatype(result);
  548. return result;
  549. }
  550. void GDScriptAnalyzer::resolve_class_interface(GDScriptParser::ClassNode *p_class) {
  551. if (p_class->resolved_interface) {
  552. return;
  553. }
  554. p_class->resolved_interface = true;
  555. GDScriptParser::ClassNode *previous_class = parser->current_class;
  556. parser->current_class = p_class;
  557. for (int i = 0; i < p_class->members.size(); i++) {
  558. GDScriptParser::ClassNode::Member member = p_class->members[i];
  559. switch (member.type) {
  560. case GDScriptParser::ClassNode::Member::VARIABLE: {
  561. check_class_member_name_conflict(p_class, member.variable->identifier->name, member.variable);
  562. GDScriptParser::DataType datatype;
  563. datatype.kind = GDScriptParser::DataType::VARIANT;
  564. datatype.type_source = GDScriptParser::DataType::UNDETECTED;
  565. GDScriptParser::DataType specified_type;
  566. if (member.variable->datatype_specifier != nullptr) {
  567. specified_type = resolve_datatype(member.variable->datatype_specifier);
  568. specified_type.is_meta_type = false;
  569. }
  570. if (member.variable->initializer != nullptr) {
  571. member.variable->set_datatype(datatype); // Allow recursive usage.
  572. reduce_expression(member.variable->initializer);
  573. if ((member.variable->infer_datatype || (member.variable->datatype_specifier != nullptr && specified_type.has_container_element_type())) && member.variable->initializer->type == GDScriptParser::Node::ARRAY) {
  574. // Typed array.
  575. GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(member.variable->initializer);
  576. // Can only infer typed array if it has elements.
  577. if ((member.variable->infer_datatype && array->elements.size() > 0) || member.variable->datatype_specifier != nullptr) {
  578. update_array_literal_element_type(specified_type, array);
  579. }
  580. }
  581. datatype = member.variable->initializer->get_datatype();
  582. if (datatype.type_source != GDScriptParser::DataType::UNDETECTED) {
  583. datatype.type_source = GDScriptParser::DataType::INFERRED;
  584. }
  585. }
  586. // Check if initializer is an unset identifier (ie: a variable within scope, but declared below)
  587. if (member.variable->initializer && !member.variable->initializer->get_datatype().is_set()) {
  588. if (member.variable->initializer->type == GDScriptParser::Node::IDENTIFIER) {
  589. GDScriptParser::IdentifierNode *initializer_identifier = static_cast<GDScriptParser::IdentifierNode *>(member.variable->initializer);
  590. push_error(vformat(R"(Identifier "%s" must be declared above current variable.)", initializer_identifier->name), member.variable->initializer);
  591. } else {
  592. ERR_PRINT("Parser bug (please report): tried to assign unset node without an identifier.");
  593. }
  594. }
  595. if (member.variable->datatype_specifier != nullptr) {
  596. datatype = specified_type;
  597. if (member.variable->initializer != nullptr) {
  598. if (!is_type_compatible(datatype, member.variable->initializer->get_datatype(), true, member.variable->initializer)) {
  599. // Try reverse test since it can be a masked subtype.
  600. if (!is_type_compatible(member.variable->initializer->get_datatype(), datatype, true, member.variable->initializer)) {
  601. push_error(vformat(R"(Value of type "%s" cannot be assigned to a variable of type "%s".)", member.variable->initializer->get_datatype().to_string(), datatype.to_string()), member.variable->initializer);
  602. } else {
  603. // TODO: Add warning.
  604. mark_node_unsafe(member.variable->initializer);
  605. member.variable->use_conversion_assign = true;
  606. }
  607. } else if (datatype.builtin_type == Variant::INT && member.variable->initializer->get_datatype().builtin_type == Variant::FLOAT) {
  608. #ifdef DEBUG_ENABLED
  609. parser->push_warning(member.variable->initializer, GDScriptWarning::NARROWING_CONVERSION);
  610. #endif
  611. }
  612. if (member.variable->initializer->get_datatype().is_variant()) {
  613. // TODO: Warn unsafe assign.
  614. mark_node_unsafe(member.variable->initializer);
  615. member.variable->use_conversion_assign = true;
  616. }
  617. }
  618. } else if (member.variable->infer_datatype) {
  619. if (member.variable->initializer == nullptr) {
  620. push_error(vformat(R"(Cannot infer the type of "%s" variable because there's no default value.)", member.variable->identifier->name), member.variable->identifier);
  621. } else if (!datatype.is_set() || datatype.has_no_type()) {
  622. push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value doesn't have a set type.)", member.variable->identifier->name), member.variable->initializer);
  623. } else if (datatype.is_variant()) {
  624. push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is Variant. Use explicit "Variant" type if this is intended.)", member.variable->identifier->name), member.variable->initializer);
  625. } else if (datatype.builtin_type == Variant::NIL) {
  626. push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is "null".)", member.variable->identifier->name), member.variable->initializer);
  627. }
  628. datatype.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  629. }
  630. datatype.is_constant = false;
  631. member.variable->set_datatype(datatype);
  632. // Apply annotations.
  633. for (GDScriptParser::AnnotationNode *&E : member.variable->annotations) {
  634. E->apply(parser, member.variable);
  635. }
  636. } break;
  637. case GDScriptParser::ClassNode::Member::CONSTANT: {
  638. check_class_member_name_conflict(p_class, member.constant->identifier->name, member.constant);
  639. reduce_expression(member.constant->initializer);
  640. GDScriptParser::DataType specified_type;
  641. if (member.constant->datatype_specifier != nullptr) {
  642. specified_type = resolve_datatype(member.constant->datatype_specifier);
  643. specified_type.is_meta_type = false;
  644. }
  645. GDScriptParser::DataType datatype;
  646. if (member.constant->initializer) {
  647. datatype = member.constant->initializer->get_datatype();
  648. if (member.constant->initializer->type == GDScriptParser::Node::ARRAY) {
  649. GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(member.constant->initializer);
  650. const_fold_array(array);
  651. // Can only infer typed array if it has elements.
  652. if (array->elements.size() > 0 || (member.constant->datatype_specifier != nullptr && specified_type.has_container_element_type())) {
  653. update_array_literal_element_type(specified_type, array);
  654. }
  655. } else if (member.constant->initializer->type == GDScriptParser::Node::DICTIONARY) {
  656. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(member.constant->initializer));
  657. }
  658. if (!member.constant->initializer->is_constant) {
  659. push_error(R"(Initializer for a constant must be a constant expression.)", member.constant->initializer);
  660. }
  661. if (member.constant->datatype_specifier != nullptr) {
  662. datatype = specified_type;
  663. if (!is_type_compatible(datatype, member.constant->initializer->get_datatype(), true)) {
  664. push_error(vformat(R"(Value of type "%s" cannot be initialized to constant of type "%s".)", member.constant->initializer->get_datatype().to_string(), datatype.to_string()), member.constant->initializer);
  665. } else if (datatype.builtin_type == Variant::INT && member.constant->initializer->get_datatype().builtin_type == Variant::FLOAT) {
  666. #ifdef DEBUG_ENABLED
  667. parser->push_warning(member.constant->initializer, GDScriptWarning::NARROWING_CONVERSION);
  668. #endif
  669. }
  670. }
  671. }
  672. datatype.is_constant = true;
  673. member.constant->set_datatype(datatype);
  674. // Apply annotations.
  675. for (GDScriptParser::AnnotationNode *&E : member.constant->annotations) {
  676. E->apply(parser, member.constant);
  677. }
  678. } break;
  679. case GDScriptParser::ClassNode::Member::SIGNAL: {
  680. check_class_member_name_conflict(p_class, member.signal->identifier->name, member.signal);
  681. for (int j = 0; j < member.signal->parameters.size(); j++) {
  682. GDScriptParser::DataType signal_type = resolve_datatype(member.signal->parameters[j]->datatype_specifier);
  683. signal_type.is_meta_type = false;
  684. member.signal->parameters[j]->set_datatype(signal_type);
  685. }
  686. // TODO: Make MethodInfo from signal.
  687. GDScriptParser::DataType signal_type;
  688. signal_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  689. signal_type.kind = GDScriptParser::DataType::BUILTIN;
  690. signal_type.builtin_type = Variant::SIGNAL;
  691. member.signal->set_datatype(signal_type);
  692. // Apply annotations.
  693. for (GDScriptParser::AnnotationNode *&E : member.signal->annotations) {
  694. E->apply(parser, member.signal);
  695. }
  696. } break;
  697. case GDScriptParser::ClassNode::Member::ENUM: {
  698. check_class_member_name_conflict(p_class, member.m_enum->identifier->name, member.m_enum);
  699. GDScriptParser::DataType enum_type;
  700. enum_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  701. enum_type.kind = GDScriptParser::DataType::ENUM;
  702. enum_type.builtin_type = Variant::DICTIONARY;
  703. enum_type.enum_type = member.m_enum->identifier->name;
  704. enum_type.native_type = p_class->fqcn + "." + member.m_enum->identifier->name;
  705. enum_type.is_meta_type = true;
  706. enum_type.is_constant = true;
  707. // Enums can't be nested, so we can safely override this.
  708. current_enum = member.m_enum;
  709. for (int j = 0; j < member.m_enum->values.size(); j++) {
  710. GDScriptParser::EnumNode::Value &element = member.m_enum->values.write[j];
  711. if (element.custom_value) {
  712. reduce_expression(element.custom_value);
  713. if (!element.custom_value->is_constant) {
  714. push_error(R"(Enum values must be constant.)", element.custom_value);
  715. } else if (element.custom_value->reduced_value.get_type() != Variant::INT) {
  716. push_error(R"(Enum values must be integers.)", element.custom_value);
  717. } else {
  718. element.value = element.custom_value->reduced_value;
  719. element.resolved = true;
  720. }
  721. } else {
  722. if (element.index > 0) {
  723. element.value = element.parent_enum->values[element.index - 1].value + 1;
  724. } else {
  725. element.value = 0;
  726. }
  727. element.resolved = true;
  728. }
  729. enum_type.enum_values[element.identifier->name] = element.value;
  730. }
  731. current_enum = nullptr;
  732. member.m_enum->set_datatype(enum_type);
  733. // Apply annotations.
  734. for (GDScriptParser::AnnotationNode *&E : member.m_enum->annotations) {
  735. E->apply(parser, member.m_enum);
  736. }
  737. } break;
  738. case GDScriptParser::ClassNode::Member::FUNCTION:
  739. resolve_function_signature(member.function);
  740. break;
  741. case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
  742. if (member.enum_value.custom_value) {
  743. check_class_member_name_conflict(p_class, member.enum_value.identifier->name, member.enum_value.custom_value);
  744. current_enum = member.enum_value.parent_enum;
  745. reduce_expression(member.enum_value.custom_value);
  746. current_enum = nullptr;
  747. if (!member.enum_value.custom_value->is_constant) {
  748. push_error(R"(Enum values must be constant.)", member.enum_value.custom_value);
  749. } else if (member.enum_value.custom_value->reduced_value.get_type() != Variant::INT) {
  750. push_error(R"(Enum values must be integers.)", member.enum_value.custom_value);
  751. } else {
  752. member.enum_value.value = member.enum_value.custom_value->reduced_value;
  753. member.enum_value.resolved = true;
  754. }
  755. } else {
  756. check_class_member_name_conflict(p_class, member.enum_value.identifier->name, member.enum_value.parent_enum);
  757. if (member.enum_value.index > 0) {
  758. member.enum_value.value = member.enum_value.parent_enum->values[member.enum_value.index - 1].value + 1;
  759. } else {
  760. member.enum_value.value = 0;
  761. }
  762. member.enum_value.resolved = true;
  763. }
  764. // Also update the original references.
  765. member.enum_value.parent_enum->values.write[member.enum_value.index] = member.enum_value;
  766. p_class->members.write[i].enum_value = member.enum_value;
  767. } break;
  768. case GDScriptParser::ClassNode::Member::CLASS:
  769. check_class_member_name_conflict(p_class, member.m_class->identifier->name, member.m_class);
  770. break;
  771. case GDScriptParser::ClassNode::Member::GROUP:
  772. // No-op, but needed to silence warnings.
  773. break;
  774. case GDScriptParser::ClassNode::Member::UNDEFINED:
  775. ERR_PRINT("Trying to resolve undefined member.");
  776. break;
  777. }
  778. }
  779. // Recurse nested classes.
  780. for (int i = 0; i < p_class->members.size(); i++) {
  781. GDScriptParser::ClassNode::Member member = p_class->members[i];
  782. if (member.type != GDScriptParser::ClassNode::Member::CLASS) {
  783. continue;
  784. }
  785. resolve_class_interface(member.m_class);
  786. }
  787. parser->current_class = previous_class;
  788. }
  789. void GDScriptAnalyzer::resolve_class_body(GDScriptParser::ClassNode *p_class) {
  790. if (p_class->resolved_body) {
  791. return;
  792. }
  793. p_class->resolved_body = true;
  794. GDScriptParser::ClassNode *previous_class = parser->current_class;
  795. parser->current_class = p_class;
  796. // Do functions and properties now.
  797. for (int i = 0; i < p_class->members.size(); i++) {
  798. GDScriptParser::ClassNode::Member member = p_class->members[i];
  799. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
  800. // Apply annotations.
  801. for (GDScriptParser::AnnotationNode *&E : member.function->annotations) {
  802. E->apply(parser, member.function);
  803. }
  804. #ifdef DEBUG_ENABLED
  805. HashSet<uint32_t> previously_ignored = parser->ignored_warning_codes;
  806. for (uint32_t ignored_warning : member.function->ignored_warnings) {
  807. parser->ignored_warning_codes.insert(ignored_warning);
  808. }
  809. #endif // DEBUG_ENABLED
  810. resolve_function_body(member.function);
  811. #ifdef DEBUG_ENABLED
  812. parser->ignored_warning_codes = previously_ignored;
  813. #endif // DEBUG_ENABLED
  814. } else if (member.type == GDScriptParser::ClassNode::Member::VARIABLE && member.variable->property != GDScriptParser::VariableNode::PROP_NONE) {
  815. if (member.variable->property == GDScriptParser::VariableNode::PROP_INLINE) {
  816. if (member.variable->getter != nullptr) {
  817. member.variable->getter->set_datatype(member.variable->datatype);
  818. resolve_function_body(member.variable->getter);
  819. }
  820. if (member.variable->setter != nullptr) {
  821. resolve_function_signature(member.variable->setter);
  822. if (member.variable->setter->parameters.size() > 0) {
  823. member.variable->setter->parameters[0]->datatype_specifier = member.variable->datatype_specifier;
  824. member.variable->setter->parameters[0]->set_datatype(member.get_datatype());
  825. }
  826. resolve_function_body(member.variable->setter);
  827. }
  828. }
  829. }
  830. }
  831. parser->current_class = previous_class;
  832. // Recurse nested classes.
  833. for (int i = 0; i < p_class->members.size(); i++) {
  834. GDScriptParser::ClassNode::Member member = p_class->members[i];
  835. if (member.type != GDScriptParser::ClassNode::Member::CLASS) {
  836. continue;
  837. }
  838. resolve_class_body(member.m_class);
  839. }
  840. // Check unused variables and datatypes of property getters and setters.
  841. for (int i = 0; i < p_class->members.size(); i++) {
  842. GDScriptParser::ClassNode::Member member = p_class->members[i];
  843. if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) {
  844. #ifdef DEBUG_ENABLED
  845. HashSet<uint32_t> previously_ignored = parser->ignored_warning_codes;
  846. for (uint32_t ignored_warning : member.function->ignored_warnings) {
  847. parser->ignored_warning_codes.insert(ignored_warning);
  848. }
  849. if (member.variable->usages == 0 && String(member.variable->identifier->name).begins_with("_")) {
  850. parser->push_warning(member.variable->identifier, GDScriptWarning::UNUSED_PRIVATE_CLASS_VARIABLE, member.variable->identifier->name);
  851. }
  852. #endif
  853. if (member.variable->property == GDScriptParser::VariableNode::PROP_SETGET) {
  854. GDScriptParser::FunctionNode *getter_function = nullptr;
  855. GDScriptParser::FunctionNode *setter_function = nullptr;
  856. bool has_valid_getter = false;
  857. bool has_valid_setter = false;
  858. if (member.variable->getter_pointer != nullptr) {
  859. if (p_class->has_function(member.variable->getter_pointer->name)) {
  860. getter_function = p_class->get_member(member.variable->getter_pointer->name).function;
  861. }
  862. if (getter_function == nullptr) {
  863. push_error(vformat(R"(Getter "%s" not found.)", member.variable->getter_pointer->name), member.variable);
  864. } else {
  865. GDScriptParser::DataType return_datatype = getter_function->datatype;
  866. if (getter_function->return_type != nullptr) {
  867. return_datatype = getter_function->return_type->datatype;
  868. return_datatype.is_meta_type = false;
  869. }
  870. if (getter_function->parameters.size() != 0 || return_datatype.has_no_type()) {
  871. push_error(vformat(R"(Function "%s" cannot be used as getter because of its signature.)", getter_function->identifier->name), member.variable);
  872. } else if (!is_type_compatible(member.variable->datatype, return_datatype, true)) {
  873. push_error(vformat(R"(Function with return type "%s" cannot be used as getter for a property of type "%s".)", return_datatype.to_string(), member.variable->datatype.to_string()), member.variable);
  874. } else {
  875. has_valid_getter = true;
  876. #ifdef DEBUG_ENABLED
  877. if (member.variable->datatype.builtin_type == Variant::INT && return_datatype.builtin_type == Variant::FLOAT) {
  878. parser->push_warning(member.variable, GDScriptWarning::NARROWING_CONVERSION);
  879. }
  880. #endif
  881. }
  882. }
  883. }
  884. if (member.variable->setter_pointer != nullptr) {
  885. if (p_class->has_function(member.variable->setter_pointer->name)) {
  886. setter_function = p_class->get_member(member.variable->setter_pointer->name).function;
  887. }
  888. if (setter_function == nullptr) {
  889. push_error(vformat(R"(Setter "%s" not found.)", member.variable->setter_pointer->name), member.variable);
  890. } else if (setter_function->parameters.size() != 1) {
  891. push_error(vformat(R"(Function "%s" cannot be used as setter because of its signature.)", setter_function->identifier->name), member.variable);
  892. } else if (!is_type_compatible(member.variable->datatype, setter_function->parameters[0]->datatype, true)) {
  893. push_error(vformat(R"(Function with argument type "%s" cannot be used as setter for a property of type "%s".)", setter_function->parameters[0]->datatype.to_string(), member.variable->datatype.to_string()), member.variable);
  894. } else {
  895. has_valid_setter = true;
  896. #ifdef DEBUG_ENABLED
  897. if (member.variable->datatype.builtin_type == Variant::FLOAT && setter_function->parameters[0]->datatype.builtin_type == Variant::INT) {
  898. parser->push_warning(member.variable, GDScriptWarning::NARROWING_CONVERSION);
  899. }
  900. #endif
  901. }
  902. }
  903. if (member.variable->datatype.is_variant() && has_valid_getter && has_valid_setter) {
  904. if (!is_type_compatible(getter_function->datatype, setter_function->parameters[0]->datatype, true)) {
  905. push_error(vformat(R"(Getter with type "%s" cannot be used along with setter of type "%s".)", getter_function->datatype.to_string(), setter_function->parameters[0]->datatype.to_string()), member.variable);
  906. }
  907. }
  908. #ifdef DEBUG_ENABLED
  909. parser->ignored_warning_codes = previously_ignored;
  910. #endif // DEBUG_ENABLED
  911. }
  912. }
  913. }
  914. }
  915. void GDScriptAnalyzer::resolve_node(GDScriptParser::Node *p_node, bool p_is_root) {
  916. ERR_FAIL_COND_MSG(p_node == nullptr, "Trying to resolve type of a null node.");
  917. switch (p_node->type) {
  918. case GDScriptParser::Node::NONE:
  919. break; // Unreachable.
  920. case GDScriptParser::Node::CLASS:
  921. resolve_class_interface(static_cast<GDScriptParser::ClassNode *>(p_node));
  922. resolve_class_body(static_cast<GDScriptParser::ClassNode *>(p_node));
  923. break;
  924. case GDScriptParser::Node::CONSTANT:
  925. resolve_constant(static_cast<GDScriptParser::ConstantNode *>(p_node));
  926. break;
  927. case GDScriptParser::Node::FOR:
  928. resolve_for(static_cast<GDScriptParser::ForNode *>(p_node));
  929. break;
  930. case GDScriptParser::Node::FUNCTION:
  931. resolve_function_signature(static_cast<GDScriptParser::FunctionNode *>(p_node));
  932. resolve_function_body(static_cast<GDScriptParser::FunctionNode *>(p_node));
  933. break;
  934. case GDScriptParser::Node::IF:
  935. resolve_if(static_cast<GDScriptParser::IfNode *>(p_node));
  936. break;
  937. case GDScriptParser::Node::SUITE:
  938. resolve_suite(static_cast<GDScriptParser::SuiteNode *>(p_node));
  939. break;
  940. case GDScriptParser::Node::VARIABLE:
  941. resolve_variable(static_cast<GDScriptParser::VariableNode *>(p_node));
  942. break;
  943. case GDScriptParser::Node::WHILE:
  944. resolve_while(static_cast<GDScriptParser::WhileNode *>(p_node));
  945. break;
  946. case GDScriptParser::Node::ANNOTATION:
  947. resolve_annotation(static_cast<GDScriptParser::AnnotationNode *>(p_node));
  948. break;
  949. case GDScriptParser::Node::ASSERT:
  950. resolve_assert(static_cast<GDScriptParser::AssertNode *>(p_node));
  951. break;
  952. case GDScriptParser::Node::MATCH:
  953. resolve_match(static_cast<GDScriptParser::MatchNode *>(p_node));
  954. break;
  955. case GDScriptParser::Node::MATCH_BRANCH:
  956. resolve_match_branch(static_cast<GDScriptParser::MatchBranchNode *>(p_node), nullptr);
  957. break;
  958. case GDScriptParser::Node::PARAMETER:
  959. resolve_parameter(static_cast<GDScriptParser::ParameterNode *>(p_node));
  960. break;
  961. case GDScriptParser::Node::PATTERN:
  962. resolve_match_pattern(static_cast<GDScriptParser::PatternNode *>(p_node), nullptr);
  963. break;
  964. case GDScriptParser::Node::RETURN:
  965. resolve_return(static_cast<GDScriptParser::ReturnNode *>(p_node));
  966. break;
  967. case GDScriptParser::Node::TYPE:
  968. resolve_datatype(static_cast<GDScriptParser::TypeNode *>(p_node));
  969. break;
  970. // Resolving expression is the same as reducing them.
  971. case GDScriptParser::Node::ARRAY:
  972. case GDScriptParser::Node::ASSIGNMENT:
  973. case GDScriptParser::Node::AWAIT:
  974. case GDScriptParser::Node::BINARY_OPERATOR:
  975. case GDScriptParser::Node::CALL:
  976. case GDScriptParser::Node::CAST:
  977. case GDScriptParser::Node::DICTIONARY:
  978. case GDScriptParser::Node::GET_NODE:
  979. case GDScriptParser::Node::IDENTIFIER:
  980. case GDScriptParser::Node::LAMBDA:
  981. case GDScriptParser::Node::LITERAL:
  982. case GDScriptParser::Node::PRELOAD:
  983. case GDScriptParser::Node::SELF:
  984. case GDScriptParser::Node::SUBSCRIPT:
  985. case GDScriptParser::Node::TERNARY_OPERATOR:
  986. case GDScriptParser::Node::UNARY_OPERATOR:
  987. reduce_expression(static_cast<GDScriptParser::ExpressionNode *>(p_node), p_is_root);
  988. break;
  989. case GDScriptParser::Node::BREAK:
  990. case GDScriptParser::Node::BREAKPOINT:
  991. case GDScriptParser::Node::CONTINUE:
  992. case GDScriptParser::Node::ENUM:
  993. case GDScriptParser::Node::PASS:
  994. case GDScriptParser::Node::SIGNAL:
  995. // Nothing to do.
  996. break;
  997. }
  998. }
  999. void GDScriptAnalyzer::resolve_annotation(GDScriptParser::AnnotationNode *p_annotation) {
  1000. // TODO: Add second validation function for annotations, so they can use checked types.
  1001. }
  1002. void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode *p_function) {
  1003. if (p_function->resolved_signature) {
  1004. return;
  1005. }
  1006. p_function->resolved_signature = true;
  1007. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  1008. parser->current_function = p_function;
  1009. #ifdef TOOLS_ENABLED
  1010. int default_value_count = 0;
  1011. #endif // TOOLS_ENABLED
  1012. for (int i = 0; i < p_function->parameters.size(); i++) {
  1013. resolve_parameter(p_function->parameters[i]);
  1014. #ifdef DEBUG_ENABLED
  1015. if (p_function->parameters[i]->usages == 0 && !String(p_function->parameters[i]->identifier->name).begins_with("_")) {
  1016. parser->push_warning(p_function->parameters[i]->identifier, GDScriptWarning::UNUSED_PARAMETER, p_function->identifier->name, p_function->parameters[i]->identifier->name);
  1017. }
  1018. is_shadowing(p_function->parameters[i]->identifier, "function parameter");
  1019. #endif // DEBUG_ENABLED
  1020. #ifdef TOOLS_ENABLED
  1021. if (p_function->parameters[i]->default_value) {
  1022. default_value_count++;
  1023. if (p_function->parameters[i]->default_value->is_constant) {
  1024. p_function->default_arg_values.push_back(p_function->parameters[i]->default_value->reduced_value);
  1025. }
  1026. }
  1027. #endif // TOOLS_ENABLED
  1028. }
  1029. if (p_function->identifier->name == GDScriptLanguage::get_singleton()->strings._init) {
  1030. // Constructor.
  1031. GDScriptParser::DataType return_type = parser->current_class->get_datatype();
  1032. return_type.is_meta_type = false;
  1033. p_function->set_datatype(return_type);
  1034. if (p_function->return_type) {
  1035. GDScriptParser::DataType declared_return = resolve_datatype(p_function->return_type);
  1036. if (declared_return.kind != GDScriptParser::DataType::BUILTIN || declared_return.builtin_type != Variant::NIL) {
  1037. push_error("Constructor cannot have an explicit return type.", p_function->return_type);
  1038. }
  1039. }
  1040. } else {
  1041. if (p_function->return_type != nullptr) {
  1042. p_function->set_datatype(resolve_datatype(p_function->return_type));
  1043. } else {
  1044. // In case the function is not typed, we can safely assume it's a Variant, so it's okay to mark as "inferred" here.
  1045. // It's not "undetected" to not mix up with unknown functions.
  1046. GDScriptParser::DataType return_type;
  1047. return_type.type_source = GDScriptParser::DataType::INFERRED;
  1048. return_type.kind = GDScriptParser::DataType::VARIANT;
  1049. p_function->set_datatype(return_type);
  1050. }
  1051. #ifdef TOOLS_ENABLED
  1052. // Check if the function signature matches the parent. If not it's an error since it breaks polymorphism.
  1053. // Not for the constructor which can vary in signature.
  1054. GDScriptParser::DataType base_type = parser->current_class->base_type;
  1055. GDScriptParser::DataType parent_return_type;
  1056. List<GDScriptParser::DataType> parameters_types;
  1057. int default_par_count = 0;
  1058. bool is_static = false;
  1059. bool is_vararg = false;
  1060. if (get_function_signature(p_function, false, base_type, p_function->identifier->name, parent_return_type, parameters_types, default_par_count, is_static, is_vararg)) {
  1061. bool valid = p_function->is_static == is_static;
  1062. valid = valid && parent_return_type == p_function->get_datatype();
  1063. int par_count_diff = p_function->parameters.size() - parameters_types.size();
  1064. valid = valid && par_count_diff >= 0;
  1065. valid = valid && default_value_count >= default_par_count + par_count_diff;
  1066. int i = 0;
  1067. for (const GDScriptParser::DataType &par_type : parameters_types) {
  1068. valid = valid && par_type == p_function->parameters[i++]->get_datatype();
  1069. }
  1070. if (!valid) {
  1071. // Compute parent signature as a string to show in the error message.
  1072. String parent_signature = parent_return_type.is_hard_type() ? parent_return_type.to_string() : "Variant";
  1073. if (parent_signature == "null") {
  1074. parent_signature = "void";
  1075. }
  1076. parent_signature += " " + p_function->identifier->name.operator String() + "(";
  1077. int j = 0;
  1078. for (const GDScriptParser::DataType &par_type : parameters_types) {
  1079. if (j > 0) {
  1080. parent_signature += ", ";
  1081. }
  1082. String parameter = par_type.to_string();
  1083. if (parameter == "null") {
  1084. parameter = "Variant";
  1085. }
  1086. parent_signature += parameter;
  1087. if (j == parameters_types.size() - default_par_count) {
  1088. parent_signature += " = default";
  1089. }
  1090. j++;
  1091. }
  1092. parent_signature += ")";
  1093. push_error(vformat(R"(The function signature doesn't match the parent. Parent signature is "%s".)", parent_signature), p_function);
  1094. }
  1095. }
  1096. #endif // TOOLS_ENABLED
  1097. }
  1098. parser->current_function = previous_function;
  1099. }
  1100. void GDScriptAnalyzer::resolve_function_body(GDScriptParser::FunctionNode *p_function) {
  1101. if (p_function->resolved_body) {
  1102. return;
  1103. }
  1104. p_function->resolved_body = true;
  1105. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  1106. parser->current_function = p_function;
  1107. resolve_suite(p_function->body);
  1108. GDScriptParser::DataType return_type = p_function->body->get_datatype();
  1109. if (!p_function->get_datatype().is_hard_type() && return_type.is_set()) {
  1110. // Use the suite inferred type if return isn't explicitly set.
  1111. return_type.type_source = GDScriptParser::DataType::INFERRED;
  1112. p_function->set_datatype(p_function->body->get_datatype());
  1113. } else if (p_function->get_datatype().is_hard_type() && (p_function->get_datatype().kind != GDScriptParser::DataType::BUILTIN || p_function->get_datatype().builtin_type != Variant::NIL)) {
  1114. if (!p_function->body->has_return && p_function->identifier->name != GDScriptLanguage::get_singleton()->strings._init) {
  1115. push_error(R"(Not all code paths return a value.)", p_function);
  1116. }
  1117. }
  1118. parser->current_function = previous_function;
  1119. }
  1120. void GDScriptAnalyzer::decide_suite_type(GDScriptParser::Node *p_suite, GDScriptParser::Node *p_statement) {
  1121. if (p_statement == nullptr) {
  1122. return;
  1123. }
  1124. switch (p_statement->type) {
  1125. case GDScriptParser::Node::IF:
  1126. case GDScriptParser::Node::FOR:
  1127. case GDScriptParser::Node::MATCH:
  1128. case GDScriptParser::Node::PATTERN:
  1129. case GDScriptParser::Node::RETURN:
  1130. case GDScriptParser::Node::WHILE:
  1131. // Use return or nested suite type as this suite type.
  1132. if (p_suite->get_datatype().is_set() && (p_suite->get_datatype() != p_statement->get_datatype())) {
  1133. // Mixed types.
  1134. // TODO: This could use the common supertype instead.
  1135. p_suite->datatype.kind = GDScriptParser::DataType::VARIANT;
  1136. p_suite->datatype.type_source = GDScriptParser::DataType::UNDETECTED;
  1137. } else {
  1138. p_suite->set_datatype(p_statement->get_datatype());
  1139. p_suite->datatype.type_source = GDScriptParser::DataType::INFERRED;
  1140. }
  1141. break;
  1142. default:
  1143. break;
  1144. }
  1145. }
  1146. void GDScriptAnalyzer::resolve_suite(GDScriptParser::SuiteNode *p_suite) {
  1147. for (int i = 0; i < p_suite->statements.size(); i++) {
  1148. GDScriptParser::Node *stmt = p_suite->statements[i];
  1149. for (GDScriptParser::AnnotationNode *&annotation : stmt->annotations) {
  1150. annotation->apply(parser, stmt);
  1151. }
  1152. #ifdef DEBUG_ENABLED
  1153. HashSet<uint32_t> previously_ignored = parser->ignored_warning_codes;
  1154. for (uint32_t ignored_warning : stmt->ignored_warnings) {
  1155. parser->ignored_warning_codes.insert(ignored_warning);
  1156. }
  1157. #endif // DEBUG_ENABLED
  1158. resolve_node(stmt);
  1159. #ifdef DEBUG_ENABLED
  1160. parser->ignored_warning_codes = previously_ignored;
  1161. #endif // DEBUG_ENABLED
  1162. decide_suite_type(p_suite, stmt);
  1163. }
  1164. }
  1165. void GDScriptAnalyzer::resolve_if(GDScriptParser::IfNode *p_if) {
  1166. reduce_expression(p_if->condition);
  1167. resolve_suite(p_if->true_block);
  1168. p_if->set_datatype(p_if->true_block->get_datatype());
  1169. if (p_if->false_block != nullptr) {
  1170. resolve_suite(p_if->false_block);
  1171. decide_suite_type(p_if, p_if->false_block);
  1172. }
  1173. }
  1174. void GDScriptAnalyzer::resolve_for(GDScriptParser::ForNode *p_for) {
  1175. bool list_resolved = false;
  1176. // Optimize constant range() call to not allocate an array.
  1177. // Use int, Vector2i, Vector3i instead, which also can be used as range iterators.
  1178. if (p_for->list && p_for->list->type == GDScriptParser::Node::CALL) {
  1179. GDScriptParser::CallNode *call = static_cast<GDScriptParser::CallNode *>(p_for->list);
  1180. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1181. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  1182. GDScriptParser::IdentifierNode *callee = static_cast<GDScriptParser::IdentifierNode *>(call->callee);
  1183. if (callee->name == "range") {
  1184. list_resolved = true;
  1185. if (call->arguments.size() < 1) {
  1186. push_error(R"*(Invalid call for "range()" function. Expected at least 1 argument, none given.)*", call->callee);
  1187. } else if (call->arguments.size() > 3) {
  1188. push_error(vformat(R"*(Invalid call for "range()" function. Expected at most 3 arguments, %d given.)*", call->arguments.size()), call->callee);
  1189. } else {
  1190. // Now we can optimize it.
  1191. bool all_is_constant = true;
  1192. Vector<Variant> args;
  1193. args.resize(call->arguments.size());
  1194. for (int i = 0; i < call->arguments.size(); i++) {
  1195. reduce_expression(call->arguments[i]);
  1196. if (!call->arguments[i]->is_constant) {
  1197. all_is_constant = false;
  1198. } else if (all_is_constant) {
  1199. args.write[i] = call->arguments[i]->reduced_value;
  1200. }
  1201. GDScriptParser::DataType arg_type = call->arguments[i]->get_datatype();
  1202. if (!arg_type.is_variant()) {
  1203. if (arg_type.kind != GDScriptParser::DataType::BUILTIN) {
  1204. all_is_constant = false;
  1205. push_error(vformat(R"*(Invalid argument for "range()" call. Argument %d should be int or float but "%s" was given.)*", i + 1, arg_type.to_string()), call->arguments[i]);
  1206. } else if (arg_type.builtin_type != Variant::INT && arg_type.builtin_type != Variant::FLOAT) {
  1207. all_is_constant = false;
  1208. push_error(vformat(R"*(Invalid argument for "range()" call. Argument %d should be int or float but "%s" was given.)*", i + 1, arg_type.to_string()), call->arguments[i]);
  1209. }
  1210. }
  1211. }
  1212. Variant reduced;
  1213. if (all_is_constant) {
  1214. switch (args.size()) {
  1215. case 1:
  1216. reduced = (int32_t)args[0];
  1217. break;
  1218. case 2:
  1219. reduced = Vector2i(args[0], args[1]);
  1220. break;
  1221. case 3:
  1222. reduced = Vector3i(args[0], args[1], args[2]);
  1223. break;
  1224. }
  1225. p_for->list->is_constant = true;
  1226. p_for->list->reduced_value = reduced;
  1227. }
  1228. }
  1229. if (p_for->list->is_constant) {
  1230. p_for->list->set_datatype(type_from_variant(p_for->list->reduced_value, p_for->list));
  1231. } else {
  1232. GDScriptParser::DataType list_type;
  1233. list_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1234. list_type.kind = GDScriptParser::DataType::BUILTIN;
  1235. list_type.builtin_type = Variant::ARRAY;
  1236. p_for->list->set_datatype(list_type);
  1237. }
  1238. }
  1239. }
  1240. }
  1241. GDScriptParser::DataType variable_type;
  1242. if (list_resolved) {
  1243. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1244. variable_type.kind = GDScriptParser::DataType::BUILTIN;
  1245. variable_type.builtin_type = Variant::INT; // Can this ever be a float or something else?
  1246. p_for->variable->set_datatype(variable_type);
  1247. } else if (p_for->list) {
  1248. resolve_node(p_for->list, false);
  1249. if (p_for->list->datatype.has_container_element_type()) {
  1250. variable_type = p_for->list->datatype.get_container_element_type();
  1251. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1252. } else if (p_for->list->datatype.is_typed_container_type()) {
  1253. variable_type = p_for->list->datatype.get_typed_container_type();
  1254. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1255. } else {
  1256. // Last resort
  1257. // TODO: Must other cases be handled? Must we mark as unsafe?
  1258. variable_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1259. variable_type.kind = GDScriptParser::DataType::VARIANT;
  1260. }
  1261. }
  1262. if (p_for->variable) {
  1263. p_for->variable->set_datatype(variable_type);
  1264. }
  1265. resolve_suite(p_for->loop);
  1266. p_for->set_datatype(p_for->loop->get_datatype());
  1267. #ifdef DEBUG_ENABLED
  1268. if (p_for->variable) {
  1269. is_shadowing(p_for->variable, R"("for" iterator variable)");
  1270. }
  1271. #endif
  1272. }
  1273. void GDScriptAnalyzer::resolve_while(GDScriptParser::WhileNode *p_while) {
  1274. resolve_node(p_while->condition, false);
  1275. resolve_suite(p_while->loop);
  1276. p_while->set_datatype(p_while->loop->get_datatype());
  1277. }
  1278. void GDScriptAnalyzer::resolve_variable(GDScriptParser::VariableNode *p_variable) {
  1279. GDScriptParser::DataType type;
  1280. type.kind = GDScriptParser::DataType::VARIANT; // By default.
  1281. GDScriptParser::DataType specified_type;
  1282. if (p_variable->datatype_specifier != nullptr) {
  1283. specified_type = resolve_datatype(p_variable->datatype_specifier);
  1284. specified_type.is_meta_type = false;
  1285. }
  1286. if (p_variable->initializer != nullptr) {
  1287. reduce_expression(p_variable->initializer);
  1288. if ((p_variable->infer_datatype || (p_variable->datatype_specifier != nullptr && specified_type.has_container_element_type())) && p_variable->initializer->type == GDScriptParser::Node::ARRAY) {
  1289. // Typed array.
  1290. GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(p_variable->initializer);
  1291. // Can only infer typed array if it has elements.
  1292. if ((p_variable->infer_datatype && array->elements.size() > 0) || p_variable->datatype_specifier != nullptr) {
  1293. update_array_literal_element_type(specified_type, array);
  1294. }
  1295. }
  1296. type = p_variable->initializer->get_datatype();
  1297. if (p_variable->infer_datatype) {
  1298. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1299. if (type.has_no_type()) {
  1300. push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value does not have a set type.)", p_variable->identifier->name), p_variable->initializer);
  1301. } else if (type.is_variant()) {
  1302. push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value is a variant. Use explicit "Variant" type if this is intended.)", p_variable->identifier->name), p_variable->initializer);
  1303. } else if (type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  1304. push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value is "null".)", p_variable->identifier->name), p_variable->initializer);
  1305. }
  1306. } else {
  1307. type.type_source = GDScriptParser::DataType::INFERRED;
  1308. }
  1309. #ifdef DEBUG_ENABLED
  1310. if (p_variable->initializer->type == GDScriptParser::Node::CALL && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  1311. parser->push_warning(p_variable->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_variable->initializer)->function_name);
  1312. }
  1313. #endif
  1314. }
  1315. if (p_variable->datatype_specifier != nullptr) {
  1316. type = specified_type;
  1317. type.is_meta_type = false;
  1318. if (p_variable->initializer != nullptr) {
  1319. if (!is_type_compatible(type, p_variable->initializer->get_datatype(), true, p_variable->initializer)) {
  1320. // Try reverse test since it can be a masked subtype.
  1321. if (!is_type_compatible(p_variable->initializer->get_datatype(), type, true, p_variable->initializer)) {
  1322. push_error(vformat(R"(Value of type "%s" cannot be assigned to a variable of type "%s".)", p_variable->initializer->get_datatype().to_string(), type.to_string()), p_variable->initializer);
  1323. } else {
  1324. // TODO: Add warning.
  1325. mark_node_unsafe(p_variable->initializer);
  1326. p_variable->use_conversion_assign = true;
  1327. }
  1328. #ifdef DEBUG_ENABLED
  1329. } else if (type.builtin_type == Variant::INT && p_variable->initializer->get_datatype().builtin_type == Variant::FLOAT) {
  1330. parser->push_warning(p_variable->initializer, GDScriptWarning::NARROWING_CONVERSION);
  1331. #endif
  1332. }
  1333. if (p_variable->initializer->get_datatype().is_variant() && !type.is_variant()) {
  1334. // TODO: Warn unsafe assign.
  1335. mark_node_unsafe(p_variable->initializer);
  1336. p_variable->use_conversion_assign = true;
  1337. }
  1338. }
  1339. } else if (p_variable->infer_datatype) {
  1340. if (type.has_no_type()) {
  1341. push_error(vformat(R"(Cannot infer the type of variable "%s" because the initial value doesn't have a set type.)", p_variable->identifier->name), p_variable->identifier);
  1342. }
  1343. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1344. }
  1345. type.is_constant = false;
  1346. p_variable->set_datatype(type);
  1347. #ifdef DEBUG_ENABLED
  1348. if (p_variable->usages == 0 && !String(p_variable->identifier->name).begins_with("_")) {
  1349. parser->push_warning(p_variable, GDScriptWarning::UNUSED_VARIABLE, p_variable->identifier->name);
  1350. } else if (p_variable->assignments == 0) {
  1351. parser->push_warning(p_variable, GDScriptWarning::UNASSIGNED_VARIABLE, p_variable->identifier->name);
  1352. }
  1353. is_shadowing(p_variable->identifier, "variable");
  1354. #endif
  1355. }
  1356. void GDScriptAnalyzer::resolve_constant(GDScriptParser::ConstantNode *p_constant) {
  1357. GDScriptParser::DataType type;
  1358. GDScriptParser::DataType explicit_type;
  1359. if (p_constant->datatype_specifier != nullptr) {
  1360. explicit_type = resolve_datatype(p_constant->datatype_specifier);
  1361. explicit_type.is_meta_type = false;
  1362. }
  1363. if (p_constant->initializer != nullptr) {
  1364. reduce_expression(p_constant->initializer);
  1365. if (p_constant->initializer->type == GDScriptParser::Node::ARRAY) {
  1366. GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(p_constant->initializer);
  1367. const_fold_array(array);
  1368. // Can only infer typed array if it has elements.
  1369. if (array->elements.size() > 0 || (p_constant->datatype_specifier != nullptr && explicit_type.has_container_element_type())) {
  1370. update_array_literal_element_type(explicit_type, array);
  1371. }
  1372. } else if (p_constant->initializer->type == GDScriptParser::Node::DICTIONARY) {
  1373. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_constant->initializer));
  1374. }
  1375. if (!p_constant->initializer->is_constant) {
  1376. push_error(vformat(R"(Assigned value for constant "%s" isn't a constant expression.)", p_constant->identifier->name), p_constant->initializer);
  1377. }
  1378. type = p_constant->initializer->get_datatype();
  1379. #ifdef DEBUG_ENABLED
  1380. if (p_constant->initializer->type == GDScriptParser::Node::CALL && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  1381. parser->push_warning(p_constant->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_constant->initializer)->function_name);
  1382. }
  1383. #endif
  1384. }
  1385. if (p_constant->datatype_specifier != nullptr) {
  1386. if (!is_type_compatible(explicit_type, type)) {
  1387. push_error(vformat(R"(Assigned value for constant "%s" has type %s which is not compatible with defined type %s.)", p_constant->identifier->name, type.to_string(), explicit_type.to_string()), p_constant->initializer);
  1388. #ifdef DEBUG_ENABLED
  1389. } else if (explicit_type.builtin_type == Variant::INT && type.builtin_type == Variant::FLOAT) {
  1390. parser->push_warning(p_constant->initializer, GDScriptWarning::NARROWING_CONVERSION);
  1391. #endif
  1392. }
  1393. type = explicit_type;
  1394. } else if (p_constant->infer_datatype) {
  1395. if (type.has_no_type()) {
  1396. push_error(vformat(R"(Cannot infer the type of constant "%s" because the initial value doesn't have a set type.)", p_constant->identifier->name), p_constant->identifier);
  1397. }
  1398. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1399. }
  1400. type.is_constant = true;
  1401. p_constant->set_datatype(type);
  1402. #ifdef DEBUG_ENABLED
  1403. if (p_constant->usages == 0) {
  1404. parser->push_warning(p_constant, GDScriptWarning::UNUSED_LOCAL_CONSTANT, p_constant->identifier->name);
  1405. }
  1406. is_shadowing(p_constant->identifier, "constant");
  1407. #endif
  1408. }
  1409. void GDScriptAnalyzer::resolve_assert(GDScriptParser::AssertNode *p_assert) {
  1410. reduce_expression(p_assert->condition);
  1411. if (p_assert->message != nullptr) {
  1412. reduce_expression(p_assert->message);
  1413. if (!p_assert->message->get_datatype().has_no_type() && (p_assert->message->get_datatype().kind != GDScriptParser::DataType::BUILTIN || p_assert->message->get_datatype().builtin_type != Variant::STRING)) {
  1414. push_error(R"(Expected string for assert error message.)", p_assert->message);
  1415. }
  1416. }
  1417. p_assert->set_datatype(p_assert->condition->get_datatype());
  1418. #ifdef DEBUG_ENABLED
  1419. if (p_assert->condition->is_constant) {
  1420. if (p_assert->condition->reduced_value.booleanize()) {
  1421. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_TRUE);
  1422. } else {
  1423. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_FALSE);
  1424. }
  1425. }
  1426. #endif
  1427. }
  1428. void GDScriptAnalyzer::resolve_match(GDScriptParser::MatchNode *p_match) {
  1429. reduce_expression(p_match->test);
  1430. for (int i = 0; i < p_match->branches.size(); i++) {
  1431. resolve_match_branch(p_match->branches[i], p_match->test);
  1432. decide_suite_type(p_match, p_match->branches[i]);
  1433. }
  1434. }
  1435. void GDScriptAnalyzer::resolve_match_branch(GDScriptParser::MatchBranchNode *p_match_branch, GDScriptParser::ExpressionNode *p_match_test) {
  1436. for (int i = 0; i < p_match_branch->patterns.size(); i++) {
  1437. resolve_match_pattern(p_match_branch->patterns[i], p_match_test);
  1438. }
  1439. resolve_suite(p_match_branch->block);
  1440. decide_suite_type(p_match_branch, p_match_branch->block);
  1441. }
  1442. void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_match_pattern, GDScriptParser::ExpressionNode *p_match_test) {
  1443. if (p_match_pattern == nullptr) {
  1444. return;
  1445. }
  1446. GDScriptParser::DataType result;
  1447. switch (p_match_pattern->pattern_type) {
  1448. case GDScriptParser::PatternNode::PT_LITERAL:
  1449. if (p_match_pattern->literal) {
  1450. reduce_literal(p_match_pattern->literal);
  1451. result = p_match_pattern->literal->get_datatype();
  1452. }
  1453. break;
  1454. case GDScriptParser::PatternNode::PT_EXPRESSION:
  1455. if (p_match_pattern->expression) {
  1456. reduce_expression(p_match_pattern->expression);
  1457. if (!p_match_pattern->expression->is_constant) {
  1458. push_error(R"(Expression in match pattern must be a constant.)", p_match_pattern->expression);
  1459. }
  1460. result = p_match_pattern->expression->get_datatype();
  1461. }
  1462. break;
  1463. case GDScriptParser::PatternNode::PT_BIND:
  1464. if (p_match_test != nullptr) {
  1465. result = p_match_test->get_datatype();
  1466. } else {
  1467. result.kind = GDScriptParser::DataType::VARIANT;
  1468. }
  1469. p_match_pattern->bind->set_datatype(result);
  1470. #ifdef DEBUG_ENABLED
  1471. is_shadowing(p_match_pattern->bind, "pattern bind");
  1472. if (p_match_pattern->bind->usages == 0 && !String(p_match_pattern->bind->name).begins_with("_")) {
  1473. parser->push_warning(p_match_pattern->bind, GDScriptWarning::UNUSED_VARIABLE, p_match_pattern->bind->name);
  1474. }
  1475. #endif
  1476. break;
  1477. case GDScriptParser::PatternNode::PT_ARRAY:
  1478. for (int i = 0; i < p_match_pattern->array.size(); i++) {
  1479. resolve_match_pattern(p_match_pattern->array[i], nullptr);
  1480. decide_suite_type(p_match_pattern, p_match_pattern->array[i]);
  1481. }
  1482. result = p_match_pattern->get_datatype();
  1483. break;
  1484. case GDScriptParser::PatternNode::PT_DICTIONARY:
  1485. for (int i = 0; i < p_match_pattern->dictionary.size(); i++) {
  1486. if (p_match_pattern->dictionary[i].key) {
  1487. reduce_expression(p_match_pattern->dictionary[i].key);
  1488. if (!p_match_pattern->dictionary[i].key->is_constant) {
  1489. push_error(R"(Expression in dictionary pattern key must be a constant.)", p_match_pattern->dictionary[i].key);
  1490. }
  1491. }
  1492. if (p_match_pattern->dictionary[i].value_pattern) {
  1493. resolve_match_pattern(p_match_pattern->dictionary[i].value_pattern, nullptr);
  1494. decide_suite_type(p_match_pattern, p_match_pattern->dictionary[i].value_pattern);
  1495. }
  1496. }
  1497. result = p_match_pattern->get_datatype();
  1498. break;
  1499. case GDScriptParser::PatternNode::PT_WILDCARD:
  1500. case GDScriptParser::PatternNode::PT_REST:
  1501. result.kind = GDScriptParser::DataType::VARIANT;
  1502. break;
  1503. }
  1504. p_match_pattern->set_datatype(result);
  1505. }
  1506. void GDScriptAnalyzer::resolve_parameter(GDScriptParser::ParameterNode *p_parameter) {
  1507. GDScriptParser::DataType result;
  1508. result.kind = GDScriptParser::DataType::VARIANT;
  1509. if (p_parameter->default_value != nullptr) {
  1510. reduce_expression(p_parameter->default_value);
  1511. result = p_parameter->default_value->get_datatype();
  1512. if (p_parameter->infer_datatype) {
  1513. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1514. } else {
  1515. result.type_source = GDScriptParser::DataType::INFERRED;
  1516. }
  1517. result.is_constant = false;
  1518. }
  1519. if (p_parameter->datatype_specifier != nullptr) {
  1520. result = resolve_datatype(p_parameter->datatype_specifier);
  1521. result.is_meta_type = false;
  1522. if (p_parameter->default_value != nullptr) {
  1523. if (!is_type_compatible(result, p_parameter->default_value->get_datatype())) {
  1524. push_error(vformat(R"(Type of default value for parameter "%s" (%s) is not compatible with parameter type (%s).)", p_parameter->identifier->name, p_parameter->default_value->get_datatype().to_string(), p_parameter->datatype_specifier->get_datatype().to_string()), p_parameter->default_value);
  1525. } else if (p_parameter->default_value->get_datatype().is_variant()) {
  1526. mark_node_unsafe(p_parameter);
  1527. }
  1528. }
  1529. }
  1530. if (result.builtin_type == Variant::Type::NIL && result.type_source == GDScriptParser::DataType::ANNOTATED_INFERRED && p_parameter->datatype_specifier == nullptr) {
  1531. push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value is "null".)", p_parameter->identifier->name), p_parameter->default_value);
  1532. }
  1533. p_parameter->set_datatype(result);
  1534. }
  1535. void GDScriptAnalyzer::resolve_return(GDScriptParser::ReturnNode *p_return) {
  1536. GDScriptParser::DataType result;
  1537. GDScriptParser::DataType expected_type;
  1538. bool has_expected_type = false;
  1539. if (parser->current_function != nullptr) {
  1540. expected_type = parser->current_function->get_datatype();
  1541. has_expected_type = true;
  1542. }
  1543. if (p_return->return_value != nullptr) {
  1544. reduce_expression(p_return->return_value);
  1545. if (p_return->return_value->type == GDScriptParser::Node::ARRAY) {
  1546. // Check if assigned value is an array literal, so we can make it a typed array too if appropriate.
  1547. if (has_expected_type && expected_type.has_container_element_type() && p_return->return_value->type == GDScriptParser::Node::ARRAY) {
  1548. update_array_literal_element_type(expected_type, static_cast<GDScriptParser::ArrayNode *>(p_return->return_value));
  1549. }
  1550. }
  1551. result = p_return->return_value->get_datatype();
  1552. } else {
  1553. // Return type is null by default.
  1554. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1555. result.kind = GDScriptParser::DataType::BUILTIN;
  1556. result.builtin_type = Variant::NIL;
  1557. result.is_constant = true;
  1558. }
  1559. if (has_expected_type) {
  1560. expected_type.is_meta_type = false;
  1561. if (expected_type.is_hard_type()) {
  1562. if (!is_type_compatible(expected_type, result)) {
  1563. // Try other way. Okay but not safe.
  1564. if (!is_type_compatible(result, expected_type)) {
  1565. push_error(vformat(R"(Cannot return value of type "%s" because the function return type is "%s".)", result.to_string(), expected_type.to_string()), p_return);
  1566. } else {
  1567. // TODO: Add warning.
  1568. mark_node_unsafe(p_return);
  1569. }
  1570. #ifdef DEBUG_ENABLED
  1571. } else if (expected_type.builtin_type == Variant::INT && result.builtin_type == Variant::FLOAT) {
  1572. parser->push_warning(p_return, GDScriptWarning::NARROWING_CONVERSION);
  1573. } else if (result.is_variant()) {
  1574. mark_node_unsafe(p_return);
  1575. #endif
  1576. }
  1577. }
  1578. }
  1579. p_return->set_datatype(result);
  1580. }
  1581. void GDScriptAnalyzer::reduce_expression(GDScriptParser::ExpressionNode *p_expression, bool p_is_root) {
  1582. // This one makes some magic happen.
  1583. if (p_expression == nullptr) {
  1584. return;
  1585. }
  1586. if (p_expression->reduced) {
  1587. // Don't do this more than once.
  1588. return;
  1589. }
  1590. p_expression->reduced = true;
  1591. switch (p_expression->type) {
  1592. case GDScriptParser::Node::ARRAY:
  1593. reduce_array(static_cast<GDScriptParser::ArrayNode *>(p_expression));
  1594. break;
  1595. case GDScriptParser::Node::ASSIGNMENT:
  1596. reduce_assignment(static_cast<GDScriptParser::AssignmentNode *>(p_expression));
  1597. break;
  1598. case GDScriptParser::Node::AWAIT:
  1599. reduce_await(static_cast<GDScriptParser::AwaitNode *>(p_expression));
  1600. break;
  1601. case GDScriptParser::Node::BINARY_OPERATOR:
  1602. reduce_binary_op(static_cast<GDScriptParser::BinaryOpNode *>(p_expression));
  1603. break;
  1604. case GDScriptParser::Node::CALL:
  1605. reduce_call(static_cast<GDScriptParser::CallNode *>(p_expression), false, p_is_root);
  1606. break;
  1607. case GDScriptParser::Node::CAST:
  1608. reduce_cast(static_cast<GDScriptParser::CastNode *>(p_expression));
  1609. break;
  1610. case GDScriptParser::Node::DICTIONARY:
  1611. reduce_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_expression));
  1612. break;
  1613. case GDScriptParser::Node::GET_NODE:
  1614. reduce_get_node(static_cast<GDScriptParser::GetNodeNode *>(p_expression));
  1615. break;
  1616. case GDScriptParser::Node::IDENTIFIER:
  1617. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_expression));
  1618. break;
  1619. case GDScriptParser::Node::LAMBDA:
  1620. reduce_lambda(static_cast<GDScriptParser::LambdaNode *>(p_expression));
  1621. break;
  1622. case GDScriptParser::Node::LITERAL:
  1623. reduce_literal(static_cast<GDScriptParser::LiteralNode *>(p_expression));
  1624. break;
  1625. case GDScriptParser::Node::PRELOAD:
  1626. reduce_preload(static_cast<GDScriptParser::PreloadNode *>(p_expression));
  1627. break;
  1628. case GDScriptParser::Node::SELF:
  1629. reduce_self(static_cast<GDScriptParser::SelfNode *>(p_expression));
  1630. break;
  1631. case GDScriptParser::Node::SUBSCRIPT:
  1632. reduce_subscript(static_cast<GDScriptParser::SubscriptNode *>(p_expression));
  1633. break;
  1634. case GDScriptParser::Node::TERNARY_OPERATOR:
  1635. reduce_ternary_op(static_cast<GDScriptParser::TernaryOpNode *>(p_expression));
  1636. break;
  1637. case GDScriptParser::Node::UNARY_OPERATOR:
  1638. reduce_unary_op(static_cast<GDScriptParser::UnaryOpNode *>(p_expression));
  1639. break;
  1640. // Non-expressions. Here only to make sure new nodes aren't forgotten.
  1641. case GDScriptParser::Node::NONE:
  1642. case GDScriptParser::Node::ANNOTATION:
  1643. case GDScriptParser::Node::ASSERT:
  1644. case GDScriptParser::Node::BREAK:
  1645. case GDScriptParser::Node::BREAKPOINT:
  1646. case GDScriptParser::Node::CLASS:
  1647. case GDScriptParser::Node::CONSTANT:
  1648. case GDScriptParser::Node::CONTINUE:
  1649. case GDScriptParser::Node::ENUM:
  1650. case GDScriptParser::Node::FOR:
  1651. case GDScriptParser::Node::FUNCTION:
  1652. case GDScriptParser::Node::IF:
  1653. case GDScriptParser::Node::MATCH:
  1654. case GDScriptParser::Node::MATCH_BRANCH:
  1655. case GDScriptParser::Node::PARAMETER:
  1656. case GDScriptParser::Node::PASS:
  1657. case GDScriptParser::Node::PATTERN:
  1658. case GDScriptParser::Node::RETURN:
  1659. case GDScriptParser::Node::SIGNAL:
  1660. case GDScriptParser::Node::SUITE:
  1661. case GDScriptParser::Node::TYPE:
  1662. case GDScriptParser::Node::VARIABLE:
  1663. case GDScriptParser::Node::WHILE:
  1664. ERR_FAIL_MSG("Reaching unreachable case");
  1665. }
  1666. }
  1667. void GDScriptAnalyzer::reduce_array(GDScriptParser::ArrayNode *p_array) {
  1668. for (int i = 0; i < p_array->elements.size(); i++) {
  1669. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  1670. reduce_expression(element);
  1671. }
  1672. // It's array in any case.
  1673. GDScriptParser::DataType arr_type;
  1674. arr_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1675. arr_type.kind = GDScriptParser::DataType::BUILTIN;
  1676. arr_type.builtin_type = Variant::ARRAY;
  1677. arr_type.is_constant = true;
  1678. p_array->set_datatype(arr_type);
  1679. }
  1680. // When an array literal is stored (or passed as function argument) to a typed context, we then assume the array is typed.
  1681. // This function determines which type is that (if any).
  1682. void GDScriptAnalyzer::update_array_literal_element_type(const GDScriptParser::DataType &p_base_type, GDScriptParser::ArrayNode *p_array_literal) {
  1683. GDScriptParser::DataType array_type = p_array_literal->get_datatype();
  1684. if (p_array_literal->elements.size() == 0) {
  1685. // Empty array literal, just make the same type as the storage.
  1686. array_type.set_container_element_type(p_base_type.get_container_element_type());
  1687. } else {
  1688. // Check if elements match.
  1689. bool all_same_type = true;
  1690. bool all_have_type = true;
  1691. GDScriptParser::DataType element_type;
  1692. for (int i = 0; i < p_array_literal->elements.size(); i++) {
  1693. if (i == 0) {
  1694. element_type = p_array_literal->elements[0]->get_datatype();
  1695. } else {
  1696. GDScriptParser::DataType this_element_type = p_array_literal->elements[i]->get_datatype();
  1697. if (this_element_type.has_no_type()) {
  1698. all_same_type = false;
  1699. all_have_type = false;
  1700. break;
  1701. } else if (element_type != this_element_type) {
  1702. if (!is_type_compatible(element_type, this_element_type, false)) {
  1703. if (is_type_compatible(this_element_type, element_type, false)) {
  1704. // This element is a super-type to the previous type, so we use the super-type.
  1705. element_type = this_element_type;
  1706. } else {
  1707. // It's incompatible.
  1708. all_same_type = false;
  1709. break;
  1710. }
  1711. }
  1712. }
  1713. }
  1714. }
  1715. if (all_same_type) {
  1716. element_type.is_constant = false;
  1717. array_type.set_container_element_type(element_type);
  1718. } else if (all_have_type) {
  1719. push_error(vformat(R"(Variant array is not compatible with an array of type "%s".)", p_base_type.get_container_element_type().to_string()), p_array_literal);
  1720. }
  1721. }
  1722. // Update the type on the value itself.
  1723. p_array_literal->set_datatype(array_type);
  1724. }
  1725. void GDScriptAnalyzer::reduce_assignment(GDScriptParser::AssignmentNode *p_assignment) {
  1726. reduce_expression(p_assignment->assignee);
  1727. reduce_expression(p_assignment->assigned_value);
  1728. if (p_assignment->assigned_value == nullptr || p_assignment->assignee == nullptr) {
  1729. return;
  1730. }
  1731. GDScriptParser::DataType assignee_type = p_assignment->assignee->get_datatype();
  1732. // Check if assigned value is an array literal, so we can make it a typed array too if appropriate.
  1733. if (assignee_type.has_container_element_type() && p_assignment->assigned_value->type == GDScriptParser::Node::ARRAY) {
  1734. update_array_literal_element_type(assignee_type, static_cast<GDScriptParser::ArrayNode *>(p_assignment->assigned_value));
  1735. }
  1736. GDScriptParser::DataType assigned_value_type = p_assignment->assigned_value->get_datatype();
  1737. if (assignee_type.is_constant) {
  1738. push_error("Cannot assign a new value to a constant.", p_assignment->assignee);
  1739. }
  1740. bool compatible = true;
  1741. GDScriptParser::DataType op_type = assigned_value_type;
  1742. if (p_assignment->operation != GDScriptParser::AssignmentNode::OP_NONE) {
  1743. op_type = get_operation_type(p_assignment->variant_op, assignee_type, assigned_value_type, compatible, p_assignment->assigned_value);
  1744. }
  1745. p_assignment->set_datatype(op_type);
  1746. if (!assignee_type.is_variant() && assigned_value_type.is_hard_type()) {
  1747. if (compatible) {
  1748. compatible = is_type_compatible(assignee_type, op_type, true, p_assignment->assigned_value);
  1749. if (!compatible) {
  1750. if (assignee_type.is_hard_type()) {
  1751. // Try reverse test since it can be a masked subtype.
  1752. if (!is_type_compatible(op_type, assignee_type, true, p_assignment->assigned_value)) {
  1753. push_error(vformat(R"(Cannot assign a value of type "%s" to a target of type "%s".)", assigned_value_type.to_string(), assignee_type.to_string()), p_assignment->assigned_value);
  1754. } else {
  1755. // TODO: Add warning.
  1756. mark_node_unsafe(p_assignment);
  1757. p_assignment->use_conversion_assign = true;
  1758. }
  1759. } else {
  1760. // TODO: Warning in this case.
  1761. mark_node_unsafe(p_assignment);
  1762. }
  1763. }
  1764. } else {
  1765. push_error(vformat(R"(Invalid operands "%s" and "%s" for assignment operator.)", assignee_type.to_string(), assigned_value_type.to_string()), p_assignment);
  1766. }
  1767. }
  1768. if (assignee_type.has_no_type() || assigned_value_type.is_variant()) {
  1769. mark_node_unsafe(p_assignment);
  1770. if (assignee_type.is_hard_type() && !assignee_type.is_variant()) {
  1771. p_assignment->use_conversion_assign = true;
  1772. }
  1773. }
  1774. if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1775. // Change source type so it's not wrongly detected later.
  1776. GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(p_assignment->assignee);
  1777. switch (identifier->source) {
  1778. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: {
  1779. GDScriptParser::DataType id_type = identifier->variable_source->get_datatype();
  1780. if (!id_type.is_hard_type()) {
  1781. id_type.kind = GDScriptParser::DataType::VARIANT;
  1782. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1783. identifier->variable_source->set_datatype(id_type);
  1784. }
  1785. } break;
  1786. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER: {
  1787. GDScriptParser::DataType id_type = identifier->parameter_source->get_datatype();
  1788. if (!id_type.is_hard_type()) {
  1789. id_type.kind = GDScriptParser::DataType::VARIANT;
  1790. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1791. identifier->parameter_source->set_datatype(id_type);
  1792. }
  1793. } break;
  1794. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE: {
  1795. GDScriptParser::DataType id_type = identifier->variable_source->get_datatype();
  1796. if (!id_type.is_hard_type()) {
  1797. id_type.kind = GDScriptParser::DataType::VARIANT;
  1798. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1799. identifier->variable_source->set_datatype(id_type);
  1800. }
  1801. } break;
  1802. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR: {
  1803. GDScriptParser::DataType id_type = identifier->bind_source->get_datatype();
  1804. if (!id_type.is_hard_type()) {
  1805. id_type.kind = GDScriptParser::DataType::VARIANT;
  1806. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1807. identifier->variable_source->set_datatype(id_type);
  1808. }
  1809. } break;
  1810. default:
  1811. // Nothing to do.
  1812. break;
  1813. }
  1814. }
  1815. #ifdef DEBUG_ENABLED
  1816. if (p_assignment->assigned_value->type == GDScriptParser::Node::CALL && assigned_value_type.kind == GDScriptParser::DataType::BUILTIN && assigned_value_type.builtin_type == Variant::NIL) {
  1817. parser->push_warning(p_assignment->assigned_value, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_assignment->assigned_value)->function_name);
  1818. } else if (assignee_type.is_hard_type() && assignee_type.builtin_type == Variant::INT && assigned_value_type.builtin_type == Variant::FLOAT) {
  1819. parser->push_warning(p_assignment->assigned_value, GDScriptWarning::NARROWING_CONVERSION);
  1820. }
  1821. #endif
  1822. }
  1823. void GDScriptAnalyzer::reduce_await(GDScriptParser::AwaitNode *p_await) {
  1824. if (p_await->to_await == nullptr) {
  1825. GDScriptParser::DataType await_type;
  1826. await_type.kind = GDScriptParser::DataType::VARIANT;
  1827. p_await->set_datatype(await_type);
  1828. return;
  1829. }
  1830. GDScriptParser::DataType awaiting_type;
  1831. if (p_await->to_await->type == GDScriptParser::Node::CALL) {
  1832. reduce_call(static_cast<GDScriptParser::CallNode *>(p_await->to_await), true);
  1833. awaiting_type = p_await->to_await->get_datatype();
  1834. } else {
  1835. reduce_expression(p_await->to_await);
  1836. }
  1837. if (p_await->to_await->is_constant) {
  1838. p_await->is_constant = p_await->to_await->is_constant;
  1839. p_await->reduced_value = p_await->to_await->reduced_value;
  1840. awaiting_type = p_await->to_await->get_datatype();
  1841. } else {
  1842. awaiting_type.kind = GDScriptParser::DataType::VARIANT;
  1843. awaiting_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1844. }
  1845. p_await->set_datatype(awaiting_type);
  1846. #ifdef DEBUG_ENABLED
  1847. awaiting_type = p_await->to_await->get_datatype();
  1848. if (!(awaiting_type.has_no_type() || awaiting_type.is_coroutine || awaiting_type.builtin_type == Variant::SIGNAL)) {
  1849. parser->push_warning(p_await, GDScriptWarning::REDUNDANT_AWAIT);
  1850. }
  1851. #endif
  1852. }
  1853. void GDScriptAnalyzer::reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op) {
  1854. reduce_expression(p_binary_op->left_operand);
  1855. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST && p_binary_op->right_operand && p_binary_op->right_operand->type == GDScriptParser::Node::IDENTIFIER) {
  1856. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_binary_op->right_operand), true);
  1857. } else {
  1858. reduce_expression(p_binary_op->right_operand);
  1859. }
  1860. // TODO: Right operand must be a valid type with the `is` operator. Need to check here.
  1861. GDScriptParser::DataType left_type;
  1862. if (p_binary_op->left_operand) {
  1863. left_type = p_binary_op->left_operand->get_datatype();
  1864. }
  1865. GDScriptParser::DataType right_type;
  1866. if (p_binary_op->right_operand) {
  1867. right_type = p_binary_op->right_operand->get_datatype();
  1868. }
  1869. if (!left_type.is_set() || !right_type.is_set()) {
  1870. return;
  1871. }
  1872. #ifdef DEBUG_ENABLED
  1873. if (p_binary_op->variant_op == Variant::OP_DIVIDE && left_type.builtin_type == Variant::INT && right_type.builtin_type == Variant::INT) {
  1874. parser->push_warning(p_binary_op, GDScriptWarning::INTEGER_DIVISION);
  1875. }
  1876. #endif
  1877. if (p_binary_op->left_operand->is_constant && p_binary_op->right_operand->is_constant) {
  1878. p_binary_op->is_constant = true;
  1879. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1880. bool valid = false;
  1881. Variant::evaluate(p_binary_op->variant_op, p_binary_op->left_operand->reduced_value, p_binary_op->right_operand->reduced_value, p_binary_op->reduced_value, valid);
  1882. if (!valid) {
  1883. if (p_binary_op->reduced_value.get_type() == Variant::STRING) {
  1884. push_error(vformat(R"(%s in operator %s.)", p_binary_op->reduced_value, Variant::get_operator_name(p_binary_op->variant_op)), p_binary_op);
  1885. } else {
  1886. push_error(vformat(R"(Invalid operands to operator %s, %s and %s.)",
  1887. Variant::get_operator_name(p_binary_op->variant_op),
  1888. Variant::get_type_name(p_binary_op->left_operand->reduced_value.get_type()),
  1889. Variant::get_type_name(p_binary_op->right_operand->reduced_value.get_type())),
  1890. p_binary_op);
  1891. }
  1892. }
  1893. } else {
  1894. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1895. GDScriptParser::DataType test_type = right_type;
  1896. test_type.is_meta_type = false;
  1897. if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) {
  1898. push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)"), p_binary_op->left_operand);
  1899. p_binary_op->reduced_value = false;
  1900. } else {
  1901. p_binary_op->reduced_value = true;
  1902. }
  1903. } else {
  1904. ERR_PRINT("Parser bug: unknown binary operation.");
  1905. }
  1906. }
  1907. p_binary_op->set_datatype(type_from_variant(p_binary_op->reduced_value, p_binary_op));
  1908. return;
  1909. }
  1910. GDScriptParser::DataType result;
  1911. if (left_type.is_variant() || right_type.is_variant()) {
  1912. // Cannot infer type because one operand can be anything.
  1913. result.kind = GDScriptParser::DataType::VARIANT;
  1914. mark_node_unsafe(p_binary_op);
  1915. } else {
  1916. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1917. bool valid = false;
  1918. result = get_operation_type(p_binary_op->variant_op, left_type, right_type, valid, p_binary_op);
  1919. if (!valid) {
  1920. push_error(vformat(R"(Invalid operands "%s" and "%s" for "%s" operator.)", left_type.to_string(), right_type.to_string(), Variant::get_operator_name(p_binary_op->variant_op)), p_binary_op);
  1921. }
  1922. } else {
  1923. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1924. GDScriptParser::DataType test_type = right_type;
  1925. test_type.is_meta_type = false;
  1926. if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) {
  1927. // Test reverse as well to consider for subtypes.
  1928. if (!is_type_compatible(p_binary_op->left_operand->get_datatype(), test_type, false)) {
  1929. if (p_binary_op->left_operand->get_datatype().is_hard_type()) {
  1930. push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)", p_binary_op->left_operand->get_datatype().to_string(), test_type.to_string()), p_binary_op->left_operand);
  1931. } else {
  1932. // TODO: Warning.
  1933. mark_node_unsafe(p_binary_op);
  1934. }
  1935. }
  1936. }
  1937. // "is" operator is always a boolean anyway.
  1938. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1939. result.kind = GDScriptParser::DataType::BUILTIN;
  1940. result.builtin_type = Variant::BOOL;
  1941. } else {
  1942. ERR_PRINT("Parser bug: unknown binary operation.");
  1943. }
  1944. }
  1945. }
  1946. p_binary_op->set_datatype(result);
  1947. }
  1948. void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_await, bool p_is_root) {
  1949. bool all_is_constant = true;
  1950. HashMap<int, GDScriptParser::ArrayNode *> arrays; // For array literal to potentially type when passing.
  1951. for (int i = 0; i < p_call->arguments.size(); i++) {
  1952. reduce_expression(p_call->arguments[i]);
  1953. if (p_call->arguments[i]->type == GDScriptParser::Node::ARRAY) {
  1954. arrays[i] = static_cast<GDScriptParser::ArrayNode *>(p_call->arguments[i]);
  1955. }
  1956. all_is_constant = all_is_constant && p_call->arguments[i]->is_constant;
  1957. }
  1958. GDScriptParser::Node::Type callee_type = p_call->get_callee_type();
  1959. GDScriptParser::DataType call_type;
  1960. if (!p_call->is_super && callee_type == GDScriptParser::Node::IDENTIFIER) {
  1961. // Call to name directly.
  1962. StringName function_name = p_call->function_name;
  1963. Variant::Type builtin_type = GDScriptParser::get_builtin_type(function_name);
  1964. if (builtin_type < Variant::VARIANT_MAX) {
  1965. // Is a builtin constructor.
  1966. call_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1967. call_type.kind = GDScriptParser::DataType::BUILTIN;
  1968. call_type.builtin_type = builtin_type;
  1969. if (builtin_type == Variant::OBJECT) {
  1970. call_type.kind = GDScriptParser::DataType::NATIVE;
  1971. call_type.native_type = function_name; // "Object".
  1972. }
  1973. bool safe_to_fold = true;
  1974. switch (builtin_type) {
  1975. // Those are stored by reference so not suited for compile-time construction.
  1976. // Because in this case they would be the same reference in all constructed values.
  1977. case Variant::OBJECT:
  1978. case Variant::DICTIONARY:
  1979. case Variant::ARRAY:
  1980. case Variant::PACKED_BYTE_ARRAY:
  1981. case Variant::PACKED_INT32_ARRAY:
  1982. case Variant::PACKED_INT64_ARRAY:
  1983. case Variant::PACKED_FLOAT32_ARRAY:
  1984. case Variant::PACKED_FLOAT64_ARRAY:
  1985. case Variant::PACKED_STRING_ARRAY:
  1986. case Variant::PACKED_VECTOR2_ARRAY:
  1987. case Variant::PACKED_VECTOR3_ARRAY:
  1988. case Variant::PACKED_COLOR_ARRAY:
  1989. safe_to_fold = false;
  1990. break;
  1991. default:
  1992. break;
  1993. }
  1994. if (all_is_constant && safe_to_fold) {
  1995. // Construct here.
  1996. Vector<const Variant *> args;
  1997. for (int i = 0; i < p_call->arguments.size(); i++) {
  1998. args.push_back(&(p_call->arguments[i]->reduced_value));
  1999. }
  2000. Callable::CallError err;
  2001. Variant value;
  2002. Variant::construct(builtin_type, value, (const Variant **)args.ptr(), args.size(), err);
  2003. switch (err.error) {
  2004. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  2005. push_error(vformat(R"(Invalid argument for %s constructor: argument %d should be %s but is %s.)", Variant::get_type_name(builtin_type), err.argument + 1,
  2006. Variant::get_type_name(Variant::Type(err.expected)), p_call->arguments[err.argument]->get_datatype().to_string()),
  2007. p_call->arguments[err.argument]);
  2008. break;
  2009. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  2010. String signature = Variant::get_type_name(builtin_type) + "(";
  2011. for (int i = 0; i < p_call->arguments.size(); i++) {
  2012. if (i > 0) {
  2013. signature += ", ";
  2014. }
  2015. signature += p_call->arguments[i]->get_datatype().to_string();
  2016. }
  2017. signature += ")";
  2018. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call->callee);
  2019. } break;
  2020. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2021. push_error(vformat(R"(Too many arguments for %s constructor. Received %d but expected %d.)", Variant::get_type_name(builtin_type), p_call->arguments.size(), err.expected), p_call);
  2022. break;
  2023. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2024. push_error(vformat(R"(Too few arguments for %s constructor. Received %d but expected %d.)", Variant::get_type_name(builtin_type), p_call->arguments.size(), err.expected), p_call);
  2025. break;
  2026. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2027. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  2028. break; // Can't happen in a builtin constructor.
  2029. case Callable::CallError::CALL_OK:
  2030. p_call->is_constant = true;
  2031. p_call->reduced_value = value;
  2032. break;
  2033. }
  2034. } else {
  2035. // TODO: Check constructors without constants.
  2036. // If there's one argument, try to use copy constructor (those aren't explicitly defined).
  2037. if (p_call->arguments.size() == 1) {
  2038. GDScriptParser::DataType arg_type = p_call->arguments[0]->get_datatype();
  2039. if (arg_type.is_variant()) {
  2040. mark_node_unsafe(p_call->arguments[0]);
  2041. } else {
  2042. if (arg_type.kind == GDScriptParser::DataType::BUILTIN && arg_type.builtin_type == builtin_type) {
  2043. // Okay.
  2044. p_call->set_datatype(call_type);
  2045. return;
  2046. }
  2047. }
  2048. }
  2049. List<MethodInfo> constructors;
  2050. Variant::get_constructor_list(builtin_type, &constructors);
  2051. bool match = false;
  2052. for (const MethodInfo &info : constructors) {
  2053. if (p_call->arguments.size() < info.arguments.size() - info.default_arguments.size()) {
  2054. continue;
  2055. }
  2056. if (p_call->arguments.size() > info.arguments.size()) {
  2057. continue;
  2058. }
  2059. bool types_match = true;
  2060. for (int i = 0; i < p_call->arguments.size(); i++) {
  2061. GDScriptParser::DataType par_type = type_from_property(info.arguments[i]);
  2062. if (!is_type_compatible(par_type, p_call->arguments[i]->get_datatype(), true)) {
  2063. types_match = false;
  2064. break;
  2065. #ifdef DEBUG_ENABLED
  2066. } else {
  2067. if (par_type.builtin_type == Variant::INT && p_call->arguments[i]->get_datatype().builtin_type == Variant::FLOAT && builtin_type != Variant::INT) {
  2068. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  2069. }
  2070. #endif
  2071. }
  2072. }
  2073. if (types_match) {
  2074. match = true;
  2075. call_type = type_from_property(info.return_val);
  2076. break;
  2077. }
  2078. }
  2079. if (!match) {
  2080. String signature = Variant::get_type_name(builtin_type) + "(";
  2081. for (int i = 0; i < p_call->arguments.size(); i++) {
  2082. if (i > 0) {
  2083. signature += ", ";
  2084. }
  2085. signature += p_call->arguments[i]->get_datatype().to_string();
  2086. }
  2087. signature += ")";
  2088. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call);
  2089. }
  2090. }
  2091. p_call->set_datatype(call_type);
  2092. return;
  2093. } else if (GDScriptUtilityFunctions::function_exists(function_name)) {
  2094. MethodInfo function_info = GDScriptUtilityFunctions::get_function_info(function_name);
  2095. if (all_is_constant && GDScriptUtilityFunctions::is_function_constant(function_name)) {
  2096. // Can call on compilation.
  2097. Vector<const Variant *> args;
  2098. for (int i = 0; i < p_call->arguments.size(); i++) {
  2099. args.push_back(&(p_call->arguments[i]->reduced_value));
  2100. }
  2101. Variant value;
  2102. Callable::CallError err;
  2103. GDScriptUtilityFunctions::get_function(function_name)(&value, (const Variant **)args.ptr(), args.size(), err);
  2104. switch (err.error) {
  2105. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2106. PropertyInfo wrong_arg = function_info.arguments[err.argument];
  2107. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1,
  2108. type_from_property(wrong_arg).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()),
  2109. p_call->arguments[err.argument]);
  2110. } break;
  2111. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  2112. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  2113. break;
  2114. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2115. push_error(vformat(R"*(Too many arguments for "%s()" call. Expected at most %d but received %d.)*", function_name, err.expected, p_call->arguments.size()), p_call);
  2116. break;
  2117. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2118. push_error(vformat(R"*(Too few arguments for "%s()" call. Expected at least %d but received %d.)*", function_name, err.expected, p_call->arguments.size()), p_call);
  2119. break;
  2120. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  2121. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2122. break; // Can't happen in a builtin constructor.
  2123. case Callable::CallError::CALL_OK:
  2124. p_call->is_constant = true;
  2125. p_call->reduced_value = value;
  2126. break;
  2127. }
  2128. } else {
  2129. validate_call_arg(function_info, p_call);
  2130. }
  2131. p_call->set_datatype(type_from_property(function_info.return_val));
  2132. return;
  2133. } else if (Variant::has_utility_function(function_name)) {
  2134. MethodInfo function_info = info_from_utility_func(function_name);
  2135. if (all_is_constant && Variant::get_utility_function_type(function_name) == Variant::UTILITY_FUNC_TYPE_MATH) {
  2136. // Can call on compilation.
  2137. Vector<const Variant *> args;
  2138. for (int i = 0; i < p_call->arguments.size(); i++) {
  2139. args.push_back(&(p_call->arguments[i]->reduced_value));
  2140. }
  2141. Variant value;
  2142. Callable::CallError err;
  2143. Variant::call_utility_function(function_name, &value, (const Variant **)args.ptr(), args.size(), err);
  2144. switch (err.error) {
  2145. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2146. String expected_type_name;
  2147. if (err.argument < function_info.arguments.size()) {
  2148. expected_type_name = type_from_property(function_info.arguments[err.argument]).to_string();
  2149. } else {
  2150. expected_type_name = Variant::get_type_name((Variant::Type)err.expected);
  2151. }
  2152. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1,
  2153. expected_type_name, p_call->arguments[err.argument]->get_datatype().to_string()),
  2154. p_call->arguments[err.argument]);
  2155. } break;
  2156. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  2157. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  2158. break;
  2159. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2160. push_error(vformat(R"*(Too many arguments for "%s()" call. Expected at most %d but received %d.)*", function_name, err.expected, p_call->arguments.size()), p_call);
  2161. break;
  2162. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2163. push_error(vformat(R"*(Too few arguments for "%s()" call. Expected at least %d but received %d.)*", function_name, err.expected, p_call->arguments.size()), p_call);
  2164. break;
  2165. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  2166. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2167. break; // Can't happen in a builtin constructor.
  2168. case Callable::CallError::CALL_OK:
  2169. p_call->is_constant = true;
  2170. p_call->reduced_value = value;
  2171. break;
  2172. }
  2173. } else {
  2174. validate_call_arg(function_info, p_call);
  2175. }
  2176. p_call->set_datatype(type_from_property(function_info.return_val));
  2177. return;
  2178. }
  2179. }
  2180. GDScriptParser::DataType base_type;
  2181. call_type.kind = GDScriptParser::DataType::VARIANT;
  2182. bool is_self = false;
  2183. if (p_call->is_super) {
  2184. base_type = parser->current_class->base_type;
  2185. base_type.is_meta_type = false;
  2186. is_self = true;
  2187. if (p_call->callee == nullptr && !lambda_stack.is_empty()) {
  2188. push_error("Cannot use `super()` inside a lambda.", p_call);
  2189. }
  2190. } else if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  2191. base_type = parser->current_class->get_datatype();
  2192. base_type.is_meta_type = false;
  2193. is_self = true;
  2194. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  2195. GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee);
  2196. if (subscript->base == nullptr) {
  2197. // Invalid syntax, error already set on parser.
  2198. p_call->set_datatype(call_type);
  2199. mark_node_unsafe(p_call);
  2200. return;
  2201. }
  2202. if (!subscript->is_attribute) {
  2203. // Invalid call. Error already sent in parser.
  2204. // TODO: Could check if Callable here.
  2205. p_call->set_datatype(call_type);
  2206. mark_node_unsafe(p_call);
  2207. return;
  2208. }
  2209. if (subscript->attribute == nullptr) {
  2210. // Invalid call. Error already sent in parser.
  2211. p_call->set_datatype(call_type);
  2212. mark_node_unsafe(p_call);
  2213. return;
  2214. }
  2215. GDScriptParser::IdentifierNode *base_id = nullptr;
  2216. if (subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2217. base_id = static_cast<GDScriptParser::IdentifierNode *>(subscript->base);
  2218. }
  2219. if (base_id && GDScriptParser::get_builtin_type(base_id->name) < Variant::VARIANT_MAX) {
  2220. base_type = make_builtin_meta_type(GDScriptParser::get_builtin_type(base_id->name));
  2221. } else {
  2222. reduce_expression(subscript->base);
  2223. base_type = subscript->base->get_datatype();
  2224. }
  2225. } else {
  2226. // Invalid call. Error already sent in parser.
  2227. // TODO: Could check if Callable here too.
  2228. p_call->set_datatype(call_type);
  2229. mark_node_unsafe(p_call);
  2230. return;
  2231. }
  2232. bool is_static = false;
  2233. bool is_vararg = false;
  2234. int default_arg_count = 0;
  2235. GDScriptParser::DataType return_type;
  2236. List<GDScriptParser::DataType> par_types;
  2237. bool is_constructor = (base_type.is_meta_type || (p_call->callee && p_call->callee->type == GDScriptParser::Node::IDENTIFIER)) && p_call->function_name == SNAME("new");
  2238. if (get_function_signature(p_call, is_constructor, base_type, p_call->function_name, return_type, par_types, default_arg_count, is_static, is_vararg)) {
  2239. // If the function require typed arrays we must make literals be typed.
  2240. for (const KeyValue<int, GDScriptParser::ArrayNode *> &E : arrays) {
  2241. int index = E.key;
  2242. if (index < par_types.size() && par_types[index].has_container_element_type()) {
  2243. update_array_literal_element_type(par_types[index], E.value);
  2244. }
  2245. }
  2246. validate_call_arg(par_types, default_arg_count, is_vararg, p_call);
  2247. if (base_type.kind == GDScriptParser::DataType::ENUM && base_type.is_meta_type) {
  2248. // Enum type is treated as a dictionary value for function calls.
  2249. base_type.is_meta_type = false;
  2250. }
  2251. if (is_self && parser->current_function != nullptr && parser->current_function->is_static && !is_static) {
  2252. // Get the parent function above any lambda.
  2253. GDScriptParser::FunctionNode *parent_function = parser->current_function;
  2254. while (parent_function->source_lambda) {
  2255. parent_function = parent_function->source_lambda->parent_function;
  2256. }
  2257. push_error(vformat(R"*(Cannot call non-static function "%s()" from static function "%s()".)*", p_call->function_name, parent_function->identifier->name), p_call);
  2258. } else if (!is_self && base_type.is_meta_type && !is_static) {
  2259. base_type.is_meta_type = false; // For `to_string()`.
  2260. push_error(vformat(R"*(Cannot call non-static function "%s()" on the class "%s" directly. Make an instance instead.)*", p_call->function_name, base_type.to_string()), p_call);
  2261. } else if (is_self && !is_static) {
  2262. mark_lambda_use_self();
  2263. }
  2264. #ifdef DEBUG_ENABLED
  2265. if (p_is_root && return_type.kind != GDScriptParser::DataType::UNRESOLVED && return_type.builtin_type != Variant::NIL) {
  2266. parser->push_warning(p_call, GDScriptWarning::RETURN_VALUE_DISCARDED, p_call->function_name);
  2267. }
  2268. if (is_static && !base_type.is_meta_type && !(callee_type != GDScriptParser::Node::SUBSCRIPT && parser->current_function != nullptr && parser->current_function->is_static)) {
  2269. parser->push_warning(p_call, GDScriptWarning::STATIC_CALLED_ON_INSTANCE, p_call->function_name, base_type.to_string());
  2270. }
  2271. #endif // DEBUG_ENABLED
  2272. call_type = return_type;
  2273. } else {
  2274. bool found = false;
  2275. // Check if the name exists as something else.
  2276. if (!p_call->is_super && callee_type != GDScriptParser::Node::NONE) {
  2277. GDScriptParser::IdentifierNode *callee_id;
  2278. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  2279. callee_id = static_cast<GDScriptParser::IdentifierNode *>(p_call->callee);
  2280. } else {
  2281. // Can only be attribute.
  2282. callee_id = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee)->attribute;
  2283. }
  2284. if (callee_id) {
  2285. reduce_identifier_from_base(callee_id, &base_type);
  2286. GDScriptParser::DataType callee_datatype = callee_id->get_datatype();
  2287. if (callee_datatype.is_set() && !callee_datatype.is_variant()) {
  2288. found = true;
  2289. if (callee_datatype.builtin_type == Variant::CALLABLE) {
  2290. push_error(vformat(R"*(Name "%s" is a Callable. You can call it with "%s.call()" instead.)*", p_call->function_name, p_call->function_name), p_call->callee);
  2291. } else {
  2292. push_error(vformat(R"*(Name "%s" called as a function but is a "%s".)*", p_call->function_name, callee_datatype.to_string()), p_call->callee);
  2293. }
  2294. #ifdef DEBUG_ENABLED
  2295. } else if (!is_self && !(base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN)) {
  2296. parser->push_warning(p_call, GDScriptWarning::UNSAFE_METHOD_ACCESS, p_call->function_name, base_type.to_string());
  2297. mark_node_unsafe(p_call);
  2298. #endif
  2299. }
  2300. }
  2301. }
  2302. if (!found && (is_self || (base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN))) {
  2303. String base_name = is_self && !p_call->is_super ? "self" : base_type.to_string();
  2304. push_error(vformat(R"*(Function "%s()" not found in base %s.)*", p_call->function_name, base_name), p_call->is_super ? p_call : p_call->callee);
  2305. } else if (!found && (!p_call->is_super && base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::NATIVE && base_type.is_meta_type)) {
  2306. push_error(vformat(R"*(Static function "%s()" not found in base "%s".)*", p_call->function_name, base_type.native_type.operator String()), p_call);
  2307. }
  2308. }
  2309. if (call_type.is_coroutine && !p_is_await && !p_is_root) {
  2310. push_error(vformat(R"*(Function "%s()" is a coroutine, so it must be called with "await".)*", p_call->function_name), p_call);
  2311. }
  2312. p_call->set_datatype(call_type);
  2313. }
  2314. void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) {
  2315. reduce_expression(p_cast->operand);
  2316. GDScriptParser::DataType cast_type = resolve_datatype(p_cast->cast_type);
  2317. if (!cast_type.is_set()) {
  2318. mark_node_unsafe(p_cast);
  2319. return;
  2320. }
  2321. cast_type = type_from_metatype(cast_type); // The casted value won't be a type name.
  2322. p_cast->set_datatype(cast_type);
  2323. if (!cast_type.is_variant()) {
  2324. GDScriptParser::DataType op_type = p_cast->operand->get_datatype();
  2325. if (!op_type.is_variant()) {
  2326. bool valid = false;
  2327. if (op_type.kind == GDScriptParser::DataType::ENUM && cast_type.kind == GDScriptParser::DataType::ENUM) {
  2328. // Enum types are compatible between each other, so it's a safe cast.
  2329. valid = true;
  2330. } else if (op_type.kind == GDScriptParser::DataType::BUILTIN && op_type.builtin_type == Variant::INT && cast_type.kind == GDScriptParser::DataType::ENUM) {
  2331. // Convertint int to enum is always valid.
  2332. valid = true;
  2333. } else if (op_type.kind == GDScriptParser::DataType::BUILTIN && cast_type.kind == GDScriptParser::DataType::BUILTIN) {
  2334. valid = Variant::can_convert(op_type.builtin_type, cast_type.builtin_type);
  2335. } else if (op_type.kind != GDScriptParser::DataType::BUILTIN && cast_type.kind != GDScriptParser::DataType::BUILTIN) {
  2336. valid = is_type_compatible(cast_type, op_type) || is_type_compatible(op_type, cast_type);
  2337. }
  2338. if (!valid) {
  2339. push_error(vformat(R"(Invalid cast. Cannot convert from "%s" to "%s".)", op_type.to_string(), cast_type.to_string()), p_cast->cast_type);
  2340. }
  2341. }
  2342. } else {
  2343. mark_node_unsafe(p_cast);
  2344. }
  2345. #ifdef DEBUG_ENABLED
  2346. if (p_cast->operand->get_datatype().is_variant()) {
  2347. parser->push_warning(p_cast, GDScriptWarning::UNSAFE_CAST, cast_type.to_string());
  2348. mark_node_unsafe(p_cast);
  2349. }
  2350. #endif
  2351. // TODO: Perform cast on constants.
  2352. }
  2353. void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  2354. HashMap<Variant, GDScriptParser::ExpressionNode *, VariantHasher, VariantComparator> elements;
  2355. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  2356. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  2357. if (p_dictionary->style == GDScriptParser::DictionaryNode::PYTHON_DICT) {
  2358. reduce_expression(element.key);
  2359. }
  2360. reduce_expression(element.value);
  2361. if (element.key->is_constant) {
  2362. if (elements.has(element.key->reduced_value)) {
  2363. push_error(vformat(R"(Key "%s" was already used in this dictionary (at line %d).)", element.key->reduced_value, elements[element.key->reduced_value]->start_line), element.key);
  2364. } else {
  2365. elements[element.key->reduced_value] = element.value;
  2366. }
  2367. }
  2368. }
  2369. // It's dictionary in any case.
  2370. GDScriptParser::DataType dict_type;
  2371. dict_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2372. dict_type.kind = GDScriptParser::DataType::BUILTIN;
  2373. dict_type.builtin_type = Variant::DICTIONARY;
  2374. dict_type.is_constant = true;
  2375. p_dictionary->set_datatype(dict_type);
  2376. }
  2377. void GDScriptAnalyzer::reduce_get_node(GDScriptParser::GetNodeNode *p_get_node) {
  2378. GDScriptParser::DataType result;
  2379. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2380. result.kind = GDScriptParser::DataType::NATIVE;
  2381. result.native_type = SNAME("Node");
  2382. result.builtin_type = Variant::OBJECT;
  2383. if (!ClassDB::is_parent_class(parser->current_class->base_type.native_type, result.native_type)) {
  2384. push_error(R"*(Cannot use shorthand "get_node()" notation ("$") on a class that isn't a node.)*", p_get_node);
  2385. }
  2386. mark_lambda_use_self();
  2387. p_get_node->set_datatype(result);
  2388. }
  2389. GDScriptParser::DataType GDScriptAnalyzer::make_global_class_meta_type(const StringName &p_class_name, const GDScriptParser::Node *p_source) {
  2390. GDScriptParser::DataType type;
  2391. String path = ScriptServer::get_global_class_path(p_class_name);
  2392. String ext = path.get_extension();
  2393. if (ext == GDScriptLanguage::get_singleton()->get_extension()) {
  2394. Ref<GDScriptParserRef> ref = get_parser_for(path);
  2395. if (ref.is_null()) {
  2396. push_error(vformat(R"(Could not find script for class "%s".)", p_class_name), p_source);
  2397. type.type_source = GDScriptParser::DataType::UNDETECTED;
  2398. type.kind = GDScriptParser::DataType::VARIANT;
  2399. return type;
  2400. }
  2401. Error err = ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  2402. if (err) {
  2403. push_error(vformat(R"(Could not resolve class "%s", because of a parser error.)", p_class_name), p_source);
  2404. type.type_source = GDScriptParser::DataType::UNDETECTED;
  2405. type.kind = GDScriptParser::DataType::VARIANT;
  2406. return type;
  2407. }
  2408. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2409. type.kind = GDScriptParser::DataType::CLASS;
  2410. type.builtin_type = Variant::OBJECT;
  2411. type.native_type = ScriptServer::get_global_class_native_base(p_class_name);
  2412. type.class_type = ref->get_parser()->head;
  2413. type.script_path = ref->get_parser()->script_path;
  2414. type.is_constant = true;
  2415. type.is_meta_type = true;
  2416. return type;
  2417. } else {
  2418. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2419. type.kind = GDScriptParser::DataType::SCRIPT;
  2420. type.builtin_type = Variant::OBJECT;
  2421. type.native_type = ScriptServer::get_global_class_native_base(p_class_name);
  2422. type.script_type = ResourceLoader::load(path, "Script");
  2423. type.script_path = path;
  2424. type.is_constant = true;
  2425. type.is_meta_type = true;
  2426. return type;
  2427. }
  2428. }
  2429. void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base) {
  2430. GDScriptParser::DataType base;
  2431. if (p_base == nullptr) {
  2432. base = type_from_metatype(parser->current_class->get_datatype());
  2433. } else {
  2434. base = *p_base;
  2435. }
  2436. const StringName &name = p_identifier->name;
  2437. if (base.kind == GDScriptParser::DataType::ENUM) {
  2438. if (base.is_meta_type) {
  2439. if (base.enum_values.has(name)) {
  2440. p_identifier->is_constant = true;
  2441. p_identifier->reduced_value = base.enum_values[name];
  2442. GDScriptParser::DataType result;
  2443. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2444. result.kind = GDScriptParser::DataType::ENUM;
  2445. result.is_constant = true;
  2446. result.builtin_type = Variant::INT;
  2447. result.native_type = base.native_type;
  2448. result.enum_type = base.enum_type;
  2449. result.enum_values = base.enum_values;
  2450. p_identifier->set_datatype(result);
  2451. return;
  2452. } else {
  2453. // Consider as a Dictionary, so it can be anything.
  2454. // This will be evaluated in the next if block.
  2455. base.kind = GDScriptParser::DataType::BUILTIN;
  2456. base.builtin_type = Variant::DICTIONARY;
  2457. base.is_meta_type = false;
  2458. }
  2459. } else {
  2460. push_error(R"(Cannot get property from enum value.)", p_identifier);
  2461. return;
  2462. }
  2463. }
  2464. if (base.kind == GDScriptParser::DataType::BUILTIN) {
  2465. if (base.is_meta_type) {
  2466. bool valid = true;
  2467. Variant result = Variant::get_constant_value(base.builtin_type, name, &valid);
  2468. if (valid) {
  2469. p_identifier->is_constant = true;
  2470. p_identifier->reduced_value = result;
  2471. p_identifier->set_datatype(type_from_variant(result, p_identifier));
  2472. } else if (base.is_hard_type()) {
  2473. push_error(vformat(R"(Cannot find constant "%s" on type "%s".)", name, base.to_string()), p_identifier);
  2474. }
  2475. } else {
  2476. switch (base.builtin_type) {
  2477. case Variant::NIL: {
  2478. if (base.is_hard_type()) {
  2479. push_error(vformat(R"(Invalid get index "%s" on base Nil)", name), p_identifier);
  2480. }
  2481. return;
  2482. }
  2483. case Variant::DICTIONARY: {
  2484. GDScriptParser::DataType dummy;
  2485. dummy.kind = GDScriptParser::DataType::VARIANT;
  2486. p_identifier->set_datatype(dummy);
  2487. return;
  2488. }
  2489. default: {
  2490. Callable::CallError temp;
  2491. Variant dummy;
  2492. Variant::construct(base.builtin_type, dummy, nullptr, 0, temp);
  2493. List<PropertyInfo> properties;
  2494. dummy.get_property_list(&properties);
  2495. for (const PropertyInfo &prop : properties) {
  2496. if (prop.name == name) {
  2497. p_identifier->set_datatype(type_from_property(prop));
  2498. return;
  2499. }
  2500. }
  2501. if (base.is_hard_type()) {
  2502. push_error(vformat(R"(Cannot find property "%s" on base "%s".)", name, base.to_string()), p_identifier);
  2503. }
  2504. }
  2505. }
  2506. }
  2507. return;
  2508. }
  2509. GDScriptParser::ClassNode *base_class = base.class_type;
  2510. // TODO: Switch current class/function/suite here to avoid misrepresenting identifiers (in recursive reduce calls).
  2511. while (base_class != nullptr) {
  2512. if (base_class->identifier && base_class->identifier->name == name) {
  2513. p_identifier->set_datatype(base_class->get_datatype());
  2514. return;
  2515. }
  2516. if (base_class->has_member(name)) {
  2517. const GDScriptParser::ClassNode::Member &member = base_class->get_member(name);
  2518. p_identifier->set_datatype(member.get_datatype());
  2519. switch (member.type) {
  2520. case GDScriptParser::ClassNode::Member::CONSTANT:
  2521. // For out-of-order resolution:
  2522. reduce_expression(member.constant->initializer);
  2523. p_identifier->is_constant = true;
  2524. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  2525. p_identifier->set_datatype(member.constant->initializer->get_datatype());
  2526. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2527. p_identifier->constant_source = member.constant;
  2528. break;
  2529. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  2530. p_identifier->is_constant = true;
  2531. p_identifier->reduced_value = member.enum_value.value;
  2532. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2533. break;
  2534. case GDScriptParser::ClassNode::Member::VARIABLE:
  2535. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_VARIABLE;
  2536. p_identifier->variable_source = member.variable;
  2537. member.variable->usages += 1;
  2538. break;
  2539. case GDScriptParser::ClassNode::Member::SIGNAL:
  2540. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_SIGNAL;
  2541. break;
  2542. case GDScriptParser::ClassNode::Member::FUNCTION:
  2543. resolve_function_signature(member.function);
  2544. p_identifier->set_datatype(make_callable_type(member.function->info));
  2545. break;
  2546. case GDScriptParser::ClassNode::Member::CLASS:
  2547. // For out-of-order resolution:
  2548. resolve_class_interface(member.m_class);
  2549. p_identifier->set_datatype(member.m_class->get_datatype());
  2550. break;
  2551. default:
  2552. break; // Type already set.
  2553. }
  2554. return;
  2555. }
  2556. // Check outer constants.
  2557. // TODO: Allow outer static functions.
  2558. GDScriptParser::ClassNode *outer = base_class->outer;
  2559. while (outer != nullptr) {
  2560. if (outer->has_member(name)) {
  2561. const GDScriptParser::ClassNode::Member &member = outer->get_member(name);
  2562. switch (member.type) {
  2563. case GDScriptParser::ClassNode::Member::CONSTANT: {
  2564. // TODO: Make sure loops won't cause problem. And make special error message for those.
  2565. // For out-of-order resolution:
  2566. reduce_expression(member.constant->initializer);
  2567. p_identifier->set_datatype(member.get_datatype());
  2568. p_identifier->is_constant = true;
  2569. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  2570. return;
  2571. } break;
  2572. case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
  2573. p_identifier->set_datatype(member.get_datatype());
  2574. p_identifier->is_constant = true;
  2575. p_identifier->reduced_value = member.enum_value.value;
  2576. return;
  2577. } break;
  2578. case GDScriptParser::ClassNode::Member::ENUM: {
  2579. p_identifier->set_datatype(member.get_datatype());
  2580. p_identifier->is_constant = false;
  2581. return;
  2582. } break;
  2583. case GDScriptParser::ClassNode::Member::CLASS: {
  2584. resolve_class_interface(member.m_class);
  2585. p_identifier->set_datatype(member.m_class->get_datatype());
  2586. return;
  2587. } break;
  2588. default:
  2589. break;
  2590. }
  2591. }
  2592. outer = outer->outer;
  2593. }
  2594. base_class = base_class->base_type.class_type;
  2595. }
  2596. // Check native members. No need for native class recursion because Node exposes all Object's properties.
  2597. const StringName &native = base.native_type;
  2598. if (class_exists(native)) {
  2599. MethodInfo method_info;
  2600. if (ClassDB::has_property(native, name)) {
  2601. StringName getter_name = ClassDB::get_property_getter(native, name);
  2602. MethodBind *getter = ClassDB::get_method(native, getter_name);
  2603. if (getter != nullptr) {
  2604. p_identifier->set_datatype(type_from_property(getter->get_return_info()));
  2605. p_identifier->source = GDScriptParser::IdentifierNode::INHERITED_VARIABLE;
  2606. }
  2607. return;
  2608. }
  2609. if (ClassDB::get_method_info(native, name, &method_info)) {
  2610. // Method is callable.
  2611. p_identifier->set_datatype(make_callable_type(method_info));
  2612. p_identifier->source = GDScriptParser::IdentifierNode::INHERITED_VARIABLE;
  2613. return;
  2614. }
  2615. if (ClassDB::get_signal(native, name, &method_info)) {
  2616. // Signal is a type too.
  2617. p_identifier->set_datatype(make_signal_type(method_info));
  2618. p_identifier->source = GDScriptParser::IdentifierNode::INHERITED_VARIABLE;
  2619. return;
  2620. }
  2621. if (ClassDB::has_enum(native, name)) {
  2622. p_identifier->set_datatype(make_native_enum_type(native, name));
  2623. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2624. return;
  2625. }
  2626. bool valid = false;
  2627. int64_t int_constant = ClassDB::get_integer_constant(native, name, &valid);
  2628. if (valid) {
  2629. p_identifier->is_constant = true;
  2630. p_identifier->reduced_value = int_constant;
  2631. p_identifier->set_datatype(type_from_variant(int_constant, p_identifier));
  2632. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2633. return;
  2634. }
  2635. }
  2636. }
  2637. void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin) {
  2638. // TODO: This is opportunity to further infer types.
  2639. // Check if we are inside and enum. This allows enum values to access other elements of the same enum.
  2640. if (current_enum) {
  2641. for (int i = 0; i < current_enum->values.size(); i++) {
  2642. const GDScriptParser::EnumNode::Value &element = current_enum->values[i];
  2643. if (element.identifier->name == p_identifier->name) {
  2644. GDScriptParser::DataType type;
  2645. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2646. type.kind = element.parent_enum->identifier ? GDScriptParser::DataType::ENUM : GDScriptParser::DataType::BUILTIN;
  2647. type.builtin_type = Variant::INT;
  2648. type.is_constant = true;
  2649. if (element.parent_enum->identifier) {
  2650. type.enum_type = element.parent_enum->identifier->name;
  2651. }
  2652. p_identifier->set_datatype(type);
  2653. if (element.resolved) {
  2654. p_identifier->is_constant = true;
  2655. p_identifier->reduced_value = element.value;
  2656. } else {
  2657. push_error(R"(Cannot use another enum element before it was declared.)", p_identifier);
  2658. }
  2659. return; // Found anyway.
  2660. }
  2661. }
  2662. }
  2663. bool found_source = false;
  2664. // Check if identifier is local.
  2665. // If that's the case, the declaration already was solved before.
  2666. switch (p_identifier->source) {
  2667. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER:
  2668. p_identifier->set_datatype(p_identifier->parameter_source->get_datatype());
  2669. found_source = true;
  2670. break;
  2671. case GDScriptParser::IdentifierNode::LOCAL_CONSTANT:
  2672. case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
  2673. p_identifier->set_datatype(p_identifier->constant_source->get_datatype());
  2674. p_identifier->is_constant = true;
  2675. // TODO: Constant should have a value on the node itself.
  2676. p_identifier->reduced_value = p_identifier->constant_source->initializer->reduced_value;
  2677. found_source = true;
  2678. break;
  2679. case GDScriptParser::IdentifierNode::MEMBER_SIGNAL:
  2680. case GDScriptParser::IdentifierNode::INHERITED_VARIABLE:
  2681. mark_lambda_use_self();
  2682. break;
  2683. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
  2684. mark_lambda_use_self();
  2685. p_identifier->variable_source->usages++;
  2686. [[fallthrough]];
  2687. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE:
  2688. p_identifier->set_datatype(p_identifier->variable_source->get_datatype());
  2689. found_source = true;
  2690. break;
  2691. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR:
  2692. p_identifier->set_datatype(p_identifier->bind_source->get_datatype());
  2693. found_source = true;
  2694. break;
  2695. case GDScriptParser::IdentifierNode::LOCAL_BIND: {
  2696. GDScriptParser::DataType result = p_identifier->bind_source->get_datatype();
  2697. result.is_constant = true;
  2698. p_identifier->set_datatype(result);
  2699. found_source = true;
  2700. } break;
  2701. case GDScriptParser::IdentifierNode::UNDEFINED_SOURCE:
  2702. break;
  2703. }
  2704. // Not a local, so check members.
  2705. if (!found_source) {
  2706. reduce_identifier_from_base(p_identifier);
  2707. if (p_identifier->source != GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->get_datatype().is_set()) {
  2708. // Found.
  2709. found_source = true;
  2710. }
  2711. }
  2712. if (found_source) {
  2713. if ((p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_VARIABLE || p_identifier->source == GDScriptParser::IdentifierNode::INHERITED_VARIABLE) && parser->current_function && parser->current_function->is_static) {
  2714. // Get the parent function above any lambda.
  2715. GDScriptParser::FunctionNode *parent_function = parser->current_function;
  2716. while (parent_function->source_lambda) {
  2717. parent_function = parent_function->source_lambda->parent_function;
  2718. }
  2719. push_error(vformat(R"*(Cannot access instance variable "%s" from the static function "%s()".)*", p_identifier->name, parent_function->identifier->name), p_identifier);
  2720. }
  2721. if (!lambda_stack.is_empty()) {
  2722. // If the identifier is a member variable (including the native class properties), we consider the lambda to be using `self`, so we keep a reference to the current instance.
  2723. if (p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_VARIABLE || p_identifier->source == GDScriptParser::IdentifierNode::INHERITED_VARIABLE) {
  2724. mark_lambda_use_self();
  2725. return; // No need to capture.
  2726. }
  2727. // If the identifier is local, check if it's any kind of capture by comparing their source function.
  2728. // Only capture locals and enum values. Constants are still accessible from the lambda using the script reference. If not, this method is done.
  2729. if (p_identifier->source == GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_CONSTANT) {
  2730. return;
  2731. }
  2732. GDScriptParser::FunctionNode *function_test = lambda_stack.back()->get()->function;
  2733. // Make sure we aren't capturing variable in the same lambda.
  2734. // This also add captures for nested lambdas.
  2735. while (function_test != nullptr && function_test != p_identifier->source_function && function_test->source_lambda != nullptr && !function_test->source_lambda->captures_indices.has(p_identifier->name)) {
  2736. function_test->source_lambda->captures_indices[p_identifier->name] = function_test->source_lambda->captures.size();
  2737. function_test->source_lambda->captures.push_back(p_identifier);
  2738. function_test = function_test->source_lambda->parent_function;
  2739. }
  2740. }
  2741. return;
  2742. }
  2743. StringName name = p_identifier->name;
  2744. p_identifier->source = GDScriptParser::IdentifierNode::UNDEFINED_SOURCE;
  2745. // Check globals. We make an exception for Variant::OBJECT because it's the base class for
  2746. // non-builtin types so we allow doing e.g. Object.new()
  2747. Variant::Type builtin_type = GDScriptParser::get_builtin_type(name);
  2748. if (builtin_type != Variant::OBJECT && builtin_type < Variant::VARIANT_MAX) {
  2749. if (can_be_builtin) {
  2750. p_identifier->set_datatype(make_builtin_meta_type(builtin_type));
  2751. return;
  2752. } else {
  2753. push_error(R"(Builtin type cannot be used as a name on its own.)", p_identifier);
  2754. }
  2755. }
  2756. if (class_exists(name)) {
  2757. p_identifier->set_datatype(make_native_meta_type(name));
  2758. return;
  2759. }
  2760. if (ScriptServer::is_global_class(name)) {
  2761. p_identifier->set_datatype(make_global_class_meta_type(name, p_identifier));
  2762. return;
  2763. }
  2764. // Try singletons.
  2765. // Do this before globals because this might be a singleton loading another one before it's compiled.
  2766. if (ProjectSettings::get_singleton()->has_autoload(name)) {
  2767. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(name);
  2768. if (autoload.is_singleton) {
  2769. // Singleton exists, so it's at least a Node.
  2770. GDScriptParser::DataType result;
  2771. result.kind = GDScriptParser::DataType::NATIVE;
  2772. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2773. if (ResourceLoader::get_resource_type(autoload.path) == "GDScript") {
  2774. Ref<GDScriptParserRef> singl_parser = get_parser_for(autoload.path);
  2775. if (singl_parser.is_valid()) {
  2776. Error err = singl_parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  2777. if (err == OK) {
  2778. result = type_from_metatype(singl_parser->get_parser()->head->get_datatype());
  2779. }
  2780. }
  2781. }
  2782. result.is_constant = true;
  2783. p_identifier->set_datatype(result);
  2784. return;
  2785. }
  2786. }
  2787. if (GDScriptLanguage::get_singleton()->get_global_map().has(name)) {
  2788. int idx = GDScriptLanguage::get_singleton()->get_global_map()[name];
  2789. Variant constant = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  2790. p_identifier->set_datatype(type_from_variant(constant, p_identifier));
  2791. p_identifier->is_constant = true;
  2792. p_identifier->reduced_value = constant;
  2793. return;
  2794. }
  2795. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(name)) {
  2796. Variant constant = GDScriptLanguage::get_singleton()->get_named_globals_map()[name];
  2797. p_identifier->set_datatype(type_from_variant(constant, p_identifier));
  2798. p_identifier->is_constant = true;
  2799. p_identifier->reduced_value = constant;
  2800. return;
  2801. }
  2802. // Not found.
  2803. // Check if it's a builtin function.
  2804. if (GDScriptUtilityFunctions::function_exists(name)) {
  2805. push_error(vformat(R"(Built-in function "%s" cannot be used as an identifier.)", name), p_identifier);
  2806. } else {
  2807. push_error(vformat(R"(Identifier "%s" not declared in the current scope.)", name), p_identifier);
  2808. }
  2809. GDScriptParser::DataType dummy;
  2810. dummy.kind = GDScriptParser::DataType::VARIANT;
  2811. p_identifier->set_datatype(dummy); // Just so type is set to something.
  2812. }
  2813. void GDScriptAnalyzer::reduce_lambda(GDScriptParser::LambdaNode *p_lambda) {
  2814. // Lambda is always a Callable.
  2815. GDScriptParser::DataType lambda_type;
  2816. lambda_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  2817. lambda_type.kind = GDScriptParser::DataType::BUILTIN;
  2818. lambda_type.builtin_type = Variant::CALLABLE;
  2819. p_lambda->set_datatype(lambda_type);
  2820. if (p_lambda->function == nullptr) {
  2821. return;
  2822. }
  2823. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  2824. parser->current_function = p_lambda->function;
  2825. lambda_stack.push_back(p_lambda);
  2826. for (int i = 0; i < p_lambda->function->parameters.size(); i++) {
  2827. resolve_parameter(p_lambda->function->parameters[i]);
  2828. }
  2829. resolve_suite(p_lambda->function->body);
  2830. int captures_amount = p_lambda->captures.size();
  2831. if (captures_amount > 0) {
  2832. // Create space for lambda parameters.
  2833. // At the beginning to not mess with optional parameters.
  2834. int param_count = p_lambda->function->parameters.size();
  2835. p_lambda->function->parameters.resize(param_count + captures_amount);
  2836. for (int i = param_count - 1; i >= 0; i--) {
  2837. p_lambda->function->parameters.write[i + captures_amount] = p_lambda->function->parameters[i];
  2838. p_lambda->function->parameters_indices[p_lambda->function->parameters[i]->identifier->name] = i + captures_amount;
  2839. }
  2840. // Add captures as extra parameters at the beginning.
  2841. for (int i = 0; i < p_lambda->captures.size(); i++) {
  2842. GDScriptParser::IdentifierNode *capture = p_lambda->captures[i];
  2843. GDScriptParser::ParameterNode *capture_param = parser->alloc_node<GDScriptParser::ParameterNode>();
  2844. capture_param->identifier = capture;
  2845. capture_param->usages = capture->usages;
  2846. capture_param->set_datatype(capture->get_datatype());
  2847. p_lambda->function->parameters.write[i] = capture_param;
  2848. p_lambda->function->parameters_indices[capture->name] = i;
  2849. }
  2850. }
  2851. lambda_stack.pop_back();
  2852. parser->current_function = previous_function;
  2853. }
  2854. void GDScriptAnalyzer::reduce_literal(GDScriptParser::LiteralNode *p_literal) {
  2855. p_literal->reduced_value = p_literal->value;
  2856. p_literal->is_constant = true;
  2857. p_literal->set_datatype(type_from_variant(p_literal->reduced_value, p_literal));
  2858. }
  2859. void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) {
  2860. if (!p_preload->path) {
  2861. return;
  2862. }
  2863. reduce_expression(p_preload->path);
  2864. if (!p_preload->path->is_constant) {
  2865. push_error("Preloaded path must be a constant string.", p_preload->path);
  2866. return;
  2867. }
  2868. if (p_preload->path->reduced_value.get_type() != Variant::STRING) {
  2869. push_error("Preloaded path must be a constant string.", p_preload->path);
  2870. } else {
  2871. p_preload->resolved_path = p_preload->path->reduced_value;
  2872. // TODO: Save this as script dependency.
  2873. if (p_preload->resolved_path.is_relative_path()) {
  2874. p_preload->resolved_path = parser->script_path.get_base_dir().path_join(p_preload->resolved_path);
  2875. }
  2876. p_preload->resolved_path = p_preload->resolved_path.simplify_path();
  2877. if (!ResourceLoader::exists(p_preload->resolved_path)) {
  2878. Ref<FileAccess> file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  2879. if (file_check->file_exists(p_preload->resolved_path)) {
  2880. push_error(vformat(R"(Preload file "%s" has no resource loaders (unrecognized file extension).)", p_preload->resolved_path), p_preload->path);
  2881. } else {
  2882. push_error(vformat(R"(Preload file "%s" does not exist.)", p_preload->resolved_path), p_preload->path);
  2883. }
  2884. } else {
  2885. // TODO: Don't load if validating: use completion cache.
  2886. // Must load GDScript and PackedScenes separately to permit cyclic references
  2887. // as ResourceLoader::load() detect and reject those.
  2888. if (ResourceLoader::get_resource_type(p_preload->resolved_path) == "GDScript") {
  2889. Error err = OK;
  2890. Ref<GDScript> res = GDScriptCache::get_shallow_script(p_preload->resolved_path, err, parser->script_path);
  2891. p_preload->resource = res;
  2892. if (err != OK) {
  2893. push_error(vformat(R"(Could not preload resource script "%s".)", p_preload->resolved_path), p_preload->path);
  2894. }
  2895. } else if (ResourceLoader::get_resource_type(p_preload->resolved_path) == "PackedScene") {
  2896. Error err = OK;
  2897. Ref<PackedScene> res = GDScriptCache::get_packed_scene(p_preload->resolved_path, err, parser->script_path);
  2898. p_preload->resource = res;
  2899. if (err != OK) {
  2900. push_error(vformat(R"(Could not preload resource scene "%s".)", p_preload->resolved_path), p_preload->path);
  2901. }
  2902. } else {
  2903. p_preload->resource = ResourceLoader::load(p_preload->resolved_path);
  2904. if (p_preload->resource.is_null()) {
  2905. push_error(vformat(R"(Could not preload resource file "%s".)", p_preload->resolved_path), p_preload->path);
  2906. }
  2907. }
  2908. }
  2909. }
  2910. p_preload->is_constant = true;
  2911. p_preload->reduced_value = p_preload->resource;
  2912. p_preload->set_datatype(type_from_variant(p_preload->reduced_value, p_preload));
  2913. }
  2914. void GDScriptAnalyzer::reduce_self(GDScriptParser::SelfNode *p_self) {
  2915. p_self->is_constant = false;
  2916. p_self->set_datatype(type_from_metatype(parser->current_class->get_datatype()));
  2917. mark_lambda_use_self();
  2918. }
  2919. void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscript) {
  2920. if (p_subscript->base == nullptr) {
  2921. return;
  2922. }
  2923. if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2924. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base), true);
  2925. } else {
  2926. reduce_expression(p_subscript->base);
  2927. if (p_subscript->base->type == GDScriptParser::Node::ARRAY) {
  2928. const_fold_array(static_cast<GDScriptParser::ArrayNode *>(p_subscript->base));
  2929. } else if (p_subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  2930. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_subscript->base));
  2931. }
  2932. }
  2933. GDScriptParser::DataType result_type;
  2934. if (p_subscript->is_attribute) {
  2935. if (p_subscript->attribute == nullptr) {
  2936. return;
  2937. }
  2938. if (p_subscript->base->is_constant) {
  2939. // Just try to get it.
  2940. bool valid = false;
  2941. Variant value = p_subscript->base->reduced_value.get_named(p_subscript->attribute->name, valid);
  2942. // If it's a GDScript instance, try to get the full script. Maybe it's not still completely loaded.
  2943. Ref<GDScript> gdscr = Ref<GDScript>(p_subscript->base->reduced_value);
  2944. if (!valid && gdscr.is_valid()) {
  2945. Error err = OK;
  2946. GDScriptCache::get_full_script(gdscr->get_path(), err);
  2947. if (err == OK) {
  2948. value = p_subscript->base->reduced_value.get_named(p_subscript->attribute->name, valid);
  2949. }
  2950. }
  2951. if (!valid) {
  2952. push_error(vformat(R"(Cannot get member "%s" from "%s".)", p_subscript->attribute->name, p_subscript->base->reduced_value), p_subscript->index);
  2953. result_type.kind = GDScriptParser::DataType::VARIANT;
  2954. } else {
  2955. p_subscript->is_constant = true;
  2956. p_subscript->reduced_value = value;
  2957. result_type = type_from_variant(value, p_subscript);
  2958. }
  2959. } else {
  2960. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  2961. if (base_type.is_variant() || !base_type.is_hard_type()) {
  2962. result_type.kind = GDScriptParser::DataType::VARIANT;
  2963. mark_node_unsafe(p_subscript);
  2964. } else {
  2965. reduce_identifier_from_base(p_subscript->attribute, &base_type);
  2966. GDScriptParser::DataType attr_type = p_subscript->attribute->get_datatype();
  2967. if (attr_type.is_set()) {
  2968. result_type = attr_type;
  2969. p_subscript->is_constant = p_subscript->attribute->is_constant;
  2970. p_subscript->reduced_value = p_subscript->attribute->reduced_value;
  2971. } else {
  2972. if (base_type.kind == GDScriptParser::DataType::BUILTIN) {
  2973. push_error(vformat(R"(Cannot find member "%s" in base "%s".)", p_subscript->attribute->name, base_type.to_string()), p_subscript->attribute);
  2974. #ifdef DEBUG_ENABLED
  2975. } else {
  2976. parser->push_warning(p_subscript, GDScriptWarning::UNSAFE_PROPERTY_ACCESS, p_subscript->attribute->name, base_type.to_string());
  2977. #endif
  2978. }
  2979. result_type.kind = GDScriptParser::DataType::VARIANT;
  2980. }
  2981. }
  2982. }
  2983. } else {
  2984. if (p_subscript->index == nullptr) {
  2985. return;
  2986. }
  2987. reduce_expression(p_subscript->index);
  2988. if (p_subscript->base->is_constant && p_subscript->index->is_constant) {
  2989. // Just try to get it.
  2990. bool valid = false;
  2991. Variant value = p_subscript->base->reduced_value.get(p_subscript->index->reduced_value, &valid);
  2992. if (!valid) {
  2993. push_error(vformat(R"(Cannot get index "%s" from "%s".)", p_subscript->index->reduced_value, p_subscript->base->reduced_value), p_subscript->index);
  2994. } else {
  2995. p_subscript->is_constant = true;
  2996. p_subscript->reduced_value = value;
  2997. result_type = type_from_variant(value, p_subscript);
  2998. }
  2999. result_type.kind = GDScriptParser::DataType::VARIANT;
  3000. } else {
  3001. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  3002. GDScriptParser::DataType index_type = p_subscript->index->get_datatype();
  3003. if (base_type.is_variant()) {
  3004. result_type.kind = GDScriptParser::DataType::VARIANT;
  3005. mark_node_unsafe(p_subscript);
  3006. } else {
  3007. if (base_type.kind == GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  3008. // Check if indexing is valid.
  3009. bool error = index_type.kind != GDScriptParser::DataType::BUILTIN && base_type.builtin_type != Variant::DICTIONARY;
  3010. if (!error) {
  3011. switch (base_type.builtin_type) {
  3012. // Expect int or real as index.
  3013. case Variant::PACKED_BYTE_ARRAY:
  3014. case Variant::PACKED_COLOR_ARRAY:
  3015. case Variant::PACKED_FLOAT32_ARRAY:
  3016. case Variant::PACKED_FLOAT64_ARRAY:
  3017. case Variant::PACKED_INT32_ARRAY:
  3018. case Variant::PACKED_INT64_ARRAY:
  3019. case Variant::PACKED_STRING_ARRAY:
  3020. case Variant::PACKED_VECTOR2_ARRAY:
  3021. case Variant::PACKED_VECTOR3_ARRAY:
  3022. case Variant::ARRAY:
  3023. case Variant::STRING:
  3024. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT;
  3025. break;
  3026. // Expect String only.
  3027. case Variant::RECT2:
  3028. case Variant::RECT2I:
  3029. case Variant::PLANE:
  3030. case Variant::QUATERNION:
  3031. case Variant::AABB:
  3032. case Variant::OBJECT:
  3033. error = index_type.builtin_type != Variant::STRING;
  3034. break;
  3035. // Expect String or number.
  3036. case Variant::BASIS:
  3037. case Variant::VECTOR2:
  3038. case Variant::VECTOR2I:
  3039. case Variant::VECTOR3:
  3040. case Variant::VECTOR3I:
  3041. case Variant::VECTOR4:
  3042. case Variant::VECTOR4I:
  3043. case Variant::TRANSFORM2D:
  3044. case Variant::TRANSFORM3D:
  3045. case Variant::PROJECTION:
  3046. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT &&
  3047. index_type.builtin_type != Variant::STRING;
  3048. break;
  3049. // Expect String or int.
  3050. case Variant::COLOR:
  3051. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING;
  3052. break;
  3053. // Don't support indexing, but we will check it later.
  3054. case Variant::RID:
  3055. case Variant::BOOL:
  3056. case Variant::CALLABLE:
  3057. case Variant::FLOAT:
  3058. case Variant::INT:
  3059. case Variant::NIL:
  3060. case Variant::NODE_PATH:
  3061. case Variant::SIGNAL:
  3062. case Variant::STRING_NAME:
  3063. break;
  3064. // Here for completeness.
  3065. case Variant::DICTIONARY:
  3066. case Variant::VARIANT_MAX:
  3067. break;
  3068. }
  3069. if (error) {
  3070. push_error(vformat(R"(Invalid index type "%s" for a base of type "%s".)", index_type.to_string(), base_type.to_string()), p_subscript->index);
  3071. }
  3072. }
  3073. } else if (base_type.kind != GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  3074. if (index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME) {
  3075. push_error(vformat(R"(Only String or StringName can be used as index for type "%s", but received a "%s".)", base_type.to_string(), index_type.to_string()), p_subscript->index);
  3076. }
  3077. }
  3078. // Check resulting type if possible.
  3079. result_type.builtin_type = Variant::NIL;
  3080. result_type.kind = GDScriptParser::DataType::BUILTIN;
  3081. result_type.type_source = base_type.is_hard_type() ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED;
  3082. if (base_type.kind != GDScriptParser::DataType::BUILTIN) {
  3083. base_type.builtin_type = Variant::OBJECT;
  3084. }
  3085. switch (base_type.builtin_type) {
  3086. // Can't index at all.
  3087. case Variant::RID:
  3088. case Variant::BOOL:
  3089. case Variant::CALLABLE:
  3090. case Variant::FLOAT:
  3091. case Variant::INT:
  3092. case Variant::NIL:
  3093. case Variant::NODE_PATH:
  3094. case Variant::SIGNAL:
  3095. case Variant::STRING_NAME:
  3096. result_type.kind = GDScriptParser::DataType::VARIANT;
  3097. push_error(vformat(R"(Cannot use subscript operator on a base of type "%s".)", base_type.to_string()), p_subscript->base);
  3098. break;
  3099. // Return int.
  3100. case Variant::PACKED_BYTE_ARRAY:
  3101. case Variant::PACKED_INT32_ARRAY:
  3102. case Variant::PACKED_INT64_ARRAY:
  3103. case Variant::VECTOR2I:
  3104. case Variant::VECTOR3I:
  3105. case Variant::VECTOR4I:
  3106. result_type.builtin_type = Variant::INT;
  3107. break;
  3108. // Return float.
  3109. case Variant::PACKED_FLOAT32_ARRAY:
  3110. case Variant::PACKED_FLOAT64_ARRAY:
  3111. case Variant::VECTOR2:
  3112. case Variant::VECTOR3:
  3113. case Variant::VECTOR4:
  3114. case Variant::QUATERNION:
  3115. result_type.builtin_type = Variant::FLOAT;
  3116. break;
  3117. // Return Color.
  3118. case Variant::PACKED_COLOR_ARRAY:
  3119. result_type.builtin_type = Variant::COLOR;
  3120. break;
  3121. // Return String.
  3122. case Variant::PACKED_STRING_ARRAY:
  3123. case Variant::STRING:
  3124. result_type.builtin_type = Variant::STRING;
  3125. break;
  3126. // Return Vector2.
  3127. case Variant::PACKED_VECTOR2_ARRAY:
  3128. case Variant::TRANSFORM2D:
  3129. case Variant::RECT2:
  3130. result_type.builtin_type = Variant::VECTOR2;
  3131. break;
  3132. // Return Vector2I.
  3133. case Variant::RECT2I:
  3134. result_type.builtin_type = Variant::VECTOR2I;
  3135. break;
  3136. // Return Vector3.
  3137. case Variant::PACKED_VECTOR3_ARRAY:
  3138. case Variant::AABB:
  3139. case Variant::BASIS:
  3140. result_type.builtin_type = Variant::VECTOR3;
  3141. break;
  3142. // Depends on the index.
  3143. case Variant::TRANSFORM3D:
  3144. case Variant::PROJECTION:
  3145. case Variant::PLANE:
  3146. case Variant::COLOR:
  3147. case Variant::DICTIONARY:
  3148. case Variant::OBJECT:
  3149. result_type.kind = GDScriptParser::DataType::VARIANT;
  3150. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  3151. break;
  3152. // Can have an element type.
  3153. case Variant::ARRAY:
  3154. if (base_type.has_container_element_type()) {
  3155. result_type = base_type.get_container_element_type();
  3156. result_type.type_source = base_type.type_source;
  3157. } else {
  3158. result_type.kind = GDScriptParser::DataType::VARIANT;
  3159. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  3160. }
  3161. break;
  3162. // Here for completeness.
  3163. case Variant::VARIANT_MAX:
  3164. break;
  3165. }
  3166. }
  3167. }
  3168. }
  3169. p_subscript->set_datatype(result_type);
  3170. }
  3171. void GDScriptAnalyzer::reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op) {
  3172. reduce_expression(p_ternary_op->condition);
  3173. reduce_expression(p_ternary_op->true_expr);
  3174. reduce_expression(p_ternary_op->false_expr);
  3175. GDScriptParser::DataType result;
  3176. if (p_ternary_op->condition && p_ternary_op->condition->is_constant && p_ternary_op->true_expr->is_constant && p_ternary_op->false_expr && p_ternary_op->false_expr->is_constant) {
  3177. p_ternary_op->is_constant = true;
  3178. if (p_ternary_op->condition->reduced_value.booleanize()) {
  3179. p_ternary_op->reduced_value = p_ternary_op->true_expr->reduced_value;
  3180. } else {
  3181. p_ternary_op->reduced_value = p_ternary_op->false_expr->reduced_value;
  3182. }
  3183. }
  3184. GDScriptParser::DataType true_type;
  3185. if (p_ternary_op->true_expr) {
  3186. true_type = p_ternary_op->true_expr->get_datatype();
  3187. } else {
  3188. true_type.kind = GDScriptParser::DataType::VARIANT;
  3189. }
  3190. GDScriptParser::DataType false_type;
  3191. if (p_ternary_op->false_expr) {
  3192. false_type = p_ternary_op->false_expr->get_datatype();
  3193. } else {
  3194. false_type.kind = GDScriptParser::DataType::VARIANT;
  3195. }
  3196. if (true_type.is_variant() || false_type.is_variant()) {
  3197. result.kind = GDScriptParser::DataType::VARIANT;
  3198. } else {
  3199. result = true_type;
  3200. if (!is_type_compatible(true_type, false_type)) {
  3201. result = false_type;
  3202. if (!is_type_compatible(false_type, true_type)) {
  3203. result.type_source = GDScriptParser::DataType::UNDETECTED;
  3204. result.kind = GDScriptParser::DataType::VARIANT;
  3205. #ifdef DEBUG_ENABLED
  3206. parser->push_warning(p_ternary_op, GDScriptWarning::INCOMPATIBLE_TERNARY);
  3207. #endif
  3208. }
  3209. }
  3210. }
  3211. p_ternary_op->set_datatype(result);
  3212. }
  3213. void GDScriptAnalyzer::reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op) {
  3214. reduce_expression(p_unary_op->operand);
  3215. GDScriptParser::DataType result;
  3216. if (p_unary_op->operand == nullptr) {
  3217. result.kind = GDScriptParser::DataType::VARIANT;
  3218. p_unary_op->set_datatype(result);
  3219. return;
  3220. }
  3221. if (p_unary_op->operand->is_constant) {
  3222. p_unary_op->is_constant = true;
  3223. p_unary_op->reduced_value = Variant::evaluate(p_unary_op->variant_op, p_unary_op->operand->reduced_value, Variant());
  3224. result = type_from_variant(p_unary_op->reduced_value, p_unary_op);
  3225. } else if (p_unary_op->operand->get_datatype().is_variant()) {
  3226. result.kind = GDScriptParser::DataType::VARIANT;
  3227. mark_node_unsafe(p_unary_op);
  3228. } else {
  3229. bool valid = false;
  3230. result = get_operation_type(p_unary_op->variant_op, p_unary_op->operand->get_datatype(), valid, p_unary_op);
  3231. if (!valid) {
  3232. push_error(vformat(R"(Invalid operand of type "%s" for unary operator "%s".)", p_unary_op->operand->get_datatype().to_string(), Variant::get_operator_name(p_unary_op->variant_op)), p_unary_op->operand);
  3233. }
  3234. }
  3235. p_unary_op->set_datatype(result);
  3236. }
  3237. void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array) {
  3238. bool all_is_constant = true;
  3239. for (int i = 0; i < p_array->elements.size(); i++) {
  3240. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  3241. if (element->type == GDScriptParser::Node::ARRAY) {
  3242. const_fold_array(static_cast<GDScriptParser::ArrayNode *>(element));
  3243. } else if (element->type == GDScriptParser::Node::DICTIONARY) {
  3244. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element));
  3245. }
  3246. all_is_constant = all_is_constant && element->is_constant;
  3247. if (!all_is_constant) {
  3248. return;
  3249. }
  3250. }
  3251. Array array;
  3252. array.resize(p_array->elements.size());
  3253. for (int i = 0; i < p_array->elements.size(); i++) {
  3254. array[i] = p_array->elements[i]->reduced_value;
  3255. }
  3256. p_array->is_constant = true;
  3257. p_array->reduced_value = array;
  3258. }
  3259. void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  3260. bool all_is_constant = true;
  3261. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  3262. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  3263. if (element.value->type == GDScriptParser::Node::ARRAY) {
  3264. const_fold_array(static_cast<GDScriptParser::ArrayNode *>(element.value));
  3265. } else if (element.value->type == GDScriptParser::Node::DICTIONARY) {
  3266. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element.value));
  3267. }
  3268. all_is_constant = all_is_constant && element.key->is_constant && element.value->is_constant;
  3269. if (!all_is_constant) {
  3270. return;
  3271. }
  3272. }
  3273. Dictionary dict;
  3274. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  3275. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  3276. dict[element.key->reduced_value] = element.value->reduced_value;
  3277. }
  3278. p_dictionary->is_constant = true;
  3279. p_dictionary->reduced_value = dict;
  3280. }
  3281. GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source) {
  3282. GDScriptParser::DataType result;
  3283. result.is_constant = true;
  3284. result.kind = GDScriptParser::DataType::BUILTIN;
  3285. result.builtin_type = p_value.get_type();
  3286. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; // Constant has explicit type.
  3287. if (p_value.get_type() == Variant::OBJECT) {
  3288. Object *obj = p_value;
  3289. if (!obj) {
  3290. return GDScriptParser::DataType();
  3291. }
  3292. result.native_type = obj->get_class_name();
  3293. Ref<Script> scr = p_value; // Check if value is a script itself.
  3294. if (scr.is_valid()) {
  3295. result.is_meta_type = true;
  3296. } else {
  3297. result.is_meta_type = false;
  3298. scr = obj->get_script();
  3299. }
  3300. if (scr.is_valid()) {
  3301. result.script_type = scr;
  3302. result.script_path = scr->get_path();
  3303. Ref<GDScript> gds = scr;
  3304. if (gds.is_valid()) {
  3305. result.kind = GDScriptParser::DataType::CLASS;
  3306. // This might be an inner class, so we want to get the parser for the root.
  3307. // But still get the inner class from that tree.
  3308. GDScript *current = gds.ptr();
  3309. List<StringName> class_chain;
  3310. while (current->_owner) {
  3311. // Push to front so it's in reverse.
  3312. class_chain.push_front(current->name);
  3313. current = current->_owner;
  3314. }
  3315. Ref<GDScriptParserRef> ref = get_parser_for(current->get_path());
  3316. if (ref.is_null()) {
  3317. push_error("Could not find script in path.", p_source);
  3318. GDScriptParser::DataType error_type;
  3319. error_type.kind = GDScriptParser::DataType::VARIANT;
  3320. return error_type;
  3321. }
  3322. ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  3323. GDScriptParser::ClassNode *found = ref->get_parser()->head;
  3324. // It should be okay to assume this exists, since we have a complete script already.
  3325. for (const StringName &E : class_chain) {
  3326. found = found->get_member(E).m_class;
  3327. }
  3328. result.class_type = found;
  3329. result.script_path = ref->get_parser()->script_path;
  3330. } else {
  3331. result.kind = GDScriptParser::DataType::SCRIPT;
  3332. }
  3333. result.native_type = scr->get_instance_base_type();
  3334. } else {
  3335. result.kind = GDScriptParser::DataType::NATIVE;
  3336. if (result.native_type == GDScriptNativeClass::get_class_static()) {
  3337. result.is_meta_type = true;
  3338. }
  3339. }
  3340. }
  3341. return result;
  3342. }
  3343. GDScriptParser::DataType GDScriptAnalyzer::type_from_metatype(const GDScriptParser::DataType &p_meta_type) const {
  3344. GDScriptParser::DataType result = p_meta_type;
  3345. result.is_meta_type = false;
  3346. result.is_constant = false;
  3347. if (p_meta_type.kind == GDScriptParser::DataType::ENUM) {
  3348. result.builtin_type = Variant::INT;
  3349. }
  3350. return result;
  3351. }
  3352. GDScriptParser::DataType GDScriptAnalyzer::type_from_property(const PropertyInfo &p_property) const {
  3353. GDScriptParser::DataType result;
  3354. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3355. if (p_property.type == Variant::NIL && (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  3356. // Variant
  3357. result.kind = GDScriptParser::DataType::VARIANT;
  3358. return result;
  3359. }
  3360. result.builtin_type = p_property.type;
  3361. if (p_property.type == Variant::OBJECT) {
  3362. result.kind = GDScriptParser::DataType::NATIVE;
  3363. result.native_type = p_property.class_name == StringName() ? SNAME("Object") : p_property.class_name;
  3364. } else {
  3365. result.kind = GDScriptParser::DataType::BUILTIN;
  3366. result.builtin_type = p_property.type;
  3367. if (p_property.type == Variant::ARRAY && p_property.hint == PROPERTY_HINT_ARRAY_TYPE) {
  3368. // Check element type.
  3369. StringName elem_type_name = p_property.hint_string;
  3370. GDScriptParser::DataType elem_type;
  3371. elem_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3372. Variant::Type elem_builtin_type = GDScriptParser::get_builtin_type(elem_type_name);
  3373. if (elem_builtin_type < Variant::VARIANT_MAX) {
  3374. // Builtin type.
  3375. elem_type.kind = GDScriptParser::DataType::BUILTIN;
  3376. elem_type.builtin_type = elem_builtin_type;
  3377. } else if (class_exists(elem_type_name)) {
  3378. elem_type.kind = GDScriptParser::DataType::NATIVE;
  3379. elem_type.builtin_type = Variant::OBJECT;
  3380. elem_type.native_type = p_property.hint_string;
  3381. } else if (ScriptServer::is_global_class(elem_type_name)) {
  3382. // Just load this as it shouldn't be a GDScript.
  3383. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(elem_type_name));
  3384. elem_type.kind = GDScriptParser::DataType::SCRIPT;
  3385. elem_type.builtin_type = Variant::OBJECT;
  3386. elem_type.native_type = script->get_instance_base_type();
  3387. elem_type.script_type = script;
  3388. } else {
  3389. ERR_FAIL_V_MSG(result, "Could not find element type from property hint of a typed array.");
  3390. }
  3391. elem_type.is_constant = false;
  3392. result.set_container_element_type(elem_type);
  3393. }
  3394. }
  3395. return result;
  3396. }
  3397. bool GDScriptAnalyzer::get_function_signature(GDScriptParser::Node *p_source, bool p_is_constructor, GDScriptParser::DataType p_base_type, const StringName &p_function, GDScriptParser::DataType &r_return_type, List<GDScriptParser::DataType> &r_par_types, int &r_default_arg_count, bool &r_static, bool &r_vararg) {
  3398. r_static = false;
  3399. r_vararg = false;
  3400. r_default_arg_count = 0;
  3401. StringName function_name = p_function;
  3402. if (p_base_type.kind == GDScriptParser::DataType::ENUM) {
  3403. if (p_base_type.is_meta_type) {
  3404. // Enum type can be treated as a dictionary value.
  3405. p_base_type.kind = GDScriptParser::DataType::BUILTIN;
  3406. p_base_type.builtin_type = Variant::DICTIONARY;
  3407. p_base_type.is_meta_type = false;
  3408. } else {
  3409. push_error("Cannot call function on enum value.", p_source);
  3410. return false;
  3411. }
  3412. }
  3413. if (p_base_type.kind == GDScriptParser::DataType::BUILTIN) {
  3414. // Construct a base type to get methods.
  3415. Callable::CallError err;
  3416. Variant dummy;
  3417. Variant::construct(p_base_type.builtin_type, dummy, nullptr, 0, err);
  3418. if (err.error != Callable::CallError::CALL_OK) {
  3419. ERR_FAIL_V_MSG(false, "Could not construct base Variant type.");
  3420. }
  3421. List<MethodInfo> methods;
  3422. dummy.get_method_list(&methods);
  3423. for (const MethodInfo &E : methods) {
  3424. if (E.name == p_function) {
  3425. function_signature_from_info(E, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3426. r_static = Variant::is_builtin_method_static(p_base_type.builtin_type, function_name);
  3427. return true;
  3428. }
  3429. }
  3430. return false;
  3431. }
  3432. if (p_is_constructor) {
  3433. function_name = "_init";
  3434. r_static = true;
  3435. }
  3436. GDScriptParser::ClassNode *base_class = p_base_type.class_type;
  3437. GDScriptParser::FunctionNode *found_function = nullptr;
  3438. while (found_function == nullptr && base_class != nullptr) {
  3439. if (base_class->has_member(function_name)) {
  3440. if (base_class->get_member(function_name).type != GDScriptParser::ClassNode::Member::FUNCTION) {
  3441. // TODO: If this is Callable it can have a better error message.
  3442. push_error(vformat(R"(Member "%s" is not a function.)", function_name), p_source);
  3443. return false;
  3444. }
  3445. found_function = base_class->get_member(function_name).function;
  3446. }
  3447. base_class = base_class->base_type.class_type;
  3448. }
  3449. if (found_function != nullptr) {
  3450. r_static = p_is_constructor || found_function->is_static;
  3451. for (int i = 0; i < found_function->parameters.size(); i++) {
  3452. r_par_types.push_back(found_function->parameters[i]->get_datatype());
  3453. if (found_function->parameters[i]->default_value != nullptr) {
  3454. r_default_arg_count++;
  3455. }
  3456. }
  3457. r_return_type = found_function->get_datatype();
  3458. r_return_type.is_meta_type = false;
  3459. r_return_type.is_coroutine = found_function->is_coroutine;
  3460. return true;
  3461. }
  3462. Ref<Script> base_script = p_base_type.script_type;
  3463. while (base_script.is_valid() && base_script->has_method(function_name)) {
  3464. MethodInfo info = base_script->get_method_info(function_name);
  3465. if (!(info == MethodInfo())) {
  3466. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3467. }
  3468. base_script = base_script->get_base_script();
  3469. }
  3470. // If the base is a script, it might be trying to access members of the Script class itself.
  3471. if (p_base_type.is_meta_type && !p_is_constructor && (p_base_type.kind == GDScriptParser::DataType::SCRIPT || p_base_type.kind == GDScriptParser::DataType::CLASS)) {
  3472. MethodInfo info;
  3473. StringName script_class = p_base_type.kind == GDScriptParser::DataType::SCRIPT ? p_base_type.script_type->get_class_name() : StringName(GDScript::get_class_static());
  3474. if (ClassDB::get_method_info(script_class, function_name, &info)) {
  3475. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3476. }
  3477. }
  3478. StringName base_native = p_base_type.native_type;
  3479. #ifdef DEBUG_ENABLED
  3480. if (base_native != StringName()) {
  3481. // Empty native class might happen in some Script implementations.
  3482. // Just ignore it.
  3483. if (!class_exists(base_native)) {
  3484. ERR_FAIL_V_MSG(false, vformat("Native class %s used in script doesn't exist or isn't exposed.", base_native));
  3485. }
  3486. }
  3487. #endif
  3488. if (p_is_constructor) {
  3489. // Native types always have a default constructor.
  3490. r_return_type = p_base_type;
  3491. r_return_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3492. r_return_type.is_meta_type = false;
  3493. return true;
  3494. }
  3495. MethodInfo info;
  3496. if (ClassDB::get_method_info(base_native, function_name, &info)) {
  3497. bool valid = function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3498. if (valid && Engine::get_singleton()->has_singleton(base_native)) {
  3499. r_static = true;
  3500. }
  3501. return valid;
  3502. }
  3503. return false;
  3504. }
  3505. bool GDScriptAnalyzer::function_signature_from_info(const MethodInfo &p_info, GDScriptParser::DataType &r_return_type, List<GDScriptParser::DataType> &r_par_types, int &r_default_arg_count, bool &r_static, bool &r_vararg) {
  3506. r_return_type = type_from_property(p_info.return_val);
  3507. r_default_arg_count = p_info.default_arguments.size();
  3508. r_vararg = (p_info.flags & METHOD_FLAG_VARARG) != 0;
  3509. r_static = (p_info.flags & METHOD_FLAG_STATIC) != 0;
  3510. for (const PropertyInfo &E : p_info.arguments) {
  3511. r_par_types.push_back(type_from_property(E));
  3512. }
  3513. return true;
  3514. }
  3515. bool GDScriptAnalyzer::validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call) {
  3516. List<GDScriptParser::DataType> arg_types;
  3517. for (const PropertyInfo &E : p_method.arguments) {
  3518. arg_types.push_back(type_from_property(E));
  3519. }
  3520. return validate_call_arg(arg_types, p_method.default_arguments.size(), (p_method.flags & METHOD_FLAG_VARARG) != 0, p_call);
  3521. }
  3522. bool GDScriptAnalyzer::validate_call_arg(const List<GDScriptParser::DataType> &p_par_types, int p_default_args_count, bool p_is_vararg, const GDScriptParser::CallNode *p_call) {
  3523. bool valid = true;
  3524. if (p_call->arguments.size() < p_par_types.size() - p_default_args_count) {
  3525. push_error(vformat(R"*(Too few arguments for "%s()" call. Expected at least %d but received %d.)*", p_call->function_name, p_par_types.size() - p_default_args_count, p_call->arguments.size()), p_call);
  3526. valid = false;
  3527. }
  3528. if (!p_is_vararg && p_call->arguments.size() > p_par_types.size()) {
  3529. push_error(vformat(R"*(Too many arguments for "%s()" call. Expected at most %d but received %d.)*", p_call->function_name, p_par_types.size(), p_call->arguments.size()), p_call->arguments[p_par_types.size()]);
  3530. valid = false;
  3531. }
  3532. for (int i = 0; i < p_call->arguments.size(); i++) {
  3533. if (i >= p_par_types.size()) {
  3534. // Already on vararg place.
  3535. break;
  3536. }
  3537. GDScriptParser::DataType par_type = p_par_types[i];
  3538. GDScriptParser::DataType arg_type = p_call->arguments[i]->get_datatype();
  3539. if (arg_type.is_variant()) {
  3540. // Argument can be anything, so this is unsafe.
  3541. mark_node_unsafe(p_call->arguments[i]);
  3542. } else if (par_type.is_hard_type() && !is_type_compatible(par_type, arg_type, true)) {
  3543. // Supertypes are acceptable for dynamic compliance, but it's unsafe.
  3544. mark_node_unsafe(p_call);
  3545. if (!is_type_compatible(arg_type, par_type)) {
  3546. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*",
  3547. p_call->function_name, i + 1, par_type.to_string(), arg_type.to_string()),
  3548. p_call->arguments[i]);
  3549. valid = false;
  3550. }
  3551. #ifdef DEBUG_ENABLED
  3552. } else {
  3553. if (par_type.kind == GDScriptParser::DataType::BUILTIN && par_type.builtin_type == Variant::INT && arg_type.kind == GDScriptParser::DataType::BUILTIN && arg_type.builtin_type == Variant::FLOAT) {
  3554. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  3555. }
  3556. #endif
  3557. }
  3558. }
  3559. return valid;
  3560. }
  3561. #ifdef DEBUG_ENABLED
  3562. bool GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context) {
  3563. const StringName &name = p_local->name;
  3564. GDScriptParser::DataType base = parser->current_class->get_datatype();
  3565. GDScriptParser::ClassNode *base_class = base.class_type;
  3566. {
  3567. List<MethodInfo> gdscript_funcs;
  3568. GDScriptLanguage::get_singleton()->get_public_functions(&gdscript_funcs);
  3569. for (MethodInfo &info : gdscript_funcs) {
  3570. if (info.name == name) {
  3571. parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function");
  3572. return true;
  3573. }
  3574. }
  3575. if (Variant::has_utility_function(name)) {
  3576. parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function");
  3577. return true;
  3578. } else if (ClassDB::class_exists(name)) {
  3579. parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "global class");
  3580. return true;
  3581. }
  3582. }
  3583. while (base_class != nullptr) {
  3584. if (base_class->has_member(name)) {
  3585. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE, p_context, p_local->name, base_class->get_member(name).get_type_name(), itos(base_class->get_member(name).get_line()));
  3586. return true;
  3587. }
  3588. base_class = base_class->base_type.class_type;
  3589. }
  3590. StringName base_native = base.native_type;
  3591. ERR_FAIL_COND_V_MSG(!class_exists(base_native), false, "Non-existent native base class.");
  3592. StringName parent = base_native;
  3593. while (parent != StringName()) {
  3594. if (ClassDB::has_method(parent, name, true)) {
  3595. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "method", parent);
  3596. return true;
  3597. } else if (ClassDB::has_signal(parent, name, true)) {
  3598. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "signal", parent);
  3599. return true;
  3600. } else if (ClassDB::has_property(parent, name, true)) {
  3601. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "property", parent);
  3602. return true;
  3603. } else if (ClassDB::has_integer_constant(parent, name, true)) {
  3604. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "constant", parent);
  3605. return true;
  3606. } else if (ClassDB::has_enum(parent, name, true)) {
  3607. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "enum", parent);
  3608. return true;
  3609. }
  3610. parent = ClassDB::get_parent_class(parent);
  3611. }
  3612. return false;
  3613. }
  3614. #endif
  3615. GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, bool &r_valid, const GDScriptParser::Node *p_source) {
  3616. // Unary version.
  3617. GDScriptParser::DataType nil_type;
  3618. nil_type.builtin_type = Variant::NIL;
  3619. nil_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  3620. return get_operation_type(p_operation, p_a, nil_type, r_valid, p_source);
  3621. }
  3622. GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, const GDScriptParser::DataType &p_b, bool &r_valid, const GDScriptParser::Node *p_source) {
  3623. GDScriptParser::DataType result;
  3624. result.kind = GDScriptParser::DataType::VARIANT;
  3625. Variant::Type a_type = p_a.builtin_type;
  3626. Variant::Type b_type = p_b.builtin_type;
  3627. if (p_a.kind == GDScriptParser::DataType::ENUM) {
  3628. if (p_a.is_meta_type) {
  3629. a_type = Variant::DICTIONARY;
  3630. } else {
  3631. a_type = Variant::INT;
  3632. }
  3633. }
  3634. if (p_b.kind == GDScriptParser::DataType::ENUM) {
  3635. if (p_b.is_meta_type) {
  3636. b_type = Variant::DICTIONARY;
  3637. } else {
  3638. b_type = Variant::INT;
  3639. }
  3640. }
  3641. Variant::ValidatedOperatorEvaluator op_eval = Variant::get_validated_operator_evaluator(p_operation, a_type, b_type);
  3642. bool hard_operation = p_a.is_hard_type() && p_b.is_hard_type();
  3643. bool validated = op_eval != nullptr;
  3644. if (hard_operation && !validated) {
  3645. r_valid = false;
  3646. return result;
  3647. } else if (hard_operation && validated) {
  3648. r_valid = true;
  3649. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  3650. result.kind = GDScriptParser::DataType::BUILTIN;
  3651. result.builtin_type = Variant::get_operator_return_type(p_operation, a_type, b_type);
  3652. } else if (!hard_operation && !validated) {
  3653. r_valid = true;
  3654. result.type_source = GDScriptParser::DataType::UNDETECTED;
  3655. result.kind = GDScriptParser::DataType::VARIANT;
  3656. result.builtin_type = Variant::NIL;
  3657. } else if (!hard_operation && validated) {
  3658. r_valid = true;
  3659. result.type_source = GDScriptParser::DataType::INFERRED;
  3660. result.kind = GDScriptParser::DataType::BUILTIN;
  3661. result.builtin_type = Variant::get_operator_return_type(p_operation, a_type, b_type);
  3662. }
  3663. return result;
  3664. }
  3665. // TODO: Add safe/unsafe return variable (for variant cases)
  3666. bool GDScriptAnalyzer::is_type_compatible(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion, const GDScriptParser::Node *p_source_node) {
  3667. // These return "true" so it doesn't affect users negatively.
  3668. ERR_FAIL_COND_V_MSG(!p_target.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset target type");
  3669. ERR_FAIL_COND_V_MSG(!p_source.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset value type");
  3670. if (p_target.kind == GDScriptParser::DataType::VARIANT) {
  3671. // Variant can receive anything.
  3672. return true;
  3673. }
  3674. if (p_source.kind == GDScriptParser::DataType::VARIANT) {
  3675. // TODO: This is acceptable but unsafe. Make sure unsafe line is set.
  3676. return true;
  3677. }
  3678. if (p_target.kind == GDScriptParser::DataType::BUILTIN) {
  3679. bool valid = p_source.kind == GDScriptParser::DataType::BUILTIN && p_target.builtin_type == p_source.builtin_type;
  3680. if (!valid && p_allow_implicit_conversion) {
  3681. valid = Variant::can_convert_strict(p_source.builtin_type, p_target.builtin_type);
  3682. }
  3683. if (!valid && p_target.builtin_type == Variant::INT && p_source.kind == GDScriptParser::DataType::ENUM && !p_source.is_meta_type) {
  3684. // Enum value is also integer.
  3685. valid = true;
  3686. }
  3687. if (valid && p_target.builtin_type == Variant::ARRAY && p_source.builtin_type == Variant::ARRAY) {
  3688. // Check the element type.
  3689. if (p_target.has_container_element_type()) {
  3690. if (!p_source.has_container_element_type()) {
  3691. // TODO: Maybe this is valid but unsafe?
  3692. // Variant array can't be appended to typed array.
  3693. valid = false;
  3694. } else {
  3695. valid = is_type_compatible(p_target.get_container_element_type(), p_source.get_container_element_type(), false);
  3696. }
  3697. }
  3698. }
  3699. return valid;
  3700. }
  3701. if (p_target.kind == GDScriptParser::DataType::ENUM) {
  3702. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::INT) {
  3703. #ifdef DEBUG_ENABLED
  3704. if (p_source_node) {
  3705. parser->push_warning(p_source_node, GDScriptWarning::INT_ASSIGNED_TO_ENUM);
  3706. }
  3707. #endif
  3708. return true;
  3709. }
  3710. if (p_source.kind == GDScriptParser::DataType::ENUM) {
  3711. if (p_source.native_type == p_target.native_type) {
  3712. return true;
  3713. }
  3714. }
  3715. return false;
  3716. }
  3717. // From here on the target type is an object, so we have to test polymorphism.
  3718. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::NIL) {
  3719. // null is acceptable in object.
  3720. return true;
  3721. }
  3722. StringName src_native;
  3723. Ref<Script> src_script;
  3724. const GDScriptParser::ClassNode *src_class = nullptr;
  3725. switch (p_source.kind) {
  3726. case GDScriptParser::DataType::NATIVE:
  3727. if (p_target.kind != GDScriptParser::DataType::NATIVE) {
  3728. // Non-native class cannot be supertype of native.
  3729. return false;
  3730. }
  3731. if (p_source.is_meta_type) {
  3732. src_native = GDScriptNativeClass::get_class_static();
  3733. } else {
  3734. src_native = p_source.native_type;
  3735. }
  3736. break;
  3737. case GDScriptParser::DataType::SCRIPT:
  3738. if (p_target.kind == GDScriptParser::DataType::CLASS) {
  3739. // A script type cannot be a subtype of a GDScript class.
  3740. return false;
  3741. }
  3742. if (p_source.is_meta_type) {
  3743. src_native = p_source.script_type->get_class_name();
  3744. } else {
  3745. src_script = p_source.script_type;
  3746. src_native = src_script->get_instance_base_type();
  3747. }
  3748. break;
  3749. case GDScriptParser::DataType::CLASS:
  3750. if (p_source.is_meta_type) {
  3751. src_native = GDScript::get_class_static();
  3752. } else {
  3753. src_class = p_source.class_type;
  3754. const GDScriptParser::ClassNode *base = src_class;
  3755. while (base->base_type.kind == GDScriptParser::DataType::CLASS) {
  3756. base = base->base_type.class_type;
  3757. }
  3758. src_native = base->base_type.native_type;
  3759. src_script = base->base_type.script_type;
  3760. }
  3761. break;
  3762. case GDScriptParser::DataType::VARIANT:
  3763. case GDScriptParser::DataType::BUILTIN:
  3764. case GDScriptParser::DataType::ENUM:
  3765. case GDScriptParser::DataType::UNRESOLVED:
  3766. break; // Already solved before.
  3767. }
  3768. switch (p_target.kind) {
  3769. case GDScriptParser::DataType::NATIVE: {
  3770. if (p_target.is_meta_type) {
  3771. return ClassDB::is_parent_class(src_native, GDScriptNativeClass::get_class_static());
  3772. }
  3773. return ClassDB::is_parent_class(src_native, p_target.native_type);
  3774. }
  3775. case GDScriptParser::DataType::SCRIPT:
  3776. if (p_target.is_meta_type) {
  3777. return ClassDB::is_parent_class(src_native, p_target.script_type->get_class_name());
  3778. }
  3779. while (src_script.is_valid()) {
  3780. if (src_script == p_target.script_type) {
  3781. return true;
  3782. }
  3783. src_script = src_script->get_base_script();
  3784. }
  3785. return false;
  3786. case GDScriptParser::DataType::CLASS:
  3787. if (p_target.is_meta_type) {
  3788. return ClassDB::is_parent_class(src_native, GDScript::get_class_static());
  3789. }
  3790. while (src_class != nullptr) {
  3791. if (src_class->fqcn == p_target.class_type->fqcn) {
  3792. return true;
  3793. }
  3794. src_class = src_class->base_type.class_type;
  3795. }
  3796. return false;
  3797. case GDScriptParser::DataType::VARIANT:
  3798. case GDScriptParser::DataType::BUILTIN:
  3799. case GDScriptParser::DataType::ENUM:
  3800. case GDScriptParser::DataType::UNRESOLVED:
  3801. break; // Already solved before.
  3802. }
  3803. return false;
  3804. }
  3805. void GDScriptAnalyzer::push_error(const String &p_message, const GDScriptParser::Node *p_origin) {
  3806. mark_node_unsafe(p_origin);
  3807. parser->push_error(p_message, p_origin);
  3808. }
  3809. void GDScriptAnalyzer::mark_node_unsafe(const GDScriptParser::Node *p_node) {
  3810. #ifdef DEBUG_ENABLED
  3811. for (int i = p_node->start_line; i <= p_node->end_line; i++) {
  3812. parser->unsafe_lines.insert(i);
  3813. }
  3814. #endif
  3815. }
  3816. void GDScriptAnalyzer::mark_lambda_use_self() {
  3817. for (GDScriptParser::LambdaNode *lambda : lambda_stack) {
  3818. lambda->use_self = true;
  3819. }
  3820. }
  3821. bool GDScriptAnalyzer::class_exists(const StringName &p_class) const {
  3822. return ClassDB::class_exists(p_class) && ClassDB::is_class_exposed(p_class);
  3823. }
  3824. Ref<GDScriptParserRef> GDScriptAnalyzer::get_parser_for(const String &p_path) {
  3825. Ref<GDScriptParserRef> ref;
  3826. if (depended_parsers.has(p_path)) {
  3827. ref = depended_parsers[p_path];
  3828. } else {
  3829. Error err = OK;
  3830. ref = GDScriptCache::get_parser(p_path, GDScriptParserRef::EMPTY, err, parser->script_path);
  3831. if (ref.is_valid()) {
  3832. depended_parsers[p_path] = ref;
  3833. }
  3834. }
  3835. return ref;
  3836. }
  3837. Error GDScriptAnalyzer::resolve_inheritance() {
  3838. return resolve_inheritance(parser->head);
  3839. }
  3840. Error GDScriptAnalyzer::resolve_interface() {
  3841. resolve_class_interface(parser->head);
  3842. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3843. }
  3844. Error GDScriptAnalyzer::resolve_body() {
  3845. resolve_class_body(parser->head);
  3846. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3847. }
  3848. Error GDScriptAnalyzer::resolve_program() {
  3849. resolve_class_interface(parser->head);
  3850. resolve_class_body(parser->head);
  3851. for (KeyValue<String, Ref<GDScriptParserRef>> &K : depended_parsers) {
  3852. if (K.value.is_null()) {
  3853. return ERR_PARSE_ERROR;
  3854. }
  3855. K.value->raise_status(GDScriptParserRef::FULLY_SOLVED);
  3856. }
  3857. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3858. }
  3859. Error GDScriptAnalyzer::analyze() {
  3860. parser->errors.clear();
  3861. Error err = resolve_inheritance(parser->head);
  3862. if (err) {
  3863. return err;
  3864. }
  3865. return resolve_program();
  3866. }
  3867. GDScriptAnalyzer::GDScriptAnalyzer(GDScriptParser *p_parser) {
  3868. parser = p_parser;
  3869. }