gdscript_parser.cpp 260 KB

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