gdscript_analyzer.cpp 256 KB

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