gdscript_parser.cpp 272 KB

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