gdscript_parser.cpp 251 KB

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