gdscript_parser.cpp 260 KB

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