gdscript_parser.cpp 248 KB

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