gdscript_parser.cpp 259 KB

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