gdscript_parser.cpp 260 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478
  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 && cln->extends_file != StringName()) {
  681. Ref<GDScript> parent = ResourceLoader::load(cln->extends_file);
  682. if (parent.is_valid() && parent->is_valid()) {
  683. Map<StringName, Variant> parent_constants;
  684. parent->get_constants(&parent_constants);
  685. if (parent_constants.has(identifier)) {
  686. ConstantNode *constant = alloc_node<ConstantNode>();
  687. constant->value = parent_constants[identifier];
  688. expr = constant;
  689. bfn = true;
  690. }
  691. }
  692. }
  693. }
  694. }
  695. if (!bfn) {
  696. #ifdef DEBUG_ENABLED
  697. if (current_function) {
  698. int arg_idx = current_function->arguments.find(identifier);
  699. if (arg_idx != -1) {
  700. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  701. // Assignment is not really usage
  702. current_function->arguments_usage.write[arg_idx] = current_function->arguments_usage[arg_idx] - 1;
  703. } else {
  704. current_function->arguments_usage.write[arg_idx] = current_function->arguments_usage[arg_idx] + 1;
  705. }
  706. }
  707. }
  708. #endif // DEBUG_ENABLED
  709. IdentifierNode *id = alloc_node<IdentifierNode>();
  710. id->name = identifier;
  711. id->line = id_line;
  712. expr = id;
  713. }
  714. } 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) {
  715. //single prefix operators like !expr +expr -expr ++expr --expr
  716. alloc_node<OperatorNode>();
  717. Expression e;
  718. e.is_op = true;
  719. switch (tokenizer->get_token()) {
  720. case GDScriptTokenizer::TK_OP_ADD: e.op = OperatorNode::OP_POS; break;
  721. case GDScriptTokenizer::TK_OP_SUB: e.op = OperatorNode::OP_NEG; break;
  722. case GDScriptTokenizer::TK_OP_NOT: e.op = OperatorNode::OP_NOT; break;
  723. case GDScriptTokenizer::TK_OP_BIT_INVERT: e.op = OperatorNode::OP_BIT_INVERT; break;
  724. default: {
  725. }
  726. }
  727. tokenizer->advance();
  728. if (e.op != OperatorNode::OP_NOT && tokenizer->get_token() == GDScriptTokenizer::TK_OP_NOT) {
  729. _set_error("Misplaced 'not'.");
  730. return NULL;
  731. }
  732. expression.push_back(e);
  733. continue; //only exception, must continue...
  734. /*
  735. Node *subexpr=_parse_expression(op,p_static);
  736. if (!subexpr)
  737. return NULL;
  738. op->arguments.push_back(subexpr);
  739. expr=op;*/
  740. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_IS && tokenizer->get_token(1) == GDScriptTokenizer::TK_BUILT_IN_TYPE) {
  741. // 'is' operator with built-in type
  742. OperatorNode *op = alloc_node<OperatorNode>();
  743. op->op = OperatorNode::OP_IS_BUILTIN;
  744. op->arguments.push_back(expr);
  745. tokenizer->advance();
  746. TypeNode *tn = alloc_node<TypeNode>();
  747. tn->vtype = tokenizer->get_token_type();
  748. op->arguments.push_back(tn);
  749. tokenizer->advance();
  750. expr = op;
  751. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_OPEN) {
  752. // array
  753. tokenizer->advance();
  754. ArrayNode *arr = alloc_node<ArrayNode>();
  755. bool expecting_comma = false;
  756. while (true) {
  757. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  758. _set_error("Unterminated array");
  759. return NULL;
  760. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  761. tokenizer->advance();
  762. break;
  763. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  764. tokenizer->advance(); //ignore newline
  765. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  766. if (!expecting_comma) {
  767. _set_error("expression or ']' expected");
  768. return NULL;
  769. }
  770. expecting_comma = false;
  771. tokenizer->advance(); //ignore newline
  772. } else {
  773. //parse expression
  774. if (expecting_comma) {
  775. _set_error("',' or ']' expected");
  776. return NULL;
  777. }
  778. Node *n = _parse_expression(arr, p_static, p_allow_assign, p_parsing_constant);
  779. if (!n)
  780. return NULL;
  781. arr->elements.push_back(n);
  782. expecting_comma = true;
  783. }
  784. }
  785. expr = arr;
  786. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_OPEN) {
  787. // array
  788. tokenizer->advance();
  789. DictionaryNode *dict = alloc_node<DictionaryNode>();
  790. enum DictExpect {
  791. DICT_EXPECT_KEY,
  792. DICT_EXPECT_COLON,
  793. DICT_EXPECT_VALUE,
  794. DICT_EXPECT_COMMA
  795. };
  796. Node *key = NULL;
  797. Set<Variant> keys;
  798. DictExpect expecting = DICT_EXPECT_KEY;
  799. while (true) {
  800. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  801. _set_error("Unterminated dictionary");
  802. return NULL;
  803. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  804. if (expecting == DICT_EXPECT_COLON) {
  805. _set_error("':' expected");
  806. return NULL;
  807. }
  808. if (expecting == DICT_EXPECT_VALUE) {
  809. _set_error("value expected");
  810. return NULL;
  811. }
  812. tokenizer->advance();
  813. break;
  814. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  815. tokenizer->advance(); //ignore newline
  816. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  817. if (expecting == DICT_EXPECT_KEY) {
  818. _set_error("key or '}' expected");
  819. return NULL;
  820. }
  821. if (expecting == DICT_EXPECT_VALUE) {
  822. _set_error("value expected");
  823. return NULL;
  824. }
  825. if (expecting == DICT_EXPECT_COLON) {
  826. _set_error("':' expected");
  827. return NULL;
  828. }
  829. expecting = DICT_EXPECT_KEY;
  830. tokenizer->advance(); //ignore newline
  831. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  832. if (expecting == DICT_EXPECT_KEY) {
  833. _set_error("key or '}' expected");
  834. return NULL;
  835. }
  836. if (expecting == DICT_EXPECT_VALUE) {
  837. _set_error("value expected");
  838. return NULL;
  839. }
  840. if (expecting == DICT_EXPECT_COMMA) {
  841. _set_error("',' or '}' expected");
  842. return NULL;
  843. }
  844. expecting = DICT_EXPECT_VALUE;
  845. tokenizer->advance(); //ignore newline
  846. } else {
  847. if (expecting == DICT_EXPECT_COMMA) {
  848. _set_error("',' or '}' expected");
  849. return NULL;
  850. }
  851. if (expecting == DICT_EXPECT_COLON) {
  852. _set_error("':' expected");
  853. return NULL;
  854. }
  855. if (expecting == DICT_EXPECT_KEY) {
  856. if (tokenizer->is_token_literal() && tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  857. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  858. //lua style identifier, easier to write
  859. ConstantNode *cn = alloc_node<ConstantNode>();
  860. cn->value = tokenizer->get_token_literal();
  861. cn->datatype = _type_from_variant(cn->value);
  862. key = cn;
  863. tokenizer->advance(2);
  864. expecting = DICT_EXPECT_VALUE;
  865. } else {
  866. //python/js style more flexible
  867. key = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
  868. if (!key)
  869. return NULL;
  870. expecting = DICT_EXPECT_COLON;
  871. }
  872. }
  873. if (expecting == DICT_EXPECT_VALUE) {
  874. Node *value = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
  875. if (!value)
  876. return NULL;
  877. expecting = DICT_EXPECT_COMMA;
  878. if (key->type == GDScriptParser::Node::TYPE_CONSTANT) {
  879. Variant const &keyName = static_cast<const GDScriptParser::ConstantNode *>(key)->value;
  880. if (keys.has(keyName)) {
  881. _set_error("Duplicate key found in Dictionary literal");
  882. return NULL;
  883. }
  884. keys.insert(keyName);
  885. }
  886. DictionaryNode::Pair pair;
  887. pair.key = key;
  888. pair.value = value;
  889. dict->elements.push_back(pair);
  890. key = NULL;
  891. }
  892. }
  893. }
  894. expr = dict;
  895. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD && (tokenizer->is_token_literal(1) || tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR)) {
  896. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  897. // parent call
  898. tokenizer->advance(); //goto identifier
  899. OperatorNode *op = alloc_node<OperatorNode>();
  900. op->op = OperatorNode::OP_PARENT_CALL;
  901. /*SelfNode *self = alloc_node<SelfNode>();
  902. op->arguments.push_back(self);
  903. forbidden for now */
  904. StringName identifier;
  905. bool is_completion = _get_completable_identifier(COMPLETION_PARENT_FUNCTION, identifier) && for_completion;
  906. IdentifierNode *id = alloc_node<IdentifierNode>();
  907. id->name = identifier;
  908. op->arguments.push_back(id);
  909. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  910. if (!is_completion) {
  911. _set_error("Expected '(' for parent function call.");
  912. return NULL;
  913. }
  914. } else {
  915. tokenizer->advance();
  916. if (!_parse_arguments(op, op->arguments, p_static, false, p_parsing_constant)) {
  917. return NULL;
  918. }
  919. }
  920. expr = op;
  921. } 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) {
  922. Expression e = expression[expression.size() - 1];
  923. e.op = OperatorNode::OP_IS_BUILTIN;
  924. expression.write[expression.size() - 1] = e;
  925. TypeNode *tn = alloc_node<TypeNode>();
  926. tn->vtype = tokenizer->get_token_type();
  927. expr = tn;
  928. tokenizer->advance();
  929. } else {
  930. //find list [ or find dictionary {
  931. _set_error("Error parsing expression, misplaced: " + String(tokenizer->get_token_name(tokenizer->get_token())));
  932. return NULL; //nothing
  933. }
  934. if (!expr) {
  935. ERR_EXPLAIN("GDScriptParser bug, couldn't figure out what expression is...");
  936. ERR_FAIL_V(NULL);
  937. }
  938. /******************/
  939. /* Parse Indexing */
  940. /******************/
  941. while (true) {
  942. //expressions can be indexed any number of times
  943. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  944. //indexing using "."
  945. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_CURSOR && !tokenizer->is_token_literal(1)) {
  946. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  947. _set_error("Expected identifier as member");
  948. return NULL;
  949. } else if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  950. //call!!
  951. OperatorNode *op = alloc_node<OperatorNode>();
  952. op->op = OperatorNode::OP_CALL;
  953. tokenizer->advance();
  954. IdentifierNode *id = alloc_node<IdentifierNode>();
  955. StringName identifier;
  956. if (_get_completable_identifier(COMPLETION_METHOD, identifier)) {
  957. completion_node = op;
  958. //indexing stuff
  959. }
  960. id->name = identifier;
  961. op->arguments.push_back(expr); // call what
  962. op->arguments.push_back(id); // call func
  963. //get arguments
  964. tokenizer->advance(1);
  965. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  966. _make_completable_call(0);
  967. completion_node = op;
  968. }
  969. if (!_parse_arguments(op, op->arguments, p_static, true, p_parsing_constant))
  970. return NULL;
  971. expr = op;
  972. } else {
  973. //simple indexing!
  974. OperatorNode *op = alloc_node<OperatorNode>();
  975. op->op = OperatorNode::OP_INDEX_NAMED;
  976. tokenizer->advance();
  977. StringName identifier;
  978. if (_get_completable_identifier(COMPLETION_INDEX, identifier)) {
  979. if (identifier == StringName()) {
  980. identifier = "@temp"; //so it parses allright
  981. }
  982. completion_node = op;
  983. //indexing stuff
  984. }
  985. IdentifierNode *id = alloc_node<IdentifierNode>();
  986. id->name = identifier;
  987. op->arguments.push_back(expr);
  988. op->arguments.push_back(id);
  989. expr = op;
  990. }
  991. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_OPEN) {
  992. //indexing using "[]"
  993. OperatorNode *op = alloc_node<OperatorNode>();
  994. op->op = OperatorNode::OP_INDEX;
  995. tokenizer->advance(1);
  996. Node *subexpr = _parse_expression(op, p_static, p_allow_assign, p_parsing_constant);
  997. if (!subexpr) {
  998. return NULL;
  999. }
  1000. if (tokenizer->get_token() != GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1001. _set_error("Expected ']'");
  1002. return NULL;
  1003. }
  1004. op->arguments.push_back(expr);
  1005. op->arguments.push_back(subexpr);
  1006. tokenizer->advance(1);
  1007. expr = op;
  1008. } else
  1009. break;
  1010. }
  1011. /*****************/
  1012. /* Parse Casting */
  1013. /*****************/
  1014. bool has_casting = expr->type == Node::TYPE_CAST;
  1015. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_AS) {
  1016. if (has_casting) {
  1017. _set_error("Unexpected 'as'.");
  1018. return NULL;
  1019. }
  1020. CastNode *cn = alloc_node<CastNode>();
  1021. if (!_parse_type(cn->cast_type)) {
  1022. _set_error("Expected type after 'as'.");
  1023. return NULL;
  1024. }
  1025. has_casting = true;
  1026. cn->source_node = expr;
  1027. expr = cn;
  1028. }
  1029. /******************/
  1030. /* Parse Operator */
  1031. /******************/
  1032. if (parenthesis > 0) {
  1033. //remove empty space (only allowed if inside parenthesis
  1034. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  1035. tokenizer->advance();
  1036. }
  1037. }
  1038. Expression e;
  1039. e.is_op = false;
  1040. e.node = expr;
  1041. expression.push_back(e);
  1042. // determine which operator is next
  1043. OperatorNode::Operator op;
  1044. bool valid = true;
  1045. //assign, if allowed is only allowed on the first operator
  1046. #define _VALIDATE_ASSIGN \
  1047. if (!p_allow_assign || has_casting) { \
  1048. _set_error("Unexpected assign."); \
  1049. return NULL; \
  1050. } \
  1051. p_allow_assign = false;
  1052. switch (tokenizer->get_token()) { //see operator
  1053. case GDScriptTokenizer::TK_OP_IN: op = OperatorNode::OP_IN; break;
  1054. case GDScriptTokenizer::TK_OP_EQUAL: op = OperatorNode::OP_EQUAL; break;
  1055. case GDScriptTokenizer::TK_OP_NOT_EQUAL: op = OperatorNode::OP_NOT_EQUAL; break;
  1056. case GDScriptTokenizer::TK_OP_LESS: op = OperatorNode::OP_LESS; break;
  1057. case GDScriptTokenizer::TK_OP_LESS_EQUAL: op = OperatorNode::OP_LESS_EQUAL; break;
  1058. case GDScriptTokenizer::TK_OP_GREATER: op = OperatorNode::OP_GREATER; break;
  1059. case GDScriptTokenizer::TK_OP_GREATER_EQUAL: op = OperatorNode::OP_GREATER_EQUAL; break;
  1060. case GDScriptTokenizer::TK_OP_AND: op = OperatorNode::OP_AND; break;
  1061. case GDScriptTokenizer::TK_OP_OR: op = OperatorNode::OP_OR; break;
  1062. case GDScriptTokenizer::TK_OP_ADD: op = OperatorNode::OP_ADD; break;
  1063. case GDScriptTokenizer::TK_OP_SUB: op = OperatorNode::OP_SUB; break;
  1064. case GDScriptTokenizer::TK_OP_MUL: op = OperatorNode::OP_MUL; break;
  1065. case GDScriptTokenizer::TK_OP_DIV: op = OperatorNode::OP_DIV; break;
  1066. case GDScriptTokenizer::TK_OP_MOD:
  1067. op = OperatorNode::OP_MOD;
  1068. break;
  1069. //case GDScriptTokenizer::TK_OP_NEG: op=OperatorNode::OP_NEG ; break;
  1070. case GDScriptTokenizer::TK_OP_SHIFT_LEFT: op = OperatorNode::OP_SHIFT_LEFT; break;
  1071. case GDScriptTokenizer::TK_OP_SHIFT_RIGHT: op = OperatorNode::OP_SHIFT_RIGHT; break;
  1072. case GDScriptTokenizer::TK_OP_ASSIGN: {
  1073. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN;
  1074. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR) {
  1075. //code complete assignment
  1076. completion_type = COMPLETION_ASSIGN;
  1077. completion_node = expr;
  1078. completion_class = current_class;
  1079. completion_function = current_function;
  1080. completion_line = tokenizer->get_token_line();
  1081. completion_block = current_block;
  1082. completion_found = true;
  1083. tokenizer->advance();
  1084. }
  1085. } break;
  1086. case GDScriptTokenizer::TK_OP_ASSIGN_ADD: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_ADD; break;
  1087. case GDScriptTokenizer::TK_OP_ASSIGN_SUB: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SUB; break;
  1088. case GDScriptTokenizer::TK_OP_ASSIGN_MUL: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MUL; break;
  1089. case GDScriptTokenizer::TK_OP_ASSIGN_DIV: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_DIV; break;
  1090. case GDScriptTokenizer::TK_OP_ASSIGN_MOD: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MOD; break;
  1091. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_LEFT: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_LEFT; break;
  1092. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_RIGHT; break;
  1093. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_AND: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_AND; break;
  1094. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_OR: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_OR; break;
  1095. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_XOR: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_XOR; break;
  1096. case GDScriptTokenizer::TK_OP_BIT_AND: op = OperatorNode::OP_BIT_AND; break;
  1097. case GDScriptTokenizer::TK_OP_BIT_OR: op = OperatorNode::OP_BIT_OR; break;
  1098. case GDScriptTokenizer::TK_OP_BIT_XOR: op = OperatorNode::OP_BIT_XOR; break;
  1099. case GDScriptTokenizer::TK_PR_IS: op = OperatorNode::OP_IS; break;
  1100. case GDScriptTokenizer::TK_CF_IF: op = OperatorNode::OP_TERNARY_IF; break;
  1101. case GDScriptTokenizer::TK_CF_ELSE: op = OperatorNode::OP_TERNARY_ELSE; break;
  1102. default: valid = false; break;
  1103. }
  1104. if (valid) {
  1105. e.is_op = true;
  1106. e.op = op;
  1107. expression.push_back(e);
  1108. tokenizer->advance();
  1109. } else {
  1110. break;
  1111. }
  1112. }
  1113. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  1114. while (expression.size() > 1) {
  1115. int next_op = -1;
  1116. int min_priority = 0xFFFFF;
  1117. bool is_unary = false;
  1118. bool is_ternary = false;
  1119. for (int i = 0; i < expression.size(); i++) {
  1120. if (!expression[i].is_op) {
  1121. continue;
  1122. }
  1123. int priority;
  1124. bool unary = false;
  1125. bool ternary = false;
  1126. bool error = false;
  1127. bool right_to_left = false;
  1128. switch (expression[i].op) {
  1129. case OperatorNode::OP_IS:
  1130. case OperatorNode::OP_IS_BUILTIN:
  1131. priority = -1;
  1132. break; //before anything
  1133. case OperatorNode::OP_BIT_INVERT:
  1134. priority = 0;
  1135. unary = true;
  1136. break;
  1137. case OperatorNode::OP_NEG:
  1138. priority = 1;
  1139. unary = true;
  1140. break;
  1141. case OperatorNode::OP_POS:
  1142. priority = 1;
  1143. unary = true;
  1144. break;
  1145. case OperatorNode::OP_MUL: priority = 2; break;
  1146. case OperatorNode::OP_DIV: priority = 2; break;
  1147. case OperatorNode::OP_MOD: priority = 2; break;
  1148. case OperatorNode::OP_ADD: priority = 3; break;
  1149. case OperatorNode::OP_SUB: priority = 3; break;
  1150. case OperatorNode::OP_SHIFT_LEFT: priority = 4; break;
  1151. case OperatorNode::OP_SHIFT_RIGHT: priority = 4; break;
  1152. case OperatorNode::OP_BIT_AND: priority = 5; break;
  1153. case OperatorNode::OP_BIT_XOR: priority = 6; break;
  1154. case OperatorNode::OP_BIT_OR: priority = 7; break;
  1155. case OperatorNode::OP_LESS: priority = 8; break;
  1156. case OperatorNode::OP_LESS_EQUAL: priority = 8; break;
  1157. case OperatorNode::OP_GREATER: priority = 8; break;
  1158. case OperatorNode::OP_GREATER_EQUAL: priority = 8; break;
  1159. case OperatorNode::OP_EQUAL: priority = 8; break;
  1160. case OperatorNode::OP_NOT_EQUAL: priority = 8; break;
  1161. case OperatorNode::OP_IN: priority = 10; break;
  1162. case OperatorNode::OP_NOT:
  1163. priority = 11;
  1164. unary = true;
  1165. break;
  1166. case OperatorNode::OP_AND: priority = 12; break;
  1167. case OperatorNode::OP_OR: priority = 13; break;
  1168. case OperatorNode::OP_TERNARY_IF:
  1169. priority = 14;
  1170. ternary = true;
  1171. right_to_left = true;
  1172. break;
  1173. case OperatorNode::OP_TERNARY_ELSE:
  1174. priority = 14;
  1175. error = true;
  1176. // Rigth-to-left should be false in this case, otherwise it would always error.
  1177. break;
  1178. case OperatorNode::OP_ASSIGN: priority = 15; break;
  1179. case OperatorNode::OP_ASSIGN_ADD: priority = 15; break;
  1180. case OperatorNode::OP_ASSIGN_SUB: priority = 15; break;
  1181. case OperatorNode::OP_ASSIGN_MUL: priority = 15; break;
  1182. case OperatorNode::OP_ASSIGN_DIV: priority = 15; break;
  1183. case OperatorNode::OP_ASSIGN_MOD: priority = 15; break;
  1184. case OperatorNode::OP_ASSIGN_SHIFT_LEFT: priority = 15; break;
  1185. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT: priority = 15; break;
  1186. case OperatorNode::OP_ASSIGN_BIT_AND: priority = 15; break;
  1187. case OperatorNode::OP_ASSIGN_BIT_OR: priority = 15; break;
  1188. case OperatorNode::OP_ASSIGN_BIT_XOR: priority = 15; break;
  1189. default: {
  1190. _set_error("GDScriptParser bug, invalid operator in expression: " + itos(expression[i].op));
  1191. return NULL;
  1192. }
  1193. }
  1194. if (priority < min_priority || (right_to_left && priority == min_priority)) {
  1195. // < is used for left to right (default)
  1196. // <= is used for right to left
  1197. if (error) {
  1198. _set_error("Unexpected operator");
  1199. return NULL;
  1200. }
  1201. next_op = i;
  1202. min_priority = priority;
  1203. is_unary = unary;
  1204. is_ternary = ternary;
  1205. }
  1206. }
  1207. if (next_op == -1) {
  1208. _set_error("Yet another parser bug....");
  1209. ERR_FAIL_V(NULL);
  1210. }
  1211. // OK! create operator..
  1212. if (is_unary) {
  1213. int expr_pos = next_op;
  1214. while (expression[expr_pos].is_op) {
  1215. expr_pos++;
  1216. if (expr_pos == expression.size()) {
  1217. //can happen..
  1218. _set_error("Unexpected end of expression...");
  1219. return NULL;
  1220. }
  1221. }
  1222. //consecutively do unary opeators
  1223. for (int i = expr_pos - 1; i >= next_op; i--) {
  1224. OperatorNode *op = alloc_node<OperatorNode>();
  1225. op->op = expression[i].op;
  1226. op->arguments.push_back(expression[i + 1].node);
  1227. op->line = op_line; //line might have been changed from a \n
  1228. expression.write[i].is_op = false;
  1229. expression.write[i].node = op;
  1230. expression.remove(i + 1);
  1231. }
  1232. } else if (is_ternary) {
  1233. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1234. _set_error("Parser bug...");
  1235. ERR_FAIL_V(NULL);
  1236. }
  1237. if (next_op >= (expression.size() - 2) || expression[next_op + 2].op != OperatorNode::OP_TERNARY_ELSE) {
  1238. _set_error("Expected else after ternary if.");
  1239. return NULL;
  1240. }
  1241. if (next_op >= (expression.size() - 3)) {
  1242. _set_error("Expected value after ternary else.");
  1243. return NULL;
  1244. }
  1245. OperatorNode *op = alloc_node<OperatorNode>();
  1246. op->op = expression[next_op].op;
  1247. op->line = op_line; //line might have been changed from a \n
  1248. if (expression[next_op - 1].is_op) {
  1249. _set_error("Parser bug...");
  1250. ERR_FAIL_V(NULL);
  1251. }
  1252. if (expression[next_op + 1].is_op) {
  1253. // this is not invalid and can really appear
  1254. // but it becomes invalid anyway because no binary op
  1255. // can be followed by a unary op in a valid combination,
  1256. // due to how precedence works, unaries will always disappear first
  1257. _set_error("Unexpected two consecutive operators after ternary if.");
  1258. return NULL;
  1259. }
  1260. if (expression[next_op + 3].is_op) {
  1261. // this is not invalid and can really appear
  1262. // but it becomes invalid anyway because no binary op
  1263. // can be followed by a unary op in a valid combination,
  1264. // due to how precedence works, unaries will always disappear first
  1265. _set_error("Unexpected two consecutive operators after ternary else.");
  1266. return NULL;
  1267. }
  1268. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as first
  1269. op->arguments.push_back(expression[next_op - 1].node); //left expression goes as when-true
  1270. op->arguments.push_back(expression[next_op + 3].node); //expression after next goes as when-false
  1271. //replace all 3 nodes by this operator and make it an expression
  1272. expression.write[next_op - 1].node = op;
  1273. expression.remove(next_op);
  1274. expression.remove(next_op);
  1275. expression.remove(next_op);
  1276. expression.remove(next_op);
  1277. } else {
  1278. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1279. _set_error("Parser bug...");
  1280. ERR_FAIL_V(NULL);
  1281. }
  1282. OperatorNode *op = alloc_node<OperatorNode>();
  1283. op->op = expression[next_op].op;
  1284. op->line = op_line; //line might have been changed from a \n
  1285. if (expression[next_op - 1].is_op) {
  1286. _set_error("Parser bug...");
  1287. ERR_FAIL_V(NULL);
  1288. }
  1289. if (expression[next_op + 1].is_op) {
  1290. // this is not invalid and can really appear
  1291. // but it becomes invalid anyway because no binary op
  1292. // can be followed by a unary op in a valid combination,
  1293. // due to how precedence works, unaries will always disappear first
  1294. _set_error("Unexpected two consecutive operators.");
  1295. return NULL;
  1296. }
  1297. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  1298. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  1299. //replace all 3 nodes by this operator and make it an expression
  1300. expression.write[next_op - 1].node = op;
  1301. expression.remove(next_op);
  1302. expression.remove(next_op);
  1303. }
  1304. }
  1305. return expression[0].node;
  1306. }
  1307. GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to_const) {
  1308. switch (p_node->type) {
  1309. case Node::TYPE_BUILT_IN_FUNCTION: {
  1310. //many may probably be optimizable
  1311. return p_node;
  1312. } break;
  1313. case Node::TYPE_ARRAY: {
  1314. ArrayNode *an = static_cast<ArrayNode *>(p_node);
  1315. bool all_constants = true;
  1316. for (int i = 0; i < an->elements.size(); i++) {
  1317. an->elements.write[i] = _reduce_expression(an->elements[i], p_to_const);
  1318. if (an->elements[i]->type != Node::TYPE_CONSTANT)
  1319. all_constants = false;
  1320. }
  1321. if (all_constants && p_to_const) {
  1322. //reduce constant array expression
  1323. ConstantNode *cn = alloc_node<ConstantNode>();
  1324. Array arr;
  1325. arr.resize(an->elements.size());
  1326. for (int i = 0; i < an->elements.size(); i++) {
  1327. ConstantNode *acn = static_cast<ConstantNode *>(an->elements[i]);
  1328. arr[i] = acn->value;
  1329. }
  1330. cn->value = arr;
  1331. cn->datatype = _type_from_variant(cn->value);
  1332. return cn;
  1333. }
  1334. return an;
  1335. } break;
  1336. case Node::TYPE_DICTIONARY: {
  1337. DictionaryNode *dn = static_cast<DictionaryNode *>(p_node);
  1338. bool all_constants = true;
  1339. for (int i = 0; i < dn->elements.size(); i++) {
  1340. dn->elements.write[i].key = _reduce_expression(dn->elements[i].key, p_to_const);
  1341. if (dn->elements[i].key->type != Node::TYPE_CONSTANT)
  1342. all_constants = false;
  1343. dn->elements.write[i].value = _reduce_expression(dn->elements[i].value, p_to_const);
  1344. if (dn->elements[i].value->type != Node::TYPE_CONSTANT)
  1345. all_constants = false;
  1346. }
  1347. if (all_constants && p_to_const) {
  1348. //reduce constant array expression
  1349. ConstantNode *cn = alloc_node<ConstantNode>();
  1350. Dictionary dict;
  1351. for (int i = 0; i < dn->elements.size(); i++) {
  1352. ConstantNode *key_c = static_cast<ConstantNode *>(dn->elements[i].key);
  1353. ConstantNode *value_c = static_cast<ConstantNode *>(dn->elements[i].value);
  1354. dict[key_c->value] = value_c->value;
  1355. }
  1356. cn->value = dict;
  1357. cn->datatype = _type_from_variant(cn->value);
  1358. return cn;
  1359. }
  1360. return dn;
  1361. } break;
  1362. case Node::TYPE_OPERATOR: {
  1363. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  1364. bool all_constants = true;
  1365. int last_not_constant = -1;
  1366. for (int i = 0; i < op->arguments.size(); i++) {
  1367. op->arguments.write[i] = _reduce_expression(op->arguments[i], p_to_const);
  1368. if (op->arguments[i]->type != Node::TYPE_CONSTANT) {
  1369. all_constants = false;
  1370. last_not_constant = i;
  1371. }
  1372. }
  1373. if (op->op == OperatorNode::OP_IS) {
  1374. //nothing much
  1375. return op;
  1376. }
  1377. if (op->op == OperatorNode::OP_PARENT_CALL) {
  1378. //nothing much
  1379. return op;
  1380. } else if (op->op == OperatorNode::OP_CALL) {
  1381. //can reduce base type constructors
  1382. 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) {
  1383. //native type constructor or intrinsic function
  1384. const Variant **vptr = NULL;
  1385. Vector<Variant *> ptrs;
  1386. if (op->arguments.size() > 1) {
  1387. ptrs.resize(op->arguments.size() - 1);
  1388. for (int i = 0; i < ptrs.size(); i++) {
  1389. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i + 1]);
  1390. ptrs.write[i] = &cn->value;
  1391. }
  1392. vptr = (const Variant **)&ptrs[0];
  1393. }
  1394. Variant::CallError ce;
  1395. Variant v;
  1396. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1397. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1398. v = Variant::construct(tn->vtype, vptr, ptrs.size(), ce);
  1399. } else {
  1400. GDScriptFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1401. GDScriptFunctions::call(func, vptr, ptrs.size(), v, ce);
  1402. }
  1403. if (ce.error != Variant::CallError::CALL_OK) {
  1404. String errwhere;
  1405. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1406. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1407. errwhere = "'" + Variant::get_type_name(tn->vtype) + "' constructor";
  1408. } else {
  1409. GDScriptFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1410. errwhere = String("'") + GDScriptFunctions::get_func_name(func) + "' intrinsic function";
  1411. }
  1412. switch (ce.error) {
  1413. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1414. _set_error("Invalid argument (#" + itos(ce.argument + 1) + ") for " + errwhere + ".");
  1415. } break;
  1416. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1417. _set_error("Too many arguments for " + errwhere + ".");
  1418. } break;
  1419. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1420. _set_error("Too few arguments for " + errwhere + ".");
  1421. } break;
  1422. default: {
  1423. _set_error("Invalid arguments for " + errwhere + ".");
  1424. } break;
  1425. }
  1426. error_line = op->line;
  1427. return p_node;
  1428. }
  1429. ConstantNode *cn = alloc_node<ConstantNode>();
  1430. cn->value = v;
  1431. cn->datatype = _type_from_variant(v);
  1432. return cn;
  1433. } else if (op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && last_not_constant == 0) {
  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. }
  4098. if (enum_name != "") {
  4099. enum_dict[const_id] = enum_value_expr->value;
  4100. } else {
  4101. if (current_class->constant_expressions.has(const_id)) {
  4102. _set_error("A constant named '" + String(const_id) + "' already exists in this class (at line: " +
  4103. itos(current_class->constant_expressions[const_id].expression->line) + ").");
  4104. return;
  4105. }
  4106. for (int i = 0; i < current_class->variables.size(); i++) {
  4107. if (current_class->variables[i].identifier == const_id) {
  4108. _set_error("A variable named '" + String(const_id) + "' already exists in this class (at line: " +
  4109. itos(current_class->variables[i].line) + ").");
  4110. return;
  4111. }
  4112. }
  4113. for (int i = 0; i < current_class->subclasses.size(); i++) {
  4114. if (current_class->subclasses[i]->name == const_id) {
  4115. _set_error("A class named '" + String(const_id) + "' already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ").");
  4116. return;
  4117. }
  4118. }
  4119. ClassNode::Constant constant;
  4120. constant.type.has_type = true;
  4121. constant.type.kind = DataType::BUILTIN;
  4122. constant.type.builtin_type = Variant::INT;
  4123. constant.expression = enum_value_expr;
  4124. p_class->constant_expressions.insert(const_id, constant);
  4125. }
  4126. }
  4127. }
  4128. if (enum_name != "") {
  4129. ClassNode::Constant enum_constant;
  4130. ConstantNode *cn = alloc_node<ConstantNode>();
  4131. cn->value = enum_dict;
  4132. cn->datatype = _type_from_variant(cn->value);
  4133. enum_constant.expression = cn;
  4134. enum_constant.type = cn->datatype;
  4135. p_class->constant_expressions.insert(enum_name, enum_constant);
  4136. }
  4137. if (!_end_statement()) {
  4138. _set_error("Expected end of statement (enum)");
  4139. return;
  4140. }
  4141. } break;
  4142. case GDScriptTokenizer::TK_CONSTANT: {
  4143. if (tokenizer->get_token_constant().get_type() == Variant::STRING) {
  4144. tokenizer->advance();
  4145. // Ignore
  4146. } else {
  4147. _set_error(String() + "Unexpected constant of type: " + Variant::get_type_name(tokenizer->get_token_constant().get_type()));
  4148. return;
  4149. }
  4150. } break;
  4151. default: {
  4152. _set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
  4153. return;
  4154. } break;
  4155. }
  4156. }
  4157. }
  4158. void GDScriptParser::_determine_inheritance(ClassNode *p_class) {
  4159. if (p_class->extends_used) {
  4160. //do inheritance
  4161. String path = p_class->extends_file;
  4162. Ref<GDScript> script;
  4163. StringName native;
  4164. ClassNode *base_class = NULL;
  4165. if (path != "") {
  4166. //path (and optionally subclasses)
  4167. if (path.is_rel_path()) {
  4168. String base = base_path;
  4169. if (base == "" || base.is_rel_path()) {
  4170. _set_error("Could not resolve relative path for parent class: " + path, p_class->line);
  4171. return;
  4172. }
  4173. path = base.plus_file(path).simplify_path();
  4174. }
  4175. script = ResourceLoader::load(path);
  4176. if (script.is_null()) {
  4177. _set_error("Could not load base class: " + path, p_class->line);
  4178. return;
  4179. }
  4180. if (!script->is_valid()) {
  4181. _set_error("Script not fully loaded (cyclic preload?): " + path, p_class->line);
  4182. return;
  4183. }
  4184. if (p_class->extends_class.size()) {
  4185. for (int i = 0; i < p_class->extends_class.size(); i++) {
  4186. String sub = p_class->extends_class[i];
  4187. if (script->get_subclasses().has(sub)) {
  4188. Ref<Script> subclass = script->get_subclasses()[sub]; //avoid reference from disappearing
  4189. script = subclass;
  4190. } else {
  4191. _set_error("Could not find subclass: " + sub, p_class->line);
  4192. return;
  4193. }
  4194. }
  4195. }
  4196. } else {
  4197. if (p_class->extends_class.size() == 0) {
  4198. _set_error("Parser bug: undecidable inheritance.", p_class->line);
  4199. ERR_FAIL();
  4200. }
  4201. //look around for the subclasses
  4202. int extend_iter = 1;
  4203. String base = p_class->extends_class[0];
  4204. ClassNode *p = p_class->owner;
  4205. Ref<GDScript> base_script;
  4206. if (ScriptServer::is_global_class(base)) {
  4207. base_script = ResourceLoader::load(ScriptServer::get_global_class_path(base));
  4208. if (!base_script.is_valid()) {
  4209. _set_error("Class '" + base + "' could not be fully loaded (script error or cyclic dependency).", p_class->line);
  4210. return;
  4211. }
  4212. p = NULL;
  4213. }
  4214. while (p) {
  4215. bool found = false;
  4216. for (int i = 0; i < p->subclasses.size(); i++) {
  4217. if (p->subclasses[i]->name == base) {
  4218. ClassNode *test = p->subclasses[i];
  4219. while (test) {
  4220. if (test == p_class) {
  4221. _set_error("Cyclic inheritance.", test->line);
  4222. return;
  4223. }
  4224. if (test->base_type.kind == DataType::CLASS) {
  4225. test = test->base_type.class_type;
  4226. } else {
  4227. break;
  4228. }
  4229. }
  4230. found = true;
  4231. if (extend_iter < p_class->extends_class.size()) {
  4232. // Keep looking at current classes if possible
  4233. base = p_class->extends_class[extend_iter++];
  4234. p = p->subclasses[i];
  4235. } else {
  4236. base_class = p->subclasses[i];
  4237. }
  4238. break;
  4239. }
  4240. }
  4241. if (base_class) break;
  4242. if (found) continue;
  4243. if (p->constant_expressions.has(base)) {
  4244. if (p->constant_expressions[base].expression->type != Node::TYPE_CONSTANT) {
  4245. _set_error("Could not resolve constant '" + base + "'.", p_class->line);
  4246. return;
  4247. }
  4248. const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[base].expression);
  4249. base_script = cn->value;
  4250. if (base_script.is_null()) {
  4251. _set_error("Constant is not a class: " + base, p_class->line);
  4252. return;
  4253. }
  4254. break;
  4255. }
  4256. p = p->owner;
  4257. }
  4258. if (base_script.is_valid()) {
  4259. String ident = base;
  4260. Ref<GDScript> find_subclass = base_script;
  4261. for (int i = extend_iter; i < p_class->extends_class.size(); i++) {
  4262. String subclass = p_class->extends_class[i];
  4263. ident += ("." + subclass);
  4264. if (base_script->get_subclasses().has(subclass)) {
  4265. find_subclass = base_script->get_subclasses()[subclass];
  4266. } else if (base_script->get_constants().has(subclass)) {
  4267. Ref<GDScript> new_base_class = base_script->get_constants()[subclass];
  4268. if (new_base_class.is_null()) {
  4269. _set_error("Constant is not a class: " + ident, p_class->line);
  4270. return;
  4271. }
  4272. find_subclass = new_base_class;
  4273. } else {
  4274. _set_error("Could not find subclass: " + ident, p_class->line);
  4275. return;
  4276. }
  4277. }
  4278. script = find_subclass;
  4279. } else if (!base_class) {
  4280. if (p_class->extends_class.size() > 1) {
  4281. _set_error("Invalid inheritance (unknown class + subclasses)", p_class->line);
  4282. return;
  4283. }
  4284. //if not found, try engine classes
  4285. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  4286. _set_error("Unknown class: '" + base + "'", p_class->line);
  4287. return;
  4288. }
  4289. native = base;
  4290. }
  4291. }
  4292. if (base_class) {
  4293. p_class->base_type.has_type = true;
  4294. p_class->base_type.kind = DataType::CLASS;
  4295. p_class->base_type.class_type = base_class;
  4296. } else if (script.is_valid()) {
  4297. p_class->base_type.has_type = true;
  4298. p_class->base_type.kind = DataType::GDSCRIPT;
  4299. p_class->base_type.script_type = script;
  4300. p_class->base_type.native_type = script->get_instance_base_type();
  4301. } else if (native != StringName()) {
  4302. p_class->base_type.has_type = true;
  4303. p_class->base_type.kind = DataType::NATIVE;
  4304. p_class->base_type.native_type = native;
  4305. } else {
  4306. _set_error("Could not determine inheritance", p_class->line);
  4307. return;
  4308. }
  4309. } else {
  4310. // without extends, implicitly extend Reference
  4311. p_class->base_type.has_type = true;
  4312. p_class->base_type.kind = DataType::NATIVE;
  4313. p_class->base_type.native_type = "Reference";
  4314. }
  4315. // Recursively determine subclasses
  4316. for (int i = 0; i < p_class->subclasses.size(); i++) {
  4317. _determine_inheritance(p_class->subclasses[i]);
  4318. }
  4319. }
  4320. String GDScriptParser::DataType::to_string() const {
  4321. if (!has_type) return "var";
  4322. switch (kind) {
  4323. case BUILTIN: {
  4324. if (builtin_type == Variant::NIL) return "null";
  4325. return Variant::get_type_name(builtin_type);
  4326. } break;
  4327. case NATIVE: {
  4328. if (is_meta_type) {
  4329. return "GDScriptNativeClass";
  4330. }
  4331. return native_type.operator String();
  4332. } break;
  4333. case GDSCRIPT: {
  4334. Ref<GDScript> gds = script_type;
  4335. const String &gds_class = gds->get_script_class_name();
  4336. if (!gds_class.empty()) {
  4337. return gds_class;
  4338. }
  4339. FALLTHROUGH;
  4340. }
  4341. case SCRIPT: {
  4342. if (is_meta_type) {
  4343. return script_type->get_class_name().operator String();
  4344. }
  4345. String name = script_type->get_name();
  4346. if (name != String()) {
  4347. return name;
  4348. }
  4349. name = script_type->get_path().get_file();
  4350. if (name != String()) {
  4351. return name;
  4352. }
  4353. return native_type.operator String();
  4354. } break;
  4355. case CLASS: {
  4356. ERR_FAIL_COND_V(!class_type, String());
  4357. if (is_meta_type) {
  4358. return "GDScript";
  4359. }
  4360. if (class_type->name == StringName()) {
  4361. return "self";
  4362. }
  4363. return class_type->name.operator String();
  4364. } break;
  4365. case UNRESOLVED: {
  4366. } break;
  4367. }
  4368. return "Unresolved";
  4369. }
  4370. bool GDScriptParser::_parse_type(DataType &r_type, bool p_can_be_void) {
  4371. tokenizer->advance();
  4372. r_type.has_type = true;
  4373. bool finished = false;
  4374. bool can_index = false;
  4375. String full_name;
  4376. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  4377. completion_cursor = StringName();
  4378. completion_type = COMPLETION_TYPE_HINT;
  4379. completion_class = current_class;
  4380. completion_function = current_function;
  4381. completion_line = tokenizer->get_token_line();
  4382. completion_argument = 0;
  4383. completion_block = current_block;
  4384. completion_found = true;
  4385. completion_ident_is_call = p_can_be_void;
  4386. tokenizer->advance();
  4387. }
  4388. switch (tokenizer->get_token()) {
  4389. case GDScriptTokenizer::TK_PR_VOID: {
  4390. if (!p_can_be_void) {
  4391. return false;
  4392. }
  4393. r_type.kind = DataType::BUILTIN;
  4394. r_type.builtin_type = Variant::NIL;
  4395. } break;
  4396. case GDScriptTokenizer::TK_BUILT_IN_TYPE: {
  4397. r_type.builtin_type = tokenizer->get_token_type();
  4398. if (tokenizer->get_token_type() == Variant::OBJECT) {
  4399. r_type.kind = DataType::NATIVE;
  4400. r_type.native_type = "Object";
  4401. } else {
  4402. r_type.kind = DataType::BUILTIN;
  4403. }
  4404. } break;
  4405. case GDScriptTokenizer::TK_IDENTIFIER: {
  4406. r_type.native_type = tokenizer->get_token_identifier();
  4407. if (ClassDB::class_exists(r_type.native_type) || ClassDB::class_exists("_" + r_type.native_type.operator String())) {
  4408. r_type.kind = DataType::NATIVE;
  4409. } else {
  4410. r_type.kind = DataType::UNRESOLVED;
  4411. can_index = true;
  4412. full_name = r_type.native_type;
  4413. }
  4414. } break;
  4415. default: {
  4416. return false;
  4417. }
  4418. }
  4419. tokenizer->advance();
  4420. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  4421. completion_cursor = r_type.native_type;
  4422. completion_type = COMPLETION_TYPE_HINT;
  4423. completion_class = current_class;
  4424. completion_function = current_function;
  4425. completion_line = tokenizer->get_token_line();
  4426. completion_argument = 0;
  4427. completion_block = current_block;
  4428. completion_found = true;
  4429. completion_ident_is_call = p_can_be_void;
  4430. tokenizer->advance();
  4431. }
  4432. if (can_index) {
  4433. while (!finished) {
  4434. switch (tokenizer->get_token()) {
  4435. case GDScriptTokenizer::TK_PERIOD: {
  4436. if (!can_index) {
  4437. _set_error("Unexpected '.'.");
  4438. return false;
  4439. }
  4440. can_index = false;
  4441. tokenizer->advance();
  4442. } break;
  4443. case GDScriptTokenizer::TK_IDENTIFIER: {
  4444. if (can_index) {
  4445. _set_error("Unexpected identifier.");
  4446. return false;
  4447. }
  4448. StringName id;
  4449. bool has_completion = _get_completable_identifier(COMPLETION_TYPE_HINT_INDEX, id);
  4450. if (id == StringName()) {
  4451. id = "@temp";
  4452. }
  4453. full_name += "." + id.operator String();
  4454. can_index = true;
  4455. if (has_completion) {
  4456. completion_cursor = full_name;
  4457. }
  4458. } break;
  4459. default: {
  4460. finished = true;
  4461. } break;
  4462. }
  4463. }
  4464. if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PERIOD) {
  4465. _set_error("Expected subclass identifier.");
  4466. return false;
  4467. }
  4468. r_type.native_type = full_name;
  4469. }
  4470. return true;
  4471. }
  4472. GDScriptParser::DataType GDScriptParser::_resolve_type(const DataType &p_source, int p_line) {
  4473. if (!p_source.has_type) return p_source;
  4474. if (p_source.kind != DataType::UNRESOLVED) return p_source;
  4475. Vector<String> full_name = p_source.native_type.operator String().split(".", false);
  4476. int name_part = 0;
  4477. DataType result;
  4478. result.has_type = true;
  4479. while (name_part < full_name.size()) {
  4480. bool found = false;
  4481. StringName id = full_name[name_part];
  4482. DataType base_type = result;
  4483. ClassNode *p = NULL;
  4484. if (name_part == 0) {
  4485. if (ScriptServer::is_global_class(id)) {
  4486. String script_path = ScriptServer::get_global_class_path(id);
  4487. if (script_path == self_path) {
  4488. result.kind = DataType::CLASS;
  4489. result.class_type = static_cast<ClassNode *>(head);
  4490. } else {
  4491. Ref<Script> script = ResourceLoader::load(script_path);
  4492. Ref<GDScript> gds = script;
  4493. if (gds.is_valid()) {
  4494. if (!gds->is_valid()) {
  4495. _set_error("Class '" + id + "' could not be fully loaded (script error or cyclic dependency).", p_line);
  4496. return DataType();
  4497. }
  4498. result.kind = DataType::GDSCRIPT;
  4499. result.script_type = gds;
  4500. } else if (script.is_valid()) {
  4501. result.kind = DataType::SCRIPT;
  4502. result.script_type = script;
  4503. } else {
  4504. _set_error("Class '" + id + "' was found in global scope but its script could not be loaded.", p_line);
  4505. return DataType();
  4506. }
  4507. }
  4508. name_part++;
  4509. continue;
  4510. } else {
  4511. p = current_class;
  4512. }
  4513. } else if (base_type.kind == DataType::CLASS) {
  4514. p = base_type.class_type;
  4515. }
  4516. while (p) {
  4517. if (p->constant_expressions.has(id)) {
  4518. if (p->constant_expressions[id].expression->type != Node::TYPE_CONSTANT) {
  4519. _set_error("Parser bug: unresolved constant.", p_line);
  4520. ERR_FAIL_V(result);
  4521. }
  4522. const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[id].expression);
  4523. Ref<GDScript> gds = cn->value;
  4524. if (gds.is_valid()) {
  4525. result.kind = DataType::GDSCRIPT;
  4526. result.script_type = gds;
  4527. found = true;
  4528. } else {
  4529. Ref<Script> scr = cn->value;
  4530. if (scr.is_valid()) {
  4531. result.kind = DataType::SCRIPT;
  4532. result.script_type = scr;
  4533. found = true;
  4534. }
  4535. }
  4536. break;
  4537. }
  4538. // Inner classes
  4539. ClassNode *outer_class = p;
  4540. while (outer_class) {
  4541. if (outer_class->name == id) {
  4542. found = true;
  4543. result.kind = DataType::CLASS;
  4544. result.class_type = outer_class;
  4545. break;
  4546. }
  4547. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  4548. if (outer_class->subclasses[i] == p) {
  4549. continue;
  4550. }
  4551. if (outer_class->subclasses[i]->name == id) {
  4552. found = true;
  4553. result.kind = DataType::CLASS;
  4554. result.class_type = outer_class->subclasses[i];
  4555. break;
  4556. }
  4557. }
  4558. if (found) {
  4559. break;
  4560. }
  4561. outer_class = outer_class->owner;
  4562. }
  4563. if (!found && p->base_type.kind == DataType::CLASS) {
  4564. p = p->base_type.class_type;
  4565. } else {
  4566. base_type = p->base_type;
  4567. break;
  4568. }
  4569. }
  4570. // Still look for class constants in parent script
  4571. if (!found && (base_type.kind == DataType::GDSCRIPT || base_type.kind == DataType::SCRIPT)) {
  4572. Ref<Script> scr = base_type.script_type;
  4573. ERR_FAIL_COND_V(scr.is_null(), result);
  4574. Map<StringName, Variant> constants;
  4575. scr->get_constants(&constants);
  4576. if (constants.has(id)) {
  4577. Ref<GDScript> gds = constants[id];
  4578. if (gds.is_valid()) {
  4579. result.kind = DataType::GDSCRIPT;
  4580. result.script_type = gds;
  4581. found = true;
  4582. } else {
  4583. Ref<Script> scr2 = constants[id];
  4584. if (scr2.is_valid()) {
  4585. result.kind = DataType::SCRIPT;
  4586. result.script_type = scr2;
  4587. found = true;
  4588. }
  4589. }
  4590. }
  4591. }
  4592. if (!found && !for_completion) {
  4593. String base;
  4594. if (name_part == 0) {
  4595. base = "self";
  4596. } else {
  4597. base = result.to_string();
  4598. }
  4599. _set_error("Identifier '" + String(id) + "' is not a valid type (not a script or class), or could not be found on base '" +
  4600. base + "'.",
  4601. p_line);
  4602. return DataType();
  4603. }
  4604. name_part++;
  4605. }
  4606. return result;
  4607. }
  4608. GDScriptParser::DataType GDScriptParser::_type_from_variant(const Variant &p_value) const {
  4609. DataType result;
  4610. result.has_type = true;
  4611. result.is_constant = true;
  4612. result.kind = DataType::BUILTIN;
  4613. result.builtin_type = p_value.get_type();
  4614. if (result.builtin_type == Variant::OBJECT) {
  4615. Object *obj = p_value.operator Object *();
  4616. if (!obj) {
  4617. return DataType();
  4618. }
  4619. result.native_type = obj->get_class_name();
  4620. Ref<Script> scr = p_value;
  4621. if (scr.is_valid()) {
  4622. result.is_meta_type = true;
  4623. } else {
  4624. result.is_meta_type = false;
  4625. scr = obj->get_script();
  4626. }
  4627. if (scr.is_valid()) {
  4628. result.script_type = scr;
  4629. Ref<GDScript> gds = scr;
  4630. if (gds.is_valid()) {
  4631. result.kind = DataType::GDSCRIPT;
  4632. } else {
  4633. result.kind = DataType::SCRIPT;
  4634. }
  4635. result.native_type = scr->get_instance_base_type();
  4636. } else {
  4637. result.kind = DataType::NATIVE;
  4638. }
  4639. }
  4640. return result;
  4641. }
  4642. GDScriptParser::DataType GDScriptParser::_type_from_property(const PropertyInfo &p_property, bool p_nil_is_variant) const {
  4643. DataType ret;
  4644. if (p_property.type == Variant::NIL && (p_nil_is_variant || (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  4645. // Variant
  4646. return ret;
  4647. }
  4648. ret.has_type = true;
  4649. ret.builtin_type = p_property.type;
  4650. if (p_property.type == Variant::OBJECT) {
  4651. ret.kind = DataType::NATIVE;
  4652. ret.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  4653. } else {
  4654. ret.kind = DataType::BUILTIN;
  4655. }
  4656. return ret;
  4657. }
  4658. GDScriptParser::DataType GDScriptParser::_type_from_gdtype(const GDScriptDataType &p_gdtype) const {
  4659. DataType result;
  4660. if (!p_gdtype.has_type) {
  4661. return result;
  4662. }
  4663. result.has_type = true;
  4664. result.builtin_type = p_gdtype.builtin_type;
  4665. result.native_type = p_gdtype.native_type;
  4666. result.script_type = p_gdtype.script_type;
  4667. switch (p_gdtype.kind) {
  4668. case GDScriptDataType::UNINITIALIZED: {
  4669. ERR_EXPLAIN("Uninitialized datatype. Please report a bug.");
  4670. } break;
  4671. case GDScriptDataType::BUILTIN: {
  4672. result.kind = DataType::BUILTIN;
  4673. } break;
  4674. case GDScriptDataType::NATIVE: {
  4675. result.kind = DataType::NATIVE;
  4676. } break;
  4677. case GDScriptDataType::GDSCRIPT: {
  4678. result.kind = DataType::GDSCRIPT;
  4679. } break;
  4680. case GDScriptDataType::SCRIPT: {
  4681. result.kind = DataType::SCRIPT;
  4682. } break;
  4683. }
  4684. return result;
  4685. }
  4686. GDScriptParser::DataType GDScriptParser::_get_operation_type(const Variant::Operator p_op, const DataType &p_a, const DataType &p_b, bool &r_valid) const {
  4687. if (!p_a.has_type || !p_b.has_type) {
  4688. r_valid = true;
  4689. return DataType();
  4690. }
  4691. Variant::Type a_type = p_a.kind == DataType::BUILTIN ? p_a.builtin_type : Variant::OBJECT;
  4692. Variant::Type b_type = p_b.kind == DataType::BUILTIN ? p_b.builtin_type : Variant::OBJECT;
  4693. Variant a;
  4694. REF a_ref;
  4695. if (a_type == Variant::OBJECT) {
  4696. a_ref.instance();
  4697. a = a_ref;
  4698. } else {
  4699. Variant::CallError err;
  4700. a = Variant::construct(a_type, NULL, 0, err);
  4701. if (err.error != Variant::CallError::CALL_OK) {
  4702. r_valid = false;
  4703. return DataType();
  4704. }
  4705. }
  4706. Variant b;
  4707. REF b_ref;
  4708. if (b_type == Variant::OBJECT) {
  4709. b_ref.instance();
  4710. b = b_ref;
  4711. } else {
  4712. Variant::CallError err;
  4713. b = Variant::construct(b_type, NULL, 0, err);
  4714. if (err.error != Variant::CallError::CALL_OK) {
  4715. r_valid = false;
  4716. return DataType();
  4717. }
  4718. }
  4719. // Avoid division by zero
  4720. if (a_type == Variant::INT || a_type == Variant::REAL) {
  4721. Variant::evaluate(Variant::OP_ADD, a, 1, a, r_valid);
  4722. }
  4723. if (b_type == Variant::INT || b_type == Variant::REAL) {
  4724. Variant::evaluate(Variant::OP_ADD, b, 1, b, r_valid);
  4725. }
  4726. if (a_type == Variant::STRING && b_type != Variant::ARRAY) {
  4727. a = "%s"; // Work around for formatting operator (%)
  4728. }
  4729. Variant ret;
  4730. Variant::evaluate(p_op, a, b, ret, r_valid);
  4731. if (r_valid) {
  4732. return _type_from_variant(ret);
  4733. }
  4734. return DataType();
  4735. }
  4736. Variant::Operator GDScriptParser::_get_variant_operation(const OperatorNode::Operator &p_op) const {
  4737. switch (p_op) {
  4738. case OperatorNode::OP_NEG: {
  4739. return Variant::OP_NEGATE;
  4740. } break;
  4741. case OperatorNode::OP_POS: {
  4742. return Variant::OP_POSITIVE;
  4743. } break;
  4744. case OperatorNode::OP_NOT: {
  4745. return Variant::OP_NOT;
  4746. } break;
  4747. case OperatorNode::OP_BIT_INVERT: {
  4748. return Variant::OP_BIT_NEGATE;
  4749. } break;
  4750. case OperatorNode::OP_IN: {
  4751. return Variant::OP_IN;
  4752. } break;
  4753. case OperatorNode::OP_EQUAL: {
  4754. return Variant::OP_EQUAL;
  4755. } break;
  4756. case OperatorNode::OP_NOT_EQUAL: {
  4757. return Variant::OP_NOT_EQUAL;
  4758. } break;
  4759. case OperatorNode::OP_LESS: {
  4760. return Variant::OP_LESS;
  4761. } break;
  4762. case OperatorNode::OP_LESS_EQUAL: {
  4763. return Variant::OP_LESS_EQUAL;
  4764. } break;
  4765. case OperatorNode::OP_GREATER: {
  4766. return Variant::OP_GREATER;
  4767. } break;
  4768. case OperatorNode::OP_GREATER_EQUAL: {
  4769. return Variant::OP_GREATER_EQUAL;
  4770. } break;
  4771. case OperatorNode::OP_AND: {
  4772. return Variant::OP_AND;
  4773. } break;
  4774. case OperatorNode::OP_OR: {
  4775. return Variant::OP_OR;
  4776. } break;
  4777. case OperatorNode::OP_ASSIGN_ADD:
  4778. case OperatorNode::OP_ADD: {
  4779. return Variant::OP_ADD;
  4780. } break;
  4781. case OperatorNode::OP_ASSIGN_SUB:
  4782. case OperatorNode::OP_SUB: {
  4783. return Variant::OP_SUBTRACT;
  4784. } break;
  4785. case OperatorNode::OP_ASSIGN_MUL:
  4786. case OperatorNode::OP_MUL: {
  4787. return Variant::OP_MULTIPLY;
  4788. } break;
  4789. case OperatorNode::OP_ASSIGN_DIV:
  4790. case OperatorNode::OP_DIV: {
  4791. return Variant::OP_DIVIDE;
  4792. } break;
  4793. case OperatorNode::OP_ASSIGN_MOD:
  4794. case OperatorNode::OP_MOD: {
  4795. return Variant::OP_MODULE;
  4796. } break;
  4797. case OperatorNode::OP_ASSIGN_BIT_AND:
  4798. case OperatorNode::OP_BIT_AND: {
  4799. return Variant::OP_BIT_AND;
  4800. } break;
  4801. case OperatorNode::OP_ASSIGN_BIT_OR:
  4802. case OperatorNode::OP_BIT_OR: {
  4803. return Variant::OP_BIT_OR;
  4804. } break;
  4805. case OperatorNode::OP_ASSIGN_BIT_XOR:
  4806. case OperatorNode::OP_BIT_XOR: {
  4807. return Variant::OP_BIT_XOR;
  4808. } break;
  4809. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  4810. case OperatorNode::OP_SHIFT_LEFT: {
  4811. return Variant::OP_SHIFT_LEFT;
  4812. }
  4813. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  4814. case OperatorNode::OP_SHIFT_RIGHT: {
  4815. return Variant::OP_SHIFT_RIGHT;
  4816. }
  4817. default: {
  4818. return Variant::OP_MAX;
  4819. } break;
  4820. }
  4821. }
  4822. bool GDScriptParser::_is_type_compatible(const DataType &p_container, const DataType &p_expression, bool p_allow_implicit_conversion) const {
  4823. // Ignore for completion
  4824. if (!check_types || for_completion) {
  4825. return true;
  4826. }
  4827. // Can't test if not all have type
  4828. if (!p_container.has_type || !p_expression.has_type) {
  4829. return true;
  4830. }
  4831. // Should never get here unresolved
  4832. ERR_FAIL_COND_V(p_container.kind == DataType::UNRESOLVED, false);
  4833. ERR_FAIL_COND_V(p_expression.kind == DataType::UNRESOLVED, false);
  4834. if (p_container.kind == DataType::BUILTIN && p_expression.kind == DataType::BUILTIN) {
  4835. bool valid = p_container.builtin_type == p_expression.builtin_type;
  4836. if (p_allow_implicit_conversion) {
  4837. valid = valid || Variant::can_convert_strict(p_expression.builtin_type, p_container.builtin_type);
  4838. }
  4839. return valid;
  4840. }
  4841. if (p_container.kind == DataType::BUILTIN && p_container.builtin_type == Variant::OBJECT) {
  4842. // Object built-in is a special case, it's compatible with any object and with null
  4843. if (p_expression.kind == DataType::BUILTIN) {
  4844. return p_expression.builtin_type == Variant::NIL;
  4845. }
  4846. // If it's not a built-in, must be an object
  4847. return true;
  4848. }
  4849. if (p_container.kind == DataType::BUILTIN || (p_expression.kind == DataType::BUILTIN && p_expression.builtin_type != Variant::NIL)) {
  4850. // Can't mix built-ins with objects
  4851. return false;
  4852. }
  4853. // From now on everything is objects, check polymorphism
  4854. // The container must be the same class or a superclass of the expression
  4855. if (p_expression.kind == DataType::BUILTIN && p_expression.builtin_type == Variant::NIL) {
  4856. // Null can be assigned to object types
  4857. return true;
  4858. }
  4859. StringName expr_native;
  4860. Ref<Script> expr_script;
  4861. ClassNode *expr_class = NULL;
  4862. switch (p_expression.kind) {
  4863. case DataType::NATIVE: {
  4864. if (p_container.kind != DataType::NATIVE) {
  4865. // Non-native type can't be a superclass of a native type
  4866. return false;
  4867. }
  4868. if (p_expression.is_meta_type) {
  4869. expr_native = GDScriptNativeClass::get_class_static();
  4870. } else {
  4871. expr_native = p_expression.native_type;
  4872. }
  4873. } break;
  4874. case DataType::SCRIPT:
  4875. case DataType::GDSCRIPT: {
  4876. if (p_container.kind == DataType::CLASS) {
  4877. // This cannot be resolved without cyclic dependencies, so just bail out
  4878. return false;
  4879. }
  4880. if (p_expression.is_meta_type) {
  4881. expr_native = p_expression.script_type->get_class_name();
  4882. } else {
  4883. expr_script = p_expression.script_type;
  4884. expr_native = expr_script->get_instance_base_type();
  4885. }
  4886. } break;
  4887. case DataType::CLASS: {
  4888. if (p_expression.is_meta_type) {
  4889. expr_native = GDScript::get_class_static();
  4890. } else {
  4891. expr_class = p_expression.class_type;
  4892. ClassNode *base = expr_class;
  4893. while (base->base_type.kind == DataType::CLASS) {
  4894. base = base->base_type.class_type;
  4895. }
  4896. expr_native = base->base_type.native_type;
  4897. expr_script = base->base_type.script_type;
  4898. }
  4899. } break;
  4900. case DataType::BUILTIN: // Already handled above
  4901. case DataType::UNRESOLVED: // Not allowed, see above
  4902. break;
  4903. }
  4904. switch (p_container.kind) {
  4905. case DataType::NATIVE: {
  4906. if (p_container.is_meta_type) {
  4907. return ClassDB::is_parent_class(expr_native, GDScriptNativeClass::get_class_static());
  4908. } else {
  4909. return ClassDB::is_parent_class(expr_native, p_container.native_type);
  4910. }
  4911. } break;
  4912. case DataType::SCRIPT:
  4913. case DataType::GDSCRIPT: {
  4914. if (p_container.is_meta_type) {
  4915. return ClassDB::is_parent_class(expr_native, GDScript::get_class_static());
  4916. }
  4917. if (expr_class == head && p_container.script_type->get_path() == self_path) {
  4918. // Special case: container is self script and expression is self
  4919. return true;
  4920. }
  4921. while (expr_script.is_valid()) {
  4922. if (expr_script == p_container.script_type) {
  4923. return true;
  4924. }
  4925. expr_script = expr_script->get_base_script();
  4926. }
  4927. return false;
  4928. } break;
  4929. case DataType::CLASS: {
  4930. if (p_container.is_meta_type) {
  4931. return ClassDB::is_parent_class(expr_native, GDScript::get_class_static());
  4932. }
  4933. if (p_container.class_type == head && expr_script.is_valid() && expr_script->get_path() == self_path) {
  4934. // Special case: container is self and expression is self script
  4935. return true;
  4936. }
  4937. while (expr_class) {
  4938. if (expr_class == p_container.class_type) {
  4939. return true;
  4940. }
  4941. expr_class = expr_class->base_type.class_type;
  4942. }
  4943. return false;
  4944. } break;
  4945. case DataType::BUILTIN: // Already handled above
  4946. case DataType::UNRESOLVED: // Not allowed, see above
  4947. break;
  4948. }
  4949. return false;
  4950. }
  4951. GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
  4952. #ifdef DEBUG_ENABLED
  4953. if (p_node->get_datatype().has_type && p_node->type != Node::TYPE_ARRAY && p_node->type != Node::TYPE_DICTIONARY) {
  4954. #else
  4955. if (p_node->get_datatype().has_type) {
  4956. #endif
  4957. return p_node->get_datatype();
  4958. }
  4959. DataType node_type;
  4960. switch (p_node->type) {
  4961. case Node::TYPE_CONSTANT: {
  4962. node_type = _type_from_variant(static_cast<ConstantNode *>(p_node)->value);
  4963. } break;
  4964. case Node::TYPE_TYPE: {
  4965. TypeNode *tn = static_cast<TypeNode *>(p_node);
  4966. node_type.has_type = true;
  4967. node_type.is_meta_type = true;
  4968. node_type.kind = DataType::BUILTIN;
  4969. node_type.builtin_type = tn->vtype;
  4970. } break;
  4971. case Node::TYPE_ARRAY: {
  4972. node_type.has_type = true;
  4973. node_type.kind = DataType::BUILTIN;
  4974. node_type.builtin_type = Variant::ARRAY;
  4975. #ifdef DEBUG_ENABLED
  4976. // Check stuff inside the array
  4977. ArrayNode *an = static_cast<ArrayNode *>(p_node);
  4978. for (int i = 0; i < an->elements.size(); i++) {
  4979. _reduce_node_type(an->elements[i]);
  4980. }
  4981. #endif // DEBUG_ENABLED
  4982. } break;
  4983. case Node::TYPE_DICTIONARY: {
  4984. node_type.has_type = true;
  4985. node_type.kind = DataType::BUILTIN;
  4986. node_type.builtin_type = Variant::DICTIONARY;
  4987. #ifdef DEBUG_ENABLED
  4988. // Check stuff inside the dictionarty
  4989. DictionaryNode *dn = static_cast<DictionaryNode *>(p_node);
  4990. for (int i = 0; i < dn->elements.size(); i++) {
  4991. _reduce_node_type(dn->elements[i].key);
  4992. _reduce_node_type(dn->elements[i].value);
  4993. }
  4994. #endif // DEBUG_ENABLED
  4995. } break;
  4996. case Node::TYPE_SELF: {
  4997. node_type.has_type = true;
  4998. node_type.kind = DataType::CLASS;
  4999. node_type.class_type = current_class;
  5000. } break;
  5001. case Node::TYPE_IDENTIFIER: {
  5002. IdentifierNode *id = static_cast<IdentifierNode *>(p_node);
  5003. if (id->declared_block) {
  5004. node_type = id->declared_block->variables[id->name]->get_datatype();
  5005. id->declared_block->variables[id->name]->usages += 1;
  5006. } else if (id->name == "#match_value") {
  5007. // It's a special id just for the match statetement, ignore
  5008. break;
  5009. } else if (current_function && current_function->arguments.find(id->name) >= 0) {
  5010. int idx = current_function->arguments.find(id->name);
  5011. node_type = current_function->argument_types[idx];
  5012. } else {
  5013. node_type = _reduce_identifier_type(NULL, id->name, id->line, false);
  5014. }
  5015. } break;
  5016. case Node::TYPE_CAST: {
  5017. CastNode *cn = static_cast<CastNode *>(p_node);
  5018. DataType source_type = _reduce_node_type(cn->source_node);
  5019. cn->cast_type = _resolve_type(cn->cast_type, cn->line);
  5020. if (source_type.has_type) {
  5021. bool valid = false;
  5022. if (check_types) {
  5023. if (cn->cast_type.kind == DataType::BUILTIN && source_type.kind == DataType::BUILTIN) {
  5024. valid = Variant::can_convert(source_type.builtin_type, cn->cast_type.builtin_type);
  5025. }
  5026. if (cn->cast_type.kind != DataType::BUILTIN && source_type.kind != DataType::BUILTIN) {
  5027. valid = _is_type_compatible(cn->cast_type, source_type) || _is_type_compatible(source_type, cn->cast_type);
  5028. }
  5029. if (!valid) {
  5030. _set_error("Invalid cast. Cannot convert from '" + source_type.to_string() +
  5031. "' to '" + cn->cast_type.to_string() + "'.",
  5032. cn->line);
  5033. return DataType();
  5034. }
  5035. }
  5036. } else {
  5037. #ifdef DEBUG_ENABLED
  5038. _add_warning(GDScriptWarning::UNSAFE_CAST, cn->line, cn->cast_type.to_string());
  5039. #endif // DEBUG_ENABLED
  5040. _mark_line_as_unsafe(cn->line);
  5041. }
  5042. node_type = cn->cast_type;
  5043. } break;
  5044. case Node::TYPE_OPERATOR: {
  5045. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  5046. switch (op->op) {
  5047. case OperatorNode::OP_CALL:
  5048. case OperatorNode::OP_PARENT_CALL: {
  5049. node_type = _reduce_function_call_type(op);
  5050. } break;
  5051. case OperatorNode::OP_YIELD: {
  5052. if (op->arguments.size() == 2) {
  5053. DataType base_type = _reduce_node_type(op->arguments[0]);
  5054. DataType signal_type = _reduce_node_type(op->arguments[1]);
  5055. // TODO: Check if signal exists when it's a constant
  5056. if (base_type.has_type && base_type.kind == DataType::BUILTIN && base_type.builtin_type != Variant::NIL && base_type.builtin_type != Variant::OBJECT) {
  5057. _set_error("First argument of 'yield()' must be an object.", op->line);
  5058. return DataType();
  5059. }
  5060. if (signal_type.has_type && (signal_type.kind != DataType::BUILTIN || signal_type.builtin_type != Variant::STRING)) {
  5061. _set_error("Second argument of 'yield()' must be a string.", op->line);
  5062. return DataType();
  5063. }
  5064. }
  5065. // yield can return anything
  5066. node_type.has_type = false;
  5067. } break;
  5068. case OperatorNode::OP_IS:
  5069. case OperatorNode::OP_IS_BUILTIN: {
  5070. if (op->arguments.size() != 2) {
  5071. _set_error("Parser bug: binary operation without 2 arguments.", op->line);
  5072. ERR_FAIL_V(DataType());
  5073. }
  5074. DataType value_type = _reduce_node_type(op->arguments[0]);
  5075. DataType type_type = _reduce_node_type(op->arguments[1]);
  5076. if (check_types && type_type.has_type) {
  5077. if (!type_type.is_meta_type && (type_type.kind != DataType::NATIVE || !ClassDB::is_parent_class(type_type.native_type, "Script"))) {
  5078. _set_error("Invalid 'is' test: right operand is not a type (not a native type nor a script).", op->line);
  5079. return DataType();
  5080. }
  5081. type_type.is_meta_type = false; // Test the actual type
  5082. if (!_is_type_compatible(type_type, value_type) && !_is_type_compatible(value_type, type_type)) {
  5083. if (op->op == OperatorNode::OP_IS) {
  5084. _set_error("A value of type '" + value_type.to_string() + "' will never be an instance of '" + type_type.to_string() + "'.", op->line);
  5085. } else {
  5086. _set_error("A value of type '" + value_type.to_string() + "' will never be of type '" + type_type.to_string() + "'.", op->line);
  5087. }
  5088. return DataType();
  5089. }
  5090. }
  5091. node_type.has_type = true;
  5092. node_type.is_constant = true;
  5093. node_type.is_meta_type = false;
  5094. node_type.kind = DataType::BUILTIN;
  5095. node_type.builtin_type = Variant::BOOL;
  5096. } break;
  5097. // Unary operators
  5098. case OperatorNode::OP_NEG:
  5099. case OperatorNode::OP_POS:
  5100. case OperatorNode::OP_NOT:
  5101. case OperatorNode::OP_BIT_INVERT: {
  5102. DataType argument_type = _reduce_node_type(op->arguments[0]);
  5103. if (!argument_type.has_type) {
  5104. break;
  5105. }
  5106. Variant::Operator var_op = _get_variant_operation(op->op);
  5107. bool valid = false;
  5108. node_type = _get_operation_type(var_op, argument_type, argument_type, valid);
  5109. if (check_types && !valid) {
  5110. _set_error("Invalid operand type ('" + argument_type.to_string() +
  5111. "') to unary operator '" + Variant::get_operator_name(var_op) + "'.",
  5112. op->line, op->column);
  5113. return DataType();
  5114. }
  5115. } break;
  5116. // Binary operators
  5117. case OperatorNode::OP_IN:
  5118. case OperatorNode::OP_EQUAL:
  5119. case OperatorNode::OP_NOT_EQUAL:
  5120. case OperatorNode::OP_LESS:
  5121. case OperatorNode::OP_LESS_EQUAL:
  5122. case OperatorNode::OP_GREATER:
  5123. case OperatorNode::OP_GREATER_EQUAL:
  5124. case OperatorNode::OP_AND:
  5125. case OperatorNode::OP_OR:
  5126. case OperatorNode::OP_ADD:
  5127. case OperatorNode::OP_SUB:
  5128. case OperatorNode::OP_MUL:
  5129. case OperatorNode::OP_DIV:
  5130. case OperatorNode::OP_MOD:
  5131. case OperatorNode::OP_SHIFT_LEFT:
  5132. case OperatorNode::OP_SHIFT_RIGHT:
  5133. case OperatorNode::OP_BIT_AND:
  5134. case OperatorNode::OP_BIT_OR:
  5135. case OperatorNode::OP_BIT_XOR: {
  5136. if (op->arguments.size() != 2) {
  5137. _set_error("Parser bug: binary operation without 2 arguments.", op->line);
  5138. ERR_FAIL_V(DataType());
  5139. }
  5140. DataType argument_a_type = _reduce_node_type(op->arguments[0]);
  5141. DataType argument_b_type = _reduce_node_type(op->arguments[1]);
  5142. if (!argument_a_type.has_type || !argument_b_type.has_type) {
  5143. _mark_line_as_unsafe(op->line);
  5144. break;
  5145. }
  5146. Variant::Operator var_op = _get_variant_operation(op->op);
  5147. bool valid = false;
  5148. node_type = _get_operation_type(var_op, argument_a_type, argument_b_type, valid);
  5149. if (check_types && !valid) {
  5150. _set_error("Invalid operand types ('" + argument_a_type.to_string() + "' and '" +
  5151. argument_b_type.to_string() + "') to operator '" + Variant::get_operator_name(var_op) + "'.",
  5152. op->line, op->column);
  5153. return DataType();
  5154. }
  5155. #ifdef DEBUG_ENABLED
  5156. if (var_op == Variant::OP_DIVIDE && argument_a_type.kind == DataType::BUILTIN && argument_a_type.builtin_type == Variant::INT &&
  5157. argument_b_type.kind == DataType::BUILTIN && argument_b_type.builtin_type == Variant::INT) {
  5158. _add_warning(GDScriptWarning::INTEGER_DIVISION, op->line);
  5159. }
  5160. #endif // DEBUG_ENABLED
  5161. } break;
  5162. // Ternary operators
  5163. case OperatorNode::OP_TERNARY_IF: {
  5164. if (op->arguments.size() != 3) {
  5165. _set_error("Parser bug: ternary operation without 3 arguments");
  5166. ERR_FAIL_V(DataType());
  5167. }
  5168. DataType true_type = _reduce_node_type(op->arguments[1]);
  5169. DataType false_type = _reduce_node_type(op->arguments[2]);
  5170. // If types are equal, then the expression is of the same type
  5171. // If they are compatible, return the broader type
  5172. if (true_type == false_type || _is_type_compatible(true_type, false_type)) {
  5173. node_type = true_type;
  5174. } else if (_is_type_compatible(false_type, true_type)) {
  5175. node_type = false_type;
  5176. } else {
  5177. #ifdef DEBUG_ENABLED
  5178. _add_warning(GDScriptWarning::INCOMPATIBLE_TERNARY, op->line);
  5179. #endif // DEBUG_ENABLED
  5180. }
  5181. } break;
  5182. // Assignment should never happen within an expression
  5183. case OperatorNode::OP_ASSIGN:
  5184. case OperatorNode::OP_ASSIGN_ADD:
  5185. case OperatorNode::OP_ASSIGN_SUB:
  5186. case OperatorNode::OP_ASSIGN_MUL:
  5187. case OperatorNode::OP_ASSIGN_DIV:
  5188. case OperatorNode::OP_ASSIGN_MOD:
  5189. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  5190. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  5191. case OperatorNode::OP_ASSIGN_BIT_AND:
  5192. case OperatorNode::OP_ASSIGN_BIT_OR:
  5193. case OperatorNode::OP_ASSIGN_BIT_XOR:
  5194. case OperatorNode::OP_INIT_ASSIGN: {
  5195. _set_error("Assignment inside expression is not allowed (parser bug?).", op->line);
  5196. return DataType();
  5197. } break;
  5198. case OperatorNode::OP_INDEX_NAMED: {
  5199. if (op->arguments.size() != 2) {
  5200. _set_error("Parser bug: named index with invalid arguments.", op->line);
  5201. ERR_FAIL_V(DataType());
  5202. }
  5203. if (op->arguments[1]->type != Node::TYPE_IDENTIFIER) {
  5204. _set_error("Parser bug: named index without identifier argument.", op->line);
  5205. ERR_FAIL_V(DataType());
  5206. }
  5207. DataType base_type = _reduce_node_type(op->arguments[0]);
  5208. IdentifierNode *member_id = static_cast<IdentifierNode *>(op->arguments[1]);
  5209. if (base_type.has_type) {
  5210. if (check_types && base_type.kind == DataType::BUILTIN) {
  5211. // Variant type, just test if it's possible
  5212. DataType result;
  5213. switch (base_type.builtin_type) {
  5214. case Variant::NIL:
  5215. case Variant::DICTIONARY: {
  5216. result.has_type = false;
  5217. } break;
  5218. default: {
  5219. Variant::CallError err;
  5220. Variant temp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  5221. bool valid = false;
  5222. Variant res = temp.get(member_id->name.operator String(), &valid);
  5223. if (valid) {
  5224. result = _type_from_variant(res);
  5225. } else if (check_types) {
  5226. _set_error("Can't get index '" + String(member_id->name.operator String()) + "' on base '" +
  5227. base_type.to_string() + "'.",
  5228. op->line);
  5229. return DataType();
  5230. }
  5231. } break;
  5232. }
  5233. result.is_constant = false;
  5234. node_type = result;
  5235. } else {
  5236. node_type = _reduce_identifier_type(&base_type, member_id->name, op->line, true);
  5237. #ifdef DEBUG_ENABLED
  5238. if (!node_type.has_type) {
  5239. _add_warning(GDScriptWarning::UNSAFE_PROPERTY_ACCESS, op->line, member_id->name.operator String(), base_type.to_string());
  5240. }
  5241. #endif // DEBUG_ENABLED
  5242. }
  5243. } else {
  5244. _mark_line_as_unsafe(op->line);
  5245. }
  5246. if (error_set) {
  5247. return DataType();
  5248. }
  5249. } break;
  5250. case OperatorNode::OP_INDEX: {
  5251. if (op->arguments[1]->type == Node::TYPE_CONSTANT) {
  5252. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[1]);
  5253. if (cn->value.get_type() == Variant::STRING) {
  5254. // Treat this as named indexing
  5255. IdentifierNode *id = alloc_node<IdentifierNode>();
  5256. id->name = cn->value.operator StringName();
  5257. op->op = OperatorNode::OP_INDEX_NAMED;
  5258. op->arguments.write[1] = id;
  5259. return _reduce_node_type(op);
  5260. }
  5261. }
  5262. DataType base_type = _reduce_node_type(op->arguments[0]);
  5263. DataType index_type = _reduce_node_type(op->arguments[1]);
  5264. if (!base_type.has_type) {
  5265. _mark_line_as_unsafe(op->line);
  5266. break;
  5267. }
  5268. if (check_types && index_type.has_type) {
  5269. if (base_type.kind == DataType::BUILTIN) {
  5270. // Check if indexing is valid
  5271. bool error = index_type.kind != DataType::BUILTIN && base_type.builtin_type != Variant::DICTIONARY;
  5272. if (!error) {
  5273. switch (base_type.builtin_type) {
  5274. // Expect int or real as index
  5275. case Variant::POOL_BYTE_ARRAY:
  5276. case Variant::POOL_COLOR_ARRAY:
  5277. case Variant::POOL_INT_ARRAY:
  5278. case Variant::POOL_REAL_ARRAY:
  5279. case Variant::POOL_STRING_ARRAY:
  5280. case Variant::POOL_VECTOR2_ARRAY:
  5281. case Variant::POOL_VECTOR3_ARRAY:
  5282. case Variant::ARRAY:
  5283. case Variant::STRING: {
  5284. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::REAL;
  5285. } break;
  5286. // Expect String only
  5287. case Variant::RECT2:
  5288. case Variant::PLANE:
  5289. case Variant::QUAT:
  5290. case Variant::AABB:
  5291. case Variant::OBJECT: {
  5292. error = index_type.builtin_type != Variant::STRING;
  5293. } break;
  5294. // Expect String or number
  5295. case Variant::VECTOR2:
  5296. case Variant::VECTOR3:
  5297. case Variant::TRANSFORM2D:
  5298. case Variant::BASIS:
  5299. case Variant::TRANSFORM: {
  5300. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::REAL &&
  5301. index_type.builtin_type != Variant::STRING;
  5302. } break;
  5303. // Expect String or int
  5304. case Variant::COLOR: {
  5305. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING;
  5306. } break;
  5307. default: {
  5308. }
  5309. }
  5310. }
  5311. if (error) {
  5312. _set_error("Invalid index type (" + index_type.to_string() + ") for base '" + base_type.to_string() + "'.",
  5313. op->line);
  5314. return DataType();
  5315. }
  5316. if (op->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT) {
  5317. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[1]);
  5318. // Index is a constant, just try it if possible
  5319. switch (base_type.builtin_type) {
  5320. // Arrays/string have variable indexing, can't test directly
  5321. case Variant::STRING:
  5322. case Variant::ARRAY:
  5323. case Variant::DICTIONARY:
  5324. case Variant::POOL_BYTE_ARRAY:
  5325. case Variant::POOL_COLOR_ARRAY:
  5326. case Variant::POOL_INT_ARRAY:
  5327. case Variant::POOL_REAL_ARRAY:
  5328. case Variant::POOL_STRING_ARRAY:
  5329. case Variant::POOL_VECTOR2_ARRAY:
  5330. case Variant::POOL_VECTOR3_ARRAY: {
  5331. break;
  5332. }
  5333. default: {
  5334. Variant::CallError err;
  5335. Variant temp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  5336. bool valid = false;
  5337. Variant res = temp.get(cn->value, &valid);
  5338. if (valid) {
  5339. node_type = _type_from_variant(res);
  5340. node_type.is_constant = false;
  5341. } else if (check_types) {
  5342. _set_error("Can't get index '" + String(cn->value) + "' on base '" +
  5343. base_type.to_string() + "'.",
  5344. op->line);
  5345. return DataType();
  5346. }
  5347. } break;
  5348. }
  5349. } else {
  5350. _mark_line_as_unsafe(op->line);
  5351. }
  5352. } else if (!for_completion && (index_type.kind != DataType::BUILTIN || index_type.builtin_type != Variant::STRING)) {
  5353. _set_error("Only strings can be used as index in the base type '" + base_type.to_string() + "'.", op->line);
  5354. return DataType();
  5355. }
  5356. }
  5357. if (check_types && !node_type.has_type) {
  5358. // Can infer indexing type for some variant types
  5359. DataType result;
  5360. result.has_type = true;
  5361. result.kind = DataType::BUILTIN;
  5362. switch (base_type.builtin_type) {
  5363. // Can't index at all
  5364. case Variant::NIL:
  5365. case Variant::BOOL:
  5366. case Variant::INT:
  5367. case Variant::REAL:
  5368. case Variant::NODE_PATH:
  5369. case Variant::_RID: {
  5370. _set_error("Can't index on a value of type '" + base_type.to_string() + "'.", op->line);
  5371. return DataType();
  5372. } break;
  5373. // Return int
  5374. case Variant::POOL_BYTE_ARRAY:
  5375. case Variant::POOL_INT_ARRAY: {
  5376. result.builtin_type = Variant::INT;
  5377. } break;
  5378. // Return real
  5379. case Variant::POOL_REAL_ARRAY:
  5380. case Variant::VECTOR2:
  5381. case Variant::VECTOR3:
  5382. case Variant::QUAT: {
  5383. result.builtin_type = Variant::REAL;
  5384. } break;
  5385. // Return color
  5386. case Variant::POOL_COLOR_ARRAY: {
  5387. result.builtin_type = Variant::COLOR;
  5388. } break;
  5389. // Return string
  5390. case Variant::POOL_STRING_ARRAY:
  5391. case Variant::STRING: {
  5392. result.builtin_type = Variant::STRING;
  5393. } break;
  5394. // Return Vector2
  5395. case Variant::POOL_VECTOR2_ARRAY:
  5396. case Variant::TRANSFORM2D:
  5397. case Variant::RECT2: {
  5398. result.builtin_type = Variant::VECTOR2;
  5399. } break;
  5400. // Return Vector3
  5401. case Variant::POOL_VECTOR3_ARRAY:
  5402. case Variant::AABB:
  5403. case Variant::BASIS: {
  5404. result.builtin_type = Variant::VECTOR3;
  5405. } break;
  5406. // Depends on the index
  5407. case Variant::TRANSFORM:
  5408. case Variant::PLANE:
  5409. case Variant::COLOR:
  5410. default: {
  5411. result.has_type = false;
  5412. } break;
  5413. }
  5414. node_type = result;
  5415. }
  5416. } break;
  5417. default: {
  5418. _set_error("Parser bug: unhandled operation.", op->line);
  5419. ERR_FAIL_V(DataType());
  5420. }
  5421. }
  5422. } break;
  5423. default: {
  5424. }
  5425. }
  5426. p_node->set_datatype(_resolve_type(node_type, p_node->line));
  5427. return node_type;
  5428. }
  5429. 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 {
  5430. r_static = false;
  5431. r_default_arg_count = 0;
  5432. DataType original_type = p_base_type;
  5433. ClassNode *base = NULL;
  5434. FunctionNode *callee = NULL;
  5435. if (p_base_type.kind == DataType::CLASS) {
  5436. base = p_base_type.class_type;
  5437. }
  5438. // Look up the current file (parse tree)
  5439. while (!callee && base) {
  5440. for (int i = 0; i < base->static_functions.size(); i++) {
  5441. FunctionNode *func = base->static_functions[i];
  5442. if (p_function == func->name) {
  5443. r_static = true;
  5444. callee = func;
  5445. break;
  5446. }
  5447. }
  5448. if (!callee && !p_base_type.is_meta_type) {
  5449. for (int i = 0; i < base->functions.size(); i++) {
  5450. FunctionNode *func = base->functions[i];
  5451. if (p_function == func->name) {
  5452. callee = func;
  5453. break;
  5454. }
  5455. }
  5456. }
  5457. p_base_type = base->base_type;
  5458. if (p_base_type.kind == DataType::CLASS) {
  5459. base = p_base_type.class_type;
  5460. } else {
  5461. break;
  5462. }
  5463. }
  5464. if (callee) {
  5465. r_return_type = callee->get_datatype();
  5466. for (int i = 0; i < callee->argument_types.size(); i++) {
  5467. r_arg_types.push_back(callee->argument_types[i]);
  5468. }
  5469. r_default_arg_count = callee->default_values.size();
  5470. return true;
  5471. }
  5472. // Nothing in current file, check parent script
  5473. Ref<GDScript> base_gdscript;
  5474. Ref<Script> base_script;
  5475. StringName native;
  5476. if (p_base_type.kind == DataType::GDSCRIPT) {
  5477. base_gdscript = p_base_type.script_type;
  5478. if (base_gdscript.is_null() || !base_gdscript->is_valid()) {
  5479. // GDScript wasn't properly compíled, don't bother trying
  5480. return false;
  5481. }
  5482. } else if (p_base_type.kind == DataType::SCRIPT) {
  5483. base_script = p_base_type.script_type;
  5484. } else if (p_base_type.kind == DataType::NATIVE) {
  5485. native = p_base_type.native_type;
  5486. }
  5487. while (base_gdscript.is_valid()) {
  5488. native = base_gdscript->get_instance_base_type();
  5489. Map<StringName, GDScriptFunction *> funcs = base_gdscript->get_member_functions();
  5490. if (funcs.has(p_function)) {
  5491. GDScriptFunction *f = funcs[p_function];
  5492. r_static = f->is_static();
  5493. r_default_arg_count = f->get_default_argument_count();
  5494. r_return_type = _type_from_gdtype(f->get_return_type());
  5495. for (int i = 0; i < f->get_argument_count(); i++) {
  5496. r_arg_types.push_back(_type_from_gdtype(f->get_argument_type(i)));
  5497. }
  5498. return true;
  5499. }
  5500. base_gdscript = base_gdscript->get_base_script();
  5501. }
  5502. while (base_script.is_valid()) {
  5503. native = base_script->get_instance_base_type();
  5504. MethodInfo mi = base_script->get_method_info(p_function);
  5505. if (!(mi == MethodInfo())) {
  5506. r_return_type = _type_from_property(mi.return_val, false);
  5507. r_default_arg_count = mi.default_arguments.size();
  5508. for (List<PropertyInfo>::Element *E = mi.arguments.front(); E; E = E->next()) {
  5509. r_arg_types.push_back(_type_from_property(E->get()));
  5510. }
  5511. return true;
  5512. }
  5513. base_script = base_script->get_base_script();
  5514. }
  5515. if (native == StringName()) {
  5516. // Empty native class, might happen in some Script implementations
  5517. // Just ignore it
  5518. return false;
  5519. }
  5520. #ifdef DEBUG_METHODS_ENABLED
  5521. // Only native remains
  5522. if (!ClassDB::class_exists(native)) {
  5523. native = "_" + native.operator String();
  5524. }
  5525. if (!ClassDB::class_exists(native)) {
  5526. if (!check_types) return false;
  5527. ERR_EXPLAIN("Parser bug: Class '" + String(native) + "' not found.");
  5528. ERR_FAIL_V(false);
  5529. }
  5530. MethodBind *method = ClassDB::get_method(native, p_function);
  5531. if (!method) {
  5532. // Try virtual methods
  5533. List<MethodInfo> virtuals;
  5534. ClassDB::get_virtual_methods(native, &virtuals);
  5535. for (const List<MethodInfo>::Element *E = virtuals.front(); E; E = E->next()) {
  5536. const MethodInfo &mi = E->get();
  5537. if (mi.name == p_function) {
  5538. r_default_arg_count = mi.default_arguments.size();
  5539. for (const List<PropertyInfo>::Element *pi = mi.arguments.front(); pi; pi = pi->next()) {
  5540. r_arg_types.push_back(_type_from_property(pi->get()));
  5541. }
  5542. r_return_type = _type_from_property(mi.return_val, false);
  5543. r_vararg = mi.flags & METHOD_FLAG_VARARG;
  5544. return true;
  5545. }
  5546. }
  5547. // If the base is a script, it might be trying to access members of the Script class itself
  5548. if (original_type.is_meta_type && !(p_function == "new") && (original_type.kind == DataType::SCRIPT || original_type.kind == DataType::GDSCRIPT)) {
  5549. method = ClassDB::get_method(original_type.script_type->get_class_name(), p_function);
  5550. if (method) {
  5551. r_static = true;
  5552. } else {
  5553. // Try virtual methods of the script type
  5554. virtuals.clear();
  5555. ClassDB::get_virtual_methods(original_type.script_type->get_class_name(), &virtuals);
  5556. for (const List<MethodInfo>::Element *E = virtuals.front(); E; E = E->next()) {
  5557. const MethodInfo &mi = E->get();
  5558. if (mi.name == p_function) {
  5559. r_default_arg_count = mi.default_arguments.size();
  5560. for (const List<PropertyInfo>::Element *pi = mi.arguments.front(); pi; pi = pi->next()) {
  5561. r_arg_types.push_back(_type_from_property(pi->get()));
  5562. }
  5563. r_return_type = _type_from_property(mi.return_val, false);
  5564. r_static = true;
  5565. r_vararg = mi.flags & METHOD_FLAG_VARARG;
  5566. return true;
  5567. }
  5568. }
  5569. return false;
  5570. }
  5571. } else {
  5572. return false;
  5573. }
  5574. }
  5575. r_default_arg_count = method->get_default_argument_count();
  5576. r_return_type = _type_from_property(method->get_return_info(), false);
  5577. r_vararg = method->is_vararg();
  5578. for (int i = 0; i < method->get_argument_count(); i++) {
  5579. r_arg_types.push_back(_type_from_property(method->get_argument_info(i)));
  5580. }
  5581. return true;
  5582. #else
  5583. return false;
  5584. #endif
  5585. }
  5586. GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const OperatorNode *p_call) {
  5587. if (p_call->arguments.size() < 1) {
  5588. _set_error("Parser bug: function call without enough arguments.", p_call->line);
  5589. ERR_FAIL_V(DataType());
  5590. }
  5591. DataType return_type;
  5592. List<DataType> arg_types;
  5593. int default_args_count = 0;
  5594. int arg_count = p_call->arguments.size();
  5595. String callee_name;
  5596. bool is_vararg = false;
  5597. switch (p_call->arguments[0]->type) {
  5598. case GDScriptParser::Node::TYPE_TYPE: {
  5599. // Built-in constructor, special case
  5600. TypeNode *tn = static_cast<TypeNode *>(p_call->arguments[0]);
  5601. Vector<DataType> par_types;
  5602. par_types.resize(p_call->arguments.size() - 1);
  5603. for (int i = 1; i < p_call->arguments.size(); i++) {
  5604. par_types.write[i - 1] = _reduce_node_type(p_call->arguments[i]);
  5605. }
  5606. if (error_set) return DataType();
  5607. bool match = false;
  5608. List<MethodInfo> constructors;
  5609. Variant::get_constructor_list(tn->vtype, &constructors);
  5610. PropertyInfo return_type2;
  5611. for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
  5612. MethodInfo &mi = E->get();
  5613. if (p_call->arguments.size() - 1 < mi.arguments.size() - mi.default_arguments.size()) {
  5614. continue;
  5615. }
  5616. if (p_call->arguments.size() - 1 > mi.arguments.size()) {
  5617. continue;
  5618. }
  5619. bool types_match = true;
  5620. for (int i = 0; i < par_types.size(); i++) {
  5621. DataType arg_type;
  5622. if (mi.arguments[i].type != Variant::NIL) {
  5623. arg_type.has_type = true;
  5624. arg_type.kind = mi.arguments[i].type == Variant::OBJECT ? DataType::NATIVE : DataType::BUILTIN;
  5625. arg_type.builtin_type = mi.arguments[i].type;
  5626. arg_type.native_type = mi.arguments[i].class_name;
  5627. }
  5628. if (!_is_type_compatible(arg_type, par_types[i], true)) {
  5629. types_match = false;
  5630. break;
  5631. } else {
  5632. #ifdef DEBUG_ENABLED
  5633. 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) {
  5634. _add_warning(GDScriptWarning::NARROWING_CONVERSION, p_call->line, Variant::get_type_name(tn->vtype));
  5635. }
  5636. if (par_types[i].may_yield && p_call->arguments[i + 1]->type == Node::TYPE_OPERATOR) {
  5637. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, p_call->line, _find_function_name(static_cast<OperatorNode *>(p_call->arguments[i + 1])));
  5638. }
  5639. #endif // DEBUG_ENABLED
  5640. }
  5641. }
  5642. if (types_match) {
  5643. match = true;
  5644. return_type2 = mi.return_val;
  5645. break;
  5646. }
  5647. }
  5648. if (match) {
  5649. return _type_from_property(return_type2, false);
  5650. } else if (check_types) {
  5651. String err = "No constructor of '";
  5652. err += Variant::get_type_name(tn->vtype);
  5653. err += "' matches the signature '";
  5654. err += Variant::get_type_name(tn->vtype) + "(";
  5655. for (int i = 0; i < par_types.size(); i++) {
  5656. if (i > 0) err += ", ";
  5657. err += par_types[i].to_string();
  5658. }
  5659. err += ")'.";
  5660. _set_error(err, p_call->line, p_call->column);
  5661. return DataType();
  5662. }
  5663. return DataType();
  5664. } break;
  5665. case GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION: {
  5666. BuiltInFunctionNode *func = static_cast<BuiltInFunctionNode *>(p_call->arguments[0]);
  5667. MethodInfo mi = GDScriptFunctions::get_info(func->function);
  5668. return_type = _type_from_property(mi.return_val, false);
  5669. #ifdef DEBUG_ENABLED
  5670. // Check all arguments beforehand to solve warnings
  5671. for (int i = 1; i < p_call->arguments.size(); i++) {
  5672. _reduce_node_type(p_call->arguments[i]);
  5673. }
  5674. #endif // DEBUG_ENABLED
  5675. // Check arguments
  5676. is_vararg = mi.flags & METHOD_FLAG_VARARG;
  5677. default_args_count = mi.default_arguments.size();
  5678. callee_name = mi.name;
  5679. arg_count -= 1;
  5680. // Check each argument type
  5681. for (List<PropertyInfo>::Element *E = mi.arguments.front(); E; E = E->next()) {
  5682. arg_types.push_back(_type_from_property(E->get()));
  5683. }
  5684. } break;
  5685. default: {
  5686. if (p_call->op == OperatorNode::OP_CALL && p_call->arguments.size() < 2) {
  5687. _set_error("Parser bug: self method call without enough arguments.", p_call->line);
  5688. ERR_FAIL_V(DataType());
  5689. }
  5690. int arg_id = p_call->op == OperatorNode::OP_CALL ? 1 : 0;
  5691. if (p_call->arguments[arg_id]->type != Node::TYPE_IDENTIFIER) {
  5692. _set_error("Parser bug: invalid function call argument.", p_call->line);
  5693. ERR_FAIL_V(DataType());
  5694. }
  5695. #ifdef DEBUG_ENABLED
  5696. // Check all arguments beforehand to solve warnings
  5697. for (int i = arg_id + 1; i < p_call->arguments.size(); i++) {
  5698. _reduce_node_type(p_call->arguments[i]);
  5699. }
  5700. #endif // DEBUG_ENABLED
  5701. IdentifierNode *func_id = static_cast<IdentifierNode *>(p_call->arguments[arg_id]);
  5702. callee_name = func_id->name;
  5703. arg_count -= 1 + arg_id;
  5704. DataType base_type;
  5705. if (p_call->op == OperatorNode::OP_PARENT_CALL) {
  5706. base_type = current_class->base_type;
  5707. } else {
  5708. base_type = _reduce_node_type(p_call->arguments[0]);
  5709. }
  5710. if (!base_type.has_type || (base_type.kind == DataType::BUILTIN && base_type.builtin_type == Variant::NIL)) {
  5711. _mark_line_as_unsafe(p_call->line);
  5712. return DataType();
  5713. }
  5714. if (base_type.kind == DataType::BUILTIN) {
  5715. Variant::CallError err;
  5716. Variant tmp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  5717. if (check_types) {
  5718. if (!tmp.has_method(callee_name)) {
  5719. _set_error("Method '" + callee_name + "' is not declared on base '" + base_type.to_string() + "'.", p_call->line);
  5720. return DataType();
  5721. }
  5722. default_args_count = Variant::get_method_default_arguments(base_type.builtin_type, callee_name).size();
  5723. const Vector<Variant::Type> &var_arg_types = Variant::get_method_argument_types(base_type.builtin_type, callee_name);
  5724. for (int i = 0; i < var_arg_types.size(); i++) {
  5725. DataType argtype;
  5726. if (var_arg_types[i] != Variant::NIL) {
  5727. argtype.has_type = true;
  5728. argtype.kind = DataType::BUILTIN;
  5729. argtype.builtin_type = var_arg_types[i];
  5730. }
  5731. arg_types.push_back(argtype);
  5732. }
  5733. }
  5734. bool rets = false;
  5735. return_type.has_type = true;
  5736. return_type.kind = DataType::BUILTIN;
  5737. return_type.builtin_type = Variant::get_method_return_type(base_type.builtin_type, callee_name, &rets);
  5738. // If the method returns, but it might return any type, (Variant::NIL), pretend we don't know the type.
  5739. // At least make sure we know that it returns
  5740. if (rets && return_type.builtin_type == Variant::NIL) {
  5741. return_type.has_type = false;
  5742. }
  5743. break;
  5744. }
  5745. DataType original_type = base_type;
  5746. bool is_initializer = callee_name == "new";
  5747. bool is_static = false;
  5748. bool valid = false;
  5749. if (is_initializer && original_type.is_meta_type) {
  5750. // Try to check it as initializer
  5751. base_type = original_type;
  5752. callee_name = "_init";
  5753. base_type.is_meta_type = false;
  5754. valid = _get_function_signature(base_type, callee_name, return_type, arg_types,
  5755. default_args_count, is_static, is_vararg);
  5756. return_type = original_type;
  5757. return_type.is_meta_type = false;
  5758. valid = true; // There's always an initializer, we can assume this is true
  5759. }
  5760. if (!valid) {
  5761. base_type = original_type;
  5762. return_type = DataType();
  5763. valid = _get_function_signature(base_type, callee_name, return_type, arg_types,
  5764. default_args_count, is_static, is_vararg);
  5765. }
  5766. if (!valid) {
  5767. #ifdef DEBUG_ENABLED
  5768. if (p_call->arguments[0]->type == Node::TYPE_SELF) {
  5769. _set_error("Method '" + callee_name + "' is not declared in the current class.", p_call->line);
  5770. return DataType();
  5771. }
  5772. DataType tmp_type;
  5773. valid = _get_member_type(original_type, func_id->name, tmp_type);
  5774. if (valid) {
  5775. if (tmp_type.is_constant) {
  5776. _add_warning(GDScriptWarning::CONSTANT_USED_AS_FUNCTION, p_call->line, callee_name, original_type.to_string());
  5777. } else {
  5778. _add_warning(GDScriptWarning::PROPERTY_USED_AS_FUNCTION, p_call->line, callee_name, original_type.to_string());
  5779. }
  5780. }
  5781. _add_warning(GDScriptWarning::UNSAFE_METHOD_ACCESS, p_call->line, callee_name, original_type.to_string());
  5782. _mark_line_as_unsafe(p_call->line);
  5783. #endif // DEBUG_ENABLED
  5784. return DataType();
  5785. }
  5786. #ifdef DEBUG_ENABLED
  5787. if (current_function && !for_completion && !is_static && p_call->arguments[0]->type == Node::TYPE_SELF && current_function->_static) {
  5788. _set_error("Can't call non-static function from a static function.", p_call->line);
  5789. return DataType();
  5790. }
  5791. if (check_types && !is_static && !is_initializer && base_type.is_meta_type) {
  5792. _set_error("Non-static function '" + String(callee_name) + "' can only be called from an instance.", p_call->line);
  5793. return DataType();
  5794. }
  5795. // Check signal emission for warnings
  5796. 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) {
  5797. ConstantNode *sig = static_cast<ConstantNode *>(p_call->arguments[2]);
  5798. String emitted = sig->value.get_type() == Variant::STRING ? sig->value.operator String() : "";
  5799. for (int i = 0; i < current_class->_signals.size(); i++) {
  5800. if (current_class->_signals[i].name == emitted) {
  5801. current_class->_signals.write[i].emissions += 1;
  5802. break;
  5803. }
  5804. }
  5805. }
  5806. #endif // DEBUG_ENABLED
  5807. } break;
  5808. }
  5809. #ifdef DEBUG_ENABLED
  5810. if (!check_types) {
  5811. return return_type;
  5812. }
  5813. if (arg_count < arg_types.size() - default_args_count) {
  5814. _set_error("Too few arguments for '" + callee_name + "()' call. Expected at least " + itos(arg_types.size() - default_args_count) + ".", p_call->line);
  5815. return return_type;
  5816. }
  5817. if (!is_vararg && arg_count > arg_types.size()) {
  5818. _set_error("Too many arguments for '" + callee_name + "()' call. Expected at most " + itos(arg_types.size()) + ".", p_call->line);
  5819. return return_type;
  5820. }
  5821. int arg_diff = p_call->arguments.size() - arg_count;
  5822. for (int i = arg_diff; i < p_call->arguments.size(); i++) {
  5823. DataType par_type = _reduce_node_type(p_call->arguments[i]);
  5824. if ((i - arg_diff) >= arg_types.size()) {
  5825. continue;
  5826. }
  5827. DataType arg_type = arg_types[i - arg_diff];
  5828. if (!par_type.has_type) {
  5829. _mark_line_as_unsafe(p_call->line);
  5830. if (par_type.may_yield && p_call->arguments[i]->type == Node::TYPE_OPERATOR) {
  5831. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, p_call->line, _find_function_name(static_cast<OperatorNode *>(p_call->arguments[i])));
  5832. }
  5833. } else if (!_is_type_compatible(arg_types[i - arg_diff], par_type, true)) {
  5834. // Supertypes are acceptable for dynamic compliance
  5835. if (!_is_type_compatible(par_type, arg_types[i - arg_diff])) {
  5836. _set_error("At '" + callee_name + "()' call, argument " + itos(i - arg_diff + 1) + ". Assigned type (" +
  5837. par_type.to_string() + ") doesn't match the function argument's type (" +
  5838. arg_types[i - arg_diff].to_string() + ").",
  5839. p_call->line);
  5840. return DataType();
  5841. } else {
  5842. _mark_line_as_unsafe(p_call->line);
  5843. }
  5844. } else {
  5845. if (arg_type.kind == DataType::BUILTIN && arg_type.builtin_type == Variant::INT && par_type.kind == DataType::BUILTIN && par_type.builtin_type == Variant::REAL) {
  5846. _add_warning(GDScriptWarning::NARROWING_CONVERSION, p_call->line, callee_name);
  5847. }
  5848. }
  5849. }
  5850. #endif // DEBUG_ENABLED
  5851. return return_type;
  5852. }
  5853. bool GDScriptParser::_get_member_type(const DataType &p_base_type, const StringName &p_member, DataType &r_member_type) const {
  5854. DataType base_type = p_base_type;
  5855. // Check classes in current file
  5856. ClassNode *base = NULL;
  5857. if (base_type.kind == DataType::CLASS) {
  5858. base = base_type.class_type;
  5859. }
  5860. while (base) {
  5861. if (base->constant_expressions.has(p_member)) {
  5862. r_member_type = base->constant_expressions[p_member].expression->get_datatype();
  5863. return true;
  5864. }
  5865. if (!base_type.is_meta_type) {
  5866. for (int i = 0; i < base->variables.size(); i++) {
  5867. if (base->variables[i].identifier == p_member) {
  5868. r_member_type = base->variables[i].data_type;
  5869. base->variables.write[i].usages += 1;
  5870. return true;
  5871. }
  5872. }
  5873. } else {
  5874. for (int i = 0; i < base->subclasses.size(); i++) {
  5875. ClassNode *c = base->subclasses[i];
  5876. if (c->name == p_member) {
  5877. DataType class_type;
  5878. class_type.has_type = true;
  5879. class_type.is_constant = true;
  5880. class_type.is_meta_type = true;
  5881. class_type.kind = DataType::CLASS;
  5882. class_type.class_type = c;
  5883. r_member_type = class_type;
  5884. return true;
  5885. }
  5886. }
  5887. }
  5888. base_type = base->base_type;
  5889. if (base_type.kind == DataType::CLASS) {
  5890. base = base_type.class_type;
  5891. } else {
  5892. break;
  5893. }
  5894. }
  5895. Ref<GDScript> gds;
  5896. if (base_type.kind == DataType::GDSCRIPT) {
  5897. gds = base_type.script_type;
  5898. if (gds.is_null() || !gds->is_valid()) {
  5899. // GDScript wasn't properly compíled, don't bother trying
  5900. return false;
  5901. }
  5902. }
  5903. Ref<Script> scr;
  5904. if (base_type.kind == DataType::SCRIPT) {
  5905. scr = base_type.script_type;
  5906. }
  5907. StringName native;
  5908. if (base_type.kind == DataType::NATIVE) {
  5909. native = base_type.native_type;
  5910. }
  5911. // Check GDScripts
  5912. while (gds.is_valid()) {
  5913. if (gds->get_constants().has(p_member)) {
  5914. Variant c = gds->get_constants()[p_member];
  5915. r_member_type = _type_from_variant(c);
  5916. return true;
  5917. }
  5918. if (!base_type.is_meta_type) {
  5919. if (gds->get_members().has(p_member)) {
  5920. r_member_type = _type_from_gdtype(gds->get_member_type(p_member));
  5921. return true;
  5922. }
  5923. }
  5924. native = gds->get_instance_base_type();
  5925. if (gds->get_base_script().is_valid()) {
  5926. gds = gds->get_base_script();
  5927. scr = gds->get_base_script();
  5928. bool is_meta = base_type.is_meta_type;
  5929. base_type = _type_from_variant(scr.operator Variant());
  5930. base_type.is_meta_type = is_meta;
  5931. } else {
  5932. break;
  5933. }
  5934. }
  5935. // Check other script types
  5936. while (scr.is_valid()) {
  5937. Map<StringName, Variant> constants;
  5938. scr->get_constants(&constants);
  5939. if (constants.has(p_member)) {
  5940. r_member_type = _type_from_variant(constants[p_member]);
  5941. return true;
  5942. }
  5943. List<PropertyInfo> properties;
  5944. scr->get_script_property_list(&properties);
  5945. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  5946. if (E->get().name == p_member) {
  5947. r_member_type = _type_from_property(E->get());
  5948. return true;
  5949. }
  5950. }
  5951. base_type = _type_from_variant(scr.operator Variant());
  5952. native = scr->get_instance_base_type();
  5953. scr = scr->get_base_script();
  5954. }
  5955. if (native == StringName()) {
  5956. // Empty native class, might happen in some Script implementations
  5957. // Just ignore it
  5958. return false;
  5959. }
  5960. // Check ClassDB
  5961. if (!ClassDB::class_exists(native)) {
  5962. native = "_" + native.operator String();
  5963. }
  5964. if (!ClassDB::class_exists(native)) {
  5965. if (!check_types) return false;
  5966. ERR_EXPLAIN("Parser bug: Class '" + String(native) + "' not found.");
  5967. ERR_FAIL_V(false);
  5968. }
  5969. bool valid = false;
  5970. ClassDB::get_integer_constant(native, p_member, &valid);
  5971. if (valid) {
  5972. DataType ct;
  5973. ct.has_type = true;
  5974. ct.is_constant = true;
  5975. ct.kind = DataType::BUILTIN;
  5976. ct.builtin_type = Variant::INT;
  5977. r_member_type = ct;
  5978. return true;
  5979. }
  5980. if (!base_type.is_meta_type) {
  5981. List<PropertyInfo> properties;
  5982. ClassDB::get_property_list(native, &properties);
  5983. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  5984. if (E->get().name == p_member) {
  5985. // Check if a getter exists
  5986. StringName getter_name = ClassDB::get_property_getter(native, p_member);
  5987. if (getter_name != StringName()) {
  5988. // Use the getter return type
  5989. #ifdef DEBUG_METHODS_ENABLED
  5990. MethodBind *getter_method = ClassDB::get_method(native, getter_name);
  5991. if (getter_method) {
  5992. r_member_type = _type_from_property(getter_method->get_return_info());
  5993. } else {
  5994. r_member_type = DataType();
  5995. }
  5996. #else
  5997. r_member_type = DataType();
  5998. #endif
  5999. } else {
  6000. r_member_type = _type_from_property(E->get());
  6001. }
  6002. return true;
  6003. }
  6004. }
  6005. }
  6006. // If the base is a script, it might be trying to access members of the Script class itself
  6007. if (p_base_type.is_meta_type && (p_base_type.kind == DataType::SCRIPT || p_base_type.kind == DataType::GDSCRIPT)) {
  6008. native = p_base_type.script_type->get_class_name();
  6009. ClassDB::get_integer_constant(native, p_member, &valid);
  6010. if (valid) {
  6011. DataType ct;
  6012. ct.has_type = true;
  6013. ct.is_constant = true;
  6014. ct.kind = DataType::BUILTIN;
  6015. ct.builtin_type = Variant::INT;
  6016. r_member_type = ct;
  6017. return true;
  6018. }
  6019. List<PropertyInfo> properties;
  6020. ClassDB::get_property_list(native, &properties);
  6021. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  6022. if (E->get().name == p_member) {
  6023. // Check if a getter exists
  6024. StringName getter_name = ClassDB::get_property_getter(native, p_member);
  6025. if (getter_name != StringName()) {
  6026. // Use the getter return type
  6027. #ifdef DEBUG_METHODS_ENABLED
  6028. MethodBind *getter_method = ClassDB::get_method(native, getter_name);
  6029. if (getter_method) {
  6030. r_member_type = _type_from_property(getter_method->get_return_info());
  6031. } else {
  6032. r_member_type = DataType();
  6033. }
  6034. #else
  6035. r_member_type = DataType();
  6036. #endif
  6037. } else {
  6038. r_member_type = _type_from_property(E->get());
  6039. }
  6040. return true;
  6041. }
  6042. }
  6043. }
  6044. return false;
  6045. }
  6046. GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType *p_base_type, const StringName &p_identifier, int p_line, bool p_is_indexing) {
  6047. if (p_base_type && !p_base_type->has_type) {
  6048. return DataType();
  6049. }
  6050. DataType base_type;
  6051. DataType member_type;
  6052. if (!p_base_type) {
  6053. base_type.has_type = true;
  6054. base_type.is_constant = true;
  6055. base_type.kind = DataType::CLASS;
  6056. base_type.class_type = current_class;
  6057. } else {
  6058. base_type = DataType(*p_base_type);
  6059. }
  6060. if (_get_member_type(base_type, p_identifier, member_type)) {
  6061. return member_type;
  6062. }
  6063. if (p_is_indexing) {
  6064. // Don't look for globals since this is an indexed identifier
  6065. return DataType();
  6066. }
  6067. if (!p_base_type) {
  6068. // Possibly this is a global, check before failing
  6069. if (ClassDB::class_exists(p_identifier) || ClassDB::class_exists("_" + p_identifier.operator String())) {
  6070. DataType result;
  6071. result.has_type = true;
  6072. result.is_constant = true;
  6073. result.is_meta_type = true;
  6074. if (Engine::get_singleton()->has_singleton(p_identifier) || Engine::get_singleton()->has_singleton("_" + p_identifier.operator String())) {
  6075. result.is_meta_type = false;
  6076. }
  6077. result.kind = DataType::NATIVE;
  6078. result.native_type = p_identifier;
  6079. return result;
  6080. }
  6081. ClassNode *outer_class = current_class;
  6082. while (outer_class) {
  6083. if (outer_class->name == p_identifier) {
  6084. DataType result;
  6085. result.has_type = true;
  6086. result.is_constant = true;
  6087. result.is_meta_type = true;
  6088. result.kind = DataType::CLASS;
  6089. result.class_type = outer_class;
  6090. return result;
  6091. }
  6092. if (outer_class->constant_expressions.has(p_identifier)) {
  6093. return outer_class->constant_expressions[p_identifier].type;
  6094. }
  6095. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  6096. if (outer_class->subclasses[i] == current_class) {
  6097. continue;
  6098. }
  6099. if (outer_class->subclasses[i]->name == p_identifier) {
  6100. DataType result;
  6101. result.has_type = true;
  6102. result.is_constant = true;
  6103. result.is_meta_type = true;
  6104. result.kind = DataType::CLASS;
  6105. result.class_type = outer_class->subclasses[i];
  6106. return result;
  6107. }
  6108. }
  6109. outer_class = outer_class->owner;
  6110. }
  6111. if (ScriptServer::is_global_class(p_identifier)) {
  6112. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  6113. if (scr.is_valid()) {
  6114. DataType result;
  6115. result.has_type = true;
  6116. result.script_type = scr;
  6117. result.is_constant = true;
  6118. result.is_meta_type = true;
  6119. Ref<GDScript> gds = scr;
  6120. if (gds.is_valid()) {
  6121. if (!gds->is_valid()) {
  6122. _set_error("Class '" + p_identifier + "' could not be fully loaded (script error or cyclic dependency).");
  6123. return DataType();
  6124. }
  6125. result.kind = DataType::GDSCRIPT;
  6126. } else {
  6127. result.kind = DataType::SCRIPT;
  6128. }
  6129. return result;
  6130. }
  6131. _set_error("Class '" + p_identifier + "' was found in global scope but its script could not be loaded.");
  6132. return DataType();
  6133. }
  6134. if (GDScriptLanguage::get_singleton()->get_global_map().has(p_identifier)) {
  6135. int idx = GDScriptLanguage::get_singleton()->get_global_map()[p_identifier];
  6136. Variant g = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  6137. return _type_from_variant(g);
  6138. }
  6139. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier)) {
  6140. Variant g = GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier];
  6141. return _type_from_variant(g);
  6142. }
  6143. // Non-tool singletons aren't loaded, check project settings
  6144. List<PropertyInfo> props;
  6145. ProjectSettings::get_singleton()->get_property_list(&props);
  6146. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  6147. String s = E->get().name;
  6148. if (!s.begins_with("autoload/")) {
  6149. continue;
  6150. }
  6151. String name = s.get_slice("/", 1);
  6152. if (name == p_identifier) {
  6153. String script = ProjectSettings::get_singleton()->get(s);
  6154. if (script.begins_with("*")) {
  6155. script = script.right(1);
  6156. }
  6157. if (!script.begins_with("res://")) {
  6158. script = "res://" + script;
  6159. }
  6160. Ref<Script> singleton = ResourceLoader::load(script);
  6161. if (singleton.is_valid()) {
  6162. DataType result;
  6163. result.has_type = true;
  6164. result.is_constant = true;
  6165. result.script_type = singleton;
  6166. Ref<GDScript> gds = singleton;
  6167. if (gds.is_valid()) {
  6168. if (!gds->is_valid()) {
  6169. _set_error("Couldn't fully load singleton script '" + p_identifier + "' (possible cyclic reference or parse error).", p_line);
  6170. return DataType();
  6171. }
  6172. result.kind = DataType::GDSCRIPT;
  6173. } else {
  6174. result.kind = DataType::SCRIPT;
  6175. }
  6176. }
  6177. }
  6178. }
  6179. // This means looking in the current class, which type is always known
  6180. _set_error("Identifier '" + p_identifier.operator String() + "' is not declared in the current scope.", p_line);
  6181. }
  6182. #ifdef DEBUG_ENABLED
  6183. {
  6184. DataType tmp_type;
  6185. List<DataType> arg_types;
  6186. int argcount;
  6187. bool _static;
  6188. bool vararg;
  6189. if (_get_function_signature(base_type, p_identifier, tmp_type, arg_types, argcount, _static, vararg)) {
  6190. _add_warning(GDScriptWarning::FUNCTION_USED_AS_PROPERTY, p_line, p_identifier.operator String(), base_type.to_string());
  6191. }
  6192. }
  6193. #endif // DEBUG_ENABLED
  6194. _mark_line_as_unsafe(p_line);
  6195. return DataType();
  6196. }
  6197. void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
  6198. _mark_line_as_safe(p_class->line);
  6199. // Constants
  6200. for (Map<StringName, ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
  6201. ClassNode::Constant &c = E->get();
  6202. _mark_line_as_safe(c.expression->line);
  6203. DataType cont = _resolve_type(c.type, c.expression->line);
  6204. DataType expr = _resolve_type(c.expression->get_datatype(), c.expression->line);
  6205. if (check_types && !_is_type_compatible(cont, expr)) {
  6206. _set_error("Constant value type (" + expr.to_string() + ") is not compatible with declared type (" + cont.to_string() + ").",
  6207. c.expression->line);
  6208. return;
  6209. }
  6210. expr.is_constant = true;
  6211. c.type = expr;
  6212. c.expression->set_datatype(expr);
  6213. DataType tmp;
  6214. if (_get_member_type(p_class->base_type, E->key(), tmp)) {
  6215. _set_error("Member '" + String(E->key()) + "' already exists in parent class.", c.expression->line);
  6216. return;
  6217. }
  6218. }
  6219. // Function declarations
  6220. for (int i = 0; i < p_class->static_functions.size(); i++) {
  6221. _check_function_types(p_class->static_functions[i]);
  6222. if (error_set) return;
  6223. }
  6224. for (int i = 0; i < p_class->functions.size(); i++) {
  6225. _check_function_types(p_class->functions[i]);
  6226. if (error_set) return;
  6227. }
  6228. // Class variables
  6229. for (int i = 0; i < p_class->variables.size(); i++) {
  6230. ClassNode::Member &v = p_class->variables.write[i];
  6231. DataType tmp;
  6232. if (_get_member_type(p_class->base_type, v.identifier, tmp)) {
  6233. _set_error("Member '" + String(v.identifier) + "' already exists in parent class.", v.line);
  6234. return;
  6235. }
  6236. _mark_line_as_safe(v.line);
  6237. v.data_type = _resolve_type(v.data_type, v.line);
  6238. if (v.expression) {
  6239. DataType expr_type = _reduce_node_type(v.expression);
  6240. if (check_types && !_is_type_compatible(v.data_type, expr_type)) {
  6241. // Try supertype test
  6242. if (_is_type_compatible(expr_type, v.data_type)) {
  6243. _mark_line_as_unsafe(v.line);
  6244. } else {
  6245. // Try with implicit conversion
  6246. if (v.data_type.kind != DataType::BUILTIN || !_is_type_compatible(v.data_type, expr_type, true)) {
  6247. _set_error("Assigned expression type (" + expr_type.to_string() + ") doesn't match the variable's type (" +
  6248. v.data_type.to_string() + ").",
  6249. v.line);
  6250. return;
  6251. }
  6252. // Replace assignment with implicit conversion
  6253. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  6254. convert->line = v.line;
  6255. convert->function = GDScriptFunctions::TYPE_CONVERT;
  6256. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  6257. tgt_type->line = v.line;
  6258. tgt_type->value = (int)v.data_type.builtin_type;
  6259. OperatorNode *convert_call = alloc_node<OperatorNode>();
  6260. convert_call->line = v.line;
  6261. convert_call->op = OperatorNode::OP_CALL;
  6262. convert_call->arguments.push_back(convert);
  6263. convert_call->arguments.push_back(v.expression);
  6264. convert_call->arguments.push_back(tgt_type);
  6265. v.expression = convert_call;
  6266. v.initial_assignment->arguments.write[1] = convert_call;
  6267. }
  6268. }
  6269. if (v.data_type.infer_type) {
  6270. if (!expr_type.has_type) {
  6271. _set_error("Assigned value does not have a set type, variable type cannot be inferred.", v.line);
  6272. return;
  6273. }
  6274. v.data_type = expr_type;
  6275. v.data_type.is_constant = false;
  6276. }
  6277. }
  6278. // Check export hint
  6279. if (v.data_type.has_type && v._export.type != Variant::NIL) {
  6280. DataType export_type = _type_from_property(v._export);
  6281. if (!_is_type_compatible(v.data_type, export_type, true)) {
  6282. _set_error("Export hint type (" + export_type.to_string() + ") doesn't match the variable's type (" +
  6283. v.data_type.to_string() + ").",
  6284. v.line);
  6285. return;
  6286. }
  6287. }
  6288. // Setter and getter
  6289. if (v.setter == StringName() && v.getter == StringName()) continue;
  6290. bool found_getter = false;
  6291. bool found_setter = false;
  6292. for (int j = 0; j < p_class->functions.size(); j++) {
  6293. if (v.setter == p_class->functions[j]->name) {
  6294. found_setter = true;
  6295. FunctionNode *setter = p_class->functions[j];
  6296. if (setter->get_required_argument_count() != 1 &&
  6297. !(setter->get_required_argument_count() == 0 && setter->default_values.size() > 0)) {
  6298. _set_error("Setter function needs to receive exactly 1 argument. See '" + setter->name +
  6299. "()' definition at line " + itos(setter->line) + ".",
  6300. v.line);
  6301. return;
  6302. }
  6303. if (!_is_type_compatible(v.data_type, setter->argument_types[0])) {
  6304. _set_error("Setter argument type (" + setter->argument_types[0].to_string() +
  6305. ") doesn't match the variable's type (" + v.data_type.to_string() + "). See '" +
  6306. setter->name + "()' definition at line " + itos(setter->line) + ".",
  6307. v.line);
  6308. return;
  6309. }
  6310. continue;
  6311. }
  6312. if (v.getter == p_class->functions[j]->name) {
  6313. found_getter = true;
  6314. FunctionNode *getter = p_class->functions[j];
  6315. if (getter->get_required_argument_count() != 0) {
  6316. _set_error("Getter function can't receive arguments. See '" + getter->name +
  6317. "()' definition at line " + itos(getter->line) + ".",
  6318. v.line);
  6319. return;
  6320. }
  6321. if (!_is_type_compatible(v.data_type, getter->get_datatype())) {
  6322. _set_error("Getter return type (" + getter->get_datatype().to_string() +
  6323. ") doesn't match the variable's type (" + v.data_type.to_string() +
  6324. "). See '" + getter->name + "()' definition at line " + itos(getter->line) + ".",
  6325. v.line);
  6326. return;
  6327. }
  6328. }
  6329. if (found_getter && found_setter) break;
  6330. }
  6331. if ((found_getter || v.getter == StringName()) && (found_setter || v.setter == StringName())) continue;
  6332. // Check for static functions
  6333. for (int j = 0; j < p_class->static_functions.size(); j++) {
  6334. if (v.setter == p_class->static_functions[j]->name) {
  6335. FunctionNode *setter = p_class->static_functions[j];
  6336. _set_error("Setter can't be a static function. See '" + setter->name + "()' definition at line " + itos(setter->line) + ".", v.line);
  6337. return;
  6338. }
  6339. if (v.getter == p_class->static_functions[j]->name) {
  6340. FunctionNode *getter = p_class->static_functions[j];
  6341. _set_error("Getter can't be a static function. See '" + getter->name + "()' definition at line " + itos(getter->line) + ".", v.line);
  6342. return;
  6343. }
  6344. }
  6345. if (!found_setter && v.setter != StringName()) {
  6346. _set_error("Setter function is not defined.", v.line);
  6347. return;
  6348. }
  6349. if (!found_getter && v.getter != StringName()) {
  6350. _set_error("Getter function is not defined.", v.line);
  6351. return;
  6352. }
  6353. }
  6354. // Inner classes
  6355. for (int i = 0; i < p_class->subclasses.size(); i++) {
  6356. current_class = p_class->subclasses[i];
  6357. _check_class_level_types(current_class);
  6358. if (error_set) return;
  6359. current_class = p_class;
  6360. }
  6361. }
  6362. void GDScriptParser::_check_function_types(FunctionNode *p_function) {
  6363. p_function->return_type = _resolve_type(p_function->return_type, p_function->line);
  6364. // Arguments
  6365. int defaults_ofs = p_function->arguments.size() - p_function->default_values.size();
  6366. for (int i = 0; i < p_function->arguments.size(); i++) {
  6367. if (i < defaults_ofs) {
  6368. p_function->argument_types.write[i] = _resolve_type(p_function->argument_types[i], p_function->line);
  6369. } else {
  6370. if (p_function->default_values[i - defaults_ofs]->type != Node::TYPE_OPERATOR) {
  6371. _set_error("Parser bug: invalid argument default value.", p_function->line, p_function->column);
  6372. return;
  6373. }
  6374. OperatorNode *op = static_cast<OperatorNode *>(p_function->default_values[i - defaults_ofs]);
  6375. if (op->op != OperatorNode::OP_ASSIGN || op->arguments.size() != 2) {
  6376. _set_error("Parser bug: invalid argument default value operation.", p_function->line);
  6377. return;
  6378. }
  6379. DataType def_type = _reduce_node_type(op->arguments[1]);
  6380. if (p_function->argument_types[i].infer_type) {
  6381. def_type.is_constant = false;
  6382. p_function->argument_types.write[i] = def_type;
  6383. } else {
  6384. p_function->return_type = _resolve_type(p_function->return_type, p_function->line);
  6385. if (!_is_type_compatible(p_function->argument_types[i], def_type, true)) {
  6386. String arg_name = p_function->arguments[i];
  6387. _set_error("Value type (" + def_type.to_string() + ") doesn't match the type of argument '" +
  6388. arg_name + "' (" + p_function->arguments[i] + ")",
  6389. p_function->line);
  6390. }
  6391. }
  6392. }
  6393. #ifdef DEBUG_ENABLED
  6394. if (p_function->arguments_usage[i] == 0 && !p_function->arguments[i].operator String().begins_with("_")) {
  6395. _add_warning(GDScriptWarning::UNUSED_ARGUMENT, p_function->line, p_function->name, p_function->arguments[i].operator String());
  6396. }
  6397. for (int j = 0; j < current_class->variables.size(); j++) {
  6398. if (current_class->variables[j].identifier == p_function->arguments[i]) {
  6399. _add_warning(GDScriptWarning::SHADOWED_VARIABLE, p_function->line, p_function->arguments[i], itos(current_class->variables[j].line));
  6400. }
  6401. }
  6402. #endif // DEBUG_ENABLED
  6403. }
  6404. if (!(p_function->name == "_init")) {
  6405. // Signature for the initializer may vary
  6406. #ifdef DEBUG_ENABLED
  6407. DataType return_type;
  6408. List<DataType> arg_types;
  6409. int default_arg_count = 0;
  6410. bool _static = false;
  6411. bool vararg = false;
  6412. DataType base_type = current_class->base_type;
  6413. if (_get_function_signature(base_type, p_function->name, return_type, arg_types, default_arg_count, _static, vararg)) {
  6414. bool valid = _static == p_function->_static;
  6415. valid = valid && return_type == p_function->return_type;
  6416. int argsize_diff = p_function->arguments.size() - arg_types.size();
  6417. valid = valid && argsize_diff >= 0;
  6418. valid = valid && p_function->default_values.size() >= default_arg_count + argsize_diff;
  6419. int i = 0;
  6420. for (List<DataType>::Element *E = arg_types.front(); valid && E; E = E->next()) {
  6421. valid = valid && E->get() == p_function->argument_types[i++];
  6422. }
  6423. if (!valid) {
  6424. String parent_signature = return_type.has_type ? return_type.to_string() : "Variant";
  6425. if (parent_signature == "null") {
  6426. parent_signature = "void";
  6427. }
  6428. parent_signature += " " + p_function->name + "(";
  6429. if (arg_types.size()) {
  6430. int j = 0;
  6431. for (List<DataType>::Element *E = arg_types.front(); E; E = E->next()) {
  6432. if (E != arg_types.front()) {
  6433. parent_signature += ", ";
  6434. }
  6435. String arg = E->get().to_string();
  6436. if (arg == "null" || arg == "var") {
  6437. arg = "Variant";
  6438. }
  6439. parent_signature += arg;
  6440. if (j == arg_types.size() - default_arg_count) {
  6441. parent_signature += "=default";
  6442. }
  6443. j++;
  6444. }
  6445. }
  6446. parent_signature += ")";
  6447. _set_error("Function signature doesn't match the parent. Parent signature is: '" + parent_signature + "'.", p_function->line);
  6448. return;
  6449. }
  6450. }
  6451. #endif // DEBUG_ENABLED
  6452. } else {
  6453. if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) {
  6454. _set_error("Constructor cannot return a value.", p_function->line);
  6455. return;
  6456. }
  6457. }
  6458. if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) {
  6459. if (!p_function->body->has_return) {
  6460. _set_error("Non-void function must return a value in all possible paths.", p_function->line);
  6461. return;
  6462. }
  6463. }
  6464. if (p_function->has_yield) {
  6465. // yield() will make the function return a GDScriptFunctionState, so the type is ambiguous
  6466. p_function->return_type.has_type = false;
  6467. p_function->return_type.may_yield = true;
  6468. }
  6469. #ifdef DEBUG_ENABLED
  6470. for (Map<StringName, LocalVarNode *>::Element *E = p_function->body->variables.front(); E; E = E->next()) {
  6471. LocalVarNode *lv = E->get();
  6472. for (int i = 0; i < current_class->variables.size(); i++) {
  6473. if (current_class->variables[i].identifier == lv->name) {
  6474. _add_warning(GDScriptWarning::SHADOWED_VARIABLE, lv->line, lv->name, itos(current_class->variables[i].line));
  6475. }
  6476. }
  6477. }
  6478. #endif // DEBUG_ENABLED
  6479. }
  6480. void GDScriptParser::_check_class_blocks_types(ClassNode *p_class) {
  6481. // Function blocks
  6482. for (int i = 0; i < p_class->static_functions.size(); i++) {
  6483. current_function = p_class->static_functions[i];
  6484. current_block = current_function->body;
  6485. _mark_line_as_safe(current_function->line);
  6486. _check_block_types(current_block);
  6487. current_block = NULL;
  6488. current_function = NULL;
  6489. if (error_set) return;
  6490. }
  6491. for (int i = 0; i < p_class->functions.size(); i++) {
  6492. current_function = p_class->functions[i];
  6493. current_block = current_function->body;
  6494. _mark_line_as_safe(current_function->line);
  6495. _check_block_types(current_block);
  6496. current_block = NULL;
  6497. current_function = NULL;
  6498. if (error_set) return;
  6499. }
  6500. #ifdef DEBUG_ENABLED
  6501. // Warnings
  6502. for (int i = 0; i < p_class->variables.size(); i++) {
  6503. if (p_class->variables[i].usages == 0) {
  6504. _add_warning(GDScriptWarning::UNUSED_CLASS_VARIABLE, p_class->variables[i].line, p_class->variables[i].identifier);
  6505. }
  6506. }
  6507. for (int i = 0; i < p_class->_signals.size(); i++) {
  6508. if (p_class->_signals[i].emissions == 0) {
  6509. _add_warning(GDScriptWarning::UNUSED_SIGNAL, p_class->_signals[i].line, p_class->_signals[i].name);
  6510. }
  6511. }
  6512. #endif // DEBUG_ENABLED
  6513. // Inner classes
  6514. for (int i = 0; i < p_class->subclasses.size(); i++) {
  6515. current_class = p_class->subclasses[i];
  6516. _check_class_blocks_types(current_class);
  6517. if (error_set) return;
  6518. current_class = p_class;
  6519. }
  6520. }
  6521. #ifdef DEBUG_ENABLED
  6522. static String _find_function_name(const GDScriptParser::OperatorNode *p_call) {
  6523. switch (p_call->arguments[0]->type) {
  6524. case GDScriptParser::Node::TYPE_TYPE: {
  6525. return Variant::get_type_name(static_cast<GDScriptParser::TypeNode *>(p_call->arguments[0])->vtype);
  6526. } break;
  6527. case GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION: {
  6528. return GDScriptFunctions::get_func_name(static_cast<GDScriptParser::BuiltInFunctionNode *>(p_call->arguments[0])->function);
  6529. } break;
  6530. default: {
  6531. int id_index = p_call->op == GDScriptParser::OperatorNode::OP_PARENT_CALL ? 0 : 1;
  6532. if (p_call->arguments.size() > id_index && p_call->arguments[id_index]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  6533. return static_cast<GDScriptParser::IdentifierNode *>(p_call->arguments[id_index])->name;
  6534. }
  6535. } break;
  6536. }
  6537. return String();
  6538. }
  6539. #endif // DEBUG_ENABLED
  6540. void GDScriptParser::_check_block_types(BlockNode *p_block) {
  6541. Node *last_var_assign = NULL;
  6542. // Check each statement
  6543. for (List<Node *>::Element *E = p_block->statements.front(); E; E = E->next()) {
  6544. Node *statement = E->get();
  6545. switch (statement->type) {
  6546. case Node::TYPE_NEWLINE:
  6547. case Node::TYPE_BREAKPOINT:
  6548. case Node::TYPE_ASSERT: {
  6549. // Nothing to do
  6550. } break;
  6551. case Node::TYPE_LOCAL_VAR: {
  6552. LocalVarNode *lv = static_cast<LocalVarNode *>(statement);
  6553. lv->datatype = _resolve_type(lv->datatype, lv->line);
  6554. _mark_line_as_safe(lv->line);
  6555. last_var_assign = lv->assign;
  6556. if (lv->assign) {
  6557. DataType assign_type = _reduce_node_type(lv->assign);
  6558. #ifdef DEBUG_ENABLED
  6559. if (assign_type.has_type && assign_type.kind == DataType::BUILTIN && assign_type.builtin_type == Variant::NIL) {
  6560. if (lv->assign->type == Node::TYPE_OPERATOR) {
  6561. OperatorNode *call = static_cast<OperatorNode *>(lv->assign);
  6562. if (call->op == OperatorNode::OP_CALL || call->op == OperatorNode::OP_PARENT_CALL) {
  6563. _add_warning(GDScriptWarning::VOID_ASSIGNMENT, lv->line, _find_function_name(call));
  6564. }
  6565. }
  6566. }
  6567. if (lv->datatype.has_type && assign_type.may_yield && lv->assign->type == Node::TYPE_OPERATOR) {
  6568. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, lv->line, _find_function_name(static_cast<OperatorNode *>(lv->assign)));
  6569. }
  6570. #endif // DEBUG_ENABLED
  6571. if (!_is_type_compatible(lv->datatype, assign_type)) {
  6572. // Try supertype test
  6573. if (_is_type_compatible(assign_type, lv->datatype)) {
  6574. _mark_line_as_unsafe(lv->line);
  6575. } else {
  6576. // Try implicit conversion
  6577. if (lv->datatype.kind != DataType::BUILTIN || !_is_type_compatible(lv->datatype, assign_type, true)) {
  6578. _set_error("Assigned value type (" + assign_type.to_string() + ") doesn't match the variable's type (" +
  6579. lv->datatype.to_string() + ").",
  6580. lv->line);
  6581. return;
  6582. }
  6583. // Replace assignment with implicit conversion
  6584. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  6585. convert->line = lv->line;
  6586. convert->function = GDScriptFunctions::TYPE_CONVERT;
  6587. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  6588. tgt_type->line = lv->line;
  6589. tgt_type->value = (int)lv->datatype.builtin_type;
  6590. OperatorNode *convert_call = alloc_node<OperatorNode>();
  6591. convert_call->line = lv->line;
  6592. convert_call->op = OperatorNode::OP_CALL;
  6593. convert_call->arguments.push_back(convert);
  6594. convert_call->arguments.push_back(lv->assign);
  6595. convert_call->arguments.push_back(tgt_type);
  6596. lv->assign = convert_call;
  6597. lv->assign_op->arguments.write[1] = convert_call;
  6598. #ifdef DEBUG_ENABLED
  6599. if (lv->datatype.builtin_type == Variant::INT && assign_type.builtin_type == Variant::REAL) {
  6600. _add_warning(GDScriptWarning::NARROWING_CONVERSION, lv->line);
  6601. }
  6602. #endif // DEBUG_ENABLED
  6603. }
  6604. }
  6605. if (lv->datatype.infer_type) {
  6606. if (!assign_type.has_type) {
  6607. _set_error("Assigned value does not have a set type, variable type cannot be inferred.", lv->line);
  6608. return;
  6609. }
  6610. lv->datatype = assign_type;
  6611. lv->datatype.is_constant = false;
  6612. }
  6613. if (lv->datatype.has_type && !assign_type.has_type) {
  6614. _mark_line_as_unsafe(lv->line);
  6615. }
  6616. }
  6617. } break;
  6618. case Node::TYPE_OPERATOR: {
  6619. OperatorNode *op = static_cast<OperatorNode *>(statement);
  6620. switch (op->op) {
  6621. case OperatorNode::OP_ASSIGN:
  6622. case OperatorNode::OP_ASSIGN_ADD:
  6623. case OperatorNode::OP_ASSIGN_SUB:
  6624. case OperatorNode::OP_ASSIGN_MUL:
  6625. case OperatorNode::OP_ASSIGN_DIV:
  6626. case OperatorNode::OP_ASSIGN_MOD:
  6627. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  6628. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  6629. case OperatorNode::OP_ASSIGN_BIT_AND:
  6630. case OperatorNode::OP_ASSIGN_BIT_OR:
  6631. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  6632. if (op->arguments.size() < 2) {
  6633. _set_error("Parser bug: operation without enough arguments.", op->line, op->column);
  6634. return;
  6635. }
  6636. if (op->arguments[1] == last_var_assign) {
  6637. // Assignment was already checked
  6638. break;
  6639. }
  6640. _mark_line_as_safe(op->line);
  6641. DataType lh_type = _reduce_node_type(op->arguments[0]);
  6642. if (error_set) {
  6643. return;
  6644. }
  6645. if (check_types) {
  6646. if (!lh_type.has_type) {
  6647. if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
  6648. _mark_line_as_unsafe(op->line);
  6649. }
  6650. }
  6651. if (lh_type.is_constant) {
  6652. _set_error("Cannot assign a new value to a constant.", op->line);
  6653. return;
  6654. }
  6655. }
  6656. DataType rh_type;
  6657. if (op->op != OperatorNode::OP_ASSIGN) {
  6658. // Validate operation
  6659. DataType arg_type = _reduce_node_type(op->arguments[1]);
  6660. if (!arg_type.has_type) {
  6661. _mark_line_as_unsafe(op->line);
  6662. break;
  6663. }
  6664. Variant::Operator oper = _get_variant_operation(op->op);
  6665. bool valid = false;
  6666. rh_type = _get_operation_type(oper, lh_type, arg_type, valid);
  6667. if (check_types && !valid) {
  6668. _set_error("Invalid operand types ('" + lh_type.to_string() + "' and '" + arg_type.to_string() +
  6669. "') to assignment operator '" + Variant::get_operator_name(oper) + "'.",
  6670. op->line);
  6671. return;
  6672. }
  6673. } else {
  6674. rh_type = _reduce_node_type(op->arguments[1]);
  6675. }
  6676. #ifdef DEBUG_ENABLED
  6677. if (rh_type.has_type && rh_type.kind == DataType::BUILTIN && rh_type.builtin_type == Variant::NIL) {
  6678. if (op->arguments[1]->type == Node::TYPE_OPERATOR) {
  6679. OperatorNode *call = static_cast<OperatorNode *>(op->arguments[1]);
  6680. if (call->op == OperatorNode::OP_CALL || call->op == OperatorNode::OP_PARENT_CALL) {
  6681. _add_warning(GDScriptWarning::VOID_ASSIGNMENT, op->line, _find_function_name(call));
  6682. }
  6683. }
  6684. }
  6685. if (lh_type.has_type && rh_type.may_yield && op->arguments[1]->type == Node::TYPE_OPERATOR) {
  6686. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, op->line, _find_function_name(static_cast<OperatorNode *>(op->arguments[1])));
  6687. }
  6688. #endif // DEBUG_ENABLED
  6689. if (check_types && !_is_type_compatible(lh_type, rh_type)) {
  6690. // Try supertype test
  6691. if (_is_type_compatible(rh_type, lh_type)) {
  6692. _mark_line_as_unsafe(op->line);
  6693. } else {
  6694. // Try implicit conversion
  6695. if (lh_type.kind != DataType::BUILTIN || !_is_type_compatible(lh_type, rh_type, true)) {
  6696. _set_error("Assigned value type (" + rh_type.to_string() + ") doesn't match the variable's type (" +
  6697. lh_type.to_string() + ").",
  6698. op->line);
  6699. return;
  6700. }
  6701. // Replace assignment with implicit conversion
  6702. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  6703. convert->line = op->line;
  6704. convert->function = GDScriptFunctions::TYPE_CONVERT;
  6705. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  6706. tgt_type->line = op->line;
  6707. tgt_type->value = (int)lh_type.builtin_type;
  6708. OperatorNode *convert_call = alloc_node<OperatorNode>();
  6709. convert_call->line = op->line;
  6710. convert_call->op = OperatorNode::OP_CALL;
  6711. convert_call->arguments.push_back(convert);
  6712. convert_call->arguments.push_back(op->arguments[1]);
  6713. convert_call->arguments.push_back(tgt_type);
  6714. op->arguments.write[1] = convert_call;
  6715. #ifdef DEBUG_ENABLED
  6716. if (lh_type.builtin_type == Variant::INT && rh_type.builtin_type == Variant::REAL) {
  6717. _add_warning(GDScriptWarning::NARROWING_CONVERSION, op->line);
  6718. }
  6719. #endif // DEBUG_ENABLED
  6720. }
  6721. }
  6722. #ifdef DEBUG_ENABLED
  6723. if (!rh_type.has_type && (op->op != OperatorNode::OP_ASSIGN || lh_type.has_type || op->arguments[0]->type == Node::TYPE_OPERATOR)) {
  6724. _mark_line_as_unsafe(op->line);
  6725. }
  6726. #endif // DEBUG_ENABLED
  6727. } break;
  6728. case OperatorNode::OP_CALL:
  6729. case OperatorNode::OP_PARENT_CALL: {
  6730. _mark_line_as_safe(op->line);
  6731. DataType func_type = _reduce_function_call_type(op);
  6732. #ifdef DEBUG_ENABLED
  6733. if (func_type.has_type && (func_type.kind != DataType::BUILTIN || func_type.builtin_type != Variant::NIL)) {
  6734. // Figure out function name for warning
  6735. String func_name = _find_function_name(op);
  6736. if (func_name.empty()) {
  6737. func_name = "<undetected name>";
  6738. }
  6739. _add_warning(GDScriptWarning::RETURN_VALUE_DISCARDED, op->line, func_name);
  6740. }
  6741. #endif // DEBUG_ENABLED
  6742. if (error_set) return;
  6743. } break;
  6744. case OperatorNode::OP_YIELD: {
  6745. _mark_line_as_safe(op->line);
  6746. _reduce_node_type(op);
  6747. } break;
  6748. default: {
  6749. _mark_line_as_safe(op->line);
  6750. _reduce_node_type(op); // Test for safety anyway
  6751. #ifdef DEBUG_ENABLED
  6752. _add_warning(GDScriptWarning::STANDALONE_EXPRESSION, statement->line);
  6753. #endif // DEBUG_ENABLED
  6754. }
  6755. }
  6756. } break;
  6757. case Node::TYPE_CONTROL_FLOW: {
  6758. ControlFlowNode *cf = static_cast<ControlFlowNode *>(statement);
  6759. _mark_line_as_safe(cf->line);
  6760. switch (cf->cf_type) {
  6761. case ControlFlowNode::CF_RETURN: {
  6762. DataType function_type = current_function->get_datatype();
  6763. DataType ret_type;
  6764. if (cf->arguments.size() > 0) {
  6765. ret_type = _reduce_node_type(cf->arguments[0]);
  6766. if (error_set) {
  6767. return;
  6768. }
  6769. }
  6770. if (!function_type.has_type) break;
  6771. if (function_type.kind == DataType::BUILTIN && function_type.builtin_type == Variant::NIL) {
  6772. // Return void, should not have arguments
  6773. if (cf->arguments.size() > 0) {
  6774. _set_error("Void function cannot return a value.", cf->line, cf->column);
  6775. return;
  6776. }
  6777. } else {
  6778. // Return something, cannot be empty
  6779. if (cf->arguments.size() == 0) {
  6780. _set_error("Non-void function must return a value.", cf->line, cf->column);
  6781. return;
  6782. }
  6783. if (!_is_type_compatible(function_type, ret_type)) {
  6784. _set_error("Returned value type (" + ret_type.to_string() + ") doesn't match the function return type (" +
  6785. function_type.to_string() + ").",
  6786. cf->line, cf->column);
  6787. return;
  6788. }
  6789. }
  6790. } break;
  6791. case ControlFlowNode::CF_MATCH: {
  6792. MatchNode *match_node = cf->match;
  6793. _transform_match_statment(match_node);
  6794. } break;
  6795. default: {
  6796. if (cf->body_else) {
  6797. _mark_line_as_safe(cf->body_else->line);
  6798. }
  6799. for (int i = 0; i < cf->arguments.size(); i++) {
  6800. _reduce_node_type(cf->arguments[i]);
  6801. }
  6802. } break;
  6803. }
  6804. } break;
  6805. case Node::TYPE_CONSTANT: {
  6806. ConstantNode *cn = static_cast<ConstantNode *>(statement);
  6807. // Strings are fine since they can be multiline comments
  6808. if (cn->value.get_type() == Variant::STRING) {
  6809. break;
  6810. }
  6811. FALLTHROUGH;
  6812. }
  6813. default: {
  6814. _mark_line_as_safe(statement->line);
  6815. _reduce_node_type(statement); // Test for safety anyway
  6816. #ifdef DEBUG_ENABLED
  6817. _add_warning(GDScriptWarning::STANDALONE_EXPRESSION, statement->line);
  6818. #endif // DEBUG_ENABLED
  6819. }
  6820. }
  6821. }
  6822. // Parse sub blocks
  6823. for (int i = 0; i < p_block->sub_blocks.size(); i++) {
  6824. current_block = p_block->sub_blocks[i];
  6825. _check_block_types(current_block);
  6826. current_block = p_block;
  6827. if (error_set) return;
  6828. }
  6829. #ifdef DEBUG_ENABLED
  6830. // Warnings check
  6831. for (Map<StringName, LocalVarNode *>::Element *E = p_block->variables.front(); E; E = E->next()) {
  6832. LocalVarNode *lv = E->get();
  6833. if (!lv->name.operator String().begins_with("_")) {
  6834. if (lv->usages == 0) {
  6835. _add_warning(GDScriptWarning::UNUSED_VARIABLE, lv->line, lv->name);
  6836. } else if (lv->assignments == 0) {
  6837. _add_warning(GDScriptWarning::UNASSIGNED_VARIABLE, lv->line, lv->name);
  6838. }
  6839. }
  6840. }
  6841. #endif // DEBUG_ENABLED
  6842. }
  6843. void GDScriptParser::_set_error(const String &p_error, int p_line, int p_column) {
  6844. if (error_set)
  6845. return; //allow no further errors
  6846. error = p_error;
  6847. error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
  6848. error_column = p_column < 0 ? tokenizer->get_token_column() : p_column;
  6849. error_set = true;
  6850. }
  6851. #ifdef DEBUG_ENABLED
  6852. 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) {
  6853. Vector<String> symbols;
  6854. if (!p_symbol1.empty()) {
  6855. symbols.push_back(p_symbol1);
  6856. }
  6857. if (!p_symbol2.empty()) {
  6858. symbols.push_back(p_symbol2);
  6859. }
  6860. if (!p_symbol3.empty()) {
  6861. symbols.push_back(p_symbol3);
  6862. }
  6863. if (!p_symbol4.empty()) {
  6864. symbols.push_back(p_symbol4);
  6865. }
  6866. _add_warning(p_code, p_line, symbols);
  6867. }
  6868. void GDScriptParser::_add_warning(int p_code, int p_line, const Vector<String> &p_symbols) {
  6869. if (tokenizer->is_ignoring_warnings() || !GLOBAL_GET("debug/gdscript/warnings/enable").booleanize()) {
  6870. return;
  6871. }
  6872. String warn_name = GDScriptWarning::get_name_from_code((GDScriptWarning::Code)p_code).to_lower();
  6873. if (tokenizer->get_warning_global_skips().has(warn_name)) {
  6874. return;
  6875. }
  6876. if (!GLOBAL_GET("debug/gdscript/warnings/" + warn_name)) {
  6877. return;
  6878. }
  6879. GDScriptWarning warn;
  6880. warn.code = (GDScriptWarning::Code)p_code;
  6881. warn.symbols = p_symbols;
  6882. warn.line = p_line == -1 ? tokenizer->get_token_line() : p_line;
  6883. List<GDScriptWarning>::Element *before = NULL;
  6884. for (List<GDScriptWarning>::Element *E = warnings.front(); E; E = E->next()) {
  6885. if (E->get().line > warn.line) {
  6886. break;
  6887. }
  6888. before = E;
  6889. }
  6890. if (before) {
  6891. warnings.insert_after(before, warn);
  6892. } else {
  6893. warnings.push_front(warn);
  6894. }
  6895. }
  6896. #endif // DEBUG_ENABLED
  6897. String GDScriptParser::get_error() const {
  6898. return error;
  6899. }
  6900. int GDScriptParser::get_error_line() const {
  6901. return error_line;
  6902. }
  6903. int GDScriptParser::get_error_column() const {
  6904. return error_column;
  6905. }
  6906. Error GDScriptParser::_parse(const String &p_base_path) {
  6907. base_path = p_base_path;
  6908. //assume class
  6909. ClassNode *main_class = alloc_node<ClassNode>();
  6910. main_class->initializer = alloc_node<BlockNode>();
  6911. main_class->initializer->parent_class = main_class;
  6912. main_class->ready = alloc_node<BlockNode>();
  6913. main_class->ready->parent_class = main_class;
  6914. current_class = main_class;
  6915. _parse_class(main_class);
  6916. if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) {
  6917. error_set = false;
  6918. _set_error("Parse Error: " + tokenizer->get_token_error());
  6919. }
  6920. if (error_set && !for_completion) {
  6921. return ERR_PARSE_ERROR;
  6922. }
  6923. if (dependencies_only) {
  6924. return OK;
  6925. }
  6926. _determine_inheritance(main_class);
  6927. if (error_set) {
  6928. return ERR_PARSE_ERROR;
  6929. }
  6930. current_class = main_class;
  6931. current_function = NULL;
  6932. current_block = NULL;
  6933. #ifdef DEBUG_ENABLED
  6934. if (for_completion) check_types = false;
  6935. #else
  6936. check_types = false;
  6937. #endif
  6938. // Resolve all class-level stuff before getting into function blocks
  6939. _check_class_level_types(main_class);
  6940. if (error_set) {
  6941. return ERR_PARSE_ERROR;
  6942. }
  6943. // Resolve the function blocks
  6944. _check_class_blocks_types(main_class);
  6945. if (error_set) {
  6946. return ERR_PARSE_ERROR;
  6947. }
  6948. #ifdef DEBUG_ENABLED
  6949. // Resolve warning ignores
  6950. Vector<Pair<int, String> > warning_skips = tokenizer->get_warning_skips();
  6951. bool warning_is_error = GLOBAL_GET("debug/gdscript/warnings/treat_warnings_as_errors").booleanize();
  6952. for (List<GDScriptWarning>::Element *E = warnings.front(); E;) {
  6953. GDScriptWarning &w = E->get();
  6954. int skip_index = -1;
  6955. for (int i = 0; i < warning_skips.size(); i++) {
  6956. if (warning_skips[i].first >= w.line) {
  6957. break;
  6958. }
  6959. skip_index = i;
  6960. }
  6961. List<GDScriptWarning>::Element *next = E->next();
  6962. bool erase = false;
  6963. if (skip_index != -1) {
  6964. if (warning_skips[skip_index].second == GDScriptWarning::get_name_from_code(w.code).to_lower()) {
  6965. erase = true;
  6966. }
  6967. warning_skips.remove(skip_index);
  6968. }
  6969. if (erase) {
  6970. warnings.erase(E);
  6971. } else if (warning_is_error) {
  6972. _set_error(w.get_message() + " (warning treated as error)", w.line);
  6973. return ERR_PARSE_ERROR;
  6974. }
  6975. E = next;
  6976. }
  6977. #endif // DEBUG_ENABLED
  6978. return OK;
  6979. }
  6980. Error GDScriptParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
  6981. clear();
  6982. self_path = p_self_path;
  6983. GDScriptTokenizerBuffer *tb = memnew(GDScriptTokenizerBuffer);
  6984. tb->set_code_buffer(p_bytecode);
  6985. tokenizer = tb;
  6986. Error ret = _parse(p_base_path);
  6987. memdelete(tb);
  6988. tokenizer = NULL;
  6989. return ret;
  6990. }
  6991. 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) {
  6992. clear();
  6993. self_path = p_self_path;
  6994. GDScriptTokenizerText *tt = memnew(GDScriptTokenizerText);
  6995. tt->set_code(p_code);
  6996. validating = p_just_validate;
  6997. for_completion = p_for_completion;
  6998. dependencies_only = p_dependencies_only;
  6999. #ifdef DEBUG_ENABLED
  7000. safe_lines = r_safe_lines;
  7001. #endif // DEBUG_ENABLED
  7002. tokenizer = tt;
  7003. Error ret = _parse(p_base_path);
  7004. memdelete(tt);
  7005. tokenizer = NULL;
  7006. return ret;
  7007. }
  7008. bool GDScriptParser::is_tool_script() const {
  7009. return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
  7010. }
  7011. const GDScriptParser::Node *GDScriptParser::get_parse_tree() const {
  7012. return head;
  7013. }
  7014. void GDScriptParser::clear() {
  7015. while (list) {
  7016. Node *l = list;
  7017. list = list->next;
  7018. memdelete(l);
  7019. }
  7020. head = NULL;
  7021. list = NULL;
  7022. completion_type = COMPLETION_NONE;
  7023. completion_node = NULL;
  7024. completion_class = NULL;
  7025. completion_function = NULL;
  7026. completion_block = NULL;
  7027. current_block = NULL;
  7028. current_class = NULL;
  7029. completion_found = false;
  7030. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  7031. current_function = NULL;
  7032. validating = false;
  7033. for_completion = false;
  7034. error_set = false;
  7035. tab_level.clear();
  7036. tab_level.push_back(0);
  7037. error_line = 0;
  7038. error_column = 0;
  7039. pending_newline = -1;
  7040. parenthesis = 0;
  7041. current_export.type = Variant::NIL;
  7042. check_types = true;
  7043. dependencies_only = false;
  7044. dependencies.clear();
  7045. error = "";
  7046. #ifdef DEBUG_ENABLED
  7047. safe_lines = NULL;
  7048. #endif // DEBUG_ENABLED
  7049. }
  7050. GDScriptParser::CompletionType GDScriptParser::get_completion_type() {
  7051. return completion_type;
  7052. }
  7053. StringName GDScriptParser::get_completion_cursor() {
  7054. return completion_cursor;
  7055. }
  7056. int GDScriptParser::get_completion_line() {
  7057. return completion_line;
  7058. }
  7059. Variant::Type GDScriptParser::get_completion_built_in_constant() {
  7060. return completion_built_in_constant;
  7061. }
  7062. GDScriptParser::Node *GDScriptParser::get_completion_node() {
  7063. return completion_node;
  7064. }
  7065. GDScriptParser::BlockNode *GDScriptParser::get_completion_block() {
  7066. return completion_block;
  7067. }
  7068. GDScriptParser::ClassNode *GDScriptParser::get_completion_class() {
  7069. return completion_class;
  7070. }
  7071. GDScriptParser::FunctionNode *GDScriptParser::get_completion_function() {
  7072. return completion_function;
  7073. }
  7074. int GDScriptParser::get_completion_argument_index() {
  7075. return completion_argument;
  7076. }
  7077. int GDScriptParser::get_completion_identifier_is_function() {
  7078. return completion_ident_is_call;
  7079. }
  7080. GDScriptParser::GDScriptParser() {
  7081. head = NULL;
  7082. list = NULL;
  7083. tokenizer = NULL;
  7084. pending_newline = -1;
  7085. clear();
  7086. }
  7087. GDScriptParser::~GDScriptParser() {
  7088. clear();
  7089. }