gdscript_parser.cpp 272 KB

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