gdscript_parser.cpp 249 KB

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