gdscript_parser.cpp 249 KB

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