BfCompiler.cpp 348 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272
  1. #pragma warning(disable:4996)
  2. #pragma warning(push)
  3. #pragma warning(disable:4800)
  4. #pragma warning(disable:4244)
  5. #pragma warning(disable:4141)
  6. #pragma warning(disable:4624)
  7. #pragma warning(disable:4146)
  8. #pragma warning(disable:4267)
  9. #pragma warning(disable:4291)
  10. #include "BeefySysLib/util/AllocDebug.h"
  11. #include "llvm/Support/Compiler.h"
  12. #include "llvm/Support/Compiler.h"
  13. #include "llvm/IR/IRBuilder.h"
  14. #include "llvm/IR/Type.h"
  15. #include "llvm/IR/DIBuilder.h"
  16. #include "llvm/IR/DebugInfo.h"
  17. #include "llvm/IR/Argument.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/Module.h"
  20. #include "BfCompiler.h"
  21. #include "BfSystem.h"
  22. #include "BfParser.h"
  23. #include "BfReducer.h"
  24. #include "BfExprEvaluator.h"
  25. #include "../Backend/BeLibManger.h"
  26. #include <fcntl.h>
  27. #include "BfConstResolver.h"
  28. #include "BfMangler.h"
  29. #include "BfDemangler.h"
  30. #include "BeefySysLib/util/PerfTimer.h"
  31. #include "BfSourceClassifier.h"
  32. #include "BfAutoComplete.h"
  33. #include "BfResolvePass.h"
  34. #include "BeefySysLib/util/BeefPerf.h"
  35. #include "BeefySysLib/util/ZipFile.h"
  36. #include "../LLVMUtils.h"
  37. #include "BfNamespaceVisitor.h"
  38. #include "CeMachine.h"
  39. #include "CeDebugger.h"
  40. #include "BfDefBuilder.h"
  41. #pragma warning(pop)
  42. namespace llvm
  43. {
  44. extern bool DebugFlag;
  45. }
  46. #define SPLIT_CONTEXTS
  47. Beefy::BfCompiler* gBfCompiler = NULL;
  48. void pt(llvm::Type* t)
  49. {
  50. //Beefy::OutputDebugStrF("pv((llvm::Type*)%p)\n", t);
  51. Beefy::debug_ostream os;
  52. t->print(os);
  53. os << "\n";
  54. os << " isSized: " << t->isSized() << "\n";
  55. os.flush();
  56. // if (auto pointerType = llvm::dyn_cast<llvm::PointerType>(t))
  57. // {
  58. // Beefy::OutputDebugStrF("Element: ");
  59. // pt(pointerType->getElementType());
  60. // }
  61. }
  62. void ppt(llvm::Type* t)
  63. {
  64. auto pointerType = llvm::dyn_cast<llvm::PointerType>(t);
  65. if (pointerType == NULL)
  66. {
  67. Beefy::OutputDebugStrF("Not a pointer type");
  68. return;
  69. }
  70. //pt(pointerType->getElementType());
  71. }
  72. void pt(llvm::DINode* t)
  73. {
  74. Beefy::debug_ostream os;
  75. t->print(os);
  76. os << "\n";
  77. os.flush();
  78. }
  79. void pt(llvm::Value* v)
  80. {
  81. pt(v->getType());
  82. }
  83. void pv(const llvm::Value* v)
  84. {
  85. Beefy::debug_ostream os;
  86. v->print(os);
  87. os << "\n";
  88. os.flush();
  89. pt(v->getType());
  90. }
  91. void ppt(llvm::Value* v)
  92. {
  93. ppt(v->getType());
  94. }
  95. void pmd(llvm::Metadata* md)
  96. {
  97. Beefy::debug_ostream os;
  98. md->print(os);
  99. os << "\n";
  100. os.flush();
  101. }
  102. void pdl(llvm::DebugLoc& dl)
  103. {
  104. Beefy::debug_ostream os;
  105. dl.print(os);
  106. os << "\n";
  107. os.flush();
  108. }
  109. void pm(llvm::Module* module)
  110. {
  111. Beefy::debug_ostream os;
  112. module->print(os, NULL);
  113. os << "\n";
  114. os.flush();
  115. }
  116. void PrintUsers(llvm::MDNode* md)
  117. {
  118. /*Beefy::debug_ostream os;
  119. //auto val = llvm::ReplaceableMetadataImpl::get(*md);
  120. auto val = md->Context.getReplaceableUses();
  121. if (val == NULL)
  122. {
  123. os << "Not replaceable\n";
  124. }
  125. else
  126. {
  127. //md->print(os);
  128. typedef std::pair<void *, std::pair<llvm::MetadataTracking::OwnerTy, uint64_t>> UseTy;
  129. llvm::SmallVector<UseTy, 8> Uses(val->UseMap.begin(), val->UseMap.end());
  130. std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) {
  131. return L.second.second < R.second.second;
  132. });
  133. for (const auto &Pair : Uses)
  134. {
  135. auto Owner = Pair.second.first;
  136. os << Beefy::StrFormat(" %d %p %d\n", Pair.second.first.isNull(), Pair.first, Pair.second.second, Pair).c_str();
  137. }
  138. os << "\n";
  139. }
  140. os.flush();*/
  141. }
  142. void ptbf(Beefy::BfType* bfType)
  143. {
  144. Beefy::OutputDebugStrF("%s\n", bfType->GetModule()->TypeToString(bfType).c_str());
  145. }
  146. void pt(const Beefy::BfTypedValue& val)
  147. {
  148. Beefy::OutputDebugStrF("%s\n", val.mType->GetModule()->TypeToString(val.mType).c_str());
  149. }
  150. void pt(llvm::SmallVectorImpl<llvm::Value*>& llvmArgs)
  151. {
  152. Beefy::debug_ostream os;
  153. for (int i = 0; i < (int)llvmArgs.size(); i++)
  154. {
  155. if (i > 0)
  156. os << ", ";
  157. llvmArgs[i]->getType()->print(os);
  158. }
  159. os << "\n";
  160. os.flush();
  161. }
  162. void PrintUsers(llvm::Value* v)
  163. {
  164. for (auto user : v->users())
  165. {
  166. pt(user);
  167. }
  168. }
  169. /*void PrintFunc(Beefy::BfMethodInstance* methodInstance)
  170. {
  171. Beefy::debug_ostream os;
  172. methodInstance->mIRFunction.mLLVMValue->print(os);
  173. os << "\n";
  174. os.flush();
  175. }*/
  176. USING_NS_BF;
  177. using namespace llvm;
  178. int Beefy::BfWorkListEntry::sCurReqId = 0;
  179. GlobalVariable* AllocGlobalVariable(Module &M, Type *Ty, bool isConstant,
  180. GlobalValue::LinkageTypes Linkage, Constant *Initializer,
  181. const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
  182. GlobalValue::ThreadLocalMode tlm = GlobalValue::NotThreadLocal, unsigned AddressSpace = 0,
  183. bool isExternallyInitialized = false);
  184. #include "BeefySysLib/util/AllocDebug.h"
  185. //////////////////////////////////////////////////////////////////////////
  186. BfCompiler::HotData::~HotData()
  187. {
  188. for (auto& kv : mMethodMap)
  189. {
  190. auto hotMethod = kv.mValue;
  191. hotMethod->Clear();
  192. }
  193. for (auto& kv : mThisType)
  194. kv.mValue->Deref();
  195. for (auto& kv : mAllocation)
  196. kv.mValue->Deref();
  197. for (auto& kv : mDevirtualizedMethods)
  198. kv.mValue->Deref();
  199. for (auto& kv : mFuncPtrs)
  200. kv.mValue->Deref();
  201. for (auto& kv : mVirtualDecls)
  202. kv.mValue->Deref();
  203. for (auto& kv : mInnerMethods)
  204. kv.mValue->Deref();
  205. for (auto& kv : mMethodMap)
  206. kv.mValue->Deref();
  207. }
  208. template <typename TDict>
  209. static void DeleteUnused(TDict& dict)
  210. {
  211. auto itr = dict.begin();
  212. while (itr != dict.end())
  213. {
  214. auto val = itr->mValue;
  215. BF_ASSERT(val->mRefCount >= 1);
  216. if (val->mRefCount == 1)
  217. {
  218. val->Deref();
  219. itr = dict.Remove(itr);
  220. }
  221. else
  222. ++itr;
  223. }
  224. }
  225. template <typename TDict, typename TElement>
  226. static typename TDict::value_type AllocFromMap(TDict& dict, TElement* elem)
  227. {
  228. typename TDict::value_type* valuePtr;
  229. if (dict.TryAdd(elem, NULL, &valuePtr))
  230. {
  231. auto val = new typename std::remove_pointer<typename TDict::value_type>::type(elem);
  232. val->mRefCount++;
  233. *valuePtr = val;
  234. }
  235. return *valuePtr;
  236. }
  237. void BfCompiler::HotData::ClearUnused(bool isHotCompile)
  238. {
  239. BP_ZONE("BfCompiler::HotData::ClearUnused");
  240. DeleteUnused(mThisType);
  241. DeleteUnused(mAllocation);
  242. DeleteUnused(mDevirtualizedMethods);
  243. DeleteUnused(mVirtualDecls);
  244. DeleteUnused(mInnerMethods);
  245. if (isHotCompile)
  246. {
  247. // We need to keep all function pointer references ever, since we can't tell if we still reference them or not
  248. DeleteUnused(mFuncPtrs);
  249. }
  250. }
  251. BfHotThisType* BfCompiler::HotData::GetThisType(BfHotTypeVersion* hotVersion)
  252. {
  253. return AllocFromMap(mThisType, hotVersion);
  254. }
  255. BfHotAllocation* BfCompiler::HotData::GetAllocation(BfHotTypeVersion* hotVersion)
  256. {
  257. return AllocFromMap(mAllocation, hotVersion);
  258. }
  259. BfHotDevirtualizedMethod* BfCompiler::HotData::GetDevirtualizedMethod(BfHotMethod* hotMethod)
  260. {
  261. return AllocFromMap(mDevirtualizedMethods, hotMethod);
  262. }
  263. BfHotFunctionReference* BfCompiler::HotData::GetFunctionReference(BfHotMethod* hotMethod)
  264. {
  265. return AllocFromMap(mFuncPtrs, hotMethod);
  266. }
  267. BfHotInnerMethod* BfCompiler::HotData::GetInnerMethod(BfHotMethod* hotMethod)
  268. {
  269. return AllocFromMap(mInnerMethods, hotMethod);
  270. }
  271. BfHotVirtualDeclaration* BfCompiler::HotData::GetVirtualDeclaration(BfHotMethod* hotMethod)
  272. {
  273. return AllocFromMap(mVirtualDecls, hotMethod);
  274. }
  275. BfCompiler::HotState::~HotState()
  276. {
  277. }
  278. bool BfCompiler::HotState::HasPendingChanges(BfTypeInstance* type)
  279. {
  280. return (type->mHotTypeData != NULL) && (type->mHotTypeData->mPendingDataChange);
  281. }
  282. void BfCompiler::HotState::RemovePendingChanges(BfTypeInstance* type)
  283. {
  284. BF_ASSERT(type->mHotTypeData->mPendingDataChange);
  285. if (!type->mHotTypeData->mPendingDataChange)
  286. return;
  287. type->mHotTypeData->mPendingDataChange = false;
  288. bool didRemove = mPendingDataChanges.Remove(type->mTypeId);
  289. BF_ASSERT(didRemove);
  290. }
  291. BfCompiler::HotResolveData::~HotResolveData()
  292. {
  293. for (auto hotMethod : mActiveMethods)
  294. hotMethod->Deref();
  295. for (auto kv : mReachableMethods)
  296. kv.mKey->Deref();
  297. }
  298. //////////////////////////////////////////////////////////////////////////
  299. BfCompiler::BfCompiler(BfSystem* bfSystem, bool isResolveOnly)
  300. {
  301. memset(&mStats, 0, sizeof(mStats));
  302. mCompletionPct = 0;
  303. mCanceling = false;
  304. mHasRequiredTypes = false;
  305. mNeedsFullRefresh = false;
  306. mFastFinish = false;
  307. mExtraCompileRequested = false;
  308. mHasQueuedTypeRebuilds = false;
  309. mIsResolveOnly = isResolveOnly;
  310. mResolvePassData = NULL;
  311. mPassInstance = NULL;
  312. mRevision = 0;
  313. mUniqueId = 0;
  314. mLastRevisionAborted = false;
  315. gBfCompiler = this;
  316. mSystem = bfSystem;
  317. mCurTypeId = 1;
  318. mTypeInitCount = 0;
  319. //mMaxInterfaceSlots = 16;
  320. mMaxInterfaceSlots = -1;
  321. mInterfaceSlotCountChanged = false;
  322. mLastHadComptimeRebuilds = false;
  323. mHasComptimeRebuilds = false;
  324. mDepsMayHaveDeletedTypes = false;
  325. mHSPreserveIdx = 0;
  326. mCompileLogFP = NULL;
  327. mWantsDeferMethodDecls = false;
  328. mHadCancel = false;
  329. mCompileState = CompileState_None;
  330. //mMaxInterfaceSlots = 4;
  331. mHotData = NULL;
  332. mHotState = NULL;
  333. mHotResolveData = NULL;
  334. mBfObjectTypeDef = NULL;
  335. mChar32TypeDef = NULL;
  336. mFloatTypeDef = NULL;
  337. mDoubleTypeDef = NULL;
  338. mMathTypeDef = NULL;
  339. mArray1TypeDef = NULL;
  340. mArray2TypeDef = NULL;
  341. mArray3TypeDef = NULL;
  342. mArray4TypeDef = NULL;
  343. mSpanTypeDef = NULL;
  344. mRangeTypeDef = NULL;
  345. mClosedRangeTypeDef = NULL;
  346. mIndexTypeDef = NULL;
  347. mIndexRangeTypeDef = NULL;
  348. mAttributeTypeDef = NULL;
  349. mAttributeUsageAttributeTypeDef = NULL;
  350. mClassVDataTypeDef = NULL;
  351. mCLinkAttributeTypeDef = NULL;
  352. mImportAttributeTypeDef = NULL;
  353. mExportAttributeTypeDef = NULL;
  354. mCReprAttributeTypeDef = NULL;
  355. mUnderlyingArrayAttributeTypeDef = NULL;
  356. mAlignAttributeTypeDef = NULL;
  357. mAllowDuplicatesAttributeTypeDef = NULL;
  358. mNoDiscardAttributeTypeDef = NULL;
  359. mDisableChecksAttributeTypeDef = NULL;
  360. mDisableObjectAccessChecksAttributeTypeDef = NULL;
  361. mDbgRawAllocDataTypeDef = NULL;
  362. mDeferredCallTypeDef = NULL;
  363. mDelegateTypeDef = NULL;
  364. mFunctionTypeDef = NULL;
  365. mActionTypeDef = NULL;
  366. mEnumTypeDef = NULL;
  367. mFriendAttributeTypeDef = NULL;
  368. mComptimeAttributeTypeDef = NULL;
  369. mIntrinsicAttributeTypeDef = NULL;
  370. mConstEvalAttributeTypeDef = NULL;
  371. mNoExtensionAttributeTypeDef = NULL;
  372. mCheckedAttributeTypeDef = NULL;
  373. mUncheckedAttributeTypeDef = NULL;
  374. mGCTypeDef = NULL;
  375. mGenericIEnumerableTypeDef = NULL;
  376. mGenericIEnumeratorTypeDef = NULL;
  377. mGenericIRefEnumeratorTypeDef = NULL;
  378. mInlineAttributeTypeDef = NULL;
  379. mThreadTypeDef = NULL;
  380. mInternalTypeDef = NULL;
  381. mPlatformTypeDef = NULL;
  382. mCompilerTypeDef = NULL;
  383. mCompilerGeneratorTypeDef = NULL;
  384. mDiagnosticsDebugTypeDef = NULL;
  385. mIDisposableTypeDef = NULL;
  386. mIIntegerTypeDef = NULL;
  387. mIPrintableTypeDef = NULL;
  388. mIHashableTypeDef = NULL;
  389. mIComptimeTypeApply = NULL;
  390. mIComptimeMethodApply = NULL;
  391. mIOnTypeInitTypeDef = NULL;
  392. mIOnTypeDoneTypeDef = NULL;
  393. mIOnFieldInitTypeDef = NULL;
  394. mIOnMethodInitTypeDef = NULL;
  395. mLinkNameAttributeTypeDef = NULL;
  396. mCallingConventionAttributeTypeDef = NULL;
  397. mMethodRefTypeDef = NULL;
  398. mNullableTypeDef = NULL;
  399. mOrderedAttributeTypeDef = NULL;
  400. mPointerTTypeDef = NULL;
  401. mPointerTypeDef = NULL;
  402. mReflectTypeIdTypeDef = NULL;
  403. mReflectArrayType = NULL;
  404. mReflectGenericParamType = NULL;
  405. mReflectFieldDataDef = NULL;
  406. mReflectFieldSplatDataDef = NULL;
  407. mReflectMethodDataDef = NULL;
  408. mReflectParamDataDef = NULL;
  409. mReflectInterfaceDataDef = NULL;
  410. mReflectPointerType = NULL;
  411. mReflectRefType = NULL;
  412. mReflectSizedArrayType = NULL;
  413. mReflectConstExprType = NULL;
  414. mReflectSpecializedGenericType = NULL;
  415. mReflectTypeInstanceTypeDef = NULL;
  416. mReflectUnspecializedGenericType = NULL;
  417. mReflectFieldInfoTypeDef = NULL;
  418. mReflectMethodInfoTypeDef = NULL;
  419. mSizedArrayTypeDef = NULL;
  420. mStaticInitAfterAttributeTypeDef = NULL;
  421. mStaticInitPriorityAttributeTypeDef = NULL;
  422. mStringTypeDef = NULL;
  423. mStringViewTypeDef = NULL;
  424. mThreadStaticAttributeTypeDef = NULL;
  425. mTypeTypeDeclDef = NULL;
  426. mTypeTypeDef = NULL;
  427. mUnboundAttributeTypeDef = NULL;
  428. mValueTypeTypeDef = NULL;
  429. mTupleTypeDef = NULL;
  430. mResultTypeDef = NULL;
  431. mObsoleteAttributeTypeDef = NULL;
  432. mErrorAttributeTypeDef = NULL;
  433. mWarnAttributeTypeDef = NULL;
  434. mConstSkipAttributeTypeDef = NULL;
  435. mIgnoreErrorsAttributeTypeDef = NULL;
  436. mReflectAttributeTypeDef = NULL;
  437. mOnCompileAttributeTypeDef = NULL;
  438. mLastAutocompleteModule = NULL;
  439. mContext = new BfContext(this);
  440. mCeMachine = new CeMachine(this);
  441. mCurCEExecuteId = -1;
  442. mLastMidCompileRefreshRevision = -1;
  443. }
  444. BfCompiler::~BfCompiler()
  445. {
  446. delete mCeMachine;
  447. mCeMachine = NULL;
  448. delete mContext;
  449. delete mHotData;
  450. delete mHotState;
  451. delete mHotResolveData;
  452. }
  453. bool BfCompiler::IsTypeAccessible(BfType* checkType, BfProject* curProject)
  454. {
  455. if (checkType->IsBoxed())
  456. return IsTypeAccessible(((BfBoxedType*)checkType)->mElementType, curProject);
  457. BfTypeInstance* typeInst = checkType->ToTypeInstance();
  458. if (typeInst != NULL)
  459. {
  460. if (checkType->IsTuple())
  461. {
  462. for (auto&& fieldInst : typeInst->mFieldInstances)
  463. {
  464. if (!IsTypeAccessible(fieldInst.mResolvedType, curProject))
  465. return false;
  466. }
  467. }
  468. auto genericTypeInst = typeInst->ToGenericTypeInstance();
  469. if (genericTypeInst != NULL)
  470. {
  471. for (auto genericArg : genericTypeInst->mGenericTypeInfo->mTypeGenericArguments)
  472. if (!IsTypeAccessible(genericArg, curProject))
  473. return false;
  474. }
  475. return curProject->ContainsReference(typeInst->mTypeDef->mProject);
  476. }
  477. if (checkType->IsPointer())
  478. return IsTypeAccessible(((BfPointerType*)checkType)->mElementType, curProject);
  479. if (checkType->IsRef())
  480. return IsTypeAccessible(((BfPointerType*)checkType)->mElementType, curProject);
  481. return true;
  482. }
  483. bool BfCompiler::IsTypeUsed(BfType* checkType, BfProject* curProject)
  484. {
  485. if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
  486. return IsTypeAccessible(checkType, curProject);
  487. BfTypeInstance* typeInst = checkType->ToTypeInstance();
  488. if (typeInst != NULL)
  489. {
  490. //TODO: Why was this here?
  491. // if ((typeInst->mTypeDef->mProject != NULL) && (typeInst->mTypeDef->mProject != curProject))
  492. // {
  493. // if (typeInst->mTypeDef->mProject->mTargetType == BfTargetType_BeefDynLib)
  494. // return false;
  495. // }
  496. if (checkType->IsInterface())
  497. return typeInst->mIsReified;
  498. //TODO: We could check to see if this project has any reified specialized instances...
  499. if (checkType->IsUnspecializedType())
  500. return typeInst->mIsReified;
  501. if (checkType->IsTuple())
  502. {
  503. for (auto&& fieldInst : typeInst->mFieldInstances)
  504. {
  505. if (!IsTypeUsed(fieldInst.mResolvedType, curProject))
  506. return false;
  507. }
  508. }
  509. auto genericTypeInst = typeInst->ToGenericTypeInstance();
  510. if (genericTypeInst != NULL)
  511. {
  512. for (auto genericArg : genericTypeInst->mGenericTypeInfo->mTypeGenericArguments)
  513. if (!IsTypeUsed(genericArg, curProject))
  514. return false;
  515. }
  516. if (checkType->IsFunction())
  517. {
  518. // These don't get their own modules so just assume "always used" at this point
  519. return true;
  520. }
  521. auto module = typeInst->GetModule();
  522. if (module == NULL)
  523. return true;
  524. return curProject->mUsedModules.Contains(module);
  525. }
  526. if (checkType->IsPointer())
  527. return IsTypeUsed(((BfPointerType*)checkType)->mElementType, curProject);
  528. if (checkType->IsRef())
  529. return IsTypeUsed(((BfRefType*)checkType)->mElementType, curProject);
  530. return true;
  531. }
  532. bool BfCompiler::IsModuleAccessible(BfModule* module, BfProject* curProject)
  533. {
  534. for (auto checkType : module->mOwnedTypeInstances)
  535. {
  536. if (!IsTypeAccessible(checkType, curProject))
  537. return false;
  538. }
  539. return curProject->ContainsReference(module->mProject);
  540. }
  541. void BfCompiler::FixVDataHash(BfModule* bfModule)
  542. {
  543. // We recreate the actual vdata hash now that we're done creating new string literals
  544. /*for (auto context : mContexts)
  545. HASH128_MIXIN(bfModule->mDataHash, bfModule->mHighestUsedStringId);*/
  546. }
  547. void BfCompiler::CheckModuleStringRefs(BfModule* module, BfVDataModule* vdataModule, int lastModuleRevision, HashSet<int>& foundStringIds, HashSet<int>& dllNameSet, Array<BfMethodInstance*>& dllMethods, Array<BfCompiler::StringValueEntry>& stringValueEntries)
  548. {
  549. for (int stringId : module->mStringPoolRefs)
  550. {
  551. if (foundStringIds.Add(stringId))
  552. {
  553. BfStringPoolEntry& stringPoolEntry = module->mContext->mStringObjectIdMap[stringId];
  554. if (IsHotCompile())
  555. {
  556. if (vdataModule->mDefinedStrings.Contains(stringId))
  557. continue;
  558. }
  559. StringValueEntry stringEntry;
  560. stringEntry.mId = stringId;
  561. vdataModule->mDefinedStrings.Add(stringId);
  562. stringEntry.mStringVal = vdataModule->CreateStringObjectValue(stringPoolEntry.mString, stringId, true);
  563. stringValueEntries.Add(stringEntry);
  564. CompileLog("String %d %s\n", stringId, stringPoolEntry.mString.c_str());
  565. }
  566. }
  567. for (auto dllNameId : module->mImportFileNames)
  568. dllNameSet.Add(dllNameId);
  569. for (auto& dllImportEntry : module->mDllImportEntries)
  570. dllMethods.push_back(dllImportEntry.mMethodInstance);
  571. auto altModule = module->mNextAltModule;
  572. while (altModule != NULL)
  573. {
  574. CheckModuleStringRefs(altModule, vdataModule, lastModuleRevision, foundStringIds, dllNameSet, dllMethods, stringValueEntries);
  575. altModule = altModule->mNextAltModule;
  576. }
  577. for (auto& specModulePair : module->mSpecializedMethodModules)
  578. CheckModuleStringRefs(specModulePair.mValue, vdataModule, lastModuleRevision, foundStringIds, dllNameSet, dllMethods, stringValueEntries);
  579. }
  580. void BfCompiler::HashModuleVData(BfModule* module, HashContext& vdataHash)
  581. {
  582. BP_ZONE("BfCompiler::HashModuleVData");
  583. if (module->mStringPoolRefs.size() > 0)
  584. {
  585. module->mStringPoolRefs.Sort([](int lhs, int rhs) { return lhs < rhs; });
  586. vdataHash.Mixin(&module->mStringPoolRefs[0], (int)module->mStringPoolRefs.size() * (int)sizeof(int));
  587. }
  588. if (module->mImportFileNames.size() > 0)
  589. {
  590. module->mImportFileNames.Sort([](int lhs, int rhs) { return lhs < rhs; });
  591. vdataHash.Mixin(&module->mImportFileNames[0], (int)module->mImportFileNames.size() * (int)sizeof(int));
  592. }
  593. auto altModule = module->mNextAltModule;
  594. while (altModule != NULL)
  595. {
  596. HashModuleVData(altModule, vdataHash);
  597. altModule = altModule->mNextAltModule;
  598. }
  599. for (auto& specModulePair : module->mSpecializedMethodModules)
  600. {
  601. HashModuleVData(specModulePair.mValue, vdataHash);
  602. }
  603. }
  604. BfIRFunction BfCompiler::CreateLoadSharedLibraries(BfVDataModule* bfModule, Array<BfMethodInstance*>& dllMethods)
  605. {
  606. BfIRType nullPtrType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr));
  607. BfIRType nullPtrPtrType = bfModule->mBfIRBuilder->MapType(bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr)));
  608. BfIRType voidType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_None));
  609. SmallVector<BfIRType, 2> paramTypes;
  610. auto loadSharedLibrariesFuncType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes, false);
  611. auto loadSharedLibFunc = bfModule->mBfIRBuilder->CreateFunction(loadSharedLibrariesFuncType, BfIRLinkageType_External, "BfLoadSharedLibraries");
  612. bfModule->SetupIRMethod(NULL, loadSharedLibFunc, false);
  613. bfModule->mBfIRBuilder->SetActiveFunction(loadSharedLibFunc);
  614. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  615. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  616. HashSet<int> dllNameSet;
  617. auto internalType = bfModule->ResolveTypeDef(mInternalTypeDef);
  618. bfModule->PopulateType(internalType);
  619. auto getSharedProcAddressInstance = bfModule->GetMethodByName(internalType->ToTypeInstance(), "GetSharedProcAddressInto");
  620. auto loadSharedLibraryProc = bfModule->GetMethodByName(internalType->ToTypeInstance(), "LoadSharedLibraryInto");
  621. BF_ASSERT(getSharedProcAddressInstance);
  622. BF_ASSERT(loadSharedLibraryProc);
  623. if (!getSharedProcAddressInstance)
  624. {
  625. bfModule->Fail("Missing Internal.GetSharedProcAddressInto");
  626. return loadSharedLibFunc;
  627. }
  628. if (!loadSharedLibraryProc)
  629. {
  630. bfModule->Fail("Missing Internal.LoadSharedLibraryInto");
  631. return loadSharedLibFunc;
  632. }
  633. Dictionary<int, BfIRValue> dllHandleMap;
  634. for (auto methodInstance : dllMethods)
  635. {
  636. auto typeInstance = methodInstance->GetOwner();
  637. auto methodDef = methodInstance->mMethodDef;
  638. BF_ASSERT(methodInstance->GetCustomAttributes() != NULL);
  639. for (auto customAttr : methodInstance->GetCustomAttributes()->mAttributes)
  640. {
  641. if (customAttr.mType->mTypeDef->mFullName.ToString() == "System.ImportAttribute")
  642. {
  643. bool doCLink = false;
  644. bool undecorated = false;
  645. BfCallingConvention callingConvention = methodDef->mCallingConvention;
  646. for (auto fieldSet : customAttr.mSetField)
  647. {
  648. BfFieldDef* fieldDef = fieldSet.mFieldRef;
  649. if (fieldDef->mName == "CLink")
  650. {
  651. auto constant = typeInstance->mConstHolder->GetConstant(fieldSet.mParam.mValue);
  652. if (constant != NULL)
  653. doCLink = constant->mBool;
  654. }
  655. if (fieldDef->mName == "Undecorated")
  656. {
  657. auto constant = typeInstance->mConstHolder->GetConstant(fieldSet.mParam.mValue);
  658. if (constant != NULL)
  659. undecorated = constant->mBool;
  660. }
  661. if (fieldDef->mName == "CallingConvention")
  662. {
  663. auto constant = typeInstance->mConstHolder->GetConstant(fieldSet.mParam.mValue);
  664. if (constant != NULL)
  665. {
  666. int callingConventionVal = (int)constant->mInt32;
  667. if ((callingConventionVal == 3) || (callingConventionVal == 1))
  668. callingConvention = BfCallingConvention_Stdcall;
  669. else if (callingConventionVal == 2)
  670. callingConvention = BfCallingConvention_Cdecl;
  671. }
  672. }
  673. }
  674. if (customAttr.mCtorArgs.size() == 1)
  675. {
  676. auto fileNameArg = customAttr.mCtorArgs[0];
  677. int strNum = 0;
  678. auto constant = typeInstance->mConstHolder->GetConstant(fileNameArg);
  679. if (constant != NULL)
  680. {
  681. if (constant->IsNull())
  682. continue; // Invalid
  683. strNum = constant->mInt32;
  684. }
  685. else
  686. {
  687. strNum = bfModule->GetStringPoolIdx(fileNameArg, typeInstance->mConstHolder);
  688. }
  689. BfIRValue dllHandleVar;
  690. if (!dllHandleMap.TryGetValue(strNum, &dllHandleVar))
  691. {
  692. String dllHandleName = StrFormat("bf_hs_preserve@dllHandle%d", strNum);
  693. dllHandleVar = bfModule->mBfIRBuilder->CreateGlobalVariable(nullPtrType, false, BfIRLinkageType_External,
  694. bfModule->GetDefaultValue(bfModule->GetPrimitiveType(BfTypeCode_NullPtr)), dllHandleName);
  695. BfIRValue namePtr = bfModule->GetStringCharPtr(strNum);
  696. SmallVector<BfIRValue, 2> args;
  697. args.push_back(namePtr);
  698. args.push_back(dllHandleVar);
  699. BfIRValue dllHandleValue = bfModule->mBfIRBuilder->CreateCall(loadSharedLibraryProc.mFunc, args);
  700. dllHandleMap[strNum] = dllHandleVar;
  701. }
  702. String methodImportName;
  703. if (undecorated)
  704. {
  705. methodImportName = methodInstance->mMethodDef->mName;
  706. }
  707. else if (doCLink)
  708. {
  709. methodImportName = methodInstance->mMethodDef->mName;
  710. if ((mSystem->mPtrSize == 4) && (callingConvention == BfCallingConvention_Stdcall))
  711. {
  712. int argSize = (int)methodDef->mParams.size() * mSystem->mPtrSize;
  713. methodImportName = StrFormat("_%s$%d", methodImportName.c_str(), argSize);
  714. }
  715. }
  716. else
  717. BfMangler::Mangle(methodImportName, GetMangleKind(), methodInstance);
  718. BfIRValue methodNameValue = bfModule->mBfIRBuilder->CreateGlobalStringPtr(methodImportName);
  719. //auto moduleMethodInstance = bfModule->ReferenceExternalMethodInstance(methodInstance);
  720. //auto globalVarPtr = bfModule->mBfIRBuilder->CreateBitCast(moduleMethodInstance.mFunc, nullPtrPtrType);
  721. auto func = bfModule->CreateDllImportGlobalVar(methodInstance, false);
  722. auto globalVarPtr = bfModule->mBfIRBuilder->CreateBitCast(func, nullPtrPtrType);
  723. BfSizedVector<BfIRValue, 2> args;
  724. args.push_back(bfModule->mBfIRBuilder->CreateLoad(dllHandleVar));
  725. args.push_back(methodNameValue);
  726. args.push_back(globalVarPtr);
  727. BfIRValue dllFuncValVoidPtr = bfModule->mBfIRBuilder->CreateCall(getSharedProcAddressInstance.mFunc, args);
  728. }
  729. }
  730. }
  731. }
  732. bfModule->mBfIRBuilder->CreateRetVoid();
  733. return loadSharedLibFunc;
  734. }
  735. void BfCompiler::GetTestMethods(BfVDataModule* bfModule, Array<TestMethod>& testMethods, HashContext& vdataHashCtx)
  736. {
  737. vdataHashCtx.Mixin(0xBEEF0001); // Marker
  738. auto _CheckMethod = [&](BfTypeInstance* typeInstance, BfMethodInstance* methodInstance)
  739. {
  740. auto project = methodInstance->mMethodDef->mDeclaringType->mProject;
  741. if (project->mTargetType != BfTargetType_BeefTest)
  742. return;
  743. if (project != bfModule->mProject)
  744. return;
  745. bool isTest = false;
  746. if ((methodInstance->GetCustomAttributes() != NULL) &&
  747. (methodInstance->GetCustomAttributes()->Contains(mTestAttributeTypeDef)))
  748. isTest = true;
  749. if (!isTest)
  750. return;
  751. if (methodInstance->mIsUnspecialized)
  752. {
  753. if (!typeInstance->IsSpecializedType())
  754. {
  755. bfModule->Fail(StrFormat("Method '%s' cannot be used for testing because it's generic", bfModule->MethodToString(methodInstance).c_str()),
  756. methodInstance->mMethodDef->GetRefNode());
  757. }
  758. bfModule->mHadBuildError = true;
  759. return;
  760. }
  761. if (!methodInstance->mMethodDef->mIsStatic)
  762. {
  763. if (!typeInstance->IsSpecializedType())
  764. {
  765. bfModule->Fail(StrFormat("Method '%s' cannot be used for testing because it's not static", bfModule->MethodToString(methodInstance).c_str()),
  766. methodInstance->mMethodDef->GetRefNode());
  767. }
  768. bfModule->mHadBuildError = true;
  769. return;
  770. }
  771. if (methodInstance->GetParamCount() > 0)
  772. {
  773. if ((methodInstance->GetParamInitializer(0) == NULL) &&
  774. (methodInstance->GetParamKind(0) != BfParamKind_Params))
  775. {
  776. if (!typeInstance->IsSpecializedType())
  777. {
  778. bfModule->Fail(StrFormat("Method '%s' cannot be used for testing because it contains parameters without defaults", bfModule->MethodToString(methodInstance).c_str()),
  779. methodInstance->mMethodDef->GetRefNode());
  780. }
  781. bfModule->mHadBuildError = true;
  782. return;
  783. }
  784. }
  785. BF_ASSERT(typeInstance->IsReified());
  786. TestMethod testMethod;
  787. testMethod.mMethodInstance = methodInstance;
  788. testMethods.Add(testMethod);
  789. BF_ASSERT_REL(!typeInstance->mModule->mIsDeleting);
  790. if (!bfModule->mProject->mUsedModules.Contains(typeInstance->mModule))
  791. {
  792. bfModule->mProject->mUsedModules.Add(typeInstance->mModule);
  793. }
  794. vdataHashCtx.Mixin(methodInstance->GetOwner()->mTypeId);
  795. vdataHashCtx.Mixin(methodInstance->mMethodDef->mIdx);
  796. };
  797. for (auto type : mContext->mResolvedTypes)
  798. {
  799. auto typeInstance = type->ToTypeInstance();
  800. if (typeInstance == NULL)
  801. continue;
  802. if (typeInstance->IsUnspecializedType())
  803. continue;
  804. for (auto& methodInstanceGroup : typeInstance->mMethodInstanceGroups)
  805. {
  806. if (methodInstanceGroup.mDefault != NULL)
  807. _CheckMethod(typeInstance, methodInstanceGroup.mDefault);
  808. }
  809. }
  810. }
  811. void BfCompiler::EmitTestMethod(BfVDataModule* bfModule, Array<TestMethod>& testMethods, BfIRValue& retValue)
  812. {
  813. for (auto& testMethod : testMethods)
  814. {
  815. auto methodInstance = testMethod.mMethodInstance;
  816. auto typeInstance = methodInstance->GetOwner();
  817. testMethod.mName += bfModule->TypeToString(typeInstance);
  818. if (!testMethod.mName.IsEmpty())
  819. testMethod.mName += ".";
  820. testMethod.mName += methodInstance->mMethodDef->mName;
  821. testMethod.mName += "\t";
  822. auto testAttribute = methodInstance->GetCustomAttributes()->Get(mTestAttributeTypeDef);
  823. for (auto& field : testAttribute->mSetField)
  824. {
  825. auto constant = typeInstance->mConstHolder->GetConstant(field.mParam.mValue);
  826. if ((constant != NULL) && (constant->mTypeCode == BfTypeCode_Boolean) && (constant->mBool))
  827. {
  828. BfFieldDef* fieldDef = field.mFieldRef;
  829. if (fieldDef->mName == "ShouldFail")
  830. {
  831. testMethod.mName += "Sf\a";
  832. }
  833. else if (fieldDef->mName == "Profile")
  834. {
  835. testMethod.mName += "Pr\a";
  836. }
  837. else if (fieldDef->mName == "Ignore")
  838. {
  839. testMethod.mName += "Ig\a";
  840. }
  841. }
  842. else if ((constant != NULL) && (constant->mTypeCode == BfTypeCode_StringId))
  843. {
  844. BfFieldDef* fieldDef = field.mFieldRef;
  845. if (fieldDef->mName == "Name")
  846. {
  847. String* str = bfModule->GetStringPoolString(field.mParam.mValue, typeInstance->mConstHolder);
  848. if (str != NULL)
  849. {
  850. testMethod.mName += "Name";
  851. testMethod.mName += SlashString(*str, true, true, true);
  852. testMethod.mName += '\a';
  853. }
  854. }
  855. }
  856. }
  857. bfModule->UpdateSrcPos(methodInstance->mMethodDef->GetRefNode(), (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
  858. testMethod.mName += StrFormat("\t%s\t%d\t%d", bfModule->mCurFilePosition.mFileInstance->mParser->mFileName.c_str(), bfModule->mCurFilePosition.mCurLine, bfModule->mCurFilePosition.mCurColumn);
  859. }
  860. std::stable_sort(testMethods.begin(), testMethods.end(),
  861. [](const TestMethod& lhs, const TestMethod& rhs)
  862. {
  863. return lhs.mName < rhs.mName;
  864. });
  865. String methodData;
  866. for (int methodIdx = 0; methodIdx < (int)testMethods.size(); methodIdx++)
  867. {
  868. String& methodName = testMethods[methodIdx].mName;
  869. if (!methodData.IsEmpty())
  870. methodData += "\n";
  871. methodData += methodName;
  872. }
  873. //////////////////////////////////////////////////////////////////////////
  874. auto testInitMethod = bfModule->GetInternalMethod("Test_Init");
  875. auto testQueryMethod = bfModule->GetInternalMethod("Test_Query");
  876. auto testFinishMethod = bfModule->GetInternalMethod("Test_Finish");
  877. auto char8PtrType = bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_Char8));
  878. BfIRType strCharType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->GetPrimitiveType(BfTypeCode_Char8), (int)methodData.length() + 1);
  879. BfIRValue strConstant = bfModule->mBfIRBuilder->CreateConstString(methodData);
  880. BfIRValue gv = bfModule->mBfIRBuilder->CreateGlobalVariable(strCharType,
  881. true, BfIRLinkageType_External,
  882. strConstant, "__bfTestData");
  883. BfIRValue strPtrVal = bfModule->mBfIRBuilder->CreateBitCast(gv, bfModule->mBfIRBuilder->MapType(char8PtrType));
  884. SizedArray<BfIRValue, 4> irArgs;
  885. irArgs.Add(strPtrVal);
  886. bfModule->mBfIRBuilder->CreateCall(testInitMethod.mFunc, irArgs);
  887. BfIRBlock testHeadBlock = bfModule->mBfIRBuilder->CreateBlock("testHead");
  888. BfIRBlock testEndBlock = bfModule->mBfIRBuilder->CreateBlock("testEnd");
  889. bfModule->mBfIRBuilder->CreateBr(testHeadBlock);
  890. bfModule->mBfIRBuilder->AddBlock(testHeadBlock);
  891. bfModule->mBfIRBuilder->SetInsertPoint(testHeadBlock);
  892. irArgs.clear();
  893. auto testVal = bfModule->mBfIRBuilder->CreateCall(testQueryMethod.mFunc, irArgs);
  894. auto switchVal = bfModule->mBfIRBuilder->CreateSwitch(testVal, testEndBlock, (int)testMethods.size());
  895. for (int methodIdx = 0; methodIdx < (int)testMethods.size(); methodIdx++)
  896. {
  897. auto methodInstance = testMethods[methodIdx].mMethodInstance;
  898. String& methodName = testMethods[methodIdx].mName;
  899. auto testBlock = bfModule->mBfIRBuilder->CreateBlock(StrFormat("test%d", methodIdx));
  900. bfModule->mBfIRBuilder->AddSwitchCase(switchVal, bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, methodIdx), testBlock);
  901. bfModule->mBfIRBuilder->AddBlock(testBlock);
  902. bfModule->mBfIRBuilder->SetInsertPoint(testBlock);
  903. auto moduleMethodInstance = bfModule->ReferenceExternalMethodInstance(methodInstance);
  904. irArgs.clear();
  905. if (methodInstance->GetParamCount() > 0)
  906. {
  907. if (methodInstance->GetParamKind(0) == BfParamKind_Params)
  908. {
  909. auto paramType = methodInstance->GetParamType(0);
  910. auto paramTypeInst = paramType->ToTypeInstance();
  911. BfTypedValue paramVal = BfTypedValue(bfModule->mBfIRBuilder->CreateAlloca(bfModule->mBfIRBuilder->MapTypeInst(paramTypeInst)), paramType);
  912. bfModule->InitTypeInst(paramVal, NULL, false, BfIRValue());
  913. //TODO: Assert 'length' var is at slot 1
  914. auto arrayBits = bfModule->mBfIRBuilder->CreateBitCast(paramVal.mValue, bfModule->mBfIRBuilder->MapType(paramTypeInst->mBaseType));
  915. auto addr = bfModule->mBfIRBuilder->CreateInBoundsGEP(arrayBits, 0, 1);
  916. auto storeInst = bfModule->mBfIRBuilder->CreateAlignedStore(bfModule->GetConstValue(0), addr, 4);
  917. irArgs.Add(paramVal.mValue);
  918. }
  919. else
  920. {
  921. for (int defaultIdx = 0; defaultIdx < (int)methodInstance->mDefaultValues.size(); defaultIdx++)
  922. {
  923. auto constHolder = methodInstance->GetOwner()->mConstHolder;
  924. auto defaultTypedValue = methodInstance->mDefaultValues[defaultIdx];
  925. auto defaultVal = bfModule->ConstantToCurrent(constHolder->GetConstant(defaultTypedValue.mValue), constHolder, defaultTypedValue.mType);
  926. auto castedVal = bfModule->Cast(methodInstance->mMethodDef->GetRefNode(), BfTypedValue(defaultVal, defaultTypedValue.mType), methodInstance->GetParamType(defaultIdx));
  927. if (castedVal)
  928. {
  929. BfExprEvaluator exprEvaluator(bfModule);
  930. exprEvaluator.PushArg(castedVal, irArgs);
  931. }
  932. }
  933. }
  934. }
  935. BfExprEvaluator exprEvaluator(bfModule);
  936. exprEvaluator.CreateCall(NULL, moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc, false, irArgs);
  937. bfModule->mBfIRBuilder->CreateBr(testHeadBlock);
  938. }
  939. bfModule->mBfIRBuilder->AddBlock(testEndBlock);
  940. bfModule->mBfIRBuilder->SetInsertPoint(testEndBlock);
  941. irArgs.clear();
  942. bfModule->mBfIRBuilder->CreateCall(testFinishMethod.mFunc, irArgs);
  943. retValue = bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 0);
  944. }
  945. void BfCompiler::CreateVData(BfVDataModule* bfModule)
  946. {
  947. bool isHotCompile = IsHotCompile();
  948. if ((isHotCompile) && (bfModule->mProject != mOptions.mHotProject))
  949. return;
  950. BP_ZONE("BfCompiler::CreateVData");
  951. BfLogSysM("CreateVData %s\n", bfModule->mProject->mName.c_str());
  952. CompileLog("CreateVData %s\n", bfModule->mProject->mName.c_str());
  953. BF_ASSERT_REL(!bfModule->mIsDeleting);
  954. bfModule->mProject->mUsedModules.Add(bfModule);
  955. auto project = bfModule->mProject;
  956. auto vdataContext = bfModule->mContext;
  957. BF_ASSERT(bfModule->mModuleName == "vdata");
  958. //////////////////////////////////////////////////////////////////////////
  959. mContext->ReflectInit();
  960. // Create types we'll need for vdata, so we won't change the vdata hash afterward
  961. // bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr));
  962. //
  963. // ///
  964. //
  965. // auto typeDefType = bfModule->ResolveTypeDef(mTypeTypeDef)->ToTypeInstance();
  966. // if (!typeDefType)
  967. // return;
  968. // BF_ASSERT(typeDefType != NULL);
  969. // vdataContext->mBfTypeType = typeDefType->ToTypeInstance();
  970. //
  971. // auto typeInstanceDefType = bfModule->ResolveTypeDef(mReflectTypeInstanceTypeDef);
  972. // if (!typeInstanceDefType)
  973. // return;
  974. // auto typeInstanceDefTypeInstance = typeInstanceDefType->ToTypeInstance();
  975. //
  976. // auto typeDef = mSystem->FindTypeDef("System.ClassVData");
  977. // BF_ASSERT(typeDef != NULL);
  978. // auto bfClassVDataType = bfModule->ResolveTypeDef(typeDef)->ToTypeInstance();
  979. // vdataContext->mBfClassVDataPtrType = bfModule->CreatePointerType(bfClassVDataType);
  980. //////////////////////////////////////////////////////////////////////////
  981. int numEntries = 0;
  982. int numConcreteTypes = 0;
  983. Array<BfType*> orderedTypes;
  984. for (auto type : mContext->mResolvedTypes)
  985. {
  986. numEntries++;
  987. BF_ASSERT((type != NULL) || (mPassInstance->HasFailed()));
  988. if (!type->IsReified())
  989. continue;
  990. orderedTypes.Add(type);
  991. CompileLog("TypeId:%d %s\n", type->mTypeId, bfModule->TypeToString(type).c_str());
  992. if ((type != NULL) && (type->IsObjectOrInterface()))
  993. {
  994. numConcreteTypes++;
  995. auto typeInst = type->ToTypeInstance();
  996. if (typeInst->mModule == NULL)
  997. {
  998. BF_ASSERT(mPassInstance->HasFailed());
  999. continue;
  1000. }
  1001. }
  1002. }
  1003. {
  1004. BP_ZONE("BfCompiler::CreateVData sort orderedTypes");
  1005. orderedTypes.Sort([](BfType* lhs, BfType* rhs)
  1006. {
  1007. return lhs->mTypeId < rhs->mTypeId;
  1008. });
  1009. }
  1010. BfLogSysM("TypeEntries: %d ConcreteTypes: %d\n", numEntries, numConcreteTypes);
  1011. HashContext vdataHashCtx;
  1012. //vdataHashCtx.mDbgViz = true;
  1013. vdataHashCtx.Mixin(bfModule->mProject->mVDataConfigHash);
  1014. Array<TestMethod> testMethods;
  1015. if (project->mTargetType == BfTargetType_BeefTest)
  1016. GetTestMethods(bfModule, testMethods, vdataHashCtx);
  1017. Array<String*> typeNameList;
  1018. defer(
  1019. {
  1020. for (auto str : typeNameList)
  1021. delete str;
  1022. });
  1023. struct _SortedTypeEntry
  1024. {
  1025. BfTypeInstance* mTypeInstance;
  1026. String* mTypeName;
  1027. int mPriority;
  1028. _SortedTypeEntry()
  1029. {
  1030. mPriority = 0;
  1031. mTypeInstance = NULL;
  1032. mTypeName = NULL;
  1033. }
  1034. _SortedTypeEntry(BfModule* module, BfTypeInstance* typeInstance, Array<String*>& nameList, String*& namePtr)
  1035. {
  1036. if (namePtr == NULL)
  1037. {
  1038. namePtr = new String(module->TypeToString(typeInstance));
  1039. nameList.Add(namePtr);
  1040. }
  1041. mTypeName = namePtr;
  1042. mTypeInstance = typeInstance;
  1043. mPriority = 0;
  1044. }
  1045. static void Sort(Array<_SortedTypeEntry>& list)
  1046. {
  1047. list.Sort(
  1048. [](const _SortedTypeEntry& lhs, const _SortedTypeEntry& rhs)
  1049. {
  1050. if (lhs.mPriority != rhs.mPriority)
  1051. return lhs.mPriority > rhs.mPriority;
  1052. return *lhs.mTypeName < *rhs.mTypeName;
  1053. });
  1054. }
  1055. static void SortRev(Array<_SortedTypeEntry>& list)
  1056. {
  1057. list.Sort(
  1058. [](const _SortedTypeEntry& lhs, const _SortedTypeEntry& rhs)
  1059. {
  1060. if (lhs.mPriority != rhs.mPriority)
  1061. return lhs.mPriority < rhs.mPriority;
  1062. return *lhs.mTypeName > *rhs.mTypeName;
  1063. });
  1064. }
  1065. };
  1066. Array<_SortedTypeEntry> preStaticInitList;
  1067. Array<_SortedTypeEntry> staticMarkList;
  1068. Array<_SortedTypeEntry> staticTLSList;
  1069. Array<BfType*> vdataTypeList;
  1070. HashSet<BfModule*> usedModuleSet;
  1071. HashSet<BfType*> reflectSkipTypeSet;
  1072. HashSet<BfType*> reflectFieldTypeSet;
  1073. vdataHashCtx.MixinStr(project->mStartupObject);
  1074. vdataHashCtx.Mixin(project->mTargetType);
  1075. for (auto type : orderedTypes)
  1076. {
  1077. if (type == NULL)
  1078. continue;
  1079. if (type->IsTemporary())
  1080. continue;
  1081. if ((type->IsGenericParam()) || (type->IsUnspecializedTypeVariation()))
  1082. continue;
  1083. auto typeInst = type->ToTypeInstance();
  1084. if ((typeInst != NULL) && (!typeInst->IsReified()) && (!typeInst->IsUnspecializedType()))
  1085. continue;
  1086. if (!IsTypeUsed(type, project))
  1087. continue;
  1088. vdataTypeList.push_back(type);
  1089. vdataHashCtx.Mixin(type->mTypeId);
  1090. BF_ASSERT((type != NULL) || (mPassInstance->HasFailed()));
  1091. if ((type != NULL) && (typeInst != NULL))
  1092. {
  1093. auto module = typeInst->mModule;
  1094. if (module == NULL)
  1095. continue;
  1096. if (type->IsEnum())
  1097. {
  1098. for (auto& fieldInst : typeInst->mFieldInstances)
  1099. {
  1100. auto fieldDef = fieldInst.GetFieldDef();
  1101. if (fieldDef == NULL)
  1102. continue;
  1103. if (!fieldDef->IsEnumCaseEntry())
  1104. continue;
  1105. if (fieldInst.mResolvedType->IsTuple())
  1106. reflectFieldTypeSet.Add(fieldInst.mResolvedType);
  1107. }
  1108. }
  1109. if (type->IsInterface())
  1110. vdataHashCtx.Mixin(typeInst->mSlotNum);
  1111. vdataHashCtx.Mixin(typeInst->mAlwaysIncludeFlags);
  1112. vdataHashCtx.Mixin(typeInst->mHasBeenInstantiated);
  1113. if (!module->mIsScratchModule)
  1114. {
  1115. BF_ASSERT(module->mIsReified);
  1116. if (usedModuleSet.Add(module))
  1117. {
  1118. CompileLog("UsedModule %p %s\n", module, module->mModuleName.c_str());
  1119. HashModuleVData(module, vdataHashCtx);
  1120. }
  1121. }
  1122. vdataHashCtx.MixinStr(module->mModuleName);
  1123. vdataHashCtx.Mixin(typeInst->mTypeDef->mSignatureHash);
  1124. vdataHashCtx.Mixin(module->mHasForceLinkMarker);
  1125. for (auto iface : typeInst->mInterfaces)
  1126. {
  1127. vdataHashCtx.Mixin(iface.mInterfaceType->mTypeId);
  1128. vdataHashCtx.Mixin(iface.mDeclaringType->mTypeCode);
  1129. vdataHashCtx.Mixin(iface.mDeclaringType->mProject);
  1130. }
  1131. if (!typeInst->IsUnspecializedType())
  1132. {
  1133. for (auto& methodInstGroup : typeInst->mMethodInstanceGroups)
  1134. {
  1135. bool isImplementedAndReified = (methodInstGroup.IsImplemented()) && (methodInstGroup.mDefault != NULL) &&
  1136. (methodInstGroup.mDefault->mIsReified) && (!methodInstGroup.mDefault->mIsUnspecialized);
  1137. vdataHashCtx.Mixin(isImplementedAndReified);
  1138. }
  1139. }
  1140. // Could be necessary if a base type in another project adds new virtual methods (for example)
  1141. auto baseType = typeInst->mBaseType;
  1142. while (baseType != NULL)
  1143. {
  1144. vdataHashCtx.Mixin(baseType->mTypeDef->mSignatureHash);
  1145. baseType = baseType->mBaseType;
  1146. }
  1147. //TODO: What was this for?
  1148. // if (module->mProject != bfModule->mProject)
  1149. // {
  1150. // if ((module->mProject != NULL) && (module->mProject->mTargetType == BfTargetType_BeefDynLib))
  1151. // continue;
  1152. // }
  1153. String* typeName = NULL;
  1154. if ((typeInst->mHasStaticInitMethod) || (typeInst->mHasStaticDtorMethod))
  1155. preStaticInitList.Add(_SortedTypeEntry(bfModule, typeInst, typeNameList, typeName));
  1156. if ((typeInst->mHasStaticMarkMethod) && (mOptions.mEnableRealtimeLeakCheck))
  1157. staticMarkList.Add(_SortedTypeEntry(bfModule, typeInst, typeNameList, typeName));
  1158. if ((typeInst->mHasTLSFindMethod) && (mOptions.mEnableRealtimeLeakCheck))
  1159. staticTLSList.Add(_SortedTypeEntry(bfModule, typeInst, typeNameList, typeName));
  1160. }
  1161. }
  1162. int lastModuleRevision = bfModule->mRevision;
  1163. Val128 vdataHash = vdataHashCtx.Finish128();
  1164. bool wantsRebuild = vdataHash != bfModule->mDataHash;
  1165. if (bfModule->mHadBuildError)
  1166. wantsRebuild = true;
  1167. // If we did one of those 'hot compile' partial vdata builds, now build the whole thing
  1168. if ((!IsHotCompile()) && (bfModule->mHadHotObjectWrites))
  1169. wantsRebuild = true;
  1170. if (mOptions.mHotProject != NULL)
  1171. {
  1172. HashContext vdataHashCtxEx;
  1173. vdataHashCtxEx.Mixin(mOptions.mHotProject->mName);
  1174. vdataHashCtxEx.Mixin((int)mHotState->mNewlySlottedTypeIds.size());
  1175. for (auto typeId : mHotState->mNewlySlottedTypeIds)
  1176. vdataHashCtxEx.Mixin(typeId);
  1177. vdataHashCtxEx.Mixin((int)mHotState->mSlotDefineTypeIds.size());
  1178. for (auto typeId : mHotState->mSlotDefineTypeIds)
  1179. vdataHashCtxEx.Mixin(typeId);
  1180. Val128 vdataHashEx = vdataHashCtxEx.Finish128();
  1181. if (mHotState->mVDataHashEx.IsZero())
  1182. {
  1183. if (!mHotState->mNewlySlottedTypeIds.IsEmpty())
  1184. wantsRebuild = true;
  1185. if (!mHotState->mSlotDefineTypeIds.IsEmpty())
  1186. wantsRebuild = true;
  1187. }
  1188. else
  1189. {
  1190. if (vdataHashEx != mHotState->mVDataHashEx)
  1191. wantsRebuild = true;
  1192. }
  1193. mHotState->mVDataHashEx = vdataHashEx;
  1194. }
  1195. if ((wantsRebuild) || (bfModule->mIsModuleMutable))
  1196. {
  1197. bfModule->StartNewRevision();
  1198. if (bfModule->mAwaitingInitFinish)
  1199. bfModule->FinishInit();
  1200. }
  1201. // We add the string hash into vdata hash later
  1202. bfModule->mDataHash = vdataHash;//vdataPreStringHash;
  1203. // This handles "no StartNewRevision" 'else' case, but also handles if vdata failed to complete from a previous compilation
  1204. if (!bfModule->mIsModuleMutable)
  1205. {
  1206. CompileLog("VData unchanged, skipping\n");
  1207. return;
  1208. }
  1209. BfTypeInstance* stringType = bfModule->ResolveTypeDef(mStringTypeDef, BfPopulateType_Data)->ToTypeInstance();
  1210. BfTypeInstance* stringViewType = bfModule->ResolveTypeDef(mStringViewTypeDef, BfPopulateType_Data)->ToTypeInstance();
  1211. BfTypeInstance* reflectSpecializedTypeInstance = bfModule->ResolveTypeDef(mReflectSpecializedGenericType)->ToTypeInstance();
  1212. BfTypeInstance* reflectUnspecializedTypeInstance = bfModule->ResolveTypeDef(mReflectUnspecializedGenericType)->ToTypeInstance();
  1213. BfTypeInstance* reflectArrayTypeInstance = bfModule->ResolveTypeDef(mReflectArrayType)->ToTypeInstance();
  1214. bool madeBfTypeData = false;
  1215. auto typeDefType = mContext->mBfTypeType;
  1216. bool needsFullTypeList = bfModule->IsMethodImplementedAndReified(typeDefType, "GetType");
  1217. bool needsTypeList = needsFullTypeList || bfModule->IsMethodImplementedAndReified(typeDefType, "GetType_");
  1218. bool needsObjectTypeData = needsTypeList || bfModule->IsMethodImplementedAndReified(vdataContext->mBfObjectType, "RawGetType") || bfModule->IsMethodImplementedAndReified(vdataContext->mBfObjectType, "GetType");
  1219. bool needsTypeNames = bfModule->IsMethodImplementedAndReified(typeDefType, "GetName") || bfModule->IsMethodImplementedAndReified(typeDefType, "GetFullName");
  1220. bool needsStringLiteralList = (mOptions.mAllowHotSwapping) || (bfModule->IsMethodImplementedAndReified(stringType, "Intern")) || (bfModule->IsMethodImplementedAndReified(stringViewType, "Intern"));
  1221. BfCreateTypeDataContext createTypeDataCtx;
  1222. if (!needsTypeList)
  1223. {
  1224. reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mTypeTypeDef));
  1225. reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectTypeInstanceTypeDef));
  1226. }
  1227. if (!needsFullTypeList)
  1228. {
  1229. reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectSpecializedGenericType));
  1230. reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectUnspecializedGenericType));
  1231. reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectConstExprType));
  1232. reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectArrayType));
  1233. reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectGenericParamType));
  1234. reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectPointerType));
  1235. reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectSizedArrayType));
  1236. reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectRefType));
  1237. }
  1238. HashSet<BfType*> boxeeSet;
  1239. for (auto type : vdataTypeList)
  1240. {
  1241. auto typeInst = type->ToTypeInstance();
  1242. if ((!type->IsReified()) || (type->IsUnspecializedType()))
  1243. continue;
  1244. if (type->IsBoxed())
  1245. boxeeSet.Add(typeInst->GetUnderlyingType());
  1246. }
  1247. int usedTypeCount = 0;
  1248. HashSet<BfType*> vDataTypeSet;
  1249. SmallVector<BfIRValue, 256> typeDataVector;
  1250. Array<BfType*> usedTypeDataVector;
  1251. for (auto type : vdataTypeList)
  1252. {
  1253. if (type->IsTypeAlias())
  1254. continue;
  1255. if (type->IsTypeInstance())
  1256. BF_ASSERT(!type->IsIncomplete());
  1257. auto typeInst = type->ToTypeInstance();
  1258. if ((typeInst != NULL) && (!typeInst->IsReified()) && (!typeInst->IsUnspecializedType()))
  1259. continue;
  1260. bool needsTypeData = (needsFullTypeList) || ((type->IsObject()) && (needsObjectTypeData));
  1261. bool needsVData = (type->IsObject()) && (typeInst->HasBeenInstantiated());
  1262. if ((needsObjectTypeData) && (boxeeSet.Contains(typeInst)))
  1263. needsTypeData = true;
  1264. bool forceReflectFields = false;
  1265. if (bfModule->mProject->mReferencedTypeData.Contains(type))
  1266. {
  1267. needsTypeData = true;
  1268. if (type->IsEnum())
  1269. forceReflectFields = true;
  1270. }
  1271. if (reflectSkipTypeSet.Contains(type))
  1272. {
  1273. if (!bfModule->mProject->mReferencedTypeData.Contains(type))
  1274. {
  1275. needsTypeData = false;
  1276. needsVData = false;
  1277. }
  1278. }
  1279. BfIRValue typeVariable;
  1280. if ((needsTypeData) || (needsVData))
  1281. {
  1282. if (reflectFieldTypeSet.Contains(type))
  1283. {
  1284. needsTypeData = true;
  1285. forceReflectFields = true;
  1286. }
  1287. /*else if (reflectTypeSet.Contains(type))
  1288. {
  1289. needsTypeData = true;
  1290. needsVData = true;
  1291. }*/
  1292. if (needsVData)
  1293. vDataTypeSet.Add(type);
  1294. typeVariable = bfModule->CreateTypeData(type, createTypeDataCtx, forceReflectFields, needsTypeData, needsTypeNames, needsVData);
  1295. if (typeVariable)
  1296. usedTypeDataVector.Add(type);
  1297. }
  1298. else if ((type->IsInterface()) && (typeInst->mSlotNum >= 0))
  1299. {
  1300. bfModule->CreateSlotOfs(typeInst);
  1301. }
  1302. usedTypeCount++;
  1303. type->mDirty = false;
  1304. if (needsTypeList)
  1305. {
  1306. int typeId = type->mTypeId;
  1307. if (typeId == -1)
  1308. continue;
  1309. if (typeId >= (int)typeDataVector.size())
  1310. typeDataVector.resize(typeId + 1);
  1311. typeDataVector[typeId] = typeVariable;
  1312. }
  1313. }
  1314. for (int typeId = 0; typeId < (int)typeDataVector.size(); typeId++)
  1315. {
  1316. if (!typeDataVector[typeId])
  1317. typeDataVector[typeId] = bfModule->GetDefaultValue(typeDefType);
  1318. }
  1319. // We only need 'sTypes' if we actually reference it
  1320. //
  1321. {
  1322. auto typeDefPtrType = bfModule->CreatePointerType(typeDefType);
  1323. StringT<128> typesVariableName;
  1324. BfMangler::MangleStaticFieldName(typesVariableName, GetMangleKind(), typeDefType->ToTypeInstance(), "sTypes", typeDefPtrType);
  1325. auto arrayType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->MapType(typeDefType), (int)typeDataVector.size());
  1326. auto typeDataConst = bfModule->mBfIRBuilder->CreateConstAgg_Value(arrayType, typeDataVector);
  1327. BfIRValue typeDataArray = bfModule->mBfIRBuilder->CreateGlobalVariable(arrayType, true, BfIRLinkageType_External,
  1328. typeDataConst, typesVariableName);
  1329. StringT<128> typeCountVariableName;
  1330. BfMangler::MangleStaticFieldName(typeCountVariableName, GetMangleKind(), typeDefType->ToTypeInstance(), "sTypeCount", bfModule->GetPrimitiveType(BfTypeCode_Int32));
  1331. bfModule->mBfIRBuilder->CreateGlobalVariable(bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_Int32)), true, BfIRLinkageType_External,
  1332. bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, (int)typeDataVector.size()), typeCountVariableName);
  1333. }
  1334. HashSet<int> foundStringIds;
  1335. for (int stringId : bfModule->mStringPoolRefs)
  1336. foundStringIds.Add(stringId);
  1337. Array<BfModule*> orderedUsedModules;
  1338. for (auto module : usedModuleSet)
  1339. orderedUsedModules.push_back(module);
  1340. std::sort(orderedUsedModules.begin(), orderedUsedModules.end(), [] (BfModule* lhs, BfModule* rhs)
  1341. {
  1342. return lhs->mModuleName < rhs->mModuleName;
  1343. });
  1344. Array<BfMethodInstance*> dllMethods;
  1345. Array<BfIRValue> forceLinkValues;
  1346. HashSet<int> dllNameSet;
  1347. Array<BfCompiler::StringValueEntry> stringValueEntries;
  1348. for (auto module : orderedUsedModules)
  1349. {
  1350. CheckModuleStringRefs(module, bfModule, lastModuleRevision, foundStringIds, dllNameSet, dllMethods, stringValueEntries);
  1351. if ((module->mHasForceLinkMarker) &&
  1352. ((!isHotCompile) || (module->mHadHotObjectWrites)) &&
  1353. (mOptions.mPlatformType != BfPlatformType_Wasm))
  1354. forceLinkValues.Add(bfModule->CreateForceLinkMarker(module, NULL));
  1355. }
  1356. if (!forceLinkValues.IsEmpty())
  1357. {
  1358. auto elemType = bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_Int8));
  1359. auto arrayType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->MapType(elemType), (int)forceLinkValues.size());
  1360. auto typeDataConst = bfModule->mBfIRBuilder->CreateConstAgg_Value(arrayType, forceLinkValues);
  1361. BfIRValue typeDataArray = bfModule->mBfIRBuilder->CreateGlobalVariable(arrayType, true, BfIRLinkageType_Internal,
  1362. typeDataConst, "FORCELINK_MODULES");
  1363. }
  1364. // Generate strings array
  1365. {
  1366. if (!needsStringLiteralList)
  1367. {
  1368. stringValueEntries.Clear();
  1369. }
  1370. std::sort(stringValueEntries.begin(), stringValueEntries.end(),
  1371. [](const StringValueEntry& lhs, const StringValueEntry& rhs)
  1372. {
  1373. return lhs.mId < rhs.mId;
  1374. });
  1375. auto stringPtrType = bfModule->CreatePointerType(stringType);
  1376. auto stringPtrIRType = bfModule->mBfIRBuilder->MapTypeInstPtr(stringType);
  1377. StringT<128> stringsVariableName;
  1378. BfMangler::MangleStaticFieldName(stringsVariableName, GetMangleKind(), stringType->ToTypeInstance(), "sStringLiterals", stringPtrType);
  1379. Array<BfIRValue> stringList;
  1380. stringList.Add(bfModule->mBfIRBuilder->CreateConstNull(stringPtrIRType));
  1381. for (auto& stringValueEntry : stringValueEntries)
  1382. stringList.Add(stringValueEntry.mStringVal);
  1383. stringList.Add(bfModule->mBfIRBuilder->CreateConstNull(stringPtrIRType));
  1384. BfIRType stringArrayType = bfModule->mBfIRBuilder->GetSizedArrayType(stringPtrIRType, (int)stringList.size());
  1385. auto stringArray = bfModule->mBfIRBuilder->CreateConstAgg_Value(stringArrayType, stringList);
  1386. auto stringArrayVar = bfModule->mBfIRBuilder->CreateGlobalVariable(stringArrayType, true, BfIRLinkageType_External, stringArray, stringsVariableName);
  1387. if (bfModule->mBfIRBuilder->DbgHasInfo())
  1388. {
  1389. auto dbgArrayType = bfModule->mBfIRBuilder->DbgCreateArrayType(stringList.size() * mSystem->mPtrSize * 8, mSystem->mPtrSize * 8, bfModule->mBfIRBuilder->DbgGetType(stringPtrType), (int)stringList.size());
  1390. bfModule->mBfIRBuilder->DbgCreateGlobalVariable(bfModule->mDICompileUnit, stringsVariableName, stringsVariableName, BfIRMDNode(), 0, dbgArrayType, false, stringArrayVar);
  1391. }
  1392. }
  1393. // Generate string ID array
  1394. {
  1395. auto stringType = bfModule->ResolveTypeDef(mStringTypeDef, BfPopulateType_Data)->ToTypeInstance();
  1396. auto stringPtrType = bfModule->CreatePointerType(stringType);
  1397. auto stringPtrIRType = bfModule->mBfIRBuilder->MapTypeInstPtr(stringType);
  1398. StringT<128> stringsVariableName;
  1399. BfMangler::MangleStaticFieldName(stringsVariableName, GetMangleKind(), stringType->ToTypeInstance(), "sIdStringLiterals", stringPtrType);
  1400. Array<BfIRValue> stringList;
  1401. stringList.Resize(createTypeDataCtx.mUsedStringIdMap.size());
  1402. for (auto& kv : createTypeDataCtx.mUsedStringIdMap)
  1403. {
  1404. stringList[kv.mValue] = bfModule->mStringObjectPool[kv.mKey];
  1405. }
  1406. BfIRType stringArrayType = bfModule->mBfIRBuilder->GetSizedArrayType(stringPtrIRType, (int)createTypeDataCtx.mUsedStringIdMap.size());
  1407. auto stringArray = bfModule->mBfIRBuilder->CreateConstAgg_Value(stringArrayType, stringList);
  1408. auto stringArrayVar = bfModule->mBfIRBuilder->CreateGlobalVariable(stringArrayType, true, BfIRLinkageType_External, stringArray, stringsVariableName);
  1409. if (bfModule->mBfIRBuilder->DbgHasInfo())
  1410. {
  1411. auto dbgArrayType = bfModule->mBfIRBuilder->DbgCreateArrayType(stringList.size() * mSystem->mPtrSize * 8, mSystem->mPtrSize * 8, bfModule->mBfIRBuilder->DbgGetType(stringPtrType), (int)stringList.size());
  1412. bfModule->mBfIRBuilder->DbgCreateGlobalVariable(bfModule->mDICompileUnit, stringsVariableName, stringsVariableName, BfIRMDNode(), 0, dbgArrayType, false, stringArrayVar);
  1413. }
  1414. }
  1415. BfIRFunction loadSharedLibFunc = CreateLoadSharedLibraries(bfModule, dllMethods);
  1416. BfIRType nullPtrType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr));
  1417. BfIRType voidType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_None));
  1418. BfIRType int32Type = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_Int32));
  1419. Array<_SortedTypeEntry> staticInitList;
  1420. // Populate staticInitList
  1421. {
  1422. Dictionary<int, BfTypeInstance*> pendingIDToInstanceMap;
  1423. HashSet<BfTypeInstance*> handledTypes;
  1424. BfType* staticInitPriorityAttributeType = vdataContext->mUnreifiedModule->ResolveTypeDef(mStaticInitPriorityAttributeTypeDef);
  1425. BfType* staticInitAfterAttributeType = vdataContext->mUnreifiedModule->ResolveTypeDef(mStaticInitAfterAttributeTypeDef);
  1426. bool forceAdd = false;
  1427. for (int pass = 0; true; pass++)
  1428. {
  1429. bool hadAdd = false;
  1430. for (auto& mapEntry : preStaticInitList)
  1431. {
  1432. auto typeInst = mapEntry.mTypeInstance;
  1433. if ((typeInst != NULL) && (!typeInst->IsUnspecializedType()))
  1434. {
  1435. if (pass == 0)
  1436. {
  1437. int priority = 0;
  1438. bool hadInitAfterAttribute = false;
  1439. if (typeInst->mCustomAttributes != NULL)
  1440. {
  1441. for (auto& customAttr : typeInst->mCustomAttributes->mAttributes)
  1442. {
  1443. if (customAttr.mType == staticInitAfterAttributeType)
  1444. hadInitAfterAttribute = true;
  1445. if (customAttr.mType == staticInitPriorityAttributeType)
  1446. {
  1447. if (customAttr.mCtorArgs.size() == 1)
  1448. {
  1449. auto constant = typeInst->mConstHolder->GetConstant(customAttr.mCtorArgs[0]);
  1450. if (constant != NULL)
  1451. priority = constant->mInt32;
  1452. }
  1453. }
  1454. }
  1455. }
  1456. if (!hadInitAfterAttribute)
  1457. {
  1458. mapEntry.mPriority = priority;
  1459. staticInitList.Add(mapEntry);
  1460. mapEntry.mTypeInstance = NULL;
  1461. }
  1462. else
  1463. {
  1464. pendingIDToInstanceMap.TryAdd(typeInst->mTypeId, typeInst);
  1465. }
  1466. }
  1467. else
  1468. {
  1469. if (pendingIDToInstanceMap.ContainsKey(typeInst->mTypeId))
  1470. {
  1471. bool doAdd = true;
  1472. if (!forceAdd)
  1473. {
  1474. for (auto& customAttr : typeInst->mCustomAttributes->mAttributes)
  1475. {
  1476. if (customAttr.mType == staticInitAfterAttributeType)
  1477. {
  1478. if (customAttr.mCtorArgs.size() == 0)
  1479. {
  1480. doAdd = false;
  1481. }
  1482. else
  1483. {
  1484. auto ctorArg = customAttr.mCtorArgs[0];
  1485. auto constant = typeInst->mConstHolder->GetConstant(ctorArg);
  1486. if (constant != NULL)
  1487. {
  1488. int refTypeId = constant->mInt32;
  1489. if (pendingIDToInstanceMap.ContainsKey(refTypeId))
  1490. doAdd = false;
  1491. }
  1492. }
  1493. }
  1494. }
  1495. }
  1496. if (doAdd)
  1497. {
  1498. staticInitList.Add(mapEntry);
  1499. pendingIDToInstanceMap.Remove(typeInst->mTypeId);
  1500. hadAdd = true;
  1501. }
  1502. }
  1503. }
  1504. }
  1505. }
  1506. if (pass == 0)
  1507. {
  1508. _SortedTypeEntry::Sort(staticInitList);
  1509. }
  1510. if ((pass > 0) && (!hadAdd) && (pendingIDToInstanceMap.size() > 0)) // Circular ref?
  1511. forceAdd = true;
  1512. if (pendingIDToInstanceMap.size() == 0)
  1513. break;
  1514. }
  1515. }
  1516. /// Generate "BfCallAllStaticDtors"
  1517. BfIRFunction dtorFunc;
  1518. {
  1519. SmallVector<BfIRType, 2> paramTypes;
  1520. auto dtorFuncType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes, false);
  1521. String dtorName = (mOptions.mPlatformType == BfPlatformType_Wasm) ? "BeefDone" : "BfCallAllStaticDtors";
  1522. dtorFunc = bfModule->mBfIRBuilder->CreateFunction(dtorFuncType, BfIRLinkageType_External, dtorName);
  1523. bfModule->SetupIRMethod(NULL, dtorFunc, false);
  1524. bfModule->mBfIRBuilder->SetActiveFunction(dtorFunc);
  1525. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1526. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1527. for (int i = staticInitList.mSize - 1; i >= 0; i--)
  1528. {
  1529. auto typeInst = staticInitList[i].mTypeInstance;
  1530. if (!typeInst->mHasStaticDtorMethod)
  1531. continue;
  1532. for (auto& methodGroup : typeInst->mMethodInstanceGroups)
  1533. {
  1534. auto methodInstance = methodGroup.mDefault;
  1535. if ((methodInstance != NULL) &&
  1536. (methodInstance->mMethodDef->mIsStatic) &&
  1537. (methodInstance->mMethodDef->mMethodType == BfMethodType_Dtor) &&
  1538. ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
  1539. {
  1540. if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
  1541. continue;
  1542. if (methodInstance->mHotMethod != NULL)
  1543. methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
  1544. auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
  1545. bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
  1546. }
  1547. }
  1548. }
  1549. bfModule->mBfIRBuilder->CreateRetVoid();
  1550. }
  1551. auto targetType = project->mTargetType;
  1552. if ((targetType == BfTargetType_BeefWindowsApplication) && (mOptions.mPlatformType != BfPlatformType_Windows))
  1553. targetType = BfTargetType_BeefConsoleApplication;
  1554. bool isConsoleApplication = (targetType == BfTargetType_BeefConsoleApplication) || (targetType == BfTargetType_BeefTest);
  1555. if ((targetType == BfTargetType_BeefWindowsApplication) && (mOptions.mPlatformType != BfPlatformType_Windows))
  1556. isConsoleApplication = true;
  1557. bool isDllMain = (targetType == BfTargetType_BeefLib_DynamicLib) && (mOptions.mPlatformType == BfPlatformType_Windows);
  1558. bool isPosixDynLib = ((targetType == BfTargetType_BeefLib_DynamicLib) || (targetType == BfTargetType_BeefLib_StaticLib)) &&
  1559. (mOptions.mPlatformType != BfPlatformType_Windows);
  1560. bool mainHasArgs = (targetType != BfTargetType_BeefLib_DynamicLib) && (targetType != BfTargetType_BeefLib_StaticLib) &&
  1561. (mOptions.mPlatformType != BfPlatformType_Wasm);
  1562. bool mainHasRet = ((targetType != BfTargetType_BeefLib_DynamicLib) && (targetType != BfTargetType_BeefLib_StaticLib)) || (isDllMain);
  1563. // Generate "main"
  1564. if (!IsHotCompile())
  1565. {
  1566. int rtFlags = 0;
  1567. BfIRFunctionType mainFuncType;
  1568. BfIRFunction mainFunc;
  1569. BfIRFunctionType shutdownFuncType;
  1570. BfIRFunction shutdownFunc;
  1571. if (isConsoleApplication)
  1572. {
  1573. SmallVector<BfIRType, 2> paramTypes;
  1574. paramTypes.push_back(int32Type);
  1575. paramTypes.push_back(nullPtrType);
  1576. mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
  1577. mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "main");
  1578. bfModule->SetupIRMethod(NULL, mainFunc, false);
  1579. }
  1580. else if (isDllMain)
  1581. {
  1582. SmallVector<BfIRType, 4> paramTypes;
  1583. paramTypes.push_back(nullPtrType); // hinstDLL
  1584. paramTypes.push_back(int32Type); // fdwReason
  1585. paramTypes.push_back(nullPtrType); // lpvReserved
  1586. mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
  1587. mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "DllMain");
  1588. if (mOptions.mMachineType == BfMachineType_x86)
  1589. bfModule->mBfIRBuilder->SetFuncCallingConv(mainFunc, BfIRCallingConv_StdCall);
  1590. bfModule->SetupIRMethod(NULL, mainFunc, false);
  1591. rtFlags = 0x10; // BfRtFlags_NoThreadExitWait
  1592. }
  1593. else if (targetType == BfTargetType_BeefWindowsApplication)
  1594. {
  1595. SmallVector<BfIRType, 4> paramTypes;
  1596. paramTypes.push_back(nullPtrType); // hInstance
  1597. paramTypes.push_back(nullPtrType); // hPrevInstance
  1598. paramTypes.push_back(nullPtrType); // lpCmdLine
  1599. paramTypes.push_back(int32Type); // nCmdShow
  1600. mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
  1601. mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "WinMain");
  1602. if (mOptions.mMachineType == BfMachineType_x86)
  1603. bfModule->mBfIRBuilder->SetFuncCallingConv(mainFunc, BfIRCallingConv_StdCall);
  1604. bfModule->SetupIRMethod(NULL, mainFunc, false);
  1605. }
  1606. else
  1607. {
  1608. BfIRFunction combinedFunc;
  1609. SmallVector<BfIRType, 2> paramTypes;
  1610. if (mainHasArgs)
  1611. {
  1612. paramTypes.push_back(int32Type);
  1613. paramTypes.push_back(nullPtrType);
  1614. }
  1615. mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(mainHasRet ? int32Type : voidType, paramTypes, false);
  1616. mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "BeefStart");
  1617. bfModule->SetupIRMethod(NULL, mainFunc, false);
  1618. combinedFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "BeefMain");
  1619. bfModule->SetupIRMethod(NULL, combinedFunc, false);
  1620. paramTypes.clear();
  1621. shutdownFuncType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes, false);
  1622. shutdownFunc = bfModule->mBfIRBuilder->CreateFunction(shutdownFuncType, BfIRLinkageType_External, "BeefStop");
  1623. bfModule->SetupIRMethod(NULL, shutdownFunc, false);
  1624. bfModule->mBfIRBuilder->SetActiveFunction(combinedFunc);
  1625. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1626. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1627. SmallVector<BfIRValue, 1> args;
  1628. if (mainHasArgs)
  1629. {
  1630. args.push_back(bfModule->mBfIRBuilder->GetArgument(0));
  1631. args.push_back(bfModule->mBfIRBuilder->GetArgument(1));
  1632. }
  1633. auto res = bfModule->mBfIRBuilder->CreateCall(mainFunc, args);
  1634. args.clear();
  1635. bfModule->mBfIRBuilder->CreateCall(shutdownFunc, args);
  1636. if (mainHasRet)
  1637. bfModule->mBfIRBuilder->CreateRet(res);
  1638. else
  1639. bfModule->mBfIRBuilder->CreateRetVoid();
  1640. }
  1641. bfModule->mBfIRBuilder->SetActiveFunction(mainFunc);
  1642. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1643. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1644. if (rtFlags != 0)
  1645. {
  1646. auto addRtFlagMethod = bfModule->GetInternalMethod("AddRtFlags", 1);
  1647. if (addRtFlagMethod)
  1648. {
  1649. SmallVector<BfIRValue, 1> args;
  1650. args.push_back(bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, rtFlags));
  1651. bfModule->mBfIRBuilder->CreateCall(addRtFlagMethod.mFunc, args);
  1652. }
  1653. }
  1654. if ((mOptions.mPlatformType != BfPlatformType_Windows) && (mainHasArgs) &&
  1655. ((targetType == BfTargetType_BeefConsoleApplication) || (targetType == BfTargetType_BeefApplication_StaticLib) ||
  1656. (targetType == BfTargetType_BeefApplication_DynamicLib) || (targetType == BfTargetType_BeefTest)))
  1657. {
  1658. SmallVector<BfIRType, 2> paramTypes;
  1659. paramTypes.push_back(int32Type);
  1660. paramTypes.push_back(nullPtrType);
  1661. auto setCmdLineFuncType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes, false);
  1662. auto setCmdLineFunc = bfModule->mBfIRBuilder->CreateFunction(setCmdLineFuncType, BfIRLinkageType_External, "BfpSystem_SetCommandLine");
  1663. bfModule->SetupIRMethod(NULL, setCmdLineFunc, false);
  1664. SmallVector<BfIRValue, 2> args;
  1665. args.push_back(bfModule->mBfIRBuilder->GetArgument(0));
  1666. args.push_back(bfModule->mBfIRBuilder->GetArgument(1));
  1667. bfModule->mBfIRBuilder->CreateCall(setCmdLineFunc, args);
  1668. }
  1669. BfIRBlock initSkipBlock;
  1670. if (isDllMain)
  1671. {
  1672. auto initBlock = bfModule->mBfIRBuilder->CreateBlock("doInit", false);
  1673. initSkipBlock = bfModule->mBfIRBuilder->CreateBlock("skipInit", false);
  1674. auto cmpResult = bfModule->mBfIRBuilder->CreateCmpEQ(bfModule->mBfIRBuilder->GetArgument(1), bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 1));
  1675. bfModule->mBfIRBuilder->CreateCondBr(cmpResult, initBlock, initSkipBlock);
  1676. bfModule->mBfIRBuilder->AddBlock(initBlock);
  1677. bfModule->mBfIRBuilder->SetInsertPoint(initBlock);
  1678. auto moduleMethodInstance = bfModule->GetInternalMethod("SetModuleHandle", 1);
  1679. if (moduleMethodInstance)
  1680. {
  1681. SmallVector<BfIRValue, 1> args;
  1682. args.push_back(bfModule->mBfIRBuilder->GetArgument(0));
  1683. bfModule->mBfIRBuilder->CreateCall(moduleMethodInstance.mFunc, args);
  1684. }
  1685. }
  1686. // Do the LoadLibrary calls below priority 100
  1687. bool didSharedLibLoad = false;
  1688. auto _CheckSharedLibLoad = [&]()
  1689. {
  1690. if (!didSharedLibLoad)
  1691. {
  1692. bfModule->mBfIRBuilder->CreateCall(loadSharedLibFunc, SmallVector<BfIRValue, 0>());
  1693. didSharedLibLoad = true;
  1694. }
  1695. };
  1696. for (auto& staticInitEntry : staticInitList)
  1697. {
  1698. auto typeInst = staticInitEntry.mTypeInstance;
  1699. if (!typeInst->mHasStaticInitMethod)
  1700. continue;
  1701. if (staticInitEntry.mPriority < 100)
  1702. _CheckSharedLibLoad();
  1703. for (auto& methodGroup : typeInst->mMethodInstanceGroups)
  1704. {
  1705. auto methodInstance = methodGroup.mDefault;
  1706. if ((methodInstance != NULL) &&
  1707. (methodInstance->mMethodDef->mIsStatic) &&
  1708. (methodInstance->mMethodDef->mMethodType == BfMethodType_Ctor) &&
  1709. ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
  1710. {
  1711. if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
  1712. continue;
  1713. auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
  1714. if (methodInstance->mHotMethod != NULL)
  1715. methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
  1716. bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
  1717. }
  1718. }
  1719. }
  1720. _CheckSharedLibLoad();
  1721. if (initSkipBlock)
  1722. {
  1723. bfModule->mBfIRBuilder->CreateBr(initSkipBlock);
  1724. bfModule->mBfIRBuilder->AddBlock(initSkipBlock);
  1725. bfModule->mBfIRBuilder->SetInsertPoint(initSkipBlock);
  1726. }
  1727. BfIRValue retValue;
  1728. if ((targetType == BfTargetType_BeefConsoleApplication) || (targetType == BfTargetType_BeefWindowsApplication) ||
  1729. (targetType == BfTargetType_BeefApplication_StaticLib) || (targetType == BfTargetType_BeefApplication_DynamicLib))
  1730. {
  1731. bool hadRet = false;
  1732. String entryClassName = project->mStartupObject;
  1733. auto typeDef = mSystem->FindTypeDef(entryClassName, 0, bfModule->mProject, {}, NULL, BfFindTypeDefFlag_AllowGlobal);
  1734. if (typeDef != NULL)
  1735. {
  1736. auto type = bfModule->ResolveTypeDef(typeDef);
  1737. BF_ASSERT((type != NULL) || (mPassInstance->HasFailed()));
  1738. if (type != NULL)
  1739. {
  1740. BfType* stringType = vdataContext->mUnreifiedModule->ResolveTypeDef(mStringTypeDef);
  1741. BfType* int32Type = bfModule->GetPrimitiveType(BfTypeCode_Int32);
  1742. BfType* intType = bfModule->GetPrimitiveType(BfTypeCode_IntPtr);
  1743. BfType* voidType = bfModule->GetPrimitiveType(BfTypeCode_None);
  1744. bool hadValidMainMethod = false;
  1745. BfModuleMethodInstance moduleMethodInst;
  1746. for (auto methodDef : typeDef->mMethods)
  1747. {
  1748. if (methodDef->mName == "Main")
  1749. {
  1750. hadValidMainMethod = true;
  1751. moduleMethodInst = bfModule->GetMethodInstanceAtIdx(type->ToTypeInstance(), methodDef->mIdx);
  1752. if (!methodDef->mIsStatic)
  1753. {
  1754. mPassInstance->Fail("Main method must be static", methodDef->GetRefNode());
  1755. hadValidMainMethod = false;
  1756. }
  1757. if ((moduleMethodInst.mMethodInstance->mReturnType != int32Type) &&
  1758. (moduleMethodInst.mMethodInstance->mReturnType != intType) &&
  1759. (moduleMethodInst.mMethodInstance->mReturnType != voidType))
  1760. {
  1761. mPassInstance->Fail("Main method must return void, int, or int32", methodDef->GetRefNode());
  1762. hadValidMainMethod = false;
  1763. }
  1764. if (moduleMethodInst.mMethodInstance->GetParamCount() == 0)
  1765. {
  1766. // No params
  1767. }
  1768. else
  1769. {
  1770. auto paramType = moduleMethodInst.mMethodInstance->GetParamType(0);
  1771. if ((moduleMethodInst.mMethodInstance->GetParamCount() != 1) || (!paramType->IsArray()) || (paramType->GetUnderlyingType() != stringType))
  1772. {
  1773. mPassInstance->Fail("Main method must be declared with either no parameters or a single String[] parameter", methodDef->GetRefNode());
  1774. hadValidMainMethod = false;
  1775. }
  1776. }
  1777. }
  1778. }
  1779. if (moduleMethodInst)
  1780. {
  1781. if (hadValidMainMethod)
  1782. {
  1783. bool hasArgs = moduleMethodInst.mMethodInstance->GetParamCount() != 0;
  1784. BfIRType intType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_IntPtr));
  1785. BfIRType int32Type = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_Int32));
  1786. // Create BeefEntry thunk
  1787. SmallVector<BfIRType, 1> paramTypes;
  1788. if (hasArgs)
  1789. {
  1790. paramTypes.push_back(bfModule->mBfIRBuilder->MapType(moduleMethodInst.mMethodInstance->GetParamType(0)));
  1791. }
  1792. BfIRFunctionType thunkFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
  1793. BfIRFunction thunkMainFunc = bfModule->mBfIRBuilder->CreateFunction(thunkFuncType, BfIRLinkageType_External, "BeefStartProgram");
  1794. bfModule->SetupIRMethod(NULL, thunkMainFunc, false);
  1795. bfModule->mBfIRBuilder->SetActiveFunction(thunkMainFunc);
  1796. auto thunkEntryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1797. bfModule->mBfIRBuilder->SetInsertPoint(thunkEntryBlock);
  1798. SmallVector<BfIRValue, 1> args;
  1799. if (hasArgs)
  1800. args.push_back(bfModule->mBfIRBuilder->GetArgument(0));
  1801. auto methodInstance = moduleMethodInst.mMethodInstance;
  1802. if (methodInstance->mHotMethod != NULL)
  1803. methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
  1804. auto retVal = bfModule->mBfIRBuilder->CreateCall(moduleMethodInst.mFunc, args);
  1805. if (moduleMethodInst.mMethodInstance->mReturnType->IsVoid())
  1806. {
  1807. bfModule->mBfIRBuilder->CreateRet(bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 0));
  1808. }
  1809. else
  1810. {
  1811. retVal = bfModule->mBfIRBuilder->CreateNumericCast(retVal, true, BfTypeCode_Int32);
  1812. bfModule->mBfIRBuilder->CreateRet(retVal);
  1813. }
  1814. hadRet = true;
  1815. auto internalType = bfModule->ResolveTypeDef(mInternalTypeDef);
  1816. args.clear();
  1817. // Call BeefEntry thunk
  1818. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1819. if (hasArgs)
  1820. {
  1821. auto createParamsMethodInstance = bfModule->GetMethodByName(internalType->ToTypeInstance(), "CreateParamsArray");
  1822. auto callValue = bfModule->mBfIRBuilder->CreateCall(createParamsMethodInstance.mFunc, SmallVector<BfIRValue, 0>());
  1823. args.push_back(callValue);
  1824. }
  1825. retValue = bfModule->mBfIRBuilder->CreateCall(thunkMainFunc, args);
  1826. if (hasArgs)
  1827. {
  1828. auto deleteStringArrayMethodInstance = bfModule->GetMethodByName(internalType->ToTypeInstance(), "DeleteStringArray");
  1829. bfModule->mBfIRBuilder->CreateCall(deleteStringArrayMethodInstance.mFunc, args);
  1830. }
  1831. }
  1832. }
  1833. else
  1834. {
  1835. if (entryClassName.IsEmpty())
  1836. mPassInstance->Fail("Unable to find Main method in global namespace. Consider specifying a Startup Object in the project properties.");
  1837. else
  1838. mPassInstance->Fail(StrFormat("Unable to find Main method in specified Startup Object '%s'", entryClassName.c_str()));
  1839. }
  1840. }
  1841. }
  1842. else
  1843. {
  1844. if (entryClassName.empty())
  1845. mPassInstance->Fail(StrFormat("No entry point class specified for executable in project '%s'", project->mName.c_str()));
  1846. else
  1847. mPassInstance->Fail(StrFormat("Unable to find entry point class '%s' in project '%s'", entryClassName.c_str(), project->mName.c_str()));
  1848. bfModule->mHadBuildError = true;
  1849. }
  1850. if (!hadRet)
  1851. retValue = bfModule->GetConstValue32(0);
  1852. }
  1853. else
  1854. {
  1855. if (mainHasRet)
  1856. retValue = bfModule->GetConstValue32(1);
  1857. }
  1858. if (targetType == BfTargetType_BeefTest)
  1859. EmitTestMethod(bfModule, testMethods, retValue);
  1860. BfIRBlock deinitSkipBlock;
  1861. if (isDllMain)
  1862. {
  1863. auto deinitBlock = bfModule->mBfIRBuilder->CreateBlock("doDeinit", false);
  1864. deinitSkipBlock = bfModule->mBfIRBuilder->CreateBlock("skipDeinit", false);
  1865. auto cmpResult = bfModule->mBfIRBuilder->CreateCmpEQ(bfModule->mBfIRBuilder->GetArgument(1), bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 0));
  1866. bfModule->mBfIRBuilder->CreateCondBr(cmpResult, deinitBlock, deinitSkipBlock);
  1867. bfModule->mBfIRBuilder->AddBlock(deinitBlock);
  1868. bfModule->mBfIRBuilder->SetInsertPoint(deinitBlock);
  1869. }
  1870. if (mOptions.mPlatformType != BfPlatformType_Wasm)
  1871. {
  1872. auto prevBlock = bfModule->mBfIRBuilder->GetInsertBlock();
  1873. if (shutdownFunc)
  1874. {
  1875. bfModule->mBfIRBuilder->SetActiveFunction(shutdownFunc);
  1876. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1877. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1878. }
  1879. bfModule->mBfIRBuilder->CreateCall(dtorFunc, SizedArray<BfIRValue, 0>());
  1880. BfModuleMethodInstance shutdownMethod = bfModule->GetInternalMethod("Shutdown");
  1881. if (shutdownMethod)
  1882. {
  1883. bfModule->mBfIRBuilder->CreateCall(shutdownMethod.mFunc, SizedArray<BfIRValue, 0>());
  1884. }
  1885. if (shutdownFunc)
  1886. {
  1887. bfModule->mBfIRBuilder->CreateRetVoid();
  1888. bfModule->mBfIRBuilder->SetActiveFunction(mainFunc);
  1889. bfModule->mBfIRBuilder->SetInsertPoint(prevBlock);
  1890. }
  1891. }
  1892. if (deinitSkipBlock)
  1893. {
  1894. bfModule->mBfIRBuilder->CreateBr(deinitSkipBlock);
  1895. bfModule->mBfIRBuilder->AddBlock(deinitSkipBlock);
  1896. bfModule->mBfIRBuilder->SetInsertPoint(deinitSkipBlock);
  1897. }
  1898. if (retValue)
  1899. bfModule->mBfIRBuilder->CreateRet(retValue);
  1900. else
  1901. bfModule->mBfIRBuilder->CreateRetVoid();
  1902. if ((mOptions.mAllowHotSwapping) && (bfModule->mHasFullDebugInfo))
  1903. {
  1904. auto int8Type = bfModule->GetPrimitiveType(BfTypeCode_Int8);
  1905. int dataSize = 16*1024;
  1906. auto irArrType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->MapType(int8Type), dataSize);
  1907. String name = "__BFTLS_EXTRA";
  1908. auto irVal = bfModule->mBfIRBuilder->CreateGlobalVariable(irArrType, false, BfIRLinkageType_External, bfModule->mBfIRBuilder->CreateConstAggZero(irArrType), name, true);
  1909. BfIRMDNode dbgArrayType = bfModule->mBfIRBuilder->DbgCreateArrayType(dataSize * 8, 8, bfModule->mBfIRBuilder->DbgGetType(int8Type), dataSize);
  1910. bfModule->mBfIRBuilder->DbgCreateGlobalVariable(bfModule->mDICompileUnit, name, name, BfIRMDNode(), 0, dbgArrayType, false, irVal);
  1911. }
  1912. if (isPosixDynLib)
  1913. {
  1914. auto voidType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_None));
  1915. SizedArray<BfIRType, 4> paramTypes;
  1916. BfIRFunctionType funcType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes);
  1917. BfIRFunction func = bfModule->mBfIRBuilder->CreateFunction(funcType, BfIRLinkageType_Internal, "BfDynLib__Startup");
  1918. bfModule->mBfIRBuilder->SetActiveFunction(func);
  1919. bfModule->mBfIRBuilder->Func_AddAttribute(func, -1, BFIRAttribute_Constructor);
  1920. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("main", true);
  1921. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1922. SmallVector<BfIRValue, 2> startArgs;
  1923. bfModule->mBfIRBuilder->CreateCall(mainFunc, startArgs);
  1924. bfModule->mBfIRBuilder->CreateRetVoid();
  1925. //////////////////////////////////////////////////////////////////////////
  1926. func = bfModule->mBfIRBuilder->CreateFunction(funcType, BfIRLinkageType_Internal, "BfDynLib__Shutdown");
  1927. bfModule->mBfIRBuilder->Func_AddAttribute(func, -1, BFIRAttribute_Destructor);
  1928. bfModule->mBfIRBuilder->SetActiveFunction(func);
  1929. entryBlock = bfModule->mBfIRBuilder->CreateBlock("main", true);
  1930. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1931. SmallVector<BfIRValue, 2> stopArgs;
  1932. bfModule->mBfIRBuilder->CreateCall(shutdownFunc, startArgs);
  1933. bfModule->mBfIRBuilder->CreateRetVoid();
  1934. }
  1935. }
  1936. // Generate "System.GC.MarkAllStaticMembers"
  1937. auto gcType = vdataContext->mUnreifiedModule->ResolveTypeDef(mGCTypeDef);
  1938. if (bfModule->IsMethodImplementedAndReified(gcType->ToTypeInstance(), "MarkAllStaticMembers"))
  1939. {
  1940. bfModule->PopulateType(gcType);
  1941. auto moduleMethodInstance = bfModule->GetMethodByName(gcType->ToTypeInstance(), "MarkAllStaticMembers");
  1942. bfModule->mBfIRBuilder->SetActiveFunction(moduleMethodInstance.mFunc);
  1943. if (!moduleMethodInstance)
  1944. {
  1945. bfModule->Fail("Internal error: System.GC doesn't contain MarkAllStaticMembers method");
  1946. }
  1947. else
  1948. {
  1949. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1950. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1951. _SortedTypeEntry::Sort(staticMarkList);
  1952. for (auto& mapEntry : staticMarkList)
  1953. {
  1954. auto typeInst = mapEntry.mTypeInstance;
  1955. if (typeInst->IsUnspecializedType())
  1956. continue;
  1957. for (auto& methodGroup : typeInst->mMethodInstanceGroups)
  1958. {
  1959. auto methodInstance = methodGroup.mDefault;
  1960. if ((methodInstance != NULL) &&
  1961. (methodInstance->mMethodDef->mIsStatic) &&
  1962. (methodInstance->mMethodDef->mMethodType == BfMethodType_Normal) &&
  1963. (methodInstance->mMethodDef->mName == BF_METHODNAME_MARKMEMBERS_STATIC) &&
  1964. ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
  1965. {
  1966. if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
  1967. continue;
  1968. auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
  1969. if (methodInstance->mHotMethod != NULL)
  1970. methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
  1971. bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
  1972. }
  1973. }
  1974. }
  1975. bfModule->mBfIRBuilder->CreateRetVoid();
  1976. }
  1977. }
  1978. // Generate "System.GC.FindAllTLSMembers"
  1979. if (bfModule->IsMethodImplementedAndReified(gcType->ToTypeInstance(), "FindAllTLSMembers"))
  1980. {
  1981. bfModule->PopulateType(gcType);
  1982. auto moduleMethodInstance = bfModule->GetMethodByName(gcType->ToTypeInstance(), "FindAllTLSMembers");
  1983. bfModule->mBfIRBuilder->SetActiveFunction(moduleMethodInstance.mFunc);
  1984. if (!moduleMethodInstance)
  1985. {
  1986. bfModule->Fail("Internal error: System.GC doesn't contain FindAllTLSMembers method");
  1987. }
  1988. else
  1989. {
  1990. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1991. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1992. _SortedTypeEntry::Sort(staticTLSList);
  1993. for (auto& mapEntry : staticTLSList)
  1994. {
  1995. auto typeInst = mapEntry.mTypeInstance;
  1996. if (typeInst->IsUnspecializedType())
  1997. continue;
  1998. for (auto& methodGroup : typeInst->mMethodInstanceGroups)
  1999. {
  2000. auto methodInstance = methodGroup.mDefault;
  2001. if ((methodInstance != NULL) &&
  2002. (methodInstance->mMethodDef->mIsStatic) &&
  2003. (methodInstance->mMethodDef->mMethodType == BfMethodType_Normal) &&
  2004. (methodInstance->mMethodDef->mName == BF_METHODNAME_FIND_TLS_MEMBERS) &&
  2005. ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
  2006. {
  2007. if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
  2008. continue;
  2009. auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
  2010. bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
  2011. }
  2012. }
  2013. }
  2014. bfModule->mBfIRBuilder->CreateRetVoid();
  2015. }
  2016. }
  2017. if (bfModule->mHadBuildError)
  2018. {
  2019. bfModule->mDataHash = 0;
  2020. }
  2021. }
  2022. // This method clears out unused generic types AFTER compilation of reified types has occurred
  2023. void BfCompiler::UpdateDependencyMap(bool deleteUnusued, bool& didWork)
  2024. {
  2025. BP_ZONE("BfCompiler::UpdateDependencyMap");
  2026. BfLogSysM("Compiler::UpdateDependencyMap %d\n", deleteUnusued);
  2027. bool madeFullPass = true;
  2028. if (mCanceling)
  2029. madeFullPass = false;
  2030. if ((mResolvePassData != NULL) && (!mResolvePassData->mParsers.IsEmpty()))
  2031. madeFullPass = false;
  2032. SetAndRestoreValue<bool> prevAssertOnPopulateType(mContext->mAssertOnPopulateType, deleteUnusued && madeFullPass);
  2033. if ((deleteUnusued) && (madeFullPass))
  2034. {
  2035. // Work queues should be empty if we're not canceling
  2036. BF_ASSERT(mContext->mPopulateTypeWorkList.size() == 0);
  2037. BF_ASSERT(mContext->mMethodWorkList.size() == 0);
  2038. }
  2039. // Remove old data in dependency maps, and find types which don't have any references (direct or indirect)
  2040. // to a non-generic type and remove them
  2041. for (int pass = 0; true; pass++)
  2042. {
  2043. // This assert can fail if we have a dependency error, where deleting a type causes a dependent type
  2044. // to be rebuilt
  2045. BF_ASSERT(pass < 100);
  2046. bool foundNew = false;
  2047. for (auto type : mContext->mResolvedTypes)
  2048. {
  2049. if (type != NULL)
  2050. {
  2051. auto depType = type->ToDependedType();
  2052. auto typeInst = type->ToTypeInstance();
  2053. if (depType != NULL)
  2054. {
  2055. extern BfModule* gLastCreatedModule;
  2056. #ifdef _DEBUG
  2057. for (auto itr = depType->mDependencyMap.begin(); itr != depType->mDependencyMap.end(); ++itr)
  2058. {
  2059. auto dependentType = itr->mKey;
  2060. if (dependentType->IsIncomplete())
  2061. {
  2062. BF_ASSERT(dependentType->IsDeleting() || dependentType->IsOnDemand() || !dependentType->HasBeenReferenced() || mCanceling || !madeFullPass || dependentType->IsSpecializedByAutoCompleteMethod());
  2063. }
  2064. }
  2065. #endif
  2066. depType->mDependencyMap.mFlagsUnion = BfDependencyMap::DependencyFlag_None;
  2067. // Not combined with previous loop because PopulateType could modify typeInst->mDependencyMap
  2068. for (auto itr = depType->mDependencyMap.begin(); itr != depType->mDependencyMap.end();)
  2069. {
  2070. auto dependentType = itr->mKey;
  2071. auto depTypeInst = dependentType->ToTypeInstance();
  2072. auto& depData = itr->mValue;
  2073. bool isInvalidVersion = (dependentType->mRevision > depData.mRevision);// && (deleteUnusued) && (madeFullPass);
  2074. //TODO: Just to cause crash if dependentType is deleted
  2075. bool isIncomplete = dependentType->IsIncomplete();
  2076. if ((isInvalidVersion) && (!dependentType->IsDeleting()))
  2077. {
  2078. if (!dependentType->HasBeenReferenced())
  2079. {
  2080. BfLogSysM("Skipping remove of old dependent %p from %p\n", dependentType, typeInst);
  2081. //BF_ASSERT(dependentType->IsGenericTypeInstance());
  2082. // We have a pending type rebuild but we're not sure whether we're being deleted or not yet...
  2083. ++itr;
  2084. continue;
  2085. }
  2086. }
  2087. if ((dependentType->IsDeleting()) || (isInvalidVersion))
  2088. {
  2089. if (dependentType->IsDeleting() || ((deleteUnusued) && (madeFullPass)))
  2090. {
  2091. // If we're deleting the type, OR the dependency of the type has been removed.
  2092. // We detect a removed dependency by the dependent type changing but the dependency revision
  2093. // is older than the dependent type.
  2094. BfLogSysM("Removing old dependent %p from %p\n", dependentType, depType);
  2095. itr = depType->mDependencyMap.erase(itr);
  2096. }
  2097. else
  2098. ++itr;
  2099. }
  2100. else
  2101. {
  2102. // There needs to be more usage than just being used as part of the method specialization's MethodGenericArg.
  2103. // Keep in mind that actually invoking a generic method creates a DependencyFlag_LocalUsage dependency. The
  2104. // DependencyFlag_MethodGenericArg is just used by the owner during creation of the method specialization
  2105. bool isDependentUsage = (depData.mFlags & BfDependencyMap::DependencyFlag_DependentUsageMask) != 0;
  2106. // We need to consider specialized generic types separately, to remove unused specializations
  2107. if (typeInst != NULL)
  2108. {
  2109. bool isDirectReference = (depTypeInst != NULL) && (!depTypeInst->IsOnDemand()) && (!dependentType->IsGenericTypeInstance());
  2110. if ((depTypeInst != NULL) && (typeInst->mLastNonGenericUsedRevision != mRevision) && (isDependentUsage) &&
  2111. ((isDirectReference) || (dependentType->IsUnspecializedType()) || (depTypeInst->mLastNonGenericUsedRevision == mRevision)))
  2112. {
  2113. typeInst->mLastNonGenericUsedRevision = mRevision;
  2114. foundNew = true;
  2115. if (!typeInst->HasBeenReferenced())
  2116. {
  2117. mContext->AddTypeToWorkList(typeInst);
  2118. foundNew = true;
  2119. }
  2120. }
  2121. }
  2122. ++itr;
  2123. }
  2124. depType->mDependencyMap.mFlagsUnion = (BfDependencyMap::DependencyFlags)(depType->mDependencyMap.mFlagsUnion | depData.mFlags);
  2125. }
  2126. if ((!depType->IsGenericTypeInstance() && (!depType->IsBoxed())) ||
  2127. (depType->IsUnspecializedType()) ||
  2128. ((typeInst != NULL) && (typeInst->mLastNonGenericUsedRevision == mRevision)))
  2129. {
  2130. if ((depType->mRebuildFlags & BfTypeRebuildFlag_AwaitingReference) != 0)
  2131. {
  2132. mContext->MarkAsReferenced(depType);
  2133. }
  2134. }
  2135. }
  2136. }
  2137. }
  2138. if (mCanceling)
  2139. madeFullPass = false;
  2140. if (!madeFullPass)
  2141. {
  2142. // We can't delete types based on the dependency map when we're canceling, because we may still
  2143. // have items in the work queues (particularly the mMethodWorkList) that will create
  2144. // new dependencies -- things may unduly be thought to be deleted.
  2145. return;
  2146. }
  2147. if (foundNew)
  2148. {
  2149. // This will work through generic method specializations for the types referenced above, clearing out AwaitingReference flags for
  2150. // newly-referenced generics, and queuing up their method specializations as well
  2151. didWork |= DoWorkLoop(false, false);
  2152. }
  2153. else if (deleteUnusued)
  2154. {
  2155. // Work queues should be empty if we're not canceling
  2156. BF_ASSERT(mContext->mPopulateTypeWorkList.size() == 0);
  2157. BF_ASSERT(mContext->mMethodWorkList.size() == 0);
  2158. // We need to use a delete queue because we trigger a RebuildType for dependent types,
  2159. // but we need to make sure we don't rebuild any types that may be next in line for
  2160. // deletion, so we must set BfTypeRebuildFlag_DeleteQueued first to avoid that
  2161. Array<BfDependedType*> deleteQueue;
  2162. // We bubble out
  2163. for (auto type : mContext->mResolvedTypes)
  2164. {
  2165. auto depType = type->ToDependedType();
  2166. // Delete if we're a generic
  2167. if ((depType != NULL) && (!depType->IsDeleting()))
  2168. {
  2169. auto typeInst = depType->ToTypeInstance();
  2170. bool wantDelete = false;
  2171. if (typeInst != NULL)
  2172. {
  2173. wantDelete = (typeInst->mLastNonGenericUsedRevision != mRevision) &&
  2174. (typeInst->IsGenericTypeInstance() || typeInst->IsBoxed()) && (!typeInst->IsUnspecializedType());
  2175. }
  2176. wantDelete |= (depType->IsOnDemand()) && (depType->mDependencyMap.IsEmpty());
  2177. if (wantDelete)
  2178. {
  2179. deleteQueue.push_back(depType);
  2180. depType->mRebuildFlags = (BfTypeRebuildFlags)(depType->mRebuildFlags | BfTypeRebuildFlag_DeleteQueued);
  2181. foundNew = true;
  2182. }
  2183. }
  2184. }
  2185. for (auto depType : deleteQueue)
  2186. {
  2187. BfLogSysM("Deleting type from deleteQueue in UpdateDependencyMap %p\n", depType);
  2188. mContext->DeleteType(depType, true);
  2189. }
  2190. if (deleteQueue.size() != 0)
  2191. {
  2192. mContext->ValidateDependencies();
  2193. mContext->UpdateAfterDeletingTypes();
  2194. mContext->ValidateDependencies();
  2195. }
  2196. }
  2197. if (!foundNew)
  2198. break;
  2199. }
  2200. #ifdef _DEBUG
  2201. if (deleteUnusued)
  2202. {
  2203. for (auto type : mContext->mResolvedTypes)
  2204. {
  2205. // This flag should be handled by now
  2206. BF_ASSERT((type->mRebuildFlags & BfTypeRebuildFlag_AwaitingReference) == 0);
  2207. }
  2208. }
  2209. #endif
  2210. BP_ZONE("UpdateDependencyMap QueuedSpecializedMethodRebuildTypes");
  2211. HashSet<BfTypeInstance*> specializerSet;
  2212. for (auto rebuildType : mContext->mQueuedSpecializedMethodRebuildTypes)
  2213. {
  2214. if (rebuildType->mRevision != mRevision)
  2215. {
  2216. mContext->RebuildType(rebuildType);
  2217. rebuildType->mRebuildFlags = (BfTypeRebuildFlags)(rebuildType->mRebuildFlags | BfTypeRebuildFlag_SpecializedMethodRebuild);
  2218. for (auto& dep : rebuildType->mDependencyMap)
  2219. {
  2220. auto depType = dep.mKey;
  2221. auto& depData = dep.mValue;
  2222. auto depTypeInst = depType->ToTypeInstance();
  2223. if (depTypeInst == NULL)
  2224. continue;
  2225. if ((depData.mFlags & BfDependencyMap::DependencyFlag_Calls) != 0)
  2226. {
  2227. specializerSet.Add(depTypeInst);
  2228. }
  2229. }
  2230. }
  2231. }
  2232. for (auto depType : specializerSet)
  2233. {
  2234. mContext->QueueMethodSpecializations(depType, true);
  2235. }
  2236. for (auto rebuildType : mContext->mQueuedSpecializedMethodRebuildTypes)
  2237. {
  2238. rebuildType->mRebuildFlags = (BfTypeRebuildFlags)(rebuildType->mRebuildFlags & ~BfTypeRebuildFlag_SpecializedMethodRebuild);
  2239. }
  2240. mContext->mQueuedSpecializedMethodRebuildTypes.Clear();
  2241. mDepsMayHaveDeletedTypes = false;
  2242. }
  2243. void BfCompiler::SanitizeDependencyMap()
  2244. {
  2245. BfLogSysM("SanitizeDependencyMap\n");
  2246. for (auto type : mContext->mResolvedTypes)
  2247. {
  2248. if (type == NULL)
  2249. continue;
  2250. auto depType = type->ToDependedType();
  2251. if (depType == NULL)
  2252. continue;
  2253. // Not combined with previous loop because PopulateType could modify typeInst->mDependencyMap
  2254. for (auto itr = depType->mDependencyMap.begin(); itr != depType->mDependencyMap.end();)
  2255. {
  2256. auto dependentType = itr->mKey;
  2257. auto depTypeInst = dependentType->ToTypeInstance();
  2258. if (dependentType->IsDeleting())
  2259. {
  2260. BfLogSysM("SanitizeDependencyMap removing old dependent %p from %p\n", dependentType, depType);
  2261. itr = depType->mDependencyMap.erase(itr);
  2262. }
  2263. else
  2264. ++itr;
  2265. }
  2266. }
  2267. mContext->RemoveInvalidWorkItems();
  2268. mDepsMayHaveDeletedTypes = false;
  2269. }
  2270. // When we are unsure of whether an old generic instance will survive, we RebuildType but don't put it in any worklist.
  2271. // One of three things happens:
  2272. // 1) It gets built on demand
  2273. // 2) It gets deleted in UpdateDependencyMap
  2274. // 3) It stays undefined and we need to build it here
  2275. bool BfCompiler::ProcessPurgatory(bool reifiedOnly)
  2276. {
  2277. BP_ZONE("BfCompiler::ProcessPurgatory");
  2278. bool didWork = false;
  2279. while (true)
  2280. {
  2281. mContext->RemoveInvalidWorkItems();
  2282. //for (auto type : mGenericInstancePurgatory)
  2283. for (int i = 0; i < (int)mGenericInstancePurgatory.size(); i++)
  2284. {
  2285. auto type = mGenericInstancePurgatory[i];
  2286. if ((reifiedOnly) && (!type->IsReified()))
  2287. continue;
  2288. if ((reifiedOnly) && ((type->mRebuildFlags & BfTypeRebuildFlag_AwaitingReference) != 0))
  2289. continue;
  2290. if (!type->IsDeleting())
  2291. {
  2292. auto module = type->GetModule();
  2293. if (module != NULL)
  2294. {
  2295. if (!module->mIsModuleMutable)
  2296. module->StartExtension();
  2297. module->PopulateType(type, BfPopulateType_Full);
  2298. }
  2299. }
  2300. if (reifiedOnly)
  2301. {
  2302. mGenericInstancePurgatory.RemoveAtFast(i);
  2303. i--;
  2304. }
  2305. }
  2306. if (!reifiedOnly)
  2307. mGenericInstancePurgatory.Clear();
  2308. int prevPurgatorySize = (int)mGenericInstancePurgatory.size();
  2309. if (mContext->ProcessWorkList(reifiedOnly, reifiedOnly))
  2310. didWork = true;
  2311. if (prevPurgatorySize == (int)mGenericInstancePurgatory.size())
  2312. break;
  2313. }
  2314. return didWork;
  2315. }
  2316. bool BfCompiler::VerifySlotNums()
  2317. {
  2318. BP_ZONE("BfCompiler::VerifySlotNums");
  2319. SmallVector<BfTypeInstance*, 16> isSlotUsed;
  2320. for (auto type : mContext->mResolvedTypes)
  2321. {
  2322. if (!type->IsReified())
  2323. continue;
  2324. auto typeInst = type->ToTypeInstance();
  2325. if (typeInst == NULL)
  2326. continue;
  2327. if (typeInst->IsUnspecializedType())
  2328. continue;
  2329. if (typeInst->IsInterface())
  2330. {
  2331. if (typeInst->mSlotNum == -2)
  2332. continue; // Not used
  2333. if ((typeInst->mVirtualMethodTableSize > 0) && (typeInst->mSlotNum == -1))
  2334. {
  2335. // Slot not assigned yet
  2336. return false;
  2337. }
  2338. continue;
  2339. }
  2340. isSlotUsed.clear();
  2341. isSlotUsed.resize(mMaxInterfaceSlots);
  2342. auto checkType = typeInst;
  2343. while (checkType != NULL)
  2344. {
  2345. for (auto iface : checkType->mInterfaces)
  2346. {
  2347. int slotNum = iface.mInterfaceType->mSlotNum;
  2348. if (slotNum >= 0)
  2349. {
  2350. if ((isSlotUsed[slotNum] != NULL) && (isSlotUsed[slotNum] != iface.mInterfaceType))
  2351. return false; // Collision
  2352. isSlotUsed[slotNum] = iface.mInterfaceType;
  2353. }
  2354. }
  2355. checkType = checkType->mBaseType;
  2356. }
  2357. }
  2358. return true;
  2359. }
  2360. bool BfCompiler::QuickGenerateSlotNums()
  2361. {
  2362. /*SmallVector<bool, 16> isSlotUsed;
  2363. for (auto globalTypeEntry : mResolvedTypes)
  2364. {
  2365. BfType* type = globalTypeEntry->mType;
  2366. auto typeInst = type->ToTypeInstance();
  2367. if (typeInst == NULL)
  2368. continue;
  2369. if (typeInst->IsInterface())
  2370. {
  2371. if ((typeInst->mVirtualMethodTableSize > 0) && (typeInst->mSlotNum == -1))
  2372. {
  2373. // Slot not assigned yet
  2374. return false;
  2375. }
  2376. continue;
  2377. }
  2378. }
  2379. return VerifySlotNums();*/
  2380. // Implement later
  2381. return false;
  2382. }
  2383. class BfSlotEntry
  2384. {
  2385. public:
  2386. BfTypeInstance* mTypeInstance;
  2387. int mRefCount;
  2388. Array<BfTypeInstance*> mConcurrentRefs;
  2389. };
  2390. typedef std::pair<BfTypeInstance*, BfTypeInstance*> InterfacePair;
  2391. typedef Dictionary<BfTypeInstance*, BfSlotEntry*> SlotEntryMap;
  2392. static BfSlotEntry* GetSlotEntry(SlotEntryMap& slotEntryMap, BfTypeInstance* typeInst)
  2393. {
  2394. BF_ASSERT(typeInst->IsReified());
  2395. BfSlotEntry** slotEntryPtr = NULL;
  2396. if (!slotEntryMap.TryAdd(typeInst, NULL, &slotEntryPtr))
  2397. return *slotEntryPtr;
  2398. BfSlotEntry* slotEntry = new BfSlotEntry();
  2399. slotEntry->mTypeInstance = typeInst;
  2400. slotEntry->mRefCount = 0;
  2401. //insertPair.first->second = slotEntry;
  2402. *slotEntryPtr = slotEntry;
  2403. return slotEntry;
  2404. }
  2405. static InterfacePair MakeInterfacePair(BfTypeInstance* iface1, BfTypeInstance* iface2)
  2406. {
  2407. if (iface1->mTypeId < iface2->mTypeId)
  2408. return InterfacePair(iface1, iface2);
  2409. return InterfacePair(iface2, iface1);
  2410. }
  2411. struct InterfacePairHash
  2412. {
  2413. size_t operator()(const InterfacePair& val) const
  2414. {
  2415. return (((size_t)val.first) >> 2) ^ ((size_t)val.second);
  2416. }
  2417. };
  2418. bool BfCompiler::SlowGenerateSlotNums()
  2419. {
  2420. BP_ZONE("BfCompiler::SlowGenerateSlotNums");
  2421. SlotEntryMap ifaceUseMap;
  2422. std::unordered_set<InterfacePair, InterfacePairHash> concurrentInterfaceSet;
  2423. HashSet<BfTypeInstance*> foundIFaces;
  2424. if (mMaxInterfaceSlots < 0)
  2425. {
  2426. mMaxInterfaceSlots = 0;
  2427. }
  2428. bool isHotCompile = IsHotCompile();
  2429. for (auto type : mContext->mResolvedTypes)
  2430. {
  2431. if (!type->IsReified())
  2432. continue;
  2433. auto typeInst = type->ToTypeInstance();
  2434. if (typeInst == NULL)
  2435. continue;
  2436. if (typeInst->IsUnspecializedType())
  2437. continue;
  2438. if (typeInst->IsInterface())
  2439. {
  2440. if (typeInst->mSlotNum == -2) // Not needed
  2441. continue;
  2442. if (!isHotCompile) // Hot compiles cannot remap slot numbers
  2443. typeInst->mSlotNum = -1;
  2444. if (typeInst->mVirtualMethodTableSize > 0)
  2445. {
  2446. GetSlotEntry(ifaceUseMap, typeInst);
  2447. }
  2448. continue;
  2449. }
  2450. foundIFaces.Clear();
  2451. auto checkTypeInst = typeInst;
  2452. while (checkTypeInst != NULL)
  2453. {
  2454. for (auto iface : checkTypeInst->mInterfaces)
  2455. {
  2456. auto interfaceType = iface.mInterfaceType;
  2457. if (interfaceType->mSlotNum == -2)
  2458. continue; // Not needed
  2459. if ((isHotCompile) && (interfaceType->mSlotNum == -1))
  2460. checkTypeInst->mDirty = true; // We're about to slot an interface here
  2461. if (interfaceType->mVirtualMethodTableSize > 0)
  2462. {
  2463. BfSlotEntry* slotEntry = GetSlotEntry(ifaceUseMap, interfaceType);
  2464. slotEntry->mRefCount++;
  2465. foundIFaces.Add(iface.mInterfaceType);
  2466. }
  2467. }
  2468. checkTypeInst = checkTypeInst->mBaseType;
  2469. }
  2470. for (auto itr1 = foundIFaces.begin(); itr1 != foundIFaces.end(); ++itr1)
  2471. {
  2472. auto itr2 = itr1;
  2473. ++itr2;
  2474. for ( ; itr2 != foundIFaces.end(); ++itr2)
  2475. {
  2476. auto iface1 = *itr1;
  2477. auto iface2 = *itr2;
  2478. InterfacePair ifacePair = MakeInterfacePair(iface1, iface2);
  2479. if (concurrentInterfaceSet.insert(ifacePair).second)
  2480. {
  2481. BfSlotEntry* entry1 = GetSlotEntry(ifaceUseMap, iface1);
  2482. BfSlotEntry* entry2 = GetSlotEntry(ifaceUseMap, iface2);
  2483. entry1->mConcurrentRefs.push_back(iface2);
  2484. entry2->mConcurrentRefs.push_back(iface1);
  2485. }
  2486. }
  2487. }
  2488. }
  2489. Array<BfSlotEntry*> sortedIfaceUseMap;
  2490. for (auto& entry : ifaceUseMap)
  2491. {
  2492. if (!isHotCompile)
  2493. BF_ASSERT(entry.mValue->mTypeInstance->mSlotNum == -1);
  2494. sortedIfaceUseMap.push_back(entry.mValue);
  2495. }
  2496. std::sort(sortedIfaceUseMap.begin(), sortedIfaceUseMap.end(), [] (BfSlotEntry* lhs, BfSlotEntry* rhs)
  2497. {
  2498. if (lhs->mRefCount != rhs->mRefCount)
  2499. return lhs->mRefCount > rhs->mRefCount;
  2500. return lhs->mTypeInstance->mTypeId < rhs->mTypeInstance->mTypeId;
  2501. });
  2502. bool failed = false;
  2503. SmallVector<bool, 16> isSlotUsed;
  2504. for (auto slotEntry : sortedIfaceUseMap)
  2505. {
  2506. BfTypeInstance* iface = slotEntry->mTypeInstance;
  2507. if (iface->mSlotNum >= 0)
  2508. {
  2509. BF_ASSERT(isHotCompile);
  2510. continue;
  2511. }
  2512. isSlotUsed.clear();
  2513. if (mMaxInterfaceSlots > 0)
  2514. isSlotUsed.resize(mMaxInterfaceSlots);
  2515. BF_ASSERT(iface->mSlotNum == -1);
  2516. BF_ASSERT(iface->IsInterface());
  2517. for (auto iface2 : slotEntry->mConcurrentRefs)
  2518. {
  2519. int slotNum2 = iface2->mSlotNum;
  2520. if (slotNum2 != -1)
  2521. isSlotUsed[slotNum2] = true;
  2522. }
  2523. for (int checkSlot = 0; checkSlot < mMaxInterfaceSlots; checkSlot++)
  2524. {
  2525. if (!isSlotUsed[checkSlot])
  2526. {
  2527. iface->mSlotNum = checkSlot;
  2528. break;
  2529. }
  2530. }
  2531. if (iface->mSlotNum == -1)
  2532. {
  2533. if (isHotCompile)
  2534. {
  2535. failed = true;
  2536. mPassInstance->Fail("Interface slot numbering overflow. Restart the program or revert changes.");
  2537. break;
  2538. }
  2539. iface->mSlotNum = mMaxInterfaceSlots;
  2540. if (mOptions.mIncrementalBuild)
  2541. {
  2542. // Allocate more than enough interface slots
  2543. mMaxInterfaceSlots += 3;
  2544. }
  2545. else
  2546. mMaxInterfaceSlots++;
  2547. // failed = true;
  2548. // mPassInstance->Fail(StrFormat("Interface slot numbering overflow, increase the maximum slot number from '%d'", mMaxInterfaceSlots));
  2549. // break;
  2550. }
  2551. // if (iface->mSlotNum == -1)
  2552. // {
  2553. // failed = true;
  2554. // mPassInstance->Fail(StrFormat("Interface slot numbering overflow, increase the maximum slot number from '%d'", mMaxInterfaceSlots));
  2555. // break;
  2556. // }
  2557. if (isHotCompile)
  2558. {
  2559. mHotState->mNewlySlottedTypeIds.Add(iface->mTypeId);
  2560. mHotState->mSlotDefineTypeIds.Add(iface->mTypeId);
  2561. }
  2562. }
  2563. if (!failed)
  2564. {
  2565. bool success = VerifySlotNums();
  2566. if ((!success) && (!isHotCompile))
  2567. {
  2568. BF_DBG_FATAL("Failed!");
  2569. }
  2570. }
  2571. for (auto& entry : ifaceUseMap)
  2572. delete entry.mValue;
  2573. return true;
  2574. }
  2575. void BfCompiler::GenerateSlotNums()
  2576. {
  2577. BP_ZONE("BfCompiler::GenerateSlotNums");
  2578. if (mMaxInterfaceSlots < 0)
  2579. {
  2580. if (mOptions.mIncrementalBuild)
  2581. mMaxInterfaceSlots = 3;
  2582. else
  2583. mMaxInterfaceSlots = 0;
  2584. }
  2585. bool isHotCompile = IsHotCompile();
  2586. for (auto type : mContext->mResolvedTypes)
  2587. {
  2588. if (!type->IsInterface())
  2589. continue;
  2590. auto typeInstance = type->ToTypeInstance();
  2591. if ((typeInstance->mSlotNum <= 0) || (!isHotCompile))
  2592. {
  2593. if ((mContext->mReferencedIFaceSlots.Contains(typeInstance)) ||
  2594. (typeInstance->mHasBeenInstantiated) || (typeInstance->IncludeAllMethods()))
  2595. {
  2596. if (typeInstance->mSlotNum == -2)
  2597. typeInstance->mSlotNum = -1;
  2598. }
  2599. else
  2600. typeInstance->mSlotNum = -2; // Not needed
  2601. }
  2602. }
  2603. if (VerifySlotNums())
  2604. return;
  2605. if (!QuickGenerateSlotNums())
  2606. SlowGenerateSlotNums();
  2607. BfLogSysM("GenerateSlotNums mMaxInterfaceSlots: %d\n", mMaxInterfaceSlots);
  2608. }
  2609. void BfCompiler::GenerateDynCastData()
  2610. {
  2611. BP_ZONE("BfCompiler::GenerateDynCastData");
  2612. Array<int> firstDerivedIds;
  2613. Array<int> nextSiblingIds;
  2614. firstDerivedIds.Resize(mCurTypeId);
  2615. nextSiblingIds.Resize(mCurTypeId);
  2616. for (auto type : mContext->mResolvedTypes)
  2617. {
  2618. if (type->IsBoxed())
  2619. continue;
  2620. auto typeInst = type->ToTypeInstance();
  2621. if (typeInst == NULL)
  2622. continue;
  2623. if (typeInst->mBaseType == NULL)
  2624. continue;
  2625. int baseId = typeInst->mBaseType->mTypeId;
  2626. int firstDerivedId = firstDerivedIds[baseId];
  2627. nextSiblingIds[typeInst->mTypeId] = firstDerivedIds[baseId];
  2628. firstDerivedIds[baseId] = typeInst->mTypeId;
  2629. }
  2630. int curInheritanceId = 1;
  2631. std::function<void(BfTypeInstance*)> _AddTypeInfo = [&](BfTypeInstance* typeInst)
  2632. {
  2633. if (typeInst->mInheritanceId != curInheritanceId)
  2634. {
  2635. typeInst->mInheritanceId = curInheritanceId;
  2636. typeInst->mDirty = true;
  2637. }
  2638. curInheritanceId++;
  2639. int childId = firstDerivedIds[typeInst->mTypeId];
  2640. while (childId != 0)
  2641. {
  2642. auto childType = mContext->mTypes[childId]->ToTypeInstance();
  2643. _AddTypeInfo(childType);
  2644. childId = nextSiblingIds[childId];
  2645. }
  2646. int inheritanceCount = curInheritanceId - typeInst->mInheritanceId - 1;
  2647. if (typeInst->mInheritanceCount != inheritanceCount)
  2648. {
  2649. typeInst->mInheritanceCount = inheritanceCount;
  2650. typeInst->mDirty = true;
  2651. }
  2652. };
  2653. _AddTypeInfo(mContext->mBfObjectType);
  2654. auto valueTypeInst = mContext->mScratchModule->ResolveTypeDef(mValueTypeTypeDef)->ToTypeInstance();
  2655. _AddTypeInfo(valueTypeInst);
  2656. }
  2657. void BfCompiler::UpdateRevisedTypes()
  2658. {
  2659. BfLogSysM("BfCompiler::UpdateRevisedTypes\n");
  2660. BP_ZONE("BfCompiler::UpdateRevisedTypes");
  2661. // See if we have any name conflicts and remove those
  2662. auto typeDefItr = mSystem->mTypeDefs.begin();
  2663. while (typeDefItr != mSystem->mTypeDefs.end())
  2664. {
  2665. auto typeDef = *typeDefItr;
  2666. auto origTypeDef = typeDef;
  2667. if (typeDef->mNextRevision != NULL)
  2668. typeDef = typeDef->mNextRevision;
  2669. if (typeDef->mDupDetectedRevision == mRevision)
  2670. {
  2671. ++typeDefItr;
  2672. continue;
  2673. }
  2674. typeDef->mDupDetectedRevision = -1;
  2675. if ((typeDef->mIsCombinedPartial) || (typeDef->mDefState == BfTypeDef::DefState_Deleted) || (typeDef->mTypeCode == BfTypeCode_Extension))
  2676. {
  2677. ++typeDefItr;
  2678. continue;
  2679. }
  2680. if ((!typeDef->IsGlobalsContainer()) && (mSystem->ContainsNamespace(typeDef->mFullName, typeDef->mProject)))
  2681. {
  2682. mPassInstance->Fail(StrFormat("The name '%s' is already defined to be a namespace name", typeDef->mFullName.ToString().c_str()), typeDef->mTypeDeclaration->mNameNode);
  2683. }
  2684. bool removedElement = false;
  2685. auto nextTypeDefItr = typeDefItr;
  2686. nextTypeDefItr.MoveToNextHashMatch();
  2687. while (nextTypeDefItr)
  2688. {
  2689. auto nextTypeDef = *nextTypeDefItr;
  2690. if (nextTypeDef->mNextRevision != NULL)
  2691. nextTypeDef = nextTypeDef->mNextRevision;
  2692. if ((nextTypeDef->mIsCombinedPartial) || (nextTypeDef->mDefState == BfTypeDef::DefState_Deleted) || (nextTypeDef->mTypeCode == BfTypeCode_Extension) ||
  2693. (typeDef->mFullNameEx != nextTypeDef->mFullNameEx) || (typeDef->mGenericParamDefs.size() != nextTypeDef->mGenericParamDefs.size()))
  2694. {
  2695. nextTypeDefItr.MoveToNextHashMatch();
  2696. continue;
  2697. }
  2698. if ((typeDef->mIsPartial) && (nextTypeDef->mIsPartial) &&
  2699. (!typeDef->IsGlobalsContainer()) &&
  2700. (typeDef->mProject != nextTypeDef->mProject))
  2701. {
  2702. BfTypeDef* typeA = NULL;
  2703. BfTypeDef* typeB = NULL;
  2704. BfError* error = NULL;
  2705. if (typeDef->mProject->ReferencesOrReferencedBy(nextTypeDef->mProject))
  2706. {
  2707. typeA = typeDef;
  2708. typeB = nextTypeDef;
  2709. }
  2710. else if (nextTypeDef->mProject->ReferencesOrReferencedBy(typeDef->mProject))
  2711. {
  2712. typeA = nextTypeDef;
  2713. typeB = typeDef;
  2714. }
  2715. if (typeA != NULL)
  2716. {
  2717. error = mPassInstance->Fail(StrFormat("Partial type in project '%s' cannot extend a type from a referenced project", typeA->mProject->mName.c_str()).c_str(),
  2718. typeA->mTypeDeclaration->mNameNode);
  2719. mPassInstance->MoreInfo(StrFormat("Previous definition in project '%s'", typeB->mProject->mName.c_str()),
  2720. typeB->mTypeDeclaration->mNameNode);
  2721. }
  2722. if (error != NULL)
  2723. error->mIsPersistent = true;
  2724. }
  2725. if (((!typeDef->mIsPartial) || (!nextTypeDef->mIsPartial)) &&
  2726. (!typeDef->IsGlobalsContainer()) && (!nextTypeDef->IsGlobalsContainer()) &&
  2727. (typeDef->mProject->ReferencesOrReferencedBy(nextTypeDef->mProject)))
  2728. {
  2729. nextTypeDef->mDupDetectedRevision = mRevision;
  2730. BfError* error = NULL;
  2731. /*if ((typeDef->mIsPartial) && (typeDef->mTypeCode != BfTypeCode_Extension))
  2732. {
  2733. error = mPassInstance->Fail("Missing 'partial' modifier; another partial definition of this type exists", nextTypeDef->mTypeDeclaration->mNameNode);
  2734. mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
  2735. }
  2736. else if ((nextTypeDef->mIsPartial) && (nextTypeDef->mTypeCode != BfTypeCode_Extension))
  2737. {
  2738. error = mPassInstance->Fail("Missing 'partial' modifier; another partial definition of this type exists", typeDef->mTypeDeclaration->mNameNode);
  2739. mPassInstance->MoreInfo("Previous definition", nextTypeDef->mTypeDeclaration->mNameNode);
  2740. }
  2741. else */if (nextTypeDef->mOuterType != NULL)
  2742. {
  2743. error = mPassInstance->Fail(StrFormat("The type '%s.%s' already has a definition for '%s'", nextTypeDef->mOuterType->mNamespace.ToString().c_str(), nextTypeDef->mOuterType->mName->mString.mPtr,
  2744. nextTypeDef->mName->mString.mPtr), nextTypeDef->mTypeDeclaration->mNameNode);
  2745. mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
  2746. }
  2747. else if (!nextTypeDef->mNamespace.IsEmpty())
  2748. {
  2749. error = mPassInstance->Fail(StrFormat("The namespace '%s' already has a definition for '%s'", nextTypeDef->mNamespace.ToString().c_str(),
  2750. nextTypeDef->mName->mString.mPtr), nextTypeDef->mTypeDeclaration->mNameNode);
  2751. mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
  2752. }
  2753. else
  2754. {
  2755. error = mPassInstance->Fail(StrFormat("The global namespace already has a definition for '%s'",
  2756. nextTypeDef->mName->mString.mPtr), nextTypeDef->mTypeDeclaration->mNameNode);
  2757. mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
  2758. }
  2759. if (error != NULL)
  2760. error->mIsPersistent = true;
  2761. }
  2762. nextTypeDefItr.MoveToNextHashMatch();
  2763. }
  2764. ++typeDefItr;
  2765. }
  2766. mContext->PreUpdateRevisedTypes();
  2767. // If we missed out on required types previously, now we should be 'okay'
  2768. mInInvalidState = false;
  2769. // We can't do any yields in here - the compiler state is invalid from the time we inject a new
  2770. // typedef revision up until we finish the associated RebuildType
  2771. int compositeBucket = 0;
  2772. // These are "extension" defs that were unmatched last run through
  2773. Array<BfTypeDef*> prevSoloExtensions;
  2774. mSystem->mTypeDefs.CheckRehash();
  2775. Array<BfProject*> corlibProjects;
  2776. //
  2777. {
  2778. BfAtomComposite objectName;
  2779. if (mSystem->ParseAtomComposite("System.Object", objectName))
  2780. {
  2781. for (auto itr = mSystem->mTypeDefs.TryGet(objectName); itr != mSystem->mTypeDefs.end(); ++itr)
  2782. {
  2783. BfTypeDef* typeDef = *itr;
  2784. if ((typeDef->mFullName == objectName) && (typeDef->mTypeCode == BfTypeCode_Object))
  2785. corlibProjects.Add(typeDef->mProject);
  2786. }
  2787. }
  2788. }
  2789. // Process the typedefs one bucket at a time. When we are combining extensions or partials (globals) into a single definition then
  2790. // we will be making multiple passes over the bucket that contains that name
  2791. for (int bucketIdx = 0; bucketIdx < mSystem->mTypeDefs.mHashSize; bucketIdx++)
  2792. {
  2793. bool hadPartials = false;
  2794. bool hadChanges = false;
  2795. if (mSystem->mTypeDefs.mHashHeads == NULL)
  2796. break;
  2797. // Partials combiner
  2798. auto outerTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  2799. while (outerTypeDefEntryIdx != -1)
  2800. {
  2801. // Make sure we can fit a composite without reallocating
  2802. mSystem->mTypeDefs.EnsureFreeCount(1);
  2803. auto outerTypeDefEntry = &mSystem->mTypeDefs.mEntries[outerTypeDefEntryIdx];
  2804. auto outerTypeDef = outerTypeDefEntry->mValue;
  2805. if (outerTypeDef->mDefState == BfTypeDef::DefState_Deleted)
  2806. {
  2807. hadChanges = true;
  2808. outerTypeDefEntryIdx = mSystem->mTypeDefs.mEntries[outerTypeDefEntryIdx].mNext;
  2809. continue;
  2810. }
  2811. if (outerTypeDef->mNextRevision != NULL)
  2812. hadChanges = true;
  2813. BfTypeDefMap::Entry* rootTypeDefEntry = NULL;
  2814. BfTypeDef* rootTypeDef = NULL;
  2815. BfTypeDef* compositeTypeDef = NULL;
  2816. BfProject* compositeProject = NULL;
  2817. auto latestOuterTypeDef = outerTypeDef->GetLatest();
  2818. if ((outerTypeDef->mTypeCode == BfTypeCode_Extension) && (!outerTypeDef->mIsPartial))
  2819. {
  2820. prevSoloExtensions.Add(outerTypeDef);
  2821. outerTypeDef->mIsPartial = true;
  2822. }
  2823. if ((outerTypeDef->mIsPartial) || (outerTypeDef->mIsCombinedPartial))
  2824. {
  2825. // Initialize mPartialUsed flags
  2826. if (!hadPartials)
  2827. {
  2828. auto checkTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  2829. while (checkTypeDefEntryIdx != -1)
  2830. {
  2831. // This clears the mPartialUsed flag for the whole bucket
  2832. auto checkTypeDef = mSystem->mTypeDefs.mEntries[checkTypeDefEntryIdx].mValue;
  2833. if ((checkTypeDef->mIsPartial) || (checkTypeDef->mIsCombinedPartial))
  2834. checkTypeDef->mPartialUsed = false;
  2835. checkTypeDefEntryIdx = mSystem->mTypeDefs.mEntries[checkTypeDefEntryIdx].mNext;
  2836. }
  2837. hadPartials = true;
  2838. }
  2839. }
  2840. bool isExplicitPartial = outerTypeDef->mIsExplicitPartial;
  2841. bool failedToFindRootType = false;
  2842. if ((outerTypeDef->mTypeCode == BfTypeCode_Extension) && (!outerTypeDef->mPartialUsed))
  2843. {
  2844. // Find root type, and we assume the composite type follows this
  2845. auto checkTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  2846. while (checkTypeDefEntryIdx != -1)
  2847. {
  2848. auto checkTypeDefEntry = &mSystem->mTypeDefs.mEntries[checkTypeDefEntryIdx];
  2849. auto checkTypeDef = checkTypeDefEntry->mValue;
  2850. if ((checkTypeDefEntry->mHashCode != outerTypeDefEntry->mHashCode) ||
  2851. (checkTypeDef->mIsCombinedPartial) ||
  2852. (checkTypeDef->mTypeCode == BfTypeCode_Extension) ||
  2853. (checkTypeDef->mDefState == BfTypeDef::DefState_Deleted) ||
  2854. (checkTypeDef->mPartialUsed) ||
  2855. (!checkTypeDef->NameEquals(outerTypeDef)) ||
  2856. (checkTypeDef->mGenericParamDefs.size() != outerTypeDef->mGenericParamDefs.size()) ||
  2857. (!outerTypeDef->mProject->ContainsReference(checkTypeDef->mProject)))
  2858. {
  2859. checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
  2860. continue;
  2861. }
  2862. // Only allow extending structs, objects, and interfaces
  2863. if ((checkTypeDef->mTypeCode == BfTypeCode_Struct) ||
  2864. (checkTypeDef->mTypeCode == BfTypeCode_Object) ||
  2865. (checkTypeDef->mTypeCode == BfTypeCode_Enum) ||
  2866. (checkTypeDef->mTypeCode == BfTypeCode_Interface))
  2867. {
  2868. rootTypeDef = checkTypeDef;
  2869. rootTypeDefEntry = checkTypeDefEntry;
  2870. compositeProject = rootTypeDef->mProject;
  2871. }
  2872. checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
  2873. if (compositeTypeDef != NULL)
  2874. {
  2875. BF_ASSERT(rootTypeDef->mFullNameEx == compositeTypeDef->mFullNameEx);
  2876. }
  2877. }
  2878. if (rootTypeDef == NULL)
  2879. {
  2880. failedToFindRootType = true;
  2881. isExplicitPartial = true;
  2882. }
  2883. }
  2884. if ((isExplicitPartial) && (!outerTypeDef->mPartialUsed))
  2885. {
  2886. // For explicit partials there is no 'root type' so we want to select any partial in the 'innermost' project
  2887. rootTypeDef = outerTypeDef;
  2888. rootTypeDefEntry = outerTypeDefEntry;
  2889. compositeProject = rootTypeDef->mProject;
  2890. // Find composite type, there is no explicit position for this
  2891. auto checkTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  2892. while (checkTypeDefEntryIdx != -1)
  2893. {
  2894. auto checkTypeDefEntry = &mSystem->mTypeDefs.mEntries[checkTypeDefEntryIdx];
  2895. auto checkTypeDef = checkTypeDefEntry->mValue;
  2896. if ((checkTypeDefEntry->mHashCode != outerTypeDefEntry->mHashCode) ||
  2897. (checkTypeDef->mPartialUsed) ||
  2898. (checkTypeDef->mDefState == BfTypeDef::DefState_Deleted) ||
  2899. (!checkTypeDef->NameEquals(outerTypeDef)) ||
  2900. (checkTypeDef->mGenericParamDefs.size() != outerTypeDef->mGenericParamDefs.size()))
  2901. {
  2902. checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
  2903. continue;
  2904. }
  2905. if (!checkTypeDef->mIsCombinedPartial)
  2906. {
  2907. // Select the innermost project for the composite project def
  2908. if (compositeProject != checkTypeDef->mProject)
  2909. {
  2910. if (checkTypeDef->mProject->ContainsReference(compositeProject))
  2911. {
  2912. // Fine, already contains it
  2913. }
  2914. else if (compositeProject->ContainsReference(checkTypeDef->mProject))
  2915. {
  2916. rootTypeDef = checkTypeDef;
  2917. rootTypeDefEntry = checkTypeDefEntry;
  2918. compositeProject = rootTypeDef->mProject;
  2919. }
  2920. else
  2921. {
  2922. // Try 'corlib'
  2923. for (auto corlibProject : corlibProjects)
  2924. {
  2925. if ((rootTypeDef->mProject->ContainsReference(corlibProject)) &&
  2926. (checkTypeDef->mProject->ContainsReference(corlibProject)))
  2927. compositeProject = corlibProject;
  2928. }
  2929. }
  2930. }
  2931. checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
  2932. continue;
  2933. }
  2934. if (!rootTypeDef->mProject->ContainsReference(checkTypeDef->mProject))
  2935. {
  2936. checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
  2937. continue;
  2938. }
  2939. compositeTypeDef = checkTypeDef;
  2940. if (rootTypeDef != NULL)
  2941. {
  2942. BF_ASSERT(rootTypeDef->mFullNameEx == compositeTypeDef->mFullNameEx);
  2943. }
  2944. if (compositeTypeDef->mNextRevision != NULL)
  2945. {
  2946. // This is an old 'next revision'
  2947. delete compositeTypeDef->mNextRevision;
  2948. compositeTypeDef->mNextRevision = NULL;
  2949. if (compositeTypeDef->mDefState != BfTypeDef::DefState_Deleted)
  2950. compositeTypeDef->mDefState = BfTypeDef::DefState_Defined;
  2951. }
  2952. checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
  2953. }
  2954. }
  2955. // Now find extensions to apply to the rootTypeDef
  2956. if (rootTypeDef != NULL)
  2957. {
  2958. bool partialsHadChanges = false;
  2959. bool hadSignatureChange = false;
  2960. bool compositeIsNew = false;
  2961. if (compositeTypeDef == NULL)
  2962. {
  2963. BfTypeDefMap::Entry* nextEntry = NULL;
  2964. if (rootTypeDefEntry->mNext != -1)
  2965. nextEntry = &mSystem->mTypeDefs.mEntries[rootTypeDefEntry->mNext];
  2966. if ((rootTypeDef->mIsExplicitPartial) || (nextEntry == NULL) ||
  2967. (!nextEntry->mValue->mIsCombinedPartial) ||
  2968. (nextEntry->mValue->mTypeCode != rootTypeDef->mTypeCode) ||
  2969. (nextEntry->mValue->mIsFunction != rootTypeDef->mIsFunction) ||
  2970. (nextEntry->mValue->mIsDelegate != rootTypeDef->mIsDelegate) ||
  2971. (nextEntry->mValue->mGenericParamDefs.size() != rootTypeDef->mGenericParamDefs.size()))
  2972. {
  2973. compositeTypeDef = new BfTypeDef();
  2974. compositeTypeDef->mSystem = rootTypeDef->mSystem;
  2975. compositeTypeDef->mProject = compositeProject;
  2976. compositeTypeDef->mName = rootTypeDef->mName;
  2977. compositeTypeDef->mName->mRefCount++;
  2978. mSystem->TrackName(compositeTypeDef);
  2979. compositeTypeDef->mNameEx = rootTypeDef->mNameEx;
  2980. compositeTypeDef->mNameEx->Ref();
  2981. compositeTypeDef->mProtection = rootTypeDef->mProtection;
  2982. compositeTypeDef->mNamespace = rootTypeDef->mNamespace;
  2983. if (rootTypeDef->mTypeCode == BfTypeCode_Extension)
  2984. compositeTypeDef->mTypeCode = BfTypeCode_Struct;
  2985. else
  2986. compositeTypeDef->mTypeCode = rootTypeDef->mTypeCode;
  2987. compositeTypeDef->mFullName = rootTypeDef->mFullName;
  2988. compositeTypeDef->mFullNameEx = rootTypeDef->mFullNameEx;
  2989. compositeTypeDef->mIsOpaque = rootTypeDef->mIsOpaque;
  2990. compositeTypeDef->mIsFunction = rootTypeDef->mIsFunction;
  2991. compositeTypeDef->mIsDelegate = rootTypeDef->mIsDelegate;
  2992. compositeTypeDef->mIsCombinedPartial = true;
  2993. for (auto prevGenericParam : rootTypeDef->mGenericParamDefs)
  2994. {
  2995. BfGenericParamDef* copiedGenericParam = new BfGenericParamDef();
  2996. *copiedGenericParam = *prevGenericParam;
  2997. compositeTypeDef->mGenericParamDefs.Add(copiedGenericParam);
  2998. }
  2999. mSystem->mTypeDefs.AddAfter(compositeTypeDef, rootTypeDefEntry);
  3000. partialsHadChanges = true;
  3001. hadSignatureChange = true;
  3002. compositeIsNew = true;
  3003. BfLogSysM("Creating compositeTypeDef %p\n", compositeTypeDef);
  3004. }
  3005. else
  3006. {
  3007. BF_ASSERT(nextEntry->mValue->NameEquals(rootTypeDef));
  3008. compositeTypeDef = nextEntry->mValue;
  3009. if (rootTypeDef != NULL)
  3010. {
  3011. BF_ASSERT(rootTypeDef->mFullNameEx == compositeTypeDef->mFullNameEx);
  3012. }
  3013. if (compositeTypeDef->mNextRevision != NULL)
  3014. {
  3015. // This is an old 'next revision'
  3016. mSystem->InjectNewRevision(compositeTypeDef);
  3017. BF_ASSERT(compositeTypeDef->mNextRevision == NULL);
  3018. }
  3019. }
  3020. }
  3021. else
  3022. {
  3023. // These may not get caught below if the composite project changes
  3024. for (auto checkTypeDef : compositeTypeDef->mPartials)
  3025. {
  3026. if (checkTypeDef->mDefState == BfTypeDef::DefState_Deleted)
  3027. {
  3028. partialsHadChanges = true;
  3029. hadSignatureChange = true;
  3030. }
  3031. }
  3032. }
  3033. // Collect the partials
  3034. BfSizedVector<BfTypeDef*, 8> typeParts;
  3035. typeParts.push_back(rootTypeDef);
  3036. auto checkTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  3037. while (checkTypeDefEntryIdx != -1)
  3038. {
  3039. auto checkTypeDefEntry = &mSystem->mTypeDefs.mEntries[checkTypeDefEntryIdx];
  3040. auto checkTypeDef = checkTypeDefEntry->mValue;
  3041. bool isValidProject = checkTypeDef->mProject->ContainsReference(compositeProject);
  3042. if (checkTypeDef != rootTypeDef)
  3043. {
  3044. if ((checkTypeDef->mIsCombinedPartial) ||
  3045. ((!checkTypeDef->mIsPartial) && (checkTypeDef->mTypeCode != BfTypeCode_Extension)) ||
  3046. (checkTypeDef->mPartialUsed) ||
  3047. (!checkTypeDef->NameEquals(rootTypeDef)) ||
  3048. (checkTypeDef->mGenericParamDefs.size() != rootTypeDef->mGenericParamDefs.size()) ||
  3049. (!isValidProject))
  3050. {
  3051. checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
  3052. continue;
  3053. }
  3054. }
  3055. if (checkTypeDef->mTypeCode == BfTypeCode_Extension)
  3056. {
  3057. // This was an extension that was orphaned but now we're taking it back
  3058. checkTypeDef->mIsPartial = true;
  3059. }
  3060. compositeTypeDef->mPartialUsed = true;
  3061. checkTypeDef->mPartialUsed = true;
  3062. if (checkTypeDef->mDefState == BfTypeDef::DefState_Deleted)
  3063. {
  3064. partialsHadChanges = true;
  3065. hadSignatureChange = true;
  3066. }
  3067. else
  3068. {
  3069. if (checkTypeDef != rootTypeDef)
  3070. typeParts.push_back(checkTypeDef);
  3071. if (checkTypeDef->mNextRevision != NULL)
  3072. {
  3073. partialsHadChanges = true;
  3074. BF_ASSERT(checkTypeDef->mNextRevision->mGenericParamDefs.size() == rootTypeDef->mGenericParamDefs.size());
  3075. //mSystem->InjectNewRevision(checkTypeDef);
  3076. //BF_ASSERT(checkTypeDef->mGenericParamDefs.size() == rootTypeDef->mGenericParamDefs.size());
  3077. }
  3078. else if (checkTypeDef->mDefState == BfTypeDef::DefState_New)
  3079. partialsHadChanges = true;
  3080. }
  3081. checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
  3082. }
  3083. // Set this down here, because the InjectNewRevision will clear this flag
  3084. rootTypeDef->mIsPartial = true;
  3085. if (partialsHadChanges)
  3086. {
  3087. BF_ASSERT(compositeTypeDef->mNextRevision == NULL);
  3088. mSystem->VerifyTypeDef(compositeTypeDef);
  3089. for (auto checkTypeDef : typeParts)
  3090. {
  3091. mSystem->VerifyTypeDef(checkTypeDef);
  3092. // Apply any def state that is more conservative
  3093. if (checkTypeDef->mDefState == BfTypeDef::DefState_Signature_Changed)
  3094. compositeTypeDef->mDefState = BfTypeDef::DefState_Signature_Changed;
  3095. else if (checkTypeDef->mDefState == BfTypeDef::DefState_InlinedInternals_Changed)
  3096. {
  3097. if (compositeTypeDef->mDefState != BfTypeDef::DefState_Signature_Changed)
  3098. compositeTypeDef->mDefState = BfTypeDef::DefState_InlinedInternals_Changed;
  3099. }
  3100. else if (checkTypeDef->mDefState == BfTypeDef::DefState_Internals_Changed)
  3101. {
  3102. if ((compositeTypeDef->mDefState != BfTypeDef::DefState_Signature_Changed) &&
  3103. (compositeTypeDef->mDefState != BfTypeDef::DefState_InlinedInternals_Changed))
  3104. compositeTypeDef->mDefState = BfTypeDef::DefState_Internals_Changed;
  3105. }
  3106. else if (checkTypeDef->mDefState == BfTypeDef::DefState_Refresh)
  3107. {
  3108. if ((compositeTypeDef->mDefState != BfTypeDef::DefState_Signature_Changed) &&
  3109. (compositeTypeDef->mDefState != BfTypeDef::DefState_InlinedInternals_Changed) &&
  3110. (compositeTypeDef->mDefState != BfTypeDef::DefState_Internals_Changed))
  3111. compositeTypeDef->mDefState = BfTypeDef::DefState_Refresh;
  3112. }
  3113. BF_ASSERT(checkTypeDef->mIsPartial);
  3114. if (checkTypeDef->mNextRevision != NULL)
  3115. {
  3116. mSystem->VerifyTypeDef(checkTypeDef->mNextRevision);
  3117. mSystem->InjectNewRevision(checkTypeDef);
  3118. }
  3119. checkTypeDef->mIsPartial = true;
  3120. checkTypeDef->mDefState = BfTypeDef::DefState_Defined;
  3121. mSystem->AddToCompositePartial(mPassInstance, compositeTypeDef, checkTypeDef);
  3122. BfLogSysM("AddToCompositePartial %p added to %p\n", checkTypeDef, compositeTypeDef);
  3123. }
  3124. mSystem->FinishCompositePartial(compositeTypeDef);
  3125. if (!compositeIsNew)
  3126. {
  3127. if (compositeTypeDef->mNextRevision != NULL)
  3128. {
  3129. BF_ASSERT(compositeTypeDef->mPartials.size() != 0);
  3130. }
  3131. }
  3132. // We use the root typedef's namespace search for the composite, but this should only be
  3133. // used for cases where we CANNOT specify a typeref on an extension. IE: custom attributes
  3134. // for a type can only be added on the root typedef. If this changes then we need to make
  3135. // sure that we attach a definingType to attributes
  3136. for (auto name : compositeTypeDef->mNamespaceSearch)
  3137. mSystem->ReleaseAtomComposite(name);
  3138. compositeTypeDef->mNamespaceSearch = rootTypeDef->mNamespaceSearch;
  3139. for (auto name : compositeTypeDef->mNamespaceSearch)
  3140. mSystem->RefAtomComposite(name);
  3141. if (rootTypeDef != NULL)
  3142. compositeTypeDef->mNamespaceSearch = rootTypeDef->mNamespaceSearch;
  3143. else
  3144. compositeTypeDef->mNamespaceSearch.Clear();
  3145. //BfLogSysM("Composite type %p updating. isNew: %d\n", compositeTypeDef, compositeIsNew);
  3146. if (compositeIsNew)
  3147. {
  3148. compositeTypeDef->mDefState = BfTypeDef::DefState_New;
  3149. if (compositeTypeDef->mNextRevision->mTypeCode != BfTypeCode_Extension)
  3150. compositeTypeDef->mTypeCode = compositeTypeDef->mNextRevision->mTypeCode;
  3151. else
  3152. compositeTypeDef->mNextRevision->mTypeCode = compositeTypeDef->mTypeCode;
  3153. mSystem->InjectNewRevision(compositeTypeDef);
  3154. // Reset 'New' state
  3155. compositeTypeDef->mDefState = BfTypeDef::DefState_New;
  3156. }
  3157. else
  3158. {
  3159. if (compositeTypeDef->mNextRevision->mTypeCode == BfTypeCode_Extension)
  3160. compositeTypeDef->mNextRevision->mTypeCode = compositeTypeDef->mTypeCode;
  3161. if (hadSignatureChange)
  3162. compositeTypeDef->mDefState = BfTypeDef::DefState_Signature_Changed;
  3163. }
  3164. if (compositeTypeDef->mDefState == BfTypeDef::DefState_Defined)
  3165. {
  3166. // No changes, just inject
  3167. mSystem->InjectNewRevision(compositeTypeDef);
  3168. }
  3169. /*if (compositeTypeDef->mTypeCode == BfTypeCode_Extension)
  3170. {
  3171. BF_ASSERT(rootTypeDef == NULL);
  3172. compositeTypeDef->mTypeCode = BfTypeCode_Object;
  3173. }*/
  3174. auto latestCompositeTypeDef = compositeTypeDef->GetLatest();
  3175. if (latestCompositeTypeDef->mTypeCode == BfTypeCode_Extension)
  3176. {
  3177. BF_ASSERT(rootTypeDef == NULL);
  3178. latestCompositeTypeDef->mTypeCode = BfTypeCode_Struct;
  3179. }
  3180. BfLogSysM("Partial combined type typedef %p updated from parser %p\n", compositeTypeDef, latestCompositeTypeDef->mTypeDeclaration->GetSourceData());
  3181. }
  3182. if (failedToFindRootType)
  3183. {
  3184. for (auto partialTypeDef : compositeTypeDef->GetLatest()->mPartials)
  3185. {
  3186. if (partialTypeDef->IsExtension())
  3187. {
  3188. // Couldn't find a root type def, treat ourselves as an explicit partial
  3189. auto error = mPassInstance->Fail(StrFormat("Unable to find root type definition for extension '%s'", BfTypeUtils::TypeToString(partialTypeDef->GetLatest()).c_str()),
  3190. partialTypeDef->GetLatest()->mTypeDeclaration->mNameNode);
  3191. if (error != NULL)
  3192. error->mIsPersistent = true;
  3193. }
  3194. }
  3195. }
  3196. else
  3197. {
  3198. for (auto partialTypeDef : compositeTypeDef->GetLatest()->mPartials)
  3199. {
  3200. if (partialTypeDef->IsExtension())
  3201. {
  3202. for (int genericParamIdx = 0; genericParamIdx < BF_MIN(partialTypeDef->mGenericParamDefs.mSize, rootTypeDef->mGenericParamDefs.mSize); genericParamIdx++)
  3203. {
  3204. auto rootParamDef = rootTypeDef->mGenericParamDefs[genericParamIdx];
  3205. auto extParamDef = partialTypeDef->mGenericParamDefs[genericParamIdx];
  3206. if ((rootParamDef->mName != extParamDef->mName) &&
  3207. (!rootParamDef->mNameNodes.IsEmpty()) &&
  3208. (!extParamDef->mNameNodes.IsEmpty()))
  3209. {
  3210. String specializedName = extParamDef->mName;
  3211. auto paramDefTypeDef = mSystem->FindTypeDef(extParamDef->mName, 0);
  3212. if (paramDefTypeDef != NULL)
  3213. {
  3214. switch (paramDefTypeDef->mTypeCode)
  3215. {
  3216. case BfTypeCode_Boolean: specializedName = "Boolean"; break;
  3217. default:
  3218. if ((paramDefTypeDef->mTypeCode >= BfTypeCode_Boolean) && (paramDefTypeDef->mTypeCode <= BfTypeCode_Double))
  3219. {
  3220. specializedName[0] = ::toupper(specializedName[0]);
  3221. if (specializedName.StartsWith("Uint"))
  3222. specializedName[1] = 'I';
  3223. }
  3224. }
  3225. }
  3226. auto error = mPassInstance->Warn(0, StrFormat("Extension generic param name '%s' does not match root definition generic param name '%s'. If generic specialization was intended then consider using 'where %s : %s'.",
  3227. extParamDef->mName.c_str(), rootParamDef->mName.c_str(), rootParamDef->mName.c_str(), specializedName.c_str()),
  3228. extParamDef->mNameNodes[0]);
  3229. if (error != NULL)
  3230. {
  3231. error->mIsPersistent = true;
  3232. mPassInstance->MoreInfo("See root definition", rootParamDef->mNameNodes[0]);
  3233. }
  3234. }
  3235. }
  3236. }
  3237. }
  3238. }
  3239. }
  3240. outerTypeDefEntryIdx = outerTypeDefEntry->mNext;
  3241. }
  3242. // Handle unused partials, apply any new revisions, process pending deletes
  3243. if ((hadPartials) || (hadChanges))
  3244. {
  3245. BfTypeDef* checkMasterTypeDef = NULL;
  3246. BfTypeDef* deletedCombinedPartial = NULL;
  3247. outerTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  3248. while (outerTypeDefEntryIdx != -1)
  3249. {
  3250. auto outerTypeDefEntry = &mSystem->mTypeDefs.mEntries[outerTypeDefEntryIdx];
  3251. auto outerTypeDef = outerTypeDefEntry->mValue;
  3252. auto nextTypeDefEntryIdx = outerTypeDefEntry->mNext;
  3253. BfTypeDefMap::Entry* nextTypeDefEntry = NULL;
  3254. if (nextTypeDefEntryIdx != -1)
  3255. nextTypeDefEntry = &mSystem->mTypeDefs.mEntries[nextTypeDefEntryIdx];
  3256. if ((outerTypeDef->mIsPartial) && (!outerTypeDef->mIsExplicitPartial) && (outerTypeDef->mTypeCode != BfTypeCode_Extension) &&
  3257. (nextTypeDefEntry != NULL) && (!nextTypeDefEntry->mValue->mPartialUsed))
  3258. {
  3259. // This is a root type that we've removed all extensions from, so now we go back to treating it as the actual definition
  3260. // instead of using the composite that immediately follows it
  3261. BF_ASSERT(outerTypeDef->mTypeCode != BfTypeCode_Extension);
  3262. outerTypeDef->mIsPartial = false;
  3263. outerTypeDef->mPartialIdx = -1;
  3264. }
  3265. if (outerTypeDef->mDefState == BfTypeDef::DefState_Deleted)
  3266. {
  3267. BfLogSysM("UpdateRevisedTypes deleting outerTypeDef %p\n", outerTypeDef);
  3268. outerTypeDef->mDefState = BfTypeDef::DefState_Deleted;
  3269. mSystem->RemoveTypeDef(outerTypeDef);
  3270. }
  3271. else if (!outerTypeDef->mPartialUsed)
  3272. {
  3273. if (outerTypeDef->mIsCombinedPartial)
  3274. {
  3275. BfLogSysM("UpdateRevisedTypes deleting combinedPartial type %p\n", outerTypeDef);
  3276. deletedCombinedPartial = outerTypeDef;
  3277. outerTypeDef->mDefState = BfTypeDef::DefState_Deleted;
  3278. mSystem->RemoveTypeDef(outerTypeDef);
  3279. }
  3280. else if (outerTypeDef->mTypeCode == BfTypeCode_Extension)
  3281. {
  3282. auto error = mPassInstance->Fail(StrFormat("Unable to find root type definition for extension '%s'", outerTypeDef->GetLatest()->mFullName.ToString().c_str()),
  3283. outerTypeDef->GetLatest()->mTypeDeclaration->mNameNode);
  3284. if (error != NULL)
  3285. error->mIsPersistent = true;
  3286. if (outerTypeDef->mIsPartial)
  3287. {
  3288. // Allow this typeDef be a full solo type by itself
  3289. //outerTypeDef->mTypeCode = BfTypeCode_Struct;
  3290. outerTypeDef->mIsPartial = false;
  3291. if (outerTypeDef->mNextRevision != NULL)
  3292. outerTypeDef->mNextRevision->mIsPartial = false;
  3293. if (outerTypeDef->mPartialIdx != -1)
  3294. {
  3295. outerTypeDef->mPartialIdx = -1;
  3296. outerTypeDef->mDefState = BfTypeDef::DefState_New;
  3297. }
  3298. }
  3299. }
  3300. }
  3301. outerTypeDefEntryIdx = nextTypeDefEntryIdx;
  3302. }
  3303. }
  3304. }
  3305. for (auto typeDef : prevSoloExtensions)
  3306. {
  3307. // If this got added to a composite partial then delete the previous solo type
  3308. if (typeDef->mIsPartial)
  3309. {
  3310. BfLogSysM("Solo partial going back to normal partial %p\n", typeDef);
  3311. typeDef->mIsPartial = false;
  3312. auto type = mContext->mScratchModule->ResolveTypeDef(typeDef, BfPopulateType_Identity);
  3313. mContext->DeleteType(type);
  3314. typeDef->mIsPartial = true;
  3315. }
  3316. }
  3317. mContext->UpdateRevisedTypes();
  3318. mContext->VerifyTypeLookups();
  3319. mContext->ValidateDependencies();
  3320. if (mStats.mTypesDeleted != 0)
  3321. {
  3322. mContext->UpdateAfterDeletingTypes();
  3323. mContext->ValidateDependencies();
  3324. }
  3325. mContext->RemoveInvalidWorkItems();
  3326. mSystem->mNeedsTypesHandledByCompiler = false;
  3327. //TODO:
  3328. //Sleep(300);
  3329. //mSystem->CheckLockYield();
  3330. }
  3331. BfTypeDef* BfCompiler::GetArrayTypeDef(int dimensions)
  3332. {
  3333. BF_ASSERT(dimensions <= 4);
  3334. if (dimensions == 1)
  3335. return mArray1TypeDef;
  3336. if (dimensions == 2)
  3337. return mArray2TypeDef;
  3338. if (dimensions == 3)
  3339. return mArray3TypeDef;
  3340. return mArray4TypeDef;
  3341. }
  3342. void BfCompiler::VisitAutocompleteExteriorIdentifiers()
  3343. {
  3344. for (auto checkNode : mResolvePassData->mExteriorAutocompleteCheckNodes)
  3345. {
  3346. bool isUsingDirective = false;
  3347. BfIdentifierNode* checkIdentifier = NULL;
  3348. if (auto usingDirective = BfNodeDynCast<BfUsingDirective>(checkNode))
  3349. {
  3350. checkIdentifier = usingDirective->mNamespace;
  3351. }
  3352. else if (auto usingDirective = BfNodeDynCast<BfUsingModDirective>(checkNode))
  3353. {
  3354. if (usingDirective->mTypeRef != NULL)
  3355. {
  3356. BF_ASSERT(mContext->mScratchModule->mCurTypeInstance == NULL);
  3357. SetAndRestoreValue<BfTypeInstance*> prevCurTypeInstance(mContext->mScratchModule->mCurTypeInstance, NULL);
  3358. SetAndRestoreValue<bool> prevIgnoreErrors(mContext->mScratchModule->mIgnoreErrors, true);
  3359. mContext->mScratchModule->ResolveTypeRef(usingDirective->mTypeRef, NULL);
  3360. if (mResolvePassData->mAutoComplete != NULL)
  3361. mResolvePassData->mAutoComplete->CheckTypeRef(usingDirective->mTypeRef, false, isUsingDirective);
  3362. continue;
  3363. }
  3364. }
  3365. else
  3366. checkIdentifier = BfNodeDynCast<BfIdentifierNode>(checkNode);
  3367. if (checkIdentifier == NULL)
  3368. continue;
  3369. if (mResolvePassData->mAutoComplete != NULL)
  3370. mResolvePassData->mAutoComplete->CheckIdentifier(checkIdentifier, false, isUsingDirective);
  3371. if (auto sourceClassifier = mResolvePassData->GetSourceClassifier(checkIdentifier))
  3372. {
  3373. if (isUsingDirective)
  3374. {
  3375. while (auto qualifiedNameNode = BfNodeDynCast<BfQualifiedNameNode>(checkIdentifier))
  3376. {
  3377. sourceClassifier->SetElementType(qualifiedNameNode->mRight, BfSourceElementType_Namespace);
  3378. checkIdentifier = qualifiedNameNode->mLeft;
  3379. }
  3380. if (checkIdentifier != NULL)
  3381. sourceClassifier->SetElementType(checkIdentifier, BfSourceElementType_Namespace);
  3382. }
  3383. }
  3384. }
  3385. mResolvePassData->mExteriorAutocompleteCheckNodes.Clear();
  3386. }
  3387. void BfCompiler::VisitSourceExteriorNodes()
  3388. {
  3389. BP_ZONE("BfCompiler::VisitSourceExteriorNodes");
  3390. String str;
  3391. Array<BfAtom*> namespaceParts;
  3392. Array<BfAstNode*> srcNodes;
  3393. std::function<bool(BfAstNode*, bool)> _AddName = [&](BfAstNode* node, bool wantErrors)
  3394. {
  3395. if (node == NULL)
  3396. return false;
  3397. if (auto qualifiedName = BfNodeDynCast<BfQualifiedNameNode>(node))
  3398. {
  3399. if (!_AddName(qualifiedName->mLeft, wantErrors))
  3400. return false;
  3401. if (!_AddName(qualifiedName->mRight, wantErrors))
  3402. return false;
  3403. return true;
  3404. }
  3405. else if (auto qualifedTypeRef = BfNodeDynCast<BfQualifiedTypeReference>(node))
  3406. {
  3407. if (!_AddName(qualifedTypeRef->mLeft, wantErrors))
  3408. return false;
  3409. if (!_AddName(qualifedTypeRef->mRight, wantErrors))
  3410. return false;
  3411. return true;
  3412. }
  3413. else if ((node->IsA<BfIdentifierNode>()) || (node->IsA<BfNamedTypeReference>()))
  3414. {
  3415. srcNodes.Add(node);
  3416. str.Clear();
  3417. node->ToString(str);
  3418. auto atom = mSystem->FindAtom(str);
  3419. if (atom == NULL)
  3420. {
  3421. String prevNamespace;
  3422. for (auto part : namespaceParts)
  3423. {
  3424. if (!prevNamespace.IsEmpty())
  3425. prevNamespace += ".";
  3426. prevNamespace += part->mString;
  3427. }
  3428. if (wantErrors)
  3429. {
  3430. if (prevNamespace.IsEmpty())
  3431. mPassInstance->Fail(StrFormat("The namespace '%s' does not exist", str.c_str()), node);
  3432. else
  3433. mPassInstance->Fail(StrFormat("The namespace '%s' does not exist in the namespace '%s'", str.c_str(), prevNamespace.c_str()), node);
  3434. }
  3435. return false;
  3436. }
  3437. namespaceParts.Add(atom);
  3438. return true;
  3439. }
  3440. return false;
  3441. };
  3442. auto _CheckNamespace = [&](BfParser* parser, bool wantErrors, bool& failed)
  3443. {
  3444. for (int i = 0; i < (int)namespaceParts.size(); i++)
  3445. {
  3446. BfAtomComposite checkNamespace;
  3447. checkNamespace.mParts = &namespaceParts[0];
  3448. checkNamespace.mSize = i + 1;
  3449. if (!mSystem->ContainsNamespace(checkNamespace, parser->mProject))
  3450. {
  3451. failed = true;
  3452. BfAtomComposite prevNamespace;
  3453. prevNamespace.mParts = &namespaceParts[0];
  3454. prevNamespace.mSize = i;
  3455. if (wantErrors)
  3456. {
  3457. if (i == 0)
  3458. mPassInstance->Fail(StrFormat("The namespace '%s' does not exist", namespaceParts[i]->mString.ToString().c_str()), srcNodes[i]);
  3459. else
  3460. mPassInstance->Fail(StrFormat("The namespace '%s' does not exist in the namespace '%s'", namespaceParts[i]->mString.ToString().c_str(), prevNamespace.ToString().c_str()), srcNodes[i]);
  3461. }
  3462. return false;
  3463. }
  3464. }
  3465. return true;
  3466. };
  3467. auto _CheckParser = [&](BfParser* parser)
  3468. {
  3469. while (parser->mNextRevision != NULL)
  3470. parser = parser->mNextRevision;
  3471. if (parser->mAwaitingDelete)
  3472. return;
  3473. if (parser->mParserData == NULL)
  3474. return;
  3475. if (parser->mParserData->mExteriorNodesCheckIdx == mSystem->mTypeMapVersion)
  3476. return;
  3477. bool failed = false;
  3478. for (auto& node : parser->mParserData->mExteriorNodes)
  3479. {
  3480. SetAndRestoreValue<BfSizedArray<BfNamespaceDeclaration*>*> prevCurNamespaceNodes(mContext->mCurNamespaceNodes, &node.mNamespaceNodes);
  3481. auto exteriorAstNode = node.mNode;
  3482. if (auto usingDirective = BfNodeDynCast<BfUsingDirective>(exteriorAstNode))
  3483. {
  3484. srcNodes.Clear();
  3485. namespaceParts.Clear();
  3486. bool success = _AddName(usingDirective->mNamespace, true);
  3487. _CheckNamespace(parser, true, failed);
  3488. }
  3489. else if (auto usingDirective = BfNodeDynCast<BfUsingModDirective>(exteriorAstNode))
  3490. {
  3491. if (usingDirective->mTypeRef != NULL)
  3492. {
  3493. BF_ASSERT(mContext->mScratchModule->mCurTypeInstance == NULL);
  3494. bool wasNamespace = false;
  3495. if (usingDirective->mModToken->mToken == BfToken_Internal)
  3496. {
  3497. srcNodes.Clear();
  3498. namespaceParts.Clear();
  3499. if (_AddName(usingDirective->mTypeRef, false))
  3500. {
  3501. wasNamespace = _CheckNamespace(parser, false, failed);
  3502. }
  3503. }
  3504. if (!wasNamespace)
  3505. {
  3506. SetAndRestoreValue<BfTypeInstance*> prevCurTypeInstance(mContext->mScratchModule->mCurTypeInstance, NULL);
  3507. if (auto genericTypeRef = BfNodeDynCast<BfGenericInstanceTypeRef>(usingDirective->mTypeRef))
  3508. {
  3509. mContext->mScratchModule->ResolveTypeRefAllowUnboundGenerics(usingDirective->mTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_NoReify);
  3510. }
  3511. else
  3512. mContext->mScratchModule->ResolveTypeRef(usingDirective->mTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_NoReify);
  3513. if ((mResolvePassData != NULL) && (mResolvePassData->mAutoComplete != NULL))
  3514. mResolvePassData->mAutoComplete->CheckTypeRef(usingDirective->mTypeRef, false, false);
  3515. }
  3516. }
  3517. }
  3518. }
  3519. if (!failed)
  3520. parser->mParserData->mExteriorNodesCheckIdx = mSystem->mTypeMapVersion;
  3521. };
  3522. if (mResolvePassData != NULL)
  3523. {
  3524. for (auto parser : mResolvePassData->mParsers)
  3525. _CheckParser(parser);
  3526. }
  3527. else
  3528. {
  3529. for (auto parser : mSystem->mParsers)
  3530. {
  3531. _CheckParser(parser);
  3532. }
  3533. }
  3534. }
  3535. void BfCompiler::ProcessAutocompleteTempType()
  3536. {
  3537. BP_ZONE_F("BfCompiler::ProcessAutocompleteTempType %d", mResolvePassData->mResolveType);
  3538. String& autoCompleteResultString = *gTLStrReturn.Get();
  3539. autoCompleteResultString.clear();
  3540. if (mContext->mBfObjectType == NULL)
  3541. return; // Not initialized yet
  3542. auto module = mContext->mScratchModule;
  3543. auto autoComplete = mResolvePassData->mAutoComplete;
  3544. BfLogSysM("ProcessAutocompleteTempType %d\n", autoComplete->mResolveType);
  3545. SetAndRestoreValue<bool> prevCanceling(mCanceling, false);
  3546. BF_ASSERT(mResolvePassData->mAutoComplete->mDefMethod == NULL);
  3547. if (autoComplete->mResolveType == BfResolveType_GetNavigationData)
  3548. {
  3549. for (auto parser : mResolvePassData->mParsers)
  3550. {
  3551. for (auto node : parser->mSidechannelRootNode->mChildArr)
  3552. {
  3553. if (auto preprocNode = BfNodeDynCast<BfPreprocessorNode>(node))
  3554. {
  3555. if (preprocNode->mCommand->Equals("region"))
  3556. {
  3557. if (!autoCompleteResultString.empty())
  3558. autoCompleteResultString += "\n";
  3559. autoCompleteResultString += "#";
  3560. preprocNode->mArgument->ToString(autoCompleteResultString);
  3561. mContext->mScratchModule->UpdateSrcPos(preprocNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
  3562. autoCompleteResultString += StrFormat("\tregion\t%d\t%d", module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
  3563. }
  3564. }
  3565. }
  3566. }
  3567. for (auto tempTypeDef : mResolvePassData->mAutoCompleteTempTypes)
  3568. {
  3569. String typeName = tempTypeDef->ToString();
  3570. BfLogSysM("BfResolveType_GetNavigationData TypeDef:%p %s\n", tempTypeDef, typeName.c_str());
  3571. auto refNode = tempTypeDef->GetRefNode();
  3572. if ((refNode != NULL) && (!tempTypeDef->IsGlobalsContainer()))
  3573. {
  3574. if (!autoCompleteResultString.empty())
  3575. autoCompleteResultString += "\n";
  3576. String typeName = BfTypeUtils::TypeToString(tempTypeDef, BfTypeNameFlag_OmitNamespace);
  3577. module->UpdateSrcPos(refNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
  3578. autoCompleteResultString += typeName;
  3579. if (tempTypeDef->mTypeCode == BfTypeCode_Object)
  3580. autoCompleteResultString += "\tclass";
  3581. else if (tempTypeDef->mTypeCode == BfTypeCode_Enum)
  3582. autoCompleteResultString += "\tenum";
  3583. else if (tempTypeDef->mTypeCode == BfTypeCode_Struct)
  3584. autoCompleteResultString += "\tstruct";
  3585. else if (tempTypeDef->mTypeCode == BfTypeCode_TypeAlias)
  3586. autoCompleteResultString += "\ttypealias";
  3587. else
  3588. autoCompleteResultString += "\t";
  3589. autoCompleteResultString += StrFormat("\t%d\t%d", module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
  3590. }
  3591. String methodText;
  3592. for (auto methodDef : tempTypeDef->mMethods)
  3593. {
  3594. if (((methodDef->mMethodType == BfMethodType_Normal) || (methodDef->mMethodType == BfMethodType_Operator) ||
  3595. (methodDef->mMethodType == BfMethodType_Ctor) || (methodDef->mMethodType == BfMethodType_Dtor) ||
  3596. (methodDef->mMethodType == BfMethodType_Mixin) || (methodDef->mMethodType == BfMethodType_Extension)) &&
  3597. (methodDef->mMethodDeclaration != NULL))
  3598. {
  3599. methodText = methodDef->ToString();
  3600. if (typeName != "@")
  3601. methodText = typeName + "." + methodText;
  3602. if (!autoCompleteResultString.empty())
  3603. autoCompleteResultString += "\n";
  3604. auto methodDeclaration = methodDef->GetMethodDeclaration();
  3605. BfAstNode* refNode = methodDeclaration;
  3606. if (methodDeclaration->mBody != NULL)
  3607. refNode = methodDeclaration->mBody;
  3608. else if (methodDeclaration->mNameNode != NULL)
  3609. refNode = methodDeclaration->mNameNode;
  3610. module->UpdateSrcPos(refNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
  3611. const char* typeStr = (methodDef->mMethodType == BfMethodType_Extension) ? "extmethod" : "method";
  3612. methodText += StrFormat("\t%s\t%d\t%d", typeStr, module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
  3613. autoCompleteResultString += methodText;
  3614. }
  3615. }
  3616. for (auto fieldDef : tempTypeDef->mFields)
  3617. {
  3618. auto fieldDeclaration = BfNodeDynCast<BfFieldDeclaration>(fieldDef->mFieldDeclaration);
  3619. if ((fieldDeclaration == NULL) || (fieldDeclaration->mNameNode == NULL) || (BfNodeIsA<BfPropertyDeclaration>(fieldDef->mFieldDeclaration)))
  3620. continue;
  3621. String fieldText = fieldDef->mName;
  3622. if (typeName != "@")
  3623. fieldText = typeName + "." + fieldText;
  3624. if (!autoCompleteResultString.empty())
  3625. autoCompleteResultString += "\n";
  3626. BfAstNode* refNode = fieldDeclaration->mNameNode;
  3627. module->UpdateSrcPos(refNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
  3628. fieldText += StrFormat("\tfield\t%d\t%d", module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
  3629. autoCompleteResultString += fieldText;
  3630. }
  3631. for (auto propDef : tempTypeDef->mProperties)
  3632. {
  3633. auto propDeclaration = BfNodeDynCast<BfPropertyDeclaration>(propDef->mFieldDeclaration);
  3634. if (propDeclaration == NULL)
  3635. continue;
  3636. String propText;
  3637. BfAstNode* refNode = propDeclaration->mNameNode;
  3638. auto indexerDeclaration = BfNodeDynCast<BfIndexerDeclaration>(propDef->mFieldDeclaration);
  3639. if (indexerDeclaration != NULL)
  3640. {
  3641. refNode = indexerDeclaration->mThisToken;
  3642. propText = "this[";
  3643. for (int i = 0; i < indexerDeclaration->mParams.mSize; i++)
  3644. {
  3645. if (i > 0)
  3646. propText += ", ";
  3647. propText += indexerDeclaration->mParams[i]->ToString();
  3648. }
  3649. propText += "]";
  3650. }
  3651. else
  3652. {
  3653. if (propDeclaration->mNameNode == NULL)
  3654. continue;
  3655. propText = propDef->mName;
  3656. }
  3657. if (typeName != "@")
  3658. propText = typeName + "." + propText;
  3659. if (!autoCompleteResultString.empty())
  3660. autoCompleteResultString += "\n";
  3661. module->UpdateSrcPos(refNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
  3662. propText += StrFormat("\tproperty\t%d\t%d", module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
  3663. autoCompleteResultString += propText;
  3664. }
  3665. }
  3666. module->CleanupFileInstances();
  3667. return;
  3668. }
  3669. if (autoComplete->mResolveType == BfResolveType_GetCurrentLocation)
  3670. {
  3671. for (auto tempTypeDef : mResolvePassData->mAutoCompleteTempTypes)
  3672. {
  3673. String typeName = tempTypeDef->mNamespace.ToString();
  3674. if (!typeName.empty())
  3675. typeName += ".";
  3676. typeName += tempTypeDef->ToString();
  3677. autoCompleteResultString = typeName;
  3678. for (auto methodDef : tempTypeDef->mMethods)
  3679. {
  3680. BfAstNode* defNode = methodDef->mMethodDeclaration;
  3681. if (auto propertyDeclaration = methodDef->GetPropertyDeclaration())
  3682. defNode = propertyDeclaration;
  3683. if (defNode == NULL)
  3684. continue;
  3685. auto parser = defNode->GetParser();
  3686. if ((parser == NULL) || (parser->mCursorIdx == -1))
  3687. continue;
  3688. if ((defNode != NULL) &&
  3689. (defNode->Contains(parser->mCursorIdx)))
  3690. {
  3691. String methodText = methodDef->ToString();
  3692. if (typeName != "@")
  3693. methodText = typeName + "." + methodText;
  3694. autoCompleteResultString = methodText;
  3695. break;
  3696. }
  3697. }
  3698. }
  3699. if (mResolvePassData->mAutoCompleteTempTypes.IsEmpty())
  3700. {
  3701. for (auto& kv : mResolvePassData->mEmitEmbedEntries)
  3702. {
  3703. if (kv.mValue.mCursorIdx < 0)
  3704. continue;
  3705. String typeName = kv.mKey;
  3706. auto type = GetType(typeName);
  3707. if (type == NULL)
  3708. continue;
  3709. auto typeInst = type->ToTypeInstance();
  3710. if (typeInst == NULL)
  3711. continue;
  3712. if (mResolvePassData->mParsers.IsEmpty())
  3713. break;
  3714. autoCompleteResultString = mContext->mScratchModule->TypeToString(typeInst);
  3715. for (auto methodDef : typeInst->mTypeDef->mMethods)
  3716. {
  3717. BfAstNode* defNode = methodDef->mMethodDeclaration;
  3718. if (auto propertyDeclaration = methodDef->GetPropertyDeclaration())
  3719. defNode = propertyDeclaration;
  3720. if (defNode == NULL)
  3721. continue;
  3722. if ((defNode != NULL) &&
  3723. (defNode->Contains(kv.mValue.mCursorIdx)))
  3724. {
  3725. auto defParser = defNode->GetParser();
  3726. if (defParser == NULL)
  3727. continue;
  3728. if (!defParser->mIsEmitted)
  3729. continue;
  3730. autoCompleteResultString += ".";
  3731. autoCompleteResultString += methodDef->ToString();
  3732. break;
  3733. }
  3734. }
  3735. }
  3736. }
  3737. module->CleanupFileInstances();
  3738. return;
  3739. }
  3740. mFastFinish = false;
  3741. SetAndRestoreValue<BfMethodState*> prevMethodState(module->mCurMethodState, NULL);
  3742. SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(module->mCurTypeInstance, NULL);
  3743. SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(module->mCurMethodInstance, NULL);
  3744. SetAndRestoreValue<BfTypeState*> prevTypeState(module->mContext->mCurTypeState, NULL);
  3745. // >>> VisitExteriorIdentifiers
  3746. mResolvePassData->mAutoComplete->SetModule(module);
  3747. {
  3748. BP_ZONE("VisitExteriorIdentifiers");
  3749. VisitAutocompleteExteriorIdentifiers();
  3750. }
  3751. VisitSourceExteriorNodes();
  3752. if (autoComplete->mResolveType == BfResolveType_GetFixits)
  3753. {
  3754. BfAstNode* conflictStart = NULL;
  3755. BfAstNode* conflictSplit = NULL;
  3756. for (auto parser : mResolvePassData->mParsers)
  3757. {
  3758. auto src = parser->mSrc;
  3759. for (int checkIdx = 0; checkIdx < (int)parser->mSidechannelRootNode->mChildArr.mSize; checkIdx++)
  3760. {
  3761. auto sideNode = parser->mSidechannelRootNode->mChildArr.mVals[checkIdx];
  3762. if (autoComplete->CheckFixit(sideNode))
  3763. {
  3764. if (src[sideNode->mSrcStart] == '<')
  3765. {
  3766. conflictStart = sideNode;
  3767. conflictSplit = NULL;
  3768. }
  3769. }
  3770. else
  3771. {
  3772. if (src[sideNode->mSrcStart] == '<')
  3773. {
  3774. conflictStart = NULL;
  3775. conflictSplit = NULL;
  3776. }
  3777. else if (src[sideNode->mSrcStart] == '=')
  3778. {
  3779. if (conflictStart != NULL)
  3780. conflictSplit = sideNode;
  3781. }
  3782. else if (src[sideNode->mSrcStart] == '>')
  3783. {
  3784. if (conflictSplit != NULL)
  3785. {
  3786. autoComplete->AddEntry(AutoCompleteEntry("fixit", StrFormat("Accept First\tdelete|%s-%d|\x01""delete|%s-%d|",
  3787. autoComplete->FixitGetLocation(parser->mParserData, conflictSplit->mSrcStart).c_str(), sideNode->mSrcEnd - conflictSplit->mSrcStart + 1,
  3788. autoComplete->FixitGetLocation(parser->mParserData, conflictStart->mSrcStart).c_str(), conflictStart->mSrcEnd - conflictStart->mSrcStart + 1).c_str()));
  3789. autoComplete->AddEntry(AutoCompleteEntry("fixit", StrFormat("Accept Second\tdelete|%s-%d|\x01""delete|%s-%d|",
  3790. autoComplete->FixitGetLocation(parser->mParserData, sideNode->mSrcStart).c_str(), sideNode->mSrcEnd - sideNode->mSrcStart + 1,
  3791. autoComplete->FixitGetLocation(parser->mParserData, conflictStart->mSrcStart).c_str(), conflictSplit->mSrcEnd - conflictStart->mSrcStart + 1).c_str()));
  3792. autoComplete->AddEntry(AutoCompleteEntry("fixit", StrFormat("Accept Both\tdelete|%s-%d|\x01""delete|%s-%d|\x01""delete|%s-%d|",
  3793. autoComplete->FixitGetLocation(parser->mParserData, sideNode->mSrcStart).c_str(), sideNode->mSrcEnd - sideNode->mSrcStart + 1,
  3794. autoComplete->FixitGetLocation(parser->mParserData, conflictSplit->mSrcStart).c_str(), conflictSplit->mSrcEnd - conflictSplit->mSrcStart + 1,
  3795. autoComplete->FixitGetLocation(parser->mParserData, conflictStart->mSrcStart).c_str(), conflictStart->mSrcEnd - conflictStart->mSrcStart + 1).c_str()));
  3796. conflictStart = NULL;
  3797. conflictSplit = NULL;
  3798. }
  3799. }
  3800. }
  3801. }
  3802. }
  3803. }
  3804. if (autoComplete->mResolveType == BfResolveType_GetSymbolInfo)
  3805. {
  3806. BfNamespaceVisitor namespaceVisitor;
  3807. namespaceVisitor.mResolvePassData = mResolvePassData;
  3808. namespaceVisitor.mSystem = mSystem;
  3809. for (auto parser : mResolvePassData->mParsers)
  3810. namespaceVisitor.Visit(parser->mRootNode);
  3811. }
  3812. auto _FindAcutalTypeDef = [&](BfTypeDef* tempTypeDef)
  3813. {
  3814. auto typeName = tempTypeDef->mFullName;
  3815. int wantNumGenericParams = (int)tempTypeDef->mGenericParamDefs.size();
  3816. auto actualTypeDefItr = mSystem->mTypeDefs.TryGet(typeName);
  3817. while (actualTypeDefItr)
  3818. {
  3819. auto checkTypeDef = *actualTypeDefItr;
  3820. if ((!checkTypeDef->mIsPartial) && (checkTypeDef->mName == tempTypeDef->mName) &&
  3821. (checkTypeDef->mIsFunction == tempTypeDef->mIsFunction) && (checkTypeDef->mIsDelegate == tempTypeDef->mIsDelegate) &&
  3822. ((checkTypeDef->mTypeCode == tempTypeDef->mTypeCode) || (tempTypeDef->mTypeCode == BfTypeCode_Extension) || (tempTypeDef->mTypeCode == BfTypeCode_Inferred)))
  3823. {
  3824. if ((checkTypeDef->NameEquals(tempTypeDef)) && (checkTypeDef->mIsCombinedPartial) &&
  3825. (checkTypeDef->mGenericParamDefs.size() == tempTypeDef->mGenericParamDefs.size()) &&
  3826. (tempTypeDef->mProject->ContainsReference(checkTypeDef->mProject)))
  3827. {
  3828. return mSystem->FilterDeletedTypeDef(checkTypeDef);
  3829. }
  3830. if ((checkTypeDef->mGenericParamDefs.size() == wantNumGenericParams) &&
  3831. (FileNameEquals(tempTypeDef->mSource->mSourceData->ToParserData()->mFileName, checkTypeDef->mSource->mSourceData->ToParserData()->mFileName)) &&
  3832. (tempTypeDef->mProject == checkTypeDef->mProject))
  3833. {
  3834. return mSystem->FilterDeletedTypeDef(checkTypeDef);
  3835. }
  3836. }
  3837. actualTypeDefItr.MoveToNextHashMatch();
  3838. }
  3839. return (BfTypeDef*)NULL;
  3840. };
  3841. BfTypeDef* tempTypeDef = NULL;
  3842. BfTypeDef* actualTypeDef = NULL;
  3843. for (auto checkTempType : mResolvePassData->mAutoCompleteTempTypes)
  3844. {
  3845. if (mResolvePassData->mAutoComplete->IsAutocompleteNode(checkTempType->mTypeDeclaration))
  3846. {
  3847. BfTypeDef* checkActualTypeDef = _FindAcutalTypeDef(checkTempType);
  3848. if ((actualTypeDef == NULL) || (checkActualTypeDef != NULL))
  3849. {
  3850. actualTypeDef = checkActualTypeDef;
  3851. tempTypeDef = checkTempType;
  3852. }
  3853. mContext->HandleChangedTypeDef(checkTempType, true);
  3854. }
  3855. auto sourceClassifier = mResolvePassData->GetSourceClassifier(checkTempType->mTypeDeclaration->mNameNode);
  3856. if (sourceClassifier == NULL)
  3857. continue;
  3858. BfSourceElementType elemType = BfSourceElementType_Type;
  3859. if (checkTempType->mTypeCode == BfTypeCode_Interface)
  3860. elemType = BfSourceElementType_Interface;
  3861. else if (checkTempType->mTypeCode == BfTypeCode_Object)
  3862. elemType = BfSourceElementType_RefType;
  3863. else if (checkTempType->mTypeCode == BfTypeCode_Struct)
  3864. elemType = BfSourceElementType_Struct;
  3865. sourceClassifier->SetElementType(checkTempType->mTypeDeclaration->mNameNode, elemType);
  3866. }
  3867. if (tempTypeDef == NULL)
  3868. {
  3869. if ((autoComplete != NULL) && (autoComplete->mResolveType == BfResolveType_GoToDefinition))
  3870. {
  3871. autoComplete->SetModule(NULL);
  3872. for (auto& kv : mResolvePassData->mEmitEmbedEntries)
  3873. {
  3874. String typeName = kv.mKey;
  3875. auto type = GetType(typeName);
  3876. if (type == NULL)
  3877. continue;
  3878. auto typeInst = type->ToTypeInstance();
  3879. if (typeInst == NULL)
  3880. continue;
  3881. mContext->RebuildType(typeInst);
  3882. if (!typeInst->mModule->mIsModuleMutable)
  3883. typeInst->mModule->StartNewRevision(BfModule::RebuildKind_All, true);
  3884. mContext->mScratchModule->PopulateType(typeInst, Beefy::BfPopulateType_Full_Force);
  3885. }
  3886. DoWorkLoop();
  3887. autoComplete->SetModule(mContext->mScratchModule);
  3888. }
  3889. GenerateAutocompleteInfo();
  3890. BfLogSysM("ProcessAutocompleteTempType - no tempTypeDef\n");
  3891. return;
  3892. }
  3893. if (tempTypeDef->mProject->mDisabled)
  3894. {
  3895. BfLogSysM("ProcessAutocompleteTempType - project disabled\n");
  3896. return;
  3897. }
  3898. BfTypeState typeState;
  3899. typeState.mCurTypeDef = tempTypeDef;
  3900. module->mContext->mCurTypeState = &typeState;
  3901. BfStaticSearch* staticSearch = NULL;
  3902. if (mResolvePassData->mStaticSearchMap.TryAdd(tempTypeDef, NULL, &staticSearch))
  3903. {
  3904. for (auto typeRef : tempTypeDef->mStaticSearch)
  3905. {
  3906. auto type = module->ResolveTypeRef(typeRef, NULL, BfPopulateType_Declaration);
  3907. if (type != NULL)
  3908. {
  3909. auto typeInst = type->ToTypeInstance();
  3910. if (typeInst != NULL)
  3911. staticSearch->mStaticTypes.Add(typeInst);
  3912. }
  3913. }
  3914. }
  3915. BfInternalAccessSet* internalAccessSet = NULL;
  3916. if (mResolvePassData->mInternalAccessMap.TryAdd(tempTypeDef, NULL, &internalAccessSet))
  3917. {
  3918. for (auto typeRef : tempTypeDef->mInternalAccessSet)
  3919. {
  3920. if ((typeRef->IsA<BfNamedTypeReference>()) ||
  3921. (typeRef->IsA<BfQualifiedTypeReference>()))
  3922. {
  3923. String checkNamespaceStr;
  3924. typeRef->ToString(checkNamespaceStr);
  3925. BfAtomComposite checkNamespace;
  3926. if (mSystem->ParseAtomComposite(checkNamespaceStr, checkNamespace))
  3927. {
  3928. if (mSystem->ContainsNamespace(checkNamespace, tempTypeDef->mProject))
  3929. {
  3930. internalAccessSet->mNamespaces.Add(checkNamespace);
  3931. continue;
  3932. }
  3933. }
  3934. }
  3935. BfType* type;
  3936. if (auto genericTypeRef = BfNodeDynCast<BfGenericInstanceTypeRef>(typeRef))
  3937. type = mContext->mScratchModule->ResolveTypeRefAllowUnboundGenerics(typeRef, BfPopulateType_Identity);
  3938. else
  3939. type = module->ResolveTypeRef(typeRef, NULL, BfPopulateType_Identity);
  3940. if (type != NULL)
  3941. {
  3942. auto typeInst = type->ToTypeInstance();
  3943. if (typeInst != NULL)
  3944. internalAccessSet->mTypes.Add(typeInst);
  3945. }
  3946. }
  3947. }
  3948. if (tempTypeDef->mTypeCode == BfTypeCode_Extension)
  3949. {
  3950. BfTypeInstance* outerTypeInstance = NULL;
  3951. if (tempTypeDef->mOuterType != NULL)
  3952. {
  3953. auto outerTypeDef = _FindAcutalTypeDef(tempTypeDef->mOuterType);
  3954. if (outerTypeDef != NULL)
  3955. {
  3956. outerTypeInstance = (BfTypeInstance*)module->ResolveTypeDef(outerTypeDef, BfPopulateType_IdentityNoRemapAlias);
  3957. if ((outerTypeInstance != NULL) && (outerTypeInstance->IsIncomplete()))
  3958. module->PopulateType(outerTypeInstance, BfPopulateType_Full);
  3959. }
  3960. }
  3961. SetAndRestoreValue<BfTypeInstance*> prevCurTypeInstance(module->mCurTypeInstance, outerTypeInstance);
  3962. auto autoComplete = mResolvePassData->mAutoComplete;
  3963. if (autoComplete->IsAutocompleteNode(tempTypeDef->mTypeDeclaration->mNameNode))
  3964. {
  3965. BfIdentifierNode* nameNode = tempTypeDef->mTypeDeclaration->mNameNode;
  3966. autoComplete->AddTopLevelNamespaces(nameNode);
  3967. autoComplete->AddTopLevelTypes(nameNode);
  3968. autoComplete->mInsertStartIdx = nameNode->GetSrcStart();
  3969. autoComplete->mInsertEndIdx = nameNode->GetSrcEnd();
  3970. }
  3971. }
  3972. // while (actualTypeDef == NULL)
  3973. // {
  3974. // checkT
  3975. // }
  3976. if ((actualTypeDef == NULL) || (actualTypeDef->mTypeDeclaration == NULL))
  3977. {
  3978. GenerateAutocompleteInfo();
  3979. return;
  3980. }
  3981. auto sourceClassifier = mResolvePassData->GetSourceClassifier(tempTypeDef->mTypeDeclaration);
  3982. if (tempTypeDef->mTypeCode == BfTypeCode_Extension)
  3983. sourceClassifier->SetElementType(tempTypeDef->mTypeDeclaration->mNameNode, actualTypeDef->mTypeCode);
  3984. if (tempTypeDef->mTypeDeclaration->mAttributes != NULL)
  3985. sourceClassifier->VisitChild(tempTypeDef->mTypeDeclaration->mAttributes);
  3986. BfTypeInstance* typeInst;
  3987. {
  3988. BP_ZONE("ProcessAutocompleteTempType.ResolveTypeDef");
  3989. typeInst = (BfTypeInstance*)module->ResolveTypeDef(actualTypeDef, BfPopulateType_IdentityNoRemapAlias);
  3990. if ((typeInst != NULL) && (typeInst->IsIncomplete()))
  3991. module->PopulateType(typeInst, BfPopulateType_Full);
  3992. }
  3993. if (typeInst == NULL)
  3994. {
  3995. return;
  3996. }
  3997. BF_ASSERT((typeInst->mSize != -1) || (typeInst->IsTypeAlias()));
  3998. #ifdef _DEBUG
  3999. if ((typeInst->mModule != NULL) && (!typeInst->mModule->mIsScratchModule))
  4000. mLastAutocompleteModule = typeInst->mModule;
  4001. #endif
  4002. SetAndRestoreValue<BfTypeInstance*> prevType(module->mCurTypeInstance, typeInst);
  4003. typeState.mType = typeInst;
  4004. BfGenericExtensionEntry* genericExEntry = NULL;
  4005. bool hadTempExtensionInfo = false;
  4006. if ((tempTypeDef->IsExtension()) && (actualTypeDef->mIsCombinedPartial) && (typeInst->IsGenericTypeInstance()))
  4007. {
  4008. // Add to our extension info map and then take it out at the end...
  4009. auto genericTypeInst = (BfTypeInstance*)typeInst;
  4010. module->BuildGenericExtensionInfo(genericTypeInst, tempTypeDef);
  4011. genericTypeInst->mGenericTypeInfo->mGenericExtensionInfo->mExtensionMap.TryGetValue(tempTypeDef, &genericExEntry);
  4012. BF_ASSERT(genericExEntry != NULL);
  4013. hadTempExtensionInfo = true;
  4014. }
  4015. if ((typeInst->IsUnspecializedType()) || (!typeInst->IsGenericTypeInstance()))
  4016. {
  4017. auto autoComplete = mResolvePassData->mAutoComplete;
  4018. BfAstNode* nameNode = tempTypeDef->mTypeDeclaration->mNameNode;
  4019. BfAstNode* refNode = nameNode;
  4020. if (refNode == NULL)
  4021. refNode = tempTypeDef->mTypeDeclaration->mStaticSpecifier;
  4022. if (autoComplete->IsAutocompleteNode(refNode))
  4023. {
  4024. if ((actualTypeDef->mIsCombinedPartial) && (tempTypeDef->mTypeCode == BfTypeCode_Extension))
  4025. {
  4026. autoComplete->AddTopLevelNamespaces(tempTypeDef->mTypeDeclaration->mNameNode);
  4027. autoComplete->AddTopLevelTypes(tempTypeDef->mTypeDeclaration->mNameNode);
  4028. autoComplete->SetDefinitionLocation(actualTypeDef->mTypeDeclaration->mNameNode);
  4029. }
  4030. else
  4031. autoComplete->SetDefinitionLocation(refNode);
  4032. autoComplete->mDefType = actualTypeDef;
  4033. if (nameNode != NULL)
  4034. {
  4035. autoComplete->mInsertStartIdx = nameNode->GetSrcStart();
  4036. autoComplete->mInsertEndIdx = nameNode->GetSrcEnd();
  4037. }
  4038. if (autoComplete->mResolveType == BfResolveType_GetResultString)
  4039. {
  4040. autoComplete->mResultString = ":";
  4041. autoComplete->mResultString += module->TypeToString(typeInst, (BfTypeNameFlags)(BfTypeNameFlag_ExtendedInfo | BfTypeNameFlag_ResolveGenericParamNames));
  4042. }
  4043. }
  4044. }
  4045. autoComplete->CheckInterfaceFixit(typeInst, tempTypeDef->mTypeDeclaration->mNameNode);
  4046. if (tempTypeDef->mTypeCode == BfTypeCode_TypeAlias)
  4047. {
  4048. auto typeAliasDecl = (BfTypeAliasDeclaration*)tempTypeDef->mTypeDeclaration;
  4049. if (typeAliasDecl->mAliasToType != NULL)
  4050. {
  4051. autoComplete->CheckTypeRef(typeAliasDecl->mAliasToType, false);
  4052. module->ResolveTypeRef(typeAliasDecl->mAliasToType);
  4053. }
  4054. }
  4055. // Save and restore mFieldResolveReentrys, we could fire off autocomplete while resolving a field
  4056. SetAndRestoreValue<decltype (module->mContext->mFieldResolveReentrys)> prevTypeResolveReentry(module->mContext->mFieldResolveReentrys);
  4057. module->mContext->mFieldResolveReentrys.Clear();
  4058. if (tempTypeDef->mTypeDeclaration->mAttributes != NULL)
  4059. {
  4060. auto customAttrs = module->GetCustomAttributes(tempTypeDef);
  4061. delete customAttrs;
  4062. }
  4063. for (int genericParamIdx = 0; genericParamIdx < (int)tempTypeDef->mGenericParamDefs.size(); genericParamIdx++)
  4064. {
  4065. auto genericParamDef = tempTypeDef->mGenericParamDefs[genericParamIdx];
  4066. auto genericParamInstance = new BfGenericTypeParamInstance(tempTypeDef, genericParamIdx);
  4067. genericParamInstance->mExternType = module->GetGenericParamType(BfGenericParamKind_Type, genericParamIdx);
  4068. module->ResolveGenericParamConstraints(genericParamInstance, true);
  4069. delete genericParamInstance;
  4070. for (auto nameNode : genericParamDef->mNameNodes)
  4071. module->HandleTypeGenericParamRef(nameNode, tempTypeDef, genericParamIdx);
  4072. }
  4073. //TODO: Do extern constraint stuff here
  4074. for (auto fieldDef : tempTypeDef->mFields)
  4075. {
  4076. BP_ZONE("ProcessAutocompleteTempType.CheckField");
  4077. if (BfNodeIsA<BfPropertyDeclaration>(fieldDef->mFieldDeclaration))
  4078. continue; // Don't process auto-generated property fields
  4079. if (fieldDef->mTypeRef != NULL)
  4080. {
  4081. BfResolveTypeRefFlags flags = BfResolveTypeRefFlag_None;
  4082. if (fieldDef->GetInitializer() != NULL)
  4083. flags = (BfResolveTypeRefFlags)(flags | BfResolveTypeRefFlag_AllowInferredSizedArray);
  4084. if ((!BfNodeIsA<BfVarTypeReference>(fieldDef->mTypeRef)) &&
  4085. (!BfNodeIsA<BfLetTypeReference>(fieldDef->mTypeRef)))
  4086. module->ResolveTypeRef(fieldDef->mTypeRef, BfPopulateType_Identity, flags);
  4087. }
  4088. mResolvePassData->mAutoComplete->CheckTypeRef(fieldDef->mTypeRef, true);
  4089. actualTypeDef->PopulateMemberSets();
  4090. BfFieldDef* actualFieldDef = NULL;
  4091. BfMemberSetEntry* memberSetEntry = NULL;
  4092. if (actualTypeDef->mFieldSet.TryGetWith((StringImpl&)fieldDef->mName, &memberSetEntry))
  4093. {
  4094. auto checkFieldDef = (BfFieldDef*)memberSetEntry->mMemberDef;
  4095. if ((checkFieldDef->mIsConst == fieldDef->mIsConst) &&
  4096. (checkFieldDef->mIsStatic == fieldDef->mIsStatic))
  4097. {
  4098. actualFieldDef = checkFieldDef;
  4099. }
  4100. }
  4101. if (actualFieldDef != NULL)
  4102. {
  4103. auto fieldInstance = &typeInst->mFieldInstances[actualFieldDef->mIdx];
  4104. autoComplete->CheckVarResolution(fieldDef->mTypeRef, fieldInstance->mResolvedType);
  4105. }
  4106. auto nameNode = fieldDef->GetNameNode();
  4107. if (((autoComplete->mIsGetDefinition) || (autoComplete->mResolveType == BfResolveType_GetResultString)) &&
  4108. (fieldDef->mFieldDeclaration != NULL) && (autoComplete->IsAutocompleteNode(nameNode)))
  4109. {
  4110. for (int i = 0; i < (int)actualTypeDef->mFields.size(); i++)
  4111. {
  4112. auto actualFieldDef = actualTypeDef->mFields[i];
  4113. if (actualFieldDef->mName == fieldDef->mName)
  4114. {
  4115. if (autoComplete->mIsGetDefinition)
  4116. {
  4117. autoComplete->mDefType = actualTypeDef;
  4118. autoComplete->mDefField = actualFieldDef;
  4119. autoComplete->SetDefinitionLocation(nameNode);
  4120. autoComplete->mInsertStartIdx = nameNode->GetSrcStart();
  4121. autoComplete->mInsertEndIdx = nameNode->GetSrcEnd();
  4122. }
  4123. else if (autoComplete->mResolveType == BfResolveType_GetResultString)
  4124. {
  4125. auto fieldInstance = &typeInst->mFieldInstances[actualFieldDef->mIdx];
  4126. if (fieldInstance->mConstIdx != -1)
  4127. {
  4128. auto constant = typeInst->mConstHolder->GetConstantById(fieldInstance->mConstIdx);
  4129. auto retVal = module->ConstantToCurrent(constant, typeInst->mConstHolder, fieldInstance->mResolvedType);
  4130. BfTypedValue typedValue = BfTypedValue(retVal, fieldInstance->mResolvedType);
  4131. autoComplete->CheckResult(fieldDef->GetRefNode(), typedValue);
  4132. }
  4133. }
  4134. break;
  4135. }
  4136. }
  4137. }
  4138. BfFieldDeclaration* fieldDecl = fieldDef->GetFieldDeclaration();
  4139. if ((fieldDecl != NULL) && (fieldDecl->mAttributes != NULL))
  4140. {
  4141. auto customAttrs = module->GetCustomAttributes(fieldDecl->mAttributes, fieldDef->mIsStatic ? BfAttributeTargets_StaticField : BfAttributeTargets_Field);
  4142. delete customAttrs;
  4143. }
  4144. if (fieldDef->mIsConst)
  4145. {
  4146. module->ResolveConstField(typeInst, NULL, fieldDef);
  4147. }
  4148. if (fieldDef->GetInitializer() == NULL)
  4149. {
  4150. if (BfNodeIsA<BfVarTypeReference>(fieldDef->mTypeRef))
  4151. {
  4152. if ((fieldDef->mTypeRef->IsA<BfVarTypeReference>()) || (fieldDef->mTypeRef->IsA<BfLetTypeReference>()))
  4153. mPassInstance->Fail("Implicitly-typed fields must be initialized", fieldDef->GetRefNode());
  4154. }
  4155. }
  4156. }
  4157. auto checkTypeDef = tempTypeDef;
  4158. while (checkTypeDef != NULL)
  4159. {
  4160. for (auto baseType : checkTypeDef->mBaseTypes)
  4161. {
  4162. autoComplete->CheckTypeRef(BfNodeDynCast<BfTypeReference>(baseType), false);
  4163. module->ResolveTypeRef_Ref(baseType, BfPopulateType_Identity);
  4164. }
  4165. checkTypeDef = checkTypeDef->mOuterType;
  4166. }
  4167. for (auto propDef : tempTypeDef->mProperties)
  4168. {
  4169. auto fieldDecl = propDef->GetFieldDeclaration();
  4170. if ((fieldDecl != NULL) && (fieldDecl->mAttributes != NULL))
  4171. {
  4172. BfAttributeTargets target = BfAttributeTargets_Property;
  4173. if (propDef->IsExpressionBodied())
  4174. target = (BfAttributeTargets)(target | BfAttributeTargets_Method);
  4175. auto customAttrs = module->GetCustomAttributes(fieldDecl->mAttributes, target);
  4176. delete customAttrs;
  4177. }
  4178. auto propDeclaration = BfNodeDynCast<BfPropertyDeclaration>(fieldDecl);
  4179. if (propDeclaration != NULL)
  4180. autoComplete->CheckProperty(propDeclaration);
  4181. if (BfNodeIsA<BfVarTypeReference>(propDef->mTypeRef))
  4182. {
  4183. // This is only valid for ConstEval properties
  4184. }
  4185. else
  4186. module->ResolveTypeRef(propDef->mTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_AllowRef);
  4187. if (auto indexerDeclaration = BfNodeDynCast<BfIndexerDeclaration>(propDef->mFieldDeclaration))
  4188. {
  4189. for (auto paramDecl : indexerDeclaration->mParams)
  4190. {
  4191. module->ResolveTypeRef(paramDecl->mTypeRef, BfPopulateType_Identity);
  4192. }
  4193. }
  4194. if ((autoComplete->mIsGetDefinition) && (fieldDecl != NULL) && (autoComplete->IsAutocompleteNode(fieldDecl->mNameNode)))
  4195. {
  4196. auto checkType = typeInst;
  4197. while (checkType != NULL)
  4198. {
  4199. for (auto checkProp : checkType->mTypeDef->mProperties)
  4200. {
  4201. if (checkProp->mName == propDef->mName)
  4202. {
  4203. auto checkPropDeclaration = BfNodeDynCast<BfPropertyDeclaration>(checkProp->mFieldDeclaration);
  4204. if ((checkPropDeclaration->mVirtualSpecifier == NULL) || (checkPropDeclaration->mVirtualSpecifier->GetToken() == BfToken_Virtual))
  4205. {
  4206. autoComplete->SetDefinitionLocation(checkPropDeclaration->mNameNode);
  4207. autoComplete->mDefType = checkType->mTypeDef;
  4208. autoComplete->mDefProp = checkProp;
  4209. checkType = NULL;
  4210. break;
  4211. }
  4212. }
  4213. }
  4214. if (checkType != NULL)
  4215. checkType = checkType->mBaseType;
  4216. }
  4217. }
  4218. }
  4219. Array<BfMethodInstance*> methodInstances;
  4220. if (tempTypeDef->IsExtension())
  4221. {
  4222. BfDefBuilder::AddMethod(tempTypeDef, BfMethodType_CtorNoBody, BfProtection_Public, true, "", false);
  4223. BfDefBuilder::AddMethod(tempTypeDef, BfMethodType_CtorNoBody, BfProtection_Public, false, "", false);
  4224. }
  4225. for (auto methodDef : tempTypeDef->mMethods)
  4226. {
  4227. auto methodDeclaration = methodDef->GetMethodDeclaration();
  4228. if (methodDeclaration != NULL)
  4229. autoComplete->CheckMethod(methodDeclaration, false);
  4230. if (!methodDef->mWantsBody)
  4231. {
  4232. if (methodDeclaration != NULL)
  4233. {
  4234. if (methodDeclaration->mAttributes != NULL)
  4235. {
  4236. auto customAttrs = module->GetCustomAttributes(methodDeclaration->mAttributes, (methodDef->mMethodType == BfMethodType_Ctor) ? BfAttributeTargets_Constructor : BfAttributeTargets_Method);
  4237. delete customAttrs;
  4238. }
  4239. }
  4240. else if (auto methodPropertyDeclaration = methodDef->GetPropertyMethodDeclaration())
  4241. {
  4242. if (methodPropertyDeclaration->mAttributes != NULL)
  4243. {
  4244. auto customAttrs = module->GetCustomAttributes(methodPropertyDeclaration->mAttributes, BfAttributeTargets_Method);
  4245. delete customAttrs;
  4246. }
  4247. }
  4248. continue;
  4249. }
  4250. BP_ZONE("ProcessAutocompleteTempType.CheckMethod");
  4251. BfMethodInstanceGroup methodInstanceGroup;
  4252. methodInstanceGroup.mOwner = typeInst;
  4253. methodInstanceGroup.mOnDemandKind = BfMethodOnDemandKind_AlwaysInclude;
  4254. BfMethodInstance* methodInstance = new BfMethodInstance();
  4255. methodInstances.push_back(methodInstance);
  4256. methodInstance->mMethodDef = methodDef;
  4257. methodInstance->mMethodInstanceGroup = &methodInstanceGroup;
  4258. methodInstance->mIsAutocompleteMethod = true;
  4259. methodInstanceGroup.mDefault = methodInstance;
  4260. defer(methodInstanceGroup.mDefault = NULL);
  4261. for (int genericParamIdx = 0; genericParamIdx < (int)methodDef->mGenericParams.size(); genericParamIdx++)
  4262. {
  4263. auto genericParamType = module->GetGenericParamType(BfGenericParamKind_Method, genericParamIdx);
  4264. methodInstance->GetMethodInfoEx()->mMethodGenericArguments.push_back(genericParamType);
  4265. auto genericParamInstance = new BfGenericMethodParamInstance(methodDef, genericParamIdx);
  4266. methodInstance->GetMethodInfoEx()->mGenericParams.push_back(genericParamInstance);
  4267. }
  4268. for (int externConstraintIdx = 0; externConstraintIdx < (int)methodDef->mExternalConstraints.size(); externConstraintIdx++)
  4269. {
  4270. auto genericParamInstance = new BfGenericMethodParamInstance(methodDef, externConstraintIdx + (int)methodDef->mGenericParams.size());
  4271. methodInstance->GetMethodInfoEx()->mGenericParams.push_back(genericParamInstance);
  4272. }
  4273. bool wantsProcess = !actualTypeDef->mIsFunction;
  4274. SetAndRestoreValue<BfFilePosition> prevFilePos(module->mCurFilePosition);
  4275. SetAndRestoreValue<BfMethodInstance*> prevMethodInst(module->mCurMethodInstance, methodInstance);
  4276. module->DoMethodDeclaration(methodDeclaration, true, wantsProcess);
  4277. if (wantsProcess)
  4278. {
  4279. module->mIncompleteMethodCount++;
  4280. module->ProcessMethod(methodInstance);
  4281. }
  4282. if (methodInstance->mIRFunction)
  4283. {
  4284. BfLogSysM("Autocomplete removing IRFunction %d\n", methodInstance->mIRFunction.mId);
  4285. module->mBfIRBuilder->Func_DeleteBody(methodInstance->mIRFunction);
  4286. module->mBfIRBuilder->Func_SafeRename(methodInstance->mIRFunction);
  4287. }
  4288. }
  4289. if ((mResolvePassData->mAutoComplete->mDefType != NULL) && (mResolvePassData->mAutoComplete->mDefType->GetDefinition() == actualTypeDef) &&
  4290. (mResolvePassData->mAutoComplete->mDefMethod != NULL))
  4291. {
  4292. BfMethodDef* tempDefMethod = NULL;
  4293. for (auto checkMethod : tempTypeDef->mMethods)
  4294. {
  4295. if (checkMethod == mResolvePassData->mAutoComplete->mDefMethod)
  4296. tempDefMethod = checkMethod;
  4297. }
  4298. if (tempDefMethod != NULL)
  4299. {
  4300. BfMethodDef* actualReplaceMethodDef = NULL;
  4301. for (auto checkMethodDef : actualTypeDef->mMethods)
  4302. {
  4303. if ((checkMethodDef->mMethodType == tempDefMethod->mMethodType) &&
  4304. (checkMethodDef->mMethodDeclaration != NULL) && (tempDefMethod->mMethodDeclaration != NULL) &&
  4305. (checkMethodDef->mMethodDeclaration->GetSrcStart() == tempDefMethod->mMethodDeclaration->GetSrcStart()))
  4306. actualReplaceMethodDef = checkMethodDef;
  4307. }
  4308. if (actualReplaceMethodDef == NULL)
  4309. {
  4310. autoComplete->mReplaceLocalId = -1;
  4311. autoComplete->mDefType = NULL;
  4312. autoComplete->mDefField = NULL;
  4313. autoComplete->mDefMethod = NULL;
  4314. autoComplete->mDefProp = NULL;
  4315. autoComplete->mDefMethodGenericParamIdx = -1;
  4316. autoComplete->mDefTypeGenericParamIdx = -1;
  4317. }
  4318. else
  4319. autoComplete->mDefMethod = actualReplaceMethodDef;
  4320. }
  4321. }
  4322. if (hadTempExtensionInfo)
  4323. {
  4324. auto genericTypeInst = (BfTypeInstance*)typeInst;
  4325. genericTypeInst->mGenericTypeInfo->mGenericExtensionInfo->mExtensionMap.Remove(tempTypeDef);
  4326. }
  4327. for (auto checkNode : mResolvePassData->mExteriorAutocompleteCheckNodes)
  4328. {
  4329. BP_ZONE("ProcessAutocompleteTempType.CheckIdentifier");
  4330. bool isUsingDirective = false;
  4331. BfIdentifierNode* checkIdentifier = NULL;
  4332. if (auto usingDirective = BfNodeDynCast<BfUsingDirective>(checkNode))
  4333. {
  4334. isUsingDirective = true;
  4335. checkIdentifier = usingDirective->mNamespace;
  4336. }
  4337. else
  4338. checkIdentifier = BfNodeDynCast<BfIdentifierNode>(checkNode);
  4339. mResolvePassData->mAutoComplete->CheckIdentifier(checkIdentifier, false, isUsingDirective);
  4340. }
  4341. GenerateAutocompleteInfo();
  4342. for (auto methodInstance : methodInstances)
  4343. delete methodInstance;
  4344. methodInstances.Clear();
  4345. module->CleanupFileInstances();
  4346. prevTypeInstance.Restore();
  4347. if (module->mCurTypeInstance == NULL)
  4348. module->ClearConstData();
  4349. BfLogSysM("ProcessAutocompleteTempType end\n");
  4350. }
  4351. BfType* BfCompiler::CheckSymbolReferenceTypeRef(BfModule* module, BfTypeReference* typeRef)
  4352. {
  4353. if (typeRef == NULL)
  4354. return NULL;
  4355. //auto resolvedType = module->ResolveTypeRef(typeRef, BfPopulateType_Declaration,
  4356. //(BfResolveTypeRefFlags)(BfResolveTypeRefFlag_AllowRef | BfResolveTypeRefFlag_AllowGenericMethodParamConstValue | BfResolveTypeRefFlag_AllowGenericTypeParamConstValue));
  4357. auto resolvedType = module->ResolveTypeRef(typeRef, BfPopulateType_Declaration, BfResolveTypeRefFlag_AllowRef);
  4358. if ((resolvedType != NULL) && (resolvedType->IsTypeInstance()))
  4359. {
  4360. auto typeInst = resolvedType->ToTypeInstance();
  4361. //TODO: Did we need this?
  4362. // The ResolveTypeRef call already does mResolvePassData->HandleTypeReference, so we were adding double entries
  4363. //mResolvePassData->HandleTypeReference(typeRef, typeInst->mTypeDef);
  4364. }
  4365. return resolvedType;
  4366. }
  4367. void BfCompiler::AddToRebuildTypeList(BfTypeInstance* typeInst, HashSet<BfTypeInstance*>& rebuildTypeInstList)
  4368. {
  4369. if (!mResolvePassData->mParsers.IsEmpty())
  4370. {
  4371. bool found = false;
  4372. for (auto parser : mResolvePassData->mParsers)
  4373. {
  4374. // Only find references within the current file
  4375. if (typeInst->mTypeDef->GetDefinition()->HasSource(parser))
  4376. found = true;
  4377. }
  4378. if (!found)
  4379. return;
  4380. }
  4381. bool allowRebuild = ((!typeInst->IsGenericTypeInstance()) ||
  4382. ((typeInst->IsUnspecializedType()) && (!typeInst->IsUnspecializedTypeVariation())));
  4383. if ((typeInst->IsClosure()) || (typeInst->IsConcreteInterfaceType()) || (typeInst->IsModifiedTypeType()))
  4384. allowRebuild = false;
  4385. if (allowRebuild)
  4386. rebuildTypeInstList.Add(typeInst);
  4387. }
  4388. void BfCompiler::AddDepsToRebuildTypeList(BfTypeInstance* replaceTypeInst, HashSet<BfTypeInstance*>& rebuildTypeInstList)
  4389. {
  4390. for (auto& dep : replaceTypeInst->mDependencyMap)
  4391. {
  4392. auto depType = dep.mKey;
  4393. auto depTypeInst = depType->ToTypeInstance();
  4394. if (depTypeInst == NULL)
  4395. continue;
  4396. AddToRebuildTypeList(depTypeInst, rebuildTypeInstList);
  4397. }
  4398. }
  4399. void BfCompiler::GetSymbolReferences()
  4400. {
  4401. BfLogSysM("GetSymbolReferences\n");
  4402. if (mInInvalidState)
  4403. return; // Don't even try
  4404. auto context = mContext;
  4405. if (context->mBfObjectType == NULL)
  4406. return; // Not initialized yet
  4407. auto module = context->mScratchModule;
  4408. if (mResolvePassData->mAutoComplete != NULL)
  4409. mResolvePassData->mAutoComplete->SetModule(module);
  4410. BfTypeDef* typeDef = NULL;
  4411. HashSet<BfTypeInstance*> rebuildTypeInstList;
  4412. if (!mResolvePassData->mQueuedSymbolReferenceNamespace.IsEmpty())
  4413. {
  4414. if (!mSystem->ParseAtomComposite(mResolvePassData->mQueuedSymbolReferenceNamespace, mResolvePassData->mSymbolReferenceNamespace))
  4415. return;
  4416. for (auto type : mContext->mResolvedTypes)
  4417. {
  4418. auto typeInst = type->ToTypeInstance();
  4419. if (typeInst == NULL)
  4420. continue;
  4421. for (auto& lookupKV : typeInst->mLookupResults)
  4422. {
  4423. auto typeDef = lookupKV.mValue.mTypeDef;
  4424. if ((typeDef != NULL) && (typeDef->mNamespace.StartsWith(mResolvePassData->mSymbolReferenceNamespace)))
  4425. {
  4426. AddToRebuildTypeList(typeInst, rebuildTypeInstList);
  4427. }
  4428. }
  4429. }
  4430. for (auto parser : mSystem->mParsers)
  4431. {
  4432. BfNamespaceVisitor namespaceVisitor;
  4433. namespaceVisitor.mResolvePassData = mResolvePassData;
  4434. namespaceVisitor.mSystem = mSystem;
  4435. namespaceVisitor.Visit(parser->mRootNode);
  4436. }
  4437. }
  4438. else
  4439. {
  4440. const char* strPtr = mResolvePassData->mQueuedReplaceTypeDef.c_str();
  4441. typeDef = mSystem->FindTypeDefEx(strPtr);
  4442. if ((typeDef == NULL) || (typeDef->mTypeDeclaration == NULL))
  4443. return;
  4444. typeDef = typeDef->GetLatest();
  4445. mResolvePassData->mSymbolReferenceTypeDef = typeDef;
  4446. auto replaceType = module->ResolveTypeDef(typeDef, BfPopulateType_IdentityNoRemapAlias);
  4447. module->PopulateType(replaceType);
  4448. auto replaceTypeInst = replaceType->ToTypeInstance();
  4449. if (mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Local)
  4450. {
  4451. AddDepsToRebuildTypeList(replaceTypeInst, rebuildTypeInstList);
  4452. // For generic types, add all references from all specialized versions
  4453. if (replaceTypeInst->IsGenericTypeInstance())
  4454. {
  4455. for (auto type : mContext->mResolvedTypes)
  4456. {
  4457. auto typeInst = type->ToTypeInstance();
  4458. if ((typeInst != replaceTypeInst) && (typeInst != NULL) && (typeInst->mTypeDef->GetLatest() == typeDef))
  4459. AddDepsToRebuildTypeList(typeInst, rebuildTypeInstList);
  4460. }
  4461. }
  4462. }
  4463. AddToRebuildTypeList(replaceTypeInst, rebuildTypeInstList);
  4464. }
  4465. //TODO: Did we need this to be rebuildTypeInst->mModule??? Why?
  4466. //auto rebuildModule = rebuildTypeInst->mModule;
  4467. auto rebuildModule = context->mScratchModule;
  4468. auto _CheckAttributes = [&](BfAttributeDirective* attrib, BfTypeDef* declaringType)
  4469. {
  4470. if ((mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Type) &&
  4471. (mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Field) &&
  4472. (mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Property))
  4473. return;
  4474. while (attrib != NULL)
  4475. {
  4476. if (attrib->mAttributeTypeRef != NULL)
  4477. {
  4478. auto attrType = module->ResolveTypeRef(attrib->mAttributeTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_Attribute);
  4479. BfTypeDef* attrTypeDef = NULL;
  4480. if ((attrType != NULL) && (attrType->IsTypeInstance()))
  4481. attrTypeDef = attrType->ToTypeInstance()->mTypeDef;
  4482. if (attrTypeDef != NULL)
  4483. {
  4484. mResolvePassData->HandleTypeReference(attrib->mAttributeTypeRef, attrTypeDef);
  4485. attrTypeDef->PopulateMemberSets();
  4486. for (auto argExpr : attrib->mArguments)
  4487. {
  4488. if (auto assignExpr = BfNodeDynCast<BfAssignmentExpression>(argExpr))
  4489. {
  4490. auto propName = assignExpr->mLeft->ToString();
  4491. BfMemberSetEntry* propDefEntry;
  4492. if (attrTypeDef->mPropertySet.TryGetWith(propName, &propDefEntry))
  4493. {
  4494. mResolvePassData->HandlePropertyReference(assignExpr->mLeft, attrTypeDef, (BfPropertyDef*)propDefEntry->mMemberDef);
  4495. }
  4496. else if (attrTypeDef->mFieldSet.TryGetWith(propName, &propDefEntry))
  4497. {
  4498. mResolvePassData->HandleFieldReference(assignExpr->mLeft, attrTypeDef, (BfFieldDef*)propDefEntry->mMemberDef);
  4499. }
  4500. }
  4501. }
  4502. }
  4503. }
  4504. attrib = attrib->mNextAttribute;
  4505. }
  4506. };
  4507. for (auto rebuildTypeInst : rebuildTypeInstList)
  4508. {
  4509. // These never have a definition. Also note that BfGenericDelegateType does not have proper generic defs
  4510. if (rebuildTypeInst->IsOnDemand())
  4511. continue;
  4512. auto context = mContext;
  4513. auto module = context->mScratchModule;
  4514. SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(module->mCurTypeInstance, rebuildTypeInst);
  4515. SetAndRestoreValue<bool> prevIgnoreErrors(module->mIgnoreErrors, true);
  4516. // Run through base types for type renames
  4517. auto typeDef = rebuildTypeInst->mTypeDef;
  4518. if ((typeDef->mTypeDeclaration != NULL) && (typeDef->mTypeDeclaration->mNameNode != NULL))
  4519. {
  4520. if (typeDef->mIsCombinedPartial)
  4521. {
  4522. for (auto checkTypeDef : typeDef->mPartials)
  4523. {
  4524. auto nameNode = checkTypeDef->mTypeDeclaration->mNameNode;
  4525. for (auto parser : mResolvePassData->mParsers)
  4526. {
  4527. if (nameNode->IsFromParser(parser))
  4528. {
  4529. mResolvePassData->HandleTypeReference(nameNode, typeDef);
  4530. break;
  4531. }
  4532. }
  4533. if (mResolvePassData->mParsers.IsEmpty())
  4534. mResolvePassData->HandleTypeReference(nameNode, typeDef);
  4535. if (checkTypeDef->IsExtension())
  4536. {
  4537. if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Type)
  4538. {
  4539. BfTypeState typeState;
  4540. typeState.mCurTypeDef = checkTypeDef;
  4541. SetAndRestoreValue<BfTypeState*> prevTypeState(module->mContext->mCurTypeState, &typeState);
  4542. for (auto baseTypeRef : checkTypeDef->mBaseTypes)
  4543. CheckSymbolReferenceTypeRef(module, BfNodeDynCast<BfTypeReference>(baseTypeRef));
  4544. for (auto genericParam : checkTypeDef->mGenericParamDefs)
  4545. {
  4546. for (auto constraint : genericParam->mConstraints)
  4547. {
  4548. if (auto constraintTypeRef = BfNodeDynCast<BfTypeReference>(constraint))
  4549. {
  4550. module->ResolveTypeRef(constraintTypeRef, BfPopulateType_Identity);
  4551. }
  4552. else if (auto opConstraint = BfNodeDynCast<BfGenericOperatorConstraint>(constraint))
  4553. {
  4554. module->ResolveTypeRef(opConstraint->mLeftType, BfPopulateType_Identity);
  4555. module->ResolveTypeRef(opConstraint->mRightType, BfPopulateType_Identity);
  4556. }
  4557. }
  4558. }
  4559. }
  4560. }
  4561. }
  4562. }
  4563. else
  4564. {
  4565. mResolvePassData->HandleTypeReference(typeDef->mTypeDeclaration->mNameNode, typeDef);
  4566. }
  4567. }
  4568. if (!typeDef->mPartials.IsEmpty())
  4569. {
  4570. for (auto partialDef : typeDef->mPartials)
  4571. {
  4572. if ((partialDef->mTypeDeclaration != NULL) && (partialDef->mTypeDeclaration->mAttributes != NULL))
  4573. _CheckAttributes(partialDef->mTypeDeclaration->mAttributes, typeDef);
  4574. }
  4575. }
  4576. else
  4577. {
  4578. if ((typeDef->mTypeDeclaration != NULL) && (typeDef->mTypeDeclaration->mAttributes != NULL))
  4579. _CheckAttributes(typeDef->mTypeDeclaration->mAttributes, typeDef);
  4580. }
  4581. if (auto typeAliasDeclaration = BfNodeDynCast<BfTypeAliasDeclaration>(typeDef->mTypeDeclaration))
  4582. {
  4583. CheckSymbolReferenceTypeRef(module, typeAliasDeclaration->mAliasToType);
  4584. }
  4585. if (mResolvePassData != NULL)
  4586. {
  4587. if (rebuildTypeInst->IsGenericTypeInstance())
  4588. {
  4589. auto genericTypeInstance = (BfTypeInstance*)rebuildTypeInst;
  4590. for (int genericParamIdx = 0; genericParamIdx < (int)genericTypeInstance->mGenericTypeInfo->mTypeGenericArguments.size(); genericParamIdx++)
  4591. {
  4592. BfGenericTypeParamInstance genericParamInstance(genericTypeInstance->mTypeDef, genericParamIdx);
  4593. auto genericParamDef = typeDef->mGenericParamDefs[genericParamIdx];
  4594. if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_TypeGenericParam)
  4595. {
  4596. for (auto nameNode : genericParamDef->mNameNodes)
  4597. if (nameNode != NULL)
  4598. mResolvePassData->HandleTypeGenericParam(nameNode, typeDef, genericParamIdx);
  4599. }
  4600. rebuildModule->ResolveGenericParamConstraints(&genericParamInstance, genericTypeInstance->IsGenericTypeInstance());
  4601. }
  4602. }
  4603. }
  4604. if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Type)
  4605. {
  4606. for (auto baseTypeRef : typeDef->mBaseTypes)
  4607. CheckSymbolReferenceTypeRef(module, BfNodeDynCast<BfTypeReference>(baseTypeRef));
  4608. }
  4609. BfTypeState typeState;
  4610. SetAndRestoreValue<BfTypeState*> prevTypeState(module->mContext->mCurTypeState, &typeState);
  4611. if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Property)
  4612. {
  4613. for (auto propDef : typeDef->mProperties)
  4614. {
  4615. BfPropertyDef* checkPropDef = propDef;
  4616. BfTypeInstance* checkTypeInst = rebuildTypeInst;
  4617. typeState.mCurTypeDef = propDef->mDeclaringType;
  4618. module->GetBasePropertyDef(checkPropDef, checkTypeInst);
  4619. if (auto fieldDecl = propDef->GetFieldDeclaration())
  4620. if (fieldDecl->mNameNode != NULL)
  4621. mResolvePassData->HandlePropertyReference(fieldDecl->mNameNode, checkTypeInst->mTypeDef, checkPropDef);
  4622. }
  4623. }
  4624. if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Field)
  4625. {
  4626. for (auto fieldDef : typeDef->mFields)
  4627. {
  4628. if (auto nameNode = fieldDef->GetNameNode())
  4629. {
  4630. typeState.mCurTypeDef = fieldDef->mDeclaringType;
  4631. mResolvePassData->HandleFieldReference(nameNode, typeDef, fieldDef);
  4632. }
  4633. }
  4634. }
  4635. for (auto& fieldInst : rebuildTypeInst->mFieldInstances)
  4636. {
  4637. auto fieldDef = fieldInst.GetFieldDef();
  4638. if (fieldDef != NULL)
  4639. {
  4640. typeState.mCurTypeDef = fieldDef->mDeclaringType;
  4641. if (fieldDef->mTypeRef != NULL)
  4642. CheckSymbolReferenceTypeRef(module, fieldDef->mTypeRef);
  4643. if ((fieldDef->mIsConst) && (fieldDef->GetInitializer() != NULL))
  4644. {
  4645. BfMethodState methodState;
  4646. methodState.mTempKind = BfMethodState::TempKind_Static;
  4647. SetAndRestoreValue<BfMethodState*> prevMethodState(module->mCurMethodState, &methodState);
  4648. BfConstResolver constResolver(module);
  4649. constResolver.Resolve(fieldDef->GetInitializer());
  4650. }
  4651. if (auto fieldDecl = fieldDef->GetFieldDeclaration())
  4652. {
  4653. if (fieldDecl->mAttributes != NULL)
  4654. _CheckAttributes(fieldDecl->mAttributes, fieldDef->mDeclaringType);
  4655. }
  4656. }
  4657. }
  4658. for (auto& propDef : rebuildTypeInst->mTypeDef->mProperties)
  4659. {
  4660. typeState.mCurTypeDef = propDef->mDeclaringType;
  4661. if (propDef->mTypeRef != NULL)
  4662. CheckSymbolReferenceTypeRef(module, propDef->mTypeRef);
  4663. }
  4664. if (rebuildModule == NULL)
  4665. continue;
  4666. rebuildModule->EnsureIRBuilder();
  4667. SetAndRestoreValue<BfTypeInstance*> prevTypeInstance2(rebuildModule->mCurTypeInstance, rebuildTypeInst);
  4668. for (auto& methodInstGroup : rebuildTypeInst->mMethodInstanceGroups)
  4669. {
  4670. // Run through all methods
  4671. bool isDefault = true;
  4672. BfMethodInstanceGroup::MapType::iterator methodItr;
  4673. if (methodInstGroup.mMethodSpecializationMap != NULL)
  4674. methodItr = methodInstGroup.mMethodSpecializationMap->begin();
  4675. while (true)
  4676. {
  4677. BfMethodInstance* rebuildMethodInstance;
  4678. if (isDefault)
  4679. {
  4680. rebuildMethodInstance = methodInstGroup.mDefault;
  4681. if (rebuildMethodInstance == NULL)
  4682. break;
  4683. isDefault = false;
  4684. }
  4685. else
  4686. {
  4687. //TODO: Why did we process specialized methods?
  4688. // This caused renaming of types picking up 'T' usage from generic methods
  4689. break;
  4690. // if (methodInstGroup.mMethodSpecializationMap == NULL)
  4691. // break;
  4692. // if (methodItr == methodInstGroup.mMethodSpecializationMap->end())
  4693. // break;
  4694. // rebuildMethodInstance = methodItr->mValue;
  4695. // ++methodItr;
  4696. }
  4697. if ((rebuildMethodInstance->IsOrInUnspecializedVariation()) || (rebuildMethodInstance->IsSpecializedGenericMethod()))
  4698. continue;
  4699. SetAndRestoreValue<BfMethodInstance*> prevTypeInstance(rebuildModule->mCurMethodInstance, rebuildMethodInstance);
  4700. auto methodDef = rebuildMethodInstance->mMethodDef;
  4701. auto methodDeclaration = methodDef->GetMethodDeclaration();
  4702. typeState.mCurTypeDef = methodDef->mDeclaringType;
  4703. if ((methodDeclaration != NULL) && (methodDeclaration->mAttributes != NULL))
  4704. _CheckAttributes(methodDeclaration->mAttributes, methodDef->mDeclaringType);
  4705. if ((mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Type) ||
  4706. (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_MethodGenericParam) ||
  4707. (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_TypeGenericParam))
  4708. {
  4709. if (methodDef->mExplicitInterface != NULL)
  4710. CheckSymbolReferenceTypeRef(rebuildModule, methodDef->mExplicitInterface);
  4711. for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
  4712. {
  4713. auto param = methodDef->mParams[paramIdx];
  4714. CheckSymbolReferenceTypeRef(rebuildModule, param->mTypeRef);
  4715. }
  4716. if (methodDef->mReturnTypeRef != NULL)
  4717. CheckSymbolReferenceTypeRef(rebuildModule, methodDef->mReturnTypeRef);
  4718. }
  4719. if (rebuildMethodInstance->mIgnoreBody)
  4720. {
  4721. auto methodDeclaration = methodDef->GetMethodDeclaration();
  4722. if (methodDeclaration != NULL)
  4723. mResolvePassData->HandleMethodReference(methodDeclaration->mNameNode, typeDef, methodDef);
  4724. for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
  4725. {
  4726. auto param = methodDef->mParams[paramIdx];
  4727. if (param->mParamDeclaration != NULL)
  4728. {
  4729. if (auto identifierNode = BfNodeDynCast<BfIdentifierNode>(param->mParamDeclaration->mNameNode))
  4730. mResolvePassData->HandleLocalReference(identifierNode, rebuildTypeInst->mTypeDef, rebuildMethodInstance->mMethodDef, paramIdx + 1);
  4731. else if (auto tupleExprNode = BfNodeDynCast<BfTupleExpression>(param->mParamDeclaration->mNameNode))
  4732. {
  4733. for (int fieldIdx = 0; fieldIdx < (int)tupleExprNode->mValues.size(); fieldIdx++)
  4734. {
  4735. if (auto identifierNode = BfNodeDynCast<BfIdentifierNode>(tupleExprNode->mValues[fieldIdx]))
  4736. mResolvePassData->HandleLocalReference(identifierNode, rebuildTypeInst->mTypeDef, rebuildMethodInstance->mMethodDef, paramIdx + 1);
  4737. }
  4738. }
  4739. }
  4740. }
  4741. }
  4742. else
  4743. {
  4744. for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
  4745. {
  4746. auto param = methodDef->mParams[paramIdx];
  4747. if ((param->mParamDeclaration != NULL) && (param->mParamDeclaration->mInitializer != NULL))
  4748. {
  4749. auto paramType = rebuildMethodInstance->GetParamType(paramIdx);
  4750. BfConstResolver constResolver(rebuildModule);
  4751. constResolver.Resolve(param->mParamDeclaration->mInitializer, paramType);
  4752. }
  4753. }
  4754. if (rebuildMethodInstance->mHasBeenProcessed)
  4755. {
  4756. if (rebuildMethodInstance->mIRFunction)
  4757. rebuildModule->mBfIRBuilder->Func_DeleteBody(rebuildMethodInstance->mIRFunction);
  4758. rebuildMethodInstance->mHasBeenProcessed = false;
  4759. rebuildModule->mIncompleteMethodCount++;
  4760. }
  4761. else
  4762. {
  4763. if ((rebuildModule->mIncompleteMethodCount == 0) && (!rebuildModule->mIsScratchModule))
  4764. {
  4765. BF_FATAL("Shouldn't be processing this method");
  4766. }
  4767. }
  4768. for (int genericParamIdx = 0; genericParamIdx < (int)rebuildMethodInstance->GetNumGenericArguments(); genericParamIdx++)
  4769. {
  4770. BfGenericMethodParamInstance genericParamInstance(rebuildMethodInstance->mMethodDef, genericParamIdx);
  4771. auto genericParamDef = methodDef->mGenericParams[genericParamIdx];
  4772. if (mResolvePassData != NULL)
  4773. {
  4774. for (auto nameNode : genericParamDef->mNameNodes)
  4775. if (nameNode != NULL)
  4776. mResolvePassData->HandleMethodGenericParam(nameNode, typeDef, methodDef, genericParamIdx);
  4777. }
  4778. rebuildModule->ResolveGenericParamConstraints(&genericParamInstance, rebuildMethodInstance->mIsUnspecialized);
  4779. }
  4780. for (int externConstraintIdx = 0; externConstraintIdx < (int)methodDef->mExternalConstraints.size(); externConstraintIdx++)
  4781. {
  4782. BfGenericMethodParamInstance genericParamInstance(rebuildMethodInstance->mMethodDef, externConstraintIdx + (int)methodDef->mGenericParams.size());
  4783. auto& externConstraintDef = methodDef->mExternalConstraints[externConstraintIdx];
  4784. CheckSymbolReferenceTypeRef(module, externConstraintDef.mTypeRef);
  4785. rebuildModule->ResolveGenericParamConstraints(&genericParamInstance, rebuildMethodInstance->mIsUnspecialized);
  4786. }
  4787. rebuildModule->ProcessMethod(rebuildMethodInstance);
  4788. }
  4789. }
  4790. }
  4791. }
  4792. }
  4793. void BfCompiler::UpdateCompletion()
  4794. {
  4795. if (mIsResolveOnly)
  4796. return;
  4797. float typeScale = 10.0f;
  4798. float methodScale = 1.0f;
  4799. float queueModuleScale = 50.0f;
  4800. float genModuleScale = 50.0f;
  4801. mCodeGen.UpdateStats();
  4802. BF_ASSERT(mCodeGen.mQueuedCount >= mCodeGen.mCompletionCount);
  4803. BF_ASSERT(mStats.mModulesFinished <= mStats.mModulesStarted);
  4804. BF_ASSERT(mCodeGen.mCompletionCount <= mStats.mModulesStarted);
  4805. float numerator = ((mStats.mQueuedTypesProcessed * typeScale) + //(mStats.mMethodsProcessed * methodScale) +
  4806. (mStats.mModulesFinished * queueModuleScale) + (mCodeGen.mCompletionCount * genModuleScale));
  4807. float divisor = ((mStats.mTypesQueued * typeScale) + //(mStats.mMethodsQueued * methodScale) +
  4808. (mStats.mModulesStarted * queueModuleScale) + (mStats.mReifiedModuleCount * genModuleScale));
  4809. float checkPct = 0;
  4810. if (divisor > 0)
  4811. {
  4812. checkPct = numerator / divisor;
  4813. BF_ASSERT(checkPct >= 0);
  4814. if (checkPct > mCompletionPct)
  4815. mCompletionPct = checkPct;
  4816. }
  4817. else
  4818. mCompletionPct = 0;
  4819. if (!mHadCancel)
  4820. BF_ASSERT(mCompletionPct <= 1.0f);
  4821. if (mCompletionPct > 1.0f)
  4822. mCompletionPct = 1.0f;
  4823. }
  4824. void BfCompiler::MarkStringPool(BfModule* module)
  4825. {
  4826. for (int stringId : module->mStringPoolRefs)
  4827. {
  4828. BfStringPoolEntry& stringPoolEntry = module->mContext->mStringObjectIdMap[stringId];
  4829. stringPoolEntry.mLastUsedRevision = mRevision;
  4830. }
  4831. for (int stringId : module->mUnreifiedStringPoolRefs)
  4832. {
  4833. BfStringPoolEntry& stringPoolEntry = module->mContext->mStringObjectIdMap[stringId];
  4834. stringPoolEntry.mLastUsedRevision = mRevision;
  4835. }
  4836. for (int stringId : module->mImportFileNames)
  4837. {
  4838. BfStringPoolEntry& stringPoolEntry = module->mContext->mStringObjectIdMap[stringId];
  4839. stringPoolEntry.mLastUsedRevision = mRevision;
  4840. }
  4841. /*if (module->mOptModule != NULL)
  4842. MarkStringPool(module->mOptModule);*/
  4843. auto altModule = module->mNextAltModule;
  4844. while (altModule != NULL)
  4845. {
  4846. MarkStringPool(altModule);
  4847. altModule = altModule->mNextAltModule;
  4848. }
  4849. for (auto& specModulePair : module->mSpecializedMethodModules)
  4850. MarkStringPool(specModulePair.mValue);
  4851. }
  4852. void BfCompiler::MarkStringPool(BfIRConstHolder* constHolder, BfIRValue irValue)
  4853. {
  4854. auto constant = constHolder->GetConstant(irValue);
  4855. if ((constant != NULL) && (constant->mTypeCode == BfTypeCode_StringId))
  4856. {
  4857. BfStringPoolEntry& stringPoolEntry = mContext->mStringObjectIdMap[constant->mInt32];
  4858. stringPoolEntry.mLastUsedRevision = mRevision;
  4859. }
  4860. }
  4861. void BfCompiler::ClearUnusedStringPoolEntries()
  4862. {
  4863. BF_ASSERT(!IsHotCompile());
  4864. for (auto module : mContext->mModules)
  4865. {
  4866. MarkStringPool(module);
  4867. }
  4868. for (auto type : mContext->mResolvedTypes)
  4869. {
  4870. auto typeInstance = type->ToTypeInstance();
  4871. if (typeInstance == NULL)
  4872. continue;
  4873. if (typeInstance->mCustomAttributes == NULL)
  4874. continue;
  4875. for (auto& attribute : typeInstance->mCustomAttributes->mAttributes)
  4876. {
  4877. for (auto arg : attribute.mCtorArgs)
  4878. MarkStringPool(typeInstance->mConstHolder, arg);
  4879. for (auto setValue : attribute.mSetProperties)
  4880. MarkStringPool(typeInstance->mConstHolder, setValue.mParam.mValue);
  4881. for (auto setValue : attribute.mSetField)
  4882. MarkStringPool(typeInstance->mConstHolder, setValue.mParam.mValue);
  4883. }
  4884. }
  4885. for (auto itr = mContext->mStringObjectIdMap.begin(); itr != mContext->mStringObjectIdMap.end(); )
  4886. {
  4887. int strId = itr->mKey;
  4888. BfStringPoolEntry& stringPoolEntry = itr->mValue;
  4889. if (stringPoolEntry.mLastUsedRevision != mRevision)
  4890. {
  4891. CompileLog("Clearing unused string: %d %s\n", itr->mKey, stringPoolEntry.mString.c_str());
  4892. mContext->mStringObjectPool.Remove(stringPoolEntry.mString);
  4893. itr = mContext->mStringObjectIdMap.Remove(itr);
  4894. }
  4895. else
  4896. ++itr;
  4897. }
  4898. }
  4899. void BfCompiler::ClearBuildCache()
  4900. {
  4901. mCodeGen.ClearBuildCache();
  4902. for (auto project : mSystem->mProjects)
  4903. {
  4904. String libPath = mOutputDirectory + "/" + project->mName + "/" + project->mName + "__.lib";
  4905. BfpFile_Delete(libPath.c_str(), NULL);
  4906. }
  4907. }
  4908. int BfCompiler::GetVDataPrefixDataCount()
  4909. {
  4910. return (mSystem->mPtrSize == 4) ? 2 : 1;
  4911. }
  4912. int BfCompiler::GetDynCastVDataCount()
  4913. {
  4914. int dynElements = 1 + mMaxInterfaceSlots;
  4915. return ((dynElements * 4) + mSystem->mPtrSize - 1) / mSystem->mPtrSize;
  4916. }
  4917. bool BfCompiler::IsAutocomplete()
  4918. {
  4919. return (mResolvePassData != NULL) && (mResolvePassData->mAutoComplete != NULL);
  4920. }
  4921. bool BfCompiler::IsDataResolvePass()
  4922. {
  4923. return (mResolvePassData != NULL) && (mResolvePassData->mResolveType == BfResolveType_GetResultString);
  4924. }
  4925. bool BfCompiler::WantsClassifyNode(BfAstNode* node)
  4926. {
  4927. return (mResolvePassData != NULL) && (mResolvePassData->GetSourceClassifier(node) != NULL);
  4928. }
  4929. BfAutoComplete* BfCompiler::GetAutoComplete()
  4930. {
  4931. if (mResolvePassData != NULL)
  4932. return mResolvePassData->mAutoComplete;
  4933. return NULL;
  4934. }
  4935. bool BfCompiler::IsHotCompile()
  4936. {
  4937. return mOptions.mHotProject != NULL;
  4938. }
  4939. bool BfCompiler::IsSkippingExtraResolveChecks()
  4940. {
  4941. return mIsResolveOnly && !mOptions.mExtraResolveChecks;
  4942. }
  4943. int BfCompiler::GetVTableMethodOffset()
  4944. {
  4945. if (mOptions.mHasVDataExtender)
  4946. return 1;
  4947. return 0;
  4948. }
  4949. bool BfCompiler::DoWorkLoop(bool onlyReifiedTypes, bool onlyReifiedMethods)
  4950. {
  4951. bool hadAnyWork = false;
  4952. while (true)
  4953. {
  4954. bool didWork = false;
  4955. didWork |= mContext->ProcessWorkList(onlyReifiedTypes, onlyReifiedMethods);
  4956. if (!didWork)
  4957. break;
  4958. hadAnyWork = true;
  4959. }
  4960. return hadAnyWork;
  4961. }
  4962. BfMangler::MangleKind BfCompiler::GetMangleKind()
  4963. {
  4964. if (mOptions.mToolsetType == BfToolsetType_GNU)
  4965. return BfMangler::MangleKind_GNU;
  4966. return (mSystem->mPtrSize == 8) ? BfMangler::MangleKind_Microsoft_64 : BfMangler::MangleKind_Microsoft_32;
  4967. }
  4968. //////////////////////////////////////////////////////////////////////////
  4969. int ArrTest()
  4970. {
  4971. //SizedArray<int, 8> intArr;
  4972. //Array<int> intArr;
  4973. //std::vector<int> intArr;
  4974. BfSizedVector<int, 8> intArr;
  4975. //int val = intArr.GetLastSafe();
  4976. intArr.push_back(123);
  4977. intArr.pop_back();
  4978. intArr.push_back(234);
  4979. intArr.push_back(345);
  4980. //intArr.push_back(567);
  4981. //auto itr = std::find(intArr.begin(), intArr.end(), 234);
  4982. //intArr.erase(itr);
  4983. for (auto itr = intArr.begin(); itr != intArr.end(); )
  4984. {
  4985. if (*itr == 234)
  4986. itr = intArr.erase(itr);
  4987. else
  4988. itr++;
  4989. }
  4990. return (int)intArr.size();
  4991. //intArr.RemoveAt(2);
  4992. }
  4993. //////////////////////////////////////////////////////////////////////////
  4994. void BfCompiler::PopulateReified()
  4995. {
  4996. BfLogSysM("BfCompiler::PopulateReified\n");
  4997. BP_ZONE("PopulateReified");
  4998. BfContext* context = mContext;
  4999. bool hasTests = mSystem->HasTestProjects();
  5000. Array<BfMethodInstance*> impChainHeadMethods;
  5001. // Types can pull in new dependencies, so fully populate types until they stop
  5002. bool reifiedOnly = mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude;
  5003. while (true)
  5004. {
  5005. BP_ZONE("Compile_PopulateTypes");
  5006. int startTypeInitCount = mTypeInitCount;
  5007. bool didWork = false;
  5008. BfLogSysM("PopulateReified iteration start\n");
  5009. Array<BfType*> typeList;
  5010. typeList.Reserve(context->mResolvedTypes.GetCount());
  5011. for (auto type : context->mResolvedTypes)
  5012. typeList.Add(type);
  5013. int typeCount = 0;
  5014. for (auto type : typeList)
  5015. {
  5016. auto module = type->GetModule();
  5017. typeCount++;
  5018. if (module == NULL)
  5019. continue;
  5020. if (!type->IsReified())
  5021. {
  5022. // On compiles, only handle reified types in this loop. This fixes cases where our first instance of a dependent type
  5023. // is found to be unreified and then we have to reify it later. It's not an error, just a compile perf issue
  5024. continue;
  5025. }
  5026. // We have to not populate generic type instances because that may force us to populate a type that SHOULD be deleted
  5027. if ((type->IsIncomplete()) && (type->IsTypeInstance()) && (!type->IsGenericTypeInstance()))
  5028. {
  5029. mSystem->CheckLockYield();
  5030. module->PopulateType(type, BfPopulateType_Full);
  5031. }
  5032. auto typeInst = type->ToTypeInstance();
  5033. if ((typeInst != NULL) && (typeInst->IsGenericTypeInstance()) && (!typeInst->IsUnspecializedType()) &&
  5034. (!typeInst->IsDelegateFromTypeRef()) && (!typeInst->IsFunctionFromTypeRef()) && (!typeInst->IsTuple()))
  5035. {
  5036. auto unspecializedType = module->GetUnspecializedTypeInstance(typeInst);
  5037. if (!unspecializedType->mIsReified)
  5038. unspecializedType->mIsReified = true;
  5039. }
  5040. if ((type->IsValueType()) && (!type->IsUnspecializedType()))
  5041. {
  5042. bool dynamicBoxing = false;
  5043. if ((typeInst != NULL) && (typeInst->mTypeOptionsIdx != -2))
  5044. {
  5045. auto typeOptions = mSystem->GetTypeOptions(typeInst->mTypeOptionsIdx);
  5046. if (typeOptions != NULL)
  5047. {
  5048. if (typeOptions->Apply(false, BfOptionFlags_ReflectBoxing))
  5049. dynamicBoxing = true;
  5050. }
  5051. }
  5052. auto reflectKind = module->GetReflectKind(BfReflectKind_None, typeInst);
  5053. if ((reflectKind & BfReflectKind_DynamicBoxing) != 0)
  5054. dynamicBoxing = true;
  5055. if (dynamicBoxing)
  5056. {
  5057. auto boxedType = module->CreateBoxedType(typeInst);
  5058. module->AddDependency(boxedType, typeInst, BfDependencyMap::DependencyFlag_Allocates);
  5059. boxedType->mHasBeenInstantiated = true;
  5060. }
  5061. }
  5062. // Check reifications forced by virtuals or interfaces
  5063. if ((typeInst != NULL) && (typeInst->mIsReified) && (!typeInst->IsUnspecializedType()) && (!typeInst->IsInterface()) &&
  5064. (!typeInst->IsIncomplete()))
  5065. {
  5066. // If we have chained methods, make sure we implement the chain members if the chain head is implemented and reified
  5067. if (typeInst->mTypeDef->mIsCombinedPartial)
  5068. {
  5069. typeInst->mTypeDef->PopulateMemberSets();
  5070. bool hasUnimpChainMembers = false;
  5071. impChainHeadMethods.Clear();
  5072. for (auto& methodInstanceGroup : typeInst->mMethodInstanceGroups)
  5073. {
  5074. auto methodInstance = methodInstanceGroup.mDefault;
  5075. if (methodInstance == NULL)
  5076. continue;
  5077. if (methodInstance->mChainType == BfMethodChainType_ChainHead)
  5078. {
  5079. if (methodInstance->IsReifiedAndImplemented())
  5080. impChainHeadMethods.Add(methodInstance);
  5081. }
  5082. else if (methodInstance->mChainType == BfMethodChainType_ChainMember)
  5083. {
  5084. if (!methodInstance->IsReifiedAndImplemented())
  5085. hasUnimpChainMembers = true;
  5086. }
  5087. else if ((methodInstance->mChainType == BfMethodChainType_None) && (methodInstance->mMethodDef->IsDefaultCtor()))
  5088. {
  5089. if (!methodInstance->IsReifiedAndImplemented())
  5090. hasUnimpChainMembers = true;
  5091. }
  5092. else if (methodInstance->mIsInnerOverride)
  5093. {
  5094. if (!methodInstance->IsReifiedAndImplemented())
  5095. {
  5096. bool forceMethod = false;
  5097. BfMemberSetEntry* memberSetEntry;
  5098. if (typeInst->mTypeDef->mMethodSet.TryGetWith((StringImpl&)methodInstance->mMethodDef->mName, &memberSetEntry))
  5099. {
  5100. BfMethodDef* checkMethodDef = (BfMethodDef*)memberSetEntry->mMemberDef;
  5101. while (checkMethodDef != NULL)
  5102. {
  5103. auto& checkMethodInstanceGroup = typeInst->mMethodInstanceGroups[checkMethodDef->mIdx];
  5104. auto checkMethodInstance = checkMethodInstanceGroup.mDefault;
  5105. if (checkMethodInstance != NULL)
  5106. {
  5107. if ((checkMethodDef->mIsExtern) && (checkMethodInstance->IsReifiedAndImplemented()))
  5108. forceMethod = true;
  5109. }
  5110. checkMethodDef = checkMethodDef->mNextWithSameName;
  5111. }
  5112. }
  5113. if (forceMethod)
  5114. {
  5115. typeInst->mModule->GetMethodInstance(methodInstance->GetOwner(), methodInstance->mMethodDef, BfTypeVector(),
  5116. (BfGetMethodInstanceFlags)(BfGetMethodInstanceFlag_UnspecializedPass));
  5117. }
  5118. }
  5119. }
  5120. }
  5121. if ((hasUnimpChainMembers) && (!impChainHeadMethods.IsEmpty()))
  5122. {
  5123. for (auto& methodInstanceGroup : typeInst->mMethodInstanceGroups)
  5124. {
  5125. auto methodInstance = methodInstanceGroup.mDefault;
  5126. if (methodInstance == NULL)
  5127. continue;
  5128. bool forceMethod = false;
  5129. if (methodInstance->mChainType == BfMethodChainType_ChainMember)
  5130. {
  5131. if (!methodInstance->IsReifiedAndImplemented())
  5132. {
  5133. for (auto impMethodInstance : impChainHeadMethods)
  5134. {
  5135. if (typeInst->mModule->CompareMethodSignatures(methodInstance, impMethodInstance))
  5136. {
  5137. forceMethod = true;
  5138. }
  5139. }
  5140. }
  5141. }
  5142. else if (methodInstance->mMethodDef->IsDefaultCtor())
  5143. {
  5144. if (!methodInstance->IsReifiedAndImplemented())
  5145. forceMethod = true;
  5146. }
  5147. if (forceMethod)
  5148. {
  5149. typeInst->mModule->GetMethodInstance(methodInstance->GetOwner(), methodInstance->mMethodDef, BfTypeVector(),
  5150. (BfGetMethodInstanceFlags)(BfGetMethodInstanceFlag_UnspecializedPass));
  5151. }
  5152. }
  5153. }
  5154. }
  5155. // Only check virtual stuff if we have been instantiated
  5156. if ((typeInst->HasBeenInstantiated()) && (!mCanceling))
  5157. {
  5158. // If we have any virtual methods overrides that are unreified but the declaring virtual method is reified then we also need to reify
  5159. for (int virtIdx = 0; virtIdx < typeInst->mVirtualMethodTable.mSize; virtIdx++)
  5160. {
  5161. auto& vEntry = typeInst->mVirtualMethodTable[virtIdx];
  5162. if ((vEntry.mDeclaringMethod.mTypeInstance == NULL) ||
  5163. (vEntry.mDeclaringMethod.mTypeInstance->IsIncomplete()) ||
  5164. (vEntry.mImplementingMethod.mTypeInstance == NULL) ||
  5165. (vEntry.mImplementingMethod.mTypeInstance->IsIncomplete()))
  5166. continue;
  5167. BfMethodInstance* declaringMethod = vEntry.mDeclaringMethod;
  5168. if (declaringMethod == NULL)
  5169. continue;
  5170. if ((declaringMethod->mIsReified) && (declaringMethod->mMethodInstanceGroup->IsImplemented()))
  5171. {
  5172. if (vEntry.mImplementingMethod.mKind == BfMethodRefKind_AmbiguousRef)
  5173. {
  5174. auto checkTypeInst = typeInst;
  5175. while (checkTypeInst != NULL)
  5176. {
  5177. BfMemberSetEntry* memberSetEntry;
  5178. if (checkTypeInst->mTypeDef->mMethodSet.TryGetWith(String(declaringMethod->mMethodDef->mName), &memberSetEntry))
  5179. {
  5180. BfMethodDef* methodDef = (BfMethodDef*)memberSetEntry->mMemberDef;
  5181. while (methodDef != NULL)
  5182. {
  5183. if ((methodDef->mIsOverride) && (methodDef->mParams.mSize == declaringMethod->mMethodDef->mParams.mSize))
  5184. {
  5185. auto implMethod = typeInst->mModule->GetRawMethodInstance(typeInst, methodDef);
  5186. if ((implMethod != NULL) && (typeInst->mModule->CompareMethodSignatures(declaringMethod, implMethod)))
  5187. {
  5188. if ((implMethod != NULL) && ((!implMethod->mMethodInstanceGroup->IsImplemented()) || (!implMethod->mIsReified)))
  5189. {
  5190. didWork = true;
  5191. if (!typeInst->mModule->mIsModuleMutable)
  5192. typeInst->mModule->StartExtension();
  5193. typeInst->mModule->GetMethodInstance(implMethod);
  5194. }
  5195. }
  5196. }
  5197. methodDef = methodDef->mNextWithSameName;
  5198. }
  5199. }
  5200. if (checkTypeInst == declaringMethod->GetOwner())
  5201. break;
  5202. checkTypeInst = checkTypeInst->mBaseType;
  5203. }
  5204. }
  5205. else
  5206. {
  5207. BfMethodInstance* implMethod = vEntry.mImplementingMethod;
  5208. if ((implMethod != NULL) && ((!implMethod->mMethodInstanceGroup->IsImplemented()) || (!implMethod->mIsReified)))
  5209. {
  5210. didWork = true;
  5211. if (!typeInst->mModule->mIsModuleMutable)
  5212. typeInst->mModule->StartExtension();
  5213. typeInst->mModule->GetMethodInstance(implMethod);
  5214. }
  5215. }
  5216. }
  5217. }
  5218. auto checkType = typeInst;
  5219. while (checkType != NULL)
  5220. {
  5221. if ((checkType != typeInst) && (checkType->HasBeenInstantiated()))
  5222. {
  5223. // We will already check this type here in its own loop
  5224. break;
  5225. }
  5226. if (checkType->mDefineState < BfTypeDefineState_DefinedAndMethodsSlotted)
  5227. break;
  5228. for (auto& ifaceTypeInst : checkType->mInterfaces)
  5229. {
  5230. auto ifaceInst = ifaceTypeInst.mInterfaceType;
  5231. int startIdx = ifaceTypeInst.mStartInterfaceTableIdx;
  5232. int iMethodCount = (int)ifaceInst->mMethodInstanceGroups.size();
  5233. auto declTypeDef = ifaceTypeInst.mDeclaringType;
  5234. for (int iMethodIdx = 0; iMethodIdx < iMethodCount; iMethodIdx++)
  5235. {
  5236. auto& ifaceMethodInstGroup = ifaceInst->mMethodInstanceGroups[iMethodIdx];
  5237. auto ifaceMethodInst = ifaceMethodInstGroup.mDefault;
  5238. if (typeInst->IsObject())
  5239. {
  5240. // If the implementor is an object then this can be dynamically dispatched
  5241. }
  5242. else
  5243. {
  5244. // If this method is explicitly reflected then a struct's implementation may be invoked with reflection
  5245. if (!ifaceMethodInstGroup.mExplicitlyReflected)
  5246. continue;
  5247. }
  5248. if ((ifaceMethodInst == NULL) || (!ifaceMethodInst->IsReifiedAndImplemented()))
  5249. continue;
  5250. auto implMethodRef = &checkType->mInterfaceMethodTable[iMethodIdx + startIdx].mMethodRef;
  5251. BfMethodInstance* implMethod = *implMethodRef;
  5252. if (implMethod == NULL)
  5253. continue;
  5254. // Reify any interface methods that could be called dynamically
  5255. if ((!implMethod->IsReifiedAndImplemented()) && (implMethod->GetNumGenericParams() == 0) && (!implMethod->mMethodDef->mIsStatic) &&
  5256. (!implMethod->mReturnType->IsConcreteInterfaceType()))
  5257. {
  5258. didWork = true;
  5259. checkType->mModule->GetMethodInstance(implMethod);
  5260. }
  5261. }
  5262. }
  5263. checkType = checkType->mBaseType;
  5264. }
  5265. for (auto& reifyDep : typeInst->mReifyMethodDependencies)
  5266. {
  5267. if ((reifyDep.mDepMethod.mTypeInstance == NULL) ||
  5268. (reifyDep.mDepMethod.mTypeInstance->IsIncomplete()))
  5269. continue;
  5270. BfMethodInstance* depMethod = reifyDep.mDepMethod;
  5271. if (depMethod == NULL)
  5272. continue;
  5273. if ((depMethod->mIsReified) && (depMethod->mMethodInstanceGroup->IsImplemented()))
  5274. {
  5275. auto methodDef = typeInst->mTypeDef->mMethods[reifyDep.mMethodIdx];
  5276. typeInst->mModule->GetMethodInstance(typeInst, methodDef, BfTypeVector());
  5277. }
  5278. }
  5279. }
  5280. }
  5281. }
  5282. if (mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude)
  5283. {
  5284. BfLogSysM("BfCompiler::PopulateReified finding Main\n");
  5285. for (auto project : mSystem->mProjects)
  5286. {
  5287. String entryClassName = project->mStartupObject;
  5288. auto typeDef = mSystem->FindTypeDef(entryClassName, 0, project, {}, NULL, BfFindTypeDefFlag_AllowGlobal);
  5289. if (typeDef != NULL)
  5290. {
  5291. typeDef->mIsAlwaysInclude = true;
  5292. auto resolvedType = mContext->mScratchModule->ResolveTypeDef(typeDef);
  5293. if (resolvedType != NULL)
  5294. {
  5295. auto resolvedTypeInst = resolvedType->ToTypeInstance();
  5296. if (resolvedTypeInst != NULL)
  5297. {
  5298. auto module = resolvedTypeInst->GetModule();
  5299. if (!module->mIsReified)
  5300. module->ReifyModule();
  5301. mContext->mScratchModule->PopulateType(resolvedType, BfPopulateType_Full);
  5302. BfMemberSetEntry* memberSetEntry;
  5303. if (resolvedTypeInst->mTypeDef->mMethodSet.TryGetWith(String("Main"), &memberSetEntry))
  5304. {
  5305. BfMethodDef* methodDef = (BfMethodDef*)memberSetEntry->mMemberDef;
  5306. while (methodDef != NULL)
  5307. {
  5308. auto moduleMethodInstance = mContext->mScratchModule->GetMethodInstanceAtIdx(resolvedTypeInst, methodDef->mIdx);
  5309. auto methodInstance = moduleMethodInstance.mMethodInstance;
  5310. if (methodInstance->GetParamCount() != 0)
  5311. {
  5312. mContext->mScratchModule->GetInternalMethod("CreateParamsArray");
  5313. mContext->mScratchModule->GetInternalMethod("DeleteStringArray");
  5314. }
  5315. methodDef = methodDef->mNextWithSameName;
  5316. }
  5317. }
  5318. }
  5319. }
  5320. }
  5321. }
  5322. }
  5323. BfLogSysM("PopulateReified iteration done\n");
  5324. didWork |= DoWorkLoop(reifiedOnly, reifiedOnly);
  5325. if (reifiedOnly)
  5326. didWork |= DoWorkLoop(false, reifiedOnly);
  5327. if (startTypeInitCount != mTypeInitCount)
  5328. didWork = true;
  5329. if (didWork)
  5330. continue;
  5331. // We get everything on the first pass through
  5332. if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
  5333. break;
  5334. if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_SkipUnused)
  5335. break;
  5336. if (startTypeInitCount == mTypeInitCount)
  5337. break;
  5338. }
  5339. }
  5340. bool BfCompiler::IsCePaused()
  5341. {
  5342. return (mCeMachine != NULL) && (mCeMachine->mDbgPaused);
  5343. }
  5344. bool BfCompiler::EnsureCeUnpaused(BfType* refType)
  5345. {
  5346. if ((mCeMachine == NULL) || (!mCeMachine->mDbgPaused))
  5347. return true;
  5348. mCeMachine->mDebugger->mCurDbgState->mReferencedIncompleteTypes = true;
  5349. //mPassInstance->Fail(StrFormat("Use of incomplete type '%s'", mCeMachine->mCeModule->TypeToString(refType).c_str()));
  5350. return false;
  5351. }
  5352. void BfCompiler::HotCommit()
  5353. {
  5354. if (mHotState == NULL)
  5355. return;
  5356. mHotState->mCommittedHotCompileIdx = mOptions.mHotCompileIdx;
  5357. for (auto type : mContext->mResolvedTypes)
  5358. {
  5359. auto typeInst = type->ToTypeInstance();
  5360. if (typeInst == NULL)
  5361. continue;
  5362. if (typeInst->mHotTypeData == NULL)
  5363. continue;
  5364. for (int typeIdx = (int)typeInst->mHotTypeData->mTypeVersions.size() - 1; typeIdx >= 0; typeIdx--)
  5365. {
  5366. auto hotVersion = typeInst->mHotTypeData->mTypeVersions[typeIdx];
  5367. if (hotVersion->mCommittedHotCompileIdx != -1)
  5368. break;
  5369. hotVersion->mCommittedHotCompileIdx = mHotState->mCommittedHotCompileIdx;
  5370. if ((!hotVersion->mInterfaceMapping.IsEmpty()) && (typeIdx > 0))
  5371. {
  5372. auto hotVersionHead = typeInst->mHotTypeData->GetLatestVersionHead();
  5373. if ((hotVersionHead != hotVersion) && (hotVersionHead->mDataHash == hotVersion->mDataHash))
  5374. {
  5375. // When we have a slot failure, the data hash will match but we actually do need to use the new mInterfaceMapping entries
  5376. // So we copy them over to the
  5377. hotVersionHead->mInterfaceMapping = hotVersion->mInterfaceMapping;
  5378. }
  5379. }
  5380. }
  5381. }
  5382. }
  5383. void BfCompiler::HotResolve_Start(HotResolveFlags flags)
  5384. {
  5385. BfLogSysM("BfCompiler::HotResolve_Start\n");
  5386. delete mHotResolveData;
  5387. mHotResolveData = new HotResolveData();
  5388. mHotResolveData->mFlags = flags;
  5389. mHotResolveData->mHotTypeIdFlags.Resize(mCurTypeId);
  5390. mHotResolveData->mReasons.Resize(mCurTypeId);
  5391. if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
  5392. {
  5393. HotResolve_AddReachableMethod("BfCallAllStaticDtors");
  5394. for (auto& kv : mHotData->mFuncPtrs)
  5395. {
  5396. auto funcRef = kv.mValue;
  5397. HotResolve_AddReachableMethod(funcRef->mMethod, HotTypeFlag_FuncPtr, true);
  5398. }
  5399. }
  5400. }
  5401. //#define HOT_DEBUG_NAME
  5402. bool BfCompiler::HotResolve_AddReachableMethod(BfHotMethod* hotMethod, HotTypeFlags flags, bool devirtualized, bool forceProcess)
  5403. {
  5404. #ifdef HOT_DEBUG_NAME
  5405. HotResolve_PopulateMethodNameMap();
  5406. String* namePtr = NULL;
  5407. if (mHotData->mMethodNameMap.TryGetValue(hotMethod, &namePtr))
  5408. {
  5409. }
  5410. #endif
  5411. HotReachableData* hotReachableData;
  5412. if (mHotResolveData->mReachableMethods.TryAdd(hotMethod, NULL, &hotReachableData))
  5413. {
  5414. hotMethod->mRefCount++;
  5415. }
  5416. else
  5417. {
  5418. hotReachableData->mTypeFlags = (HotTypeFlags)(hotReachableData->mTypeFlags | flags);
  5419. if ((!devirtualized) && (!hotReachableData->mHadNonDevirtualizedCall))
  5420. {
  5421. hotReachableData->mHadNonDevirtualizedCall = true;
  5422. if (!forceProcess)
  5423. return true;
  5424. }
  5425. if (!forceProcess)
  5426. return false;
  5427. }
  5428. hotReachableData->mTypeFlags = (HotTypeFlags)(hotReachableData->mTypeFlags | flags);
  5429. if (!devirtualized)
  5430. hotReachableData->mHadNonDevirtualizedCall = true;
  5431. for (auto hotDepData : hotMethod->mReferences)
  5432. {
  5433. if (hotDepData->mDataKind == BfHotDepDataKind_ThisType)
  5434. {
  5435. auto hotThisType = (BfHotThisType*)hotDepData;
  5436. auto hotTypeVersion = hotThisType->mTypeVersion;
  5437. HotTypeFlags hotTypeFlags = mHotResolveData->mHotTypeIdFlags[hotTypeVersion->mTypeId];
  5438. bool isAllocated = (hotTypeFlags & (HotTypeFlag_Heap | HotTypeFlag_CanAllocate)) != 0;
  5439. if (!isAllocated)
  5440. {
  5441. if (mHotResolveData->mDeferredThisCheckMethods.Add(hotMethod))
  5442. {
  5443. return true;
  5444. }
  5445. else
  5446. {
  5447. return false;
  5448. }
  5449. }
  5450. else
  5451. {
  5452. mHotResolveData->mDeferredThisCheckMethods.Remove(hotMethod);
  5453. }
  5454. }
  5455. else if (hotDepData->mDataKind == BfHotDepDataKind_Allocation)
  5456. {
  5457. auto hotAllocation = (BfHotAllocation*)hotDepData;
  5458. auto hotTypeVersion = hotAllocation->mTypeVersion;
  5459. HotResolve_ReportType(hotTypeVersion, flags, hotMethod);
  5460. HotResolve_ReportType(hotTypeVersion, HotTypeFlag_CanAllocate, hotMethod);
  5461. }
  5462. else if (hotDepData->mDataKind == BfHotDepDataKind_TypeVersion)
  5463. {
  5464. auto hotTypeVersion = (BfHotTypeVersion*)hotDepData;
  5465. HotResolve_ReportType(hotTypeVersion, flags, hotMethod);
  5466. }
  5467. else if (hotDepData->mDataKind == BfHotDepDataKind_Method)
  5468. {
  5469. auto checkMethod = (BfHotMethod*)hotDepData;
  5470. HotResolve_AddReachableMethod(checkMethod, flags, false);
  5471. }
  5472. else if (hotDepData->mDataKind == BfHotDepDataKind_DevirtualizedMethod)
  5473. {
  5474. auto checkMethod = (BfHotDevirtualizedMethod*)hotDepData;
  5475. HotResolve_AddReachableMethod(checkMethod->mMethod, flags, true);
  5476. }
  5477. else if (hotDepData->mDataKind == BfHotDepDataKind_DupMethod)
  5478. {
  5479. auto checkMethod = (BfHotDupMethod*)hotDepData;
  5480. HotResolve_AddReachableMethod(checkMethod->mMethod, flags, true);
  5481. }
  5482. }
  5483. return true;
  5484. }
  5485. void BfCompiler::HotResolve_AddReachableMethod(const StringImpl& methodName)
  5486. {
  5487. BfLogSysM("HotResolve_AddReachableMethod %s\n", methodName.c_str());
  5488. String mangledName = methodName;
  5489. BfHotMethod** hotMethodPtr;
  5490. if (!mHotData->mMethodMap.TryGetValue(mangledName, &hotMethodPtr))
  5491. {
  5492. BfLogSysM("Hot method not found\n");
  5493. return;
  5494. }
  5495. BfHotMethod* hotMethod = *hotMethodPtr;
  5496. while (hotMethod->mPrevVersion != NULL)
  5497. {
  5498. if (hotMethod->mSrcTypeVersion->mCommittedHotCompileIdx != -1)
  5499. break;
  5500. hotMethod = hotMethod->mPrevVersion;
  5501. }
  5502. HotResolve_AddReachableMethod(hotMethod, HotTypeFlag_ActiveFunction, true);
  5503. }
  5504. void BfCompiler::HotResolve_AddActiveMethod(BfHotMethod* hotMethod)
  5505. {
  5506. #ifdef HOT_DEBUG_NAME
  5507. HotResolve_PopulateMethodNameMap();
  5508. String* namePtr = NULL;
  5509. if (mHotData->mMethodNameMap.TryGetValue(hotMethod, &namePtr))
  5510. {
  5511. }
  5512. #endif
  5513. if (mHotResolveData->mActiveMethods.Add(hotMethod))
  5514. {
  5515. hotMethod->mRefCount++;
  5516. }
  5517. // We don't need to mark reachable methods unless we had data changes
  5518. if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
  5519. {
  5520. HotResolve_AddReachableMethod(hotMethod, HotTypeFlag_ActiveFunction, true);
  5521. }
  5522. if ((hotMethod->mFlags & BfHotDepDataFlag_HasDup) != 0)
  5523. {
  5524. for (auto depData : hotMethod->mReferences)
  5525. {
  5526. if (depData->mDataKind != BfHotDepDataKind_DupMethod)
  5527. continue;
  5528. auto hotDupMethod = (BfHotDupMethod*)depData;
  5529. HotResolve_AddActiveMethod(hotDupMethod->mMethod);
  5530. }
  5531. }
  5532. }
  5533. void BfCompiler::HotResolve_AddActiveMethod(const StringImpl& methodName)
  5534. {
  5535. BfLogSysM("HotResolve_AddActiveMethod %s\n", methodName.c_str());
  5536. StringT<512> mangledName;
  5537. int hotCompileIdx = 0;
  5538. int tabIdx = (int)methodName.IndexOf('\t');
  5539. if (tabIdx != -1)
  5540. {
  5541. mangledName = methodName.Substring(0, tabIdx);
  5542. hotCompileIdx = atoi(methodName.c_str() + tabIdx + 1);
  5543. }
  5544. else
  5545. mangledName = methodName;
  5546. bool isDelegateRef = false;
  5547. BfHotMethod** hotMethodPtr;
  5548. if (!mHotData->mMethodMap.TryGetValue(mangledName, &hotMethodPtr))
  5549. {
  5550. BfLogSysM("Hot method not found\n");
  5551. return;
  5552. }
  5553. BfHotMethod* hotMethod = *hotMethodPtr;
  5554. while (hotMethod->mPrevVersion != NULL)
  5555. {
  5556. if ((hotMethod->mSrcTypeVersion->mCommittedHotCompileIdx != -1) && (hotCompileIdx < hotMethod->mSrcTypeVersion->mCommittedHotCompileIdx))
  5557. break;
  5558. hotMethod = hotMethod->mPrevVersion;
  5559. }
  5560. HotResolve_AddActiveMethod(hotMethod);
  5561. }
  5562. void BfCompiler::HotResolve_AddDelegateMethod(const StringImpl& methodName)
  5563. {
  5564. BfLogSysM("HotResolve_HotResolve_AddDelegateMethod %s\n", methodName.c_str());
  5565. String mangledName = methodName;
  5566. BfHotMethod** hotMethodPtr;
  5567. if (!mHotData->mMethodMap.TryGetValue(mangledName, &hotMethodPtr))
  5568. {
  5569. BfLogSysM("Hot method not found\n");
  5570. return;
  5571. }
  5572. BfHotMethod* hotMethod = *hotMethodPtr;
  5573. HotResolve_AddReachableMethod(hotMethod, HotTypeFlag_Delegate, true);
  5574. }
  5575. void BfCompiler::HotResolve_ReportType(BfHotTypeVersion* hotTypeVersion, HotTypeFlags flags, BfHotDepData* reason)
  5576. {
  5577. auto& flagsRef = mHotResolveData->mHotTypeFlags[hotTypeVersion];
  5578. if (flagsRef == (flagsRef | flags))
  5579. return;
  5580. flagsRef = (HotTypeFlags)(flags | flagsRef);
  5581. bool applyFlags = true;
  5582. if ((flags & (BfCompiler::HotTypeFlag_ActiveFunction | BfCompiler::HotTypeFlag_Delegate | BfCompiler::HotTypeFlag_FuncPtr)) != 0)
  5583. {
  5584. applyFlags = (hotTypeVersion->mCommittedHotCompileIdx != -1) && (mHotState->mPendingDataChanges.Contains(hotTypeVersion->mTypeId));
  5585. if ((!applyFlags) && (hotTypeVersion->mCommittedHotCompileIdx != -1))
  5586. applyFlags = mHotState->mPendingFailedSlottings.Contains(hotTypeVersion->mTypeId);
  5587. if (applyFlags)
  5588. {
  5589. if (reason != NULL)
  5590. mHotResolveData->mReasons[hotTypeVersion->mTypeId] = reason;
  5591. }
  5592. }
  5593. if (applyFlags)
  5594. {
  5595. auto& flagsIdRef = mHotResolveData->mHotTypeIdFlags[hotTypeVersion->mTypeId];
  5596. flagsIdRef = (HotTypeFlags)(flags | flagsIdRef);
  5597. }
  5598. BfLogSysM("HotResolve_ReportType %p %s Flags:%X DeclHotIdx:%d\n", hotTypeVersion, mContext->TypeIdToString(hotTypeVersion->mTypeId).c_str(), flags, hotTypeVersion->mDeclHotCompileIdx);
  5599. for (auto member : hotTypeVersion->mMembers)
  5600. {
  5601. HotResolve_ReportType(member, flags, reason);
  5602. }
  5603. }
  5604. void BfCompiler::HotResolve_ReportType(int typeId, HotTypeFlags flags)
  5605. {
  5606. if ((uint)typeId >= mHotResolveData->mHotTypeIdFlags.size())
  5607. {
  5608. BF_DBG_FATAL("Invalid typeId");
  5609. return;
  5610. }
  5611. if (mHotResolveData->mHotTypeIdFlags[typeId] == (mHotResolveData->mHotTypeIdFlags[typeId] | flags))
  5612. return;
  5613. auto hotTypeData = mContext->GetHotTypeData(typeId);
  5614. if (hotTypeData != NULL)
  5615. {
  5616. auto hotTypeVersion = hotTypeData->GetTypeVersion(mHotState->mCommittedHotCompileIdx);
  5617. BF_ASSERT(hotTypeVersion != NULL);
  5618. if (hotTypeVersion != NULL)
  5619. HotResolve_ReportType(hotTypeVersion, flags, NULL);
  5620. }
  5621. mHotResolveData->mHotTypeIdFlags[typeId] = (HotTypeFlags)(flags | mHotResolveData->mHotTypeIdFlags[typeId]);
  5622. }
  5623. void BfCompiler::HotResolve_PopulateMethodNameMap()
  5624. {
  5625. if (!mHotData->mMethodNameMap.IsEmpty())
  5626. return;
  5627. for (auto& kv : mHotData->mMethodMap)
  5628. {
  5629. auto hotMethod = kv.mValue;
  5630. while (hotMethod != NULL)
  5631. {
  5632. mHotData->mMethodNameMap[hotMethod] = &kv.mKey;
  5633. hotMethod = hotMethod->mPrevVersion;
  5634. }
  5635. }
  5636. }
  5637. String BfCompiler::HotResolve_Finish()
  5638. {
  5639. BfLogSysM("HotResolve_Finish\n");
  5640. if (mHotState == NULL)
  5641. {
  5642. // It's possible we did a HotCompile with no file changes and therefore didn't actually do a compile
  5643. return "";
  5644. }
  5645. String result;
  5646. if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
  5647. {
  5648. BF_ASSERT(!mHotState->mPendingDataChanges.IsEmpty() || !mHotState->mPendingFailedSlottings.IsEmpty());
  5649. }
  5650. else
  5651. {
  5652. BF_ASSERT(mHotState->mPendingDataChanges.IsEmpty() && mHotState->mPendingFailedSlottings.IsEmpty());
  5653. }
  5654. if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
  5655. {
  5656. auto _AddUsedType = [&](BfTypeDef* typeDef)
  5657. {
  5658. auto type = mContext->mUnreifiedModule->ResolveTypeDef(mReflectTypeInstanceTypeDef);
  5659. if (type != NULL)
  5660. HotResolve_ReportType(type->mTypeId, BfCompiler::HotTypeFlag_Heap);
  5661. };
  5662. // We have some types that can be allocated in a read-only section- pretend they are on the heap
  5663. _AddUsedType(mReflectTypeInstanceTypeDef);
  5664. _AddUsedType(mStringTypeDef);
  5665. // Find any virtual method overrides that may have been called.
  5666. // These can cause new reachable virtual methods to be called, which may take more than one iteration to fully resolve
  5667. for (int methodPass = 0; true; methodPass++)
  5668. {
  5669. bool didWork = false;
  5670. for (auto hotMethod : mHotResolveData->mDeferredThisCheckMethods)
  5671. {
  5672. if (HotResolve_AddReachableMethod(hotMethod, BfCompiler::HotTypeFlag_ActiveFunction, true, true))
  5673. didWork = true;
  5674. }
  5675. HotTypeFlags typeFlags = HotTypeFlag_None;
  5676. for (auto& kv : mHotData->mMethodMap)
  5677. {
  5678. String& methodName = kv.mKey;
  5679. auto hotMethod = kv.mValue;
  5680. bool doCall = false;
  5681. bool forceAdd = false;
  5682. if (mHotResolveData->mReachableMethods.ContainsKey(hotMethod))
  5683. continue;
  5684. for (auto ref : hotMethod->mReferences)
  5685. {
  5686. if (ref->mDataKind == BfHotDepDataKind_ThisType)
  5687. continue;
  5688. if (ref->mDataKind != BfHotDepDataKind_VirtualDecl)
  5689. break;
  5690. auto hotVirtualDecl = (BfHotVirtualDeclaration*)ref;
  5691. HotReachableData* hotReachableData;
  5692. if (mHotResolveData->mReachableMethods.TryGetValue(hotVirtualDecl->mMethod, &hotReachableData))
  5693. {
  5694. #ifdef HOT_DEBUG_NAME
  5695. HotResolve_PopulateMethodNameMap();
  5696. String* namePtr = NULL;
  5697. if (mHotData->mMethodNameMap.TryGetValue(hotVirtualDecl->mMethod, &namePtr))
  5698. {
  5699. }
  5700. #endif
  5701. if (hotReachableData->mHadNonDevirtualizedCall)
  5702. {
  5703. typeFlags = hotReachableData->mTypeFlags;
  5704. doCall = true;
  5705. }
  5706. }
  5707. }
  5708. if (!doCall)
  5709. {
  5710. if ((hotMethod->mFlags & BfHotDepDataFlag_AlwaysCalled) != 0)
  5711. {
  5712. typeFlags = BfCompiler::HotTypeFlag_ActiveFunction;
  5713. doCall = true;
  5714. }
  5715. }
  5716. if (doCall)
  5717. {
  5718. if (HotResolve_AddReachableMethod(hotMethod, typeFlags, true, forceAdd))
  5719. didWork = true;
  5720. }
  5721. }
  5722. if (!didWork)
  5723. break;
  5724. }
  5725. int errorCount = 0;
  5726. for (int typeId = 0; typeId < (int)mHotResolveData->mHotTypeIdFlags.size(); typeId++)
  5727. {
  5728. auto flags = mHotResolveData->mHotTypeIdFlags[typeId];
  5729. if (flags == 0)
  5730. continue;
  5731. auto type = mContext->mTypes[typeId];
  5732. Dictionary<BfHotMethod*, String*> methodNameMap;
  5733. if ((flags > BfCompiler::HotTypeFlag_UserNotUsed) &&
  5734. ((mHotState->mPendingDataChanges.Contains(typeId)) || (mHotState->mPendingFailedSlottings.Contains(typeId))))
  5735. {
  5736. bool isBadTypeUsed = false;
  5737. if ((flags & HotTypeFlag_Heap) != 0)
  5738. isBadTypeUsed = true;
  5739. else if ((flags & (HotTypeFlag_ActiveFunction | HotTypeFlag_Delegate | HotTypeFlag_FuncPtr)) != 0)
  5740. {
  5741. // If we detect an old version being used, it's only an issue if this type can actually be allocated
  5742. if ((flags & HotTypeFlag_CanAllocate) != 0)
  5743. {
  5744. isBadTypeUsed = true;
  5745. }
  5746. }
  5747. if (isBadTypeUsed)
  5748. {
  5749. bool reasonIsActiveMethod = false;
  5750. String methodReason;
  5751. auto reason = mHotResolveData->mReasons[typeId];
  5752. if ((reason != NULL) && (reason->mDataKind == BfHotDepDataKind_Method))
  5753. {
  5754. auto hotMethod = (BfHotMethod*)reason;
  5755. reasonIsActiveMethod = mHotResolveData->mActiveMethods.Contains(hotMethod);
  5756. HotResolve_PopulateMethodNameMap();
  5757. String** strPtr;
  5758. if (mHotData->mMethodNameMap.TryGetValue(hotMethod, &strPtr))
  5759. {
  5760. methodReason += BfDemangler::Demangle((**strPtr), DbgLanguage_Beef, BfDemangler::Flag_BeefFixed);
  5761. }
  5762. }
  5763. errorCount++;
  5764. if (errorCount >= 1000)
  5765. {
  5766. result += "\n (more errors)...";
  5767. break;
  5768. }
  5769. if (!result.IsEmpty())
  5770. result += "\n";
  5771. result += "'";
  5772. result += mContext->TypeIdToString(typeId);
  5773. result += "'";
  5774. if ((flags & BfCompiler::HotTypeFlag_Heap) != 0)
  5775. result += " allocated on the heap";
  5776. else if ((flags & BfCompiler::HotTypeFlag_ActiveFunction) != 0)
  5777. {
  5778. if (reasonIsActiveMethod)
  5779. result += StrFormat(" used by active method '%s'", methodReason.c_str());
  5780. else if (!methodReason.IsEmpty())
  5781. result += StrFormat(" previous data version used by deleted method '%s', reachable by an active method", methodReason.c_str());
  5782. else
  5783. result += " previous data version used by a deleted method reachable by an active method";
  5784. }
  5785. else if ((flags & BfCompiler::HotTypeFlag_Delegate) != 0)
  5786. {
  5787. if (!methodReason.IsEmpty())
  5788. result += StrFormat(" previous data version used by deleted method '%s', reachable by a delegate", methodReason.c_str());
  5789. else
  5790. result += " previous data version used by a deleted method reachable by a delegate";
  5791. }
  5792. else if ((flags & BfCompiler::HotTypeFlag_FuncPtr) != 0)
  5793. {
  5794. if (!methodReason.IsEmpty())
  5795. result += StrFormat(" previous data version used by deleted method '%s', reachable by a function pointer", methodReason.c_str());
  5796. else
  5797. result += " previous data version used by a deleted method reachable by a function pointer";
  5798. }
  5799. else if ((flags & BfCompiler::HotTypeFlag_UserUsed) != 0)
  5800. result += " stated as used by the program";
  5801. }
  5802. }
  5803. String typeName = mContext->TypeIdToString(typeId);
  5804. BfLogSysM(" %d %s %02X\n", typeId, typeName.c_str(), flags);
  5805. }
  5806. if (result.IsEmpty())
  5807. {
  5808. for (auto typeId : mHotState->mPendingDataChanges)
  5809. {
  5810. auto type = mContext->mTypes[typeId];
  5811. auto typeInstance = type->ToTypeInstance();
  5812. BF_ASSERT(typeInstance->mHotTypeData->mPendingDataChange);
  5813. typeInstance->mHotTypeData->mPendingDataChange = false;
  5814. typeInstance->mHotTypeData->mHadDataChange = true;
  5815. typeInstance->mHotTypeData->mVTableOrigLength = -1;
  5816. typeInstance->mHotTypeData->mOrigInterfaceMethodsLength = -1;
  5817. BfLogSysM("Pending data change applied to type %p\n", typeInstance);
  5818. }
  5819. mHotState->mPendingDataChanges.Clear();
  5820. mHotState->mPendingFailedSlottings.Clear();
  5821. }
  5822. }
  5823. ClearOldHotData();
  5824. if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
  5825. {
  5826. for (int pass = 0; pass < 2; pass++)
  5827. {
  5828. bool wantsReachable = pass == 0;
  5829. Array<String> methodList;
  5830. for (auto& kv : mHotData->mMethodMap)
  5831. {
  5832. auto hotMethod = kv.mValue;
  5833. bool reachable = mHotResolveData->mReachableMethods.ContainsKey(hotMethod);
  5834. if (reachable != wantsReachable)
  5835. continue;
  5836. String methodName;
  5837. methodName += BfDemangler::Demangle(kv.mKey, DbgLanguage_Beef, BfDemangler::Flag_BeefFixed);
  5838. methodName += " - ";
  5839. methodName += kv.mKey;
  5840. methodList.Add(methodName);
  5841. }
  5842. methodList.Sort([](const String& lhs, const String& rhs) { return lhs < rhs; });
  5843. for (auto& methodName : methodList)
  5844. BfLogSysM("%s: %s\n", wantsReachable ? "Reachable" : "Unreachable", methodName.c_str());
  5845. }
  5846. }
  5847. delete mHotResolveData;
  5848. mHotResolveData = NULL;
  5849. return result;
  5850. }
  5851. void BfCompiler::ClearOldHotData()
  5852. {
  5853. if (mHotData == NULL)
  5854. return;
  5855. // TODO: Get rid of old hot data during hot compiles, too
  5856. // if (IsHotCompile())
  5857. // return;
  5858. BP_ZONE("BfCompiler::ClearOldHotData");
  5859. bool isHotCompile = IsHotCompile();
  5860. auto itr = mHotData->mMethodMap.begin();
  5861. while (itr != mHotData->mMethodMap.end())
  5862. {
  5863. String& methodName = itr->mKey;
  5864. auto hotMethod = itr->mValue;
  5865. bool doDelete = false;
  5866. // If a previous version of a method is not currently active then it should be impossible to ever reach it
  5867. while (hotMethod->mPrevVersion != NULL)
  5868. {
  5869. auto prevMethod = hotMethod->mPrevVersion;
  5870. if (prevMethod->mRefCount > 1)
  5871. {
  5872. BF_ASSERT((mHotResolveData != NULL) && (mHotResolveData->mActiveMethods.Contains(prevMethod)));
  5873. break;
  5874. }
  5875. hotMethod->mPrevVersion = prevMethod->mPrevVersion;
  5876. prevMethod->mPrevVersion = NULL;
  5877. prevMethod->Deref();
  5878. }
  5879. BF_ASSERT(hotMethod->mRefCount >= 1);
  5880. if (hotMethod->mPrevVersion == NULL)
  5881. {
  5882. if (hotMethod->mRefCount <= 1)
  5883. {
  5884. doDelete = true;
  5885. }
  5886. else if ((!isHotCompile) && ((hotMethod->mFlags & (BfHotDepDataFlag_IsBound | BfHotDepDataFlag_RetainMethodWithoutBinding)) == 0))
  5887. {
  5888. doDelete = true;
  5889. }
  5890. }
  5891. bool doRemove = doDelete;
  5892. if ((hotMethod->mFlags & BfHotDepDataFlag_HasDup) != 0)
  5893. {
  5894. bool hasDupMethod = false;
  5895. for (int idx = 0; idx < (int)hotMethod->mReferences.size(); idx++)
  5896. {
  5897. auto depData = hotMethod->mReferences[idx];
  5898. if (depData->mDataKind == BfHotDepDataKind_DupMethod)
  5899. {
  5900. auto dupMethod = (BfHotDupMethod*)depData;
  5901. if (doDelete)
  5902. {
  5903. doRemove = false;
  5904. dupMethod->mMethod->mRefCount++;
  5905. itr->mValue = dupMethod->mMethod;
  5906. }
  5907. else
  5908. {
  5909. if ((dupMethod->mMethod->mRefCount == 1) ||
  5910. ((!IsHotCompile()) && (dupMethod->mMethod->mFlags & BfHotDepDataFlag_IsBound) == 0))
  5911. {
  5912. dupMethod->Deref();
  5913. hotMethod->mReferences.RemoveAt(idx);
  5914. idx--;
  5915. }
  5916. }
  5917. }
  5918. }
  5919. }
  5920. if (doDelete)
  5921. {
  5922. BfLogSysM("Deleting hot method %p %s\n", hotMethod, methodName.c_str());
  5923. //BF_ASSERT(hotMethod->mRefCount == 1);
  5924. hotMethod->Clear();
  5925. hotMethod->Deref();
  5926. if (doRemove)
  5927. itr = mHotData->mMethodMap.Remove(itr);
  5928. }
  5929. else
  5930. ++itr;
  5931. }
  5932. mHotData->ClearUnused(IsHotCompile());
  5933. for (auto type : mContext->mResolvedTypes)
  5934. {
  5935. auto typeInst = type->ToTypeInstance();
  5936. if (typeInst == NULL)
  5937. continue;
  5938. if (typeInst->mHotTypeData == NULL)
  5939. continue;
  5940. bool foundCommittedVersion = false;
  5941. auto latestVersionHead = typeInst->mHotTypeData->GetLatestVersionHead();
  5942. for (int typeIdx = (int)typeInst->mHotTypeData->mTypeVersions.size() - 1; typeIdx >= 0; typeIdx--)
  5943. {
  5944. auto hotVersion = typeInst->mHotTypeData->mTypeVersions[typeIdx];
  5945. if (hotVersion == latestVersionHead)
  5946. {
  5947. // We have to keep the latest version head -- otherwise we would lose vdata and interface mapping data
  5948. continue;
  5949. }
  5950. if ((!foundCommittedVersion) && (mHotState != NULL) && (hotVersion->mDeclHotCompileIdx <= mHotState->mCommittedHotCompileIdx))
  5951. {
  5952. // Don't remove the latest committed version
  5953. foundCommittedVersion = true;
  5954. }
  5955. else if (hotVersion->mRefCount == 1)
  5956. {
  5957. typeInst->mHotTypeData->mTypeVersions.RemoveAt(typeIdx);
  5958. hotVersion->Deref();
  5959. BF_ASSERT(typeInst->mHotTypeData->mTypeVersions.size() > 0);
  5960. }
  5961. }
  5962. }
  5963. }
  5964. void BfCompiler::CompileReified()
  5965. {
  5966. BfLogSysM("BfCompiler::CompileReified\n");
  5967. BP_ZONE("Compile_ResolveTypeDefs");
  5968. Array<BfTypeDef*> deferTypeDefs;
  5969. for (auto typeDef : mSystem->mTypeDefs)
  5970. {
  5971. mSystem->CheckLockYield();
  5972. if (mCanceling)
  5973. {
  5974. BfLogSysM("Canceling from Compile typeDef loop\n");
  5975. break;
  5976. }
  5977. if (typeDef->mProject->mDisabled)
  5978. continue;
  5979. if (typeDef->mIsPartial)
  5980. continue;
  5981. auto scratchModule = mContext->mScratchModule;
  5982. bool isAlwaysInclude = (typeDef->mIsAlwaysInclude) || (typeDef->mProject->mAlwaysIncludeAll);
  5983. auto typeOptions = scratchModule->GetTypeOptions(typeDef);
  5984. if (typeOptions != NULL)
  5985. isAlwaysInclude = typeOptions->Apply(isAlwaysInclude, BfOptionFlags_ReflectAlwaysIncludeType);
  5986. if (typeDef->mProject->IsTestProject())
  5987. {
  5988. for (auto methodDef : typeDef->mMethods)
  5989. {
  5990. auto methodDeclaration = methodDef->GetMethodDeclaration();
  5991. if ((methodDeclaration != NULL) && (methodDeclaration->mAttributes != NULL) &&
  5992. (methodDeclaration->mAttributes->Contains("Test")))
  5993. isAlwaysInclude = true;
  5994. }
  5995. }
  5996. //TODO: Just because the type is required doesn't mean we want to reify it. Why did we have that check?
  5997. if ((mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude) && (!isAlwaysInclude))
  5998. {
  5999. if (typeDef->mGenericParamDefs.IsEmpty())
  6000. deferTypeDefs.Add(typeDef);
  6001. continue;
  6002. }
  6003. scratchModule->ResolveTypeDef(typeDef, BfPopulateType_Full);
  6004. }
  6005. // Resolve remaining typedefs as unreified so we can check their attributes
  6006. for (auto typeDef : deferTypeDefs)
  6007. {
  6008. auto type = mContext->mUnreifiedModule->ResolveTypeDef(typeDef, BfPopulateType_Identity);
  6009. if (type == NULL)
  6010. continue;
  6011. auto typeInst = type->ToTypeInstance();
  6012. if (typeInst == NULL)
  6013. continue;
  6014. if (typeInst->mIsReified)
  6015. continue;
  6016. mContext->mUnreifiedModule->PopulateType(typeInst, BfPopulateType_Interfaces_Direct);
  6017. if (typeInst->mCustomAttributes == NULL)
  6018. continue;
  6019. bool alwaysInclude = false;
  6020. for (auto& customAttribute : typeInst->mCustomAttributes->mAttributes)
  6021. {
  6022. if (customAttribute.mType->mAttributeData != NULL)
  6023. {
  6024. if (customAttribute.mType->mAttributeData->mAlwaysIncludeUser != 0)
  6025. alwaysInclude = true;
  6026. if ((customAttribute.mType->mAttributeData->mFlags & BfAttributeFlag_AlwaysIncludeTarget) != 0)
  6027. alwaysInclude = true;
  6028. }
  6029. }
  6030. if (alwaysInclude)
  6031. mContext->mScratchModule->PopulateType(typeInst, BfPopulateType_Full);
  6032. }
  6033. PopulateReified();
  6034. }
  6035. bool BfCompiler::DoCompile(const StringImpl& outputDirectory)
  6036. {
  6037. BP_ZONE("BfCompiler::Compile");
  6038. uint32 frontendStartTick = BFTickCount();
  6039. if (mSystem->mTypeDefs.mCount == 0)
  6040. {
  6041. // No-source bailout
  6042. return true;
  6043. }
  6044. if (!mOptions.mErrorString.IsEmpty())
  6045. {
  6046. mPassInstance->Fail(mOptions.mErrorString);
  6047. return false;
  6048. }
  6049. {
  6050. String hotSwapErrors;
  6051. String toolsetErrors;
  6052. for (auto project : mSystem->mProjects)
  6053. {
  6054. project->ClearCache();
  6055. if (project->mDisabled)
  6056. continue;
  6057. if (project->mCodeGenOptions.mLTOType != BfLTOType_None)
  6058. {
  6059. if (mOptions.mAllowHotSwapping)
  6060. {
  6061. if (!hotSwapErrors.IsEmpty())
  6062. hotSwapErrors += ", ";
  6063. hotSwapErrors += project->mName;
  6064. }
  6065. if (mOptions.mToolsetType != BfToolsetType_LLVM)
  6066. {
  6067. if (!toolsetErrors.IsEmpty())
  6068. toolsetErrors += ", ";
  6069. toolsetErrors += project->mName;
  6070. }
  6071. }
  6072. }
  6073. if (!hotSwapErrors.IsEmpty())
  6074. mPassInstance->Fail(StrFormat("Hot compilation cannot be used when LTO is enabled in '%s'. Consider setting 'Workspace/Beef/Debug/Enable Hot Compilation' to 'No'.", hotSwapErrors.c_str()));
  6075. if (!toolsetErrors.IsEmpty())
  6076. mPassInstance->Fail(StrFormat("The Workspace Toolset must be set to 'LLVM' in order to use LTO in '%s'. Consider changing 'Workspace/Targeted/Build/Toolset' to 'LLVM'.", toolsetErrors.c_str()));
  6077. }
  6078. //
  6079. {
  6080. String attribName;
  6081. mAttributeTypeOptionMap.Clear();
  6082. for (int typeOptionsIdx = 0; typeOptionsIdx < (int)mSystem->mTypeOptions.size(); typeOptionsIdx++)
  6083. {
  6084. auto& typeOptions = mSystem->mTypeOptions[typeOptionsIdx];
  6085. for (auto& attributeFilter : typeOptions.mAttributeFilters)
  6086. {
  6087. attribName = attributeFilter;
  6088. attribName += "Attribute";
  6089. Array<int>* arrPtr = NULL;
  6090. mAttributeTypeOptionMap.TryAdd(attribName, NULL, &arrPtr);
  6091. arrPtr->Add(typeOptionsIdx);
  6092. }
  6093. }
  6094. }
  6095. mRebuildFileSet.Clear();
  6096. // Inc revision for next run through Compile
  6097. mRevision++;
  6098. mHasComptimeRebuilds = false;
  6099. int revision = mRevision;
  6100. BfLogSysM("Compile Start. Revision: %d. HasParser:%d AutoComplete:%d\n", revision,
  6101. (mResolvePassData != NULL) && (!mResolvePassData->mParsers.IsEmpty()),
  6102. (mResolvePassData != NULL) && (mResolvePassData->mAutoComplete != NULL));
  6103. if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
  6104. mContext->mUnreifiedModule->mIsReified = true;
  6105. else
  6106. mContext->mUnreifiedModule->mIsReified = false;
  6107. if (mCeMachine != NULL)
  6108. mCeMachine->CompileStarted();
  6109. if (mOptions.mAllowHotSwapping)
  6110. {
  6111. if (mHotData == NULL)
  6112. {
  6113. mHotData = new HotData();
  6114. mHotData->mCompiler = this;
  6115. }
  6116. }
  6117. else
  6118. {
  6119. delete mHotData;
  6120. mHotData = NULL;
  6121. }
  6122. if (IsHotCompile())
  6123. {
  6124. if (!mOptions.mAllowHotSwapping)
  6125. {
  6126. mPassInstance->Fail("Hot Compilation is not enabled");
  6127. return true;
  6128. }
  6129. if (mHotState == NULL)
  6130. {
  6131. mHotState = new HotState();
  6132. mHotState->mHotProject = mOptions.mHotProject;
  6133. }
  6134. else
  6135. {
  6136. // It should be impossible to switch hot projects without a non-hot compile between them
  6137. BF_ASSERT(mHotState->mHotProject == mOptions.mHotProject);
  6138. }
  6139. }
  6140. else
  6141. {
  6142. for (auto& kv : mContext->mSavedTypeDataMap)
  6143. {
  6144. auto savedTypeData = kv.mValue;
  6145. delete savedTypeData->mHotTypeData;
  6146. savedTypeData->mHotTypeData = NULL;
  6147. }
  6148. delete mHotState;
  6149. mHotState = NULL;
  6150. // This will get rid of any old method data so we don't have any more mPrevVersions
  6151. ClearOldHotData();
  6152. }
  6153. int prevUnfinishedModules = mStats.mModulesStarted - mStats.mModulesFinished;
  6154. mCompletionPct = 0;
  6155. memset(&mStats, 0, sizeof(mStats));
  6156. mCodeGen.ClearResults();
  6157. mCodeGen.ResetStats();
  6158. mStats.mModulesStarted = prevUnfinishedModules;
  6159. if ((mLastRevisionAborted) && (!mIsResolveOnly))
  6160. {
  6161. auto _AddCount = [&](BfModule* module)
  6162. {
  6163. if (module->mAddedToCount)
  6164. {
  6165. if (module->mIsReified)
  6166. mStats.mReifiedModuleCount++;
  6167. }
  6168. };
  6169. for (auto mainModule : mContext->mModules)
  6170. {
  6171. _AddCount(mainModule);
  6172. for (auto specKV : mainModule->mSpecializedMethodModules)
  6173. {
  6174. _AddCount(specKV.mValue);
  6175. }
  6176. }
  6177. }
  6178. if (IsHotCompile())
  6179. {
  6180. mContext->EnsureHotMangledVirtualMethodNames();
  6181. }
  6182. mOutputDirectory = outputDirectory;
  6183. mSystem->StartYieldSection();
  6184. mExtraCompileRequested = false;
  6185. mFastFinish = false;
  6186. mHasQueuedTypeRebuilds = false;
  6187. mCanceling = false;
  6188. mSystem->CheckLockYield();
  6189. #ifdef WANT_COMPILE_LOG
  6190. if (!mIsResolveOnly)
  6191. {
  6192. mCompileLogFP = fopen(StrFormat("compile%d.txt", mRevision).c_str(), "wb");
  6193. }
  6194. #endif
  6195. BfTypeDef* typeDef;
  6196. BfLogSysM("UpdateRevisedTypes Revision %d. ResolvePass:%d CursorIdx:%d\n", mRevision, mIsResolveOnly,
  6197. ((mResolvePassData == NULL) || (mResolvePassData->mParsers.IsEmpty())) ? - 1 : mResolvePassData->mParsers[0]->mCursorIdx);
  6198. mCompileState = CompileState_Normal;
  6199. UpdateRevisedTypes();
  6200. // We need to defer processing the graveyard until here, because mLookupResults contain atom references so we need to make sure
  6201. // those aren't deleted until we can properly handle it.
  6202. mSystem->ProcessAtomGraveyard();
  6203. BpEnter("Compile_Start");
  6204. mHasRequiredTypes = true;
  6205. //HashSet<BfTypeDef*> internalTypeDefs;
  6206. auto _GetRequiredType = [&](const StringImpl& typeName, int genericArgCount = 0)
  6207. {
  6208. auto typeDef = mSystem->FindTypeDef(typeName, genericArgCount);
  6209. if (typeDef == NULL)
  6210. {
  6211. mPassInstance->Fail(StrFormat("Unable to find system type: %s", typeName.c_str()));
  6212. mHasRequiredTypes = false;
  6213. }
  6214. return typeDef;
  6215. };
  6216. _GetRequiredType("System.Void");
  6217. _GetRequiredType("System.Boolean");
  6218. _GetRequiredType("System.Int");
  6219. _GetRequiredType("System.Int8");
  6220. _GetRequiredType("System.Int16");
  6221. _GetRequiredType("System.Int32");
  6222. _GetRequiredType("System.Int64");
  6223. _GetRequiredType("System.UInt");
  6224. _GetRequiredType("System.UInt8");
  6225. _GetRequiredType("System.UInt16");
  6226. _GetRequiredType("System.UInt32");
  6227. _GetRequiredType("System.UInt64");
  6228. _GetRequiredType("System.Char8");
  6229. _GetRequiredType("System.Char16");
  6230. mChar32TypeDef = _GetRequiredType("System.Char32");
  6231. mFloatTypeDef = _GetRequiredType("System.Float");
  6232. mDoubleTypeDef = _GetRequiredType("System.Double");
  6233. mMathTypeDef = _GetRequiredType("System.Math");
  6234. mBfObjectTypeDef = _GetRequiredType("System.Object");
  6235. mArray1TypeDef = _GetRequiredType("System.Array1", 1);
  6236. mArray2TypeDef = _GetRequiredType("System.Array2", 1);
  6237. mArray3TypeDef = _GetRequiredType("System.Array3", 1);
  6238. mArray4TypeDef = _GetRequiredType("System.Array4", 1);
  6239. mSpanTypeDef = _GetRequiredType("System.Span", 1);
  6240. mRangeTypeDef = _GetRequiredType("System.Range");
  6241. mClosedRangeTypeDef = _GetRequiredType("System.ClosedRange");
  6242. mIndexTypeDef = _GetRequiredType("System.Index");
  6243. mIndexRangeTypeDef = _GetRequiredType("System.IndexRange");
  6244. mAttributeTypeDef = _GetRequiredType("System.Attribute");
  6245. mAttributeUsageAttributeTypeDef = _GetRequiredType("System.AttributeUsageAttribute");
  6246. mClassVDataTypeDef = _GetRequiredType("System.ClassVData");
  6247. mCLinkAttributeTypeDef = _GetRequiredType("System.CLinkAttribute");
  6248. mImportAttributeTypeDef = _GetRequiredType("System.ImportAttribute");
  6249. mExportAttributeTypeDef = _GetRequiredType("System.ExportAttribute");
  6250. mCReprAttributeTypeDef = _GetRequiredType("System.CReprAttribute");
  6251. mUnderlyingArrayAttributeTypeDef = _GetRequiredType("System.UnderlyingArrayAttribute");
  6252. mAlignAttributeTypeDef = _GetRequiredType("System.AlignAttribute");
  6253. mAllowDuplicatesAttributeTypeDef = _GetRequiredType("System.AllowDuplicatesAttribute");
  6254. mNoDiscardAttributeTypeDef = _GetRequiredType("System.NoDiscardAttribute");
  6255. mDisableChecksAttributeTypeDef = _GetRequiredType("System.DisableChecksAttribute");
  6256. mDisableObjectAccessChecksAttributeTypeDef = _GetRequiredType("System.DisableObjectAccessChecksAttribute");
  6257. mDbgRawAllocDataTypeDef = _GetRequiredType("System.DbgRawAllocData");
  6258. mDeferredCallTypeDef = _GetRequiredType("System.DeferredCall");
  6259. mDelegateTypeDef = _GetRequiredType("System.Delegate");
  6260. mFunctionTypeDef = _GetRequiredType("System.Function");
  6261. mActionTypeDef = _GetRequiredType("System.Action");
  6262. mEnumTypeDef = _GetRequiredType("System.Enum");
  6263. mFriendAttributeTypeDef = _GetRequiredType("System.FriendAttribute");
  6264. mNoStaticCtorAttributeTypeDef = _GetRequiredType("System.NoStaticCtorAttribute");
  6265. mComptimeAttributeTypeDef = _GetRequiredType("System.ComptimeAttribute");
  6266. mIntrinsicAttributeTypeDef = _GetRequiredType("System.IntrinsicAttribute");
  6267. mConstEvalAttributeTypeDef = _GetRequiredType("System.ConstEvalAttribute");
  6268. mNoExtensionAttributeTypeDef = _GetRequiredType("System.NoExtensionAttribute");
  6269. mCheckedAttributeTypeDef = _GetRequiredType("System.CheckedAttribute");
  6270. mUncheckedAttributeTypeDef = _GetRequiredType("System.UncheckedAttribute");
  6271. mResultTypeDef = _GetRequiredType("System.Result", 1);
  6272. mGCTypeDef = _GetRequiredType("System.GC");
  6273. mGenericIEnumerableTypeDef = _GetRequiredType("System.Collections.IEnumerable", 1);
  6274. mGenericIEnumeratorTypeDef = _GetRequiredType("System.Collections.IEnumerator", 1);
  6275. mGenericIRefEnumeratorTypeDef = _GetRequiredType("System.Collections.IRefEnumerator", 1);
  6276. mInlineAttributeTypeDef = _GetRequiredType("System.InlineAttribute");
  6277. mThreadTypeDef = _GetRequiredType("System.Threading.Thread");
  6278. mInternalTypeDef = _GetRequiredType("System.Internal");
  6279. mPlatformTypeDef = _GetRequiredType("System.Platform");
  6280. mCompilerTypeDef = _GetRequiredType("System.Compiler");
  6281. mCompilerGeneratorTypeDef = _GetRequiredType("System.Compiler.Generator");
  6282. mDiagnosticsDebugTypeDef = _GetRequiredType("System.Diagnostics.Debug");
  6283. mIDisposableTypeDef = _GetRequiredType("System.IDisposable");
  6284. mIIntegerTypeDef = _GetRequiredType("System.IInteger");
  6285. mIPrintableTypeDef = _GetRequiredType("System.IPrintable");
  6286. mIHashableTypeDef = _GetRequiredType("System.IHashable");
  6287. mIComptimeTypeApply = _GetRequiredType("System.IComptimeTypeApply");
  6288. mIComptimeMethodApply = _GetRequiredType("System.IComptimeMethodApply");
  6289. mIOnTypeInitTypeDef = _GetRequiredType("System.IOnTypeInit");
  6290. mIOnTypeDoneTypeDef = _GetRequiredType("System.IOnTypeDone");
  6291. mIOnFieldInitTypeDef = _GetRequiredType("System.IOnFieldInit");
  6292. mIOnMethodInitTypeDef = _GetRequiredType("System.IOnMethodInit");
  6293. mLinkNameAttributeTypeDef = _GetRequiredType("System.LinkNameAttribute");
  6294. mCallingConventionAttributeTypeDef = _GetRequiredType("System.CallingConventionAttribute");
  6295. mMethodRefTypeDef = _GetRequiredType("System.MethodReference", 1);
  6296. mNullableTypeDef = _GetRequiredType("System.Nullable", 1);
  6297. mOrderedAttributeTypeDef = _GetRequiredType("System.OrderedAttribute");
  6298. mPointerTTypeDef = _GetRequiredType("System.Pointer", 1);
  6299. mPointerTypeDef = _GetRequiredType("System.Pointer", 0);
  6300. mReflectTypeIdTypeDef = _GetRequiredType("System.Reflection.TypeId");
  6301. mReflectArrayType = _GetRequiredType("System.Reflection.ArrayType");
  6302. mReflectGenericParamType = _GetRequiredType("System.Reflection.GenericParamType");
  6303. mReflectFieldDataDef = _GetRequiredType("System.Reflection.TypeInstance.FieldData");
  6304. mReflectFieldSplatDataDef = _GetRequiredType("System.Reflection.TypeInstance.FieldSplatData");
  6305. mReflectMethodDataDef = _GetRequiredType("System.Reflection.TypeInstance.MethodData");
  6306. mReflectParamDataDef = _GetRequiredType("System.Reflection.TypeInstance.ParamData");
  6307. mReflectInterfaceDataDef = _GetRequiredType("System.Reflection.TypeInstance.InterfaceData");
  6308. mReflectPointerType = _GetRequiredType("System.Reflection.PointerType");
  6309. mReflectRefType = _GetRequiredType("System.Reflection.RefType");
  6310. mReflectSizedArrayType = _GetRequiredType("System.Reflection.SizedArrayType");
  6311. mReflectConstExprType = _GetRequiredType("System.Reflection.ConstExprType");
  6312. mReflectSpecializedGenericType = _GetRequiredType("System.Reflection.SpecializedGenericType");
  6313. mReflectTypeInstanceTypeDef = _GetRequiredType("System.Reflection.TypeInstance");
  6314. mReflectUnspecializedGenericType = _GetRequiredType("System.Reflection.UnspecializedGenericType");
  6315. mReflectFieldInfoTypeDef = _GetRequiredType("System.Reflection.FieldInfo");
  6316. mReflectMethodInfoTypeDef = _GetRequiredType("System.Reflection.MethodInfo");
  6317. mSizedArrayTypeDef = _GetRequiredType("System.SizedArray", 2);
  6318. mStaticInitAfterAttributeTypeDef = _GetRequiredType("System.StaticInitAfterAttribute");
  6319. mStaticInitPriorityAttributeTypeDef = _GetRequiredType("System.StaticInitPriorityAttribute");
  6320. mStringTypeDef = _GetRequiredType("System.String");
  6321. mStringViewTypeDef = _GetRequiredType("System.StringView");
  6322. mTestAttributeTypeDef = _GetRequiredType("System.TestAttribute");
  6323. mThreadStaticAttributeTypeDef = _GetRequiredType("System.ThreadStaticAttribute");
  6324. mTypeTypeDeclDef = _GetRequiredType("System.TypeDeclaration");
  6325. mTypeTypeDef = _GetRequiredType("System.Type");
  6326. mUnboundAttributeTypeDef = _GetRequiredType("System.UnboundAttribute");
  6327. mValueTypeTypeDef = _GetRequiredType("System.ValueType");
  6328. mTupleTypeDef = _GetRequiredType("System.Tuple");
  6329. mObsoleteAttributeTypeDef = _GetRequiredType("System.ObsoleteAttribute");
  6330. mErrorAttributeTypeDef = _GetRequiredType("System.ErrorAttribute");
  6331. mWarnAttributeTypeDef = _GetRequiredType("System.WarnAttribute");
  6332. mConstSkipAttributeTypeDef = _GetRequiredType("System.ConstSkipAttribute");
  6333. mIgnoreErrorsAttributeTypeDef = _GetRequiredType("System.IgnoreErrorsAttribute");
  6334. mReflectAttributeTypeDef = _GetRequiredType("System.ReflectAttribute");
  6335. mOnCompileAttributeTypeDef = _GetRequiredType("System.OnCompileAttribute");
  6336. for (int i = 0; i < BfTypeCode_Length; i++)
  6337. mContext->mPrimitiveStructTypes[i] = NULL;
  6338. mContext->mBfTypeType = NULL;
  6339. mContext->mBfClassVDataPtrType = NULL;
  6340. if (!mHasRequiredTypes)
  6341. {
  6342. // Force rebuilding
  6343. BfLogSysM("Compile missing required types\n");
  6344. mOptions.mForceRebuildIdx++;
  6345. }
  6346. mSystem->CheckLockYield();
  6347. if (mBfObjectTypeDef != NULL)
  6348. mContext->mScratchModule->ResolveTypeDef(mBfObjectTypeDef);
  6349. VisitSourceExteriorNodes();
  6350. if (!mIsResolveOnly)
  6351. {
  6352. HashSet<BfModule*> foundVDataModuleSet;
  6353. for (auto bfProject : mSystem->mProjects)
  6354. {
  6355. if (bfProject->mDisabled)
  6356. continue;
  6357. if ((mBfObjectTypeDef != NULL) && (!bfProject->ContainsReference(mBfObjectTypeDef->mProject)))
  6358. {
  6359. mPassInstance->Fail(StrFormat("Project '%s' must reference core library '%s'", bfProject->mName.c_str(), mBfObjectTypeDef->mProject->mName.c_str()));
  6360. }
  6361. if ((bfProject->mTargetType != BfTargetType_BeefConsoleApplication) && (bfProject->mTargetType != BfTargetType_BeefWindowsApplication) &&
  6362. (bfProject->mTargetType != BfTargetType_BeefLib_DynamicLib) && (bfProject->mTargetType != BfTargetType_BeefLib_StaticLib) &&
  6363. (bfProject->mTargetType != BfTargetType_C_ConsoleApplication) && (bfProject->mTargetType != BfTargetType_C_WindowsApplication) &&
  6364. (bfProject->mTargetType != BfTargetType_BeefTest) &&
  6365. (bfProject->mTargetType != BfTargetType_BeefApplication_StaticLib) && (bfProject->mTargetType != BfTargetType_BeefApplication_DynamicLib))
  6366. continue;
  6367. if (bfProject->mTargetType == BfTargetType_BeefTest)
  6368. {
  6369. // Force internal test methods
  6370. auto bfModule = mContext->mScratchModule;
  6371. bfModule->GetInternalMethod("Test_Init");
  6372. bfModule->GetInternalMethod("Test_Query");
  6373. bfModule->GetInternalMethod("Test_Finish");
  6374. }
  6375. bool found = false;
  6376. for (auto module : mVDataModules)
  6377. {
  6378. if (module->mProject == bfProject)
  6379. {
  6380. found = true;
  6381. foundVDataModuleSet.Add(module);
  6382. //module->StartNewRevision();
  6383. }
  6384. }
  6385. if (!found)
  6386. {
  6387. auto module = new BfVDataModule(mContext);
  6388. module->mProject = bfProject;
  6389. module->Init();
  6390. module->FinishInit();
  6391. module->mIsSpecialModule = true;
  6392. BF_ASSERT(!mContext->mLockModules);
  6393. mContext->mModules.push_back(module);
  6394. mVDataModules.push_back(module);
  6395. foundVDataModuleSet.Add(module);
  6396. }
  6397. }
  6398. // Remove old vdata
  6399. for (int moduleIdx = 0; moduleIdx < (int) mVDataModules.size(); moduleIdx++)
  6400. {
  6401. auto module = mVDataModules[moduleIdx];
  6402. if (!foundVDataModuleSet.Contains(module))
  6403. {
  6404. delete module;
  6405. mVDataModules.erase(mVDataModules.begin() + moduleIdx);
  6406. moduleIdx--;
  6407. mContext->mModules.Remove(module);
  6408. }
  6409. }
  6410. }
  6411. if (mIsResolveOnly)
  6412. VisitAutocompleteExteriorIdentifiers();
  6413. mStats.mTypesQueued = 0;
  6414. mStats.mMethodsQueued = 0;
  6415. mStats.mTypesQueued += (int)mContext->mPopulateTypeWorkList.size();
  6416. mStats.mMethodsQueued += (int)mContext->mMethodWorkList.size();
  6417. while (true)
  6418. {
  6419. //
  6420. {
  6421. if (mBfObjectTypeDef != NULL)
  6422. mContext->mScratchModule->ResolveTypeDef(mBfObjectTypeDef, BfPopulateType_Full);
  6423. mContext->RemapObject();
  6424. mSystem->CheckLockYield();
  6425. mWantsDeferMethodDecls = mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude;
  6426. CompileReified();
  6427. mWantsDeferMethodDecls = false;
  6428. }
  6429. BpLeave();
  6430. BpEnter("Compile_End");
  6431. mContext->mHasReifiedQueuedRebuildTypes = false;
  6432. //
  6433. {
  6434. BP_ZONE("ProcessingLiveness");
  6435. for (auto type : mContext->mResolvedTypes)
  6436. {
  6437. auto depType = type->ToDependedType();
  6438. if (depType != NULL)
  6439. depType->mRebuildFlags = (BfTypeRebuildFlags)(depType->mRebuildFlags | BfTypeRebuildFlag_AwaitingReference);
  6440. }
  6441. bool didWork = false;
  6442. UpdateDependencyMap(mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_ResolveUnused, didWork);
  6443. // If UpdateDependencyMap caused methods to be reified, then we need to run PopulateReified again-
  6444. // because those methods may be virtual and we need to reify overrides (for example).
  6445. // We use the DoWorkLoop result to determine if there were actually any changes from UpdateDependencyMap
  6446. if (didWork)
  6447. {
  6448. PopulateReified();
  6449. }
  6450. }
  6451. if (!mContext->mHasReifiedQueuedRebuildTypes)
  6452. break;
  6453. BfLogSysM("DoCompile looping over CompileReified due to mHasReifiedQueuedRebuildTypes\n");
  6454. }
  6455. // Handle purgatory (ie: old generic types)
  6456. {
  6457. bool didWork = ProcessPurgatory(true);
  6458. if (mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude)
  6459. {
  6460. if (DoWorkLoop())
  6461. didWork = true;
  6462. if (didWork)
  6463. PopulateReified();
  6464. }
  6465. }
  6466. // Mark used modules
  6467. if ((mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude) && (!mCanceling))
  6468. {
  6469. bool hadActualTarget = false;
  6470. if (!mIsResolveOnly)
  6471. {
  6472. SizedArray<BfModule*, 32> requiredModules;
  6473. for (auto type : mContext->mResolvedTypes)
  6474. {
  6475. auto typeInst = type->ToTypeInstance();
  6476. if (typeInst == NULL)
  6477. continue;
  6478. if (typeInst->mAlwaysIncludeFlags == BfAlwaysIncludeFlag_None)
  6479. continue;
  6480. if (typeInst->IsGenericTypeInstance())
  6481. {
  6482. if ((!typeInst->IsUnspecializedType()) || (typeInst->IsUnspecializedTypeVariation()))
  6483. continue;
  6484. }
  6485. auto requiredModule = typeInst->GetModule();
  6486. if (requiredModule != NULL)
  6487. requiredModules.push_back(requiredModule);
  6488. }
  6489. mContext->mReferencedIFaceSlots.Clear();
  6490. bool hasTests = false;
  6491. for (auto project : mSystem->mProjects)
  6492. {
  6493. if (project->mTargetType == BfTargetType_BeefTest)
  6494. hasTests = true;
  6495. project->mUsedModules.Clear();
  6496. project->mReferencedTypeData.Clear();
  6497. if (project->mDisabled)
  6498. continue;
  6499. if (project->mTargetType == BfTargetType_BeefLib)
  6500. continue;
  6501. hadActualTarget = true;
  6502. for (auto requiredModule : requiredModules)
  6503. {
  6504. mContext->MarkUsedModules(project, requiredModule);
  6505. }
  6506. String entryClassName = project->mStartupObject;
  6507. typeDef = mSystem->FindTypeDef(entryClassName, 0, project, {}, NULL, BfFindTypeDefFlag_AllowGlobal);
  6508. if (typeDef != NULL)
  6509. {
  6510. auto startupType = mContext->mScratchModule->ResolveTypeDef(typeDef);
  6511. if (startupType != NULL)
  6512. {
  6513. auto startupTypeInst = startupType->ToTypeInstance();
  6514. if (startupTypeInst != NULL)
  6515. {
  6516. mContext->MarkUsedModules(project, startupTypeInst->GetModule());
  6517. }
  6518. }
  6519. }
  6520. if (hasTests)
  6521. {
  6522. HashSet<BfProject*> projectSet;
  6523. for (auto type : mContext->mResolvedTypes)
  6524. {
  6525. auto typeInstance = type->ToTypeInstance();
  6526. if (typeInstance != NULL)
  6527. {
  6528. for (auto& methodInstanceGroup : typeInstance->mMethodInstanceGroups)
  6529. {
  6530. if (methodInstanceGroup.mDefault != NULL)
  6531. {
  6532. auto methodInstance = methodInstanceGroup.mDefault;
  6533. auto project = methodInstance->mMethodDef->mDeclaringType->mProject;
  6534. if (project->mTargetType != BfTargetType_BeefTest)
  6535. continue;
  6536. if ((methodInstance->GetCustomAttributes() != NULL) &&
  6537. (methodInstance->GetCustomAttributes()->Contains(mTestAttributeTypeDef)))
  6538. {
  6539. projectSet.Add(project);
  6540. }
  6541. }
  6542. }
  6543. if (!projectSet.IsEmpty())
  6544. {
  6545. for (auto project : projectSet)
  6546. mContext->MarkUsedModules(project, typeInstance->mModule);
  6547. projectSet.Clear();
  6548. }
  6549. }
  6550. }
  6551. }
  6552. }
  6553. // Leave types reified when hot compiling
  6554. if ((!IsHotCompile()) && (hadActualTarget))
  6555. mContext->TryUnreifyModules();
  6556. }
  6557. }
  6558. // Generate slot nums
  6559. if ((!mIsResolveOnly) && (!mCanceling))
  6560. {
  6561. if ((!IsHotCompile()) || (mHotState->mHasNewInterfaceTypes))
  6562. {
  6563. int prevSlotCount = mMaxInterfaceSlots;
  6564. GenerateSlotNums();
  6565. if ((prevSlotCount != -1) && (prevSlotCount != mMaxInterfaceSlots))
  6566. {
  6567. mInterfaceSlotCountChanged = true;
  6568. }
  6569. if (mHotState != NULL)
  6570. mHotState->mHasNewInterfaceTypes = false;
  6571. }
  6572. }
  6573. // Resolve unused types
  6574. if ((mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_ResolveUnused) && (!mCanceling))
  6575. {
  6576. // Finish off any outstanding modules so we can code generate in parallel with handling the unreified stuff
  6577. for (auto module : mContext->mModules)
  6578. {
  6579. if (!module->mIsSpecialModule)
  6580. {
  6581. if ((module->HasCompiledOutput()) && (module->mIsModuleMutable))
  6582. {
  6583. module->Finish();
  6584. }
  6585. }
  6586. }
  6587. DoWorkLoop();
  6588. BfLogSysM("Compile QueueUnused\n");
  6589. mCompileState = BfCompiler::CompileState_Unreified;
  6590. BpLeave();
  6591. BpEnter("Compile_QueueUnused");
  6592. while (true)
  6593. {
  6594. BP_ZONE("Compile_QueueUnused");
  6595. bool queuedMoreMethods = false;
  6596. int startTypeInitCount = mTypeInitCount;
  6597. for (auto typeDef : mSystem->mTypeDefs)
  6598. {
  6599. mSystem->CheckLockYield();
  6600. if (mCanceling)
  6601. {
  6602. BfLogSysM("Canceling from Compile typeDef loop\n");
  6603. break;
  6604. }
  6605. if (typeDef->mProject->mDisabled)
  6606. continue;
  6607. if (typeDef->mIsPartial)
  6608. continue;
  6609. if (typeDef->mTypeCode == BfTypeCode_Extension)
  6610. continue;
  6611. mContext->mUnreifiedModule->ResolveTypeDef(typeDef, BfPopulateType_Full);
  6612. }
  6613. Array<BfTypeInstance*> typeWorkList;
  6614. Array<BfType*> typeList;
  6615. typeList.Reserve(mContext->mResolvedTypes.GetCount());
  6616. for (auto type : mContext->mResolvedTypes)
  6617. typeList.Add(type);
  6618. for (auto type : typeList)
  6619. {
  6620. auto module = type->GetModule();
  6621. if (module == NULL)
  6622. continue;
  6623. if ((type->IsIncomplete()) && (type->IsTypeInstance()) && (!type->IsSpecializedType()))
  6624. {
  6625. mSystem->CheckLockYield();
  6626. module->PopulateType(type, BfPopulateType_Full);
  6627. }
  6628. auto typeInst = type->ToTypeInstance();
  6629. if (typeInst == NULL)
  6630. continue;
  6631. if (typeInst->IsUnspecializedTypeVariation())
  6632. continue;
  6633. if (!typeInst->IsSpecializedType())
  6634. {
  6635. typeWorkList.Add(typeInst);
  6636. }
  6637. }
  6638. for (auto typeInst : typeWorkList)
  6639. {
  6640. // Find any remaining methods for unreified processing
  6641. for (auto&& methodInstGroup : typeInst->mMethodInstanceGroups)
  6642. {
  6643. if ((methodInstGroup.mOnDemandKind == BfMethodOnDemandKind_Decl_AwaitingReference) ||
  6644. (methodInstGroup.mOnDemandKind == BfMethodOnDemandKind_NoDecl_AwaitingReference))
  6645. {
  6646. if ((methodInstGroup.mDefault != NULL) && (methodInstGroup.mDefault->mIsForeignMethodDef))
  6647. {
  6648. mContext->mUnreifiedModule->GetMethodInstance(typeInst, methodInstGroup.mDefault->mMethodDef, BfTypeVector(),
  6649. (BfGetMethodInstanceFlags)(BfGetMethodInstanceFlag_ForeignMethodDef | BfGetMethodInstanceFlag_UnspecializedPass | BfGetMethodInstanceFlag_ExplicitResolveOnlyPass));
  6650. queuedMoreMethods = true;
  6651. }
  6652. else
  6653. {
  6654. auto methodDef = typeInst->mTypeDef->mMethods[methodInstGroup.mMethodIdx];
  6655. if (methodDef->mMethodType == BfMethodType_Init)
  6656. continue;
  6657. mContext->mUnreifiedModule->GetMethodInstance(typeInst, methodDef, BfTypeVector(),
  6658. (BfGetMethodInstanceFlags)(BfGetMethodInstanceFlag_UnspecializedPass | BfGetMethodInstanceFlag_ExplicitResolveOnlyPass));
  6659. queuedMoreMethods = true;
  6660. }
  6661. }
  6662. }
  6663. }
  6664. if ((!queuedMoreMethods) && (startTypeInitCount == mTypeInitCount))
  6665. break;
  6666. DoWorkLoop();
  6667. }
  6668. bool didWork = false;
  6669. UpdateDependencyMap(true, didWork);
  6670. bool hadReifiedRebuild = false;
  6671. for (auto entry : mContext->mPopulateTypeWorkList)
  6672. {
  6673. if ((entry != NULL) && (entry->mType->IsReified()))
  6674. hadReifiedRebuild = true;
  6675. }
  6676. // Deleting types can cause reified types to rebuild, so allow that
  6677. mCompileState = BfCompiler::CompileState_Normal;
  6678. DoWorkLoop();
  6679. if ((hadReifiedRebuild) && (!mIsResolveOnly))
  6680. CompileReified();
  6681. }
  6682. else
  6683. {
  6684. DoWorkLoop();
  6685. }
  6686. ProcessPurgatory(false);
  6687. // ProcessPurgatory MAY cause type rebuilds which we need to handle
  6688. DoWorkLoop();
  6689. BfLogSysM("Checking mDepsMayHaveDeletedTypes for SanitizeDependencyMap\n");
  6690. if (mDepsMayHaveDeletedTypes)
  6691. SanitizeDependencyMap();
  6692. // Old Mark used modules
  6693. if (!mIsResolveOnly)
  6694. {
  6695. // if ((!mPassInstance->HasFailed()) && (!mCanceling))
  6696. // {
  6697. // if ((!IsHotCompile()) || (mHotState->mHasNewInterfaceTypes))
  6698. // {
  6699. // GenerateSlotNums();
  6700. // if (mHotState != NULL)
  6701. // mHotState->mHasNewInterfaceTypes = false;
  6702. // }
  6703. // }
  6704. if ((!mPassInstance->HasFailed()) && (!mCanceling))
  6705. {
  6706. if (!mOptions.mAllowHotSwapping)
  6707. {
  6708. GenerateDynCastData();
  6709. mContext->ProcessWorkList(false, false);
  6710. }
  6711. mCompileState = BfCompiler::CompileState_VData;
  6712. for (auto vdataModule : mVDataModules)
  6713. CreateVData(vdataModule);
  6714. for (auto vdataModule : mVDataModules)
  6715. FixVDataHash(vdataModule);
  6716. mCompileState = BfCompiler::CompileState_Normal;
  6717. }
  6718. // Don't clear out unused string pool entries while we are hot swapping, because we want string literals
  6719. // to still be the same pointer if it's erased and then put back
  6720. if ((!IsHotCompile()) && (!mCanceling))
  6721. ClearUnusedStringPoolEntries();
  6722. mContext->ValidateDependencies();
  6723. mContext->UpdateAfterDeletingTypes();
  6724. }
  6725. // We need to check the specialized errors before writing out modules --
  6726. // this call is responsible for deleting dead method specializations that contained errors, or for setting
  6727. // the mHadBuildErrors on the module if there was a method specialization error that didn't die
  6728. mContext->CheckSpecializedErrorData();
  6729. mContext->Finish();
  6730. if ((!mIsResolveOnly) && (!IsHotCompile()))
  6731. ClearOldHotData();
  6732. mPassInstance->TryFlushDeferredError();
  6733. BpLeave();
  6734. BpEnter("Compile_Finish");
  6735. int frontendTicks = (int)(BFTickCount() - frontendStartTick);
  6736. //TODO:!!
  6737. //mCanceling = true;
  6738. String moduleListStr;
  6739. int numModulesWritten = 0;
  6740. if (!mCanceling)
  6741. {
  6742. if (!mIsResolveOnly)
  6743. {
  6744. int idx = 0;
  6745. BF_ASSERT(mContext->mMethodWorkList.IsEmpty());
  6746. //bfContext->mLockModules = true;
  6747. for (int moduleIdx = 0; moduleIdx < (int)mContext->mModules.size(); moduleIdx++)
  6748. {
  6749. //bool clearModule = false;
  6750. auto mainModule = mContext->mModules[moduleIdx];
  6751. BfModule* bfModule = mainModule;
  6752. if (bfModule->mIsReified)
  6753. {
  6754. auto itr = mainModule->mSpecializedMethodModules.begin();
  6755. while (true)
  6756. {
  6757. if (bfModule->mIsModuleMutable)
  6758. {
  6759. //clearModule = true;
  6760. // Note that Finish will just return immediately if we have errors, we don't write out modules with errors
  6761. // The 'mLastModuleWrittenRevision' will not be updated in the case.
  6762. bfModule->Finish();
  6763. mainModule->mRevision = std::max(mainModule->mRevision, bfModule->mRevision);
  6764. }
  6765. if (bfModule->mLastModuleWrittenRevision == mRevision)
  6766. {
  6767. if (!moduleListStr.empty())
  6768. moduleListStr += ", ";
  6769. moduleListStr += bfModule->mModuleName;
  6770. numModulesWritten++;
  6771. }
  6772. if (bfModule->mParentModule != NULL)
  6773. {
  6774. for (auto& fileName : bfModule->mOutFileNames)
  6775. {
  6776. if (!mainModule->mOutFileNames.Contains(fileName))
  6777. mainModule->mOutFileNames.push_back(fileName);
  6778. }
  6779. }
  6780. if (bfModule->mNextAltModule != NULL)
  6781. {
  6782. bfModule = bfModule->mNextAltModule;
  6783. }
  6784. else
  6785. {
  6786. if (itr == mainModule->mSpecializedMethodModules.end())
  6787. break;
  6788. bfModule = itr->mValue;
  6789. ++itr;
  6790. }
  6791. }
  6792. }
  6793. mainModule->ClearModule();
  6794. }
  6795. //bfContext->mLockModules = false;
  6796. }
  6797. else
  6798. {
  6799. bool isTargeted = (mResolvePassData != NULL) && (!mResolvePassData->mParsers.IsEmpty());
  6800. if (!isTargeted)
  6801. {
  6802. for (auto bfModule : mContext->mModules)
  6803. {
  6804. if (bfModule->mIsModuleMutable)
  6805. {
  6806. bfModule->Finish();
  6807. bfModule->mRevision = std::max(bfModule->mRevision, bfModule->mRevision);
  6808. bfModule->ClearModuleData();
  6809. }
  6810. }
  6811. }
  6812. }
  6813. }
  6814. /*if (!moduleListStr.empty())
  6815. mPassInstance->OutputLine(StrFormat("%d modules generated: %s", numModulesWritten, moduleListStr.c_str()));*/
  6816. //CompileLog("%d object files written: %s\n", numModulesWritten, moduleListStr.c_str());
  6817. //printf("Compile done, waiting for finish\n");
  6818. while (true)
  6819. {
  6820. if (mCanceling)
  6821. mCodeGen.Cancel();
  6822. bool isDone = mCodeGen.Finish();
  6823. UpdateCompletion();
  6824. if (isDone)
  6825. break;
  6826. }
  6827. mCodeGen.ProcessErrors(mPassInstance, mCanceling);
  6828. mCeMachine->CompileDone();
  6829. // This has to happen after codegen because we may delete modules that are referenced in codegen
  6830. mContext->Cleanup();
  6831. if ((!IsHotCompile()) && (!mIsResolveOnly) && (!mCanceling))
  6832. {
  6833. // Only save 'saved type data' for temporarily-deleted types like on-demand types.
  6834. // If we don't reuse it within a compilation pass then we put those IDs up to be
  6835. // reused later. We don't do this for hot reloading because there are cases like
  6836. // a user renaming a type that we want to allow him to be able to undo and then
  6837. // hot-recompile successfully.
  6838. for (auto& kv : mContext->mSavedTypeDataMap)
  6839. {
  6840. auto savedTypeData = kv.mValue;
  6841. mTypeIdFreeList.Add(savedTypeData->mTypeId);
  6842. delete savedTypeData;
  6843. }
  6844. mContext->mSavedTypeDataMap.Clear();
  6845. mContext->mSavedTypeData.Clear();
  6846. }
  6847. #ifdef BF_PLATFORM_WINDOWS
  6848. if (!mIsResolveOnly)
  6849. {
  6850. if (!IsHotCompile())
  6851. {
  6852. // Remove individually-written object files from any libs that previously had them,
  6853. // in the case that lib settings changed (ie: switching a module from Og+ to O0)
  6854. for (auto mainModule : mContext->mModules)
  6855. {
  6856. BfModule* bfModule = mainModule;
  6857. if (bfModule->mIsReified)
  6858. {
  6859. for (auto& outFileName : bfModule->mOutFileNames)
  6860. {
  6861. if (outFileName.mModuleWritten)
  6862. BeLibManager::Get()->AddUsedFileName(outFileName.mFileName);
  6863. }
  6864. }
  6865. }
  6866. }
  6867. auto libManager = BeLibManager::Get();
  6868. libManager->Finish();
  6869. if (!libManager->mErrors.IsEmpty())
  6870. {
  6871. for (auto& error : libManager->mErrors)
  6872. mPassInstance->Fail(error);
  6873. // We need to rebuild everything just to force that lib to get repopulated
  6874. mOptions.mForceRebuildIdx++;
  6875. }
  6876. libManager->mErrors.Clear();
  6877. }
  6878. #endif
  6879. int numObjFilesWritten = 0;
  6880. for (auto& fileEntry : mCodeGen.mCodeGenFiles)
  6881. {
  6882. if (!fileEntry.mWasCached)
  6883. numObjFilesWritten++;
  6884. }
  6885. mPassInstance->OutputLine(StrFormat(":low %d module%s built, %d object file%s generated",
  6886. numModulesWritten, (numModulesWritten != 1) ? "s" : "",
  6887. numObjFilesWritten, (numObjFilesWritten != 1) ? "s" : ""));
  6888. if (!mIsResolveOnly)
  6889. {
  6890. mPassInstance->OutputLine(StrFormat(":med Frontend time: %0.2fs", frontendTicks / 1000.0f));
  6891. if ((mCeMachine != NULL) && (mCeMachine->mRevisionExecuteTime > 0))
  6892. {
  6893. mPassInstance->OutputLine(StrFormat(":med Comptime execution time: %0.2fs", mCeMachine->mRevisionExecuteTime / 1000.0f));
  6894. }
  6895. }
  6896. BpLeave();
  6897. mPassInstance->WriteErrorSummary();
  6898. if ((mCanceling) && (!mIsResolveOnly))
  6899. {
  6900. mPassInstance->Fail("Build canceled");
  6901. mContext->CancelWorkItems();
  6902. CompileLog("Compile canceled\n");
  6903. }
  6904. BfLogSysM("Compile Done. Revision:%d TypesPopulated:%d MethodsDeclared:%d MethodsProcessed:%d Canceled? %d\n", revision, mStats.mTypesPopulated, mStats.mMethodDeclarations, mStats.mMethodsProcessed, mCanceling);
  6905. UpdateCompletion();
  6906. if ((!mIsResolveOnly) && (!mPassInstance->HasFailed()) && (!mCanceling))
  6907. {
  6908. //BF_ASSERT(mCompletionPct >= 0.99999f);
  6909. }
  6910. if (mCompileLogFP != NULL)
  6911. {
  6912. fclose(mCompileLogFP);
  6913. mCompileLogFP = NULL;
  6914. }
  6915. UpdateCompletion();
  6916. mStats.mTotalTypes = mContext->mResolvedTypes.GetCount();
  6917. String compileInfo;
  6918. if (mIsResolveOnly)
  6919. compileInfo += StrFormat("ResolveOnly ResolveType:%d Parser:%d\n", mResolvePassData->mResolveType, !mResolvePassData->mParsers.IsEmpty());
  6920. compileInfo += StrFormat("TotalTypes:%d\nTypesPopulated:%d\nMethodsDeclared:%d\nMethodsProcessed:%d\nCanceled? %d\n", mStats.mTotalTypes, mStats.mTypesPopulated, mStats.mMethodDeclarations, mStats.mMethodsProcessed, mCanceling);
  6921. compileInfo += StrFormat("TypesPopulated:%d\n", mStats.mTypesPopulated);
  6922. compileInfo += StrFormat("MethodDecls:%d\nMethodsProcessed:%d\nModulesStarted:%d\nModulesFinished:%d\n", mStats.mMethodDeclarations, mStats.mMethodsProcessed, mStats.mModulesFinished);
  6923. BpEvent("CompileDone", compileInfo.c_str());
  6924. if (mHotState != NULL)
  6925. {
  6926. for (auto& fileEntry : mCodeGen.mCodeGenFiles)
  6927. {
  6928. if (fileEntry.mWasCached)
  6929. continue;
  6930. mHotState->mQueuedOutFiles.Add(fileEntry);
  6931. }
  6932. if (!mPassInstance->HasFailed())
  6933. {
  6934. // Clear these out when we know we've compiled without error
  6935. mHotState->mNewlySlottedTypeIds.Clear();
  6936. mHotState->mSlotDefineTypeIds.Clear();
  6937. }
  6938. }
  6939. mCompileState = BfCompiler::CompileState_None;
  6940. // extern MemReporter gBEMemReporter;
  6941. // extern int gBEMemReporterSize;
  6942. // gBEMemReporter.Report();
  6943. // int memReporterSize = gBEMemReporterSize;
  6944. mLastRevisionAborted = mCanceling;
  6945. bool didCancel = mCanceling;
  6946. mCanceling = false;
  6947. mContext->ValidateDependencies();
  6948. if (mNeedsFullRefresh)
  6949. {
  6950. mNeedsFullRefresh = false;
  6951. return false;
  6952. }
  6953. if (didCancel)
  6954. mLastHadComptimeRebuilds = mHasComptimeRebuilds || mLastHadComptimeRebuilds;
  6955. else
  6956. mLastHadComptimeRebuilds = mHasComptimeRebuilds;
  6957. return !didCancel && !mHasQueuedTypeRebuilds;
  6958. }
  6959. bool BfCompiler::Compile(const StringImpl& outputDirectory)
  6960. {
  6961. int passIdx = 0;
  6962. bool success = false;
  6963. while (true)
  6964. {
  6965. auto passState = mPassInstance->GetState();
  6966. success = DoCompile(outputDirectory);
  6967. if (!mExtraCompileRequested)
  6968. break;
  6969. mPassInstance->RestoreState(passState);
  6970. if (passIdx == 1)
  6971. break;
  6972. passIdx++;
  6973. }
  6974. if (!success)
  6975. return false;
  6976. if (mPassInstance->HasFailed())
  6977. return true;
  6978. if (!mInterfaceSlotCountChanged)
  6979. return true;
  6980. BfLogSysM("Interface slot count increased. Rebuilding relevant modules.\n");
  6981. mPassInstance->OutputLine("Interface slot count increased. Rebuilding relevant modules.");
  6982. // Recompile with the increased slot count
  6983. success = DoCompile(outputDirectory);
  6984. BF_ASSERT(!mInterfaceSlotCountChanged);
  6985. return success;
  6986. }
  6987. void BfCompiler::ClearResults()
  6988. {
  6989. BP_ZONE("BfCompiler::ClearResults");
  6990. mCodeGen.ClearResults();
  6991. }
  6992. // Can should still leave the system in a state such that we when we save as much progress as possible while
  6993. // still leaving the system in a state that the next attempt at compile will resume with a valid state
  6994. // Canceling will still process the pending PopulateType calls but may leave items in the method worklist.
  6995. // Note that Cancel is an async request to cancel
  6996. void BfCompiler::Cancel()
  6997. {
  6998. mCanceling = true;
  6999. mFastFinish = true;
  7000. mHadCancel = true;
  7001. if (mCeMachine != NULL)
  7002. {
  7003. AutoCrit autoCrit(mCeMachine->mCritSect);
  7004. mCeMachine->mSpecialCheck = true;
  7005. mFastFinish = true;
  7006. }
  7007. BfLogSysM("BfCompiler::Cancel\n");
  7008. BpEvent("BfCompiler::Cancel", "");
  7009. }
  7010. void BfCompiler::RequestFastFinish()
  7011. {
  7012. mFastFinish = true;
  7013. if (mCeMachine != NULL)
  7014. mCeMachine->mSpecialCheck = true;
  7015. BfLogSysM("BfCompiler::RequestFastFinish\n");
  7016. BpEvent("BfCompiler::RequestFastFinish", "");
  7017. }
  7018. void BfCompiler::RequestExtraCompile()
  7019. {
  7020. mExtraCompileRequested = true;
  7021. }
  7022. //#define WANT_COMPILE_LOG
  7023. void BfCompiler::CompileLog(const char* fmt ...)
  7024. {
  7025. #ifdef WANT_COMPILE_LOG
  7026. if (mCompileLogFP == NULL)
  7027. return;
  7028. //static int lineNum = 0;
  7029. //lineNum++;
  7030. va_list argList;
  7031. va_start(argList, fmt);
  7032. String aResult = vformat(fmt, argList);
  7033. va_end(argList);
  7034. //aResult = StrFormat("%d ", lineNum) + aResult;
  7035. fwrite(aResult.c_str(), 1, aResult.length(), mCompileLogFP);
  7036. #endif
  7037. }
  7038. void BfCompiler::ReportMemory(MemReporter* memReporter)
  7039. {
  7040. AutoCrit crit(mSystem->mDataLock);
  7041. {
  7042. AutoMemReporter autoMemReporter(memReporter, "Context");
  7043. mContext->ReportMemory(memReporter);
  7044. }
  7045. for (auto type : mContext->mResolvedTypes)
  7046. {
  7047. AutoMemReporter autoMemReporter(memReporter, "Types");
  7048. type->ReportMemory(memReporter);
  7049. }
  7050. for (auto module : mContext->mModules)
  7051. {
  7052. AutoMemReporter autoMemReporter(memReporter, "Modules");
  7053. module->ReportMemory(memReporter);
  7054. }
  7055. {
  7056. AutoMemReporter autoMemReporter(memReporter, "ScratchModule");
  7057. mContext->mScratchModule->ReportMemory(memReporter);
  7058. }
  7059. for (auto vdataModule : mVDataModules)
  7060. {
  7061. AutoMemReporter autoMemReporter(memReporter, "VDataModules");
  7062. vdataModule->ReportMemory(memReporter);
  7063. }
  7064. if (mHotData != NULL)
  7065. {
  7066. AutoMemReporter autoMemReporter(memReporter, "HotData");
  7067. memReporter->Add(sizeof(HotData));
  7068. memReporter->AddMap(mHotData->mMethodMap);
  7069. for (auto& kv : mHotData->mMethodMap)
  7070. {
  7071. memReporter->AddStr(kv.mKey);
  7072. memReporter->Add(sizeof(BfHotMethod));
  7073. memReporter->AddVec(kv.mValue->mReferences);
  7074. }
  7075. }
  7076. if (mHotState != NULL)
  7077. {
  7078. AutoMemReporter autoMemReporter(memReporter, "HotState");
  7079. memReporter->Add(sizeof(HotState));
  7080. memReporter->AddVec(mHotState->mQueuedOutFiles, false);
  7081. memReporter->AddHashSet(mHotState->mSlotDefineTypeIds, false);
  7082. memReporter->AddHashSet(mHotState->mPendingDataChanges, false);
  7083. memReporter->AddMap(mHotState->mDeletedTypeNameMap, false);
  7084. for (auto& kv : mHotState->mDeletedTypeNameMap)
  7085. {
  7086. memReporter->AddStr(kv.mKey, false);
  7087. }
  7088. }
  7089. }
  7090. //////////////////////////////////////////////////////////////////////////
  7091. void BfCompiler::GenerateAutocompleteInfo()
  7092. {
  7093. BP_ZONE("BfCompiler::GetAutocompleteInfo");
  7094. String& autoCompleteResultString = *gTLStrReturn.Get();
  7095. autoCompleteResultString.Clear();
  7096. auto bfModule = mResolvePassData->mAutoComplete->mModule;
  7097. if (bfModule != NULL)
  7098. {
  7099. auto autoComplete = mResolvePassData->mAutoComplete;
  7100. if (autoComplete->mResolveType == BfResolveType_GetNavigationData)
  7101. return; // Already handled
  7102. if (autoComplete->mResolveType == BfResolveType_GetResultString)
  7103. {
  7104. autoCompleteResultString = autoComplete->mResultString;
  7105. return;
  7106. }
  7107. if (autoComplete->mUncertain)
  7108. autoCompleteResultString += "uncertain\n";
  7109. if (autoComplete->mDefaultSelection.length() != 0)
  7110. autoCompleteResultString += StrFormat("select\t%s\n", autoComplete->mDefaultSelection.c_str());
  7111. auto _EncodeTypeDef = [] (BfTypeDef* typeDef)
  7112. {
  7113. StringT<128> typeName = typeDef->mProject->mName;
  7114. typeName += ":";
  7115. typeName += typeDef->mFullName.ToString();
  7116. if (!typeDef->mGenericParamDefs.IsEmpty())
  7117. typeName += StrFormat("`%d", (int)typeDef->mGenericParamDefs.size());
  7118. return typeName;
  7119. };
  7120. if (autoComplete->mResolveType == BfResolveType_GetSymbolInfo)
  7121. {
  7122. if (autoComplete->mDefTypeGenericParamIdx != -1)
  7123. {
  7124. autoCompleteResultString += StrFormat("typeGenericParam\t%d\n", autoComplete->mDefTypeGenericParamIdx);
  7125. autoCompleteResultString += StrFormat("typeRef\t%s\n", _EncodeTypeDef(autoComplete->mDefType).c_str());
  7126. }
  7127. else if (autoComplete->mDefMethodGenericParamIdx != -1)
  7128. {
  7129. autoCompleteResultString += StrFormat("methodGenericParam\t%d\n", autoComplete->mDefMethodGenericParamIdx);
  7130. autoCompleteResultString += StrFormat("methodRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
  7131. }
  7132. else if ((autoComplete->mReplaceLocalId != -1) && (autoComplete->mDefMethod != NULL))
  7133. {
  7134. autoCompleteResultString += StrFormat("localId\t%d\n", autoComplete->mReplaceLocalId);
  7135. autoCompleteResultString += StrFormat("methodRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
  7136. }
  7137. else if (autoComplete->mDefField != NULL)
  7138. {
  7139. autoCompleteResultString += StrFormat("fieldRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefField->mIdx);
  7140. }
  7141. else if (autoComplete->mDefProp != NULL)
  7142. {
  7143. autoCompleteResultString += StrFormat("propertyRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefProp->mIdx);
  7144. }
  7145. else if (autoComplete->mDefMethod != NULL)
  7146. {
  7147. if (autoComplete->mDefMethod->mMethodType == BfMethodType_Ctor)
  7148. autoCompleteResultString += StrFormat("ctorRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
  7149. else
  7150. autoCompleteResultString += StrFormat("methodRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
  7151. }
  7152. else if (autoComplete->mDefType != NULL)
  7153. {
  7154. autoCompleteResultString += StrFormat("typeRef\t%s\n", _EncodeTypeDef(autoComplete->mDefType).c_str());
  7155. }
  7156. else if (!autoComplete->mDefNamespace.IsEmpty())
  7157. {
  7158. autoCompleteResultString += StrFormat("namespaceRef\t%s\n", autoComplete->mDefNamespace.ToString().c_str());
  7159. }
  7160. if (autoComplete->mInsertEndIdx > 0)
  7161. {
  7162. if (!mResolvePassData->mParsers.IsEmpty())
  7163. {
  7164. if (mResolvePassData->mParsers[0]->mSrc[autoComplete->mInsertEndIdx - 1] == '!')
  7165. autoComplete->mInsertEndIdx--;
  7166. }
  7167. }
  7168. }
  7169. const char* wantsDocEntry = NULL;
  7170. if (!autoComplete->mDocumentationEntryName.IsEmpty())
  7171. wantsDocEntry = autoComplete->mDocumentationEntryName.c_str();
  7172. if (autoComplete->mInsertStartIdx != -1)
  7173. {
  7174. autoCompleteResultString += StrFormat("insertRange\t%d %d\n", autoComplete->mInsertStartIdx, autoComplete->mInsertEndIdx);
  7175. }
  7176. if ((autoComplete->mDefMethod == NULL) && (autoComplete->mGetDefinitionNode == NULL) && (autoComplete->mIsGetDefinition) && (autoComplete->mMethodMatchInfo != NULL))
  7177. {
  7178. // Take loc from methodMatchInfo
  7179. if (autoComplete->mMethodMatchInfo->mInstanceList.size() > 0)
  7180. {
  7181. int bestIdx = autoComplete->mMethodMatchInfo->mBestIdx;
  7182. auto typeInst = autoComplete->mMethodMatchInfo->mInstanceList[bestIdx].mTypeInstance;
  7183. auto methodDef = autoComplete->mMethodMatchInfo->mInstanceList[bestIdx].mMethodDef;
  7184. if (methodDef->mMethodDeclaration != NULL)
  7185. {
  7186. auto ctorDecl = BfNodeDynCast<BfConstructorDeclaration>(methodDef->mMethodDeclaration);
  7187. if (ctorDecl != NULL)
  7188. autoComplete->SetDefinitionLocation(ctorDecl->mThisToken);
  7189. else
  7190. autoComplete->SetDefinitionLocation(methodDef->GetMethodDeclaration()->mNameNode);
  7191. }
  7192. else // Just select type then
  7193. autoComplete->SetDefinitionLocation(typeInst->mTypeDef->mTypeDeclaration->mNameNode);
  7194. }
  7195. }
  7196. if (autoComplete->mGetDefinitionNode != NULL)
  7197. {
  7198. auto astNode = autoComplete->mGetDefinitionNode;
  7199. auto bfSource = autoComplete->mGetDefinitionNode->GetSourceData()->ToParserData();
  7200. if (bfSource != NULL)
  7201. {
  7202. int line = 0;
  7203. int lineChar = 0;
  7204. bfSource->GetLineCharAtIdx(astNode->GetSrcStart(), line, lineChar);
  7205. autoCompleteResultString += StrFormat("defLoc\t%s\t%d\t%d\n", bfSource->mFileName.c_str(), line, lineChar);
  7206. }
  7207. }
  7208. auto methodMatchInfo = autoComplete->mMethodMatchInfo;
  7209. if ((methodMatchInfo != NULL) && (wantsDocEntry == NULL))
  7210. {
  7211. if ((methodMatchInfo->mInstanceList.size() > 0) && (methodMatchInfo->mBestIdx >= 0))
  7212. {
  7213. if (autoComplete->mIdentifierUsed != NULL)
  7214. {
  7215. String filter;
  7216. if (autoComplete->mIdentifierUsed != NULL)
  7217. autoComplete->mIdentifierUsed->ToString(filter);
  7218. auto& bestInstance = methodMatchInfo->mInstanceList[methodMatchInfo->mBestIdx];
  7219. auto bestMethodDef = bestInstance.mMethodDef;
  7220. if (bestMethodDef != NULL)
  7221. {
  7222. for (int paramIdx = 0; paramIdx < bestMethodDef->mParams.mSize; paramIdx++)
  7223. {
  7224. if ((paramIdx == 0) && (bestMethodDef->mMethodType == BfMethodType_Extension))
  7225. continue;
  7226. autoComplete->AddEntry(AutoCompleteEntry("param", bestMethodDef->mParams[paramIdx]->mName + ":"), filter);
  7227. }
  7228. }
  7229. }
  7230. String invokeInfoText;
  7231. invokeInfoText += StrFormat("%d", methodMatchInfo->mBestIdx);
  7232. for (int srcPosIdx = 0; srcPosIdx < (int)methodMatchInfo->mSrcPositions.size(); srcPosIdx++)
  7233. invokeInfoText += StrFormat(" %d", methodMatchInfo->mSrcPositions[srcPosIdx]);
  7234. autoCompleteResultString += "invokeInfo\t";
  7235. autoCompleteResultString += invokeInfoText;
  7236. autoCompleteResultString += "\n";
  7237. }
  7238. int idx = 0;
  7239. for (auto& methodEntry : methodMatchInfo->mInstanceList)
  7240. {
  7241. String methodText;
  7242. if (methodEntry.mPayloadEnumField != NULL)
  7243. {
  7244. auto payloadFieldDef = methodEntry.mPayloadEnumField->GetFieldDef();
  7245. methodText += payloadFieldDef->mName;
  7246. methodText += "(\x1";
  7247. auto payloadType = methodEntry.mPayloadEnumField->mResolvedType;
  7248. BF_ASSERT(payloadType->IsTuple());
  7249. if (payloadType->IsTuple())
  7250. {
  7251. auto tupleType = (BfTypeInstance*)payloadType;
  7252. for (int fieldIdx = 0; fieldIdx < (int)tupleType->mFieldInstances.size(); fieldIdx++)
  7253. {
  7254. auto fieldInstance = &tupleType->mFieldInstances[fieldIdx];
  7255. auto fieldDef = fieldInstance->GetFieldDef();
  7256. if (fieldIdx > 0)
  7257. methodText += ",\x1 ";
  7258. methodText += bfModule->TypeToString(fieldInstance->mResolvedType, BfTypeNameFlag_ResolveGenericParamNames);
  7259. if (!fieldDef->IsUnnamedTupleField())
  7260. {
  7261. methodText += " ";
  7262. if (fieldDef->mName.StartsWith("_"))
  7263. methodText += fieldDef->mName.Substring(1);
  7264. else
  7265. methodText += fieldDef->mName;
  7266. }
  7267. }
  7268. }
  7269. methodText += "\x1)";
  7270. }
  7271. else
  7272. {
  7273. BfMethodInstance* methodInstance = NULL;
  7274. if (methodEntry.mMethodDef->mIdx < 0)
  7275. {
  7276. for (auto localMethod : mContext->mLocalMethodGraveyard)
  7277. {
  7278. if (localMethod->mMethodDef == methodEntry.mMethodDef)
  7279. {
  7280. methodInstance = localMethod->mMethodInstanceGroup->mDefault;
  7281. break;
  7282. }
  7283. }
  7284. }
  7285. else
  7286. methodInstance = bfModule->GetRawMethodInstanceAtIdx(methodEntry.mTypeInstance, methodEntry.mMethodDef->mIdx);
  7287. auto curMethodInstance = methodInstance;
  7288. curMethodInstance = methodMatchInfo->mCurMethodInstance;
  7289. SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(bfModule->mCurTypeInstance, methodMatchInfo->mCurTypeInstance);
  7290. SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(bfModule->mCurMethodInstance, curMethodInstance);
  7291. Array<String> genericMethodNameOverrides;
  7292. Array<String>* genericMethodNameOverridesPtr = NULL;
  7293. if (methodInstance->GetNumGenericArguments() != 0)
  7294. {
  7295. genericMethodNameOverridesPtr = &genericMethodNameOverrides;
  7296. for (int methodGenericArgIdx = 0; methodGenericArgIdx < (int)methodInstance->GetNumGenericArguments(); methodGenericArgIdx++)
  7297. {
  7298. BfType* methodGenericArg = NULL;
  7299. if (methodEntry.mGenericArguments.size() > 0)
  7300. methodGenericArg = methodEntry.mGenericArguments[methodGenericArgIdx];
  7301. String argName;
  7302. if (methodGenericArg == NULL)
  7303. argName = methodInstance->mMethodDef->mGenericParams[methodGenericArgIdx]->mName;
  7304. else
  7305. argName = bfModule->TypeToString(methodGenericArg, BfTypeNameFlag_ResolveGenericParamNames, NULL);
  7306. genericMethodNameOverrides.push_back(argName);
  7307. }
  7308. }
  7309. if (methodInstance->mMethodDef->mMethodType == BfMethodType_Extension)
  7310. methodText += "(extension) ";
  7311. if (methodInstance->mMethodDef->mMethodType != BfMethodType_Ctor)
  7312. {
  7313. if (methodInstance->mReturnType != NULL)
  7314. methodText += bfModule->TypeToString(methodInstance->mReturnType, BfTypeNameFlag_ResolveGenericParamNames, genericMethodNameOverridesPtr);
  7315. else
  7316. methodText += BfTypeUtils::TypeToString(methodInstance->mMethodDef->mReturnTypeRef);
  7317. methodText += " ";
  7318. }
  7319. if (methodInstance->mMethodDef->mMethodType == BfMethodType_Ctor)
  7320. methodText += "this";
  7321. else
  7322. {
  7323. auto methodName = methodInstance->mMethodDef->mName;
  7324. int splitIdx = (int)methodName.IndexOf('@');
  7325. if (splitIdx != -1)
  7326. methodText += methodName.Substring(0, splitIdx);
  7327. else
  7328. methodText += methodName;
  7329. }
  7330. if (methodInstance->GetNumGenericArguments() != 0)
  7331. {
  7332. methodText += "<";
  7333. for (int methodGenericArgIdx = 0; methodGenericArgIdx < (int)methodInstance->GetNumGenericArguments(); methodGenericArgIdx++)
  7334. {
  7335. if (methodGenericArgIdx > 0)
  7336. methodText += ", ";
  7337. methodText += genericMethodNameOverrides[methodGenericArgIdx];
  7338. }
  7339. methodText += ">";
  7340. }
  7341. //TODO: Show default param values also
  7342. methodText += "(\x1";
  7343. if (methodInstance->GetParamCount() == 0)
  7344. {
  7345. // Hm - is this ever useful? Messes up some cases actually
  7346. // If param resolution failed then we need to print the original param def
  7347. /*for (int paramIdx = 0; paramIdx < (int) methodInstance->mMethodDef->mParams.size(); paramIdx++)
  7348. {
  7349. if (paramIdx > 0)
  7350. methodText += ",\x1 ";
  7351. auto paramDef = methodInstance->mMethodDef->mParams[paramIdx];
  7352. methodText += BfTypeUtils::TypeToString(paramDef->mTypeRef);
  7353. methodText += " ";
  7354. methodText += paramDef->mName;
  7355. }*/
  7356. }
  7357. int dispParamIdx = 0;
  7358. StringT<64> paramName;
  7359. for (int paramIdx = 0; paramIdx < (int)methodInstance->GetParamCount(); paramIdx++)
  7360. {
  7361. auto paramKind = methodInstance->GetParamKind(paramIdx);
  7362. if ((paramKind == BfParamKind_ImplicitCapture) || (paramKind == BfParamKind_AppendIdx))
  7363. continue;
  7364. if (dispParamIdx > 0)
  7365. methodText += ",\x1 ";
  7366. if ((paramIdx == 0) && (methodInstance->mMethodDef->mMethodType == BfMethodType_Extension))
  7367. continue;
  7368. auto type = methodInstance->GetParamType(paramIdx);
  7369. BfExpression* paramInitializer = methodInstance->GetParamInitializer(paramIdx);
  7370. if (paramInitializer != NULL)
  7371. methodText += "[";
  7372. if (paramKind == BfParamKind_Params)
  7373. methodText += "params ";
  7374. if (type->IsGenericParam())
  7375. {
  7376. auto genericParamType = (BfGenericParamType*)type;
  7377. if (genericParamType->mGenericParamKind == BfGenericParamKind_Method)
  7378. {
  7379. BfMethodInstance* checkMethodInstance = methodInstance;
  7380. if (checkMethodInstance->GetNumGenericParams() == 0)
  7381. checkMethodInstance = methodEntry.mCurMethodInstance;
  7382. bool handled = false;
  7383. if (genericParamType->mGenericParamIdx < checkMethodInstance->GetNumGenericParams())
  7384. {
  7385. auto genericParamInstance = checkMethodInstance->mMethodInfoEx->mGenericParams[genericParamType->mGenericParamIdx];
  7386. auto genericParamDef = genericParamInstance->GetGenericParamDef();
  7387. if (genericParamDef != NULL)
  7388. {
  7389. methodText += genericParamDef->mName;
  7390. handled = true;
  7391. }
  7392. }
  7393. if (!handled)
  7394. {
  7395. methodText += StrFormat("@M%d", genericParamType->mGenericParamIdx);
  7396. }
  7397. }
  7398. else
  7399. {
  7400. BfTypeInstance* genericType = methodEntry.mTypeInstance->ToGenericTypeInstance();
  7401. if (genericType == NULL)
  7402. {
  7403. if (methodEntry.mCurMethodInstance != NULL)
  7404. genericType = methodEntry.mCurMethodInstance->GetOwner()->ToGenericTypeInstance();
  7405. }
  7406. bool handled = false;
  7407. if ((genericType != NULL) && (genericParamType->mGenericParamIdx < (int)genericType->mGenericTypeInfo->mGenericParams.size()))
  7408. {
  7409. auto genericParamInstance = genericType->mGenericTypeInfo->mGenericParams[genericParamType->mGenericParamIdx];
  7410. auto genericParamDef = genericParamInstance->GetGenericParamDef();
  7411. if (genericParamDef != NULL)
  7412. {
  7413. methodText += genericParamDef->mName;
  7414. handled = true;
  7415. }
  7416. }
  7417. if (!handled)
  7418. {
  7419. methodText += StrFormat("@T%d", genericParamType->mGenericParamIdx);
  7420. }
  7421. }
  7422. }
  7423. else
  7424. methodText += bfModule->TypeToString(type, BfTypeNameFlag_ResolveGenericParamNames, genericMethodNameOverridesPtr);
  7425. int namePrefixCount = 0;
  7426. methodInstance->GetParamName(paramIdx, paramName, namePrefixCount);
  7427. if (!paramName.IsEmpty())
  7428. {
  7429. methodText += " ";
  7430. for (int i = 0; i < namePrefixCount; i++)
  7431. methodText += "@";
  7432. methodText += paramName;
  7433. }
  7434. if (paramInitializer != NULL)
  7435. {
  7436. methodText += " = ";
  7437. methodText += paramInitializer->ToString();
  7438. methodText += "]";
  7439. }
  7440. dispParamIdx++;
  7441. }
  7442. methodText += "\x1)";
  7443. }
  7444. autoCompleteResultString += "invoke\t" + methodText;
  7445. autoCompleteResultString += StrFormat("\t%d", methodEntry.mArgMatchCount);
  7446. if (methodEntry.mMethodDef != NULL)
  7447. {
  7448. auto methodDeclaration = methodEntry.mMethodDef->GetMethodDeclaration();
  7449. if ((methodDeclaration != NULL) && (methodDeclaration->mDocumentation != NULL))
  7450. {
  7451. String docString;
  7452. methodDeclaration->mDocumentation->GetDocString(docString);
  7453. autoCompleteResultString += "\x03";
  7454. autoCompleteResultString += docString;
  7455. }
  7456. }
  7457. autoCompleteResultString += "\n";
  7458. idx++;
  7459. }
  7460. }
  7461. Array<AutoCompleteEntry*> entries;
  7462. for (auto& entry : autoComplete->mEntriesSet)
  7463. {
  7464. entries.Add(&entry);
  7465. }
  7466. std::sort(entries.begin(), entries.end(), [](AutoCompleteEntry* lhs, AutoCompleteEntry* rhs)
  7467. {
  7468. if (lhs->mScore == rhs->mScore)
  7469. return stricmp(lhs->mDisplay, rhs->mDisplay) < 0;
  7470. return lhs->mScore > rhs->mScore;
  7471. });
  7472. String docString;
  7473. for (auto entry : entries)
  7474. {
  7475. if ((wantsDocEntry != NULL) && (entry->mDocumentation == NULL))
  7476. continue;
  7477. autoCompleteResultString += String(entry->mEntryType);
  7478. autoCompleteResultString += '\t';
  7479. for (int i = 0; i < entry->mNamePrefixCount; i++)
  7480. autoCompleteResultString += '@';
  7481. autoCompleteResultString += String(entry->mDisplay);
  7482. if (entry->mMatchesLength > 0)
  7483. {
  7484. autoCompleteResultString += "\x02";
  7485. for (int i = 0; i < entry->mMatchesLength; i++)
  7486. {
  7487. int match = entry->mMatches[i];
  7488. char buffer[16];
  7489. itoa(match, buffer, 16);
  7490. autoCompleteResultString += String(buffer);
  7491. autoCompleteResultString += ",";
  7492. }
  7493. autoCompleteResultString += "X";
  7494. }
  7495. if (entry->mDocumentation != NULL)
  7496. {
  7497. autoCompleteResultString += '\x03';
  7498. autoCompleteResultString += entry->mDocumentation;
  7499. }
  7500. autoCompleteResultString += '\n';
  7501. }
  7502. }
  7503. }
  7504. struct TypeDefMatchHelper
  7505. {
  7506. public:
  7507. struct SearchEntry
  7508. {
  7509. String mStr;
  7510. int mGenericCount;
  7511. SearchEntry()
  7512. {
  7513. mGenericCount = 0;
  7514. }
  7515. };
  7516. public:
  7517. StringImpl& mResult;
  7518. Array<SearchEntry> mSearch;
  7519. uint32 mFoundFlags;
  7520. int32 mFoundCount;
  7521. bool mHasDotSearch;
  7522. String mCurTypeName;
  7523. String mTempStr;
  7524. public:
  7525. TypeDefMatchHelper(StringImpl& str) : mResult(str)
  7526. {
  7527. mFoundFlags = 0;
  7528. mFoundCount = 0;
  7529. mHasDotSearch = false;
  7530. }
  7531. void Sanitize(StringImpl& str)
  7532. {
  7533. for (int i = 0; i < (int)str.length(); i++)
  7534. {
  7535. char c = str[i];
  7536. if (c < (char)32)
  7537. {
  7538. str[i] = ' ';
  7539. }
  7540. }
  7541. }
  7542. void AddParams(BfMethodDef* methodDef, StringImpl& result)
  7543. {
  7544. int visParamIdx = 0;
  7545. for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
  7546. {
  7547. auto paramDef = methodDef->mParams[paramIdx];
  7548. if ((paramDef->mParamKind == BfParamKind_AppendIdx) || (paramDef->mParamKind == BfParamKind_ImplicitCapture))
  7549. continue;
  7550. if (visParamIdx > 0)
  7551. result += ", ";
  7552. StringT<64> refName;
  7553. paramDef->mTypeRef->ToString(refName);
  7554. Sanitize(refName);
  7555. result += refName;
  7556. result += " ";
  7557. result += paramDef->mName;
  7558. visParamIdx++;
  7559. }
  7560. }
  7561. void AddLocation(BfAstNode* node)
  7562. {
  7563. if (node == NULL)
  7564. return;
  7565. auto parserData = node->GetSourceData()->ToParserData();
  7566. if (parserData != NULL)
  7567. {
  7568. mResult += parserData->mFileName;
  7569. int lineNum = 0;
  7570. int column = 0;
  7571. parserData->GetLineCharAtIdx(node->GetSrcStart(), lineNum, column);
  7572. mResult += StrFormat("\t%d\t%d", lineNum, column);
  7573. }
  7574. };
  7575. void AddFieldDef(BfFieldDef* fieldDef)
  7576. {
  7577. mResult += "\t";
  7578. AddLocation(fieldDef->GetRefNode());
  7579. mResult += "\n";
  7580. }
  7581. void AddPropertyDef(BfTypeDef* typeDef, BfPropertyDef* propDef)
  7582. {
  7583. if (propDef->mName == "[]")
  7584. {
  7585. mResult += "[";
  7586. for (auto methodDef : propDef->mMethods)
  7587. {
  7588. if (methodDef->mMethodType == BfMethodType_PropertyGetter)
  7589. {
  7590. AddParams(methodDef, mResult);
  7591. break;
  7592. }
  7593. }
  7594. mResult += "]";
  7595. }
  7596. else
  7597. mResult += propDef->mName;
  7598. mResult += "\t";
  7599. auto refNode = propDef->GetRefNode();
  7600. if (refNode == NULL)
  7601. refNode = typeDef->GetRefNode();
  7602. AddLocation(refNode);
  7603. mResult += "\n";
  7604. }
  7605. void GetGenericStr(BfMethodDef* methodDef, StringImpl& result)
  7606. {
  7607. if (methodDef->mGenericParams.IsEmpty())
  7608. return;
  7609. result += "<";
  7610. for (int i = 0; i < methodDef->mGenericParams.mSize; i++)
  7611. {
  7612. if (i > 0)
  7613. result += ", ";
  7614. result += methodDef->mGenericParams[i]->mName;
  7615. }
  7616. result += ">";
  7617. }
  7618. void GetMethodDefString(BfMethodDef* methodDef, StringImpl& result, int* genericStrIdx = NULL)
  7619. {
  7620. if (methodDef->mMethodType == BfMethodType_Ctor)
  7621. {
  7622. if (methodDef->mIsStatic)
  7623. result += "static ";
  7624. result += "this";
  7625. }
  7626. else if (methodDef->mMethodType == BfMethodType_Dtor)
  7627. {
  7628. if (methodDef->mIsStatic)
  7629. result += "static ";
  7630. result += "~this";
  7631. }
  7632. else
  7633. result += methodDef->mName;
  7634. if (methodDef->mMethodType == BfMethodType_Mixin)
  7635. result += "!";
  7636. if (!methodDef->mGenericParams.IsEmpty())
  7637. {
  7638. if (genericStrIdx != NULL)
  7639. {
  7640. *genericStrIdx = result.mLength;
  7641. }
  7642. else
  7643. {
  7644. GetGenericStr(methodDef, result);
  7645. }
  7646. }
  7647. result += "(";
  7648. AddParams(methodDef, result);
  7649. result += ")";
  7650. }
  7651. void AddMethodDef(BfMethodDef* methodDef, StringImpl* methodDefString = NULL)
  7652. {
  7653. if (methodDefString != NULL)
  7654. mResult += *methodDefString;
  7655. else
  7656. GetMethodDefString(methodDef, mResult);
  7657. mResult += "\t";
  7658. AddLocation(methodDef->GetRefNode());
  7659. mResult += "\n";
  7660. }
  7661. void ClearResults()
  7662. {
  7663. mFoundFlags = 0;
  7664. mFoundCount = 0;
  7665. }
  7666. bool MergeFlags(uint32 flags)
  7667. {
  7668. int flagIdx = 0;
  7669. while (flags > 0)
  7670. {
  7671. if (((flags & 1) != 0) && ((mFoundFlags & (1 << flagIdx)) == 0))
  7672. {
  7673. mFoundFlags |= (1 << flagIdx);
  7674. mFoundCount++;
  7675. }
  7676. flags >>= 1;
  7677. flagIdx++;
  7678. }
  7679. return mFoundCount == mSearch.mSize;
  7680. }
  7681. uint32 CheckMatch(const StringView& str, int startIdx = 0)
  7682. {
  7683. uint32 matchFlags = 0;
  7684. for (int i = startIdx; i < mSearch.mSize; i++)
  7685. {
  7686. auto& search = mSearch[i];
  7687. if (((mFoundFlags & (1 << i)) == 0) && (str.IndexOf(search.mStr, true) != -1))
  7688. {
  7689. bool genericMatches = true;
  7690. if (search.mGenericCount > 0)
  7691. {
  7692. genericMatches = false;
  7693. int countIdx = (int)str.LastIndexOf('`');
  7694. if (countIdx > 0)
  7695. {
  7696. int genericCount = atoi(str.mPtr + countIdx + 1);
  7697. genericMatches = ((genericCount == search.mGenericCount) || (search.mGenericCount == 1)) &&
  7698. (countIdx == (int)search.mStr.length());
  7699. }
  7700. }
  7701. if (genericMatches)
  7702. {
  7703. mFoundCount++;
  7704. matchFlags |= (1 << i);
  7705. mFoundFlags |= (1 << i);
  7706. }
  7707. }
  7708. }
  7709. return matchFlags;
  7710. }
  7711. bool CheckCompletesMatch(BfAtomComposite& name)
  7712. {
  7713. for (int i = 0; i < name.mSize; i++)
  7714. {
  7715. CheckMatch(name.mParts[i]->mString);
  7716. if (mFoundCount == mSearch.mSize)
  7717. return true;
  7718. }
  7719. return false;
  7720. }
  7721. bool IsFullMatch()
  7722. {
  7723. return mFoundCount == mSearch.mSize;
  7724. }
  7725. bool CheckMemberMatch(BfTypeDef* typeDef, const StringView& str)
  7726. {
  7727. if (CheckMatch(str) == 0)
  7728. {
  7729. if (mHasDotSearch)
  7730. {
  7731. mTempStr.Clear();
  7732. mTempStr += mCurTypeName;
  7733. mTempStr += ".";
  7734. mTempStr += str;
  7735. if (CheckMatch(mTempStr) == 0)
  7736. return false;
  7737. }
  7738. else
  7739. return false;
  7740. }
  7741. if ((IsFullMatch()) || (CheckCompletesMatch(typeDef->mFullName)))
  7742. return true;
  7743. return false;
  7744. }
  7745. };
  7746. String BfCompiler::GetTypeDefList(bool includeLocation)
  7747. {
  7748. String result;
  7749. TypeDefMatchHelper matchHelper(result);
  7750. BfProject* curProject = NULL;
  7751. Dictionary<BfProject*, int> projectIds;
  7752. for (auto typeDef : mSystem->mTypeDefs)
  7753. {
  7754. if (typeDef->mProject != curProject)
  7755. {
  7756. curProject = typeDef->mProject;
  7757. int* projectIdPtr;
  7758. if (projectIds.TryAdd(curProject, NULL, &projectIdPtr))
  7759. {
  7760. *projectIdPtr = (int)projectIds.size() - 1;
  7761. result += '+';
  7762. result += curProject->mName;
  7763. result += '\n';
  7764. }
  7765. else
  7766. {
  7767. char str[32];
  7768. sprintf(str, "=%d\n", *projectIdPtr);
  7769. result += str;
  7770. }
  7771. }
  7772. if (((!typeDef->mIsPartial) || (typeDef->mIsCombinedPartial)))
  7773. {
  7774. if (typeDef->IsGlobalsContainer())
  7775. {
  7776. result += 'g';
  7777. if (!typeDef->mNamespace.IsEmpty())
  7778. {
  7779. typeDef->mNamespace.ToString(result);
  7780. result += '.';
  7781. }
  7782. result += ":static\n";
  7783. continue;
  7784. }
  7785. else if (typeDef->mTypeCode == BfTypeCode_Interface)
  7786. result += 'i';
  7787. else if (typeDef->mTypeCode == BfTypeCode_Object)
  7788. result += 'c';
  7789. else
  7790. result += 'v';
  7791. String typeName = BfTypeUtils::TypeToString(typeDef, BfTypeNameFlag_InternalName);
  7792. if (includeLocation)
  7793. {
  7794. result += typeName + "\t";
  7795. matchHelper.AddLocation(typeDef->GetRefNode());
  7796. result += "\n";
  7797. }
  7798. else
  7799. {
  7800. result += typeName + "\n";
  7801. }
  7802. }
  7803. }
  7804. return result;
  7805. }
  7806. String BfCompiler::GetGeneratorString(BfTypeDef* typeDef, BfTypeInstance* typeInst, const StringImpl& generatorMethodName, const StringImpl* args)
  7807. {
  7808. if (typeInst == NULL)
  7809. {
  7810. auto type = mContext->mUnreifiedModule->ResolveTypeDef(typeDef, BfPopulateType_BaseType);
  7811. if (type != NULL)
  7812. typeInst = type->ToTypeInstance();
  7813. if (typeInst == NULL)
  7814. return "";
  7815. }
  7816. BfTypeVector typeVector;
  7817. typeVector.Add(typeInst);
  7818. auto generatorTypeInst = mContext->mUnreifiedModule->ResolveTypeDef(mCompilerGeneratorTypeDef)->ToTypeInstance();
  7819. auto methodDef = generatorTypeInst->mTypeDef->GetMethodByName(generatorMethodName);
  7820. auto moduleMethodInstance = mContext->mUnreifiedModule->GetMethodInstance(generatorTypeInst, methodDef, typeVector);
  7821. SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mContext->mUnreifiedModule->mCurMethodInstance, moduleMethodInstance.mMethodInstance);
  7822. SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mContext->mUnreifiedModule->mCurTypeInstance, typeInst);
  7823. BfExprEvaluator exprEvaluator(mContext->mUnreifiedModule);
  7824. exprEvaluator.mBfEvalExprFlags = (BfEvalExprFlags)(BfEvalExprFlags_Comptime | BfEvalExprFlags_NoCeRebuildFlags);
  7825. SizedArray<BfIRValue, 1> irArgs;
  7826. if (args != NULL)
  7827. irArgs.Add(mContext->mUnreifiedModule->GetStringObjectValue(*args));
  7828. auto callResult = exprEvaluator.CreateCall(NULL, moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc, false, irArgs, NULL, BfCreateCallFlags_None);
  7829. if (callResult.mValue.IsConst())
  7830. {
  7831. auto stringPtr = mContext->mUnreifiedModule->GetStringPoolString(callResult.mValue, mContext->mUnreifiedModule->mBfIRBuilder);
  7832. if (stringPtr != NULL)
  7833. return *stringPtr;
  7834. }
  7835. return "";
  7836. }
  7837. void BfCompiler::HandleGeneratorErrors(StringImpl& result)
  7838. {
  7839. if ((mPassInstance->mErrors.IsEmpty()) && (mPassInstance->mOutStream.IsEmpty()))
  7840. return;
  7841. result.Clear();
  7842. for (auto& msg : mPassInstance->mOutStream)
  7843. {
  7844. String error = msg;
  7845. error.Replace('\n', '\r');
  7846. result += "!error\t";
  7847. result += error;
  7848. result += "\n";
  7849. }
  7850. }
  7851. String BfCompiler::GetGeneratorTypeDefList()
  7852. {
  7853. String result;
  7854. BfProject* curProject = NULL;
  7855. Dictionary<BfProject*, int> projectIds;
  7856. BfResolvePassData resolvePassData;
  7857. SetAndRestoreValue<BfResolvePassData*> prevResolvePassData(mResolvePassData, &resolvePassData);
  7858. BfPassInstance passInstance(mSystem);
  7859. SetAndRestoreValue<BfPassInstance*> prevPassInstance(mPassInstance, &passInstance);
  7860. for (auto typeDef : mSystem->mTypeDefs)
  7861. {
  7862. if (typeDef->mProject->mDisabled)
  7863. continue;
  7864. if (typeDef->mIsPartial)
  7865. continue;
  7866. auto type = mContext->mUnreifiedModule->ResolveTypeDef(typeDef, BfPopulateType_BaseType);
  7867. if ((type != NULL) && (type->IsTypeInstance()))
  7868. {
  7869. auto typeInst = type->ToTypeInstance();
  7870. if ((typeInst->mBaseType != NULL) && (typeInst->mBaseType->IsInstanceOf(mCompilerGeneratorTypeDef)))
  7871. {
  7872. result += typeDef->mProject->mName;
  7873. result += ":";
  7874. result += BfTypeUtils::TypeToString(typeDef, BfTypeNameFlag_InternalName);
  7875. String nameString = GetGeneratorString(typeDef, typeInst, "GetName", NULL);
  7876. if (!nameString.IsEmpty())
  7877. result += "\t" + nameString;
  7878. result += "\n";
  7879. }
  7880. }
  7881. }
  7882. HandleGeneratorErrors(result);
  7883. return result;
  7884. }
  7885. String BfCompiler::GetGeneratorInitData(const StringImpl& typeName, const StringImpl& args)
  7886. {
  7887. BfResolvePassData resolvePassData;
  7888. SetAndRestoreValue<BfResolvePassData*> prevResolvePassData(mResolvePassData, &resolvePassData);
  7889. BfPassInstance passInstance(mSystem);
  7890. SetAndRestoreValue<BfPassInstance*> prevPassInstance(mPassInstance, &passInstance);
  7891. Array<BfTypeDef*> typeDefs;
  7892. GetTypeDefs(typeName, typeDefs);
  7893. String result;
  7894. for (auto typeDef : typeDefs)
  7895. {
  7896. result += GetGeneratorString(typeDef, NULL, "InitUI", &args);
  7897. if (!result.IsEmpty())
  7898. break;
  7899. }
  7900. HandleGeneratorErrors(result);
  7901. return result;
  7902. }
  7903. String BfCompiler::GetGeneratorGenData(const StringImpl& typeName, const StringImpl& args)
  7904. {
  7905. BfResolvePassData resolvePassData;
  7906. SetAndRestoreValue<BfResolvePassData*> prevResolvePassData(mResolvePassData, &resolvePassData);
  7907. BfPassInstance passInstance(mSystem);
  7908. SetAndRestoreValue<BfPassInstance*> prevPassInstance(mPassInstance, &passInstance);
  7909. Array<BfTypeDef*> typeDefs;
  7910. GetTypeDefs(typeName, typeDefs);
  7911. String result;
  7912. for (auto typeDef : typeDefs)
  7913. {
  7914. result += GetGeneratorString(typeDef, NULL, "Generate", &args);
  7915. if (!result.IsEmpty())
  7916. break;
  7917. }
  7918. HandleGeneratorErrors(result);
  7919. return result;
  7920. }
  7921. String BfCompiler::GetTypeDefMatches(const StringImpl& searchStr, bool includeLocation)
  7922. {
  7923. String result;
  7924. TypeDefMatchHelper matchHelper(result);
  7925. int openParenIdx = -1;
  7926. bool parenHasDot = false;
  7927. //
  7928. {
  7929. int searchIdx = 0;
  7930. while (searchIdx < (int)searchStr.length())
  7931. {
  7932. int spacePos = (int)searchStr.IndexOf(' ', searchIdx);
  7933. String str;
  7934. if (spacePos == -1)
  7935. str = searchStr.Substring(searchIdx);
  7936. else
  7937. str = searchStr.Substring(searchIdx, (int)(spacePos - searchIdx));
  7938. str.Trim();
  7939. TypeDefMatchHelper::SearchEntry searchEntry;
  7940. for (int i = 0; i < (int)str.length(); i++)
  7941. {
  7942. char c = str[i];
  7943. if (c == '<')
  7944. {
  7945. searchEntry.mGenericCount = 1;
  7946. searchEntry.mStr = str.Substring(0, i);
  7947. }
  7948. else if (searchEntry.mGenericCount > 0)
  7949. {
  7950. if (c == ',')
  7951. searchEntry.mGenericCount++;
  7952. }
  7953. }
  7954. if (searchEntry.mStr.IsEmpty())
  7955. searchEntry.mStr = str;
  7956. if (str.Contains('('))
  7957. {
  7958. if (str.Contains('.'))
  7959. parenHasDot = true;
  7960. if (openParenIdx == -1)
  7961. openParenIdx = matchHelper.mSearch.mSize;
  7962. }
  7963. if (!searchEntry.mStr.IsEmpty())
  7964. matchHelper.mSearch.Add(searchEntry);
  7965. if (str.Contains('.'))
  7966. matchHelper.mHasDotSearch = true;
  7967. if (spacePos == -1)
  7968. break;
  7969. searchIdx = spacePos + 1;
  7970. }
  7971. //// We sort from longest to shortest to make sure longer strings match before shorter, which
  7972. //// matters when the shorter string is a subset of the longer string
  7973. //matchHelper.mSearch.Sort([](const String& lhs, const String& rhs)
  7974. // {
  7975. // int lenCmp = (int)(rhs.length() - lhs.length());
  7976. // if (lenCmp != 0)
  7977. // return lenCmp < 0;
  7978. // return lhs < rhs;
  7979. // });
  7980. }
  7981. BfProject* curProject = NULL;
  7982. Dictionary<BfProject*, int> projectIds;
  7983. Dictionary<BfAtom*, int> atomMatchMap;
  7984. struct ProjectInfo
  7985. {
  7986. Dictionary<String, int> matchedNames;
  7987. };
  7988. Array<ProjectInfo> projectInfos;
  7989. projectInfos.Resize(mSystem->mProjects.size());
  7990. String typeName;
  7991. String foundName;
  7992. int partialIdx = 0;
  7993. for (auto typeDef : mSystem->mTypeDefs)
  7994. {
  7995. if (typeDef->mIsPartial)
  7996. continue;
  7997. bool fullyMatchesName = false;
  7998. if (matchHelper.mHasDotSearch)
  7999. {
  8000. matchHelper.mCurTypeName.Clear();
  8001. typeDef->mFullNameEx.ToString(matchHelper.mCurTypeName);
  8002. matchHelper.ClearResults();
  8003. matchHelper.CheckMatch(matchHelper.mCurTypeName);
  8004. fullyMatchesName = matchHelper.IsFullMatch();
  8005. }
  8006. int matchIdx = -1;
  8007. String tempStr;
  8008. if (!fullyMatchesName)
  8009. {
  8010. for (auto fieldDef : typeDef->mFields)
  8011. {
  8012. if (BfNodeIsA<BfPropertyDeclaration>(fieldDef->mFieldDeclaration))
  8013. continue;
  8014. matchHelper.ClearResults();
  8015. bool hasMatch = false;
  8016. if (matchHelper.CheckMemberMatch(typeDef, fieldDef->mName))
  8017. {
  8018. result += "F";
  8019. if (BfTypeUtils::TypeToString(result, typeDef, (BfTypeNameFlags)(BfTypeNameFlag_HideGlobalName | BfTypeNameFlag_InternalName)))
  8020. result += ".";
  8021. result += fieldDef->mName;
  8022. matchHelper.AddFieldDef(fieldDef);
  8023. }
  8024. }
  8025. for (auto propDef : typeDef->mProperties)
  8026. {
  8027. if (propDef->GetRefNode() == NULL)
  8028. continue;
  8029. matchHelper.ClearResults();
  8030. if (matchHelper.CheckMemberMatch(typeDef, propDef->mName))
  8031. {
  8032. result += "P";
  8033. if (BfTypeUtils::TypeToString(result, typeDef, (BfTypeNameFlags)(BfTypeNameFlag_HideGlobalName | BfTypeNameFlag_InternalName)))
  8034. result += ".";
  8035. matchHelper.AddPropertyDef(typeDef, propDef);
  8036. }
  8037. }
  8038. for (auto methodDef : typeDef->mMethods)
  8039. {
  8040. if ((methodDef->mMethodType != BfMethodType_Normal) &&
  8041. (methodDef->mMethodType != BfMethodType_Mixin) &&
  8042. (methodDef->mMethodType != BfMethodType_Ctor) &&
  8043. (methodDef->mMethodType != BfMethodType_Dtor))
  8044. continue;
  8045. if (methodDef->mMethodDeclaration == NULL)
  8046. continue;
  8047. matchHelper.ClearResults();
  8048. bool matches = matchHelper.CheckMemberMatch(typeDef, methodDef->mName);
  8049. bool hasTypeString = false;
  8050. bool hasMethodString = false;
  8051. int genericMethodParamIdx = -1;
  8052. if ((!matches) && (openParenIdx != -1))
  8053. {
  8054. hasMethodString = true;
  8055. tempStr.Clear();
  8056. if (parenHasDot)
  8057. {
  8058. hasTypeString = true;
  8059. if (BfTypeUtils::TypeToString(tempStr, typeDef, (BfTypeNameFlags)(BfTypeNameFlag_HideGlobalName | BfTypeNameFlag_InternalName)))
  8060. tempStr += ".";
  8061. }
  8062. matchHelper.GetMethodDefString(methodDef, tempStr, &genericMethodParamIdx);
  8063. matchHelper.CheckMatch(tempStr, openParenIdx);
  8064. matches = matchHelper.IsFullMatch();
  8065. }
  8066. if (matches)
  8067. {
  8068. if (methodDef->mIsOverride)
  8069. result += "o";
  8070. else
  8071. result += "M";
  8072. if (!hasTypeString)
  8073. {
  8074. if (BfTypeUtils::TypeToString(result, typeDef, (BfTypeNameFlags)(BfTypeNameFlag_HideGlobalName | BfTypeNameFlag_InternalName)))
  8075. result += ".";
  8076. }
  8077. if (hasMethodString)
  8078. {
  8079. if (genericMethodParamIdx != -1)
  8080. {
  8081. result += StringView(tempStr, 0, genericMethodParamIdx);
  8082. matchHelper.GetGenericStr(methodDef, result);
  8083. result += StringView(tempStr, genericMethodParamIdx);
  8084. tempStr.Clear();
  8085. matchHelper.AddMethodDef(methodDef, &tempStr);
  8086. }
  8087. else
  8088. matchHelper.AddMethodDef(methodDef, &tempStr);
  8089. }
  8090. else
  8091. matchHelper.AddMethodDef(methodDef);
  8092. }
  8093. }
  8094. uint32 matchFlags = 0;
  8095. for (int atomIdx = typeDef->mFullNameEx.mSize - 1; atomIdx >= 0; atomIdx--)
  8096. {
  8097. auto atom = typeDef->mFullNameEx.mParts[atomIdx];
  8098. int* matchesPtr = NULL;
  8099. if (atomMatchMap.TryAdd(atom, NULL, &matchesPtr))
  8100. {
  8101. matchHelper.ClearResults();
  8102. *matchesPtr = matchHelper.CheckMatch(atom->mString);
  8103. }
  8104. if (*matchesPtr != 0)
  8105. {
  8106. if (matchIdx == -1)
  8107. matchIdx = atomIdx;
  8108. matchFlags |= *matchesPtr;
  8109. }
  8110. }
  8111. matchHelper.ClearResults();
  8112. if (!matchHelper.MergeFlags(matchFlags))
  8113. {
  8114. continue;
  8115. }
  8116. }
  8117. if (typeDef->mProject != curProject)
  8118. {
  8119. curProject = typeDef->mProject;
  8120. int* projectIdPtr;
  8121. if (projectIds.TryAdd(curProject, NULL, &projectIdPtr))
  8122. {
  8123. *projectIdPtr = (int)projectIds.size() - 1;
  8124. result += "+";
  8125. result += curProject->mName;
  8126. result += "\n";
  8127. }
  8128. else
  8129. {
  8130. char str[32];
  8131. sprintf(str, "=%d\n", *projectIdPtr);
  8132. result += str;
  8133. }
  8134. }
  8135. typeName = BfTypeUtils::TypeToString(typeDef, BfTypeNameFlag_InternalName);
  8136. if (matchIdx != -1)
  8137. {
  8138. int* matchIdxPtr = 0;
  8139. auto projectInfo = &projectInfos[typeDef->mProject->mIdx];
  8140. int dotCount = 0;
  8141. foundName = typeName;
  8142. for (int i = 0; i < (int)typeName.length(); i++)
  8143. {
  8144. if (typeName[i] == '.')
  8145. {
  8146. if (dotCount == matchIdx)
  8147. {
  8148. foundName.Clear();
  8149. foundName.Append(typeName.c_str(), i);
  8150. break;
  8151. }
  8152. dotCount++;
  8153. }
  8154. }
  8155. if (projectInfo->matchedNames.TryAdd(foundName, NULL, &matchIdxPtr))
  8156. {
  8157. *matchIdxPtr = partialIdx++;
  8158. result += StrFormat(">%d@", matchIdx);
  8159. }
  8160. else
  8161. {
  8162. result += StrFormat("<%d@", *matchIdxPtr);
  8163. }
  8164. }
  8165. else
  8166. {
  8167. result += ":";
  8168. }
  8169. if (typeDef->IsGlobalsContainer())
  8170. {
  8171. result += "g";
  8172. if (!typeDef->mNamespace.IsEmpty())
  8173. {
  8174. typeDef->mNamespace.ToString(result);
  8175. result += ".";
  8176. }
  8177. result += ":static\n";
  8178. continue;
  8179. }
  8180. else if (typeDef->mTypeCode == BfTypeCode_Interface)
  8181. result += "i";
  8182. else if (typeDef->mTypeCode == BfTypeCode_Object)
  8183. result += "c";
  8184. else
  8185. result += "v";
  8186. if (includeLocation)
  8187. {
  8188. result += typeName + "\t";
  8189. matchHelper.AddLocation(typeDef->GetRefNode());
  8190. result += "\n";
  8191. }
  8192. else
  8193. {
  8194. result += typeName + "\n";
  8195. }
  8196. }
  8197. return result;
  8198. }
  8199. void BfCompiler::GetTypeDefs(const StringImpl& inTypeName, Array<BfTypeDef*>& typeDefs)
  8200. {
  8201. BfProject* project = NULL;
  8202. int idx = 0;
  8203. int sep = (int)inTypeName.LastIndexOf(':');
  8204. if (sep != -1)
  8205. {
  8206. int startProjName = (int)inTypeName.LastIndexOf(':', sep - 1) + 1;
  8207. idx = sep + 1;
  8208. project = mSystem->GetProject(inTypeName.Substring(startProjName, sep - startProjName));
  8209. }
  8210. String typeName;
  8211. int genericCount = 0;
  8212. int pendingGenericCount = 0;
  8213. for (; idx < (int)inTypeName.length(); idx++)
  8214. {
  8215. char c = inTypeName[idx];
  8216. if (c == '<')
  8217. genericCount = 1;
  8218. else if (genericCount > 0)
  8219. {
  8220. if (c == ',')
  8221. genericCount++;
  8222. else if (c == '>')
  8223. {
  8224. pendingGenericCount = genericCount;
  8225. genericCount = 0;
  8226. }
  8227. }
  8228. else
  8229. {
  8230. if (pendingGenericCount != 0)
  8231. {
  8232. typeName += StrFormat("`%d", pendingGenericCount);
  8233. pendingGenericCount = 0;
  8234. }
  8235. typeName += c;
  8236. }
  8237. }
  8238. bool isGlobals = false;
  8239. if (typeName == ":static")
  8240. {
  8241. typeName.clear();
  8242. isGlobals = true;
  8243. }
  8244. if (typeName.EndsWith(".:static"))
  8245. {
  8246. typeName.RemoveToEnd(typeName.length() - 8);
  8247. isGlobals = true;
  8248. }
  8249. for (int i = 0; i < (int)typeName.length(); i++)
  8250. if (typeName[i] == '+')
  8251. typeName[i] = '.';
  8252. BfAtomComposite nameComposite;
  8253. if ((typeName.IsEmpty()) || (mSystem->ParseAtomComposite(typeName, nameComposite)))
  8254. {
  8255. auto itr = mSystem->mTypeDefs.TryGet(nameComposite);
  8256. while (itr)
  8257. {
  8258. auto typeDef = *itr;
  8259. if ((!typeDef->mIsPartial) &&
  8260. (typeDef->mProject == project) &&
  8261. (typeDef->mFullName == nameComposite) &&
  8262. (typeDef->IsGlobalsContainer() == isGlobals) &&
  8263. (typeDef->GetSelfGenericParamCount() == pendingGenericCount))
  8264. {
  8265. typeDefs.Add(typeDef);
  8266. }
  8267. itr.MoveToNextHashMatch();
  8268. }
  8269. }
  8270. }
  8271. String BfCompiler::GetTypeDefInfo(const StringImpl& inTypeName)
  8272. {
  8273. Array<BfTypeDef*> typeDefs;
  8274. GetTypeDefs(inTypeName, typeDefs);
  8275. String result;
  8276. TypeDefMatchHelper matchHelper(result);
  8277. for (auto typeDef : typeDefs)
  8278. {
  8279. auto refNode = typeDef->GetRefNode();
  8280. result += "S";
  8281. matchHelper.AddLocation(refNode);
  8282. result += "\n";
  8283. for (auto fieldDef : typeDef->mFields)
  8284. {
  8285. result += "F";
  8286. result += fieldDef->mName;
  8287. matchHelper.AddFieldDef(fieldDef);
  8288. }
  8289. for (auto propDef : typeDef->mProperties)
  8290. {
  8291. if (propDef->GetRefNode() == NULL)
  8292. continue;
  8293. result += "P";
  8294. matchHelper.AddPropertyDef(typeDef, propDef);
  8295. }
  8296. for (auto methodDef : typeDef->mMethods)
  8297. {
  8298. if ((methodDef->mMethodType != BfMethodType_Normal) &&
  8299. (methodDef->mMethodType != BfMethodType_Mixin) &&
  8300. (methodDef->mMethodType != BfMethodType_Ctor) &&
  8301. (methodDef->mMethodType != BfMethodType_Dtor))
  8302. continue;
  8303. if (methodDef->mMethodDeclaration == NULL)
  8304. continue;
  8305. result += "M";
  8306. matchHelper.AddMethodDef(methodDef);
  8307. }
  8308. }
  8309. return result;
  8310. }
  8311. int BfCompiler::GetTypeId(const StringImpl& typeName)
  8312. {
  8313. auto type = GetType(typeName);
  8314. if (type != NULL)
  8315. return type->mTypeId;
  8316. return -1;
  8317. }
  8318. BfType* BfCompiler::GetType(const StringImpl& fullTypeName)
  8319. {
  8320. AutoCrit autoCrit(mSystem->mSystemLock);
  8321. BfPassInstance passInstance(mSystem);
  8322. BfProject* activeProject = NULL;
  8323. String typeName = fullTypeName;
  8324. int colonPos = (int)typeName.LastIndexOf(':');
  8325. if (colonPos != -1)
  8326. {
  8327. int startProjName = (int)typeName.LastIndexOf(':', colonPos - 1) + 1;
  8328. activeProject = mSystem->GetProject(typeName.Substring(startProjName, colonPos - startProjName));
  8329. typeName.Remove(0, colonPos + 1);
  8330. }
  8331. BfTypeState typeState;
  8332. typeState.mPrevState = mContext->mCurTypeState;
  8333. typeState.mActiveProject = activeProject;
  8334. SetAndRestoreValue<BfTypeState*> prevTypeState(mContext->mCurTypeState, &typeState);
  8335. BfParser parser(mSystem);
  8336. parser.SetSource(typeName.c_str(), (int)typeName.length());
  8337. parser.Parse(&passInstance);
  8338. BfReducer reducer;
  8339. reducer.mAlloc = parser.mAlloc;
  8340. reducer.mPassInstance = &passInstance;
  8341. if (parser.mRootNode->mChildArr.mSize == 0)
  8342. return NULL;
  8343. auto firstNode = parser.mRootNode->mChildArr[0];
  8344. auto endIdx = parser.mRootNode->mSrcEnd;
  8345. reducer.mVisitorPos = BfReducer::BfVisitorPos(parser.mRootNode);
  8346. reducer.mVisitorPos.MoveNext();
  8347. auto typeRef = reducer.CreateTypeRef(firstNode);
  8348. if (typeRef == NULL)
  8349. return NULL;
  8350. BfResolvePassData resolvePass;
  8351. SetAndRestoreValue<bool> prevIgnoreError(mContext->mScratchModule->mIgnoreErrors, true);
  8352. SetAndRestoreValue<bool> prevIgnoreWarnings(mContext->mScratchModule->mIgnoreWarnings, true);
  8353. SetAndRestoreValue<BfResolvePassData*> prevResolvePass(mResolvePassData, &resolvePass);
  8354. auto type = mContext->mScratchModule->ResolveTypeRef(typeRef, BfPopulateType_Identity, (BfResolveTypeRefFlags)(
  8355. BfResolveTypeRefFlag_NoCreate | BfResolveTypeRefFlag_AllowUnboundGeneric | BfResolveTypeRefFlag_AllowGlobalContainer));
  8356. if (type != NULL)
  8357. return type;
  8358. return NULL;
  8359. }
  8360. int BfCompiler::GetEmitSource(const StringImpl& fileName, StringImpl* outBuffer)
  8361. {
  8362. int lastDollarPos = (int)fileName.LastIndexOf('$');
  8363. if (lastDollarPos == -1)
  8364. return -1;
  8365. String typeName = fileName.Substring(lastDollarPos + 1);
  8366. int typeId = GetTypeId(typeName);
  8367. if ((typeId <= 0) || (typeId >= mContext->mTypes.mSize))
  8368. return -1;
  8369. auto type = mContext->mTypes[typeId];
  8370. if (type == NULL)
  8371. return -1;
  8372. auto typeInst = type->ToTypeInstance();
  8373. if (typeInst == NULL)
  8374. return -1;
  8375. auto typeDef = typeInst->mTypeDef;
  8376. if (typeDef->mEmitParent == NULL)
  8377. return -1;
  8378. auto emitParser = typeDef->mSource->ToParser();
  8379. if (emitParser == NULL)
  8380. return -1;
  8381. if (outBuffer != NULL)
  8382. outBuffer->Append(emitParser->mSrc, emitParser->mSrcLength);
  8383. return typeInst->mRevision;
  8384. }
  8385. String BfCompiler::GetEmitLocation(const StringImpl& typeName, int emitLine, int& outEmbedLine, int& outEmbedLineChar, uint64& outHash)
  8386. {
  8387. outEmbedLine = 0;
  8388. int typeId = GetTypeId(typeName);
  8389. if (typeId <= 0)
  8390. return "";
  8391. auto bfType = mContext->FindTypeById(typeId);
  8392. if (bfType == NULL)
  8393. return "";
  8394. auto typeInst = bfType->ToTypeInstance();
  8395. if (typeInst == NULL)
  8396. return "";
  8397. if (typeInst->mCeTypeInfo == NULL)
  8398. return "";
  8399. for (auto& kv : typeInst->mCeTypeInfo->mEmitSourceMap)
  8400. {
  8401. int partialIdx = (int)(kv.mKey >> 32);
  8402. int charIdx = (int)(kv.mKey & 0xFFFFFFFF);
  8403. auto typeDef = typeInst->mTypeDef;
  8404. if (partialIdx > 0)
  8405. typeDef = typeDef->mPartials[partialIdx];
  8406. auto origParser = typeDef->GetDefinition()->GetLastSource()->ToParser();
  8407. if (origParser == NULL)
  8408. continue;
  8409. auto emitParser = typeInst->mTypeDef->GetLastSource()->ToParser();
  8410. if (emitParser == NULL)
  8411. continue;
  8412. int startLine = 0;
  8413. int startLineChar = 0;
  8414. emitParser->GetLineCharAtIdx(kv.mValue.mSrcStart, startLine, startLineChar);
  8415. int endLine = 0;
  8416. int endLineChar = 0;
  8417. emitParser->GetLineCharAtIdx(kv.mValue.mSrcEnd - 1, endLine, endLineChar);
  8418. outHash = Hash64(emitParser->mSrc + kv.mValue.mSrcStart, kv.mValue.mSrcEnd - kv.mValue.mSrcStart);
  8419. if ((emitLine >= startLine) && (emitLine <= endLine))
  8420. {
  8421. origParser->GetLineCharAtIdx(charIdx, outEmbedLine, outEmbedLineChar);
  8422. return origParser->mFileName;
  8423. }
  8424. }
  8425. return "";
  8426. }
  8427. bool BfCompiler::WriteEmitData(const StringImpl& filePath, BfProject* project)
  8428. {
  8429. ZipFile zipFile;
  8430. for (auto type : mContext->mResolvedTypes)
  8431. {
  8432. auto typeInst = type->ToTypeInstance();
  8433. if (typeInst == NULL)
  8434. continue;
  8435. if (typeInst->mTypeDef->mEmitParent == NULL)
  8436. continue;
  8437. if (!project->ContainsReference(typeInst->mTypeDef->mProject))
  8438. continue;
  8439. auto bfParser = typeInst->mTypeDef->GetLastSource()->ToParser();
  8440. String name = bfParser->mFileName;
  8441. if (name.StartsWith("$Emit$"))
  8442. name.Remove(0, 6);
  8443. String path = EncodeFileName(name);
  8444. path.Append(".bf");
  8445. if (!zipFile.IsOpen())
  8446. {
  8447. if (!zipFile.Create(filePath))
  8448. return false;
  8449. }
  8450. zipFile.Add(path, Span<uint8>((uint8*)bfParser->mSrc, bfParser->mSrcLength));
  8451. }
  8452. if (zipFile.IsOpen())
  8453. return zipFile.Close();
  8454. return true;
  8455. }
  8456. //////////////////////////////////////////////////////////////////////////
  8457. PerfManager* BfGetPerfManager(BfParser* bfParser);
  8458. /*BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetDefaultTargetTriple(BfCompiler* bfCompiler)
  8459. {
  8460. String& autoCompleteResultString = *gTLStrReturn.Get();
  8461. return autoCompleteResultString.c_str();
  8462. }*/
  8463. BF_EXPORT bool BF_CALLTYPE BfCompiler_Compile(BfCompiler* bfCompiler, BfPassInstance* bfPassInstance, const char* outputPath)
  8464. {
  8465. BP_ZONE("BfCompiler_Compile");
  8466. SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, bfPassInstance);
  8467. bfCompiler->mPassInstance = bfPassInstance;
  8468. bfCompiler->Compile(outputPath);
  8469. return !bfCompiler->mPassInstance->HasFailed();
  8470. }
  8471. BF_EXPORT void BF_CALLTYPE BfCompiler_ClearResults(BfCompiler* bfCompiler)
  8472. {
  8473. bfCompiler->ClearResults();
  8474. }
  8475. BF_EXPORT bool BF_CALLTYPE BfCompiler_ClassifySource(BfCompiler* bfCompiler, BfPassInstance* bfPassInstance, BfResolvePassData* resolvePassData)
  8476. {
  8477. BP_ZONE("BfCompiler_ClassifySource");
  8478. bfCompiler->mSystem->AssertWeHaveLock();
  8479. String& autoCompleteResultString = *gTLStrReturn.Get();
  8480. autoCompleteResultString.clear();
  8481. bfPassInstance->mCompiler = bfCompiler;
  8482. for (auto parser : resolvePassData->mParsers)
  8483. bfPassInstance->mFilterErrorsTo.Add(parser->mSourceData);
  8484. bfPassInstance->mTrimMessagesToCursor = true;
  8485. SetAndRestoreValue<BfResolvePassData*> prevCompilerResolvePassData(bfCompiler->mResolvePassData, resolvePassData);
  8486. SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, bfPassInstance);
  8487. bool canceled = false;
  8488. if ((resolvePassData->mAutoComplete != NULL) && (!resolvePassData->mParsers.IsEmpty()))
  8489. {
  8490. bfCompiler->ProcessAutocompleteTempType();
  8491. }
  8492. else
  8493. canceled = !bfCompiler->Compile("");
  8494. return !canceled;
  8495. }
  8496. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetCollapseRegions(BfCompiler* bfCompiler, BfParser* bfParser, BfResolvePassData* resolvePassData, char* explicitEmitTypeNames)
  8497. {
  8498. bfCompiler->mSystem->AssertWeHaveLock();
  8499. String& outString = *gTLStrReturn.Get();
  8500. outString.Clear();
  8501. class CollapseVisitor : public BfElementVisitor
  8502. {
  8503. public:
  8504. BfAstNode* mParentNode;
  8505. BfParser* mParser;
  8506. String& mOutString;
  8507. HashSet<int> mEndsFound;
  8508. char mSeriesKind;
  8509. int mStartSeriesIdx;
  8510. int mEndSeriesIdx;
  8511. public:
  8512. CollapseVisitor(BfParser* parser, String& string) : mOutString(string)
  8513. {
  8514. mParser = parser;
  8515. mParentNode = NULL;
  8516. mSeriesKind = 0;
  8517. mStartSeriesIdx = -1;
  8518. mEndSeriesIdx = -1;
  8519. }
  8520. void UpdateSeries(BfAstNode* node, char kind)
  8521. {
  8522. if (mStartSeriesIdx != -1)
  8523. {
  8524. if ((node->mTriviaStart != mEndSeriesIdx) || (kind != mSeriesKind))
  8525. {
  8526. FlushSeries();
  8527. mStartSeriesIdx = node->mSrcStart;
  8528. }
  8529. }
  8530. else
  8531. mStartSeriesIdx = node->mSrcStart;
  8532. mSeriesKind = kind;
  8533. mEndSeriesIdx = BF_MIN(node->mSrcEnd, mParser->mSrcLength -1);
  8534. }
  8535. void FlushSeries()
  8536. {
  8537. if (mStartSeriesIdx != -1)
  8538. {
  8539. bool ownsLine = true;
  8540. for (int checkIdx = mStartSeriesIdx - 1; checkIdx >= 0; checkIdx--)
  8541. {
  8542. char c = mParser->mSrc[checkIdx];
  8543. if (c == '\n')
  8544. break;
  8545. if (!isspace((uint8)c))
  8546. {
  8547. ownsLine = false;
  8548. break;
  8549. }
  8550. }
  8551. int anchor = mStartSeriesIdx;
  8552. if (!ownsLine)
  8553. {
  8554. int checkLine = GetLineStartAfter(anchor);
  8555. if (checkLine != -1)
  8556. {
  8557. anchor = checkLine;
  8558. for (; anchor < mEndSeriesIdx; anchor++)
  8559. {
  8560. if (!::isspace((uint8)mParser->mSrc[anchor]))
  8561. break;
  8562. }
  8563. }
  8564. }
  8565. Add(anchor, mEndSeriesIdx, mSeriesKind);
  8566. }
  8567. mStartSeriesIdx = -1;
  8568. }
  8569. int GetLineStartAfter(int startIdx)
  8570. {
  8571. for (int i = startIdx; i < mParser->mSrcLength - 1; i++)
  8572. {
  8573. if (mParser->mSrc[i] == '\n')
  8574. return i + 1;
  8575. }
  8576. return -1;
  8577. }
  8578. int GetLineEndBefore(int startIdx)
  8579. {
  8580. for (int i = startIdx; i >= 1; i--)
  8581. {
  8582. if (mParser->mSrc[i] == '\n')
  8583. return i;
  8584. }
  8585. return -1;
  8586. }
  8587. void Add(int anchor, int end, char kind = '?', int minLines = 2)
  8588. {
  8589. if ((anchor == -1) || (end == -1))
  8590. return;
  8591. if (!mEndsFound.Add(end))
  8592. return;
  8593. int lineCount = 1;
  8594. for (int i = anchor; i < end; i++)
  8595. {
  8596. if (mParser->mSrc[i] == '\n')
  8597. {
  8598. lineCount++;
  8599. if (lineCount >= minLines)
  8600. break;
  8601. }
  8602. }
  8603. if (lineCount < minLines)
  8604. return;
  8605. char str[1024];
  8606. sprintf(str, "%c%d,%d\n", kind, anchor, end);
  8607. mOutString.Append(str);
  8608. }
  8609. void Add(BfAstNode* anchor, BfAstNode* end, char kind = '?', int minLines = 2)
  8610. {
  8611. if ((anchor == NULL) || (end == NULL))
  8612. return;
  8613. Add(anchor->mSrcStart, end->mSrcEnd - 1, kind, minLines);
  8614. }
  8615. virtual void Visit(BfMethodDeclaration* methodDeclaration) override
  8616. {
  8617. int anchorIdx = methodDeclaration->mSrcStart;
  8618. if (methodDeclaration->mNameNode != NULL)
  8619. anchorIdx = methodDeclaration->mNameNode->mSrcEnd - 1;
  8620. if (methodDeclaration->mCloseParen != NULL)
  8621. anchorIdx = methodDeclaration->mCloseParen->mSrcEnd - 1;
  8622. if (methodDeclaration->mBody != NULL)
  8623. Add(anchorIdx, methodDeclaration->mBody->mSrcEnd - 1, 'M');
  8624. BfElementVisitor::Visit(methodDeclaration);
  8625. }
  8626. virtual void Visit(BfNamespaceDeclaration* namespaceDeclaration) override
  8627. {
  8628. Add(namespaceDeclaration->mNamespaceNode, namespaceDeclaration->mBody, 'N');
  8629. BfElementVisitor::Visit(namespaceDeclaration);
  8630. }
  8631. virtual void Visit(BfUsingDirective* usingDirective) override
  8632. {
  8633. UpdateSeries(usingDirective, 'U');
  8634. BfElementVisitor::Visit(usingDirective);
  8635. }
  8636. virtual void Visit(BfTypeDeclaration* typeDeclaration) override
  8637. {
  8638. BfAstNode* anchor = typeDeclaration->mNameNode;
  8639. if (anchor == NULL)
  8640. anchor = typeDeclaration->mStaticSpecifier;
  8641. Add(anchor, typeDeclaration->mDefineNode, 'T');
  8642. BfElementVisitor::Visit(typeDeclaration);
  8643. }
  8644. virtual void Visit(BfPropertyDeclaration* properyDeclaration) override
  8645. {
  8646. Add(properyDeclaration->mNameNode, properyDeclaration->mDefinitionBlock, 'P');
  8647. BfElementVisitor::Visit(properyDeclaration);
  8648. }
  8649. virtual void Visit(BfIndexerDeclaration* indexerDeclaration) override
  8650. {
  8651. Add(indexerDeclaration->mThisToken, indexerDeclaration->mDefinitionBlock, 'P');
  8652. BfElementVisitor::Visit(indexerDeclaration);
  8653. }
  8654. virtual void Visit(BfPropertyMethodDeclaration* methodDeclaration) override
  8655. {
  8656. Add(methodDeclaration->mNameNode, methodDeclaration->mBody);
  8657. BfElementVisitor::Visit(methodDeclaration);
  8658. }
  8659. virtual void Visit(BfIfStatement* ifStatement) override
  8660. {
  8661. Add(ifStatement->mCloseParen, ifStatement->mTrueStatement);
  8662. if (auto elseBlock = BfNodeDynCast<BfBlock>(ifStatement->mFalseStatement))
  8663. Add(ifStatement->mElseToken, elseBlock);
  8664. BfElementVisitor::Visit(ifStatement);
  8665. }
  8666. virtual void Visit(BfRepeatStatement* repeatStatement) override
  8667. {
  8668. Add(repeatStatement->mRepeatToken, repeatStatement);
  8669. if (repeatStatement->mEmbeddedStatement != NULL)
  8670. mEndsFound.Add(repeatStatement->mEmbeddedStatement->mSrcEnd - 1);
  8671. BfElementVisitor::Visit(repeatStatement);
  8672. }
  8673. virtual void Visit(BfWhileStatement* whileStatement) override
  8674. {
  8675. Add(whileStatement->mCloseParen, whileStatement->mEmbeddedStatement);
  8676. BfElementVisitor::Visit(whileStatement);
  8677. }
  8678. virtual void Visit(BfDoStatement* doStatement) override
  8679. {
  8680. Add(doStatement->mDoToken, doStatement->mEmbeddedStatement);
  8681. BfElementVisitor::Visit(doStatement);
  8682. }
  8683. virtual void Visit(BfForStatement* forStatement) override
  8684. {
  8685. Add(forStatement->mCloseParen, forStatement->mEmbeddedStatement);
  8686. BfElementVisitor::Visit(forStatement);
  8687. }
  8688. virtual void Visit(BfForEachStatement* forStatement) override
  8689. {
  8690. Add(forStatement->mCloseParen, forStatement->mEmbeddedStatement);
  8691. BfElementVisitor::Visit(forStatement);
  8692. }
  8693. virtual void Visit(BfUsingStatement* usingStatement) override
  8694. {
  8695. Add(usingStatement->mUsingToken, usingStatement->mEmbeddedStatement);
  8696. BfElementVisitor::Visit(usingStatement);
  8697. }
  8698. virtual void Visit(BfSwitchStatement* switchStatement) override
  8699. {
  8700. Add(switchStatement->mOpenParen, switchStatement->mCloseBrace);
  8701. BfElementVisitor::Visit(switchStatement);
  8702. }
  8703. virtual void Visit(BfLambdaBindExpression* lambdaExpression) override
  8704. {
  8705. Add(lambdaExpression->mFatArrowToken, lambdaExpression->mBody);
  8706. BfElementVisitor::Visit(lambdaExpression);
  8707. }
  8708. virtual void Visit(BfBlock* block) override
  8709. {
  8710. Add(block->mOpenBrace, block->mCloseBrace);
  8711. BfElementVisitor::Visit(block);
  8712. }
  8713. virtual void Visit(BfInitializerExpression* initExpr) override
  8714. {
  8715. Add(initExpr->mOpenBrace, initExpr->mCloseBrace);
  8716. BfElementVisitor::Visit(initExpr);
  8717. }
  8718. virtual void Visit(BfInvocationExpression* invocationExpr) override
  8719. {
  8720. Add(invocationExpr->mOpenParen, invocationExpr->mCloseParen, '?', 3);
  8721. BfElementVisitor::Visit(invocationExpr);
  8722. }
  8723. };
  8724. CollapseVisitor collapseVisitor(bfParser, outString);
  8725. collapseVisitor.VisitChild(bfParser->mRootNode);
  8726. BfAstNode* condStart = NULL;
  8727. BfAstNode* regionStart = NULL;
  8728. BfPreprocessorNode* prevPreprocessorNode = NULL;
  8729. int ignoredSectionStart = -1;
  8730. if (bfParser->mSidechannelRootNode != NULL)
  8731. {
  8732. for (auto element : bfParser->mSidechannelRootNode->mChildArr)
  8733. {
  8734. if (auto preprocessorNode = BfNodeDynCast<BfPreprocessorNode>(element))
  8735. {
  8736. if ((ignoredSectionStart != -1) && (prevPreprocessorNode != NULL) && (prevPreprocessorNode->mCommand != NULL))
  8737. {
  8738. collapseVisitor.Add(prevPreprocessorNode->mCommand->mSrcStart, preprocessorNode->mSrcEnd - 1);
  8739. ignoredSectionStart = -1;
  8740. }
  8741. StringView sv = preprocessorNode->mCommand->ToStringView();
  8742. if (sv == "region")
  8743. regionStart = preprocessorNode->mCommand;
  8744. else if (sv == "endregion")
  8745. {
  8746. if (regionStart != NULL)
  8747. collapseVisitor.Add(regionStart->mSrcStart, preprocessorNode->mCommand->mSrcStart, 'R');
  8748. regionStart = NULL;
  8749. }
  8750. else if (sv == "if")
  8751. {
  8752. condStart = preprocessorNode->mCommand;
  8753. }
  8754. else if (sv == "endif")
  8755. {
  8756. if (condStart != NULL)
  8757. collapseVisitor.Add(condStart->mSrcStart, preprocessorNode->mCommand->mSrcStart);
  8758. condStart = NULL;
  8759. }
  8760. else if ((sv == "else") || (sv == "elif"))
  8761. {
  8762. if (condStart != NULL)
  8763. collapseVisitor.Add(condStart->mSrcStart, collapseVisitor.GetLineEndBefore(preprocessorNode->mSrcStart));
  8764. condStart = preprocessorNode->mCommand;
  8765. }
  8766. prevPreprocessorNode = preprocessorNode;
  8767. }
  8768. if (auto preprocessorNode = BfNodeDynCast<BfPreprocesorIgnoredSectionNode>(element))
  8769. {
  8770. if (ignoredSectionStart == -1)
  8771. {
  8772. for (int i = preprocessorNode->mSrcStart; i < preprocessorNode->mSrcEnd - 1; i++)
  8773. {
  8774. if (bfParser->mSrc[i] == '\n')
  8775. {
  8776. ignoredSectionStart = i + 1;
  8777. break;
  8778. }
  8779. }
  8780. }
  8781. }
  8782. char kind = 0;
  8783. if (auto commentNode = BfNodeDynCast<BfCommentNode>(element))
  8784. {
  8785. collapseVisitor.UpdateSeries(commentNode, 'C');
  8786. }
  8787. }
  8788. }
  8789. collapseVisitor.FlushSeries();
  8790. Array<BfTypeInstance*> explicitEmitTypes;
  8791. String checkStr = explicitEmitTypeNames;
  8792. for (auto typeName : checkStr.Split('\n'))
  8793. {
  8794. if (typeName.IsEmpty())
  8795. continue;
  8796. auto bfType = bfCompiler->GetType(typeName);
  8797. if ((bfType != NULL) && (bfType->IsTypeInstance()))
  8798. explicitEmitTypes.Add(bfType->ToTypeInstance());
  8799. }
  8800. // Embed emit info
  8801. BfPassInstance bfPassInstance(bfCompiler->mSystem);
  8802. SetAndRestoreValue<BfResolvePassData*> prevCompilerResolvePassData(bfCompiler->mResolvePassData, resolvePassData);
  8803. SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, &bfPassInstance);
  8804. HashSet<BfTypeDef*> typeHashSet;
  8805. for (auto typeDef : bfParser->mTypeDefs)
  8806. typeHashSet.Add(typeDef);
  8807. Dictionary<int, int> foundTypeIds;
  8808. struct _EmitSource
  8809. {
  8810. BfParser* mEmitParser;
  8811. bool mIsPrimary;
  8812. };
  8813. Dictionary<BfTypeDef*, Dictionary<int, _EmitSource>> emitLocMap;
  8814. bool mayHaveMissedEmits = false;
  8815. for (auto type : bfCompiler->mContext->mResolvedTypes)
  8816. {
  8817. auto typeInst = type->ToTypeInstance();
  8818. if (typeInst == NULL)
  8819. continue;
  8820. if (typeHashSet.Contains(typeInst->mTypeDef->GetDefinition()->GetLatest()))
  8821. {
  8822. if (typeInst->mCeTypeInfo == NULL)
  8823. continue;
  8824. if ((typeInst->mCeTypeInfo->mMayHaveUniqueEmitLocations) && (!mayHaveMissedEmits))
  8825. {
  8826. outString += "~\n";
  8827. mayHaveMissedEmits = true;
  8828. }
  8829. for (auto& kv : typeInst->mCeTypeInfo->mEmitSourceMap)
  8830. {
  8831. int partialIdx = (int)(kv.mKey >> 32);
  8832. int charIdx = (int)(kv.mKey & 0xFFFFFFFF);
  8833. auto typeDef = typeInst->mTypeDef;
  8834. if (partialIdx > 0)
  8835. typeDef = typeDef->mPartials[partialIdx];
  8836. auto emitParser = typeInst->mTypeDef->GetLastSource()->ToParser();
  8837. Dictionary<int, _EmitSource>* map = NULL;
  8838. emitLocMap.TryAdd(typeDef->GetDefinition()->GetLatest(), NULL, &map);
  8839. _EmitSource emitSource;
  8840. emitSource.mEmitParser = emitParser;
  8841. emitSource.mIsPrimary = false;
  8842. (*map)[charIdx] = emitSource;
  8843. }
  8844. }
  8845. }
  8846. for (auto typeDef : bfParser->mTypeDefs)
  8847. {
  8848. auto useTypeDef = typeDef;
  8849. if (useTypeDef->mIsPartial)
  8850. {
  8851. useTypeDef = bfCompiler->mSystem->GetCombinedPartial(useTypeDef);
  8852. if (useTypeDef == NULL)
  8853. continue;
  8854. }
  8855. auto _GetTypeEmbedId = [&](BfTypeInstance* typeInst, BfParser* emitParser)
  8856. {
  8857. int* keyPtr = NULL;
  8858. int* valuePtr = NULL;
  8859. if (foundTypeIds.TryAdd(typeInst->mTypeId, &keyPtr, &valuePtr))
  8860. {
  8861. *valuePtr = foundTypeIds.mCount - 1;
  8862. outString += "+";
  8863. if ((emitParser == NULL) || (!emitParser->mIsEmitted))
  8864. {
  8865. outString += bfCompiler->mContext->mScratchModule->TypeToString(typeInst, BfTypeNameFlag_AddProjectName);
  8866. }
  8867. else
  8868. {
  8869. int dollarPos = (int)emitParser->mFileName.LastIndexOf('$');
  8870. if (dollarPos != -1)
  8871. outString += emitParser->mFileName.Substring(dollarPos + 1);
  8872. }
  8873. outString += "\n";
  8874. }
  8875. return *valuePtr;
  8876. };
  8877. auto type = bfCompiler->mContext->mScratchModule->ResolveTypeDef(useTypeDef);
  8878. if (type == NULL)
  8879. continue;
  8880. if (auto typeInst = type->ToTypeInstance())
  8881. {
  8882. auto origTypeInst = typeInst;
  8883. for (auto checkIdx = explicitEmitTypes.mSize - 1; checkIdx >= 0; checkIdx--)
  8884. {
  8885. auto checkType = explicitEmitTypes[checkIdx];
  8886. if (checkType->mTypeDef->GetDefinition()->GetLatest() == typeInst->mTypeDef->GetDefinition()->GetLatest())
  8887. {
  8888. typeInst = checkType;
  8889. bfCompiler->mContext->mScratchModule->PopulateType(typeInst);
  8890. break;
  8891. }
  8892. }
  8893. if (typeInst->mCeTypeInfo != NULL)
  8894. {
  8895. for (auto& kv : typeInst->mCeTypeInfo->mEmitSourceMap)
  8896. {
  8897. int partialIdx = (int)(kv.mKey >> 32);
  8898. int charIdx = (int)(kv.mKey & 0xFFFFFFFF);
  8899. auto typeDef = typeInst->mTypeDef;
  8900. if (partialIdx > 0)
  8901. typeDef = typeDef->mPartials[partialIdx];
  8902. auto parser = typeDef->GetDefinition()->GetLastSource()->ToParser();
  8903. if (parser == NULL)
  8904. continue;
  8905. if (!FileNameEquals(parser->mFileName, bfParser->mFileName))
  8906. continue;
  8907. auto bfParser = typeDef->GetLastSource()->ToParser();
  8908. auto emitParser = typeInst->mTypeDef->GetLastSource()->ToParser();
  8909. if (emitParser == NULL)
  8910. continue;
  8911. Dictionary<int, _EmitSource>* map = NULL;
  8912. if (emitLocMap.TryGetValue(typeDef->GetDefinition(), &map))
  8913. {
  8914. _EmitSource emitSource;
  8915. emitSource.mEmitParser = emitParser;
  8916. emitSource.mIsPrimary = true;
  8917. (*map)[charIdx] = emitSource;
  8918. }
  8919. int startLine = 0;
  8920. int startLineChar = 0;
  8921. if (kv.mValue.mSrcStart >= 0)
  8922. emitParser->GetLineCharAtIdx(kv.mValue.mSrcStart, startLine, startLineChar);
  8923. int srcEnd = kv.mValue.mSrcEnd - 1;
  8924. if (srcEnd >= emitParser->mOrigSrcLength)
  8925. continue;
  8926. while (srcEnd >= kv.mValue.mSrcStart)
  8927. {
  8928. char c = emitParser->mSrc[srcEnd];
  8929. if (!::isspace((uint8)c))
  8930. break;
  8931. srcEnd--;
  8932. }
  8933. int embedId = _GetTypeEmbedId(typeInst, emitParser);
  8934. if (embedId == -1)
  8935. continue;
  8936. int endLine = 0;
  8937. int endLineChar = 0;
  8938. if (srcEnd >= 0)
  8939. emitParser->GetLineCharAtIdx(srcEnd, endLine, endLineChar);
  8940. int textVersion = -1;
  8941. auto declParser = typeInst->mTypeDef->mTypeDeclaration->GetParser();
  8942. if (declParser != NULL)
  8943. textVersion = declParser->mTextVersion;
  8944. char emitChar = (kv.mValue.mKind == BfCeTypeEmitSourceKind_Type) ? 't' : 'm';
  8945. outString += StrFormat("%c%d,%d,%d,%d,%d\n", emitChar, embedId, charIdx, startLine, endLine + 1, textVersion);
  8946. }
  8947. }
  8948. if (typeInst->IsGenericTypeInstance())
  8949. {
  8950. String emitName;
  8951. auto _CheckTypeDef = [&](BfTypeDef* typeDef)
  8952. {
  8953. auto parser = typeDef->GetDefinition()->GetLastSource()->ToParser();
  8954. if (parser == NULL)
  8955. return;
  8956. if (!FileNameEquals(parser->mFileName, bfParser->mFileName))
  8957. return;
  8958. Dictionary<int, _EmitSource>* map = NULL;
  8959. if (emitLocMap.TryGetValue(typeDef, &map))
  8960. {
  8961. for (auto& kv : *map)
  8962. {
  8963. if (kv.mValue.mIsPrimary)
  8964. continue;
  8965. int embedId = _GetTypeEmbedId(typeInst, NULL);
  8966. if (embedId == -1)
  8967. continue;
  8968. char emitChar = 't';
  8969. outString += StrFormat("%c%d,%d,%d,%d,%d\n", emitChar, embedId, kv.mKey, 0, 0, -1);
  8970. }
  8971. }
  8972. };
  8973. _CheckTypeDef(typeInst->mTypeDef->GetDefinition());
  8974. for (auto partialTypeDef : typeInst->mTypeDef->mPartials)
  8975. _CheckTypeDef(partialTypeDef);
  8976. }
  8977. }
  8978. }
  8979. return outString.c_str();
  8980. }
  8981. BF_EXPORT bool BF_CALLTYPE BfCompiler_VerifyTypeName(BfCompiler* bfCompiler, char* name, int cursorPos)
  8982. {
  8983. bfCompiler->mSystem->AssertWeHaveLock();
  8984. String typeName = name;
  8985. auto system = bfCompiler->mSystem;
  8986. AutoCrit autoCrit(system->mSystemLock);
  8987. String& autoCompleteResultString = *gTLStrReturn.Get();
  8988. autoCompleteResultString.Clear();
  8989. BfPassInstance passInstance(bfCompiler->mSystem);
  8990. BfParser parser(bfCompiler->mSystem);
  8991. parser.SetSource(typeName.c_str(), (int)typeName.length());
  8992. parser.Parse(&passInstance);
  8993. parser.mCursorIdx = cursorPos;
  8994. parser.mCursorCheckIdx = cursorPos;
  8995. BfReducer reducer;
  8996. reducer.mAlloc = parser.mAlloc;
  8997. reducer.mPassInstance = &passInstance;
  8998. reducer.mAllowTypeWildcard = true;
  8999. if (parser.mRootNode->mChildArr.mSize == 0)
  9000. return false;
  9001. bool attribWasClosed = false;
  9002. bool isAttributeRef = false;
  9003. auto firstNode = parser.mRootNode->mChildArr[0];
  9004. auto endIdx = parser.mRootNode->mSrcEnd;
  9005. reducer.mVisitorPos = BfReducer::BfVisitorPos(parser.mRootNode);
  9006. if (auto tokenNode = BfNodeDynCast<BfTokenNode>(firstNode))
  9007. {
  9008. if (tokenNode->mToken == BfToken_LBracket)
  9009. {
  9010. if (auto lastToken = BfNodeDynCast<BfTokenNode>(parser.mRootNode->mChildArr.back()))
  9011. {
  9012. if (lastToken->mToken == BfToken_RBracket)
  9013. {
  9014. attribWasClosed = true;
  9015. endIdx = lastToken->mSrcStart;
  9016. }
  9017. }
  9018. isAttributeRef = true;
  9019. if (parser.mRootNode->mChildArr.mSize < 2)
  9020. return false;
  9021. firstNode = parser.mRootNode->mChildArr[1];
  9022. reducer.mVisitorPos.MoveNext();
  9023. }
  9024. }
  9025. reducer.mVisitorPos.MoveNext();
  9026. auto typeRef = reducer.CreateTypeRef(firstNode);
  9027. if (typeRef == NULL)
  9028. return false;
  9029. BfResolvePassData resolvePassData;
  9030. if (cursorPos != -1)
  9031. {
  9032. resolvePassData.mResolveType = BfResolveType_Autocomplete;
  9033. parser.mParserFlags = (BfParserFlag)(parser.mParserFlags | ParserFlag_Autocomplete);
  9034. resolvePassData.mAutoComplete = new BfAutoComplete();
  9035. resolvePassData.mAutoComplete->mResolveType = BfResolveType_VerifyTypeName;
  9036. resolvePassData.mAutoComplete->mSystem = bfCompiler->mSystem;
  9037. resolvePassData.mAutoComplete->mCompiler = bfCompiler;
  9038. resolvePassData.mAutoComplete->mModule = bfCompiler->mContext->mScratchModule;
  9039. }
  9040. resolvePassData.mParsers.Add(&parser);
  9041. SetAndRestoreValue<BfResolvePassData*> prevCompilerResolvePassData(bfCompiler->mResolvePassData, &resolvePassData);
  9042. SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, &passInstance);
  9043. if (resolvePassData.mAutoComplete != NULL)
  9044. {
  9045. if (isAttributeRef)
  9046. resolvePassData.mAutoComplete->CheckAttributeTypeRef(typeRef);
  9047. else
  9048. resolvePassData.mAutoComplete->CheckTypeRef(typeRef, false);
  9049. bfCompiler->GenerateAutocompleteInfo();
  9050. }
  9051. if (passInstance.HasFailed())
  9052. return false;
  9053. if (typeRef->mSrcEnd != endIdx)
  9054. return false;
  9055. if (!bfCompiler->mContext->mScratchModule->ValidateTypeWildcard(typeRef, isAttributeRef))
  9056. return false;
  9057. if ((isAttributeRef) && (!attribWasClosed))
  9058. return false;
  9059. return true;
  9060. }
  9061. BF_EXPORT void BF_CALLTYPE BfCompiler_ClearCompletionPercentage(BfCompiler* bfCompiler)
  9062. {
  9063. bfCompiler->mCompletionPct = 0;
  9064. }
  9065. BF_EXPORT float BF_CALLTYPE BfCompiler_GetCompletionPercentage(BfCompiler* bfCompiler)
  9066. {
  9067. return bfCompiler->mCompletionPct;
  9068. }
  9069. BF_EXPORT int BF_CALLTYPE BfCompiler_GetCompileRevision(BfCompiler* bfCompiler)
  9070. {
  9071. return bfCompiler->mRevision;
  9072. }
  9073. BF_EXPORT int BF_CALLTYPE BfCompiler_GetCurConstEvalExecuteId(BfCompiler* bfCompiler)
  9074. {
  9075. return bfCompiler->mCurCEExecuteId;
  9076. }
  9077. BF_EXPORT float BF_CALLTYPE BfCompiler_GetLastHadComptimeRebuilds(BfCompiler* bfCompiler)
  9078. {
  9079. return bfCompiler->mLastHadComptimeRebuilds;
  9080. }
  9081. BF_EXPORT void BF_CALLTYPE BfCompiler_Cancel(BfCompiler* bfCompiler)
  9082. {
  9083. bfCompiler->Cancel();
  9084. }
  9085. BF_EXPORT void BF_CALLTYPE BfCompiler_RequestFastFinish(BfCompiler* bfCompiler)
  9086. {
  9087. bfCompiler->RequestFastFinish();
  9088. }
  9089. BF_EXPORT void BF_CALLTYPE BfCompiler_ClearBuildCache(BfCompiler* bfCompiler)
  9090. {
  9091. bfCompiler->ClearBuildCache();
  9092. }
  9093. BF_EXPORT void BF_CALLTYPE BfCompiler_SetBuildValue(BfCompiler* bfCompiler, char* cacheDir, char* key, char* value)
  9094. {
  9095. bfCompiler->mCodeGen.SetBuildValue(cacheDir, key, value);
  9096. }
  9097. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetBuildValue(BfCompiler* bfCompiler, char* cacheDir, char* key)
  9098. {
  9099. String& outString = *gTLStrReturn.Get();
  9100. outString = bfCompiler->mCodeGen.GetBuildValue(cacheDir, key);
  9101. return outString.c_str();
  9102. }
  9103. BF_EXPORT void BF_CALLTYPE BfCompiler_WriteBuildCache(BfCompiler* bfCompiler, char* cacheDir)
  9104. {
  9105. bfCompiler->mCodeGen.WriteBuildCache(cacheDir);
  9106. }
  9107. BF_EXPORT void BF_CALLTYPE BfCompiler_Delete(BfCompiler* bfCompiler)
  9108. {
  9109. delete bfCompiler;
  9110. }
  9111. BF_EXPORT void BF_CALLTYPE BfCompiler_ProgramDone()
  9112. {
  9113. #ifdef BF_PLATFORM_WINDOWS
  9114. BeLibManager::Get()->Clear();
  9115. #endif
  9116. }
  9117. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetGeneratorTypeDefList(BfCompiler* bfCompiler)
  9118. {
  9119. String& outString = *gTLStrReturn.Get();
  9120. outString.clear();
  9121. outString = bfCompiler->GetGeneratorTypeDefList();
  9122. return outString.c_str();
  9123. }
  9124. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetGeneratorInitData(BfCompiler* bfCompiler, char* typeDefName, char* args)
  9125. {
  9126. String& outString = *gTLStrReturn.Get();
  9127. outString.clear();
  9128. outString = bfCompiler->GetGeneratorInitData(typeDefName, args);
  9129. return outString.c_str();
  9130. }
  9131. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetGeneratorGenData(BfCompiler* bfCompiler, char* typeDefName, char* args)
  9132. {
  9133. String& outString = *gTLStrReturn.Get();
  9134. outString.clear();
  9135. outString = bfCompiler->GetGeneratorGenData(typeDefName, args);
  9136. return outString.c_str();
  9137. }
  9138. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetTypeDefList(BfCompiler* bfCompiler, bool includeLocation)
  9139. {
  9140. String& outString = *gTLStrReturn.Get();
  9141. outString.clear();
  9142. outString = bfCompiler->GetTypeDefList(includeLocation);
  9143. return outString.c_str();
  9144. }
  9145. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetTypeDefMatches(BfCompiler* bfCompiler, const char* searchStr, bool includeLocation)
  9146. {
  9147. String& outString = *gTLStrReturn.Get();
  9148. outString.clear();
  9149. outString = bfCompiler->GetTypeDefMatches(searchStr, includeLocation);
  9150. return outString.c_str();
  9151. }
  9152. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetTypeDefInfo(BfCompiler* bfCompiler, const char* typeDefName)
  9153. {
  9154. String& outString = *gTLStrReturn.Get();
  9155. outString.clear();
  9156. outString = bfCompiler->GetTypeDefInfo(typeDefName);
  9157. return outString.c_str();
  9158. }
  9159. BF_EXPORT int BF_CALLTYPE BfCompiler_GetTypeId(BfCompiler* bfCompiler, const char* name)
  9160. {
  9161. return bfCompiler->GetTypeId(name);
  9162. }
  9163. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetTypeInfo(BfCompiler* bfCompiler, const char* name)
  9164. {
  9165. bfCompiler->mSystem->AssertWeHaveLock();
  9166. String& outString = *gTLStrReturn.Get();
  9167. outString = "";
  9168. String typeName = name;
  9169. auto system = bfCompiler->mSystem;
  9170. AutoCrit autoCrit(system->mSystemLock);
  9171. String& autoCompleteResultString = *gTLStrReturn.Get();
  9172. autoCompleteResultString.Clear();
  9173. BfPassInstance passInstance(bfCompiler->mSystem);
  9174. BfParser parser(bfCompiler->mSystem);
  9175. parser.SetSource(typeName.c_str(), (int)typeName.length());
  9176. parser.Parse(&passInstance);
  9177. BfReducer reducer;
  9178. reducer.mAlloc = parser.mAlloc;
  9179. reducer.mPassInstance = &passInstance;
  9180. reducer.mAllowTypeWildcard = true;
  9181. if (parser.mRootNode->mChildArr.mSize == 0)
  9182. return "";
  9183. bool attribWasClosed = false;
  9184. bool isAttributeRef = false;
  9185. auto firstNode = parser.mRootNode->mChildArr[0];
  9186. auto endIdx = parser.mRootNode->mSrcEnd;
  9187. reducer.mVisitorPos = BfReducer::BfVisitorPos(parser.mRootNode);
  9188. if (auto tokenNode = BfNodeDynCast<BfTokenNode>(firstNode))
  9189. {
  9190. if (tokenNode->mToken == BfToken_LBracket)
  9191. {
  9192. if (auto lastToken = BfNodeDynCast<BfTokenNode>(parser.mRootNode->mChildArr.back()))
  9193. {
  9194. if (lastToken->mToken == BfToken_RBracket)
  9195. {
  9196. attribWasClosed = true;
  9197. endIdx = lastToken->mSrcStart;
  9198. }
  9199. }
  9200. isAttributeRef = true;
  9201. if (parser.mRootNode->mChildArr.mSize < 2)
  9202. return "";
  9203. firstNode = parser.mRootNode->mChildArr[1];
  9204. reducer.mVisitorPos.MoveNext();
  9205. }
  9206. }
  9207. reducer.mVisitorPos.MoveNext();
  9208. auto typeRef = reducer.CreateTypeRef(firstNode);
  9209. if (typeRef == NULL)
  9210. return "";
  9211. BfResolvePassData resolvePass;
  9212. SetAndRestoreValue<bool> prevIgnoreError(bfCompiler->mContext->mScratchModule->mIgnoreErrors, true);
  9213. SetAndRestoreValue<bool> prevIgnoreWarnings(bfCompiler->mContext->mScratchModule->mIgnoreWarnings, true);
  9214. SetAndRestoreValue<BfResolvePassData*> prevResolvePass(bfCompiler->mResolvePassData, &resolvePass);
  9215. auto type = bfCompiler->mContext->mScratchModule->ResolveTypeRef(typeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_NoCreate);
  9216. if (type != NULL)
  9217. {
  9218. bfCompiler->mContext->mScratchModule->PopulateType(type);
  9219. outString += "Found";
  9220. if (auto typeInst = type->ToTypeInstance())
  9221. {
  9222. if (typeInst->mIsReified)
  9223. outString += " Reified";
  9224. if (typeInst->HasBeenInstantiated())
  9225. outString += " Instantiated";
  9226. if (typeInst->mTypeFailed)
  9227. outString += " TypeFailed";
  9228. if (auto genericTypeInst = typeInst->ToGenericTypeInstance())
  9229. {
  9230. if (genericTypeInst->mGenericTypeInfo->mHadValidateErrors)
  9231. outString += " ValidateErrors";
  9232. }
  9233. }
  9234. }
  9235. return outString.c_str();
  9236. }
  9237. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetGenericTypeInstances(BfCompiler* bfCompiler, const char* typeName)
  9238. {
  9239. String& outString = *gTLStrReturn.Get();
  9240. outString = "";
  9241. String checkTypeName = typeName;
  9242. auto lookupType = bfCompiler->GetType(checkTypeName);
  9243. if (lookupType == NULL)
  9244. {
  9245. // Convert potentially-specialized type name into an unspecialized type name
  9246. int chevronDepth = 0;
  9247. int chevronStart = -1;
  9248. for (int i = 0; i < (int)checkTypeName.mLength; i++)
  9249. {
  9250. char c = checkTypeName[i];
  9251. if (c == '<')
  9252. {
  9253. if (chevronDepth == 0)
  9254. chevronStart = i;
  9255. chevronDepth++;
  9256. }
  9257. else if (c == '>')
  9258. {
  9259. chevronDepth--;
  9260. if (chevronDepth == 0)
  9261. {
  9262. checkTypeName.Remove(chevronStart + 1, i - chevronStart - 1);
  9263. i = chevronStart + 1;
  9264. }
  9265. }
  9266. }
  9267. lookupType = bfCompiler->GetType(checkTypeName);
  9268. if (lookupType == NULL)
  9269. return "";
  9270. }
  9271. auto lookupTypeInst = lookupType->ToTypeInstance();
  9272. if (lookupTypeInst == NULL)
  9273. return "";
  9274. for (auto type : bfCompiler->mContext->mResolvedTypes)
  9275. {
  9276. auto typeInst = type->ToTypeInstance();
  9277. if (typeInst == NULL)
  9278. continue;
  9279. if (typeInst->IsUnspecializedTypeVariation())
  9280. continue;
  9281. if (typeInst->mTypeDef->GetDefinition()->GetLatest() == lookupTypeInst->mTypeDef->GetDefinition()->GetLatest())
  9282. {
  9283. outString += bfCompiler->mContext->mScratchModule->TypeToString(typeInst, BfTypeNameFlag_AddProjectName);
  9284. outString += "\n";
  9285. }
  9286. }
  9287. return outString.c_str();
  9288. }
  9289. enum BfUsedOutputFlags
  9290. {
  9291. BfUsedOutputFlags_None = 0,
  9292. BfUsedOutputFlags_FlushQueuedHotFiles = 1,
  9293. BfUsedOutputFlags_SkipImports = 2,
  9294. };
  9295. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetUsedOutputFileNames(BfCompiler* bfCompiler, BfProject* bfProject, BfUsedOutputFlags flags, bool* hadOutputChanges)
  9296. {
  9297. bfCompiler->mSystem->AssertWeHaveLock();
  9298. BP_ZONE("BfCompiler_GetUsedOutputFileNames");
  9299. *hadOutputChanges = false;
  9300. String& outString = *gTLStrReturn.Get();
  9301. outString.clear();
  9302. Array<BfModule*> moduleList;
  9303. moduleList.Reserve(bfProject->mUsedModules.size());
  9304. if (bfCompiler->mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
  9305. {
  9306. for (auto mainModule : bfCompiler->mContext->mModules)
  9307. {
  9308. if ((!mainModule->mIsReified) || (mainModule->mIsScratchModule))
  9309. continue;
  9310. if (bfCompiler->mOptions.mHotProject != NULL)
  9311. continue; // Only add new objs from mCodeGen.mCodeGenFiles during hot reload
  9312. if (!bfCompiler->IsModuleAccessible(mainModule, bfProject))
  9313. continue;
  9314. moduleList.push_back(mainModule);
  9315. }
  9316. }
  9317. else
  9318. {
  9319. for (auto mainModule : bfProject->mUsedModules)
  9320. {
  9321. if ((!mainModule->mIsReified) || (mainModule->mIsScratchModule))
  9322. continue;
  9323. if (bfCompiler->mOptions.mHotProject != NULL)
  9324. continue; // Only add new objs from mCodeGen.mCodeGenFiles during hot reload
  9325. moduleList.push_back(mainModule);
  9326. }
  9327. }
  9328. std::sort(moduleList.begin(), moduleList.end(), [&](BfModule* moduleA, BfModule* moduleB) { return moduleA->mModuleName < moduleB->mModuleName; } );
  9329. HashSet<String> usedFileNames;
  9330. usedFileNames.Reserve(moduleList.size());
  9331. //for (auto mainModule : moduleList)
  9332. for (int i = 0; i < moduleList.mSize; i++)
  9333. {
  9334. auto mainModule = moduleList[i];
  9335. BF_ASSERT_REL(!mainModule->mIsDeleting);
  9336. BF_ASSERT_REL((mainModule->mRevision > -2) && (mainModule->mRevision < 1000000));
  9337. if ((flags & BfUsedOutputFlags_SkipImports) == 0)
  9338. {
  9339. for (auto fileNameIdx : mainModule->mImportFileNames)
  9340. {
  9341. auto fileName = bfCompiler->mContext->mStringObjectIdMap[fileNameIdx].mString;
  9342. if (!usedFileNames.TryAdd(fileName, NULL))
  9343. continue;
  9344. if (!outString.empty())
  9345. outString += "\n";
  9346. outString += fileName;
  9347. }
  9348. }
  9349. for (auto& moduleFileName : mainModule->mOutFileNames)
  9350. {
  9351. if (!moduleFileName.mModuleWritten)
  9352. continue;
  9353. bool canReference = true;
  9354. for (auto project : moduleFileName.mProjects)
  9355. {
  9356. if (!bfProject->ContainsReference(project))
  9357. canReference = false;
  9358. if (bfProject != project)
  9359. {
  9360. //TODO: What was this for?
  9361. // if (project->mTargetType == BfTargetType_BeefDynLib)
  9362. // canReference = false;
  9363. }
  9364. }
  9365. if (!canReference)
  9366. continue;
  9367. String fileName = moduleFileName.mFileName;
  9368. #ifdef BF_PLATFORM_WINDOWS
  9369. if (moduleFileName.mWroteToLib)
  9370. fileName = BeLibManager::GetLibFilePath(fileName);
  9371. #endif
  9372. if (!usedFileNames.TryAdd(fileName, NULL))
  9373. continue;
  9374. if (!outString.empty())
  9375. outString += "\n";
  9376. outString += fileName;
  9377. }
  9378. }
  9379. if (bfCompiler->mHotState != NULL)
  9380. {
  9381. Array<String> outPaths;
  9382. for (int i = 0; i < (int)bfCompiler->mHotState->mQueuedOutFiles.size(); i++)
  9383. {
  9384. auto& fileEntry = bfCompiler->mHotState->mQueuedOutFiles[i];
  9385. if (fileEntry.mProject != bfProject)
  9386. continue;
  9387. if (!bfCompiler->mHotState->mHotProject->mUsedModules.Contains(fileEntry.mModule))
  9388. continue;
  9389. outPaths.Add(fileEntry.mFileName);
  9390. if ((flags & BfUsedOutputFlags_FlushQueuedHotFiles) != 0)
  9391. {
  9392. bfCompiler->mHotState->mQueuedOutFiles.RemoveAtFast(i);
  9393. i--;
  9394. }
  9395. }
  9396. std::sort(outPaths.begin(), outPaths.end(), [](const String& lhs, const String& rhs) { return lhs < rhs; });
  9397. for (auto& path : outPaths)
  9398. {
  9399. if (!outString.empty())
  9400. outString += "\n";
  9401. outString += path;
  9402. outString += BF_OBJ_EXT;
  9403. }
  9404. }
  9405. for (auto& fileEntry : bfCompiler->mCodeGen.mCodeGenFiles)
  9406. {
  9407. if (fileEntry.mWasCached)
  9408. continue;
  9409. if (!bfProject->ContainsReference(fileEntry.mProject))
  9410. continue;
  9411. *hadOutputChanges = true;
  9412. }
  9413. return outString.c_str();
  9414. }
  9415. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetAutocompleteInfo(BfCompiler* bfCompiler)
  9416. {
  9417. String& autoCompleteResultString = *gTLStrReturn.Get();
  9418. return autoCompleteResultString.c_str();
  9419. }
  9420. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetRebuildFileSet(BfCompiler* bfCompiler)
  9421. {
  9422. String& autoCompleteResultString = *gTLStrReturn.Get();
  9423. for (auto& val : bfCompiler->mRebuildFileSet)
  9424. {
  9425. autoCompleteResultString += val;
  9426. autoCompleteResultString += "\n";
  9427. }
  9428. return autoCompleteResultString.c_str();
  9429. }
  9430. BF_EXPORT void BF_CALLTYPE BfCompiler_FileChanged(BfCompiler* bfCompiler, const char* dirPath)
  9431. {
  9432. bfCompiler->mRebuildChangedFileSet.Add(dirPath);
  9433. }
  9434. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetSymbolReferences(BfCompiler* bfCompiler, BfPassInstance* bfPassInstance, BfResolvePassData* resolvePassData)
  9435. {
  9436. bfCompiler->mSystem->AssertWeHaveLock();
  9437. BP_ZONE("BfCompiler_GetSymbolReferences");
  9438. String& outString = *gTLStrReturn.Get();
  9439. outString.clear();
  9440. SetAndRestoreValue<BfResolvePassData*> prevCompilerResolvePassData(bfCompiler->mResolvePassData, resolvePassData);
  9441. SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, bfPassInstance);
  9442. bfCompiler->GetSymbolReferences();
  9443. std::map<String, String*> sortedParserMap;
  9444. for (auto& parserDataPair : resolvePassData->mFoundSymbolReferencesParserData)
  9445. {
  9446. if (!parserDataPair.mKey->mFileName.Contains('|'))
  9447. sortedParserMap.insert(std::make_pair(parserDataPair.mKey->mFileName, &parserDataPair.mValue));
  9448. }
  9449. for (auto& parserData : sortedParserMap)
  9450. {
  9451. if (!outString.empty())
  9452. outString += "\n";
  9453. outString += parserData.first + "\t" + *(parserData.second);
  9454. }
  9455. return outString.c_str();
  9456. }
  9457. BF_EXPORT bool BF_CALLTYPE BfCompiler_GetHasHotPendingDataChanges(BfCompiler* bfCompiler)
  9458. {
  9459. return (bfCompiler->mHotState != NULL) &&
  9460. ((!bfCompiler->mHotState->mPendingDataChanges.IsEmpty()) || (!bfCompiler->mHotState->mPendingFailedSlottings.IsEmpty()));
  9461. }
  9462. BF_EXPORT void BF_CALLTYPE BfCompiler_HotCommit(BfCompiler* bfCompiler)
  9463. {
  9464. bfCompiler->HotCommit();
  9465. }
  9466. BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_Start(BfCompiler* bfCompiler, int flags)
  9467. {
  9468. bfCompiler->HotResolve_Start((BfCompiler::HotResolveFlags)flags);
  9469. }
  9470. BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_AddActiveMethod(BfCompiler* bfCompiler, const char* methodName)
  9471. {
  9472. bfCompiler->HotResolve_AddActiveMethod(methodName);
  9473. }
  9474. BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_AddDelegateMethod(BfCompiler* bfCompiler, const char* methodName)
  9475. {
  9476. bfCompiler->HotResolve_AddDelegateMethod(methodName);
  9477. }
  9478. // 0: heap, 1: user stated 'not used', 2: user stated 'used'
  9479. BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_ReportType(BfCompiler* bfCompiler, int typeId, int usageKind)
  9480. {
  9481. bfCompiler->HotResolve_ReportType(typeId, (BfCompiler::HotTypeFlags)usageKind);
  9482. }
  9483. // 0: heap, 1: user stated 'used', 2: user stated 'not used'
  9484. BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_ReportTypeRange(BfCompiler* bfCompiler, const char* typeName, int usageKind)
  9485. {
  9486. //TODO: Implement
  9487. }
  9488. BF_EXPORT const char* BF_CALLTYPE BfCompiler_HotResolve_Finish(BfCompiler* bfCompiler)
  9489. {
  9490. String& outString = *gTLStrReturn.Get();
  9491. outString = bfCompiler->HotResolve_Finish();
  9492. return outString.c_str();
  9493. }
  9494. static BfPlatformType GetPlatform(StringView str)
  9495. {
  9496. while (!str.IsEmpty())
  9497. {
  9498. char c = str[str.mLength - 1];
  9499. if (((c >= '0') && (c <= '9')) || (c == '.'))
  9500. str.RemoveFromEnd(1);
  9501. else
  9502. break;
  9503. }
  9504. bool hasLinux = false;
  9505. for (auto elem : str.Split('-'))
  9506. {
  9507. if (elem == "linux")
  9508. hasLinux = true;
  9509. else if (elem == "windows")
  9510. return BfPlatformType_Windows;
  9511. else if (elem == "macosx")
  9512. return BfPlatformType_macOS;
  9513. else if (elem == "ios")
  9514. return BfPlatformType_iOS;
  9515. else if ((elem == "android") || (elem == "androideabi"))
  9516. return BfPlatformType_Android;
  9517. else if ((elem == "wasm32") || (elem == "wasm64"))
  9518. return BfPlatformType_Wasm;
  9519. }
  9520. if (hasLinux)
  9521. return BfPlatformType_Linux;
  9522. return BfPlatformType_Unknown;
  9523. }
  9524. BF_EXPORT void BF_CALLTYPE BfCompiler_SetOptions(BfCompiler* bfCompiler, BfProject* hotProject, int hotIdx,
  9525. const char* targetTriple, const char* targetCPU, int toolsetType, int simdSetting, int allocStackCount, int maxWorkerThreads,
  9526. BfCompilerOptionFlags optionFlags, char* mallocLinkName, char* freeLinkName)
  9527. {
  9528. BfLogSys(bfCompiler->mSystem, "BfCompiler_SetOptions\n");
  9529. //printf("BfCompiler_SetOptions Threads:%d\n", maxWorkerThreads);
  9530. auto options = &bfCompiler->mOptions;
  9531. options->mErrorString.Clear();
  9532. options->mHotProject = hotProject;
  9533. options->mHotCompileIdx = hotIdx;
  9534. options->mTargetTriple = targetTriple;
  9535. options->mTargetCPU = targetCPU;
  9536. if (options->mTargetTriple.StartsWith("x86_64-"))
  9537. options->mMachineType = BfMachineType_x64;
  9538. else if (options->mTargetTriple.StartsWith("i686-"))
  9539. options->mMachineType = BfMachineType_x86;
  9540. else if ((options->mTargetTriple.StartsWith("arm64")) || (options->mTargetTriple.StartsWith("aarch64")))
  9541. options->mMachineType = BfMachineType_AArch64;
  9542. else if (options->mTargetTriple.StartsWith("armv"))
  9543. options->mMachineType = BfMachineType_ARM;
  9544. else if (options->mTargetTriple.StartsWith("wasm32"))
  9545. options->mMachineType = BfMachineType_Wasm32;
  9546. else if (options->mTargetTriple.StartsWith("wasm64"))
  9547. options->mMachineType = BfMachineType_Wasm64;
  9548. else
  9549. options->mMachineType = BfMachineType_x64; // Default
  9550. options->mPlatformType = GetPlatform(options->mTargetTriple);
  9551. options->mCLongSize = 4;
  9552. if ((options->mMachineType == BfMachineType_AArch64) || (options->mMachineType == BfMachineType_x64))
  9553. {
  9554. if (options->mPlatformType != BfPlatformType_Windows)
  9555. options->mCLongSize = 8;
  9556. }
  9557. bfCompiler->mCodeGen.SetMaxThreads(maxWorkerThreads);
  9558. if (!bfCompiler->mIsResolveOnly)
  9559. {
  9560. bool allowHotSwapping = (optionFlags & BfCompilerOptionFlag_EnableHotSwapping) != 0;
  9561. bool emitDebugInfo = (optionFlags & BfCompilerOptionFlag_EmitDebugInfo) != 0;
  9562. // These settings only matter for code generation, they are not applicable for resolveOnly
  9563. options->mCompileOnDemandKind = BfCompileOnDemandKind_ResolveUnused;
  9564. //options->mCompileOnDemandKind = BfCompileOnDemandKind_AlwaysInclude;
  9565. options->mToolsetType = (BfToolsetType)toolsetType;
  9566. options->mSIMDSetting = (BfSIMDSetting)simdSetting;
  9567. options->mIncrementalBuild = (optionFlags & BfCompilerOptionFlag_IncrementalBuild) != 0;
  9568. options->mWriteIR = (optionFlags & BfCompilerOptionFlag_WriteIR) != 0;
  9569. options->mGenerateObj = (optionFlags & BfCompilerOptionFlag_GenerateOBJ) != 0;
  9570. options->mGenerateBitcode = (optionFlags & BfCompilerOptionFlag_GenerateBitcode) != 0;
  9571. options->mNoFramePointerElim = (optionFlags & BfCompilerOptionFlag_NoFramePointerElim) != 0;
  9572. options->mInitLocalVariables = (optionFlags & BfCompilerOptionFlag_ClearLocalVars) != 0;
  9573. options->mRuntimeChecks = (optionFlags & BfCompilerOptionFlag_RuntimeChecks) != 0;
  9574. options->mEmitDynamicCastCheck = (optionFlags & BfCompilerOptionFlag_EmitDynamicCastCheck) != 0;
  9575. options->mObjectHasDebugFlags = (optionFlags & BfCompilerOptionFlag_EnableObjectDebugFlags) != 0;
  9576. options->mEnableRealtimeLeakCheck = ((optionFlags & BfCompilerOptionFlag_EnableRealtimeLeakCheck) != 0) && options->mObjectHasDebugFlags;
  9577. options->mDebugAlloc = ((optionFlags & BfCompilerOptionFlag_DebugAlloc) != 0) || options->mEnableRealtimeLeakCheck;
  9578. options->mOmitDebugHelpers = (optionFlags & BfCompilerOptionFlag_OmitDebugHelpers) != 0;
  9579. #ifdef _WINDOWS
  9580. // if (options->mToolsetType == BfToolsetType_GNU)
  9581. // {
  9582. // options->mErrorString = "Toolset 'GNU' is not available on this platform. Consider changing 'Workspace/General/Toolset'.";
  9583. // }
  9584. #else
  9585. // if (options->mToolsetType == BfToolsetType_Microsoft)
  9586. // {
  9587. // options->mErrorString = "Toolset 'Microsoft' is not available on this platform. Consider changing 'Workspace/General/Toolset'.";
  9588. // }
  9589. BF_ASSERT(!options->mEnableRealtimeLeakCheck);
  9590. #endif
  9591. options->mEmitObjectAccessCheck = (optionFlags & BfCompilerOptionFlag_EmitObjectAccessCheck) != 0;
  9592. options->mArithmeticChecks = (optionFlags & BfCompilerOptionFlag_ArithmeticChecks) != 0;
  9593. options->mAllocStackCount = allocStackCount;
  9594. if (hotProject != NULL)
  9595. {
  9596. String errorName;
  9597. if (options->mAllowHotSwapping != allowHotSwapping)
  9598. errorName = "Hot Compilation Enabled";
  9599. else if (options->mMallocLinkName != mallocLinkName)
  9600. errorName = "Malloc";
  9601. else if (options->mFreeLinkName != freeLinkName)
  9602. errorName = "Free";
  9603. if (!options->mEmitDebugInfo)
  9604. {
  9605. options->mErrorString = "Hot compilation cannot be used when the target is not built with debug information. Consider setting 'Workspace/Beef/Debug/Debug Information' to 'Yes'.";
  9606. }
  9607. else if (!errorName.IsEmpty())
  9608. {
  9609. options->mErrorString = StrFormat("Unable to change option '%s' during hot compilation", errorName.c_str());
  9610. }
  9611. }
  9612. else
  9613. {
  9614. options->mAllowHotSwapping = allowHotSwapping;
  9615. options->mHasVDataExtender = options->mAllowHotSwapping;
  9616. options->mMallocLinkName = mallocLinkName;
  9617. options->mFreeLinkName = freeLinkName;
  9618. options->mEmitDebugInfo = emitDebugInfo;
  9619. options->mEmitLineInfo = (optionFlags & BfCompilerOptionFlag_EmitLineInfo) != 0;;
  9620. options->mEnableCustodian = (optionFlags & BfCompilerOptionFlag_EnableCustodian) != 0;
  9621. options->mEnableSideStack = (optionFlags & BfCompilerOptionFlag_EnableSideStack) != 0;
  9622. }
  9623. }
  9624. else
  9625. {
  9626. options->mCompileOnDemandKind = BfCompileOnDemandKind_AlwaysInclude;
  9627. options->mAllowHotSwapping = false;
  9628. options->mObjectHasDebugFlags = false;
  9629. options->mEnableRealtimeLeakCheck = false;
  9630. options->mEmitObjectAccessCheck = false;
  9631. options->mArithmeticChecks = false;
  9632. options->mEmitDynamicCastCheck = false;
  9633. options->mRuntimeChecks = (optionFlags & BfCompilerOptionFlag_RuntimeChecks) != 0;
  9634. }
  9635. }
  9636. BF_EXPORT void BF_CALLTYPE BfCompiler_ForceRebuild(BfCompiler* bfCompiler)
  9637. {
  9638. bfCompiler->mOptions.mForceRebuildIdx++;
  9639. }
  9640. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetEmitSource(BfCompiler* bfCompiler, char* fileName)
  9641. {
  9642. String& outString = *gTLStrReturn.Get();
  9643. outString.clear();
  9644. bfCompiler->GetEmitSource(fileName, &outString);
  9645. if (outString.IsEmpty())
  9646. return NULL;
  9647. return outString.c_str();
  9648. }
  9649. BF_EXPORT int32 BF_CALLTYPE BfCompiler_GetEmitSourceVersion(BfCompiler* bfCompiler, char* fileName)
  9650. {
  9651. return bfCompiler->GetEmitSource(fileName, NULL);
  9652. }
  9653. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetEmitLocation(BfCompiler* bfCompiler, char* typeName, int line, int& outEmbedLine, int& outEmbedLineChar, uint64& outHash)
  9654. {
  9655. String& outString = *gTLStrReturn.Get();
  9656. outString.clear();
  9657. outString = bfCompiler->GetEmitLocation(typeName, line, outEmbedLine, outEmbedLineChar, outHash);
  9658. return outString.c_str();
  9659. }
  9660. BF_EXPORT bool BF_CALLTYPE BfCompiler_WriteEmitData(BfCompiler* bfCompiler, char* filePath, BfProject* project)
  9661. {
  9662. return bfCompiler->WriteEmitData(filePath, project);
  9663. }