gdscript_parser.cpp 283 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004
  1. /**************************************************************************/
  2. /* gdscript_parser.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_parser.h"
  31. #include "core/core_string_names.h"
  32. #include "core/engine.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/os/file_access.h"
  35. #include "core/print_string.h"
  36. #include "core/project_settings.h"
  37. #include "core/reference.h"
  38. #include "core/script_language.h"
  39. #include "gdscript.h"
  40. template <class T>
  41. T *GDScriptParser::alloc_node() {
  42. T *t = memnew(T);
  43. t->next = list;
  44. list = t;
  45. if (!head) {
  46. head = t;
  47. }
  48. t->line = tokenizer->get_token_line();
  49. t->column = tokenizer->get_token_column();
  50. return t;
  51. }
  52. static String _lookup_autoload_path_for_identifier(const String &p_identifier) {
  53. String autoload_path;
  54. String autoload_setting_path = "autoload/" + p_identifier;
  55. if (ProjectSettings::get_singleton()->has_setting(autoload_setting_path)) {
  56. autoload_path = ProjectSettings::get_singleton()->get(autoload_setting_path);
  57. if (autoload_path.begins_with("*")) {
  58. autoload_path = autoload_path.right(1);
  59. }
  60. if (!autoload_path.begins_with("res://")) {
  61. autoload_path = "res://" + autoload_path;
  62. }
  63. }
  64. return autoload_path;
  65. }
  66. #ifdef DEBUG_ENABLED
  67. static String _find_function_name(const GDScriptParser::OperatorNode *p_call);
  68. #endif // DEBUG_ENABLED
  69. bool GDScriptParser::_end_statement() {
  70. if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON) {
  71. tokenizer->advance();
  72. return true; //handle next
  73. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE || tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  74. return true; //will be handled properly
  75. }
  76. return false;
  77. }
  78. void GDScriptParser::_set_end_statement_error(String p_name) {
  79. String error_msg;
  80. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER) {
  81. error_msg = vformat("Expected end of statement (\"%s\"), got %s (\"%s\") instead.", p_name, tokenizer->get_token_name(tokenizer->get_token()), tokenizer->get_token_identifier());
  82. } else {
  83. error_msg = vformat("Expected end of statement (\"%s\"), got %s instead.", p_name, tokenizer->get_token_name(tokenizer->get_token()));
  84. }
  85. _set_error(error_msg);
  86. }
  87. bool GDScriptParser::_enter_indent_block(BlockNode *p_block) {
  88. if (!_parse_colon()) {
  89. return false;
  90. }
  91. if (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE) {
  92. // Be more Python-like.
  93. IndentLevel current_level = indent_level.back()->get();
  94. indent_level.push_back(current_level);
  95. return true;
  96. }
  97. return _parse_indent_block_newlines(p_block);
  98. }
  99. bool GDScriptParser::_enter_inner_class_indent_block() {
  100. if (!_parse_colon()) {
  101. return false;
  102. }
  103. if (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE) {
  104. // Check Python-like one-liner class declaration "class Foo: pass".
  105. // Note: only "pass" is allowed on the same line after the colon.
  106. if (tokenizer->get_token() != GDScriptTokenizer::TK_CF_PASS) {
  107. return false;
  108. }
  109. GDScriptTokenizer::Token token = tokenizer->get_token(1);
  110. if (token != GDScriptTokenizer::TK_NEWLINE && token != GDScriptTokenizer::TK_EOF) {
  111. int line = tokenizer->get_token_line();
  112. int col = tokenizer->get_token_column();
  113. String message = "Invalid syntax: unexpected \"";
  114. message += GDScriptTokenizer::get_token_name(token);
  115. message += "\".";
  116. _set_error(message, line, col);
  117. return false;
  118. }
  119. return true;
  120. }
  121. return _parse_indent_block_newlines();
  122. }
  123. bool GDScriptParser::_parse_colon() {
  124. if (tokenizer->get_token() != GDScriptTokenizer::TK_COLON) {
  125. // Report location at the previous token (on the previous line).
  126. int error_line = tokenizer->get_token_line(-1);
  127. int error_column = tokenizer->get_token_column(-1);
  128. _set_error("':' expected at end of line.", error_line, error_column);
  129. return false;
  130. }
  131. tokenizer->advance();
  132. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  133. return false;
  134. }
  135. return true;
  136. }
  137. bool GDScriptParser::_parse_indent_block_newlines(BlockNode *p_block) {
  138. while (true) {
  139. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_EOF) {
  140. return false;
  141. } else if (tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE) {
  142. int indent = tokenizer->get_token_line_indent();
  143. int tabs = tokenizer->get_token_line_tab_indent();
  144. IndentLevel current_level = indent_level.back()->get();
  145. IndentLevel new_indent(indent, tabs);
  146. if (new_indent.is_mixed(current_level)) {
  147. _set_error("Mixed tabs and spaces in indentation.");
  148. return false;
  149. }
  150. if (indent <= current_level.indent) {
  151. return false;
  152. }
  153. indent_level.push_back(new_indent);
  154. tokenizer->advance();
  155. return true;
  156. } else if (p_block) {
  157. NewLineNode *nl = alloc_node<NewLineNode>();
  158. nl->line = tokenizer->get_token_line();
  159. p_block->statements.push_back(nl);
  160. }
  161. tokenizer->advance(); // Go to the next newline.
  162. }
  163. }
  164. bool GDScriptParser::_parse_arguments(Node *p_parent, Vector<Node *> &p_args, bool p_static, bool p_can_codecomplete, bool p_parsing_constant) {
  165. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  166. tokenizer->advance();
  167. } else {
  168. parenthesis++;
  169. int argidx = 0;
  170. while (true) {
  171. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  172. _make_completable_call(argidx);
  173. completion_node = p_parent;
  174. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING && tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR) {
  175. //completing a string argument..
  176. completion_cursor = tokenizer->get_token_constant();
  177. _make_completable_call(argidx);
  178. completion_node = p_parent;
  179. tokenizer->advance(1);
  180. return false;
  181. }
  182. Node *arg = _parse_expression(p_parent, p_static, false, p_parsing_constant);
  183. if (!arg) {
  184. return false;
  185. }
  186. p_args.push_back(arg);
  187. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  188. tokenizer->advance();
  189. break;
  190. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  191. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  192. _set_error("Expression expected");
  193. return false;
  194. }
  195. tokenizer->advance();
  196. argidx++;
  197. } else {
  198. // something is broken
  199. _set_error("Expected ',' or ')'");
  200. return false;
  201. }
  202. }
  203. parenthesis--;
  204. }
  205. return true;
  206. }
  207. void GDScriptParser::_make_completable_call(int p_arg) {
  208. completion_cursor = StringName();
  209. completion_type = COMPLETION_CALL_ARGUMENTS;
  210. completion_class = current_class;
  211. completion_function = current_function;
  212. completion_line = tokenizer->get_token_line();
  213. completion_argument = p_arg;
  214. completion_block = current_block;
  215. completion_found = true;
  216. tokenizer->advance();
  217. }
  218. bool GDScriptParser::_get_completable_identifier(CompletionType p_type, StringName &identifier) {
  219. identifier = StringName();
  220. if (tokenizer->is_token_literal()) {
  221. identifier = tokenizer->get_token_literal();
  222. tokenizer->advance();
  223. }
  224. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  225. completion_cursor = identifier;
  226. completion_type = p_type;
  227. completion_class = current_class;
  228. completion_function = current_function;
  229. completion_line = tokenizer->get_token_line();
  230. completion_block = current_block;
  231. completion_found = true;
  232. completion_ident_is_call = false;
  233. tokenizer->advance();
  234. if (tokenizer->is_token_literal()) {
  235. identifier = identifier.operator String() + tokenizer->get_token_literal().operator String();
  236. tokenizer->advance();
  237. }
  238. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  239. completion_ident_is_call = true;
  240. }
  241. return true;
  242. }
  243. return false;
  244. }
  245. GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign, bool p_parsing_constant) {
  246. //Vector<Node*> expressions;
  247. //Vector<OperatorNode::Operator> operators;
  248. Vector<Expression> expression;
  249. Node *expr = nullptr;
  250. int op_line = tokenizer->get_token_line(); // when operators are created at the bottom, the line might have been changed (\n found)
  251. while (true) {
  252. /*****************/
  253. /* Parse Operand */
  254. /*****************/
  255. if (parenthesis > 0) {
  256. //remove empty space (only allowed if inside parenthesis
  257. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  258. tokenizer->advance();
  259. }
  260. }
  261. // Check that the next token is not TK_CURSOR and if it is, the offset should be incremented.
  262. int next_valid_offset = 1;
  263. if (tokenizer->get_token(next_valid_offset) == GDScriptTokenizer::TK_CURSOR) {
  264. next_valid_offset++;
  265. // There is a chunk of the identifier that also needs to be ignored (not always there!)
  266. if (tokenizer->get_token(next_valid_offset) == GDScriptTokenizer::TK_IDENTIFIER) {
  267. next_valid_offset++;
  268. }
  269. }
  270. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  271. //subexpression ()
  272. tokenizer->advance();
  273. parenthesis++;
  274. Node *subexpr = _parse_expression(p_parent, p_static, p_allow_assign, p_parsing_constant);
  275. parenthesis--;
  276. if (!subexpr) {
  277. return nullptr;
  278. }
  279. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  280. _set_error("Expected ')' in expression");
  281. return nullptr;
  282. }
  283. tokenizer->advance();
  284. expr = subexpr;
  285. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_DOLLAR) {
  286. tokenizer->advance();
  287. String path;
  288. bool need_identifier = true;
  289. bool done = false;
  290. int line = tokenizer->get_token_line();
  291. while (!done) {
  292. switch (tokenizer->get_token()) {
  293. case GDScriptTokenizer::TK_CURSOR: {
  294. completion_type = COMPLETION_GET_NODE;
  295. completion_class = current_class;
  296. completion_function = current_function;
  297. completion_line = tokenizer->get_token_line();
  298. completion_cursor = path;
  299. completion_argument = 0;
  300. completion_block = current_block;
  301. completion_found = true;
  302. tokenizer->advance();
  303. } break;
  304. case GDScriptTokenizer::TK_CONSTANT: {
  305. if (!need_identifier) {
  306. done = true;
  307. break;
  308. }
  309. if (tokenizer->get_token_constant().get_type() != Variant::STRING) {
  310. _set_error("Expected string constant or identifier after '$' or '/'.");
  311. return nullptr;
  312. }
  313. path += String(tokenizer->get_token_constant());
  314. tokenizer->advance();
  315. need_identifier = false;
  316. } break;
  317. case GDScriptTokenizer::TK_OP_DIV: {
  318. if (need_identifier) {
  319. done = true;
  320. break;
  321. }
  322. path += "/";
  323. tokenizer->advance();
  324. need_identifier = true;
  325. } break;
  326. default: {
  327. // Instead of checking for TK_IDENTIFIER, we check with is_token_literal, as this allows us to use match/sync/etc. as a name
  328. if (need_identifier && tokenizer->is_token_literal()) {
  329. path += String(tokenizer->get_token_literal());
  330. tokenizer->advance();
  331. need_identifier = false;
  332. } else {
  333. done = true;
  334. }
  335. break;
  336. }
  337. }
  338. }
  339. if (path == "") {
  340. _set_error("Path expected after $.");
  341. return nullptr;
  342. }
  343. OperatorNode *op = alloc_node<OperatorNode>();
  344. op->op = OperatorNode::OP_CALL;
  345. op->line = line;
  346. op->arguments.push_back(alloc_node<SelfNode>());
  347. op->arguments[0]->line = line;
  348. IdentifierNode *funcname = alloc_node<IdentifierNode>();
  349. funcname->name = "get_node";
  350. funcname->line = line;
  351. op->arguments.push_back(funcname);
  352. ConstantNode *nodepath = alloc_node<ConstantNode>();
  353. nodepath->value = NodePath(StringName(path));
  354. nodepath->datatype = _type_from_variant(nodepath->value);
  355. nodepath->line = line;
  356. op->arguments.push_back(nodepath);
  357. expr = op;
  358. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  359. tokenizer->advance();
  360. continue; //no point in cursor in the middle of expression
  361. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) {
  362. //constant defined by tokenizer
  363. ConstantNode *constant = alloc_node<ConstantNode>();
  364. constant->value = tokenizer->get_token_constant();
  365. constant->datatype = _type_from_variant(constant->value);
  366. tokenizer->advance();
  367. expr = constant;
  368. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_PI) {
  369. //constant defined by tokenizer
  370. ConstantNode *constant = alloc_node<ConstantNode>();
  371. constant->value = Math_PI;
  372. constant->datatype = _type_from_variant(constant->value);
  373. tokenizer->advance();
  374. expr = constant;
  375. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_TAU) {
  376. //constant defined by tokenizer
  377. ConstantNode *constant = alloc_node<ConstantNode>();
  378. constant->value = Math_TAU;
  379. constant->datatype = _type_from_variant(constant->value);
  380. tokenizer->advance();
  381. expr = constant;
  382. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_INF) {
  383. //constant defined by tokenizer
  384. ConstantNode *constant = alloc_node<ConstantNode>();
  385. constant->value = Math_INF;
  386. constant->datatype = _type_from_variant(constant->value);
  387. tokenizer->advance();
  388. expr = constant;
  389. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_NAN) {
  390. //constant defined by tokenizer
  391. ConstantNode *constant = alloc_node<ConstantNode>();
  392. constant->value = Math_NAN;
  393. constant->datatype = _type_from_variant(constant->value);
  394. tokenizer->advance();
  395. expr = constant;
  396. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_PRELOAD) {
  397. //constant defined by tokenizer
  398. tokenizer->advance();
  399. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  400. _set_error("Expected '(' after 'preload'");
  401. return nullptr;
  402. }
  403. tokenizer->advance();
  404. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  405. completion_cursor = StringName();
  406. completion_node = p_parent;
  407. completion_type = COMPLETION_RESOURCE_PATH;
  408. completion_class = current_class;
  409. completion_function = current_function;
  410. completion_line = tokenizer->get_token_line();
  411. completion_block = current_block;
  412. completion_argument = 0;
  413. completion_found = true;
  414. tokenizer->advance();
  415. }
  416. String path;
  417. bool found_constant = false;
  418. bool valid = false;
  419. ConstantNode *cn;
  420. parenthesis++;
  421. Node *subexpr = _parse_and_reduce_expression(p_parent, p_static);
  422. parenthesis--;
  423. if (subexpr) {
  424. if (subexpr->type == Node::TYPE_CONSTANT) {
  425. cn = static_cast<ConstantNode *>(subexpr);
  426. found_constant = true;
  427. }
  428. if (subexpr->type == Node::TYPE_IDENTIFIER) {
  429. IdentifierNode *in = static_cast<IdentifierNode *>(subexpr);
  430. // Try to find the constant expression by the identifier
  431. if (current_class->constant_expressions.has(in->name)) {
  432. Node *cn_exp = current_class->constant_expressions[in->name].expression;
  433. if (cn_exp->type == Node::TYPE_CONSTANT) {
  434. cn = static_cast<ConstantNode *>(cn_exp);
  435. found_constant = true;
  436. }
  437. }
  438. }
  439. if (found_constant && cn->value.get_type() == Variant::STRING) {
  440. valid = true;
  441. path = (String)cn->value;
  442. }
  443. }
  444. if (!valid) {
  445. _set_error("expected string constant as 'preload' argument.");
  446. return nullptr;
  447. }
  448. if (!path.is_abs_path() && base_path != "") {
  449. path = base_path.plus_file(path);
  450. }
  451. path = path.replace("///", "//").simplify_path();
  452. if (path == self_path) {
  453. _set_error("Can't preload itself (use 'get_script()').");
  454. return nullptr;
  455. }
  456. Ref<Resource> res;
  457. dependencies.push_back(path);
  458. if (!dependencies_only) {
  459. if (!validating) {
  460. //this can be too slow for just validating code
  461. if (for_completion && ScriptCodeCompletionCache::get_singleton() && FileAccess::exists(path)) {
  462. res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
  463. } else if (!for_completion || FileAccess::exists(path)) {
  464. res = ResourceLoader::load(path);
  465. }
  466. } else {
  467. if (!FileAccess::exists(path)) {
  468. _set_error("Can't preload resource at path: " + path);
  469. return nullptr;
  470. } else if (ScriptCodeCompletionCache::get_singleton()) {
  471. res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
  472. }
  473. }
  474. if (!res.is_valid()) {
  475. _set_error("Can't preload resource at path: " + path);
  476. return nullptr;
  477. }
  478. }
  479. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  480. _set_error("Expected ')' after 'preload' path");
  481. return nullptr;
  482. }
  483. Ref<GDScript> gds = res;
  484. if (gds.is_valid() && !gds->is_valid()) {
  485. _set_error("Couldn't fully preload the script, possible cyclic reference or compilation error. Use \"load()\" instead if a cyclic reference is intended.");
  486. return nullptr;
  487. }
  488. tokenizer->advance();
  489. ConstantNode *constant = alloc_node<ConstantNode>();
  490. constant->value = res;
  491. constant->datatype = _type_from_variant(constant->value);
  492. expr = constant;
  493. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_YIELD) {
  494. if (!current_function) {
  495. _set_error("\"yield()\" can only be used inside function blocks.");
  496. return nullptr;
  497. }
  498. current_function->has_yield = true;
  499. tokenizer->advance();
  500. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  501. _set_error("Expected \"(\" after \"yield\".");
  502. return nullptr;
  503. }
  504. tokenizer->advance();
  505. OperatorNode *yield = alloc_node<OperatorNode>();
  506. yield->op = OperatorNode::OP_YIELD;
  507. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  508. tokenizer->advance();
  509. }
  510. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  511. expr = yield;
  512. tokenizer->advance();
  513. } else {
  514. parenthesis++;
  515. Node *object = _parse_and_reduce_expression(p_parent, p_static);
  516. if (!object) {
  517. return nullptr;
  518. }
  519. yield->arguments.push_back(object);
  520. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  521. _set_error("Expected \",\" after the first argument of \"yield\".");
  522. return nullptr;
  523. }
  524. tokenizer->advance();
  525. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  526. completion_cursor = StringName();
  527. completion_node = object;
  528. completion_type = COMPLETION_YIELD;
  529. completion_class = current_class;
  530. completion_function = current_function;
  531. completion_line = tokenizer->get_token_line();
  532. completion_argument = 0;
  533. completion_block = current_block;
  534. completion_found = true;
  535. tokenizer->advance();
  536. }
  537. Node *signal = _parse_and_reduce_expression(p_parent, p_static);
  538. if (!signal) {
  539. return nullptr;
  540. }
  541. yield->arguments.push_back(signal);
  542. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  543. _set_error("Expected \")\" after the second argument of \"yield\".");
  544. return nullptr;
  545. }
  546. parenthesis--;
  547. tokenizer->advance();
  548. expr = yield;
  549. }
  550. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_SELF) {
  551. if (p_static) {
  552. _set_error("\"self\" isn't allowed in a static function or constant expression.");
  553. return nullptr;
  554. }
  555. //constant defined by tokenizer
  556. SelfNode *self = alloc_node<SelfNode>();
  557. tokenizer->advance();
  558. expr = self;
  559. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
  560. Variant::Type bi_type = tokenizer->get_token_type();
  561. tokenizer->advance(2);
  562. StringName identifier;
  563. if (_get_completable_identifier(COMPLETION_BUILT_IN_TYPE_CONSTANT, identifier)) {
  564. completion_built_in_constant = bi_type;
  565. }
  566. if (identifier == StringName()) {
  567. _set_error("Built-in type constant or static function expected after \".\".");
  568. return nullptr;
  569. }
  570. if (!Variant::has_constant(bi_type, identifier)) {
  571. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN &&
  572. Variant::is_method_const(bi_type, identifier) &&
  573. Variant::get_method_return_type(bi_type, identifier) == bi_type) {
  574. tokenizer->advance();
  575. OperatorNode *construct = alloc_node<OperatorNode>();
  576. construct->op = OperatorNode::OP_CALL;
  577. TypeNode *tn = alloc_node<TypeNode>();
  578. tn->vtype = bi_type;
  579. construct->arguments.push_back(tn);
  580. OperatorNode *op = alloc_node<OperatorNode>();
  581. op->op = OperatorNode::OP_CALL;
  582. op->arguments.push_back(construct);
  583. IdentifierNode *id = alloc_node<IdentifierNode>();
  584. id->name = identifier;
  585. op->arguments.push_back(id);
  586. if (!_parse_arguments(op, op->arguments, p_static, true, p_parsing_constant)) {
  587. return nullptr;
  588. }
  589. expr = op;
  590. } else {
  591. // Object is a special case
  592. bool valid = false;
  593. if (bi_type == Variant::OBJECT) {
  594. int object_constant = ClassDB::get_integer_constant("Object", identifier, &valid);
  595. if (valid) {
  596. ConstantNode *cn = alloc_node<ConstantNode>();
  597. cn->value = object_constant;
  598. cn->datatype = _type_from_variant(cn->value);
  599. expr = cn;
  600. }
  601. }
  602. if (!valid) {
  603. _set_error("Static constant '" + identifier.operator String() + "' not present in built-in type " + Variant::get_type_name(bi_type) + ".");
  604. return nullptr;
  605. }
  606. }
  607. } else {
  608. ConstantNode *cn = alloc_node<ConstantNode>();
  609. cn->value = Variant::get_constant_value(bi_type, identifier);
  610. cn->datatype = _type_from_variant(cn->value);
  611. expr = cn;
  612. }
  613. } else if (tokenizer->get_token(next_valid_offset) == GDScriptTokenizer::TK_PARENTHESIS_OPEN && tokenizer->is_token_literal()) {
  614. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  615. //function or constructor
  616. OperatorNode *op = alloc_node<OperatorNode>();
  617. op->op = OperatorNode::OP_CALL;
  618. //Do a quick Array and Dictionary Check. Replace if either require no arguments.
  619. bool replaced = false;
  620. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE) {
  621. Variant::Type ct = tokenizer->get_token_type();
  622. if (!p_parsing_constant) {
  623. if (ct == Variant::ARRAY) {
  624. if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  625. ArrayNode *arr = alloc_node<ArrayNode>();
  626. expr = arr;
  627. replaced = true;
  628. tokenizer->advance(3);
  629. }
  630. }
  631. if (ct == Variant::DICTIONARY) {
  632. if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  633. DictionaryNode *dict = alloc_node<DictionaryNode>();
  634. expr = dict;
  635. replaced = true;
  636. tokenizer->advance(3);
  637. }
  638. }
  639. }
  640. if (!replaced) {
  641. TypeNode *tn = alloc_node<TypeNode>();
  642. tn->vtype = tokenizer->get_token_type();
  643. op->arguments.push_back(tn);
  644. tokenizer->advance(2);
  645. }
  646. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_FUNC) {
  647. BuiltInFunctionNode *bn = alloc_node<BuiltInFunctionNode>();
  648. bn->function = tokenizer->get_token_built_in_func();
  649. op->arguments.push_back(bn);
  650. tokenizer->advance(2);
  651. } else {
  652. SelfNode *self = alloc_node<SelfNode>();
  653. op->arguments.push_back(self);
  654. StringName identifier;
  655. if (_get_completable_identifier(COMPLETION_FUNCTION, identifier)) {
  656. }
  657. IdentifierNode *id = alloc_node<IdentifierNode>();
  658. id->name = identifier;
  659. op->arguments.push_back(id);
  660. tokenizer->advance(1);
  661. }
  662. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  663. _make_completable_call(0);
  664. completion_node = op;
  665. }
  666. if (!replaced) {
  667. if (!_parse_arguments(op, op->arguments, p_static, true, p_parsing_constant)) {
  668. return nullptr;
  669. }
  670. expr = op;
  671. }
  672. } else if (tokenizer->is_token_literal(0, true)) {
  673. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  674. //identifier (reference)
  675. const ClassNode *cln = current_class;
  676. bool bfn = false;
  677. StringName identifier;
  678. int id_line = tokenizer->get_token_line();
  679. if (_get_completable_identifier(COMPLETION_IDENTIFIER, identifier)) {
  680. }
  681. BlockNode *b = current_block;
  682. while (!bfn && b) {
  683. if (b->variables.has(identifier)) {
  684. IdentifierNode *id = alloc_node<IdentifierNode>();
  685. id->name = identifier;
  686. id->declared_block = b;
  687. id->line = id_line;
  688. expr = id;
  689. bfn = true;
  690. #ifdef DEBUG_ENABLED
  691. LocalVarNode *lv = b->variables[identifier];
  692. switch (tokenizer->get_token()) {
  693. case GDScriptTokenizer::TK_OP_ASSIGN_ADD:
  694. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_AND:
  695. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_OR:
  696. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_XOR:
  697. case GDScriptTokenizer::TK_OP_ASSIGN_DIV:
  698. case GDScriptTokenizer::TK_OP_ASSIGN_MOD:
  699. case GDScriptTokenizer::TK_OP_ASSIGN_MUL:
  700. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_LEFT:
  701. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT:
  702. case GDScriptTokenizer::TK_OP_ASSIGN_SUB: {
  703. if (lv->assignments == 0) {
  704. if (!lv->datatype.has_type) {
  705. _set_error("Using assignment with operation on a variable that was never assigned.");
  706. return nullptr;
  707. }
  708. _add_warning(GDScriptWarning::UNASSIGNED_VARIABLE_OP_ASSIGN, -1, identifier.operator String());
  709. }
  710. FALLTHROUGH;
  711. }
  712. case GDScriptTokenizer::TK_OP_ASSIGN: {
  713. lv->assignments += 1;
  714. lv->usages--; // Assignment is not really usage
  715. } break;
  716. default: {
  717. lv->usages++;
  718. }
  719. }
  720. #endif // DEBUG_ENABLED
  721. break;
  722. }
  723. b = b->parent_block;
  724. }
  725. if (!bfn && p_parsing_constant) {
  726. if (cln->constant_expressions.has(identifier)) {
  727. expr = cln->constant_expressions[identifier].expression;
  728. bfn = true;
  729. } else if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
  730. //check from constants
  731. ConstantNode *constant = alloc_node<ConstantNode>();
  732. constant->value = GDScriptLanguage::get_singleton()->get_global_array()[GDScriptLanguage::get_singleton()->get_global_map()[identifier]];
  733. constant->datatype = _type_from_variant(constant->value);
  734. constant->line = id_line;
  735. expr = constant;
  736. bfn = true;
  737. }
  738. if (!bfn && GDScriptLanguage::get_singleton()->get_named_globals_map().has(identifier)) {
  739. //check from singletons
  740. ConstantNode *constant = alloc_node<ConstantNode>();
  741. constant->value = GDScriptLanguage::get_singleton()->get_named_globals_map()[identifier];
  742. expr = constant;
  743. bfn = true;
  744. }
  745. if (!dependencies_only) {
  746. if (!bfn && ScriptServer::is_global_class(identifier)) {
  747. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(identifier));
  748. if (scr.is_valid() && scr->is_valid()) {
  749. ConstantNode *constant = alloc_node<ConstantNode>();
  750. constant->value = scr;
  751. expr = constant;
  752. bfn = true;
  753. }
  754. }
  755. // Check parents for the constant
  756. if (!bfn) {
  757. // Using current_class instead of cln here, since cln is const*
  758. _determine_inheritance(current_class, false);
  759. if (cln->base_type.has_type && cln->base_type.kind == DataType::GDSCRIPT && cln->base_type.script_type->is_valid()) {
  760. Map<StringName, Variant> parent_constants;
  761. current_class->base_type.script_type->get_constants(&parent_constants);
  762. if (parent_constants.has(identifier)) {
  763. ConstantNode *constant = alloc_node<ConstantNode>();
  764. constant->value = parent_constants[identifier];
  765. expr = constant;
  766. bfn = true;
  767. }
  768. }
  769. }
  770. }
  771. }
  772. if (!bfn) {
  773. #ifdef DEBUG_ENABLED
  774. if (current_function) {
  775. int arg_idx = current_function->arguments.find(identifier);
  776. if (arg_idx != -1) {
  777. switch (tokenizer->get_token()) {
  778. case GDScriptTokenizer::TK_OP_ASSIGN_ADD:
  779. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_AND:
  780. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_OR:
  781. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_XOR:
  782. case GDScriptTokenizer::TK_OP_ASSIGN_DIV:
  783. case GDScriptTokenizer::TK_OP_ASSIGN_MOD:
  784. case GDScriptTokenizer::TK_OP_ASSIGN_MUL:
  785. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_LEFT:
  786. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT:
  787. case GDScriptTokenizer::TK_OP_ASSIGN_SUB:
  788. case GDScriptTokenizer::TK_OP_ASSIGN: {
  789. // Assignment is not really usage
  790. } break;
  791. default: {
  792. current_function->arguments_usage.write[arg_idx] = current_function->arguments_usage[arg_idx] + 1;
  793. }
  794. }
  795. }
  796. }
  797. #endif // DEBUG_ENABLED
  798. IdentifierNode *id = alloc_node<IdentifierNode>();
  799. id->name = identifier;
  800. id->line = id_line;
  801. expr = id;
  802. }
  803. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ADD || tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB || tokenizer->get_token() == GDScriptTokenizer::TK_OP_NOT || tokenizer->get_token() == GDScriptTokenizer::TK_OP_BIT_INVERT) {
  804. //single prefix operators like !expr +expr -expr ++expr --expr
  805. alloc_node<OperatorNode>();
  806. Expression e;
  807. e.is_op = true;
  808. switch (tokenizer->get_token()) {
  809. case GDScriptTokenizer::TK_OP_ADD:
  810. e.op = OperatorNode::OP_POS;
  811. break;
  812. case GDScriptTokenizer::TK_OP_SUB:
  813. e.op = OperatorNode::OP_NEG;
  814. break;
  815. case GDScriptTokenizer::TK_OP_NOT:
  816. e.op = OperatorNode::OP_NOT;
  817. break;
  818. case GDScriptTokenizer::TK_OP_BIT_INVERT:
  819. e.op = OperatorNode::OP_BIT_INVERT;
  820. break;
  821. default: {
  822. }
  823. }
  824. tokenizer->advance();
  825. if (e.op != OperatorNode::OP_NOT && tokenizer->get_token() == GDScriptTokenizer::TK_OP_NOT) {
  826. _set_error("Misplaced 'not'.");
  827. return nullptr;
  828. }
  829. expression.push_back(e);
  830. continue; //only exception, must continue...
  831. /*
  832. Node *subexpr=_parse_expression(op,p_static);
  833. if (!subexpr)
  834. return NULL;
  835. op->arguments.push_back(subexpr);
  836. expr=op;*/
  837. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_IS && tokenizer->get_token(1) == GDScriptTokenizer::TK_BUILT_IN_TYPE) {
  838. // 'is' operator with built-in type
  839. if (!expr) {
  840. _set_error("Expected identifier before 'is' operator");
  841. return nullptr;
  842. }
  843. OperatorNode *op = alloc_node<OperatorNode>();
  844. op->op = OperatorNode::OP_IS_BUILTIN;
  845. op->arguments.push_back(expr);
  846. tokenizer->advance();
  847. TypeNode *tn = alloc_node<TypeNode>();
  848. tn->vtype = tokenizer->get_token_type();
  849. op->arguments.push_back(tn);
  850. tokenizer->advance();
  851. expr = op;
  852. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_OPEN) {
  853. // array
  854. tokenizer->advance();
  855. ArrayNode *arr = alloc_node<ArrayNode>();
  856. bool expecting_comma = false;
  857. while (true) {
  858. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  859. _set_error("Unterminated array");
  860. return nullptr;
  861. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  862. tokenizer->advance();
  863. break;
  864. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  865. tokenizer->advance(); //ignore newline
  866. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  867. if (!expecting_comma) {
  868. _set_error("expression or ']' expected");
  869. return nullptr;
  870. }
  871. expecting_comma = false;
  872. tokenizer->advance(); //ignore newline
  873. } else {
  874. //parse expression
  875. if (expecting_comma) {
  876. _set_error("',' or ']' expected");
  877. return nullptr;
  878. }
  879. Node *n = _parse_expression(arr, p_static, p_allow_assign, p_parsing_constant);
  880. if (!n) {
  881. return nullptr;
  882. }
  883. arr->elements.push_back(n);
  884. expecting_comma = true;
  885. }
  886. }
  887. expr = arr;
  888. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_OPEN) {
  889. // array
  890. tokenizer->advance();
  891. DictionaryNode *dict = alloc_node<DictionaryNode>();
  892. enum DictExpect {
  893. DICT_EXPECT_KEY,
  894. DICT_EXPECT_COLON,
  895. DICT_EXPECT_VALUE,
  896. DICT_EXPECT_COMMA
  897. };
  898. Node *key = nullptr;
  899. Set<Variant> keys;
  900. DictExpect expecting = DICT_EXPECT_KEY;
  901. while (true) {
  902. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  903. _set_error("Unterminated dictionary");
  904. return nullptr;
  905. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  906. if (expecting == DICT_EXPECT_COLON) {
  907. _set_error("':' expected");
  908. return nullptr;
  909. }
  910. if (expecting == DICT_EXPECT_VALUE) {
  911. _set_error("value expected");
  912. return nullptr;
  913. }
  914. tokenizer->advance();
  915. break;
  916. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  917. tokenizer->advance(); //ignore newline
  918. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  919. if (expecting == DICT_EXPECT_KEY) {
  920. _set_error("key or '}' expected");
  921. return nullptr;
  922. }
  923. if (expecting == DICT_EXPECT_VALUE) {
  924. _set_error("value expected");
  925. return nullptr;
  926. }
  927. if (expecting == DICT_EXPECT_COLON) {
  928. _set_error("':' expected");
  929. return nullptr;
  930. }
  931. expecting = DICT_EXPECT_KEY;
  932. tokenizer->advance(); //ignore newline
  933. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  934. if (expecting == DICT_EXPECT_KEY) {
  935. _set_error("key or '}' expected");
  936. return nullptr;
  937. }
  938. if (expecting == DICT_EXPECT_VALUE) {
  939. _set_error("value expected");
  940. return nullptr;
  941. }
  942. if (expecting == DICT_EXPECT_COMMA) {
  943. _set_error("',' or '}' expected");
  944. return nullptr;
  945. }
  946. expecting = DICT_EXPECT_VALUE;
  947. tokenizer->advance(); //ignore newline
  948. } else {
  949. if (expecting == DICT_EXPECT_COMMA) {
  950. _set_error("',' or '}' expected");
  951. return nullptr;
  952. }
  953. if (expecting == DICT_EXPECT_COLON) {
  954. _set_error("':' expected");
  955. return nullptr;
  956. }
  957. if (expecting == DICT_EXPECT_KEY) {
  958. if (tokenizer->is_token_literal() && tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  959. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  960. //lua style identifier, easier to write
  961. ConstantNode *cn = alloc_node<ConstantNode>();
  962. cn->value = tokenizer->get_token_literal();
  963. cn->datatype = _type_from_variant(cn->value);
  964. key = cn;
  965. tokenizer->advance(2);
  966. expecting = DICT_EXPECT_VALUE;
  967. } else {
  968. //python/js style more flexible
  969. key = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
  970. if (!key) {
  971. return nullptr;
  972. }
  973. expecting = DICT_EXPECT_COLON;
  974. }
  975. }
  976. if (expecting == DICT_EXPECT_VALUE) {
  977. Node *value = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
  978. if (!value) {
  979. return nullptr;
  980. }
  981. expecting = DICT_EXPECT_COMMA;
  982. const Variant *key_value = _try_to_find_constant_value_for_expression(key);
  983. if (key_value) {
  984. if (keys.has(*key_value)) {
  985. _set_error("Duplicate key \"" + String(*key_value) + "\" found in Dictionary literal",
  986. key->line,
  987. key->column);
  988. return nullptr;
  989. }
  990. keys.insert(*key_value);
  991. }
  992. DictionaryNode::Pair pair;
  993. pair.key = key;
  994. pair.value = value;
  995. dict->elements.push_back(pair);
  996. key = nullptr;
  997. }
  998. }
  999. }
  1000. expr = dict;
  1001. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD && (tokenizer->is_token_literal(1) || tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR)) {
  1002. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  1003. // parent call
  1004. tokenizer->advance(); //goto identifier
  1005. OperatorNode *op = alloc_node<OperatorNode>();
  1006. op->op = OperatorNode::OP_PARENT_CALL;
  1007. /*SelfNode *self = alloc_node<SelfNode>();
  1008. op->arguments.push_back(self);
  1009. forbidden for now */
  1010. StringName identifier;
  1011. bool is_completion = _get_completable_identifier(COMPLETION_PARENT_FUNCTION, identifier) && for_completion;
  1012. IdentifierNode *id = alloc_node<IdentifierNode>();
  1013. id->name = identifier;
  1014. op->arguments.push_back(id);
  1015. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  1016. if (!is_completion) {
  1017. _set_error("Expected '(' for parent function call.");
  1018. return nullptr;
  1019. }
  1020. } else {
  1021. tokenizer->advance();
  1022. if (!_parse_arguments(op, op->arguments, p_static, false, p_parsing_constant)) {
  1023. return nullptr;
  1024. }
  1025. }
  1026. expr = op;
  1027. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && expression.size() > 0 && expression[expression.size() - 1].is_op && expression[expression.size() - 1].op == OperatorNode::OP_IS) {
  1028. Expression e = expression[expression.size() - 1];
  1029. e.op = OperatorNode::OP_IS_BUILTIN;
  1030. expression.write[expression.size() - 1] = e;
  1031. TypeNode *tn = alloc_node<TypeNode>();
  1032. tn->vtype = tokenizer->get_token_type();
  1033. expr = tn;
  1034. tokenizer->advance();
  1035. } else {
  1036. //find list [ or find dictionary {
  1037. _set_error("Error parsing expression, misplaced: " + String(tokenizer->get_token_name(tokenizer->get_token())));
  1038. return nullptr; //nothing
  1039. }
  1040. ERR_FAIL_COND_V_MSG(!expr, nullptr, "GDScriptParser bug, couldn't figure out what expression is.");
  1041. /******************/
  1042. /* Parse Indexing */
  1043. /******************/
  1044. while (true) {
  1045. //expressions can be indexed any number of times
  1046. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  1047. //indexing using "."
  1048. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_CURSOR && !tokenizer->is_token_literal(1)) {
  1049. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  1050. _set_error("Expected identifier as member");
  1051. return nullptr;
  1052. } else if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  1053. //call!!
  1054. OperatorNode *op = alloc_node<OperatorNode>();
  1055. op->op = OperatorNode::OP_CALL;
  1056. tokenizer->advance();
  1057. IdentifierNode *id = alloc_node<IdentifierNode>();
  1058. StringName identifier;
  1059. if (_get_completable_identifier(COMPLETION_METHOD, identifier)) {
  1060. completion_node = op;
  1061. //indexing stuff
  1062. }
  1063. id->name = identifier;
  1064. op->arguments.push_back(expr); // call what
  1065. op->arguments.push_back(id); // call func
  1066. //get arguments
  1067. tokenizer->advance(1);
  1068. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  1069. _make_completable_call(0);
  1070. completion_node = op;
  1071. }
  1072. if (!_parse_arguments(op, op->arguments, p_static, true, p_parsing_constant)) {
  1073. return nullptr;
  1074. }
  1075. expr = op;
  1076. } else {
  1077. //simple indexing!
  1078. OperatorNode *op = alloc_node<OperatorNode>();
  1079. op->op = OperatorNode::OP_INDEX_NAMED;
  1080. tokenizer->advance();
  1081. StringName identifier;
  1082. if (_get_completable_identifier(COMPLETION_INDEX, identifier)) {
  1083. if (identifier == StringName()) {
  1084. identifier = "@temp"; //so it parses alright
  1085. }
  1086. completion_node = op;
  1087. //indexing stuff
  1088. }
  1089. IdentifierNode *id = alloc_node<IdentifierNode>();
  1090. id->name = identifier;
  1091. op->arguments.push_back(expr);
  1092. op->arguments.push_back(id);
  1093. expr = op;
  1094. }
  1095. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_OPEN) {
  1096. //indexing using "[]"
  1097. OperatorNode *op = alloc_node<OperatorNode>();
  1098. op->op = OperatorNode::OP_INDEX;
  1099. tokenizer->advance(1);
  1100. parenthesis++;
  1101. Node *subexpr = _parse_expression(op, p_static, p_allow_assign, p_parsing_constant);
  1102. parenthesis--;
  1103. if (!subexpr) {
  1104. return nullptr;
  1105. }
  1106. if (tokenizer->get_token() != GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1107. _set_error("Expected ']'");
  1108. return nullptr;
  1109. }
  1110. op->arguments.push_back(expr);
  1111. op->arguments.push_back(subexpr);
  1112. tokenizer->advance(1);
  1113. expr = op;
  1114. } else {
  1115. break;
  1116. }
  1117. }
  1118. /*****************/
  1119. /* Parse Casting */
  1120. /*****************/
  1121. bool has_casting = expr->type == Node::TYPE_CAST;
  1122. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_AS) {
  1123. if (has_casting) {
  1124. _set_error("Unexpected 'as'.");
  1125. return nullptr;
  1126. }
  1127. CastNode *cn = alloc_node<CastNode>();
  1128. if (!_parse_type(cn->cast_type)) {
  1129. _set_error("Expected type after 'as'.");
  1130. return nullptr;
  1131. }
  1132. has_casting = true;
  1133. cn->source_node = expr;
  1134. expr = cn;
  1135. }
  1136. /******************/
  1137. /* Parse Operator */
  1138. /******************/
  1139. if (parenthesis > 0) {
  1140. //remove empty space (only allowed if inside parenthesis
  1141. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  1142. tokenizer->advance();
  1143. }
  1144. }
  1145. Expression e;
  1146. e.is_op = false;
  1147. e.node = expr;
  1148. expression.push_back(e);
  1149. // determine which operator is next
  1150. OperatorNode::Operator op;
  1151. bool valid = true;
  1152. //assign, if allowed is only allowed on the first operator
  1153. #define _VALIDATE_ASSIGN \
  1154. if (!p_allow_assign || has_casting) { \
  1155. _set_error("Unexpected assign."); \
  1156. return NULL; \
  1157. } \
  1158. p_allow_assign = false;
  1159. switch (tokenizer->get_token()) { //see operator
  1160. case GDScriptTokenizer::TK_OP_IN:
  1161. op = OperatorNode::OP_IN;
  1162. break;
  1163. case GDScriptTokenizer::TK_OP_EQUAL:
  1164. op = OperatorNode::OP_EQUAL;
  1165. break;
  1166. case GDScriptTokenizer::TK_OP_NOT_EQUAL:
  1167. op = OperatorNode::OP_NOT_EQUAL;
  1168. break;
  1169. case GDScriptTokenizer::TK_OP_LESS:
  1170. op = OperatorNode::OP_LESS;
  1171. break;
  1172. case GDScriptTokenizer::TK_OP_LESS_EQUAL:
  1173. op = OperatorNode::OP_LESS_EQUAL;
  1174. break;
  1175. case GDScriptTokenizer::TK_OP_GREATER:
  1176. op = OperatorNode::OP_GREATER;
  1177. break;
  1178. case GDScriptTokenizer::TK_OP_GREATER_EQUAL:
  1179. op = OperatorNode::OP_GREATER_EQUAL;
  1180. break;
  1181. case GDScriptTokenizer::TK_OP_AND:
  1182. op = OperatorNode::OP_AND;
  1183. break;
  1184. case GDScriptTokenizer::TK_OP_OR:
  1185. op = OperatorNode::OP_OR;
  1186. break;
  1187. case GDScriptTokenizer::TK_OP_ADD:
  1188. op = OperatorNode::OP_ADD;
  1189. break;
  1190. case GDScriptTokenizer::TK_OP_SUB:
  1191. op = OperatorNode::OP_SUB;
  1192. break;
  1193. case GDScriptTokenizer::TK_OP_MUL:
  1194. op = OperatorNode::OP_MUL;
  1195. break;
  1196. case GDScriptTokenizer::TK_OP_DIV:
  1197. op = OperatorNode::OP_DIV;
  1198. break;
  1199. case GDScriptTokenizer::TK_OP_MOD:
  1200. op = OperatorNode::OP_MOD;
  1201. break;
  1202. //case GDScriptTokenizer::TK_OP_NEG: op=OperatorNode::OP_NEG ; break;
  1203. case GDScriptTokenizer::TK_OP_SHIFT_LEFT:
  1204. op = OperatorNode::OP_SHIFT_LEFT;
  1205. break;
  1206. case GDScriptTokenizer::TK_OP_SHIFT_RIGHT:
  1207. op = OperatorNode::OP_SHIFT_RIGHT;
  1208. break;
  1209. case GDScriptTokenizer::TK_OP_ASSIGN: {
  1210. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN;
  1211. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR) {
  1212. //code complete assignment
  1213. completion_type = COMPLETION_ASSIGN;
  1214. completion_node = expr;
  1215. completion_class = current_class;
  1216. completion_function = current_function;
  1217. completion_line = tokenizer->get_token_line();
  1218. completion_block = current_block;
  1219. completion_found = true;
  1220. tokenizer->advance();
  1221. }
  1222. } break;
  1223. case GDScriptTokenizer::TK_OP_ASSIGN_ADD:
  1224. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_ADD;
  1225. break;
  1226. case GDScriptTokenizer::TK_OP_ASSIGN_SUB:
  1227. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SUB;
  1228. break;
  1229. case GDScriptTokenizer::TK_OP_ASSIGN_MUL:
  1230. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MUL;
  1231. break;
  1232. case GDScriptTokenizer::TK_OP_ASSIGN_DIV:
  1233. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_DIV;
  1234. break;
  1235. case GDScriptTokenizer::TK_OP_ASSIGN_MOD:
  1236. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MOD;
  1237. break;
  1238. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_LEFT:
  1239. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_LEFT;
  1240. break;
  1241. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT:
  1242. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_RIGHT;
  1243. break;
  1244. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_AND:
  1245. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_AND;
  1246. break;
  1247. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_OR:
  1248. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_OR;
  1249. break;
  1250. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_XOR:
  1251. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_XOR;
  1252. break;
  1253. case GDScriptTokenizer::TK_OP_BIT_AND:
  1254. op = OperatorNode::OP_BIT_AND;
  1255. break;
  1256. case GDScriptTokenizer::TK_OP_BIT_OR:
  1257. op = OperatorNode::OP_BIT_OR;
  1258. break;
  1259. case GDScriptTokenizer::TK_OP_BIT_XOR:
  1260. op = OperatorNode::OP_BIT_XOR;
  1261. break;
  1262. case GDScriptTokenizer::TK_PR_IS:
  1263. op = OperatorNode::OP_IS;
  1264. break;
  1265. case GDScriptTokenizer::TK_CF_IF:
  1266. op = OperatorNode::OP_TERNARY_IF;
  1267. break;
  1268. case GDScriptTokenizer::TK_CF_ELSE:
  1269. op = OperatorNode::OP_TERNARY_ELSE;
  1270. break;
  1271. default:
  1272. valid = false;
  1273. break;
  1274. }
  1275. if (valid) {
  1276. e.is_op = true;
  1277. e.op = op;
  1278. expression.push_back(e);
  1279. tokenizer->advance();
  1280. } else {
  1281. break;
  1282. }
  1283. }
  1284. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  1285. while (expression.size() > 1) {
  1286. int next_op = -1;
  1287. int min_priority = 0xFFFFF;
  1288. bool is_unary = false;
  1289. bool is_ternary = false;
  1290. for (int i = 0; i < expression.size(); i++) {
  1291. if (!expression[i].is_op) {
  1292. continue;
  1293. }
  1294. int priority;
  1295. bool unary = false;
  1296. bool ternary = false;
  1297. bool error = false;
  1298. bool right_to_left = false;
  1299. switch (expression[i].op) {
  1300. case OperatorNode::OP_IS:
  1301. case OperatorNode::OP_IS_BUILTIN:
  1302. priority = -1;
  1303. break; //before anything
  1304. case OperatorNode::OP_BIT_INVERT:
  1305. priority = 0;
  1306. unary = true;
  1307. break;
  1308. case OperatorNode::OP_NEG:
  1309. case OperatorNode::OP_POS:
  1310. priority = 1;
  1311. unary = true;
  1312. break;
  1313. case OperatorNode::OP_MUL:
  1314. priority = 2;
  1315. break;
  1316. case OperatorNode::OP_DIV:
  1317. priority = 2;
  1318. break;
  1319. case OperatorNode::OP_MOD:
  1320. priority = 2;
  1321. break;
  1322. case OperatorNode::OP_ADD:
  1323. priority = 3;
  1324. break;
  1325. case OperatorNode::OP_SUB:
  1326. priority = 3;
  1327. break;
  1328. case OperatorNode::OP_SHIFT_LEFT:
  1329. priority = 4;
  1330. break;
  1331. case OperatorNode::OP_SHIFT_RIGHT:
  1332. priority = 4;
  1333. break;
  1334. case OperatorNode::OP_BIT_AND:
  1335. priority = 5;
  1336. break;
  1337. case OperatorNode::OP_BIT_XOR:
  1338. priority = 6;
  1339. break;
  1340. case OperatorNode::OP_BIT_OR:
  1341. priority = 7;
  1342. break;
  1343. case OperatorNode::OP_LESS:
  1344. priority = 8;
  1345. break;
  1346. case OperatorNode::OP_LESS_EQUAL:
  1347. priority = 8;
  1348. break;
  1349. case OperatorNode::OP_GREATER:
  1350. priority = 8;
  1351. break;
  1352. case OperatorNode::OP_GREATER_EQUAL:
  1353. priority = 8;
  1354. break;
  1355. case OperatorNode::OP_EQUAL:
  1356. priority = 8;
  1357. break;
  1358. case OperatorNode::OP_NOT_EQUAL:
  1359. priority = 8;
  1360. break;
  1361. case OperatorNode::OP_IN:
  1362. priority = 10;
  1363. break;
  1364. case OperatorNode::OP_NOT:
  1365. priority = 11;
  1366. unary = true;
  1367. break;
  1368. case OperatorNode::OP_AND:
  1369. priority = 12;
  1370. break;
  1371. case OperatorNode::OP_OR:
  1372. priority = 13;
  1373. break;
  1374. case OperatorNode::OP_TERNARY_IF:
  1375. priority = 14;
  1376. ternary = true;
  1377. right_to_left = true;
  1378. break;
  1379. case OperatorNode::OP_TERNARY_ELSE:
  1380. priority = 14;
  1381. error = true;
  1382. // Rigth-to-left should be false in this case, otherwise it would always error.
  1383. break;
  1384. case OperatorNode::OP_ASSIGN:
  1385. priority = 15;
  1386. break;
  1387. case OperatorNode::OP_ASSIGN_ADD:
  1388. priority = 15;
  1389. break;
  1390. case OperatorNode::OP_ASSIGN_SUB:
  1391. priority = 15;
  1392. break;
  1393. case OperatorNode::OP_ASSIGN_MUL:
  1394. priority = 15;
  1395. break;
  1396. case OperatorNode::OP_ASSIGN_DIV:
  1397. priority = 15;
  1398. break;
  1399. case OperatorNode::OP_ASSIGN_MOD:
  1400. priority = 15;
  1401. break;
  1402. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  1403. priority = 15;
  1404. break;
  1405. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  1406. priority = 15;
  1407. break;
  1408. case OperatorNode::OP_ASSIGN_BIT_AND:
  1409. priority = 15;
  1410. break;
  1411. case OperatorNode::OP_ASSIGN_BIT_OR:
  1412. priority = 15;
  1413. break;
  1414. case OperatorNode::OP_ASSIGN_BIT_XOR:
  1415. priority = 15;
  1416. break;
  1417. default: {
  1418. _set_error("GDScriptParser bug, invalid operator in expression: " + itos(expression[i].op));
  1419. return nullptr;
  1420. }
  1421. }
  1422. if (priority < min_priority || (right_to_left && priority == min_priority)) {
  1423. // < is used for left to right (default)
  1424. // <= is used for right to left
  1425. if (error) {
  1426. _set_error("Unexpected operator");
  1427. return nullptr;
  1428. }
  1429. next_op = i;
  1430. min_priority = priority;
  1431. is_unary = unary;
  1432. is_ternary = ternary;
  1433. }
  1434. }
  1435. if (next_op == -1) {
  1436. _set_error("Yet another parser bug....");
  1437. ERR_FAIL_V(nullptr);
  1438. }
  1439. // OK! create operator..
  1440. if (is_unary) {
  1441. int expr_pos = next_op;
  1442. while (expression[expr_pos].is_op) {
  1443. expr_pos++;
  1444. if (expr_pos == expression.size()) {
  1445. //can happen..
  1446. _set_error("Unexpected end of expression...");
  1447. return nullptr;
  1448. }
  1449. }
  1450. //consecutively do unary operators
  1451. for (int i = expr_pos - 1; i >= next_op; i--) {
  1452. OperatorNode *op = alloc_node<OperatorNode>();
  1453. op->op = expression[i].op;
  1454. op->arguments.push_back(expression[i + 1].node);
  1455. op->line = op_line; //line might have been changed from a \n
  1456. expression.write[i].is_op = false;
  1457. expression.write[i].node = op;
  1458. expression.remove(i + 1);
  1459. }
  1460. } else if (is_ternary) {
  1461. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1462. _set_error("Parser bug...");
  1463. ERR_FAIL_V(nullptr);
  1464. }
  1465. if (next_op >= (expression.size() - 2) || expression[next_op + 2].op != OperatorNode::OP_TERNARY_ELSE) {
  1466. _set_error("Expected else after ternary if.");
  1467. return nullptr;
  1468. }
  1469. if (next_op >= (expression.size() - 3)) {
  1470. _set_error("Expected value after ternary else.");
  1471. return nullptr;
  1472. }
  1473. OperatorNode *op = alloc_node<OperatorNode>();
  1474. op->op = expression[next_op].op;
  1475. op->line = op_line; //line might have been changed from a \n
  1476. if (expression[next_op - 1].is_op) {
  1477. _set_error("Parser bug...");
  1478. ERR_FAIL_V(nullptr);
  1479. }
  1480. if (expression[next_op + 1].is_op) {
  1481. // this is not invalid and can really appear
  1482. // but it becomes invalid anyway because no binary op
  1483. // can be followed by a unary op in a valid combination,
  1484. // due to how precedence works, unaries will always disappear first
  1485. _set_error("Unexpected two consecutive operators after ternary if.");
  1486. return nullptr;
  1487. }
  1488. if (expression[next_op + 3].is_op) {
  1489. // this is not invalid and can really appear
  1490. // but it becomes invalid anyway because no binary op
  1491. // can be followed by a unary op in a valid combination,
  1492. // due to how precedence works, unaries will always disappear first
  1493. _set_error("Unexpected two consecutive operators after ternary else.");
  1494. return nullptr;
  1495. }
  1496. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as first
  1497. op->arguments.push_back(expression[next_op - 1].node); //left expression goes as when-true
  1498. op->arguments.push_back(expression[next_op + 3].node); //expression after next goes as when-false
  1499. //replace all 3 nodes by this operator and make it an expression
  1500. expression.write[next_op - 1].node = op;
  1501. expression.remove(next_op);
  1502. expression.remove(next_op);
  1503. expression.remove(next_op);
  1504. expression.remove(next_op);
  1505. } else {
  1506. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1507. _set_error("Parser bug...");
  1508. ERR_FAIL_V(nullptr);
  1509. }
  1510. OperatorNode *op = alloc_node<OperatorNode>();
  1511. op->op = expression[next_op].op;
  1512. op->line = op_line; //line might have been changed from a \n
  1513. if (expression[next_op - 1].is_op) {
  1514. _set_error("Parser bug...");
  1515. ERR_FAIL_V(nullptr);
  1516. }
  1517. if (expression[next_op + 1].is_op) {
  1518. // this is not invalid and can really appear
  1519. // but it becomes invalid anyway because no binary op
  1520. // can be followed by a unary op in a valid combination,
  1521. // due to how precedence works, unaries will always disappear first
  1522. _set_error("Unexpected two consecutive operators.");
  1523. return nullptr;
  1524. }
  1525. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  1526. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  1527. //replace all 3 nodes by this operator and make it an expression
  1528. expression.write[next_op - 1].node = op;
  1529. expression.remove(next_op);
  1530. expression.remove(next_op);
  1531. }
  1532. }
  1533. return expression[0].node;
  1534. }
  1535. GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to_const) {
  1536. switch (p_node->type) {
  1537. case Node::TYPE_BUILT_IN_FUNCTION: {
  1538. //many may probably be optimizable
  1539. return p_node;
  1540. } break;
  1541. case Node::TYPE_ARRAY: {
  1542. ArrayNode *an = static_cast<ArrayNode *>(p_node);
  1543. bool all_constants = true;
  1544. for (int i = 0; i < an->elements.size(); i++) {
  1545. an->elements.write[i] = _reduce_expression(an->elements[i], p_to_const);
  1546. if (an->elements[i]->type != Node::TYPE_CONSTANT) {
  1547. all_constants = false;
  1548. }
  1549. }
  1550. if (all_constants && p_to_const) {
  1551. //reduce constant array expression
  1552. ConstantNode *cn = alloc_node<ConstantNode>();
  1553. Array arr;
  1554. arr.resize(an->elements.size());
  1555. for (int i = 0; i < an->elements.size(); i++) {
  1556. ConstantNode *acn = static_cast<ConstantNode *>(an->elements[i]);
  1557. arr[i] = acn->value;
  1558. }
  1559. cn->value = arr;
  1560. cn->datatype = _type_from_variant(cn->value);
  1561. return cn;
  1562. }
  1563. return an;
  1564. } break;
  1565. case Node::TYPE_DICTIONARY: {
  1566. DictionaryNode *dn = static_cast<DictionaryNode *>(p_node);
  1567. bool all_constants = true;
  1568. for (int i = 0; i < dn->elements.size(); i++) {
  1569. dn->elements.write[i].key = _reduce_expression(dn->elements[i].key, p_to_const);
  1570. if (dn->elements[i].key->type != Node::TYPE_CONSTANT) {
  1571. all_constants = false;
  1572. }
  1573. dn->elements.write[i].value = _reduce_expression(dn->elements[i].value, p_to_const);
  1574. if (dn->elements[i].value->type != Node::TYPE_CONSTANT) {
  1575. all_constants = false;
  1576. }
  1577. }
  1578. if (all_constants && p_to_const) {
  1579. //reduce constant array expression
  1580. ConstantNode *cn = alloc_node<ConstantNode>();
  1581. Dictionary dict;
  1582. for (int i = 0; i < dn->elements.size(); i++) {
  1583. ConstantNode *key_c = static_cast<ConstantNode *>(dn->elements[i].key);
  1584. ConstantNode *value_c = static_cast<ConstantNode *>(dn->elements[i].value);
  1585. dict[key_c->value] = value_c->value;
  1586. }
  1587. cn->value = dict;
  1588. cn->datatype = _type_from_variant(cn->value);
  1589. return cn;
  1590. }
  1591. return dn;
  1592. } break;
  1593. case Node::TYPE_OPERATOR: {
  1594. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  1595. bool all_constants = true;
  1596. int last_not_constant = -1;
  1597. for (int i = 0; i < op->arguments.size(); i++) {
  1598. op->arguments.write[i] = _reduce_expression(op->arguments[i], p_to_const);
  1599. if (op->arguments[i]->type != Node::TYPE_CONSTANT) {
  1600. all_constants = false;
  1601. last_not_constant = i;
  1602. }
  1603. }
  1604. if (op->op == OperatorNode::OP_IS) {
  1605. //nothing much
  1606. return op;
  1607. }
  1608. if (op->op == OperatorNode::OP_PARENT_CALL) {
  1609. //nothing much
  1610. return op;
  1611. } else if (op->op == OperatorNode::OP_CALL) {
  1612. //can reduce base type constructors
  1613. if ((op->arguments[0]->type == Node::TYPE_TYPE || (op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && GDScriptFunctions::is_deterministic(static_cast<BuiltInFunctionNode *>(op->arguments[0])->function))) && last_not_constant == 0) {
  1614. //native type constructor or intrinsic function
  1615. const Variant **vptr = nullptr;
  1616. Vector<Variant *> ptrs;
  1617. if (op->arguments.size() > 1) {
  1618. ptrs.resize(op->arguments.size() - 1);
  1619. for (int i = 0; i < ptrs.size(); i++) {
  1620. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i + 1]);
  1621. ptrs.write[i] = &cn->value;
  1622. }
  1623. vptr = (const Variant **)&ptrs[0];
  1624. }
  1625. Variant::CallError ce;
  1626. Variant v;
  1627. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1628. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1629. v = Variant::construct(tn->vtype, vptr, ptrs.size(), ce);
  1630. } else {
  1631. GDScriptFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1632. GDScriptFunctions::call(func, vptr, ptrs.size(), v, ce);
  1633. }
  1634. if (ce.error != Variant::CallError::CALL_OK) {
  1635. String errwhere;
  1636. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1637. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1638. errwhere = "'" + Variant::get_type_name(tn->vtype) + "' constructor";
  1639. } else {
  1640. GDScriptFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1641. errwhere = String("'") + GDScriptFunctions::get_func_name(func) + "' intrinsic function";
  1642. }
  1643. switch (ce.error) {
  1644. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1645. _set_error("Invalid argument (#" + itos(ce.argument + 1) + ") for " + errwhere + ".");
  1646. } break;
  1647. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1648. _set_error("Too many arguments for " + errwhere + ".");
  1649. } break;
  1650. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1651. _set_error("Too few arguments for " + errwhere + ".");
  1652. } break;
  1653. default: {
  1654. _set_error("Invalid arguments for " + errwhere + ".");
  1655. } break;
  1656. }
  1657. error_line = op->line;
  1658. return p_node;
  1659. }
  1660. ConstantNode *cn = alloc_node<ConstantNode>();
  1661. cn->value = v;
  1662. cn->datatype = _type_from_variant(v);
  1663. return cn;
  1664. }
  1665. return op; //don't reduce yet
  1666. } else if (op->op == OperatorNode::OP_YIELD) {
  1667. return op;
  1668. } else if (op->op == OperatorNode::OP_INDEX) {
  1669. //can reduce indices into constant arrays or dictionaries
  1670. if (all_constants) {
  1671. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1672. ConstantNode *cb = static_cast<ConstantNode *>(op->arguments[1]);
  1673. bool valid;
  1674. Variant v = ca->value.get(cb->value, &valid);
  1675. if (!valid) {
  1676. _set_error("invalid index in constant expression");
  1677. error_line = op->line;
  1678. return op;
  1679. }
  1680. ConstantNode *cn = alloc_node<ConstantNode>();
  1681. cn->value = v;
  1682. cn->datatype = _type_from_variant(v);
  1683. return cn;
  1684. }
  1685. return op;
  1686. } else if (op->op == OperatorNode::OP_INDEX_NAMED) {
  1687. if (op->arguments[0]->type == Node::TYPE_CONSTANT && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  1688. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1689. IdentifierNode *ib = static_cast<IdentifierNode *>(op->arguments[1]);
  1690. bool valid;
  1691. Variant v = ca->value.get_named(ib->name, &valid);
  1692. if (!valid) {
  1693. _set_error("invalid index '" + String(ib->name) + "' in constant expression");
  1694. error_line = op->line;
  1695. return op;
  1696. }
  1697. ConstantNode *cn = alloc_node<ConstantNode>();
  1698. cn->value = v;
  1699. cn->datatype = _type_from_variant(v);
  1700. return cn;
  1701. }
  1702. return op;
  1703. }
  1704. //validate assignment (don't assign to constant expression
  1705. switch (op->op) {
  1706. case OperatorNode::OP_ASSIGN:
  1707. case OperatorNode::OP_ASSIGN_ADD:
  1708. case OperatorNode::OP_ASSIGN_SUB:
  1709. case OperatorNode::OP_ASSIGN_MUL:
  1710. case OperatorNode::OP_ASSIGN_DIV:
  1711. case OperatorNode::OP_ASSIGN_MOD:
  1712. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  1713. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  1714. case OperatorNode::OP_ASSIGN_BIT_AND:
  1715. case OperatorNode::OP_ASSIGN_BIT_OR:
  1716. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  1717. if (op->arguments[0]->type == Node::TYPE_CONSTANT) {
  1718. _set_error("Can't assign to constant", tokenizer->get_token_line() - 1);
  1719. error_line = op->line;
  1720. return op;
  1721. } else if (op->arguments[0]->type == Node::TYPE_SELF) {
  1722. _set_error("Can't assign to self.", op->line);
  1723. error_line = op->line;
  1724. return op;
  1725. }
  1726. if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
  1727. OperatorNode *on = static_cast<OperatorNode *>(op->arguments[0]);
  1728. if (on->op != OperatorNode::OP_INDEX && on->op != OperatorNode::OP_INDEX_NAMED) {
  1729. _set_error("Can't assign to an expression", tokenizer->get_token_line() - 1);
  1730. error_line = op->line;
  1731. return op;
  1732. }
  1733. }
  1734. } break;
  1735. default: {
  1736. break;
  1737. }
  1738. }
  1739. //now se if all are constants
  1740. if (!all_constants) {
  1741. return op; //nothing to reduce from here on
  1742. }
  1743. #define _REDUCE_UNARY(m_vop) \
  1744. bool valid = false; \
  1745. Variant res; \
  1746. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, Variant(), res, valid); \
  1747. if (!valid) { \
  1748. _set_error("Invalid operand for unary operator"); \
  1749. error_line = op->line; \
  1750. return p_node; \
  1751. } \
  1752. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1753. cn->value = res; \
  1754. cn->datatype = _type_from_variant(res); \
  1755. return cn;
  1756. #define _REDUCE_BINARY(m_vop) \
  1757. bool valid = false; \
  1758. Variant res; \
  1759. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, static_cast<ConstantNode *>(op->arguments[1])->value, res, valid); \
  1760. if (!valid) { \
  1761. _set_error("Invalid operands for operator"); \
  1762. error_line = op->line; \
  1763. return p_node; \
  1764. } \
  1765. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1766. cn->value = res; \
  1767. cn->datatype = _type_from_variant(res); \
  1768. return cn;
  1769. switch (op->op) {
  1770. //unary operators
  1771. case OperatorNode::OP_NEG: {
  1772. _REDUCE_UNARY(Variant::OP_NEGATE);
  1773. } break;
  1774. case OperatorNode::OP_POS: {
  1775. _REDUCE_UNARY(Variant::OP_POSITIVE);
  1776. } break;
  1777. case OperatorNode::OP_NOT: {
  1778. _REDUCE_UNARY(Variant::OP_NOT);
  1779. } break;
  1780. case OperatorNode::OP_BIT_INVERT: {
  1781. _REDUCE_UNARY(Variant::OP_BIT_NEGATE);
  1782. } break;
  1783. //binary operators (in precedence order)
  1784. case OperatorNode::OP_IN: {
  1785. _REDUCE_BINARY(Variant::OP_IN);
  1786. } break;
  1787. case OperatorNode::OP_EQUAL: {
  1788. _REDUCE_BINARY(Variant::OP_EQUAL);
  1789. } break;
  1790. case OperatorNode::OP_NOT_EQUAL: {
  1791. _REDUCE_BINARY(Variant::OP_NOT_EQUAL);
  1792. } break;
  1793. case OperatorNode::OP_LESS: {
  1794. _REDUCE_BINARY(Variant::OP_LESS);
  1795. } break;
  1796. case OperatorNode::OP_LESS_EQUAL: {
  1797. _REDUCE_BINARY(Variant::OP_LESS_EQUAL);
  1798. } break;
  1799. case OperatorNode::OP_GREATER: {
  1800. _REDUCE_BINARY(Variant::OP_GREATER);
  1801. } break;
  1802. case OperatorNode::OP_GREATER_EQUAL: {
  1803. _REDUCE_BINARY(Variant::OP_GREATER_EQUAL);
  1804. } break;
  1805. case OperatorNode::OP_AND: {
  1806. _REDUCE_BINARY(Variant::OP_AND);
  1807. } break;
  1808. case OperatorNode::OP_OR: {
  1809. _REDUCE_BINARY(Variant::OP_OR);
  1810. } break;
  1811. case OperatorNode::OP_ADD: {
  1812. _REDUCE_BINARY(Variant::OP_ADD);
  1813. } break;
  1814. case OperatorNode::OP_SUB: {
  1815. _REDUCE_BINARY(Variant::OP_SUBTRACT);
  1816. } break;
  1817. case OperatorNode::OP_MUL: {
  1818. _REDUCE_BINARY(Variant::OP_MULTIPLY);
  1819. } break;
  1820. case OperatorNode::OP_DIV: {
  1821. _REDUCE_BINARY(Variant::OP_DIVIDE);
  1822. } break;
  1823. case OperatorNode::OP_MOD: {
  1824. _REDUCE_BINARY(Variant::OP_MODULE);
  1825. } break;
  1826. case OperatorNode::OP_SHIFT_LEFT: {
  1827. _REDUCE_BINARY(Variant::OP_SHIFT_LEFT);
  1828. } break;
  1829. case OperatorNode::OP_SHIFT_RIGHT: {
  1830. _REDUCE_BINARY(Variant::OP_SHIFT_RIGHT);
  1831. } break;
  1832. case OperatorNode::OP_BIT_AND: {
  1833. _REDUCE_BINARY(Variant::OP_BIT_AND);
  1834. } break;
  1835. case OperatorNode::OP_BIT_OR: {
  1836. _REDUCE_BINARY(Variant::OP_BIT_OR);
  1837. } break;
  1838. case OperatorNode::OP_BIT_XOR: {
  1839. _REDUCE_BINARY(Variant::OP_BIT_XOR);
  1840. } break;
  1841. case OperatorNode::OP_TERNARY_IF: {
  1842. if (static_cast<ConstantNode *>(op->arguments[0])->value.booleanize()) {
  1843. return op->arguments[1];
  1844. } else {
  1845. return op->arguments[2];
  1846. }
  1847. } break;
  1848. default: {
  1849. ERR_FAIL_V(op);
  1850. }
  1851. }
  1852. } break;
  1853. default: {
  1854. return p_node;
  1855. } break;
  1856. }
  1857. }
  1858. GDScriptParser::Node *GDScriptParser::_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const, bool p_allow_assign) {
  1859. Node *expr = _parse_expression(p_parent, p_static, p_allow_assign, p_reduce_const);
  1860. if (!expr || error_set) {
  1861. return nullptr;
  1862. }
  1863. expr = _reduce_expression(expr, p_reduce_const);
  1864. if (!expr || error_set) {
  1865. return nullptr;
  1866. }
  1867. return expr;
  1868. }
  1869. bool GDScriptParser::_reduce_export_var_type(Variant &p_value, int p_line) {
  1870. if (p_value.get_type() == Variant::ARRAY) {
  1871. Array arr = p_value;
  1872. for (int i = 0; i < arr.size(); i++) {
  1873. if (!_reduce_export_var_type(arr[i], p_line)) {
  1874. return false;
  1875. }
  1876. }
  1877. return true;
  1878. }
  1879. if (p_value.get_type() == Variant::DICTIONARY) {
  1880. Dictionary dict = p_value;
  1881. for (int i = 0; i < dict.size(); i++) {
  1882. Variant value = dict.get_value_at_index(i);
  1883. if (!_reduce_export_var_type(value, p_line)) {
  1884. return false;
  1885. }
  1886. }
  1887. return true;
  1888. }
  1889. // validate type
  1890. DataType type = _type_from_variant(p_value);
  1891. if (type.kind == DataType::BUILTIN) {
  1892. return true;
  1893. } else if (type.kind == DataType::NATIVE) {
  1894. if (ClassDB::is_parent_class(type.native_type, "Resource")) {
  1895. return true;
  1896. }
  1897. }
  1898. _set_error("Invalid export type. Only built-in and native resource types can be exported.", p_line);
  1899. return false;
  1900. }
  1901. const Variant *GDScriptParser::_try_to_find_constant_value_for_expression(const Node *p_expr) const {
  1902. if (p_expr->type == Node::TYPE_CONSTANT) {
  1903. return &(static_cast<const ConstantNode *>(p_expr)->value);
  1904. } else if (p_expr->type == Node::TYPE_IDENTIFIER) {
  1905. const StringName &name = static_cast<const IdentifierNode *>(p_expr)->name;
  1906. const Map<StringName, ClassNode::Constant>::Element *element =
  1907. current_class->constant_expressions.find(name);
  1908. if (element) {
  1909. Node *cn_exp = element->value().expression;
  1910. if (cn_exp->type == Node::TYPE_CONSTANT) {
  1911. return &(static_cast<ConstantNode *>(cn_exp)->value);
  1912. }
  1913. }
  1914. } else if (p_expr->type == Node::TYPE_OPERATOR) {
  1915. // Check if expression `p_expr` is a named enum (e.g. `State.IDLE`).
  1916. const OperatorNode *op_node = static_cast<const OperatorNode *>(p_expr);
  1917. if (op_node->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  1918. const Vector<Node *> &op_args = op_node->arguments;
  1919. if (op_args.size() < 2) {
  1920. return nullptr; // Invalid expression.
  1921. }
  1922. if (op_args[0]->type != Node::TYPE_IDENTIFIER || op_args[1]->type != Node::TYPE_IDENTIFIER) {
  1923. return nullptr; // Not an enum expression.
  1924. }
  1925. const StringName &enum_name = static_cast<const IdentifierNode *>(op_args[0])->name;
  1926. const StringName &const_name = static_cast<const IdentifierNode *>(op_args[1])->name;
  1927. Map<StringName, ClassNode::Constant>::Element *element =
  1928. current_class->constant_expressions.find(enum_name);
  1929. if (element) {
  1930. Node *cn_exp = element->value().expression;
  1931. if (cn_exp->type == Node::TYPE_CONSTANT) {
  1932. const Dictionary &enum_dict = static_cast<ConstantNode *>(cn_exp)->value;
  1933. return enum_dict.getptr(const_name);
  1934. }
  1935. }
  1936. }
  1937. }
  1938. return nullptr;
  1939. }
  1940. bool GDScriptParser::_recover_from_completion() {
  1941. if (!completion_found) {
  1942. return false; //can't recover if no completion
  1943. }
  1944. //skip stuff until newline
  1945. while (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE && tokenizer->get_token() != GDScriptTokenizer::TK_EOF && tokenizer->get_token() != GDScriptTokenizer::TK_ERROR) {
  1946. tokenizer->advance();
  1947. }
  1948. completion_found = false;
  1949. error_set = false;
  1950. if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) {
  1951. error_set = true;
  1952. }
  1953. return true;
  1954. }
  1955. GDScriptParser::PatternNode *GDScriptParser::_parse_pattern(bool p_static) {
  1956. PatternNode *pattern = alloc_node<PatternNode>();
  1957. GDScriptTokenizer::Token token = tokenizer->get_token();
  1958. if (error_set) {
  1959. return nullptr;
  1960. }
  1961. if (token == GDScriptTokenizer::TK_EOF) {
  1962. return nullptr;
  1963. }
  1964. switch (token) {
  1965. // array
  1966. case GDScriptTokenizer::TK_BRACKET_OPEN: {
  1967. tokenizer->advance();
  1968. pattern->pt_type = GDScriptParser::PatternNode::PT_ARRAY;
  1969. while (true) {
  1970. if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1971. tokenizer->advance();
  1972. break;
  1973. }
  1974. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
  1975. // match everything
  1976. tokenizer->advance(2);
  1977. PatternNode *sub_pattern = alloc_node<PatternNode>();
  1978. sub_pattern->pt_type = GDScriptParser::PatternNode::PT_IGNORE_REST;
  1979. pattern->array.push_back(sub_pattern);
  1980. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA && tokenizer->get_token(1) == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1981. tokenizer->advance(2);
  1982. break;
  1983. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1984. tokenizer->advance(1);
  1985. break;
  1986. } else {
  1987. _set_error("'..' pattern only allowed at the end of an array pattern");
  1988. return nullptr;
  1989. }
  1990. }
  1991. PatternNode *sub_pattern = _parse_pattern(p_static);
  1992. if (!sub_pattern) {
  1993. return nullptr;
  1994. }
  1995. pattern->array.push_back(sub_pattern);
  1996. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  1997. tokenizer->advance();
  1998. continue;
  1999. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  2000. tokenizer->advance();
  2001. break;
  2002. } else {
  2003. _set_error("Not a valid pattern");
  2004. return nullptr;
  2005. }
  2006. }
  2007. } break;
  2008. // bind
  2009. case GDScriptTokenizer::TK_PR_VAR: {
  2010. tokenizer->advance();
  2011. if (!tokenizer->is_token_literal()) {
  2012. _set_error("Expected identifier for binding variable name.");
  2013. return nullptr;
  2014. }
  2015. pattern->pt_type = GDScriptParser::PatternNode::PT_BIND;
  2016. pattern->bind = tokenizer->get_token_literal();
  2017. // Check if variable name is already used
  2018. BlockNode *bl = current_block;
  2019. while (bl) {
  2020. if (bl->variables.has(pattern->bind)) {
  2021. _set_error("Binding name of '" + pattern->bind.operator String() + "' is already declared in this scope.");
  2022. return nullptr;
  2023. }
  2024. bl = bl->parent_block;
  2025. }
  2026. // Create local variable for proper identifier detection later
  2027. LocalVarNode *lv = alloc_node<LocalVarNode>();
  2028. lv->name = pattern->bind;
  2029. current_block->variables.insert(lv->name, lv);
  2030. tokenizer->advance();
  2031. } break;
  2032. // dictionary
  2033. case GDScriptTokenizer::TK_CURLY_BRACKET_OPEN: {
  2034. tokenizer->advance();
  2035. pattern->pt_type = GDScriptParser::PatternNode::PT_DICTIONARY;
  2036. while (true) {
  2037. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  2038. tokenizer->advance();
  2039. break;
  2040. }
  2041. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
  2042. // match everything
  2043. tokenizer->advance(2);
  2044. PatternNode *sub_pattern = alloc_node<PatternNode>();
  2045. sub_pattern->pt_type = PatternNode::PT_IGNORE_REST;
  2046. pattern->array.push_back(sub_pattern);
  2047. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA && tokenizer->get_token(1) == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  2048. tokenizer->advance(2);
  2049. break;
  2050. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  2051. tokenizer->advance(1);
  2052. break;
  2053. } else {
  2054. _set_error("'..' pattern only allowed at the end of a dictionary pattern");
  2055. return nullptr;
  2056. }
  2057. }
  2058. Node *key = _parse_and_reduce_expression(pattern, p_static);
  2059. if (!key) {
  2060. _set_error("Not a valid key in pattern");
  2061. return nullptr;
  2062. }
  2063. if (key->type != GDScriptParser::Node::TYPE_CONSTANT) {
  2064. _set_error("Not a constant expression as key");
  2065. return nullptr;
  2066. }
  2067. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  2068. tokenizer->advance();
  2069. PatternNode *value = _parse_pattern(p_static);
  2070. if (!value) {
  2071. _set_error("Expected pattern in dictionary value");
  2072. return nullptr;
  2073. }
  2074. pattern->dictionary.insert(static_cast<ConstantNode *>(key), value);
  2075. } else {
  2076. pattern->dictionary.insert(static_cast<ConstantNode *>(key), NULL);
  2077. }
  2078. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2079. tokenizer->advance();
  2080. continue;
  2081. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  2082. tokenizer->advance();
  2083. break;
  2084. } else {
  2085. _set_error("Not a valid pattern");
  2086. return nullptr;
  2087. }
  2088. }
  2089. } break;
  2090. case GDScriptTokenizer::TK_WILDCARD: {
  2091. tokenizer->advance();
  2092. pattern->pt_type = PatternNode::PT_WILDCARD;
  2093. } break;
  2094. // all the constants like strings and numbers
  2095. default: {
  2096. Node *value = _parse_and_reduce_expression(pattern, p_static);
  2097. if (!value) {
  2098. _set_error("Expect constant expression or variables in a pattern");
  2099. return nullptr;
  2100. }
  2101. if (value->type == Node::TYPE_OPERATOR) {
  2102. // Maybe it's SomeEnum.VALUE
  2103. Node *current_value = value;
  2104. while (current_value->type == Node::TYPE_OPERATOR) {
  2105. OperatorNode *op_node = static_cast<OperatorNode *>(current_value);
  2106. if (op_node->op != OperatorNode::OP_INDEX_NAMED) {
  2107. _set_error("Invalid operator in pattern. Only index (`A.B`) is allowed");
  2108. return nullptr;
  2109. }
  2110. current_value = op_node->arguments[0];
  2111. }
  2112. if (current_value->type != Node::TYPE_IDENTIFIER) {
  2113. _set_error("Only constant expression or variables allowed in a pattern");
  2114. return nullptr;
  2115. }
  2116. } else if (value->type != Node::TYPE_IDENTIFIER && value->type != Node::TYPE_CONSTANT) {
  2117. _set_error("Only constant expressions or variables allowed in a pattern");
  2118. return nullptr;
  2119. }
  2120. pattern->pt_type = PatternNode::PT_CONSTANT;
  2121. pattern->constant = value;
  2122. } break;
  2123. }
  2124. return pattern;
  2125. }
  2126. void GDScriptParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static) {
  2127. IndentLevel current_level = indent_level.back()->get();
  2128. p_block->has_return = true;
  2129. bool catch_all_appeared = false;
  2130. while (true) {
  2131. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline()) {
  2132. ;
  2133. }
  2134. // GDScriptTokenizer::Token token = tokenizer->get_token();
  2135. if (error_set) {
  2136. return;
  2137. }
  2138. if (current_level.indent > indent_level.back()->get().indent) {
  2139. break; // go back a level
  2140. }
  2141. pending_newline = -1;
  2142. PatternBranchNode *branch = alloc_node<PatternBranchNode>();
  2143. branch->body = alloc_node<BlockNode>();
  2144. branch->body->parent_block = p_block;
  2145. p_block->sub_blocks.push_back(branch->body);
  2146. current_block = branch->body;
  2147. branch->patterns.push_back(_parse_pattern(p_static));
  2148. if (!branch->patterns[0]) {
  2149. break;
  2150. }
  2151. bool has_binding = branch->patterns[0]->pt_type == PatternNode::PT_BIND;
  2152. bool catch_all = has_binding || branch->patterns[0]->pt_type == PatternNode::PT_WILDCARD;
  2153. #ifdef DEBUG_ENABLED
  2154. // Branches after a wildcard or binding are unreachable
  2155. if (catch_all_appeared && !current_function->has_unreachable_code) {
  2156. _add_warning(GDScriptWarning::UNREACHABLE_CODE, -1, current_function->name.operator String());
  2157. current_function->has_unreachable_code = true;
  2158. }
  2159. #endif
  2160. while (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2161. tokenizer->advance();
  2162. branch->patterns.push_back(_parse_pattern(p_static));
  2163. if (!branch->patterns[branch->patterns.size() - 1]) {
  2164. return;
  2165. }
  2166. PatternNode::PatternType pt = branch->patterns[branch->patterns.size() - 1]->pt_type;
  2167. if (pt == PatternNode::PT_BIND) {
  2168. _set_error("Cannot use bindings with multipattern.");
  2169. return;
  2170. }
  2171. catch_all = catch_all || pt == PatternNode::PT_WILDCARD;
  2172. }
  2173. catch_all_appeared = catch_all_appeared || catch_all;
  2174. if (!_enter_indent_block()) {
  2175. _set_error("Expected block in pattern branch");
  2176. return;
  2177. }
  2178. _parse_block(branch->body, p_static);
  2179. current_block = p_block;
  2180. if (!branch->body->has_return) {
  2181. p_block->has_return = false;
  2182. }
  2183. p_branches.push_back(branch);
  2184. }
  2185. // Even if all branches return, there is possibility of default fallthrough
  2186. if (!catch_all_appeared) {
  2187. p_block->has_return = false;
  2188. }
  2189. }
  2190. void GDScriptParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings) {
  2191. const DataType &to_match_type = p_node_to_match->get_datatype();
  2192. switch (p_pattern->pt_type) {
  2193. case PatternNode::PT_CONSTANT: {
  2194. DataType pattern_type = _reduce_node_type(p_pattern->constant);
  2195. if (error_set) {
  2196. return;
  2197. }
  2198. OperatorNode *type_comp = nullptr;
  2199. // static type check if possible
  2200. if (pattern_type.has_type && to_match_type.has_type) {
  2201. if (!_is_type_compatible(to_match_type, pattern_type) && !_is_type_compatible(pattern_type, to_match_type)) {
  2202. _set_error("The pattern type (" + pattern_type.to_string() + ") isn't compatible with the type of the value to match (" + to_match_type.to_string() + ").",
  2203. p_pattern->line);
  2204. return;
  2205. }
  2206. } else {
  2207. // runtime typecheck
  2208. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  2209. typeof_node->function = GDScriptFunctions::TYPE_OF;
  2210. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  2211. typeof_match_value->op = OperatorNode::OP_CALL;
  2212. typeof_match_value->arguments.push_back(typeof_node);
  2213. typeof_match_value->arguments.push_back(p_node_to_match);
  2214. OperatorNode *typeof_pattern_value = alloc_node<OperatorNode>();
  2215. typeof_pattern_value->op = OperatorNode::OP_CALL;
  2216. typeof_pattern_value->arguments.push_back(typeof_node);
  2217. typeof_pattern_value->arguments.push_back(p_pattern->constant);
  2218. type_comp = alloc_node<OperatorNode>();
  2219. type_comp->op = OperatorNode::OP_EQUAL;
  2220. type_comp->arguments.push_back(typeof_match_value);
  2221. type_comp->arguments.push_back(typeof_pattern_value);
  2222. }
  2223. // compare the actual values
  2224. OperatorNode *value_comp = alloc_node<OperatorNode>();
  2225. value_comp->op = OperatorNode::OP_EQUAL;
  2226. value_comp->arguments.push_back(p_pattern->constant);
  2227. value_comp->arguments.push_back(p_node_to_match);
  2228. if (type_comp) {
  2229. OperatorNode *full_comparison = alloc_node<OperatorNode>();
  2230. full_comparison->op = OperatorNode::OP_AND;
  2231. full_comparison->arguments.push_back(type_comp);
  2232. full_comparison->arguments.push_back(value_comp);
  2233. p_resulting_node = full_comparison;
  2234. } else {
  2235. p_resulting_node = value_comp;
  2236. }
  2237. } break;
  2238. case PatternNode::PT_BIND: {
  2239. p_bindings[p_pattern->bind] = p_node_to_match;
  2240. // a bind always matches
  2241. ConstantNode *true_value = alloc_node<ConstantNode>();
  2242. true_value->value = Variant(true);
  2243. p_resulting_node = true_value;
  2244. } break;
  2245. case PatternNode::PT_ARRAY: {
  2246. bool open_ended = false;
  2247. if (p_pattern->array.size() > 0) {
  2248. if (p_pattern->array[p_pattern->array.size() - 1]->pt_type == PatternNode::PT_IGNORE_REST) {
  2249. open_ended = true;
  2250. }
  2251. }
  2252. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() >= length
  2253. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() == length
  2254. {
  2255. OperatorNode *type_comp = nullptr;
  2256. // static type check if possible
  2257. if (to_match_type.has_type) {
  2258. // must be an array
  2259. if (to_match_type.kind != DataType::BUILTIN || to_match_type.builtin_type != Variant::ARRAY) {
  2260. _set_error("Cannot match an array pattern with a non-array expression.", p_pattern->line);
  2261. return;
  2262. }
  2263. } else {
  2264. // runtime typecheck
  2265. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  2266. typeof_node->function = GDScriptFunctions::TYPE_OF;
  2267. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  2268. typeof_match_value->op = OperatorNode::OP_CALL;
  2269. typeof_match_value->arguments.push_back(typeof_node);
  2270. typeof_match_value->arguments.push_back(p_node_to_match);
  2271. IdentifierNode *typeof_array = alloc_node<IdentifierNode>();
  2272. typeof_array->name = "TYPE_ARRAY";
  2273. type_comp = alloc_node<OperatorNode>();
  2274. type_comp->op = OperatorNode::OP_EQUAL;
  2275. type_comp->arguments.push_back(typeof_match_value);
  2276. type_comp->arguments.push_back(typeof_array);
  2277. }
  2278. // size
  2279. ConstantNode *length = alloc_node<ConstantNode>();
  2280. length->value = Variant(open_ended ? p_pattern->array.size() - 1 : p_pattern->array.size());
  2281. OperatorNode *call = alloc_node<OperatorNode>();
  2282. call->op = OperatorNode::OP_CALL;
  2283. call->arguments.push_back(p_node_to_match);
  2284. IdentifierNode *size = alloc_node<IdentifierNode>();
  2285. size->name = "size";
  2286. call->arguments.push_back(size);
  2287. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  2288. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  2289. length_comparison->arguments.push_back(call);
  2290. length_comparison->arguments.push_back(length);
  2291. if (type_comp) {
  2292. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  2293. type_and_length_comparison->op = OperatorNode::OP_AND;
  2294. type_and_length_comparison->arguments.push_back(type_comp);
  2295. type_and_length_comparison->arguments.push_back(length_comparison);
  2296. p_resulting_node = type_and_length_comparison;
  2297. } else {
  2298. p_resulting_node = length_comparison;
  2299. }
  2300. }
  2301. for (int i = 0; i < p_pattern->array.size(); i++) {
  2302. PatternNode *pattern = p_pattern->array[i];
  2303. Node *condition = nullptr;
  2304. ConstantNode *index = alloc_node<ConstantNode>();
  2305. index->value = Variant(i);
  2306. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  2307. indexed_value->op = OperatorNode::OP_INDEX;
  2308. indexed_value->arguments.push_back(p_node_to_match);
  2309. indexed_value->arguments.push_back(index);
  2310. _generate_pattern(pattern, indexed_value, condition, p_bindings);
  2311. // concatenate all the patterns with &&
  2312. OperatorNode *and_node = alloc_node<OperatorNode>();
  2313. and_node->op = OperatorNode::OP_AND;
  2314. and_node->arguments.push_back(p_resulting_node);
  2315. and_node->arguments.push_back(condition);
  2316. p_resulting_node = and_node;
  2317. }
  2318. } break;
  2319. case PatternNode::PT_DICTIONARY: {
  2320. bool open_ended = false;
  2321. if (p_pattern->array.size() > 0) {
  2322. open_ended = true;
  2323. }
  2324. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() >= length
  2325. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() == length
  2326. {
  2327. OperatorNode *type_comp = nullptr;
  2328. // static type check if possible
  2329. if (to_match_type.has_type) {
  2330. // must be an dictionary
  2331. if (to_match_type.kind != DataType::BUILTIN || to_match_type.builtin_type != Variant::DICTIONARY) {
  2332. _set_error("Cannot match an dictionary pattern with a non-dictionary expression.", p_pattern->line);
  2333. return;
  2334. }
  2335. } else {
  2336. // runtime typecheck
  2337. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  2338. typeof_node->function = GDScriptFunctions::TYPE_OF;
  2339. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  2340. typeof_match_value->op = OperatorNode::OP_CALL;
  2341. typeof_match_value->arguments.push_back(typeof_node);
  2342. typeof_match_value->arguments.push_back(p_node_to_match);
  2343. IdentifierNode *typeof_dictionary = alloc_node<IdentifierNode>();
  2344. typeof_dictionary->name = "TYPE_DICTIONARY";
  2345. type_comp = alloc_node<OperatorNode>();
  2346. type_comp->op = OperatorNode::OP_EQUAL;
  2347. type_comp->arguments.push_back(typeof_match_value);
  2348. type_comp->arguments.push_back(typeof_dictionary);
  2349. }
  2350. // size
  2351. ConstantNode *length = alloc_node<ConstantNode>();
  2352. length->value = Variant(open_ended ? p_pattern->dictionary.size() - 1 : p_pattern->dictionary.size());
  2353. OperatorNode *call = alloc_node<OperatorNode>();
  2354. call->op = OperatorNode::OP_CALL;
  2355. call->arguments.push_back(p_node_to_match);
  2356. IdentifierNode *size = alloc_node<IdentifierNode>();
  2357. size->name = "size";
  2358. call->arguments.push_back(size);
  2359. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  2360. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  2361. length_comparison->arguments.push_back(call);
  2362. length_comparison->arguments.push_back(length);
  2363. if (type_comp) {
  2364. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  2365. type_and_length_comparison->op = OperatorNode::OP_AND;
  2366. type_and_length_comparison->arguments.push_back(type_comp);
  2367. type_and_length_comparison->arguments.push_back(length_comparison);
  2368. p_resulting_node = type_and_length_comparison;
  2369. } else {
  2370. p_resulting_node = length_comparison;
  2371. }
  2372. }
  2373. for (Map<ConstantNode *, PatternNode *>::Element *e = p_pattern->dictionary.front(); e; e = e->next()) {
  2374. Node *condition = nullptr;
  2375. // check for has, then for pattern
  2376. IdentifierNode *has = alloc_node<IdentifierNode>();
  2377. has->name = "has";
  2378. OperatorNode *has_call = alloc_node<OperatorNode>();
  2379. has_call->op = OperatorNode::OP_CALL;
  2380. has_call->arguments.push_back(p_node_to_match);
  2381. has_call->arguments.push_back(has);
  2382. has_call->arguments.push_back(e->key());
  2383. if (e->value()) {
  2384. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  2385. indexed_value->op = OperatorNode::OP_INDEX;
  2386. indexed_value->arguments.push_back(p_node_to_match);
  2387. indexed_value->arguments.push_back(e->key());
  2388. _generate_pattern(e->value(), indexed_value, condition, p_bindings);
  2389. OperatorNode *has_and_pattern = alloc_node<OperatorNode>();
  2390. has_and_pattern->op = OperatorNode::OP_AND;
  2391. has_and_pattern->arguments.push_back(has_call);
  2392. has_and_pattern->arguments.push_back(condition);
  2393. condition = has_and_pattern;
  2394. } else {
  2395. condition = has_call;
  2396. }
  2397. // concatenate all the patterns with &&
  2398. OperatorNode *and_node = alloc_node<OperatorNode>();
  2399. and_node->op = OperatorNode::OP_AND;
  2400. and_node->arguments.push_back(p_resulting_node);
  2401. and_node->arguments.push_back(condition);
  2402. p_resulting_node = and_node;
  2403. }
  2404. } break;
  2405. case PatternNode::PT_IGNORE_REST:
  2406. case PatternNode::PT_WILDCARD: {
  2407. // simply generate a `true`
  2408. ConstantNode *true_value = alloc_node<ConstantNode>();
  2409. true_value->value = Variant(true);
  2410. p_resulting_node = true_value;
  2411. } break;
  2412. default: {
  2413. } break;
  2414. }
  2415. }
  2416. void GDScriptParser::_transform_match_statment(MatchNode *p_match_statement) {
  2417. IdentifierNode *id = alloc_node<IdentifierNode>();
  2418. id->name = "#match_value";
  2419. id->line = p_match_statement->line;
  2420. id->datatype = _reduce_node_type(p_match_statement->val_to_match);
  2421. if (id->datatype.has_type) {
  2422. _mark_line_as_safe(id->line);
  2423. } else {
  2424. _mark_line_as_unsafe(id->line);
  2425. }
  2426. if (error_set) {
  2427. return;
  2428. }
  2429. for (int i = 0; i < p_match_statement->branches.size(); i++) {
  2430. PatternBranchNode *branch = p_match_statement->branches[i];
  2431. MatchNode::CompiledPatternBranch compiled_branch;
  2432. compiled_branch.compiled_pattern = nullptr;
  2433. Map<StringName, Node *> binding;
  2434. for (int j = 0; j < branch->patterns.size(); j++) {
  2435. PatternNode *pattern = branch->patterns[j];
  2436. _mark_line_as_safe(pattern->line);
  2437. Map<StringName, Node *> bindings;
  2438. Node *resulting_node = nullptr;
  2439. _generate_pattern(pattern, id, resulting_node, bindings);
  2440. if (!resulting_node) {
  2441. return;
  2442. }
  2443. if (!binding.empty() && !bindings.empty()) {
  2444. _set_error("Multipatterns can't contain bindings");
  2445. return;
  2446. } else {
  2447. binding = bindings;
  2448. }
  2449. // Result is always a boolean
  2450. DataType resulting_node_type;
  2451. resulting_node_type.has_type = true;
  2452. resulting_node_type.is_constant = true;
  2453. resulting_node_type.kind = DataType::BUILTIN;
  2454. resulting_node_type.builtin_type = Variant::BOOL;
  2455. resulting_node->set_datatype(resulting_node_type);
  2456. if (compiled_branch.compiled_pattern) {
  2457. OperatorNode *or_node = alloc_node<OperatorNode>();
  2458. or_node->op = OperatorNode::OP_OR;
  2459. or_node->arguments.push_back(compiled_branch.compiled_pattern);
  2460. or_node->arguments.push_back(resulting_node);
  2461. compiled_branch.compiled_pattern = or_node;
  2462. } else {
  2463. // single pattern | first one
  2464. compiled_branch.compiled_pattern = resulting_node;
  2465. }
  2466. }
  2467. // prepare the body ...hehe
  2468. for (Map<StringName, Node *>::Element *e = binding.front(); e; e = e->next()) {
  2469. if (!branch->body->variables.has(e->key())) {
  2470. _set_error("Parser bug: missing pattern bind variable.", branch->line);
  2471. ERR_FAIL();
  2472. }
  2473. LocalVarNode *local_var = branch->body->variables[e->key()];
  2474. local_var->assign = e->value();
  2475. local_var->set_datatype(local_var->assign->get_datatype());
  2476. local_var->assignments++;
  2477. IdentifierNode *id2 = alloc_node<IdentifierNode>();
  2478. id2->name = local_var->name;
  2479. id2->datatype = local_var->datatype;
  2480. id2->declared_block = branch->body;
  2481. id2->set_datatype(local_var->assign->get_datatype());
  2482. OperatorNode *op = alloc_node<OperatorNode>();
  2483. op->op = OperatorNode::OP_ASSIGN;
  2484. op->arguments.push_back(id2);
  2485. op->arguments.push_back(local_var->assign);
  2486. local_var->assign_op = op;
  2487. branch->body->statements.insert(0, op);
  2488. branch->body->statements.insert(0, local_var);
  2489. }
  2490. compiled_branch.body = branch->body;
  2491. p_match_statement->compiled_pattern_branches.push_back(compiled_branch);
  2492. }
  2493. }
  2494. void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
  2495. IndentLevel current_level = indent_level.back()->get();
  2496. #ifdef DEBUG_ENABLED
  2497. pending_newline = -1; // reset for the new block
  2498. NewLineNode *nl = alloc_node<NewLineNode>();
  2499. nl->line = tokenizer->get_token_line();
  2500. p_block->statements.push_back(nl);
  2501. #endif
  2502. bool is_first_line = true;
  2503. while (true) {
  2504. if (!is_first_line && indent_level.back()->prev() && indent_level.back()->prev()->get().indent == current_level.indent) {
  2505. if (indent_level.back()->prev()->get().is_mixed(current_level)) {
  2506. _set_error("Mixed tabs and spaces in indentation.");
  2507. return;
  2508. }
  2509. // pythonic single-line expression, don't parse future lines
  2510. indent_level.pop_back();
  2511. p_block->end_line = tokenizer->get_token_line();
  2512. return;
  2513. }
  2514. is_first_line = false;
  2515. GDScriptTokenizer::Token token = tokenizer->get_token();
  2516. if (error_set) {
  2517. return;
  2518. }
  2519. if (current_level.indent > indent_level.back()->get().indent) {
  2520. p_block->end_line = tokenizer->get_token_line();
  2521. return; //go back a level
  2522. }
  2523. if (pending_newline != -1) {
  2524. NewLineNode *nl2 = alloc_node<NewLineNode>();
  2525. nl2->line = pending_newline;
  2526. p_block->statements.push_back(nl2);
  2527. pending_newline = -1;
  2528. }
  2529. #ifdef DEBUG_ENABLED
  2530. switch (token) {
  2531. case GDScriptTokenizer::TK_EOF:
  2532. case GDScriptTokenizer::TK_ERROR:
  2533. case GDScriptTokenizer::TK_NEWLINE:
  2534. case GDScriptTokenizer::TK_CF_PASS: {
  2535. // will check later
  2536. } break;
  2537. default: {
  2538. if (p_block->has_return && !current_function->has_unreachable_code) {
  2539. _add_warning(GDScriptWarning::UNREACHABLE_CODE, -1, current_function->name.operator String());
  2540. current_function->has_unreachable_code = true;
  2541. }
  2542. } break;
  2543. }
  2544. #endif // DEBUG_ENABLED
  2545. switch (token) {
  2546. case GDScriptTokenizer::TK_EOF:
  2547. p_block->end_line = tokenizer->get_token_line();
  2548. case GDScriptTokenizer::TK_ERROR: {
  2549. return; //go back
  2550. //end of file!
  2551. } break;
  2552. case GDScriptTokenizer::TK_NEWLINE: {
  2553. int line = tokenizer->get_token_line();
  2554. if (!_parse_newline()) {
  2555. if (!error_set) {
  2556. p_block->end_line = tokenizer->get_token_line();
  2557. pending_newline = p_block->end_line;
  2558. }
  2559. return;
  2560. }
  2561. _mark_line_as_safe(line);
  2562. NewLineNode *nl2 = alloc_node<NewLineNode>();
  2563. nl2->line = line;
  2564. p_block->statements.push_back(nl2);
  2565. } break;
  2566. case GDScriptTokenizer::TK_CF_PASS: {
  2567. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_SEMICOLON && tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE && tokenizer->get_token(1) != GDScriptTokenizer::TK_EOF) {
  2568. _set_error("Expected \";\" or a line break.");
  2569. return;
  2570. }
  2571. _mark_line_as_safe(tokenizer->get_token_line());
  2572. tokenizer->advance();
  2573. if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON) {
  2574. // Ignore semicolon after 'pass'.
  2575. tokenizer->advance();
  2576. }
  2577. } break;
  2578. case GDScriptTokenizer::TK_PR_VAR: {
  2579. // Variable declaration and (eventual) initialization.
  2580. tokenizer->advance();
  2581. int var_line = tokenizer->get_token_line();
  2582. if (!tokenizer->is_token_literal(0, true)) {
  2583. _set_error("Expected an identifier for the local variable name.");
  2584. return;
  2585. }
  2586. StringName n = tokenizer->get_token_literal();
  2587. if (current_function) {
  2588. for (int i = 0; i < current_function->arguments.size(); i++) {
  2589. if (n == current_function->arguments[i]) {
  2590. _set_error("Variable \"" + String(n) + "\" already defined in the scope (at line " + itos(current_function->line) + ").");
  2591. return;
  2592. }
  2593. }
  2594. }
  2595. BlockNode *check_block = p_block;
  2596. while (check_block) {
  2597. if (check_block->variables.has(n)) {
  2598. _set_error("Variable \"" + String(n) + "\" already defined in the scope (at line " + itos(check_block->variables[n]->line) + ").");
  2599. return;
  2600. }
  2601. check_block = check_block->parent_block;
  2602. }
  2603. tokenizer->advance();
  2604. //must know when the local variable is declared
  2605. LocalVarNode *lv = alloc_node<LocalVarNode>();
  2606. lv->name = n;
  2607. lv->line = var_line;
  2608. p_block->statements.push_back(lv);
  2609. Node *assigned = nullptr;
  2610. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  2611. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  2612. lv->datatype = DataType();
  2613. lv->datatype.infer_type = true;
  2614. tokenizer->advance();
  2615. } else if (!_parse_type(lv->datatype)) {
  2616. _set_error("Expected a type for the variable.");
  2617. return;
  2618. }
  2619. }
  2620. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  2621. tokenizer->advance();
  2622. Node *subexpr = _parse_and_reduce_expression(p_block, p_static);
  2623. if (!subexpr) {
  2624. if (_recover_from_completion()) {
  2625. break;
  2626. }
  2627. return;
  2628. }
  2629. lv->assignments++;
  2630. assigned = subexpr;
  2631. } else {
  2632. assigned = _get_default_value_for_type(lv->datatype, var_line);
  2633. }
  2634. //must be added later, to avoid self-referencing.
  2635. p_block->variables.insert(n, lv);
  2636. IdentifierNode *id = alloc_node<IdentifierNode>();
  2637. id->name = n;
  2638. id->declared_block = p_block;
  2639. id->line = var_line;
  2640. OperatorNode *op = alloc_node<OperatorNode>();
  2641. op->op = OperatorNode::OP_ASSIGN;
  2642. op->arguments.push_back(id);
  2643. op->arguments.push_back(assigned);
  2644. op->line = var_line;
  2645. p_block->statements.push_back(op);
  2646. lv->assign_op = op;
  2647. lv->assign = assigned;
  2648. if (!_end_statement()) {
  2649. _set_end_statement_error("var");
  2650. return;
  2651. }
  2652. } break;
  2653. case GDScriptTokenizer::TK_CF_IF: {
  2654. tokenizer->advance();
  2655. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2656. if (!condition) {
  2657. if (_recover_from_completion()) {
  2658. break;
  2659. }
  2660. return;
  2661. }
  2662. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  2663. cf_if->cf_type = ControlFlowNode::CF_IF;
  2664. cf_if->arguments.push_back(condition);
  2665. cf_if->body = alloc_node<BlockNode>();
  2666. cf_if->body->parent_block = p_block;
  2667. cf_if->body->if_condition = condition; //helps code completion
  2668. p_block->sub_blocks.push_back(cf_if->body);
  2669. if (!_enter_indent_block(cf_if->body)) {
  2670. _set_error("Expected an indented block after \"if\".");
  2671. p_block->end_line = tokenizer->get_token_line();
  2672. return;
  2673. }
  2674. current_block = cf_if->body;
  2675. _parse_block(cf_if->body, p_static);
  2676. current_block = p_block;
  2677. if (error_set) {
  2678. return;
  2679. }
  2680. p_block->statements.push_back(cf_if);
  2681. bool all_have_return = cf_if->body->has_return;
  2682. bool have_else = false;
  2683. while (true) {
  2684. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline()) {
  2685. ;
  2686. }
  2687. if (indent_level.back()->get().indent < current_level.indent) { //not at current indent level
  2688. p_block->end_line = tokenizer->get_token_line();
  2689. return;
  2690. }
  2691. if (tokenizer->get_token() == GDScriptTokenizer::TK_CF_ELIF) {
  2692. if (indent_level.back()->get().indent > current_level.indent) {
  2693. _set_error("Invalid indentation.");
  2694. return;
  2695. }
  2696. tokenizer->advance();
  2697. cf_if->body_else = alloc_node<BlockNode>();
  2698. cf_if->body_else->parent_block = p_block;
  2699. p_block->sub_blocks.push_back(cf_if->body_else);
  2700. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  2701. cf_else->cf_type = ControlFlowNode::CF_IF;
  2702. //condition
  2703. Node *condition2 = _parse_and_reduce_expression(p_block, p_static);
  2704. if (!condition2) {
  2705. if (_recover_from_completion()) {
  2706. break;
  2707. }
  2708. return;
  2709. }
  2710. cf_else->arguments.push_back(condition2);
  2711. cf_else->cf_type = ControlFlowNode::CF_IF;
  2712. cf_if->body_else->statements.push_back(cf_else);
  2713. cf_if = cf_else;
  2714. cf_if->body = alloc_node<BlockNode>();
  2715. cf_if->body->parent_block = p_block;
  2716. p_block->sub_blocks.push_back(cf_if->body);
  2717. if (!_enter_indent_block(cf_if->body)) {
  2718. _set_error("Expected an indented block after \"elif\".");
  2719. p_block->end_line = tokenizer->get_token_line();
  2720. return;
  2721. }
  2722. current_block = cf_else->body;
  2723. _parse_block(cf_else->body, p_static);
  2724. current_block = p_block;
  2725. if (error_set) {
  2726. return;
  2727. }
  2728. all_have_return = all_have_return && cf_else->body->has_return;
  2729. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CF_ELSE) {
  2730. if (indent_level.back()->get().indent > current_level.indent) {
  2731. _set_error("Invalid indentation.");
  2732. return;
  2733. }
  2734. tokenizer->advance();
  2735. cf_if->body_else = alloc_node<BlockNode>();
  2736. cf_if->body_else->parent_block = p_block;
  2737. p_block->sub_blocks.push_back(cf_if->body_else);
  2738. if (!_enter_indent_block(cf_if->body_else)) {
  2739. _set_error("Expected an indented block after \"else\".");
  2740. p_block->end_line = tokenizer->get_token_line();
  2741. return;
  2742. }
  2743. current_block = cf_if->body_else;
  2744. _parse_block(cf_if->body_else, p_static);
  2745. current_block = p_block;
  2746. if (error_set) {
  2747. return;
  2748. }
  2749. all_have_return = all_have_return && cf_if->body_else->has_return;
  2750. have_else = true;
  2751. break; //after else, exit
  2752. } else {
  2753. break;
  2754. }
  2755. }
  2756. cf_if->body->has_return = all_have_return;
  2757. // If there's no else block, path out of the if might not have a return
  2758. p_block->has_return = all_have_return && have_else;
  2759. } break;
  2760. case GDScriptTokenizer::TK_CF_WHILE: {
  2761. tokenizer->advance();
  2762. Node *condition2 = _parse_and_reduce_expression(p_block, p_static);
  2763. if (!condition2) {
  2764. if (_recover_from_completion()) {
  2765. break;
  2766. }
  2767. return;
  2768. }
  2769. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  2770. cf_while->cf_type = ControlFlowNode::CF_WHILE;
  2771. cf_while->arguments.push_back(condition2);
  2772. cf_while->body = alloc_node<BlockNode>();
  2773. cf_while->body->parent_block = p_block;
  2774. cf_while->body->can_break = true;
  2775. cf_while->body->can_continue = true;
  2776. p_block->sub_blocks.push_back(cf_while->body);
  2777. if (!_enter_indent_block(cf_while->body)) {
  2778. _set_error("Expected an indented block after \"while\".");
  2779. p_block->end_line = tokenizer->get_token_line();
  2780. return;
  2781. }
  2782. current_block = cf_while->body;
  2783. _parse_block(cf_while->body, p_static);
  2784. current_block = p_block;
  2785. if (error_set) {
  2786. return;
  2787. }
  2788. p_block->statements.push_back(cf_while);
  2789. } break;
  2790. case GDScriptTokenizer::TK_CF_FOR: {
  2791. tokenizer->advance();
  2792. if (!tokenizer->is_token_literal(0, true)) {
  2793. _set_error("Identifier expected after \"for\".");
  2794. }
  2795. IdentifierNode *id = alloc_node<IdentifierNode>();
  2796. id->name = tokenizer->get_token_identifier();
  2797. #ifdef DEBUG_ENABLED
  2798. for (int j = 0; j < current_class->variables.size(); j++) {
  2799. if (current_class->variables[j].identifier == id->name) {
  2800. _add_warning(GDScriptWarning::SHADOWED_VARIABLE, id->line, id->name, itos(current_class->variables[j].line));
  2801. }
  2802. }
  2803. #endif // DEBUG_ENABLED
  2804. BlockNode *check_block = p_block;
  2805. while (check_block) {
  2806. if (check_block->variables.has(id->name)) {
  2807. _set_error("Variable \"" + String(id->name) + "\" already defined in the scope (at line " + itos(check_block->variables[id->name]->line) + ").");
  2808. return;
  2809. }
  2810. check_block = check_block->parent_block;
  2811. }
  2812. tokenizer->advance();
  2813. if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_IN) {
  2814. _set_error("\"in\" expected after identifier.");
  2815. return;
  2816. }
  2817. tokenizer->advance();
  2818. Node *container = _parse_and_reduce_expression(p_block, p_static);
  2819. if (!container) {
  2820. if (_recover_from_completion()) {
  2821. break;
  2822. }
  2823. return;
  2824. }
  2825. DataType iter_type;
  2826. if (container->type == Node::TYPE_OPERATOR) {
  2827. OperatorNode *op = static_cast<OperatorNode *>(container);
  2828. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && static_cast<BuiltInFunctionNode *>(op->arguments[0])->function == GDScriptFunctions::GEN_RANGE) {
  2829. //iterating a range, so see if range() can be optimized without allocating memory, by replacing it by vectors (which can work as iterable too!)
  2830. Vector<Node *> args;
  2831. Vector<double> constants;
  2832. bool constant = true;
  2833. for (int i = 1; i < op->arguments.size(); i++) {
  2834. args.push_back(op->arguments[i]);
  2835. if (op->arguments[i]->type == Node::TYPE_CONSTANT) {
  2836. ConstantNode *c = static_cast<ConstantNode *>(op->arguments[i]);
  2837. if (c->value.get_type() == Variant::REAL || c->value.get_type() == Variant::INT) {
  2838. constants.push_back(c->value);
  2839. } else {
  2840. constant = false;
  2841. }
  2842. } else {
  2843. constant = false;
  2844. }
  2845. }
  2846. if (args.size() > 0 && args.size() < 4) {
  2847. if (constant) {
  2848. ConstantNode *cn = alloc_node<ConstantNode>();
  2849. switch (args.size()) {
  2850. case 1:
  2851. cn->value = (int)constants[0];
  2852. break;
  2853. case 2:
  2854. cn->value = Vector2(constants[0], constants[1]);
  2855. break;
  2856. case 3:
  2857. cn->value = Vector3(constants[0], constants[1], constants[2]);
  2858. break;
  2859. }
  2860. cn->datatype = _type_from_variant(cn->value);
  2861. container = cn;
  2862. } else {
  2863. OperatorNode *on = alloc_node<OperatorNode>();
  2864. on->op = OperatorNode::OP_CALL;
  2865. TypeNode *tn = alloc_node<TypeNode>();
  2866. on->arguments.push_back(tn);
  2867. switch (args.size()) {
  2868. case 1:
  2869. tn->vtype = Variant::INT;
  2870. break;
  2871. case 2:
  2872. tn->vtype = Variant::VECTOR2;
  2873. break;
  2874. case 3:
  2875. tn->vtype = Variant::VECTOR3;
  2876. break;
  2877. }
  2878. for (int i = 0; i < args.size(); i++) {
  2879. on->arguments.push_back(args[i]);
  2880. }
  2881. container = on;
  2882. }
  2883. }
  2884. iter_type.has_type = true;
  2885. iter_type.kind = DataType::BUILTIN;
  2886. iter_type.builtin_type = Variant::INT;
  2887. }
  2888. }
  2889. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  2890. cf_for->cf_type = ControlFlowNode::CF_FOR;
  2891. cf_for->arguments.push_back(id);
  2892. cf_for->arguments.push_back(container);
  2893. cf_for->body = alloc_node<BlockNode>();
  2894. cf_for->body->parent_block = p_block;
  2895. cf_for->body->can_break = true;
  2896. cf_for->body->can_continue = true;
  2897. p_block->sub_blocks.push_back(cf_for->body);
  2898. if (!_enter_indent_block(cf_for->body)) {
  2899. _set_error("Expected indented block after \"for\".");
  2900. p_block->end_line = tokenizer->get_token_line();
  2901. return;
  2902. }
  2903. current_block = cf_for->body;
  2904. // this is for checking variable for redefining
  2905. // inside this _parse_block
  2906. LocalVarNode *lv = alloc_node<LocalVarNode>();
  2907. lv->name = id->name;
  2908. lv->line = id->line;
  2909. lv->assignments++;
  2910. id->declared_block = cf_for->body;
  2911. lv->set_datatype(iter_type);
  2912. id->set_datatype(iter_type);
  2913. cf_for->body->variables.insert(id->name, lv);
  2914. _parse_block(cf_for->body, p_static);
  2915. current_block = p_block;
  2916. if (error_set) {
  2917. return;
  2918. }
  2919. p_block->statements.push_back(cf_for);
  2920. } break;
  2921. case GDScriptTokenizer::TK_CF_CONTINUE: {
  2922. BlockNode *upper_block = p_block;
  2923. bool is_continue_valid = false;
  2924. while (upper_block) {
  2925. if (upper_block->can_continue) {
  2926. is_continue_valid = true;
  2927. break;
  2928. }
  2929. upper_block = upper_block->parent_block;
  2930. }
  2931. if (!is_continue_valid) {
  2932. _set_error("Unexpected keyword \"continue\" outside a loop.");
  2933. return;
  2934. }
  2935. _mark_line_as_safe(tokenizer->get_token_line());
  2936. tokenizer->advance();
  2937. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  2938. cf_continue->cf_type = ControlFlowNode::CF_CONTINUE;
  2939. p_block->statements.push_back(cf_continue);
  2940. if (!_end_statement()) {
  2941. _set_end_statement_error("continue");
  2942. return;
  2943. }
  2944. } break;
  2945. case GDScriptTokenizer::TK_CF_BREAK: {
  2946. BlockNode *upper_block = p_block;
  2947. bool is_break_valid = false;
  2948. while (upper_block) {
  2949. if (upper_block->can_break) {
  2950. is_break_valid = true;
  2951. break;
  2952. }
  2953. upper_block = upper_block->parent_block;
  2954. }
  2955. if (!is_break_valid) {
  2956. _set_error("Unexpected keyword \"break\" outside a loop.");
  2957. return;
  2958. }
  2959. _mark_line_as_safe(tokenizer->get_token_line());
  2960. tokenizer->advance();
  2961. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  2962. cf_break->cf_type = ControlFlowNode::CF_BREAK;
  2963. p_block->statements.push_back(cf_break);
  2964. if (!_end_statement()) {
  2965. _set_end_statement_error("break");
  2966. return;
  2967. }
  2968. } break;
  2969. case GDScriptTokenizer::TK_CF_RETURN: {
  2970. tokenizer->advance();
  2971. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  2972. cf_return->cf_type = ControlFlowNode::CF_RETURN;
  2973. cf_return->line = tokenizer->get_token_line(-1);
  2974. if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON || tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE || tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  2975. //expect end of statement
  2976. p_block->statements.push_back(cf_return);
  2977. if (!_end_statement()) {
  2978. return;
  2979. }
  2980. } else {
  2981. //expect expression
  2982. Node *retexpr = _parse_and_reduce_expression(p_block, p_static);
  2983. if (!retexpr) {
  2984. if (_recover_from_completion()) {
  2985. break;
  2986. }
  2987. return;
  2988. }
  2989. cf_return->arguments.push_back(retexpr);
  2990. p_block->statements.push_back(cf_return);
  2991. if (!_end_statement()) {
  2992. _set_end_statement_error("return");
  2993. return;
  2994. }
  2995. }
  2996. p_block->has_return = true;
  2997. } break;
  2998. case GDScriptTokenizer::TK_CF_MATCH: {
  2999. tokenizer->advance();
  3000. MatchNode *match_node = alloc_node<MatchNode>();
  3001. Node *val_to_match = _parse_and_reduce_expression(p_block, p_static);
  3002. if (!val_to_match) {
  3003. if (_recover_from_completion()) {
  3004. break;
  3005. }
  3006. return;
  3007. }
  3008. match_node->val_to_match = val_to_match;
  3009. if (!_enter_indent_block()) {
  3010. _set_error("Expected indented pattern matching block after \"match\".");
  3011. return;
  3012. }
  3013. BlockNode *compiled_branches = alloc_node<BlockNode>();
  3014. compiled_branches->parent_block = p_block;
  3015. compiled_branches->parent_class = p_block->parent_class;
  3016. compiled_branches->can_continue = true;
  3017. p_block->sub_blocks.push_back(compiled_branches);
  3018. _parse_pattern_block(compiled_branches, match_node->branches, p_static);
  3019. if (error_set) {
  3020. return;
  3021. }
  3022. ControlFlowNode *match_cf_node = alloc_node<ControlFlowNode>();
  3023. match_cf_node->cf_type = ControlFlowNode::CF_MATCH;
  3024. match_cf_node->match = match_node;
  3025. match_cf_node->body = compiled_branches;
  3026. p_block->has_return = p_block->has_return || compiled_branches->has_return;
  3027. p_block->statements.push_back(match_cf_node);
  3028. _end_statement();
  3029. } break;
  3030. case GDScriptTokenizer::TK_PR_ASSERT: {
  3031. tokenizer->advance();
  3032. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  3033. _set_error("Expected '(' after assert");
  3034. return;
  3035. }
  3036. int assert_line = tokenizer->get_token_line();
  3037. tokenizer->advance();
  3038. Vector<Node *> args;
  3039. const bool result = _parse_arguments(p_block, args, p_static);
  3040. if (!result) {
  3041. return;
  3042. }
  3043. if (args.empty() || args.size() > 2) {
  3044. _set_error("Wrong number of arguments, expected 1 or 2", assert_line);
  3045. return;
  3046. }
  3047. AssertNode *an = alloc_node<AssertNode>();
  3048. an->condition = _reduce_expression(args[0], p_static);
  3049. an->line = assert_line;
  3050. if (args.size() == 2) {
  3051. an->message = _reduce_expression(args[1], p_static);
  3052. } else {
  3053. ConstantNode *message_node = alloc_node<ConstantNode>();
  3054. message_node->value = String();
  3055. an->message = message_node;
  3056. }
  3057. p_block->statements.push_back(an);
  3058. if (!_end_statement()) {
  3059. _set_end_statement_error("assert");
  3060. return;
  3061. }
  3062. } break;
  3063. case GDScriptTokenizer::TK_PR_BREAKPOINT: {
  3064. tokenizer->advance();
  3065. BreakpointNode *bn = alloc_node<BreakpointNode>();
  3066. p_block->statements.push_back(bn);
  3067. if (!_end_statement()) {
  3068. _set_end_statement_error("breakpoint");
  3069. return;
  3070. }
  3071. } break;
  3072. default: {
  3073. Node *expression = _parse_and_reduce_expression(p_block, p_static, false, true);
  3074. if (!expression) {
  3075. if (_recover_from_completion()) {
  3076. break;
  3077. }
  3078. return;
  3079. }
  3080. p_block->statements.push_back(expression);
  3081. if (!_end_statement()) {
  3082. // Attempt to guess a better error message if the user "retypes" a variable
  3083. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON && tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  3084. _set_error("Unexpected ':=', use '=' instead. Expected end of statement after expression.");
  3085. } else {
  3086. _set_error(vformat("Expected end of statement after expression, got %s instead.", tokenizer->get_token_name(tokenizer->get_token())));
  3087. }
  3088. return;
  3089. }
  3090. } break;
  3091. }
  3092. }
  3093. }
  3094. bool GDScriptParser::_parse_newline() {
  3095. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_EOF && tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE) {
  3096. IndentLevel current_level = indent_level.back()->get();
  3097. int indent = tokenizer->get_token_line_indent();
  3098. int tabs = tokenizer->get_token_line_tab_indent();
  3099. IndentLevel new_level(indent, tabs);
  3100. if (new_level.is_mixed(current_level)) {
  3101. _set_error("Mixed tabs and spaces in indentation.");
  3102. return false;
  3103. }
  3104. if (indent > current_level.indent) {
  3105. _set_error("Unexpected indentation.");
  3106. return false;
  3107. }
  3108. if (indent < current_level.indent) {
  3109. while (indent < current_level.indent) {
  3110. //exit block
  3111. if (indent_level.size() == 1) {
  3112. _set_error("Invalid indentation. Bug?");
  3113. return false;
  3114. }
  3115. indent_level.pop_back();
  3116. if (indent_level.back()->get().indent < indent) {
  3117. _set_error("Unindent does not match any outer indentation level.");
  3118. return false;
  3119. }
  3120. if (indent_level.back()->get().is_mixed(current_level)) {
  3121. _set_error("Mixed tabs and spaces in indentation.");
  3122. return false;
  3123. }
  3124. current_level = indent_level.back()->get();
  3125. }
  3126. tokenizer->advance();
  3127. return false;
  3128. }
  3129. }
  3130. tokenizer->advance();
  3131. return true;
  3132. }
  3133. void GDScriptParser::_parse_extends(ClassNode *p_class) {
  3134. if (p_class->extends_used) {
  3135. _set_error("\"extends\" can only be present once per script.");
  3136. return;
  3137. }
  3138. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  3139. _set_error("\"extends\" must be used before anything else.");
  3140. return;
  3141. }
  3142. p_class->extends_used = true;
  3143. tokenizer->advance();
  3144. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type() == Variant::OBJECT) {
  3145. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  3146. tokenizer->advance();
  3147. return;
  3148. }
  3149. // see if inheritance happens from a file
  3150. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) {
  3151. Variant constant = tokenizer->get_token_constant();
  3152. if (constant.get_type() != Variant::STRING) {
  3153. _set_error("\"extends\" constant must be a string.");
  3154. return;
  3155. }
  3156. p_class->extends_file = constant;
  3157. tokenizer->advance();
  3158. // Add parent script as a dependency
  3159. String parent = constant;
  3160. if (parent.is_rel_path()) {
  3161. parent = base_path.plus_file(parent).simplify_path();
  3162. }
  3163. dependencies.push_back(parent);
  3164. if (tokenizer->get_token() != GDScriptTokenizer::TK_PERIOD) {
  3165. return;
  3166. } else {
  3167. tokenizer->advance();
  3168. }
  3169. }
  3170. while (true) {
  3171. switch (tokenizer->get_token()) {
  3172. case GDScriptTokenizer::TK_IDENTIFIER: {
  3173. StringName identifier = tokenizer->get_token_identifier();
  3174. p_class->extends_class.push_back(identifier);
  3175. } break;
  3176. case GDScriptTokenizer::TK_CURSOR:
  3177. case GDScriptTokenizer::TK_PERIOD:
  3178. break;
  3179. default: {
  3180. _set_error("Invalid \"extends\" syntax, expected string constant (path) and/or identifier (parent class).");
  3181. return;
  3182. }
  3183. }
  3184. tokenizer->advance(1);
  3185. switch (tokenizer->get_token()) {
  3186. case GDScriptTokenizer::TK_IDENTIFIER:
  3187. case GDScriptTokenizer::TK_PERIOD:
  3188. continue;
  3189. case GDScriptTokenizer::TK_CURSOR:
  3190. completion_type = COMPLETION_EXTENDS;
  3191. completion_class = current_class;
  3192. completion_function = current_function;
  3193. completion_line = tokenizer->get_token_line();
  3194. completion_block = current_block;
  3195. completion_ident_is_call = false;
  3196. completion_found = true;
  3197. return;
  3198. default:
  3199. return;
  3200. }
  3201. }
  3202. }
  3203. void GDScriptParser::_parse_class(ClassNode *p_class) {
  3204. IndentLevel current_level = indent_level.back()->get();
  3205. while (true) {
  3206. GDScriptTokenizer::Token token = tokenizer->get_token();
  3207. if (error_set) {
  3208. return;
  3209. }
  3210. if (current_level.indent > indent_level.back()->get().indent) {
  3211. p_class->end_line = tokenizer->get_token_line();
  3212. return; //go back a level
  3213. }
  3214. switch (token) {
  3215. case GDScriptTokenizer::TK_CURSOR: {
  3216. tokenizer->advance();
  3217. } break;
  3218. case GDScriptTokenizer::TK_EOF:
  3219. p_class->end_line = tokenizer->get_token_line();
  3220. case GDScriptTokenizer::TK_ERROR: {
  3221. return; //go back
  3222. //end of file!
  3223. } break;
  3224. case GDScriptTokenizer::TK_NEWLINE: {
  3225. if (!_parse_newline()) {
  3226. if (!error_set) {
  3227. p_class->end_line = tokenizer->get_token_line();
  3228. }
  3229. return;
  3230. }
  3231. } break;
  3232. case GDScriptTokenizer::TK_PR_EXTENDS: {
  3233. _mark_line_as_safe(tokenizer->get_token_line());
  3234. _parse_extends(p_class);
  3235. if (error_set) {
  3236. return;
  3237. }
  3238. if (!_end_statement()) {
  3239. _set_end_statement_error("extends");
  3240. return;
  3241. }
  3242. } break;
  3243. case GDScriptTokenizer::TK_PR_CLASS_NAME: {
  3244. _mark_line_as_safe(tokenizer->get_token_line());
  3245. if (p_class->owner) {
  3246. _set_error("\"class_name\" is only valid for the main class namespace.");
  3247. return;
  3248. }
  3249. if (self_path.begins_with("res://") && self_path.find("::") != -1) {
  3250. _set_error("\"class_name\" isn't allowed in built-in scripts.");
  3251. return;
  3252. }
  3253. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) {
  3254. _set_error("\"class_name\" syntax: \"class_name <UniqueName>\"");
  3255. return;
  3256. }
  3257. if (p_class->classname_used) {
  3258. _set_error("\"class_name\" can only be present once per script.");
  3259. return;
  3260. }
  3261. p_class->classname_used = true;
  3262. p_class->name = tokenizer->get_token_identifier(1);
  3263. if (self_path != String() && ScriptServer::is_global_class(p_class->name) && ScriptServer::get_global_class_path(p_class->name) != self_path) {
  3264. _set_error("Unique global class \"" + p_class->name + "\" already exists at path: " + ScriptServer::get_global_class_path(p_class->name));
  3265. return;
  3266. }
  3267. if (ClassDB::class_exists(p_class->name) || ClassDB::class_exists("_" + p_class->name.operator String())) {
  3268. _set_error("The class \"" + p_class->name + "\" shadows a native class.");
  3269. return;
  3270. }
  3271. if (p_class->classname_used && ProjectSettings::get_singleton()->has_setting("autoload/" + p_class->name)) {
  3272. const String autoload_path = ProjectSettings::get_singleton()->get_setting("autoload/" + p_class->name);
  3273. if (autoload_path.begins_with("*")) {
  3274. // It's a singleton, and not just a regular AutoLoad script.
  3275. _set_error("The class \"" + p_class->name + "\" conflicts with the AutoLoad singleton of the same name, and is therefore redundant. Remove the class_name declaration to fix this error.");
  3276. }
  3277. return;
  3278. }
  3279. tokenizer->advance(2);
  3280. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3281. tokenizer->advance();
  3282. if ((tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING)) {
  3283. #ifdef TOOLS_ENABLED
  3284. if (Engine::get_singleton()->is_editor_hint()) {
  3285. Variant constant = tokenizer->get_token_constant();
  3286. String icon_path = constant.operator String();
  3287. String abs_icon_path = icon_path.is_rel_path() ? self_path.get_base_dir().plus_file(icon_path).simplify_path() : icon_path;
  3288. if (!FileAccess::exists(abs_icon_path)) {
  3289. _set_error("No class icon found at: " + abs_icon_path);
  3290. return;
  3291. }
  3292. p_class->icon_path = icon_path;
  3293. }
  3294. #endif
  3295. tokenizer->advance();
  3296. } else {
  3297. _set_error("The optional parameter after \"class_name\" must be a string constant file path to an icon.");
  3298. return;
  3299. }
  3300. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) {
  3301. _set_error("The class icon must be separated by a comma.");
  3302. return;
  3303. }
  3304. } break;
  3305. case GDScriptTokenizer::TK_PR_TOOL: {
  3306. if (p_class->tool) {
  3307. _set_error("The \"tool\" keyword can only be present once per script.");
  3308. return;
  3309. }
  3310. p_class->tool = true;
  3311. tokenizer->advance();
  3312. } break;
  3313. case GDScriptTokenizer::TK_PR_CLASS: {
  3314. //class inside class :D
  3315. StringName name;
  3316. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) {
  3317. _set_error("\"class\" syntax: \"class <Name>:\" or \"class <Name> extends <BaseClass>:\"");
  3318. return;
  3319. }
  3320. name = tokenizer->get_token_identifier(1);
  3321. tokenizer->advance(2);
  3322. // Check if name is shadowing something else
  3323. if (ClassDB::class_exists(name) || ClassDB::class_exists("_" + name.operator String())) {
  3324. _set_error("The class \"" + String(name) + "\" shadows a native class.");
  3325. return;
  3326. }
  3327. if (ScriptServer::is_global_class(name)) {
  3328. _set_error("Can't override name of the unique global class \"" + name + "\". It already exists at: " + ScriptServer::get_global_class_path(p_class->name));
  3329. return;
  3330. }
  3331. ClassNode *outer_class = p_class;
  3332. while (outer_class) {
  3333. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  3334. if (outer_class->subclasses[i]->name == name) {
  3335. _set_error("Another class named \"" + String(name) + "\" already exists in this scope (at line " + itos(outer_class->subclasses[i]->line) + ").");
  3336. return;
  3337. }
  3338. }
  3339. if (outer_class->constant_expressions.has(name)) {
  3340. _set_error("A constant named \"" + String(name) + "\" already exists in the outer class scope (at line" + itos(outer_class->constant_expressions[name].expression->line) + ").");
  3341. return;
  3342. }
  3343. for (int i = 0; i < outer_class->variables.size(); i++) {
  3344. if (outer_class->variables[i].identifier == name) {
  3345. _set_error("A variable named \"" + String(name) + "\" already exists in the outer class scope (at line " + itos(outer_class->variables[i].line) + ").");
  3346. return;
  3347. }
  3348. }
  3349. outer_class = outer_class->owner;
  3350. }
  3351. ClassNode *newclass = alloc_node<ClassNode>();
  3352. newclass->initializer = alloc_node<BlockNode>();
  3353. newclass->initializer->parent_class = newclass;
  3354. newclass->ready = alloc_node<BlockNode>();
  3355. newclass->ready->parent_class = newclass;
  3356. newclass->name = name;
  3357. newclass->owner = p_class;
  3358. p_class->subclasses.push_back(newclass);
  3359. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_EXTENDS) {
  3360. _parse_extends(newclass);
  3361. if (error_set) {
  3362. return;
  3363. }
  3364. }
  3365. if (!_enter_inner_class_indent_block()) {
  3366. if (!error_set) {
  3367. _set_error("Indented block or \"pass\" expected.");
  3368. }
  3369. return;
  3370. }
  3371. if (tokenizer->get_token() != GDScriptTokenizer::TK_CF_PASS) {
  3372. current_class = newclass;
  3373. _parse_class(newclass);
  3374. current_class = p_class;
  3375. }
  3376. } break;
  3377. /* this is for functions....
  3378. case GDScriptTokenizer::TK_CF_PASS: {
  3379. tokenizer->advance(1);
  3380. } break;
  3381. */
  3382. case GDScriptTokenizer::TK_PR_STATIC: {
  3383. tokenizer->advance();
  3384. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3385. _set_error("Expected \"func\".");
  3386. return;
  3387. }
  3388. FALLTHROUGH;
  3389. }
  3390. case GDScriptTokenizer::TK_PR_FUNCTION: {
  3391. bool _static = false;
  3392. pending_newline = -1;
  3393. if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_STATIC) {
  3394. _static = true;
  3395. }
  3396. tokenizer->advance();
  3397. StringName name;
  3398. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC, name)) {
  3399. }
  3400. if (name == StringName()) {
  3401. _set_error("Expected an identifier after \"func\" (syntax: \"func <identifier>([arguments]):\").");
  3402. return;
  3403. }
  3404. for (int i = 0; i < p_class->functions.size(); i++) {
  3405. if (p_class->functions[i]->name == name) {
  3406. _set_error("The function \"" + String(name) + "\" already exists in this class (at line " + itos(p_class->functions[i]->line) + ").");
  3407. }
  3408. }
  3409. for (int i = 0; i < p_class->static_functions.size(); i++) {
  3410. if (p_class->static_functions[i]->name == name) {
  3411. _set_error("The function \"" + String(name) + "\" already exists in this class (at line " + itos(p_class->static_functions[i]->line) + ").");
  3412. }
  3413. }
  3414. #ifdef DEBUG_ENABLED
  3415. if (p_class->constant_expressions.has(name)) {
  3416. _add_warning(GDScriptWarning::FUNCTION_CONFLICTS_CONSTANT, -1, name);
  3417. }
  3418. for (int i = 0; i < p_class->variables.size(); i++) {
  3419. if (p_class->variables[i].identifier == name) {
  3420. _add_warning(GDScriptWarning::FUNCTION_CONFLICTS_VARIABLE, -1, name);
  3421. }
  3422. }
  3423. for (int i = 0; i < p_class->subclasses.size(); i++) {
  3424. if (p_class->subclasses[i]->name == name) {
  3425. _add_warning(GDScriptWarning::FUNCTION_CONFLICTS_CONSTANT, -1, name);
  3426. }
  3427. }
  3428. #endif // DEBUG_ENABLED
  3429. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  3430. _set_error("Expected \"(\" after the identifier (syntax: \"func <identifier>([arguments]):\" ).");
  3431. return;
  3432. }
  3433. tokenizer->advance();
  3434. Vector<StringName> arguments;
  3435. Vector<DataType> argument_types;
  3436. Vector<Node *> default_values;
  3437. #ifdef DEBUG_ENABLED
  3438. Vector<int> arguments_usage;
  3439. #endif // DEBUG_ENABLED
  3440. int fnline = tokenizer->get_token_line();
  3441. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3442. //has arguments
  3443. bool defaulting = false;
  3444. while (true) {
  3445. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  3446. tokenizer->advance();
  3447. continue;
  3448. }
  3449. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_VAR) {
  3450. tokenizer->advance(); //var before the identifier is allowed
  3451. }
  3452. if (!tokenizer->is_token_literal(0, true)) {
  3453. _set_error("Expected an identifier for an argument.");
  3454. return;
  3455. }
  3456. StringName argname = tokenizer->get_token_identifier();
  3457. for (int i = 0; i < arguments.size(); i++) {
  3458. if (arguments[i] == argname) {
  3459. _set_error("The argument name \"" + String(argname) + "\" is defined multiple times.");
  3460. return;
  3461. }
  3462. }
  3463. arguments.push_back(argname);
  3464. #ifdef DEBUG_ENABLED
  3465. arguments_usage.push_back(0);
  3466. #endif // DEBUG_ENABLED
  3467. tokenizer->advance();
  3468. DataType argtype;
  3469. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  3470. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  3471. argtype.infer_type = true;
  3472. tokenizer->advance();
  3473. } else if (!_parse_type(argtype)) {
  3474. _set_error("Expected a type for an argument.");
  3475. return;
  3476. }
  3477. }
  3478. argument_types.push_back(argtype);
  3479. if (defaulting && tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
  3480. _set_error("Default parameter expected.");
  3481. return;
  3482. }
  3483. //tokenizer->advance();
  3484. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  3485. defaulting = true;
  3486. tokenizer->advance(1);
  3487. Node *defval = _parse_and_reduce_expression(p_class, _static);
  3488. if (!defval || error_set) {
  3489. return;
  3490. }
  3491. OperatorNode *on = alloc_node<OperatorNode>();
  3492. on->op = OperatorNode::OP_ASSIGN;
  3493. on->line = fnline;
  3494. IdentifierNode *in = alloc_node<IdentifierNode>();
  3495. in->name = argname;
  3496. in->line = fnline;
  3497. on->arguments.push_back(in);
  3498. on->arguments.push_back(defval);
  3499. /* no ..
  3500. if (defval->type!=Node::TYPE_CONSTANT) {
  3501. _set_error("default argument must be constant");
  3502. }
  3503. */
  3504. default_values.push_back(on);
  3505. }
  3506. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  3507. tokenizer->advance();
  3508. }
  3509. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3510. tokenizer->advance();
  3511. continue;
  3512. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3513. _set_error("Expected \",\" or \")\".");
  3514. return;
  3515. }
  3516. break;
  3517. }
  3518. }
  3519. tokenizer->advance();
  3520. BlockNode *block = alloc_node<BlockNode>();
  3521. block->parent_class = p_class;
  3522. FunctionNode *function = alloc_node<FunctionNode>();
  3523. function->name = name;
  3524. function->arguments = arguments;
  3525. function->argument_types = argument_types;
  3526. function->default_values = default_values;
  3527. function->_static = _static;
  3528. function->line = fnline;
  3529. #ifdef DEBUG_ENABLED
  3530. function->arguments_usage = arguments_usage;
  3531. #endif // DEBUG_ENABLED
  3532. function->rpc_mode = rpc_mode;
  3533. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  3534. if (name == "_init") {
  3535. if (_static) {
  3536. _set_error("The constructor cannot be static.");
  3537. return;
  3538. }
  3539. if (p_class->extends_used) {
  3540. OperatorNode *cparent = alloc_node<OperatorNode>();
  3541. cparent->op = OperatorNode::OP_PARENT_CALL;
  3542. block->statements.push_back(cparent);
  3543. IdentifierNode *id = alloc_node<IdentifierNode>();
  3544. id->name = "_init";
  3545. cparent->arguments.push_back(id);
  3546. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  3547. tokenizer->advance();
  3548. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  3549. _set_error("Expected \"(\" for parent constructor arguments.");
  3550. return;
  3551. }
  3552. tokenizer->advance();
  3553. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3554. //has arguments
  3555. parenthesis++;
  3556. while (true) {
  3557. current_function = function;
  3558. Node *arg = _parse_and_reduce_expression(p_class, _static);
  3559. if (!arg) {
  3560. return;
  3561. }
  3562. current_function = nullptr;
  3563. cparent->arguments.push_back(arg);
  3564. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3565. tokenizer->advance();
  3566. continue;
  3567. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3568. _set_error("Expected \",\" or \")\".");
  3569. return;
  3570. }
  3571. break;
  3572. }
  3573. parenthesis--;
  3574. }
  3575. tokenizer->advance();
  3576. }
  3577. } else {
  3578. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  3579. _set_error("Parent constructor call found for a class without inheritance.");
  3580. return;
  3581. }
  3582. }
  3583. }
  3584. DataType return_type;
  3585. if (tokenizer->get_token() == GDScriptTokenizer::TK_FORWARD_ARROW) {
  3586. if (!_parse_type(return_type, true)) {
  3587. _set_error("Expected a return type for the function.");
  3588. return;
  3589. }
  3590. }
  3591. if (!_enter_indent_block(block)) {
  3592. _set_error(vformat("Indented block expected after declaration of \"%s\" function.", function->name));
  3593. return;
  3594. }
  3595. function->return_type = return_type;
  3596. if (_static) {
  3597. p_class->static_functions.push_back(function);
  3598. } else {
  3599. p_class->functions.push_back(function);
  3600. }
  3601. current_function = function;
  3602. function->body = block;
  3603. current_block = block;
  3604. _parse_block(block, _static);
  3605. current_block = nullptr;
  3606. //arguments
  3607. } break;
  3608. case GDScriptTokenizer::TK_PR_SIGNAL: {
  3609. _mark_line_as_safe(tokenizer->get_token_line());
  3610. tokenizer->advance();
  3611. if (!tokenizer->is_token_literal()) {
  3612. _set_error("Expected an identifier after \"signal\".");
  3613. return;
  3614. }
  3615. ClassNode::Signal sig;
  3616. sig.name = tokenizer->get_token_identifier();
  3617. sig.emissions = 0;
  3618. sig.line = tokenizer->get_token_line();
  3619. for (int i = 0; i < current_class->_signals.size(); i++) {
  3620. if (current_class->_signals[i].name == sig.name) {
  3621. _set_error("The signal \"" + sig.name + "\" already exists in this class (at line: " + itos(current_class->_signals[i].line) + ").");
  3622. return;
  3623. }
  3624. }
  3625. tokenizer->advance();
  3626. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  3627. tokenizer->advance();
  3628. while (true) {
  3629. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  3630. tokenizer->advance();
  3631. continue;
  3632. }
  3633. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3634. tokenizer->advance();
  3635. break;
  3636. }
  3637. if (!tokenizer->is_token_literal(0, true)) {
  3638. _set_error("Expected an identifier in a \"signal\" argument.");
  3639. return;
  3640. }
  3641. sig.arguments.push_back(tokenizer->get_token_identifier());
  3642. tokenizer->advance();
  3643. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  3644. tokenizer->advance();
  3645. }
  3646. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3647. tokenizer->advance();
  3648. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3649. _set_error("Expected \",\" or \")\" after a \"signal\" parameter identifier.");
  3650. return;
  3651. }
  3652. }
  3653. }
  3654. p_class->_signals.push_back(sig);
  3655. if (!_end_statement()) {
  3656. _set_end_statement_error("signal");
  3657. return;
  3658. }
  3659. } break;
  3660. case GDScriptTokenizer::TK_PR_EXPORT: {
  3661. tokenizer->advance();
  3662. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  3663. #define _ADVANCE_AND_CONSUME_NEWLINES \
  3664. do { \
  3665. tokenizer->advance(); \
  3666. } while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE)
  3667. _ADVANCE_AND_CONSUME_NEWLINES;
  3668. parenthesis++;
  3669. String hint_prefix = "";
  3670. bool is_arrayed = false;
  3671. while (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE &&
  3672. tokenizer->get_token_type() == Variant::ARRAY &&
  3673. tokenizer->get_token(1) == GDScriptTokenizer::TK_COMMA) {
  3674. tokenizer->advance(); // Array
  3675. tokenizer->advance(); // Comma
  3676. if (is_arrayed) {
  3677. hint_prefix += itos(Variant::ARRAY) + ":";
  3678. } else {
  3679. is_arrayed = true;
  3680. }
  3681. }
  3682. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE) {
  3683. Variant::Type type = tokenizer->get_token_type();
  3684. if (type == Variant::NIL) {
  3685. _set_error("Can't export null type.");
  3686. return;
  3687. }
  3688. if (type == Variant::OBJECT) {
  3689. _set_error("Can't export raw object type.");
  3690. return;
  3691. }
  3692. current_export.type = type;
  3693. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3694. _ADVANCE_AND_CONSUME_NEWLINES;
  3695. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3696. // hint expected next!
  3697. _ADVANCE_AND_CONSUME_NEWLINES;
  3698. switch (type) {
  3699. case Variant::INT: {
  3700. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  3701. _ADVANCE_AND_CONSUME_NEWLINES;
  3702. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3703. WARN_DEPRECATED_MSG("Exporting bit flags hint requires string constants.");
  3704. break;
  3705. }
  3706. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3707. _set_error("Expected \",\" in the bit flags hint.");
  3708. return;
  3709. }
  3710. current_export.hint = PROPERTY_HINT_FLAGS;
  3711. _ADVANCE_AND_CONSUME_NEWLINES;
  3712. bool first = true;
  3713. while (true) {
  3714. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3715. current_export = PropertyInfo();
  3716. _set_error("Expected a string constant in the named bit flags hint.");
  3717. return;
  3718. }
  3719. String c = tokenizer->get_token_constant();
  3720. if (!first) {
  3721. current_export.hint_string += ",";
  3722. } else {
  3723. first = false;
  3724. }
  3725. current_export.hint_string += c.xml_escape();
  3726. _ADVANCE_AND_CONSUME_NEWLINES;
  3727. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3728. break;
  3729. }
  3730. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3731. current_export = PropertyInfo();
  3732. _set_error("Expected \")\" or \",\" in the named bit flags hint.");
  3733. return;
  3734. }
  3735. _ADVANCE_AND_CONSUME_NEWLINES;
  3736. }
  3737. break;
  3738. }
  3739. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "LAYERS_2D_RENDER") {
  3740. _ADVANCE_AND_CONSUME_NEWLINES;
  3741. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3742. _set_error("Expected \")\" in the layers 2D render hint.");
  3743. return;
  3744. }
  3745. current_export.hint = PROPERTY_HINT_LAYERS_2D_RENDER;
  3746. break;
  3747. }
  3748. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "LAYERS_2D_PHYSICS") {
  3749. _ADVANCE_AND_CONSUME_NEWLINES;
  3750. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3751. _set_error("Expected \")\" in the layers 2D physics hint.");
  3752. return;
  3753. }
  3754. current_export.hint = PROPERTY_HINT_LAYERS_2D_PHYSICS;
  3755. break;
  3756. }
  3757. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "LAYERS_2D_NAVIGATION") {
  3758. _ADVANCE_AND_CONSUME_NEWLINES;
  3759. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3760. _set_error("Expected \")\" in the layers 2D navigation hint.");
  3761. return;
  3762. }
  3763. current_export.hint = PROPERTY_HINT_LAYERS_2D_NAVIGATION;
  3764. break;
  3765. }
  3766. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "LAYERS_3D_RENDER") {
  3767. _ADVANCE_AND_CONSUME_NEWLINES;
  3768. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3769. _set_error("Expected \")\" in the layers 3D render hint.");
  3770. return;
  3771. }
  3772. current_export.hint = PROPERTY_HINT_LAYERS_3D_RENDER;
  3773. break;
  3774. }
  3775. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "LAYERS_3D_PHYSICS") {
  3776. _ADVANCE_AND_CONSUME_NEWLINES;
  3777. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3778. _set_error("Expected \")\" in the layers 3D physics hint.");
  3779. return;
  3780. }
  3781. current_export.hint = PROPERTY_HINT_LAYERS_3D_PHYSICS;
  3782. break;
  3783. }
  3784. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "LAYERS_3D_NAVIGATION") {
  3785. _ADVANCE_AND_CONSUME_NEWLINES;
  3786. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3787. _set_error("Expected \")\" in the layers 3D navigation hint.");
  3788. return;
  3789. }
  3790. current_export.hint = PROPERTY_HINT_LAYERS_3D_NAVIGATION;
  3791. break;
  3792. }
  3793. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3794. //enumeration
  3795. current_export.hint = PROPERTY_HINT_ENUM;
  3796. bool first = true;
  3797. while (true) {
  3798. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3799. current_export = PropertyInfo();
  3800. _set_error("Expected a string constant in the enumeration hint.");
  3801. return;
  3802. }
  3803. String c = tokenizer->get_token_constant();
  3804. if (!first) {
  3805. current_export.hint_string += ",";
  3806. } else {
  3807. first = false;
  3808. }
  3809. current_export.hint_string += c.xml_escape();
  3810. _ADVANCE_AND_CONSUME_NEWLINES;
  3811. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3812. break;
  3813. }
  3814. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3815. current_export = PropertyInfo();
  3816. _set_error("Expected \")\" or \",\" in the enumeration hint.");
  3817. return;
  3818. }
  3819. _ADVANCE_AND_CONSUME_NEWLINES;
  3820. }
  3821. break;
  3822. }
  3823. FALLTHROUGH;
  3824. }
  3825. case Variant::REAL: {
  3826. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
  3827. current_export.hint = PROPERTY_HINT_EXP_EASING;
  3828. _ADVANCE_AND_CONSUME_NEWLINES;
  3829. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3830. _set_error("Expected \")\" in the hint.");
  3831. return;
  3832. }
  3833. break;
  3834. }
  3835. // range
  3836. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EXP") {
  3837. current_export.hint = PROPERTY_HINT_EXP_RANGE;
  3838. _ADVANCE_AND_CONSUME_NEWLINES;
  3839. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3840. break;
  3841. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3842. _set_error("Expected \")\" or \",\" in the exponential range hint.");
  3843. return;
  3844. }
  3845. _ADVANCE_AND_CONSUME_NEWLINES;
  3846. } else {
  3847. current_export.hint = PROPERTY_HINT_RANGE;
  3848. }
  3849. float sign = 1.0;
  3850. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3851. sign = -1;
  3852. _ADVANCE_AND_CONSUME_NEWLINES;
  3853. }
  3854. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3855. current_export = PropertyInfo();
  3856. _set_error("Expected a range in the numeric hint.");
  3857. return;
  3858. }
  3859. current_export.hint_string = rtos(sign * double(tokenizer->get_token_constant()));
  3860. _ADVANCE_AND_CONSUME_NEWLINES;
  3861. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3862. current_export.hint_string = "0," + current_export.hint_string;
  3863. break;
  3864. }
  3865. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3866. current_export = PropertyInfo();
  3867. _set_error("Expected \",\" or \")\" in the numeric range hint.");
  3868. return;
  3869. }
  3870. _ADVANCE_AND_CONSUME_NEWLINES;
  3871. sign = 1.0;
  3872. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3873. sign = -1;
  3874. _ADVANCE_AND_CONSUME_NEWLINES;
  3875. }
  3876. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3877. current_export = PropertyInfo();
  3878. _set_error("Expected a number as upper bound in the numeric range hint.");
  3879. return;
  3880. }
  3881. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  3882. _ADVANCE_AND_CONSUME_NEWLINES;
  3883. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3884. break;
  3885. }
  3886. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3887. current_export = PropertyInfo();
  3888. _set_error("Expected \",\" or \")\" in the numeric range hint.");
  3889. return;
  3890. }
  3891. _ADVANCE_AND_CONSUME_NEWLINES;
  3892. sign = 1.0;
  3893. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3894. sign = -1;
  3895. _ADVANCE_AND_CONSUME_NEWLINES;
  3896. }
  3897. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3898. current_export = PropertyInfo();
  3899. _set_error("Expected a number as step in the numeric range hint.");
  3900. return;
  3901. }
  3902. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  3903. _ADVANCE_AND_CONSUME_NEWLINES;
  3904. } break;
  3905. case Variant::STRING: {
  3906. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3907. //enumeration
  3908. current_export.hint = PROPERTY_HINT_ENUM;
  3909. bool first = true;
  3910. while (true) {
  3911. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3912. current_export = PropertyInfo();
  3913. _set_error("Expected a string constant in the enumeration hint.");
  3914. return;
  3915. }
  3916. String c = tokenizer->get_token_constant();
  3917. if (!first) {
  3918. current_export.hint_string += ",";
  3919. } else {
  3920. first = false;
  3921. }
  3922. current_export.hint_string += c.xml_escape();
  3923. _ADVANCE_AND_CONSUME_NEWLINES;
  3924. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3925. break;
  3926. }
  3927. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3928. current_export = PropertyInfo();
  3929. _set_error("Expected \")\" or \",\" in the enumeration hint.");
  3930. return;
  3931. }
  3932. _ADVANCE_AND_CONSUME_NEWLINES;
  3933. }
  3934. break;
  3935. }
  3936. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "DIR") {
  3937. _ADVANCE_AND_CONSUME_NEWLINES;
  3938. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3939. current_export.hint = PROPERTY_HINT_DIR;
  3940. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3941. _ADVANCE_AND_CONSUME_NEWLINES;
  3942. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) {
  3943. _set_error("Expected \"GLOBAL\" after comma in the directory hint.");
  3944. return;
  3945. }
  3946. if (!p_class->tool) {
  3947. _set_error("Global filesystem hints may only be used in tool scripts.");
  3948. return;
  3949. }
  3950. current_export.hint = PROPERTY_HINT_GLOBAL_DIR;
  3951. _ADVANCE_AND_CONSUME_NEWLINES;
  3952. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3953. _set_error("Expected \")\" in the hint.");
  3954. return;
  3955. }
  3956. } else {
  3957. _set_error("Expected \")\" or \",\" in the hint.");
  3958. return;
  3959. }
  3960. break;
  3961. }
  3962. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FILE") {
  3963. current_export.hint = PROPERTY_HINT_FILE;
  3964. _ADVANCE_AND_CONSUME_NEWLINES;
  3965. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3966. _ADVANCE_AND_CONSUME_NEWLINES;
  3967. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "GLOBAL") {
  3968. if (!p_class->tool) {
  3969. _set_error("Global filesystem hints may only be used in tool scripts.");
  3970. return;
  3971. }
  3972. current_export.hint = PROPERTY_HINT_GLOBAL_FILE;
  3973. _ADVANCE_AND_CONSUME_NEWLINES;
  3974. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3975. break;
  3976. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3977. _ADVANCE_AND_CONSUME_NEWLINES;
  3978. } else {
  3979. _set_error("Expected \")\" or \",\" in the hint.");
  3980. return;
  3981. }
  3982. }
  3983. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3984. if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE) {
  3985. _set_error("Expected string constant with filter.");
  3986. } else {
  3987. _set_error("Expected \"GLOBAL\" or string constant with filter.");
  3988. }
  3989. return;
  3990. }
  3991. current_export.hint_string = tokenizer->get_token_constant();
  3992. _ADVANCE_AND_CONSUME_NEWLINES;
  3993. }
  3994. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3995. _set_error("Expected \")\" in the hint.");
  3996. return;
  3997. }
  3998. break;
  3999. }
  4000. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "MULTILINE") {
  4001. current_export.hint = PROPERTY_HINT_MULTILINE_TEXT;
  4002. _ADVANCE_AND_CONSUME_NEWLINES;
  4003. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  4004. _set_error("Expected \")\" in the hint.");
  4005. return;
  4006. }
  4007. break;
  4008. }
  4009. } break;
  4010. case Variant::COLOR: {
  4011. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER) {
  4012. current_export = PropertyInfo();
  4013. _set_error("Color type hint expects RGB or RGBA as hints.");
  4014. return;
  4015. }
  4016. String identifier = tokenizer->get_token_identifier();
  4017. if (identifier == "RGB") {
  4018. current_export.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  4019. } else if (identifier == "RGBA") {
  4020. //none
  4021. } else {
  4022. current_export = PropertyInfo();
  4023. _set_error("Color type hint expects RGB or RGBA as hints.");
  4024. return;
  4025. }
  4026. _ADVANCE_AND_CONSUME_NEWLINES;
  4027. } break;
  4028. default: {
  4029. current_export = PropertyInfo();
  4030. _set_error("Type \"" + Variant::get_type_name(type) + "\" can't take hints.");
  4031. return;
  4032. } break;
  4033. }
  4034. }
  4035. } else {
  4036. parenthesis++;
  4037. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  4038. if (!subexpr) {
  4039. if (_recover_from_completion()) {
  4040. break;
  4041. }
  4042. return;
  4043. }
  4044. parenthesis--;
  4045. if (subexpr->type != Node::TYPE_CONSTANT) {
  4046. current_export = PropertyInfo();
  4047. _set_error("Expected a constant expression.");
  4048. return;
  4049. }
  4050. Variant constant = static_cast<ConstantNode *>(subexpr)->value;
  4051. if (constant.get_type() == Variant::OBJECT) {
  4052. GDScriptNativeClass *native_class = Object::cast_to<GDScriptNativeClass>(constant);
  4053. if (native_class && ClassDB::is_parent_class(native_class->get_name(), "Resource")) {
  4054. current_export.type = Variant::OBJECT;
  4055. current_export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  4056. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  4057. current_export.hint_string = native_class->get_name();
  4058. current_export.class_name = native_class->get_name();
  4059. } else {
  4060. current_export = PropertyInfo();
  4061. _set_error("The export hint isn't a resource type.");
  4062. }
  4063. } else if (constant.get_type() == Variant::DICTIONARY) {
  4064. // Enumeration
  4065. bool is_flags = false;
  4066. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  4067. _ADVANCE_AND_CONSUME_NEWLINES;
  4068. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  4069. is_flags = true;
  4070. _ADVANCE_AND_CONSUME_NEWLINES;
  4071. } else {
  4072. current_export = PropertyInfo();
  4073. _set_error("Expected \"FLAGS\" after comma.");
  4074. }
  4075. }
  4076. current_export.type = Variant::INT;
  4077. current_export.hint = is_flags ? PROPERTY_HINT_FLAGS : PROPERTY_HINT_ENUM;
  4078. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  4079. Dictionary enum_values = constant;
  4080. List<Variant> keys;
  4081. enum_values.get_key_list(&keys);
  4082. bool first = true;
  4083. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  4084. if (enum_values[E->get()].get_type() == Variant::INT) {
  4085. if (!first) {
  4086. current_export.hint_string += ",";
  4087. } else {
  4088. first = false;
  4089. }
  4090. current_export.hint_string += E->get().operator String().capitalize().xml_escape();
  4091. if (!is_flags) {
  4092. current_export.hint_string += ":";
  4093. current_export.hint_string += enum_values[E->get()].operator String().xml_escape();
  4094. }
  4095. }
  4096. }
  4097. } else {
  4098. current_export = PropertyInfo();
  4099. _set_error("Expected type for export.");
  4100. return;
  4101. }
  4102. }
  4103. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  4104. current_export = PropertyInfo();
  4105. _set_error("Expected \")\" or \",\" after the export hint.");
  4106. return;
  4107. }
  4108. tokenizer->advance();
  4109. parenthesis--;
  4110. if (is_arrayed) {
  4111. hint_prefix += itos(current_export.type);
  4112. if (current_export.hint) {
  4113. hint_prefix += "/" + itos(current_export.hint);
  4114. }
  4115. current_export.hint_string = hint_prefix + ":" + current_export.hint_string;
  4116. current_export.hint = PROPERTY_HINT_TYPE_STRING;
  4117. current_export.type = Variant::ARRAY;
  4118. }
  4119. #undef _ADVANCE_AND_CONSUME_NEWLINES
  4120. }
  4121. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_ONREADY && tokenizer->get_token() != GDScriptTokenizer::TK_PR_REMOTE && tokenizer->get_token() != GDScriptTokenizer::TK_PR_MASTER && tokenizer->get_token() != GDScriptTokenizer::TK_PR_PUPPET && tokenizer->get_token() != GDScriptTokenizer::TK_PR_SYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_REMOTESYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_MASTERSYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_PUPPETSYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_SLAVE) {
  4122. current_export = PropertyInfo();
  4123. _set_error("Expected \"var\", \"onready\", \"remote\", \"master\", \"puppet\", \"sync\", \"remotesync\", \"mastersync\", \"puppetsync\".");
  4124. return;
  4125. }
  4126. continue;
  4127. } break;
  4128. case GDScriptTokenizer::TK_PR_ONREADY: {
  4129. //may be fallthrough from export, ignore if so
  4130. tokenizer->advance();
  4131. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  4132. _set_error("Expected \"var\".");
  4133. return;
  4134. }
  4135. continue;
  4136. } break;
  4137. case GDScriptTokenizer::TK_PR_REMOTE: {
  4138. //may be fallthrough from export, ignore if so
  4139. tokenizer->advance();
  4140. if (current_export.type) {
  4141. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  4142. _set_error("Expected \"var\".");
  4143. return;
  4144. }
  4145. } else {
  4146. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  4147. _set_error("Expected \"var\" or \"func\".");
  4148. return;
  4149. }
  4150. }
  4151. rpc_mode = MultiplayerAPI::RPC_MODE_REMOTE;
  4152. continue;
  4153. } break;
  4154. case GDScriptTokenizer::TK_PR_MASTER: {
  4155. //may be fallthrough from export, ignore if so
  4156. tokenizer->advance();
  4157. if (current_export.type) {
  4158. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  4159. _set_error("Expected \"var\".");
  4160. return;
  4161. }
  4162. } else {
  4163. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  4164. _set_error("Expected \"var\" or \"func\".");
  4165. return;
  4166. }
  4167. }
  4168. rpc_mode = MultiplayerAPI::RPC_MODE_MASTER;
  4169. continue;
  4170. } break;
  4171. case GDScriptTokenizer::TK_PR_SLAVE:
  4172. #ifdef DEBUG_ENABLED
  4173. _add_warning(GDScriptWarning::DEPRECATED_KEYWORD, tokenizer->get_token_line(), "slave", "puppet");
  4174. #endif
  4175. FALLTHROUGH;
  4176. case GDScriptTokenizer::TK_PR_PUPPET: {
  4177. //may be fallthrough from export, ignore if so
  4178. tokenizer->advance();
  4179. if (current_export.type) {
  4180. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  4181. _set_error("Expected \"var\".");
  4182. return;
  4183. }
  4184. } else {
  4185. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  4186. _set_error("Expected \"var\" or \"func\".");
  4187. return;
  4188. }
  4189. }
  4190. rpc_mode = MultiplayerAPI::RPC_MODE_PUPPET;
  4191. continue;
  4192. } break;
  4193. case GDScriptTokenizer::TK_PR_REMOTESYNC:
  4194. case GDScriptTokenizer::TK_PR_SYNC: {
  4195. //may be fallthrough from export, ignore if so
  4196. tokenizer->advance();
  4197. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  4198. if (current_export.type) {
  4199. _set_error("Expected \"var\".");
  4200. } else {
  4201. _set_error("Expected \"var\" or \"func\".");
  4202. }
  4203. return;
  4204. }
  4205. rpc_mode = MultiplayerAPI::RPC_MODE_REMOTESYNC;
  4206. continue;
  4207. } break;
  4208. case GDScriptTokenizer::TK_PR_MASTERSYNC: {
  4209. //may be fallthrough from export, ignore if so
  4210. tokenizer->advance();
  4211. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  4212. if (current_export.type) {
  4213. _set_error("Expected \"var\".");
  4214. } else {
  4215. _set_error("Expected \"var\" or \"func\".");
  4216. }
  4217. return;
  4218. }
  4219. rpc_mode = MultiplayerAPI::RPC_MODE_MASTERSYNC;
  4220. continue;
  4221. } break;
  4222. case GDScriptTokenizer::TK_PR_PUPPETSYNC: {
  4223. //may be fallthrough from export, ignore if so
  4224. tokenizer->advance();
  4225. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  4226. if (current_export.type) {
  4227. _set_error("Expected \"var\".");
  4228. } else {
  4229. _set_error("Expected \"var\" or \"func\".");
  4230. }
  4231. return;
  4232. }
  4233. rpc_mode = MultiplayerAPI::RPC_MODE_PUPPETSYNC;
  4234. continue;
  4235. } break;
  4236. case GDScriptTokenizer::TK_PR_VAR: {
  4237. // variable declaration and (eventual) initialization
  4238. ClassNode::Member member;
  4239. bool autoexport = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_EXPORT;
  4240. if (current_export.type != Variant::NIL) {
  4241. member._export = current_export;
  4242. current_export = PropertyInfo();
  4243. }
  4244. bool onready = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_ONREADY;
  4245. tokenizer->advance();
  4246. if (!tokenizer->is_token_literal(0, true)) {
  4247. _set_error("Expected an identifier for the member variable name.");
  4248. return;
  4249. }
  4250. member.identifier = tokenizer->get_token_literal();
  4251. member.expression = nullptr;
  4252. member._export.name = member.identifier;
  4253. member.line = tokenizer->get_token_line();
  4254. member.usages = 0;
  4255. member.rpc_mode = rpc_mode;
  4256. // GH-57496
  4257. if (ClassDB::class_exists(member.identifier) || ClassDB::class_exists("_" + member.identifier.operator String())) {
  4258. _set_error("Variable \"" + String(member.identifier) + "\" shadows a native class.");
  4259. return;
  4260. }
  4261. if (current_class->constant_expressions.has(member.identifier)) {
  4262. _set_error("A constant named \"" + String(member.identifier) + "\" already exists in this class (at line: " +
  4263. itos(current_class->constant_expressions[member.identifier].expression->line) + ").");
  4264. return;
  4265. }
  4266. for (int i = 0; i < current_class->variables.size(); i++) {
  4267. if (current_class->variables[i].identifier == member.identifier) {
  4268. _set_error("Variable \"" + String(member.identifier) + "\" already exists in this class (at line: " +
  4269. itos(current_class->variables[i].line) + ").");
  4270. return;
  4271. }
  4272. }
  4273. for (int i = 0; i < current_class->subclasses.size(); i++) {
  4274. if (current_class->subclasses[i]->name == member.identifier) {
  4275. _set_error("A class named \"" + String(member.identifier) + "\" already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ").");
  4276. return;
  4277. }
  4278. }
  4279. #ifdef DEBUG_ENABLED
  4280. for (int i = 0; i < current_class->functions.size(); i++) {
  4281. if (current_class->functions[i]->name == member.identifier) {
  4282. _add_warning(GDScriptWarning::VARIABLE_CONFLICTS_FUNCTION, member.line, member.identifier);
  4283. break;
  4284. }
  4285. }
  4286. for (int i = 0; i < current_class->static_functions.size(); i++) {
  4287. if (current_class->static_functions[i]->name == member.identifier) {
  4288. _add_warning(GDScriptWarning::VARIABLE_CONFLICTS_FUNCTION, member.line, member.identifier);
  4289. break;
  4290. }
  4291. }
  4292. #endif // DEBUG_ENABLED
  4293. tokenizer->advance();
  4294. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  4295. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  4296. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  4297. member.data_type = DataType();
  4298. member.data_type.infer_type = true;
  4299. tokenizer->advance();
  4300. } else if (!_parse_type(member.data_type)) {
  4301. _set_error("Expected a type for the class variable.");
  4302. return;
  4303. }
  4304. }
  4305. if (autoexport && member.data_type.has_type) {
  4306. if (member.data_type.kind == DataType::BUILTIN) {
  4307. member._export.type = member.data_type.builtin_type;
  4308. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  4309. } else if (member.data_type.kind == DataType::NATIVE) {
  4310. if (ClassDB::is_parent_class(member.data_type.native_type, "Resource")) {
  4311. member._export.type = Variant::OBJECT;
  4312. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  4313. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  4314. member._export.hint_string = member.data_type.native_type;
  4315. member._export.class_name = member.data_type.native_type;
  4316. } else {
  4317. _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
  4318. return;
  4319. }
  4320. } else {
  4321. _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
  4322. return;
  4323. }
  4324. }
  4325. #ifdef TOOLS_ENABLED
  4326. Variant::CallError ce;
  4327. member.default_value = Variant::construct(member._export.type, nullptr, 0, ce);
  4328. #endif
  4329. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  4330. #ifdef DEBUG_ENABLED
  4331. int line = tokenizer->get_token_line();
  4332. #endif
  4333. tokenizer->advance();
  4334. Node *subexpr = _parse_and_reduce_expression(p_class, false, autoexport || member._export.type != Variant::NIL);
  4335. if (!subexpr) {
  4336. if (_recover_from_completion()) {
  4337. break;
  4338. }
  4339. return;
  4340. }
  4341. //discourage common error
  4342. if (!onready && subexpr->type == Node::TYPE_OPERATOR) {
  4343. OperatorNode *op = static_cast<OperatorNode *>(subexpr);
  4344. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_SELF && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  4345. IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]);
  4346. if (id->name == "get_node") {
  4347. _set_error("Use \"onready var " + String(member.identifier) + " = get_node(...)\" instead.");
  4348. return;
  4349. }
  4350. }
  4351. }
  4352. member.expression = subexpr;
  4353. if (autoexport && !member.data_type.has_type) {
  4354. if (subexpr->type != Node::TYPE_CONSTANT) {
  4355. _set_error("Type-less export needs a constant expression assigned to infer type.");
  4356. return;
  4357. }
  4358. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  4359. if (cn->value.get_type() == Variant::NIL) {
  4360. _set_error("Can't accept a null constant expression for inferring export type.");
  4361. return;
  4362. }
  4363. if (!_reduce_export_var_type(cn->value, member.line)) {
  4364. return;
  4365. }
  4366. member._export.type = cn->value.get_type();
  4367. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  4368. if (cn->value.get_type() == Variant::OBJECT) {
  4369. Object *obj = cn->value;
  4370. Resource *res = Object::cast_to<Resource>(obj);
  4371. if (res == nullptr) {
  4372. _set_error("The exported constant isn't a type or resource.");
  4373. return;
  4374. }
  4375. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  4376. member._export.hint_string = res->get_class();
  4377. }
  4378. }
  4379. #ifdef TOOLS_ENABLED
  4380. // Warn if the default value set is not the same as the export type, since it won't be coerced and
  4381. // may create wrong expectations.
  4382. if (subexpr->type == Node::TYPE_CONSTANT && (member._export.type != Variant::NIL || member.data_type.has_type)) {
  4383. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  4384. if (cn->value.get_type() != Variant::NIL) {
  4385. if (member._export.type != Variant::NIL && cn->value.get_type() != member._export.type) {
  4386. if (!Variant::can_convert(cn->value.get_type(), member._export.type)) {
  4387. _set_error("Can't convert the provided value to the export type.");
  4388. return;
  4389. } else if (!member.data_type.has_type) {
  4390. _add_warning(GDScriptWarning::EXPORT_HINT_TYPE_MISTMATCH, member.line, Variant::get_type_name(cn->value.get_type()), Variant::get_type_name(member._export.type));
  4391. }
  4392. }
  4393. }
  4394. member.default_value = cn->value;
  4395. }
  4396. #endif
  4397. IdentifierNode *id = alloc_node<IdentifierNode>();
  4398. id->name = member.identifier;
  4399. id->datatype = member.data_type;
  4400. OperatorNode *op = alloc_node<OperatorNode>();
  4401. op->op = OperatorNode::OP_INIT_ASSIGN;
  4402. op->arguments.push_back(id);
  4403. op->arguments.push_back(subexpr);
  4404. #ifdef DEBUG_ENABLED
  4405. NewLineNode *nl2 = alloc_node<NewLineNode>();
  4406. nl2->line = line;
  4407. if (onready) {
  4408. p_class->ready->statements.push_back(nl2);
  4409. } else {
  4410. p_class->initializer->statements.push_back(nl2);
  4411. }
  4412. #endif
  4413. if (onready) {
  4414. p_class->ready->statements.push_back(op);
  4415. } else {
  4416. p_class->initializer->statements.push_back(op);
  4417. }
  4418. member.initial_assignment = op;
  4419. } else {
  4420. if (autoexport && !member.data_type.has_type) {
  4421. _set_error("Type-less export needs a constant expression assigned to infer type.");
  4422. return;
  4423. }
  4424. Node *expr;
  4425. if (member.data_type.has_type) {
  4426. expr = _get_default_value_for_type(member.data_type);
  4427. } else {
  4428. DataType exported_type;
  4429. exported_type.has_type = true;
  4430. exported_type.kind = DataType::BUILTIN;
  4431. exported_type.builtin_type = member._export.type;
  4432. expr = _get_default_value_for_type(exported_type);
  4433. }
  4434. IdentifierNode *id = alloc_node<IdentifierNode>();
  4435. id->name = member.identifier;
  4436. id->datatype = member.data_type;
  4437. OperatorNode *op = alloc_node<OperatorNode>();
  4438. op->op = OperatorNode::OP_INIT_ASSIGN;
  4439. op->arguments.push_back(id);
  4440. op->arguments.push_back(expr);
  4441. p_class->initializer->statements.push_back(op);
  4442. member.initial_assignment = op;
  4443. }
  4444. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_SETGET) {
  4445. tokenizer->advance();
  4446. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  4447. //just comma means using only getter
  4448. if (!tokenizer->is_token_literal()) {
  4449. _set_error("Expected an identifier for the setter function after \"setget\".");
  4450. }
  4451. member.setter = tokenizer->get_token_literal();
  4452. tokenizer->advance();
  4453. }
  4454. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  4455. //there is a getter
  4456. tokenizer->advance();
  4457. if (!tokenizer->is_token_literal()) {
  4458. _set_error("Expected an identifier for the getter function after \",\".");
  4459. }
  4460. member.getter = tokenizer->get_token_literal();
  4461. tokenizer->advance();
  4462. }
  4463. }
  4464. p_class->variables.push_back(member);
  4465. if (!_end_statement()) {
  4466. _set_end_statement_error("var");
  4467. return;
  4468. }
  4469. } break;
  4470. case GDScriptTokenizer::TK_PR_CONST: {
  4471. // constant declaration and initialization
  4472. ClassNode::Constant constant;
  4473. tokenizer->advance();
  4474. if (!tokenizer->is_token_literal(0, true)) {
  4475. _set_error("Expected an identifier for the constant.");
  4476. return;
  4477. }
  4478. StringName const_id = tokenizer->get_token_literal();
  4479. int line = tokenizer->get_token_line();
  4480. // GH-57496
  4481. if (ClassDB::class_exists(const_id) || ClassDB::class_exists("_" + const_id.operator String())) {
  4482. _set_error("Constant \"" + String(const_id) + "\" shadows a native class.");
  4483. return;
  4484. }
  4485. if (current_class->constant_expressions.has(const_id)) {
  4486. _set_error("Constant \"" + String(const_id) + "\" already exists in this class (at line " +
  4487. itos(current_class->constant_expressions[const_id].expression->line) + ").");
  4488. return;
  4489. }
  4490. for (int i = 0; i < current_class->variables.size(); i++) {
  4491. if (current_class->variables[i].identifier == const_id) {
  4492. _set_error("A variable named \"" + String(const_id) + "\" already exists in this class (at line " +
  4493. itos(current_class->variables[i].line) + ").");
  4494. return;
  4495. }
  4496. }
  4497. for (int i = 0; i < current_class->subclasses.size(); i++) {
  4498. if (current_class->subclasses[i]->name == const_id) {
  4499. _set_error("A class named \"" + String(const_id) + "\" already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ").");
  4500. return;
  4501. }
  4502. }
  4503. tokenizer->advance();
  4504. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  4505. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  4506. constant.type = DataType();
  4507. constant.type.infer_type = true;
  4508. tokenizer->advance();
  4509. } else if (!_parse_type(constant.type)) {
  4510. _set_error("Expected a type for the class constant.");
  4511. return;
  4512. }
  4513. }
  4514. if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
  4515. _set_error("Constants must be assigned immediately.");
  4516. return;
  4517. }
  4518. tokenizer->advance();
  4519. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  4520. if (!subexpr) {
  4521. if (_recover_from_completion()) {
  4522. break;
  4523. }
  4524. return;
  4525. }
  4526. if (subexpr->type != Node::TYPE_CONSTANT) {
  4527. _set_error("Expected a constant expression.", line);
  4528. return;
  4529. }
  4530. subexpr->line = line;
  4531. constant.expression = subexpr;
  4532. p_class->constant_expressions.insert(const_id, constant);
  4533. if (!_end_statement()) {
  4534. _set_end_statement_error("const");
  4535. return;
  4536. }
  4537. } break;
  4538. case GDScriptTokenizer::TK_PR_ENUM: {
  4539. //multiple constant declarations..
  4540. int last_assign = -1; // Incremented by 1 right before the assignment.
  4541. String enum_name;
  4542. Dictionary enum_dict;
  4543. int enum_start_line = tokenizer->get_token_line();
  4544. tokenizer->advance();
  4545. if (tokenizer->is_token_literal(0, true)) {
  4546. enum_name = tokenizer->get_token_literal();
  4547. // GH-57496
  4548. if (ClassDB::class_exists(enum_name) || ClassDB::class_exists("_" + enum_name)) {
  4549. _set_error("Enumeration \"" + enum_name + "\" shadows a native class.");
  4550. return;
  4551. }
  4552. if (current_class->constant_expressions.has(enum_name)) {
  4553. _set_error("A constant named \"" + String(enum_name) + "\" already exists in this class (at line " +
  4554. itos(current_class->constant_expressions[enum_name].expression->line) + ").");
  4555. return;
  4556. }
  4557. for (int i = 0; i < current_class->variables.size(); i++) {
  4558. if (current_class->variables[i].identifier == enum_name) {
  4559. _set_error("A variable named \"" + String(enum_name) + "\" already exists in this class (at line " +
  4560. itos(current_class->variables[i].line) + ").");
  4561. return;
  4562. }
  4563. }
  4564. for (int i = 0; i < current_class->subclasses.size(); i++) {
  4565. if (current_class->subclasses[i]->name == enum_name) {
  4566. _set_error("A class named \"" + String(enum_name) + "\" already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ").");
  4567. return;
  4568. }
  4569. }
  4570. tokenizer->advance();
  4571. }
  4572. if (tokenizer->get_token() != GDScriptTokenizer::TK_CURLY_BRACKET_OPEN) {
  4573. _set_error("Expected \"{\" in the enum declaration.");
  4574. return;
  4575. }
  4576. tokenizer->advance();
  4577. while (true) {
  4578. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  4579. tokenizer->advance(); // Ignore newlines
  4580. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  4581. tokenizer->advance();
  4582. break; // End of enum
  4583. } else if (!tokenizer->is_token_literal(0, true)) {
  4584. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  4585. _set_error("Unexpected end of file.");
  4586. } else {
  4587. _set_error(String("Unexpected ") + GDScriptTokenizer::get_token_name(tokenizer->get_token()) + ", expected an identifier.");
  4588. }
  4589. return;
  4590. } else { // tokenizer->is_token_literal(0, true)
  4591. StringName const_id = tokenizer->get_token_literal();
  4592. tokenizer->advance();
  4593. ConstantNode *enum_value_expr;
  4594. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  4595. tokenizer->advance();
  4596. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  4597. if (!subexpr) {
  4598. if (_recover_from_completion()) {
  4599. break;
  4600. }
  4601. return;
  4602. }
  4603. if (subexpr->type != Node::TYPE_CONSTANT) {
  4604. _set_error("Expected a constant expression.");
  4605. return;
  4606. }
  4607. enum_value_expr = static_cast<ConstantNode *>(subexpr);
  4608. if (enum_value_expr->value.get_type() != Variant::INT) {
  4609. _set_error("Expected an integer value for \"enum\".");
  4610. return;
  4611. }
  4612. last_assign = enum_value_expr->value;
  4613. } else {
  4614. last_assign = last_assign + 1;
  4615. enum_value_expr = alloc_node<ConstantNode>();
  4616. enum_value_expr->value = last_assign;
  4617. enum_value_expr->datatype = _type_from_variant(enum_value_expr->value);
  4618. }
  4619. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  4620. tokenizer->advance();
  4621. } else if (tokenizer->is_token_literal(0, true)) {
  4622. _set_error("Unexpected identifier.");
  4623. return;
  4624. }
  4625. if (enum_name != "") {
  4626. enum_dict[const_id] = enum_value_expr->value;
  4627. } else {
  4628. // GH-57496
  4629. if (ClassDB::class_exists(const_id) || ClassDB::class_exists("_" + const_id.operator String())) {
  4630. _set_error("Constant \"" + String(const_id) + "\" shadows a native class.");
  4631. return;
  4632. }
  4633. if (current_class->constant_expressions.has(const_id)) {
  4634. _set_error("A constant named \"" + String(const_id) + "\" already exists in this class (at line " +
  4635. itos(current_class->constant_expressions[const_id].expression->line) + ").");
  4636. return;
  4637. }
  4638. for (int i = 0; i < current_class->variables.size(); i++) {
  4639. if (current_class->variables[i].identifier == const_id) {
  4640. _set_error("A variable named \"" + String(const_id) + "\" already exists in this class (at line " +
  4641. itos(current_class->variables[i].line) + ").");
  4642. return;
  4643. }
  4644. }
  4645. for (int i = 0; i < current_class->subclasses.size(); i++) {
  4646. if (current_class->subclasses[i]->name == const_id) {
  4647. _set_error("A class named \"" + String(const_id) + "\" already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ").");
  4648. return;
  4649. }
  4650. }
  4651. ClassNode::Constant constant;
  4652. constant.type.has_type = true;
  4653. constant.type.kind = DataType::BUILTIN;
  4654. constant.type.builtin_type = Variant::INT;
  4655. constant.expression = enum_value_expr;
  4656. p_class->constant_expressions.insert(const_id, constant);
  4657. }
  4658. }
  4659. }
  4660. if (enum_name != "") {
  4661. ClassNode::Constant enum_constant;
  4662. ConstantNode *cn = alloc_node<ConstantNode>();
  4663. cn->value = enum_dict;
  4664. cn->datatype = _type_from_variant(cn->value);
  4665. cn->line = enum_start_line;
  4666. enum_constant.expression = cn;
  4667. enum_constant.type = cn->datatype;
  4668. p_class->constant_expressions.insert(enum_name, enum_constant);
  4669. }
  4670. if (!_end_statement()) {
  4671. _set_end_statement_error("enum");
  4672. return;
  4673. }
  4674. } break;
  4675. case GDScriptTokenizer::TK_CONSTANT: {
  4676. if (tokenizer->get_token_constant().get_type() == Variant::STRING) {
  4677. tokenizer->advance();
  4678. // Ignore
  4679. } else {
  4680. _set_error(String() + "Unexpected constant of type: " + Variant::get_type_name(tokenizer->get_token_constant().get_type()));
  4681. return;
  4682. }
  4683. } break;
  4684. case GDScriptTokenizer::TK_CF_PASS: {
  4685. tokenizer->advance();
  4686. } break;
  4687. default: {
  4688. if (token == GDScriptTokenizer::TK_IDENTIFIER) {
  4689. completion_type = COMPLETION_IDENTIFIER;
  4690. completion_class = current_class;
  4691. completion_function = current_function;
  4692. completion_line = tokenizer->get_token_line();
  4693. completion_block = current_block;
  4694. completion_ident_is_call = false;
  4695. completion_found = true;
  4696. }
  4697. _set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
  4698. return;
  4699. } break;
  4700. }
  4701. }
  4702. }
  4703. void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive) {
  4704. if (p_class->base_type.has_type) {
  4705. // Already determined
  4706. } else if (p_class->extends_used) {
  4707. //do inheritance
  4708. String path = p_class->extends_file;
  4709. Ref<GDScript> script;
  4710. StringName native;
  4711. ClassNode *base_class = nullptr;
  4712. if (path != "") {
  4713. //path (and optionally subclasses)
  4714. if (path.is_rel_path()) {
  4715. String base = base_path;
  4716. if (base == "" || base.is_rel_path()) {
  4717. _set_error("Couldn't resolve relative path for the parent class: " + path, p_class->line);
  4718. return;
  4719. }
  4720. path = base.plus_file(path).simplify_path();
  4721. }
  4722. script = ResourceLoader::load(path);
  4723. if (script.is_null()) {
  4724. _set_error("Couldn't load the base class: " + path, p_class->line);
  4725. return;
  4726. }
  4727. if (!script->is_valid()) {
  4728. _set_error("Script isn't fully loaded (cyclic preload?): " + path, p_class->line);
  4729. return;
  4730. }
  4731. if (p_class->extends_class.size()) {
  4732. for (int i = 0; i < p_class->extends_class.size(); i++) {
  4733. String sub = p_class->extends_class[i];
  4734. if (script->get_subclasses().has(sub)) {
  4735. Ref<Script> subclass = script->get_subclasses()[sub]; //avoid reference from disappearing
  4736. script = subclass;
  4737. } else {
  4738. _set_error("Couldn't find the subclass: " + sub, p_class->line);
  4739. return;
  4740. }
  4741. }
  4742. }
  4743. } else {
  4744. if (p_class->extends_class.size() == 0) {
  4745. _set_error("Parser bug: undecidable inheritance.", p_class->line);
  4746. ERR_FAIL();
  4747. }
  4748. //look around for the subclasses
  4749. int extend_iter = 1;
  4750. String base = p_class->extends_class[0];
  4751. ClassNode *p = p_class->owner;
  4752. Ref<GDScript> base_script;
  4753. if (ScriptServer::is_global_class(base)) {
  4754. base_script = ResourceLoader::load(ScriptServer::get_global_class_path(base));
  4755. if (!base_script.is_valid()) {
  4756. _set_error("The class \"" + base + "\" couldn't be fully loaded (script error or cyclic dependency).", p_class->line);
  4757. return;
  4758. }
  4759. p = nullptr;
  4760. } else {
  4761. String autoload_path = _lookup_autoload_path_for_identifier(base);
  4762. if (!autoload_path.empty()) {
  4763. base_script = ResourceLoader::load(autoload_path);
  4764. if (!base_script.is_valid()) {
  4765. _set_error("Class '" + base + "' could not be fully loaded (script error or cyclic inheritance).", p_class->line);
  4766. return;
  4767. }
  4768. p = nullptr;
  4769. }
  4770. }
  4771. while (p) {
  4772. bool found = false;
  4773. for (int i = 0; i < p->subclasses.size(); i++) {
  4774. if (p->subclasses[i]->name == base) {
  4775. ClassNode *test = p->subclasses[i];
  4776. while (test) {
  4777. if (test == p_class) {
  4778. _set_error("Cyclic inheritance.", test->line);
  4779. return;
  4780. }
  4781. if (test->base_type.kind == DataType::CLASS) {
  4782. test = test->base_type.class_type;
  4783. } else {
  4784. break;
  4785. }
  4786. }
  4787. found = true;
  4788. if (extend_iter < p_class->extends_class.size()) {
  4789. // Keep looking at current classes if possible
  4790. base = p_class->extends_class[extend_iter++];
  4791. p = p->subclasses[i];
  4792. } else {
  4793. base_class = p->subclasses[i];
  4794. }
  4795. break;
  4796. }
  4797. }
  4798. if (base_class) {
  4799. break;
  4800. }
  4801. if (found) {
  4802. continue;
  4803. }
  4804. if (p->constant_expressions.has(base)) {
  4805. if (p->constant_expressions[base].expression->type != Node::TYPE_CONSTANT) {
  4806. _set_error("Couldn't resolve the constant \"" + base + "\".", p_class->line);
  4807. return;
  4808. }
  4809. const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[base].expression);
  4810. base_script = cn->value;
  4811. if (base_script.is_null()) {
  4812. _set_error("Constant isn't a class: " + base, p_class->line);
  4813. return;
  4814. }
  4815. break;
  4816. }
  4817. p = p->owner;
  4818. }
  4819. if (base_script.is_valid()) {
  4820. String ident = base;
  4821. Ref<GDScript> find_subclass = base_script;
  4822. for (int i = extend_iter; i < p_class->extends_class.size(); i++) {
  4823. String subclass = p_class->extends_class[i];
  4824. ident += ("." + subclass);
  4825. if (find_subclass->get_subclasses().has(subclass)) {
  4826. find_subclass = find_subclass->get_subclasses()[subclass];
  4827. } else if (find_subclass->get_constants().has(subclass)) {
  4828. Ref<GDScript> new_base_class = find_subclass->get_constants()[subclass];
  4829. if (new_base_class.is_null()) {
  4830. _set_error("Constant isn't a class: " + ident, p_class->line);
  4831. return;
  4832. }
  4833. find_subclass = new_base_class;
  4834. } else {
  4835. _set_error("Couldn't find the subclass: " + ident, p_class->line);
  4836. return;
  4837. }
  4838. }
  4839. script = find_subclass;
  4840. } else if (!base_class) {
  4841. if (p_class->extends_class.size() > 1) {
  4842. _set_error("Invalid inheritance (unknown class + subclasses).", p_class->line);
  4843. return;
  4844. }
  4845. //if not found, try engine classes
  4846. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  4847. _set_error("Unknown class: \"" + base + "\"", p_class->line);
  4848. return;
  4849. }
  4850. native = base;
  4851. }
  4852. }
  4853. if (base_class) {
  4854. p_class->base_type.has_type = true;
  4855. p_class->base_type.kind = DataType::CLASS;
  4856. p_class->base_type.class_type = base_class;
  4857. } else if (script.is_valid()) {
  4858. p_class->base_type.has_type = true;
  4859. p_class->base_type.kind = DataType::GDSCRIPT;
  4860. p_class->base_type.script_type = script;
  4861. p_class->base_type.native_type = script->get_instance_base_type();
  4862. } else if (native != StringName()) {
  4863. p_class->base_type.has_type = true;
  4864. p_class->base_type.kind = DataType::NATIVE;
  4865. p_class->base_type.native_type = native;
  4866. } else {
  4867. _set_error("Couldn't determine inheritance.", p_class->line);
  4868. return;
  4869. }
  4870. } else {
  4871. // without extends, implicitly extend Reference
  4872. p_class->base_type.has_type = true;
  4873. p_class->base_type.kind = DataType::NATIVE;
  4874. p_class->base_type.native_type = "Reference";
  4875. }
  4876. if (p_recursive) {
  4877. // Recursively determine subclasses
  4878. for (int i = 0; i < p_class->subclasses.size(); i++) {
  4879. _determine_inheritance(p_class->subclasses[i], p_recursive);
  4880. }
  4881. }
  4882. }
  4883. String GDScriptParser::DataType::to_string() const {
  4884. if (!has_type) {
  4885. return "var";
  4886. }
  4887. switch (kind) {
  4888. case BUILTIN: {
  4889. if (builtin_type == Variant::NIL) {
  4890. return "null";
  4891. }
  4892. return Variant::get_type_name(builtin_type);
  4893. } break;
  4894. case NATIVE: {
  4895. if (is_meta_type) {
  4896. return "GDScriptNativeClass";
  4897. }
  4898. return native_type.operator String();
  4899. } break;
  4900. case GDSCRIPT: {
  4901. Ref<GDScript> gds = script_type;
  4902. const String &gds_class = gds->get_script_class_name();
  4903. if (!gds_class.empty()) {
  4904. return gds_class;
  4905. }
  4906. FALLTHROUGH;
  4907. }
  4908. case SCRIPT: {
  4909. if (is_meta_type) {
  4910. return script_type->get_class_name().operator String();
  4911. }
  4912. String name = script_type->get_name();
  4913. if (name != String()) {
  4914. return name;
  4915. }
  4916. name = script_type->get_path().get_file();
  4917. if (name != String()) {
  4918. return name;
  4919. }
  4920. return native_type.operator String();
  4921. } break;
  4922. case CLASS: {
  4923. ERR_FAIL_COND_V(!class_type, String());
  4924. if (is_meta_type) {
  4925. return "GDScript";
  4926. }
  4927. if (class_type->name == StringName()) {
  4928. return "self";
  4929. }
  4930. return class_type->name.operator String();
  4931. } break;
  4932. case UNRESOLVED: {
  4933. } break;
  4934. }
  4935. return "Unresolved";
  4936. }
  4937. bool GDScriptParser::_parse_type(DataType &r_type, bool p_can_be_void) {
  4938. tokenizer->advance();
  4939. r_type.has_type = true;
  4940. bool finished = false;
  4941. bool can_index = false;
  4942. String full_name;
  4943. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  4944. completion_cursor = StringName();
  4945. completion_type = COMPLETION_TYPE_HINT;
  4946. completion_class = current_class;
  4947. completion_function = current_function;
  4948. completion_line = tokenizer->get_token_line();
  4949. completion_argument = 0;
  4950. completion_block = current_block;
  4951. completion_found = true;
  4952. completion_ident_is_call = p_can_be_void;
  4953. tokenizer->advance();
  4954. }
  4955. switch (tokenizer->get_token()) {
  4956. case GDScriptTokenizer::TK_PR_VOID: {
  4957. if (!p_can_be_void) {
  4958. return false;
  4959. }
  4960. r_type.kind = DataType::BUILTIN;
  4961. r_type.builtin_type = Variant::NIL;
  4962. } break;
  4963. case GDScriptTokenizer::TK_BUILT_IN_TYPE: {
  4964. r_type.builtin_type = tokenizer->get_token_type();
  4965. if (tokenizer->get_token_type() == Variant::OBJECT) {
  4966. r_type.kind = DataType::NATIVE;
  4967. r_type.native_type = "Object";
  4968. } else {
  4969. r_type.kind = DataType::BUILTIN;
  4970. }
  4971. } break;
  4972. case GDScriptTokenizer::TK_IDENTIFIER: {
  4973. r_type.native_type = tokenizer->get_token_identifier();
  4974. if (ClassDB::class_exists(r_type.native_type) || ClassDB::class_exists("_" + r_type.native_type.operator String())) {
  4975. r_type.kind = DataType::NATIVE;
  4976. } else {
  4977. r_type.kind = DataType::UNRESOLVED;
  4978. can_index = true;
  4979. full_name = r_type.native_type;
  4980. }
  4981. } break;
  4982. default: {
  4983. return false;
  4984. }
  4985. }
  4986. tokenizer->advance();
  4987. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  4988. completion_cursor = r_type.native_type;
  4989. completion_type = COMPLETION_TYPE_HINT;
  4990. completion_class = current_class;
  4991. completion_function = current_function;
  4992. completion_line = tokenizer->get_token_line();
  4993. completion_argument = 0;
  4994. completion_block = current_block;
  4995. completion_found = true;
  4996. completion_ident_is_call = p_can_be_void;
  4997. tokenizer->advance();
  4998. }
  4999. if (can_index) {
  5000. while (!finished) {
  5001. switch (tokenizer->get_token()) {
  5002. case GDScriptTokenizer::TK_PERIOD: {
  5003. if (!can_index) {
  5004. _set_error("Unexpected \".\".");
  5005. return false;
  5006. }
  5007. can_index = false;
  5008. tokenizer->advance();
  5009. } break;
  5010. case GDScriptTokenizer::TK_CURSOR:
  5011. case GDScriptTokenizer::TK_IDENTIFIER: {
  5012. if (can_index) {
  5013. _set_error("Unexpected identifier.");
  5014. return false;
  5015. }
  5016. StringName id;
  5017. bool has_completion = _get_completable_identifier(COMPLETION_TYPE_HINT_INDEX, id);
  5018. if (id == StringName()) {
  5019. id = "@temp";
  5020. }
  5021. full_name += "." + id.operator String();
  5022. can_index = true;
  5023. if (has_completion) {
  5024. completion_cursor = full_name;
  5025. }
  5026. } break;
  5027. default: {
  5028. finished = true;
  5029. } break;
  5030. }
  5031. }
  5032. if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PERIOD) {
  5033. _set_error("Expected a subclass identifier.");
  5034. return false;
  5035. }
  5036. r_type.native_type = full_name;
  5037. }
  5038. return true;
  5039. }
  5040. GDScriptParser::DataType GDScriptParser::_resolve_type(const DataType &p_source, int p_line) {
  5041. if (!p_source.has_type) {
  5042. return p_source;
  5043. }
  5044. if (p_source.kind != DataType::UNRESOLVED) {
  5045. return p_source;
  5046. }
  5047. Vector<String> full_name = p_source.native_type.operator String().split(".", false);
  5048. int name_part = 0;
  5049. DataType result;
  5050. result.has_type = true;
  5051. while (name_part < full_name.size()) {
  5052. bool found = false;
  5053. StringName id = full_name[name_part];
  5054. DataType base_type = result;
  5055. ClassNode *p = nullptr;
  5056. if (name_part == 0) {
  5057. if (ScriptServer::is_global_class(id)) {
  5058. String script_path = ScriptServer::get_global_class_path(id);
  5059. if (script_path == self_path) {
  5060. result.kind = DataType::CLASS;
  5061. result.class_type = static_cast<ClassNode *>(head);
  5062. } else {
  5063. Ref<Script> script = ResourceLoader::load(script_path);
  5064. Ref<GDScript> gds = script;
  5065. if (gds.is_valid()) {
  5066. if (!gds->is_valid()) {
  5067. _set_error("The class \"" + id + "\" couldn't be fully loaded (script error or cyclic dependency).", p_line);
  5068. return DataType();
  5069. }
  5070. result.kind = DataType::GDSCRIPT;
  5071. result.script_type = gds;
  5072. } else if (script.is_valid()) {
  5073. result.kind = DataType::SCRIPT;
  5074. result.script_type = script;
  5075. } else {
  5076. _set_error("The class \"" + id + "\" was found in global scope, but its script couldn't be loaded.", p_line);
  5077. return DataType();
  5078. }
  5079. }
  5080. name_part++;
  5081. continue;
  5082. }
  5083. String autoload_path = _lookup_autoload_path_for_identifier(id);
  5084. if (!autoload_path.empty()) {
  5085. Ref<Script> script = ResourceLoader::load(autoload_path);
  5086. Ref<GDScript> gds = script;
  5087. if (gds.is_valid()) {
  5088. if (!gds->is_valid()) {
  5089. _set_error("Class '" + id + "' could not be fully loaded (script error or cyclic inheritance).", p_line);
  5090. return DataType();
  5091. }
  5092. result.kind = DataType::GDSCRIPT;
  5093. result.script_type = gds;
  5094. } else if (script.is_valid()) {
  5095. result.kind = DataType::SCRIPT;
  5096. result.script_type = script;
  5097. } else {
  5098. _set_error("Couldn't fully load singleton script '" + id + "' (possible cyclic reference or parse error).", p_line);
  5099. return DataType();
  5100. }
  5101. name_part++;
  5102. continue;
  5103. }
  5104. p = current_class;
  5105. } else if (base_type.kind == DataType::CLASS) {
  5106. p = base_type.class_type;
  5107. }
  5108. while (p) {
  5109. if (p->constant_expressions.has(id)) {
  5110. if (p->constant_expressions[id].expression->type != Node::TYPE_CONSTANT) {
  5111. _set_error("Parser bug: unresolved constant.", p_line);
  5112. ERR_FAIL_V(result);
  5113. }
  5114. const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[id].expression);
  5115. Ref<GDScript> gds = cn->value;
  5116. if (gds.is_valid()) {
  5117. result.kind = DataType::GDSCRIPT;
  5118. result.script_type = gds;
  5119. found = true;
  5120. } else {
  5121. Ref<Script> scr = cn->value;
  5122. if (scr.is_valid()) {
  5123. result.kind = DataType::SCRIPT;
  5124. result.script_type = scr;
  5125. found = true;
  5126. }
  5127. }
  5128. break;
  5129. }
  5130. // Inner classes
  5131. ClassNode *outer_class = p;
  5132. while (outer_class) {
  5133. if (outer_class->name == id) {
  5134. found = true;
  5135. result.kind = DataType::CLASS;
  5136. result.class_type = outer_class;
  5137. break;
  5138. }
  5139. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  5140. if (outer_class->subclasses[i] == p) {
  5141. continue;
  5142. }
  5143. if (outer_class->subclasses[i]->name == id) {
  5144. found = true;
  5145. result.kind = DataType::CLASS;
  5146. result.class_type = outer_class->subclasses[i];
  5147. break;
  5148. }
  5149. }
  5150. if (found) {
  5151. break;
  5152. }
  5153. outer_class = outer_class->owner;
  5154. }
  5155. if (!found && p->base_type.kind == DataType::CLASS) {
  5156. p = p->base_type.class_type;
  5157. } else {
  5158. base_type = p->base_type;
  5159. break;
  5160. }
  5161. }
  5162. // Still look for class constants in parent scripts
  5163. if (!found && (base_type.kind == DataType::GDSCRIPT || base_type.kind == DataType::SCRIPT)) {
  5164. Ref<Script> scr = base_type.script_type;
  5165. ERR_FAIL_COND_V(scr.is_null(), result);
  5166. while (scr.is_valid()) {
  5167. Map<StringName, Variant> constants;
  5168. scr->get_constants(&constants);
  5169. if (constants.has(id)) {
  5170. Ref<GDScript> gds = constants[id];
  5171. if (gds.is_valid()) {
  5172. result.kind = DataType::GDSCRIPT;
  5173. result.script_type = gds;
  5174. found = true;
  5175. } else {
  5176. Ref<Script> scr2 = constants[id];
  5177. if (scr2.is_valid()) {
  5178. result.kind = DataType::SCRIPT;
  5179. result.script_type = scr2;
  5180. found = true;
  5181. }
  5182. }
  5183. }
  5184. if (found) {
  5185. break;
  5186. } else {
  5187. scr = scr->get_base_script();
  5188. }
  5189. }
  5190. }
  5191. if (!found && !for_completion) {
  5192. String base;
  5193. if (name_part == 0) {
  5194. base = "self";
  5195. } else {
  5196. base = result.to_string();
  5197. }
  5198. _set_error("The identifier \"" + String(id) +
  5199. "\" isn't a valid type (not a script or class), or couldn't be found on base \"" + base + "\".",
  5200. p_line);
  5201. return DataType();
  5202. }
  5203. name_part++;
  5204. }
  5205. return result;
  5206. }
  5207. GDScriptParser::DataType GDScriptParser::_type_from_variant(const Variant &p_value) const {
  5208. DataType result;
  5209. result.has_type = true;
  5210. result.is_constant = true;
  5211. result.kind = DataType::BUILTIN;
  5212. result.builtin_type = p_value.get_type();
  5213. if (result.builtin_type == Variant::OBJECT) {
  5214. Object *obj = p_value.operator Object *();
  5215. if (!obj) {
  5216. return DataType();
  5217. }
  5218. result.native_type = obj->get_class_name();
  5219. Ref<Script> scr = p_value;
  5220. if (scr.is_valid()) {
  5221. result.is_meta_type = true;
  5222. } else {
  5223. result.is_meta_type = false;
  5224. scr = obj->get_script();
  5225. }
  5226. if (scr.is_valid()) {
  5227. result.script_type = scr;
  5228. Ref<GDScript> gds = scr;
  5229. if (gds.is_valid()) {
  5230. result.kind = DataType::GDSCRIPT;
  5231. } else {
  5232. result.kind = DataType::SCRIPT;
  5233. }
  5234. result.native_type = scr->get_instance_base_type();
  5235. } else {
  5236. result.kind = DataType::NATIVE;
  5237. }
  5238. }
  5239. return result;
  5240. }
  5241. GDScriptParser::DataType GDScriptParser::_type_from_property(const PropertyInfo &p_property, bool p_nil_is_variant) const {
  5242. DataType ret;
  5243. if (p_property.type == Variant::NIL && (p_nil_is_variant || (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  5244. // Variant
  5245. return ret;
  5246. }
  5247. ret.has_type = true;
  5248. ret.builtin_type = p_property.type;
  5249. if (p_property.type == Variant::OBJECT) {
  5250. ret.kind = DataType::NATIVE;
  5251. ret.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  5252. } else {
  5253. ret.kind = DataType::BUILTIN;
  5254. }
  5255. return ret;
  5256. }
  5257. GDScriptParser::DataType GDScriptParser::_type_from_gdtype(const GDScriptDataType &p_gdtype) const {
  5258. DataType result;
  5259. if (!p_gdtype.has_type) {
  5260. return result;
  5261. }
  5262. result.has_type = true;
  5263. result.builtin_type = p_gdtype.builtin_type;
  5264. result.native_type = p_gdtype.native_type;
  5265. result.script_type = Ref<Script>(p_gdtype.script_type);
  5266. switch (p_gdtype.kind) {
  5267. case GDScriptDataType::UNINITIALIZED: {
  5268. ERR_PRINT("Uninitialized datatype. Please report a bug.");
  5269. } break;
  5270. case GDScriptDataType::BUILTIN: {
  5271. result.kind = DataType::BUILTIN;
  5272. } break;
  5273. case GDScriptDataType::NATIVE: {
  5274. result.kind = DataType::NATIVE;
  5275. } break;
  5276. case GDScriptDataType::GDSCRIPT: {
  5277. result.kind = DataType::GDSCRIPT;
  5278. } break;
  5279. case GDScriptDataType::SCRIPT: {
  5280. result.kind = DataType::SCRIPT;
  5281. } break;
  5282. }
  5283. return result;
  5284. }
  5285. GDScriptParser::DataType GDScriptParser::_get_operation_type(const Variant::Operator p_op, const DataType &p_a, const DataType &p_b, bool &r_valid) const {
  5286. if (!p_a.has_type || !p_b.has_type) {
  5287. r_valid = true;
  5288. return DataType();
  5289. }
  5290. Variant::Type a_type = p_a.kind == DataType::BUILTIN ? p_a.builtin_type : Variant::OBJECT;
  5291. Variant::Type b_type = p_b.kind == DataType::BUILTIN ? p_b.builtin_type : Variant::OBJECT;
  5292. Variant a;
  5293. REF a_ref;
  5294. if (a_type == Variant::OBJECT) {
  5295. a_ref.instance();
  5296. a = a_ref;
  5297. } else {
  5298. Variant::CallError err;
  5299. a = Variant::construct(a_type, nullptr, 0, err);
  5300. if (err.error != Variant::CallError::CALL_OK) {
  5301. r_valid = false;
  5302. return DataType();
  5303. }
  5304. }
  5305. Variant b;
  5306. REF b_ref;
  5307. if (b_type == Variant::OBJECT) {
  5308. b_ref.instance();
  5309. b = b_ref;
  5310. } else {
  5311. Variant::CallError err;
  5312. b = Variant::construct(b_type, nullptr, 0, err);
  5313. if (err.error != Variant::CallError::CALL_OK) {
  5314. r_valid = false;
  5315. return DataType();
  5316. }
  5317. }
  5318. // Avoid division by zero
  5319. if (a_type == Variant::INT || a_type == Variant::REAL) {
  5320. Variant::evaluate(Variant::OP_ADD, a, 1, a, r_valid);
  5321. }
  5322. if (b_type == Variant::INT || b_type == Variant::REAL) {
  5323. Variant::evaluate(Variant::OP_ADD, b, 1, b, r_valid);
  5324. }
  5325. if (a_type == Variant::STRING && b_type != Variant::ARRAY) {
  5326. a = "%s"; // Work around for formatting operator (%)
  5327. }
  5328. Variant ret;
  5329. Variant::evaluate(p_op, a, b, ret, r_valid);
  5330. if (r_valid) {
  5331. return _type_from_variant(ret);
  5332. }
  5333. return DataType();
  5334. }
  5335. Variant::Operator GDScriptParser::_get_variant_operation(const OperatorNode::Operator &p_op) const {
  5336. switch (p_op) {
  5337. case OperatorNode::OP_NEG: {
  5338. return Variant::OP_NEGATE;
  5339. } break;
  5340. case OperatorNode::OP_POS: {
  5341. return Variant::OP_POSITIVE;
  5342. } break;
  5343. case OperatorNode::OP_NOT: {
  5344. return Variant::OP_NOT;
  5345. } break;
  5346. case OperatorNode::OP_BIT_INVERT: {
  5347. return Variant::OP_BIT_NEGATE;
  5348. } break;
  5349. case OperatorNode::OP_IN: {
  5350. return Variant::OP_IN;
  5351. } break;
  5352. case OperatorNode::OP_EQUAL: {
  5353. return Variant::OP_EQUAL;
  5354. } break;
  5355. case OperatorNode::OP_NOT_EQUAL: {
  5356. return Variant::OP_NOT_EQUAL;
  5357. } break;
  5358. case OperatorNode::OP_LESS: {
  5359. return Variant::OP_LESS;
  5360. } break;
  5361. case OperatorNode::OP_LESS_EQUAL: {
  5362. return Variant::OP_LESS_EQUAL;
  5363. } break;
  5364. case OperatorNode::OP_GREATER: {
  5365. return Variant::OP_GREATER;
  5366. } break;
  5367. case OperatorNode::OP_GREATER_EQUAL: {
  5368. return Variant::OP_GREATER_EQUAL;
  5369. } break;
  5370. case OperatorNode::OP_AND: {
  5371. return Variant::OP_AND;
  5372. } break;
  5373. case OperatorNode::OP_OR: {
  5374. return Variant::OP_OR;
  5375. } break;
  5376. case OperatorNode::OP_ASSIGN_ADD:
  5377. case OperatorNode::OP_ADD: {
  5378. return Variant::OP_ADD;
  5379. } break;
  5380. case OperatorNode::OP_ASSIGN_SUB:
  5381. case OperatorNode::OP_SUB: {
  5382. return Variant::OP_SUBTRACT;
  5383. } break;
  5384. case OperatorNode::OP_ASSIGN_MUL:
  5385. case OperatorNode::OP_MUL: {
  5386. return Variant::OP_MULTIPLY;
  5387. } break;
  5388. case OperatorNode::OP_ASSIGN_DIV:
  5389. case OperatorNode::OP_DIV: {
  5390. return Variant::OP_DIVIDE;
  5391. } break;
  5392. case OperatorNode::OP_ASSIGN_MOD:
  5393. case OperatorNode::OP_MOD: {
  5394. return Variant::OP_MODULE;
  5395. } break;
  5396. case OperatorNode::OP_ASSIGN_BIT_AND:
  5397. case OperatorNode::OP_BIT_AND: {
  5398. return Variant::OP_BIT_AND;
  5399. } break;
  5400. case OperatorNode::OP_ASSIGN_BIT_OR:
  5401. case OperatorNode::OP_BIT_OR: {
  5402. return Variant::OP_BIT_OR;
  5403. } break;
  5404. case OperatorNode::OP_ASSIGN_BIT_XOR:
  5405. case OperatorNode::OP_BIT_XOR: {
  5406. return Variant::OP_BIT_XOR;
  5407. } break;
  5408. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  5409. case OperatorNode::OP_SHIFT_LEFT: {
  5410. return Variant::OP_SHIFT_LEFT;
  5411. }
  5412. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  5413. case OperatorNode::OP_SHIFT_RIGHT: {
  5414. return Variant::OP_SHIFT_RIGHT;
  5415. }
  5416. default: {
  5417. return Variant::OP_MAX;
  5418. } break;
  5419. }
  5420. }
  5421. bool GDScriptParser::_is_type_compatible(const DataType &p_container, const DataType &p_expression, bool p_allow_implicit_conversion) const {
  5422. // Ignore for completion
  5423. if (!check_types || for_completion) {
  5424. return true;
  5425. }
  5426. // Can't test if not all have type
  5427. if (!p_container.has_type || !p_expression.has_type) {
  5428. return true;
  5429. }
  5430. // Should never get here unresolved
  5431. ERR_FAIL_COND_V(p_container.kind == DataType::UNRESOLVED, false);
  5432. ERR_FAIL_COND_V(p_expression.kind == DataType::UNRESOLVED, false);
  5433. if (p_container.kind == DataType::BUILTIN && p_expression.kind == DataType::BUILTIN) {
  5434. bool valid = p_container.builtin_type == p_expression.builtin_type;
  5435. if (p_allow_implicit_conversion) {
  5436. valid = valid || Variant::can_convert_strict(p_expression.builtin_type, p_container.builtin_type);
  5437. }
  5438. return valid;
  5439. }
  5440. if (p_container.kind == DataType::BUILTIN && p_container.builtin_type == Variant::OBJECT) {
  5441. // Object built-in is a special case, it's compatible with any object and with null
  5442. if (p_expression.kind == DataType::BUILTIN) {
  5443. return p_expression.builtin_type == Variant::NIL;
  5444. }
  5445. // If it's not a built-in, must be an object
  5446. return true;
  5447. }
  5448. if (p_container.kind == DataType::BUILTIN || (p_expression.kind == DataType::BUILTIN && p_expression.builtin_type != Variant::NIL)) {
  5449. // Can't mix built-ins with objects
  5450. return false;
  5451. }
  5452. // From now on everything is objects, check polymorphism
  5453. // The container must be the same class or a superclass of the expression
  5454. if (p_expression.kind == DataType::BUILTIN && p_expression.builtin_type == Variant::NIL) {
  5455. // Null can be assigned to object types
  5456. return true;
  5457. }
  5458. StringName expr_native;
  5459. Ref<Script> expr_script;
  5460. ClassNode *expr_class = nullptr;
  5461. switch (p_expression.kind) {
  5462. case DataType::NATIVE: {
  5463. if (p_container.kind != DataType::NATIVE) {
  5464. // Non-native type can't be a superclass of a native type
  5465. return false;
  5466. }
  5467. if (p_expression.is_meta_type) {
  5468. expr_native = GDScriptNativeClass::get_class_static();
  5469. } else {
  5470. expr_native = p_expression.native_type;
  5471. }
  5472. } break;
  5473. case DataType::SCRIPT:
  5474. case DataType::GDSCRIPT: {
  5475. if (p_container.kind == DataType::CLASS) {
  5476. // This cannot be resolved without cyclic dependencies, so just bail out
  5477. return false;
  5478. }
  5479. if (p_expression.is_meta_type) {
  5480. expr_native = p_expression.script_type->get_class_name();
  5481. } else {
  5482. expr_script = p_expression.script_type;
  5483. expr_native = expr_script->get_instance_base_type();
  5484. }
  5485. } break;
  5486. case DataType::CLASS: {
  5487. if (p_expression.is_meta_type) {
  5488. expr_native = GDScript::get_class_static();
  5489. } else {
  5490. expr_class = p_expression.class_type;
  5491. ClassNode *base = expr_class;
  5492. while (base->base_type.kind == DataType::CLASS) {
  5493. base = base->base_type.class_type;
  5494. }
  5495. expr_native = base->base_type.native_type;
  5496. expr_script = base->base_type.script_type;
  5497. }
  5498. } break;
  5499. case DataType::BUILTIN: // Already handled above
  5500. case DataType::UNRESOLVED: // Not allowed, see above
  5501. break;
  5502. }
  5503. // Some classes are prefixed with `_` internally
  5504. if (!ClassDB::class_exists(expr_native)) {
  5505. expr_native = "_" + expr_native;
  5506. }
  5507. switch (p_container.kind) {
  5508. case DataType::NATIVE: {
  5509. if (p_container.is_meta_type) {
  5510. return ClassDB::is_parent_class(expr_native, GDScriptNativeClass::get_class_static());
  5511. } else {
  5512. StringName container_native = ClassDB::class_exists(p_container.native_type) ? p_container.native_type : StringName("_" + p_container.native_type);
  5513. return ClassDB::is_parent_class(expr_native, container_native);
  5514. }
  5515. } break;
  5516. case DataType::SCRIPT:
  5517. case DataType::GDSCRIPT: {
  5518. if (p_container.is_meta_type) {
  5519. return ClassDB::is_parent_class(expr_native, GDScript::get_class_static());
  5520. }
  5521. if (expr_class == head && p_container.script_type->get_path() == self_path) {
  5522. // Special case: container is self script and expression is self
  5523. return true;
  5524. }
  5525. while (expr_script.is_valid()) {
  5526. if (expr_script == p_container.script_type) {
  5527. return true;
  5528. }
  5529. expr_script = expr_script->get_base_script();
  5530. }
  5531. return false;
  5532. } break;
  5533. case DataType::CLASS: {
  5534. if (p_container.is_meta_type) {
  5535. return ClassDB::is_parent_class(expr_native, GDScript::get_class_static());
  5536. }
  5537. if (p_container.class_type == head && expr_script.is_valid() && expr_script->get_path() == self_path) {
  5538. // Special case: container is self and expression is self script
  5539. return true;
  5540. }
  5541. while (expr_class) {
  5542. if (expr_class == p_container.class_type) {
  5543. return true;
  5544. }
  5545. expr_class = expr_class->base_type.class_type;
  5546. }
  5547. return false;
  5548. } break;
  5549. case DataType::BUILTIN: // Already handled above
  5550. case DataType::UNRESOLVED: // Not allowed, see above
  5551. break;
  5552. }
  5553. return false;
  5554. }
  5555. GDScriptParser::Node *GDScriptParser::_get_default_value_for_type(const DataType &p_type, int p_line) {
  5556. Node *result;
  5557. if (p_type.has_type && p_type.kind == DataType::BUILTIN && p_type.builtin_type != Variant::NIL && p_type.builtin_type != Variant::OBJECT) {
  5558. if (p_type.builtin_type == Variant::ARRAY) {
  5559. result = alloc_node<ArrayNode>();
  5560. } else if (p_type.builtin_type == Variant::DICTIONARY) {
  5561. result = alloc_node<DictionaryNode>();
  5562. } else {
  5563. ConstantNode *c = alloc_node<ConstantNode>();
  5564. Variant::CallError err;
  5565. c->value = Variant::construct(p_type.builtin_type, nullptr, 0, err);
  5566. result = c;
  5567. }
  5568. } else {
  5569. ConstantNode *c = alloc_node<ConstantNode>();
  5570. c->value = Variant();
  5571. result = c;
  5572. }
  5573. result->line = p_line;
  5574. return result;
  5575. }
  5576. GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
  5577. #ifdef DEBUG_ENABLED
  5578. if (p_node->get_datatype().has_type && p_node->type != Node::TYPE_ARRAY && p_node->type != Node::TYPE_DICTIONARY) {
  5579. #else
  5580. if (p_node->get_datatype().has_type) {
  5581. #endif
  5582. return p_node->get_datatype();
  5583. }
  5584. DataType node_type;
  5585. switch (p_node->type) {
  5586. case Node::TYPE_CONSTANT: {
  5587. node_type = _type_from_variant(static_cast<ConstantNode *>(p_node)->value);
  5588. } break;
  5589. case Node::TYPE_TYPE: {
  5590. TypeNode *tn = static_cast<TypeNode *>(p_node);
  5591. node_type.has_type = true;
  5592. node_type.is_meta_type = true;
  5593. node_type.kind = DataType::BUILTIN;
  5594. node_type.builtin_type = tn->vtype;
  5595. } break;
  5596. case Node::TYPE_ARRAY: {
  5597. node_type.has_type = true;
  5598. node_type.kind = DataType::BUILTIN;
  5599. node_type.builtin_type = Variant::ARRAY;
  5600. #ifdef DEBUG_ENABLED
  5601. // Check stuff inside the array
  5602. ArrayNode *an = static_cast<ArrayNode *>(p_node);
  5603. for (int i = 0; i < an->elements.size(); i++) {
  5604. _reduce_node_type(an->elements[i]);
  5605. }
  5606. #endif // DEBUG_ENABLED
  5607. } break;
  5608. case Node::TYPE_DICTIONARY: {
  5609. node_type.has_type = true;
  5610. node_type.kind = DataType::BUILTIN;
  5611. node_type.builtin_type = Variant::DICTIONARY;
  5612. #ifdef DEBUG_ENABLED
  5613. // Check stuff inside the dictionarty
  5614. DictionaryNode *dn = static_cast<DictionaryNode *>(p_node);
  5615. for (int i = 0; i < dn->elements.size(); i++) {
  5616. _reduce_node_type(dn->elements[i].key);
  5617. _reduce_node_type(dn->elements[i].value);
  5618. }
  5619. #endif // DEBUG_ENABLED
  5620. } break;
  5621. case Node::TYPE_SELF: {
  5622. node_type.has_type = true;
  5623. node_type.kind = DataType::CLASS;
  5624. node_type.class_type = current_class;
  5625. node_type.is_constant = true;
  5626. } break;
  5627. case Node::TYPE_IDENTIFIER: {
  5628. IdentifierNode *id = static_cast<IdentifierNode *>(p_node);
  5629. if (id->declared_block) {
  5630. node_type = id->declared_block->variables[id->name]->get_datatype();
  5631. id->declared_block->variables[id->name]->usages += 1;
  5632. } else if (id->name == "#match_value") {
  5633. // It's a special id just for the match statetement, ignore
  5634. break;
  5635. } else if (current_function && current_function->arguments.find(id->name) >= 0) {
  5636. int idx = current_function->arguments.find(id->name);
  5637. node_type = current_function->argument_types[idx];
  5638. } else {
  5639. node_type = _reduce_identifier_type(nullptr, id->name, id->line, false);
  5640. }
  5641. } break;
  5642. case Node::TYPE_CAST: {
  5643. CastNode *cn = static_cast<CastNode *>(p_node);
  5644. DataType source_type = _reduce_node_type(cn->source_node);
  5645. cn->cast_type = _resolve_type(cn->cast_type, cn->line);
  5646. if (source_type.has_type) {
  5647. bool valid = false;
  5648. if (check_types) {
  5649. if (cn->cast_type.kind == DataType::BUILTIN && source_type.kind == DataType::BUILTIN) {
  5650. valid = Variant::can_convert(source_type.builtin_type, cn->cast_type.builtin_type);
  5651. }
  5652. if (cn->cast_type.kind != DataType::BUILTIN && source_type.kind != DataType::BUILTIN) {
  5653. valid = _is_type_compatible(cn->cast_type, source_type) || _is_type_compatible(source_type, cn->cast_type);
  5654. }
  5655. if (!valid) {
  5656. _set_error("Invalid cast. Cannot convert from \"" + source_type.to_string() +
  5657. "\" to \"" + cn->cast_type.to_string() + "\".",
  5658. cn->line);
  5659. return DataType();
  5660. }
  5661. }
  5662. } else {
  5663. #ifdef DEBUG_ENABLED
  5664. _add_warning(GDScriptWarning::UNSAFE_CAST, cn->line, cn->cast_type.to_string());
  5665. #endif // DEBUG_ENABLED
  5666. _mark_line_as_unsafe(cn->line);
  5667. }
  5668. node_type = cn->cast_type;
  5669. } break;
  5670. case Node::TYPE_OPERATOR: {
  5671. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  5672. switch (op->op) {
  5673. case OperatorNode::OP_CALL:
  5674. case OperatorNode::OP_PARENT_CALL: {
  5675. node_type = _reduce_function_call_type(op);
  5676. } break;
  5677. case OperatorNode::OP_YIELD: {
  5678. if (op->arguments.size() == 2) {
  5679. DataType base_type = _reduce_node_type(op->arguments[0]);
  5680. DataType signal_type = _reduce_node_type(op->arguments[1]);
  5681. // TODO: Check if signal exists when it's a constant
  5682. if (base_type.has_type && base_type.kind == DataType::BUILTIN && base_type.builtin_type != Variant::NIL && base_type.builtin_type != Variant::OBJECT) {
  5683. _set_error("The first argument of \"yield()\" must be an object.", op->line);
  5684. return DataType();
  5685. }
  5686. if (signal_type.has_type && (signal_type.kind != DataType::BUILTIN || signal_type.builtin_type != Variant::STRING)) {
  5687. _set_error("The second argument of \"yield()\" must be a string.", op->line);
  5688. return DataType();
  5689. }
  5690. }
  5691. // yield can return anything
  5692. node_type.has_type = false;
  5693. } break;
  5694. case OperatorNode::OP_IS:
  5695. case OperatorNode::OP_IS_BUILTIN: {
  5696. if (op->arguments.size() != 2) {
  5697. _set_error("Parser bug: binary operation without 2 arguments.", op->line);
  5698. ERR_FAIL_V(DataType());
  5699. }
  5700. DataType value_type = _reduce_node_type(op->arguments[0]);
  5701. DataType type_type = _reduce_node_type(op->arguments[1]);
  5702. if (check_types && type_type.has_type) {
  5703. if (!type_type.is_meta_type && (type_type.kind != DataType::NATIVE || !ClassDB::is_parent_class(type_type.native_type, "Script"))) {
  5704. _set_error("Invalid \"is\" test: the right operand isn't a type (neither a native type nor a script).", op->line);
  5705. return DataType();
  5706. }
  5707. type_type.is_meta_type = false; // Test the actual type
  5708. if (!_is_type_compatible(type_type, value_type) && !_is_type_compatible(value_type, type_type)) {
  5709. if (op->op == OperatorNode::OP_IS) {
  5710. _set_error("A value of type \"" + value_type.to_string() + "\" will never be an instance of \"" + type_type.to_string() + "\".", op->line);
  5711. } else {
  5712. _set_error("A value of type \"" + value_type.to_string() + "\" will never be of type \"" + type_type.to_string() + "\".", op->line);
  5713. }
  5714. return DataType();
  5715. }
  5716. }
  5717. node_type.has_type = true;
  5718. node_type.is_constant = true;
  5719. node_type.is_meta_type = false;
  5720. node_type.kind = DataType::BUILTIN;
  5721. node_type.builtin_type = Variant::BOOL;
  5722. } break;
  5723. // Unary operators
  5724. case OperatorNode::OP_NEG:
  5725. case OperatorNode::OP_POS:
  5726. case OperatorNode::OP_NOT:
  5727. case OperatorNode::OP_BIT_INVERT: {
  5728. DataType argument_type = _reduce_node_type(op->arguments[0]);
  5729. if (!argument_type.has_type) {
  5730. break;
  5731. }
  5732. Variant::Operator var_op = _get_variant_operation(op->op);
  5733. bool valid = false;
  5734. node_type = _get_operation_type(var_op, argument_type, argument_type, valid);
  5735. if (check_types && !valid) {
  5736. _set_error("Invalid operand type (\"" + argument_type.to_string() +
  5737. "\") to unary operator \"" + Variant::get_operator_name(var_op) + "\".",
  5738. op->line, op->column);
  5739. return DataType();
  5740. }
  5741. } break;
  5742. // Binary operators
  5743. case OperatorNode::OP_IN:
  5744. case OperatorNode::OP_EQUAL:
  5745. case OperatorNode::OP_NOT_EQUAL:
  5746. case OperatorNode::OP_LESS:
  5747. case OperatorNode::OP_LESS_EQUAL:
  5748. case OperatorNode::OP_GREATER:
  5749. case OperatorNode::OP_GREATER_EQUAL:
  5750. case OperatorNode::OP_AND:
  5751. case OperatorNode::OP_OR:
  5752. case OperatorNode::OP_ADD:
  5753. case OperatorNode::OP_SUB:
  5754. case OperatorNode::OP_MUL:
  5755. case OperatorNode::OP_DIV:
  5756. case OperatorNode::OP_MOD:
  5757. case OperatorNode::OP_SHIFT_LEFT:
  5758. case OperatorNode::OP_SHIFT_RIGHT:
  5759. case OperatorNode::OP_BIT_AND:
  5760. case OperatorNode::OP_BIT_OR:
  5761. case OperatorNode::OP_BIT_XOR: {
  5762. if (op->arguments.size() != 2) {
  5763. _set_error("Parser bug: binary operation without 2 arguments.", op->line);
  5764. ERR_FAIL_V(DataType());
  5765. }
  5766. DataType argument_a_type = _reduce_node_type(op->arguments[0]);
  5767. DataType argument_b_type = _reduce_node_type(op->arguments[1]);
  5768. if (!argument_a_type.has_type || !argument_b_type.has_type) {
  5769. _mark_line_as_unsafe(op->line);
  5770. break;
  5771. }
  5772. Variant::Operator var_op = _get_variant_operation(op->op);
  5773. bool valid = false;
  5774. node_type = _get_operation_type(var_op, argument_a_type, argument_b_type, valid);
  5775. if (check_types && !valid) {
  5776. _set_error("Invalid operand types (\"" + argument_a_type.to_string() + "\" and \"" +
  5777. argument_b_type.to_string() + "\") to operator \"" + Variant::get_operator_name(var_op) + "\".",
  5778. op->line, op->column);
  5779. return DataType();
  5780. }
  5781. #ifdef DEBUG_ENABLED
  5782. if (var_op == Variant::OP_DIVIDE && argument_a_type.kind == DataType::BUILTIN && argument_a_type.builtin_type == Variant::INT &&
  5783. argument_b_type.kind == DataType::BUILTIN && argument_b_type.builtin_type == Variant::INT) {
  5784. _add_warning(GDScriptWarning::INTEGER_DIVISION, op->line);
  5785. }
  5786. #endif // DEBUG_ENABLED
  5787. } break;
  5788. // Ternary operators
  5789. case OperatorNode::OP_TERNARY_IF: {
  5790. if (op->arguments.size() != 3) {
  5791. _set_error("Parser bug: ternary operation without 3 arguments.");
  5792. ERR_FAIL_V(DataType());
  5793. }
  5794. DataType true_type = _reduce_node_type(op->arguments[1]);
  5795. DataType false_type = _reduce_node_type(op->arguments[2]);
  5796. // Check arguments[0] errors.
  5797. _reduce_node_type(op->arguments[0]);
  5798. // If types are equal, then the expression is of the same type
  5799. // If they are compatible, return the broader type
  5800. if (true_type == false_type || _is_type_compatible(true_type, false_type)) {
  5801. node_type = true_type;
  5802. } else if (_is_type_compatible(false_type, true_type)) {
  5803. node_type = false_type;
  5804. } else {
  5805. #ifdef DEBUG_ENABLED
  5806. _add_warning(GDScriptWarning::INCOMPATIBLE_TERNARY, op->line);
  5807. #endif // DEBUG_ENABLED
  5808. }
  5809. } break;
  5810. // Assignment should never happen within an expression
  5811. case OperatorNode::OP_ASSIGN:
  5812. case OperatorNode::OP_ASSIGN_ADD:
  5813. case OperatorNode::OP_ASSIGN_SUB:
  5814. case OperatorNode::OP_ASSIGN_MUL:
  5815. case OperatorNode::OP_ASSIGN_DIV:
  5816. case OperatorNode::OP_ASSIGN_MOD:
  5817. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  5818. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  5819. case OperatorNode::OP_ASSIGN_BIT_AND:
  5820. case OperatorNode::OP_ASSIGN_BIT_OR:
  5821. case OperatorNode::OP_ASSIGN_BIT_XOR:
  5822. case OperatorNode::OP_INIT_ASSIGN: {
  5823. _set_error("Assignment inside an expression isn't allowed (parser bug?).", op->line);
  5824. return DataType();
  5825. } break;
  5826. case OperatorNode::OP_INDEX_NAMED: {
  5827. if (op->arguments.size() != 2) {
  5828. _set_error("Parser bug: named index with invalid arguments.", op->line);
  5829. ERR_FAIL_V(DataType());
  5830. }
  5831. if (op->arguments[1]->type != Node::TYPE_IDENTIFIER) {
  5832. _set_error("Parser bug: named index without identifier argument.", op->line);
  5833. ERR_FAIL_V(DataType());
  5834. }
  5835. DataType base_type = _reduce_node_type(op->arguments[0]);
  5836. IdentifierNode *member_id = static_cast<IdentifierNode *>(op->arguments[1]);
  5837. if (base_type.has_type) {
  5838. if (check_types && base_type.kind == DataType::BUILTIN) {
  5839. // Variant type, just test if it's possible
  5840. DataType result;
  5841. switch (base_type.builtin_type) {
  5842. case Variant::NIL:
  5843. case Variant::DICTIONARY: {
  5844. result.has_type = false;
  5845. } break;
  5846. default: {
  5847. Variant::CallError err;
  5848. Variant temp = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  5849. bool valid = false;
  5850. Variant res = temp.get(member_id->name.operator String(), &valid);
  5851. if (valid) {
  5852. result = _type_from_variant(res);
  5853. } else if (check_types) {
  5854. _set_error("Can't get index \"" + String(member_id->name.operator String()) + "\" on base \"" +
  5855. base_type.to_string() + "\".",
  5856. op->line);
  5857. return DataType();
  5858. }
  5859. } break;
  5860. }
  5861. result.is_constant = false;
  5862. node_type = result;
  5863. } else {
  5864. node_type = _reduce_identifier_type(&base_type, member_id->name, op->line, true);
  5865. #ifdef DEBUG_ENABLED
  5866. if (!node_type.has_type) {
  5867. _mark_line_as_unsafe(op->line);
  5868. _add_warning(GDScriptWarning::UNSAFE_PROPERTY_ACCESS, op->line, member_id->name.operator String(), base_type.to_string());
  5869. }
  5870. #endif // DEBUG_ENABLED
  5871. }
  5872. } else {
  5873. _mark_line_as_unsafe(op->line);
  5874. }
  5875. if (error_set) {
  5876. return DataType();
  5877. }
  5878. } break;
  5879. case OperatorNode::OP_INDEX: {
  5880. if (op->arguments[1]->type == Node::TYPE_CONSTANT) {
  5881. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[1]);
  5882. if (cn->value.get_type() == Variant::STRING) {
  5883. // Treat this as named indexing
  5884. IdentifierNode *id = alloc_node<IdentifierNode>();
  5885. id->name = cn->value.operator StringName();
  5886. id->datatype = cn->datatype;
  5887. op->op = OperatorNode::OP_INDEX_NAMED;
  5888. op->arguments.write[1] = id;
  5889. return _reduce_node_type(op);
  5890. }
  5891. }
  5892. DataType base_type = _reduce_node_type(op->arguments[0]);
  5893. DataType index_type = _reduce_node_type(op->arguments[1]);
  5894. if (!base_type.has_type) {
  5895. _mark_line_as_unsafe(op->line);
  5896. break;
  5897. }
  5898. if (check_types && index_type.has_type) {
  5899. if (base_type.kind == DataType::BUILTIN) {
  5900. // Check if indexing is valid
  5901. bool error = index_type.kind != DataType::BUILTIN && base_type.builtin_type != Variant::DICTIONARY;
  5902. if (!error) {
  5903. switch (base_type.builtin_type) {
  5904. // Expect int or real as index
  5905. case Variant::POOL_BYTE_ARRAY:
  5906. case Variant::POOL_COLOR_ARRAY:
  5907. case Variant::POOL_INT_ARRAY:
  5908. case Variant::POOL_REAL_ARRAY:
  5909. case Variant::POOL_STRING_ARRAY:
  5910. case Variant::POOL_VECTOR2_ARRAY:
  5911. case Variant::POOL_VECTOR3_ARRAY:
  5912. case Variant::ARRAY:
  5913. case Variant::STRING: {
  5914. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::REAL;
  5915. } break;
  5916. // Expect String only
  5917. case Variant::RECT2:
  5918. case Variant::PLANE:
  5919. case Variant::QUAT:
  5920. case Variant::AABB:
  5921. case Variant::OBJECT: {
  5922. error = index_type.builtin_type != Variant::STRING;
  5923. } break;
  5924. // Expect String or number
  5925. case Variant::VECTOR2:
  5926. case Variant::VECTOR3:
  5927. case Variant::TRANSFORM2D:
  5928. case Variant::BASIS:
  5929. case Variant::TRANSFORM: {
  5930. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::REAL &&
  5931. index_type.builtin_type != Variant::STRING;
  5932. } break;
  5933. // Expect String or int
  5934. case Variant::COLOR: {
  5935. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING;
  5936. } break;
  5937. default: {
  5938. }
  5939. }
  5940. }
  5941. if (error) {
  5942. _set_error("Invalid index type (" + index_type.to_string() + ") for base \"" + base_type.to_string() + "\".",
  5943. op->line);
  5944. return DataType();
  5945. }
  5946. if (op->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT) {
  5947. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[1]);
  5948. // Index is a constant, just try it if possible
  5949. switch (base_type.builtin_type) {
  5950. // Arrays/string have variable indexing, can't test directly
  5951. case Variant::STRING:
  5952. case Variant::ARRAY:
  5953. case Variant::DICTIONARY:
  5954. case Variant::POOL_BYTE_ARRAY:
  5955. case Variant::POOL_COLOR_ARRAY:
  5956. case Variant::POOL_INT_ARRAY:
  5957. case Variant::POOL_REAL_ARRAY:
  5958. case Variant::POOL_STRING_ARRAY:
  5959. case Variant::POOL_VECTOR2_ARRAY:
  5960. case Variant::POOL_VECTOR3_ARRAY: {
  5961. break;
  5962. }
  5963. default: {
  5964. Variant::CallError err;
  5965. Variant temp = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  5966. bool valid = false;
  5967. Variant res = temp.get(cn->value, &valid);
  5968. if (valid) {
  5969. node_type = _type_from_variant(res);
  5970. node_type.is_constant = false;
  5971. } else if (check_types) {
  5972. _set_error("Can't get index \"" + String(cn->value) + "\" on base \"" +
  5973. base_type.to_string() + "\".",
  5974. op->line);
  5975. return DataType();
  5976. }
  5977. } break;
  5978. }
  5979. } else {
  5980. _mark_line_as_unsafe(op->line);
  5981. }
  5982. } else if (!for_completion && (index_type.kind != DataType::BUILTIN || index_type.builtin_type != Variant::STRING)) {
  5983. _set_error("Only strings can be used as an index in the base type \"" + base_type.to_string() + "\".", op->line);
  5984. return DataType();
  5985. }
  5986. }
  5987. if (check_types && !node_type.has_type && base_type.kind == DataType::BUILTIN) {
  5988. // Can infer indexing type for some variant types
  5989. DataType result;
  5990. result.has_type = true;
  5991. result.kind = DataType::BUILTIN;
  5992. switch (base_type.builtin_type) {
  5993. // Can't index at all
  5994. case Variant::NIL:
  5995. case Variant::BOOL:
  5996. case Variant::INT:
  5997. case Variant::REAL:
  5998. case Variant::NODE_PATH:
  5999. case Variant::_RID: {
  6000. _set_error("Can't index on a value of type \"" + base_type.to_string() + "\".", op->line);
  6001. return DataType();
  6002. } break;
  6003. // Return int
  6004. case Variant::POOL_BYTE_ARRAY:
  6005. case Variant::POOL_INT_ARRAY: {
  6006. result.builtin_type = Variant::INT;
  6007. } break;
  6008. // Return real
  6009. case Variant::POOL_REAL_ARRAY:
  6010. case Variant::VECTOR2:
  6011. case Variant::VECTOR3:
  6012. case Variant::QUAT: {
  6013. result.builtin_type = Variant::REAL;
  6014. } break;
  6015. // Return color
  6016. case Variant::POOL_COLOR_ARRAY: {
  6017. result.builtin_type = Variant::COLOR;
  6018. } break;
  6019. // Return string
  6020. case Variant::POOL_STRING_ARRAY:
  6021. case Variant::STRING: {
  6022. result.builtin_type = Variant::STRING;
  6023. } break;
  6024. // Return Vector2
  6025. case Variant::POOL_VECTOR2_ARRAY:
  6026. case Variant::TRANSFORM2D:
  6027. case Variant::RECT2: {
  6028. result.builtin_type = Variant::VECTOR2;
  6029. } break;
  6030. // Return Vector3
  6031. case Variant::POOL_VECTOR3_ARRAY:
  6032. case Variant::AABB:
  6033. case Variant::BASIS: {
  6034. result.builtin_type = Variant::VECTOR3;
  6035. } break;
  6036. // Depends on the index
  6037. case Variant::TRANSFORM:
  6038. case Variant::PLANE:
  6039. case Variant::COLOR:
  6040. default: {
  6041. result.has_type = false;
  6042. } break;
  6043. }
  6044. node_type = result;
  6045. }
  6046. } break;
  6047. default: {
  6048. _set_error("Parser bug: unhandled operation.", op->line);
  6049. ERR_FAIL_V(DataType());
  6050. }
  6051. }
  6052. } break;
  6053. default: {
  6054. }
  6055. }
  6056. node_type = _resolve_type(node_type, p_node->line);
  6057. p_node->set_datatype(node_type);
  6058. return node_type;
  6059. }
  6060. bool GDScriptParser::_get_function_signature(DataType &p_base_type, const StringName &p_function, DataType &r_return_type, List<DataType> &r_arg_types, int &r_default_arg_count, bool &r_static, bool &r_vararg) const {
  6061. r_static = false;
  6062. r_default_arg_count = 0;
  6063. DataType original_type = p_base_type;
  6064. ClassNode *base = nullptr;
  6065. FunctionNode *callee = nullptr;
  6066. if (p_base_type.kind == DataType::CLASS) {
  6067. base = p_base_type.class_type;
  6068. }
  6069. // Look up the current file (parse tree)
  6070. while (!callee && base) {
  6071. for (int i = 0; i < base->static_functions.size(); i++) {
  6072. FunctionNode *func = base->static_functions[i];
  6073. if (p_function == func->name) {
  6074. r_static = true;
  6075. callee = func;
  6076. break;
  6077. }
  6078. }
  6079. if (!callee && !p_base_type.is_meta_type) {
  6080. for (int i = 0; i < base->functions.size(); i++) {
  6081. FunctionNode *func = base->functions[i];
  6082. if (p_function == func->name) {
  6083. callee = func;
  6084. break;
  6085. }
  6086. }
  6087. }
  6088. p_base_type = base->base_type;
  6089. if (p_base_type.kind == DataType::CLASS) {
  6090. base = p_base_type.class_type;
  6091. } else {
  6092. break;
  6093. }
  6094. }
  6095. if (callee) {
  6096. r_return_type = callee->get_datatype();
  6097. for (int i = 0; i < callee->argument_types.size(); i++) {
  6098. r_arg_types.push_back(callee->argument_types[i]);
  6099. }
  6100. r_default_arg_count = callee->default_values.size();
  6101. return true;
  6102. }
  6103. // Nothing in current file, check parent script
  6104. Ref<GDScript> base_gdscript;
  6105. Ref<Script> base_script;
  6106. StringName native;
  6107. if (p_base_type.kind == DataType::GDSCRIPT) {
  6108. base_gdscript = p_base_type.script_type;
  6109. if (base_gdscript.is_null() || !base_gdscript->is_valid()) {
  6110. // GDScript wasn't properly compíled, don't bother trying
  6111. return false;
  6112. }
  6113. } else if (p_base_type.kind == DataType::SCRIPT) {
  6114. base_script = p_base_type.script_type;
  6115. } else if (p_base_type.kind == DataType::NATIVE) {
  6116. native = p_base_type.native_type;
  6117. }
  6118. while (base_gdscript.is_valid()) {
  6119. native = base_gdscript->get_instance_base_type();
  6120. Map<StringName, GDScriptFunction *> funcs = base_gdscript->get_member_functions();
  6121. if (funcs.has(p_function)) {
  6122. GDScriptFunction *f = funcs[p_function];
  6123. r_static = f->is_static();
  6124. r_default_arg_count = f->get_default_argument_count();
  6125. r_return_type = _type_from_gdtype(f->get_return_type());
  6126. for (int i = 0; i < f->get_argument_count(); i++) {
  6127. r_arg_types.push_back(_type_from_gdtype(f->get_argument_type(i)));
  6128. }
  6129. return true;
  6130. }
  6131. base_gdscript = base_gdscript->get_base_script();
  6132. }
  6133. while (base_script.is_valid()) {
  6134. native = base_script->get_instance_base_type();
  6135. MethodInfo mi = base_script->get_method_info(p_function);
  6136. if (!(mi == MethodInfo())) {
  6137. r_return_type = _type_from_property(mi.return_val, false);
  6138. r_default_arg_count = mi.default_arguments.size();
  6139. for (List<PropertyInfo>::Element *E = mi.arguments.front(); E; E = E->next()) {
  6140. r_arg_types.push_back(_type_from_property(E->get()));
  6141. }
  6142. return true;
  6143. }
  6144. base_script = base_script->get_base_script();
  6145. }
  6146. if (native == StringName()) {
  6147. // Empty native class, might happen in some Script implementations
  6148. // Just ignore it
  6149. return false;
  6150. }
  6151. // Only native remains
  6152. if (!ClassDB::class_exists(native)) {
  6153. native = "_" + native.operator String();
  6154. }
  6155. if (!ClassDB::class_exists(native)) {
  6156. if (!check_types) {
  6157. return false;
  6158. }
  6159. ERR_FAIL_V_MSG(false, "Parser bug: Class '" + String(native) + "' not found.");
  6160. }
  6161. MethodBind *method = ClassDB::get_method(native, p_function);
  6162. if (!method) {
  6163. // Try virtual methods
  6164. List<MethodInfo> virtuals;
  6165. ClassDB::get_virtual_methods(native, &virtuals);
  6166. for (const List<MethodInfo>::Element *E = virtuals.front(); E; E = E->next()) {
  6167. const MethodInfo &mi = E->get();
  6168. if (mi.name == p_function) {
  6169. r_default_arg_count = mi.default_arguments.size();
  6170. for (const List<PropertyInfo>::Element *pi = mi.arguments.front(); pi; pi = pi->next()) {
  6171. r_arg_types.push_back(_type_from_property(pi->get()));
  6172. }
  6173. r_return_type = _type_from_property(mi.return_val, false);
  6174. r_vararg = mi.flags & METHOD_FLAG_VARARG;
  6175. return true;
  6176. }
  6177. }
  6178. // If the base is a script, it might be trying to access members of the Script class itself
  6179. if (original_type.is_meta_type && !(p_function == "new") && (original_type.kind == DataType::SCRIPT || original_type.kind == DataType::GDSCRIPT)) {
  6180. method = ClassDB::get_method(original_type.script_type->get_class_name(), p_function);
  6181. if (method) {
  6182. r_static = true;
  6183. } else {
  6184. // Try virtual methods of the script type
  6185. virtuals.clear();
  6186. ClassDB::get_virtual_methods(original_type.script_type->get_class_name(), &virtuals);
  6187. for (const List<MethodInfo>::Element *E = virtuals.front(); E; E = E->next()) {
  6188. const MethodInfo &mi = E->get();
  6189. if (mi.name == p_function) {
  6190. r_default_arg_count = mi.default_arguments.size();
  6191. for (const List<PropertyInfo>::Element *pi = mi.arguments.front(); pi; pi = pi->next()) {
  6192. r_arg_types.push_back(_type_from_property(pi->get()));
  6193. }
  6194. r_return_type = _type_from_property(mi.return_val, false);
  6195. r_static = true;
  6196. r_vararg = mi.flags & METHOD_FLAG_VARARG;
  6197. return true;
  6198. }
  6199. }
  6200. return false;
  6201. }
  6202. } else {
  6203. return false;
  6204. }
  6205. }
  6206. r_default_arg_count = method->get_default_argument_count();
  6207. if (method->get_name() == "get_script") {
  6208. r_return_type = DataType(); // Variant for now and let runtime decide.
  6209. } else {
  6210. r_return_type = _type_from_property(method->get_return_info(), false);
  6211. }
  6212. r_vararg = method->is_vararg();
  6213. for (int i = 0; i < method->get_argument_count(); i++) {
  6214. r_arg_types.push_back(_type_from_property(method->get_argument_info(i)));
  6215. }
  6216. return true;
  6217. }
  6218. GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const OperatorNode *p_call) {
  6219. if (p_call->arguments.size() < 1) {
  6220. _set_error("Parser bug: function call without enough arguments.", p_call->line);
  6221. ERR_FAIL_V(DataType());
  6222. }
  6223. DataType return_type;
  6224. List<DataType> arg_types;
  6225. int default_args_count = 0;
  6226. String callee_name;
  6227. bool is_vararg = false;
  6228. #ifdef DEBUG_ENABLED
  6229. int arg_count = p_call->arguments.size();
  6230. #endif
  6231. switch (p_call->arguments[0]->type) {
  6232. case GDScriptParser::Node::TYPE_TYPE: {
  6233. // Built-in constructor, special case
  6234. TypeNode *tn = static_cast<TypeNode *>(p_call->arguments[0]);
  6235. Vector<DataType> par_types;
  6236. par_types.resize(p_call->arguments.size() - 1);
  6237. for (int i = 1; i < p_call->arguments.size(); i++) {
  6238. par_types.write[i - 1] = _reduce_node_type(p_call->arguments[i]);
  6239. }
  6240. if (error_set) {
  6241. return DataType();
  6242. }
  6243. // Special case: check copy constructor. Those are defined implicitly in Variant.
  6244. if (par_types.size() == 1) {
  6245. if (!par_types[0].has_type || (par_types[0].kind == DataType::BUILTIN && par_types[0].builtin_type == tn->vtype)) {
  6246. DataType result;
  6247. result.has_type = true;
  6248. result.kind = DataType::BUILTIN;
  6249. result.builtin_type = tn->vtype;
  6250. return result;
  6251. }
  6252. }
  6253. bool match = false;
  6254. List<MethodInfo> constructors;
  6255. Variant::get_constructor_list(tn->vtype, &constructors);
  6256. PropertyInfo return_type2;
  6257. for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
  6258. MethodInfo &mi = E->get();
  6259. if (p_call->arguments.size() - 1 < mi.arguments.size() - mi.default_arguments.size()) {
  6260. continue;
  6261. }
  6262. if (p_call->arguments.size() - 1 > mi.arguments.size()) {
  6263. continue;
  6264. }
  6265. bool types_match = true;
  6266. for (int i = 0; i < par_types.size(); i++) {
  6267. DataType arg_type;
  6268. if (mi.arguments[i].type != Variant::NIL) {
  6269. arg_type.has_type = true;
  6270. arg_type.kind = mi.arguments[i].type == Variant::OBJECT ? DataType::NATIVE : DataType::BUILTIN;
  6271. arg_type.builtin_type = mi.arguments[i].type;
  6272. arg_type.native_type = mi.arguments[i].class_name;
  6273. }
  6274. if (!_is_type_compatible(arg_type, par_types[i], true)) {
  6275. types_match = false;
  6276. break;
  6277. } else {
  6278. #ifdef DEBUG_ENABLED
  6279. if (arg_type.kind == DataType::BUILTIN && arg_type.builtin_type == Variant::INT && par_types[i].kind == DataType::BUILTIN && par_types[i].builtin_type == Variant::REAL) {
  6280. _add_warning(GDScriptWarning::NARROWING_CONVERSION, p_call->line, Variant::get_type_name(tn->vtype));
  6281. }
  6282. if (par_types[i].may_yield && p_call->arguments[i + 1]->type == Node::TYPE_OPERATOR) {
  6283. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, p_call->line, _find_function_name(static_cast<OperatorNode *>(p_call->arguments[i + 1])));
  6284. }
  6285. #endif // DEBUG_ENABLED
  6286. }
  6287. }
  6288. if (types_match) {
  6289. match = true;
  6290. return_type2 = mi.return_val;
  6291. break;
  6292. }
  6293. }
  6294. if (match) {
  6295. return _type_from_property(return_type2, false);
  6296. } else if (check_types) {
  6297. String err = "No constructor of '";
  6298. err += Variant::get_type_name(tn->vtype);
  6299. err += "' matches the signature '";
  6300. err += Variant::get_type_name(tn->vtype) + "(";
  6301. for (int i = 0; i < par_types.size(); i++) {
  6302. if (i > 0) {
  6303. err += ", ";
  6304. }
  6305. err += par_types[i].to_string();
  6306. }
  6307. err += ")'.";
  6308. _set_error(err, p_call->line, p_call->column);
  6309. return DataType();
  6310. }
  6311. return DataType();
  6312. } break;
  6313. case GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION: {
  6314. BuiltInFunctionNode *func = static_cast<BuiltInFunctionNode *>(p_call->arguments[0]);
  6315. MethodInfo mi = GDScriptFunctions::get_info(func->function);
  6316. return_type = _type_from_property(mi.return_val, false);
  6317. // Check all arguments beforehand to solve warnings
  6318. for (int i = 1; i < p_call->arguments.size(); i++) {
  6319. _reduce_node_type(p_call->arguments[i]);
  6320. }
  6321. // Check arguments
  6322. is_vararg = mi.flags & METHOD_FLAG_VARARG;
  6323. default_args_count = mi.default_arguments.size();
  6324. callee_name = mi.name;
  6325. #ifdef DEBUG_ENABLED
  6326. arg_count -= 1;
  6327. #endif
  6328. // Check each argument type
  6329. for (List<PropertyInfo>::Element *E = mi.arguments.front(); E; E = E->next()) {
  6330. arg_types.push_back(_type_from_property(E->get()));
  6331. }
  6332. } break;
  6333. default: {
  6334. if (p_call->op == OperatorNode::OP_CALL && p_call->arguments.size() < 2) {
  6335. _set_error("Parser bug: self method call without enough arguments.", p_call->line);
  6336. ERR_FAIL_V(DataType());
  6337. }
  6338. int arg_id = p_call->op == OperatorNode::OP_CALL ? 1 : 0;
  6339. if (p_call->arguments[arg_id]->type != Node::TYPE_IDENTIFIER) {
  6340. _set_error("Parser bug: invalid function call argument.", p_call->line);
  6341. ERR_FAIL_V(DataType());
  6342. }
  6343. // Check all arguments beforehand to solve warnings
  6344. for (int i = arg_id + 1; i < p_call->arguments.size(); i++) {
  6345. _reduce_node_type(p_call->arguments[i]);
  6346. }
  6347. IdentifierNode *func_id = static_cast<IdentifierNode *>(p_call->arguments[arg_id]);
  6348. callee_name = func_id->name;
  6349. #ifdef DEBUG_ENABLED
  6350. arg_count -= 1 + arg_id;
  6351. #endif
  6352. DataType base_type;
  6353. if (p_call->op == OperatorNode::OP_PARENT_CALL) {
  6354. base_type = current_class->base_type;
  6355. } else {
  6356. base_type = _reduce_node_type(p_call->arguments[0]);
  6357. }
  6358. if (!base_type.has_type || (base_type.kind == DataType::BUILTIN && base_type.builtin_type == Variant::NIL)) {
  6359. _mark_line_as_unsafe(p_call->line);
  6360. return DataType();
  6361. }
  6362. if (base_type.kind == DataType::BUILTIN) {
  6363. Variant::CallError err;
  6364. Variant tmp = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  6365. if (check_types) {
  6366. if (!tmp.has_method(callee_name)) {
  6367. _set_error("The method \"" + callee_name + "\" isn't declared on base \"" + base_type.to_string() + "\".", p_call->line);
  6368. return DataType();
  6369. }
  6370. default_args_count = Variant::get_method_default_arguments(base_type.builtin_type, callee_name).size();
  6371. const Vector<Variant::Type> &var_arg_types = Variant::get_method_argument_types(base_type.builtin_type, callee_name);
  6372. for (int i = 0; i < var_arg_types.size(); i++) {
  6373. DataType argtype;
  6374. if (var_arg_types[i] != Variant::NIL) {
  6375. argtype.has_type = true;
  6376. argtype.kind = DataType::BUILTIN;
  6377. argtype.builtin_type = var_arg_types[i];
  6378. }
  6379. arg_types.push_back(argtype);
  6380. }
  6381. }
  6382. bool rets = false;
  6383. return_type.has_type = true;
  6384. return_type.kind = DataType::BUILTIN;
  6385. return_type.builtin_type = Variant::get_method_return_type(base_type.builtin_type, callee_name, &rets);
  6386. // If the method returns, but it might return any type, (Variant::NIL), pretend we don't know the type.
  6387. // At least make sure we know that it returns
  6388. if (rets && return_type.builtin_type == Variant::NIL) {
  6389. return_type.has_type = false;
  6390. }
  6391. break;
  6392. }
  6393. DataType original_type = base_type;
  6394. bool is_initializer = callee_name == "new";
  6395. bool is_get_script = p_call->arguments[0]->type == Node::TYPE_SELF && callee_name == "get_script";
  6396. bool is_static = false;
  6397. bool valid = false;
  6398. if (is_initializer && original_type.is_meta_type) {
  6399. // Try to check it as initializer
  6400. base_type = original_type;
  6401. callee_name = "_init";
  6402. base_type.is_meta_type = false;
  6403. valid = _get_function_signature(base_type, callee_name, return_type, arg_types,
  6404. default_args_count, is_static, is_vararg);
  6405. return_type = original_type;
  6406. return_type.is_meta_type = false;
  6407. valid = true; // There's always an initializer, we can assume this is true
  6408. }
  6409. if (is_get_script) {
  6410. // get_script() can be considered a meta-type.
  6411. return_type.kind = DataType::CLASS;
  6412. return_type.class_type = static_cast<ClassNode *>(head);
  6413. return_type.is_meta_type = true;
  6414. valid = true;
  6415. }
  6416. if (!valid) {
  6417. base_type = original_type;
  6418. return_type = DataType();
  6419. valid = _get_function_signature(base_type, callee_name, return_type, arg_types,
  6420. default_args_count, is_static, is_vararg);
  6421. }
  6422. if (!valid) {
  6423. #ifdef DEBUG_ENABLED
  6424. if (p_call->arguments[0]->type == Node::TYPE_SELF) {
  6425. _set_error("The method \"" + callee_name + "\" isn't declared in the current class.", p_call->line);
  6426. return DataType();
  6427. }
  6428. DataType tmp_type;
  6429. valid = _get_member_type(original_type, func_id->name, tmp_type);
  6430. if (valid) {
  6431. if (tmp_type.is_constant) {
  6432. _add_warning(GDScriptWarning::CONSTANT_USED_AS_FUNCTION, p_call->line, callee_name, original_type.to_string());
  6433. } else {
  6434. _add_warning(GDScriptWarning::PROPERTY_USED_AS_FUNCTION, p_call->line, callee_name, original_type.to_string());
  6435. }
  6436. }
  6437. _add_warning(GDScriptWarning::UNSAFE_METHOD_ACCESS, p_call->line, callee_name, original_type.to_string());
  6438. _mark_line_as_unsafe(p_call->line);
  6439. #endif // DEBUG_ENABLED
  6440. return DataType();
  6441. }
  6442. #ifdef DEBUG_ENABLED
  6443. if (current_function && !for_completion && !is_static && p_call->arguments[0]->type == Node::TYPE_SELF && current_function->_static) {
  6444. _set_error("Can't call non-static function from a static function.", p_call->line);
  6445. return DataType();
  6446. }
  6447. if (check_types && !is_static && !is_initializer && base_type.is_meta_type) {
  6448. _set_error("Non-static function \"" + String(callee_name) + "\" can only be called from an instance.", p_call->line);
  6449. return DataType();
  6450. }
  6451. // Check signal emission for warnings
  6452. if (callee_name == "emit_signal" && p_call->op == OperatorNode::OP_CALL && p_call->arguments[0]->type == Node::TYPE_SELF && p_call->arguments.size() >= 3 && p_call->arguments[2]->type == Node::TYPE_CONSTANT) {
  6453. ConstantNode *sig = static_cast<ConstantNode *>(p_call->arguments[2]);
  6454. String emitted = sig->value.get_type() == Variant::STRING ? sig->value.operator String() : "";
  6455. for (int i = 0; i < current_class->_signals.size(); i++) {
  6456. if (current_class->_signals[i].name == emitted) {
  6457. current_class->_signals.write[i].emissions += 1;
  6458. break;
  6459. }
  6460. }
  6461. }
  6462. #endif // DEBUG_ENABLED
  6463. } break;
  6464. }
  6465. #ifdef DEBUG_ENABLED
  6466. if (!check_types) {
  6467. return return_type;
  6468. }
  6469. if (arg_count < arg_types.size() - default_args_count) {
  6470. _set_error("Too few arguments for \"" + callee_name + "()\" call. Expected at least " + itos(arg_types.size() - default_args_count) + ".", p_call->line);
  6471. return return_type;
  6472. }
  6473. if (!is_vararg && arg_count > arg_types.size()) {
  6474. _set_error("Too many arguments for \"" + callee_name + "()\" call. Expected at most " + itos(arg_types.size()) + ".", p_call->line);
  6475. return return_type;
  6476. }
  6477. int arg_diff = p_call->arguments.size() - arg_count;
  6478. for (int i = arg_diff; i < p_call->arguments.size(); i++) {
  6479. DataType par_type = _reduce_node_type(p_call->arguments[i]);
  6480. if ((i - arg_diff) >= arg_types.size()) {
  6481. continue;
  6482. }
  6483. DataType arg_type = arg_types[i - arg_diff];
  6484. if (!par_type.has_type) {
  6485. _mark_line_as_unsafe(p_call->line);
  6486. if (par_type.may_yield && p_call->arguments[i]->type == Node::TYPE_OPERATOR) {
  6487. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, p_call->line, _find_function_name(static_cast<OperatorNode *>(p_call->arguments[i])));
  6488. }
  6489. } else if (!_is_type_compatible(arg_types[i - arg_diff], par_type, true)) {
  6490. // Supertypes are acceptable for dynamic compliance
  6491. if (!_is_type_compatible(par_type, arg_types[i - arg_diff])) {
  6492. _set_error("At \"" + callee_name + "()\" call, argument " + itos(i - arg_diff + 1) + ". The passed argument's type (" +
  6493. par_type.to_string() + ") doesn't match the function's expected argument type (" +
  6494. arg_types[i - arg_diff].to_string() + ").",
  6495. p_call->line);
  6496. return DataType();
  6497. } else {
  6498. _mark_line_as_unsafe(p_call->line);
  6499. }
  6500. } else {
  6501. if (arg_type.kind == DataType::BUILTIN && arg_type.builtin_type == Variant::INT && par_type.kind == DataType::BUILTIN && par_type.builtin_type == Variant::REAL) {
  6502. _add_warning(GDScriptWarning::NARROWING_CONVERSION, p_call->line, callee_name);
  6503. }
  6504. }
  6505. }
  6506. #endif // DEBUG_ENABLED
  6507. return return_type;
  6508. }
  6509. bool GDScriptParser::_get_member_type(const DataType &p_base_type, const StringName &p_member, DataType &r_member_type, bool *r_is_const) const {
  6510. DataType base_type = p_base_type;
  6511. // Check classes in current file
  6512. ClassNode *base = nullptr;
  6513. if (base_type.kind == DataType::CLASS) {
  6514. base = base_type.class_type;
  6515. }
  6516. while (base) {
  6517. if (base->constant_expressions.has(p_member)) {
  6518. if (r_is_const) {
  6519. *r_is_const = true;
  6520. }
  6521. r_member_type = base->constant_expressions[p_member].expression->get_datatype();
  6522. return true;
  6523. }
  6524. if (!base_type.is_meta_type) {
  6525. for (int i = 0; i < base->variables.size(); i++) {
  6526. if (base->variables[i].identifier == p_member) {
  6527. r_member_type = base->variables[i].data_type;
  6528. base->variables.write[i].usages += 1;
  6529. return true;
  6530. }
  6531. }
  6532. } else {
  6533. for (int i = 0; i < base->subclasses.size(); i++) {
  6534. ClassNode *c = base->subclasses[i];
  6535. if (c->name == p_member) {
  6536. DataType class_type;
  6537. class_type.has_type = true;
  6538. class_type.is_constant = true;
  6539. class_type.is_meta_type = true;
  6540. class_type.kind = DataType::CLASS;
  6541. class_type.class_type = c;
  6542. r_member_type = class_type;
  6543. return true;
  6544. }
  6545. }
  6546. }
  6547. base_type = base->base_type;
  6548. if (base_type.kind == DataType::CLASS) {
  6549. base = base_type.class_type;
  6550. } else {
  6551. break;
  6552. }
  6553. }
  6554. Ref<GDScript> gds;
  6555. if (base_type.kind == DataType::GDSCRIPT) {
  6556. gds = base_type.script_type;
  6557. if (gds.is_null() || !gds->is_valid()) {
  6558. // GDScript wasn't properly compíled, don't bother trying
  6559. return false;
  6560. }
  6561. }
  6562. Ref<Script> scr;
  6563. if (base_type.kind == DataType::SCRIPT) {
  6564. scr = base_type.script_type;
  6565. }
  6566. StringName native;
  6567. if (base_type.kind == DataType::NATIVE) {
  6568. native = base_type.native_type;
  6569. }
  6570. // Check GDScripts
  6571. while (gds.is_valid()) {
  6572. if (gds->get_constants().has(p_member)) {
  6573. Variant c = gds->get_constants()[p_member];
  6574. r_member_type = _type_from_variant(c);
  6575. return true;
  6576. }
  6577. if (!base_type.is_meta_type) {
  6578. if (gds->get_members().has(p_member)) {
  6579. r_member_type = _type_from_gdtype(gds->get_member_type(p_member));
  6580. return true;
  6581. }
  6582. }
  6583. native = gds->get_instance_base_type();
  6584. if (gds->get_base_script().is_valid()) {
  6585. gds = gds->get_base_script();
  6586. scr = gds->get_base_script();
  6587. bool is_meta = base_type.is_meta_type;
  6588. base_type = _type_from_variant(scr.operator Variant());
  6589. base_type.is_meta_type = is_meta;
  6590. } else {
  6591. break;
  6592. }
  6593. }
  6594. #define IS_USAGE_MEMBER(m_usage) (!(m_usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)))
  6595. // Check other script types
  6596. while (scr.is_valid()) {
  6597. Map<StringName, Variant> constants;
  6598. scr->get_constants(&constants);
  6599. if (constants.has(p_member)) {
  6600. r_member_type = _type_from_variant(constants[p_member]);
  6601. return true;
  6602. }
  6603. List<PropertyInfo> properties;
  6604. scr->get_script_property_list(&properties);
  6605. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  6606. if (E->get().name == p_member && IS_USAGE_MEMBER(E->get().usage)) {
  6607. r_member_type = _type_from_property(E->get());
  6608. return true;
  6609. }
  6610. }
  6611. base_type = _type_from_variant(scr.operator Variant());
  6612. native = scr->get_instance_base_type();
  6613. scr = scr->get_base_script();
  6614. }
  6615. if (native == StringName()) {
  6616. // Empty native class, might happen in some Script implementations
  6617. // Just ignore it
  6618. return false;
  6619. }
  6620. // Check ClassDB
  6621. if (!ClassDB::class_exists(native)) {
  6622. native = "_" + native.operator String();
  6623. }
  6624. if (!ClassDB::class_exists(native)) {
  6625. if (!check_types) {
  6626. return false;
  6627. }
  6628. ERR_FAIL_V_MSG(false, "Parser bug: Class \"" + String(native) + "\" not found.");
  6629. }
  6630. bool valid = false;
  6631. ClassDB::get_integer_constant(native, p_member, &valid);
  6632. if (valid) {
  6633. DataType ct;
  6634. ct.has_type = true;
  6635. ct.is_constant = true;
  6636. ct.kind = DataType::BUILTIN;
  6637. ct.builtin_type = Variant::INT;
  6638. r_member_type = ct;
  6639. return true;
  6640. }
  6641. if (!base_type.is_meta_type) {
  6642. List<PropertyInfo> properties;
  6643. ClassDB::get_property_list(native, &properties);
  6644. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  6645. if (E->get().name == p_member && IS_USAGE_MEMBER(E->get().usage)) {
  6646. // Check if a getter exists
  6647. StringName getter_name = ClassDB::get_property_getter(native, p_member);
  6648. if (getter_name != StringName()) {
  6649. // Use the getter return type
  6650. MethodBind *getter_method = ClassDB::get_method(native, getter_name);
  6651. if (getter_method) {
  6652. r_member_type = _type_from_property(getter_method->get_return_info());
  6653. } else {
  6654. r_member_type = DataType();
  6655. }
  6656. } else {
  6657. r_member_type = _type_from_property(E->get());
  6658. }
  6659. return true;
  6660. }
  6661. }
  6662. }
  6663. // If the base is a script, it might be trying to access members of the Script class itself
  6664. if (p_base_type.is_meta_type && (p_base_type.kind == DataType::SCRIPT || p_base_type.kind == DataType::GDSCRIPT)) {
  6665. native = p_base_type.script_type->get_class_name();
  6666. ClassDB::get_integer_constant(native, p_member, &valid);
  6667. if (valid) {
  6668. DataType ct;
  6669. ct.has_type = true;
  6670. ct.is_constant = true;
  6671. ct.kind = DataType::BUILTIN;
  6672. ct.builtin_type = Variant::INT;
  6673. r_member_type = ct;
  6674. return true;
  6675. }
  6676. List<PropertyInfo> properties;
  6677. ClassDB::get_property_list(native, &properties);
  6678. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  6679. if (E->get().name == p_member && IS_USAGE_MEMBER(E->get().usage)) {
  6680. // Check if a getter exists
  6681. StringName getter_name = ClassDB::get_property_getter(native, p_member);
  6682. if (getter_name != StringName()) {
  6683. // Use the getter return type
  6684. MethodBind *getter_method = ClassDB::get_method(native, getter_name);
  6685. if (getter_method) {
  6686. r_member_type = _type_from_property(getter_method->get_return_info());
  6687. } else {
  6688. r_member_type = DataType();
  6689. }
  6690. } else {
  6691. r_member_type = _type_from_property(E->get());
  6692. }
  6693. return true;
  6694. }
  6695. }
  6696. }
  6697. #undef IS_USAGE_MEMBER
  6698. return false;
  6699. }
  6700. GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType *p_base_type, const StringName &p_identifier, int p_line, bool p_is_indexing) {
  6701. if (p_base_type && !p_base_type->has_type) {
  6702. return DataType();
  6703. }
  6704. DataType base_type;
  6705. DataType member_type;
  6706. if (!p_base_type) {
  6707. base_type.has_type = true;
  6708. base_type.is_constant = true;
  6709. base_type.kind = DataType::CLASS;
  6710. base_type.class_type = current_class;
  6711. } else {
  6712. base_type = DataType(*p_base_type);
  6713. }
  6714. bool is_const = false;
  6715. if (_get_member_type(base_type, p_identifier, member_type, &is_const)) {
  6716. if (!p_base_type && current_function && current_function->_static && !is_const) {
  6717. _set_error("Can't access member variable (\"" + p_identifier.operator String() + "\") from a static function.", p_line);
  6718. return DataType();
  6719. }
  6720. return member_type;
  6721. }
  6722. if (p_is_indexing) {
  6723. // Don't look for globals since this is an indexed identifier
  6724. return DataType();
  6725. }
  6726. if (!p_base_type) {
  6727. // Possibly this is a global, check before failing
  6728. if (ClassDB::class_exists(p_identifier) || ClassDB::class_exists("_" + p_identifier.operator String())) {
  6729. DataType result;
  6730. result.has_type = true;
  6731. result.is_constant = true;
  6732. result.is_meta_type = true;
  6733. if (Engine::get_singleton()->has_singleton(p_identifier) || Engine::get_singleton()->has_singleton("_" + p_identifier.operator String())) {
  6734. result.is_meta_type = false;
  6735. }
  6736. result.kind = DataType::NATIVE;
  6737. result.native_type = p_identifier;
  6738. return result;
  6739. }
  6740. ClassNode *outer_class = current_class;
  6741. while (outer_class) {
  6742. if (outer_class->name == p_identifier) {
  6743. DataType result;
  6744. result.has_type = true;
  6745. result.is_constant = true;
  6746. result.is_meta_type = true;
  6747. result.kind = DataType::CLASS;
  6748. result.class_type = outer_class;
  6749. return result;
  6750. }
  6751. if (outer_class->constant_expressions.has(p_identifier)) {
  6752. return outer_class->constant_expressions[p_identifier].type;
  6753. }
  6754. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  6755. if (outer_class->subclasses[i] == current_class) {
  6756. continue;
  6757. }
  6758. if (outer_class->subclasses[i]->name == p_identifier) {
  6759. DataType result;
  6760. result.has_type = true;
  6761. result.is_constant = true;
  6762. result.is_meta_type = true;
  6763. result.kind = DataType::CLASS;
  6764. result.class_type = outer_class->subclasses[i];
  6765. return result;
  6766. }
  6767. }
  6768. outer_class = outer_class->owner;
  6769. }
  6770. if (ScriptServer::is_global_class(p_identifier)) {
  6771. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  6772. if (scr.is_valid()) {
  6773. DataType result;
  6774. result.has_type = true;
  6775. result.script_type = scr;
  6776. result.is_constant = true;
  6777. result.is_meta_type = true;
  6778. Ref<GDScript> gds = scr;
  6779. if (gds.is_valid()) {
  6780. if (!gds->is_valid()) {
  6781. _set_error("The class \"" + p_identifier + "\" couldn't be fully loaded (script error or cyclic dependency).");
  6782. return DataType();
  6783. }
  6784. result.kind = DataType::GDSCRIPT;
  6785. } else {
  6786. result.kind = DataType::SCRIPT;
  6787. }
  6788. return result;
  6789. }
  6790. _set_error("The class \"" + p_identifier + "\" was found in global scope, but its script couldn't be loaded.");
  6791. return DataType();
  6792. }
  6793. if (GDScriptLanguage::get_singleton()->get_global_map().has(p_identifier)) {
  6794. int idx = GDScriptLanguage::get_singleton()->get_global_map()[p_identifier];
  6795. Variant g = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  6796. return _type_from_variant(g);
  6797. }
  6798. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier)) {
  6799. Variant g = GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier];
  6800. return _type_from_variant(g);
  6801. }
  6802. // Non-tool singletons aren't loaded, check project settings
  6803. String autoload_path = _lookup_autoload_path_for_identifier(p_identifier);
  6804. if (!autoload_path.empty()) {
  6805. Ref<Script> singleton = ResourceLoader::load(autoload_path);
  6806. if (singleton.is_valid()) {
  6807. DataType result;
  6808. result.has_type = true;
  6809. result.is_constant = true;
  6810. result.script_type = singleton;
  6811. Ref<GDScript> gds = singleton;
  6812. if (gds.is_valid()) {
  6813. if (!gds->is_valid()) {
  6814. _set_error("Couldn't fully load the singleton script \"" + p_identifier + "\" (possible cyclic reference or parse error).", p_line);
  6815. return DataType();
  6816. }
  6817. result.kind = DataType::GDSCRIPT;
  6818. } else {
  6819. result.kind = DataType::SCRIPT;
  6820. }
  6821. }
  6822. }
  6823. // This means looking in the current class, which type is always known
  6824. _set_error("The identifier \"" + p_identifier.operator String() + "\" isn't declared in the current scope.", p_line);
  6825. }
  6826. #ifdef DEBUG_ENABLED
  6827. {
  6828. DataType tmp_type;
  6829. List<DataType> arg_types;
  6830. int argcount;
  6831. bool _static;
  6832. bool vararg;
  6833. if (_get_function_signature(base_type, p_identifier, tmp_type, arg_types, argcount, _static, vararg)) {
  6834. _add_warning(GDScriptWarning::FUNCTION_USED_AS_PROPERTY, p_line, p_identifier.operator String(), base_type.to_string());
  6835. }
  6836. }
  6837. #endif // DEBUG_ENABLED
  6838. _mark_line_as_unsafe(p_line);
  6839. return DataType();
  6840. }
  6841. void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
  6842. // Names of internal object properties that we check to avoid overriding them.
  6843. // "__meta__" could also be in here, but since it doesn't really affect object metadata,
  6844. // it is okay to override it on script.
  6845. StringName script_name = CoreStringNames::get_singleton()->_script;
  6846. _mark_line_as_safe(p_class->line);
  6847. // Constants
  6848. for (Map<StringName, ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
  6849. ClassNode::Constant &c = E->get();
  6850. _mark_line_as_safe(c.expression->line);
  6851. DataType cont = _resolve_type(c.type, c.expression->line);
  6852. DataType expr = _resolve_type(c.expression->get_datatype(), c.expression->line);
  6853. if (check_types && !_is_type_compatible(cont, expr)) {
  6854. _set_error("The constant value type (" + expr.to_string() + ") isn't compatible with declared type (" + cont.to_string() + ").",
  6855. c.expression->line);
  6856. return;
  6857. }
  6858. expr.is_constant = true;
  6859. c.type = expr;
  6860. c.expression->set_datatype(expr);
  6861. DataType tmp;
  6862. const StringName &constant_name = E->key();
  6863. if (constant_name == script_name || _get_member_type(p_class->base_type, constant_name, tmp)) {
  6864. _set_error("The member \"" + String(constant_name) + "\" already exists in a parent class.", c.expression->line);
  6865. return;
  6866. }
  6867. }
  6868. // Function declarations
  6869. for (int i = 0; i < p_class->static_functions.size(); i++) {
  6870. _check_function_types(p_class->static_functions[i]);
  6871. if (error_set) {
  6872. return;
  6873. }
  6874. }
  6875. for (int i = 0; i < p_class->functions.size(); i++) {
  6876. _check_function_types(p_class->functions[i]);
  6877. if (error_set) {
  6878. return;
  6879. }
  6880. }
  6881. // Class variables
  6882. for (int i = 0; i < p_class->variables.size(); i++) {
  6883. ClassNode::Member &v = p_class->variables.write[i];
  6884. DataType tmp;
  6885. if (v.identifier == script_name || _get_member_type(p_class->base_type, v.identifier, tmp)) {
  6886. _set_error("The member \"" + String(v.identifier) + "\" already exists in a parent class.", v.line);
  6887. return;
  6888. }
  6889. _mark_line_as_safe(v.line);
  6890. v.data_type = _resolve_type(v.data_type, v.line);
  6891. v.initial_assignment->arguments[0]->set_datatype(v.data_type);
  6892. if (v.expression) {
  6893. DataType expr_type = _reduce_node_type(v.expression);
  6894. if (check_types && !_is_type_compatible(v.data_type, expr_type)) {
  6895. // Try supertype test
  6896. if (_is_type_compatible(expr_type, v.data_type)) {
  6897. _mark_line_as_unsafe(v.line);
  6898. } else {
  6899. // Try with implicit conversion
  6900. if (v.data_type.kind != DataType::BUILTIN || !_is_type_compatible(v.data_type, expr_type, true)) {
  6901. _set_error("The assigned expression's type (" + expr_type.to_string() + ") doesn't match the variable's type (" +
  6902. v.data_type.to_string() + ").",
  6903. v.line);
  6904. return;
  6905. }
  6906. // Replace assignment with implicit conversion
  6907. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  6908. convert->line = v.line;
  6909. convert->function = GDScriptFunctions::TYPE_CONVERT;
  6910. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  6911. tgt_type->line = v.line;
  6912. tgt_type->value = (int)v.data_type.builtin_type;
  6913. OperatorNode *convert_call = alloc_node<OperatorNode>();
  6914. convert_call->line = v.line;
  6915. convert_call->op = OperatorNode::OP_CALL;
  6916. convert_call->arguments.push_back(convert);
  6917. convert_call->arguments.push_back(v.expression);
  6918. convert_call->arguments.push_back(tgt_type);
  6919. v.expression = convert_call;
  6920. v.initial_assignment->arguments.write[1] = convert_call;
  6921. }
  6922. }
  6923. if (v.data_type.infer_type) {
  6924. if (!expr_type.has_type) {
  6925. _set_error("The assigned value doesn't have a set type; the variable type can't be inferred.", v.line);
  6926. return;
  6927. }
  6928. if (expr_type.kind == DataType::BUILTIN && expr_type.builtin_type == Variant::NIL) {
  6929. _set_error("The variable type cannot be inferred because its value is \"null\".", v.line);
  6930. return;
  6931. }
  6932. v.data_type = expr_type;
  6933. v.data_type.is_constant = false;
  6934. }
  6935. }
  6936. // Check export hint
  6937. if (v.data_type.has_type && v._export.type != Variant::NIL) {
  6938. DataType export_type = _type_from_property(v._export);
  6939. if (!_is_type_compatible(v.data_type, export_type, true)) {
  6940. _set_error("The export hint's type (" + export_type.to_string() + ") doesn't match the variable's type (" +
  6941. v.data_type.to_string() + ").",
  6942. v.line);
  6943. return;
  6944. }
  6945. }
  6946. // Setter and getter
  6947. if (v.setter == StringName() && v.getter == StringName()) {
  6948. continue;
  6949. }
  6950. bool found_getter = false;
  6951. bool found_setter = false;
  6952. for (int j = 0; j < p_class->functions.size(); j++) {
  6953. if (v.setter == p_class->functions[j]->name) {
  6954. found_setter = true;
  6955. FunctionNode *setter = p_class->functions[j];
  6956. if (setter->get_required_argument_count() != 1 &&
  6957. !(setter->get_required_argument_count() == 0 && setter->default_values.size() > 0)) {
  6958. _set_error("The setter function needs to receive exactly 1 argument. See \"" + setter->name +
  6959. "()\" definition at line " + itos(setter->line) + ".",
  6960. v.line);
  6961. return;
  6962. }
  6963. if (!_is_type_compatible(v.data_type, setter->argument_types[0])) {
  6964. _set_error("The setter argument's type (" + setter->argument_types[0].to_string() +
  6965. ") doesn't match the variable's type (" + v.data_type.to_string() + "). See \"" +
  6966. setter->name + "()\" definition at line " + itos(setter->line) + ".",
  6967. v.line);
  6968. return;
  6969. }
  6970. continue;
  6971. }
  6972. if (v.getter == p_class->functions[j]->name) {
  6973. found_getter = true;
  6974. FunctionNode *getter = p_class->functions[j];
  6975. if (getter->get_required_argument_count() != 0) {
  6976. _set_error("The getter function can't receive arguments. See \"" + getter->name +
  6977. "()\" definition at line " + itos(getter->line) + ".",
  6978. v.line);
  6979. return;
  6980. }
  6981. if (!_is_type_compatible(v.data_type, getter->get_datatype())) {
  6982. _set_error("The getter return type (" + getter->get_datatype().to_string() +
  6983. ") doesn't match the variable's type (" + v.data_type.to_string() +
  6984. "). See \"" + getter->name + "()\" definition at line " + itos(getter->line) + ".",
  6985. v.line);
  6986. return;
  6987. }
  6988. }
  6989. if (found_getter && found_setter) {
  6990. break;
  6991. }
  6992. }
  6993. if ((found_getter || v.getter == StringName()) && (found_setter || v.setter == StringName())) {
  6994. continue;
  6995. }
  6996. // Check for static functions
  6997. for (int j = 0; j < p_class->static_functions.size(); j++) {
  6998. if (v.setter == p_class->static_functions[j]->name) {
  6999. FunctionNode *setter = p_class->static_functions[j];
  7000. _set_error("The setter can't be a static function. See \"" + setter->name + "()\" definition at line " + itos(setter->line) + ".", v.line);
  7001. return;
  7002. }
  7003. if (v.getter == p_class->static_functions[j]->name) {
  7004. FunctionNode *getter = p_class->static_functions[j];
  7005. _set_error("The getter can't be a static function. See \"" + getter->name + "()\" definition at line " + itos(getter->line) + ".", v.line);
  7006. return;
  7007. }
  7008. }
  7009. if (!found_setter && v.setter != StringName()) {
  7010. _set_error("The setter function isn't defined.", v.line);
  7011. return;
  7012. }
  7013. if (!found_getter && v.getter != StringName()) {
  7014. _set_error("The getter function isn't defined.", v.line);
  7015. return;
  7016. }
  7017. }
  7018. // Signals
  7019. DataType base = p_class->base_type;
  7020. while (base.kind == DataType::CLASS) {
  7021. ClassNode *base_class = base.class_type;
  7022. for (int i = 0; i < p_class->_signals.size(); i++) {
  7023. for (int j = 0; j < base_class->_signals.size(); j++) {
  7024. if (p_class->_signals[i].name == base_class->_signals[j].name) {
  7025. _set_error("The signal \"" + p_class->_signals[i].name + "\" already exists in a parent class.", p_class->_signals[i].line);
  7026. return;
  7027. }
  7028. }
  7029. }
  7030. base = base_class->base_type;
  7031. }
  7032. StringName native;
  7033. if (base.kind == DataType::GDSCRIPT || base.kind == DataType::SCRIPT) {
  7034. Ref<Script> scr = base.script_type;
  7035. if (scr.is_valid() && scr->is_valid()) {
  7036. native = scr->get_instance_base_type();
  7037. for (int i = 0; i < p_class->_signals.size(); i++) {
  7038. if (scr->has_script_signal(p_class->_signals[i].name)) {
  7039. _set_error("The signal \"" + p_class->_signals[i].name + "\" already exists in a parent class.", p_class->_signals[i].line);
  7040. return;
  7041. }
  7042. }
  7043. }
  7044. } else if (base.kind == DataType::NATIVE) {
  7045. native = base.native_type;
  7046. }
  7047. if (native != StringName()) {
  7048. for (int i = 0; i < p_class->_signals.size(); i++) {
  7049. if (ClassDB::has_signal(native, p_class->_signals[i].name)) {
  7050. _set_error("The signal \"" + p_class->_signals[i].name + "\" already exists in a parent class.", p_class->_signals[i].line);
  7051. return;
  7052. }
  7053. }
  7054. }
  7055. // Inner classes
  7056. for (int i = 0; i < p_class->subclasses.size(); i++) {
  7057. current_class = p_class->subclasses[i];
  7058. _check_class_level_types(current_class);
  7059. if (error_set) {
  7060. return;
  7061. }
  7062. current_class = p_class;
  7063. }
  7064. }
  7065. void GDScriptParser::_check_function_types(FunctionNode *p_function) {
  7066. p_function->return_type = _resolve_type(p_function->return_type, p_function->line);
  7067. // Arguments
  7068. int defaults_ofs = p_function->arguments.size() - p_function->default_values.size();
  7069. for (int i = 0; i < p_function->arguments.size(); i++) {
  7070. if (i < defaults_ofs) {
  7071. p_function->argument_types.write[i] = _resolve_type(p_function->argument_types[i], p_function->line);
  7072. } else {
  7073. if (p_function->default_values[i - defaults_ofs]->type != Node::TYPE_OPERATOR) {
  7074. _set_error("Parser bug: invalid argument default value.", p_function->line, p_function->column);
  7075. return;
  7076. }
  7077. OperatorNode *op = static_cast<OperatorNode *>(p_function->default_values[i - defaults_ofs]);
  7078. if (op->op != OperatorNode::OP_ASSIGN || op->arguments.size() != 2) {
  7079. _set_error("Parser bug: invalid argument default value operation.", p_function->line);
  7080. return;
  7081. }
  7082. DataType def_type = _reduce_node_type(op->arguments[1]);
  7083. if (p_function->argument_types[i].infer_type) {
  7084. def_type.is_constant = false;
  7085. p_function->argument_types.write[i] = def_type;
  7086. } else {
  7087. p_function->argument_types.write[i] = _resolve_type(p_function->argument_types[i], p_function->line);
  7088. if (!_is_type_compatible(p_function->argument_types[i], def_type, true)) {
  7089. String arg_name = p_function->arguments[i];
  7090. _set_error("Value type (" + def_type.to_string() + ") doesn't match the type of argument '" +
  7091. arg_name + "' (" + p_function->argument_types[i].to_string() + ").",
  7092. p_function->line);
  7093. }
  7094. }
  7095. }
  7096. #ifdef DEBUG_ENABLED
  7097. if (p_function->arguments_usage[i] == 0 && !p_function->arguments[i].operator String().begins_with("_")) {
  7098. _add_warning(GDScriptWarning::UNUSED_ARGUMENT, p_function->line, p_function->name, p_function->arguments[i].operator String());
  7099. }
  7100. for (int j = 0; j < current_class->variables.size(); j++) {
  7101. if (current_class->variables[j].identifier == p_function->arguments[i]) {
  7102. _add_warning(GDScriptWarning::SHADOWED_VARIABLE, p_function->line, p_function->arguments[i], itos(current_class->variables[j].line));
  7103. }
  7104. }
  7105. #endif // DEBUG_ENABLED
  7106. }
  7107. if (!(p_function->name == "_init")) {
  7108. // Signature for the initializer may vary
  7109. #ifdef DEBUG_ENABLED
  7110. DataType return_type;
  7111. List<DataType> arg_types;
  7112. int default_arg_count = 0;
  7113. bool _static = false;
  7114. bool vararg = false;
  7115. DataType base_type = current_class->base_type;
  7116. if (_get_function_signature(base_type, p_function->name, return_type, arg_types, default_arg_count, _static, vararg)) {
  7117. bool valid = _static == p_function->_static;
  7118. valid = valid && return_type == p_function->return_type;
  7119. int argsize_diff = p_function->arguments.size() - arg_types.size();
  7120. valid = valid && argsize_diff >= 0;
  7121. valid = valid && p_function->default_values.size() >= default_arg_count + argsize_diff;
  7122. int i = 0;
  7123. for (List<DataType>::Element *E = arg_types.front(); valid && E; E = E->next()) {
  7124. valid = valid && E->get() == p_function->argument_types[i++];
  7125. }
  7126. if (!valid) {
  7127. String parent_signature = return_type.has_type ? return_type.to_string() : "Variant";
  7128. if (parent_signature == "null") {
  7129. parent_signature = "void";
  7130. }
  7131. parent_signature += " " + p_function->name + "(";
  7132. if (arg_types.size()) {
  7133. int j = 0;
  7134. for (List<DataType>::Element *E = arg_types.front(); E; E = E->next()) {
  7135. if (E != arg_types.front()) {
  7136. parent_signature += ", ";
  7137. }
  7138. String arg = E->get().to_string();
  7139. if (arg == "null" || arg == "var") {
  7140. arg = "Variant";
  7141. }
  7142. parent_signature += arg;
  7143. if (j == arg_types.size() - default_arg_count) {
  7144. parent_signature += "=default";
  7145. }
  7146. j++;
  7147. }
  7148. }
  7149. parent_signature += ")";
  7150. _set_error("The function signature doesn't match the parent. Parent signature is: \"" + parent_signature + "\".", p_function->line);
  7151. return;
  7152. }
  7153. }
  7154. #endif // DEBUG_ENABLED
  7155. } else {
  7156. if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) {
  7157. _set_error("The constructor can't return a value.", p_function->line);
  7158. return;
  7159. }
  7160. }
  7161. if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) {
  7162. if (!p_function->body->has_return) {
  7163. _set_error("A non-void function must return a value in all possible paths.", p_function->line);
  7164. return;
  7165. }
  7166. }
  7167. if (p_function->has_yield) {
  7168. // yield() will make the function return a GDScriptFunctionState, so the type is ambiguous
  7169. p_function->return_type.has_type = false;
  7170. p_function->return_type.may_yield = true;
  7171. }
  7172. }
  7173. void GDScriptParser::_check_class_blocks_types(ClassNode *p_class) {
  7174. // Function blocks
  7175. for (int i = 0; i < p_class->static_functions.size(); i++) {
  7176. current_function = p_class->static_functions[i];
  7177. current_block = current_function->body;
  7178. _mark_line_as_safe(current_function->line);
  7179. _check_block_types(current_block);
  7180. current_block = nullptr;
  7181. current_function = nullptr;
  7182. if (error_set) {
  7183. return;
  7184. }
  7185. }
  7186. for (int i = 0; i < p_class->functions.size(); i++) {
  7187. current_function = p_class->functions[i];
  7188. current_block = current_function->body;
  7189. _mark_line_as_safe(current_function->line);
  7190. _check_block_types(current_block);
  7191. current_block = nullptr;
  7192. current_function = nullptr;
  7193. if (error_set) {
  7194. return;
  7195. }
  7196. }
  7197. #ifdef DEBUG_ENABLED
  7198. // Warnings
  7199. for (int i = 0; i < p_class->variables.size(); i++) {
  7200. if (p_class->variables[i].usages == 0) {
  7201. _add_warning(GDScriptWarning::UNUSED_CLASS_VARIABLE, p_class->variables[i].line, p_class->variables[i].identifier);
  7202. }
  7203. }
  7204. for (int i = 0; i < p_class->_signals.size(); i++) {
  7205. if (p_class->_signals[i].emissions == 0) {
  7206. _add_warning(GDScriptWarning::UNUSED_SIGNAL, p_class->_signals[i].line, p_class->_signals[i].name);
  7207. }
  7208. }
  7209. #endif // DEBUG_ENABLED
  7210. // Inner classes
  7211. for (int i = 0; i < p_class->subclasses.size(); i++) {
  7212. current_class = p_class->subclasses[i];
  7213. _check_class_blocks_types(current_class);
  7214. if (error_set) {
  7215. return;
  7216. }
  7217. current_class = p_class;
  7218. }
  7219. }
  7220. #ifdef DEBUG_ENABLED
  7221. static String _find_function_name(const GDScriptParser::OperatorNode *p_call) {
  7222. switch (p_call->arguments[0]->type) {
  7223. case GDScriptParser::Node::TYPE_TYPE: {
  7224. return Variant::get_type_name(static_cast<GDScriptParser::TypeNode *>(p_call->arguments[0])->vtype);
  7225. } break;
  7226. case GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION: {
  7227. return GDScriptFunctions::get_func_name(static_cast<GDScriptParser::BuiltInFunctionNode *>(p_call->arguments[0])->function);
  7228. } break;
  7229. default: {
  7230. int id_index = p_call->op == GDScriptParser::OperatorNode::OP_PARENT_CALL ? 0 : 1;
  7231. if (p_call->arguments.size() > id_index && p_call->arguments[id_index]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  7232. return static_cast<GDScriptParser::IdentifierNode *>(p_call->arguments[id_index])->name;
  7233. }
  7234. } break;
  7235. }
  7236. return String();
  7237. }
  7238. #endif // DEBUG_ENABLED
  7239. void GDScriptParser::_check_block_types(BlockNode *p_block) {
  7240. Node *last_var_assign = nullptr;
  7241. // Check each statement
  7242. for (int z = 0; z < p_block->statements.size(); z++) {
  7243. Node *statement = p_block->statements[z];
  7244. switch (statement->type) {
  7245. case Node::TYPE_NEWLINE:
  7246. case Node::TYPE_BREAKPOINT: {
  7247. // Nothing to do
  7248. } break;
  7249. case Node::TYPE_ASSERT: {
  7250. AssertNode *an = static_cast<AssertNode *>(statement);
  7251. _mark_line_as_safe(an->line);
  7252. _reduce_node_type(an->condition);
  7253. _reduce_node_type(an->message);
  7254. } break;
  7255. case Node::TYPE_LOCAL_VAR: {
  7256. LocalVarNode *lv = static_cast<LocalVarNode *>(statement);
  7257. lv->datatype = _resolve_type(lv->datatype, lv->line);
  7258. _mark_line_as_safe(lv->line);
  7259. last_var_assign = lv->assign;
  7260. if (lv->assign) {
  7261. lv->assign_op->arguments[0]->set_datatype(lv->datatype);
  7262. DataType assign_type = _reduce_node_type(lv->assign);
  7263. #ifdef DEBUG_ENABLED
  7264. if (assign_type.has_type && assign_type.kind == DataType::BUILTIN && assign_type.builtin_type == Variant::NIL) {
  7265. if (lv->assign->type == Node::TYPE_OPERATOR) {
  7266. OperatorNode *call = static_cast<OperatorNode *>(lv->assign);
  7267. if (call->op == OperatorNode::OP_CALL || call->op == OperatorNode::OP_PARENT_CALL) {
  7268. _add_warning(GDScriptWarning::VOID_ASSIGNMENT, lv->line, _find_function_name(call));
  7269. }
  7270. }
  7271. }
  7272. if (lv->datatype.has_type && assign_type.may_yield && lv->assign->type == Node::TYPE_OPERATOR) {
  7273. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, lv->line, _find_function_name(static_cast<OperatorNode *>(lv->assign)));
  7274. }
  7275. for (int i = 0; i < current_class->variables.size(); i++) {
  7276. if (current_class->variables[i].identifier == lv->name) {
  7277. _add_warning(GDScriptWarning::SHADOWED_VARIABLE, lv->line, lv->name, itos(current_class->variables[i].line));
  7278. }
  7279. }
  7280. #endif // DEBUG_ENABLED
  7281. if (!_is_type_compatible(lv->datatype, assign_type)) {
  7282. // Try supertype test
  7283. if (_is_type_compatible(assign_type, lv->datatype)) {
  7284. _mark_line_as_unsafe(lv->line);
  7285. } else {
  7286. // Try implicit conversion
  7287. if (lv->datatype.kind != DataType::BUILTIN || !_is_type_compatible(lv->datatype, assign_type, true)) {
  7288. _set_error("The assigned value type (" + assign_type.to_string() + ") doesn't match the variable's type (" +
  7289. lv->datatype.to_string() + ").",
  7290. lv->line);
  7291. return;
  7292. }
  7293. // Replace assignment with implicit conversion
  7294. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  7295. convert->line = lv->line;
  7296. convert->function = GDScriptFunctions::TYPE_CONVERT;
  7297. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  7298. tgt_type->line = lv->line;
  7299. tgt_type->value = (int)lv->datatype.builtin_type;
  7300. OperatorNode *convert_call = alloc_node<OperatorNode>();
  7301. convert_call->line = lv->line;
  7302. convert_call->op = OperatorNode::OP_CALL;
  7303. convert_call->arguments.push_back(convert);
  7304. convert_call->arguments.push_back(lv->assign);
  7305. convert_call->arguments.push_back(tgt_type);
  7306. lv->assign = convert_call;
  7307. lv->assign_op->arguments.write[1] = convert_call;
  7308. #ifdef DEBUG_ENABLED
  7309. if (lv->datatype.builtin_type == Variant::INT && assign_type.builtin_type == Variant::REAL) {
  7310. _add_warning(GDScriptWarning::NARROWING_CONVERSION, lv->line);
  7311. }
  7312. #endif // DEBUG_ENABLED
  7313. }
  7314. }
  7315. if (lv->datatype.infer_type) {
  7316. if (!assign_type.has_type) {
  7317. _set_error("The assigned value doesn't have a set type; the variable type can't be inferred.", lv->line);
  7318. return;
  7319. }
  7320. if (assign_type.kind == DataType::BUILTIN && assign_type.builtin_type == Variant::NIL) {
  7321. _set_error("The variable type cannot be inferred because its value is \"null\".", lv->line);
  7322. return;
  7323. }
  7324. lv->datatype = assign_type;
  7325. lv->datatype.is_constant = false;
  7326. }
  7327. if (lv->datatype.has_type && !assign_type.has_type) {
  7328. _mark_line_as_unsafe(lv->line);
  7329. }
  7330. }
  7331. } break;
  7332. case Node::TYPE_OPERATOR: {
  7333. OperatorNode *op = static_cast<OperatorNode *>(statement);
  7334. switch (op->op) {
  7335. case OperatorNode::OP_ASSIGN:
  7336. case OperatorNode::OP_ASSIGN_ADD:
  7337. case OperatorNode::OP_ASSIGN_SUB:
  7338. case OperatorNode::OP_ASSIGN_MUL:
  7339. case OperatorNode::OP_ASSIGN_DIV:
  7340. case OperatorNode::OP_ASSIGN_MOD:
  7341. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  7342. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  7343. case OperatorNode::OP_ASSIGN_BIT_AND:
  7344. case OperatorNode::OP_ASSIGN_BIT_OR:
  7345. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  7346. if (op->arguments.size() < 2) {
  7347. _set_error("Parser bug: operation without enough arguments.", op->line, op->column);
  7348. return;
  7349. }
  7350. if (op->arguments[1] == last_var_assign) {
  7351. // Assignment was already checked
  7352. break;
  7353. }
  7354. _mark_line_as_safe(op->line);
  7355. DataType lh_type = _reduce_node_type(op->arguments[0]);
  7356. if (error_set) {
  7357. return;
  7358. }
  7359. if (check_types) {
  7360. if (!lh_type.has_type) {
  7361. if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
  7362. _mark_line_as_unsafe(op->line);
  7363. }
  7364. }
  7365. if (lh_type.is_constant) {
  7366. _set_error("Can't assign a new value to a constant.", op->line);
  7367. return;
  7368. }
  7369. }
  7370. DataType rh_type;
  7371. if (op->op != OperatorNode::OP_ASSIGN) {
  7372. // Validate operation
  7373. DataType arg_type = _reduce_node_type(op->arguments[1]);
  7374. if (!arg_type.has_type) {
  7375. _mark_line_as_unsafe(op->line);
  7376. break;
  7377. }
  7378. Variant::Operator oper = _get_variant_operation(op->op);
  7379. bool valid = false;
  7380. rh_type = _get_operation_type(oper, lh_type, arg_type, valid);
  7381. if (check_types && !valid) {
  7382. _set_error("Invalid operand types (\"" + lh_type.to_string() + "\" and \"" + arg_type.to_string() +
  7383. "\") to assignment operator \"" + Variant::get_operator_name(oper) + "\".",
  7384. op->line);
  7385. return;
  7386. }
  7387. } else {
  7388. rh_type = _reduce_node_type(op->arguments[1]);
  7389. }
  7390. #ifdef DEBUG_ENABLED
  7391. if (rh_type.has_type && rh_type.kind == DataType::BUILTIN && rh_type.builtin_type == Variant::NIL) {
  7392. if (op->arguments[1]->type == Node::TYPE_OPERATOR) {
  7393. OperatorNode *call = static_cast<OperatorNode *>(op->arguments[1]);
  7394. if (call->op == OperatorNode::OP_CALL || call->op == OperatorNode::OP_PARENT_CALL) {
  7395. _add_warning(GDScriptWarning::VOID_ASSIGNMENT, op->line, _find_function_name(call));
  7396. }
  7397. }
  7398. }
  7399. if (lh_type.has_type && rh_type.may_yield && op->arguments[1]->type == Node::TYPE_OPERATOR) {
  7400. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, op->line, _find_function_name(static_cast<OperatorNode *>(op->arguments[1])));
  7401. }
  7402. #endif // DEBUG_ENABLED
  7403. bool type_match = lh_type.has_type && rh_type.has_type;
  7404. if (check_types && !_is_type_compatible(lh_type, rh_type)) {
  7405. type_match = false;
  7406. // Try supertype test
  7407. if (_is_type_compatible(rh_type, lh_type)) {
  7408. _mark_line_as_unsafe(op->line);
  7409. } else {
  7410. // Try implicit conversion
  7411. if (lh_type.kind != DataType::BUILTIN || !_is_type_compatible(lh_type, rh_type, true)) {
  7412. _set_error("The assigned value's type (" + rh_type.to_string() + ") doesn't match the variable's type (" +
  7413. lh_type.to_string() + ").",
  7414. op->line);
  7415. return;
  7416. }
  7417. if (op->op == OperatorNode::OP_ASSIGN) {
  7418. // Replace assignment with implicit conversion
  7419. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  7420. convert->line = op->line;
  7421. convert->function = GDScriptFunctions::TYPE_CONVERT;
  7422. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  7423. tgt_type->line = op->line;
  7424. tgt_type->value = (int)lh_type.builtin_type;
  7425. OperatorNode *convert_call = alloc_node<OperatorNode>();
  7426. convert_call->line = op->line;
  7427. convert_call->op = OperatorNode::OP_CALL;
  7428. convert_call->arguments.push_back(convert);
  7429. convert_call->arguments.push_back(op->arguments[1]);
  7430. convert_call->arguments.push_back(tgt_type);
  7431. op->arguments.write[1] = convert_call;
  7432. type_match = true; // Since we are converting, the type is matching
  7433. }
  7434. #ifdef DEBUG_ENABLED
  7435. if (lh_type.builtin_type == Variant::INT && rh_type.builtin_type == Variant::REAL) {
  7436. _add_warning(GDScriptWarning::NARROWING_CONVERSION, op->line);
  7437. }
  7438. #endif // DEBUG_ENABLED
  7439. }
  7440. }
  7441. #ifdef DEBUG_ENABLED
  7442. if (!rh_type.has_type && (op->op != OperatorNode::OP_ASSIGN || lh_type.has_type || op->arguments[0]->type == Node::TYPE_OPERATOR)) {
  7443. _mark_line_as_unsafe(op->line);
  7444. }
  7445. #endif // DEBUG_ENABLED
  7446. op->datatype.has_type = type_match;
  7447. } break;
  7448. case OperatorNode::OP_CALL:
  7449. case OperatorNode::OP_PARENT_CALL: {
  7450. _mark_line_as_safe(op->line);
  7451. DataType func_type = _reduce_function_call_type(op);
  7452. #ifdef DEBUG_ENABLED
  7453. if (func_type.has_type && (func_type.kind != DataType::BUILTIN || func_type.builtin_type != Variant::NIL)) {
  7454. // Figure out function name for warning
  7455. String func_name = _find_function_name(op);
  7456. if (func_name.empty()) {
  7457. func_name = "<undetected name>";
  7458. }
  7459. _add_warning(GDScriptWarning::RETURN_VALUE_DISCARDED, op->line, func_name);
  7460. }
  7461. #endif // DEBUG_ENABLED
  7462. if (error_set) {
  7463. return;
  7464. }
  7465. } break;
  7466. case OperatorNode::OP_YIELD: {
  7467. _mark_line_as_safe(op->line);
  7468. _reduce_node_type(op);
  7469. } break;
  7470. default: {
  7471. _mark_line_as_safe(op->line);
  7472. _reduce_node_type(op); // Test for safety anyway
  7473. #ifdef DEBUG_ENABLED
  7474. if (op->op == OperatorNode::OP_TERNARY_IF) {
  7475. _add_warning(GDScriptWarning::STANDALONE_TERNARY, statement->line);
  7476. } else {
  7477. _add_warning(GDScriptWarning::STANDALONE_EXPRESSION, statement->line);
  7478. }
  7479. #endif // DEBUG_ENABLED
  7480. }
  7481. }
  7482. } break;
  7483. case Node::TYPE_CONTROL_FLOW: {
  7484. ControlFlowNode *cf = static_cast<ControlFlowNode *>(statement);
  7485. _mark_line_as_safe(cf->line);
  7486. switch (cf->cf_type) {
  7487. case ControlFlowNode::CF_RETURN: {
  7488. DataType function_type = current_function->get_datatype();
  7489. DataType ret_type;
  7490. if (cf->arguments.size() > 0) {
  7491. ret_type = _reduce_node_type(cf->arguments[0]);
  7492. if (error_set) {
  7493. return;
  7494. }
  7495. }
  7496. if (!function_type.has_type) {
  7497. break;
  7498. }
  7499. if (function_type.kind == DataType::BUILTIN && function_type.builtin_type == Variant::NIL) {
  7500. // Return void, should not have arguments
  7501. if (cf->arguments.size() > 0) {
  7502. _set_error("A void function cannot return a value.", cf->line, cf->column);
  7503. return;
  7504. }
  7505. } else {
  7506. // Return something, cannot be empty
  7507. if (cf->arguments.size() == 0) {
  7508. _set_error("A non-void function must return a value.", cf->line, cf->column);
  7509. return;
  7510. }
  7511. if (!_is_type_compatible(function_type, ret_type)) {
  7512. _set_error("The returned value type (" + ret_type.to_string() + ") doesn't match the function return type (" +
  7513. function_type.to_string() + ").",
  7514. cf->line, cf->column);
  7515. return;
  7516. }
  7517. }
  7518. } break;
  7519. case ControlFlowNode::CF_MATCH: {
  7520. MatchNode *match_node = cf->match;
  7521. _transform_match_statment(match_node);
  7522. } break;
  7523. default: {
  7524. if (cf->body_else) {
  7525. _mark_line_as_safe(cf->body_else->line);
  7526. }
  7527. for (int i = 0; i < cf->arguments.size(); i++) {
  7528. _reduce_node_type(cf->arguments[i]);
  7529. }
  7530. } break;
  7531. }
  7532. } break;
  7533. case Node::TYPE_CONSTANT: {
  7534. ConstantNode *cn = static_cast<ConstantNode *>(statement);
  7535. // Strings are fine since they can be multiline comments
  7536. if (cn->value.get_type() == Variant::STRING) {
  7537. break;
  7538. }
  7539. FALLTHROUGH;
  7540. }
  7541. default: {
  7542. _mark_line_as_safe(statement->line);
  7543. _reduce_node_type(statement); // Test for safety anyway
  7544. #ifdef DEBUG_ENABLED
  7545. _add_warning(GDScriptWarning::STANDALONE_EXPRESSION, statement->line);
  7546. #endif // DEBUG_ENABLED
  7547. }
  7548. }
  7549. }
  7550. // Parse sub blocks
  7551. for (const List<BlockNode *>::Element *E = p_block->sub_blocks.front(); E; E = E->next()) {
  7552. current_block = E->get();
  7553. _check_block_types(current_block);
  7554. current_block = p_block;
  7555. if (error_set) {
  7556. return;
  7557. }
  7558. }
  7559. #ifdef DEBUG_ENABLED
  7560. // Warnings check
  7561. for (Map<StringName, LocalVarNode *>::Element *E = p_block->variables.front(); E; E = E->next()) {
  7562. LocalVarNode *lv = E->get();
  7563. if (!lv->name.operator String().begins_with("_")) {
  7564. if (lv->usages == 0) {
  7565. _add_warning(GDScriptWarning::UNUSED_VARIABLE, lv->line, lv->name);
  7566. } else if (lv->assignments == 0) {
  7567. _add_warning(GDScriptWarning::UNASSIGNED_VARIABLE, lv->line, lv->name);
  7568. }
  7569. }
  7570. }
  7571. #endif // DEBUG_ENABLED
  7572. }
  7573. void GDScriptParser::_set_error(const String &p_error, int p_line, int p_column) {
  7574. if (error_set) {
  7575. return; //allow no further errors
  7576. }
  7577. error = p_error;
  7578. error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
  7579. error_column = p_column < 0 ? tokenizer->get_token_column() : p_column;
  7580. error_set = true;
  7581. }
  7582. #ifdef DEBUG_ENABLED
  7583. void GDScriptParser::_add_warning(int p_code, int p_line, const String &p_symbol1, const String &p_symbol2, const String &p_symbol3, const String &p_symbol4) {
  7584. Vector<String> symbols;
  7585. if (!p_symbol1.empty()) {
  7586. symbols.push_back(p_symbol1);
  7587. }
  7588. if (!p_symbol2.empty()) {
  7589. symbols.push_back(p_symbol2);
  7590. }
  7591. if (!p_symbol3.empty()) {
  7592. symbols.push_back(p_symbol3);
  7593. }
  7594. if (!p_symbol4.empty()) {
  7595. symbols.push_back(p_symbol4);
  7596. }
  7597. _add_warning(p_code, p_line, symbols);
  7598. }
  7599. void GDScriptParser::_add_warning(int p_code, int p_line, const Vector<String> &p_symbols) {
  7600. if (GLOBAL_GET("debug/gdscript/warnings/exclude_addons").booleanize() && base_path.begins_with("res://addons/")) {
  7601. return;
  7602. }
  7603. if (tokenizer->is_ignoring_warnings() || !GLOBAL_GET("debug/gdscript/warnings/enable").booleanize()) {
  7604. return;
  7605. }
  7606. String warn_name = GDScriptWarning::get_name_from_code((GDScriptWarning::Code)p_code).to_lower();
  7607. if (tokenizer->get_warning_global_skips().has(warn_name)) {
  7608. return;
  7609. }
  7610. if (!GLOBAL_GET("debug/gdscript/warnings/" + warn_name)) {
  7611. return;
  7612. }
  7613. GDScriptWarning warn;
  7614. warn.code = (GDScriptWarning::Code)p_code;
  7615. warn.symbols = p_symbols;
  7616. warn.line = p_line == -1 ? tokenizer->get_token_line() : p_line;
  7617. List<GDScriptWarning>::Element *before = nullptr;
  7618. for (List<GDScriptWarning>::Element *E = warnings.front(); E; E = E->next()) {
  7619. if (E->get().line > warn.line) {
  7620. break;
  7621. }
  7622. before = E;
  7623. }
  7624. if (before) {
  7625. warnings.insert_after(before, warn);
  7626. } else {
  7627. warnings.push_front(warn);
  7628. }
  7629. }
  7630. #endif // DEBUG_ENABLED
  7631. String GDScriptParser::get_error() const {
  7632. return error;
  7633. }
  7634. int GDScriptParser::get_error_line() const {
  7635. return error_line;
  7636. }
  7637. int GDScriptParser::get_error_column() const {
  7638. return error_column;
  7639. }
  7640. bool GDScriptParser::has_error() const {
  7641. return error_set;
  7642. }
  7643. Error GDScriptParser::_parse(const String &p_base_path) {
  7644. base_path = p_base_path;
  7645. //assume class
  7646. ClassNode *main_class = alloc_node<ClassNode>();
  7647. main_class->initializer = alloc_node<BlockNode>();
  7648. main_class->initializer->parent_class = main_class;
  7649. main_class->ready = alloc_node<BlockNode>();
  7650. main_class->ready->parent_class = main_class;
  7651. current_class = main_class;
  7652. _parse_class(main_class);
  7653. if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) {
  7654. error_set = false;
  7655. _set_error("Parse error: " + tokenizer->get_token_error());
  7656. }
  7657. bool for_completion_error_set = false;
  7658. if (error_set && for_completion) {
  7659. for_completion_error_set = true;
  7660. error_set = false;
  7661. }
  7662. if (error_set) {
  7663. return ERR_PARSE_ERROR;
  7664. }
  7665. if (dependencies_only) {
  7666. return OK;
  7667. }
  7668. _determine_inheritance(main_class);
  7669. if (error_set) {
  7670. return ERR_PARSE_ERROR;
  7671. }
  7672. current_class = main_class;
  7673. current_function = nullptr;
  7674. current_block = nullptr;
  7675. if (for_completion) {
  7676. check_types = false;
  7677. }
  7678. // Resolve all class-level stuff before getting into function blocks
  7679. _check_class_level_types(main_class);
  7680. if (error_set) {
  7681. return ERR_PARSE_ERROR;
  7682. }
  7683. // Resolve the function blocks
  7684. _check_class_blocks_types(main_class);
  7685. if (for_completion_error_set) {
  7686. error_set = true;
  7687. }
  7688. if (error_set) {
  7689. return ERR_PARSE_ERROR;
  7690. }
  7691. #ifdef DEBUG_ENABLED
  7692. // Resolve warning ignores
  7693. Vector<Pair<int, String>> warning_skips = tokenizer->get_warning_skips();
  7694. bool warning_is_error = GLOBAL_GET("debug/gdscript/warnings/treat_warnings_as_errors").booleanize();
  7695. for (List<GDScriptWarning>::Element *E = warnings.front(); E;) {
  7696. GDScriptWarning &w = E->get();
  7697. int skip_index = -1;
  7698. for (int i = 0; i < warning_skips.size(); i++) {
  7699. if (warning_skips[i].first > w.line) {
  7700. break;
  7701. }
  7702. skip_index = i;
  7703. }
  7704. List<GDScriptWarning>::Element *next = E->next();
  7705. bool erase = false;
  7706. if (skip_index != -1) {
  7707. if (warning_skips[skip_index].second == GDScriptWarning::get_name_from_code(w.code).to_lower()) {
  7708. erase = true;
  7709. }
  7710. warning_skips.remove(skip_index);
  7711. }
  7712. if (erase) {
  7713. warnings.erase(E);
  7714. } else if (warning_is_error) {
  7715. _set_error(w.get_message() + " (warning treated as error)", w.line);
  7716. return ERR_PARSE_ERROR;
  7717. }
  7718. E = next;
  7719. }
  7720. #endif // DEBUG_ENABLED
  7721. return OK;
  7722. }
  7723. Error GDScriptParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
  7724. clear();
  7725. self_path = p_self_path;
  7726. GDScriptTokenizerBuffer *tb = memnew(GDScriptTokenizerBuffer);
  7727. tb->set_code_buffer(p_bytecode);
  7728. tokenizer = tb;
  7729. Error ret = _parse(p_base_path);
  7730. memdelete(tb);
  7731. tokenizer = nullptr;
  7732. return ret;
  7733. }
  7734. Error GDScriptParser::parse(const String &p_code, const String &p_base_path, bool p_just_validate, const String &p_self_path, bool p_for_completion, Set<int> *r_safe_lines, bool p_dependencies_only) {
  7735. clear();
  7736. self_path = p_self_path;
  7737. GDScriptTokenizerText *tt = memnew(GDScriptTokenizerText);
  7738. tt->set_code(p_code);
  7739. validating = p_just_validate;
  7740. for_completion = p_for_completion;
  7741. dependencies_only = p_dependencies_only;
  7742. #ifdef DEBUG_ENABLED
  7743. safe_lines = r_safe_lines;
  7744. #endif // DEBUG_ENABLED
  7745. tokenizer = tt;
  7746. Error ret = _parse(p_base_path);
  7747. memdelete(tt);
  7748. tokenizer = nullptr;
  7749. return ret;
  7750. }
  7751. bool GDScriptParser::is_tool_script() const {
  7752. return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
  7753. }
  7754. const GDScriptParser::Node *GDScriptParser::get_parse_tree() const {
  7755. return head;
  7756. }
  7757. void GDScriptParser::clear() {
  7758. while (list) {
  7759. Node *l = list;
  7760. list = list->next;
  7761. memdelete(l);
  7762. }
  7763. head = nullptr;
  7764. list = nullptr;
  7765. completion_type = COMPLETION_NONE;
  7766. completion_node = nullptr;
  7767. completion_class = nullptr;
  7768. completion_function = nullptr;
  7769. completion_block = nullptr;
  7770. current_block = nullptr;
  7771. current_class = nullptr;
  7772. completion_found = false;
  7773. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  7774. current_function = nullptr;
  7775. validating = false;
  7776. for_completion = false;
  7777. error_set = false;
  7778. indent_level.clear();
  7779. indent_level.push_back(IndentLevel(0, 0));
  7780. error_line = 0;
  7781. error_column = 0;
  7782. pending_newline = -1;
  7783. parenthesis = 0;
  7784. current_export.type = Variant::NIL;
  7785. check_types = true;
  7786. dependencies_only = false;
  7787. dependencies.clear();
  7788. error = "";
  7789. #ifdef DEBUG_ENABLED
  7790. safe_lines = nullptr;
  7791. #endif // DEBUG_ENABLED
  7792. }
  7793. GDScriptParser::CompletionType GDScriptParser::get_completion_type() {
  7794. return completion_type;
  7795. }
  7796. StringName GDScriptParser::get_completion_cursor() {
  7797. return completion_cursor;
  7798. }
  7799. int GDScriptParser::get_completion_line() {
  7800. return completion_line;
  7801. }
  7802. Variant::Type GDScriptParser::get_completion_built_in_constant() {
  7803. return completion_built_in_constant;
  7804. }
  7805. GDScriptParser::Node *GDScriptParser::get_completion_node() {
  7806. return completion_node;
  7807. }
  7808. GDScriptParser::BlockNode *GDScriptParser::get_completion_block() {
  7809. return completion_block;
  7810. }
  7811. GDScriptParser::ClassNode *GDScriptParser::get_completion_class() {
  7812. return completion_class;
  7813. }
  7814. GDScriptParser::FunctionNode *GDScriptParser::get_completion_function() {
  7815. return completion_function;
  7816. }
  7817. int GDScriptParser::get_completion_argument_index() {
  7818. return completion_argument;
  7819. }
  7820. int GDScriptParser::get_completion_identifier_is_function() {
  7821. return completion_ident_is_call;
  7822. }
  7823. GDScriptParser::GDScriptParser() {
  7824. head = nullptr;
  7825. list = nullptr;
  7826. tokenizer = nullptr;
  7827. pending_newline = -1;
  7828. clear();
  7829. }
  7830. GDScriptParser::~GDScriptParser() {
  7831. clear();
  7832. }