gdscript_analyzer.cpp 180 KB

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