gdscript_parser.cpp 251 KB

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