gdscript_parser.cpp 261 KB

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