gdscript_parser.cpp 260 KB

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