gdscript_analyzer.cpp 259 KB

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