gdscript_parser.cpp 279 KB

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