gdscript_analyzer.cpp 226 KB

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