gdscript_parser.cpp 278 KB

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