gdscript_analyzer.cpp 226 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721
  1. /**************************************************************************/
  2. /* gdscript_analyzer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "gdscript.h"
  32. #include "gdscript_utility_callable.h"
  33. #include "gdscript_utility_functions.h"
  34. #include "core/config/engine.h"
  35. #include "core/config/project_settings.h"
  36. #include "core/core_constants.h"
  37. #include "core/core_string_names.h"
  38. #include "core/io/file_access.h"
  39. #include "core/io/resource_loader.h"
  40. #include "core/object/class_db.h"
  41. #include "core/object/script_language.h"
  42. #include "core/templates/hash_map.h"
  43. #include "scene/resources/packed_scene.h"
  44. #if defined(TOOLS_ENABLED) && !defined(DISABLE_DEPRECATED)
  45. #define SUGGEST_GODOT4_RENAMES
  46. #include "editor/renames_map_3_to_4.h"
  47. #endif
  48. #define UNNAMED_ENUM "<anonymous enum>"
  49. #define ENUM_SEPARATOR "."
  50. static MethodInfo info_from_utility_func(const StringName &p_function) {
  51. ERR_FAIL_COND_V(!Variant::has_utility_function(p_function), MethodInfo());
  52. MethodInfo info(p_function);
  53. if (Variant::has_utility_function_return_value(p_function)) {
  54. info.return_val.type = Variant::get_utility_function_return_type(p_function);
  55. if (info.return_val.type == Variant::NIL) {
  56. info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  57. }
  58. }
  59. if (Variant::is_utility_function_vararg(p_function)) {
  60. info.flags |= METHOD_FLAG_VARARG;
  61. } else {
  62. for (int i = 0; i < Variant::get_utility_function_argument_count(p_function); i++) {
  63. PropertyInfo pi;
  64. #ifdef DEBUG_METHODS_ENABLED
  65. pi.name = Variant::get_utility_function_argument_name(p_function, i);
  66. #else
  67. pi.name = "arg" + itos(i + 1);
  68. #endif
  69. pi.type = Variant::get_utility_function_argument_type(p_function, i);
  70. info.arguments.push_back(pi);
  71. }
  72. }
  73. return info;
  74. }
  75. static GDScriptParser::DataType make_callable_type(const MethodInfo &p_info) {
  76. GDScriptParser::DataType type;
  77. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  78. type.kind = GDScriptParser::DataType::BUILTIN;
  79. type.builtin_type = Variant::CALLABLE;
  80. type.is_constant = true;
  81. type.method_info = p_info;
  82. return type;
  83. }
  84. static GDScriptParser::DataType make_signal_type(const MethodInfo &p_info) {
  85. GDScriptParser::DataType type;
  86. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  87. type.kind = GDScriptParser::DataType::BUILTIN;
  88. type.builtin_type = Variant::SIGNAL;
  89. type.is_constant = true;
  90. type.method_info = p_info;
  91. return type;
  92. }
  93. static GDScriptParser::DataType make_native_meta_type(const StringName &p_class_name) {
  94. GDScriptParser::DataType type;
  95. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  96. type.kind = GDScriptParser::DataType::NATIVE;
  97. type.builtin_type = Variant::OBJECT;
  98. type.native_type = p_class_name;
  99. type.is_constant = true;
  100. type.is_meta_type = true;
  101. return type;
  102. }
  103. static GDScriptParser::DataType make_script_meta_type(const Ref<Script> &p_script) {
  104. GDScriptParser::DataType type;
  105. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  106. type.kind = GDScriptParser::DataType::SCRIPT;
  107. type.builtin_type = Variant::OBJECT;
  108. type.native_type = p_script->get_instance_base_type();
  109. type.script_type = p_script;
  110. type.script_path = p_script->get_path();
  111. type.is_constant = true;
  112. type.is_meta_type = true;
  113. return type;
  114. }
  115. // In enum types, native_type is used to store the class (native or otherwise) that the enum belongs to.
  116. // This disambiguates between similarly named enums in base classes or outer classes
  117. static GDScriptParser::DataType make_enum_type(const StringName &p_enum_name, const String &p_base_name, const bool p_meta = false) {
  118. GDScriptParser::DataType type;
  119. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  120. type.kind = GDScriptParser::DataType::ENUM;
  121. type.builtin_type = p_meta ? Variant::DICTIONARY : Variant::INT;
  122. type.enum_type = p_enum_name;
  123. type.is_constant = true;
  124. type.is_meta_type = p_meta;
  125. // For enums, native_type is only used to check compatibility in is_type_compatible()
  126. // We can set anything readable here for error messages, as long as it uniquely identifies the type of the enum
  127. if (p_base_name.is_empty()) {
  128. type.native_type = p_enum_name;
  129. } else {
  130. type.native_type = p_base_name + ENUM_SEPARATOR + p_enum_name;
  131. }
  132. return type;
  133. }
  134. static GDScriptParser::DataType make_native_enum_type(const StringName &p_enum_name, const StringName &p_native_class, bool p_meta = true) {
  135. // Find out which base class declared the enum, so the name is always the same even when coming from other contexts.
  136. StringName native_base = p_native_class;
  137. while (true && native_base != StringName()) {
  138. if (ClassDB::has_enum(native_base, p_enum_name, true)) {
  139. break;
  140. }
  141. native_base = ClassDB::get_parent_class_nocheck(native_base);
  142. }
  143. GDScriptParser::DataType type = make_enum_type(p_enum_name, native_base, p_meta);
  144. if (p_meta) {
  145. type.builtin_type = Variant::NIL; // Native enum types are not Dictionaries.
  146. }
  147. List<StringName> enum_values;
  148. ClassDB::get_enum_constants(native_base, p_enum_name, &enum_values, true);
  149. for (const StringName &E : enum_values) {
  150. type.enum_values[E] = ClassDB::get_integer_constant(native_base, E);
  151. }
  152. return type;
  153. }
  154. static GDScriptParser::DataType make_global_enum_type(const StringName &p_enum_name, const StringName &p_base, bool p_meta = true) {
  155. GDScriptParser::DataType type = make_enum_type(p_enum_name, p_base, p_meta);
  156. if (p_meta) {
  157. type.builtin_type = Variant::NIL; // Native enum types are not Dictionaries.
  158. type.is_pseudo_type = true;
  159. }
  160. HashMap<StringName, int64_t> enum_values;
  161. CoreConstants::get_enum_values(type.native_type, &enum_values);
  162. for (const KeyValue<StringName, int64_t> &element : enum_values) {
  163. type.enum_values[element.key] = element.value;
  164. }
  165. return type;
  166. }
  167. static GDScriptParser::DataType make_builtin_meta_type(Variant::Type p_type) {
  168. GDScriptParser::DataType type;
  169. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  170. type.kind = GDScriptParser::DataType::BUILTIN;
  171. type.builtin_type = p_type;
  172. type.is_constant = true;
  173. type.is_meta_type = true;
  174. return type;
  175. }
  176. bool GDScriptAnalyzer::has_member_name_conflict_in_script_class(const StringName &p_member_name, const GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_member) {
  177. if (p_class->members_indices.has(p_member_name)) {
  178. int index = p_class->members_indices[p_member_name];
  179. const GDScriptParser::ClassNode::Member *member = &p_class->members[index];
  180. if (member->type == GDScriptParser::ClassNode::Member::VARIABLE ||
  181. member->type == GDScriptParser::ClassNode::Member::CONSTANT ||
  182. member->type == GDScriptParser::ClassNode::Member::ENUM ||
  183. member->type == GDScriptParser::ClassNode::Member::ENUM_VALUE ||
  184. member->type == GDScriptParser::ClassNode::Member::CLASS ||
  185. member->type == GDScriptParser::ClassNode::Member::SIGNAL) {
  186. return true;
  187. }
  188. if (p_member->type != GDScriptParser::Node::FUNCTION && member->type == GDScriptParser::ClassNode::Member::FUNCTION) {
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. bool GDScriptAnalyzer::has_member_name_conflict_in_native_type(const StringName &p_member_name, const StringName &p_native_type_string) {
  195. if (ClassDB::has_signal(p_native_type_string, p_member_name)) {
  196. return true;
  197. }
  198. if (ClassDB::has_property(p_native_type_string, p_member_name)) {
  199. return true;
  200. }
  201. if (ClassDB::has_integer_constant(p_native_type_string, p_member_name)) {
  202. return true;
  203. }
  204. if (p_member_name == CoreStringNames::get_singleton()->_script) {
  205. return true;
  206. }
  207. return false;
  208. }
  209. Error GDScriptAnalyzer::check_native_member_name_conflict(const StringName &p_member_name, const GDScriptParser::Node *p_member_node, const StringName &p_native_type_string) {
  210. if (has_member_name_conflict_in_native_type(p_member_name, p_native_type_string)) {
  211. push_error(vformat(R"(Member "%s" redefined (original in native class '%s'))", p_member_name, p_native_type_string), p_member_node);
  212. return ERR_PARSE_ERROR;
  213. }
  214. if (class_exists(p_member_name)) {
  215. push_error(vformat(R"(The member "%s" shadows a native class.)", p_member_name), p_member_node);
  216. return ERR_PARSE_ERROR;
  217. }
  218. if (GDScriptParser::get_builtin_type(p_member_name) < Variant::VARIANT_MAX) {
  219. push_error(vformat(R"(The member "%s" cannot have the same name as a builtin type.)", p_member_name), p_member_node);
  220. return ERR_PARSE_ERROR;
  221. }
  222. return OK;
  223. }
  224. Error GDScriptAnalyzer::check_class_member_name_conflict(const GDScriptParser::ClassNode *p_class_node, const StringName &p_member_name, const GDScriptParser::Node *p_member_node) {
  225. // TODO check outer classes for static members only
  226. const GDScriptParser::DataType *current_data_type = &p_class_node->base_type;
  227. while (current_data_type && current_data_type->kind == GDScriptParser::DataType::Kind::CLASS) {
  228. GDScriptParser::ClassNode *current_class_node = current_data_type->class_type;
  229. if (has_member_name_conflict_in_script_class(p_member_name, current_class_node, p_member_node)) {
  230. String parent_class_name = current_class_node->fqcn;
  231. if (current_class_node->identifier != nullptr) {
  232. parent_class_name = current_class_node->identifier->name;
  233. }
  234. push_error(vformat(R"(The member "%s" already exists in parent class %s.)", p_member_name, parent_class_name), p_member_node);
  235. return ERR_PARSE_ERROR;
  236. }
  237. current_data_type = &current_class_node->base_type;
  238. }
  239. // No need for native class recursion because Node exposes all Object's properties.
  240. if (current_data_type && current_data_type->kind == GDScriptParser::DataType::Kind::NATIVE) {
  241. if (current_data_type->native_type != StringName()) {
  242. return check_native_member_name_conflict(
  243. p_member_name,
  244. p_member_node,
  245. current_data_type->native_type);
  246. }
  247. }
  248. return OK;
  249. }
  250. void GDScriptAnalyzer::get_class_node_current_scope_classes(GDScriptParser::ClassNode *p_node, List<GDScriptParser::ClassNode *> *p_list) {
  251. ERR_FAIL_NULL(p_node);
  252. ERR_FAIL_NULL(p_list);
  253. if (p_list->find(p_node) != nullptr) {
  254. return;
  255. }
  256. p_list->push_back(p_node);
  257. // TODO: Try to solve class inheritance if not yet resolving.
  258. // Prioritize node base type over its outer class
  259. if (p_node->base_type.class_type != nullptr) {
  260. get_class_node_current_scope_classes(p_node->base_type.class_type, p_list);
  261. }
  262. if (p_node->outer != nullptr) {
  263. get_class_node_current_scope_classes(p_node->outer, p_list);
  264. }
  265. }
  266. Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_source) {
  267. if (p_source == nullptr && parser->has_class(p_class)) {
  268. p_source = p_class;
  269. }
  270. if (p_class->base_type.is_resolving()) {
  271. push_error(vformat(R"(Could not resolve class "%s": Cyclic reference.)", type_from_metatype(p_class->get_datatype()).to_string()), p_source);
  272. return ERR_PARSE_ERROR;
  273. }
  274. if (!p_class->base_type.has_no_type()) {
  275. // Already resolved.
  276. return OK;
  277. }
  278. if (!parser->has_class(p_class)) {
  279. String script_path = p_class->get_datatype().script_path;
  280. Ref<GDScriptParserRef> parser_ref = parser->get_depended_parser_for(script_path);
  281. if (parser_ref.is_null()) {
  282. push_error(vformat(R"(Could not find script "%s".)", script_path), p_source);
  283. return ERR_PARSE_ERROR;
  284. }
  285. Error err = parser_ref->raise_status(GDScriptParserRef::PARSED);
  286. if (err) {
  287. push_error(vformat(R"(Could not parse script "%s": %s.)", script_path, error_names[err]), p_source);
  288. return ERR_PARSE_ERROR;
  289. }
  290. ERR_FAIL_COND_V_MSG(!parser_ref->get_parser()->has_class(p_class), ERR_PARSE_ERROR, R"(Parser bug: Mismatched external parser.)");
  291. GDScriptAnalyzer *other_analyzer = parser_ref->get_analyzer();
  292. GDScriptParser *other_parser = parser_ref->get_parser();
  293. int error_count = other_parser->errors.size();
  294. other_analyzer->resolve_class_inheritance(p_class);
  295. if (other_parser->errors.size() > error_count) {
  296. push_error(vformat(R"(Could not resolve inheritance for class "%s".)", p_class->fqcn), p_source);
  297. return ERR_PARSE_ERROR;
  298. }
  299. return OK;
  300. }
  301. GDScriptParser::ClassNode *previous_class = parser->current_class;
  302. parser->current_class = p_class;
  303. if (p_class->identifier) {
  304. StringName class_name = p_class->identifier->name;
  305. if (GDScriptParser::get_builtin_type(class_name) < Variant::VARIANT_MAX) {
  306. push_error(vformat(R"(Class "%s" hides a built-in type.)", class_name), p_class->identifier);
  307. } else if (class_exists(class_name)) {
  308. push_error(vformat(R"(Class "%s" hides a native class.)", class_name), p_class->identifier);
  309. } else if (ScriptServer::is_global_class(class_name) && (!GDScript::is_canonically_equal_paths(ScriptServer::get_global_class_path(class_name), parser->script_path) || p_class != parser->head)) {
  310. push_error(vformat(R"(Class "%s" hides a global script class.)", class_name), p_class->identifier);
  311. } else if (ProjectSettings::get_singleton()->has_autoload(class_name) && ProjectSettings::get_singleton()->get_autoload(class_name).is_singleton) {
  312. push_error(vformat(R"(Class "%s" hides an autoload singleton.)", class_name), p_class->identifier);
  313. }
  314. }
  315. GDScriptParser::DataType resolving_datatype;
  316. resolving_datatype.kind = GDScriptParser::DataType::RESOLVING;
  317. p_class->base_type = resolving_datatype;
  318. // Set datatype for class.
  319. GDScriptParser::DataType class_type;
  320. class_type.is_constant = true;
  321. class_type.is_meta_type = true;
  322. class_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  323. class_type.kind = GDScriptParser::DataType::CLASS;
  324. class_type.class_type = p_class;
  325. class_type.script_path = parser->script_path;
  326. class_type.builtin_type = Variant::OBJECT;
  327. p_class->set_datatype(class_type);
  328. GDScriptParser::DataType result;
  329. if (!p_class->extends_used) {
  330. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  331. result.kind = GDScriptParser::DataType::NATIVE;
  332. result.builtin_type = Variant::OBJECT;
  333. result.native_type = SNAME("RefCounted");
  334. } else {
  335. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  336. GDScriptParser::DataType base;
  337. int extends_index = 0;
  338. if (!p_class->extends_path.is_empty()) {
  339. if (p_class->extends_path.is_relative_path()) {
  340. p_class->extends_path = class_type.script_path.get_base_dir().path_join(p_class->extends_path).simplify_path();
  341. }
  342. Ref<GDScriptParserRef> ext_parser = parser->get_depended_parser_for(p_class->extends_path);
  343. if (ext_parser.is_null()) {
  344. push_error(vformat(R"(Could not resolve super class path "%s".)", p_class->extends_path), p_class);
  345. return ERR_PARSE_ERROR;
  346. }
  347. Error err = ext_parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  348. if (err != OK) {
  349. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", p_class->extends_path), p_class);
  350. return err;
  351. }
  352. base = ext_parser->get_parser()->head->get_datatype();
  353. } else {
  354. if (p_class->extends.is_empty()) {
  355. push_error("Could not resolve an empty super class path.", p_class);
  356. return ERR_PARSE_ERROR;
  357. }
  358. GDScriptParser::IdentifierNode *id = p_class->extends[extends_index++];
  359. const StringName &name = id->name;
  360. base.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  361. if (ScriptServer::is_global_class(name)) {
  362. String base_path = ScriptServer::get_global_class_path(name);
  363. if (GDScript::is_canonically_equal_paths(base_path, parser->script_path)) {
  364. base = parser->head->get_datatype();
  365. } else {
  366. Ref<GDScriptParserRef> base_parser = parser->get_depended_parser_for(base_path);
  367. if (base_parser.is_null()) {
  368. push_error(vformat(R"(Could not resolve super class "%s".)", name), id);
  369. return ERR_PARSE_ERROR;
  370. }
  371. Error err = base_parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  372. if (err != OK) {
  373. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", name), id);
  374. return err;
  375. }
  376. base = base_parser->get_parser()->head->get_datatype();
  377. }
  378. } else if (ProjectSettings::get_singleton()->has_autoload(name) && ProjectSettings::get_singleton()->get_autoload(name).is_singleton) {
  379. const ProjectSettings::AutoloadInfo &info = ProjectSettings::get_singleton()->get_autoload(name);
  380. if (info.path.get_extension().to_lower() != GDScriptLanguage::get_singleton()->get_extension()) {
  381. push_error(vformat(R"(Singleton %s is not a GDScript.)", info.name), id);
  382. return ERR_PARSE_ERROR;
  383. }
  384. Ref<GDScriptParserRef> info_parser = parser->get_depended_parser_for(info.path);
  385. if (info_parser.is_null()) {
  386. push_error(vformat(R"(Could not parse singleton from "%s".)", info.path), id);
  387. return ERR_PARSE_ERROR;
  388. }
  389. Error err = info_parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  390. if (err != OK) {
  391. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", name), id);
  392. return err;
  393. }
  394. base = info_parser->get_parser()->head->get_datatype();
  395. } else if (class_exists(name)) {
  396. if (Engine::get_singleton()->has_singleton(name)) {
  397. push_error(vformat(R"(Cannot inherit native class "%s" because it is an engine singleton.)", name), id);
  398. return ERR_PARSE_ERROR;
  399. }
  400. base.kind = GDScriptParser::DataType::NATIVE;
  401. base.builtin_type = Variant::OBJECT;
  402. base.native_type = name;
  403. } else {
  404. // Look for other classes in script.
  405. bool found = false;
  406. List<GDScriptParser::ClassNode *> script_classes;
  407. get_class_node_current_scope_classes(p_class, &script_classes);
  408. for (GDScriptParser::ClassNode *look_class : script_classes) {
  409. if (look_class->identifier && look_class->identifier->name == name) {
  410. if (!look_class->get_datatype().is_set()) {
  411. Error err = resolve_class_inheritance(look_class, id);
  412. if (err) {
  413. return err;
  414. }
  415. }
  416. base = look_class->get_datatype();
  417. found = true;
  418. break;
  419. }
  420. if (look_class->has_member(name)) {
  421. resolve_class_member(look_class, name, id);
  422. GDScriptParser::ClassNode::Member member = look_class->get_member(name);
  423. GDScriptParser::DataType member_datatype = member.get_datatype();
  424. switch (member.type) {
  425. case GDScriptParser::ClassNode::Member::CLASS:
  426. break; // OK.
  427. case GDScriptParser::ClassNode::Member::CONSTANT:
  428. if (member_datatype.kind != GDScriptParser::DataType::SCRIPT && member_datatype.kind != GDScriptParser::DataType::CLASS) {
  429. push_error(vformat(R"(Constant "%s" is not a preloaded script or class.)", name), id);
  430. return ERR_PARSE_ERROR;
  431. }
  432. break;
  433. default:
  434. push_error(vformat(R"(Cannot use %s "%s" in extends chain.)", member.get_type_name(), name), id);
  435. return ERR_PARSE_ERROR;
  436. }
  437. base = member_datatype;
  438. found = true;
  439. break;
  440. }
  441. }
  442. if (!found) {
  443. push_error(vformat(R"(Could not find base class "%s".)", name), id);
  444. return ERR_PARSE_ERROR;
  445. }
  446. }
  447. }
  448. for (int index = extends_index; index < p_class->extends.size(); index++) {
  449. GDScriptParser::IdentifierNode *id = p_class->extends[index];
  450. if (base.kind != GDScriptParser::DataType::CLASS) {
  451. push_error(vformat(R"(Cannot get nested types for extension from non-GDScript type "%s".)", base.to_string()), id);
  452. return ERR_PARSE_ERROR;
  453. }
  454. reduce_identifier_from_base(id, &base);
  455. GDScriptParser::DataType id_type = id->get_datatype();
  456. if (!id_type.is_set()) {
  457. push_error(vformat(R"(Could not find nested type "%s".)", id->name), id);
  458. return ERR_PARSE_ERROR;
  459. } else if (id_type.kind != GDScriptParser::DataType::SCRIPT && id_type.kind != GDScriptParser::DataType::CLASS) {
  460. push_error(vformat(R"(Identifier "%s" is not a preloaded script or class.)", id->name), id);
  461. return ERR_PARSE_ERROR;
  462. }
  463. base = id_type;
  464. }
  465. result = base;
  466. }
  467. if (!result.is_set() || result.has_no_type()) {
  468. // TODO: More specific error messages.
  469. push_error(vformat(R"(Could not resolve inheritance for class "%s".)", p_class->identifier == nullptr ? "<main>" : p_class->identifier->name), p_class);
  470. return ERR_PARSE_ERROR;
  471. }
  472. // Check for cyclic inheritance.
  473. const GDScriptParser::ClassNode *base_class = result.class_type;
  474. while (base_class) {
  475. if (base_class->fqcn == p_class->fqcn) {
  476. push_error("Cyclic inheritance.", p_class);
  477. return ERR_PARSE_ERROR;
  478. }
  479. base_class = base_class->base_type.class_type;
  480. }
  481. p_class->base_type = result;
  482. class_type.native_type = result.native_type;
  483. p_class->set_datatype(class_type);
  484. // Add base class to the list of dependencies.
  485. if (result.kind == GDScriptParser::DataType::CLASS) {
  486. parser->add_dependency(result.script_path);
  487. }
  488. // Apply annotations.
  489. for (GDScriptParser::AnnotationNode *&E : p_class->annotations) {
  490. resolve_annotation(E);
  491. E->apply(parser, p_class, p_class->outer);
  492. }
  493. parser->current_class = previous_class;
  494. return OK;
  495. }
  496. Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive) {
  497. Error err = resolve_class_inheritance(p_class);
  498. if (err) {
  499. return err;
  500. }
  501. if (p_recursive) {
  502. for (int i = 0; i < p_class->members.size(); i++) {
  503. if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
  504. err = resolve_class_inheritance(p_class->members[i].m_class, true);
  505. if (err) {
  506. return err;
  507. }
  508. }
  509. }
  510. }
  511. return OK;
  512. }
  513. GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::TypeNode *p_type) {
  514. GDScriptParser::DataType bad_type;
  515. bad_type.kind = GDScriptParser::DataType::VARIANT;
  516. bad_type.type_source = GDScriptParser::DataType::INFERRED;
  517. if (p_type == nullptr) {
  518. return bad_type;
  519. }
  520. if (p_type->get_datatype().is_resolving()) {
  521. push_error(R"(Could not resolve datatype: Cyclic reference.)", p_type);
  522. return bad_type;
  523. }
  524. if (!p_type->get_datatype().has_no_type()) {
  525. return p_type->get_datatype();
  526. }
  527. GDScriptParser::DataType resolving_datatype;
  528. resolving_datatype.kind = GDScriptParser::DataType::RESOLVING;
  529. p_type->set_datatype(resolving_datatype);
  530. GDScriptParser::DataType result;
  531. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  532. if (p_type->type_chain.is_empty()) {
  533. // void.
  534. result.kind = GDScriptParser::DataType::BUILTIN;
  535. result.builtin_type = Variant::NIL;
  536. p_type->set_datatype(result);
  537. return result;
  538. }
  539. const GDScriptParser::IdentifierNode *first_id = p_type->type_chain[0];
  540. StringName first = first_id->name;
  541. bool type_found = false;
  542. if (first_id->suite && first_id->suite->has_local(first)) {
  543. const GDScriptParser::SuiteNode::Local &local = first_id->suite->get_local(first);
  544. if (local.type == GDScriptParser::SuiteNode::Local::CONSTANT) {
  545. result = local.get_datatype();
  546. if (!result.is_set()) {
  547. // Don't try to resolve it as the constant can be declared below.
  548. push_error(vformat(R"(Local constant "%s" is not resolved at this point.)", first), first_id);
  549. return bad_type;
  550. }
  551. if (result.is_meta_type) {
  552. type_found = true;
  553. } else if (Ref<Script>(local.constant->initializer->reduced_value).is_valid()) {
  554. Ref<GDScript> gdscript = local.constant->initializer->reduced_value;
  555. if (gdscript.is_valid()) {
  556. Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(gdscript->get_script_path());
  557. if (ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED) != OK) {
  558. push_error(vformat(R"(Could not parse script from "%s".)", gdscript->get_script_path()), first_id);
  559. return bad_type;
  560. }
  561. result = ref->get_parser()->head->get_datatype();
  562. } else {
  563. result = make_script_meta_type(local.constant->initializer->reduced_value);
  564. }
  565. type_found = true;
  566. } else {
  567. push_error(vformat(R"(Local constant "%s" is not a valid type.)", first), first_id);
  568. return bad_type;
  569. }
  570. } else {
  571. push_error(vformat(R"(Local %s "%s" cannot be used as a type.)", local.get_name(), first), first_id);
  572. return bad_type;
  573. }
  574. }
  575. if (!type_found) {
  576. if (first == SNAME("Variant")) {
  577. if (p_type->type_chain.size() == 2) {
  578. // May be nested enum.
  579. StringName enum_name = p_type->type_chain[1]->name;
  580. StringName qualified_name = String(first) + ENUM_SEPARATOR + String(p_type->type_chain[1]->name);
  581. if (CoreConstants::is_global_enum(qualified_name)) {
  582. result = make_global_enum_type(enum_name, first, true);
  583. return result;
  584. } else {
  585. push_error(vformat(R"(Name "%s" is not a nested type of "Variant".)", enum_name), p_type->type_chain[1]);
  586. return bad_type;
  587. }
  588. } else if (p_type->type_chain.size() > 2) {
  589. push_error(R"(Variant only contains enum types, which do not have nested types.)", p_type->type_chain[2]);
  590. return bad_type;
  591. }
  592. result.kind = GDScriptParser::DataType::VARIANT;
  593. } else if (GDScriptParser::get_builtin_type(first) < Variant::VARIANT_MAX) {
  594. // Built-in types.
  595. if (p_type->type_chain.size() > 1) {
  596. push_error(R"(Built-in types don't contain nested types.)", p_type->type_chain[1]);
  597. return bad_type;
  598. }
  599. result.kind = GDScriptParser::DataType::BUILTIN;
  600. result.builtin_type = GDScriptParser::get_builtin_type(first);
  601. if (result.builtin_type == Variant::ARRAY) {
  602. GDScriptParser::DataType container_type = type_from_metatype(resolve_datatype(p_type->get_container_type_or_null(0)));
  603. if (container_type.kind != GDScriptParser::DataType::VARIANT) {
  604. container_type.is_constant = false;
  605. result.set_container_element_type(0, container_type);
  606. }
  607. }
  608. } else if (class_exists(first)) {
  609. // Native engine classes.
  610. result.kind = GDScriptParser::DataType::NATIVE;
  611. result.builtin_type = Variant::OBJECT;
  612. result.native_type = first;
  613. } else if (ScriptServer::is_global_class(first)) {
  614. if (GDScript::is_canonically_equal_paths(parser->script_path, ScriptServer::get_global_class_path(first))) {
  615. result = parser->head->get_datatype();
  616. } else {
  617. String path = ScriptServer::get_global_class_path(first);
  618. String ext = path.get_extension();
  619. if (ext == GDScriptLanguage::get_singleton()->get_extension()) {
  620. Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(path);
  621. if (!ref.is_valid() || ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED) != OK) {
  622. push_error(vformat(R"(Could not parse global class "%s" from "%s".)", first, ScriptServer::get_global_class_path(first)), p_type);
  623. return bad_type;
  624. }
  625. result = ref->get_parser()->head->get_datatype();
  626. } else {
  627. result = make_script_meta_type(ResourceLoader::load(path, "Script"));
  628. }
  629. }
  630. } else if (ProjectSettings::get_singleton()->has_autoload(first) && ProjectSettings::get_singleton()->get_autoload(first).is_singleton) {
  631. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(first);
  632. String script_path;
  633. if (ResourceLoader::get_resource_type(autoload.path) == "PackedScene") {
  634. // Try to get script from scene if possible.
  635. if (GDScriptLanguage::get_singleton()->has_any_global_constant(autoload.name)) {
  636. Variant constant = GDScriptLanguage::get_singleton()->get_any_global_constant(autoload.name);
  637. Node *node = Object::cast_to<Node>(constant);
  638. if (node != nullptr) {
  639. Ref<GDScript> scr = node->get_script();
  640. if (scr.is_valid()) {
  641. script_path = scr->get_script_path();
  642. }
  643. }
  644. }
  645. } else if (ResourceLoader::get_resource_type(autoload.path) == "GDScript") {
  646. script_path = autoload.path;
  647. }
  648. if (script_path.is_empty()) {
  649. return bad_type;
  650. }
  651. Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(script_path);
  652. if (ref.is_null()) {
  653. push_error(vformat(R"(The referenced autoload "%s" (from "%s") could not be loaded.)", first, script_path), p_type);
  654. return bad_type;
  655. }
  656. if (ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED) != OK) {
  657. push_error(vformat(R"(Could not parse singleton "%s" from "%s".)", first, script_path), p_type);
  658. return bad_type;
  659. }
  660. result = ref->get_parser()->head->get_datatype();
  661. } else if (ClassDB::has_enum(parser->current_class->base_type.native_type, first)) {
  662. // Native enum in current class.
  663. result = make_native_enum_type(first, parser->current_class->base_type.native_type);
  664. } else if (CoreConstants::is_global_enum(first)) {
  665. if (p_type->type_chain.size() > 1) {
  666. push_error(R"(Enums cannot contain nested types.)", p_type->type_chain[1]);
  667. return bad_type;
  668. }
  669. result = make_global_enum_type(first, StringName());
  670. } else {
  671. // Classes in current scope.
  672. List<GDScriptParser::ClassNode *> script_classes;
  673. bool found = false;
  674. get_class_node_current_scope_classes(parser->current_class, &script_classes);
  675. for (GDScriptParser::ClassNode *script_class : script_classes) {
  676. if (found) {
  677. break;
  678. }
  679. if (script_class->identifier && script_class->identifier->name == first) {
  680. result = script_class->get_datatype();
  681. break;
  682. }
  683. if (script_class->members_indices.has(first)) {
  684. resolve_class_member(script_class, first, p_type);
  685. GDScriptParser::ClassNode::Member member = script_class->get_member(first);
  686. switch (member.type) {
  687. case GDScriptParser::ClassNode::Member::CLASS:
  688. result = member.get_datatype();
  689. found = true;
  690. break;
  691. case GDScriptParser::ClassNode::Member::ENUM:
  692. result = member.get_datatype();
  693. found = true;
  694. break;
  695. case GDScriptParser::ClassNode::Member::CONSTANT:
  696. if (member.get_datatype().is_meta_type) {
  697. result = member.get_datatype();
  698. found = true;
  699. break;
  700. } else if (Ref<Script>(member.constant->initializer->reduced_value).is_valid()) {
  701. Ref<GDScript> gdscript = member.constant->initializer->reduced_value;
  702. if (gdscript.is_valid()) {
  703. Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(gdscript->get_script_path());
  704. if (ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED) != OK) {
  705. push_error(vformat(R"(Could not parse script from "%s".)", gdscript->get_script_path()), p_type);
  706. return bad_type;
  707. }
  708. result = ref->get_parser()->head->get_datatype();
  709. } else {
  710. result = make_script_meta_type(member.constant->initializer->reduced_value);
  711. }
  712. found = true;
  713. break;
  714. }
  715. [[fallthrough]];
  716. default:
  717. push_error(vformat(R"("%s" is a %s but does not contain a type.)", first, member.get_type_name()), p_type);
  718. return bad_type;
  719. }
  720. }
  721. }
  722. }
  723. }
  724. if (!result.is_set()) {
  725. push_error(vformat(R"(Could not find type "%s" in the current scope.)", first), p_type);
  726. return bad_type;
  727. }
  728. if (p_type->type_chain.size() > 1) {
  729. if (result.kind == GDScriptParser::DataType::CLASS) {
  730. for (int i = 1; i < p_type->type_chain.size(); i++) {
  731. GDScriptParser::DataType base = result;
  732. reduce_identifier_from_base(p_type->type_chain[i], &base);
  733. result = p_type->type_chain[i]->get_datatype();
  734. if (!result.is_set()) {
  735. 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]);
  736. return bad_type;
  737. } else if (!result.is_meta_type) {
  738. 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]);
  739. return bad_type;
  740. }
  741. }
  742. } else if (result.kind == GDScriptParser::DataType::NATIVE) {
  743. // Only enums allowed for native.
  744. if (ClassDB::has_enum(result.native_type, p_type->type_chain[1]->name)) {
  745. if (p_type->type_chain.size() > 2) {
  746. push_error(R"(Enums cannot contain nested types.)", p_type->type_chain[2]);
  747. return bad_type;
  748. } else {
  749. result = make_native_enum_type(p_type->type_chain[1]->name, result.native_type);
  750. }
  751. } else {
  752. push_error(vformat(R"(Could not find type "%s" in "%s".)", p_type->type_chain[1]->name, first), p_type->type_chain[1]);
  753. return bad_type;
  754. }
  755. } else {
  756. 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]);
  757. return bad_type;
  758. }
  759. }
  760. if (!p_type->container_types.is_empty()) {
  761. if (result.builtin_type == Variant::ARRAY) {
  762. if (p_type->container_types.size() != 1) {
  763. push_error("Arrays require exactly one collection element type.", p_type);
  764. return bad_type;
  765. }
  766. } else {
  767. push_error("Only arrays can specify collection element types.", p_type);
  768. return bad_type;
  769. }
  770. }
  771. p_type->set_datatype(result);
  772. if (result.kind == GDScriptParser::DataType::CLASS || result.kind == GDScriptParser::DataType::SCRIPT) {
  773. parser->add_dependency(result.script_path);
  774. }
  775. return result;
  776. }
  777. void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class, const StringName &p_name, const GDScriptParser::Node *p_source) {
  778. ERR_FAIL_COND(!p_class->has_member(p_name));
  779. resolve_class_member(p_class, p_class->members_indices[p_name], p_source);
  780. }
  781. void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class, int p_index, const GDScriptParser::Node *p_source) {
  782. ERR_FAIL_INDEX(p_index, p_class->members.size());
  783. GDScriptParser::ClassNode::Member &member = p_class->members.write[p_index];
  784. if (p_source == nullptr && parser->has_class(p_class)) {
  785. p_source = member.get_source_node();
  786. }
  787. if (member.get_datatype().is_resolving()) {
  788. push_error(vformat(R"(Could not resolve member "%s": Cyclic reference.)", member.get_name()), p_source);
  789. return;
  790. }
  791. if (member.get_datatype().is_set()) {
  792. return;
  793. }
  794. if (!parser->has_class(p_class)) {
  795. String script_path = p_class->get_datatype().script_path;
  796. Ref<GDScriptParserRef> parser_ref = parser->get_depended_parser_for(script_path);
  797. if (parser_ref.is_null()) {
  798. push_error(vformat(R"(Could not find script "%s" (While resolving "%s").)", script_path, member.get_name()), p_source);
  799. return;
  800. }
  801. Error err = parser_ref->raise_status(GDScriptParserRef::PARSED);
  802. if (err) {
  803. push_error(vformat(R"(Could not resolve script "%s": %s (While resolving "%s").)", script_path, error_names[err], member.get_name()), p_source);
  804. return;
  805. }
  806. ERR_FAIL_COND_MSG(!parser_ref->get_parser()->has_class(p_class), R"(Parser bug: Mismatched external parser.)");
  807. GDScriptAnalyzer *other_analyzer = parser_ref->get_analyzer();
  808. GDScriptParser *other_parser = parser_ref->get_parser();
  809. int error_count = other_parser->errors.size();
  810. other_analyzer->resolve_class_member(p_class, p_index);
  811. if (other_parser->errors.size() > error_count) {
  812. push_error(vformat(R"(Could not resolve member "%s".)", member.get_name()), p_source);
  813. }
  814. return;
  815. }
  816. // If it's already resolving, that's ok.
  817. if (!p_class->base_type.is_resolving()) {
  818. Error err = resolve_class_inheritance(p_class);
  819. if (err) {
  820. return;
  821. }
  822. }
  823. GDScriptParser::ClassNode *previous_class = parser->current_class;
  824. parser->current_class = p_class;
  825. GDScriptParser::DataType resolving_datatype;
  826. resolving_datatype.kind = GDScriptParser::DataType::RESOLVING;
  827. {
  828. #ifdef DEBUG_ENABLED
  829. GDScriptParser::Node *member_node = member.get_source_node();
  830. if (member_node && member_node->type != GDScriptParser::Node::ANNOTATION) {
  831. // Apply @warning_ignore annotations before resolving member.
  832. for (GDScriptParser::AnnotationNode *&E : member_node->annotations) {
  833. if (E->name == SNAME("@warning_ignore")) {
  834. resolve_annotation(E);
  835. E->apply(parser, member.variable, p_class);
  836. }
  837. }
  838. }
  839. #endif
  840. switch (member.type) {
  841. case GDScriptParser::ClassNode::Member::VARIABLE: {
  842. bool previous_static_context = static_context;
  843. static_context = member.variable->is_static;
  844. check_class_member_name_conflict(p_class, member.variable->identifier->name, member.variable);
  845. member.variable->set_datatype(resolving_datatype);
  846. resolve_variable(member.variable, false);
  847. resolve_pending_lambda_bodies();
  848. // Apply annotations.
  849. for (GDScriptParser::AnnotationNode *&E : member.variable->annotations) {
  850. if (E->name != SNAME("@warning_ignore")) {
  851. resolve_annotation(E);
  852. E->apply(parser, member.variable, p_class);
  853. }
  854. }
  855. static_context = previous_static_context;
  856. #ifdef DEBUG_ENABLED
  857. if (member.variable->exported && member.variable->onready) {
  858. parser->push_warning(member.variable, GDScriptWarning::ONREADY_WITH_EXPORT);
  859. }
  860. if (member.variable->initializer) {
  861. // Check if it is call to get_node() on self (using shorthand $ or not), so we can check if @onready is needed.
  862. // This could be improved by traversing the expression fully and checking the presence of get_node at any level.
  863. if (!member.variable->is_static && !member.variable->onready && member.variable->initializer && (member.variable->initializer->type == GDScriptParser::Node::GET_NODE || member.variable->initializer->type == GDScriptParser::Node::CALL || member.variable->initializer->type == GDScriptParser::Node::CAST)) {
  864. GDScriptParser::Node *expr = member.variable->initializer;
  865. if (expr->type == GDScriptParser::Node::CAST) {
  866. expr = static_cast<GDScriptParser::CastNode *>(expr)->operand;
  867. }
  868. bool is_get_node = expr->type == GDScriptParser::Node::GET_NODE;
  869. bool is_using_shorthand = is_get_node;
  870. if (!is_get_node && expr->type == GDScriptParser::Node::CALL) {
  871. is_using_shorthand = false;
  872. GDScriptParser::CallNode *call = static_cast<GDScriptParser::CallNode *>(expr);
  873. if (call->function_name == SNAME("get_node")) {
  874. switch (call->get_callee_type()) {
  875. case GDScriptParser::Node::IDENTIFIER: {
  876. is_get_node = true;
  877. } break;
  878. case GDScriptParser::Node::SUBSCRIPT: {
  879. GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(call->callee);
  880. is_get_node = subscript->is_attribute && subscript->base->type == GDScriptParser::Node::SELF;
  881. } break;
  882. default:
  883. break;
  884. }
  885. }
  886. }
  887. if (is_get_node) {
  888. parser->push_warning(member.variable, GDScriptWarning::GET_NODE_DEFAULT_WITHOUT_ONREADY, is_using_shorthand ? "$" : "get_node()");
  889. }
  890. }
  891. }
  892. #endif
  893. } break;
  894. case GDScriptParser::ClassNode::Member::CONSTANT: {
  895. check_class_member_name_conflict(p_class, member.constant->identifier->name, member.constant);
  896. member.constant->set_datatype(resolving_datatype);
  897. resolve_constant(member.constant, false);
  898. // Apply annotations.
  899. for (GDScriptParser::AnnotationNode *&E : member.constant->annotations) {
  900. resolve_annotation(E);
  901. E->apply(parser, member.constant, p_class);
  902. }
  903. } break;
  904. case GDScriptParser::ClassNode::Member::SIGNAL: {
  905. check_class_member_name_conflict(p_class, member.signal->identifier->name, member.signal);
  906. member.signal->set_datatype(resolving_datatype);
  907. // This is the _only_ way to declare a signal. Therefore, we can generate its
  908. // MethodInfo inline so it's a tiny bit more efficient.
  909. MethodInfo mi = MethodInfo(member.signal->identifier->name);
  910. for (int j = 0; j < member.signal->parameters.size(); j++) {
  911. GDScriptParser::ParameterNode *param = member.signal->parameters[j];
  912. GDScriptParser::DataType param_type = type_from_metatype(resolve_datatype(param->datatype_specifier));
  913. param->set_datatype(param_type);
  914. #ifdef DEBUG_ENABLED
  915. if (param->datatype_specifier == nullptr) {
  916. parser->push_warning(param, GDScriptWarning::UNTYPED_DECLARATION, "Parameter", param->identifier->name);
  917. }
  918. #endif
  919. mi.arguments.push_back(param_type.to_property_info(param->identifier->name));
  920. // Signals do not support parameter default values.
  921. }
  922. member.signal->set_datatype(make_signal_type(mi));
  923. member.signal->method_info = mi;
  924. // Apply annotations.
  925. for (GDScriptParser::AnnotationNode *&E : member.signal->annotations) {
  926. resolve_annotation(E);
  927. E->apply(parser, member.signal, p_class);
  928. }
  929. } break;
  930. case GDScriptParser::ClassNode::Member::ENUM: {
  931. check_class_member_name_conflict(p_class, member.m_enum->identifier->name, member.m_enum);
  932. member.m_enum->set_datatype(resolving_datatype);
  933. GDScriptParser::DataType enum_type = make_enum_type(member.m_enum->identifier->name, p_class->fqcn, true);
  934. const GDScriptParser::EnumNode *prev_enum = current_enum;
  935. current_enum = member.m_enum;
  936. Dictionary dictionary;
  937. for (int j = 0; j < member.m_enum->values.size(); j++) {
  938. GDScriptParser::EnumNode::Value &element = member.m_enum->values.write[j];
  939. if (element.custom_value) {
  940. reduce_expression(element.custom_value);
  941. if (!element.custom_value->is_constant) {
  942. push_error(R"(Enum values must be constant.)", element.custom_value);
  943. } else if (element.custom_value->reduced_value.get_type() != Variant::INT) {
  944. push_error(R"(Enum values must be integers.)", element.custom_value);
  945. } else {
  946. element.value = element.custom_value->reduced_value;
  947. element.resolved = true;
  948. }
  949. } else {
  950. if (element.index > 0) {
  951. element.value = element.parent_enum->values[element.index - 1].value + 1;
  952. } else {
  953. element.value = 0;
  954. }
  955. element.resolved = true;
  956. }
  957. enum_type.enum_values[element.identifier->name] = element.value;
  958. dictionary[String(element.identifier->name)] = element.value;
  959. #ifdef DEBUG_ENABLED
  960. // Named enum identifiers do not shadow anything since you can only access them with `NamedEnum.ENUM_VALUE`.
  961. if (member.m_enum->identifier->name == StringName()) {
  962. is_shadowing(element.identifier, "enum member", false);
  963. }
  964. #endif
  965. }
  966. current_enum = prev_enum;
  967. dictionary.make_read_only();
  968. member.m_enum->set_datatype(enum_type);
  969. member.m_enum->dictionary = dictionary;
  970. // Apply annotations.
  971. for (GDScriptParser::AnnotationNode *&E : member.m_enum->annotations) {
  972. resolve_annotation(E);
  973. E->apply(parser, member.m_enum, p_class);
  974. }
  975. } break;
  976. case GDScriptParser::ClassNode::Member::FUNCTION:
  977. for (GDScriptParser::AnnotationNode *&E : member.function->annotations) {
  978. resolve_annotation(E);
  979. E->apply(parser, member.function, p_class);
  980. }
  981. resolve_function_signature(member.function, p_source);
  982. break;
  983. case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
  984. member.enum_value.identifier->set_datatype(resolving_datatype);
  985. if (member.enum_value.custom_value) {
  986. check_class_member_name_conflict(p_class, member.enum_value.identifier->name, member.enum_value.custom_value);
  987. const GDScriptParser::EnumNode *prev_enum = current_enum;
  988. current_enum = member.enum_value.parent_enum;
  989. reduce_expression(member.enum_value.custom_value);
  990. current_enum = prev_enum;
  991. if (!member.enum_value.custom_value->is_constant) {
  992. push_error(R"(Enum values must be constant.)", member.enum_value.custom_value);
  993. } else if (member.enum_value.custom_value->reduced_value.get_type() != Variant::INT) {
  994. push_error(R"(Enum values must be integers.)", member.enum_value.custom_value);
  995. } else {
  996. member.enum_value.value = member.enum_value.custom_value->reduced_value;
  997. member.enum_value.resolved = true;
  998. }
  999. } else {
  1000. check_class_member_name_conflict(p_class, member.enum_value.identifier->name, member.enum_value.parent_enum);
  1001. if (member.enum_value.index > 0) {
  1002. const GDScriptParser::EnumNode::Value &prev_value = member.enum_value.parent_enum->values[member.enum_value.index - 1];
  1003. resolve_class_member(p_class, prev_value.identifier->name, member.enum_value.identifier);
  1004. member.enum_value.value = prev_value.value + 1;
  1005. } else {
  1006. member.enum_value.value = 0;
  1007. }
  1008. member.enum_value.resolved = true;
  1009. }
  1010. // Also update the original references.
  1011. member.enum_value.parent_enum->values.set(member.enum_value.index, member.enum_value);
  1012. member.enum_value.identifier->set_datatype(make_enum_type(UNNAMED_ENUM, p_class->fqcn, false));
  1013. } break;
  1014. case GDScriptParser::ClassNode::Member::CLASS:
  1015. check_class_member_name_conflict(p_class, member.m_class->identifier->name, member.m_class);
  1016. // If it's already resolving, that's ok.
  1017. if (!member.m_class->base_type.is_resolving()) {
  1018. resolve_class_inheritance(member.m_class, p_source);
  1019. }
  1020. break;
  1021. case GDScriptParser::ClassNode::Member::GROUP:
  1022. // No-op, but needed to silence warnings.
  1023. break;
  1024. case GDScriptParser::ClassNode::Member::UNDEFINED:
  1025. ERR_PRINT("Trying to resolve undefined member.");
  1026. break;
  1027. }
  1028. }
  1029. parser->current_class = previous_class;
  1030. }
  1031. void GDScriptAnalyzer::resolve_class_interface(GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_source) {
  1032. if (p_source == nullptr && parser->has_class(p_class)) {
  1033. p_source = p_class;
  1034. }
  1035. if (!p_class->resolved_interface) {
  1036. #ifdef DEBUG_ENABLED
  1037. bool has_static_data = p_class->has_static_data;
  1038. #endif
  1039. if (!parser->has_class(p_class)) {
  1040. String script_path = p_class->get_datatype().script_path;
  1041. Ref<GDScriptParserRef> parser_ref = parser->get_depended_parser_for(script_path);
  1042. if (parser_ref.is_null()) {
  1043. push_error(vformat(R"(Could not find script "%s".)", script_path), p_source);
  1044. return;
  1045. }
  1046. Error err = parser_ref->raise_status(GDScriptParserRef::PARSED);
  1047. if (err) {
  1048. push_error(vformat(R"(Could not resolve script "%s": %s.)", script_path, error_names[err]), p_source);
  1049. return;
  1050. }
  1051. ERR_FAIL_COND_MSG(!parser_ref->get_parser()->has_class(p_class), R"(Parser bug: Mismatched external parser.)");
  1052. GDScriptAnalyzer *other_analyzer = parser_ref->get_analyzer();
  1053. GDScriptParser *other_parser = parser_ref->get_parser();
  1054. int error_count = other_parser->errors.size();
  1055. other_analyzer->resolve_class_interface(p_class);
  1056. if (other_parser->errors.size() > error_count) {
  1057. push_error(vformat(R"(Could not resolve class "%s".)", p_class->fqcn), p_source);
  1058. }
  1059. return;
  1060. }
  1061. p_class->resolved_interface = true;
  1062. if (resolve_class_inheritance(p_class) != OK) {
  1063. return;
  1064. }
  1065. GDScriptParser::DataType base_type = p_class->base_type;
  1066. if (base_type.kind == GDScriptParser::DataType::CLASS) {
  1067. GDScriptParser::ClassNode *base_class = base_type.class_type;
  1068. resolve_class_interface(base_class, p_class);
  1069. }
  1070. for (int i = 0; i < p_class->members.size(); i++) {
  1071. resolve_class_member(p_class, i);
  1072. #ifdef DEBUG_ENABLED
  1073. if (!has_static_data) {
  1074. GDScriptParser::ClassNode::Member member = p_class->members[i];
  1075. if (member.type == GDScriptParser::ClassNode::Member::CLASS) {
  1076. has_static_data = member.m_class->has_static_data;
  1077. }
  1078. }
  1079. #endif
  1080. }
  1081. #ifdef DEBUG_ENABLED
  1082. if (!has_static_data && p_class->annotated_static_unload) {
  1083. GDScriptParser::Node *static_unload = nullptr;
  1084. for (GDScriptParser::AnnotationNode *node : p_class->annotations) {
  1085. if (node->name == "@static_unload") {
  1086. static_unload = node;
  1087. break;
  1088. }
  1089. }
  1090. parser->push_warning(static_unload ? static_unload : p_class, GDScriptWarning::REDUNDANT_STATIC_UNLOAD);
  1091. }
  1092. #endif
  1093. }
  1094. }
  1095. void GDScriptAnalyzer::resolve_class_interface(GDScriptParser::ClassNode *p_class, bool p_recursive) {
  1096. resolve_class_interface(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_interface(member.m_class, true);
  1102. }
  1103. }
  1104. }
  1105. }
  1106. void GDScriptAnalyzer::resolve_class_body(GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_source) {
  1107. if (p_source == nullptr && parser->has_class(p_class)) {
  1108. p_source = p_class;
  1109. }
  1110. if (p_class->resolved_body) {
  1111. return;
  1112. }
  1113. if (!parser->has_class(p_class)) {
  1114. String script_path = p_class->get_datatype().script_path;
  1115. Ref<GDScriptParserRef> parser_ref = parser->get_depended_parser_for(script_path);
  1116. if (parser_ref.is_null()) {
  1117. push_error(vformat(R"(Could not find script "%s".)", script_path), p_source);
  1118. return;
  1119. }
  1120. Error err = parser_ref->raise_status(GDScriptParserRef::PARSED);
  1121. if (err) {
  1122. push_error(vformat(R"(Could not resolve script "%s": %s.)", script_path, error_names[err]), p_source);
  1123. return;
  1124. }
  1125. ERR_FAIL_COND_MSG(!parser_ref->get_parser()->has_class(p_class), R"(Parser bug: Mismatched external parser.)");
  1126. GDScriptAnalyzer *other_analyzer = parser_ref->get_analyzer();
  1127. GDScriptParser *other_parser = parser_ref->get_parser();
  1128. int error_count = other_parser->errors.size();
  1129. other_analyzer->resolve_class_body(p_class);
  1130. if (other_parser->errors.size() > error_count) {
  1131. push_error(vformat(R"(Could not resolve class "%s".)", p_class->fqcn), p_source);
  1132. }
  1133. return;
  1134. }
  1135. p_class->resolved_body = true;
  1136. GDScriptParser::ClassNode *previous_class = parser->current_class;
  1137. parser->current_class = p_class;
  1138. resolve_class_interface(p_class, p_source);
  1139. GDScriptParser::DataType base_type = p_class->base_type;
  1140. if (base_type.kind == GDScriptParser::DataType::CLASS) {
  1141. GDScriptParser::ClassNode *base_class = base_type.class_type;
  1142. resolve_class_body(base_class, p_class);
  1143. }
  1144. // Do functions, properties, and groups now.
  1145. for (int i = 0; i < p_class->members.size(); i++) {
  1146. GDScriptParser::ClassNode::Member member = p_class->members[i];
  1147. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
  1148. // Apply annotations.
  1149. for (GDScriptParser::AnnotationNode *&E : member.function->annotations) {
  1150. resolve_annotation(E);
  1151. E->apply(parser, member.function, p_class);
  1152. }
  1153. resolve_function_body(member.function);
  1154. } else if (member.type == GDScriptParser::ClassNode::Member::VARIABLE && member.variable->property != GDScriptParser::VariableNode::PROP_NONE) {
  1155. if (member.variable->property == GDScriptParser::VariableNode::PROP_INLINE) {
  1156. if (member.variable->getter != nullptr) {
  1157. member.variable->getter->return_type = member.variable->datatype_specifier;
  1158. member.variable->getter->set_datatype(member.get_datatype());
  1159. resolve_function_body(member.variable->getter);
  1160. }
  1161. if (member.variable->setter != nullptr) {
  1162. ERR_CONTINUE(member.variable->setter->parameters.is_empty());
  1163. member.variable->setter->parameters[0]->datatype_specifier = member.variable->datatype_specifier;
  1164. member.variable->setter->parameters[0]->set_datatype(member.get_datatype());
  1165. resolve_function_body(member.variable->setter);
  1166. }
  1167. }
  1168. } else if (member.type == GDScriptParser::ClassNode::Member::GROUP) {
  1169. // Apply annotation (`@export_{category,group,subgroup}`).
  1170. resolve_annotation(member.annotation);
  1171. member.annotation->apply(parser, nullptr, p_class);
  1172. }
  1173. }
  1174. // Check unused variables and datatypes of property getters and setters.
  1175. for (int i = 0; i < p_class->members.size(); i++) {
  1176. GDScriptParser::ClassNode::Member member = p_class->members[i];
  1177. if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) {
  1178. #ifdef DEBUG_ENABLED
  1179. if (member.variable->usages == 0 && String(member.variable->identifier->name).begins_with("_")) {
  1180. parser->push_warning(member.variable->identifier, GDScriptWarning::UNUSED_PRIVATE_CLASS_VARIABLE, member.variable->identifier->name);
  1181. }
  1182. #endif
  1183. if (member.variable->property == GDScriptParser::VariableNode::PROP_SETGET) {
  1184. GDScriptParser::FunctionNode *getter_function = nullptr;
  1185. GDScriptParser::FunctionNode *setter_function = nullptr;
  1186. bool has_valid_getter = false;
  1187. bool has_valid_setter = false;
  1188. if (member.variable->getter_pointer != nullptr) {
  1189. if (p_class->has_function(member.variable->getter_pointer->name)) {
  1190. getter_function = p_class->get_member(member.variable->getter_pointer->name).function;
  1191. }
  1192. if (getter_function == nullptr) {
  1193. push_error(vformat(R"(Getter "%s" not found.)", member.variable->getter_pointer->name), member.variable);
  1194. } else {
  1195. GDScriptParser::DataType return_datatype = getter_function->datatype;
  1196. if (getter_function->return_type != nullptr) {
  1197. return_datatype = getter_function->return_type->datatype;
  1198. return_datatype.is_meta_type = false;
  1199. }
  1200. if (getter_function->parameters.size() != 0 || return_datatype.has_no_type()) {
  1201. push_error(vformat(R"(Function "%s" cannot be used as getter because of its signature.)", getter_function->identifier->name), member.variable);
  1202. } else if (!is_type_compatible(member.variable->datatype, return_datatype, true)) {
  1203. 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);
  1204. } else {
  1205. has_valid_getter = true;
  1206. #ifdef DEBUG_ENABLED
  1207. if (member.variable->datatype.builtin_type == Variant::INT && return_datatype.builtin_type == Variant::FLOAT) {
  1208. parser->push_warning(member.variable, GDScriptWarning::NARROWING_CONVERSION);
  1209. }
  1210. #endif
  1211. }
  1212. }
  1213. }
  1214. if (member.variable->setter_pointer != nullptr) {
  1215. if (p_class->has_function(member.variable->setter_pointer->name)) {
  1216. setter_function = p_class->get_member(member.variable->setter_pointer->name).function;
  1217. }
  1218. if (setter_function == nullptr) {
  1219. push_error(vformat(R"(Setter "%s" not found.)", member.variable->setter_pointer->name), member.variable);
  1220. } else if (setter_function->parameters.size() != 1) {
  1221. push_error(vformat(R"(Function "%s" cannot be used as setter because of its signature.)", setter_function->identifier->name), member.variable);
  1222. } else if (!is_type_compatible(member.variable->datatype, setter_function->parameters[0]->datatype, true)) {
  1223. 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);
  1224. } else {
  1225. has_valid_setter = true;
  1226. #ifdef DEBUG_ENABLED
  1227. if (member.variable->datatype.builtin_type == Variant::FLOAT && setter_function->parameters[0]->datatype.builtin_type == Variant::INT) {
  1228. parser->push_warning(member.variable, GDScriptWarning::NARROWING_CONVERSION);
  1229. }
  1230. #endif
  1231. }
  1232. }
  1233. if (member.variable->datatype.is_variant() && has_valid_getter && has_valid_setter) {
  1234. if (!is_type_compatible(getter_function->datatype, setter_function->parameters[0]->datatype, true)) {
  1235. 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);
  1236. }
  1237. }
  1238. }
  1239. } else if (member.type == GDScriptParser::ClassNode::Member::SIGNAL) {
  1240. #ifdef DEBUG_ENABLED
  1241. if (member.signal->usages == 0) {
  1242. parser->push_warning(member.signal->identifier, GDScriptWarning::UNUSED_SIGNAL, member.signal->identifier->name);
  1243. }
  1244. #endif
  1245. }
  1246. }
  1247. if (!pending_body_resolution_lambdas.is_empty()) {
  1248. ERR_PRINT("GDScript bug (please report): Not all pending lambda bodies were resolved in time.");
  1249. resolve_pending_lambda_bodies();
  1250. }
  1251. parser->current_class = previous_class;
  1252. }
  1253. void GDScriptAnalyzer::resolve_class_body(GDScriptParser::ClassNode *p_class, bool p_recursive) {
  1254. resolve_class_body(p_class);
  1255. if (p_recursive) {
  1256. for (int i = 0; i < p_class->members.size(); i++) {
  1257. GDScriptParser::ClassNode::Member member = p_class->members[i];
  1258. if (member.type == GDScriptParser::ClassNode::Member::CLASS) {
  1259. resolve_class_body(member.m_class, true);
  1260. }
  1261. }
  1262. }
  1263. }
  1264. void GDScriptAnalyzer::resolve_node(GDScriptParser::Node *p_node, bool p_is_root) {
  1265. ERR_FAIL_NULL_MSG(p_node, "Trying to resolve type of a null node.");
  1266. switch (p_node->type) {
  1267. case GDScriptParser::Node::NONE:
  1268. break; // Unreachable.
  1269. case GDScriptParser::Node::CLASS:
  1270. // NOTE: Currently this route is never executed, `resolve_class_*()` is called directly.
  1271. if (OK == resolve_class_inheritance(static_cast<GDScriptParser::ClassNode *>(p_node), true)) {
  1272. resolve_class_interface(static_cast<GDScriptParser::ClassNode *>(p_node), true);
  1273. resolve_class_body(static_cast<GDScriptParser::ClassNode *>(p_node), true);
  1274. }
  1275. break;
  1276. case GDScriptParser::Node::CONSTANT:
  1277. resolve_constant(static_cast<GDScriptParser::ConstantNode *>(p_node), true);
  1278. break;
  1279. case GDScriptParser::Node::FOR:
  1280. resolve_for(static_cast<GDScriptParser::ForNode *>(p_node));
  1281. break;
  1282. case GDScriptParser::Node::IF:
  1283. resolve_if(static_cast<GDScriptParser::IfNode *>(p_node));
  1284. break;
  1285. case GDScriptParser::Node::SUITE:
  1286. resolve_suite(static_cast<GDScriptParser::SuiteNode *>(p_node));
  1287. break;
  1288. case GDScriptParser::Node::VARIABLE:
  1289. resolve_variable(static_cast<GDScriptParser::VariableNode *>(p_node), true);
  1290. break;
  1291. case GDScriptParser::Node::WHILE:
  1292. resolve_while(static_cast<GDScriptParser::WhileNode *>(p_node));
  1293. break;
  1294. case GDScriptParser::Node::ANNOTATION:
  1295. resolve_annotation(static_cast<GDScriptParser::AnnotationNode *>(p_node));
  1296. break;
  1297. case GDScriptParser::Node::ASSERT:
  1298. resolve_assert(static_cast<GDScriptParser::AssertNode *>(p_node));
  1299. break;
  1300. case GDScriptParser::Node::MATCH:
  1301. resolve_match(static_cast<GDScriptParser::MatchNode *>(p_node));
  1302. break;
  1303. case GDScriptParser::Node::MATCH_BRANCH:
  1304. resolve_match_branch(static_cast<GDScriptParser::MatchBranchNode *>(p_node), nullptr);
  1305. break;
  1306. case GDScriptParser::Node::PARAMETER:
  1307. resolve_parameter(static_cast<GDScriptParser::ParameterNode *>(p_node));
  1308. break;
  1309. case GDScriptParser::Node::PATTERN:
  1310. resolve_match_pattern(static_cast<GDScriptParser::PatternNode *>(p_node), nullptr);
  1311. break;
  1312. case GDScriptParser::Node::RETURN:
  1313. resolve_return(static_cast<GDScriptParser::ReturnNode *>(p_node));
  1314. break;
  1315. case GDScriptParser::Node::TYPE:
  1316. resolve_datatype(static_cast<GDScriptParser::TypeNode *>(p_node));
  1317. break;
  1318. // Resolving expression is the same as reducing them.
  1319. case GDScriptParser::Node::ARRAY:
  1320. case GDScriptParser::Node::ASSIGNMENT:
  1321. case GDScriptParser::Node::AWAIT:
  1322. case GDScriptParser::Node::BINARY_OPERATOR:
  1323. case GDScriptParser::Node::CALL:
  1324. case GDScriptParser::Node::CAST:
  1325. case GDScriptParser::Node::DICTIONARY:
  1326. case GDScriptParser::Node::GET_NODE:
  1327. case GDScriptParser::Node::IDENTIFIER:
  1328. case GDScriptParser::Node::LAMBDA:
  1329. case GDScriptParser::Node::LITERAL:
  1330. case GDScriptParser::Node::PRELOAD:
  1331. case GDScriptParser::Node::SELF:
  1332. case GDScriptParser::Node::SUBSCRIPT:
  1333. case GDScriptParser::Node::TERNARY_OPERATOR:
  1334. case GDScriptParser::Node::TYPE_TEST:
  1335. case GDScriptParser::Node::UNARY_OPERATOR:
  1336. reduce_expression(static_cast<GDScriptParser::ExpressionNode *>(p_node), p_is_root);
  1337. break;
  1338. case GDScriptParser::Node::BREAK:
  1339. case GDScriptParser::Node::BREAKPOINT:
  1340. case GDScriptParser::Node::CONTINUE:
  1341. case GDScriptParser::Node::ENUM:
  1342. case GDScriptParser::Node::FUNCTION:
  1343. case GDScriptParser::Node::PASS:
  1344. case GDScriptParser::Node::SIGNAL:
  1345. // Nothing to do.
  1346. break;
  1347. }
  1348. }
  1349. void GDScriptAnalyzer::resolve_annotation(GDScriptParser::AnnotationNode *p_annotation) {
  1350. ERR_FAIL_COND_MSG(!parser->valid_annotations.has(p_annotation->name), vformat(R"(Annotation "%s" not found to validate.)", p_annotation->name));
  1351. if (p_annotation->is_resolved) {
  1352. return;
  1353. }
  1354. p_annotation->is_resolved = true;
  1355. const MethodInfo &annotation_info = parser->valid_annotations[p_annotation->name].info;
  1356. const List<PropertyInfo>::Element *E = annotation_info.arguments.front();
  1357. for (int i = 0; i < p_annotation->arguments.size(); i++) {
  1358. GDScriptParser::ExpressionNode *argument = p_annotation->arguments[i];
  1359. const PropertyInfo &argument_info = E->get();
  1360. if (E->next() != nullptr) {
  1361. E = E->next();
  1362. }
  1363. reduce_expression(argument);
  1364. if (!argument->is_constant) {
  1365. push_error(vformat(R"(Argument %d of annotation "%s" isn't a constant expression.)", i + 1, p_annotation->name), argument);
  1366. return;
  1367. }
  1368. Variant value = argument->reduced_value;
  1369. if (value.get_type() != argument_info.type) {
  1370. #ifdef DEBUG_ENABLED
  1371. if (argument_info.type == Variant::INT && value.get_type() == Variant::FLOAT) {
  1372. parser->push_warning(argument, GDScriptWarning::NARROWING_CONVERSION);
  1373. }
  1374. #endif
  1375. if (!Variant::can_convert_strict(value.get_type(), argument_info.type)) {
  1376. push_error(vformat(R"(Invalid argument for annotation "%s": argument %d should be "%s" but is "%s".)", p_annotation->name, i + 1, Variant::get_type_name(argument_info.type), argument->get_datatype().to_string()), argument);
  1377. return;
  1378. }
  1379. Variant converted_to;
  1380. const Variant *converted_from = &value;
  1381. Callable::CallError call_error;
  1382. Variant::construct(argument_info.type, converted_to, &converted_from, 1, call_error);
  1383. if (call_error.error != Callable::CallError::CALL_OK) {
  1384. push_error(vformat(R"(Cannot convert argument %d of annotation "%s" from "%s" to "%s".)", i + 1, p_annotation->name, Variant::get_type_name(value.get_type()), Variant::get_type_name(argument_info.type)), argument);
  1385. return;
  1386. }
  1387. value = converted_to;
  1388. }
  1389. p_annotation->resolved_arguments.push_back(value);
  1390. }
  1391. }
  1392. void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode *p_function, const GDScriptParser::Node *p_source, bool p_is_lambda) {
  1393. if (p_source == nullptr) {
  1394. p_source = p_function;
  1395. }
  1396. StringName function_name = p_function->identifier != nullptr ? p_function->identifier->name : StringName();
  1397. if (p_function->get_datatype().is_resolving()) {
  1398. push_error(vformat(R"(Could not resolve function "%s": Cyclic reference.)", function_name), p_source);
  1399. return;
  1400. }
  1401. if (p_function->resolved_signature) {
  1402. return;
  1403. }
  1404. p_function->resolved_signature = true;
  1405. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  1406. parser->current_function = p_function;
  1407. bool previous_static_context = static_context;
  1408. if (p_is_lambda) {
  1409. // For lambdas this is determined from the context, the `static` keyword is not allowed.
  1410. p_function->is_static = static_context;
  1411. } else {
  1412. // For normal functions, this is determined in the parser by the `static` keyword.
  1413. static_context = p_function->is_static;
  1414. }
  1415. GDScriptParser::DataType prev_datatype = p_function->get_datatype();
  1416. GDScriptParser::DataType resolving_datatype;
  1417. resolving_datatype.kind = GDScriptParser::DataType::RESOLVING;
  1418. p_function->set_datatype(resolving_datatype);
  1419. #ifdef TOOLS_ENABLED
  1420. int default_value_count = 0;
  1421. #endif // TOOLS_ENABLED
  1422. #ifdef DEBUG_ENABLED
  1423. String function_visible_name = function_name;
  1424. if (function_name == StringName()) {
  1425. function_visible_name = p_is_lambda ? "<anonymous lambda>" : "<unknown function>";
  1426. }
  1427. #endif
  1428. for (int i = 0; i < p_function->parameters.size(); i++) {
  1429. resolve_parameter(p_function->parameters[i]);
  1430. #ifdef DEBUG_ENABLED
  1431. if (p_function->parameters[i]->usages == 0 && !String(p_function->parameters[i]->identifier->name).begins_with("_")) {
  1432. parser->push_warning(p_function->parameters[i]->identifier, GDScriptWarning::UNUSED_PARAMETER, function_visible_name, p_function->parameters[i]->identifier->name);
  1433. }
  1434. is_shadowing(p_function->parameters[i]->identifier, "function parameter", true);
  1435. #endif // DEBUG_ENABLED
  1436. if (p_function->parameters[i]->initializer) {
  1437. #ifdef TOOLS_ENABLED
  1438. default_value_count++;
  1439. #endif // TOOLS_ENABLED
  1440. if (p_function->parameters[i]->initializer->is_constant) {
  1441. p_function->default_arg_values.push_back(p_function->parameters[i]->initializer->reduced_value);
  1442. } else {
  1443. p_function->default_arg_values.push_back(Variant()); // Prevent shift.
  1444. }
  1445. }
  1446. }
  1447. if (!p_is_lambda && function_name == GDScriptLanguage::get_singleton()->strings._init) {
  1448. // Constructor.
  1449. GDScriptParser::DataType return_type = parser->current_class->get_datatype();
  1450. return_type.is_meta_type = false;
  1451. p_function->set_datatype(return_type);
  1452. if (p_function->return_type) {
  1453. GDScriptParser::DataType declared_return = resolve_datatype(p_function->return_type);
  1454. if (declared_return.kind != GDScriptParser::DataType::BUILTIN || declared_return.builtin_type != Variant::NIL) {
  1455. push_error("Constructor cannot have an explicit return type.", p_function->return_type);
  1456. }
  1457. }
  1458. } else if (!p_is_lambda && function_name == GDScriptLanguage::get_singleton()->strings._static_init) {
  1459. // Static constructor.
  1460. GDScriptParser::DataType return_type;
  1461. return_type.kind = GDScriptParser::DataType::BUILTIN;
  1462. return_type.builtin_type = Variant::NIL;
  1463. p_function->set_datatype(return_type);
  1464. if (p_function->return_type) {
  1465. GDScriptParser::DataType declared_return = resolve_datatype(p_function->return_type);
  1466. if (declared_return.kind != GDScriptParser::DataType::BUILTIN || declared_return.builtin_type != Variant::NIL) {
  1467. push_error("Static constructor cannot have an explicit return type.", p_function->return_type);
  1468. }
  1469. }
  1470. } else {
  1471. if (p_function->return_type != nullptr) {
  1472. p_function->set_datatype(type_from_metatype(resolve_datatype(p_function->return_type)));
  1473. } else {
  1474. // In case the function is not typed, we can safely assume it's a Variant, so it's okay to mark as "inferred" here.
  1475. // It's not "undetected" to not mix up with unknown functions.
  1476. GDScriptParser::DataType return_type;
  1477. return_type.type_source = GDScriptParser::DataType::INFERRED;
  1478. return_type.kind = GDScriptParser::DataType::VARIANT;
  1479. p_function->set_datatype(return_type);
  1480. }
  1481. #ifdef TOOLS_ENABLED
  1482. // Check if the function signature matches the parent. If not it's an error since it breaks polymorphism.
  1483. // Not for the constructor which can vary in signature.
  1484. GDScriptParser::DataType base_type = parser->current_class->base_type;
  1485. base_type.is_meta_type = false;
  1486. GDScriptParser::DataType parent_return_type;
  1487. List<GDScriptParser::DataType> parameters_types;
  1488. int default_par_count = 0;
  1489. BitField<MethodFlags> method_flags;
  1490. StringName native_base;
  1491. if (!p_is_lambda && get_function_signature(p_function, false, base_type, function_name, parent_return_type, parameters_types, default_par_count, method_flags, &native_base)) {
  1492. bool valid = p_function->is_static == method_flags.has_flag(METHOD_FLAG_STATIC);
  1493. if (p_function->return_type != nullptr) {
  1494. // Check return type covariance.
  1495. GDScriptParser::DataType return_type = p_function->get_datatype();
  1496. if (return_type.is_variant()) {
  1497. // `is_type_compatible()` returns `true` if one of the types is `Variant`.
  1498. // Don't allow an explicitly specified `Variant` if the parent return type is narrower.
  1499. valid = valid && parent_return_type.is_variant();
  1500. } else if (return_type.kind == GDScriptParser::DataType::BUILTIN && return_type.builtin_type == Variant::NIL) {
  1501. // `is_type_compatible()` returns `true` if target is an `Object` and source is `null`.
  1502. // Don't allow `void` if the parent return type is a hard non-`void` type.
  1503. if (parent_return_type.is_hard_type() && !(parent_return_type.kind == GDScriptParser::DataType::BUILTIN && parent_return_type.builtin_type == Variant::NIL)) {
  1504. valid = false;
  1505. }
  1506. } else {
  1507. valid = valid && is_type_compatible(parent_return_type, return_type);
  1508. }
  1509. }
  1510. int par_count_diff = p_function->parameters.size() - parameters_types.size();
  1511. valid = valid && par_count_diff >= 0;
  1512. valid = valid && default_value_count >= default_par_count + par_count_diff;
  1513. if (valid) {
  1514. int i = 0;
  1515. for (const GDScriptParser::DataType &parent_par_type : parameters_types) {
  1516. // Check parameter type contravariance.
  1517. GDScriptParser::DataType current_par_type = p_function->parameters[i++]->get_datatype();
  1518. if (parent_par_type.is_variant() && parent_par_type.is_hard_type()) {
  1519. // `is_type_compatible()` returns `true` if one of the types is `Variant`.
  1520. // Don't allow narrowing a hard `Variant`.
  1521. valid = valid && current_par_type.is_variant();
  1522. } else {
  1523. valid = valid && is_type_compatible(current_par_type, parent_par_type);
  1524. }
  1525. }
  1526. }
  1527. if (!valid) {
  1528. // Compute parent signature as a string to show in the error message.
  1529. String parent_signature = String(function_name) + "(";
  1530. int j = 0;
  1531. for (const GDScriptParser::DataType &par_type : parameters_types) {
  1532. if (j > 0) {
  1533. parent_signature += ", ";
  1534. }
  1535. String parameter = par_type.to_string();
  1536. if (parameter == "null") {
  1537. parameter = "Variant";
  1538. }
  1539. parent_signature += parameter;
  1540. if (j >= parameters_types.size() - default_par_count) {
  1541. parent_signature += " = <default>";
  1542. }
  1543. j++;
  1544. }
  1545. parent_signature += ") -> ";
  1546. const String return_type = parent_return_type.to_string_strict();
  1547. if (return_type == "null") {
  1548. parent_signature += "void";
  1549. } else {
  1550. parent_signature += return_type;
  1551. }
  1552. push_error(vformat(R"(The function signature doesn't match the parent. Parent signature is "%s".)", parent_signature), p_function);
  1553. }
  1554. #ifdef DEBUG_ENABLED
  1555. if (native_base != StringName()) {
  1556. parser->push_warning(p_function, GDScriptWarning::NATIVE_METHOD_OVERRIDE, function_name, native_base);
  1557. }
  1558. #endif
  1559. }
  1560. #endif // TOOLS_ENABLED
  1561. }
  1562. #ifdef DEBUG_ENABLED
  1563. if (p_function->return_type == nullptr) {
  1564. parser->push_warning(p_function, GDScriptWarning::UNTYPED_DECLARATION, "Function", function_visible_name);
  1565. }
  1566. #endif
  1567. if (p_function->get_datatype().is_resolving()) {
  1568. p_function->set_datatype(prev_datatype);
  1569. }
  1570. parser->current_function = previous_function;
  1571. static_context = previous_static_context;
  1572. }
  1573. void GDScriptAnalyzer::resolve_function_body(GDScriptParser::FunctionNode *p_function, bool p_is_lambda) {
  1574. if (p_function->resolved_body) {
  1575. return;
  1576. }
  1577. p_function->resolved_body = true;
  1578. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  1579. parser->current_function = p_function;
  1580. bool previous_static_context = static_context;
  1581. static_context = p_function->is_static;
  1582. resolve_suite(p_function->body);
  1583. if (!p_function->get_datatype().is_hard_type() && p_function->body->get_datatype().is_set()) {
  1584. // Use the suite inferred type if return isn't explicitly set.
  1585. p_function->set_datatype(p_function->body->get_datatype());
  1586. } 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)) {
  1587. if (!p_function->body->has_return && (p_is_lambda || p_function->identifier->name != GDScriptLanguage::get_singleton()->strings._init)) {
  1588. push_error(R"(Not all code paths return a value.)", p_function);
  1589. }
  1590. }
  1591. parser->current_function = previous_function;
  1592. static_context = previous_static_context;
  1593. }
  1594. void GDScriptAnalyzer::decide_suite_type(GDScriptParser::Node *p_suite, GDScriptParser::Node *p_statement) {
  1595. if (p_statement == nullptr) {
  1596. return;
  1597. }
  1598. switch (p_statement->type) {
  1599. case GDScriptParser::Node::IF:
  1600. case GDScriptParser::Node::FOR:
  1601. case GDScriptParser::Node::MATCH:
  1602. case GDScriptParser::Node::PATTERN:
  1603. case GDScriptParser::Node::RETURN:
  1604. case GDScriptParser::Node::WHILE:
  1605. // Use return or nested suite type as this suite type.
  1606. if (p_suite->get_datatype().is_set() && (p_suite->get_datatype() != p_statement->get_datatype())) {
  1607. // Mixed types.
  1608. // TODO: This could use the common supertype instead.
  1609. p_suite->datatype.kind = GDScriptParser::DataType::VARIANT;
  1610. p_suite->datatype.type_source = GDScriptParser::DataType::UNDETECTED;
  1611. } else {
  1612. p_suite->set_datatype(p_statement->get_datatype());
  1613. p_suite->datatype.type_source = GDScriptParser::DataType::INFERRED;
  1614. }
  1615. break;
  1616. default:
  1617. break;
  1618. }
  1619. }
  1620. void GDScriptAnalyzer::resolve_suite(GDScriptParser::SuiteNode *p_suite) {
  1621. for (int i = 0; i < p_suite->statements.size(); i++) {
  1622. GDScriptParser::Node *stmt = p_suite->statements[i];
  1623. // Apply annotations.
  1624. for (GDScriptParser::AnnotationNode *&E : stmt->annotations) {
  1625. resolve_annotation(E);
  1626. E->apply(parser, stmt, nullptr); // TODO: Provide `p_class`.
  1627. }
  1628. resolve_node(stmt);
  1629. resolve_pending_lambda_bodies();
  1630. decide_suite_type(p_suite, stmt);
  1631. }
  1632. }
  1633. void GDScriptAnalyzer::resolve_assignable(GDScriptParser::AssignableNode *p_assignable, const char *p_kind) {
  1634. GDScriptParser::DataType type;
  1635. type.kind = GDScriptParser::DataType::VARIANT;
  1636. bool is_constant = p_assignable->type == GDScriptParser::Node::CONSTANT;
  1637. #ifdef DEBUG_ENABLED
  1638. if (p_assignable->identifier != nullptr && p_assignable->identifier->suite != nullptr && p_assignable->identifier->suite->parent_block != nullptr) {
  1639. if (p_assignable->identifier->suite->parent_block->has_local(p_assignable->identifier->name)) {
  1640. const GDScriptParser::SuiteNode::Local &local = p_assignable->identifier->suite->parent_block->get_local(p_assignable->identifier->name);
  1641. parser->push_warning(p_assignable->identifier, GDScriptWarning::CONFUSABLE_LOCAL_DECLARATION, local.get_name(), p_assignable->identifier->name);
  1642. }
  1643. }
  1644. #endif
  1645. GDScriptParser::DataType specified_type;
  1646. bool has_specified_type = p_assignable->datatype_specifier != nullptr;
  1647. if (has_specified_type) {
  1648. specified_type = type_from_metatype(resolve_datatype(p_assignable->datatype_specifier));
  1649. type = specified_type;
  1650. }
  1651. if (p_assignable->initializer != nullptr) {
  1652. reduce_expression(p_assignable->initializer);
  1653. if (p_assignable->initializer->type == GDScriptParser::Node::ARRAY) {
  1654. GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(p_assignable->initializer);
  1655. if (has_specified_type && specified_type.has_container_element_type(0)) {
  1656. update_array_literal_element_type(array, specified_type.get_container_element_type(0));
  1657. }
  1658. }
  1659. if (is_constant && !p_assignable->initializer->is_constant) {
  1660. bool is_initializer_value_reduced = false;
  1661. Variant initializer_value = make_expression_reduced_value(p_assignable->initializer, is_initializer_value_reduced);
  1662. if (is_initializer_value_reduced) {
  1663. p_assignable->initializer->is_constant = true;
  1664. p_assignable->initializer->reduced_value = initializer_value;
  1665. } else {
  1666. push_error(vformat(R"(Assigned value for %s "%s" isn't a constant expression.)", p_kind, p_assignable->identifier->name), p_assignable->initializer);
  1667. }
  1668. }
  1669. if (has_specified_type && p_assignable->initializer->is_constant) {
  1670. update_const_expression_builtin_type(p_assignable->initializer, specified_type, "assign");
  1671. }
  1672. GDScriptParser::DataType initializer_type = p_assignable->initializer->get_datatype();
  1673. if (p_assignable->infer_datatype) {
  1674. if (!initializer_type.is_set() || initializer_type.has_no_type() || !initializer_type.is_hard_type()) {
  1675. push_error(vformat(R"(Cannot infer the type of "%s" %s because the value doesn't have a set type.)", p_assignable->identifier->name, p_kind), p_assignable->initializer);
  1676. } else if (initializer_type.kind == GDScriptParser::DataType::BUILTIN && initializer_type.builtin_type == Variant::NIL && !is_constant) {
  1677. push_error(vformat(R"(Cannot infer the type of "%s" %s because the value is "null".)", p_assignable->identifier->name, p_kind), p_assignable->initializer);
  1678. }
  1679. #ifdef DEBUG_ENABLED
  1680. if (initializer_type.is_hard_type() && initializer_type.is_variant()) {
  1681. parser->push_warning(p_assignable, GDScriptWarning::INFERENCE_ON_VARIANT, p_kind);
  1682. }
  1683. #endif
  1684. } else {
  1685. if (!initializer_type.is_set()) {
  1686. push_error(vformat(R"(Could not resolve type for %s "%s".)", p_kind, p_assignable->identifier->name), p_assignable->initializer);
  1687. }
  1688. }
  1689. if (!has_specified_type) {
  1690. type = initializer_type;
  1691. if (!type.is_set() || (type.is_hard_type() && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL && !is_constant)) {
  1692. type.kind = GDScriptParser::DataType::VARIANT;
  1693. }
  1694. if (p_assignable->infer_datatype || is_constant) {
  1695. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1696. } else {
  1697. type.type_source = GDScriptParser::DataType::INFERRED;
  1698. }
  1699. } else if (!specified_type.is_variant()) {
  1700. if (initializer_type.is_variant() || !initializer_type.is_hard_type()) {
  1701. mark_node_unsafe(p_assignable->initializer);
  1702. p_assignable->use_conversion_assign = true;
  1703. if (!initializer_type.is_variant() && !is_type_compatible(specified_type, initializer_type, true, p_assignable->initializer)) {
  1704. downgrade_node_type_source(p_assignable->initializer);
  1705. }
  1706. } else if (!is_type_compatible(specified_type, initializer_type, true, p_assignable->initializer)) {
  1707. if (!is_constant && is_type_compatible(initializer_type, specified_type)) {
  1708. mark_node_unsafe(p_assignable->initializer);
  1709. p_assignable->use_conversion_assign = true;
  1710. } else {
  1711. push_error(vformat(R"(Cannot assign a value of type %s to %s "%s" with specified type %s.)", initializer_type.to_string(), p_kind, p_assignable->identifier->name, specified_type.to_string()), p_assignable->initializer);
  1712. }
  1713. } else if (specified_type.has_container_element_type(0) && !initializer_type.has_container_element_type(0)) {
  1714. mark_node_unsafe(p_assignable->initializer);
  1715. #ifdef DEBUG_ENABLED
  1716. } else if (specified_type.builtin_type == Variant::INT && initializer_type.builtin_type == Variant::FLOAT) {
  1717. parser->push_warning(p_assignable->initializer, GDScriptWarning::NARROWING_CONVERSION);
  1718. #endif
  1719. }
  1720. }
  1721. }
  1722. #ifdef DEBUG_ENABLED
  1723. if (!has_specified_type) {
  1724. const bool is_parameter = p_assignable->type == GDScriptParser::Node::PARAMETER;
  1725. const String declaration_type = is_constant ? "Constant" : (is_parameter ? "Parameter" : "Variable");
  1726. if (p_assignable->infer_datatype || is_constant) {
  1727. parser->push_warning(p_assignable, GDScriptWarning::INFERRED_DECLARATION, declaration_type, p_assignable->identifier->name);
  1728. } else {
  1729. parser->push_warning(p_assignable, GDScriptWarning::UNTYPED_DECLARATION, declaration_type, p_assignable->identifier->name);
  1730. }
  1731. } else if (specified_type.kind == GDScriptParser::DataType::ENUM && p_assignable->initializer == nullptr) {
  1732. // Warn about enum variables without default value. Unless the enum defines the "0" value, then it's fine.
  1733. bool has_zero_value = false;
  1734. for (const KeyValue<StringName, int64_t> &kv : specified_type.enum_values) {
  1735. if (kv.value == 0) {
  1736. has_zero_value = true;
  1737. break;
  1738. }
  1739. }
  1740. if (!has_zero_value) {
  1741. parser->push_warning(p_assignable, GDScriptWarning::ENUM_VARIABLE_WITHOUT_DEFAULT, p_assignable->identifier->name);
  1742. }
  1743. }
  1744. #endif
  1745. type.is_constant = is_constant;
  1746. type.is_read_only = false;
  1747. p_assignable->set_datatype(type);
  1748. }
  1749. void GDScriptAnalyzer::resolve_variable(GDScriptParser::VariableNode *p_variable, bool p_is_local) {
  1750. static constexpr const char *kind = "variable";
  1751. resolve_assignable(p_variable, kind);
  1752. #ifdef DEBUG_ENABLED
  1753. if (p_is_local) {
  1754. if (p_variable->usages == 0 && !String(p_variable->identifier->name).begins_with("_")) {
  1755. parser->push_warning(p_variable, GDScriptWarning::UNUSED_VARIABLE, p_variable->identifier->name);
  1756. }
  1757. }
  1758. is_shadowing(p_variable->identifier, kind, p_is_local);
  1759. #endif
  1760. }
  1761. void GDScriptAnalyzer::resolve_constant(GDScriptParser::ConstantNode *p_constant, bool p_is_local) {
  1762. static constexpr const char *kind = "constant";
  1763. resolve_assignable(p_constant, kind);
  1764. #ifdef DEBUG_ENABLED
  1765. if (p_is_local) {
  1766. if (p_constant->usages == 0 && !String(p_constant->identifier->name).begins_with("_")) {
  1767. parser->push_warning(p_constant, GDScriptWarning::UNUSED_LOCAL_CONSTANT, p_constant->identifier->name);
  1768. }
  1769. }
  1770. is_shadowing(p_constant->identifier, kind, p_is_local);
  1771. #endif
  1772. }
  1773. void GDScriptAnalyzer::resolve_parameter(GDScriptParser::ParameterNode *p_parameter) {
  1774. static constexpr const char *kind = "parameter";
  1775. resolve_assignable(p_parameter, kind);
  1776. }
  1777. void GDScriptAnalyzer::resolve_if(GDScriptParser::IfNode *p_if) {
  1778. reduce_expression(p_if->condition);
  1779. resolve_suite(p_if->true_block);
  1780. p_if->set_datatype(p_if->true_block->get_datatype());
  1781. if (p_if->false_block != nullptr) {
  1782. resolve_suite(p_if->false_block);
  1783. decide_suite_type(p_if, p_if->false_block);
  1784. }
  1785. }
  1786. void GDScriptAnalyzer::resolve_for(GDScriptParser::ForNode *p_for) {
  1787. bool list_resolved = false;
  1788. // Optimize constant range() call to not allocate an array.
  1789. // Use int, Vector2i, Vector3i instead, which also can be used as range iterators.
  1790. if (p_for->list && p_for->list->type == GDScriptParser::Node::CALL) {
  1791. GDScriptParser::CallNode *call = static_cast<GDScriptParser::CallNode *>(p_for->list);
  1792. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1793. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  1794. GDScriptParser::IdentifierNode *callee = static_cast<GDScriptParser::IdentifierNode *>(call->callee);
  1795. if (callee->name == "range") {
  1796. list_resolved = true;
  1797. if (call->arguments.size() < 1) {
  1798. push_error(R"*(Invalid call for "range()" function. Expected at least 1 argument, none given.)*", call->callee);
  1799. } else if (call->arguments.size() > 3) {
  1800. push_error(vformat(R"*(Invalid call for "range()" function. Expected at most 3 arguments, %d given.)*", call->arguments.size()), call->callee);
  1801. } else {
  1802. // Now we can optimize it.
  1803. bool can_reduce = true;
  1804. Vector<Variant> args;
  1805. args.resize(call->arguments.size());
  1806. for (int i = 0; i < call->arguments.size(); i++) {
  1807. GDScriptParser::ExpressionNode *argument = call->arguments[i];
  1808. reduce_expression(argument);
  1809. if (argument->is_constant) {
  1810. if (argument->reduced_value.get_type() != Variant::INT && argument->reduced_value.get_type() != Variant::FLOAT) {
  1811. can_reduce = false;
  1812. push_error(vformat(R"*(Invalid argument for "range()" call. Argument %d should be int or float but "%s" was given.)*", i + 1, Variant::get_type_name(argument->reduced_value.get_type())), argument);
  1813. }
  1814. if (can_reduce) {
  1815. args.write[i] = argument->reduced_value;
  1816. }
  1817. } else {
  1818. can_reduce = false;
  1819. GDScriptParser::DataType argument_type = argument->get_datatype();
  1820. if (argument_type.is_variant() || !argument_type.is_hard_type()) {
  1821. mark_node_unsafe(argument);
  1822. }
  1823. if (!argument_type.is_variant() && (argument_type.builtin_type != Variant::INT && argument_type.builtin_type != Variant::FLOAT)) {
  1824. if (!argument_type.is_hard_type()) {
  1825. downgrade_node_type_source(argument);
  1826. } else {
  1827. push_error(vformat(R"*(Invalid argument for "range()" call. Argument %d should be int or float but "%s" was given.)*", i + 1, argument_type.to_string()), argument);
  1828. }
  1829. }
  1830. }
  1831. }
  1832. Variant reduced;
  1833. if (can_reduce) {
  1834. switch (args.size()) {
  1835. case 1:
  1836. reduced = (int32_t)args[0];
  1837. break;
  1838. case 2:
  1839. reduced = Vector2i(args[0], args[1]);
  1840. break;
  1841. case 3:
  1842. reduced = Vector3i(args[0], args[1], args[2]);
  1843. break;
  1844. }
  1845. p_for->list->is_constant = true;
  1846. p_for->list->reduced_value = reduced;
  1847. }
  1848. }
  1849. if (p_for->list->is_constant) {
  1850. p_for->list->set_datatype(type_from_variant(p_for->list->reduced_value, p_for->list));
  1851. } else {
  1852. GDScriptParser::DataType list_type;
  1853. list_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1854. list_type.kind = GDScriptParser::DataType::BUILTIN;
  1855. list_type.builtin_type = Variant::ARRAY;
  1856. p_for->list->set_datatype(list_type);
  1857. }
  1858. }
  1859. }
  1860. }
  1861. GDScriptParser::DataType variable_type;
  1862. String list_visible_type = "<unresolved type>";
  1863. if (list_resolved) {
  1864. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1865. variable_type.kind = GDScriptParser::DataType::BUILTIN;
  1866. variable_type.builtin_type = Variant::INT;
  1867. list_visible_type = "Array[int]"; // NOTE: `range()` has `Array` return type.
  1868. } else if (p_for->list) {
  1869. resolve_node(p_for->list, false);
  1870. GDScriptParser::DataType list_type = p_for->list->get_datatype();
  1871. list_visible_type = list_type.to_string();
  1872. if (!list_type.is_hard_type()) {
  1873. mark_node_unsafe(p_for->list);
  1874. }
  1875. if (list_type.is_variant()) {
  1876. variable_type.kind = GDScriptParser::DataType::VARIANT;
  1877. mark_node_unsafe(p_for->list);
  1878. } else if (list_type.has_container_element_type(0)) {
  1879. variable_type = list_type.get_container_element_type(0);
  1880. variable_type.type_source = list_type.type_source;
  1881. } else if (list_type.is_typed_container_type()) {
  1882. variable_type = list_type.get_typed_container_type();
  1883. variable_type.type_source = list_type.type_source;
  1884. } else if (list_type.builtin_type == Variant::INT || list_type.builtin_type == Variant::FLOAT || list_type.builtin_type == Variant::STRING) {
  1885. variable_type.type_source = list_type.type_source;
  1886. variable_type.kind = GDScriptParser::DataType::BUILTIN;
  1887. variable_type.builtin_type = list_type.builtin_type;
  1888. } else if (list_type.builtin_type == Variant::VECTOR2I || list_type.builtin_type == Variant::VECTOR3I) {
  1889. variable_type.type_source = list_type.type_source;
  1890. variable_type.kind = GDScriptParser::DataType::BUILTIN;
  1891. variable_type.builtin_type = Variant::INT;
  1892. } else if (list_type.builtin_type == Variant::VECTOR2 || list_type.builtin_type == Variant::VECTOR3) {
  1893. variable_type.type_source = list_type.type_source;
  1894. variable_type.kind = GDScriptParser::DataType::BUILTIN;
  1895. variable_type.builtin_type = Variant::FLOAT;
  1896. } else if (list_type.builtin_type == Variant::OBJECT) {
  1897. GDScriptParser::DataType return_type;
  1898. List<GDScriptParser::DataType> par_types;
  1899. int default_arg_count = 0;
  1900. BitField<MethodFlags> method_flags;
  1901. if (get_function_signature(p_for->list, false, list_type, CoreStringNames::get_singleton()->_iter_get, return_type, par_types, default_arg_count, method_flags)) {
  1902. variable_type = return_type;
  1903. variable_type.type_source = list_type.type_source;
  1904. } else if (!list_type.is_hard_type()) {
  1905. variable_type.kind = GDScriptParser::DataType::VARIANT;
  1906. } else {
  1907. push_error(vformat(R"(Unable to iterate on object of type "%s".)", list_type.to_string()), p_for->list);
  1908. }
  1909. } else if (list_type.builtin_type == Variant::ARRAY || list_type.builtin_type == Variant::DICTIONARY || !list_type.is_hard_type()) {
  1910. variable_type.kind = GDScriptParser::DataType::VARIANT;
  1911. } else {
  1912. push_error(vformat(R"(Unable to iterate on value of type "%s".)", list_type.to_string()), p_for->list);
  1913. }
  1914. }
  1915. if (p_for->variable) {
  1916. if (p_for->datatype_specifier) {
  1917. GDScriptParser::DataType specified_type = type_from_metatype(resolve_datatype(p_for->datatype_specifier));
  1918. if (!specified_type.is_variant()) {
  1919. if (variable_type.is_variant() || !variable_type.is_hard_type()) {
  1920. mark_node_unsafe(p_for->variable);
  1921. p_for->use_conversion_assign = true;
  1922. } else if (!is_type_compatible(specified_type, variable_type, true, p_for->variable)) {
  1923. if (is_type_compatible(variable_type, specified_type)) {
  1924. mark_node_unsafe(p_for->variable);
  1925. p_for->use_conversion_assign = true;
  1926. } else {
  1927. push_error(vformat(R"(Unable to iterate on value of type "%s" with variable of type "%s".)", list_visible_type, specified_type.to_string()), p_for->datatype_specifier);
  1928. }
  1929. } else if (!is_type_compatible(specified_type, variable_type)) {
  1930. p_for->use_conversion_assign = true;
  1931. }
  1932. if (p_for->list && p_for->list->type == GDScriptParser::Node::ARRAY) {
  1933. update_array_literal_element_type(static_cast<GDScriptParser::ArrayNode *>(p_for->list), specified_type);
  1934. }
  1935. }
  1936. p_for->variable->set_datatype(specified_type);
  1937. } else {
  1938. p_for->variable->set_datatype(variable_type);
  1939. #ifdef DEBUG_ENABLED
  1940. if (variable_type.is_hard_type()) {
  1941. parser->push_warning(p_for->variable, GDScriptWarning::INFERRED_DECLARATION, R"("for" iterator variable)", p_for->variable->name);
  1942. } else {
  1943. parser->push_warning(p_for->variable, GDScriptWarning::UNTYPED_DECLARATION, R"("for" iterator variable)", p_for->variable->name);
  1944. }
  1945. #endif
  1946. }
  1947. }
  1948. resolve_suite(p_for->loop);
  1949. p_for->set_datatype(p_for->loop->get_datatype());
  1950. #ifdef DEBUG_ENABLED
  1951. if (p_for->variable) {
  1952. is_shadowing(p_for->variable, R"("for" iterator variable)", true);
  1953. }
  1954. #endif
  1955. }
  1956. void GDScriptAnalyzer::resolve_while(GDScriptParser::WhileNode *p_while) {
  1957. resolve_node(p_while->condition, false);
  1958. resolve_suite(p_while->loop);
  1959. p_while->set_datatype(p_while->loop->get_datatype());
  1960. }
  1961. void GDScriptAnalyzer::resolve_assert(GDScriptParser::AssertNode *p_assert) {
  1962. reduce_expression(p_assert->condition);
  1963. if (p_assert->message != nullptr) {
  1964. reduce_expression(p_assert->message);
  1965. 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)) {
  1966. push_error(R"(Expected string for assert error message.)", p_assert->message);
  1967. }
  1968. }
  1969. p_assert->set_datatype(p_assert->condition->get_datatype());
  1970. #ifdef DEBUG_ENABLED
  1971. if (p_assert->condition->is_constant) {
  1972. if (p_assert->condition->reduced_value.booleanize()) {
  1973. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_TRUE);
  1974. } else if (!(p_assert->condition->type == GDScriptParser::Node::LITERAL && static_cast<GDScriptParser::LiteralNode *>(p_assert->condition)->value.get_type() == Variant::BOOL)) {
  1975. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_FALSE);
  1976. }
  1977. }
  1978. #endif
  1979. }
  1980. void GDScriptAnalyzer::resolve_match(GDScriptParser::MatchNode *p_match) {
  1981. reduce_expression(p_match->test);
  1982. for (int i = 0; i < p_match->branches.size(); i++) {
  1983. resolve_match_branch(p_match->branches[i], p_match->test);
  1984. decide_suite_type(p_match, p_match->branches[i]);
  1985. }
  1986. }
  1987. void GDScriptAnalyzer::resolve_match_branch(GDScriptParser::MatchBranchNode *p_match_branch, GDScriptParser::ExpressionNode *p_match_test) {
  1988. // Apply annotations.
  1989. for (GDScriptParser::AnnotationNode *&E : p_match_branch->annotations) {
  1990. resolve_annotation(E);
  1991. E->apply(parser, p_match_branch, nullptr); // TODO: Provide `p_class`.
  1992. }
  1993. for (int i = 0; i < p_match_branch->patterns.size(); i++) {
  1994. resolve_match_pattern(p_match_branch->patterns[i], p_match_test);
  1995. }
  1996. if (p_match_branch->guard_body) {
  1997. resolve_suite(p_match_branch->guard_body);
  1998. }
  1999. resolve_suite(p_match_branch->block);
  2000. decide_suite_type(p_match_branch, p_match_branch->block);
  2001. }
  2002. void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_match_pattern, GDScriptParser::ExpressionNode *p_match_test) {
  2003. if (p_match_pattern == nullptr) {
  2004. return;
  2005. }
  2006. GDScriptParser::DataType result;
  2007. switch (p_match_pattern->pattern_type) {
  2008. case GDScriptParser::PatternNode::PT_LITERAL:
  2009. if (p_match_pattern->literal) {
  2010. reduce_literal(p_match_pattern->literal);
  2011. result = p_match_pattern->literal->get_datatype();
  2012. }
  2013. break;
  2014. case GDScriptParser::PatternNode::PT_EXPRESSION:
  2015. if (p_match_pattern->expression) {
  2016. GDScriptParser::ExpressionNode *expr = p_match_pattern->expression;
  2017. reduce_expression(expr);
  2018. result = expr->get_datatype();
  2019. if (!expr->is_constant) {
  2020. while (expr && expr->type == GDScriptParser::Node::SUBSCRIPT) {
  2021. GDScriptParser::SubscriptNode *sub = static_cast<GDScriptParser::SubscriptNode *>(expr);
  2022. if (!sub->is_attribute) {
  2023. expr = nullptr;
  2024. } else {
  2025. expr = sub->base;
  2026. }
  2027. }
  2028. if (!expr || expr->type != GDScriptParser::Node::IDENTIFIER) {
  2029. push_error(R"(Expression in match pattern must be a constant expression, an identifier, or an attribute access ("A.B").)", expr);
  2030. }
  2031. }
  2032. }
  2033. break;
  2034. case GDScriptParser::PatternNode::PT_BIND:
  2035. if (p_match_test != nullptr) {
  2036. result = p_match_test->get_datatype();
  2037. } else {
  2038. result.kind = GDScriptParser::DataType::VARIANT;
  2039. }
  2040. p_match_pattern->bind->set_datatype(result);
  2041. #ifdef DEBUG_ENABLED
  2042. is_shadowing(p_match_pattern->bind, "pattern bind", true);
  2043. if (p_match_pattern->bind->usages == 0 && !String(p_match_pattern->bind->name).begins_with("_")) {
  2044. parser->push_warning(p_match_pattern->bind, GDScriptWarning::UNUSED_VARIABLE, p_match_pattern->bind->name);
  2045. }
  2046. #endif
  2047. break;
  2048. case GDScriptParser::PatternNode::PT_ARRAY:
  2049. for (int i = 0; i < p_match_pattern->array.size(); i++) {
  2050. resolve_match_pattern(p_match_pattern->array[i], nullptr);
  2051. decide_suite_type(p_match_pattern, p_match_pattern->array[i]);
  2052. }
  2053. result = p_match_pattern->get_datatype();
  2054. break;
  2055. case GDScriptParser::PatternNode::PT_DICTIONARY:
  2056. for (int i = 0; i < p_match_pattern->dictionary.size(); i++) {
  2057. if (p_match_pattern->dictionary[i].key) {
  2058. reduce_expression(p_match_pattern->dictionary[i].key);
  2059. if (!p_match_pattern->dictionary[i].key->is_constant) {
  2060. push_error(R"(Expression in dictionary pattern key must be a constant.)", p_match_pattern->dictionary[i].key);
  2061. }
  2062. }
  2063. if (p_match_pattern->dictionary[i].value_pattern) {
  2064. resolve_match_pattern(p_match_pattern->dictionary[i].value_pattern, nullptr);
  2065. decide_suite_type(p_match_pattern, p_match_pattern->dictionary[i].value_pattern);
  2066. }
  2067. }
  2068. result = p_match_pattern->get_datatype();
  2069. break;
  2070. case GDScriptParser::PatternNode::PT_WILDCARD:
  2071. case GDScriptParser::PatternNode::PT_REST:
  2072. result.kind = GDScriptParser::DataType::VARIANT;
  2073. break;
  2074. }
  2075. p_match_pattern->set_datatype(result);
  2076. }
  2077. void GDScriptAnalyzer::resolve_return(GDScriptParser::ReturnNode *p_return) {
  2078. GDScriptParser::DataType result;
  2079. GDScriptParser::DataType expected_type;
  2080. bool has_expected_type = parser->current_function != nullptr;
  2081. if (has_expected_type) {
  2082. expected_type = parser->current_function->get_datatype();
  2083. }
  2084. if (p_return->return_value != nullptr) {
  2085. bool is_void_function = has_expected_type && expected_type.is_hard_type() && expected_type.kind == GDScriptParser::DataType::BUILTIN && expected_type.builtin_type == Variant::NIL;
  2086. bool is_call = p_return->return_value->type == GDScriptParser::Node::CALL;
  2087. if (is_void_function && is_call) {
  2088. // Pretend the call is a root expression to allow those that are "void".
  2089. reduce_call(static_cast<GDScriptParser::CallNode *>(p_return->return_value), false, true);
  2090. } else {
  2091. reduce_expression(p_return->return_value);
  2092. }
  2093. if (is_void_function) {
  2094. p_return->void_return = true;
  2095. const GDScriptParser::DataType &return_type = p_return->return_value->datatype;
  2096. if (is_call && !return_type.is_hard_type()) {
  2097. String function_name = parser->current_function->identifier ? parser->current_function->identifier->name.operator String() : String("<anonymous function>");
  2098. String called_function_name = static_cast<GDScriptParser::CallNode *>(p_return->return_value)->function_name.operator String();
  2099. #ifdef DEBUG_ENABLED
  2100. parser->push_warning(p_return, GDScriptWarning::UNSAFE_VOID_RETURN, function_name, called_function_name);
  2101. #endif
  2102. mark_node_unsafe(p_return);
  2103. } else if (!is_call) {
  2104. push_error("A void function cannot return a value.", p_return);
  2105. }
  2106. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2107. result.kind = GDScriptParser::DataType::BUILTIN;
  2108. result.builtin_type = Variant::NIL;
  2109. result.is_constant = true;
  2110. } else {
  2111. if (p_return->return_value->type == GDScriptParser::Node::ARRAY && has_expected_type && expected_type.has_container_element_type(0)) {
  2112. update_array_literal_element_type(static_cast<GDScriptParser::ArrayNode *>(p_return->return_value), expected_type.get_container_element_type(0));
  2113. }
  2114. if (has_expected_type && expected_type.is_hard_type() && p_return->return_value->is_constant) {
  2115. update_const_expression_builtin_type(p_return->return_value, expected_type, "return");
  2116. }
  2117. result = p_return->return_value->get_datatype();
  2118. }
  2119. } else {
  2120. // Return type is null by default.
  2121. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2122. result.kind = GDScriptParser::DataType::BUILTIN;
  2123. result.builtin_type = Variant::NIL;
  2124. result.is_constant = true;
  2125. }
  2126. if (has_expected_type && !expected_type.is_variant()) {
  2127. if (result.is_variant() || !result.is_hard_type()) {
  2128. mark_node_unsafe(p_return);
  2129. if (!is_type_compatible(expected_type, result, true, p_return)) {
  2130. downgrade_node_type_source(p_return);
  2131. }
  2132. } else if (!is_type_compatible(expected_type, result, true, p_return)) {
  2133. mark_node_unsafe(p_return);
  2134. if (!is_type_compatible(result, expected_type)) {
  2135. 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);
  2136. }
  2137. #ifdef DEBUG_ENABLED
  2138. } else if (expected_type.builtin_type == Variant::INT && result.builtin_type == Variant::FLOAT) {
  2139. parser->push_warning(p_return, GDScriptWarning::NARROWING_CONVERSION);
  2140. #endif
  2141. }
  2142. }
  2143. p_return->set_datatype(result);
  2144. }
  2145. void GDScriptAnalyzer::reduce_expression(GDScriptParser::ExpressionNode *p_expression, bool p_is_root) {
  2146. // This one makes some magic happen.
  2147. if (p_expression == nullptr) {
  2148. return;
  2149. }
  2150. if (p_expression->reduced) {
  2151. // Don't do this more than once.
  2152. return;
  2153. }
  2154. p_expression->reduced = true;
  2155. switch (p_expression->type) {
  2156. case GDScriptParser::Node::ARRAY:
  2157. reduce_array(static_cast<GDScriptParser::ArrayNode *>(p_expression));
  2158. break;
  2159. case GDScriptParser::Node::ASSIGNMENT:
  2160. reduce_assignment(static_cast<GDScriptParser::AssignmentNode *>(p_expression));
  2161. break;
  2162. case GDScriptParser::Node::AWAIT:
  2163. reduce_await(static_cast<GDScriptParser::AwaitNode *>(p_expression));
  2164. break;
  2165. case GDScriptParser::Node::BINARY_OPERATOR:
  2166. reduce_binary_op(static_cast<GDScriptParser::BinaryOpNode *>(p_expression));
  2167. break;
  2168. case GDScriptParser::Node::CALL:
  2169. reduce_call(static_cast<GDScriptParser::CallNode *>(p_expression), false, p_is_root);
  2170. break;
  2171. case GDScriptParser::Node::CAST:
  2172. reduce_cast(static_cast<GDScriptParser::CastNode *>(p_expression));
  2173. break;
  2174. case GDScriptParser::Node::DICTIONARY:
  2175. reduce_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_expression));
  2176. break;
  2177. case GDScriptParser::Node::GET_NODE:
  2178. reduce_get_node(static_cast<GDScriptParser::GetNodeNode *>(p_expression));
  2179. break;
  2180. case GDScriptParser::Node::IDENTIFIER:
  2181. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_expression));
  2182. break;
  2183. case GDScriptParser::Node::LAMBDA:
  2184. reduce_lambda(static_cast<GDScriptParser::LambdaNode *>(p_expression));
  2185. break;
  2186. case GDScriptParser::Node::LITERAL:
  2187. reduce_literal(static_cast<GDScriptParser::LiteralNode *>(p_expression));
  2188. break;
  2189. case GDScriptParser::Node::PRELOAD:
  2190. reduce_preload(static_cast<GDScriptParser::PreloadNode *>(p_expression));
  2191. break;
  2192. case GDScriptParser::Node::SELF:
  2193. reduce_self(static_cast<GDScriptParser::SelfNode *>(p_expression));
  2194. break;
  2195. case GDScriptParser::Node::SUBSCRIPT:
  2196. reduce_subscript(static_cast<GDScriptParser::SubscriptNode *>(p_expression));
  2197. break;
  2198. case GDScriptParser::Node::TERNARY_OPERATOR:
  2199. reduce_ternary_op(static_cast<GDScriptParser::TernaryOpNode *>(p_expression), p_is_root);
  2200. break;
  2201. case GDScriptParser::Node::TYPE_TEST:
  2202. reduce_type_test(static_cast<GDScriptParser::TypeTestNode *>(p_expression));
  2203. break;
  2204. case GDScriptParser::Node::UNARY_OPERATOR:
  2205. reduce_unary_op(static_cast<GDScriptParser::UnaryOpNode *>(p_expression));
  2206. break;
  2207. // Non-expressions. Here only to make sure new nodes aren't forgotten.
  2208. case GDScriptParser::Node::NONE:
  2209. case GDScriptParser::Node::ANNOTATION:
  2210. case GDScriptParser::Node::ASSERT:
  2211. case GDScriptParser::Node::BREAK:
  2212. case GDScriptParser::Node::BREAKPOINT:
  2213. case GDScriptParser::Node::CLASS:
  2214. case GDScriptParser::Node::CONSTANT:
  2215. case GDScriptParser::Node::CONTINUE:
  2216. case GDScriptParser::Node::ENUM:
  2217. case GDScriptParser::Node::FOR:
  2218. case GDScriptParser::Node::FUNCTION:
  2219. case GDScriptParser::Node::IF:
  2220. case GDScriptParser::Node::MATCH:
  2221. case GDScriptParser::Node::MATCH_BRANCH:
  2222. case GDScriptParser::Node::PARAMETER:
  2223. case GDScriptParser::Node::PASS:
  2224. case GDScriptParser::Node::PATTERN:
  2225. case GDScriptParser::Node::RETURN:
  2226. case GDScriptParser::Node::SIGNAL:
  2227. case GDScriptParser::Node::SUITE:
  2228. case GDScriptParser::Node::TYPE:
  2229. case GDScriptParser::Node::VARIABLE:
  2230. case GDScriptParser::Node::WHILE:
  2231. ERR_FAIL_MSG("Reaching unreachable case");
  2232. }
  2233. if (p_expression->get_datatype().kind == GDScriptParser::DataType::UNRESOLVED) {
  2234. // Prevent `is_type_compatible()` errors for incomplete expressions.
  2235. // The error can still occur if `reduce_*()` is called directly.
  2236. GDScriptParser::DataType dummy;
  2237. dummy.kind = GDScriptParser::DataType::VARIANT;
  2238. p_expression->set_datatype(dummy);
  2239. }
  2240. }
  2241. void GDScriptAnalyzer::reduce_array(GDScriptParser::ArrayNode *p_array) {
  2242. for (int i = 0; i < p_array->elements.size(); i++) {
  2243. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  2244. reduce_expression(element);
  2245. }
  2246. // It's array in any case.
  2247. GDScriptParser::DataType arr_type;
  2248. arr_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2249. arr_type.kind = GDScriptParser::DataType::BUILTIN;
  2250. arr_type.builtin_type = Variant::ARRAY;
  2251. arr_type.is_constant = true;
  2252. p_array->set_datatype(arr_type);
  2253. }
  2254. #ifdef DEBUG_ENABLED
  2255. static bool enum_has_value(const GDScriptParser::DataType p_type, int64_t p_value) {
  2256. for (const KeyValue<StringName, int64_t> &E : p_type.enum_values) {
  2257. if (E.value == p_value) {
  2258. return true;
  2259. }
  2260. }
  2261. return false;
  2262. }
  2263. #endif
  2264. void GDScriptAnalyzer::update_const_expression_builtin_type(GDScriptParser::ExpressionNode *p_expression, const GDScriptParser::DataType &p_type, const char *p_usage, bool p_is_cast) {
  2265. if (p_expression->get_datatype() == p_type) {
  2266. return;
  2267. }
  2268. if (p_type.kind != GDScriptParser::DataType::BUILTIN && p_type.kind != GDScriptParser::DataType::ENUM) {
  2269. return;
  2270. }
  2271. GDScriptParser::DataType expression_type = p_expression->get_datatype();
  2272. bool is_enum_cast = p_is_cast && p_type.kind == GDScriptParser::DataType::ENUM && p_type.is_meta_type == false && expression_type.builtin_type == Variant::INT;
  2273. if (!is_enum_cast && !is_type_compatible(p_type, expression_type, true, p_expression)) {
  2274. push_error(vformat(R"(Cannot %s a value of type "%s" as "%s".)", p_usage, expression_type.to_string(), p_type.to_string()), p_expression);
  2275. return;
  2276. }
  2277. GDScriptParser::DataType value_type = type_from_variant(p_expression->reduced_value, p_expression);
  2278. if (expression_type.is_variant() && !is_enum_cast && !is_type_compatible(p_type, value_type, true, p_expression)) {
  2279. push_error(vformat(R"(Cannot %s a value of type "%s" as "%s".)", p_usage, value_type.to_string(), p_type.to_string()), p_expression);
  2280. return;
  2281. }
  2282. #ifdef DEBUG_ENABLED
  2283. if (p_type.kind == GDScriptParser::DataType::ENUM && value_type.builtin_type == Variant::INT && !enum_has_value(p_type, p_expression->reduced_value)) {
  2284. parser->push_warning(p_expression, GDScriptWarning::INT_AS_ENUM_WITHOUT_MATCH, p_usage, p_expression->reduced_value.stringify(), p_type.to_string());
  2285. }
  2286. #endif
  2287. if (value_type.builtin_type == p_type.builtin_type) {
  2288. p_expression->set_datatype(p_type);
  2289. return;
  2290. }
  2291. Variant converted_to;
  2292. const Variant *converted_from = &p_expression->reduced_value;
  2293. Callable::CallError call_error;
  2294. Variant::construct(p_type.builtin_type, converted_to, &converted_from, 1, call_error);
  2295. if (call_error.error) {
  2296. push_error(vformat(R"(Failed to convert a value of type "%s" to "%s".)", value_type.to_string(), p_type.to_string()), p_expression);
  2297. return;
  2298. }
  2299. #ifdef DEBUG_ENABLED
  2300. if (p_type.builtin_type == Variant::INT && value_type.builtin_type == Variant::FLOAT) {
  2301. parser->push_warning(p_expression, GDScriptWarning::NARROWING_CONVERSION);
  2302. }
  2303. #endif
  2304. p_expression->reduced_value = converted_to;
  2305. p_expression->set_datatype(p_type);
  2306. }
  2307. // When an array literal is stored (or passed as function argument) to a typed context, we then assume the array is typed.
  2308. // This function determines which type is that (if any).
  2309. void GDScriptAnalyzer::update_array_literal_element_type(GDScriptParser::ArrayNode *p_array, const GDScriptParser::DataType &p_element_type) {
  2310. GDScriptParser::DataType expected_type = p_element_type;
  2311. expected_type.container_element_types.clear(); // Nested types (like `Array[Array[int]]`) are not currently supported.
  2312. for (int i = 0; i < p_array->elements.size(); i++) {
  2313. GDScriptParser::ExpressionNode *element_node = p_array->elements[i];
  2314. if (element_node->is_constant) {
  2315. update_const_expression_builtin_type(element_node, expected_type, "include");
  2316. }
  2317. const GDScriptParser::DataType &actual_type = element_node->get_datatype();
  2318. if (actual_type.has_no_type() || actual_type.is_variant() || !actual_type.is_hard_type()) {
  2319. mark_node_unsafe(element_node);
  2320. continue;
  2321. }
  2322. if (!is_type_compatible(expected_type, actual_type, true, p_array)) {
  2323. if (is_type_compatible(actual_type, expected_type)) {
  2324. mark_node_unsafe(element_node);
  2325. continue;
  2326. }
  2327. push_error(vformat(R"(Cannot have an element of type "%s" in an array of type "Array[%s]".)", actual_type.to_string(), expected_type.to_string()), element_node);
  2328. return;
  2329. }
  2330. }
  2331. GDScriptParser::DataType array_type = p_array->get_datatype();
  2332. array_type.set_container_element_type(0, expected_type);
  2333. p_array->set_datatype(array_type);
  2334. }
  2335. void GDScriptAnalyzer::reduce_assignment(GDScriptParser::AssignmentNode *p_assignment) {
  2336. reduce_expression(p_assignment->assigned_value);
  2337. #ifdef DEBUG_ENABLED
  2338. // Increment assignment count for local variables.
  2339. // Before we reduce the assignee because we don't want to warn about not being assigned when performing the assignment.
  2340. if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  2341. GDScriptParser::IdentifierNode *id = static_cast<GDScriptParser::IdentifierNode *>(p_assignment->assignee);
  2342. if (id->source == GDScriptParser::IdentifierNode::LOCAL_VARIABLE && id->variable_source) {
  2343. id->variable_source->assignments++;
  2344. }
  2345. }
  2346. #endif
  2347. reduce_expression(p_assignment->assignee);
  2348. if (p_assignment->assigned_value == nullptr || p_assignment->assignee == nullptr) {
  2349. return;
  2350. }
  2351. GDScriptParser::DataType assignee_type = p_assignment->assignee->get_datatype();
  2352. if (assignee_type.is_constant) {
  2353. push_error("Cannot assign a new value to a constant.", p_assignment->assignee);
  2354. return;
  2355. } else if (p_assignment->assignee->type == GDScriptParser::Node::SUBSCRIPT && static_cast<GDScriptParser::SubscriptNode *>(p_assignment->assignee)->base->is_constant) {
  2356. const GDScriptParser::DataType &base_type = static_cast<GDScriptParser::SubscriptNode *>(p_assignment->assignee)->base->datatype;
  2357. if (base_type.kind != GDScriptParser::DataType::SCRIPT && base_type.kind != GDScriptParser::DataType::CLASS) { // Static variables.
  2358. push_error("Cannot assign a new value to a constant.", p_assignment->assignee);
  2359. return;
  2360. }
  2361. } else if (assignee_type.is_read_only) {
  2362. push_error("Cannot assign a new value to a read-only property.", p_assignment->assignee);
  2363. return;
  2364. } else if (p_assignment->assignee->type == GDScriptParser::Node::SUBSCRIPT) {
  2365. GDScriptParser::SubscriptNode *sub = static_cast<GDScriptParser::SubscriptNode *>(p_assignment->assignee);
  2366. while (sub) {
  2367. const GDScriptParser::DataType &base_type = sub->base->datatype;
  2368. if (base_type.is_hard_type() && base_type.is_read_only) {
  2369. if (base_type.kind == GDScriptParser::DataType::BUILTIN && !Variant::is_type_shared(base_type.builtin_type)) {
  2370. push_error("Cannot assign a new value to a read-only property.", p_assignment->assignee);
  2371. return;
  2372. }
  2373. } else {
  2374. break;
  2375. }
  2376. if (sub->base->type == GDScriptParser::Node::SUBSCRIPT) {
  2377. sub = static_cast<GDScriptParser::SubscriptNode *>(sub->base);
  2378. } else {
  2379. sub = nullptr;
  2380. }
  2381. }
  2382. }
  2383. // Check if assigned value is an array literal, so we can make it a typed array too if appropriate.
  2384. if (p_assignment->assigned_value->type == GDScriptParser::Node::ARRAY && assignee_type.is_hard_type() && assignee_type.has_container_element_type(0)) {
  2385. update_array_literal_element_type(static_cast<GDScriptParser::ArrayNode *>(p_assignment->assigned_value), assignee_type.get_container_element_type(0));
  2386. }
  2387. if (p_assignment->operation == GDScriptParser::AssignmentNode::OP_NONE && assignee_type.is_hard_type() && p_assignment->assigned_value->is_constant) {
  2388. update_const_expression_builtin_type(p_assignment->assigned_value, assignee_type, "assign");
  2389. }
  2390. GDScriptParser::DataType assigned_value_type = p_assignment->assigned_value->get_datatype();
  2391. bool assignee_is_variant = assignee_type.is_variant();
  2392. bool assignee_is_hard = assignee_type.is_hard_type();
  2393. bool assigned_is_variant = assigned_value_type.is_variant();
  2394. bool assigned_is_hard = assigned_value_type.is_hard_type();
  2395. bool compatible = true;
  2396. bool downgrades_assignee = false;
  2397. bool downgrades_assigned = false;
  2398. GDScriptParser::DataType op_type = assigned_value_type;
  2399. if (p_assignment->operation != GDScriptParser::AssignmentNode::OP_NONE && !op_type.is_variant()) {
  2400. op_type = get_operation_type(p_assignment->variant_op, assignee_type, assigned_value_type, compatible, p_assignment->assigned_value);
  2401. if (assignee_is_variant) {
  2402. // variant assignee
  2403. mark_node_unsafe(p_assignment);
  2404. } else if (!compatible) {
  2405. // incompatible hard types and non-variant assignee
  2406. mark_node_unsafe(p_assignment);
  2407. if (assigned_is_variant) {
  2408. // incompatible hard non-variant assignee and hard variant assigned
  2409. p_assignment->use_conversion_assign = true;
  2410. } else {
  2411. // incompatible hard non-variant types
  2412. push_error(vformat(R"(Invalid operands "%s" and "%s" for assignment operator.)", assignee_type.to_string(), assigned_value_type.to_string()), p_assignment);
  2413. }
  2414. } else if (op_type.type_source == GDScriptParser::DataType::UNDETECTED && !assigned_is_variant) {
  2415. // incompatible non-variant types (at least one weak)
  2416. downgrades_assignee = !assignee_is_hard;
  2417. downgrades_assigned = !assigned_is_hard;
  2418. }
  2419. }
  2420. p_assignment->set_datatype(op_type);
  2421. if (assignee_is_variant) {
  2422. if (!assignee_is_hard) {
  2423. // weak variant assignee
  2424. mark_node_unsafe(p_assignment);
  2425. }
  2426. } else {
  2427. if (assignee_is_hard && !assigned_is_hard) {
  2428. // hard non-variant assignee and weak assigned
  2429. mark_node_unsafe(p_assignment);
  2430. p_assignment->use_conversion_assign = true;
  2431. downgrades_assigned = downgrades_assigned || (!assigned_is_variant && !is_type_compatible(assignee_type, op_type, true, p_assignment->assigned_value));
  2432. } else if (compatible) {
  2433. if (op_type.is_variant()) {
  2434. // non-variant assignee and variant result
  2435. mark_node_unsafe(p_assignment);
  2436. if (assignee_is_hard) {
  2437. // hard non-variant assignee and variant result
  2438. p_assignment->use_conversion_assign = true;
  2439. } else {
  2440. // weak non-variant assignee and variant result
  2441. downgrades_assignee = true;
  2442. }
  2443. } else if (!is_type_compatible(assignee_type, op_type, assignee_is_hard, p_assignment->assigned_value)) {
  2444. // non-variant assignee and incompatible result
  2445. mark_node_unsafe(p_assignment);
  2446. if (assignee_is_hard) {
  2447. if (is_type_compatible(op_type, assignee_type)) {
  2448. // hard non-variant assignee and maybe compatible result
  2449. p_assignment->use_conversion_assign = true;
  2450. } else {
  2451. // hard non-variant assignee and incompatible result
  2452. push_error(vformat(R"(Value of type "%s" cannot be assigned to a variable of type "%s".)", assigned_value_type.to_string(), assignee_type.to_string()), p_assignment->assigned_value);
  2453. }
  2454. } else {
  2455. // weak non-variant assignee and incompatible result
  2456. downgrades_assignee = true;
  2457. }
  2458. } else if (assignee_type.has_container_element_type(0) && !op_type.has_container_element_type(0)) {
  2459. // typed array assignee and untyped array result
  2460. mark_node_unsafe(p_assignment);
  2461. }
  2462. }
  2463. }
  2464. if (downgrades_assignee) {
  2465. downgrade_node_type_source(p_assignment->assignee);
  2466. }
  2467. if (downgrades_assigned) {
  2468. downgrade_node_type_source(p_assignment->assigned_value);
  2469. }
  2470. #ifdef DEBUG_ENABLED
  2471. if (assignee_type.is_hard_type() && assignee_type.builtin_type == Variant::INT && assigned_value_type.builtin_type == Variant::FLOAT) {
  2472. parser->push_warning(p_assignment->assigned_value, GDScriptWarning::NARROWING_CONVERSION);
  2473. }
  2474. // Check for assignment with operation before assignment.
  2475. if (p_assignment->operation != GDScriptParser::AssignmentNode::OP_NONE && p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  2476. GDScriptParser::IdentifierNode *id = static_cast<GDScriptParser::IdentifierNode *>(p_assignment->assignee);
  2477. // Use == 1 here because this assignment was already counted in the beginning of the function.
  2478. if (id->source == GDScriptParser::IdentifierNode::LOCAL_VARIABLE && id->variable_source && id->variable_source->assignments == 1) {
  2479. parser->push_warning(p_assignment, GDScriptWarning::UNASSIGNED_VARIABLE_OP_ASSIGN, id->name);
  2480. }
  2481. }
  2482. #endif
  2483. }
  2484. void GDScriptAnalyzer::reduce_await(GDScriptParser::AwaitNode *p_await) {
  2485. if (p_await->to_await == nullptr) {
  2486. GDScriptParser::DataType await_type;
  2487. await_type.kind = GDScriptParser::DataType::VARIANT;
  2488. p_await->set_datatype(await_type);
  2489. return;
  2490. }
  2491. if (p_await->to_await->type == GDScriptParser::Node::CALL) {
  2492. reduce_call(static_cast<GDScriptParser::CallNode *>(p_await->to_await), true);
  2493. } else {
  2494. reduce_expression(p_await->to_await);
  2495. }
  2496. GDScriptParser::DataType await_type = p_await->to_await->get_datatype();
  2497. // We cannot infer the type of the result of waiting for a signal.
  2498. if (await_type.is_hard_type() && await_type.kind == GDScriptParser::DataType::BUILTIN && await_type.builtin_type == Variant::SIGNAL) {
  2499. await_type.kind = GDScriptParser::DataType::VARIANT;
  2500. await_type.type_source = GDScriptParser::DataType::UNDETECTED;
  2501. } else if (p_await->to_await->is_constant) {
  2502. p_await->is_constant = p_await->to_await->is_constant;
  2503. p_await->reduced_value = p_await->to_await->reduced_value;
  2504. }
  2505. await_type.is_coroutine = false;
  2506. p_await->set_datatype(await_type);
  2507. #ifdef DEBUG_ENABLED
  2508. GDScriptParser::DataType to_await_type = p_await->to_await->get_datatype();
  2509. if (!to_await_type.is_coroutine && !to_await_type.is_variant() && to_await_type.builtin_type != Variant::SIGNAL) {
  2510. parser->push_warning(p_await, GDScriptWarning::REDUNDANT_AWAIT);
  2511. }
  2512. #endif
  2513. }
  2514. void GDScriptAnalyzer::reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op) {
  2515. reduce_expression(p_binary_op->left_operand);
  2516. reduce_expression(p_binary_op->right_operand);
  2517. GDScriptParser::DataType left_type;
  2518. if (p_binary_op->left_operand) {
  2519. left_type = p_binary_op->left_operand->get_datatype();
  2520. }
  2521. GDScriptParser::DataType right_type;
  2522. if (p_binary_op->right_operand) {
  2523. right_type = p_binary_op->right_operand->get_datatype();
  2524. }
  2525. if (!left_type.is_set() || !right_type.is_set()) {
  2526. return;
  2527. }
  2528. #ifdef DEBUG_ENABLED
  2529. if (p_binary_op->variant_op == Variant::OP_DIVIDE && left_type.builtin_type == Variant::INT && right_type.builtin_type == Variant::INT) {
  2530. parser->push_warning(p_binary_op, GDScriptWarning::INTEGER_DIVISION);
  2531. }
  2532. #endif
  2533. if (p_binary_op->left_operand->is_constant && p_binary_op->right_operand->is_constant) {
  2534. p_binary_op->is_constant = true;
  2535. if (p_binary_op->variant_op < Variant::OP_MAX) {
  2536. bool valid = false;
  2537. 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);
  2538. if (!valid) {
  2539. if (p_binary_op->reduced_value.get_type() == Variant::STRING) {
  2540. 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);
  2541. } else {
  2542. push_error(vformat(R"(Invalid operands to operator %s, %s and %s.)",
  2543. Variant::get_operator_name(p_binary_op->variant_op),
  2544. Variant::get_type_name(p_binary_op->left_operand->reduced_value.get_type()),
  2545. Variant::get_type_name(p_binary_op->right_operand->reduced_value.get_type())),
  2546. p_binary_op);
  2547. }
  2548. }
  2549. } else {
  2550. ERR_PRINT("Parser bug: unknown binary operation.");
  2551. }
  2552. p_binary_op->set_datatype(type_from_variant(p_binary_op->reduced_value, p_binary_op));
  2553. return;
  2554. }
  2555. GDScriptParser::DataType result;
  2556. if ((p_binary_op->variant_op == Variant::OP_EQUAL || p_binary_op->variant_op == Variant::OP_NOT_EQUAL) &&
  2557. ((left_type.kind == GDScriptParser::DataType::BUILTIN && left_type.builtin_type == Variant::NIL) || (right_type.kind == GDScriptParser::DataType::BUILTIN && right_type.builtin_type == Variant::NIL))) {
  2558. // "==" and "!=" operators always return a boolean when comparing to null.
  2559. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2560. result.kind = GDScriptParser::DataType::BUILTIN;
  2561. result.builtin_type = Variant::BOOL;
  2562. } else if (p_binary_op->variant_op == Variant::OP_MODULE && left_type.builtin_type == Variant::STRING) {
  2563. // The modulo operator (%) on string acts as formatting and will always return a string.
  2564. result.type_source = left_type.type_source;
  2565. result.kind = GDScriptParser::DataType::BUILTIN;
  2566. result.builtin_type = Variant::STRING;
  2567. } else if (left_type.is_variant() || right_type.is_variant()) {
  2568. // Cannot infer type because one operand can be anything.
  2569. result.kind = GDScriptParser::DataType::VARIANT;
  2570. mark_node_unsafe(p_binary_op);
  2571. } else if (p_binary_op->variant_op < Variant::OP_MAX) {
  2572. bool valid = false;
  2573. result = get_operation_type(p_binary_op->variant_op, left_type, right_type, valid, p_binary_op);
  2574. if (!valid) {
  2575. 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);
  2576. } else if (!result.is_hard_type()) {
  2577. mark_node_unsafe(p_binary_op);
  2578. }
  2579. } else {
  2580. ERR_PRINT("Parser bug: unknown binary operation.");
  2581. }
  2582. p_binary_op->set_datatype(result);
  2583. }
  2584. #ifdef SUGGEST_GODOT4_RENAMES
  2585. const char *get_rename_from_map(const char *map[][2], String key) {
  2586. for (int index = 0; map[index][0]; index++) {
  2587. if (map[index][0] == key) {
  2588. return map[index][1];
  2589. }
  2590. }
  2591. return nullptr;
  2592. }
  2593. // Checks if an identifier/function name has been renamed in Godot 4, uses ProjectConverter3To4 for rename map.
  2594. // Returns the new name if found, nullptr otherwise.
  2595. const char *check_for_renamed_identifier(String identifier, GDScriptParser::Node::Type type) {
  2596. switch (type) {
  2597. case GDScriptParser::Node::IDENTIFIER: {
  2598. // Check properties
  2599. const char *result = get_rename_from_map(RenamesMap3To4::gdscript_properties_renames, identifier);
  2600. if (result) {
  2601. return result;
  2602. }
  2603. // Check enum values
  2604. result = get_rename_from_map(RenamesMap3To4::enum_renames, identifier);
  2605. if (result) {
  2606. return result;
  2607. }
  2608. // Check color constants
  2609. result = get_rename_from_map(RenamesMap3To4::color_renames, identifier);
  2610. if (result) {
  2611. return result;
  2612. }
  2613. // Check type names
  2614. result = get_rename_from_map(RenamesMap3To4::class_renames, identifier);
  2615. if (result) {
  2616. return result;
  2617. }
  2618. return get_rename_from_map(RenamesMap3To4::builtin_types_renames, identifier);
  2619. }
  2620. case GDScriptParser::Node::CALL: {
  2621. const char *result = get_rename_from_map(RenamesMap3To4::gdscript_function_renames, identifier);
  2622. if (result) {
  2623. return result;
  2624. }
  2625. // Built-in Types are mistaken for function calls when the built-in type is not found.
  2626. // Check built-in types if function rename not found
  2627. return get_rename_from_map(RenamesMap3To4::builtin_types_renames, identifier);
  2628. }
  2629. // Signal references don't get parsed through the GDScriptAnalyzer. No support for signal rename hints.
  2630. default:
  2631. // No rename found, return null
  2632. return nullptr;
  2633. }
  2634. }
  2635. #endif // SUGGEST_GODOT4_RENAMES
  2636. void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_await, bool p_is_root) {
  2637. bool all_is_constant = true;
  2638. HashMap<int, GDScriptParser::ArrayNode *> arrays; // For array literal to potentially type when passing.
  2639. for (int i = 0; i < p_call->arguments.size(); i++) {
  2640. reduce_expression(p_call->arguments[i]);
  2641. if (p_call->arguments[i]->type == GDScriptParser::Node::ARRAY) {
  2642. arrays[i] = static_cast<GDScriptParser::ArrayNode *>(p_call->arguments[i]);
  2643. }
  2644. all_is_constant = all_is_constant && p_call->arguments[i]->is_constant;
  2645. }
  2646. GDScriptParser::Node::Type callee_type = p_call->get_callee_type();
  2647. GDScriptParser::DataType call_type;
  2648. if (!p_call->is_super && callee_type == GDScriptParser::Node::IDENTIFIER) {
  2649. // Call to name directly.
  2650. StringName function_name = p_call->function_name;
  2651. if (function_name == SNAME("Object")) {
  2652. push_error(R"*(Invalid constructor "Object()", use "Object.new()" instead.)*", p_call);
  2653. p_call->set_datatype(call_type);
  2654. return;
  2655. }
  2656. Variant::Type builtin_type = GDScriptParser::get_builtin_type(function_name);
  2657. if (builtin_type < Variant::VARIANT_MAX) {
  2658. // Is a builtin constructor.
  2659. call_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2660. call_type.kind = GDScriptParser::DataType::BUILTIN;
  2661. call_type.builtin_type = builtin_type;
  2662. bool safe_to_fold = true;
  2663. switch (builtin_type) {
  2664. // Those are stored by reference so not suited for compile-time construction.
  2665. // Because in this case they would be the same reference in all constructed values.
  2666. case Variant::OBJECT:
  2667. case Variant::DICTIONARY:
  2668. case Variant::ARRAY:
  2669. case Variant::PACKED_BYTE_ARRAY:
  2670. case Variant::PACKED_INT32_ARRAY:
  2671. case Variant::PACKED_INT64_ARRAY:
  2672. case Variant::PACKED_FLOAT32_ARRAY:
  2673. case Variant::PACKED_FLOAT64_ARRAY:
  2674. case Variant::PACKED_STRING_ARRAY:
  2675. case Variant::PACKED_VECTOR2_ARRAY:
  2676. case Variant::PACKED_VECTOR3_ARRAY:
  2677. case Variant::PACKED_COLOR_ARRAY:
  2678. case Variant::PACKED_VECTOR4_ARRAY:
  2679. safe_to_fold = false;
  2680. break;
  2681. default:
  2682. break;
  2683. }
  2684. if (all_is_constant && safe_to_fold) {
  2685. // Construct here.
  2686. Vector<const Variant *> args;
  2687. for (int i = 0; i < p_call->arguments.size(); i++) {
  2688. args.push_back(&(p_call->arguments[i]->reduced_value));
  2689. }
  2690. Callable::CallError err;
  2691. Variant value;
  2692. Variant::construct(builtin_type, value, (const Variant **)args.ptr(), args.size(), err);
  2693. switch (err.error) {
  2694. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  2695. 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,
  2696. Variant::get_type_name(Variant::Type(err.expected)), p_call->arguments[err.argument]->get_datatype().to_string()),
  2697. p_call->arguments[err.argument]);
  2698. break;
  2699. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  2700. String signature = Variant::get_type_name(builtin_type) + "(";
  2701. for (int i = 0; i < p_call->arguments.size(); i++) {
  2702. if (i > 0) {
  2703. signature += ", ";
  2704. }
  2705. signature += p_call->arguments[i]->get_datatype().to_string();
  2706. }
  2707. signature += ")";
  2708. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call->callee);
  2709. } break;
  2710. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2711. 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);
  2712. break;
  2713. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2714. 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);
  2715. break;
  2716. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2717. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  2718. break; // Can't happen in a builtin constructor.
  2719. case Callable::CallError::CALL_OK:
  2720. p_call->is_constant = true;
  2721. p_call->reduced_value = value;
  2722. break;
  2723. }
  2724. } else {
  2725. // If there's one argument, try to use copy constructor (those aren't explicitly defined).
  2726. if (p_call->arguments.size() == 1) {
  2727. GDScriptParser::DataType arg_type = p_call->arguments[0]->get_datatype();
  2728. if (arg_type.is_hard_type() && !arg_type.is_variant()) {
  2729. if (arg_type.kind == GDScriptParser::DataType::BUILTIN && arg_type.builtin_type == builtin_type) {
  2730. // Okay.
  2731. p_call->set_datatype(call_type);
  2732. return;
  2733. }
  2734. } else {
  2735. #ifdef DEBUG_ENABLED
  2736. mark_node_unsafe(p_call);
  2737. // Constructors support overloads.
  2738. Vector<String> types;
  2739. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2740. if (i != builtin_type && Variant::can_convert_strict((Variant::Type)i, builtin_type)) {
  2741. types.push_back(Variant::get_type_name((Variant::Type)i));
  2742. }
  2743. }
  2744. String expected_types = function_name;
  2745. if (types.size() == 1) {
  2746. expected_types += "\" or \"" + types[0];
  2747. } else if (types.size() >= 2) {
  2748. for (int i = 0; i < types.size() - 1; i++) {
  2749. expected_types += "\", \"" + types[i];
  2750. }
  2751. expected_types += "\", or \"" + types[types.size() - 1];
  2752. }
  2753. parser->push_warning(p_call->arguments[0], GDScriptWarning::UNSAFE_CALL_ARGUMENT, "1", "constructor", function_name, expected_types, "Variant");
  2754. #endif
  2755. p_call->set_datatype(call_type);
  2756. return;
  2757. }
  2758. }
  2759. List<MethodInfo> constructors;
  2760. Variant::get_constructor_list(builtin_type, &constructors);
  2761. bool match = false;
  2762. for (const MethodInfo &info : constructors) {
  2763. if (p_call->arguments.size() < info.arguments.size() - info.default_arguments.size()) {
  2764. continue;
  2765. }
  2766. if (p_call->arguments.size() > info.arguments.size()) {
  2767. continue;
  2768. }
  2769. bool types_match = true;
  2770. for (int i = 0; i < p_call->arguments.size(); i++) {
  2771. GDScriptParser::DataType par_type = type_from_property(info.arguments[i], true);
  2772. GDScriptParser::DataType arg_type = p_call->arguments[i]->get_datatype();
  2773. if (!is_type_compatible(par_type, arg_type, true)) {
  2774. types_match = false;
  2775. break;
  2776. #ifdef DEBUG_ENABLED
  2777. } else {
  2778. if (par_type.builtin_type == Variant::INT && arg_type.builtin_type == Variant::FLOAT && builtin_type != Variant::INT) {
  2779. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, function_name);
  2780. }
  2781. #endif
  2782. }
  2783. }
  2784. if (types_match) {
  2785. for (int i = 0; i < p_call->arguments.size(); i++) {
  2786. GDScriptParser::DataType par_type = type_from_property(info.arguments[i], true);
  2787. if (p_call->arguments[i]->is_constant) {
  2788. update_const_expression_builtin_type(p_call->arguments[i], par_type, "pass");
  2789. }
  2790. #ifdef DEBUG_ENABLED
  2791. if (!(par_type.is_variant() && par_type.is_hard_type())) {
  2792. GDScriptParser::DataType arg_type = p_call->arguments[i]->get_datatype();
  2793. if (arg_type.is_variant() || !arg_type.is_hard_type()) {
  2794. mark_node_unsafe(p_call);
  2795. parser->push_warning(p_call->arguments[i], GDScriptWarning::UNSAFE_CALL_ARGUMENT, itos(i + 1), "constructor", function_name, par_type.to_string(), arg_type.to_string_strict());
  2796. }
  2797. }
  2798. #endif
  2799. }
  2800. match = true;
  2801. call_type = type_from_property(info.return_val);
  2802. break;
  2803. }
  2804. }
  2805. if (!match) {
  2806. String signature = Variant::get_type_name(builtin_type) + "(";
  2807. for (int i = 0; i < p_call->arguments.size(); i++) {
  2808. if (i > 0) {
  2809. signature += ", ";
  2810. }
  2811. signature += p_call->arguments[i]->get_datatype().to_string();
  2812. }
  2813. signature += ")";
  2814. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call);
  2815. }
  2816. }
  2817. p_call->set_datatype(call_type);
  2818. return;
  2819. } else if (GDScriptUtilityFunctions::function_exists(function_name)) {
  2820. MethodInfo function_info = GDScriptUtilityFunctions::get_function_info(function_name);
  2821. if (!p_is_root && !p_is_await && function_info.return_val.type == Variant::NIL && ((function_info.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) == 0)) {
  2822. push_error(vformat(R"*(Cannot get return value of call to "%s()" because it returns "void".)*", function_name), p_call);
  2823. }
  2824. if (all_is_constant && GDScriptUtilityFunctions::is_function_constant(function_name)) {
  2825. // Can call on compilation.
  2826. Vector<const Variant *> args;
  2827. for (int i = 0; i < p_call->arguments.size(); i++) {
  2828. args.push_back(&(p_call->arguments[i]->reduced_value));
  2829. }
  2830. Variant value;
  2831. Callable::CallError err;
  2832. GDScriptUtilityFunctions::get_function(function_name)(&value, (const Variant **)args.ptr(), args.size(), err);
  2833. switch (err.error) {
  2834. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  2835. if (value.get_type() == Variant::STRING && !value.operator String().is_empty()) {
  2836. push_error(vformat(R"*(Invalid argument for "%s()" function: %s)*", function_name, value), p_call->arguments[err.argument]);
  2837. } else {
  2838. // Do not use `type_from_property()` for expected type, since utility functions use their own checks.
  2839. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be "%s" but is "%s".)*", function_name, err.argument + 1,
  2840. Variant::get_type_name((Variant::Type)err.expected), p_call->arguments[err.argument]->get_datatype().to_string()),
  2841. p_call->arguments[err.argument]);
  2842. }
  2843. break;
  2844. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  2845. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  2846. break;
  2847. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2848. 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);
  2849. break;
  2850. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2851. 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);
  2852. break;
  2853. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  2854. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2855. break; // Can't happen in a builtin constructor.
  2856. case Callable::CallError::CALL_OK:
  2857. p_call->is_constant = true;
  2858. p_call->reduced_value = value;
  2859. break;
  2860. }
  2861. } else {
  2862. validate_call_arg(function_info, p_call);
  2863. }
  2864. p_call->set_datatype(type_from_property(function_info.return_val));
  2865. return;
  2866. } else if (Variant::has_utility_function(function_name)) {
  2867. MethodInfo function_info = info_from_utility_func(function_name);
  2868. if (!p_is_root && !p_is_await && function_info.return_val.type == Variant::NIL && ((function_info.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) == 0)) {
  2869. push_error(vformat(R"*(Cannot get return value of call to "%s()" because it returns "void".)*", function_name), p_call);
  2870. }
  2871. if (all_is_constant && Variant::get_utility_function_type(function_name) == Variant::UTILITY_FUNC_TYPE_MATH) {
  2872. // Can call on compilation.
  2873. Vector<const Variant *> args;
  2874. for (int i = 0; i < p_call->arguments.size(); i++) {
  2875. args.push_back(&(p_call->arguments[i]->reduced_value));
  2876. }
  2877. Variant value;
  2878. Callable::CallError err;
  2879. Variant::call_utility_function(function_name, &value, (const Variant **)args.ptr(), args.size(), err);
  2880. switch (err.error) {
  2881. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  2882. if (value.get_type() == Variant::STRING && !value.operator String().is_empty()) {
  2883. push_error(vformat(R"*(Invalid argument for "%s()" function: %s)*", function_name, value), p_call->arguments[err.argument]);
  2884. } else {
  2885. // Do not use `type_from_property()` for expected type, since utility functions use their own checks.
  2886. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be "%s" but is "%s".)*", function_name, err.argument + 1,
  2887. Variant::get_type_name((Variant::Type)err.expected), p_call->arguments[err.argument]->get_datatype().to_string()),
  2888. p_call->arguments[err.argument]);
  2889. }
  2890. break;
  2891. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  2892. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  2893. break;
  2894. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2895. 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);
  2896. break;
  2897. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2898. 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);
  2899. break;
  2900. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  2901. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2902. break; // Can't happen in a builtin constructor.
  2903. case Callable::CallError::CALL_OK:
  2904. p_call->is_constant = true;
  2905. p_call->reduced_value = value;
  2906. break;
  2907. }
  2908. } else {
  2909. validate_call_arg(function_info, p_call);
  2910. }
  2911. p_call->set_datatype(type_from_property(function_info.return_val));
  2912. return;
  2913. }
  2914. }
  2915. GDScriptParser::DataType base_type;
  2916. call_type.kind = GDScriptParser::DataType::VARIANT;
  2917. bool is_self = false;
  2918. if (p_call->is_super) {
  2919. base_type = parser->current_class->base_type;
  2920. base_type.is_meta_type = false;
  2921. is_self = true;
  2922. if (p_call->callee == nullptr && current_lambda != nullptr) {
  2923. push_error("Cannot use `super()` inside a lambda.", p_call);
  2924. }
  2925. } else if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  2926. base_type = parser->current_class->get_datatype();
  2927. base_type.is_meta_type = false;
  2928. is_self = true;
  2929. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  2930. GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee);
  2931. if (subscript->base == nullptr) {
  2932. // Invalid syntax, error already set on parser.
  2933. p_call->set_datatype(call_type);
  2934. mark_node_unsafe(p_call);
  2935. return;
  2936. }
  2937. if (!subscript->is_attribute) {
  2938. // Invalid call. Error already sent in parser.
  2939. // TODO: Could check if Callable here.
  2940. p_call->set_datatype(call_type);
  2941. mark_node_unsafe(p_call);
  2942. return;
  2943. }
  2944. if (subscript->attribute == nullptr) {
  2945. // Invalid call. Error already sent in parser.
  2946. p_call->set_datatype(call_type);
  2947. mark_node_unsafe(p_call);
  2948. return;
  2949. }
  2950. GDScriptParser::IdentifierNode *base_id = nullptr;
  2951. if (subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2952. base_id = static_cast<GDScriptParser::IdentifierNode *>(subscript->base);
  2953. }
  2954. if (base_id && GDScriptParser::get_builtin_type(base_id->name) < Variant::VARIANT_MAX) {
  2955. base_type = make_builtin_meta_type(GDScriptParser::get_builtin_type(base_id->name));
  2956. } else {
  2957. reduce_expression(subscript->base);
  2958. base_type = subscript->base->get_datatype();
  2959. is_self = subscript->base->type == GDScriptParser::Node::SELF;
  2960. }
  2961. } else {
  2962. // Invalid call. Error already sent in parser.
  2963. // TODO: Could check if Callable here too.
  2964. p_call->set_datatype(call_type);
  2965. mark_node_unsafe(p_call);
  2966. return;
  2967. }
  2968. int default_arg_count = 0;
  2969. BitField<MethodFlags> method_flags;
  2970. GDScriptParser::DataType return_type;
  2971. List<GDScriptParser::DataType> par_types;
  2972. bool is_constructor = (base_type.is_meta_type || (p_call->callee && p_call->callee->type == GDScriptParser::Node::IDENTIFIER)) && p_call->function_name == SNAME("new");
  2973. if (is_constructor && Engine::get_singleton()->has_singleton(base_type.native_type)) {
  2974. push_error(vformat(R"(Cannot construct native class "%s" because it is an engine singleton.)", base_type.native_type), p_call);
  2975. p_call->set_datatype(call_type);
  2976. return;
  2977. }
  2978. if (get_function_signature(p_call, is_constructor, base_type, p_call->function_name, return_type, par_types, default_arg_count, method_flags)) {
  2979. // If the method is implemented in the class hierarchy, the virtual flag will not be set for that MethodInfo and the search stops there.
  2980. // Virtual check only possible for super() calls because class hierarchy is known. Node/Objects may have scripts attached we don't know of at compile-time.
  2981. p_call->is_static = method_flags.has_flag(METHOD_FLAG_STATIC);
  2982. if (p_call->is_super && method_flags.has_flag(METHOD_FLAG_VIRTUAL)) {
  2983. push_error(vformat(R"*(Cannot call the parent class' virtual function "%s()" because it hasn't been defined.)*", p_call->function_name), p_call);
  2984. }
  2985. // If the function requires typed arrays we must make literals be typed.
  2986. for (const KeyValue<int, GDScriptParser::ArrayNode *> &E : arrays) {
  2987. int index = E.key;
  2988. if (index < par_types.size() && par_types[index].is_hard_type() && par_types[index].has_container_element_type(0)) {
  2989. update_array_literal_element_type(E.value, par_types[index].get_container_element_type(0));
  2990. }
  2991. }
  2992. validate_call_arg(par_types, default_arg_count, method_flags.has_flag(METHOD_FLAG_VARARG), p_call);
  2993. if (base_type.kind == GDScriptParser::DataType::ENUM && base_type.is_meta_type) {
  2994. // Enum type is treated as a dictionary value for function calls.
  2995. base_type.is_meta_type = false;
  2996. }
  2997. if (is_self && static_context && !p_call->is_static) {
  2998. // Get the parent function above any lambda.
  2999. GDScriptParser::FunctionNode *parent_function = parser->current_function;
  3000. while (parent_function && parent_function->source_lambda) {
  3001. parent_function = parent_function->source_lambda->parent_function;
  3002. }
  3003. if (parent_function) {
  3004. push_error(vformat(R"*(Cannot call non-static function "%s()" from the static function "%s()".)*", p_call->function_name, parent_function->identifier->name), p_call);
  3005. } else {
  3006. push_error(vformat(R"*(Cannot call non-static function "%s()" from a static variable initializer.)*", p_call->function_name), p_call);
  3007. }
  3008. } else if (!is_self && base_type.is_meta_type && !p_call->is_static) {
  3009. base_type.is_meta_type = false; // For `to_string()`.
  3010. 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);
  3011. } else if (is_self && !p_call->is_static) {
  3012. mark_lambda_use_self();
  3013. }
  3014. if (!p_is_root && !p_is_await && return_type.is_hard_type() && return_type.kind == GDScriptParser::DataType::BUILTIN && return_type.builtin_type == Variant::NIL) {
  3015. push_error(vformat(R"*(Cannot get return value of call to "%s()" because it returns "void".)*", p_call->function_name), p_call);
  3016. }
  3017. #ifdef DEBUG_ENABLED
  3018. if (p_is_root && return_type.kind != GDScriptParser::DataType::UNRESOLVED && return_type.builtin_type != Variant::NIL &&
  3019. !(p_call->is_super && p_call->function_name == GDScriptLanguage::get_singleton()->strings._init)) {
  3020. parser->push_warning(p_call, GDScriptWarning::RETURN_VALUE_DISCARDED, p_call->function_name);
  3021. }
  3022. if (method_flags.has_flag(METHOD_FLAG_STATIC) && !is_constructor && !base_type.is_meta_type && !is_self) {
  3023. String caller_type = base_type.to_string();
  3024. parser->push_warning(p_call, GDScriptWarning::STATIC_CALLED_ON_INSTANCE, p_call->function_name, caller_type);
  3025. }
  3026. #endif // DEBUG_ENABLED
  3027. call_type = return_type;
  3028. } else {
  3029. bool found = false;
  3030. // Enums do not have functions other than the built-in dictionary ones.
  3031. if (base_type.kind == GDScriptParser::DataType::ENUM && base_type.is_meta_type) {
  3032. if (base_type.builtin_type == Variant::DICTIONARY) {
  3033. push_error(vformat(R"*(Enums only have Dictionary built-in methods. Function "%s()" does not exist for enum "%s".)*", p_call->function_name, base_type.enum_type), p_call->callee);
  3034. } else {
  3035. push_error(vformat(R"*(The native enum "%s" does not behave like Dictionary and does not have methods of its own.)*", base_type.enum_type), p_call->callee);
  3036. }
  3037. } else if (!p_call->is_super && callee_type != GDScriptParser::Node::NONE) { // Check if the name exists as something else.
  3038. GDScriptParser::IdentifierNode *callee_id;
  3039. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  3040. callee_id = static_cast<GDScriptParser::IdentifierNode *>(p_call->callee);
  3041. } else {
  3042. // Can only be attribute.
  3043. callee_id = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee)->attribute;
  3044. }
  3045. if (callee_id) {
  3046. reduce_identifier_from_base(callee_id, &base_type);
  3047. GDScriptParser::DataType callee_datatype = callee_id->get_datatype();
  3048. if (callee_datatype.is_set() && !callee_datatype.is_variant()) {
  3049. found = true;
  3050. if (callee_datatype.builtin_type == Variant::CALLABLE) {
  3051. 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);
  3052. } else {
  3053. 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);
  3054. }
  3055. #ifdef DEBUG_ENABLED
  3056. } else if (!is_self && !(base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN)) {
  3057. parser->push_warning(p_call, GDScriptWarning::UNSAFE_METHOD_ACCESS, p_call->function_name, base_type.to_string());
  3058. mark_node_unsafe(p_call);
  3059. #endif
  3060. }
  3061. }
  3062. }
  3063. if (!found && (is_self || (base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN))) {
  3064. String base_name = is_self && !p_call->is_super ? "self" : base_type.to_string();
  3065. #ifdef SUGGEST_GODOT4_RENAMES
  3066. String rename_hint;
  3067. if (GLOBAL_GET(GDScriptWarning::get_settings_path_from_code(GDScriptWarning::RENAMED_IN_GODOT_4_HINT)).booleanize()) {
  3068. const char *renamed_function_name = check_for_renamed_identifier(p_call->function_name, p_call->type);
  3069. if (renamed_function_name) {
  3070. rename_hint = " " + vformat(R"(Did you mean to use "%s"?)", String(renamed_function_name) + "()");
  3071. }
  3072. }
  3073. push_error(vformat(R"*(Function "%s()" not found in base %s.%s)*", p_call->function_name, base_name, rename_hint), p_call->is_super ? p_call : p_call->callee);
  3074. #else
  3075. 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);
  3076. #endif // SUGGEST_GODOT4_RENAMES
  3077. } else if (!found && (!p_call->is_super && base_type.is_hard_type() && base_type.is_meta_type)) {
  3078. push_error(vformat(R"*(Static function "%s()" not found in base "%s".)*", p_call->function_name, base_type.to_string()), p_call);
  3079. }
  3080. }
  3081. if (call_type.is_coroutine && !p_is_await && !p_is_root) {
  3082. push_error(vformat(R"*(Function "%s()" is a coroutine, so it must be called with "await".)*", p_call->function_name), p_call);
  3083. }
  3084. p_call->set_datatype(call_type);
  3085. }
  3086. void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) {
  3087. reduce_expression(p_cast->operand);
  3088. GDScriptParser::DataType cast_type = type_from_metatype(resolve_datatype(p_cast->cast_type));
  3089. if (!cast_type.is_set()) {
  3090. mark_node_unsafe(p_cast);
  3091. return;
  3092. }
  3093. p_cast->set_datatype(cast_type);
  3094. if (p_cast->operand->is_constant) {
  3095. update_const_expression_builtin_type(p_cast->operand, cast_type, "cast", true);
  3096. if (cast_type.is_variant() || p_cast->operand->get_datatype() == cast_type) {
  3097. p_cast->is_constant = true;
  3098. p_cast->reduced_value = p_cast->operand->reduced_value;
  3099. }
  3100. }
  3101. if (p_cast->operand->type == GDScriptParser::Node::ARRAY && cast_type.has_container_element_type(0)) {
  3102. update_array_literal_element_type(static_cast<GDScriptParser::ArrayNode *>(p_cast->operand), cast_type.get_container_element_type(0));
  3103. }
  3104. if (!cast_type.is_variant()) {
  3105. GDScriptParser::DataType op_type = p_cast->operand->get_datatype();
  3106. if (op_type.is_variant() || !op_type.is_hard_type()) {
  3107. mark_node_unsafe(p_cast);
  3108. #ifdef DEBUG_ENABLED
  3109. parser->push_warning(p_cast, GDScriptWarning::UNSAFE_CAST, cast_type.to_string());
  3110. #endif
  3111. } else {
  3112. bool valid = false;
  3113. if (op_type.builtin_type == Variant::INT && cast_type.kind == GDScriptParser::DataType::ENUM) {
  3114. mark_node_unsafe(p_cast);
  3115. valid = true;
  3116. } else if (op_type.kind == GDScriptParser::DataType::ENUM && cast_type.builtin_type == Variant::INT) {
  3117. valid = true;
  3118. } else if (op_type.kind == GDScriptParser::DataType::BUILTIN && cast_type.kind == GDScriptParser::DataType::BUILTIN) {
  3119. valid = Variant::can_convert(op_type.builtin_type, cast_type.builtin_type);
  3120. } else if (op_type.kind != GDScriptParser::DataType::BUILTIN && cast_type.kind != GDScriptParser::DataType::BUILTIN) {
  3121. valid = is_type_compatible(cast_type, op_type) || is_type_compatible(op_type, cast_type);
  3122. }
  3123. if (!valid) {
  3124. push_error(vformat(R"(Invalid cast. Cannot convert from "%s" to "%s".)", op_type.to_string(), cast_type.to_string()), p_cast->cast_type);
  3125. }
  3126. }
  3127. }
  3128. }
  3129. void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  3130. HashMap<Variant, GDScriptParser::ExpressionNode *, VariantHasher, StringLikeVariantComparator> elements;
  3131. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  3132. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  3133. if (p_dictionary->style == GDScriptParser::DictionaryNode::PYTHON_DICT) {
  3134. reduce_expression(element.key);
  3135. }
  3136. reduce_expression(element.value);
  3137. if (element.key->is_constant) {
  3138. if (elements.has(element.key->reduced_value)) {
  3139. 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);
  3140. } else {
  3141. elements[element.key->reduced_value] = element.value;
  3142. }
  3143. }
  3144. }
  3145. // It's dictionary in any case.
  3146. GDScriptParser::DataType dict_type;
  3147. dict_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3148. dict_type.kind = GDScriptParser::DataType::BUILTIN;
  3149. dict_type.builtin_type = Variant::DICTIONARY;
  3150. dict_type.is_constant = true;
  3151. p_dictionary->set_datatype(dict_type);
  3152. }
  3153. void GDScriptAnalyzer::reduce_get_node(GDScriptParser::GetNodeNode *p_get_node) {
  3154. GDScriptParser::DataType result;
  3155. result.kind = GDScriptParser::DataType::VARIANT;
  3156. if (!ClassDB::is_parent_class(parser->current_class->base_type.native_type, SNAME("Node"))) {
  3157. push_error(vformat(R"*(Cannot use shorthand "get_node()" notation ("%c") on a class that isn't a node.)*", p_get_node->use_dollar ? '$' : '%'), p_get_node);
  3158. p_get_node->set_datatype(result);
  3159. return;
  3160. }
  3161. if (static_context) {
  3162. push_error(vformat(R"*(Cannot use shorthand "get_node()" notation ("%c") in a static function.)*", p_get_node->use_dollar ? '$' : '%'), p_get_node);
  3163. p_get_node->set_datatype(result);
  3164. return;
  3165. }
  3166. mark_lambda_use_self();
  3167. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3168. result.kind = GDScriptParser::DataType::NATIVE;
  3169. result.builtin_type = Variant::OBJECT;
  3170. result.native_type = SNAME("Node");
  3171. p_get_node->set_datatype(result);
  3172. }
  3173. GDScriptParser::DataType GDScriptAnalyzer::make_global_class_meta_type(const StringName &p_class_name, const GDScriptParser::Node *p_source) {
  3174. GDScriptParser::DataType type;
  3175. String path = ScriptServer::get_global_class_path(p_class_name);
  3176. String ext = path.get_extension();
  3177. if (ext == GDScriptLanguage::get_singleton()->get_extension()) {
  3178. Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(path);
  3179. if (ref.is_null()) {
  3180. push_error(vformat(R"(Could not find script for class "%s".)", p_class_name), p_source);
  3181. type.type_source = GDScriptParser::DataType::UNDETECTED;
  3182. type.kind = GDScriptParser::DataType::VARIANT;
  3183. return type;
  3184. }
  3185. Error err = ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  3186. if (err) {
  3187. push_error(vformat(R"(Could not resolve class "%s", because of a parser error.)", p_class_name), p_source);
  3188. type.type_source = GDScriptParser::DataType::UNDETECTED;
  3189. type.kind = GDScriptParser::DataType::VARIANT;
  3190. return type;
  3191. }
  3192. return ref->get_parser()->head->get_datatype();
  3193. } else {
  3194. return make_script_meta_type(ResourceLoader::load(path, "Script"));
  3195. }
  3196. }
  3197. void GDScriptAnalyzer::reduce_identifier_from_base_set_class(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType p_identifier_datatype) {
  3198. ERR_FAIL_NULL(p_identifier);
  3199. p_identifier->set_datatype(p_identifier_datatype);
  3200. Error err = OK;
  3201. Ref<GDScript> scr = GDScriptCache::get_shallow_script(p_identifier_datatype.script_path, err, parser->script_path);
  3202. if (err) {
  3203. push_error(vformat(R"(Error while getting cache for script "%s".)", p_identifier_datatype.script_path), p_identifier);
  3204. return;
  3205. }
  3206. p_identifier->reduced_value = scr->find_class(p_identifier_datatype.class_type->fqcn);
  3207. p_identifier->is_constant = true;
  3208. }
  3209. void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base) {
  3210. if (!p_identifier->get_datatype().has_no_type()) {
  3211. return;
  3212. }
  3213. GDScriptParser::DataType base;
  3214. if (p_base == nullptr) {
  3215. base = type_from_metatype(parser->current_class->get_datatype());
  3216. } else {
  3217. base = *p_base;
  3218. }
  3219. StringName name = p_identifier->name;
  3220. if (base.kind == GDScriptParser::DataType::ENUM) {
  3221. if (base.is_meta_type) {
  3222. if (base.enum_values.has(name)) {
  3223. p_identifier->set_datatype(type_from_metatype(base));
  3224. p_identifier->is_constant = true;
  3225. p_identifier->reduced_value = base.enum_values[name];
  3226. return;
  3227. }
  3228. // Enum does not have this value, return.
  3229. return;
  3230. } else {
  3231. push_error(R"(Cannot get property from enum value.)", p_identifier);
  3232. return;
  3233. }
  3234. }
  3235. if (base.kind == GDScriptParser::DataType::BUILTIN) {
  3236. if (base.is_meta_type) {
  3237. bool valid = true;
  3238. Variant result = Variant::get_constant_value(base.builtin_type, name, &valid);
  3239. if (valid) {
  3240. p_identifier->is_constant = true;
  3241. p_identifier->reduced_value = result;
  3242. p_identifier->set_datatype(type_from_variant(result, p_identifier));
  3243. } else if (base.is_hard_type()) {
  3244. #ifdef SUGGEST_GODOT4_RENAMES
  3245. String rename_hint;
  3246. if (GLOBAL_GET(GDScriptWarning::get_settings_path_from_code(GDScriptWarning::RENAMED_IN_GODOT_4_HINT)).booleanize()) {
  3247. const char *renamed_identifier_name = check_for_renamed_identifier(name, p_identifier->type);
  3248. if (renamed_identifier_name) {
  3249. rename_hint = " " + vformat(R"(Did you mean to use "%s"?)", renamed_identifier_name);
  3250. }
  3251. }
  3252. push_error(vformat(R"(Cannot find constant "%s" on base "%s".%s)", name, base.to_string(), rename_hint), p_identifier);
  3253. #else
  3254. push_error(vformat(R"(Cannot find constant "%s" on base "%s".)", name, base.to_string()), p_identifier);
  3255. #endif // SUGGEST_GODOT4_RENAMES
  3256. }
  3257. } else {
  3258. switch (base.builtin_type) {
  3259. case Variant::NIL: {
  3260. if (base.is_hard_type()) {
  3261. push_error(vformat(R"(Cannot get property "%s" on a null object.)", name), p_identifier);
  3262. }
  3263. return;
  3264. }
  3265. case Variant::DICTIONARY: {
  3266. GDScriptParser::DataType dummy;
  3267. dummy.kind = GDScriptParser::DataType::VARIANT;
  3268. p_identifier->set_datatype(dummy);
  3269. return;
  3270. }
  3271. default: {
  3272. Callable::CallError temp;
  3273. Variant dummy;
  3274. Variant::construct(base.builtin_type, dummy, nullptr, 0, temp);
  3275. List<PropertyInfo> properties;
  3276. dummy.get_property_list(&properties);
  3277. for (const PropertyInfo &prop : properties) {
  3278. if (prop.name == name) {
  3279. p_identifier->set_datatype(type_from_property(prop));
  3280. return;
  3281. }
  3282. }
  3283. if (Variant::has_builtin_method(base.builtin_type, name)) {
  3284. p_identifier->set_datatype(make_callable_type(Variant::get_builtin_method_info(base.builtin_type, name)));
  3285. return;
  3286. }
  3287. if (base.is_hard_type()) {
  3288. #ifdef SUGGEST_GODOT4_RENAMES
  3289. String rename_hint;
  3290. if (GLOBAL_GET(GDScriptWarning::get_settings_path_from_code(GDScriptWarning::RENAMED_IN_GODOT_4_HINT)).booleanize()) {
  3291. const char *renamed_identifier_name = check_for_renamed_identifier(name, p_identifier->type);
  3292. if (renamed_identifier_name) {
  3293. rename_hint = " " + vformat(R"(Did you mean to use "%s"?)", renamed_identifier_name);
  3294. }
  3295. }
  3296. push_error(vformat(R"(Cannot find property "%s" on base "%s".%s)", name, base.to_string(), rename_hint), p_identifier);
  3297. #else
  3298. push_error(vformat(R"(Cannot find property "%s" on base "%s".)", name, base.to_string()), p_identifier);
  3299. #endif // SUGGEST_GODOT4_RENAMES
  3300. }
  3301. }
  3302. }
  3303. }
  3304. return;
  3305. }
  3306. GDScriptParser::ClassNode *base_class = base.class_type;
  3307. List<GDScriptParser::ClassNode *> script_classes;
  3308. bool is_base = true;
  3309. if (base_class != nullptr) {
  3310. get_class_node_current_scope_classes(base_class, &script_classes);
  3311. }
  3312. bool is_constructor = base.is_meta_type && p_identifier->name == SNAME("new");
  3313. for (GDScriptParser::ClassNode *script_class : script_classes) {
  3314. if (p_base == nullptr && script_class->identifier && script_class->identifier->name == name) {
  3315. reduce_identifier_from_base_set_class(p_identifier, script_class->get_datatype());
  3316. if (script_class->outer != nullptr) {
  3317. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CLASS;
  3318. }
  3319. return;
  3320. }
  3321. if (is_constructor) {
  3322. name = "_init";
  3323. }
  3324. if (script_class->has_member(name)) {
  3325. resolve_class_member(script_class, name, p_identifier);
  3326. GDScriptParser::ClassNode::Member member = script_class->get_member(name);
  3327. switch (member.type) {
  3328. case GDScriptParser::ClassNode::Member::CONSTANT: {
  3329. p_identifier->set_datatype(member.get_datatype());
  3330. p_identifier->is_constant = true;
  3331. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  3332. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  3333. p_identifier->constant_source = member.constant;
  3334. return;
  3335. }
  3336. case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
  3337. p_identifier->set_datatype(member.get_datatype());
  3338. p_identifier->is_constant = true;
  3339. p_identifier->reduced_value = member.enum_value.value;
  3340. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  3341. return;
  3342. }
  3343. case GDScriptParser::ClassNode::Member::ENUM: {
  3344. p_identifier->set_datatype(member.get_datatype());
  3345. p_identifier->is_constant = true;
  3346. p_identifier->reduced_value = member.m_enum->dictionary;
  3347. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  3348. return;
  3349. }
  3350. case GDScriptParser::ClassNode::Member::VARIABLE: {
  3351. if (is_base && (!base.is_meta_type || member.variable->is_static)) {
  3352. p_identifier->set_datatype(member.get_datatype());
  3353. p_identifier->source = member.variable->is_static ? GDScriptParser::IdentifierNode::STATIC_VARIABLE : GDScriptParser::IdentifierNode::MEMBER_VARIABLE;
  3354. p_identifier->variable_source = member.variable;
  3355. member.variable->usages += 1;
  3356. return;
  3357. }
  3358. } break;
  3359. case GDScriptParser::ClassNode::Member::SIGNAL: {
  3360. if (is_base && !base.is_meta_type) {
  3361. p_identifier->set_datatype(member.get_datatype());
  3362. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_SIGNAL;
  3363. p_identifier->signal_source = member.signal;
  3364. member.signal->usages += 1;
  3365. return;
  3366. }
  3367. } break;
  3368. case GDScriptParser::ClassNode::Member::FUNCTION: {
  3369. if (is_base && (!base.is_meta_type || member.function->is_static || is_constructor)) {
  3370. p_identifier->set_datatype(make_callable_type(member.function->info));
  3371. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_FUNCTION;
  3372. p_identifier->function_source = member.function;
  3373. p_identifier->function_source_is_static = member.function->is_static;
  3374. return;
  3375. }
  3376. } break;
  3377. case GDScriptParser::ClassNode::Member::CLASS: {
  3378. reduce_identifier_from_base_set_class(p_identifier, member.get_datatype());
  3379. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CLASS;
  3380. return;
  3381. }
  3382. default: {
  3383. // Do nothing
  3384. }
  3385. }
  3386. }
  3387. if (is_base) {
  3388. is_base = script_class->base_type.class_type != nullptr;
  3389. if (!is_base && p_base != nullptr) {
  3390. break;
  3391. }
  3392. }
  3393. }
  3394. // Check non-GDScript scripts.
  3395. Ref<Script> script_type = base.script_type;
  3396. if (base_class == nullptr && script_type.is_valid()) {
  3397. List<PropertyInfo> property_list;
  3398. script_type->get_script_property_list(&property_list);
  3399. for (const PropertyInfo &property_info : property_list) {
  3400. if (property_info.name != p_identifier->name) {
  3401. continue;
  3402. }
  3403. const GDScriptParser::DataType property_type = GDScriptAnalyzer::type_from_property(property_info, false, false);
  3404. p_identifier->set_datatype(property_type);
  3405. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_VARIABLE;
  3406. return;
  3407. }
  3408. MethodInfo method_info = script_type->get_method_info(p_identifier->name);
  3409. if (method_info.name == p_identifier->name) {
  3410. p_identifier->set_datatype(make_callable_type(method_info));
  3411. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_FUNCTION;
  3412. p_identifier->function_source_is_static = method_info.flags & METHOD_FLAG_STATIC;
  3413. return;
  3414. }
  3415. List<MethodInfo> signal_list;
  3416. script_type->get_script_signal_list(&signal_list);
  3417. for (const MethodInfo &signal_info : signal_list) {
  3418. if (signal_info.name != p_identifier->name) {
  3419. continue;
  3420. }
  3421. const GDScriptParser::DataType signal_type = make_signal_type(signal_info);
  3422. p_identifier->set_datatype(signal_type);
  3423. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_SIGNAL;
  3424. return;
  3425. }
  3426. HashMap<StringName, Variant> constant_map;
  3427. script_type->get_constants(&constant_map);
  3428. if (constant_map.has(p_identifier->name)) {
  3429. Variant constant = constant_map.get(p_identifier->name);
  3430. p_identifier->set_datatype(make_builtin_meta_type(constant.get_type()));
  3431. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  3432. return;
  3433. }
  3434. }
  3435. // Check native members. No need for native class recursion because Node exposes all Object's properties.
  3436. const StringName &native = base.native_type;
  3437. if (class_exists(native)) {
  3438. if (is_constructor) {
  3439. name = "_init";
  3440. }
  3441. MethodInfo method_info;
  3442. if (ClassDB::has_property(native, name)) {
  3443. StringName getter_name = ClassDB::get_property_getter(native, name);
  3444. MethodBind *getter = ClassDB::get_method(native, getter_name);
  3445. if (getter != nullptr) {
  3446. bool has_setter = ClassDB::get_property_setter(native, name) != StringName();
  3447. p_identifier->set_datatype(type_from_property(getter->get_return_info(), false, !has_setter));
  3448. p_identifier->source = GDScriptParser::IdentifierNode::INHERITED_VARIABLE;
  3449. }
  3450. return;
  3451. }
  3452. if (ClassDB::get_method_info(native, name, &method_info)) {
  3453. // Method is callable.
  3454. p_identifier->set_datatype(make_callable_type(method_info));
  3455. p_identifier->source = GDScriptParser::IdentifierNode::INHERITED_VARIABLE;
  3456. return;
  3457. }
  3458. if (ClassDB::get_signal(native, name, &method_info)) {
  3459. // Signal is a type too.
  3460. p_identifier->set_datatype(make_signal_type(method_info));
  3461. p_identifier->source = GDScriptParser::IdentifierNode::INHERITED_VARIABLE;
  3462. return;
  3463. }
  3464. if (ClassDB::has_enum(native, name)) {
  3465. p_identifier->set_datatype(make_native_enum_type(name, native));
  3466. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  3467. return;
  3468. }
  3469. bool valid = false;
  3470. int64_t int_constant = ClassDB::get_integer_constant(native, name, &valid);
  3471. if (valid) {
  3472. p_identifier->is_constant = true;
  3473. p_identifier->reduced_value = int_constant;
  3474. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  3475. // Check whether this constant, which exists, belongs to an enum
  3476. StringName enum_name = ClassDB::get_integer_constant_enum(native, name);
  3477. if (enum_name != StringName()) {
  3478. p_identifier->set_datatype(make_native_enum_type(enum_name, native, false));
  3479. } else {
  3480. p_identifier->set_datatype(type_from_variant(int_constant, p_identifier));
  3481. }
  3482. }
  3483. }
  3484. }
  3485. void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin) {
  3486. // TODO: This is an opportunity to further infer types.
  3487. // Check if we are inside an enum. This allows enum values to access other elements of the same enum.
  3488. if (current_enum) {
  3489. for (int i = 0; i < current_enum->values.size(); i++) {
  3490. const GDScriptParser::EnumNode::Value &element = current_enum->values[i];
  3491. if (element.identifier->name == p_identifier->name) {
  3492. StringName enum_name = current_enum->identifier ? current_enum->identifier->name : UNNAMED_ENUM;
  3493. GDScriptParser::DataType type = make_enum_type(enum_name, parser->current_class->fqcn, false);
  3494. if (element.parent_enum->identifier) {
  3495. type.enum_type = element.parent_enum->identifier->name;
  3496. }
  3497. p_identifier->set_datatype(type);
  3498. if (element.resolved) {
  3499. p_identifier->is_constant = true;
  3500. p_identifier->reduced_value = element.value;
  3501. } else {
  3502. push_error(R"(Cannot use another enum element before it was declared.)", p_identifier);
  3503. }
  3504. return; // Found anyway.
  3505. }
  3506. }
  3507. }
  3508. bool found_source = false;
  3509. // Check if identifier is local.
  3510. // If that's the case, the declaration already was solved before.
  3511. switch (p_identifier->source) {
  3512. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER:
  3513. p_identifier->set_datatype(p_identifier->parameter_source->get_datatype());
  3514. found_source = true;
  3515. break;
  3516. case GDScriptParser::IdentifierNode::LOCAL_CONSTANT:
  3517. case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
  3518. p_identifier->set_datatype(p_identifier->constant_source->get_datatype());
  3519. p_identifier->is_constant = true;
  3520. // TODO: Constant should have a value on the node itself.
  3521. p_identifier->reduced_value = p_identifier->constant_source->initializer->reduced_value;
  3522. found_source = true;
  3523. break;
  3524. case GDScriptParser::IdentifierNode::MEMBER_SIGNAL:
  3525. p_identifier->signal_source->usages++;
  3526. [[fallthrough]];
  3527. case GDScriptParser::IdentifierNode::INHERITED_VARIABLE:
  3528. mark_lambda_use_self();
  3529. break;
  3530. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
  3531. mark_lambda_use_self();
  3532. p_identifier->variable_source->usages++;
  3533. [[fallthrough]];
  3534. case GDScriptParser::IdentifierNode::STATIC_VARIABLE:
  3535. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE:
  3536. p_identifier->set_datatype(p_identifier->variable_source->get_datatype());
  3537. found_source = true;
  3538. #ifdef DEBUG_ENABLED
  3539. if (p_identifier->variable_source && p_identifier->variable_source->assignments == 0 && !(p_identifier->get_datatype().is_hard_type() && p_identifier->get_datatype().kind == GDScriptParser::DataType::BUILTIN)) {
  3540. parser->push_warning(p_identifier, GDScriptWarning::UNASSIGNED_VARIABLE, p_identifier->name);
  3541. }
  3542. #endif
  3543. break;
  3544. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR:
  3545. p_identifier->set_datatype(p_identifier->bind_source->get_datatype());
  3546. found_source = true;
  3547. break;
  3548. case GDScriptParser::IdentifierNode::LOCAL_BIND: {
  3549. GDScriptParser::DataType result = p_identifier->bind_source->get_datatype();
  3550. result.is_constant = true;
  3551. p_identifier->set_datatype(result);
  3552. found_source = true;
  3553. } break;
  3554. case GDScriptParser::IdentifierNode::UNDEFINED_SOURCE:
  3555. case GDScriptParser::IdentifierNode::MEMBER_FUNCTION:
  3556. case GDScriptParser::IdentifierNode::MEMBER_CLASS:
  3557. break;
  3558. }
  3559. #ifdef DEBUG_ENABLED
  3560. if (!found_source && p_identifier->suite != nullptr && p_identifier->suite->has_local(p_identifier->name)) {
  3561. parser->push_warning(p_identifier, GDScriptWarning::CONFUSABLE_LOCAL_USAGE, p_identifier->name);
  3562. }
  3563. #endif
  3564. // Not a local, so check members.
  3565. if (!found_source) {
  3566. reduce_identifier_from_base(p_identifier);
  3567. if (p_identifier->source != GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->get_datatype().is_set()) {
  3568. // Found.
  3569. found_source = true;
  3570. }
  3571. }
  3572. if (found_source) {
  3573. const bool source_is_instance_variable = p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_VARIABLE || p_identifier->source == GDScriptParser::IdentifierNode::INHERITED_VARIABLE;
  3574. const bool source_is_instance_function = p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_FUNCTION && !p_identifier->function_source_is_static;
  3575. const bool source_is_signal = p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_SIGNAL;
  3576. if (static_context && (source_is_instance_variable || source_is_instance_function || source_is_signal)) {
  3577. // Get the parent function above any lambda.
  3578. GDScriptParser::FunctionNode *parent_function = parser->current_function;
  3579. while (parent_function && parent_function->source_lambda) {
  3580. parent_function = parent_function->source_lambda->parent_function;
  3581. }
  3582. String source_type;
  3583. if (source_is_instance_variable) {
  3584. source_type = "non-static variable";
  3585. } else if (source_is_instance_function) {
  3586. source_type = "non-static function";
  3587. } else { // source_is_signal
  3588. source_type = "signal";
  3589. }
  3590. if (parent_function) {
  3591. push_error(vformat(R"*(Cannot access %s "%s" from the static function "%s()".)*", source_type, p_identifier->name, parent_function->identifier->name), p_identifier);
  3592. } else {
  3593. push_error(vformat(R"*(Cannot access %s "%s" from a static variable initializer.)*", source_type, p_identifier->name), p_identifier);
  3594. }
  3595. }
  3596. if (current_lambda != nullptr) {
  3597. // If the identifier is a member variable (including the native class properties), member function, or a signal,
  3598. // we consider the lambda to be using `self`, so we keep a reference to the current instance.
  3599. if (source_is_instance_variable || source_is_instance_function || source_is_signal) {
  3600. mark_lambda_use_self();
  3601. return; // No need to capture.
  3602. }
  3603. // If the identifier is local, check if it's any kind of capture by comparing their source function.
  3604. // Only capture locals and enum values. Constants are still accessible from the lambda using the script reference. If not, this method is done.
  3605. if (p_identifier->source == GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_CONSTANT) {
  3606. return;
  3607. }
  3608. GDScriptParser::FunctionNode *function_test = current_lambda->function;
  3609. // Make sure we aren't capturing variable in the same lambda.
  3610. // This also add captures for nested lambdas.
  3611. 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)) {
  3612. function_test->source_lambda->captures_indices[p_identifier->name] = function_test->source_lambda->captures.size();
  3613. function_test->source_lambda->captures.push_back(p_identifier);
  3614. function_test = function_test->source_lambda->parent_function;
  3615. }
  3616. }
  3617. return;
  3618. }
  3619. StringName name = p_identifier->name;
  3620. p_identifier->source = GDScriptParser::IdentifierNode::UNDEFINED_SOURCE;
  3621. // Not a local or a member, so check globals.
  3622. Variant::Type builtin_type = GDScriptParser::get_builtin_type(name);
  3623. if (builtin_type < Variant::VARIANT_MAX) {
  3624. if (can_be_builtin) {
  3625. p_identifier->set_datatype(make_builtin_meta_type(builtin_type));
  3626. return;
  3627. } else {
  3628. push_error(R"(Builtin type cannot be used as a name on its own.)", p_identifier);
  3629. }
  3630. }
  3631. if (class_exists(name)) {
  3632. p_identifier->set_datatype(make_native_meta_type(name));
  3633. return;
  3634. }
  3635. if (ScriptServer::is_global_class(name)) {
  3636. p_identifier->set_datatype(make_global_class_meta_type(name, p_identifier));
  3637. parser->add_dependency(p_identifier->get_datatype().script_path);
  3638. return;
  3639. }
  3640. // Try singletons.
  3641. // Do this before globals because this might be a singleton loading another one before it's compiled.
  3642. if (ProjectSettings::get_singleton()->has_autoload(name)) {
  3643. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(name);
  3644. if (autoload.is_singleton) {
  3645. // Singleton exists, so it's at least a Node.
  3646. GDScriptParser::DataType result;
  3647. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3648. result.kind = GDScriptParser::DataType::NATIVE;
  3649. result.builtin_type = Variant::OBJECT;
  3650. result.native_type = SNAME("Node");
  3651. if (ResourceLoader::get_resource_type(autoload.path) == "GDScript") {
  3652. Ref<GDScriptParserRef> singl_parser = parser->get_depended_parser_for(autoload.path);
  3653. if (singl_parser.is_valid()) {
  3654. Error err = singl_parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  3655. if (err == OK) {
  3656. result = type_from_metatype(singl_parser->get_parser()->head->get_datatype());
  3657. }
  3658. }
  3659. } else if (ResourceLoader::get_resource_type(autoload.path) == "PackedScene") {
  3660. if (GDScriptLanguage::get_singleton()->has_any_global_constant(name)) {
  3661. Variant constant = GDScriptLanguage::get_singleton()->get_any_global_constant(name);
  3662. Node *node = Object::cast_to<Node>(constant);
  3663. if (node != nullptr) {
  3664. Ref<GDScript> scr = node->get_script();
  3665. if (scr.is_valid()) {
  3666. Ref<GDScriptParserRef> singl_parser = parser->get_depended_parser_for(scr->get_script_path());
  3667. if (singl_parser.is_valid()) {
  3668. Error err = singl_parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  3669. if (err == OK) {
  3670. result = type_from_metatype(singl_parser->get_parser()->head->get_datatype());
  3671. }
  3672. }
  3673. }
  3674. }
  3675. }
  3676. }
  3677. result.is_constant = true;
  3678. p_identifier->set_datatype(result);
  3679. parser->add_dependency(autoload.path);
  3680. return;
  3681. }
  3682. }
  3683. if (CoreConstants::is_global_constant(name)) {
  3684. int index = CoreConstants::get_global_constant_index(name);
  3685. StringName enum_name = CoreConstants::get_global_constant_enum(index);
  3686. int64_t value = CoreConstants::get_global_constant_value(index);
  3687. if (enum_name != StringName()) {
  3688. p_identifier->set_datatype(make_global_enum_type(enum_name, StringName(), false));
  3689. } else {
  3690. p_identifier->set_datatype(type_from_variant(value, p_identifier));
  3691. }
  3692. p_identifier->is_constant = true;
  3693. p_identifier->reduced_value = value;
  3694. return;
  3695. }
  3696. if (GDScriptLanguage::get_singleton()->has_any_global_constant(name)) {
  3697. Variant constant = GDScriptLanguage::get_singleton()->get_any_global_constant(name);
  3698. p_identifier->set_datatype(type_from_variant(constant, p_identifier));
  3699. p_identifier->is_constant = true;
  3700. p_identifier->reduced_value = constant;
  3701. return;
  3702. }
  3703. if (CoreConstants::is_global_enum(name)) {
  3704. p_identifier->set_datatype(make_global_enum_type(name, StringName(), true));
  3705. if (!can_be_builtin) {
  3706. push_error(vformat(R"(Global enum "%s" cannot be used on its own.)", name), p_identifier);
  3707. }
  3708. return;
  3709. }
  3710. if (Variant::has_utility_function(name) || GDScriptUtilityFunctions::function_exists(name)) {
  3711. p_identifier->is_constant = true;
  3712. p_identifier->reduced_value = Callable(memnew(GDScriptUtilityCallable(name)));
  3713. MethodInfo method_info;
  3714. if (GDScriptUtilityFunctions::function_exists(name)) {
  3715. method_info = GDScriptUtilityFunctions::get_function_info(name);
  3716. } else {
  3717. method_info = Variant::get_utility_function_info(name);
  3718. }
  3719. p_identifier->set_datatype(make_callable_type(method_info));
  3720. return;
  3721. }
  3722. // Allow "Variant" here since it might be used for nested enums.
  3723. if (can_be_builtin && name == SNAME("Variant")) {
  3724. GDScriptParser::DataType variant;
  3725. variant.kind = GDScriptParser::DataType::VARIANT;
  3726. variant.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3727. variant.is_meta_type = true;
  3728. variant.is_pseudo_type = true;
  3729. p_identifier->set_datatype(variant);
  3730. return;
  3731. }
  3732. // Not found.
  3733. #ifdef SUGGEST_GODOT4_RENAMES
  3734. String rename_hint;
  3735. if (GLOBAL_GET(GDScriptWarning::get_settings_path_from_code(GDScriptWarning::RENAMED_IN_GODOT_4_HINT)).booleanize()) {
  3736. const char *renamed_identifier_name = check_for_renamed_identifier(name, p_identifier->type);
  3737. if (renamed_identifier_name) {
  3738. rename_hint = " " + vformat(R"(Did you mean to use "%s"?)", renamed_identifier_name);
  3739. }
  3740. }
  3741. push_error(vformat(R"(Identifier "%s" not declared in the current scope.%s)", name, rename_hint), p_identifier);
  3742. #else
  3743. push_error(vformat(R"(Identifier "%s" not declared in the current scope.)", name), p_identifier);
  3744. #endif // SUGGEST_GODOT4_RENAMES
  3745. GDScriptParser::DataType dummy;
  3746. dummy.kind = GDScriptParser::DataType::VARIANT;
  3747. p_identifier->set_datatype(dummy); // Just so type is set to something.
  3748. }
  3749. void GDScriptAnalyzer::reduce_lambda(GDScriptParser::LambdaNode *p_lambda) {
  3750. // Lambda is always a Callable.
  3751. GDScriptParser::DataType lambda_type;
  3752. lambda_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  3753. lambda_type.kind = GDScriptParser::DataType::BUILTIN;
  3754. lambda_type.builtin_type = Variant::CALLABLE;
  3755. p_lambda->set_datatype(lambda_type);
  3756. if (p_lambda->function == nullptr) {
  3757. return;
  3758. }
  3759. GDScriptParser::LambdaNode *previous_lambda = current_lambda;
  3760. current_lambda = p_lambda;
  3761. resolve_function_signature(p_lambda->function, p_lambda, true);
  3762. current_lambda = previous_lambda;
  3763. pending_body_resolution_lambdas.push_back(p_lambda);
  3764. }
  3765. void GDScriptAnalyzer::reduce_literal(GDScriptParser::LiteralNode *p_literal) {
  3766. p_literal->reduced_value = p_literal->value;
  3767. p_literal->is_constant = true;
  3768. p_literal->set_datatype(type_from_variant(p_literal->reduced_value, p_literal));
  3769. }
  3770. void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) {
  3771. if (!p_preload->path) {
  3772. return;
  3773. }
  3774. reduce_expression(p_preload->path);
  3775. if (!p_preload->path->is_constant) {
  3776. push_error("Preloaded path must be a constant string.", p_preload->path);
  3777. return;
  3778. }
  3779. if (p_preload->path->reduced_value.get_type() != Variant::STRING) {
  3780. push_error("Preloaded path must be a constant string.", p_preload->path);
  3781. } else {
  3782. p_preload->resolved_path = p_preload->path->reduced_value;
  3783. if (p_preload->resolved_path.is_relative_path()) {
  3784. p_preload->resolved_path = parser->script_path.get_base_dir().path_join(p_preload->resolved_path);
  3785. }
  3786. p_preload->resolved_path = p_preload->resolved_path.simplify_path();
  3787. if (!ResourceLoader::exists(p_preload->resolved_path)) {
  3788. Ref<FileAccess> file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  3789. if (file_check->file_exists(p_preload->resolved_path)) {
  3790. push_error(vformat(R"(Preload file "%s" has no resource loaders (unrecognized file extension).)", p_preload->resolved_path), p_preload->path);
  3791. } else {
  3792. push_error(vformat(R"(Preload file "%s" does not exist.)", p_preload->resolved_path), p_preload->path);
  3793. }
  3794. } else {
  3795. // TODO: Don't load if validating: use completion cache.
  3796. // Must load GDScript separately to permit cyclic references
  3797. // as ResourceLoader::load() detects and rejects those.
  3798. if (ResourceLoader::get_resource_type(p_preload->resolved_path) == "GDScript") {
  3799. Error err = OK;
  3800. Ref<GDScript> res = GDScriptCache::get_shallow_script(p_preload->resolved_path, err, parser->script_path);
  3801. p_preload->resource = res;
  3802. if (err != OK) {
  3803. push_error(vformat(R"(Could not preload resource script "%s".)", p_preload->resolved_path), p_preload->path);
  3804. }
  3805. } else {
  3806. p_preload->resource = ResourceLoader::load(p_preload->resolved_path);
  3807. if (p_preload->resource.is_null()) {
  3808. push_error(vformat(R"(Could not preload resource file "%s".)", p_preload->resolved_path), p_preload->path);
  3809. }
  3810. }
  3811. parser->add_dependency(p_preload->resolved_path);
  3812. }
  3813. }
  3814. p_preload->is_constant = true;
  3815. p_preload->reduced_value = p_preload->resource;
  3816. p_preload->set_datatype(type_from_variant(p_preload->reduced_value, p_preload));
  3817. }
  3818. void GDScriptAnalyzer::reduce_self(GDScriptParser::SelfNode *p_self) {
  3819. p_self->is_constant = false;
  3820. p_self->set_datatype(type_from_metatype(parser->current_class->get_datatype()));
  3821. mark_lambda_use_self();
  3822. }
  3823. void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscript, bool p_can_be_pseudo_type) {
  3824. if (p_subscript->base == nullptr) {
  3825. return;
  3826. }
  3827. if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  3828. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base), true);
  3829. } else if (p_subscript->base->type == GDScriptParser::Node::SUBSCRIPT) {
  3830. reduce_subscript(static_cast<GDScriptParser::SubscriptNode *>(p_subscript->base), true);
  3831. } else {
  3832. reduce_expression(p_subscript->base);
  3833. }
  3834. GDScriptParser::DataType result_type;
  3835. if (p_subscript->is_attribute) {
  3836. if (p_subscript->attribute == nullptr) {
  3837. return;
  3838. }
  3839. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  3840. bool valid = false;
  3841. // If the base is a metatype, use the analyzer instead.
  3842. if (p_subscript->base->is_constant && !base_type.is_meta_type && base_type.kind != GDScriptParser::DataType::CLASS) {
  3843. // Just try to get it.
  3844. Variant value = p_subscript->base->reduced_value.get_named(p_subscript->attribute->name, valid);
  3845. if (valid) {
  3846. p_subscript->is_constant = true;
  3847. p_subscript->reduced_value = value;
  3848. result_type = type_from_variant(value, p_subscript);
  3849. }
  3850. } else if (base_type.is_variant() || !base_type.is_hard_type()) {
  3851. valid = !base_type.is_pseudo_type || p_can_be_pseudo_type;
  3852. result_type.kind = GDScriptParser::DataType::VARIANT;
  3853. if (base_type.is_variant() && base_type.is_hard_type() && base_type.is_meta_type && base_type.is_pseudo_type) {
  3854. // Special case: it may be a global enum with pseudo base (e.g. Variant.Type).
  3855. String enum_name;
  3856. if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  3857. enum_name = String(static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base)->name) + ENUM_SEPARATOR + String(p_subscript->attribute->name);
  3858. }
  3859. if (CoreConstants::is_global_enum(enum_name)) {
  3860. result_type = make_global_enum_type(enum_name, StringName());
  3861. } else {
  3862. valid = false;
  3863. mark_node_unsafe(p_subscript);
  3864. }
  3865. } else {
  3866. mark_node_unsafe(p_subscript);
  3867. }
  3868. } else {
  3869. reduce_identifier_from_base(p_subscript->attribute, &base_type);
  3870. GDScriptParser::DataType attr_type = p_subscript->attribute->get_datatype();
  3871. if (attr_type.is_set()) {
  3872. valid = !attr_type.is_pseudo_type || p_can_be_pseudo_type;
  3873. result_type = attr_type;
  3874. p_subscript->is_constant = p_subscript->attribute->is_constant;
  3875. p_subscript->reduced_value = p_subscript->attribute->reduced_value;
  3876. } else if (!base_type.is_meta_type || !base_type.is_constant) {
  3877. valid = base_type.kind != GDScriptParser::DataType::BUILTIN;
  3878. #ifdef DEBUG_ENABLED
  3879. if (valid) {
  3880. parser->push_warning(p_subscript, GDScriptWarning::UNSAFE_PROPERTY_ACCESS, p_subscript->attribute->name, base_type.to_string());
  3881. }
  3882. #endif
  3883. result_type.kind = GDScriptParser::DataType::VARIANT;
  3884. mark_node_unsafe(p_subscript);
  3885. }
  3886. }
  3887. if (!valid) {
  3888. GDScriptParser::DataType attr_type = p_subscript->attribute->get_datatype();
  3889. if (!p_can_be_pseudo_type && (attr_type.is_pseudo_type || result_type.is_pseudo_type)) {
  3890. push_error(vformat(R"(Type "%s" in base "%s" cannot be used on its own.)", p_subscript->attribute->name, type_from_metatype(base_type).to_string()), p_subscript->attribute);
  3891. } else {
  3892. 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);
  3893. }
  3894. result_type.kind = GDScriptParser::DataType::VARIANT;
  3895. }
  3896. } else {
  3897. if (p_subscript->index == nullptr) {
  3898. return;
  3899. }
  3900. reduce_expression(p_subscript->index);
  3901. if (p_subscript->base->is_constant && p_subscript->index->is_constant) {
  3902. // Just try to get it.
  3903. bool valid = false;
  3904. Variant value = p_subscript->base->reduced_value.get(p_subscript->index->reduced_value, &valid);
  3905. if (!valid) {
  3906. push_error(vformat(R"(Cannot get index "%s" from "%s".)", p_subscript->index->reduced_value, p_subscript->base->reduced_value), p_subscript->index);
  3907. result_type.kind = GDScriptParser::DataType::VARIANT;
  3908. } else {
  3909. p_subscript->is_constant = true;
  3910. p_subscript->reduced_value = value;
  3911. result_type = type_from_variant(value, p_subscript);
  3912. }
  3913. } else {
  3914. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  3915. GDScriptParser::DataType index_type = p_subscript->index->get_datatype();
  3916. if (base_type.is_variant()) {
  3917. result_type.kind = GDScriptParser::DataType::VARIANT;
  3918. mark_node_unsafe(p_subscript);
  3919. } else {
  3920. if (base_type.kind == GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  3921. // Check if indexing is valid.
  3922. bool error = index_type.kind != GDScriptParser::DataType::BUILTIN && base_type.builtin_type != Variant::DICTIONARY;
  3923. if (!error) {
  3924. switch (base_type.builtin_type) {
  3925. // Expect int or real as index.
  3926. case Variant::PACKED_BYTE_ARRAY:
  3927. case Variant::PACKED_FLOAT32_ARRAY:
  3928. case Variant::PACKED_FLOAT64_ARRAY:
  3929. case Variant::PACKED_INT32_ARRAY:
  3930. case Variant::PACKED_INT64_ARRAY:
  3931. case Variant::PACKED_STRING_ARRAY:
  3932. case Variant::PACKED_VECTOR2_ARRAY:
  3933. case Variant::PACKED_VECTOR3_ARRAY:
  3934. case Variant::PACKED_COLOR_ARRAY:
  3935. case Variant::PACKED_VECTOR4_ARRAY:
  3936. case Variant::ARRAY:
  3937. case Variant::STRING:
  3938. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT;
  3939. break;
  3940. // Expect String only.
  3941. case Variant::RECT2:
  3942. case Variant::RECT2I:
  3943. case Variant::PLANE:
  3944. case Variant::QUATERNION:
  3945. case Variant::AABB:
  3946. case Variant::OBJECT:
  3947. error = index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME;
  3948. break;
  3949. // Expect String or number.
  3950. case Variant::BASIS:
  3951. case Variant::VECTOR2:
  3952. case Variant::VECTOR2I:
  3953. case Variant::VECTOR3:
  3954. case Variant::VECTOR3I:
  3955. case Variant::VECTOR4:
  3956. case Variant::VECTOR4I:
  3957. case Variant::TRANSFORM2D:
  3958. case Variant::TRANSFORM3D:
  3959. case Variant::PROJECTION:
  3960. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT &&
  3961. index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME;
  3962. break;
  3963. // Expect String or int.
  3964. case Variant::COLOR:
  3965. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME;
  3966. break;
  3967. // Don't support indexing, but we will check it later.
  3968. case Variant::RID:
  3969. case Variant::BOOL:
  3970. case Variant::CALLABLE:
  3971. case Variant::FLOAT:
  3972. case Variant::INT:
  3973. case Variant::NIL:
  3974. case Variant::NODE_PATH:
  3975. case Variant::SIGNAL:
  3976. case Variant::STRING_NAME:
  3977. break;
  3978. // Here for completeness.
  3979. case Variant::DICTIONARY:
  3980. case Variant::VARIANT_MAX:
  3981. break;
  3982. }
  3983. if (error) {
  3984. 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);
  3985. }
  3986. }
  3987. } else if (base_type.kind != GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  3988. if (index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME) {
  3989. push_error(vformat(R"(Only "String" or "StringName" can be used as index for type "%s", but received "%s".)", base_type.to_string(), index_type.to_string()), p_subscript->index);
  3990. }
  3991. }
  3992. // Check resulting type if possible.
  3993. result_type.builtin_type = Variant::NIL;
  3994. result_type.kind = GDScriptParser::DataType::BUILTIN;
  3995. result_type.type_source = base_type.is_hard_type() ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED;
  3996. if (base_type.kind != GDScriptParser::DataType::BUILTIN) {
  3997. base_type.builtin_type = Variant::OBJECT;
  3998. }
  3999. switch (base_type.builtin_type) {
  4000. // Can't index at all.
  4001. case Variant::RID:
  4002. case Variant::BOOL:
  4003. case Variant::CALLABLE:
  4004. case Variant::FLOAT:
  4005. case Variant::INT:
  4006. case Variant::NIL:
  4007. case Variant::NODE_PATH:
  4008. case Variant::SIGNAL:
  4009. case Variant::STRING_NAME:
  4010. result_type.kind = GDScriptParser::DataType::VARIANT;
  4011. push_error(vformat(R"(Cannot use subscript operator on a base of type "%s".)", base_type.to_string()), p_subscript->base);
  4012. break;
  4013. // Return int.
  4014. case Variant::PACKED_BYTE_ARRAY:
  4015. case Variant::PACKED_INT32_ARRAY:
  4016. case Variant::PACKED_INT64_ARRAY:
  4017. case Variant::VECTOR2I:
  4018. case Variant::VECTOR3I:
  4019. case Variant::VECTOR4I:
  4020. result_type.builtin_type = Variant::INT;
  4021. break;
  4022. // Return float.
  4023. case Variant::PACKED_FLOAT32_ARRAY:
  4024. case Variant::PACKED_FLOAT64_ARRAY:
  4025. case Variant::VECTOR2:
  4026. case Variant::VECTOR3:
  4027. case Variant::VECTOR4:
  4028. case Variant::QUATERNION:
  4029. result_type.builtin_type = Variant::FLOAT;
  4030. break;
  4031. // Return String.
  4032. case Variant::PACKED_STRING_ARRAY:
  4033. case Variant::STRING:
  4034. result_type.builtin_type = Variant::STRING;
  4035. break;
  4036. // Return Vector2.
  4037. case Variant::PACKED_VECTOR2_ARRAY:
  4038. case Variant::TRANSFORM2D:
  4039. case Variant::RECT2:
  4040. result_type.builtin_type = Variant::VECTOR2;
  4041. break;
  4042. // Return Vector2I.
  4043. case Variant::RECT2I:
  4044. result_type.builtin_type = Variant::VECTOR2I;
  4045. break;
  4046. // Return Vector3.
  4047. case Variant::PACKED_VECTOR3_ARRAY:
  4048. case Variant::AABB:
  4049. case Variant::BASIS:
  4050. result_type.builtin_type = Variant::VECTOR3;
  4051. break;
  4052. // Return Color.
  4053. case Variant::PACKED_COLOR_ARRAY:
  4054. result_type.builtin_type = Variant::COLOR;
  4055. break;
  4056. // Return Vector4.
  4057. case Variant::PACKED_VECTOR4_ARRAY:
  4058. result_type.builtin_type = Variant::VECTOR4;
  4059. break;
  4060. // Depends on the index.
  4061. case Variant::TRANSFORM3D:
  4062. case Variant::PROJECTION:
  4063. case Variant::PLANE:
  4064. case Variant::COLOR:
  4065. case Variant::DICTIONARY:
  4066. case Variant::OBJECT:
  4067. result_type.kind = GDScriptParser::DataType::VARIANT;
  4068. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  4069. break;
  4070. // Can have an element type.
  4071. case Variant::ARRAY:
  4072. if (base_type.has_container_element_type(0)) {
  4073. result_type = base_type.get_container_element_type(0);
  4074. result_type.type_source = base_type.type_source;
  4075. } else {
  4076. result_type.kind = GDScriptParser::DataType::VARIANT;
  4077. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  4078. }
  4079. break;
  4080. // Here for completeness.
  4081. case Variant::VARIANT_MAX:
  4082. break;
  4083. }
  4084. }
  4085. }
  4086. }
  4087. p_subscript->set_datatype(result_type);
  4088. }
  4089. void GDScriptAnalyzer::reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op, bool p_is_root) {
  4090. reduce_expression(p_ternary_op->condition);
  4091. reduce_expression(p_ternary_op->true_expr, p_is_root);
  4092. reduce_expression(p_ternary_op->false_expr, p_is_root);
  4093. GDScriptParser::DataType result;
  4094. 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) {
  4095. p_ternary_op->is_constant = true;
  4096. if (p_ternary_op->condition->reduced_value.booleanize()) {
  4097. p_ternary_op->reduced_value = p_ternary_op->true_expr->reduced_value;
  4098. } else {
  4099. p_ternary_op->reduced_value = p_ternary_op->false_expr->reduced_value;
  4100. }
  4101. }
  4102. GDScriptParser::DataType true_type;
  4103. if (p_ternary_op->true_expr) {
  4104. true_type = p_ternary_op->true_expr->get_datatype();
  4105. } else {
  4106. true_type.kind = GDScriptParser::DataType::VARIANT;
  4107. }
  4108. GDScriptParser::DataType false_type;
  4109. if (p_ternary_op->false_expr) {
  4110. false_type = p_ternary_op->false_expr->get_datatype();
  4111. } else {
  4112. false_type.kind = GDScriptParser::DataType::VARIANT;
  4113. }
  4114. if (true_type.is_variant() || false_type.is_variant()) {
  4115. result.kind = GDScriptParser::DataType::VARIANT;
  4116. } else {
  4117. result = true_type;
  4118. if (!is_type_compatible(true_type, false_type)) {
  4119. result = false_type;
  4120. if (!is_type_compatible(false_type, true_type)) {
  4121. result.kind = GDScriptParser::DataType::VARIANT;
  4122. #ifdef DEBUG_ENABLED
  4123. parser->push_warning(p_ternary_op, GDScriptWarning::INCOMPATIBLE_TERNARY);
  4124. #endif
  4125. }
  4126. }
  4127. }
  4128. result.type_source = true_type.is_hard_type() && false_type.is_hard_type() ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED;
  4129. p_ternary_op->set_datatype(result);
  4130. }
  4131. void GDScriptAnalyzer::reduce_type_test(GDScriptParser::TypeTestNode *p_type_test) {
  4132. GDScriptParser::DataType result;
  4133. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  4134. result.kind = GDScriptParser::DataType::BUILTIN;
  4135. result.builtin_type = Variant::BOOL;
  4136. p_type_test->set_datatype(result);
  4137. if (!p_type_test->operand || !p_type_test->test_type) {
  4138. return;
  4139. }
  4140. reduce_expression(p_type_test->operand);
  4141. GDScriptParser::DataType operand_type = p_type_test->operand->get_datatype();
  4142. GDScriptParser::DataType test_type = type_from_metatype(resolve_datatype(p_type_test->test_type));
  4143. p_type_test->test_datatype = test_type;
  4144. if (!operand_type.is_set() || !test_type.is_set()) {
  4145. return;
  4146. }
  4147. if (p_type_test->operand->is_constant) {
  4148. p_type_test->is_constant = true;
  4149. p_type_test->reduced_value = false;
  4150. if (!is_type_compatible(test_type, operand_type)) {
  4151. push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)", operand_type.to_string(), test_type.to_string()), p_type_test->operand);
  4152. } else if (is_type_compatible(test_type, type_from_variant(p_type_test->operand->reduced_value, p_type_test->operand))) {
  4153. p_type_test->reduced_value = test_type.builtin_type != Variant::OBJECT || !p_type_test->operand->reduced_value.is_null();
  4154. }
  4155. return;
  4156. }
  4157. if (!is_type_compatible(test_type, operand_type) && !is_type_compatible(operand_type, test_type)) {
  4158. if (operand_type.is_hard_type()) {
  4159. push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)", operand_type.to_string(), test_type.to_string()), p_type_test->operand);
  4160. } else {
  4161. downgrade_node_type_source(p_type_test->operand);
  4162. }
  4163. }
  4164. }
  4165. void GDScriptAnalyzer::reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op) {
  4166. reduce_expression(p_unary_op->operand);
  4167. GDScriptParser::DataType result;
  4168. if (p_unary_op->operand == nullptr) {
  4169. result.kind = GDScriptParser::DataType::VARIANT;
  4170. p_unary_op->set_datatype(result);
  4171. return;
  4172. }
  4173. GDScriptParser::DataType operand_type = p_unary_op->operand->get_datatype();
  4174. if (p_unary_op->operand->is_constant) {
  4175. p_unary_op->is_constant = true;
  4176. p_unary_op->reduced_value = Variant::evaluate(p_unary_op->variant_op, p_unary_op->operand->reduced_value, Variant());
  4177. result = type_from_variant(p_unary_op->reduced_value, p_unary_op);
  4178. }
  4179. if (operand_type.is_variant()) {
  4180. result.kind = GDScriptParser::DataType::VARIANT;
  4181. mark_node_unsafe(p_unary_op);
  4182. } else {
  4183. bool valid = false;
  4184. result = get_operation_type(p_unary_op->variant_op, operand_type, valid, p_unary_op);
  4185. if (!valid) {
  4186. 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);
  4187. }
  4188. }
  4189. p_unary_op->set_datatype(result);
  4190. }
  4191. Variant GDScriptAnalyzer::make_expression_reduced_value(GDScriptParser::ExpressionNode *p_expression, bool &is_reduced) {
  4192. Variant value;
  4193. if (p_expression == nullptr) {
  4194. return value;
  4195. }
  4196. if (p_expression->is_constant) {
  4197. is_reduced = true;
  4198. value = p_expression->reduced_value;
  4199. } else if (p_expression->type == GDScriptParser::Node::ARRAY) {
  4200. value = make_array_reduced_value(static_cast<GDScriptParser::ArrayNode *>(p_expression), is_reduced);
  4201. } else if (p_expression->type == GDScriptParser::Node::DICTIONARY) {
  4202. value = make_dictionary_reduced_value(static_cast<GDScriptParser::DictionaryNode *>(p_expression), is_reduced);
  4203. } else if (p_expression->type == GDScriptParser::Node::SUBSCRIPT) {
  4204. value = make_subscript_reduced_value(static_cast<GDScriptParser::SubscriptNode *>(p_expression), is_reduced);
  4205. }
  4206. return value;
  4207. }
  4208. Variant GDScriptAnalyzer::make_array_reduced_value(GDScriptParser::ArrayNode *p_array, bool &is_reduced) {
  4209. Array array = p_array->get_datatype().has_container_element_type(0) ? make_array_from_element_datatype(p_array->get_datatype().get_container_element_type(0)) : Array();
  4210. array.resize(p_array->elements.size());
  4211. for (int i = 0; i < p_array->elements.size(); i++) {
  4212. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  4213. bool is_element_value_reduced = false;
  4214. Variant element_value = make_expression_reduced_value(element, is_element_value_reduced);
  4215. if (!is_element_value_reduced) {
  4216. return Variant();
  4217. }
  4218. array[i] = element_value;
  4219. }
  4220. array.make_read_only();
  4221. is_reduced = true;
  4222. return array;
  4223. }
  4224. Variant GDScriptAnalyzer::make_dictionary_reduced_value(GDScriptParser::DictionaryNode *p_dictionary, bool &is_reduced) {
  4225. Dictionary dictionary;
  4226. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  4227. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  4228. bool is_element_key_reduced = false;
  4229. Variant element_key = make_expression_reduced_value(element.key, is_element_key_reduced);
  4230. if (!is_element_key_reduced) {
  4231. return Variant();
  4232. }
  4233. bool is_element_value_reduced = false;
  4234. Variant element_value = make_expression_reduced_value(element.value, is_element_value_reduced);
  4235. if (!is_element_value_reduced) {
  4236. return Variant();
  4237. }
  4238. dictionary[element_key] = element_value;
  4239. }
  4240. dictionary.make_read_only();
  4241. is_reduced = true;
  4242. return dictionary;
  4243. }
  4244. Variant GDScriptAnalyzer::make_subscript_reduced_value(GDScriptParser::SubscriptNode *p_subscript, bool &is_reduced) {
  4245. if (p_subscript->base == nullptr || p_subscript->index == nullptr) {
  4246. return Variant();
  4247. }
  4248. bool is_base_value_reduced = false;
  4249. Variant base_value = make_expression_reduced_value(p_subscript->base, is_base_value_reduced);
  4250. if (!is_base_value_reduced) {
  4251. return Variant();
  4252. }
  4253. if (p_subscript->is_attribute) {
  4254. bool is_valid = false;
  4255. Variant value = base_value.get_named(p_subscript->attribute->name, is_valid);
  4256. if (is_valid) {
  4257. is_reduced = true;
  4258. return value;
  4259. } else {
  4260. return Variant();
  4261. }
  4262. } else {
  4263. bool is_index_value_reduced = false;
  4264. Variant index_value = make_expression_reduced_value(p_subscript->index, is_index_value_reduced);
  4265. if (!is_index_value_reduced) {
  4266. return Variant();
  4267. }
  4268. bool is_valid = false;
  4269. Variant value = base_value.get(index_value, &is_valid);
  4270. if (is_valid) {
  4271. is_reduced = true;
  4272. return value;
  4273. } else {
  4274. return Variant();
  4275. }
  4276. }
  4277. }
  4278. Array GDScriptAnalyzer::make_array_from_element_datatype(const GDScriptParser::DataType &p_element_datatype, const GDScriptParser::Node *p_source_node) {
  4279. Array array;
  4280. if (p_element_datatype.builtin_type == Variant::OBJECT) {
  4281. Ref<Script> script_type = p_element_datatype.script_type;
  4282. if (p_element_datatype.kind == GDScriptParser::DataType::CLASS && script_type.is_null()) {
  4283. Error err = OK;
  4284. Ref<GDScript> scr = GDScriptCache::get_shallow_script(p_element_datatype.script_path, err, parser->script_path);
  4285. if (err) {
  4286. push_error(vformat(R"(Error while getting cache for script "%s".)", p_element_datatype.script_path), p_source_node);
  4287. return array;
  4288. }
  4289. script_type.reference_ptr(scr->find_class(p_element_datatype.class_type->fqcn));
  4290. }
  4291. array.set_typed(p_element_datatype.builtin_type, p_element_datatype.native_type, script_type);
  4292. } else {
  4293. array.set_typed(p_element_datatype.builtin_type, StringName(), Variant());
  4294. }
  4295. return array;
  4296. }
  4297. Variant GDScriptAnalyzer::make_variable_default_value(GDScriptParser::VariableNode *p_variable) {
  4298. Variant result = Variant();
  4299. if (p_variable->initializer) {
  4300. bool is_initializer_value_reduced = false;
  4301. Variant initializer_value = make_expression_reduced_value(p_variable->initializer, is_initializer_value_reduced);
  4302. if (is_initializer_value_reduced) {
  4303. result = initializer_value;
  4304. }
  4305. } else {
  4306. GDScriptParser::DataType datatype = p_variable->get_datatype();
  4307. if (datatype.is_hard_type()) {
  4308. if (datatype.kind == GDScriptParser::DataType::BUILTIN && datatype.builtin_type != Variant::OBJECT) {
  4309. if (datatype.builtin_type == Variant::ARRAY && datatype.has_container_element_type(0)) {
  4310. result = make_array_from_element_datatype(datatype.get_container_element_type(0));
  4311. } else {
  4312. VariantInternal::initialize(&result, datatype.builtin_type);
  4313. }
  4314. } else if (datatype.kind == GDScriptParser::DataType::ENUM) {
  4315. result = 0;
  4316. }
  4317. }
  4318. }
  4319. return result;
  4320. }
  4321. GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source) {
  4322. GDScriptParser::DataType result;
  4323. result.is_constant = true;
  4324. result.kind = GDScriptParser::DataType::BUILTIN;
  4325. result.builtin_type = p_value.get_type();
  4326. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; // Constant has explicit type.
  4327. if (p_value.get_type() == Variant::ARRAY) {
  4328. const Array &array = p_value;
  4329. if (array.get_typed_script()) {
  4330. result.set_container_element_type(0, type_from_metatype(make_script_meta_type(array.get_typed_script())));
  4331. } else if (array.get_typed_class_name()) {
  4332. result.set_container_element_type(0, type_from_metatype(make_native_meta_type(array.get_typed_class_name())));
  4333. } else if (array.get_typed_builtin() != Variant::NIL) {
  4334. result.set_container_element_type(0, type_from_metatype(make_builtin_meta_type((Variant::Type)array.get_typed_builtin())));
  4335. }
  4336. } else if (p_value.get_type() == Variant::OBJECT) {
  4337. // Object is treated as a native type, not a builtin type.
  4338. result.kind = GDScriptParser::DataType::NATIVE;
  4339. Object *obj = p_value;
  4340. if (!obj) {
  4341. return GDScriptParser::DataType();
  4342. }
  4343. result.native_type = obj->get_class_name();
  4344. Ref<Script> scr = p_value; // Check if value is a script itself.
  4345. if (scr.is_valid()) {
  4346. result.is_meta_type = true;
  4347. } else {
  4348. result.is_meta_type = false;
  4349. scr = obj->get_script();
  4350. }
  4351. if (scr.is_valid()) {
  4352. Ref<GDScript> gds = scr;
  4353. if (gds.is_valid()) {
  4354. // This might be an inner class, so we want to get the parser for the root.
  4355. // But still get the inner class from that tree.
  4356. String script_path = gds->get_script_path();
  4357. Ref<GDScriptParserRef> ref = parser->get_depended_parser_for(script_path);
  4358. if (ref.is_null()) {
  4359. push_error(vformat(R"(Could not find script "%s".)", script_path), p_source);
  4360. GDScriptParser::DataType error_type;
  4361. error_type.kind = GDScriptParser::DataType::VARIANT;
  4362. return error_type;
  4363. }
  4364. Error err = ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  4365. GDScriptParser::ClassNode *found = nullptr;
  4366. if (err == OK) {
  4367. found = ref->get_parser()->find_class(gds->fully_qualified_name);
  4368. if (found != nullptr) {
  4369. err = resolve_class_inheritance(found, p_source);
  4370. }
  4371. }
  4372. if (err || found == nullptr) {
  4373. push_error(vformat(R"(Could not resolve script "%s".)", script_path), p_source);
  4374. GDScriptParser::DataType error_type;
  4375. error_type.kind = GDScriptParser::DataType::VARIANT;
  4376. return error_type;
  4377. }
  4378. result.kind = GDScriptParser::DataType::CLASS;
  4379. result.native_type = found->get_datatype().native_type;
  4380. result.class_type = found;
  4381. result.script_path = ref->get_parser()->script_path;
  4382. } else {
  4383. result.kind = GDScriptParser::DataType::SCRIPT;
  4384. result.native_type = scr->get_instance_base_type();
  4385. result.script_path = scr->get_path();
  4386. }
  4387. result.script_type = scr;
  4388. } else {
  4389. result.kind = GDScriptParser::DataType::NATIVE;
  4390. if (result.native_type == GDScriptNativeClass::get_class_static()) {
  4391. result.is_meta_type = true;
  4392. }
  4393. }
  4394. }
  4395. return result;
  4396. }
  4397. GDScriptParser::DataType GDScriptAnalyzer::type_from_metatype(const GDScriptParser::DataType &p_meta_type) {
  4398. GDScriptParser::DataType result = p_meta_type;
  4399. result.is_meta_type = false;
  4400. result.is_pseudo_type = false;
  4401. if (p_meta_type.kind == GDScriptParser::DataType::ENUM) {
  4402. result.builtin_type = Variant::INT;
  4403. } else {
  4404. result.is_constant = false;
  4405. }
  4406. return result;
  4407. }
  4408. GDScriptParser::DataType GDScriptAnalyzer::type_from_property(const PropertyInfo &p_property, bool p_is_arg, bool p_is_readonly) const {
  4409. GDScriptParser::DataType result;
  4410. result.is_read_only = p_is_readonly;
  4411. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  4412. if (p_property.type == Variant::NIL && (p_is_arg || (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  4413. // Variant
  4414. result.kind = GDScriptParser::DataType::VARIANT;
  4415. return result;
  4416. }
  4417. result.builtin_type = p_property.type;
  4418. if (p_property.type == Variant::OBJECT) {
  4419. if (ScriptServer::is_global_class(p_property.class_name)) {
  4420. result.kind = GDScriptParser::DataType::SCRIPT;
  4421. result.script_path = ScriptServer::get_global_class_path(p_property.class_name);
  4422. result.native_type = ScriptServer::get_global_class_native_base(p_property.class_name);
  4423. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_property.class_name));
  4424. if (scr.is_valid()) {
  4425. result.script_type = scr;
  4426. }
  4427. } else {
  4428. result.kind = GDScriptParser::DataType::NATIVE;
  4429. result.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  4430. }
  4431. } else {
  4432. result.kind = GDScriptParser::DataType::BUILTIN;
  4433. result.builtin_type = p_property.type;
  4434. if (p_property.type == Variant::ARRAY && p_property.hint == PROPERTY_HINT_ARRAY_TYPE) {
  4435. // Check element type.
  4436. StringName elem_type_name = p_property.hint_string;
  4437. GDScriptParser::DataType elem_type;
  4438. elem_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  4439. Variant::Type elem_builtin_type = GDScriptParser::get_builtin_type(elem_type_name);
  4440. if (elem_builtin_type < Variant::VARIANT_MAX) {
  4441. // Builtin type.
  4442. elem_type.kind = GDScriptParser::DataType::BUILTIN;
  4443. elem_type.builtin_type = elem_builtin_type;
  4444. } else if (class_exists(elem_type_name)) {
  4445. elem_type.kind = GDScriptParser::DataType::NATIVE;
  4446. elem_type.builtin_type = Variant::OBJECT;
  4447. elem_type.native_type = elem_type_name;
  4448. } else if (ScriptServer::is_global_class(elem_type_name)) {
  4449. // Just load this as it shouldn't be a GDScript.
  4450. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(elem_type_name));
  4451. elem_type.kind = GDScriptParser::DataType::SCRIPT;
  4452. elem_type.builtin_type = Variant::OBJECT;
  4453. elem_type.native_type = script->get_instance_base_type();
  4454. elem_type.script_type = script;
  4455. } else {
  4456. ERR_FAIL_V_MSG(result, "Could not find element type from property hint of a typed array.");
  4457. }
  4458. elem_type.is_constant = false;
  4459. result.set_container_element_type(0, elem_type);
  4460. } else if (p_property.type == Variant::INT) {
  4461. // Check if it's enum.
  4462. if ((p_property.usage & PROPERTY_USAGE_CLASS_IS_ENUM) && p_property.class_name != StringName()) {
  4463. if (CoreConstants::is_global_enum(p_property.class_name)) {
  4464. result = make_global_enum_type(p_property.class_name, StringName(), false);
  4465. result.is_constant = false;
  4466. } else {
  4467. Vector<String> names = String(p_property.class_name).split(ENUM_SEPARATOR);
  4468. if (names.size() == 2) {
  4469. result = make_native_enum_type(names[1], names[0], false);
  4470. result.is_constant = false;
  4471. }
  4472. }
  4473. }
  4474. // PROPERTY_USAGE_CLASS_IS_BITFIELD: BitField[T] isn't supported (yet?), use plain int.
  4475. }
  4476. }
  4477. return result;
  4478. }
  4479. 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, BitField<MethodFlags> &r_method_flags, StringName *r_native_class) {
  4480. r_method_flags = METHOD_FLAGS_DEFAULT;
  4481. r_default_arg_count = 0;
  4482. if (r_native_class) {
  4483. *r_native_class = StringName();
  4484. }
  4485. StringName function_name = p_function;
  4486. bool was_enum = false;
  4487. if (p_base_type.kind == GDScriptParser::DataType::ENUM) {
  4488. was_enum = true;
  4489. if (p_base_type.is_meta_type) {
  4490. // Enum type can be treated as a dictionary value.
  4491. p_base_type.kind = GDScriptParser::DataType::BUILTIN;
  4492. p_base_type.is_meta_type = false;
  4493. } else {
  4494. push_error("Cannot call function on enum value.", p_source);
  4495. return false;
  4496. }
  4497. }
  4498. if (p_base_type.kind == GDScriptParser::DataType::BUILTIN) {
  4499. // Construct a base type to get methods.
  4500. Callable::CallError err;
  4501. Variant dummy;
  4502. Variant::construct(p_base_type.builtin_type, dummy, nullptr, 0, err);
  4503. if (err.error != Callable::CallError::CALL_OK) {
  4504. ERR_FAIL_V_MSG(false, "Could not construct base Variant type.");
  4505. }
  4506. List<MethodInfo> methods;
  4507. dummy.get_method_list(&methods);
  4508. for (const MethodInfo &E : methods) {
  4509. if (E.name == p_function) {
  4510. function_signature_from_info(E, r_return_type, r_par_types, r_default_arg_count, r_method_flags);
  4511. // Cannot use non-const methods on enums.
  4512. if (!r_method_flags.has_flag(METHOD_FLAG_STATIC) && was_enum && !(E.flags & METHOD_FLAG_CONST)) {
  4513. push_error(vformat(R"*(Cannot call non-const Dictionary function "%s()" on enum "%s".)*", p_function, p_base_type.enum_type), p_source);
  4514. }
  4515. return true;
  4516. }
  4517. }
  4518. return false;
  4519. }
  4520. StringName base_native = p_base_type.native_type;
  4521. if (base_native != StringName()) {
  4522. // Empty native class might happen in some Script implementations.
  4523. // Just ignore it.
  4524. if (!class_exists(base_native)) {
  4525. push_error(vformat("Native class %s used in script doesn't exist or isn't exposed.", base_native), p_source);
  4526. return false;
  4527. } else if (p_is_constructor && !ClassDB::can_instantiate(base_native)) {
  4528. if (p_base_type.kind == GDScriptParser::DataType::CLASS) {
  4529. push_error(vformat(R"(Class "%s" cannot be constructed as it is based on abstract native class "%s".)", p_base_type.class_type->fqcn.get_file(), base_native), p_source);
  4530. } else if (p_base_type.kind == GDScriptParser::DataType::SCRIPT) {
  4531. push_error(vformat(R"(Script "%s" cannot be constructed as it is based on abstract native class "%s".)", p_base_type.script_path.get_file(), base_native), p_source);
  4532. } else {
  4533. push_error(vformat(R"(Native class "%s" cannot be constructed as it is abstract.)", base_native), p_source);
  4534. }
  4535. return false;
  4536. }
  4537. }
  4538. if (p_is_constructor) {
  4539. function_name = GDScriptLanguage::get_singleton()->strings._init;
  4540. r_method_flags.set_flag(METHOD_FLAG_STATIC);
  4541. }
  4542. GDScriptParser::ClassNode *base_class = p_base_type.class_type;
  4543. GDScriptParser::FunctionNode *found_function = nullptr;
  4544. while (found_function == nullptr && base_class != nullptr) {
  4545. if (base_class->has_member(function_name)) {
  4546. if (base_class->get_member(function_name).type != GDScriptParser::ClassNode::Member::FUNCTION) {
  4547. // TODO: If this is Callable it can have a better error message.
  4548. push_error(vformat(R"(Member "%s" is not a function.)", function_name), p_source);
  4549. return false;
  4550. }
  4551. resolve_class_member(base_class, function_name, p_source);
  4552. found_function = base_class->get_member(function_name).function;
  4553. }
  4554. resolve_class_inheritance(base_class, p_source);
  4555. base_class = base_class->base_type.class_type;
  4556. }
  4557. if (found_function != nullptr) {
  4558. if (p_is_constructor || found_function->is_static) {
  4559. r_method_flags.set_flag(METHOD_FLAG_STATIC);
  4560. }
  4561. for (int i = 0; i < found_function->parameters.size(); i++) {
  4562. r_par_types.push_back(found_function->parameters[i]->get_datatype());
  4563. if (found_function->parameters[i]->initializer != nullptr) {
  4564. r_default_arg_count++;
  4565. }
  4566. }
  4567. r_return_type = p_is_constructor ? p_base_type : found_function->get_datatype();
  4568. r_return_type.is_meta_type = false;
  4569. r_return_type.is_coroutine = found_function->is_coroutine;
  4570. return true;
  4571. }
  4572. Ref<Script> base_script = p_base_type.script_type;
  4573. while (base_script.is_valid() && base_script->has_method(function_name)) {
  4574. MethodInfo info = base_script->get_method_info(function_name);
  4575. if (!(info == MethodInfo())) {
  4576. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_method_flags);
  4577. }
  4578. base_script = base_script->get_base_script();
  4579. }
  4580. // If the base is a script, it might be trying to access members of the Script class itself.
  4581. if (p_base_type.is_meta_type && !p_is_constructor && (p_base_type.kind == GDScriptParser::DataType::SCRIPT || p_base_type.kind == GDScriptParser::DataType::CLASS)) {
  4582. MethodInfo info;
  4583. StringName script_class = p_base_type.kind == GDScriptParser::DataType::SCRIPT ? p_base_type.script_type->get_class_name() : StringName(GDScript::get_class_static());
  4584. if (ClassDB::get_method_info(script_class, function_name, &info)) {
  4585. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_method_flags);
  4586. }
  4587. }
  4588. if (p_is_constructor) {
  4589. // Native types always have a default constructor.
  4590. r_return_type = p_base_type;
  4591. r_return_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  4592. r_return_type.is_meta_type = false;
  4593. return true;
  4594. }
  4595. MethodInfo info;
  4596. if (ClassDB::get_method_info(base_native, function_name, &info)) {
  4597. bool valid = function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_method_flags);
  4598. if (valid && Engine::get_singleton()->has_singleton(base_native)) {
  4599. r_method_flags.set_flag(METHOD_FLAG_STATIC);
  4600. }
  4601. #ifdef DEBUG_ENABLED
  4602. MethodBind *native_method = ClassDB::get_method(base_native, function_name);
  4603. if (native_method && r_native_class) {
  4604. *r_native_class = native_method->get_instance_class();
  4605. }
  4606. #endif
  4607. return valid;
  4608. }
  4609. return false;
  4610. }
  4611. 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, BitField<MethodFlags> &r_method_flags) {
  4612. r_return_type = type_from_property(p_info.return_val);
  4613. r_default_arg_count = p_info.default_arguments.size();
  4614. r_method_flags = p_info.flags;
  4615. for (const PropertyInfo &E : p_info.arguments) {
  4616. r_par_types.push_back(type_from_property(E, true));
  4617. }
  4618. return true;
  4619. }
  4620. void GDScriptAnalyzer::validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call) {
  4621. List<GDScriptParser::DataType> arg_types;
  4622. for (const PropertyInfo &E : p_method.arguments) {
  4623. arg_types.push_back(type_from_property(E, true));
  4624. }
  4625. validate_call_arg(arg_types, p_method.default_arguments.size(), (p_method.flags & METHOD_FLAG_VARARG) != 0, p_call);
  4626. }
  4627. void 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) {
  4628. if (p_call->arguments.size() < p_par_types.size() - p_default_args_count) {
  4629. 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);
  4630. }
  4631. if (!p_is_vararg && p_call->arguments.size() > p_par_types.size()) {
  4632. 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()]);
  4633. }
  4634. for (int i = 0; i < p_call->arguments.size(); i++) {
  4635. if (i >= p_par_types.size()) {
  4636. // Already on vararg place.
  4637. break;
  4638. }
  4639. GDScriptParser::DataType par_type = p_par_types[i];
  4640. if (par_type.is_hard_type() && p_call->arguments[i]->is_constant) {
  4641. update_const_expression_builtin_type(p_call->arguments[i], par_type, "pass");
  4642. }
  4643. GDScriptParser::DataType arg_type = p_call->arguments[i]->get_datatype();
  4644. if (arg_type.is_variant() || !arg_type.is_hard_type()) {
  4645. #ifdef DEBUG_ENABLED
  4646. // Argument can be anything, so this is unsafe (unless the parameter is a hard variant).
  4647. if (!(par_type.is_hard_type() && par_type.is_variant())) {
  4648. mark_node_unsafe(p_call->arguments[i]);
  4649. parser->push_warning(p_call->arguments[i], GDScriptWarning::UNSAFE_CALL_ARGUMENT, itos(i + 1), "function", p_call->function_name, par_type.to_string(), arg_type.to_string_strict());
  4650. }
  4651. #endif
  4652. } else if (par_type.is_hard_type() && !is_type_compatible(par_type, arg_type, true)) {
  4653. if (!is_type_compatible(arg_type, par_type)) {
  4654. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be "%s" but is "%s".)*",
  4655. p_call->function_name, i + 1, par_type.to_string(), arg_type.to_string()),
  4656. p_call->arguments[i]);
  4657. #ifdef DEBUG_ENABLED
  4658. } else {
  4659. // Supertypes are acceptable for dynamic compliance, but it's unsafe.
  4660. mark_node_unsafe(p_call);
  4661. parser->push_warning(p_call->arguments[i], GDScriptWarning::UNSAFE_CALL_ARGUMENT, itos(i + 1), "function", p_call->function_name, par_type.to_string(), arg_type.to_string_strict());
  4662. #endif
  4663. }
  4664. #ifdef DEBUG_ENABLED
  4665. } else 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) {
  4666. parser->push_warning(p_call->arguments[i], GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  4667. #endif
  4668. }
  4669. }
  4670. }
  4671. #ifdef DEBUG_ENABLED
  4672. void GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_identifier, const String &p_context, const bool p_in_local_scope) {
  4673. const StringName &name = p_identifier->name;
  4674. GDScriptParser::DataType base = parser->current_class->get_datatype();
  4675. GDScriptParser::ClassNode *base_class = base.class_type;
  4676. {
  4677. List<MethodInfo> gdscript_funcs;
  4678. GDScriptLanguage::get_singleton()->get_public_functions(&gdscript_funcs);
  4679. for (MethodInfo &info : gdscript_funcs) {
  4680. if (info.name == name) {
  4681. parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function");
  4682. return;
  4683. }
  4684. }
  4685. if (Variant::has_utility_function(name)) {
  4686. parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function");
  4687. return;
  4688. } else if (ClassDB::class_exists(name)) {
  4689. parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "native class");
  4690. return;
  4691. } else if (ScriptServer::is_global_class(name)) {
  4692. String class_path = ScriptServer::get_global_class_path(name).get_file();
  4693. parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, vformat(R"(global class defined in "%s")", class_path));
  4694. return;
  4695. } else if (GDScriptParser::get_builtin_type(name) < Variant::VARIANT_MAX) {
  4696. parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in type");
  4697. return;
  4698. }
  4699. }
  4700. if (p_in_local_scope) {
  4701. while (base_class != nullptr) {
  4702. if (base_class->has_member(name)) {
  4703. parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE, p_context, p_identifier->name, base_class->get_member(name).get_type_name(), itos(base_class->get_member(name).get_line()));
  4704. return;
  4705. }
  4706. base_class = base_class->base_type.class_type;
  4707. }
  4708. }
  4709. StringName parent = base.native_type;
  4710. while (parent != StringName()) {
  4711. ERR_FAIL_COND_MSG(!class_exists(parent), "Non-existent native base class.");
  4712. if (ClassDB::has_method(parent, name, true)) {
  4713. parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_identifier->name, "method", parent);
  4714. return;
  4715. } else if (ClassDB::has_signal(parent, name, true)) {
  4716. parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_identifier->name, "signal", parent);
  4717. return;
  4718. } else if (ClassDB::has_property(parent, name, true)) {
  4719. parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_identifier->name, "property", parent);
  4720. return;
  4721. } else if (ClassDB::has_integer_constant(parent, name, true)) {
  4722. parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_identifier->name, "constant", parent);
  4723. return;
  4724. } else if (ClassDB::has_enum(parent, name, true)) {
  4725. parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_identifier->name, "enum", parent);
  4726. return;
  4727. }
  4728. parent = ClassDB::get_parent_class(parent);
  4729. }
  4730. }
  4731. #endif
  4732. GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, bool &r_valid, const GDScriptParser::Node *p_source) {
  4733. // Unary version.
  4734. GDScriptParser::DataType nil_type;
  4735. nil_type.builtin_type = Variant::NIL;
  4736. nil_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  4737. return get_operation_type(p_operation, p_a, nil_type, r_valid, p_source);
  4738. }
  4739. 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) {
  4740. if (p_operation == Variant::OP_AND || p_operation == Variant::OP_OR) {
  4741. // Those work for any type of argument and always return a boolean.
  4742. // They don't use the Variant operator since they have short-circuit semantics.
  4743. r_valid = true;
  4744. GDScriptParser::DataType result;
  4745. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  4746. result.kind = GDScriptParser::DataType::BUILTIN;
  4747. result.builtin_type = Variant::BOOL;
  4748. return result;
  4749. }
  4750. Variant::Type a_type = p_a.builtin_type;
  4751. Variant::Type b_type = p_b.builtin_type;
  4752. if (p_a.kind == GDScriptParser::DataType::ENUM) {
  4753. if (p_a.is_meta_type) {
  4754. a_type = Variant::DICTIONARY;
  4755. } else {
  4756. a_type = Variant::INT;
  4757. }
  4758. }
  4759. if (p_b.kind == GDScriptParser::DataType::ENUM) {
  4760. if (p_b.is_meta_type) {
  4761. b_type = Variant::DICTIONARY;
  4762. } else {
  4763. b_type = Variant::INT;
  4764. }
  4765. }
  4766. GDScriptParser::DataType result;
  4767. bool hard_operation = p_a.is_hard_type() && p_b.is_hard_type();
  4768. if (p_operation == Variant::OP_ADD && a_type == Variant::ARRAY && b_type == Variant::ARRAY) {
  4769. if (p_a.has_container_element_type(0) && p_b.has_container_element_type(0) && p_a.get_container_element_type(0) == p_b.get_container_element_type(0)) {
  4770. r_valid = true;
  4771. result = p_a;
  4772. result.type_source = hard_operation ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED;
  4773. return result;
  4774. }
  4775. }
  4776. Variant::ValidatedOperatorEvaluator op_eval = Variant::get_validated_operator_evaluator(p_operation, a_type, b_type);
  4777. bool validated = op_eval != nullptr;
  4778. if (validated) {
  4779. r_valid = true;
  4780. result.type_source = hard_operation ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED;
  4781. result.kind = GDScriptParser::DataType::BUILTIN;
  4782. result.builtin_type = Variant::get_operator_return_type(p_operation, a_type, b_type);
  4783. } else {
  4784. r_valid = !hard_operation;
  4785. result.kind = GDScriptParser::DataType::VARIANT;
  4786. }
  4787. return result;
  4788. }
  4789. 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) {
  4790. #ifdef DEBUG_ENABLED
  4791. if (p_source_node) {
  4792. if (p_target.kind == GDScriptParser::DataType::ENUM) {
  4793. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::INT) {
  4794. parser->push_warning(p_source_node, GDScriptWarning::INT_AS_ENUM_WITHOUT_CAST);
  4795. }
  4796. }
  4797. }
  4798. #endif
  4799. return check_type_compatibility(p_target, p_source, p_allow_implicit_conversion, p_source_node);
  4800. }
  4801. // TODO: Add safe/unsafe return variable (for variant cases)
  4802. bool GDScriptAnalyzer::check_type_compatibility(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion, const GDScriptParser::Node *p_source_node) {
  4803. // These return "true" so it doesn't affect users negatively.
  4804. ERR_FAIL_COND_V_MSG(!p_target.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset target type");
  4805. ERR_FAIL_COND_V_MSG(!p_source.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset value type");
  4806. if (p_target.kind == GDScriptParser::DataType::VARIANT) {
  4807. // Variant can receive anything.
  4808. return true;
  4809. }
  4810. if (p_source.kind == GDScriptParser::DataType::VARIANT) {
  4811. // TODO: This is acceptable but unsafe. Make sure unsafe line is set.
  4812. return true;
  4813. }
  4814. if (p_target.kind == GDScriptParser::DataType::BUILTIN) {
  4815. bool valid = p_source.kind == GDScriptParser::DataType::BUILTIN && p_target.builtin_type == p_source.builtin_type;
  4816. if (!valid && p_allow_implicit_conversion) {
  4817. valid = Variant::can_convert_strict(p_source.builtin_type, p_target.builtin_type);
  4818. }
  4819. if (!valid && p_target.builtin_type == Variant::INT && p_source.kind == GDScriptParser::DataType::ENUM && !p_source.is_meta_type) {
  4820. // Enum value is also integer.
  4821. valid = true;
  4822. }
  4823. if (valid && p_target.builtin_type == Variant::ARRAY && p_source.builtin_type == Variant::ARRAY) {
  4824. // Check the element type.
  4825. if (p_target.has_container_element_type(0) && p_source.has_container_element_type(0)) {
  4826. valid = p_target.get_container_element_type(0) == p_source.get_container_element_type(0);
  4827. }
  4828. }
  4829. return valid;
  4830. }
  4831. if (p_target.kind == GDScriptParser::DataType::ENUM) {
  4832. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::INT) {
  4833. return true;
  4834. }
  4835. if (p_source.kind == GDScriptParser::DataType::ENUM) {
  4836. if (p_source.native_type == p_target.native_type) {
  4837. return true;
  4838. }
  4839. }
  4840. return false;
  4841. }
  4842. // From here on the target type is an object, so we have to test polymorphism.
  4843. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::NIL) {
  4844. // null is acceptable in object.
  4845. return true;
  4846. }
  4847. StringName src_native;
  4848. Ref<Script> src_script;
  4849. const GDScriptParser::ClassNode *src_class = nullptr;
  4850. switch (p_source.kind) {
  4851. case GDScriptParser::DataType::NATIVE:
  4852. if (p_target.kind != GDScriptParser::DataType::NATIVE) {
  4853. // Non-native class cannot be supertype of native.
  4854. return false;
  4855. }
  4856. if (p_source.is_meta_type) {
  4857. src_native = GDScriptNativeClass::get_class_static();
  4858. } else {
  4859. src_native = p_source.native_type;
  4860. }
  4861. break;
  4862. case GDScriptParser::DataType::SCRIPT:
  4863. if (p_target.kind == GDScriptParser::DataType::CLASS) {
  4864. // A script type cannot be a subtype of a GDScript class.
  4865. return false;
  4866. }
  4867. if (p_source.is_meta_type) {
  4868. src_native = p_source.script_type->get_class_name();
  4869. } else {
  4870. src_script = p_source.script_type;
  4871. src_native = src_script->get_instance_base_type();
  4872. }
  4873. break;
  4874. case GDScriptParser::DataType::CLASS:
  4875. if (p_source.is_meta_type) {
  4876. src_native = GDScript::get_class_static();
  4877. } else {
  4878. src_class = p_source.class_type;
  4879. const GDScriptParser::ClassNode *base = src_class;
  4880. while (base->base_type.kind == GDScriptParser::DataType::CLASS) {
  4881. base = base->base_type.class_type;
  4882. }
  4883. src_native = base->base_type.native_type;
  4884. src_script = base->base_type.script_type;
  4885. }
  4886. break;
  4887. case GDScriptParser::DataType::VARIANT:
  4888. case GDScriptParser::DataType::BUILTIN:
  4889. case GDScriptParser::DataType::ENUM:
  4890. case GDScriptParser::DataType::RESOLVING:
  4891. case GDScriptParser::DataType::UNRESOLVED:
  4892. break; // Already solved before.
  4893. }
  4894. switch (p_target.kind) {
  4895. case GDScriptParser::DataType::NATIVE: {
  4896. if (p_target.is_meta_type) {
  4897. return ClassDB::is_parent_class(src_native, GDScriptNativeClass::get_class_static());
  4898. }
  4899. return ClassDB::is_parent_class(src_native, p_target.native_type);
  4900. }
  4901. case GDScriptParser::DataType::SCRIPT:
  4902. if (p_target.is_meta_type) {
  4903. return ClassDB::is_parent_class(src_native, p_target.script_type->get_class_name());
  4904. }
  4905. while (src_script.is_valid()) {
  4906. if (src_script == p_target.script_type) {
  4907. return true;
  4908. }
  4909. src_script = src_script->get_base_script();
  4910. }
  4911. return false;
  4912. case GDScriptParser::DataType::CLASS:
  4913. if (p_target.is_meta_type) {
  4914. return ClassDB::is_parent_class(src_native, GDScript::get_class_static());
  4915. }
  4916. while (src_class != nullptr) {
  4917. if (src_class == p_target.class_type || src_class->fqcn == p_target.class_type->fqcn) {
  4918. return true;
  4919. }
  4920. src_class = src_class->base_type.class_type;
  4921. }
  4922. return false;
  4923. case GDScriptParser::DataType::VARIANT:
  4924. case GDScriptParser::DataType::BUILTIN:
  4925. case GDScriptParser::DataType::ENUM:
  4926. case GDScriptParser::DataType::RESOLVING:
  4927. case GDScriptParser::DataType::UNRESOLVED:
  4928. break; // Already solved before.
  4929. }
  4930. return false;
  4931. }
  4932. void GDScriptAnalyzer::push_error(const String &p_message, const GDScriptParser::Node *p_origin) {
  4933. mark_node_unsafe(p_origin);
  4934. parser->push_error(p_message, p_origin);
  4935. }
  4936. void GDScriptAnalyzer::mark_node_unsafe(const GDScriptParser::Node *p_node) {
  4937. #ifdef DEBUG_ENABLED
  4938. if (p_node == nullptr) {
  4939. return;
  4940. }
  4941. for (int i = p_node->start_line; i <= p_node->end_line; i++) {
  4942. parser->unsafe_lines.insert(i);
  4943. }
  4944. #endif
  4945. }
  4946. void GDScriptAnalyzer::downgrade_node_type_source(GDScriptParser::Node *p_node) {
  4947. GDScriptParser::IdentifierNode *identifier = nullptr;
  4948. if (p_node->type == GDScriptParser::Node::IDENTIFIER) {
  4949. identifier = static_cast<GDScriptParser::IdentifierNode *>(p_node);
  4950. } else if (p_node->type == GDScriptParser::Node::SUBSCRIPT) {
  4951. GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(p_node);
  4952. if (subscript->is_attribute) {
  4953. identifier = subscript->attribute;
  4954. }
  4955. }
  4956. if (identifier == nullptr) {
  4957. return;
  4958. }
  4959. GDScriptParser::Node *source = nullptr;
  4960. switch (identifier->source) {
  4961. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: {
  4962. source = identifier->variable_source;
  4963. } break;
  4964. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER: {
  4965. source = identifier->parameter_source;
  4966. } break;
  4967. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE: {
  4968. source = identifier->variable_source;
  4969. } break;
  4970. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR: {
  4971. source = identifier->bind_source;
  4972. } break;
  4973. default:
  4974. break;
  4975. }
  4976. if (source == nullptr) {
  4977. return;
  4978. }
  4979. GDScriptParser::DataType datatype;
  4980. datatype.kind = GDScriptParser::DataType::VARIANT;
  4981. source->set_datatype(datatype);
  4982. }
  4983. void GDScriptAnalyzer::mark_lambda_use_self() {
  4984. GDScriptParser::LambdaNode *lambda = current_lambda;
  4985. while (lambda != nullptr) {
  4986. lambda->use_self = true;
  4987. lambda = lambda->parent_lambda;
  4988. }
  4989. }
  4990. void GDScriptAnalyzer::resolve_pending_lambda_bodies() {
  4991. if (pending_body_resolution_lambdas.is_empty()) {
  4992. return;
  4993. }
  4994. GDScriptParser::LambdaNode *previous_lambda = current_lambda;
  4995. bool previous_static_context = static_context;
  4996. List<GDScriptParser::LambdaNode *> lambdas = pending_body_resolution_lambdas;
  4997. pending_body_resolution_lambdas.clear();
  4998. for (GDScriptParser::LambdaNode *lambda : lambdas) {
  4999. current_lambda = lambda;
  5000. static_context = lambda->function->is_static;
  5001. resolve_function_body(lambda->function, true);
  5002. int captures_amount = lambda->captures.size();
  5003. if (captures_amount > 0) {
  5004. // Create space for lambda parameters.
  5005. // At the beginning to not mess with optional parameters.
  5006. int param_count = lambda->function->parameters.size();
  5007. lambda->function->parameters.resize(param_count + captures_amount);
  5008. for (int i = param_count - 1; i >= 0; i--) {
  5009. lambda->function->parameters.write[i + captures_amount] = lambda->function->parameters[i];
  5010. lambda->function->parameters_indices[lambda->function->parameters[i]->identifier->name] = i + captures_amount;
  5011. }
  5012. // Add captures as extra parameters at the beginning.
  5013. for (int i = 0; i < lambda->captures.size(); i++) {
  5014. GDScriptParser::IdentifierNode *capture = lambda->captures[i];
  5015. GDScriptParser::ParameterNode *capture_param = parser->alloc_node<GDScriptParser::ParameterNode>();
  5016. capture_param->identifier = capture;
  5017. capture_param->usages = capture->usages;
  5018. capture_param->set_datatype(capture->get_datatype());
  5019. lambda->function->parameters.write[i] = capture_param;
  5020. lambda->function->parameters_indices[capture->name] = i;
  5021. }
  5022. }
  5023. }
  5024. current_lambda = previous_lambda;
  5025. static_context = previous_static_context;
  5026. }
  5027. bool GDScriptAnalyzer::class_exists(const StringName &p_class) const {
  5028. return ClassDB::class_exists(p_class) && ClassDB::is_class_exposed(p_class);
  5029. }
  5030. Error GDScriptAnalyzer::resolve_inheritance() {
  5031. return resolve_class_inheritance(parser->head, true);
  5032. }
  5033. Error GDScriptAnalyzer::resolve_interface() {
  5034. resolve_class_interface(parser->head, true);
  5035. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  5036. }
  5037. Error GDScriptAnalyzer::resolve_body() {
  5038. resolve_class_body(parser->head, true);
  5039. #ifdef DEBUG_ENABLED
  5040. // Apply here, after all `@warning_ignore`s have been resolved and applied.
  5041. parser->apply_pending_warnings();
  5042. #endif
  5043. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  5044. }
  5045. Error GDScriptAnalyzer::resolve_dependencies() {
  5046. for (KeyValue<String, Ref<GDScriptParserRef>> &K : parser->depended_parsers) {
  5047. if (K.value.is_null()) {
  5048. return ERR_PARSE_ERROR;
  5049. }
  5050. K.value->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  5051. }
  5052. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  5053. }
  5054. Error GDScriptAnalyzer::analyze() {
  5055. parser->errors.clear();
  5056. Error err = resolve_inheritance();
  5057. if (err) {
  5058. return err;
  5059. }
  5060. resolve_interface();
  5061. err = resolve_body();
  5062. if (err) {
  5063. return err;
  5064. }
  5065. return resolve_dependencies();
  5066. }
  5067. GDScriptAnalyzer::GDScriptAnalyzer(GDScriptParser *p_parser) {
  5068. parser = p_parser;
  5069. }