2
0

BfCompiler.cpp 258 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321
  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 "BfCompiler.h"
  13. #include "BfSystem.h"
  14. #include "BfParser.h"
  15. #include "BfReducer.h"
  16. #include "BfExprEvaluator.h"
  17. #include "../Backend/BeLibManger.h"
  18. #include <fcntl.h>
  19. #include "BfConstResolver.h"
  20. #include "BfMangler.h"
  21. #include "BfDemangler.h"
  22. #include "BeefySysLib/util/PerfTimer.h"
  23. #include "BfSourceClassifier.h"
  24. #include "BfAutoComplete.h"
  25. #include "BfResolvePass.h"
  26. #include "BeefySysLib/util/BeefPerf.h"
  27. #include "../LLVMUtils.h"
  28. #pragma warning(pop)
  29. namespace llvm
  30. {
  31. extern bool DebugFlag;
  32. }
  33. #define SPLIT_CONTEXTS
  34. Beefy::BfCompiler* gBfCompiler = NULL;
  35. void pt(llvm::Type* t)
  36. {
  37. //Beefy::OutputDebugStrF("pv((llvm::Type*)%p)\n", t);
  38. Beefy::debug_ostream os;
  39. t->print(os);
  40. os << "\n";
  41. os << " isSized: " << t->isSized() << "\n";
  42. os.flush();
  43. if (auto pointerType = llvm::dyn_cast<llvm::PointerType>(t))
  44. {
  45. Beefy::OutputDebugStrF("Element: ");
  46. pt(pointerType->getElementType());
  47. }
  48. }
  49. void ppt(llvm::Type* t)
  50. {
  51. auto pointerType = llvm::dyn_cast<llvm::PointerType>(t);
  52. if (pointerType == NULL)
  53. {
  54. Beefy::OutputDebugStrF("Not a pointer type");
  55. return;
  56. }
  57. pt(pointerType->getElementType());
  58. }
  59. void pt(llvm::DINode* t)
  60. {
  61. Beefy::debug_ostream os;
  62. t->print(os);
  63. os << "\n";
  64. os.flush();
  65. }
  66. void pt(llvm::Value* v)
  67. {
  68. pt(v->getType());
  69. }
  70. void pv(const llvm::Value* v)
  71. {
  72. Beefy::debug_ostream os;
  73. v->print(os);
  74. os << "\n";
  75. os.flush();
  76. pt(v->getType());
  77. }
  78. void ppt(llvm::Value* v)
  79. {
  80. ppt(v->getType());
  81. }
  82. void pmd(llvm::Metadata* md)
  83. {
  84. Beefy::debug_ostream os;
  85. md->print(os);
  86. os << "\n";
  87. os.flush();
  88. }
  89. void pdl(llvm::DebugLoc& dl)
  90. {
  91. Beefy::debug_ostream os;
  92. dl.print(os);
  93. os << "\n";
  94. os.flush();
  95. }
  96. void pm(llvm::Module* module)
  97. {
  98. Beefy::debug_ostream os;
  99. module->print(os, NULL);
  100. os << "\n";
  101. os.flush();
  102. }
  103. void PrintUsers(llvm::MDNode* md)
  104. {
  105. /*Beefy::debug_ostream os;
  106. //auto val = llvm::ReplaceableMetadataImpl::get(*md);
  107. auto val = md->Context.getReplaceableUses();
  108. if (val == NULL)
  109. {
  110. os << "Not replaceable\n";
  111. }
  112. else
  113. {
  114. //md->print(os);
  115. typedef std::pair<void *, std::pair<llvm::MetadataTracking::OwnerTy, uint64_t>> UseTy;
  116. llvm::SmallVector<UseTy, 8> Uses(val->UseMap.begin(), val->UseMap.end());
  117. std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) {
  118. return L.second.second < R.second.second;
  119. });
  120. for (const auto &Pair : Uses)
  121. {
  122. auto Owner = Pair.second.first;
  123. os << Beefy::StrFormat(" %d %p %d\n", Pair.second.first.isNull(), Pair.first, Pair.second.second, Pair).c_str();
  124. }
  125. os << "\n";
  126. }
  127. os.flush();*/
  128. }
  129. void ptbf(Beefy::BfType* bfType)
  130. {
  131. Beefy::OutputDebugStrF("%s\n", bfType->GetModule()->TypeToString(bfType).c_str());
  132. }
  133. void pt(const Beefy::BfTypedValue& val)
  134. {
  135. Beefy::OutputDebugStrF("%s\n", val.mType->GetModule()->TypeToString(val.mType).c_str());
  136. }
  137. void pt(llvm::SmallVectorImpl<llvm::Value*>& llvmArgs)
  138. {
  139. Beefy::debug_ostream os;
  140. for (int i = 0; i < (int)llvmArgs.size(); i++)
  141. {
  142. if (i > 0)
  143. os << ", ";
  144. llvmArgs[i]->getType()->print(os);
  145. }
  146. os << "\n";
  147. os.flush();
  148. }
  149. void PrintUsers(llvm::Value* v)
  150. {
  151. for (auto user : v->users())
  152. {
  153. pt(user);
  154. }
  155. }
  156. /*void PrintFunc(Beefy::BfMethodInstance* methodInstance)
  157. {
  158. Beefy::debug_ostream os;
  159. methodInstance->mIRFunction.mLLVMValue->print(os);
  160. os << "\n";
  161. os.flush();
  162. }*/
  163. USING_NS_BF;
  164. using namespace llvm;
  165. int Beefy::BfWorkListEntry::sCurReqId = 0;
  166. GlobalVariable* AllocGlobalVariable(Module &M, Type *Ty, bool isConstant,
  167. GlobalValue::LinkageTypes Linkage, Constant *Initializer,
  168. const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
  169. GlobalValue::ThreadLocalMode tlm = GlobalValue::NotThreadLocal, unsigned AddressSpace = 0,
  170. bool isExternallyInitialized = false);
  171. #include "BeefySysLib/util/AllocDebug.h"
  172. //////////////////////////////////////////////////////////////////////////
  173. BfCompiler::HotData::~HotData()
  174. {
  175. for (auto& kv : mMethodMap)
  176. {
  177. auto hotMethod = kv.mValue;
  178. hotMethod->Clear();
  179. }
  180. for (auto& kv : mThisType)
  181. kv.mValue->Deref();
  182. for (auto& kv : mAllocation)
  183. kv.mValue->Deref();
  184. for (auto& kv : mDevirtualizedMethods)
  185. kv.mValue->Deref();
  186. for (auto& kv : mFuncPtrs)
  187. kv.mValue->Deref();
  188. for (auto& kv : mVirtualDecls)
  189. kv.mValue->Deref();
  190. for (auto& kv : mInnerMethods)
  191. kv.mValue->Deref();
  192. for (auto& kv : mMethodMap)
  193. kv.mValue->Deref();
  194. }
  195. template <typename TDict>
  196. static void DeleteUnused(TDict& dict)
  197. {
  198. auto itr = dict.begin();
  199. while (itr != dict.end())
  200. {
  201. auto val = itr->mValue;
  202. BF_ASSERT(val->mRefCount >= 1);
  203. if (val->mRefCount == 1)
  204. {
  205. val->Deref();
  206. itr = dict.Remove(itr);
  207. }
  208. else
  209. ++itr;
  210. }
  211. }
  212. template <typename TDict, typename TElement>
  213. static typename TDict::value_type AllocFromMap(TDict& dict, TElement* elem)
  214. {
  215. typename TDict::value_type* valuePtr;
  216. if (dict.TryAdd(elem, NULL, &valuePtr))
  217. {
  218. auto val = new typename std::remove_pointer<typename TDict::value_type>::type(elem);
  219. val->mRefCount++;
  220. *valuePtr = val;
  221. }
  222. return *valuePtr;
  223. }
  224. void BfCompiler::HotData::ClearUnused(bool isHotCompile)
  225. {
  226. BP_ZONE("BfCompiler::HotData::ClearUnused");
  227. DeleteUnused(mThisType);
  228. DeleteUnused(mAllocation);
  229. DeleteUnused(mDevirtualizedMethods);
  230. DeleteUnused(mVirtualDecls);
  231. DeleteUnused(mInnerMethods);
  232. if (isHotCompile)
  233. {
  234. // We need to keep all function pointer references ever, since we can't tell if we still reference them or not
  235. DeleteUnused(mFuncPtrs);
  236. }
  237. }
  238. BfHotThisType* BfCompiler::HotData::GetThisType(BfHotTypeVersion* hotVersion)
  239. {
  240. return AllocFromMap(mThisType, hotVersion);
  241. }
  242. BfHotAllocation* BfCompiler::HotData::GetAllocation(BfHotTypeVersion* hotVersion)
  243. {
  244. return AllocFromMap(mAllocation, hotVersion);
  245. }
  246. BfHotDevirtualizedMethod* BfCompiler::HotData::GetDevirtualizedMethod(BfHotMethod* hotMethod)
  247. {
  248. return AllocFromMap(mDevirtualizedMethods, hotMethod);
  249. }
  250. BfHotFunctionReference* BfCompiler::HotData::GetFunctionReference(BfHotMethod* hotMethod)
  251. {
  252. return AllocFromMap(mFuncPtrs, hotMethod);
  253. }
  254. BfHotInnerMethod* BfCompiler::HotData::GetInnerMethod(BfHotMethod* hotMethod)
  255. {
  256. return AllocFromMap(mInnerMethods, hotMethod);
  257. }
  258. BfHotVirtualDeclaration* BfCompiler::HotData::GetVirtualDeclaration(BfHotMethod* hotMethod)
  259. {
  260. return AllocFromMap(mVirtualDecls, hotMethod);
  261. }
  262. BfCompiler::HotState::~HotState()
  263. {
  264. }
  265. bool BfCompiler::HotState::HasPendingChanges(BfTypeInstance* type)
  266. {
  267. return (type->mHotTypeData != NULL) && (type->mHotTypeData->mPendingDataChange);
  268. }
  269. void BfCompiler::HotState::RemovePendingChanges(BfTypeInstance* type)
  270. {
  271. BF_ASSERT(type->mHotTypeData->mPendingDataChange);
  272. if (!type->mHotTypeData->mPendingDataChange)
  273. return;
  274. type->mHotTypeData->mPendingDataChange = false;
  275. bool didRemove = mPendingDataChanges.Remove(type->mTypeId);
  276. BF_ASSERT(didRemove);
  277. }
  278. BfCompiler::HotResolveData::~HotResolveData()
  279. {
  280. for (auto hotMethod : mActiveMethods)
  281. hotMethod->Deref();
  282. for (auto kv : mReachableMethods)
  283. kv.mKey->Deref();
  284. }
  285. //////////////////////////////////////////////////////////////////////////
  286. BfCompiler::BfCompiler(BfSystem* bfSystem, bool isResolveOnly)
  287. {
  288. //llvm::DebugFlag = true;
  289. memset(&mStats, 0, sizeof(mStats));
  290. mCompletionPct = 0;
  291. mCanceling = false;
  292. mIsResolveOnly = isResolveOnly;
  293. mResolvePassData = NULL;
  294. mPassInstance = NULL;
  295. mRevision = 0;
  296. mLastRevisionAborted = false;
  297. gBfCompiler = this;
  298. mSystem = bfSystem;
  299. mCurTypeId = 1;
  300. mTypeInitCount = 0;
  301. //mMaxInterfaceSlots = 16;
  302. mMaxInterfaceSlots = -1;
  303. mInterfaceSlotCountChanged = false;
  304. mHSPreserveIdx = 0;
  305. mCompileLogFP = NULL;
  306. mWantsDeferMethodDecls = false;
  307. mHadCancel = false;
  308. mCompileState = CompileState_None;
  309. //mMaxInterfaceSlots = 4;
  310. mContext = new BfContext(this);
  311. mHotData = NULL;
  312. mHotState = NULL;
  313. mHotResolveData = NULL;
  314. mArray1TypeDef = NULL;
  315. mArray2TypeDef = NULL;
  316. mArray3TypeDef = NULL;
  317. mArray4TypeDef = NULL;
  318. mSpanTypeDef = NULL;
  319. mAttributeTypeDef = NULL;
  320. mAttributeUsageAttributeTypeDef = NULL;
  321. mBfObjectTypeDef = NULL;
  322. mClassVDataTypeDef = NULL;
  323. mCLinkAttributeTypeDef = NULL;
  324. mCReprAttributeTypeDef = NULL;
  325. mAlignAttributeTypeDef = NULL;
  326. mNoDiscardAttributeTypeDef = NULL;
  327. mDisableObjectAccessChecksAttributeTypeDef = NULL;
  328. mDbgRawAllocDataTypeDef = NULL;
  329. mDeferredCallTypeDef = NULL;
  330. mDelegateTypeDef = NULL;
  331. mEnumTypeDef = NULL;
  332. mFriendAttributeTypeDef = NULL;
  333. mCheckedAttributeTypeDef = NULL;
  334. mUncheckedAttributeTypeDef = NULL;
  335. mFunctionTypeDef = NULL;
  336. mGCTypeDef = NULL;
  337. mGenericIEnumerableTypeDef = NULL;
  338. mGenericIEnumeratorTypeDef = NULL;
  339. mGenericIRefEnumeratorTypeDef = NULL;
  340. mInlineAttributeTypeDef = NULL;
  341. mInternalTypeDef = NULL;
  342. mIPrintableTypeDef = NULL;
  343. mIHashableTypeDef = NULL;
  344. mLinkNameAttributeTypeDef = NULL;
  345. mMethodRefTypeDef = NULL;
  346. mNullableTypeDef = NULL;
  347. mOrderedAttributeTypeDef = NULL;
  348. mPointerTTypeDef = NULL;
  349. mPointerTypeDef = NULL;
  350. mReflectArrayType = NULL;
  351. mReflectFieldDataDef = NULL;
  352. mReflectFieldSplatDataDef = NULL;
  353. mReflectMethodDataDef = NULL;
  354. mReflectParamDataDef = NULL;
  355. mReflectPointerType = NULL;
  356. mReflectSizedArrayType = NULL;
  357. mReflectSpecializedGenericType = NULL;
  358. mReflectTypeInstanceTypeDef = NULL;
  359. mReflectUnspecializedGenericType = NULL;
  360. mSizedArrayTypeDef = NULL;
  361. mSkipAccessCheckAttributeTypeDef = NULL;
  362. mStaticInitAfterAttributeTypeDef = NULL;
  363. mStaticInitPriorityAttributeTypeDef = NULL;
  364. mStringTypeDef = NULL;
  365. mThreadStaticAttributeTypeDef = NULL;
  366. mTypeTypeDef = NULL;
  367. mUnboundAttributeTypeDef = NULL;
  368. mValueTypeTypeDef = NULL;
  369. mObsoleteAttributeTypeDef = NULL;
  370. mErrorAttributeTypeDef = NULL;
  371. mWarnAttributeTypeDef = NULL;
  372. mLastAutocompleteModule = NULL;
  373. }
  374. BfCompiler::~BfCompiler()
  375. {
  376. delete mContext;
  377. delete mHotData;
  378. delete mHotState;
  379. delete mHotResolveData;
  380. }
  381. bool BfCompiler::IsTypeAccessible(BfType* checkType, BfProject* curProject)
  382. {
  383. if (checkType->IsBoxed())
  384. return IsTypeAccessible(((BfBoxedType*)checkType)->mElementType, curProject);
  385. BfTypeInstance* typeInst = checkType->ToTypeInstance();
  386. if (typeInst != NULL)
  387. {
  388. if (checkType->IsTuple())
  389. {
  390. for (auto&& fieldInst : typeInst->mFieldInstances)
  391. {
  392. if (!IsTypeAccessible(fieldInst.mResolvedType, curProject))
  393. return false;
  394. }
  395. }
  396. auto genericTypeInst = typeInst->ToGenericTypeInstance();
  397. if (genericTypeInst != NULL)
  398. {
  399. for (auto genericArg : genericTypeInst->mTypeGenericArguments)
  400. if (!IsTypeAccessible(genericArg, curProject))
  401. return false;
  402. }
  403. return curProject->ContainsReference(typeInst->mTypeDef->mProject);
  404. }
  405. if (checkType->IsPointer())
  406. return IsTypeAccessible(((BfPointerType*)checkType)->mElementType, curProject);
  407. if (checkType->IsRef())
  408. return IsTypeAccessible(((BfPointerType*)checkType)->mElementType, curProject);
  409. return true;
  410. }
  411. bool BfCompiler::IsTypeUsed(BfType* checkType, BfProject* curProject)
  412. {
  413. if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
  414. return IsTypeAccessible(checkType, curProject);
  415. BfTypeInstance* typeInst = checkType->ToTypeInstance();
  416. if (typeInst != NULL)
  417. {
  418. if ((typeInst->mTypeDef->mProject != NULL) && (typeInst->mTypeDef->mProject != curProject))
  419. {
  420. if (typeInst->mTypeDef->mProject->mTargetType == BfTargetType_BeefDynLib)
  421. return false;
  422. }
  423. if (checkType->IsInterface())
  424. return typeInst->mIsReified;
  425. //TODO: We could check to see if this project has any reified specialized instances...
  426. if (checkType->IsUnspecializedType())
  427. return typeInst->mIsReified;
  428. if (checkType->IsTuple())
  429. {
  430. for (auto&& fieldInst : typeInst->mFieldInstances)
  431. {
  432. if (!IsTypeUsed(fieldInst.mResolvedType, curProject))
  433. return false;
  434. }
  435. }
  436. auto genericTypeInst = typeInst->ToGenericTypeInstance();
  437. if (genericTypeInst != NULL)
  438. {
  439. for (auto genericArg : genericTypeInst->mTypeGenericArguments)
  440. if (!IsTypeUsed(genericArg, curProject))
  441. return false;
  442. }
  443. auto module = typeInst->GetModule();
  444. if (module == NULL)
  445. return true;
  446. return curProject->mUsedModules.Contains(module);
  447. }
  448. if (checkType->IsPointer())
  449. return IsTypeUsed(((BfPointerType*)checkType)->mElementType, curProject);
  450. if (checkType->IsRef())
  451. return IsTypeUsed(((BfPointerType*)checkType)->mElementType, curProject);
  452. return true;
  453. }
  454. bool BfCompiler::IsModuleAccessible(BfModule* module, BfProject* curProject)
  455. {
  456. for (auto checkType : module->mOwnedTypeInstances)
  457. {
  458. if (!IsTypeAccessible(checkType, curProject))
  459. return false;
  460. }
  461. return curProject->ContainsReference(module->mProject);
  462. }
  463. void BfCompiler::FixVDataHash(BfModule* bfModule)
  464. {
  465. // We recreate the actual vdata hash now that we're done creating new string literals
  466. /*for (auto context : mContexts)
  467. HASH128_MIXIN(bfModule->mDataHash, bfModule->mHighestUsedStringId);*/
  468. }
  469. void BfCompiler::CheckModuleStringRefs(BfModule* module, BfVDataModule* vdataModule, int lastModuleRevision, HashSet<int>& foundStringIds, HashSet<int>& dllNameSet, Array<BfMethodInstance*>& dllMethods, Array<BfCompiler::StringValueEntry>& stringValueEntries)
  470. {
  471. for (int stringId : module->mStringPoolRefs)
  472. {
  473. if (foundStringIds.Add(stringId))
  474. {
  475. BfStringPoolEntry& stringPoolEntry = module->mContext->mStringObjectIdMap[stringId];
  476. if (IsHotCompile())
  477. {
  478. if (vdataModule->mDefinedStrings.Contains(stringId))
  479. continue;
  480. }
  481. StringValueEntry stringEntry;
  482. stringEntry.mId = stringId;
  483. vdataModule->mDefinedStrings.Add(stringId);
  484. stringEntry.mStringVal = vdataModule->CreateStringObjectValue(stringPoolEntry.mString, stringId, true);
  485. stringValueEntries.Add(stringEntry);
  486. CompileLog("String %d %s\n", stringId, stringPoolEntry.mString.c_str());
  487. }
  488. }
  489. for (auto dllNameId : module->mImportFileNames)
  490. dllNameSet.Add(dllNameId);
  491. for (auto& dllImportEntry : module->mDllImportEntries)
  492. dllMethods.push_back(dllImportEntry.mMethodInstance);
  493. auto altModule = module->mNextAltModule;
  494. while (altModule != NULL)
  495. {
  496. CheckModuleStringRefs(altModule, vdataModule, lastModuleRevision, foundStringIds, dllNameSet, dllMethods, stringValueEntries);
  497. altModule = altModule->mNextAltModule;
  498. }
  499. for (auto& specModulePair : module->mSpecializedMethodModules)
  500. CheckModuleStringRefs(specModulePair.mValue, vdataModule, lastModuleRevision, foundStringIds, dllNameSet, dllMethods, stringValueEntries);
  501. }
  502. void BfCompiler::HashModuleVData(BfModule* module, HashContext& vdataHash)
  503. {
  504. BP_ZONE("BfCompiler::HashModuleVData");
  505. if (module->mStringPoolRefs.size() > 0)
  506. {
  507. module->mStringPoolRefs.Sort([](int lhs, int rhs) { return lhs < rhs; });
  508. vdataHash.Mixin(&module->mStringPoolRefs[0], (int)module->mStringPoolRefs.size() * (int)sizeof(int));
  509. }
  510. if (module->mImportFileNames.size() > 0)
  511. {
  512. module->mImportFileNames.Sort([](int lhs, int rhs) { return lhs < rhs; });
  513. vdataHash.Mixin(&module->mImportFileNames[0], (int)module->mImportFileNames.size() * (int)sizeof(int));
  514. }
  515. auto altModule = module->mNextAltModule;
  516. while (altModule != NULL)
  517. {
  518. HashModuleVData(altModule, vdataHash);
  519. altModule = altModule->mNextAltModule;
  520. }
  521. for (auto& specModulePair : module->mSpecializedMethodModules)
  522. {
  523. HashModuleVData(specModulePair.mValue, vdataHash);
  524. }
  525. }
  526. BfIRFunction BfCompiler::CreateLoadSharedLibraries(BfVDataModule* bfModule, Array<BfMethodInstance*>& dllMethods)
  527. {
  528. BfIRType nullPtrType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr));
  529. BfIRType nullPtrPtrType = bfModule->mBfIRBuilder->MapType(bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr)));
  530. BfIRType voidType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_None));
  531. SmallVector<BfIRType, 2> paramTypes;
  532. auto loadSharedLibrariesFuncType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes, false);
  533. auto loadSharedLibFunc = bfModule->mBfIRBuilder->CreateFunction(loadSharedLibrariesFuncType, BfIRLinkageType_External, "BfLoadSharedLibraries");
  534. bfModule->SetupIRMethod(NULL, loadSharedLibFunc, false);
  535. bfModule->mBfIRBuilder->SetActiveFunction(loadSharedLibFunc);
  536. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  537. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  538. HashSet<int> dllNameSet;
  539. auto internalType = bfModule->ResolveTypeDef(mInternalTypeDef);
  540. bfModule->PopulateType(internalType);
  541. auto getSharedProcAddressInstance = bfModule->GetMethodByName(internalType->ToTypeInstance(), "GetSharedProcAddressInto");
  542. auto loadSharedLibraryProc = bfModule->GetMethodByName(internalType->ToTypeInstance(), "LoadSharedLibraryInto");
  543. BF_ASSERT(getSharedProcAddressInstance);
  544. BF_ASSERT(loadSharedLibraryProc);
  545. if (!getSharedProcAddressInstance)
  546. {
  547. bfModule->Fail("Missing Internal.GetSharedProcAddressInto");
  548. return loadSharedLibFunc;
  549. }
  550. if (!loadSharedLibraryProc)
  551. {
  552. bfModule->Fail("Missing Internal.LoadSharedLibraryInto");
  553. return loadSharedLibFunc;
  554. }
  555. Dictionary<int, BfIRValue> dllHandleMap;
  556. for (auto methodInstance : dllMethods)
  557. {
  558. auto typeInstance = methodInstance->GetOwner();
  559. auto methodDef = methodInstance->mMethodDef;
  560. BF_ASSERT(methodInstance->GetCustomAttributes() != NULL);
  561. for (auto customAttr : methodInstance->GetCustomAttributes()->mAttributes)
  562. {
  563. if (customAttr.mType->mTypeDef->mFullName.ToString() == "System.ImportAttribute")
  564. {
  565. bool doCLink = false;
  566. bool undecorated = false;
  567. BfCallingConvention callingConvention = methodDef->mCallingConvention;
  568. for (auto fieldSet : customAttr.mSetField)
  569. {
  570. BfFieldDef* fieldDef = fieldSet.mFieldRef;
  571. if (fieldDef->mName == "CLink")
  572. {
  573. auto constant = typeInstance->mConstHolder->GetConstant(fieldSet.mParam.mValue);
  574. if (constant != NULL)
  575. doCLink = constant->mBool;
  576. }
  577. if (fieldDef->mName == "Undecorated")
  578. {
  579. auto constant = typeInstance->mConstHolder->GetConstant(fieldSet.mParam.mValue);
  580. if (constant != NULL)
  581. undecorated = constant->mBool;
  582. }
  583. if (fieldDef->mName == "CallingConvention")
  584. {
  585. auto constant = typeInstance->mConstHolder->GetConstant(fieldSet.mParam.mValue);
  586. if (constant != NULL)
  587. {
  588. int callingConventionVal = (int)constant->mInt32;
  589. if ((callingConventionVal == 3) || (callingConventionVal == 1))
  590. callingConvention = BfCallingConvention_Stdcall;
  591. else if (callingConventionVal == 2)
  592. callingConvention = BfCallingConvention_Cdecl;
  593. }
  594. }
  595. }
  596. if (customAttr.mCtorArgs.size() == 1)
  597. {
  598. auto fileNameArg = customAttr.mCtorArgs[0];
  599. int strNum = 0;
  600. auto constant = typeInstance->mConstHolder->GetConstant(fileNameArg);
  601. if (constant != NULL)
  602. {
  603. if (constant->IsNull())
  604. continue; // Invalid
  605. strNum = constant->mInt32;
  606. }
  607. else
  608. {
  609. strNum = bfModule->GetStringPoolIdx(fileNameArg, typeInstance->mConstHolder);
  610. }
  611. BfIRValue dllHandleVar;
  612. if (!dllHandleMap.TryGetValue(strNum, &dllHandleVar))
  613. {
  614. String dllHandleName = StrFormat("bf_hs_preserve@dllHandle%d", strNum);
  615. dllHandleVar = bfModule->mBfIRBuilder->CreateGlobalVariable(nullPtrType, false, BfIRLinkageType_External,
  616. bfModule->GetDefaultValue(bfModule->GetPrimitiveType(BfTypeCode_NullPtr)), dllHandleName);
  617. BfIRValue namePtr = bfModule->GetStringCharPtr(strNum);
  618. SmallVector<BfIRValue, 1> args;
  619. args.push_back(namePtr);
  620. args.push_back(dllHandleVar);
  621. BfIRValue dllHandleValue = bfModule->mBfIRBuilder->CreateCall(loadSharedLibraryProc.mFunc, args);
  622. dllHandleMap[strNum] = dllHandleVar;
  623. }
  624. String methodImportName;
  625. if (undecorated)
  626. {
  627. methodImportName = methodInstance->mMethodDef->mName;
  628. }
  629. else if (doCLink)
  630. {
  631. methodImportName = methodInstance->mMethodDef->mName;
  632. if ((mSystem->mPtrSize == 4) && (callingConvention == BfCallingConvention_Stdcall))
  633. {
  634. int argSize = (int)methodDef->mParams.size() * mSystem->mPtrSize;
  635. methodImportName = StrFormat("_%s$%d", methodImportName.c_str(), argSize);
  636. }
  637. }
  638. else
  639. BfMangler::Mangle(methodImportName, GetMangleKind(), methodInstance);
  640. BfIRValue methodNameValue = bfModule->mBfIRBuilder->CreateGlobalStringPtr(methodImportName);
  641. //auto moduleMethodInstance = bfModule->ReferenceExternalMethodInstance(methodInstance);
  642. //auto globalVarPtr = bfModule->mBfIRBuilder->CreateBitCast(moduleMethodInstance.mFunc, nullPtrPtrType);
  643. auto func = bfModule->CreateDllImportGlobalVar(methodInstance, false);
  644. auto globalVarPtr = bfModule->mBfIRBuilder->CreateBitCast(func, nullPtrPtrType);
  645. BfSizedVector<BfIRValue, 2> args;
  646. args.push_back(bfModule->mBfIRBuilder->CreateLoad(dllHandleVar));
  647. args.push_back(methodNameValue);
  648. args.push_back(globalVarPtr);
  649. BfIRValue dllFuncValVoidPtr = bfModule->mBfIRBuilder->CreateCall(getSharedProcAddressInstance.mFunc, args);
  650. }
  651. }
  652. }
  653. }
  654. bfModule->mBfIRBuilder->CreateRetVoid();
  655. return loadSharedLibFunc;
  656. }
  657. void BfCompiler::GetTestMethods(BfVDataModule* bfModule, Array<TestMethod>& testMethods, HashContext& vdataHashCtx)
  658. {
  659. vdataHashCtx.Mixin(0xBEEF0001); // Marker
  660. auto _CheckMethod = [&](BfTypeInstance* typeInstance, BfMethodInstance* methodInstance)
  661. {
  662. auto project = typeInstance->mTypeDef->mProject;
  663. if (project->mTargetType != BfTargetType_BeefTest)
  664. return;
  665. if (project != bfModule->mProject)
  666. return;
  667. bool isTest = false;
  668. if ((methodInstance->GetCustomAttributes() != NULL) &&
  669. (methodInstance->GetCustomAttributes()->Contains(mTestAttributeTypeDef)))
  670. isTest = true;
  671. if (!isTest)
  672. return;
  673. if (!methodInstance->mMethodDef->mIsStatic)
  674. {
  675. bfModule->Fail(StrFormat("Method '%s' cannot be used for testing because it is not static", bfModule->MethodToString(methodInstance).c_str()),
  676. methodInstance->mMethodDef->GetRefNode());
  677. bfModule->mHadBuildError = true;
  678. return;
  679. }
  680. if (methodInstance->GetParamCount() > 0)
  681. {
  682. if ((methodInstance->GetParamInitializer(0) == NULL) &&
  683. (methodInstance->GetParamKind(0) != BfParamKind_Params))
  684. {
  685. bfModule->Fail(StrFormat("Method '%s' cannot be used for testing because it contains parameters without defaults", bfModule->MethodToString(methodInstance).c_str()),
  686. methodInstance->mMethodDef->GetRefNode());
  687. bfModule->mHadBuildError = true;
  688. return;
  689. }
  690. }
  691. BF_ASSERT(typeInstance->IsReified());
  692. TestMethod testMethod;
  693. testMethod.mMethodInstance = methodInstance;
  694. testMethods.Add(testMethod);
  695. if (!bfModule->mProject->mUsedModules.Contains(typeInstance->mModule))
  696. bfModule->mProject->mUsedModules.Add(typeInstance->mModule);
  697. vdataHashCtx.Mixin(methodInstance->GetOwner()->mTypeId);
  698. vdataHashCtx.Mixin(methodInstance->mMethodDef->mIdx);
  699. };
  700. for (auto type : mContext->mResolvedTypes)
  701. {
  702. auto typeInstance = type->ToTypeInstance();
  703. if (typeInstance == NULL)
  704. continue;
  705. for (auto& methodInstanceGroup : typeInstance->mMethodInstanceGroups)
  706. {
  707. if (methodInstanceGroup.mDefault != NULL)
  708. {
  709. _CheckMethod(typeInstance, methodInstanceGroup.mDefault);
  710. }
  711. }
  712. }
  713. }
  714. void BfCompiler::EmitTestMethod(BfVDataModule* bfModule, Array<TestMethod>& testMethods, BfIRValue& retValue)
  715. {
  716. for (auto& testMethod : testMethods)
  717. {
  718. auto methodInstance = testMethod.mMethodInstance;
  719. auto typeInstance = methodInstance->GetOwner();
  720. testMethod.mName += bfModule->TypeToString(typeInstance);
  721. testMethod.mName += ".";
  722. testMethod.mName += methodInstance->mMethodDef->mName;
  723. testMethod.mName += "\t";
  724. auto testAttribute = methodInstance->GetCustomAttributes()->Get(mTestAttributeTypeDef);
  725. for (auto& field : testAttribute->mSetField)
  726. {
  727. auto constant = typeInstance->mConstHolder->GetConstant(field.mParam.mValue);
  728. if ((constant != NULL) && (constant->mTypeCode == BfTypeCode_Boolean) && (constant->mBool))
  729. {
  730. BfFieldDef* fieldDef = field.mFieldRef;
  731. if (fieldDef->mName == "ShouldFail")
  732. {
  733. testMethod.mName += "Sf";
  734. }
  735. else if (fieldDef->mName == "Profile")
  736. {
  737. testMethod.mName += "Pr";
  738. }
  739. else if (fieldDef->mName == "Ignore")
  740. {
  741. testMethod.mName += "Ig";
  742. }
  743. }
  744. }
  745. bfModule->UpdateSrcPos(methodInstance->mMethodDef->GetRefNode(), (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
  746. testMethod.mName += StrFormat("\t%s\t%d\t%d", bfModule->mCurFilePosition.mFileInstance->mParser->mFileName.c_str(), bfModule->mCurFilePosition.mCurLine, bfModule->mCurFilePosition.mCurColumn);
  747. }
  748. std::stable_sort(testMethods.begin(), testMethods.end(),
  749. [](const TestMethod& lhs, const TestMethod& rhs)
  750. {
  751. return lhs.mName < rhs.mName;
  752. });
  753. String methodData;
  754. for (int methodIdx = 0; methodIdx < (int)testMethods.size(); methodIdx++)
  755. {
  756. String& methodName = testMethods[methodIdx].mName;
  757. if (!methodData.IsEmpty())
  758. methodData += "\n";
  759. methodData += methodName;
  760. }
  761. //////////////////////////////////////////////////////////////////////////
  762. auto testInitMethod = bfModule->GetInternalMethod("Test_Init");
  763. auto testQueryMethod = bfModule->GetInternalMethod("Test_Query");
  764. auto testFinishMethod = bfModule->GetInternalMethod("Test_Finish");
  765. auto char8PtrType = bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_Char8));
  766. BfIRType strCharType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->GetPrimitiveType(BfTypeCode_Char8), (int)methodData.length() + 1);
  767. BfIRValue strConstant = bfModule->mBfIRBuilder->CreateConstString(methodData);
  768. BfIRValue gv = bfModule->mBfIRBuilder->CreateGlobalVariable(strCharType,
  769. true, BfIRLinkageType_External,
  770. strConstant, "__bfTestData");
  771. BfIRValue strPtrVal = bfModule->mBfIRBuilder->CreateBitCast(gv, bfModule->mBfIRBuilder->MapType(char8PtrType));
  772. SizedArray<BfIRValue, 4> irArgs;
  773. irArgs.Add(strPtrVal);
  774. bfModule->mBfIRBuilder->CreateCall(testInitMethod.mFunc, irArgs);
  775. BfIRBlock testHeadBlock = bfModule->mBfIRBuilder->CreateBlock("testHead");
  776. BfIRBlock testEndBlock = bfModule->mBfIRBuilder->CreateBlock("testEnd");
  777. bfModule->mBfIRBuilder->CreateBr(testHeadBlock);
  778. bfModule->mBfIRBuilder->AddBlock(testHeadBlock);
  779. bfModule->mBfIRBuilder->SetInsertPoint(testHeadBlock);
  780. irArgs.clear();
  781. auto testVal = bfModule->mBfIRBuilder->CreateCall(testQueryMethod.mFunc, irArgs);
  782. auto switchVal = bfModule->mBfIRBuilder->CreateSwitch(testVal, testEndBlock, (int)testMethods.size());
  783. for (int methodIdx = 0; methodIdx < (int)testMethods.size(); methodIdx++)
  784. {
  785. auto methodInstance = testMethods[methodIdx].mMethodInstance;
  786. String& methodName = testMethods[methodIdx].mName;
  787. auto testBlock = bfModule->mBfIRBuilder->CreateBlock(StrFormat("test%d", methodIdx));
  788. bfModule->mBfIRBuilder->AddSwitchCase(switchVal, bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, methodIdx), testBlock);
  789. bfModule->mBfIRBuilder->AddBlock(testBlock);
  790. bfModule->mBfIRBuilder->SetInsertPoint(testBlock);
  791. auto moduleMethodInstance = bfModule->ReferenceExternalMethodInstance(methodInstance);
  792. irArgs.clear();
  793. if (methodInstance->GetParamCount() > 0)
  794. {
  795. if (methodInstance->GetParamKind(0) == BfParamKind_Params)
  796. {
  797. auto paramType = methodInstance->GetParamType(0);
  798. auto paramTypeInst = paramType->ToTypeInstance();
  799. BfTypedValue paramVal = BfTypedValue(bfModule->mBfIRBuilder->CreateAlloca(bfModule->mBfIRBuilder->MapTypeInst(paramTypeInst)), paramType);
  800. bfModule->InitTypeInst(paramVal, NULL, false, BfIRValue());
  801. //TODO: Assert 'length' var is at slot 1
  802. auto arrayBits = bfModule->mBfIRBuilder->CreateBitCast(paramVal.mValue, bfModule->mBfIRBuilder->MapType(paramTypeInst->mBaseType));
  803. auto addr = bfModule->mBfIRBuilder->CreateInBoundsGEP(arrayBits, 0, 1);
  804. auto storeInst = bfModule->mBfIRBuilder->CreateAlignedStore(bfModule->GetConstValue(0), addr, 4);
  805. irArgs.Add(paramVal.mValue);
  806. }
  807. else
  808. {
  809. for (int defaultIdx = 0; defaultIdx < (int)methodInstance->mDefaultValues.size(); defaultIdx++)
  810. {
  811. irArgs.Add(methodInstance->mDefaultValues[defaultIdx]);
  812. }
  813. }
  814. }
  815. BfExprEvaluator exprEvaluator(bfModule);
  816. exprEvaluator.CreateCall(moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc, false, irArgs);
  817. bfModule->mBfIRBuilder->CreateBr(testHeadBlock);
  818. }
  819. bfModule->mBfIRBuilder->AddBlock(testEndBlock);
  820. bfModule->mBfIRBuilder->SetInsertPoint(testEndBlock);
  821. irArgs.clear();
  822. bfModule->mBfIRBuilder->CreateCall(testFinishMethod.mFunc, irArgs);
  823. retValue = bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 0);
  824. }
  825. void BfCompiler::CreateVData(BfVDataModule* bfModule)
  826. {
  827. bool isHotCompile = IsHotCompile();
  828. if ((isHotCompile) && (bfModule->mProject != mOptions.mHotProject))
  829. return;
  830. BP_ZONE("BfCompiler::CreateVData");
  831. BfLogSysM("CreateVData %s\n", bfModule->mProject->mName.c_str());
  832. CompileLog("CreateVData %s\n", bfModule->mProject->mName.c_str());
  833. bfModule->mProject->mUsedModules.Add(bfModule);
  834. auto project = bfModule->mProject;
  835. auto vdataContext = bfModule->mContext;
  836. BF_ASSERT(bfModule->mModuleName == "vdata");
  837. //////////////////////////////////////////////////////////////////////////
  838. // Create types we'll need for vdata, so we won't change the vdata hash afterward
  839. bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr));
  840. ///
  841. auto typeDefType = bfModule->ResolveTypeDef(mTypeTypeDef)->ToTypeInstance();
  842. if (!typeDefType)
  843. return;
  844. BF_ASSERT(typeDefType != NULL);
  845. vdataContext->mBfTypeType = typeDefType->ToTypeInstance();
  846. auto typeInstanceDefType = bfModule->ResolveTypeDef(mReflectTypeInstanceTypeDef);
  847. if (!typeInstanceDefType)
  848. return;
  849. auto typeInstanceDefTypeInstance = typeInstanceDefType->ToTypeInstance();
  850. auto typeDef = mSystem->FindTypeDef("System.ClassVData");
  851. BF_ASSERT(typeDef != NULL);
  852. auto bfClassVDataType = bfModule->ResolveTypeDef(typeDef)->ToTypeInstance();
  853. vdataContext->mBfClassVDataPtrType = bfModule->CreatePointerType(bfClassVDataType);
  854. //////////////////////////////////////////////////////////////////////////
  855. int numEntries = 0;
  856. int numConcreteTypes = 0;
  857. Array<BfType*> orderedTypes;
  858. for (auto type : mContext->mResolvedTypes)
  859. {
  860. numEntries++;
  861. BF_ASSERT((type != NULL) || (mPassInstance->HasFailed()));
  862. if (!type->IsReified())
  863. continue;
  864. orderedTypes.Add(type);
  865. CompileLog("TypeId:%d %s\n", type->mTypeId, bfModule->TypeToString(type).c_str());
  866. if ((type != NULL) && (type->IsObjectOrInterface()))
  867. {
  868. numConcreteTypes++;
  869. auto typeInst = type->ToTypeInstance();
  870. if (typeInst->mModule == NULL)
  871. {
  872. BF_ASSERT(mPassInstance->HasFailed());
  873. continue;
  874. }
  875. }
  876. }
  877. {
  878. BP_ZONE("BfCompiler::CreateVData sort orderedTypes");
  879. orderedTypes.Sort([](BfType* lhs, BfType* rhs)
  880. {
  881. return lhs->mTypeId < rhs->mTypeId;
  882. });
  883. }
  884. BfLogSysM("TypeEntries: %d ConcreteTypes: %d\n", numEntries, numConcreteTypes);
  885. HashContext vdataHashCtx;
  886. //vdataHashCtx.mDbgViz = true;
  887. vdataHashCtx.Mixin(bfModule->mProject->mVDataConfigHash);
  888. Array<TestMethod> testMethods;
  889. if (project->mTargetType == BfTargetType_BeefTest)
  890. GetTestMethods(bfModule, testMethods, vdataHashCtx);
  891. Array<BfType*> vdataTypeList;
  892. std::multimap<String, BfTypeInstance*> sortedStaticInitMap;
  893. std::multimap<String, BfTypeInstance*> sortedStaticDtorMap;
  894. std::multimap<String, BfTypeInstance*> sortedStaticMarkMap;
  895. std::multimap<String, BfTypeInstance*> sortedStaticTLSMap;
  896. HashSet<BfModule*> usedModuleSet;
  897. vdataHashCtx.MixinStr(project->mStartupObject);
  898. vdataHashCtx.Mixin(project->mTargetType);
  899. for (auto type : orderedTypes)
  900. {
  901. if (type == NULL)
  902. continue;
  903. if (type->IsTemporary())
  904. continue;
  905. if ((type->IsGenericParam()) || (type->IsUnspecializedTypeVariation()))
  906. continue;
  907. auto typeInst = type->ToTypeInstance();
  908. if ((typeInst != NULL) && (!typeInst->IsReified()) && (!typeInst->IsUnspecializedType()))
  909. continue;
  910. if (!IsTypeUsed(type, project))
  911. continue;
  912. vdataTypeList.push_back(type);
  913. vdataHashCtx.Mixin(type->mTypeId);
  914. BF_ASSERT((type != NULL) || (mPassInstance->HasFailed()));
  915. if ((type != NULL) && (typeInst != NULL))
  916. {
  917. auto module = typeInst->mModule;
  918. if (module == NULL)
  919. continue;
  920. if (type->IsInterface())
  921. vdataHashCtx.Mixin(typeInst->mSlotNum);
  922. if (!module->mIsScratchModule)
  923. {
  924. BF_ASSERT(module->mIsReified);
  925. if (usedModuleSet.Add(module))
  926. {
  927. CompileLog("UsedModule %p %s\n", module, module->mModuleName.c_str());
  928. HashModuleVData(module, vdataHashCtx);
  929. }
  930. }
  931. vdataHashCtx.MixinStr(module->mModuleName);
  932. vdataHashCtx.Mixin(typeInst->mTypeDef->mSignatureHash);
  933. vdataHashCtx.Mixin(module->mHasForceLinkMarker);
  934. for (auto iface : typeInst->mInterfaces)
  935. {
  936. vdataHashCtx.Mixin(iface.mInterfaceType->mTypeId);
  937. vdataHashCtx.Mixin(iface.mDeclaringType->mTypeCode);
  938. vdataHashCtx.Mixin(iface.mDeclaringType->mProject);
  939. }
  940. if (!typeInst->IsUnspecializedType())
  941. {
  942. for (auto& methodInstGroup : typeInst->mMethodInstanceGroups)
  943. {
  944. bool isImplementedAndReified = (methodInstGroup.IsImplemented()) && (methodInstGroup.mDefault != NULL) &&
  945. (methodInstGroup.mDefault->mIsReified) && (!methodInstGroup.mDefault->mIsUnspecialized);
  946. vdataHashCtx.Mixin(isImplementedAndReified);
  947. }
  948. }
  949. // Could be necessary if a base type in another project adds new virtual methods (for example)
  950. auto baseType = typeInst->mBaseType;
  951. while (baseType != NULL)
  952. {
  953. vdataHashCtx.Mixin(baseType->mTypeDef->mSignatureHash);
  954. baseType = baseType->mBaseType;
  955. }
  956. if (module->mProject != bfModule->mProject)
  957. {
  958. if ((module->mProject != NULL) && (module->mProject->mTargetType == BfTargetType_BeefDynLib))
  959. continue;
  960. }
  961. if (typeInst->mHasStaticInitMethod)
  962. sortedStaticInitMap.insert(std::make_pair(bfModule->TypeToString(type), typeInst));
  963. else if (typeInst->mHasStaticDtorMethod) // Only store types not already in the static init map
  964. sortedStaticDtorMap.insert(std::make_pair(bfModule->TypeToString(type), typeInst));
  965. if ((typeInst->mHasStaticMarkMethod) && (mOptions.mEnableRealtimeLeakCheck))
  966. sortedStaticMarkMap.insert(std::make_pair(bfModule->TypeToString(type), typeInst));
  967. if ((typeInst->mHasTLSFindMethod) && (mOptions.mEnableRealtimeLeakCheck))
  968. sortedStaticTLSMap.insert(std::make_pair(bfModule->TypeToString(type), typeInst));
  969. }
  970. }
  971. int lastModuleRevision = bfModule->mRevision;
  972. Val128 vdataHash = vdataHashCtx.Finish128();
  973. bool wantsRebuild = vdataHash != bfModule->mDataHash;
  974. if (bfModule->mHadBuildError)
  975. wantsRebuild = true;
  976. // If we did one of those 'hot compile' partial vdata builds, now build the whole thing
  977. if ((!IsHotCompile()) && (bfModule->mHadHotObjectWrites))
  978. wantsRebuild = true;
  979. if (mOptions.mHotProject != NULL)
  980. {
  981. HashContext vdataHashCtxEx;
  982. vdataHashCtxEx.Mixin(mOptions.mHotProject->mName);
  983. vdataHashCtxEx.Mixin((int)mHotState->mNewlySlottedTypeIds.size());
  984. for (auto typeId : mHotState->mNewlySlottedTypeIds)
  985. vdataHashCtxEx.Mixin(typeId);
  986. vdataHashCtxEx.Mixin((int)mHotState->mSlotDefineTypeIds.size());
  987. for (auto typeId : mHotState->mSlotDefineTypeIds)
  988. vdataHashCtxEx.Mixin(typeId);
  989. Val128 vdataHashEx = vdataHashCtxEx.Finish128();
  990. if (mHotState->mVDataHashEx.IsZero())
  991. {
  992. if (!mHotState->mNewlySlottedTypeIds.IsEmpty())
  993. wantsRebuild = true;
  994. if (!mHotState->mSlotDefineTypeIds.IsEmpty())
  995. wantsRebuild = true;
  996. }
  997. else
  998. {
  999. if (vdataHashEx != mHotState->mVDataHashEx)
  1000. wantsRebuild = true;
  1001. }
  1002. mHotState->mVDataHashEx = vdataHashEx;
  1003. }
  1004. if ((wantsRebuild) || (bfModule->mIsModuleMutable))
  1005. {
  1006. bfModule->StartNewRevision();
  1007. if (bfModule->mAwaitingInitFinish)
  1008. bfModule->FinishInit();
  1009. }
  1010. // We add the string hash into vdata hash later
  1011. bfModule->mDataHash = vdataHash;//vdataPreStringHash;
  1012. // This handles "no StartNewRevision" 'else' case, but also handles if vdata failed to complete from a previous compilation
  1013. if (!bfModule->mIsModuleMutable)
  1014. {
  1015. CompileLog("VData unchanged, skipping\n");
  1016. return;
  1017. }
  1018. BfTypeInstance* stringType = bfModule->ResolveTypeDef(mStringTypeDef, BfPopulateType_Data)->ToTypeInstance();
  1019. BfTypeInstance* reflectSpecializedTypeInstance = bfModule->ResolveTypeDef(mReflectSpecializedGenericType)->ToTypeInstance();
  1020. BfTypeInstance* reflectUnspecializedTypeInstance = bfModule->ResolveTypeDef(mReflectUnspecializedGenericType)->ToTypeInstance();
  1021. BfTypeInstance* reflectArrayTypeInstance = bfModule->ResolveTypeDef(mReflectArrayType)->ToTypeInstance();
  1022. bool madeBfTypeData = false;
  1023. bool needsTypeList = bfModule->IsMethodImplementedAndReified(typeDefType, "GetType");
  1024. bool needsObjectTypeData = needsTypeList || bfModule->IsMethodImplementedAndReified(vdataContext->mBfObjectType, "RawGetType") || bfModule->IsMethodImplementedAndReified(vdataContext->mBfObjectType, "GetType");
  1025. bool needsTypeNames = bfModule->IsMethodImplementedAndReified(typeDefType, "GetName");
  1026. bool needsStringLiteralList = (mOptions.mAllowHotSwapping) || (bfModule->IsMethodImplementedAndReified(stringType, "Intern"));
  1027. Dictionary<int, int> usedStringIdMap;
  1028. HashSet<BfType*> reflectTypeSet;
  1029. reflectTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectTypeInstanceTypeDef));
  1030. reflectTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectSpecializedGenericType));
  1031. reflectTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectUnspecializedGenericType));
  1032. reflectTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectArrayType));
  1033. SmallVector<BfIRValue, 256> typeDataVector;
  1034. for (auto type : vdataTypeList)
  1035. {
  1036. if (type->IsTypeAlias())
  1037. continue;
  1038. if (type->IsTypeInstance())
  1039. BF_ASSERT(!type->IsIncomplete());
  1040. auto typeInst = type->ToTypeInstance();
  1041. if ((typeInst != NULL) && (!typeInst->IsReified()) && (!typeInst->IsUnspecializedType()))
  1042. continue;
  1043. bool needsTypeData = (needsTypeList) || ((type->IsObject()) && (needsObjectTypeData));
  1044. bool forceReflectFields = false;
  1045. if (bfModule->mProject->mReferencedTypeData.Contains(type))
  1046. {
  1047. needsTypeData = true;
  1048. if (type->IsEnum())
  1049. forceReflectFields = true;
  1050. }
  1051. bool needsVData = (type->IsObject()) && (typeInst->mHasBeenInstantiated);
  1052. BfIRValue typeVariable;
  1053. if ((needsTypeData) || (needsVData))
  1054. {
  1055. if (reflectTypeSet.Contains(type))
  1056. {
  1057. needsTypeData = true;
  1058. needsVData = true;
  1059. }
  1060. typeVariable = bfModule->CreateTypeData(type, usedStringIdMap, forceReflectFields, needsTypeData, needsTypeNames, needsVData);
  1061. }
  1062. type->mDirty = false;
  1063. if (needsTypeList)
  1064. {
  1065. int typeId = type->mTypeId;
  1066. if (typeId == -1)
  1067. continue;
  1068. if (typeId >= (int)typeDataVector.size())
  1069. typeDataVector.resize(typeId + 1);
  1070. typeDataVector[typeId] = typeVariable;
  1071. }
  1072. }
  1073. for (int typeId = 0; typeId < (int)typeDataVector.size(); typeId++)
  1074. {
  1075. if (!typeDataVector[typeId])
  1076. typeDataVector[typeId] = bfModule->GetDefaultValue(typeDefType);
  1077. }
  1078. // We only need 'sTypes' if we actually reference it
  1079. //
  1080. {
  1081. auto typeDefPtrType = bfModule->CreatePointerType(typeDefType);
  1082. StringT<128> typesVariableName;
  1083. BfMangler::MangleStaticFieldName(typesVariableName, GetMangleKind(), typeDefType->ToTypeInstance(), "sTypes", typeDefPtrType);
  1084. auto arrayType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->MapType(typeDefType), (int)typeDataVector.size());
  1085. auto typeDataConst = bfModule->mBfIRBuilder->CreateConstArray(arrayType, typeDataVector);
  1086. BfIRValue typeDataArray = bfModule->mBfIRBuilder->CreateGlobalVariable(arrayType, true, BfIRLinkageType_External,
  1087. typeDataConst, typesVariableName);
  1088. }
  1089. HashSet<int> foundStringIds;
  1090. for (int stringId : bfModule->mStringPoolRefs)
  1091. foundStringIds.Add(stringId);
  1092. Array<BfModule*> orderedUsedModules;
  1093. for (auto module : usedModuleSet)
  1094. orderedUsedModules.push_back(module);
  1095. std::sort(orderedUsedModules.begin(), orderedUsedModules.end(), [] (BfModule* lhs, BfModule* rhs)
  1096. {
  1097. return lhs->mModuleName < rhs->mModuleName;
  1098. });
  1099. Array<BfMethodInstance*> dllMethods;
  1100. Array<BfIRValue> forceLinkValues;
  1101. HashSet<int> dllNameSet;
  1102. Array<BfCompiler::StringValueEntry> stringValueEntries;
  1103. for (auto module : orderedUsedModules)
  1104. {
  1105. CheckModuleStringRefs(module, bfModule, lastModuleRevision, foundStringIds, dllNameSet, dllMethods, stringValueEntries);
  1106. if ((module->mHasForceLinkMarker) &&
  1107. ((!isHotCompile) || (module->mHadHotObjectWrites)))
  1108. forceLinkValues.Add(bfModule->CreateForceLinkMarker(module, NULL));
  1109. }
  1110. if (!forceLinkValues.IsEmpty())
  1111. {
  1112. auto elemType = bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_Int8));
  1113. auto arrayType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->MapType(elemType), (int)forceLinkValues.size());
  1114. auto typeDataConst = bfModule->mBfIRBuilder->CreateConstArray(arrayType, forceLinkValues);
  1115. BfIRValue typeDataArray = bfModule->mBfIRBuilder->CreateGlobalVariable(arrayType, true, BfIRLinkageType_Internal,
  1116. typeDataConst, "FORCELINK_MODULES");
  1117. }
  1118. // Generate strings array
  1119. {
  1120. if (!needsStringLiteralList)
  1121. {
  1122. stringValueEntries.Clear();
  1123. }
  1124. std::sort(stringValueEntries.begin(), stringValueEntries.end(),
  1125. [](const StringValueEntry& lhs, const StringValueEntry& rhs)
  1126. {
  1127. return lhs.mId < rhs.mId;
  1128. });
  1129. auto stringPtrType = bfModule->CreatePointerType(stringType);
  1130. auto stringPtrIRType = bfModule->mBfIRBuilder->MapTypeInstPtr(stringType);
  1131. StringT<128> stringsVariableName;
  1132. BfMangler::MangleStaticFieldName(stringsVariableName, GetMangleKind(), stringType->ToTypeInstance(), "sStringLiterals", stringPtrType);
  1133. Array<BfIRValue> stringList;
  1134. stringList.Add(bfModule->mBfIRBuilder->CreateConstNull(stringPtrIRType));
  1135. for (auto& stringValueEntry : stringValueEntries)
  1136. stringList.Add(stringValueEntry.mStringVal);
  1137. stringList.Add(bfModule->mBfIRBuilder->CreateConstNull(stringPtrIRType));
  1138. BfIRType stringArrayType = bfModule->mBfIRBuilder->GetSizedArrayType(stringPtrIRType, (int)stringList.size());
  1139. auto stringArray = bfModule->mBfIRBuilder->CreateConstArray(stringArrayType, stringList);
  1140. auto stringArrayVar = bfModule->mBfIRBuilder->CreateGlobalVariable(stringArrayType, true, BfIRLinkageType_External, stringArray, stringsVariableName);
  1141. if (bfModule->mBfIRBuilder->DbgHasInfo())
  1142. {
  1143. auto dbgArrayType = bfModule->mBfIRBuilder->DbgCreateArrayType(stringList.size() * mSystem->mPtrSize * 8, mSystem->mPtrSize * 8, bfModule->mBfIRBuilder->DbgGetType(stringPtrType), (int)stringList.size());
  1144. bfModule->mBfIRBuilder->DbgCreateGlobalVariable(bfModule->mDICompileUnit, stringsVariableName, stringsVariableName, NULL, 0, dbgArrayType, false, stringArrayVar);
  1145. }
  1146. }
  1147. // Generate string ID array
  1148. {
  1149. auto stringType = bfModule->ResolveTypeDef(mStringTypeDef, BfPopulateType_Data)->ToTypeInstance();
  1150. auto stringPtrType = bfModule->CreatePointerType(stringType);
  1151. auto stringPtrIRType = bfModule->mBfIRBuilder->MapTypeInstPtr(stringType);
  1152. StringT<128> stringsVariableName;
  1153. BfMangler::MangleStaticFieldName(stringsVariableName, GetMangleKind(), stringType->ToTypeInstance(), "sIdStringLiterals", stringPtrType);
  1154. Array<BfIRValue> stringList;
  1155. stringList.Resize(usedStringIdMap.size());
  1156. for (auto& kv : usedStringIdMap)
  1157. {
  1158. stringList[kv.mValue] = bfModule->mStringObjectPool[kv.mKey];
  1159. }
  1160. BfIRType stringArrayType = bfModule->mBfIRBuilder->GetSizedArrayType(stringPtrIRType, (int)usedStringIdMap.size());
  1161. auto stringArray = bfModule->mBfIRBuilder->CreateConstArray(stringArrayType, stringList);
  1162. auto stringArrayVar = bfModule->mBfIRBuilder->CreateGlobalVariable(stringArrayType, true, BfIRLinkageType_External, stringArray, stringsVariableName);
  1163. if (bfModule->mBfIRBuilder->DbgHasInfo())
  1164. {
  1165. auto dbgArrayType = bfModule->mBfIRBuilder->DbgCreateArrayType(stringList.size() * mSystem->mPtrSize * 8, mSystem->mPtrSize * 8, bfModule->mBfIRBuilder->DbgGetType(stringPtrType), (int)stringList.size());
  1166. bfModule->mBfIRBuilder->DbgCreateGlobalVariable(bfModule->mDICompileUnit, stringsVariableName, stringsVariableName, NULL, 0, dbgArrayType, false, stringArrayVar);
  1167. }
  1168. }
  1169. BfIRFunction loadSharedLibFunc = CreateLoadSharedLibraries(bfModule, dllMethods);
  1170. BfIRType nullPtrType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr));
  1171. BfIRType voidType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_None));
  1172. BfIRType int32Type = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_Int32));
  1173. struct _StaticInitEntry
  1174. {
  1175. int mPriority;
  1176. BfTypeInstance* mTypeInstance;
  1177. };
  1178. Array<_StaticInitEntry> staticInitList;
  1179. // Populate staticInitList
  1180. {
  1181. Dictionary<int, BfTypeInstance*> pendingIDToInstanceMap;
  1182. HashSet<BfTypeInstance*> handledTypes;
  1183. BfType* staticInitPriorityAttributeType = vdataContext->mUnreifiedModule->ResolveTypeDef(mStaticInitPriorityAttributeTypeDef);
  1184. BfType* staticInitAfterAttributeType = vdataContext->mUnreifiedModule->ResolveTypeDef(mStaticInitAfterAttributeTypeDef);
  1185. bool forceAdd = false;
  1186. for (int pass = 0; true; pass++)
  1187. {
  1188. bool hadAdd = false;
  1189. for (auto& mapEntry : sortedStaticInitMap)
  1190. {
  1191. auto typeInst = mapEntry.second;
  1192. if ((typeInst != NULL) && (!typeInst->IsUnspecializedType()) && (typeInst->mHasStaticInitMethod))
  1193. {
  1194. if (pass == 0)
  1195. {
  1196. int priority = 0;
  1197. bool hadInitAfterAttribute = false;
  1198. if (typeInst->mCustomAttributes != NULL)
  1199. {
  1200. for (auto& customAttr : typeInst->mCustomAttributes->mAttributes)
  1201. {
  1202. if (customAttr.mType == staticInitAfterAttributeType)
  1203. hadInitAfterAttribute = true;
  1204. if (customAttr.mType == staticInitPriorityAttributeType)
  1205. {
  1206. if (customAttr.mCtorArgs.size() == 1)
  1207. {
  1208. auto constant = typeInst->mConstHolder->GetConstant(customAttr.mCtorArgs[0]);
  1209. if (constant != NULL)
  1210. priority = constant->mInt32;
  1211. }
  1212. }
  1213. }
  1214. }
  1215. if (!hadInitAfterAttribute)
  1216. {
  1217. staticInitList.push_back({ priority, typeInst });
  1218. mapEntry.second = NULL;
  1219. }
  1220. else
  1221. {
  1222. pendingIDToInstanceMap.TryAdd(typeInst->mTypeId, typeInst);
  1223. }
  1224. }
  1225. else
  1226. {
  1227. if (pendingIDToInstanceMap.ContainsKey(typeInst->mTypeId))
  1228. {
  1229. bool doAdd = true;
  1230. if (!forceAdd)
  1231. {
  1232. for (auto& customAttr : typeInst->mCustomAttributes->mAttributes)
  1233. {
  1234. if (customAttr.mType == staticInitAfterAttributeType)
  1235. {
  1236. if (customAttr.mCtorArgs.size() == 0)
  1237. {
  1238. doAdd = false;
  1239. }
  1240. else
  1241. {
  1242. auto ctorArg = customAttr.mCtorArgs[0];
  1243. auto constant = typeInst->mConstHolder->GetConstant(ctorArg);
  1244. if (constant != NULL)
  1245. {
  1246. int refTypeId = constant->mInt32;
  1247. if (pendingIDToInstanceMap.ContainsKey(refTypeId))
  1248. doAdd = false;
  1249. }
  1250. }
  1251. }
  1252. }
  1253. }
  1254. if (doAdd)
  1255. {
  1256. staticInitList.push_back({ 0, typeInst });
  1257. pendingIDToInstanceMap.Remove(typeInst->mTypeId);
  1258. hadAdd = true;
  1259. }
  1260. }
  1261. }
  1262. }
  1263. }
  1264. if (pass == 0)
  1265. {
  1266. std::sort(staticInitList.begin(), staticInitList.end(),
  1267. [](const _StaticInitEntry& lhs, const _StaticInitEntry& rhs)
  1268. {
  1269. return lhs.mPriority > rhs.mPriority;
  1270. });
  1271. }
  1272. if ((pass > 0) && (!hadAdd) && (pendingIDToInstanceMap.size() > 0)) // Circular ref?
  1273. forceAdd = true;
  1274. if (pendingIDToInstanceMap.size() == 0)
  1275. break;
  1276. }
  1277. }
  1278. // We want to call DTORS in reverse order from CTORS
  1279. Array<BfTypeInstance*> dtorList;
  1280. for (intptr idx = staticInitList.size() - 1; idx >= 0; idx--)
  1281. {
  1282. auto typeInst = staticInitList[idx].mTypeInstance;
  1283. if (typeInst->mHasStaticDtorMethod)
  1284. {
  1285. dtorList.push_back(typeInst);
  1286. }
  1287. }
  1288. for (auto itr = sortedStaticDtorMap.rbegin(); itr != sortedStaticDtorMap.rend(); itr++)
  1289. {
  1290. auto typeInst = itr->second;
  1291. dtorList.push_back(typeInst);
  1292. }
  1293. /// Generate "BfCallAllStaticDtors"
  1294. BfIRFunction dtorFunc;
  1295. {
  1296. SmallVector<BfIRType, 2> paramTypes;
  1297. auto dtorFuncType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes, false);
  1298. dtorFunc = bfModule->mBfIRBuilder->CreateFunction(dtorFuncType, BfIRLinkageType_External, "BfCallAllStaticDtors");
  1299. bfModule->SetupIRMethod(NULL, dtorFunc, false);
  1300. bfModule->mBfIRBuilder->SetActiveFunction(dtorFunc);
  1301. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1302. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1303. for (auto typeInst : dtorList)
  1304. {
  1305. for (auto& methodGroup : typeInst->mMethodInstanceGroups)
  1306. {
  1307. auto methodInstance = methodGroup.mDefault;
  1308. if ((methodInstance != NULL) &&
  1309. (methodInstance->mMethodDef->mIsStatic) &&
  1310. (methodInstance->mMethodDef->mMethodType == BfMethodType_Dtor) &&
  1311. ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
  1312. {
  1313. if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
  1314. continue;
  1315. if (methodInstance->mHotMethod != NULL)
  1316. methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
  1317. auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
  1318. bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
  1319. }
  1320. }
  1321. }
  1322. bfModule->mBfIRBuilder->CreateRetVoid();
  1323. }
  1324. // Generate "main"
  1325. if (!IsHotCompile())
  1326. {
  1327. BfIRFunctionType mainFuncType;
  1328. BfIRFunction mainFunc;
  1329. if ((project->mTargetType == BfTargetType_BeefConsoleApplication) || (project->mTargetType == BfTargetType_BeefTest))
  1330. {
  1331. SmallVector<BfIRType, 2> paramTypes;
  1332. paramTypes.push_back(int32Type);
  1333. paramTypes.push_back(nullPtrType);
  1334. mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
  1335. mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "main");
  1336. bfModule->SetupIRMethod(NULL, mainFunc, false);
  1337. }
  1338. else if (project->mTargetType == BfTargetType_BeefDynLib)
  1339. {
  1340. SmallVector<BfIRType, 4> paramTypes;
  1341. paramTypes.push_back(nullPtrType); // hinstDLL
  1342. paramTypes.push_back(int32Type); // fdwReason
  1343. paramTypes.push_back(nullPtrType); // lpvReserved
  1344. mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
  1345. mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "DllMain");
  1346. if (mOptions.mMachineType == BfMachineType_x86)
  1347. bfModule->mBfIRBuilder->SetFuncCallingConv(mainFunc, BfIRCallingConv_StdCall);
  1348. bfModule->SetupIRMethod(NULL, mainFunc, false);
  1349. }
  1350. else if (project->mTargetType == BfTargetType_BeefWindowsApplication)
  1351. {
  1352. SmallVector<BfIRType, 4> paramTypes;
  1353. paramTypes.push_back(nullPtrType); // hInstance
  1354. paramTypes.push_back(nullPtrType); // hPrevInstance
  1355. paramTypes.push_back(nullPtrType); // lpCmdLine
  1356. paramTypes.push_back(int32Type); // nCmdShow
  1357. mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
  1358. mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "WinMain");
  1359. if (mOptions.mMachineType == BfMachineType_x86)
  1360. bfModule->mBfIRBuilder->SetFuncCallingConv(mainFunc, BfIRCallingConv_StdCall);
  1361. bfModule->SetupIRMethod(NULL, mainFunc, false);
  1362. }
  1363. else
  1364. {
  1365. SmallVector<BfIRType, 2> paramTypes;
  1366. paramTypes.push_back(int32Type);
  1367. paramTypes.push_back(nullPtrType);
  1368. mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
  1369. mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "BeefMain");
  1370. bfModule->SetupIRMethod(NULL, mainFunc, false);
  1371. }
  1372. bfModule->mBfIRBuilder->SetActiveFunction(mainFunc);
  1373. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1374. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1375. #ifndef BF_PLATFORM_WINDOWS
  1376. {
  1377. SmallVector<BfIRType, 2> paramTypes;
  1378. paramTypes.push_back(int32Type);
  1379. paramTypes.push_back(nullPtrType);
  1380. auto setCmdLineFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
  1381. auto setCmdLineFunc = bfModule->mBfIRBuilder->CreateFunction(setCmdLineFuncType, BfIRLinkageType_External, "BfpSystem_SetCommandLine");
  1382. bfModule->SetupIRMethod(NULL, setCmdLineFunc, false);
  1383. SmallVector<BfIRValue, 2> args;
  1384. args.push_back(bfModule->mBfIRBuilder->GetArgument(0));
  1385. args.push_back(bfModule->mBfIRBuilder->GetArgument(1));
  1386. bfModule->mBfIRBuilder->CreateCall(setCmdLineFunc, args);
  1387. }
  1388. #endif
  1389. BfIRBlock initSkipBlock;
  1390. if (project->mTargetType == BfTargetType_BeefDynLib)
  1391. {
  1392. auto initBlock = bfModule->mBfIRBuilder->CreateBlock("doInit", false);
  1393. initSkipBlock = bfModule->mBfIRBuilder->CreateBlock("skipInit", false);
  1394. auto cmpResult = bfModule->mBfIRBuilder->CreateCmpEQ(bfModule->mBfIRBuilder->GetArgument(1), bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 1));
  1395. bfModule->mBfIRBuilder->CreateCondBr(cmpResult, initBlock, initSkipBlock);
  1396. bfModule->mBfIRBuilder->AddBlock(initBlock);
  1397. bfModule->mBfIRBuilder->SetInsertPoint(initBlock);
  1398. auto moduleMethodInstance = bfModule->GetInternalMethod("SetModuleHandle", 1);
  1399. if (moduleMethodInstance)
  1400. {
  1401. SmallVector<BfIRValue, 1> args;
  1402. args.push_back(bfModule->mBfIRBuilder->GetArgument(0));
  1403. bfModule->mBfIRBuilder->CreateCall(moduleMethodInstance.mFunc, args);
  1404. }
  1405. }
  1406. // Do the LoadLibrary calls below priority 100
  1407. bool didSharedLibLoad = false;
  1408. auto _CheckSharedLibLoad = [&]()
  1409. {
  1410. if (!didSharedLibLoad)
  1411. {
  1412. bfModule->mBfIRBuilder->CreateCall(loadSharedLibFunc, SmallVector<BfIRValue, 0>());
  1413. didSharedLibLoad = true;
  1414. }
  1415. };
  1416. for (auto& staticInitEntry : staticInitList)
  1417. {
  1418. if (staticInitEntry.mPriority < 100)
  1419. _CheckSharedLibLoad();
  1420. auto typeInst = staticInitEntry.mTypeInstance;
  1421. for (auto& methodGroup : typeInst->mMethodInstanceGroups)
  1422. {
  1423. auto methodInstance = methodGroup.mDefault;
  1424. if ((methodInstance != NULL) &&
  1425. (methodInstance->mMethodDef->mIsStatic) &&
  1426. (methodInstance->mMethodDef->mMethodType == BfMethodType_Ctor) &&
  1427. ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
  1428. {
  1429. if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
  1430. continue;
  1431. auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
  1432. if (methodInstance->mHotMethod != NULL)
  1433. methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
  1434. bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
  1435. }
  1436. }
  1437. }
  1438. _CheckSharedLibLoad();
  1439. if (initSkipBlock)
  1440. {
  1441. bfModule->mBfIRBuilder->CreateBr(initSkipBlock);
  1442. bfModule->mBfIRBuilder->AddBlock(initSkipBlock);
  1443. bfModule->mBfIRBuilder->SetInsertPoint(initSkipBlock);
  1444. }
  1445. BfIRValue retValue;
  1446. if ((project->mTargetType == BfTargetType_BeefConsoleApplication) || (project->mTargetType == BfTargetType_BeefWindowsApplication) ||
  1447. (project->mTargetType == BfTargetType_BeefApplication_StaticLib) || (project->mTargetType == BfTargetType_BeefApplication_DynamicLib))
  1448. {
  1449. bool hadRet = false;
  1450. String entryClassName = project->mStartupObject;
  1451. typeDef = mSystem->FindTypeDef(entryClassName, 0, bfModule->mProject);
  1452. if (typeDef != NULL)
  1453. {
  1454. auto type = bfModule->ResolveTypeDef(typeDef);
  1455. BF_ASSERT((type != NULL) || (mPassInstance->HasFailed()));
  1456. if (type != NULL)
  1457. {
  1458. BfType* stringType = vdataContext->mUnreifiedModule->ResolveTypeDef(mStringTypeDef);
  1459. BfType* int32Type = bfModule->GetPrimitiveType(BfTypeCode_Int32);
  1460. BfType* intType = bfModule->GetPrimitiveType(BfTypeCode_IntPtr);
  1461. BfType* voidType = bfModule->GetPrimitiveType(BfTypeCode_None);
  1462. bool hadValidMainMethod = false;
  1463. BfModuleMethodInstance moduleMethodInst;
  1464. for (auto methodDef : typeDef->mMethods)
  1465. {
  1466. if (methodDef->mName == "Main")
  1467. {
  1468. hadValidMainMethod = true;
  1469. moduleMethodInst = bfModule->GetMethodInstanceAtIdx(type->ToTypeInstance(), methodDef->mIdx);
  1470. if (!methodDef->mIsStatic)
  1471. {
  1472. mPassInstance->Fail("Main method must be static", methodDef->GetRefNode());
  1473. hadValidMainMethod = false;
  1474. }
  1475. if ((moduleMethodInst.mMethodInstance->mReturnType != int32Type) &&
  1476. (moduleMethodInst.mMethodInstance->mReturnType != intType) &&
  1477. (moduleMethodInst.mMethodInstance->mReturnType != voidType))
  1478. {
  1479. mPassInstance->Fail("Main method must return void, int, or int32", methodDef->GetRefNode());
  1480. hadValidMainMethod = false;
  1481. }
  1482. if (moduleMethodInst.mMethodInstance->GetParamCount() == 0)
  1483. {
  1484. // No params
  1485. }
  1486. else
  1487. {
  1488. auto paramType = moduleMethodInst.mMethodInstance->GetParamType(0);
  1489. if ((moduleMethodInst.mMethodInstance->GetParamCount() != 1) || (!paramType->IsArray()) || (paramType->GetUnderlyingType() != stringType))
  1490. {
  1491. mPassInstance->Fail("Main method must be declared with either no parameters or a single String[] parameter", methodDef->GetRefNode());
  1492. hadValidMainMethod = false;
  1493. }
  1494. }
  1495. }
  1496. }
  1497. if (moduleMethodInst)
  1498. {
  1499. if (hadValidMainMethod)
  1500. {
  1501. bool hasArgs = moduleMethodInst.mMethodInstance->GetParamCount() != 0;
  1502. BfIRType intType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_IntPtr));
  1503. BfIRType int32Type = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_Int32));
  1504. // Create BeefEntry thunk
  1505. SmallVector<BfIRType, 1> paramTypes;
  1506. if (hasArgs)
  1507. {
  1508. paramTypes.push_back(bfModule->mBfIRBuilder->MapType(moduleMethodInst.mMethodInstance->GetParamType(0)));
  1509. }
  1510. BfIRFunctionType thunkFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
  1511. BfIRFunction thunkMainFunc = bfModule->mBfIRBuilder->CreateFunction(thunkFuncType, BfIRLinkageType_External, "BeefStartProgram");
  1512. bfModule->SetupIRMethod(NULL, thunkMainFunc, false);
  1513. bfModule->mBfIRBuilder->SetActiveFunction(thunkMainFunc);
  1514. auto thunkEntryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1515. bfModule->mBfIRBuilder->SetInsertPoint(thunkEntryBlock);
  1516. SmallVector<BfIRValue, 1> args;
  1517. if (hasArgs)
  1518. args.push_back(bfModule->mBfIRBuilder->GetArgument(0));
  1519. auto methodInstance = moduleMethodInst.mMethodInstance;
  1520. if (methodInstance->mHotMethod != NULL)
  1521. methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
  1522. auto retVal = bfModule->mBfIRBuilder->CreateCall(moduleMethodInst.mFunc, args);
  1523. if (moduleMethodInst.mMethodInstance->mReturnType->IsVoid())
  1524. {
  1525. bfModule->mBfIRBuilder->CreateRet(bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 0));
  1526. }
  1527. else
  1528. {
  1529. retVal = bfModule->mBfIRBuilder->CreateNumericCast(retVal, true, BfTypeCode_Int32);
  1530. bfModule->mBfIRBuilder->CreateRet(retVal);
  1531. }
  1532. hadRet = true;
  1533. auto internalType = bfModule->ResolveTypeDef(mInternalTypeDef);
  1534. args.clear();
  1535. // Call BeefEntry thunk
  1536. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1537. if (hasArgs)
  1538. {
  1539. auto createParamsMethodInstance = bfModule->GetMethodByName(internalType->ToTypeInstance(), "CreateParamsArray");
  1540. auto callValue = bfModule->mBfIRBuilder->CreateCall(createParamsMethodInstance.mFunc, SmallVector<BfIRValue, 0>());
  1541. args.push_back(callValue);
  1542. }
  1543. retValue = bfModule->mBfIRBuilder->CreateCall(thunkMainFunc, args);
  1544. if (hasArgs)
  1545. {
  1546. auto deleteStringArrayMethodInstance = bfModule->GetMethodByName(internalType->ToTypeInstance(), "DeleteStringArray");
  1547. bfModule->mBfIRBuilder->CreateCall(deleteStringArrayMethodInstance.mFunc, args);
  1548. }
  1549. }
  1550. }
  1551. else
  1552. {
  1553. mPassInstance->Fail(StrFormat("Unable to find Main method in class '%s'", entryClassName.c_str()));
  1554. }
  1555. }
  1556. }
  1557. else
  1558. {
  1559. if (entryClassName.empty())
  1560. mPassInstance->Fail(StrFormat("No entry point class specified for executable in project '%s'", project->mName.c_str()));
  1561. else
  1562. mPassInstance->Fail(StrFormat("Unable to find entry point class '%s' in project '%s'", entryClassName.c_str(), project->mName.c_str()));
  1563. bfModule->mHadBuildError = true;
  1564. }
  1565. if (!hadRet)
  1566. retValue = bfModule->GetConstValue32(0);
  1567. }
  1568. else if (project->mTargetType == BfTargetType_BeefDynLib)
  1569. {
  1570. retValue = bfModule->GetConstValue32(1);
  1571. }
  1572. if (project->mTargetType == BfTargetType_BeefTest)
  1573. EmitTestMethod(bfModule, testMethods, retValue);
  1574. BfIRBlock deinitSkipBlock;
  1575. if (project->mTargetType == BfTargetType_BeefDynLib)
  1576. {
  1577. auto deinitBlock = bfModule->mBfIRBuilder->CreateBlock("doDeinit", false);
  1578. deinitSkipBlock = bfModule->mBfIRBuilder->CreateBlock("skipDeinit", false);
  1579. auto cmpResult = bfModule->mBfIRBuilder->CreateCmpEQ(bfModule->mBfIRBuilder->GetArgument(1), bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 0));
  1580. bfModule->mBfIRBuilder->CreateCondBr(cmpResult, deinitBlock, deinitSkipBlock);
  1581. bfModule->mBfIRBuilder->AddBlock(deinitBlock);
  1582. bfModule->mBfIRBuilder->SetInsertPoint(deinitBlock);
  1583. }
  1584. bfModule->mBfIRBuilder->CreateCall(dtorFunc, SizedArray<BfIRValue, 0>());
  1585. BfModuleMethodInstance shutdownMethod = bfModule->GetInternalMethod("Shutdown");
  1586. if (shutdownMethod)
  1587. {
  1588. bfModule->mBfIRBuilder->CreateCall(shutdownMethod.mFunc, SizedArray<BfIRValue, 0>());
  1589. }
  1590. if (deinitSkipBlock)
  1591. {
  1592. bfModule->mBfIRBuilder->CreateBr(deinitSkipBlock);
  1593. bfModule->mBfIRBuilder->AddBlock(deinitSkipBlock);
  1594. bfModule->mBfIRBuilder->SetInsertPoint(deinitSkipBlock);
  1595. }
  1596. if (retValue)
  1597. bfModule->mBfIRBuilder->CreateRet(retValue);
  1598. else
  1599. bfModule->mBfIRBuilder->CreateRetVoid();
  1600. if ((mOptions.mAllowHotSwapping) && (bfModule->mHasFullDebugInfo))
  1601. {
  1602. auto int8Type = bfModule->GetPrimitiveType(BfTypeCode_Int8);
  1603. int dataSize = 16*1024;
  1604. auto irArrType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->MapType(int8Type), dataSize);
  1605. String name = "__BFTLS_EXTRA";
  1606. auto irVal = bfModule->mBfIRBuilder->CreateGlobalVariable(irArrType, false, BfIRLinkageType_External, bfModule->mBfIRBuilder->CreateConstStructZero(irArrType), name, true);
  1607. BfIRMDNode dbgArrayType = bfModule->mBfIRBuilder->DbgCreateArrayType(dataSize * 8, 8, bfModule->mBfIRBuilder->DbgGetType(int8Type), dataSize);
  1608. bfModule->mBfIRBuilder->DbgCreateGlobalVariable(bfModule->mDICompileUnit, name, name, NULL, 0, dbgArrayType, false, irVal);
  1609. }
  1610. }
  1611. // Generate "System.GC.MarkAllStaticMembers"
  1612. auto gcType = vdataContext->mUnreifiedModule->ResolveTypeDef(mGCTypeDef);
  1613. if (bfModule->IsMethodImplementedAndReified(gcType->ToTypeInstance(), "MarkAllStaticMembers"))
  1614. {
  1615. bfModule->PopulateType(gcType);
  1616. auto moduleMethodInstance = bfModule->GetMethodByName(gcType->ToTypeInstance(), "MarkAllStaticMembers");
  1617. bfModule->mBfIRBuilder->SetActiveFunction(moduleMethodInstance.mFunc);
  1618. if (!moduleMethodInstance)
  1619. {
  1620. bfModule->Fail("Internal error: System.GC doesn't contain MarkAllStaticMembers method");
  1621. }
  1622. else
  1623. {
  1624. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1625. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1626. for (auto& mapEntry : sortedStaticMarkMap)
  1627. {
  1628. auto typeInst = mapEntry.second;
  1629. if (typeInst->IsUnspecializedType())
  1630. continue;
  1631. for (auto& methodGroup : typeInst->mMethodInstanceGroups)
  1632. {
  1633. auto methodInstance = methodGroup.mDefault;
  1634. if ((methodInstance != NULL) &&
  1635. (methodInstance->mMethodDef->mIsStatic) &&
  1636. (methodInstance->mMethodDef->mMethodType == BfMethodType_Normal) &&
  1637. (methodInstance->mMethodDef->mName == BF_METHODNAME_MARKMEMBERS_STATIC) &&
  1638. ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
  1639. {
  1640. if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
  1641. continue;
  1642. auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
  1643. if (methodInstance->mHotMethod != NULL)
  1644. methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
  1645. bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
  1646. }
  1647. }
  1648. }
  1649. bfModule->mBfIRBuilder->CreateRetVoid();
  1650. }
  1651. }
  1652. // Generate "System.GC.FindAllTLSMembers"
  1653. if (bfModule->IsMethodImplementedAndReified(gcType->ToTypeInstance(), "FindAllTLSMembers"))
  1654. {
  1655. bfModule->PopulateType(gcType);
  1656. auto moduleMethodInstance = bfModule->GetMethodByName(gcType->ToTypeInstance(), "FindAllTLSMembers");
  1657. bfModule->mBfIRBuilder->SetActiveFunction(moduleMethodInstance.mFunc);
  1658. if (!moduleMethodInstance)
  1659. {
  1660. bfModule->Fail("Internal error: System.GC doesn't contain FindAllTLSMembers method");
  1661. }
  1662. else
  1663. {
  1664. auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
  1665. bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
  1666. for (auto& mapEntry : sortedStaticTLSMap)
  1667. {
  1668. auto typeInst = mapEntry.second;
  1669. if (typeInst->IsUnspecializedType())
  1670. continue;
  1671. for (auto& methodGroup : typeInst->mMethodInstanceGroups)
  1672. {
  1673. auto methodInstance = methodGroup.mDefault;
  1674. if ((methodInstance != NULL) &&
  1675. (methodInstance->mMethodDef->mIsStatic) &&
  1676. (methodInstance->mMethodDef->mMethodType == BfMethodType_Normal) &&
  1677. (methodInstance->mMethodDef->mName == BF_METHODNAME_FIND_TLS_MEMBERS) &&
  1678. ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
  1679. {
  1680. if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
  1681. continue;
  1682. auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
  1683. bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
  1684. }
  1685. }
  1686. }
  1687. bfModule->mBfIRBuilder->CreateRetVoid();
  1688. }
  1689. }
  1690. if (bfModule->mHadBuildError)
  1691. {
  1692. bfModule->mDataHash = 0;
  1693. }
  1694. }
  1695. // This method clears out unused generic types AFTER compilation of reified types has occurred
  1696. void BfCompiler::UpdateDependencyMap(bool deleteUnusued, bool& didWork)
  1697. {
  1698. BP_ZONE("BfCompiler::UpdateDependencyMap");
  1699. BfLogSysM("Compiler::UpdateDependencyMap %d\n", deleteUnusued);
  1700. bool madeFullPass = true;
  1701. if (mCanceling)
  1702. madeFullPass = false;
  1703. if ((mResolvePassData != NULL) && (mResolvePassData->mParser != NULL))
  1704. madeFullPass = false;
  1705. // Remove old data in dependency maps, and find types which don't have any references (direct or indirect)
  1706. // to a non-generic type and remove them
  1707. for (int pass = 0; true; pass++)
  1708. {
  1709. // This assert can fail if we have a dependency error, where deleting a type causes a dependent type
  1710. // to be rebuilt
  1711. BF_ASSERT(pass < 100);
  1712. bool foundNew = false;
  1713. for (auto type : mContext->mResolvedTypes)
  1714. {
  1715. if (type != NULL)
  1716. {
  1717. auto depType = type->ToDependedType();
  1718. auto typeInst = type->ToTypeInstance();
  1719. if (depType != NULL)
  1720. {
  1721. extern BfModule* gLastCreatedModule;
  1722. for (auto itr = depType->mDependencyMap.begin(); itr != depType->mDependencyMap.end(); ++itr)
  1723. {
  1724. auto dependentType = itr->mKey;
  1725. if (dependentType->IsIncomplete())
  1726. {
  1727. BF_ASSERT(dependentType->IsDeleting() || dependentType->IsOnDemand() || !dependentType->HasBeenReferenced() || !madeFullPass || dependentType->IsSpecializedByAutoCompleteMethod());
  1728. }
  1729. }
  1730. // Not combined with previous loop because PopulateType could modify typeInst->mDependencyMap
  1731. for (auto itr = depType->mDependencyMap.begin(); itr != depType->mDependencyMap.end();)
  1732. {
  1733. auto dependentType = itr->mKey;
  1734. auto depTypeInst = dependentType->ToTypeInstance();
  1735. auto& depData = itr->mValue;
  1736. bool isInvalidVersion = (dependentType->mRevision > depData.mRevision) && (deleteUnusued) && (madeFullPass);
  1737. //TODO: Just to cause crash if dependentType is deleted
  1738. bool isIncomplete = dependentType->IsIncomplete();
  1739. if ((isInvalidVersion) && (!dependentType->IsDeleting()))
  1740. {
  1741. if (!dependentType->HasBeenReferenced())
  1742. {
  1743. BfLogSysM("Skipping remove of old dependent %p from %p\n", dependentType, typeInst);
  1744. //BF_ASSERT(dependentType->IsGenericTypeInstance());
  1745. // We have a pending type rebuild but we're not sure whether we're being deleted or not yet...
  1746. ++itr;
  1747. continue;
  1748. }
  1749. }
  1750. if ((dependentType->IsDeleting()) || (isInvalidVersion))
  1751. {
  1752. // If we're deleting the type, OR the dependency of the type has been removed.
  1753. // We detect a removed dependency by the dependent type changing but the dependency revision
  1754. // is older than the dependent type.
  1755. BfLogSysM("Removing old dependent %p from %p\n", dependentType, depType);
  1756. itr = depType->mDependencyMap.erase(itr);
  1757. }
  1758. else
  1759. {
  1760. // There needs to be more usage than just being used as part of the method specialization's MethodGenericArg.
  1761. // Keep in mind that actually invoking a generic method creates a DependencyFlag_LocalUsage dependency. The
  1762. // DependencyFlag_MethodGenericArg is just used by the owner during creation of the method specialization
  1763. bool isDependentUsage =
  1764. (depData.mFlags != BfDependencyMap::DependencyFlag_UnspecializedType) &&
  1765. (depData.mFlags != BfDependencyMap::DependencyFlag_MethodGenericArg);
  1766. // We need to consider specialized generic types separately, to remove unused specializations
  1767. if (typeInst != NULL)
  1768. {
  1769. if ((depTypeInst != NULL) && (typeInst->mLastNonGenericUsedRevision != mRevision) && (isDependentUsage) &&
  1770. ((!dependentType->IsGenericTypeInstance()) || (dependentType->IsUnspecializedType()) || (depTypeInst->mLastNonGenericUsedRevision == mRevision)))
  1771. {
  1772. typeInst->mLastNonGenericUsedRevision = mRevision;
  1773. foundNew = true;
  1774. if (!typeInst->HasBeenReferenced())
  1775. mContext->AddTypeToWorkList(typeInst);
  1776. }
  1777. }
  1778. ++itr;
  1779. }
  1780. }
  1781. if ((!depType->IsGenericTypeInstance() && (!depType->IsBoxed())) ||
  1782. (depType->IsUnspecializedType()) ||
  1783. ((typeInst != NULL) && (typeInst->mLastNonGenericUsedRevision == mRevision)))
  1784. {
  1785. if ((depType->mRebuildFlags & BfTypeRebuildFlag_AwaitingReference) != 0)
  1786. {
  1787. mContext->MarkAsReferenced(depType);
  1788. }
  1789. }
  1790. }
  1791. }
  1792. }
  1793. if (mCanceling)
  1794. madeFullPass = false;
  1795. if (!madeFullPass)
  1796. {
  1797. // We can't delete types based on the dependency map when we're canceling, because we may still
  1798. // have items in the work queues (particularly the mMethodWorkList) that will create
  1799. // new dependencies -- things may unduly be thought to be deleted.
  1800. return;
  1801. }
  1802. if (foundNew)
  1803. {
  1804. // This will work through generic method specializations for the types referenced above, clearing out AwaitingReference flags for
  1805. // newly-referenced generics, and queuing up their method specializations as well
  1806. didWork |= DoWorkLoop(false, false);
  1807. }
  1808. else if (deleteUnusued)
  1809. {
  1810. // Work queues should be empty if we're not canceling
  1811. BF_ASSERT(mContext->mPopulateTypeWorkList.size() == 0);
  1812. BF_ASSERT(mContext->mMethodWorkList.size() == 0);
  1813. // We need to use a delete queue because we trigger a RebuildType for dependent types,
  1814. // but we need to make sure we don't rebuild any types that may be next in line for
  1815. // deletion, so we must set BfTypeRebuildFlag_DeleteQueued first to avoid that
  1816. Array<BfDependedType*> deleteQueue;
  1817. // We bubble out
  1818. for (auto type : mContext->mResolvedTypes)
  1819. {
  1820. auto depType = type->ToDependedType();
  1821. // Delete if we're a generic
  1822. if ((depType != NULL) && (!depType->IsDeleting()))
  1823. {
  1824. auto typeInst = depType->ToTypeInstance();
  1825. bool wantDelete = false;
  1826. if (typeInst != NULL)
  1827. {
  1828. wantDelete = (typeInst->mLastNonGenericUsedRevision != mRevision) &&
  1829. (typeInst->IsGenericTypeInstance() || typeInst->IsBoxed()) && (!typeInst->IsUnspecializedType());
  1830. }
  1831. wantDelete |= (depType->IsOnDemand()) && (depType->mDependencyMap.IsEmpty());
  1832. if (wantDelete)
  1833. {
  1834. deleteQueue.push_back(depType);
  1835. depType->mRebuildFlags = (BfTypeRebuildFlags)(depType->mRebuildFlags | BfTypeRebuildFlag_DeleteQueued);
  1836. foundNew = true;
  1837. }
  1838. }
  1839. }
  1840. for (auto depType : deleteQueue)
  1841. {
  1842. BfLogSysM("Deleting type from deleteQueue in UpdateDependencyMap %p\n", depType);
  1843. mContext->DeleteType(depType, true);
  1844. }
  1845. if (deleteQueue.size() != 0)
  1846. {
  1847. mContext->ValidateDependencies();
  1848. mContext->UpdateAfterDeletingTypes();
  1849. mContext->ValidateDependencies();
  1850. }
  1851. }
  1852. if (!foundNew)
  1853. break;
  1854. }
  1855. #ifdef _DEBUG
  1856. if (deleteUnusued)
  1857. {
  1858. for (auto type : mContext->mResolvedTypes)
  1859. {
  1860. // This flag should be handled by now
  1861. BF_ASSERT((type->mRebuildFlags & BfTypeRebuildFlag_AwaitingReference) == 0);
  1862. }
  1863. }
  1864. #endif
  1865. BP_ZONE("UpdateDependencyMap QueuedSpecializedMethodRebuildTypes");
  1866. HashSet<BfTypeInstance*> specializerSet;
  1867. for (auto rebuildType : mContext->mQueuedSpecializedMethodRebuildTypes)
  1868. {
  1869. if (rebuildType->mRevision != mRevision)
  1870. {
  1871. mContext->RebuildType(rebuildType);
  1872. rebuildType->mRebuildFlags = (BfTypeRebuildFlags)(rebuildType->mRebuildFlags | BfTypeRebuildFlag_SpecializedMethodRebuild);
  1873. for (auto& dep : rebuildType->mDependencyMap)
  1874. {
  1875. auto depType = dep.mKey;
  1876. auto& depData = dep.mValue;
  1877. auto depTypeInst = depType->ToTypeInstance();
  1878. if (depTypeInst == NULL)
  1879. continue;
  1880. if ((depData.mFlags & BfDependencyMap::DependencyFlag_Calls) != 0)
  1881. {
  1882. specializerSet.Add(depTypeInst);
  1883. }
  1884. }
  1885. }
  1886. }
  1887. for (auto depType : specializerSet)
  1888. {
  1889. mContext->QueueMethodSpecializations(depType, true);
  1890. }
  1891. for (auto rebuildType : mContext->mQueuedSpecializedMethodRebuildTypes)
  1892. {
  1893. rebuildType->mRebuildFlags = (BfTypeRebuildFlags)(rebuildType->mRebuildFlags & ~BfTypeRebuildFlag_SpecializedMethodRebuild);
  1894. }
  1895. mContext->mQueuedSpecializedMethodRebuildTypes.Clear();
  1896. }
  1897. // When we are unsure of whether an old generic instance will survive, we RebuildType but don't put it in any worklist.
  1898. // One of three things happens:
  1899. // 1) It gets built on demand
  1900. // 2) It gets deleted in UpdateDependencyMap
  1901. // 3) It stays undefined and we need to build it here
  1902. void BfCompiler::ProcessPurgatory(bool reifiedOnly)
  1903. {
  1904. BP_ZONE("BfCompiler::ProcessPuragory");
  1905. while (true)
  1906. {
  1907. mContext->RemoveInvalidWorkItems();
  1908. //for (auto type : mGenericInstancePurgatory)
  1909. for (int i = 0; i < (int)mGenericInstancePurgatory.size(); i++)
  1910. {
  1911. auto type = mGenericInstancePurgatory[i];
  1912. if ((reifiedOnly) && (!type->IsReified()))
  1913. continue;
  1914. if (!type->IsDeleting())
  1915. {
  1916. auto module = type->GetModule();
  1917. if (module != NULL)
  1918. module->PopulateType(type, BfPopulateType_Full);
  1919. }
  1920. if (reifiedOnly)
  1921. {
  1922. mGenericInstancePurgatory.RemoveAtFast(i);
  1923. i--;
  1924. }
  1925. }
  1926. if (!reifiedOnly)
  1927. mGenericInstancePurgatory.Clear();
  1928. int prevPurgatorySize = (int)mGenericInstancePurgatory.size();
  1929. mContext->ProcessWorkList(reifiedOnly, reifiedOnly);
  1930. if (prevPurgatorySize == (int)mGenericInstancePurgatory.size())
  1931. break;
  1932. }
  1933. }
  1934. bool BfCompiler::VerifySlotNums()
  1935. {
  1936. BP_ZONE("BfCompiler::VerifySlotNums");
  1937. SmallVector<BfTypeInstance*, 16> isSlotUsed;
  1938. for (auto type : mContext->mResolvedTypes)
  1939. {
  1940. if (!type->IsReified())
  1941. continue;
  1942. auto typeInst = type->ToTypeInstance();
  1943. if (typeInst == NULL)
  1944. continue;
  1945. if (typeInst->IsUnspecializedType())
  1946. continue;
  1947. if (typeInst->IsInterface())
  1948. {
  1949. if (typeInst->mSlotNum == -2)
  1950. continue; // Not used
  1951. if ((typeInst->mVirtualMethodTableSize > 0) && (typeInst->mSlotNum == -1))
  1952. {
  1953. // Slot not assigned yet
  1954. return false;
  1955. }
  1956. continue;
  1957. }
  1958. isSlotUsed.clear();
  1959. isSlotUsed.resize(mMaxInterfaceSlots);
  1960. auto checkType = typeInst;
  1961. while (checkType != NULL)
  1962. {
  1963. for (auto iface : checkType->mInterfaces)
  1964. {
  1965. int slotNum = iface.mInterfaceType->mSlotNum;
  1966. if (slotNum >= 0)
  1967. {
  1968. if ((isSlotUsed[slotNum] != NULL) && (isSlotUsed[slotNum] != iface.mInterfaceType))
  1969. return false; // Collision
  1970. isSlotUsed[slotNum] = iface.mInterfaceType;
  1971. }
  1972. }
  1973. checkType = checkType->mBaseType;
  1974. }
  1975. }
  1976. return true;
  1977. }
  1978. bool BfCompiler::QuickGenerateSlotNums()
  1979. {
  1980. /*SmallVector<bool, 16> isSlotUsed;
  1981. for (auto globalTypeEntry : mResolvedTypes)
  1982. {
  1983. BfType* type = globalTypeEntry->mType;
  1984. auto typeInst = type->ToTypeInstance();
  1985. if (typeInst == NULL)
  1986. continue;
  1987. if (typeInst->IsInterface())
  1988. {
  1989. if ((typeInst->mVirtualMethodTableSize > 0) && (typeInst->mSlotNum == -1))
  1990. {
  1991. // Slot not assigned yet
  1992. return false;
  1993. }
  1994. continue;
  1995. }
  1996. }
  1997. return VerifySlotNums();*/
  1998. // Implement later
  1999. return false;
  2000. }
  2001. class BfSlotEntry
  2002. {
  2003. public:
  2004. BfTypeInstance* mTypeInstance;
  2005. int mRefCount;
  2006. Array<BfTypeInstance*> mConcurrentRefs;
  2007. };
  2008. typedef std::pair<BfTypeInstance*, BfTypeInstance*> InterfacePair;
  2009. typedef Dictionary<BfTypeInstance*, BfSlotEntry*> SlotEntryMap;
  2010. static BfSlotEntry* GetSlotEntry(SlotEntryMap& slotEntryMap, BfTypeInstance* typeInst)
  2011. {
  2012. BF_ASSERT(typeInst->IsReified());
  2013. BfSlotEntry** slotEntryPtr = NULL;
  2014. if (!slotEntryMap.TryAdd(typeInst, NULL, &slotEntryPtr))
  2015. return *slotEntryPtr;
  2016. BfSlotEntry* slotEntry = new BfSlotEntry();
  2017. slotEntry->mTypeInstance = typeInst;
  2018. slotEntry->mRefCount = 0;
  2019. //insertPair.first->second = slotEntry;
  2020. *slotEntryPtr = slotEntry;
  2021. return slotEntry;
  2022. }
  2023. static InterfacePair MakeInterfacePair(BfTypeInstance* iface1, BfTypeInstance* iface2)
  2024. {
  2025. if (iface1->mTypeId < iface2->mTypeId)
  2026. return InterfacePair(iface1, iface2);
  2027. return InterfacePair(iface2, iface1);
  2028. }
  2029. struct InterfacePairHash
  2030. {
  2031. size_t operator()(const InterfacePair& val) const
  2032. {
  2033. return (((size_t)val.first) >> 2) ^ ((size_t)val.second);
  2034. }
  2035. };
  2036. bool BfCompiler::SlowGenerateSlotNums()
  2037. {
  2038. BP_ZONE("BfCompiler::SlowGenerateSlotNums");
  2039. SlotEntryMap ifaceUseMap;
  2040. std::unordered_set<InterfacePair, InterfacePairHash> concurrentInterfaceSet;
  2041. HashSet<BfTypeInstance*> foundIFaces;
  2042. if (mMaxInterfaceSlots < 0)
  2043. {
  2044. mMaxInterfaceSlots = 0;
  2045. }
  2046. bool isHotCompile = IsHotCompile();
  2047. for (auto type : mContext->mResolvedTypes)
  2048. {
  2049. if (!type->IsReified())
  2050. continue;
  2051. auto typeInst = type->ToTypeInstance();
  2052. if (typeInst == NULL)
  2053. continue;
  2054. if (typeInst->IsUnspecializedType())
  2055. continue;
  2056. if (typeInst->IsInterface())
  2057. {
  2058. if (typeInst->mSlotNum == -2) // Not needed
  2059. continue;
  2060. if (!isHotCompile) // Hot compiles cannot remap slot numbers
  2061. typeInst->mSlotNum = -1;
  2062. if (typeInst->mVirtualMethodTableSize > 0)
  2063. {
  2064. GetSlotEntry(ifaceUseMap, typeInst);
  2065. }
  2066. continue;
  2067. }
  2068. foundIFaces.Clear();
  2069. auto checkTypeInst = typeInst;
  2070. while (checkTypeInst != NULL)
  2071. {
  2072. for (auto iface : checkTypeInst->mInterfaces)
  2073. {
  2074. auto interfaceType = iface.mInterfaceType;
  2075. if (interfaceType->mSlotNum == -2)
  2076. continue; // Not needed
  2077. if ((isHotCompile) && (interfaceType->mSlotNum == -1))
  2078. checkTypeInst->mDirty = true; // We're about to slot an interface here
  2079. if (interfaceType->mVirtualMethodTableSize > 0)
  2080. {
  2081. BfSlotEntry* slotEntry = GetSlotEntry(ifaceUseMap, interfaceType);
  2082. slotEntry->mRefCount++;
  2083. foundIFaces.Add(iface.mInterfaceType);
  2084. }
  2085. }
  2086. checkTypeInst = checkTypeInst->mBaseType;
  2087. }
  2088. for (auto itr1 = foundIFaces.begin(); itr1 != foundIFaces.end(); ++itr1)
  2089. {
  2090. auto itr2 = itr1;
  2091. ++itr2;
  2092. for ( ; itr2 != foundIFaces.end(); ++itr2)
  2093. {
  2094. auto iface1 = *itr1;
  2095. auto iface2 = *itr2;
  2096. InterfacePair ifacePair = MakeInterfacePair(iface1, iface2);
  2097. if (concurrentInterfaceSet.insert(ifacePair).second)
  2098. {
  2099. BfSlotEntry* entry1 = GetSlotEntry(ifaceUseMap, iface1);
  2100. BfSlotEntry* entry2 = GetSlotEntry(ifaceUseMap, iface2);
  2101. entry1->mConcurrentRefs.push_back(iface2);
  2102. entry2->mConcurrentRefs.push_back(iface1);
  2103. }
  2104. }
  2105. }
  2106. }
  2107. Array<BfSlotEntry*> sortedIfaceUseMap;
  2108. for (auto& entry : ifaceUseMap)
  2109. {
  2110. if (!isHotCompile)
  2111. BF_ASSERT(entry.mValue->mTypeInstance->mSlotNum == -1);
  2112. sortedIfaceUseMap.push_back(entry.mValue);
  2113. }
  2114. std::sort(sortedIfaceUseMap.begin(), sortedIfaceUseMap.end(), [] (BfSlotEntry* lhs, BfSlotEntry* rhs)
  2115. {
  2116. if (lhs->mRefCount != rhs->mRefCount)
  2117. return lhs->mRefCount > rhs->mRefCount;
  2118. return lhs->mTypeInstance->mTypeId < rhs->mTypeInstance->mTypeId;
  2119. });
  2120. bool failed = false;
  2121. SmallVector<bool, 16> isSlotUsed;
  2122. for (auto slotEntry : sortedIfaceUseMap)
  2123. {
  2124. BfTypeInstance* iface = slotEntry->mTypeInstance;
  2125. if (iface->mSlotNum >= 0)
  2126. {
  2127. BF_ASSERT(isHotCompile);
  2128. continue;
  2129. }
  2130. isSlotUsed.clear();
  2131. if (mMaxInterfaceSlots > 0)
  2132. isSlotUsed.resize(mMaxInterfaceSlots);
  2133. BF_ASSERT(iface->mSlotNum == -1);
  2134. BF_ASSERT(iface->IsInterface());
  2135. for (auto iface2 : slotEntry->mConcurrentRefs)
  2136. {
  2137. int slotNum2 = iface2->mSlotNum;
  2138. if (slotNum2 != -1)
  2139. isSlotUsed[slotNum2] = true;
  2140. }
  2141. for (int checkSlot = 0; checkSlot < mMaxInterfaceSlots; checkSlot++)
  2142. {
  2143. if (!isSlotUsed[checkSlot])
  2144. {
  2145. iface->mSlotNum = checkSlot;
  2146. break;
  2147. }
  2148. }
  2149. if (iface->mSlotNum == -1)
  2150. {
  2151. if (isHotCompile)
  2152. {
  2153. failed = true;
  2154. mPassInstance->Fail("Interface slot numbering overflow. Restart the program or revert changes.");
  2155. break;
  2156. }
  2157. iface->mSlotNum = mMaxInterfaceSlots;
  2158. if (mOptions.mIncrementalBuild)
  2159. {
  2160. // Allocate more than enough interface slots
  2161. mMaxInterfaceSlots += 3;
  2162. }
  2163. else
  2164. mMaxInterfaceSlots++;
  2165. // failed = true;
  2166. // mPassInstance->Fail(StrFormat("Interface slot numbering overflow, increase the maximum slot number from '%d'", mMaxInterfaceSlots));
  2167. // break;
  2168. }
  2169. // if (iface->mSlotNum == -1)
  2170. // {
  2171. // failed = true;
  2172. // mPassInstance->Fail(StrFormat("Interface slot numbering overflow, increase the maximum slot number from '%d'", mMaxInterfaceSlots));
  2173. // break;
  2174. // }
  2175. if (isHotCompile)
  2176. {
  2177. mHotState->mNewlySlottedTypeIds.Add(iface->mTypeId);
  2178. mHotState->mSlotDefineTypeIds.Add(iface->mTypeId);
  2179. }
  2180. }
  2181. if (!failed)
  2182. {
  2183. bool success = VerifySlotNums();
  2184. if (!success)
  2185. {
  2186. BF_DBG_FATAL("Failed!");
  2187. }
  2188. }
  2189. for (auto& entry : ifaceUseMap)
  2190. delete entry.mValue;
  2191. return true;
  2192. }
  2193. void BfCompiler::GenerateSlotNums()
  2194. {
  2195. BP_ZONE("BfCompiler::GenerateSlotNums");
  2196. if (mMaxInterfaceSlots < 0)
  2197. {
  2198. if (mOptions.mIncrementalBuild)
  2199. mMaxInterfaceSlots = 3;
  2200. else
  2201. mMaxInterfaceSlots = 0;
  2202. }
  2203. bool isHotCompile = IsHotCompile();
  2204. for (auto type : mContext->mResolvedTypes)
  2205. {
  2206. if (!type->IsInterface())
  2207. continue;
  2208. auto typeInstance = type->ToTypeInstance();
  2209. if ((typeInstance->mSlotNum <= 0) || (!isHotCompile))
  2210. {
  2211. if (mContext->mReferencedIFaceSlots.Contains(typeInstance))
  2212. {
  2213. if (typeInstance->mSlotNum == -2)
  2214. typeInstance->mSlotNum = -1;
  2215. }
  2216. else
  2217. typeInstance->mSlotNum = -2; // Not needed
  2218. }
  2219. }
  2220. if (VerifySlotNums())
  2221. return;
  2222. if (!QuickGenerateSlotNums())
  2223. SlowGenerateSlotNums();
  2224. BfLogSysM("GenerateSlotNums mMaxInterfaceSlots: %d\n", mMaxInterfaceSlots);
  2225. }
  2226. void BfCompiler::GenerateDynCastData()
  2227. {
  2228. BP_ZONE("BfCompiler::GenerateDynCastData");
  2229. Array<int> firstDerivedIds;
  2230. Array<int> nextSiblingIds;
  2231. firstDerivedIds.Resize(mCurTypeId);
  2232. nextSiblingIds.Resize(mCurTypeId);
  2233. for (auto type : mContext->mResolvedTypes)
  2234. {
  2235. if (type->IsBoxed())
  2236. continue;
  2237. auto typeInst = type->ToTypeInstance();
  2238. if (typeInst == NULL)
  2239. continue;
  2240. if (typeInst->mBaseType == NULL)
  2241. continue;
  2242. int baseId = typeInst->mBaseType->mTypeId;
  2243. int firstDerivedId = firstDerivedIds[baseId];
  2244. nextSiblingIds[typeInst->mTypeId] = firstDerivedIds[baseId];
  2245. firstDerivedIds[baseId] = typeInst->mTypeId;
  2246. }
  2247. int curInheritanceId = 1;
  2248. std::function<void(BfTypeInstance*)> _AddTypeInfo = [&](BfTypeInstance* typeInst)
  2249. {
  2250. if (typeInst->mInheritanceId != curInheritanceId)
  2251. {
  2252. typeInst->mInheritanceId = curInheritanceId;
  2253. typeInst->mDirty = true;
  2254. }
  2255. curInheritanceId++;
  2256. int childId = firstDerivedIds[typeInst->mTypeId];
  2257. while (childId != 0)
  2258. {
  2259. auto childType = mContext->mTypes[childId]->ToTypeInstance();
  2260. _AddTypeInfo(childType);
  2261. childId = nextSiblingIds[childId];
  2262. }
  2263. int inheritanceCount = curInheritanceId - typeInst->mInheritanceId - 1;
  2264. if (typeInst->mInheritanceCount != inheritanceCount)
  2265. {
  2266. typeInst->mInheritanceCount = inheritanceCount;
  2267. typeInst->mDirty = true;
  2268. }
  2269. };
  2270. _AddTypeInfo(mContext->mBfObjectType);
  2271. auto valueTypeInst = mContext->mScratchModule->ResolveTypeDef(mValueTypeTypeDef)->ToTypeInstance();
  2272. _AddTypeInfo(valueTypeInst);
  2273. }
  2274. void BfCompiler::UpdateRevisedTypes()
  2275. {
  2276. BfLogSysM("UpdateRevisedTypes\n");
  2277. BP_ZONE("BfCompiler::UpdateRevisedTypes");
  2278. // See if we have any name conflicts and remove those
  2279. auto typeDefItr = mSystem->mTypeDefs.begin();
  2280. while (typeDefItr != mSystem->mTypeDefs.end())
  2281. {
  2282. auto typeDef = *typeDefItr;
  2283. auto origTypeDef = typeDef;
  2284. if (typeDef->mNextRevision != NULL)
  2285. typeDef = typeDef->mNextRevision;
  2286. if (typeDef->mDupDetectedRevision == mRevision)
  2287. {
  2288. ++typeDefItr;
  2289. continue;
  2290. }
  2291. typeDef->mDupDetectedRevision = -1;
  2292. if ((typeDef->mIsCombinedPartial) || (typeDef->mDefState == BfTypeDef::DefState_Deleted) || (typeDef->mTypeCode == BfTypeCode_Extension))
  2293. {
  2294. ++typeDefItr;
  2295. continue;
  2296. }
  2297. bool removedElement = false;
  2298. auto nextTypeDefItr = typeDefItr;
  2299. nextTypeDefItr.MoveToNextHashMatch();
  2300. while (nextTypeDefItr)
  2301. {
  2302. auto nextTypeDef = *nextTypeDefItr;
  2303. if (nextTypeDef->mNextRevision != NULL)
  2304. nextTypeDef = nextTypeDef->mNextRevision;
  2305. if ((nextTypeDef->mIsCombinedPartial) || (nextTypeDef->mDefState == BfTypeDef::DefState_Deleted) || (nextTypeDef->mTypeCode == BfTypeCode_Extension) ||
  2306. (typeDef->mFullName != nextTypeDef->mFullName) || (typeDef->mGenericParamDefs.size() != nextTypeDef->mGenericParamDefs.size()))
  2307. {
  2308. nextTypeDefItr.MoveToNextHashMatch();
  2309. continue;
  2310. }
  2311. if ((typeDef->mIsPartial) && (nextTypeDef->mIsPartial) &&
  2312. (!typeDef->IsGlobalsContainer()) &&
  2313. (typeDef->mProject != nextTypeDef->mProject))
  2314. {
  2315. BfTypeDef* typeA = NULL;
  2316. BfTypeDef* typeB = NULL;
  2317. BfError* error = NULL;
  2318. if (typeDef->mProject->ReferencesOrReferencedBy(nextTypeDef->mProject))
  2319. {
  2320. typeA = typeDef;
  2321. typeB = nextTypeDef;
  2322. }
  2323. else if (nextTypeDef->mProject->ReferencesOrReferencedBy(typeDef->mProject))
  2324. {
  2325. typeA = nextTypeDef;
  2326. typeB = typeDef;
  2327. }
  2328. if (typeA != NULL)
  2329. {
  2330. error = mPassInstance->Fail(StrFormat("Partial type in project '%s' cannot extend a type from a referenced project", typeA->mProject->mName.c_str()).c_str(),
  2331. typeA->mTypeDeclaration->mNameNode);
  2332. mPassInstance->MoreInfo(StrFormat("Previous definition in project '%s'", typeB->mProject->mName.c_str()),
  2333. typeB->mTypeDeclaration->mNameNode);
  2334. }
  2335. if (error != NULL)
  2336. error->mIsPersistent = true;
  2337. }
  2338. if (((!typeDef->mIsPartial) || (!nextTypeDef->mIsPartial)) &&
  2339. (!typeDef->IsGlobalsContainer()) && (!nextTypeDef->IsGlobalsContainer()) &&
  2340. (typeDef->mProject->ReferencesOrReferencedBy(nextTypeDef->mProject)))
  2341. {
  2342. nextTypeDef->mDupDetectedRevision = mRevision;
  2343. BfError* error = NULL;
  2344. /*if ((typeDef->mIsPartial) && (typeDef->mTypeCode != BfTypeCode_Extension))
  2345. {
  2346. error = mPassInstance->Fail("Missing 'partial' modifier; another partial definition of this type exists", nextTypeDef->mTypeDeclaration->mNameNode);
  2347. mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
  2348. }
  2349. else if ((nextTypeDef->mIsPartial) && (nextTypeDef->mTypeCode != BfTypeCode_Extension))
  2350. {
  2351. error = mPassInstance->Fail("Missing 'partial' modifier; another partial definition of this type exists", typeDef->mTypeDeclaration->mNameNode);
  2352. mPassInstance->MoreInfo("Previous definition", nextTypeDef->mTypeDeclaration->mNameNode);
  2353. }
  2354. else */if (nextTypeDef->mOuterType != NULL)
  2355. {
  2356. 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,
  2357. nextTypeDef->mName->mString.mPtr), nextTypeDef->mTypeDeclaration->mNameNode);
  2358. mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
  2359. }
  2360. else if (!nextTypeDef->mNamespace.IsEmpty())
  2361. {
  2362. error = mPassInstance->Fail(StrFormat("The namespace '%s' already has a definition for '%s'", nextTypeDef->mNamespace.ToString().c_str(),
  2363. nextTypeDef->mName->mString.mPtr), nextTypeDef->mTypeDeclaration->mNameNode);
  2364. mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
  2365. }
  2366. else
  2367. {
  2368. error = mPassInstance->Fail(StrFormat("The global namespace already has a definition for '%s'",
  2369. nextTypeDef->mName->mString.mPtr), nextTypeDef->mTypeDeclaration->mNameNode);
  2370. mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
  2371. }
  2372. if (error != NULL)
  2373. error->mIsPersistent = true;
  2374. }
  2375. nextTypeDefItr.MoveToNextHashMatch();
  2376. }
  2377. ++typeDefItr;
  2378. }
  2379. mContext->PreUpdateRevisedTypes();
  2380. // If we missed out on required types previously, now we should be 'okay'
  2381. mInInvalidState = false;
  2382. // We can't do any yields in here - the compiler state is invalid from the time we inject a new
  2383. // typedef revision up until we finish the associated RebuildType
  2384. int compositeBucket = 0;
  2385. // These are "extension" defs that were unmatched last run through
  2386. Array<BfTypeDef*> prevSoloExtensions;
  2387. mSystem->mTypeDefs.CheckRehash();
  2388. // Process the typedefs one bucket at a time. When we are combining extensions or partials (globals) into a single definition then
  2389. // we will be making multiple passes over the bucket that contains that name
  2390. for (int bucketIdx = 0; bucketIdx < mSystem->mTypeDefs.mHashSize; bucketIdx++)
  2391. {
  2392. bool hadPartials = false;
  2393. bool hadChanges = false;
  2394. if (mSystem->mTypeDefs.mHashHeads == NULL)
  2395. break;
  2396. // Partials combiner
  2397. auto outerTypeDefEntry = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  2398. while (outerTypeDefEntry != NULL)
  2399. {
  2400. auto outerTypeDef = outerTypeDefEntry->mValue;
  2401. if (outerTypeDef->mDefState == BfTypeDef::DefState_Deleted)
  2402. {
  2403. hadChanges = true;
  2404. outerTypeDefEntry = outerTypeDefEntry->mNext;
  2405. continue;
  2406. }
  2407. if (outerTypeDef->mNextRevision != NULL)
  2408. hadChanges = true;
  2409. BfTypeDefMap::Entry* rootTypeDefEntry = NULL;
  2410. BfTypeDef* rootTypeDef = NULL;
  2411. BfTypeDef* compositeTypeDef = NULL;
  2412. auto latestOuterTypeDef = outerTypeDef->GetLatest();
  2413. if ((outerTypeDef->mTypeCode == BfTypeCode_Extension) && (!outerTypeDef->mIsPartial))
  2414. {
  2415. prevSoloExtensions.Add(outerTypeDef);
  2416. outerTypeDef->mIsPartial = true;
  2417. }
  2418. if ((outerTypeDef->mIsPartial) || (outerTypeDef->mIsCombinedPartial))
  2419. {
  2420. // Initialize mPartialUsed flags
  2421. if (!hadPartials)
  2422. {
  2423. auto checkTypeDefEntry = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  2424. while (checkTypeDefEntry != NULL)
  2425. {
  2426. auto checkTypeDef = checkTypeDefEntry->mValue;
  2427. if ((checkTypeDefEntry->mHash == outerTypeDefEntry->mHash) &&
  2428. (checkTypeDef->NameEquals(outerTypeDef)))
  2429. {
  2430. checkTypeDef->mPartialUsed = false;
  2431. }
  2432. checkTypeDefEntry = checkTypeDefEntry->mNext;
  2433. }
  2434. hadPartials = true;
  2435. }
  2436. }
  2437. if ((outerTypeDef->mTypeCode == BfTypeCode_Extension) && (!outerTypeDef->mPartialUsed))
  2438. {
  2439. // Find root type, and we assume the composite type follows this
  2440. auto checkTypeDefEntry = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  2441. while (checkTypeDefEntry != NULL)
  2442. {
  2443. auto checkTypeDef = checkTypeDefEntry->mValue;
  2444. if ((checkTypeDefEntry->mHash != outerTypeDefEntry->mHash) ||
  2445. (checkTypeDef->mIsCombinedPartial) ||
  2446. (checkTypeDef->mTypeCode == BfTypeCode_Extension) ||
  2447. (checkTypeDef->mDefState == BfTypeDef::DefState_Deleted) ||
  2448. (checkTypeDef->mPartialUsed) ||
  2449. (!checkTypeDef->NameEquals(outerTypeDef)) ||
  2450. (checkTypeDef->mGenericParamDefs.size() != outerTypeDef->mGenericParamDefs.size()) ||
  2451. (!outerTypeDef->mProject->ContainsReference(checkTypeDef->mProject)))
  2452. {
  2453. checkTypeDefEntry = checkTypeDefEntry->mNext;
  2454. continue;
  2455. }
  2456. rootTypeDef = checkTypeDef;
  2457. rootTypeDefEntry = checkTypeDefEntry;
  2458. checkTypeDefEntry = checkTypeDefEntry->mNext;
  2459. }
  2460. }
  2461. else if ((outerTypeDef->mIsExplicitPartial) && (!outerTypeDef->mPartialUsed))
  2462. {
  2463. // For explicit partials there is no 'root type' so we just use the first explicit partial
  2464. rootTypeDef = outerTypeDef;
  2465. rootTypeDefEntry = outerTypeDefEntry;
  2466. // Find composite type, there is no explicit position for this
  2467. auto checkTypeDefEntry = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  2468. while (checkTypeDefEntry != NULL)
  2469. {
  2470. auto checkTypeDef = checkTypeDefEntry->mValue;
  2471. if ((checkTypeDefEntry->mHash != outerTypeDefEntry->mHash) ||
  2472. (!checkTypeDef->mIsCombinedPartial) ||
  2473. (checkTypeDef->mPartialUsed) ||
  2474. (checkTypeDef->mDefState == BfTypeDef::DefState_Deleted) ||
  2475. (!checkTypeDef->NameEquals(outerTypeDef)) ||
  2476. (checkTypeDef->mGenericParamDefs.size() != outerTypeDef->mGenericParamDefs.size()) ||
  2477. (outerTypeDef->mProject != checkTypeDef->mProject))
  2478. {
  2479. checkTypeDefEntry = checkTypeDefEntry->mNext;
  2480. continue;
  2481. }
  2482. compositeTypeDef = checkTypeDef;
  2483. if (compositeTypeDef->mNextRevision != NULL)
  2484. {
  2485. // This is an old 'next revision'
  2486. delete compositeTypeDef->mNextRevision;
  2487. compositeTypeDef->mNextRevision = NULL;
  2488. }
  2489. checkTypeDefEntry = checkTypeDefEntry->mNext;
  2490. }
  2491. }
  2492. // Now find extensions to apply to the rootTypeDef
  2493. if (rootTypeDef != NULL)
  2494. {
  2495. bool partialsHadChanges = false;
  2496. bool hadSignatureChange = false;
  2497. bool compositeIsNew = false;
  2498. if (compositeTypeDef == NULL)
  2499. {
  2500. if ((rootTypeDef->mIsExplicitPartial) || (rootTypeDefEntry->mNext == NULL) || (!rootTypeDefEntry->mNext->mValue->mIsCombinedPartial))
  2501. {
  2502. compositeTypeDef = new BfTypeDef();
  2503. compositeTypeDef->mSystem = rootTypeDef->mSystem;
  2504. compositeTypeDef->mProject = rootTypeDef->mProject;
  2505. compositeTypeDef->mName = rootTypeDef->mName;
  2506. compositeTypeDef->mName->mRefCount++;
  2507. mSystem->TrackName(compositeTypeDef);
  2508. compositeTypeDef->mNameEx = rootTypeDef->mNameEx;
  2509. compositeTypeDef->mNameEx->Ref();
  2510. compositeTypeDef->mProtection = rootTypeDef->mProtection;
  2511. compositeTypeDef->mNamespace = rootTypeDef->mNamespace;
  2512. compositeTypeDef->mTypeCode = BfTypeCode_Extension;
  2513. compositeTypeDef->mFullName = rootTypeDef->mFullName;
  2514. compositeTypeDef->mFullNameEx = rootTypeDef->mFullNameEx;
  2515. compositeTypeDef->mIsCombinedPartial = true;
  2516. for (auto prevGenericParam : rootTypeDef->mGenericParamDefs)
  2517. {
  2518. BfGenericParamDef* copiedGenericParam = new BfGenericParamDef();
  2519. *copiedGenericParam = *prevGenericParam;
  2520. compositeTypeDef->mGenericParamDefs.Add(copiedGenericParam);
  2521. }
  2522. mSystem->mTypeDefs.AddAfter(compositeTypeDef, rootTypeDefEntry);
  2523. partialsHadChanges = true;
  2524. hadSignatureChange = true;
  2525. compositeIsNew = true;
  2526. BfLogSysM("Creating compositeTypeDef %p\n", compositeTypeDef);
  2527. }
  2528. else
  2529. {
  2530. BF_ASSERT(rootTypeDefEntry->mNext->mValue->NameEquals(rootTypeDef));
  2531. compositeTypeDef = rootTypeDefEntry->mNext->mValue;
  2532. if (compositeTypeDef->mNextRevision != NULL)
  2533. {
  2534. // This is an old 'next revision'
  2535. delete compositeTypeDef->mNextRevision;
  2536. compositeTypeDef->mNextRevision = NULL;
  2537. }
  2538. }
  2539. }
  2540. // Collect the partials
  2541. BfSizedVector<BfTypeDef*, 8> typeParts;
  2542. typeParts.push_back(rootTypeDef);
  2543. auto checkTypeDefEntry = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  2544. while (checkTypeDefEntry != NULL)
  2545. {
  2546. auto checkTypeDef = checkTypeDefEntry->mValue;
  2547. bool isValidProject;
  2548. if (rootTypeDef->mIsExplicitPartial)
  2549. isValidProject = rootTypeDef->mProject == checkTypeDef->mProject;
  2550. else
  2551. isValidProject = checkTypeDef->mProject->ContainsReference(rootTypeDef->mProject);
  2552. if (checkTypeDef != rootTypeDef)
  2553. {
  2554. if ((checkTypeDef->mIsCombinedPartial) ||
  2555. (!checkTypeDef->mIsPartial) ||
  2556. (checkTypeDef->mPartialUsed) ||
  2557. (!checkTypeDef->NameEquals(rootTypeDef)) ||
  2558. (checkTypeDef->mGenericParamDefs.size() != rootTypeDef->mGenericParamDefs.size()) ||
  2559. (!isValidProject))
  2560. {
  2561. checkTypeDefEntry = checkTypeDefEntry->mNext;
  2562. continue;
  2563. }
  2564. }
  2565. compositeTypeDef->mPartialUsed = true;
  2566. checkTypeDef->mPartialUsed = true;
  2567. if (checkTypeDef->mDefState == BfTypeDef::DefState_Deleted)
  2568. {
  2569. partialsHadChanges = true;
  2570. hadSignatureChange = true;
  2571. }
  2572. else
  2573. {
  2574. if (checkTypeDef != rootTypeDef)
  2575. typeParts.push_back(checkTypeDef);
  2576. if (checkTypeDef->mNextRevision != NULL)
  2577. {
  2578. partialsHadChanges = true;
  2579. BF_ASSERT(checkTypeDef->mNextRevision->mGenericParamDefs.size() == rootTypeDef->mGenericParamDefs.size());
  2580. //mSystem->InjectNewRevision(checkTypeDef);
  2581. //BF_ASSERT(checkTypeDef->mGenericParamDefs.size() == rootTypeDef->mGenericParamDefs.size());
  2582. }
  2583. else if (checkTypeDef->mDefState == BfTypeDef::DefState_New)
  2584. partialsHadChanges = true;
  2585. }
  2586. checkTypeDefEntry = checkTypeDefEntry->mNext;
  2587. }
  2588. // Set this down here, because the InjectNewRevision will clear this flag
  2589. rootTypeDef->mIsPartial = true;
  2590. if (partialsHadChanges)
  2591. {
  2592. BF_ASSERT(compositeTypeDef->mNextRevision == NULL);
  2593. mSystem->VerifyTypeDef(compositeTypeDef);
  2594. for (auto checkTypeDef : typeParts)
  2595. {
  2596. mSystem->VerifyTypeDef(checkTypeDef);
  2597. // Apply any def state that is more conservative
  2598. if (checkTypeDef->mDefState == BfTypeDef::DefState_Signature_Changed)
  2599. compositeTypeDef->mDefState = BfTypeDef::DefState_Signature_Changed;
  2600. else if (checkTypeDef->mDefState == BfTypeDef::DefState_InlinedInternals_Changed)
  2601. {
  2602. if (compositeTypeDef->mDefState != BfTypeDef::DefState_Signature_Changed)
  2603. compositeTypeDef->mDefState = BfTypeDef::DefState_InlinedInternals_Changed;
  2604. }
  2605. else if (checkTypeDef->mDefState == BfTypeDef::DefState_Internals_Changed)
  2606. {
  2607. if ((compositeTypeDef->mDefState != BfTypeDef::DefState_Signature_Changed) &&
  2608. (compositeTypeDef->mDefState != BfTypeDef::DefState_InlinedInternals_Changed))
  2609. compositeTypeDef->mDefState = BfTypeDef::DefState_Internals_Changed;
  2610. }
  2611. BF_ASSERT(checkTypeDef->mIsPartial);
  2612. if (checkTypeDef->mNextRevision != NULL)
  2613. {
  2614. mSystem->VerifyTypeDef(checkTypeDef->mNextRevision);
  2615. mSystem->InjectNewRevision(checkTypeDef);
  2616. }
  2617. checkTypeDef->mIsPartial = true;
  2618. checkTypeDef->mDefState = BfTypeDef::DefState_Defined;
  2619. mSystem->AddToCompositePartial(mPassInstance, compositeTypeDef, checkTypeDef);
  2620. }
  2621. mSystem->FinishCompositePartial(compositeTypeDef);
  2622. if (!compositeIsNew)
  2623. {
  2624. if (compositeTypeDef->mNextRevision != NULL)
  2625. {
  2626. BF_ASSERT(compositeTypeDef->mPartials.size() != 0);
  2627. }
  2628. }
  2629. // We use the root typedef's namespace search for the composite, but this should only be
  2630. // used for cases where we CANNOT specify a typeref on an extension. IE: custom attributes
  2631. // for a type can only be added on the root typedef. If this changes then we need to make
  2632. // sure that we attach a definingType to attributes
  2633. for (auto name : compositeTypeDef->mNamespaceSearch)
  2634. mSystem->ReleaseAtomComposite(name);
  2635. compositeTypeDef->mNamespaceSearch = rootTypeDef->mNamespaceSearch;
  2636. for (auto name : compositeTypeDef->mNamespaceSearch)
  2637. mSystem->RefAtomComposite(name);
  2638. if (rootTypeDef != NULL)
  2639. compositeTypeDef->mNamespaceSearch = rootTypeDef->mNamespaceSearch;
  2640. else
  2641. compositeTypeDef->mNamespaceSearch.Clear();
  2642. //BfLogSysM("Composite type %p updating. isNew: %d\n", compositeTypeDef, compositeIsNew);
  2643. if (compositeIsNew)
  2644. {
  2645. compositeTypeDef->mDefState = BfTypeDef::DefState_New;
  2646. mSystem->InjectNewRevision(compositeTypeDef);
  2647. // Reset 'New' state
  2648. compositeTypeDef->mDefState = BfTypeDef::DefState_New;
  2649. }
  2650. else if (hadSignatureChange)
  2651. compositeTypeDef->mDefState = BfTypeDef::DefState_Signature_Changed;
  2652. if (compositeTypeDef->mDefState == BfTypeDef::DefState_Defined)
  2653. {
  2654. // No changes, just inject
  2655. mSystem->InjectNewRevision(compositeTypeDef);
  2656. }
  2657. /*if (compositeTypeDef->mTypeCode == BfTypeCode_Extension)
  2658. {
  2659. BF_ASSERT(rootTypeDef == NULL);
  2660. compositeTypeDef->mTypeCode = BfTypeCode_Object;
  2661. }*/
  2662. auto latestCompositeTypeDef = compositeTypeDef->GetLatest();
  2663. if (latestCompositeTypeDef->mTypeCode == BfTypeCode_Extension)
  2664. {
  2665. BF_ASSERT(rootTypeDef == NULL);
  2666. latestCompositeTypeDef->mTypeCode = BfTypeCode_Object;
  2667. }
  2668. BfLogSysM("Partial combined type typedef %p updated from parser %p\n", compositeTypeDef, latestCompositeTypeDef->mTypeDeclaration->GetSourceData());
  2669. }
  2670. }
  2671. outerTypeDefEntry = outerTypeDefEntry->mNext;
  2672. }
  2673. // Handle unused partials, apply any new revisions, process pending deletes
  2674. if ((hadPartials) || (hadChanges))
  2675. {
  2676. BfTypeDef* checkMasterTypeDef = NULL;
  2677. BfTypeDef* deletedCombinedPartial = NULL;
  2678. outerTypeDefEntry = mSystem->mTypeDefs.mHashHeads[bucketIdx];
  2679. while (outerTypeDefEntry != NULL)
  2680. {
  2681. auto outerTypeDef = outerTypeDefEntry->mValue;
  2682. auto nextTypeDefEntry = outerTypeDefEntry->mNext;
  2683. if ((outerTypeDef->mIsPartial) && (!outerTypeDef->mIsExplicitPartial) && (outerTypeDef->mTypeCode != BfTypeCode_Extension) &&
  2684. (nextTypeDefEntry != NULL) && (!nextTypeDefEntry->mValue->mPartialUsed))
  2685. {
  2686. // This is a root type that we've removed all extensions from, so now we go back to treating it as the actual definition
  2687. // instead of using the composite that immediately follows it
  2688. BF_ASSERT(outerTypeDef->mTypeCode != BfTypeCode_Extension);
  2689. outerTypeDef->mIsPartial = false;
  2690. outerTypeDef->mPartialIdx = -1;
  2691. }
  2692. if (outerTypeDef->mDefState == BfTypeDef::DefState_Deleted)
  2693. {
  2694. BfLogSysM("UpdateRevisedTypes deleting outerTypeDef %p\n", outerTypeDef);
  2695. outerTypeDef->mDefState = BfTypeDef::DefState_Deleted;
  2696. mSystem->RemoveTypeDef(outerTypeDef);
  2697. }
  2698. else if (!outerTypeDef->mPartialUsed)
  2699. {
  2700. if (outerTypeDef->mIsCombinedPartial)
  2701. {
  2702. BfLogSysM("UpdateRevisedTypes deleting combinedPartial type %p\n", outerTypeDef);
  2703. deletedCombinedPartial = outerTypeDef;
  2704. outerTypeDef->mDefState = BfTypeDef::DefState_Deleted;
  2705. mSystem->RemoveTypeDef(outerTypeDef);
  2706. }
  2707. else if (outerTypeDef->mTypeCode == BfTypeCode_Extension)
  2708. {
  2709. auto error = mPassInstance->Fail(StrFormat("Unable to find root type definition for extension '%s'", outerTypeDef->GetLatest()->mFullName.ToString().c_str()),
  2710. outerTypeDef->GetLatest()->mTypeDeclaration->mNameNode);
  2711. if (error != NULL)
  2712. error->mIsPersistent = true;
  2713. if (outerTypeDef->mIsPartial)
  2714. {
  2715. // Allow this typeDef be a full solo type by itself
  2716. outerTypeDef->mIsPartial = false;
  2717. if (outerTypeDef->mNextRevision != NULL)
  2718. outerTypeDef->mNextRevision->mIsPartial = false;
  2719. if (outerTypeDef->mPartialIdx != -1)
  2720. {
  2721. outerTypeDef->mPartialIdx = -1;
  2722. outerTypeDef->mDefState = BfTypeDef::DefState_New;
  2723. }
  2724. }
  2725. }
  2726. }
  2727. if (outerTypeDef->mDefState != BfTypeDef::DefState_Deleted)
  2728. checkMasterTypeDef = outerTypeDef;
  2729. if ((deletedCombinedPartial != NULL) && (checkMasterTypeDef != NULL) &&
  2730. (deletedCombinedPartial->NameEquals(checkMasterTypeDef)))
  2731. {
  2732. // Remap nested types to their master typeDef
  2733. for (auto nestedType : deletedCombinedPartial->mNestedTypes)
  2734. {
  2735. nestedType->mOuterType = checkMasterTypeDef;
  2736. }
  2737. deletedCombinedPartial = NULL;
  2738. checkMasterTypeDef = NULL;
  2739. }
  2740. outerTypeDefEntry = nextTypeDefEntry;
  2741. }
  2742. }
  2743. }
  2744. for (auto typeDef : prevSoloExtensions)
  2745. {
  2746. // If this got added to a composite partial then delete the previous solo type
  2747. if (typeDef->mIsPartial)
  2748. {
  2749. BfLogSysM("Solo partial going back to normal partial %p\n", typeDef);
  2750. typeDef->mIsPartial = false;
  2751. auto type = mContext->mScratchModule->ResolveTypeDef(typeDef, BfPopulateType_Identity);
  2752. mContext->DeleteType(type);
  2753. typeDef->mIsPartial = true;
  2754. }
  2755. }
  2756. mContext->UpdateRevisedTypes();
  2757. mContext->VerifyTypeLookups();
  2758. mContext->ValidateDependencies();
  2759. if (mStats.mTypesDeleted != 0)
  2760. {
  2761. mContext->UpdateAfterDeletingTypes();
  2762. mContext->ValidateDependencies();
  2763. }
  2764. mContext->RemoveInvalidWorkItems();
  2765. //for (auto typeDef : mSystem->mTypeDefs)
  2766. //{
  2767. // auto latestTypeDef = typeDef->GetLatest();
  2768. // if ((latestTypeDef->mOuterType != NULL) && (latestTypeDef->mOuterType->mIsPartial) && (latestTypeDef->mIsCombinedPartial))
  2769. // //((!latestTypeDef->mIsPartial) || (latestTypeDef->mIsCombinedPartial)))
  2770. // latestTypeDef->mOuterType = mSystem->GetOuterTypeNonPartial(latestTypeDef);
  2771. // /*String fullName = typeDef->mFullNameEx.ToString();
  2772. // if (fullName == "System.Collections.Generic.List`1.Enumerator`1")
  2773. // {
  2774. // NOP;
  2775. // }
  2776. // if ((typeDef->mOuterType != NULL) && (!typeDef->mIsPartial) && (typeDef->mOuterType->mIsPartial) && (!typeDef->mOuterType->mIsCombinedPartial))
  2777. // {
  2778. // NOP;
  2779. // }*/
  2780. //}
  2781. mSystem->mNeedsTypesHandledByCompiler = false;
  2782. //TODO:
  2783. //Sleep(300);
  2784. //mSystem->CheckLockYield();
  2785. }
  2786. BfTypeDef* BfCompiler::GetArrayTypeDef(int dimensions)
  2787. {
  2788. BF_ASSERT(dimensions <= 4);
  2789. if (dimensions == 1)
  2790. return mArray1TypeDef;
  2791. if (dimensions == 2)
  2792. return mArray2TypeDef;
  2793. if (dimensions == 3)
  2794. return mArray3TypeDef;
  2795. return mArray4TypeDef;
  2796. }
  2797. void BfCompiler::VisitAutocompleteExteriorIdentifiers()
  2798. {
  2799. for (auto checkNode : mResolvePassData->mExteriorAutocompleteCheckNodes)
  2800. {
  2801. bool isUsingDirective = false;
  2802. BfIdentifierNode* checkIdentifier = NULL;
  2803. if (auto usingDirective = BfNodeDynCast<BfUsingDirective>(checkNode))
  2804. {
  2805. checkIdentifier = usingDirective->mNamespace;
  2806. }
  2807. else if (auto usingDirective = BfNodeDynCast<BfUsingStaticDirective>(checkNode))
  2808. {
  2809. if (usingDirective->mTypeRef != NULL)
  2810. {
  2811. BF_ASSERT(mContext->mScratchModule->mCurTypeInstance == NULL);
  2812. SetAndRestoreValue<BfTypeInstance*> prevCurTypeInstance(mContext->mScratchModule->mCurTypeInstance, NULL);
  2813. mContext->mScratchModule->ResolveTypeRef(usingDirective->mTypeRef, NULL);
  2814. if (mResolvePassData->mAutoComplete != NULL)
  2815. mResolvePassData->mAutoComplete->CheckTypeRef(usingDirective->mTypeRef, false, isUsingDirective);
  2816. continue;
  2817. }
  2818. }
  2819. else
  2820. checkIdentifier = BfNodeDynCast<BfIdentifierNode>(checkNode);
  2821. if (checkIdentifier == NULL)
  2822. continue;
  2823. if (mResolvePassData->mAutoComplete != NULL)
  2824. mResolvePassData->mAutoComplete->CheckIdentifier(checkIdentifier, false, isUsingDirective);
  2825. if ((checkIdentifier->IsFromParser(mResolvePassData->mParser)) && (mResolvePassData->mSourceClassifier != NULL))
  2826. {
  2827. if (isUsingDirective)
  2828. {
  2829. while (auto qualifiedNameNode = BfNodeDynCast<BfQualifiedNameNode>(checkIdentifier))
  2830. {
  2831. mResolvePassData->mSourceClassifier->SetElementType(qualifiedNameNode->mRight, BfSourceElementType_Namespace);
  2832. checkIdentifier = qualifiedNameNode->mLeft;
  2833. }
  2834. if (checkIdentifier != NULL)
  2835. mResolvePassData->mSourceClassifier->SetElementType(checkIdentifier, BfSourceElementType_Namespace);
  2836. }
  2837. }
  2838. }
  2839. mResolvePassData->mExteriorAutocompleteCheckNodes.Clear();
  2840. }
  2841. void BfCompiler::VisitSourceExteriorNodes()
  2842. {
  2843. BP_ZONE("BfCompiler::VisitSourceExteriorNodes");
  2844. String str;
  2845. Array<BfAtom*> namespaceParts;
  2846. Array<BfAstNode*> srcNodes;
  2847. std::function<bool(BfAstNode*)> _AddName = [&](BfAstNode* node)
  2848. {
  2849. if (auto qualifiedName = BfNodeDynCast<BfQualifiedNameNode>(node))
  2850. {
  2851. if (!_AddName(qualifiedName->mLeft))
  2852. return false;
  2853. if (!_AddName(qualifiedName->mRight))
  2854. return false;
  2855. }
  2856. else if (auto identifier = BfNodeDynCast<BfIdentifierNode>(node))
  2857. {
  2858. srcNodes.Add(identifier);
  2859. str.Clear();
  2860. identifier->ToString(str);
  2861. auto atom = mSystem->FindAtom(str);
  2862. if (atom == NULL)
  2863. {
  2864. String prevNamespace;
  2865. for (auto part : namespaceParts)
  2866. {
  2867. if (!prevNamespace.IsEmpty())
  2868. prevNamespace += ".";
  2869. prevNamespace += part->mString;
  2870. }
  2871. if (prevNamespace.IsEmpty())
  2872. mPassInstance->Fail(StrFormat("The namespace '%s' does not exist", str.c_str()), identifier);
  2873. else
  2874. mPassInstance->Fail(StrFormat("The namespace '%s' does not exist in the namespace '%s'", str.c_str(), prevNamespace.c_str()), identifier);
  2875. return false;
  2876. }
  2877. namespaceParts.Add(atom);
  2878. }
  2879. return true;
  2880. };
  2881. auto _CheckParser = [&](BfParser* parser)
  2882. {
  2883. if (parser->mNextRevision != NULL)
  2884. parser = parser->mNextRevision;
  2885. if (parser->mAwaitingDelete)
  2886. return;
  2887. if (parser->mParserData->mExteriorNodesCheckIdx == mSystem->mTypeMapVersion)
  2888. return;
  2889. bool failed = false;
  2890. for (auto node : parser->mParserData->mExteriorNodes)
  2891. {
  2892. if (auto usingDirective = BfNodeDynCast<BfUsingDirective>(node))
  2893. {
  2894. srcNodes.Clear();
  2895. namespaceParts.Clear();
  2896. bool success = _AddName(usingDirective->mNamespace);
  2897. for (int i = 0; i < (int)namespaceParts.size(); i++)
  2898. {
  2899. BfAtomComposite checkNamespace;
  2900. checkNamespace.mParts = &namespaceParts[0];
  2901. checkNamespace.mSize = i + 1;
  2902. if (!mSystem->ContainsNamespace(checkNamespace, parser->mProject))
  2903. {
  2904. failed = true;
  2905. BfAtomComposite prevNamespace;
  2906. prevNamespace.mParts = &namespaceParts[0];
  2907. prevNamespace.mSize = i;
  2908. if (i == 0)
  2909. mPassInstance->Fail(StrFormat("The namespace '%s' does not exist", namespaceParts[i]->mString.ToString().c_str()), srcNodes[i]);
  2910. else
  2911. 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]);
  2912. break;
  2913. }
  2914. }
  2915. }
  2916. else if (auto usingDirective = BfNodeDynCast<BfUsingStaticDirective>(node))
  2917. {
  2918. if (usingDirective->mTypeRef != NULL)
  2919. {
  2920. BF_ASSERT(mContext->mScratchModule->mCurTypeInstance == NULL);
  2921. SetAndRestoreValue<BfTypeInstance*> prevCurTypeInstance(mContext->mScratchModule->mCurTypeInstance, NULL);
  2922. mContext->mScratchModule->ResolveTypeRef(usingDirective->mTypeRef, NULL);
  2923. if ((mResolvePassData != NULL) && (mResolvePassData->mAutoComplete != NULL))
  2924. mResolvePassData->mAutoComplete->CheckTypeRef(usingDirective->mTypeRef, false, false);
  2925. return;
  2926. }
  2927. }
  2928. }
  2929. if (!failed)
  2930. parser->mParserData->mExteriorNodesCheckIdx = mSystem->mTypeMapVersion;
  2931. };
  2932. if ((mResolvePassData != NULL) && (mResolvePassData->mParser != NULL))
  2933. {
  2934. _CheckParser(mResolvePassData->mParser);
  2935. }
  2936. else
  2937. {
  2938. for (auto parser : mSystem->mParsers)
  2939. {
  2940. _CheckParser(parser);
  2941. }
  2942. }
  2943. }
  2944. void BfCompiler::ProcessAutocompleteTempType()
  2945. {
  2946. BP_ZONE_F("BfCompiler::ProcessAutocompleteTempType %d", mResolvePassData->mResolveType);
  2947. String& autoCompleteResultString = *gTLStrReturn.Get();
  2948. autoCompleteResultString.clear();
  2949. if (mContext->mBfObjectType == NULL)
  2950. return; // Not initialized yet
  2951. auto module = mContext->mScratchModule;
  2952. auto autoComplete = mResolvePassData->mAutoComplete;
  2953. BfLogSysM("ProcessAutocompleteTempType %d\n", autoComplete->mResolveType);
  2954. SetAndRestoreValue<bool> prevCanceling(mCanceling, false);
  2955. BF_ASSERT(mResolvePassData->mAutoComplete->mDefMethod == NULL);
  2956. if (autoComplete->mResolveType == BfResolveType_GetNavigationData)
  2957. {
  2958. for (auto tempTypeDef : mResolvePassData->mAutoCompleteTempTypes)
  2959. {
  2960. String typeName = tempTypeDef->ToString();
  2961. BfLogSysM("BfResolveType_GetNavigationData TypeDef:%p %s\n", tempTypeDef, typeName.c_str());
  2962. auto refNode = tempTypeDef->GetRefNode();
  2963. if ((refNode != NULL) && (!tempTypeDef->IsGlobalsContainer()))
  2964. {
  2965. if (!autoCompleteResultString.empty())
  2966. autoCompleteResultString += "\n";
  2967. String typeName = BfTypeUtils::TypeToString(tempTypeDef, BfTypeNameFlag_OmitNamespace);
  2968. module->UpdateSrcPos(refNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
  2969. autoCompleteResultString += typeName;
  2970. if (tempTypeDef->mTypeCode == BfTypeCode_Object)
  2971. autoCompleteResultString += "\tclass";
  2972. else if (tempTypeDef->mTypeCode == BfTypeCode_Enum)
  2973. autoCompleteResultString += "\tenum";
  2974. else if (tempTypeDef->mTypeCode == BfTypeCode_Struct)
  2975. autoCompleteResultString += "\tstruct";
  2976. else if (tempTypeDef->mTypeCode == BfTypeCode_TypeAlias)
  2977. autoCompleteResultString += "\ttypealias";
  2978. else
  2979. autoCompleteResultString += "\t";
  2980. autoCompleteResultString += StrFormat("\t%d\t%d", module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
  2981. }
  2982. String methodText;
  2983. for (auto methodDef : tempTypeDef->mMethods)
  2984. {
  2985. if (((methodDef->mMethodType == BfMethodType_Normal) || (methodDef->mMethodType == BfMethodType_Operator) ||
  2986. (methodDef->mMethodType == BfMethodType_Ctor) || (methodDef->mMethodType == BfMethodType_Dtor) ||
  2987. (methodDef->mMethodType == BfMethodType_Mixin)) &&
  2988. (methodDef->mMethodDeclaration != NULL))
  2989. {
  2990. methodText = methodDef->ToString();
  2991. if (typeName != "@")
  2992. methodText = typeName + "." + methodText;
  2993. if (!autoCompleteResultString.empty())
  2994. autoCompleteResultString += "\n";
  2995. auto methodDeclaration = methodDef->GetMethodDeclaration();
  2996. BfAstNode* refNode = methodDeclaration;
  2997. if (methodDeclaration->mBody != NULL)
  2998. refNode = methodDeclaration->mBody;
  2999. else if (methodDeclaration->mNameNode != NULL)
  3000. refNode = methodDeclaration->mNameNode;
  3001. module->UpdateSrcPos(refNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
  3002. methodText += StrFormat("\tmethod\t%d\t%d", module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
  3003. autoCompleteResultString += methodText;
  3004. }
  3005. }
  3006. for (auto propDef : tempTypeDef->mProperties)
  3007. {
  3008. auto propDeclaration = BfNodeDynCast<BfPropertyDeclaration>(propDef->mFieldDeclaration);
  3009. if ((propDeclaration == NULL) || (propDeclaration->mNameNode == NULL))
  3010. continue;
  3011. String propText = propDef->mName;
  3012. if (typeName != "@")
  3013. propText = typeName + "." + propText;
  3014. if (!autoCompleteResultString.empty())
  3015. autoCompleteResultString += "\n";
  3016. BfAstNode* refNode = propDeclaration->mNameNode;
  3017. module->UpdateSrcPos(refNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
  3018. propText += StrFormat("\tproperty\t%d\t%d", module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
  3019. autoCompleteResultString += propText;
  3020. }
  3021. }
  3022. module->CleanupFileInstances();
  3023. return;
  3024. }
  3025. if (autoComplete->mResolveType == BfResolveType_GetCurrentLocation)
  3026. {
  3027. for (auto tempTypeDef : mResolvePassData->mAutoCompleteTempTypes)
  3028. {
  3029. String typeName = tempTypeDef->mNamespace.ToString();
  3030. if (!typeName.empty())
  3031. typeName += ".";
  3032. typeName += tempTypeDef->ToString();
  3033. autoCompleteResultString = typeName;
  3034. int cursorPos = mResolvePassData->mParser->mCursorIdx;
  3035. for (auto methodDef : tempTypeDef->mMethods)
  3036. {
  3037. BfAstNode* defNode = methodDef->mMethodDeclaration;
  3038. if (auto propertyDeclaration = methodDef->GetPropertyDeclaration())
  3039. defNode = propertyDeclaration;
  3040. if ((defNode != NULL) &&
  3041. (defNode->Contains(cursorPos)))
  3042. {
  3043. String methodText = methodDef->ToString();
  3044. if (typeName != "@")
  3045. methodText = typeName + "." + methodText;
  3046. autoCompleteResultString = methodText;
  3047. break;
  3048. }
  3049. }
  3050. }
  3051. module->CleanupFileInstances();
  3052. return;
  3053. }
  3054. // >>> VisitExteriorIdentifiers
  3055. mResolvePassData->mAutoComplete->SetModule(module);
  3056. {
  3057. SetAndRestoreValue<BfTypeState*> prevTypeState(module->mContext->mCurTypeState, NULL);
  3058. BP_ZONE("VisitExteriorIdentifiers");
  3059. VisitAutocompleteExteriorIdentifiers();
  3060. }
  3061. VisitSourceExteriorNodes();
  3062. BfTypeDef* tempTypeDef = NULL;
  3063. for (auto checkTempType : mResolvePassData->mAutoCompleteTempTypes)
  3064. {
  3065. if (mResolvePassData->mAutoComplete->IsAutocompleteNode(checkTempType->mTypeDeclaration))
  3066. {
  3067. tempTypeDef = checkTempType;
  3068. mContext->HandleChangedTypeDef(tempTypeDef, true);
  3069. }
  3070. }
  3071. if (tempTypeDef == NULL)
  3072. {
  3073. GenerateAutocompleteInfo();
  3074. BfLogSysM("ProcessAutocompleteTempType - no tempTypeDef\n");
  3075. return;
  3076. }
  3077. if (tempTypeDef->mProject->mDisabled)
  3078. {
  3079. BfLogSysM("ProcessAutocompleteTempType - project disabled\n");
  3080. return;
  3081. }
  3082. SetAndRestoreValue<BfMethodState*> prevMethodState(module->mCurMethodState, NULL);
  3083. BfTypeState typeState;
  3084. typeState.mCurTypeDef = tempTypeDef;
  3085. SetAndRestoreValue<BfTypeState*> prevTypeState(module->mContext->mCurTypeState, &typeState);
  3086. BfTypeDef* actualTypeDef = NULL;
  3087. auto typeName = tempTypeDef->mFullName;
  3088. int wantNumGenericParams = (int)tempTypeDef->mGenericParamDefs.size();
  3089. auto actualTypeDefItr = mSystem->mTypeDefs.TryGet(typeName);
  3090. while (actualTypeDefItr)
  3091. {
  3092. auto checkTypeDef = *actualTypeDefItr;
  3093. if ((!checkTypeDef->mIsPartial) /*&& (checkTypeDef->mTypeCode != BfTypeCode_Extension)*/ &&
  3094. ((checkTypeDef->mTypeCode == tempTypeDef->mTypeCode) || (tempTypeDef->mTypeCode == BfTypeCode_Extension)))
  3095. {
  3096. if ((checkTypeDef->NameEquals(tempTypeDef)) && (checkTypeDef->mIsCombinedPartial) &&
  3097. (checkTypeDef->mGenericParamDefs.size() == tempTypeDef->mGenericParamDefs.size()) &&
  3098. (tempTypeDef->mProject->ContainsReference(checkTypeDef->mProject)))
  3099. {
  3100. actualTypeDef = mSystem->FilterDeletedTypeDef(checkTypeDef);
  3101. break;
  3102. }
  3103. if ((checkTypeDef->mGenericParamDefs.size() == wantNumGenericParams) &&
  3104. (FileNameEquals(tempTypeDef->mSource->mSourceData->ToParserData()->mFileName, checkTypeDef->mSource->mSourceData->ToParserData()->mFileName)) &&
  3105. (tempTypeDef->mProject == checkTypeDef->mProject))
  3106. {
  3107. actualTypeDef = mSystem->FilterDeletedTypeDef(checkTypeDef);
  3108. break;
  3109. }
  3110. }
  3111. actualTypeDefItr.MoveToNextHashMatch();
  3112. }
  3113. if ((actualTypeDef == NULL) || (actualTypeDef->mTypeDeclaration == NULL))
  3114. {
  3115. auto autoComplete = mResolvePassData->mAutoComplete;
  3116. if (autoComplete->IsAutocompleteNode(tempTypeDef->mTypeDeclaration->mNameNode))
  3117. {
  3118. BfIdentifierNode* nameNode = tempTypeDef->mTypeDeclaration->mNameNode;
  3119. if (tempTypeDef->mTypeCode == BfTypeCode_Extension)
  3120. {
  3121. autoComplete->AddTopLevelNamespaces(nameNode);
  3122. autoComplete->AddTopLevelTypes(nameNode);
  3123. autoComplete->mInsertStartIdx = nameNode->GetSrcStart();
  3124. autoComplete->mInsertEndIdx = nameNode->GetSrcEnd();
  3125. }
  3126. }
  3127. //mResolvePassData->mSourceClassifier->MarkSkipped(tempTypeDef->mTypeDeclaration);
  3128. GenerateAutocompleteInfo();
  3129. return;
  3130. }
  3131. if (tempTypeDef->mTypeDeclaration->mAttributes != NULL)
  3132. {
  3133. mResolvePassData->mSourceClassifier->VisitChild(tempTypeDef->mTypeDeclaration->mAttributes);
  3134. }
  3135. BfTypeInstance* typeInst;
  3136. {
  3137. BP_ZONE("ProcessAutocompleteTempType.ResolveTypeDef");
  3138. typeInst = (BfTypeInstance*)module->ResolveTypeDef(actualTypeDef, BfPopulateType_IdentityNoRemapAlias);
  3139. if ((typeInst != NULL) && (typeInst->IsIncomplete()))
  3140. module->PopulateType(typeInst, BfPopulateType_Full);
  3141. }
  3142. if (typeInst == NULL)
  3143. {
  3144. return;
  3145. }
  3146. BF_ASSERT((typeInst->mSize != -1) || (typeInst->IsTypeAlias()));
  3147. #ifdef _DEBUG
  3148. if ((typeInst->mModule != NULL) && (!typeInst->mModule->mIsScratchModule))
  3149. mLastAutocompleteModule = typeInst->mModule;
  3150. #endif
  3151. SetAndRestoreValue<BfTypeInstance*> prevType(module->mCurTypeInstance, typeInst);
  3152. typeState.mTypeInstance = typeInst;
  3153. BfGenericExtensionEntry* genericExEntry = NULL;
  3154. bool hadTempExtensionInfo = false;
  3155. if ((tempTypeDef->IsExtension()) && (actualTypeDef->mIsCombinedPartial) && (typeInst->IsGenericTypeInstance()))
  3156. {
  3157. // Add to our extension info map and then take it out at the end...
  3158. auto genericTypeInst = (BfGenericTypeInstance*)typeInst;
  3159. module->BuildGenericExtensionInfo(genericTypeInst, tempTypeDef);
  3160. genericTypeInst->mGenericExtensionInfo->mExtensionMap.TryGetValue(tempTypeDef, &genericExEntry);
  3161. BF_ASSERT(genericExEntry != NULL);
  3162. hadTempExtensionInfo = true;
  3163. }
  3164. if ((typeInst->IsUnspecializedType()) || (!typeInst->IsGenericTypeInstance()))
  3165. {
  3166. auto autoComplete = mResolvePassData->mAutoComplete;
  3167. if (autoComplete->IsAutocompleteNode(tempTypeDef->mTypeDeclaration->mNameNode))
  3168. {
  3169. BfIdentifierNode* nameNode;
  3170. nameNode = tempTypeDef->mTypeDeclaration->mNameNode;
  3171. if ((actualTypeDef->mIsCombinedPartial) && (tempTypeDef->mTypeCode == BfTypeCode_Extension))
  3172. {
  3173. autoComplete->AddTopLevelNamespaces(tempTypeDef->mTypeDeclaration->mNameNode);
  3174. autoComplete->AddTopLevelTypes(tempTypeDef->mTypeDeclaration->mNameNode);
  3175. autoComplete->SetDefinitionLocation(actualTypeDef->mTypeDeclaration->mNameNode);
  3176. }
  3177. else
  3178. autoComplete->SetDefinitionLocation(nameNode);
  3179. autoComplete->mDefType = actualTypeDef;
  3180. autoComplete->mInsertStartIdx = nameNode->GetSrcStart();
  3181. autoComplete->mInsertEndIdx = nameNode->GetSrcEnd();
  3182. }
  3183. }
  3184. if (tempTypeDef->mTypeCode == BfTypeCode_TypeAlias)
  3185. {
  3186. auto typeAliasDecl = (BfTypeAliasDeclaration*)tempTypeDef->mTypeDeclaration;
  3187. if (typeAliasDecl->mAliasToType != NULL)
  3188. {
  3189. autoComplete->CheckTypeRef(typeAliasDecl->mAliasToType, false);
  3190. module->ResolveTypeRef(typeAliasDecl->mAliasToType);
  3191. }
  3192. }
  3193. // Save and restore mFieldResolveReentrys, we could fire off autocomplete while resolving a field
  3194. SetAndRestoreValue<decltype (module->mContext->mFieldResolveReentrys)> prevTypeResolveReentry(module->mContext->mFieldResolveReentrys);
  3195. module->mContext->mFieldResolveReentrys.Clear();
  3196. if (tempTypeDef->mTypeDeclaration->mAttributes != NULL)
  3197. {
  3198. BfAttributeTargets attrTarget;
  3199. if (tempTypeDef->mIsDelegate)
  3200. attrTarget = BfAttributeTargets_Delegate;
  3201. else if (typeInst->IsEnum())
  3202. attrTarget = BfAttributeTargets_Enum;
  3203. else if (typeInst->IsInterface())
  3204. attrTarget = BfAttributeTargets_Interface;
  3205. else if (typeInst->IsStruct())
  3206. attrTarget = BfAttributeTargets_Struct;
  3207. else
  3208. attrTarget = BfAttributeTargets_Class;
  3209. auto customAttrs = module->GetCustomAttributes(tempTypeDef->mTypeDeclaration->mAttributes, attrTarget);
  3210. delete customAttrs;
  3211. }
  3212. for (int genericParamIdx = 0; genericParamIdx < (int)tempTypeDef->mGenericParamDefs.size(); genericParamIdx++)
  3213. {
  3214. auto genericParamDef = tempTypeDef->mGenericParamDefs[genericParamIdx];
  3215. auto genericParamInstance = new BfGenericTypeParamInstance(tempTypeDef, genericParamIdx);
  3216. genericParamInstance->mExternType = module->GetGenericParamType(BfGenericParamKind_Type, genericParamIdx);
  3217. module->ResolveGenericParamConstraints(genericParamInstance, true);
  3218. delete genericParamInstance;
  3219. for (auto nameNode : genericParamDef->mNameNodes)
  3220. module->HandleTypeGenericParamRef(nameNode, tempTypeDef, genericParamIdx);
  3221. }
  3222. for (auto fieldDef : tempTypeDef->mFields)
  3223. {
  3224. BP_ZONE("ProcessAutocompleteTempType.CheckField");
  3225. auto fieldDecl = fieldDef->mFieldDeclaration;
  3226. if (BfNodeIsA<BfPropertyDeclaration>(fieldDecl))
  3227. continue; // Don't process auto-generated property fields
  3228. if (fieldDef->mTypeRef != NULL)
  3229. module->ResolveTypeRef(fieldDef->mTypeRef);
  3230. mResolvePassData->mAutoComplete->CheckTypeRef(fieldDef->mTypeRef, true);
  3231. BfFieldDef* actualFieldDef = NULL;
  3232. for (auto checkFieldDef : actualTypeDef->mFields)
  3233. {
  3234. if ((checkFieldDef->mName == fieldDef->mName) &&
  3235. (checkFieldDef->mIsConst == fieldDef->mIsConst) &&
  3236. (checkFieldDef->mIsStatic == fieldDef->mIsStatic))
  3237. {
  3238. actualFieldDef = checkFieldDef;
  3239. }
  3240. }
  3241. if ((autoComplete->mIsGetDefinition) && (fieldDef->mFieldDeclaration != NULL) && (autoComplete->IsAutocompleteNode(fieldDef->mFieldDeclaration->mNameNode)))
  3242. {
  3243. for (int i = 0; i < (int)actualTypeDef->mFields.size(); i++)
  3244. {
  3245. auto actualFieldDef = actualTypeDef->mFields[i];
  3246. if (actualFieldDef->mName == fieldDef->mName)
  3247. {
  3248. autoComplete->mDefType = actualTypeDef;
  3249. autoComplete->mDefField = actualFieldDef;
  3250. autoComplete->SetDefinitionLocation(fieldDef->mFieldDeclaration->mNameNode);
  3251. autoComplete->mInsertStartIdx = fieldDef->mFieldDeclaration->mNameNode->GetSrcStart();
  3252. autoComplete->mInsertEndIdx = fieldDef->mFieldDeclaration->mNameNode->GetSrcEnd();
  3253. break;
  3254. }
  3255. }
  3256. }
  3257. if ((fieldDef->mFieldDeclaration != NULL) && (fieldDef->mFieldDeclaration->mAttributes != NULL))
  3258. {
  3259. auto customAttrs = module->GetCustomAttributes(fieldDef->mFieldDeclaration->mAttributes, BfAttributeTargets_Field);
  3260. delete customAttrs;
  3261. }
  3262. if (fieldDef->mIsConst)
  3263. {
  3264. module->ResolveConstField(typeInst, NULL, fieldDef);
  3265. }
  3266. if (fieldDef->mInitializer == NULL)
  3267. {
  3268. if (BfNodeIsA<BfVarTypeReference>(fieldDef->mTypeRef))
  3269. {
  3270. if (fieldDef->mInitializer == NULL)
  3271. {
  3272. if ((fieldDef->mTypeRef->IsA<BfVarTypeReference>()) || (fieldDef->mTypeRef->IsA<BfLetTypeReference>()))
  3273. mPassInstance->Fail("Implicitly-typed fields must be initialized", fieldDef->GetRefNode());
  3274. }
  3275. }
  3276. }
  3277. }
  3278. auto checkTypeDef = tempTypeDef;
  3279. while (checkTypeDef != NULL)
  3280. {
  3281. for (auto baseType : checkTypeDef->mBaseTypes)
  3282. {
  3283. autoComplete->CheckTypeRef(baseType, false);
  3284. module->ResolveTypeRef(baseType);
  3285. }
  3286. checkTypeDef = checkTypeDef->mOuterType;
  3287. }
  3288. for (auto propDef : tempTypeDef->mProperties)
  3289. {
  3290. if ((propDef->mFieldDeclaration != NULL) && (propDef->mFieldDeclaration->mAttributes != NULL))
  3291. {
  3292. auto customAttrs = module->GetCustomAttributes(propDef->mFieldDeclaration->mAttributes, BfAttributeTargets_Property);
  3293. delete customAttrs;
  3294. }
  3295. auto propDeclaration = BfNodeDynCast<BfPropertyDeclaration>(propDef->mFieldDeclaration);
  3296. if (propDeclaration != NULL)
  3297. autoComplete->CheckProperty(propDeclaration);
  3298. module->ResolveTypeRef(propDef->mTypeRef, BfPopulateType_Data, BfResolveTypeRefFlag_AllowRef);
  3299. if ((autoComplete->mIsGetDefinition) && (propDef->mFieldDeclaration != NULL) && (autoComplete->IsAutocompleteNode(propDef->mFieldDeclaration->mNameNode)))
  3300. {
  3301. auto checkType = typeInst;
  3302. while (checkType != NULL)
  3303. {
  3304. for (auto checkProp : checkType->mTypeDef->mProperties)
  3305. {
  3306. if (checkProp->mName == propDef->mName)
  3307. {
  3308. auto checkPropDeclaration = BfNodeDynCast<BfPropertyDeclaration>(checkProp->mFieldDeclaration);
  3309. if ((checkPropDeclaration->mVirtualSpecifier == NULL) || (checkPropDeclaration->mVirtualSpecifier->GetToken() == BfToken_Virtual))
  3310. {
  3311. autoComplete->SetDefinitionLocation(checkPropDeclaration->mNameNode);
  3312. autoComplete->mDefType = checkType->mTypeDef;
  3313. autoComplete->mDefProp = checkProp;
  3314. checkType = NULL;
  3315. break;
  3316. }
  3317. }
  3318. }
  3319. if (checkType != NULL)
  3320. checkType = checkType->mBaseType;
  3321. }
  3322. }
  3323. }
  3324. Array<BfMethodInstance*> methodInstances;
  3325. for (auto methodDef : tempTypeDef->mMethods)
  3326. {
  3327. auto methodDeclaration = methodDef->GetMethodDeclaration();
  3328. if (methodDeclaration != NULL)
  3329. autoComplete->CheckMethod(methodDeclaration, false);
  3330. if (!methodDef->mWantsBody)
  3331. {
  3332. if (methodDeclaration != NULL)
  3333. {
  3334. if (methodDeclaration->mAttributes != NULL)
  3335. {
  3336. auto customAttrs = module->GetCustomAttributes(methodDeclaration->mAttributes, (methodDef->mMethodType == BfMethodType_Ctor) ? BfAttributeTargets_Constructor : BfAttributeTargets_Method);
  3337. delete customAttrs;
  3338. }
  3339. }
  3340. else if (auto methodPropertyDeclaration = methodDef->GetPropertyMethodDeclaration())
  3341. {
  3342. if (methodPropertyDeclaration->mAttributes != NULL)
  3343. {
  3344. auto customAttrs = module->GetCustomAttributes(methodPropertyDeclaration->mAttributes, BfAttributeTargets_Method);
  3345. delete customAttrs;
  3346. }
  3347. }
  3348. continue;
  3349. }
  3350. BP_ZONE("ProcessAutocompleteTempType.CheckMethod");
  3351. BfMethodInstanceGroup methodInstanceGroup;
  3352. methodInstanceGroup.mOwner = typeInst;
  3353. methodInstanceGroup.mOnDemandKind = BfMethodOnDemandKind_AlwaysInclude;
  3354. BfMethodInstance* methodInstance = new BfMethodInstance();
  3355. methodInstances.push_back(methodInstance);
  3356. methodInstance->mMethodDef = methodDef;
  3357. methodInstance->mMethodInstanceGroup = &methodInstanceGroup;
  3358. methodInstance->mIsAutocompleteMethod = true;
  3359. for (int genericParamIdx = 0; genericParamIdx < (int)methodDef->mGenericParams.size(); genericParamIdx++)
  3360. {
  3361. auto genericParamType = module->GetGenericParamType(BfGenericParamKind_Method, genericParamIdx);
  3362. methodInstance->GetMethodInfoEx()->mMethodGenericArguments.push_back(genericParamType);
  3363. auto genericParamInstance = new BfGenericMethodParamInstance(methodDef, genericParamIdx);
  3364. methodInstance->GetMethodInfoEx()->mGenericParams.push_back(genericParamInstance);
  3365. }
  3366. for (int externConstraintIdx = 0; externConstraintIdx < (int)methodDef->mExternalConstraints.size(); externConstraintIdx++)
  3367. {
  3368. auto genericParamInstance = new BfGenericMethodParamInstance(methodDef, externConstraintIdx + (int)methodDef->mGenericParams.size());
  3369. methodInstance->GetMethodInfoEx()->mGenericParams.push_back(genericParamInstance);
  3370. }
  3371. SetAndRestoreValue<BfFilePosition> prevFilePos(module->mCurFilePosition);
  3372. SetAndRestoreValue<BfMethodInstance*> prevMethodInst(module->mCurMethodInstance, methodInstance);
  3373. module->DoMethodDeclaration(methodDeclaration, true);
  3374. module->mIncompleteMethodCount++;
  3375. module->ProcessMethod(methodInstance);
  3376. if (methodInstance->mIRFunction)
  3377. {
  3378. BfLogSysM("Autocomplete removing IRFunction %d\n", methodInstance->mIRFunction.mId);
  3379. module->mBfIRBuilder->Func_DeleteBody(methodInstance->mIRFunction);
  3380. module->mBfIRBuilder->Func_EraseFromParent(methodInstance->mIRFunction);
  3381. }
  3382. }
  3383. if ((mResolvePassData->mAutoComplete->mDefType == actualTypeDef) && (mResolvePassData->mAutoComplete->mDefMethod != NULL))
  3384. {
  3385. BfMethodDef* tempDefMethod = NULL;
  3386. for (auto checkMethod : tempTypeDef->mMethods)
  3387. {
  3388. if (checkMethod == mResolvePassData->mAutoComplete->mDefMethod)
  3389. tempDefMethod = checkMethod;
  3390. }
  3391. if (tempDefMethod != NULL)
  3392. {
  3393. BfMethodDef* actualReplaceMethodDef = NULL;
  3394. for (auto checkMethodDef : actualTypeDef->mMethods)
  3395. {
  3396. if ((checkMethodDef->mMethodType == tempDefMethod->mMethodType) &&
  3397. (checkMethodDef->mMethodDeclaration != NULL) && (tempDefMethod->mMethodDeclaration != NULL) &&
  3398. (checkMethodDef->mMethodDeclaration->GetSrcStart() == tempDefMethod->mMethodDeclaration->GetSrcStart()))
  3399. actualReplaceMethodDef = checkMethodDef;
  3400. }
  3401. if (actualReplaceMethodDef == NULL)
  3402. {
  3403. autoComplete->mDefType = NULL;
  3404. autoComplete->mDefField = NULL;
  3405. autoComplete->mDefProp = NULL;
  3406. autoComplete->mReplaceLocalId = -1;
  3407. autoComplete->mDefMethod = NULL;
  3408. }
  3409. else
  3410. autoComplete->mDefMethod = actualReplaceMethodDef;
  3411. }
  3412. }
  3413. if (hadTempExtensionInfo)
  3414. {
  3415. auto genericTypeInst = (BfGenericTypeInstance*)typeInst;
  3416. genericTypeInst->mGenericExtensionInfo->mExtensionMap.Remove(tempTypeDef);
  3417. }
  3418. for (auto checkNode : mResolvePassData->mExteriorAutocompleteCheckNodes)
  3419. {
  3420. BP_ZONE("ProcessAutocompleteTempType.CheckIdentifier");
  3421. bool isUsingDirective = false;
  3422. BfIdentifierNode* checkIdentifier = NULL;
  3423. if (auto usingDirective = BfNodeDynCast<BfUsingDirective>(checkNode))
  3424. {
  3425. isUsingDirective = true;
  3426. checkIdentifier = usingDirective->mNamespace;
  3427. }
  3428. else
  3429. checkIdentifier = BfNodeDynCast<BfIdentifierNode>(checkNode);
  3430. mResolvePassData->mAutoComplete->CheckIdentifier(checkIdentifier, false, isUsingDirective);
  3431. }
  3432. GenerateAutocompleteInfo();
  3433. for (auto methodInstance : methodInstances)
  3434. delete methodInstance;
  3435. methodInstances.Clear();
  3436. module->CleanupFileInstances();
  3437. module->ClearConstData();
  3438. BfLogSysM("ProcessAutocompleteTempType end\n");
  3439. }
  3440. BfType* BfCompiler::CheckSymbolReferenceTypeRef(BfModule* module, BfTypeReference* typeRef)
  3441. {
  3442. //auto resolvedType = module->ResolveTypeRef(typeRef, BfPopulateType_Declaration,
  3443. //(BfResolveTypeRefFlags)(BfResolveTypeRefFlag_AllowRef | BfResolveTypeRefFlag_AllowGenericMethodParamConstValue | BfResolveTypeRefFlag_AllowGenericTypeParamConstValue));
  3444. auto resolvedType = module->ResolveTypeRef(typeRef, BfPopulateType_Declaration, BfResolveTypeRefFlag_AllowRef);
  3445. if ((resolvedType != NULL) && (resolvedType->IsTypeInstance()))
  3446. {
  3447. auto typeInst = resolvedType->ToTypeInstance();
  3448. //TODO: Did we need this?
  3449. // The ResolveTypeRef call already does mResolvePassData->HandleTypeReference, so we were adding double entries
  3450. //mResolvePassData->HandleTypeReference(typeRef, typeInst->mTypeDef);
  3451. }
  3452. return resolvedType;
  3453. }
  3454. void BfCompiler::AddDepsToRebuildTypeList(BfTypeInstance* replaceTypeInst, HashSet<BfTypeInstance*>& rebuildTypeInstList)
  3455. {
  3456. for (auto& dep : replaceTypeInst->mDependencyMap)
  3457. {
  3458. auto depType = dep.mKey;
  3459. auto depTypeInst = depType->ToTypeInstance();
  3460. if (depTypeInst == NULL)
  3461. continue;
  3462. if (mResolvePassData->mParser != NULL)
  3463. {
  3464. // Only find references within the current file
  3465. if (!depTypeInst->mTypeDef->HasSource(mResolvePassData->mParser))
  3466. continue;
  3467. }
  3468. bool allowRebuild = ((!depTypeInst->IsGenericTypeInstance()) ||
  3469. ((depTypeInst->IsUnspecializedType()) && (!depTypeInst->IsUnspecializedTypeVariation())));
  3470. if ((depTypeInst->IsClosure()) || (depTypeInst->IsConcreteInterfaceType()) || (depTypeInst->IsRetTypeType()))
  3471. allowRebuild = false;
  3472. if (allowRebuild)
  3473. rebuildTypeInstList.Add(depTypeInst);
  3474. }
  3475. }
  3476. void BfCompiler::GetSymbolReferences()
  3477. {
  3478. BfLogSysM("GetSymbolReferences\n");
  3479. if (mInInvalidState)
  3480. return; // Don't even try
  3481. auto context = mContext;
  3482. if (context->mBfObjectType == NULL)
  3483. return; // Not initialized yet
  3484. auto module = context->mScratchModule;
  3485. if (mResolvePassData->mAutoComplete != NULL)
  3486. mResolvePassData->mAutoComplete->SetModule(module);
  3487. const char* strPtr = mResolvePassData->mQueuedReplaceTypeDef.c_str();
  3488. BfTypeDef* typeDef = mSystem->FindTypeDefEx(strPtr);
  3489. if ((typeDef == NULL) || (typeDef->mTypeDeclaration == NULL))
  3490. return;
  3491. mResolvePassData->mSymbolReferenceTypeDef = typeDef;
  3492. auto replaceType = module->ResolveTypeDef(typeDef, BfPopulateType_IdentityNoRemapAlias);
  3493. module->PopulateType(replaceType);
  3494. auto replaceTypeInst = replaceType->ToTypeInstance();
  3495. HashSet<BfTypeInstance*> rebuildTypeInstList;
  3496. if (mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Local)
  3497. {
  3498. AddDepsToRebuildTypeList(replaceTypeInst, rebuildTypeInstList);
  3499. // For generic types, add all references from all specialized versions
  3500. if (replaceTypeInst->IsGenericTypeInstance())
  3501. {
  3502. for (auto type : mContext->mResolvedTypes)
  3503. {
  3504. auto typeInst = type->ToTypeInstance();
  3505. if ((typeInst != replaceTypeInst) && (typeInst != NULL) && (typeInst->mTypeDef == typeDef))
  3506. AddDepsToRebuildTypeList(typeInst, rebuildTypeInstList);
  3507. }
  3508. }
  3509. }
  3510. rebuildTypeInstList.Add(replaceTypeInst);
  3511. //TODO: Did we need this to be rebuildTypeInst->mModule??? Why?
  3512. //auto rebuildModule = rebuildTypeInst->mModule;
  3513. auto rebuildModule = context->mScratchModule;
  3514. auto _CheckAttributes = [&](BfAttributeDirective* attrib, BfTypeDef* declaringType)
  3515. {
  3516. if ((mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Type) &&
  3517. (mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Field) &&
  3518. (mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Property))
  3519. return;
  3520. while (attrib != NULL)
  3521. {
  3522. String attrName = attrib->mAttributeTypeRef->ToString();
  3523. BfType* attrType = NULL;
  3524. BfAtomComposite nameComposite;
  3525. if (mSystem->ParseAtomComposite(attrName + "Attribute", nameComposite))
  3526. {
  3527. BfTypeDef* attrTypeDef = module->FindTypeDefRaw(nameComposite, 0, replaceTypeInst, declaringType, NULL);
  3528. if (attrTypeDef != NULL)
  3529. {
  3530. mResolvePassData->HandleTypeReference(attrib->mAttributeTypeRef, attrTypeDef);
  3531. attrTypeDef->PopulateMemberSets();
  3532. for (auto argExpr : attrib->mArguments)
  3533. {
  3534. if (auto assignExpr = BfNodeDynCast<BfAssignmentExpression>(argExpr))
  3535. {
  3536. auto propName = assignExpr->mLeft->ToString();
  3537. BfMemberSetEntry* propDefEntry;
  3538. if (attrTypeDef->mPropertySet.TryGetWith(propName, &propDefEntry))
  3539. {
  3540. mResolvePassData->HandlePropertyReference(assignExpr->mLeft, attrTypeDef, (BfPropertyDef*)propDefEntry->mMemberDef);
  3541. }
  3542. else if (attrTypeDef->mFieldSet.TryGetWith(propName, &propDefEntry))
  3543. {
  3544. mResolvePassData->HandleFieldReference(assignExpr->mLeft, attrTypeDef, (BfFieldDef*)propDefEntry->mMemberDef);
  3545. }
  3546. }
  3547. }
  3548. }
  3549. }
  3550. attrib = attrib->mNextAttribute;
  3551. }
  3552. };
  3553. for (auto rebuildTypeInst : rebuildTypeInstList)
  3554. {
  3555. auto context = mContext;
  3556. auto module = context->mScratchModule;
  3557. SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(module->mCurTypeInstance, rebuildTypeInst);
  3558. SetAndRestoreValue<bool> prevIgnoreErrors(module->mIgnoreErrors, true);
  3559. // Run through base types for type renames
  3560. auto typeDef = rebuildTypeInst->mTypeDef;
  3561. if ((typeDef->mTypeDeclaration != NULL) && (typeDef->mTypeDeclaration->mNameNode != NULL))
  3562. {
  3563. if (typeDef->mIsCombinedPartial)
  3564. {
  3565. for (auto checkTypeDef : typeDef->mPartials)
  3566. {
  3567. auto nameNode = checkTypeDef->mTypeDeclaration->mNameNode;
  3568. if ((mResolvePassData->mParser == NULL) || (nameNode->IsFromParser(mResolvePassData->mParser)))
  3569. mResolvePassData->HandleTypeReference(nameNode, typeDef);
  3570. if (checkTypeDef->IsExtension())
  3571. {
  3572. if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Type)
  3573. {
  3574. BfTypeState typeState;
  3575. typeState.mCurTypeDef = checkTypeDef;
  3576. SetAndRestoreValue<BfTypeState*> prevTypeState(module->mContext->mCurTypeState, &typeState);
  3577. for (auto baseTypeRef : checkTypeDef->mBaseTypes)
  3578. CheckSymbolReferenceTypeRef(module, baseTypeRef);
  3579. for (auto genericParam : checkTypeDef->mGenericParamDefs)
  3580. {
  3581. for (auto constraint : genericParam->mConstraints)
  3582. {
  3583. if (auto constraintTypeRef = BfNodeDynCast<BfTypeReference>(constraint))
  3584. {
  3585. module->ResolveTypeRef(constraintTypeRef, BfPopulateType_Identity);
  3586. }
  3587. else if (auto opConstraint = BfNodeDynCast<BfGenericOperatorConstraint>(constraint))
  3588. {
  3589. module->ResolveTypeRef(opConstraint->mLeftType, BfPopulateType_Identity);
  3590. module->ResolveTypeRef(opConstraint->mRightType, BfPopulateType_Identity);
  3591. }
  3592. }
  3593. }
  3594. }
  3595. }
  3596. }
  3597. }
  3598. else
  3599. {
  3600. mResolvePassData->HandleTypeReference(typeDef->mTypeDeclaration->mNameNode, typeDef);
  3601. }
  3602. }
  3603. if (!typeDef->mPartials.IsEmpty())
  3604. {
  3605. for (auto partialDef : typeDef->mPartials)
  3606. {
  3607. if ((partialDef->mTypeDeclaration != NULL) && (partialDef->mTypeDeclaration->mAttributes != NULL))
  3608. _CheckAttributes(partialDef->mTypeDeclaration->mAttributes, typeDef);
  3609. }
  3610. }
  3611. else
  3612. {
  3613. if ((typeDef->mTypeDeclaration != NULL) && (typeDef->mTypeDeclaration->mAttributes != NULL))
  3614. _CheckAttributes(typeDef->mTypeDeclaration->mAttributes, typeDef);
  3615. }
  3616. if (auto typeAliasDeclaration = BfNodeDynCast<BfTypeAliasDeclaration>(typeDef->mTypeDeclaration))
  3617. {
  3618. CheckSymbolReferenceTypeRef(module, typeAliasDeclaration->mAliasToType);
  3619. }
  3620. if (mResolvePassData != NULL)
  3621. {
  3622. if (rebuildTypeInst->IsGenericTypeInstance())
  3623. {
  3624. auto genericTypeInstance = (BfGenericTypeInstance*)rebuildTypeInst;
  3625. for (int genericParamIdx = 0; genericParamIdx < (int)genericTypeInstance->mTypeGenericArguments.size(); genericParamIdx++)
  3626. {
  3627. BfGenericTypeParamInstance genericParamInstance(genericTypeInstance->mTypeDef, genericParamIdx);
  3628. auto genericParamDef = typeDef->mGenericParamDefs[genericParamIdx];
  3629. if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_TypeGenericParam)
  3630. {
  3631. for (auto nameNode : genericParamDef->mNameNodes)
  3632. if (nameNode != NULL)
  3633. mResolvePassData->HandleTypeGenericParam(nameNode, typeDef, genericParamIdx);
  3634. }
  3635. rebuildModule->ResolveGenericParamConstraints(&genericParamInstance, genericTypeInstance->IsGenericTypeInstance());
  3636. }
  3637. }
  3638. }
  3639. if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Type)
  3640. {
  3641. for (auto baseTypeRef : typeDef->mBaseTypes)
  3642. CheckSymbolReferenceTypeRef(module, baseTypeRef);
  3643. }
  3644. BfTypeState typeState;
  3645. SetAndRestoreValue<BfTypeState*> prevTypeState(module->mContext->mCurTypeState, &typeState);
  3646. if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Property)
  3647. {
  3648. for (auto propDef : typeDef->mProperties)
  3649. {
  3650. BfPropertyDef* checkPropDef = propDef;
  3651. BfTypeInstance* checkTypeInst = rebuildTypeInst;
  3652. typeState.mCurTypeDef = propDef->mDeclaringType;
  3653. module->GetBasePropertyDef(checkPropDef, checkTypeInst);
  3654. if (propDef->mFieldDeclaration != NULL)
  3655. mResolvePassData->HandlePropertyReference(propDef->mFieldDeclaration->mNameNode, checkTypeInst->mTypeDef, checkPropDef);
  3656. }
  3657. }
  3658. if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Field)
  3659. {
  3660. for (auto fieldDef : typeDef->mFields)
  3661. {
  3662. if (fieldDef->mFieldDeclaration != NULL)
  3663. {
  3664. typeState.mCurTypeDef = fieldDef->mDeclaringType;
  3665. mResolvePassData->HandleFieldReference(fieldDef->mFieldDeclaration->mNameNode, typeDef, fieldDef);
  3666. }
  3667. }
  3668. }
  3669. for (auto& fieldInst : rebuildTypeInst->mFieldInstances)
  3670. {
  3671. auto fieldDef = fieldInst.GetFieldDef();
  3672. if (fieldDef != NULL)
  3673. {
  3674. typeState.mCurTypeDef = fieldDef->mDeclaringType;
  3675. if (fieldDef->mTypeRef != NULL)
  3676. CheckSymbolReferenceTypeRef(module, fieldDef->mTypeRef);
  3677. if ((fieldDef->mIsConst) && (fieldDef->mInitializer != NULL))
  3678. {
  3679. BfConstResolver constResolver(module);
  3680. constResolver.Resolve(fieldDef->mInitializer);
  3681. }
  3682. if ((fieldDef->mFieldDeclaration != NULL) && (fieldDef->mFieldDeclaration->mAttributes != NULL))
  3683. _CheckAttributes(fieldDef->mFieldDeclaration->mAttributes, fieldDef->mDeclaringType);
  3684. }
  3685. }
  3686. for (auto& propDef : rebuildTypeInst->mTypeDef->mProperties)
  3687. {
  3688. typeState.mCurTypeDef = propDef->mDeclaringType;
  3689. if (propDef->mTypeRef != NULL)
  3690. CheckSymbolReferenceTypeRef(module, propDef->mTypeRef);
  3691. }
  3692. if (rebuildModule == NULL)
  3693. continue;
  3694. rebuildModule->EnsureIRBuilder();
  3695. SetAndRestoreValue<BfTypeInstance*> prevTypeInstance2(rebuildModule->mCurTypeInstance, rebuildTypeInst);
  3696. for (auto& methodInstGroup : rebuildTypeInst->mMethodInstanceGroups)
  3697. {
  3698. // Run through all methods
  3699. bool isDefault = true;
  3700. BfMethodInstanceGroup::MapType::iterator methodItr;
  3701. if (methodInstGroup.mMethodSpecializationMap != NULL)
  3702. methodItr = methodInstGroup.mMethodSpecializationMap->begin();
  3703. while (true)
  3704. {
  3705. BfMethodInstance* rebuildMethodInstance;
  3706. if (isDefault)
  3707. {
  3708. rebuildMethodInstance = methodInstGroup.mDefault;
  3709. if (rebuildMethodInstance == NULL)
  3710. break;
  3711. isDefault = false;
  3712. }
  3713. else
  3714. {
  3715. //TODO: Why did we process specialized methods?
  3716. // This caused renaming of types picking up 'T' usage from generic methods
  3717. break;
  3718. // if (methodInstGroup.mMethodSpecializationMap == NULL)
  3719. // break;
  3720. // if (methodItr == methodInstGroup.mMethodSpecializationMap->end())
  3721. // break;
  3722. // rebuildMethodInstance = methodItr->mValue;
  3723. // ++methodItr;
  3724. }
  3725. if ((rebuildMethodInstance->mIsUnspecializedVariation) || (rebuildMethodInstance->IsSpecializedGenericMethod()))
  3726. continue;
  3727. SetAndRestoreValue<BfMethodInstance*> prevTypeInstance(rebuildModule->mCurMethodInstance, rebuildMethodInstance);
  3728. auto methodDef = rebuildMethodInstance->mMethodDef;
  3729. auto methodDeclaration = methodDef->GetMethodDeclaration();
  3730. typeState.mCurTypeDef = methodDef->mDeclaringType;
  3731. if ((methodDeclaration != NULL) && (methodDeclaration->mAttributes != NULL))
  3732. _CheckAttributes(methodDeclaration->mAttributes, methodDef->mDeclaringType);
  3733. if ((mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Type) ||
  3734. (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_MethodGenericParam) ||
  3735. (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_TypeGenericParam))
  3736. {
  3737. if (methodDef->mExplicitInterface != NULL)
  3738. CheckSymbolReferenceTypeRef(rebuildModule, methodDef->mExplicitInterface);
  3739. for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
  3740. {
  3741. auto param = methodDef->mParams[paramIdx];
  3742. CheckSymbolReferenceTypeRef(rebuildModule, param->mTypeRef);
  3743. }
  3744. if (methodDef->mReturnTypeRef != NULL)
  3745. CheckSymbolReferenceTypeRef(rebuildModule, methodDef->mReturnTypeRef);
  3746. }
  3747. if (rebuildMethodInstance->mIgnoreBody)
  3748. {
  3749. auto methodDeclaration = methodDef->GetMethodDeclaration();
  3750. if (methodDeclaration != NULL)
  3751. mResolvePassData->HandleMethodReference(methodDeclaration->mNameNode, typeDef, methodDef);
  3752. for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
  3753. {
  3754. auto param = methodDef->mParams[paramIdx];
  3755. if (param->mParamDeclaration != NULL)
  3756. {
  3757. if (auto identifierNode = BfNodeDynCast<BfIdentifierNode>(param->mParamDeclaration->mNameNode))
  3758. mResolvePassData->HandleLocalReference(identifierNode, rebuildTypeInst->mTypeDef, rebuildMethodInstance->mMethodDef, paramIdx + 1);
  3759. else if (auto tupleExprNode = BfNodeDynCast<BfTupleExpression>(param->mParamDeclaration->mNameNode))
  3760. {
  3761. for (int fieldIdx = 0; fieldIdx < (int)tupleExprNode->mValues.size(); fieldIdx++)
  3762. {
  3763. if (auto identifierNode = BfNodeDynCast<BfIdentifierNode>(tupleExprNode->mValues[fieldIdx]))
  3764. mResolvePassData->HandleLocalReference(identifierNode, rebuildTypeInst->mTypeDef, rebuildMethodInstance->mMethodDef, paramIdx + 1);
  3765. }
  3766. }
  3767. }
  3768. }
  3769. }
  3770. else
  3771. {
  3772. for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
  3773. {
  3774. auto param = methodDef->mParams[paramIdx];
  3775. if ((param->mParamDeclaration != NULL) && (param->mParamDeclaration->mInitializer != NULL))
  3776. {
  3777. auto paramType = rebuildMethodInstance->GetParamType(paramIdx);
  3778. BfConstResolver constResolver(rebuildModule);
  3779. constResolver.Resolve(param->mParamDeclaration->mInitializer, paramType);
  3780. }
  3781. }
  3782. if (rebuildMethodInstance->mHasBeenProcessed)
  3783. {
  3784. if (rebuildMethodInstance->mIRFunction)
  3785. rebuildModule->mBfIRBuilder->Func_DeleteBody(rebuildMethodInstance->mIRFunction);
  3786. rebuildMethodInstance->mHasBeenProcessed = false;
  3787. rebuildModule->mIncompleteMethodCount++;
  3788. }
  3789. else
  3790. {
  3791. if ((rebuildModule->mIncompleteMethodCount == 0) && (!rebuildModule->mIsScratchModule))
  3792. {
  3793. BF_FATAL("Shouldn't be processing this method");
  3794. }
  3795. }
  3796. for (int genericParamIdx = 0; genericParamIdx < (int)rebuildMethodInstance->GetNumGenericArguments(); genericParamIdx++)
  3797. {
  3798. BfGenericMethodParamInstance genericParamInstance(rebuildMethodInstance->mMethodDef, genericParamIdx);
  3799. auto genericParamDef = methodDef->mGenericParams[genericParamIdx];
  3800. if (mResolvePassData != NULL)
  3801. {
  3802. for (auto nameNode : genericParamDef->mNameNodes)
  3803. if (nameNode != NULL)
  3804. mResolvePassData->HandleMethodGenericParam(nameNode, typeDef, methodDef, genericParamIdx);
  3805. }
  3806. rebuildModule->ResolveGenericParamConstraints(&genericParamInstance, rebuildMethodInstance->mIsUnspecialized);
  3807. }
  3808. for (int externConstraintIdx = 0; externConstraintIdx < (int)methodDef->mExternalConstraints.size(); externConstraintIdx++)
  3809. {
  3810. BfGenericMethodParamInstance genericParamInstance(rebuildMethodInstance->mMethodDef, externConstraintIdx + (int)methodDef->mGenericParams.size());
  3811. auto& externConstraintDef = methodDef->mExternalConstraints[externConstraintIdx];
  3812. CheckSymbolReferenceTypeRef(module, externConstraintDef.mTypeRef);
  3813. rebuildModule->ResolveGenericParamConstraints(&genericParamInstance, rebuildMethodInstance->mIsUnspecialized);
  3814. }
  3815. rebuildModule->ProcessMethod(rebuildMethodInstance);
  3816. }
  3817. }
  3818. }
  3819. }
  3820. }
  3821. void BfCompiler::UpdateCompletion()
  3822. {
  3823. if (mIsResolveOnly)
  3824. return;
  3825. float typeScale = 10.0f;
  3826. float methodScale = 1.0f;
  3827. float queueModuleScale = 50.0f;
  3828. float genModuleScale = 50.0f;
  3829. mCodeGen.UpdateStats();
  3830. BF_ASSERT(mCodeGen.mQueuedCount >= mCodeGen.mCompletionCount);
  3831. BF_ASSERT(mStats.mModulesFinished <= mStats.mModulesStarted);
  3832. BF_ASSERT(mCodeGen.mCompletionCount <= mStats.mModulesStarted);
  3833. float numerator = ((mStats.mQueuedTypesProcessed * typeScale) + //(mStats.mMethodsProcessed * methodScale) +
  3834. (mStats.mModulesFinished * queueModuleScale) + (mCodeGen.mCompletionCount * genModuleScale));
  3835. float divisor = ((mStats.mTypesQueued * typeScale) + //(mStats.mMethodsQueued * methodScale) +
  3836. (mStats.mModulesStarted * queueModuleScale) + (mStats.mReifiedModuleCount * genModuleScale));
  3837. float checkPct = 0;
  3838. if (divisor > 0)
  3839. {
  3840. checkPct = numerator / divisor;
  3841. BF_ASSERT(checkPct >= 0);
  3842. if (checkPct > mCompletionPct)
  3843. mCompletionPct = checkPct;
  3844. }
  3845. else
  3846. mCompletionPct = 0;
  3847. if (!mHadCancel)
  3848. BF_ASSERT(mCompletionPct <= 1.0f);
  3849. if (mCompletionPct > 1.0f)
  3850. mCompletionPct = 1.0f;
  3851. }
  3852. void BfCompiler::MarkStringPool(BfModule* module)
  3853. {
  3854. for (int stringId : module->mStringPoolRefs)
  3855. {
  3856. BfStringPoolEntry& stringPoolEntry = module->mContext->mStringObjectIdMap[stringId];
  3857. stringPoolEntry.mLastUsedRevision = mRevision;
  3858. }
  3859. /*if (module->mOptModule != NULL)
  3860. MarkStringPool(module->mOptModule);*/
  3861. auto altModule = module->mNextAltModule;
  3862. while (altModule != NULL)
  3863. {
  3864. MarkStringPool(altModule);
  3865. altModule = altModule->mNextAltModule;
  3866. }
  3867. for (auto& specModulePair : module->mSpecializedMethodModules)
  3868. MarkStringPool(specModulePair.mValue);
  3869. }
  3870. void BfCompiler::ClearUnusedStringPoolEntries()
  3871. {
  3872. BF_ASSERT(!IsHotCompile());
  3873. for (auto module : mContext->mModules)
  3874. {
  3875. MarkStringPool(module);
  3876. }
  3877. for (auto itr = mContext->mStringObjectIdMap.begin(); itr != mContext->mStringObjectIdMap.end(); )
  3878. {
  3879. int strId = itr->mKey;
  3880. BfStringPoolEntry& stringPoolEntry = itr->mValue;
  3881. if (stringPoolEntry.mLastUsedRevision != mRevision)
  3882. {
  3883. CompileLog("Clearing unused string: %d %s\n", itr->mKey, stringPoolEntry.mString.c_str());
  3884. mContext->mStringObjectPool.Remove(stringPoolEntry.mString);
  3885. itr = mContext->mStringObjectIdMap.Remove(itr);
  3886. }
  3887. else
  3888. ++itr;
  3889. }
  3890. }
  3891. void BfCompiler::ClearBuildCache()
  3892. {
  3893. mCodeGen.ClearBuildCache();
  3894. for (auto project : mSystem->mProjects)
  3895. {
  3896. String libPath = mOutputDirectory + "/" + project->mName + "/" + project->mName + "__.lib";
  3897. BfpFile_Delete(libPath.c_str(), NULL);
  3898. }
  3899. }
  3900. int BfCompiler::GetDynCastVDataCount()
  3901. {
  3902. int dynElements = 1 + mMaxInterfaceSlots;
  3903. return ((dynElements * 4) + mSystem->mPtrSize - 1) / mSystem->mPtrSize;
  3904. }
  3905. bool BfCompiler::IsAutocomplete()
  3906. {
  3907. return (mResolvePassData != NULL) && (mResolvePassData->mAutoComplete != NULL);
  3908. }
  3909. BfAutoComplete* BfCompiler::GetAutoComplete()
  3910. {
  3911. if (mResolvePassData != NULL)
  3912. return mResolvePassData->mAutoComplete;
  3913. return NULL;
  3914. }
  3915. bool BfCompiler::IsHotCompile()
  3916. {
  3917. return mOptions.mHotProject != NULL;
  3918. }
  3919. bool BfCompiler::IsSkippingExtraResolveChecks()
  3920. {
  3921. return mIsResolveOnly && !mOptions.mExtraResolveChecks;
  3922. }
  3923. int BfCompiler::GetVTableMethodOffset()
  3924. {
  3925. if (mOptions.mHasVDataExtender)
  3926. return 1;
  3927. return 0;
  3928. }
  3929. bool BfCompiler::DoWorkLoop(bool onlyReifiedTypes, bool onlyReifiedMethods)
  3930. {
  3931. bool hadAnyWork = false;
  3932. while (true)
  3933. {
  3934. bool didWork = false;
  3935. didWork |= mContext->ProcessWorkList(onlyReifiedTypes, onlyReifiedMethods);
  3936. if (!didWork)
  3937. break;
  3938. hadAnyWork = true;
  3939. }
  3940. return hadAnyWork;
  3941. }
  3942. BfMangler::MangleKind BfCompiler::GetMangleKind()
  3943. {
  3944. if (mOptions.mToolsetType == BfToolsetType_GNU)
  3945. return BfMangler::MangleKind_GNU;
  3946. return (mSystem->mPtrSize == 8) ? BfMangler::MangleKind_Microsoft_64 : BfMangler::MangleKind_Microsoft_32;
  3947. }
  3948. //////////////////////////////////////////////////////////////////////////
  3949. int ArrTest()
  3950. {
  3951. //SizedArray<int, 8> intArr;
  3952. //Array<int> intArr;
  3953. //std::vector<int> intArr;
  3954. BfSizedVector<int, 8> intArr;
  3955. //int val = intArr.GetLastSafe();
  3956. intArr.push_back(123);
  3957. intArr.pop_back();
  3958. intArr.push_back(234);
  3959. intArr.push_back(345);
  3960. //intArr.push_back(567);
  3961. //auto itr = std::find(intArr.begin(), intArr.end(), 234);
  3962. //intArr.erase(itr);
  3963. for (auto itr = intArr.begin(); itr != intArr.end(); )
  3964. {
  3965. if (*itr == 234)
  3966. itr = intArr.erase(itr);
  3967. else
  3968. itr++;
  3969. }
  3970. return (int)intArr.size();
  3971. //intArr.RemoveAt(2);
  3972. }
  3973. //////////////////////////////////////////////////////////////////////////
  3974. void BfCompiler::PopulateReified()
  3975. {
  3976. BP_ZONE("PopulateReified");
  3977. BfContext* context = mContext;
  3978. bool hasTests = mSystem->HasTestProjects();
  3979. Array<BfMethodInstance*> impChainHeadMethods;
  3980. // Types can pull in new dependencies, so fully populate types until they stop
  3981. bool reifiedOnly = mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude;
  3982. while (true)
  3983. {
  3984. BP_ZONE("Compile_PopulateTypes");
  3985. int startTypeInitCount = mTypeInitCount;
  3986. bool didWork = false;
  3987. BfLogSysM("PopulateReified iteration start\n");
  3988. int typeCount = 0;
  3989. for (auto type : context->mResolvedTypes)
  3990. {
  3991. auto module = type->GetModule();
  3992. typeCount++;
  3993. if (module == NULL)
  3994. continue;
  3995. if (!type->IsReified())
  3996. {
  3997. // On compiles, only handle reified types in this loop. This fixes cases where our first instance of a dependent type
  3998. // is found to be unreified and then we have to reify it later. It's not an error, just a compile perf issue
  3999. continue;
  4000. }
  4001. // We have to not populate generic type instances because that may force us to populate a type that SHOULD be deleted
  4002. if ((type->IsIncomplete()) && (type->IsTypeInstance()) && (!type->IsGenericTypeInstance()))
  4003. {
  4004. mSystem->CheckLockYield();
  4005. module->PopulateType(type, BfPopulateType_Full);
  4006. }
  4007. auto typeInst = type->ToTypeInstance();
  4008. if ((typeInst != NULL) && (typeInst->IsGenericTypeInstance()) && (!typeInst->IsUnspecializedType()))
  4009. {
  4010. auto unspecializedType = module->GetUnspecializedTypeInstance(typeInst);
  4011. if (!unspecializedType->mIsReified)
  4012. unspecializedType->mIsReified = true;
  4013. }
  4014. // Check reifications forced by virtuals or interfaces
  4015. if ((!mIsResolveOnly) && (typeInst != NULL) && (typeInst->mIsReified) && (typeInst->IsObject()) && (!typeInst->IsUnspecializedType())
  4016. && (typeInst->mHasBeenInstantiated) && (!typeInst->IsIncomplete()))
  4017. {
  4018. // If we have chained methods, make sure we implement the chain members if the chain head is implemented and reified
  4019. if (typeInst->mTypeDef->mIsCombinedPartial)
  4020. {
  4021. bool hasUnimpChainMembers = false;
  4022. impChainHeadMethods.Clear();
  4023. for (auto& methodInstanceGroup : typeInst->mMethodInstanceGroups)
  4024. {
  4025. auto methodInstance = methodInstanceGroup.mDefault;
  4026. if (methodInstance == NULL)
  4027. continue;
  4028. if (methodInstance->mChainType == BfMethodChainType_ChainHead)
  4029. {
  4030. if (methodInstance->IsReifiedAndImplemented())
  4031. impChainHeadMethods.Add(methodInstance);
  4032. }
  4033. else if (methodInstance->mChainType == BfMethodChainType_ChainMember)
  4034. {
  4035. if (!methodInstance->IsReifiedAndImplemented())
  4036. hasUnimpChainMembers = true;
  4037. }
  4038. else if ((methodInstance->mChainType == BfMethodChainType_None) && (methodInstance->mMethodDef->IsDefaultCtor()))
  4039. {
  4040. if (!methodInstance->IsReifiedAndImplemented())
  4041. hasUnimpChainMembers = true;
  4042. }
  4043. }
  4044. if ((hasUnimpChainMembers) && (!impChainHeadMethods.IsEmpty()))
  4045. {
  4046. for (auto& methodInstanceGroup : typeInst->mMethodInstanceGroups)
  4047. {
  4048. auto methodInstance = methodInstanceGroup.mDefault;
  4049. if (methodInstance == NULL)
  4050. continue;
  4051. bool forceMethod = false;
  4052. if (methodInstance->mChainType == BfMethodChainType_ChainMember)
  4053. {
  4054. if (!methodInstance->IsReifiedAndImplemented())
  4055. {
  4056. for (auto impMethodInstance : impChainHeadMethods)
  4057. {
  4058. if (typeInst->mModule->CompareMethodSignatures(methodInstance, impMethodInstance))
  4059. {
  4060. forceMethod = true;
  4061. }
  4062. }
  4063. }
  4064. }
  4065. else if (methodInstance->mMethodDef->IsDefaultCtor())
  4066. {
  4067. if (!methodInstance->IsReifiedAndImplemented())
  4068. forceMethod = true;
  4069. }
  4070. if (forceMethod)
  4071. {
  4072. typeInst->mModule->GetMethodInstance(methodInstance->GetOwner(), methodInstance->mMethodDef, BfTypeVector(),
  4073. (BfGetMethodInstanceFlags)(BfGetMethodInstanceFlag_UnspecializedPass));
  4074. }
  4075. }
  4076. }
  4077. }
  4078. // If we have any virtual methods overrides that are unreified but the declaring virtual method is reified then we also need to reify
  4079. for (auto&& vEntry : typeInst->mVirtualMethodTable)
  4080. {
  4081. if ((vEntry.mDeclaringMethod.mTypeInstance == NULL) ||
  4082. (vEntry.mDeclaringMethod.mTypeInstance->IsIncomplete()) ||
  4083. (vEntry.mImplementingMethod.mTypeInstance == NULL) ||
  4084. (vEntry.mImplementingMethod.mTypeInstance->IsIncomplete()))
  4085. continue;
  4086. BfMethodInstance* declaringMethod = vEntry.mDeclaringMethod;
  4087. if (declaringMethod == NULL)
  4088. continue;
  4089. if ((declaringMethod->mIsReified) && (declaringMethod->mMethodInstanceGroup->IsImplemented()))
  4090. {
  4091. BfMethodInstance* implMethod = vEntry.mImplementingMethod;
  4092. if ((implMethod != NULL) && ((!implMethod->mMethodInstanceGroup->IsImplemented()) || (!implMethod->mIsReified)))
  4093. {
  4094. didWork = true;
  4095. typeInst->mModule->GetMethodInstance(implMethod);
  4096. }
  4097. }
  4098. }
  4099. for (auto& ifaceTypeInst : typeInst->mInterfaces)
  4100. {
  4101. auto ifaceInst = ifaceTypeInst.mInterfaceType;
  4102. int startIdx = ifaceTypeInst.mStartInterfaceTableIdx;
  4103. int iMethodCount = (int)ifaceInst->mMethodInstanceGroups.size();
  4104. auto declTypeDef = ifaceTypeInst.mDeclaringType;
  4105. for (int iMethodIdx = 0; iMethodIdx < iMethodCount; iMethodIdx++)
  4106. {
  4107. auto ifaceMethodInst = ifaceInst->mMethodInstanceGroups[iMethodIdx].mDefault;
  4108. if ((ifaceMethodInst == NULL) || (!ifaceMethodInst->IsReifiedAndImplemented()))
  4109. continue;
  4110. auto implMethodRef = &typeInst->mInterfaceMethodTable[iMethodIdx + startIdx].mMethodRef;
  4111. BfMethodInstance* implMethod = *implMethodRef;
  4112. if (implMethod == NULL)
  4113. continue;
  4114. if (!implMethod->IsReifiedAndImplemented())
  4115. {
  4116. didWork = true;
  4117. typeInst->mModule->GetMethodInstance(implMethod);
  4118. }
  4119. }
  4120. }
  4121. }
  4122. }
  4123. BfLogSysM("PopulateReified iteration done\n");
  4124. didWork |= DoWorkLoop(reifiedOnly, reifiedOnly);
  4125. if (reifiedOnly)
  4126. didWork |= DoWorkLoop(false, reifiedOnly);
  4127. if (startTypeInitCount != mTypeInitCount)
  4128. didWork = true;
  4129. if (didWork)
  4130. continue;
  4131. // We get everything on the first pass through
  4132. if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
  4133. break;
  4134. if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_SkipUnused)
  4135. break;
  4136. if (startTypeInitCount == mTypeInitCount)
  4137. break;
  4138. }
  4139. }
  4140. void BfCompiler::HotCommit()
  4141. {
  4142. if (mHotState == NULL)
  4143. return;
  4144. mHotState->mCommittedHotCompileIdx = mOptions.mHotCompileIdx;
  4145. for (auto type : mContext->mResolvedTypes)
  4146. {
  4147. auto typeInst = type->ToTypeInstance();
  4148. if (typeInst == NULL)
  4149. continue;
  4150. if (typeInst->mHotTypeData == NULL)
  4151. continue;
  4152. for (int typeIdx = (int)typeInst->mHotTypeData->mTypeVersions.size() - 1; typeIdx >= 0; typeIdx--)
  4153. {
  4154. auto hotVersion = typeInst->mHotTypeData->mTypeVersions[typeIdx];
  4155. if (hotVersion->mCommittedHotCompileIdx != -1)
  4156. break;
  4157. hotVersion->mCommittedHotCompileIdx = mHotState->mCommittedHotCompileIdx;
  4158. if ((!hotVersion->mInterfaceMapping.IsEmpty()) && (typeIdx > 0))
  4159. {
  4160. auto hotVersionHead = typeInst->mHotTypeData->GetLatestVersionHead();
  4161. if ((hotVersionHead != hotVersion) && (hotVersionHead->mDataHash == hotVersion->mDataHash))
  4162. {
  4163. // When we have a slot failure, the data hash will match but we actually do need to use the new mInterfaceMapping entries
  4164. // So we copy them over to the
  4165. hotVersionHead->mInterfaceMapping = hotVersion->mInterfaceMapping;
  4166. }
  4167. }
  4168. }
  4169. }
  4170. }
  4171. void BfCompiler::HotResolve_Start(HotResolveFlags flags)
  4172. {
  4173. BfLogSysM("BfCompiler::HotResolve_Start\n");
  4174. delete mHotResolveData;
  4175. mHotResolveData = new HotResolveData();
  4176. mHotResolveData->mFlags = flags;
  4177. mHotResolveData->mHotTypeIdFlags.Resize(mCurTypeId);
  4178. mHotResolveData->mReasons.Resize(mCurTypeId);
  4179. if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
  4180. {
  4181. HotResolve_AddReachableMethod("BfCallAllStaticDtors");
  4182. for (auto& kv : mHotData->mFuncPtrs)
  4183. {
  4184. auto funcRef = kv.mValue;
  4185. HotResolve_AddReachableMethod(funcRef->mMethod, HotTypeFlag_FuncPtr, true);
  4186. }
  4187. }
  4188. }
  4189. bool BfCompiler::HotResolve_AddReachableMethod(BfHotMethod* hotMethod, HotTypeFlags flags, bool devirtualized, bool forceProcess)
  4190. {
  4191. HotReachableData* hotReachableData;
  4192. if (mHotResolveData->mReachableMethods.TryAdd(hotMethod, NULL, &hotReachableData))
  4193. {
  4194. hotMethod->mRefCount++;
  4195. }
  4196. else
  4197. {
  4198. hotReachableData->mTypeFlags = (HotTypeFlags)(hotReachableData->mTypeFlags | flags);
  4199. if ((!devirtualized) && (!hotReachableData->mHadNonDevirtualizedCall))
  4200. {
  4201. hotReachableData->mHadNonDevirtualizedCall = true;
  4202. if (!forceProcess)
  4203. return true;
  4204. }
  4205. if (!forceProcess)
  4206. return false;
  4207. }
  4208. hotReachableData->mTypeFlags = (HotTypeFlags)(hotReachableData->mTypeFlags | flags);
  4209. if (!devirtualized)
  4210. hotReachableData->mHadNonDevirtualizedCall = true;
  4211. for (auto hotDepData : hotMethod->mReferences)
  4212. {
  4213. if (hotDepData->mDataKind == BfHotDepDataKind_ThisType)
  4214. {
  4215. auto hotThisType = (BfHotThisType*)hotDepData;
  4216. auto hotTypeVersion = hotThisType->mTypeVersion;
  4217. HotTypeFlags hotTypeFlags = mHotResolveData->mHotTypeIdFlags[hotTypeVersion->mTypeId];
  4218. bool isAllocated = (hotTypeFlags & (HotTypeFlag_Heap | HotTypeFlag_CanAllocate)) != 0;
  4219. if (!isAllocated)
  4220. {
  4221. if (mHotResolveData->mDeferredThisCheckMethods.Add(hotMethod))
  4222. {
  4223. return true;
  4224. }
  4225. else
  4226. {
  4227. return false;
  4228. }
  4229. }
  4230. else
  4231. {
  4232. mHotResolveData->mDeferredThisCheckMethods.Remove(hotMethod);
  4233. }
  4234. }
  4235. else if (hotDepData->mDataKind == BfHotDepDataKind_Allocation)
  4236. {
  4237. auto hotAllocation = (BfHotAllocation*)hotDepData;
  4238. auto hotTypeVersion = hotAllocation->mTypeVersion;
  4239. HotResolve_ReportType(hotTypeVersion, flags, hotMethod);
  4240. HotResolve_ReportType(hotTypeVersion, HotTypeFlag_CanAllocate, hotMethod);
  4241. }
  4242. else if (hotDepData->mDataKind == BfHotDepDataKind_TypeVersion)
  4243. {
  4244. auto hotTypeVersion = (BfHotTypeVersion*)hotDepData;
  4245. HotResolve_ReportType(hotTypeVersion, flags, hotMethod);
  4246. }
  4247. else if (hotDepData->mDataKind == BfHotDepDataKind_Method)
  4248. {
  4249. auto checkMethod = (BfHotMethod*)hotDepData;
  4250. HotResolve_AddReachableMethod(checkMethod, flags, false);
  4251. }
  4252. else if (hotDepData->mDataKind == BfHotDepDataKind_DevirtualizedMethod)
  4253. {
  4254. auto checkMethod = (BfHotDevirtualizedMethod*)hotDepData;
  4255. HotResolve_AddReachableMethod(checkMethod->mMethod, flags, true);
  4256. }
  4257. else if (hotDepData->mDataKind == BfHotDepDataKind_DupMethod)
  4258. {
  4259. auto checkMethod = (BfHotDupMethod*)hotDepData;
  4260. HotResolve_AddReachableMethod(checkMethod->mMethod, flags, true);
  4261. }
  4262. }
  4263. return true;
  4264. }
  4265. void BfCompiler::HotResolve_AddReachableMethod(const StringImpl& methodName)
  4266. {
  4267. BfLogSysM("HotResolve_AddReachableMethod %s\n", methodName.c_str());
  4268. String mangledName = methodName;
  4269. BfHotMethod** hotMethodPtr;
  4270. if (!mHotData->mMethodMap.TryGetValue(mangledName, &hotMethodPtr))
  4271. {
  4272. BfLogSysM("Hot method not found\n");
  4273. return;
  4274. }
  4275. BfHotMethod* hotMethod = *hotMethodPtr;
  4276. while (hotMethod->mPrevVersion != NULL)
  4277. {
  4278. if (hotMethod->mSrcTypeVersion->mCommittedHotCompileIdx != -1)
  4279. break;
  4280. hotMethod = hotMethod->mPrevVersion;
  4281. }
  4282. HotResolve_AddReachableMethod(hotMethod, HotTypeFlag_ActiveFunction, true);
  4283. }
  4284. void BfCompiler::HotResolve_AddActiveMethod(BfHotMethod* hotMethod)
  4285. {
  4286. if (mHotResolveData->mActiveMethods.Add(hotMethod))
  4287. {
  4288. hotMethod->mRefCount++;
  4289. }
  4290. // We don't need to mark reachable methods unless we had data changes
  4291. if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
  4292. {
  4293. HotResolve_AddReachableMethod(hotMethod, HotTypeFlag_ActiveFunction, true);
  4294. }
  4295. if ((hotMethod->mFlags & BfHotDepDataFlag_HasDup) != 0)
  4296. {
  4297. for (auto depData : hotMethod->mReferences)
  4298. {
  4299. if (depData->mDataKind != BfHotDepDataKind_DupMethod)
  4300. continue;
  4301. auto hotDupMethod = (BfHotDupMethod*)depData;
  4302. HotResolve_AddActiveMethod(hotDupMethod->mMethod);
  4303. }
  4304. }
  4305. }
  4306. void BfCompiler::HotResolve_AddActiveMethod(const StringImpl& methodName)
  4307. {
  4308. BfLogSysM("HotResolve_AddActiveMethod %s\n", methodName.c_str());
  4309. String mangledName;
  4310. int hotCompileIdx = 0;
  4311. int tabIdx = (int)methodName.IndexOf('\t');
  4312. if (tabIdx != -1)
  4313. {
  4314. mangledName = methodName.Substring(0, tabIdx);
  4315. hotCompileIdx = atoi(methodName.c_str() + tabIdx + 1);
  4316. }
  4317. else
  4318. mangledName = methodName;
  4319. bool isDelegateRef = false;
  4320. BfHotMethod** hotMethodPtr;
  4321. if (!mHotData->mMethodMap.TryGetValue(mangledName, &hotMethodPtr))
  4322. {
  4323. BfLogSysM("Hot method not found\n");
  4324. return;
  4325. }
  4326. BfHotMethod* hotMethod = *hotMethodPtr;
  4327. while (hotMethod->mPrevVersion != NULL)
  4328. {
  4329. if ((hotMethod->mSrcTypeVersion->mCommittedHotCompileIdx != -1) && (hotCompileIdx < hotMethod->mSrcTypeVersion->mCommittedHotCompileIdx))
  4330. break;
  4331. hotMethod = hotMethod->mPrevVersion;
  4332. }
  4333. HotResolve_AddActiveMethod(hotMethod);
  4334. }
  4335. void BfCompiler::HotResolve_AddDelegateMethod(const StringImpl& methodName)
  4336. {
  4337. BfLogSysM("HotResolve_HotResolve_AddDelegateMethod %s\n", methodName.c_str());
  4338. String mangledName = methodName;
  4339. BfHotMethod** hotMethodPtr;
  4340. if (!mHotData->mMethodMap.TryGetValue(mangledName, &hotMethodPtr))
  4341. {
  4342. BfLogSysM("Hot method not found\n");
  4343. return;
  4344. }
  4345. BfHotMethod* hotMethod = *hotMethodPtr;
  4346. HotResolve_AddReachableMethod(hotMethod, HotTypeFlag_Delegate, true);
  4347. }
  4348. void BfCompiler::HotResolve_ReportType(BfHotTypeVersion* hotTypeVersion, HotTypeFlags flags, BfHotDepData* reason)
  4349. {
  4350. auto& flagsRef = mHotResolveData->mHotTypeFlags[hotTypeVersion];
  4351. if (flagsRef == (flagsRef | flags))
  4352. return;
  4353. flagsRef = (HotTypeFlags)(flags | flagsRef);
  4354. bool applyFlags = true;
  4355. if ((flags & (BfCompiler::HotTypeFlag_ActiveFunction | BfCompiler::HotTypeFlag_Delegate | BfCompiler::HotTypeFlag_FuncPtr)) != 0)
  4356. {
  4357. applyFlags = (hotTypeVersion->mCommittedHotCompileIdx != -1) && (mHotState->mPendingDataChanges.Contains(hotTypeVersion->mTypeId));
  4358. if ((!applyFlags) && (hotTypeVersion->mCommittedHotCompileIdx != -1))
  4359. applyFlags = mHotState->mPendingFailedSlottings.Contains(hotTypeVersion->mTypeId);
  4360. if (applyFlags)
  4361. {
  4362. if (reason != NULL)
  4363. mHotResolveData->mReasons[hotTypeVersion->mTypeId] = reason;
  4364. }
  4365. }
  4366. if (applyFlags)
  4367. {
  4368. auto& flagsIdRef = mHotResolveData->mHotTypeIdFlags[hotTypeVersion->mTypeId];
  4369. flagsIdRef = (HotTypeFlags)(flags | flagsIdRef);
  4370. }
  4371. BfLogSysM("HotResolve_ReportType %p %s Flags:%X DeclHotIdx:%d\n", hotTypeVersion, mContext->TypeIdToString(hotTypeVersion->mTypeId).c_str(), flags, hotTypeVersion->mDeclHotCompileIdx);
  4372. for (auto member : hotTypeVersion->mMembers)
  4373. {
  4374. HotResolve_ReportType(member, flags, reason);
  4375. }
  4376. }
  4377. void BfCompiler::HotResolve_ReportType(int typeId, HotTypeFlags flags)
  4378. {
  4379. if ((uint)typeId >= mHotResolveData->mHotTypeIdFlags.size())
  4380. {
  4381. BF_DBG_FATAL("Invalid typeId");
  4382. return;
  4383. }
  4384. if (mHotResolveData->mHotTypeIdFlags[typeId] == (mHotResolveData->mHotTypeIdFlags[typeId] | flags))
  4385. return;
  4386. auto hotTypeData = mContext->GetHotTypeData(typeId);
  4387. if (hotTypeData != NULL)
  4388. {
  4389. auto hotTypeVersion = hotTypeData->GetTypeVersion(mHotState->mCommittedHotCompileIdx);
  4390. BF_ASSERT(hotTypeVersion != NULL);
  4391. if (hotTypeVersion != NULL)
  4392. HotResolve_ReportType(hotTypeVersion, flags, NULL);
  4393. }
  4394. mHotResolveData->mHotTypeIdFlags[typeId] = (HotTypeFlags)(flags | mHotResolveData->mHotTypeIdFlags[typeId]);
  4395. }
  4396. String BfCompiler::HotResolve_Finish()
  4397. {
  4398. BfLogSysM("HotResolve_Finish\n");
  4399. if (mHotState == NULL)
  4400. {
  4401. // It's possible we did a HotCompile with no file changes and therefore didn't actually do a compile
  4402. return "";
  4403. }
  4404. String result;
  4405. if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
  4406. {
  4407. BF_ASSERT(!mHotState->mPendingDataChanges.IsEmpty() || !mHotState->mPendingFailedSlottings.IsEmpty());
  4408. }
  4409. else
  4410. {
  4411. BF_ASSERT(mHotState->mPendingDataChanges.IsEmpty() && mHotState->mPendingFailedSlottings.IsEmpty());
  4412. }
  4413. if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
  4414. {
  4415. auto _AddUsedType = [&](BfTypeDef* typeDef)
  4416. {
  4417. auto type = mContext->mUnreifiedModule->ResolveTypeDef(mReflectTypeInstanceTypeDef);
  4418. if (type != NULL)
  4419. HotResolve_ReportType(type->mTypeId, BfCompiler::HotTypeFlag_Heap);
  4420. };
  4421. // We have some types that can be allocated in a read-only section- pretend they are on the heap
  4422. _AddUsedType(mReflectTypeInstanceTypeDef);
  4423. _AddUsedType(mStringTypeDef);
  4424. // Find any virtual method overrides that may have been called.
  4425. // These can cause new reachable virtual methods to be called, which may take more than one iteration to fully resolve
  4426. for (int methodPass = 0; true; methodPass++)
  4427. {
  4428. bool didWork = false;
  4429. for (auto hotMethod : mHotResolveData->mDeferredThisCheckMethods)
  4430. {
  4431. if (HotResolve_AddReachableMethod(hotMethod, BfCompiler::HotTypeFlag_ActiveFunction, true, true))
  4432. didWork = true;
  4433. }
  4434. HotTypeFlags typeFlags = HotTypeFlag_None;
  4435. for (auto& kv : mHotData->mMethodMap)
  4436. {
  4437. String& methodName = kv.mKey;
  4438. auto hotMethod = kv.mValue;
  4439. bool doCall = false;
  4440. bool forceAdd = false;
  4441. if (mHotResolveData->mReachableMethods.ContainsKey(hotMethod))
  4442. continue;
  4443. for (auto ref : hotMethod->mReferences)
  4444. {
  4445. if (ref->mDataKind == BfHotDepDataKind_ThisType)
  4446. continue;
  4447. if (ref->mDataKind != BfHotDepDataKind_VirtualDecl)
  4448. break;
  4449. auto hotVirtualDecl = (BfHotVirtualDeclaration*)ref;
  4450. HotReachableData* hotReachableData;
  4451. if (mHotResolveData->mReachableMethods.TryGetValue(hotVirtualDecl->mMethod, &hotReachableData))
  4452. {
  4453. if (hotReachableData->mHadNonDevirtualizedCall)
  4454. {
  4455. typeFlags = hotReachableData->mTypeFlags;
  4456. doCall = true;
  4457. }
  4458. }
  4459. }
  4460. if (!doCall)
  4461. {
  4462. if ((hotMethod->mFlags & BfHotDepDataFlag_AlwaysCalled) != 0)
  4463. {
  4464. typeFlags = BfCompiler::HotTypeFlag_ActiveFunction;
  4465. doCall = true;
  4466. }
  4467. }
  4468. if (doCall)
  4469. {
  4470. if (HotResolve_AddReachableMethod(hotMethod, typeFlags, true, forceAdd))
  4471. didWork = true;
  4472. }
  4473. }
  4474. if (!didWork)
  4475. break;
  4476. }
  4477. int errorCount = 0;
  4478. for (int typeId = 0; typeId < (int)mHotResolveData->mHotTypeIdFlags.size(); typeId++)
  4479. {
  4480. auto flags = mHotResolveData->mHotTypeIdFlags[typeId];
  4481. if (flags == 0)
  4482. continue;
  4483. auto type = mContext->mTypes[typeId];
  4484. Dictionary<BfHotMethod*, String*> methodNameMap;
  4485. if ((flags > BfCompiler::HotTypeFlag_UserNotUsed) &&
  4486. ((mHotState->mPendingDataChanges.Contains(typeId)) || (mHotState->mPendingFailedSlottings.Contains(typeId))))
  4487. {
  4488. bool isBadTypeUsed = false;
  4489. if ((flags & HotTypeFlag_Heap) != 0)
  4490. isBadTypeUsed = true;
  4491. else if ((flags & (HotTypeFlag_ActiveFunction | HotTypeFlag_Delegate | HotTypeFlag_FuncPtr)) != 0)
  4492. {
  4493. // If we detect an old version being used, it's only an issue if this type can actually be allocated
  4494. if ((flags & HotTypeFlag_CanAllocate) != 0)
  4495. {
  4496. isBadTypeUsed = true;
  4497. }
  4498. }
  4499. if (isBadTypeUsed)
  4500. {
  4501. bool reasonIsActiveMethod = false;
  4502. String methodReason;
  4503. auto reason = mHotResolveData->mReasons[typeId];
  4504. if ((reason != NULL) && (reason->mDataKind == BfHotDepDataKind_Method))
  4505. {
  4506. auto hotMethod = (BfHotMethod*)reason;
  4507. reasonIsActiveMethod = mHotResolveData->mActiveMethods.Contains(hotMethod);
  4508. if (methodNameMap.IsEmpty())
  4509. {
  4510. for (auto& kv : mHotData->mMethodMap)
  4511. {
  4512. auto hotMethod = kv.mValue;
  4513. while (hotMethod != NULL)
  4514. {
  4515. methodNameMap[hotMethod] = &kv.mKey;
  4516. hotMethod = hotMethod->mPrevVersion;
  4517. }
  4518. }
  4519. }
  4520. String** strPtr;
  4521. if (methodNameMap.TryGetValue(hotMethod, &strPtr))
  4522. {
  4523. methodReason += BfDemangler::Demangle((**strPtr), DbgLanguage_Beef, BfDemangler::Flag_BeefFixed);
  4524. }
  4525. }
  4526. errorCount++;
  4527. if (errorCount >= 1000)
  4528. {
  4529. result += "\n (more errors)...";
  4530. break;
  4531. }
  4532. if (!result.IsEmpty())
  4533. result += "\n";
  4534. result += "'";
  4535. result += mContext->TypeIdToString(typeId);
  4536. result += "'";
  4537. if ((flags & BfCompiler::HotTypeFlag_Heap) != 0)
  4538. result += " allocated on the heap";
  4539. else if ((flags & BfCompiler::HotTypeFlag_ActiveFunction) != 0)
  4540. {
  4541. if (reasonIsActiveMethod)
  4542. result += StrFormat(" used by active method '%s'", methodReason.c_str());
  4543. else if (!methodReason.IsEmpty())
  4544. result += StrFormat(" previous data version used by deleted method '%s', reachable by an active method", methodReason.c_str());
  4545. else
  4546. result += " previous data version used by a deleted method reachable by an active method";
  4547. }
  4548. else if ((flags & BfCompiler::HotTypeFlag_Delegate) != 0)
  4549. {
  4550. if (!methodReason.IsEmpty())
  4551. result += StrFormat(" previous data version used by deleted method '%s', reachable by a delegate", methodReason.c_str());
  4552. else
  4553. result += " previous data version used by a deleted method reachable by a delegate";
  4554. }
  4555. else if ((flags & BfCompiler::HotTypeFlag_FuncPtr) != 0)
  4556. {
  4557. if (!methodReason.IsEmpty())
  4558. result += StrFormat(" previous data version used by deleted method '%s', reachable by a function pointer", methodReason.c_str());
  4559. else
  4560. result += " previous data version used by a deleted method reachable by a function pointer";
  4561. }
  4562. else if ((flags & BfCompiler::HotTypeFlag_UserUsed) != 0)
  4563. result += " stated as used by the program";
  4564. }
  4565. }
  4566. String typeName = mContext->TypeIdToString(typeId);
  4567. BfLogSysM(" %d %s %02X\n", typeId, typeName.c_str(), flags);
  4568. }
  4569. if (result.IsEmpty())
  4570. {
  4571. for (auto typeId : mHotState->mPendingDataChanges)
  4572. {
  4573. auto type = mContext->mTypes[typeId];
  4574. auto typeInstance = type->ToTypeInstance();
  4575. BF_ASSERT(typeInstance->mHotTypeData->mPendingDataChange);
  4576. typeInstance->mHotTypeData->mPendingDataChange = false;
  4577. typeInstance->mHotTypeData->mHadDataChange = true;
  4578. typeInstance->mHotTypeData->mVTableOrigLength = -1;
  4579. typeInstance->mHotTypeData->mOrigInterfaceMethodsLength = -1;
  4580. BfLogSysM("Pending data change applied to type %p\n", typeInstance);
  4581. }
  4582. mHotState->mPendingDataChanges.Clear();
  4583. mHotState->mPendingFailedSlottings.Clear();
  4584. }
  4585. }
  4586. ClearOldHotData();
  4587. if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
  4588. {
  4589. for (int pass = 0; pass < 2; pass++)
  4590. {
  4591. bool wantsReachable = pass == 0;
  4592. Array<String> methodList;
  4593. for (auto& kv : mHotData->mMethodMap)
  4594. {
  4595. auto hotMethod = kv.mValue;
  4596. bool reachable = mHotResolveData->mReachableMethods.ContainsKey(hotMethod);
  4597. if (reachable != wantsReachable)
  4598. continue;
  4599. String methodName;
  4600. methodName += BfDemangler::Demangle(kv.mKey, DbgLanguage_Beef, BfDemangler::Flag_BeefFixed);
  4601. methodName += " - ";
  4602. methodName += kv.mKey;
  4603. methodList.Add(methodName);
  4604. }
  4605. methodList.Sort([](const String& lhs, const String& rhs) { return lhs < rhs; });
  4606. for (auto& methodName : methodList)
  4607. BfLogSysM("%s: %s\n", wantsReachable ? "Reachable" : "Unreachable", methodName.c_str());
  4608. }
  4609. }
  4610. delete mHotResolveData;
  4611. mHotResolveData = NULL;
  4612. return result;
  4613. }
  4614. void BfCompiler::ClearOldHotData()
  4615. {
  4616. if (mHotData == NULL)
  4617. return;
  4618. // TODO: Get rid of old hot data during hot compiles, too
  4619. // if (IsHotCompile())
  4620. // return;
  4621. BP_ZONE("BfCompiler::ClearOldHotData");
  4622. bool isHotCompile = IsHotCompile();
  4623. auto itr = mHotData->mMethodMap.begin();
  4624. while (itr != mHotData->mMethodMap.end())
  4625. {
  4626. String& methodName = itr->mKey;
  4627. auto hotMethod = itr->mValue;
  4628. bool doDelete = false;
  4629. // If a previous version of a method is not currently active then it should be impossible to ever reach it
  4630. while (hotMethod->mPrevVersion != NULL)
  4631. {
  4632. auto prevMethod = hotMethod->mPrevVersion;
  4633. if (prevMethod->mRefCount > 1)
  4634. {
  4635. BF_ASSERT((mHotResolveData != NULL) && (mHotResolveData->mActiveMethods.Contains(prevMethod)));
  4636. break;
  4637. }
  4638. hotMethod->mPrevVersion = prevMethod->mPrevVersion;
  4639. prevMethod->mPrevVersion = NULL;
  4640. prevMethod->Deref();
  4641. }
  4642. BF_ASSERT(hotMethod->mRefCount >= 1);
  4643. if (hotMethod->mPrevVersion == NULL)
  4644. {
  4645. if (hotMethod->mRefCount <= 1)
  4646. {
  4647. doDelete = true;
  4648. }
  4649. else if ((!isHotCompile) && ((hotMethod->mFlags & (BfHotDepDataFlag_IsBound | BfHotDepDataFlag_RetainMethodWithoutBinding)) == 0))
  4650. {
  4651. doDelete = true;
  4652. }
  4653. }
  4654. bool doRemove = doDelete;
  4655. if ((hotMethod->mFlags & BfHotDepDataFlag_HasDup) != 0)
  4656. {
  4657. bool hasDupMethod = false;
  4658. for (int idx = 0; idx < (int)hotMethod->mReferences.size(); idx++)
  4659. {
  4660. auto depData = hotMethod->mReferences[idx];
  4661. if (depData->mDataKind == BfHotDepDataKind_DupMethod)
  4662. {
  4663. auto dupMethod = (BfHotDupMethod*)depData;
  4664. if (doDelete)
  4665. {
  4666. doRemove = false;
  4667. dupMethod->mMethod->mRefCount++;
  4668. itr->mValue = dupMethod->mMethod;
  4669. }
  4670. else
  4671. {
  4672. if ((dupMethod->mMethod->mRefCount == 1) ||
  4673. ((!IsHotCompile()) && (dupMethod->mMethod->mFlags & BfHotDepDataFlag_IsBound) == 0))
  4674. {
  4675. dupMethod->Deref();
  4676. hotMethod->mReferences.RemoveAt(idx);
  4677. idx--;
  4678. }
  4679. }
  4680. }
  4681. }
  4682. }
  4683. if (doDelete)
  4684. {
  4685. BfLogSysM("Deleting hot method %p %s\n", hotMethod, methodName.c_str());
  4686. //BF_ASSERT(hotMethod->mRefCount == 1);
  4687. hotMethod->Clear();
  4688. hotMethod->Deref();
  4689. if (doRemove)
  4690. itr = mHotData->mMethodMap.Remove(itr);
  4691. }
  4692. else
  4693. ++itr;
  4694. }
  4695. mHotData->ClearUnused(IsHotCompile());
  4696. for (auto type : mContext->mResolvedTypes)
  4697. {
  4698. auto typeInst = type->ToTypeInstance();
  4699. if (typeInst == NULL)
  4700. continue;
  4701. if (typeInst->mHotTypeData == NULL)
  4702. continue;
  4703. bool foundCommittedVersion = false;
  4704. auto latestVersionHead = typeInst->mHotTypeData->GetLatestVersionHead();
  4705. for (int typeIdx = (int)typeInst->mHotTypeData->mTypeVersions.size() - 1; typeIdx >= 0; typeIdx--)
  4706. {
  4707. auto hotVersion = typeInst->mHotTypeData->mTypeVersions[typeIdx];
  4708. if (hotVersion == latestVersionHead)
  4709. {
  4710. // We have to keep the latest version head -- otherwise we would lose vdata and interface mapping data
  4711. continue;
  4712. }
  4713. if ((!foundCommittedVersion) && (mHotState != NULL) && (hotVersion->mDeclHotCompileIdx <= mHotState->mCommittedHotCompileIdx))
  4714. {
  4715. // Don't remove the latest committed version
  4716. foundCommittedVersion = true;
  4717. }
  4718. else if (hotVersion->mRefCount == 1)
  4719. {
  4720. typeInst->mHotTypeData->mTypeVersions.RemoveAt(typeIdx);
  4721. hotVersion->Deref();
  4722. BF_ASSERT(typeInst->mHotTypeData->mTypeVersions.size() > 0);
  4723. }
  4724. }
  4725. }
  4726. }
  4727. void BfCompiler::CompileReified()
  4728. {
  4729. BP_ZONE("Compile_ResolveTypeDefs");
  4730. for (auto typeDef : mSystem->mTypeDefs)
  4731. {
  4732. mSystem->CheckLockYield();
  4733. if (mCanceling)
  4734. {
  4735. BfLogSysM("Canceling from Compile typeDef loop\n");
  4736. break;
  4737. }
  4738. if (typeDef->mProject->mDisabled)
  4739. continue;
  4740. if (typeDef->mIsPartial)
  4741. continue;
  4742. bool isAlwaysInclude = (typeDef->mIsAlwaysInclude) || (typeDef->mProject->mAlwaysIncludeAll);
  4743. if (typeDef->mProject->IsTestProject())
  4744. {
  4745. for (auto methodDef : typeDef->mMethods)
  4746. {
  4747. auto methodDeclaration = methodDef->GetMethodDeclaration();
  4748. if ((methodDeclaration != NULL) && (methodDeclaration->mAttributes != NULL) &&
  4749. (methodDeclaration->mAttributes->Contains("Test")))
  4750. isAlwaysInclude = true;
  4751. }
  4752. }
  4753. //TODO: Just because the type is required doesn't mean we want to reify it. Why did we have that check?
  4754. if ((mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude) && (!isAlwaysInclude))
  4755. continue;
  4756. auto scratchModule = mContext->mScratchModule;
  4757. scratchModule->ResolveTypeDef(typeDef, BfPopulateType_Full);
  4758. }
  4759. if (mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude)
  4760. {
  4761. for (auto project : mSystem->mProjects)
  4762. {
  4763. String entryClassName = project->mStartupObject;
  4764. auto typeDef = mSystem->FindTypeDef(entryClassName, 0, project);
  4765. if (typeDef != NULL)
  4766. {
  4767. typeDef->mIsAlwaysInclude = true;
  4768. auto resolvedType = mContext->mScratchModule->ResolveTypeDef(typeDef);
  4769. if (resolvedType != NULL)
  4770. {
  4771. auto resolvedTypeInst = resolvedType->ToTypeInstance();
  4772. if (resolvedTypeInst != NULL)
  4773. {
  4774. auto module = resolvedTypeInst->GetModule();
  4775. if (!module->mIsReified)
  4776. module->ReifyModule();
  4777. mContext->mScratchModule->PopulateType(resolvedType, BfPopulateType_Full);
  4778. BfMemberSetEntry* memberSetEntry;
  4779. if (resolvedTypeInst->mTypeDef->mMethodSet.TryGetWith(String("Main"), &memberSetEntry))
  4780. {
  4781. BfMethodDef* methodDef = (BfMethodDef*)memberSetEntry->mMemberDef;
  4782. while (methodDef != NULL)
  4783. {
  4784. auto moduleMethodInstance = mContext->mScratchModule->GetMethodInstanceAtIdx(resolvedTypeInst, methodDef->mIdx);
  4785. auto methodInstance = moduleMethodInstance.mMethodInstance;
  4786. if (methodInstance->GetParamCount() != 0)
  4787. {
  4788. mContext->mScratchModule->GetInternalMethod("CreateParamsArray");
  4789. mContext->mScratchModule->GetInternalMethod("DeleteStringArray");
  4790. }
  4791. methodDef = methodDef->mNextWithSameName;
  4792. }
  4793. }
  4794. }
  4795. }
  4796. }
  4797. }
  4798. }
  4799. PopulateReified();
  4800. }
  4801. bool BfCompiler::DoCompile(const StringImpl& outputDirectory)
  4802. {
  4803. BP_ZONE("BfCompiler::Compile");
  4804. if (!mOptions.mErrorString.IsEmpty())
  4805. {
  4806. mPassInstance->Fail(mOptions.mErrorString);
  4807. return false;
  4808. }
  4809. {
  4810. String hotSwapErrors;
  4811. String toolsetErrors;
  4812. for (auto project : mSystem->mProjects)
  4813. {
  4814. if (project->mDisabled)
  4815. continue;
  4816. if (project->mCodeGenOptions.mLTOType != BfLTOType_None)
  4817. {
  4818. if (mOptions.mAllowHotSwapping)
  4819. {
  4820. if (!hotSwapErrors.IsEmpty())
  4821. hotSwapErrors += ", ";
  4822. hotSwapErrors += project->mName;
  4823. }
  4824. if (mOptions.mToolsetType != BfToolsetType_LLVM)
  4825. {
  4826. if (!toolsetErrors.IsEmpty())
  4827. toolsetErrors += ", ";
  4828. toolsetErrors += project->mName;
  4829. }
  4830. }
  4831. }
  4832. if (!hotSwapErrors.IsEmpty())
  4833. 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()));
  4834. if (!toolsetErrors.IsEmpty())
  4835. mPassInstance->Fail(StrFormat("The Workspace Toolset must be set to 'LLVM' in order to use LTO in '%s'. Consider changing 'Workspace/General/Toolset' to 'LLVM'.", toolsetErrors.c_str()));
  4836. }
  4837. //
  4838. {
  4839. String attribName;
  4840. mAttributeTypeOptionMap.Clear();
  4841. for (int typeOptionsIdx = 0; typeOptionsIdx < (int)mSystem->mTypeOptions.size(); typeOptionsIdx++)
  4842. {
  4843. auto& typeOptions = mSystem->mTypeOptions[typeOptionsIdx];
  4844. for (auto& attributeFilter : typeOptions.mAttributeFilters)
  4845. {
  4846. attribName = attributeFilter;
  4847. attribName += "Attribute";
  4848. Array<int>* arrPtr = NULL;
  4849. mAttributeTypeOptionMap.TryAdd(attribName, NULL, &arrPtr);
  4850. arrPtr->Add(typeOptionsIdx);
  4851. }
  4852. }
  4853. }
  4854. // Inc revision for next run through Compile
  4855. mRevision++;
  4856. BfLogSysM("Compile Start. Revision: %d\n", mRevision);
  4857. if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
  4858. mContext->mUnreifiedModule->mIsReified = true;
  4859. else
  4860. mContext->mUnreifiedModule->mIsReified = false;
  4861. if (mOptions.mAllowHotSwapping)
  4862. {
  4863. if (mHotData == NULL)
  4864. {
  4865. mHotData = new HotData();
  4866. mHotData->mCompiler = this;
  4867. }
  4868. }
  4869. else
  4870. {
  4871. delete mHotData;
  4872. mHotData = NULL;
  4873. }
  4874. if (IsHotCompile())
  4875. {
  4876. if (!mOptions.mAllowHotSwapping)
  4877. {
  4878. mPassInstance->Fail("Hot Compilation is not enabled");
  4879. return true;
  4880. }
  4881. if (mHotState == NULL)
  4882. {
  4883. mHotState = new HotState();
  4884. mHotState->mHotProject = mOptions.mHotProject;
  4885. }
  4886. else
  4887. {
  4888. // It should be impossible to switch hot projects without a non-hot compile between them
  4889. BF_ASSERT(mHotState->mHotProject == mOptions.mHotProject);
  4890. }
  4891. }
  4892. else
  4893. {
  4894. for (auto& kv : mContext->mSavedTypeDataMap)
  4895. {
  4896. auto savedTypeData = kv.mValue;
  4897. delete savedTypeData->mHotTypeData;
  4898. savedTypeData->mHotTypeData = NULL;
  4899. }
  4900. delete mHotState;
  4901. mHotState = NULL;
  4902. // This will get rid of any old method data so we don't have any more mPrevVersions
  4903. ClearOldHotData();
  4904. }
  4905. int prevUnfinishedModules = mStats.mModulesStarted - mStats.mModulesFinished;
  4906. mCompletionPct = 0;
  4907. memset(&mStats, 0, sizeof(mStats));
  4908. mCodeGen.ClearResults();
  4909. mCodeGen.ResetStats();
  4910. mStats.mModulesStarted = prevUnfinishedModules;
  4911. if ((mLastRevisionAborted) && (!mIsResolveOnly))
  4912. {
  4913. auto _AddCount = [&](BfModule* module)
  4914. {
  4915. if (module->mAddedToCount)
  4916. {
  4917. if (module->mIsReified)
  4918. mStats.mReifiedModuleCount++;
  4919. }
  4920. };
  4921. for (auto mainModule : mContext->mModules)
  4922. {
  4923. _AddCount(mainModule);
  4924. for (auto specKV : mainModule->mSpecializedMethodModules)
  4925. {
  4926. _AddCount(specKV.mValue);
  4927. }
  4928. }
  4929. }
  4930. if (IsHotCompile())
  4931. {
  4932. mContext->EnsureHotMangledVirtualMethodNames();
  4933. }
  4934. mOutputDirectory = outputDirectory;
  4935. mSystem->StartYieldSection();
  4936. mCanceling = false;
  4937. mSystem->CheckLockYield();
  4938. #ifdef WANT_COMPILE_LOG
  4939. if (!mIsResolveOnly)
  4940. {
  4941. mCompileLogFP = fopen(StrFormat("compile%d.txt", mRevision).c_str(), "wb");
  4942. }
  4943. #endif
  4944. BfTypeDef* typeDef;
  4945. BfLogSysM("UpdateRevisedTypes Revision %d. ResolvePass:%d CursorIdx:%d\n", mRevision, mIsResolveOnly,
  4946. ((mResolvePassData == NULL) || (mResolvePassData->mParser == NULL)) ? - 1 : mResolvePassData->mParser->mCursorIdx);
  4947. mCompileState = CompileState_Normal;
  4948. UpdateRevisedTypes();
  4949. // We need to defer processing the graveyard until here, because mLookupResults contain atom references so we need to make sure
  4950. // those aren't deleted until we can properly handle it.
  4951. mSystem->ProcessAtomGraveyard();
  4952. BpEnter("Compile_Start");
  4953. bool hasRequiredTypes = true;
  4954. //HashSet<BfTypeDef*> internalTypeDefs;
  4955. auto _GetRequiredType = [&](const StringImpl& typeName, int genericArgCount = 0)
  4956. {
  4957. auto typeDef = mSystem->FindTypeDef(typeName, genericArgCount);
  4958. if (typeDef == NULL)
  4959. {
  4960. mPassInstance->Fail(StrFormat("Unable to find system type: %s", typeName.c_str()));
  4961. hasRequiredTypes = false;
  4962. }
  4963. return typeDef;
  4964. };
  4965. mArray1TypeDef = _GetRequiredType("System.Array1");
  4966. mArray2TypeDef = _GetRequiredType("System.Array2");
  4967. mArray3TypeDef = _GetRequiredType("System.Array3");
  4968. mArray4TypeDef = _GetRequiredType("System.Array4");
  4969. mSpanTypeDef = _GetRequiredType("System.Span", 1);
  4970. mAttributeTypeDef = _GetRequiredType("System.Attribute");
  4971. mAttributeUsageAttributeTypeDef = _GetRequiredType("System.AttributeUsageAttribute");
  4972. mBfObjectTypeDef = _GetRequiredType("System.Object");
  4973. mClassVDataTypeDef = _GetRequiredType("System.ClassVData");
  4974. mCLinkAttributeTypeDef = _GetRequiredType("System.CLinkAttribute");
  4975. mCReprAttributeTypeDef = _GetRequiredType("System.CReprAttribute");
  4976. mAlignAttributeTypeDef = _GetRequiredType("System.AlignAttribute");
  4977. mNoDiscardAttributeTypeDef = _GetRequiredType("System.NoDiscardAttribute");
  4978. mDisableObjectAccessChecksAttributeTypeDef = _GetRequiredType("System.DisableObjectAccessChecksAttribute");
  4979. mDbgRawAllocDataTypeDef = _GetRequiredType("System.DbgRawAllocData");
  4980. mDeferredCallTypeDef = _GetRequiredType("System.DeferredCall");
  4981. mDelegateTypeDef = _GetRequiredType("System.Delegate");
  4982. mEnumTypeDef = _GetRequiredType("System.Enum");
  4983. mFriendAttributeTypeDef = _GetRequiredType("System.FriendAttribute");
  4984. mCheckedAttributeTypeDef = _GetRequiredType("System.CheckedAttribute");
  4985. mUncheckedAttributeTypeDef = _GetRequiredType("System.UncheckedAttribute");
  4986. mFunctionTypeDef = _GetRequiredType("System.Function");
  4987. mGCTypeDef = _GetRequiredType("System.GC");
  4988. mGenericIEnumerableTypeDef = _GetRequiredType("System.Collections.Generic.IEnumerable");
  4989. mGenericIEnumeratorTypeDef = _GetRequiredType("System.Collections.Generic.IEnumerator");
  4990. mGenericIRefEnumeratorTypeDef = _GetRequiredType("System.Collections.Generic.IRefEnumerator");
  4991. mInlineAttributeTypeDef = _GetRequiredType("System.InlineAttribute");
  4992. mInternalTypeDef = _GetRequiredType("System.Internal");
  4993. mIPrintableTypeDef = _GetRequiredType("System.IPrintable");
  4994. mIHashableTypeDef = _GetRequiredType("System.IHashable");
  4995. mLinkNameAttributeTypeDef = _GetRequiredType("System.LinkNameAttribute");
  4996. mMethodRefTypeDef = _GetRequiredType("System.MethodReference", 1);
  4997. mNullableTypeDef = _GetRequiredType("System.Nullable");
  4998. mOrderedAttributeTypeDef = _GetRequiredType("System.OrderedAttribute");
  4999. mPointerTTypeDef = _GetRequiredType("System.Pointer", 1);
  5000. mPointerTypeDef = _GetRequiredType("System.Pointer", 0);
  5001. mReflectArrayType = _GetRequiredType("System.Reflection.ArrayType");
  5002. mReflectFieldDataDef = _GetRequiredType("System.Reflection.TypeInstance.FieldData");
  5003. mReflectFieldSplatDataDef = _GetRequiredType("System.Reflection.TypeInstance.FieldSplatData");
  5004. mReflectMethodDataDef = _GetRequiredType("System.Reflection.TypeInstance.MethodData");
  5005. mReflectParamDataDef = _GetRequiredType("System.Reflection.TypeInstance.ParamData");
  5006. mReflectPointerType = _GetRequiredType("System.Reflection.PointerType");
  5007. mReflectSizedArrayType = _GetRequiredType("System.Reflection.SizedArrayType");
  5008. mReflectSpecializedGenericType = _GetRequiredType("System.Reflection.SpecializedGenericType");
  5009. mReflectTypeInstanceTypeDef = _GetRequiredType("System.Reflection.TypeInstance");
  5010. mReflectUnspecializedGenericType = _GetRequiredType("System.Reflection.UnspecializedGenericType");
  5011. mSizedArrayTypeDef = _GetRequiredType("System.SizedArray", 2);
  5012. mSkipAccessCheckAttributeTypeDef = _GetRequiredType("System.SkipAccessCheckAttribute");
  5013. mStaticInitAfterAttributeTypeDef = _GetRequiredType("System.StaticInitAfterAttribute");
  5014. mStaticInitPriorityAttributeTypeDef = _GetRequiredType("System.StaticInitPriorityAttribute");
  5015. mStringTypeDef = _GetRequiredType("System.String");
  5016. mTestAttributeTypeDef = _GetRequiredType("System.TestAttribute");
  5017. mThreadStaticAttributeTypeDef = _GetRequiredType("System.ThreadStaticAttribute");
  5018. mTypeTypeDef = _GetRequiredType("System.Type");
  5019. mUnboundAttributeTypeDef = _GetRequiredType("System.UnboundAttribute");
  5020. mValueTypeTypeDef = _GetRequiredType("System.ValueType");
  5021. mObsoleteAttributeTypeDef = _GetRequiredType("System.ObsoleteAttribute");
  5022. mErrorAttributeTypeDef = _GetRequiredType("System.ErrorAttribute");
  5023. mWarnAttributeTypeDef = _GetRequiredType("System.WarnAttribute");
  5024. for (int i = 0; i < BfTypeCode_Length; i++)
  5025. mContext->mPrimitiveStructTypes[i] = NULL;
  5026. if (!hasRequiredTypes)
  5027. {
  5028. // Force rebuilding
  5029. mInInvalidState = true;
  5030. mOptions.mForceRebuildIdx++;
  5031. return true;
  5032. }
  5033. mSystem->CheckLockYield();
  5034. VisitSourceExteriorNodes();
  5035. //BF_ASSERT(hasRequiredTypes);
  5036. if (!mIsResolveOnly)
  5037. {
  5038. HashSet<BfModule*> foundVDataModuleSet;
  5039. for (auto bfProject : mSystem->mProjects)
  5040. {
  5041. if (bfProject->mDisabled)
  5042. continue;
  5043. if ((mBfObjectTypeDef != NULL) && (!bfProject->ContainsReference(mBfObjectTypeDef->mProject)))
  5044. {
  5045. mPassInstance->Fail(StrFormat("Project '%s' must reference core library '%s'", bfProject->mName.c_str(), mBfObjectTypeDef->mProject->mName.c_str()));
  5046. }
  5047. if ((bfProject->mTargetType != BfTargetType_BeefConsoleApplication) && (bfProject->mTargetType != BfTargetType_BeefWindowsApplication) &&
  5048. (bfProject->mTargetType != BfTargetType_BeefDynLib) &&
  5049. (bfProject->mTargetType != BfTargetType_C_ConsoleApplication) && (bfProject->mTargetType != BfTargetType_C_WindowsApplication) &&
  5050. (bfProject->mTargetType != BfTargetType_BeefTest) &&
  5051. (bfProject->mTargetType != BfTargetType_BeefApplication_StaticLib) && (bfProject->mTargetType != BfTargetType_BeefApplication_DynamicLib))
  5052. continue;
  5053. if (bfProject->mTargetType == BfTargetType_BeefTest)
  5054. {
  5055. // Force internal test methods
  5056. auto bfModule = mContext->mScratchModule;
  5057. bfModule->GetInternalMethod("Test_Init");
  5058. bfModule->GetInternalMethod("Test_Query");
  5059. bfModule->GetInternalMethod("Test_Finish");
  5060. }
  5061. bool found = false;
  5062. for (auto module : mVDataModules)
  5063. {
  5064. if (module->mProject == bfProject)
  5065. {
  5066. found = true;
  5067. foundVDataModuleSet.Add(module);
  5068. //module->StartNewRevision();
  5069. }
  5070. }
  5071. if (!found)
  5072. {
  5073. auto module = new BfVDataModule(mContext);
  5074. module->mProject = bfProject;
  5075. module->Init();
  5076. module->FinishInit();
  5077. module->mIsSpecialModule = true;
  5078. BF_ASSERT(!mContext->mLockModules);
  5079. mContext->mModules.push_back(module);
  5080. mVDataModules.push_back(module);
  5081. foundVDataModuleSet.Add(module);
  5082. }
  5083. }
  5084. // Remove old vdata
  5085. for (int moduleIdx = 0; moduleIdx < (int) mVDataModules.size(); moduleIdx++)
  5086. {
  5087. auto module = mVDataModules[moduleIdx];
  5088. if (!foundVDataModuleSet.Contains(module))
  5089. {
  5090. delete module;
  5091. mVDataModules.erase(mVDataModules.begin() + moduleIdx);
  5092. moduleIdx--;
  5093. mContext->mModules.Remove(module);
  5094. }
  5095. }
  5096. }
  5097. if (mIsResolveOnly)
  5098. VisitAutocompleteExteriorIdentifiers();
  5099. if (!hasRequiredTypes)
  5100. {
  5101. BfLogSysM("Missing required types\n");
  5102. }
  5103. mStats.mTypesQueued = 0;
  5104. mStats.mMethodsQueued = 0;
  5105. mStats.mTypesQueued += (int)mContext->mPopulateTypeWorkList.size();
  5106. mStats.mMethodsQueued += (int)mContext->mMethodWorkList.size();
  5107. if (hasRequiredTypes)
  5108. {
  5109. mContext->mScratchModule->ResolveTypeDef(mBfObjectTypeDef, BfPopulateType_Full);
  5110. mContext->RemapObject();
  5111. mSystem->CheckLockYield();
  5112. mWantsDeferMethodDecls = mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude;
  5113. CompileReified();
  5114. mWantsDeferMethodDecls = false;
  5115. }
  5116. BpLeave();
  5117. BpEnter("Compile_End");
  5118. {
  5119. BP_ZONE("ProcessingLiveness");
  5120. for (auto type : mContext->mResolvedTypes)
  5121. {
  5122. auto depType = type->ToDependedType();
  5123. if (depType != NULL)
  5124. depType->mRebuildFlags = (BfTypeRebuildFlags)(depType->mRebuildFlags | BfTypeRebuildFlag_AwaitingReference);
  5125. }
  5126. bool didWork = false;
  5127. UpdateDependencyMap(mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_ResolveUnused, didWork);
  5128. if (mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude)
  5129. {
  5130. // If UpdateDependencyMap caused methods to be reified, then we need to run PopulateReified again-
  5131. // because those methods may be virtual and we need to reify overrides (for example).
  5132. // We use the DoWorkLoop result to determine if there were actually any changes from UpdateDependencyMap
  5133. if (didWork)
  5134. {
  5135. PopulateReified();
  5136. }
  5137. }
  5138. }
  5139. if (hasRequiredTypes)
  5140. ProcessPurgatory(true);
  5141. // Mark used modules
  5142. if ((mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude) && (!mCanceling))
  5143. {
  5144. bool hadActualTarget = false;
  5145. if (!mIsResolveOnly)
  5146. {
  5147. SizedArray<BfModule*, 32> requiredModules;
  5148. for (auto typeDef : mSystem->mTypeDefs)
  5149. {
  5150. if ((typeDef->mIsAlwaysInclude) && (!typeDef->mIsPartial))
  5151. {
  5152. auto requiredType = mContext->mScratchModule->ResolveTypeDef(typeDef);
  5153. if (requiredType != NULL)
  5154. {
  5155. auto requiredModule = requiredType->GetModule();
  5156. if (requiredModule != NULL)
  5157. requiredModules.push_back(requiredModule);
  5158. }
  5159. }
  5160. }
  5161. mContext->mReferencedIFaceSlots.Clear();
  5162. bool hasTests = false;
  5163. for (auto project : mSystem->mProjects)
  5164. {
  5165. if (project->mTargetType == BfTargetType_BeefTest)
  5166. hasTests = true;
  5167. project->mUsedModules.Clear();
  5168. project->mReferencedTypeData.Clear();
  5169. if (project->mDisabled)
  5170. continue;
  5171. if (project->mTargetType == BfTargetType_BeefLib)
  5172. continue;
  5173. hadActualTarget = true;
  5174. for (auto requiredModule : requiredModules)
  5175. {
  5176. mContext->MarkUsedModules(project, requiredModule);
  5177. }
  5178. String entryClassName = project->mStartupObject;
  5179. typeDef = mSystem->FindTypeDef(entryClassName, 0, project);
  5180. if (typeDef != NULL)
  5181. {
  5182. auto startupType = mContext->mScratchModule->ResolveTypeDef(typeDef);
  5183. if (startupType != NULL)
  5184. {
  5185. auto startupTypeInst = startupType->ToTypeInstance();
  5186. if (startupTypeInst != NULL)
  5187. {
  5188. mContext->MarkUsedModules(project, startupTypeInst->GetModule());
  5189. }
  5190. }
  5191. }
  5192. if (hasTests)
  5193. {
  5194. for (auto type : mContext->mResolvedTypes)
  5195. {
  5196. auto typeInstance = type->ToTypeInstance();
  5197. if ((typeInstance != NULL) &&
  5198. (typeInstance->mTypeDef->mProject->mTargetType == BfTargetType_BeefTest))
  5199. {
  5200. bool typeHasTest = false;
  5201. for (auto& methodInstanceGroup : typeInstance->mMethodInstanceGroups)
  5202. {
  5203. if (methodInstanceGroup.mDefault != NULL)
  5204. {
  5205. auto methodInstance = methodInstanceGroup.mDefault;
  5206. if ((methodInstance->GetCustomAttributes() != NULL) &&
  5207. (methodInstance->GetCustomAttributes()->Contains(mTestAttributeTypeDef)))
  5208. {
  5209. typeHasTest = true;
  5210. }
  5211. }
  5212. }
  5213. if (typeHasTest)
  5214. mContext->MarkUsedModules(typeInstance->mTypeDef->mProject, typeInstance->mModule);
  5215. }
  5216. }
  5217. }
  5218. }
  5219. // Leave types reified when hot compiling
  5220. if ((!IsHotCompile()) && (hadActualTarget))
  5221. mContext->TryUnreifyModules();
  5222. }
  5223. }
  5224. // Generate slot nums
  5225. if ((!mIsResolveOnly) && (hasRequiredTypes) && (!mCanceling))
  5226. {
  5227. if ((!IsHotCompile()) || (mHotState->mHasNewInterfaceTypes))
  5228. {
  5229. int prevSlotCount = mMaxInterfaceSlots;
  5230. GenerateSlotNums();
  5231. if ((prevSlotCount != -1) && (prevSlotCount != mMaxInterfaceSlots))
  5232. {
  5233. mInterfaceSlotCountChanged = true;
  5234. }
  5235. if (mHotState != NULL)
  5236. mHotState->mHasNewInterfaceTypes = false;
  5237. }
  5238. }
  5239. // Resolve unused types
  5240. if ((mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_ResolveUnused) && (!mCanceling))
  5241. {
  5242. // Finish off any outstanding modules so we can code generate in parallel with handling the unreified stuff
  5243. for (auto module : mContext->mModules)
  5244. {
  5245. if (!module->mIsSpecialModule)
  5246. {
  5247. if ((module->mIsReified) && (module->mIsModuleMutable))
  5248. {
  5249. module->Finish();
  5250. }
  5251. }
  5252. }
  5253. DoWorkLoop();
  5254. BfLogSysM("Compile QueueUnused\n");
  5255. mCompileState = BfCompiler::CompileState_Unreified;
  5256. BpLeave();
  5257. BpEnter("Compile_QueueUnused");
  5258. while (true)
  5259. {
  5260. BP_ZONE("Compile_QueueUnused");
  5261. bool queuedMoreMethods = false;
  5262. int startTypeInitCount = mTypeInitCount;
  5263. for (auto typeDef : mSystem->mTypeDefs)
  5264. {
  5265. mSystem->CheckLockYield();
  5266. if (mCanceling)
  5267. {
  5268. BfLogSysM("Canceling from Compile typeDef loop\n");
  5269. break;
  5270. }
  5271. if (typeDef->mProject->mDisabled)
  5272. continue;
  5273. if (typeDef->mIsPartial)
  5274. continue;
  5275. if (typeDef->mTypeCode == BfTypeCode_Extension)
  5276. continue;
  5277. mContext->mUnreifiedModule->ResolveTypeDef(typeDef, BfPopulateType_Full);
  5278. }
  5279. for (auto type : mContext->mResolvedTypes)
  5280. {
  5281. auto module = type->GetModule();
  5282. if (module == NULL)
  5283. continue;
  5284. if ((type->IsIncomplete()) && (type->IsTypeInstance()) && (!type->IsSpecializedType()))
  5285. {
  5286. mSystem->CheckLockYield();
  5287. module->PopulateType(type, BfPopulateType_Full);
  5288. }
  5289. auto typeInst = type->ToTypeInstance();
  5290. if (typeInst == NULL)
  5291. continue;
  5292. if (typeInst->IsUnspecializedTypeVariation())
  5293. continue;
  5294. if (!typeInst->IsSpecializedType())
  5295. {
  5296. // Find any remaining methods for unreified processing
  5297. for (auto&& methodInstGroup : typeInst->mMethodInstanceGroups)
  5298. {
  5299. if ((methodInstGroup.mOnDemandKind == BfMethodOnDemandKind_Decl_AwaitingReference) ||
  5300. (methodInstGroup.mOnDemandKind == BfMethodOnDemandKind_NoDecl_AwaitingReference))
  5301. {
  5302. queuedMoreMethods = true;
  5303. if ((methodInstGroup.mDefault != NULL) && (methodInstGroup.mDefault->mIsForeignMethodDef))
  5304. {
  5305. mContext->mUnreifiedModule->GetMethodInstance(typeInst, methodInstGroup.mDefault->mMethodDef, BfTypeVector(),
  5306. (BfGetMethodInstanceFlags)(BfGetMethodInstanceFlag_ForeignMethodDef | BfGetMethodInstanceFlag_UnspecializedPass | BfGetMethodInstanceFlag_ExplicitResolveOnlyPass));
  5307. }
  5308. else
  5309. mContext->mUnreifiedModule->GetMethodInstance(typeInst, typeInst->mTypeDef->mMethods[methodInstGroup.mMethodIdx], BfTypeVector(),
  5310. (BfGetMethodInstanceFlags)(BfGetMethodInstanceFlag_UnspecializedPass | BfGetMethodInstanceFlag_ExplicitResolveOnlyPass));
  5311. }
  5312. }
  5313. }
  5314. }
  5315. if ((!queuedMoreMethods) && (startTypeInitCount == mTypeInitCount))
  5316. break;
  5317. DoWorkLoop();
  5318. }
  5319. bool didWork = false;
  5320. UpdateDependencyMap(true, didWork);
  5321. DoWorkLoop();
  5322. mCompileState = BfCompiler::CompileState_Normal;
  5323. }
  5324. else
  5325. {
  5326. DoWorkLoop();
  5327. }
  5328. if (hasRequiredTypes)
  5329. ProcessPurgatory(false);
  5330. // Old Mark used modules
  5331. if ((!mIsResolveOnly) && (hasRequiredTypes))
  5332. {
  5333. // if ((!mPassInstance->HasFailed()) && (!mCanceling))
  5334. // {
  5335. // if ((!IsHotCompile()) || (mHotState->mHasNewInterfaceTypes))
  5336. // {
  5337. // GenerateSlotNums();
  5338. // if (mHotState != NULL)
  5339. // mHotState->mHasNewInterfaceTypes = false;
  5340. // }
  5341. // }
  5342. if ((!mPassInstance->HasFailed()) && (!mCanceling))
  5343. {
  5344. if (!mOptions.mAllowHotSwapping)
  5345. {
  5346. GenerateDynCastData();
  5347. mContext->ProcessWorkList(false, false);
  5348. }
  5349. mCompileState = BfCompiler::CompileState_VData;
  5350. for (auto vdataModule : mVDataModules)
  5351. CreateVData(vdataModule);
  5352. for (auto vdataModule : mVDataModules)
  5353. FixVDataHash(vdataModule);
  5354. mCompileState = BfCompiler::CompileState_Normal;
  5355. }
  5356. // Don't clear out unused string pool entries while we are hot swapping, because we want string literals
  5357. // to still be the same pointer if it's erased and then put back
  5358. if ((!IsHotCompile()) && (!mCanceling))
  5359. ClearUnusedStringPoolEntries();
  5360. mContext->ValidateDependencies();
  5361. mContext->UpdateAfterDeletingTypes();
  5362. }
  5363. // We need to check the specialized errors before writing out modules --
  5364. // this call is responsible for deleting dead method specializations that contained errors, or for setting
  5365. // the mHadBuildErrors on the module if there was a method specialization error that didn't die
  5366. mContext->CheckSpecializedErrorData();
  5367. mContext->Finish();
  5368. if ((!mIsResolveOnly) && (!IsHotCompile()))
  5369. ClearOldHotData();
  5370. mPassInstance->TryFlushDeferredError();
  5371. BpLeave();
  5372. BpEnter("Compile_Finish");
  5373. //TODO:!!
  5374. //mCanceling = true;
  5375. String moduleListStr;
  5376. int numModulesWritten = 0;
  5377. if ((hasRequiredTypes) && (!mCanceling))
  5378. {
  5379. if (!mIsResolveOnly)
  5380. {
  5381. int idx = 0;
  5382. BF_ASSERT(mContext->mMethodWorkList.IsEmpty());
  5383. //bfContext->mLockModules = true;
  5384. for (int moduleIdx = 0; moduleIdx < (int)mContext->mModules.size(); moduleIdx++)
  5385. {
  5386. //bool clearModule = false;
  5387. auto mainModule = mContext->mModules[moduleIdx];
  5388. BfModule* bfModule = mainModule;
  5389. if (bfModule->mIsReified)
  5390. {
  5391. auto itr = mainModule->mSpecializedMethodModules.begin();
  5392. while (true)
  5393. {
  5394. if (bfModule->mIsModuleMutable)
  5395. {
  5396. //clearModule = true;
  5397. // Note that Finish will just return immediately if we have errors, we don't write out modules with errors
  5398. // The 'mLastModuleWrittenRevision' will not be updated in the case.
  5399. bfModule->Finish();
  5400. mainModule->mRevision = std::max(mainModule->mRevision, bfModule->mRevision);
  5401. }
  5402. if (bfModule->mLastModuleWrittenRevision == mRevision)
  5403. {
  5404. if (!moduleListStr.empty())
  5405. moduleListStr += ", ";
  5406. moduleListStr += bfModule->mModuleName;
  5407. numModulesWritten++;
  5408. }
  5409. if (bfModule->mParentModule != NULL)
  5410. {
  5411. for (auto&& fileName : bfModule->mOutFileNames)
  5412. {
  5413. if (!mainModule->mOutFileNames.Contains(fileName))
  5414. mainModule->mOutFileNames.push_back(fileName);
  5415. }
  5416. }
  5417. if (bfModule->mNextAltModule != NULL)
  5418. {
  5419. bfModule = bfModule->mNextAltModule;
  5420. }
  5421. else
  5422. {
  5423. if (itr == mainModule->mSpecializedMethodModules.end())
  5424. break;
  5425. bfModule = itr->mValue;
  5426. ++itr;
  5427. }
  5428. }
  5429. }
  5430. mainModule->ClearModule();
  5431. }
  5432. //bfContext->mLockModules = false;
  5433. }
  5434. else
  5435. {
  5436. bool isTargeted = (mResolvePassData != NULL) && (mResolvePassData->mParser != NULL);
  5437. if (!isTargeted)
  5438. {
  5439. for (auto bfModule : mContext->mModules)
  5440. {
  5441. if (bfModule->mIsModuleMutable)
  5442. {
  5443. bfModule->Finish();
  5444. bfModule->mRevision = std::max(bfModule->mRevision, bfModule->mRevision);
  5445. bfModule->ClearModuleData();
  5446. }
  5447. }
  5448. }
  5449. }
  5450. }
  5451. /*if (!moduleListStr.empty())
  5452. mPassInstance->OutputLine(StrFormat("%d modules generated: %s", numModulesWritten, moduleListStr.c_str()));*/
  5453. //CompileLog("%d object files written: %s\n", numModulesWritten, moduleListStr.c_str());
  5454. //printf("Compile done, waiting for finish\n");
  5455. while (true)
  5456. {
  5457. if (!hasRequiredTypes)
  5458. break;
  5459. if (mCanceling)
  5460. mCodeGen.Cancel();
  5461. bool isDone = mCodeGen.Finish();
  5462. UpdateCompletion();
  5463. if (isDone)
  5464. break;
  5465. }
  5466. mCodeGen.ProcessErrors(mPassInstance, mCanceling);
  5467. // This has to happen after codegen because we may delete modules that are referenced in codegen
  5468. mContext->Cleanup();
  5469. if ((!IsHotCompile()) && (!mIsResolveOnly) && (!mCanceling))
  5470. {
  5471. // Only save 'saved type data' for temporarily-deleted types like on-demand types.
  5472. // If we don't reuse it within a compilation pass then we put those IDs up to be
  5473. // reused later. We don't do this for hot reloading because there are cases like
  5474. // a user renaming a type that we want to allow him to be able to undo and then
  5475. // hot-recompile successfully.
  5476. for (auto& kv : mContext->mSavedTypeDataMap)
  5477. {
  5478. auto savedTypeData = kv.mValue;
  5479. mTypeIdFreeList.Add(savedTypeData->mTypeId);
  5480. delete savedTypeData;
  5481. }
  5482. mContext->mSavedTypeDataMap.Clear();
  5483. mContext->mSavedTypeData.Clear();
  5484. }
  5485. #ifdef BF_PLATFORM_WINDOWS
  5486. if (!mIsResolveOnly)
  5487. {
  5488. for (auto mainModule : mContext->mModules)
  5489. {
  5490. BfModule* bfModule = mainModule;
  5491. if (bfModule->mIsReified)
  5492. {
  5493. for (auto outFileName : bfModule->mOutFileNames)
  5494. {
  5495. if (outFileName.mModuleWritten)
  5496. BeLibManager::Get()->AddUsedFileName(outFileName.mFileName);
  5497. }
  5498. }
  5499. }
  5500. BeLibManager::Get()->Finish();
  5501. }
  5502. #endif
  5503. int numObjFilesWritten = 0;
  5504. for (auto& fileEntry : mCodeGen.mCodeGenFiles)
  5505. {
  5506. if (!fileEntry.mWasCached)
  5507. numObjFilesWritten++;
  5508. }
  5509. mPassInstance->OutputLine(StrFormat(":low %d module%s built, %d object file%s generated",
  5510. numModulesWritten, (numModulesWritten != 1) ? "s" : "",
  5511. numObjFilesWritten, (numObjFilesWritten != 1) ? "s" : ""));
  5512. BpLeave();
  5513. mPassInstance->WriteErrorSummary();
  5514. if ((mCanceling) && (!mIsResolveOnly))
  5515. {
  5516. mPassInstance->Fail("Build canceled");
  5517. mContext->CancelWorkItems();
  5518. CompileLog("Compile canceled\n");
  5519. }
  5520. BfLogSysM("TypesPopulated:%d MethodsDeclared:%d MethodsProcessed:%d Canceled? %d\n", mStats.mTypesPopulated, mStats.mMethodDeclarations, mStats.mMethodsProcessed, mCanceling);
  5521. UpdateCompletion();
  5522. if ((!mIsResolveOnly) && (!mPassInstance->HasFailed()) && (!mCanceling))
  5523. {
  5524. //BF_ASSERT(mCompletionPct >= 0.99999f);
  5525. }
  5526. if (mCompileLogFP != NULL)
  5527. {
  5528. fclose(mCompileLogFP);
  5529. mCompileLogFP = NULL;
  5530. }
  5531. UpdateCompletion();
  5532. mStats.mTotalTypes = mContext->mResolvedTypes.mCount;
  5533. String compileInfo;
  5534. if (mIsResolveOnly)
  5535. compileInfo += StrFormat("ResolveOnly ResolveType:%d Parser:%d\n", mResolvePassData->mResolveType, mResolvePassData->mParser != NULL);
  5536. compileInfo += StrFormat("TotalTypes:%d\nTypesPopulated:%d\nMethodsDeclared:%d\nMethodsProcessed:%d\nCanceled? %d\n", mStats.mTotalTypes, mStats.mTypesPopulated, mStats.mMethodDeclarations, mStats.mMethodsProcessed, mCanceling);
  5537. compileInfo += StrFormat("TypesPopulated:%d\n", mStats.mTypesPopulated);
  5538. compileInfo += StrFormat("MethodDecls:%d\nMethodsProcessed:%d\nModulesStarted:%d\nModulesFinished:%d\n", mStats.mMethodDeclarations, mStats.mMethodsProcessed, mStats.mModulesFinished);
  5539. BpEvent("CompileDone", compileInfo.c_str());
  5540. if (mHotState != NULL)
  5541. {
  5542. for (auto& fileEntry : mCodeGen.mCodeGenFiles)
  5543. {
  5544. if (fileEntry.mWasCached)
  5545. continue;
  5546. mHotState->mQueuedOutFiles.Add(fileEntry);
  5547. }
  5548. if (!mPassInstance->HasFailed())
  5549. {
  5550. // Clear these out when we know we've compiled without error
  5551. mHotState->mNewlySlottedTypeIds.Clear();
  5552. mHotState->mSlotDefineTypeIds.Clear();
  5553. }
  5554. }
  5555. mCompileState = BfCompiler::CompileState_None;
  5556. // extern MemReporter gBEMemReporter;
  5557. // extern int gBEMemReporterSize;
  5558. // gBEMemReporter.Report();
  5559. // int memReporterSize = gBEMemReporterSize;
  5560. mLastRevisionAborted = mCanceling || !hasRequiredTypes;
  5561. bool didCancel = mCanceling && hasRequiredTypes;
  5562. mCanceling = false;
  5563. mContext->ValidateDependencies();
  5564. return !didCancel;
  5565. }
  5566. bool BfCompiler::Compile(const StringImpl& outputDirectory)
  5567. {
  5568. bool success = DoCompile(outputDirectory);
  5569. if (!success)
  5570. return false;
  5571. if (mPassInstance->HasFailed())
  5572. return true;
  5573. if (!mInterfaceSlotCountChanged)
  5574. return true;
  5575. BfLogSysM("Interface slot count increased. Rebuilding relevant modules.\n");
  5576. mPassInstance->OutputLine("Interface slot count increased. Rebuilding relevant modules.");
  5577. // Recompile with the increased slot count
  5578. success = DoCompile(outputDirectory);
  5579. BF_ASSERT(!mInterfaceSlotCountChanged);
  5580. return success;
  5581. }
  5582. void BfCompiler::ClearResults()
  5583. {
  5584. BP_ZONE("BfCompiler::ClearResults");
  5585. mCodeGen.ClearResults();
  5586. }
  5587. // Can should still leave the system in a state such that we when we save as much progress as possible while
  5588. // still leaving the system in a state that the next attempt at compile will resume with a valid state
  5589. // Canceling will still process the pending PopulateType calls but may leave items in the method worklist.
  5590. // Note that Cancel is an async request to cancel
  5591. void BfCompiler::Cancel()
  5592. {
  5593. mCanceling = true;
  5594. mHadCancel = true;
  5595. BfLogSysM("BfCompiler::Cancel\n");
  5596. BpEvent("BfCompiler::Cancel", "");
  5597. }
  5598. //#define WANT_COMPILE_LOG
  5599. void BfCompiler::CompileLog(const char* fmt ...)
  5600. {
  5601. #ifdef WANT_COMPILE_LOG
  5602. if (mCompileLogFP == NULL)
  5603. return;
  5604. //static int lineNum = 0;
  5605. //lineNum++;
  5606. va_list argList;
  5607. va_start(argList, fmt);
  5608. String aResult = vformat(fmt, argList);
  5609. va_end(argList);
  5610. //aResult = StrFormat("%d ", lineNum) + aResult;
  5611. fwrite(aResult.c_str(), 1, aResult.length(), mCompileLogFP);
  5612. #endif
  5613. }
  5614. void BfCompiler::ReportMemory(MemReporter* memReporter)
  5615. {
  5616. AutoCrit crit(mSystem->mDataLock);
  5617. {
  5618. AutoMemReporter autoMemReporter(memReporter, "Context");
  5619. mContext->ReportMemory(memReporter);
  5620. }
  5621. for (auto type : mContext->mResolvedTypes)
  5622. {
  5623. AutoMemReporter autoMemReporter(memReporter, "Types");
  5624. type->ReportMemory(memReporter);
  5625. }
  5626. for (auto module : mContext->mModules)
  5627. {
  5628. AutoMemReporter autoMemReporter(memReporter, "Modules");
  5629. module->ReportMemory(memReporter);
  5630. }
  5631. {
  5632. AutoMemReporter autoMemReporter(memReporter, "ScratchModule");
  5633. mContext->mScratchModule->ReportMemory(memReporter);
  5634. }
  5635. for (auto vdataModule : mVDataModules)
  5636. {
  5637. AutoMemReporter autoMemReporter(memReporter, "VDataModules");
  5638. vdataModule->ReportMemory(memReporter);
  5639. }
  5640. if (mHotData != NULL)
  5641. {
  5642. AutoMemReporter autoMemReporter(memReporter, "HotData");
  5643. memReporter->Add(sizeof(HotData));
  5644. memReporter->AddMap(mHotData->mMethodMap);
  5645. for (auto& kv : mHotData->mMethodMap)
  5646. {
  5647. memReporter->AddStr(kv.mKey);
  5648. memReporter->Add(sizeof(BfHotMethod));
  5649. memReporter->AddVec(kv.mValue->mReferences);
  5650. }
  5651. }
  5652. if (mHotState != NULL)
  5653. {
  5654. AutoMemReporter autoMemReporter(memReporter, "HotState");
  5655. memReporter->Add(sizeof(HotState));
  5656. memReporter->AddVec(mHotState->mQueuedOutFiles, false);
  5657. memReporter->AddHashSet(mHotState->mSlotDefineTypeIds, false);
  5658. memReporter->AddHashSet(mHotState->mPendingDataChanges, false);
  5659. memReporter->AddMap(mHotState->mDeletedTypeNameMap, false);
  5660. for (auto& kv : mHotState->mDeletedTypeNameMap)
  5661. {
  5662. memReporter->AddStr(kv.mKey, false);
  5663. }
  5664. }
  5665. }
  5666. //////////////////////////////////////////////////////////////////////////
  5667. void BfCompiler::GenerateAutocompleteInfo()
  5668. {
  5669. BP_ZONE("BfCompiler::GetAutocompleteInfo");
  5670. String& autoCompleteResultString = *gTLStrReturn.Get();
  5671. autoCompleteResultString.Clear();
  5672. auto _GetDocString = [&](BfCommentNode* commentNode, StringImpl& docString)
  5673. {
  5674. commentNode->ToString(docString);
  5675. for (int i = 0; i < (int)docString.length(); i++)
  5676. {
  5677. char c = docString[i];
  5678. if (c == '\n')
  5679. docString[i] = '\x3';
  5680. }
  5681. };
  5682. auto bfModule = mResolvePassData->mAutoComplete->mModule;
  5683. if (bfModule != NULL)
  5684. {
  5685. auto autoComplete = mResolvePassData->mAutoComplete;
  5686. if (autoComplete->mResolveType == BfResolveType_GetNavigationData)
  5687. return; // Already handled
  5688. if (autoComplete->mResolveType == BfResolveType_GetVarType)
  5689. {
  5690. autoCompleteResultString = autoComplete->mVarTypeName;
  5691. return;
  5692. }
  5693. if (autoComplete->mUncertain)
  5694. autoCompleteResultString += "uncertain\n";
  5695. if (autoComplete->mDefaultSelection.length() != 0)
  5696. autoCompleteResultString += StrFormat("select\t%s\n", autoComplete->mDefaultSelection.c_str());
  5697. auto _EncodeTypeDef = [] (BfTypeDef* typeDef)
  5698. {
  5699. String typeName = typeDef->mProject->mName + ":" + typeDef->mFullName.ToString();
  5700. if (!typeDef->mGenericParamDefs.IsEmpty())
  5701. typeName += StrFormat("`%d", (int)typeDef->mGenericParamDefs.size());
  5702. return typeName;
  5703. };
  5704. if (autoComplete->mResolveType == BfResolveType_GetSymbolInfo)
  5705. {
  5706. if (autoComplete->mDefTypeGenericParamIdx != -1)
  5707. {
  5708. autoCompleteResultString += StrFormat("typeGenericParam\t%d\n", autoComplete->mDefTypeGenericParamIdx);
  5709. autoCompleteResultString += StrFormat("typeRef\t%s\n", _EncodeTypeDef(autoComplete->mDefType).c_str());
  5710. }
  5711. else if (autoComplete->mDefMethodGenericParamIdx != -1)
  5712. {
  5713. autoCompleteResultString += StrFormat("methodGenericParam\t%d\n", autoComplete->mDefMethodGenericParamIdx);
  5714. autoCompleteResultString += StrFormat("methodRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
  5715. }
  5716. else if ((autoComplete->mReplaceLocalId != -1) && (autoComplete->mDefMethod != NULL))
  5717. {
  5718. autoCompleteResultString += StrFormat("localId\t%d\n", autoComplete->mReplaceLocalId);
  5719. autoCompleteResultString += StrFormat("methodRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
  5720. }
  5721. else if (autoComplete->mDefField != NULL)
  5722. {
  5723. autoCompleteResultString += StrFormat("fieldRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefField->mIdx);
  5724. }
  5725. else if (autoComplete->mDefProp != NULL)
  5726. {
  5727. autoCompleteResultString += StrFormat("propertyRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefProp->mIdx);
  5728. }
  5729. else if (autoComplete->mDefMethod != NULL)
  5730. {
  5731. if (autoComplete->mDefMethod->mMethodType == BfMethodType_Ctor)
  5732. autoCompleteResultString += StrFormat("ctorRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
  5733. else
  5734. autoCompleteResultString += StrFormat("methodRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
  5735. }
  5736. else if (autoComplete->mDefType != NULL)
  5737. {
  5738. autoCompleteResultString += StrFormat("typeRef\t%s\n", _EncodeTypeDef(autoComplete->mDefType).c_str());
  5739. }
  5740. if (autoComplete->mInsertEndIdx > 0)
  5741. {
  5742. if (mResolvePassData->mParser->mSrc[autoComplete->mInsertEndIdx - 1] == '!')
  5743. autoComplete->mInsertEndIdx--;
  5744. }
  5745. }
  5746. const char* wantsDocEntry = NULL;
  5747. if (!autoComplete->mDocumentationEntryName.IsEmpty())
  5748. wantsDocEntry = autoComplete->mDocumentationEntryName.c_str();
  5749. if (autoComplete->mInsertStartIdx != -1)
  5750. {
  5751. autoCompleteResultString += StrFormat("insertRange\t%d %d\n", autoComplete->mInsertStartIdx, autoComplete->mInsertEndIdx);
  5752. }
  5753. if ((autoComplete->mDefMethod == NULL) && (autoComplete->mGetDefinitionNode == NULL) && (autoComplete->mIsGetDefinition) && (autoComplete->mMethodMatchInfo != NULL))
  5754. {
  5755. // Take loc from methodMatchInfo
  5756. if (autoComplete->mMethodMatchInfo->mInstanceList.size() > 0)
  5757. {
  5758. int bestIdx = autoComplete->mMethodMatchInfo->mBestIdx;
  5759. auto typeInst = autoComplete->mMethodMatchInfo->mInstanceList[bestIdx].mTypeInstance;
  5760. auto methodDef = autoComplete->mMethodMatchInfo->mInstanceList[bestIdx].mMethodDef;
  5761. if (methodDef->mMethodDeclaration != NULL)
  5762. {
  5763. auto ctorDecl = BfNodeDynCast<BfConstructorDeclaration>(methodDef->mMethodDeclaration);
  5764. if (ctorDecl != NULL)
  5765. autoComplete->SetDefinitionLocation(ctorDecl->mThisToken);
  5766. else
  5767. autoComplete->SetDefinitionLocation(methodDef->GetMethodDeclaration()->mNameNode);
  5768. }
  5769. else // Just select type then
  5770. autoComplete->SetDefinitionLocation(typeInst->mTypeDef->mTypeDeclaration->mNameNode);
  5771. }
  5772. }
  5773. if (autoComplete->mGetDefinitionNode != NULL)
  5774. {
  5775. auto astNode = autoComplete->mGetDefinitionNode;
  5776. auto bfSource = autoComplete->mGetDefinitionNode->GetSourceData()->ToParserData();
  5777. if (bfSource != NULL)
  5778. {
  5779. int line = 0;
  5780. int lineChar = 0;
  5781. bfSource->GetLineCharAtIdx(astNode->GetSrcStart(), line, lineChar);
  5782. autoCompleteResultString += StrFormat("defLoc\t%s\t%d\t%d\n", bfSource->mFileName.c_str(), line, lineChar);
  5783. }
  5784. }
  5785. auto methodMatchInfo = autoComplete->mMethodMatchInfo;
  5786. if ((methodMatchInfo != NULL) && (wantsDocEntry == NULL))
  5787. {
  5788. if (methodMatchInfo->mInstanceList.size() > 0)
  5789. {
  5790. String invokeInfoText;
  5791. invokeInfoText += StrFormat("%d", methodMatchInfo->mBestIdx);
  5792. for (int srcPosIdx = 0; srcPosIdx < (int) methodMatchInfo->mSrcPositions.size(); srcPosIdx++)
  5793. invokeInfoText += StrFormat(" %d", methodMatchInfo->mSrcPositions[srcPosIdx]);
  5794. autoCompleteResultString += "invokeInfo\t";
  5795. autoCompleteResultString += invokeInfoText;
  5796. autoCompleteResultString += "\n";
  5797. }
  5798. int idx = 0;
  5799. for (auto& methodEntry : methodMatchInfo->mInstanceList)
  5800. {
  5801. String methodText;
  5802. if (methodEntry.mPayloadEnumField != NULL)
  5803. {
  5804. auto payloadFieldDef = methodEntry.mPayloadEnumField->GetFieldDef();
  5805. methodText += payloadFieldDef->mName;
  5806. methodText += "(\x1";
  5807. auto payloadType = methodEntry.mPayloadEnumField->mResolvedType;
  5808. BF_ASSERT(payloadType->IsTuple());
  5809. if (payloadType->IsTuple())
  5810. {
  5811. auto tupleType = (BfTupleType*)payloadType;
  5812. for (int fieldIdx = 0; fieldIdx < (int)tupleType->mFieldInstances.size(); fieldIdx++)
  5813. {
  5814. auto fieldInstance = &tupleType->mFieldInstances[fieldIdx];
  5815. auto fieldDef = fieldInstance->GetFieldDef();
  5816. if (fieldIdx > 0)
  5817. methodText += ",\x1 ";
  5818. methodText += bfModule->TypeToString(fieldInstance->mResolvedType, BfTypeNameFlag_ResolveGenericParamNames);
  5819. if (!fieldDef->IsUnnamedTupleField())
  5820. {
  5821. methodText += " ";
  5822. if (fieldDef->mName.StartsWith("_"))
  5823. methodText += fieldDef->mName.Substring(1);
  5824. else
  5825. methodText += fieldDef->mName;
  5826. }
  5827. }
  5828. }
  5829. methodText += "\x1)";
  5830. }
  5831. else
  5832. {
  5833. BfMethodInstance* methodInstance = NULL;
  5834. if (methodEntry.mMethodDef->mIdx < 0)
  5835. {
  5836. for (auto localMethod : mContext->mLocalMethodGraveyard)
  5837. {
  5838. if (localMethod->mMethodDef == methodEntry.mMethodDef)
  5839. {
  5840. methodInstance = localMethod->mMethodInstanceGroup->mDefault;
  5841. break;
  5842. }
  5843. }
  5844. }
  5845. else
  5846. methodInstance = bfModule->GetRawMethodInstanceAtIdx(methodEntry.mTypeInstance, methodEntry.mMethodDef->mIdx);
  5847. auto curMethodInstance = methodInstance;
  5848. curMethodInstance = methodMatchInfo->mCurMethodInstance;
  5849. SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(bfModule->mCurTypeInstance, methodMatchInfo->mCurTypeInstance);
  5850. SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(bfModule->mCurMethodInstance, curMethodInstance);
  5851. Array<String> genericMethodNameOverrides;
  5852. Array<String>* genericMethodNameOverridesPtr = NULL;
  5853. if (methodInstance->GetNumGenericArguments() != 0)
  5854. {
  5855. genericMethodNameOverridesPtr = &genericMethodNameOverrides;
  5856. for (int methodGenericArgIdx = 0; methodGenericArgIdx < (int)methodInstance->GetNumGenericArguments(); methodGenericArgIdx++)
  5857. {
  5858. BfType* methodGenericArg = NULL;
  5859. if (methodEntry.mGenericArguments.size() > 0)
  5860. methodGenericArg = methodEntry.mGenericArguments[methodGenericArgIdx];
  5861. String argName;
  5862. if (methodGenericArg == NULL)
  5863. argName = methodInstance->mMethodDef->mGenericParams[methodGenericArgIdx]->mName;
  5864. else
  5865. argName = bfModule->TypeToString(methodGenericArg, BfTypeNameFlag_ResolveGenericParamNames, NULL);
  5866. genericMethodNameOverrides.push_back(argName);
  5867. }
  5868. }
  5869. if (methodInstance->mMethodDef->mMethodType != BfMethodType_Ctor)
  5870. {
  5871. if (methodInstance->mReturnType != NULL)
  5872. methodText += bfModule->TypeToString(methodInstance->mReturnType, BfTypeNameFlag_ResolveGenericParamNames, genericMethodNameOverridesPtr);
  5873. else
  5874. methodText += BfTypeUtils::TypeToString(methodInstance->mMethodDef->mReturnTypeRef);
  5875. methodText += " ";
  5876. }
  5877. if (methodInstance->mMethodDef->mMethodType == BfMethodType_Ctor)
  5878. methodText += "this";
  5879. else
  5880. {
  5881. auto methodName = methodInstance->mMethodDef->mName;
  5882. int splitIdx = (int)methodName.IndexOf('@');
  5883. if (splitIdx != -1)
  5884. methodText += methodName.Substring(0, splitIdx);
  5885. else
  5886. methodText += methodName;
  5887. }
  5888. if (methodInstance->GetNumGenericArguments() != 0)
  5889. {
  5890. methodText += "<";
  5891. for (int methodGenericArgIdx = 0; methodGenericArgIdx < (int)methodInstance->GetNumGenericArguments(); methodGenericArgIdx++)
  5892. {
  5893. if (methodGenericArgIdx > 0)
  5894. methodText += ", ";
  5895. methodText += genericMethodNameOverrides[methodGenericArgIdx];
  5896. }
  5897. methodText += ">";
  5898. }
  5899. //TODO: Show default param values also
  5900. methodText += "(\x1";
  5901. if (methodInstance->GetParamCount() == 0)
  5902. {
  5903. // Hm - is this ever useful? Messes up some cases actually
  5904. // If param resolution failed then we need to print the original param def
  5905. /*for (int paramIdx = 0; paramIdx < (int) methodInstance->mMethodDef->mParams.size(); paramIdx++)
  5906. {
  5907. if (paramIdx > 0)
  5908. methodText += ",\x1 ";
  5909. auto paramDef = methodInstance->mMethodDef->mParams[paramIdx];
  5910. methodText += BfTypeUtils::TypeToString(paramDef->mTypeRef);
  5911. methodText += " ";
  5912. methodText += paramDef->mName;
  5913. }*/
  5914. }
  5915. int dispParamIdx = 0;
  5916. for (int paramIdx = 0; paramIdx < (int)methodInstance->GetParamCount(); paramIdx++)
  5917. {
  5918. auto paramKind = methodInstance->GetParamKind(paramIdx);
  5919. if ((paramKind == BfParamKind_ImplicitCapture) || (paramKind == BfParamKind_AppendIdx))
  5920. continue;
  5921. if (dispParamIdx > 0)
  5922. methodText += ",\x1 ";
  5923. auto type = methodInstance->GetParamType(paramIdx);
  5924. BfExpression* paramInitializer = methodInstance->GetParamInitializer(paramIdx);
  5925. if (paramInitializer != NULL)
  5926. methodText += "[";
  5927. if (paramKind == BfParamKind_Params)
  5928. methodText += "params ";
  5929. if (type->IsGenericParam())
  5930. {
  5931. auto genericParamType = (BfGenericParamType*)type;
  5932. if (genericParamType->mGenericParamKind == BfGenericParamKind_Method)
  5933. {
  5934. if (methodInstance->GetNumGenericParams() > 0)
  5935. {
  5936. auto genericParamInstance = methodInstance->mMethodInfoEx->mGenericParams[genericParamType->mGenericParamIdx];
  5937. methodText += genericParamInstance->GetGenericParamDef()->mName;
  5938. }
  5939. else
  5940. {
  5941. BfMethodInstance* curMethodInstance = methodEntry.mCurMethodInstance;
  5942. auto genericParamInstance = curMethodInstance->mMethodInfoEx->mGenericParams[genericParamType->mGenericParamIdx];
  5943. methodText += genericParamInstance->GetGenericParamDef()->mName;
  5944. }
  5945. }
  5946. else
  5947. {
  5948. BfGenericTypeInstance* genericType = (BfGenericTypeInstance*)methodEntry.mTypeInstance;
  5949. auto genericParamInstance = genericType->mGenericParams[genericParamType->mGenericParamIdx];
  5950. methodText += genericParamInstance->GetGenericParamDef()->mName;
  5951. }
  5952. }
  5953. else
  5954. methodText += bfModule->TypeToString(type, BfTypeNameFlag_ResolveGenericParamNames, genericMethodNameOverridesPtr);
  5955. methodText += " ";
  5956. methodText += methodInstance->GetParamName(paramIdx);
  5957. if (paramInitializer != NULL)
  5958. {
  5959. methodText += " = ";
  5960. methodText += paramInitializer->ToString();
  5961. methodText += "]";
  5962. }
  5963. dispParamIdx++;
  5964. }
  5965. methodText += "\x1)";
  5966. }
  5967. if (methodEntry.mMethodDef != NULL)
  5968. {
  5969. auto methodDeclaration = methodEntry.mMethodDef->GetMethodDeclaration();
  5970. if ((methodDeclaration != NULL) && (methodDeclaration->mDocumentation != NULL))
  5971. {
  5972. String docString;
  5973. _GetDocString(methodDeclaration->mDocumentation, docString);
  5974. methodText += "\x03";
  5975. methodText += docString;
  5976. }
  5977. }
  5978. autoCompleteResultString += "invoke\t" + methodText + "\n";
  5979. idx++;
  5980. }
  5981. }
  5982. Array<AutoCompleteEntry*> entries;
  5983. for (auto& entry : autoComplete->mEntriesSet)
  5984. {
  5985. entries.Add(&entry);
  5986. }
  5987. std::sort(entries.begin(), entries.end(), [](AutoCompleteEntry* lhs, AutoCompleteEntry* rhs)
  5988. {
  5989. return stricmp(lhs->mDisplay, rhs->mDisplay) < 0;
  5990. });
  5991. String docString;
  5992. for (auto entry : entries)
  5993. {
  5994. if ((wantsDocEntry != NULL) && (entry->mDocumentation == NULL))
  5995. continue;
  5996. autoCompleteResultString += String(entry->mEntryType);
  5997. autoCompleteResultString += "\t";
  5998. autoCompleteResultString += String(entry->mDisplay);
  5999. if ((entry->mDocumentation != NULL) && (wantsDocEntry != NULL) && (strcmp(wantsDocEntry, entry->mDisplay) == 0))
  6000. {
  6001. docString.Clear();
  6002. _GetDocString(entry->mDocumentation, docString);
  6003. autoCompleteResultString += '\x03';
  6004. autoCompleteResultString += docString;
  6005. }
  6006. autoCompleteResultString += "\n";
  6007. }
  6008. }
  6009. }
  6010. String BfCompiler::GetTypeDefList()
  6011. {
  6012. String result;
  6013. BfProject* curProject = NULL;
  6014. Dictionary<BfProject*, int> projectIds;
  6015. for (auto typeDef : mSystem->mTypeDefs)
  6016. {
  6017. if (typeDef->mProject != curProject)
  6018. {
  6019. curProject = typeDef->mProject;
  6020. int* projectIdPtr;
  6021. if (projectIds.TryAdd(curProject, NULL, &projectIdPtr))
  6022. {
  6023. *projectIdPtr = (int)projectIds.size() - 1;
  6024. result += "+";
  6025. result += curProject->mName;
  6026. result += "\n";
  6027. }
  6028. else
  6029. {
  6030. char str[32];
  6031. sprintf(str, "=%d\n", *projectIdPtr);
  6032. result += str;
  6033. }
  6034. }
  6035. if (((!typeDef->mIsPartial) || (typeDef->mIsCombinedPartial)))
  6036. {
  6037. if (typeDef->IsGlobalsContainer())
  6038. {
  6039. result += "g";
  6040. if (!typeDef->mNamespace.IsEmpty())
  6041. {
  6042. typeDef->mNamespace.ToString(result);
  6043. result += ".";
  6044. }
  6045. result += ":static\n";
  6046. continue;
  6047. }
  6048. else if (typeDef->mTypeCode == BfTypeCode_Interface)
  6049. result += "i";
  6050. else if (typeDef->mTypeCode == BfTypeCode_Object)
  6051. result += "c";
  6052. else
  6053. result += "v";
  6054. result += BfTypeUtils::TypeToString(typeDef) + "\n";
  6055. }
  6056. }
  6057. return result;
  6058. }
  6059. struct TypeDefMatchHelper
  6060. {
  6061. public:
  6062. StringImpl& mResult;
  6063. Array<String> mSearch;
  6064. uint32 mFoundFlags;
  6065. int32 mFoundCount;
  6066. bool mHasDotSearch;
  6067. String mCurTypeName;
  6068. String mTempStr;
  6069. public:
  6070. TypeDefMatchHelper(StringImpl& str) : mResult(str)
  6071. {
  6072. mFoundFlags = 0;
  6073. mFoundCount = 0;
  6074. mHasDotSearch = false;
  6075. }
  6076. void Sanitize(StringImpl& str)
  6077. {
  6078. for (int i = 0; i < (int)str.length(); i++)
  6079. {
  6080. char c = str[i];
  6081. if (c < (char)32)
  6082. {
  6083. str[i] = ' ';
  6084. }
  6085. }
  6086. }
  6087. void AddParams(BfMethodDef* methodDef)
  6088. {
  6089. int visParamIdx = 0;
  6090. for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
  6091. {
  6092. auto paramDef = methodDef->mParams[paramIdx];
  6093. if ((paramDef->mParamKind == BfParamKind_AppendIdx) || (paramDef->mParamKind == BfParamKind_ImplicitCapture))
  6094. continue;
  6095. if (visParamIdx > 0)
  6096. mResult += ", ";
  6097. StringT<64> refName;
  6098. paramDef->mTypeRef->ToString(refName);
  6099. Sanitize(refName);
  6100. mResult += refName;
  6101. mResult += " ";
  6102. mResult += paramDef->mName;
  6103. visParamIdx++;
  6104. }
  6105. }
  6106. void AddLocation(BfAstNode* node)
  6107. {
  6108. if (node == NULL)
  6109. return;
  6110. auto parserData = node->GetSourceData()->ToParserData();
  6111. if (parserData != NULL)
  6112. {
  6113. mResult += parserData->mFileName;
  6114. int lineNum = 0;
  6115. int column = 0;
  6116. parserData->GetLineCharAtIdx(node->GetSrcStart(), lineNum, column);
  6117. mResult += StrFormat("\t%d\t%d", lineNum, column);
  6118. }
  6119. };
  6120. void AddFieldDef(BfFieldDef* fieldDef)
  6121. {
  6122. mResult += "\t";
  6123. AddLocation(fieldDef->GetRefNode());
  6124. mResult += "\n";
  6125. }
  6126. void AddPropertyDef(BfTypeDef* typeDef, BfPropertyDef* propDef)
  6127. {
  6128. if (propDef->mName == "[]")
  6129. {
  6130. mResult += "[";
  6131. for (auto methodDef : propDef->mMethods)
  6132. {
  6133. if (methodDef->mMethodType == BfMethodType_PropertyGetter)
  6134. {
  6135. AddParams(methodDef);
  6136. break;
  6137. }
  6138. }
  6139. mResult += "]";
  6140. }
  6141. else
  6142. mResult += propDef->mName;
  6143. mResult += "\t";
  6144. auto refNode = propDef->GetRefNode();
  6145. if (refNode == NULL)
  6146. refNode = typeDef->GetRefNode();
  6147. AddLocation(refNode);
  6148. mResult += "\n";
  6149. }
  6150. void AddMethodDef(BfMethodDef* methodDef)
  6151. {
  6152. if (methodDef->mMethodType == BfMethodType_Ctor)
  6153. {
  6154. if (methodDef->mIsStatic)
  6155. mResult += "static ";
  6156. mResult += "this";
  6157. }
  6158. else if (methodDef->mMethodType == BfMethodType_Dtor)
  6159. {
  6160. if (methodDef->mIsStatic)
  6161. mResult += "static ";
  6162. mResult += "~this";
  6163. }
  6164. else
  6165. mResult += methodDef->mName;
  6166. if (methodDef->mMethodType == BfMethodType_Mixin)
  6167. mResult += "!";
  6168. mResult += "(";
  6169. AddParams(methodDef);
  6170. mResult += ")";
  6171. mResult += "\t";
  6172. AddLocation(methodDef->GetRefNode());
  6173. mResult += "\n";
  6174. }
  6175. void ClearResults()
  6176. {
  6177. mFoundFlags = 0;
  6178. mFoundCount = 0;
  6179. }
  6180. bool MergeFlags(uint32 flags)
  6181. {
  6182. int flagIdx = 0;
  6183. while (flags > 0)
  6184. {
  6185. if (((flags & 1) != 0) && ((mFoundFlags & (1 << flagIdx)) == 0))
  6186. {
  6187. mFoundFlags |= (1 << flagIdx);
  6188. mFoundCount++;
  6189. }
  6190. flags >>= 1;
  6191. flagIdx++;
  6192. }
  6193. return mFoundCount == mSearch.mSize;
  6194. }
  6195. uint32 CheckMatch(const StringView& str)
  6196. {
  6197. uint32 matchFlags = 0;
  6198. for (int i = 0; i < mSearch.mSize; i++)
  6199. {
  6200. if (((mFoundFlags & (1 << i)) == 0) && (str.IndexOf(mSearch[i], true) != -1))
  6201. {
  6202. mFoundCount++;
  6203. matchFlags |= (1 << i);
  6204. mFoundFlags |= (1 << i);
  6205. }
  6206. }
  6207. return matchFlags;
  6208. }
  6209. bool CheckCompletesMatch(BfAtomComposite& name)
  6210. {
  6211. for (int i = 0; i < name.mSize; i++)
  6212. {
  6213. CheckMatch(name.mParts[i]->mString);
  6214. if (mFoundCount == mSearch.mSize)
  6215. return true;
  6216. }
  6217. return false;
  6218. }
  6219. bool IsFullMatch()
  6220. {
  6221. return mFoundCount == mSearch.mSize;
  6222. }
  6223. bool CheckMemberMatch(BfTypeDef* typeDef, const StringView& str)
  6224. {
  6225. if (CheckMatch(str) == 0)
  6226. {
  6227. if (mHasDotSearch)
  6228. {
  6229. mTempStr.Clear();
  6230. mTempStr += mCurTypeName;
  6231. mTempStr += ".";
  6232. mTempStr += str;
  6233. if (CheckMatch(mTempStr) == 0)
  6234. return false;
  6235. }
  6236. else
  6237. return false;
  6238. }
  6239. if ((IsFullMatch()) || (CheckCompletesMatch(typeDef->mFullName)))
  6240. return true;
  6241. return false;
  6242. }
  6243. };
  6244. String BfCompiler::GetTypeDefMatches(const StringImpl& searchStr)
  6245. {
  6246. String result;
  6247. TypeDefMatchHelper matchHelper(result);
  6248. //
  6249. {
  6250. int searchIdx = 0;
  6251. while (searchIdx < (int)searchStr.length())
  6252. {
  6253. int spacePos = (int)searchStr.IndexOf(' ', searchIdx);
  6254. String str;
  6255. if (spacePos == -1)
  6256. str = searchStr.Substring(searchIdx);
  6257. else
  6258. str = searchStr.Substring(searchIdx, spacePos - searchIdx);
  6259. str.Trim();
  6260. if (!str.IsEmpty())
  6261. matchHelper.mSearch.Add(str);
  6262. if (str.Contains('.'))
  6263. matchHelper.mHasDotSearch = true;
  6264. if (spacePos == -1)
  6265. break;
  6266. searchIdx = spacePos + 1;
  6267. }
  6268. //// We sort from longest to shortest to make sure longer strings match before shorter, which
  6269. //// matters when the shorter string is a subset of the longer string
  6270. //matchHelper.mSearch.Sort([](const String& lhs, const String& rhs)
  6271. // {
  6272. // int lenCmp = (int)(rhs.length() - lhs.length());
  6273. // if (lenCmp != 0)
  6274. // return lenCmp < 0;
  6275. // return lhs < rhs;
  6276. // });
  6277. }
  6278. BfProject* curProject = NULL;
  6279. Dictionary<BfProject*, int> projectIds;
  6280. Dictionary<BfAtom*, int> atomMatchMap;
  6281. struct ProjectInfo
  6282. {
  6283. Dictionary<String, int> matchedNames;
  6284. };
  6285. Array<ProjectInfo> projectInfos;
  6286. projectInfos.Resize(mSystem->mProjects.size());
  6287. String typeName;
  6288. String foundName;
  6289. int partialIdx = 0;
  6290. for (auto typeDef : mSystem->mTypeDefs)
  6291. {
  6292. if (typeDef->mIsPartial)
  6293. continue;
  6294. bool fullyMatchesName = false;
  6295. if (matchHelper.mHasDotSearch)
  6296. {
  6297. matchHelper.mCurTypeName.Clear();
  6298. typeDef->mFullName.ToString(matchHelper.mCurTypeName);
  6299. matchHelper.ClearResults();
  6300. matchHelper.CheckMatch(matchHelper.mCurTypeName);
  6301. fullyMatchesName = matchHelper.IsFullMatch();
  6302. }
  6303. int matchIdx = -1;
  6304. //BfAtomComposite foundComposite;
  6305. if (!fullyMatchesName)
  6306. {
  6307. for (auto fieldDef : typeDef->mFields)
  6308. {
  6309. matchHelper.ClearResults();
  6310. bool hasMatch = false;
  6311. if (matchHelper.CheckMemberMatch(typeDef, fieldDef->mName))
  6312. {
  6313. result += "F";
  6314. if (BfTypeUtils::TypeToString(result, typeDef, BfTypeNameFlag_HideGlobalName))
  6315. result += ".";
  6316. result += fieldDef->mName;
  6317. matchHelper.AddFieldDef(fieldDef);
  6318. }
  6319. }
  6320. for (auto propDef : typeDef->mProperties)
  6321. {
  6322. if (propDef->GetRefNode() == NULL)
  6323. continue;
  6324. matchHelper.ClearResults();
  6325. if (matchHelper.CheckMemberMatch(typeDef, propDef->mName))
  6326. {
  6327. result += "P";
  6328. if (BfTypeUtils::TypeToString(result, typeDef, BfTypeNameFlag_HideGlobalName))
  6329. result += ".";
  6330. matchHelper.AddPropertyDef(typeDef, propDef);
  6331. }
  6332. }
  6333. for (auto methodDef : typeDef->mMethods)
  6334. {
  6335. if ((methodDef->mMethodType != BfMethodType_Normal) &&
  6336. (methodDef->mMethodType != BfMethodType_Mixin) &&
  6337. (methodDef->mMethodType != BfMethodType_Ctor) &&
  6338. (methodDef->mMethodType != BfMethodType_Dtor))
  6339. continue;
  6340. if (methodDef->mMethodDeclaration == NULL)
  6341. continue;
  6342. matchHelper.ClearResults();
  6343. if (matchHelper.CheckMemberMatch(typeDef, methodDef->mName))
  6344. {
  6345. result += "M";
  6346. if (BfTypeUtils::TypeToString(result, typeDef, BfTypeNameFlag_HideGlobalName))
  6347. result += ".";
  6348. matchHelper.AddMethodDef(methodDef);
  6349. }
  6350. }
  6351. uint32 matchFlags = 0;
  6352. for (int atomIdx = typeDef->mFullName.mSize - 1; atomIdx >= 0; atomIdx--)
  6353. {
  6354. auto atom = typeDef->mFullName.mParts[atomIdx];
  6355. int* matchesPtr = NULL;
  6356. if (atomMatchMap.TryAdd(atom, NULL, &matchesPtr))
  6357. {
  6358. matchHelper.ClearResults();
  6359. *matchesPtr = matchHelper.CheckMatch(atom->mString);
  6360. }
  6361. if (*matchesPtr != 0)
  6362. {
  6363. if (matchIdx == -1)
  6364. matchIdx = atomIdx;
  6365. matchFlags |= *matchesPtr;
  6366. }
  6367. }
  6368. matchHelper.ClearResults();
  6369. if (!matchHelper.MergeFlags(matchFlags))
  6370. {
  6371. continue;
  6372. }
  6373. //foundComposite.Set(typeDef->mFullName.mParts, matchIdx + 1, NULL, 0);
  6374. //foundComposite = typeDef->mFullName;
  6375. }
  6376. if (typeDef->mProject != curProject)
  6377. {
  6378. curProject = typeDef->mProject;
  6379. int* projectIdPtr;
  6380. if (projectIds.TryAdd(curProject, NULL, &projectIdPtr))
  6381. {
  6382. *projectIdPtr = (int)projectIds.size() - 1;
  6383. result += "+";
  6384. result += curProject->mName;
  6385. result += "\n";
  6386. }
  6387. else
  6388. {
  6389. char str[32];
  6390. sprintf(str, "=%d\n", *projectIdPtr);
  6391. result += str;
  6392. }
  6393. }
  6394. typeName = BfTypeUtils::TypeToString(typeDef);
  6395. if (matchIdx != -1)
  6396. {
  6397. int* matchIdxPtr = 0;
  6398. auto projectInfo = &projectInfos[typeDef->mProject->mIdx];
  6399. int dotCount = 0;
  6400. foundName = typeName;
  6401. for (int i = 0; i < (int)typeName.length(); i++)
  6402. {
  6403. if (typeName[i] == '.')
  6404. {
  6405. if (dotCount == matchIdx)
  6406. {
  6407. foundName.Clear();
  6408. foundName.Append(typeName.c_str(), i);
  6409. break;
  6410. }
  6411. dotCount++;
  6412. }
  6413. }
  6414. if (projectInfo->matchedNames.TryAdd(foundName, NULL, &matchIdxPtr))
  6415. {
  6416. *matchIdxPtr = partialIdx++;
  6417. result += StrFormat(">%d@", matchIdx);
  6418. }
  6419. else
  6420. {
  6421. result += StrFormat("<%d@", *matchIdxPtr);
  6422. }
  6423. }
  6424. else
  6425. {
  6426. result += ":";
  6427. }
  6428. if (typeDef->IsGlobalsContainer())
  6429. {
  6430. result += "g";
  6431. if (!typeDef->mNamespace.IsEmpty())
  6432. {
  6433. typeDef->mNamespace.ToString(result);
  6434. result += ".";
  6435. }
  6436. result += ":static\n";
  6437. continue;
  6438. }
  6439. else if (typeDef->mTypeCode == BfTypeCode_Interface)
  6440. result += "i";
  6441. else if (typeDef->mTypeCode == BfTypeCode_Object)
  6442. result += "c";
  6443. else
  6444. result += "v";
  6445. result += typeName + "\n";
  6446. }
  6447. return result;
  6448. }
  6449. String BfCompiler::GetTypeDefInfo(const StringImpl& inTypeName)
  6450. {
  6451. BfProject* project = NULL;
  6452. int idx = 0;
  6453. int sep = (int)inTypeName.IndexOf(':');
  6454. if (sep != -1)
  6455. {
  6456. idx = sep + 1;
  6457. project = mSystem->GetProject(inTypeName.Substring(0, sep));
  6458. }
  6459. String typeName;
  6460. int genericCount = 0;
  6461. int pendingGenericCount = 0;
  6462. for ( ; idx < (int)inTypeName.length(); idx++)
  6463. {
  6464. char c = inTypeName[idx];
  6465. if (c == '<')
  6466. genericCount = 1;
  6467. else if (genericCount > 0)
  6468. {
  6469. if (c == ',')
  6470. genericCount++;
  6471. else if (c == '>')
  6472. {
  6473. pendingGenericCount = genericCount;
  6474. genericCount = 0;
  6475. }
  6476. }
  6477. else
  6478. {
  6479. if (pendingGenericCount != 0)
  6480. {
  6481. typeName += StrFormat("`%d", pendingGenericCount);
  6482. pendingGenericCount = 0;
  6483. }
  6484. typeName += c;
  6485. }
  6486. }
  6487. bool isGlobals = false;
  6488. if (typeName == ":static")
  6489. {
  6490. typeName.clear();
  6491. isGlobals = true;
  6492. }
  6493. if (typeName.EndsWith(".:static"))
  6494. {
  6495. typeName.RemoveToEnd(typeName.length() - 8);
  6496. isGlobals = true;
  6497. }
  6498. String result;
  6499. TypeDefMatchHelper matchHelper(result);
  6500. BfAtomComposite nameComposite;
  6501. if ((typeName.IsEmpty()) || (mSystem->ParseAtomComposite(typeName, nameComposite)))
  6502. {
  6503. auto itr = mSystem->mTypeDefs.TryGet(nameComposite);
  6504. while (itr)
  6505. {
  6506. auto typeDef = *itr;
  6507. if ((!typeDef->mIsPartial) &&
  6508. (typeDef->mProject == project) &&
  6509. (typeDef->mFullName == nameComposite) &&
  6510. (typeDef->IsGlobalsContainer() == isGlobals) &&
  6511. (typeDef->GetSelfGenericParamCount() == pendingGenericCount))
  6512. {
  6513. auto refNode = typeDef->GetRefNode();
  6514. result += "S";
  6515. matchHelper.AddLocation(refNode);
  6516. result += "\n";
  6517. for (auto fieldDef : typeDef->mFields)
  6518. {
  6519. result += "F";
  6520. result += fieldDef->mName;
  6521. matchHelper.AddFieldDef(fieldDef);
  6522. }
  6523. for (auto propDef : typeDef->mProperties)
  6524. {
  6525. if (propDef->GetRefNode() == NULL)
  6526. continue;
  6527. result += "P";
  6528. matchHelper.AddPropertyDef(typeDef, propDef);
  6529. }
  6530. for (auto methodDef : typeDef->mMethods)
  6531. {
  6532. if ((methodDef->mMethodType != BfMethodType_Normal) &&
  6533. (methodDef->mMethodType != BfMethodType_Mixin) &&
  6534. (methodDef->mMethodType != BfMethodType_Ctor) &&
  6535. (methodDef->mMethodType != BfMethodType_Dtor))
  6536. continue;
  6537. if (methodDef->mMethodDeclaration == NULL)
  6538. continue;
  6539. result += "M";
  6540. matchHelper.AddMethodDef(methodDef);
  6541. }
  6542. }
  6543. itr.MoveToNextHashMatch();
  6544. }
  6545. }
  6546. return result;
  6547. }
  6548. //////////////////////////////////////////////////////////////////////////
  6549. PerfManager* BfGetPerfManager(BfParser* bfParser);
  6550. /*BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetDefaultTargetTriple(BfCompiler* bfCompiler)
  6551. {
  6552. String& autoCompleteResultString = *gTLStrReturn.Get();
  6553. return autoCompleteResultString.c_str();
  6554. }*/
  6555. BF_EXPORT bool BF_CALLTYPE BfCompiler_Compile(BfCompiler* bfCompiler, BfPassInstance* bfPassInstance, const char* outputPath)
  6556. {
  6557. BP_ZONE("BfCompiler_Compile");
  6558. SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, bfPassInstance);
  6559. bfCompiler->mPassInstance = bfPassInstance;
  6560. bfCompiler->Compile(outputPath);
  6561. return !bfCompiler->mPassInstance->HasFailed();
  6562. }
  6563. BF_EXPORT void BF_CALLTYPE BfCompiler_ClearResults(BfCompiler* bfCompiler)
  6564. {
  6565. bfCompiler->ClearResults();
  6566. }
  6567. BF_EXPORT bool BF_CALLTYPE BfCompiler_ClassifySource(BfCompiler* bfCompiler, BfPassInstance* bfPassInstance, BfParser* bfParser, BfResolvePassData* resolvePassData, BfSourceClassifier::CharData* charData)
  6568. {
  6569. BP_ZONE("BfCompiler_ClassifySource");
  6570. BfSourceClassifier bfSourceClassifier(bfParser, charData);
  6571. bfSourceClassifier.mClassifierPassId = bfPassInstance->mClassifierPassId;
  6572. String& autoCompleteResultString = *gTLStrReturn.Get();
  6573. autoCompleteResultString.clear();
  6574. bool doClassifyPass = (charData != NULL) && (resolvePassData->mResolveType <= BfResolveType_Autocomplete_HighPri);
  6575. bfSourceClassifier.mEnabled = doClassifyPass;
  6576. // Full classifier pass?
  6577. bfSourceClassifier.mSkipMethodInternals = true;
  6578. bfSourceClassifier.mSkipTypeDeclarations = true;
  6579. if ((charData != NULL) && (doClassifyPass))
  6580. bfSourceClassifier.Visit(bfParser->mRootNode);
  6581. bfSourceClassifier.mSkipTypeDeclarations = false;
  6582. bfSourceClassifier.mSkipMethodInternals = false;
  6583. if (charData != NULL)
  6584. resolvePassData->mSourceClassifier = &bfSourceClassifier;
  6585. bfPassInstance->mFilterErrorsTo = bfParser;
  6586. bfPassInstance->mTrimMessagesToCursor = true;
  6587. SetAndRestoreValue<BfResolvePassData*> prevCompilerResolvePassData(bfCompiler->mResolvePassData, resolvePassData);
  6588. SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, bfPassInstance);
  6589. bool canceled = false;
  6590. if (resolvePassData->mAutoComplete != NULL)
  6591. {
  6592. bfCompiler->ProcessAutocompleteTempType();
  6593. }
  6594. else
  6595. canceled = !bfCompiler->Compile("");
  6596. resolvePassData->mSourceClassifier = NULL;
  6597. if ((charData != NULL) && (doClassifyPass))
  6598. {
  6599. bfSourceClassifier.mIsSideChannel = false;
  6600. bfSourceClassifier.Visit(bfParser->mErrorRootNode);
  6601. bfSourceClassifier.mIsSideChannel = true;
  6602. bfSourceClassifier.Visit(bfParser->mSidechannelRootNode);
  6603. }
  6604. return !canceled;
  6605. }
  6606. BF_EXPORT bool BF_CALLTYPE BfCompiler_VerifyTypeName(BfCompiler* bfCompiler, char* name, int cursorPos)
  6607. {
  6608. String typeName = name;
  6609. auto system = bfCompiler->mSystem;
  6610. AutoCrit autoCrit(system->mSystemLock);
  6611. String& autoCompleteResultString = *gTLStrReturn.Get();
  6612. autoCompleteResultString.Clear();
  6613. BfPassInstance passInstance(bfCompiler->mSystem);
  6614. BfParser parser(bfCompiler->mSystem);
  6615. parser.SetSource(typeName.c_str(), (int)typeName.length());
  6616. parser.Parse(&passInstance);
  6617. parser.mCursorIdx = cursorPos;
  6618. parser.mCursorCheckIdx = cursorPos;
  6619. BfReducer reducer;
  6620. reducer.mAlloc = parser.mAlloc;
  6621. reducer.mPassInstance = &passInstance;
  6622. reducer.mAllowTypeWildcard = true;
  6623. if (parser.mRootNode->mChildArr.mSize == 0)
  6624. return false;
  6625. bool attribWasClosed = false;
  6626. bool isAttributeRef = false;
  6627. auto firstNode = parser.mRootNode->mChildArr[0];
  6628. auto endIdx = parser.mRootNode->mSrcEnd;
  6629. reducer.mVisitorPos = BfReducer::BfVisitorPos(parser.mRootNode);
  6630. if (auto tokenNode = BfNodeDynCast<BfTokenNode>(firstNode))
  6631. {
  6632. if (tokenNode->mToken == BfToken_LBracket)
  6633. {
  6634. if (auto lastToken = BfNodeDynCast<BfTokenNode>(parser.mRootNode->mChildArr.back()))
  6635. {
  6636. if (lastToken->mToken == BfToken_RBracket)
  6637. {
  6638. attribWasClosed = true;
  6639. endIdx = lastToken->mSrcStart;
  6640. }
  6641. }
  6642. isAttributeRef = true;
  6643. if (parser.mRootNode->mChildArr.mSize < 2)
  6644. return false;
  6645. firstNode = parser.mRootNode->mChildArr[1];
  6646. reducer.mVisitorPos.MoveNext();
  6647. }
  6648. }
  6649. reducer.mVisitorPos.MoveNext();
  6650. auto typeRef = reducer.CreateTypeRef(firstNode);
  6651. if (typeRef == NULL)
  6652. return false;
  6653. BfResolvePassData resolvePassData;
  6654. if (cursorPos != -1)
  6655. {
  6656. resolvePassData.mResolveType = BfResolveType_Autocomplete;
  6657. parser.mParserFlags = (BfParserFlag)(parser.mParserFlags | ParserFlag_Autocomplete);
  6658. resolvePassData.mAutoComplete = new BfAutoComplete();
  6659. resolvePassData.mAutoComplete->mSystem = bfCompiler->mSystem;
  6660. resolvePassData.mAutoComplete->mCompiler = bfCompiler;
  6661. resolvePassData.mAutoComplete->mModule = bfCompiler->mContext->mScratchModule;
  6662. }
  6663. resolvePassData.mParser = &parser;
  6664. SetAndRestoreValue<BfResolvePassData*> prevCompilerResolvePassData(bfCompiler->mResolvePassData, &resolvePassData);
  6665. SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, &passInstance);
  6666. if (resolvePassData.mAutoComplete != NULL)
  6667. {
  6668. if (isAttributeRef)
  6669. resolvePassData.mAutoComplete->CheckAttributeTypeRef(typeRef);
  6670. else
  6671. resolvePassData.mAutoComplete->CheckTypeRef(typeRef, false);
  6672. bfCompiler->GenerateAutocompleteInfo();
  6673. }
  6674. if (passInstance.HasFailed())
  6675. return false;
  6676. if (typeRef->mSrcEnd != endIdx)
  6677. return false;
  6678. if (!bfCompiler->mContext->mScratchModule->ValidateTypeWildcard(typeRef, isAttributeRef))
  6679. return false;
  6680. if ((isAttributeRef) && (!attribWasClosed))
  6681. return false;
  6682. return true;
  6683. }
  6684. BF_EXPORT void BF_CALLTYPE BfCompiler_ClearCompletionPercentage(BfCompiler* bfCompiler)
  6685. {
  6686. bfCompiler->mCompletionPct = 0;
  6687. }
  6688. BF_EXPORT float BF_CALLTYPE BfCompiler_GetCompletionPercentage(BfCompiler* bfCompiler)
  6689. {
  6690. return bfCompiler->mCompletionPct;
  6691. }
  6692. BF_EXPORT int BF_CALLTYPE BfCompiler_GetCompileRevision(BfCompiler* bfCompiler)
  6693. {
  6694. return bfCompiler->mRevision;
  6695. }
  6696. BF_EXPORT void BF_CALLTYPE BfCompiler_Cancel(BfCompiler* bfCompiler)
  6697. {
  6698. bfCompiler->Cancel();
  6699. }
  6700. BF_EXPORT void BF_CALLTYPE BfCompiler_ClearBuildCache(BfCompiler* bfCompiler)
  6701. {
  6702. bfCompiler->ClearBuildCache();
  6703. }
  6704. BF_EXPORT void BF_CALLTYPE BfCompiler_SetBuildValue(BfCompiler* bfCompiler, char* cacheDir, char* key, char* value)
  6705. {
  6706. bfCompiler->mCodeGen.SetBuildValue(cacheDir, key, value);
  6707. }
  6708. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetBuildValue(BfCompiler* bfCompiler, char* cacheDir, char* key)
  6709. {
  6710. String& outString = *gTLStrReturn.Get();
  6711. outString = bfCompiler->mCodeGen.GetBuildValue(cacheDir, key);
  6712. return outString.c_str();
  6713. }
  6714. BF_EXPORT void BF_CALLTYPE BfCompiler_WriteBuildCache(BfCompiler* bfCompiler, char* cacheDir)
  6715. {
  6716. bfCompiler->mCodeGen.WriteBuildCache(cacheDir);
  6717. }
  6718. BF_EXPORT void BF_CALLTYPE BfCompiler_Delete(BfCompiler* bfCompiler)
  6719. {
  6720. delete bfCompiler;
  6721. }
  6722. BF_EXPORT void BF_CALLTYPE BfCompiler_ProgramDone()
  6723. {
  6724. #ifdef BF_PLATFORM_WINDOWS
  6725. BeLibManager::Get()->Clear();
  6726. #endif
  6727. }
  6728. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetTypeDefList(BfCompiler* bfCompiler)
  6729. {
  6730. String& outString = *gTLStrReturn.Get();
  6731. outString.clear();
  6732. outString = bfCompiler->GetTypeDefList();
  6733. return outString.c_str();
  6734. }
  6735. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetTypeDefMatches(BfCompiler* bfCompiler, const char* searchStr)
  6736. {
  6737. String& outString = *gTLStrReturn.Get();
  6738. outString.clear();
  6739. outString = bfCompiler->GetTypeDefMatches(searchStr);
  6740. return outString.c_str();
  6741. }
  6742. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetTypeDefInfo(BfCompiler* bfCompiler, const char* typeDefName)
  6743. {
  6744. String& outString = *gTLStrReturn.Get();
  6745. outString.clear();
  6746. outString = bfCompiler->GetTypeDefInfo(typeDefName);
  6747. return outString.c_str();
  6748. }
  6749. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetOutputFileNames(BfCompiler* bfCompiler, BfProject* bfProject, bool* hadOutputChanges)
  6750. {
  6751. BF_FATAL("not used ?");
  6752. *hadOutputChanges = false;
  6753. String& outString = *gTLStrReturn.Get();
  6754. outString.clear();
  6755. for (auto mainModule : bfCompiler->mContext->mModules)
  6756. {
  6757. if (!mainModule->mIsReified)
  6758. continue;
  6759. if (mainModule->mProject != bfProject)
  6760. continue;
  6761. if (bfCompiler->mOptions.mHotProject != NULL)
  6762. continue; // Only add new objs from mCodeGen.mCodeGenFiles during hot reload
  6763. for (auto&& moduleFileName : mainModule->mOutFileNames)
  6764. {
  6765. if (!moduleFileName.mModuleWritten)
  6766. continue;
  6767. if (!outString.empty())
  6768. outString += "\n";
  6769. outString += moduleFileName.mFileName;
  6770. }
  6771. }
  6772. if (bfCompiler->mHotState != NULL)
  6773. {
  6774. Array<String> outPaths;
  6775. for (int i = 0; i < (int)bfCompiler->mHotState->mQueuedOutFiles.size(); i++)
  6776. {
  6777. auto& fileEntry = bfCompiler->mHotState->mQueuedOutFiles[i];
  6778. if (fileEntry.mProject != bfProject)
  6779. continue;
  6780. outPaths.Add(fileEntry.mFileName);
  6781. bfCompiler->mHotState->mQueuedOutFiles.RemoveAtFast(i);
  6782. i--;
  6783. }
  6784. //outPaths.Sort();
  6785. std::sort(outPaths.begin(), outPaths.end(), [](const String& lhs, const String& rhs) { return lhs < rhs; });
  6786. for (auto& path : outPaths)
  6787. {
  6788. if (!outString.empty())
  6789. outString += "\n";
  6790. outString += path;
  6791. outString += BF_OBJ_EXT;
  6792. }
  6793. }
  6794. return outString.c_str();
  6795. }
  6796. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetUsedOutputFileNames(BfCompiler* bfCompiler, BfProject* bfProject, bool flushQueuedHotFiles, bool* hadOutputChanges)
  6797. {
  6798. BP_ZONE("BfCompiler_GetUsedOutputFileNames");
  6799. *hadOutputChanges = false;
  6800. String& outString = *gTLStrReturn.Get();
  6801. outString.clear();
  6802. Array<BfModule*> moduleList;
  6803. moduleList.Reserve(bfProject->mUsedModules.size());
  6804. if (bfCompiler->mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
  6805. {
  6806. for (auto mainModule : bfCompiler->mContext->mModules)
  6807. {
  6808. if ((!mainModule->mIsReified) || (mainModule->mIsScratchModule))
  6809. continue;
  6810. if (bfCompiler->mOptions.mHotProject != NULL)
  6811. continue; // Only add new objs from mCodeGen.mCodeGenFiles during hot reload
  6812. if (!bfCompiler->IsModuleAccessible(mainModule, bfProject))
  6813. continue;
  6814. moduleList.push_back(mainModule);
  6815. }
  6816. }
  6817. else
  6818. {
  6819. for (auto mainModule : bfProject->mUsedModules)
  6820. {
  6821. if ((!mainModule->mIsReified) || (mainModule->mIsScratchModule))
  6822. continue;
  6823. if (bfCompiler->mOptions.mHotProject != NULL)
  6824. continue; // Only add new objs from mCodeGen.mCodeGenFiles during hot reload
  6825. moduleList.push_back(mainModule);
  6826. }
  6827. }
  6828. std::sort(moduleList.begin(), moduleList.end(), [&](BfModule* moduleA, BfModule* moduleB) { return moduleA->mModuleName < moduleB->mModuleName; } );
  6829. HashSet<String> usedFileNames;
  6830. usedFileNames.Reserve(moduleList.size());
  6831. for (auto mainModule : moduleList)
  6832. {
  6833. for (auto fileNameIdx : mainModule->mImportFileNames)
  6834. {
  6835. auto fileName = bfCompiler->mContext->mStringObjectIdMap[fileNameIdx].mString;
  6836. if (!usedFileNames.TryAdd(fileName, NULL))
  6837. continue;
  6838. if (!outString.empty())
  6839. outString += "\n";
  6840. outString += fileName;
  6841. }
  6842. for (auto&& moduleFileName : mainModule->mOutFileNames)
  6843. {
  6844. if (!moduleFileName.mModuleWritten)
  6845. continue;
  6846. bool canReference = true;
  6847. for (auto project : moduleFileName.mProjects)
  6848. {
  6849. if (!bfProject->ContainsReference(project))
  6850. canReference = false;
  6851. if (bfProject != project)
  6852. {
  6853. if (project->mTargetType == BfTargetType_BeefDynLib)
  6854. canReference = false;
  6855. }
  6856. }
  6857. if (!canReference)
  6858. continue;
  6859. String fileName = moduleFileName.mFileName;
  6860. #ifdef BF_PLATFORM_WINDOWS
  6861. if (moduleFileName.mWroteToLib)
  6862. fileName = BeLibManager::GetLibFilePath(fileName);
  6863. #endif
  6864. if (!usedFileNames.TryAdd(fileName, NULL))
  6865. continue;
  6866. if (!outString.empty())
  6867. outString += "\n";
  6868. outString += fileName;
  6869. if (mainModule->mWroteToLib)
  6870. break;
  6871. }
  6872. }
  6873. if (bfCompiler->mHotState != NULL)
  6874. {
  6875. Array<String> outPaths;
  6876. for (int i = 0; i < (int)bfCompiler->mHotState->mQueuedOutFiles.size(); i++)
  6877. {
  6878. auto& fileEntry = bfCompiler->mHotState->mQueuedOutFiles[i];
  6879. if (fileEntry.mProject != bfProject)
  6880. continue;
  6881. if (!bfCompiler->mHotState->mHotProject->mUsedModules.Contains(fileEntry.mModule))
  6882. continue;
  6883. outPaths.Add(fileEntry.mFileName);
  6884. if (flushQueuedHotFiles)
  6885. {
  6886. bfCompiler->mHotState->mQueuedOutFiles.RemoveAtFast(i);
  6887. i--;
  6888. }
  6889. }
  6890. std::sort(outPaths.begin(), outPaths.end(), [](const String& lhs, const String& rhs) { return lhs < rhs; });
  6891. for (auto& path : outPaths)
  6892. {
  6893. if (!outString.empty())
  6894. outString += "\n";
  6895. outString += path;
  6896. outString += BF_OBJ_EXT;
  6897. }
  6898. }
  6899. for (auto& fileEntry : bfCompiler->mCodeGen.mCodeGenFiles)
  6900. {
  6901. if (fileEntry.mWasCached)
  6902. continue;
  6903. if (!bfProject->ContainsReference(fileEntry.mProject))
  6904. continue;
  6905. *hadOutputChanges = true;
  6906. }
  6907. return outString.c_str();
  6908. }
  6909. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetAutocompleteInfo(BfCompiler* bfCompiler)
  6910. {
  6911. String& autoCompleteResultString = *gTLStrReturn.Get();
  6912. return autoCompleteResultString.c_str();
  6913. }
  6914. BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetSymbolReferences(BfCompiler* bfCompiler, BfPassInstance* bfPassInstance, BfResolvePassData* resolvePassData)
  6915. {
  6916. BP_ZONE("BfCompiler_GetSymbolReferences");
  6917. String& outString = *gTLStrReturn.Get();
  6918. outString.clear();
  6919. SetAndRestoreValue<BfResolvePassData*> prevCompilerResolvePassData(bfCompiler->mResolvePassData, resolvePassData);
  6920. SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, bfPassInstance);
  6921. bfCompiler->GetSymbolReferences();
  6922. std::map<String, String*> sortedParserMap;
  6923. for (auto& parserDataPair : resolvePassData->mFoundSymbolReferencesParserData)
  6924. {
  6925. sortedParserMap.insert(std::make_pair(parserDataPair.mKey->mFileName, &parserDataPair.mValue));
  6926. }
  6927. for (auto& parserData : sortedParserMap)
  6928. {
  6929. if (!outString.empty())
  6930. outString += "\n";
  6931. outString += parserData.first + "\t" + *(parserData.second);
  6932. }
  6933. return outString.c_str();
  6934. }
  6935. BF_EXPORT bool BF_CALLTYPE BfCompiler_GetHasHotPendingDataChanges(BfCompiler* bfCompiler)
  6936. {
  6937. return (bfCompiler->mHotState != NULL) &&
  6938. ((!bfCompiler->mHotState->mPendingDataChanges.IsEmpty()) || (!bfCompiler->mHotState->mPendingFailedSlottings.IsEmpty()));
  6939. }
  6940. BF_EXPORT void BF_CALLTYPE BfCompiler_HotCommit(BfCompiler* bfCompiler)
  6941. {
  6942. bfCompiler->HotCommit();
  6943. }
  6944. BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_Start(BfCompiler* bfCompiler, int flags)
  6945. {
  6946. bfCompiler->HotResolve_Start((BfCompiler::HotResolveFlags)flags);
  6947. }
  6948. BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_AddActiveMethod(BfCompiler* bfCompiler, const char* methodName)
  6949. {
  6950. bfCompiler->HotResolve_AddActiveMethod(methodName);
  6951. }
  6952. BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_AddDelegateMethod(BfCompiler* bfCompiler, const char* methodName)
  6953. {
  6954. bfCompiler->HotResolve_AddDelegateMethod(methodName);
  6955. }
  6956. // 0: heap, 1: user stated 'not used', 2: user stated 'used'
  6957. BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_ReportType(BfCompiler* bfCompiler, int typeId, int usageKind)
  6958. {
  6959. bfCompiler->HotResolve_ReportType(typeId, (BfCompiler::HotTypeFlags)usageKind);
  6960. }
  6961. // 0: heap, 1: user stated 'used', 2: user stated 'not used'
  6962. BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_ReportTypeRange(BfCompiler* bfCompiler, const char* typeName, int usageKind)
  6963. {
  6964. //TODO: Implement
  6965. }
  6966. BF_EXPORT const char* BF_CALLTYPE BfCompiler_HotResolve_Finish(BfCompiler* bfCompiler)
  6967. {
  6968. String& outString = *gTLStrReturn.Get();
  6969. outString = bfCompiler->HotResolve_Finish();
  6970. return outString.c_str();
  6971. }
  6972. static BfPlatformType GetPlatform(StringView str)
  6973. {
  6974. while (!str.IsEmpty())
  6975. {
  6976. char c = str[str.mLength - 1];
  6977. if (((c >= '0') && (c <= '9')) || (c == '.'))
  6978. str.RemoveFromEnd(1);
  6979. else
  6980. break;
  6981. }
  6982. bool hasLinux = false;
  6983. for (auto elem : str.Split('-'))
  6984. {
  6985. if (elem == "linux")
  6986. hasLinux = true;
  6987. else if (elem == "windows")
  6988. return BfPlatformType_Windows;
  6989. else if (elem == "macosx")
  6990. return BfPlatformType_macOS;
  6991. else if (elem == "ios")
  6992. return BfPlatformType_iOS;
  6993. else if ((elem == "android") || (elem == "androideabi"))
  6994. return BfPlatformType_Android;
  6995. }
  6996. if (hasLinux)
  6997. return BfPlatformType_Linux;
  6998. return BfPlatformType_Unknown;
  6999. }
  7000. BF_EXPORT void BF_CALLTYPE BfCompiler_SetOptions(BfCompiler* bfCompiler, BfProject* hotProject, int hotIdx,
  7001. const char* targetTriple, int toolsetType, int simdSetting, int allocStackCount, int maxWorkerThreads,
  7002. BfCompilerOptionFlags optionFlags, char* mallocLinkName, char* freeLinkName)
  7003. {
  7004. BfLogSys(bfCompiler->mSystem, "BfCompiler_SetOptions\n");
  7005. //printf("BfCompiler_SetOptions Threads:%d\n", maxWorkerThreads);
  7006. auto options = &bfCompiler->mOptions;
  7007. options->mErrorString.Clear();
  7008. options->mHotProject = hotProject;
  7009. options->mHotCompileIdx = hotIdx;
  7010. options->mTargetTriple = targetTriple;
  7011. if (options->mTargetTriple.StartsWith("x86_64-"))
  7012. options->mMachineType = BfMachineType_x64;
  7013. else if (options->mTargetTriple.StartsWith("i686-"))
  7014. options->mMachineType = BfMachineType_x86;
  7015. else if ((options->mTargetTriple.StartsWith("arm64")) || (options->mTargetTriple.StartsWith("aarch64")))
  7016. options->mMachineType = BfMachineType_AArch64;
  7017. else if (options->mTargetTriple.StartsWith("armv"))
  7018. options->mMachineType = BfMachineType_ARM;
  7019. else
  7020. options->mMachineType = BfMachineType_x64; // Default
  7021. options->mPlatformType = GetPlatform(options->mTargetTriple);
  7022. options->mCLongSize = 4;
  7023. if ((options->mMachineType == BfMachineType_AArch64) || (options->mMachineType == BfMachineType_x64))
  7024. {
  7025. if ((options->mPlatformType == BfPlatformType_macOS) || (options->mPlatformType == BfPlatformType_iOS) ||
  7026. (options->mPlatformType == BfPlatformType_Linux) || (options->mPlatformType == BfPlatformType_Android))
  7027. options->mCLongSize = 8;
  7028. }
  7029. bfCompiler->mCodeGen.SetMaxThreads(maxWorkerThreads);
  7030. if (!bfCompiler->mIsResolveOnly)
  7031. {
  7032. bool allowHotSwapping = (optionFlags & BfCompilerOptionFlag_EnableHotSwapping) != 0;
  7033. bool emitDebugInfo = (optionFlags & BfCompilerOptionFlag_EmitDebugInfo) != 0;
  7034. // These settings only matter for code generation, they are not applicable for resolveOnly
  7035. options->mCompileOnDemandKind = BfCompileOnDemandKind_ResolveUnused;
  7036. //options->mCompileOnDemandKind = BfCompileOnDemandKind_AlwaysInclude;
  7037. options->mToolsetType = (BfToolsetType)toolsetType;
  7038. options->mSIMDSetting = (BfSIMDSetting)simdSetting;
  7039. options->mIncrementalBuild = (optionFlags & BfCompilerOptionFlag_IncrementalBuild) != 0;
  7040. options->mWriteIR = (optionFlags & BfCompilerOptionFlag_WriteIR) != 0;
  7041. options->mGenerateObj = (optionFlags & BfCompilerOptionFlag_GenerateOBJ) != 0;
  7042. options->mGenerateBitcode = (optionFlags & BfCompilerOptionFlag_GenerateBitcode) != 0;
  7043. options->mNoFramePointerElim = (optionFlags & BfCompilerOptionFlag_NoFramePointerElim) != 0;
  7044. options->mInitLocalVariables = (optionFlags & BfCompilerOptionFlag_ClearLocalVars) != 0;
  7045. options->mRuntimeChecks = (optionFlags & BfCompilerOptionFlag_RuntimeChecks) != 0;
  7046. options->mEmitDynamicCastCheck = (optionFlags & BfCompilerOptionFlag_EmitDynamicCastCheck) != 0;
  7047. options->mObjectHasDebugFlags = (optionFlags & BfCompilerOptionFlag_EnableObjectDebugFlags) != 0;
  7048. options->mEnableRealtimeLeakCheck = ((optionFlags & BfCompilerOptionFlag_EnableRealtimeLeakCheck) != 0) && options->mObjectHasDebugFlags;
  7049. options->mDebugAlloc = ((optionFlags & BfCompilerOptionFlag_DebugAlloc) != 0) || options->mEnableRealtimeLeakCheck;
  7050. options->mOmitDebugHelpers = (optionFlags & BfCompilerOptionFlag_OmitDebugHelpers) != 0;
  7051. #ifdef _WINDOWS
  7052. // if (options->mToolsetType == BfToolsetType_GNU)
  7053. // {
  7054. // options->mErrorString = "Toolset 'GNU' is not available on this platform. Consider changing 'Workspace/General/Toolset'.";
  7055. // }
  7056. #else
  7057. // if (options->mToolsetType == BfToolsetType_Microsoft)
  7058. // {
  7059. // options->mErrorString = "Toolset 'Microsoft' is not available on this platform. Consider changing 'Workspace/General/Toolset'.";
  7060. // }
  7061. BF_ASSERT(!options->mEnableRealtimeLeakCheck);
  7062. #endif
  7063. options->mEmitObjectAccessCheck = (optionFlags & BfCompilerOptionFlag_EmitDebugInfo) != 0;
  7064. options->mAllocStackCount = allocStackCount;
  7065. if (hotProject != NULL)
  7066. {
  7067. String errorName;
  7068. if (options->mAllowHotSwapping != allowHotSwapping)
  7069. errorName = "Hot Compilation Enabled";
  7070. else if (options->mMallocLinkName != mallocLinkName)
  7071. errorName = "Malloc";
  7072. else if (options->mFreeLinkName != freeLinkName)
  7073. errorName = "Free";
  7074. if (!options->mEmitDebugInfo)
  7075. {
  7076. 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'.";
  7077. }
  7078. else if (!errorName.IsEmpty())
  7079. {
  7080. options->mErrorString = StrFormat("Unable to change option '%s' during hot compilation", errorName.c_str());
  7081. }
  7082. }
  7083. else
  7084. {
  7085. options->mAllowHotSwapping = allowHotSwapping;
  7086. options->mHasVDataExtender = options->mAllowHotSwapping;
  7087. options->mMallocLinkName = mallocLinkName;
  7088. options->mFreeLinkName = freeLinkName;
  7089. options->mEmitDebugInfo = emitDebugInfo;
  7090. options->mEmitLineInfo = (optionFlags & BfCompilerOptionFlag_EmitLineInfo) != 0;;
  7091. options->mEnableCustodian = (optionFlags & BfCompilerOptionFlag_EnableCustodian) != 0;
  7092. options->mEnableSideStack = (optionFlags & BfCompilerOptionFlag_EnableSideStack) != 0;
  7093. }
  7094. }
  7095. else
  7096. {
  7097. options->mCompileOnDemandKind = BfCompileOnDemandKind_AlwaysInclude;
  7098. options->mAllowHotSwapping = false;
  7099. options->mObjectHasDebugFlags = false;
  7100. options->mEnableRealtimeLeakCheck = false;
  7101. options->mEmitObjectAccessCheck = false;
  7102. options->mEmitDynamicCastCheck = false;
  7103. options->mRuntimeChecks = (optionFlags & BfCompilerOptionFlag_RuntimeChecks) != 0;
  7104. }
  7105. }
  7106. BF_EXPORT void BF_CALLTYPE BfCompiler_ForceRebuild(BfCompiler* bfCompiler)
  7107. {
  7108. bfCompiler->mOptions.mForceRebuildIdx++;
  7109. }