gdscript_parser.cpp 232 KB

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