gdscript_parser.cpp 266 KB

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