gdscript_parser.cpp 252 KB

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