gdscript_parser.cpp 266 KB

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