gdscript_parser.cpp 279 KB

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