gdscript_parser.cpp 251 KB

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