gdscript_parser.cpp 261 KB

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