gdscript_parser.cpp 256 KB

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