BfCompiler.cpp 339 KB

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