gdscript_parser.cpp 249 KB

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