2
0

BfCompiler.cpp 349 KB

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