gdscript_parser.cpp 257 KB

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