gdscript_parser.cpp 233 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761
  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_EOF:
  2679. p_class->end_line = tokenizer->get_token_line();
  2680. case GDScriptTokenizer::TK_ERROR: {
  2681. return; //go back
  2682. //end of file!
  2683. } break;
  2684. case GDScriptTokenizer::TK_NEWLINE: {
  2685. if (!_parse_newline()) {
  2686. if (!error_set) {
  2687. p_class->end_line = tokenizer->get_token_line();
  2688. }
  2689. return;
  2690. }
  2691. } break;
  2692. case GDScriptTokenizer::TK_PR_EXTENDS: {
  2693. _mark_line_as_safe(tokenizer->get_token_line());
  2694. _parse_extends(p_class);
  2695. if (error_set)
  2696. return;
  2697. if (!_end_statement()) {
  2698. _set_error("Expected end of statement after extends");
  2699. return;
  2700. }
  2701. } break;
  2702. case GDScriptTokenizer::TK_PR_CLASS_NAME: {
  2703. if (p_class->owner) {
  2704. _set_error("'class_name' is only valid for the main class namespace.");
  2705. return;
  2706. }
  2707. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) {
  2708. _set_error("'class_name' syntax: 'class_name <UniqueName>'");
  2709. return;
  2710. }
  2711. p_class->name = tokenizer->get_token_identifier(1);
  2712. if (self_path != String() && ScriptServer::is_global_class(p_class->name) && ScriptServer::get_global_class_path(p_class->name) != self_path) {
  2713. _set_error("Unique global class '" + p_class->name + "' already exists at path: " + ScriptServer::get_global_class_path(p_class->name));
  2714. return;
  2715. }
  2716. if (ClassDB::class_exists(p_class->name)) {
  2717. _set_error("Class '" + p_class->name + "' shadows a native class.");
  2718. return;
  2719. }
  2720. tokenizer->advance(2);
  2721. } break;
  2722. case GDScriptTokenizer::TK_PR_TOOL: {
  2723. if (p_class->tool) {
  2724. _set_error("tool used more than once");
  2725. return;
  2726. }
  2727. p_class->tool = true;
  2728. tokenizer->advance();
  2729. } break;
  2730. case GDScriptTokenizer::TK_PR_CLASS: {
  2731. //class inside class :D
  2732. StringName name;
  2733. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) {
  2734. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  2735. return;
  2736. }
  2737. name = tokenizer->get_token_identifier(1);
  2738. tokenizer->advance(2);
  2739. // Check if name is shadowing something else
  2740. if (ClassDB::class_exists(name) || ClassDB::class_exists("_" + name.operator String())) {
  2741. _set_error("Class '" + String(name) + "' shadows a native class.");
  2742. return;
  2743. }
  2744. if (ScriptServer::is_global_class(name)) {
  2745. _set_error("Can't override name of unique global class '" + name + "' already exists at path: " + ScriptServer::get_global_class_path(p_class->name));
  2746. return;
  2747. }
  2748. ClassNode *outer_class = p_class;
  2749. while (outer_class) {
  2750. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  2751. if (outer_class->subclasses[i]->name == name) {
  2752. _set_error("Another class named '" + String(name) + "' already exists in this scope (at line " + itos(outer_class->subclasses[i]->line) + ").");
  2753. return;
  2754. }
  2755. }
  2756. if (outer_class->constant_expressions.has(name)) {
  2757. _set_error("A constant named '" + String(name) + "' already exists in the outer class scope (at line" + itos(outer_class->constant_expressions[name].expression->line) + ").");
  2758. return;
  2759. }
  2760. outer_class = outer_class->owner;
  2761. }
  2762. ClassNode *newclass = alloc_node<ClassNode>();
  2763. newclass->initializer = alloc_node<BlockNode>();
  2764. newclass->initializer->parent_class = newclass;
  2765. newclass->ready = alloc_node<BlockNode>();
  2766. newclass->ready->parent_class = newclass;
  2767. newclass->name = name;
  2768. newclass->owner = p_class;
  2769. p_class->subclasses.push_back(newclass);
  2770. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_EXTENDS) {
  2771. _parse_extends(newclass);
  2772. if (error_set)
  2773. return;
  2774. }
  2775. if (!_enter_indent_block()) {
  2776. _set_error("Indented block expected.");
  2777. return;
  2778. }
  2779. current_class = newclass;
  2780. _parse_class(newclass);
  2781. current_class = p_class;
  2782. } break;
  2783. /* this is for functions....
  2784. case GDScriptTokenizer::TK_CF_PASS: {
  2785. tokenizer->advance(1);
  2786. } break;
  2787. */
  2788. case GDScriptTokenizer::TK_PR_STATIC: {
  2789. tokenizer->advance();
  2790. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  2791. _set_error("Expected 'func'.");
  2792. return;
  2793. }
  2794. }; //fallthrough to function
  2795. case GDScriptTokenizer::TK_PR_FUNCTION: {
  2796. bool _static = false;
  2797. pending_newline = -1;
  2798. if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_STATIC) {
  2799. _static = true;
  2800. }
  2801. tokenizer->advance();
  2802. StringName name;
  2803. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC, name)) {
  2804. }
  2805. if (name == StringName()) {
  2806. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  2807. return;
  2808. }
  2809. for (int i = 0; i < p_class->functions.size(); i++) {
  2810. if (p_class->functions[i]->name == name) {
  2811. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->functions[i]->line) + ").");
  2812. }
  2813. }
  2814. for (int i = 0; i < p_class->static_functions.size(); i++) {
  2815. if (p_class->static_functions[i]->name == name) {
  2816. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->static_functions[i]->line) + ").");
  2817. }
  2818. }
  2819. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2820. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  2821. return;
  2822. }
  2823. tokenizer->advance();
  2824. Vector<StringName> arguments;
  2825. Vector<DataType> argument_types;
  2826. Vector<Node *> default_values;
  2827. int fnline = tokenizer->get_token_line();
  2828. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2829. //has arguments
  2830. bool defaulting = false;
  2831. while (true) {
  2832. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2833. tokenizer->advance();
  2834. continue;
  2835. }
  2836. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_VAR) {
  2837. tokenizer->advance(); //var before the identifier is allowed
  2838. }
  2839. if (!tokenizer->is_token_literal(0, true)) {
  2840. _set_error("Expected identifier for argument.");
  2841. return;
  2842. }
  2843. StringName argname = tokenizer->get_token_identifier();
  2844. arguments.push_back(argname);
  2845. tokenizer->advance();
  2846. DataType argtype;
  2847. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  2848. if (!_parse_type(argtype)) {
  2849. _set_error("Expected type for argument.");
  2850. return;
  2851. }
  2852. }
  2853. argument_types.push_back(argtype);
  2854. if (defaulting && tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
  2855. _set_error("Default parameter expected.");
  2856. return;
  2857. }
  2858. //tokenizer->advance();
  2859. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  2860. defaulting = true;
  2861. tokenizer->advance(1);
  2862. Node *defval = _parse_and_reduce_expression(p_class, _static);
  2863. if (!defval || error_set)
  2864. return;
  2865. OperatorNode *on = alloc_node<OperatorNode>();
  2866. on->op = OperatorNode::OP_ASSIGN;
  2867. on->line = fnline;
  2868. IdentifierNode *in = alloc_node<IdentifierNode>();
  2869. in->name = argname;
  2870. in->line = fnline;
  2871. on->arguments.push_back(in);
  2872. on->arguments.push_back(defval);
  2873. /* no ..
  2874. if (defval->type!=Node::TYPE_CONSTANT) {
  2875. _set_error("default argument must be constant");
  2876. }
  2877. */
  2878. default_values.push_back(on);
  2879. }
  2880. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2881. tokenizer->advance();
  2882. }
  2883. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2884. tokenizer->advance();
  2885. continue;
  2886. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2887. _set_error("Expected ',' or ')'.");
  2888. return;
  2889. }
  2890. break;
  2891. }
  2892. }
  2893. tokenizer->advance();
  2894. BlockNode *block = alloc_node<BlockNode>();
  2895. block->parent_class = p_class;
  2896. if (name == "_init") {
  2897. if (_static) {
  2898. _set_error("Constructor cannot be static.");
  2899. return;
  2900. }
  2901. if (p_class->extends_used) {
  2902. OperatorNode *cparent = alloc_node<OperatorNode>();
  2903. cparent->op = OperatorNode::OP_PARENT_CALL;
  2904. block->statements.push_back(cparent);
  2905. IdentifierNode *id = alloc_node<IdentifierNode>();
  2906. id->name = "_init";
  2907. cparent->arguments.push_back(id);
  2908. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  2909. tokenizer->advance();
  2910. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2911. _set_error("expected '(' for parent constructor arguments.");
  2912. return;
  2913. }
  2914. tokenizer->advance();
  2915. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2916. //has arguments
  2917. parenthesis++;
  2918. while (true) {
  2919. Node *arg = _parse_and_reduce_expression(p_class, _static);
  2920. cparent->arguments.push_back(arg);
  2921. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2922. tokenizer->advance();
  2923. continue;
  2924. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2925. _set_error("Expected ',' or ')'.");
  2926. return;
  2927. }
  2928. break;
  2929. }
  2930. parenthesis--;
  2931. }
  2932. tokenizer->advance();
  2933. }
  2934. } else {
  2935. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  2936. _set_error("Parent constructor call found for a class without inheritance.");
  2937. return;
  2938. }
  2939. }
  2940. }
  2941. DataType return_type;
  2942. if (tokenizer->get_token() == GDScriptTokenizer::TK_FORWARD_ARROW) {
  2943. if (!_parse_type(return_type, true)) {
  2944. _set_error("Expected return type for function.");
  2945. return;
  2946. }
  2947. }
  2948. if (!_enter_indent_block(block)) {
  2949. _set_error("Indented block expected.");
  2950. return;
  2951. }
  2952. FunctionNode *function = alloc_node<FunctionNode>();
  2953. function->name = name;
  2954. function->return_type = return_type;
  2955. function->arguments = arguments;
  2956. function->argument_types = argument_types;
  2957. function->default_values = default_values;
  2958. function->_static = _static;
  2959. function->line = fnline;
  2960. function->rpc_mode = rpc_mode;
  2961. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  2962. if (_static)
  2963. p_class->static_functions.push_back(function);
  2964. else
  2965. p_class->functions.push_back(function);
  2966. current_function = function;
  2967. function->body = block;
  2968. current_block = block;
  2969. _parse_block(block, _static);
  2970. current_block = NULL;
  2971. //arguments
  2972. } break;
  2973. case GDScriptTokenizer::TK_PR_SIGNAL: {
  2974. tokenizer->advance();
  2975. if (!tokenizer->is_token_literal()) {
  2976. _set_error("Expected identifier after 'signal'.");
  2977. return;
  2978. }
  2979. ClassNode::Signal sig;
  2980. sig.name = tokenizer->get_token_identifier();
  2981. tokenizer->advance();
  2982. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2983. tokenizer->advance();
  2984. while (true) {
  2985. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2986. tokenizer->advance();
  2987. continue;
  2988. }
  2989. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2990. tokenizer->advance();
  2991. break;
  2992. }
  2993. if (!tokenizer->is_token_literal(0, true)) {
  2994. _set_error("Expected identifier in signal argument.");
  2995. return;
  2996. }
  2997. sig.arguments.push_back(tokenizer->get_token_identifier());
  2998. tokenizer->advance();
  2999. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  3000. tokenizer->advance();
  3001. }
  3002. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3003. tokenizer->advance();
  3004. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3005. _set_error("Expected ',' or ')' after signal parameter identifier.");
  3006. return;
  3007. }
  3008. }
  3009. }
  3010. p_class->_signals.push_back(sig);
  3011. if (!_end_statement()) {
  3012. _set_error("Expected end of statement (signal)");
  3013. return;
  3014. }
  3015. } break;
  3016. case GDScriptTokenizer::TK_PR_EXPORT: {
  3017. tokenizer->advance();
  3018. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  3019. tokenizer->advance();
  3020. String hint_prefix = "";
  3021. bool is_arrayed = false;
  3022. while (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE &&
  3023. tokenizer->get_token_type() == Variant::ARRAY &&
  3024. tokenizer->get_token(1) == GDScriptTokenizer::TK_COMMA) {
  3025. tokenizer->advance(); // Array
  3026. tokenizer->advance(); // Comma
  3027. if (is_arrayed) {
  3028. hint_prefix += itos(Variant::ARRAY) + ":";
  3029. } else {
  3030. is_arrayed = true;
  3031. }
  3032. }
  3033. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE) {
  3034. Variant::Type type = tokenizer->get_token_type();
  3035. if (type == Variant::NIL) {
  3036. _set_error("Can't export null type.");
  3037. return;
  3038. }
  3039. if (type == Variant::OBJECT) {
  3040. _set_error("Can't export raw object type.");
  3041. return;
  3042. }
  3043. current_export.type = type;
  3044. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3045. tokenizer->advance();
  3046. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3047. // hint expected next!
  3048. tokenizer->advance();
  3049. switch (type) {
  3050. case Variant::INT: {
  3051. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  3052. //current_export.hint=PROPERTY_HINT_ALL_FLAGS;
  3053. tokenizer->advance();
  3054. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3055. break;
  3056. }
  3057. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3058. _set_error("Expected ')' or ',' in bit flags hint.");
  3059. return;
  3060. }
  3061. current_export.hint = PROPERTY_HINT_FLAGS;
  3062. tokenizer->advance();
  3063. bool first = true;
  3064. while (true) {
  3065. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3066. current_export = PropertyInfo();
  3067. _set_error("Expected a string constant in named bit flags hint.");
  3068. return;
  3069. }
  3070. String c = tokenizer->get_token_constant();
  3071. if (!first)
  3072. current_export.hint_string += ",";
  3073. else
  3074. first = false;
  3075. current_export.hint_string += c.xml_escape();
  3076. tokenizer->advance();
  3077. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3078. break;
  3079. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3080. current_export = PropertyInfo();
  3081. _set_error("Expected ')' or ',' in named bit flags hint.");
  3082. return;
  3083. }
  3084. tokenizer->advance();
  3085. }
  3086. break;
  3087. }
  3088. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3089. //enumeration
  3090. current_export.hint = PROPERTY_HINT_ENUM;
  3091. bool first = true;
  3092. while (true) {
  3093. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3094. current_export = PropertyInfo();
  3095. _set_error("Expected a string constant in enumeration hint.");
  3096. return;
  3097. }
  3098. String c = tokenizer->get_token_constant();
  3099. if (!first)
  3100. current_export.hint_string += ",";
  3101. else
  3102. first = false;
  3103. current_export.hint_string += c.xml_escape();
  3104. tokenizer->advance();
  3105. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3106. break;
  3107. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3108. current_export = PropertyInfo();
  3109. _set_error("Expected ')' or ',' in enumeration hint.");
  3110. return;
  3111. }
  3112. tokenizer->advance();
  3113. }
  3114. break;
  3115. }
  3116. }; //fallthrough to use the same
  3117. case Variant::REAL: {
  3118. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
  3119. current_export.hint = PROPERTY_HINT_EXP_EASING;
  3120. tokenizer->advance();
  3121. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3122. _set_error("Expected ')' in hint.");
  3123. return;
  3124. }
  3125. break;
  3126. }
  3127. // range
  3128. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EXP") {
  3129. current_export.hint = PROPERTY_HINT_EXP_RANGE;
  3130. tokenizer->advance();
  3131. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3132. break;
  3133. else if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3134. _set_error("Expected ')' or ',' in exponential range hint.");
  3135. return;
  3136. }
  3137. tokenizer->advance();
  3138. } else
  3139. current_export.hint = PROPERTY_HINT_RANGE;
  3140. float sign = 1.0;
  3141. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3142. sign = -1;
  3143. tokenizer->advance();
  3144. }
  3145. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3146. current_export = PropertyInfo();
  3147. _set_error("Expected a range in numeric hint.");
  3148. return;
  3149. }
  3150. current_export.hint_string = rtos(sign * double(tokenizer->get_token_constant()));
  3151. tokenizer->advance();
  3152. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3153. current_export.hint_string = "0," + current_export.hint_string;
  3154. break;
  3155. }
  3156. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3157. current_export = PropertyInfo();
  3158. _set_error("Expected ',' or ')' in numeric range hint.");
  3159. return;
  3160. }
  3161. tokenizer->advance();
  3162. sign = 1.0;
  3163. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3164. sign = -1;
  3165. tokenizer->advance();
  3166. }
  3167. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3168. current_export = PropertyInfo();
  3169. _set_error("Expected a number as upper bound in numeric range hint.");
  3170. return;
  3171. }
  3172. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  3173. tokenizer->advance();
  3174. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3175. break;
  3176. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3177. current_export = PropertyInfo();
  3178. _set_error("Expected ',' or ')' in numeric range hint.");
  3179. return;
  3180. }
  3181. tokenizer->advance();
  3182. sign = 1.0;
  3183. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3184. sign = -1;
  3185. tokenizer->advance();
  3186. }
  3187. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3188. current_export = PropertyInfo();
  3189. _set_error("Expected a number as step in numeric range hint.");
  3190. return;
  3191. }
  3192. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  3193. tokenizer->advance();
  3194. } break;
  3195. case Variant::STRING: {
  3196. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3197. //enumeration
  3198. current_export.hint = PROPERTY_HINT_ENUM;
  3199. bool first = true;
  3200. while (true) {
  3201. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3202. current_export = PropertyInfo();
  3203. _set_error("Expected a string constant in enumeration hint.");
  3204. return;
  3205. }
  3206. String c = tokenizer->get_token_constant();
  3207. if (!first)
  3208. current_export.hint_string += ",";
  3209. else
  3210. first = false;
  3211. current_export.hint_string += c.xml_escape();
  3212. tokenizer->advance();
  3213. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3214. break;
  3215. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3216. current_export = PropertyInfo();
  3217. _set_error("Expected ')' or ',' in enumeration hint.");
  3218. return;
  3219. }
  3220. tokenizer->advance();
  3221. }
  3222. break;
  3223. }
  3224. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "DIR") {
  3225. tokenizer->advance();
  3226. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3227. current_export.hint = PROPERTY_HINT_DIR;
  3228. else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3229. tokenizer->advance();
  3230. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) {
  3231. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  3232. return;
  3233. }
  3234. if (!p_class->tool) {
  3235. _set_error("Global filesystem hints may only be used in tool scripts.");
  3236. return;
  3237. }
  3238. current_export.hint = PROPERTY_HINT_GLOBAL_DIR;
  3239. tokenizer->advance();
  3240. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3241. _set_error("Expected ')' in hint.");
  3242. return;
  3243. }
  3244. } else {
  3245. _set_error("Expected ')' or ',' in hint.");
  3246. return;
  3247. }
  3248. break;
  3249. }
  3250. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FILE") {
  3251. current_export.hint = PROPERTY_HINT_FILE;
  3252. tokenizer->advance();
  3253. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3254. tokenizer->advance();
  3255. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "GLOBAL") {
  3256. if (!p_class->tool) {
  3257. _set_error("Global filesystem hints may only be used in tool scripts.");
  3258. return;
  3259. }
  3260. current_export.hint = PROPERTY_HINT_GLOBAL_FILE;
  3261. tokenizer->advance();
  3262. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3263. break;
  3264. else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA)
  3265. tokenizer->advance();
  3266. else {
  3267. _set_error("Expected ')' or ',' in hint.");
  3268. return;
  3269. }
  3270. }
  3271. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3272. if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE)
  3273. _set_error("Expected string constant with filter");
  3274. else
  3275. _set_error("Expected 'GLOBAL' or string constant with filter");
  3276. return;
  3277. }
  3278. current_export.hint_string = tokenizer->get_token_constant();
  3279. tokenizer->advance();
  3280. }
  3281. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3282. _set_error("Expected ')' in hint.");
  3283. return;
  3284. }
  3285. break;
  3286. }
  3287. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "MULTILINE") {
  3288. current_export.hint = PROPERTY_HINT_MULTILINE_TEXT;
  3289. tokenizer->advance();
  3290. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3291. _set_error("Expected ')' in hint.");
  3292. return;
  3293. }
  3294. break;
  3295. }
  3296. } break;
  3297. case Variant::COLOR: {
  3298. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER) {
  3299. current_export = PropertyInfo();
  3300. _set_error("Color type hint expects RGB or RGBA as hints");
  3301. return;
  3302. }
  3303. String identifier = tokenizer->get_token_identifier();
  3304. if (identifier == "RGB") {
  3305. current_export.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  3306. } else if (identifier == "RGBA") {
  3307. //none
  3308. } else {
  3309. current_export = PropertyInfo();
  3310. _set_error("Color type hint expects RGB or RGBA as hints");
  3311. return;
  3312. }
  3313. tokenizer->advance();
  3314. } break;
  3315. default: {
  3316. current_export = PropertyInfo();
  3317. _set_error("Type '" + Variant::get_type_name(type) + "' can't take hints.");
  3318. return;
  3319. } break;
  3320. }
  3321. }
  3322. } else {
  3323. parenthesis++;
  3324. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3325. if (!subexpr) {
  3326. if (_recover_from_completion()) {
  3327. break;
  3328. }
  3329. return;
  3330. }
  3331. parenthesis--;
  3332. if (subexpr->type != Node::TYPE_CONSTANT) {
  3333. current_export = PropertyInfo();
  3334. _set_error("Expected a constant expression.");
  3335. }
  3336. Variant constant = static_cast<ConstantNode *>(subexpr)->value;
  3337. if (constant.get_type() == Variant::OBJECT) {
  3338. GDScriptNativeClass *native_class = Object::cast_to<GDScriptNativeClass>(constant);
  3339. if (native_class && ClassDB::is_parent_class(native_class->get_name(), "Resource")) {
  3340. current_export.type = Variant::OBJECT;
  3341. current_export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3342. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3343. current_export.hint_string = native_class->get_name();
  3344. current_export.class_name = native_class->get_name();
  3345. } else {
  3346. current_export = PropertyInfo();
  3347. _set_error("Export hint not a resource type.");
  3348. }
  3349. } else if (constant.get_type() == Variant::DICTIONARY) {
  3350. // Enumeration
  3351. bool is_flags = false;
  3352. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3353. tokenizer->advance();
  3354. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  3355. is_flags = true;
  3356. tokenizer->advance();
  3357. } else {
  3358. current_export = PropertyInfo();
  3359. _set_error("Expected 'FLAGS' after comma.");
  3360. }
  3361. }
  3362. current_export.type = Variant::INT;
  3363. current_export.hint = is_flags ? PROPERTY_HINT_FLAGS : PROPERTY_HINT_ENUM;
  3364. Dictionary enum_values = constant;
  3365. List<Variant> keys;
  3366. enum_values.get_key_list(&keys);
  3367. bool first = true;
  3368. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  3369. if (enum_values[E->get()].get_type() == Variant::INT) {
  3370. if (!first)
  3371. current_export.hint_string += ",";
  3372. else
  3373. first = false;
  3374. current_export.hint_string += E->get().operator String().camelcase_to_underscore(true).capitalize().xml_escape();
  3375. if (!is_flags) {
  3376. current_export.hint_string += ":";
  3377. current_export.hint_string += enum_values[E->get()].operator String().xml_escape();
  3378. }
  3379. }
  3380. }
  3381. } else {
  3382. current_export = PropertyInfo();
  3383. _set_error("Expected type for export.");
  3384. return;
  3385. }
  3386. }
  3387. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3388. current_export = PropertyInfo();
  3389. _set_error("Expected ')' or ',' after export hint.");
  3390. return;
  3391. }
  3392. if (is_arrayed) {
  3393. hint_prefix += itos(current_export.type);
  3394. if (current_export.hint) {
  3395. hint_prefix += "/" + itos(current_export.hint);
  3396. }
  3397. current_export.hint_string = hint_prefix + ":" + current_export.hint_string;
  3398. current_export.hint = PROPERTY_HINT_TYPE_STRING;
  3399. current_export.type = Variant::ARRAY;
  3400. }
  3401. tokenizer->advance();
  3402. }
  3403. 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) {
  3404. current_export = PropertyInfo();
  3405. _set_error("Expected 'var', 'onready', 'remote', 'master', 'slave', 'sync', 'remotesync', 'mastersync', 'slavesync'.");
  3406. return;
  3407. }
  3408. continue;
  3409. } break;
  3410. case GDScriptTokenizer::TK_PR_ONREADY: {
  3411. //may be fallthrough from export, ignore if so
  3412. tokenizer->advance();
  3413. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3414. _set_error("Expected 'var'.");
  3415. return;
  3416. }
  3417. continue;
  3418. } break;
  3419. case GDScriptTokenizer::TK_PR_REMOTE: {
  3420. //may be fallthrough from export, ignore if so
  3421. tokenizer->advance();
  3422. if (current_export.type) {
  3423. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3424. _set_error("Expected 'var'.");
  3425. return;
  3426. }
  3427. } else {
  3428. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3429. _set_error("Expected 'var' or 'func'.");
  3430. return;
  3431. }
  3432. }
  3433. rpc_mode = MultiplayerAPI::RPC_MODE_REMOTE;
  3434. continue;
  3435. } break;
  3436. case GDScriptTokenizer::TK_PR_MASTER: {
  3437. //may be fallthrough from export, ignore if so
  3438. tokenizer->advance();
  3439. if (current_export.type) {
  3440. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3441. _set_error("Expected 'var'.");
  3442. return;
  3443. }
  3444. } else {
  3445. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3446. _set_error("Expected 'var' or 'func'.");
  3447. return;
  3448. }
  3449. }
  3450. rpc_mode = MultiplayerAPI::RPC_MODE_MASTER;
  3451. continue;
  3452. } break;
  3453. case GDScriptTokenizer::TK_PR_SLAVE: {
  3454. //may be fallthrough from export, ignore if so
  3455. tokenizer->advance();
  3456. if (current_export.type) {
  3457. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3458. _set_error("Expected 'var'.");
  3459. return;
  3460. }
  3461. } else {
  3462. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3463. _set_error("Expected 'var' or 'func'.");
  3464. return;
  3465. }
  3466. }
  3467. rpc_mode = MultiplayerAPI::RPC_MODE_SLAVE;
  3468. continue;
  3469. } break;
  3470. case GDScriptTokenizer::TK_PR_REMOTESYNC:
  3471. case GDScriptTokenizer::TK_PR_SYNC: {
  3472. //may be fallthrough from export, ignore if so
  3473. tokenizer->advance();
  3474. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3475. if (current_export.type)
  3476. _set_error("Expected 'var'.");
  3477. else
  3478. _set_error("Expected 'var' or 'func'.");
  3479. return;
  3480. }
  3481. rpc_mode = MultiplayerAPI::RPC_MODE_SYNC;
  3482. continue;
  3483. } break;
  3484. case GDScriptTokenizer::TK_PR_MASTERSYNC: {
  3485. //may be fallthrough from export, ignore if so
  3486. tokenizer->advance();
  3487. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3488. if (current_export.type)
  3489. _set_error("Expected 'var'.");
  3490. else
  3491. _set_error("Expected 'var' or 'func'.");
  3492. return;
  3493. }
  3494. rpc_mode = MultiplayerAPI::RPC_MODE_MASTERSYNC;
  3495. continue;
  3496. } break;
  3497. case GDScriptTokenizer::TK_PR_SLAVESYNC: {
  3498. //may be fallthrough from export, ignore if so
  3499. tokenizer->advance();
  3500. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3501. if (current_export.type)
  3502. _set_error("Expected 'var'.");
  3503. else
  3504. _set_error("Expected 'var' or 'func'.");
  3505. return;
  3506. }
  3507. rpc_mode = MultiplayerAPI::RPC_MODE_SLAVESYNC;
  3508. continue;
  3509. } break;
  3510. case GDScriptTokenizer::TK_PR_VAR: {
  3511. //variale declaration and (eventual) initialization
  3512. ClassNode::Member member;
  3513. bool autoexport = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_EXPORT;
  3514. if (current_export.type != Variant::NIL) {
  3515. member._export = current_export;
  3516. current_export = PropertyInfo();
  3517. }
  3518. bool onready = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_ONREADY;
  3519. tokenizer->advance();
  3520. if (!tokenizer->is_token_literal(0, true)) {
  3521. _set_error("Expected identifier for member variable name.");
  3522. return;
  3523. }
  3524. member.identifier = tokenizer->get_token_literal();
  3525. member.expression = NULL;
  3526. member._export.name = member.identifier;
  3527. member.line = tokenizer->get_token_line();
  3528. member.rpc_mode = rpc_mode;
  3529. if (current_class->constant_expressions.has(member.identifier)) {
  3530. _set_error("A constant named '" + String(member.identifier) + "' alread exists in this class (at line: " +
  3531. itos(current_class->constant_expressions[member.identifier].expression->line) + ").");
  3532. return;
  3533. }
  3534. for (int i = 0; i < current_class->variables.size(); i++) {
  3535. if (current_class->variables[i].identifier == member.identifier) {
  3536. _set_error("Variable '" + String(member.identifier) + "' alread exists in this class (at line: " +
  3537. itos(current_class->variables[i].line) + ").");
  3538. return;
  3539. }
  3540. }
  3541. tokenizer->advance();
  3542. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  3543. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  3544. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  3545. member.data_type = DataType();
  3546. #ifdef DEBUG_ENABLED
  3547. member.data_type.infer_type = true;
  3548. #endif
  3549. tokenizer->advance();
  3550. } else if (!_parse_type(member.data_type)) {
  3551. _set_error("Expected type for class variable.");
  3552. return;
  3553. }
  3554. }
  3555. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  3556. #ifdef DEBUG_ENABLED
  3557. int line = tokenizer->get_token_line();
  3558. #endif
  3559. tokenizer->advance();
  3560. Node *subexpr = _parse_and_reduce_expression(p_class, false, autoexport || member._export.type != Variant::NIL);
  3561. if (!subexpr) {
  3562. if (_recover_from_completion()) {
  3563. break;
  3564. }
  3565. return;
  3566. }
  3567. //discourage common error
  3568. if (!onready && subexpr->type == Node::TYPE_OPERATOR) {
  3569. OperatorNode *op = static_cast<OperatorNode *>(subexpr);
  3570. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_SELF && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  3571. IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]);
  3572. if (id->name == "get_node") {
  3573. _set_error("Use 'onready var " + String(member.identifier) + " = get_node(..)' instead");
  3574. return;
  3575. }
  3576. }
  3577. }
  3578. member.expression = subexpr;
  3579. if (autoexport && !member.data_type.has_type) {
  3580. if (subexpr->type != Node::TYPE_CONSTANT) {
  3581. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3582. return;
  3583. }
  3584. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3585. if (cn->value.get_type() == Variant::NIL) {
  3586. _set_error("Can't accept a null constant expression for inferring export type.");
  3587. return;
  3588. }
  3589. member._export.type = cn->value.get_type();
  3590. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3591. if (cn->value.get_type() == Variant::OBJECT) {
  3592. Object *obj = cn->value;
  3593. Resource *res = Object::cast_to<Resource>(obj);
  3594. if (res == NULL) {
  3595. _set_error("Exported constant not a type or resource.");
  3596. return;
  3597. }
  3598. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3599. member._export.hint_string = res->get_class();
  3600. }
  3601. }
  3602. #ifdef TOOLS_ENABLED
  3603. if (subexpr->type == Node::TYPE_CONSTANT && member._export.type != Variant::NIL) {
  3604. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3605. if (cn->value.get_type() != Variant::NIL) {
  3606. member.default_value = cn->value;
  3607. }
  3608. }
  3609. #endif
  3610. IdentifierNode *id = alloc_node<IdentifierNode>();
  3611. id->name = member.identifier;
  3612. OperatorNode *op = alloc_node<OperatorNode>();
  3613. op->op = OperatorNode::OP_INIT_ASSIGN;
  3614. op->arguments.push_back(id);
  3615. op->arguments.push_back(subexpr);
  3616. #ifdef DEBUG_ENABLED
  3617. NewLineNode *nl = alloc_node<NewLineNode>();
  3618. nl->line = line;
  3619. if (onready)
  3620. p_class->ready->statements.push_back(nl);
  3621. else
  3622. p_class->initializer->statements.push_back(nl);
  3623. #endif
  3624. if (onready)
  3625. p_class->ready->statements.push_back(op);
  3626. else
  3627. p_class->initializer->statements.push_back(op);
  3628. member.initial_assignment = op;
  3629. } else {
  3630. if (autoexport && !member.data_type.has_type) {
  3631. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3632. return;
  3633. }
  3634. }
  3635. if (autoexport && member.data_type.has_type) {
  3636. if (member.data_type.kind == DataType::BUILTIN) {
  3637. member._export.type = member.data_type.builtin_type;
  3638. } else if (member.data_type.kind == DataType::NATIVE) {
  3639. if (ClassDB::is_parent_class(member.data_type.native_type, "Resource")) {
  3640. member._export.type = Variant::OBJECT;
  3641. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3642. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3643. member._export.hint_string = member.data_type.native_type;
  3644. member._export.class_name = member.data_type.native_type;
  3645. } else {
  3646. _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
  3647. return;
  3648. }
  3649. } else {
  3650. _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
  3651. return;
  3652. }
  3653. }
  3654. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_SETGET) {
  3655. tokenizer->advance();
  3656. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3657. //just comma means using only getter
  3658. if (!tokenizer->is_token_literal()) {
  3659. _set_error("Expected identifier for setter function after 'setget'.");
  3660. }
  3661. member.setter = tokenizer->get_token_literal();
  3662. tokenizer->advance();
  3663. }
  3664. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3665. //there is a getter
  3666. tokenizer->advance();
  3667. if (!tokenizer->is_token_literal()) {
  3668. _set_error("Expected identifier for getter function after ','.");
  3669. }
  3670. member.getter = tokenizer->get_token_literal();
  3671. tokenizer->advance();
  3672. }
  3673. }
  3674. p_class->variables.push_back(member);
  3675. if (!_end_statement()) {
  3676. _set_error("Expected end of statement (continue)");
  3677. return;
  3678. }
  3679. } break;
  3680. case GDScriptTokenizer::TK_PR_CONST: {
  3681. // constant declaration and initialization
  3682. ClassNode::Constant constant;
  3683. tokenizer->advance();
  3684. if (!tokenizer->is_token_literal(0, true)) {
  3685. _set_error("Expected name (identifier) for constant.");
  3686. return;
  3687. }
  3688. StringName const_id = tokenizer->get_token_literal();
  3689. int line = tokenizer->get_token_line();
  3690. if (current_class->constant_expressions.has(const_id)) {
  3691. _set_error("Constant '" + String(const_id) + "' alread exists in this class (at line: " +
  3692. itos(current_class->constant_expressions[const_id].expression->line) + ").");
  3693. return;
  3694. }
  3695. for (int i = 0; i < current_class->variables.size(); i++) {
  3696. if (current_class->variables[i].identifier == const_id) {
  3697. _set_error("A variable named '" + String(const_id) + "' alread exists in this class (at line: " +
  3698. itos(current_class->variables[i].line) + ").");
  3699. return;
  3700. }
  3701. }
  3702. tokenizer->advance();
  3703. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  3704. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  3705. constant.type = DataType();
  3706. #ifdef DEBUG_ENABLED
  3707. constant.type.infer_type = true;
  3708. #endif
  3709. tokenizer->advance();
  3710. } else if (!_parse_type(constant.type)) {
  3711. _set_error("Expected type for class constant.");
  3712. return;
  3713. }
  3714. }
  3715. if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
  3716. _set_error("Constant expects assignment.");
  3717. return;
  3718. }
  3719. tokenizer->advance();
  3720. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3721. if (!subexpr) {
  3722. if (_recover_from_completion()) {
  3723. break;
  3724. }
  3725. return;
  3726. }
  3727. if (subexpr->type != Node::TYPE_CONSTANT) {
  3728. _set_error("Expected constant expression", line);
  3729. return;
  3730. }
  3731. subexpr->line = line;
  3732. constant.expression = subexpr;
  3733. p_class->constant_expressions.insert(const_id, constant);
  3734. if (!_end_statement()) {
  3735. _set_error("Expected end of statement (constant)", line);
  3736. return;
  3737. }
  3738. } break;
  3739. case GDScriptTokenizer::TK_PR_ENUM: {
  3740. //multiple constant declarations..
  3741. int last_assign = -1; // Incremented by 1 right before the assingment.
  3742. String enum_name;
  3743. Dictionary enum_dict;
  3744. tokenizer->advance();
  3745. if (tokenizer->is_token_literal(0, true)) {
  3746. enum_name = tokenizer->get_token_literal();
  3747. tokenizer->advance();
  3748. }
  3749. if (tokenizer->get_token() != GDScriptTokenizer::TK_CURLY_BRACKET_OPEN) {
  3750. _set_error("Expected '{' in enum declaration");
  3751. return;
  3752. }
  3753. tokenizer->advance();
  3754. while (true) {
  3755. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  3756. tokenizer->advance(); // Ignore newlines
  3757. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  3758. tokenizer->advance();
  3759. break; // End of enum
  3760. } else if (!tokenizer->is_token_literal(0, true)) {
  3761. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  3762. _set_error("Unexpected end of file.");
  3763. } else {
  3764. _set_error(String("Unexpected ") + GDScriptTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier");
  3765. }
  3766. return;
  3767. } else { // tokenizer->is_token_literal(0, true)
  3768. ClassNode::Constant constant;
  3769. StringName const_id = tokenizer->get_token_literal();
  3770. tokenizer->advance();
  3771. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  3772. tokenizer->advance();
  3773. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3774. if (!subexpr) {
  3775. if (_recover_from_completion()) {
  3776. break;
  3777. }
  3778. return;
  3779. }
  3780. if (subexpr->type != Node::TYPE_CONSTANT) {
  3781. _set_error("Expected constant expression");
  3782. return;
  3783. }
  3784. ConstantNode *subexpr_const = static_cast<ConstantNode *>(subexpr);
  3785. if (subexpr_const->value.get_type() != Variant::INT) {
  3786. _set_error("Expected an int value for enum");
  3787. return;
  3788. }
  3789. last_assign = subexpr_const->value;
  3790. constant.expression = subexpr_const;
  3791. } else {
  3792. last_assign = last_assign + 1;
  3793. ConstantNode *cn = alloc_node<ConstantNode>();
  3794. cn->value = last_assign;
  3795. cn->datatype = _type_from_variant(cn->value);
  3796. constant.expression = cn;
  3797. }
  3798. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3799. tokenizer->advance();
  3800. }
  3801. if (enum_name != "") {
  3802. const ConstantNode *cn = static_cast<const ConstantNode *>(constant.expression);
  3803. enum_dict[const_id] = cn->value;
  3804. }
  3805. constant.type.has_type = true;
  3806. constant.type.kind = DataType::BUILTIN;
  3807. constant.type.builtin_type = Variant::INT;
  3808. p_class->constant_expressions.insert(const_id, constant);
  3809. }
  3810. }
  3811. if (enum_name != "") {
  3812. ClassNode::Constant enum_constant;
  3813. ConstantNode *cn = alloc_node<ConstantNode>();
  3814. cn->value = enum_dict;
  3815. cn->datatype = _type_from_variant(cn->value);
  3816. enum_constant.expression = cn;
  3817. enum_constant.type = cn->datatype;
  3818. p_class->constant_expressions.insert(enum_name, enum_constant);
  3819. }
  3820. if (!_end_statement()) {
  3821. _set_error("Expected end of statement (enum)");
  3822. return;
  3823. }
  3824. } break;
  3825. case GDScriptTokenizer::TK_CONSTANT: {
  3826. if (tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3827. tokenizer->advance();
  3828. // Ignore
  3829. } else {
  3830. _set_error(String() + "Unexpected constant of type: " + Variant::get_type_name(tokenizer->get_token_constant().get_type()));
  3831. return;
  3832. }
  3833. } break;
  3834. default: {
  3835. _set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
  3836. return;
  3837. } break;
  3838. }
  3839. }
  3840. }
  3841. void GDScriptParser::_determine_inheritance(ClassNode *p_class) {
  3842. if (p_class->extends_used) {
  3843. //do inheritance
  3844. String path = p_class->extends_file;
  3845. Ref<GDScript> script;
  3846. StringName native;
  3847. ClassNode *base_class = NULL;
  3848. if (path != "") {
  3849. //path (and optionally subclasses)
  3850. if (path.is_rel_path()) {
  3851. String base = base_path;
  3852. if (base == "" || base.is_rel_path()) {
  3853. _set_error("Could not resolve relative path for parent class: " + path, p_class->line);
  3854. return;
  3855. }
  3856. path = base.plus_file(path).simplify_path();
  3857. }
  3858. script = ResourceLoader::load(path);
  3859. if (script.is_null()) {
  3860. _set_error("Could not load base class: " + path, p_class->line);
  3861. return;
  3862. }
  3863. if (!script->is_valid()) {
  3864. _set_error("Script not fully loaded (cyclic preload?): " + path, p_class->line);
  3865. return;
  3866. }
  3867. if (p_class->extends_class.size()) {
  3868. for (int i = 0; i < p_class->extends_class.size(); i++) {
  3869. String sub = p_class->extends_class[i];
  3870. if (script->get_subclasses().has(sub)) {
  3871. Ref<Script> subclass = script->get_subclasses()[sub]; //avoid reference from disappearing
  3872. script = subclass;
  3873. } else {
  3874. _set_error("Could not find subclass: " + sub, p_class->line);
  3875. return;
  3876. }
  3877. }
  3878. }
  3879. } else {
  3880. if (p_class->extends_class.size() == 0) {
  3881. _set_error("Parser bug: undecidable inheritance.", p_class->line);
  3882. ERR_FAIL();
  3883. }
  3884. //look around for the subclasses
  3885. int extend_iter = 1;
  3886. String base = p_class->extends_class[0];
  3887. ClassNode *p = p_class->owner;
  3888. Ref<GDScript> base_script;
  3889. if (ScriptServer::is_global_class(base)) {
  3890. base_script = ResourceLoader::load(ScriptServer::get_global_class_path(base));
  3891. if (!base_script.is_valid()) {
  3892. _set_error("Class '" + base + "' could not be fully loaded (script error or cyclic inheritance).", p_class->line);
  3893. return;
  3894. }
  3895. p = NULL;
  3896. }
  3897. while (p) {
  3898. bool found = false;
  3899. for (int i = 0; i < p->subclasses.size(); i++) {
  3900. if (p->subclasses[i]->name == base) {
  3901. ClassNode *test = p->subclasses[i];
  3902. while (test) {
  3903. if (test == p_class) {
  3904. _set_error("Cyclic inheritance.", test->line);
  3905. return;
  3906. }
  3907. if (test->base_type.kind == DataType::CLASS) {
  3908. test = test->base_type.class_type;
  3909. } else {
  3910. break;
  3911. }
  3912. }
  3913. found = true;
  3914. if (extend_iter < p_class->extends_class.size()) {
  3915. // Keep looking at current classes if possible
  3916. base = p_class->extends_class[extend_iter++];
  3917. p = p->subclasses[i];
  3918. } else {
  3919. base_class = p->subclasses[i];
  3920. }
  3921. break;
  3922. }
  3923. }
  3924. if (base_class) break;
  3925. if (found) continue;
  3926. if (p->constant_expressions.has(base)) {
  3927. if (!p->constant_expressions[base].expression->type == Node::TYPE_CONSTANT) {
  3928. _set_error("Could not resolve constant '" + base + "'.", p_class->line);
  3929. return;
  3930. }
  3931. const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[base].expression);
  3932. base_script = cn->value;
  3933. if (base_script.is_null()) {
  3934. _set_error("Constant is not a class: " + base, p_class->line);
  3935. return;
  3936. }
  3937. break;
  3938. }
  3939. p = p->owner;
  3940. }
  3941. if (base_script.is_valid()) {
  3942. String ident = base;
  3943. for (int i = extend_iter; i < p_class->extends_class.size(); i++) {
  3944. String subclass = p_class->extends_class[i];
  3945. ident += ("." + subclass);
  3946. if (base_script->get_subclasses().has(subclass)) {
  3947. base_script = base_script->get_subclasses()[subclass];
  3948. } else if (base_script->get_constants().has(subclass)) {
  3949. Ref<GDScript> new_base_class = base_script->get_constants()[subclass];
  3950. if (new_base_class.is_null()) {
  3951. _set_error("Constant is not a class: " + ident, p_class->line);
  3952. return;
  3953. }
  3954. base_script = new_base_class;
  3955. } else {
  3956. _set_error("Could not find subclass: " + ident, p_class->line);
  3957. return;
  3958. }
  3959. }
  3960. script = base_script;
  3961. } else if (!base_class) {
  3962. if (p_class->extends_class.size() > 1) {
  3963. _set_error("Invalid inheritance (unknown class + subclasses)", p_class->line);
  3964. return;
  3965. }
  3966. //if not found, try engine classes
  3967. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  3968. _set_error("Unknown class: '" + base + "'", p_class->line);
  3969. return;
  3970. }
  3971. native = base;
  3972. }
  3973. }
  3974. if (base_class) {
  3975. p_class->base_type.has_type = true;
  3976. p_class->base_type.kind = DataType::CLASS;
  3977. p_class->base_type.class_type = base_class;
  3978. } else if (script.is_valid()) {
  3979. p_class->base_type.has_type = true;
  3980. p_class->base_type.kind = DataType::GDSCRIPT;
  3981. p_class->base_type.script_type = script;
  3982. p_class->base_type.native_type = script->get_instance_base_type();
  3983. } else if (native != StringName()) {
  3984. p_class->base_type.has_type = true;
  3985. p_class->base_type.kind = DataType::NATIVE;
  3986. p_class->base_type.native_type = native;
  3987. } else {
  3988. _set_error("Could not determine inheritance", p_class->line);
  3989. return;
  3990. }
  3991. } else {
  3992. // without extends, implicitly extend Reference
  3993. p_class->base_type.has_type = true;
  3994. p_class->base_type.kind = DataType::NATIVE;
  3995. p_class->base_type.native_type = "Reference";
  3996. }
  3997. // Recursively determine subclasses
  3998. for (int i = 0; i < p_class->subclasses.size(); i++) {
  3999. _determine_inheritance(p_class->subclasses[i]);
  4000. }
  4001. }
  4002. String GDScriptParser::DataType::to_string() const {
  4003. if (!has_type) return "var";
  4004. switch (kind) {
  4005. case BUILTIN: {
  4006. if (builtin_type == Variant::NIL) return "null";
  4007. return Variant::get_type_name(builtin_type);
  4008. } break;
  4009. case NATIVE: {
  4010. if (is_meta_type) {
  4011. return "GDScriptNativeClass";
  4012. }
  4013. return native_type.operator String();
  4014. } break;
  4015. case GDSCRIPT: {
  4016. Ref<GDScript> gds = script_type;
  4017. const String &gds_class = gds->get_script_class_name();
  4018. if (!gds_class.empty()) {
  4019. return gds_class;
  4020. }
  4021. } // fallthrough
  4022. case SCRIPT: {
  4023. if (is_meta_type) {
  4024. return script_type->get_class_name().operator String();
  4025. }
  4026. String name = script_type->get_name();
  4027. if (name != String()) {
  4028. return name;
  4029. }
  4030. name = script_type->get_path().get_file();
  4031. if (name != String()) {
  4032. return name;
  4033. }
  4034. return native_type.operator String();
  4035. } break;
  4036. case CLASS: {
  4037. ERR_FAIL_COND_V(!class_type, String());
  4038. if (is_meta_type) {
  4039. return "GDScript";
  4040. }
  4041. if (class_type->name == StringName()) {
  4042. return "self";
  4043. }
  4044. return class_type->name.operator String();
  4045. } break;
  4046. }
  4047. return "Unresolved";
  4048. }
  4049. bool GDScriptParser::_parse_type(DataType &r_type, bool p_can_be_void) {
  4050. tokenizer->advance();
  4051. r_type.has_type = true;
  4052. bool finished = false;
  4053. bool can_index = false;
  4054. String full_name;
  4055. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  4056. completion_cursor = StringName();
  4057. completion_type = COMPLETION_TYPE_HINT;
  4058. completion_class = current_class;
  4059. completion_function = current_function;
  4060. completion_line = tokenizer->get_token_line();
  4061. completion_argument = 0;
  4062. completion_block = current_block;
  4063. completion_found = true;
  4064. completion_ident_is_call = p_can_be_void;
  4065. tokenizer->advance();
  4066. }
  4067. switch (tokenizer->get_token()) {
  4068. case GDScriptTokenizer::TK_PR_VOID: {
  4069. if (!p_can_be_void) {
  4070. return false;
  4071. }
  4072. r_type.kind = DataType::BUILTIN;
  4073. r_type.builtin_type = Variant::NIL;
  4074. } break;
  4075. case GDScriptTokenizer::TK_BUILT_IN_TYPE: {
  4076. r_type.builtin_type = tokenizer->get_token_type();
  4077. if (tokenizer->get_token_type() == Variant::OBJECT) {
  4078. r_type.kind = DataType::NATIVE;
  4079. r_type.native_type = "Object";
  4080. } else {
  4081. r_type.kind = DataType::BUILTIN;
  4082. }
  4083. } break;
  4084. case GDScriptTokenizer::TK_IDENTIFIER: {
  4085. r_type.native_type = tokenizer->get_token_identifier();
  4086. if (ClassDB::class_exists(r_type.native_type) || ClassDB::class_exists("_" + r_type.native_type.operator String())) {
  4087. r_type.kind = DataType::NATIVE;
  4088. } else {
  4089. r_type.kind = DataType::UNRESOLVED;
  4090. can_index = true;
  4091. full_name = r_type.native_type;
  4092. }
  4093. } break;
  4094. default: {
  4095. return false;
  4096. }
  4097. }
  4098. tokenizer->advance();
  4099. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  4100. completion_cursor = r_type.native_type;
  4101. completion_type = COMPLETION_TYPE_HINT;
  4102. completion_class = current_class;
  4103. completion_function = current_function;
  4104. completion_line = tokenizer->get_token_line();
  4105. completion_argument = 0;
  4106. completion_block = current_block;
  4107. completion_found = true;
  4108. completion_ident_is_call = p_can_be_void;
  4109. tokenizer->advance();
  4110. }
  4111. if (can_index) {
  4112. while (!finished) {
  4113. switch (tokenizer->get_token()) {
  4114. case GDScriptTokenizer::TK_PERIOD: {
  4115. if (!can_index) {
  4116. _set_error("Unexpected '.'.");
  4117. return false;
  4118. }
  4119. can_index = false;
  4120. tokenizer->advance();
  4121. } break;
  4122. case GDScriptTokenizer::TK_IDENTIFIER: {
  4123. if (can_index) {
  4124. _set_error("Unexpected identifier.");
  4125. return false;
  4126. }
  4127. StringName id;
  4128. bool has_completion = _get_completable_identifier(COMPLETION_TYPE_HINT_INDEX, id);
  4129. if (id == StringName()) {
  4130. id = "@temp";
  4131. }
  4132. full_name += "." + id.operator String();
  4133. can_index = true;
  4134. if (has_completion) {
  4135. completion_cursor = full_name;
  4136. }
  4137. } break;
  4138. default: {
  4139. finished = true;
  4140. } break;
  4141. }
  4142. }
  4143. if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PERIOD) {
  4144. _set_error("Expected subclass identifier.");
  4145. return false;
  4146. }
  4147. r_type.native_type = full_name;
  4148. }
  4149. return true;
  4150. }
  4151. GDScriptParser::DataType GDScriptParser::_resolve_type(const DataType &p_source, int p_line) {
  4152. if (!p_source.has_type) return p_source;
  4153. if (p_source.kind != DataType::UNRESOLVED) return p_source;
  4154. Vector<String> full_name = p_source.native_type.operator String().split(".", false);
  4155. int name_part = 0;
  4156. DataType result;
  4157. result.has_type = true;
  4158. while (name_part < full_name.size()) {
  4159. bool found = false;
  4160. StringName id = full_name[name_part];
  4161. DataType base_type = result;
  4162. ClassNode *p = NULL;
  4163. if (name_part == 0) {
  4164. if (ScriptServer::is_global_class(id)) {
  4165. String script_path = ScriptServer::get_global_class_path(id);
  4166. if (script_path == self_path) {
  4167. result.kind = DataType::CLASS;
  4168. result.class_type = current_class;
  4169. } else {
  4170. Ref<Script> script = ResourceLoader::load(script_path);
  4171. Ref<GDScript> gds = script;
  4172. if (gds.is_valid()) {
  4173. if (!gds->is_valid()) {
  4174. _set_error("Class '" + id + "' could not be fully loaded (script error or cyclic inheritance).", p_line);
  4175. return DataType();
  4176. }
  4177. result.kind = DataType::GDSCRIPT;
  4178. result.script_type = gds;
  4179. } else if (script.is_valid()) {
  4180. result.kind = DataType::SCRIPT;
  4181. result.script_type = script;
  4182. } else {
  4183. _set_error("Class '" + id + "' was found in global scope but its script could not be loaded.", p_line);
  4184. return DataType();
  4185. }
  4186. }
  4187. name_part++;
  4188. continue;
  4189. } else {
  4190. p = current_class;
  4191. }
  4192. } else if (base_type.kind == DataType::CLASS) {
  4193. p = base_type.class_type;
  4194. }
  4195. while (p) {
  4196. if (p->constant_expressions.has(id)) {
  4197. if (p->constant_expressions[id].expression->type != Node::TYPE_CONSTANT) {
  4198. _set_error("Parser bug: unresolved constant.", p_line);
  4199. ERR_FAIL_V(result);
  4200. }
  4201. const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[id].expression);
  4202. Ref<GDScript> gds = cn->value;
  4203. if (gds.is_valid()) {
  4204. result.kind = DataType::GDSCRIPT;
  4205. result.script_type = gds;
  4206. found = true;
  4207. } else {
  4208. Ref<Script> scr = cn->value;
  4209. if (scr.is_valid()) {
  4210. result.kind = DataType::SCRIPT;
  4211. result.script_type = scr;
  4212. found = true;
  4213. }
  4214. }
  4215. break;
  4216. }
  4217. // Inner classes
  4218. ClassNode *outer_class = p;
  4219. while (outer_class) {
  4220. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  4221. if (outer_class->subclasses[i] == p) {
  4222. continue;
  4223. }
  4224. if (outer_class->subclasses[i]->name == id) {
  4225. found = true;
  4226. result.kind = DataType::CLASS;
  4227. result.class_type = outer_class->subclasses[i];
  4228. break;
  4229. }
  4230. }
  4231. if (found) {
  4232. break;
  4233. }
  4234. outer_class = outer_class->owner;
  4235. }
  4236. if (!found && p->base_type.kind == DataType::CLASS) {
  4237. p = p->base_type.class_type;
  4238. } else {
  4239. base_type = p->base_type;
  4240. break;
  4241. }
  4242. }
  4243. // Still look for class constants in parent script
  4244. if (!found && (base_type.kind == DataType::GDSCRIPT || base_type.kind == DataType::SCRIPT)) {
  4245. Ref<Script> scr = base_type.script_type;
  4246. ERR_FAIL_COND_V(scr.is_null(), result);
  4247. Map<StringName, Variant> constants;
  4248. scr->get_constants(&constants);
  4249. if (constants.has(id)) {
  4250. Ref<GDScript> gds = constants[id];
  4251. if (gds.is_valid()) {
  4252. result.kind = DataType::GDSCRIPT;
  4253. result.script_type = gds;
  4254. found = true;
  4255. } else {
  4256. Ref<Script> scr = constants[id];
  4257. if (scr.is_valid()) {
  4258. result.kind = DataType::SCRIPT;
  4259. result.script_type = scr;
  4260. found = true;
  4261. }
  4262. }
  4263. }
  4264. }
  4265. if (!found && !for_completion) {
  4266. String base;
  4267. if (name_part == 0) {
  4268. base = "self";
  4269. } else {
  4270. base = result.to_string();
  4271. }
  4272. _set_error("Identifier '" + String(id) + "' is not a valid type (not a script or class), or could not be found on base '" +
  4273. base + "'.",
  4274. p_line);
  4275. return DataType();
  4276. }
  4277. name_part++;
  4278. }
  4279. return result;
  4280. }
  4281. GDScriptParser::DataType GDScriptParser::_type_from_variant(const Variant &p_value) const {
  4282. DataType result;
  4283. result.has_type = true;
  4284. result.is_constant = true;
  4285. result.kind = DataType::BUILTIN;
  4286. result.builtin_type = p_value.get_type();
  4287. if (result.builtin_type == Variant::OBJECT) {
  4288. Object *obj = p_value.operator Object *();
  4289. if (!obj) {
  4290. return DataType();
  4291. }
  4292. result.native_type = obj->get_class_name();
  4293. Ref<Script> scr = p_value;
  4294. if (scr.is_valid()) {
  4295. result.is_meta_type = true;
  4296. } else {
  4297. result.is_meta_type = false;
  4298. scr = obj->get_script();
  4299. }
  4300. if (scr.is_valid()) {
  4301. result.script_type = scr;
  4302. Ref<GDScript> gds = scr;
  4303. if (gds.is_valid()) {
  4304. result.kind = DataType::GDSCRIPT;
  4305. } else {
  4306. result.kind = DataType::SCRIPT;
  4307. }
  4308. result.native_type = scr->get_instance_base_type();
  4309. } else {
  4310. result.kind = DataType::NATIVE;
  4311. }
  4312. }
  4313. return result;
  4314. }
  4315. GDScriptParser::DataType GDScriptParser::_type_from_property(const PropertyInfo &p_property, bool p_nil_is_variant) const {
  4316. DataType ret;
  4317. if (p_property.type == Variant::NIL && (p_nil_is_variant || (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  4318. // Variant
  4319. return ret;
  4320. }
  4321. ret.has_type = true;
  4322. ret.builtin_type = p_property.type;
  4323. if (p_property.type == Variant::OBJECT) {
  4324. ret.kind = DataType::NATIVE;
  4325. ret.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  4326. } else {
  4327. ret.kind = DataType::BUILTIN;
  4328. }
  4329. return ret;
  4330. }
  4331. GDScriptParser::DataType GDScriptParser::_type_from_gdtype(const GDScriptDataType &p_gdtype) const {
  4332. DataType result;
  4333. if (!p_gdtype.has_type) {
  4334. return result;
  4335. }
  4336. result.has_type = true;
  4337. result.builtin_type = p_gdtype.builtin_type;
  4338. result.native_type = p_gdtype.native_type;
  4339. result.script_type = p_gdtype.script_type;
  4340. switch (p_gdtype.kind) {
  4341. case GDScriptDataType::BUILTIN: {
  4342. result.kind = DataType::BUILTIN;
  4343. } break;
  4344. case GDScriptDataType::NATIVE: {
  4345. result.kind = DataType::NATIVE;
  4346. } break;
  4347. case GDScriptDataType::GDSCRIPT: {
  4348. result.kind = DataType::GDSCRIPT;
  4349. } break;
  4350. case GDScriptDataType::SCRIPT: {
  4351. result.kind = DataType::SCRIPT;
  4352. } break;
  4353. }
  4354. return result;
  4355. }
  4356. GDScriptParser::DataType GDScriptParser::_get_operation_type(const Variant::Operator p_op, const DataType &p_a, const DataType &p_b, bool &r_valid) const {
  4357. if (!p_a.has_type || !p_b.has_type) {
  4358. r_valid = true;
  4359. return DataType();
  4360. }
  4361. Variant::Type a_type = p_a.kind == DataType::BUILTIN ? p_a.builtin_type : Variant::OBJECT;
  4362. Variant::Type b_type = p_b.kind == DataType::BUILTIN ? p_b.builtin_type : Variant::OBJECT;
  4363. Variant a;
  4364. REF a_ref;
  4365. if (a_type == Variant::OBJECT) {
  4366. a_ref.instance();
  4367. a = a_ref;
  4368. } else {
  4369. Variant::CallError err;
  4370. a = Variant::construct(a_type, NULL, 0, err);
  4371. if (err.error != Variant::CallError::CALL_OK) {
  4372. r_valid = false;
  4373. return DataType();
  4374. }
  4375. }
  4376. Variant b;
  4377. REF b_ref;
  4378. if (b_type == Variant::OBJECT) {
  4379. b_ref.instance();
  4380. b = b_ref;
  4381. } else {
  4382. Variant::CallError err;
  4383. b = Variant::construct(b_type, NULL, 0, err);
  4384. if (err.error != Variant::CallError::CALL_OK) {
  4385. r_valid = false;
  4386. return DataType();
  4387. }
  4388. }
  4389. // Avoid division by zero
  4390. if (a_type == Variant::INT || a_type == Variant::REAL) {
  4391. Variant::evaluate(Variant::OP_ADD, a, 1, a, r_valid);
  4392. }
  4393. if (b_type == Variant::INT || b_type == Variant::REAL) {
  4394. Variant::evaluate(Variant::OP_ADD, b, 1, b, r_valid);
  4395. }
  4396. Variant ret;
  4397. Variant::evaluate(p_op, a, b, ret, r_valid);
  4398. if (r_valid) {
  4399. return _type_from_variant(ret);
  4400. }
  4401. return DataType();
  4402. }
  4403. Variant::Operator GDScriptParser::_get_variant_operation(const OperatorNode::Operator &p_op) const {
  4404. switch (p_op) {
  4405. case OperatorNode::OP_NEG: {
  4406. return Variant::OP_NEGATE;
  4407. } break;
  4408. case OperatorNode::OP_POS: {
  4409. return Variant::OP_POSITIVE;
  4410. } break;
  4411. case OperatorNode::OP_NOT: {
  4412. return Variant::OP_NOT;
  4413. } break;
  4414. case OperatorNode::OP_BIT_INVERT: {
  4415. return Variant::OP_BIT_NEGATE;
  4416. } break;
  4417. case OperatorNode::OP_IN: {
  4418. return Variant::OP_IN;
  4419. } break;
  4420. case OperatorNode::OP_EQUAL: {
  4421. return Variant::OP_EQUAL;
  4422. } break;
  4423. case OperatorNode::OP_NOT_EQUAL: {
  4424. return Variant::OP_NOT_EQUAL;
  4425. } break;
  4426. case OperatorNode::OP_LESS: {
  4427. return Variant::OP_LESS;
  4428. } break;
  4429. case OperatorNode::OP_LESS_EQUAL: {
  4430. return Variant::OP_LESS_EQUAL;
  4431. } break;
  4432. case OperatorNode::OP_GREATER: {
  4433. return Variant::OP_GREATER;
  4434. } break;
  4435. case OperatorNode::OP_GREATER_EQUAL: {
  4436. return Variant::OP_GREATER_EQUAL;
  4437. } break;
  4438. case OperatorNode::OP_AND: {
  4439. return Variant::OP_AND;
  4440. } break;
  4441. case OperatorNode::OP_OR: {
  4442. return Variant::OP_OR;
  4443. } break;
  4444. case OperatorNode::OP_ASSIGN_ADD:
  4445. case OperatorNode::OP_ADD: {
  4446. return Variant::OP_ADD;
  4447. } break;
  4448. case OperatorNode::OP_ASSIGN_SUB:
  4449. case OperatorNode::OP_SUB: {
  4450. return Variant::OP_SUBTRACT;
  4451. } break;
  4452. case OperatorNode::OP_ASSIGN_MUL:
  4453. case OperatorNode::OP_MUL: {
  4454. return Variant::OP_MULTIPLY;
  4455. } break;
  4456. case OperatorNode::OP_ASSIGN_DIV:
  4457. case OperatorNode::OP_DIV: {
  4458. return Variant::OP_DIVIDE;
  4459. } break;
  4460. case OperatorNode::OP_ASSIGN_MOD:
  4461. case OperatorNode::OP_MOD: {
  4462. return Variant::OP_MODULE;
  4463. } break;
  4464. case OperatorNode::OP_ASSIGN_BIT_AND:
  4465. case OperatorNode::OP_BIT_AND: {
  4466. return Variant::OP_BIT_AND;
  4467. } break;
  4468. case OperatorNode::OP_ASSIGN_BIT_OR:
  4469. case OperatorNode::OP_BIT_OR: {
  4470. return Variant::OP_BIT_OR;
  4471. } break;
  4472. case OperatorNode::OP_ASSIGN_BIT_XOR:
  4473. case OperatorNode::OP_BIT_XOR: {
  4474. return Variant::OP_BIT_XOR;
  4475. } break;
  4476. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  4477. case OperatorNode::OP_SHIFT_LEFT: {
  4478. return Variant::OP_SHIFT_LEFT;
  4479. }
  4480. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  4481. case OperatorNode::OP_SHIFT_RIGHT: {
  4482. return Variant::OP_SHIFT_RIGHT;
  4483. }
  4484. default: {
  4485. return Variant::OP_MAX;
  4486. } break;
  4487. }
  4488. }
  4489. bool GDScriptParser::_is_type_compatible(const DataType &p_container, const DataType &p_expression, bool p_allow_implicit_conversion) const {
  4490. // Ignore for completion
  4491. if (!check_types || for_completion) {
  4492. return true;
  4493. }
  4494. // Can't test if not all have type
  4495. if (!p_container.has_type || !p_expression.has_type) {
  4496. return true;
  4497. }
  4498. // Should never get here unresolved
  4499. ERR_FAIL_COND_V(p_container.kind == DataType::UNRESOLVED, false);
  4500. ERR_FAIL_COND_V(p_expression.kind == DataType::UNRESOLVED, false);
  4501. if (p_container.kind == DataType::BUILTIN && p_expression.kind == DataType::BUILTIN) {
  4502. bool valid = p_container.builtin_type == p_expression.builtin_type;
  4503. if (p_allow_implicit_conversion) {
  4504. valid = valid || (p_container.builtin_type == Variant::INT && p_expression.builtin_type == Variant::REAL);
  4505. valid = valid || (p_container.builtin_type == Variant::REAL && p_expression.builtin_type == Variant::INT);
  4506. valid = valid || (p_container.builtin_type == Variant::STRING && p_expression.builtin_type == Variant::NODE_PATH);
  4507. valid = valid || (p_container.builtin_type == Variant::NODE_PATH && p_expression.builtin_type == Variant::STRING);
  4508. valid = valid || (p_container.builtin_type == Variant::BOOL && p_expression.builtin_type == Variant::REAL);
  4509. valid = valid || (p_container.builtin_type == Variant::BOOL && p_expression.builtin_type == Variant::INT);
  4510. valid = valid || (p_container.builtin_type == Variant::INT && p_expression.builtin_type == Variant::BOOL);
  4511. valid = valid || (p_container.builtin_type == Variant::REAL && p_expression.builtin_type == Variant::BOOL);
  4512. }
  4513. return valid;
  4514. }
  4515. if (p_container.kind == DataType::BUILTIN || (p_expression.kind == DataType::BUILTIN && p_expression.builtin_type != Variant::NIL)) {
  4516. // Can't mix built-ins with objects
  4517. return false;
  4518. }
  4519. // From now on everything is objects, check polymorphism
  4520. // The container must be the same class or a superclass of the expression
  4521. if (p_expression.kind == DataType::BUILTIN && p_expression.builtin_type == Variant::NIL) {
  4522. // Null can be assigned to object types
  4523. return true;
  4524. }
  4525. StringName expr_native;
  4526. Ref<Script> expr_script;
  4527. ClassNode *expr_class = NULL;
  4528. switch (p_expression.kind) {
  4529. case DataType::NATIVE: {
  4530. if (p_container.kind != DataType::NATIVE) {
  4531. // Non-native type can't be a superclass of a native type
  4532. return false;
  4533. }
  4534. if (p_expression.is_meta_type) {
  4535. expr_native = GDScriptNativeClass::get_class_static();
  4536. } else {
  4537. expr_native = p_expression.native_type;
  4538. }
  4539. } break;
  4540. case DataType::SCRIPT:
  4541. case DataType::GDSCRIPT: {
  4542. if (p_container.kind == DataType::CLASS) {
  4543. // This cannot be resolved without cyclic dependencies, so just bail out
  4544. return false;
  4545. }
  4546. if (p_expression.is_meta_type) {
  4547. expr_native = p_expression.script_type->get_class_name();
  4548. } else {
  4549. expr_script = p_expression.script_type;
  4550. expr_native = expr_script->get_instance_base_type();
  4551. }
  4552. } break;
  4553. case DataType::CLASS: {
  4554. if (p_expression.is_meta_type) {
  4555. expr_native = GDScript::get_class_static();
  4556. } else {
  4557. expr_class = p_expression.class_type;
  4558. ClassNode *base = expr_class;
  4559. while (base->base_type.kind == DataType::CLASS) {
  4560. base = base->base_type.class_type;
  4561. }
  4562. expr_native = base->base_type.native_type;
  4563. expr_script = base->base_type.script_type;
  4564. }
  4565. }
  4566. }
  4567. switch (p_container.kind) {
  4568. case DataType::NATIVE: {
  4569. if (p_container.is_meta_type) {
  4570. return ClassDB::is_parent_class(expr_native, GDScriptNativeClass::get_class_static());
  4571. } else {
  4572. return ClassDB::is_parent_class(expr_native, p_container.native_type);
  4573. }
  4574. } break;
  4575. case DataType::SCRIPT:
  4576. case DataType::GDSCRIPT: {
  4577. if (p_container.is_meta_type) {
  4578. return ClassDB::is_parent_class(expr_native, GDScript::get_class_static());
  4579. }
  4580. if (expr_class == head && p_container.script_type->get_path() == self_path) {
  4581. // Special case: container is self script and expression is self
  4582. return true;
  4583. }
  4584. while (expr_script.is_valid()) {
  4585. if (expr_script == p_container.script_type) {
  4586. return true;
  4587. }
  4588. expr_script = expr_script->get_base_script();
  4589. }
  4590. return false;
  4591. } break;
  4592. case DataType::CLASS: {
  4593. if (p_container.is_meta_type) {
  4594. return ClassDB::is_parent_class(expr_native, GDScript::get_class_static());
  4595. }
  4596. if (p_container.class_type == head && expr_script.is_valid() && expr_script->get_path() == self_path) {
  4597. // Special case: container is self and expression is self script
  4598. return true;
  4599. }
  4600. while (expr_class) {
  4601. if (expr_class == p_container.class_type) {
  4602. return true;
  4603. }
  4604. expr_class = expr_class->base_type.class_type;
  4605. }
  4606. return false;
  4607. }
  4608. }
  4609. return false;
  4610. }
  4611. GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
  4612. if (p_node->get_datatype().has_type) {
  4613. return p_node->get_datatype();
  4614. }
  4615. DataType node_type;
  4616. switch (p_node->type) {
  4617. case Node::TYPE_CONSTANT: {
  4618. node_type = _type_from_variant(static_cast<ConstantNode *>(p_node)->value);
  4619. } break;
  4620. case Node::TYPE_ARRAY: {
  4621. node_type.has_type = true;
  4622. node_type.kind = DataType::BUILTIN;
  4623. node_type.builtin_type = Variant::ARRAY;
  4624. } break;
  4625. case Node::TYPE_DICTIONARY: {
  4626. node_type.has_type = true;
  4627. node_type.kind = DataType::BUILTIN;
  4628. node_type.builtin_type = Variant::DICTIONARY;
  4629. } break;
  4630. case Node::TYPE_SELF: {
  4631. node_type.has_type = true;
  4632. node_type.kind = DataType::CLASS;
  4633. node_type.class_type = current_class;
  4634. } break;
  4635. case Node::TYPE_IDENTIFIER: {
  4636. IdentifierNode *id = static_cast<IdentifierNode *>(p_node);
  4637. if (id->declared_block) {
  4638. node_type = id->declared_block->variables[id->name]->get_datatype();
  4639. } else if (id->name == "#match_value") {
  4640. // It's a special id just for the match statetement, ignore
  4641. break;
  4642. } else if (current_function && current_function->arguments.find(id->name) >= 0) {
  4643. int idx = current_function->arguments.find(id->name);
  4644. node_type = current_function->argument_types[idx];
  4645. } else {
  4646. node_type = _reduce_identifier_type(NULL, id->name, id->line);
  4647. }
  4648. } break;
  4649. case Node::TYPE_CAST: {
  4650. CastNode *cn = static_cast<CastNode *>(p_node);
  4651. DataType source_type = _reduce_node_type(cn->source_node);
  4652. cn->cast_type = _resolve_type(cn->cast_type, cn->line);
  4653. if (source_type.has_type) {
  4654. bool valid = false;
  4655. if (check_types) {
  4656. if (cn->cast_type.kind == DataType::BUILTIN && source_type.kind == DataType::BUILTIN) {
  4657. valid = Variant::can_convert(source_type.builtin_type, cn->cast_type.builtin_type);
  4658. }
  4659. if (cn->cast_type.kind != DataType::BUILTIN && source_type.kind != DataType::BUILTIN) {
  4660. valid = _is_type_compatible(cn->cast_type, source_type) || _is_type_compatible(source_type, cn->cast_type);
  4661. }
  4662. if (!valid) {
  4663. _set_error("Invalid cast. Cannot convert from '" + source_type.to_string() +
  4664. "' to '" + cn->cast_type.to_string() + "'.",
  4665. cn->line);
  4666. return DataType();
  4667. }
  4668. }
  4669. } else {
  4670. _mark_line_as_unsafe(cn->line);
  4671. }
  4672. node_type = cn->cast_type;
  4673. } break;
  4674. case Node::TYPE_OPERATOR: {
  4675. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  4676. switch (op->op) {
  4677. case OperatorNode::OP_CALL:
  4678. case OperatorNode::OP_PARENT_CALL: {
  4679. node_type = _reduce_function_call_type(op);
  4680. } break;
  4681. case OperatorNode::OP_YIELD: {
  4682. if (op->arguments.size() == 2) {
  4683. DataType base_type = _reduce_node_type(op->arguments[0]);
  4684. DataType signal_type = _reduce_node_type(op->arguments[1]);
  4685. // TODO: Check if signal exists when it's a constant
  4686. if (base_type.has_type && base_type.kind == DataType::BUILTIN && base_type.builtin_type != Variant::NIL && base_type.builtin_type != Variant::OBJECT) {
  4687. _set_error("First argument of 'yield()' must be an object.", op->line);
  4688. return DataType();
  4689. }
  4690. if (signal_type.has_type && (signal_type.kind != DataType::BUILTIN || signal_type.builtin_type != Variant::STRING)) {
  4691. _set_error("Second argument of 'yield()' must be a string.", op->line);
  4692. return DataType();
  4693. }
  4694. }
  4695. // yield can return anything
  4696. node_type.has_type = false;
  4697. } break;
  4698. case OperatorNode::OP_IS: {
  4699. if (op->arguments.size() != 2) {
  4700. _set_error("Parser bug: binary operation without 2 arguments.", op->line);
  4701. ERR_FAIL_V(DataType());
  4702. }
  4703. DataType value_type = _reduce_node_type(op->arguments[0]);
  4704. DataType type_type = _reduce_node_type(op->arguments[1]);
  4705. if (check_types && type_type.has_type) {
  4706. if (!type_type.is_meta_type && (type_type.kind != DataType::NATIVE || !ClassDB::is_parent_class(type_type.native_type, "Script"))) {
  4707. _set_error("Invalid 'is' test: right operand is not a type (not a native type nor a script).", op->line);
  4708. return DataType();
  4709. }
  4710. type_type.is_meta_type = false; // Test the actual type
  4711. if (!_is_type_compatible(type_type, value_type) && !_is_type_compatible(value_type, type_type)) {
  4712. // TODO: Make this a warning?
  4713. _set_error("A value of type '" + value_type.to_string() + "' will never be an instance of '" + type_type.to_string() + "'.", op->line);
  4714. return DataType();
  4715. }
  4716. }
  4717. node_type.has_type = true;
  4718. node_type.is_constant = true;
  4719. node_type.is_meta_type = false;
  4720. node_type.kind = DataType::BUILTIN;
  4721. node_type.builtin_type = Variant::BOOL;
  4722. } break;
  4723. // Unary operators
  4724. case OperatorNode::OP_NEG:
  4725. case OperatorNode::OP_POS:
  4726. case OperatorNode::OP_NOT:
  4727. case OperatorNode::OP_BIT_INVERT: {
  4728. DataType argument_type = _reduce_node_type(op->arguments[0]);
  4729. if (!argument_type.has_type) {
  4730. break;
  4731. }
  4732. Variant::Operator var_op = _get_variant_operation(op->op);
  4733. bool valid = false;
  4734. node_type = _get_operation_type(var_op, argument_type, argument_type, valid);
  4735. if (check_types && !valid) {
  4736. _set_error("Invalid operand type ('" + argument_type.to_string() +
  4737. "') to unary operator '" + Variant::get_operator_name(var_op) + "'.",
  4738. op->line, op->column);
  4739. return DataType();
  4740. }
  4741. } break;
  4742. // Binary operators
  4743. case OperatorNode::OP_IN:
  4744. case OperatorNode::OP_EQUAL:
  4745. case OperatorNode::OP_NOT_EQUAL:
  4746. case OperatorNode::OP_LESS:
  4747. case OperatorNode::OP_LESS_EQUAL:
  4748. case OperatorNode::OP_GREATER:
  4749. case OperatorNode::OP_GREATER_EQUAL:
  4750. case OperatorNode::OP_AND:
  4751. case OperatorNode::OP_OR:
  4752. case OperatorNode::OP_ADD:
  4753. case OperatorNode::OP_SUB:
  4754. case OperatorNode::OP_MUL:
  4755. case OperatorNode::OP_DIV:
  4756. case OperatorNode::OP_MOD:
  4757. case OperatorNode::OP_SHIFT_LEFT:
  4758. case OperatorNode::OP_SHIFT_RIGHT:
  4759. case OperatorNode::OP_BIT_AND:
  4760. case OperatorNode::OP_BIT_OR:
  4761. case OperatorNode::OP_BIT_XOR: {
  4762. if (op->arguments.size() != 2) {
  4763. _set_error("Parser bug: binary operation without 2 arguments.", op->line);
  4764. ERR_FAIL_V(DataType());
  4765. }
  4766. DataType argument_a_type = _reduce_node_type(op->arguments[0]);
  4767. DataType argument_b_type = _reduce_node_type(op->arguments[1]);
  4768. if (!argument_a_type.has_type || !argument_b_type.has_type) {
  4769. _mark_line_as_unsafe(op->line);
  4770. break;
  4771. }
  4772. Variant::Operator var_op = _get_variant_operation(op->op);
  4773. bool valid = false;
  4774. node_type = _get_operation_type(var_op, argument_a_type, argument_b_type, valid);
  4775. if (check_types && !valid) {
  4776. _set_error("Invalid operand types ('" + argument_a_type.to_string() + "' and '" +
  4777. argument_b_type.to_string() + "') to operator '" + Variant::get_operator_name(var_op) + "'.",
  4778. op->line, op->column);
  4779. return DataType();
  4780. }
  4781. } break;
  4782. // Ternary operators
  4783. case OperatorNode::OP_TERNARY_IF: {
  4784. if (op->arguments.size() != 3) {
  4785. _set_error("Parser bug: ternary operation without 3 arguments");
  4786. ERR_FAIL_V(DataType());
  4787. }
  4788. DataType true_type = _reduce_node_type(op->arguments[1]);
  4789. DataType false_type = _reduce_node_type(op->arguments[2]);
  4790. // If types are equal, then the expression is of the same type
  4791. // If they are compatible, return the broader type
  4792. if (true_type == false_type || _is_type_compatible(true_type, false_type)) {
  4793. node_type = true_type;
  4794. } else if (_is_type_compatible(false_type, true_type)) {
  4795. node_type = false_type;
  4796. }
  4797. // TODO: Warn if types aren't compatible
  4798. } break;
  4799. // Assignment should never happen within an expression
  4800. case OperatorNode::OP_ASSIGN:
  4801. case OperatorNode::OP_ASSIGN_ADD:
  4802. case OperatorNode::OP_ASSIGN_SUB:
  4803. case OperatorNode::OP_ASSIGN_MUL:
  4804. case OperatorNode::OP_ASSIGN_DIV:
  4805. case OperatorNode::OP_ASSIGN_MOD:
  4806. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  4807. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  4808. case OperatorNode::OP_ASSIGN_BIT_AND:
  4809. case OperatorNode::OP_ASSIGN_BIT_OR:
  4810. case OperatorNode::OP_ASSIGN_BIT_XOR:
  4811. case OperatorNode::OP_INIT_ASSIGN: {
  4812. _set_error("Assignment inside expression is not allowed (parser bug?).", op->line);
  4813. return DataType();
  4814. } break;
  4815. case OperatorNode::OP_INDEX_NAMED: {
  4816. if (op->arguments.size() != 2) {
  4817. _set_error("Parser bug: named index with invalid arguments.", op->line);
  4818. ERR_FAIL_V(DataType());
  4819. }
  4820. if (op->arguments[1]->type != Node::TYPE_IDENTIFIER) {
  4821. _set_error("Parser bug: named index without identifier argument.", op->line);
  4822. ERR_FAIL_V(DataType());
  4823. }
  4824. DataType base_type = _reduce_node_type(op->arguments[0]);
  4825. IdentifierNode *member_id = static_cast<IdentifierNode *>(op->arguments[1]);
  4826. if (base_type.has_type) {
  4827. if (check_types && base_type.kind == DataType::BUILTIN) {
  4828. // Variant type, just test if it's possible
  4829. DataType result;
  4830. switch (base_type.builtin_type) {
  4831. case Variant::NIL:
  4832. case Variant::DICTIONARY: {
  4833. result.has_type = false;
  4834. } break;
  4835. default: {
  4836. Variant::CallError err;
  4837. Variant temp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  4838. bool valid = false;
  4839. Variant res = temp.get(member_id->name.operator String(), &valid);
  4840. if (valid) {
  4841. result = _type_from_variant(res);
  4842. } else if (check_types) {
  4843. _set_error("Can't get index '" + String(member_id->name.operator String()) + "' on base '" +
  4844. base_type.to_string() + "'.",
  4845. op->line);
  4846. return DataType();
  4847. }
  4848. } break;
  4849. }
  4850. result.is_constant = false;
  4851. node_type = result;
  4852. } else {
  4853. node_type = _reduce_identifier_type(&base_type, member_id->name, op->line);
  4854. }
  4855. } else {
  4856. _mark_line_as_unsafe(op->line);
  4857. }
  4858. if (error_set) {
  4859. return DataType();
  4860. }
  4861. } break;
  4862. case OperatorNode::OP_INDEX: {
  4863. if (op->arguments[1]->type == Node::TYPE_CONSTANT) {
  4864. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[1]);
  4865. if (cn->value.get_type() == Variant::STRING) {
  4866. // Treat this as named indexing
  4867. IdentifierNode *id = alloc_node<IdentifierNode>();
  4868. id->name = cn->value.operator StringName();
  4869. op->op = OperatorNode::OP_INDEX_NAMED;
  4870. op->arguments.write[1] = id;
  4871. return _reduce_node_type(op);
  4872. }
  4873. }
  4874. DataType base_type = _reduce_node_type(op->arguments[0]);
  4875. DataType index_type = _reduce_node_type(op->arguments[1]);
  4876. if (!base_type.has_type) {
  4877. _mark_line_as_unsafe(op->line);
  4878. break;
  4879. }
  4880. if (check_types && index_type.has_type) {
  4881. if (base_type.kind == DataType::BUILTIN) {
  4882. // Check if indexing is valid
  4883. bool error = index_type.kind != DataType::BUILTIN;
  4884. if (!error) {
  4885. switch (base_type.builtin_type) {
  4886. // Expect int or real as index
  4887. case Variant::POOL_BYTE_ARRAY:
  4888. case Variant::POOL_COLOR_ARRAY:
  4889. case Variant::POOL_INT_ARRAY:
  4890. case Variant::POOL_REAL_ARRAY:
  4891. case Variant::POOL_STRING_ARRAY:
  4892. case Variant::POOL_VECTOR2_ARRAY:
  4893. case Variant::POOL_VECTOR3_ARRAY:
  4894. case Variant::ARRAY:
  4895. case Variant::STRING: {
  4896. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::REAL;
  4897. } break;
  4898. // Expect String only
  4899. case Variant::RECT2:
  4900. case Variant::PLANE:
  4901. case Variant::QUAT:
  4902. case Variant::AABB:
  4903. case Variant::OBJECT: {
  4904. error = index_type.builtin_type != Variant::STRING;
  4905. } break;
  4906. // Expect String or number
  4907. case Variant::VECTOR2:
  4908. case Variant::VECTOR3:
  4909. case Variant::TRANSFORM2D:
  4910. case Variant::BASIS:
  4911. case Variant::TRANSFORM: {
  4912. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::REAL &&
  4913. index_type.builtin_type != Variant::STRING;
  4914. } break;
  4915. // Expect String or int
  4916. case Variant::COLOR: {
  4917. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING;
  4918. } break;
  4919. }
  4920. }
  4921. if (error) {
  4922. _set_error("Invalid index type (" + index_type.to_string() + ") for base '" + base_type.to_string() + "'.",
  4923. op->line);
  4924. return DataType();
  4925. }
  4926. if (op->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT) {
  4927. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[1]);
  4928. // Index is a constant, just try it if possible
  4929. switch (base_type.builtin_type) {
  4930. // Arrays/string have variable indexing, can't test directly
  4931. case Variant::STRING:
  4932. case Variant::ARRAY:
  4933. case Variant::DICTIONARY:
  4934. case Variant::POOL_BYTE_ARRAY:
  4935. case Variant::POOL_COLOR_ARRAY:
  4936. case Variant::POOL_INT_ARRAY:
  4937. case Variant::POOL_REAL_ARRAY:
  4938. case Variant::POOL_STRING_ARRAY:
  4939. case Variant::POOL_VECTOR2_ARRAY:
  4940. case Variant::POOL_VECTOR3_ARRAY: {
  4941. break;
  4942. }
  4943. default: {
  4944. Variant::CallError err;
  4945. Variant temp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  4946. bool valid = false;
  4947. Variant res = temp.get(cn->value, &valid);
  4948. if (valid) {
  4949. node_type = _type_from_variant(res);
  4950. node_type.is_constant = false;
  4951. } else if (check_types) {
  4952. _set_error("Can't get index '" + String(cn->value) + "' on base '" +
  4953. base_type.to_string() + "'.",
  4954. op->line);
  4955. return DataType();
  4956. }
  4957. } break;
  4958. }
  4959. } else {
  4960. _mark_line_as_unsafe(op->line);
  4961. }
  4962. } else if (!for_completion && (index_type.kind != DataType::BUILTIN || index_type.builtin_type != Variant::STRING)) {
  4963. _set_error("Only strings can be used as index in the base type '" + base_type.to_string() + "'.", op->line);
  4964. return DataType();
  4965. }
  4966. }
  4967. if (check_types && !node_type.has_type) {
  4968. // Can infer indexing type for some variant types
  4969. DataType result;
  4970. result.has_type = true;
  4971. result.kind = DataType::BUILTIN;
  4972. switch (base_type.builtin_type) {
  4973. // Can't index at all
  4974. case Variant::NIL:
  4975. case Variant::BOOL:
  4976. case Variant::INT:
  4977. case Variant::REAL:
  4978. case Variant::NODE_PATH:
  4979. case Variant::_RID: {
  4980. _set_error("Can't index on a value of type '" + base_type.to_string() + "'.", op->line);
  4981. return DataType();
  4982. } break;
  4983. // Return int
  4984. case Variant::POOL_BYTE_ARRAY:
  4985. case Variant::POOL_INT_ARRAY: {
  4986. result.builtin_type = Variant::INT;
  4987. } break;
  4988. // Return real
  4989. case Variant::POOL_REAL_ARRAY:
  4990. case Variant::VECTOR2:
  4991. case Variant::VECTOR3:
  4992. case Variant::QUAT: {
  4993. result.builtin_type = Variant::REAL;
  4994. } break;
  4995. // Return color
  4996. case Variant::POOL_COLOR_ARRAY: {
  4997. result.builtin_type = Variant::COLOR;
  4998. } break;
  4999. // Return string
  5000. case Variant::POOL_STRING_ARRAY:
  5001. case Variant::STRING: {
  5002. result.builtin_type = Variant::STRING;
  5003. } break;
  5004. // Return Vector2
  5005. case Variant::POOL_VECTOR2_ARRAY:
  5006. case Variant::TRANSFORM2D:
  5007. case Variant::RECT2: {
  5008. result.builtin_type = Variant::VECTOR2;
  5009. } break;
  5010. // Return Vector3
  5011. case Variant::POOL_VECTOR3_ARRAY:
  5012. case Variant::AABB:
  5013. case Variant::BASIS: {
  5014. result.builtin_type = Variant::VECTOR3;
  5015. } break;
  5016. // Depends on the index
  5017. case Variant::TRANSFORM:
  5018. case Variant::PLANE:
  5019. case Variant::COLOR:
  5020. default: {
  5021. result.has_type = false;
  5022. } break;
  5023. }
  5024. node_type = result;
  5025. }
  5026. } break;
  5027. default: {
  5028. _set_error("Parser bug: unhandled operation.", op->line);
  5029. ERR_FAIL_V(DataType());
  5030. }
  5031. }
  5032. } break;
  5033. }
  5034. p_node->set_datatype(_resolve_type(node_type, p_node->line));
  5035. return node_type;
  5036. }
  5037. 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 {
  5038. r_static = false;
  5039. r_default_arg_count = 0;
  5040. DataType original_type = p_base_type;
  5041. ClassNode *base = NULL;
  5042. FunctionNode *callee = NULL;
  5043. if (p_base_type.kind == DataType::CLASS) {
  5044. base = p_base_type.class_type;
  5045. }
  5046. // Look up the current file (parse tree)
  5047. while (!callee && base) {
  5048. for (int i = 0; i < base->static_functions.size(); i++) {
  5049. FunctionNode *func = base->static_functions[i];
  5050. if (p_function == func->name) {
  5051. r_static = true;
  5052. callee = func;
  5053. break;
  5054. }
  5055. }
  5056. if (!callee && !p_base_type.is_meta_type) {
  5057. for (int i = 0; i < base->functions.size(); i++) {
  5058. FunctionNode *func = base->functions[i];
  5059. if (p_function == func->name) {
  5060. callee = func;
  5061. break;
  5062. }
  5063. }
  5064. }
  5065. p_base_type = base->base_type;
  5066. if (p_base_type.kind == DataType::CLASS) {
  5067. base = p_base_type.class_type;
  5068. } else {
  5069. break;
  5070. }
  5071. }
  5072. if (callee) {
  5073. r_return_type = callee->get_datatype();
  5074. for (int i = 0; i < callee->argument_types.size(); i++) {
  5075. r_arg_types.push_back(callee->argument_types[i]);
  5076. }
  5077. r_default_arg_count = callee->default_values.size();
  5078. return true;
  5079. }
  5080. // Nothing in current file, check parent script
  5081. Ref<GDScript> base_gdscript;
  5082. Ref<Script> base_script;
  5083. StringName native;
  5084. if (p_base_type.kind == DataType::GDSCRIPT) {
  5085. base_gdscript = p_base_type.script_type;
  5086. } else if (p_base_type.kind == DataType::SCRIPT) {
  5087. base_script = p_base_type.script_type;
  5088. } else if (p_base_type.kind == DataType::NATIVE) {
  5089. native = p_base_type.native_type;
  5090. }
  5091. while (base_gdscript.is_valid()) {
  5092. native = base_gdscript->get_instance_base_type();
  5093. Map<StringName, GDScriptFunction *> funcs = base_gdscript->get_member_functions();
  5094. if (funcs.has(p_function)) {
  5095. GDScriptFunction *f = funcs[p_function];
  5096. r_static = f->is_static();
  5097. r_default_arg_count = f->get_default_argument_count();
  5098. r_return_type = _type_from_gdtype(f->get_return_type());
  5099. for (int i = 0; i < f->get_argument_count(); i++) {
  5100. r_arg_types.push_back(_type_from_gdtype(f->get_argument_type(i)));
  5101. }
  5102. return true;
  5103. }
  5104. base_gdscript = base_gdscript->get_base_script();
  5105. }
  5106. while (base_script.is_valid()) {
  5107. native = base_script->get_instance_base_type();
  5108. MethodInfo mi = base_script->get_method_info(p_function);
  5109. if (!(mi == MethodInfo())) {
  5110. r_return_type = _type_from_property(mi.return_val, false);
  5111. r_default_arg_count = mi.default_arguments.size();
  5112. for (List<PropertyInfo>::Element *E = mi.arguments.front(); E; E = E->next()) {
  5113. r_arg_types.push_back(_type_from_property(E->get()));
  5114. }
  5115. return true;
  5116. }
  5117. base_script = base_script->get_base_script();
  5118. }
  5119. #ifdef DEBUG_METHODS_ENABLED
  5120. // Only native remains
  5121. if (!ClassDB::class_exists(native)) {
  5122. native = "_" + native.operator String();
  5123. }
  5124. if (!ClassDB::class_exists(native)) {
  5125. if (!check_types) return false;
  5126. ERR_EXPLAIN("Parser bug: Class '" + String(native) + "' not found.");
  5127. ERR_FAIL_V(false);
  5128. }
  5129. MethodBind *method = ClassDB::get_method(native, p_function);
  5130. if (!method) {
  5131. // Try virtual methods
  5132. List<MethodInfo> virtuals;
  5133. ClassDB::get_virtual_methods(native, &virtuals);
  5134. for (const List<MethodInfo>::Element *E = virtuals.front(); E; E = E->next()) {
  5135. const MethodInfo &mi = E->get();
  5136. if (mi.name == p_function) {
  5137. r_default_arg_count = mi.default_arguments.size();
  5138. for (const List<PropertyInfo>::Element *pi = mi.arguments.front(); pi; pi = pi->next()) {
  5139. r_arg_types.push_back(_type_from_property(pi->get()));
  5140. }
  5141. r_return_type = _type_from_property(mi.return_val, false);
  5142. r_vararg = mi.flags & METHOD_FLAG_VARARG;
  5143. return true;
  5144. }
  5145. }
  5146. // If the base is a script, it might be trying to access members of the Script class itself
  5147. if (original_type.is_meta_type && !(p_function == "new") && (original_type.kind == DataType::SCRIPT || original_type.kind == DataType::GDSCRIPT)) {
  5148. method = ClassDB::get_method(original_type.script_type->get_class_name(), p_function);
  5149. if (method) {
  5150. r_static = true;
  5151. } else {
  5152. // Try virtual methods of the script type
  5153. virtuals.clear();
  5154. ClassDB::get_virtual_methods(original_type.script_type->get_class_name(), &virtuals);
  5155. for (const List<MethodInfo>::Element *E = virtuals.front(); E; E = E->next()) {
  5156. const MethodInfo &mi = E->get();
  5157. if (mi.name == p_function) {
  5158. r_default_arg_count = mi.default_arguments.size();
  5159. for (const List<PropertyInfo>::Element *pi = mi.arguments.front(); pi; pi = pi->next()) {
  5160. r_arg_types.push_back(_type_from_property(pi->get()));
  5161. }
  5162. r_return_type = _type_from_property(mi.return_val, false);
  5163. r_static = true;
  5164. r_vararg = mi.flags & METHOD_FLAG_VARARG;
  5165. return true;
  5166. }
  5167. }
  5168. return false;
  5169. }
  5170. } else {
  5171. return false;
  5172. }
  5173. }
  5174. r_default_arg_count = method->get_default_argument_count();
  5175. r_return_type = _type_from_property(method->get_return_info(), false);
  5176. r_vararg = method->is_vararg();
  5177. for (int i = 0; i < method->get_argument_count(); i++) {
  5178. r_arg_types.push_back(_type_from_property(method->get_argument_info(i)));
  5179. }
  5180. return true;
  5181. #else
  5182. return false;
  5183. #endif
  5184. }
  5185. GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const OperatorNode *p_call) {
  5186. if (p_call->arguments.size() < 1) {
  5187. _set_error("Parser bug: function call without enough arguments.", p_call->line);
  5188. ERR_FAIL_V(DataType());
  5189. }
  5190. DataType return_type;
  5191. List<DataType> arg_types;
  5192. int default_args_count = 0;
  5193. int arg_count = p_call->arguments.size();
  5194. String callee_name;
  5195. bool is_vararg = false;
  5196. switch (p_call->arguments[0]->type) {
  5197. case GDScriptParser::Node::TYPE_TYPE: {
  5198. // Built-in constructor, special case
  5199. TypeNode *tn = static_cast<TypeNode *>(p_call->arguments[0]);
  5200. Vector<DataType> par_types;
  5201. par_types.resize(p_call->arguments.size() - 1);
  5202. for (int i = 1; i < p_call->arguments.size(); i++) {
  5203. par_types.write[i - 1] = _reduce_node_type(p_call->arguments[i]);
  5204. }
  5205. if (error_set) return DataType();
  5206. bool match = false;
  5207. List<MethodInfo> constructors;
  5208. Variant::get_constructor_list(tn->vtype, &constructors);
  5209. PropertyInfo return_type;
  5210. for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
  5211. MethodInfo &mi = E->get();
  5212. if (p_call->arguments.size() - 1 < mi.arguments.size() - mi.default_arguments.size()) {
  5213. continue;
  5214. }
  5215. if (p_call->arguments.size() - 1 > mi.arguments.size()) {
  5216. continue;
  5217. }
  5218. bool types_match = true;
  5219. for (int i = 0; i < par_types.size(); i++) {
  5220. DataType arg_type;
  5221. if (mi.arguments[i].type != Variant::NIL) {
  5222. arg_type.has_type = true;
  5223. arg_type.kind = mi.arguments[i].type == Variant::OBJECT ? DataType::NATIVE : DataType::BUILTIN;
  5224. arg_type.builtin_type = mi.arguments[i].type;
  5225. arg_type.native_type = mi.arguments[i].class_name;
  5226. }
  5227. if (!_is_type_compatible(arg_type, par_types[i], true)) {
  5228. types_match = false;
  5229. break;
  5230. }
  5231. }
  5232. if (types_match) {
  5233. match = true;
  5234. return_type = mi.return_val;
  5235. break;
  5236. }
  5237. }
  5238. if (match) {
  5239. return _type_from_property(return_type, false);
  5240. } else if (check_types) {
  5241. String err = "No constructor of '";
  5242. err += Variant::get_type_name(tn->vtype);
  5243. err += "' matches the signature '";
  5244. err += Variant::get_type_name(tn->vtype) + "(";
  5245. for (int i = 0; i < par_types.size(); i++) {
  5246. if (i > 0) err += ", ";
  5247. err += par_types[i].to_string();
  5248. }
  5249. err += ")'.";
  5250. _set_error(err, p_call->line, p_call->column);
  5251. return DataType();
  5252. }
  5253. return DataType();
  5254. } break;
  5255. case GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION: {
  5256. BuiltInFunctionNode *func = static_cast<BuiltInFunctionNode *>(p_call->arguments[0]);
  5257. MethodInfo mi = GDScriptFunctions::get_info(func->function);
  5258. return_type = _type_from_property(mi.return_val, false);
  5259. // Check arguments
  5260. is_vararg = mi.flags & METHOD_FLAG_VARARG;
  5261. default_args_count = mi.default_arguments.size();
  5262. callee_name = mi.name;
  5263. arg_count -= 1;
  5264. // Check each argument type
  5265. for (List<PropertyInfo>::Element *E = mi.arguments.front(); E; E = E->next()) {
  5266. arg_types.push_back(_type_from_property(E->get()));
  5267. }
  5268. } break;
  5269. default: {
  5270. if (p_call->op == OperatorNode::OP_CALL && p_call->arguments.size() < 2) {
  5271. _set_error("Parser bug: self method call without enough arguments.", p_call->line);
  5272. ERR_FAIL_V(DataType());
  5273. }
  5274. int arg_id = p_call->op == OperatorNode::OP_CALL ? 1 : 0;
  5275. if (p_call->arguments[arg_id]->type != Node::TYPE_IDENTIFIER) {
  5276. _set_error("Parser bug: invalid function call argument.", p_call->line);
  5277. ERR_FAIL_V(DataType());
  5278. }
  5279. IdentifierNode *func_id = static_cast<IdentifierNode *>(p_call->arguments[arg_id]);
  5280. callee_name = func_id->name;
  5281. arg_count -= 1 + arg_id;
  5282. DataType base_type;
  5283. if (p_call->op == OperatorNode::OP_PARENT_CALL) {
  5284. base_type = current_class->base_type;
  5285. } else {
  5286. base_type = _reduce_node_type(p_call->arguments[0]);
  5287. }
  5288. if (!base_type.has_type || (base_type.kind == DataType::BUILTIN && base_type.builtin_type == Variant::NIL)) {
  5289. _mark_line_as_unsafe(p_call->line);
  5290. return DataType();
  5291. }
  5292. if (base_type.kind == DataType::BUILTIN) {
  5293. Variant::CallError err;
  5294. Variant tmp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  5295. if (check_types) {
  5296. if (!tmp.has_method(callee_name)) {
  5297. _set_error("Method '" + callee_name + "' is not declared on base '" + base_type.to_string() + "'.", p_call->line);
  5298. return DataType();
  5299. }
  5300. default_args_count = Variant::get_method_default_arguments(base_type.builtin_type, callee_name).size();
  5301. const Vector<Variant::Type> &var_arg_types = Variant::get_method_argument_types(base_type.builtin_type, callee_name);
  5302. for (int i = 0; i < var_arg_types.size(); i++) {
  5303. DataType argtype;
  5304. if (var_arg_types[i] != Variant::NIL) {
  5305. argtype.has_type = true;
  5306. argtype.kind = DataType::BUILTIN;
  5307. argtype.builtin_type = var_arg_types[i];
  5308. }
  5309. arg_types.push_back(argtype);
  5310. }
  5311. }
  5312. return_type.has_type = true;
  5313. return_type.kind = DataType::BUILTIN;
  5314. return_type.builtin_type = Variant::get_method_return_type(base_type.builtin_type, callee_name);
  5315. break;
  5316. }
  5317. DataType original_type = base_type;
  5318. bool is_initializer = callee_name == "new";
  5319. bool is_static = false;
  5320. bool valid = false;
  5321. if (is_initializer && original_type.is_meta_type) {
  5322. // Try to check it as initializer
  5323. base_type = original_type;
  5324. callee_name = "_init";
  5325. base_type.is_meta_type = false;
  5326. valid = _get_function_signature(base_type, callee_name, return_type, arg_types,
  5327. default_args_count, is_static, is_vararg);
  5328. if (valid) {
  5329. return_type = original_type;
  5330. return_type.is_meta_type = false;
  5331. }
  5332. }
  5333. if (!valid) {
  5334. base_type = original_type;
  5335. return_type = DataType();
  5336. valid = _get_function_signature(base_type, callee_name, return_type, arg_types,
  5337. default_args_count, is_static, is_vararg);
  5338. }
  5339. if (!valid) {
  5340. #ifdef DEBUG_ENABLED
  5341. if (p_call->arguments[0]->type == Node::TYPE_SELF) {
  5342. _set_error("Method '" + callee_name + "' is not declared in the current class.", p_call->line);
  5343. return DataType();
  5344. }
  5345. _mark_line_as_unsafe(p_call->line);
  5346. #endif
  5347. return DataType();
  5348. }
  5349. #ifdef DEBUG_ENABLED
  5350. if (current_function && !for_completion && !is_static && p_call->arguments[0]->type == Node::TYPE_SELF && current_function->_static) {
  5351. if (current_function && current_function->_static && p_call->arguments[0]->type == Node::TYPE_SELF) {
  5352. _set_error("Can't call non-static function from a static function.", p_call->line);
  5353. return DataType();
  5354. }
  5355. }
  5356. if (check_types && !is_static && !is_initializer && base_type.is_meta_type) {
  5357. _set_error("Non-static function '" + String(callee_name) + "' can only be called from an instance.", p_call->line);
  5358. return DataType();
  5359. }
  5360. #endif
  5361. } break;
  5362. }
  5363. if (!check_types) {
  5364. return return_type;
  5365. }
  5366. if (arg_count < arg_types.size() - default_args_count) {
  5367. _set_error("Too few arguments for '" + callee_name + "()' call. Expected at least " + itos(arg_types.size() - default_args_count) + ".", p_call->line);
  5368. return return_type;
  5369. }
  5370. if (!is_vararg && arg_count > arg_types.size()) {
  5371. _set_error("Too many arguments for '" + callee_name + "()' call. Expected at most " + itos(arg_types.size()) + ".", p_call->line);
  5372. return return_type;
  5373. }
  5374. int arg_diff = p_call->arguments.size() - arg_count;
  5375. for (int i = arg_diff; i < p_call->arguments.size(); i++) {
  5376. DataType par_type = _reduce_node_type(p_call->arguments[i]);
  5377. if ((i - arg_diff) >= arg_types.size()) {
  5378. continue;
  5379. }
  5380. if (!par_type.has_type) {
  5381. _mark_line_as_unsafe(p_call->line);
  5382. } else if (!_is_type_compatible(arg_types[i - arg_diff], par_type, true)) {
  5383. // Supertypes are acceptable for dynamic compliance
  5384. if (!_is_type_compatible(par_type, arg_types[i - arg_diff])) {
  5385. _set_error("At '" + callee_name + "()' call, argument " + itos(i - arg_diff + 1) + ". Assigned type (" +
  5386. par_type.to_string() + ") doesn't match the function argument's type (" +
  5387. arg_types[i - arg_diff].to_string() + ").",
  5388. p_call->line);
  5389. return DataType();
  5390. } else {
  5391. _mark_line_as_unsafe(p_call->line);
  5392. }
  5393. }
  5394. }
  5395. return return_type;
  5396. }
  5397. bool GDScriptParser::_get_member_type(const DataType &p_base_type, const StringName &p_member, DataType &r_member_type) const {
  5398. DataType base_type = p_base_type;
  5399. // Check classes in current file
  5400. ClassNode *base = NULL;
  5401. if (base_type.kind == DataType::CLASS) {
  5402. base = base_type.class_type;
  5403. }
  5404. while (base) {
  5405. if (base->constant_expressions.has(p_member)) {
  5406. r_member_type = base->constant_expressions[p_member].expression->get_datatype();
  5407. return true;
  5408. }
  5409. if (!base_type.is_meta_type) {
  5410. for (int i = 0; i < base->variables.size(); i++) {
  5411. ClassNode::Member m = base->variables[i];
  5412. if (m.identifier == p_member) {
  5413. r_member_type = m.data_type;
  5414. return true;
  5415. }
  5416. }
  5417. } else {
  5418. for (int i = 0; i < base->subclasses.size(); i++) {
  5419. ClassNode *c = base->subclasses[i];
  5420. if (c->name == p_member) {
  5421. DataType class_type;
  5422. class_type.has_type = true;
  5423. class_type.is_constant = true;
  5424. class_type.is_meta_type = true;
  5425. class_type.kind = DataType::CLASS;
  5426. class_type.class_type = c;
  5427. r_member_type = class_type;
  5428. return true;
  5429. }
  5430. }
  5431. }
  5432. base_type = base->base_type;
  5433. if (base_type.kind == DataType::CLASS) {
  5434. base = base_type.class_type;
  5435. } else {
  5436. break;
  5437. }
  5438. }
  5439. Ref<GDScript> gds;
  5440. if (base_type.kind == DataType::GDSCRIPT) {
  5441. gds = base_type.script_type;
  5442. }
  5443. Ref<Script> scr;
  5444. if (base_type.kind == DataType::SCRIPT) {
  5445. scr = base_type.script_type;
  5446. }
  5447. StringName native;
  5448. if (base_type.kind == DataType::NATIVE) {
  5449. native = base_type.native_type;
  5450. }
  5451. // Check GDScripts
  5452. while (gds.is_valid()) {
  5453. if (gds->get_constants().has(p_member)) {
  5454. Variant c = gds->get_constants()[p_member];
  5455. r_member_type = _type_from_variant(c);
  5456. return true;
  5457. }
  5458. if (!base_type.is_meta_type) {
  5459. if (gds->get_members().has(p_member)) {
  5460. r_member_type = _type_from_gdtype(gds->get_member_type(p_member));
  5461. return true;
  5462. }
  5463. }
  5464. native = gds->get_instance_base_type();
  5465. if (gds->get_base_script().is_valid()) {
  5466. gds = gds->get_base_script();
  5467. scr = gds->get_base_script();
  5468. bool is_meta = base_type.is_meta_type;
  5469. base_type = _type_from_variant(scr.operator Variant());
  5470. base_type.is_meta_type = is_meta;
  5471. } else {
  5472. break;
  5473. }
  5474. }
  5475. // Check other script types
  5476. while (scr.is_valid()) {
  5477. Map<StringName, Variant> constants;
  5478. scr->get_constants(&constants);
  5479. if (constants.has(p_member)) {
  5480. r_member_type = _type_from_variant(constants[p_member]);
  5481. return true;
  5482. }
  5483. List<PropertyInfo> properties;
  5484. scr->get_script_property_list(&properties);
  5485. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  5486. if (E->get().name == p_member) {
  5487. r_member_type = _type_from_property(E->get());
  5488. return true;
  5489. }
  5490. }
  5491. base_type = _type_from_variant(scr.operator Variant());
  5492. native = scr->get_instance_base_type();
  5493. scr = scr->get_base_script();
  5494. }
  5495. // Check ClassDB
  5496. if (!ClassDB::class_exists(native)) {
  5497. native = "_" + native.operator String();
  5498. }
  5499. if (!ClassDB::class_exists(native)) {
  5500. if (!check_types) return false;
  5501. ERR_EXPLAIN("Parser bug: Class '" + String(native) + "' not found.");
  5502. ERR_FAIL_V(false);
  5503. }
  5504. bool valid = false;
  5505. ClassDB::get_integer_constant(native, p_member, &valid);
  5506. if (valid) {
  5507. DataType ct;
  5508. ct.has_type = true;
  5509. ct.is_constant = true;
  5510. ct.kind = DataType::BUILTIN;
  5511. ct.builtin_type = Variant::INT;
  5512. r_member_type = ct;
  5513. return true;
  5514. }
  5515. if (!base_type.is_meta_type) {
  5516. List<PropertyInfo> properties;
  5517. ClassDB::get_property_list(native, &properties);
  5518. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  5519. if (E->get().name == p_member) {
  5520. // Check if a getter exists
  5521. StringName getter_name = ClassDB::get_property_getter(native, p_member);
  5522. if (getter_name != StringName()) {
  5523. // Use the getter return type
  5524. #ifdef DEBUG_METHODS_ENABLED
  5525. MethodBind *getter_method = ClassDB::get_method(native, getter_name);
  5526. if (getter_method) {
  5527. r_member_type = _type_from_property(getter_method->get_return_info());
  5528. } else {
  5529. r_member_type = DataType();
  5530. }
  5531. #else
  5532. r_member_type = DataType();
  5533. #endif
  5534. } else {
  5535. r_member_type = _type_from_property(E->get());
  5536. }
  5537. return true;
  5538. }
  5539. }
  5540. }
  5541. // If the base is a script, it might be trying to access members of the Script class itself
  5542. if (p_base_type.is_meta_type && (p_base_type.kind == DataType::SCRIPT || p_base_type.kind == DataType::GDSCRIPT)) {
  5543. native = p_base_type.script_type->get_class_name();
  5544. ClassDB::get_integer_constant(native, p_member, &valid);
  5545. if (valid) {
  5546. DataType ct;
  5547. ct.has_type = true;
  5548. ct.is_constant = true;
  5549. ct.kind = DataType::BUILTIN;
  5550. ct.builtin_type = Variant::INT;
  5551. r_member_type = ct;
  5552. return true;
  5553. }
  5554. List<PropertyInfo> properties;
  5555. ClassDB::get_property_list(native, &properties);
  5556. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  5557. if (E->get().name == p_member) {
  5558. // Check if a getter exists
  5559. StringName getter_name = ClassDB::get_property_getter(native, p_member);
  5560. if (getter_name != StringName()) {
  5561. // Use the getter return type
  5562. #ifdef DEBUG_METHODS_ENABLED
  5563. MethodBind *getter_method = ClassDB::get_method(native, getter_name);
  5564. if (getter_method) {
  5565. r_member_type = _type_from_property(getter_method->get_return_info());
  5566. } else {
  5567. r_member_type = DataType();
  5568. }
  5569. #else
  5570. r_member_type = DataType();
  5571. #endif
  5572. } else {
  5573. r_member_type = _type_from_property(E->get());
  5574. }
  5575. return true;
  5576. }
  5577. }
  5578. }
  5579. return false;
  5580. }
  5581. GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType *p_base_type, const StringName &p_identifier, int p_line) {
  5582. if (p_base_type && !p_base_type->has_type) {
  5583. return DataType();
  5584. }
  5585. DataType base_type;
  5586. // Check classes in current file
  5587. ClassNode *base = NULL;
  5588. if (!p_base_type) {
  5589. base = current_class;
  5590. base_type.has_type = true;
  5591. base_type.is_constant = true;
  5592. base_type.kind = DataType::CLASS;
  5593. base_type.class_type = base;
  5594. } else {
  5595. base_type = DataType(*p_base_type);
  5596. if (base_type.kind == DataType::CLASS) {
  5597. base = base_type.class_type;
  5598. }
  5599. }
  5600. DataType member_type;
  5601. if (_get_member_type(base_type, p_identifier, member_type)) {
  5602. return member_type;
  5603. }
  5604. if (!p_base_type) {
  5605. // Possibly this is a global, check before failing
  5606. if (ClassDB::class_exists(p_identifier) || ClassDB::class_exists("_" + p_identifier.operator String())) {
  5607. DataType result;
  5608. result.has_type = true;
  5609. result.is_constant = true;
  5610. result.is_meta_type = true;
  5611. if (Engine::get_singleton()->has_singleton(p_identifier) || Engine::get_singleton()->has_singleton("_" + p_identifier.operator String())) {
  5612. result.is_meta_type = false;
  5613. }
  5614. result.kind = DataType::NATIVE;
  5615. result.native_type = p_identifier;
  5616. return result;
  5617. }
  5618. ClassNode *outer_class = current_class;
  5619. while (outer_class) {
  5620. if (outer_class->name == p_identifier) {
  5621. DataType result;
  5622. result.has_type = true;
  5623. result.is_constant = true;
  5624. result.is_meta_type = true;
  5625. result.kind = DataType::CLASS;
  5626. result.class_type = outer_class;
  5627. return result;
  5628. }
  5629. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  5630. if (outer_class->subclasses[i] == current_class) {
  5631. continue;
  5632. }
  5633. if (outer_class->subclasses[i]->name == p_identifier) {
  5634. DataType result;
  5635. result.has_type = true;
  5636. result.is_constant = true;
  5637. result.is_meta_type = true;
  5638. result.kind = DataType::CLASS;
  5639. result.class_type = outer_class->subclasses[i];
  5640. return result;
  5641. }
  5642. }
  5643. outer_class = outer_class->owner;
  5644. }
  5645. if (ScriptServer::is_global_class(p_identifier)) {
  5646. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  5647. if (scr.is_valid()) {
  5648. DataType result;
  5649. result.has_type = true;
  5650. result.script_type = scr;
  5651. result.is_meta_type = true;
  5652. Ref<GDScript> gds = scr;
  5653. if (gds.is_valid()) {
  5654. if (!gds->is_valid()) {
  5655. _set_error("Class '" + p_identifier + "' could not be fully loaded (script error or cyclic inheritance).");
  5656. return DataType();
  5657. }
  5658. result.kind = DataType::GDSCRIPT;
  5659. } else {
  5660. result.kind = DataType::SCRIPT;
  5661. }
  5662. return result;
  5663. }
  5664. _set_error("Class '" + p_identifier + "' was found in global scope but its script could not be loaded.");
  5665. return DataType();
  5666. }
  5667. if (GDScriptLanguage::get_singleton()->get_global_map().has(p_identifier)) {
  5668. int idx = GDScriptLanguage::get_singleton()->get_global_map()[p_identifier];
  5669. Variant g = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  5670. return _type_from_variant(g);
  5671. }
  5672. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier)) {
  5673. Variant g = GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier];
  5674. return _type_from_variant(g);
  5675. }
  5676. // Non-tool singletons aren't loaded, check project settings
  5677. List<PropertyInfo> props;
  5678. ProjectSettings::get_singleton()->get_property_list(&props);
  5679. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  5680. String s = E->get().name;
  5681. if (!s.begins_with("autoload/")) {
  5682. continue;
  5683. }
  5684. String name = s.get_slice("/", 1);
  5685. if (name == p_identifier) {
  5686. String script = ProjectSettings::get_singleton()->get(s);
  5687. if (script.begins_with("*")) {
  5688. script = script.right(1);
  5689. }
  5690. if (!script.begins_with("res://")) {
  5691. script = "res://" + script;
  5692. }
  5693. Ref<Script> singleton = ResourceLoader::load(script);
  5694. if (singleton.is_valid()) {
  5695. DataType result;
  5696. result.has_type = true;
  5697. result.script_type = singleton;
  5698. Ref<GDScript> gds = singleton;
  5699. if (gds.is_valid()) {
  5700. if (!gds->is_valid()) {
  5701. _set_error("Couldn't fully load singleton script '" + p_identifier + "' (possible cyclic reference or parse error).", p_line);
  5702. return DataType();
  5703. }
  5704. result.kind = DataType::GDSCRIPT;
  5705. } else {
  5706. result.kind = DataType::SCRIPT;
  5707. }
  5708. }
  5709. }
  5710. }
  5711. // This means looking in the current class, which type is always known
  5712. _set_error("Identifier '" + p_identifier.operator String() + "' is not declared in the current scope.", p_line);
  5713. }
  5714. _mark_line_as_unsafe(p_line);
  5715. return DataType();
  5716. }
  5717. void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
  5718. _mark_line_as_safe(p_class->line);
  5719. // Constants
  5720. for (Map<StringName, ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
  5721. ClassNode::Constant &c = E->get();
  5722. _mark_line_as_safe(c.expression->line);
  5723. DataType cont = _resolve_type(c.type, c.expression->line);
  5724. DataType expr = _resolve_type(c.expression->get_datatype(), c.expression->line);
  5725. if (!_is_type_compatible(cont, expr)) {
  5726. _set_error("Constant value type (" + expr.to_string() + ") is not compatible with declared type (" + cont.to_string() + ").",
  5727. c.expression->line);
  5728. return;
  5729. }
  5730. expr.is_constant = true;
  5731. c.type = expr;
  5732. c.expression->set_datatype(expr);
  5733. }
  5734. // Function declarations
  5735. for (int i = 0; i < p_class->static_functions.size(); i++) {
  5736. _check_function_types(p_class->static_functions[i]);
  5737. if (error_set) return;
  5738. }
  5739. for (int i = 0; i < p_class->functions.size(); i++) {
  5740. _check_function_types(p_class->functions[i]);
  5741. if (error_set) return;
  5742. }
  5743. // Class variables
  5744. for (int i = 0; i < p_class->variables.size(); i++) {
  5745. ClassNode::Member &v = p_class->variables.write[i];
  5746. DataType tmp;
  5747. if (_get_member_type(p_class->base_type, v.identifier, tmp)) {
  5748. _set_error("Member '" + String(v.identifier) + "' already exists in parent class.", v.line);
  5749. return;
  5750. }
  5751. _mark_line_as_safe(v.line);
  5752. v.data_type = _resolve_type(v.data_type, v.line);
  5753. if (v.expression) {
  5754. DataType expr_type = _reduce_node_type(v.expression);
  5755. if (!_is_type_compatible(v.data_type, expr_type)) {
  5756. // Try supertype test
  5757. if (_is_type_compatible(expr_type, v.data_type)) {
  5758. _mark_line_as_unsafe(v.line);
  5759. } else {
  5760. // Try with implicit conversion
  5761. if (v.data_type.kind != DataType::BUILTIN || !_is_type_compatible(v.data_type, expr_type, true)) {
  5762. _set_error("Assigned expression type (" + expr_type.to_string() + ") doesn't match the variable's type (" +
  5763. v.data_type.to_string() + ").",
  5764. v.line);
  5765. return;
  5766. }
  5767. // Replace assigment with implict conversion
  5768. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  5769. convert->line = v.line;
  5770. convert->function = GDScriptFunctions::TYPE_CONVERT;
  5771. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  5772. tgt_type->line = v.line;
  5773. tgt_type->value = (int)v.data_type.builtin_type;
  5774. OperatorNode *convert_call = alloc_node<OperatorNode>();
  5775. convert_call->line = v.line;
  5776. convert_call->op = OperatorNode::OP_CALL;
  5777. convert_call->arguments.push_back(convert);
  5778. convert_call->arguments.push_back(v.expression);
  5779. convert_call->arguments.push_back(tgt_type);
  5780. v.expression = convert_call;
  5781. v.initial_assignment->arguments.write[1] = convert_call;
  5782. }
  5783. }
  5784. if (v.data_type.infer_type) {
  5785. if (!expr_type.has_type) {
  5786. _set_error("Assigned value does not have a set type, variable type cannot be inferred.", v.line);
  5787. return;
  5788. }
  5789. v.data_type = expr_type;
  5790. v.data_type.is_constant = false;
  5791. }
  5792. } else if (v.data_type.has_type && v.data_type.kind == DataType::BUILTIN) {
  5793. // Create default value based on the type
  5794. IdentifierNode *id = alloc_node<IdentifierNode>();
  5795. id->line = v.line;
  5796. id->name = v.identifier;
  5797. ConstantNode *init = alloc_node<ConstantNode>();
  5798. init->line = v.line;
  5799. Variant::CallError err;
  5800. init->value = Variant::construct(v.data_type.builtin_type, NULL, 0, err);
  5801. OperatorNode *op = alloc_node<OperatorNode>();
  5802. op->line = v.line;
  5803. op->op = OperatorNode::OP_INIT_ASSIGN;
  5804. op->arguments.push_back(id);
  5805. op->arguments.push_back(init);
  5806. p_class->initializer->statements.push_front(op);
  5807. v.initial_assignment = op;
  5808. #ifdef DEBUG_ENABLED
  5809. NewLineNode *nl = alloc_node<NewLineNode>();
  5810. nl->line = v.line - 1;
  5811. p_class->initializer->statements.push_front(nl);
  5812. #endif
  5813. }
  5814. // Check export hint
  5815. if (v.data_type.has_type && v._export.type != Variant::NIL) {
  5816. DataType export_type = _type_from_property(v._export);
  5817. if (!_is_type_compatible(v.data_type, export_type, true)) {
  5818. _set_error("Export hint type (" + export_type.to_string() + ") doesn't match the variable's type (" +
  5819. v.data_type.to_string() + ").",
  5820. v.line);
  5821. return;
  5822. }
  5823. }
  5824. // Setter and getter
  5825. if (v.setter == StringName() && v.getter == StringName()) continue;
  5826. bool found_getter = false;
  5827. bool found_setter = false;
  5828. for (int j = 0; j < p_class->functions.size(); j++) {
  5829. if (v.setter == p_class->functions[j]->name) {
  5830. found_setter = true;
  5831. FunctionNode *setter = p_class->functions[j];
  5832. if (setter->arguments.size() != 1) {
  5833. _set_error("Setter function needs to receive exactly 1 argument. See '" + setter->name +
  5834. "()' definition at line " + itos(setter->line) + ".",
  5835. v.line);
  5836. return;
  5837. }
  5838. if (!_is_type_compatible(v.data_type, setter->argument_types[0])) {
  5839. _set_error("Setter argument type (" + setter->argument_types[0].to_string() +
  5840. ") doesn't match the variable's type (" + v.data_type.to_string() + "). See '" +
  5841. setter->name + "()' definition at line " + itos(setter->line) + ".",
  5842. v.line);
  5843. return;
  5844. }
  5845. continue;
  5846. }
  5847. if (v.getter == p_class->functions[j]->name) {
  5848. found_getter = true;
  5849. FunctionNode *getter = p_class->functions[j];
  5850. if (getter->arguments.size() != 0) {
  5851. _set_error("Getter function can't receive arguments. See '" + getter->name +
  5852. "()' definition at line " + itos(getter->line) + ".",
  5853. v.line);
  5854. return;
  5855. }
  5856. if (!_is_type_compatible(v.data_type, getter->get_datatype())) {
  5857. _set_error("Getter return type (" + getter->get_datatype().to_string() +
  5858. ") doesn't match the variable's type (" + v.data_type.to_string() +
  5859. "). See '" + getter->name + "()' definition at line " + itos(getter->line) + ".",
  5860. v.line);
  5861. return;
  5862. }
  5863. }
  5864. if (found_getter && found_setter) break;
  5865. }
  5866. if ((found_getter || v.getter == StringName()) && (found_setter || v.setter == StringName())) continue;
  5867. // Check for static functions
  5868. for (int j = 0; j < p_class->static_functions.size(); j++) {
  5869. if (v.setter == p_class->static_functions[j]->name) {
  5870. FunctionNode *setter = p_class->static_functions[j];
  5871. _set_error("Setter can't be a static function. See '" + setter->name + "()' definition at line " + itos(setter->line) + ".", v.line);
  5872. return;
  5873. }
  5874. if (v.getter == p_class->static_functions[j]->name) {
  5875. FunctionNode *getter = p_class->static_functions[j];
  5876. _set_error("Getter can't be a static function. See '" + getter->name + "()' definition at line " + itos(getter->line) + ".", v.line);
  5877. return;
  5878. }
  5879. }
  5880. if (!found_setter && v.setter != StringName()) {
  5881. _set_error("Setter function is not defined.", v.line);
  5882. return;
  5883. }
  5884. if (!found_getter && v.getter != StringName()) {
  5885. _set_error("Getter function is not defined.", v.line);
  5886. return;
  5887. }
  5888. }
  5889. // Inner classes
  5890. for (int i = 0; i < p_class->subclasses.size(); i++) {
  5891. current_class = p_class->subclasses[i];
  5892. _check_class_level_types(current_class);
  5893. if (error_set) return;
  5894. current_class = p_class;
  5895. }
  5896. }
  5897. void GDScriptParser::_check_function_types(FunctionNode *p_function) {
  5898. p_function->return_type = _resolve_type(p_function->return_type, p_function->line);
  5899. // Arguments
  5900. int defaults_ofs = p_function->arguments.size() - p_function->default_values.size();
  5901. for (int i = 0; i < p_function->arguments.size(); i++) {
  5902. // Resolve types
  5903. p_function->argument_types.write[i] = _resolve_type(p_function->argument_types[i], p_function->line);
  5904. if (i >= defaults_ofs) {
  5905. if (p_function->default_values[i - defaults_ofs]->type != Node::TYPE_OPERATOR) {
  5906. _set_error("Parser bug: invalid argument default value.", p_function->line, p_function->column);
  5907. return;
  5908. }
  5909. OperatorNode *op = static_cast<OperatorNode *>(p_function->default_values[i - defaults_ofs]);
  5910. if (op->op != OperatorNode::OP_ASSIGN || op->arguments.size() != 2) {
  5911. _set_error("Parser bug: invalid argument default value operation.", p_function->line);
  5912. return;
  5913. }
  5914. DataType def_type = _reduce_node_type(op->arguments[1]);
  5915. if (!_is_type_compatible(p_function->argument_types[i], def_type, true)) {
  5916. String arg_name = p_function->arguments[i];
  5917. _set_error("Value type (" + def_type.to_string() + ") doesn't match the type of argument '" +
  5918. arg_name + "' (" + p_function->arguments[i] + ")",
  5919. p_function->line);
  5920. }
  5921. }
  5922. }
  5923. if (!(p_function->name == "_init")) {
  5924. // Signature for the initializer may vary
  5925. #ifdef DEBUG_ENABLED
  5926. DataType return_type;
  5927. List<DataType> arg_types;
  5928. int default_arg_count = 0;
  5929. bool _static = false;
  5930. bool vararg = false;
  5931. DataType base_type = current_class->base_type;
  5932. if (_get_function_signature(base_type, p_function->name, return_type, arg_types, default_arg_count, _static, vararg)) {
  5933. bool valid = _static == p_function->_static;
  5934. valid = valid && return_type == p_function->return_type;
  5935. int argsize_diff = p_function->arguments.size() - arg_types.size();
  5936. valid = valid && argsize_diff >= 0;
  5937. valid = valid && p_function->default_values.size() >= default_arg_count + argsize_diff;
  5938. int i = 0;
  5939. for (List<DataType>::Element *E = arg_types.front(); valid && E; E = E->next()) {
  5940. valid = valid && E->get() == p_function->argument_types[i++];
  5941. }
  5942. if (!valid) {
  5943. String parent_signature = return_type.has_type ? return_type.to_string() : "Variant";
  5944. if (parent_signature == "null") {
  5945. parent_signature = "void";
  5946. }
  5947. parent_signature += " " + p_function->name + "(";
  5948. if (arg_types.size()) {
  5949. int i = 0;
  5950. for (List<DataType>::Element *E = arg_types.front(); E; E = E->next()) {
  5951. if (E != arg_types.front()) {
  5952. parent_signature += ", ";
  5953. }
  5954. String arg = E->get().to_string();
  5955. if (arg == "null" || arg == "var") {
  5956. arg = "Variant";
  5957. }
  5958. parent_signature += arg;
  5959. if (i == arg_types.size() - default_arg_count) {
  5960. parent_signature += "=default";
  5961. }
  5962. i++;
  5963. }
  5964. }
  5965. parent_signature += ")";
  5966. _set_error("Function signature doesn't match the parent. Parent signature is: '" + parent_signature + "'.", p_function->line);
  5967. return;
  5968. }
  5969. }
  5970. #endif // DEBUG_ENABLED
  5971. } else {
  5972. if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) {
  5973. _set_error("Constructor cannot return a value.", p_function->line);
  5974. return;
  5975. }
  5976. }
  5977. if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) {
  5978. if (!p_function->body->has_return) {
  5979. _set_error("Non-void function must return a value in all possible paths.", p_function->line);
  5980. return;
  5981. }
  5982. }
  5983. if (p_function->has_yield) {
  5984. // yield() will make the function return a GDScriptFunctionState, so the type is ambiguous
  5985. p_function->return_type.has_type = false;
  5986. }
  5987. }
  5988. void GDScriptParser::_check_class_blocks_types(ClassNode *p_class) {
  5989. // Function blocks
  5990. for (int i = 0; i < p_class->static_functions.size(); i++) {
  5991. current_function = p_class->static_functions[i];
  5992. current_block = current_function->body;
  5993. _mark_line_as_safe(current_function->line);
  5994. _check_block_types(current_block);
  5995. current_block = NULL;
  5996. current_function = NULL;
  5997. if (error_set) return;
  5998. }
  5999. for (int i = 0; i < p_class->functions.size(); i++) {
  6000. current_function = p_class->functions[i];
  6001. current_block = current_function->body;
  6002. _mark_line_as_safe(current_function->line);
  6003. _check_block_types(current_block);
  6004. current_block = NULL;
  6005. current_function = NULL;
  6006. if (error_set) return;
  6007. }
  6008. // Inner classes
  6009. for (int i = 0; i < p_class->subclasses.size(); i++) {
  6010. current_class = p_class->subclasses[i];
  6011. _check_class_blocks_types(current_class);
  6012. if (error_set) return;
  6013. current_class = p_class;
  6014. }
  6015. }
  6016. void GDScriptParser::_check_block_types(BlockNode *p_block) {
  6017. Node *last_var_assign = NULL;
  6018. // Check each statement
  6019. for (List<Node *>::Element *E = p_block->statements.front(); E; E = E->next()) {
  6020. Node *statement = E->get();
  6021. switch (statement->type) {
  6022. case Node::TYPE_NEWLINE:
  6023. case Node::TYPE_BREAKPOINT:
  6024. case Node::TYPE_ASSERT: {
  6025. // Nothing to do
  6026. } break;
  6027. case Node::TYPE_LOCAL_VAR: {
  6028. LocalVarNode *lv = static_cast<LocalVarNode *>(statement);
  6029. lv->datatype = _resolve_type(lv->datatype, lv->line);
  6030. _mark_line_as_safe(lv->line);
  6031. if (lv->assign) {
  6032. DataType assign_type = _reduce_node_type(lv->assign);
  6033. if (!_is_type_compatible(lv->datatype, assign_type)) {
  6034. // Try supertype test
  6035. if (_is_type_compatible(assign_type, lv->datatype)) {
  6036. _mark_line_as_unsafe(lv->line);
  6037. } else {
  6038. // Try implict conversion
  6039. if (lv->datatype.kind != DataType::BUILTIN || !_is_type_compatible(lv->datatype, assign_type, true)) {
  6040. _set_error("Assigned value type (" + assign_type.to_string() + ") doesn't match the variable's type (" +
  6041. lv->datatype.to_string() + ").",
  6042. lv->line);
  6043. return;
  6044. }
  6045. // Replace assigment with implict conversion
  6046. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  6047. convert->line = lv->line;
  6048. convert->function = GDScriptFunctions::TYPE_CONVERT;
  6049. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  6050. tgt_type->line = lv->line;
  6051. tgt_type->value = (int)lv->datatype.builtin_type;
  6052. OperatorNode *convert_call = alloc_node<OperatorNode>();
  6053. convert_call->line = lv->line;
  6054. convert_call->op = OperatorNode::OP_CALL;
  6055. convert_call->arguments.push_back(convert);
  6056. convert_call->arguments.push_back(lv->assign);
  6057. convert_call->arguments.push_back(tgt_type);
  6058. lv->assign = convert_call;
  6059. lv->assign_op->arguments.write[1] = convert_call;
  6060. }
  6061. }
  6062. if (lv->datatype.infer_type) {
  6063. if (!assign_type.has_type) {
  6064. _set_error("Assigned value does not have a set type, variable type cannot be inferred.", lv->line);
  6065. return;
  6066. }
  6067. lv->datatype = assign_type;
  6068. lv->datatype.is_constant = false;
  6069. }
  6070. if (lv->datatype.has_type && !assign_type.has_type) {
  6071. _mark_line_as_unsafe(lv->line);
  6072. }
  6073. }
  6074. last_var_assign = lv->assign;
  6075. // TODO: Make a warning
  6076. /*
  6077. if (lv->assignments == 0) {
  6078. _set_error("Variable '" + String(lv->name) + "' is never assigned.", lv->line);
  6079. return;
  6080. }
  6081. */
  6082. } break;
  6083. case Node::TYPE_OPERATOR: {
  6084. OperatorNode *op = static_cast<OperatorNode *>(statement);
  6085. switch (op->op) {
  6086. case OperatorNode::OP_ASSIGN:
  6087. case OperatorNode::OP_ASSIGN_ADD:
  6088. case OperatorNode::OP_ASSIGN_SUB:
  6089. case OperatorNode::OP_ASSIGN_MUL:
  6090. case OperatorNode::OP_ASSIGN_DIV:
  6091. case OperatorNode::OP_ASSIGN_MOD:
  6092. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  6093. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  6094. case OperatorNode::OP_ASSIGN_BIT_AND:
  6095. case OperatorNode::OP_ASSIGN_BIT_OR:
  6096. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  6097. if (op->arguments.size() < 2) {
  6098. _set_error("Parser bug: operation without enough arguments.", op->line, op->column);
  6099. return;
  6100. }
  6101. if (op->arguments[1] == last_var_assign) {
  6102. // Assignment was already checked
  6103. break;
  6104. }
  6105. _mark_line_as_safe(op->line);
  6106. DataType lh_type = _reduce_node_type(op->arguments[0]);
  6107. if (error_set) {
  6108. return;
  6109. }
  6110. if (!lh_type.has_type) {
  6111. if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
  6112. _mark_line_as_unsafe(op->line);
  6113. }
  6114. } else if (lh_type.is_constant) {
  6115. _set_error("Cannot assign a new value to a constant.", op->line);
  6116. return;
  6117. }
  6118. DataType rh_type;
  6119. if (op->op != OperatorNode::OP_ASSIGN) {
  6120. // Validate operation
  6121. DataType arg_type = _reduce_node_type(op->arguments[1]);
  6122. if (!arg_type.has_type) {
  6123. _mark_line_as_unsafe(op->line);
  6124. break;
  6125. }
  6126. Variant::Operator oper = _get_variant_operation(op->op);
  6127. bool valid = false;
  6128. rh_type = _get_operation_type(oper, lh_type, arg_type, valid);
  6129. if (!valid) {
  6130. _set_error("Invalid operand types ('" + lh_type.to_string() + "' and '" + arg_type.to_string() +
  6131. "') to assignment operator '" + Variant::get_operator_name(oper) + "'.",
  6132. op->line);
  6133. return;
  6134. }
  6135. } else {
  6136. rh_type = _reduce_node_type(op->arguments[1]);
  6137. }
  6138. if (!_is_type_compatible(lh_type, rh_type)) {
  6139. // Try supertype test
  6140. if (_is_type_compatible(rh_type, lh_type)) {
  6141. _mark_line_as_unsafe(op->line);
  6142. } else {
  6143. // Try implict conversion
  6144. if (lh_type.kind != DataType::BUILTIN || !_is_type_compatible(lh_type, rh_type, true)) {
  6145. _set_error("Assigned value type (" + rh_type.to_string() + ") doesn't match the variable's type (" +
  6146. lh_type.to_string() + ").",
  6147. op->line);
  6148. return;
  6149. }
  6150. // Replace assigment with implict conversion
  6151. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  6152. convert->line = op->line;
  6153. convert->function = GDScriptFunctions::TYPE_CONVERT;
  6154. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  6155. tgt_type->line = op->line;
  6156. tgt_type->value = (int)lh_type.builtin_type;
  6157. OperatorNode *convert_call = alloc_node<OperatorNode>();
  6158. convert_call->line = op->line;
  6159. convert_call->op = OperatorNode::OP_CALL;
  6160. convert_call->arguments.push_back(convert);
  6161. convert_call->arguments.push_back(op->arguments[1]);
  6162. convert_call->arguments.push_back(tgt_type);
  6163. op->arguments.write[1] = convert_call;
  6164. }
  6165. }
  6166. if (!rh_type.has_type && (op->op != OperatorNode::OP_ASSIGN || lh_type.has_type || op->arguments[0]->type == Node::TYPE_OPERATOR)) {
  6167. _mark_line_as_unsafe(op->line);
  6168. }
  6169. } break;
  6170. case OperatorNode::OP_CALL:
  6171. case OperatorNode::OP_PARENT_CALL: {
  6172. _mark_line_as_safe(op->line);
  6173. _reduce_function_call_type(op);
  6174. if (error_set) return;
  6175. } break;
  6176. default: {
  6177. _mark_line_as_safe(op->line);
  6178. _reduce_node_type(op); // Test for safety anyway
  6179. // TODO: Make this a warning
  6180. /*_set_error("Standalone expression, nothing is done in this line.", statement->line);
  6181. return; */
  6182. }
  6183. }
  6184. } break;
  6185. case Node::TYPE_CONTROL_FLOW: {
  6186. ControlFlowNode *cf = static_cast<ControlFlowNode *>(statement);
  6187. _mark_line_as_safe(cf->line);
  6188. switch (cf->cf_type) {
  6189. case ControlFlowNode::CF_RETURN: {
  6190. DataType function_type = current_function->get_datatype();
  6191. DataType ret_type;
  6192. if (cf->arguments.size() > 0) {
  6193. ret_type = _reduce_node_type(cf->arguments[0]);
  6194. if (error_set) {
  6195. return;
  6196. }
  6197. }
  6198. if (!function_type.has_type) break;
  6199. if (function_type.kind == DataType::BUILTIN && function_type.builtin_type == Variant::NIL) {
  6200. // Return void, should not have arguments
  6201. if (cf->arguments.size() > 0) {
  6202. _set_error("Void function cannot return a value.", cf->line, cf->column);
  6203. return;
  6204. }
  6205. } else {
  6206. // Return something, cannot be empty
  6207. if (cf->arguments.size() == 0) {
  6208. _set_error("Non-void function must return a value.", cf->line, cf->column);
  6209. return;
  6210. }
  6211. if (!_is_type_compatible(function_type, ret_type)) {
  6212. _set_error("Returned value type (" + ret_type.to_string() + ") doesn't match the function return type (" +
  6213. function_type.to_string() + ").",
  6214. cf->line, cf->column);
  6215. return;
  6216. }
  6217. }
  6218. } break;
  6219. case ControlFlowNode::CF_MATCH: {
  6220. MatchNode *match_node = cf->match;
  6221. _transform_match_statment(match_node);
  6222. } break;
  6223. default: {
  6224. if (cf->body_else) {
  6225. _mark_line_as_safe(cf->body_else->line);
  6226. }
  6227. for (int i = 0; i < cf->arguments.size(); i++) {
  6228. _reduce_node_type(cf->arguments[i]);
  6229. }
  6230. } break;
  6231. }
  6232. } break;
  6233. case Node::TYPE_CONSTANT: {
  6234. ConstantNode *cn = static_cast<ConstantNode *>(statement);
  6235. // Strings are fine since they can be multiline comments
  6236. if (cn->value.get_type() == Variant::STRING) {
  6237. break;
  6238. }
  6239. } // falthrough
  6240. default: {
  6241. _mark_line_as_safe(statement->line);
  6242. _reduce_node_type(statement); // Test for safety anyway
  6243. // TODO: Make this a warning
  6244. /* _set_error("Standalone expression, nothing is done in this line.", statement->line);
  6245. return; */
  6246. }
  6247. }
  6248. }
  6249. // Parse sub blocks
  6250. for (int i = 0; i < p_block->sub_blocks.size(); i++) {
  6251. current_block = p_block->sub_blocks[i];
  6252. _check_block_types(current_block);
  6253. current_block = p_block;
  6254. if (error_set) return;
  6255. }
  6256. }
  6257. void GDScriptParser::_set_error(const String &p_error, int p_line, int p_column) {
  6258. if (error_set)
  6259. return; //allow no further errors
  6260. error = p_error;
  6261. error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
  6262. error_column = p_column < 0 ? tokenizer->get_token_column() : p_column;
  6263. error_set = true;
  6264. }
  6265. String GDScriptParser::get_error() const {
  6266. return error;
  6267. }
  6268. int GDScriptParser::get_error_line() const {
  6269. return error_line;
  6270. }
  6271. int GDScriptParser::get_error_column() const {
  6272. return error_column;
  6273. }
  6274. Error GDScriptParser::_parse(const String &p_base_path) {
  6275. base_path = p_base_path;
  6276. //assume class
  6277. ClassNode *main_class = alloc_node<ClassNode>();
  6278. main_class->initializer = alloc_node<BlockNode>();
  6279. main_class->initializer->parent_class = main_class;
  6280. main_class->ready = alloc_node<BlockNode>();
  6281. main_class->ready->parent_class = main_class;
  6282. current_class = main_class;
  6283. _parse_class(main_class);
  6284. if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) {
  6285. error_set = false;
  6286. _set_error("Parse Error: " + tokenizer->get_token_error());
  6287. }
  6288. if (error_set && !for_completion) {
  6289. return ERR_PARSE_ERROR;
  6290. }
  6291. _determine_inheritance(main_class);
  6292. if (error_set) {
  6293. return ERR_PARSE_ERROR;
  6294. }
  6295. current_class = main_class;
  6296. current_function = NULL;
  6297. current_block = NULL;
  6298. #ifdef DEBUG_ENABLED
  6299. if (for_completion) check_types = false;
  6300. #else
  6301. check_types = false;
  6302. #endif
  6303. // Resolve all class-level stuff before getting into function blocks
  6304. _check_class_level_types(main_class);
  6305. if (error_set) {
  6306. return ERR_PARSE_ERROR;
  6307. }
  6308. // Resolve the function blocks
  6309. _check_class_blocks_types(main_class);
  6310. if (error_set) {
  6311. return ERR_PARSE_ERROR;
  6312. }
  6313. return OK;
  6314. }
  6315. Error GDScriptParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
  6316. clear();
  6317. self_path = p_self_path;
  6318. GDScriptTokenizerBuffer *tb = memnew(GDScriptTokenizerBuffer);
  6319. tb->set_code_buffer(p_bytecode);
  6320. tokenizer = tb;
  6321. Error ret = _parse(p_base_path);
  6322. memdelete(tb);
  6323. tokenizer = NULL;
  6324. return ret;
  6325. }
  6326. 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) {
  6327. clear();
  6328. self_path = p_self_path;
  6329. GDScriptTokenizerText *tt = memnew(GDScriptTokenizerText);
  6330. tt->set_code(p_code);
  6331. validating = p_just_validate;
  6332. for_completion = p_for_completion;
  6333. #ifdef DEBUG_ENABLED
  6334. safe_lines = r_safe_lines;
  6335. #endif // DEBUG_ENABLED
  6336. tokenizer = tt;
  6337. Error ret = _parse(p_base_path);
  6338. memdelete(tt);
  6339. tokenizer = NULL;
  6340. return ret;
  6341. }
  6342. bool GDScriptParser::is_tool_script() const {
  6343. return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
  6344. }
  6345. const GDScriptParser::Node *GDScriptParser::get_parse_tree() const {
  6346. return head;
  6347. }
  6348. void GDScriptParser::clear() {
  6349. while (list) {
  6350. Node *l = list;
  6351. list = list->next;
  6352. memdelete(l);
  6353. }
  6354. head = NULL;
  6355. list = NULL;
  6356. completion_type = COMPLETION_NONE;
  6357. completion_node = NULL;
  6358. completion_class = NULL;
  6359. completion_function = NULL;
  6360. completion_block = NULL;
  6361. current_block = NULL;
  6362. current_class = NULL;
  6363. completion_found = false;
  6364. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  6365. current_function = NULL;
  6366. validating = false;
  6367. for_completion = false;
  6368. error_set = false;
  6369. tab_level.clear();
  6370. tab_level.push_back(0);
  6371. error_line = 0;
  6372. error_column = 0;
  6373. pending_newline = -1;
  6374. parenthesis = 0;
  6375. current_export.type = Variant::NIL;
  6376. check_types = true;
  6377. error = "";
  6378. #ifdef DEBUG_ENABLED
  6379. safe_lines = NULL;
  6380. #endif // DEBUG_ENABLED
  6381. }
  6382. GDScriptParser::CompletionType GDScriptParser::get_completion_type() {
  6383. return completion_type;
  6384. }
  6385. StringName GDScriptParser::get_completion_cursor() {
  6386. return completion_cursor;
  6387. }
  6388. int GDScriptParser::get_completion_line() {
  6389. return completion_line;
  6390. }
  6391. Variant::Type GDScriptParser::get_completion_built_in_constant() {
  6392. return completion_built_in_constant;
  6393. }
  6394. GDScriptParser::Node *GDScriptParser::get_completion_node() {
  6395. return completion_node;
  6396. }
  6397. GDScriptParser::BlockNode *GDScriptParser::get_completion_block() {
  6398. return completion_block;
  6399. }
  6400. GDScriptParser::ClassNode *GDScriptParser::get_completion_class() {
  6401. return completion_class;
  6402. }
  6403. GDScriptParser::FunctionNode *GDScriptParser::get_completion_function() {
  6404. return completion_function;
  6405. }
  6406. int GDScriptParser::get_completion_argument_index() {
  6407. return completion_argument;
  6408. }
  6409. int GDScriptParser::get_completion_identifier_is_function() {
  6410. return completion_ident_is_call;
  6411. }
  6412. GDScriptParser::GDScriptParser() {
  6413. head = NULL;
  6414. list = NULL;
  6415. tokenizer = NULL;
  6416. pending_newline = -1;
  6417. clear();
  6418. }
  6419. GDScriptParser::~GDScriptParser() {
  6420. clear();
  6421. }