gdscript_parser.cpp 249 KB

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