gdscript_parser.cpp 233 KB

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