12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311 |
- (*
- * Copyright (C)2005-2013 Haxe Foundation
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- *)
- (*
- Gen Common API
- This module intends to be a common set of utilities common to all targets.
- It's intended to provide a set of tools to be able to make targets in Haxe more easily, and to
- allow the programmer to have more control of how the target language will handle the program.
- For example, as of now, the hxcpp target, while greatly done, relies heavily on cpp's own operator
- overloading, and implicit conversions, which make it very hard to deliver a similar solution for languages
- that lack these features.
- So this little framework is here so you can manipulate the Haxe AST and start bringing the AST closer
- to how it's intenteded to be in your host language.
- Rules
- Design goals
- Naming convention
- Weaknesses and TODO's
- *)
- open Unix
- open Ast
- open Type
- open Common
- open Option
- open Printf
- open ExtString
- let debug_type_ctor = function
- | TMono _ -> "TMono"
- | TEnum _ -> "TEnum"
- | TInst _ -> "TInst"
- | TType _ -> "TType"
- | TFun _ -> "TFun"
- | TAnon _ -> "TAnon"
- | TDynamic _ -> "TDynamic"
- | TLazy _ -> "TLazy"
- | TAbstract _ -> "TAbstract"
- let debug_type = (s_type (print_context()))
- let debug_expr = s_expr debug_type
- let rec like_float t =
- match follow t with
- | TAbstract({ a_path = ([], "Float") },[])
- | TAbstract({ a_path = ([], "Int") },[]) -> true
- | TAbstract({ a_path = (["cs"], "Pointer") },_) -> false
- | TAbstract(a, _) -> List.exists (fun t -> like_float t) a.a_from || List.exists (fun t -> like_float t) a.a_to
- | _ -> false
- let rec like_int t =
- match follow t with
- | TAbstract({ a_path = ([], "Int") },[]) -> true
- | TAbstract({ a_path = (["cs"], "Pointer") },_) -> false
- | TAbstract(a, _) -> List.exists (fun t -> like_int t) a.a_from || List.exists (fun t -> like_int t) a.a_to
- | _ -> false
- let rec like_i64 t =
- match follow t with
- | TInst({ cl_path = (["cs"], "Int64") },[])
- | TAbstract({ a_path = (["cs"], "Int64") },[])
- | TInst({ cl_path = (["cs"], "UInt64") },[])
- | TInst({ cl_path = (["java"], "Int64") },[])
- | TAbstract({ a_path = (["java"], "Int64") },[])
- | TInst({ cl_path = (["haxe"], "Int64") },[])
- | TAbstract({ a_path = (["haxe"], "Int64") },[]) -> true
- | TAbstract(a, _) -> List.exists (fun t -> like_i64 t) a.a_from || List.exists (fun t -> like_i64 t) a.a_to
- | _ -> false
- let follow_once t =
- match t with
- | TMono r ->
- (match !r with
- | Some t -> t
- | _ -> t_dynamic (* avoid infinite loop / should be the same in this context *))
- | TLazy f ->
- !f()
- | TType (t,tl) ->
- apply_params t.t_params tl t.t_type
- | _ -> t
- let t_empty = TAnon({ a_fields = PMap.empty; a_status = ref (Closed) })
- (* the undefined is a special var that works like null, but can have special meaning *)
- let v_undefined = alloc_var "__undefined__" t_dynamic
- let undefined pos = { eexpr = TLocal(v_undefined); etype = t_dynamic; epos = pos }
- module ExprHashtblHelper =
- struct
- type hash_texpr_t =
- {
- hepos : pos;
- heexpr : int;
- hetype : int;
- }
- let mk_heexpr = function
- | TConst _ -> 0 | TLocal _ -> 1 | TArray _ -> 3 | TBinop _ -> 4 | TField _ -> 5 | TTypeExpr _ -> 7 | TParenthesis _ -> 8 | TObjectDecl _ -> 9
- | TArrayDecl _ -> 10 | TCall _ -> 11 | TNew _ -> 12 | TUnop _ -> 13 | TFunction _ -> 14 | TVar _ -> 15 | TBlock _ -> 16 | TFor _ -> 17 | TIf _ -> 18 | TWhile _ -> 19
- | TSwitch _ -> 20 (* | TPatMatch _ -> 21 *) | TTry _ -> 22 | TReturn _ -> 23 | TBreak -> 24 | TContinue -> 25 | TThrow _ -> 26 | TCast _ -> 27 | TMeta _ -> 28 | TEnumParameter _ -> 29
- let mk_heetype = function
- | TMono _ -> 0 | TEnum _ -> 1 | TInst _ -> 2 | TType _ -> 3 | TFun _ -> 4
- | TAnon _ -> 5 | TDynamic _ -> 6 | TLazy _ -> 7 | TAbstract _ -> 8
- let mk_type e =
- {
- hepos = e.epos;
- heexpr = mk_heexpr e.eexpr;
- hetype = mk_heetype e.etype;
- }
- end;;
- let path_of_md_def md_def =
- match md_def.m_types with
- | [TClassDecl c] -> c.cl_path
- | _ -> md_def.m_path
- open ExprHashtblHelper;;
- (* Expression Hashtbl. This shouldn't be kept indefinately as it's not a weak Hashtbl. *)
- module ExprHashtbl = Hashtbl.Make(
- struct
- type t = Type.texpr
- let equal = (==)
- let hash t = Hashtbl.hash (mk_type t)
- end
- );;
- (* ******************************************* *)
- (* Gen Common
- This is the key module for generation of Java and C# sources
- In order for both modules to share as much code as possible, some
- rules were devised:
- - every feature has its own submodule, and may contain the following methods:
- - configure
- sets all the configuration variables for the module to run. If a module has this method,
- it *should* be called once before running any filter
- - run_filter ->
- runs the filter immediately on the context
- - add_filter ->
- adds the filter to an expr->expr list. Most filter modules will provide this option so the filter
- function can only run once.
- - most submodules will have side-effects so the order of operations will matter.
- When running configure / add_filter this might be taken care of with the rule-based dispatch system working
- underneath, but still there might be some incompatibilities. There will be an effort to document it.
- The modules can hint on the order by suffixing their functions with _first or _last.
- - any of those methods might have different parameters, that configure how the filter will run.
- For example, a simple filter that maps switch() expressions to if () .. else if... might receive
- a function that filters what content should be mapped
- - Other targets can use those filters on their own code. In order to do that,
- a simple configuration step is needed: you need to initialize a generator_ctx type with
- Gencommon.new_gen (context:Common.context)
- with a generator_ctx context you will be able to add filters to your code, and execute them with
- Gencommon.run_filters (gen_context:Gencommon.generator_ctx)
- After running the filters, you can run your own generator normally.
- (* , or you can run
- Gencommon.generate_modules (gen_context:Gencommon.generator_ctx) (extension:string) (module_gen:module_type list->bool)
- where module_gen will take a whole module (can be *)
- *)
- (* ******************************************* *)
- (* common helpers *)
- (* ******************************************* *)
- let assertions = false (* when assertions == true, many assertions will be made to guarantee the quality of the data input *)
- let debug_mode = ref false
- let trace s = if !debug_mode then print_endline s else ()
- let timer name = if !debug_mode then Common.timer name else fun () -> ()
- let is_string t = match follow t with | TInst({ cl_path = ([], "String") }, []) -> true | _ -> false
- (* helper function for creating Anon types of class / enum modules *)
- let anon_of_classtype cl =
- TAnon {
- a_fields = cl.cl_statics;
- a_status = ref (Statics cl)
- }
- let anon_of_enum e =
- TAnon {
- a_fields = PMap.empty;
- a_status = ref (EnumStatics e)
- }
- let anon_of_abstract a =
- TAnon {
- a_fields = PMap.empty;
- a_status = ref (AbstractStatics a)
- }
- let anon_of_mt mt = match mt with
- | TClassDecl cl -> anon_of_classtype cl
- | TEnumDecl e -> anon_of_enum e
- | TAbstractDecl a -> anon_of_abstract a
- | _ -> assert false
- let anon_class t =
- match follow t with
- | TAnon anon ->
- (match !(anon.a_status) with
- | Statics (cl) -> Some(TClassDecl(cl))
- | EnumStatics (e) -> Some(TEnumDecl(e))
- | AbstractStatics (a) -> Some(TAbstractDecl(a))
- | _ -> None)
- | _ -> None
- let path_s path =
- match path with | ([], s) -> s | (p, s) -> (String.concat "." (fst path)) ^ "." ^ (snd path)
- let rec t_to_md t = match t with
- | TInst (cl,_) -> TClassDecl cl
- | TEnum (e,_) -> TEnumDecl e
- | TType (t,_) -> TTypeDecl t
- | TAbstract (a,_) -> TAbstractDecl a
- | TAnon anon ->
- (match !(anon.a_status) with
- | EnumStatics e -> TEnumDecl e
- | Statics cl -> TClassDecl cl
- | AbstractStatics a -> TAbstractDecl a
- | _ -> assert false)
- | TLazy f -> t_to_md (!f())
- | TMono r -> (match !r with | Some t -> t_to_md t | None -> assert false)
- | _ -> assert false
- let get_cl mt = match mt with | TClassDecl cl -> cl | _ -> failwith ("Unexpected module type of '" ^ path_s (t_path mt) ^ "'")
- let get_abstract mt = match mt with | TAbstractDecl a -> a | _ -> failwith ("Unexpected module type of '" ^ path_s (t_path mt) ^ "'")
- let get_tdef mt = match mt with | TTypeDecl t -> t | _ -> assert false
- let mk_mt_access mt pos = { eexpr = TTypeExpr(mt); etype = anon_of_mt mt; epos = pos }
- let is_void t = match follow t with
- | TAbstract ({ a_path = ([], "Void") },[]) ->
- true
- | _ -> false
- let mk_local var pos = { eexpr = TLocal(var); etype = var.v_type; epos = pos }
- (* this function is used by CastDetection module *)
- let get_fun t =
- match follow t with | TFun(r1,r2) -> (r1,r2) | _ -> (trace (s_type (print_context()) (follow t) )); assert false
- let mk_cast t e =
- { eexpr = TCast(e, None); etype = t; epos = e.epos }
- let mk_classtype_access cl pos =
- { eexpr = TTypeExpr(TClassDecl(cl)); etype = anon_of_classtype cl; epos = pos }
- let mk_static_field_access_infer cl field pos params =
- try
- let cf = (PMap.find field cl.cl_statics) in
- { eexpr = TField(mk_classtype_access cl pos, FStatic(cl, cf)); etype = (if params = [] then cf.cf_type else apply_params cf.cf_params params cf.cf_type); epos = pos }
- with | Not_found -> failwith ("Cannot find field " ^ field ^ " in type " ^ (path_s cl.cl_path))
- let mk_static_field_access cl field fieldt pos =
- { (mk_static_field_access_infer cl field pos []) with etype = fieldt }
- (* stolen from Hugh's sources ;-) *)
- (* this used to be a class, but there was something in there that crashed ocaml native compiler in windows *)
- module SourceWriter =
- struct
- type source_writer =
- {
- sw_buf : Buffer.t;
- mutable sw_has_content : bool;
- mutable sw_indent : string;
- mutable sw_indents : string list;
- }
- let new_source_writer () =
- {
- sw_buf = Buffer.create 0;
- sw_has_content = false;
- sw_indent = "";
- sw_indents = [];
- }
- let add_writer w_write w_read = Buffer.add_buffer w_read.sw_buf w_write.sw_buf
- let contents w = Buffer.contents w.sw_buf
- let len w = Buffer.length w.sw_buf
- let write w x =
- (if not w.sw_has_content then begin w.sw_has_content <- true; Buffer.add_string w.sw_buf w.sw_indent; Buffer.add_string w.sw_buf x; end else Buffer.add_string w.sw_buf x);
- let len = (String.length x)-1 in
- if len >= 0 && String.get x len = '\n' then begin w.sw_has_content <- false end else w.sw_has_content <- true
- let push_indent w = w.sw_indents <- "\t"::w.sw_indents; w.sw_indent <- String.concat "" w.sw_indents
- let pop_indent w =
- match w.sw_indents with
- | h::tail -> w.sw_indents <- tail; w.sw_indent <- String.concat "" w.sw_indents
- | [] -> w.sw_indent <- "/*?*/"
- let newline w = write w "\n"
- let begin_block w = (if w.sw_has_content then newline w); write w "{"; push_indent w; newline w
- let end_block w = pop_indent w; (if w.sw_has_content then newline w); write w "}"; newline w
- let print w =
- (if not w.sw_has_content then begin w.sw_has_content <- true; Buffer.add_string w.sw_buf w.sw_indent end);
- bprintf w.sw_buf;
- end;;
- (* rule_dispatcher's priority *)
- type priority =
- | PFirst
- | PLast
- | PZero
- | PCustom of float
- exception DuplicateName of string
- exception NoRulesApplied
- let indent = ref []
- (* the rule dispatcher is the primary way to deal with distributed "plugins" *)
- (* we will define rules that will form a distributed / extensible match system *)
- class ['tp, 'ret] rule_dispatcher name ignore_not_found =
- object(self)
- val tbl = Hashtbl.create 16
- val mutable keys = []
- val names = Hashtbl.create 16
- val mutable temp = 0
- method add ?(name : string option) (* name helps debugging *) ?(priority : priority = PZero) (rule : 'tp->'ret option) =
- let p = match priority with
- | PFirst -> infinity
- | PLast -> neg_infinity
- | PZero -> 0.0
- | PCustom i -> i
- in
- let q = if not( Hashtbl.mem tbl p ) then begin
- let q = Stack.create() in
- Hashtbl.add tbl p q;
- keys <- p :: keys;
- keys <- List.sort (fun x y -> - (compare x y)) keys;
- q
- end else Hashtbl.find tbl p in
- let name = match name with
- | None -> temp <- temp + 1; "$_" ^ (string_of_int temp)
- | Some s -> s
- in
- (if Hashtbl.mem names name then raise (DuplicateName(name)));
- Hashtbl.add names name q;
- Stack.push (name, rule) q
- method describe =
- Hashtbl.iter (fun s _ -> (trace s)) names;
- method remove (name : string) =
- if Hashtbl.mem names name then begin
- let q = Hashtbl.find names name in
- let q_temp = Stack.create () in
- Stack.iter (function
- | (n, _) when n = name -> ()
- | _ as r -> Stack.push r q_temp
- ) q;
- Stack.clear q;
- Stack.iter (fun r -> Stack.push r q) q_temp;
- Hashtbl.remove names name;
- true
- end else false
- method run_f tp = get (self#run tp)
- method did_run tp = is_some (self#run tp)
- method get_list =
- let ret = ref [] in
- List.iter (fun key ->
- let q = Hashtbl.find tbl key in
- Stack.iter (fun (_, rule) -> ret := rule :: !ret) q
- ) keys;
- List.rev !ret
- method run_from (priority:float) (tp:'tp) : 'ret option =
- let ok = ref ignore_not_found in
- let ret = ref None in
- indent := "\t" :: !indent;
- (try begin
- List.iter (fun key ->
- if key < priority then begin
- let q = Hashtbl.find tbl key in
- Stack.iter (fun (n, rule) ->
- let t = if !debug_mode then Common.timer ("rule dispatcher rule: " ^ n) else fun () -> () in
- let r = rule(tp) in
- t();
- if is_some r then begin ret := r; raise Exit end
- ) q
- end
- ) keys
- end with Exit -> ok := true);
- (match !indent with
- | [] -> ()
- | h::t -> indent := t);
- (if not (!ok) then raise NoRulesApplied);
- !ret
- method run (tp:'tp) : 'ret option =
- self#run_from infinity tp
- end;;
- (* this is a special case where tp = tret and you stack their output as the next's input *)
- class ['tp] rule_map_dispatcher name =
- object(self)
- inherit ['tp, 'tp] rule_dispatcher name true as super
- method run_f tp = get (self#run tp)
- method run_from (priority:float) (tp:'tp) : 'ret option =
- let cur = ref tp in
- (try begin
- List.iter (fun key ->
- if key < priority then begin
- let q = Hashtbl.find tbl key in
- Stack.iter (fun (n, rule) ->
- trace ("running rule " ^ n);
- let t = if !debug_mode then Common.timer ("rule map dispatcher rule: " ^ n) else fun () -> () in
- let r = rule(!cur) in
- t();
- if is_some r then begin cur := get r end
- ) q
- end
- ) keys
- end with Exit -> ());
- Some (!cur)
- end;;
- type generator_ctx =
- {
- (* these are the basic context fields. If another target is using this context, *)
- (* this is all you need to care about *)
- mutable gcon : Common.context;
- gclasses : gen_classes;
- gtools : gen_tools;
- (*
- configurable function that receives a desired name and makes it "internal", doing the best
- to ensure that it will not be called from outside.
- To avoid name clashes between internal names, user must specify two strings: a "namespace" and the name itself
- *)
- mutable gmk_internal_name : string->string->string;
- (*
- module filters run before module filters and they should generate valid haxe syntax as a result.
- Module filters shouldn't go through the expressions as it adds an unnecessary burden to the GC,
- and it can all be done in a single step with gexpr_filters and proper priority selection.
- As a convention, Module filters should end their name with Modf, so they aren't mistaken with expression filters
- *)
- gmodule_filters : (module_type) rule_map_dispatcher;
- (*
- expression filters are the most common filters to be applied.
- They should also generate only valid haxe expressions, so e.g. calls to non-existant methods
- should be avoided, although there are some ways around them (like gspecial_methods)
- *)
- gexpr_filters : (texpr) rule_map_dispatcher;
- (*
- syntax filters are also expression filters but they no longer require
- that the resulting expressions be valid haxe expressions.
- They then have no guarantee that either the input expressions or the output one follow the same
- rules as normal haxe code.
- *)
- gsyntax_filters : (texpr) rule_map_dispatcher;
- (* these are more advanced features, but they would require a rewrite of targets *)
- (* they are just helpers to ditribute functions like "follow" or "type to string" *)
- (* so adding a module will already take care of correctly following a certain type of *)
- (* variable, for example *)
- (* follows the type through typedefs, lazy typing, etc. *)
- (* it's the place to put specific rules to handle typedefs, like *)
- (* other basic types like UInt *)
- gfollow : (t, t) rule_dispatcher;
- gtypes : (path, module_type) Hashtbl.t;
- (* cast detection helpers / settings *)
- (* this is a cache for all field access types *)
- greal_field_types : (path * string, (tclass_field (* does the cf exist *) * t (*cf's type in relation to current class type params *) * t * tclass (* declared class *) ) option) Hashtbl.t;
- (* this function allows any code to handle casts as if it were inside the cast_detect module *)
- mutable ghandle_cast : t->t->texpr->texpr;
- (* when an unsafe cast is made, we can warn the user *)
- mutable gon_unsafe_cast : t->t->pos->unit;
- (* does this type needs to be boxed? Normally always false, unless special type handling must be made *)
- mutable gneeds_box : t->bool;
- (* does this 'special type' needs cast to this other type? *)
- (* this is here so we can implement custom behavior for "opaque" typedefs *)
- mutable gspecial_needs_cast : t->t->bool;
- (* sometimes we may want to support unrelated conversions on cast detection *)
- (* for example, haxe.lang.Null<T> -> T on C# *)
- (* every time an unrelated conversion is found, each to/from path is searched on this hashtbl *)
- (* if found, the function will be executed with from_type, to_type. If returns true, it means that *)
- (* it is a supported conversion, and the unsafe cast routine changes to a simple cast *)
- gsupported_conversions : (path, t->t->bool) Hashtbl.t;
- (* API for filters *)
- (* add type can be called at any time, and will add a new module_def that may or may not be filtered *)
- (* module_type -> should_filter *)
- mutable gadd_type : module_type -> bool -> unit;
- (* during expr filters, add_to_module will be available so module_types can be added to current module_def. we must pass the priority argument so the filters can be resumed *)
- mutable gadd_to_module : module_type -> float -> unit;
- (* during expr filters, shows the current class path *)
- mutable gcurrent_path : path;
- (* current class *)
- mutable gcurrent_class : tclass option;
- (* current class field, if any *)
- mutable gcurrent_classfield : tclass_field option;
- (* events *)
- (* is executed once every new classfield *)
- mutable gon_classfield_start : (unit -> unit) list;
- (* is executed once every new module type *)
- mutable gon_new_module_type : (unit -> unit) list;
- (* after module filters ended *)
- mutable gafter_mod_filters_ended : (unit -> unit) list;
- (* after expression filters ended *)
- mutable gafter_expr_filters_ended : (unit -> unit) list;
- (* after all filters are run *)
- mutable gafter_filters_ended : (unit -> unit) list;
- mutable gbase_class_fields : (string, tclass_field) PMap.t;
- (* real type is the type as it is read by the target. *)
- (* This function is here because most targets don't have *)
- (* a 1:1 translation between haxe types and its native types *)
- (* But types aren't changed to this representation as we might lose *)
- (* some valuable type information in the process *)
- mutable greal_type : t -> t;
- (*
- the same as greal_type but for type parameters.
- *)
- mutable greal_type_param : module_type -> tparams -> tparams;
- (*
- is the type a value type?
- This may be used in some optimizations where reference types and value types
- are handled differently. At first the default is very good to use, and if tweaks are needed,
- it's best to be done by adding @:struct meta to the value types
- *
- mutable gis_value_type : t -> bool;*)
- (* misc configuration *)
- (*
- Should the target allow type parameter dynamic conversion,
- or should we add a cast to those cases as well?
- *)
- mutable gallow_tp_dynamic_conversion : bool;
- (*
- Does the target support type parameter constraints?
- If not, they will be ignored when detecting casts
- *)
- mutable guse_tp_constraints : bool;
- (* internal apis *)
- (* param_func_call : used by TypeParams and CastDetection *)
- mutable gparam_func_call : texpr->texpr->tparams->texpr list->texpr;
- (* does it already have a type parameter cast handler? This is used by CastDetect to know if it should handle type parameter casts *)
- mutable ghas_tparam_cast_handler : bool;
- (* type parameter casts - special cases *)
- (* function cast_from, cast_to -> texpr *)
- gtparam_cast : (path, (texpr->t->texpr)) Hashtbl.t;
- (*
- special vars are used for adding special behavior to
- *)
- gspecial_vars : (string, bool) Hashtbl.t;
- }
- and gen_classes =
- {
- cl_reflect : tclass;
- cl_type : tclass;
- cl_dyn : tclass;
- t_iterator : tdef;
- mutable nativearray_len : texpr -> pos -> texpr;
- mutable nativearray_type : Type.t -> Type.t;
- mutable nativearray : Type.t -> Type.t;
- }
- (* add here all reflection transformation additions *)
- and gen_tools =
- {
- (* (klass : texpr, t : t) : texpr *)
- mutable r_create_empty : texpr->t->texpr;
- (* Reflect.fields(). The bool is if we are iterating in a read-only manner. If it is read-only we might not need to allocate a new array *)
- mutable r_fields : bool->texpr->texpr;
- (* (first argument = return type. should be void in most cases) Reflect.setField(obj, field, val) *)
- mutable r_set_field : t->texpr->texpr->texpr->texpr;
- (* Reflect.field. bool indicates if is safe (no error throwing) or unsafe; t is the expected return type true = safe *)
- mutable r_field : bool->t->texpr->texpr->texpr;
- (*
- these are now the functions that will later be used when creating the reflection classes
- *)
- (* on the default implementation (at OverloadingCtors), it will be new SomeClass<params>(EmptyInstance) *)
- mutable rf_create_empty : tclass->tparams->pos->texpr;
- }
- let get_type types path =
- List.find (fun md -> match md with
- | TClassDecl cl when cl.cl_path = path -> true
- | TEnumDecl e when e.e_path = path -> true
- | TTypeDecl t when t.t_path = path -> true
- | TAbstractDecl a when a.a_path = path -> true
- | _ -> false
- ) types
- let new_ctx con =
- let types = Hashtbl.create (List.length con.types) in
- List.iter (fun mt ->
- match mt with
- | TClassDecl cl -> Hashtbl.add types cl.cl_path mt
- | TEnumDecl e -> Hashtbl.add types e.e_path mt
- | TTypeDecl t -> Hashtbl.add types t.t_path mt
- | TAbstractDecl a -> Hashtbl.add types a.a_path mt
- ) con.types;
- let cl_dyn = match get_type con.types ([], "Dynamic") with
- | TClassDecl c -> c
- | TAbstractDecl a ->
- mk_class a.a_module ([], "Dynamic") a.a_pos
- | _ -> assert false
- in
- let rec gen = {
- gcon = con;
- gclasses = {
- cl_reflect = get_cl (get_type con.types ([], "Reflect"));
- cl_type = get_cl (get_type con.types ([], "Type"));
- cl_dyn = cl_dyn;
- t_iterator = get_tdef (get_type con.types ([], "Iterator"));
- nativearray = (fun _ -> assert false);
- nativearray_type = (fun _ -> assert false);
- nativearray_len = (fun _ -> assert false);
- };
- gtools = {
- r_create_empty = (fun eclass t ->
- let fieldcall = mk_static_field_access_infer gen.gclasses.cl_type "createEmptyInstance" eclass.epos [t] in
- { eexpr = TCall(fieldcall, [eclass]); etype = t; epos = eclass.epos }
- );
- r_fields = (fun is_used_only_by_iteration expr ->
- let fieldcall = mk_static_field_access_infer gen.gclasses.cl_reflect "fields" expr.epos [] in
- { eexpr = TCall(fieldcall, [expr]); etype = gen.gcon.basic.tarray gen.gcon.basic.tstring; epos = expr.epos }
- );
- (* Reflect.setField(obj, field, val). t by now is ignored. FIXME : fix this implementation *)
- r_set_field = (fun t obj field v ->
- let fieldcall = mk_static_field_access_infer gen.gclasses.cl_reflect "setField" v.epos [] in
- { eexpr = TCall(fieldcall, [obj; field; v]); etype = t_dynamic; epos = v.epos }
- );
- (* Reflect.field. bool indicates if is safe (no error throwing) or unsafe. true = safe *)
- r_field = (fun is_safe t obj field ->
- let fieldcall = mk_static_field_access_infer gen.gclasses.cl_reflect "field" obj.epos [] in
- (* FIXME: should we see if needs to cast? *)
- mk_cast t { eexpr = TCall(fieldcall, [obj; field]); etype = t_dynamic; epos = obj.epos }
- );
- rf_create_empty = (fun cl p pos ->
- gen.gtools.r_create_empty { eexpr = TTypeExpr(TClassDecl cl); epos = pos; etype = t_dynamic } (TInst(cl,p))
- ); (* TODO: Maybe implement using normal reflection? Type.createEmpty(MyClass) *)
- };
- gmk_internal_name = (fun ns s -> sprintf "__%s_%s" ns s);
- gexpr_filters = new rule_map_dispatcher "gexpr_filters";
- gmodule_filters = new rule_map_dispatcher "gmodule_filters";
- gsyntax_filters = new rule_map_dispatcher "gsyntax_filters";
- gfollow = new rule_dispatcher "gfollow" false;
- gtypes = types;
- greal_field_types = Hashtbl.create 0;
- ghandle_cast = (fun to_t from_t e -> mk_cast to_t e);
- gon_unsafe_cast = (fun t t2 pos -> (gen.gcon.warning ("Type " ^ (debug_type t2) ^ " is being cast to the unrelated type " ^ (s_type (print_context()) t)) pos));
- gneeds_box = (fun t -> false);
- gspecial_needs_cast = (fun to_t from_t -> true);
- gsupported_conversions = Hashtbl.create 0;
- gadd_type = (fun md should_filter ->
- if should_filter then begin
- con.types <- md :: con.types;
- con.modules <- { m_id = alloc_mid(); m_path = (t_path md); m_types = [md]; m_extra = module_extra "" "" 0. MFake } :: con.modules
- end else gen.gafter_filters_ended <- (fun () ->
- con.types <- md :: con.types;
- con.modules <- { m_id = alloc_mid(); m_path = (t_path md); m_types = [md]; m_extra = module_extra "" "" 0. MFake } :: con.modules
- ) :: gen.gafter_filters_ended;
- );
- gadd_to_module = (fun md pr -> failwith "module added outside expr filters");
- gcurrent_path = ([],"");
- gcurrent_class = None;
- gcurrent_classfield = None;
- gon_classfield_start = [];
- gon_new_module_type = [];
- gafter_mod_filters_ended = [];
- gafter_expr_filters_ended = [];
- gafter_filters_ended = [];
- gbase_class_fields = PMap.empty;
- greal_type = (fun t -> t);
- greal_type_param = (fun _ t -> t);
- gallow_tp_dynamic_conversion = false;
- guse_tp_constraints = false;
- (* as a default, ignore the params *)
- gparam_func_call = (fun ecall efield params elist -> { ecall with eexpr = TCall(efield, elist) });
- ghas_tparam_cast_handler = false;
- gtparam_cast = Hashtbl.create 0;
- gspecial_vars = Hashtbl.create 0;
- } in
- (*gen.gtools.r_create_empty <-
- gen.gtools.r_get_class <-
- gen.gtools.r_fields <- *)
- gen
- let init_ctx gen =
- (* ultimately add a follow once handler as the last follow handler *)
- let follow_f = gen.gfollow#run in
- let follow t =
- match t with
- | TMono r ->
- (match !r with
- | Some t -> follow_f t
- | _ -> Some t)
- | TLazy f ->
- follow_f (!f())
- | TType (t,tl) ->
- follow_f (apply_params t.t_params tl t.t_type)
- | _ -> Some t
- in
- gen.gfollow#add ~name:"final" ~priority:PLast follow
- (* run_follow (gen:generator_ctx) (t:t) *)
- let run_follow gen = gen.gfollow#run_f
- let reorder_modules gen =
- let modules = Hashtbl.create 20 in
- List.iter (fun md ->
- Hashtbl.add modules ( (t_infos md).mt_module ).m_path md
- ) gen.gcon.types;
- let con = gen.gcon in
- con.modules <- [];
- let processed = Hashtbl.create 20 in
- Hashtbl.iter (fun md_path md ->
- if not (Hashtbl.mem processed md_path) then begin
- Hashtbl.add processed md_path true;
- con.modules <- { m_id = alloc_mid(); m_path = md_path; m_types = List.rev ( Hashtbl.find_all modules md_path ); m_extra = (t_infos md).mt_module.m_extra } :: con.modules
- end
- ) modules
- let run_filters_from gen t filters =
- match t with
- | TClassDecl c ->
- trace (snd c.cl_path);
- gen.gcurrent_path <- c.cl_path;
- gen.gcurrent_class <- Some(c);
- List.iter (fun fn -> fn()) gen.gon_new_module_type;
- gen.gcurrent_classfield <- None;
- let rec process_field f =
- gen.gcurrent_classfield <- Some(f);
- List.iter (fun fn -> fn()) gen.gon_classfield_start;
- trace f.cf_name;
- (match f.cf_expr with
- | None -> ()
- | Some e ->
- f.cf_expr <- Some (List.fold_left (fun e f -> f e) e filters));
- List.iter process_field f.cf_overloads;
- in
- List.iter process_field c.cl_ordered_fields;
- List.iter process_field c.cl_ordered_statics;
- gen.gcurrent_classfield <- None;
- (match c.cl_constructor with
- | None -> ()
- | Some f -> process_field f);
- (match c.cl_init with
- | None -> ()
- | Some e ->
- c.cl_init <- Some (List.fold_left (fun e f -> f e) e filters));
- | TEnumDecl _ -> ()
- | TTypeDecl _ -> ()
- | TAbstractDecl _ -> ()
- let run_filters gen =
- let last_error = gen.gcon.error in
- let has_errors = ref false in
- gen.gcon.error <- (fun msg pos -> has_errors := true; last_error msg pos);
- (* first of all, we have to make sure that the filters won't trigger a major Gc collection *)
- let t = Common.timer "gencommon_filters" in
- (if Common.defined gen.gcon Define.GencommonDebug then debug_mode := true);
- let run_filters filter =
- let rec loop acc mds =
- match mds with
- | [] -> acc
- | md :: tl ->
- let filters = [ filter#run_f ] in
- let added_types = ref [] in
- gen.gadd_to_module <- (fun md_type priority ->
- gen.gcon.types <- md_type :: gen.gcon.types;
- added_types := (md_type, priority) :: !added_types
- );
- run_filters_from gen md filters;
- let added_types = List.map (fun (t,p) ->
- run_filters_from gen t [ fun e -> get (filter#run_from p e) ];
- if Hashtbl.mem gen.gtypes (t_path t) then begin
- let rec loop i =
- let p = t_path t in
- let new_p = (fst p, snd p ^ "_" ^ (string_of_int i)) in
- if Hashtbl.mem gen.gtypes new_p then
- loop (i+1)
- else
- match t with
- | TClassDecl cl -> cl.cl_path <- new_p
- | TEnumDecl e -> e.e_path <- new_p
- | TTypeDecl _ | TAbstractDecl _ -> ()
- in
- loop 0
- end;
- Hashtbl.add gen.gtypes (t_path t) t;
- t
- ) !added_types in
- loop (added_types @ (md :: acc)) tl
- in
- List.rev (loop [] gen.gcon.types)
- in
- let run_mod_filter filter =
- let last_add_to_module = gen.gadd_to_module in
- let added_types = ref [] in
- gen.gadd_to_module <- (fun md_type priority ->
- Hashtbl.add gen.gtypes (t_path md_type) md_type;
- added_types := (md_type, priority) :: !added_types
- );
- let rec loop processed not_processed =
- match not_processed with
- | hd :: tl ->
- (match hd with
- | TClassDecl c ->
- gen.gcurrent_class <- Some c
- | _ ->
- gen.gcurrent_class <- None);
- let new_hd = filter#run_f hd in
- let added_types_new = !added_types in
- added_types := [];
- let added_types = List.map (fun (t,p) ->
- get (filter#run_from p t)
- ) added_types_new in
- loop ( added_types @ (new_hd :: processed) ) tl
- | [] ->
- processed
- in
- let filtered = loop [] gen.gcon.types in
- gen.gadd_to_module <- last_add_to_module;
- gen.gcon.types <- List.rev (filtered)
- in
- run_mod_filter gen.gmodule_filters;
- List.iter (fun fn -> fn()) gen.gafter_mod_filters_ended;
- let last_add_to_module = gen.gadd_to_module in
- gen.gcon.types <- run_filters gen.gexpr_filters;
- gen.gadd_to_module <- last_add_to_module;
- List.iter (fun fn -> fn()) gen.gafter_expr_filters_ended;
- (* Codegen.post_process gen.gcon.types [gen.gexpr_filters#run_f]; *)
- gen.gcon.types <- run_filters gen.gsyntax_filters;
- List.iter (fun fn -> fn()) gen.gafter_filters_ended;
- reorder_modules gen;
- t();
- if !has_errors then raise (Abort("Compilation aborted with errors",null_pos))
- (* ******************************************* *)
- (* basic generation module that source code compilation implementations can use *)
- (* ******************************************* *)
- let write_file gen w source_dir path extension out_files =
- let t = timer "write file" in
- let s_path = source_dir ^ "/" ^ (snd path) ^ "." ^ (extension) in
- (* create the folders if they don't exist *)
- mkdir_from_path s_path;
- let contents = SourceWriter.contents w in
- let should_write = if not (Common.defined gen.gcon Define.ReplaceFiles) && Sys.file_exists s_path then begin
- let in_file = open_in s_path in
- let old_contents = Std.input_all in_file in
- close_in in_file;
- contents <> old_contents
- end else true in
- if should_write then begin
- let f = open_out s_path in
- output_string f contents;
- close_out f
- end;
- out_files := (unique_full_path s_path) :: !out_files;
- t()
- let clean_files path excludes verbose =
- let rec iter_files pack dir path = try
- let file = Unix.readdir dir in
- if file <> "." && file <> ".." then begin
- let filepath = path ^ "/" ^ file in
- if (Unix.stat filepath).st_kind = S_DIR then
- let pack = pack @ [file] in
- iter_files (pack) (Unix.opendir filepath) filepath;
- try Unix.rmdir filepath with Unix.Unix_error (ENOTEMPTY,_,_) -> ();
- else if not (String.ends_with filepath ".meta") && not (List.mem (unique_full_path filepath) excludes) then begin
- if verbose then print_endline ("Removing " ^ filepath);
- Sys.remove filepath
- end
- end;
- iter_files pack dir path
- with | End_of_file | Unix.Unix_error _ ->
- Unix.closedir dir
- in
- iter_files [] (Unix.opendir path) path
- let dump_descriptor gen name path_s module_s =
- let w = SourceWriter.new_source_writer () in
- (* dump called path *)
- SourceWriter.write w (Sys.getcwd());
- SourceWriter.newline w;
- (* dump all defines. deprecated *)
- SourceWriter.write w "begin defines";
- SourceWriter.newline w;
- PMap.iter (fun name _ ->
- SourceWriter.write w name;
- SourceWriter.newline w
- ) gen.gcon.defines;
- SourceWriter.write w "end defines";
- SourceWriter.newline w;
- (* dump all defines with their values; keeping the old defines for compatibility *)
- SourceWriter.write w "begin defines_data";
- SourceWriter.newline w;
- PMap.iter (fun name v ->
- SourceWriter.write w name;
- SourceWriter.write w "=";
- SourceWriter.write w v;
- SourceWriter.newline w
- ) gen.gcon.defines;
- SourceWriter.write w "end defines_data";
- SourceWriter.newline w;
- (* dump all generated types *)
- SourceWriter.write w "begin modules";
- SourceWriter.newline w;
- let main_paths = Hashtbl.create 0 in
- List.iter (fun md_def ->
- SourceWriter.write w "M ";
- SourceWriter.write w (path_s (path_of_md_def md_def));
- SourceWriter.newline w;
- List.iter (fun m ->
- match m with
- | TClassDecl cl when not cl.cl_extern ->
- SourceWriter.write w "C ";
- let s = module_s m in
- Hashtbl.add main_paths cl.cl_path s;
- SourceWriter.write w (s);
- SourceWriter.newline w
- | TEnumDecl e when not e.e_extern ->
- SourceWriter.write w "E ";
- SourceWriter.write w (module_s m);
- SourceWriter.newline w
- | _ -> () (* still no typedef or abstract is generated *)
- ) md_def.m_types
- ) gen.gcon.modules;
- SourceWriter.write w "end modules";
- SourceWriter.newline w;
- (* dump all resources *)
- (match gen.gcon.main_class with
- | Some path ->
- SourceWriter.write w "begin main";
- SourceWriter.newline w;
- (try
- SourceWriter.write w (Hashtbl.find main_paths path)
- with
- | Not_found -> SourceWriter.write w (path_s path));
- SourceWriter.newline w;
- SourceWriter.write w "end main";
- SourceWriter.newline w
- | _ -> ()
- );
- SourceWriter.write w "begin resources";
- SourceWriter.newline w;
- Hashtbl.iter (fun name _ ->
- SourceWriter.write w name;
- SourceWriter.newline w
- ) gen.gcon.resources;
- SourceWriter.write w "end resources";
- SourceWriter.newline w;
- SourceWriter.write w "begin libs";
- SourceWriter.newline w;
- let path file ext =
- if Sys.file_exists file then
- file
- else try Common.find_file gen.gcon file with
- | Not_found -> try Common.find_file gen.gcon (file ^ ext) with
- | Not_found ->
- file
- in
- if Common.platform gen.gcon Java then
- List.iter (fun (s,std,_,_,_) ->
- if not std then begin
- SourceWriter.write w (path s ".jar");
- SourceWriter.newline w;
- end
- ) gen.gcon.java_libs
- else if Common.platform gen.gcon Cs then
- List.iter (fun (s,std,_,_) ->
- if not std then begin
- SourceWriter.write w (path s ".dll");
- SourceWriter.newline w;
- end
- ) gen.gcon.net_libs;
- SourceWriter.write w "end libs";
- SourceWriter.newline w;
- let args = gen.gcon.c_args in
- if args <> [] then begin
- SourceWriter.write w "begin opts";
- SourceWriter.newline w;
- List.iter (fun opt -> SourceWriter.write w opt; SourceWriter.newline w) (List.rev args);
- SourceWriter.write w "end opts";
- SourceWriter.newline w;
- end;
- let contents = SourceWriter.contents w in
- let f = open_out (gen.gcon.file ^ "/" ^ name) in
- output_string f contents;
- close_out f
- let path_regex = Str.regexp "[/\\]+"
- let normalize path =
- let rec normalize acc m = match m with
- | [] ->
- List.rev acc
- | Str.Text "." :: Str.Delim _ :: tl when acc = [] ->
- normalize [] tl
- | Str.Text ".." :: Str.Delim _ :: tl -> (match acc with
- | [] -> raise Exit
- | _ :: acc -> normalize acc tl)
- | Str.Text t :: Str.Delim _ :: tl ->
- normalize (t :: acc) tl
- | Str.Delim _ :: tl ->
- normalize ("" :: acc) tl
- | Str.Text t :: [] ->
- List.rev (t :: acc)
- | Str.Text _ :: Str.Text _ :: _ -> assert false
- in
- String.concat "/" (normalize [] (Str.full_split path_regex path))
- let is_relative cwd rel =
- try
- let rel = normalize rel in
- Filename.is_relative rel || (String.starts_with rel cwd || String.starts_with (Common.unique_full_path rel) cwd)
- with | Exit ->
- String.starts_with rel cwd || String.starts_with (Common.unique_full_path rel) cwd
- (*
- helper function to create the source structure. Will send each module_def to the function passed.
- If received true, it means that module_gen has generated this content, so the file must be saved.
- See that it will write a whole module
- *)
- let generate_modules gen extension source_dir (module_gen : SourceWriter.source_writer->module_def->bool) out_files =
- let cwd = Common.unique_full_path (Sys.getcwd()) in
- List.iter (fun md_def ->
- let source_dir =
- if Common.defined gen.gcon Define.UnityStdTarget then
- let file = md_def.m_extra.m_file in
- let file = if file = "" then "." else file in
- if is_relative cwd file then
- let base_path = try
- let last = Str.search_backward path_regex file (String.length file - 1) in
- String.sub file 0 last
- with | Not_found ->
- "."
- in
- match List.rev (fst md_def.m_path) with
- | "editor" :: _ ->
- base_path ^ "/" ^ gen.gcon.file ^ "/Editor"
- | _ ->
- base_path ^ "/" ^ gen.gcon.file
- else match List.rev (fst md_def.m_path) with
- | "editor" :: _ ->
- Common.defined_value gen.gcon Define.UnityStdTarget ^ "/Editor/" ^ (String.concat "/" (fst md_def.m_path))
- | _ ->
- Common.defined_value gen.gcon Define.UnityStdTarget ^ "/Haxe-Std/" ^ (String.concat "/" (fst md_def.m_path))
- else
- gen.gcon.file ^ "/" ^ source_dir ^ "/" ^ (String.concat "/" (fst (path_of_md_def md_def)))
- in
- let w = SourceWriter.new_source_writer () in
- (*let should_write = List.fold_left (fun should md -> module_gen w md or should) false md_def.m_types in*)
- let should_write = module_gen w md_def in
- if should_write then begin
- let path = path_of_md_def md_def in
- write_file gen w source_dir path extension out_files
- end
- ) gen.gcon.modules
- let generate_modules_t gen extension source_dir change_path (module_gen : SourceWriter.source_writer->module_type->bool) out_files =
- let source_dir = gen.gcon.file ^ "/" ^ source_dir in
- List.iter (fun md ->
- let w = SourceWriter.new_source_writer () in
- (*let should_write = List.fold_left (fun should md -> module_gen w md or should) false md_def.m_types in*)
- let should_write = module_gen w md in
- if should_write then begin
- let path = change_path (t_path md) in
- write_file gen w (source_dir ^ "/" ^ (String.concat "/" (fst path))) path extension out_files;
- end
- ) gen.gcon.types
- (*
- various helper functions
- *)
- let mk_paren e =
- match e.eexpr with | TParenthesis _ -> e | _ -> { e with eexpr=TParenthesis(e) }
- (* private *)
- let tmp_count = ref 0
- let get_real_fun gen t =
- match follow t with
- | TFun(args,t) -> TFun(List.map (fun (n,o,t) -> n,o,gen.greal_type t) args, gen.greal_type t)
- | _ -> t
- let mk_int gen i pos = { eexpr = TConst(TInt ( Int32.of_int i)); etype = gen.gcon.basic.tint; epos = pos }
- let mk_return e = { eexpr = TReturn (Some e); etype = e.etype; epos = e.epos }
- let mk_temp gen name t =
- incr tmp_count;
- let name = gen.gmk_internal_name "temp" (name ^ (string_of_int !tmp_count)) in
- alloc_var name t
- let v_nativearray = alloc_var "__array__" t_dynamic
- let mk_nativearray_decl gen t el pos =
- {
- eexpr = TCall(mk_local v_nativearray pos, el);
- etype = gen.gclasses.nativearray t;
- epos = pos;
- }
- let ensure_local gen block name e =
- match e.eexpr with
- | TLocal _ -> e
- | _ ->
- let var = mk_temp gen name e.etype in
- block := { e with eexpr = TVar(var, Some e); etype = gen.gcon.basic.tvoid; } :: !block;
- { e with eexpr = TLocal var }
- let reset_temps () = tmp_count := 0
- let follow_module follow_func md = match md with
- | TClassDecl _
- | TEnumDecl _
- | TAbstractDecl _ -> md
- | TTypeDecl tdecl -> match (follow_func (TType(tdecl, List.map snd tdecl.t_params))) with
- | TInst(cl,_) -> TClassDecl cl
- | TEnum(e,_) -> TEnumDecl e
- | TType(t,_) -> TTypeDecl t
- | TAbstract(a,_) -> TAbstractDecl a
- | _ -> assert false
- (*
- hxgen means if the type was generated by haxe. If a type was generated by haxe, it means
- it will contain special constructs for speedy reflection, for example
- @see SetHXGen module
- *)
- let rec is_hxgen md =
- match md with
- | TClassDecl cl -> Meta.has Meta.HxGen cl.cl_meta
- | TEnumDecl e -> Meta.has Meta.HxGen e.e_meta
- | TTypeDecl t -> Meta.has Meta.HxGen t.t_meta || ( match follow t.t_type with | TInst(cl,_) -> is_hxgen (TClassDecl cl) | TEnum(e,_) -> is_hxgen (TEnumDecl e) | _ -> false )
- | TAbstractDecl a -> Meta.has Meta.HxGen a.a_meta
- let is_hxgen_t t =
- match t with
- | TInst (cl, _) -> Meta.has Meta.HxGen cl.cl_meta
- | TEnum (e, _) -> Meta.has Meta.HxGen e.e_meta
- | TAbstract (a, _) -> Meta.has Meta.HxGen a.a_meta
- | TType (t, _) -> Meta.has Meta.HxGen t.t_meta
- | _ -> false
- let mt_to_t_dyn md =
- match md with
- | TClassDecl cl -> TInst(cl, List.map (fun _ -> t_dynamic) cl.cl_params)
- | TEnumDecl e -> TEnum(e, List.map (fun _ -> t_dynamic) e.e_params)
- | TAbstractDecl a -> TAbstract(a, List.map (fun _ -> t_dynamic) a.a_params)
- | TTypeDecl t -> TType(t, List.map (fun _ -> t_dynamic) t.t_params)
- let mt_to_t mt params =
- match mt with
- | TClassDecl (cl) -> TInst(cl, params)
- | TEnumDecl (e) -> TEnum(e, params)
- | TAbstractDecl a -> TAbstract(a, params)
- | _ -> assert false
- let t_to_mt t =
- match follow t with
- | TInst(cl, _) -> TClassDecl(cl)
- | TEnum(e, _) -> TEnumDecl(e)
- | TAbstract(a, _) -> TAbstractDecl a
- | _ -> assert false
- let rec get_last_ctor cl =
- Option.map_default (fun (super,_) -> if is_some super.cl_constructor then Some(get super.cl_constructor) else get_last_ctor super) None cl.cl_super
- let add_constructor cl cf =
- match cl.cl_constructor with
- | None -> cl.cl_constructor <- Some cf
- | Some ctor ->
- if ctor != cf && not (List.memq cf ctor.cf_overloads) then
- ctor.cf_overloads <- cf :: ctor.cf_overloads
- (* replace open TMonos with TDynamic *)
- let rec replace_mono t =
- match t with
- | TMono t ->
- (match !t with
- | None -> t := Some t_dynamic
- | Some _ -> ())
- | TEnum (_,p) | TInst (_,p) | TType (_,p) | TAbstract (_,p) ->
- List.iter replace_mono p
- | TFun (args,ret) ->
- List.iter (fun (_,_,t) -> replace_mono t) args;
- replace_mono ret
- | TAnon _
- | TDynamic _ -> ()
- | TLazy f ->
- replace_mono (!f())
- (* helper *)
- let mk_class_field name t public pos kind params =
- {
- cf_name = name;
- cf_type = t;
- cf_public = public;
- cf_pos = pos;
- cf_doc = None;
- cf_meta = [ Meta.CompilerGenerated, [], Ast.null_pos ]; (* annotate that this class field was generated by the compiler *)
- cf_kind = kind;
- cf_params = params;
- cf_expr = None;
- cf_overloads = [];
- }
- (* this helper just duplicates the type parameter class, which is assumed that cl is. *)
- (* This is so we can use class parameters on function parameters, without running the risk of name clash *)
- (* between both *)
- let map_param cl =
- let ret = mk_class cl.cl_module (fst cl.cl_path, snd cl.cl_path ^ "_c") cl.cl_pos in
- ret.cl_implements <- cl.cl_implements;
- ret.cl_kind <- cl.cl_kind;
- ret
- let get_cl_t t =
- match follow t with | TInst (cl,_) -> cl | _ -> assert false
- let mk_class m path pos =
- let cl = Type.mk_class m path pos in
- cl.cl_meta <- [ Meta.CompilerGenerated, [], Ast.null_pos ];
- cl
- type tfield_access =
- | FClassField of tclass * tparams * tclass (* declared class *) * tclass_field * bool (* is static? *) * t (* the actual cf type, in relation to the class type params *) * t (* declared type *)
- | FEnumField of tenum * tenum_field * bool (* is parameterized enum ? *)
- | FAnonField of tclass_field
- | FDynamicField of t
- | FNotFound
- let is_var f = match f.cf_kind with | Var _ -> true | _ -> false
- let find_first_declared_field gen orig_cl ?exact_field field =
- let chosen = ref None in
- let is_overload = ref false in
- let rec loop_cl depth c tl tlch =
- (try
- let ret = PMap.find field c.cl_fields in
- if Meta.has Meta.Overload ret.cf_meta then is_overload := true;
- match !chosen, exact_field with
- | Some(d,f,_,_,_), _ when depth <= d || (is_var ret && not (is_var f)) -> ()
- | _, None ->
- chosen := Some(depth,ret,c,tl,tlch)
- | _, Some f2 ->
- List.iter (fun f ->
- let declared_t = apply_params c.cl_params tl f.cf_type in
- if Typeload.same_overload_args declared_t f2.cf_type f f2 then
- chosen := Some(depth,f,c,tl,tlch)
- ) (ret :: ret.cf_overloads)
- with | Not_found -> ());
- (match c.cl_super with
- | Some (sup,stl) ->
- let tl = List.map (apply_params c.cl_params tl) stl in
- let stl = gen.greal_type_param (TClassDecl sup) stl in
- let tlch = List.map (apply_params c.cl_params tlch) stl in
- loop_cl (depth+1) sup tl tlch
- | None -> ());
- if c.cl_interface then
- List.iter (fun (sup,stl) ->
- let tl = List.map (apply_params c.cl_params tl) stl in
- let stl = gen.greal_type_param (TClassDecl sup) stl in
- let tlch = List.map (apply_params c.cl_params tlch) stl in
- loop_cl (depth+1) sup tl tlch
- ) c.cl_implements
- in
- loop_cl 0 orig_cl (List.map snd orig_cl.cl_params) (List.map snd orig_cl.cl_params);
- match !chosen with
- | None -> None
- | Some(_,f,c,tl,tlch) ->
- if !is_overload && not (Meta.has Meta.Overload f.cf_meta) then
- f.cf_meta <- (Meta.Overload,[],f.cf_pos) :: f.cf_meta;
- let declared_t = apply_params c.cl_params tl f.cf_type in
- let params_t = apply_params c.cl_params tlch f.cf_type in
- let actual_t = match follow params_t with
- | TFun(args,ret) -> TFun(List.map (fun (n,o,t) -> (n,o,gen.greal_type t)) args, gen.greal_type ret)
- | _ -> gen.greal_type params_t in
- Some(f,actual_t,declared_t,params_t,c,tl,tlch)
- let field_access gen (t:t) (field:string) : (tfield_access) =
- (*
- t can be either an haxe-type as a real-type;
- 'follow' should be applied here since we can generalize that a TType will be accessible as its
- underlying type.
- *)
- (* let pointers to values be accessed as the underlying values *)
- let t = match gen.greal_type t with
- | TAbstract({ a_path = ["cs"],"Pointer" },[t]) ->
- gen.greal_type t
- | _ -> t
- in
- match follow t with
- | TInst(cl, params) ->
- let orig_cl = cl in
- let orig_params = params in
- let rec not_found cl params =
- match cl.cl_dynamic with
- | Some t ->
- let t = apply_params cl.cl_params params t in
- FDynamicField t
- | None ->
- match cl.cl_super with
- | None -> FNotFound
- | Some (super,p) -> not_found super p
- in
- let not_found () =
- try
- let cf = PMap.find field gen.gbase_class_fields in
- FClassField (orig_cl, orig_params, gen.gclasses.cl_dyn, cf, false, cf.cf_type, cf.cf_type)
- with
- | Not_found -> not_found cl params
- in
- (* this is a hack for C#'s different generic types with same path *)
- let hashtbl_field = (String.concat "" (List.map (fun _ -> "]") cl.cl_params)) ^ field in
- let types = try
- Hashtbl.find gen.greal_field_types (orig_cl.cl_path, hashtbl_field)
- with | Not_found ->
- let ret = find_first_declared_field gen cl field in
- let ret = match ret with
- | None -> None
- | Some(cf,t,dt,_,cl,_,_) -> Some(cf,t,dt,cl)
- in
- if ret <> None then Hashtbl.add gen.greal_field_types (orig_cl.cl_path, hashtbl_field) ret;
- ret
- in
- (match types with
- | None -> not_found()
- | Some (cf, actual_t, declared_t, declared_cl) ->
- FClassField(orig_cl, orig_params, declared_cl, cf, false, actual_t, declared_t))
- | TEnum _ | TAbstract _ ->
- (* enums have no field *) FNotFound
- | TAnon anon ->
- (try match !(anon.a_status) with
- | Statics cl ->
- let cf = PMap.find field cl.cl_statics in
- FClassField(cl, List.map (fun _ -> t_dynamic) cl.cl_params, cl, cf, true, cf.cf_type, cf.cf_type)
- | EnumStatics e ->
- let f = PMap.find field e.e_constrs in
- let is_param = match follow f.ef_type with | TFun _ -> true | _ -> false in
- FEnumField(e, f, is_param)
- | _ when PMap.mem field gen.gbase_class_fields ->
- let cf = PMap.find field gen.gbase_class_fields in
- FClassField(gen.gclasses.cl_dyn, [t_dynamic], gen.gclasses.cl_dyn, cf, false, cf.cf_type, cf.cf_type)
- | _ ->
- FAnonField(PMap.find field anon.a_fields)
- with | Not_found -> FNotFound)
- | _ when PMap.mem field gen.gbase_class_fields ->
- let cf = PMap.find field gen.gbase_class_fields in
- FClassField(gen.gclasses.cl_dyn, [t_dynamic], gen.gclasses.cl_dyn, cf, false, cf.cf_type, cf.cf_type)
- | TDynamic t -> FDynamicField t
- | TMono _ -> FDynamicField t_dynamic
- | _ -> FNotFound
- let field_access_esp gen t field = match field with
- | FStatic(cl,cf) | FInstance(cl,_,cf) when Meta.has Meta.Extern cf.cf_meta ->
- let static = match field with
- | FStatic _ -> true
- | _ -> false
- in
- let p = match follow (run_follow gen t) with
- | TInst(_,p) -> p
- | _ -> List.map snd cl.cl_params
- in
- FClassField(cl,p,cl,cf,static,cf.cf_type,cf.cf_type)
- | _ -> field_access gen t (field_name field)
- let mk_field_access gen expr field pos =
- match field_access gen expr.etype field with
- | FClassField(c,p,dc,cf,false,at,_) ->
- { eexpr = TField(expr, FInstance(dc,p,cf)); etype = apply_params c.cl_params p at; epos = pos }
- | FClassField(c,p,dc,cf,true,at,_) ->
- { eexpr = TField(expr, FStatic(dc,cf)); etype = at; epos = pos }
- | FAnonField cf ->
- { eexpr = TField(expr, FAnon cf); etype = cf.cf_type; epos = pos }
- | FDynamicField t ->
- { eexpr = TField(expr, FDynamic field); etype = t; epos = pos }
- | FNotFound ->
- { eexpr = TField(expr, FDynamic field); etype = t_dynamic; epos = pos }
- | FEnumField _ -> assert false
- let mk_iterator_access gen t expr =
- let pos = expr.epos in
- let itf = mk_field_access gen expr "iterator" pos in
- { eexpr = TCall(itf, []); epos = pos; etype = snd (get_fun itf.etype) }
- (* ******************************************* *)
- (* Module dependency resolution *)
- (* ******************************************* *)
- type t_dependency =
- | DAfter of float
- | DBefore of float
- exception ImpossibleDependency of string
- let max_dep = 10000.0
- let min_dep = - (10000.0)
- let solve_deps name (deps:t_dependency list) =
- let vmin = min_dep -. 1.0 in
- let vmax = max_dep +. 1.0 in
- let rec loop dep vmin vmax =
- match dep with
- | [] ->
- (if vmin >= vmax then raise (ImpossibleDependency name));
- (vmin +. vmax) /. 2.0
- | head :: tail ->
- match head with
- | DBefore f ->
- loop tail (max vmin f) vmax
- | DAfter f ->
- loop tail vmin (min vmax f)
- in
- loop deps vmin vmax
- (* type resolution *)
- exception TypeNotFound of path
- let get_type gen path =
- try Hashtbl.find gen.gtypes path with | Not_found -> raise (TypeNotFound path)
- (* ******************************************* *)
- (* follow all module *)
- (* ******************************************* *)
- (*
- this module will follow each and every type using the rules defined in
- gen.gfollow. This is a minor helper module, so we don't end up
- having to follow the same time multiple times in the many filter iterations
- because of this, it will be one of the first modules to run.
- *)
- module FollowAll =
- struct
- let follow gen e =
- let follow_func = gen.gfollow#run_f in
- Some (Type.map_expr_type (fun e->e) (follow_func) (fun tvar-> tvar.v_type <- (follow_func tvar.v_type); tvar) e)
- let priority = max_dep
- (* will add an expression filter as the first filter *)
- let configure gen =
- gen.gexpr_filters#add ~name:"follow_all" ~priority:(PCustom(priority)) (follow gen)
- end;;
- (* ******************************************* *)
- (* set hxgen module *)
- (* ******************************************* *)
- (*
- goes through all module types and sets the :hxgen meta on all which
- then is_hxgen_func returns true. There is a default is_hxgen_func implementation also
- *)
- module SetHXGen =
- struct
- (*
- basically, everything that is extern is assumed to not be hxgen, unless meta :hxgen is set, and
- everything that is not extern is assumed to be hxgen, unless meta :nativegen is set
- *)
- let rec default_hxgen_func md =
- match md with
- | TClassDecl { cl_kind = KAbstractImpl a } ->
- default_hxgen_func (TAbstractDecl a)
- | TClassDecl cl ->
- let rec is_hxgen_class (c,_) =
- if c.cl_extern then begin
- if Meta.has Meta.HxGen c.cl_meta then true else Option.map_default (is_hxgen_class) false c.cl_super || List.exists is_hxgen_class c.cl_implements
- end else begin
- if Meta.has Meta.NativeChildren c.cl_meta || Meta.has Meta.NativeGen c.cl_meta then
- Option.map_default (is_hxgen_class) false c.cl_super || List.exists is_hxgen_class c.cl_implements
- else
- let rec has_nativec (c,p) =
- if is_hxgen_class (c,p) then
- false
- else
- (Meta.has Meta.NativeChildren c.cl_meta && not (Option.map_default is_hxgen_class false c.cl_super || List.exists is_hxgen_class c.cl_implements))
- || Option.map_default has_nativec false c.cl_super
- in
- if Option.map_default has_nativec false c.cl_super && not (List.exists is_hxgen_class c.cl_implements) then
- false
- else
- true
- end
- in
- is_hxgen_class (cl,[])
- | TEnumDecl e -> if e.e_extern then Meta.has Meta.HxGen e.e_meta else not (Meta.has Meta.NativeGen e.e_meta)
- | TAbstractDecl a when Meta.has Meta.CoreType a.a_meta -> not (Meta.has Meta.NativeGen a.a_meta)
- | TAbstractDecl a -> (match follow a.a_this with
- | TInst _ | TEnum _ | TAbstract _ ->
- default_hxgen_func (t_to_md (follow a.a_this))
- | _ ->
- Meta.has Meta.NativeGen a.a_meta)
- | TTypeDecl t -> (* TODO see when would we use this *)
- false
- (*
- by now the only option is to run it eagerly, because it must be one of the first filters to run,
- since many others depend of it
- *)
- let run_filter gen is_hxgen_func =
- let filter md =
- let meta = if is_hxgen_func md then Meta.HxGen else Meta.NativeGen in
- begin
- match md with
- | TClassDecl cl -> cl.cl_meta <- (meta, [], cl.cl_pos) :: cl.cl_meta
- | TEnumDecl e -> e.e_meta <- (meta, [], e.e_pos) :: e.e_meta
- | TTypeDecl t -> t.t_meta <- (meta, [], t.t_pos) :: t.t_meta
- | TAbstractDecl a -> a.a_meta <- (meta, [], a.a_pos) :: a.a_meta
- end
- in
- List.iter filter gen.gcon.types
- end;;
- (* ******************************************* *)
- (* overloading reflection constructors *)
- (* ******************************************* *)
- (*
- this module works on languages that support function overloading and
- enable function hiding via static functions.
- it takes the constructor body out of the constructor and adds it to a special ctor
- static function. The static function will receive the same parameters as the constructor,
- plus the special "me" var, which will replace "this"
- Then it always adds two constructors to the function: one that receives a special class,
- indicating that it should be constructed without any parameters, and one that receives its normal constructor.
- Both will only include a super() call to the superclasses' emtpy constructor.
- This enables two things:
- empty construction without the need of incompatibility with the platform's native construction method
- the ability to call super() constructor in any place in the constructor
- This will insert itself in the default reflection-related module filter
- *)
- module OverloadingConstructor =
- struct
- let priority = 0.0
- let name = "overloading_constructor"
- let set_new_create_empty gen empty_ctor_expr =
- let old = gen.gtools.rf_create_empty in
- gen.gtools.rf_create_empty <- (fun cl params pos ->
- if is_hxgen (TClassDecl cl) then
- { eexpr = TNew(cl,params,[empty_ctor_expr]); etype = TInst(cl,params); epos = pos }
- else
- old cl params pos
- )
- let rec cur_ctor c tl =
- match c.cl_constructor with
- | Some ctor -> ctor, c, tl
- | None -> match c.cl_super with
- | None -> raise Not_found
- | Some (sup,stl) ->
- cur_ctor sup (List.map (apply_params c.cl_params tl) stl)
- let rec prev_ctor c tl =
- match c.cl_super with
- | None -> raise Not_found
- | Some (sup,stl) ->
- let stl = List.map (apply_params c.cl_params tl) stl in
- match sup.cl_constructor with
- | None -> prev_ctor sup stl
- | Some ctor -> ctor, sup, stl
- (* replaces super() call with last static constructor call *)
- let replace_super_call gen name c tl with_params me p =
- let rec loop_super c tl = match c.cl_super with
- | None -> raise Not_found
- | Some(sup,stl) ->
- let stl = List.map (apply_params c.cl_params tl) stl in
- try
- let static_ctor_name = name ^ "_" ^ (String.concat "_" (fst sup.cl_path)) ^ "_" ^ (snd sup.cl_path) in
- sup, stl, PMap.find static_ctor_name sup.cl_statics
- with | Not_found ->
- loop_super sup stl
- in
- let sup, stl, cf = loop_super c tl in
- let with_params = { eexpr = TLocal me; etype = me.v_type; epos = p } :: with_params in
- let cf = match cf.cf_overloads with
- (* | [] -> cf *)
- | _ -> try
- (* choose best super function *)
- List.iter (fun e -> replace_mono e.etype) with_params;
- List.find (fun cf ->
- replace_mono cf.cf_type;
- let args, _ = get_fun (apply_params cf.cf_params stl cf.cf_type) in
- try
- List.for_all2 (fun (_,_,t) e -> try
- let e_etype = run_follow gen e.etype in
- let t = run_follow gen t in
- unify e_etype t; true
- with | Unify_error _ -> false) args with_params
- with | Invalid_argument("List.for_all2") -> false
- ) (cf :: cf.cf_overloads)
- with | Not_found ->
- gen.gcon.error "No suitable overload for the super call arguments was found" p; cf
- in
- {
- eexpr = TCall({
- eexpr = TField(
- mk_classtype_access sup p,
- FStatic(sup,cf));
- etype = apply_params cf.cf_params stl cf.cf_type;
- epos = p},
- with_params);
- etype = gen.gcon.basic.tvoid;
- epos = p;
- }
- (* will create a static counterpart of 'ctor', and replace its contents to a call to the static version*)
- let create_static_ctor gen ~empty_ctor_expr cl name ctor =
- match Meta.has Meta.SkipCtor ctor.cf_meta with
- | true -> ()
- | false when is_none ctor.cf_expr -> ()
- | false ->
- let static_ctor_name = name ^ "_" ^ (String.concat "_" (fst cl.cl_path)) ^ "_" ^ (snd cl.cl_path) in
- (* create the static constructor *)
- let basic = gen.gcon.basic in
- let ctor_types = List.map (fun (s,t) -> (s, TInst(map_param (get_cl_t t), []))) cl.cl_params in
- let me = mk_temp gen "me" (TInst(cl, List.map snd ctor_types)) in
- me.v_capture <- true;
- let fn_args, _ = get_fun ctor.cf_type in
- let ctor_params = List.map snd ctor_types in
- let fn_type = TFun((me.v_name,false, me.v_type) :: List.map (fun (n,o,t) -> (n,o,apply_params cl.cl_params ctor_params t)) fn_args, basic.tvoid) in
- let cur_tf_args = match ctor.cf_expr with
- | Some { eexpr = TFunction(tf) } -> tf.tf_args
- | _ -> assert false
- in
- let changed_tf_args = List.map (fun (v,_) -> (v,None)) cur_tf_args in
- let local_map = Hashtbl.create (List.length cur_tf_args) in
- let static_tf_args = (me, None) :: List.map (fun (v,b) ->
- let new_v = alloc_var v.v_name (apply_params cl.cl_params ctor_params v.v_type) in
- new_v.v_capture <- v.v_capture;
- Hashtbl.add local_map v.v_id new_v;
- (new_v, b)
- ) cur_tf_args in
- let static_ctor = mk_class_field static_ctor_name fn_type false ctor.cf_pos (Method MethNormal) ctor_types in
- (* change ctor contents to reference the 'me' var instead of 'this' *)
- let actual_super_call = ref None in
- let rec map_expr ~is_first e = match e.eexpr with
- | TCall (({ eexpr = TConst TSuper } as tsuper), params) -> (try
- let params = List.map (fun e -> map_expr ~is_first:false e) params in
- actual_super_call := Some { e with eexpr = TCall(tsuper, [empty_ctor_expr]) };
- replace_super_call gen name cl ctor_params params me e.epos
- with | Not_found ->
- (* last static function was not found *)
- actual_super_call := Some e;
- if not is_first then
- gen.gcon.error "Super call must be the first call when extending native types" e.epos;
- { e with eexpr = TBlock([]) })
- | TFunction tf when is_first ->
- do_map ~is_first:true e
- | TConst TThis ->
- mk_local me e.epos
- | TBlock (fst :: bl) ->
- let fst = map_expr ~is_first:is_first fst in
- { e with eexpr = TBlock(fst :: List.map (fun e -> map_expr ~is_first:false e) bl); etype = apply_params cl.cl_params ctor_params e.etype }
- | _ ->
- do_map e
- and do_map ?(is_first=false) e =
- let do_t = apply_params cl.cl_params ctor_params in
- let do_v v = try
- Hashtbl.find local_map v.v_id
- with | Not_found ->
- v.v_type <- do_t v.v_type; v
- in
- Type.map_expr_type (map_expr ~is_first:is_first) do_t do_v e
- in
- let expr = do_map ~is_first:true (get ctor.cf_expr) in
- let expr = match expr.eexpr with
- | TFunction(tf) ->
- { expr with etype = fn_type; eexpr = TFunction({ tf with tf_args = static_tf_args }) }
- | _ -> assert false in
- static_ctor.cf_expr <- Some expr;
- (* add to the statics *)
- (try
- let stat = PMap.find static_ctor_name cl.cl_statics in
- stat.cf_overloads <- static_ctor :: stat.cf_overloads
- with | Not_found ->
- cl.cl_ordered_statics <- static_ctor :: cl.cl_ordered_statics;
- cl.cl_statics <- PMap.add static_ctor_name static_ctor cl.cl_statics);
- (* change current super call *)
- match ctor.cf_expr with
- | Some({ eexpr = TFunction(tf) } as e) ->
- let block_contents, p = match !actual_super_call with
- | None -> [], ctor.cf_pos
- | Some super -> [super], super.epos
- in
- let block_contents = block_contents @ [{
- eexpr = TCall(
- {
- eexpr = TField(
- mk_classtype_access cl p,
- FStatic(cl, static_ctor));
- etype = apply_params static_ctor.cf_params (List.map snd cl.cl_params) static_ctor.cf_type;
- epos = p
- },
- [{ eexpr = TConst TThis; etype = TInst(cl, List.map snd cl.cl_params); epos = p }]
- @ List.map (fun (v,_) -> mk_local v p) cur_tf_args
- );
- etype = basic.tvoid;
- epos = p
- }] in
- ctor.cf_expr <- Some { e with eexpr = TFunction({ tf with tf_expr = { tf.tf_expr with eexpr = TBlock block_contents }; tf_args = changed_tf_args }) }
- | _ -> assert false
- (* makes constructors that only call super() for the 'ctor' argument *)
- let clone_ctors gen ctor sup stl cl =
- let basic = gen.gcon.basic in
- let rec clone cf =
- let ncf = mk_class_field "new" (apply_params sup.cl_params stl cf.cf_type) cf.cf_public cf.cf_pos cf.cf_kind cf.cf_params in
- let args, ret = get_fun ncf.cf_type in
- (* single expression: call to super() *)
- let tf_args = List.map (fun (name,_,t) ->
- (* the constructor will have no optional arguments, as presumably this will be handled by the underlying expr *)
- alloc_var name t, None
- ) args in
- let super_call =
- {
- eexpr = TCall(
- { eexpr = TConst TSuper; etype = TInst(cl, List.map snd cl.cl_params); epos = ctor.cf_pos },
- List.map (fun (v,_) -> mk_local v ctor.cf_pos) tf_args);
- etype = basic.tvoid;
- epos = ctor.cf_pos;
- } in
- ncf.cf_expr <- Some
- {
- eexpr = TFunction {
- tf_args = tf_args;
- tf_type = basic.tvoid;
- tf_expr = mk_block super_call;
- };
- etype = ncf.cf_type;
- epos = ctor.cf_pos;
- };
- ncf
- in
- (* take off createEmpty *)
- let all = List.filter (fun cf -> replace_mono cf.cf_type; not (Meta.has Meta.SkipCtor cf.cf_meta)) (ctor :: ctor.cf_overloads) in
- let clones = List.map clone all in
- match clones with
- | [] ->
- (* raise Not_found *)
- assert false (* should never happen *)
- | cf :: [] -> cf
- | cf :: overl ->
- cf.cf_meta <- (Meta.Overload,[],cf.cf_pos) :: cf.cf_meta;
- cf.cf_overloads <- overl; cf
- let rec descends_from_native_or_skipctor cl =
- not (is_hxgen (TClassDecl cl)) || Meta.has Meta.SkipCtor cl.cl_meta || match cl.cl_super with
- | None -> false
- | Some(c,_) -> descends_from_native_or_skipctor c
- let ensure_super_is_first gen cf =
- let rec loop e =
- match e.eexpr with
- | TBlock (b :: block) ->
- loop b
- | TBlock []
- | TCall({ eexpr = TConst TSuper },_) -> ()
- | _ ->
- gen.gcon.error "Types that derive from a native class must have its super() call as the first statement in the constructor" cf.cf_pos
- in
- match cf.cf_expr with
- | None -> ()
- | Some e -> Type.iter loop e
- (* major restructring made at r6493 *)
- let configure ~(empty_ctor_type : t) ~(empty_ctor_expr : texpr) ~supports_ctor_inheritance gen =
- set_new_create_empty gen empty_ctor_expr;
- let basic = gen.gcon.basic in
- let should_change cl = not cl.cl_interface && (not cl.cl_extern || is_hxgen (TClassDecl cl)) && (match cl.cl_kind with KAbstractImpl _ -> false | _ -> true) in
- let static_ctor_name = gen.gmk_internal_name "hx" "ctor" in
- let msize = List.length gen.gcon.types in
- let processed, empty_ctors = Hashtbl.create msize, Hashtbl.create msize in
- let rec get_last_empty cl =
- try
- Hashtbl.find empty_ctors cl.cl_path
- with | Not_found ->
- match cl.cl_super with
- | None -> raise Not_found
- | Some (sup,_) -> get_last_empty sup
- in
- let rec change cl =
- match Hashtbl.mem processed cl.cl_path with
- | true -> ()
- | false ->
- Hashtbl.add processed cl.cl_path true;
- (* make sure we've processed the super types *)
- (match cl.cl_super with
- | Some (super,_) when should_change super && not (Hashtbl.mem processed super.cl_path) ->
- change super
- | _ -> ());
- (* implement static hx_ctor and reimplement constructors *)
- (try
- let ctor = match cl.cl_constructor with
- | Some ctor -> ctor
- | None -> try
- let sctor, sup, stl = prev_ctor cl (List.map snd cl.cl_params) in
- (* we have a previous constructor. if we support inheritance, exit *)
- if supports_ctor_inheritance then raise Exit;
- (* we'll make constructors that will only call super() *)
- let ctor = clone_ctors gen sctor sup stl cl in
- cl.cl_constructor <- Some ctor;
- ctor
- with | Not_found -> (* create default constructor *)
- let ctor = mk_class_field "new" (TFun([], basic.tvoid)) false cl.cl_pos (Method MethNormal) [] in
- ctor.cf_expr <- Some
- {
- eexpr = TFunction {
- tf_args = [];
- tf_type = basic.tvoid;
- tf_expr = { eexpr = TBlock[]; etype = basic.tvoid; epos = cl.cl_pos };
- };
- etype = ctor.cf_type;
- epos = ctor.cf_pos;
- };
- cl.cl_constructor <- Some ctor;
- ctor
- in
- (* now that we made sure we have a constructor, exit if native gen *)
- if not (is_hxgen (TClassDecl cl)) || Meta.has Meta.SkipCtor cl.cl_meta then begin
- if descends_from_native_or_skipctor cl && is_some cl.cl_super then
- List.iter (fun cf -> ensure_super_is_first gen cf) (ctor :: ctor.cf_overloads);
- raise Exit
- end;
- (* if cl descends from a native class, we cannot use the static constructor strategy *)
- if descends_from_native_or_skipctor cl && is_some cl.cl_super then
- List.iter (fun cf -> ensure_super_is_first gen cf) (ctor :: ctor.cf_overloads)
- else
- (* now that we have a current ctor, create the static counterparts *)
- List.iter (fun cf ->
- create_static_ctor gen ~empty_ctor_expr:empty_ctor_expr cl static_ctor_name cf
- ) (ctor :: ctor.cf_overloads)
- with | Exit ->());
- (* implement empty ctor *)
- (try
- (* now that we made sure we have a constructor, exit if native gen *)
- if not (is_hxgen (TClassDecl cl)) then raise Exit;
- (* get first *)
- let empty_type = TFun(["empty",false,empty_ctor_type],basic.tvoid) in
- let super = match cl.cl_super with
- | None -> (* implement empty *)
- []
- | Some (sup,_) -> try
- ignore (get_last_empty sup);
- if supports_ctor_inheritance && is_none cl.cl_constructor then raise Exit;
- [{
- eexpr = TCall(
- { eexpr = TConst TSuper; etype = TInst(cl, List.map snd cl.cl_params); epos = cl.cl_pos },
- [ empty_ctor_expr ]);
- etype = basic.tvoid;
- epos = cl.cl_pos
- }]
- with | Not_found -> try
- (* super type is native: find super constructor with least arguments *)
- let sctor, sup, stl = prev_ctor cl (List.map snd cl.cl_params) in
- let rec loop remaining (best,n) =
- match remaining with
- | [] -> best
- | cf :: r ->
- let args,_ = get_fun cf.cf_type in
- if (List.length args) < n then
- loop r (cf,List.length args)
- else
- loop r (best,n)
- in
- let args,_ = get_fun sctor.cf_type in
- let best = loop sctor.cf_overloads (sctor, List.length args) in
- let args,_ = get_fun (apply_params sup.cl_params stl best.cf_type) in
- [{
- eexpr = TCall(
- { eexpr = TConst TSuper; etype = TInst(sup, stl); epos = cl.cl_pos },
- List.map (fun (n,o,t) -> null t cl.cl_pos) args);
- etype = basic.tvoid;
- epos = cl.cl_pos
- }]
- with | Not_found ->
- (* extends native type, but no ctor found *)
- []
- in
- let ctor = mk_class_field "new" empty_type false cl.cl_pos (Method MethNormal) [] in
- ctor.cf_expr <- Some {
- eexpr = TFunction {
- tf_type = basic.tvoid;
- tf_args = [alloc_var "empty" empty_ctor_type, None];
- tf_expr = { eexpr = TBlock super; etype = basic.tvoid; epos = cl.cl_pos }
- };
- etype = empty_type;
- epos = cl.cl_pos;
- };
- ctor.cf_meta <- [Meta.SkipCtor, [], ctor.cf_pos];
- Hashtbl.add empty_ctors cl.cl_path ctor;
- match cl.cl_constructor with
- | None -> cl.cl_constructor <- Some ctor
- | Some c -> c.cf_overloads <- ctor :: c.cf_overloads
- with | Exit -> ());
- in
- let module_filter md = match md with
- | TClassDecl cl when should_change cl && not (Hashtbl.mem processed cl.cl_path) ->
- change cl;
- None
- | _ -> None
- in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) module_filter
- end;;
- (* ******************************************* *)
- (* init function module *)
- (* ******************************************* *)
- (*
- This module will take proper care of the init function, by taking off all expressions from static vars and putting them
- in order in the init function.
- It will also initialize dynamic functions, both by putting them in the constructor and in the init function
- depends on:
- (syntax) must run before ExprStatement module
- (ok) must run before OverloadingCtor module so the constructor can be in the correct place
- (syntax) must run before FunctionToClass module
- *)
- module InitFunction =
- struct
- let name = "init_funcs"
- let priority = solve_deps name [DBefore OverloadingConstructor.priority]
- let ensure_simple_expr gen e =
- let rec iter e = match e.eexpr with
- | TConst _ | TLocal _ | TArray _ | TBinop _
- | TField _ | TTypeExpr _ | TParenthesis _ | TCast _
- | TCall _ | TNew _ | TUnop _ ->
- Type.iter iter e
- | _ ->
- print_endline (debug_expr e);
- gen.gcon.error "Expression is too complex for a readonly variable initialization" e.epos
- in
- iter e
- let configure gen should_handle_dynamic_functions readonly_support =
- let handle_override_dynfun acc e this field =
- let add_expr = ref None in
- let v = mk_temp gen ("super_" ^ field) e.etype in
- v.v_capture <- true;
- let rec loop e =
- match e.eexpr with
- | TField({ eexpr = TConst(TSuper) }, f) ->
- let n = field_name f in
- (if n <> field then assert false);
- let local = mk_local v e.epos in
- (match !add_expr with
- | None ->
- add_expr := Some { e with eexpr = TVar(v, Some this) }
- | Some _ -> ());
- local
- | TConst TSuper -> assert false
- | _ -> Type.map_expr loop e
- in
- let e = loop e in
- match !add_expr with
- | None -> e :: acc
- | Some add_expr -> add_expr :: e :: acc
- in
- let handle_class cl =
- let init = match cl.cl_init with
- | None -> []
- | Some i -> [i]
- in
- let init = List.fold_left (fun acc cf ->
- match cf.cf_kind, should_handle_dynamic_functions with
- | (Var v, _) when Meta.has Meta.ReadOnly cf.cf_meta && readonly_support ->
- if v.v_write <> AccNever then gen.gcon.warning "@:readOnly variable declared without `never` setter modifier" cf.cf_pos;
- (match cf.cf_expr with
- | None -> gen.gcon.warning "Uninitialized readonly variable" cf.cf_pos; acc
- | Some e -> ensure_simple_expr gen e; acc)
- | (Var _, _)
- | (Method (MethDynamic), true) when not (Type.is_extern_field cf) ->
- (match cf.cf_expr with
- | Some e ->
- (match cf.cf_params with
- | [] ->
- let var = { eexpr = TField(mk_classtype_access cl cf.cf_pos, FStatic(cl,cf)); etype = cf.cf_type; epos = cf.cf_pos } in
- let ret = ({ eexpr = TBinop(Ast.OpAssign, var, e); etype = cf.cf_type; epos = cf.cf_pos; }) in
- cf.cf_expr <- None;
- ret :: acc
- | _ ->
- let params = List.map (fun _ -> t_dynamic) cf.cf_params in
- let fn = apply_params cf.cf_params params in
- let var = { eexpr = TField(mk_classtype_access cl cf.cf_pos, FStatic(cl,cf)); etype = fn cf.cf_type; epos = cf.cf_pos } in
- let rec change_expr e =
- Type.map_expr_type (change_expr) fn (fun v -> v.v_type <- fn v.v_type; v) e
- in
- let ret = ({ eexpr = TBinop(Ast.OpAssign, var, change_expr e); etype = fn cf.cf_type; epos = cf.cf_pos; }) in
- cf.cf_expr <- None;
- ret :: acc
- )
- | None -> acc)
- | _ -> acc
- ) init cl.cl_ordered_statics
- in
- let init = List.rev init in
- (match init with
- | [] -> cl.cl_init <- None
- | _ -> cl.cl_init <- Some { eexpr = TBlock(init); epos = cl.cl_pos; etype = gen.gcon.basic.tvoid; });
- (* FIXME: find a way to tell OverloadingCtors to execute this code even with empty constructors *)
- if should_handle_dynamic_functions then begin
- let vars, funs = List.fold_left (fun (acc_vars,acc_funs) cf ->
- match cf.cf_kind with
- | Var v when Meta.has Meta.ReadOnly cf.cf_meta && readonly_support ->
- if v.v_write <> AccNever then gen.gcon.warning "@:readOnly variable declared without `never` setter modifier" cf.cf_pos;
- (match cf.cf_expr with
- | None -> (acc_vars,acc_funs)
- | Some e -> ensure_simple_expr gen e; (acc_vars,acc_funs))
- | Var _
- | Method(MethDynamic) ->
- let is_var = match cf.cf_kind with | Var _ -> true | _ -> false in
- (match cf.cf_expr, cf.cf_params with
- | Some e, [] ->
- let var = { eexpr = TField({ eexpr = TConst(TThis); epos = cf.cf_pos; etype = TInst(cl, List.map snd cl.cl_params); }, FInstance(cl, List.map snd cl.cl_params, cf)); etype = cf.cf_type; epos = cf.cf_pos } in
- let ret = ({ eexpr = TBinop(Ast.OpAssign, var, e); etype = cf.cf_type; epos = cf.cf_pos; }) in
- cf.cf_expr <- None;
- let is_override = List.memq cf cl.cl_overrides in
- if is_override then begin
- cl.cl_ordered_fields <- List.filter (fun f -> f.cf_name <> cf.cf_name) cl.cl_ordered_fields;
- cl.cl_fields <- PMap.remove cf.cf_name cl.cl_fields;
- acc_vars, handle_override_dynfun acc_funs ret var cf.cf_name
- end else if is_var then
- ret :: acc_vars, acc_funs
- else
- acc_vars, ret :: acc_funs
- | Some e, _ ->
- let params = List.map (fun _ -> t_dynamic) cf.cf_params in
- let fn = apply_params cf.cf_params params in
- let var = { eexpr = TField({ eexpr = TConst(TThis); epos = cf.cf_pos; etype = TInst(cl, List.map snd cl.cl_params); }, FInstance(cl, List.map snd cl.cl_params, cf)); etype = cf.cf_type; epos = cf.cf_pos } in
- let rec change_expr e =
- Type.map_expr_type (change_expr) fn (fun v -> v.v_type <- fn v.v_type; v) e
- in
- let ret = ({ eexpr = TBinop(Ast.OpAssign, var, change_expr e); etype = fn cf.cf_type; epos = cf.cf_pos; }) in
- cf.cf_expr <- None;
- let is_override = List.memq cf cl.cl_overrides in
- if is_override then begin
- cl.cl_ordered_fields <- List.filter (fun f -> f.cf_name <> cf.cf_name) cl.cl_ordered_fields;
- cl.cl_fields <- PMap.remove cf.cf_name cl.cl_fields;
- acc_vars, handle_override_dynfun acc_funs ret var cf.cf_name
- end else if is_var then
- ret :: acc_vars, acc_funs
- else
- acc_vars, ret :: acc_funs
- | None, _ -> acc_vars,acc_funs)
- | _ -> acc_vars,acc_funs
- ) ([],[]) cl.cl_ordered_fields
- in
- (* let vars = List.rev vars in *)
- (* let funs = List.rev funs in *)
- (* see if there is any *)
- (match vars, funs with
- | [], [] -> ()
- | _ ->
- (* if there is, we need to find the constructor *)
- let ctors = match cl.cl_constructor with
- | Some ctor -> ctor
- | None -> try
- let sctor, sup, stl = OverloadingConstructor.prev_ctor cl (List.map snd cl.cl_params) in
- let ctor = OverloadingConstructor.clone_ctors gen sctor sup stl cl in
- cl.cl_constructor <- Some ctor;
- ctor
- with | Not_found ->
- let basic = gen.gcon.basic in
- let ctor = mk_class_field "new" (TFun([], basic.tvoid)) false cl.cl_pos (Method MethNormal) [] in
- ctor.cf_expr <- Some
- {
- eexpr = TFunction {
- tf_args = [];
- tf_type = basic.tvoid;
- tf_expr = { eexpr = TBlock[]; etype = basic.tvoid; epos = cl.cl_pos };
- };
- etype = ctor.cf_type;
- epos = ctor.cf_pos;
- };
- cl.cl_constructor <- Some ctor;
- ctor
- in
- let process ctor =
- let func = match ctor.cf_expr with
- | Some({eexpr = TFunction(tf)} as e) ->
- let rec add_fn e = match e.eexpr with
- | TBlock(hd :: tl) -> (match hd.eexpr with
- | TCall({ eexpr = TConst TSuper }, _) ->
- if not (OverloadingConstructor.descends_from_native_or_skipctor cl) then
- { e with eexpr = TBlock(vars @ (hd :: (funs @ tl))) }
- else
- { e with eexpr = TBlock(hd :: (vars @ funs @ tl)) }
- | TBlock(_) ->
- { e with eexpr = TBlock( (add_fn hd) :: tl ) }
- | _ ->
- { e with eexpr = TBlock( vars @ funs @ (hd :: tl) ) })
- | _ -> Type.concat { e with eexpr = TBlock(vars @ funs) } e
- in
- let tf_expr = add_fn (mk_block tf.tf_expr) in
- { e with eexpr = TFunction({ tf with tf_expr = tf_expr }) }
- | _ -> assert false
- in
- ctor.cf_expr <- Some(func)
- in
- List.iter process (ctors :: ctors.cf_overloads)
- )
- end
- in
- let mod_filter = function
- | TClassDecl cl -> (if not cl.cl_extern then handle_class cl); None
- | _ -> None in
- gen.gmodule_filters#add ~name:"init_funcs" ~priority:(PCustom priority) mod_filter
- end;;
- (* ******************************************* *)
- (* Dynamic Binop/Unop handler *)
- (* ******************************************* *)
- (*
- On some languages there is limited support for operations on
- dynamic variables, so those operations must be changed.
- There are 5 types of binary operators:
- 1 - can take any variable and returns a bool (== and !=)
- 2 - can take either a string, or a number and returns either a bool or the underlying type ( >, < for bool and + for returning its type)
- 3 - take numbers and return a number ( *, /, ...)
- 4 - take ints and return an int (bit manipulation)
- 5 - take a bool and returns a bool ( &&, || ...)
- On the default implementation, type 1 and the plus function will be handled with a function call;
- Type 2 will be handled with the parameter "compare_handler", which will do something like Reflect.compare(x1, x2);
- Types 3, 4 and 5 will perform a cast to double, int and bool, which will then be handled normally by the platform
- Unary operators are the most difficult to handle correctly.
- With unary operators, there are 2 types:
- 1 - can take a number, changes and returns the result (++, --, ~)
- 2 - can take a number (-) or bool (!), and returns the result
- The first case is much trickier, because it doesn't seem a good idea to change any variable to double just because it is dynamic,
- but this is how we will handle right now.
- something like that:
- var x:Dynamic = 10;
- x++;
- will be:
- object x = 10;
- x = ((IConvertible)x).ToDouble(null) + 1;
- depends on:
- (syntax) must run before expression/statment normalization because it may generate complex expressions
- must run before OverloadingCtor due to later priority conflicts. Since ExpressionUnwrap is only
- defined afterwards, we will set this value with absolute values
- *)
- module DynamicOperators =
- struct
- let name = "dyn_ops"
- let priority = 0.0
- let priority_as_synf = 100.0 (*solve_deps name [DBefore ExpressionUnwrap.priority]*)
- let abstract_implementation gen ?(handle_strings = true) (should_change:texpr->bool) (equals_handler:texpr->texpr->texpr) (dyn_plus_handler:texpr->texpr->texpr->texpr) (compare_handler:texpr->texpr->texpr) =
- let get_etype_one e =
- if like_int e.etype then
- (gen.gcon.basic.tint, { eexpr = TConst(TInt(Int32.one)); etype = gen.gcon.basic.tint; epos = e.epos })
- else
- (gen.gcon.basic.tfloat, { eexpr = TConst(TFloat("1.0")); etype = gen.gcon.basic.tfloat; epos = e.epos })
- in
- let basic = gen.gcon.basic in
- let rec run e =
- match e.eexpr with
- | TBinop (OpAssignOp op, e1, e2) when should_change e -> (* e1 will never contain another TBinop *)
- (match e1.eexpr with
- | TLocal _ ->
- mk_paren { e with eexpr = TBinop(OpAssign, e1, run { e with eexpr = TBinop(op, e1, e2) }) }
- | TField _ | TArray _ ->
- let eleft, rest = match e1.eexpr with
- | TField(ef, f) ->
- let v = mk_temp gen "dynop" ef.etype in
- { e1 with eexpr = TField(mk_local v ef.epos, f) }, [ { eexpr = TVar(v,Some (run ef)); etype = basic.tvoid; epos = ef.epos } ]
- | TArray(e1a, e2a) ->
- let v = mk_temp gen "dynop" e1a.etype in
- let v2 = mk_temp gen "dynopi" e2a.etype in
- { e1 with eexpr = TArray(mk_local v e1a.epos, mk_local v2 e2a.epos) }, [
- { eexpr = TVar(v,Some (run e1a)); etype = basic.tvoid; epos = e1.epos };
- { eexpr = TVar(v2, Some (run e2a)); etype = basic.tvoid; epos = e1.epos }
- ]
- | _ -> assert false
- in
- { e with
- eexpr = TBlock (rest @ [ { e with eexpr = TBinop(OpAssign, eleft, run { e with eexpr = TBinop(op, eleft, e2) }) } ]);
- }
- | _ ->
- assert false
- )
- | TBinop (OpAssign, e1, e2)
- | TBinop (OpInterval, e1, e2) -> Type.map_expr run e
- | TBinop (op, e1, e2) when should_change e->
- (match op with
- | OpEq -> (* type 1 *)
- equals_handler (run e1) (run e2)
- | OpNotEq -> (* != -> !equals() *)
- mk_paren { eexpr = TUnop(Ast.Not, Prefix, (equals_handler (run e1) (run e2))); etype = gen.gcon.basic.tbool; epos = e.epos }
- | OpAdd ->
- if handle_strings && (is_string e.etype || is_string e1.etype || is_string e2.etype) then
- { e with eexpr = TBinop(op, mk_cast gen.gcon.basic.tstring (run e1), mk_cast gen.gcon.basic.tstring (run e2)) }
- else
- dyn_plus_handler e (run e1) (run e2)
- | OpGt | OpGte | OpLt | OpLte -> (* type 2 *)
- { eexpr = TBinop(op, compare_handler (run e1) (run e2), { eexpr = TConst(TInt(Int32.zero)); etype = gen.gcon.basic.tint; epos = e.epos} ); etype = gen.gcon.basic.tbool; epos = e.epos }
- | OpMult | OpDiv | OpSub | OpMod -> (* always cast everything to double *)
- let etype, _ = get_etype_one e in
- { e with eexpr = TBinop(op, mk_cast etype (run e1), mk_cast etype (run e2)) }
- | OpBoolAnd | OpBoolOr ->
- { e with eexpr = TBinop(op, mk_cast gen.gcon.basic.tbool (run e1), mk_cast gen.gcon.basic.tbool (run e2)) }
- | OpAnd | OpOr | OpXor | OpShl | OpShr | OpUShr ->
- { e with eexpr = TBinop(op, mk_cast gen.gcon.basic.tint (run e1), mk_cast gen.gcon.basic.tint (run e2)) }
- | OpAssign | OpAssignOp _ | OpInterval | OpArrow -> assert false)
- | TUnop (Increment as op, flag, e1)
- | TUnop (Decrement as op, flag, e1) when should_change e ->
- (*
- some naming definitions:
- * ret => the returning variable
- * _g => the get body
- * getvar => the get variable expr
- This will work like this:
- - if e1 is a TField, set _g = get body, getvar = (get body).varname
- - if Prefix, return getvar = getvar + 1.0
- - if Postfix, set ret = getvar; getvar = getvar + 1.0; ret;
- *)
- let etype, one = get_etype_one e in
- let op = (match op with Increment -> OpAdd | Decrement -> OpSub | _ -> assert false) in
- let var, getvar =
- match e1.eexpr with
- | TField(fexpr, field) ->
- let tmp = mk_temp gen "getvar" fexpr.etype in
- let var = { eexpr = TVar(tmp, Some(run fexpr)); etype = gen.gcon.basic.tvoid; epos = e.epos } in
- (Some var, { eexpr = TField( { fexpr with eexpr = TLocal(tmp) }, field); etype = etype; epos = e1.epos })
- | _ ->
- (None, e1)
- in
- (match flag with
- | Prefix ->
- let block = (match var with | Some e -> [e] | None -> []) @
- [
- mk_cast etype { e with eexpr = TBinop(OpAssign, getvar,{ eexpr = TBinop(op, mk_cast etype getvar, one); etype = etype; epos = e.epos }); etype = getvar.etype; }
- ]
- in
- { eexpr = TBlock(block); etype = etype; epos = e.epos }
- | Postfix ->
- let ret = mk_temp gen "ret" etype in
- let vars = (match var with Some e -> [e] | None -> []) @ [{ eexpr = TVar(ret, Some (mk_cast etype getvar)); etype = gen.gcon.basic.tvoid; epos = e.epos }] in
- let retlocal = { eexpr = TLocal(ret); etype = etype; epos = e.epos } in
- let block = vars @
- [
- { e with eexpr = TBinop(OpAssign, getvar, { eexpr = TBinop(op, retlocal, one); etype = getvar.etype; epos = e.epos }) };
- retlocal
- ] in
- { eexpr = TBlock(block); etype = etype; epos = e.epos }
- )
- | TUnop (op, flag, e1) when should_change e ->
- let etype = match op with | Not -> gen.gcon.basic.tbool | _ -> gen.gcon.basic.tint in
- mk_paren { eexpr = TUnop(op, flag, mk_cast etype (run e1)); etype = etype; epos = e.epos }
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:"dyn_ops" ~priority:(PCustom priority) map
- let configure_as_synf gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:"dyn_ops" ~priority:(PCustom priority_as_synf) map
- end;;
- (* ******************************************* *)
- (* Dynamic Field Access *)
- (* ******************************************* *)
- (*
- This module will filter every dynamic field access in haxe.
- On platforms that do not support dynamic access, it is with this that you should
- replace dynamic calls with x.field / Reflect.setField calls, and guess what -
- this is the default implemenation!
- Actually there is a problem with Reflect.setField because it returns void, which is a bad thing for us,
- so even in the default implementation, the function call should be specified to a Reflect.setField version that returns
- the value that was set
- (TODO: should it be separated?)
- As a plus, the default implementation adds something that doesn't hurt anybody, it looks for
- TAnon with Statics / EnumStatics field accesses and transforms them into real static calls.
- This means it will take this
- var m = Math;
- for (i in 0...1000) m.cos(10);
- which is an optimization in dynamic platforms, but performs horribly on strongly typed platforms
- and transform into:
- var m = Math;
- for (i in 0...1000) Math.cos(10);
- (addendum:)
- configure_generate_classes will already take care of generating the reflection-enabled class fields and calling abstract_implementation
- with the right arguments.
- Also
- depends on:
- (ok) must run AFTER Binop/Unop handler - so Unops / Binops are already unrolled
- *)
- module DynamicFieldAccess =
- struct
- let name = "dynamic_field_access"
- let priority = solve_deps name [DAfter DynamicOperators.priority]
- let priority_as_synf = solve_deps name [DAfter DynamicOperators.priority_as_synf]
- (*
- is_dynamic (expr) (field_access_expr) (field) : a function that indicates if the field access should be changed
- change_expr (expr) (field_access_expr) (field) (setting expr) (is_unsafe) : changes the expression
- call_expr (expr) (field_access_expr) (field) (call_params) : changes a call expression
- *)
- let abstract_implementation gen (is_dynamic:texpr->texpr->Type.tfield_access->bool) (change_expr:texpr->texpr->string->texpr option->bool->texpr) (call_expr:texpr->texpr->string->texpr list->texpr) =
- let rec run e =
- match e.eexpr with
- (* class types *)
- | TField(fexpr, f) when is_some (anon_class fexpr.etype) ->
- let decl = get (anon_class fexpr.etype) in
- let name = field_name f in
- (try
- match decl with
- | TClassDecl cl ->
- let cf = PMap.find name cl.cl_statics in
- { e with eexpr = TField({ fexpr with eexpr = TTypeExpr decl }, FStatic(cl, cf)) }
- | TEnumDecl en ->
- let ef = PMap.find name en.e_constrs in
- { e with eexpr = TField({ fexpr with eexpr = TTypeExpr decl }, FEnum(en, ef)) }
- | TAbstractDecl _ -> (* abstracts don't have TFields *) assert false
- | TTypeDecl _ -> (* anon_class doesn't return TTypeDecl *) assert false
- with
- | Not_found -> match f with
- | FStatic(cl,cf) when Meta.has Meta.Extern cf.cf_meta ->
- { e with eexpr = TField({ fexpr with eexpr = TTypeExpr decl }, FStatic(cl, cf)) }
- | _ ->
- change_expr e { fexpr with eexpr = TTypeExpr decl } (field_name f) None true
- )
- | TField(fexpr, f) when is_dynamic e fexpr (f) ->
- change_expr e (run fexpr) (field_name f) None true
- | TCall(
- { eexpr = TField(_, FStatic({ cl_path = ([], "Reflect") }, { cf_name = "field" })) } ,
- [obj; { eexpr = TConst(TString(field)) }]
- ) ->
- let t = match gen.greal_type obj.etype with
- | TDynamic _ | TAnon _ | TMono _ -> t_dynamic
- | t -> t
- in
- change_expr (mk_field_access gen { obj with etype = t } field obj.epos) (run obj) field None false
- | TCall(
- { eexpr = TField(_, FStatic({ cl_path = ([], "Reflect") }, { cf_name = "setField" } )) },
- [obj; { eexpr = TConst(TString(field)) }; evalue]
- ) ->
- change_expr (mk_field_access gen obj field obj.epos) (run obj) field (Some (run evalue)) false
- | TBinop(OpAssign, ({eexpr = TField(fexpr, f)}), evalue) when is_dynamic e fexpr (f) ->
- change_expr e (run fexpr) (field_name f) (Some (run evalue)) true
- | TBinop(OpAssign, { eexpr = TField(fexpr, f) }, evalue) ->
- (match field_access_esp gen fexpr.etype (f) with
- | FClassField(_,_,_,cf,false,t,_) when (try PMap.find cf.cf_name gen.gbase_class_fields == cf with Not_found -> false) ->
- change_expr e (run fexpr) (field_name f) (Some (run evalue)) true
- | _ -> Type.map_expr run e
- )
- (* #if debug *)
- | TBinop(OpAssignOp op, ({eexpr = TField(fexpr, f)}), evalue) when is_dynamic e fexpr (f) -> assert false (* this case shouldn't happen *)
- | TUnop(Increment, _, ({eexpr = TField( ( { eexpr=TLocal(local) } as fexpr ), f)}))
- | TUnop(Decrement, _, ({eexpr = TField( ( { eexpr=TLocal(local) } as fexpr ), f)})) when is_dynamic e fexpr (f) -> assert false (* this case shouldn't happen *)
- (* #end *)
- | TCall( ({ eexpr = TField(fexpr, f) }), params ) when is_dynamic e fexpr (f) ->
- call_expr e (run fexpr) (field_name f) (List.map run params)
- | _ -> Type.map_expr run e
- in run
- (*
- this function will already configure with the abstract implementation, and also will create the needed class fields to
- enable reflection on platforms that don't support reflection.
- this means it will create the following class methods:
- - getField(field, isStatic) - gets the value of the field. isStatic
- - setField -
- -
- *)
- let configure_generate_classes gen optimize (runtime_getset_field:texpr->texpr->string->texpr option->texpr) (runtime_call_expr:texpr->texpr->string->texpr list->texpr) =
- ()
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:"dynamic_field_access" ~priority:(PCustom(priority)) map
- let configure_as_synf gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:"dynamic_field_access" ~priority:(PCustom(priority_as_synf)) map
- end;;
- (* ******************************************* *)
- (* Closure Detection *)
- (* ******************************************* *)
- (*
- Just a small utility filter that detects when a closure must be created.
- On the default implementation, this means when a function field is being accessed
- not via reflection and not to be called instantly
- dependencies:
- must run after DynamicFieldAccess, so any TAnon { Statics / EnumStatics } will be changed to the corresponding TTypeExpr
- *)
- module FilterClosures =
- struct
- let name = "filter_closures"
- let priority = solve_deps name [DAfter DynamicFieldAccess.priority]
- let traverse gen (should_change:texpr->string->bool) (filter:texpr->texpr->string->bool->texpr) =
- let rec run e =
- match e.eexpr with
- (*(* this is precisely the only case where we won't even ask if we should change, because it is a direct use of TClosure *)
- | TCall ( {eexpr = TClosure(e1,s)} as clos, args ) ->
- { e with eexpr = TCall({ clos with eexpr = TClosure(run e1, s) }, List.map run args ) }
- | TCall ( clos, args ) ->
- let rec loop clos = match clos.eexpr with
- | TClosure(e1,s) -> Some (clos, e1, s)
- | TParenthesis p -> loop p
- | _ -> None
- in
- let clos = loop clos in
- (match clos with
- | Some (clos, e1, s) -> { e with eexpr = TCall({ clos with eexpr = TClosure(run e1, s) }, List.map run args ) }
- | None -> Type.map_expr run e)*)
- | TCall({ eexpr = TLocal{ v_name = "__delegate__" } } as local, [del]) ->
- { e with eexpr = TCall(local, [Type.map_expr run del]) }
- | TCall(({ eexpr = TField(_, _) } as ef), params) ->
- { e with eexpr = TCall(Type.map_expr run ef, List.map run params) }
- | TField(ef, FEnum(en, field)) ->
- (* FIXME replace t_dynamic with actual enum Anon field *)
- let ef = run ef in
- (match follow field.ef_type with
- | TFun _ when should_change ef field.ef_name ->
- filter e ef field.ef_name true
- | _ ->
- { e with eexpr = TField(ef, FEnum(en,field)) }
- )
- | TField(({ eexpr = TTypeExpr _ } as tf), f) ->
- (match field_access_esp gen tf.etype (f) with
- | FClassField(_,_,_,cf,_,_,_) ->
- (match cf.cf_kind with
- | Method(MethDynamic)
- | Var _ ->
- e
- | _ when should_change tf cf.cf_name ->
- filter e tf cf.cf_name true
- | _ ->
- e
- )
- | _ -> e)
- | TField(e1, FClosure (Some _, cf)) when should_change e1 cf.cf_name ->
- (match cf.cf_kind with
- | Method MethDynamic | Var _ ->
- Type.map_expr run e
- | _ ->
- filter e (run e1) cf.cf_name false)
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* Dynamic TArray Handling *)
- (* ******************************************* *)
- (*
- In some languages you cannot overload the [] operator,
- so we need to decide what is kept as TArray and what gets mapped.
- - in order to do this you must ensure that
- depends on:
- (syntax) must run before expression/statment normalization because it may generate complex expressions
- (ok) must run before binop transformations because it may generate some untreated binop ops
- (ok) must run before dynamic field access is transformed into reflection
- *)
- module TArrayTransform =
- struct
- let name = "dyn_tarray"
- let priority = solve_deps name [DBefore DynamicOperators.priority; DBefore DynamicFieldAccess.priority]
- let priority_as_synf = solve_deps name [DBefore DynamicOperators.priority_as_synf; DBefore DynamicFieldAccess.priority_as_synf]
- (* should change signature: tarray expr -> binop operation -> should change? *)
- let default_implementation gen (should_change:texpr->Ast.binop option->bool) (get_fun:string) (set_fun:string) =
- let basic = gen.gcon.basic in
- let mk_get e e1 e2 =
- let efield = mk_field_access gen e1 get_fun e.epos in
- { e with eexpr = TCall(efield, [e2]) }
- in
- let mk_set e e1 e2 evalue =
- let efield = mk_field_access gen e1 set_fun e.epos in
- { e with eexpr = TCall(efield, [e2; evalue]) }
- in
- let rec run e =
- match e.eexpr with
- | TArray(e1, e2) ->
- (* e1 should always be a var; no need to map there *)
- if should_change e None then mk_get e (run e1) (run e2) else Type.map_expr run e
- | TBinop (Ast.OpAssign, ({ eexpr = TArray(e1a,e2a) } as earray), evalue) when should_change earray (Some Ast.OpAssign) ->
- mk_set e (run e1a) (run e2a) (run evalue)
- | TBinop (Ast.OpAssignOp op,({ eexpr = TArray(e1a,e2a) } as earray) , evalue) when should_change earray (Some (Ast.OpAssignOp op)) ->
- (* cache all arguments in vars so they don't get executed twice *)
- (* let ensure_local gen block name e = *)
- let block = ref [] in
- let arr_local = ensure_local gen block "array" (run e1a) in
- let idx_local = ensure_local gen block "index" (run e2a) in
- block := (mk_set e arr_local idx_local ( { e with eexpr=TBinop(op, mk_get earray arr_local idx_local, run evalue) } )) :: !block;
- { e with eexpr = TBlock (List.rev !block) }
- | TUnop(op, flag, ({ eexpr = TArray(e1a, e2a) } as earray)) ->
- if should_change earray None && match op with | Not | Neg -> false | _ -> true then begin
- let block = ref [] in
- let actual_t = match op with
- | Ast.Increment | Ast.Decrement -> (match follow earray.etype with
- | TInst _ | TAbstract _ | TEnum _ -> earray.etype
- | _ -> basic.tfloat)
- | Ast.Not -> basic.tbool
- | _ -> basic.tint
- in
- let val_v = mk_temp gen "arrVal" actual_t in
- let ret_v = mk_temp gen "arrRet" actual_t in
- let arr_local = ensure_local gen block "arr" (run e1a) in
- let idx_local = ensure_local gen block "arrIndex" (run e2a) in
- let val_local = { earray with eexpr = TLocal(val_v) } in
- let ret_local = { earray with eexpr = TLocal(ret_v) } in
- (* var idx = 1; var val = x._get(idx); var ret = val++; x._set(idx, val); ret; *)
- block := { eexpr = TVar(val_v, Some(mk_get earray arr_local idx_local)); (* var val = x._get(idx) *)
- etype = gen.gcon.basic.tvoid;
- epos = e2a.epos
- } :: !block;
- block := { eexpr = TVar(ret_v, Some { e with eexpr = TUnop(op, flag, val_local) }); (* var ret = val++ *)
- etype = gen.gcon.basic.tvoid;
- epos = e2a.epos
- } :: !block;
- block := (mk_set e arr_local idx_local val_local) (*x._set(idx,val)*) :: !block;
- block := ret_local :: !block;
- { e with eexpr = TBlock (List.rev !block) }
- end else
- Type.map_expr run e
- | _ -> Type.map_expr run e
- in run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:"dyn_tarray" ~priority:(PCustom priority) map
- let configure_as_synf gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:"dyn_tarray" ~priority:(PCustom priority_as_synf) map
- end;;
- (* ******************************************* *)
- (* Try / Catch + throw native types handling *)
- (* ******************************************* *)
- (*
- Some languages/vm's do not support throwing any kind of value. For them, only
- special kinds of objects can be thrown. Because of this, we must wrap some throw
- statements with an expression, and also we must unwrap it on the catch() phase, and
- maybe manually test with Std.is()
- dependencies:
- must run before dynamic field access (?) TODO review
- It's a syntax filter, as it alters types (throw wrapper)
- *)
- module TryCatchWrapper =
- struct
- let priority = solve_deps "try_catch" [DBefore DynamicFieldAccess.priority]
- (*
- should_wrap : does the type should be wrapped? This of course works on the reverse way, so it tells us if the type should be unwrapped as well
- wrap_throw : the wrapper for throw (throw expr->expr inside throw->returning wrapped expression)
- unwrap_expr : the other way around : given the catch var (maybe will need casting to wrapper_type) , return the unwrap expr
- rethrow_expr : how to rethrow ane exception in the platform
- catchall_type : the class used for catchall (e:Dynamic)
- wrapper_type : the wrapper type, so we can test if exception is of type 'wrapper'
- catch_map : maps the catch expression to include some intialization code (e.g. setting up Stack.exceptionStack)
- *)
- let traverse gen (should_wrap:t->bool) (wrap_throw:texpr->texpr->texpr) (unwrap_expr:tvar->pos->texpr) (rethrow_expr:texpr->texpr) (catchall_type:t) (wrapper_type:t) (catch_map:tvar->texpr->texpr) =
- let rec run e =
- match e.eexpr with
- | TThrow texpr when should_wrap texpr.etype -> wrap_throw e (run texpr)
- | TTry (ttry, catches) ->
- let nowrap_catches, must_wrap_catches, catchall = List.fold_left (fun (nowrap_catches, must_wrap_catches, catchall) (v, catch) ->
- (* first we'll see if the type is Dynamic (catchall) *)
- match follow v.v_type with
- | TDynamic _ ->
- assert (is_none catchall);
- (nowrap_catches, must_wrap_catches, Some(v,run catch))
- (* see if we should unwrap it *)
- | _ when should_wrap (follow v.v_type) ->
- (nowrap_catches, (v,run catch) :: must_wrap_catches, catchall)
- | _ ->
- ( (v,catch_map v (run catch)) :: nowrap_catches, must_wrap_catches, catchall )
- ) ([], [], None) catches
- in
- (*
- 1st catch all nowrap "the easy way"
- 2nd see if there are any must_wrap or catchall. If there is,
- do a catchall first with a temp var.
- then get catchall var (as dynamic) (or create one), and declare it = catchall exception
- then test if it is of type wrapper_type. If it is, unwrap it
- then start doing Std.is() tests for each catch type
- if there is a catchall in the end, end with it. If there isn't, rethrow
- *)
- let dyn_catch = match (catchall, must_wrap_catches) with
- | Some (v,c), _
- | _, (v, c) :: _ ->
- let pos = c.epos in
- let temp_var = mk_temp gen "catchallException" catchall_type in
- let temp_local = { eexpr=TLocal(temp_var); etype = temp_var.v_type; epos = pos } in
- let catchall_var = (*match catchall with
- | None -> *) mk_temp gen "catchall" t_dynamic
- (*| Some (v,_) -> v*)
- in
- let catchall_decl = { eexpr = TVar(catchall_var, Some(temp_local)); etype=gen.gcon.basic.tvoid; epos = pos } in
- let catchall_local = { eexpr = TLocal(catchall_var); etype = t_dynamic; epos = pos } in
- (* if it is of type wrapper_type, unwrap it *)
- let std_is = mk_static_field_access (get_cl (get_type gen ([],"Std"))) "is" (TFun(["v",false,t_dynamic;"cl",false,mt_to_t (get_type gen ([], "Class")) [t_dynamic]],gen.gcon.basic.tbool)) pos in
- let mk_std_is t pos = { eexpr = TCall(std_is, [catchall_local; mk_mt_access (t_to_mt t) pos]); etype = gen.gcon.basic.tbool; epos = pos } in
- let if_is_wrapper_expr = { eexpr = TIf(mk_std_is wrapper_type pos,
- { eexpr = TBinop(OpAssign, catchall_local, unwrap_expr temp_var pos); etype = t_dynamic; epos = pos }
- , None); etype = gen.gcon.basic.tvoid; epos = pos } in
- let rec loop must_wrap_catches = match must_wrap_catches with
- | (vcatch,catch) :: tl ->
- { eexpr = TIf(mk_std_is vcatch.v_type catch.epos,
- { eexpr = TBlock({ eexpr=TVar(vcatch, Some(mk_cast vcatch.v_type catchall_local)); etype=gen.gcon.basic.tvoid; epos=catch.epos } :: [catch] ); etype = catch.etype; epos = catch.epos },
- Some (loop tl));
- etype = catch.etype; epos = catch.epos }
- | [] ->
- match catchall with
- | Some (v,s) ->
- Type.concat { eexpr = TVar(v, Some(catchall_local)); etype = gen.gcon.basic.tvoid; epos = pos } s
- | None ->
- mk_block (rethrow_expr temp_local)
- in
- [ ( temp_var, catch_map temp_var { e with eexpr = TBlock([ catchall_decl; if_is_wrapper_expr; loop must_wrap_catches ]) } ) ]
- | _ ->
- []
- in
- { e with eexpr = TTry(run ttry, (List.rev nowrap_catches) @ dyn_catch) }
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:"try_catch" ~priority:(PCustom priority) map
- end;;
- let fun_args = List.map (function | (v,s) -> (v.v_name, (match s with | None -> false | Some _ -> true), v.v_type))
- (* ******************************************* *)
- (* Closures To Class *)
- (* ******************************************* *)
- (*
- This is a very important filter. It will take all anonymous functions from the AST, will search for all captured variables, and will create a class
- that implements an abstract interface for calling functions. This is very important for targets that don't support anonymous functions to work correctly.
- Also it is possible to implement some strategies to avoid value type boxing, such as NaN tagging or double/object arguments. All this will be abstracted away
- from this interface.
- dependencies:
- must run after dynamic field access, because of conflicting ways to deal with invokeField
- (module filter) must run after OverloadingCtor so we can also change the dynamic function expressions
- uses TArray expressions for array. TODO see interaction
- uses TThrow expressions.
- *)
- module ClosuresToClass =
- struct
- let name = "closures_to_class"
- let priority = solve_deps name [ DAfter DynamicFieldAccess.priority ]
- let priority_as_synf = solve_deps name [ DAfter DynamicFieldAccess.priority_as_synf ]
- type closures_ctx =
- {
- fgen : generator_ctx;
- mutable func_class : tclass;
- (*
- this is what will actually turn the function into class field.
- The standard implementation by default will already take care of creating the class, and setting the captured variables.
- It will also return the super arguments to be called
- *)
- mutable closure_to_classfield : tfunc->t->pos->tclass_field * (texpr list);
- (*
- when a dynamic function call is made, we need to convert it as if it were calling the dynamic function interface.
- TCall expr -> new TCall expr
- *)
- mutable dynamic_fun_call : texpr->texpr;
- (*
- called once so the implementation can make one of a time initializations in the base class
- for all functions
- *)
- mutable initialize_base_class : tclass->unit;
- (*
- Base classfields are the class fields for the abstract implementation of either the Function implementation,
- or the invokeField implementation for the classes
- They will either try to call the right function or will fail with
- (tclass - subject (so we know the type of this)) -> is_function_base -> additional arguments for each function (at the beginning) -> list of the abstract implementation class fields
- *)
- mutable get_base_classfields_for : tclass->bool->(unit->(tvar * tconstant option) list)->tclass_field list;
- (*
- This is a more complex version of get_base_classfields_for.
- It's meant to provide a toolchain so we can easily create classes that extend Function
- and add more functionality on top of it.
- arguments:
- tclass -> subject (so we know the type of this)
- bool -> is it a function type
- ( int -> (int->t->tconstant option->texpr) -> ( (tvar * tconstant option) list * texpr) )
- int -> current arity of the function whose member will be mapped; -1 for dynamic function. It is guaranteed that dynamic function will be called last
- t -> the return type of the function
- (int->t->tconstant option->texpr) -> api to get exprs that unwrap arguments correctly
- int -> argument wanted to unwrap
- t -> solicited type
- tconstant option -> map to this default value if null
- returns a texpr that tells how the default
- should return a list with additional arguments (only works if is_function_base = true)
- and the underlying function expression
- *)
- mutable map_base_classfields : tclass->bool->( int -> t -> (tvar list) -> (int->t->tconstant option->texpr) -> ( (tvar * tconstant option) list * texpr) )->tclass_field list;
- mutable transform_closure : texpr->texpr->string->texpr;
- }
- type map_info = {
- in_unsafe : bool;
- in_unused : bool;
- }
- let null_map_info = { in_unsafe = false; in_unused = false; }
- (*
- the default implementation will take 3 transformation functions:
- * one that will transform closures that are not called immediately (instance.myFunc).
- normally on this case it's best to have a runtime handler that will take the instance, the function and call its invokeField when invoked
- * one that will actually handle the anonymous functions themselves.
- * one that will transform calling a dynamic function. So for example, dynFunc(arg1, arg2) might turn into dynFunc.apply2(arg1, arg2);
- ( suspended ) * an option to match papplied functions
- * handling parameterized anonymous function declaration (optional - tparam_anon_decl and tparam_anon_acc)
- *)
- let rec cleanup_delegate e = match e.eexpr with
- | TParenthesis e | TMeta(_,e)
- | TCast(e,_) -> cleanup_delegate e
- | _ -> e
- let funct gen t = match follow (run_follow gen t) with
- | TFun(args,ret) -> args,ret
- | _ -> raise Not_found
- let mk_conversion_fun gen e =
- let args, ret = funct gen e.etype in
- let tf_args = List.map (fun (n,o,t) -> alloc_var n t,None) args in
- let block, local = match e.eexpr with
- | TLocal v ->
- v.v_capture <- true;
- [],e
- | _ ->
- let tmp = mk_temp gen "delegate_conv" e.etype in
- tmp.v_capture <- true;
- [{ eexpr = TVar(tmp,Some e); etype = gen.gcon.basic.tvoid; epos = e.epos }], mk_local tmp e.epos
- in
- let body = {
- eexpr = TCall(local, List.map (fun (v,_) -> mk_local v e.epos) tf_args);
- etype = ret;
- epos = e.epos;
- } in
- let body = if not (is_void ret) then
- { body with eexpr = TReturn( Some body ) }
- else
- body
- in
- let body = {
- eexpr = TBlock(block @ [body]);
- etype = body.etype;
- epos = body.epos;
- } in
- {
- tf_args = tf_args;
- tf_expr = body;
- tf_type = ret;
- }
- let traverse gen ?tparam_anon_decl ?tparam_anon_acc (transform_closure:texpr->texpr->string->texpr) (handle_anon_func:texpr->tfunc->map_info->t option->texpr) (dynamic_func_call:texpr->texpr) e =
- let info = ref null_map_info in
- let rec run e =
- match e.eexpr with
- | TCast({ eexpr = TCall({ eexpr = TLocal{ v_name = "__delegate__" } } as local, [del] ) } as e2, _) ->
- let e2 = { e2 with etype = e.etype } in
- let replace_delegate ex =
- { e with eexpr = TCast({ e2 with eexpr = TCall(local, [ex]) }, None) }
- in
- (* found a delegate; let's see if it's a closure or not *)
- let clean = cleanup_delegate del in
- (match clean.eexpr with
- | TField( ef, (FClosure _ as f)) | TField( ef, (FStatic _ as f)) ->
- (* a closure; let's leave this unchanged for FilterClosures to handle it *)
- replace_delegate { clean with eexpr = TField( run ef, f ) }
- | TFunction tf ->
- (* handle like we'd handle a normal function, but create an unchanged closure field for it *)
- let ret = handle_anon_func clean { tf with tf_expr = run tf.tf_expr } !info (Some e.etype) in
- replace_delegate ret
- | _ -> try
- let tf = mk_conversion_fun gen del in
- let ret = handle_anon_func del { tf with tf_expr = run tf.tf_expr } !info (Some e.etype) in
- replace_delegate ret
- with Not_found ->
- gen.gcon.error "This delegate construct is unsupported" e.epos;
- replace_delegate (run clean))
- | TCall(({ eexpr = TLocal{ v_name = "__unsafe__" } } as local), [arg]) ->
- let old = !info in
- info := { !info with in_unsafe = true };
- let arg2 = run arg in
- info := old;
- { e with eexpr = TCall(local,[arg2]) }
- (* parameterized functions handling *)
- | TVar(vv, ve) -> (match tparam_anon_decl with
- | None -> Type.map_expr run e
- | Some tparam_anon_decl ->
- (match (vv, ve) with
- | ({ v_extra = Some( _ :: _, _) } as v), Some ({ eexpr = TFunction tf } as f)
- | ({ v_extra = Some( _ :: _, _) } as v), Some { eexpr = TArrayDecl([{ eexpr = TFunction tf } as f]) | TCall({ eexpr = TLocal { v_name = "__array__" } }, [{ eexpr = TFunction tf } as f]) } -> (* captured transformation *)
- ignore(tparam_anon_decl v f { tf with tf_expr = run tf.tf_expr });
- { e with eexpr = TBlock([]) }
- | _ ->
- Type.map_expr run { e with eexpr = TVar(vv, ve) })
- )
- | TLocal ({ v_extra = Some( _ :: _, _) } as v)
- | TArray ({ eexpr = TLocal ({ v_extra = Some( _ :: _, _) } as v) }, _) -> (* captured transformation *)
- (match tparam_anon_acc with
- | None -> Type.map_expr run e
- | Some tparam_anon_acc -> tparam_anon_acc v e)
- | TCall( { eexpr = TField(_, FEnum _) }, _ ) ->
- Type.map_expr run e
- (* if a TClosure is being call immediately, there's no need to convert it to a TClosure *)
- | TCall(( { eexpr = TField(ecl,f) } as e1), params) ->
- (* check to see if called field is known and if it is a MethNormal (only MethNormal fields can be called directly) *)
- (* let name = field_name f in *)
- (match field_access_esp gen (gen.greal_type ecl.etype) f with
- | FClassField(_,_,_,cf,_,_,_) ->
- (match cf.cf_kind with
- | Method MethNormal
- | Method MethInline ->
- { e with eexpr = TCall({ e1 with eexpr = TField(run ecl, f) }, List.map run params) }
- | _ ->
- match gen.gfollow#run_f e1.etype with
- | TFun _ ->
- dynamic_func_call { e with eexpr = TCall(run e1, List.map run params) }
- | _ ->
- let i = ref 0 in
- let t = TFun(List.map (fun e -> incr i; "arg" ^ (string_of_int !i), false, e.etype) params, e.etype) in
- dynamic_func_call { e with eexpr = TCall( mk_cast t (run e1), List.map run params ) }
- )
- (* | FNotFound ->
- { e with eexpr = TCall({ e1 with eexpr = TField(run ecl, f) }, List.map run params) }
- (* expressions by now may have generated invalid expressions *) *)
- | _ ->
- match gen.gfollow#run_f e1.etype with
- | TFun _ ->
- dynamic_func_call { e with eexpr = TCall(run e1, List.map run params) }
- | _ ->
- let i = ref 0 in
- let t = TFun(List.map (fun e -> incr i; "arg" ^ (string_of_int !i), false, e.etype) params, e.etype) in
- dynamic_func_call { e with eexpr = TCall( mk_cast t (run e1), List.map run params ) }
- )
- | TField(ecl, FClosure (_,cf)) ->
- transform_closure e (run ecl) cf.cf_name
- | TFunction tf ->
- handle_anon_func e { tf with tf_expr = run tf.tf_expr } !info None
- | TCall({ eexpr = TConst(TSuper) }, _) ->
- Type.map_expr run e
- | TCall({ eexpr = TLocal(v) }, args) when String.get v.v_name 0 = '_' && Hashtbl.mem gen.gspecial_vars v.v_name ->
- Type.map_expr run e
- | TCall(tc,params) ->
- let i = ref 0 in
- let may_cast = match gen.gfollow#run_f tc.etype with
- | TFun _ -> fun e -> e
- | _ ->
- let t = TFun(List.map (fun e ->
- incr i;
- ("p" ^ (string_of_int !i), false, e.etype)
- ) params, e.etype)
- in
- fun e -> mk_cast t e
- in
- dynamic_func_call { e with eexpr = TCall(run (may_cast tc), List.map run params) }
- | _ -> Type.map_expr run e
- in
- (match e.eexpr with
- | TFunction(tf) -> Type.map_expr run e
- | _ -> run e)
- let rec get_type_params acc t =
- match t with
- | TInst(( { cl_kind = KTypeParameter _ } as cl), []) ->
- if List.memq cl acc then acc else cl :: acc
- | TFun (params,tret) ->
- List.fold_left get_type_params acc ( tret :: List.map (fun (_,_,t) -> t) params )
- | TDynamic t ->
- (match t with | TDynamic _ -> acc | _ -> get_type_params acc t)
- | TAbstract (a, pl) when not (Meta.has Meta.CoreType a.a_meta) ->
- get_type_params acc ( Abstract.get_underlying_type a pl)
- | TAnon a ->
- PMap.fold (fun cf acc ->
- let params = List.map (fun (_,t) -> match follow t with
- | TInst(c,_) -> c
- | _ -> assert false) cf.cf_params
- in
- List.filter (fun t -> not (List.memq t params)) (get_type_params acc cf.cf_type)
- ) a.a_fields acc
- | TType(_, [])
- | TAbstract (_, [])
- | TInst(_, [])
- | TEnum(_, []) ->
- acc
- | TType(_, params)
- | TAbstract(_, params)
- | TEnum(_, params)
- | TInst(_, params) ->
- List.fold_left get_type_params acc params
- | TMono r -> (match !r with
- | Some t -> get_type_params acc t
- | None -> acc)
- | _ -> get_type_params acc (follow_once t)
- let get_captured expr =
- let ret = Hashtbl.create 1 in
- let ignored = Hashtbl.create 0 in
- let params = ref [] in
- let check_params t = params := get_type_params !params t in
- let rec traverse expr =
- match expr.eexpr with
- | TFor (v, _, _) ->
- Hashtbl.add ignored v.v_id v;
- check_params v.v_type;
- Type.iter traverse expr
- | TFunction(tf) ->
- List.iter (fun (v,_) -> Hashtbl.add ignored v.v_id v) tf.tf_args;
- (match follow expr.etype with
- | TFun(args,ret) ->
- List.iter (fun (_,_,t) ->
- check_params t
- ) args;
- check_params ret
- | _ -> ());
- Type.iter traverse expr
- | TVar (v, opt) ->
- (match v.v_extra with
- | Some(_ :: _, _) -> ()
- | _ ->
- check_params v.v_type);
- Hashtbl.add ignored v.v_id v;
- ignore(Option.map traverse opt)
- | TLocal { v_extra = Some( (_ :: _ ),_) } ->
- ()
- | TLocal(( { v_capture = true } ) as v) ->
- (if not (Hashtbl.mem ignored v.v_id || Hashtbl.mem ret v.v_id) then begin check_params v.v_type; Hashtbl.replace ret v.v_id expr end);
- | _ -> Type.iter traverse expr
- in traverse expr;
- ret, !params
- (*
- OPTIMIZEME:
- Take off from Codegen the code that wraps captured variables,
- traverse through all variables, looking for their use (just like local_usage)
- three possible outcomes for captured variables:
- - become a function member variable <- best performance.
- Will not work on functions that can be created more than once (functions inside a loop or functions inside functions)
- The function will have to be created on top of the block, so its variables can be filled in instead of being declared
- - single-element array - the most compatible way, though also creates a slight overhead.
- - we'll have some labels for captured variables:
- - used in loop
- *)
- (*
- The default implementation will impose a naming convention:
- invoke(arity)_(o for returning object/d for returning double) when arity < max_arity
- invoke_dynamic_(o/d) when arity > max_arity
- This means that it also imposes that the dynamic function return types may only be Dynamic or Float, and all other basic types must be converted to/from it.
- *)
- let default_implementation ft parent_func_class (* e.g. new haxe.lang.ClassClosure *) =
- let gen = ft.fgen in
- ft.initialize_base_class parent_func_class;
- let cfs = ft.get_base_classfields_for parent_func_class true (fun () -> []) in
- List.iter (fun cf ->
- (if cf.cf_name = "new" then parent_func_class.cl_constructor <- Some cf else
- parent_func_class.cl_fields <- PMap.add cf.cf_name cf parent_func_class.cl_fields
- )
- ) cfs;
- parent_func_class.cl_ordered_fields <- (List.filter (fun cf -> cf.cf_name <> "new") cfs) @ parent_func_class.cl_ordered_fields;
- ft.func_class <- parent_func_class;
- let handle_anon_func fexpr tfunc mapinfo delegate_type : texpr * (tclass * texpr list) =
- let gen = ft.fgen in
- let in_unsafe = mapinfo.in_unsafe || match gen.gcurrent_class, gen.gcurrent_classfield with
- | Some c, _ when Meta.has Meta.Unsafe c.cl_meta -> true
- | _, Some cf when Meta.has Meta.Unsafe cf.cf_meta -> true
- | _ -> false
- in
- (* get all captured variables it uses *)
- let captured_ht, tparams = get_captured fexpr in
- let captured = Hashtbl.fold (fun _ e acc -> e :: acc) captured_ht [] in
- (*let cltypes = List.map (fun cl -> (snd cl.cl_path, TInst(map_param cl, []) )) tparams in*)
- let cltypes = List.map (fun cl -> (snd cl.cl_path, TInst(cl, []) )) tparams in
- (* create a new class that extends abstract function class, with a ctor implementation that will setup all captured variables *)
- let cfield = match ft.fgen.gcurrent_classfield with
- | None -> "Anon"
- | Some cf -> cf.cf_name
- in
- let cur_line = Lexer.get_error_line fexpr.epos in
- let path = (fst ft.fgen.gcurrent_path, Printf.sprintf "%s_%s_%d__Fun" (snd ft.fgen.gcurrent_path) cfield cur_line) in
- let cls = mk_class (get ft.fgen.gcurrent_class).cl_module path tfunc.tf_expr.epos in
- if in_unsafe then cls.cl_meta <- (Meta.Unsafe,[],Ast.null_pos) :: cls.cl_meta;
- if Common.defined gen.gcon Define.EraseGenerics then begin
- cls.cl_meta <- (Meta.HaxeGeneric,[],Ast.null_pos) :: cls.cl_meta
- end;
- cls.cl_module <- (get ft.fgen.gcurrent_class).cl_module;
- cls.cl_params <- cltypes;
- let mk_this v pos =
- {
- (mk_field_access gen { eexpr = TConst TThis; etype = TInst(cls, List.map snd cls.cl_params); epos = pos } v.v_name pos)
- with etype = v.v_type
- }
- in
- let mk_this_assign v pos =
- {
- eexpr = TBinop(OpAssign, mk_this v pos, { eexpr = TLocal(v); etype = v.v_type; epos = pos });
- etype = v.v_type;
- epos = pos
- } in
- (* mk_class_field name t public pos kind params *)
- let ctor_args, ctor_sig, ctor_exprs = List.fold_left (fun (ctor_args, ctor_sig, ctor_exprs) lexpr ->
- match lexpr.eexpr with
- | TLocal(v) ->
- let cf = mk_class_field v.v_name v.v_type false lexpr.epos (Var({ v_read = AccNormal; v_write = AccNormal; })) [] in
- cls.cl_fields <- PMap.add v.v_name cf cls.cl_fields;
- cls.cl_ordered_fields <- cf :: cls.cl_ordered_fields;
- let ctor_v = alloc_var v.v_name v.v_type in
- ((ctor_v, None) :: ctor_args, (v.v_name, false, v.v_type) :: ctor_sig, (mk_this_assign v cls.cl_pos) :: ctor_exprs)
- | _ -> assert false
- ) ([],[],[]) captured in
- (* change all captured variables to this.capturedVariable *)
- let rec change_captured e =
- match e.eexpr with
- | TLocal( ({ v_capture = true }) as v ) when Hashtbl.mem captured_ht v.v_id ->
- mk_this v e.epos
- | _ -> Type.map_expr change_captured e
- in
- let func_expr = change_captured tfunc.tf_expr in
- let invokecf, invoke_field, super_args = match delegate_type with
- | None -> (* no delegate *)
- let ifield, sa = ft.closure_to_classfield { tfunc with tf_expr = func_expr } fexpr.etype fexpr.epos in
- ifield,ifield,sa
- | Some _ ->
- let pos = cls.cl_pos in
- let cf = mk_class_field "Delegate" (TFun(fun_args tfunc.tf_args, tfunc.tf_type)) true pos (Method MethNormal) [] in
- cf.cf_expr <- Some { fexpr with eexpr = TFunction { tfunc with tf_expr = func_expr }; };
- cf.cf_meta <- (Meta.Final,[],pos) :: cf.cf_meta;
- cls.cl_ordered_fields <- cf :: cls.cl_ordered_fields;
- cls.cl_fields <- PMap.add cf.cf_name cf cls.cl_fields;
- (* invoke function body: call Delegate function *)
- let ibody = {
- eexpr = TCall({
- eexpr = TField({
- eexpr = TConst TThis;
- etype = TInst(cls, List.map snd cls.cl_params);
- epos = pos;
- }, FInstance(cls, List.map snd cls.cl_params, cf));
- etype = cf.cf_type;
- epos = pos;
- }, List.map (fun (v,_) -> mk_local v pos) tfunc.tf_args);
- etype = tfunc.tf_type;
- epos = pos
- } in
- let ibody = if not (is_void tfunc.tf_type) then
- { ibody with eexpr = TReturn( Some ibody ) }
- else
- ibody
- in
- let ifield, sa = ft.closure_to_classfield { tfunc with tf_expr = ibody } fexpr.etype fexpr.epos in
- cf,ifield,sa
- in
- (* create the constructor *)
- (* todo properly abstract how type var is set *)
- cls.cl_super <- Some(parent_func_class, []);
- let pos = cls.cl_pos in
- let super_call =
- {
- eexpr = TCall({ eexpr = TConst(TSuper); etype = TInst(parent_func_class,[]); epos = pos }, super_args);
- etype = ft.fgen.gcon.basic.tvoid;
- epos = pos;
- } in
- let ctor_type = (TFun(ctor_sig, ft.fgen.gcon.basic.tvoid)) in
- let ctor = mk_class_field "new" ctor_type true cls.cl_pos (Method(MethNormal)) [] in
- ctor.cf_expr <- Some(
- {
- eexpr = TFunction(
- {
- tf_args = ctor_args;
- tf_type = ft.fgen.gcon.basic.tvoid;
- tf_expr = { eexpr = TBlock(super_call :: ctor_exprs); etype = ft.fgen.gcon.basic.tvoid; epos = cls.cl_pos }
- });
- etype = ctor_type;
- epos = cls.cl_pos;
- });
- cls.cl_constructor <- Some(ctor);
- (* add invoke function to the class *)
- cls.cl_ordered_fields <- invoke_field :: cls.cl_ordered_fields;
- cls.cl_fields <- PMap.add invoke_field.cf_name invoke_field cls.cl_fields;
- cls.cl_overrides <- invoke_field :: cls.cl_overrides;
- (* add this class to the module with gadd_to_module *)
- ft.fgen.gadd_to_module (TClassDecl(cls)) priority;
- (* if there are no captured variables, we can create a cache so subsequent calls don't need to create a new function *)
- let expr, clscapt =
- match captured, tparams with
- | [], [] ->
- let cache_var = ft.fgen.gmk_internal_name "hx" "current" in
- let cache_cf = mk_class_field cache_var (TInst(cls,[])) false func_expr.epos (Var({ v_read = AccNormal; v_write = AccNormal })) [] in
- cls.cl_ordered_statics <- cache_cf :: cls.cl_ordered_statics;
- cls.cl_statics <- PMap.add cache_var cache_cf cls.cl_statics;
- (* if (FuncClass.hx_current != null) FuncClass.hx_current; else (FuncClass.hx_current = new FuncClass()); *)
- (* let mk_static_field_access cl field fieldt pos = *)
- let hx_current = mk_static_field_access cls cache_var (TInst(cls,[])) func_expr.epos in
- let pos = func_expr.epos in
- { fexpr with
- eexpr = TIf(
- {
- eexpr = TBinop(OpNotEq, hx_current, null (TInst(cls,[])) pos);
- etype = ft.fgen.gcon.basic.tbool;
- epos = pos;
- },
- hx_current,
- Some(
- {
- eexpr = TBinop(OpAssign, hx_current, { fexpr with eexpr = TNew(cls, [], captured) });
- etype = (TInst(cls,[]));
- epos = pos;
- }))
- }, (cls,captured)
- | _ ->
- (* change the expression so it will be a new "added class" ( captured variables arguments ) *)
- { fexpr with eexpr = TNew(cls, List.map (fun cl -> TInst(cl,[])) tparams, List.rev captured) }, (cls,captured)
- in
- match delegate_type with
- | None ->
- expr,clscapt
- | Some _ ->
- {
- eexpr = TField(expr, FClosure(Some (cls,[]),invokecf)); (* TODO: FClosure change *)
- etype = invokecf.cf_type;
- epos = cls.cl_pos
- }, clscapt
- in
- let tvar_to_cdecl = Hashtbl.create 0 in
- traverse
- ft.fgen
- ~tparam_anon_decl:(fun v e fn ->
- let _, info = handle_anon_func e fn null_map_info None in
- Hashtbl.add tvar_to_cdecl v.v_id info
- )
- ~tparam_anon_acc:(fun v e -> try
- let cls, captured = Hashtbl.find tvar_to_cdecl v.v_id in
- let types = match v.v_extra with
- | Some(t,_) -> t
- | _ -> assert false
- in
- let monos = List.map (fun _ -> mk_mono()) types in
- let vt = match follow v.v_type with
- | TInst(_, [v]) -> v
- | v -> v
- in
- let et = match follow e.etype with
- | TInst(_, [v]) -> v
- | v -> v
- in
- let original = apply_params types monos vt in
- unify et original;
- let monos = List.map (fun t -> apply_params types (List.map (fun _ -> t_dynamic) types) t) monos in
- let same_cl t1 t2 = match follow t1, follow t2 with
- | TInst(c,_), TInst(c2,_) -> c == c2
- | _ -> false
- in
- let passoc = List.map2 (fun (_,t) m -> t,m) types monos in
- let cltparams = List.map (fun (_,t) ->
- try
- snd (List.find (fun (t2,_) -> same_cl t t2) passoc)
- with | Not_found -> t) cls.cl_params
- in
- { e with eexpr = TNew(cls, cltparams, captured) }
- with
- | Not_found ->
- gen.gcon.warning "This expression may be invalid" e.epos;
- e
- | Unify_error el ->
- List.iter (fun el -> gen.gcon.warning (Typecore.unify_error_msg (print_context()) el) e.epos) el;
- gen.gcon.warning "This expression may be invalid" e.epos;
- e
- )
- (* (transform_closure:texpr->texpr->string->texpr) (handle_anon_func:texpr->tfunc->texpr) (dynamic_func_call:texpr->texpr->texpr list->texpr) *)
- ft.transform_closure
- (fun e f info delegate_type -> fst (handle_anon_func e f info delegate_type))
- ft.dynamic_fun_call
- (* (dynamic_func_call:texpr->texpr->texpr list->texpr) *)
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map
- let configure_as_synf gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority_as_synf) map
- (*
- this submodule will provide the default implementation for the C# and Java targets.
- it will have two return types: double and dynamic, and
- *)
- module DoubleAndDynamicClosureImpl =
- struct
- let get_ctx gen max_arity =
- let basic = gen.gcon.basic in
- let func_args_i i =
- let rec loop i (acc) =
- if i = 0 then (acc) else begin
- let vfloat = alloc_var (gen.gmk_internal_name "fn" ("float" ^ string_of_int i)) basic.tfloat in
- let vdyn = alloc_var (gen.gmk_internal_name "fn" ("dyn" ^ string_of_int i)) t_dynamic in
- loop (i - 1) ((vfloat, None) :: (vdyn, None) :: acc)
- end
- in
- loop i []
- in
- let args_real_to_func args =
- let arity = List.length args in
- if arity >= max_arity then
- [ alloc_var (gen.gmk_internal_name "fn" "dynargs") (basic.tarray t_dynamic), None ]
- else func_args_i arity
- in
- let func_sig_i i =
- let rec loop i acc =
- if i = 0 then acc else begin
- let vfloat = gen.gmk_internal_name "fn" ("float" ^ string_of_int i) in
- let vdyn = gen.gmk_internal_name "fn" ("dyn" ^ string_of_int i) in
- loop (i - 1) ( (vfloat,false,basic.tfloat) :: (vdyn,false,t_dynamic) :: acc )
- end
- in
- loop i []
- in
- let args_real_to_func_sig args =
- let arity = List.length args in
- if arity >= max_arity then
- [gen.gmk_internal_name "fn" "dynargs", false, basic.tarray t_dynamic]
- else begin
- func_sig_i arity
- end
- in
- let rettype_real_to_func t = match run_follow gen t with
- | TType({ t_path = [],"Null" }, _) ->
- 0,t_dynamic
- | _ when like_float t && not (like_i64 t) ->
- (1, basic.tfloat)
- | _ ->
- (0, t_dynamic)
- in
- let args_real_to_func_call el (pos:Ast.pos) =
- if List.length el >= max_arity then
- [{ eexpr = TArrayDecl el; etype = basic.tarray t_dynamic; epos = pos }]
- else begin
- List.fold_left (fun acc e ->
- if like_float (gen.greal_type e.etype) && not (like_i64 (gen.greal_type e.etype)) then
- ( e :: undefined e.epos :: acc )
- else
- ( null basic.tfloat e.epos :: e :: acc )
- ) ([]) (List.rev el)
- end
- in
- let const_type c def =
- match c with
- | TString _ -> basic.tstring | TInt _ -> basic.tint
- | TFloat _ -> basic.tfloat | TBool _ -> basic.tbool
- | _ -> def
- in
- let get_args_func args changed_args pos =
- let arity = List.length args in
- let mk_const const elocal t =
- match const with
- | None -> mk_cast t elocal
- | Some const ->
- { eexpr = TIf(
- { elocal with eexpr = TBinop(Ast.OpEq, elocal, null elocal.etype elocal.epos); etype = basic.tbool },
- { elocal with eexpr = TConst(const); etype = const_type const t },
- Some ( mk_cast t elocal )
- ); etype = t; epos = elocal.epos }
- in
- if arity >= max_arity then begin
- let varray = match changed_args with | [v,_] -> v | _ -> assert false in
- let varray_local = mk_local varray pos in
- let mk_varray i = { eexpr = TArray(varray_local, { eexpr = TConst(TInt(Int32.of_int i)); etype = basic.tint; epos = pos }); etype = t_dynamic; epos = pos } in
- snd (List.fold_left (fun (count,acc) (v,const) ->
- (count + 1,
- {
- eexpr = TVar(v, Some(mk_const const ( mk_varray count ) v.v_type));
- etype = basic.tvoid;
- epos = pos;
- } :: acc)
- ) (0,[]) args)
- end else begin
- let _, dyn_args, float_args = List.fold_left (fun (count,fargs, dargs) arg ->
- if count land 1 = 0 then
- (count + 1, fargs, arg :: dargs)
- else
- (count + 1, arg :: fargs, dargs)
- ) (1,[],[]) (List.rev changed_args) in
- let rec loop acc args fargs dargs =
- match args, fargs, dargs with
- | [], [], [] -> acc
- | (v,const) :: args, (vf,_) :: fargs, (vd,_) :: dargs ->
- let acc = { eexpr = TVar(v, Some(
- {
- eexpr = TIf(
- { eexpr = TBinop(Ast.OpEq, mk_local vd pos, undefined pos); etype = basic.tbool; epos = pos },
- mk_cast v.v_type (mk_local vf pos),
- Some ( mk_const const (mk_local vd pos) v.v_type )
- );
- etype = v.v_type;
- epos = pos
- } )); etype = basic.tvoid; epos = pos } :: acc in
- loop acc args fargs dargs
- | _ -> assert false
- in
- loop [] args float_args dyn_args
- end
- in
- let closure_to_classfield tfunc old_sig pos =
- (* change function signature *)
- let old_args = tfunc.tf_args in
- let changed_args = args_real_to_func old_args in
- (*
- FIXME properly handle int64 cases, which will break here (because of inference to int)
- UPDATE: the fix will be that Int64 won't be a typedef to Float/Int
- *)
- let changed_sig, arity, type_number, changed_sig_ret, is_void, is_dynamic_func = match follow old_sig with
- | TFun(_sig, ret) ->
- let type_n, ret_t = rettype_real_to_func ret in
- let arity = List.length _sig in
- let is_dynamic_func = arity >= max_arity in
- let ret_t = if is_dynamic_func then t_dynamic else ret_t in
- (TFun(args_real_to_func_sig _sig, ret_t), arity, type_n, ret_t, is_void ret, is_dynamic_func)
- | _ -> (print_endline (s_type (print_context()) (follow old_sig) )); assert false
- in
- let tf_expr = if is_void then begin
- let rec map e =
- match e.eexpr with
- | TReturn None -> { e with eexpr = TReturn (Some (null t_dynamic e.epos)) }
- | _ -> Type.map_expr map e
- in
- let e = mk_block (map tfunc.tf_expr) in
- match e.eexpr with
- | TBlock(bl) ->
- { e with eexpr = TBlock(bl @ [{ eexpr = TReturn (Some (null t_dynamic e.epos)); etype = t_dynamic; epos = e.epos }]) }
- | _ -> assert false
- end else tfunc.tf_expr in
- let changed_sig_ret = if is_dynamic_func then t_dynamic else changed_sig_ret in
- (* get real arguments on top of function body *)
- let get_args = get_args_func tfunc.tf_args changed_args pos in
- (*
- FIXME HACK: in order to be able to run the filters that have already ran for this piece of code,
- we will cheat and run it as if it was the whole code
- We could just make ClosuresToClass run before TArrayTransform, but we cannot because of the
- dependency between ClosuresToClass (after DynamicFieldAccess, and before TArrayTransform)
- maybe a way to solve this would be to add an "until" field to run_from
- *)
- let real_get_args = gen.gexpr_filters#run_f { eexpr = TBlock(get_args); etype = basic.tvoid; epos = pos } in
- let func_expr = Type.concat real_get_args tf_expr in
- (* set invoke function *)
- (* todo properly abstract how naming for invoke is made *)
- let invoke_name = if is_dynamic_func then "invokeDynamic" else ("invoke" ^ (string_of_int arity) ^ (if type_number = 0 then "_o" else "_f")) in
- let invoke_name = gen.gmk_internal_name "hx" invoke_name in
- let invoke_field = mk_class_field invoke_name changed_sig false func_expr.epos (Method(MethNormal)) [] in
- let invoke_fun =
- {
- eexpr = TFunction(
- {
- tf_args = changed_args;
- tf_type = changed_sig_ret;
- tf_expr = func_expr;
- });
- etype = changed_sig;
- epos = func_expr.epos;
- } in
- invoke_field.cf_expr <- Some(invoke_fun);
- (invoke_field, [
- { eexpr = TConst(TInt( Int32.of_int arity )); etype = gen.gcon.basic.tint; epos = pos };
- { eexpr = TConst(TInt( Int32.of_int type_number )); etype = gen.gcon.basic.tint; epos = pos };
- ])
- in
- let dynamic_fun_call call_expr =
- let tc, params = match call_expr.eexpr with
- | TCall(tc, params) -> (tc, params)
- | _ -> assert false
- in
- let ct = gen.greal_type call_expr.etype in
- let postfix, ret_t =
- if like_float ct && not (like_i64 ct) then
- "_f", gen.gcon.basic.tfloat
- else
- "_o", t_dynamic
- in
- let params_len = List.length params in
- let ret_t = if params_len >= max_arity then t_dynamic else ret_t in
- let invoke_fun = if params_len >= max_arity then "invokeDynamic" else "invoke" ^ (string_of_int params_len) ^ postfix in
- let invoke_fun = gen.gmk_internal_name "hx" invoke_fun in
- let fun_t = match follow tc.etype with
- | TFun(_sig, _) ->
- TFun(args_real_to_func_sig _sig, ret_t)
- | _ ->
- let i = ref 0 in
- let _sig = List.map (fun p -> let name = "arg" ^ (string_of_int !i) in incr i; (name,false,p.etype) ) params in
- TFun(args_real_to_func_sig _sig, ret_t)
- in
- let may_cast = match follow call_expr.etype with
- | TAbstract ({ a_path = ([], "Void") },[]) -> (fun e -> e)
- | _ -> mk_cast call_expr.etype
- in
- may_cast
- {
- eexpr = TCall(
- { (mk_field_access gen { tc with etype = gen.greal_type tc.etype } invoke_fun tc.epos) with etype = fun_t },
- args_real_to_func_call params call_expr.epos
- );
- etype = ret_t;
- epos = call_expr.epos
- }
- in
- let iname is_function i is_float =
- let postfix = if is_float then "_f" else "_o" in
- gen.gmk_internal_name "hx" ("invoke" ^ (if not is_function then "Field" else "") ^ string_of_int i) ^ postfix
- in
- let map_base_classfields cl is_function map_fn =
- let pos = cl.cl_pos in
- let this_t = TInst(cl,List.map snd cl.cl_params) in
- let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
- let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
- let mk_invoke_i i is_float =
- let cf = mk_class_field (iname is_function i is_float) (TFun(func_sig_i i, if is_float then basic.tfloat else t_dynamic)) false pos (Method MethNormal) [] in
- cf
- in
- let type_name = gen.gmk_internal_name "fn" "type" in
- let dynamic_arg = alloc_var (gen.gmk_internal_name "fn" "dynargs") (basic.tarray t_dynamic) in
- let mk_invoke_complete_i i is_float =
- (* let arity = i in *)
- let args = func_args_i i in
- (* api fn *)
- (* only cast if needed *)
- let mk_cast tto efrom = gen.ghandle_cast (gen.greal_type tto) (gen.greal_type efrom.etype) efrom in
- let api i t const =
- let vf, _ = List.nth args (i * 2) in
- let vo, _ = List.nth args (i * 2 + 1) in
- let needs_cast, is_float = match t, like_float t && not (like_i64 t) with
- | TAbstract({ a_path = ([], "Float") },[]), _ -> false, true
- | _, true -> true, true
- | _ -> false,false
- in
- let olocal = mk_local vo pos in
- let flocal = mk_local vf pos in
- let get_from_obj e = match const with
- | None -> mk_cast t e
- | Some tc ->
- {
- eexpr = TIf(
- { eexpr = TBinop(Ast.OpEq, olocal, null t_dynamic pos); etype = basic.tbool; epos = pos } ,
- { eexpr = TConst(tc); etype = t; epos = pos },
- Some (mk_cast t e)
- );
- etype = t;
- epos = pos;
- }
- in
- {
- eexpr = TIf(
- { eexpr = TBinop(Ast.OpEq, olocal, undefined pos); etype = basic.tbool; epos = pos },
- (if needs_cast then mk_cast t flocal else flocal),
- Some ( get_from_obj olocal )
- );
- etype = t;
- epos = pos
- }
- in
- (* end of api fn *)
- let ret = if is_float then basic.tfloat else t_dynamic in
- let added_args, fn_expr = map_fn i ret (List.map fst args) api in
- let args = added_args @ args in
- let t = TFun(fun_args args, ret) in
- let tfunction =
- {
- eexpr = TFunction({
- tf_args = args;
- tf_type = ret;
- tf_expr =
- mk_block fn_expr
- });
- etype = t;
- epos = pos;
- }
- in
- let cf = mk_invoke_i i is_float in
- cf.cf_expr <- Some tfunction;
- cf
- in
- let rec loop i cfs =
- if i < 0 then cfs else begin
- (*let mk_invoke_complete_i i is_float =*)
- (mk_invoke_complete_i i false) :: (mk_invoke_complete_i i true) :: (loop (i-1) cfs)
- end
- in
- let cfs = loop max_arity [] in
- let added_s_args, switch =
- let api i t const =
- match i with
- | -1 ->
- mk_local dynamic_arg pos
- | _ ->
- mk_cast t {
- eexpr = TArray(
- mk_local dynamic_arg pos,
- { eexpr = TConst(TInt(Int32.of_int i)); etype = basic.tint; epos = pos });
- etype = t;
- epos = pos;
- }
- in
- map_fn (-1) t_dynamic [dynamic_arg] api
- in
- let args = added_s_args @ [dynamic_arg, None] in
- let dyn_t = TFun(fun_args args, t_dynamic) in
- let dyn_cf = mk_class_field (gen.gmk_internal_name "hx" "invokeDynamic") dyn_t false pos (Method MethNormal) [] in
- dyn_cf.cf_expr <-
- Some {
- eexpr = TFunction({
- tf_args = args;
- tf_type = t_dynamic;
- tf_expr = mk_block switch
- });
- etype = dyn_t;
- epos = pos;
- };
- let additional_cfs = if is_function then begin
- let new_t = TFun(["arity", false, basic.tint; "type", false, basic.tint],basic.tvoid) in
- let new_cf = mk_class_field "new" (new_t) true pos (Method MethNormal) [] in
- let v_arity, v_type = alloc_var "arity" basic.tint, alloc_var "type" basic.tint in
- let mk_assign v field = { eexpr = TBinop(Ast.OpAssign, mk_this field v.v_type, mk_local v pos); etype = v.v_type; epos = pos } in
- let arity_name = gen.gmk_internal_name "hx" "arity" in
- new_cf.cf_expr <-
- Some {
- eexpr = TFunction({
- tf_args = [v_arity, None; v_type, None];
- tf_type = basic.tvoid;
- tf_expr =
- {
- eexpr = TBlock([
- mk_assign v_type type_name;
- mk_assign v_arity arity_name
- ]);
- etype = basic.tvoid;
- epos = pos;
- }
- });
- etype = new_t;
- epos = pos;
- }
- ;
- [
- new_cf;
- mk_class_field type_name basic.tint true pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
- mk_class_field arity_name basic.tint true pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
- ]
- end else [] in
- dyn_cf :: (additional_cfs @ cfs)
- in
- (* maybe another param for prefix *)
- let get_base_classfields_for cl is_function mk_additional_args =
- let pos = cl.cl_pos in
- let this_t = TInst(cl,List.map snd cl.cl_params) in
- let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
- let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
- let rec mk_dyn_call arity api =
- let zero = { eexpr = TConst(TFloat("0.0")); etype = basic.tfloat; epos = pos } in
- let rec loop i acc =
- if i = 0 then acc else begin
- let arr = api (i-1) t_dynamic None in
- loop (i - 1) (zero :: arr :: acc)
- end
- in
- loop arity ([])
- in
- let mk_invoke_switch i (api:(int->t->tconstant option->texpr)) =
- let t = TFun(func_sig_i i,t_dynamic) in
- (* case i: return this.invokeX_o(0, 0, 0, 0, 0, ... arg[0], args[1]....); *)
- ( [{ eexpr = TConst(TInt(Int32.of_int i)); etype = basic.tint; epos = pos }],
- {
- eexpr = TReturn(Some( {
- eexpr = TCall(mk_this (iname is_function i false) t, mk_dyn_call i api);
- etype = t_dynamic;
- epos = pos;
- } ));
- etype = t_dynamic;
- epos = pos;
- } )
- in
- let cl_t = TInst(cl,List.map snd cl.cl_params) in
- let this = { eexpr = TConst(TThis); etype = cl_t; epos = pos } in
- let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
- let mk_int i = { eexpr = TConst(TInt ( Int32.of_int i)); etype = basic.tint; epos = pos } in
- let mk_string s = { eexpr = TConst(TString s); etype = basic.tstring; epos = pos } in
- (*
- if it is the Function class, the base class fields will be
- * hx::invokeX_d|o (where X is from 0 to max_arity) (args)
- {
- if (this.type == 0|1) return invokeX_o|d(args); else throw "Invalid number of arguments."
- }
- hx::invokeDynamic, which will work in the same way
- new(arity, type)
- {
- if (type != 0 && type != 1) throw "Invalid type";
- this.arity = arity;
- this.type = type;
- }
- *)
- let type_name = gen.gmk_internal_name "fn" "type" in
- let mk_expr i is_float vars =
- let name = if is_function then "invoke" else "invokeField" in
- let look_ahead = alloc_var "lookAhead" basic.tbool in
- let add_args = if not is_function then mk_additional_args() else [] in
- let vars = if not is_function then (List.map fst add_args) @ (look_ahead :: vars) else vars in
- let call_expr =
- let call_t = TFun(List.map (fun v -> (v.v_name, false, v.v_type)) vars, if is_float then t_dynamic else basic.tfloat) in
- {
- eexpr = TCall(mk_this (gen.gmk_internal_name "hx" (name ^ (string_of_int i) ^ (if is_float then "_o" else "_f"))) call_t, List.map (fun v -> if v.v_id = look_ahead.v_id then ( { eexpr = TConst(TBool false); etype = basic.tbool; epos = pos } ) else mk_local v pos) vars );
- etype = if is_float then t_dynamic else basic.tfloat;
- epos = pos
- }
- in
- (*let call_expr = if is_float then mk_cast basic.tfloat call_expr else call_expr in*)
- let if_cond = if is_function then
- { eexpr=TBinop(Ast.OpNotEq, mk_this type_name basic.tint, mk_int (if is_float then 0 else 1) ); etype = basic.tbool; epos = pos }
- else
- mk_local look_ahead pos
- in
- let if_expr = if is_function then
- {
- eexpr = TIf(if_cond,
- { eexpr = TThrow(mk_string "Wrong number of arguments"); etype = basic.tstring; epos = pos },
- Some( { eexpr = TReturn( Some( call_expr ) ); etype = call_expr.etype; epos = pos } )
- );
- etype = t_dynamic;
- epos = pos;
- }
- else
- {
- eexpr = TIf(if_cond,
- { eexpr = TReturn( Some( call_expr ) ); etype = call_expr.etype; epos = pos },
- Some( { eexpr = TThrow(mk_string "Field not found or wrong number of arguments"); etype = basic.tstring; epos = pos } )
- );
- etype = t_dynamic;
- epos = pos;
- }
- in
- let args = if not is_function then (mk_additional_args()) @ [look_ahead, None] else [] in
- (args, if_expr)
- in
- let arities_processed = Hashtbl.create 10 in
- let max_arity = ref 0 in
- let rec loop_cases api arity acc =
- if arity < 0 then acc else
- loop_cases api (arity - 1) (mk_invoke_switch arity api :: acc)
- in
- (* let rec loop goes here *)
- let map_fn cur_arity fun_ret_type vars (api:(int->t->tconstant option->texpr)) =
- let is_float = like_float fun_ret_type && not (like_i64 fun_ret_type) in
- match cur_arity with
- | -1 ->
- let dynargs = api (-1) (t_dynamic) None in
- let switch_cond = mk_field_access gen dynargs "length" pos in
- let switch_cond = {
- eexpr = TIf(
- { eexpr = TBinop(Ast.OpEq, dynargs, null dynargs.etype pos); etype = basic.tbool; epos = pos; },
- { eexpr = TConst(TInt(Int32.zero)); etype = basic.tint; epos = pos },
- Some switch_cond);
- etype = basic.tint;
- epos = pos;
- } in
- let switch =
- {
- eexpr = TSwitch( switch_cond,
- loop_cases api !max_arity [],
- Some({ eexpr = TThrow(mk_string "Too many arguments"); etype = basic.tvoid; epos = pos; }) );
- etype = basic.tvoid;
- epos = pos;
- } in
- ( (if not is_function then mk_additional_args () else []), switch )
- | _ ->
- if not (Hashtbl.mem arities_processed cur_arity) then begin
- Hashtbl.add arities_processed cur_arity true;
- if cur_arity > !max_arity then max_arity := cur_arity
- end;
- mk_expr cur_arity is_float vars
- in
- map_base_classfields cl is_function map_fn
- in
- let initialize_base_class cl =
- ()
- in
- {
- fgen = gen;
- func_class = null_class;
- closure_to_classfield = closure_to_classfield;
- dynamic_fun_call = dynamic_fun_call;
- (*
- called once so the implementation can make one of a time initializations in the base class
- for all functions
- *)
- initialize_base_class = initialize_base_class;
- (*
- Base classfields are the class fields for the abstract implementation of either the Function implementation,
- or the invokeField implementation for the classes
- They will either try to call the right function or will fail with
- (tclass - subject (so we know the type of this)) -> is_function_base -> list of the abstract implementation class fields
- *)
- get_base_classfields_for = get_base_classfields_for;
- map_base_classfields = map_base_classfields;
- (*
- for now we won't deal with the closures.
- They can be dealt with the module ReflectionCFs,
- or a custom implementation
- *)
- transform_closure = (fun tclosure texpr str -> tclosure);
- }
- end;;
- end;;
- (* ******************************************* *)
- (* Type Parameters *)
- (* ******************************************* *)
- (*
- This module will handle type parameters. There are lots of changes we need to do to correctly support type parameters:
- traverse will:
- V Detect when parameterized function calls are made
- * Detect when a parameterized class instance is being cast to another parameter
- * Change new<> parameterized function calls
- *
- extras:
- * On languages that support "real" type parameters, a Cast function is provided that will convert from a <Dynamic> to the requested type.
- This cast will call createEmpty with the correct type, and then set each variable to the new form. Some types will be handled specially, namely the Native Array.
- Other implementations may be delegated to the runtime.
- * parameterized classes will implement a new interface (with only a Cast<> function added to it), so we can access the <Dynamic> type parameter for them. Also any reference to <Dynamic> will be replaced by a reference to this interface. (also on TTypeExpr - Std.is())
- * Type parameter renaming to avoid name clash
- * Detect type parameter casting and call Cast<> instead
- for java:
- * for specially assigned classes, parameters will be replaced by _d and _i versions of parameterized functions. This will only work for parameterized classes, not functions.
- dependencies:
- must run after casts are detected. This will be ensured at CastDetect module.
- *)
- module TypeParams =
- struct
- let name = "type_params"
- let priority = max_dep -. 20.
- (* this function will receive the original function argument, the applied function argument and the original function parameters. *)
- (* from this info, it will infer the applied tparams for the function *)
- (* this function is used by CastDetection module *)
- let infer_params gen pos (original_args:((string * bool * t) list * t)) (applied_args:((string * bool * t) list * t)) (params:(string * t) list) calls_parameters_explicitly : tparams =
- match params with
- | [] -> []
- | _ ->
- let args_list args = (if not calls_parameters_explicitly then t_dynamic else snd args) :: (List.map (fun (n,o,t) -> t) (fst args)) in
- let monos = List.map (fun _ -> mk_mono()) params in
- let original = args_list (get_fun (apply_params params monos (TFun(fst original_args,snd original_args)))) in
- let applied = args_list applied_args in
- (try
- List.iter2 (fun a o ->
- let o = run_follow gen o in
- let a = run_follow gen a in
- unify a o
- (* type_eq EqStrict a o *)
- ) applied original
- (* unify applied original *)
- with | Unify_error el ->
- (* List.iter (fun el -> gen.gcon.warning (Typecore.unify_error_msg (print_context()) el) pos) el; *)
- gen.gcon.warning ("This expression may be invalid") pos
- | Invalid_argument("List.map2") ->
- gen.gcon.warning ("This expression may be invalid") pos
- );
- List.map (fun t ->
- match follow t with
- | TMono _ -> t_empty
- | t -> t
- ) monos
- (* ******************************************* *)
- (* Real Type Parameters Module *)
- (* ******************************************* *)
- (*
- This submodule is by now specially made for the .NET platform. There might be other targets that will
- make use of this, but it IS very specific.
- On the .NET platform, generics are real specialized classes that are JIT compiled. For this reason, we cannot
- cast from one type parameter to another. Also there is no common type for the type parameters, so for example
- an instance of type Array<Int> will return false for instance is Array<object> .
- So we need to:
- 1. create a common interface (without type parameters) (e.g. "Array") which will only contain a __Cast<> function, which will cast from one type into another
- 2. Implement the __Cast function. This part is a little hard, as we must identify all type parameter-dependent fields contained in the class and convert them.
- In most cases the conversion will just be to call .__Cast<>() on the instances, or just a simple cast. But when the instance is a @:nativegen type, there will be no .__Cast
- function, and we will need to deal with this case either at compile-time (added handlers - specially for NativeArray), or at runtime (adding new runtime handlers)
- 3. traverse the AST looking for casts involving type parameters, and replace them with .__Cast<>() calls. If type is @:nativegen, throw a warning. If really casting from one type parameter to another on a @:nativegen context, throw an error.
- special literals:
- it will use the special literal __typehandle__ that the target must implement in order to run this. This literal is a way to get the typehandle of e.g. the type parameters,
- so we can compare them. In C# it's the equivalent of typeof(T).TypeHandle (TypeHandle compare is faster than System.Type.Equals())
- dependencies:
- (module filter) Interface creation must run AFTER enums are converted into classes, otherwise there is no way to tell parameterized enums to implement an interface
- Must run AFTER CastDetect. This will be ensured per CastDetect
- *)
- module RealTypeParams =
- struct
- let name = "real_type_params"
- let priority = priority
- let cast_field_name = "cast"
- let rec has_type_params t =
- match follow t with
- | TInst( { cl_kind = KTypeParameter _ }, _) -> true
- | TAbstract(_, params)
- | TEnum(_, params)
- | TInst(_, params) -> List.exists (fun t -> has_type_params t) params
- | TFun(args,ret) ->
- List.exists (fun (n,o,t) -> has_type_params t) args || has_type_params ret
- | _ -> false
- let rec follow_all_md md =
- match md with
- | TClassDecl { cl_kind = KAbstractImpl a } ->
- follow_all_md (TAbstractDecl a)
- | TAbstractDecl a -> if Meta.has Meta.CoreType a.a_meta then
- None
- else (
- match follow (apply_params a.a_params (List.map snd a.a_params) a.a_this) with
- | TInst(c,_) -> follow_all_md (TClassDecl c)
- | TEnum(e,_) -> follow_all_md (TEnumDecl e)
- | TAbstract(a,_) -> follow_all_md (TAbstractDecl a)
- | TType(t,_) -> follow_all_md (TTypeDecl t)
- | _ -> None)
- | TTypeDecl t -> (
- match follow (apply_params t.t_params (List.map snd t.t_params) t.t_type) with
- | TInst(c,_) -> follow_all_md (TClassDecl c)
- | TEnum(e,_) -> follow_all_md (TEnumDecl e)
- | TAbstract(a,_) -> follow_all_md (TAbstractDecl a)
- | TType(t,_) -> follow_all_md (TTypeDecl t)
- | _ -> None)
- | md -> Some md
- let rec is_hxgeneric md =
- match md with
- | TClassDecl { cl_kind = KAbstractImpl a } ->
- is_hxgeneric (TAbstractDecl a)
- | TClassDecl(cl) ->
- not (Meta.has Meta.NativeGeneric cl.cl_meta)
- | TEnumDecl(e) ->
- not (Meta.has Meta.NativeGeneric e.e_meta)
- | md -> match follow_all_md md with
- | Some md -> is_hxgeneric md
- | None -> true
- let rec set_hxgeneric gen mds isfirst md =
- let path = t_path md in
- if List.exists (fun m -> path = t_path m) mds then begin
- if isfirst then
- None (* we still can't determine *)
- else
- Some true (* if we're in second pass and still can't determine, it's because it can be hxgeneric *)
- end else begin
- let has_unresolved = ref false in
- let is_false v =
- match v with
- | Some false -> true
- | None -> has_unresolved := true; false
- | Some true -> false
- in
- let mds = md :: mds in
- match md with
- | TClassDecl(cl) ->
- (* first see if any meta is present (already processed) *)
- if Meta.has Meta.NativeGeneric cl.cl_meta then
- Some false
- else if Meta.has Meta.HaxeGeneric cl.cl_meta then
- Some true
- else if cl.cl_params = [] && is_hxgen md then
- (cl.cl_meta <- (Meta.HaxeGeneric,[],cl.cl_pos) :: cl.cl_meta;
- Some true)
- else if cl.cl_params = [] then
- (cl.cl_meta <- (Meta.NativeGeneric, [], cl.cl_pos) :: cl.cl_meta;
- Some false)
- else if not (is_hxgen md) then
- (cl.cl_meta <- (Meta.NativeGeneric, [], cl.cl_pos) :: cl.cl_meta;
- Some false)
- else begin
- (*
- if it's not present, see if any superclass is nativegeneric.
- nativegeneric is inherited, while hxgeneric can be later changed to nativegeneric
- *)
- (* on the first pass, our job is to find any evidence that makes it not be hxgeneric. Otherwise it will be hxgeneric *)
- match cl.cl_super with
- | Some (c,_) when is_false (set_hxgeneric gen mds isfirst (TClassDecl c)) ->
- cl.cl_meta <- (Meta.NativeGeneric, [], cl.cl_pos) :: cl.cl_meta;
- Some false
- | _ ->
- (* see if it's a generic class *)
- match cl.cl_params with
- | [] ->
- (* if it's not, then it will follow hxgen *)
- if is_hxgen (TClassDecl cl) then
- cl.cl_meta <- (Meta.HaxeGeneric, [], cl.cl_pos) :: cl.cl_meta
- else
- cl.cl_meta <- (Meta.NativeGeneric, [], cl.cl_pos) :: cl.cl_meta;
- Some true
- | _ ->
- (* if it is, loop through all fields + statics and look for non-hxgeneric
- generic classes that have KTypeParameter as params *)
- let rec loop cfs =
- match cfs with
- | [] -> false
- | cf :: cfs ->
- let t = follow (gen.greal_type cf.cf_type) in
- match t with
- | TInst( { cl_kind = KTypeParameter _ }, _ ) -> loop cfs
- | TInst(cl,p) when has_type_params t && is_false (set_hxgeneric gen mds isfirst (TClassDecl cl)) ->
- if not (Hashtbl.mem gen.gtparam_cast cl.cl_path) then true else loop cfs
- | TEnum(e,p) when has_type_params t && is_false (set_hxgeneric gen mds isfirst (TEnumDecl e)) ->
- if not (Hashtbl.mem gen.gtparam_cast e.e_path) then true else loop cfs
- | _ -> loop cfs (* TAbstracts / Dynamics can't be generic *)
- in
- if loop cl.cl_ordered_fields then begin
- cl.cl_meta <- (Meta.NativeGeneric, [], cl.cl_pos) :: cl.cl_meta;
- Some false
- end else if isfirst && !has_unresolved then
- None
- else begin
- cl.cl_meta <- (Meta.HaxeGeneric, [], cl.cl_pos) :: cl.cl_meta;
- Some true
- end
- end
- | TEnumDecl e ->
- if Meta.has Meta.NativeGeneric e.e_meta then
- Some false
- else if Meta.has Meta.HaxeGeneric e.e_meta then
- Some true
- else if not (is_hxgen (TEnumDecl e)) then begin
- e.e_meta <- (Meta.NativeGeneric, [], e.e_pos) :: e.e_meta;
- Some false
- end else begin
- (* if enum is not generic, then it's hxgeneric *)
- match e.e_params with
- | [] ->
- e.e_meta <- (Meta.HaxeGeneric, [], e.e_pos) :: e.e_meta;
- Some true
- | _ ->
- let rec loop efs =
- match efs with
- | [] -> false
- | ef :: efs ->
- let t = follow (gen.greal_type ef.ef_type) in
- match t with
- | TFun(args, _) ->
- if List.exists (fun (n,o,t) ->
- let t = follow t in
- match t with
- | TInst( { cl_kind = KTypeParameter _ }, _ ) ->
- false
- | TInst(cl,p) when has_type_params t && is_false (set_hxgeneric gen mds isfirst (TClassDecl cl)) ->
- not (Hashtbl.mem gen.gtparam_cast cl.cl_path)
- | TEnum(e,p) when has_type_params t && is_false (set_hxgeneric gen mds isfirst (TEnumDecl e)) ->
- not (Hashtbl.mem gen.gtparam_cast e.e_path)
- | _ -> false
- ) args then
- true
- else
- loop efs
- | _ -> loop efs
- in
- let efs = PMap.fold (fun ef acc -> ef :: acc) e.e_constrs [] in
- if loop efs then begin
- e.e_meta <- (Meta.NativeGeneric, [], e.e_pos) :: e.e_meta;
- Some false
- end else if isfirst && !has_unresolved then
- None
- else begin
- e.e_meta <- (Meta.HaxeGeneric, [], e.e_pos) :: e.e_meta;
- Some true
- end
- end
- | _ -> assert false
- end
- let set_hxgeneric gen md =
- let ret = match md with
- | TClassDecl { cl_kind = KAbstractImpl a } -> (match follow_all_md md with
- | Some md ->
- let ret = set_hxgeneric gen [] true md in
- if ret = None then get (set_hxgeneric gen [] false md) else get ret
- | None ->
- true)
- | _ -> match set_hxgeneric gen [] true md with
- | None ->
- get (set_hxgeneric gen [] false md)
- | Some v ->
- v
- in
- if not ret then begin
- match md with
- | TClassDecl c ->
- let set_hxgeneric (_,param) = match follow param with
- | TInst(c,_) ->
- c.cl_meta <- (Meta.NativeGeneric, [], c.cl_pos) :: c.cl_meta
- | _ -> ()
- in
- List.iter set_hxgeneric c.cl_params;
- let rec handle_field cf =
- List.iter set_hxgeneric cf.cf_params;
- List.iter handle_field cf.cf_overloads
- in
- (match c.cl_kind with
- | KAbstractImpl a ->
- List.iter set_hxgeneric a.a_params;
- | _ -> ());
- List.iter handle_field c.cl_ordered_fields;
- List.iter handle_field c.cl_ordered_statics
- | _ -> ()
- end;
- ret
- let params_has_tparams params =
- List.fold_left (fun acc t -> acc || has_type_params t) false params
- (* ******************************************* *)
- (* RealTypeParamsModf *)
- (* ******************************************* *)
- (*
- This is the module filter of Real Type Parameters. It will traverse through all types and look for hxgeneric classes (only classes).
- When found, a parameterless interface will be created and associated via the "ifaces" Hashtbl to the original class.
- Also a "cast" function will be automatically generated which will handle unsafe downcasts to more specific type parameters (necessary for serialization)
- dependencies:
- Anything that may create hxgeneric classes must run before it.
- Should run before ReflectionCFs (this dependency will be added to ReflectionCFs), so the added interfaces also get to be real IHxObject's
- *)
- module RealTypeParamsModf =
- struct
- let set_only_hxgeneric gen =
- let rec run md =
- match md with
- | TTypeDecl _ | TAbstractDecl _ -> md
- | _ -> ignore (set_hxgeneric gen md); md
- in
- run
- let name = "real_type_params_modf"
- let priority = solve_deps name []
- let rec get_fields gen cl params_cl params_cf acc =
- let fields = List.fold_left (fun acc cf ->
- match follow (gen.greal_type (gen.gfollow#run_f (cf.cf_type))) with
- | TInst(cli, ((_ :: _) as p)) when (not (is_hxgeneric (TClassDecl cli))) && params_has_tparams p ->
- (cf, apply_params cl.cl_params params_cl cf.cf_type, apply_params cl.cl_params params_cf cf.cf_type) :: acc
- | TEnum(e, ((_ :: _) as p)) when not (is_hxgeneric (TEnumDecl e)) && params_has_tparams p ->
- (cf, apply_params cl.cl_params params_cl cf.cf_type, apply_params cl.cl_params params_cf cf.cf_type) :: acc
- | _ -> acc
- ) [] cl.cl_ordered_fields in
- match cl.cl_super with
- | Some(cs, tls) ->
- get_fields gen cs (List.map (apply_params cl.cl_params params_cl) tls) (List.map (apply_params cl.cl_params params_cf) tls) (fields @ acc)
- | None -> (fields @ acc)
- (* overrides all needed cast functions from super classes / interfaces to call the new cast function *)
- let create_stub_casts gen cl cast_cfield =
- (* go through superclasses and interfaces *)
- let p = cl.cl_pos in
- let this = { eexpr = TConst TThis; etype = (TInst(cl, List.map snd cl.cl_params)); epos = p } in
- let rec loop cls tls level reverse_params =
- if (level <> 0 || cls.cl_interface) && tls <> [] && is_hxgeneric (TClassDecl cls) then begin
- let cparams = List.map (fun (s,t) -> (s, TInst (map_param (get_cl_t t), []))) cls.cl_params in
- let name = String.concat "_" ((fst cls.cl_path) @ [snd cls.cl_path; cast_field_name]) in
- if not (PMap.mem name cl.cl_fields) then begin
- let reverse_params = List.map (apply_params cls.cl_params (List.map snd cparams)) reverse_params in
- let cfield = mk_class_field name (TFun([], t_dynamic)) false cl.cl_pos (Method MethNormal) cparams in
- let field = { eexpr = TField(this, FInstance(cl,List.map snd cl.cl_params, cast_cfield)); etype = apply_params cast_cfield.cf_params reverse_params cast_cfield.cf_type; epos = p } in
- let call =
- {
- eexpr = TCall(field, []);
- etype = t_dynamic;
- epos = p;
- } in
- let call = gen.gparam_func_call call field reverse_params [] in
- let delay () =
- cfield.cf_expr <-
- Some {
- eexpr = TFunction(
- {
- tf_args = [];
- tf_type = t_dynamic;
- tf_expr =
- {
- eexpr = TReturn( Some call );
- etype = t_dynamic;
- epos = p;
- }
- });
- etype = cfield.cf_type;
- epos = p;
- }
- in
- gen.gafter_filters_ended <- delay :: gen.gafter_filters_ended; (* do not let filters alter this expression content *)
- cl.cl_ordered_fields <- cfield :: cl.cl_ordered_fields;
- cl.cl_fields <- PMap.add cfield.cf_name cfield cl.cl_fields;
- if level <> 0 then cl.cl_overrides <- cfield :: cl.cl_overrides
- end
- end;
- let get_reverse super supertl =
- let kv = List.map2 (fun (_,tparam) applied -> (follow applied, follow tparam)) super.cl_params supertl in
- List.map (fun t ->
- try
- List.assq (follow t) kv
- with | Not_found -> t
- ) reverse_params
- in
- (match cls.cl_super with
- | None -> ()
- | Some(super, supertl) ->
- loop super supertl (level + 1) (get_reverse super supertl));
- List.iter (fun (iface, ifacetl) ->
- loop iface ifacetl level (get_reverse iface ifacetl)
- ) cls.cl_implements
- in
- loop cl (List.map snd cl.cl_params) 0 (List.map snd cl.cl_params)
- (*
- Creates a cast classfield, with the desired name
- Will also look for previous cast() definitions and override them, to reflect the current type and fields
- FIXME: this function still doesn't support generics that extend generics, and are cast as one of its subclasses. This needs to be taken care, by
- looking at previous superclasses and whenever a generic class is found, its cast argument must be overriden. the toughest part is to know how to type
- the current type correctly.
- *)
- let create_cast_cfield gen cl name =
- let basic = gen.gcon.basic in
- let cparams = List.map (fun (s,t) -> (s, TInst (map_param (get_cl_t t), []))) cl.cl_params in
- let cfield = mk_class_field name (TFun([], t_dynamic)) false cl.cl_pos (Method MethNormal) cparams in
- let params = List.map snd cparams in
- let fields = get_fields gen cl (List.map snd cl.cl_params) params [] in
- (* now create the contents of the function *)
- (*
- it will look something like:
- if (typeof(T) == typeof(T2)) return this;
- var new_me = new CurrentClass<T2>(EmptyInstnace);
- for (field in Reflect.fields(this))
- {
- switch(field)
- {
- case "aNativeArray":
- var newArray = new NativeArray(this.aNativeArray.Length);
- default:
- Reflect.setField(new_me, field, Reflect.field(this, field));
- }
- }
- *)
- let new_t = TInst(cl, params) in
- let pos = cl.cl_pos in
- let new_me_var = alloc_var "new_me" new_t in
- let local_new_me = { eexpr = TLocal(new_me_var); etype = new_t; epos = pos } in
- let this = { eexpr = TConst(TThis); etype = (TInst(cl, List.map snd cl.cl_params)); epos = pos } in
- let field_var = alloc_var "field" gen.gcon.basic.tstring in
- let local_field = { eexpr = TLocal(field_var); etype = field_var.v_type; epos = pos } in
- let i_var = alloc_var "i" gen.gcon.basic.tint in
- let local_i = { eexpr = TLocal(i_var); etype = gen.gcon.basic.tint; epos = pos } in
- let incr_i = { eexpr = TUnop(Ast.Increment, Ast.Postfix, local_i); etype = basic.tint; epos = pos } in
- let fields_var = alloc_var "fields" (gen.gcon.basic.tarray gen.gcon.basic.tstring) in
- let local_fields = { eexpr = TLocal(fields_var); etype = (gen.gcon.basic.tarray gen.gcon.basic.tstring); epos = pos } in
- let get_path t =
- match follow t with
- | TInst(cl,_) -> cl.cl_path
- | TEnum(e,_) -> e.e_path
- | TAbstract(a,_) -> a.a_path
- | TMono _
- | TDynamic _ -> ([], "Dynamic")
- | _ -> assert false
- in
- (* this will take all fields that were *)
- let fields_to_cases fields =
- List.map (fun (cf, t_cl, t_cf) ->
- let this_field = { eexpr = TField(this, FInstance(cl, List.map snd cl.cl_params, cf)); etype = t_cl; epos = pos } in
- let expr =
- {
- eexpr = TBinop(OpAssign, { eexpr = TField(local_new_me, FInstance(cl, List.map snd cl.cl_params, cf) ); etype = t_cf; epos = pos },
- try (Hashtbl.find gen.gtparam_cast (get_path t_cf)) this_field t_cf with | Not_found -> (* if not found tparam cast, it shouldn't be a valid hxgeneric *) assert false
- );
- etype = t_cf;
- epos = pos;
- } in
- ([{ eexpr = TConst(TString(cf.cf_name)); etype = gen.gcon.basic.tstring; epos = pos }], expr)
- ) fields
- in
- let mk_typehandle =
- let thandle = alloc_var "__typeof__" t_dynamic in
- (fun cl -> { eexpr = TCall(mk_local thandle pos, [ mk_classtype_access cl pos ]); etype = t_dynamic; epos = pos })
- in
- let mk_eq cl1 cl2 =
- { eexpr = TBinop(Ast.OpEq, mk_typehandle cl1, mk_typehandle cl2); etype = basic.tbool; epos = pos }
- in
- let rec mk_typehandle_cond thisparams cfparams =
- match thisparams, cfparams with
- | TInst(cl_this,[]) :: [], TInst(cl_cf,[]) :: [] ->
- mk_eq cl_this cl_cf
- | TInst(cl_this,[]) :: hd, TInst(cl_cf,[]) :: hd2 ->
- { eexpr = TBinop(Ast.OpBoolAnd, mk_eq cl_this cl_cf, mk_typehandle_cond hd hd2); etype = basic.tbool; epos = pos }
- | v :: hd, v2 :: hd2 ->
- (match follow v, follow v2 with
- | (TInst(cl1,[]) as v), (TInst(cl2,[]) as v2) ->
- mk_typehandle_cond (v :: hd) (v2 :: hd2)
- | _ ->
- assert false
- )
- | _ -> assert false
- in
- let fn =
- {
- tf_args = [];
- tf_type = t_dynamic;
- tf_expr =
- {
- eexpr = TBlock([
- (* if (typeof(T) == typeof(T2)) return this *)
- {
- eexpr = TIf(
- mk_typehandle_cond (List.map snd cl.cl_params) params,
- mk_return this,
- None);
- etype = basic.tvoid;
- epos = pos;
- };
- (* var new_me = /*special create empty with tparams construct*/ *)
- {
- eexpr = TVar(new_me_var, Some(gen.gtools.rf_create_empty cl params pos));
- etype = gen.gcon.basic.tvoid;
- epos = pos
- };
- (* var fields = Reflect.fields(this); *)
- {
- eexpr = TVar(fields_var, Some(gen.gtools.r_fields true this));
- etype = gen.gcon.basic.tvoid;
- epos = pos
- };
- (* var i = 0; *)
- {
- eexpr = TVar(i_var, Some(mk_int gen 0 pos));
- etype = gen.gcon.basic.tvoid;
- epos = pos
- };
- {
- eexpr = TWhile( (* while (i < fields.length) *)
- {
- eexpr = TBinop(Ast.OpLt,
- local_i,
- mk_field_access gen local_fields "length" pos);
- etype = gen.gcon.basic.tbool;
- epos = pos
- },
- {
- eexpr = TBlock [
- (* var field = fields[i++]; *)
- {
- eexpr = TVar(field_var, Some { eexpr = TArray (local_fields, incr_i); etype = gen.gcon.basic.tstring; epos = pos });
- etype = gen.gcon.basic.tvoid;
- epos = pos
- };
- (
- (* default: Reflect.setField(new_me, field, Reflect.field(this, field)) *)
- let edef = gen.gtools.r_set_field gen.gcon.basic.tvoid local_new_me local_field (gen.gtools.r_field false gen.gcon.basic.tvoid this local_field) in
- if fields <> [] then
- (* switch(field) { ... } *)
- {
- eexpr = TSwitch(local_field, fields_to_cases fields, Some(edef));
- etype = gen.gcon.basic.tvoid;
- epos = pos;
- }
- else
- edef;
- )
- ];
- etype = gen.gcon.basic.tvoid;
- epos = pos
- },
- Ast.NormalWhile
- );
- etype = gen.gcon.basic.tvoid;
- epos = pos;
- };
- (* return new_me *)
- mk_return local_new_me
- ]);
- etype = t_dynamic;
- epos = pos;
- };
- }
- in
- cfield.cf_expr <- Some( { eexpr = TFunction(fn); etype = cfield.cf_type; epos = pos } );
- cfield
- let create_static_cast_cf gen iface cf =
- let p = iface.cl_pos in
- let basic = gen.gcon.basic in
- let cparams = List.map (fun (s,t) -> ("To_" ^ s, TInst (map_param (get_cl_t t), []))) cf.cf_params in
- let me_type = TInst(iface,[]) in
- let cfield = mk_class_field "__hx_cast" (TFun(["me",false,me_type], t_dynamic)) false iface.cl_pos (Method MethNormal) (cparams) in
- let params = List.map snd cparams in
- let me = alloc_var "me" me_type in
- let field = { eexpr = TField(mk_local me p, FInstance(iface, List.map snd iface.cl_params, cf)); etype = apply_params cf.cf_params params cf.cf_type; epos = p } in
- let call =
- {
- eexpr = TCall(field, []);
- etype = t_dynamic;
- epos = p;
- } in
- let call = gen.gparam_func_call call field params [] in
- (* since object.someCall<ExplicitParameterDefinition>() isn't allowed on Haxe, we need to directly apply the params and delay this call *)
- let delay () =
- cfield.cf_expr <-
- Some {
- eexpr = TFunction(
- {
- tf_args = [me,None];
- tf_type = t_dynamic;
- tf_expr =
- {
- eexpr = TReturn( Some
- {
- eexpr = TIf(
- { eexpr = TBinop(Ast.OpNotEq, mk_local me p, null me.v_type p); etype = basic.tbool; epos = p },
- call,
- Some( null me.v_type p )
- );
- etype = t_dynamic;
- epos = p;
- });
- etype = basic.tvoid;
- epos = p;
- }
- });
- etype = cfield.cf_type;
- epos = p;
- }
- in
- cfield, delay
- let get_cast_name cl = String.concat "_" ((fst cl.cl_path) @ [snd cl.cl_path; cast_field_name]) (* explicitly define it *)
- let default_implementation gen ifaces base_generic =
- let add_iface cl =
- gen.gadd_to_module (TClassDecl cl) (max_dep);
- in
- let implement_stub_cast cthis iface tl =
- let name = get_cast_name iface in
- if not (PMap.mem name cthis.cl_fields) then begin
- let cparams = List.map (fun (s,t) -> ("To_" ^ s, TInst(map_param (get_cl_t t), []))) iface.cl_params in
- let field = mk_class_field name (TFun([],t_dynamic)) false iface.cl_pos (Method MethNormal) cparams in
- let this = { eexpr = TConst TThis; etype = TInst(cthis, List.map snd cthis.cl_params); epos = cthis.cl_pos } in
- field.cf_expr <- Some {
- etype = TFun([],t_dynamic);
- epos = this.epos;
- eexpr = TFunction {
- tf_type = t_dynamic;
- tf_args = [];
- tf_expr = mk_block { this with
- eexpr = TReturn (Some this)
- }
- }
- };
- cthis.cl_ordered_fields <- field :: cthis.cl_ordered_fields;
- cthis.cl_fields <- PMap.add name field cthis.cl_fields
- end
- in
- let rec run md =
- match md with
- | TClassDecl ({ cl_params = [] } as cl) ->
- (* see if we're implementing any generic interface *)
- let rec check (iface,tl) =
- if tl <> [] && set_hxgeneric gen (TClassDecl iface) then
- (* implement cast stub *)
- implement_stub_cast cl iface tl;
- List.iter (fun (s,stl) -> check (s, List.map (apply_params iface.cl_params tl) stl)) iface.cl_implements;
- in
- List.iter (check) cl.cl_implements;
- md
- | TClassDecl ({ cl_params = hd :: tl } as cl) when set_hxgeneric gen md ->
- let iface = mk_class cl.cl_module cl.cl_path cl.cl_pos in
- iface.cl_array_access <- Option.map (apply_params (cl.cl_params) (List.map (fun _ -> t_dynamic) cl.cl_params)) cl.cl_array_access;
- iface.cl_module <- cl.cl_module;
- iface.cl_meta <- (Meta.HxGen, [], cl.cl_pos) :: iface.cl_meta;
- Hashtbl.add ifaces cl.cl_path iface;
- iface.cl_implements <- (base_generic, []) :: iface.cl_implements;
- iface.cl_interface <- true;
- cl.cl_implements <- (iface, []) :: cl.cl_implements;
- let name = get_cast_name cl in
- let cast_cf = create_cast_cfield gen cl name in
- if not cl.cl_interface then create_stub_casts gen cl cast_cf;
- let rec loop c = match c.cl_super with
- | None -> ()
- | Some(sup,_) -> try
- let siface = Hashtbl.find ifaces sup.cl_path in
- iface.cl_implements <- (siface,[]) :: iface.cl_implements;
- ()
- with | Not_found -> loop sup
- in
- loop cl;
- (if not cl.cl_interface then cl.cl_ordered_fields <- cast_cf :: cl.cl_ordered_fields);
- let iface_cf = mk_class_field name cast_cf.cf_type false cast_cf.cf_pos (Method MethNormal) cast_cf.cf_params in
- let cast_static_cf, delay = create_static_cast_cf gen iface iface_cf in
- cl.cl_ordered_statics <- cast_static_cf :: cl.cl_ordered_statics;
- cl.cl_statics <- PMap.add cast_static_cf.cf_name cast_static_cf cl.cl_statics;
- gen.gafter_filters_ended <- delay :: gen.gafter_filters_ended; (* do not let filters alter this expression content *)
- iface_cf.cf_type <- cast_cf.cf_type;
- iface.cl_fields <- PMap.add name iface_cf iface.cl_fields;
- let fields = List.filter (fun cf -> match cf.cf_kind with
- | Var _ | Method MethDynamic -> false
- | _ ->
- let is_override = List.memq cf cl.cl_overrides in
- let cf_type = if is_override && not (Meta.has Meta.Overload cf.cf_meta) then
- match field_access gen (TInst(cl, List.map snd cl.cl_params)) cf.cf_name with
- | FClassField(_,_,_,_,_,actual_t,_) -> actual_t
- | _ -> assert false
- else
- cf.cf_type
- in
- not (has_type_params cf_type)) cl.cl_ordered_fields
- in
- let fields = List.map (fun f -> mk_class_field f.cf_name f.cf_type f.cf_public f.cf_pos f.cf_kind f.cf_params) fields in
- let fields = iface_cf :: fields in
- iface.cl_ordered_fields <- fields;
- List.iter (fun f -> iface.cl_fields <- PMap.add f.cf_name f iface.cl_fields) fields;
- add_iface iface;
- md
- | TTypeDecl _ | TAbstractDecl _ -> md
- | TEnumDecl _ ->
- ignore (set_hxgeneric gen md);
- md
- | _ -> ignore (set_hxgeneric gen md); md
- in
- run
- let configure gen mapping_func =
- let map e = Some(mapping_func e) in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* create a common interface without type parameters and only a __Cast<> function *)
- let default_implementation gen (dyn_tparam_cast:texpr->t->texpr) ifaces =
- let change_expr e cl iface params =
- let field = mk_static_field_access_infer cl "__hx_cast" e.epos params in
- let elist = [mk_cast (TInst(iface,[])) e] in
- let call = { eexpr = TCall(field, elist); etype = t_dynamic; epos = e.epos } in
- gen.gparam_func_call call field params elist
- in
- let rec run e =
- match e.eexpr with
- | TCast(cast_expr, _) ->
- (* see if casting to a native generic class *)
- let t = gen.greal_type e.etype in
- let unifies =
- let ctype = gen.greal_type cast_expr.etype in
- match follow ctype with
- | TInst(cl,_) -> (try
- unify ctype t;
- true
- with | Unify_error el ->
- false)
- | _ -> false
- in
- let unifies = unifies && not (PMap.mem "cs_safe_casts" gen.gcon.defines) in
- (match follow t with
- | TInst(cl, p1 :: pl) when is_hxgeneric (TClassDecl cl) && not unifies && not (Meta.has Meta.Enum cl.cl_meta) ->
- let iface = Hashtbl.find ifaces cl.cl_path in
- mk_cast e.etype (change_expr (Type.map_expr run cast_expr) cl iface (p1 :: pl))
- | _ -> Type.map_expr run e
- )
- | _ -> Type.map_expr run e
- in
- run
- let configure gen traverse =
- gen.ghas_tparam_cast_handler <- true;
- let map e = Some(traverse e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- let default_config gen (dyn_tparam_cast:texpr->t->texpr) ifaces base_generic =
- configure gen (default_implementation gen dyn_tparam_cast ifaces);
- RealTypeParamsModf.configure gen (RealTypeParamsModf.default_implementation gen ifaces base_generic)
- end;;
- (* ******************************************* *)
- (* Rename Type Parameters *)
- (* ******************************************* *)
- (*
- This module should run after everything is already applied,
- it will look for possible type parameter name clashing and change the classes names to a
- dependencies:
- should run after everything is already applied. There's no configure on this module, only 'run'.
- *)
- module RenameTypeParameters =
- struct
- let name = "rename_type_parameters"
- let run gen =
- let i = ref 0 in
- let found_types = ref PMap.empty in
- let check_type name on_changed =
- let rec loop name =
- incr i;
- let changed_name = (name ^ (string_of_int !i)) in
- if PMap.mem changed_name !found_types then loop name else changed_name
- in
- if PMap.mem name !found_types then begin
- let new_name = loop name in
- found_types := PMap.add new_name true !found_types;
- on_changed new_name
- end else found_types := PMap.add name true !found_types
- in
- let get_cls t =
- match follow t with
- | TInst(cl,_) -> cl
- | _ -> assert false
- in
- let iter_types (_,t) =
- let cls = get_cls t in
- check_type (snd cls.cl_path) (fun name -> cls.cl_path <- (fst cls.cl_path, name))
- in
- List.iter (function
- | TClassDecl cl ->
- i := 0;
- found_types := PMap.empty;
- List.iter iter_types cl.cl_params;
- let cur_found_types = !found_types in
- List.iter (fun cf ->
- found_types := cur_found_types;
- List.iter iter_types cf.cf_params
- ) (cl.cl_ordered_fields @ cl.cl_ordered_statics)
- | TEnumDecl ( ({ e_params = hd :: tl }) ) ->
- i := 0;
- found_types := PMap.empty;
- List.iter iter_types (hd :: tl)
- | TAbstractDecl { a_params = hd :: tl } ->
- i := 0;
- found_types := PMap.empty;
- List.iter iter_types (hd :: tl)
- | _ -> ()
- ) gen.gcon.types
- end;;
- let configure gen (param_func_call:texpr->texpr->tparams->texpr list->texpr) =
- (*let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map*)
- gen.gparam_func_call <- param_func_call
- end;;
- (**************************************************************************************************************************)
- (* SYNTAX FILTERS *)
- (**************************************************************************************************************************)
- (* ******************************************* *)
- (* Expression Unwrap *)
- (* ******************************************* *)
- (*
- This is the most important module for source-code based targets. It will follow a convention of what's an expression and what's a statement,
- and will unwrap statements where expressions are expected, and vice-versa.
- It should be one of the first syntax filters to be applied. As a consequence, it's applied after all filters that add code to the AST, and by being
- the first of the syntax filters, it will also have the AST retain most of the meaning of normal Haxe code. So it's easier to detect cases which are
- side-effects free, for example
- Any target can make use of this, but there is one requirement: The target must accept null to be set to any kind of variable. For example,
- var i:Int = null; must be accepted. The best way to deal with this is to (like it's done in C#) make null equal to "default(Type)"
- dependencies:
- While it's best for Expression Unwrap to delay its execution as much as possible, since theoretically any
- filter can return an expression that needs to be unwrapped, it is also desirable for ExpresionUnwrap to have
- the AST as close as possible as Haxe's, so it can make some correct predictions (for example, so it can
- more accurately know what can be side-effects-free and what can't).
- This way, it will run slightly after the Normal priority, so if you don't say that a syntax filter must run
- before Expression Unwrap, it will run after it.
- TODO : While statement must become do / while, with the actual block inside an if for the condition, and else for 'break'
- *)
- module ExpressionUnwrap =
- struct
- let name = "expression_unwrap"
- (* priority: first syntax filter *)
- let priority = -10.0
- (*
- We always need to rely on Blocks to be able to unwrap expressions correctly.
- So the the standard traverse will always be based on blocks.
- Normal block statements, like for(), while(), if(), ... will be mk_block'ed so there is always a block inside of them.
- At the block level, we'll define an "add_statement" function, which will allow the current expression to
- add statements to the block. This statement may or may not contain statements as expressions, so the texpr will be evaluated recursively before being added.
- - traverse will always evaluate TBlocks
- - for each texpr in a TBlock list,
- check shallow type
- if type is Statement or Both when it has problematic expression (var problematic_expr = count_problematic_expressions),
- if we can eagerly call unwrap_statement on the whole expression (try_call_unwrap_statement), use the return expression
- else
- check expr_type of each underlying type (with expr_stat_map)
- if it has ExprWithStatement or Statement,
- call problematic_expression_unwrap in it
- problematic_expr--
- else if problematic_expr == 0, just add the unchanged expression
- else if NoSideEffects and doesn't have short-circuit, just add the unchanged expression
- else call problematic_expression_unwrap in it
- if type is Expression, check if there are statements or Both inside.
- if there are, problematic_expression_unwrap in it
- aftewards, use on_expr_as_statement to get it
- helpers:
- try_call_unwrap_statement: (returns texpr option)
- if underlying statement is TBinop(OpAssign/OpAssignOp), or TVar, with the right side being a Statement or a short circuit op, we can call apply_assign.
- apply_assign:
- if is TVar, first declare the tvar with default expression = null;
- will receive the left and right side of the assignment; right-side must be Statement
- see if right side is a short-circuit operation, call short_circuit_op_unwrap
- else see eexpr of the right side
- if it's void, just add the statement with add_statement, and set the right side as null;
- if not, it will have a block inside. set the left side = to the last expression on each block inside. add_statement for it.
- short_circuit_op_unwrap: x() && (1 + {var x = 0; x + 1;} == 2) && z()
- -> var x = x();
- var y = false;
- var z = false;
- if (x) //for &&, neg for ||
- {
- var temp = null;
- {
- var x = 0;
- temp = x + 1;
- }
- y = (1 + temp) == 2;
- if (y)
- {
- z = z();
- }
- }
- expects to receive a texpr with TBinop(OpBoolAnd/OpBoolOr)
- will traverse the AST while there is a TBinop(OpBoolAnd/OpBoolOr) as a right-side expr, and declare new temp vars in the for each found.
- will collect the return value, a mapped expr with all exprs as TLocal of the temp vars created
- problematic_expression_unwrap:
- check expr_kind:
- if it is NoSideEffects and not short-circuit, leave it there
- if it is ExprWithStatement and not short-circuit, call Type.map_expr problematic_expression_unwrap
- if it is Statement or Expression or short-circuit expr, call add_assign for this expression
- add_assign:
- see if the type is void. If it is, just add_statement the expression argument, and return a null value
- else create a new variable, set TVar with Some() with the expression argument, add TVar with add_statement, and return the TLocal of this expression.
- map_problematic_expr:
- call expr_stat_map on statement with problematic_expression_unwrap
- types:
- type shallow_expr_type = | Statement | Expression | Both (* shallow expression classification. Both means that they can be either Statements as Expressions *)
- type expr_kind = | NormalExpr | ExprNoSideEffects (* -> short-circuit is considered side-effects *) | ExprWithStatement | Statement
- evaluates an expression (as in not a statement) type. If it is ExprWithStatement or Statement, it means it contains errors
- functions:
- shallow_expr_type (expr:texpr) : shallow_expr_type
- expr_kind (expr:texpr) : expr_kind
- deeply evaluates an expression type
- expr_stat_map (fn:texpr->texpr) (expr:texpr) : texpr
- it will traverse the AST looking for places where an expression is expected, and map the value according to fn
- aggregate_expr_type (is_side_effects_free:bool) (children:expr_type list) : expr_type
- helper function to deal with expr_type aggregation (e.g. an Expression + a Statement as a children, is a ExprWithStatement)
- check_statement_in_expression (expr:texpr) : texpr option :
- will check
- *)
- type shallow_expr_type = | Statement | Expression of texpr | Both of texpr (* shallow expression classification. Both means that they can be either Statements as Expressions *)
- type expr_kind = | KNormalExpr | KNoSideEffects (* -> short-circuit is considered side-effects *) | KExprWithStatement | KStatement
- let rec no_paren e =
- match e.eexpr with
- | TParenthesis e -> no_paren e
- | _ -> e
- (* must be called in a statement. Will execute fn whenever an expression (not statement) is expected *)
- let rec expr_stat_map fn (expr:texpr) =
- match (no_paren expr).eexpr with
- | TBinop ( (Ast.OpAssign as op), left_e, right_e )
- | TBinop ( (Ast.OpAssignOp _ as op), left_e, right_e ) ->
- { expr with eexpr = TBinop(op, fn left_e, fn right_e) }
- | TParenthesis _ -> assert false
- | TCall(left_e, params) ->
- { expr with eexpr = TCall(fn left_e, List.map fn params) }
- | TNew(cl, tparams, params) ->
- { expr with eexpr = TNew(cl, tparams, List.map fn params) }
- | TVar(v,eopt) ->
- { expr with eexpr = TVar(v, Option.map fn eopt) }
- | TFor (v,cond,block) ->
- { expr with eexpr = TFor(v, fn cond, block) }
- | TIf(cond,eif,eelse) ->
- { expr with eexpr = TIf(fn cond, eif, eelse) }
- | TWhile(cond, block, flag) ->
- { expr with eexpr = TWhile(fn cond, block, flag) }
- | TSwitch(cond, el_block_l, default) ->
- { expr with eexpr = TSwitch( fn cond, List.map (fun (el,block) -> (List.map fn el, block)) el_block_l, default ) }
- (* | TMatch(cond, enum, cases, default) ->
- { expr with eexpr = TMatch(fn cond, enum, cases, default) } *)
- | TReturn(eopt) ->
- { expr with eexpr = TReturn(Option.map fn eopt) }
- | TThrow (texpr) ->
- { expr with eexpr = TThrow(fn texpr) }
- | TBreak
- | TContinue
- | TTry _
- | TUnop (Ast.Increment, _, _)
- | TUnop (Ast.Decrement, _, _) (* unop is a special case because the haxe compiler won't let us generate complex expressions with Increment/Decrement *)
- | TBlock _ -> expr (* there is no expected expression here. Only statements *)
- | TMeta(m,e) ->
- { expr with eexpr = TMeta(m,expr_stat_map fn e) }
- | _ -> assert false (* we only expect valid statements here. other expressions aren't valid statements *)
- let is_expr = function | Expression _ -> true | _ -> false
- let aggregate_expr_type map_fn side_effects_free children =
- let rec loop acc children =
- match children with
- | [] -> acc
- | hd :: children ->
- match acc, map_fn hd with
- | _, KExprWithStatement
- | _, KStatement
- | KExprWithStatement, _
- | KStatement, _ -> KExprWithStatement
- | KNormalExpr, KNoSideEffects
- | KNoSideEffects, KNormalExpr
- | KNormalExpr, KNormalExpr -> loop KNormalExpr children
- | KNoSideEffects, KNoSideEffects -> loop KNoSideEffects children
- in
- loop (if side_effects_free then KNoSideEffects else KNormalExpr) children
- (* statements: *)
- (* Error CS0201: Only assignment, call, increment, *)
- (* decrement, and new object expressions can be used as a *)
- (* statement (CS0201). *)
- let rec shallow_expr_type expr : shallow_expr_type =
- match expr.eexpr with
- | TCall _ when not (is_void expr.etype) -> Both expr
- | TNew _
- | TUnop (Ast.Increment, _, _)
- | TUnop (Ast.Decrement, _, _)
- | TBinop (Ast.OpAssign, _, _)
- | TBinop (Ast.OpAssignOp _, _, _) -> Both expr
- | TIf (cond, eif, Some(eelse)) -> (match aggregate_expr_type expr_kind true [cond;eif;eelse] with
- | KExprWithStatement -> Statement
- | _ -> Both expr)
- | TConst _
- | TLocal _
- | TArray _
- | TBinop _
- | TField _
- | TEnumParameter _
- | TTypeExpr _
- | TObjectDecl _
- | TArrayDecl _
- | TFunction _
- | TCast _
- | TUnop _ -> Expression (expr)
- | TParenthesis p | TMeta(_,p) -> shallow_expr_type p
- | TBlock ([e]) -> shallow_expr_type e
- | TCall _
- | TVar _
- | TBlock _
- | TFor _
- | TWhile _
- | TSwitch _
- | TTry _
- | TReturn _
- | TBreak
- | TContinue
- | TIf _
- | TThrow _ -> Statement
- and expr_kind expr =
- match shallow_expr_type expr with
- | Statement -> KStatement
- | Both expr | Expression expr ->
- let aggregate = aggregate_expr_type expr_kind in
- match expr.eexpr with
- | TConst _
- | TLocal _
- | TFunction _
- | TTypeExpr _ ->
- KNoSideEffects
- | TCall (ecall, params) ->
- aggregate false (ecall :: params)
- | TNew (_,_,params) ->
- aggregate false params
- | TUnop (Increment,_,e)
- | TUnop (Decrement,_,e) ->
- aggregate false [e]
- | TUnop (_,_,e) ->
- aggregate true [e]
- | TBinop (Ast.OpBoolAnd, e1, e2)
- | TBinop (Ast.OpBoolOr, e1, e2) -> (* TODO: should OpBool never be side-effects free? *)
- aggregate true [e1;e2]
- | TBinop (Ast.OpAssign, e1, e2)
- | TBinop (Ast.OpAssignOp _, e1, e2) ->
- aggregate false [e1;e2]
- | TBinop (_, e1, e2) ->
- aggregate true [e1;e2]
- | TIf (cond, eif, Some(eelse)) -> (match aggregate true [cond;eif;eelse] with
- | KExprWithStatement -> KStatement
- | k -> k)
- | TArray (e1,e2) ->
- aggregate true [e1;e2]
- | TParenthesis e
- | TMeta(_,e)
- | TField (e,_) ->
- aggregate true [e]
- | TArrayDecl (el) ->
- aggregate true el
- | TObjectDecl (sel) ->
- aggregate true (List.map snd sel)
- | TCast (e,_) ->
- aggregate true [e]
- | _ -> trace (debug_expr expr); assert false (* should have been read as Statement by shallow_expr_type *)
- let is_side_effects_free e =
- match expr_kind e with | KNoSideEffects -> true | _ -> false
- let get_kinds (statement:texpr) =
- let kinds = ref [] in
- ignore (expr_stat_map (fun e ->
- kinds := (expr_kind e) :: !kinds;
- e
- ) statement);
- List.rev !kinds
- let has_problematic_expressions (kinds:expr_kind list) =
- let rec loop kinds =
- match kinds with
- | [] -> false
- | KStatement :: _
- | KExprWithStatement :: _ -> true
- | _ :: tl -> loop tl
- in
- loop kinds
- let count_problematic_expressions (statement:texpr) =
- let count = ref 0 in
- ignore (expr_stat_map (fun e ->
- (match expr_kind e with
- | KStatement | KExprWithStatement -> incr count
- | _ -> ()
- );
- e
- ) statement);
- !count
- let apply_assign_block assign_fun elist =
- let rec assign acc elist =
- match elist with
- | [] -> acc
- | last :: [] ->
- (assign_fun last) :: acc
- | hd :: tl ->
- assign (hd :: acc) tl
- in
- List.rev (assign [] elist)
- let mk_get_block assign_fun e =
- match e.eexpr with
- | TBlock [] -> e
- | TBlock (el) ->
- { e with eexpr = TBlock(apply_assign_block assign_fun el) }
- | _ ->
- { e with eexpr = TBlock([ assign_fun e ]) }
- let add_assign gen add_statement expr =
- match expr.eexpr, follow expr.etype with
- | _, TAbstract ({ a_path = ([],"Void") },[])
- | TThrow _, _ ->
- add_statement expr;
- null expr.etype expr.epos
- | _ ->
- let var = mk_temp gen "stmt" expr.etype in
- let tvars = { expr with eexpr = TVar(var,Some(expr)) } in
- let local = { expr with eexpr = TLocal(var) } in
- add_statement tvars;
- local
- (* requirement: right must be a statement *)
- let rec apply_assign assign_fun right =
- match right.eexpr with
- | TBlock el ->
- { right with eexpr = TBlock(apply_assign_block assign_fun el) }
- | TSwitch (cond, elblock_l, default) ->
- { right with eexpr = TSwitch(cond, List.map (fun (el,block) -> (el, mk_get_block assign_fun block)) elblock_l, Option.map (mk_get_block assign_fun) default) }
- (* | TMatch (cond, ep, il_vlo_e_l, default) ->
- { right with eexpr = TMatch(cond, ep, List.map (fun (il,vlo,e) -> (il,vlo,mk_get_block assign_fun e)) il_vlo_e_l, Option.map (mk_get_block assign_fun) default) } *)
- | TTry (block, catches) ->
- { right with eexpr = TTry(mk_get_block assign_fun block, List.map (fun (v,block) -> (v,mk_get_block assign_fun block) ) catches) }
- | TIf (cond,eif,eelse) ->
- { right with eexpr = TIf(cond, mk_get_block assign_fun eif, Option.map (mk_get_block assign_fun) eelse) }
- | TThrow _
- | TWhile _
- | TFor _
- | TReturn _
- | TBreak
- | TContinue -> right
- | TParenthesis p | TMeta(_,p) ->
- apply_assign assign_fun p
- | TVar _ ->
- right
- | _ ->
- match follow right.etype with
- | TAbstract ({ a_path = ([], "Void") },[]) ->
- right
- | _ -> trace (debug_expr right); assert false (* a statement is required *)
- let short_circuit_op_unwrap gen add_statement expr :texpr =
- let do_not expr =
- { expr with eexpr = TUnop(Ast.Not, Ast.Prefix, expr) }
- in
- (* loop will always return its own TBlock, and the mapped expression *)
- let rec loop acc expr =
- match expr.eexpr with
- | TBinop ( (Ast.OpBoolAnd as op), left, right) ->
- let var = mk_temp gen "boolv" right.etype in
- let tvars = { right with eexpr = TVar(var, Some( { right with eexpr = TConst(TBool false); etype = gen.gcon.basic.tbool } )); etype = gen.gcon.basic.tvoid } in
- let local = { right with eexpr = TLocal(var) } in
- let mapped_left, ret_acc = loop ( (local, { right with eexpr = TBinop(Ast.OpAssign, local, right) } ) :: acc) left in
- add_statement tvars;
- ({ expr with eexpr = TBinop(op, mapped_left, local) }, ret_acc)
- (* we only accept OpBoolOr when it's the first to be evaluated *)
- | TBinop ( (Ast.OpBoolOr as op), left, right) when acc = [] ->
- let left = match left.eexpr with
- | TLocal _ | TConst _ -> left
- | _ -> add_assign gen add_statement left
- in
- let var = mk_temp gen "boolv" right.etype in
- let tvars = { right with eexpr = TVar(var, Some( { right with eexpr = TConst(TBool false); etype = gen.gcon.basic.tbool } )); etype = gen.gcon.basic.tvoid } in
- let local = { right with eexpr = TLocal(var) } in
- add_statement tvars;
- ({ expr with eexpr = TBinop(op, left, local) }, [ do_not left, { right with eexpr = TBinop(Ast.OpAssign, local, right) } ])
- | _ when acc = [] -> assert false
- | _ ->
- let var = mk_temp gen "boolv" expr.etype in
- let tvars = { expr with eexpr = TVar(var, Some( { expr with etype = gen.gcon.basic.tbool } )); etype = gen.gcon.basic.tvoid } in
- let local = { expr with eexpr = TLocal(var) } in
- let last_local = ref local in
- let acc = List.map (fun (local, assign) ->
- let l = !last_local in
- last_local := local;
- (l, assign)
- ) acc in
- add_statement tvars;
- (local, acc)
- in
- let mapped_expr, local_assign_list = loop [] expr in
- let rec loop local_assign_list : texpr =
- match local_assign_list with
- | [local, assign] ->
- { eexpr = TIf(local, assign, None); etype = gen.gcon.basic.tvoid; epos = assign.epos }
- | (local, assign) :: tl ->
- { eexpr = TIf(local,
- {
- eexpr = TBlock ( assign :: [loop tl] );
- etype = gen.gcon.basic.tvoid;
- epos = assign.epos;
- },
- None); etype = gen.gcon.basic.tvoid; epos = assign.epos }
- | [] -> assert false
- in
- add_statement (loop local_assign_list);
- mapped_expr
- (* there are two short_circuit fuctions as I'm still testing the best way to do it *)
- (*let short_circuit_op_unwrap gen add_statement expr :texpr =
- let block = ref [] in
- let rec short_circuit_op_unwrap is_first last_block expr =
- match expr.eexpr with
- | TBinop ( (Ast.OpBoolAnd as op), left, right)
- | TBinop ( (Ast.OpBoolOr as op), left, right) ->
- let var = mk_temp gen "boolv" left.etype in
- let tvars = { left with eexpr = TVar([var, if is_first then Some(left) else Some( { left with eexpr = TConst(TBool false) } )]); etype = gen.gcon.basic.tvoid } in
- let local = { left with eexpr = TLocal(var) } in
- if not is_first then begin
- last_block := !last_block @ [ { left with eexpr = TBinop(Ast.OpAssign, local, left) } ]
- end;
- add_statement tvars;
- let local_op = match op with | Ast.OpBoolAnd -> local | Ast.OpBoolOr -> { local with eexpr = TUnop(Ast.Not, Ast.Prefix, local) } | _ -> assert false in
- let new_block = ref [] in
- let new_right = short_circuit_op_unwrap false new_block right in
- last_block := !last_block @ [ { expr with eexpr = TIf(local_op, { right with eexpr = TBlock(!new_block) }, None) } ];
- { expr with eexpr = TBinop(op, local, new_right) }
- | _ when is_first -> assert false
- | _ ->
- let var = mk_temp gen "boolv" expr.etype in
- let tvars = { expr with eexpr = TVar([var, Some ( { expr with eexpr = TConst(TBool false) } ) ]); etype = gen.gcon.basic.tvoid } in
- let local = { expr with eexpr = TLocal(var) } in
- last_block := !last_block @ [ { expr with eexpr = TBinop(Ast.OpAssign, local, expr) } ];
- add_statement tvars;
- local
- in
- let mapped_expr = short_circuit_op_unwrap true block expr in
- add_statement { eexpr = TBlock(!block); etype = gen.gcon.basic.tvoid; epos = expr.epos };
- mapped_expr*)
- let twhile_with_condition_statement gen add_statement twhile cond e1 flag =
- (* when a TWhile is found with a problematic condition *)
- let basic = gen.gcon.basic in
- let block = if flag = Ast.NormalWhile then
- { e1 with eexpr = TIf(cond, e1, Some({ e1 with eexpr = TBreak; etype = basic.tvoid })) }
- else
- Type.concat e1 { e1 with
- eexpr = TIf({
- eexpr = TUnop(Ast.Not, Ast.Prefix, mk_paren cond);
- etype = basic.tbool;
- epos = cond.epos
- }, { e1 with eexpr = TBreak; etype = basic.tvoid }, None);
- etype = basic.tvoid
- }
- in
- add_statement { twhile with
- eexpr = TWhile(
- { eexpr = TConst(TBool true); etype = basic.tbool; epos = cond.epos },
- block,
- Ast.DoWhile
- );
- }
- let try_call_unwrap_statement gen problematic_expression_unwrap (add_statement:texpr->unit) (expr:texpr) : texpr option =
- let check_left left =
- match expr_kind left with
- | KExprWithStatement ->
- problematic_expression_unwrap add_statement left KExprWithStatement
- | KStatement -> assert false (* doesn't make sense a KStatement as a left side expression *)
- | _ -> left
- in
- let handle_assign op left right =
- let left = check_left left in
- Some (apply_assign (fun e -> { e with eexpr = TBinop(op, left, if is_void left.etype then e else gen.ghandle_cast left.etype e.etype e) }) right )
- in
- let handle_return e =
- Some( apply_assign (fun e ->
- match e.eexpr with
- | TThrow _ -> e
- | _ when is_void e.etype ->
- { e with eexpr = TBlock([e; { e with eexpr = TReturn None }]) }
- | _ ->
- { e with eexpr = TReturn( Some e ) }
- ) e )
- in
- let is_problematic_if right =
- match expr_kind right with
- | KStatement | KExprWithStatement -> true
- | _ -> false
- in
- match expr.eexpr with
- | TBinop((Ast.OpAssign as op),left,right)
- | TBinop((Ast.OpAssignOp _ as op),left,right) when shallow_expr_type right = Statement ->
- handle_assign op left right
- | TReturn( Some right ) when shallow_expr_type right = Statement ->
- handle_return right
- | TBinop((Ast.OpAssign as op),left, ({ eexpr = TBinop(Ast.OpBoolAnd,_,_) } as right) )
- | TBinop((Ast.OpAssign as op),left,({ eexpr = TBinop(Ast.OpBoolOr,_,_) } as right))
- | TBinop((Ast.OpAssignOp _ as op),left,({ eexpr = TBinop(Ast.OpBoolAnd,_,_) } as right) )
- | TBinop((Ast.OpAssignOp _ as op),left,({ eexpr = TBinop(Ast.OpBoolOr,_,_) } as right) ) ->
- let right = short_circuit_op_unwrap gen add_statement right in
- Some { expr with eexpr = TBinop(op, check_left left, right) }
- | TVar(v,Some({ eexpr = TBinop(Ast.OpBoolAnd,_,_) } as right))
- | TVar(v,Some({ eexpr = TBinop(Ast.OpBoolOr,_,_) } as right)) ->
- let right = short_circuit_op_unwrap gen add_statement right in
- Some { expr with eexpr = TVar(v, Some(right)) }
- | TVar(v,Some(right)) when shallow_expr_type right = Statement ->
- add_statement ({ expr with eexpr = TVar(v, Some(null right.etype right.epos)) });
- handle_assign Ast.OpAssign { expr with eexpr = TLocal(v); etype = v.v_type } right
- (* TIf handling *)
- | TBinop((Ast.OpAssign as op),left, ({ eexpr = TIf _ } as right))
- | TBinop((Ast.OpAssignOp _ as op),left,({ eexpr = TIf _ } as right)) when is_problematic_if right ->
- handle_assign op left right
- | TVar(v,Some({ eexpr = TIf _ } as right)) when is_problematic_if right ->
- add_statement ({ expr with eexpr = TVar(v, Some(null right.etype right.epos)) });
- handle_assign Ast.OpAssign { expr with eexpr = TLocal(v); etype = v.v_type } right
- | TWhile(cond, e1, flag) when is_problematic_if cond ->
- twhile_with_condition_statement gen add_statement expr cond e1 flag;
- Some (null expr.etype expr.epos)
- | _ -> None
- let traverse gen (on_expr_as_statement:texpr->texpr option) =
- let add_assign = add_assign gen in
- let problematic_expression_unwrap add_statement expr e_type =
- let rec problematic_expression_unwrap is_first expr e_type =
- match e_type, expr.eexpr with
- | _, TBinop(Ast.OpBoolAnd, _, _)
- | _, TBinop(Ast.OpBoolOr, _, _) -> add_assign add_statement expr (* add_assign so try_call_unwrap_expr *)
- | KNoSideEffects, _ -> expr
- | KStatement, _
- | KNormalExpr, _ -> add_assign add_statement expr
- | KExprWithStatement, TCall _
- | KExprWithStatement, TNew _
- | KExprWithStatement, TBinop (Ast.OpAssign,_,_)
- | KExprWithStatement, TBinop (Ast.OpAssignOp _,_,_)
- | KExprWithStatement, TUnop (Ast.Increment,_,_) (* all of these may have side-effects, so they must also be add_assign'ed . is_first avoids infinite loop *)
- | KExprWithStatement, TUnop (Ast.Decrement,_,_) when not is_first -> add_assign add_statement expr
- (* bugfix: Type.map_expr doesn't guarantee the correct order of execution *)
- | KExprWithStatement, TBinop(op,e1,e2) ->
- let e1 = problematic_expression_unwrap false e1 (expr_kind e1) in
- let e2 = problematic_expression_unwrap false e2 (expr_kind e2) in
- { expr with eexpr = TBinop(op, e1, e2) }
- | KExprWithStatement, TArray(e1,e2) ->
- let e1 = problematic_expression_unwrap false e1 (expr_kind e1) in
- let e2 = problematic_expression_unwrap false e2 (expr_kind e2) in
- { expr with eexpr = TArray(e1, e2) }
- (* bugfix: calls should not be transformed into closure calls *)
- | KExprWithStatement, TCall(( { eexpr = TField (ef_left, f) } as ef ), eargs) ->
- { expr with eexpr = TCall(
- { ef with eexpr = TField(problematic_expression_unwrap false ef_left (expr_kind ef_left), f) },
- List.map (fun e -> problematic_expression_unwrap false e (expr_kind e)) eargs)
- }
- | KExprWithStatement, _ -> Type.map_expr (fun e -> problematic_expression_unwrap false e (expr_kind e)) expr
- in
- problematic_expression_unwrap true expr e_type
- in
- let rec traverse e =
- match e.eexpr with
- | TBlock el ->
- let new_block = ref [] in
- let rec process_statement e =
- let e = no_paren e in
- match e.eexpr, shallow_expr_type e with
- | TCall( { eexpr = TLocal v } as elocal, elist ), _ when String.get v.v_name 0 = '_' && Hashtbl.mem gen.gspecial_vars v.v_name ->
- new_block := { e with eexpr = TCall( elocal, List.map (fun e ->
- match e.eexpr with
- | TBlock _ -> traverse e
- | _ -> e
- ) elist ) } :: !new_block
- | _, Statement | _, Both _ ->
- let e = match e.eexpr with | TReturn (Some ({ eexpr = TThrow _ } as ethrow)) -> ethrow | _ -> e in
- let kinds = get_kinds e in
- if has_problematic_expressions kinds then begin
- match try_call_unwrap_statement gen problematic_expression_unwrap add_statement e with
- | Some { eexpr = TConst(TNull) } (* no op *)
- | Some { eexpr = TBlock [] } -> ()
- | Some e ->
- if has_problematic_expressions (get_kinds e) then begin
- process_statement e
- end else
- new_block := (traverse e) :: !new_block
- | None ->
- (
- let acc = ref kinds in
- let new_e = expr_stat_map (fun e ->
- match !acc with
- | hd :: tl ->
- acc := tl;
- if has_problematic_expressions (hd :: tl) then begin
- problematic_expression_unwrap add_statement e hd
- end else
- e
- | [] -> assert false
- ) e in
- new_block := (traverse new_e) :: !new_block
- )
- end else begin new_block := (traverse e) :: !new_block end
- | _, Expression e ->
- match on_expr_as_statement e with
- | None -> ()
- | Some e -> process_statement e
- and add_statement expr =
- process_statement expr
- in
- List.iter (process_statement) el;
- let block = List.rev !new_block in
- { e with eexpr = TBlock(block) }
- | TTry (block, catches) ->
- { e with eexpr = TTry(traverse (mk_block block), List.map (fun (v,block) -> (v, traverse (mk_block block))) catches) }
- (* | TMatch (cond,ep,il_vol_e_l,default) ->
- { e with eexpr = TMatch(cond,ep,List.map (fun (il,vol,e) -> (il,vol,traverse (mk_block e))) il_vol_e_l, Option.map (fun e -> traverse (mk_block e)) default) } *)
- | TSwitch (cond,el_e_l, default) ->
- { e with eexpr = TSwitch(cond, List.map (fun (el,e) -> (el, traverse (mk_block e))) el_e_l, Option.map (fun e -> traverse (mk_block e)) default) }
- | TWhile (cond,block,flag) ->
- {e with eexpr = TWhile(cond,traverse (mk_block block), flag) }
- | TIf (cond, eif, eelse) ->
- { e with eexpr = TIf(cond, traverse (mk_block eif), Option.map (fun e -> traverse (mk_block e)) eelse) }
- | TFor (v,it,block) ->
- { e with eexpr = TFor(v,it, traverse (mk_block block)) }
- | TFunction (tfunc) ->
- { e with eexpr = TFunction({ tfunc with tf_expr = traverse (mk_block tfunc.tf_expr) }) }
- | _ -> e (* if expression doesn't have a block, we will exit *)
- in
- traverse
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* Casts detection v2 *)
- (* ******************************************* *)
- (*
- Will detect implicit casts and add TCast for them. Since everything is already followed by follow_all, typedefs are considered a new type altogether
- Types shouldn't be cast if:
- * When an instance is being coerced to a superclass or to an implemented interface
- * When anything is being coerced to Dynamic
- edit:
- As a matter of performance, we will also run the type parameters casts in here. Otherwise the exact same computation would have to be performed twice,
- with maybe even some loss of information
- * TAnon / TDynamic will call
- * Type parameter handling will be abstracted
- dependencies:
- Must run before ExpressionUnwrap
- *)
- module CastDetect =
- struct
- let name = "cast_detect_2"
- let priority = solve_deps name [DBefore TypeParams.priority; DBefore ExpressionUnwrap.priority]
- (* ******************************************* *)
- (* ReturnCast *)
- (* ******************************************* *)
- (*
- Cast detection for return types can't be done at CastDetect time, since we need an
- unwrapped expression to make sure we catch all return cast detections. So this module
- is specifically to deal with that, and is configured automatically by CastDetect
- dependencies:
- *)
- module ReturnCast =
- struct
- let name = "return_cast"
- let priority = solve_deps name [DAfter priority; DAfter ExpressionUnwrap.priority]
- let default_implementation gen =
- let rec extract_expr e = match e.eexpr with
- | TParenthesis e
- | TMeta (_,e)
- | TCast(e,_) -> extract_expr e
- | _ -> e
- in
- let current_ret_type = ref None in
- let handle e tto tfrom = gen.ghandle_cast (gen.greal_type tto) (gen.greal_type tfrom) e in
- let in_value = ref false in
- let rec run e =
- let was_in_value = !in_value in
- in_value := true;
- match e.eexpr with
- | TReturn (eopt) ->
- (* a return must be inside a function *)
- let ret_type = match !current_ret_type with | Some(s) -> s | None -> gen.gcon.error "Invalid return outside function declaration." e.epos; assert false in
- (match eopt with
- | None when not (is_void ret_type) ->
- { e with eexpr = TReturn( Some(null ret_type e.epos)) }
- | None -> e
- | Some eret ->
- { e with eexpr = TReturn( Some(handle (run eret) ret_type eret.etype ) ) })
- | TFunction(tfunc) ->
- let last_ret = !current_ret_type in
- current_ret_type := Some(tfunc.tf_type);
- let ret = Type.map_expr run e in
- current_ret_type := last_ret;
- ret
- | TBlock el ->
- { e with eexpr = TBlock ( List.map (fun e -> in_value := false; run e) el ) }
- | TBinop ( (Ast.OpAssign as op),e1,e2)
- | TBinop ( (Ast.OpAssignOp _ as op),e1,e2) when was_in_value ->
- let e1 = extract_expr (run e1) in
- let r = { e with eexpr = TBinop(op, e1, handle (run e2) e1.etype e2.etype); etype = e1.etype } in
- handle r e.etype e1.etype
- | TBinop ( (Ast.OpAssign as op),({ eexpr = TField(tf, f) } as e1), e2 )
- | TBinop ( (Ast.OpAssignOp _ as op),({ eexpr = TField(tf, f) } as e1), e2 ) ->
- (match field_access_esp gen (gen.greal_type tf.etype) (f) with
- | FClassField(cl,params,_,_,is_static,actual_t,_) ->
- let actual_t = if is_static then actual_t else apply_params cl.cl_params params actual_t in
- let e1 = extract_expr (run e1) in
- { e with eexpr = TBinop(op, e1, handle (run e2) actual_t e2.etype); etype = e1.etype }
- | _ ->
- let e1 = extract_expr (run e1) in
- { e with eexpr = TBinop(op, e1, handle (run e2) e1.etype e2.etype); etype = e1.etype }
- )
- | TBinop ( (Ast.OpAssign as op),e1,e2)
- | TBinop ( (Ast.OpAssignOp _ as op),e1,e2) ->
- let e1 = extract_expr (run e1) in
- { e with eexpr = TBinop(op, e1, handle (run e2) e1.etype e2.etype); etype = e1.etype }
- | _ -> Type.map_expr run e
- in
- run
- let configure gen =
- let map e = Some(default_implementation gen e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- let get_args t = match follow t with
- | TFun(args,ret) -> args,ret
- | _ -> trace (debug_type t); assert false
- let s_path (pack,n) = (String.concat "." (pack @ [n]))
- (*
- Since this function is applied under native-context only, the type paraters will already be changed
- *)
- let map_cls gen also_implements fn super =
- let rec loop c tl =
- if c == super then
- fn c tl
- else (match c.cl_super with
- | None -> false
- | Some (cs,tls) ->
- let tls = gen.greal_type_param (TClassDecl cs) tls in
- loop cs (List.map (apply_params c.cl_params tl) tls)
- ) || (if also_implements then List.exists (fun (cs,tls) ->
- loop cs (List.map (apply_params c.cl_params tl) tls)
- ) c.cl_implements else false)
- in
- loop
- let follow_dyn t = match follow t with
- | TMono _ | TLazy _ -> t_dynamic
- | t -> t
- (*
- this has a slight change from the type.ml version, in which it doesn't
- change a TMono into the other parameter
- *)
- let rec type_eq gen param a b =
- if a == b then
- ()
- else match follow_dyn (gen.greal_type a) , follow_dyn (gen.greal_type b) with
- | TEnum (e1,tl1) , TEnum (e2,tl2) ->
- if e1 != e2 && not (param = EqCoreType && e1.e_path = e2.e_path) then Type.error [cannot_unify a b];
- List.iter2 (type_eq gen param) tl1 tl2
- | TAbstract (a1,tl1) , TAbstract (a2,tl2) ->
- if a1 != a2 && not (param = EqCoreType && a1.a_path = a2.a_path) then Type.error [cannot_unify a b];
- List.iter2 (type_eq gen param) tl1 tl2
- | TInst (c1,tl1) , TInst (c2,tl2) ->
- if c1 != c2 && not (param = EqCoreType && c1.cl_path = c2.cl_path) && (match c1.cl_kind, c2.cl_kind with KExpr _, KExpr _ -> false | _ -> true) then Type.error [cannot_unify a b];
- List.iter2 (type_eq gen param) tl1 tl2
- | TFun (l1,r1) , TFun (l2,r2) when List.length l1 = List.length l2 ->
- (try
- type_eq gen param r1 r2;
- List.iter2 (fun (n,o1,t1) (_,o2,t2) ->
- if o1 <> o2 then Type.error [Not_matching_optional n];
- type_eq gen param t1 t2
- ) l1 l2
- with
- Unify_error l -> Type.error (cannot_unify a b :: l))
- | TDynamic a , TDynamic b ->
- type_eq gen param a b
- | TAnon a1, TAnon a2 ->
- (try
- PMap.iter (fun n f1 ->
- try
- let f2 = PMap.find n a2.a_fields in
- if f1.cf_kind <> f2.cf_kind && (param = EqStrict || param = EqCoreType || not (unify_kind f1.cf_kind f2.cf_kind)) then Type.error [invalid_kind n f1.cf_kind f2.cf_kind];
- try
- type_eq gen param f1.cf_type f2.cf_type
- with
- Unify_error l -> Type.error (invalid_field n :: l)
- with
- Not_found ->
- if is_closed a2 then Type.error [has_no_field b n];
- if not (link (ref None) b f1.cf_type) then Type.error [cannot_unify a b];
- a2.a_fields <- PMap.add n f1 a2.a_fields
- ) a1.a_fields;
- PMap.iter (fun n f2 ->
- if not (PMap.mem n a1.a_fields) then begin
- if is_closed a1 then Type.error [has_no_field a n];
- if not (link (ref None) a f2.cf_type) then Type.error [cannot_unify a b];
- a1.a_fields <- PMap.add n f2 a1.a_fields
- end;
- ) a2.a_fields;
- with
- Unify_error l -> Type.error (cannot_unify a b :: l))
- | _ , _ ->
- if b == t_dynamic && (param = EqRightDynamic || param = EqBothDynamic) then
- ()
- else if a == t_dynamic && param = EqBothDynamic then
- ()
- else
- Type.error [cannot_unify a b]
- let type_iseq gen a b =
- try
- type_eq gen EqStrict a b;
- true
- with
- Unify_error _ -> false
- (* will return true if both arguments are compatible. If it's not the case, a runtime error is very likely *)
- let is_cl_related gen cl tl super superl =
- let is_cl_related cl tl super superl = map_cls gen (gen.guse_tp_constraints || (match cl.cl_kind,super.cl_kind with KTypeParameter _, _ | _,KTypeParameter _ -> false | _ -> true)) (fun _ _ -> true) super cl tl in
- is_cl_related cl tl super superl || is_cl_related super superl cl tl
- let rec is_unsafe_cast gen to_t from_t =
- match (follow to_t, follow from_t) with
- | TInst(cl_to, to_params), TInst(cl_from, from_params) ->
- not (is_cl_related gen cl_from from_params cl_to to_params)
- | TEnum(e_to, _), TEnum(e_from, _) ->
- e_to.e_path <> e_from.e_path
- | TFun _, TFun _ ->
- (* functions are never unsafe cast by default. This behavior might be changed *)
- (* with a later AST pass which will run through TFun to TFun casts *)
- false
- | TMono _, _
- | _, TMono _
- | TDynamic _, _
- | _, TDynamic _ ->
- false
- | TAnon _, _
- | _, TAnon _ ->
- (* anonymous are never unsafe also. *)
- (* Though they will generate a cast, so if this cast is unneeded it's better to avoid them by tweaking gen.greal_type *)
- false
- | TAbstract _, _
- | _, TAbstract _ ->
- (try
- unify from_t to_t;
- false
- with | Unify_error _ ->
- try
- unify to_t from_t; (* still not unsafe *)
- false
- with | Unify_error _ ->
- true)
- | _ -> true
- let unifies tfrom tto = try
- unify tfrom tto;
- true
- with | _ ->
- false
- let do_unsafe_cast gen from_t to_t e =
- let t_path t =
- match t with
- | TInst(cl, _) -> cl.cl_path
- | TEnum(e, _) -> e.e_path
- | TType(t, _) -> t.t_path
- | TAbstract(a, _) -> a.a_path
- | TDynamic _ -> ([], "Dynamic")
- | _ -> raise Not_found
- in
- match gen.gfollow#run_f from_t, gen.gfollow#run_f to_t with
- | TInst({ cl_kind = KTypeParameter tl },_), t2 when List.exists (fun t -> unifies t t2) tl ->
- mk_cast to_t (mk_cast t_dynamic e)
- | _ ->
- let do_default () =
- gen.gon_unsafe_cast to_t e.etype e.epos;
- mk_cast to_t (mk_cast t_dynamic e)
- in
- (* TODO: there really should be a better way to write that *)
- try
- if (Hashtbl.find gen.gsupported_conversions (t_path from_t)) from_t to_t then
- mk_cast to_t e
- else
- do_default()
- with
- | Not_found ->
- try
- if (Hashtbl.find gen.gsupported_conversions (t_path to_t)) from_t to_t then
- mk_cast to_t e
- else
- do_default()
- with
- | Not_found -> do_default()
- (* ****************************** *)
- (* cast handler *)
- (* decides if a cast should be emitted, given a from and a to type *)
- (*
- this function is like a mini unify, without e.g. subtyping, which makes sense
- at the backend level, since most probably Anons and TInst will have a different representation there
- *)
- let rec handle_cast gen e real_to_t real_from_t =
- let do_unsafe_cast () = do_unsafe_cast gen real_from_t real_to_t { e with etype = real_from_t } in
- let to_t, from_t = real_to_t, real_from_t in
- let mk_cast t e =
- match e.eexpr with
- (* TThrow is always typed as Dynamic, we just need to type it accordingly *)
- | TThrow _ -> { e with etype = t }
- | _ -> mk_cast t e
- in
- let e = { e with etype = real_from_t } in
- if try fast_eq real_to_t real_from_t with Invalid_argument("List.for_all2") -> false then e else
- match real_to_t, real_from_t with
- (* string is the only type that can be implicitly converted from any other *)
- | TInst( { cl_path = ([], "String") }, []), _ ->
- mk_cast to_t e
- | TInst(cl_to, params_to), TInst(cl_from, params_from) ->
- let ret = ref None in
- (*
- this is a little confusing:
- we are here mapping classes until we have the same to and from classes, applying the type parameters in each step, so we can
- compare the type parameters;
- If a class is found - meaning that the cl_from can be converted without a cast into cl_to,
- we still need to check their type parameters.
- *)
- ignore (map_cls gen (gen.guse_tp_constraints || (match cl_from.cl_kind,cl_to.cl_kind with KTypeParameter _, _ | _,KTypeParameter _ -> false | _ -> true)) (fun _ tl ->
- try
- (* type found, checking type parameters *)
- List.iter2 (type_eq gen EqStrict) tl params_to;
- ret := Some e;
- true
- with | Unify_error _ ->
- (* type parameters need casting *)
- if gen.ghas_tparam_cast_handler then begin
- (*
- if we are already handling type parameter casts on other part of code (e.g. RealTypeParameters),
- we'll just make a cast to indicate that this place needs type parameter-involved casting
- *)
- ret := Some (mk_cast to_t e);
- true
- end else
- (*
- if not, we're going to check if we only need a simple cast,
- or if we need to first cast into the dynamic version of it
- *)
- try
- List.iter2 (type_eq gen EqRightDynamic) tl params_to;
- ret := Some (mk_cast to_t e);
- true
- with | Unify_error _ ->
- ret := Some (mk_cast to_t (mk_cast (TInst(cl_to, List.map (fun _ -> t_dynamic) params_to)) e));
- true
- ) cl_to cl_from params_from);
- if is_some !ret then
- get !ret
- else if is_cl_related gen cl_from params_from cl_to params_to then
- mk_cast to_t e
- else
- (* potential unsafe cast *)
- (do_unsafe_cast ())
- | TMono _, TMono _
- | TMono _, TDynamic _
- | TDynamic _, TDynamic _
- | TDynamic _, TMono _ ->
- e
- | TMono _, _
- | TDynamic _, _
- | TAnon _, _ when gen.gneeds_box real_from_t ->
- mk_cast to_t e
- | TMono _, _
- | TDynamic _, _ -> e
- | _, TMono _
- | _, TDynamic _ -> mk_cast to_t e
- | TAnon (a_to), TAnon (a_from) ->
- if a_to == a_from then
- e
- else if type_iseq gen to_t from_t then (* FIXME apply unify correctly *)
- e
- else
- mk_cast to_t e
- | _, TAnon(anon) -> (try
- let p2 = match !(anon.a_status) with
- | Statics c -> TInst(c,List.map (fun _ -> t_dynamic) c.cl_params)
- | EnumStatics e -> TEnum(e, List.map (fun _ -> t_dynamic) e.e_params)
- | AbstractStatics a -> TAbstract(a, List.map (fun _ -> t_dynamic) a.a_params)
- | _ -> raise Not_found
- in
- let tclass = match get_type gen ([],"Class") with
- | TAbstractDecl(a) -> a
- | _ -> assert false in
- handle_cast gen e real_to_t (gen.greal_type (TAbstract(tclass, [p2])))
- with | Not_found ->
- mk_cast to_t e)
- | TAbstract (a_to, _), TAbstract(a_from, _) when a_to == a_from ->
- e
- | TAbstract _, TInst({ cl_kind = KTypeParameter _ }, _)
- | TInst({ cl_kind = KTypeParameter _ }, _), TAbstract _ ->
- do_unsafe_cast()
- | TAbstract _, _
- | _, TAbstract _ ->
- (try
- unify from_t to_t;
- mk_cast to_t e
- with | Unify_error _ ->
- try
- unify to_t from_t;
- mk_cast to_t e
- with | Unify_error _ ->
- do_unsafe_cast())
- | TEnum(e_to, []), TEnum(e_from, []) ->
- if e_to == e_from then
- e
- else
- (* potential unsafe cast *)
- (do_unsafe_cast ())
- | TEnum(e_to, params_to), TEnum(e_from, params_from) when e_to.e_path = e_from.e_path ->
- (try
- List.iter2 (type_eq gen (if gen.gallow_tp_dynamic_conversion then EqRightDynamic else EqStrict)) params_from params_to;
- e
- with
- | Unify_error _ -> do_unsafe_cast ()
- )
- | TEnum(en, params_to), TInst(cl, params_from)
- | TInst(cl, params_to), TEnum(en, params_from) ->
- (* this is here for max compatibility with EnumsToClass module *)
- if en.e_path = cl.cl_path && en.e_extern then begin
- (try
- List.iter2 (type_eq gen (if gen.gallow_tp_dynamic_conversion then EqRightDynamic else EqStrict)) params_from params_to;
- e
- with
- | Invalid_argument("List.iter2") ->
- (*
- this is a hack for RealTypeParams. Since there is no way at this stage to know if the class is the actual
- EnumsToClass derived from the enum, we need to imply from possible ArgumentErrors (because of RealTypeParams interfaces),
- that they would only happen if they were a RealTypeParams created interface
- *)
- e
- | Unify_error _ -> do_unsafe_cast ()
- )
- end else
- do_unsafe_cast ()
- | TType(t_to, params_to), TType(t_from, params_from) when t_to == t_from ->
- if gen.gspecial_needs_cast real_to_t real_from_t then
- (try
- List.iter2 (type_eq gen (if gen.gallow_tp_dynamic_conversion then EqRightDynamic else EqStrict)) params_from params_to;
- e
- with
- | Unify_error _ -> do_unsafe_cast ()
- )
- else
- e
- | TType(t_to, _), TType(t_from,_) ->
- if gen.gspecial_needs_cast real_to_t real_from_t then
- mk_cast to_t e
- else
- e
- | TType _, _ when gen.gspecial_needs_cast real_to_t real_from_t ->
- mk_cast to_t e
- | _, TType _ when gen.gspecial_needs_cast real_to_t real_from_t ->
- mk_cast to_t e
- (*| TType(t_to, _), TType(t_from, _) ->
- if t_to.t_path = t_from.t_path then
- e
- else if is_unsafe_cast gen real_to_t real_from_t then (* is_unsafe_cast will already follow both *)
- (do_unsafe_cast ())
- else
- mk_cast to_t e*)
- | TType _, _
- | _, TType _ ->
- if is_unsafe_cast gen real_to_t real_from_t then (* is_unsafe_cast will already follow both *)
- (do_unsafe_cast ())
- else
- mk_cast to_t e
- | TAnon anon, _ ->
- if PMap.is_empty anon.a_fields then
- e
- else
- mk_cast to_t e
- | TFun(args, ret), TFun(args2, ret2) ->
- let get_args = List.map (fun (_,_,t) -> t) in
- (try List.iter2 (type_eq gen (EqBothDynamic)) (ret :: get_args args) (ret2 :: get_args args2); e with | Unify_error _ | Invalid_argument("List.iter2") -> mk_cast to_t e)
- | _, _ ->
- do_unsafe_cast ()
- (* end of cast handler *)
- (* ******************* *)
- let is_static_overload c name =
- match c.cl_super with
- | None -> false
- | Some (sup,_) ->
- let rec loop c =
- (PMap.mem name c.cl_statics) || (match c.cl_super with
- | None -> false
- | Some (sup,_) -> loop sup)
- in
- loop sup
- let does_unify a b =
- try
- unify a b;
- true
- with | Unify_error _ -> false
- (* this is a workaround for issue #1743, as FInstance() is returning the incorrect classfield *)
- let rec clean_t t = match follow t with
- | TAbstract(a,tl) when not (Meta.has Meta.CoreType a.a_meta) ->
- clean_t (Abstract.get_underlying_type a tl)
- | t -> t
- let select_overload gen applied_f overloads types params =
- let rec check_arg arglist elist =
- match arglist, elist with
- | [], [] -> true (* it is valid *)
- | (_,_,TAbstract({ a_path = (["haxe";"extern"],"Rest") }, [t])) :: [], elist ->
- List.for_all (fun (_,_,et) -> Type.type_iseq (clean_t et) (clean_t t)) elist
- | (_,_,t) :: arglist, (_,_,et) :: elist when Type.type_iseq (clean_t et) (clean_t t) ->
- check_arg arglist elist
- | _ -> false
- in
- match follow applied_f with
- | TFun _ ->
- replace_mono applied_f;
- let args, _ = get_fun applied_f in
- let elist = List.rev args in
- let rec check_overload overloads =
- match overloads with
- | (t, cf) :: overloads ->
- let cft = apply_params types params t in
- let cft = monomorphs cf.cf_params cft in
- let args, _ = get_fun cft in
- if check_arg (List.rev args) elist then
- cf,t,false
- else if overloads = [] then
- cf,t,true (* no compatible overload was found *)
- else
- check_overload overloads
- | [] -> assert false
- in
- check_overload overloads
- | _ -> match overloads with (* issue #1742 *)
- | (t,cf) :: [] -> cf,t,true
- | (t,cf) :: _ -> cf,t,false
- | _ -> assert false
- let choose_ctor gen cl tparams etl maybe_empty_t p =
- let ctor, sup, stl = OverloadingConstructor.cur_ctor cl tparams in
- (* get returned stl, with Dynamic as t_empty *)
- let rec get_changed_stl c tl =
- if c == sup then
- tl
- else match c.cl_super with
- | None -> stl
- | Some(sup,stl) -> get_changed_stl sup (List.map (apply_params c.cl_params tl) stl)
- in
- let ret_tparams = List.map (fun t -> match follow t with
- | TDynamic _ | TMono _ -> t_empty
- | _ -> t) tparams in
- let ret_stl = get_changed_stl cl ret_tparams in
- let ctors = ctor :: ctor.cf_overloads in
- List.iter replace_mono etl;
- (* first filter out or select outright maybe_empty *)
- let ctors, is_overload = match etl, maybe_empty_t with
- | [t], Some empty_t ->
- let count = ref 0 in
- let is_empty_call = Type.type_iseq t empty_t in
- let ret = List.filter (fun cf -> match follow cf.cf_type with
- (* | TFun([_,_,t],_) -> incr count; true *)
- | TFun([_,_,t],_) ->
- replace_mono t; incr count; is_empty_call = (Type.type_iseq t empty_t)
- | _ -> false) ctors in
- ret, !count > 1
- | _ ->
- let len = List.length etl in
- let ret = List.filter (fun cf -> List.length (fst (get_fun cf.cf_type)) = len) ctors in
- ret, (match ret with | _ :: [] -> false | _ -> true)
- in
- let rec check_arg arglist elist =
- match arglist, elist with
- | [], [] -> true
- | (_,_,t) :: arglist, et :: elist -> (try
- let t = run_follow gen t in
- unify et t;
- check_arg arglist elist
- with | Unify_error el ->
- (* List.iter (fun el -> gen.gcon.warning (Typecore.unify_error_msg (print_context()) el) p) el; *)
- false)
- | _ ->
- false
- in
- let rec check_cf cf =
- let t = apply_params sup.cl_params stl cf.cf_type in
- replace_mono t;
- let args, _ = get_fun t in
- check_arg args etl
- in
- match is_overload, ctors with
- | false, [c] ->
- false, c, sup, ret_stl
- | _ ->
- is_overload, List.find check_cf ctors, sup, ret_stl
- let change_rest tfun elist =
- let rec loop acc arglist elist = match arglist, elist with
- | (_,_,TAbstract({ a_path = (["haxe";"extern"],"Rest") },[t])) :: [], elist ->
- List.rev (List.map (fun _ -> "rest",false,t) elist @ acc)
- | (n,o,t) :: arglist, _ :: elist ->
- loop ((n,o,t) :: acc) arglist elist
- | _, _ ->
- List.rev acc
- in
- let args,ret = get_fun tfun in
- TFun(loop [] args elist, ret)
- (*
- Type parameter handling
- It will detect if/what type parameters were used, and call the cast handler
- It will handle both TCall(TField) and TCall by receiving a texpr option field: e
- Also it will transform the type parameters with greal_type_param and make
- handle_impossible_tparam - should cases where the type parameter is impossible to be determined from the called parameters be Dynamic?
- e.g. static function test<T>():T {}
- *)
- (* match e.eexpr with | TCall( ({ eexpr = TField(ef, f) }) as e1, elist ) -> *)
- let handle_type_parameter gen e e1 ef ~clean_ef ~overloads_cast_to_base f elist calls_parameters_explicitly =
- (* the ONLY way to know if this call has parameters is to analyze the calling field. *)
- (* To make matters a little worse, on both C# and Java only in some special cases that type parameters will be used *)
- (* Namely, when using reflection type parameters are useless, of course. This also includes anonymous types *)
- (* this will have to be handled by gparam_func_call *)
- let return_var efield =
- match e with
- | None ->
- efield
- | Some ecall ->
- match follow efield.etype with
- | TFun(_,ret) ->
- (* closures will be handled by the closure handler. So we will just hint what's the expected type *)
- (* FIXME: should closures have also its arguments cast correctly? In the current implementation I think not. TO_REVIEW *)
- handle_cast gen { ecall with eexpr = TCall(efield, elist) } (gen.greal_type ecall.etype) ret
- | _ ->
- { ecall with eexpr = TCall(efield, elist) }
- in
- let real_type = gen.greal_type ef.etype in
- (* this part was rewritten at roughly r6477 in order to correctly support overloads *)
- (match field_access_esp gen real_type (f) with
- | FClassField (cl, params, _, cf, is_static, actual_t, declared_t) when e <> None && (cf.cf_kind = Method MethNormal || cf.cf_kind = Method MethInline) ->
- (* C# target changes params with a real_type function *)
- let params = match follow clean_ef.etype with
- | TInst(_,params) -> params
- | _ -> params
- in
- let ecall = get e in
- let ef = ref ef in
- let is_overload = cf.cf_overloads <> [] || Meta.has Meta.Overload cf.cf_meta || (is_static && is_static_overload cl (field_name f)) in
- let cf, actual_t, error = match is_overload with
- | false ->
- (* since actual_t from FClassField already applies greal_type, we're using the get_overloads helper to get this info *)
- let t = if cf.cf_params = [] then (* this if statement must be eliminated - it's a workaround for #3516 + infer params. *)
- actual_t
- else
- declared_t
- in
- cf,t,false
- | true ->
- let (cf, actual_t, error), is_static = match f with
- | FInstance(c,_,cf) | FClosure(Some (c,_),cf) ->
- (* get from overloads *)
- (* FIXME: this is a workaround for issue #1743 . Uncomment this code after it was solved *)
- (* let t, cf = List.find (fun (t,cf2) -> cf == cf2) (Typeload.get_overloads cl (field_name f)) in *)
- (* cf, t, false *)
- select_overload gen e1.etype (Typeload.get_overloads cl (field_name f)) cl.cl_params params, false
- | FStatic(c,f) ->
- (* workaround for issue #1743 *)
- (* f,f.cf_type, false *)
- select_overload gen e1.etype ((f.cf_type,f) :: List.map (fun f -> f.cf_type,f) f.cf_overloads) [] [], true
- | _ ->
- gen.gcon.warning "Overloaded classfield typed as anonymous" ecall.epos;
- (cf, actual_t, true), true
- in
- if not (is_static || error) then match find_first_declared_field gen cl ~exact_field:{ cf with cf_type = actual_t } cf.cf_name with
- | Some(cf_orig,actual_t,_,_,declared_cl,tl,tlch) ->
- let rec is_super e = match e.eexpr with
- | TConst TSuper -> true
- | TParenthesis p | TMeta(_,p) -> is_super p
- | _ -> false
- in
- if declared_cl != cl && overloads_cast_to_base && not (is_super !ef) then begin
- let pos = (!ef).epos in
- ef := {
- eexpr = TCall(
- { eexpr = TLocal(alloc_var "__as__" t_dynamic); etype = t_dynamic; epos = pos },
- [!ef]);
- etype = TInst(declared_cl,List.map (apply_params cl.cl_params params) tl);
- epos = pos
- }
- end;
- { cf_orig with cf_name = cf.cf_name },actual_t,false
- | None ->
- gen.gcon.warning "Cannot find matching overload" ecall.epos;
- cf, actual_t, true
- else
- cf,actual_t,error
- in
- (* take off Rest param *)
- let actual_t = change_rest actual_t elist in
- (* set the real (selected) class field *)
- let f = match f with
- | FInstance(c,tl,_) -> FInstance(c,tl,cf)
- | FClosure(c,_) -> FClosure(c,cf)
- | FStatic(c,_) -> FStatic(c,cf)
- | f -> f
- in
- let error = error || (match follow actual_t with | TFun _ -> false | _ -> true) in
- if error then (* if error, ignore arguments *)
- if is_void ecall.etype then
- { ecall with eexpr = TCall({ e1 with eexpr = TField(!ef, f) }, elist ) }
- else
- mk_cast ecall.etype { ecall with eexpr = TCall({ e1 with eexpr = TField(!ef, f) }, elist ) }
- else begin
- (* infer arguments *)
- (* let called_t = TFun(List.map (fun e -> "arg",false,e.etype) elist, ecall.etype) in *)
- let called_t = match follow e1.etype with | TFun _ -> e1.etype | _ -> TFun(List.map (fun e -> "arg",false,e.etype) elist, ecall.etype) in (* workaround for issue #1742 *)
- let called_t = change_rest called_t elist in
- let fparams = TypeParams.infer_params gen ecall.epos (get_fun (apply_params cl.cl_params params actual_t)) (get_fun called_t) cf.cf_params calls_parameters_explicitly in
- (* get what the backend actually sees *)
- (* actual field's function *)
- let actual_t = get_real_fun gen actual_t in
- let real_params = gen.greal_type_param (TClassDecl cl) params in
- let function_t = apply_params cl.cl_params real_params actual_t in
- let real_fparams = if calls_parameters_explicitly then
- gen.greal_type_param (TClassDecl cl) fparams
- else
- gen.greal_type_param (TClassDecl cl) (TypeParams.infer_params gen ecall.epos (get_fun function_t) (get_fun (get_real_fun gen called_t)) cf.cf_params calls_parameters_explicitly) in
- let function_t = get_real_fun gen (apply_params cf.cf_params real_fparams function_t) in
- let args_ft, ret_ft = get_fun function_t in
- (* applied function *)
- let applied = elist in
- (* check types list *)
- let new_ecall, elist = try
- let elist = List.map2 (fun applied (_,_,funct) ->
- match is_overload, applied.eexpr with
- | true, TConst TNull ->
- mk_cast (gen.greal_type funct) applied
- | true, _ -> (* when not (type_iseq gen (gen.greal_type applied.etype) funct) -> *)
- let ret = handle_cast gen applied (funct) (gen.greal_type applied.etype) in
- (match ret.eexpr with
- | TCast _ -> ret
- | _ -> mk_cast (funct) ret)
- | _ ->
- handle_cast gen applied (funct) (gen.greal_type applied.etype)
- ) applied args_ft in
- { ecall with
- eexpr = TCall(
- { e1 with eexpr = TField(!ef, f) },
- elist);
- }, elist
- with | Invalid_argument("List.map2") ->
- gen.gcon.warning ("This expression may be invalid" ) ecall.epos;
- { ecall with eexpr = TCall({ e1 with eexpr = TField(!ef, f) }, elist) }, elist
- in
- let new_ecall = if fparams <> [] then gen.gparam_func_call new_ecall { e1 with eexpr = TField(!ef, f) } fparams elist else new_ecall in
- let ret = handle_cast gen new_ecall (gen.greal_type ecall.etype) (gen.greal_type ret_ft) in
- (match gen.gcon.platform, cf.cf_params, ret.eexpr with
- | _, _, TCast _ -> ret
- | Java, _ :: _, _ ->
- (* this is a workaround for a javac openjdk issue with unused type parameters and return type inference *)
- (* see more at issue #3123 *)
- mk_cast (gen.greal_type ret_ft) new_ecall
- | _ -> ret)
- end
- | FClassField (cl,params,_,{ cf_kind = (Method MethDynamic | Var _) },_,actual_t,_) ->
- (* if it's a var, we will just try to apply the class parameters that have been changed with greal_type_param *)
- let t = apply_params cl.cl_params (gen.greal_type_param (TClassDecl cl) params) (gen.greal_type actual_t) in
- return_var (handle_cast gen { e1 with eexpr = TField(ef, f) } (gen.greal_type e1.etype) (gen.greal_type t))
- | FClassField (cl,params,_,cf,_,actual_t,_) ->
- return_var (handle_cast gen { e1 with eexpr = TField({ ef with etype = t_dynamic }, f) } e1.etype t_dynamic) (* force dynamic and cast back to needed type *)
- | FEnumField (en, efield, true) ->
- let ecall = match e with | None -> trace (field_name f); trace efield.ef_name; gen.gcon.error "This field should be called immediately" ef.epos; assert false | Some ecall -> ecall in
- (match en.e_params with
- (*
- | [] ->
- let args, ret = get_args (efield.ef_type) in
- let ef = { ef with eexpr = TTypeExpr( TEnumDecl en ); etype = TEnum(en, []) } in
- handle_cast gen { ecall with eexpr = TCall({ e1 with eexpr = TField(ef, FEnum(en, efield)) }, List.map2 (fun param (_,_,t) -> handle_cast gen param (gen.greal_type t) (gen.greal_type param.etype)) elist args) } (gen.greal_type ecall.etype) (gen.greal_type ret)
- *)
- | _ ->
- let pt = match e with | None -> real_type | Some _ -> snd (get_fun e1.etype) in
- let _params = match follow pt with | TEnum(_, p) -> p | _ -> gen.gcon.warning (debug_expr e1) e1.epos; assert false in
- let args, ret = get_args efield.ef_type in
- let actual_t = TFun(List.map (fun (n,o,t) -> (n,o,gen.greal_type t)) args, gen.greal_type ret) in
- (*
- because of differences on how <Dynamic> is handled on the platforms, this is a hack to be able to
- correctly use class field type parameters with RealTypeParams
- *)
- let cf_params = List.map (fun t -> match follow t with | TDynamic _ -> t_empty | _ -> t) _params in
- let t = apply_params en.e_params (gen.greal_type_param (TEnumDecl en) cf_params) actual_t in
- let t = apply_params efield.ef_params (List.map (fun _ -> t_dynamic) efield.ef_params) t in
- let args, ret = get_args t in
- let elist = List.map2 (fun param (_,_,t) -> handle_cast gen (param) (gen.greal_type t) (gen.greal_type param.etype)) elist args in
- let e1 = { e1 with eexpr = TField({ ef with eexpr = TTypeExpr( TEnumDecl en ); etype = TEnum(en, _params) }, FEnum(en, efield) ) } in
- let new_ecall = gen.gparam_func_call ecall e1 _params elist in
- handle_cast gen new_ecall (gen.greal_type ecall.etype) (gen.greal_type ret)
- )
- | FEnumField _ when is_some e -> assert false
- | FEnumField (en,efield,_) ->
- return_var { e1 with eexpr = TField({ ef with eexpr = TTypeExpr( TEnumDecl en ); },FEnum(en,efield)) }
- (* no target by date will uses this.so this code may not be correct at all *)
- | FAnonField cf ->
- let t = gen.greal_type cf.cf_type in
- return_var (handle_cast gen { e1 with eexpr = TField(ef, f) } (gen.greal_type e1.etype) t)
- | FNotFound
- | FDynamicField _ ->
- if is_some e then
- return_var { e1 with eexpr = TField(ef, f) }
- else
- return_var (handle_cast gen { e1 with eexpr = TField({ ef with etype = t_dynamic }, f) } e1.etype t_dynamic) (* force dynamic and cast back to needed type *)
- )
- (* end of type parameter handling *)
- (* ****************************** *)
- (** overloads_cast_to_base argument will cast overloaded function types to the class that declared it. **)
- (** This is necessary for C#, and if true, will require the target to implement __as__, as a `quicker` form of casting **)
- let default_implementation gen ?(native_string_cast = true) ?(overloads_cast_to_base = false) maybe_empty_t calls_parameters_explicitly =
- let handle e t1 t2 = handle_cast gen e (gen.greal_type t1) (gen.greal_type t2) in
- let in_value = ref false in
- let rec clean_cast e = match e.eexpr with
- | TCast(e,_) -> clean_cast e
- | TParenthesis(e) | TMeta(_,e) -> clean_cast e
- | _ -> e
- in
- let rec run ?(just_type = false) e =
- let handle = if not just_type then handle else fun e t1 t2 -> { e with etype = gen.greal_type t2 } in
- let was_in_value = !in_value in
- in_value := true;
- match e.eexpr with
- | TConst ( TInt _ | TFloat _ | TBool _ as const ) ->
- (* take off any Null<> that it may have *)
- let t = follow (run_follow gen e.etype) in
- (* do not allow constants typed as Single - need to cast them *)
- let real_t = match const with
- | TInt _ -> gen.gcon.basic.tint
- | TFloat _ -> gen.gcon.basic.tfloat
- | TBool _ -> gen.gcon.basic.tbool
- | _ -> assert false
- in
- handle e t real_t
- | TCast( { eexpr = TCall( { eexpr = TLocal { v_name = "__delegate__" } } as local, [del] ) } as e2, _) ->
- { e with eexpr = TCast({ e2 with eexpr = TCall(local, [Type.map_expr run del]) }, None) }
- | TBinop ( (Ast.OpAssign | Ast.OpAssignOp _ as op), e1, e2 ) ->
- let e1 = run ~just_type:true e1 in
- let e2 = handle (run e2) e1.etype e2.etype in
- { e with eexpr = TBinop(op, clean_cast e1, e2) }
- | TBinop ( (Ast.OpShl | Ast.OpShr | Ast.OpUShr as op), e1, e2 ) ->
- let e1 = run e1 in
- let e2 = handle (run e2) (gen.gcon.basic.tint) e2.etype in
- { e with eexpr = TBinop(op, e1, e2) }
- | TField(ef, f) ->
- handle_type_parameter gen None e (run ef) ~clean_ef:ef ~overloads_cast_to_base:overloads_cast_to_base f [] calls_parameters_explicitly
- | TArrayDecl el ->
- let et = e.etype in
- let base_type = match follow et with
- | TInst({ cl_path = ([], "Array") } as cl, bt) -> gen.greal_type_param (TClassDecl cl) bt
- | _ -> assert false
- in
- let base_type = List.hd base_type in
- { e with eexpr = TArrayDecl( List.map (fun e -> handle (run e) base_type e.etype) el ); etype = et }
- | TCall ({ eexpr = TLocal { v_name = "__array__" } } as arr_local, el) ->
- let et = e.etype in
- let base_type = match follow et with
- | TInst(cl, bt) -> gen.greal_type_param (TClassDecl cl) bt
- | _ -> assert false
- in
- let base_type = List.hd base_type in
- { e with eexpr = TCall(arr_local, List.map (fun e -> handle (run e) base_type e.etype) el ); etype = et }
- | TCall( ({ eexpr = TLocal v } as local), params ) when String.get v.v_name 0 = '_' && String.get v.v_name 1 = '_' && Hashtbl.mem gen.gspecial_vars v.v_name ->
- { e with eexpr = TCall(local, List.map (fun e -> (match e.eexpr with TBlock _ -> in_value := false | _ -> ()); run e) params) }
- | TCall( ({ eexpr = TField(ef, f) }) as e1, elist ) ->
- handle_type_parameter gen (Some e) (e1) (run ef) ~clean_ef:ef ~overloads_cast_to_base:overloads_cast_to_base f (List.map run elist) calls_parameters_explicitly
- (* the TNew and TSuper code was modified at r6497 *)
- | TCall( { eexpr = TConst TSuper } as ef, eparams ) ->
- let cl, tparams = match follow ef.etype with
- | TInst(cl,p) ->
- cl,p
- | _ -> assert false in
- (try
- let is_overload, cf, sup, stl = choose_ctor gen cl tparams (List.map (fun e -> e.etype) eparams) maybe_empty_t e.epos in
- let handle e t1 t2 =
- if is_overload then
- let ret = handle e t1 t2 in
- match ret.eexpr with
- | TCast _ -> ret
- | _ -> mk_cast (gen.greal_type t1) e
- else
- handle e t1 t2
- in
- let stl = gen.greal_type_param (TClassDecl sup) stl in
- let args, _ = get_fun (apply_params sup.cl_params stl cf.cf_type) in
- let eparams = List.map2 (fun e (_,_,t) ->
- handle (run e) t e.etype
- ) eparams args in
- { e with eexpr = TCall(ef, eparams) }
- with | Not_found ->
- gen.gcon.warning "No overload found for this constructor call" e.epos;
- { e with eexpr = TCall(ef, List.map run eparams) })
- | TCall (ef, eparams) ->
- (match ef.etype with
- | TFun(p, ret) ->
- handle ({ e with eexpr = TCall(run ef, List.map2 (fun param (_,_,t) -> handle (run param) t param.etype) eparams p) }) e.etype ret
- | _ -> Type.map_expr run e
- )
- (* the TNew and TSuper code was modified at r6497 *)
- | TNew ({ cl_kind = KTypeParameter _ }, _, _) ->
- Type.map_expr run e
- | TNew (cl, tparams, eparams) -> (try
- let is_overload, cf, sup, stl = choose_ctor gen cl tparams (List.map (fun e -> e.etype) eparams) maybe_empty_t e.epos in
- let handle e t1 t2 =
- if is_overload then
- let ret = handle e t1 t2 in
- match ret.eexpr with
- | TCast _ -> ret
- | _ -> mk_cast (gen.greal_type t1) e
- else
- handle e t1 t2
- in
- let stl = gen.greal_type_param (TClassDecl sup) stl in
- let args, _ = get_fun (apply_params sup.cl_params stl cf.cf_type) in
- let eparams = List.map2 (fun e (_,_,t) ->
- handle (run e) t e.etype
- ) eparams args in
- { e with eexpr = TNew(cl, tparams, eparams) }
- with | Not_found ->
- gen.gcon.warning "No overload found for this constructor call" e.epos;
- { e with eexpr = TNew(cl, tparams, List.map run eparams) })
- | TArray(arr, idx) ->
- let arr_etype = match follow arr.etype with
- | (TInst _ as t) -> t
- | TAbstract (a, pl) when not (Meta.has Meta.CoreType a.a_meta) ->
- follow (Abstract.get_underlying_type a pl)
- | t -> t in
- let idx = match gen.greal_type idx.etype with
- | TAbstract({ a_path = [],"Int" },_) -> run idx
- | _ -> match handle (run idx) gen.gcon.basic.tint (gen.greal_type idx.etype) with
- | ({ eexpr = TCast _ } as idx) -> idx
- | idx -> mk_cast gen.gcon.basic.tint idx
- in
- let e = { e with eexpr = TArray(run arr, idx) } in
- (* get underlying class (if it's a class *)
- (match arr_etype with
- | TInst(cl, params) ->
- (* see if it implements ArrayAccess *)
- (match cl.cl_array_access with
- | None -> e
- | Some t ->
- (* if it does, apply current parameters (and change them) *)
- (* let real_t = apply_params_internal (List.map (gen.greal_type_param (TClassDecl cl))) cl params t in *)
- let param = apply_params cl.cl_params (gen.greal_type_param (TClassDecl cl) params) t in
- let real_t = apply_params cl.cl_params params param in
- (* see if it needs a cast *)
- handle (e) (gen.greal_type e.etype) (gen.greal_type real_t)
- )
- | _ -> Type.map_expr run e)
- | TVar (v, eopt) ->
- { e with eexpr = TVar (v, match eopt with
- | None -> eopt
- | Some e -> Some( handle (run e) v.v_type e.etype ))
- }
- (* FIXME deal with in_value when using other statements that may not have a TBlock wrapped on them *)
- | TIf (econd, ethen, Some(eelse)) when was_in_value ->
- { e with eexpr = TIf (handle (run econd) gen.gcon.basic.tbool econd.etype, handle (run ethen) e.etype ethen.etype, Some( handle (run eelse) e.etype eelse.etype ) ) }
- | TIf (econd, ethen, eelse) ->
- { e with eexpr = TIf (handle (run econd) gen.gcon.basic.tbool econd.etype, (in_value := false; run (mk_block ethen)), Option.map (fun e -> in_value := false; run (mk_block e)) eelse) }
- | TWhile (econd, e1, flag) ->
- { e with eexpr = TWhile (handle (run econd) gen.gcon.basic.tbool econd.etype, (in_value := false; run (mk_block e1)), flag) }
- | TSwitch (cond, el_e_l, edef) ->
- { e with eexpr = TSwitch(run cond, List.map (fun (el,e) -> (List.map run el, (in_value := false; run (mk_block e)))) el_e_l, Option.map (fun e -> in_value := false; run (mk_block e)) edef) }
- (* | TMatch (cond, en, il_vl_e_l, edef) ->
- { e with eexpr = TMatch(run cond, en, List.map (fun (il, vl, e) -> (il, vl, run (mk_block e))) il_vl_e_l, Option.map (fun e -> run (mk_block e)) edef) } *)
- | TFor (v,cond,e1) ->
- { e with eexpr = TFor(v, run cond, (in_value := false; run (mk_block e1))) }
- | TTry (e, ve_l) ->
- { e with eexpr = TTry((in_value := false; run (mk_block e)), List.map (fun (v,e) -> in_value := false; (v, run (mk_block e))) ve_l) }
- | TBlock el ->
- let i = ref 0 in
- let len = List.length el in
- { e with eexpr = TBlock ( List.map (fun e ->
- incr i;
- if !i <> len || not was_in_value then
- in_value := false;
- run e
- ) el ) }
- | TCast (expr, md) when is_void (follow e.etype) ->
- run expr
- | TCast (expr, md) ->
- let rec get_null e =
- match e.eexpr with
- | TConst TNull -> Some e
- | TParenthesis e | TMeta(_,e) -> get_null e
- | _ -> None
- in
- (match get_null expr with
- | Some enull ->
- if gen.gcon.platform = Cs then
- { enull with etype = gen.greal_type e.etype }
- else
- mk_cast (gen.greal_type e.etype) enull
- | _ ->
- let last_unsafe = gen.gon_unsafe_cast in
- gen.gon_unsafe_cast <- (fun t t2 pos -> ());
- let ret = handle (run expr) e.etype expr.etype in
- gen.gon_unsafe_cast <- last_unsafe;
- match ret.eexpr with
- | TCast _ -> ret
- | _ -> { e with eexpr = TCast(ret,md); etype = gen.greal_type e.etype }
- )
- (*| TCast _ ->
- (* if there is already a cast, we should skip this cast check *)
- Type.map_expr run e*)
- | TFunction f ->
- in_value := false;
- Type.map_expr run e
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- gen.ghandle_cast <- (fun tto tfrom expr -> handle_cast gen expr (gen.greal_type tto) (gen.greal_type tfrom));
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map;
- ReturnCast.configure gen
- end;;
- (* ******************************************* *)
- (* Reflection-enabling Class fields *)
- (* ******************************************* *)
- (*
- This is the most hardcore codegen part of the code. There's much to improve so this code can be more readable, but at least it's running correctly right now! This will be improved. (TODO)
- This module will create class fields that enable reflection for targets that have a slow or inexistent reflection abilities. Because of the similarity
- of strategies between what should have been different modules, they are all unified in this reflection-enabling class fields.
- They include:
- * Get(isStatic, throwErrors, isCheck) / Set fields . Remember to allow implements Dynamic also.
- * Invoke fields(isStatic) -> You need to configure how many invoke_field fields there will be. + invokeDynamic
- * Has field -> parameter in get field that returns __undefined__ if it doesn't exist.
- * GetType -> return the current Class<> / Enum<>
- * Fields(isStatic) -> returns all the fields / static fields. Remember to allow implements Dynamic also
- * Create(arguments array), CreateEmpty - calls new() or create empty
- * getInstanceFields / getClassFields -> show even function fields, everything!
- * deleteField -> only for implements Dynamic
- for enums:
- * createEnum -> invokeField for classes
- * createEnumIndex -> use invokeField as well, and use numbers e.g. "0", "1", "2" .... For this, use "@:alias" metadata
- * getEnumConstructs -> fields()
- need to be solved outside:
- * getEnumName
- * enumIndex
- *
- need to be solved by haxe code:
- * enumParameters -> for (field in Reflect.fields(enum)) arr.push(Reflect.field(enum, field))
- Standard:
- if a class contains a @:$enum metadata, it's treated as a converted enum to class
- Optimizations:
- * if optimize is true, all fields will be hashed by the same hashing function as neko (31 bits int : always positive). Every function that expects a string for the field will expect also an int, for the hash
- a string (which is nullable for compile-time hashes) + an int.
- At compile-time, a collision will throw an error (like neko).
- At runtime, a collision will make a negative int. Negative ints will always resolve to a special Hash<> field which takes a string.
- * if optimize is true, Reflect.field/setField will be replaced by either the runtime version (with already hashed string), either by the own .Field()/.SetField() HxObject's version,
- if the type is detected to already be hxgen
- * TODO: if for() optimization for arrays is disabled, we can replace for(field in Reflect.fields(obj)) to:
- for (field in ( (Std.is(obj, HxObject) ? ((HxObject)obj).Fields() : Reflect.fields(obj)) )) // no array copying . for further optimization this could be guaranteed to return
- the already hashed fields.
- Mappings:
- * if create Dynamic class is true, TObjectDecl will be mapped to new DynamicClass(fields, [hashedFields], values)
- *
- dependencies:
- There is no big dependency from this target. Though it should be a syntax filter, mainly one of the first so most expression generation has already been done,
- while the AST has its meaning close to haxe's.
- Should run before InitFunction so it detects variables containing expressions as "always-execute" expressions, even when using CreateEmpty
- * Must run before switch() syntax changes
- *)
- open ClosuresToClass;;
- module ReflectionCFs =
- struct
- let name = "reflection_cfs"
- type rcf_ctx =
- {
- rcf_gen : generator_ctx;
- rcf_ft : ClosuresToClass.closures_ctx;
- rcf_optimize : bool;
- mutable rcf_float_special_case : bool;
- mutable rcf_object_iface : tclass;
- mutable rcf_create_getsetinvoke_fields : bool;
- (* should we create the get type (get Class)? *)
- mutable rcf_create_get_type : bool;
- (* should we handle implements dynamic? *)
- mutable rcf_handle_impl_dynamic : bool;
- (*
- create_dyn_overloading_ctor :
- when creating the implements dynamic code, we can also create a special constructor for
- the actual DynamicObject class, which will receive all its <implements Dynamic> fields from the code outside.
- Note that this will only work on targets that support overloading contrstuctors, as any class that extends
- our DynamicObject will have an empty super() call
- *)
- mutable rcf_create_dyn_ctor : bool;
- mutable rcf_max_func_arity : int;
- (*
- the hash lookup function. can be an inlined expr or simply a function call.
- its only needed features is that it should return the index of the key if found, and the
- complement of the index of where it should be inserted if not found (Ints).
- hash->hash_array->length->returning expression
- *)
- mutable rcf_hash_function : texpr->texpr->texpr->texpr;
- mutable rcf_lookup_function : texpr->texpr;
- (* hash_array->length->pos->value *)
- mutable rcf_insert_function : texpr->texpr->texpr->texpr->texpr;
- (* hash_array->length->pos->value *)
- mutable rcf_remove_function : texpr->texpr->texpr->texpr;
- (*
- class_cl is the real class for Class<> instances.
- In the current implementation, due to some targets' limitations, (in particular, Java),
- we have to use an empty object so we can access its virtual mehtods.
- FIXME find a better way to create Class<> objects in a performant way
- *)
- mutable rcf_class_cl : tclass option;
- (*
- Also about the Class<> type, should we crate all classes eagerly?
- If false, it means that we should have a way at runtime to create the class when needed by
- Type.resolveClass/Enum
- *)
- mutable rcf_class_eager_creation : bool;
- rcf_hash_fields : (int, string) Hashtbl.t;
- (*
- main expr -> field expr -> field string -> possible hash int (if optimize) -> possible set expr -> should_throw_exceptions -> changed expression
- Changes a get / set field to the runtime resolution function
- *)
- mutable rcf_on_getset_field : texpr->texpr->string->int32 option->texpr option->bool->texpr;
- mutable rcf_on_call_field : texpr->texpr->string->int32 option->texpr list->texpr;
- mutable rcf_handle_statics : bool;
- }
- let new_ctx gen ft object_iface optimize dynamic_getset_field dynamic_call_field hash_function lookup_function insert_function remove_function handle_statics =
- {
- rcf_gen = gen;
- rcf_ft = ft;
- rcf_optimize = optimize;
- rcf_float_special_case = true;
- rcf_object_iface = object_iface;
- rcf_create_getsetinvoke_fields = true;
- rcf_create_get_type = true;
- rcf_handle_impl_dynamic = true;
- rcf_create_dyn_ctor = true;
- rcf_max_func_arity = 10;
- rcf_hash_function = hash_function;
- rcf_lookup_function = lookup_function;
- rcf_insert_function = insert_function;
- rcf_remove_function = remove_function;
- rcf_class_cl = None;
- rcf_class_eager_creation = false;
- rcf_hash_fields = Hashtbl.create 100;
- rcf_on_getset_field = dynamic_getset_field;
- rcf_on_call_field = dynamic_call_field;
- rcf_handle_statics = handle_statics;
- }
- (*
- methods as a bool option is a little laziness of my part.
- None means that methods are included with normal fields;
- Some(true) means collect only methods
- Some(false) means collect only fields (and MethDynamic fields)
- *)
- let collect_fields cl (methods : bool option) (statics : bool option) =
- let collected = Hashtbl.create 0 in
- let collect cf acc =
- if Meta.has Meta.CompilerGenerated cf.cf_meta || Meta.has Meta.SkipReflection cf.cf_meta then
- acc
- else match methods, cf.cf_kind with
- | None, _ when not (Hashtbl.mem collected cf.cf_name) -> Hashtbl.add collected cf.cf_name true; ([cf.cf_name], cf) :: acc
- | Some true, Method MethDynamic -> acc
- | Some true, Method _ when not (Hashtbl.mem collected cf.cf_name) -> Hashtbl.add collected cf.cf_name true; ([cf.cf_name], cf) :: acc
- | Some false, Method MethDynamic
- | Some false, Var _ when not (Hashtbl.mem collected cf.cf_name) -> Hashtbl.add collected cf.cf_name true; ([cf.cf_name], cf) :: acc
- | _ -> acc
- in
- let collect_cfs cfs acc =
- let rec loop cfs acc =
- match cfs with
- | [] -> acc
- | hd :: tl -> loop tl (collect hd acc)
- in
- loop cfs acc
- in
- let rec loop cl acc =
- let acc = match statics with
- | None -> collect_cfs cl.cl_ordered_fields (collect_cfs cl.cl_ordered_statics acc)
- | Some true -> collect_cfs cl.cl_ordered_statics acc
- | Some false -> collect_cfs cl.cl_ordered_fields acc
- in
- match cl.cl_super with
- | None -> acc
- | Some(cl,_) ->
- if not (is_hxgen (TClassDecl cl)) then loop cl acc else acc
- in
- loop cl []
- let hash f =
- let h = ref 0 in
- for i = 0 to String.length f - 1 do
- h := !h * 223 + int_of_char (String.unsafe_get f i);
- done;
- if Sys.word_size = 64 then Int32.to_int (Int32.shift_right (Int32.shift_left (Int32.of_int !h) 1) 1) else !h
- let hash_field ctx f pos =
- let h = hash f in
- (try
- let f2 = Hashtbl.find ctx.rcf_hash_fields h in
- if f <> f2 then ctx.rcf_gen.gcon.error ("Field conflict between " ^ f ^ " and " ^ f2) pos
- with Not_found ->
- Hashtbl.add ctx.rcf_hash_fields h f);
- h
- (* ( tf_args, switch_var ) *)
- let field_type_args ctx pos =
- match ctx.rcf_optimize with
- | true ->
- let field_name, field_hash = alloc_var "field" ctx.rcf_gen.gcon.basic.tstring, alloc_var "hash" ctx.rcf_gen.gcon.basic.tint in
- [field_name, None; field_hash, None], field_hash
- | false ->
- let field_name = alloc_var "field" ctx.rcf_gen.gcon.basic.tstring in
- [field_name, None], field_name
- let hash_field_i32 ctx pos field_name =
- let i = hash_field ctx field_name pos in
- let i = Int32.of_int (i) in
- if i < Int32.zero then
- Int32.logor (Int32.logand i (Int32.of_int 0x3FFFFFFF)) (Int32.shift_left Int32.one 30)
- else i
- let switch_case ctx pos field_name =
- match ctx.rcf_optimize with
- | true ->
- let i = hash_field_i32 ctx pos field_name in
- { eexpr = TConst(TInt(i)); etype = ctx.rcf_gen.gcon.basic.tint; epos = pos }
- | false ->
- { eexpr = TConst(TString(field_name)); etype = ctx.rcf_gen.gcon.basic.tstring; epos = pos }
- (*
- Will implement getField / setField which will follow the following rule:
- function getField(field, isStatic, throwErrors, isCheck, handleProperty, isFirst):Dynamic
- {
- if (isStatic)
- {
- switch(field)
- {
- case "aStaticField": return ThisClass.aStaticField;
- case "aDynamicField": return ThisClass.aDynamicField;
- default:
- if (isFirst) return getField_d(field, isStatic, throwErrors, handleProperty, false);
- if(throwErrors) throw "Field not found"; else if (isCheck) return __undefined__ else return null;
- }
- } else {
- switch(field)
- {
- case "aNormalField": return this.aNormalField;
- case "aBoolField": return this.aBoolField;
- case "aDoubleField": return this.aDoubleField;
- default: return getField_d(field, isStatic, throwErrors, isCheck);
- }
- }
- }
- function getField_d(field, isStatic, throwErrors, handleProperty, isFirst):Float
- {
- if (isStatic)
- {
- switch(field)
- {
- case "aDynamicField": return cast ThisClass.aDynamicField;
- default: if (throwErrors) throw "Field not found"; else return null;
- }
- }
- etc...
- }
- function setField(field, value, isStatic):Dynamic {}
- function setField_d(field, value:Float, isStatic):Float {}
- *)
- let call_super ctx fn_args ret_t cf cl this_t pos =
- {
- eexpr = TCall({
- eexpr = TField({ eexpr = TConst(TSuper); etype = this_t; epos = pos }, FInstance(cl,List.map snd cl.cl_params,cf));
- etype = TFun(fun_args fn_args, ret_t);
- epos = pos;
- }, List.map (fun (v,_) -> mk_local v pos) fn_args);
- etype = ret_t;
- epos = pos;
- }
- let mk_string ctx str pos =
- { eexpr = TConst(TString(str)); etype = ctx.rcf_gen.gcon.basic.tstring; epos = pos }
- let mk_int ctx i pos =
- { eexpr = TConst(TInt(Int32.of_int i)); etype = ctx.rcf_gen.gcon.basic.tint; epos = pos }
- let mk_bool ctx b pos =
- { eexpr = TConst(TBool(b)); etype = ctx.rcf_gen.gcon.basic.tbool; epos = pos }
- let mk_throw ctx str pos = { eexpr = TThrow (mk_string ctx str pos); etype = ctx.rcf_gen.gcon.basic.tvoid; epos = pos }
- let enumerate_dynamic_fields ctx cl when_found =
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let pos = cl.cl_pos in
- let vtmp = alloc_var "i" basic.tint in
- let mk_for arr len =
- let t = if ctx.rcf_optimize then basic.tint else basic.tstring in
- let convert_str e = if ctx.rcf_optimize then ctx.rcf_lookup_function e else e in
- let tmpinc = { eexpr = TUnop(Ast.Increment, Ast.Postfix, mk_local vtmp pos); etype = basic.tint; epos = pos } in
- {
- eexpr = TBlock [
- { eexpr = TBinop(OpAssign, mk_local vtmp pos, mk_int ctx 0 pos); etype = basic.tint; epos = pos };
- {
- eexpr = TWhile (
- { eexpr = TBinop(Ast.OpLt, mk_local vtmp pos, len); etype = basic.tbool; epos = pos },
- mk_block (when_found (convert_str { eexpr = TArray (arr, tmpinc); etype = t; epos = pos })),
- Ast.NormalWhile
- );
- etype = basic.tvoid;
- epos = pos
- }
- ];
- etype = basic.tvoid;
- epos = pos;
- }
- in
- let this_t = TInst(cl, List.map snd cl.cl_params) in
- let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
- let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
- { eexpr = TVar (vtmp,None); etype = basic.tvoid; epos = pos }
- ::
- if ctx.rcf_optimize then
- [
- mk_for (mk_this (gen.gmk_internal_name "hx" "hashes") (gen.gclasses.nativearray basic.tint)) (mk_this (gen.gmk_internal_name "hx" "length") basic.tint);
- mk_for (mk_this (gen.gmk_internal_name "hx" "hashes_f") (gen.gclasses.nativearray basic.tint)) (mk_this (gen.gmk_internal_name "hx" "length_f") basic.tint);
- ] else [
- mk_for (mk_this (gen.gmk_internal_name "hx" "hashes") (gen.gclasses.nativearray basic.tstring)) (mk_this (gen.gmk_internal_name "hx" "length") basic.tint);
- mk_for (mk_this (gen.gmk_internal_name "hx" "hashes_f") (gen.gclasses.nativearray basic.tstring)) (mk_this (gen.gmk_internal_name "hx" "length_f") basic.tint);
- ]
- (* *********************
- Dynamic lookup
- *********************
- This is the behavior of standard <implements Dynamic> classes. It will replace the error throwing
- if a field doesn't exists when looking it up.
- In order for it to work, an implementation for hash_function must be created.
- hash_function is the function to be called/inlined that will allow us to lookup the hash into a sorted array of hashes.
- A binary search or linear search algorithm may be implemented. The only need is that if not found, the NegBits of
- the place where it should be inserted must be returned.
- *)
- let abstract_dyn_lookup_implementation ctx this hash_local may_value is_float pos =
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
- let a_t = if ctx.rcf_optimize then basic.tint else basic.tstring in
- let hx_hashes = mk_this (gen.gmk_internal_name "hx" "hashes") (gen.gclasses.nativearray a_t) in
- let hx_hashes_f = mk_this (gen.gmk_internal_name "hx" "hashes_f") (gen.gclasses.nativearray a_t) in
- let hx_dynamics = mk_this (gen.gmk_internal_name "hx" "dynamics") (gen.gclasses.nativearray t_empty) in
- let hx_dynamics_f = mk_this (gen.gmk_internal_name "hx" "dynamics_f") (gen.gclasses.nativearray basic.tfloat) in
- let hx_length = mk_this (gen.gmk_internal_name "hx" "length") (basic.tint) in
- let hx_length_f = mk_this (gen.gmk_internal_name "hx" "length_f") (basic.tint) in
- let res = alloc_var "res" basic.tint in
- let fst_hash, snd_hash, fst_dynamics, snd_dynamics, fst_length, snd_length =
- if is_float then
- hx_hashes_f, hx_hashes, hx_dynamics_f, hx_dynamics, hx_length_f, hx_length
- else
- hx_hashes, hx_hashes_f, hx_dynamics, hx_dynamics_f, hx_length, hx_length_f
- in
- let res_local = mk_local res pos in
- let gte = {
- eexpr = TBinop(Ast.OpGte, res_local, { eexpr = TConst(TInt(Int32.zero)); etype = basic.tint; epos = pos });
- etype = basic.tbool;
- epos = pos;
- } in
- let mk_tarray arr idx =
- {
- eexpr = TArray(arr, idx);
- etype = gen.gclasses.nativearray_type arr.etype;
- epos = pos;
- }
- in
- let ret_t = if is_float then basic.tfloat else t_dynamic in
- match may_value with
- | None ->
- (*
- var res = lookup(this.__hx_hashes/f, hash);
- if (res < 0)
- {
- res = lookup(this.__hx_hashes_f/_, hash);
- if(res < 0)
- return null;
- else
- return __hx_dynamics_f[res];
- } else {
- return __hx_dynamics[res];
- }
- *)
- let block =
- [
- { eexpr = TVar(res, Some(ctx.rcf_hash_function hash_local fst_hash fst_length)); etype = basic.tvoid; epos = pos };
- { eexpr = TIf(gte, mk_return (mk_tarray fst_dynamics res_local), Some({
- eexpr = TBlock(
- [
- { eexpr = TBinop(Ast.OpAssign, res_local, ctx.rcf_hash_function hash_local snd_hash snd_length); etype = basic.tint; epos = pos };
- { eexpr = TIf(gte, mk_return (mk_tarray snd_dynamics res_local), None); etype = ret_t; epos = pos }
- ]);
- etype = ret_t;
- epos = pos;
- })); etype = ret_t; epos = pos }
- ] in
- block
- | Some value_local ->
- (*
- //if is not float:
- //if (isNumber(value_local)) return this.__hx_setField_f(field, getNumber(value_local), false(not static));
- var res = lookup(this.__hx_hashes/f, hash);
- if (res >= 0)
- {
- return __hx_dynamics/f[res] = value_local;
- } else {
- res = lookup(this.__hx_hashes_f/_, hash);
- if (res >= 0)
- {
- __hx_dynamics_f/_.splice(res,1);
- __hx_hashes_f/_.splice(res,1);
- }
- }
- __hx_hashses/_f.insert(~res, hash);
- __hx_dynamics/_f.insert(~res, value_local);
- return value_local;
- *)
- let neg_res = { eexpr = TUnop(Ast.NegBits, Ast.Prefix, res_local); etype = basic.tint; epos = pos } in
- let res2 = alloc_var "res2" basic.tint in
- let res2_local = mk_local res2 pos in
- let gte2 = {
- eexpr = TBinop(Ast.OpGte, res2_local, { eexpr = TConst(TInt(Int32.zero)); etype = basic.tint; epos = pos });
- etype = basic.tbool;
- epos = pos;
- } in
- let block =
- [
- { eexpr = TVar(res, Some(ctx.rcf_hash_function hash_local fst_hash fst_length)); etype = basic.tvoid; epos = pos };
- {
- eexpr = TIf(gte,
- mk_return { eexpr = TBinop(Ast.OpAssign, mk_tarray fst_dynamics res_local, value_local); etype = value_local.etype; epos = pos },
- Some({ eexpr = TBlock([
- { eexpr = TVar( res2, Some(ctx.rcf_hash_function hash_local snd_hash snd_length)); etype = basic.tvoid; epos = pos };
- {
- eexpr = TIf(gte2, { eexpr = TBlock([
- ctx.rcf_remove_function snd_hash snd_length res2_local;
- ctx.rcf_remove_function snd_dynamics snd_length res2_local;
- mk (TUnop(Decrement,Postfix,snd_length)) basic.tint pos
- ]); etype = t_dynamic; epos = pos }, None);
- etype = t_dynamic;
- epos = pos;
- }
- ]); etype = t_dynamic; epos = pos }));
- etype = t_dynamic;
- epos = pos;
- };
- ctx.rcf_insert_function fst_hash fst_length neg_res hash_local;
- ctx.rcf_insert_function fst_dynamics fst_length neg_res value_local;
- mk (TUnop(Increment,Postfix,fst_length)) basic.tint pos;
- mk_return value_local
- ] in
- block
- let get_delete_field ctx cl is_dynamic =
- let pos = cl.cl_pos in
- let this_t = TInst(cl, List.map snd cl.cl_params) in
- let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let tf_args, switch_var = field_type_args ctx pos in
- let local_switch_var = mk_local switch_var pos in
- let fun_type = TFun(fun_args tf_args,basic.tbool) in
- let cf = mk_class_field (gen.gmk_internal_name "hx" "deleteField") fun_type false pos (Method MethNormal) [] in
- let body = if is_dynamic then begin
- let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
- let a_t = if ctx.rcf_optimize then basic.tint else basic.tstring in
- let hx_hashes = mk_this (gen.gmk_internal_name "hx" "hashes") (gen.gclasses.nativearray a_t) in
- let hx_hashes_f = mk_this (gen.gmk_internal_name "hx" "hashes_f") (gen.gclasses.nativearray a_t) in
- let hx_dynamics = mk_this (gen.gmk_internal_name "hx" "dynamics") (gen.gclasses.nativearray t_empty) in
- let hx_dynamics_f = mk_this (gen.gmk_internal_name "hx" "dynamics_f") (gen.gclasses.nativearray basic.tfloat) in
- let hx_length = mk_this (gen.gmk_internal_name "hx" "length") (basic.tint) in
- let hx_length_f = mk_this (gen.gmk_internal_name "hx" "length_f") (basic.tint) in
- let res = alloc_var "res" basic.tint in
- let res_local = mk_local res pos in
- let gte = {
- eexpr = TBinop(Ast.OpGte, res_local, { eexpr = TConst(TInt(Int32.zero)); etype = basic.tint; epos = pos });
- etype = basic.tbool;
- epos = pos;
- } in
- (*
- var res = lookup(this.__hx_hashes, hash);
- if (res >= 0)
- {
- __hx_dynamics.splice(res,1);
- __hx_hashes.splice(res,1);
- return true;
- } else {
- res = lookup(this.__hx_hashes_f, hash);
- if (res >= 0)
- {
- __hx_dynamics_f.splice(res,1);
- __hx_hashes_f.splice(res,1);
- return true;
- }
- }
- return false;
- *)
- [
- { eexpr = TVar(res,Some(ctx.rcf_hash_function local_switch_var hx_hashes hx_length)); etype = basic.tvoid; epos = pos };
- {
- eexpr = TIf(gte, { eexpr = TBlock([
- ctx.rcf_remove_function hx_hashes hx_length res_local;
- ctx.rcf_remove_function hx_dynamics hx_length res_local;
- mk (TUnop(Decrement,Postfix,hx_length)) basic.tint pos;
- mk_return { eexpr = TConst(TBool true); etype = basic.tbool; epos = pos }
- ]); etype = t_dynamic; epos = pos }, Some({ eexpr = TBlock([
- { eexpr = TBinop(Ast.OpAssign, res_local, ctx.rcf_hash_function local_switch_var hx_hashes_f hx_length_f); etype = basic.tint; epos = pos };
- { eexpr = TIf(gte, { eexpr = TBlock([
- ctx.rcf_remove_function hx_hashes_f hx_length_f res_local;
- ctx.rcf_remove_function hx_dynamics_f hx_length_f res_local;
- mk (TUnop(Decrement,Postfix,hx_length_f)) basic.tint pos;
- mk_return { eexpr = TConst(TBool true); etype = basic.tbool; epos = pos }
- ]); etype = t_dynamic; epos = pos }, None); etype = t_dynamic; epos = pos }
- ]); etype = t_dynamic; epos = pos }));
- etype = t_dynamic;
- epos = pos;
- };
- mk_return { eexpr = TConst(TBool false); etype = basic.tbool; epos = pos }
- ]
- end else
- [
- mk_return { eexpr = TConst(TBool false); etype = basic.tbool; epos = pos }
- ] in
- (* create function *)
- let fn =
- {
- tf_args = tf_args;
- tf_type = basic.tbool;
- tf_expr = { eexpr = TBlock(body); etype = t_dynamic; epos = pos }
- } in
- cf.cf_expr <- Some({ eexpr = TFunction(fn); etype = fun_type; epos = pos });
- cf
- let rec is_first_dynamic cl =
- match cl.cl_super with
- | Some(cl,_) ->
- if is_some cl.cl_dynamic then false else is_first_dynamic cl
- | None -> true
- let is_override cl = match cl.cl_super with
- | Some (cl, _) when is_hxgen (TClassDecl cl) -> true
- | _ -> false
- let get_args t = match follow t with
- | TFun(args,ret) -> args,ret
- | _ -> assert false
- (* WARNING: this will only work if overloading contructors is possible on target language *)
- let implement_dynamic_object_ctor ctx cl =
- let rec is_side_effects_free e =
- match e.eexpr with
- | TConst _
- | TLocal _
- | TFunction _
- | TTypeExpr _ ->
- true
- | TNew(clnew,[],params) when clnew == cl ->
- List.for_all is_side_effects_free params
- | TUnop(Increment,_,_)
- | TUnop(Decrement,_,_)
- | TBinop(OpAssign,_,_)
- | TBinop(OpAssignOp _,_,_) ->
- false
- | TUnop(_,_,e) ->
- is_side_effects_free e
- | TArray(e1,e2)
- | TBinop(_,e1,e2) ->
- is_side_effects_free e1 && is_side_effects_free e2
- | TIf(cond,e1,Some e2) ->
- is_side_effects_free cond && is_side_effects_free e1 && is_side_effects_free e2
- | TField(e,_)
- | TParenthesis e | TMeta(_,e) -> is_side_effects_free e
- | TArrayDecl el -> List.for_all is_side_effects_free el
- | TCast(e,_) -> is_side_effects_free e
- | _ -> false
- in
- let pos = cl.cl_pos in
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let hasht = if ctx.rcf_optimize then basic.tint else basic.tstring in
- let hashes_field = gen.gmk_internal_name "hx" "hashes", gen.gclasses.nativearray hasht in
- let hashes_f_field = gen.gmk_internal_name "hx" "hashes_f", gen.gclasses.nativearray hasht in
- let dynamics_field = gen.gmk_internal_name "hx" "dynamics", gen.gclasses.nativearray t_empty in
- let dynamics_f_field = gen.gmk_internal_name "hx" "dynamics_f", gen.gclasses.nativearray basic.tfloat in
- let fields =
- [
- hashes_field;
- dynamics_field;
- hashes_f_field;
- dynamics_f_field;
- ] in
- let hashes_var = alloc_var (fst hashes_field) (snd hashes_field) in
- let hashes_f_var = alloc_var (fst hashes_f_field) (snd hashes_f_field) in
- let tf_args = [
- hashes_var, None;
- alloc_var (fst dynamics_field) (snd dynamics_field), None;
- hashes_f_var, None;
- alloc_var (fst dynamics_f_field) (snd dynamics_f_field), None;
- ] in
- let this = { eexpr = TConst TThis; etype = TInst(cl, List.map snd cl.cl_params); epos = pos } in
- let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
- let fun_t = TFun(fun_args tf_args,basic.tvoid) in
- let ctor = mk_class_field "new" fun_t true pos (Method MethNormal) [] in
- ctor.cf_expr <- Some(
- {
- eexpr = TFunction({
- tf_args = tf_args;
- tf_type = basic.tvoid;
- tf_expr =
- {
- eexpr = TBlock(
- List.map (fun (v,_) ->
- { eexpr = TBinop(Ast.OpAssign, mk_this v.v_name v.v_type, mk_local v pos); etype = v.v_type; epos = pos }
- ) tf_args
- @
- [
- mk (TBinop(OpAssign, mk_this (gen.gmk_internal_name "hx" "length") basic.tint, gen.gclasses.nativearray_len (mk_local hashes_var pos) pos)) basic.tint pos;
- mk (TBinop(OpAssign, mk_this (gen.gmk_internal_name "hx" "length_f") basic.tint, gen.gclasses.nativearray_len (mk_local hashes_f_var pos) pos)) basic.tint pos;
- ]
- );
- etype = basic.tvoid;
- epos = pos
- }
- });
- etype = fun_t;
- epos = pos
- });
- add_constructor cl ctor;
- (* default ctor also *)
- let ctor = mk_class_field "new" (TFun([],basic.tvoid)) false pos (Method MethNormal) [] in
- ctor.cf_expr <- Some {
- eexpr = TFunction {
- tf_type = basic.tvoid;
- tf_args = [];
- tf_expr = {
- eexpr = TBlock(List.map (fun (f,t) ->
- { eexpr = TBinop(Ast.OpAssign, mk_this f t,{ eexpr = TCall(mk_local v_nativearray pos, []); etype = t; epos = pos; }); etype = t; epos = pos }
- ) fields);
- etype = basic.tvoid;
- epos = pos;
- }
- };
- etype = ctor.cf_type;
- epos = pos;
- };
- add_constructor cl ctor;
- (* and finally we will return a function that transforms a TObjectDecl into a new DynamicObject() call *)
- let rec loop objdecl acc acc_f =
- match objdecl with
- | [] -> acc,acc_f
- | (name,expr) :: tl ->
- let real_t = gen.greal_type expr.etype in
- match follow expr.etype with
- | TInst ( { cl_path = ["haxe"], "Int64" }, [] ) ->
- loop tl ((name, gen.ghandle_cast t_dynamic real_t expr) :: acc) acc_f
- | _ ->
- if like_float real_t && not (like_i64 real_t) then
- loop tl acc ((name, gen.ghandle_cast basic.tfloat real_t expr) :: acc_f)
- else
- loop tl ((name, gen.ghandle_cast t_dynamic real_t expr) :: acc) acc_f
- in
- let may_hash_field s =
- if ctx.rcf_optimize then begin
- (* let hash_field ctx f pos = *)
- { eexpr = TConst(TInt (hash_field_i32 ctx pos s)); etype = basic.tint; epos = pos }
- end else begin
- { eexpr = TConst(TString s); etype = basic.tstring; epos = pos }
- end
- in
- let do_objdecl e objdecl =
- let exprs_before = ref [] in
- let rec change_exprs decl acc = match decl with
- | (name,expr) :: tl ->
- if is_side_effects_free expr then
- change_exprs tl ((name,expr) :: acc)
- else begin
- let var = mk_temp gen "odecl" expr.etype in
- exprs_before := { eexpr = TVar(var,Some expr); etype = basic.tvoid; epos = expr.epos } :: !exprs_before;
- change_exprs tl ((name,mk_local var expr.epos) :: acc)
- end
- | [] -> acc
- in
- let objdecl = change_exprs objdecl [] in
- let odecl, odecl_f = loop objdecl [] [] in
- let changed_expr = List.map (fun (s,e) -> (may_hash_field s,e)) in
- let odecl, odecl_f = changed_expr odecl, changed_expr odecl_f in
- let sort_fn (e1,_) (e2,_) =
- match e1.eexpr, e2.eexpr with
- | TConst(TInt i1), TConst(TInt i2) -> compare i1 i2
- | TConst(TString s1), TConst(TString s2) -> compare s1 s2
- | _ -> assert false
- in
- let odecl, odecl_f = List.sort sort_fn odecl, List.sort sort_fn odecl_f in
- let ret = {
- e with eexpr = TNew(cl,[],
- [
- mk_nativearray_decl gen hasht (List.map fst odecl) pos;
- mk_nativearray_decl gen t_empty (List.map snd odecl) pos;
- mk_nativearray_decl gen hasht (List.map fst odecl_f) pos;
- mk_nativearray_decl gen basic.tfloat (List.map snd odecl_f) pos;
- ]);
- } in
- match !exprs_before with
- | [] -> ret
- | block ->
- {
- eexpr = TBlock(List.rev block @ [ret]);
- etype = ret.etype;
- epos = ret.epos;
- }
- in
- do_objdecl
- let implement_dynamics ctx cl =
- let pos = cl.cl_pos in
- let is_override = is_override cl in
- if is_some cl.cl_dynamic then begin
- if is_first_dynamic cl then begin
- (*
- * add hx_hashes, hx_hashes_f, hx_dynamics, hx_dynamics_f to class
- * implement hx_deleteField
- *)
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let hasht = if ctx.rcf_optimize then basic.tint else basic.tstring in
- let new_fields =
- [
- mk_class_field (gen.gmk_internal_name "hx" "hashes") (gen.gclasses.nativearray hasht) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
- mk_class_field (gen.gmk_internal_name "hx" "dynamics") (gen.gclasses.nativearray t_empty) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
- mk_class_field (gen.gmk_internal_name "hx" "hashes_f") (gen.gclasses.nativearray hasht) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
- mk_class_field (gen.gmk_internal_name "hx" "dynamics_f") (gen.gclasses.nativearray basic.tfloat) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
- ] in
- (if cl.cl_path <> (["haxe"; "lang"], "DynamicObject") then
- List.iter (fun cf -> cf.cf_expr <- Some { eexpr = TCall(mk_local v_nativearray pos, []); etype = cf.cf_type; epos = cf.cf_pos }) new_fields
- );
- let delete = get_delete_field ctx cl true in
- let new_fields = new_fields @ [
- mk_class_field (gen.gmk_internal_name "hx" "length") (basic.tint) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
- mk_class_field (gen.gmk_internal_name "hx" "length_f") (basic.tint) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
- delete;
- ] in
- List.iter (fun cf ->
- cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields
- ) new_fields;
- (*
- let rec last_ctor cl =
- match cl.cl_constructor with
- | None -> (match cl.cl_super with | None -> None | Some (cl,_) -> last_ctor cl)
- | Some c -> Some c
- in
- *)
- (*
- in order for the next to work, we need to execute our script before InitFunction, so the expressions inside the variables are initialized by the constructor
- *)
- (*
- Now we need to add their initialization.
- This will consist of different parts:
- Check if there are constructors. If not, create one and add initialization to it (calling super, ok)
- If there are, add as first statement (or second if there is a super() call in the first)
- If class has @:dynamicObject meta, also create another new() class with its parameters as constructor arguments
- *)
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ new_fields;
- if is_override then cl.cl_overrides <- delete :: cl.cl_overrides
- end
- end else if not is_override then begin
- let delete = get_delete_field ctx cl false in
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ [delete];
- cl.cl_fields <- PMap.add delete.cf_name delete cl.cl_fields
- end
- let implement_create_empty ctx cl =
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let pos = cl.cl_pos in
- let is_override = is_override cl in
- let tparams = List.map (fun _ -> t_empty) cl.cl_params in
- let create =
- let arr = alloc_var "arr" (basic.tarray t_dynamic) in
- let tf_args = [ arr, None ] in
- let t = TFun(fun_args tf_args, t_dynamic) in
- let cf = mk_class_field (gen.gmk_internal_name "hx" "create") t false pos (Method MethNormal) [] in
- let i = ref 0 in
- let arr_local = mk_local arr pos in
- let ctor = if is_some cl.cl_constructor then cl.cl_constructor else get_last_ctor cl in
- let params = match ctor with
- | None -> []
- | Some ctor ->
- List.map (fun (n,_,t) ->
- let old = !i in
- incr i;
- {
- eexpr = TArray(arr_local, { eexpr = TConst(TInt (Int32.of_int old)); etype = basic.tint; epos = pos } );
- etype = t_dynamic;
- epos = pos
- }
- ) ( fst ( get_fun ctor.cf_type ) )
- in
- let expr = mk_return {
- eexpr = TNew(cl, tparams, params);
- etype = TInst(cl, tparams);
- epos = pos
- } in
- let fn = {
- eexpr = TFunction({
- tf_args = tf_args;
- tf_type = t_dynamic;
- tf_expr = mk_block expr
- });
- etype = t;
- epos = pos
- } in
- cf.cf_expr <- Some fn;
- cf
- in
- let create_empty =
- let t = TFun([],t_dynamic) in
- let cf = mk_class_field (gen.gmk_internal_name "hx" "createEmpty") t false pos (Method MethNormal) [] in
- let fn = {
- eexpr = TFunction({
- tf_args = [];
- tf_type = t_dynamic;
- tf_expr = mk_block (mk_return ( gen.gtools.rf_create_empty cl tparams pos ))
- });
- etype = t;
- epos = pos
- } in
- cf.cf_expr <- Some fn;
- cf
- in
- (* if rcf_handle_statics is false, there is no reason to make createEmpty/create not be static *)
- if ctx.rcf_handle_statics then begin
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ [create_empty; create];
- cl.cl_fields <- PMap.add create_empty.cf_name create_empty cl.cl_fields;
- cl.cl_fields <- PMap.add create.cf_name create cl.cl_fields;
- if is_override then begin
- cl.cl_overrides <- create_empty :: create :: cl.cl_overrides
- end
- end else begin
- cl.cl_ordered_statics <- cl.cl_ordered_statics @ [create_empty; create];
- cl.cl_statics <- PMap.add create_empty.cf_name create_empty cl.cl_statics;
- cl.cl_statics <- PMap.add create.cf_name create cl.cl_statics
- end
- (*
- Implements:
- __hx_lookupField(field:String, throwErrors:Bool, isCheck:Bool, handleProperties:Bool, isFirst:Bool):Dynamic
- __hx_lookupField_f(field:String, throwErrors:Bool, handleProperties:Bool, isFirst:Bool):Float
- __hx_lookupSetField(field:String, value:Dynamic, handleProperties:Bool, isFirst:Bool):Dynamic;
- __hx_lookupSetField(field:String, value:Float, handleProperties:Bool, isFirst:Bool):Float;
- *)
- let implement_final_lookup ctx cl =
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let pos = cl.cl_pos in
- let is_override = is_override cl in
- let this = { eexpr = TConst(TThis); etype = TInst(cl, List.map snd cl.cl_params); epos = pos } in
- (*
- this function will create the class fields and call callback for each version
- callback : is_float fields_args switch_var throw_errors_option is_check_option value_option : texpr list
- *)
- let create_cfs is_dynamic callback =
- let create_cf is_float is_set =
- let name = gen.gmk_internal_name "hx" ( (if is_set then "lookupSetField" else "lookupField") ^ (if is_float then "_f" else "") ) in
- let field_args, switch_var = field_type_args ctx pos in
- let ret_t = if is_float then basic.tfloat else t_dynamic in
- let tf_args, throw_errors_opt =
- if is_set then
- field_args, None
- else
- let v = alloc_var "throwErrors" basic.tbool in
- field_args @ [v,None], Some v
- in
- let tf_args, is_check_opt =
- if is_set || is_float then
- tf_args, None
- else
- let v = alloc_var "isCheck" basic.tbool in
- tf_args @ [v,None], Some v
- in
- let tf_args, value_opt =
- if not is_set then
- tf_args, None
- else
- let v = alloc_var "value" ret_t in
- field_args @ [v,None], Some v
- in
- let fun_t = TFun(fun_args tf_args, ret_t) in
- let cf = mk_class_field name fun_t false pos (Method MethNormal) [] in
- let block = callback is_float field_args switch_var throw_errors_opt is_check_opt value_opt in
- let block = if not is_set then let tl = begin
- let throw_errors_local = mk_local (get throw_errors_opt) pos in
- let mk_check_throw msg =
- {
- eexpr = TIf(throw_errors_local, mk_throw ctx msg pos, Some (mk_return (null ret_t pos)));
- etype = ret_t;
- epos = pos
- } in
- let mk_may_check_throw msg = if is_dynamic then mk_return (null ret_t pos) else mk_check_throw msg in
- if is_float then begin
- [
- mk_may_check_throw "Field not found or incompatible field type.";
- ]
- end else begin
- let is_check_local = mk_local (get is_check_opt) pos in
- [
- {
- eexpr = TIf(is_check_local, mk_return (undefined pos), Some( mk_may_check_throw "Field not found." ));
- etype = ret_t;
- epos = pos;
- }
- ]
- end
- end in block @ tl else block in
- cf.cf_expr <- Some(
- {
- eexpr = TFunction({
- tf_args = tf_args;
- tf_type = ret_t;
- tf_expr = { eexpr = TBlock(block); etype = ret_t; epos = pos }
- });
- etype = fun_t;
- epos = pos
- }
- );
- cf
- in
- let cfs =
- [
- create_cf false false;
- create_cf true false;
- create_cf false true;
- create_cf true true
- ] in
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ cfs;
- List.iter (fun cf ->
- cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields;
- if is_override then cl.cl_overrides <- cf :: cl.cl_overrides
- ) cfs
- in
- if is_some cl.cl_dynamic then begin
- (* let abstract_dyn_lookup_implementation ctx this hash_local may_value is_float pos = *)
- (* callback : is_float fields_args switch_var throw_errors_option is_check_option value_option : texpr list *)
- if is_first_dynamic cl then
- create_cfs true (fun is_float fields_args switch_var _ _ value_opt ->
- abstract_dyn_lookup_implementation ctx this (mk_local switch_var pos) (Option.map (fun v -> mk_local v pos) value_opt) is_float pos
- )
- end else if not is_override then begin
- create_cfs false (fun is_float fields_args switch_var _ _ value_opt ->
- match value_opt with
- | None -> (* is not set *)
- []
- | Some _ -> (* is set *)
- if is_float then
- [ mk_throw ctx "Cannot access field for writing or incompatible type." pos ]
- else
- [ mk_throw ctx "Cannot access field for writing." pos ]
- )
- end
- (* *)
- let implement_get_set ctx cl =
- let gen = ctx.rcf_gen in
- let mk_cfield is_set is_float =
- let pos = cl.cl_pos in
- let basic = ctx.rcf_gen.gcon.basic in
- let tf_args, switch_var = field_type_args ctx pos in
- let field_args = tf_args in
- let local_switch_var = { eexpr = TLocal(switch_var); etype = switch_var.v_type; epos = pos } in
- let is_static = alloc_var "isStatic" basic.tbool in
- let is_static_local = { eexpr = TLocal(is_static); etype = basic.tbool; epos = pos } in
- let handle_prop = alloc_var "handleProperties" basic.tbool in
- let handle_prop_local = mk_local handle_prop pos in
- let this = { eexpr = TConst TThis; etype = TInst(cl, List.map snd cl.cl_params); epos = pos } in
- let mk_this_call_raw name fun_t params =
- { eexpr = TCall( { (mk_field_access gen this name pos) with etype = fun_t; }, params ); etype = snd (get_args fun_t); epos = pos }
- in
- let tf_args = if ctx.rcf_handle_statics then tf_args @ [is_static, None] else tf_args in
- let fun_type = ref (TFun([], basic.tvoid)) in
- let fun_name = ctx.rcf_gen.gmk_internal_name "hx" ( (if is_set then "setField" else "getField") ^ (if is_float then "_f" else "") ) in
- let cfield = mk_class_field fun_name !fun_type false pos (Method MethNormal) [] in
- let maybe_cast e = e in
- let t = TInst(cl, List.map snd cl.cl_params) in
- (* if it's not latest hxgen class -> check super *)
- let mk_do_default args do_default =
- match cl.cl_super with
- | None -> fun () -> maybe_cast (do_default ())
- | Some (super, sparams) when not (is_hxgen (TClassDecl super)) ->
- fun () -> maybe_cast (do_default ())
- | _ ->
- fun () ->
- mk_return {
- eexpr = TCall(
- { eexpr = TField({ eexpr = TConst TSuper; etype = t; epos = pos }, FInstance(cl, List.map snd cl.cl_params, cfield)); etype = !fun_type; epos = pos },
- (List.map (fun (v,_) -> mk_local v pos) args) );
- etype = if is_float then basic.tfloat else t_dynamic;
- epos = pos;
- };
- in
- (* if it is set function, there are some different set fields to do *)
- let do_default, do_default_static , do_field, tf_args = if is_set then begin
- let value_var = alloc_var "value" (if is_float then basic.tfloat else t_dynamic) in
- let value_local = { eexpr = TLocal(value_var); etype = value_var.v_type; epos = pos } in
- let tf_args = tf_args @ [value_var,None; handle_prop, None; ] in
- let lookup_name = gen.gmk_internal_name "hx" ("lookupSetField" ^ if is_float then "_f" else "") in
- let do_default =
- fun () ->
- mk_return (mk_this_call_raw lookup_name (TFun(fun_args (field_args @ [value_var,None]),value_var.v_type)) ( List.map (fun (v,_) -> mk_local v pos) field_args @ [ value_local ] ))
- in
- let do_field cf cf_type is_static =
- let get_field ethis = { eexpr = TField (ethis, if is_static then FStatic (cl, cf) else FInstance(cl, List.map snd cl.cl_params, cf)); etype = cf_type; epos = pos } in
- let this = if is_static then mk_classtype_access cl pos else { eexpr = TConst(TThis); etype = t; epos = pos } in
- let value_local = if is_float then match follow cf_type with
- | TInst({ cl_kind = KTypeParameter _ }, _) ->
- mk_cast t_dynamic value_local
- | _ ->
- value_local
- else
- value_local
- in
- let ret =
- {
- eexpr = TBlock([
- {
- eexpr = TBinop(Ast.OpAssign,
- get_field this,
- mk_cast cf_type value_local);
- etype = cf_type;
- epos = pos;
- };
- mk_return value_local
- ]);
- etype = cf_type;
- epos = pos;
- } in
- match cf.cf_kind with
- | Var { v_write = AccCall } ->
- let bl =
- [
- mk_this_call_raw ("set_" ^ cf.cf_name) (TFun(["value",false,cf.cf_type], cf.cf_type)) [ value_local ];
- mk_return value_local
- ] in
- if Type.is_extern_field cf then
- { eexpr = TBlock bl; etype = value_local.etype; epos = pos }
- else
- {
- eexpr = TIf(
- handle_prop_local,
- { eexpr = TBlock bl; etype = value_local.etype; epos = pos },
- Some ret);
- etype = value_local.etype;
- epos = pos;
- }
- | _ ->
- ret
- in
- (mk_do_default tf_args do_default, do_default, do_field, tf_args)
- end else begin
- (* (field, isStatic, throwErrors, isCheck):Dynamic *)
- let throw_errors = alloc_var "throwErrors" basic.tbool in
- let throw_errors_local = mk_local throw_errors pos in
- let do_default, tf_args = if not is_float then begin
- let is_check = alloc_var "isCheck" basic.tbool in
- let is_check_local = mk_local is_check pos in
- let tf_args = tf_args @ [ throw_errors,None; ] in
- (* default: if (isCheck) return __undefined__ else if(throwErrors) throw "Field not found"; else return null; *)
- let lookup_name = gen.gmk_internal_name "hx" "lookupField" in
- let do_default =
- fun () ->
- mk_return (mk_this_call_raw lookup_name (TFun(fun_args (field_args @ [throw_errors,None;is_check,None; ]),t_dynamic)) ( List.map (fun (v,_) -> mk_local v pos) field_args @ [ throw_errors_local; is_check_local; ] ))
- in
- (do_default, tf_args @ [ is_check,None; handle_prop,None; ])
- end else begin
- let tf_args = tf_args @ [ throw_errors,None; ] in
- let lookup_name = gen.gmk_internal_name "hx" "lookupField_f" in
- let do_default =
- fun () ->
- mk_return (mk_this_call_raw lookup_name (TFun(fun_args (field_args @ [throw_errors,None; ]),basic.tfloat)) ( List.map (fun (v,_) -> mk_local v pos) field_args @ [ throw_errors_local; ] ))
- in
- (do_default, tf_args @ [ handle_prop,None; ])
- end in
- let get_field cf cf_type ethis cl name =
- match cf.cf_kind with
- | Var { v_read = AccCall } when Type.is_extern_field cf ->
- mk_return (mk_this_call_raw ("get_" ^ cf.cf_name) (TFun(["value",false,cf.cf_type], cf.cf_type)) [ ])
- | Var { v_read = AccCall } ->
- {
- eexpr = TIf(
- handle_prop_local,
- mk_return (mk_this_call_raw ("get_" ^ cf.cf_name) (TFun(["value",false,cf.cf_type], cf.cf_type)) [ ]),
- Some { eexpr = TField (ethis, FInstance(cl, List.map snd cl.cl_params, cf)); etype = cf_type; epos = pos }
- );
- etype = cf_type;
- epos = pos;
- }
- | Var _
- | Method MethDynamic -> { eexpr = TField (ethis, FInstance(cl,List.map snd cl.cl_params,cf)); etype = cf_type; epos = pos }
- | _ ->
- { eexpr = TField (this, FClosure(Some (cl,[]), cf)); etype = cf_type; epos = pos } (* TODO: FClosure change *)
- in
- let do_field cf cf_type static =
- let this = if static then mk_classtype_access cl pos else { eexpr = TConst(TThis); etype = t; epos = pos } in
- match is_float, follow cf_type with
- | true, TInst( { cl_kind = KTypeParameter _ }, _ ) ->
- mk_return (mk_cast basic.tfloat (mk_cast t_dynamic (get_field cf cf_type this cl cf.cf_name)))
- | _ ->
- mk_return (maybe_cast (get_field cf cf_type this cl cf.cf_name ))
- in
- (mk_do_default tf_args do_default, do_default, do_field, tf_args)
- end in
- let get_fields static =
- let ret = collect_fields cl ( if is_float || is_set then Some (false) else None ) (Some static) in
- let ret = if is_set then List.filter (fun (_,cf) ->
- match cf.cf_kind with
- (* | Var { v_write = AccNever } -> false *)
- | _ -> not (Meta.has Meta.ReadOnly cf.cf_meta)) ret
- else
- List.filter (fun (_,cf) ->
- match cf.cf_kind with
- (* | Var { v_read = AccNever } -> false *)
- | _ -> true) ret in
- if is_float then
- List.filter (fun (_,cf) -> (* TODO: maybe really apply_params in cf.cf_type. The benefits would be limited, though *)
- match follow (ctx.rcf_gen.greal_type (ctx.rcf_gen.gfollow#run_f cf.cf_type)) with
- | TDynamic _ | TMono _
- | TInst ({ cl_kind = KTypeParameter _ }, _) -> true
- | t when like_float t && not (like_i64 t) -> true
- | _ -> false
- ) ret
- else
- (* dynamic will always contain all references *)
- ret
- in
- (* now we have do_default, do_field and tf_args *)
- (* so create the switch expr *)
- fun_type := TFun(List.map (fun (v,_) -> (v.v_name, false, v.v_type)) tf_args, if is_float then basic.tfloat else t_dynamic );
- let has_fields = ref false in
- let mk_switch static =
- let fields = get_fields static in
- let fields = List.filter (fun (_, cf) -> match is_set, cf.cf_kind with
- | true, Var { v_write = AccCall } -> true
- | false, Var { v_read = AccCall } -> true
- | _ -> not (Type.is_extern_field cf)) fields
- in
- (if fields <> [] then has_fields := true);
- let cases = List.map (fun (names, cf) ->
- (if names = [] then assert false);
- (List.map (switch_case ctx pos) names, do_field cf cf.cf_type static)
- ) fields in
- let default = Some(if static then do_default_static() else do_default()) in
- { eexpr = TSwitch(local_switch_var, cases, default); etype = basic.tvoid; epos = pos }
- in
- let content = if ctx.rcf_handle_statics then
- mk_block { eexpr = TIf(is_static_local, mk_switch true, Some(mk_switch false)); etype = basic.tvoid; epos = pos }
- else
- mk_block (mk_switch false)
- in
- let is_override = match cl.cl_super with
- | Some (cl, _) when is_hxgen (TClassDecl cl) -> true
- | _ -> false
- in
- if !has_fields || (not is_override) then begin
- let func =
- {
- tf_args = tf_args;
- tf_type = if is_float then basic.tfloat else t_dynamic;
- tf_expr = content;
- } in
- let func = { eexpr = TFunction(func); etype = !fun_type; epos = pos } in
- cfield.cf_type <- !fun_type;
- cfield.cf_expr <- Some func;
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ [cfield];
- cl.cl_fields <- PMap.add fun_name cfield cl.cl_fields;
- (if is_override then cl.cl_overrides <- cfield :: cl.cl_overrides)
- end else ()
- in
- (if ctx.rcf_float_special_case then mk_cfield true true);
- mk_cfield true false;
- mk_cfield false false;
- (if ctx.rcf_float_special_case then mk_cfield false true)
- let mk_field_access_r ctx pos local field is_float is_static throw_errors set_option =
- let is_set = is_some set_option in
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let fun_name = ctx.rcf_gen.gmk_internal_name "hx" ( (if is_set then "setField" else "getField") ^ (if is_float then "_f" else "") ) in
- let tf_args, _ = field_type_args ctx pos in
- let tf_args, args = fun_args tf_args, field in
- let rett = if is_float then basic.tfloat else t_dynamic in
- let tf_args, args = if ctx.rcf_handle_statics then tf_args @ [ "isStatic", false, basic.tbool ], args @ [is_static] else tf_args, args in
- let tf_args, args = if is_set then tf_args @ [ "setVal", false, rett ], args @ [get set_option] else tf_args, args in
- let tf_args, args = tf_args @ [ "throwErrors",false,basic.tbool ], args @ [throw_errors] in
- let tf_args, args = if is_set || is_float then tf_args, args else tf_args @ [ "isCheck", false, basic.tbool ], args @ [{ eexpr = TConst(TBool false); etype = basic.tbool; epos = pos }] in
- let tf_args, args = tf_args @ [ "handleProperties",false,basic.tbool; ], args @ [ mk_bool ctx false pos; ] in
- {
- eexpr = TCall(
- { (mk_field_access gen local fun_name pos) with etype = TFun(tf_args, rett) },
- args);
- etype = rett;
- epos = pos;
- }
- let implement_fields ctx cl =
- (*
- implement two kinds of fields get:
- classFields
- generic 'fields': receives a parameter isInstance
- will receive an Array<String> and start pushing the fields into it.
- //add all common fields
- if(isInstance)
- {
- //add methods
- } else {
- super.fields(isInstance, array);
- }
- *)
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let pos = cl.cl_pos in
- (*
- let rec has_no_dynamic cl =
- if is_some cl.cl_dynamic then
- false
- else match cl.cl_super with
- | None -> true
- | Some(cl,_) -> has_no_dynamic cl
- in
- *)
- (* Type.getClassFields() *)
- if ctx.rcf_handle_statics then begin
- let name = gen.gmk_internal_name "hx" "classFields" in
- let v_base_arr = alloc_var "baseArr" (basic.tarray basic.tstring) in
- let base_arr = mk_local v_base_arr pos in
- let tf_args = [v_base_arr,None] in
- let t = TFun(fun_args tf_args, basic.tvoid) in
- let cf = mk_class_field name t false pos (Method MethNormal) [] in
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ [cf];
- cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields;
- (if is_override cl then cl.cl_overrides <- cf :: cl.cl_overrides);
- (*
- var newarr = ["field1", "field2"] ...;
- *)
- let fields = collect_fields cl None (Some true) in
- let mk_push value =
- { eexpr = TCall({ (mk_field_access gen base_arr "push" pos) with etype = TFun(["x", false, basic.tstring], basic.tint) }, [value] ); etype = basic.tint; epos = pos }
- in
- let new_arr_contents =
- {
- eexpr = TBlock(
- List.map (fun (_,cf) -> mk_push { eexpr = TConst(TString(cf.cf_name)); etype = basic.tstring; epos = pos }) fields
- );
- etype = basic.tvoid;
- epos = pos
- } in
- let expr = new_arr_contents in
- let fn =
- {
- tf_args = tf_args;
- tf_type = basic.tvoid;
- tf_expr = mk_block expr
- } in
- cf.cf_expr <- Some { eexpr = TFunction(fn); etype = t; epos = pos }
- end;
- let fields =
- (*
- function __hx_fields(baseArr:Array<String>, isInstanceFields:Bool)
- {
- //add all variable fields
- //then:
- if (isInstanceFields)
- {
- //add all method fields as well
- } else {
- super.__hx_fields(baseArr, isInstanceFields);
- }
- }
- *)
- let name = gen.gmk_internal_name "hx" "getFields" in
- let v_base_arr, v_is_inst = alloc_var "baseArr" (basic.tarray basic.tstring), alloc_var "isInstanceFields" basic.tbool in
- let base_arr, is_inst = mk_local v_base_arr pos, mk_local v_is_inst pos in
- let tf_args = (v_base_arr,None) :: (if ctx.rcf_handle_statics then [v_is_inst, None] else []) in
- let t = TFun(fun_args tf_args, basic.tvoid) in
- let cf = mk_class_field name t false pos (Method MethNormal) [] in
- let mk_push value =
- { eexpr = TCall({ (mk_field_access gen base_arr "push" pos) with etype = TFun(["x", false, basic.tstring], basic.tint); }, [value] ); etype = basic.tint; epos = pos }
- in
- let has_value = ref false in
- let map_fields =
- List.map (fun (_,cf) ->
- match cf.cf_kind with
- | Var _
- | Method MethDynamic when not (List.memq cf cl.cl_overrides) ->
- has_value := true;
- mk_push { eexpr = TConst(TString(cf.cf_name)); etype = basic.tstring; epos = pos }
- | _ -> null basic.tvoid pos
- )
- in
- (*
- if it is first_dynamic, then we need to enumerate the dynamic fields
- *)
- let if_not_inst = if is_some cl.cl_dynamic && is_first_dynamic cl then begin
- has_value := true;
- Some (enumerate_dynamic_fields ctx cl mk_push)
- end else
- None
- in
- let if_not_inst = if is_override cl then
- Some(
- {
- eexpr = TBlock(
- (if is_some if_not_inst then get if_not_inst else []) @
- [{
- eexpr = TCall(
- { eexpr = TField({ eexpr = TConst TSuper; etype = TInst(cl, List.map snd cl.cl_params); epos = pos }, FInstance(cl, List.map snd cl.cl_params, cf)); etype = t; epos = pos },
- base_arr :: (if ctx.rcf_handle_statics then [is_inst] else [])
- );
- etype = basic.tvoid;
- epos = pos
- }]
- );
- etype = basic.tvoid;
- epos = pos
- }
- ) else if is_some if_not_inst then
- Some({ eexpr = TBlock(get if_not_inst); etype = basic.tvoid; epos = pos })
- else
- None
- in
- let expr_contents = map_fields (collect_fields cl (Some false) (Some false)) in
- let expr_contents = if ctx.rcf_handle_statics then
- expr_contents @
- [ {
- eexpr = TIf(is_inst,
- { eexpr = TBlock( map_fields (collect_fields cl (Some true) (Some false)) ); etype = basic.tvoid; epos = pos },
- if_not_inst
- );
- etype = basic.tvoid;
- epos = pos
- } ]
- else
- expr_contents @ (if is_some if_not_inst then [ get if_not_inst ] else [])
- in
- let expr =
- {
- eexpr = TBlock( expr_contents );
- etype = basic.tvoid;
- epos = pos;
- } in
- let fn =
- {
- tf_args = tf_args;
- tf_type = basic.tvoid;
- tf_expr = expr
- } in
- (if !has_value || (not (is_override cl)) then begin
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ [cf];
- cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields;
- (if is_override cl then cl.cl_overrides <- cf :: cl.cl_overrides)
- end);
- cf.cf_expr <- Some { eexpr = TFunction(fn); etype = t; epos = pos }
- in
- ignore fields
- let implement_class_methods ctx cl =
- ctx.rcf_class_cl <- Some cl;
- let pos = cl.cl_pos in
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- (*
- fields -> redirected to classFields
- getField -> redirected to getField with isStatic true
- setField -> isStatic true
- invokeField -> isStatic true
- getClass -> null
- create -> proxy
- createEmpty -> proxy
- *)
- let is_override = is_override cl in
- let name = "classProxy" in
- let t = (TInst(ctx.rcf_object_iface,[])) in
- (* let cf = mk_class_field name t false pos (Var { v_read = AccNormal; v_write = AccNormal }) [] in *)
- let register_cf cf override =
- cl.cl_ordered_fields <- cf :: cl.cl_ordered_fields;
- cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields;
- if override then cl.cl_overrides <- cf :: cl.cl_overrides
- in
- (* register_cf cf false; *)
- let this_t = TInst(cl, List.map snd cl.cl_params) in
- let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
- let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
- let proxy = mk_this name t in
- (*let ctor =
- let cls = alloc_var "cls" t in
- let tf_args = [cls, None] in
- let t = TFun(fun_args tf_args, basic.tvoid) in
- let cf = mk_class_field "new" t true pos (Method MethNormal) [] in
- cf.cf_expr <- Some({
- eexpr = TFunction({
- tf_args = tf_args;
- tf_type = basic.tvoid;
- tf_expr = mk_block {
- eexpr = TBinop(Ast.OpAssign, proxy, mk_local cls pos);
- etype = cls.v_type;
- epos = pos;
- }
- });
- etype = t;
- epos = pos;
- });
- cf
- in
- register_cf ctor false;*)
- (* setting it as DynamicObject makes getClass return null *)
- let get_class =
- cl.cl_meta <- (Meta.DynamicObject, [], pos) :: cl.cl_meta
- in
- ignore get_class;
- (* fields -> if isInstanceField, redir the method. If not, return classFields *)
- let fields =
- let name = gen.gmk_internal_name "hx" "getFields" in
- let v_base_arr, v_is_inst = alloc_var "baseArr" (basic.tarray basic.tstring), alloc_var "isInstanceFields" basic.tbool in
- let base_arr, is_inst = mk_local v_base_arr pos, mk_local v_is_inst pos in
- let tf_args = [ v_base_arr,None; v_is_inst, None ] in
- let t = TFun(fun_args tf_args, basic.tvoid) in
- let cf = mk_class_field name t false pos (Method MethNormal) [] in
- cf.cf_expr <- Some({
- eexpr = TFunction({
- tf_args = tf_args;
- tf_type = basic.tvoid;
- tf_expr = mk_block {
- eexpr = TIf(is_inst,
- { eexpr = TCall( { (mk_field_access gen proxy name pos) with etype = t }, [base_arr;is_inst]); etype = basic.tvoid; epos = pos },
- Some { eexpr = TCall(mk_this (gen.gmk_internal_name "hx" "classFields") (TFun(["baseArr",false,basic.tarray basic.tstring], basic.tvoid)), [base_arr]); etype = basic.tvoid; epos = pos });
- etype = basic.tvoid;
- epos = pos
- }
- });
- etype = t;
- epos = pos;
- });
- cf
- in
- register_cf fields (is_override);
- let do_proxy field tf_args ret is_static_argnum =
- let field = gen.gmk_internal_name "hx" field in
- let t = TFun(fun_args tf_args, ret) in
- let cf = mk_class_field field t false pos (Method MethNormal) [] in
- let is_void = is_void ret in
- let may_return e = if is_void then mk_block e else mk_block (mk_return e) in
- let i = ref 0 in
- cf.cf_expr <- Some({
- eexpr = TFunction({
- tf_args = tf_args;
- tf_type = ret;
- tf_expr = may_return {
- eexpr = TCall(
- { (mk_field_access gen proxy field pos) with etype = t },
- List.map (fun (v,_) ->
- let lasti = !i in
- incr i;
- if lasti = is_static_argnum then
- { eexpr = TConst(TBool true); etype = basic.tbool; epos = pos }
- else
- mk_local v pos
- ) tf_args);
- etype = ret;
- epos = pos
- }
- });
- etype = t;
- epos = pos;
- });
- cf
- in
- (* getClassFields -> redir *)
- register_cf (do_proxy "classFields" [ alloc_var "baseArr" (basic.tarray basic.tstring), None ] basic.tvoid (-1)) true;
- (*register_cf (do_proxy "classFields" [ alloc_var "baseArr" (basic.tarray basic.tstring), None ] basic.tvoid (-1)) true;*)
- let fst_args, _ = field_type_args ctx pos in
- let fst_args_len = List.length fst_args in
- (* getField -> redir the method with static = true *)
- (* setField -> redir the methods with static = true *)
- (if ctx.rcf_float_special_case then
- register_cf (do_proxy "getField_f" (fst_args @ [ alloc_var "isStatic" basic.tbool, None; alloc_var "throwErrors" basic.tbool, None ]) basic.tfloat fst_args_len) true;
- register_cf (do_proxy "setField_f" (fst_args @ [ alloc_var "isStatic" basic.tbool, None; alloc_var "value" basic.tfloat, None ]) basic.tfloat fst_args_len) true
- );
- register_cf (do_proxy "getField" (fst_args @ [ alloc_var "isStatic" basic.tbool, None; alloc_var "throwErrors" basic.tbool, None; alloc_var "isCheck" basic.tbool, None; alloc_var "handleProperties" basic.tbool,None; ]) t_dynamic fst_args_len) true;
- register_cf (do_proxy "setField" (fst_args @ [ alloc_var "isStatic" basic.tbool, None; alloc_var "value" t_dynamic, None; alloc_var "handleProperties" basic.tbool,None; ]) t_dynamic fst_args_len) true;
- (* invokeField -> redir the method with static = true *)
- register_cf (do_proxy "invokeField" (fst_args @ [ alloc_var "isStatic" basic.tbool, None; alloc_var "dynArgs" (basic.tarray t_dynamic), None ]) t_dynamic fst_args_len) true;
- (* create / createEmpty -> redir the method *)
- register_cf (do_proxy "create" [ alloc_var "arr" (basic.tarray t_dynamic), None ] t_dynamic (-1)) true;
- register_cf (do_proxy "createEmpty" [ ] t_dynamic (-1)) true
- let implement_get_class ctx cl =
- (*
- if it is DynamicObject, return null;
- if it is not, just do the following:
- if (typehandle(this.class) == typehandle(MyClass))
- return (MyClass.__hx_class != null ? MyClass.__hx_class : MyClass.__hx_class = create_empty(MyClass));
- return MyClass.__hx_class = haxe.lang.Runtime.getClass(MyClass);
- implement both on static and non-static contexts. This way we can call without references.
- *)
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let pos = cl.cl_pos in
- let tclass = get_cl ( (Hashtbl.find gen.gtypes ([],"Class")) ) in
- let cls = TInst(tclass, [ TInst(cl, List.map (fun _ -> t_dynamic) cl.cl_params) ]) in
- let cls_dyn = TInst(tclass, [t_dynamic]) in
- let expr, static_cfs =
- if Meta.has Meta.DynamicObject cl.cl_meta then
- mk_return (null t_dynamic pos), []
- else
- let cache_name = (gen.gmk_internal_name "hx" "class") in
- let cache = mk_class_field cache_name cls false pos (Var { v_read = AccNormal; v_write = AccNormal }) [] in
- cl.cl_ordered_statics <- cl.cl_ordered_statics @ [ cache ];
- cl.cl_statics <- PMap.add cache_name cache cl.cl_statics;
- let cache_access = mk_static_field_access cl cache_name cls pos in
- let create_expr = {
- eexpr = TNew(get ctx.rcf_class_cl, [], [gen.gtools.rf_create_empty cl (List.map (fun _ -> t_dynamic) cl.cl_params) pos]);
- etype = cls;
- epos = pos
- } in
- (if ctx.rcf_class_eager_creation then cache.cf_expr <- Some(create_expr));
- let expr = if ctx.rcf_class_eager_creation then
- mk_return cache_access
- else
- mk_return {
- eexpr = TIf(
- { eexpr = TBinop(Ast.OpNotEq, cache_access, null cls pos); etype = basic.tbool; epos = pos },
- cache_access,
- Some({ eexpr = TBinop(Ast.OpAssign, cache_access, create_expr); etype = cls; epos = pos })
- );
- etype = cls;
- epos = pos
- }
- in
- expr, []
- in
- let func =
- {
- eexpr = TFunction({
- tf_args = [];
- tf_type = cls_dyn;
- tf_expr = expr
- });
- etype = TFun([],cls_dyn);
- epos = pos
- } in
- let get_cl_static = mk_class_field (gen.gmk_internal_name "hx" "getClassStatic") (TFun([],cls_dyn)) false pos (Method MethNormal) [] in
- let get_cl = mk_class_field (gen.gmk_internal_name "hx" "getClass") (TFun([],cls_dyn)) false pos (Method MethNormal) [] in
- get_cl_static.cf_expr <- Some func;
- get_cl.cf_expr <- Some func;
- let all_f = [get_cl] in
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ all_f;
- List.iter (fun cf -> cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields) all_f;
- let all_f = get_cl_static :: static_cfs in
- cl.cl_ordered_statics <- cl.cl_ordered_statics @ all_f;
- List.iter (fun cf -> cl.cl_statics <- PMap.add cf.cf_name cf cl.cl_statics) all_f;
- if is_override cl then cl.cl_overrides <- get_cl :: cl.cl_overrides
- let implement_invokeField ctx ~slow_invoke cl =
- (*
- There are two ways to implement an haxe reflection-enabled class:
- When we extend a non-hxgen class, and when we extend the base HxObject class.
- Because of the added boiler plate we'd add every time we extend a non-hxgen class to implement a big IHxObject
- interface, we'll handle the cases differently when implementing each interface.
- At the IHxObject interface, there's only invokeDynamic(field, args[]), while at the HxObject class there are
- the other, more optimized methods, that follow the Function class interface.
- Since this will only be called by the Closure class, this conversion can be properly dealt with later.
- TODO: create the faster version. By now only invokeDynamic will be implemented
- *)
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let pos = cl.cl_pos in
- let has_method = ref false in
- let is_override = ref false in
- let rec extends_hxobject cl =
- match cl.cl_super with
- | None -> true
- | Some (cl,_) when is_hxgen (TClassDecl cl) -> is_override := true; extends_hxobject cl
- | _ -> false
- in
- let field_args, switch_var = field_type_args ctx cl.cl_pos in
- let field_args_exprs = List.map (fun (v,_) -> mk_local v pos) field_args in
- let is_static = alloc_var "isStatic" basic.tbool in
- let dynamic_arg = alloc_var "dynargs" (basic.tarray t_dynamic) in
- let all_args = field_args @ (if ctx.rcf_handle_statics then [ is_static,None; dynamic_arg,None ] else [ dynamic_arg, None ] ) in
- let fun_t = TFun(fun_args all_args, t_dynamic) in
- let this_t = TInst(cl, List.map snd cl.cl_params) in
- let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
- let apply_object cf = apply_params cf.cf_params (List.map (fun _ -> t_dynamic) cf.cf_params) cf.cf_type in
- let mk_this_call_raw name fun_t params =
- { eexpr = TCall( { (mk_field_access gen this name pos) with etype = fun_t }, params ); etype = snd (get_args fun_t); epos = pos }
- in
- let mk_this_call cf params =
- let t = apply_object cf in
- (* the return type transformation into Dynamic *)
- (* is meant to avoid return-type casting after functions with *)
- (* type parameters are properly inferred at TypeParams.infer_params *)
- (* e.g. function getArray<T : SomeType>(t:T):Array<T>; after infer_params, *)
- (* T will be inferred as SomeType, but the returned type will still be typed *)
- (* as Array<Dynamic> *)
- let args, ret = get_args t in
- let ret = match follow ret with
- | TAbstract ({ a_path = ([], "Void") },[]) -> ret
- | _ -> ret
- in
- mk_this_call_raw cf.cf_name (TFun(args, ret)) params
- in
- let mk_static_call cf params =
- let t = apply_object cf in
- let _, ret = get_fun (follow t) in
- { eexpr = TCall( mk_static_field_access cl cf.cf_name t pos, params ); etype = ret; epos = pos }
- in
- let extends_hxobject = extends_hxobject cl in
- ignore extends_hxobject;
- (* creates a dynamicInvoke of the class fields listed here *)
- (*
- function dynamicInvoke(field, isStatic, dynargs)
- {
- switch(field)
- {
- case "a": this.a(dynargs[0], dynargs[1], dynargs[2]...);
- default: super.dynamicInvoke //or this.getField(field).invokeField(dynargs)
- }
- }
- *)
- let dyn_fun = mk_class_field (ctx.rcf_gen.gmk_internal_name "hx" "invokeField") fun_t false cl.cl_pos (Method MethNormal) [] in
- let mk_switch_dyn cfs static old =
- (* mk_class_field name t public pos kind params = *)
- let get_case (names,cf) =
- has_method := true;
- let i = ref 0 in
- let dyn_arg_local = mk_local dynamic_arg pos in
- let cases = List.map (switch_case ctx pos) names in
- (cases,
- { eexpr = TReturn(Some ( (if static then mk_static_call else mk_this_call) cf (List.map (fun (name,_,t) ->
- let ret = { eexpr = TArray(dyn_arg_local, mk_int ctx !i pos); etype = t_dynamic; epos = pos } in
- incr i;
- ret
- ) (fst (get_args (cf.cf_type))) ) ));
- etype = basic.tvoid;
- epos = pos
- }
- )
- in
- let cfs = List.filter (fun (_,cf) -> match cf.cf_kind with
- | Method _ -> if List.memq cf cl.cl_overrides then false else true
- | _ -> true) cfs
- in
- let cases = List.map get_case cfs in
- let cases = match old with
- | [] -> cases
- | _ ->
- let ncases = List.map (fun cf -> switch_case ctx pos cf.cf_name) old in
- ( ncases, mk_return ((get slow_invoke) this (mk_local (fst (List.hd field_args)) pos) (mk_local dynamic_arg pos)) ) :: cases
- in
- let default = if !is_override && not(static) then
- (* let call_super ctx fn_args ret_t fn_name this_t pos = *)
- { eexpr = TReturn(Some (call_super ctx all_args t_dynamic dyn_fun cl this_t pos) ); etype = basic.tvoid; epos = pos }
- (*else if ctx.rcf_create_getsetinvoke_fields then (* we always need to run create_getset before *)
- let get_field_name = gen.gmk_internal_name "hx" "getField" in
- { eexpr = TReturn( Some (mk_this_call (PMap.find get_field_name cl.cl_fields) [mk_local dynamic_arg pos] ) ); etype = basic.tvoid; epos = pos }*)
- else (
- (*let field = (gen.gtools.r_field false (TInst(ctx.rcf_ft.func_class,[])) this (mk_local (fst (List.hd all_args)) pos)) in*)
- (* let mk_field_access ctx pos local field is_float is_static throw_errors set_option = *)
- let field = mk_field_access_r ctx pos this field_args_exprs false {eexpr = TConst(TBool static); etype = basic.tbool; epos = pos} { eexpr = TConst(TBool true); etype = basic.tbool; epos = pos } None in
- let field = mk_cast (TInst(ctx.rcf_ft.func_class,[])) field in
- mk_return {
- eexpr = TCall(
- mk_field_access gen field (gen.gmk_internal_name "hx" "invokeDynamic") pos,
- [mk_local dynamic_arg pos]);
- etype = t_dynamic;
- epos = pos
- } )
- in
- {
- eexpr = TSwitch(mk_local switch_var pos, cases, Some default);
- etype = basic.tvoid;
- epos = pos;
- }
- in
- let contents =
- let statics = collect_fields cl (Some true) (Some true) in
- let nonstatics = collect_fields cl (Some true) (Some false) in
- let old_nonstatics = ref [] in
- let nonstatics = match slow_invoke with
- | None -> nonstatics
- | Some _ ->
- List.filter (fun (n,cf) ->
- let is_old = not (PMap.mem cf.cf_name cl.cl_fields) || List.memq cf cl.cl_overrides in
- (if is_old then old_nonstatics := cf :: !old_nonstatics);
- not is_old
- ) nonstatics
- in
- if ctx.rcf_handle_statics then
- {
- eexpr = TIf(mk_local is_static pos, mk_switch_dyn statics true [], Some(mk_switch_dyn nonstatics false !old_nonstatics));
- etype = basic.tvoid;
- epos = pos;
- } else
- mk_switch_dyn nonstatics false !old_nonstatics
- in
- dyn_fun.cf_expr <- Some
- {
- eexpr = TFunction(
- {
- tf_args = all_args;
- tf_type = t_dynamic;
- tf_expr = mk_block contents;
- });
- etype = TFun(fun_args all_args, t_dynamic);
- epos = pos;
- };
- if !is_override && not (!has_method) then () else begin
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ [dyn_fun];
- cl.cl_fields <- PMap.add dyn_fun.cf_name dyn_fun cl.cl_fields;
- (if !is_override then cl.cl_overrides <- dyn_fun :: cl.cl_overrides)
- end
- let implement_varargs_cl ctx cl =
- let pos = cl.cl_pos in
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let this_t = TInst(cl, List.map snd cl.cl_params) in
- let this = { eexpr = TConst(TThis); etype = this_t ; epos = pos } in
- let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
- let invokedyn = gen.gmk_internal_name "hx" "invokeDynamic" in
- let idyn_t = TFun([gen.gmk_internal_name "fn" "dynargs", false, basic.tarray t_dynamic], t_dynamic) in
- let this_idyn = mk_this invokedyn idyn_t in
- let map_fn arity ret vars api =
- let rec loop i acc =
- if i < 0 then
- acc
- else
- let obj = api i t_dynamic None in
- loop (i - 1) (obj :: acc)
- in
- let call_arg = if arity = (-1) then
- api (-1) t_dynamic None
- else if arity = 0 then
- null (basic.tarray t_empty) pos
- else
- { eexpr = TArrayDecl(loop (arity - 1) []); etype = basic.tarray t_empty; epos = pos }
- in
- let expr = {
- eexpr = TCall(
- this_idyn,
- [ call_arg ]
- );
- etype = t_dynamic;
- epos = pos
- } in
- let expr = if like_float ret && not (like_int ret) then mk_cast ret expr else expr in
- [], mk_return expr
- in
- let all_cfs = List.filter (fun cf -> cf.cf_name <> "new" && cf.cf_name <> (invokedyn) && match cf.cf_kind with Method _ -> true | _ -> false) (ctx.rcf_ft.map_base_classfields cl true map_fn) in
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ all_cfs;
- List.iter (fun cf ->
- cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields
- ) all_cfs;
- List.iter (fun cf ->
- cl.cl_overrides <- cf :: cl.cl_overrides
- ) cl.cl_ordered_fields
- let implement_closure_cl ctx cl =
- let pos = cl.cl_pos in
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let field_args, _ = field_type_args ctx pos in
- let obj_arg = alloc_var "target" (TInst(ctx.rcf_object_iface, [])) in
- let this_t = TInst(cl, List.map snd cl.cl_params) in
- let this = { eexpr = TConst(TThis); etype = this_t ; epos = pos } in
- let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
- let tf_args = field_args @ [obj_arg, None] in
- let cfs, ctor_body = List.fold_left (fun (acc_cf,acc_expr) (v,_) ->
- let cf = mk_class_field v.v_name v.v_type false pos (Var { v_read = AccNormal; v_write = AccNormal } ) [] in
- let expr = { eexpr = TBinop(Ast.OpAssign, mk_this v.v_name v.v_type, mk_local v pos); etype = v.v_type; epos = pos } in
- (cf :: acc_cf, expr :: acc_expr)
- ) ([], []) tf_args in
- let map_fn arity ret vars api =
- let this_obj = mk_this "target" (TInst(ctx.rcf_object_iface, [])) in
- let rec loop i acc =
- if i < 0 then
- acc
- else
- let obj = api i t_dynamic None in
- loop (i - 1) (obj :: acc)
- in
- let call_arg = if arity = (-1) then
- api (-1) t_dynamic None
- else if arity = 0 then
- null (basic.tarray t_empty) pos
- else
- { eexpr = TArrayDecl(loop (arity - 1) []); etype = basic.tarray t_empty; epos = pos }
- in
- let expr = {
- eexpr = TCall(
- mk_field_access gen this_obj (gen.gmk_internal_name "hx" "invokeField") pos,
- (List.map (fun (v,_) -> mk_this v.v_name v.v_type) field_args) @
- (if ctx.rcf_handle_statics then
- [ { eexpr = TConst(TBool false); etype = basic.tbool; epos = pos }; call_arg ]
- else
- [ call_arg ]
- )
- );
- etype = t_dynamic;
- epos = pos
- } in
- let expr = if like_float ret && not (like_int ret) then mk_cast ret expr else expr in
- [], mk_return expr
- in
- let all_cfs = List.filter (fun cf -> cf.cf_name <> "new" && match cf.cf_kind with Method _ -> true | _ -> false) (ctx.rcf_ft.map_base_classfields cl true map_fn) in
- List.iter (fun cf ->
- cl.cl_overrides <- cf :: cl.cl_overrides
- ) all_cfs;
- let all_cfs = cfs @ all_cfs in
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ all_cfs;
- List.iter (fun cf ->
- cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields
- ) all_cfs;
- let ctor_t = TFun(fun_args tf_args, basic.tvoid) in
- let ctor_cf = mk_class_field "new" ctor_t true pos (Method MethNormal) [] in
- ctor_cf.cf_expr <- Some {
- eexpr = TFunction({
- tf_args = tf_args;
- tf_type = basic.tvoid;
- tf_expr = { eexpr = TBlock({
- eexpr = TCall({ eexpr = TConst(TSuper); etype = TInst(cl,[]); epos = pos }, [mk_int ctx (-1) pos; mk_int ctx (-1) pos]);
- etype = basic.tvoid;
- epos = pos
- } :: ctor_body); etype = basic.tvoid; epos = pos }
- });
- etype = ctor_t;
- epos = pos
- };
- cl.cl_constructor <- Some ctor_cf;
- let closure_fun eclosure e field is_static =
- let f = { eexpr = TConst(TString field); etype = basic.tstring; epos = eclosure.epos } in
- let args = if ctx.rcf_optimize then [ f; { eexpr = TConst(TInt (hash_field_i32 ctx eclosure.epos field)); etype = basic.tint; epos = eclosure.epos } ] else [ f ] in
- let args = args @ [ mk_cast (TInst(ctx.rcf_object_iface, [])) e ] in
- { eclosure with eexpr = TNew(cl,[],args) }
- in
- closure_fun
- let get_closure_func ctx closure_cl =
- let gen = ctx.rcf_gen in
- let basic = gen.gcon.basic in
- let closure_func eclosure e field is_static =
- mk_cast eclosure.etype { eclosure with
- eexpr = TNew(closure_cl, [], [
- e;
- { eexpr = TConst(TString field); etype = basic.tstring; epos = eclosure.epos }
- ] @ (
- if ctx.rcf_optimize then [ { eexpr = TConst(TInt (hash_field_i32 ctx eclosure.epos field)); etype = basic.tint; epos = eclosure.epos } ] else []
- ));
- etype = TInst(closure_cl,[])
- }
- in
- closure_func
- (*
- main expr -> field expr -> field string -> possible set expr -> should_throw_exceptions -> changed expression
- Changes a get / set
- *
- mutable rcf_on_getset_field : texpr->texpr->string->texpr option->bool->texpr;*)
- let configure_dynamic_field_access ctx is_synf =
- let gen = ctx.rcf_gen in
- let is_dynamic expr fexpr field =
- match (field_access_esp gen (gen.greal_type fexpr.etype) field) with
- | FEnumField _
- | FClassField _ -> false
- | _ -> true
- in
- let configure = if is_synf then DynamicFieldAccess.configure_as_synf else DynamicFieldAccess.configure in
- let maybe_hash = if ctx.rcf_optimize then fun str pos -> Some (hash_field_i32 ctx pos str) else fun str pos -> None in
- configure gen (DynamicFieldAccess.abstract_implementation gen is_dynamic
- (* print_endline *)
- (fun expr fexpr field set is_unsafe ->
- let hash = maybe_hash field fexpr.epos in
- ctx.rcf_on_getset_field expr fexpr field hash set is_unsafe
- )
- (fun ecall fexpr field call_list ->
- let hash = maybe_hash field fexpr.epos in
- ctx.rcf_on_call_field ecall fexpr field hash call_list
- )
- );
- ()
- let replace_reflection ctx cl =
- let gen = ctx.rcf_gen in
- let pos = cl.cl_pos in
- let this_t = TInst(cl, List.map snd cl.cl_params) in
- let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
- let last_fields = match cl.cl_super with
- | None -> PMap.empty
- | Some (super,_) -> super.cl_fields
- in
- let new_fields = ref [] in
- let process_cf static cf =
- match cf.cf_kind with
- | Var _ -> ()
- | _ when Meta.has Meta.ReplaceReflection cf.cf_meta ->
- let name = if String.get cf.cf_name 0 = '_' then String.sub cf.cf_name 1 (String.length cf.cf_name - 1) else cf.cf_name in
- let new_name = gen.gmk_internal_name "hx" name in
- let new_cf = mk_class_field new_name cf.cf_type cf.cf_public cf.cf_pos cf.cf_kind cf.cf_params in
- let fn_args, ret = get_fun (follow cf.cf_type) in
- let tf_args = List.map (fun (name,_,t) -> alloc_var name t, None) fn_args in
- let is_void = is_void ret in
- let expr = {
- eexpr = TCall(
- {
- eexpr = (if static then TField(mk_classtype_access cl pos, FStatic(cl, cf)) else TField(this, FInstance(cl, List.map snd cl.cl_params, cf)));
- etype = cf.cf_type;
- epos = cf.cf_pos;
- },
- List.map (fun (v,_) -> mk_local v cf.cf_pos) tf_args);
- etype = ret;
- epos = cf.cf_pos
- } in
- let new_f =
- {
- tf_args = tf_args;
- tf_type = ret;
- tf_expr = {
- eexpr = TBlock([if is_void then expr else mk_return expr]);
- etype = ret;
- epos = pos;
- }
- } in
- new_cf.cf_expr <- Some({ eexpr = TFunction(new_f); etype = cf.cf_type; epos = cf.cf_pos});
- new_fields := new_cf :: !new_fields;
- (if static then cl.cl_statics <- PMap.add new_name new_cf cl.cl_statics else cl.cl_fields <- PMap.add new_name new_cf cl.cl_fields);
- if not static && PMap.mem new_name last_fields then cl.cl_overrides <- new_cf :: cl.cl_overrides
- | _ -> ()
- in
- List.iter (process_cf false) cl.cl_ordered_fields;
- cl.cl_ordered_fields <- cl.cl_ordered_fields @ !new_fields;
- new_fields := [];
- List.iter (process_cf true) cl.cl_ordered_statics;
- cl.cl_ordered_statics <- cl.cl_ordered_statics @ !new_fields
- (* ******************************************* *)
- (* UniversalBaseClass *)
- (* ******************************************* *)
- (*
- Sets the universal base class for hxgen types (HxObject / IHxObject)
- dependencies:
- As a rule, it should be one of the last module filters to run so any @:hxgen class created in the process
- -Should- only run after TypeParams.RealTypeParams.Modf, since
- *)
- module UniversalBaseClass =
- struct
- let name = "rcf_universal_base_class"
- let priority = min_dep +. 10.
- let default_implementation gen baseclass baseinterface basedynamic =
- (* baseinterface.cl_meta <- (Meta.BaseInterface, [], baseinterface.cl_pos) :: baseinterface.cl_meta; *)
- let rec run md =
- (if is_hxgen md then
- match md with
- | TClassDecl ( { cl_interface = true } as cl ) when cl.cl_path <> baseclass.cl_path && cl.cl_path <> baseinterface.cl_path && cl.cl_path <> basedynamic.cl_path ->
- cl.cl_implements <- (baseinterface, []) :: cl.cl_implements
- | TClassDecl ({ cl_kind = KAbstractImpl _ } as cl) ->
- (*
- TODO: probably here is not the best place to add @:final to KAbstractImpl, also:
- Doesn't it make sense to add @:final to KAbstractImpls on all platforms?
- *)
- if not (Meta.has Meta.Final cl.cl_meta) then cl.cl_meta <- (Meta.Final, [], cl.cl_pos) :: cl.cl_meta
- | TClassDecl ( { cl_super = None } as cl ) when cl.cl_path <> baseclass.cl_path && cl.cl_path <> baseinterface.cl_path && cl.cl_path <> basedynamic.cl_path ->
- if is_some cl.cl_dynamic then
- cl.cl_super <- Some (basedynamic,[])
- else
- cl.cl_super <- Some (baseclass,[])
- | TClassDecl ( { cl_super = Some(super,_) } as cl ) when cl.cl_path <> baseclass.cl_path && cl.cl_path <> baseinterface.cl_path && not ( is_hxgen (TClassDecl super) ) ->
- cl.cl_implements <- (baseinterface, []) :: cl.cl_implements
- | _ -> ()
- );
- md
- in
- run
- let configure gen mapping_func =
- let map e = Some(mapping_func e) in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
- let default_config gen baseclass baseinterface basedynamic =
- let impl = (default_implementation gen baseclass baseinterface basedynamic) in
- configure gen impl
- end;;
- (*
- Priority: must run AFTER UniversalBaseClass
- *)
- let priority = solve_deps name [DAfter UniversalBaseClass.priority]
- let configure ?slow_invoke ctx baseinterface =
- let gen = ctx.rcf_gen in
- let run = (fun md -> match md with
- | TClassDecl cl when is_hxgen md && ( not cl.cl_interface || cl.cl_path = baseinterface.cl_path ) && (match cl.cl_kind with KAbstractImpl _ -> false | _ -> true) ->
- (if Meta.has Meta.ReplaceReflection cl.cl_meta then replace_reflection ctx cl);
- (implement_dynamics ctx cl);
- (if not (PMap.mem (gen.gmk_internal_name "hx" "lookupField") cl.cl_fields) then implement_final_lookup ctx cl);
- (if not (PMap.mem (gen.gmk_internal_name "hx" "getField") cl.cl_fields) then implement_get_set ctx cl);
- (if not (PMap.mem (gen.gmk_internal_name "hx" "invokeField") cl.cl_fields) then implement_invokeField ctx ~slow_invoke:slow_invoke cl);
- (if not (PMap.mem (gen.gmk_internal_name "hx" "classFields") cl.cl_fields) then implement_fields ctx cl);
- (if ctx.rcf_handle_statics && not (PMap.mem (gen.gmk_internal_name "hx" "getClassStatic") cl.cl_statics) then implement_get_class ctx cl);
- (if not cl.cl_interface && not (PMap.mem (gen.gmk_internal_name "hx" "create") cl.cl_fields) then implement_create_empty ctx cl);
- None
- | _ -> None)
- in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) run
- end;;
- (* ******************************************* *)
- (* Object Declaration Mapper *)
- (* ******************************************* *)
- (*
- A simple Object Declaration Mapper. By default it will be a syntax filter, which only runs
- after
- dependencies:
- *)
- module ObjectDeclMap =
- struct
- let name = "object_decl_map"
- let priority = solve_deps name []
- let traverse gen map_fn =
- let rec run e =
- match e.eexpr with
- | TObjectDecl odecl ->
- let e = Type.map_expr run e in
- (match e.eexpr with | TObjectDecl odecl -> map_fn e odecl | _ -> assert false)
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* EnumToClass *)
- (* ******************************************* *)
- (*
- For languages that don't support parameterized enums and/or metadata in enums, we need to transform
- enums into normal classes. This is done at the first module pass by creating new classes with the same
- path inside the modules, and removing the actual enum module by setting it as en extern.
- Later, on the last expression pass, it will transform the TMatch codes into TSwitch. it will introduce a new
- dependency, though:
- * The target must create its own strategy to deal with reflection. As it is right now, we will have a base class
- which the class will extend, create @:$IsEnum metadata for the class, and create @:alias() metadatas for the fields,
- with their tag order (as a string) as their alias. If you are using ReflectionCFs, then you don't have to worry
- about that, as it's already generating all information needed by the haxe runtime.
- so they can be
- dependencies:
- The MatchToSwitch part must run after ExprStatementUnwrap as modified expressions might confuse it (not so true anymore)
- *)
- module EnumToClass =
- struct
- let name = "enum_to_class"
- let priority = solve_deps name []
- type t = {
- ec_tbl : (path, tclass) Hashtbl.t;
- }
- let new_t () =
- {
- ec_tbl = Hashtbl.create 10
- }
- (* ******************************************* *)
- (* EnumToClassModf *)
- (* ******************************************* *)
- (*
- The actual Module Filter that will transform the enum into a class
- dependencies:
- Should run before ReflectionCFs, in order to enable proper reflection access.
- Should run before TypeParams.RealTypeParams.RealTypeParamsModf, since generic enums must be first converted to generic classes
- It needs that the target platform implements __array__() as a shortcut to declare haxe.ds.Vector
- *)
- module EnumToClassModf =
- struct
- let name = "enum_to_class_mod"
- let priority = solve_deps name [DBefore ReflectionCFs.priority; DBefore TypeParams.RealTypeParams.RealTypeParamsModf.priority]
- let pmap_exists fn pmap = try PMap.iter (fun a b -> if fn a b then raise Exit) pmap; false with | Exit -> true
- let has_any_meta en =
- let has_meta meta = List.exists (fun (m,_,_) -> match m with Meta.Custom _ -> true | _ -> false) meta in
- has_meta en.e_meta || pmap_exists (fun _ ef -> has_meta ef.ef_meta) en.e_constrs
- let convert gen t base_class base_param_class en should_be_hxgen handle_type_params =
- let basic = gen.gcon.basic in
- let pos = en.e_pos in
- (* create the class *)
- let cl = mk_class en.e_module en.e_path pos in
- Hashtbl.add t.ec_tbl en.e_path cl;
- (match Codegen.build_metadata gen.gcon (TEnumDecl en) with
- | Some expr ->
- let cf = mk_class_field "__meta__" expr.etype false expr.epos (Var { v_read = AccNormal; v_write = AccNormal }) [] in
- cf.cf_expr <- Some expr;
- cl.cl_statics <- PMap.add "__meta__" cf cl.cl_statics;
- cl.cl_ordered_statics <- cf :: cl.cl_ordered_statics
- | _ -> ()
- );
- let super, has_params = if Meta.has Meta.FlatEnum en.e_meta then base_class, false else base_param_class, true in
- cl.cl_super <- Some(super,[]);
- cl.cl_extern <- en.e_extern;
- en.e_extern <- true;
- en.e_meta <- (Meta.Class, [], pos) :: en.e_meta;
- cl.cl_module <- en.e_module;
- cl.cl_meta <- ( Meta.Enum, [], pos ) :: cl.cl_meta;
- (match gen.gcon.platform with
- | Cs when Common.defined gen.gcon Define.CoreApiSerialize ->
- cl.cl_meta <- ( Meta.Meta, [ (EField( (EConst (Ident "System"), null_pos ), "Serializable" ), null_pos) ], null_pos ) :: cl.cl_meta
- | _ -> ());
- let c_types =
- if handle_type_params then
- List.map (fun (s,t) -> (s, TInst (map_param (get_cl_t t), []))) en.e_params
- else
- []
- in
- cl.cl_params <- c_types;
- let i = ref 0 in
- let cfs = List.map (fun name ->
- let ef = PMap.find name en.e_constrs in
- let pos = ef.ef_pos in
- let old_i = !i in
- incr i;
- let cf = match follow ef.ef_type with
- | TFun(params,ret) ->
- let dup_types =
- if handle_type_params then
- List.map (fun (s,t) -> (s, TInst (map_param (get_cl_t t), []))) en.e_params
- else
- []
- in
- let ef_type =
- let fn, types = if handle_type_params then snd, dup_types else (fun _ -> t_dynamic), en.e_params in
- let t = apply_params en.e_params (List.map fn types) ef.ef_type in
- apply_params ef.ef_params (List.map fn ef.ef_params) t
- in
- let params, ret = get_fun ef_type in
- let cf_params = if handle_type_params then dup_types @ ef.ef_params else [] in
- let cf = mk_class_field name ef_type true pos (Method MethNormal) cf_params in
- cf.cf_meta <- [];
- let tf_args = List.map (fun (name,opt,t) -> (alloc_var name t, if opt then Some TNull else None) ) params in
- let arr_decl = mk_nativearray_decl gen t_dynamic (List.map (fun (v,_) -> mk_local v pos) tf_args) pos in
- let expr = {
- eexpr = TFunction({
- tf_args = tf_args;
- tf_type = ret;
- tf_expr = mk_block ( mk_return { eexpr = TNew(cl,List.map snd dup_types, [mk_int gen old_i pos; arr_decl] ); etype = TInst(cl, List.map snd dup_types); epos = pos } );
- });
- etype = ef_type;
- epos = pos
- } in
- cf.cf_expr <- Some expr;
- cf
- | _ ->
- let actual_t = match follow ef.ef_type with
- | TEnum(e, p) -> TEnum(e, List.map (fun _ -> t_dynamic) p)
- | _ -> assert false
- in
- let cf = mk_class_field name actual_t true pos (Var { v_read = AccNormal; v_write = AccNever }) [] in
- let args = if has_params then
- [mk_int gen old_i pos; null (gen.gclasses.nativearray t_dynamic) pos]
- else
- [mk_int gen old_i pos]
- in
- cf.cf_meta <- [Meta.ReadOnly,[],pos];
- cf.cf_expr <- Some {
- eexpr = TNew(cl, List.map (fun _ -> t_empty) cl.cl_params, args);
- etype = TInst(cl, List.map (fun _ -> t_empty) cl.cl_params);
- epos = pos;
- };
- cf
- in
- cl.cl_statics <- PMap.add cf.cf_name cf cl.cl_statics;
- cf
- ) en.e_names in
- let constructs_cf = mk_class_field "__hx_constructs" (gen.gclasses.nativearray basic.tstring) true pos (Var { v_read = AccNormal; v_write = AccNever }) [] in
- constructs_cf.cf_meta <- [Meta.ReadOnly,[],pos];
- constructs_cf.cf_expr <- Some (mk_nativearray_decl gen basic.tstring (List.map (fun s -> { eexpr = TConst(TString s); etype = basic.tstring; epos = pos }) en.e_names) pos);
- cl.cl_ordered_statics <- constructs_cf :: cfs @ cl.cl_ordered_statics ;
- cl.cl_statics <- PMap.add "__hx_constructs" constructs_cf cl.cl_statics;
- let getTag_cf_type = tfun [] basic.tstring in
- let getTag_cf = mk_class_field "getTag" getTag_cf_type true pos (Method MethNormal) [] in
- getTag_cf.cf_meta <- [(Meta.Final, [], pos)];
- getTag_cf.cf_expr <- Some {
- eexpr = TFunction {
- tf_args = [];
- tf_type = basic.tstring;
- tf_expr = {
- eexpr = TReturn (Some (
- let e_constructs = mk_static_field_access_infer cl "__hx_constructs" pos [] in
- let e_this = mk (TConst TThis) (TInst (cl,[])) pos in
- let e_index = mk_field_access gen e_this "index" pos in
- {
- eexpr = TArray(e_constructs,e_index);
- etype = basic.tstring;
- epos = pos;
- }
- ));
- epos = pos;
- etype = basic.tvoid;
- }
- };
- etype = getTag_cf_type;
- epos = pos;
- };
- cl.cl_ordered_fields <- getTag_cf :: cl.cl_ordered_fields ;
- cl.cl_fields <- PMap.add "getTag" getTag_cf cl.cl_fields;
- cl.cl_overrides <- getTag_cf :: cl.cl_overrides;
- if should_be_hxgen then
- cl.cl_meta <- (Meta.HxGen,[],cl.cl_pos) :: cl.cl_meta
- else
- cl.cl_meta <- (Meta.NativeGen,[],cl.cl_pos) :: cl.cl_meta;
- gen.gadd_to_module (TClassDecl cl) (max_dep);
- TEnumDecl en
- (*
- traverse
- gen - gen context
- convert_all : bool - should we convert all enums? If set, convert_if_has_meta will be ignored.
- convert_if_has_meta : bool - should we convert only if it has meta?
- enum_base_class : tclass - the enum base class.
- should_be_hxgen : bool - should the created enum be hxgen?
- *)
- let traverse gen t convert_all convert_if_has_meta enum_base_class param_enum_class should_be_hxgen handle_tparams =
- let convert e = convert gen t enum_base_class param_enum_class e should_be_hxgen handle_tparams in
- let run md = match md with
- | TEnumDecl e when is_hxgen md ->
- if convert_all then
- convert e
- else if convert_if_has_meta && has_any_meta e then
- convert e
- else if not (Meta.has Meta.FlatEnum e.e_meta) then
- convert e
- else begin
- (* take off the :hxgen meta from it, if there's any *)
- e.e_meta <- List.filter (fun (n,_,_) -> not (n = Meta.HxGen)) e.e_meta;
- md
- end
- | _ -> md
- in
- run
- let configure gen (mapping_func:module_type->module_type) =
- let map md = Some(mapping_func md) in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* EnumToClassExprf *)
- (* ******************************************* *)
- (*
- Enum to class Expression Filter
- will convert TMatch into TSwitch
- dependencies:
- Should run before TArrayTransform, since it generates array access expressions
- *)
- module EnumToClassExprf =
- struct
- let name = "enum_to_class_exprf"
- let priority = solve_deps name [DBefore TArrayTransform.priority]
- let traverse gen t opt_get_native_enum_tag =
- let rec run e =
- let get_converted_enum_type et =
- let en, eparams = match follow (gen.gfollow#run_f et) with
- | TEnum(en,p) -> en, p
- | _ -> raise Not_found
- in
- let cl = Hashtbl.find t.ec_tbl en.e_path in
- TInst(cl, eparams)
- in
- match e.eexpr with
- | TCall (({eexpr = TField(_, FStatic({cl_path=[],"Type"},{cf_name="enumIndex"}))} as left), [f]) ->
- let f = run f in
- (try
- mk_field_access gen {f with etype = get_converted_enum_type f.etype} "index" e.epos
- with Not_found ->
- { e with eexpr = TCall(left, [f]) })
- | TEnumParameter(f, _,i) ->
- let f = run f in
- (* check if en was converted to class *)
- (* if it was, switch on tag field and change cond type *)
- let f = try
- { f with etype = get_converted_enum_type f.etype }
- with Not_found ->
- f
- in
- let cond_array = { (mk_field_access gen f "params" f.epos) with etype = gen.gclasses.nativearray t_dynamic } in
- { e with eexpr = TArray(cond_array, mk_int gen i cond_array.epos); }
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- let configure gen opt_get_native_enum_tag convert_all convert_if_has_meta enum_base_class param_enum_class should_be_hxgen handle_tparams =
- let t = new_t () in
- EnumToClassModf.configure gen (EnumToClassModf.traverse gen t convert_all convert_if_has_meta enum_base_class param_enum_class should_be_hxgen handle_tparams);
- EnumToClassExprf.configure gen (EnumToClassExprf.traverse gen t opt_get_native_enum_tag)
- end;;
- (* ******************************************* *)
- (* IteratorsInterface *)
- (* ******************************************* *)
- (*
- This module will handle with Iterators, Iterables and TFor() expressions.
- At first, a module filter will receive a Iterator<T> and Iterable<T> interface, which will be implemented
- if hasNext(), next() or iterator() fields are detected with the correct type.
- At this part a custom function will be called which can adequate the class fields so they are compatible with
- native Iterators as well
- The expression filter part of this module will look for TFor() expressions, and transform like that:
- for (anInt in value.iterator())
- {
- }
- {
- var s:haxe.lang.Iterator<Int> = ExternalFunction.getIterator(value.iterator());
- while (s.hasNext())
- {
- var anInt:Int = s.next();
- }
- }
- dependencies:
- None.
- *)
- module IteratorsInterface =
- struct
- let name = "iterators_interface"
- (* TODO later
- (* ******************************************* *)
- (* IteratorsInterfaceModf *)
- (* ******************************************* *)
- (*
- The module filter for Iterators Interface, which will implement the iterator/iterable interface on each
- class that conforms with the typedefs Iterator<> and Iterable<>
- It's a very simple module and it will rely on cast detection to work correctly. This is so that
- when the
- dependencies:
- Must run at the Module Filters, so cast detection can detect a cast to the interface and we can
- *)
- module IteratorsInterfaceModf =
- struct
- let name = "iterators_interface_modf"
- let conforms_cfs has_next next =
- try (match follow has_next.cf_type with
- | TFun([],ret) when
- (match follow ret with | TAbstract({ a_path = ([], "Bool") }, []) -> () | _ -> raise Not_found) ->
- ()
- | _ -> raise Not_found);
- (match follow next.cf_type with
- | TFun([], ret) -> ret
- | _ -> raise Not_found
- )
- let conforms_type_iterator t =
- try match follow t with
- | TInst(cl,params) ->
- let has_next = PMap.find "hasNext" cl.cl_fields in
- let next = PMap.find "next" cl.cl_fields in
- Some (conforms_cfs has_next next)
- | TAnon(anon) ->
- let has_next = PMap.find "hasNext" anon.a_fields in
- let next = PMap.find "next" anon.a_fields in
- Some (conforms_cfs has_next next)
- | _ -> None
- with | Not_found -> None
- let conforms_as_iterable cl =
- try
- let iterator = PMap.find "iterator" cl.cl_fields in
- match follow iterator.cf_type with
- | TFun([], ret) -> conforms_type_iterator ret
- | _ -> None
- with | Not_found -> None
- let conforms_as_iterator cl =
- try
- let has_next = PMap.find "hasNext" cl.cl_fields in
- let next = PMap.find "next" cl.cl_fields in
- Some (conforms_cfs has_next next)
- with | Not_found -> None
- let priority = solve_deps name []
- let traverse gen iterator_iface iterable_iface on_found_iterator on_found_iterable =
- let rec run md =
- match md with
- | TClassDecl cl when not cl.cl_extern && is_hxgen cl ->
- let conforms_iterator = conforms_as_iterator cl in
- let conforms_iterable = conforms_as_iterable cl in
- if is_some conforms_iterator then begin
- let it_t = get conforms_iterator in
- cl.cl_interfaces <- (iterator_iface, [it_t]);
- on_found_iterator cl
- end;
- if is_some conforms_iterable then begin
- let it_t = get conforms_iterable in
- cl.cl_interfaces <- (iterable_iface, [it_t]);
- on_found_iterable cl
- end;
- md
- | _ -> md
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- *)
- (* ******************************************* *)
- (* IteratorsInterfaceExprf *)
- (* ******************************************* *)
- (*
- The expression filter for Iterators. Will look for TFor, transform it into
- {
- var iterator = // in expression here
- while (iterator.hasNext())
- {
- var varName = iterator.next();
- }
- }
- dependencies:
- Must run before Dynamic fields access is run
- *)
- module IteratorsInterfaceExprf =
- struct
- let name = "iterators_interface_exprf"
- let priority = solve_deps name [DBefore DynamicFieldAccess.priority]
- let priority_as_synf = solve_deps name [DBefore DynamicFieldAccess.priority_as_synf]
- let mk_access gen v name pos =
- let field_t =
- try match follow v.v_type with
- | TInst(cl, params) ->
- let field = PMap.find name cl.cl_fields in
- apply_params cl.cl_params params field.cf_type
- | TAnon(anon) ->
- let field = PMap.find name anon.a_fields in
- field.cf_type
- | _ -> t_dynamic
- with | Not_found -> t_dynamic
- in
- { (mk_field_access gen (mk_local v pos) name pos) with etype = field_t }
- let traverse gen change_in_expr =
- let basic = gen.gcon.basic in
- let rec run e =
- match e.eexpr with
- | TFor(var, in_expr, block) ->
- let in_expr = change_in_expr (run in_expr) in
- let temp = mk_temp gen "iterator" in_expr.etype in
- let block =
- [
- { eexpr = TVar(temp, Some(in_expr)); etype = basic.tvoid; epos = in_expr.epos };
- {
- eexpr = TWhile(
- { eexpr = TCall(mk_access gen temp "hasNext" in_expr.epos, []); etype = basic.tbool; epos = in_expr.epos },
- Type.concat ({
- eexpr = TVar(var, Some({ eexpr = TCall(mk_access gen temp "next" in_expr.epos, []); etype = var.v_type; epos = in_expr.epos }));
- etype = basic.tvoid;
- epos = in_expr.epos
- }) ( run block ),
- Ast.NormalWhile);
- etype = basic.tvoid;
- epos = e.epos
- }
- ] in
- { eexpr = TBlock(block); etype = e.etype; epos = e.epos }
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map
- let configure_as_synf gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gexpr_filters#add ~name:name ~priority:(PCustom priority_as_synf) map
- end;;
- let configure gen change_in_expr =
- IteratorsInterfaceExprf.configure gen (IteratorsInterfaceExprf.traverse gen change_in_expr)
- let configure_as_synf gen change_in_expr =
- IteratorsInterfaceExprf.configure_as_synf gen (IteratorsInterfaceExprf.traverse gen change_in_expr)
- end;;
- (* ******************************************* *)
- (* SwitchToIf *)
- (* ******************************************* *)
- (*
- Just a syntax filter which changes switch expressions to if() else if() else if() ...
- It can be also an expression filter
- dependencies:
- *)
- module SwitchToIf =
- struct
- let name = "switch_to_if"
- let priority = solve_deps name []
- let rec simplify_expr e = match e.eexpr with
- | TParenthesis e
- | TMeta(_,e) -> simplify_expr e
- | _ -> e
- let traverse gen (should_convert:texpr->bool) (handle_nullables:bool) =
- let basic = gen.gcon.basic in
- let rec run e =
- match e.eexpr with
- | TSwitch(cond,cases,default) when should_convert e ->
- let cond_etype, should_cache = match handle_nullables, gen.gfollow#run_f cond.etype with
- | true, TType({ t_path = ([], "Null") }, [t]) ->
- let rec take_off_nullable t = match gen.gfollow#run_f t with
- | TType({ t_path = ([], "Null") }, [t]) -> take_off_nullable t
- | _ -> t
- in
- take_off_nullable t, true
- | _, _ -> cond.etype, false
- in
- if should_cache && not (should_convert { e with eexpr = TSwitch({ cond with etype = cond_etype }, cases, default) }) then begin
- { e with eexpr = TSwitch(mk_cast cond_etype (run cond), List.map (fun (cs,e) -> (List.map run cs, run e)) cases, Option.map run default) }
- end else begin
- let local, fst_block = match cond.eexpr, should_cache with
- | TLocal _, false -> cond, []
- | _ ->
- let var = mk_temp gen "switch" cond_etype in
- let cond = run cond in
- let cond = if should_cache then mk_cast cond_etype cond else cond in
- mk_local var cond.epos, [ { eexpr = TVar(var,Some(cond)); etype = basic.tvoid; epos = cond.epos } ]
- in
- let mk_eq cond =
- { eexpr = TBinop(Ast.OpEq, local, cond); etype = basic.tbool; epos = cond.epos }
- in
- let rec mk_many_cond conds =
- match conds with
- | cond :: [] ->
- mk_eq cond
- | cond :: tl ->
- { eexpr = TBinop(Ast.OpBoolOr, mk_eq (run cond), mk_many_cond tl); etype = basic.tbool; epos = cond.epos }
- | [] -> assert false
- in
- let mk_many_cond conds =
- let ret = mk_many_cond conds in
- (*
- this might be considered a hack. But since we're on a syntax filter and
- the condition is guaranteed to not have run twice, we can really run the
- expr filters again for it (so to change e.g. OpEq accordingly
- *)
- gen.gexpr_filters#run_f ret
- in
- let rec loop cases = match cases with
- | (conds,e) :: [] ->
- { eexpr = TIf(mk_many_cond conds, run e, Option.map run default); etype = e.etype; epos = e.epos }
- | (conds,e) :: tl ->
- { eexpr = TIf(mk_many_cond conds, run e, Some(loop tl)); etype = e.etype; epos = e.epos }
- | [] -> match default with
- | None ->
- raise Exit
- | Some d -> run d
- in
- try
- { e with eexpr = TBlock(fst_block @ [loop cases]) }
- with | Exit ->
- { e with eexpr = TBlock [] }
- end
- | TSwitch(cond,cases,default) -> (try
- match (simplify_expr cond).eexpr with
- | TCall( { eexpr = TField(_,FStatic({ cl_path = [],"Type" }, { cf_name = "enumIndex" })) }, [enum] ) ->
- let real_enum = match enum.etype with
- | TEnum(e,_) -> e
- | _ -> raise Not_found
- in
- if Meta.has Meta.Class real_enum.e_meta then raise Not_found;
- let enum_expr = mk_mt_access (TEnumDecl(real_enum)) e.epos in
- let fields = Hashtbl.create (List.length real_enum.e_names) in
- PMap.iter (fun _ ef -> Hashtbl.add fields ef.ef_index ef) real_enum.e_constrs;
- let cases = List.map (fun (el,e) ->
- List.map (fun e -> match e.eexpr with
- | TConst(TInt i) ->
- let ef = Hashtbl.find fields (Int32.to_int i) in
- { e with eexpr = TField(enum_expr, FEnum(real_enum,ef)); etype = TEnum(real_enum,List.map (fun _ -> t_dynamic) real_enum.e_params) }
- | _ -> raise Not_found) el, run e
- ) cases in
- { e with eexpr = TSwitch(enum,cases,Option.map run default) }
- | _ -> raise Not_found
- with Not_found -> Type.map_expr run e)
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* Anonymous Class object handling *)
- (* ******************************************* *)
- (*
- (syntax)
- When we pass a class as an object, in some languages we will need a special construct to be able to
- access its statics as if they were normal object fields. On C# and Java the way found to do that is
- by handling statics reflection also by a normal instance. This also happens in hxcpp and neko, so I
- guess it's a valid practice.
- So if we want to handle the reflection of the static MyClass, here's roughly how it will be done:
- var x = MyClass;
- gets converted into
- Haxe.Lang.Class x = Haxe.Lang.Runtime.GetType(typeof(MyClass).RuntimeHandle);
- which will in turn look in its cache but roughly would do:
- Haxe.Lang.Class x = new Haxe.Lang.Class(new MyClass(EmptyObject.EMPTY));
- This module will of course let the caller choose how this will be implemented. It will just identify all
- uses of class that will require it to be cast as an object.
- dependencies:
- *)
- module ClassInstance =
- struct
- let priority = solve_deps "class_instance" []
- let traverse gen (change_expr:texpr->module_type->texpr) =
- let rec run e =
- match e.eexpr with
- | TCall( ({ eexpr = TLocal({ v_name = ("__is__" | "__as__" | "__typeof__") } as v) } as local), calls ) when Hashtbl.mem gen.gspecial_vars v.v_name ->
- { e with eexpr = TCall(local, List.map (fun e ->
- match e.eexpr with
- | TTypeExpr _ -> e
- | _ -> run e) calls) }
- | TField({ eexpr = TTypeExpr(mt) }, f) ->
- e
- | TField(ef, f) ->
- (match anon_class ef.etype with
- | None -> Type.map_expr run e
- | Some t ->
- { e with eexpr = TField( { ef with eexpr = TTypeExpr(t) }, f) }
- )
- | TTypeExpr(mt) -> change_expr e mt
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:"class_instance" ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* HardNullableSynf *)
- (* ******************************************* *)
- (*
- This module will handle Null<T> types for languages that offer a way of dealing with
- stack-allocated structures or tuples and generics. Essentialy on those targets a Null<T>
- will be a tuple ( 'a * bool ), where bool is whether the value is null or not.
- At first (configure-time), we will modify the follow function so it can follow correctly nested Null<Null<T>>,
- and do not follow Null<T> to its underlying type
- Then we will run a syntax filter, which will look for casts to Null<T> and replace them by
- a call to the new Null<T> creation;
- Also casts from Null<T> to T or direct uses of Null<T> (call, field access, array access, closure)
- will result in the actual value being accessed
- For compatibility with the C# target, HardNullable will accept both Null<T> and haxe.lang.Null<T> types
- dependencies:
- Needs to be run after all cast detection modules
- *)
- module HardNullableSynf =
- struct
- let name = "hard_nullable"
- let priority = solve_deps name [DAfter CastDetect.ReturnCast.priority]
- let rec is_null_t gen t = match gen.greal_type t with
- | TType( { t_path = ([], "Null") }, [of_t])
- | TInst( { cl_path = (["haxe";"lang"], "Null") }, [of_t]) ->
- let rec take_off_null t =
- match is_null_t gen t with | None -> t | Some s -> take_off_null s
- in
- Some (take_off_null of_t)
- | TMono r -> (match !r with | Some t -> is_null_t gen t | None -> None)
- | TLazy f -> is_null_t gen (!f())
- | TType (t, tl) ->
- is_null_t gen (apply_params t.t_params tl t.t_type)
- | _ -> None
- let follow_addon gen t =
- let rec strip_off_nullable t =
- let t = gen.gfollow#run_f t in
- match t with
- (* haxe.lang.Null<haxe.lang.Null<>> wouldn't be a valid construct, so only follow Null<> *)
- | TType ( { t_path = ([], "Null") }, [of_t] ) -> strip_off_nullable of_t
- | _ -> t
- in
- match t with
- | TType( ({ t_path = ([], "Null") } as tdef), [of_t]) ->
- Some( TType(tdef, [ strip_off_nullable of_t ]) )
- | _ -> None
- let traverse gen unwrap_null wrap_val null_to_dynamic has_value opeq_handler handle_opeq handle_cast =
- (* let unwrap_null e = *)
- (* let ret = unwrap_null e in *)
- (* { ret with eexpr = TParenthesis(ret) } *)
- (* in *)
- (* let wrap_val e t b = *)
- (* let ret = wrap_val e t b in *)
- (* { ret with eexpr = TParenthesis(ret) } *)
- (* in *)
- let is_string t = match gen.greal_type t with
- | TInst({ cl_path=([],"String") },_) -> true
- | _ -> false
- in
- let handle_unwrap to_t e =
- let e_null_t = get (is_null_t gen e.etype) in
- match gen.greal_type to_t with
- | TDynamic _ | TMono _ | TAnon _ ->
- (match e_null_t with
- | TDynamic _ | TMono _ | TAnon _ ->
- gen.ghandle_cast to_t e_null_t (unwrap_null e)
- | _ -> null_to_dynamic e
- )
- | _ ->
- gen.ghandle_cast to_t e_null_t (unwrap_null e)
- in
- let handle_wrap e t =
- match e.eexpr with
- | TConst(TNull) ->
- wrap_val e t false
- | _ ->
- wrap_val e t true
- in
- let is_null_t = is_null_t gen in
- let cur_block = ref [] in
- let add_tmp v e p =
- cur_block := { eexpr = TVar(v,e); etype = gen.gcon.basic.tvoid; epos = p } :: !cur_block
- in
- let get_local e = match e.eexpr with
- | TLocal _ ->
- e, e
- | _ ->
- let v = mk_temp gen "nulltmp" e.etype in
- add_tmp v (Some (null e.etype e.epos)) e.epos;
- let local = { e with eexpr = TLocal(v) } in
- mk_paren { e with eexpr = TBinop(Ast.OpAssign, local, e) }, local
- in
- let rec run e =
- match e.eexpr with
- | TBlock(bl) ->
- let lst = !cur_block in
- cur_block := [];
- List.iter (fun e ->
- let e = run e in
- cur_block := (e :: !cur_block)
- ) bl;
- let ret = !cur_block in
- cur_block := lst;
- { e with eexpr = TBlock(List.rev ret) }
- | TCast(v, _) ->
- let null_et = is_null_t e.etype in
- let null_vt = is_null_t v.etype in
- (match null_vt, null_et with
- | Some(vt), None when is_string e.etype ->
- let v = run v in
- { e with eexpr = TCast(null_to_dynamic v,None) }
- | Some(vt), None ->
- (match v.eexpr with
- (* is there an unnecessary cast to Nullable? *)
- | TCast(v2, _) ->
- run { v with etype = e.etype }
- | _ ->
- handle_unwrap e.etype (run v)
- )
- | None, Some(et) ->
- handle_wrap (run v) et
- | Some(vt), Some(et) when handle_cast ->
- handle_wrap (gen.ghandle_cast et vt (handle_unwrap vt (run v))) et
- | Some(vt), Some(et) when not (type_iseq (run_follow gen vt) (run_follow gen et)) ->
- (* check if has value and convert *)
- let vlocal_fst, vlocal = get_local (run v) in
- {
- eexpr = TIf(
- has_value vlocal_fst,
- handle_wrap (mk_cast et (unwrap_null vlocal)) et,
- Some( handle_wrap (null et e.epos) et ));
- etype = e.etype;
- epos = e.epos
- }
- | _ ->
- Type.map_expr run e
- )
- | TField(ef, field) when is_some (is_null_t ef.etype) ->
- let to_t = get (is_null_t ef.etype) in
- { e with eexpr = TField(handle_unwrap to_t (run ef), field) }
- | TCall(ecall, params) when is_some (is_null_t ecall.etype) ->
- let to_t = get (is_null_t ecall.etype) in
- { e with eexpr = TCall(handle_unwrap to_t (run ecall), List.map run params) }
- | TArray(earray, p) when is_some (is_null_t earray.etype) ->
- let to_t = get (is_null_t earray.etype) in
- { e with eexpr = TArray(handle_unwrap to_t (run earray), p) }
- | TBinop(op, e1, e2) ->
- let e1_t = is_null_t e1.etype in
- let e2_t = is_null_t e2.etype in
- (match op with
- | Ast.OpAssign
- | Ast.OpAssignOp _ ->
- (match e1_t, e2_t with
- | Some t1, Some t2 ->
- (match op with
- | Ast.OpAssign ->
- Type.map_expr run e
- | Ast.OpAssignOp op ->
- (match e1.eexpr with
- | TLocal _ ->
- { e with eexpr = TBinop( Ast.OpAssign, e1, handle_wrap { e with eexpr = TBinop (op, handle_unwrap t1 e1, handle_unwrap t2 (run e2) ) } t1 ) }
- | _ ->
- let v, e1, evars = match e1.eexpr with
- | TField(ef, f) ->
- let v = mk_temp gen "nullbinop" ef.etype in
- v, { e1 with eexpr = TField(mk_local v ef.epos, f) }, ef
- | _ ->
- let v = mk_temp gen "nullbinop" e1.etype in
- v, mk_local v e1.epos, e1
- in
- { e with eexpr = TBlock([
- { eexpr = TVar(v, Some evars); etype = gen.gcon.basic.tvoid; epos = e.epos };
- { e with eexpr = TBinop( Ast.OpAssign, e1, handle_wrap { e with eexpr = TBinop (op, handle_unwrap t1 e1, handle_unwrap t2 (run e2) ) } t1 ) }
- ]) }
- )
- | _ -> assert false
- )
- | _ ->
- Type.map_expr run e (* casts are already dealt with normal CastDetection module *)
- )
- | Ast.OpEq | Ast.OpNotEq when not handle_opeq ->
- Type.map_expr run e
- | Ast.OpEq | Ast.OpNotEq ->
- (match e1.eexpr, e2.eexpr with
- | TConst(TNull), _ when is_some e2_t ->
- let e = has_value (run e2) in
- if op = Ast.OpEq then
- { e with eexpr = TUnop(Ast.Not, Ast.Prefix, e) }
- else
- e
- | _, TConst(TNull) when is_some e1_t ->
- let e = has_value (run e1) in
- if op = Ast.OpEq then
- { e with eexpr = TUnop(Ast.Not, Ast.Prefix, e) }
- else
- e
- | _ when is_some e1_t || is_some e2_t ->
- let e1, e2 =
- if not (is_some e1_t) then
- run e2, handle_wrap (run e1) (get e2_t)
- else if not (is_some e2_t) then
- run e1, handle_wrap (run e2) (get e1_t)
- else
- run e1, run e2
- in
- let e = opeq_handler e1 e2 in
- if op = Ast.OpEq then
- { e with eexpr = TUnop(Ast.Not, Ast.Prefix, e) }
- else
- e
- | _ ->
- Type.map_expr run e
- )
- | Ast.OpAdd when is_string e1.etype || is_string e2.etype ->
- let e1 = if is_some e1_t then
- null_to_dynamic (run e1)
- else
- run e1
- in
- let e2 = if is_some e2_t then
- null_to_dynamic (run e2)
- else
- run e2
- in
- let e_t = is_null_t e.etype in
- if is_some e_t then
- wrap_val { eexpr = TBinop(op,e1,e2); etype = get e_t; epos = e.epos } (get e_t) true
- else
- { e with eexpr = TBinop(op,e1,e2) }
- | _ ->
- let e1 = if is_some e1_t then
- handle_unwrap (get e1_t) (run e1)
- else run e1 in
- let e2 = if is_some e2_t then
- handle_unwrap (get e2_t) (run e2)
- else
- run e2 in
- (* if it is Null<T>, we need to convert the result again to null *)
- let e_t = (is_null_t e.etype) in
- if is_some e_t then
- wrap_val { eexpr = TBinop(op, e1, e2); etype = get e_t; epos = e.epos } (get e_t) true
- else
- { e with eexpr = TBinop(op, e1, e2) }
- )
- (*| TUnop( (Ast.Increment as op)*)
- | _ -> Type.map_expr run e
- in
- let run e = match e.eexpr with
- | TFunction tf ->
- run { e with eexpr = TFunction { tf with tf_expr = mk_block tf.tf_expr } }
- | TBlock _ ->
- run e
- | _ -> match run (mk_block e) with
- | { eexpr = TBlock([e]) } -> e
- | e -> e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- gen.gfollow#add ~name:(name ^ "_follow") (follow_addon gen);
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* ArrayDeclSynf *)
- (* ******************************************* *)
- (*
- A syntax filter that will change array declarations to the actual native array declarations plus
- the haxe array initialization
- dependencies:
- Must run after ObjectDeclMap since it can add TArrayDecl expressions
- *)
- module ArrayDeclSynf =
- struct
- let name = "array_decl_synf"
- let priority = solve_deps name [DAfter ObjectDeclMap.priority]
- let default_implementation gen native_array_cl =
- let rec run e =
- match e.eexpr with
- | TArrayDecl el ->
- let cl, params = match follow e.etype with
- | TInst(({ cl_path = ([], "Array") } as cl), ( _ :: _ as params)) -> cl, params
- | TInst(({ cl_path = ([], "Array") } as cl), []) -> cl, [t_dynamic]
- | _ -> assert false
- in
- let changed_params = gen.greal_type_param (TClassDecl cl) params in
- { e with eexpr = TNew(cl, changed_params, [ { e with eexpr = TArrayDecl(List.map run el); etype = TInst(native_array_cl, changed_params) } ] ); }
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* SwitchBreakSynf *)
- (* ******************************************* *)
- (*
- In most languages, 'break' is used as a statement also to break from switch statements.
- This generates an incompatibility with haxe code, as we can use break to break from loops from inside a switch
- This script will detect 'breaks' inside switch statements, and will offer the opportunity to change both
- when this pattern is found.
- Some options are possible:
- On languages that support goto, 'break' may mean goto " after the loop ". There also can be special labels for
- loops, so you can write "break label" (javascript, java, d)
- On languages that do not support goto, a custom solution must be enforced
- dependencies:
- Since UnreachableCodeElimination must run before it, and Unreachable should be one of the
- very last filters to run, we will make a fixed value which runs after UnreachableCodeElimination
- (meaning: it's the very last filter)
- *)
- module SwitchBreakSynf =
- struct
- let name = "switch_break_synf"
- let priority = min_dep -. 150.0
- type add_to_block_api = texpr->bool->unit
- let traverse gen (change_loop:texpr->int->add_to_block_api->texpr) (change_break:texpr->int->add_to_block_api->texpr) =
- let in_switch = ref false in
- let cur_block = ref [] in
- let to_add = ref [] in
- let did_found = ref (-1) in
- let api expr before =
- if before then cur_block := expr :: !cur_block else to_add := expr :: !to_add
- in
- let num = ref 0 in
- let cur_num = ref 0 in
- let rec run e =
- match e.eexpr with
- | TFunction _ ->
- let old_num = !num in
- num := 0;
- let ret = Type.map_expr run e in
- num := old_num;
- ret
- | TFor _
- | TWhile _ ->
- let last_switch = !in_switch in
- let last_found = !did_found in
- let last_num = !cur_num in
- in_switch := false;
- incr num;
- cur_num := !num;
- did_found := -1;
- let new_e = Type.map_expr run e in (* assuming that no loop will be found in the condition *)
- let new_e = if !did_found <> -1 then change_loop new_e !did_found api else new_e in
- did_found := last_found;
- in_switch := last_switch;
- cur_num := last_num;
- new_e
- | TSwitch _ ->
- let last_switch = !in_switch in
- in_switch := true;
- let new_e = Type.map_expr run e in
- in_switch := last_switch;
- new_e
- | TBlock bl ->
- let last_block = !cur_block in
- let last_toadd = !to_add in
- to_add := [];
- cur_block := [];
- List.iter (fun e ->
- let new_e = run e in
- cur_block := new_e :: !cur_block;
- match !to_add with
- | [] -> ()
- | _ -> cur_block := !to_add @ !cur_block; to_add := []
- ) bl;
- let ret = List.rev !cur_block in
- cur_block := last_block;
- to_add := last_toadd;
- { e with eexpr = TBlock(ret) }
- | TBreak ->
- if !in_switch then (did_found := !cur_num; change_break e !cur_num api) else e
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* Unreachable Code Elimination *)
- (* ******************************************* *)
- (*
- In some source code platforms, the code won't compile if there is Unreachable code, so this filter will take off any unreachable code.
- If the parameter "handle_switch_break" is set to true, it will already add a "break" statement on switch cases when suitable;
- in order to not confuse with while break, it will be a special expression __sbreak__
- If the parameter "handle_not_final_returns" is set to true, it will also add final returns when functions are detected to be lacking of them.
- (Will respect __fallback__ expressions)
- If the parameter "java_mode" is set to true, some additional checks following the java unreachable specs
- (http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21) will be added
- dependencies:
- This must run before SwitchBreakSynf (see SwitchBreakSynf dependecy value)
- This must be the LAST syntax filter to run. It expects ExpressionUnwrap to have run correctly, since this will only work for source-code based targets
- *)
- module UnreachableCodeEliminationSynf =
- struct
- let name = "unreachable_synf"
- let priority = min_dep -. 100.0
- type uexpr_kind =
- | Normal
- | BreaksLoop
- | BreaksFunction
- let aggregate_kind e1 e2 =
- match e1, e2 with
- | Normal, _
- | _, Normal -> Normal
- | BreaksLoop, _
- | _, BreaksLoop -> BreaksLoop
- | BreaksFunction, BreaksFunction -> BreaksFunction
- let aggregate_constant op c1 c2=
- match op, c1, c2 with
- | OpEq, Some v1, Some v2 -> Some (TBool (v1 = v2))
- | OpNotEq, Some v1, Some v2 -> Some (TBool (v1 <> v2))
- | OpBoolOr, Some (TBool v1) , Some (TBool v2) -> Some (TBool (v1 || v2))
- | OpBoolAnd, Some (TBool v1) , Some (TBool v2) -> Some (TBool (v1 && v2))
- | OpAssign, _, Some v2 -> Some v2
- | _ -> None
- let rec get_constant_expr e =
- match e.eexpr with
- | TConst (v) -> Some v
- | TBinop(op, v1, v2) -> aggregate_constant op (get_constant_expr v1) (get_constant_expr v2)
- | TParenthesis(e) | TMeta(_,e) -> get_constant_expr e
- | _ -> None
- let traverse gen should_warn handle_switch_break handle_not_final_returns java_mode =
- let basic = gen.gcon.basic in
- let do_warn =
- if should_warn then gen.gcon.warning "Unreachable code" else (fun pos -> ())
- in
- let return_loop expr kind =
- match kind with
- | Normal | BreaksLoop -> expr, Normal
- | _ -> expr, kind
- in
- let sbreak = alloc_var "__sbreak__" t_dynamic in
- let mk_sbreak = mk_local sbreak in
- let rec has_fallback expr = match expr.eexpr with
- | TBlock(bl) -> (match List.rev bl with
- | { eexpr = TLocal { v_name = "__fallback__" } } :: _ -> true
- | ({ eexpr = TBlock(_) } as bl) :: _ -> has_fallback bl
- | _ -> false)
- | TLocal { v_name = "__fallback__" } -> true
- | _ -> false
- in
- let handle_case = if handle_switch_break then
- (fun (expr,kind) ->
- match kind with
- | Normal when has_fallback expr -> expr
- | Normal -> Type.concat expr (mk_sbreak expr.epos)
- | BreaksLoop | BreaksFunction -> expr
- )
- else
- fst
- in
- let has_break = ref false in
- let rec process_expr expr =
- match expr.eexpr with
- | TReturn _ | TThrow _ -> expr, BreaksFunction
- | TContinue -> expr, BreaksLoop
- | TBreak -> has_break := true; expr, BreaksLoop
- | TCall( { eexpr = TLocal { v_name = "__goto__" } }, _ ) -> expr, BreaksLoop
- | TBlock bl ->
- let new_block = ref [] in
- let is_unreachable = ref false in
- let ret_kind = ref Normal in
- List.iter (fun e ->
- if !is_unreachable then
- do_warn e.epos
- else begin
- let changed_e, kind = process_expr e in
- new_block := changed_e :: !new_block;
- match kind with
- | BreaksLoop | BreaksFunction ->
- ret_kind := kind;
- is_unreachable := true
- | _ -> ()
- end
- ) bl;
- { expr with eexpr = TBlock(List.rev !new_block) }, !ret_kind
- | TFunction tf ->
- let changed, kind = process_expr tf.tf_expr in
- let changed = if handle_not_final_returns && not (is_void tf.tf_type) && kind <> BreaksFunction then
- Type.concat changed { eexpr = TReturn( Some (null tf.tf_type expr.epos) ); etype = basic.tvoid; epos = expr.epos }
- else
- changed
- in
- { expr with eexpr = TFunction({ tf with tf_expr = changed }) }, Normal
- | TFor(var, cond, block) ->
- let last_has_break = !has_break in
- has_break := false;
- let changed_block, _ = process_expr block in
- has_break := last_has_break;
- let expr = { expr with eexpr = TFor(var, cond, changed_block) } in
- return_loop expr Normal
- | TIf(cond, eif, None) ->
- if java_mode then
- match get_constant_expr cond with
- | Some (TBool true) ->
- process_expr eif
- | _ ->
- { expr with eexpr = TIf(cond, fst (process_expr eif), None) }, Normal
- else
- { expr with eexpr = TIf(cond, fst (process_expr eif), None) }, Normal
- | TIf(cond, eif, Some eelse) ->
- let eif, eif_k = process_expr eif in
- let eelse, eelse_k = process_expr eelse in
- let k = aggregate_kind eif_k eelse_k in
- { expr with eexpr = TIf(cond, eif, Some eelse) }, k
- | TWhile(cond, block, flag) ->
- let last_has_break = !has_break in
- has_break := false;
- let block, k = process_expr block in
- if java_mode then
- match get_constant_expr cond, flag, !has_break with
- | Some (TBool true), _, false ->
- has_break := last_has_break;
- { expr with eexpr = TWhile(cond, block, flag) }, BreaksFunction
- | Some (TBool false), NormalWhile, _ ->
- has_break := last_has_break;
- do_warn expr.epos;
- null expr.etype expr.epos, Normal
- | _ ->
- has_break := last_has_break;
- return_loop { expr with eexpr = TWhile(cond,block,flag) } Normal
- else begin
- has_break := last_has_break;
- return_loop { expr with eexpr = TWhile(cond,block,flag) } Normal
- end
- | TSwitch(cond, el_e_l, None) ->
- { expr with eexpr = TSwitch(cond, List.map (fun (el, e) -> (el, handle_case (process_expr e))) el_e_l, None) }, Normal
- | TSwitch(cond, el_e_l, Some def) ->
- let def, k = process_expr def in
- let def = handle_case (def, k) in
- let k = ref k in
- let ret = { expr with eexpr = TSwitch(cond, List.map (fun (el, e) ->
- let e, ek = process_expr e in
- k := aggregate_kind !k ek;
- (el, handle_case (e, ek))
- ) el_e_l, Some def) } in
- ret, !k
- (* | TMatch(cond, ep, il_vopt_e_l, None) ->
- { expr with eexpr = TMatch(cond, ep, List.map (fun (il, vopt, e) -> (il, vopt, handle_case (process_expr e))) il_vopt_e_l, None) }, Normal *)
- (* | TMatch(cond, ep, il_vopt_e_l, Some def) ->
- let def, k = process_expr def in
- let def = handle_case (def, k) in
- let k = ref k in
- let ret = { expr with eexpr = TMatch(cond, ep, List.map (fun (il, vopt, e) ->
- let e, ek = process_expr e in
- k := aggregate_kind !k ek;
- (il, vopt, handle_case (e, ek))
- ) il_vopt_e_l, Some def) } in
- ret, !k *)
- | TTry (e, catches) ->
- let e, k = process_expr e in
- let k = ref k in
- let ret = { expr with eexpr = TTry(e, List.map (fun (v, e) ->
- let e, ek = process_expr e in
- k := aggregate_kind !k ek;
- (v, e)
- ) catches) } in
- ret, !k
- | _ -> expr, Normal
- in
- let run e = fst (process_expr e) in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* DefaultArguments *)
- (* ******************************************* *)
- (*
- This Module Filter will go through all defined functions in all modules and change them
- so they set all default arguments to be of a Nullable type, and adds the unroll from nullable to
- the not-nullable type in the beginning of the function.
- dependencies:
- It must run before OverloadingCtors, since OverloadingCtors will change optional structures behavior
- *)
- module DefaultArguments =
- struct
- let name = "default_arguments"
- let priority = solve_deps name [ DBefore OverloadingConstructor.priority ]
- let gen_check basic t nullable_var const pos =
- let is_null t = match t with TType({t_path = ([],"Null")}, _) -> true | _ -> false in
- let needs_cast t1 t2 = match is_null t1, is_null t2 with
- | true, false | false, true -> true
- | _ -> false
- in
- let const_t = match const with
- | TString _ -> basic.tstring | TInt _ -> basic.tint | TFloat _ -> basic.tfloat
- | TNull -> t | TBool _ -> basic.tbool | _ -> assert false
- in
- let const = { eexpr = TConst(const); etype = const_t; epos = pos } in
- let const = if needs_cast t const_t then mk_cast t const else const in
- let arg = mk_local nullable_var pos in
- let arg = if needs_cast t nullable_var.v_type then mk_cast t arg else arg in
- {
- eexpr = TIf(
- { eexpr = TBinop(Ast.OpEq, mk_local nullable_var pos, null nullable_var.v_type pos); etype = basic.tbool; epos = pos },
- const,
- Some(arg)
- );
- etype = t;
- epos = pos;
- }
- let add_opt gen block pos (var,opt) =
- match opt with
- | None | Some TNull -> (var,opt)
- | Some (TString str) ->
- block := Codegen.set_default gen.gcon var (TString str) pos :: !block;
- (var, opt)
- | Some const ->
- let basic = gen.gcon.basic in
- let nullable_var = mk_temp gen var.v_name (basic.tnull var.v_type) in
- let orig_name = var.v_name in
- var.v_name <- nullable_var.v_name;
- nullable_var.v_name <- orig_name;
- (* var v = (temp_var == null) ? const : cast temp_var; *)
- block :=
- {
- eexpr = TVar(var, Some(gen_check basic var.v_type nullable_var const pos));
- etype = basic.tvoid;
- epos = pos;
- } :: !block;
- (nullable_var, opt)
- let rec change_func gen cf =
- List.iter (change_func gen) cf.cf_overloads;
- let is_ctor = cf.cf_name = "new" in
- let basic = gen.gcon.basic in
- match cf.cf_kind, follow cf.cf_type with
- | Var _, _ | Method MethDynamic, _ -> ()
- | _, TFun(args, ret) ->
- let found = ref false in
- let args = ref (List.map (fun (n,opt,t) ->
- (n,opt, if opt then (found := true; basic.tnull t) else t)
- ) args) in
- (match !found, cf.cf_expr with
- | true, Some ({ eexpr = TFunction tf } as texpr) ->
- let block = ref [] in
- let tf_args = List.map (add_opt gen block tf.tf_expr.epos) tf.tf_args in
- let arg_assoc = List.map2 (fun (v,o) (v2,_) -> v,(v2,o) ) tf.tf_args tf_args in
- let rec extract_super e = match e.eexpr with
- | TBlock(({ eexpr = TCall({ eexpr = TConst TSuper }, _) } as e2) :: tl) ->
- e2, tl
- | TBlock(hd :: tl) ->
- let e2, tl2 = extract_super hd in
- e2, tl2 @ tl
- | _ -> raise Not_found
- in
- let block = try
- if not is_ctor then raise Not_found;
- (* issue #2570 *)
- (* check if the class really needs the super as the first statement -
- just to make sure we don't inadvertently break any existing code *)
- let rec check cl =
- if not (is_hxgen (TClassDecl cl)) then
- ()
- else match cl.cl_super with
- | None ->
- raise Not_found
- | Some (cl,_) ->
- check cl
- in
- (match gen.gcurrent_class with
- | Some cl -> check cl
- | _ -> ());
- let super, tl = extract_super tf.tf_expr in
- (match super.eexpr with
- | TCall({ eexpr = TConst TSuper } as e1, args) ->
- (* any super argument will be replaced by an inlined version of the check *)
- let found = ref false in
- let rec replace_args e = match e.eexpr with
- | TLocal(v) -> (try
- let v2,o = List.assq v arg_assoc in
- let o = match o with
- | None -> raise Not_found
- | Some o -> o
- in
- found := true;
- gen_check gen.gcon.basic v.v_type v2 o e.epos
- with | Not_found -> e)
- | _ -> Type.map_expr replace_args e
- in
- let args = List.map (replace_args) args in
- { tf.tf_expr with eexpr = TBlock((if !found then { super with eexpr = TCall(e1,args) } else super) :: !block @ tl) }
- | _ -> assert false)
- with | Not_found ->
- Type.concat { tf.tf_expr with eexpr = TBlock(!block); etype = basic.tvoid } tf.tf_expr
- in
- args := fun_args tf_args;
- cf.cf_expr <- Some( {texpr with eexpr = TFunction( { tf with
- tf_args = tf_args;
- tf_expr = block
- } ); etype = TFun(!args, ret) } );
- cf.cf_type <- TFun(!args, ret)
- | _ -> ()
- );
- (if !found then cf.cf_type <- TFun(!args, ret))
- | _, _ -> assert false
- let traverse gen =
- let run md = match md with
- | TClassDecl cl ->
- List.iter (change_func gen) cl.cl_ordered_fields;
- List.iter (change_func gen) cl.cl_ordered_statics;
- (match cl.cl_constructor with | None -> () | Some cf -> change_func gen cf);
- md
- | _ -> md
- in
- run
- let configure gen (mapping_func:module_type->module_type) =
- let map md = Some(mapping_func md) in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* Interface Variables Removal Modf *)
- (* ******************************************* *)
- (*
- This module filter will take care of sanitizing interfaces for targets that do not support
- variables declaration in interfaces. By now this will mean that if anything is typed as the interface,
- and a variable access is made, a FNotFound will be returned for the field_access, so
- the field will be only accessible by reflection.
- Speed-wise, ideally it would be best to create getProp/setProp functions in this case and change
- the AST to call them when accessing by interface. (TODO)
- But right now it will be accessed by reflection.
- dependencies:
- *)
- module InterfaceVarsDeleteModf =
- struct
- let name = "interface_vars"
- let priority = solve_deps name []
- let run gen =
- let run md = match md with
- | TClassDecl ( { cl_interface = true } as cl ) ->
- let to_add = ref [] in
- let fields = List.filter (fun cf ->
- match cf.cf_kind with
- | Var _ when gen.gcon.platform = Cs && Meta.has Meta.Event cf.cf_meta ->
- true
- | Var vkind when not (Type.is_extern_field cf && Meta.has Meta.Property cf.cf_meta) ->
- (match vkind.v_read with
- | AccCall ->
- let newcf = mk_class_field ("get_" ^ cf.cf_name) (TFun([],cf.cf_type)) true cf.cf_pos (Method MethNormal) [] in
- to_add := newcf :: !to_add;
- | _ -> ()
- );
- (match vkind.v_write with
- | AccCall ->
- let newcf = mk_class_field ("set_" ^ cf.cf_name) (TFun(["val",false,cf.cf_type],cf.cf_type)) true cf.cf_pos (Method MethNormal) [] in
- to_add := newcf :: !to_add;
- | _ -> ()
- );
- cl.cl_fields <- PMap.remove cf.cf_name cl.cl_fields;
- false
- | Method MethDynamic ->
- (* TODO OPTIMIZATION - add a `_dispatch` method to the interface which will call the dynamic function itself *)
- cl.cl_fields <- PMap.remove cf.cf_name cl.cl_fields;
- false
- | _ -> true
- ) cl.cl_ordered_fields in
- cl.cl_ordered_fields <- fields;
- List.iter (fun cf ->
- match field_access gen (TInst(cl,List.map snd cl.cl_params)) cf.cf_name with
- | FNotFound | FDynamicField _ ->
- cl.cl_ordered_fields <- cf :: cl.cl_ordered_fields;
- cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields
- | _ -> ()
- ) !to_add;
- md
- | _ -> md
- in
- run
- let configure gen =
- let run = run gen in
- let map md = Some(run md) in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* InterfaceProps *)
- (* ******************************************* *)
- (*
- This module filter will go through all declared properties, and see if they are conforming to a native interface.
- If they are, it will add Meta.Property to it
- dependencies:
- *)
- module InterfaceProps =
- struct
- let name = "interface_props"
- let priority = solve_deps name []
- let run gen =
- let run md = match md with
- | TClassDecl ( { cl_interface = false; cl_extern = false } as cl ) ->
- let vars = List.fold_left (fun acc (iface,_) ->
- if Meta.has Meta.CsNative iface.cl_meta then
- List.filter (fun cf -> match cf.cf_kind with
- | Var { v_read = AccCall } | Var { v_write = AccCall } ->
- true
- | _ -> false
- ) iface.cl_ordered_fields @ acc
- else
- acc
- ) [] cl.cl_implements in
- let vars = List.map (fun cf -> cf.cf_name) vars in
- if vars <> [] then
- List.iter (fun cf -> match cf.cf_kind with
- | Var { v_read = AccCall } | Var { v_write = AccCall } when List.mem cf.cf_name vars ->
- cf.cf_meta <- (Meta.Property, [], Ast.null_pos) :: cf.cf_meta
- | _ -> ()
- ) cl.cl_ordered_fields;
- md
- | _ -> md
- in
- run
- let configure gen =
- let run = run gen in
- let map md = Some(run md) in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* Int Division Synf *)
- (* ******************************************* *)
- (*
- On targets that support int division, this module will force a float division to be performed,
- so compatibility with current haxe targets is ensured.
- If catch_int_div is set to true, though, it will look for casts to int or use of Std.int() to optimize
- this kind of operation.
- dependencies:
- since it depends on nothing, but many modules might generate division expressions,
- it will be one of the last modules to run
- *)
- module IntDivisionSynf =
- struct
- let name = "int_division_synf"
- let priority = solve_deps name [ DAfter ExpressionUnwrap.priority; DAfter ObjectDeclMap.priority; DAfter ArrayDeclSynf.priority ]
- let is_int = like_int
- let is_exactly_int t = match follow t with
- | TAbstract ({ a_path=[],"Int" }, []) -> true
- | _ -> false
- let default_implementation gen catch_int_div =
- let basic = gen.gcon.basic in
- let rec run e =
- match e.eexpr with
- | TBinop((Ast.OpDiv as op), e1, e2) when is_int e1.etype && is_int e2.etype ->
- { e with eexpr = TBinop(op, mk_cast basic.tfloat (run e1), run e2) }
- | TCall(
- { eexpr = TField(_, FStatic({ cl_path = ([], "Std") }, { cf_name = "int" })) },
- [ ({ eexpr = TBinop((Ast.OpDiv as op), e1, e2) } as ebinop ) ]
- ) when catch_int_div && is_int e1.etype && is_int e2.etype ->
- let e = { ebinop with eexpr = TBinop(op, run e1, run e2); etype = basic.tint } in
- if not (is_exactly_int e1.etype && is_exactly_int e2.etype) then
- mk_cast basic.tint e
- else
- e
- | TCast( ({ eexpr = TBinop((Ast.OpDiv as op), e1, e2) } as ebinop ), _ )
- | TCast( ({ eexpr = TBinop(( (Ast.OpAssignOp Ast.OpDiv) as op), e1, e2) } as ebinop ), _ ) when catch_int_div && is_int e1.etype && is_int e2.etype && is_int e.etype ->
- let ret = { ebinop with eexpr = TBinop(op, run e1, run e2); etype = e.etype } in
- if not (is_exactly_int e1.etype && is_exactly_int e2.etype) then
- mk_cast e.etype ret
- else
- e
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* UnnecessaryCastsRemoval *)
- (* ******************************************* *)
- (*
- This module will take care of simplifying unnecessary casts, specially those made by the compiler
- when inlining. Right now, it will only take care of casts used as a statement, which are always useless;
- TODO: Take care of more cases, e.g. when the to and from types are the same
- dependencies:
- This must run after CastDetection, but before ExpressionUnwrap
- *)
- module UnnecessaryCastsRemoval =
- struct
- let name = "casts_removal"
- let priority = solve_deps name [DAfter CastDetect.priority; DBefore ExpressionUnwrap.priority]
- let rec take_off_cast run e =
- match e.eexpr with
- | TCast (c, _) ->
- take_off_cast run c
- | _ -> run e
- let default_implementation gen =
- let rec traverse e =
- match e.eexpr with
- | TBlock bl ->
- let bl = List.map (fun e ->
- take_off_cast traverse e
- ) bl in
- { e with eexpr = TBlock bl }
- | TTry (block, catches) ->
- { e with eexpr = TTry(traverse (mk_block block), List.map (fun (v,block) -> (v, traverse (mk_block block))) catches) }
- (* | TMatch (cond,ep,il_vol_e_l,default) ->
- { e with eexpr = TMatch(cond,ep,List.map (fun (il,vol,e) -> (il,vol,traverse (mk_block e))) il_vol_e_l, Option.map (fun e -> traverse (mk_block e)) default) } *)
- | TSwitch (cond,el_e_l, default) ->
- { e with eexpr = TSwitch(cond, List.map (fun (el,e) -> (el, traverse (mk_block e))) el_e_l, Option.map (fun e -> traverse (mk_block e)) default) }
- | TWhile (cond,block,flag) ->
- {e with eexpr = TWhile(cond,traverse (mk_block block), flag) }
- | TIf (cond, eif, eelse) ->
- { e with eexpr = TIf(cond, traverse (mk_block eif), Option.map (fun e -> traverse (mk_block e)) eelse) }
- | TFor (v,it,block) ->
- { e with eexpr = TFor(v,it, traverse (mk_block block)) }
- | TFunction (tfunc) ->
- { e with eexpr = TFunction({ tfunc with tf_expr = traverse (mk_block tfunc.tf_expr) }) }
- | _ -> e (* if expression doesn't have a block, we will exit *)
- in
- traverse
- let configure gen =
- let map e = Some(default_implementation gen e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* OverrideFix *)
- (* ******************************************* *)
- (*
- When DCE is on, sometimes a field is marked as override when it
- really doesn't override anything. This module filter will take care of this.
- dependencies:
- No dependencies
- *)
- module OverrideFix =
- struct
- let name = "override_fix"
- let priority = solve_deps name []
- let default_implementation gen =
- let rec run e =
- match e.eexpr with
- | _ -> Type.map_expr run e
- in
- run
- let configure gen =
- let map md =
- match md with
- | TClassDecl cl ->
- cl.cl_overrides <- List.filter (fun s ->
- let rec loop cl =
- match cl.cl_super with
- | Some (cl,_) when PMap.mem s.cf_name cl.cl_fields -> true
- | Some (cl,_) -> loop cl
- | None -> false
- in
- loop cl
- ) cl.cl_overrides;
- Some md
- | _ -> Some md
- in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* AbstractImplementationFix *)
- (* ******************************************* *)
- (*
- This module filter will map the compiler created classes from abstract
- implementations to valid haxe code, as needed by gencommon
- dependencies:
- No dependencies
- *)
- module AbstractImplementationFix =
- struct
- let name = "abstract_implementation_fix"
- let priority = solve_deps name []
- let default_implementation gen =
- let rec run md =
- match md with
- | TClassDecl ({ cl_kind = KAbstractImpl a } as c) ->
- List.iter (function
- | cf when Meta.has Meta.Impl cf.cf_meta ->
- (* add type parameters to all implementation functions *)
- cf.cf_params <- cf.cf_params @ a.a_params
- | _ -> ()
- ) c.cl_ordered_statics;
- Some md
- | _ -> Some md
- in
- run
- let configure gen =
- let map = default_implementation gen in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* FixOverrides *)
- (* ******************************************* *)
- (*
- Covariant return types, contravariant function arguments and applied type parameters may change
- in a way that expected implementations / overrides aren't recognized as such.
- This filter will fix that.
- dependencies:
- FixOverrides expects that the target platform is able to deal with overloaded functions
- It must run after DefaultArguments, otherwise code added by the default arguments may be invalid
- *)
- module FixOverrides =
- struct
- let name = "fix_overrides"
- let priority = solve_deps name [DAfter DefaultArguments.priority]
- (*
- if the platform allows explicit interface implementation (C#),
- specify a explicit_fn_name function (tclass->string->string)
- Otherwise, it expects the platform to be able to handle covariant return types
- *)
- let run ~explicit_fn_name gen =
- let implement_explicitly = is_some explicit_fn_name in
- let run md = match md with
- | TClassDecl ( { cl_interface = true; cl_extern = false } as c ) ->
- (* overrides can be removed from interfaces *)
- c.cl_ordered_fields <- List.filter (fun f ->
- try
- if Meta.has Meta.Overload f.cf_meta then raise Not_found;
- let f2 = Codegen.find_field gen.gcon c f in
- if f2 == f then raise Not_found;
- c.cl_fields <- PMap.remove f.cf_name c.cl_fields;
- false;
- with Not_found ->
- true
- ) c.cl_ordered_fields;
- md
- | TClassDecl({ cl_extern = false } as c) ->
- let this = { eexpr = TConst TThis; etype = TInst(c,List.map snd c.cl_params); epos = c.cl_pos } in
- (* look through all interfaces, and try to find a type that applies exactly *)
- let rec loop_iface (iface:tclass) itl =
- List.iter (fun (s,stl) -> loop_iface s (List.map (apply_params iface.cl_params itl) stl)) iface.cl_implements;
- let real_itl = gen.greal_type_param (TClassDecl iface) itl in
- let rec loop_f f =
- List.iter loop_f f.cf_overloads;
- let ftype = apply_params iface.cl_params itl f.cf_type in
- let real_ftype = get_real_fun gen (apply_params iface.cl_params real_itl f.cf_type) in
- replace_mono real_ftype;
- let overloads = Typeload.get_overloads c f.cf_name in
- try
- let t2, f2 =
- match overloads with
- | (_, cf) :: _ when Meta.has Meta.Overload cf.cf_meta -> (* overloaded function *)
- (* try to find exact function *)
- List.find (fun (t,f2) ->
- Typeload.same_overload_args ftype t f f2
- ) overloads
- | _ :: _ ->
- (match field_access gen (TInst(c, List.map snd c.cl_params)) f.cf_name with
- | FClassField(_,_,_,f2,false,t,_) -> t,f2 (* if it's not an overload, all functions should have the same signature *)
- | _ -> raise Not_found)
- | [] -> raise Not_found
- in
- replace_mono t2;
- (* if we find a function with the exact type of real_ftype, it means this interface has already been taken care of *)
- if not (type_iseq (get_real_fun gen (apply_params f2.cf_params (List.map snd f.cf_params) t2)) real_ftype) then begin
- (match f.cf_kind with | Method (MethNormal | MethInline) -> () | _ -> raise Not_found);
- let t2 = get_real_fun gen t2 in
- if List.length f.cf_params <> List.length f2.cf_params then raise Not_found;
- replace_mono t2;
- match follow (apply_params f2.cf_params (List.map snd f.cf_params) t2), follow real_ftype with
- | TFun(a1,r1), TFun(a2,r2) when not implement_explicitly && not (type_iseq r1 r2) && Typeload.same_overload_args real_ftype t2 f f2 ->
- (* different return types are the trickiest cases to deal with *)
- (* check for covariant return type *)
- let is_covariant = match follow r1, follow r2 with
- | _, TDynamic _ -> true
- | r1, r2 -> try
- unify r1 r2;
- true
- with | Unify_error _ -> false
- in
- (* we only have to worry about non-covariant issues *)
- if not is_covariant then begin
- (* override return type and cast implemented function *)
- let args, newr = match follow t2, follow (apply_params f.cf_params (List.map snd f2.cf_params) real_ftype) with
- | TFun(a,_), TFun(_,r) -> a,r
- | _ -> assert false
- in
- f2.cf_type <- TFun(args,newr);
- (match f2.cf_expr with
- | Some ({ eexpr = TFunction tf } as e) ->
- f2.cf_expr <- Some { e with eexpr = TFunction { tf with tf_type = newr } }
- | _ -> ())
- end
- | TFun(a1,r1), TFun(a2,r2) ->
- (* just implement a function that will call the main one *)
- let name, is_explicit = match explicit_fn_name with
- | Some fn when not (type_iseq r1 r2) && Typeload.same_overload_args real_ftype t2 f f2 ->
- fn iface itl f.cf_name, true
- | _ -> f.cf_name, false
- in
- let p = f2.cf_pos in
- let newf = mk_class_field name real_ftype true f.cf_pos (Method MethNormal) f.cf_params in
- let vars = List.map (fun (n,_,t) -> alloc_var n t) a2 in
- let args = List.map2 (fun v (_,_,t) -> mk_cast t (mk_local v f2.cf_pos)) vars a1 in
- let field = { eexpr = TField(this, FInstance(c,List.map snd c.cl_params,f2)); etype = TFun(a1,r1); epos = p } in
- let call = { eexpr = TCall(field, args); etype = r1; epos = p } in
- (* let call = gen.gparam_func_call call field (List.map snd f.cf_params) args in *)
- let is_void = is_void r2 in
- newf.cf_expr <- Some {
- eexpr = TFunction({
- tf_args = List.map (fun v -> v,None) vars;
- tf_type = r2;
- tf_expr = (if is_void then call else {
- eexpr = TReturn (Some (mk_cast r2 call));
- etype = r2;
- epos = p
- })
- });
- etype = real_ftype;
- epos = p;
- };
- (* delayed: add to class *)
- let delay () =
- try
- let fm = PMap.find f.cf_name c.cl_fields in
- fm.cf_overloads <- newf :: fm.cf_overloads
- with | Not_found ->
- c.cl_fields <- PMap.add f.cf_name newf c.cl_fields;
- c.cl_ordered_fields <- newf :: c.cl_ordered_fields
- in
- (* gen.gafter_filters_ended <- delay :: gen.gafter_filters_ended *)
- delay();
- | _ -> assert false
- end
- with | Not_found -> ()
- in
- List.iter (fun f -> match f.cf_kind with | Var _ -> () | _ -> loop_f f) iface.cl_ordered_fields
- in
- List.iter (fun (iface,itl) -> loop_iface iface itl) c.cl_implements;
- (* now go through all overrides, *)
- let rec check_f f =
- (* find the first declared field *)
- let is_overload = Meta.has Meta.Overload f.cf_meta in
- let decl = if is_overload then
- find_first_declared_field gen c ~exact_field:f f.cf_name
- else
- find_first_declared_field gen c f.cf_name
- in
- match decl with
- | Some(f2,actual_t,_,t,declared_cl,_,_)
- when not (Typeload.same_overload_args actual_t (get_real_fun gen f.cf_type) f2 f) ->
- if Meta.has Meta.Overload f.cf_meta then begin
- (* if it is overload, create another field with the requested type *)
- let f3 = mk_class_field f.cf_name t f.cf_public f.cf_pos f.cf_kind f.cf_params in
- let p = f.cf_pos in
- let old_args, old_ret = get_fun f.cf_type in
- let args, ret = get_fun t in
- let tf_args = List.map (fun (n,o,t) -> alloc_var n t, None) args in
- let f3_mk_return = if is_void ret then (fun e -> e) else (fun e -> mk_return (mk_cast ret e)) in
- f3.cf_expr <- Some {
- eexpr = TFunction({
- tf_args = tf_args;
- tf_type = ret;
- tf_expr = mk_block (f3_mk_return {
- eexpr = TCall(
- {
- eexpr = TField(
- { eexpr = TConst TThis; etype = TInst(c, List.map snd c.cl_params); epos = p },
- FInstance(c,List.map snd c.cl_params,f));
- etype = f.cf_type;
- epos = p
- },
- List.map2 (fun (v,_) (_,_,t) -> mk_cast t (mk_local v p)) tf_args old_args);
- etype = old_ret;
- epos = p
- })
- });
- etype = t;
- epos = p;
- };
- gen.gafter_filters_ended <- ((fun () ->
- f.cf_overloads <- f3 :: f.cf_overloads;
- ) :: gen.gafter_filters_ended);
- f3
- end else begin match f.cf_expr with
- | Some({ eexpr = TFunction(tf) } as e) ->
- (* if it's not overload, just cast the vars *)
- let actual_args, _ = get_fun (get_real_fun gen actual_t) in
- let new_args, vardecl = List.fold_left2 (fun (args,vdecl) (v,_) (_,_,t) ->
- if not (type_iseq (gen.greal_type v.v_type) (gen.greal_type t)) then begin
- let new_var = mk_temp gen v.v_name t in
- (new_var,None) :: args, (v, Some(mk_cast v.v_type (mk_local new_var f.cf_pos))) :: vdecl
- end else
- (v,None) :: args, vdecl
- ) ([],[]) tf.tf_args actual_args in
- if vardecl <> [] then
- f.cf_expr <- Some({ e with
- eexpr = TFunction({ tf with
- tf_args = List.rev new_args;
- tf_expr = Type.concat { eexpr = TBlock(List.map (fun (v,ve) -> { eexpr = TVar(v,ve); etype = gen.gcon.basic.tvoid; epos = e.epos }) vardecl); etype = gen.gcon.basic.tvoid; epos = e.epos } tf.tf_expr
- });
- });
- f
- | _ -> f
- end
- | _ -> f
- in
- if not c.cl_extern then
- c.cl_overrides <- List.map (fun f -> check_f f) c.cl_overrides;
- md
- | _ -> md
- in
- run
- let configure ?explicit_fn_name gen =
- let delay () =
- Hashtbl.clear gen.greal_field_types
- in
- gen.gafter_mod_filters_ended <- delay :: gen.gafter_mod_filters_ended;
- let run = run ~explicit_fn_name:explicit_fn_name gen in
- let map md = Some(run md) in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* Normalize *)
- (* ******************************************* *)
- (*
- - Filters out enum constructor type parameters from the AST; See Issue #1796
- - Filters out monomorphs
- - Filters out all non-whitelisted AST metadata
- dependencies:
- No dependencies; but it still should be one of the first filters to run,
- as it will help normalize the AST
- *)
- module Normalize =
- struct
- let name = "normalize_type"
- let priority = max_dep
- let rec filter_param t = match t with
- | TInst({ cl_kind = KTypeParameter _ } as c,_) when Meta.has Meta.EnumConstructorParam c.cl_meta ->
- t_dynamic
- | TMono r -> (match !r with
- | None -> t_dynamic
- | Some t -> filter_param t)
- | TInst(_,[]) | TEnum(_,[]) | TType(_,[]) | TAbstract(_,[]) -> t
- | TType(t,tl) -> TType(t,List.map filter_param tl)
- | TInst(c,tl) -> TInst(c,List.map filter_param tl)
- | TEnum(e,tl) -> TEnum(e,List.map filter_param tl)
- | TAbstract({ a_path = (["haxe";"extern"],"Rest") } as a,tl) -> TAbstract(a, List.map filter_param tl)
- | TAbstract(a,tl) when not (Meta.has Meta.CoreType a.a_meta) ->
- filter_param (Abstract.get_underlying_type a tl)
- | TAbstract(a,tl) -> TAbstract(a, List.map filter_param tl)
- | TAnon a ->
- TAnon {
- a_fields = PMap.map (fun f -> { f with cf_type = filter_param f.cf_type }) a.a_fields;
- a_status = a.a_status;
- }
- | TFun(args,ret) -> TFun(List.map (fun (n,o,t) -> (n,o,filter_param t)) args, filter_param ret)
- | TDynamic _ -> t
- | TLazy f -> filter_param (!f())
- let default_implementation gen ~metas =
- let rec run e =
- match e.eexpr with
- | TMeta(entry, e) when not (Hashtbl.mem metas entry) ->
- run e
- | _ ->
- map_expr_type (fun e -> run e) filter_param (fun v -> v.v_type <- filter_param v.v_type; v) e
- in
- run
- let default_implementation_module gen ~metas =
- let rec run md = match md with
- | TClassDecl cl ->
- let rec map cf =
- cf.cf_type <- filter_param cf.cf_type;
- List.iter map cf.cf_overloads
- in
- List.iter map cl.cl_ordered_fields;
- List.iter map cl.cl_ordered_statics;
- Option.may map cl.cl_constructor;
- md
- | _ -> md
- in
- run
- let configure gen ~metas =
- let map e = Some(default_implementation gen e ~metas:metas) in
- gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map;
- let map md = Some(default_implementation_module gen ~metas md) in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (* ******************************************* *)
- (* InterfaceMetas *)
- (* ******************************************* *)
- (*
- Deal with metadata on interfaces by taking it off from interface, and adding a new class with `_HxMeta` suffix
- dependencies:
- Must run before InitFunction
- *)
- module InterfaceMetas =
- struct
- let name = "interface_metas"
- let priority = solve_deps name [ DBefore InitFunction.priority ]
- let traverse gen =
- let run md = match md with
- | TClassDecl ({ cl_interface = true; cl_ordered_statics = (_ :: _) } as cl) ->
- cl.cl_ordered_statics <- [];
- let path = fst cl.cl_path,snd cl.cl_path ^ "_HxMeta" in
- (match Codegen.build_metadata gen.gcon (TClassDecl cl) with
- | Some expr ->
- let ncls = mk_class cl.cl_module path cl.cl_pos in
- let cf = mk_class_field "__meta__" expr.etype false expr.epos (Var { v_read = AccNormal; v_write = AccNormal }) [] in
- cf.cf_expr <- Some expr;
- ncls.cl_statics <- PMap.add "__meta__" cf ncls.cl_statics;
- ncls.cl_ordered_statics <- cf :: ncls.cl_ordered_statics;
- gen.gadd_to_module (TClassDecl(ncls)) priority;
- | _ -> ())
- | _ -> ()
- in
- run
- let configure gen =
- let map md = traverse gen md; Some(md) in
- gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- (*
- (* ******************************************* *)
- (* Example *)
- (* ******************************************* *)
- (*
- description
- dependencies:
- *)
- module Example =
- struct
- let name = "example"
- let priority = solve_deps name []
- let default_implementation gen =
- let rec run e =
- match e.eexpr with
- | _ -> Type.map_expr run e
- in
- run
- let configure gen (mapping_func:texpr->texpr) =
- let map e = Some(mapping_func e) in
- gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
- end;;
- *)
|