2
0

gdscript_parser.cpp 257 KB

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