gdscript_parser.cpp 261 KB

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