gdscript_parser.cpp 279 KB

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