gdscript_parser.cpp 265 KB

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