gdscript_parser.cpp 252 KB

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