gdscript_parser.cpp 282 KB

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