gencommon.ml 410 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311
  1. (*
  2. * Copyright (C)2005-2013 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *)
  22. (*
  23. Gen Common API
  24. This module intends to be a common set of utilities common to all targets.
  25. It's intended to provide a set of tools to be able to make targets in Haxe more easily, and to
  26. allow the programmer to have more control of how the target language will handle the program.
  27. For example, as of now, the hxcpp target, while greatly done, relies heavily on cpp's own operator
  28. overloading, and implicit conversions, which make it very hard to deliver a similar solution for languages
  29. that lack these features.
  30. So this little framework is here so you can manipulate the Haxe AST and start bringing the AST closer
  31. to how it's intenteded to be in your host language.
  32. Rules
  33. Design goals
  34. Naming convention
  35. Weaknesses and TODO's
  36. *)
  37. open Unix
  38. open Ast
  39. open Type
  40. open Common
  41. open Option
  42. open Printf
  43. open ExtString
  44. let debug_type_ctor = function
  45. | TMono _ -> "TMono"
  46. | TEnum _ -> "TEnum"
  47. | TInst _ -> "TInst"
  48. | TType _ -> "TType"
  49. | TFun _ -> "TFun"
  50. | TAnon _ -> "TAnon"
  51. | TDynamic _ -> "TDynamic"
  52. | TLazy _ -> "TLazy"
  53. | TAbstract _ -> "TAbstract"
  54. let debug_type = (s_type (print_context()))
  55. let debug_expr = s_expr debug_type
  56. let rec like_float t =
  57. match follow t with
  58. | TAbstract({ a_path = ([], "Float") },[])
  59. | TAbstract({ a_path = ([], "Int") },[]) -> true
  60. | TAbstract({ a_path = (["cs"], "Pointer") },_) -> false
  61. | TAbstract(a, _) -> List.exists (fun t -> like_float t) a.a_from || List.exists (fun t -> like_float t) a.a_to
  62. | _ -> false
  63. let rec like_int t =
  64. match follow t with
  65. | TAbstract({ a_path = ([], "Int") },[]) -> true
  66. | TAbstract({ a_path = (["cs"], "Pointer") },_) -> false
  67. | TAbstract(a, _) -> List.exists (fun t -> like_int t) a.a_from || List.exists (fun t -> like_int t) a.a_to
  68. | _ -> false
  69. let rec like_i64 t =
  70. match follow t with
  71. | TInst({ cl_path = (["cs"], "Int64") },[])
  72. | TAbstract({ a_path = (["cs"], "Int64") },[])
  73. | TInst({ cl_path = (["cs"], "UInt64") },[])
  74. | TInst({ cl_path = (["java"], "Int64") },[])
  75. | TAbstract({ a_path = (["java"], "Int64") },[])
  76. | TInst({ cl_path = (["haxe"], "Int64") },[])
  77. | TAbstract({ a_path = (["haxe"], "Int64") },[]) -> true
  78. | TAbstract(a, _) -> List.exists (fun t -> like_i64 t) a.a_from || List.exists (fun t -> like_i64 t) a.a_to
  79. | _ -> false
  80. let follow_once t =
  81. match t with
  82. | TMono r ->
  83. (match !r with
  84. | Some t -> t
  85. | _ -> t_dynamic (* avoid infinite loop / should be the same in this context *))
  86. | TLazy f ->
  87. !f()
  88. | TType (t,tl) ->
  89. apply_params t.t_params tl t.t_type
  90. | _ -> t
  91. let t_empty = TAnon({ a_fields = PMap.empty; a_status = ref (Closed) })
  92. (* the undefined is a special var that works like null, but can have special meaning *)
  93. let v_undefined = alloc_var "__undefined__" t_dynamic
  94. let undefined pos = { eexpr = TLocal(v_undefined); etype = t_dynamic; epos = pos }
  95. module ExprHashtblHelper =
  96. struct
  97. type hash_texpr_t =
  98. {
  99. hepos : pos;
  100. heexpr : int;
  101. hetype : int;
  102. }
  103. let mk_heexpr = function
  104. | TConst _ -> 0 | TLocal _ -> 1 | TArray _ -> 3 | TBinop _ -> 4 | TField _ -> 5 | TTypeExpr _ -> 7 | TParenthesis _ -> 8 | TObjectDecl _ -> 9
  105. | TArrayDecl _ -> 10 | TCall _ -> 11 | TNew _ -> 12 | TUnop _ -> 13 | TFunction _ -> 14 | TVar _ -> 15 | TBlock _ -> 16 | TFor _ -> 17 | TIf _ -> 18 | TWhile _ -> 19
  106. | TSwitch _ -> 20 (* | TPatMatch _ -> 21 *) | TTry _ -> 22 | TReturn _ -> 23 | TBreak -> 24 | TContinue -> 25 | TThrow _ -> 26 | TCast _ -> 27 | TMeta _ -> 28 | TEnumParameter _ -> 29
  107. let mk_heetype = function
  108. | TMono _ -> 0 | TEnum _ -> 1 | TInst _ -> 2 | TType _ -> 3 | TFun _ -> 4
  109. | TAnon _ -> 5 | TDynamic _ -> 6 | TLazy _ -> 7 | TAbstract _ -> 8
  110. let mk_type e =
  111. {
  112. hepos = e.epos;
  113. heexpr = mk_heexpr e.eexpr;
  114. hetype = mk_heetype e.etype;
  115. }
  116. end;;
  117. let path_of_md_def md_def =
  118. match md_def.m_types with
  119. | [TClassDecl c] -> c.cl_path
  120. | _ -> md_def.m_path
  121. open ExprHashtblHelper;;
  122. (* Expression Hashtbl. This shouldn't be kept indefinately as it's not a weak Hashtbl. *)
  123. module ExprHashtbl = Hashtbl.Make(
  124. struct
  125. type t = Type.texpr
  126. let equal = (==)
  127. let hash t = Hashtbl.hash (mk_type t)
  128. end
  129. );;
  130. (* ******************************************* *)
  131. (* Gen Common
  132. This is the key module for generation of Java and C# sources
  133. In order for both modules to share as much code as possible, some
  134. rules were devised:
  135. - every feature has its own submodule, and may contain the following methods:
  136. - configure
  137. sets all the configuration variables for the module to run. If a module has this method,
  138. it *should* be called once before running any filter
  139. - run_filter ->
  140. runs the filter immediately on the context
  141. - add_filter ->
  142. adds the filter to an expr->expr list. Most filter modules will provide this option so the filter
  143. function can only run once.
  144. - most submodules will have side-effects so the order of operations will matter.
  145. When running configure / add_filter this might be taken care of with the rule-based dispatch system working
  146. underneath, but still there might be some incompatibilities. There will be an effort to document it.
  147. The modules can hint on the order by suffixing their functions with _first or _last.
  148. - any of those methods might have different parameters, that configure how the filter will run.
  149. For example, a simple filter that maps switch() expressions to if () .. else if... might receive
  150. a function that filters what content should be mapped
  151. - Other targets can use those filters on their own code. In order to do that,
  152. a simple configuration step is needed: you need to initialize a generator_ctx type with
  153. Gencommon.new_gen (context:Common.context)
  154. with a generator_ctx context you will be able to add filters to your code, and execute them with
  155. Gencommon.run_filters (gen_context:Gencommon.generator_ctx)
  156. After running the filters, you can run your own generator normally.
  157. (* , or you can run
  158. Gencommon.generate_modules (gen_context:Gencommon.generator_ctx) (extension:string) (module_gen:module_type list->bool)
  159. where module_gen will take a whole module (can be *)
  160. *)
  161. (* ******************************************* *)
  162. (* common helpers *)
  163. (* ******************************************* *)
  164. let assertions = false (* when assertions == true, many assertions will be made to guarantee the quality of the data input *)
  165. let debug_mode = ref false
  166. let trace s = if !debug_mode then print_endline s else ()
  167. let timer name = if !debug_mode then Common.timer name else fun () -> ()
  168. let is_string t = match follow t with | TInst({ cl_path = ([], "String") }, []) -> true | _ -> false
  169. (* helper function for creating Anon types of class / enum modules *)
  170. let anon_of_classtype cl =
  171. TAnon {
  172. a_fields = cl.cl_statics;
  173. a_status = ref (Statics cl)
  174. }
  175. let anon_of_enum e =
  176. TAnon {
  177. a_fields = PMap.empty;
  178. a_status = ref (EnumStatics e)
  179. }
  180. let anon_of_abstract a =
  181. TAnon {
  182. a_fields = PMap.empty;
  183. a_status = ref (AbstractStatics a)
  184. }
  185. let anon_of_mt mt = match mt with
  186. | TClassDecl cl -> anon_of_classtype cl
  187. | TEnumDecl e -> anon_of_enum e
  188. | TAbstractDecl a -> anon_of_abstract a
  189. | _ -> assert false
  190. let anon_class t =
  191. match follow t with
  192. | TAnon anon ->
  193. (match !(anon.a_status) with
  194. | Statics (cl) -> Some(TClassDecl(cl))
  195. | EnumStatics (e) -> Some(TEnumDecl(e))
  196. | AbstractStatics (a) -> Some(TAbstractDecl(a))
  197. | _ -> None)
  198. | _ -> None
  199. let path_s path =
  200. match path with | ([], s) -> s | (p, s) -> (String.concat "." (fst path)) ^ "." ^ (snd path)
  201. let rec t_to_md t = match t with
  202. | TInst (cl,_) -> TClassDecl cl
  203. | TEnum (e,_) -> TEnumDecl e
  204. | TType (t,_) -> TTypeDecl t
  205. | TAbstract (a,_) -> TAbstractDecl a
  206. | TAnon anon ->
  207. (match !(anon.a_status) with
  208. | EnumStatics e -> TEnumDecl e
  209. | Statics cl -> TClassDecl cl
  210. | AbstractStatics a -> TAbstractDecl a
  211. | _ -> assert false)
  212. | TLazy f -> t_to_md (!f())
  213. | TMono r -> (match !r with | Some t -> t_to_md t | None -> assert false)
  214. | _ -> assert false
  215. let get_cl mt = match mt with | TClassDecl cl -> cl | _ -> failwith ("Unexpected module type of '" ^ path_s (t_path mt) ^ "'")
  216. let get_abstract mt = match mt with | TAbstractDecl a -> a | _ -> failwith ("Unexpected module type of '" ^ path_s (t_path mt) ^ "'")
  217. let get_tdef mt = match mt with | TTypeDecl t -> t | _ -> assert false
  218. let mk_mt_access mt pos = { eexpr = TTypeExpr(mt); etype = anon_of_mt mt; epos = pos }
  219. let is_void t = match follow t with
  220. | TAbstract ({ a_path = ([], "Void") },[]) ->
  221. true
  222. | _ -> false
  223. let mk_local var pos = { eexpr = TLocal(var); etype = var.v_type; epos = pos }
  224. (* this function is used by CastDetection module *)
  225. let get_fun t =
  226. match follow t with | TFun(r1,r2) -> (r1,r2) | _ -> (trace (s_type (print_context()) (follow t) )); assert false
  227. let mk_cast t e =
  228. { eexpr = TCast(e, None); etype = t; epos = e.epos }
  229. let mk_classtype_access cl pos =
  230. { eexpr = TTypeExpr(TClassDecl(cl)); etype = anon_of_classtype cl; epos = pos }
  231. let mk_static_field_access_infer cl field pos params =
  232. try
  233. let cf = (PMap.find field cl.cl_statics) in
  234. { 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 }
  235. with | Not_found -> failwith ("Cannot find field " ^ field ^ " in type " ^ (path_s cl.cl_path))
  236. let mk_static_field_access cl field fieldt pos =
  237. { (mk_static_field_access_infer cl field pos []) with etype = fieldt }
  238. (* stolen from Hugh's sources ;-) *)
  239. (* this used to be a class, but there was something in there that crashed ocaml native compiler in windows *)
  240. module SourceWriter =
  241. struct
  242. type source_writer =
  243. {
  244. sw_buf : Buffer.t;
  245. mutable sw_has_content : bool;
  246. mutable sw_indent : string;
  247. mutable sw_indents : string list;
  248. }
  249. let new_source_writer () =
  250. {
  251. sw_buf = Buffer.create 0;
  252. sw_has_content = false;
  253. sw_indent = "";
  254. sw_indents = [];
  255. }
  256. let add_writer w_write w_read = Buffer.add_buffer w_read.sw_buf w_write.sw_buf
  257. let contents w = Buffer.contents w.sw_buf
  258. let len w = Buffer.length w.sw_buf
  259. let write w x =
  260. (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);
  261. let len = (String.length x)-1 in
  262. if len >= 0 && String.get x len = '\n' then begin w.sw_has_content <- false end else w.sw_has_content <- true
  263. let push_indent w = w.sw_indents <- "\t"::w.sw_indents; w.sw_indent <- String.concat "" w.sw_indents
  264. let pop_indent w =
  265. match w.sw_indents with
  266. | h::tail -> w.sw_indents <- tail; w.sw_indent <- String.concat "" w.sw_indents
  267. | [] -> w.sw_indent <- "/*?*/"
  268. let newline w = write w "\n"
  269. let begin_block w = (if w.sw_has_content then newline w); write w "{"; push_indent w; newline w
  270. let end_block w = pop_indent w; (if w.sw_has_content then newline w); write w "}"; newline w
  271. let print w =
  272. (if not w.sw_has_content then begin w.sw_has_content <- true; Buffer.add_string w.sw_buf w.sw_indent end);
  273. bprintf w.sw_buf;
  274. end;;
  275. (* rule_dispatcher's priority *)
  276. type priority =
  277. | PFirst
  278. | PLast
  279. | PZero
  280. | PCustom of float
  281. exception DuplicateName of string
  282. exception NoRulesApplied
  283. let indent = ref []
  284. (* the rule dispatcher is the primary way to deal with distributed "plugins" *)
  285. (* we will define rules that will form a distributed / extensible match system *)
  286. class ['tp, 'ret] rule_dispatcher name ignore_not_found =
  287. object(self)
  288. val tbl = Hashtbl.create 16
  289. val mutable keys = []
  290. val names = Hashtbl.create 16
  291. val mutable temp = 0
  292. method add ?(name : string option) (* name helps debugging *) ?(priority : priority = PZero) (rule : 'tp->'ret option) =
  293. let p = match priority with
  294. | PFirst -> infinity
  295. | PLast -> neg_infinity
  296. | PZero -> 0.0
  297. | PCustom i -> i
  298. in
  299. let q = if not( Hashtbl.mem tbl p ) then begin
  300. let q = Stack.create() in
  301. Hashtbl.add tbl p q;
  302. keys <- p :: keys;
  303. keys <- List.sort (fun x y -> - (compare x y)) keys;
  304. q
  305. end else Hashtbl.find tbl p in
  306. let name = match name with
  307. | None -> temp <- temp + 1; "$_" ^ (string_of_int temp)
  308. | Some s -> s
  309. in
  310. (if Hashtbl.mem names name then raise (DuplicateName(name)));
  311. Hashtbl.add names name q;
  312. Stack.push (name, rule) q
  313. method describe =
  314. Hashtbl.iter (fun s _ -> (trace s)) names;
  315. method remove (name : string) =
  316. if Hashtbl.mem names name then begin
  317. let q = Hashtbl.find names name in
  318. let q_temp = Stack.create () in
  319. Stack.iter (function
  320. | (n, _) when n = name -> ()
  321. | _ as r -> Stack.push r q_temp
  322. ) q;
  323. Stack.clear q;
  324. Stack.iter (fun r -> Stack.push r q) q_temp;
  325. Hashtbl.remove names name;
  326. true
  327. end else false
  328. method run_f tp = get (self#run tp)
  329. method did_run tp = is_some (self#run tp)
  330. method get_list =
  331. let ret = ref [] in
  332. List.iter (fun key ->
  333. let q = Hashtbl.find tbl key in
  334. Stack.iter (fun (_, rule) -> ret := rule :: !ret) q
  335. ) keys;
  336. List.rev !ret
  337. method run_from (priority:float) (tp:'tp) : 'ret option =
  338. let ok = ref ignore_not_found in
  339. let ret = ref None in
  340. indent := "\t" :: !indent;
  341. (try begin
  342. List.iter (fun key ->
  343. if key < priority then begin
  344. let q = Hashtbl.find tbl key in
  345. Stack.iter (fun (n, rule) ->
  346. let t = if !debug_mode then Common.timer ("rule dispatcher rule: " ^ n) else fun () -> () in
  347. let r = rule(tp) in
  348. t();
  349. if is_some r then begin ret := r; raise Exit end
  350. ) q
  351. end
  352. ) keys
  353. end with Exit -> ok := true);
  354. (match !indent with
  355. | [] -> ()
  356. | h::t -> indent := t);
  357. (if not (!ok) then raise NoRulesApplied);
  358. !ret
  359. method run (tp:'tp) : 'ret option =
  360. self#run_from infinity tp
  361. end;;
  362. (* this is a special case where tp = tret and you stack their output as the next's input *)
  363. class ['tp] rule_map_dispatcher name =
  364. object(self)
  365. inherit ['tp, 'tp] rule_dispatcher name true as super
  366. method run_f tp = get (self#run tp)
  367. method run_from (priority:float) (tp:'tp) : 'ret option =
  368. let cur = ref tp in
  369. (try begin
  370. List.iter (fun key ->
  371. if key < priority then begin
  372. let q = Hashtbl.find tbl key in
  373. Stack.iter (fun (n, rule) ->
  374. trace ("running rule " ^ n);
  375. let t = if !debug_mode then Common.timer ("rule map dispatcher rule: " ^ n) else fun () -> () in
  376. let r = rule(!cur) in
  377. t();
  378. if is_some r then begin cur := get r end
  379. ) q
  380. end
  381. ) keys
  382. end with Exit -> ());
  383. Some (!cur)
  384. end;;
  385. type generator_ctx =
  386. {
  387. (* these are the basic context fields. If another target is using this context, *)
  388. (* this is all you need to care about *)
  389. mutable gcon : Common.context;
  390. gclasses : gen_classes;
  391. gtools : gen_tools;
  392. (*
  393. configurable function that receives a desired name and makes it "internal", doing the best
  394. to ensure that it will not be called from outside.
  395. To avoid name clashes between internal names, user must specify two strings: a "namespace" and the name itself
  396. *)
  397. mutable gmk_internal_name : string->string->string;
  398. (*
  399. module filters run before module filters and they should generate valid haxe syntax as a result.
  400. Module filters shouldn't go through the expressions as it adds an unnecessary burden to the GC,
  401. and it can all be done in a single step with gexpr_filters and proper priority selection.
  402. As a convention, Module filters should end their name with Modf, so they aren't mistaken with expression filters
  403. *)
  404. gmodule_filters : (module_type) rule_map_dispatcher;
  405. (*
  406. expression filters are the most common filters to be applied.
  407. They should also generate only valid haxe expressions, so e.g. calls to non-existant methods
  408. should be avoided, although there are some ways around them (like gspecial_methods)
  409. *)
  410. gexpr_filters : (texpr) rule_map_dispatcher;
  411. (*
  412. syntax filters are also expression filters but they no longer require
  413. that the resulting expressions be valid haxe expressions.
  414. They then have no guarantee that either the input expressions or the output one follow the same
  415. rules as normal haxe code.
  416. *)
  417. gsyntax_filters : (texpr) rule_map_dispatcher;
  418. (* these are more advanced features, but they would require a rewrite of targets *)
  419. (* they are just helpers to ditribute functions like "follow" or "type to string" *)
  420. (* so adding a module will already take care of correctly following a certain type of *)
  421. (* variable, for example *)
  422. (* follows the type through typedefs, lazy typing, etc. *)
  423. (* it's the place to put specific rules to handle typedefs, like *)
  424. (* other basic types like UInt *)
  425. gfollow : (t, t) rule_dispatcher;
  426. gtypes : (path, module_type) Hashtbl.t;
  427. (* cast detection helpers / settings *)
  428. (* this is a cache for all field access types *)
  429. 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;
  430. (* this function allows any code to handle casts as if it were inside the cast_detect module *)
  431. mutable ghandle_cast : t->t->texpr->texpr;
  432. (* when an unsafe cast is made, we can warn the user *)
  433. mutable gon_unsafe_cast : t->t->pos->unit;
  434. (* does this type needs to be boxed? Normally always false, unless special type handling must be made *)
  435. mutable gneeds_box : t->bool;
  436. (* does this 'special type' needs cast to this other type? *)
  437. (* this is here so we can implement custom behavior for "opaque" typedefs *)
  438. mutable gspecial_needs_cast : t->t->bool;
  439. (* sometimes we may want to support unrelated conversions on cast detection *)
  440. (* for example, haxe.lang.Null<T> -> T on C# *)
  441. (* every time an unrelated conversion is found, each to/from path is searched on this hashtbl *)
  442. (* if found, the function will be executed with from_type, to_type. If returns true, it means that *)
  443. (* it is a supported conversion, and the unsafe cast routine changes to a simple cast *)
  444. gsupported_conversions : (path, t->t->bool) Hashtbl.t;
  445. (* API for filters *)
  446. (* add type can be called at any time, and will add a new module_def that may or may not be filtered *)
  447. (* module_type -> should_filter *)
  448. mutable gadd_type : module_type -> bool -> unit;
  449. (* 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 *)
  450. mutable gadd_to_module : module_type -> float -> unit;
  451. (* during expr filters, shows the current class path *)
  452. mutable gcurrent_path : path;
  453. (* current class *)
  454. mutable gcurrent_class : tclass option;
  455. (* current class field, if any *)
  456. mutable gcurrent_classfield : tclass_field option;
  457. (* events *)
  458. (* is executed once every new classfield *)
  459. mutable gon_classfield_start : (unit -> unit) list;
  460. (* is executed once every new module type *)
  461. mutable gon_new_module_type : (unit -> unit) list;
  462. (* after module filters ended *)
  463. mutable gafter_mod_filters_ended : (unit -> unit) list;
  464. (* after expression filters ended *)
  465. mutable gafter_expr_filters_ended : (unit -> unit) list;
  466. (* after all filters are run *)
  467. mutable gafter_filters_ended : (unit -> unit) list;
  468. mutable gbase_class_fields : (string, tclass_field) PMap.t;
  469. (* real type is the type as it is read by the target. *)
  470. (* This function is here because most targets don't have *)
  471. (* a 1:1 translation between haxe types and its native types *)
  472. (* But types aren't changed to this representation as we might lose *)
  473. (* some valuable type information in the process *)
  474. mutable greal_type : t -> t;
  475. (*
  476. the same as greal_type but for type parameters.
  477. *)
  478. mutable greal_type_param : module_type -> tparams -> tparams;
  479. (*
  480. is the type a value type?
  481. This may be used in some optimizations where reference types and value types
  482. are handled differently. At first the default is very good to use, and if tweaks are needed,
  483. it's best to be done by adding @:struct meta to the value types
  484. *
  485. mutable gis_value_type : t -> bool;*)
  486. (* misc configuration *)
  487. (*
  488. Should the target allow type parameter dynamic conversion,
  489. or should we add a cast to those cases as well?
  490. *)
  491. mutable gallow_tp_dynamic_conversion : bool;
  492. (*
  493. Does the target support type parameter constraints?
  494. If not, they will be ignored when detecting casts
  495. *)
  496. mutable guse_tp_constraints : bool;
  497. (* internal apis *)
  498. (* param_func_call : used by TypeParams and CastDetection *)
  499. mutable gparam_func_call : texpr->texpr->tparams->texpr list->texpr;
  500. (* does it already have a type parameter cast handler? This is used by CastDetect to know if it should handle type parameter casts *)
  501. mutable ghas_tparam_cast_handler : bool;
  502. (* type parameter casts - special cases *)
  503. (* function cast_from, cast_to -> texpr *)
  504. gtparam_cast : (path, (texpr->t->texpr)) Hashtbl.t;
  505. (*
  506. special vars are used for adding special behavior to
  507. *)
  508. gspecial_vars : (string, bool) Hashtbl.t;
  509. }
  510. and gen_classes =
  511. {
  512. cl_reflect : tclass;
  513. cl_type : tclass;
  514. cl_dyn : tclass;
  515. t_iterator : tdef;
  516. mutable nativearray_len : texpr -> pos -> texpr;
  517. mutable nativearray_type : Type.t -> Type.t;
  518. mutable nativearray : Type.t -> Type.t;
  519. }
  520. (* add here all reflection transformation additions *)
  521. and gen_tools =
  522. {
  523. (* (klass : texpr, t : t) : texpr *)
  524. mutable r_create_empty : texpr->t->texpr;
  525. (* 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 *)
  526. mutable r_fields : bool->texpr->texpr;
  527. (* (first argument = return type. should be void in most cases) Reflect.setField(obj, field, val) *)
  528. mutable r_set_field : t->texpr->texpr->texpr->texpr;
  529. (* Reflect.field. bool indicates if is safe (no error throwing) or unsafe; t is the expected return type true = safe *)
  530. mutable r_field : bool->t->texpr->texpr->texpr;
  531. (*
  532. these are now the functions that will later be used when creating the reflection classes
  533. *)
  534. (* on the default implementation (at OverloadingCtors), it will be new SomeClass<params>(EmptyInstance) *)
  535. mutable rf_create_empty : tclass->tparams->pos->texpr;
  536. }
  537. let get_type types path =
  538. List.find (fun md -> match md with
  539. | TClassDecl cl when cl.cl_path = path -> true
  540. | TEnumDecl e when e.e_path = path -> true
  541. | TTypeDecl t when t.t_path = path -> true
  542. | TAbstractDecl a when a.a_path = path -> true
  543. | _ -> false
  544. ) types
  545. let new_ctx con =
  546. let types = Hashtbl.create (List.length con.types) in
  547. List.iter (fun mt ->
  548. match mt with
  549. | TClassDecl cl -> Hashtbl.add types cl.cl_path mt
  550. | TEnumDecl e -> Hashtbl.add types e.e_path mt
  551. | TTypeDecl t -> Hashtbl.add types t.t_path mt
  552. | TAbstractDecl a -> Hashtbl.add types a.a_path mt
  553. ) con.types;
  554. let cl_dyn = match get_type con.types ([], "Dynamic") with
  555. | TClassDecl c -> c
  556. | TAbstractDecl a ->
  557. mk_class a.a_module ([], "Dynamic") a.a_pos
  558. | _ -> assert false
  559. in
  560. let rec gen = {
  561. gcon = con;
  562. gclasses = {
  563. cl_reflect = get_cl (get_type con.types ([], "Reflect"));
  564. cl_type = get_cl (get_type con.types ([], "Type"));
  565. cl_dyn = cl_dyn;
  566. t_iterator = get_tdef (get_type con.types ([], "Iterator"));
  567. nativearray = (fun _ -> assert false);
  568. nativearray_type = (fun _ -> assert false);
  569. nativearray_len = (fun _ -> assert false);
  570. };
  571. gtools = {
  572. r_create_empty = (fun eclass t ->
  573. let fieldcall = mk_static_field_access_infer gen.gclasses.cl_type "createEmptyInstance" eclass.epos [t] in
  574. { eexpr = TCall(fieldcall, [eclass]); etype = t; epos = eclass.epos }
  575. );
  576. r_fields = (fun is_used_only_by_iteration expr ->
  577. let fieldcall = mk_static_field_access_infer gen.gclasses.cl_reflect "fields" expr.epos [] in
  578. { eexpr = TCall(fieldcall, [expr]); etype = gen.gcon.basic.tarray gen.gcon.basic.tstring; epos = expr.epos }
  579. );
  580. (* Reflect.setField(obj, field, val). t by now is ignored. FIXME : fix this implementation *)
  581. r_set_field = (fun t obj field v ->
  582. let fieldcall = mk_static_field_access_infer gen.gclasses.cl_reflect "setField" v.epos [] in
  583. { eexpr = TCall(fieldcall, [obj; field; v]); etype = t_dynamic; epos = v.epos }
  584. );
  585. (* Reflect.field. bool indicates if is safe (no error throwing) or unsafe. true = safe *)
  586. r_field = (fun is_safe t obj field ->
  587. let fieldcall = mk_static_field_access_infer gen.gclasses.cl_reflect "field" obj.epos [] in
  588. (* FIXME: should we see if needs to cast? *)
  589. mk_cast t { eexpr = TCall(fieldcall, [obj; field]); etype = t_dynamic; epos = obj.epos }
  590. );
  591. rf_create_empty = (fun cl p pos ->
  592. gen.gtools.r_create_empty { eexpr = TTypeExpr(TClassDecl cl); epos = pos; etype = t_dynamic } (TInst(cl,p))
  593. ); (* TODO: Maybe implement using normal reflection? Type.createEmpty(MyClass) *)
  594. };
  595. gmk_internal_name = (fun ns s -> sprintf "__%s_%s" ns s);
  596. gexpr_filters = new rule_map_dispatcher "gexpr_filters";
  597. gmodule_filters = new rule_map_dispatcher "gmodule_filters";
  598. gsyntax_filters = new rule_map_dispatcher "gsyntax_filters";
  599. gfollow = new rule_dispatcher "gfollow" false;
  600. gtypes = types;
  601. greal_field_types = Hashtbl.create 0;
  602. ghandle_cast = (fun to_t from_t e -> mk_cast to_t e);
  603. 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));
  604. gneeds_box = (fun t -> false);
  605. gspecial_needs_cast = (fun to_t from_t -> true);
  606. gsupported_conversions = Hashtbl.create 0;
  607. gadd_type = (fun md should_filter ->
  608. if should_filter then begin
  609. con.types <- md :: con.types;
  610. con.modules <- { m_id = alloc_mid(); m_path = (t_path md); m_types = [md]; m_extra = module_extra "" "" 0. MFake } :: con.modules
  611. end else gen.gafter_filters_ended <- (fun () ->
  612. con.types <- md :: con.types;
  613. con.modules <- { m_id = alloc_mid(); m_path = (t_path md); m_types = [md]; m_extra = module_extra "" "" 0. MFake } :: con.modules
  614. ) :: gen.gafter_filters_ended;
  615. );
  616. gadd_to_module = (fun md pr -> failwith "module added outside expr filters");
  617. gcurrent_path = ([],"");
  618. gcurrent_class = None;
  619. gcurrent_classfield = None;
  620. gon_classfield_start = [];
  621. gon_new_module_type = [];
  622. gafter_mod_filters_ended = [];
  623. gafter_expr_filters_ended = [];
  624. gafter_filters_ended = [];
  625. gbase_class_fields = PMap.empty;
  626. greal_type = (fun t -> t);
  627. greal_type_param = (fun _ t -> t);
  628. gallow_tp_dynamic_conversion = false;
  629. guse_tp_constraints = false;
  630. (* as a default, ignore the params *)
  631. gparam_func_call = (fun ecall efield params elist -> { ecall with eexpr = TCall(efield, elist) });
  632. ghas_tparam_cast_handler = false;
  633. gtparam_cast = Hashtbl.create 0;
  634. gspecial_vars = Hashtbl.create 0;
  635. } in
  636. (*gen.gtools.r_create_empty <-
  637. gen.gtools.r_get_class <-
  638. gen.gtools.r_fields <- *)
  639. gen
  640. let init_ctx gen =
  641. (* ultimately add a follow once handler as the last follow handler *)
  642. let follow_f = gen.gfollow#run in
  643. let follow t =
  644. match t with
  645. | TMono r ->
  646. (match !r with
  647. | Some t -> follow_f t
  648. | _ -> Some t)
  649. | TLazy f ->
  650. follow_f (!f())
  651. | TType (t,tl) ->
  652. follow_f (apply_params t.t_params tl t.t_type)
  653. | _ -> Some t
  654. in
  655. gen.gfollow#add ~name:"final" ~priority:PLast follow
  656. (* run_follow (gen:generator_ctx) (t:t) *)
  657. let run_follow gen = gen.gfollow#run_f
  658. let reorder_modules gen =
  659. let modules = Hashtbl.create 20 in
  660. List.iter (fun md ->
  661. Hashtbl.add modules ( (t_infos md).mt_module ).m_path md
  662. ) gen.gcon.types;
  663. let con = gen.gcon in
  664. con.modules <- [];
  665. let processed = Hashtbl.create 20 in
  666. Hashtbl.iter (fun md_path md ->
  667. if not (Hashtbl.mem processed md_path) then begin
  668. Hashtbl.add processed md_path true;
  669. 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
  670. end
  671. ) modules
  672. let run_filters_from gen t filters =
  673. match t with
  674. | TClassDecl c ->
  675. trace (snd c.cl_path);
  676. gen.gcurrent_path <- c.cl_path;
  677. gen.gcurrent_class <- Some(c);
  678. List.iter (fun fn -> fn()) gen.gon_new_module_type;
  679. gen.gcurrent_classfield <- None;
  680. let rec process_field f =
  681. gen.gcurrent_classfield <- Some(f);
  682. List.iter (fun fn -> fn()) gen.gon_classfield_start;
  683. trace f.cf_name;
  684. (match f.cf_expr with
  685. | None -> ()
  686. | Some e ->
  687. f.cf_expr <- Some (List.fold_left (fun e f -> f e) e filters));
  688. List.iter process_field f.cf_overloads;
  689. in
  690. List.iter process_field c.cl_ordered_fields;
  691. List.iter process_field c.cl_ordered_statics;
  692. gen.gcurrent_classfield <- None;
  693. (match c.cl_constructor with
  694. | None -> ()
  695. | Some f -> process_field f);
  696. (match c.cl_init with
  697. | None -> ()
  698. | Some e ->
  699. c.cl_init <- Some (List.fold_left (fun e f -> f e) e filters));
  700. | TEnumDecl _ -> ()
  701. | TTypeDecl _ -> ()
  702. | TAbstractDecl _ -> ()
  703. let run_filters gen =
  704. let last_error = gen.gcon.error in
  705. let has_errors = ref false in
  706. gen.gcon.error <- (fun msg pos -> has_errors := true; last_error msg pos);
  707. (* first of all, we have to make sure that the filters won't trigger a major Gc collection *)
  708. let t = Common.timer "gencommon_filters" in
  709. (if Common.defined gen.gcon Define.GencommonDebug then debug_mode := true);
  710. let run_filters filter =
  711. let rec loop acc mds =
  712. match mds with
  713. | [] -> acc
  714. | md :: tl ->
  715. let filters = [ filter#run_f ] in
  716. let added_types = ref [] in
  717. gen.gadd_to_module <- (fun md_type priority ->
  718. gen.gcon.types <- md_type :: gen.gcon.types;
  719. added_types := (md_type, priority) :: !added_types
  720. );
  721. run_filters_from gen md filters;
  722. let added_types = List.map (fun (t,p) ->
  723. run_filters_from gen t [ fun e -> get (filter#run_from p e) ];
  724. if Hashtbl.mem gen.gtypes (t_path t) then begin
  725. let rec loop i =
  726. let p = t_path t in
  727. let new_p = (fst p, snd p ^ "_" ^ (string_of_int i)) in
  728. if Hashtbl.mem gen.gtypes new_p then
  729. loop (i+1)
  730. else
  731. match t with
  732. | TClassDecl cl -> cl.cl_path <- new_p
  733. | TEnumDecl e -> e.e_path <- new_p
  734. | TTypeDecl _ | TAbstractDecl _ -> ()
  735. in
  736. loop 0
  737. end;
  738. Hashtbl.add gen.gtypes (t_path t) t;
  739. t
  740. ) !added_types in
  741. loop (added_types @ (md :: acc)) tl
  742. in
  743. List.rev (loop [] gen.gcon.types)
  744. in
  745. let run_mod_filter filter =
  746. let last_add_to_module = gen.gadd_to_module in
  747. let added_types = ref [] in
  748. gen.gadd_to_module <- (fun md_type priority ->
  749. Hashtbl.add gen.gtypes (t_path md_type) md_type;
  750. added_types := (md_type, priority) :: !added_types
  751. );
  752. let rec loop processed not_processed =
  753. match not_processed with
  754. | hd :: tl ->
  755. (match hd with
  756. | TClassDecl c ->
  757. gen.gcurrent_class <- Some c
  758. | _ ->
  759. gen.gcurrent_class <- None);
  760. let new_hd = filter#run_f hd in
  761. let added_types_new = !added_types in
  762. added_types := [];
  763. let added_types = List.map (fun (t,p) ->
  764. get (filter#run_from p t)
  765. ) added_types_new in
  766. loop ( added_types @ (new_hd :: processed) ) tl
  767. | [] ->
  768. processed
  769. in
  770. let filtered = loop [] gen.gcon.types in
  771. gen.gadd_to_module <- last_add_to_module;
  772. gen.gcon.types <- List.rev (filtered)
  773. in
  774. run_mod_filter gen.gmodule_filters;
  775. List.iter (fun fn -> fn()) gen.gafter_mod_filters_ended;
  776. let last_add_to_module = gen.gadd_to_module in
  777. gen.gcon.types <- run_filters gen.gexpr_filters;
  778. gen.gadd_to_module <- last_add_to_module;
  779. List.iter (fun fn -> fn()) gen.gafter_expr_filters_ended;
  780. (* Codegen.post_process gen.gcon.types [gen.gexpr_filters#run_f]; *)
  781. gen.gcon.types <- run_filters gen.gsyntax_filters;
  782. List.iter (fun fn -> fn()) gen.gafter_filters_ended;
  783. reorder_modules gen;
  784. t();
  785. if !has_errors then raise (Abort("Compilation aborted with errors",null_pos))
  786. (* ******************************************* *)
  787. (* basic generation module that source code compilation implementations can use *)
  788. (* ******************************************* *)
  789. let write_file gen w source_dir path extension out_files =
  790. let t = timer "write file" in
  791. let s_path = source_dir ^ "/" ^ (snd path) ^ "." ^ (extension) in
  792. (* create the folders if they don't exist *)
  793. mkdir_from_path s_path;
  794. let contents = SourceWriter.contents w in
  795. let should_write = if not (Common.defined gen.gcon Define.ReplaceFiles) && Sys.file_exists s_path then begin
  796. let in_file = open_in s_path in
  797. let old_contents = Std.input_all in_file in
  798. close_in in_file;
  799. contents <> old_contents
  800. end else true in
  801. if should_write then begin
  802. let f = open_out s_path in
  803. output_string f contents;
  804. close_out f
  805. end;
  806. out_files := (unique_full_path s_path) :: !out_files;
  807. t()
  808. let clean_files path excludes verbose =
  809. let rec iter_files pack dir path = try
  810. let file = Unix.readdir dir in
  811. if file <> "." && file <> ".." then begin
  812. let filepath = path ^ "/" ^ file in
  813. if (Unix.stat filepath).st_kind = S_DIR then
  814. let pack = pack @ [file] in
  815. iter_files (pack) (Unix.opendir filepath) filepath;
  816. try Unix.rmdir filepath with Unix.Unix_error (ENOTEMPTY,_,_) -> ();
  817. else if not (String.ends_with filepath ".meta") && not (List.mem (unique_full_path filepath) excludes) then begin
  818. if verbose then print_endline ("Removing " ^ filepath);
  819. Sys.remove filepath
  820. end
  821. end;
  822. iter_files pack dir path
  823. with | End_of_file | Unix.Unix_error _ ->
  824. Unix.closedir dir
  825. in
  826. iter_files [] (Unix.opendir path) path
  827. let dump_descriptor gen name path_s module_s =
  828. let w = SourceWriter.new_source_writer () in
  829. (* dump called path *)
  830. SourceWriter.write w (Sys.getcwd());
  831. SourceWriter.newline w;
  832. (* dump all defines. deprecated *)
  833. SourceWriter.write w "begin defines";
  834. SourceWriter.newline w;
  835. PMap.iter (fun name _ ->
  836. SourceWriter.write w name;
  837. SourceWriter.newline w
  838. ) gen.gcon.defines;
  839. SourceWriter.write w "end defines";
  840. SourceWriter.newline w;
  841. (* dump all defines with their values; keeping the old defines for compatibility *)
  842. SourceWriter.write w "begin defines_data";
  843. SourceWriter.newline w;
  844. PMap.iter (fun name v ->
  845. SourceWriter.write w name;
  846. SourceWriter.write w "=";
  847. SourceWriter.write w v;
  848. SourceWriter.newline w
  849. ) gen.gcon.defines;
  850. SourceWriter.write w "end defines_data";
  851. SourceWriter.newline w;
  852. (* dump all generated types *)
  853. SourceWriter.write w "begin modules";
  854. SourceWriter.newline w;
  855. let main_paths = Hashtbl.create 0 in
  856. List.iter (fun md_def ->
  857. SourceWriter.write w "M ";
  858. SourceWriter.write w (path_s (path_of_md_def md_def));
  859. SourceWriter.newline w;
  860. List.iter (fun m ->
  861. match m with
  862. | TClassDecl cl when not cl.cl_extern ->
  863. SourceWriter.write w "C ";
  864. let s = module_s m in
  865. Hashtbl.add main_paths cl.cl_path s;
  866. SourceWriter.write w (s);
  867. SourceWriter.newline w
  868. | TEnumDecl e when not e.e_extern ->
  869. SourceWriter.write w "E ";
  870. SourceWriter.write w (module_s m);
  871. SourceWriter.newline w
  872. | _ -> () (* still no typedef or abstract is generated *)
  873. ) md_def.m_types
  874. ) gen.gcon.modules;
  875. SourceWriter.write w "end modules";
  876. SourceWriter.newline w;
  877. (* dump all resources *)
  878. (match gen.gcon.main_class with
  879. | Some path ->
  880. SourceWriter.write w "begin main";
  881. SourceWriter.newline w;
  882. (try
  883. SourceWriter.write w (Hashtbl.find main_paths path)
  884. with
  885. | Not_found -> SourceWriter.write w (path_s path));
  886. SourceWriter.newline w;
  887. SourceWriter.write w "end main";
  888. SourceWriter.newline w
  889. | _ -> ()
  890. );
  891. SourceWriter.write w "begin resources";
  892. SourceWriter.newline w;
  893. Hashtbl.iter (fun name _ ->
  894. SourceWriter.write w name;
  895. SourceWriter.newline w
  896. ) gen.gcon.resources;
  897. SourceWriter.write w "end resources";
  898. SourceWriter.newline w;
  899. SourceWriter.write w "begin libs";
  900. SourceWriter.newline w;
  901. let path file ext =
  902. if Sys.file_exists file then
  903. file
  904. else try Common.find_file gen.gcon file with
  905. | Not_found -> try Common.find_file gen.gcon (file ^ ext) with
  906. | Not_found ->
  907. file
  908. in
  909. if Common.platform gen.gcon Java then
  910. List.iter (fun (s,std,_,_,_) ->
  911. if not std then begin
  912. SourceWriter.write w (path s ".jar");
  913. SourceWriter.newline w;
  914. end
  915. ) gen.gcon.java_libs
  916. else if Common.platform gen.gcon Cs then
  917. List.iter (fun (s,std,_,_) ->
  918. if not std then begin
  919. SourceWriter.write w (path s ".dll");
  920. SourceWriter.newline w;
  921. end
  922. ) gen.gcon.net_libs;
  923. SourceWriter.write w "end libs";
  924. SourceWriter.newline w;
  925. let args = gen.gcon.c_args in
  926. if args <> [] then begin
  927. SourceWriter.write w "begin opts";
  928. SourceWriter.newline w;
  929. List.iter (fun opt -> SourceWriter.write w opt; SourceWriter.newline w) (List.rev args);
  930. SourceWriter.write w "end opts";
  931. SourceWriter.newline w;
  932. end;
  933. let contents = SourceWriter.contents w in
  934. let f = open_out (gen.gcon.file ^ "/" ^ name) in
  935. output_string f contents;
  936. close_out f
  937. let path_regex = Str.regexp "[/\\]+"
  938. let normalize path =
  939. let rec normalize acc m = match m with
  940. | [] ->
  941. List.rev acc
  942. | Str.Text "." :: Str.Delim _ :: tl when acc = [] ->
  943. normalize [] tl
  944. | Str.Text ".." :: Str.Delim _ :: tl -> (match acc with
  945. | [] -> raise Exit
  946. | _ :: acc -> normalize acc tl)
  947. | Str.Text t :: Str.Delim _ :: tl ->
  948. normalize (t :: acc) tl
  949. | Str.Delim _ :: tl ->
  950. normalize ("" :: acc) tl
  951. | Str.Text t :: [] ->
  952. List.rev (t :: acc)
  953. | Str.Text _ :: Str.Text _ :: _ -> assert false
  954. in
  955. String.concat "/" (normalize [] (Str.full_split path_regex path))
  956. let is_relative cwd rel =
  957. try
  958. let rel = normalize rel in
  959. Filename.is_relative rel || (String.starts_with rel cwd || String.starts_with (Common.unique_full_path rel) cwd)
  960. with | Exit ->
  961. String.starts_with rel cwd || String.starts_with (Common.unique_full_path rel) cwd
  962. (*
  963. helper function to create the source structure. Will send each module_def to the function passed.
  964. If received true, it means that module_gen has generated this content, so the file must be saved.
  965. See that it will write a whole module
  966. *)
  967. let generate_modules gen extension source_dir (module_gen : SourceWriter.source_writer->module_def->bool) out_files =
  968. let cwd = Common.unique_full_path (Sys.getcwd()) in
  969. List.iter (fun md_def ->
  970. let source_dir =
  971. if Common.defined gen.gcon Define.UnityStdTarget then
  972. let file = md_def.m_extra.m_file in
  973. let file = if file = "" then "." else file in
  974. if is_relative cwd file then
  975. let base_path = try
  976. let last = Str.search_backward path_regex file (String.length file - 1) in
  977. String.sub file 0 last
  978. with | Not_found ->
  979. "."
  980. in
  981. match List.rev (fst md_def.m_path) with
  982. | "editor" :: _ ->
  983. base_path ^ "/" ^ gen.gcon.file ^ "/Editor"
  984. | _ ->
  985. base_path ^ "/" ^ gen.gcon.file
  986. else match List.rev (fst md_def.m_path) with
  987. | "editor" :: _ ->
  988. Common.defined_value gen.gcon Define.UnityStdTarget ^ "/Editor/" ^ (String.concat "/" (fst md_def.m_path))
  989. | _ ->
  990. Common.defined_value gen.gcon Define.UnityStdTarget ^ "/Haxe-Std/" ^ (String.concat "/" (fst md_def.m_path))
  991. else
  992. gen.gcon.file ^ "/" ^ source_dir ^ "/" ^ (String.concat "/" (fst (path_of_md_def md_def)))
  993. in
  994. let w = SourceWriter.new_source_writer () in
  995. (*let should_write = List.fold_left (fun should md -> module_gen w md or should) false md_def.m_types in*)
  996. let should_write = module_gen w md_def in
  997. if should_write then begin
  998. let path = path_of_md_def md_def in
  999. write_file gen w source_dir path extension out_files
  1000. end
  1001. ) gen.gcon.modules
  1002. let generate_modules_t gen extension source_dir change_path (module_gen : SourceWriter.source_writer->module_type->bool) out_files =
  1003. let source_dir = gen.gcon.file ^ "/" ^ source_dir in
  1004. List.iter (fun md ->
  1005. let w = SourceWriter.new_source_writer () in
  1006. (*let should_write = List.fold_left (fun should md -> module_gen w md or should) false md_def.m_types in*)
  1007. let should_write = module_gen w md in
  1008. if should_write then begin
  1009. let path = change_path (t_path md) in
  1010. write_file gen w (source_dir ^ "/" ^ (String.concat "/" (fst path))) path extension out_files;
  1011. end
  1012. ) gen.gcon.types
  1013. (*
  1014. various helper functions
  1015. *)
  1016. let mk_paren e =
  1017. match e.eexpr with | TParenthesis _ -> e | _ -> { e with eexpr=TParenthesis(e) }
  1018. (* private *)
  1019. let tmp_count = ref 0
  1020. let get_real_fun gen t =
  1021. match follow t with
  1022. | TFun(args,t) -> TFun(List.map (fun (n,o,t) -> n,o,gen.greal_type t) args, gen.greal_type t)
  1023. | _ -> t
  1024. let mk_int gen i pos = { eexpr = TConst(TInt ( Int32.of_int i)); etype = gen.gcon.basic.tint; epos = pos }
  1025. let mk_return e = { eexpr = TReturn (Some e); etype = e.etype; epos = e.epos }
  1026. let mk_temp gen name t =
  1027. incr tmp_count;
  1028. let name = gen.gmk_internal_name "temp" (name ^ (string_of_int !tmp_count)) in
  1029. alloc_var name t
  1030. let v_nativearray = alloc_var "__array__" t_dynamic
  1031. let mk_nativearray_decl gen t el pos =
  1032. {
  1033. eexpr = TCall(mk_local v_nativearray pos, el);
  1034. etype = gen.gclasses.nativearray t;
  1035. epos = pos;
  1036. }
  1037. let ensure_local gen block name e =
  1038. match e.eexpr with
  1039. | TLocal _ -> e
  1040. | _ ->
  1041. let var = mk_temp gen name e.etype in
  1042. block := { e with eexpr = TVar(var, Some e); etype = gen.gcon.basic.tvoid; } :: !block;
  1043. { e with eexpr = TLocal var }
  1044. let reset_temps () = tmp_count := 0
  1045. let follow_module follow_func md = match md with
  1046. | TClassDecl _
  1047. | TEnumDecl _
  1048. | TAbstractDecl _ -> md
  1049. | TTypeDecl tdecl -> match (follow_func (TType(tdecl, List.map snd tdecl.t_params))) with
  1050. | TInst(cl,_) -> TClassDecl cl
  1051. | TEnum(e,_) -> TEnumDecl e
  1052. | TType(t,_) -> TTypeDecl t
  1053. | TAbstract(a,_) -> TAbstractDecl a
  1054. | _ -> assert false
  1055. (*
  1056. hxgen means if the type was generated by haxe. If a type was generated by haxe, it means
  1057. it will contain special constructs for speedy reflection, for example
  1058. @see SetHXGen module
  1059. *)
  1060. let rec is_hxgen md =
  1061. match md with
  1062. | TClassDecl cl -> Meta.has Meta.HxGen cl.cl_meta
  1063. | TEnumDecl e -> Meta.has Meta.HxGen e.e_meta
  1064. | 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 )
  1065. | TAbstractDecl a -> Meta.has Meta.HxGen a.a_meta
  1066. let is_hxgen_t t =
  1067. match t with
  1068. | TInst (cl, _) -> Meta.has Meta.HxGen cl.cl_meta
  1069. | TEnum (e, _) -> Meta.has Meta.HxGen e.e_meta
  1070. | TAbstract (a, _) -> Meta.has Meta.HxGen a.a_meta
  1071. | TType (t, _) -> Meta.has Meta.HxGen t.t_meta
  1072. | _ -> false
  1073. let mt_to_t_dyn md =
  1074. match md with
  1075. | TClassDecl cl -> TInst(cl, List.map (fun _ -> t_dynamic) cl.cl_params)
  1076. | TEnumDecl e -> TEnum(e, List.map (fun _ -> t_dynamic) e.e_params)
  1077. | TAbstractDecl a -> TAbstract(a, List.map (fun _ -> t_dynamic) a.a_params)
  1078. | TTypeDecl t -> TType(t, List.map (fun _ -> t_dynamic) t.t_params)
  1079. let mt_to_t mt params =
  1080. match mt with
  1081. | TClassDecl (cl) -> TInst(cl, params)
  1082. | TEnumDecl (e) -> TEnum(e, params)
  1083. | TAbstractDecl a -> TAbstract(a, params)
  1084. | _ -> assert false
  1085. let t_to_mt t =
  1086. match follow t with
  1087. | TInst(cl, _) -> TClassDecl(cl)
  1088. | TEnum(e, _) -> TEnumDecl(e)
  1089. | TAbstract(a, _) -> TAbstractDecl a
  1090. | _ -> assert false
  1091. let rec get_last_ctor cl =
  1092. 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
  1093. let add_constructor cl cf =
  1094. match cl.cl_constructor with
  1095. | None -> cl.cl_constructor <- Some cf
  1096. | Some ctor ->
  1097. if ctor != cf && not (List.memq cf ctor.cf_overloads) then
  1098. ctor.cf_overloads <- cf :: ctor.cf_overloads
  1099. (* replace open TMonos with TDynamic *)
  1100. let rec replace_mono t =
  1101. match t with
  1102. | TMono t ->
  1103. (match !t with
  1104. | None -> t := Some t_dynamic
  1105. | Some _ -> ())
  1106. | TEnum (_,p) | TInst (_,p) | TType (_,p) | TAbstract (_,p) ->
  1107. List.iter replace_mono p
  1108. | TFun (args,ret) ->
  1109. List.iter (fun (_,_,t) -> replace_mono t) args;
  1110. replace_mono ret
  1111. | TAnon _
  1112. | TDynamic _ -> ()
  1113. | TLazy f ->
  1114. replace_mono (!f())
  1115. (* helper *)
  1116. let mk_class_field name t public pos kind params =
  1117. {
  1118. cf_name = name;
  1119. cf_type = t;
  1120. cf_public = public;
  1121. cf_pos = pos;
  1122. cf_doc = None;
  1123. cf_meta = [ Meta.CompilerGenerated, [], Ast.null_pos ]; (* annotate that this class field was generated by the compiler *)
  1124. cf_kind = kind;
  1125. cf_params = params;
  1126. cf_expr = None;
  1127. cf_overloads = [];
  1128. }
  1129. (* this helper just duplicates the type parameter class, which is assumed that cl is. *)
  1130. (* This is so we can use class parameters on function parameters, without running the risk of name clash *)
  1131. (* between both *)
  1132. let map_param cl =
  1133. let ret = mk_class cl.cl_module (fst cl.cl_path, snd cl.cl_path ^ "_c") cl.cl_pos in
  1134. ret.cl_implements <- cl.cl_implements;
  1135. ret.cl_kind <- cl.cl_kind;
  1136. ret
  1137. let get_cl_t t =
  1138. match follow t with | TInst (cl,_) -> cl | _ -> assert false
  1139. let mk_class m path pos =
  1140. let cl = Type.mk_class m path pos in
  1141. cl.cl_meta <- [ Meta.CompilerGenerated, [], Ast.null_pos ];
  1142. cl
  1143. type tfield_access =
  1144. | 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 *)
  1145. | FEnumField of tenum * tenum_field * bool (* is parameterized enum ? *)
  1146. | FAnonField of tclass_field
  1147. | FDynamicField of t
  1148. | FNotFound
  1149. let is_var f = match f.cf_kind with | Var _ -> true | _ -> false
  1150. let find_first_declared_field gen orig_cl ?exact_field field =
  1151. let chosen = ref None in
  1152. let is_overload = ref false in
  1153. let rec loop_cl depth c tl tlch =
  1154. (try
  1155. let ret = PMap.find field c.cl_fields in
  1156. if Meta.has Meta.Overload ret.cf_meta then is_overload := true;
  1157. match !chosen, exact_field with
  1158. | Some(d,f,_,_,_), _ when depth <= d || (is_var ret && not (is_var f)) -> ()
  1159. | _, None ->
  1160. chosen := Some(depth,ret,c,tl,tlch)
  1161. | _, Some f2 ->
  1162. List.iter (fun f ->
  1163. let declared_t = apply_params c.cl_params tl f.cf_type in
  1164. if Typeload.same_overload_args declared_t f2.cf_type f f2 then
  1165. chosen := Some(depth,f,c,tl,tlch)
  1166. ) (ret :: ret.cf_overloads)
  1167. with | Not_found -> ());
  1168. (match c.cl_super with
  1169. | Some (sup,stl) ->
  1170. let tl = List.map (apply_params c.cl_params tl) stl in
  1171. let stl = gen.greal_type_param (TClassDecl sup) stl in
  1172. let tlch = List.map (apply_params c.cl_params tlch) stl in
  1173. loop_cl (depth+1) sup tl tlch
  1174. | None -> ());
  1175. if c.cl_interface then
  1176. List.iter (fun (sup,stl) ->
  1177. let tl = List.map (apply_params c.cl_params tl) stl in
  1178. let stl = gen.greal_type_param (TClassDecl sup) stl in
  1179. let tlch = List.map (apply_params c.cl_params tlch) stl in
  1180. loop_cl (depth+1) sup tl tlch
  1181. ) c.cl_implements
  1182. in
  1183. loop_cl 0 orig_cl (List.map snd orig_cl.cl_params) (List.map snd orig_cl.cl_params);
  1184. match !chosen with
  1185. | None -> None
  1186. | Some(_,f,c,tl,tlch) ->
  1187. if !is_overload && not (Meta.has Meta.Overload f.cf_meta) then
  1188. f.cf_meta <- (Meta.Overload,[],f.cf_pos) :: f.cf_meta;
  1189. let declared_t = apply_params c.cl_params tl f.cf_type in
  1190. let params_t = apply_params c.cl_params tlch f.cf_type in
  1191. let actual_t = match follow params_t with
  1192. | TFun(args,ret) -> TFun(List.map (fun (n,o,t) -> (n,o,gen.greal_type t)) args, gen.greal_type ret)
  1193. | _ -> gen.greal_type params_t in
  1194. Some(f,actual_t,declared_t,params_t,c,tl,tlch)
  1195. let field_access gen (t:t) (field:string) : (tfield_access) =
  1196. (*
  1197. t can be either an haxe-type as a real-type;
  1198. 'follow' should be applied here since we can generalize that a TType will be accessible as its
  1199. underlying type.
  1200. *)
  1201. (* let pointers to values be accessed as the underlying values *)
  1202. let t = match gen.greal_type t with
  1203. | TAbstract({ a_path = ["cs"],"Pointer" },[t]) ->
  1204. gen.greal_type t
  1205. | _ -> t
  1206. in
  1207. match follow t with
  1208. | TInst(cl, params) ->
  1209. let orig_cl = cl in
  1210. let orig_params = params in
  1211. let rec not_found cl params =
  1212. match cl.cl_dynamic with
  1213. | Some t ->
  1214. let t = apply_params cl.cl_params params t in
  1215. FDynamicField t
  1216. | None ->
  1217. match cl.cl_super with
  1218. | None -> FNotFound
  1219. | Some (super,p) -> not_found super p
  1220. in
  1221. let not_found () =
  1222. try
  1223. let cf = PMap.find field gen.gbase_class_fields in
  1224. FClassField (orig_cl, orig_params, gen.gclasses.cl_dyn, cf, false, cf.cf_type, cf.cf_type)
  1225. with
  1226. | Not_found -> not_found cl params
  1227. in
  1228. (* this is a hack for C#'s different generic types with same path *)
  1229. let hashtbl_field = (String.concat "" (List.map (fun _ -> "]") cl.cl_params)) ^ field in
  1230. let types = try
  1231. Hashtbl.find gen.greal_field_types (orig_cl.cl_path, hashtbl_field)
  1232. with | Not_found ->
  1233. let ret = find_first_declared_field gen cl field in
  1234. let ret = match ret with
  1235. | None -> None
  1236. | Some(cf,t,dt,_,cl,_,_) -> Some(cf,t,dt,cl)
  1237. in
  1238. if ret <> None then Hashtbl.add gen.greal_field_types (orig_cl.cl_path, hashtbl_field) ret;
  1239. ret
  1240. in
  1241. (match types with
  1242. | None -> not_found()
  1243. | Some (cf, actual_t, declared_t, declared_cl) ->
  1244. FClassField(orig_cl, orig_params, declared_cl, cf, false, actual_t, declared_t))
  1245. | TEnum _ | TAbstract _ ->
  1246. (* enums have no field *) FNotFound
  1247. | TAnon anon ->
  1248. (try match !(anon.a_status) with
  1249. | Statics cl ->
  1250. let cf = PMap.find field cl.cl_statics in
  1251. FClassField(cl, List.map (fun _ -> t_dynamic) cl.cl_params, cl, cf, true, cf.cf_type, cf.cf_type)
  1252. | EnumStatics e ->
  1253. let f = PMap.find field e.e_constrs in
  1254. let is_param = match follow f.ef_type with | TFun _ -> true | _ -> false in
  1255. FEnumField(e, f, is_param)
  1256. | _ when PMap.mem field gen.gbase_class_fields ->
  1257. let cf = PMap.find field gen.gbase_class_fields in
  1258. FClassField(gen.gclasses.cl_dyn, [t_dynamic], gen.gclasses.cl_dyn, cf, false, cf.cf_type, cf.cf_type)
  1259. | _ ->
  1260. FAnonField(PMap.find field anon.a_fields)
  1261. with | Not_found -> FNotFound)
  1262. | _ when PMap.mem field gen.gbase_class_fields ->
  1263. let cf = PMap.find field gen.gbase_class_fields in
  1264. FClassField(gen.gclasses.cl_dyn, [t_dynamic], gen.gclasses.cl_dyn, cf, false, cf.cf_type, cf.cf_type)
  1265. | TDynamic t -> FDynamicField t
  1266. | TMono _ -> FDynamicField t_dynamic
  1267. | _ -> FNotFound
  1268. let field_access_esp gen t field = match field with
  1269. | FStatic(cl,cf) | FInstance(cl,_,cf) when Meta.has Meta.Extern cf.cf_meta ->
  1270. let static = match field with
  1271. | FStatic _ -> true
  1272. | _ -> false
  1273. in
  1274. let p = match follow (run_follow gen t) with
  1275. | TInst(_,p) -> p
  1276. | _ -> List.map snd cl.cl_params
  1277. in
  1278. FClassField(cl,p,cl,cf,static,cf.cf_type,cf.cf_type)
  1279. | _ -> field_access gen t (field_name field)
  1280. let mk_field_access gen expr field pos =
  1281. match field_access gen expr.etype field with
  1282. | FClassField(c,p,dc,cf,false,at,_) ->
  1283. { eexpr = TField(expr, FInstance(dc,p,cf)); etype = apply_params c.cl_params p at; epos = pos }
  1284. | FClassField(c,p,dc,cf,true,at,_) ->
  1285. { eexpr = TField(expr, FStatic(dc,cf)); etype = at; epos = pos }
  1286. | FAnonField cf ->
  1287. { eexpr = TField(expr, FAnon cf); etype = cf.cf_type; epos = pos }
  1288. | FDynamicField t ->
  1289. { eexpr = TField(expr, FDynamic field); etype = t; epos = pos }
  1290. | FNotFound ->
  1291. { eexpr = TField(expr, FDynamic field); etype = t_dynamic; epos = pos }
  1292. | FEnumField _ -> assert false
  1293. let mk_iterator_access gen t expr =
  1294. let pos = expr.epos in
  1295. let itf = mk_field_access gen expr "iterator" pos in
  1296. { eexpr = TCall(itf, []); epos = pos; etype = snd (get_fun itf.etype) }
  1297. (* ******************************************* *)
  1298. (* Module dependency resolution *)
  1299. (* ******************************************* *)
  1300. type t_dependency =
  1301. | DAfter of float
  1302. | DBefore of float
  1303. exception ImpossibleDependency of string
  1304. let max_dep = 10000.0
  1305. let min_dep = - (10000.0)
  1306. let solve_deps name (deps:t_dependency list) =
  1307. let vmin = min_dep -. 1.0 in
  1308. let vmax = max_dep +. 1.0 in
  1309. let rec loop dep vmin vmax =
  1310. match dep with
  1311. | [] ->
  1312. (if vmin >= vmax then raise (ImpossibleDependency name));
  1313. (vmin +. vmax) /. 2.0
  1314. | head :: tail ->
  1315. match head with
  1316. | DBefore f ->
  1317. loop tail (max vmin f) vmax
  1318. | DAfter f ->
  1319. loop tail vmin (min vmax f)
  1320. in
  1321. loop deps vmin vmax
  1322. (* type resolution *)
  1323. exception TypeNotFound of path
  1324. let get_type gen path =
  1325. try Hashtbl.find gen.gtypes path with | Not_found -> raise (TypeNotFound path)
  1326. (* ******************************************* *)
  1327. (* follow all module *)
  1328. (* ******************************************* *)
  1329. (*
  1330. this module will follow each and every type using the rules defined in
  1331. gen.gfollow. This is a minor helper module, so we don't end up
  1332. having to follow the same time multiple times in the many filter iterations
  1333. because of this, it will be one of the first modules to run.
  1334. *)
  1335. module FollowAll =
  1336. struct
  1337. let follow gen e =
  1338. let follow_func = gen.gfollow#run_f in
  1339. Some (Type.map_expr_type (fun e->e) (follow_func) (fun tvar-> tvar.v_type <- (follow_func tvar.v_type); tvar) e)
  1340. let priority = max_dep
  1341. (* will add an expression filter as the first filter *)
  1342. let configure gen =
  1343. gen.gexpr_filters#add ~name:"follow_all" ~priority:(PCustom(priority)) (follow gen)
  1344. end;;
  1345. (* ******************************************* *)
  1346. (* set hxgen module *)
  1347. (* ******************************************* *)
  1348. (*
  1349. goes through all module types and sets the :hxgen meta on all which
  1350. then is_hxgen_func returns true. There is a default is_hxgen_func implementation also
  1351. *)
  1352. module SetHXGen =
  1353. struct
  1354. (*
  1355. basically, everything that is extern is assumed to not be hxgen, unless meta :hxgen is set, and
  1356. everything that is not extern is assumed to be hxgen, unless meta :nativegen is set
  1357. *)
  1358. let rec default_hxgen_func md =
  1359. match md with
  1360. | TClassDecl { cl_kind = KAbstractImpl a } ->
  1361. default_hxgen_func (TAbstractDecl a)
  1362. | TClassDecl cl ->
  1363. let rec is_hxgen_class (c,_) =
  1364. if c.cl_extern then begin
  1365. 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
  1366. end else begin
  1367. if Meta.has Meta.NativeChildren c.cl_meta || Meta.has Meta.NativeGen c.cl_meta then
  1368. Option.map_default (is_hxgen_class) false c.cl_super || List.exists is_hxgen_class c.cl_implements
  1369. else
  1370. let rec has_nativec (c,p) =
  1371. if is_hxgen_class (c,p) then
  1372. false
  1373. else
  1374. (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))
  1375. || Option.map_default has_nativec false c.cl_super
  1376. in
  1377. if Option.map_default has_nativec false c.cl_super && not (List.exists is_hxgen_class c.cl_implements) then
  1378. false
  1379. else
  1380. true
  1381. end
  1382. in
  1383. is_hxgen_class (cl,[])
  1384. | TEnumDecl e -> if e.e_extern then Meta.has Meta.HxGen e.e_meta else not (Meta.has Meta.NativeGen e.e_meta)
  1385. | TAbstractDecl a when Meta.has Meta.CoreType a.a_meta -> not (Meta.has Meta.NativeGen a.a_meta)
  1386. | TAbstractDecl a -> (match follow a.a_this with
  1387. | TInst _ | TEnum _ | TAbstract _ ->
  1388. default_hxgen_func (t_to_md (follow a.a_this))
  1389. | _ ->
  1390. Meta.has Meta.NativeGen a.a_meta)
  1391. | TTypeDecl t -> (* TODO see when would we use this *)
  1392. false
  1393. (*
  1394. by now the only option is to run it eagerly, because it must be one of the first filters to run,
  1395. since many others depend of it
  1396. *)
  1397. let run_filter gen is_hxgen_func =
  1398. let filter md =
  1399. let meta = if is_hxgen_func md then Meta.HxGen else Meta.NativeGen in
  1400. begin
  1401. match md with
  1402. | TClassDecl cl -> cl.cl_meta <- (meta, [], cl.cl_pos) :: cl.cl_meta
  1403. | TEnumDecl e -> e.e_meta <- (meta, [], e.e_pos) :: e.e_meta
  1404. | TTypeDecl t -> t.t_meta <- (meta, [], t.t_pos) :: t.t_meta
  1405. | TAbstractDecl a -> a.a_meta <- (meta, [], a.a_pos) :: a.a_meta
  1406. end
  1407. in
  1408. List.iter filter gen.gcon.types
  1409. end;;
  1410. (* ******************************************* *)
  1411. (* overloading reflection constructors *)
  1412. (* ******************************************* *)
  1413. (*
  1414. this module works on languages that support function overloading and
  1415. enable function hiding via static functions.
  1416. it takes the constructor body out of the constructor and adds it to a special ctor
  1417. static function. The static function will receive the same parameters as the constructor,
  1418. plus the special "me" var, which will replace "this"
  1419. Then it always adds two constructors to the function: one that receives a special class,
  1420. indicating that it should be constructed without any parameters, and one that receives its normal constructor.
  1421. Both will only include a super() call to the superclasses' emtpy constructor.
  1422. This enables two things:
  1423. empty construction without the need of incompatibility with the platform's native construction method
  1424. the ability to call super() constructor in any place in the constructor
  1425. This will insert itself in the default reflection-related module filter
  1426. *)
  1427. module OverloadingConstructor =
  1428. struct
  1429. let priority = 0.0
  1430. let name = "overloading_constructor"
  1431. let set_new_create_empty gen empty_ctor_expr =
  1432. let old = gen.gtools.rf_create_empty in
  1433. gen.gtools.rf_create_empty <- (fun cl params pos ->
  1434. if is_hxgen (TClassDecl cl) then
  1435. { eexpr = TNew(cl,params,[empty_ctor_expr]); etype = TInst(cl,params); epos = pos }
  1436. else
  1437. old cl params pos
  1438. )
  1439. let rec cur_ctor c tl =
  1440. match c.cl_constructor with
  1441. | Some ctor -> ctor, c, tl
  1442. | None -> match c.cl_super with
  1443. | None -> raise Not_found
  1444. | Some (sup,stl) ->
  1445. cur_ctor sup (List.map (apply_params c.cl_params tl) stl)
  1446. let rec prev_ctor c tl =
  1447. match c.cl_super with
  1448. | None -> raise Not_found
  1449. | Some (sup,stl) ->
  1450. let stl = List.map (apply_params c.cl_params tl) stl in
  1451. match sup.cl_constructor with
  1452. | None -> prev_ctor sup stl
  1453. | Some ctor -> ctor, sup, stl
  1454. (* replaces super() call with last static constructor call *)
  1455. let replace_super_call gen name c tl with_params me p =
  1456. let rec loop_super c tl = match c.cl_super with
  1457. | None -> raise Not_found
  1458. | Some(sup,stl) ->
  1459. let stl = List.map (apply_params c.cl_params tl) stl in
  1460. try
  1461. let static_ctor_name = name ^ "_" ^ (String.concat "_" (fst sup.cl_path)) ^ "_" ^ (snd sup.cl_path) in
  1462. sup, stl, PMap.find static_ctor_name sup.cl_statics
  1463. with | Not_found ->
  1464. loop_super sup stl
  1465. in
  1466. let sup, stl, cf = loop_super c tl in
  1467. let with_params = { eexpr = TLocal me; etype = me.v_type; epos = p } :: with_params in
  1468. let cf = match cf.cf_overloads with
  1469. (* | [] -> cf *)
  1470. | _ -> try
  1471. (* choose best super function *)
  1472. List.iter (fun e -> replace_mono e.etype) with_params;
  1473. List.find (fun cf ->
  1474. replace_mono cf.cf_type;
  1475. let args, _ = get_fun (apply_params cf.cf_params stl cf.cf_type) in
  1476. try
  1477. List.for_all2 (fun (_,_,t) e -> try
  1478. let e_etype = run_follow gen e.etype in
  1479. let t = run_follow gen t in
  1480. unify e_etype t; true
  1481. with | Unify_error _ -> false) args with_params
  1482. with | Invalid_argument("List.for_all2") -> false
  1483. ) (cf :: cf.cf_overloads)
  1484. with | Not_found ->
  1485. gen.gcon.error "No suitable overload for the super call arguments was found" p; cf
  1486. in
  1487. {
  1488. eexpr = TCall({
  1489. eexpr = TField(
  1490. mk_classtype_access sup p,
  1491. FStatic(sup,cf));
  1492. etype = apply_params cf.cf_params stl cf.cf_type;
  1493. epos = p},
  1494. with_params);
  1495. etype = gen.gcon.basic.tvoid;
  1496. epos = p;
  1497. }
  1498. (* will create a static counterpart of 'ctor', and replace its contents to a call to the static version*)
  1499. let create_static_ctor gen ~empty_ctor_expr cl name ctor =
  1500. match Meta.has Meta.SkipCtor ctor.cf_meta with
  1501. | true -> ()
  1502. | false when is_none ctor.cf_expr -> ()
  1503. | false ->
  1504. let static_ctor_name = name ^ "_" ^ (String.concat "_" (fst cl.cl_path)) ^ "_" ^ (snd cl.cl_path) in
  1505. (* create the static constructor *)
  1506. let basic = gen.gcon.basic in
  1507. let ctor_types = List.map (fun (s,t) -> (s, TInst(map_param (get_cl_t t), []))) cl.cl_params in
  1508. let me = mk_temp gen "me" (TInst(cl, List.map snd ctor_types)) in
  1509. me.v_capture <- true;
  1510. let fn_args, _ = get_fun ctor.cf_type in
  1511. let ctor_params = List.map snd ctor_types in
  1512. 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
  1513. let cur_tf_args = match ctor.cf_expr with
  1514. | Some { eexpr = TFunction(tf) } -> tf.tf_args
  1515. | _ -> assert false
  1516. in
  1517. let changed_tf_args = List.map (fun (v,_) -> (v,None)) cur_tf_args in
  1518. let local_map = Hashtbl.create (List.length cur_tf_args) in
  1519. let static_tf_args = (me, None) :: List.map (fun (v,b) ->
  1520. let new_v = alloc_var v.v_name (apply_params cl.cl_params ctor_params v.v_type) in
  1521. new_v.v_capture <- v.v_capture;
  1522. Hashtbl.add local_map v.v_id new_v;
  1523. (new_v, b)
  1524. ) cur_tf_args in
  1525. let static_ctor = mk_class_field static_ctor_name fn_type false ctor.cf_pos (Method MethNormal) ctor_types in
  1526. (* change ctor contents to reference the 'me' var instead of 'this' *)
  1527. let actual_super_call = ref None in
  1528. let rec map_expr ~is_first e = match e.eexpr with
  1529. | TCall (({ eexpr = TConst TSuper } as tsuper), params) -> (try
  1530. let params = List.map (fun e -> map_expr ~is_first:false e) params in
  1531. actual_super_call := Some { e with eexpr = TCall(tsuper, [empty_ctor_expr]) };
  1532. replace_super_call gen name cl ctor_params params me e.epos
  1533. with | Not_found ->
  1534. (* last static function was not found *)
  1535. actual_super_call := Some e;
  1536. if not is_first then
  1537. gen.gcon.error "Super call must be the first call when extending native types" e.epos;
  1538. { e with eexpr = TBlock([]) })
  1539. | TFunction tf when is_first ->
  1540. do_map ~is_first:true e
  1541. | TConst TThis ->
  1542. mk_local me e.epos
  1543. | TBlock (fst :: bl) ->
  1544. let fst = map_expr ~is_first:is_first fst in
  1545. { 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 }
  1546. | _ ->
  1547. do_map e
  1548. and do_map ?(is_first=false) e =
  1549. let do_t = apply_params cl.cl_params ctor_params in
  1550. let do_v v = try
  1551. Hashtbl.find local_map v.v_id
  1552. with | Not_found ->
  1553. v.v_type <- do_t v.v_type; v
  1554. in
  1555. Type.map_expr_type (map_expr ~is_first:is_first) do_t do_v e
  1556. in
  1557. let expr = do_map ~is_first:true (get ctor.cf_expr) in
  1558. let expr = match expr.eexpr with
  1559. | TFunction(tf) ->
  1560. { expr with etype = fn_type; eexpr = TFunction({ tf with tf_args = static_tf_args }) }
  1561. | _ -> assert false in
  1562. static_ctor.cf_expr <- Some expr;
  1563. (* add to the statics *)
  1564. (try
  1565. let stat = PMap.find static_ctor_name cl.cl_statics in
  1566. stat.cf_overloads <- static_ctor :: stat.cf_overloads
  1567. with | Not_found ->
  1568. cl.cl_ordered_statics <- static_ctor :: cl.cl_ordered_statics;
  1569. cl.cl_statics <- PMap.add static_ctor_name static_ctor cl.cl_statics);
  1570. (* change current super call *)
  1571. match ctor.cf_expr with
  1572. | Some({ eexpr = TFunction(tf) } as e) ->
  1573. let block_contents, p = match !actual_super_call with
  1574. | None -> [], ctor.cf_pos
  1575. | Some super -> [super], super.epos
  1576. in
  1577. let block_contents = block_contents @ [{
  1578. eexpr = TCall(
  1579. {
  1580. eexpr = TField(
  1581. mk_classtype_access cl p,
  1582. FStatic(cl, static_ctor));
  1583. etype = apply_params static_ctor.cf_params (List.map snd cl.cl_params) static_ctor.cf_type;
  1584. epos = p
  1585. },
  1586. [{ eexpr = TConst TThis; etype = TInst(cl, List.map snd cl.cl_params); epos = p }]
  1587. @ List.map (fun (v,_) -> mk_local v p) cur_tf_args
  1588. );
  1589. etype = basic.tvoid;
  1590. epos = p
  1591. }] in
  1592. 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 }) }
  1593. | _ -> assert false
  1594. (* makes constructors that only call super() for the 'ctor' argument *)
  1595. let clone_ctors gen ctor sup stl cl =
  1596. let basic = gen.gcon.basic in
  1597. let rec clone cf =
  1598. 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
  1599. let args, ret = get_fun ncf.cf_type in
  1600. (* single expression: call to super() *)
  1601. let tf_args = List.map (fun (name,_,t) ->
  1602. (* the constructor will have no optional arguments, as presumably this will be handled by the underlying expr *)
  1603. alloc_var name t, None
  1604. ) args in
  1605. let super_call =
  1606. {
  1607. eexpr = TCall(
  1608. { eexpr = TConst TSuper; etype = TInst(cl, List.map snd cl.cl_params); epos = ctor.cf_pos },
  1609. List.map (fun (v,_) -> mk_local v ctor.cf_pos) tf_args);
  1610. etype = basic.tvoid;
  1611. epos = ctor.cf_pos;
  1612. } in
  1613. ncf.cf_expr <- Some
  1614. {
  1615. eexpr = TFunction {
  1616. tf_args = tf_args;
  1617. tf_type = basic.tvoid;
  1618. tf_expr = mk_block super_call;
  1619. };
  1620. etype = ncf.cf_type;
  1621. epos = ctor.cf_pos;
  1622. };
  1623. ncf
  1624. in
  1625. (* take off createEmpty *)
  1626. let all = List.filter (fun cf -> replace_mono cf.cf_type; not (Meta.has Meta.SkipCtor cf.cf_meta)) (ctor :: ctor.cf_overloads) in
  1627. let clones = List.map clone all in
  1628. match clones with
  1629. | [] ->
  1630. (* raise Not_found *)
  1631. assert false (* should never happen *)
  1632. | cf :: [] -> cf
  1633. | cf :: overl ->
  1634. cf.cf_meta <- (Meta.Overload,[],cf.cf_pos) :: cf.cf_meta;
  1635. cf.cf_overloads <- overl; cf
  1636. let rec descends_from_native_or_skipctor cl =
  1637. not (is_hxgen (TClassDecl cl)) || Meta.has Meta.SkipCtor cl.cl_meta || match cl.cl_super with
  1638. | None -> false
  1639. | Some(c,_) -> descends_from_native_or_skipctor c
  1640. let ensure_super_is_first gen cf =
  1641. let rec loop e =
  1642. match e.eexpr with
  1643. | TBlock (b :: block) ->
  1644. loop b
  1645. | TBlock []
  1646. | TCall({ eexpr = TConst TSuper },_) -> ()
  1647. | _ ->
  1648. 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
  1649. in
  1650. match cf.cf_expr with
  1651. | None -> ()
  1652. | Some e -> Type.iter loop e
  1653. (* major restructring made at r6493 *)
  1654. let configure ~(empty_ctor_type : t) ~(empty_ctor_expr : texpr) ~supports_ctor_inheritance gen =
  1655. set_new_create_empty gen empty_ctor_expr;
  1656. let basic = gen.gcon.basic in
  1657. 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
  1658. let static_ctor_name = gen.gmk_internal_name "hx" "ctor" in
  1659. let msize = List.length gen.gcon.types in
  1660. let processed, empty_ctors = Hashtbl.create msize, Hashtbl.create msize in
  1661. let rec get_last_empty cl =
  1662. try
  1663. Hashtbl.find empty_ctors cl.cl_path
  1664. with | Not_found ->
  1665. match cl.cl_super with
  1666. | None -> raise Not_found
  1667. | Some (sup,_) -> get_last_empty sup
  1668. in
  1669. let rec change cl =
  1670. match Hashtbl.mem processed cl.cl_path with
  1671. | true -> ()
  1672. | false ->
  1673. Hashtbl.add processed cl.cl_path true;
  1674. (* make sure we've processed the super types *)
  1675. (match cl.cl_super with
  1676. | Some (super,_) when should_change super && not (Hashtbl.mem processed super.cl_path) ->
  1677. change super
  1678. | _ -> ());
  1679. (* implement static hx_ctor and reimplement constructors *)
  1680. (try
  1681. let ctor = match cl.cl_constructor with
  1682. | Some ctor -> ctor
  1683. | None -> try
  1684. let sctor, sup, stl = prev_ctor cl (List.map snd cl.cl_params) in
  1685. (* we have a previous constructor. if we support inheritance, exit *)
  1686. if supports_ctor_inheritance then raise Exit;
  1687. (* we'll make constructors that will only call super() *)
  1688. let ctor = clone_ctors gen sctor sup stl cl in
  1689. cl.cl_constructor <- Some ctor;
  1690. ctor
  1691. with | Not_found -> (* create default constructor *)
  1692. let ctor = mk_class_field "new" (TFun([], basic.tvoid)) false cl.cl_pos (Method MethNormal) [] in
  1693. ctor.cf_expr <- Some
  1694. {
  1695. eexpr = TFunction {
  1696. tf_args = [];
  1697. tf_type = basic.tvoid;
  1698. tf_expr = { eexpr = TBlock[]; etype = basic.tvoid; epos = cl.cl_pos };
  1699. };
  1700. etype = ctor.cf_type;
  1701. epos = ctor.cf_pos;
  1702. };
  1703. cl.cl_constructor <- Some ctor;
  1704. ctor
  1705. in
  1706. (* now that we made sure we have a constructor, exit if native gen *)
  1707. if not (is_hxgen (TClassDecl cl)) || Meta.has Meta.SkipCtor cl.cl_meta then begin
  1708. if descends_from_native_or_skipctor cl && is_some cl.cl_super then
  1709. List.iter (fun cf -> ensure_super_is_first gen cf) (ctor :: ctor.cf_overloads);
  1710. raise Exit
  1711. end;
  1712. (* if cl descends from a native class, we cannot use the static constructor strategy *)
  1713. if descends_from_native_or_skipctor cl && is_some cl.cl_super then
  1714. List.iter (fun cf -> ensure_super_is_first gen cf) (ctor :: ctor.cf_overloads)
  1715. else
  1716. (* now that we have a current ctor, create the static counterparts *)
  1717. List.iter (fun cf ->
  1718. create_static_ctor gen ~empty_ctor_expr:empty_ctor_expr cl static_ctor_name cf
  1719. ) (ctor :: ctor.cf_overloads)
  1720. with | Exit ->());
  1721. (* implement empty ctor *)
  1722. (try
  1723. (* now that we made sure we have a constructor, exit if native gen *)
  1724. if not (is_hxgen (TClassDecl cl)) then raise Exit;
  1725. (* get first *)
  1726. let empty_type = TFun(["empty",false,empty_ctor_type],basic.tvoid) in
  1727. let super = match cl.cl_super with
  1728. | None -> (* implement empty *)
  1729. []
  1730. | Some (sup,_) -> try
  1731. ignore (get_last_empty sup);
  1732. if supports_ctor_inheritance && is_none cl.cl_constructor then raise Exit;
  1733. [{
  1734. eexpr = TCall(
  1735. { eexpr = TConst TSuper; etype = TInst(cl, List.map snd cl.cl_params); epos = cl.cl_pos },
  1736. [ empty_ctor_expr ]);
  1737. etype = basic.tvoid;
  1738. epos = cl.cl_pos
  1739. }]
  1740. with | Not_found -> try
  1741. (* super type is native: find super constructor with least arguments *)
  1742. let sctor, sup, stl = prev_ctor cl (List.map snd cl.cl_params) in
  1743. let rec loop remaining (best,n) =
  1744. match remaining with
  1745. | [] -> best
  1746. | cf :: r ->
  1747. let args,_ = get_fun cf.cf_type in
  1748. if (List.length args) < n then
  1749. loop r (cf,List.length args)
  1750. else
  1751. loop r (best,n)
  1752. in
  1753. let args,_ = get_fun sctor.cf_type in
  1754. let best = loop sctor.cf_overloads (sctor, List.length args) in
  1755. let args,_ = get_fun (apply_params sup.cl_params stl best.cf_type) in
  1756. [{
  1757. eexpr = TCall(
  1758. { eexpr = TConst TSuper; etype = TInst(sup, stl); epos = cl.cl_pos },
  1759. List.map (fun (n,o,t) -> null t cl.cl_pos) args);
  1760. etype = basic.tvoid;
  1761. epos = cl.cl_pos
  1762. }]
  1763. with | Not_found ->
  1764. (* extends native type, but no ctor found *)
  1765. []
  1766. in
  1767. let ctor = mk_class_field "new" empty_type false cl.cl_pos (Method MethNormal) [] in
  1768. ctor.cf_expr <- Some {
  1769. eexpr = TFunction {
  1770. tf_type = basic.tvoid;
  1771. tf_args = [alloc_var "empty" empty_ctor_type, None];
  1772. tf_expr = { eexpr = TBlock super; etype = basic.tvoid; epos = cl.cl_pos }
  1773. };
  1774. etype = empty_type;
  1775. epos = cl.cl_pos;
  1776. };
  1777. ctor.cf_meta <- [Meta.SkipCtor, [], ctor.cf_pos];
  1778. Hashtbl.add empty_ctors cl.cl_path ctor;
  1779. match cl.cl_constructor with
  1780. | None -> cl.cl_constructor <- Some ctor
  1781. | Some c -> c.cf_overloads <- ctor :: c.cf_overloads
  1782. with | Exit -> ());
  1783. in
  1784. let module_filter md = match md with
  1785. | TClassDecl cl when should_change cl && not (Hashtbl.mem processed cl.cl_path) ->
  1786. change cl;
  1787. None
  1788. | _ -> None
  1789. in
  1790. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) module_filter
  1791. end;;
  1792. (* ******************************************* *)
  1793. (* init function module *)
  1794. (* ******************************************* *)
  1795. (*
  1796. This module will take proper care of the init function, by taking off all expressions from static vars and putting them
  1797. in order in the init function.
  1798. It will also initialize dynamic functions, both by putting them in the constructor and in the init function
  1799. depends on:
  1800. (syntax) must run before ExprStatement module
  1801. (ok) must run before OverloadingCtor module so the constructor can be in the correct place
  1802. (syntax) must run before FunctionToClass module
  1803. *)
  1804. module InitFunction =
  1805. struct
  1806. let name = "init_funcs"
  1807. let priority = solve_deps name [DBefore OverloadingConstructor.priority]
  1808. let ensure_simple_expr gen e =
  1809. let rec iter e = match e.eexpr with
  1810. | TConst _ | TLocal _ | TArray _ | TBinop _
  1811. | TField _ | TTypeExpr _ | TParenthesis _ | TCast _
  1812. | TCall _ | TNew _ | TUnop _ ->
  1813. Type.iter iter e
  1814. | _ ->
  1815. print_endline (debug_expr e);
  1816. gen.gcon.error "Expression is too complex for a readonly variable initialization" e.epos
  1817. in
  1818. iter e
  1819. let configure gen should_handle_dynamic_functions readonly_support =
  1820. let handle_override_dynfun acc e this field =
  1821. let add_expr = ref None in
  1822. let v = mk_temp gen ("super_" ^ field) e.etype in
  1823. v.v_capture <- true;
  1824. let rec loop e =
  1825. match e.eexpr with
  1826. | TField({ eexpr = TConst(TSuper) }, f) ->
  1827. let n = field_name f in
  1828. (if n <> field then assert false);
  1829. let local = mk_local v e.epos in
  1830. (match !add_expr with
  1831. | None ->
  1832. add_expr := Some { e with eexpr = TVar(v, Some this) }
  1833. | Some _ -> ());
  1834. local
  1835. | TConst TSuper -> assert false
  1836. | _ -> Type.map_expr loop e
  1837. in
  1838. let e = loop e in
  1839. match !add_expr with
  1840. | None -> e :: acc
  1841. | Some add_expr -> add_expr :: e :: acc
  1842. in
  1843. let handle_class cl =
  1844. let init = match cl.cl_init with
  1845. | None -> []
  1846. | Some i -> [i]
  1847. in
  1848. let init = List.fold_left (fun acc cf ->
  1849. match cf.cf_kind, should_handle_dynamic_functions with
  1850. | (Var v, _) when Meta.has Meta.ReadOnly cf.cf_meta && readonly_support ->
  1851. if v.v_write <> AccNever then gen.gcon.warning "@:readOnly variable declared without `never` setter modifier" cf.cf_pos;
  1852. (match cf.cf_expr with
  1853. | None -> gen.gcon.warning "Uninitialized readonly variable" cf.cf_pos; acc
  1854. | Some e -> ensure_simple_expr gen e; acc)
  1855. | (Var _, _)
  1856. | (Method (MethDynamic), true) when not (Type.is_extern_field cf) ->
  1857. (match cf.cf_expr with
  1858. | Some e ->
  1859. (match cf.cf_params with
  1860. | [] ->
  1861. let var = { eexpr = TField(mk_classtype_access cl cf.cf_pos, FStatic(cl,cf)); etype = cf.cf_type; epos = cf.cf_pos } in
  1862. let ret = ({ eexpr = TBinop(Ast.OpAssign, var, e); etype = cf.cf_type; epos = cf.cf_pos; }) in
  1863. cf.cf_expr <- None;
  1864. ret :: acc
  1865. | _ ->
  1866. let params = List.map (fun _ -> t_dynamic) cf.cf_params in
  1867. let fn = apply_params cf.cf_params params in
  1868. let var = { eexpr = TField(mk_classtype_access cl cf.cf_pos, FStatic(cl,cf)); etype = fn cf.cf_type; epos = cf.cf_pos } in
  1869. let rec change_expr e =
  1870. Type.map_expr_type (change_expr) fn (fun v -> v.v_type <- fn v.v_type; v) e
  1871. in
  1872. let ret = ({ eexpr = TBinop(Ast.OpAssign, var, change_expr e); etype = fn cf.cf_type; epos = cf.cf_pos; }) in
  1873. cf.cf_expr <- None;
  1874. ret :: acc
  1875. )
  1876. | None -> acc)
  1877. | _ -> acc
  1878. ) init cl.cl_ordered_statics
  1879. in
  1880. let init = List.rev init in
  1881. (match init with
  1882. | [] -> cl.cl_init <- None
  1883. | _ -> cl.cl_init <- Some { eexpr = TBlock(init); epos = cl.cl_pos; etype = gen.gcon.basic.tvoid; });
  1884. (* FIXME: find a way to tell OverloadingCtors to execute this code even with empty constructors *)
  1885. if should_handle_dynamic_functions then begin
  1886. let vars, funs = List.fold_left (fun (acc_vars,acc_funs) cf ->
  1887. match cf.cf_kind with
  1888. | Var v when Meta.has Meta.ReadOnly cf.cf_meta && readonly_support ->
  1889. if v.v_write <> AccNever then gen.gcon.warning "@:readOnly variable declared without `never` setter modifier" cf.cf_pos;
  1890. (match cf.cf_expr with
  1891. | None -> (acc_vars,acc_funs)
  1892. | Some e -> ensure_simple_expr gen e; (acc_vars,acc_funs))
  1893. | Var _
  1894. | Method(MethDynamic) ->
  1895. let is_var = match cf.cf_kind with | Var _ -> true | _ -> false in
  1896. (match cf.cf_expr, cf.cf_params with
  1897. | Some e, [] ->
  1898. 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
  1899. let ret = ({ eexpr = TBinop(Ast.OpAssign, var, e); etype = cf.cf_type; epos = cf.cf_pos; }) in
  1900. cf.cf_expr <- None;
  1901. let is_override = List.memq cf cl.cl_overrides in
  1902. if is_override then begin
  1903. cl.cl_ordered_fields <- List.filter (fun f -> f.cf_name <> cf.cf_name) cl.cl_ordered_fields;
  1904. cl.cl_fields <- PMap.remove cf.cf_name cl.cl_fields;
  1905. acc_vars, handle_override_dynfun acc_funs ret var cf.cf_name
  1906. end else if is_var then
  1907. ret :: acc_vars, acc_funs
  1908. else
  1909. acc_vars, ret :: acc_funs
  1910. | Some e, _ ->
  1911. let params = List.map (fun _ -> t_dynamic) cf.cf_params in
  1912. let fn = apply_params cf.cf_params params in
  1913. 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
  1914. let rec change_expr e =
  1915. Type.map_expr_type (change_expr) fn (fun v -> v.v_type <- fn v.v_type; v) e
  1916. in
  1917. let ret = ({ eexpr = TBinop(Ast.OpAssign, var, change_expr e); etype = fn cf.cf_type; epos = cf.cf_pos; }) in
  1918. cf.cf_expr <- None;
  1919. let is_override = List.memq cf cl.cl_overrides in
  1920. if is_override then begin
  1921. cl.cl_ordered_fields <- List.filter (fun f -> f.cf_name <> cf.cf_name) cl.cl_ordered_fields;
  1922. cl.cl_fields <- PMap.remove cf.cf_name cl.cl_fields;
  1923. acc_vars, handle_override_dynfun acc_funs ret var cf.cf_name
  1924. end else if is_var then
  1925. ret :: acc_vars, acc_funs
  1926. else
  1927. acc_vars, ret :: acc_funs
  1928. | None, _ -> acc_vars,acc_funs)
  1929. | _ -> acc_vars,acc_funs
  1930. ) ([],[]) cl.cl_ordered_fields
  1931. in
  1932. (* let vars = List.rev vars in *)
  1933. (* let funs = List.rev funs in *)
  1934. (* see if there is any *)
  1935. (match vars, funs with
  1936. | [], [] -> ()
  1937. | _ ->
  1938. (* if there is, we need to find the constructor *)
  1939. let ctors = match cl.cl_constructor with
  1940. | Some ctor -> ctor
  1941. | None -> try
  1942. let sctor, sup, stl = OverloadingConstructor.prev_ctor cl (List.map snd cl.cl_params) in
  1943. let ctor = OverloadingConstructor.clone_ctors gen sctor sup stl cl in
  1944. cl.cl_constructor <- Some ctor;
  1945. ctor
  1946. with | Not_found ->
  1947. let basic = gen.gcon.basic in
  1948. let ctor = mk_class_field "new" (TFun([], basic.tvoid)) false cl.cl_pos (Method MethNormal) [] in
  1949. ctor.cf_expr <- Some
  1950. {
  1951. eexpr = TFunction {
  1952. tf_args = [];
  1953. tf_type = basic.tvoid;
  1954. tf_expr = { eexpr = TBlock[]; etype = basic.tvoid; epos = cl.cl_pos };
  1955. };
  1956. etype = ctor.cf_type;
  1957. epos = ctor.cf_pos;
  1958. };
  1959. cl.cl_constructor <- Some ctor;
  1960. ctor
  1961. in
  1962. let process ctor =
  1963. let func = match ctor.cf_expr with
  1964. | Some({eexpr = TFunction(tf)} as e) ->
  1965. let rec add_fn e = match e.eexpr with
  1966. | TBlock(hd :: tl) -> (match hd.eexpr with
  1967. | TCall({ eexpr = TConst TSuper }, _) ->
  1968. if not (OverloadingConstructor.descends_from_native_or_skipctor cl) then
  1969. { e with eexpr = TBlock(vars @ (hd :: (funs @ tl))) }
  1970. else
  1971. { e with eexpr = TBlock(hd :: (vars @ funs @ tl)) }
  1972. | TBlock(_) ->
  1973. { e with eexpr = TBlock( (add_fn hd) :: tl ) }
  1974. | _ ->
  1975. { e with eexpr = TBlock( vars @ funs @ (hd :: tl) ) })
  1976. | _ -> Type.concat { e with eexpr = TBlock(vars @ funs) } e
  1977. in
  1978. let tf_expr = add_fn (mk_block tf.tf_expr) in
  1979. { e with eexpr = TFunction({ tf with tf_expr = tf_expr }) }
  1980. | _ -> assert false
  1981. in
  1982. ctor.cf_expr <- Some(func)
  1983. in
  1984. List.iter process (ctors :: ctors.cf_overloads)
  1985. )
  1986. end
  1987. in
  1988. let mod_filter = function
  1989. | TClassDecl cl -> (if not cl.cl_extern then handle_class cl); None
  1990. | _ -> None in
  1991. gen.gmodule_filters#add ~name:"init_funcs" ~priority:(PCustom priority) mod_filter
  1992. end;;
  1993. (* ******************************************* *)
  1994. (* Dynamic Binop/Unop handler *)
  1995. (* ******************************************* *)
  1996. (*
  1997. On some languages there is limited support for operations on
  1998. dynamic variables, so those operations must be changed.
  1999. There are 5 types of binary operators:
  2000. 1 - can take any variable and returns a bool (== and !=)
  2001. 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)
  2002. 3 - take numbers and return a number ( *, /, ...)
  2003. 4 - take ints and return an int (bit manipulation)
  2004. 5 - take a bool and returns a bool ( &&, || ...)
  2005. On the default implementation, type 1 and the plus function will be handled with a function call;
  2006. Type 2 will be handled with the parameter "compare_handler", which will do something like Reflect.compare(x1, x2);
  2007. Types 3, 4 and 5 will perform a cast to double, int and bool, which will then be handled normally by the platform
  2008. Unary operators are the most difficult to handle correctly.
  2009. With unary operators, there are 2 types:
  2010. 1 - can take a number, changes and returns the result (++, --, ~)
  2011. 2 - can take a number (-) or bool (!), and returns the result
  2012. 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,
  2013. but this is how we will handle right now.
  2014. something like that:
  2015. var x:Dynamic = 10;
  2016. x++;
  2017. will be:
  2018. object x = 10;
  2019. x = ((IConvertible)x).ToDouble(null) + 1;
  2020. depends on:
  2021. (syntax) must run before expression/statment normalization because it may generate complex expressions
  2022. must run before OverloadingCtor due to later priority conflicts. Since ExpressionUnwrap is only
  2023. defined afterwards, we will set this value with absolute values
  2024. *)
  2025. module DynamicOperators =
  2026. struct
  2027. let name = "dyn_ops"
  2028. let priority = 0.0
  2029. let priority_as_synf = 100.0 (*solve_deps name [DBefore ExpressionUnwrap.priority]*)
  2030. 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) =
  2031. let get_etype_one e =
  2032. if like_int e.etype then
  2033. (gen.gcon.basic.tint, { eexpr = TConst(TInt(Int32.one)); etype = gen.gcon.basic.tint; epos = e.epos })
  2034. else
  2035. (gen.gcon.basic.tfloat, { eexpr = TConst(TFloat("1.0")); etype = gen.gcon.basic.tfloat; epos = e.epos })
  2036. in
  2037. let basic = gen.gcon.basic in
  2038. let rec run e =
  2039. match e.eexpr with
  2040. | TBinop (OpAssignOp op, e1, e2) when should_change e -> (* e1 will never contain another TBinop *)
  2041. (match e1.eexpr with
  2042. | TLocal _ ->
  2043. mk_paren { e with eexpr = TBinop(OpAssign, e1, run { e with eexpr = TBinop(op, e1, e2) }) }
  2044. | TField _ | TArray _ ->
  2045. let eleft, rest = match e1.eexpr with
  2046. | TField(ef, f) ->
  2047. let v = mk_temp gen "dynop" ef.etype in
  2048. { e1 with eexpr = TField(mk_local v ef.epos, f) }, [ { eexpr = TVar(v,Some (run ef)); etype = basic.tvoid; epos = ef.epos } ]
  2049. | TArray(e1a, e2a) ->
  2050. let v = mk_temp gen "dynop" e1a.etype in
  2051. let v2 = mk_temp gen "dynopi" e2a.etype in
  2052. { e1 with eexpr = TArray(mk_local v e1a.epos, mk_local v2 e2a.epos) }, [
  2053. { eexpr = TVar(v,Some (run e1a)); etype = basic.tvoid; epos = e1.epos };
  2054. { eexpr = TVar(v2, Some (run e2a)); etype = basic.tvoid; epos = e1.epos }
  2055. ]
  2056. | _ -> assert false
  2057. in
  2058. { e with
  2059. eexpr = TBlock (rest @ [ { e with eexpr = TBinop(OpAssign, eleft, run { e with eexpr = TBinop(op, eleft, e2) }) } ]);
  2060. }
  2061. | _ ->
  2062. assert false
  2063. )
  2064. | TBinop (OpAssign, e1, e2)
  2065. | TBinop (OpInterval, e1, e2) -> Type.map_expr run e
  2066. | TBinop (op, e1, e2) when should_change e->
  2067. (match op with
  2068. | OpEq -> (* type 1 *)
  2069. equals_handler (run e1) (run e2)
  2070. | OpNotEq -> (* != -> !equals() *)
  2071. mk_paren { eexpr = TUnop(Ast.Not, Prefix, (equals_handler (run e1) (run e2))); etype = gen.gcon.basic.tbool; epos = e.epos }
  2072. | OpAdd ->
  2073. if handle_strings && (is_string e.etype || is_string e1.etype || is_string e2.etype) then
  2074. { e with eexpr = TBinop(op, mk_cast gen.gcon.basic.tstring (run e1), mk_cast gen.gcon.basic.tstring (run e2)) }
  2075. else
  2076. dyn_plus_handler e (run e1) (run e2)
  2077. | OpGt | OpGte | OpLt | OpLte -> (* type 2 *)
  2078. { 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 }
  2079. | OpMult | OpDiv | OpSub | OpMod -> (* always cast everything to double *)
  2080. let etype, _ = get_etype_one e in
  2081. { e with eexpr = TBinop(op, mk_cast etype (run e1), mk_cast etype (run e2)) }
  2082. | OpBoolAnd | OpBoolOr ->
  2083. { e with eexpr = TBinop(op, mk_cast gen.gcon.basic.tbool (run e1), mk_cast gen.gcon.basic.tbool (run e2)) }
  2084. | OpAnd | OpOr | OpXor | OpShl | OpShr | OpUShr ->
  2085. { e with eexpr = TBinop(op, mk_cast gen.gcon.basic.tint (run e1), mk_cast gen.gcon.basic.tint (run e2)) }
  2086. | OpAssign | OpAssignOp _ | OpInterval | OpArrow -> assert false)
  2087. | TUnop (Increment as op, flag, e1)
  2088. | TUnop (Decrement as op, flag, e1) when should_change e ->
  2089. (*
  2090. some naming definitions:
  2091. * ret => the returning variable
  2092. * _g => the get body
  2093. * getvar => the get variable expr
  2094. This will work like this:
  2095. - if e1 is a TField, set _g = get body, getvar = (get body).varname
  2096. - if Prefix, return getvar = getvar + 1.0
  2097. - if Postfix, set ret = getvar; getvar = getvar + 1.0; ret;
  2098. *)
  2099. let etype, one = get_etype_one e in
  2100. let op = (match op with Increment -> OpAdd | Decrement -> OpSub | _ -> assert false) in
  2101. let var, getvar =
  2102. match e1.eexpr with
  2103. | TField(fexpr, field) ->
  2104. let tmp = mk_temp gen "getvar" fexpr.etype in
  2105. let var = { eexpr = TVar(tmp, Some(run fexpr)); etype = gen.gcon.basic.tvoid; epos = e.epos } in
  2106. (Some var, { eexpr = TField( { fexpr with eexpr = TLocal(tmp) }, field); etype = etype; epos = e1.epos })
  2107. | _ ->
  2108. (None, e1)
  2109. in
  2110. (match flag with
  2111. | Prefix ->
  2112. let block = (match var with | Some e -> [e] | None -> []) @
  2113. [
  2114. 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; }
  2115. ]
  2116. in
  2117. { eexpr = TBlock(block); etype = etype; epos = e.epos }
  2118. | Postfix ->
  2119. let ret = mk_temp gen "ret" etype in
  2120. 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
  2121. let retlocal = { eexpr = TLocal(ret); etype = etype; epos = e.epos } in
  2122. let block = vars @
  2123. [
  2124. { e with eexpr = TBinop(OpAssign, getvar, { eexpr = TBinop(op, retlocal, one); etype = getvar.etype; epos = e.epos }) };
  2125. retlocal
  2126. ] in
  2127. { eexpr = TBlock(block); etype = etype; epos = e.epos }
  2128. )
  2129. | TUnop (op, flag, e1) when should_change e ->
  2130. let etype = match op with | Not -> gen.gcon.basic.tbool | _ -> gen.gcon.basic.tint in
  2131. mk_paren { eexpr = TUnop(op, flag, mk_cast etype (run e1)); etype = etype; epos = e.epos }
  2132. | _ -> Type.map_expr run e
  2133. in
  2134. run
  2135. let configure gen (mapping_func:texpr->texpr) =
  2136. let map e = Some(mapping_func e) in
  2137. gen.gexpr_filters#add ~name:"dyn_ops" ~priority:(PCustom priority) map
  2138. let configure_as_synf gen (mapping_func:texpr->texpr) =
  2139. let map e = Some(mapping_func e) in
  2140. gen.gexpr_filters#add ~name:"dyn_ops" ~priority:(PCustom priority_as_synf) map
  2141. end;;
  2142. (* ******************************************* *)
  2143. (* Dynamic Field Access *)
  2144. (* ******************************************* *)
  2145. (*
  2146. This module will filter every dynamic field access in haxe.
  2147. On platforms that do not support dynamic access, it is with this that you should
  2148. replace dynamic calls with x.field / Reflect.setField calls, and guess what -
  2149. this is the default implemenation!
  2150. Actually there is a problem with Reflect.setField because it returns void, which is a bad thing for us,
  2151. so even in the default implementation, the function call should be specified to a Reflect.setField version that returns
  2152. the value that was set
  2153. (TODO: should it be separated?)
  2154. As a plus, the default implementation adds something that doesn't hurt anybody, it looks for
  2155. TAnon with Statics / EnumStatics field accesses and transforms them into real static calls.
  2156. This means it will take this
  2157. var m = Math;
  2158. for (i in 0...1000) m.cos(10);
  2159. which is an optimization in dynamic platforms, but performs horribly on strongly typed platforms
  2160. and transform into:
  2161. var m = Math;
  2162. for (i in 0...1000) Math.cos(10);
  2163. (addendum:)
  2164. configure_generate_classes will already take care of generating the reflection-enabled class fields and calling abstract_implementation
  2165. with the right arguments.
  2166. Also
  2167. depends on:
  2168. (ok) must run AFTER Binop/Unop handler - so Unops / Binops are already unrolled
  2169. *)
  2170. module DynamicFieldAccess =
  2171. struct
  2172. let name = "dynamic_field_access"
  2173. let priority = solve_deps name [DAfter DynamicOperators.priority]
  2174. let priority_as_synf = solve_deps name [DAfter DynamicOperators.priority_as_synf]
  2175. (*
  2176. is_dynamic (expr) (field_access_expr) (field) : a function that indicates if the field access should be changed
  2177. change_expr (expr) (field_access_expr) (field) (setting expr) (is_unsafe) : changes the expression
  2178. call_expr (expr) (field_access_expr) (field) (call_params) : changes a call expression
  2179. *)
  2180. 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) =
  2181. let rec run e =
  2182. match e.eexpr with
  2183. (* class types *)
  2184. | TField(fexpr, f) when is_some (anon_class fexpr.etype) ->
  2185. let decl = get (anon_class fexpr.etype) in
  2186. let name = field_name f in
  2187. (try
  2188. match decl with
  2189. | TClassDecl cl ->
  2190. let cf = PMap.find name cl.cl_statics in
  2191. { e with eexpr = TField({ fexpr with eexpr = TTypeExpr decl }, FStatic(cl, cf)) }
  2192. | TEnumDecl en ->
  2193. let ef = PMap.find name en.e_constrs in
  2194. { e with eexpr = TField({ fexpr with eexpr = TTypeExpr decl }, FEnum(en, ef)) }
  2195. | TAbstractDecl _ -> (* abstracts don't have TFields *) assert false
  2196. | TTypeDecl _ -> (* anon_class doesn't return TTypeDecl *) assert false
  2197. with
  2198. | Not_found -> match f with
  2199. | FStatic(cl,cf) when Meta.has Meta.Extern cf.cf_meta ->
  2200. { e with eexpr = TField({ fexpr with eexpr = TTypeExpr decl }, FStatic(cl, cf)) }
  2201. | _ ->
  2202. change_expr e { fexpr with eexpr = TTypeExpr decl } (field_name f) None true
  2203. )
  2204. | TField(fexpr, f) when is_dynamic e fexpr (f) ->
  2205. change_expr e (run fexpr) (field_name f) None true
  2206. | TCall(
  2207. { eexpr = TField(_, FStatic({ cl_path = ([], "Reflect") }, { cf_name = "field" })) } ,
  2208. [obj; { eexpr = TConst(TString(field)) }]
  2209. ) ->
  2210. let t = match gen.greal_type obj.etype with
  2211. | TDynamic _ | TAnon _ | TMono _ -> t_dynamic
  2212. | t -> t
  2213. in
  2214. change_expr (mk_field_access gen { obj with etype = t } field obj.epos) (run obj) field None false
  2215. | TCall(
  2216. { eexpr = TField(_, FStatic({ cl_path = ([], "Reflect") }, { cf_name = "setField" } )) },
  2217. [obj; { eexpr = TConst(TString(field)) }; evalue]
  2218. ) ->
  2219. change_expr (mk_field_access gen obj field obj.epos) (run obj) field (Some (run evalue)) false
  2220. | TBinop(OpAssign, ({eexpr = TField(fexpr, f)}), evalue) when is_dynamic e fexpr (f) ->
  2221. change_expr e (run fexpr) (field_name f) (Some (run evalue)) true
  2222. | TBinop(OpAssign, { eexpr = TField(fexpr, f) }, evalue) ->
  2223. (match field_access_esp gen fexpr.etype (f) with
  2224. | FClassField(_,_,_,cf,false,t,_) when (try PMap.find cf.cf_name gen.gbase_class_fields == cf with Not_found -> false) ->
  2225. change_expr e (run fexpr) (field_name f) (Some (run evalue)) true
  2226. | _ -> Type.map_expr run e
  2227. )
  2228. (* #if debug *)
  2229. | TBinop(OpAssignOp op, ({eexpr = TField(fexpr, f)}), evalue) when is_dynamic e fexpr (f) -> assert false (* this case shouldn't happen *)
  2230. | TUnop(Increment, _, ({eexpr = TField( ( { eexpr=TLocal(local) } as fexpr ), f)}))
  2231. | TUnop(Decrement, _, ({eexpr = TField( ( { eexpr=TLocal(local) } as fexpr ), f)})) when is_dynamic e fexpr (f) -> assert false (* this case shouldn't happen *)
  2232. (* #end *)
  2233. | TCall( ({ eexpr = TField(fexpr, f) }), params ) when is_dynamic e fexpr (f) ->
  2234. call_expr e (run fexpr) (field_name f) (List.map run params)
  2235. | _ -> Type.map_expr run e
  2236. in run
  2237. (*
  2238. this function will already configure with the abstract implementation, and also will create the needed class fields to
  2239. enable reflection on platforms that don't support reflection.
  2240. this means it will create the following class methods:
  2241. - getField(field, isStatic) - gets the value of the field. isStatic
  2242. - setField -
  2243. -
  2244. *)
  2245. let configure_generate_classes gen optimize (runtime_getset_field:texpr->texpr->string->texpr option->texpr) (runtime_call_expr:texpr->texpr->string->texpr list->texpr) =
  2246. ()
  2247. let configure gen (mapping_func:texpr->texpr) =
  2248. let map e = Some(mapping_func e) in
  2249. gen.gexpr_filters#add ~name:"dynamic_field_access" ~priority:(PCustom(priority)) map
  2250. let configure_as_synf gen (mapping_func:texpr->texpr) =
  2251. let map e = Some(mapping_func e) in
  2252. gen.gexpr_filters#add ~name:"dynamic_field_access" ~priority:(PCustom(priority_as_synf)) map
  2253. end;;
  2254. (* ******************************************* *)
  2255. (* Closure Detection *)
  2256. (* ******************************************* *)
  2257. (*
  2258. Just a small utility filter that detects when a closure must be created.
  2259. On the default implementation, this means when a function field is being accessed
  2260. not via reflection and not to be called instantly
  2261. dependencies:
  2262. must run after DynamicFieldAccess, so any TAnon { Statics / EnumStatics } will be changed to the corresponding TTypeExpr
  2263. *)
  2264. module FilterClosures =
  2265. struct
  2266. let name = "filter_closures"
  2267. let priority = solve_deps name [DAfter DynamicFieldAccess.priority]
  2268. let traverse gen (should_change:texpr->string->bool) (filter:texpr->texpr->string->bool->texpr) =
  2269. let rec run e =
  2270. match e.eexpr with
  2271. (*(* this is precisely the only case where we won't even ask if we should change, because it is a direct use of TClosure *)
  2272. | TCall ( {eexpr = TClosure(e1,s)} as clos, args ) ->
  2273. { e with eexpr = TCall({ clos with eexpr = TClosure(run e1, s) }, List.map run args ) }
  2274. | TCall ( clos, args ) ->
  2275. let rec loop clos = match clos.eexpr with
  2276. | TClosure(e1,s) -> Some (clos, e1, s)
  2277. | TParenthesis p -> loop p
  2278. | _ -> None
  2279. in
  2280. let clos = loop clos in
  2281. (match clos with
  2282. | Some (clos, e1, s) -> { e with eexpr = TCall({ clos with eexpr = TClosure(run e1, s) }, List.map run args ) }
  2283. | None -> Type.map_expr run e)*)
  2284. | TCall({ eexpr = TLocal{ v_name = "__delegate__" } } as local, [del]) ->
  2285. { e with eexpr = TCall(local, [Type.map_expr run del]) }
  2286. | TCall(({ eexpr = TField(_, _) } as ef), params) ->
  2287. { e with eexpr = TCall(Type.map_expr run ef, List.map run params) }
  2288. | TField(ef, FEnum(en, field)) ->
  2289. (* FIXME replace t_dynamic with actual enum Anon field *)
  2290. let ef = run ef in
  2291. (match follow field.ef_type with
  2292. | TFun _ when should_change ef field.ef_name ->
  2293. filter e ef field.ef_name true
  2294. | _ ->
  2295. { e with eexpr = TField(ef, FEnum(en,field)) }
  2296. )
  2297. | TField(({ eexpr = TTypeExpr _ } as tf), f) ->
  2298. (match field_access_esp gen tf.etype (f) with
  2299. | FClassField(_,_,_,cf,_,_,_) ->
  2300. (match cf.cf_kind with
  2301. | Method(MethDynamic)
  2302. | Var _ ->
  2303. e
  2304. | _ when should_change tf cf.cf_name ->
  2305. filter e tf cf.cf_name true
  2306. | _ ->
  2307. e
  2308. )
  2309. | _ -> e)
  2310. | TField(e1, FClosure (Some _, cf)) when should_change e1 cf.cf_name ->
  2311. (match cf.cf_kind with
  2312. | Method MethDynamic | Var _ ->
  2313. Type.map_expr run e
  2314. | _ ->
  2315. filter e (run e1) cf.cf_name false)
  2316. | _ -> Type.map_expr run e
  2317. in
  2318. run
  2319. let configure gen (mapping_func:texpr->texpr) =
  2320. let map e = Some(mapping_func e) in
  2321. gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map
  2322. end;;
  2323. (* ******************************************* *)
  2324. (* Dynamic TArray Handling *)
  2325. (* ******************************************* *)
  2326. (*
  2327. In some languages you cannot overload the [] operator,
  2328. so we need to decide what is kept as TArray and what gets mapped.
  2329. - in order to do this you must ensure that
  2330. depends on:
  2331. (syntax) must run before expression/statment normalization because it may generate complex expressions
  2332. (ok) must run before binop transformations because it may generate some untreated binop ops
  2333. (ok) must run before dynamic field access is transformed into reflection
  2334. *)
  2335. module TArrayTransform =
  2336. struct
  2337. let name = "dyn_tarray"
  2338. let priority = solve_deps name [DBefore DynamicOperators.priority; DBefore DynamicFieldAccess.priority]
  2339. let priority_as_synf = solve_deps name [DBefore DynamicOperators.priority_as_synf; DBefore DynamicFieldAccess.priority_as_synf]
  2340. (* should change signature: tarray expr -> binop operation -> should change? *)
  2341. let default_implementation gen (should_change:texpr->Ast.binop option->bool) (get_fun:string) (set_fun:string) =
  2342. let basic = gen.gcon.basic in
  2343. let mk_get e e1 e2 =
  2344. let efield = mk_field_access gen e1 get_fun e.epos in
  2345. { e with eexpr = TCall(efield, [e2]) }
  2346. in
  2347. let mk_set e e1 e2 evalue =
  2348. let efield = mk_field_access gen e1 set_fun e.epos in
  2349. { e with eexpr = TCall(efield, [e2; evalue]) }
  2350. in
  2351. let rec run e =
  2352. match e.eexpr with
  2353. | TArray(e1, e2) ->
  2354. (* e1 should always be a var; no need to map there *)
  2355. if should_change e None then mk_get e (run e1) (run e2) else Type.map_expr run e
  2356. | TBinop (Ast.OpAssign, ({ eexpr = TArray(e1a,e2a) } as earray), evalue) when should_change earray (Some Ast.OpAssign) ->
  2357. mk_set e (run e1a) (run e2a) (run evalue)
  2358. | TBinop (Ast.OpAssignOp op,({ eexpr = TArray(e1a,e2a) } as earray) , evalue) when should_change earray (Some (Ast.OpAssignOp op)) ->
  2359. (* cache all arguments in vars so they don't get executed twice *)
  2360. (* let ensure_local gen block name e = *)
  2361. let block = ref [] in
  2362. let arr_local = ensure_local gen block "array" (run e1a) in
  2363. let idx_local = ensure_local gen block "index" (run e2a) in
  2364. block := (mk_set e arr_local idx_local ( { e with eexpr=TBinop(op, mk_get earray arr_local idx_local, run evalue) } )) :: !block;
  2365. { e with eexpr = TBlock (List.rev !block) }
  2366. | TUnop(op, flag, ({ eexpr = TArray(e1a, e2a) } as earray)) ->
  2367. if should_change earray None && match op with | Not | Neg -> false | _ -> true then begin
  2368. let block = ref [] in
  2369. let actual_t = match op with
  2370. | Ast.Increment | Ast.Decrement -> (match follow earray.etype with
  2371. | TInst _ | TAbstract _ | TEnum _ -> earray.etype
  2372. | _ -> basic.tfloat)
  2373. | Ast.Not -> basic.tbool
  2374. | _ -> basic.tint
  2375. in
  2376. let val_v = mk_temp gen "arrVal" actual_t in
  2377. let ret_v = mk_temp gen "arrRet" actual_t in
  2378. let arr_local = ensure_local gen block "arr" (run e1a) in
  2379. let idx_local = ensure_local gen block "arrIndex" (run e2a) in
  2380. let val_local = { earray with eexpr = TLocal(val_v) } in
  2381. let ret_local = { earray with eexpr = TLocal(ret_v) } in
  2382. (* var idx = 1; var val = x._get(idx); var ret = val++; x._set(idx, val); ret; *)
  2383. block := { eexpr = TVar(val_v, Some(mk_get earray arr_local idx_local)); (* var val = x._get(idx) *)
  2384. etype = gen.gcon.basic.tvoid;
  2385. epos = e2a.epos
  2386. } :: !block;
  2387. block := { eexpr = TVar(ret_v, Some { e with eexpr = TUnop(op, flag, val_local) }); (* var ret = val++ *)
  2388. etype = gen.gcon.basic.tvoid;
  2389. epos = e2a.epos
  2390. } :: !block;
  2391. block := (mk_set e arr_local idx_local val_local) (*x._set(idx,val)*) :: !block;
  2392. block := ret_local :: !block;
  2393. { e with eexpr = TBlock (List.rev !block) }
  2394. end else
  2395. Type.map_expr run e
  2396. | _ -> Type.map_expr run e
  2397. in run
  2398. let configure gen (mapping_func:texpr->texpr) =
  2399. let map e = Some(mapping_func e) in
  2400. gen.gexpr_filters#add ~name:"dyn_tarray" ~priority:(PCustom priority) map
  2401. let configure_as_synf gen (mapping_func:texpr->texpr) =
  2402. let map e = Some(mapping_func e) in
  2403. gen.gexpr_filters#add ~name:"dyn_tarray" ~priority:(PCustom priority_as_synf) map
  2404. end;;
  2405. (* ******************************************* *)
  2406. (* Try / Catch + throw native types handling *)
  2407. (* ******************************************* *)
  2408. (*
  2409. Some languages/vm's do not support throwing any kind of value. For them, only
  2410. special kinds of objects can be thrown. Because of this, we must wrap some throw
  2411. statements with an expression, and also we must unwrap it on the catch() phase, and
  2412. maybe manually test with Std.is()
  2413. dependencies:
  2414. must run before dynamic field access (?) TODO review
  2415. It's a syntax filter, as it alters types (throw wrapper)
  2416. *)
  2417. module TryCatchWrapper =
  2418. struct
  2419. let priority = solve_deps "try_catch" [DBefore DynamicFieldAccess.priority]
  2420. (*
  2421. 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
  2422. wrap_throw : the wrapper for throw (throw expr->expr inside throw->returning wrapped expression)
  2423. unwrap_expr : the other way around : given the catch var (maybe will need casting to wrapper_type) , return the unwrap expr
  2424. rethrow_expr : how to rethrow ane exception in the platform
  2425. catchall_type : the class used for catchall (e:Dynamic)
  2426. wrapper_type : the wrapper type, so we can test if exception is of type 'wrapper'
  2427. catch_map : maps the catch expression to include some intialization code (e.g. setting up Stack.exceptionStack)
  2428. *)
  2429. 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) =
  2430. let rec run e =
  2431. match e.eexpr with
  2432. | TThrow texpr when should_wrap texpr.etype -> wrap_throw e (run texpr)
  2433. | TTry (ttry, catches) ->
  2434. let nowrap_catches, must_wrap_catches, catchall = List.fold_left (fun (nowrap_catches, must_wrap_catches, catchall) (v, catch) ->
  2435. (* first we'll see if the type is Dynamic (catchall) *)
  2436. match follow v.v_type with
  2437. | TDynamic _ ->
  2438. assert (is_none catchall);
  2439. (nowrap_catches, must_wrap_catches, Some(v,run catch))
  2440. (* see if we should unwrap it *)
  2441. | _ when should_wrap (follow v.v_type) ->
  2442. (nowrap_catches, (v,run catch) :: must_wrap_catches, catchall)
  2443. | _ ->
  2444. ( (v,catch_map v (run catch)) :: nowrap_catches, must_wrap_catches, catchall )
  2445. ) ([], [], None) catches
  2446. in
  2447. (*
  2448. 1st catch all nowrap "the easy way"
  2449. 2nd see if there are any must_wrap or catchall. If there is,
  2450. do a catchall first with a temp var.
  2451. then get catchall var (as dynamic) (or create one), and declare it = catchall exception
  2452. then test if it is of type wrapper_type. If it is, unwrap it
  2453. then start doing Std.is() tests for each catch type
  2454. if there is a catchall in the end, end with it. If there isn't, rethrow
  2455. *)
  2456. let dyn_catch = match (catchall, must_wrap_catches) with
  2457. | Some (v,c), _
  2458. | _, (v, c) :: _ ->
  2459. let pos = c.epos in
  2460. let temp_var = mk_temp gen "catchallException" catchall_type in
  2461. let temp_local = { eexpr=TLocal(temp_var); etype = temp_var.v_type; epos = pos } in
  2462. let catchall_var = (*match catchall with
  2463. | None -> *) mk_temp gen "catchall" t_dynamic
  2464. (*| Some (v,_) -> v*)
  2465. in
  2466. let catchall_decl = { eexpr = TVar(catchall_var, Some(temp_local)); etype=gen.gcon.basic.tvoid; epos = pos } in
  2467. let catchall_local = { eexpr = TLocal(catchall_var); etype = t_dynamic; epos = pos } in
  2468. (* if it is of type wrapper_type, unwrap it *)
  2469. 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
  2470. 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
  2471. let if_is_wrapper_expr = { eexpr = TIf(mk_std_is wrapper_type pos,
  2472. { eexpr = TBinop(OpAssign, catchall_local, unwrap_expr temp_var pos); etype = t_dynamic; epos = pos }
  2473. , None); etype = gen.gcon.basic.tvoid; epos = pos } in
  2474. let rec loop must_wrap_catches = match must_wrap_catches with
  2475. | (vcatch,catch) :: tl ->
  2476. { eexpr = TIf(mk_std_is vcatch.v_type catch.epos,
  2477. { 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 },
  2478. Some (loop tl));
  2479. etype = catch.etype; epos = catch.epos }
  2480. | [] ->
  2481. match catchall with
  2482. | Some (v,s) ->
  2483. Type.concat { eexpr = TVar(v, Some(catchall_local)); etype = gen.gcon.basic.tvoid; epos = pos } s
  2484. | None ->
  2485. mk_block (rethrow_expr temp_local)
  2486. in
  2487. [ ( temp_var, catch_map temp_var { e with eexpr = TBlock([ catchall_decl; if_is_wrapper_expr; loop must_wrap_catches ]) } ) ]
  2488. | _ ->
  2489. []
  2490. in
  2491. { e with eexpr = TTry(run ttry, (List.rev nowrap_catches) @ dyn_catch) }
  2492. | _ -> Type.map_expr run e
  2493. in
  2494. run
  2495. let configure gen (mapping_func:texpr->texpr) =
  2496. let map e = Some(mapping_func e) in
  2497. gen.gsyntax_filters#add ~name:"try_catch" ~priority:(PCustom priority) map
  2498. end;;
  2499. let fun_args = List.map (function | (v,s) -> (v.v_name, (match s with | None -> false | Some _ -> true), v.v_type))
  2500. (* ******************************************* *)
  2501. (* Closures To Class *)
  2502. (* ******************************************* *)
  2503. (*
  2504. 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
  2505. that implements an abstract interface for calling functions. This is very important for targets that don't support anonymous functions to work correctly.
  2506. 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
  2507. from this interface.
  2508. dependencies:
  2509. must run after dynamic field access, because of conflicting ways to deal with invokeField
  2510. (module filter) must run after OverloadingCtor so we can also change the dynamic function expressions
  2511. uses TArray expressions for array. TODO see interaction
  2512. uses TThrow expressions.
  2513. *)
  2514. module ClosuresToClass =
  2515. struct
  2516. let name = "closures_to_class"
  2517. let priority = solve_deps name [ DAfter DynamicFieldAccess.priority ]
  2518. let priority_as_synf = solve_deps name [ DAfter DynamicFieldAccess.priority_as_synf ]
  2519. type closures_ctx =
  2520. {
  2521. fgen : generator_ctx;
  2522. mutable func_class : tclass;
  2523. (*
  2524. this is what will actually turn the function into class field.
  2525. The standard implementation by default will already take care of creating the class, and setting the captured variables.
  2526. It will also return the super arguments to be called
  2527. *)
  2528. mutable closure_to_classfield : tfunc->t->pos->tclass_field * (texpr list);
  2529. (*
  2530. when a dynamic function call is made, we need to convert it as if it were calling the dynamic function interface.
  2531. TCall expr -> new TCall expr
  2532. *)
  2533. mutable dynamic_fun_call : texpr->texpr;
  2534. (*
  2535. called once so the implementation can make one of a time initializations in the base class
  2536. for all functions
  2537. *)
  2538. mutable initialize_base_class : tclass->unit;
  2539. (*
  2540. Base classfields are the class fields for the abstract implementation of either the Function implementation,
  2541. or the invokeField implementation for the classes
  2542. They will either try to call the right function or will fail with
  2543. (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
  2544. *)
  2545. mutable get_base_classfields_for : tclass->bool->(unit->(tvar * tconstant option) list)->tclass_field list;
  2546. (*
  2547. This is a more complex version of get_base_classfields_for.
  2548. It's meant to provide a toolchain so we can easily create classes that extend Function
  2549. and add more functionality on top of it.
  2550. arguments:
  2551. tclass -> subject (so we know the type of this)
  2552. bool -> is it a function type
  2553. ( int -> (int->t->tconstant option->texpr) -> ( (tvar * tconstant option) list * texpr) )
  2554. 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
  2555. t -> the return type of the function
  2556. (int->t->tconstant option->texpr) -> api to get exprs that unwrap arguments correctly
  2557. int -> argument wanted to unwrap
  2558. t -> solicited type
  2559. tconstant option -> map to this default value if null
  2560. returns a texpr that tells how the default
  2561. should return a list with additional arguments (only works if is_function_base = true)
  2562. and the underlying function expression
  2563. *)
  2564. mutable map_base_classfields : tclass->bool->( int -> t -> (tvar list) -> (int->t->tconstant option->texpr) -> ( (tvar * tconstant option) list * texpr) )->tclass_field list;
  2565. mutable transform_closure : texpr->texpr->string->texpr;
  2566. }
  2567. type map_info = {
  2568. in_unsafe : bool;
  2569. in_unused : bool;
  2570. }
  2571. let null_map_info = { in_unsafe = false; in_unused = false; }
  2572. (*
  2573. the default implementation will take 3 transformation functions:
  2574. * one that will transform closures that are not called immediately (instance.myFunc).
  2575. 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
  2576. * one that will actually handle the anonymous functions themselves.
  2577. * one that will transform calling a dynamic function. So for example, dynFunc(arg1, arg2) might turn into dynFunc.apply2(arg1, arg2);
  2578. ( suspended ) * an option to match papplied functions
  2579. * handling parameterized anonymous function declaration (optional - tparam_anon_decl and tparam_anon_acc)
  2580. *)
  2581. let rec cleanup_delegate e = match e.eexpr with
  2582. | TParenthesis e | TMeta(_,e)
  2583. | TCast(e,_) -> cleanup_delegate e
  2584. | _ -> e
  2585. let funct gen t = match follow (run_follow gen t) with
  2586. | TFun(args,ret) -> args,ret
  2587. | _ -> raise Not_found
  2588. let mk_conversion_fun gen e =
  2589. let args, ret = funct gen e.etype in
  2590. let tf_args = List.map (fun (n,o,t) -> alloc_var n t,None) args in
  2591. let block, local = match e.eexpr with
  2592. | TLocal v ->
  2593. v.v_capture <- true;
  2594. [],e
  2595. | _ ->
  2596. let tmp = mk_temp gen "delegate_conv" e.etype in
  2597. tmp.v_capture <- true;
  2598. [{ eexpr = TVar(tmp,Some e); etype = gen.gcon.basic.tvoid; epos = e.epos }], mk_local tmp e.epos
  2599. in
  2600. let body = {
  2601. eexpr = TCall(local, List.map (fun (v,_) -> mk_local v e.epos) tf_args);
  2602. etype = ret;
  2603. epos = e.epos;
  2604. } in
  2605. let body = if not (is_void ret) then
  2606. { body with eexpr = TReturn( Some body ) }
  2607. else
  2608. body
  2609. in
  2610. let body = {
  2611. eexpr = TBlock(block @ [body]);
  2612. etype = body.etype;
  2613. epos = body.epos;
  2614. } in
  2615. {
  2616. tf_args = tf_args;
  2617. tf_expr = body;
  2618. tf_type = ret;
  2619. }
  2620. 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 =
  2621. let info = ref null_map_info in
  2622. let rec run e =
  2623. match e.eexpr with
  2624. | TCast({ eexpr = TCall({ eexpr = TLocal{ v_name = "__delegate__" } } as local, [del] ) } as e2, _) ->
  2625. let e2 = { e2 with etype = e.etype } in
  2626. let replace_delegate ex =
  2627. { e with eexpr = TCast({ e2 with eexpr = TCall(local, [ex]) }, None) }
  2628. in
  2629. (* found a delegate; let's see if it's a closure or not *)
  2630. let clean = cleanup_delegate del in
  2631. (match clean.eexpr with
  2632. | TField( ef, (FClosure _ as f)) | TField( ef, (FStatic _ as f)) ->
  2633. (* a closure; let's leave this unchanged for FilterClosures to handle it *)
  2634. replace_delegate { clean with eexpr = TField( run ef, f ) }
  2635. | TFunction tf ->
  2636. (* handle like we'd handle a normal function, but create an unchanged closure field for it *)
  2637. let ret = handle_anon_func clean { tf with tf_expr = run tf.tf_expr } !info (Some e.etype) in
  2638. replace_delegate ret
  2639. | _ -> try
  2640. let tf = mk_conversion_fun gen del in
  2641. let ret = handle_anon_func del { tf with tf_expr = run tf.tf_expr } !info (Some e.etype) in
  2642. replace_delegate ret
  2643. with Not_found ->
  2644. gen.gcon.error "This delegate construct is unsupported" e.epos;
  2645. replace_delegate (run clean))
  2646. | TCall(({ eexpr = TLocal{ v_name = "__unsafe__" } } as local), [arg]) ->
  2647. let old = !info in
  2648. info := { !info with in_unsafe = true };
  2649. let arg2 = run arg in
  2650. info := old;
  2651. { e with eexpr = TCall(local,[arg2]) }
  2652. (* parameterized functions handling *)
  2653. | TVar(vv, ve) -> (match tparam_anon_decl with
  2654. | None -> Type.map_expr run e
  2655. | Some tparam_anon_decl ->
  2656. (match (vv, ve) with
  2657. | ({ v_extra = Some( _ :: _, _) } as v), Some ({ eexpr = TFunction tf } as f)
  2658. | ({ 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 *)
  2659. ignore(tparam_anon_decl v f { tf with tf_expr = run tf.tf_expr });
  2660. { e with eexpr = TBlock([]) }
  2661. | _ ->
  2662. Type.map_expr run { e with eexpr = TVar(vv, ve) })
  2663. )
  2664. | TLocal ({ v_extra = Some( _ :: _, _) } as v)
  2665. | TArray ({ eexpr = TLocal ({ v_extra = Some( _ :: _, _) } as v) }, _) -> (* captured transformation *)
  2666. (match tparam_anon_acc with
  2667. | None -> Type.map_expr run e
  2668. | Some tparam_anon_acc -> tparam_anon_acc v e)
  2669. | TCall( { eexpr = TField(_, FEnum _) }, _ ) ->
  2670. Type.map_expr run e
  2671. (* if a TClosure is being call immediately, there's no need to convert it to a TClosure *)
  2672. | TCall(( { eexpr = TField(ecl,f) } as e1), params) ->
  2673. (* check to see if called field is known and if it is a MethNormal (only MethNormal fields can be called directly) *)
  2674. (* let name = field_name f in *)
  2675. (match field_access_esp gen (gen.greal_type ecl.etype) f with
  2676. | FClassField(_,_,_,cf,_,_,_) ->
  2677. (match cf.cf_kind with
  2678. | Method MethNormal
  2679. | Method MethInline ->
  2680. { e with eexpr = TCall({ e1 with eexpr = TField(run ecl, f) }, List.map run params) }
  2681. | _ ->
  2682. match gen.gfollow#run_f e1.etype with
  2683. | TFun _ ->
  2684. dynamic_func_call { e with eexpr = TCall(run e1, List.map run params) }
  2685. | _ ->
  2686. let i = ref 0 in
  2687. let t = TFun(List.map (fun e -> incr i; "arg" ^ (string_of_int !i), false, e.etype) params, e.etype) in
  2688. dynamic_func_call { e with eexpr = TCall( mk_cast t (run e1), List.map run params ) }
  2689. )
  2690. (* | FNotFound ->
  2691. { e with eexpr = TCall({ e1 with eexpr = TField(run ecl, f) }, List.map run params) }
  2692. (* expressions by now may have generated invalid expressions *) *)
  2693. | _ ->
  2694. match gen.gfollow#run_f e1.etype with
  2695. | TFun _ ->
  2696. dynamic_func_call { e with eexpr = TCall(run e1, List.map run params) }
  2697. | _ ->
  2698. let i = ref 0 in
  2699. let t = TFun(List.map (fun e -> incr i; "arg" ^ (string_of_int !i), false, e.etype) params, e.etype) in
  2700. dynamic_func_call { e with eexpr = TCall( mk_cast t (run e1), List.map run params ) }
  2701. )
  2702. | TField(ecl, FClosure (_,cf)) ->
  2703. transform_closure e (run ecl) cf.cf_name
  2704. | TFunction tf ->
  2705. handle_anon_func e { tf with tf_expr = run tf.tf_expr } !info None
  2706. | TCall({ eexpr = TConst(TSuper) }, _) ->
  2707. Type.map_expr run e
  2708. | TCall({ eexpr = TLocal(v) }, args) when String.get v.v_name 0 = '_' && Hashtbl.mem gen.gspecial_vars v.v_name ->
  2709. Type.map_expr run e
  2710. | TCall(tc,params) ->
  2711. let i = ref 0 in
  2712. let may_cast = match gen.gfollow#run_f tc.etype with
  2713. | TFun _ -> fun e -> e
  2714. | _ ->
  2715. let t = TFun(List.map (fun e ->
  2716. incr i;
  2717. ("p" ^ (string_of_int !i), false, e.etype)
  2718. ) params, e.etype)
  2719. in
  2720. fun e -> mk_cast t e
  2721. in
  2722. dynamic_func_call { e with eexpr = TCall(run (may_cast tc), List.map run params) }
  2723. | _ -> Type.map_expr run e
  2724. in
  2725. (match e.eexpr with
  2726. | TFunction(tf) -> Type.map_expr run e
  2727. | _ -> run e)
  2728. let rec get_type_params acc t =
  2729. match t with
  2730. | TInst(( { cl_kind = KTypeParameter _ } as cl), []) ->
  2731. if List.memq cl acc then acc else cl :: acc
  2732. | TFun (params,tret) ->
  2733. List.fold_left get_type_params acc ( tret :: List.map (fun (_,_,t) -> t) params )
  2734. | TDynamic t ->
  2735. (match t with | TDynamic _ -> acc | _ -> get_type_params acc t)
  2736. | TAbstract (a, pl) when not (Meta.has Meta.CoreType a.a_meta) ->
  2737. get_type_params acc ( Abstract.get_underlying_type a pl)
  2738. | TAnon a ->
  2739. PMap.fold (fun cf acc ->
  2740. let params = List.map (fun (_,t) -> match follow t with
  2741. | TInst(c,_) -> c
  2742. | _ -> assert false) cf.cf_params
  2743. in
  2744. List.filter (fun t -> not (List.memq t params)) (get_type_params acc cf.cf_type)
  2745. ) a.a_fields acc
  2746. | TType(_, [])
  2747. | TAbstract (_, [])
  2748. | TInst(_, [])
  2749. | TEnum(_, []) ->
  2750. acc
  2751. | TType(_, params)
  2752. | TAbstract(_, params)
  2753. | TEnum(_, params)
  2754. | TInst(_, params) ->
  2755. List.fold_left get_type_params acc params
  2756. | TMono r -> (match !r with
  2757. | Some t -> get_type_params acc t
  2758. | None -> acc)
  2759. | _ -> get_type_params acc (follow_once t)
  2760. let get_captured expr =
  2761. let ret = Hashtbl.create 1 in
  2762. let ignored = Hashtbl.create 0 in
  2763. let params = ref [] in
  2764. let check_params t = params := get_type_params !params t in
  2765. let rec traverse expr =
  2766. match expr.eexpr with
  2767. | TFor (v, _, _) ->
  2768. Hashtbl.add ignored v.v_id v;
  2769. check_params v.v_type;
  2770. Type.iter traverse expr
  2771. | TFunction(tf) ->
  2772. List.iter (fun (v,_) -> Hashtbl.add ignored v.v_id v) tf.tf_args;
  2773. (match follow expr.etype with
  2774. | TFun(args,ret) ->
  2775. List.iter (fun (_,_,t) ->
  2776. check_params t
  2777. ) args;
  2778. check_params ret
  2779. | _ -> ());
  2780. Type.iter traverse expr
  2781. | TVar (v, opt) ->
  2782. (match v.v_extra with
  2783. | Some(_ :: _, _) -> ()
  2784. | _ ->
  2785. check_params v.v_type);
  2786. Hashtbl.add ignored v.v_id v;
  2787. ignore(Option.map traverse opt)
  2788. | TLocal { v_extra = Some( (_ :: _ ),_) } ->
  2789. ()
  2790. | TLocal(( { v_capture = true } ) as v) ->
  2791. (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);
  2792. | _ -> Type.iter traverse expr
  2793. in traverse expr;
  2794. ret, !params
  2795. (*
  2796. OPTIMIZEME:
  2797. Take off from Codegen the code that wraps captured variables,
  2798. traverse through all variables, looking for their use (just like local_usage)
  2799. three possible outcomes for captured variables:
  2800. - become a function member variable <- best performance.
  2801. Will not work on functions that can be created more than once (functions inside a loop or functions inside functions)
  2802. The function will have to be created on top of the block, so its variables can be filled in instead of being declared
  2803. - single-element array - the most compatible way, though also creates a slight overhead.
  2804. - we'll have some labels for captured variables:
  2805. - used in loop
  2806. *)
  2807. (*
  2808. The default implementation will impose a naming convention:
  2809. invoke(arity)_(o for returning object/d for returning double) when arity < max_arity
  2810. invoke_dynamic_(o/d) when arity > max_arity
  2811. 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.
  2812. *)
  2813. let default_implementation ft parent_func_class (* e.g. new haxe.lang.ClassClosure *) =
  2814. let gen = ft.fgen in
  2815. ft.initialize_base_class parent_func_class;
  2816. let cfs = ft.get_base_classfields_for parent_func_class true (fun () -> []) in
  2817. List.iter (fun cf ->
  2818. (if cf.cf_name = "new" then parent_func_class.cl_constructor <- Some cf else
  2819. parent_func_class.cl_fields <- PMap.add cf.cf_name cf parent_func_class.cl_fields
  2820. )
  2821. ) cfs;
  2822. parent_func_class.cl_ordered_fields <- (List.filter (fun cf -> cf.cf_name <> "new") cfs) @ parent_func_class.cl_ordered_fields;
  2823. ft.func_class <- parent_func_class;
  2824. let handle_anon_func fexpr tfunc mapinfo delegate_type : texpr * (tclass * texpr list) =
  2825. let gen = ft.fgen in
  2826. let in_unsafe = mapinfo.in_unsafe || match gen.gcurrent_class, gen.gcurrent_classfield with
  2827. | Some c, _ when Meta.has Meta.Unsafe c.cl_meta -> true
  2828. | _, Some cf when Meta.has Meta.Unsafe cf.cf_meta -> true
  2829. | _ -> false
  2830. in
  2831. (* get all captured variables it uses *)
  2832. let captured_ht, tparams = get_captured fexpr in
  2833. let captured = Hashtbl.fold (fun _ e acc -> e :: acc) captured_ht [] in
  2834. (*let cltypes = List.map (fun cl -> (snd cl.cl_path, TInst(map_param cl, []) )) tparams in*)
  2835. let cltypes = List.map (fun cl -> (snd cl.cl_path, TInst(cl, []) )) tparams in
  2836. (* create a new class that extends abstract function class, with a ctor implementation that will setup all captured variables *)
  2837. let cfield = match ft.fgen.gcurrent_classfield with
  2838. | None -> "Anon"
  2839. | Some cf -> cf.cf_name
  2840. in
  2841. let cur_line = Lexer.get_error_line fexpr.epos in
  2842. let path = (fst ft.fgen.gcurrent_path, Printf.sprintf "%s_%s_%d__Fun" (snd ft.fgen.gcurrent_path) cfield cur_line) in
  2843. let cls = mk_class (get ft.fgen.gcurrent_class).cl_module path tfunc.tf_expr.epos in
  2844. if in_unsafe then cls.cl_meta <- (Meta.Unsafe,[],Ast.null_pos) :: cls.cl_meta;
  2845. if Common.defined gen.gcon Define.EraseGenerics then begin
  2846. cls.cl_meta <- (Meta.HaxeGeneric,[],Ast.null_pos) :: cls.cl_meta
  2847. end;
  2848. cls.cl_module <- (get ft.fgen.gcurrent_class).cl_module;
  2849. cls.cl_params <- cltypes;
  2850. let mk_this v pos =
  2851. {
  2852. (mk_field_access gen { eexpr = TConst TThis; etype = TInst(cls, List.map snd cls.cl_params); epos = pos } v.v_name pos)
  2853. with etype = v.v_type
  2854. }
  2855. in
  2856. let mk_this_assign v pos =
  2857. {
  2858. eexpr = TBinop(OpAssign, mk_this v pos, { eexpr = TLocal(v); etype = v.v_type; epos = pos });
  2859. etype = v.v_type;
  2860. epos = pos
  2861. } in
  2862. (* mk_class_field name t public pos kind params *)
  2863. let ctor_args, ctor_sig, ctor_exprs = List.fold_left (fun (ctor_args, ctor_sig, ctor_exprs) lexpr ->
  2864. match lexpr.eexpr with
  2865. | TLocal(v) ->
  2866. let cf = mk_class_field v.v_name v.v_type false lexpr.epos (Var({ v_read = AccNormal; v_write = AccNormal; })) [] in
  2867. cls.cl_fields <- PMap.add v.v_name cf cls.cl_fields;
  2868. cls.cl_ordered_fields <- cf :: cls.cl_ordered_fields;
  2869. let ctor_v = alloc_var v.v_name v.v_type in
  2870. ((ctor_v, None) :: ctor_args, (v.v_name, false, v.v_type) :: ctor_sig, (mk_this_assign v cls.cl_pos) :: ctor_exprs)
  2871. | _ -> assert false
  2872. ) ([],[],[]) captured in
  2873. (* change all captured variables to this.capturedVariable *)
  2874. let rec change_captured e =
  2875. match e.eexpr with
  2876. | TLocal( ({ v_capture = true }) as v ) when Hashtbl.mem captured_ht v.v_id ->
  2877. mk_this v e.epos
  2878. | _ -> Type.map_expr change_captured e
  2879. in
  2880. let func_expr = change_captured tfunc.tf_expr in
  2881. let invokecf, invoke_field, super_args = match delegate_type with
  2882. | None -> (* no delegate *)
  2883. let ifield, sa = ft.closure_to_classfield { tfunc with tf_expr = func_expr } fexpr.etype fexpr.epos in
  2884. ifield,ifield,sa
  2885. | Some _ ->
  2886. let pos = cls.cl_pos in
  2887. let cf = mk_class_field "Delegate" (TFun(fun_args tfunc.tf_args, tfunc.tf_type)) true pos (Method MethNormal) [] in
  2888. cf.cf_expr <- Some { fexpr with eexpr = TFunction { tfunc with tf_expr = func_expr }; };
  2889. cf.cf_meta <- (Meta.Final,[],pos) :: cf.cf_meta;
  2890. cls.cl_ordered_fields <- cf :: cls.cl_ordered_fields;
  2891. cls.cl_fields <- PMap.add cf.cf_name cf cls.cl_fields;
  2892. (* invoke function body: call Delegate function *)
  2893. let ibody = {
  2894. eexpr = TCall({
  2895. eexpr = TField({
  2896. eexpr = TConst TThis;
  2897. etype = TInst(cls, List.map snd cls.cl_params);
  2898. epos = pos;
  2899. }, FInstance(cls, List.map snd cls.cl_params, cf));
  2900. etype = cf.cf_type;
  2901. epos = pos;
  2902. }, List.map (fun (v,_) -> mk_local v pos) tfunc.tf_args);
  2903. etype = tfunc.tf_type;
  2904. epos = pos
  2905. } in
  2906. let ibody = if not (is_void tfunc.tf_type) then
  2907. { ibody with eexpr = TReturn( Some ibody ) }
  2908. else
  2909. ibody
  2910. in
  2911. let ifield, sa = ft.closure_to_classfield { tfunc with tf_expr = ibody } fexpr.etype fexpr.epos in
  2912. cf,ifield,sa
  2913. in
  2914. (* create the constructor *)
  2915. (* todo properly abstract how type var is set *)
  2916. cls.cl_super <- Some(parent_func_class, []);
  2917. let pos = cls.cl_pos in
  2918. let super_call =
  2919. {
  2920. eexpr = TCall({ eexpr = TConst(TSuper); etype = TInst(parent_func_class,[]); epos = pos }, super_args);
  2921. etype = ft.fgen.gcon.basic.tvoid;
  2922. epos = pos;
  2923. } in
  2924. let ctor_type = (TFun(ctor_sig, ft.fgen.gcon.basic.tvoid)) in
  2925. let ctor = mk_class_field "new" ctor_type true cls.cl_pos (Method(MethNormal)) [] in
  2926. ctor.cf_expr <- Some(
  2927. {
  2928. eexpr = TFunction(
  2929. {
  2930. tf_args = ctor_args;
  2931. tf_type = ft.fgen.gcon.basic.tvoid;
  2932. tf_expr = { eexpr = TBlock(super_call :: ctor_exprs); etype = ft.fgen.gcon.basic.tvoid; epos = cls.cl_pos }
  2933. });
  2934. etype = ctor_type;
  2935. epos = cls.cl_pos;
  2936. });
  2937. cls.cl_constructor <- Some(ctor);
  2938. (* add invoke function to the class *)
  2939. cls.cl_ordered_fields <- invoke_field :: cls.cl_ordered_fields;
  2940. cls.cl_fields <- PMap.add invoke_field.cf_name invoke_field cls.cl_fields;
  2941. cls.cl_overrides <- invoke_field :: cls.cl_overrides;
  2942. (* add this class to the module with gadd_to_module *)
  2943. ft.fgen.gadd_to_module (TClassDecl(cls)) priority;
  2944. (* if there are no captured variables, we can create a cache so subsequent calls don't need to create a new function *)
  2945. let expr, clscapt =
  2946. match captured, tparams with
  2947. | [], [] ->
  2948. let cache_var = ft.fgen.gmk_internal_name "hx" "current" in
  2949. let cache_cf = mk_class_field cache_var (TInst(cls,[])) false func_expr.epos (Var({ v_read = AccNormal; v_write = AccNormal })) [] in
  2950. cls.cl_ordered_statics <- cache_cf :: cls.cl_ordered_statics;
  2951. cls.cl_statics <- PMap.add cache_var cache_cf cls.cl_statics;
  2952. (* if (FuncClass.hx_current != null) FuncClass.hx_current; else (FuncClass.hx_current = new FuncClass()); *)
  2953. (* let mk_static_field_access cl field fieldt pos = *)
  2954. let hx_current = mk_static_field_access cls cache_var (TInst(cls,[])) func_expr.epos in
  2955. let pos = func_expr.epos in
  2956. { fexpr with
  2957. eexpr = TIf(
  2958. {
  2959. eexpr = TBinop(OpNotEq, hx_current, null (TInst(cls,[])) pos);
  2960. etype = ft.fgen.gcon.basic.tbool;
  2961. epos = pos;
  2962. },
  2963. hx_current,
  2964. Some(
  2965. {
  2966. eexpr = TBinop(OpAssign, hx_current, { fexpr with eexpr = TNew(cls, [], captured) });
  2967. etype = (TInst(cls,[]));
  2968. epos = pos;
  2969. }))
  2970. }, (cls,captured)
  2971. | _ ->
  2972. (* change the expression so it will be a new "added class" ( captured variables arguments ) *)
  2973. { fexpr with eexpr = TNew(cls, List.map (fun cl -> TInst(cl,[])) tparams, List.rev captured) }, (cls,captured)
  2974. in
  2975. match delegate_type with
  2976. | None ->
  2977. expr,clscapt
  2978. | Some _ ->
  2979. {
  2980. eexpr = TField(expr, FClosure(Some (cls,[]),invokecf)); (* TODO: FClosure change *)
  2981. etype = invokecf.cf_type;
  2982. epos = cls.cl_pos
  2983. }, clscapt
  2984. in
  2985. let tvar_to_cdecl = Hashtbl.create 0 in
  2986. traverse
  2987. ft.fgen
  2988. ~tparam_anon_decl:(fun v e fn ->
  2989. let _, info = handle_anon_func e fn null_map_info None in
  2990. Hashtbl.add tvar_to_cdecl v.v_id info
  2991. )
  2992. ~tparam_anon_acc:(fun v e -> try
  2993. let cls, captured = Hashtbl.find tvar_to_cdecl v.v_id in
  2994. let types = match v.v_extra with
  2995. | Some(t,_) -> t
  2996. | _ -> assert false
  2997. in
  2998. let monos = List.map (fun _ -> mk_mono()) types in
  2999. let vt = match follow v.v_type with
  3000. | TInst(_, [v]) -> v
  3001. | v -> v
  3002. in
  3003. let et = match follow e.etype with
  3004. | TInst(_, [v]) -> v
  3005. | v -> v
  3006. in
  3007. let original = apply_params types monos vt in
  3008. unify et original;
  3009. let monos = List.map (fun t -> apply_params types (List.map (fun _ -> t_dynamic) types) t) monos in
  3010. let same_cl t1 t2 = match follow t1, follow t2 with
  3011. | TInst(c,_), TInst(c2,_) -> c == c2
  3012. | _ -> false
  3013. in
  3014. let passoc = List.map2 (fun (_,t) m -> t,m) types monos in
  3015. let cltparams = List.map (fun (_,t) ->
  3016. try
  3017. snd (List.find (fun (t2,_) -> same_cl t t2) passoc)
  3018. with | Not_found -> t) cls.cl_params
  3019. in
  3020. { e with eexpr = TNew(cls, cltparams, captured) }
  3021. with
  3022. | Not_found ->
  3023. gen.gcon.warning "This expression may be invalid" e.epos;
  3024. e
  3025. | Unify_error el ->
  3026. List.iter (fun el -> gen.gcon.warning (Typecore.unify_error_msg (print_context()) el) e.epos) el;
  3027. gen.gcon.warning "This expression may be invalid" e.epos;
  3028. e
  3029. )
  3030. (* (transform_closure:texpr->texpr->string->texpr) (handle_anon_func:texpr->tfunc->texpr) (dynamic_func_call:texpr->texpr->texpr list->texpr) *)
  3031. ft.transform_closure
  3032. (fun e f info delegate_type -> fst (handle_anon_func e f info delegate_type))
  3033. ft.dynamic_fun_call
  3034. (* (dynamic_func_call:texpr->texpr->texpr list->texpr) *)
  3035. let configure gen (mapping_func:texpr->texpr) =
  3036. let map e = Some(mapping_func e) in
  3037. gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map
  3038. let configure_as_synf gen (mapping_func:texpr->texpr) =
  3039. let map e = Some(mapping_func e) in
  3040. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority_as_synf) map
  3041. (*
  3042. this submodule will provide the default implementation for the C# and Java targets.
  3043. it will have two return types: double and dynamic, and
  3044. *)
  3045. module DoubleAndDynamicClosureImpl =
  3046. struct
  3047. let get_ctx gen max_arity =
  3048. let basic = gen.gcon.basic in
  3049. let func_args_i i =
  3050. let rec loop i (acc) =
  3051. if i = 0 then (acc) else begin
  3052. let vfloat = alloc_var (gen.gmk_internal_name "fn" ("float" ^ string_of_int i)) basic.tfloat in
  3053. let vdyn = alloc_var (gen.gmk_internal_name "fn" ("dyn" ^ string_of_int i)) t_dynamic in
  3054. loop (i - 1) ((vfloat, None) :: (vdyn, None) :: acc)
  3055. end
  3056. in
  3057. loop i []
  3058. in
  3059. let args_real_to_func args =
  3060. let arity = List.length args in
  3061. if arity >= max_arity then
  3062. [ alloc_var (gen.gmk_internal_name "fn" "dynargs") (basic.tarray t_dynamic), None ]
  3063. else func_args_i arity
  3064. in
  3065. let func_sig_i i =
  3066. let rec loop i acc =
  3067. if i = 0 then acc else begin
  3068. let vfloat = gen.gmk_internal_name "fn" ("float" ^ string_of_int i) in
  3069. let vdyn = gen.gmk_internal_name "fn" ("dyn" ^ string_of_int i) in
  3070. loop (i - 1) ( (vfloat,false,basic.tfloat) :: (vdyn,false,t_dynamic) :: acc )
  3071. end
  3072. in
  3073. loop i []
  3074. in
  3075. let args_real_to_func_sig args =
  3076. let arity = List.length args in
  3077. if arity >= max_arity then
  3078. [gen.gmk_internal_name "fn" "dynargs", false, basic.tarray t_dynamic]
  3079. else begin
  3080. func_sig_i arity
  3081. end
  3082. in
  3083. let rettype_real_to_func t = match run_follow gen t with
  3084. | TType({ t_path = [],"Null" }, _) ->
  3085. 0,t_dynamic
  3086. | _ when like_float t && not (like_i64 t) ->
  3087. (1, basic.tfloat)
  3088. | _ ->
  3089. (0, t_dynamic)
  3090. in
  3091. let args_real_to_func_call el (pos:Ast.pos) =
  3092. if List.length el >= max_arity then
  3093. [{ eexpr = TArrayDecl el; etype = basic.tarray t_dynamic; epos = pos }]
  3094. else begin
  3095. List.fold_left (fun acc e ->
  3096. if like_float (gen.greal_type e.etype) && not (like_i64 (gen.greal_type e.etype)) then
  3097. ( e :: undefined e.epos :: acc )
  3098. else
  3099. ( null basic.tfloat e.epos :: e :: acc )
  3100. ) ([]) (List.rev el)
  3101. end
  3102. in
  3103. let const_type c def =
  3104. match c with
  3105. | TString _ -> basic.tstring | TInt _ -> basic.tint
  3106. | TFloat _ -> basic.tfloat | TBool _ -> basic.tbool
  3107. | _ -> def
  3108. in
  3109. let get_args_func args changed_args pos =
  3110. let arity = List.length args in
  3111. let mk_const const elocal t =
  3112. match const with
  3113. | None -> mk_cast t elocal
  3114. | Some const ->
  3115. { eexpr = TIf(
  3116. { elocal with eexpr = TBinop(Ast.OpEq, elocal, null elocal.etype elocal.epos); etype = basic.tbool },
  3117. { elocal with eexpr = TConst(const); etype = const_type const t },
  3118. Some ( mk_cast t elocal )
  3119. ); etype = t; epos = elocal.epos }
  3120. in
  3121. if arity >= max_arity then begin
  3122. let varray = match changed_args with | [v,_] -> v | _ -> assert false in
  3123. let varray_local = mk_local varray pos in
  3124. 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
  3125. snd (List.fold_left (fun (count,acc) (v,const) ->
  3126. (count + 1,
  3127. {
  3128. eexpr = TVar(v, Some(mk_const const ( mk_varray count ) v.v_type));
  3129. etype = basic.tvoid;
  3130. epos = pos;
  3131. } :: acc)
  3132. ) (0,[]) args)
  3133. end else begin
  3134. let _, dyn_args, float_args = List.fold_left (fun (count,fargs, dargs) arg ->
  3135. if count land 1 = 0 then
  3136. (count + 1, fargs, arg :: dargs)
  3137. else
  3138. (count + 1, arg :: fargs, dargs)
  3139. ) (1,[],[]) (List.rev changed_args) in
  3140. let rec loop acc args fargs dargs =
  3141. match args, fargs, dargs with
  3142. | [], [], [] -> acc
  3143. | (v,const) :: args, (vf,_) :: fargs, (vd,_) :: dargs ->
  3144. let acc = { eexpr = TVar(v, Some(
  3145. {
  3146. eexpr = TIf(
  3147. { eexpr = TBinop(Ast.OpEq, mk_local vd pos, undefined pos); etype = basic.tbool; epos = pos },
  3148. mk_cast v.v_type (mk_local vf pos),
  3149. Some ( mk_const const (mk_local vd pos) v.v_type )
  3150. );
  3151. etype = v.v_type;
  3152. epos = pos
  3153. } )); etype = basic.tvoid; epos = pos } :: acc in
  3154. loop acc args fargs dargs
  3155. | _ -> assert false
  3156. in
  3157. loop [] args float_args dyn_args
  3158. end
  3159. in
  3160. let closure_to_classfield tfunc old_sig pos =
  3161. (* change function signature *)
  3162. let old_args = tfunc.tf_args in
  3163. let changed_args = args_real_to_func old_args in
  3164. (*
  3165. FIXME properly handle int64 cases, which will break here (because of inference to int)
  3166. UPDATE: the fix will be that Int64 won't be a typedef to Float/Int
  3167. *)
  3168. let changed_sig, arity, type_number, changed_sig_ret, is_void, is_dynamic_func = match follow old_sig with
  3169. | TFun(_sig, ret) ->
  3170. let type_n, ret_t = rettype_real_to_func ret in
  3171. let arity = List.length _sig in
  3172. let is_dynamic_func = arity >= max_arity in
  3173. let ret_t = if is_dynamic_func then t_dynamic else ret_t in
  3174. (TFun(args_real_to_func_sig _sig, ret_t), arity, type_n, ret_t, is_void ret, is_dynamic_func)
  3175. | _ -> (print_endline (s_type (print_context()) (follow old_sig) )); assert false
  3176. in
  3177. let tf_expr = if is_void then begin
  3178. let rec map e =
  3179. match e.eexpr with
  3180. | TReturn None -> { e with eexpr = TReturn (Some (null t_dynamic e.epos)) }
  3181. | _ -> Type.map_expr map e
  3182. in
  3183. let e = mk_block (map tfunc.tf_expr) in
  3184. match e.eexpr with
  3185. | TBlock(bl) ->
  3186. { e with eexpr = TBlock(bl @ [{ eexpr = TReturn (Some (null t_dynamic e.epos)); etype = t_dynamic; epos = e.epos }]) }
  3187. | _ -> assert false
  3188. end else tfunc.tf_expr in
  3189. let changed_sig_ret = if is_dynamic_func then t_dynamic else changed_sig_ret in
  3190. (* get real arguments on top of function body *)
  3191. let get_args = get_args_func tfunc.tf_args changed_args pos in
  3192. (*
  3193. FIXME HACK: in order to be able to run the filters that have already ran for this piece of code,
  3194. we will cheat and run it as if it was the whole code
  3195. We could just make ClosuresToClass run before TArrayTransform, but we cannot because of the
  3196. dependency between ClosuresToClass (after DynamicFieldAccess, and before TArrayTransform)
  3197. maybe a way to solve this would be to add an "until" field to run_from
  3198. *)
  3199. let real_get_args = gen.gexpr_filters#run_f { eexpr = TBlock(get_args); etype = basic.tvoid; epos = pos } in
  3200. let func_expr = Type.concat real_get_args tf_expr in
  3201. (* set invoke function *)
  3202. (* todo properly abstract how naming for invoke is made *)
  3203. let invoke_name = if is_dynamic_func then "invokeDynamic" else ("invoke" ^ (string_of_int arity) ^ (if type_number = 0 then "_o" else "_f")) in
  3204. let invoke_name = gen.gmk_internal_name "hx" invoke_name in
  3205. let invoke_field = mk_class_field invoke_name changed_sig false func_expr.epos (Method(MethNormal)) [] in
  3206. let invoke_fun =
  3207. {
  3208. eexpr = TFunction(
  3209. {
  3210. tf_args = changed_args;
  3211. tf_type = changed_sig_ret;
  3212. tf_expr = func_expr;
  3213. });
  3214. etype = changed_sig;
  3215. epos = func_expr.epos;
  3216. } in
  3217. invoke_field.cf_expr <- Some(invoke_fun);
  3218. (invoke_field, [
  3219. { eexpr = TConst(TInt( Int32.of_int arity )); etype = gen.gcon.basic.tint; epos = pos };
  3220. { eexpr = TConst(TInt( Int32.of_int type_number )); etype = gen.gcon.basic.tint; epos = pos };
  3221. ])
  3222. in
  3223. let dynamic_fun_call call_expr =
  3224. let tc, params = match call_expr.eexpr with
  3225. | TCall(tc, params) -> (tc, params)
  3226. | _ -> assert false
  3227. in
  3228. let ct = gen.greal_type call_expr.etype in
  3229. let postfix, ret_t =
  3230. if like_float ct && not (like_i64 ct) then
  3231. "_f", gen.gcon.basic.tfloat
  3232. else
  3233. "_o", t_dynamic
  3234. in
  3235. let params_len = List.length params in
  3236. let ret_t = if params_len >= max_arity then t_dynamic else ret_t in
  3237. let invoke_fun = if params_len >= max_arity then "invokeDynamic" else "invoke" ^ (string_of_int params_len) ^ postfix in
  3238. let invoke_fun = gen.gmk_internal_name "hx" invoke_fun in
  3239. let fun_t = match follow tc.etype with
  3240. | TFun(_sig, _) ->
  3241. TFun(args_real_to_func_sig _sig, ret_t)
  3242. | _ ->
  3243. let i = ref 0 in
  3244. let _sig = List.map (fun p -> let name = "arg" ^ (string_of_int !i) in incr i; (name,false,p.etype) ) params in
  3245. TFun(args_real_to_func_sig _sig, ret_t)
  3246. in
  3247. let may_cast = match follow call_expr.etype with
  3248. | TAbstract ({ a_path = ([], "Void") },[]) -> (fun e -> e)
  3249. | _ -> mk_cast call_expr.etype
  3250. in
  3251. may_cast
  3252. {
  3253. eexpr = TCall(
  3254. { (mk_field_access gen { tc with etype = gen.greal_type tc.etype } invoke_fun tc.epos) with etype = fun_t },
  3255. args_real_to_func_call params call_expr.epos
  3256. );
  3257. etype = ret_t;
  3258. epos = call_expr.epos
  3259. }
  3260. in
  3261. let iname is_function i is_float =
  3262. let postfix = if is_float then "_f" else "_o" in
  3263. gen.gmk_internal_name "hx" ("invoke" ^ (if not is_function then "Field" else "") ^ string_of_int i) ^ postfix
  3264. in
  3265. let map_base_classfields cl is_function map_fn =
  3266. let pos = cl.cl_pos in
  3267. let this_t = TInst(cl,List.map snd cl.cl_params) in
  3268. let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
  3269. let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
  3270. let mk_invoke_i i is_float =
  3271. 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
  3272. cf
  3273. in
  3274. let type_name = gen.gmk_internal_name "fn" "type" in
  3275. let dynamic_arg = alloc_var (gen.gmk_internal_name "fn" "dynargs") (basic.tarray t_dynamic) in
  3276. let mk_invoke_complete_i i is_float =
  3277. (* let arity = i in *)
  3278. let args = func_args_i i in
  3279. (* api fn *)
  3280. (* only cast if needed *)
  3281. let mk_cast tto efrom = gen.ghandle_cast (gen.greal_type tto) (gen.greal_type efrom.etype) efrom in
  3282. let api i t const =
  3283. let vf, _ = List.nth args (i * 2) in
  3284. let vo, _ = List.nth args (i * 2 + 1) in
  3285. let needs_cast, is_float = match t, like_float t && not (like_i64 t) with
  3286. | TAbstract({ a_path = ([], "Float") },[]), _ -> false, true
  3287. | _, true -> true, true
  3288. | _ -> false,false
  3289. in
  3290. let olocal = mk_local vo pos in
  3291. let flocal = mk_local vf pos in
  3292. let get_from_obj e = match const with
  3293. | None -> mk_cast t e
  3294. | Some tc ->
  3295. {
  3296. eexpr = TIf(
  3297. { eexpr = TBinop(Ast.OpEq, olocal, null t_dynamic pos); etype = basic.tbool; epos = pos } ,
  3298. { eexpr = TConst(tc); etype = t; epos = pos },
  3299. Some (mk_cast t e)
  3300. );
  3301. etype = t;
  3302. epos = pos;
  3303. }
  3304. in
  3305. {
  3306. eexpr = TIf(
  3307. { eexpr = TBinop(Ast.OpEq, olocal, undefined pos); etype = basic.tbool; epos = pos },
  3308. (if needs_cast then mk_cast t flocal else flocal),
  3309. Some ( get_from_obj olocal )
  3310. );
  3311. etype = t;
  3312. epos = pos
  3313. }
  3314. in
  3315. (* end of api fn *)
  3316. let ret = if is_float then basic.tfloat else t_dynamic in
  3317. let added_args, fn_expr = map_fn i ret (List.map fst args) api in
  3318. let args = added_args @ args in
  3319. let t = TFun(fun_args args, ret) in
  3320. let tfunction =
  3321. {
  3322. eexpr = TFunction({
  3323. tf_args = args;
  3324. tf_type = ret;
  3325. tf_expr =
  3326. mk_block fn_expr
  3327. });
  3328. etype = t;
  3329. epos = pos;
  3330. }
  3331. in
  3332. let cf = mk_invoke_i i is_float in
  3333. cf.cf_expr <- Some tfunction;
  3334. cf
  3335. in
  3336. let rec loop i cfs =
  3337. if i < 0 then cfs else begin
  3338. (*let mk_invoke_complete_i i is_float =*)
  3339. (mk_invoke_complete_i i false) :: (mk_invoke_complete_i i true) :: (loop (i-1) cfs)
  3340. end
  3341. in
  3342. let cfs = loop max_arity [] in
  3343. let added_s_args, switch =
  3344. let api i t const =
  3345. match i with
  3346. | -1 ->
  3347. mk_local dynamic_arg pos
  3348. | _ ->
  3349. mk_cast t {
  3350. eexpr = TArray(
  3351. mk_local dynamic_arg pos,
  3352. { eexpr = TConst(TInt(Int32.of_int i)); etype = basic.tint; epos = pos });
  3353. etype = t;
  3354. epos = pos;
  3355. }
  3356. in
  3357. map_fn (-1) t_dynamic [dynamic_arg] api
  3358. in
  3359. let args = added_s_args @ [dynamic_arg, None] in
  3360. let dyn_t = TFun(fun_args args, t_dynamic) in
  3361. let dyn_cf = mk_class_field (gen.gmk_internal_name "hx" "invokeDynamic") dyn_t false pos (Method MethNormal) [] in
  3362. dyn_cf.cf_expr <-
  3363. Some {
  3364. eexpr = TFunction({
  3365. tf_args = args;
  3366. tf_type = t_dynamic;
  3367. tf_expr = mk_block switch
  3368. });
  3369. etype = dyn_t;
  3370. epos = pos;
  3371. };
  3372. let additional_cfs = if is_function then begin
  3373. let new_t = TFun(["arity", false, basic.tint; "type", false, basic.tint],basic.tvoid) in
  3374. let new_cf = mk_class_field "new" (new_t) true pos (Method MethNormal) [] in
  3375. let v_arity, v_type = alloc_var "arity" basic.tint, alloc_var "type" basic.tint in
  3376. 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
  3377. let arity_name = gen.gmk_internal_name "hx" "arity" in
  3378. new_cf.cf_expr <-
  3379. Some {
  3380. eexpr = TFunction({
  3381. tf_args = [v_arity, None; v_type, None];
  3382. tf_type = basic.tvoid;
  3383. tf_expr =
  3384. {
  3385. eexpr = TBlock([
  3386. mk_assign v_type type_name;
  3387. mk_assign v_arity arity_name
  3388. ]);
  3389. etype = basic.tvoid;
  3390. epos = pos;
  3391. }
  3392. });
  3393. etype = new_t;
  3394. epos = pos;
  3395. }
  3396. ;
  3397. [
  3398. new_cf;
  3399. mk_class_field type_name basic.tint true pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
  3400. mk_class_field arity_name basic.tint true pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
  3401. ]
  3402. end else [] in
  3403. dyn_cf :: (additional_cfs @ cfs)
  3404. in
  3405. (* maybe another param for prefix *)
  3406. let get_base_classfields_for cl is_function mk_additional_args =
  3407. let pos = cl.cl_pos in
  3408. let this_t = TInst(cl,List.map snd cl.cl_params) in
  3409. let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
  3410. let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
  3411. let rec mk_dyn_call arity api =
  3412. let zero = { eexpr = TConst(TFloat("0.0")); etype = basic.tfloat; epos = pos } in
  3413. let rec loop i acc =
  3414. if i = 0 then acc else begin
  3415. let arr = api (i-1) t_dynamic None in
  3416. loop (i - 1) (zero :: arr :: acc)
  3417. end
  3418. in
  3419. loop arity ([])
  3420. in
  3421. let mk_invoke_switch i (api:(int->t->tconstant option->texpr)) =
  3422. let t = TFun(func_sig_i i,t_dynamic) in
  3423. (* case i: return this.invokeX_o(0, 0, 0, 0, 0, ... arg[0], args[1]....); *)
  3424. ( [{ eexpr = TConst(TInt(Int32.of_int i)); etype = basic.tint; epos = pos }],
  3425. {
  3426. eexpr = TReturn(Some( {
  3427. eexpr = TCall(mk_this (iname is_function i false) t, mk_dyn_call i api);
  3428. etype = t_dynamic;
  3429. epos = pos;
  3430. } ));
  3431. etype = t_dynamic;
  3432. epos = pos;
  3433. } )
  3434. in
  3435. let cl_t = TInst(cl,List.map snd cl.cl_params) in
  3436. let this = { eexpr = TConst(TThis); etype = cl_t; epos = pos } in
  3437. let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
  3438. let mk_int i = { eexpr = TConst(TInt ( Int32.of_int i)); etype = basic.tint; epos = pos } in
  3439. let mk_string s = { eexpr = TConst(TString s); etype = basic.tstring; epos = pos } in
  3440. (*
  3441. if it is the Function class, the base class fields will be
  3442. * hx::invokeX_d|o (where X is from 0 to max_arity) (args)
  3443. {
  3444. if (this.type == 0|1) return invokeX_o|d(args); else throw "Invalid number of arguments."
  3445. }
  3446. hx::invokeDynamic, which will work in the same way
  3447. new(arity, type)
  3448. {
  3449. if (type != 0 && type != 1) throw "Invalid type";
  3450. this.arity = arity;
  3451. this.type = type;
  3452. }
  3453. *)
  3454. let type_name = gen.gmk_internal_name "fn" "type" in
  3455. let mk_expr i is_float vars =
  3456. let name = if is_function then "invoke" else "invokeField" in
  3457. let look_ahead = alloc_var "lookAhead" basic.tbool in
  3458. let add_args = if not is_function then mk_additional_args() else [] in
  3459. let vars = if not is_function then (List.map fst add_args) @ (look_ahead :: vars) else vars in
  3460. let call_expr =
  3461. 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
  3462. {
  3463. 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 );
  3464. etype = if is_float then t_dynamic else basic.tfloat;
  3465. epos = pos
  3466. }
  3467. in
  3468. (*let call_expr = if is_float then mk_cast basic.tfloat call_expr else call_expr in*)
  3469. let if_cond = if is_function then
  3470. { eexpr=TBinop(Ast.OpNotEq, mk_this type_name basic.tint, mk_int (if is_float then 0 else 1) ); etype = basic.tbool; epos = pos }
  3471. else
  3472. mk_local look_ahead pos
  3473. in
  3474. let if_expr = if is_function then
  3475. {
  3476. eexpr = TIf(if_cond,
  3477. { eexpr = TThrow(mk_string "Wrong number of arguments"); etype = basic.tstring; epos = pos },
  3478. Some( { eexpr = TReturn( Some( call_expr ) ); etype = call_expr.etype; epos = pos } )
  3479. );
  3480. etype = t_dynamic;
  3481. epos = pos;
  3482. }
  3483. else
  3484. {
  3485. eexpr = TIf(if_cond,
  3486. { eexpr = TReturn( Some( call_expr ) ); etype = call_expr.etype; epos = pos },
  3487. Some( { eexpr = TThrow(mk_string "Field not found or wrong number of arguments"); etype = basic.tstring; epos = pos } )
  3488. );
  3489. etype = t_dynamic;
  3490. epos = pos;
  3491. }
  3492. in
  3493. let args = if not is_function then (mk_additional_args()) @ [look_ahead, None] else [] in
  3494. (args, if_expr)
  3495. in
  3496. let arities_processed = Hashtbl.create 10 in
  3497. let max_arity = ref 0 in
  3498. let rec loop_cases api arity acc =
  3499. if arity < 0 then acc else
  3500. loop_cases api (arity - 1) (mk_invoke_switch arity api :: acc)
  3501. in
  3502. (* let rec loop goes here *)
  3503. let map_fn cur_arity fun_ret_type vars (api:(int->t->tconstant option->texpr)) =
  3504. let is_float = like_float fun_ret_type && not (like_i64 fun_ret_type) in
  3505. match cur_arity with
  3506. | -1 ->
  3507. let dynargs = api (-1) (t_dynamic) None in
  3508. let switch_cond = mk_field_access gen dynargs "length" pos in
  3509. let switch_cond = {
  3510. eexpr = TIf(
  3511. { eexpr = TBinop(Ast.OpEq, dynargs, null dynargs.etype pos); etype = basic.tbool; epos = pos; },
  3512. { eexpr = TConst(TInt(Int32.zero)); etype = basic.tint; epos = pos },
  3513. Some switch_cond);
  3514. etype = basic.tint;
  3515. epos = pos;
  3516. } in
  3517. let switch =
  3518. {
  3519. eexpr = TSwitch( switch_cond,
  3520. loop_cases api !max_arity [],
  3521. Some({ eexpr = TThrow(mk_string "Too many arguments"); etype = basic.tvoid; epos = pos; }) );
  3522. etype = basic.tvoid;
  3523. epos = pos;
  3524. } in
  3525. ( (if not is_function then mk_additional_args () else []), switch )
  3526. | _ ->
  3527. if not (Hashtbl.mem arities_processed cur_arity) then begin
  3528. Hashtbl.add arities_processed cur_arity true;
  3529. if cur_arity > !max_arity then max_arity := cur_arity
  3530. end;
  3531. mk_expr cur_arity is_float vars
  3532. in
  3533. map_base_classfields cl is_function map_fn
  3534. in
  3535. let initialize_base_class cl =
  3536. ()
  3537. in
  3538. {
  3539. fgen = gen;
  3540. func_class = null_class;
  3541. closure_to_classfield = closure_to_classfield;
  3542. dynamic_fun_call = dynamic_fun_call;
  3543. (*
  3544. called once so the implementation can make one of a time initializations in the base class
  3545. for all functions
  3546. *)
  3547. initialize_base_class = initialize_base_class;
  3548. (*
  3549. Base classfields are the class fields for the abstract implementation of either the Function implementation,
  3550. or the invokeField implementation for the classes
  3551. They will either try to call the right function or will fail with
  3552. (tclass - subject (so we know the type of this)) -> is_function_base -> list of the abstract implementation class fields
  3553. *)
  3554. get_base_classfields_for = get_base_classfields_for;
  3555. map_base_classfields = map_base_classfields;
  3556. (*
  3557. for now we won't deal with the closures.
  3558. They can be dealt with the module ReflectionCFs,
  3559. or a custom implementation
  3560. *)
  3561. transform_closure = (fun tclosure texpr str -> tclosure);
  3562. }
  3563. end;;
  3564. end;;
  3565. (* ******************************************* *)
  3566. (* Type Parameters *)
  3567. (* ******************************************* *)
  3568. (*
  3569. This module will handle type parameters. There are lots of changes we need to do to correctly support type parameters:
  3570. traverse will:
  3571. V Detect when parameterized function calls are made
  3572. * Detect when a parameterized class instance is being cast to another parameter
  3573. * Change new<> parameterized function calls
  3574. *
  3575. extras:
  3576. * On languages that support "real" type parameters, a Cast function is provided that will convert from a <Dynamic> to the requested type.
  3577. 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.
  3578. Other implementations may be delegated to the runtime.
  3579. * 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())
  3580. * Type parameter renaming to avoid name clash
  3581. * Detect type parameter casting and call Cast<> instead
  3582. for java:
  3583. * 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.
  3584. dependencies:
  3585. must run after casts are detected. This will be ensured at CastDetect module.
  3586. *)
  3587. module TypeParams =
  3588. struct
  3589. let name = "type_params"
  3590. let priority = max_dep -. 20.
  3591. (* this function will receive the original function argument, the applied function argument and the original function parameters. *)
  3592. (* from this info, it will infer the applied tparams for the function *)
  3593. (* this function is used by CastDetection module *)
  3594. 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 =
  3595. match params with
  3596. | [] -> []
  3597. | _ ->
  3598. 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
  3599. let monos = List.map (fun _ -> mk_mono()) params in
  3600. let original = args_list (get_fun (apply_params params monos (TFun(fst original_args,snd original_args)))) in
  3601. let applied = args_list applied_args in
  3602. (try
  3603. List.iter2 (fun a o ->
  3604. let o = run_follow gen o in
  3605. let a = run_follow gen a in
  3606. unify a o
  3607. (* type_eq EqStrict a o *)
  3608. ) applied original
  3609. (* unify applied original *)
  3610. with | Unify_error el ->
  3611. (* List.iter (fun el -> gen.gcon.warning (Typecore.unify_error_msg (print_context()) el) pos) el; *)
  3612. gen.gcon.warning ("This expression may be invalid") pos
  3613. | Invalid_argument("List.map2") ->
  3614. gen.gcon.warning ("This expression may be invalid") pos
  3615. );
  3616. List.map (fun t ->
  3617. match follow t with
  3618. | TMono _ -> t_empty
  3619. | t -> t
  3620. ) monos
  3621. (* ******************************************* *)
  3622. (* Real Type Parameters Module *)
  3623. (* ******************************************* *)
  3624. (*
  3625. This submodule is by now specially made for the .NET platform. There might be other targets that will
  3626. make use of this, but it IS very specific.
  3627. On the .NET platform, generics are real specialized classes that are JIT compiled. For this reason, we cannot
  3628. cast from one type parameter to another. Also there is no common type for the type parameters, so for example
  3629. an instance of type Array<Int> will return false for instance is Array<object> .
  3630. So we need to:
  3631. 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
  3632. 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.
  3633. 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
  3634. 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)
  3635. 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.
  3636. special literals:
  3637. 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,
  3638. so we can compare them. In C# it's the equivalent of typeof(T).TypeHandle (TypeHandle compare is faster than System.Type.Equals())
  3639. dependencies:
  3640. (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
  3641. Must run AFTER CastDetect. This will be ensured per CastDetect
  3642. *)
  3643. module RealTypeParams =
  3644. struct
  3645. let name = "real_type_params"
  3646. let priority = priority
  3647. let cast_field_name = "cast"
  3648. let rec has_type_params t =
  3649. match follow t with
  3650. | TInst( { cl_kind = KTypeParameter _ }, _) -> true
  3651. | TAbstract(_, params)
  3652. | TEnum(_, params)
  3653. | TInst(_, params) -> List.exists (fun t -> has_type_params t) params
  3654. | TFun(args,ret) ->
  3655. List.exists (fun (n,o,t) -> has_type_params t) args || has_type_params ret
  3656. | _ -> false
  3657. let rec follow_all_md md =
  3658. match md with
  3659. | TClassDecl { cl_kind = KAbstractImpl a } ->
  3660. follow_all_md (TAbstractDecl a)
  3661. | TAbstractDecl a -> if Meta.has Meta.CoreType a.a_meta then
  3662. None
  3663. else (
  3664. match follow (apply_params a.a_params (List.map snd a.a_params) a.a_this) with
  3665. | TInst(c,_) -> follow_all_md (TClassDecl c)
  3666. | TEnum(e,_) -> follow_all_md (TEnumDecl e)
  3667. | TAbstract(a,_) -> follow_all_md (TAbstractDecl a)
  3668. | TType(t,_) -> follow_all_md (TTypeDecl t)
  3669. | _ -> None)
  3670. | TTypeDecl t -> (
  3671. match follow (apply_params t.t_params (List.map snd t.t_params) t.t_type) with
  3672. | TInst(c,_) -> follow_all_md (TClassDecl c)
  3673. | TEnum(e,_) -> follow_all_md (TEnumDecl e)
  3674. | TAbstract(a,_) -> follow_all_md (TAbstractDecl a)
  3675. | TType(t,_) -> follow_all_md (TTypeDecl t)
  3676. | _ -> None)
  3677. | md -> Some md
  3678. let rec is_hxgeneric md =
  3679. match md with
  3680. | TClassDecl { cl_kind = KAbstractImpl a } ->
  3681. is_hxgeneric (TAbstractDecl a)
  3682. | TClassDecl(cl) ->
  3683. not (Meta.has Meta.NativeGeneric cl.cl_meta)
  3684. | TEnumDecl(e) ->
  3685. not (Meta.has Meta.NativeGeneric e.e_meta)
  3686. | md -> match follow_all_md md with
  3687. | Some md -> is_hxgeneric md
  3688. | None -> true
  3689. let rec set_hxgeneric gen mds isfirst md =
  3690. let path = t_path md in
  3691. if List.exists (fun m -> path = t_path m) mds then begin
  3692. if isfirst then
  3693. None (* we still can't determine *)
  3694. else
  3695. Some true (* if we're in second pass and still can't determine, it's because it can be hxgeneric *)
  3696. end else begin
  3697. let has_unresolved = ref false in
  3698. let is_false v =
  3699. match v with
  3700. | Some false -> true
  3701. | None -> has_unresolved := true; false
  3702. | Some true -> false
  3703. in
  3704. let mds = md :: mds in
  3705. match md with
  3706. | TClassDecl(cl) ->
  3707. (* first see if any meta is present (already processed) *)
  3708. if Meta.has Meta.NativeGeneric cl.cl_meta then
  3709. Some false
  3710. else if Meta.has Meta.HaxeGeneric cl.cl_meta then
  3711. Some true
  3712. else if cl.cl_params = [] && is_hxgen md then
  3713. (cl.cl_meta <- (Meta.HaxeGeneric,[],cl.cl_pos) :: cl.cl_meta;
  3714. Some true)
  3715. else if cl.cl_params = [] then
  3716. (cl.cl_meta <- (Meta.NativeGeneric, [], cl.cl_pos) :: cl.cl_meta;
  3717. Some false)
  3718. else if not (is_hxgen md) then
  3719. (cl.cl_meta <- (Meta.NativeGeneric, [], cl.cl_pos) :: cl.cl_meta;
  3720. Some false)
  3721. else begin
  3722. (*
  3723. if it's not present, see if any superclass is nativegeneric.
  3724. nativegeneric is inherited, while hxgeneric can be later changed to nativegeneric
  3725. *)
  3726. (* on the first pass, our job is to find any evidence that makes it not be hxgeneric. Otherwise it will be hxgeneric *)
  3727. match cl.cl_super with
  3728. | Some (c,_) when is_false (set_hxgeneric gen mds isfirst (TClassDecl c)) ->
  3729. cl.cl_meta <- (Meta.NativeGeneric, [], cl.cl_pos) :: cl.cl_meta;
  3730. Some false
  3731. | _ ->
  3732. (* see if it's a generic class *)
  3733. match cl.cl_params with
  3734. | [] ->
  3735. (* if it's not, then it will follow hxgen *)
  3736. if is_hxgen (TClassDecl cl) then
  3737. cl.cl_meta <- (Meta.HaxeGeneric, [], cl.cl_pos) :: cl.cl_meta
  3738. else
  3739. cl.cl_meta <- (Meta.NativeGeneric, [], cl.cl_pos) :: cl.cl_meta;
  3740. Some true
  3741. | _ ->
  3742. (* if it is, loop through all fields + statics and look for non-hxgeneric
  3743. generic classes that have KTypeParameter as params *)
  3744. let rec loop cfs =
  3745. match cfs with
  3746. | [] -> false
  3747. | cf :: cfs ->
  3748. let t = follow (gen.greal_type cf.cf_type) in
  3749. match t with
  3750. | TInst( { cl_kind = KTypeParameter _ }, _ ) -> loop cfs
  3751. | TInst(cl,p) when has_type_params t && is_false (set_hxgeneric gen mds isfirst (TClassDecl cl)) ->
  3752. if not (Hashtbl.mem gen.gtparam_cast cl.cl_path) then true else loop cfs
  3753. | TEnum(e,p) when has_type_params t && is_false (set_hxgeneric gen mds isfirst (TEnumDecl e)) ->
  3754. if not (Hashtbl.mem gen.gtparam_cast e.e_path) then true else loop cfs
  3755. | _ -> loop cfs (* TAbstracts / Dynamics can't be generic *)
  3756. in
  3757. if loop cl.cl_ordered_fields then begin
  3758. cl.cl_meta <- (Meta.NativeGeneric, [], cl.cl_pos) :: cl.cl_meta;
  3759. Some false
  3760. end else if isfirst && !has_unresolved then
  3761. None
  3762. else begin
  3763. cl.cl_meta <- (Meta.HaxeGeneric, [], cl.cl_pos) :: cl.cl_meta;
  3764. Some true
  3765. end
  3766. end
  3767. | TEnumDecl e ->
  3768. if Meta.has Meta.NativeGeneric e.e_meta then
  3769. Some false
  3770. else if Meta.has Meta.HaxeGeneric e.e_meta then
  3771. Some true
  3772. else if not (is_hxgen (TEnumDecl e)) then begin
  3773. e.e_meta <- (Meta.NativeGeneric, [], e.e_pos) :: e.e_meta;
  3774. Some false
  3775. end else begin
  3776. (* if enum is not generic, then it's hxgeneric *)
  3777. match e.e_params with
  3778. | [] ->
  3779. e.e_meta <- (Meta.HaxeGeneric, [], e.e_pos) :: e.e_meta;
  3780. Some true
  3781. | _ ->
  3782. let rec loop efs =
  3783. match efs with
  3784. | [] -> false
  3785. | ef :: efs ->
  3786. let t = follow (gen.greal_type ef.ef_type) in
  3787. match t with
  3788. | TFun(args, _) ->
  3789. if List.exists (fun (n,o,t) ->
  3790. let t = follow t in
  3791. match t with
  3792. | TInst( { cl_kind = KTypeParameter _ }, _ ) ->
  3793. false
  3794. | TInst(cl,p) when has_type_params t && is_false (set_hxgeneric gen mds isfirst (TClassDecl cl)) ->
  3795. not (Hashtbl.mem gen.gtparam_cast cl.cl_path)
  3796. | TEnum(e,p) when has_type_params t && is_false (set_hxgeneric gen mds isfirst (TEnumDecl e)) ->
  3797. not (Hashtbl.mem gen.gtparam_cast e.e_path)
  3798. | _ -> false
  3799. ) args then
  3800. true
  3801. else
  3802. loop efs
  3803. | _ -> loop efs
  3804. in
  3805. let efs = PMap.fold (fun ef acc -> ef :: acc) e.e_constrs [] in
  3806. if loop efs then begin
  3807. e.e_meta <- (Meta.NativeGeneric, [], e.e_pos) :: e.e_meta;
  3808. Some false
  3809. end else if isfirst && !has_unresolved then
  3810. None
  3811. else begin
  3812. e.e_meta <- (Meta.HaxeGeneric, [], e.e_pos) :: e.e_meta;
  3813. Some true
  3814. end
  3815. end
  3816. | _ -> assert false
  3817. end
  3818. let set_hxgeneric gen md =
  3819. let ret = match md with
  3820. | TClassDecl { cl_kind = KAbstractImpl a } -> (match follow_all_md md with
  3821. | Some md ->
  3822. let ret = set_hxgeneric gen [] true md in
  3823. if ret = None then get (set_hxgeneric gen [] false md) else get ret
  3824. | None ->
  3825. true)
  3826. | _ -> match set_hxgeneric gen [] true md with
  3827. | None ->
  3828. get (set_hxgeneric gen [] false md)
  3829. | Some v ->
  3830. v
  3831. in
  3832. if not ret then begin
  3833. match md with
  3834. | TClassDecl c ->
  3835. let set_hxgeneric (_,param) = match follow param with
  3836. | TInst(c,_) ->
  3837. c.cl_meta <- (Meta.NativeGeneric, [], c.cl_pos) :: c.cl_meta
  3838. | _ -> ()
  3839. in
  3840. List.iter set_hxgeneric c.cl_params;
  3841. let rec handle_field cf =
  3842. List.iter set_hxgeneric cf.cf_params;
  3843. List.iter handle_field cf.cf_overloads
  3844. in
  3845. (match c.cl_kind with
  3846. | KAbstractImpl a ->
  3847. List.iter set_hxgeneric a.a_params;
  3848. | _ -> ());
  3849. List.iter handle_field c.cl_ordered_fields;
  3850. List.iter handle_field c.cl_ordered_statics
  3851. | _ -> ()
  3852. end;
  3853. ret
  3854. let params_has_tparams params =
  3855. List.fold_left (fun acc t -> acc || has_type_params t) false params
  3856. (* ******************************************* *)
  3857. (* RealTypeParamsModf *)
  3858. (* ******************************************* *)
  3859. (*
  3860. This is the module filter of Real Type Parameters. It will traverse through all types and look for hxgeneric classes (only classes).
  3861. When found, a parameterless interface will be created and associated via the "ifaces" Hashtbl to the original class.
  3862. Also a "cast" function will be automatically generated which will handle unsafe downcasts to more specific type parameters (necessary for serialization)
  3863. dependencies:
  3864. Anything that may create hxgeneric classes must run before it.
  3865. Should run before ReflectionCFs (this dependency will be added to ReflectionCFs), so the added interfaces also get to be real IHxObject's
  3866. *)
  3867. module RealTypeParamsModf =
  3868. struct
  3869. let set_only_hxgeneric gen =
  3870. let rec run md =
  3871. match md with
  3872. | TTypeDecl _ | TAbstractDecl _ -> md
  3873. | _ -> ignore (set_hxgeneric gen md); md
  3874. in
  3875. run
  3876. let name = "real_type_params_modf"
  3877. let priority = solve_deps name []
  3878. let rec get_fields gen cl params_cl params_cf acc =
  3879. let fields = List.fold_left (fun acc cf ->
  3880. match follow (gen.greal_type (gen.gfollow#run_f (cf.cf_type))) with
  3881. | TInst(cli, ((_ :: _) as p)) when (not (is_hxgeneric (TClassDecl cli))) && params_has_tparams p ->
  3882. (cf, apply_params cl.cl_params params_cl cf.cf_type, apply_params cl.cl_params params_cf cf.cf_type) :: acc
  3883. | TEnum(e, ((_ :: _) as p)) when not (is_hxgeneric (TEnumDecl e)) && params_has_tparams p ->
  3884. (cf, apply_params cl.cl_params params_cl cf.cf_type, apply_params cl.cl_params params_cf cf.cf_type) :: acc
  3885. | _ -> acc
  3886. ) [] cl.cl_ordered_fields in
  3887. match cl.cl_super with
  3888. | Some(cs, tls) ->
  3889. 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)
  3890. | None -> (fields @ acc)
  3891. (* overrides all needed cast functions from super classes / interfaces to call the new cast function *)
  3892. let create_stub_casts gen cl cast_cfield =
  3893. (* go through superclasses and interfaces *)
  3894. let p = cl.cl_pos in
  3895. let this = { eexpr = TConst TThis; etype = (TInst(cl, List.map snd cl.cl_params)); epos = p } in
  3896. let rec loop cls tls level reverse_params =
  3897. if (level <> 0 || cls.cl_interface) && tls <> [] && is_hxgeneric (TClassDecl cls) then begin
  3898. let cparams = List.map (fun (s,t) -> (s, TInst (map_param (get_cl_t t), []))) cls.cl_params in
  3899. let name = String.concat "_" ((fst cls.cl_path) @ [snd cls.cl_path; cast_field_name]) in
  3900. if not (PMap.mem name cl.cl_fields) then begin
  3901. let reverse_params = List.map (apply_params cls.cl_params (List.map snd cparams)) reverse_params in
  3902. let cfield = mk_class_field name (TFun([], t_dynamic)) false cl.cl_pos (Method MethNormal) cparams in
  3903. 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
  3904. let call =
  3905. {
  3906. eexpr = TCall(field, []);
  3907. etype = t_dynamic;
  3908. epos = p;
  3909. } in
  3910. let call = gen.gparam_func_call call field reverse_params [] in
  3911. let delay () =
  3912. cfield.cf_expr <-
  3913. Some {
  3914. eexpr = TFunction(
  3915. {
  3916. tf_args = [];
  3917. tf_type = t_dynamic;
  3918. tf_expr =
  3919. {
  3920. eexpr = TReturn( Some call );
  3921. etype = t_dynamic;
  3922. epos = p;
  3923. }
  3924. });
  3925. etype = cfield.cf_type;
  3926. epos = p;
  3927. }
  3928. in
  3929. gen.gafter_filters_ended <- delay :: gen.gafter_filters_ended; (* do not let filters alter this expression content *)
  3930. cl.cl_ordered_fields <- cfield :: cl.cl_ordered_fields;
  3931. cl.cl_fields <- PMap.add cfield.cf_name cfield cl.cl_fields;
  3932. if level <> 0 then cl.cl_overrides <- cfield :: cl.cl_overrides
  3933. end
  3934. end;
  3935. let get_reverse super supertl =
  3936. let kv = List.map2 (fun (_,tparam) applied -> (follow applied, follow tparam)) super.cl_params supertl in
  3937. List.map (fun t ->
  3938. try
  3939. List.assq (follow t) kv
  3940. with | Not_found -> t
  3941. ) reverse_params
  3942. in
  3943. (match cls.cl_super with
  3944. | None -> ()
  3945. | Some(super, supertl) ->
  3946. loop super supertl (level + 1) (get_reverse super supertl));
  3947. List.iter (fun (iface, ifacetl) ->
  3948. loop iface ifacetl level (get_reverse iface ifacetl)
  3949. ) cls.cl_implements
  3950. in
  3951. loop cl (List.map snd cl.cl_params) 0 (List.map snd cl.cl_params)
  3952. (*
  3953. Creates a cast classfield, with the desired name
  3954. Will also look for previous cast() definitions and override them, to reflect the current type and fields
  3955. 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
  3956. 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
  3957. the current type correctly.
  3958. *)
  3959. let create_cast_cfield gen cl name =
  3960. let basic = gen.gcon.basic in
  3961. let cparams = List.map (fun (s,t) -> (s, TInst (map_param (get_cl_t t), []))) cl.cl_params in
  3962. let cfield = mk_class_field name (TFun([], t_dynamic)) false cl.cl_pos (Method MethNormal) cparams in
  3963. let params = List.map snd cparams in
  3964. let fields = get_fields gen cl (List.map snd cl.cl_params) params [] in
  3965. (* now create the contents of the function *)
  3966. (*
  3967. it will look something like:
  3968. if (typeof(T) == typeof(T2)) return this;
  3969. var new_me = new CurrentClass<T2>(EmptyInstnace);
  3970. for (field in Reflect.fields(this))
  3971. {
  3972. switch(field)
  3973. {
  3974. case "aNativeArray":
  3975. var newArray = new NativeArray(this.aNativeArray.Length);
  3976. default:
  3977. Reflect.setField(new_me, field, Reflect.field(this, field));
  3978. }
  3979. }
  3980. *)
  3981. let new_t = TInst(cl, params) in
  3982. let pos = cl.cl_pos in
  3983. let new_me_var = alloc_var "new_me" new_t in
  3984. let local_new_me = { eexpr = TLocal(new_me_var); etype = new_t; epos = pos } in
  3985. let this = { eexpr = TConst(TThis); etype = (TInst(cl, List.map snd cl.cl_params)); epos = pos } in
  3986. let field_var = alloc_var "field" gen.gcon.basic.tstring in
  3987. let local_field = { eexpr = TLocal(field_var); etype = field_var.v_type; epos = pos } in
  3988. let i_var = alloc_var "i" gen.gcon.basic.tint in
  3989. let local_i = { eexpr = TLocal(i_var); etype = gen.gcon.basic.tint; epos = pos } in
  3990. let incr_i = { eexpr = TUnop(Ast.Increment, Ast.Postfix, local_i); etype = basic.tint; epos = pos } in
  3991. let fields_var = alloc_var "fields" (gen.gcon.basic.tarray gen.gcon.basic.tstring) in
  3992. let local_fields = { eexpr = TLocal(fields_var); etype = (gen.gcon.basic.tarray gen.gcon.basic.tstring); epos = pos } in
  3993. let get_path t =
  3994. match follow t with
  3995. | TInst(cl,_) -> cl.cl_path
  3996. | TEnum(e,_) -> e.e_path
  3997. | TAbstract(a,_) -> a.a_path
  3998. | TMono _
  3999. | TDynamic _ -> ([], "Dynamic")
  4000. | _ -> assert false
  4001. in
  4002. (* this will take all fields that were *)
  4003. let fields_to_cases fields =
  4004. List.map (fun (cf, t_cl, t_cf) ->
  4005. let this_field = { eexpr = TField(this, FInstance(cl, List.map snd cl.cl_params, cf)); etype = t_cl; epos = pos } in
  4006. let expr =
  4007. {
  4008. eexpr = TBinop(OpAssign, { eexpr = TField(local_new_me, FInstance(cl, List.map snd cl.cl_params, cf) ); etype = t_cf; epos = pos },
  4009. 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
  4010. );
  4011. etype = t_cf;
  4012. epos = pos;
  4013. } in
  4014. ([{ eexpr = TConst(TString(cf.cf_name)); etype = gen.gcon.basic.tstring; epos = pos }], expr)
  4015. ) fields
  4016. in
  4017. let mk_typehandle =
  4018. let thandle = alloc_var "__typeof__" t_dynamic in
  4019. (fun cl -> { eexpr = TCall(mk_local thandle pos, [ mk_classtype_access cl pos ]); etype = t_dynamic; epos = pos })
  4020. in
  4021. let mk_eq cl1 cl2 =
  4022. { eexpr = TBinop(Ast.OpEq, mk_typehandle cl1, mk_typehandle cl2); etype = basic.tbool; epos = pos }
  4023. in
  4024. let rec mk_typehandle_cond thisparams cfparams =
  4025. match thisparams, cfparams with
  4026. | TInst(cl_this,[]) :: [], TInst(cl_cf,[]) :: [] ->
  4027. mk_eq cl_this cl_cf
  4028. | TInst(cl_this,[]) :: hd, TInst(cl_cf,[]) :: hd2 ->
  4029. { eexpr = TBinop(Ast.OpBoolAnd, mk_eq cl_this cl_cf, mk_typehandle_cond hd hd2); etype = basic.tbool; epos = pos }
  4030. | v :: hd, v2 :: hd2 ->
  4031. (match follow v, follow v2 with
  4032. | (TInst(cl1,[]) as v), (TInst(cl2,[]) as v2) ->
  4033. mk_typehandle_cond (v :: hd) (v2 :: hd2)
  4034. | _ ->
  4035. assert false
  4036. )
  4037. | _ -> assert false
  4038. in
  4039. let fn =
  4040. {
  4041. tf_args = [];
  4042. tf_type = t_dynamic;
  4043. tf_expr =
  4044. {
  4045. eexpr = TBlock([
  4046. (* if (typeof(T) == typeof(T2)) return this *)
  4047. {
  4048. eexpr = TIf(
  4049. mk_typehandle_cond (List.map snd cl.cl_params) params,
  4050. mk_return this,
  4051. None);
  4052. etype = basic.tvoid;
  4053. epos = pos;
  4054. };
  4055. (* var new_me = /*special create empty with tparams construct*/ *)
  4056. {
  4057. eexpr = TVar(new_me_var, Some(gen.gtools.rf_create_empty cl params pos));
  4058. etype = gen.gcon.basic.tvoid;
  4059. epos = pos
  4060. };
  4061. (* var fields = Reflect.fields(this); *)
  4062. {
  4063. eexpr = TVar(fields_var, Some(gen.gtools.r_fields true this));
  4064. etype = gen.gcon.basic.tvoid;
  4065. epos = pos
  4066. };
  4067. (* var i = 0; *)
  4068. {
  4069. eexpr = TVar(i_var, Some(mk_int gen 0 pos));
  4070. etype = gen.gcon.basic.tvoid;
  4071. epos = pos
  4072. };
  4073. {
  4074. eexpr = TWhile( (* while (i < fields.length) *)
  4075. {
  4076. eexpr = TBinop(Ast.OpLt,
  4077. local_i,
  4078. mk_field_access gen local_fields "length" pos);
  4079. etype = gen.gcon.basic.tbool;
  4080. epos = pos
  4081. },
  4082. {
  4083. eexpr = TBlock [
  4084. (* var field = fields[i++]; *)
  4085. {
  4086. eexpr = TVar(field_var, Some { eexpr = TArray (local_fields, incr_i); etype = gen.gcon.basic.tstring; epos = pos });
  4087. etype = gen.gcon.basic.tvoid;
  4088. epos = pos
  4089. };
  4090. (
  4091. (* default: Reflect.setField(new_me, field, Reflect.field(this, field)) *)
  4092. 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
  4093. if fields <> [] then
  4094. (* switch(field) { ... } *)
  4095. {
  4096. eexpr = TSwitch(local_field, fields_to_cases fields, Some(edef));
  4097. etype = gen.gcon.basic.tvoid;
  4098. epos = pos;
  4099. }
  4100. else
  4101. edef;
  4102. )
  4103. ];
  4104. etype = gen.gcon.basic.tvoid;
  4105. epos = pos
  4106. },
  4107. Ast.NormalWhile
  4108. );
  4109. etype = gen.gcon.basic.tvoid;
  4110. epos = pos;
  4111. };
  4112. (* return new_me *)
  4113. mk_return local_new_me
  4114. ]);
  4115. etype = t_dynamic;
  4116. epos = pos;
  4117. };
  4118. }
  4119. in
  4120. cfield.cf_expr <- Some( { eexpr = TFunction(fn); etype = cfield.cf_type; epos = pos } );
  4121. cfield
  4122. let create_static_cast_cf gen iface cf =
  4123. let p = iface.cl_pos in
  4124. let basic = gen.gcon.basic in
  4125. let cparams = List.map (fun (s,t) -> ("To_" ^ s, TInst (map_param (get_cl_t t), []))) cf.cf_params in
  4126. let me_type = TInst(iface,[]) in
  4127. let cfield = mk_class_field "__hx_cast" (TFun(["me",false,me_type], t_dynamic)) false iface.cl_pos (Method MethNormal) (cparams) in
  4128. let params = List.map snd cparams in
  4129. let me = alloc_var "me" me_type in
  4130. 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
  4131. let call =
  4132. {
  4133. eexpr = TCall(field, []);
  4134. etype = t_dynamic;
  4135. epos = p;
  4136. } in
  4137. let call = gen.gparam_func_call call field params [] in
  4138. (* since object.someCall<ExplicitParameterDefinition>() isn't allowed on Haxe, we need to directly apply the params and delay this call *)
  4139. let delay () =
  4140. cfield.cf_expr <-
  4141. Some {
  4142. eexpr = TFunction(
  4143. {
  4144. tf_args = [me,None];
  4145. tf_type = t_dynamic;
  4146. tf_expr =
  4147. {
  4148. eexpr = TReturn( Some
  4149. {
  4150. eexpr = TIf(
  4151. { eexpr = TBinop(Ast.OpNotEq, mk_local me p, null me.v_type p); etype = basic.tbool; epos = p },
  4152. call,
  4153. Some( null me.v_type p )
  4154. );
  4155. etype = t_dynamic;
  4156. epos = p;
  4157. });
  4158. etype = basic.tvoid;
  4159. epos = p;
  4160. }
  4161. });
  4162. etype = cfield.cf_type;
  4163. epos = p;
  4164. }
  4165. in
  4166. cfield, delay
  4167. let get_cast_name cl = String.concat "_" ((fst cl.cl_path) @ [snd cl.cl_path; cast_field_name]) (* explicitly define it *)
  4168. let default_implementation gen ifaces base_generic =
  4169. let add_iface cl =
  4170. gen.gadd_to_module (TClassDecl cl) (max_dep);
  4171. in
  4172. let implement_stub_cast cthis iface tl =
  4173. let name = get_cast_name iface in
  4174. if not (PMap.mem name cthis.cl_fields) then begin
  4175. let cparams = List.map (fun (s,t) -> ("To_" ^ s, TInst(map_param (get_cl_t t), []))) iface.cl_params in
  4176. let field = mk_class_field name (TFun([],t_dynamic)) false iface.cl_pos (Method MethNormal) cparams in
  4177. let this = { eexpr = TConst TThis; etype = TInst(cthis, List.map snd cthis.cl_params); epos = cthis.cl_pos } in
  4178. field.cf_expr <- Some {
  4179. etype = TFun([],t_dynamic);
  4180. epos = this.epos;
  4181. eexpr = TFunction {
  4182. tf_type = t_dynamic;
  4183. tf_args = [];
  4184. tf_expr = mk_block { this with
  4185. eexpr = TReturn (Some this)
  4186. }
  4187. }
  4188. };
  4189. cthis.cl_ordered_fields <- field :: cthis.cl_ordered_fields;
  4190. cthis.cl_fields <- PMap.add name field cthis.cl_fields
  4191. end
  4192. in
  4193. let rec run md =
  4194. match md with
  4195. | TClassDecl ({ cl_params = [] } as cl) ->
  4196. (* see if we're implementing any generic interface *)
  4197. let rec check (iface,tl) =
  4198. if tl <> [] && set_hxgeneric gen (TClassDecl iface) then
  4199. (* implement cast stub *)
  4200. implement_stub_cast cl iface tl;
  4201. List.iter (fun (s,stl) -> check (s, List.map (apply_params iface.cl_params tl) stl)) iface.cl_implements;
  4202. in
  4203. List.iter (check) cl.cl_implements;
  4204. md
  4205. | TClassDecl ({ cl_params = hd :: tl } as cl) when set_hxgeneric gen md ->
  4206. let iface = mk_class cl.cl_module cl.cl_path cl.cl_pos in
  4207. iface.cl_array_access <- Option.map (apply_params (cl.cl_params) (List.map (fun _ -> t_dynamic) cl.cl_params)) cl.cl_array_access;
  4208. iface.cl_module <- cl.cl_module;
  4209. iface.cl_meta <- (Meta.HxGen, [], cl.cl_pos) :: iface.cl_meta;
  4210. Hashtbl.add ifaces cl.cl_path iface;
  4211. iface.cl_implements <- (base_generic, []) :: iface.cl_implements;
  4212. iface.cl_interface <- true;
  4213. cl.cl_implements <- (iface, []) :: cl.cl_implements;
  4214. let name = get_cast_name cl in
  4215. let cast_cf = create_cast_cfield gen cl name in
  4216. if not cl.cl_interface then create_stub_casts gen cl cast_cf;
  4217. let rec loop c = match c.cl_super with
  4218. | None -> ()
  4219. | Some(sup,_) -> try
  4220. let siface = Hashtbl.find ifaces sup.cl_path in
  4221. iface.cl_implements <- (siface,[]) :: iface.cl_implements;
  4222. ()
  4223. with | Not_found -> loop sup
  4224. in
  4225. loop cl;
  4226. (if not cl.cl_interface then cl.cl_ordered_fields <- cast_cf :: cl.cl_ordered_fields);
  4227. let iface_cf = mk_class_field name cast_cf.cf_type false cast_cf.cf_pos (Method MethNormal) cast_cf.cf_params in
  4228. let cast_static_cf, delay = create_static_cast_cf gen iface iface_cf in
  4229. cl.cl_ordered_statics <- cast_static_cf :: cl.cl_ordered_statics;
  4230. cl.cl_statics <- PMap.add cast_static_cf.cf_name cast_static_cf cl.cl_statics;
  4231. gen.gafter_filters_ended <- delay :: gen.gafter_filters_ended; (* do not let filters alter this expression content *)
  4232. iface_cf.cf_type <- cast_cf.cf_type;
  4233. iface.cl_fields <- PMap.add name iface_cf iface.cl_fields;
  4234. let fields = List.filter (fun cf -> match cf.cf_kind with
  4235. | Var _ | Method MethDynamic -> false
  4236. | _ ->
  4237. let is_override = List.memq cf cl.cl_overrides in
  4238. let cf_type = if is_override && not (Meta.has Meta.Overload cf.cf_meta) then
  4239. match field_access gen (TInst(cl, List.map snd cl.cl_params)) cf.cf_name with
  4240. | FClassField(_,_,_,_,_,actual_t,_) -> actual_t
  4241. | _ -> assert false
  4242. else
  4243. cf.cf_type
  4244. in
  4245. not (has_type_params cf_type)) cl.cl_ordered_fields
  4246. in
  4247. 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
  4248. let fields = iface_cf :: fields in
  4249. iface.cl_ordered_fields <- fields;
  4250. List.iter (fun f -> iface.cl_fields <- PMap.add f.cf_name f iface.cl_fields) fields;
  4251. add_iface iface;
  4252. md
  4253. | TTypeDecl _ | TAbstractDecl _ -> md
  4254. | TEnumDecl _ ->
  4255. ignore (set_hxgeneric gen md);
  4256. md
  4257. | _ -> ignore (set_hxgeneric gen md); md
  4258. in
  4259. run
  4260. let configure gen mapping_func =
  4261. let map e = Some(mapping_func e) in
  4262. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
  4263. end;;
  4264. (* create a common interface without type parameters and only a __Cast<> function *)
  4265. let default_implementation gen (dyn_tparam_cast:texpr->t->texpr) ifaces =
  4266. let change_expr e cl iface params =
  4267. let field = mk_static_field_access_infer cl "__hx_cast" e.epos params in
  4268. let elist = [mk_cast (TInst(iface,[])) e] in
  4269. let call = { eexpr = TCall(field, elist); etype = t_dynamic; epos = e.epos } in
  4270. gen.gparam_func_call call field params elist
  4271. in
  4272. let rec run e =
  4273. match e.eexpr with
  4274. | TCast(cast_expr, _) ->
  4275. (* see if casting to a native generic class *)
  4276. let t = gen.greal_type e.etype in
  4277. let unifies =
  4278. let ctype = gen.greal_type cast_expr.etype in
  4279. match follow ctype with
  4280. | TInst(cl,_) -> (try
  4281. unify ctype t;
  4282. true
  4283. with | Unify_error el ->
  4284. false)
  4285. | _ -> false
  4286. in
  4287. let unifies = unifies && not (PMap.mem "cs_safe_casts" gen.gcon.defines) in
  4288. (match follow t with
  4289. | TInst(cl, p1 :: pl) when is_hxgeneric (TClassDecl cl) && not unifies && not (Meta.has Meta.Enum cl.cl_meta) ->
  4290. let iface = Hashtbl.find ifaces cl.cl_path in
  4291. mk_cast e.etype (change_expr (Type.map_expr run cast_expr) cl iface (p1 :: pl))
  4292. | _ -> Type.map_expr run e
  4293. )
  4294. | _ -> Type.map_expr run e
  4295. in
  4296. run
  4297. let configure gen traverse =
  4298. gen.ghas_tparam_cast_handler <- true;
  4299. let map e = Some(traverse e) in
  4300. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  4301. let default_config gen (dyn_tparam_cast:texpr->t->texpr) ifaces base_generic =
  4302. configure gen (default_implementation gen dyn_tparam_cast ifaces);
  4303. RealTypeParamsModf.configure gen (RealTypeParamsModf.default_implementation gen ifaces base_generic)
  4304. end;;
  4305. (* ******************************************* *)
  4306. (* Rename Type Parameters *)
  4307. (* ******************************************* *)
  4308. (*
  4309. This module should run after everything is already applied,
  4310. it will look for possible type parameter name clashing and change the classes names to a
  4311. dependencies:
  4312. should run after everything is already applied. There's no configure on this module, only 'run'.
  4313. *)
  4314. module RenameTypeParameters =
  4315. struct
  4316. let name = "rename_type_parameters"
  4317. let run gen =
  4318. let i = ref 0 in
  4319. let found_types = ref PMap.empty in
  4320. let check_type name on_changed =
  4321. let rec loop name =
  4322. incr i;
  4323. let changed_name = (name ^ (string_of_int !i)) in
  4324. if PMap.mem changed_name !found_types then loop name else changed_name
  4325. in
  4326. if PMap.mem name !found_types then begin
  4327. let new_name = loop name in
  4328. found_types := PMap.add new_name true !found_types;
  4329. on_changed new_name
  4330. end else found_types := PMap.add name true !found_types
  4331. in
  4332. let get_cls t =
  4333. match follow t with
  4334. | TInst(cl,_) -> cl
  4335. | _ -> assert false
  4336. in
  4337. let iter_types (_,t) =
  4338. let cls = get_cls t in
  4339. check_type (snd cls.cl_path) (fun name -> cls.cl_path <- (fst cls.cl_path, name))
  4340. in
  4341. List.iter (function
  4342. | TClassDecl cl ->
  4343. i := 0;
  4344. found_types := PMap.empty;
  4345. List.iter iter_types cl.cl_params;
  4346. let cur_found_types = !found_types in
  4347. List.iter (fun cf ->
  4348. found_types := cur_found_types;
  4349. List.iter iter_types cf.cf_params
  4350. ) (cl.cl_ordered_fields @ cl.cl_ordered_statics)
  4351. | TEnumDecl ( ({ e_params = hd :: tl }) ) ->
  4352. i := 0;
  4353. found_types := PMap.empty;
  4354. List.iter iter_types (hd :: tl)
  4355. | TAbstractDecl { a_params = hd :: tl } ->
  4356. i := 0;
  4357. found_types := PMap.empty;
  4358. List.iter iter_types (hd :: tl)
  4359. | _ -> ()
  4360. ) gen.gcon.types
  4361. end;;
  4362. let configure gen (param_func_call:texpr->texpr->tparams->texpr list->texpr) =
  4363. (*let map e = Some(mapping_func e) in
  4364. gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map*)
  4365. gen.gparam_func_call <- param_func_call
  4366. end;;
  4367. (**************************************************************************************************************************)
  4368. (* SYNTAX FILTERS *)
  4369. (**************************************************************************************************************************)
  4370. (* ******************************************* *)
  4371. (* Expression Unwrap *)
  4372. (* ******************************************* *)
  4373. (*
  4374. 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,
  4375. and will unwrap statements where expressions are expected, and vice-versa.
  4376. 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
  4377. 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
  4378. side-effects free, for example
  4379. 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,
  4380. 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)"
  4381. dependencies:
  4382. While it's best for Expression Unwrap to delay its execution as much as possible, since theoretically any
  4383. filter can return an expression that needs to be unwrapped, it is also desirable for ExpresionUnwrap to have
  4384. the AST as close as possible as Haxe's, so it can make some correct predictions (for example, so it can
  4385. more accurately know what can be side-effects-free and what can't).
  4386. This way, it will run slightly after the Normal priority, so if you don't say that a syntax filter must run
  4387. before Expression Unwrap, it will run after it.
  4388. TODO : While statement must become do / while, with the actual block inside an if for the condition, and else for 'break'
  4389. *)
  4390. module ExpressionUnwrap =
  4391. struct
  4392. let name = "expression_unwrap"
  4393. (* priority: first syntax filter *)
  4394. let priority = -10.0
  4395. (*
  4396. We always need to rely on Blocks to be able to unwrap expressions correctly.
  4397. So the the standard traverse will always be based on blocks.
  4398. Normal block statements, like for(), while(), if(), ... will be mk_block'ed so there is always a block inside of them.
  4399. At the block level, we'll define an "add_statement" function, which will allow the current expression to
  4400. 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.
  4401. - traverse will always evaluate TBlocks
  4402. - for each texpr in a TBlock list,
  4403. check shallow type
  4404. if type is Statement or Both when it has problematic expression (var problematic_expr = count_problematic_expressions),
  4405. if we can eagerly call unwrap_statement on the whole expression (try_call_unwrap_statement), use the return expression
  4406. else
  4407. check expr_type of each underlying type (with expr_stat_map)
  4408. if it has ExprWithStatement or Statement,
  4409. call problematic_expression_unwrap in it
  4410. problematic_expr--
  4411. else if problematic_expr == 0, just add the unchanged expression
  4412. else if NoSideEffects and doesn't have short-circuit, just add the unchanged expression
  4413. else call problematic_expression_unwrap in it
  4414. if type is Expression, check if there are statements or Both inside.
  4415. if there are, problematic_expression_unwrap in it
  4416. aftewards, use on_expr_as_statement to get it
  4417. helpers:
  4418. try_call_unwrap_statement: (returns texpr option)
  4419. 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.
  4420. apply_assign:
  4421. if is TVar, first declare the tvar with default expression = null;
  4422. will receive the left and right side of the assignment; right-side must be Statement
  4423. see if right side is a short-circuit operation, call short_circuit_op_unwrap
  4424. else see eexpr of the right side
  4425. if it's void, just add the statement with add_statement, and set the right side as null;
  4426. if not, it will have a block inside. set the left side = to the last expression on each block inside. add_statement for it.
  4427. short_circuit_op_unwrap: x() && (1 + {var x = 0; x + 1;} == 2) && z()
  4428. -> var x = x();
  4429. var y = false;
  4430. var z = false;
  4431. if (x) //for &&, neg for ||
  4432. {
  4433. var temp = null;
  4434. {
  4435. var x = 0;
  4436. temp = x + 1;
  4437. }
  4438. y = (1 + temp) == 2;
  4439. if (y)
  4440. {
  4441. z = z();
  4442. }
  4443. }
  4444. expects to receive a texpr with TBinop(OpBoolAnd/OpBoolOr)
  4445. 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.
  4446. will collect the return value, a mapped expr with all exprs as TLocal of the temp vars created
  4447. problematic_expression_unwrap:
  4448. check expr_kind:
  4449. if it is NoSideEffects and not short-circuit, leave it there
  4450. if it is ExprWithStatement and not short-circuit, call Type.map_expr problematic_expression_unwrap
  4451. if it is Statement or Expression or short-circuit expr, call add_assign for this expression
  4452. add_assign:
  4453. see if the type is void. If it is, just add_statement the expression argument, and return a null value
  4454. 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.
  4455. map_problematic_expr:
  4456. call expr_stat_map on statement with problematic_expression_unwrap
  4457. types:
  4458. type shallow_expr_type = | Statement | Expression | Both (* shallow expression classification. Both means that they can be either Statements as Expressions *)
  4459. type expr_kind = | NormalExpr | ExprNoSideEffects (* -> short-circuit is considered side-effects *) | ExprWithStatement | Statement
  4460. evaluates an expression (as in not a statement) type. If it is ExprWithStatement or Statement, it means it contains errors
  4461. functions:
  4462. shallow_expr_type (expr:texpr) : shallow_expr_type
  4463. expr_kind (expr:texpr) : expr_kind
  4464. deeply evaluates an expression type
  4465. expr_stat_map (fn:texpr->texpr) (expr:texpr) : texpr
  4466. it will traverse the AST looking for places where an expression is expected, and map the value according to fn
  4467. aggregate_expr_type (is_side_effects_free:bool) (children:expr_type list) : expr_type
  4468. helper function to deal with expr_type aggregation (e.g. an Expression + a Statement as a children, is a ExprWithStatement)
  4469. check_statement_in_expression (expr:texpr) : texpr option :
  4470. will check
  4471. *)
  4472. type shallow_expr_type = | Statement | Expression of texpr | Both of texpr (* shallow expression classification. Both means that they can be either Statements as Expressions *)
  4473. type expr_kind = | KNormalExpr | KNoSideEffects (* -> short-circuit is considered side-effects *) | KExprWithStatement | KStatement
  4474. let rec no_paren e =
  4475. match e.eexpr with
  4476. | TParenthesis e -> no_paren e
  4477. | _ -> e
  4478. (* must be called in a statement. Will execute fn whenever an expression (not statement) is expected *)
  4479. let rec expr_stat_map fn (expr:texpr) =
  4480. match (no_paren expr).eexpr with
  4481. | TBinop ( (Ast.OpAssign as op), left_e, right_e )
  4482. | TBinop ( (Ast.OpAssignOp _ as op), left_e, right_e ) ->
  4483. { expr with eexpr = TBinop(op, fn left_e, fn right_e) }
  4484. | TParenthesis _ -> assert false
  4485. | TCall(left_e, params) ->
  4486. { expr with eexpr = TCall(fn left_e, List.map fn params) }
  4487. | TNew(cl, tparams, params) ->
  4488. { expr with eexpr = TNew(cl, tparams, List.map fn params) }
  4489. | TVar(v,eopt) ->
  4490. { expr with eexpr = TVar(v, Option.map fn eopt) }
  4491. | TFor (v,cond,block) ->
  4492. { expr with eexpr = TFor(v, fn cond, block) }
  4493. | TIf(cond,eif,eelse) ->
  4494. { expr with eexpr = TIf(fn cond, eif, eelse) }
  4495. | TWhile(cond, block, flag) ->
  4496. { expr with eexpr = TWhile(fn cond, block, flag) }
  4497. | TSwitch(cond, el_block_l, default) ->
  4498. { expr with eexpr = TSwitch( fn cond, List.map (fun (el,block) -> (List.map fn el, block)) el_block_l, default ) }
  4499. (* | TMatch(cond, enum, cases, default) ->
  4500. { expr with eexpr = TMatch(fn cond, enum, cases, default) } *)
  4501. | TReturn(eopt) ->
  4502. { expr with eexpr = TReturn(Option.map fn eopt) }
  4503. | TThrow (texpr) ->
  4504. { expr with eexpr = TThrow(fn texpr) }
  4505. | TBreak
  4506. | TContinue
  4507. | TTry _
  4508. | TUnop (Ast.Increment, _, _)
  4509. | TUnop (Ast.Decrement, _, _) (* unop is a special case because the haxe compiler won't let us generate complex expressions with Increment/Decrement *)
  4510. | TBlock _ -> expr (* there is no expected expression here. Only statements *)
  4511. | TMeta(m,e) ->
  4512. { expr with eexpr = TMeta(m,expr_stat_map fn e) }
  4513. | _ -> assert false (* we only expect valid statements here. other expressions aren't valid statements *)
  4514. let is_expr = function | Expression _ -> true | _ -> false
  4515. let aggregate_expr_type map_fn side_effects_free children =
  4516. let rec loop acc children =
  4517. match children with
  4518. | [] -> acc
  4519. | hd :: children ->
  4520. match acc, map_fn hd with
  4521. | _, KExprWithStatement
  4522. | _, KStatement
  4523. | KExprWithStatement, _
  4524. | KStatement, _ -> KExprWithStatement
  4525. | KNormalExpr, KNoSideEffects
  4526. | KNoSideEffects, KNormalExpr
  4527. | KNormalExpr, KNormalExpr -> loop KNormalExpr children
  4528. | KNoSideEffects, KNoSideEffects -> loop KNoSideEffects children
  4529. in
  4530. loop (if side_effects_free then KNoSideEffects else KNormalExpr) children
  4531. (* statements: *)
  4532. (* Error CS0201: Only assignment, call, increment, *)
  4533. (* decrement, and new object expressions can be used as a *)
  4534. (* statement (CS0201). *)
  4535. let rec shallow_expr_type expr : shallow_expr_type =
  4536. match expr.eexpr with
  4537. | TCall _ when not (is_void expr.etype) -> Both expr
  4538. | TNew _
  4539. | TUnop (Ast.Increment, _, _)
  4540. | TUnop (Ast.Decrement, _, _)
  4541. | TBinop (Ast.OpAssign, _, _)
  4542. | TBinop (Ast.OpAssignOp _, _, _) -> Both expr
  4543. | TIf (cond, eif, Some(eelse)) -> (match aggregate_expr_type expr_kind true [cond;eif;eelse] with
  4544. | KExprWithStatement -> Statement
  4545. | _ -> Both expr)
  4546. | TConst _
  4547. | TLocal _
  4548. | TArray _
  4549. | TBinop _
  4550. | TField _
  4551. | TEnumParameter _
  4552. | TTypeExpr _
  4553. | TObjectDecl _
  4554. | TArrayDecl _
  4555. | TFunction _
  4556. | TCast _
  4557. | TUnop _ -> Expression (expr)
  4558. | TParenthesis p | TMeta(_,p) -> shallow_expr_type p
  4559. | TBlock ([e]) -> shallow_expr_type e
  4560. | TCall _
  4561. | TVar _
  4562. | TBlock _
  4563. | TFor _
  4564. | TWhile _
  4565. | TSwitch _
  4566. | TTry _
  4567. | TReturn _
  4568. | TBreak
  4569. | TContinue
  4570. | TIf _
  4571. | TThrow _ -> Statement
  4572. and expr_kind expr =
  4573. match shallow_expr_type expr with
  4574. | Statement -> KStatement
  4575. | Both expr | Expression expr ->
  4576. let aggregate = aggregate_expr_type expr_kind in
  4577. match expr.eexpr with
  4578. | TConst _
  4579. | TLocal _
  4580. | TFunction _
  4581. | TTypeExpr _ ->
  4582. KNoSideEffects
  4583. | TCall (ecall, params) ->
  4584. aggregate false (ecall :: params)
  4585. | TNew (_,_,params) ->
  4586. aggregate false params
  4587. | TUnop (Increment,_,e)
  4588. | TUnop (Decrement,_,e) ->
  4589. aggregate false [e]
  4590. | TUnop (_,_,e) ->
  4591. aggregate true [e]
  4592. | TBinop (Ast.OpBoolAnd, e1, e2)
  4593. | TBinop (Ast.OpBoolOr, e1, e2) -> (* TODO: should OpBool never be side-effects free? *)
  4594. aggregate true [e1;e2]
  4595. | TBinop (Ast.OpAssign, e1, e2)
  4596. | TBinop (Ast.OpAssignOp _, e1, e2) ->
  4597. aggregate false [e1;e2]
  4598. | TBinop (_, e1, e2) ->
  4599. aggregate true [e1;e2]
  4600. | TIf (cond, eif, Some(eelse)) -> (match aggregate true [cond;eif;eelse] with
  4601. | KExprWithStatement -> KStatement
  4602. | k -> k)
  4603. | TArray (e1,e2) ->
  4604. aggregate true [e1;e2]
  4605. | TParenthesis e
  4606. | TMeta(_,e)
  4607. | TField (e,_) ->
  4608. aggregate true [e]
  4609. | TArrayDecl (el) ->
  4610. aggregate true el
  4611. | TObjectDecl (sel) ->
  4612. aggregate true (List.map snd sel)
  4613. | TCast (e,_) ->
  4614. aggregate true [e]
  4615. | _ -> trace (debug_expr expr); assert false (* should have been read as Statement by shallow_expr_type *)
  4616. let is_side_effects_free e =
  4617. match expr_kind e with | KNoSideEffects -> true | _ -> false
  4618. let get_kinds (statement:texpr) =
  4619. let kinds = ref [] in
  4620. ignore (expr_stat_map (fun e ->
  4621. kinds := (expr_kind e) :: !kinds;
  4622. e
  4623. ) statement);
  4624. List.rev !kinds
  4625. let has_problematic_expressions (kinds:expr_kind list) =
  4626. let rec loop kinds =
  4627. match kinds with
  4628. | [] -> false
  4629. | KStatement :: _
  4630. | KExprWithStatement :: _ -> true
  4631. | _ :: tl -> loop tl
  4632. in
  4633. loop kinds
  4634. let count_problematic_expressions (statement:texpr) =
  4635. let count = ref 0 in
  4636. ignore (expr_stat_map (fun e ->
  4637. (match expr_kind e with
  4638. | KStatement | KExprWithStatement -> incr count
  4639. | _ -> ()
  4640. );
  4641. e
  4642. ) statement);
  4643. !count
  4644. let apply_assign_block assign_fun elist =
  4645. let rec assign acc elist =
  4646. match elist with
  4647. | [] -> acc
  4648. | last :: [] ->
  4649. (assign_fun last) :: acc
  4650. | hd :: tl ->
  4651. assign (hd :: acc) tl
  4652. in
  4653. List.rev (assign [] elist)
  4654. let mk_get_block assign_fun e =
  4655. match e.eexpr with
  4656. | TBlock [] -> e
  4657. | TBlock (el) ->
  4658. { e with eexpr = TBlock(apply_assign_block assign_fun el) }
  4659. | _ ->
  4660. { e with eexpr = TBlock([ assign_fun e ]) }
  4661. let add_assign gen add_statement expr =
  4662. match expr.eexpr, follow expr.etype with
  4663. | _, TAbstract ({ a_path = ([],"Void") },[])
  4664. | TThrow _, _ ->
  4665. add_statement expr;
  4666. null expr.etype expr.epos
  4667. | _ ->
  4668. let var = mk_temp gen "stmt" expr.etype in
  4669. let tvars = { expr with eexpr = TVar(var,Some(expr)) } in
  4670. let local = { expr with eexpr = TLocal(var) } in
  4671. add_statement tvars;
  4672. local
  4673. (* requirement: right must be a statement *)
  4674. let rec apply_assign assign_fun right =
  4675. match right.eexpr with
  4676. | TBlock el ->
  4677. { right with eexpr = TBlock(apply_assign_block assign_fun el) }
  4678. | TSwitch (cond, elblock_l, default) ->
  4679. { 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) }
  4680. (* | TMatch (cond, ep, il_vlo_e_l, default) ->
  4681. { 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) } *)
  4682. | TTry (block, catches) ->
  4683. { right with eexpr = TTry(mk_get_block assign_fun block, List.map (fun (v,block) -> (v,mk_get_block assign_fun block) ) catches) }
  4684. | TIf (cond,eif,eelse) ->
  4685. { right with eexpr = TIf(cond, mk_get_block assign_fun eif, Option.map (mk_get_block assign_fun) eelse) }
  4686. | TThrow _
  4687. | TWhile _
  4688. | TFor _
  4689. | TReturn _
  4690. | TBreak
  4691. | TContinue -> right
  4692. | TParenthesis p | TMeta(_,p) ->
  4693. apply_assign assign_fun p
  4694. | TVar _ ->
  4695. right
  4696. | _ ->
  4697. match follow right.etype with
  4698. | TAbstract ({ a_path = ([], "Void") },[]) ->
  4699. right
  4700. | _ -> trace (debug_expr right); assert false (* a statement is required *)
  4701. let short_circuit_op_unwrap gen add_statement expr :texpr =
  4702. let do_not expr =
  4703. { expr with eexpr = TUnop(Ast.Not, Ast.Prefix, expr) }
  4704. in
  4705. (* loop will always return its own TBlock, and the mapped expression *)
  4706. let rec loop acc expr =
  4707. match expr.eexpr with
  4708. | TBinop ( (Ast.OpBoolAnd as op), left, right) ->
  4709. let var = mk_temp gen "boolv" right.etype in
  4710. 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
  4711. let local = { right with eexpr = TLocal(var) } in
  4712. let mapped_left, ret_acc = loop ( (local, { right with eexpr = TBinop(Ast.OpAssign, local, right) } ) :: acc) left in
  4713. add_statement tvars;
  4714. ({ expr with eexpr = TBinop(op, mapped_left, local) }, ret_acc)
  4715. (* we only accept OpBoolOr when it's the first to be evaluated *)
  4716. | TBinop ( (Ast.OpBoolOr as op), left, right) when acc = [] ->
  4717. let left = match left.eexpr with
  4718. | TLocal _ | TConst _ -> left
  4719. | _ -> add_assign gen add_statement left
  4720. in
  4721. let var = mk_temp gen "boolv" right.etype in
  4722. 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
  4723. let local = { right with eexpr = TLocal(var) } in
  4724. add_statement tvars;
  4725. ({ expr with eexpr = TBinop(op, left, local) }, [ do_not left, { right with eexpr = TBinop(Ast.OpAssign, local, right) } ])
  4726. | _ when acc = [] -> assert false
  4727. | _ ->
  4728. let var = mk_temp gen "boolv" expr.etype in
  4729. let tvars = { expr with eexpr = TVar(var, Some( { expr with etype = gen.gcon.basic.tbool } )); etype = gen.gcon.basic.tvoid } in
  4730. let local = { expr with eexpr = TLocal(var) } in
  4731. let last_local = ref local in
  4732. let acc = List.map (fun (local, assign) ->
  4733. let l = !last_local in
  4734. last_local := local;
  4735. (l, assign)
  4736. ) acc in
  4737. add_statement tvars;
  4738. (local, acc)
  4739. in
  4740. let mapped_expr, local_assign_list = loop [] expr in
  4741. let rec loop local_assign_list : texpr =
  4742. match local_assign_list with
  4743. | [local, assign] ->
  4744. { eexpr = TIf(local, assign, None); etype = gen.gcon.basic.tvoid; epos = assign.epos }
  4745. | (local, assign) :: tl ->
  4746. { eexpr = TIf(local,
  4747. {
  4748. eexpr = TBlock ( assign :: [loop tl] );
  4749. etype = gen.gcon.basic.tvoid;
  4750. epos = assign.epos;
  4751. },
  4752. None); etype = gen.gcon.basic.tvoid; epos = assign.epos }
  4753. | [] -> assert false
  4754. in
  4755. add_statement (loop local_assign_list);
  4756. mapped_expr
  4757. (* there are two short_circuit fuctions as I'm still testing the best way to do it *)
  4758. (*let short_circuit_op_unwrap gen add_statement expr :texpr =
  4759. let block = ref [] in
  4760. let rec short_circuit_op_unwrap is_first last_block expr =
  4761. match expr.eexpr with
  4762. | TBinop ( (Ast.OpBoolAnd as op), left, right)
  4763. | TBinop ( (Ast.OpBoolOr as op), left, right) ->
  4764. let var = mk_temp gen "boolv" left.etype in
  4765. 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
  4766. let local = { left with eexpr = TLocal(var) } in
  4767. if not is_first then begin
  4768. last_block := !last_block @ [ { left with eexpr = TBinop(Ast.OpAssign, local, left) } ]
  4769. end;
  4770. add_statement tvars;
  4771. let local_op = match op with | Ast.OpBoolAnd -> local | Ast.OpBoolOr -> { local with eexpr = TUnop(Ast.Not, Ast.Prefix, local) } | _ -> assert false in
  4772. let new_block = ref [] in
  4773. let new_right = short_circuit_op_unwrap false new_block right in
  4774. last_block := !last_block @ [ { expr with eexpr = TIf(local_op, { right with eexpr = TBlock(!new_block) }, None) } ];
  4775. { expr with eexpr = TBinop(op, local, new_right) }
  4776. | _ when is_first -> assert false
  4777. | _ ->
  4778. let var = mk_temp gen "boolv" expr.etype in
  4779. let tvars = { expr with eexpr = TVar([var, Some ( { expr with eexpr = TConst(TBool false) } ) ]); etype = gen.gcon.basic.tvoid } in
  4780. let local = { expr with eexpr = TLocal(var) } in
  4781. last_block := !last_block @ [ { expr with eexpr = TBinop(Ast.OpAssign, local, expr) } ];
  4782. add_statement tvars;
  4783. local
  4784. in
  4785. let mapped_expr = short_circuit_op_unwrap true block expr in
  4786. add_statement { eexpr = TBlock(!block); etype = gen.gcon.basic.tvoid; epos = expr.epos };
  4787. mapped_expr*)
  4788. let twhile_with_condition_statement gen add_statement twhile cond e1 flag =
  4789. (* when a TWhile is found with a problematic condition *)
  4790. let basic = gen.gcon.basic in
  4791. let block = if flag = Ast.NormalWhile then
  4792. { e1 with eexpr = TIf(cond, e1, Some({ e1 with eexpr = TBreak; etype = basic.tvoid })) }
  4793. else
  4794. Type.concat e1 { e1 with
  4795. eexpr = TIf({
  4796. eexpr = TUnop(Ast.Not, Ast.Prefix, mk_paren cond);
  4797. etype = basic.tbool;
  4798. epos = cond.epos
  4799. }, { e1 with eexpr = TBreak; etype = basic.tvoid }, None);
  4800. etype = basic.tvoid
  4801. }
  4802. in
  4803. add_statement { twhile with
  4804. eexpr = TWhile(
  4805. { eexpr = TConst(TBool true); etype = basic.tbool; epos = cond.epos },
  4806. block,
  4807. Ast.DoWhile
  4808. );
  4809. }
  4810. let try_call_unwrap_statement gen problematic_expression_unwrap (add_statement:texpr->unit) (expr:texpr) : texpr option =
  4811. let check_left left =
  4812. match expr_kind left with
  4813. | KExprWithStatement ->
  4814. problematic_expression_unwrap add_statement left KExprWithStatement
  4815. | KStatement -> assert false (* doesn't make sense a KStatement as a left side expression *)
  4816. | _ -> left
  4817. in
  4818. let handle_assign op left right =
  4819. let left = check_left left in
  4820. 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 )
  4821. in
  4822. let handle_return e =
  4823. Some( apply_assign (fun e ->
  4824. match e.eexpr with
  4825. | TThrow _ -> e
  4826. | _ when is_void e.etype ->
  4827. { e with eexpr = TBlock([e; { e with eexpr = TReturn None }]) }
  4828. | _ ->
  4829. { e with eexpr = TReturn( Some e ) }
  4830. ) e )
  4831. in
  4832. let is_problematic_if right =
  4833. match expr_kind right with
  4834. | KStatement | KExprWithStatement -> true
  4835. | _ -> false
  4836. in
  4837. match expr.eexpr with
  4838. | TBinop((Ast.OpAssign as op),left,right)
  4839. | TBinop((Ast.OpAssignOp _ as op),left,right) when shallow_expr_type right = Statement ->
  4840. handle_assign op left right
  4841. | TReturn( Some right ) when shallow_expr_type right = Statement ->
  4842. handle_return right
  4843. | TBinop((Ast.OpAssign as op),left, ({ eexpr = TBinop(Ast.OpBoolAnd,_,_) } as right) )
  4844. | TBinop((Ast.OpAssign as op),left,({ eexpr = TBinop(Ast.OpBoolOr,_,_) } as right))
  4845. | TBinop((Ast.OpAssignOp _ as op),left,({ eexpr = TBinop(Ast.OpBoolAnd,_,_) } as right) )
  4846. | TBinop((Ast.OpAssignOp _ as op),left,({ eexpr = TBinop(Ast.OpBoolOr,_,_) } as right) ) ->
  4847. let right = short_circuit_op_unwrap gen add_statement right in
  4848. Some { expr with eexpr = TBinop(op, check_left left, right) }
  4849. | TVar(v,Some({ eexpr = TBinop(Ast.OpBoolAnd,_,_) } as right))
  4850. | TVar(v,Some({ eexpr = TBinop(Ast.OpBoolOr,_,_) } as right)) ->
  4851. let right = short_circuit_op_unwrap gen add_statement right in
  4852. Some { expr with eexpr = TVar(v, Some(right)) }
  4853. | TVar(v,Some(right)) when shallow_expr_type right = Statement ->
  4854. add_statement ({ expr with eexpr = TVar(v, Some(null right.etype right.epos)) });
  4855. handle_assign Ast.OpAssign { expr with eexpr = TLocal(v); etype = v.v_type } right
  4856. (* TIf handling *)
  4857. | TBinop((Ast.OpAssign as op),left, ({ eexpr = TIf _ } as right))
  4858. | TBinop((Ast.OpAssignOp _ as op),left,({ eexpr = TIf _ } as right)) when is_problematic_if right ->
  4859. handle_assign op left right
  4860. | TVar(v,Some({ eexpr = TIf _ } as right)) when is_problematic_if right ->
  4861. add_statement ({ expr with eexpr = TVar(v, Some(null right.etype right.epos)) });
  4862. handle_assign Ast.OpAssign { expr with eexpr = TLocal(v); etype = v.v_type } right
  4863. | TWhile(cond, e1, flag) when is_problematic_if cond ->
  4864. twhile_with_condition_statement gen add_statement expr cond e1 flag;
  4865. Some (null expr.etype expr.epos)
  4866. | _ -> None
  4867. let traverse gen (on_expr_as_statement:texpr->texpr option) =
  4868. let add_assign = add_assign gen in
  4869. let problematic_expression_unwrap add_statement expr e_type =
  4870. let rec problematic_expression_unwrap is_first expr e_type =
  4871. match e_type, expr.eexpr with
  4872. | _, TBinop(Ast.OpBoolAnd, _, _)
  4873. | _, TBinop(Ast.OpBoolOr, _, _) -> add_assign add_statement expr (* add_assign so try_call_unwrap_expr *)
  4874. | KNoSideEffects, _ -> expr
  4875. | KStatement, _
  4876. | KNormalExpr, _ -> add_assign add_statement expr
  4877. | KExprWithStatement, TCall _
  4878. | KExprWithStatement, TNew _
  4879. | KExprWithStatement, TBinop (Ast.OpAssign,_,_)
  4880. | KExprWithStatement, TBinop (Ast.OpAssignOp _,_,_)
  4881. | KExprWithStatement, TUnop (Ast.Increment,_,_) (* all of these may have side-effects, so they must also be add_assign'ed . is_first avoids infinite loop *)
  4882. | KExprWithStatement, TUnop (Ast.Decrement,_,_) when not is_first -> add_assign add_statement expr
  4883. (* bugfix: Type.map_expr doesn't guarantee the correct order of execution *)
  4884. | KExprWithStatement, TBinop(op,e1,e2) ->
  4885. let e1 = problematic_expression_unwrap false e1 (expr_kind e1) in
  4886. let e2 = problematic_expression_unwrap false e2 (expr_kind e2) in
  4887. { expr with eexpr = TBinop(op, e1, e2) }
  4888. | KExprWithStatement, TArray(e1,e2) ->
  4889. let e1 = problematic_expression_unwrap false e1 (expr_kind e1) in
  4890. let e2 = problematic_expression_unwrap false e2 (expr_kind e2) in
  4891. { expr with eexpr = TArray(e1, e2) }
  4892. (* bugfix: calls should not be transformed into closure calls *)
  4893. | KExprWithStatement, TCall(( { eexpr = TField (ef_left, f) } as ef ), eargs) ->
  4894. { expr with eexpr = TCall(
  4895. { ef with eexpr = TField(problematic_expression_unwrap false ef_left (expr_kind ef_left), f) },
  4896. List.map (fun e -> problematic_expression_unwrap false e (expr_kind e)) eargs)
  4897. }
  4898. | KExprWithStatement, _ -> Type.map_expr (fun e -> problematic_expression_unwrap false e (expr_kind e)) expr
  4899. in
  4900. problematic_expression_unwrap true expr e_type
  4901. in
  4902. let rec traverse e =
  4903. match e.eexpr with
  4904. | TBlock el ->
  4905. let new_block = ref [] in
  4906. let rec process_statement e =
  4907. let e = no_paren e in
  4908. match e.eexpr, shallow_expr_type e with
  4909. | TCall( { eexpr = TLocal v } as elocal, elist ), _ when String.get v.v_name 0 = '_' && Hashtbl.mem gen.gspecial_vars v.v_name ->
  4910. new_block := { e with eexpr = TCall( elocal, List.map (fun e ->
  4911. match e.eexpr with
  4912. | TBlock _ -> traverse e
  4913. | _ -> e
  4914. ) elist ) } :: !new_block
  4915. | _, Statement | _, Both _ ->
  4916. let e = match e.eexpr with | TReturn (Some ({ eexpr = TThrow _ } as ethrow)) -> ethrow | _ -> e in
  4917. let kinds = get_kinds e in
  4918. if has_problematic_expressions kinds then begin
  4919. match try_call_unwrap_statement gen problematic_expression_unwrap add_statement e with
  4920. | Some { eexpr = TConst(TNull) } (* no op *)
  4921. | Some { eexpr = TBlock [] } -> ()
  4922. | Some e ->
  4923. if has_problematic_expressions (get_kinds e) then begin
  4924. process_statement e
  4925. end else
  4926. new_block := (traverse e) :: !new_block
  4927. | None ->
  4928. (
  4929. let acc = ref kinds in
  4930. let new_e = expr_stat_map (fun e ->
  4931. match !acc with
  4932. | hd :: tl ->
  4933. acc := tl;
  4934. if has_problematic_expressions (hd :: tl) then begin
  4935. problematic_expression_unwrap add_statement e hd
  4936. end else
  4937. e
  4938. | [] -> assert false
  4939. ) e in
  4940. new_block := (traverse new_e) :: !new_block
  4941. )
  4942. end else begin new_block := (traverse e) :: !new_block end
  4943. | _, Expression e ->
  4944. match on_expr_as_statement e with
  4945. | None -> ()
  4946. | Some e -> process_statement e
  4947. and add_statement expr =
  4948. process_statement expr
  4949. in
  4950. List.iter (process_statement) el;
  4951. let block = List.rev !new_block in
  4952. { e with eexpr = TBlock(block) }
  4953. | TTry (block, catches) ->
  4954. { e with eexpr = TTry(traverse (mk_block block), List.map (fun (v,block) -> (v, traverse (mk_block block))) catches) }
  4955. (* | TMatch (cond,ep,il_vol_e_l,default) ->
  4956. { 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) } *)
  4957. | TSwitch (cond,el_e_l, default) ->
  4958. { 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) }
  4959. | TWhile (cond,block,flag) ->
  4960. {e with eexpr = TWhile(cond,traverse (mk_block block), flag) }
  4961. | TIf (cond, eif, eelse) ->
  4962. { e with eexpr = TIf(cond, traverse (mk_block eif), Option.map (fun e -> traverse (mk_block e)) eelse) }
  4963. | TFor (v,it,block) ->
  4964. { e with eexpr = TFor(v,it, traverse (mk_block block)) }
  4965. | TFunction (tfunc) ->
  4966. { e with eexpr = TFunction({ tfunc with tf_expr = traverse (mk_block tfunc.tf_expr) }) }
  4967. | _ -> e (* if expression doesn't have a block, we will exit *)
  4968. in
  4969. traverse
  4970. let configure gen (mapping_func:texpr->texpr) =
  4971. let map e = Some(mapping_func e) in
  4972. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  4973. end;;
  4974. (* ******************************************* *)
  4975. (* Casts detection v2 *)
  4976. (* ******************************************* *)
  4977. (*
  4978. Will detect implicit casts and add TCast for them. Since everything is already followed by follow_all, typedefs are considered a new type altogether
  4979. Types shouldn't be cast if:
  4980. * When an instance is being coerced to a superclass or to an implemented interface
  4981. * When anything is being coerced to Dynamic
  4982. edit:
  4983. 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,
  4984. with maybe even some loss of information
  4985. * TAnon / TDynamic will call
  4986. * Type parameter handling will be abstracted
  4987. dependencies:
  4988. Must run before ExpressionUnwrap
  4989. *)
  4990. module CastDetect =
  4991. struct
  4992. let name = "cast_detect_2"
  4993. let priority = solve_deps name [DBefore TypeParams.priority; DBefore ExpressionUnwrap.priority]
  4994. (* ******************************************* *)
  4995. (* ReturnCast *)
  4996. (* ******************************************* *)
  4997. (*
  4998. Cast detection for return types can't be done at CastDetect time, since we need an
  4999. unwrapped expression to make sure we catch all return cast detections. So this module
  5000. is specifically to deal with that, and is configured automatically by CastDetect
  5001. dependencies:
  5002. *)
  5003. module ReturnCast =
  5004. struct
  5005. let name = "return_cast"
  5006. let priority = solve_deps name [DAfter priority; DAfter ExpressionUnwrap.priority]
  5007. let default_implementation gen =
  5008. let rec extract_expr e = match e.eexpr with
  5009. | TParenthesis e
  5010. | TMeta (_,e)
  5011. | TCast(e,_) -> extract_expr e
  5012. | _ -> e
  5013. in
  5014. let current_ret_type = ref None in
  5015. let handle e tto tfrom = gen.ghandle_cast (gen.greal_type tto) (gen.greal_type tfrom) e in
  5016. let in_value = ref false in
  5017. let rec run e =
  5018. let was_in_value = !in_value in
  5019. in_value := true;
  5020. match e.eexpr with
  5021. | TReturn (eopt) ->
  5022. (* a return must be inside a function *)
  5023. 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
  5024. (match eopt with
  5025. | None when not (is_void ret_type) ->
  5026. { e with eexpr = TReturn( Some(null ret_type e.epos)) }
  5027. | None -> e
  5028. | Some eret ->
  5029. { e with eexpr = TReturn( Some(handle (run eret) ret_type eret.etype ) ) })
  5030. | TFunction(tfunc) ->
  5031. let last_ret = !current_ret_type in
  5032. current_ret_type := Some(tfunc.tf_type);
  5033. let ret = Type.map_expr run e in
  5034. current_ret_type := last_ret;
  5035. ret
  5036. | TBlock el ->
  5037. { e with eexpr = TBlock ( List.map (fun e -> in_value := false; run e) el ) }
  5038. | TBinop ( (Ast.OpAssign as op),e1,e2)
  5039. | TBinop ( (Ast.OpAssignOp _ as op),e1,e2) when was_in_value ->
  5040. let e1 = extract_expr (run e1) in
  5041. let r = { e with eexpr = TBinop(op, e1, handle (run e2) e1.etype e2.etype); etype = e1.etype } in
  5042. handle r e.etype e1.etype
  5043. | TBinop ( (Ast.OpAssign as op),({ eexpr = TField(tf, f) } as e1), e2 )
  5044. | TBinop ( (Ast.OpAssignOp _ as op),({ eexpr = TField(tf, f) } as e1), e2 ) ->
  5045. (match field_access_esp gen (gen.greal_type tf.etype) (f) with
  5046. | FClassField(cl,params,_,_,is_static,actual_t,_) ->
  5047. let actual_t = if is_static then actual_t else apply_params cl.cl_params params actual_t in
  5048. let e1 = extract_expr (run e1) in
  5049. { e with eexpr = TBinop(op, e1, handle (run e2) actual_t e2.etype); etype = e1.etype }
  5050. | _ ->
  5051. let e1 = extract_expr (run e1) in
  5052. { e with eexpr = TBinop(op, e1, handle (run e2) e1.etype e2.etype); etype = e1.etype }
  5053. )
  5054. | TBinop ( (Ast.OpAssign as op),e1,e2)
  5055. | TBinop ( (Ast.OpAssignOp _ as op),e1,e2) ->
  5056. let e1 = extract_expr (run e1) in
  5057. { e with eexpr = TBinop(op, e1, handle (run e2) e1.etype e2.etype); etype = e1.etype }
  5058. | _ -> Type.map_expr run e
  5059. in
  5060. run
  5061. let configure gen =
  5062. let map e = Some(default_implementation gen e) in
  5063. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  5064. end;;
  5065. let get_args t = match follow t with
  5066. | TFun(args,ret) -> args,ret
  5067. | _ -> trace (debug_type t); assert false
  5068. let s_path (pack,n) = (String.concat "." (pack @ [n]))
  5069. (*
  5070. Since this function is applied under native-context only, the type paraters will already be changed
  5071. *)
  5072. let map_cls gen also_implements fn super =
  5073. let rec loop c tl =
  5074. if c == super then
  5075. fn c tl
  5076. else (match c.cl_super with
  5077. | None -> false
  5078. | Some (cs,tls) ->
  5079. let tls = gen.greal_type_param (TClassDecl cs) tls in
  5080. loop cs (List.map (apply_params c.cl_params tl) tls)
  5081. ) || (if also_implements then List.exists (fun (cs,tls) ->
  5082. loop cs (List.map (apply_params c.cl_params tl) tls)
  5083. ) c.cl_implements else false)
  5084. in
  5085. loop
  5086. let follow_dyn t = match follow t with
  5087. | TMono _ | TLazy _ -> t_dynamic
  5088. | t -> t
  5089. (*
  5090. this has a slight change from the type.ml version, in which it doesn't
  5091. change a TMono into the other parameter
  5092. *)
  5093. let rec type_eq gen param a b =
  5094. if a == b then
  5095. ()
  5096. else match follow_dyn (gen.greal_type a) , follow_dyn (gen.greal_type b) with
  5097. | TEnum (e1,tl1) , TEnum (e2,tl2) ->
  5098. if e1 != e2 && not (param = EqCoreType && e1.e_path = e2.e_path) then Type.error [cannot_unify a b];
  5099. List.iter2 (type_eq gen param) tl1 tl2
  5100. | TAbstract (a1,tl1) , TAbstract (a2,tl2) ->
  5101. if a1 != a2 && not (param = EqCoreType && a1.a_path = a2.a_path) then Type.error [cannot_unify a b];
  5102. List.iter2 (type_eq gen param) tl1 tl2
  5103. | TInst (c1,tl1) , TInst (c2,tl2) ->
  5104. 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];
  5105. List.iter2 (type_eq gen param) tl1 tl2
  5106. | TFun (l1,r1) , TFun (l2,r2) when List.length l1 = List.length l2 ->
  5107. (try
  5108. type_eq gen param r1 r2;
  5109. List.iter2 (fun (n,o1,t1) (_,o2,t2) ->
  5110. if o1 <> o2 then Type.error [Not_matching_optional n];
  5111. type_eq gen param t1 t2
  5112. ) l1 l2
  5113. with
  5114. Unify_error l -> Type.error (cannot_unify a b :: l))
  5115. | TDynamic a , TDynamic b ->
  5116. type_eq gen param a b
  5117. | TAnon a1, TAnon a2 ->
  5118. (try
  5119. PMap.iter (fun n f1 ->
  5120. try
  5121. let f2 = PMap.find n a2.a_fields in
  5122. 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];
  5123. try
  5124. type_eq gen param f1.cf_type f2.cf_type
  5125. with
  5126. Unify_error l -> Type.error (invalid_field n :: l)
  5127. with
  5128. Not_found ->
  5129. if is_closed a2 then Type.error [has_no_field b n];
  5130. if not (link (ref None) b f1.cf_type) then Type.error [cannot_unify a b];
  5131. a2.a_fields <- PMap.add n f1 a2.a_fields
  5132. ) a1.a_fields;
  5133. PMap.iter (fun n f2 ->
  5134. if not (PMap.mem n a1.a_fields) then begin
  5135. if is_closed a1 then Type.error [has_no_field a n];
  5136. if not (link (ref None) a f2.cf_type) then Type.error [cannot_unify a b];
  5137. a1.a_fields <- PMap.add n f2 a1.a_fields
  5138. end;
  5139. ) a2.a_fields;
  5140. with
  5141. Unify_error l -> Type.error (cannot_unify a b :: l))
  5142. | _ , _ ->
  5143. if b == t_dynamic && (param = EqRightDynamic || param = EqBothDynamic) then
  5144. ()
  5145. else if a == t_dynamic && param = EqBothDynamic then
  5146. ()
  5147. else
  5148. Type.error [cannot_unify a b]
  5149. let type_iseq gen a b =
  5150. try
  5151. type_eq gen EqStrict a b;
  5152. true
  5153. with
  5154. Unify_error _ -> false
  5155. (* will return true if both arguments are compatible. If it's not the case, a runtime error is very likely *)
  5156. let is_cl_related gen cl tl super superl =
  5157. 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
  5158. is_cl_related cl tl super superl || is_cl_related super superl cl tl
  5159. let rec is_unsafe_cast gen to_t from_t =
  5160. match (follow to_t, follow from_t) with
  5161. | TInst(cl_to, to_params), TInst(cl_from, from_params) ->
  5162. not (is_cl_related gen cl_from from_params cl_to to_params)
  5163. | TEnum(e_to, _), TEnum(e_from, _) ->
  5164. e_to.e_path <> e_from.e_path
  5165. | TFun _, TFun _ ->
  5166. (* functions are never unsafe cast by default. This behavior might be changed *)
  5167. (* with a later AST pass which will run through TFun to TFun casts *)
  5168. false
  5169. | TMono _, _
  5170. | _, TMono _
  5171. | TDynamic _, _
  5172. | _, TDynamic _ ->
  5173. false
  5174. | TAnon _, _
  5175. | _, TAnon _ ->
  5176. (* anonymous are never unsafe also. *)
  5177. (* Though they will generate a cast, so if this cast is unneeded it's better to avoid them by tweaking gen.greal_type *)
  5178. false
  5179. | TAbstract _, _
  5180. | _, TAbstract _ ->
  5181. (try
  5182. unify from_t to_t;
  5183. false
  5184. with | Unify_error _ ->
  5185. try
  5186. unify to_t from_t; (* still not unsafe *)
  5187. false
  5188. with | Unify_error _ ->
  5189. true)
  5190. | _ -> true
  5191. let unifies tfrom tto = try
  5192. unify tfrom tto;
  5193. true
  5194. with | _ ->
  5195. false
  5196. let do_unsafe_cast gen from_t to_t e =
  5197. let t_path t =
  5198. match t with
  5199. | TInst(cl, _) -> cl.cl_path
  5200. | TEnum(e, _) -> e.e_path
  5201. | TType(t, _) -> t.t_path
  5202. | TAbstract(a, _) -> a.a_path
  5203. | TDynamic _ -> ([], "Dynamic")
  5204. | _ -> raise Not_found
  5205. in
  5206. match gen.gfollow#run_f from_t, gen.gfollow#run_f to_t with
  5207. | TInst({ cl_kind = KTypeParameter tl },_), t2 when List.exists (fun t -> unifies t t2) tl ->
  5208. mk_cast to_t (mk_cast t_dynamic e)
  5209. | _ ->
  5210. let do_default () =
  5211. gen.gon_unsafe_cast to_t e.etype e.epos;
  5212. mk_cast to_t (mk_cast t_dynamic e)
  5213. in
  5214. (* TODO: there really should be a better way to write that *)
  5215. try
  5216. if (Hashtbl.find gen.gsupported_conversions (t_path from_t)) from_t to_t then
  5217. mk_cast to_t e
  5218. else
  5219. do_default()
  5220. with
  5221. | Not_found ->
  5222. try
  5223. if (Hashtbl.find gen.gsupported_conversions (t_path to_t)) from_t to_t then
  5224. mk_cast to_t e
  5225. else
  5226. do_default()
  5227. with
  5228. | Not_found -> do_default()
  5229. (* ****************************** *)
  5230. (* cast handler *)
  5231. (* decides if a cast should be emitted, given a from and a to type *)
  5232. (*
  5233. this function is like a mini unify, without e.g. subtyping, which makes sense
  5234. at the backend level, since most probably Anons and TInst will have a different representation there
  5235. *)
  5236. let rec handle_cast gen e real_to_t real_from_t =
  5237. let do_unsafe_cast () = do_unsafe_cast gen real_from_t real_to_t { e with etype = real_from_t } in
  5238. let to_t, from_t = real_to_t, real_from_t in
  5239. let mk_cast t e =
  5240. match e.eexpr with
  5241. (* TThrow is always typed as Dynamic, we just need to type it accordingly *)
  5242. | TThrow _ -> { e with etype = t }
  5243. | _ -> mk_cast t e
  5244. in
  5245. let e = { e with etype = real_from_t } in
  5246. if try fast_eq real_to_t real_from_t with Invalid_argument("List.for_all2") -> false then e else
  5247. match real_to_t, real_from_t with
  5248. (* string is the only type that can be implicitly converted from any other *)
  5249. | TInst( { cl_path = ([], "String") }, []), _ ->
  5250. mk_cast to_t e
  5251. | TInst(cl_to, params_to), TInst(cl_from, params_from) ->
  5252. let ret = ref None in
  5253. (*
  5254. this is a little confusing:
  5255. we are here mapping classes until we have the same to and from classes, applying the type parameters in each step, so we can
  5256. compare the type parameters;
  5257. If a class is found - meaning that the cl_from can be converted without a cast into cl_to,
  5258. we still need to check their type parameters.
  5259. *)
  5260. ignore (map_cls gen (gen.guse_tp_constraints || (match cl_from.cl_kind,cl_to.cl_kind with KTypeParameter _, _ | _,KTypeParameter _ -> false | _ -> true)) (fun _ tl ->
  5261. try
  5262. (* type found, checking type parameters *)
  5263. List.iter2 (type_eq gen EqStrict) tl params_to;
  5264. ret := Some e;
  5265. true
  5266. with | Unify_error _ ->
  5267. (* type parameters need casting *)
  5268. if gen.ghas_tparam_cast_handler then begin
  5269. (*
  5270. if we are already handling type parameter casts on other part of code (e.g. RealTypeParameters),
  5271. we'll just make a cast to indicate that this place needs type parameter-involved casting
  5272. *)
  5273. ret := Some (mk_cast to_t e);
  5274. true
  5275. end else
  5276. (*
  5277. if not, we're going to check if we only need a simple cast,
  5278. or if we need to first cast into the dynamic version of it
  5279. *)
  5280. try
  5281. List.iter2 (type_eq gen EqRightDynamic) tl params_to;
  5282. ret := Some (mk_cast to_t e);
  5283. true
  5284. with | Unify_error _ ->
  5285. ret := Some (mk_cast to_t (mk_cast (TInst(cl_to, List.map (fun _ -> t_dynamic) params_to)) e));
  5286. true
  5287. ) cl_to cl_from params_from);
  5288. if is_some !ret then
  5289. get !ret
  5290. else if is_cl_related gen cl_from params_from cl_to params_to then
  5291. mk_cast to_t e
  5292. else
  5293. (* potential unsafe cast *)
  5294. (do_unsafe_cast ())
  5295. | TMono _, TMono _
  5296. | TMono _, TDynamic _
  5297. | TDynamic _, TDynamic _
  5298. | TDynamic _, TMono _ ->
  5299. e
  5300. | TMono _, _
  5301. | TDynamic _, _
  5302. | TAnon _, _ when gen.gneeds_box real_from_t ->
  5303. mk_cast to_t e
  5304. | TMono _, _
  5305. | TDynamic _, _ -> e
  5306. | _, TMono _
  5307. | _, TDynamic _ -> mk_cast to_t e
  5308. | TAnon (a_to), TAnon (a_from) ->
  5309. if a_to == a_from then
  5310. e
  5311. else if type_iseq gen to_t from_t then (* FIXME apply unify correctly *)
  5312. e
  5313. else
  5314. mk_cast to_t e
  5315. | _, TAnon(anon) -> (try
  5316. let p2 = match !(anon.a_status) with
  5317. | Statics c -> TInst(c,List.map (fun _ -> t_dynamic) c.cl_params)
  5318. | EnumStatics e -> TEnum(e, List.map (fun _ -> t_dynamic) e.e_params)
  5319. | AbstractStatics a -> TAbstract(a, List.map (fun _ -> t_dynamic) a.a_params)
  5320. | _ -> raise Not_found
  5321. in
  5322. let tclass = match get_type gen ([],"Class") with
  5323. | TAbstractDecl(a) -> a
  5324. | _ -> assert false in
  5325. handle_cast gen e real_to_t (gen.greal_type (TAbstract(tclass, [p2])))
  5326. with | Not_found ->
  5327. mk_cast to_t e)
  5328. | TAbstract (a_to, _), TAbstract(a_from, _) when a_to == a_from ->
  5329. e
  5330. | TAbstract _, TInst({ cl_kind = KTypeParameter _ }, _)
  5331. | TInst({ cl_kind = KTypeParameter _ }, _), TAbstract _ ->
  5332. do_unsafe_cast()
  5333. | TAbstract _, _
  5334. | _, TAbstract _ ->
  5335. (try
  5336. unify from_t to_t;
  5337. mk_cast to_t e
  5338. with | Unify_error _ ->
  5339. try
  5340. unify to_t from_t;
  5341. mk_cast to_t e
  5342. with | Unify_error _ ->
  5343. do_unsafe_cast())
  5344. | TEnum(e_to, []), TEnum(e_from, []) ->
  5345. if e_to == e_from then
  5346. e
  5347. else
  5348. (* potential unsafe cast *)
  5349. (do_unsafe_cast ())
  5350. | TEnum(e_to, params_to), TEnum(e_from, params_from) when e_to.e_path = e_from.e_path ->
  5351. (try
  5352. List.iter2 (type_eq gen (if gen.gallow_tp_dynamic_conversion then EqRightDynamic else EqStrict)) params_from params_to;
  5353. e
  5354. with
  5355. | Unify_error _ -> do_unsafe_cast ()
  5356. )
  5357. | TEnum(en, params_to), TInst(cl, params_from)
  5358. | TInst(cl, params_to), TEnum(en, params_from) ->
  5359. (* this is here for max compatibility with EnumsToClass module *)
  5360. if en.e_path = cl.cl_path && en.e_extern then begin
  5361. (try
  5362. List.iter2 (type_eq gen (if gen.gallow_tp_dynamic_conversion then EqRightDynamic else EqStrict)) params_from params_to;
  5363. e
  5364. with
  5365. | Invalid_argument("List.iter2") ->
  5366. (*
  5367. this is a hack for RealTypeParams. Since there is no way at this stage to know if the class is the actual
  5368. EnumsToClass derived from the enum, we need to imply from possible ArgumentErrors (because of RealTypeParams interfaces),
  5369. that they would only happen if they were a RealTypeParams created interface
  5370. *)
  5371. e
  5372. | Unify_error _ -> do_unsafe_cast ()
  5373. )
  5374. end else
  5375. do_unsafe_cast ()
  5376. | TType(t_to, params_to), TType(t_from, params_from) when t_to == t_from ->
  5377. if gen.gspecial_needs_cast real_to_t real_from_t then
  5378. (try
  5379. List.iter2 (type_eq gen (if gen.gallow_tp_dynamic_conversion then EqRightDynamic else EqStrict)) params_from params_to;
  5380. e
  5381. with
  5382. | Unify_error _ -> do_unsafe_cast ()
  5383. )
  5384. else
  5385. e
  5386. | TType(t_to, _), TType(t_from,_) ->
  5387. if gen.gspecial_needs_cast real_to_t real_from_t then
  5388. mk_cast to_t e
  5389. else
  5390. e
  5391. | TType _, _ when gen.gspecial_needs_cast real_to_t real_from_t ->
  5392. mk_cast to_t e
  5393. | _, TType _ when gen.gspecial_needs_cast real_to_t real_from_t ->
  5394. mk_cast to_t e
  5395. (*| TType(t_to, _), TType(t_from, _) ->
  5396. if t_to.t_path = t_from.t_path then
  5397. e
  5398. else if is_unsafe_cast gen real_to_t real_from_t then (* is_unsafe_cast will already follow both *)
  5399. (do_unsafe_cast ())
  5400. else
  5401. mk_cast to_t e*)
  5402. | TType _, _
  5403. | _, TType _ ->
  5404. if is_unsafe_cast gen real_to_t real_from_t then (* is_unsafe_cast will already follow both *)
  5405. (do_unsafe_cast ())
  5406. else
  5407. mk_cast to_t e
  5408. | TAnon anon, _ ->
  5409. if PMap.is_empty anon.a_fields then
  5410. e
  5411. else
  5412. mk_cast to_t e
  5413. | TFun(args, ret), TFun(args2, ret2) ->
  5414. let get_args = List.map (fun (_,_,t) -> t) in
  5415. (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)
  5416. | _, _ ->
  5417. do_unsafe_cast ()
  5418. (* end of cast handler *)
  5419. (* ******************* *)
  5420. let is_static_overload c name =
  5421. match c.cl_super with
  5422. | None -> false
  5423. | Some (sup,_) ->
  5424. let rec loop c =
  5425. (PMap.mem name c.cl_statics) || (match c.cl_super with
  5426. | None -> false
  5427. | Some (sup,_) -> loop sup)
  5428. in
  5429. loop sup
  5430. let does_unify a b =
  5431. try
  5432. unify a b;
  5433. true
  5434. with | Unify_error _ -> false
  5435. (* this is a workaround for issue #1743, as FInstance() is returning the incorrect classfield *)
  5436. let rec clean_t t = match follow t with
  5437. | TAbstract(a,tl) when not (Meta.has Meta.CoreType a.a_meta) ->
  5438. clean_t (Abstract.get_underlying_type a tl)
  5439. | t -> t
  5440. let select_overload gen applied_f overloads types params =
  5441. let rec check_arg arglist elist =
  5442. match arglist, elist with
  5443. | [], [] -> true (* it is valid *)
  5444. | (_,_,TAbstract({ a_path = (["haxe";"extern"],"Rest") }, [t])) :: [], elist ->
  5445. List.for_all (fun (_,_,et) -> Type.type_iseq (clean_t et) (clean_t t)) elist
  5446. | (_,_,t) :: arglist, (_,_,et) :: elist when Type.type_iseq (clean_t et) (clean_t t) ->
  5447. check_arg arglist elist
  5448. | _ -> false
  5449. in
  5450. match follow applied_f with
  5451. | TFun _ ->
  5452. replace_mono applied_f;
  5453. let args, _ = get_fun applied_f in
  5454. let elist = List.rev args in
  5455. let rec check_overload overloads =
  5456. match overloads with
  5457. | (t, cf) :: overloads ->
  5458. let cft = apply_params types params t in
  5459. let cft = monomorphs cf.cf_params cft in
  5460. let args, _ = get_fun cft in
  5461. if check_arg (List.rev args) elist then
  5462. cf,t,false
  5463. else if overloads = [] then
  5464. cf,t,true (* no compatible overload was found *)
  5465. else
  5466. check_overload overloads
  5467. | [] -> assert false
  5468. in
  5469. check_overload overloads
  5470. | _ -> match overloads with (* issue #1742 *)
  5471. | (t,cf) :: [] -> cf,t,true
  5472. | (t,cf) :: _ -> cf,t,false
  5473. | _ -> assert false
  5474. let choose_ctor gen cl tparams etl maybe_empty_t p =
  5475. let ctor, sup, stl = OverloadingConstructor.cur_ctor cl tparams in
  5476. (* get returned stl, with Dynamic as t_empty *)
  5477. let rec get_changed_stl c tl =
  5478. if c == sup then
  5479. tl
  5480. else match c.cl_super with
  5481. | None -> stl
  5482. | Some(sup,stl) -> get_changed_stl sup (List.map (apply_params c.cl_params tl) stl)
  5483. in
  5484. let ret_tparams = List.map (fun t -> match follow t with
  5485. | TDynamic _ | TMono _ -> t_empty
  5486. | _ -> t) tparams in
  5487. let ret_stl = get_changed_stl cl ret_tparams in
  5488. let ctors = ctor :: ctor.cf_overloads in
  5489. List.iter replace_mono etl;
  5490. (* first filter out or select outright maybe_empty *)
  5491. let ctors, is_overload = match etl, maybe_empty_t with
  5492. | [t], Some empty_t ->
  5493. let count = ref 0 in
  5494. let is_empty_call = Type.type_iseq t empty_t in
  5495. let ret = List.filter (fun cf -> match follow cf.cf_type with
  5496. (* | TFun([_,_,t],_) -> incr count; true *)
  5497. | TFun([_,_,t],_) ->
  5498. replace_mono t; incr count; is_empty_call = (Type.type_iseq t empty_t)
  5499. | _ -> false) ctors in
  5500. ret, !count > 1
  5501. | _ ->
  5502. let len = List.length etl in
  5503. let ret = List.filter (fun cf -> List.length (fst (get_fun cf.cf_type)) = len) ctors in
  5504. ret, (match ret with | _ :: [] -> false | _ -> true)
  5505. in
  5506. let rec check_arg arglist elist =
  5507. match arglist, elist with
  5508. | [], [] -> true
  5509. | (_,_,t) :: arglist, et :: elist -> (try
  5510. let t = run_follow gen t in
  5511. unify et t;
  5512. check_arg arglist elist
  5513. with | Unify_error el ->
  5514. (* List.iter (fun el -> gen.gcon.warning (Typecore.unify_error_msg (print_context()) el) p) el; *)
  5515. false)
  5516. | _ ->
  5517. false
  5518. in
  5519. let rec check_cf cf =
  5520. let t = apply_params sup.cl_params stl cf.cf_type in
  5521. replace_mono t;
  5522. let args, _ = get_fun t in
  5523. check_arg args etl
  5524. in
  5525. match is_overload, ctors with
  5526. | false, [c] ->
  5527. false, c, sup, ret_stl
  5528. | _ ->
  5529. is_overload, List.find check_cf ctors, sup, ret_stl
  5530. let change_rest tfun elist =
  5531. let rec loop acc arglist elist = match arglist, elist with
  5532. | (_,_,TAbstract({ a_path = (["haxe";"extern"],"Rest") },[t])) :: [], elist ->
  5533. List.rev (List.map (fun _ -> "rest",false,t) elist @ acc)
  5534. | (n,o,t) :: arglist, _ :: elist ->
  5535. loop ((n,o,t) :: acc) arglist elist
  5536. | _, _ ->
  5537. List.rev acc
  5538. in
  5539. let args,ret = get_fun tfun in
  5540. TFun(loop [] args elist, ret)
  5541. (*
  5542. Type parameter handling
  5543. It will detect if/what type parameters were used, and call the cast handler
  5544. It will handle both TCall(TField) and TCall by receiving a texpr option field: e
  5545. Also it will transform the type parameters with greal_type_param and make
  5546. handle_impossible_tparam - should cases where the type parameter is impossible to be determined from the called parameters be Dynamic?
  5547. e.g. static function test<T>():T {}
  5548. *)
  5549. (* match e.eexpr with | TCall( ({ eexpr = TField(ef, f) }) as e1, elist ) -> *)
  5550. let handle_type_parameter gen e e1 ef ~clean_ef ~overloads_cast_to_base f elist calls_parameters_explicitly =
  5551. (* the ONLY way to know if this call has parameters is to analyze the calling field. *)
  5552. (* To make matters a little worse, on both C# and Java only in some special cases that type parameters will be used *)
  5553. (* Namely, when using reflection type parameters are useless, of course. This also includes anonymous types *)
  5554. (* this will have to be handled by gparam_func_call *)
  5555. let return_var efield =
  5556. match e with
  5557. | None ->
  5558. efield
  5559. | Some ecall ->
  5560. match follow efield.etype with
  5561. | TFun(_,ret) ->
  5562. (* closures will be handled by the closure handler. So we will just hint what's the expected type *)
  5563. (* FIXME: should closures have also its arguments cast correctly? In the current implementation I think not. TO_REVIEW *)
  5564. handle_cast gen { ecall with eexpr = TCall(efield, elist) } (gen.greal_type ecall.etype) ret
  5565. | _ ->
  5566. { ecall with eexpr = TCall(efield, elist) }
  5567. in
  5568. let real_type = gen.greal_type ef.etype in
  5569. (* this part was rewritten at roughly r6477 in order to correctly support overloads *)
  5570. (match field_access_esp gen real_type (f) with
  5571. | FClassField (cl, params, _, cf, is_static, actual_t, declared_t) when e <> None && (cf.cf_kind = Method MethNormal || cf.cf_kind = Method MethInline) ->
  5572. (* C# target changes params with a real_type function *)
  5573. let params = match follow clean_ef.etype with
  5574. | TInst(_,params) -> params
  5575. | _ -> params
  5576. in
  5577. let ecall = get e in
  5578. let ef = ref ef in
  5579. let is_overload = cf.cf_overloads <> [] || Meta.has Meta.Overload cf.cf_meta || (is_static && is_static_overload cl (field_name f)) in
  5580. let cf, actual_t, error = match is_overload with
  5581. | false ->
  5582. (* since actual_t from FClassField already applies greal_type, we're using the get_overloads helper to get this info *)
  5583. let t = if cf.cf_params = [] then (* this if statement must be eliminated - it's a workaround for #3516 + infer params. *)
  5584. actual_t
  5585. else
  5586. declared_t
  5587. in
  5588. cf,t,false
  5589. | true ->
  5590. let (cf, actual_t, error), is_static = match f with
  5591. | FInstance(c,_,cf) | FClosure(Some (c,_),cf) ->
  5592. (* get from overloads *)
  5593. (* FIXME: this is a workaround for issue #1743 . Uncomment this code after it was solved *)
  5594. (* let t, cf = List.find (fun (t,cf2) -> cf == cf2) (Typeload.get_overloads cl (field_name f)) in *)
  5595. (* cf, t, false *)
  5596. select_overload gen e1.etype (Typeload.get_overloads cl (field_name f)) cl.cl_params params, false
  5597. | FStatic(c,f) ->
  5598. (* workaround for issue #1743 *)
  5599. (* f,f.cf_type, false *)
  5600. select_overload gen e1.etype ((f.cf_type,f) :: List.map (fun f -> f.cf_type,f) f.cf_overloads) [] [], true
  5601. | _ ->
  5602. gen.gcon.warning "Overloaded classfield typed as anonymous" ecall.epos;
  5603. (cf, actual_t, true), true
  5604. in
  5605. 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
  5606. | Some(cf_orig,actual_t,_,_,declared_cl,tl,tlch) ->
  5607. let rec is_super e = match e.eexpr with
  5608. | TConst TSuper -> true
  5609. | TParenthesis p | TMeta(_,p) -> is_super p
  5610. | _ -> false
  5611. in
  5612. if declared_cl != cl && overloads_cast_to_base && not (is_super !ef) then begin
  5613. let pos = (!ef).epos in
  5614. ef := {
  5615. eexpr = TCall(
  5616. { eexpr = TLocal(alloc_var "__as__" t_dynamic); etype = t_dynamic; epos = pos },
  5617. [!ef]);
  5618. etype = TInst(declared_cl,List.map (apply_params cl.cl_params params) tl);
  5619. epos = pos
  5620. }
  5621. end;
  5622. { cf_orig with cf_name = cf.cf_name },actual_t,false
  5623. | None ->
  5624. gen.gcon.warning "Cannot find matching overload" ecall.epos;
  5625. cf, actual_t, true
  5626. else
  5627. cf,actual_t,error
  5628. in
  5629. (* take off Rest param *)
  5630. let actual_t = change_rest actual_t elist in
  5631. (* set the real (selected) class field *)
  5632. let f = match f with
  5633. | FInstance(c,tl,_) -> FInstance(c,tl,cf)
  5634. | FClosure(c,_) -> FClosure(c,cf)
  5635. | FStatic(c,_) -> FStatic(c,cf)
  5636. | f -> f
  5637. in
  5638. let error = error || (match follow actual_t with | TFun _ -> false | _ -> true) in
  5639. if error then (* if error, ignore arguments *)
  5640. if is_void ecall.etype then
  5641. { ecall with eexpr = TCall({ e1 with eexpr = TField(!ef, f) }, elist ) }
  5642. else
  5643. mk_cast ecall.etype { ecall with eexpr = TCall({ e1 with eexpr = TField(!ef, f) }, elist ) }
  5644. else begin
  5645. (* infer arguments *)
  5646. (* let called_t = TFun(List.map (fun e -> "arg",false,e.etype) elist, ecall.etype) in *)
  5647. 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 *)
  5648. let called_t = change_rest called_t elist in
  5649. 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
  5650. (* get what the backend actually sees *)
  5651. (* actual field's function *)
  5652. let actual_t = get_real_fun gen actual_t in
  5653. let real_params = gen.greal_type_param (TClassDecl cl) params in
  5654. let function_t = apply_params cl.cl_params real_params actual_t in
  5655. let real_fparams = if calls_parameters_explicitly then
  5656. gen.greal_type_param (TClassDecl cl) fparams
  5657. else
  5658. 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
  5659. let function_t = get_real_fun gen (apply_params cf.cf_params real_fparams function_t) in
  5660. let args_ft, ret_ft = get_fun function_t in
  5661. (* applied function *)
  5662. let applied = elist in
  5663. (* check types list *)
  5664. let new_ecall, elist = try
  5665. let elist = List.map2 (fun applied (_,_,funct) ->
  5666. match is_overload, applied.eexpr with
  5667. | true, TConst TNull ->
  5668. mk_cast (gen.greal_type funct) applied
  5669. | true, _ -> (* when not (type_iseq gen (gen.greal_type applied.etype) funct) -> *)
  5670. let ret = handle_cast gen applied (funct) (gen.greal_type applied.etype) in
  5671. (match ret.eexpr with
  5672. | TCast _ -> ret
  5673. | _ -> mk_cast (funct) ret)
  5674. | _ ->
  5675. handle_cast gen applied (funct) (gen.greal_type applied.etype)
  5676. ) applied args_ft in
  5677. { ecall with
  5678. eexpr = TCall(
  5679. { e1 with eexpr = TField(!ef, f) },
  5680. elist);
  5681. }, elist
  5682. with | Invalid_argument("List.map2") ->
  5683. gen.gcon.warning ("This expression may be invalid" ) ecall.epos;
  5684. { ecall with eexpr = TCall({ e1 with eexpr = TField(!ef, f) }, elist) }, elist
  5685. in
  5686. let new_ecall = if fparams <> [] then gen.gparam_func_call new_ecall { e1 with eexpr = TField(!ef, f) } fparams elist else new_ecall in
  5687. let ret = handle_cast gen new_ecall (gen.greal_type ecall.etype) (gen.greal_type ret_ft) in
  5688. (match gen.gcon.platform, cf.cf_params, ret.eexpr with
  5689. | _, _, TCast _ -> ret
  5690. | Java, _ :: _, _ ->
  5691. (* this is a workaround for a javac openjdk issue with unused type parameters and return type inference *)
  5692. (* see more at issue #3123 *)
  5693. mk_cast (gen.greal_type ret_ft) new_ecall
  5694. | _ -> ret)
  5695. end
  5696. | FClassField (cl,params,_,{ cf_kind = (Method MethDynamic | Var _) },_,actual_t,_) ->
  5697. (* if it's a var, we will just try to apply the class parameters that have been changed with greal_type_param *)
  5698. let t = apply_params cl.cl_params (gen.greal_type_param (TClassDecl cl) params) (gen.greal_type actual_t) in
  5699. return_var (handle_cast gen { e1 with eexpr = TField(ef, f) } (gen.greal_type e1.etype) (gen.greal_type t))
  5700. | FClassField (cl,params,_,cf,_,actual_t,_) ->
  5701. 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 *)
  5702. | FEnumField (en, efield, true) ->
  5703. 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
  5704. (match en.e_params with
  5705. (*
  5706. | [] ->
  5707. let args, ret = get_args (efield.ef_type) in
  5708. let ef = { ef with eexpr = TTypeExpr( TEnumDecl en ); etype = TEnum(en, []) } in
  5709. 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)
  5710. *)
  5711. | _ ->
  5712. let pt = match e with | None -> real_type | Some _ -> snd (get_fun e1.etype) in
  5713. let _params = match follow pt with | TEnum(_, p) -> p | _ -> gen.gcon.warning (debug_expr e1) e1.epos; assert false in
  5714. let args, ret = get_args efield.ef_type in
  5715. let actual_t = TFun(List.map (fun (n,o,t) -> (n,o,gen.greal_type t)) args, gen.greal_type ret) in
  5716. (*
  5717. because of differences on how <Dynamic> is handled on the platforms, this is a hack to be able to
  5718. correctly use class field type parameters with RealTypeParams
  5719. *)
  5720. let cf_params = List.map (fun t -> match follow t with | TDynamic _ -> t_empty | _ -> t) _params in
  5721. let t = apply_params en.e_params (gen.greal_type_param (TEnumDecl en) cf_params) actual_t in
  5722. let t = apply_params efield.ef_params (List.map (fun _ -> t_dynamic) efield.ef_params) t in
  5723. let args, ret = get_args t in
  5724. let elist = List.map2 (fun param (_,_,t) -> handle_cast gen (param) (gen.greal_type t) (gen.greal_type param.etype)) elist args in
  5725. let e1 = { e1 with eexpr = TField({ ef with eexpr = TTypeExpr( TEnumDecl en ); etype = TEnum(en, _params) }, FEnum(en, efield) ) } in
  5726. let new_ecall = gen.gparam_func_call ecall e1 _params elist in
  5727. handle_cast gen new_ecall (gen.greal_type ecall.etype) (gen.greal_type ret)
  5728. )
  5729. | FEnumField _ when is_some e -> assert false
  5730. | FEnumField (en,efield,_) ->
  5731. return_var { e1 with eexpr = TField({ ef with eexpr = TTypeExpr( TEnumDecl en ); },FEnum(en,efield)) }
  5732. (* no target by date will uses this.so this code may not be correct at all *)
  5733. | FAnonField cf ->
  5734. let t = gen.greal_type cf.cf_type in
  5735. return_var (handle_cast gen { e1 with eexpr = TField(ef, f) } (gen.greal_type e1.etype) t)
  5736. | FNotFound
  5737. | FDynamicField _ ->
  5738. if is_some e then
  5739. return_var { e1 with eexpr = TField(ef, f) }
  5740. else
  5741. 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 *)
  5742. )
  5743. (* end of type parameter handling *)
  5744. (* ****************************** *)
  5745. (** overloads_cast_to_base argument will cast overloaded function types to the class that declared it. **)
  5746. (** This is necessary for C#, and if true, will require the target to implement __as__, as a `quicker` form of casting **)
  5747. let default_implementation gen ?(native_string_cast = true) ?(overloads_cast_to_base = false) maybe_empty_t calls_parameters_explicitly =
  5748. let handle e t1 t2 = handle_cast gen e (gen.greal_type t1) (gen.greal_type t2) in
  5749. let in_value = ref false in
  5750. let rec clean_cast e = match e.eexpr with
  5751. | TCast(e,_) -> clean_cast e
  5752. | TParenthesis(e) | TMeta(_,e) -> clean_cast e
  5753. | _ -> e
  5754. in
  5755. let rec run ?(just_type = false) e =
  5756. let handle = if not just_type then handle else fun e t1 t2 -> { e with etype = gen.greal_type t2 } in
  5757. let was_in_value = !in_value in
  5758. in_value := true;
  5759. match e.eexpr with
  5760. | TConst ( TInt _ | TFloat _ | TBool _ as const ) ->
  5761. (* take off any Null<> that it may have *)
  5762. let t = follow (run_follow gen e.etype) in
  5763. (* do not allow constants typed as Single - need to cast them *)
  5764. let real_t = match const with
  5765. | TInt _ -> gen.gcon.basic.tint
  5766. | TFloat _ -> gen.gcon.basic.tfloat
  5767. | TBool _ -> gen.gcon.basic.tbool
  5768. | _ -> assert false
  5769. in
  5770. handle e t real_t
  5771. | TCast( { eexpr = TCall( { eexpr = TLocal { v_name = "__delegate__" } } as local, [del] ) } as e2, _) ->
  5772. { e with eexpr = TCast({ e2 with eexpr = TCall(local, [Type.map_expr run del]) }, None) }
  5773. | TBinop ( (Ast.OpAssign | Ast.OpAssignOp _ as op), e1, e2 ) ->
  5774. let e1 = run ~just_type:true e1 in
  5775. let e2 = handle (run e2) e1.etype e2.etype in
  5776. { e with eexpr = TBinop(op, clean_cast e1, e2) }
  5777. | TBinop ( (Ast.OpShl | Ast.OpShr | Ast.OpUShr as op), e1, e2 ) ->
  5778. let e1 = run e1 in
  5779. let e2 = handle (run e2) (gen.gcon.basic.tint) e2.etype in
  5780. { e with eexpr = TBinop(op, e1, e2) }
  5781. | TField(ef, f) ->
  5782. handle_type_parameter gen None e (run ef) ~clean_ef:ef ~overloads_cast_to_base:overloads_cast_to_base f [] calls_parameters_explicitly
  5783. | TArrayDecl el ->
  5784. let et = e.etype in
  5785. let base_type = match follow et with
  5786. | TInst({ cl_path = ([], "Array") } as cl, bt) -> gen.greal_type_param (TClassDecl cl) bt
  5787. | _ -> assert false
  5788. in
  5789. let base_type = List.hd base_type in
  5790. { e with eexpr = TArrayDecl( List.map (fun e -> handle (run e) base_type e.etype) el ); etype = et }
  5791. | TCall ({ eexpr = TLocal { v_name = "__array__" } } as arr_local, el) ->
  5792. let et = e.etype in
  5793. let base_type = match follow et with
  5794. | TInst(cl, bt) -> gen.greal_type_param (TClassDecl cl) bt
  5795. | _ -> assert false
  5796. in
  5797. let base_type = List.hd base_type in
  5798. { e with eexpr = TCall(arr_local, List.map (fun e -> handle (run e) base_type e.etype) el ); etype = et }
  5799. | 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 ->
  5800. { e with eexpr = TCall(local, List.map (fun e -> (match e.eexpr with TBlock _ -> in_value := false | _ -> ()); run e) params) }
  5801. | TCall( ({ eexpr = TField(ef, f) }) as e1, elist ) ->
  5802. 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
  5803. (* the TNew and TSuper code was modified at r6497 *)
  5804. | TCall( { eexpr = TConst TSuper } as ef, eparams ) ->
  5805. let cl, tparams = match follow ef.etype with
  5806. | TInst(cl,p) ->
  5807. cl,p
  5808. | _ -> assert false in
  5809. (try
  5810. let is_overload, cf, sup, stl = choose_ctor gen cl tparams (List.map (fun e -> e.etype) eparams) maybe_empty_t e.epos in
  5811. let handle e t1 t2 =
  5812. if is_overload then
  5813. let ret = handle e t1 t2 in
  5814. match ret.eexpr with
  5815. | TCast _ -> ret
  5816. | _ -> mk_cast (gen.greal_type t1) e
  5817. else
  5818. handle e t1 t2
  5819. in
  5820. let stl = gen.greal_type_param (TClassDecl sup) stl in
  5821. let args, _ = get_fun (apply_params sup.cl_params stl cf.cf_type) in
  5822. let eparams = List.map2 (fun e (_,_,t) ->
  5823. handle (run e) t e.etype
  5824. ) eparams args in
  5825. { e with eexpr = TCall(ef, eparams) }
  5826. with | Not_found ->
  5827. gen.gcon.warning "No overload found for this constructor call" e.epos;
  5828. { e with eexpr = TCall(ef, List.map run eparams) })
  5829. | TCall (ef, eparams) ->
  5830. (match ef.etype with
  5831. | TFun(p, ret) ->
  5832. handle ({ e with eexpr = TCall(run ef, List.map2 (fun param (_,_,t) -> handle (run param) t param.etype) eparams p) }) e.etype ret
  5833. | _ -> Type.map_expr run e
  5834. )
  5835. (* the TNew and TSuper code was modified at r6497 *)
  5836. | TNew ({ cl_kind = KTypeParameter _ }, _, _) ->
  5837. Type.map_expr run e
  5838. | TNew (cl, tparams, eparams) -> (try
  5839. let is_overload, cf, sup, stl = choose_ctor gen cl tparams (List.map (fun e -> e.etype) eparams) maybe_empty_t e.epos in
  5840. let handle e t1 t2 =
  5841. if is_overload then
  5842. let ret = handle e t1 t2 in
  5843. match ret.eexpr with
  5844. | TCast _ -> ret
  5845. | _ -> mk_cast (gen.greal_type t1) e
  5846. else
  5847. handle e t1 t2
  5848. in
  5849. let stl = gen.greal_type_param (TClassDecl sup) stl in
  5850. let args, _ = get_fun (apply_params sup.cl_params stl cf.cf_type) in
  5851. let eparams = List.map2 (fun e (_,_,t) ->
  5852. handle (run e) t e.etype
  5853. ) eparams args in
  5854. { e with eexpr = TNew(cl, tparams, eparams) }
  5855. with | Not_found ->
  5856. gen.gcon.warning "No overload found for this constructor call" e.epos;
  5857. { e with eexpr = TNew(cl, tparams, List.map run eparams) })
  5858. | TArray(arr, idx) ->
  5859. let arr_etype = match follow arr.etype with
  5860. | (TInst _ as t) -> t
  5861. | TAbstract (a, pl) when not (Meta.has Meta.CoreType a.a_meta) ->
  5862. follow (Abstract.get_underlying_type a pl)
  5863. | t -> t in
  5864. let idx = match gen.greal_type idx.etype with
  5865. | TAbstract({ a_path = [],"Int" },_) -> run idx
  5866. | _ -> match handle (run idx) gen.gcon.basic.tint (gen.greal_type idx.etype) with
  5867. | ({ eexpr = TCast _ } as idx) -> idx
  5868. | idx -> mk_cast gen.gcon.basic.tint idx
  5869. in
  5870. let e = { e with eexpr = TArray(run arr, idx) } in
  5871. (* get underlying class (if it's a class *)
  5872. (match arr_etype with
  5873. | TInst(cl, params) ->
  5874. (* see if it implements ArrayAccess *)
  5875. (match cl.cl_array_access with
  5876. | None -> e
  5877. | Some t ->
  5878. (* if it does, apply current parameters (and change them) *)
  5879. (* let real_t = apply_params_internal (List.map (gen.greal_type_param (TClassDecl cl))) cl params t in *)
  5880. let param = apply_params cl.cl_params (gen.greal_type_param (TClassDecl cl) params) t in
  5881. let real_t = apply_params cl.cl_params params param in
  5882. (* see if it needs a cast *)
  5883. handle (e) (gen.greal_type e.etype) (gen.greal_type real_t)
  5884. )
  5885. | _ -> Type.map_expr run e)
  5886. | TVar (v, eopt) ->
  5887. { e with eexpr = TVar (v, match eopt with
  5888. | None -> eopt
  5889. | Some e -> Some( handle (run e) v.v_type e.etype ))
  5890. }
  5891. (* FIXME deal with in_value when using other statements that may not have a TBlock wrapped on them *)
  5892. | TIf (econd, ethen, Some(eelse)) when was_in_value ->
  5893. { 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 ) ) }
  5894. | TIf (econd, ethen, eelse) ->
  5895. { 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) }
  5896. | TWhile (econd, e1, flag) ->
  5897. { e with eexpr = TWhile (handle (run econd) gen.gcon.basic.tbool econd.etype, (in_value := false; run (mk_block e1)), flag) }
  5898. | TSwitch (cond, el_e_l, edef) ->
  5899. { 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) }
  5900. (* | TMatch (cond, en, il_vl_e_l, edef) ->
  5901. { 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) } *)
  5902. | TFor (v,cond,e1) ->
  5903. { e with eexpr = TFor(v, run cond, (in_value := false; run (mk_block e1))) }
  5904. | TTry (e, ve_l) ->
  5905. { 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) }
  5906. | TBlock el ->
  5907. let i = ref 0 in
  5908. let len = List.length el in
  5909. { e with eexpr = TBlock ( List.map (fun e ->
  5910. incr i;
  5911. if !i <> len || not was_in_value then
  5912. in_value := false;
  5913. run e
  5914. ) el ) }
  5915. | TCast (expr, md) when is_void (follow e.etype) ->
  5916. run expr
  5917. | TCast (expr, md) ->
  5918. let rec get_null e =
  5919. match e.eexpr with
  5920. | TConst TNull -> Some e
  5921. | TParenthesis e | TMeta(_,e) -> get_null e
  5922. | _ -> None
  5923. in
  5924. (match get_null expr with
  5925. | Some enull ->
  5926. if gen.gcon.platform = Cs then
  5927. { enull with etype = gen.greal_type e.etype }
  5928. else
  5929. mk_cast (gen.greal_type e.etype) enull
  5930. | _ ->
  5931. let last_unsafe = gen.gon_unsafe_cast in
  5932. gen.gon_unsafe_cast <- (fun t t2 pos -> ());
  5933. let ret = handle (run expr) e.etype expr.etype in
  5934. gen.gon_unsafe_cast <- last_unsafe;
  5935. match ret.eexpr with
  5936. | TCast _ -> ret
  5937. | _ -> { e with eexpr = TCast(ret,md); etype = gen.greal_type e.etype }
  5938. )
  5939. (*| TCast _ ->
  5940. (* if there is already a cast, we should skip this cast check *)
  5941. Type.map_expr run e*)
  5942. | TFunction f ->
  5943. in_value := false;
  5944. Type.map_expr run e
  5945. | _ -> Type.map_expr run e
  5946. in
  5947. run
  5948. let configure gen (mapping_func:texpr->texpr) =
  5949. gen.ghandle_cast <- (fun tto tfrom expr -> handle_cast gen expr (gen.greal_type tto) (gen.greal_type tfrom));
  5950. let map e = Some(mapping_func e) in
  5951. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map;
  5952. ReturnCast.configure gen
  5953. end;;
  5954. (* ******************************************* *)
  5955. (* Reflection-enabling Class fields *)
  5956. (* ******************************************* *)
  5957. (*
  5958. 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)
  5959. This module will create class fields that enable reflection for targets that have a slow or inexistent reflection abilities. Because of the similarity
  5960. of strategies between what should have been different modules, they are all unified in this reflection-enabling class fields.
  5961. They include:
  5962. * Get(isStatic, throwErrors, isCheck) / Set fields . Remember to allow implements Dynamic also.
  5963. * Invoke fields(isStatic) -> You need to configure how many invoke_field fields there will be. + invokeDynamic
  5964. * Has field -> parameter in get field that returns __undefined__ if it doesn't exist.
  5965. * GetType -> return the current Class<> / Enum<>
  5966. * Fields(isStatic) -> returns all the fields / static fields. Remember to allow implements Dynamic also
  5967. * Create(arguments array), CreateEmpty - calls new() or create empty
  5968. * getInstanceFields / getClassFields -> show even function fields, everything!
  5969. * deleteField -> only for implements Dynamic
  5970. for enums:
  5971. * createEnum -> invokeField for classes
  5972. * createEnumIndex -> use invokeField as well, and use numbers e.g. "0", "1", "2" .... For this, use "@:alias" metadata
  5973. * getEnumConstructs -> fields()
  5974. need to be solved outside:
  5975. * getEnumName
  5976. * enumIndex
  5977. *
  5978. need to be solved by haxe code:
  5979. * enumParameters -> for (field in Reflect.fields(enum)) arr.push(Reflect.field(enum, field))
  5980. Standard:
  5981. if a class contains a @:$enum metadata, it's treated as a converted enum to class
  5982. Optimizations:
  5983. * 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
  5984. a string (which is nullable for compile-time hashes) + an int.
  5985. At compile-time, a collision will throw an error (like neko).
  5986. At runtime, a collision will make a negative int. Negative ints will always resolve to a special Hash<> field which takes a string.
  5987. * 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,
  5988. if the type is detected to already be hxgen
  5989. * TODO: if for() optimization for arrays is disabled, we can replace for(field in Reflect.fields(obj)) to:
  5990. 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
  5991. the already hashed fields.
  5992. Mappings:
  5993. * if create Dynamic class is true, TObjectDecl will be mapped to new DynamicClass(fields, [hashedFields], values)
  5994. *
  5995. dependencies:
  5996. 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,
  5997. while the AST has its meaning close to haxe's.
  5998. Should run before InitFunction so it detects variables containing expressions as "always-execute" expressions, even when using CreateEmpty
  5999. * Must run before switch() syntax changes
  6000. *)
  6001. open ClosuresToClass;;
  6002. module ReflectionCFs =
  6003. struct
  6004. let name = "reflection_cfs"
  6005. type rcf_ctx =
  6006. {
  6007. rcf_gen : generator_ctx;
  6008. rcf_ft : ClosuresToClass.closures_ctx;
  6009. rcf_optimize : bool;
  6010. mutable rcf_float_special_case : bool;
  6011. mutable rcf_object_iface : tclass;
  6012. mutable rcf_create_getsetinvoke_fields : bool;
  6013. (* should we create the get type (get Class)? *)
  6014. mutable rcf_create_get_type : bool;
  6015. (* should we handle implements dynamic? *)
  6016. mutable rcf_handle_impl_dynamic : bool;
  6017. (*
  6018. create_dyn_overloading_ctor :
  6019. when creating the implements dynamic code, we can also create a special constructor for
  6020. the actual DynamicObject class, which will receive all its <implements Dynamic> fields from the code outside.
  6021. Note that this will only work on targets that support overloading contrstuctors, as any class that extends
  6022. our DynamicObject will have an empty super() call
  6023. *)
  6024. mutable rcf_create_dyn_ctor : bool;
  6025. mutable rcf_max_func_arity : int;
  6026. (*
  6027. the hash lookup function. can be an inlined expr or simply a function call.
  6028. its only needed features is that it should return the index of the key if found, and the
  6029. complement of the index of where it should be inserted if not found (Ints).
  6030. hash->hash_array->length->returning expression
  6031. *)
  6032. mutable rcf_hash_function : texpr->texpr->texpr->texpr;
  6033. mutable rcf_lookup_function : texpr->texpr;
  6034. (* hash_array->length->pos->value *)
  6035. mutable rcf_insert_function : texpr->texpr->texpr->texpr->texpr;
  6036. (* hash_array->length->pos->value *)
  6037. mutable rcf_remove_function : texpr->texpr->texpr->texpr;
  6038. (*
  6039. class_cl is the real class for Class<> instances.
  6040. In the current implementation, due to some targets' limitations, (in particular, Java),
  6041. we have to use an empty object so we can access its virtual mehtods.
  6042. FIXME find a better way to create Class<> objects in a performant way
  6043. *)
  6044. mutable rcf_class_cl : tclass option;
  6045. (*
  6046. Also about the Class<> type, should we crate all classes eagerly?
  6047. If false, it means that we should have a way at runtime to create the class when needed by
  6048. Type.resolveClass/Enum
  6049. *)
  6050. mutable rcf_class_eager_creation : bool;
  6051. rcf_hash_fields : (int, string) Hashtbl.t;
  6052. (*
  6053. main expr -> field expr -> field string -> possible hash int (if optimize) -> possible set expr -> should_throw_exceptions -> changed expression
  6054. Changes a get / set field to the runtime resolution function
  6055. *)
  6056. mutable rcf_on_getset_field : texpr->texpr->string->int32 option->texpr option->bool->texpr;
  6057. mutable rcf_on_call_field : texpr->texpr->string->int32 option->texpr list->texpr;
  6058. mutable rcf_handle_statics : bool;
  6059. }
  6060. let new_ctx gen ft object_iface optimize dynamic_getset_field dynamic_call_field hash_function lookup_function insert_function remove_function handle_statics =
  6061. {
  6062. rcf_gen = gen;
  6063. rcf_ft = ft;
  6064. rcf_optimize = optimize;
  6065. rcf_float_special_case = true;
  6066. rcf_object_iface = object_iface;
  6067. rcf_create_getsetinvoke_fields = true;
  6068. rcf_create_get_type = true;
  6069. rcf_handle_impl_dynamic = true;
  6070. rcf_create_dyn_ctor = true;
  6071. rcf_max_func_arity = 10;
  6072. rcf_hash_function = hash_function;
  6073. rcf_lookup_function = lookup_function;
  6074. rcf_insert_function = insert_function;
  6075. rcf_remove_function = remove_function;
  6076. rcf_class_cl = None;
  6077. rcf_class_eager_creation = false;
  6078. rcf_hash_fields = Hashtbl.create 100;
  6079. rcf_on_getset_field = dynamic_getset_field;
  6080. rcf_on_call_field = dynamic_call_field;
  6081. rcf_handle_statics = handle_statics;
  6082. }
  6083. (*
  6084. methods as a bool option is a little laziness of my part.
  6085. None means that methods are included with normal fields;
  6086. Some(true) means collect only methods
  6087. Some(false) means collect only fields (and MethDynamic fields)
  6088. *)
  6089. let collect_fields cl (methods : bool option) (statics : bool option) =
  6090. let collected = Hashtbl.create 0 in
  6091. let collect cf acc =
  6092. if Meta.has Meta.CompilerGenerated cf.cf_meta || Meta.has Meta.SkipReflection cf.cf_meta then
  6093. acc
  6094. else match methods, cf.cf_kind with
  6095. | None, _ when not (Hashtbl.mem collected cf.cf_name) -> Hashtbl.add collected cf.cf_name true; ([cf.cf_name], cf) :: acc
  6096. | Some true, Method MethDynamic -> acc
  6097. | Some true, Method _ when not (Hashtbl.mem collected cf.cf_name) -> Hashtbl.add collected cf.cf_name true; ([cf.cf_name], cf) :: acc
  6098. | Some false, Method MethDynamic
  6099. | Some false, Var _ when not (Hashtbl.mem collected cf.cf_name) -> Hashtbl.add collected cf.cf_name true; ([cf.cf_name], cf) :: acc
  6100. | _ -> acc
  6101. in
  6102. let collect_cfs cfs acc =
  6103. let rec loop cfs acc =
  6104. match cfs with
  6105. | [] -> acc
  6106. | hd :: tl -> loop tl (collect hd acc)
  6107. in
  6108. loop cfs acc
  6109. in
  6110. let rec loop cl acc =
  6111. let acc = match statics with
  6112. | None -> collect_cfs cl.cl_ordered_fields (collect_cfs cl.cl_ordered_statics acc)
  6113. | Some true -> collect_cfs cl.cl_ordered_statics acc
  6114. | Some false -> collect_cfs cl.cl_ordered_fields acc
  6115. in
  6116. match cl.cl_super with
  6117. | None -> acc
  6118. | Some(cl,_) ->
  6119. if not (is_hxgen (TClassDecl cl)) then loop cl acc else acc
  6120. in
  6121. loop cl []
  6122. let hash f =
  6123. let h = ref 0 in
  6124. for i = 0 to String.length f - 1 do
  6125. h := !h * 223 + int_of_char (String.unsafe_get f i);
  6126. done;
  6127. if Sys.word_size = 64 then Int32.to_int (Int32.shift_right (Int32.shift_left (Int32.of_int !h) 1) 1) else !h
  6128. let hash_field ctx f pos =
  6129. let h = hash f in
  6130. (try
  6131. let f2 = Hashtbl.find ctx.rcf_hash_fields h in
  6132. if f <> f2 then ctx.rcf_gen.gcon.error ("Field conflict between " ^ f ^ " and " ^ f2) pos
  6133. with Not_found ->
  6134. Hashtbl.add ctx.rcf_hash_fields h f);
  6135. h
  6136. (* ( tf_args, switch_var ) *)
  6137. let field_type_args ctx pos =
  6138. match ctx.rcf_optimize with
  6139. | true ->
  6140. let field_name, field_hash = alloc_var "field" ctx.rcf_gen.gcon.basic.tstring, alloc_var "hash" ctx.rcf_gen.gcon.basic.tint in
  6141. [field_name, None; field_hash, None], field_hash
  6142. | false ->
  6143. let field_name = alloc_var "field" ctx.rcf_gen.gcon.basic.tstring in
  6144. [field_name, None], field_name
  6145. let hash_field_i32 ctx pos field_name =
  6146. let i = hash_field ctx field_name pos in
  6147. let i = Int32.of_int (i) in
  6148. if i < Int32.zero then
  6149. Int32.logor (Int32.logand i (Int32.of_int 0x3FFFFFFF)) (Int32.shift_left Int32.one 30)
  6150. else i
  6151. let switch_case ctx pos field_name =
  6152. match ctx.rcf_optimize with
  6153. | true ->
  6154. let i = hash_field_i32 ctx pos field_name in
  6155. { eexpr = TConst(TInt(i)); etype = ctx.rcf_gen.gcon.basic.tint; epos = pos }
  6156. | false ->
  6157. { eexpr = TConst(TString(field_name)); etype = ctx.rcf_gen.gcon.basic.tstring; epos = pos }
  6158. (*
  6159. Will implement getField / setField which will follow the following rule:
  6160. function getField(field, isStatic, throwErrors, isCheck, handleProperty, isFirst):Dynamic
  6161. {
  6162. if (isStatic)
  6163. {
  6164. switch(field)
  6165. {
  6166. case "aStaticField": return ThisClass.aStaticField;
  6167. case "aDynamicField": return ThisClass.aDynamicField;
  6168. default:
  6169. if (isFirst) return getField_d(field, isStatic, throwErrors, handleProperty, false);
  6170. if(throwErrors) throw "Field not found"; else if (isCheck) return __undefined__ else return null;
  6171. }
  6172. } else {
  6173. switch(field)
  6174. {
  6175. case "aNormalField": return this.aNormalField;
  6176. case "aBoolField": return this.aBoolField;
  6177. case "aDoubleField": return this.aDoubleField;
  6178. default: return getField_d(field, isStatic, throwErrors, isCheck);
  6179. }
  6180. }
  6181. }
  6182. function getField_d(field, isStatic, throwErrors, handleProperty, isFirst):Float
  6183. {
  6184. if (isStatic)
  6185. {
  6186. switch(field)
  6187. {
  6188. case "aDynamicField": return cast ThisClass.aDynamicField;
  6189. default: if (throwErrors) throw "Field not found"; else return null;
  6190. }
  6191. }
  6192. etc...
  6193. }
  6194. function setField(field, value, isStatic):Dynamic {}
  6195. function setField_d(field, value:Float, isStatic):Float {}
  6196. *)
  6197. let call_super ctx fn_args ret_t cf cl this_t pos =
  6198. {
  6199. eexpr = TCall({
  6200. eexpr = TField({ eexpr = TConst(TSuper); etype = this_t; epos = pos }, FInstance(cl,List.map snd cl.cl_params,cf));
  6201. etype = TFun(fun_args fn_args, ret_t);
  6202. epos = pos;
  6203. }, List.map (fun (v,_) -> mk_local v pos) fn_args);
  6204. etype = ret_t;
  6205. epos = pos;
  6206. }
  6207. let mk_string ctx str pos =
  6208. { eexpr = TConst(TString(str)); etype = ctx.rcf_gen.gcon.basic.tstring; epos = pos }
  6209. let mk_int ctx i pos =
  6210. { eexpr = TConst(TInt(Int32.of_int i)); etype = ctx.rcf_gen.gcon.basic.tint; epos = pos }
  6211. let mk_bool ctx b pos =
  6212. { eexpr = TConst(TBool(b)); etype = ctx.rcf_gen.gcon.basic.tbool; epos = pos }
  6213. let mk_throw ctx str pos = { eexpr = TThrow (mk_string ctx str pos); etype = ctx.rcf_gen.gcon.basic.tvoid; epos = pos }
  6214. let enumerate_dynamic_fields ctx cl when_found =
  6215. let gen = ctx.rcf_gen in
  6216. let basic = gen.gcon.basic in
  6217. let pos = cl.cl_pos in
  6218. let vtmp = alloc_var "i" basic.tint in
  6219. let mk_for arr len =
  6220. let t = if ctx.rcf_optimize then basic.tint else basic.tstring in
  6221. let convert_str e = if ctx.rcf_optimize then ctx.rcf_lookup_function e else e in
  6222. let tmpinc = { eexpr = TUnop(Ast.Increment, Ast.Postfix, mk_local vtmp pos); etype = basic.tint; epos = pos } in
  6223. {
  6224. eexpr = TBlock [
  6225. { eexpr = TBinop(OpAssign, mk_local vtmp pos, mk_int ctx 0 pos); etype = basic.tint; epos = pos };
  6226. {
  6227. eexpr = TWhile (
  6228. { eexpr = TBinop(Ast.OpLt, mk_local vtmp pos, len); etype = basic.tbool; epos = pos },
  6229. mk_block (when_found (convert_str { eexpr = TArray (arr, tmpinc); etype = t; epos = pos })),
  6230. Ast.NormalWhile
  6231. );
  6232. etype = basic.tvoid;
  6233. epos = pos
  6234. }
  6235. ];
  6236. etype = basic.tvoid;
  6237. epos = pos;
  6238. }
  6239. in
  6240. let this_t = TInst(cl, List.map snd cl.cl_params) in
  6241. let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
  6242. let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
  6243. { eexpr = TVar (vtmp,None); etype = basic.tvoid; epos = pos }
  6244. ::
  6245. if ctx.rcf_optimize then
  6246. [
  6247. 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);
  6248. 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);
  6249. ] else [
  6250. 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);
  6251. 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);
  6252. ]
  6253. (* *********************
  6254. Dynamic lookup
  6255. *********************
  6256. This is the behavior of standard <implements Dynamic> classes. It will replace the error throwing
  6257. if a field doesn't exists when looking it up.
  6258. In order for it to work, an implementation for hash_function must be created.
  6259. hash_function is the function to be called/inlined that will allow us to lookup the hash into a sorted array of hashes.
  6260. A binary search or linear search algorithm may be implemented. The only need is that if not found, the NegBits of
  6261. the place where it should be inserted must be returned.
  6262. *)
  6263. let abstract_dyn_lookup_implementation ctx this hash_local may_value is_float pos =
  6264. let gen = ctx.rcf_gen in
  6265. let basic = gen.gcon.basic in
  6266. let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
  6267. let a_t = if ctx.rcf_optimize then basic.tint else basic.tstring in
  6268. let hx_hashes = mk_this (gen.gmk_internal_name "hx" "hashes") (gen.gclasses.nativearray a_t) in
  6269. let hx_hashes_f = mk_this (gen.gmk_internal_name "hx" "hashes_f") (gen.gclasses.nativearray a_t) in
  6270. let hx_dynamics = mk_this (gen.gmk_internal_name "hx" "dynamics") (gen.gclasses.nativearray t_empty) in
  6271. let hx_dynamics_f = mk_this (gen.gmk_internal_name "hx" "dynamics_f") (gen.gclasses.nativearray basic.tfloat) in
  6272. let hx_length = mk_this (gen.gmk_internal_name "hx" "length") (basic.tint) in
  6273. let hx_length_f = mk_this (gen.gmk_internal_name "hx" "length_f") (basic.tint) in
  6274. let res = alloc_var "res" basic.tint in
  6275. let fst_hash, snd_hash, fst_dynamics, snd_dynamics, fst_length, snd_length =
  6276. if is_float then
  6277. hx_hashes_f, hx_hashes, hx_dynamics_f, hx_dynamics, hx_length_f, hx_length
  6278. else
  6279. hx_hashes, hx_hashes_f, hx_dynamics, hx_dynamics_f, hx_length, hx_length_f
  6280. in
  6281. let res_local = mk_local res pos in
  6282. let gte = {
  6283. eexpr = TBinop(Ast.OpGte, res_local, { eexpr = TConst(TInt(Int32.zero)); etype = basic.tint; epos = pos });
  6284. etype = basic.tbool;
  6285. epos = pos;
  6286. } in
  6287. let mk_tarray arr idx =
  6288. {
  6289. eexpr = TArray(arr, idx);
  6290. etype = gen.gclasses.nativearray_type arr.etype;
  6291. epos = pos;
  6292. }
  6293. in
  6294. let ret_t = if is_float then basic.tfloat else t_dynamic in
  6295. match may_value with
  6296. | None ->
  6297. (*
  6298. var res = lookup(this.__hx_hashes/f, hash);
  6299. if (res < 0)
  6300. {
  6301. res = lookup(this.__hx_hashes_f/_, hash);
  6302. if(res < 0)
  6303. return null;
  6304. else
  6305. return __hx_dynamics_f[res];
  6306. } else {
  6307. return __hx_dynamics[res];
  6308. }
  6309. *)
  6310. let block =
  6311. [
  6312. { eexpr = TVar(res, Some(ctx.rcf_hash_function hash_local fst_hash fst_length)); etype = basic.tvoid; epos = pos };
  6313. { eexpr = TIf(gte, mk_return (mk_tarray fst_dynamics res_local), Some({
  6314. eexpr = TBlock(
  6315. [
  6316. { eexpr = TBinop(Ast.OpAssign, res_local, ctx.rcf_hash_function hash_local snd_hash snd_length); etype = basic.tint; epos = pos };
  6317. { eexpr = TIf(gte, mk_return (mk_tarray snd_dynamics res_local), None); etype = ret_t; epos = pos }
  6318. ]);
  6319. etype = ret_t;
  6320. epos = pos;
  6321. })); etype = ret_t; epos = pos }
  6322. ] in
  6323. block
  6324. | Some value_local ->
  6325. (*
  6326. //if is not float:
  6327. //if (isNumber(value_local)) return this.__hx_setField_f(field, getNumber(value_local), false(not static));
  6328. var res = lookup(this.__hx_hashes/f, hash);
  6329. if (res >= 0)
  6330. {
  6331. return __hx_dynamics/f[res] = value_local;
  6332. } else {
  6333. res = lookup(this.__hx_hashes_f/_, hash);
  6334. if (res >= 0)
  6335. {
  6336. __hx_dynamics_f/_.splice(res,1);
  6337. __hx_hashes_f/_.splice(res,1);
  6338. }
  6339. }
  6340. __hx_hashses/_f.insert(~res, hash);
  6341. __hx_dynamics/_f.insert(~res, value_local);
  6342. return value_local;
  6343. *)
  6344. let neg_res = { eexpr = TUnop(Ast.NegBits, Ast.Prefix, res_local); etype = basic.tint; epos = pos } in
  6345. let res2 = alloc_var "res2" basic.tint in
  6346. let res2_local = mk_local res2 pos in
  6347. let gte2 = {
  6348. eexpr = TBinop(Ast.OpGte, res2_local, { eexpr = TConst(TInt(Int32.zero)); etype = basic.tint; epos = pos });
  6349. etype = basic.tbool;
  6350. epos = pos;
  6351. } in
  6352. let block =
  6353. [
  6354. { eexpr = TVar(res, Some(ctx.rcf_hash_function hash_local fst_hash fst_length)); etype = basic.tvoid; epos = pos };
  6355. {
  6356. eexpr = TIf(gte,
  6357. mk_return { eexpr = TBinop(Ast.OpAssign, mk_tarray fst_dynamics res_local, value_local); etype = value_local.etype; epos = pos },
  6358. Some({ eexpr = TBlock([
  6359. { eexpr = TVar( res2, Some(ctx.rcf_hash_function hash_local snd_hash snd_length)); etype = basic.tvoid; epos = pos };
  6360. {
  6361. eexpr = TIf(gte2, { eexpr = TBlock([
  6362. ctx.rcf_remove_function snd_hash snd_length res2_local;
  6363. ctx.rcf_remove_function snd_dynamics snd_length res2_local;
  6364. mk (TUnop(Decrement,Postfix,snd_length)) basic.tint pos
  6365. ]); etype = t_dynamic; epos = pos }, None);
  6366. etype = t_dynamic;
  6367. epos = pos;
  6368. }
  6369. ]); etype = t_dynamic; epos = pos }));
  6370. etype = t_dynamic;
  6371. epos = pos;
  6372. };
  6373. ctx.rcf_insert_function fst_hash fst_length neg_res hash_local;
  6374. ctx.rcf_insert_function fst_dynamics fst_length neg_res value_local;
  6375. mk (TUnop(Increment,Postfix,fst_length)) basic.tint pos;
  6376. mk_return value_local
  6377. ] in
  6378. block
  6379. let get_delete_field ctx cl is_dynamic =
  6380. let pos = cl.cl_pos in
  6381. let this_t = TInst(cl, List.map snd cl.cl_params) in
  6382. let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
  6383. let gen = ctx.rcf_gen in
  6384. let basic = gen.gcon.basic in
  6385. let tf_args, switch_var = field_type_args ctx pos in
  6386. let local_switch_var = mk_local switch_var pos in
  6387. let fun_type = TFun(fun_args tf_args,basic.tbool) in
  6388. let cf = mk_class_field (gen.gmk_internal_name "hx" "deleteField") fun_type false pos (Method MethNormal) [] in
  6389. let body = if is_dynamic then begin
  6390. let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
  6391. let a_t = if ctx.rcf_optimize then basic.tint else basic.tstring in
  6392. let hx_hashes = mk_this (gen.gmk_internal_name "hx" "hashes") (gen.gclasses.nativearray a_t) in
  6393. let hx_hashes_f = mk_this (gen.gmk_internal_name "hx" "hashes_f") (gen.gclasses.nativearray a_t) in
  6394. let hx_dynamics = mk_this (gen.gmk_internal_name "hx" "dynamics") (gen.gclasses.nativearray t_empty) in
  6395. let hx_dynamics_f = mk_this (gen.gmk_internal_name "hx" "dynamics_f") (gen.gclasses.nativearray basic.tfloat) in
  6396. let hx_length = mk_this (gen.gmk_internal_name "hx" "length") (basic.tint) in
  6397. let hx_length_f = mk_this (gen.gmk_internal_name "hx" "length_f") (basic.tint) in
  6398. let res = alloc_var "res" basic.tint in
  6399. let res_local = mk_local res pos in
  6400. let gte = {
  6401. eexpr = TBinop(Ast.OpGte, res_local, { eexpr = TConst(TInt(Int32.zero)); etype = basic.tint; epos = pos });
  6402. etype = basic.tbool;
  6403. epos = pos;
  6404. } in
  6405. (*
  6406. var res = lookup(this.__hx_hashes, hash);
  6407. if (res >= 0)
  6408. {
  6409. __hx_dynamics.splice(res,1);
  6410. __hx_hashes.splice(res,1);
  6411. return true;
  6412. } else {
  6413. res = lookup(this.__hx_hashes_f, hash);
  6414. if (res >= 0)
  6415. {
  6416. __hx_dynamics_f.splice(res,1);
  6417. __hx_hashes_f.splice(res,1);
  6418. return true;
  6419. }
  6420. }
  6421. return false;
  6422. *)
  6423. [
  6424. { eexpr = TVar(res,Some(ctx.rcf_hash_function local_switch_var hx_hashes hx_length)); etype = basic.tvoid; epos = pos };
  6425. {
  6426. eexpr = TIf(gte, { eexpr = TBlock([
  6427. ctx.rcf_remove_function hx_hashes hx_length res_local;
  6428. ctx.rcf_remove_function hx_dynamics hx_length res_local;
  6429. mk (TUnop(Decrement,Postfix,hx_length)) basic.tint pos;
  6430. mk_return { eexpr = TConst(TBool true); etype = basic.tbool; epos = pos }
  6431. ]); etype = t_dynamic; epos = pos }, Some({ eexpr = TBlock([
  6432. { eexpr = TBinop(Ast.OpAssign, res_local, ctx.rcf_hash_function local_switch_var hx_hashes_f hx_length_f); etype = basic.tint; epos = pos };
  6433. { eexpr = TIf(gte, { eexpr = TBlock([
  6434. ctx.rcf_remove_function hx_hashes_f hx_length_f res_local;
  6435. ctx.rcf_remove_function hx_dynamics_f hx_length_f res_local;
  6436. mk (TUnop(Decrement,Postfix,hx_length_f)) basic.tint pos;
  6437. mk_return { eexpr = TConst(TBool true); etype = basic.tbool; epos = pos }
  6438. ]); etype = t_dynamic; epos = pos }, None); etype = t_dynamic; epos = pos }
  6439. ]); etype = t_dynamic; epos = pos }));
  6440. etype = t_dynamic;
  6441. epos = pos;
  6442. };
  6443. mk_return { eexpr = TConst(TBool false); etype = basic.tbool; epos = pos }
  6444. ]
  6445. end else
  6446. [
  6447. mk_return { eexpr = TConst(TBool false); etype = basic.tbool; epos = pos }
  6448. ] in
  6449. (* create function *)
  6450. let fn =
  6451. {
  6452. tf_args = tf_args;
  6453. tf_type = basic.tbool;
  6454. tf_expr = { eexpr = TBlock(body); etype = t_dynamic; epos = pos }
  6455. } in
  6456. cf.cf_expr <- Some({ eexpr = TFunction(fn); etype = fun_type; epos = pos });
  6457. cf
  6458. let rec is_first_dynamic cl =
  6459. match cl.cl_super with
  6460. | Some(cl,_) ->
  6461. if is_some cl.cl_dynamic then false else is_first_dynamic cl
  6462. | None -> true
  6463. let is_override cl = match cl.cl_super with
  6464. | Some (cl, _) when is_hxgen (TClassDecl cl) -> true
  6465. | _ -> false
  6466. let get_args t = match follow t with
  6467. | TFun(args,ret) -> args,ret
  6468. | _ -> assert false
  6469. (* WARNING: this will only work if overloading contructors is possible on target language *)
  6470. let implement_dynamic_object_ctor ctx cl =
  6471. let rec is_side_effects_free e =
  6472. match e.eexpr with
  6473. | TConst _
  6474. | TLocal _
  6475. | TFunction _
  6476. | TTypeExpr _ ->
  6477. true
  6478. | TNew(clnew,[],params) when clnew == cl ->
  6479. List.for_all is_side_effects_free params
  6480. | TUnop(Increment,_,_)
  6481. | TUnop(Decrement,_,_)
  6482. | TBinop(OpAssign,_,_)
  6483. | TBinop(OpAssignOp _,_,_) ->
  6484. false
  6485. | TUnop(_,_,e) ->
  6486. is_side_effects_free e
  6487. | TArray(e1,e2)
  6488. | TBinop(_,e1,e2) ->
  6489. is_side_effects_free e1 && is_side_effects_free e2
  6490. | TIf(cond,e1,Some e2) ->
  6491. is_side_effects_free cond && is_side_effects_free e1 && is_side_effects_free e2
  6492. | TField(e,_)
  6493. | TParenthesis e | TMeta(_,e) -> is_side_effects_free e
  6494. | TArrayDecl el -> List.for_all is_side_effects_free el
  6495. | TCast(e,_) -> is_side_effects_free e
  6496. | _ -> false
  6497. in
  6498. let pos = cl.cl_pos in
  6499. let gen = ctx.rcf_gen in
  6500. let basic = gen.gcon.basic in
  6501. let hasht = if ctx.rcf_optimize then basic.tint else basic.tstring in
  6502. let hashes_field = gen.gmk_internal_name "hx" "hashes", gen.gclasses.nativearray hasht in
  6503. let hashes_f_field = gen.gmk_internal_name "hx" "hashes_f", gen.gclasses.nativearray hasht in
  6504. let dynamics_field = gen.gmk_internal_name "hx" "dynamics", gen.gclasses.nativearray t_empty in
  6505. let dynamics_f_field = gen.gmk_internal_name "hx" "dynamics_f", gen.gclasses.nativearray basic.tfloat in
  6506. let fields =
  6507. [
  6508. hashes_field;
  6509. dynamics_field;
  6510. hashes_f_field;
  6511. dynamics_f_field;
  6512. ] in
  6513. let hashes_var = alloc_var (fst hashes_field) (snd hashes_field) in
  6514. let hashes_f_var = alloc_var (fst hashes_f_field) (snd hashes_f_field) in
  6515. let tf_args = [
  6516. hashes_var, None;
  6517. alloc_var (fst dynamics_field) (snd dynamics_field), None;
  6518. hashes_f_var, None;
  6519. alloc_var (fst dynamics_f_field) (snd dynamics_f_field), None;
  6520. ] in
  6521. let this = { eexpr = TConst TThis; etype = TInst(cl, List.map snd cl.cl_params); epos = pos } in
  6522. let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
  6523. let fun_t = TFun(fun_args tf_args,basic.tvoid) in
  6524. let ctor = mk_class_field "new" fun_t true pos (Method MethNormal) [] in
  6525. ctor.cf_expr <- Some(
  6526. {
  6527. eexpr = TFunction({
  6528. tf_args = tf_args;
  6529. tf_type = basic.tvoid;
  6530. tf_expr =
  6531. {
  6532. eexpr = TBlock(
  6533. List.map (fun (v,_) ->
  6534. { eexpr = TBinop(Ast.OpAssign, mk_this v.v_name v.v_type, mk_local v pos); etype = v.v_type; epos = pos }
  6535. ) tf_args
  6536. @
  6537. [
  6538. 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;
  6539. 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;
  6540. ]
  6541. );
  6542. etype = basic.tvoid;
  6543. epos = pos
  6544. }
  6545. });
  6546. etype = fun_t;
  6547. epos = pos
  6548. });
  6549. add_constructor cl ctor;
  6550. (* default ctor also *)
  6551. let ctor = mk_class_field "new" (TFun([],basic.tvoid)) false pos (Method MethNormal) [] in
  6552. ctor.cf_expr <- Some {
  6553. eexpr = TFunction {
  6554. tf_type = basic.tvoid;
  6555. tf_args = [];
  6556. tf_expr = {
  6557. eexpr = TBlock(List.map (fun (f,t) ->
  6558. { eexpr = TBinop(Ast.OpAssign, mk_this f t,{ eexpr = TCall(mk_local v_nativearray pos, []); etype = t; epos = pos; }); etype = t; epos = pos }
  6559. ) fields);
  6560. etype = basic.tvoid;
  6561. epos = pos;
  6562. }
  6563. };
  6564. etype = ctor.cf_type;
  6565. epos = pos;
  6566. };
  6567. add_constructor cl ctor;
  6568. (* and finally we will return a function that transforms a TObjectDecl into a new DynamicObject() call *)
  6569. let rec loop objdecl acc acc_f =
  6570. match objdecl with
  6571. | [] -> acc,acc_f
  6572. | (name,expr) :: tl ->
  6573. let real_t = gen.greal_type expr.etype in
  6574. match follow expr.etype with
  6575. | TInst ( { cl_path = ["haxe"], "Int64" }, [] ) ->
  6576. loop tl ((name, gen.ghandle_cast t_dynamic real_t expr) :: acc) acc_f
  6577. | _ ->
  6578. if like_float real_t && not (like_i64 real_t) then
  6579. loop tl acc ((name, gen.ghandle_cast basic.tfloat real_t expr) :: acc_f)
  6580. else
  6581. loop tl ((name, gen.ghandle_cast t_dynamic real_t expr) :: acc) acc_f
  6582. in
  6583. let may_hash_field s =
  6584. if ctx.rcf_optimize then begin
  6585. (* let hash_field ctx f pos = *)
  6586. { eexpr = TConst(TInt (hash_field_i32 ctx pos s)); etype = basic.tint; epos = pos }
  6587. end else begin
  6588. { eexpr = TConst(TString s); etype = basic.tstring; epos = pos }
  6589. end
  6590. in
  6591. let do_objdecl e objdecl =
  6592. let exprs_before = ref [] in
  6593. let rec change_exprs decl acc = match decl with
  6594. | (name,expr) :: tl ->
  6595. if is_side_effects_free expr then
  6596. change_exprs tl ((name,expr) :: acc)
  6597. else begin
  6598. let var = mk_temp gen "odecl" expr.etype in
  6599. exprs_before := { eexpr = TVar(var,Some expr); etype = basic.tvoid; epos = expr.epos } :: !exprs_before;
  6600. change_exprs tl ((name,mk_local var expr.epos) :: acc)
  6601. end
  6602. | [] -> acc
  6603. in
  6604. let objdecl = change_exprs objdecl [] in
  6605. let odecl, odecl_f = loop objdecl [] [] in
  6606. let changed_expr = List.map (fun (s,e) -> (may_hash_field s,e)) in
  6607. let odecl, odecl_f = changed_expr odecl, changed_expr odecl_f in
  6608. let sort_fn (e1,_) (e2,_) =
  6609. match e1.eexpr, e2.eexpr with
  6610. | TConst(TInt i1), TConst(TInt i2) -> compare i1 i2
  6611. | TConst(TString s1), TConst(TString s2) -> compare s1 s2
  6612. | _ -> assert false
  6613. in
  6614. let odecl, odecl_f = List.sort sort_fn odecl, List.sort sort_fn odecl_f in
  6615. let ret = {
  6616. e with eexpr = TNew(cl,[],
  6617. [
  6618. mk_nativearray_decl gen hasht (List.map fst odecl) pos;
  6619. mk_nativearray_decl gen t_empty (List.map snd odecl) pos;
  6620. mk_nativearray_decl gen hasht (List.map fst odecl_f) pos;
  6621. mk_nativearray_decl gen basic.tfloat (List.map snd odecl_f) pos;
  6622. ]);
  6623. } in
  6624. match !exprs_before with
  6625. | [] -> ret
  6626. | block ->
  6627. {
  6628. eexpr = TBlock(List.rev block @ [ret]);
  6629. etype = ret.etype;
  6630. epos = ret.epos;
  6631. }
  6632. in
  6633. do_objdecl
  6634. let implement_dynamics ctx cl =
  6635. let pos = cl.cl_pos in
  6636. let is_override = is_override cl in
  6637. if is_some cl.cl_dynamic then begin
  6638. if is_first_dynamic cl then begin
  6639. (*
  6640. * add hx_hashes, hx_hashes_f, hx_dynamics, hx_dynamics_f to class
  6641. * implement hx_deleteField
  6642. *)
  6643. let gen = ctx.rcf_gen in
  6644. let basic = gen.gcon.basic in
  6645. let hasht = if ctx.rcf_optimize then basic.tint else basic.tstring in
  6646. let new_fields =
  6647. [
  6648. mk_class_field (gen.gmk_internal_name "hx" "hashes") (gen.gclasses.nativearray hasht) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
  6649. mk_class_field (gen.gmk_internal_name "hx" "dynamics") (gen.gclasses.nativearray t_empty) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
  6650. mk_class_field (gen.gmk_internal_name "hx" "hashes_f") (gen.gclasses.nativearray hasht) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
  6651. mk_class_field (gen.gmk_internal_name "hx" "dynamics_f") (gen.gclasses.nativearray basic.tfloat) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
  6652. ] in
  6653. (if cl.cl_path <> (["haxe"; "lang"], "DynamicObject") then
  6654. 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
  6655. );
  6656. let delete = get_delete_field ctx cl true in
  6657. let new_fields = new_fields @ [
  6658. mk_class_field (gen.gmk_internal_name "hx" "length") (basic.tint) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
  6659. mk_class_field (gen.gmk_internal_name "hx" "length_f") (basic.tint) false pos (Var { v_read = AccNormal; v_write = AccNormal }) [];
  6660. delete;
  6661. ] in
  6662. List.iter (fun cf ->
  6663. cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields
  6664. ) new_fields;
  6665. (*
  6666. let rec last_ctor cl =
  6667. match cl.cl_constructor with
  6668. | None -> (match cl.cl_super with | None -> None | Some (cl,_) -> last_ctor cl)
  6669. | Some c -> Some c
  6670. in
  6671. *)
  6672. (*
  6673. 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
  6674. *)
  6675. (*
  6676. Now we need to add their initialization.
  6677. This will consist of different parts:
  6678. Check if there are constructors. If not, create one and add initialization to it (calling super, ok)
  6679. If there are, add as first statement (or second if there is a super() call in the first)
  6680. If class has @:dynamicObject meta, also create another new() class with its parameters as constructor arguments
  6681. *)
  6682. cl.cl_ordered_fields <- cl.cl_ordered_fields @ new_fields;
  6683. if is_override then cl.cl_overrides <- delete :: cl.cl_overrides
  6684. end
  6685. end else if not is_override then begin
  6686. let delete = get_delete_field ctx cl false in
  6687. cl.cl_ordered_fields <- cl.cl_ordered_fields @ [delete];
  6688. cl.cl_fields <- PMap.add delete.cf_name delete cl.cl_fields
  6689. end
  6690. let implement_create_empty ctx cl =
  6691. let gen = ctx.rcf_gen in
  6692. let basic = gen.gcon.basic in
  6693. let pos = cl.cl_pos in
  6694. let is_override = is_override cl in
  6695. let tparams = List.map (fun _ -> t_empty) cl.cl_params in
  6696. let create =
  6697. let arr = alloc_var "arr" (basic.tarray t_dynamic) in
  6698. let tf_args = [ arr, None ] in
  6699. let t = TFun(fun_args tf_args, t_dynamic) in
  6700. let cf = mk_class_field (gen.gmk_internal_name "hx" "create") t false pos (Method MethNormal) [] in
  6701. let i = ref 0 in
  6702. let arr_local = mk_local arr pos in
  6703. let ctor = if is_some cl.cl_constructor then cl.cl_constructor else get_last_ctor cl in
  6704. let params = match ctor with
  6705. | None -> []
  6706. | Some ctor ->
  6707. List.map (fun (n,_,t) ->
  6708. let old = !i in
  6709. incr i;
  6710. {
  6711. eexpr = TArray(arr_local, { eexpr = TConst(TInt (Int32.of_int old)); etype = basic.tint; epos = pos } );
  6712. etype = t_dynamic;
  6713. epos = pos
  6714. }
  6715. ) ( fst ( get_fun ctor.cf_type ) )
  6716. in
  6717. let expr = mk_return {
  6718. eexpr = TNew(cl, tparams, params);
  6719. etype = TInst(cl, tparams);
  6720. epos = pos
  6721. } in
  6722. let fn = {
  6723. eexpr = TFunction({
  6724. tf_args = tf_args;
  6725. tf_type = t_dynamic;
  6726. tf_expr = mk_block expr
  6727. });
  6728. etype = t;
  6729. epos = pos
  6730. } in
  6731. cf.cf_expr <- Some fn;
  6732. cf
  6733. in
  6734. let create_empty =
  6735. let t = TFun([],t_dynamic) in
  6736. let cf = mk_class_field (gen.gmk_internal_name "hx" "createEmpty") t false pos (Method MethNormal) [] in
  6737. let fn = {
  6738. eexpr = TFunction({
  6739. tf_args = [];
  6740. tf_type = t_dynamic;
  6741. tf_expr = mk_block (mk_return ( gen.gtools.rf_create_empty cl tparams pos ))
  6742. });
  6743. etype = t;
  6744. epos = pos
  6745. } in
  6746. cf.cf_expr <- Some fn;
  6747. cf
  6748. in
  6749. (* if rcf_handle_statics is false, there is no reason to make createEmpty/create not be static *)
  6750. if ctx.rcf_handle_statics then begin
  6751. cl.cl_ordered_fields <- cl.cl_ordered_fields @ [create_empty; create];
  6752. cl.cl_fields <- PMap.add create_empty.cf_name create_empty cl.cl_fields;
  6753. cl.cl_fields <- PMap.add create.cf_name create cl.cl_fields;
  6754. if is_override then begin
  6755. cl.cl_overrides <- create_empty :: create :: cl.cl_overrides
  6756. end
  6757. end else begin
  6758. cl.cl_ordered_statics <- cl.cl_ordered_statics @ [create_empty; create];
  6759. cl.cl_statics <- PMap.add create_empty.cf_name create_empty cl.cl_statics;
  6760. cl.cl_statics <- PMap.add create.cf_name create cl.cl_statics
  6761. end
  6762. (*
  6763. Implements:
  6764. __hx_lookupField(field:String, throwErrors:Bool, isCheck:Bool, handleProperties:Bool, isFirst:Bool):Dynamic
  6765. __hx_lookupField_f(field:String, throwErrors:Bool, handleProperties:Bool, isFirst:Bool):Float
  6766. __hx_lookupSetField(field:String, value:Dynamic, handleProperties:Bool, isFirst:Bool):Dynamic;
  6767. __hx_lookupSetField(field:String, value:Float, handleProperties:Bool, isFirst:Bool):Float;
  6768. *)
  6769. let implement_final_lookup ctx cl =
  6770. let gen = ctx.rcf_gen in
  6771. let basic = gen.gcon.basic in
  6772. let pos = cl.cl_pos in
  6773. let is_override = is_override cl in
  6774. let this = { eexpr = TConst(TThis); etype = TInst(cl, List.map snd cl.cl_params); epos = pos } in
  6775. (*
  6776. this function will create the class fields and call callback for each version
  6777. callback : is_float fields_args switch_var throw_errors_option is_check_option value_option : texpr list
  6778. *)
  6779. let create_cfs is_dynamic callback =
  6780. let create_cf is_float is_set =
  6781. let name = gen.gmk_internal_name "hx" ( (if is_set then "lookupSetField" else "lookupField") ^ (if is_float then "_f" else "") ) in
  6782. let field_args, switch_var = field_type_args ctx pos in
  6783. let ret_t = if is_float then basic.tfloat else t_dynamic in
  6784. let tf_args, throw_errors_opt =
  6785. if is_set then
  6786. field_args, None
  6787. else
  6788. let v = alloc_var "throwErrors" basic.tbool in
  6789. field_args @ [v,None], Some v
  6790. in
  6791. let tf_args, is_check_opt =
  6792. if is_set || is_float then
  6793. tf_args, None
  6794. else
  6795. let v = alloc_var "isCheck" basic.tbool in
  6796. tf_args @ [v,None], Some v
  6797. in
  6798. let tf_args, value_opt =
  6799. if not is_set then
  6800. tf_args, None
  6801. else
  6802. let v = alloc_var "value" ret_t in
  6803. field_args @ [v,None], Some v
  6804. in
  6805. let fun_t = TFun(fun_args tf_args, ret_t) in
  6806. let cf = mk_class_field name fun_t false pos (Method MethNormal) [] in
  6807. let block = callback is_float field_args switch_var throw_errors_opt is_check_opt value_opt in
  6808. let block = if not is_set then let tl = begin
  6809. let throw_errors_local = mk_local (get throw_errors_opt) pos in
  6810. let mk_check_throw msg =
  6811. {
  6812. eexpr = TIf(throw_errors_local, mk_throw ctx msg pos, Some (mk_return (null ret_t pos)));
  6813. etype = ret_t;
  6814. epos = pos
  6815. } in
  6816. let mk_may_check_throw msg = if is_dynamic then mk_return (null ret_t pos) else mk_check_throw msg in
  6817. if is_float then begin
  6818. [
  6819. mk_may_check_throw "Field not found or incompatible field type.";
  6820. ]
  6821. end else begin
  6822. let is_check_local = mk_local (get is_check_opt) pos in
  6823. [
  6824. {
  6825. eexpr = TIf(is_check_local, mk_return (undefined pos), Some( mk_may_check_throw "Field not found." ));
  6826. etype = ret_t;
  6827. epos = pos;
  6828. }
  6829. ]
  6830. end
  6831. end in block @ tl else block in
  6832. cf.cf_expr <- Some(
  6833. {
  6834. eexpr = TFunction({
  6835. tf_args = tf_args;
  6836. tf_type = ret_t;
  6837. tf_expr = { eexpr = TBlock(block); etype = ret_t; epos = pos }
  6838. });
  6839. etype = fun_t;
  6840. epos = pos
  6841. }
  6842. );
  6843. cf
  6844. in
  6845. let cfs =
  6846. [
  6847. create_cf false false;
  6848. create_cf true false;
  6849. create_cf false true;
  6850. create_cf true true
  6851. ] in
  6852. cl.cl_ordered_fields <- cl.cl_ordered_fields @ cfs;
  6853. List.iter (fun cf ->
  6854. cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields;
  6855. if is_override then cl.cl_overrides <- cf :: cl.cl_overrides
  6856. ) cfs
  6857. in
  6858. if is_some cl.cl_dynamic then begin
  6859. (* let abstract_dyn_lookup_implementation ctx this hash_local may_value is_float pos = *)
  6860. (* callback : is_float fields_args switch_var throw_errors_option is_check_option value_option : texpr list *)
  6861. if is_first_dynamic cl then
  6862. create_cfs true (fun is_float fields_args switch_var _ _ value_opt ->
  6863. abstract_dyn_lookup_implementation ctx this (mk_local switch_var pos) (Option.map (fun v -> mk_local v pos) value_opt) is_float pos
  6864. )
  6865. end else if not is_override then begin
  6866. create_cfs false (fun is_float fields_args switch_var _ _ value_opt ->
  6867. match value_opt with
  6868. | None -> (* is not set *)
  6869. []
  6870. | Some _ -> (* is set *)
  6871. if is_float then
  6872. [ mk_throw ctx "Cannot access field for writing or incompatible type." pos ]
  6873. else
  6874. [ mk_throw ctx "Cannot access field for writing." pos ]
  6875. )
  6876. end
  6877. (* *)
  6878. let implement_get_set ctx cl =
  6879. let gen = ctx.rcf_gen in
  6880. let mk_cfield is_set is_float =
  6881. let pos = cl.cl_pos in
  6882. let basic = ctx.rcf_gen.gcon.basic in
  6883. let tf_args, switch_var = field_type_args ctx pos in
  6884. let field_args = tf_args in
  6885. let local_switch_var = { eexpr = TLocal(switch_var); etype = switch_var.v_type; epos = pos } in
  6886. let is_static = alloc_var "isStatic" basic.tbool in
  6887. let is_static_local = { eexpr = TLocal(is_static); etype = basic.tbool; epos = pos } in
  6888. let handle_prop = alloc_var "handleProperties" basic.tbool in
  6889. let handle_prop_local = mk_local handle_prop pos in
  6890. let this = { eexpr = TConst TThis; etype = TInst(cl, List.map snd cl.cl_params); epos = pos } in
  6891. let mk_this_call_raw name fun_t params =
  6892. { eexpr = TCall( { (mk_field_access gen this name pos) with etype = fun_t; }, params ); etype = snd (get_args fun_t); epos = pos }
  6893. in
  6894. let tf_args = if ctx.rcf_handle_statics then tf_args @ [is_static, None] else tf_args in
  6895. let fun_type = ref (TFun([], basic.tvoid)) in
  6896. let fun_name = ctx.rcf_gen.gmk_internal_name "hx" ( (if is_set then "setField" else "getField") ^ (if is_float then "_f" else "") ) in
  6897. let cfield = mk_class_field fun_name !fun_type false pos (Method MethNormal) [] in
  6898. let maybe_cast e = e in
  6899. let t = TInst(cl, List.map snd cl.cl_params) in
  6900. (* if it's not latest hxgen class -> check super *)
  6901. let mk_do_default args do_default =
  6902. match cl.cl_super with
  6903. | None -> fun () -> maybe_cast (do_default ())
  6904. | Some (super, sparams) when not (is_hxgen (TClassDecl super)) ->
  6905. fun () -> maybe_cast (do_default ())
  6906. | _ ->
  6907. fun () ->
  6908. mk_return {
  6909. eexpr = TCall(
  6910. { eexpr = TField({ eexpr = TConst TSuper; etype = t; epos = pos }, FInstance(cl, List.map snd cl.cl_params, cfield)); etype = !fun_type; epos = pos },
  6911. (List.map (fun (v,_) -> mk_local v pos) args) );
  6912. etype = if is_float then basic.tfloat else t_dynamic;
  6913. epos = pos;
  6914. };
  6915. in
  6916. (* if it is set function, there are some different set fields to do *)
  6917. let do_default, do_default_static , do_field, tf_args = if is_set then begin
  6918. let value_var = alloc_var "value" (if is_float then basic.tfloat else t_dynamic) in
  6919. let value_local = { eexpr = TLocal(value_var); etype = value_var.v_type; epos = pos } in
  6920. let tf_args = tf_args @ [value_var,None; handle_prop, None; ] in
  6921. let lookup_name = gen.gmk_internal_name "hx" ("lookupSetField" ^ if is_float then "_f" else "") in
  6922. let do_default =
  6923. fun () ->
  6924. 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 ] ))
  6925. in
  6926. let do_field cf cf_type is_static =
  6927. 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
  6928. let this = if is_static then mk_classtype_access cl pos else { eexpr = TConst(TThis); etype = t; epos = pos } in
  6929. let value_local = if is_float then match follow cf_type with
  6930. | TInst({ cl_kind = KTypeParameter _ }, _) ->
  6931. mk_cast t_dynamic value_local
  6932. | _ ->
  6933. value_local
  6934. else
  6935. value_local
  6936. in
  6937. let ret =
  6938. {
  6939. eexpr = TBlock([
  6940. {
  6941. eexpr = TBinop(Ast.OpAssign,
  6942. get_field this,
  6943. mk_cast cf_type value_local);
  6944. etype = cf_type;
  6945. epos = pos;
  6946. };
  6947. mk_return value_local
  6948. ]);
  6949. etype = cf_type;
  6950. epos = pos;
  6951. } in
  6952. match cf.cf_kind with
  6953. | Var { v_write = AccCall } ->
  6954. let bl =
  6955. [
  6956. mk_this_call_raw ("set_" ^ cf.cf_name) (TFun(["value",false,cf.cf_type], cf.cf_type)) [ value_local ];
  6957. mk_return value_local
  6958. ] in
  6959. if Type.is_extern_field cf then
  6960. { eexpr = TBlock bl; etype = value_local.etype; epos = pos }
  6961. else
  6962. {
  6963. eexpr = TIf(
  6964. handle_prop_local,
  6965. { eexpr = TBlock bl; etype = value_local.etype; epos = pos },
  6966. Some ret);
  6967. etype = value_local.etype;
  6968. epos = pos;
  6969. }
  6970. | _ ->
  6971. ret
  6972. in
  6973. (mk_do_default tf_args do_default, do_default, do_field, tf_args)
  6974. end else begin
  6975. (* (field, isStatic, throwErrors, isCheck):Dynamic *)
  6976. let throw_errors = alloc_var "throwErrors" basic.tbool in
  6977. let throw_errors_local = mk_local throw_errors pos in
  6978. let do_default, tf_args = if not is_float then begin
  6979. let is_check = alloc_var "isCheck" basic.tbool in
  6980. let is_check_local = mk_local is_check pos in
  6981. let tf_args = tf_args @ [ throw_errors,None; ] in
  6982. (* default: if (isCheck) return __undefined__ else if(throwErrors) throw "Field not found"; else return null; *)
  6983. let lookup_name = gen.gmk_internal_name "hx" "lookupField" in
  6984. let do_default =
  6985. fun () ->
  6986. 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; ] ))
  6987. in
  6988. (do_default, tf_args @ [ is_check,None; handle_prop,None; ])
  6989. end else begin
  6990. let tf_args = tf_args @ [ throw_errors,None; ] in
  6991. let lookup_name = gen.gmk_internal_name "hx" "lookupField_f" in
  6992. let do_default =
  6993. fun () ->
  6994. 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; ] ))
  6995. in
  6996. (do_default, tf_args @ [ handle_prop,None; ])
  6997. end in
  6998. let get_field cf cf_type ethis cl name =
  6999. match cf.cf_kind with
  7000. | Var { v_read = AccCall } when Type.is_extern_field cf ->
  7001. mk_return (mk_this_call_raw ("get_" ^ cf.cf_name) (TFun(["value",false,cf.cf_type], cf.cf_type)) [ ])
  7002. | Var { v_read = AccCall } ->
  7003. {
  7004. eexpr = TIf(
  7005. handle_prop_local,
  7006. mk_return (mk_this_call_raw ("get_" ^ cf.cf_name) (TFun(["value",false,cf.cf_type], cf.cf_type)) [ ]),
  7007. Some { eexpr = TField (ethis, FInstance(cl, List.map snd cl.cl_params, cf)); etype = cf_type; epos = pos }
  7008. );
  7009. etype = cf_type;
  7010. epos = pos;
  7011. }
  7012. | Var _
  7013. | Method MethDynamic -> { eexpr = TField (ethis, FInstance(cl,List.map snd cl.cl_params,cf)); etype = cf_type; epos = pos }
  7014. | _ ->
  7015. { eexpr = TField (this, FClosure(Some (cl,[]), cf)); etype = cf_type; epos = pos } (* TODO: FClosure change *)
  7016. in
  7017. let do_field cf cf_type static =
  7018. let this = if static then mk_classtype_access cl pos else { eexpr = TConst(TThis); etype = t; epos = pos } in
  7019. match is_float, follow cf_type with
  7020. | true, TInst( { cl_kind = KTypeParameter _ }, _ ) ->
  7021. mk_return (mk_cast basic.tfloat (mk_cast t_dynamic (get_field cf cf_type this cl cf.cf_name)))
  7022. | _ ->
  7023. mk_return (maybe_cast (get_field cf cf_type this cl cf.cf_name ))
  7024. in
  7025. (mk_do_default tf_args do_default, do_default, do_field, tf_args)
  7026. end in
  7027. let get_fields static =
  7028. let ret = collect_fields cl ( if is_float || is_set then Some (false) else None ) (Some static) in
  7029. let ret = if is_set then List.filter (fun (_,cf) ->
  7030. match cf.cf_kind with
  7031. (* | Var { v_write = AccNever } -> false *)
  7032. | _ -> not (Meta.has Meta.ReadOnly cf.cf_meta)) ret
  7033. else
  7034. List.filter (fun (_,cf) ->
  7035. match cf.cf_kind with
  7036. (* | Var { v_read = AccNever } -> false *)
  7037. | _ -> true) ret in
  7038. if is_float then
  7039. List.filter (fun (_,cf) -> (* TODO: maybe really apply_params in cf.cf_type. The benefits would be limited, though *)
  7040. match follow (ctx.rcf_gen.greal_type (ctx.rcf_gen.gfollow#run_f cf.cf_type)) with
  7041. | TDynamic _ | TMono _
  7042. | TInst ({ cl_kind = KTypeParameter _ }, _) -> true
  7043. | t when like_float t && not (like_i64 t) -> true
  7044. | _ -> false
  7045. ) ret
  7046. else
  7047. (* dynamic will always contain all references *)
  7048. ret
  7049. in
  7050. (* now we have do_default, do_field and tf_args *)
  7051. (* so create the switch expr *)
  7052. 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 );
  7053. let has_fields = ref false in
  7054. let mk_switch static =
  7055. let fields = get_fields static in
  7056. let fields = List.filter (fun (_, cf) -> match is_set, cf.cf_kind with
  7057. | true, Var { v_write = AccCall } -> true
  7058. | false, Var { v_read = AccCall } -> true
  7059. | _ -> not (Type.is_extern_field cf)) fields
  7060. in
  7061. (if fields <> [] then has_fields := true);
  7062. let cases = List.map (fun (names, cf) ->
  7063. (if names = [] then assert false);
  7064. (List.map (switch_case ctx pos) names, do_field cf cf.cf_type static)
  7065. ) fields in
  7066. let default = Some(if static then do_default_static() else do_default()) in
  7067. { eexpr = TSwitch(local_switch_var, cases, default); etype = basic.tvoid; epos = pos }
  7068. in
  7069. let content = if ctx.rcf_handle_statics then
  7070. mk_block { eexpr = TIf(is_static_local, mk_switch true, Some(mk_switch false)); etype = basic.tvoid; epos = pos }
  7071. else
  7072. mk_block (mk_switch false)
  7073. in
  7074. let is_override = match cl.cl_super with
  7075. | Some (cl, _) when is_hxgen (TClassDecl cl) -> true
  7076. | _ -> false
  7077. in
  7078. if !has_fields || (not is_override) then begin
  7079. let func =
  7080. {
  7081. tf_args = tf_args;
  7082. tf_type = if is_float then basic.tfloat else t_dynamic;
  7083. tf_expr = content;
  7084. } in
  7085. let func = { eexpr = TFunction(func); etype = !fun_type; epos = pos } in
  7086. cfield.cf_type <- !fun_type;
  7087. cfield.cf_expr <- Some func;
  7088. cl.cl_ordered_fields <- cl.cl_ordered_fields @ [cfield];
  7089. cl.cl_fields <- PMap.add fun_name cfield cl.cl_fields;
  7090. (if is_override then cl.cl_overrides <- cfield :: cl.cl_overrides)
  7091. end else ()
  7092. in
  7093. (if ctx.rcf_float_special_case then mk_cfield true true);
  7094. mk_cfield true false;
  7095. mk_cfield false false;
  7096. (if ctx.rcf_float_special_case then mk_cfield false true)
  7097. let mk_field_access_r ctx pos local field is_float is_static throw_errors set_option =
  7098. let is_set = is_some set_option in
  7099. let gen = ctx.rcf_gen in
  7100. let basic = gen.gcon.basic in
  7101. let fun_name = ctx.rcf_gen.gmk_internal_name "hx" ( (if is_set then "setField" else "getField") ^ (if is_float then "_f" else "") ) in
  7102. let tf_args, _ = field_type_args ctx pos in
  7103. let tf_args, args = fun_args tf_args, field in
  7104. let rett = if is_float then basic.tfloat else t_dynamic in
  7105. let tf_args, args = if ctx.rcf_handle_statics then tf_args @ [ "isStatic", false, basic.tbool ], args @ [is_static] else tf_args, args in
  7106. let tf_args, args = if is_set then tf_args @ [ "setVal", false, rett ], args @ [get set_option] else tf_args, args in
  7107. let tf_args, args = tf_args @ [ "throwErrors",false,basic.tbool ], args @ [throw_errors] in
  7108. 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
  7109. let tf_args, args = tf_args @ [ "handleProperties",false,basic.tbool; ], args @ [ mk_bool ctx false pos; ] in
  7110. {
  7111. eexpr = TCall(
  7112. { (mk_field_access gen local fun_name pos) with etype = TFun(tf_args, rett) },
  7113. args);
  7114. etype = rett;
  7115. epos = pos;
  7116. }
  7117. let implement_fields ctx cl =
  7118. (*
  7119. implement two kinds of fields get:
  7120. classFields
  7121. generic 'fields': receives a parameter isInstance
  7122. will receive an Array<String> and start pushing the fields into it.
  7123. //add all common fields
  7124. if(isInstance)
  7125. {
  7126. //add methods
  7127. } else {
  7128. super.fields(isInstance, array);
  7129. }
  7130. *)
  7131. let gen = ctx.rcf_gen in
  7132. let basic = gen.gcon.basic in
  7133. let pos = cl.cl_pos in
  7134. (*
  7135. let rec has_no_dynamic cl =
  7136. if is_some cl.cl_dynamic then
  7137. false
  7138. else match cl.cl_super with
  7139. | None -> true
  7140. | Some(cl,_) -> has_no_dynamic cl
  7141. in
  7142. *)
  7143. (* Type.getClassFields() *)
  7144. if ctx.rcf_handle_statics then begin
  7145. let name = gen.gmk_internal_name "hx" "classFields" in
  7146. let v_base_arr = alloc_var "baseArr" (basic.tarray basic.tstring) in
  7147. let base_arr = mk_local v_base_arr pos in
  7148. let tf_args = [v_base_arr,None] in
  7149. let t = TFun(fun_args tf_args, basic.tvoid) in
  7150. let cf = mk_class_field name t false pos (Method MethNormal) [] in
  7151. cl.cl_ordered_fields <- cl.cl_ordered_fields @ [cf];
  7152. cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields;
  7153. (if is_override cl then cl.cl_overrides <- cf :: cl.cl_overrides);
  7154. (*
  7155. var newarr = ["field1", "field2"] ...;
  7156. *)
  7157. let fields = collect_fields cl None (Some true) in
  7158. let mk_push value =
  7159. { 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 }
  7160. in
  7161. let new_arr_contents =
  7162. {
  7163. eexpr = TBlock(
  7164. List.map (fun (_,cf) -> mk_push { eexpr = TConst(TString(cf.cf_name)); etype = basic.tstring; epos = pos }) fields
  7165. );
  7166. etype = basic.tvoid;
  7167. epos = pos
  7168. } in
  7169. let expr = new_arr_contents in
  7170. let fn =
  7171. {
  7172. tf_args = tf_args;
  7173. tf_type = basic.tvoid;
  7174. tf_expr = mk_block expr
  7175. } in
  7176. cf.cf_expr <- Some { eexpr = TFunction(fn); etype = t; epos = pos }
  7177. end;
  7178. let fields =
  7179. (*
  7180. function __hx_fields(baseArr:Array<String>, isInstanceFields:Bool)
  7181. {
  7182. //add all variable fields
  7183. //then:
  7184. if (isInstanceFields)
  7185. {
  7186. //add all method fields as well
  7187. } else {
  7188. super.__hx_fields(baseArr, isInstanceFields);
  7189. }
  7190. }
  7191. *)
  7192. let name = gen.gmk_internal_name "hx" "getFields" in
  7193. let v_base_arr, v_is_inst = alloc_var "baseArr" (basic.tarray basic.tstring), alloc_var "isInstanceFields" basic.tbool in
  7194. let base_arr, is_inst = mk_local v_base_arr pos, mk_local v_is_inst pos in
  7195. let tf_args = (v_base_arr,None) :: (if ctx.rcf_handle_statics then [v_is_inst, None] else []) in
  7196. let t = TFun(fun_args tf_args, basic.tvoid) in
  7197. let cf = mk_class_field name t false pos (Method MethNormal) [] in
  7198. let mk_push value =
  7199. { 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 }
  7200. in
  7201. let has_value = ref false in
  7202. let map_fields =
  7203. List.map (fun (_,cf) ->
  7204. match cf.cf_kind with
  7205. | Var _
  7206. | Method MethDynamic when not (List.memq cf cl.cl_overrides) ->
  7207. has_value := true;
  7208. mk_push { eexpr = TConst(TString(cf.cf_name)); etype = basic.tstring; epos = pos }
  7209. | _ -> null basic.tvoid pos
  7210. )
  7211. in
  7212. (*
  7213. if it is first_dynamic, then we need to enumerate the dynamic fields
  7214. *)
  7215. let if_not_inst = if is_some cl.cl_dynamic && is_first_dynamic cl then begin
  7216. has_value := true;
  7217. Some (enumerate_dynamic_fields ctx cl mk_push)
  7218. end else
  7219. None
  7220. in
  7221. let if_not_inst = if is_override cl then
  7222. Some(
  7223. {
  7224. eexpr = TBlock(
  7225. (if is_some if_not_inst then get if_not_inst else []) @
  7226. [{
  7227. eexpr = TCall(
  7228. { 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 },
  7229. base_arr :: (if ctx.rcf_handle_statics then [is_inst] else [])
  7230. );
  7231. etype = basic.tvoid;
  7232. epos = pos
  7233. }]
  7234. );
  7235. etype = basic.tvoid;
  7236. epos = pos
  7237. }
  7238. ) else if is_some if_not_inst then
  7239. Some({ eexpr = TBlock(get if_not_inst); etype = basic.tvoid; epos = pos })
  7240. else
  7241. None
  7242. in
  7243. let expr_contents = map_fields (collect_fields cl (Some false) (Some false)) in
  7244. let expr_contents = if ctx.rcf_handle_statics then
  7245. expr_contents @
  7246. [ {
  7247. eexpr = TIf(is_inst,
  7248. { eexpr = TBlock( map_fields (collect_fields cl (Some true) (Some false)) ); etype = basic.tvoid; epos = pos },
  7249. if_not_inst
  7250. );
  7251. etype = basic.tvoid;
  7252. epos = pos
  7253. } ]
  7254. else
  7255. expr_contents @ (if is_some if_not_inst then [ get if_not_inst ] else [])
  7256. in
  7257. let expr =
  7258. {
  7259. eexpr = TBlock( expr_contents );
  7260. etype = basic.tvoid;
  7261. epos = pos;
  7262. } in
  7263. let fn =
  7264. {
  7265. tf_args = tf_args;
  7266. tf_type = basic.tvoid;
  7267. tf_expr = expr
  7268. } in
  7269. (if !has_value || (not (is_override cl)) then begin
  7270. cl.cl_ordered_fields <- cl.cl_ordered_fields @ [cf];
  7271. cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields;
  7272. (if is_override cl then cl.cl_overrides <- cf :: cl.cl_overrides)
  7273. end);
  7274. cf.cf_expr <- Some { eexpr = TFunction(fn); etype = t; epos = pos }
  7275. in
  7276. ignore fields
  7277. let implement_class_methods ctx cl =
  7278. ctx.rcf_class_cl <- Some cl;
  7279. let pos = cl.cl_pos in
  7280. let gen = ctx.rcf_gen in
  7281. let basic = gen.gcon.basic in
  7282. (*
  7283. fields -> redirected to classFields
  7284. getField -> redirected to getField with isStatic true
  7285. setField -> isStatic true
  7286. invokeField -> isStatic true
  7287. getClass -> null
  7288. create -> proxy
  7289. createEmpty -> proxy
  7290. *)
  7291. let is_override = is_override cl in
  7292. let name = "classProxy" in
  7293. let t = (TInst(ctx.rcf_object_iface,[])) in
  7294. (* let cf = mk_class_field name t false pos (Var { v_read = AccNormal; v_write = AccNormal }) [] in *)
  7295. let register_cf cf override =
  7296. cl.cl_ordered_fields <- cf :: cl.cl_ordered_fields;
  7297. cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields;
  7298. if override then cl.cl_overrides <- cf :: cl.cl_overrides
  7299. in
  7300. (* register_cf cf false; *)
  7301. let this_t = TInst(cl, List.map snd cl.cl_params) in
  7302. let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
  7303. let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
  7304. let proxy = mk_this name t in
  7305. (*let ctor =
  7306. let cls = alloc_var "cls" t in
  7307. let tf_args = [cls, None] in
  7308. let t = TFun(fun_args tf_args, basic.tvoid) in
  7309. let cf = mk_class_field "new" t true pos (Method MethNormal) [] in
  7310. cf.cf_expr <- Some({
  7311. eexpr = TFunction({
  7312. tf_args = tf_args;
  7313. tf_type = basic.tvoid;
  7314. tf_expr = mk_block {
  7315. eexpr = TBinop(Ast.OpAssign, proxy, mk_local cls pos);
  7316. etype = cls.v_type;
  7317. epos = pos;
  7318. }
  7319. });
  7320. etype = t;
  7321. epos = pos;
  7322. });
  7323. cf
  7324. in
  7325. register_cf ctor false;*)
  7326. (* setting it as DynamicObject makes getClass return null *)
  7327. let get_class =
  7328. cl.cl_meta <- (Meta.DynamicObject, [], pos) :: cl.cl_meta
  7329. in
  7330. ignore get_class;
  7331. (* fields -> if isInstanceField, redir the method. If not, return classFields *)
  7332. let fields =
  7333. let name = gen.gmk_internal_name "hx" "getFields" in
  7334. let v_base_arr, v_is_inst = alloc_var "baseArr" (basic.tarray basic.tstring), alloc_var "isInstanceFields" basic.tbool in
  7335. let base_arr, is_inst = mk_local v_base_arr pos, mk_local v_is_inst pos in
  7336. let tf_args = [ v_base_arr,None; v_is_inst, None ] in
  7337. let t = TFun(fun_args tf_args, basic.tvoid) in
  7338. let cf = mk_class_field name t false pos (Method MethNormal) [] in
  7339. cf.cf_expr <- Some({
  7340. eexpr = TFunction({
  7341. tf_args = tf_args;
  7342. tf_type = basic.tvoid;
  7343. tf_expr = mk_block {
  7344. eexpr = TIf(is_inst,
  7345. { eexpr = TCall( { (mk_field_access gen proxy name pos) with etype = t }, [base_arr;is_inst]); etype = basic.tvoid; epos = pos },
  7346. 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 });
  7347. etype = basic.tvoid;
  7348. epos = pos
  7349. }
  7350. });
  7351. etype = t;
  7352. epos = pos;
  7353. });
  7354. cf
  7355. in
  7356. register_cf fields (is_override);
  7357. let do_proxy field tf_args ret is_static_argnum =
  7358. let field = gen.gmk_internal_name "hx" field in
  7359. let t = TFun(fun_args tf_args, ret) in
  7360. let cf = mk_class_field field t false pos (Method MethNormal) [] in
  7361. let is_void = is_void ret in
  7362. let may_return e = if is_void then mk_block e else mk_block (mk_return e) in
  7363. let i = ref 0 in
  7364. cf.cf_expr <- Some({
  7365. eexpr = TFunction({
  7366. tf_args = tf_args;
  7367. tf_type = ret;
  7368. tf_expr = may_return {
  7369. eexpr = TCall(
  7370. { (mk_field_access gen proxy field pos) with etype = t },
  7371. List.map (fun (v,_) ->
  7372. let lasti = !i in
  7373. incr i;
  7374. if lasti = is_static_argnum then
  7375. { eexpr = TConst(TBool true); etype = basic.tbool; epos = pos }
  7376. else
  7377. mk_local v pos
  7378. ) tf_args);
  7379. etype = ret;
  7380. epos = pos
  7381. }
  7382. });
  7383. etype = t;
  7384. epos = pos;
  7385. });
  7386. cf
  7387. in
  7388. (* getClassFields -> redir *)
  7389. register_cf (do_proxy "classFields" [ alloc_var "baseArr" (basic.tarray basic.tstring), None ] basic.tvoid (-1)) true;
  7390. (*register_cf (do_proxy "classFields" [ alloc_var "baseArr" (basic.tarray basic.tstring), None ] basic.tvoid (-1)) true;*)
  7391. let fst_args, _ = field_type_args ctx pos in
  7392. let fst_args_len = List.length fst_args in
  7393. (* getField -> redir the method with static = true *)
  7394. (* setField -> redir the methods with static = true *)
  7395. (if ctx.rcf_float_special_case then
  7396. 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;
  7397. 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
  7398. );
  7399. 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;
  7400. 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;
  7401. (* invokeField -> redir the method with static = true *)
  7402. 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;
  7403. (* create / createEmpty -> redir the method *)
  7404. register_cf (do_proxy "create" [ alloc_var "arr" (basic.tarray t_dynamic), None ] t_dynamic (-1)) true;
  7405. register_cf (do_proxy "createEmpty" [ ] t_dynamic (-1)) true
  7406. let implement_get_class ctx cl =
  7407. (*
  7408. if it is DynamicObject, return null;
  7409. if it is not, just do the following:
  7410. if (typehandle(this.class) == typehandle(MyClass))
  7411. return (MyClass.__hx_class != null ? MyClass.__hx_class : MyClass.__hx_class = create_empty(MyClass));
  7412. return MyClass.__hx_class = haxe.lang.Runtime.getClass(MyClass);
  7413. implement both on static and non-static contexts. This way we can call without references.
  7414. *)
  7415. let gen = ctx.rcf_gen in
  7416. let basic = gen.gcon.basic in
  7417. let pos = cl.cl_pos in
  7418. let tclass = get_cl ( (Hashtbl.find gen.gtypes ([],"Class")) ) in
  7419. let cls = TInst(tclass, [ TInst(cl, List.map (fun _ -> t_dynamic) cl.cl_params) ]) in
  7420. let cls_dyn = TInst(tclass, [t_dynamic]) in
  7421. let expr, static_cfs =
  7422. if Meta.has Meta.DynamicObject cl.cl_meta then
  7423. mk_return (null t_dynamic pos), []
  7424. else
  7425. let cache_name = (gen.gmk_internal_name "hx" "class") in
  7426. let cache = mk_class_field cache_name cls false pos (Var { v_read = AccNormal; v_write = AccNormal }) [] in
  7427. cl.cl_ordered_statics <- cl.cl_ordered_statics @ [ cache ];
  7428. cl.cl_statics <- PMap.add cache_name cache cl.cl_statics;
  7429. let cache_access = mk_static_field_access cl cache_name cls pos in
  7430. let create_expr = {
  7431. eexpr = TNew(get ctx.rcf_class_cl, [], [gen.gtools.rf_create_empty cl (List.map (fun _ -> t_dynamic) cl.cl_params) pos]);
  7432. etype = cls;
  7433. epos = pos
  7434. } in
  7435. (if ctx.rcf_class_eager_creation then cache.cf_expr <- Some(create_expr));
  7436. let expr = if ctx.rcf_class_eager_creation then
  7437. mk_return cache_access
  7438. else
  7439. mk_return {
  7440. eexpr = TIf(
  7441. { eexpr = TBinop(Ast.OpNotEq, cache_access, null cls pos); etype = basic.tbool; epos = pos },
  7442. cache_access,
  7443. Some({ eexpr = TBinop(Ast.OpAssign, cache_access, create_expr); etype = cls; epos = pos })
  7444. );
  7445. etype = cls;
  7446. epos = pos
  7447. }
  7448. in
  7449. expr, []
  7450. in
  7451. let func =
  7452. {
  7453. eexpr = TFunction({
  7454. tf_args = [];
  7455. tf_type = cls_dyn;
  7456. tf_expr = expr
  7457. });
  7458. etype = TFun([],cls_dyn);
  7459. epos = pos
  7460. } in
  7461. let get_cl_static = mk_class_field (gen.gmk_internal_name "hx" "getClassStatic") (TFun([],cls_dyn)) false pos (Method MethNormal) [] in
  7462. let get_cl = mk_class_field (gen.gmk_internal_name "hx" "getClass") (TFun([],cls_dyn)) false pos (Method MethNormal) [] in
  7463. get_cl_static.cf_expr <- Some func;
  7464. get_cl.cf_expr <- Some func;
  7465. let all_f = [get_cl] in
  7466. cl.cl_ordered_fields <- cl.cl_ordered_fields @ all_f;
  7467. List.iter (fun cf -> cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields) all_f;
  7468. let all_f = get_cl_static :: static_cfs in
  7469. cl.cl_ordered_statics <- cl.cl_ordered_statics @ all_f;
  7470. List.iter (fun cf -> cl.cl_statics <- PMap.add cf.cf_name cf cl.cl_statics) all_f;
  7471. if is_override cl then cl.cl_overrides <- get_cl :: cl.cl_overrides
  7472. let implement_invokeField ctx ~slow_invoke cl =
  7473. (*
  7474. There are two ways to implement an haxe reflection-enabled class:
  7475. When we extend a non-hxgen class, and when we extend the base HxObject class.
  7476. Because of the added boiler plate we'd add every time we extend a non-hxgen class to implement a big IHxObject
  7477. interface, we'll handle the cases differently when implementing each interface.
  7478. At the IHxObject interface, there's only invokeDynamic(field, args[]), while at the HxObject class there are
  7479. the other, more optimized methods, that follow the Function class interface.
  7480. Since this will only be called by the Closure class, this conversion can be properly dealt with later.
  7481. TODO: create the faster version. By now only invokeDynamic will be implemented
  7482. *)
  7483. let gen = ctx.rcf_gen in
  7484. let basic = gen.gcon.basic in
  7485. let pos = cl.cl_pos in
  7486. let has_method = ref false in
  7487. let is_override = ref false in
  7488. let rec extends_hxobject cl =
  7489. match cl.cl_super with
  7490. | None -> true
  7491. | Some (cl,_) when is_hxgen (TClassDecl cl) -> is_override := true; extends_hxobject cl
  7492. | _ -> false
  7493. in
  7494. let field_args, switch_var = field_type_args ctx cl.cl_pos in
  7495. let field_args_exprs = List.map (fun (v,_) -> mk_local v pos) field_args in
  7496. let is_static = alloc_var "isStatic" basic.tbool in
  7497. let dynamic_arg = alloc_var "dynargs" (basic.tarray t_dynamic) in
  7498. let all_args = field_args @ (if ctx.rcf_handle_statics then [ is_static,None; dynamic_arg,None ] else [ dynamic_arg, None ] ) in
  7499. let fun_t = TFun(fun_args all_args, t_dynamic) in
  7500. let this_t = TInst(cl, List.map snd cl.cl_params) in
  7501. let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
  7502. let apply_object cf = apply_params cf.cf_params (List.map (fun _ -> t_dynamic) cf.cf_params) cf.cf_type in
  7503. let mk_this_call_raw name fun_t params =
  7504. { eexpr = TCall( { (mk_field_access gen this name pos) with etype = fun_t }, params ); etype = snd (get_args fun_t); epos = pos }
  7505. in
  7506. let mk_this_call cf params =
  7507. let t = apply_object cf in
  7508. (* the return type transformation into Dynamic *)
  7509. (* is meant to avoid return-type casting after functions with *)
  7510. (* type parameters are properly inferred at TypeParams.infer_params *)
  7511. (* e.g. function getArray<T : SomeType>(t:T):Array<T>; after infer_params, *)
  7512. (* T will be inferred as SomeType, but the returned type will still be typed *)
  7513. (* as Array<Dynamic> *)
  7514. let args, ret = get_args t in
  7515. let ret = match follow ret with
  7516. | TAbstract ({ a_path = ([], "Void") },[]) -> ret
  7517. | _ -> ret
  7518. in
  7519. mk_this_call_raw cf.cf_name (TFun(args, ret)) params
  7520. in
  7521. let mk_static_call cf params =
  7522. let t = apply_object cf in
  7523. let _, ret = get_fun (follow t) in
  7524. { eexpr = TCall( mk_static_field_access cl cf.cf_name t pos, params ); etype = ret; epos = pos }
  7525. in
  7526. let extends_hxobject = extends_hxobject cl in
  7527. ignore extends_hxobject;
  7528. (* creates a dynamicInvoke of the class fields listed here *)
  7529. (*
  7530. function dynamicInvoke(field, isStatic, dynargs)
  7531. {
  7532. switch(field)
  7533. {
  7534. case "a": this.a(dynargs[0], dynargs[1], dynargs[2]...);
  7535. default: super.dynamicInvoke //or this.getField(field).invokeField(dynargs)
  7536. }
  7537. }
  7538. *)
  7539. let dyn_fun = mk_class_field (ctx.rcf_gen.gmk_internal_name "hx" "invokeField") fun_t false cl.cl_pos (Method MethNormal) [] in
  7540. let mk_switch_dyn cfs static old =
  7541. (* mk_class_field name t public pos kind params = *)
  7542. let get_case (names,cf) =
  7543. has_method := true;
  7544. let i = ref 0 in
  7545. let dyn_arg_local = mk_local dynamic_arg pos in
  7546. let cases = List.map (switch_case ctx pos) names in
  7547. (cases,
  7548. { eexpr = TReturn(Some ( (if static then mk_static_call else mk_this_call) cf (List.map (fun (name,_,t) ->
  7549. let ret = { eexpr = TArray(dyn_arg_local, mk_int ctx !i pos); etype = t_dynamic; epos = pos } in
  7550. incr i;
  7551. ret
  7552. ) (fst (get_args (cf.cf_type))) ) ));
  7553. etype = basic.tvoid;
  7554. epos = pos
  7555. }
  7556. )
  7557. in
  7558. let cfs = List.filter (fun (_,cf) -> match cf.cf_kind with
  7559. | Method _ -> if List.memq cf cl.cl_overrides then false else true
  7560. | _ -> true) cfs
  7561. in
  7562. let cases = List.map get_case cfs in
  7563. let cases = match old with
  7564. | [] -> cases
  7565. | _ ->
  7566. let ncases = List.map (fun cf -> switch_case ctx pos cf.cf_name) old in
  7567. ( ncases, mk_return ((get slow_invoke) this (mk_local (fst (List.hd field_args)) pos) (mk_local dynamic_arg pos)) ) :: cases
  7568. in
  7569. let default = if !is_override && not(static) then
  7570. (* let call_super ctx fn_args ret_t fn_name this_t pos = *)
  7571. { eexpr = TReturn(Some (call_super ctx all_args t_dynamic dyn_fun cl this_t pos) ); etype = basic.tvoid; epos = pos }
  7572. (*else if ctx.rcf_create_getsetinvoke_fields then (* we always need to run create_getset before *)
  7573. let get_field_name = gen.gmk_internal_name "hx" "getField" in
  7574. { eexpr = TReturn( Some (mk_this_call (PMap.find get_field_name cl.cl_fields) [mk_local dynamic_arg pos] ) ); etype = basic.tvoid; epos = pos }*)
  7575. else (
  7576. (*let field = (gen.gtools.r_field false (TInst(ctx.rcf_ft.func_class,[])) this (mk_local (fst (List.hd all_args)) pos)) in*)
  7577. (* let mk_field_access ctx pos local field is_float is_static throw_errors set_option = *)
  7578. 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
  7579. let field = mk_cast (TInst(ctx.rcf_ft.func_class,[])) field in
  7580. mk_return {
  7581. eexpr = TCall(
  7582. mk_field_access gen field (gen.gmk_internal_name "hx" "invokeDynamic") pos,
  7583. [mk_local dynamic_arg pos]);
  7584. etype = t_dynamic;
  7585. epos = pos
  7586. } )
  7587. in
  7588. {
  7589. eexpr = TSwitch(mk_local switch_var pos, cases, Some default);
  7590. etype = basic.tvoid;
  7591. epos = pos;
  7592. }
  7593. in
  7594. let contents =
  7595. let statics = collect_fields cl (Some true) (Some true) in
  7596. let nonstatics = collect_fields cl (Some true) (Some false) in
  7597. let old_nonstatics = ref [] in
  7598. let nonstatics = match slow_invoke with
  7599. | None -> nonstatics
  7600. | Some _ ->
  7601. List.filter (fun (n,cf) ->
  7602. let is_old = not (PMap.mem cf.cf_name cl.cl_fields) || List.memq cf cl.cl_overrides in
  7603. (if is_old then old_nonstatics := cf :: !old_nonstatics);
  7604. not is_old
  7605. ) nonstatics
  7606. in
  7607. if ctx.rcf_handle_statics then
  7608. {
  7609. eexpr = TIf(mk_local is_static pos, mk_switch_dyn statics true [], Some(mk_switch_dyn nonstatics false !old_nonstatics));
  7610. etype = basic.tvoid;
  7611. epos = pos;
  7612. } else
  7613. mk_switch_dyn nonstatics false !old_nonstatics
  7614. in
  7615. dyn_fun.cf_expr <- Some
  7616. {
  7617. eexpr = TFunction(
  7618. {
  7619. tf_args = all_args;
  7620. tf_type = t_dynamic;
  7621. tf_expr = mk_block contents;
  7622. });
  7623. etype = TFun(fun_args all_args, t_dynamic);
  7624. epos = pos;
  7625. };
  7626. if !is_override && not (!has_method) then () else begin
  7627. cl.cl_ordered_fields <- cl.cl_ordered_fields @ [dyn_fun];
  7628. cl.cl_fields <- PMap.add dyn_fun.cf_name dyn_fun cl.cl_fields;
  7629. (if !is_override then cl.cl_overrides <- dyn_fun :: cl.cl_overrides)
  7630. end
  7631. let implement_varargs_cl ctx cl =
  7632. let pos = cl.cl_pos in
  7633. let gen = ctx.rcf_gen in
  7634. let basic = gen.gcon.basic in
  7635. let this_t = TInst(cl, List.map snd cl.cl_params) in
  7636. let this = { eexpr = TConst(TThis); etype = this_t ; epos = pos } in
  7637. let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
  7638. let invokedyn = gen.gmk_internal_name "hx" "invokeDynamic" in
  7639. let idyn_t = TFun([gen.gmk_internal_name "fn" "dynargs", false, basic.tarray t_dynamic], t_dynamic) in
  7640. let this_idyn = mk_this invokedyn idyn_t in
  7641. let map_fn arity ret vars api =
  7642. let rec loop i acc =
  7643. if i < 0 then
  7644. acc
  7645. else
  7646. let obj = api i t_dynamic None in
  7647. loop (i - 1) (obj :: acc)
  7648. in
  7649. let call_arg = if arity = (-1) then
  7650. api (-1) t_dynamic None
  7651. else if arity = 0 then
  7652. null (basic.tarray t_empty) pos
  7653. else
  7654. { eexpr = TArrayDecl(loop (arity - 1) []); etype = basic.tarray t_empty; epos = pos }
  7655. in
  7656. let expr = {
  7657. eexpr = TCall(
  7658. this_idyn,
  7659. [ call_arg ]
  7660. );
  7661. etype = t_dynamic;
  7662. epos = pos
  7663. } in
  7664. let expr = if like_float ret && not (like_int ret) then mk_cast ret expr else expr in
  7665. [], mk_return expr
  7666. in
  7667. 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
  7668. cl.cl_ordered_fields <- cl.cl_ordered_fields @ all_cfs;
  7669. List.iter (fun cf ->
  7670. cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields
  7671. ) all_cfs;
  7672. List.iter (fun cf ->
  7673. cl.cl_overrides <- cf :: cl.cl_overrides
  7674. ) cl.cl_ordered_fields
  7675. let implement_closure_cl ctx cl =
  7676. let pos = cl.cl_pos in
  7677. let gen = ctx.rcf_gen in
  7678. let basic = gen.gcon.basic in
  7679. let field_args, _ = field_type_args ctx pos in
  7680. let obj_arg = alloc_var "target" (TInst(ctx.rcf_object_iface, [])) in
  7681. let this_t = TInst(cl, List.map snd cl.cl_params) in
  7682. let this = { eexpr = TConst(TThis); etype = this_t ; epos = pos } in
  7683. let mk_this field t = { (mk_field_access gen this field pos) with etype = t } in
  7684. let tf_args = field_args @ [obj_arg, None] in
  7685. let cfs, ctor_body = List.fold_left (fun (acc_cf,acc_expr) (v,_) ->
  7686. let cf = mk_class_field v.v_name v.v_type false pos (Var { v_read = AccNormal; v_write = AccNormal } ) [] in
  7687. 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
  7688. (cf :: acc_cf, expr :: acc_expr)
  7689. ) ([], []) tf_args in
  7690. let map_fn arity ret vars api =
  7691. let this_obj = mk_this "target" (TInst(ctx.rcf_object_iface, [])) in
  7692. let rec loop i acc =
  7693. if i < 0 then
  7694. acc
  7695. else
  7696. let obj = api i t_dynamic None in
  7697. loop (i - 1) (obj :: acc)
  7698. in
  7699. let call_arg = if arity = (-1) then
  7700. api (-1) t_dynamic None
  7701. else if arity = 0 then
  7702. null (basic.tarray t_empty) pos
  7703. else
  7704. { eexpr = TArrayDecl(loop (arity - 1) []); etype = basic.tarray t_empty; epos = pos }
  7705. in
  7706. let expr = {
  7707. eexpr = TCall(
  7708. mk_field_access gen this_obj (gen.gmk_internal_name "hx" "invokeField") pos,
  7709. (List.map (fun (v,_) -> mk_this v.v_name v.v_type) field_args) @
  7710. (if ctx.rcf_handle_statics then
  7711. [ { eexpr = TConst(TBool false); etype = basic.tbool; epos = pos }; call_arg ]
  7712. else
  7713. [ call_arg ]
  7714. )
  7715. );
  7716. etype = t_dynamic;
  7717. epos = pos
  7718. } in
  7719. let expr = if like_float ret && not (like_int ret) then mk_cast ret expr else expr in
  7720. [], mk_return expr
  7721. in
  7722. 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
  7723. List.iter (fun cf ->
  7724. cl.cl_overrides <- cf :: cl.cl_overrides
  7725. ) all_cfs;
  7726. let all_cfs = cfs @ all_cfs in
  7727. cl.cl_ordered_fields <- cl.cl_ordered_fields @ all_cfs;
  7728. List.iter (fun cf ->
  7729. cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields
  7730. ) all_cfs;
  7731. let ctor_t = TFun(fun_args tf_args, basic.tvoid) in
  7732. let ctor_cf = mk_class_field "new" ctor_t true pos (Method MethNormal) [] in
  7733. ctor_cf.cf_expr <- Some {
  7734. eexpr = TFunction({
  7735. tf_args = tf_args;
  7736. tf_type = basic.tvoid;
  7737. tf_expr = { eexpr = TBlock({
  7738. eexpr = TCall({ eexpr = TConst(TSuper); etype = TInst(cl,[]); epos = pos }, [mk_int ctx (-1) pos; mk_int ctx (-1) pos]);
  7739. etype = basic.tvoid;
  7740. epos = pos
  7741. } :: ctor_body); etype = basic.tvoid; epos = pos }
  7742. });
  7743. etype = ctor_t;
  7744. epos = pos
  7745. };
  7746. cl.cl_constructor <- Some ctor_cf;
  7747. let closure_fun eclosure e field is_static =
  7748. let f = { eexpr = TConst(TString field); etype = basic.tstring; epos = eclosure.epos } in
  7749. 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
  7750. let args = args @ [ mk_cast (TInst(ctx.rcf_object_iface, [])) e ] in
  7751. { eclosure with eexpr = TNew(cl,[],args) }
  7752. in
  7753. closure_fun
  7754. let get_closure_func ctx closure_cl =
  7755. let gen = ctx.rcf_gen in
  7756. let basic = gen.gcon.basic in
  7757. let closure_func eclosure e field is_static =
  7758. mk_cast eclosure.etype { eclosure with
  7759. eexpr = TNew(closure_cl, [], [
  7760. e;
  7761. { eexpr = TConst(TString field); etype = basic.tstring; epos = eclosure.epos }
  7762. ] @ (
  7763. if ctx.rcf_optimize then [ { eexpr = TConst(TInt (hash_field_i32 ctx eclosure.epos field)); etype = basic.tint; epos = eclosure.epos } ] else []
  7764. ));
  7765. etype = TInst(closure_cl,[])
  7766. }
  7767. in
  7768. closure_func
  7769. (*
  7770. main expr -> field expr -> field string -> possible set expr -> should_throw_exceptions -> changed expression
  7771. Changes a get / set
  7772. *
  7773. mutable rcf_on_getset_field : texpr->texpr->string->texpr option->bool->texpr;*)
  7774. let configure_dynamic_field_access ctx is_synf =
  7775. let gen = ctx.rcf_gen in
  7776. let is_dynamic expr fexpr field =
  7777. match (field_access_esp gen (gen.greal_type fexpr.etype) field) with
  7778. | FEnumField _
  7779. | FClassField _ -> false
  7780. | _ -> true
  7781. in
  7782. let configure = if is_synf then DynamicFieldAccess.configure_as_synf else DynamicFieldAccess.configure in
  7783. let maybe_hash = if ctx.rcf_optimize then fun str pos -> Some (hash_field_i32 ctx pos str) else fun str pos -> None in
  7784. configure gen (DynamicFieldAccess.abstract_implementation gen is_dynamic
  7785. (* print_endline *)
  7786. (fun expr fexpr field set is_unsafe ->
  7787. let hash = maybe_hash field fexpr.epos in
  7788. ctx.rcf_on_getset_field expr fexpr field hash set is_unsafe
  7789. )
  7790. (fun ecall fexpr field call_list ->
  7791. let hash = maybe_hash field fexpr.epos in
  7792. ctx.rcf_on_call_field ecall fexpr field hash call_list
  7793. )
  7794. );
  7795. ()
  7796. let replace_reflection ctx cl =
  7797. let gen = ctx.rcf_gen in
  7798. let pos = cl.cl_pos in
  7799. let this_t = TInst(cl, List.map snd cl.cl_params) in
  7800. let this = { eexpr = TConst(TThis); etype = this_t; epos = pos } in
  7801. let last_fields = match cl.cl_super with
  7802. | None -> PMap.empty
  7803. | Some (super,_) -> super.cl_fields
  7804. in
  7805. let new_fields = ref [] in
  7806. let process_cf static cf =
  7807. match cf.cf_kind with
  7808. | Var _ -> ()
  7809. | _ when Meta.has Meta.ReplaceReflection cf.cf_meta ->
  7810. 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
  7811. let new_name = gen.gmk_internal_name "hx" name in
  7812. let new_cf = mk_class_field new_name cf.cf_type cf.cf_public cf.cf_pos cf.cf_kind cf.cf_params in
  7813. let fn_args, ret = get_fun (follow cf.cf_type) in
  7814. let tf_args = List.map (fun (name,_,t) -> alloc_var name t, None) fn_args in
  7815. let is_void = is_void ret in
  7816. let expr = {
  7817. eexpr = TCall(
  7818. {
  7819. 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)));
  7820. etype = cf.cf_type;
  7821. epos = cf.cf_pos;
  7822. },
  7823. List.map (fun (v,_) -> mk_local v cf.cf_pos) tf_args);
  7824. etype = ret;
  7825. epos = cf.cf_pos
  7826. } in
  7827. let new_f =
  7828. {
  7829. tf_args = tf_args;
  7830. tf_type = ret;
  7831. tf_expr = {
  7832. eexpr = TBlock([if is_void then expr else mk_return expr]);
  7833. etype = ret;
  7834. epos = pos;
  7835. }
  7836. } in
  7837. new_cf.cf_expr <- Some({ eexpr = TFunction(new_f); etype = cf.cf_type; epos = cf.cf_pos});
  7838. new_fields := new_cf :: !new_fields;
  7839. (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);
  7840. if not static && PMap.mem new_name last_fields then cl.cl_overrides <- new_cf :: cl.cl_overrides
  7841. | _ -> ()
  7842. in
  7843. List.iter (process_cf false) cl.cl_ordered_fields;
  7844. cl.cl_ordered_fields <- cl.cl_ordered_fields @ !new_fields;
  7845. new_fields := [];
  7846. List.iter (process_cf true) cl.cl_ordered_statics;
  7847. cl.cl_ordered_statics <- cl.cl_ordered_statics @ !new_fields
  7848. (* ******************************************* *)
  7849. (* UniversalBaseClass *)
  7850. (* ******************************************* *)
  7851. (*
  7852. Sets the universal base class for hxgen types (HxObject / IHxObject)
  7853. dependencies:
  7854. As a rule, it should be one of the last module filters to run so any @:hxgen class created in the process
  7855. -Should- only run after TypeParams.RealTypeParams.Modf, since
  7856. *)
  7857. module UniversalBaseClass =
  7858. struct
  7859. let name = "rcf_universal_base_class"
  7860. let priority = min_dep +. 10.
  7861. let default_implementation gen baseclass baseinterface basedynamic =
  7862. (* baseinterface.cl_meta <- (Meta.BaseInterface, [], baseinterface.cl_pos) :: baseinterface.cl_meta; *)
  7863. let rec run md =
  7864. (if is_hxgen md then
  7865. match md with
  7866. | 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 ->
  7867. cl.cl_implements <- (baseinterface, []) :: cl.cl_implements
  7868. | TClassDecl ({ cl_kind = KAbstractImpl _ } as cl) ->
  7869. (*
  7870. TODO: probably here is not the best place to add @:final to KAbstractImpl, also:
  7871. Doesn't it make sense to add @:final to KAbstractImpls on all platforms?
  7872. *)
  7873. if not (Meta.has Meta.Final cl.cl_meta) then cl.cl_meta <- (Meta.Final, [], cl.cl_pos) :: cl.cl_meta
  7874. | 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 ->
  7875. if is_some cl.cl_dynamic then
  7876. cl.cl_super <- Some (basedynamic,[])
  7877. else
  7878. cl.cl_super <- Some (baseclass,[])
  7879. | 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) ) ->
  7880. cl.cl_implements <- (baseinterface, []) :: cl.cl_implements
  7881. | _ -> ()
  7882. );
  7883. md
  7884. in
  7885. run
  7886. let configure gen mapping_func =
  7887. let map e = Some(mapping_func e) in
  7888. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
  7889. let default_config gen baseclass baseinterface basedynamic =
  7890. let impl = (default_implementation gen baseclass baseinterface basedynamic) in
  7891. configure gen impl
  7892. end;;
  7893. (*
  7894. Priority: must run AFTER UniversalBaseClass
  7895. *)
  7896. let priority = solve_deps name [DAfter UniversalBaseClass.priority]
  7897. let configure ?slow_invoke ctx baseinterface =
  7898. let gen = ctx.rcf_gen in
  7899. let run = (fun md -> match md with
  7900. | TClassDecl cl when is_hxgen md && ( not cl.cl_interface || cl.cl_path = baseinterface.cl_path ) && (match cl.cl_kind with KAbstractImpl _ -> false | _ -> true) ->
  7901. (if Meta.has Meta.ReplaceReflection cl.cl_meta then replace_reflection ctx cl);
  7902. (implement_dynamics ctx cl);
  7903. (if not (PMap.mem (gen.gmk_internal_name "hx" "lookupField") cl.cl_fields) then implement_final_lookup ctx cl);
  7904. (if not (PMap.mem (gen.gmk_internal_name "hx" "getField") cl.cl_fields) then implement_get_set ctx cl);
  7905. (if not (PMap.mem (gen.gmk_internal_name "hx" "invokeField") cl.cl_fields) then implement_invokeField ctx ~slow_invoke:slow_invoke cl);
  7906. (if not (PMap.mem (gen.gmk_internal_name "hx" "classFields") cl.cl_fields) then implement_fields ctx cl);
  7907. (if ctx.rcf_handle_statics && not (PMap.mem (gen.gmk_internal_name "hx" "getClassStatic") cl.cl_statics) then implement_get_class ctx cl);
  7908. (if not cl.cl_interface && not (PMap.mem (gen.gmk_internal_name "hx" "create") cl.cl_fields) then implement_create_empty ctx cl);
  7909. None
  7910. | _ -> None)
  7911. in
  7912. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) run
  7913. end;;
  7914. (* ******************************************* *)
  7915. (* Object Declaration Mapper *)
  7916. (* ******************************************* *)
  7917. (*
  7918. A simple Object Declaration Mapper. By default it will be a syntax filter, which only runs
  7919. after
  7920. dependencies:
  7921. *)
  7922. module ObjectDeclMap =
  7923. struct
  7924. let name = "object_decl_map"
  7925. let priority = solve_deps name []
  7926. let traverse gen map_fn =
  7927. let rec run e =
  7928. match e.eexpr with
  7929. | TObjectDecl odecl ->
  7930. let e = Type.map_expr run e in
  7931. (match e.eexpr with | TObjectDecl odecl -> map_fn e odecl | _ -> assert false)
  7932. | _ -> Type.map_expr run e
  7933. in
  7934. run
  7935. let configure gen (mapping_func:texpr->texpr) =
  7936. let map e = Some(mapping_func e) in
  7937. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  7938. end;;
  7939. (* ******************************************* *)
  7940. (* EnumToClass *)
  7941. (* ******************************************* *)
  7942. (*
  7943. For languages that don't support parameterized enums and/or metadata in enums, we need to transform
  7944. enums into normal classes. This is done at the first module pass by creating new classes with the same
  7945. path inside the modules, and removing the actual enum module by setting it as en extern.
  7946. Later, on the last expression pass, it will transform the TMatch codes into TSwitch. it will introduce a new
  7947. dependency, though:
  7948. * The target must create its own strategy to deal with reflection. As it is right now, we will have a base class
  7949. which the class will extend, create @:$IsEnum metadata for the class, and create @:alias() metadatas for the fields,
  7950. with their tag order (as a string) as their alias. If you are using ReflectionCFs, then you don't have to worry
  7951. about that, as it's already generating all information needed by the haxe runtime.
  7952. so they can be
  7953. dependencies:
  7954. The MatchToSwitch part must run after ExprStatementUnwrap as modified expressions might confuse it (not so true anymore)
  7955. *)
  7956. module EnumToClass =
  7957. struct
  7958. let name = "enum_to_class"
  7959. let priority = solve_deps name []
  7960. type t = {
  7961. ec_tbl : (path, tclass) Hashtbl.t;
  7962. }
  7963. let new_t () =
  7964. {
  7965. ec_tbl = Hashtbl.create 10
  7966. }
  7967. (* ******************************************* *)
  7968. (* EnumToClassModf *)
  7969. (* ******************************************* *)
  7970. (*
  7971. The actual Module Filter that will transform the enum into a class
  7972. dependencies:
  7973. Should run before ReflectionCFs, in order to enable proper reflection access.
  7974. Should run before TypeParams.RealTypeParams.RealTypeParamsModf, since generic enums must be first converted to generic classes
  7975. It needs that the target platform implements __array__() as a shortcut to declare haxe.ds.Vector
  7976. *)
  7977. module EnumToClassModf =
  7978. struct
  7979. let name = "enum_to_class_mod"
  7980. let priority = solve_deps name [DBefore ReflectionCFs.priority; DBefore TypeParams.RealTypeParams.RealTypeParamsModf.priority]
  7981. let pmap_exists fn pmap = try PMap.iter (fun a b -> if fn a b then raise Exit) pmap; false with | Exit -> true
  7982. let has_any_meta en =
  7983. let has_meta meta = List.exists (fun (m,_,_) -> match m with Meta.Custom _ -> true | _ -> false) meta in
  7984. has_meta en.e_meta || pmap_exists (fun _ ef -> has_meta ef.ef_meta) en.e_constrs
  7985. let convert gen t base_class base_param_class en should_be_hxgen handle_type_params =
  7986. let basic = gen.gcon.basic in
  7987. let pos = en.e_pos in
  7988. (* create the class *)
  7989. let cl = mk_class en.e_module en.e_path pos in
  7990. Hashtbl.add t.ec_tbl en.e_path cl;
  7991. (match Codegen.build_metadata gen.gcon (TEnumDecl en) with
  7992. | Some expr ->
  7993. let cf = mk_class_field "__meta__" expr.etype false expr.epos (Var { v_read = AccNormal; v_write = AccNormal }) [] in
  7994. cf.cf_expr <- Some expr;
  7995. cl.cl_statics <- PMap.add "__meta__" cf cl.cl_statics;
  7996. cl.cl_ordered_statics <- cf :: cl.cl_ordered_statics
  7997. | _ -> ()
  7998. );
  7999. let super, has_params = if Meta.has Meta.FlatEnum en.e_meta then base_class, false else base_param_class, true in
  8000. cl.cl_super <- Some(super,[]);
  8001. cl.cl_extern <- en.e_extern;
  8002. en.e_extern <- true;
  8003. en.e_meta <- (Meta.Class, [], pos) :: en.e_meta;
  8004. cl.cl_module <- en.e_module;
  8005. cl.cl_meta <- ( Meta.Enum, [], pos ) :: cl.cl_meta;
  8006. (match gen.gcon.platform with
  8007. | Cs when Common.defined gen.gcon Define.CoreApiSerialize ->
  8008. cl.cl_meta <- ( Meta.Meta, [ (EField( (EConst (Ident "System"), null_pos ), "Serializable" ), null_pos) ], null_pos ) :: cl.cl_meta
  8009. | _ -> ());
  8010. let c_types =
  8011. if handle_type_params then
  8012. List.map (fun (s,t) -> (s, TInst (map_param (get_cl_t t), []))) en.e_params
  8013. else
  8014. []
  8015. in
  8016. cl.cl_params <- c_types;
  8017. let i = ref 0 in
  8018. let cfs = List.map (fun name ->
  8019. let ef = PMap.find name en.e_constrs in
  8020. let pos = ef.ef_pos in
  8021. let old_i = !i in
  8022. incr i;
  8023. let cf = match follow ef.ef_type with
  8024. | TFun(params,ret) ->
  8025. let dup_types =
  8026. if handle_type_params then
  8027. List.map (fun (s,t) -> (s, TInst (map_param (get_cl_t t), []))) en.e_params
  8028. else
  8029. []
  8030. in
  8031. let ef_type =
  8032. let fn, types = if handle_type_params then snd, dup_types else (fun _ -> t_dynamic), en.e_params in
  8033. let t = apply_params en.e_params (List.map fn types) ef.ef_type in
  8034. apply_params ef.ef_params (List.map fn ef.ef_params) t
  8035. in
  8036. let params, ret = get_fun ef_type in
  8037. let cf_params = if handle_type_params then dup_types @ ef.ef_params else [] in
  8038. let cf = mk_class_field name ef_type true pos (Method MethNormal) cf_params in
  8039. cf.cf_meta <- [];
  8040. let tf_args = List.map (fun (name,opt,t) -> (alloc_var name t, if opt then Some TNull else None) ) params in
  8041. let arr_decl = mk_nativearray_decl gen t_dynamic (List.map (fun (v,_) -> mk_local v pos) tf_args) pos in
  8042. let expr = {
  8043. eexpr = TFunction({
  8044. tf_args = tf_args;
  8045. tf_type = ret;
  8046. 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 } );
  8047. });
  8048. etype = ef_type;
  8049. epos = pos
  8050. } in
  8051. cf.cf_expr <- Some expr;
  8052. cf
  8053. | _ ->
  8054. let actual_t = match follow ef.ef_type with
  8055. | TEnum(e, p) -> TEnum(e, List.map (fun _ -> t_dynamic) p)
  8056. | _ -> assert false
  8057. in
  8058. let cf = mk_class_field name actual_t true pos (Var { v_read = AccNormal; v_write = AccNever }) [] in
  8059. let args = if has_params then
  8060. [mk_int gen old_i pos; null (gen.gclasses.nativearray t_dynamic) pos]
  8061. else
  8062. [mk_int gen old_i pos]
  8063. in
  8064. cf.cf_meta <- [Meta.ReadOnly,[],pos];
  8065. cf.cf_expr <- Some {
  8066. eexpr = TNew(cl, List.map (fun _ -> t_empty) cl.cl_params, args);
  8067. etype = TInst(cl, List.map (fun _ -> t_empty) cl.cl_params);
  8068. epos = pos;
  8069. };
  8070. cf
  8071. in
  8072. cl.cl_statics <- PMap.add cf.cf_name cf cl.cl_statics;
  8073. cf
  8074. ) en.e_names in
  8075. let constructs_cf = mk_class_field "__hx_constructs" (gen.gclasses.nativearray basic.tstring) true pos (Var { v_read = AccNormal; v_write = AccNever }) [] in
  8076. constructs_cf.cf_meta <- [Meta.ReadOnly,[],pos];
  8077. 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);
  8078. cl.cl_ordered_statics <- constructs_cf :: cfs @ cl.cl_ordered_statics ;
  8079. cl.cl_statics <- PMap.add "__hx_constructs" constructs_cf cl.cl_statics;
  8080. let getTag_cf_type = tfun [] basic.tstring in
  8081. let getTag_cf = mk_class_field "getTag" getTag_cf_type true pos (Method MethNormal) [] in
  8082. getTag_cf.cf_meta <- [(Meta.Final, [], pos)];
  8083. getTag_cf.cf_expr <- Some {
  8084. eexpr = TFunction {
  8085. tf_args = [];
  8086. tf_type = basic.tstring;
  8087. tf_expr = {
  8088. eexpr = TReturn (Some (
  8089. let e_constructs = mk_static_field_access_infer cl "__hx_constructs" pos [] in
  8090. let e_this = mk (TConst TThis) (TInst (cl,[])) pos in
  8091. let e_index = mk_field_access gen e_this "index" pos in
  8092. {
  8093. eexpr = TArray(e_constructs,e_index);
  8094. etype = basic.tstring;
  8095. epos = pos;
  8096. }
  8097. ));
  8098. epos = pos;
  8099. etype = basic.tvoid;
  8100. }
  8101. };
  8102. etype = getTag_cf_type;
  8103. epos = pos;
  8104. };
  8105. cl.cl_ordered_fields <- getTag_cf :: cl.cl_ordered_fields ;
  8106. cl.cl_fields <- PMap.add "getTag" getTag_cf cl.cl_fields;
  8107. cl.cl_overrides <- getTag_cf :: cl.cl_overrides;
  8108. if should_be_hxgen then
  8109. cl.cl_meta <- (Meta.HxGen,[],cl.cl_pos) :: cl.cl_meta
  8110. else
  8111. cl.cl_meta <- (Meta.NativeGen,[],cl.cl_pos) :: cl.cl_meta;
  8112. gen.gadd_to_module (TClassDecl cl) (max_dep);
  8113. TEnumDecl en
  8114. (*
  8115. traverse
  8116. gen - gen context
  8117. convert_all : bool - should we convert all enums? If set, convert_if_has_meta will be ignored.
  8118. convert_if_has_meta : bool - should we convert only if it has meta?
  8119. enum_base_class : tclass - the enum base class.
  8120. should_be_hxgen : bool - should the created enum be hxgen?
  8121. *)
  8122. let traverse gen t convert_all convert_if_has_meta enum_base_class param_enum_class should_be_hxgen handle_tparams =
  8123. let convert e = convert gen t enum_base_class param_enum_class e should_be_hxgen handle_tparams in
  8124. let run md = match md with
  8125. | TEnumDecl e when is_hxgen md ->
  8126. if convert_all then
  8127. convert e
  8128. else if convert_if_has_meta && has_any_meta e then
  8129. convert e
  8130. else if not (Meta.has Meta.FlatEnum e.e_meta) then
  8131. convert e
  8132. else begin
  8133. (* take off the :hxgen meta from it, if there's any *)
  8134. e.e_meta <- List.filter (fun (n,_,_) -> not (n = Meta.HxGen)) e.e_meta;
  8135. md
  8136. end
  8137. | _ -> md
  8138. in
  8139. run
  8140. let configure gen (mapping_func:module_type->module_type) =
  8141. let map md = Some(mapping_func md) in
  8142. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
  8143. end;;
  8144. (* ******************************************* *)
  8145. (* EnumToClassExprf *)
  8146. (* ******************************************* *)
  8147. (*
  8148. Enum to class Expression Filter
  8149. will convert TMatch into TSwitch
  8150. dependencies:
  8151. Should run before TArrayTransform, since it generates array access expressions
  8152. *)
  8153. module EnumToClassExprf =
  8154. struct
  8155. let name = "enum_to_class_exprf"
  8156. let priority = solve_deps name [DBefore TArrayTransform.priority]
  8157. let traverse gen t opt_get_native_enum_tag =
  8158. let rec run e =
  8159. let get_converted_enum_type et =
  8160. let en, eparams = match follow (gen.gfollow#run_f et) with
  8161. | TEnum(en,p) -> en, p
  8162. | _ -> raise Not_found
  8163. in
  8164. let cl = Hashtbl.find t.ec_tbl en.e_path in
  8165. TInst(cl, eparams)
  8166. in
  8167. match e.eexpr with
  8168. | TCall (({eexpr = TField(_, FStatic({cl_path=[],"Type"},{cf_name="enumIndex"}))} as left), [f]) ->
  8169. let f = run f in
  8170. (try
  8171. mk_field_access gen {f with etype = get_converted_enum_type f.etype} "index" e.epos
  8172. with Not_found ->
  8173. { e with eexpr = TCall(left, [f]) })
  8174. | TEnumParameter(f, _,i) ->
  8175. let f = run f in
  8176. (* check if en was converted to class *)
  8177. (* if it was, switch on tag field and change cond type *)
  8178. let f = try
  8179. { f with etype = get_converted_enum_type f.etype }
  8180. with Not_found ->
  8181. f
  8182. in
  8183. let cond_array = { (mk_field_access gen f "params" f.epos) with etype = gen.gclasses.nativearray t_dynamic } in
  8184. { e with eexpr = TArray(cond_array, mk_int gen i cond_array.epos); }
  8185. | _ -> Type.map_expr run e
  8186. in
  8187. run
  8188. let configure gen (mapping_func:texpr->texpr) =
  8189. let map e = Some(mapping_func e) in
  8190. gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map
  8191. end;;
  8192. 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 =
  8193. let t = new_t () in
  8194. EnumToClassModf.configure gen (EnumToClassModf.traverse gen t convert_all convert_if_has_meta enum_base_class param_enum_class should_be_hxgen handle_tparams);
  8195. EnumToClassExprf.configure gen (EnumToClassExprf.traverse gen t opt_get_native_enum_tag)
  8196. end;;
  8197. (* ******************************************* *)
  8198. (* IteratorsInterface *)
  8199. (* ******************************************* *)
  8200. (*
  8201. This module will handle with Iterators, Iterables and TFor() expressions.
  8202. At first, a module filter will receive a Iterator<T> and Iterable<T> interface, which will be implemented
  8203. if hasNext(), next() or iterator() fields are detected with the correct type.
  8204. At this part a custom function will be called which can adequate the class fields so they are compatible with
  8205. native Iterators as well
  8206. The expression filter part of this module will look for TFor() expressions, and transform like that:
  8207. for (anInt in value.iterator())
  8208. {
  8209. }
  8210. {
  8211. var s:haxe.lang.Iterator<Int> = ExternalFunction.getIterator(value.iterator());
  8212. while (s.hasNext())
  8213. {
  8214. var anInt:Int = s.next();
  8215. }
  8216. }
  8217. dependencies:
  8218. None.
  8219. *)
  8220. module IteratorsInterface =
  8221. struct
  8222. let name = "iterators_interface"
  8223. (* TODO later
  8224. (* ******************************************* *)
  8225. (* IteratorsInterfaceModf *)
  8226. (* ******************************************* *)
  8227. (*
  8228. The module filter for Iterators Interface, which will implement the iterator/iterable interface on each
  8229. class that conforms with the typedefs Iterator<> and Iterable<>
  8230. It's a very simple module and it will rely on cast detection to work correctly. This is so that
  8231. when the
  8232. dependencies:
  8233. Must run at the Module Filters, so cast detection can detect a cast to the interface and we can
  8234. *)
  8235. module IteratorsInterfaceModf =
  8236. struct
  8237. let name = "iterators_interface_modf"
  8238. let conforms_cfs has_next next =
  8239. try (match follow has_next.cf_type with
  8240. | TFun([],ret) when
  8241. (match follow ret with | TAbstract({ a_path = ([], "Bool") }, []) -> () | _ -> raise Not_found) ->
  8242. ()
  8243. | _ -> raise Not_found);
  8244. (match follow next.cf_type with
  8245. | TFun([], ret) -> ret
  8246. | _ -> raise Not_found
  8247. )
  8248. let conforms_type_iterator t =
  8249. try match follow t with
  8250. | TInst(cl,params) ->
  8251. let has_next = PMap.find "hasNext" cl.cl_fields in
  8252. let next = PMap.find "next" cl.cl_fields in
  8253. Some (conforms_cfs has_next next)
  8254. | TAnon(anon) ->
  8255. let has_next = PMap.find "hasNext" anon.a_fields in
  8256. let next = PMap.find "next" anon.a_fields in
  8257. Some (conforms_cfs has_next next)
  8258. | _ -> None
  8259. with | Not_found -> None
  8260. let conforms_as_iterable cl =
  8261. try
  8262. let iterator = PMap.find "iterator" cl.cl_fields in
  8263. match follow iterator.cf_type with
  8264. | TFun([], ret) -> conforms_type_iterator ret
  8265. | _ -> None
  8266. with | Not_found -> None
  8267. let conforms_as_iterator cl =
  8268. try
  8269. let has_next = PMap.find "hasNext" cl.cl_fields in
  8270. let next = PMap.find "next" cl.cl_fields in
  8271. Some (conforms_cfs has_next next)
  8272. with | Not_found -> None
  8273. let priority = solve_deps name []
  8274. let traverse gen iterator_iface iterable_iface on_found_iterator on_found_iterable =
  8275. let rec run md =
  8276. match md with
  8277. | TClassDecl cl when not cl.cl_extern && is_hxgen cl ->
  8278. let conforms_iterator = conforms_as_iterator cl in
  8279. let conforms_iterable = conforms_as_iterable cl in
  8280. if is_some conforms_iterator then begin
  8281. let it_t = get conforms_iterator in
  8282. cl.cl_interfaces <- (iterator_iface, [it_t]);
  8283. on_found_iterator cl
  8284. end;
  8285. if is_some conforms_iterable then begin
  8286. let it_t = get conforms_iterable in
  8287. cl.cl_interfaces <- (iterable_iface, [it_t]);
  8288. on_found_iterable cl
  8289. end;
  8290. md
  8291. | _ -> md
  8292. in
  8293. run
  8294. let configure gen (mapping_func:texpr->texpr) =
  8295. let map e = Some(mapping_func e) in
  8296. gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map
  8297. end;;
  8298. *)
  8299. (* ******************************************* *)
  8300. (* IteratorsInterfaceExprf *)
  8301. (* ******************************************* *)
  8302. (*
  8303. The expression filter for Iterators. Will look for TFor, transform it into
  8304. {
  8305. var iterator = // in expression here
  8306. while (iterator.hasNext())
  8307. {
  8308. var varName = iterator.next();
  8309. }
  8310. }
  8311. dependencies:
  8312. Must run before Dynamic fields access is run
  8313. *)
  8314. module IteratorsInterfaceExprf =
  8315. struct
  8316. let name = "iterators_interface_exprf"
  8317. let priority = solve_deps name [DBefore DynamicFieldAccess.priority]
  8318. let priority_as_synf = solve_deps name [DBefore DynamicFieldAccess.priority_as_synf]
  8319. let mk_access gen v name pos =
  8320. let field_t =
  8321. try match follow v.v_type with
  8322. | TInst(cl, params) ->
  8323. let field = PMap.find name cl.cl_fields in
  8324. apply_params cl.cl_params params field.cf_type
  8325. | TAnon(anon) ->
  8326. let field = PMap.find name anon.a_fields in
  8327. field.cf_type
  8328. | _ -> t_dynamic
  8329. with | Not_found -> t_dynamic
  8330. in
  8331. { (mk_field_access gen (mk_local v pos) name pos) with etype = field_t }
  8332. let traverse gen change_in_expr =
  8333. let basic = gen.gcon.basic in
  8334. let rec run e =
  8335. match e.eexpr with
  8336. | TFor(var, in_expr, block) ->
  8337. let in_expr = change_in_expr (run in_expr) in
  8338. let temp = mk_temp gen "iterator" in_expr.etype in
  8339. let block =
  8340. [
  8341. { eexpr = TVar(temp, Some(in_expr)); etype = basic.tvoid; epos = in_expr.epos };
  8342. {
  8343. eexpr = TWhile(
  8344. { eexpr = TCall(mk_access gen temp "hasNext" in_expr.epos, []); etype = basic.tbool; epos = in_expr.epos },
  8345. Type.concat ({
  8346. eexpr = TVar(var, Some({ eexpr = TCall(mk_access gen temp "next" in_expr.epos, []); etype = var.v_type; epos = in_expr.epos }));
  8347. etype = basic.tvoid;
  8348. epos = in_expr.epos
  8349. }) ( run block ),
  8350. Ast.NormalWhile);
  8351. etype = basic.tvoid;
  8352. epos = e.epos
  8353. }
  8354. ] in
  8355. { eexpr = TBlock(block); etype = e.etype; epos = e.epos }
  8356. | _ -> Type.map_expr run e
  8357. in
  8358. run
  8359. let configure gen (mapping_func:texpr->texpr) =
  8360. let map e = Some(mapping_func e) in
  8361. gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map
  8362. let configure_as_synf gen (mapping_func:texpr->texpr) =
  8363. let map e = Some(mapping_func e) in
  8364. gen.gexpr_filters#add ~name:name ~priority:(PCustom priority_as_synf) map
  8365. end;;
  8366. let configure gen change_in_expr =
  8367. IteratorsInterfaceExprf.configure gen (IteratorsInterfaceExprf.traverse gen change_in_expr)
  8368. let configure_as_synf gen change_in_expr =
  8369. IteratorsInterfaceExprf.configure_as_synf gen (IteratorsInterfaceExprf.traverse gen change_in_expr)
  8370. end;;
  8371. (* ******************************************* *)
  8372. (* SwitchToIf *)
  8373. (* ******************************************* *)
  8374. (*
  8375. Just a syntax filter which changes switch expressions to if() else if() else if() ...
  8376. It can be also an expression filter
  8377. dependencies:
  8378. *)
  8379. module SwitchToIf =
  8380. struct
  8381. let name = "switch_to_if"
  8382. let priority = solve_deps name []
  8383. let rec simplify_expr e = match e.eexpr with
  8384. | TParenthesis e
  8385. | TMeta(_,e) -> simplify_expr e
  8386. | _ -> e
  8387. let traverse gen (should_convert:texpr->bool) (handle_nullables:bool) =
  8388. let basic = gen.gcon.basic in
  8389. let rec run e =
  8390. match e.eexpr with
  8391. | TSwitch(cond,cases,default) when should_convert e ->
  8392. let cond_etype, should_cache = match handle_nullables, gen.gfollow#run_f cond.etype with
  8393. | true, TType({ t_path = ([], "Null") }, [t]) ->
  8394. let rec take_off_nullable t = match gen.gfollow#run_f t with
  8395. | TType({ t_path = ([], "Null") }, [t]) -> take_off_nullable t
  8396. | _ -> t
  8397. in
  8398. take_off_nullable t, true
  8399. | _, _ -> cond.etype, false
  8400. in
  8401. if should_cache && not (should_convert { e with eexpr = TSwitch({ cond with etype = cond_etype }, cases, default) }) then begin
  8402. { 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) }
  8403. end else begin
  8404. let local, fst_block = match cond.eexpr, should_cache with
  8405. | TLocal _, false -> cond, []
  8406. | _ ->
  8407. let var = mk_temp gen "switch" cond_etype in
  8408. let cond = run cond in
  8409. let cond = if should_cache then mk_cast cond_etype cond else cond in
  8410. mk_local var cond.epos, [ { eexpr = TVar(var,Some(cond)); etype = basic.tvoid; epos = cond.epos } ]
  8411. in
  8412. let mk_eq cond =
  8413. { eexpr = TBinop(Ast.OpEq, local, cond); etype = basic.tbool; epos = cond.epos }
  8414. in
  8415. let rec mk_many_cond conds =
  8416. match conds with
  8417. | cond :: [] ->
  8418. mk_eq cond
  8419. | cond :: tl ->
  8420. { eexpr = TBinop(Ast.OpBoolOr, mk_eq (run cond), mk_many_cond tl); etype = basic.tbool; epos = cond.epos }
  8421. | [] -> assert false
  8422. in
  8423. let mk_many_cond conds =
  8424. let ret = mk_many_cond conds in
  8425. (*
  8426. this might be considered a hack. But since we're on a syntax filter and
  8427. the condition is guaranteed to not have run twice, we can really run the
  8428. expr filters again for it (so to change e.g. OpEq accordingly
  8429. *)
  8430. gen.gexpr_filters#run_f ret
  8431. in
  8432. let rec loop cases = match cases with
  8433. | (conds,e) :: [] ->
  8434. { eexpr = TIf(mk_many_cond conds, run e, Option.map run default); etype = e.etype; epos = e.epos }
  8435. | (conds,e) :: tl ->
  8436. { eexpr = TIf(mk_many_cond conds, run e, Some(loop tl)); etype = e.etype; epos = e.epos }
  8437. | [] -> match default with
  8438. | None ->
  8439. raise Exit
  8440. | Some d -> run d
  8441. in
  8442. try
  8443. { e with eexpr = TBlock(fst_block @ [loop cases]) }
  8444. with | Exit ->
  8445. { e with eexpr = TBlock [] }
  8446. end
  8447. | TSwitch(cond,cases,default) -> (try
  8448. match (simplify_expr cond).eexpr with
  8449. | TCall( { eexpr = TField(_,FStatic({ cl_path = [],"Type" }, { cf_name = "enumIndex" })) }, [enum] ) ->
  8450. let real_enum = match enum.etype with
  8451. | TEnum(e,_) -> e
  8452. | _ -> raise Not_found
  8453. in
  8454. if Meta.has Meta.Class real_enum.e_meta then raise Not_found;
  8455. let enum_expr = mk_mt_access (TEnumDecl(real_enum)) e.epos in
  8456. let fields = Hashtbl.create (List.length real_enum.e_names) in
  8457. PMap.iter (fun _ ef -> Hashtbl.add fields ef.ef_index ef) real_enum.e_constrs;
  8458. let cases = List.map (fun (el,e) ->
  8459. List.map (fun e -> match e.eexpr with
  8460. | TConst(TInt i) ->
  8461. let ef = Hashtbl.find fields (Int32.to_int i) in
  8462. { e with eexpr = TField(enum_expr, FEnum(real_enum,ef)); etype = TEnum(real_enum,List.map (fun _ -> t_dynamic) real_enum.e_params) }
  8463. | _ -> raise Not_found) el, run e
  8464. ) cases in
  8465. { e with eexpr = TSwitch(enum,cases,Option.map run default) }
  8466. | _ -> raise Not_found
  8467. with Not_found -> Type.map_expr run e)
  8468. | _ -> Type.map_expr run e
  8469. in
  8470. run
  8471. let configure gen (mapping_func:texpr->texpr) =
  8472. let map e = Some(mapping_func e) in
  8473. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  8474. end;;
  8475. (* ******************************************* *)
  8476. (* Anonymous Class object handling *)
  8477. (* ******************************************* *)
  8478. (*
  8479. (syntax)
  8480. When we pass a class as an object, in some languages we will need a special construct to be able to
  8481. access its statics as if they were normal object fields. On C# and Java the way found to do that is
  8482. by handling statics reflection also by a normal instance. This also happens in hxcpp and neko, so I
  8483. guess it's a valid practice.
  8484. So if we want to handle the reflection of the static MyClass, here's roughly how it will be done:
  8485. var x = MyClass;
  8486. gets converted into
  8487. Haxe.Lang.Class x = Haxe.Lang.Runtime.GetType(typeof(MyClass).RuntimeHandle);
  8488. which will in turn look in its cache but roughly would do:
  8489. Haxe.Lang.Class x = new Haxe.Lang.Class(new MyClass(EmptyObject.EMPTY));
  8490. This module will of course let the caller choose how this will be implemented. It will just identify all
  8491. uses of class that will require it to be cast as an object.
  8492. dependencies:
  8493. *)
  8494. module ClassInstance =
  8495. struct
  8496. let priority = solve_deps "class_instance" []
  8497. let traverse gen (change_expr:texpr->module_type->texpr) =
  8498. let rec run e =
  8499. match e.eexpr with
  8500. | TCall( ({ eexpr = TLocal({ v_name = ("__is__" | "__as__" | "__typeof__") } as v) } as local), calls ) when Hashtbl.mem gen.gspecial_vars v.v_name ->
  8501. { e with eexpr = TCall(local, List.map (fun e ->
  8502. match e.eexpr with
  8503. | TTypeExpr _ -> e
  8504. | _ -> run e) calls) }
  8505. | TField({ eexpr = TTypeExpr(mt) }, f) ->
  8506. e
  8507. | TField(ef, f) ->
  8508. (match anon_class ef.etype with
  8509. | None -> Type.map_expr run e
  8510. | Some t ->
  8511. { e with eexpr = TField( { ef with eexpr = TTypeExpr(t) }, f) }
  8512. )
  8513. | TTypeExpr(mt) -> change_expr e mt
  8514. | _ -> Type.map_expr run e
  8515. in
  8516. run
  8517. let configure gen (mapping_func:texpr->texpr) =
  8518. let map e = Some(mapping_func e) in
  8519. gen.gsyntax_filters#add ~name:"class_instance" ~priority:(PCustom priority) map
  8520. end;;
  8521. (* ******************************************* *)
  8522. (* HardNullableSynf *)
  8523. (* ******************************************* *)
  8524. (*
  8525. This module will handle Null<T> types for languages that offer a way of dealing with
  8526. stack-allocated structures or tuples and generics. Essentialy on those targets a Null<T>
  8527. will be a tuple ( 'a * bool ), where bool is whether the value is null or not.
  8528. At first (configure-time), we will modify the follow function so it can follow correctly nested Null<Null<T>>,
  8529. and do not follow Null<T> to its underlying type
  8530. Then we will run a syntax filter, which will look for casts to Null<T> and replace them by
  8531. a call to the new Null<T> creation;
  8532. Also casts from Null<T> to T or direct uses of Null<T> (call, field access, array access, closure)
  8533. will result in the actual value being accessed
  8534. For compatibility with the C# target, HardNullable will accept both Null<T> and haxe.lang.Null<T> types
  8535. dependencies:
  8536. Needs to be run after all cast detection modules
  8537. *)
  8538. module HardNullableSynf =
  8539. struct
  8540. let name = "hard_nullable"
  8541. let priority = solve_deps name [DAfter CastDetect.ReturnCast.priority]
  8542. let rec is_null_t gen t = match gen.greal_type t with
  8543. | TType( { t_path = ([], "Null") }, [of_t])
  8544. | TInst( { cl_path = (["haxe";"lang"], "Null") }, [of_t]) ->
  8545. let rec take_off_null t =
  8546. match is_null_t gen t with | None -> t | Some s -> take_off_null s
  8547. in
  8548. Some (take_off_null of_t)
  8549. | TMono r -> (match !r with | Some t -> is_null_t gen t | None -> None)
  8550. | TLazy f -> is_null_t gen (!f())
  8551. | TType (t, tl) ->
  8552. is_null_t gen (apply_params t.t_params tl t.t_type)
  8553. | _ -> None
  8554. let follow_addon gen t =
  8555. let rec strip_off_nullable t =
  8556. let t = gen.gfollow#run_f t in
  8557. match t with
  8558. (* haxe.lang.Null<haxe.lang.Null<>> wouldn't be a valid construct, so only follow Null<> *)
  8559. | TType ( { t_path = ([], "Null") }, [of_t] ) -> strip_off_nullable of_t
  8560. | _ -> t
  8561. in
  8562. match t with
  8563. | TType( ({ t_path = ([], "Null") } as tdef), [of_t]) ->
  8564. Some( TType(tdef, [ strip_off_nullable of_t ]) )
  8565. | _ -> None
  8566. let traverse gen unwrap_null wrap_val null_to_dynamic has_value opeq_handler handle_opeq handle_cast =
  8567. (* let unwrap_null e = *)
  8568. (* let ret = unwrap_null e in *)
  8569. (* { ret with eexpr = TParenthesis(ret) } *)
  8570. (* in *)
  8571. (* let wrap_val e t b = *)
  8572. (* let ret = wrap_val e t b in *)
  8573. (* { ret with eexpr = TParenthesis(ret) } *)
  8574. (* in *)
  8575. let is_string t = match gen.greal_type t with
  8576. | TInst({ cl_path=([],"String") },_) -> true
  8577. | _ -> false
  8578. in
  8579. let handle_unwrap to_t e =
  8580. let e_null_t = get (is_null_t gen e.etype) in
  8581. match gen.greal_type to_t with
  8582. | TDynamic _ | TMono _ | TAnon _ ->
  8583. (match e_null_t with
  8584. | TDynamic _ | TMono _ | TAnon _ ->
  8585. gen.ghandle_cast to_t e_null_t (unwrap_null e)
  8586. | _ -> null_to_dynamic e
  8587. )
  8588. | _ ->
  8589. gen.ghandle_cast to_t e_null_t (unwrap_null e)
  8590. in
  8591. let handle_wrap e t =
  8592. match e.eexpr with
  8593. | TConst(TNull) ->
  8594. wrap_val e t false
  8595. | _ ->
  8596. wrap_val e t true
  8597. in
  8598. let is_null_t = is_null_t gen in
  8599. let cur_block = ref [] in
  8600. let add_tmp v e p =
  8601. cur_block := { eexpr = TVar(v,e); etype = gen.gcon.basic.tvoid; epos = p } :: !cur_block
  8602. in
  8603. let get_local e = match e.eexpr with
  8604. | TLocal _ ->
  8605. e, e
  8606. | _ ->
  8607. let v = mk_temp gen "nulltmp" e.etype in
  8608. add_tmp v (Some (null e.etype e.epos)) e.epos;
  8609. let local = { e with eexpr = TLocal(v) } in
  8610. mk_paren { e with eexpr = TBinop(Ast.OpAssign, local, e) }, local
  8611. in
  8612. let rec run e =
  8613. match e.eexpr with
  8614. | TBlock(bl) ->
  8615. let lst = !cur_block in
  8616. cur_block := [];
  8617. List.iter (fun e ->
  8618. let e = run e in
  8619. cur_block := (e :: !cur_block)
  8620. ) bl;
  8621. let ret = !cur_block in
  8622. cur_block := lst;
  8623. { e with eexpr = TBlock(List.rev ret) }
  8624. | TCast(v, _) ->
  8625. let null_et = is_null_t e.etype in
  8626. let null_vt = is_null_t v.etype in
  8627. (match null_vt, null_et with
  8628. | Some(vt), None when is_string e.etype ->
  8629. let v = run v in
  8630. { e with eexpr = TCast(null_to_dynamic v,None) }
  8631. | Some(vt), None ->
  8632. (match v.eexpr with
  8633. (* is there an unnecessary cast to Nullable? *)
  8634. | TCast(v2, _) ->
  8635. run { v with etype = e.etype }
  8636. | _ ->
  8637. handle_unwrap e.etype (run v)
  8638. )
  8639. | None, Some(et) ->
  8640. handle_wrap (run v) et
  8641. | Some(vt), Some(et) when handle_cast ->
  8642. handle_wrap (gen.ghandle_cast et vt (handle_unwrap vt (run v))) et
  8643. | Some(vt), Some(et) when not (type_iseq (run_follow gen vt) (run_follow gen et)) ->
  8644. (* check if has value and convert *)
  8645. let vlocal_fst, vlocal = get_local (run v) in
  8646. {
  8647. eexpr = TIf(
  8648. has_value vlocal_fst,
  8649. handle_wrap (mk_cast et (unwrap_null vlocal)) et,
  8650. Some( handle_wrap (null et e.epos) et ));
  8651. etype = e.etype;
  8652. epos = e.epos
  8653. }
  8654. | _ ->
  8655. Type.map_expr run e
  8656. )
  8657. | TField(ef, field) when is_some (is_null_t ef.etype) ->
  8658. let to_t = get (is_null_t ef.etype) in
  8659. { e with eexpr = TField(handle_unwrap to_t (run ef), field) }
  8660. | TCall(ecall, params) when is_some (is_null_t ecall.etype) ->
  8661. let to_t = get (is_null_t ecall.etype) in
  8662. { e with eexpr = TCall(handle_unwrap to_t (run ecall), List.map run params) }
  8663. | TArray(earray, p) when is_some (is_null_t earray.etype) ->
  8664. let to_t = get (is_null_t earray.etype) in
  8665. { e with eexpr = TArray(handle_unwrap to_t (run earray), p) }
  8666. | TBinop(op, e1, e2) ->
  8667. let e1_t = is_null_t e1.etype in
  8668. let e2_t = is_null_t e2.etype in
  8669. (match op with
  8670. | Ast.OpAssign
  8671. | Ast.OpAssignOp _ ->
  8672. (match e1_t, e2_t with
  8673. | Some t1, Some t2 ->
  8674. (match op with
  8675. | Ast.OpAssign ->
  8676. Type.map_expr run e
  8677. | Ast.OpAssignOp op ->
  8678. (match e1.eexpr with
  8679. | TLocal _ ->
  8680. { e with eexpr = TBinop( Ast.OpAssign, e1, handle_wrap { e with eexpr = TBinop (op, handle_unwrap t1 e1, handle_unwrap t2 (run e2) ) } t1 ) }
  8681. | _ ->
  8682. let v, e1, evars = match e1.eexpr with
  8683. | TField(ef, f) ->
  8684. let v = mk_temp gen "nullbinop" ef.etype in
  8685. v, { e1 with eexpr = TField(mk_local v ef.epos, f) }, ef
  8686. | _ ->
  8687. let v = mk_temp gen "nullbinop" e1.etype in
  8688. v, mk_local v e1.epos, e1
  8689. in
  8690. { e with eexpr = TBlock([
  8691. { eexpr = TVar(v, Some evars); etype = gen.gcon.basic.tvoid; epos = e.epos };
  8692. { e with eexpr = TBinop( Ast.OpAssign, e1, handle_wrap { e with eexpr = TBinop (op, handle_unwrap t1 e1, handle_unwrap t2 (run e2) ) } t1 ) }
  8693. ]) }
  8694. )
  8695. | _ -> assert false
  8696. )
  8697. | _ ->
  8698. Type.map_expr run e (* casts are already dealt with normal CastDetection module *)
  8699. )
  8700. | Ast.OpEq | Ast.OpNotEq when not handle_opeq ->
  8701. Type.map_expr run e
  8702. | Ast.OpEq | Ast.OpNotEq ->
  8703. (match e1.eexpr, e2.eexpr with
  8704. | TConst(TNull), _ when is_some e2_t ->
  8705. let e = has_value (run e2) in
  8706. if op = Ast.OpEq then
  8707. { e with eexpr = TUnop(Ast.Not, Ast.Prefix, e) }
  8708. else
  8709. e
  8710. | _, TConst(TNull) when is_some e1_t ->
  8711. let e = has_value (run e1) in
  8712. if op = Ast.OpEq then
  8713. { e with eexpr = TUnop(Ast.Not, Ast.Prefix, e) }
  8714. else
  8715. e
  8716. | _ when is_some e1_t || is_some e2_t ->
  8717. let e1, e2 =
  8718. if not (is_some e1_t) then
  8719. run e2, handle_wrap (run e1) (get e2_t)
  8720. else if not (is_some e2_t) then
  8721. run e1, handle_wrap (run e2) (get e1_t)
  8722. else
  8723. run e1, run e2
  8724. in
  8725. let e = opeq_handler e1 e2 in
  8726. if op = Ast.OpEq then
  8727. { e with eexpr = TUnop(Ast.Not, Ast.Prefix, e) }
  8728. else
  8729. e
  8730. | _ ->
  8731. Type.map_expr run e
  8732. )
  8733. | Ast.OpAdd when is_string e1.etype || is_string e2.etype ->
  8734. let e1 = if is_some e1_t then
  8735. null_to_dynamic (run e1)
  8736. else
  8737. run e1
  8738. in
  8739. let e2 = if is_some e2_t then
  8740. null_to_dynamic (run e2)
  8741. else
  8742. run e2
  8743. in
  8744. let e_t = is_null_t e.etype in
  8745. if is_some e_t then
  8746. wrap_val { eexpr = TBinop(op,e1,e2); etype = get e_t; epos = e.epos } (get e_t) true
  8747. else
  8748. { e with eexpr = TBinop(op,e1,e2) }
  8749. | _ ->
  8750. let e1 = if is_some e1_t then
  8751. handle_unwrap (get e1_t) (run e1)
  8752. else run e1 in
  8753. let e2 = if is_some e2_t then
  8754. handle_unwrap (get e2_t) (run e2)
  8755. else
  8756. run e2 in
  8757. (* if it is Null<T>, we need to convert the result again to null *)
  8758. let e_t = (is_null_t e.etype) in
  8759. if is_some e_t then
  8760. wrap_val { eexpr = TBinop(op, e1, e2); etype = get e_t; epos = e.epos } (get e_t) true
  8761. else
  8762. { e with eexpr = TBinop(op, e1, e2) }
  8763. )
  8764. (*| TUnop( (Ast.Increment as op)*)
  8765. | _ -> Type.map_expr run e
  8766. in
  8767. let run e = match e.eexpr with
  8768. | TFunction tf ->
  8769. run { e with eexpr = TFunction { tf with tf_expr = mk_block tf.tf_expr } }
  8770. | TBlock _ ->
  8771. run e
  8772. | _ -> match run (mk_block e) with
  8773. | { eexpr = TBlock([e]) } -> e
  8774. | e -> e
  8775. in
  8776. run
  8777. let configure gen (mapping_func:texpr->texpr) =
  8778. gen.gfollow#add ~name:(name ^ "_follow") (follow_addon gen);
  8779. let map e = Some(mapping_func e) in
  8780. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  8781. end;;
  8782. (* ******************************************* *)
  8783. (* ArrayDeclSynf *)
  8784. (* ******************************************* *)
  8785. (*
  8786. A syntax filter that will change array declarations to the actual native array declarations plus
  8787. the haxe array initialization
  8788. dependencies:
  8789. Must run after ObjectDeclMap since it can add TArrayDecl expressions
  8790. *)
  8791. module ArrayDeclSynf =
  8792. struct
  8793. let name = "array_decl_synf"
  8794. let priority = solve_deps name [DAfter ObjectDeclMap.priority]
  8795. let default_implementation gen native_array_cl =
  8796. let rec run e =
  8797. match e.eexpr with
  8798. | TArrayDecl el ->
  8799. let cl, params = match follow e.etype with
  8800. | TInst(({ cl_path = ([], "Array") } as cl), ( _ :: _ as params)) -> cl, params
  8801. | TInst(({ cl_path = ([], "Array") } as cl), []) -> cl, [t_dynamic]
  8802. | _ -> assert false
  8803. in
  8804. let changed_params = gen.greal_type_param (TClassDecl cl) params in
  8805. { e with eexpr = TNew(cl, changed_params, [ { e with eexpr = TArrayDecl(List.map run el); etype = TInst(native_array_cl, changed_params) } ] ); }
  8806. | _ -> Type.map_expr run e
  8807. in
  8808. run
  8809. let configure gen (mapping_func:texpr->texpr) =
  8810. let map e = Some(mapping_func e) in
  8811. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  8812. end;;
  8813. (* ******************************************* *)
  8814. (* SwitchBreakSynf *)
  8815. (* ******************************************* *)
  8816. (*
  8817. In most languages, 'break' is used as a statement also to break from switch statements.
  8818. This generates an incompatibility with haxe code, as we can use break to break from loops from inside a switch
  8819. This script will detect 'breaks' inside switch statements, and will offer the opportunity to change both
  8820. when this pattern is found.
  8821. Some options are possible:
  8822. On languages that support goto, 'break' may mean goto " after the loop ". There also can be special labels for
  8823. loops, so you can write "break label" (javascript, java, d)
  8824. On languages that do not support goto, a custom solution must be enforced
  8825. dependencies:
  8826. Since UnreachableCodeElimination must run before it, and Unreachable should be one of the
  8827. very last filters to run, we will make a fixed value which runs after UnreachableCodeElimination
  8828. (meaning: it's the very last filter)
  8829. *)
  8830. module SwitchBreakSynf =
  8831. struct
  8832. let name = "switch_break_synf"
  8833. let priority = min_dep -. 150.0
  8834. type add_to_block_api = texpr->bool->unit
  8835. let traverse gen (change_loop:texpr->int->add_to_block_api->texpr) (change_break:texpr->int->add_to_block_api->texpr) =
  8836. let in_switch = ref false in
  8837. let cur_block = ref [] in
  8838. let to_add = ref [] in
  8839. let did_found = ref (-1) in
  8840. let api expr before =
  8841. if before then cur_block := expr :: !cur_block else to_add := expr :: !to_add
  8842. in
  8843. let num = ref 0 in
  8844. let cur_num = ref 0 in
  8845. let rec run e =
  8846. match e.eexpr with
  8847. | TFunction _ ->
  8848. let old_num = !num in
  8849. num := 0;
  8850. let ret = Type.map_expr run e in
  8851. num := old_num;
  8852. ret
  8853. | TFor _
  8854. | TWhile _ ->
  8855. let last_switch = !in_switch in
  8856. let last_found = !did_found in
  8857. let last_num = !cur_num in
  8858. in_switch := false;
  8859. incr num;
  8860. cur_num := !num;
  8861. did_found := -1;
  8862. let new_e = Type.map_expr run e in (* assuming that no loop will be found in the condition *)
  8863. let new_e = if !did_found <> -1 then change_loop new_e !did_found api else new_e in
  8864. did_found := last_found;
  8865. in_switch := last_switch;
  8866. cur_num := last_num;
  8867. new_e
  8868. | TSwitch _ ->
  8869. let last_switch = !in_switch in
  8870. in_switch := true;
  8871. let new_e = Type.map_expr run e in
  8872. in_switch := last_switch;
  8873. new_e
  8874. | TBlock bl ->
  8875. let last_block = !cur_block in
  8876. let last_toadd = !to_add in
  8877. to_add := [];
  8878. cur_block := [];
  8879. List.iter (fun e ->
  8880. let new_e = run e in
  8881. cur_block := new_e :: !cur_block;
  8882. match !to_add with
  8883. | [] -> ()
  8884. | _ -> cur_block := !to_add @ !cur_block; to_add := []
  8885. ) bl;
  8886. let ret = List.rev !cur_block in
  8887. cur_block := last_block;
  8888. to_add := last_toadd;
  8889. { e with eexpr = TBlock(ret) }
  8890. | TBreak ->
  8891. if !in_switch then (did_found := !cur_num; change_break e !cur_num api) else e
  8892. | _ -> Type.map_expr run e
  8893. in
  8894. run
  8895. let configure gen (mapping_func:texpr->texpr) =
  8896. let map e = Some(mapping_func e) in
  8897. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  8898. end;;
  8899. (* ******************************************* *)
  8900. (* Unreachable Code Elimination *)
  8901. (* ******************************************* *)
  8902. (*
  8903. In some source code platforms, the code won't compile if there is Unreachable code, so this filter will take off any unreachable code.
  8904. If the parameter "handle_switch_break" is set to true, it will already add a "break" statement on switch cases when suitable;
  8905. in order to not confuse with while break, it will be a special expression __sbreak__
  8906. 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.
  8907. (Will respect __fallback__ expressions)
  8908. If the parameter "java_mode" is set to true, some additional checks following the java unreachable specs
  8909. (http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21) will be added
  8910. dependencies:
  8911. This must run before SwitchBreakSynf (see SwitchBreakSynf dependecy value)
  8912. 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
  8913. *)
  8914. module UnreachableCodeEliminationSynf =
  8915. struct
  8916. let name = "unreachable_synf"
  8917. let priority = min_dep -. 100.0
  8918. type uexpr_kind =
  8919. | Normal
  8920. | BreaksLoop
  8921. | BreaksFunction
  8922. let aggregate_kind e1 e2 =
  8923. match e1, e2 with
  8924. | Normal, _
  8925. | _, Normal -> Normal
  8926. | BreaksLoop, _
  8927. | _, BreaksLoop -> BreaksLoop
  8928. | BreaksFunction, BreaksFunction -> BreaksFunction
  8929. let aggregate_constant op c1 c2=
  8930. match op, c1, c2 with
  8931. | OpEq, Some v1, Some v2 -> Some (TBool (v1 = v2))
  8932. | OpNotEq, Some v1, Some v2 -> Some (TBool (v1 <> v2))
  8933. | OpBoolOr, Some (TBool v1) , Some (TBool v2) -> Some (TBool (v1 || v2))
  8934. | OpBoolAnd, Some (TBool v1) , Some (TBool v2) -> Some (TBool (v1 && v2))
  8935. | OpAssign, _, Some v2 -> Some v2
  8936. | _ -> None
  8937. let rec get_constant_expr e =
  8938. match e.eexpr with
  8939. | TConst (v) -> Some v
  8940. | TBinop(op, v1, v2) -> aggregate_constant op (get_constant_expr v1) (get_constant_expr v2)
  8941. | TParenthesis(e) | TMeta(_,e) -> get_constant_expr e
  8942. | _ -> None
  8943. let traverse gen should_warn handle_switch_break handle_not_final_returns java_mode =
  8944. let basic = gen.gcon.basic in
  8945. let do_warn =
  8946. if should_warn then gen.gcon.warning "Unreachable code" else (fun pos -> ())
  8947. in
  8948. let return_loop expr kind =
  8949. match kind with
  8950. | Normal | BreaksLoop -> expr, Normal
  8951. | _ -> expr, kind
  8952. in
  8953. let sbreak = alloc_var "__sbreak__" t_dynamic in
  8954. let mk_sbreak = mk_local sbreak in
  8955. let rec has_fallback expr = match expr.eexpr with
  8956. | TBlock(bl) -> (match List.rev bl with
  8957. | { eexpr = TLocal { v_name = "__fallback__" } } :: _ -> true
  8958. | ({ eexpr = TBlock(_) } as bl) :: _ -> has_fallback bl
  8959. | _ -> false)
  8960. | TLocal { v_name = "__fallback__" } -> true
  8961. | _ -> false
  8962. in
  8963. let handle_case = if handle_switch_break then
  8964. (fun (expr,kind) ->
  8965. match kind with
  8966. | Normal when has_fallback expr -> expr
  8967. | Normal -> Type.concat expr (mk_sbreak expr.epos)
  8968. | BreaksLoop | BreaksFunction -> expr
  8969. )
  8970. else
  8971. fst
  8972. in
  8973. let has_break = ref false in
  8974. let rec process_expr expr =
  8975. match expr.eexpr with
  8976. | TReturn _ | TThrow _ -> expr, BreaksFunction
  8977. | TContinue -> expr, BreaksLoop
  8978. | TBreak -> has_break := true; expr, BreaksLoop
  8979. | TCall( { eexpr = TLocal { v_name = "__goto__" } }, _ ) -> expr, BreaksLoop
  8980. | TBlock bl ->
  8981. let new_block = ref [] in
  8982. let is_unreachable = ref false in
  8983. let ret_kind = ref Normal in
  8984. List.iter (fun e ->
  8985. if !is_unreachable then
  8986. do_warn e.epos
  8987. else begin
  8988. let changed_e, kind = process_expr e in
  8989. new_block := changed_e :: !new_block;
  8990. match kind with
  8991. | BreaksLoop | BreaksFunction ->
  8992. ret_kind := kind;
  8993. is_unreachable := true
  8994. | _ -> ()
  8995. end
  8996. ) bl;
  8997. { expr with eexpr = TBlock(List.rev !new_block) }, !ret_kind
  8998. | TFunction tf ->
  8999. let changed, kind = process_expr tf.tf_expr in
  9000. let changed = if handle_not_final_returns && not (is_void tf.tf_type) && kind <> BreaksFunction then
  9001. Type.concat changed { eexpr = TReturn( Some (null tf.tf_type expr.epos) ); etype = basic.tvoid; epos = expr.epos }
  9002. else
  9003. changed
  9004. in
  9005. { expr with eexpr = TFunction({ tf with tf_expr = changed }) }, Normal
  9006. | TFor(var, cond, block) ->
  9007. let last_has_break = !has_break in
  9008. has_break := false;
  9009. let changed_block, _ = process_expr block in
  9010. has_break := last_has_break;
  9011. let expr = { expr with eexpr = TFor(var, cond, changed_block) } in
  9012. return_loop expr Normal
  9013. | TIf(cond, eif, None) ->
  9014. if java_mode then
  9015. match get_constant_expr cond with
  9016. | Some (TBool true) ->
  9017. process_expr eif
  9018. | _ ->
  9019. { expr with eexpr = TIf(cond, fst (process_expr eif), None) }, Normal
  9020. else
  9021. { expr with eexpr = TIf(cond, fst (process_expr eif), None) }, Normal
  9022. | TIf(cond, eif, Some eelse) ->
  9023. let eif, eif_k = process_expr eif in
  9024. let eelse, eelse_k = process_expr eelse in
  9025. let k = aggregate_kind eif_k eelse_k in
  9026. { expr with eexpr = TIf(cond, eif, Some eelse) }, k
  9027. | TWhile(cond, block, flag) ->
  9028. let last_has_break = !has_break in
  9029. has_break := false;
  9030. let block, k = process_expr block in
  9031. if java_mode then
  9032. match get_constant_expr cond, flag, !has_break with
  9033. | Some (TBool true), _, false ->
  9034. has_break := last_has_break;
  9035. { expr with eexpr = TWhile(cond, block, flag) }, BreaksFunction
  9036. | Some (TBool false), NormalWhile, _ ->
  9037. has_break := last_has_break;
  9038. do_warn expr.epos;
  9039. null expr.etype expr.epos, Normal
  9040. | _ ->
  9041. has_break := last_has_break;
  9042. return_loop { expr with eexpr = TWhile(cond,block,flag) } Normal
  9043. else begin
  9044. has_break := last_has_break;
  9045. return_loop { expr with eexpr = TWhile(cond,block,flag) } Normal
  9046. end
  9047. | TSwitch(cond, el_e_l, None) ->
  9048. { expr with eexpr = TSwitch(cond, List.map (fun (el, e) -> (el, handle_case (process_expr e))) el_e_l, None) }, Normal
  9049. | TSwitch(cond, el_e_l, Some def) ->
  9050. let def, k = process_expr def in
  9051. let def = handle_case (def, k) in
  9052. let k = ref k in
  9053. let ret = { expr with eexpr = TSwitch(cond, List.map (fun (el, e) ->
  9054. let e, ek = process_expr e in
  9055. k := aggregate_kind !k ek;
  9056. (el, handle_case (e, ek))
  9057. ) el_e_l, Some def) } in
  9058. ret, !k
  9059. (* | TMatch(cond, ep, il_vopt_e_l, None) ->
  9060. { 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 *)
  9061. (* | TMatch(cond, ep, il_vopt_e_l, Some def) ->
  9062. let def, k = process_expr def in
  9063. let def = handle_case (def, k) in
  9064. let k = ref k in
  9065. let ret = { expr with eexpr = TMatch(cond, ep, List.map (fun (il, vopt, e) ->
  9066. let e, ek = process_expr e in
  9067. k := aggregate_kind !k ek;
  9068. (il, vopt, handle_case (e, ek))
  9069. ) il_vopt_e_l, Some def) } in
  9070. ret, !k *)
  9071. | TTry (e, catches) ->
  9072. let e, k = process_expr e in
  9073. let k = ref k in
  9074. let ret = { expr with eexpr = TTry(e, List.map (fun (v, e) ->
  9075. let e, ek = process_expr e in
  9076. k := aggregate_kind !k ek;
  9077. (v, e)
  9078. ) catches) } in
  9079. ret, !k
  9080. | _ -> expr, Normal
  9081. in
  9082. let run e = fst (process_expr e) in
  9083. run
  9084. let configure gen (mapping_func:texpr->texpr) =
  9085. let map e = Some(mapping_func e) in
  9086. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  9087. end;;
  9088. (* ******************************************* *)
  9089. (* DefaultArguments *)
  9090. (* ******************************************* *)
  9091. (*
  9092. This Module Filter will go through all defined functions in all modules and change them
  9093. so they set all default arguments to be of a Nullable type, and adds the unroll from nullable to
  9094. the not-nullable type in the beginning of the function.
  9095. dependencies:
  9096. It must run before OverloadingCtors, since OverloadingCtors will change optional structures behavior
  9097. *)
  9098. module DefaultArguments =
  9099. struct
  9100. let name = "default_arguments"
  9101. let priority = solve_deps name [ DBefore OverloadingConstructor.priority ]
  9102. let gen_check basic t nullable_var const pos =
  9103. let is_null t = match t with TType({t_path = ([],"Null")}, _) -> true | _ -> false in
  9104. let needs_cast t1 t2 = match is_null t1, is_null t2 with
  9105. | true, false | false, true -> true
  9106. | _ -> false
  9107. in
  9108. let const_t = match const with
  9109. | TString _ -> basic.tstring | TInt _ -> basic.tint | TFloat _ -> basic.tfloat
  9110. | TNull -> t | TBool _ -> basic.tbool | _ -> assert false
  9111. in
  9112. let const = { eexpr = TConst(const); etype = const_t; epos = pos } in
  9113. let const = if needs_cast t const_t then mk_cast t const else const in
  9114. let arg = mk_local nullable_var pos in
  9115. let arg = if needs_cast t nullable_var.v_type then mk_cast t arg else arg in
  9116. {
  9117. eexpr = TIf(
  9118. { eexpr = TBinop(Ast.OpEq, mk_local nullable_var pos, null nullable_var.v_type pos); etype = basic.tbool; epos = pos },
  9119. const,
  9120. Some(arg)
  9121. );
  9122. etype = t;
  9123. epos = pos;
  9124. }
  9125. let add_opt gen block pos (var,opt) =
  9126. match opt with
  9127. | None | Some TNull -> (var,opt)
  9128. | Some (TString str) ->
  9129. block := Codegen.set_default gen.gcon var (TString str) pos :: !block;
  9130. (var, opt)
  9131. | Some const ->
  9132. let basic = gen.gcon.basic in
  9133. let nullable_var = mk_temp gen var.v_name (basic.tnull var.v_type) in
  9134. let orig_name = var.v_name in
  9135. var.v_name <- nullable_var.v_name;
  9136. nullable_var.v_name <- orig_name;
  9137. (* var v = (temp_var == null) ? const : cast temp_var; *)
  9138. block :=
  9139. {
  9140. eexpr = TVar(var, Some(gen_check basic var.v_type nullable_var const pos));
  9141. etype = basic.tvoid;
  9142. epos = pos;
  9143. } :: !block;
  9144. (nullable_var, opt)
  9145. let rec change_func gen cf =
  9146. List.iter (change_func gen) cf.cf_overloads;
  9147. let is_ctor = cf.cf_name = "new" in
  9148. let basic = gen.gcon.basic in
  9149. match cf.cf_kind, follow cf.cf_type with
  9150. | Var _, _ | Method MethDynamic, _ -> ()
  9151. | _, TFun(args, ret) ->
  9152. let found = ref false in
  9153. let args = ref (List.map (fun (n,opt,t) ->
  9154. (n,opt, if opt then (found := true; basic.tnull t) else t)
  9155. ) args) in
  9156. (match !found, cf.cf_expr with
  9157. | true, Some ({ eexpr = TFunction tf } as texpr) ->
  9158. let block = ref [] in
  9159. let tf_args = List.map (add_opt gen block tf.tf_expr.epos) tf.tf_args in
  9160. let arg_assoc = List.map2 (fun (v,o) (v2,_) -> v,(v2,o) ) tf.tf_args tf_args in
  9161. let rec extract_super e = match e.eexpr with
  9162. | TBlock(({ eexpr = TCall({ eexpr = TConst TSuper }, _) } as e2) :: tl) ->
  9163. e2, tl
  9164. | TBlock(hd :: tl) ->
  9165. let e2, tl2 = extract_super hd in
  9166. e2, tl2 @ tl
  9167. | _ -> raise Not_found
  9168. in
  9169. let block = try
  9170. if not is_ctor then raise Not_found;
  9171. (* issue #2570 *)
  9172. (* check if the class really needs the super as the first statement -
  9173. just to make sure we don't inadvertently break any existing code *)
  9174. let rec check cl =
  9175. if not (is_hxgen (TClassDecl cl)) then
  9176. ()
  9177. else match cl.cl_super with
  9178. | None ->
  9179. raise Not_found
  9180. | Some (cl,_) ->
  9181. check cl
  9182. in
  9183. (match gen.gcurrent_class with
  9184. | Some cl -> check cl
  9185. | _ -> ());
  9186. let super, tl = extract_super tf.tf_expr in
  9187. (match super.eexpr with
  9188. | TCall({ eexpr = TConst TSuper } as e1, args) ->
  9189. (* any super argument will be replaced by an inlined version of the check *)
  9190. let found = ref false in
  9191. let rec replace_args e = match e.eexpr with
  9192. | TLocal(v) -> (try
  9193. let v2,o = List.assq v arg_assoc in
  9194. let o = match o with
  9195. | None -> raise Not_found
  9196. | Some o -> o
  9197. in
  9198. found := true;
  9199. gen_check gen.gcon.basic v.v_type v2 o e.epos
  9200. with | Not_found -> e)
  9201. | _ -> Type.map_expr replace_args e
  9202. in
  9203. let args = List.map (replace_args) args in
  9204. { tf.tf_expr with eexpr = TBlock((if !found then { super with eexpr = TCall(e1,args) } else super) :: !block @ tl) }
  9205. | _ -> assert false)
  9206. with | Not_found ->
  9207. Type.concat { tf.tf_expr with eexpr = TBlock(!block); etype = basic.tvoid } tf.tf_expr
  9208. in
  9209. args := fun_args tf_args;
  9210. cf.cf_expr <- Some( {texpr with eexpr = TFunction( { tf with
  9211. tf_args = tf_args;
  9212. tf_expr = block
  9213. } ); etype = TFun(!args, ret) } );
  9214. cf.cf_type <- TFun(!args, ret)
  9215. | _ -> ()
  9216. );
  9217. (if !found then cf.cf_type <- TFun(!args, ret))
  9218. | _, _ -> assert false
  9219. let traverse gen =
  9220. let run md = match md with
  9221. | TClassDecl cl ->
  9222. List.iter (change_func gen) cl.cl_ordered_fields;
  9223. List.iter (change_func gen) cl.cl_ordered_statics;
  9224. (match cl.cl_constructor with | None -> () | Some cf -> change_func gen cf);
  9225. md
  9226. | _ -> md
  9227. in
  9228. run
  9229. let configure gen (mapping_func:module_type->module_type) =
  9230. let map md = Some(mapping_func md) in
  9231. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
  9232. end;;
  9233. (* ******************************************* *)
  9234. (* Interface Variables Removal Modf *)
  9235. (* ******************************************* *)
  9236. (*
  9237. This module filter will take care of sanitizing interfaces for targets that do not support
  9238. variables declaration in interfaces. By now this will mean that if anything is typed as the interface,
  9239. and a variable access is made, a FNotFound will be returned for the field_access, so
  9240. the field will be only accessible by reflection.
  9241. Speed-wise, ideally it would be best to create getProp/setProp functions in this case and change
  9242. the AST to call them when accessing by interface. (TODO)
  9243. But right now it will be accessed by reflection.
  9244. dependencies:
  9245. *)
  9246. module InterfaceVarsDeleteModf =
  9247. struct
  9248. let name = "interface_vars"
  9249. let priority = solve_deps name []
  9250. let run gen =
  9251. let run md = match md with
  9252. | TClassDecl ( { cl_interface = true } as cl ) ->
  9253. let to_add = ref [] in
  9254. let fields = List.filter (fun cf ->
  9255. match cf.cf_kind with
  9256. | Var _ when gen.gcon.platform = Cs && Meta.has Meta.Event cf.cf_meta ->
  9257. true
  9258. | Var vkind when not (Type.is_extern_field cf && Meta.has Meta.Property cf.cf_meta) ->
  9259. (match vkind.v_read with
  9260. | AccCall ->
  9261. let newcf = mk_class_field ("get_" ^ cf.cf_name) (TFun([],cf.cf_type)) true cf.cf_pos (Method MethNormal) [] in
  9262. to_add := newcf :: !to_add;
  9263. | _ -> ()
  9264. );
  9265. (match vkind.v_write with
  9266. | AccCall ->
  9267. 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
  9268. to_add := newcf :: !to_add;
  9269. | _ -> ()
  9270. );
  9271. cl.cl_fields <- PMap.remove cf.cf_name cl.cl_fields;
  9272. false
  9273. | Method MethDynamic ->
  9274. (* TODO OPTIMIZATION - add a `_dispatch` method to the interface which will call the dynamic function itself *)
  9275. cl.cl_fields <- PMap.remove cf.cf_name cl.cl_fields;
  9276. false
  9277. | _ -> true
  9278. ) cl.cl_ordered_fields in
  9279. cl.cl_ordered_fields <- fields;
  9280. List.iter (fun cf ->
  9281. match field_access gen (TInst(cl,List.map snd cl.cl_params)) cf.cf_name with
  9282. | FNotFound | FDynamicField _ ->
  9283. cl.cl_ordered_fields <- cf :: cl.cl_ordered_fields;
  9284. cl.cl_fields <- PMap.add cf.cf_name cf cl.cl_fields
  9285. | _ -> ()
  9286. ) !to_add;
  9287. md
  9288. | _ -> md
  9289. in
  9290. run
  9291. let configure gen =
  9292. let run = run gen in
  9293. let map md = Some(run md) in
  9294. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
  9295. end;;
  9296. (* ******************************************* *)
  9297. (* InterfaceProps *)
  9298. (* ******************************************* *)
  9299. (*
  9300. This module filter will go through all declared properties, and see if they are conforming to a native interface.
  9301. If they are, it will add Meta.Property to it
  9302. dependencies:
  9303. *)
  9304. module InterfaceProps =
  9305. struct
  9306. let name = "interface_props"
  9307. let priority = solve_deps name []
  9308. let run gen =
  9309. let run md = match md with
  9310. | TClassDecl ( { cl_interface = false; cl_extern = false } as cl ) ->
  9311. let vars = List.fold_left (fun acc (iface,_) ->
  9312. if Meta.has Meta.CsNative iface.cl_meta then
  9313. List.filter (fun cf -> match cf.cf_kind with
  9314. | Var { v_read = AccCall } | Var { v_write = AccCall } ->
  9315. true
  9316. | _ -> false
  9317. ) iface.cl_ordered_fields @ acc
  9318. else
  9319. acc
  9320. ) [] cl.cl_implements in
  9321. let vars = List.map (fun cf -> cf.cf_name) vars in
  9322. if vars <> [] then
  9323. List.iter (fun cf -> match cf.cf_kind with
  9324. | Var { v_read = AccCall } | Var { v_write = AccCall } when List.mem cf.cf_name vars ->
  9325. cf.cf_meta <- (Meta.Property, [], Ast.null_pos) :: cf.cf_meta
  9326. | _ -> ()
  9327. ) cl.cl_ordered_fields;
  9328. md
  9329. | _ -> md
  9330. in
  9331. run
  9332. let configure gen =
  9333. let run = run gen in
  9334. let map md = Some(run md) in
  9335. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
  9336. end;;
  9337. (* ******************************************* *)
  9338. (* Int Division Synf *)
  9339. (* ******************************************* *)
  9340. (*
  9341. On targets that support int division, this module will force a float division to be performed,
  9342. so compatibility with current haxe targets is ensured.
  9343. If catch_int_div is set to true, though, it will look for casts to int or use of Std.int() to optimize
  9344. this kind of operation.
  9345. dependencies:
  9346. since it depends on nothing, but many modules might generate division expressions,
  9347. it will be one of the last modules to run
  9348. *)
  9349. module IntDivisionSynf =
  9350. struct
  9351. let name = "int_division_synf"
  9352. let priority = solve_deps name [ DAfter ExpressionUnwrap.priority; DAfter ObjectDeclMap.priority; DAfter ArrayDeclSynf.priority ]
  9353. let is_int = like_int
  9354. let is_exactly_int t = match follow t with
  9355. | TAbstract ({ a_path=[],"Int" }, []) -> true
  9356. | _ -> false
  9357. let default_implementation gen catch_int_div =
  9358. let basic = gen.gcon.basic in
  9359. let rec run e =
  9360. match e.eexpr with
  9361. | TBinop((Ast.OpDiv as op), e1, e2) when is_int e1.etype && is_int e2.etype ->
  9362. { e with eexpr = TBinop(op, mk_cast basic.tfloat (run e1), run e2) }
  9363. | TCall(
  9364. { eexpr = TField(_, FStatic({ cl_path = ([], "Std") }, { cf_name = "int" })) },
  9365. [ ({ eexpr = TBinop((Ast.OpDiv as op), e1, e2) } as ebinop ) ]
  9366. ) when catch_int_div && is_int e1.etype && is_int e2.etype ->
  9367. let e = { ebinop with eexpr = TBinop(op, run e1, run e2); etype = basic.tint } in
  9368. if not (is_exactly_int e1.etype && is_exactly_int e2.etype) then
  9369. mk_cast basic.tint e
  9370. else
  9371. e
  9372. | TCast( ({ eexpr = TBinop((Ast.OpDiv as op), e1, e2) } as ebinop ), _ )
  9373. | 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 ->
  9374. let ret = { ebinop with eexpr = TBinop(op, run e1, run e2); etype = e.etype } in
  9375. if not (is_exactly_int e1.etype && is_exactly_int e2.etype) then
  9376. mk_cast e.etype ret
  9377. else
  9378. e
  9379. | _ -> Type.map_expr run e
  9380. in
  9381. run
  9382. let configure gen (mapping_func:texpr->texpr) =
  9383. let map e = Some(mapping_func e) in
  9384. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  9385. end;;
  9386. (* ******************************************* *)
  9387. (* UnnecessaryCastsRemoval *)
  9388. (* ******************************************* *)
  9389. (*
  9390. This module will take care of simplifying unnecessary casts, specially those made by the compiler
  9391. when inlining. Right now, it will only take care of casts used as a statement, which are always useless;
  9392. TODO: Take care of more cases, e.g. when the to and from types are the same
  9393. dependencies:
  9394. This must run after CastDetection, but before ExpressionUnwrap
  9395. *)
  9396. module UnnecessaryCastsRemoval =
  9397. struct
  9398. let name = "casts_removal"
  9399. let priority = solve_deps name [DAfter CastDetect.priority; DBefore ExpressionUnwrap.priority]
  9400. let rec take_off_cast run e =
  9401. match e.eexpr with
  9402. | TCast (c, _) ->
  9403. take_off_cast run c
  9404. | _ -> run e
  9405. let default_implementation gen =
  9406. let rec traverse e =
  9407. match e.eexpr with
  9408. | TBlock bl ->
  9409. let bl = List.map (fun e ->
  9410. take_off_cast traverse e
  9411. ) bl in
  9412. { e with eexpr = TBlock bl }
  9413. | TTry (block, catches) ->
  9414. { e with eexpr = TTry(traverse (mk_block block), List.map (fun (v,block) -> (v, traverse (mk_block block))) catches) }
  9415. (* | TMatch (cond,ep,il_vol_e_l,default) ->
  9416. { 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) } *)
  9417. | TSwitch (cond,el_e_l, default) ->
  9418. { 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) }
  9419. | TWhile (cond,block,flag) ->
  9420. {e with eexpr = TWhile(cond,traverse (mk_block block), flag) }
  9421. | TIf (cond, eif, eelse) ->
  9422. { e with eexpr = TIf(cond, traverse (mk_block eif), Option.map (fun e -> traverse (mk_block e)) eelse) }
  9423. | TFor (v,it,block) ->
  9424. { e with eexpr = TFor(v,it, traverse (mk_block block)) }
  9425. | TFunction (tfunc) ->
  9426. { e with eexpr = TFunction({ tfunc with tf_expr = traverse (mk_block tfunc.tf_expr) }) }
  9427. | _ -> e (* if expression doesn't have a block, we will exit *)
  9428. in
  9429. traverse
  9430. let configure gen =
  9431. let map e = Some(default_implementation gen e) in
  9432. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  9433. end;;
  9434. (* ******************************************* *)
  9435. (* OverrideFix *)
  9436. (* ******************************************* *)
  9437. (*
  9438. When DCE is on, sometimes a field is marked as override when it
  9439. really doesn't override anything. This module filter will take care of this.
  9440. dependencies:
  9441. No dependencies
  9442. *)
  9443. module OverrideFix =
  9444. struct
  9445. let name = "override_fix"
  9446. let priority = solve_deps name []
  9447. let default_implementation gen =
  9448. let rec run e =
  9449. match e.eexpr with
  9450. | _ -> Type.map_expr run e
  9451. in
  9452. run
  9453. let configure gen =
  9454. let map md =
  9455. match md with
  9456. | TClassDecl cl ->
  9457. cl.cl_overrides <- List.filter (fun s ->
  9458. let rec loop cl =
  9459. match cl.cl_super with
  9460. | Some (cl,_) when PMap.mem s.cf_name cl.cl_fields -> true
  9461. | Some (cl,_) -> loop cl
  9462. | None -> false
  9463. in
  9464. loop cl
  9465. ) cl.cl_overrides;
  9466. Some md
  9467. | _ -> Some md
  9468. in
  9469. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
  9470. end;;
  9471. (* ******************************************* *)
  9472. (* AbstractImplementationFix *)
  9473. (* ******************************************* *)
  9474. (*
  9475. This module filter will map the compiler created classes from abstract
  9476. implementations to valid haxe code, as needed by gencommon
  9477. dependencies:
  9478. No dependencies
  9479. *)
  9480. module AbstractImplementationFix =
  9481. struct
  9482. let name = "abstract_implementation_fix"
  9483. let priority = solve_deps name []
  9484. let default_implementation gen =
  9485. let rec run md =
  9486. match md with
  9487. | TClassDecl ({ cl_kind = KAbstractImpl a } as c) ->
  9488. List.iter (function
  9489. | cf when Meta.has Meta.Impl cf.cf_meta ->
  9490. (* add type parameters to all implementation functions *)
  9491. cf.cf_params <- cf.cf_params @ a.a_params
  9492. | _ -> ()
  9493. ) c.cl_ordered_statics;
  9494. Some md
  9495. | _ -> Some md
  9496. in
  9497. run
  9498. let configure gen =
  9499. let map = default_implementation gen in
  9500. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
  9501. end;;
  9502. (* ******************************************* *)
  9503. (* FixOverrides *)
  9504. (* ******************************************* *)
  9505. (*
  9506. Covariant return types, contravariant function arguments and applied type parameters may change
  9507. in a way that expected implementations / overrides aren't recognized as such.
  9508. This filter will fix that.
  9509. dependencies:
  9510. FixOverrides expects that the target platform is able to deal with overloaded functions
  9511. It must run after DefaultArguments, otherwise code added by the default arguments may be invalid
  9512. *)
  9513. module FixOverrides =
  9514. struct
  9515. let name = "fix_overrides"
  9516. let priority = solve_deps name [DAfter DefaultArguments.priority]
  9517. (*
  9518. if the platform allows explicit interface implementation (C#),
  9519. specify a explicit_fn_name function (tclass->string->string)
  9520. Otherwise, it expects the platform to be able to handle covariant return types
  9521. *)
  9522. let run ~explicit_fn_name gen =
  9523. let implement_explicitly = is_some explicit_fn_name in
  9524. let run md = match md with
  9525. | TClassDecl ( { cl_interface = true; cl_extern = false } as c ) ->
  9526. (* overrides can be removed from interfaces *)
  9527. c.cl_ordered_fields <- List.filter (fun f ->
  9528. try
  9529. if Meta.has Meta.Overload f.cf_meta then raise Not_found;
  9530. let f2 = Codegen.find_field gen.gcon c f in
  9531. if f2 == f then raise Not_found;
  9532. c.cl_fields <- PMap.remove f.cf_name c.cl_fields;
  9533. false;
  9534. with Not_found ->
  9535. true
  9536. ) c.cl_ordered_fields;
  9537. md
  9538. | TClassDecl({ cl_extern = false } as c) ->
  9539. let this = { eexpr = TConst TThis; etype = TInst(c,List.map snd c.cl_params); epos = c.cl_pos } in
  9540. (* look through all interfaces, and try to find a type that applies exactly *)
  9541. let rec loop_iface (iface:tclass) itl =
  9542. List.iter (fun (s,stl) -> loop_iface s (List.map (apply_params iface.cl_params itl) stl)) iface.cl_implements;
  9543. let real_itl = gen.greal_type_param (TClassDecl iface) itl in
  9544. let rec loop_f f =
  9545. List.iter loop_f f.cf_overloads;
  9546. let ftype = apply_params iface.cl_params itl f.cf_type in
  9547. let real_ftype = get_real_fun gen (apply_params iface.cl_params real_itl f.cf_type) in
  9548. replace_mono real_ftype;
  9549. let overloads = Typeload.get_overloads c f.cf_name in
  9550. try
  9551. let t2, f2 =
  9552. match overloads with
  9553. | (_, cf) :: _ when Meta.has Meta.Overload cf.cf_meta -> (* overloaded function *)
  9554. (* try to find exact function *)
  9555. List.find (fun (t,f2) ->
  9556. Typeload.same_overload_args ftype t f f2
  9557. ) overloads
  9558. | _ :: _ ->
  9559. (match field_access gen (TInst(c, List.map snd c.cl_params)) f.cf_name with
  9560. | FClassField(_,_,_,f2,false,t,_) -> t,f2 (* if it's not an overload, all functions should have the same signature *)
  9561. | _ -> raise Not_found)
  9562. | [] -> raise Not_found
  9563. in
  9564. replace_mono t2;
  9565. (* if we find a function with the exact type of real_ftype, it means this interface has already been taken care of *)
  9566. if not (type_iseq (get_real_fun gen (apply_params f2.cf_params (List.map snd f.cf_params) t2)) real_ftype) then begin
  9567. (match f.cf_kind with | Method (MethNormal | MethInline) -> () | _ -> raise Not_found);
  9568. let t2 = get_real_fun gen t2 in
  9569. if List.length f.cf_params <> List.length f2.cf_params then raise Not_found;
  9570. replace_mono t2;
  9571. match follow (apply_params f2.cf_params (List.map snd f.cf_params) t2), follow real_ftype with
  9572. | TFun(a1,r1), TFun(a2,r2) when not implement_explicitly && not (type_iseq r1 r2) && Typeload.same_overload_args real_ftype t2 f f2 ->
  9573. (* different return types are the trickiest cases to deal with *)
  9574. (* check for covariant return type *)
  9575. let is_covariant = match follow r1, follow r2 with
  9576. | _, TDynamic _ -> true
  9577. | r1, r2 -> try
  9578. unify r1 r2;
  9579. true
  9580. with | Unify_error _ -> false
  9581. in
  9582. (* we only have to worry about non-covariant issues *)
  9583. if not is_covariant then begin
  9584. (* override return type and cast implemented function *)
  9585. let args, newr = match follow t2, follow (apply_params f.cf_params (List.map snd f2.cf_params) real_ftype) with
  9586. | TFun(a,_), TFun(_,r) -> a,r
  9587. | _ -> assert false
  9588. in
  9589. f2.cf_type <- TFun(args,newr);
  9590. (match f2.cf_expr with
  9591. | Some ({ eexpr = TFunction tf } as e) ->
  9592. f2.cf_expr <- Some { e with eexpr = TFunction { tf with tf_type = newr } }
  9593. | _ -> ())
  9594. end
  9595. | TFun(a1,r1), TFun(a2,r2) ->
  9596. (* just implement a function that will call the main one *)
  9597. let name, is_explicit = match explicit_fn_name with
  9598. | Some fn when not (type_iseq r1 r2) && Typeload.same_overload_args real_ftype t2 f f2 ->
  9599. fn iface itl f.cf_name, true
  9600. | _ -> f.cf_name, false
  9601. in
  9602. let p = f2.cf_pos in
  9603. let newf = mk_class_field name real_ftype true f.cf_pos (Method MethNormal) f.cf_params in
  9604. let vars = List.map (fun (n,_,t) -> alloc_var n t) a2 in
  9605. let args = List.map2 (fun v (_,_,t) -> mk_cast t (mk_local v f2.cf_pos)) vars a1 in
  9606. let field = { eexpr = TField(this, FInstance(c,List.map snd c.cl_params,f2)); etype = TFun(a1,r1); epos = p } in
  9607. let call = { eexpr = TCall(field, args); etype = r1; epos = p } in
  9608. (* let call = gen.gparam_func_call call field (List.map snd f.cf_params) args in *)
  9609. let is_void = is_void r2 in
  9610. newf.cf_expr <- Some {
  9611. eexpr = TFunction({
  9612. tf_args = List.map (fun v -> v,None) vars;
  9613. tf_type = r2;
  9614. tf_expr = (if is_void then call else {
  9615. eexpr = TReturn (Some (mk_cast r2 call));
  9616. etype = r2;
  9617. epos = p
  9618. })
  9619. });
  9620. etype = real_ftype;
  9621. epos = p;
  9622. };
  9623. (* delayed: add to class *)
  9624. let delay () =
  9625. try
  9626. let fm = PMap.find f.cf_name c.cl_fields in
  9627. fm.cf_overloads <- newf :: fm.cf_overloads
  9628. with | Not_found ->
  9629. c.cl_fields <- PMap.add f.cf_name newf c.cl_fields;
  9630. c.cl_ordered_fields <- newf :: c.cl_ordered_fields
  9631. in
  9632. (* gen.gafter_filters_ended <- delay :: gen.gafter_filters_ended *)
  9633. delay();
  9634. | _ -> assert false
  9635. end
  9636. with | Not_found -> ()
  9637. in
  9638. List.iter (fun f -> match f.cf_kind with | Var _ -> () | _ -> loop_f f) iface.cl_ordered_fields
  9639. in
  9640. List.iter (fun (iface,itl) -> loop_iface iface itl) c.cl_implements;
  9641. (* now go through all overrides, *)
  9642. let rec check_f f =
  9643. (* find the first declared field *)
  9644. let is_overload = Meta.has Meta.Overload f.cf_meta in
  9645. let decl = if is_overload then
  9646. find_first_declared_field gen c ~exact_field:f f.cf_name
  9647. else
  9648. find_first_declared_field gen c f.cf_name
  9649. in
  9650. match decl with
  9651. | Some(f2,actual_t,_,t,declared_cl,_,_)
  9652. when not (Typeload.same_overload_args actual_t (get_real_fun gen f.cf_type) f2 f) ->
  9653. if Meta.has Meta.Overload f.cf_meta then begin
  9654. (* if it is overload, create another field with the requested type *)
  9655. let f3 = mk_class_field f.cf_name t f.cf_public f.cf_pos f.cf_kind f.cf_params in
  9656. let p = f.cf_pos in
  9657. let old_args, old_ret = get_fun f.cf_type in
  9658. let args, ret = get_fun t in
  9659. let tf_args = List.map (fun (n,o,t) -> alloc_var n t, None) args in
  9660. let f3_mk_return = if is_void ret then (fun e -> e) else (fun e -> mk_return (mk_cast ret e)) in
  9661. f3.cf_expr <- Some {
  9662. eexpr = TFunction({
  9663. tf_args = tf_args;
  9664. tf_type = ret;
  9665. tf_expr = mk_block (f3_mk_return {
  9666. eexpr = TCall(
  9667. {
  9668. eexpr = TField(
  9669. { eexpr = TConst TThis; etype = TInst(c, List.map snd c.cl_params); epos = p },
  9670. FInstance(c,List.map snd c.cl_params,f));
  9671. etype = f.cf_type;
  9672. epos = p
  9673. },
  9674. List.map2 (fun (v,_) (_,_,t) -> mk_cast t (mk_local v p)) tf_args old_args);
  9675. etype = old_ret;
  9676. epos = p
  9677. })
  9678. });
  9679. etype = t;
  9680. epos = p;
  9681. };
  9682. gen.gafter_filters_ended <- ((fun () ->
  9683. f.cf_overloads <- f3 :: f.cf_overloads;
  9684. ) :: gen.gafter_filters_ended);
  9685. f3
  9686. end else begin match f.cf_expr with
  9687. | Some({ eexpr = TFunction(tf) } as e) ->
  9688. (* if it's not overload, just cast the vars *)
  9689. let actual_args, _ = get_fun (get_real_fun gen actual_t) in
  9690. let new_args, vardecl = List.fold_left2 (fun (args,vdecl) (v,_) (_,_,t) ->
  9691. if not (type_iseq (gen.greal_type v.v_type) (gen.greal_type t)) then begin
  9692. let new_var = mk_temp gen v.v_name t in
  9693. (new_var,None) :: args, (v, Some(mk_cast v.v_type (mk_local new_var f.cf_pos))) :: vdecl
  9694. end else
  9695. (v,None) :: args, vdecl
  9696. ) ([],[]) tf.tf_args actual_args in
  9697. if vardecl <> [] then
  9698. f.cf_expr <- Some({ e with
  9699. eexpr = TFunction({ tf with
  9700. tf_args = List.rev new_args;
  9701. 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
  9702. });
  9703. });
  9704. f
  9705. | _ -> f
  9706. end
  9707. | _ -> f
  9708. in
  9709. if not c.cl_extern then
  9710. c.cl_overrides <- List.map (fun f -> check_f f) c.cl_overrides;
  9711. md
  9712. | _ -> md
  9713. in
  9714. run
  9715. let configure ?explicit_fn_name gen =
  9716. let delay () =
  9717. Hashtbl.clear gen.greal_field_types
  9718. in
  9719. gen.gafter_mod_filters_ended <- delay :: gen.gafter_mod_filters_ended;
  9720. let run = run ~explicit_fn_name:explicit_fn_name gen in
  9721. let map md = Some(run md) in
  9722. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
  9723. end;;
  9724. (* ******************************************* *)
  9725. (* Normalize *)
  9726. (* ******************************************* *)
  9727. (*
  9728. - Filters out enum constructor type parameters from the AST; See Issue #1796
  9729. - Filters out monomorphs
  9730. - Filters out all non-whitelisted AST metadata
  9731. dependencies:
  9732. No dependencies; but it still should be one of the first filters to run,
  9733. as it will help normalize the AST
  9734. *)
  9735. module Normalize =
  9736. struct
  9737. let name = "normalize_type"
  9738. let priority = max_dep
  9739. let rec filter_param t = match t with
  9740. | TInst({ cl_kind = KTypeParameter _ } as c,_) when Meta.has Meta.EnumConstructorParam c.cl_meta ->
  9741. t_dynamic
  9742. | TMono r -> (match !r with
  9743. | None -> t_dynamic
  9744. | Some t -> filter_param t)
  9745. | TInst(_,[]) | TEnum(_,[]) | TType(_,[]) | TAbstract(_,[]) -> t
  9746. | TType(t,tl) -> TType(t,List.map filter_param tl)
  9747. | TInst(c,tl) -> TInst(c,List.map filter_param tl)
  9748. | TEnum(e,tl) -> TEnum(e,List.map filter_param tl)
  9749. | TAbstract({ a_path = (["haxe";"extern"],"Rest") } as a,tl) -> TAbstract(a, List.map filter_param tl)
  9750. | TAbstract(a,tl) when not (Meta.has Meta.CoreType a.a_meta) ->
  9751. filter_param (Abstract.get_underlying_type a tl)
  9752. | TAbstract(a,tl) -> TAbstract(a, List.map filter_param tl)
  9753. | TAnon a ->
  9754. TAnon {
  9755. a_fields = PMap.map (fun f -> { f with cf_type = filter_param f.cf_type }) a.a_fields;
  9756. a_status = a.a_status;
  9757. }
  9758. | TFun(args,ret) -> TFun(List.map (fun (n,o,t) -> (n,o,filter_param t)) args, filter_param ret)
  9759. | TDynamic _ -> t
  9760. | TLazy f -> filter_param (!f())
  9761. let default_implementation gen ~metas =
  9762. let rec run e =
  9763. match e.eexpr with
  9764. | TMeta(entry, e) when not (Hashtbl.mem metas entry) ->
  9765. run e
  9766. | _ ->
  9767. map_expr_type (fun e -> run e) filter_param (fun v -> v.v_type <- filter_param v.v_type; v) e
  9768. in
  9769. run
  9770. let default_implementation_module gen ~metas =
  9771. let rec run md = match md with
  9772. | TClassDecl cl ->
  9773. let rec map cf =
  9774. cf.cf_type <- filter_param cf.cf_type;
  9775. List.iter map cf.cf_overloads
  9776. in
  9777. List.iter map cl.cl_ordered_fields;
  9778. List.iter map cl.cl_ordered_statics;
  9779. Option.may map cl.cl_constructor;
  9780. md
  9781. | _ -> md
  9782. in
  9783. run
  9784. let configure gen ~metas =
  9785. let map e = Some(default_implementation gen e ~metas:metas) in
  9786. gen.gexpr_filters#add ~name:name ~priority:(PCustom priority) map;
  9787. let map md = Some(default_implementation_module gen ~metas md) in
  9788. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
  9789. end;;
  9790. (* ******************************************* *)
  9791. (* InterfaceMetas *)
  9792. (* ******************************************* *)
  9793. (*
  9794. Deal with metadata on interfaces by taking it off from interface, and adding a new class with `_HxMeta` suffix
  9795. dependencies:
  9796. Must run before InitFunction
  9797. *)
  9798. module InterfaceMetas =
  9799. struct
  9800. let name = "interface_metas"
  9801. let priority = solve_deps name [ DBefore InitFunction.priority ]
  9802. let traverse gen =
  9803. let run md = match md with
  9804. | TClassDecl ({ cl_interface = true; cl_ordered_statics = (_ :: _) } as cl) ->
  9805. cl.cl_ordered_statics <- [];
  9806. let path = fst cl.cl_path,snd cl.cl_path ^ "_HxMeta" in
  9807. (match Codegen.build_metadata gen.gcon (TClassDecl cl) with
  9808. | Some expr ->
  9809. let ncls = mk_class cl.cl_module path cl.cl_pos in
  9810. let cf = mk_class_field "__meta__" expr.etype false expr.epos (Var { v_read = AccNormal; v_write = AccNormal }) [] in
  9811. cf.cf_expr <- Some expr;
  9812. ncls.cl_statics <- PMap.add "__meta__" cf ncls.cl_statics;
  9813. ncls.cl_ordered_statics <- cf :: ncls.cl_ordered_statics;
  9814. gen.gadd_to_module (TClassDecl(ncls)) priority;
  9815. | _ -> ())
  9816. | _ -> ()
  9817. in
  9818. run
  9819. let configure gen =
  9820. let map md = traverse gen md; Some(md) in
  9821. gen.gmodule_filters#add ~name:name ~priority:(PCustom priority) map
  9822. end;;
  9823. (*
  9824. (* ******************************************* *)
  9825. (* Example *)
  9826. (* ******************************************* *)
  9827. (*
  9828. description
  9829. dependencies:
  9830. *)
  9831. module Example =
  9832. struct
  9833. let name = "example"
  9834. let priority = solve_deps name []
  9835. let default_implementation gen =
  9836. let rec run e =
  9837. match e.eexpr with
  9838. | _ -> Type.map_expr run e
  9839. in
  9840. run
  9841. let configure gen (mapping_func:texpr->texpr) =
  9842. let map e = Some(mapping_func e) in
  9843. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  9844. end;;
  9845. *)