gdscript_parser.cpp 260 KB

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