2
0

gdscript_parser.cpp 249 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176
  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. //current_export.hint=PROPERTY_HINT_ALL_FLAGS;
  3148. tokenizer->advance();
  3149. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3150. break;
  3151. }
  3152. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3153. _set_error("Expected ')' or ',' in bit flags hint.");
  3154. return;
  3155. }
  3156. current_export.hint = PROPERTY_HINT_FLAGS;
  3157. tokenizer->advance();
  3158. bool first = true;
  3159. while (true) {
  3160. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3161. current_export = PropertyInfo();
  3162. _set_error("Expected a string constant in named bit flags hint.");
  3163. return;
  3164. }
  3165. String c = tokenizer->get_token_constant();
  3166. if (!first)
  3167. current_export.hint_string += ",";
  3168. else
  3169. first = false;
  3170. current_export.hint_string += c.xml_escape();
  3171. tokenizer->advance();
  3172. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3173. break;
  3174. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3175. current_export = PropertyInfo();
  3176. _set_error("Expected ')' or ',' in named bit flags hint.");
  3177. return;
  3178. }
  3179. tokenizer->advance();
  3180. }
  3181. break;
  3182. }
  3183. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3184. //enumeration
  3185. current_export.hint = PROPERTY_HINT_ENUM;
  3186. bool first = true;
  3187. while (true) {
  3188. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3189. current_export = PropertyInfo();
  3190. _set_error("Expected a string constant in enumeration hint.");
  3191. return;
  3192. }
  3193. String c = tokenizer->get_token_constant();
  3194. if (!first)
  3195. current_export.hint_string += ",";
  3196. else
  3197. first = false;
  3198. current_export.hint_string += c.xml_escape();
  3199. tokenizer->advance();
  3200. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3201. break;
  3202. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3203. current_export = PropertyInfo();
  3204. _set_error("Expected ')' or ',' in enumeration hint.");
  3205. return;
  3206. }
  3207. tokenizer->advance();
  3208. }
  3209. break;
  3210. }
  3211. }; //fallthrough to use the same
  3212. case Variant::REAL: {
  3213. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
  3214. current_export.hint = PROPERTY_HINT_EXP_EASING;
  3215. tokenizer->advance();
  3216. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3217. _set_error("Expected ')' in hint.");
  3218. return;
  3219. }
  3220. break;
  3221. }
  3222. // range
  3223. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EXP") {
  3224. current_export.hint = PROPERTY_HINT_EXP_RANGE;
  3225. tokenizer->advance();
  3226. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3227. break;
  3228. else if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3229. _set_error("Expected ')' or ',' in exponential range hint.");
  3230. return;
  3231. }
  3232. tokenizer->advance();
  3233. } else
  3234. current_export.hint = PROPERTY_HINT_RANGE;
  3235. float sign = 1.0;
  3236. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3237. sign = -1;
  3238. tokenizer->advance();
  3239. }
  3240. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3241. current_export = PropertyInfo();
  3242. _set_error("Expected a range in numeric hint.");
  3243. return;
  3244. }
  3245. current_export.hint_string = rtos(sign * double(tokenizer->get_token_constant()));
  3246. tokenizer->advance();
  3247. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3248. current_export.hint_string = "0," + current_export.hint_string;
  3249. break;
  3250. }
  3251. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3252. current_export = PropertyInfo();
  3253. _set_error("Expected ',' or ')' in numeric range hint.");
  3254. return;
  3255. }
  3256. tokenizer->advance();
  3257. sign = 1.0;
  3258. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3259. sign = -1;
  3260. tokenizer->advance();
  3261. }
  3262. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3263. current_export = PropertyInfo();
  3264. _set_error("Expected a number as upper bound in numeric range hint.");
  3265. return;
  3266. }
  3267. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  3268. tokenizer->advance();
  3269. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3270. break;
  3271. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3272. current_export = PropertyInfo();
  3273. _set_error("Expected ',' or ')' in numeric range hint.");
  3274. return;
  3275. }
  3276. tokenizer->advance();
  3277. sign = 1.0;
  3278. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3279. sign = -1;
  3280. tokenizer->advance();
  3281. }
  3282. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3283. current_export = PropertyInfo();
  3284. _set_error("Expected a number as step in numeric range hint.");
  3285. return;
  3286. }
  3287. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  3288. tokenizer->advance();
  3289. } break;
  3290. case Variant::STRING: {
  3291. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3292. //enumeration
  3293. current_export.hint = PROPERTY_HINT_ENUM;
  3294. bool first = true;
  3295. while (true) {
  3296. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3297. current_export = PropertyInfo();
  3298. _set_error("Expected a string constant in enumeration hint.");
  3299. return;
  3300. }
  3301. String c = tokenizer->get_token_constant();
  3302. if (!first)
  3303. current_export.hint_string += ",";
  3304. else
  3305. first = false;
  3306. current_export.hint_string += c.xml_escape();
  3307. tokenizer->advance();
  3308. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3309. break;
  3310. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3311. current_export = PropertyInfo();
  3312. _set_error("Expected ')' or ',' in enumeration hint.");
  3313. return;
  3314. }
  3315. tokenizer->advance();
  3316. }
  3317. break;
  3318. }
  3319. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "DIR") {
  3320. tokenizer->advance();
  3321. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3322. current_export.hint = PROPERTY_HINT_DIR;
  3323. else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3324. tokenizer->advance();
  3325. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) {
  3326. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  3327. return;
  3328. }
  3329. if (!p_class->tool) {
  3330. _set_error("Global filesystem hints may only be used in tool scripts.");
  3331. return;
  3332. }
  3333. current_export.hint = PROPERTY_HINT_GLOBAL_DIR;
  3334. tokenizer->advance();
  3335. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3336. _set_error("Expected ')' in hint.");
  3337. return;
  3338. }
  3339. } else {
  3340. _set_error("Expected ')' or ',' in hint.");
  3341. return;
  3342. }
  3343. break;
  3344. }
  3345. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FILE") {
  3346. current_export.hint = PROPERTY_HINT_FILE;
  3347. tokenizer->advance();
  3348. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3349. tokenizer->advance();
  3350. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "GLOBAL") {
  3351. if (!p_class->tool) {
  3352. _set_error("Global filesystem hints may only be used in tool scripts.");
  3353. return;
  3354. }
  3355. current_export.hint = PROPERTY_HINT_GLOBAL_FILE;
  3356. tokenizer->advance();
  3357. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3358. break;
  3359. else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA)
  3360. tokenizer->advance();
  3361. else {
  3362. _set_error("Expected ')' or ',' in hint.");
  3363. return;
  3364. }
  3365. }
  3366. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3367. if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE)
  3368. _set_error("Expected string constant with filter");
  3369. else
  3370. _set_error("Expected 'GLOBAL' or string constant with filter");
  3371. return;
  3372. }
  3373. current_export.hint_string = tokenizer->get_token_constant();
  3374. tokenizer->advance();
  3375. }
  3376. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3377. _set_error("Expected ')' in hint.");
  3378. return;
  3379. }
  3380. break;
  3381. }
  3382. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "MULTILINE") {
  3383. current_export.hint = PROPERTY_HINT_MULTILINE_TEXT;
  3384. tokenizer->advance();
  3385. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3386. _set_error("Expected ')' in hint.");
  3387. return;
  3388. }
  3389. break;
  3390. }
  3391. } break;
  3392. case Variant::COLOR: {
  3393. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER) {
  3394. current_export = PropertyInfo();
  3395. _set_error("Color type hint expects RGB or RGBA as hints");
  3396. return;
  3397. }
  3398. String identifier = tokenizer->get_token_identifier();
  3399. if (identifier == "RGB") {
  3400. current_export.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  3401. } else if (identifier == "RGBA") {
  3402. //none
  3403. } else {
  3404. current_export = PropertyInfo();
  3405. _set_error("Color type hint expects RGB or RGBA as hints");
  3406. return;
  3407. }
  3408. tokenizer->advance();
  3409. } break;
  3410. default: {
  3411. current_export = PropertyInfo();
  3412. _set_error("Type '" + Variant::get_type_name(type) + "' can't take hints.");
  3413. return;
  3414. } break;
  3415. }
  3416. }
  3417. } else {
  3418. parenthesis++;
  3419. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3420. if (!subexpr) {
  3421. if (_recover_from_completion()) {
  3422. break;
  3423. }
  3424. return;
  3425. }
  3426. parenthesis--;
  3427. if (subexpr->type != Node::TYPE_CONSTANT) {
  3428. current_export = PropertyInfo();
  3429. _set_error("Expected a constant expression.");
  3430. }
  3431. Variant constant = static_cast<ConstantNode *>(subexpr)->value;
  3432. if (constant.get_type() == Variant::OBJECT) {
  3433. GDScriptNativeClass *native_class = Object::cast_to<GDScriptNativeClass>(constant);
  3434. if (native_class && ClassDB::is_parent_class(native_class->get_name(), "Resource")) {
  3435. current_export.type = Variant::OBJECT;
  3436. current_export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3437. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3438. current_export.hint_string = native_class->get_name();
  3439. current_export.class_name = native_class->get_name();
  3440. } else {
  3441. current_export = PropertyInfo();
  3442. _set_error("Export hint not a resource type.");
  3443. }
  3444. } else if (constant.get_type() == Variant::DICTIONARY) {
  3445. // Enumeration
  3446. bool is_flags = false;
  3447. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3448. tokenizer->advance();
  3449. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  3450. is_flags = true;
  3451. tokenizer->advance();
  3452. } else {
  3453. current_export = PropertyInfo();
  3454. _set_error("Expected 'FLAGS' after comma.");
  3455. }
  3456. }
  3457. current_export.type = Variant::INT;
  3458. current_export.hint = is_flags ? PROPERTY_HINT_FLAGS : PROPERTY_HINT_ENUM;
  3459. Dictionary enum_values = constant;
  3460. List<Variant> keys;
  3461. enum_values.get_key_list(&keys);
  3462. bool first = true;
  3463. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  3464. if (enum_values[E->get()].get_type() == Variant::INT) {
  3465. if (!first)
  3466. current_export.hint_string += ",";
  3467. else
  3468. first = false;
  3469. current_export.hint_string += E->get().operator String().camelcase_to_underscore(true).capitalize().xml_escape();
  3470. if (!is_flags) {
  3471. current_export.hint_string += ":";
  3472. current_export.hint_string += enum_values[E->get()].operator String().xml_escape();
  3473. }
  3474. }
  3475. }
  3476. } else {
  3477. current_export = PropertyInfo();
  3478. _set_error("Expected type for export.");
  3479. return;
  3480. }
  3481. }
  3482. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3483. current_export = PropertyInfo();
  3484. _set_error("Expected ')' or ',' after export hint.");
  3485. return;
  3486. }
  3487. if (is_arrayed) {
  3488. hint_prefix += itos(current_export.type);
  3489. if (current_export.hint) {
  3490. hint_prefix += "/" + itos(current_export.hint);
  3491. }
  3492. current_export.hint_string = hint_prefix + ":" + current_export.hint_string;
  3493. current_export.hint = PROPERTY_HINT_TYPE_STRING;
  3494. current_export.type = Variant::ARRAY;
  3495. }
  3496. tokenizer->advance();
  3497. }
  3498. 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) {
  3499. current_export = PropertyInfo();
  3500. _set_error("Expected 'var', 'onready', 'remote', 'master', 'slave', 'sync', 'remotesync', 'mastersync', 'slavesync'.");
  3501. return;
  3502. }
  3503. continue;
  3504. } break;
  3505. case GDScriptTokenizer::TK_PR_ONREADY: {
  3506. //may be fallthrough from export, ignore if so
  3507. tokenizer->advance();
  3508. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3509. _set_error("Expected 'var'.");
  3510. return;
  3511. }
  3512. continue;
  3513. } break;
  3514. case GDScriptTokenizer::TK_PR_REMOTE: {
  3515. //may be fallthrough from export, ignore if so
  3516. tokenizer->advance();
  3517. if (current_export.type) {
  3518. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3519. _set_error("Expected 'var'.");
  3520. return;
  3521. }
  3522. } else {
  3523. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3524. _set_error("Expected 'var' or 'func'.");
  3525. return;
  3526. }
  3527. }
  3528. rpc_mode = MultiplayerAPI::RPC_MODE_REMOTE;
  3529. continue;
  3530. } break;
  3531. case GDScriptTokenizer::TK_PR_MASTER: {
  3532. //may be fallthrough from export, ignore if so
  3533. tokenizer->advance();
  3534. if (current_export.type) {
  3535. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3536. _set_error("Expected 'var'.");
  3537. return;
  3538. }
  3539. } else {
  3540. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3541. _set_error("Expected 'var' or 'func'.");
  3542. return;
  3543. }
  3544. }
  3545. rpc_mode = MultiplayerAPI::RPC_MODE_MASTER;
  3546. continue;
  3547. } break;
  3548. case GDScriptTokenizer::TK_PR_SLAVE: {
  3549. //may be fallthrough from export, ignore if so
  3550. tokenizer->advance();
  3551. if (current_export.type) {
  3552. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3553. _set_error("Expected 'var'.");
  3554. return;
  3555. }
  3556. } else {
  3557. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3558. _set_error("Expected 'var' or 'func'.");
  3559. return;
  3560. }
  3561. }
  3562. rpc_mode = MultiplayerAPI::RPC_MODE_SLAVE;
  3563. continue;
  3564. } break;
  3565. case GDScriptTokenizer::TK_PR_REMOTESYNC:
  3566. case GDScriptTokenizer::TK_PR_SYNC: {
  3567. //may be fallthrough from export, ignore if so
  3568. tokenizer->advance();
  3569. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3570. if (current_export.type)
  3571. _set_error("Expected 'var'.");
  3572. else
  3573. _set_error("Expected 'var' or 'func'.");
  3574. return;
  3575. }
  3576. rpc_mode = MultiplayerAPI::RPC_MODE_SYNC;
  3577. continue;
  3578. } break;
  3579. case GDScriptTokenizer::TK_PR_MASTERSYNC: {
  3580. //may be fallthrough from export, ignore if so
  3581. tokenizer->advance();
  3582. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3583. if (current_export.type)
  3584. _set_error("Expected 'var'.");
  3585. else
  3586. _set_error("Expected 'var' or 'func'.");
  3587. return;
  3588. }
  3589. rpc_mode = MultiplayerAPI::RPC_MODE_MASTERSYNC;
  3590. continue;
  3591. } break;
  3592. case GDScriptTokenizer::TK_PR_SLAVESYNC: {
  3593. //may be fallthrough from export, ignore if so
  3594. tokenizer->advance();
  3595. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3596. if (current_export.type)
  3597. _set_error("Expected 'var'.");
  3598. else
  3599. _set_error("Expected 'var' or 'func'.");
  3600. return;
  3601. }
  3602. rpc_mode = MultiplayerAPI::RPC_MODE_SLAVESYNC;
  3603. continue;
  3604. } break;
  3605. case GDScriptTokenizer::TK_PR_VAR: {
  3606. //variale declaration and (eventual) initialization
  3607. ClassNode::Member member;
  3608. bool autoexport = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_EXPORT;
  3609. if (current_export.type != Variant::NIL) {
  3610. member._export = current_export;
  3611. current_export = PropertyInfo();
  3612. }
  3613. bool onready = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_ONREADY;
  3614. tokenizer->advance();
  3615. if (!tokenizer->is_token_literal(0, true)) {
  3616. _set_error("Expected identifier for member variable name.");
  3617. return;
  3618. }
  3619. member.identifier = tokenizer->get_token_literal();
  3620. member.expression = NULL;
  3621. member._export.name = member.identifier;
  3622. member.line = tokenizer->get_token_line();
  3623. member.usages = 0;
  3624. member.rpc_mode = rpc_mode;
  3625. if (current_class->constant_expressions.has(member.identifier)) {
  3626. _set_error("A constant named '" + String(member.identifier) + "' alread exists in this class (at line: " +
  3627. itos(current_class->constant_expressions[member.identifier].expression->line) + ").");
  3628. return;
  3629. }
  3630. for (int i = 0; i < current_class->variables.size(); i++) {
  3631. if (current_class->variables[i].identifier == member.identifier) {
  3632. _set_error("Variable '" + String(member.identifier) + "' alread exists in this class (at line: " +
  3633. itos(current_class->variables[i].line) + ").");
  3634. return;
  3635. }
  3636. }
  3637. #ifdef DEBUG_ENABLED
  3638. for (int i = 0; i < current_class->functions.size(); i++) {
  3639. if (current_class->functions[i]->name == member.identifier) {
  3640. _add_warning(GDScriptWarning::VARIABLE_CONFLICTS_FUNCTION, member.line, member.identifier);
  3641. break;
  3642. }
  3643. }
  3644. for (int i = 0; i < current_class->static_functions.size(); i++) {
  3645. if (current_class->static_functions[i]->name == member.identifier) {
  3646. _add_warning(GDScriptWarning::VARIABLE_CONFLICTS_FUNCTION, member.line, member.identifier);
  3647. break;
  3648. }
  3649. }
  3650. #endif // DEBUG_ENABLED
  3651. tokenizer->advance();
  3652. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  3653. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  3654. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  3655. member.data_type = DataType();
  3656. #ifdef DEBUG_ENABLED
  3657. member.data_type.infer_type = true;
  3658. #endif
  3659. tokenizer->advance();
  3660. } else if (!_parse_type(member.data_type)) {
  3661. _set_error("Expected type for class variable.");
  3662. return;
  3663. }
  3664. }
  3665. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  3666. #ifdef DEBUG_ENABLED
  3667. int line = tokenizer->get_token_line();
  3668. #endif
  3669. tokenizer->advance();
  3670. Node *subexpr = _parse_and_reduce_expression(p_class, false, autoexport || member._export.type != Variant::NIL);
  3671. if (!subexpr) {
  3672. if (_recover_from_completion()) {
  3673. break;
  3674. }
  3675. return;
  3676. }
  3677. //discourage common error
  3678. if (!onready && subexpr->type == Node::TYPE_OPERATOR) {
  3679. OperatorNode *op = static_cast<OperatorNode *>(subexpr);
  3680. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_SELF && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  3681. IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]);
  3682. if (id->name == "get_node") {
  3683. _set_error("Use 'onready var " + String(member.identifier) + " = get_node(..)' instead");
  3684. return;
  3685. }
  3686. }
  3687. }
  3688. member.expression = subexpr;
  3689. if (autoexport && !member.data_type.has_type) {
  3690. if (subexpr->type != Node::TYPE_CONSTANT) {
  3691. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3692. return;
  3693. }
  3694. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3695. if (cn->value.get_type() == Variant::NIL) {
  3696. _set_error("Can't accept a null constant expression for inferring export type.");
  3697. return;
  3698. }
  3699. member._export.type = cn->value.get_type();
  3700. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3701. if (cn->value.get_type() == Variant::OBJECT) {
  3702. Object *obj = cn->value;
  3703. Resource *res = Object::cast_to<Resource>(obj);
  3704. if (res == NULL) {
  3705. _set_error("Exported constant not a type or resource.");
  3706. return;
  3707. }
  3708. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3709. member._export.hint_string = res->get_class();
  3710. }
  3711. }
  3712. #ifdef TOOLS_ENABLED
  3713. if (subexpr->type == Node::TYPE_CONSTANT && member._export.type != Variant::NIL) {
  3714. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3715. if (cn->value.get_type() != Variant::NIL) {
  3716. member.default_value = cn->value;
  3717. }
  3718. }
  3719. #endif
  3720. IdentifierNode *id = alloc_node<IdentifierNode>();
  3721. id->name = member.identifier;
  3722. OperatorNode *op = alloc_node<OperatorNode>();
  3723. op->op = OperatorNode::OP_INIT_ASSIGN;
  3724. op->arguments.push_back(id);
  3725. op->arguments.push_back(subexpr);
  3726. #ifdef DEBUG_ENABLED
  3727. NewLineNode *nl = alloc_node<NewLineNode>();
  3728. nl->line = line;
  3729. if (onready)
  3730. p_class->ready->statements.push_back(nl);
  3731. else
  3732. p_class->initializer->statements.push_back(nl);
  3733. #endif
  3734. if (onready)
  3735. p_class->ready->statements.push_back(op);
  3736. else
  3737. p_class->initializer->statements.push_back(op);
  3738. member.initial_assignment = op;
  3739. } else {
  3740. if (autoexport && !member.data_type.has_type) {
  3741. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3742. return;
  3743. }
  3744. }
  3745. if (autoexport && member.data_type.has_type) {
  3746. if (member.data_type.kind == DataType::BUILTIN) {
  3747. member._export.type = member.data_type.builtin_type;
  3748. } else if (member.data_type.kind == DataType::NATIVE) {
  3749. if (ClassDB::is_parent_class(member.data_type.native_type, "Resource")) {
  3750. member._export.type = Variant::OBJECT;
  3751. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3752. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3753. member._export.hint_string = member.data_type.native_type;
  3754. member._export.class_name = member.data_type.native_type;
  3755. } else {
  3756. _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
  3757. return;
  3758. }
  3759. } else {
  3760. _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
  3761. return;
  3762. }
  3763. }
  3764. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_SETGET) {
  3765. tokenizer->advance();
  3766. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3767. //just comma means using only getter
  3768. if (!tokenizer->is_token_literal()) {
  3769. _set_error("Expected identifier for setter function after 'setget'.");
  3770. }
  3771. member.setter = tokenizer->get_token_literal();
  3772. tokenizer->advance();
  3773. }
  3774. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3775. //there is a getter
  3776. tokenizer->advance();
  3777. if (!tokenizer->is_token_literal()) {
  3778. _set_error("Expected identifier for getter function after ','.");
  3779. }
  3780. member.getter = tokenizer->get_token_literal();
  3781. tokenizer->advance();
  3782. }
  3783. }
  3784. p_class->variables.push_back(member);
  3785. if (!_end_statement()) {
  3786. _set_error("Expected end of statement (continue)");
  3787. return;
  3788. }
  3789. } break;
  3790. case GDScriptTokenizer::TK_PR_CONST: {
  3791. // constant declaration and initialization
  3792. ClassNode::Constant constant;
  3793. tokenizer->advance();
  3794. if (!tokenizer->is_token_literal(0, true)) {
  3795. _set_error("Expected name (identifier) for constant.");
  3796. return;
  3797. }
  3798. StringName const_id = tokenizer->get_token_literal();
  3799. int line = tokenizer->get_token_line();
  3800. if (current_class->constant_expressions.has(const_id)) {
  3801. _set_error("Constant '" + String(const_id) + "' alread exists in this class (at line: " +
  3802. itos(current_class->constant_expressions[const_id].expression->line) + ").");
  3803. return;
  3804. }
  3805. for (int i = 0; i < current_class->variables.size(); i++) {
  3806. if (current_class->variables[i].identifier == const_id) {
  3807. _set_error("A variable named '" + String(const_id) + "' alread exists in this class (at line: " +
  3808. itos(current_class->variables[i].line) + ").");
  3809. return;
  3810. }
  3811. }
  3812. tokenizer->advance();
  3813. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  3814. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  3815. constant.type = DataType();
  3816. #ifdef DEBUG_ENABLED
  3817. constant.type.infer_type = true;
  3818. #endif
  3819. tokenizer->advance();
  3820. } else if (!_parse_type(constant.type)) {
  3821. _set_error("Expected type for class constant.");
  3822. return;
  3823. }
  3824. }
  3825. if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
  3826. _set_error("Constant expects assignment.");
  3827. return;
  3828. }
  3829. tokenizer->advance();
  3830. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3831. if (!subexpr) {
  3832. if (_recover_from_completion()) {
  3833. break;
  3834. }
  3835. return;
  3836. }
  3837. if (subexpr->type != Node::TYPE_CONSTANT) {
  3838. _set_error("Expected constant expression", line);
  3839. return;
  3840. }
  3841. subexpr->line = line;
  3842. constant.expression = subexpr;
  3843. p_class->constant_expressions.insert(const_id, constant);
  3844. if (!_end_statement()) {
  3845. _set_error("Expected end of statement (constant)", line);
  3846. return;
  3847. }
  3848. } break;
  3849. case GDScriptTokenizer::TK_PR_ENUM: {
  3850. //multiple constant declarations..
  3851. int last_assign = -1; // Incremented by 1 right before the assingment.
  3852. String enum_name;
  3853. Dictionary enum_dict;
  3854. tokenizer->advance();
  3855. if (tokenizer->is_token_literal(0, true)) {
  3856. enum_name = tokenizer->get_token_literal();
  3857. tokenizer->advance();
  3858. }
  3859. if (tokenizer->get_token() != GDScriptTokenizer::TK_CURLY_BRACKET_OPEN) {
  3860. _set_error("Expected '{' in enum declaration");
  3861. return;
  3862. }
  3863. tokenizer->advance();
  3864. while (true) {
  3865. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  3866. tokenizer->advance(); // Ignore newlines
  3867. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  3868. tokenizer->advance();
  3869. break; // End of enum
  3870. } else if (!tokenizer->is_token_literal(0, true)) {
  3871. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  3872. _set_error("Unexpected end of file.");
  3873. } else {
  3874. _set_error(String("Unexpected ") + GDScriptTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier");
  3875. }
  3876. return;
  3877. } else { // tokenizer->is_token_literal(0, true)
  3878. ClassNode::Constant constant;
  3879. StringName const_id = tokenizer->get_token_literal();
  3880. tokenizer->advance();
  3881. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  3882. tokenizer->advance();
  3883. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3884. if (!subexpr) {
  3885. if (_recover_from_completion()) {
  3886. break;
  3887. }
  3888. return;
  3889. }
  3890. if (subexpr->type != Node::TYPE_CONSTANT) {
  3891. _set_error("Expected constant expression");
  3892. return;
  3893. }
  3894. ConstantNode *subexpr_const = static_cast<ConstantNode *>(subexpr);
  3895. if (subexpr_const->value.get_type() != Variant::INT) {
  3896. _set_error("Expected an int value for enum");
  3897. return;
  3898. }
  3899. last_assign = subexpr_const->value;
  3900. constant.expression = subexpr_const;
  3901. } else {
  3902. last_assign = last_assign + 1;
  3903. ConstantNode *cn = alloc_node<ConstantNode>();
  3904. cn->value = last_assign;
  3905. cn->datatype = _type_from_variant(cn->value);
  3906. constant.expression = cn;
  3907. }
  3908. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3909. tokenizer->advance();
  3910. }
  3911. if (enum_name != "") {
  3912. const ConstantNode *cn = static_cast<const ConstantNode *>(constant.expression);
  3913. enum_dict[const_id] = cn->value;
  3914. }
  3915. constant.type.has_type = true;
  3916. constant.type.kind = DataType::BUILTIN;
  3917. constant.type.builtin_type = Variant::INT;
  3918. p_class->constant_expressions.insert(const_id, constant);
  3919. }
  3920. }
  3921. if (enum_name != "") {
  3922. ClassNode::Constant enum_constant;
  3923. ConstantNode *cn = alloc_node<ConstantNode>();
  3924. cn->value = enum_dict;
  3925. cn->datatype = _type_from_variant(cn->value);
  3926. enum_constant.expression = cn;
  3927. enum_constant.type = cn->datatype;
  3928. p_class->constant_expressions.insert(enum_name, enum_constant);
  3929. }
  3930. if (!_end_statement()) {
  3931. _set_error("Expected end of statement (enum)");
  3932. return;
  3933. }
  3934. } break;
  3935. case GDScriptTokenizer::TK_CONSTANT: {
  3936. if (tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3937. tokenizer->advance();
  3938. // Ignore
  3939. } else {
  3940. _set_error(String() + "Unexpected constant of type: " + Variant::get_type_name(tokenizer->get_token_constant().get_type()));
  3941. return;
  3942. }
  3943. } break;
  3944. default: {
  3945. _set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
  3946. return;
  3947. } break;
  3948. }
  3949. }
  3950. }
  3951. void GDScriptParser::_determine_inheritance(ClassNode *p_class) {
  3952. if (p_class->extends_used) {
  3953. //do inheritance
  3954. String path = p_class->extends_file;
  3955. Ref<GDScript> script;
  3956. StringName native;
  3957. ClassNode *base_class = NULL;
  3958. if (path != "") {
  3959. //path (and optionally subclasses)
  3960. if (path.is_rel_path()) {
  3961. String base = base_path;
  3962. if (base == "" || base.is_rel_path()) {
  3963. _set_error("Could not resolve relative path for parent class: " + path, p_class->line);
  3964. return;
  3965. }
  3966. path = base.plus_file(path).simplify_path();
  3967. }
  3968. script = ResourceLoader::load(path);
  3969. if (script.is_null()) {
  3970. _set_error("Could not load base class: " + path, p_class->line);
  3971. return;
  3972. }
  3973. if (!script->is_valid()) {
  3974. _set_error("Script not fully loaded (cyclic preload?): " + path, p_class->line);
  3975. return;
  3976. }
  3977. if (p_class->extends_class.size()) {
  3978. for (int i = 0; i < p_class->extends_class.size(); i++) {
  3979. String sub = p_class->extends_class[i];
  3980. if (script->get_subclasses().has(sub)) {
  3981. Ref<Script> subclass = script->get_subclasses()[sub]; //avoid reference from disappearing
  3982. script = subclass;
  3983. } else {
  3984. _set_error("Could not find subclass: " + sub, p_class->line);
  3985. return;
  3986. }
  3987. }
  3988. }
  3989. } else {
  3990. if (p_class->extends_class.size() == 0) {
  3991. _set_error("Parser bug: undecidable inheritance.", p_class->line);
  3992. ERR_FAIL();
  3993. }
  3994. //look around for the subclasses
  3995. int extend_iter = 1;
  3996. String base = p_class->extends_class[0];
  3997. ClassNode *p = p_class->owner;
  3998. Ref<GDScript> base_script;
  3999. if (ScriptServer::is_global_class(base)) {
  4000. base_script = ResourceLoader::load(ScriptServer::get_global_class_path(base));
  4001. if (!base_script.is_valid()) {
  4002. _set_error("Class '" + base + "' could not be fully loaded (script error or cyclic inheritance).", p_class->line);
  4003. return;
  4004. }
  4005. p = NULL;
  4006. }
  4007. while (p) {
  4008. bool found = false;
  4009. for (int i = 0; i < p->subclasses.size(); i++) {
  4010. if (p->subclasses[i]->name == base) {
  4011. ClassNode *test = p->subclasses[i];
  4012. while (test) {
  4013. if (test == p_class) {
  4014. _set_error("Cyclic inheritance.", test->line);
  4015. return;
  4016. }
  4017. if (test->base_type.kind == DataType::CLASS) {
  4018. test = test->base_type.class_type;
  4019. } else {
  4020. break;
  4021. }
  4022. }
  4023. found = true;
  4024. if (extend_iter < p_class->extends_class.size()) {
  4025. // Keep looking at current classes if possible
  4026. base = p_class->extends_class[extend_iter++];
  4027. p = p->subclasses[i];
  4028. } else {
  4029. base_class = p->subclasses[i];
  4030. }
  4031. break;
  4032. }
  4033. }
  4034. if (base_class) break;
  4035. if (found) continue;
  4036. if (p->constant_expressions.has(base)) {
  4037. if (!p->constant_expressions[base].expression->type == Node::TYPE_CONSTANT) {
  4038. _set_error("Could not resolve constant '" + base + "'.", p_class->line);
  4039. return;
  4040. }
  4041. const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[base].expression);
  4042. base_script = cn->value;
  4043. if (base_script.is_null()) {
  4044. _set_error("Constant is not a class: " + base, p_class->line);
  4045. return;
  4046. }
  4047. break;
  4048. }
  4049. p = p->owner;
  4050. }
  4051. if (base_script.is_valid()) {
  4052. String ident = base;
  4053. for (int i = extend_iter; i < p_class->extends_class.size(); i++) {
  4054. String subclass = p_class->extends_class[i];
  4055. ident += ("." + subclass);
  4056. if (base_script->get_subclasses().has(subclass)) {
  4057. base_script = base_script->get_subclasses()[subclass];
  4058. } else if (base_script->get_constants().has(subclass)) {
  4059. Ref<GDScript> new_base_class = base_script->get_constants()[subclass];
  4060. if (new_base_class.is_null()) {
  4061. _set_error("Constant is not a class: " + ident, p_class->line);
  4062. return;
  4063. }
  4064. base_script = new_base_class;
  4065. } else {
  4066. _set_error("Could not find subclass: " + ident, p_class->line);
  4067. return;
  4068. }
  4069. }
  4070. script = base_script;
  4071. } else if (!base_class) {
  4072. if (p_class->extends_class.size() > 1) {
  4073. _set_error("Invalid inheritance (unknown class + subclasses)", p_class->line);
  4074. return;
  4075. }
  4076. //if not found, try engine classes
  4077. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  4078. _set_error("Unknown class: '" + base + "'", p_class->line);
  4079. return;
  4080. }
  4081. native = base;
  4082. }
  4083. }
  4084. if (base_class) {
  4085. p_class->base_type.has_type = true;
  4086. p_class->base_type.kind = DataType::CLASS;
  4087. p_class->base_type.class_type = base_class;
  4088. } else if (script.is_valid()) {
  4089. p_class->base_type.has_type = true;
  4090. p_class->base_type.kind = DataType::GDSCRIPT;
  4091. p_class->base_type.script_type = script;
  4092. p_class->base_type.native_type = script->get_instance_base_type();
  4093. } else if (native != StringName()) {
  4094. p_class->base_type.has_type = true;
  4095. p_class->base_type.kind = DataType::NATIVE;
  4096. p_class->base_type.native_type = native;
  4097. } else {
  4098. _set_error("Could not determine inheritance", p_class->line);
  4099. return;
  4100. }
  4101. } else {
  4102. // without extends, implicitly extend Reference
  4103. p_class->base_type.has_type = true;
  4104. p_class->base_type.kind = DataType::NATIVE;
  4105. p_class->base_type.native_type = "Reference";
  4106. }
  4107. // Recursively determine subclasses
  4108. for (int i = 0; i < p_class->subclasses.size(); i++) {
  4109. _determine_inheritance(p_class->subclasses[i]);
  4110. }
  4111. }
  4112. String GDScriptParser::DataType::to_string() const {
  4113. if (!has_type) return "var";
  4114. switch (kind) {
  4115. case BUILTIN: {
  4116. if (builtin_type == Variant::NIL) return "null";
  4117. return Variant::get_type_name(builtin_type);
  4118. } break;
  4119. case NATIVE: {
  4120. if (is_meta_type) {
  4121. return "GDScriptNativeClass";
  4122. }
  4123. return native_type.operator String();
  4124. } break;
  4125. case GDSCRIPT: {
  4126. Ref<GDScript> gds = script_type;
  4127. const String &gds_class = gds->get_script_class_name();
  4128. if (!gds_class.empty()) {
  4129. return gds_class;
  4130. }
  4131. } // fallthrough
  4132. case SCRIPT: {
  4133. if (is_meta_type) {
  4134. return script_type->get_class_name().operator String();
  4135. }
  4136. String name = script_type->get_name();
  4137. if (name != String()) {
  4138. return name;
  4139. }
  4140. name = script_type->get_path().get_file();
  4141. if (name != String()) {
  4142. return name;
  4143. }
  4144. return native_type.operator String();
  4145. } break;
  4146. case CLASS: {
  4147. ERR_FAIL_COND_V(!class_type, String());
  4148. if (is_meta_type) {
  4149. return "GDScript";
  4150. }
  4151. if (class_type->name == StringName()) {
  4152. return "self";
  4153. }
  4154. return class_type->name.operator String();
  4155. } break;
  4156. }
  4157. return "Unresolved";
  4158. }
  4159. bool GDScriptParser::_parse_type(DataType &r_type, bool p_can_be_void) {
  4160. tokenizer->advance();
  4161. r_type.has_type = true;
  4162. bool finished = false;
  4163. bool can_index = false;
  4164. String full_name;
  4165. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  4166. completion_cursor = StringName();
  4167. completion_type = COMPLETION_TYPE_HINT;
  4168. completion_class = current_class;
  4169. completion_function = current_function;
  4170. completion_line = tokenizer->get_token_line();
  4171. completion_argument = 0;
  4172. completion_block = current_block;
  4173. completion_found = true;
  4174. completion_ident_is_call = p_can_be_void;
  4175. tokenizer->advance();
  4176. }
  4177. switch (tokenizer->get_token()) {
  4178. case GDScriptTokenizer::TK_PR_VOID: {
  4179. if (!p_can_be_void) {
  4180. return false;
  4181. }
  4182. r_type.kind = DataType::BUILTIN;
  4183. r_type.builtin_type = Variant::NIL;
  4184. } break;
  4185. case GDScriptTokenizer::TK_BUILT_IN_TYPE: {
  4186. r_type.builtin_type = tokenizer->get_token_type();
  4187. if (tokenizer->get_token_type() == Variant::OBJECT) {
  4188. r_type.kind = DataType::NATIVE;
  4189. r_type.native_type = "Object";
  4190. } else {
  4191. r_type.kind = DataType::BUILTIN;
  4192. }
  4193. } break;
  4194. case GDScriptTokenizer::TK_IDENTIFIER: {
  4195. r_type.native_type = tokenizer->get_token_identifier();
  4196. if (ClassDB::class_exists(r_type.native_type) || ClassDB::class_exists("_" + r_type.native_type.operator String())) {
  4197. r_type.kind = DataType::NATIVE;
  4198. } else {
  4199. r_type.kind = DataType::UNRESOLVED;
  4200. can_index = true;
  4201. full_name = r_type.native_type;
  4202. }
  4203. } break;
  4204. default: {
  4205. return false;
  4206. }
  4207. }
  4208. tokenizer->advance();
  4209. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  4210. completion_cursor = r_type.native_type;
  4211. completion_type = COMPLETION_TYPE_HINT;
  4212. completion_class = current_class;
  4213. completion_function = current_function;
  4214. completion_line = tokenizer->get_token_line();
  4215. completion_argument = 0;
  4216. completion_block = current_block;
  4217. completion_found = true;
  4218. completion_ident_is_call = p_can_be_void;
  4219. tokenizer->advance();
  4220. }
  4221. if (can_index) {
  4222. while (!finished) {
  4223. switch (tokenizer->get_token()) {
  4224. case GDScriptTokenizer::TK_PERIOD: {
  4225. if (!can_index) {
  4226. _set_error("Unexpected '.'.");
  4227. return false;
  4228. }
  4229. can_index = false;
  4230. tokenizer->advance();
  4231. } break;
  4232. case GDScriptTokenizer::TK_IDENTIFIER: {
  4233. if (can_index) {
  4234. _set_error("Unexpected identifier.");
  4235. return false;
  4236. }
  4237. StringName id;
  4238. bool has_completion = _get_completable_identifier(COMPLETION_TYPE_HINT_INDEX, id);
  4239. if (id == StringName()) {
  4240. id = "@temp";
  4241. }
  4242. full_name += "." + id.operator String();
  4243. can_index = true;
  4244. if (has_completion) {
  4245. completion_cursor = full_name;
  4246. }
  4247. } break;
  4248. default: {
  4249. finished = true;
  4250. } break;
  4251. }
  4252. }
  4253. if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PERIOD) {
  4254. _set_error("Expected subclass identifier.");
  4255. return false;
  4256. }
  4257. r_type.native_type = full_name;
  4258. }
  4259. return true;
  4260. }
  4261. GDScriptParser::DataType GDScriptParser::_resolve_type(const DataType &p_source, int p_line) {
  4262. if (!p_source.has_type) return p_source;
  4263. if (p_source.kind != DataType::UNRESOLVED) return p_source;
  4264. Vector<String> full_name = p_source.native_type.operator String().split(".", false);
  4265. int name_part = 0;
  4266. DataType result;
  4267. result.has_type = true;
  4268. while (name_part < full_name.size()) {
  4269. bool found = false;
  4270. StringName id = full_name[name_part];
  4271. DataType base_type = result;
  4272. ClassNode *p = NULL;
  4273. if (name_part == 0) {
  4274. if (ScriptServer::is_global_class(id)) {
  4275. String script_path = ScriptServer::get_global_class_path(id);
  4276. if (script_path == self_path) {
  4277. result.kind = DataType::CLASS;
  4278. result.class_type = current_class;
  4279. } else {
  4280. Ref<Script> script = ResourceLoader::load(script_path);
  4281. Ref<GDScript> gds = script;
  4282. if (gds.is_valid()) {
  4283. if (!gds->is_valid()) {
  4284. _set_error("Class '" + id + "' could not be fully loaded (script error or cyclic inheritance).", p_line);
  4285. return DataType();
  4286. }
  4287. result.kind = DataType::GDSCRIPT;
  4288. result.script_type = gds;
  4289. } else if (script.is_valid()) {
  4290. result.kind = DataType::SCRIPT;
  4291. result.script_type = script;
  4292. } else {
  4293. _set_error("Class '" + id + "' was found in global scope but its script could not be loaded.", p_line);
  4294. return DataType();
  4295. }
  4296. }
  4297. name_part++;
  4298. continue;
  4299. } else {
  4300. p = current_class;
  4301. }
  4302. } else if (base_type.kind == DataType::CLASS) {
  4303. p = base_type.class_type;
  4304. }
  4305. while (p) {
  4306. if (p->constant_expressions.has(id)) {
  4307. if (p->constant_expressions[id].expression->type != Node::TYPE_CONSTANT) {
  4308. _set_error("Parser bug: unresolved constant.", p_line);
  4309. ERR_FAIL_V(result);
  4310. }
  4311. const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[id].expression);
  4312. Ref<GDScript> gds = cn->value;
  4313. if (gds.is_valid()) {
  4314. result.kind = DataType::GDSCRIPT;
  4315. result.script_type = gds;
  4316. found = true;
  4317. } else {
  4318. Ref<Script> scr = cn->value;
  4319. if (scr.is_valid()) {
  4320. result.kind = DataType::SCRIPT;
  4321. result.script_type = scr;
  4322. found = true;
  4323. }
  4324. }
  4325. break;
  4326. }
  4327. // Inner classes
  4328. ClassNode *outer_class = p;
  4329. while (outer_class) {
  4330. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  4331. if (outer_class->subclasses[i] == p) {
  4332. continue;
  4333. }
  4334. if (outer_class->subclasses[i]->name == id) {
  4335. found = true;
  4336. result.kind = DataType::CLASS;
  4337. result.class_type = outer_class->subclasses[i];
  4338. break;
  4339. }
  4340. }
  4341. if (found) {
  4342. break;
  4343. }
  4344. outer_class = outer_class->owner;
  4345. }
  4346. if (!found && p->base_type.kind == DataType::CLASS) {
  4347. p = p->base_type.class_type;
  4348. } else {
  4349. base_type = p->base_type;
  4350. break;
  4351. }
  4352. }
  4353. // Still look for class constants in parent script
  4354. if (!found && (base_type.kind == DataType::GDSCRIPT || base_type.kind == DataType::SCRIPT)) {
  4355. Ref<Script> scr = base_type.script_type;
  4356. ERR_FAIL_COND_V(scr.is_null(), result);
  4357. Map<StringName, Variant> constants;
  4358. scr->get_constants(&constants);
  4359. if (constants.has(id)) {
  4360. Ref<GDScript> gds = constants[id];
  4361. if (gds.is_valid()) {
  4362. result.kind = DataType::GDSCRIPT;
  4363. result.script_type = gds;
  4364. found = true;
  4365. } else {
  4366. Ref<Script> scr = constants[id];
  4367. if (scr.is_valid()) {
  4368. result.kind = DataType::SCRIPT;
  4369. result.script_type = scr;
  4370. found = true;
  4371. }
  4372. }
  4373. }
  4374. }
  4375. if (!found && !for_completion) {
  4376. String base;
  4377. if (name_part == 0) {
  4378. base = "self";
  4379. } else {
  4380. base = result.to_string();
  4381. }
  4382. _set_error("Identifier '" + String(id) + "' is not a valid type (not a script or class), or could not be found on base '" +
  4383. base + "'.",
  4384. p_line);
  4385. return DataType();
  4386. }
  4387. name_part++;
  4388. }
  4389. return result;
  4390. }
  4391. GDScriptParser::DataType GDScriptParser::_type_from_variant(const Variant &p_value) const {
  4392. DataType result;
  4393. result.has_type = true;
  4394. result.is_constant = true;
  4395. result.kind = DataType::BUILTIN;
  4396. result.builtin_type = p_value.get_type();
  4397. if (result.builtin_type == Variant::OBJECT) {
  4398. Object *obj = p_value.operator Object *();
  4399. if (!obj) {
  4400. return DataType();
  4401. }
  4402. result.native_type = obj->get_class_name();
  4403. Ref<Script> scr = p_value;
  4404. if (scr.is_valid()) {
  4405. result.is_meta_type = true;
  4406. } else {
  4407. result.is_meta_type = false;
  4408. scr = obj->get_script();
  4409. }
  4410. if (scr.is_valid()) {
  4411. result.script_type = scr;
  4412. Ref<GDScript> gds = scr;
  4413. if (gds.is_valid()) {
  4414. result.kind = DataType::GDSCRIPT;
  4415. } else {
  4416. result.kind = DataType::SCRIPT;
  4417. }
  4418. result.native_type = scr->get_instance_base_type();
  4419. } else {
  4420. result.kind = DataType::NATIVE;
  4421. }
  4422. }
  4423. return result;
  4424. }
  4425. GDScriptParser::DataType GDScriptParser::_type_from_property(const PropertyInfo &p_property, bool p_nil_is_variant) const {
  4426. DataType ret;
  4427. if (p_property.type == Variant::NIL && (p_nil_is_variant || (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  4428. // Variant
  4429. return ret;
  4430. }
  4431. ret.has_type = true;
  4432. ret.builtin_type = p_property.type;
  4433. if (p_property.type == Variant::OBJECT) {
  4434. ret.kind = DataType::NATIVE;
  4435. ret.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  4436. } else {
  4437. ret.kind = DataType::BUILTIN;
  4438. }
  4439. return ret;
  4440. }
  4441. GDScriptParser::DataType GDScriptParser::_type_from_gdtype(const GDScriptDataType &p_gdtype) const {
  4442. DataType result;
  4443. if (!p_gdtype.has_type) {
  4444. return result;
  4445. }
  4446. result.has_type = true;
  4447. result.builtin_type = p_gdtype.builtin_type;
  4448. result.native_type = p_gdtype.native_type;
  4449. result.script_type = p_gdtype.script_type;
  4450. switch (p_gdtype.kind) {
  4451. case GDScriptDataType::BUILTIN: {
  4452. result.kind = DataType::BUILTIN;
  4453. } break;
  4454. case GDScriptDataType::NATIVE: {
  4455. result.kind = DataType::NATIVE;
  4456. } break;
  4457. case GDScriptDataType::GDSCRIPT: {
  4458. result.kind = DataType::GDSCRIPT;
  4459. } break;
  4460. case GDScriptDataType::SCRIPT: {
  4461. result.kind = DataType::SCRIPT;
  4462. } break;
  4463. }
  4464. return result;
  4465. }
  4466. GDScriptParser::DataType GDScriptParser::_get_operation_type(const Variant::Operator p_op, const DataType &p_a, const DataType &p_b, bool &r_valid) const {
  4467. if (!p_a.has_type || !p_b.has_type) {
  4468. r_valid = true;
  4469. return DataType();
  4470. }
  4471. Variant::Type a_type = p_a.kind == DataType::BUILTIN ? p_a.builtin_type : Variant::OBJECT;
  4472. Variant::Type b_type = p_b.kind == DataType::BUILTIN ? p_b.builtin_type : Variant::OBJECT;
  4473. Variant a;
  4474. REF a_ref;
  4475. if (a_type == Variant::OBJECT) {
  4476. a_ref.instance();
  4477. a = a_ref;
  4478. } else {
  4479. Variant::CallError err;
  4480. a = Variant::construct(a_type, NULL, 0, err);
  4481. if (err.error != Variant::CallError::CALL_OK) {
  4482. r_valid = false;
  4483. return DataType();
  4484. }
  4485. }
  4486. Variant b;
  4487. REF b_ref;
  4488. if (b_type == Variant::OBJECT) {
  4489. b_ref.instance();
  4490. b = b_ref;
  4491. } else {
  4492. Variant::CallError err;
  4493. b = Variant::construct(b_type, NULL, 0, err);
  4494. if (err.error != Variant::CallError::CALL_OK) {
  4495. r_valid = false;
  4496. return DataType();
  4497. }
  4498. }
  4499. // Avoid division by zero
  4500. if (a_type == Variant::INT || a_type == Variant::REAL) {
  4501. Variant::evaluate(Variant::OP_ADD, a, 1, a, r_valid);
  4502. }
  4503. if (b_type == Variant::INT || b_type == Variant::REAL) {
  4504. Variant::evaluate(Variant::OP_ADD, b, 1, b, r_valid);
  4505. }
  4506. if (a_type == Variant::STRING && b_type != Variant::ARRAY) {
  4507. a = "%s"; // Work around for formatting operator (%)
  4508. }
  4509. Variant ret;
  4510. Variant::evaluate(p_op, a, b, ret, r_valid);
  4511. if (r_valid) {
  4512. return _type_from_variant(ret);
  4513. }
  4514. return DataType();
  4515. }
  4516. Variant::Operator GDScriptParser::_get_variant_operation(const OperatorNode::Operator &p_op) const {
  4517. switch (p_op) {
  4518. case OperatorNode::OP_NEG: {
  4519. return Variant::OP_NEGATE;
  4520. } break;
  4521. case OperatorNode::OP_POS: {
  4522. return Variant::OP_POSITIVE;
  4523. } break;
  4524. case OperatorNode::OP_NOT: {
  4525. return Variant::OP_NOT;
  4526. } break;
  4527. case OperatorNode::OP_BIT_INVERT: {
  4528. return Variant::OP_BIT_NEGATE;
  4529. } break;
  4530. case OperatorNode::OP_IN: {
  4531. return Variant::OP_IN;
  4532. } break;
  4533. case OperatorNode::OP_EQUAL: {
  4534. return Variant::OP_EQUAL;
  4535. } break;
  4536. case OperatorNode::OP_NOT_EQUAL: {
  4537. return Variant::OP_NOT_EQUAL;
  4538. } break;
  4539. case OperatorNode::OP_LESS: {
  4540. return Variant::OP_LESS;
  4541. } break;
  4542. case OperatorNode::OP_LESS_EQUAL: {
  4543. return Variant::OP_LESS_EQUAL;
  4544. } break;
  4545. case OperatorNode::OP_GREATER: {
  4546. return Variant::OP_GREATER;
  4547. } break;
  4548. case OperatorNode::OP_GREATER_EQUAL: {
  4549. return Variant::OP_GREATER_EQUAL;
  4550. } break;
  4551. case OperatorNode::OP_AND: {
  4552. return Variant::OP_AND;
  4553. } break;
  4554. case OperatorNode::OP_OR: {
  4555. return Variant::OP_OR;
  4556. } break;
  4557. case OperatorNode::OP_ASSIGN_ADD:
  4558. case OperatorNode::OP_ADD: {
  4559. return Variant::OP_ADD;
  4560. } break;
  4561. case OperatorNode::OP_ASSIGN_SUB:
  4562. case OperatorNode::OP_SUB: {
  4563. return Variant::OP_SUBTRACT;
  4564. } break;
  4565. case OperatorNode::OP_ASSIGN_MUL:
  4566. case OperatorNode::OP_MUL: {
  4567. return Variant::OP_MULTIPLY;
  4568. } break;
  4569. case OperatorNode::OP_ASSIGN_DIV:
  4570. case OperatorNode::OP_DIV: {
  4571. return Variant::OP_DIVIDE;
  4572. } break;
  4573. case OperatorNode::OP_ASSIGN_MOD:
  4574. case OperatorNode::OP_MOD: {
  4575. return Variant::OP_MODULE;
  4576. } break;
  4577. case OperatorNode::OP_ASSIGN_BIT_AND:
  4578. case OperatorNode::OP_BIT_AND: {
  4579. return Variant::OP_BIT_AND;
  4580. } break;
  4581. case OperatorNode::OP_ASSIGN_BIT_OR:
  4582. case OperatorNode::OP_BIT_OR: {
  4583. return Variant::OP_BIT_OR;
  4584. } break;
  4585. case OperatorNode::OP_ASSIGN_BIT_XOR:
  4586. case OperatorNode::OP_BIT_XOR: {
  4587. return Variant::OP_BIT_XOR;
  4588. } break;
  4589. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  4590. case OperatorNode::OP_SHIFT_LEFT: {
  4591. return Variant::OP_SHIFT_LEFT;
  4592. }
  4593. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  4594. case OperatorNode::OP_SHIFT_RIGHT: {
  4595. return Variant::OP_SHIFT_RIGHT;
  4596. }
  4597. default: {
  4598. return Variant::OP_MAX;
  4599. } break;
  4600. }
  4601. }
  4602. bool GDScriptParser::_is_type_compatible(const DataType &p_container, const DataType &p_expression, bool p_allow_implicit_conversion) const {
  4603. // Ignore for completion
  4604. if (!check_types || for_completion) {
  4605. return true;
  4606. }
  4607. // Can't test if not all have type
  4608. if (!p_container.has_type || !p_expression.has_type) {
  4609. return true;
  4610. }
  4611. // Should never get here unresolved
  4612. ERR_FAIL_COND_V(p_container.kind == DataType::UNRESOLVED, false);
  4613. ERR_FAIL_COND_V(p_expression.kind == DataType::UNRESOLVED, false);
  4614. if (p_container.kind == DataType::BUILTIN && p_expression.kind == DataType::BUILTIN) {
  4615. bool valid = p_container.builtin_type == p_expression.builtin_type;
  4616. if (p_allow_implicit_conversion) {
  4617. valid = valid || (p_container.builtin_type == Variant::INT && p_expression.builtin_type == Variant::REAL);
  4618. valid = valid || (p_container.builtin_type == Variant::REAL && p_expression.builtin_type == Variant::INT);
  4619. valid = valid || (p_container.builtin_type == Variant::STRING && p_expression.builtin_type == Variant::NODE_PATH);
  4620. valid = valid || (p_container.builtin_type == Variant::NODE_PATH && p_expression.builtin_type == Variant::STRING);
  4621. valid = valid || (p_container.builtin_type == Variant::BOOL && p_expression.builtin_type == Variant::REAL);
  4622. valid = valid || (p_container.builtin_type == Variant::BOOL && p_expression.builtin_type == Variant::INT);
  4623. valid = valid || (p_container.builtin_type == Variant::INT && p_expression.builtin_type == Variant::BOOL);
  4624. valid = valid || (p_container.builtin_type == Variant::REAL && p_expression.builtin_type == Variant::BOOL);
  4625. }
  4626. return valid;
  4627. }
  4628. if (p_container.kind == DataType::BUILTIN || (p_expression.kind == DataType::BUILTIN && p_expression.builtin_type != Variant::NIL)) {
  4629. // Can't mix built-ins with objects
  4630. return false;
  4631. }
  4632. // From now on everything is objects, check polymorphism
  4633. // The container must be the same class or a superclass of the expression
  4634. if (p_expression.kind == DataType::BUILTIN && p_expression.builtin_type == Variant::NIL) {
  4635. // Null can be assigned to object types
  4636. return true;
  4637. }
  4638. StringName expr_native;
  4639. Ref<Script> expr_script;
  4640. ClassNode *expr_class = NULL;
  4641. switch (p_expression.kind) {
  4642. case DataType::NATIVE: {
  4643. if (p_container.kind != DataType::NATIVE) {
  4644. // Non-native type can't be a superclass of a native type
  4645. return false;
  4646. }
  4647. if (p_expression.is_meta_type) {
  4648. expr_native = GDScriptNativeClass::get_class_static();
  4649. } else {
  4650. expr_native = p_expression.native_type;
  4651. }
  4652. } break;
  4653. case DataType::SCRIPT:
  4654. case DataType::GDSCRIPT: {
  4655. if (p_container.kind == DataType::CLASS) {
  4656. // This cannot be resolved without cyclic dependencies, so just bail out
  4657. return false;
  4658. }
  4659. if (p_expression.is_meta_type) {
  4660. expr_native = p_expression.script_type->get_class_name();
  4661. } else {
  4662. expr_script = p_expression.script_type;
  4663. expr_native = expr_script->get_instance_base_type();
  4664. }
  4665. } break;
  4666. case DataType::CLASS: {
  4667. if (p_expression.is_meta_type) {
  4668. expr_native = GDScript::get_class_static();
  4669. } else {
  4670. expr_class = p_expression.class_type;
  4671. ClassNode *base = expr_class;
  4672. while (base->base_type.kind == DataType::CLASS) {
  4673. base = base->base_type.class_type;
  4674. }
  4675. expr_native = base->base_type.native_type;
  4676. expr_script = base->base_type.script_type;
  4677. }
  4678. }
  4679. }
  4680. switch (p_container.kind) {
  4681. case DataType::NATIVE: {
  4682. if (p_container.is_meta_type) {
  4683. return ClassDB::is_parent_class(expr_native, GDScriptNativeClass::get_class_static());
  4684. } else {
  4685. return ClassDB::is_parent_class(expr_native, p_container.native_type);
  4686. }
  4687. } break;
  4688. case DataType::SCRIPT:
  4689. case DataType::GDSCRIPT: {
  4690. if (p_container.is_meta_type) {
  4691. return ClassDB::is_parent_class(expr_native, GDScript::get_class_static());
  4692. }
  4693. if (expr_class == head && p_container.script_type->get_path() == self_path) {
  4694. // Special case: container is self script and expression is self
  4695. return true;
  4696. }
  4697. while (expr_script.is_valid()) {
  4698. if (expr_script == p_container.script_type) {
  4699. return true;
  4700. }
  4701. expr_script = expr_script->get_base_script();
  4702. }
  4703. return false;
  4704. } break;
  4705. case DataType::CLASS: {
  4706. if (p_container.is_meta_type) {
  4707. return ClassDB::is_parent_class(expr_native, GDScript::get_class_static());
  4708. }
  4709. if (p_container.class_type == head && expr_script.is_valid() && expr_script->get_path() == self_path) {
  4710. // Special case: container is self and expression is self script
  4711. return true;
  4712. }
  4713. while (expr_class) {
  4714. if (expr_class == p_container.class_type) {
  4715. return true;
  4716. }
  4717. expr_class = expr_class->base_type.class_type;
  4718. }
  4719. return false;
  4720. }
  4721. }
  4722. return false;
  4723. }
  4724. GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
  4725. if (p_node->get_datatype().has_type) {
  4726. return p_node->get_datatype();
  4727. }
  4728. DataType node_type;
  4729. switch (p_node->type) {
  4730. case Node::TYPE_CONSTANT: {
  4731. node_type = _type_from_variant(static_cast<ConstantNode *>(p_node)->value);
  4732. } break;
  4733. case Node::TYPE_ARRAY: {
  4734. node_type.has_type = true;
  4735. node_type.kind = DataType::BUILTIN;
  4736. node_type.builtin_type = Variant::ARRAY;
  4737. #ifdef DEBUG_ENABLED
  4738. // Check stuff inside the array
  4739. ArrayNode *an = static_cast<ArrayNode *>(p_node);
  4740. for (int i = 0; i < an->elements.size(); i++) {
  4741. _reduce_node_type(an->elements[i]);
  4742. }
  4743. #endif // DEBUG_ENABLED
  4744. } break;
  4745. case Node::TYPE_DICTIONARY: {
  4746. node_type.has_type = true;
  4747. node_type.kind = DataType::BUILTIN;
  4748. node_type.builtin_type = Variant::DICTIONARY;
  4749. #ifdef DEBUG_ENABLED
  4750. // Check stuff inside the dictionarty
  4751. DictionaryNode *dn = static_cast<DictionaryNode *>(p_node);
  4752. for (int i = 0; i < dn->elements.size(); i++) {
  4753. _reduce_node_type(dn->elements[i].key);
  4754. _reduce_node_type(dn->elements[i].value);
  4755. }
  4756. #endif // DEBUG_ENABLED
  4757. } break;
  4758. case Node::TYPE_SELF: {
  4759. node_type.has_type = true;
  4760. node_type.kind = DataType::CLASS;
  4761. node_type.class_type = current_class;
  4762. } break;
  4763. case Node::TYPE_IDENTIFIER: {
  4764. IdentifierNode *id = static_cast<IdentifierNode *>(p_node);
  4765. if (id->declared_block) {
  4766. node_type = id->declared_block->variables[id->name]->get_datatype();
  4767. id->declared_block->variables[id->name]->usages += 1;
  4768. } else if (id->name == "#match_value") {
  4769. // It's a special id just for the match statetement, ignore
  4770. break;
  4771. } else if (current_function && current_function->arguments.find(id->name) >= 0) {
  4772. int idx = current_function->arguments.find(id->name);
  4773. node_type = current_function->argument_types[idx];
  4774. } else {
  4775. node_type = _reduce_identifier_type(NULL, id->name, id->line);
  4776. }
  4777. } break;
  4778. case Node::TYPE_CAST: {
  4779. CastNode *cn = static_cast<CastNode *>(p_node);
  4780. DataType source_type = _reduce_node_type(cn->source_node);
  4781. cn->cast_type = _resolve_type(cn->cast_type, cn->line);
  4782. if (source_type.has_type) {
  4783. bool valid = false;
  4784. if (check_types) {
  4785. if (cn->cast_type.kind == DataType::BUILTIN && source_type.kind == DataType::BUILTIN) {
  4786. valid = Variant::can_convert(source_type.builtin_type, cn->cast_type.builtin_type);
  4787. }
  4788. if (cn->cast_type.kind != DataType::BUILTIN && source_type.kind != DataType::BUILTIN) {
  4789. valid = _is_type_compatible(cn->cast_type, source_type) || _is_type_compatible(source_type, cn->cast_type);
  4790. }
  4791. if (!valid) {
  4792. _set_error("Invalid cast. Cannot convert from '" + source_type.to_string() +
  4793. "' to '" + cn->cast_type.to_string() + "'.",
  4794. cn->line);
  4795. return DataType();
  4796. }
  4797. }
  4798. } else {
  4799. #ifdef DEBUG_ENABLED
  4800. _add_warning(GDScriptWarning::UNSAFE_CAST, cn->line, cn->cast_type.to_string());
  4801. #endif // DEBUG_ENABLED
  4802. _mark_line_as_unsafe(cn->line);
  4803. }
  4804. node_type = cn->cast_type;
  4805. } break;
  4806. case Node::TYPE_OPERATOR: {
  4807. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  4808. switch (op->op) {
  4809. case OperatorNode::OP_CALL:
  4810. case OperatorNode::OP_PARENT_CALL: {
  4811. node_type = _reduce_function_call_type(op);
  4812. } break;
  4813. case OperatorNode::OP_YIELD: {
  4814. if (op->arguments.size() == 2) {
  4815. DataType base_type = _reduce_node_type(op->arguments[0]);
  4816. DataType signal_type = _reduce_node_type(op->arguments[1]);
  4817. // TODO: Check if signal exists when it's a constant
  4818. if (base_type.has_type && base_type.kind == DataType::BUILTIN && base_type.builtin_type != Variant::NIL && base_type.builtin_type != Variant::OBJECT) {
  4819. _set_error("First argument of 'yield()' must be an object.", op->line);
  4820. return DataType();
  4821. }
  4822. if (signal_type.has_type && (signal_type.kind != DataType::BUILTIN || signal_type.builtin_type != Variant::STRING)) {
  4823. _set_error("Second argument of 'yield()' must be a string.", op->line);
  4824. return DataType();
  4825. }
  4826. }
  4827. // yield can return anything
  4828. node_type.has_type = false;
  4829. } break;
  4830. case OperatorNode::OP_IS: {
  4831. if (op->arguments.size() != 2) {
  4832. _set_error("Parser bug: binary operation without 2 arguments.", op->line);
  4833. ERR_FAIL_V(DataType());
  4834. }
  4835. DataType value_type = _reduce_node_type(op->arguments[0]);
  4836. DataType type_type = _reduce_node_type(op->arguments[1]);
  4837. if (check_types && type_type.has_type) {
  4838. if (!type_type.is_meta_type && (type_type.kind != DataType::NATIVE || !ClassDB::is_parent_class(type_type.native_type, "Script"))) {
  4839. _set_error("Invalid 'is' test: right operand is not a type (not a native type nor a script).", op->line);
  4840. return DataType();
  4841. }
  4842. type_type.is_meta_type = false; // Test the actual type
  4843. if (!_is_type_compatible(type_type, value_type) && !_is_type_compatible(value_type, type_type)) {
  4844. // TODO: Make this a warning?
  4845. _set_error("A value of type '" + value_type.to_string() + "' will never be an instance of '" + type_type.to_string() + "'.", op->line);
  4846. return DataType();
  4847. }
  4848. }
  4849. node_type.has_type = true;
  4850. node_type.is_constant = true;
  4851. node_type.is_meta_type = false;
  4852. node_type.kind = DataType::BUILTIN;
  4853. node_type.builtin_type = Variant::BOOL;
  4854. } break;
  4855. // Unary operators
  4856. case OperatorNode::OP_NEG:
  4857. case OperatorNode::OP_POS:
  4858. case OperatorNode::OP_NOT:
  4859. case OperatorNode::OP_BIT_INVERT: {
  4860. DataType argument_type = _reduce_node_type(op->arguments[0]);
  4861. if (!argument_type.has_type) {
  4862. break;
  4863. }
  4864. Variant::Operator var_op = _get_variant_operation(op->op);
  4865. bool valid = false;
  4866. node_type = _get_operation_type(var_op, argument_type, argument_type, valid);
  4867. if (check_types && !valid) {
  4868. _set_error("Invalid operand type ('" + argument_type.to_string() +
  4869. "') to unary operator '" + Variant::get_operator_name(var_op) + "'.",
  4870. op->line, op->column);
  4871. return DataType();
  4872. }
  4873. } break;
  4874. // Binary operators
  4875. case OperatorNode::OP_IN:
  4876. case OperatorNode::OP_EQUAL:
  4877. case OperatorNode::OP_NOT_EQUAL:
  4878. case OperatorNode::OP_LESS:
  4879. case OperatorNode::OP_LESS_EQUAL:
  4880. case OperatorNode::OP_GREATER:
  4881. case OperatorNode::OP_GREATER_EQUAL:
  4882. case OperatorNode::OP_AND:
  4883. case OperatorNode::OP_OR:
  4884. case OperatorNode::OP_ADD:
  4885. case OperatorNode::OP_SUB:
  4886. case OperatorNode::OP_MUL:
  4887. case OperatorNode::OP_DIV:
  4888. case OperatorNode::OP_MOD:
  4889. case OperatorNode::OP_SHIFT_LEFT:
  4890. case OperatorNode::OP_SHIFT_RIGHT:
  4891. case OperatorNode::OP_BIT_AND:
  4892. case OperatorNode::OP_BIT_OR:
  4893. case OperatorNode::OP_BIT_XOR: {
  4894. if (op->arguments.size() != 2) {
  4895. _set_error("Parser bug: binary operation without 2 arguments.", op->line);
  4896. ERR_FAIL_V(DataType());
  4897. }
  4898. DataType argument_a_type = _reduce_node_type(op->arguments[0]);
  4899. DataType argument_b_type = _reduce_node_type(op->arguments[1]);
  4900. if (!argument_a_type.has_type || !argument_b_type.has_type) {
  4901. _mark_line_as_unsafe(op->line);
  4902. break;
  4903. }
  4904. Variant::Operator var_op = _get_variant_operation(op->op);
  4905. bool valid = false;
  4906. node_type = _get_operation_type(var_op, argument_a_type, argument_b_type, valid);
  4907. if (check_types && !valid) {
  4908. _set_error("Invalid operand types ('" + argument_a_type.to_string() + "' and '" +
  4909. argument_b_type.to_string() + "') to operator '" + Variant::get_operator_name(var_op) + "'.",
  4910. op->line, op->column);
  4911. return DataType();
  4912. }
  4913. #ifdef DEBUG_ENABLED
  4914. if (var_op == Variant::OP_DIVIDE && argument_a_type.has_type && argument_a_type.kind == DataType::BUILTIN && argument_a_type.builtin_type == Variant::INT &&
  4915. argument_b_type.has_type && argument_b_type.kind == DataType::BUILTIN && argument_b_type.builtin_type == Variant::INT) {
  4916. _add_warning(GDScriptWarning::INTEGER_DIVISION, op->line);
  4917. }
  4918. #endif // DEBUG_ENABLED
  4919. } break;
  4920. // Ternary operators
  4921. case OperatorNode::OP_TERNARY_IF: {
  4922. if (op->arguments.size() != 3) {
  4923. _set_error("Parser bug: ternary operation without 3 arguments");
  4924. ERR_FAIL_V(DataType());
  4925. }
  4926. DataType true_type = _reduce_node_type(op->arguments[1]);
  4927. DataType false_type = _reduce_node_type(op->arguments[2]);
  4928. // If types are equal, then the expression is of the same type
  4929. // If they are compatible, return the broader type
  4930. if (true_type == false_type || _is_type_compatible(true_type, false_type)) {
  4931. node_type = true_type;
  4932. } else if (_is_type_compatible(false_type, true_type)) {
  4933. node_type = false_type;
  4934. } else {
  4935. #ifdef DEBUG_ENABLED
  4936. _add_warning(GDScriptWarning::INCOMPATIBLE_TERNARY, op->line);
  4937. #endif // DEBUG_ENABLED
  4938. }
  4939. } break;
  4940. // Assignment should never happen within an expression
  4941. case OperatorNode::OP_ASSIGN:
  4942. case OperatorNode::OP_ASSIGN_ADD:
  4943. case OperatorNode::OP_ASSIGN_SUB:
  4944. case OperatorNode::OP_ASSIGN_MUL:
  4945. case OperatorNode::OP_ASSIGN_DIV:
  4946. case OperatorNode::OP_ASSIGN_MOD:
  4947. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  4948. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  4949. case OperatorNode::OP_ASSIGN_BIT_AND:
  4950. case OperatorNode::OP_ASSIGN_BIT_OR:
  4951. case OperatorNode::OP_ASSIGN_BIT_XOR:
  4952. case OperatorNode::OP_INIT_ASSIGN: {
  4953. _set_error("Assignment inside expression is not allowed (parser bug?).", op->line);
  4954. return DataType();
  4955. } break;
  4956. case OperatorNode::OP_INDEX_NAMED: {
  4957. if (op->arguments.size() != 2) {
  4958. _set_error("Parser bug: named index with invalid arguments.", op->line);
  4959. ERR_FAIL_V(DataType());
  4960. }
  4961. if (op->arguments[1]->type != Node::TYPE_IDENTIFIER) {
  4962. _set_error("Parser bug: named index without identifier argument.", op->line);
  4963. ERR_FAIL_V(DataType());
  4964. }
  4965. DataType base_type = _reduce_node_type(op->arguments[0]);
  4966. IdentifierNode *member_id = static_cast<IdentifierNode *>(op->arguments[1]);
  4967. if (base_type.has_type) {
  4968. if (check_types && base_type.kind == DataType::BUILTIN) {
  4969. // Variant type, just test if it's possible
  4970. DataType result;
  4971. switch (base_type.builtin_type) {
  4972. case Variant::NIL:
  4973. case Variant::DICTIONARY: {
  4974. result.has_type = false;
  4975. } break;
  4976. default: {
  4977. Variant::CallError err;
  4978. Variant temp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  4979. bool valid = false;
  4980. Variant res = temp.get(member_id->name.operator String(), &valid);
  4981. if (valid) {
  4982. result = _type_from_variant(res);
  4983. } else if (check_types) {
  4984. _set_error("Can't get index '" + String(member_id->name.operator String()) + "' on base '" +
  4985. base_type.to_string() + "'.",
  4986. op->line);
  4987. return DataType();
  4988. }
  4989. } break;
  4990. }
  4991. result.is_constant = false;
  4992. node_type = result;
  4993. } else {
  4994. node_type = _reduce_identifier_type(&base_type, member_id->name, op->line);
  4995. #ifdef DEBUG_ENABLED
  4996. if (!node_type.has_type) {
  4997. _add_warning(GDScriptWarning::UNSAFE_PROPERTY_ACCESS, op->line, member_id->name.operator String(), base_type.to_string());
  4998. }
  4999. #endif // DEBUG_ENABLED
  5000. }
  5001. } else {
  5002. _mark_line_as_unsafe(op->line);
  5003. }
  5004. if (error_set) {
  5005. return DataType();
  5006. }
  5007. } break;
  5008. case OperatorNode::OP_INDEX: {
  5009. if (op->arguments[1]->type == Node::TYPE_CONSTANT) {
  5010. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[1]);
  5011. if (cn->value.get_type() == Variant::STRING) {
  5012. // Treat this as named indexing
  5013. IdentifierNode *id = alloc_node<IdentifierNode>();
  5014. id->name = cn->value.operator StringName();
  5015. op->op = OperatorNode::OP_INDEX_NAMED;
  5016. op->arguments.write[1] = id;
  5017. return _reduce_node_type(op);
  5018. }
  5019. }
  5020. DataType base_type = _reduce_node_type(op->arguments[0]);
  5021. DataType index_type = _reduce_node_type(op->arguments[1]);
  5022. if (!base_type.has_type) {
  5023. _mark_line_as_unsafe(op->line);
  5024. break;
  5025. }
  5026. if (check_types && index_type.has_type) {
  5027. if (base_type.kind == DataType::BUILTIN) {
  5028. // Check if indexing is valid
  5029. bool error = index_type.kind != DataType::BUILTIN;
  5030. if (!error) {
  5031. switch (base_type.builtin_type) {
  5032. // Expect int or real as index
  5033. case Variant::POOL_BYTE_ARRAY:
  5034. case Variant::POOL_COLOR_ARRAY:
  5035. case Variant::POOL_INT_ARRAY:
  5036. case Variant::POOL_REAL_ARRAY:
  5037. case Variant::POOL_STRING_ARRAY:
  5038. case Variant::POOL_VECTOR2_ARRAY:
  5039. case Variant::POOL_VECTOR3_ARRAY:
  5040. case Variant::ARRAY:
  5041. case Variant::STRING: {
  5042. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::REAL;
  5043. } break;
  5044. // Expect String only
  5045. case Variant::RECT2:
  5046. case Variant::PLANE:
  5047. case Variant::QUAT:
  5048. case Variant::AABB:
  5049. case Variant::OBJECT: {
  5050. error = index_type.builtin_type != Variant::STRING;
  5051. } break;
  5052. // Expect String or number
  5053. case Variant::VECTOR2:
  5054. case Variant::VECTOR3:
  5055. case Variant::TRANSFORM2D:
  5056. case Variant::BASIS:
  5057. case Variant::TRANSFORM: {
  5058. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::REAL &&
  5059. index_type.builtin_type != Variant::STRING;
  5060. } break;
  5061. // Expect String or int
  5062. case Variant::COLOR: {
  5063. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING;
  5064. } break;
  5065. }
  5066. }
  5067. if (error) {
  5068. _set_error("Invalid index type (" + index_type.to_string() + ") for base '" + base_type.to_string() + "'.",
  5069. op->line);
  5070. return DataType();
  5071. }
  5072. if (op->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT) {
  5073. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[1]);
  5074. // Index is a constant, just try it if possible
  5075. switch (base_type.builtin_type) {
  5076. // Arrays/string have variable indexing, can't test directly
  5077. case Variant::STRING:
  5078. case Variant::ARRAY:
  5079. case Variant::DICTIONARY:
  5080. case Variant::POOL_BYTE_ARRAY:
  5081. case Variant::POOL_COLOR_ARRAY:
  5082. case Variant::POOL_INT_ARRAY:
  5083. case Variant::POOL_REAL_ARRAY:
  5084. case Variant::POOL_STRING_ARRAY:
  5085. case Variant::POOL_VECTOR2_ARRAY:
  5086. case Variant::POOL_VECTOR3_ARRAY: {
  5087. break;
  5088. }
  5089. default: {
  5090. Variant::CallError err;
  5091. Variant temp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  5092. bool valid = false;
  5093. Variant res = temp.get(cn->value, &valid);
  5094. if (valid) {
  5095. node_type = _type_from_variant(res);
  5096. node_type.is_constant = false;
  5097. } else if (check_types) {
  5098. _set_error("Can't get index '" + String(cn->value) + "' on base '" +
  5099. base_type.to_string() + "'.",
  5100. op->line);
  5101. return DataType();
  5102. }
  5103. } break;
  5104. }
  5105. } else {
  5106. _mark_line_as_unsafe(op->line);
  5107. }
  5108. } else if (!for_completion && (index_type.kind != DataType::BUILTIN || index_type.builtin_type != Variant::STRING)) {
  5109. _set_error("Only strings can be used as index in the base type '" + base_type.to_string() + "'.", op->line);
  5110. return DataType();
  5111. }
  5112. }
  5113. if (check_types && !node_type.has_type) {
  5114. // Can infer indexing type for some variant types
  5115. DataType result;
  5116. result.has_type = true;
  5117. result.kind = DataType::BUILTIN;
  5118. switch (base_type.builtin_type) {
  5119. // Can't index at all
  5120. case Variant::NIL:
  5121. case Variant::BOOL:
  5122. case Variant::INT:
  5123. case Variant::REAL:
  5124. case Variant::NODE_PATH:
  5125. case Variant::_RID: {
  5126. _set_error("Can't index on a value of type '" + base_type.to_string() + "'.", op->line);
  5127. return DataType();
  5128. } break;
  5129. // Return int
  5130. case Variant::POOL_BYTE_ARRAY:
  5131. case Variant::POOL_INT_ARRAY: {
  5132. result.builtin_type = Variant::INT;
  5133. } break;
  5134. // Return real
  5135. case Variant::POOL_REAL_ARRAY:
  5136. case Variant::VECTOR2:
  5137. case Variant::VECTOR3:
  5138. case Variant::QUAT: {
  5139. result.builtin_type = Variant::REAL;
  5140. } break;
  5141. // Return color
  5142. case Variant::POOL_COLOR_ARRAY: {
  5143. result.builtin_type = Variant::COLOR;
  5144. } break;
  5145. // Return string
  5146. case Variant::POOL_STRING_ARRAY:
  5147. case Variant::STRING: {
  5148. result.builtin_type = Variant::STRING;
  5149. } break;
  5150. // Return Vector2
  5151. case Variant::POOL_VECTOR2_ARRAY:
  5152. case Variant::TRANSFORM2D:
  5153. case Variant::RECT2: {
  5154. result.builtin_type = Variant::VECTOR2;
  5155. } break;
  5156. // Return Vector3
  5157. case Variant::POOL_VECTOR3_ARRAY:
  5158. case Variant::AABB:
  5159. case Variant::BASIS: {
  5160. result.builtin_type = Variant::VECTOR3;
  5161. } break;
  5162. // Depends on the index
  5163. case Variant::TRANSFORM:
  5164. case Variant::PLANE:
  5165. case Variant::COLOR:
  5166. default: {
  5167. result.has_type = false;
  5168. } break;
  5169. }
  5170. node_type = result;
  5171. }
  5172. } break;
  5173. default: {
  5174. _set_error("Parser bug: unhandled operation.", op->line);
  5175. ERR_FAIL_V(DataType());
  5176. }
  5177. }
  5178. } break;
  5179. }
  5180. p_node->set_datatype(_resolve_type(node_type, p_node->line));
  5181. return node_type;
  5182. }
  5183. 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 {
  5184. r_static = false;
  5185. r_default_arg_count = 0;
  5186. DataType original_type = p_base_type;
  5187. ClassNode *base = NULL;
  5188. FunctionNode *callee = NULL;
  5189. if (p_base_type.kind == DataType::CLASS) {
  5190. base = p_base_type.class_type;
  5191. }
  5192. // Look up the current file (parse tree)
  5193. while (!callee && base) {
  5194. for (int i = 0; i < base->static_functions.size(); i++) {
  5195. FunctionNode *func = base->static_functions[i];
  5196. if (p_function == func->name) {
  5197. r_static = true;
  5198. callee = func;
  5199. break;
  5200. }
  5201. }
  5202. if (!callee && !p_base_type.is_meta_type) {
  5203. for (int i = 0; i < base->functions.size(); i++) {
  5204. FunctionNode *func = base->functions[i];
  5205. if (p_function == func->name) {
  5206. callee = func;
  5207. break;
  5208. }
  5209. }
  5210. }
  5211. p_base_type = base->base_type;
  5212. if (p_base_type.kind == DataType::CLASS) {
  5213. base = p_base_type.class_type;
  5214. } else {
  5215. break;
  5216. }
  5217. }
  5218. if (callee) {
  5219. r_return_type = callee->get_datatype();
  5220. for (int i = 0; i < callee->argument_types.size(); i++) {
  5221. r_arg_types.push_back(callee->argument_types[i]);
  5222. }
  5223. r_default_arg_count = callee->default_values.size();
  5224. return true;
  5225. }
  5226. // Nothing in current file, check parent script
  5227. Ref<GDScript> base_gdscript;
  5228. Ref<Script> base_script;
  5229. StringName native;
  5230. if (p_base_type.kind == DataType::GDSCRIPT) {
  5231. base_gdscript = p_base_type.script_type;
  5232. } else if (p_base_type.kind == DataType::SCRIPT) {
  5233. base_script = p_base_type.script_type;
  5234. } else if (p_base_type.kind == DataType::NATIVE) {
  5235. native = p_base_type.native_type;
  5236. }
  5237. while (base_gdscript.is_valid()) {
  5238. native = base_gdscript->get_instance_base_type();
  5239. Map<StringName, GDScriptFunction *> funcs = base_gdscript->get_member_functions();
  5240. if (funcs.has(p_function)) {
  5241. GDScriptFunction *f = funcs[p_function];
  5242. r_static = f->is_static();
  5243. r_default_arg_count = f->get_default_argument_count();
  5244. r_return_type = _type_from_gdtype(f->get_return_type());
  5245. for (int i = 0; i < f->get_argument_count(); i++) {
  5246. r_arg_types.push_back(_type_from_gdtype(f->get_argument_type(i)));
  5247. }
  5248. return true;
  5249. }
  5250. base_gdscript = base_gdscript->get_base_script();
  5251. }
  5252. while (base_script.is_valid()) {
  5253. native = base_script->get_instance_base_type();
  5254. MethodInfo mi = base_script->get_method_info(p_function);
  5255. if (!(mi == MethodInfo())) {
  5256. r_return_type = _type_from_property(mi.return_val, false);
  5257. r_default_arg_count = mi.default_arguments.size();
  5258. for (List<PropertyInfo>::Element *E = mi.arguments.front(); E; E = E->next()) {
  5259. r_arg_types.push_back(_type_from_property(E->get()));
  5260. }
  5261. return true;
  5262. }
  5263. base_script = base_script->get_base_script();
  5264. }
  5265. #ifdef DEBUG_METHODS_ENABLED
  5266. // Only native remains
  5267. if (!ClassDB::class_exists(native)) {
  5268. native = "_" + native.operator String();
  5269. }
  5270. if (!ClassDB::class_exists(native)) {
  5271. if (!check_types) return false;
  5272. ERR_EXPLAIN("Parser bug: Class '" + String(native) + "' not found.");
  5273. ERR_FAIL_V(false);
  5274. }
  5275. MethodBind *method = ClassDB::get_method(native, p_function);
  5276. if (!method) {
  5277. // Try virtual methods
  5278. List<MethodInfo> virtuals;
  5279. ClassDB::get_virtual_methods(native, &virtuals);
  5280. for (const List<MethodInfo>::Element *E = virtuals.front(); E; E = E->next()) {
  5281. const MethodInfo &mi = E->get();
  5282. if (mi.name == p_function) {
  5283. r_default_arg_count = mi.default_arguments.size();
  5284. for (const List<PropertyInfo>::Element *pi = mi.arguments.front(); pi; pi = pi->next()) {
  5285. r_arg_types.push_back(_type_from_property(pi->get()));
  5286. }
  5287. r_return_type = _type_from_property(mi.return_val, false);
  5288. r_vararg = mi.flags & METHOD_FLAG_VARARG;
  5289. return true;
  5290. }
  5291. }
  5292. // If the base is a script, it might be trying to access members of the Script class itself
  5293. if (original_type.is_meta_type && !(p_function == "new") && (original_type.kind == DataType::SCRIPT || original_type.kind == DataType::GDSCRIPT)) {
  5294. method = ClassDB::get_method(original_type.script_type->get_class_name(), p_function);
  5295. if (method) {
  5296. r_static = true;
  5297. } else {
  5298. // Try virtual methods of the script type
  5299. virtuals.clear();
  5300. ClassDB::get_virtual_methods(original_type.script_type->get_class_name(), &virtuals);
  5301. for (const List<MethodInfo>::Element *E = virtuals.front(); E; E = E->next()) {
  5302. const MethodInfo &mi = E->get();
  5303. if (mi.name == p_function) {
  5304. r_default_arg_count = mi.default_arguments.size();
  5305. for (const List<PropertyInfo>::Element *pi = mi.arguments.front(); pi; pi = pi->next()) {
  5306. r_arg_types.push_back(_type_from_property(pi->get()));
  5307. }
  5308. r_return_type = _type_from_property(mi.return_val, false);
  5309. r_static = true;
  5310. r_vararg = mi.flags & METHOD_FLAG_VARARG;
  5311. return true;
  5312. }
  5313. }
  5314. return false;
  5315. }
  5316. } else {
  5317. return false;
  5318. }
  5319. }
  5320. r_default_arg_count = method->get_default_argument_count();
  5321. r_return_type = _type_from_property(method->get_return_info(), false);
  5322. r_vararg = method->is_vararg();
  5323. for (int i = 0; i < method->get_argument_count(); i++) {
  5324. r_arg_types.push_back(_type_from_property(method->get_argument_info(i)));
  5325. }
  5326. return true;
  5327. #else
  5328. return false;
  5329. #endif
  5330. }
  5331. GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const OperatorNode *p_call) {
  5332. if (p_call->arguments.size() < 1) {
  5333. _set_error("Parser bug: function call without enough arguments.", p_call->line);
  5334. ERR_FAIL_V(DataType());
  5335. }
  5336. DataType return_type;
  5337. List<DataType> arg_types;
  5338. int default_args_count = 0;
  5339. int arg_count = p_call->arguments.size();
  5340. String callee_name;
  5341. bool is_vararg = false;
  5342. switch (p_call->arguments[0]->type) {
  5343. case GDScriptParser::Node::TYPE_TYPE: {
  5344. // Built-in constructor, special case
  5345. TypeNode *tn = static_cast<TypeNode *>(p_call->arguments[0]);
  5346. Vector<DataType> par_types;
  5347. par_types.resize(p_call->arguments.size() - 1);
  5348. for (int i = 1; i < p_call->arguments.size(); i++) {
  5349. par_types.write[i - 1] = _reduce_node_type(p_call->arguments[i]);
  5350. }
  5351. if (error_set) return DataType();
  5352. bool match = false;
  5353. List<MethodInfo> constructors;
  5354. Variant::get_constructor_list(tn->vtype, &constructors);
  5355. PropertyInfo return_type;
  5356. for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
  5357. MethodInfo &mi = E->get();
  5358. if (p_call->arguments.size() - 1 < mi.arguments.size() - mi.default_arguments.size()) {
  5359. continue;
  5360. }
  5361. if (p_call->arguments.size() - 1 > mi.arguments.size()) {
  5362. continue;
  5363. }
  5364. bool types_match = true;
  5365. for (int i = 0; i < par_types.size(); i++) {
  5366. DataType arg_type;
  5367. if (mi.arguments[i].type != Variant::NIL) {
  5368. arg_type.has_type = true;
  5369. arg_type.kind = mi.arguments[i].type == Variant::OBJECT ? DataType::NATIVE : DataType::BUILTIN;
  5370. arg_type.builtin_type = mi.arguments[i].type;
  5371. arg_type.native_type = mi.arguments[i].class_name;
  5372. }
  5373. if (!_is_type_compatible(arg_type, par_types[i], true)) {
  5374. types_match = false;
  5375. break;
  5376. } else {
  5377. #ifdef DEBUG_ENABLED
  5378. 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) {
  5379. _add_warning(GDScriptWarning::NARROWING_CONVERSION, p_call->line, Variant::get_type_name(tn->vtype));
  5380. }
  5381. if (par_types[i].may_yield && p_call->arguments[i + 1]->type == Node::TYPE_OPERATOR) {
  5382. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, p_call->line, _find_function_name(static_cast<OperatorNode *>(p_call->arguments[i + 1])));
  5383. }
  5384. #endif // DEBUG_ENABLED
  5385. }
  5386. }
  5387. if (types_match) {
  5388. match = true;
  5389. return_type = mi.return_val;
  5390. break;
  5391. }
  5392. }
  5393. if (match) {
  5394. return _type_from_property(return_type, false);
  5395. } else if (check_types) {
  5396. String err = "No constructor of '";
  5397. err += Variant::get_type_name(tn->vtype);
  5398. err += "' matches the signature '";
  5399. err += Variant::get_type_name(tn->vtype) + "(";
  5400. for (int i = 0; i < par_types.size(); i++) {
  5401. if (i > 0) err += ", ";
  5402. err += par_types[i].to_string();
  5403. }
  5404. err += ")'.";
  5405. _set_error(err, p_call->line, p_call->column);
  5406. return DataType();
  5407. }
  5408. return DataType();
  5409. } break;
  5410. case GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION: {
  5411. BuiltInFunctionNode *func = static_cast<BuiltInFunctionNode *>(p_call->arguments[0]);
  5412. MethodInfo mi = GDScriptFunctions::get_info(func->function);
  5413. return_type = _type_from_property(mi.return_val, false);
  5414. #ifdef DEBUG_ENABLED
  5415. // Check all arguments beforehand to solve warnings
  5416. for (int i = 1; i < p_call->arguments.size(); i++) {
  5417. _reduce_node_type(p_call->arguments[i]);
  5418. }
  5419. #endif // DEBUG_ENABLED
  5420. // Check arguments
  5421. is_vararg = mi.flags & METHOD_FLAG_VARARG;
  5422. default_args_count = mi.default_arguments.size();
  5423. callee_name = mi.name;
  5424. arg_count -= 1;
  5425. // Check each argument type
  5426. for (List<PropertyInfo>::Element *E = mi.arguments.front(); E; E = E->next()) {
  5427. arg_types.push_back(_type_from_property(E->get()));
  5428. }
  5429. } break;
  5430. default: {
  5431. if (p_call->op == OperatorNode::OP_CALL && p_call->arguments.size() < 2) {
  5432. _set_error("Parser bug: self method call without enough arguments.", p_call->line);
  5433. ERR_FAIL_V(DataType());
  5434. }
  5435. int arg_id = p_call->op == OperatorNode::OP_CALL ? 1 : 0;
  5436. if (p_call->arguments[arg_id]->type != Node::TYPE_IDENTIFIER) {
  5437. _set_error("Parser bug: invalid function call argument.", p_call->line);
  5438. ERR_FAIL_V(DataType());
  5439. }
  5440. #ifdef DEBUG_ENABLED
  5441. // Check all arguments beforehand to solve warnings
  5442. for (int i = arg_id + 1; i < p_call->arguments.size(); i++) {
  5443. _reduce_node_type(p_call->arguments[i]);
  5444. }
  5445. #endif // DEBUG_ENABLED
  5446. IdentifierNode *func_id = static_cast<IdentifierNode *>(p_call->arguments[arg_id]);
  5447. callee_name = func_id->name;
  5448. arg_count -= 1 + arg_id;
  5449. DataType base_type;
  5450. if (p_call->op == OperatorNode::OP_PARENT_CALL) {
  5451. base_type = current_class->base_type;
  5452. } else {
  5453. base_type = _reduce_node_type(p_call->arguments[0]);
  5454. }
  5455. if (!base_type.has_type || (base_type.kind == DataType::BUILTIN && base_type.builtin_type == Variant::NIL)) {
  5456. _mark_line_as_unsafe(p_call->line);
  5457. return DataType();
  5458. }
  5459. if (base_type.kind == DataType::BUILTIN) {
  5460. Variant::CallError err;
  5461. Variant tmp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  5462. if (check_types) {
  5463. if (!tmp.has_method(callee_name)) {
  5464. _set_error("Method '" + callee_name + "' is not declared on base '" + base_type.to_string() + "'.", p_call->line);
  5465. return DataType();
  5466. }
  5467. default_args_count = Variant::get_method_default_arguments(base_type.builtin_type, callee_name).size();
  5468. const Vector<Variant::Type> &var_arg_types = Variant::get_method_argument_types(base_type.builtin_type, callee_name);
  5469. for (int i = 0; i < var_arg_types.size(); i++) {
  5470. DataType argtype;
  5471. if (var_arg_types[i] != Variant::NIL) {
  5472. argtype.has_type = true;
  5473. argtype.kind = DataType::BUILTIN;
  5474. argtype.builtin_type = var_arg_types[i];
  5475. }
  5476. arg_types.push_back(argtype);
  5477. }
  5478. }
  5479. return_type.has_type = true;
  5480. return_type.kind = DataType::BUILTIN;
  5481. return_type.builtin_type = Variant::get_method_return_type(base_type.builtin_type, callee_name);
  5482. break;
  5483. }
  5484. DataType original_type = base_type;
  5485. bool is_initializer = callee_name == "new";
  5486. bool is_static = false;
  5487. bool valid = false;
  5488. if (is_initializer && original_type.is_meta_type) {
  5489. // Try to check it as initializer
  5490. base_type = original_type;
  5491. callee_name = "_init";
  5492. base_type.is_meta_type = false;
  5493. valid = _get_function_signature(base_type, callee_name, return_type, arg_types,
  5494. default_args_count, is_static, is_vararg);
  5495. if (valid) {
  5496. return_type = original_type;
  5497. return_type.is_meta_type = false;
  5498. }
  5499. }
  5500. if (!valid) {
  5501. base_type = original_type;
  5502. return_type = DataType();
  5503. valid = _get_function_signature(base_type, callee_name, return_type, arg_types,
  5504. default_args_count, is_static, is_vararg);
  5505. }
  5506. if (!valid) {
  5507. #ifdef DEBUG_ENABLED
  5508. if (p_call->arguments[0]->type == Node::TYPE_SELF) {
  5509. _set_error("Method '" + callee_name + "' is not declared in the current class.", p_call->line);
  5510. return DataType();
  5511. }
  5512. DataType tmp_type;
  5513. valid = _get_member_type(original_type, func_id->name, tmp_type);
  5514. if (valid) {
  5515. if (tmp_type.is_constant) {
  5516. _add_warning(GDScriptWarning::CONSTANT_USED_AS_FUNCTION, p_call->line, callee_name, original_type.to_string());
  5517. } else {
  5518. _add_warning(GDScriptWarning::PROPERTY_USED_AS_FUNCTION, p_call->line, callee_name, original_type.to_string());
  5519. }
  5520. }
  5521. _add_warning(GDScriptWarning::UNSAFE_METHOD_ACCESS, p_call->line, callee_name, original_type.to_string());
  5522. _mark_line_as_unsafe(p_call->line);
  5523. #endif // DEBUG_ENABLED
  5524. return DataType();
  5525. }
  5526. #ifdef DEBUG_ENABLED
  5527. if (current_function && !for_completion && !is_static && p_call->arguments[0]->type == Node::TYPE_SELF && current_function->_static) {
  5528. if (current_function && current_function->_static && p_call->arguments[0]->type == Node::TYPE_SELF) {
  5529. _set_error("Can't call non-static function from a static function.", p_call->line);
  5530. return DataType();
  5531. }
  5532. }
  5533. if (check_types && !is_static && !is_initializer && base_type.is_meta_type) {
  5534. _set_error("Non-static function '" + String(callee_name) + "' can only be called from an instance.", p_call->line);
  5535. return DataType();
  5536. }
  5537. // Check signal emission for warnings
  5538. 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) {
  5539. ConstantNode *sig = static_cast<ConstantNode *>(p_call->arguments[2]);
  5540. String emitted = sig->value.get_type() == Variant::STRING ? sig->value.operator String() : "";
  5541. for (int i = 0; i < current_class->_signals.size(); i++) {
  5542. if (current_class->_signals[i].name == emitted) {
  5543. current_class->_signals.write[i].emissions += 1;
  5544. break;
  5545. }
  5546. }
  5547. }
  5548. #endif // DEBUG_ENABLED
  5549. } break;
  5550. }
  5551. if (!check_types) {
  5552. return return_type;
  5553. }
  5554. if (arg_count < arg_types.size() - default_args_count) {
  5555. _set_error("Too few arguments for '" + callee_name + "()' call. Expected at least " + itos(arg_types.size() - default_args_count) + ".", p_call->line);
  5556. return return_type;
  5557. }
  5558. if (!is_vararg && arg_count > arg_types.size()) {
  5559. _set_error("Too many arguments for '" + callee_name + "()' call. Expected at most " + itos(arg_types.size()) + ".", p_call->line);
  5560. return return_type;
  5561. }
  5562. int arg_diff = p_call->arguments.size() - arg_count;
  5563. for (int i = arg_diff; i < p_call->arguments.size(); i++) {
  5564. DataType par_type = _reduce_node_type(p_call->arguments[i]);
  5565. if ((i - arg_diff) >= arg_types.size()) {
  5566. continue;
  5567. }
  5568. DataType arg_type = arg_types[i - arg_diff];
  5569. if (!par_type.has_type) {
  5570. _mark_line_as_unsafe(p_call->line);
  5571. #ifdef DEBUG_ENABLED
  5572. if (par_type.may_yield && p_call->arguments[i]->type == Node::TYPE_OPERATOR) {
  5573. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, p_call->line, _find_function_name(static_cast<OperatorNode *>(p_call->arguments[i])));
  5574. }
  5575. #endif // DEBUG_ENABLED
  5576. } else if (!_is_type_compatible(arg_types[i - arg_diff], par_type, true)) {
  5577. // Supertypes are acceptable for dynamic compliance
  5578. if (!_is_type_compatible(par_type, arg_types[i - arg_diff])) {
  5579. _set_error("At '" + callee_name + "()' call, argument " + itos(i - arg_diff + 1) + ". Assigned type (" +
  5580. par_type.to_string() + ") doesn't match the function argument's type (" +
  5581. arg_types[i - arg_diff].to_string() + ").",
  5582. p_call->line);
  5583. return DataType();
  5584. } else {
  5585. _mark_line_as_unsafe(p_call->line);
  5586. }
  5587. } else {
  5588. #ifdef DEBUG_ENABLED
  5589. if (arg_type.kind == DataType::BUILTIN && arg_type.builtin_type == Variant::INT && par_type.kind == DataType::BUILTIN && par_type.builtin_type == Variant::REAL) {
  5590. _add_warning(GDScriptWarning::NARROWING_CONVERSION, p_call->line, callee_name);
  5591. }
  5592. #endif // DEBUG_ENABLED
  5593. }
  5594. }
  5595. return return_type;
  5596. }
  5597. bool GDScriptParser::_get_member_type(const DataType &p_base_type, const StringName &p_member, DataType &r_member_type) const {
  5598. DataType base_type = p_base_type;
  5599. // Check classes in current file
  5600. ClassNode *base = NULL;
  5601. if (base_type.kind == DataType::CLASS) {
  5602. base = base_type.class_type;
  5603. }
  5604. while (base) {
  5605. if (base->constant_expressions.has(p_member)) {
  5606. r_member_type = base->constant_expressions[p_member].expression->get_datatype();
  5607. return true;
  5608. }
  5609. if (!base_type.is_meta_type) {
  5610. for (int i = 0; i < base->variables.size(); i++) {
  5611. ClassNode::Member m = base->variables[i];
  5612. if (m.identifier == p_member) {
  5613. r_member_type = m.data_type;
  5614. return true;
  5615. }
  5616. }
  5617. } else {
  5618. for (int i = 0; i < base->subclasses.size(); i++) {
  5619. ClassNode *c = base->subclasses[i];
  5620. if (c->name == p_member) {
  5621. DataType class_type;
  5622. class_type.has_type = true;
  5623. class_type.is_constant = true;
  5624. class_type.is_meta_type = true;
  5625. class_type.kind = DataType::CLASS;
  5626. class_type.class_type = c;
  5627. r_member_type = class_type;
  5628. return true;
  5629. }
  5630. }
  5631. }
  5632. base_type = base->base_type;
  5633. if (base_type.kind == DataType::CLASS) {
  5634. base = base_type.class_type;
  5635. } else {
  5636. break;
  5637. }
  5638. }
  5639. Ref<GDScript> gds;
  5640. if (base_type.kind == DataType::GDSCRIPT) {
  5641. gds = base_type.script_type;
  5642. }
  5643. Ref<Script> scr;
  5644. if (base_type.kind == DataType::SCRIPT) {
  5645. scr = base_type.script_type;
  5646. }
  5647. StringName native;
  5648. if (base_type.kind == DataType::NATIVE) {
  5649. native = base_type.native_type;
  5650. }
  5651. // Check GDScripts
  5652. while (gds.is_valid()) {
  5653. if (gds->get_constants().has(p_member)) {
  5654. Variant c = gds->get_constants()[p_member];
  5655. r_member_type = _type_from_variant(c);
  5656. return true;
  5657. }
  5658. if (!base_type.is_meta_type) {
  5659. if (gds->get_members().has(p_member)) {
  5660. r_member_type = _type_from_gdtype(gds->get_member_type(p_member));
  5661. return true;
  5662. }
  5663. }
  5664. native = gds->get_instance_base_type();
  5665. if (gds->get_base_script().is_valid()) {
  5666. gds = gds->get_base_script();
  5667. scr = gds->get_base_script();
  5668. bool is_meta = base_type.is_meta_type;
  5669. base_type = _type_from_variant(scr.operator Variant());
  5670. base_type.is_meta_type = is_meta;
  5671. } else {
  5672. break;
  5673. }
  5674. }
  5675. // Check other script types
  5676. while (scr.is_valid()) {
  5677. Map<StringName, Variant> constants;
  5678. scr->get_constants(&constants);
  5679. if (constants.has(p_member)) {
  5680. r_member_type = _type_from_variant(constants[p_member]);
  5681. return true;
  5682. }
  5683. List<PropertyInfo> properties;
  5684. scr->get_script_property_list(&properties);
  5685. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  5686. if (E->get().name == p_member) {
  5687. r_member_type = _type_from_property(E->get());
  5688. return true;
  5689. }
  5690. }
  5691. base_type = _type_from_variant(scr.operator Variant());
  5692. native = scr->get_instance_base_type();
  5693. scr = scr->get_base_script();
  5694. }
  5695. // Check ClassDB
  5696. if (!ClassDB::class_exists(native)) {
  5697. native = "_" + native.operator String();
  5698. }
  5699. if (!ClassDB::class_exists(native)) {
  5700. if (!check_types) return false;
  5701. ERR_EXPLAIN("Parser bug: Class '" + String(native) + "' not found.");
  5702. ERR_FAIL_V(false);
  5703. }
  5704. bool valid = false;
  5705. ClassDB::get_integer_constant(native, p_member, &valid);
  5706. if (valid) {
  5707. DataType ct;
  5708. ct.has_type = true;
  5709. ct.is_constant = true;
  5710. ct.kind = DataType::BUILTIN;
  5711. ct.builtin_type = Variant::INT;
  5712. r_member_type = ct;
  5713. return true;
  5714. }
  5715. if (!base_type.is_meta_type) {
  5716. List<PropertyInfo> properties;
  5717. ClassDB::get_property_list(native, &properties);
  5718. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  5719. if (E->get().name == p_member) {
  5720. // Check if a getter exists
  5721. StringName getter_name = ClassDB::get_property_getter(native, p_member);
  5722. if (getter_name != StringName()) {
  5723. // Use the getter return type
  5724. #ifdef DEBUG_METHODS_ENABLED
  5725. MethodBind *getter_method = ClassDB::get_method(native, getter_name);
  5726. if (getter_method) {
  5727. r_member_type = _type_from_property(getter_method->get_return_info());
  5728. } else {
  5729. r_member_type = DataType();
  5730. }
  5731. #else
  5732. r_member_type = DataType();
  5733. #endif
  5734. } else {
  5735. r_member_type = _type_from_property(E->get());
  5736. }
  5737. return true;
  5738. }
  5739. }
  5740. }
  5741. // If the base is a script, it might be trying to access members of the Script class itself
  5742. if (p_base_type.is_meta_type && (p_base_type.kind == DataType::SCRIPT || p_base_type.kind == DataType::GDSCRIPT)) {
  5743. native = p_base_type.script_type->get_class_name();
  5744. ClassDB::get_integer_constant(native, p_member, &valid);
  5745. if (valid) {
  5746. DataType ct;
  5747. ct.has_type = true;
  5748. ct.is_constant = true;
  5749. ct.kind = DataType::BUILTIN;
  5750. ct.builtin_type = Variant::INT;
  5751. r_member_type = ct;
  5752. return true;
  5753. }
  5754. List<PropertyInfo> properties;
  5755. ClassDB::get_property_list(native, &properties);
  5756. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  5757. if (E->get().name == p_member) {
  5758. // Check if a getter exists
  5759. StringName getter_name = ClassDB::get_property_getter(native, p_member);
  5760. if (getter_name != StringName()) {
  5761. // Use the getter return type
  5762. #ifdef DEBUG_METHODS_ENABLED
  5763. MethodBind *getter_method = ClassDB::get_method(native, getter_name);
  5764. if (getter_method) {
  5765. r_member_type = _type_from_property(getter_method->get_return_info());
  5766. } else {
  5767. r_member_type = DataType();
  5768. }
  5769. #else
  5770. r_member_type = DataType();
  5771. #endif
  5772. } else {
  5773. r_member_type = _type_from_property(E->get());
  5774. }
  5775. return true;
  5776. }
  5777. }
  5778. }
  5779. return false;
  5780. }
  5781. GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType *p_base_type, const StringName &p_identifier, int p_line) {
  5782. if (p_base_type && !p_base_type->has_type) {
  5783. return DataType();
  5784. }
  5785. DataType base_type;
  5786. // Check classes in current file
  5787. ClassNode *base = NULL;
  5788. if (!p_base_type) {
  5789. base = current_class;
  5790. base_type.has_type = true;
  5791. base_type.is_constant = true;
  5792. base_type.kind = DataType::CLASS;
  5793. base_type.class_type = base;
  5794. } else {
  5795. base_type = DataType(*p_base_type);
  5796. if (base_type.kind == DataType::CLASS) {
  5797. base = base_type.class_type;
  5798. }
  5799. }
  5800. DataType member_type;
  5801. for (int i = 0; i < current_class->variables.size(); i++) {
  5802. ClassNode::Member m = current_class->variables[i];
  5803. if (current_class->variables[i].identifier == p_identifier) {
  5804. member_type = current_class->variables[i].data_type;
  5805. current_class->variables.write[i].usages += 1;
  5806. return member_type;
  5807. }
  5808. }
  5809. if (_get_member_type(base_type, p_identifier, member_type)) {
  5810. return member_type;
  5811. }
  5812. if (!p_base_type) {
  5813. // Possibly this is a global, check before failing
  5814. if (ClassDB::class_exists(p_identifier) || ClassDB::class_exists("_" + p_identifier.operator String())) {
  5815. DataType result;
  5816. result.has_type = true;
  5817. result.is_constant = true;
  5818. result.is_meta_type = true;
  5819. if (Engine::get_singleton()->has_singleton(p_identifier) || Engine::get_singleton()->has_singleton("_" + p_identifier.operator String())) {
  5820. result.is_meta_type = false;
  5821. }
  5822. result.kind = DataType::NATIVE;
  5823. result.native_type = p_identifier;
  5824. return result;
  5825. }
  5826. ClassNode *outer_class = current_class;
  5827. while (outer_class) {
  5828. if (outer_class->name == p_identifier) {
  5829. DataType result;
  5830. result.has_type = true;
  5831. result.is_constant = true;
  5832. result.is_meta_type = true;
  5833. result.kind = DataType::CLASS;
  5834. result.class_type = outer_class;
  5835. return result;
  5836. }
  5837. if (outer_class->constant_expressions.has(p_identifier)) {
  5838. return outer_class->constant_expressions[p_identifier].type;
  5839. }
  5840. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  5841. if (outer_class->subclasses[i] == current_class) {
  5842. continue;
  5843. }
  5844. if (outer_class->subclasses[i]->name == p_identifier) {
  5845. DataType result;
  5846. result.has_type = true;
  5847. result.is_constant = true;
  5848. result.is_meta_type = true;
  5849. result.kind = DataType::CLASS;
  5850. result.class_type = outer_class->subclasses[i];
  5851. return result;
  5852. }
  5853. }
  5854. outer_class = outer_class->owner;
  5855. }
  5856. if (ScriptServer::is_global_class(p_identifier)) {
  5857. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  5858. if (scr.is_valid()) {
  5859. DataType result;
  5860. result.has_type = true;
  5861. result.script_type = scr;
  5862. result.is_meta_type = true;
  5863. Ref<GDScript> gds = scr;
  5864. if (gds.is_valid()) {
  5865. if (!gds->is_valid()) {
  5866. _set_error("Class '" + p_identifier + "' could not be fully loaded (script error or cyclic inheritance).");
  5867. return DataType();
  5868. }
  5869. result.kind = DataType::GDSCRIPT;
  5870. } else {
  5871. result.kind = DataType::SCRIPT;
  5872. }
  5873. return result;
  5874. }
  5875. _set_error("Class '" + p_identifier + "' was found in global scope but its script could not be loaded.");
  5876. return DataType();
  5877. }
  5878. if (GDScriptLanguage::get_singleton()->get_global_map().has(p_identifier)) {
  5879. int idx = GDScriptLanguage::get_singleton()->get_global_map()[p_identifier];
  5880. Variant g = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  5881. return _type_from_variant(g);
  5882. }
  5883. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier)) {
  5884. Variant g = GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier];
  5885. return _type_from_variant(g);
  5886. }
  5887. // Non-tool singletons aren't loaded, check project settings
  5888. List<PropertyInfo> props;
  5889. ProjectSettings::get_singleton()->get_property_list(&props);
  5890. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  5891. String s = E->get().name;
  5892. if (!s.begins_with("autoload/")) {
  5893. continue;
  5894. }
  5895. String name = s.get_slice("/", 1);
  5896. if (name == p_identifier) {
  5897. String script = ProjectSettings::get_singleton()->get(s);
  5898. if (script.begins_with("*")) {
  5899. script = script.right(1);
  5900. }
  5901. if (!script.begins_with("res://")) {
  5902. script = "res://" + script;
  5903. }
  5904. Ref<Script> singleton = ResourceLoader::load(script);
  5905. if (singleton.is_valid()) {
  5906. DataType result;
  5907. result.has_type = true;
  5908. result.script_type = singleton;
  5909. Ref<GDScript> gds = singleton;
  5910. if (gds.is_valid()) {
  5911. if (!gds->is_valid()) {
  5912. _set_error("Couldn't fully load singleton script '" + p_identifier + "' (possible cyclic reference or parse error).", p_line);
  5913. return DataType();
  5914. }
  5915. result.kind = DataType::GDSCRIPT;
  5916. } else {
  5917. result.kind = DataType::SCRIPT;
  5918. }
  5919. }
  5920. }
  5921. }
  5922. // This means looking in the current class, which type is always known
  5923. _set_error("Identifier '" + p_identifier.operator String() + "' is not declared in the current scope.", p_line);
  5924. }
  5925. #ifdef DEBUG_ENABLED
  5926. {
  5927. DataType tmp_type;
  5928. List<DataType> arg_types;
  5929. int argcount;
  5930. bool _static;
  5931. bool vararg;
  5932. if (_get_function_signature(base_type, p_identifier, tmp_type, arg_types, argcount, _static, vararg)) {
  5933. _add_warning(GDScriptWarning::FUNCTION_USED_AS_PROPERTY, p_line, p_identifier.operator String(), base_type.to_string());
  5934. }
  5935. }
  5936. #endif // DEBUG_ENABLED
  5937. _mark_line_as_unsafe(p_line);
  5938. return DataType();
  5939. }
  5940. void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
  5941. _mark_line_as_safe(p_class->line);
  5942. // Constants
  5943. for (Map<StringName, ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
  5944. ClassNode::Constant &c = E->get();
  5945. _mark_line_as_safe(c.expression->line);
  5946. DataType cont = _resolve_type(c.type, c.expression->line);
  5947. DataType expr = _resolve_type(c.expression->get_datatype(), c.expression->line);
  5948. if (!_is_type_compatible(cont, expr)) {
  5949. _set_error("Constant value type (" + expr.to_string() + ") is not compatible with declared type (" + cont.to_string() + ").",
  5950. c.expression->line);
  5951. return;
  5952. }
  5953. expr.is_constant = true;
  5954. c.type = expr;
  5955. c.expression->set_datatype(expr);
  5956. }
  5957. // Function declarations
  5958. for (int i = 0; i < p_class->static_functions.size(); i++) {
  5959. _check_function_types(p_class->static_functions[i]);
  5960. if (error_set) return;
  5961. }
  5962. for (int i = 0; i < p_class->functions.size(); i++) {
  5963. _check_function_types(p_class->functions[i]);
  5964. if (error_set) return;
  5965. }
  5966. // Class variables
  5967. for (int i = 0; i < p_class->variables.size(); i++) {
  5968. ClassNode::Member &v = p_class->variables.write[i];
  5969. DataType tmp;
  5970. if (_get_member_type(p_class->base_type, v.identifier, tmp)) {
  5971. _set_error("Member '" + String(v.identifier) + "' already exists in parent class.", v.line);
  5972. return;
  5973. }
  5974. _mark_line_as_safe(v.line);
  5975. v.data_type = _resolve_type(v.data_type, v.line);
  5976. if (v.expression) {
  5977. DataType expr_type = _reduce_node_type(v.expression);
  5978. if (!_is_type_compatible(v.data_type, expr_type)) {
  5979. // Try supertype test
  5980. if (_is_type_compatible(expr_type, v.data_type)) {
  5981. _mark_line_as_unsafe(v.line);
  5982. } else {
  5983. // Try with implicit conversion
  5984. if (v.data_type.kind != DataType::BUILTIN || !_is_type_compatible(v.data_type, expr_type, true)) {
  5985. _set_error("Assigned expression type (" + expr_type.to_string() + ") doesn't match the variable's type (" +
  5986. v.data_type.to_string() + ").",
  5987. v.line);
  5988. return;
  5989. }
  5990. // Replace assigment with implict conversion
  5991. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  5992. convert->line = v.line;
  5993. convert->function = GDScriptFunctions::TYPE_CONVERT;
  5994. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  5995. tgt_type->line = v.line;
  5996. tgt_type->value = (int)v.data_type.builtin_type;
  5997. OperatorNode *convert_call = alloc_node<OperatorNode>();
  5998. convert_call->line = v.line;
  5999. convert_call->op = OperatorNode::OP_CALL;
  6000. convert_call->arguments.push_back(convert);
  6001. convert_call->arguments.push_back(v.expression);
  6002. convert_call->arguments.push_back(tgt_type);
  6003. v.expression = convert_call;
  6004. v.initial_assignment->arguments.write[1] = convert_call;
  6005. }
  6006. }
  6007. if (v.data_type.infer_type) {
  6008. if (!expr_type.has_type) {
  6009. _set_error("Assigned value does not have a set type, variable type cannot be inferred.", v.line);
  6010. return;
  6011. }
  6012. v.data_type = expr_type;
  6013. v.data_type.is_constant = false;
  6014. }
  6015. } else if (v.data_type.has_type && v.data_type.kind == DataType::BUILTIN) {
  6016. // Create default value based on the type
  6017. IdentifierNode *id = alloc_node<IdentifierNode>();
  6018. id->line = v.line;
  6019. id->name = v.identifier;
  6020. ConstantNode *init = alloc_node<ConstantNode>();
  6021. init->line = v.line;
  6022. Variant::CallError err;
  6023. init->value = Variant::construct(v.data_type.builtin_type, NULL, 0, err);
  6024. OperatorNode *op = alloc_node<OperatorNode>();
  6025. op->line = v.line;
  6026. op->op = OperatorNode::OP_INIT_ASSIGN;
  6027. op->arguments.push_back(id);
  6028. op->arguments.push_back(init);
  6029. p_class->initializer->statements.push_front(op);
  6030. v.initial_assignment = op;
  6031. #ifdef DEBUG_ENABLED
  6032. NewLineNode *nl = alloc_node<NewLineNode>();
  6033. nl->line = v.line - 1;
  6034. p_class->initializer->statements.push_front(nl);
  6035. #endif
  6036. }
  6037. // Check export hint
  6038. if (v.data_type.has_type && v._export.type != Variant::NIL) {
  6039. DataType export_type = _type_from_property(v._export);
  6040. if (!_is_type_compatible(v.data_type, export_type, true)) {
  6041. _set_error("Export hint type (" + export_type.to_string() + ") doesn't match the variable's type (" +
  6042. v.data_type.to_string() + ").",
  6043. v.line);
  6044. return;
  6045. }
  6046. }
  6047. // Setter and getter
  6048. if (v.setter == StringName() && v.getter == StringName()) continue;
  6049. bool found_getter = false;
  6050. bool found_setter = false;
  6051. for (int j = 0; j < p_class->functions.size(); j++) {
  6052. if (v.setter == p_class->functions[j]->name) {
  6053. found_setter = true;
  6054. FunctionNode *setter = p_class->functions[j];
  6055. if (setter->arguments.size() != 1) {
  6056. _set_error("Setter function needs to receive exactly 1 argument. See '" + setter->name +
  6057. "()' definition at line " + itos(setter->line) + ".",
  6058. v.line);
  6059. return;
  6060. }
  6061. if (!_is_type_compatible(v.data_type, setter->argument_types[0])) {
  6062. _set_error("Setter argument type (" + setter->argument_types[0].to_string() +
  6063. ") doesn't match the variable's type (" + v.data_type.to_string() + "). See '" +
  6064. setter->name + "()' definition at line " + itos(setter->line) + ".",
  6065. v.line);
  6066. return;
  6067. }
  6068. continue;
  6069. }
  6070. if (v.getter == p_class->functions[j]->name) {
  6071. found_getter = true;
  6072. FunctionNode *getter = p_class->functions[j];
  6073. if (getter->arguments.size() != 0) {
  6074. _set_error("Getter function can't receive arguments. See '" + getter->name +
  6075. "()' definition at line " + itos(getter->line) + ".",
  6076. v.line);
  6077. return;
  6078. }
  6079. if (!_is_type_compatible(v.data_type, getter->get_datatype())) {
  6080. _set_error("Getter return type (" + getter->get_datatype().to_string() +
  6081. ") doesn't match the variable's type (" + v.data_type.to_string() +
  6082. "). See '" + getter->name + "()' definition at line " + itos(getter->line) + ".",
  6083. v.line);
  6084. return;
  6085. }
  6086. }
  6087. if (found_getter && found_setter) break;
  6088. }
  6089. if ((found_getter || v.getter == StringName()) && (found_setter || v.setter == StringName())) continue;
  6090. // Check for static functions
  6091. for (int j = 0; j < p_class->static_functions.size(); j++) {
  6092. if (v.setter == p_class->static_functions[j]->name) {
  6093. FunctionNode *setter = p_class->static_functions[j];
  6094. _set_error("Setter can't be a static function. See '" + setter->name + "()' definition at line " + itos(setter->line) + ".", v.line);
  6095. return;
  6096. }
  6097. if (v.getter == p_class->static_functions[j]->name) {
  6098. FunctionNode *getter = p_class->static_functions[j];
  6099. _set_error("Getter can't be a static function. See '" + getter->name + "()' definition at line " + itos(getter->line) + ".", v.line);
  6100. return;
  6101. }
  6102. }
  6103. if (!found_setter && v.setter != StringName()) {
  6104. _set_error("Setter function is not defined.", v.line);
  6105. return;
  6106. }
  6107. if (!found_getter && v.getter != StringName()) {
  6108. _set_error("Getter function is not defined.", v.line);
  6109. return;
  6110. }
  6111. }
  6112. // Inner classes
  6113. for (int i = 0; i < p_class->subclasses.size(); i++) {
  6114. current_class = p_class->subclasses[i];
  6115. _check_class_level_types(current_class);
  6116. if (error_set) return;
  6117. current_class = p_class;
  6118. }
  6119. }
  6120. void GDScriptParser::_check_function_types(FunctionNode *p_function) {
  6121. p_function->return_type = _resolve_type(p_function->return_type, p_function->line);
  6122. // Arguments
  6123. int defaults_ofs = p_function->arguments.size() - p_function->default_values.size();
  6124. for (int i = 0; i < p_function->arguments.size(); i++) {
  6125. if (i < defaults_ofs) {
  6126. p_function->argument_types.write[i] = _resolve_type(p_function->argument_types[i], p_function->line);
  6127. } else {
  6128. if (p_function->default_values[i - defaults_ofs]->type != Node::TYPE_OPERATOR) {
  6129. _set_error("Parser bug: invalid argument default value.", p_function->line, p_function->column);
  6130. return;
  6131. }
  6132. OperatorNode *op = static_cast<OperatorNode *>(p_function->default_values[i - defaults_ofs]);
  6133. if (op->op != OperatorNode::OP_ASSIGN || op->arguments.size() != 2) {
  6134. _set_error("Parser bug: invalid argument default value operation.", p_function->line);
  6135. return;
  6136. }
  6137. DataType def_type = _reduce_node_type(op->arguments[1]);
  6138. if (p_function->argument_types[i].infer_type) {
  6139. def_type.is_constant = false;
  6140. p_function->argument_types.write[i] = def_type;
  6141. } else {
  6142. p_function->return_type = _resolve_type(p_function->return_type, p_function->line);
  6143. if (!_is_type_compatible(p_function->argument_types[i], def_type, true)) {
  6144. String arg_name = p_function->arguments[i];
  6145. _set_error("Value type (" + def_type.to_string() + ") doesn't match the type of argument '" +
  6146. arg_name + "' (" + p_function->arguments[i] + ")",
  6147. p_function->line);
  6148. }
  6149. }
  6150. }
  6151. #ifdef DEBUG_ENABLED
  6152. if (p_function->arguments_usage[i] == 0) {
  6153. _add_warning(GDScriptWarning::UNUSED_ARGUMENT, p_function->line, p_function->name, p_function->arguments[i].operator String());
  6154. }
  6155. #endif // DEBUG_ENABLED
  6156. }
  6157. if (!(p_function->name == "_init")) {
  6158. // Signature for the initializer may vary
  6159. #ifdef DEBUG_ENABLED
  6160. DataType return_type;
  6161. List<DataType> arg_types;
  6162. int default_arg_count = 0;
  6163. bool _static = false;
  6164. bool vararg = false;
  6165. DataType base_type = current_class->base_type;
  6166. if (_get_function_signature(base_type, p_function->name, return_type, arg_types, default_arg_count, _static, vararg)) {
  6167. bool valid = _static == p_function->_static;
  6168. valid = valid && return_type == p_function->return_type;
  6169. int argsize_diff = p_function->arguments.size() - arg_types.size();
  6170. valid = valid && argsize_diff >= 0;
  6171. valid = valid && p_function->default_values.size() >= default_arg_count + argsize_diff;
  6172. int i = 0;
  6173. for (List<DataType>::Element *E = arg_types.front(); valid && E; E = E->next()) {
  6174. valid = valid && E->get() == p_function->argument_types[i++];
  6175. }
  6176. if (!valid) {
  6177. String parent_signature = return_type.has_type ? return_type.to_string() : "Variant";
  6178. if (parent_signature == "null") {
  6179. parent_signature = "void";
  6180. }
  6181. parent_signature += " " + p_function->name + "(";
  6182. if (arg_types.size()) {
  6183. int i = 0;
  6184. for (List<DataType>::Element *E = arg_types.front(); E; E = E->next()) {
  6185. if (E != arg_types.front()) {
  6186. parent_signature += ", ";
  6187. }
  6188. String arg = E->get().to_string();
  6189. if (arg == "null" || arg == "var") {
  6190. arg = "Variant";
  6191. }
  6192. parent_signature += arg;
  6193. if (i == arg_types.size() - default_arg_count) {
  6194. parent_signature += "=default";
  6195. }
  6196. i++;
  6197. }
  6198. }
  6199. parent_signature += ")";
  6200. _set_error("Function signature doesn't match the parent. Parent signature is: '" + parent_signature + "'.", p_function->line);
  6201. return;
  6202. }
  6203. }
  6204. #endif // DEBUG_ENABLED
  6205. } else {
  6206. if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) {
  6207. _set_error("Constructor cannot return a value.", p_function->line);
  6208. return;
  6209. }
  6210. }
  6211. if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) {
  6212. if (!p_function->body->has_return) {
  6213. _set_error("Non-void function must return a value in all possible paths.", p_function->line);
  6214. return;
  6215. }
  6216. }
  6217. if (p_function->has_yield) {
  6218. // yield() will make the function return a GDScriptFunctionState, so the type is ambiguous
  6219. p_function->return_type.has_type = false;
  6220. p_function->return_type.may_yield = true;
  6221. }
  6222. }
  6223. void GDScriptParser::_check_class_blocks_types(ClassNode *p_class) {
  6224. // Function blocks
  6225. for (int i = 0; i < p_class->static_functions.size(); i++) {
  6226. current_function = p_class->static_functions[i];
  6227. current_block = current_function->body;
  6228. _mark_line_as_safe(current_function->line);
  6229. _check_block_types(current_block);
  6230. current_block = NULL;
  6231. current_function = NULL;
  6232. if (error_set) return;
  6233. }
  6234. for (int i = 0; i < p_class->functions.size(); i++) {
  6235. current_function = p_class->functions[i];
  6236. current_block = current_function->body;
  6237. _mark_line_as_safe(current_function->line);
  6238. _check_block_types(current_block);
  6239. current_block = NULL;
  6240. current_function = NULL;
  6241. if (error_set) return;
  6242. }
  6243. #ifdef DEBUG_ENABLED
  6244. // Warnings
  6245. for (int i = 0; i < p_class->variables.size(); i++) {
  6246. if (p_class->variables[i].usages == 0) {
  6247. _add_warning(GDScriptWarning::UNUSED_CLASS_VARIABLE, p_class->variables[i].line, p_class->variables[i].identifier);
  6248. }
  6249. }
  6250. for (int i = 0; i < p_class->_signals.size(); i++) {
  6251. if (p_class->_signals[i].emissions == 0) {
  6252. _add_warning(GDScriptWarning::UNUSED_SIGNAL, p_class->_signals[i].line, p_class->_signals[i].name);
  6253. }
  6254. }
  6255. #endif // DEBUG_ENABLED
  6256. // Inner classes
  6257. for (int i = 0; i < p_class->subclasses.size(); i++) {
  6258. current_class = p_class->subclasses[i];
  6259. _check_class_blocks_types(current_class);
  6260. if (error_set) return;
  6261. current_class = p_class;
  6262. }
  6263. }
  6264. #ifdef DEBUG_ENABLED
  6265. static String _find_function_name(const GDScriptParser::OperatorNode *p_call) {
  6266. switch (p_call->arguments[0]->type) {
  6267. case GDScriptParser::Node::TYPE_TYPE: {
  6268. return Variant::get_type_name(static_cast<GDScriptParser::TypeNode *>(p_call->arguments[0])->vtype);
  6269. } break;
  6270. case GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION: {
  6271. return GDScriptFunctions::get_func_name(static_cast<GDScriptParser::BuiltInFunctionNode *>(p_call->arguments[0])->function);
  6272. } break;
  6273. default: {
  6274. int id_index = p_call->op == GDScriptParser::OperatorNode::OP_PARENT_CALL ? 0 : 1;
  6275. if (p_call->arguments.size() > id_index && p_call->arguments[id_index]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  6276. return static_cast<GDScriptParser::IdentifierNode *>(p_call->arguments[id_index])->name;
  6277. }
  6278. } break;
  6279. }
  6280. return String();
  6281. }
  6282. #endif // DEBUG_ENABLED
  6283. void GDScriptParser::_check_block_types(BlockNode *p_block) {
  6284. Node *last_var_assign = NULL;
  6285. // Check each statement
  6286. for (List<Node *>::Element *E = p_block->statements.front(); E; E = E->next()) {
  6287. Node *statement = E->get();
  6288. switch (statement->type) {
  6289. case Node::TYPE_NEWLINE:
  6290. case Node::TYPE_BREAKPOINT:
  6291. case Node::TYPE_ASSERT: {
  6292. // Nothing to do
  6293. } break;
  6294. case Node::TYPE_LOCAL_VAR: {
  6295. LocalVarNode *lv = static_cast<LocalVarNode *>(statement);
  6296. lv->datatype = _resolve_type(lv->datatype, lv->line);
  6297. _mark_line_as_safe(lv->line);
  6298. last_var_assign = lv->assign;
  6299. if (lv->assign) {
  6300. DataType assign_type = _reduce_node_type(lv->assign);
  6301. #ifdef DEBUG_ENABLED
  6302. if (assign_type.has_type && assign_type.kind == DataType::BUILTIN && assign_type.builtin_type == Variant::NIL) {
  6303. if (lv->assign->type == Node::TYPE_OPERATOR) {
  6304. OperatorNode *call = static_cast<OperatorNode *>(lv->assign);
  6305. if (call->op == OperatorNode::OP_CALL || call->op == OperatorNode::OP_PARENT_CALL) {
  6306. _add_warning(GDScriptWarning::VOID_ASSIGNMENT, lv->line, _find_function_name(call));
  6307. }
  6308. }
  6309. }
  6310. if (lv->datatype.has_type && assign_type.may_yield && lv->assign->type == Node::TYPE_OPERATOR) {
  6311. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, lv->line, _find_function_name(static_cast<OperatorNode *>(lv->assign)));
  6312. }
  6313. #endif // DEBUG_ENABLED
  6314. if (!_is_type_compatible(lv->datatype, assign_type)) {
  6315. // Try supertype test
  6316. if (_is_type_compatible(assign_type, lv->datatype)) {
  6317. _mark_line_as_unsafe(lv->line);
  6318. } else {
  6319. // Try implict conversion
  6320. if (lv->datatype.kind != DataType::BUILTIN || !_is_type_compatible(lv->datatype, assign_type, true)) {
  6321. _set_error("Assigned value type (" + assign_type.to_string() + ") doesn't match the variable's type (" +
  6322. lv->datatype.to_string() + ").",
  6323. lv->line);
  6324. return;
  6325. }
  6326. // Replace assigment with implict conversion
  6327. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  6328. convert->line = lv->line;
  6329. convert->function = GDScriptFunctions::TYPE_CONVERT;
  6330. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  6331. tgt_type->line = lv->line;
  6332. tgt_type->value = (int)lv->datatype.builtin_type;
  6333. OperatorNode *convert_call = alloc_node<OperatorNode>();
  6334. convert_call->line = lv->line;
  6335. convert_call->op = OperatorNode::OP_CALL;
  6336. convert_call->arguments.push_back(convert);
  6337. convert_call->arguments.push_back(lv->assign);
  6338. convert_call->arguments.push_back(tgt_type);
  6339. lv->assign = convert_call;
  6340. lv->assign_op->arguments.write[1] = convert_call;
  6341. #ifdef DEBUG_ENABLED
  6342. if (lv->datatype.builtin_type == Variant::INT && assign_type.builtin_type == Variant::REAL) {
  6343. _add_warning(GDScriptWarning::NARROWING_CONVERSION, lv->line);
  6344. }
  6345. #endif // DEBUG_ENABLED
  6346. }
  6347. }
  6348. if (lv->datatype.infer_type) {
  6349. if (!assign_type.has_type) {
  6350. _set_error("Assigned value does not have a set type, variable type cannot be inferred.", lv->line);
  6351. return;
  6352. }
  6353. lv->datatype = assign_type;
  6354. lv->datatype.is_constant = false;
  6355. }
  6356. if (lv->datatype.has_type && !assign_type.has_type) {
  6357. _mark_line_as_unsafe(lv->line);
  6358. }
  6359. }
  6360. } break;
  6361. case Node::TYPE_OPERATOR: {
  6362. OperatorNode *op = static_cast<OperatorNode *>(statement);
  6363. switch (op->op) {
  6364. case OperatorNode::OP_ASSIGN:
  6365. case OperatorNode::OP_ASSIGN_ADD:
  6366. case OperatorNode::OP_ASSIGN_SUB:
  6367. case OperatorNode::OP_ASSIGN_MUL:
  6368. case OperatorNode::OP_ASSIGN_DIV:
  6369. case OperatorNode::OP_ASSIGN_MOD:
  6370. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  6371. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  6372. case OperatorNode::OP_ASSIGN_BIT_AND:
  6373. case OperatorNode::OP_ASSIGN_BIT_OR:
  6374. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  6375. if (op->arguments.size() < 2) {
  6376. _set_error("Parser bug: operation without enough arguments.", op->line, op->column);
  6377. return;
  6378. }
  6379. if (op->arguments[1] == last_var_assign) {
  6380. // Assignment was already checked
  6381. break;
  6382. }
  6383. _mark_line_as_safe(op->line);
  6384. DataType lh_type = _reduce_node_type(op->arguments[0]);
  6385. if (error_set) {
  6386. return;
  6387. }
  6388. if (!lh_type.has_type) {
  6389. if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
  6390. _mark_line_as_unsafe(op->line);
  6391. }
  6392. } else if (lh_type.is_constant) {
  6393. _set_error("Cannot assign a new value to a constant.", op->line);
  6394. return;
  6395. }
  6396. DataType rh_type;
  6397. if (op->op != OperatorNode::OP_ASSIGN) {
  6398. // Validate operation
  6399. DataType arg_type = _reduce_node_type(op->arguments[1]);
  6400. if (!arg_type.has_type) {
  6401. _mark_line_as_unsafe(op->line);
  6402. break;
  6403. }
  6404. Variant::Operator oper = _get_variant_operation(op->op);
  6405. bool valid = false;
  6406. rh_type = _get_operation_type(oper, lh_type, arg_type, valid);
  6407. if (!valid) {
  6408. _set_error("Invalid operand types ('" + lh_type.to_string() + "' and '" + arg_type.to_string() +
  6409. "') to assignment operator '" + Variant::get_operator_name(oper) + "'.",
  6410. op->line);
  6411. return;
  6412. }
  6413. } else {
  6414. rh_type = _reduce_node_type(op->arguments[1]);
  6415. }
  6416. #ifdef DEBUG_ENABLED
  6417. if (rh_type.has_type && rh_type.kind == DataType::BUILTIN && rh_type.builtin_type == Variant::NIL) {
  6418. if (op->arguments[1]->type == Node::TYPE_OPERATOR) {
  6419. OperatorNode *call = static_cast<OperatorNode *>(op->arguments[1]);
  6420. if (call->op == OperatorNode::OP_CALL || call->op == OperatorNode::OP_PARENT_CALL) {
  6421. _add_warning(GDScriptWarning::VOID_ASSIGNMENT, op->line, _find_function_name(call));
  6422. }
  6423. }
  6424. }
  6425. if (lh_type.has_type && rh_type.may_yield && op->arguments[1]->type == Node::TYPE_OPERATOR) {
  6426. _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, op->line, _find_function_name(static_cast<OperatorNode *>(op->arguments[1])));
  6427. }
  6428. #endif // DEBUG_ENABLED
  6429. if (!_is_type_compatible(lh_type, rh_type)) {
  6430. // Try supertype test
  6431. if (_is_type_compatible(rh_type, lh_type)) {
  6432. _mark_line_as_unsafe(op->line);
  6433. } else {
  6434. // Try implict conversion
  6435. if (lh_type.kind != DataType::BUILTIN || !_is_type_compatible(lh_type, rh_type, true)) {
  6436. _set_error("Assigned value type (" + rh_type.to_string() + ") doesn't match the variable's type (" +
  6437. lh_type.to_string() + ").",
  6438. op->line);
  6439. return;
  6440. }
  6441. // Replace assigment with implict conversion
  6442. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  6443. convert->line = op->line;
  6444. convert->function = GDScriptFunctions::TYPE_CONVERT;
  6445. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  6446. tgt_type->line = op->line;
  6447. tgt_type->value = (int)lh_type.builtin_type;
  6448. OperatorNode *convert_call = alloc_node<OperatorNode>();
  6449. convert_call->line = op->line;
  6450. convert_call->op = OperatorNode::OP_CALL;
  6451. convert_call->arguments.push_back(convert);
  6452. convert_call->arguments.push_back(op->arguments[1]);
  6453. convert_call->arguments.push_back(tgt_type);
  6454. op->arguments.write[1] = convert_call;
  6455. #ifdef DEBUG_ENABLED
  6456. if (lh_type.builtin_type == Variant::INT && rh_type.builtin_type == Variant::REAL) {
  6457. _add_warning(GDScriptWarning::NARROWING_CONVERSION, op->line);
  6458. }
  6459. #endif // DEBUG_ENABLED
  6460. }
  6461. }
  6462. if (!rh_type.has_type && (op->op != OperatorNode::OP_ASSIGN || lh_type.has_type || op->arguments[0]->type == Node::TYPE_OPERATOR)) {
  6463. _mark_line_as_unsafe(op->line);
  6464. }
  6465. } break;
  6466. case OperatorNode::OP_CALL:
  6467. case OperatorNode::OP_PARENT_CALL: {
  6468. _mark_line_as_safe(op->line);
  6469. DataType func_type = _reduce_function_call_type(op);
  6470. #ifdef DEBUG_ENABLED
  6471. if (func_type.has_type && (func_type.kind != DataType::BUILTIN || func_type.builtin_type != Variant::NIL)) {
  6472. // Figure out function name for warning
  6473. String func_name = _find_function_name(op);
  6474. if (func_name.empty()) {
  6475. func_name == "<undetected name>";
  6476. }
  6477. _add_warning(GDScriptWarning::RETURN_VALUE_DISCARDED, op->line, func_name);
  6478. }
  6479. #endif // DEBUG_ENABLED
  6480. if (error_set) return;
  6481. } break;
  6482. case OperatorNode::OP_YIELD: {
  6483. _mark_line_as_safe(op->line);
  6484. _reduce_node_type(op);
  6485. } break;
  6486. default: {
  6487. _mark_line_as_safe(op->line);
  6488. _reduce_node_type(op); // Test for safety anyway
  6489. #ifdef DEBUG_ENABLED
  6490. _add_warning(GDScriptWarning::STANDALONE_EXPRESSION, statement->line);
  6491. #endif // DEBUG_ENABLED
  6492. }
  6493. }
  6494. } break;
  6495. case Node::TYPE_CONTROL_FLOW: {
  6496. ControlFlowNode *cf = static_cast<ControlFlowNode *>(statement);
  6497. _mark_line_as_safe(cf->line);
  6498. switch (cf->cf_type) {
  6499. case ControlFlowNode::CF_RETURN: {
  6500. DataType function_type = current_function->get_datatype();
  6501. DataType ret_type;
  6502. if (cf->arguments.size() > 0) {
  6503. ret_type = _reduce_node_type(cf->arguments[0]);
  6504. if (error_set) {
  6505. return;
  6506. }
  6507. }
  6508. if (!function_type.has_type) break;
  6509. if (function_type.kind == DataType::BUILTIN && function_type.builtin_type == Variant::NIL) {
  6510. // Return void, should not have arguments
  6511. if (cf->arguments.size() > 0) {
  6512. _set_error("Void function cannot return a value.", cf->line, cf->column);
  6513. return;
  6514. }
  6515. } else {
  6516. // Return something, cannot be empty
  6517. if (cf->arguments.size() == 0) {
  6518. _set_error("Non-void function must return a value.", cf->line, cf->column);
  6519. return;
  6520. }
  6521. if (!_is_type_compatible(function_type, ret_type)) {
  6522. _set_error("Returned value type (" + ret_type.to_string() + ") doesn't match the function return type (" +
  6523. function_type.to_string() + ").",
  6524. cf->line, cf->column);
  6525. return;
  6526. }
  6527. }
  6528. } break;
  6529. case ControlFlowNode::CF_MATCH: {
  6530. MatchNode *match_node = cf->match;
  6531. _transform_match_statment(match_node);
  6532. } break;
  6533. default: {
  6534. if (cf->body_else) {
  6535. _mark_line_as_safe(cf->body_else->line);
  6536. }
  6537. for (int i = 0; i < cf->arguments.size(); i++) {
  6538. _reduce_node_type(cf->arguments[i]);
  6539. }
  6540. } break;
  6541. }
  6542. } break;
  6543. case Node::TYPE_CONSTANT: {
  6544. ConstantNode *cn = static_cast<ConstantNode *>(statement);
  6545. // Strings are fine since they can be multiline comments
  6546. if (cn->value.get_type() == Variant::STRING) {
  6547. break;
  6548. }
  6549. } // falthrough
  6550. default: {
  6551. _mark_line_as_safe(statement->line);
  6552. _reduce_node_type(statement); // Test for safety anyway
  6553. #ifdef DEBUG_ENABLED
  6554. _add_warning(GDScriptWarning::STANDALONE_EXPRESSION, statement->line);
  6555. #endif // DEBUG_ENABLED
  6556. }
  6557. }
  6558. }
  6559. // Parse sub blocks
  6560. for (int i = 0; i < p_block->sub_blocks.size(); i++) {
  6561. current_block = p_block->sub_blocks[i];
  6562. _check_block_types(current_block);
  6563. current_block = p_block;
  6564. if (error_set) return;
  6565. }
  6566. #ifdef DEBUG_ENABLED
  6567. // Warnings check
  6568. for (Map<StringName, LocalVarNode *>::Element *E = p_block->variables.front(); E; E = E->next()) {
  6569. LocalVarNode *lv = E->get();
  6570. if (lv->usages == 0) {
  6571. _add_warning(GDScriptWarning::UNUSED_VARIABLE, lv->line, lv->name);
  6572. } else if (lv->assignments == 0) {
  6573. _add_warning(GDScriptWarning::UNASSIGNED_VARIABLE, lv->line, lv->name);
  6574. }
  6575. }
  6576. #endif // DEBUG_ENABLED
  6577. }
  6578. void GDScriptParser::_set_error(const String &p_error, int p_line, int p_column) {
  6579. if (error_set)
  6580. return; //allow no further errors
  6581. error = p_error;
  6582. error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
  6583. error_column = p_column < 0 ? tokenizer->get_token_column() : p_column;
  6584. error_set = true;
  6585. }
  6586. #ifdef DEBUG_ENABLED
  6587. 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) {
  6588. Vector<String> symbols;
  6589. if (!p_symbol1.empty()) {
  6590. symbols.push_back(p_symbol1);
  6591. }
  6592. if (!p_symbol2.empty()) {
  6593. symbols.push_back(p_symbol2);
  6594. }
  6595. if (!p_symbol3.empty()) {
  6596. symbols.push_back(p_symbol3);
  6597. }
  6598. if (!p_symbol4.empty()) {
  6599. symbols.push_back(p_symbol4);
  6600. }
  6601. _add_warning(p_code, p_line, symbols);
  6602. }
  6603. void GDScriptParser::_add_warning(int p_code, int p_line, const Vector<String> &p_symbols) {
  6604. if (tokenizer->is_ignoring_warnings() || !GLOBAL_GET("debug/gdscript/warnings/enable").booleanize()) {
  6605. return;
  6606. }
  6607. String warn_name = GDScriptWarning::get_name_from_code((GDScriptWarning::Code)p_code).to_lower();
  6608. if (tokenizer->get_warning_global_skips().has(warn_name)) {
  6609. return;
  6610. }
  6611. if (!GLOBAL_GET("debug/gdscript/warnings/" + warn_name)) {
  6612. return;
  6613. }
  6614. GDScriptWarning warn;
  6615. warn.code = (GDScriptWarning::Code)p_code;
  6616. warn.symbols = p_symbols;
  6617. warn.line = p_line == -1 ? tokenizer->get_token_line() : p_line;
  6618. List<GDScriptWarning>::Element *before = NULL;
  6619. for (List<GDScriptWarning>::Element *E = warnings.front(); E; E = E->next()) {
  6620. if (E->get().line > warn.line) {
  6621. break;
  6622. }
  6623. before = E;
  6624. }
  6625. if (before) {
  6626. warnings.insert_after(before, warn);
  6627. } else {
  6628. warnings.push_front(warn);
  6629. }
  6630. }
  6631. #endif // DEBUG_ENABLED
  6632. String GDScriptParser::get_error() const {
  6633. return error;
  6634. }
  6635. int GDScriptParser::get_error_line() const {
  6636. return error_line;
  6637. }
  6638. int GDScriptParser::get_error_column() const {
  6639. return error_column;
  6640. }
  6641. Error GDScriptParser::_parse(const String &p_base_path) {
  6642. base_path = p_base_path;
  6643. //assume class
  6644. ClassNode *main_class = alloc_node<ClassNode>();
  6645. main_class->initializer = alloc_node<BlockNode>();
  6646. main_class->initializer->parent_class = main_class;
  6647. main_class->ready = alloc_node<BlockNode>();
  6648. main_class->ready->parent_class = main_class;
  6649. current_class = main_class;
  6650. _parse_class(main_class);
  6651. if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) {
  6652. error_set = false;
  6653. _set_error("Parse Error: " + tokenizer->get_token_error());
  6654. }
  6655. if (error_set && !for_completion) {
  6656. return ERR_PARSE_ERROR;
  6657. }
  6658. _determine_inheritance(main_class);
  6659. if (error_set) {
  6660. return ERR_PARSE_ERROR;
  6661. }
  6662. current_class = main_class;
  6663. current_function = NULL;
  6664. current_block = NULL;
  6665. #ifdef DEBUG_ENABLED
  6666. if (for_completion) check_types = false;
  6667. #else
  6668. check_types = false;
  6669. #endif
  6670. // Resolve all class-level stuff before getting into function blocks
  6671. _check_class_level_types(main_class);
  6672. if (error_set) {
  6673. return ERR_PARSE_ERROR;
  6674. }
  6675. // Resolve the function blocks
  6676. _check_class_blocks_types(main_class);
  6677. if (error_set) {
  6678. return ERR_PARSE_ERROR;
  6679. }
  6680. #ifdef DEBUG_ENABLED
  6681. // Resolve warning ignores
  6682. Vector<Pair<int, String> > warning_skips = tokenizer->get_warning_skips();
  6683. bool warning_is_error = GLOBAL_GET("debug/gdscript/warnings/treat_warnings_as_errors").booleanize();
  6684. for (List<GDScriptWarning>::Element *E = warnings.front(); E;) {
  6685. GDScriptWarning &w = E->get();
  6686. int skip_index = -1;
  6687. for (int i = 0; i < warning_skips.size(); i++) {
  6688. if (warning_skips[i].first >= w.line) {
  6689. break;
  6690. }
  6691. skip_index = i;
  6692. }
  6693. List<GDScriptWarning>::Element *next = E->next();
  6694. bool erase = false;
  6695. if (skip_index != -1) {
  6696. if (warning_skips[skip_index].second == GDScriptWarning::get_name_from_code(w.code).to_lower()) {
  6697. erase = true;
  6698. }
  6699. warning_skips.remove(skip_index);
  6700. }
  6701. if (erase) {
  6702. warnings.erase(E);
  6703. } else if (warning_is_error) {
  6704. _set_error(w.get_message() + " (warning treated as error)", w.line);
  6705. return ERR_PARSE_ERROR;
  6706. }
  6707. E = next;
  6708. }
  6709. #endif // DEBUG_ENABLED
  6710. return OK;
  6711. }
  6712. Error GDScriptParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
  6713. clear();
  6714. self_path = p_self_path;
  6715. GDScriptTokenizerBuffer *tb = memnew(GDScriptTokenizerBuffer);
  6716. tb->set_code_buffer(p_bytecode);
  6717. tokenizer = tb;
  6718. Error ret = _parse(p_base_path);
  6719. memdelete(tb);
  6720. tokenizer = NULL;
  6721. return ret;
  6722. }
  6723. 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) {
  6724. clear();
  6725. self_path = p_self_path;
  6726. GDScriptTokenizerText *tt = memnew(GDScriptTokenizerText);
  6727. tt->set_code(p_code);
  6728. validating = p_just_validate;
  6729. for_completion = p_for_completion;
  6730. #ifdef DEBUG_ENABLED
  6731. safe_lines = r_safe_lines;
  6732. #endif // DEBUG_ENABLED
  6733. tokenizer = tt;
  6734. Error ret = _parse(p_base_path);
  6735. memdelete(tt);
  6736. tokenizer = NULL;
  6737. return ret;
  6738. }
  6739. bool GDScriptParser::is_tool_script() const {
  6740. return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
  6741. }
  6742. const GDScriptParser::Node *GDScriptParser::get_parse_tree() const {
  6743. return head;
  6744. }
  6745. void GDScriptParser::clear() {
  6746. while (list) {
  6747. Node *l = list;
  6748. list = list->next;
  6749. memdelete(l);
  6750. }
  6751. head = NULL;
  6752. list = NULL;
  6753. completion_type = COMPLETION_NONE;
  6754. completion_node = NULL;
  6755. completion_class = NULL;
  6756. completion_function = NULL;
  6757. completion_block = NULL;
  6758. current_block = NULL;
  6759. current_class = NULL;
  6760. completion_found = false;
  6761. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  6762. current_function = NULL;
  6763. validating = false;
  6764. for_completion = false;
  6765. error_set = false;
  6766. tab_level.clear();
  6767. tab_level.push_back(0);
  6768. error_line = 0;
  6769. error_column = 0;
  6770. pending_newline = -1;
  6771. parenthesis = 0;
  6772. current_export.type = Variant::NIL;
  6773. check_types = true;
  6774. error = "";
  6775. #ifdef DEBUG_ENABLED
  6776. safe_lines = NULL;
  6777. #endif // DEBUG_ENABLED
  6778. }
  6779. GDScriptParser::CompletionType GDScriptParser::get_completion_type() {
  6780. return completion_type;
  6781. }
  6782. StringName GDScriptParser::get_completion_cursor() {
  6783. return completion_cursor;
  6784. }
  6785. int GDScriptParser::get_completion_line() {
  6786. return completion_line;
  6787. }
  6788. Variant::Type GDScriptParser::get_completion_built_in_constant() {
  6789. return completion_built_in_constant;
  6790. }
  6791. GDScriptParser::Node *GDScriptParser::get_completion_node() {
  6792. return completion_node;
  6793. }
  6794. GDScriptParser::BlockNode *GDScriptParser::get_completion_block() {
  6795. return completion_block;
  6796. }
  6797. GDScriptParser::ClassNode *GDScriptParser::get_completion_class() {
  6798. return completion_class;
  6799. }
  6800. GDScriptParser::FunctionNode *GDScriptParser::get_completion_function() {
  6801. return completion_function;
  6802. }
  6803. int GDScriptParser::get_completion_argument_index() {
  6804. return completion_argument;
  6805. }
  6806. int GDScriptParser::get_completion_identifier_is_function() {
  6807. return completion_ident_is_call;
  6808. }
  6809. GDScriptParser::GDScriptParser() {
  6810. head = NULL;
  6811. list = NULL;
  6812. tokenizer = NULL;
  6813. pending_newline = -1;
  6814. clear();
  6815. }
  6816. GDScriptParser::~GDScriptParser() {
  6817. clear();
  6818. }