2
0

gdscript_analyzer.cpp 256 KB

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