gdscript_parser.cpp 282 KB

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