gdscript_parser.cpp 266 KB

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