gdscript_parser.cpp 281 KB

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