123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298 |
- #pragma warning(disable:4996)
- #pragma warning(push)
- #pragma warning(disable:4800)
- #pragma warning(disable:4244)
- #pragma warning(disable:4141)
- #pragma warning(disable:4624)
- #pragma warning(disable:4146)
- #pragma warning(disable:4267)
- #pragma warning(disable:4291)
- #include "BeefySysLib/util/AllocDebug.h"
- #include "llvm/Support/Compiler.h"
- #include "llvm/Support/Compiler.h"
- #include "llvm/IR/IRBuilder.h"
- #include "llvm/IR/Type.h"
- #include "llvm/IR/DIBuilder.h"
- #include "llvm/IR/DebugInfo.h"
- #include "llvm/IR/Argument.h"
- #include "llvm/IR/Constants.h"
- #include "llvm/IR/Module.h"
- #include "BfCompiler.h"
- #include "BfSystem.h"
- #include "BfParser.h"
- #include "BfReducer.h"
- #include "BfExprEvaluator.h"
- #include "../Backend/BeLibManger.h"
- #include <fcntl.h>
- #include "BfConstResolver.h"
- #include "BfMangler.h"
- #include "BfDemangler.h"
- #include "BeefySysLib/util/PerfTimer.h"
- #include "BfSourceClassifier.h"
- #include "BfAutoComplete.h"
- #include "BfResolvePass.h"
- #include "BeefySysLib/util/BeefPerf.h"
- #include "BeefySysLib/util/ZipFile.h"
- #include "../LLVMUtils.h"
- #include "BfNamespaceVisitor.h"
- #include "CeMachine.h"
- #include "CeDebugger.h"
- #include "BfDefBuilder.h"
- #pragma warning(pop)
- namespace llvm
- {
- extern bool DebugFlag;
- }
- #define SPLIT_CONTEXTS
- Beefy::BfCompiler* gBfCompiler = NULL;
- void pt(llvm::Type* t)
- {
- //Beefy::OutputDebugStrF("pv((llvm::Type*)%p)\n", t);
- Beefy::debug_ostream os;
- t->print(os);
- os << "\n";
- os << " isSized: " << t->isSized() << "\n";
- os.flush();
- // if (auto pointerType = llvm::dyn_cast<llvm::PointerType>(t))
- // {
- // Beefy::OutputDebugStrF("Element: ");
- // pt(pointerType->getElementType());
- // }
- }
- void ppt(llvm::Type* t)
- {
- auto pointerType = llvm::dyn_cast<llvm::PointerType>(t);
- if (pointerType == NULL)
- {
- Beefy::OutputDebugStrF("Not a pointer type");
- return;
- }
- //pt(pointerType->getElementType());
- }
- void pt(llvm::DINode* t)
- {
- Beefy::debug_ostream os;
- t->print(os);
- os << "\n";
- os.flush();
- }
- void pt(llvm::Value* v)
- {
- pt(v->getType());
- }
- void pv(const llvm::Value* v)
- {
- Beefy::debug_ostream os;
- v->print(os);
- os << "\n";
- os.flush();
- pt(v->getType());
- }
- void ppt(llvm::Value* v)
- {
- ppt(v->getType());
- }
- void pmd(llvm::Metadata* md)
- {
- Beefy::debug_ostream os;
- md->print(os);
- os << "\n";
- os.flush();
- }
- void pdl(llvm::DebugLoc& dl)
- {
- Beefy::debug_ostream os;
- dl.print(os);
- os << "\n";
- os.flush();
- }
- void pm(llvm::Module* module)
- {
- Beefy::debug_ostream os;
- module->print(os, NULL);
- os << "\n";
- os.flush();
- }
- void PrintUsers(llvm::MDNode* md)
- {
- /*Beefy::debug_ostream os;
- //auto val = llvm::ReplaceableMetadataImpl::get(*md);
- auto val = md->Context.getReplaceableUses();
- if (val == NULL)
- {
- os << "Not replaceable\n";
- }
- else
- {
- //md->print(os);
- typedef std::pair<void *, std::pair<llvm::MetadataTracking::OwnerTy, uint64_t>> UseTy;
- llvm::SmallVector<UseTy, 8> Uses(val->UseMap.begin(), val->UseMap.end());
- std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) {
- return L.second.second < R.second.second;
- });
- for (const auto &Pair : Uses)
- {
- auto Owner = Pair.second.first;
- os << Beefy::StrFormat(" %d %p %d\n", Pair.second.first.isNull(), Pair.first, Pair.second.second, Pair).c_str();
- }
- os << "\n";
- }
- os.flush();*/
- }
- void ptbf(Beefy::BfType* bfType)
- {
- Beefy::OutputDebugStrF("%s\n", bfType->GetModule()->TypeToString(bfType).c_str());
- }
- void pt(const Beefy::BfTypedValue& val)
- {
- Beefy::OutputDebugStrF("%s\n", val.mType->GetModule()->TypeToString(val.mType).c_str());
- }
- void pt(llvm::SmallVectorImpl<llvm::Value*>& llvmArgs)
- {
- Beefy::debug_ostream os;
- for (int i = 0; i < (int)llvmArgs.size(); i++)
- {
- if (i > 0)
- os << ", ";
- llvmArgs[i]->getType()->print(os);
- }
- os << "\n";
- os.flush();
- }
- void PrintUsers(llvm::Value* v)
- {
- for (auto user : v->users())
- {
- pt(user);
- }
- }
- /*void PrintFunc(Beefy::BfMethodInstance* methodInstance)
- {
- Beefy::debug_ostream os;
- methodInstance->mIRFunction.mLLVMValue->print(os);
- os << "\n";
- os.flush();
- }*/
- USING_NS_BF;
- using namespace llvm;
- int Beefy::BfWorkListEntry::sCurReqId = 0;
- GlobalVariable* AllocGlobalVariable(Module &M, Type *Ty, bool isConstant,
- GlobalValue::LinkageTypes Linkage, Constant *Initializer,
- const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
- GlobalValue::ThreadLocalMode tlm = GlobalValue::NotThreadLocal, unsigned AddressSpace = 0,
- bool isExternallyInitialized = false);
- #include "BeefySysLib/util/AllocDebug.h"
- //////////////////////////////////////////////////////////////////////////
- BfCompiler::HotData::~HotData()
- {
- for (auto& kv : mMethodMap)
- {
- auto hotMethod = kv.mValue;
- hotMethod->Clear();
- }
- for (auto& kv : mThisType)
- kv.mValue->Deref();
- for (auto& kv : mAllocation)
- kv.mValue->Deref();
- for (auto& kv : mDevirtualizedMethods)
- kv.mValue->Deref();
- for (auto& kv : mFuncPtrs)
- kv.mValue->Deref();
- for (auto& kv : mVirtualDecls)
- kv.mValue->Deref();
- for (auto& kv : mInnerMethods)
- kv.mValue->Deref();
- for (auto& kv : mMethodMap)
- kv.mValue->Deref();
- }
- template <typename TDict>
- static void DeleteUnused(TDict& dict)
- {
- auto itr = dict.begin();
- while (itr != dict.end())
- {
- auto val = itr->mValue;
- BF_ASSERT(val->mRefCount >= 1);
- if (val->mRefCount == 1)
- {
- val->Deref();
- itr = dict.Remove(itr);
- }
- else
- ++itr;
- }
- }
- template <typename TDict, typename TElement>
- static typename TDict::value_type AllocFromMap(TDict& dict, TElement* elem)
- {
- typename TDict::value_type* valuePtr;
- if (dict.TryAdd(elem, NULL, &valuePtr))
- {
- auto val = new typename std::remove_pointer<typename TDict::value_type>::type(elem);
- val->mRefCount++;
- *valuePtr = val;
- }
- return *valuePtr;
- }
- void BfCompiler::HotData::ClearUnused(bool isHotCompile)
- {
- BP_ZONE("BfCompiler::HotData::ClearUnused");
- DeleteUnused(mThisType);
- DeleteUnused(mAllocation);
- DeleteUnused(mDevirtualizedMethods);
- DeleteUnused(mVirtualDecls);
- DeleteUnused(mInnerMethods);
- if (isHotCompile)
- {
- // We need to keep all function pointer references ever, since we can't tell if we still reference them or not
- DeleteUnused(mFuncPtrs);
- }
- }
- BfHotThisType* BfCompiler::HotData::GetThisType(BfHotTypeVersion* hotVersion)
- {
- return AllocFromMap(mThisType, hotVersion);
- }
- BfHotAllocation* BfCompiler::HotData::GetAllocation(BfHotTypeVersion* hotVersion)
- {
- return AllocFromMap(mAllocation, hotVersion);
- }
- BfHotDevirtualizedMethod* BfCompiler::HotData::GetDevirtualizedMethod(BfHotMethod* hotMethod)
- {
- return AllocFromMap(mDevirtualizedMethods, hotMethod);
- }
- BfHotFunctionReference* BfCompiler::HotData::GetFunctionReference(BfHotMethod* hotMethod)
- {
- return AllocFromMap(mFuncPtrs, hotMethod);
- }
- BfHotInnerMethod* BfCompiler::HotData::GetInnerMethod(BfHotMethod* hotMethod)
- {
- return AllocFromMap(mInnerMethods, hotMethod);
- }
- BfHotVirtualDeclaration* BfCompiler::HotData::GetVirtualDeclaration(BfHotMethod* hotMethod)
- {
- return AllocFromMap(mVirtualDecls, hotMethod);
- }
- BfCompiler::HotState::~HotState()
- {
- }
- bool BfCompiler::HotState::HasPendingChanges(BfTypeInstance* type)
- {
- return (type->mHotTypeData != NULL) && (type->mHotTypeData->mPendingDataChange);
- }
- void BfCompiler::HotState::RemovePendingChanges(BfTypeInstance* type)
- {
- BF_ASSERT(type->mHotTypeData->mPendingDataChange);
- if (!type->mHotTypeData->mPendingDataChange)
- return;
- type->mHotTypeData->mPendingDataChange = false;
- bool didRemove = mPendingDataChanges.Remove(type->mTypeId);
- BF_ASSERT(didRemove);
- }
- BfCompiler::HotResolveData::~HotResolveData()
- {
- for (auto hotMethod : mActiveMethods)
- hotMethod->Deref();
- for (auto kv : mReachableMethods)
- kv.mKey->Deref();
- }
- //////////////////////////////////////////////////////////////////////////
- BfCompiler::BfCompiler(BfSystem* bfSystem, bool isResolveOnly)
- {
- memset(&mStats, 0, sizeof(mStats));
- mCompletionPct = 0;
- mCanceling = false;
- mHasRequiredTypes = false;
- mNeedsFullRefresh = false;
- mFastFinish = false;
- mExtraCompileRequested = false;
- mHasQueuedTypeRebuilds = false;
- mIsResolveOnly = isResolveOnly;
- mResolvePassData = NULL;
- mPassInstance = NULL;
- mRevision = 0;
- mUniqueId = 0;
- mLastRevisionAborted = false;
- gBfCompiler = this;
- mSystem = bfSystem;
- mCurTypeId = 1;
- mTypeInitCount = 0;
- //mMaxInterfaceSlots = 16;
- mMaxInterfaceSlots = -1;
- mInterfaceSlotCountChanged = false;
- mLastHadComptimeRebuilds = false;
- mHasComptimeRebuilds = false;
- mDepsMayHaveDeletedTypes = false;
- mHSPreserveIdx = 0;
- mCompileLogFP = NULL;
- mWantsDeferMethodDecls = false;
- mHadCancel = false;
- mCompileState = CompileState_None;
- //mMaxInterfaceSlots = 4;
- mHotData = NULL;
- mHotState = NULL;
- mHotResolveData = NULL;
- mBfObjectTypeDef = NULL;
- mChar32TypeDef = NULL;
- mFloatTypeDef = NULL;
- mDoubleTypeDef = NULL;
- mMathTypeDef = NULL;
- mArray1TypeDef = NULL;
- mArray2TypeDef = NULL;
- mArray3TypeDef = NULL;
- mArray4TypeDef = NULL;
- mSpanTypeDef = NULL;
- mRangeTypeDef = NULL;
- mClosedRangeTypeDef = NULL;
- mIndexTypeDef = NULL;
- mIndexRangeTypeDef = NULL;
- mAttributeTypeDef = NULL;
- mAttributeUsageAttributeTypeDef = NULL;
- mClassVDataTypeDef = NULL;
- mCLinkAttributeTypeDef = NULL;
- mImportAttributeTypeDef = NULL;
- mExportAttributeTypeDef = NULL;
- mCReprAttributeTypeDef = NULL;
- mUnderlyingArrayAttributeTypeDef = NULL;
- mAlignAttributeTypeDef = NULL;
- mAllowDuplicatesAttributeTypeDef = NULL;
- mNoDiscardAttributeTypeDef = NULL;
- mDisableChecksAttributeTypeDef = NULL;
- mDisableObjectAccessChecksAttributeTypeDef = NULL;
- mDbgRawAllocDataTypeDef = NULL;
- mDeferredCallTypeDef = NULL;
- mDelegateTypeDef = NULL;
- mFunctionTypeDef = NULL;
- mActionTypeDef = NULL;
- mEnumTypeDef = NULL;
- mFriendAttributeTypeDef = NULL;
- mComptimeAttributeTypeDef = NULL;
- mIntrinsicAttributeTypeDef = NULL;
- mConstEvalAttributeTypeDef = NULL;
- mNoExtensionAttributeTypeDef = NULL;
- mCheckedAttributeTypeDef = NULL;
- mUncheckedAttributeTypeDef = NULL;
- mGCTypeDef = NULL;
- mGenericIEnumerableTypeDef = NULL;
- mGenericIEnumeratorTypeDef = NULL;
- mGenericIRefEnumeratorTypeDef = NULL;
- mInlineAttributeTypeDef = NULL;
- mThreadTypeDef = NULL;
- mInternalTypeDef = NULL;
- mPlatformTypeDef = NULL;
- mCompilerTypeDef = NULL;
- mCompilerGeneratorTypeDef = NULL;
- mDiagnosticsDebugTypeDef = NULL;
- mIDisposableTypeDef = NULL;
- mIIntegerTypeDef = NULL;
- mIPrintableTypeDef = NULL;
- mIHashableTypeDef = NULL;
- mIComptimeTypeApply = NULL;
- mIComptimeMethodApply = NULL;
- mIOnTypeInitTypeDef = NULL;
- mIOnTypeDoneTypeDef = NULL;
- mIOnFieldInitTypeDef = NULL;
- mIOnMethodInitTypeDef = NULL;
- mLinkNameAttributeTypeDef = NULL;
- mCallingConventionAttributeTypeDef = NULL;
- mMethodRefTypeDef = NULL;
- mNullableTypeDef = NULL;
- mOrderedAttributeTypeDef = NULL;
- mPointerTTypeDef = NULL;
- mPointerTypeDef = NULL;
- mReflectTypeIdTypeDef = NULL;
- mReflectArrayType = NULL;
- mReflectGenericParamType = NULL;
- mReflectFieldDataDef = NULL;
- mReflectFieldSplatDataDef = NULL;
- mReflectMethodDataDef = NULL;
- mReflectParamDataDef = NULL;
- mReflectInterfaceDataDef = NULL;
- mReflectPointerType = NULL;
- mReflectRefType = NULL;
- mReflectSizedArrayType = NULL;
- mReflectConstExprType = NULL;
- mReflectSpecializedGenericType = NULL;
- mReflectTypeInstanceTypeDef = NULL;
- mReflectUnspecializedGenericType = NULL;
- mReflectFieldInfoTypeDef = NULL;
- mReflectMethodInfoTypeDef = NULL;
- mSizedArrayTypeDef = NULL;
- mStaticInitAfterAttributeTypeDef = NULL;
- mStaticInitPriorityAttributeTypeDef = NULL;
- mStringTypeDef = NULL;
- mStringViewTypeDef = NULL;
- mThreadStaticAttributeTypeDef = NULL;
- mTypeTypeDeclDef = NULL;
- mTypeTypeDef = NULL;
- mUnboundAttributeTypeDef = NULL;
- mValueTypeTypeDef = NULL;
- mTupleTypeDef = NULL;
- mResultTypeDef = NULL;
- mObsoleteAttributeTypeDef = NULL;
- mErrorAttributeTypeDef = NULL;
- mWarnAttributeTypeDef = NULL;
- mConstSkipAttributeTypeDef = NULL;
- mIgnoreErrorsAttributeTypeDef = NULL;
- mReflectAttributeTypeDef = NULL;
- mOnCompileAttributeTypeDef = NULL;
- mLastAutocompleteModule = NULL;
- mContext = new BfContext(this);
- mCeMachine = new CeMachine(this);
- mCurCEExecuteId = -1;
- mLastMidCompileRefreshRevision = -1;
- }
- BfCompiler::~BfCompiler()
- {
- delete mCeMachine;
- mCeMachine = NULL;
- delete mContext;
- delete mHotData;
- delete mHotState;
- delete mHotResolveData;
- }
- bool BfCompiler::IsTypeAccessible(BfType* checkType, BfProject* curProject)
- {
- if (checkType->IsBoxed())
- return IsTypeAccessible(((BfBoxedType*)checkType)->mElementType, curProject);
- BfTypeInstance* typeInst = checkType->ToTypeInstance();
- if (typeInst != NULL)
- {
- if (checkType->IsTuple())
- {
- for (auto&& fieldInst : typeInst->mFieldInstances)
- {
- if (!IsTypeAccessible(fieldInst.mResolvedType, curProject))
- return false;
- }
- }
- auto genericTypeInst = typeInst->ToGenericTypeInstance();
- if (genericTypeInst != NULL)
- {
- for (auto genericArg : genericTypeInst->mGenericTypeInfo->mTypeGenericArguments)
- if (!IsTypeAccessible(genericArg, curProject))
- return false;
- }
- return curProject->ContainsReference(typeInst->mTypeDef->mProject);
- }
- if (checkType->IsPointer())
- return IsTypeAccessible(((BfPointerType*)checkType)->mElementType, curProject);
- if (checkType->IsRef())
- return IsTypeAccessible(((BfPointerType*)checkType)->mElementType, curProject);
- return true;
- }
- bool BfCompiler::IsTypeUsed(BfType* checkType, BfProject* curProject, bool conservativeCheck)
- {
- if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
- return IsTypeAccessible(checkType, curProject);
- BfTypeInstance* typeInst = checkType->ToTypeInstance();
- if (typeInst != NULL)
- {
- //TODO: Why was this here?
- // if ((typeInst->mTypeDef->mProject != NULL) && (typeInst->mTypeDef->mProject != curProject))
- // {
- // if (typeInst->mTypeDef->mProject->mTargetType == BfTargetType_BeefDynLib)
- // return false;
- // }
- if (checkType->IsInterface())
- return typeInst->mIsReified || conservativeCheck;
- //TODO: We could check to see if this project has any reified specialized instances...
- if (checkType->IsUnspecializedType())
- return typeInst->mIsReified || conservativeCheck;
- if (checkType->IsTuple())
- {
- for (auto&& fieldInst : typeInst->mFieldInstances)
- {
- if (!IsTypeUsed(fieldInst.mResolvedType, curProject, true))
- return false;
- }
- }
- auto genericTypeInst = typeInst->ToGenericTypeInstance();
- if (genericTypeInst != NULL)
- {
- for (auto genericArg : genericTypeInst->mGenericTypeInfo->mTypeGenericArguments)
- if (!IsTypeUsed(genericArg, curProject, true))
- return false;
- }
- if (checkType->IsFunction())
- {
- // These don't get their own modules so just assume "always used" at this point
- return true;
- }
- auto module = typeInst->GetModule();
- if (module == NULL)
- return true;
- return curProject->mUsedModules.Contains(module);
- }
- if (checkType->IsPointer())
- return IsTypeUsed(((BfPointerType*)checkType)->mElementType, curProject);
- if (checkType->IsRef())
- return IsTypeUsed(((BfRefType*)checkType)->mElementType, curProject);
- return true;
- }
- bool BfCompiler::IsModuleAccessible(BfModule* module, BfProject* curProject)
- {
- for (auto checkType : module->mOwnedTypeInstances)
- {
- if (!IsTypeAccessible(checkType, curProject))
- return false;
- }
- return curProject->ContainsReference(module->mProject);
- }
- void BfCompiler::FixVDataHash(BfModule* bfModule)
- {
- // We recreate the actual vdata hash now that we're done creating new string literals
- /*for (auto context : mContexts)
- HASH128_MIXIN(bfModule->mDataHash, bfModule->mHighestUsedStringId);*/
- }
- void BfCompiler::CheckModuleStringRefs(BfModule* module, BfVDataModule* vdataModule, int lastModuleRevision, HashSet<int>& foundStringIds, HashSet<int>& dllNameSet, Array<BfMethodInstance*>& dllMethods, Array<BfCompiler::StringValueEntry>& stringValueEntries)
- {
- for (int stringId : module->mStringPoolRefs)
- {
- if (foundStringIds.Add(stringId))
- {
- BfStringPoolEntry& stringPoolEntry = module->mContext->mStringObjectIdMap[stringId];
- if (IsHotCompile())
- {
- if (vdataModule->mDefinedStrings.Contains(stringId))
- continue;
- }
- StringValueEntry stringEntry;
- stringEntry.mId = stringId;
- vdataModule->mDefinedStrings.Add(stringId);
- stringEntry.mStringVal = vdataModule->CreateStringObjectValue(stringPoolEntry.mString, stringId, true);
- stringValueEntries.Add(stringEntry);
- CompileLog("String %d %s\n", stringId, stringPoolEntry.mString.c_str());
- }
- }
- for (auto dllNameId : module->mImportFileNames)
- dllNameSet.Add(dllNameId);
- for (auto& dllImportEntry : module->mDllImportEntries)
- dllMethods.push_back(dllImportEntry.mMethodInstance);
- auto altModule = module->mNextAltModule;
- while (altModule != NULL)
- {
- CheckModuleStringRefs(altModule, vdataModule, lastModuleRevision, foundStringIds, dllNameSet, dllMethods, stringValueEntries);
- altModule = altModule->mNextAltModule;
- }
- for (auto& specModulePair : module->mSpecializedMethodModules)
- CheckModuleStringRefs(specModulePair.mValue, vdataModule, lastModuleRevision, foundStringIds, dllNameSet, dllMethods, stringValueEntries);
- }
- void BfCompiler::HashModuleVData(BfModule* module, HashContext& vdataHash)
- {
- BP_ZONE("BfCompiler::HashModuleVData");
- if (module->mStringPoolRefs.size() > 0)
- {
- module->mStringPoolRefs.Sort([](int lhs, int rhs) { return lhs < rhs; });
- vdataHash.Mixin(&module->mStringPoolRefs[0], (int)module->mStringPoolRefs.size() * (int)sizeof(int));
- }
- if (module->mImportFileNames.size() > 0)
- {
- module->mImportFileNames.Sort([](int lhs, int rhs) { return lhs < rhs; });
- vdataHash.Mixin(&module->mImportFileNames[0], (int)module->mImportFileNames.size() * (int)sizeof(int));
- }
- auto altModule = module->mNextAltModule;
- while (altModule != NULL)
- {
- HashModuleVData(altModule, vdataHash);
- altModule = altModule->mNextAltModule;
- }
- for (auto& specModulePair : module->mSpecializedMethodModules)
- {
- HashModuleVData(specModulePair.mValue, vdataHash);
- }
- }
- BfIRFunction BfCompiler::CreateLoadSharedLibraries(BfVDataModule* bfModule, Array<BfMethodInstance*>& dllMethods)
- {
- BfIRType nullPtrType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr));
- BfIRType nullPtrPtrType = bfModule->mBfIRBuilder->MapType(bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr)));
- BfIRType voidType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_None));
- SmallVector<BfIRType, 2> paramTypes;
- auto loadSharedLibrariesFuncType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes, false);
- auto loadSharedLibFunc = bfModule->mBfIRBuilder->CreateFunction(loadSharedLibrariesFuncType, BfIRLinkageType_External, "BfLoadSharedLibraries");
- bfModule->SetupIRMethod(NULL, loadSharedLibFunc, false);
- bfModule->mBfIRBuilder->SetActiveFunction(loadSharedLibFunc);
- auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
- bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
- HashSet<int> dllNameSet;
- auto internalType = bfModule->ResolveTypeDef(mInternalTypeDef);
- bfModule->PopulateType(internalType);
- auto getSharedProcAddressInstance = bfModule->GetMethodByName(internalType->ToTypeInstance(), "GetSharedProcAddressInto");
- auto loadSharedLibraryProc = bfModule->GetMethodByName(internalType->ToTypeInstance(), "LoadSharedLibraryInto");
- BF_ASSERT(getSharedProcAddressInstance);
- BF_ASSERT(loadSharedLibraryProc);
- if (!getSharedProcAddressInstance)
- {
- bfModule->Fail("Missing Internal.GetSharedProcAddressInto");
- return loadSharedLibFunc;
- }
- if (!loadSharedLibraryProc)
- {
- bfModule->Fail("Missing Internal.LoadSharedLibraryInto");
- return loadSharedLibFunc;
- }
- Dictionary<int, BfIRValue> dllHandleMap;
- for (auto methodInstance : dllMethods)
- {
- auto typeInstance = methodInstance->GetOwner();
- auto methodDef = methodInstance->mMethodDef;
- BF_ASSERT(methodInstance->GetCustomAttributes() != NULL);
- for (auto customAttr : methodInstance->GetCustomAttributes()->mAttributes)
- {
- if (customAttr.mType->mTypeDef->mFullName.ToString() == "System.ImportAttribute")
- {
- bool doCLink = false;
- bool undecorated = false;
- BfCallingConvention callingConvention = methodDef->mCallingConvention;
- for (auto fieldSet : customAttr.mSetField)
- {
- BfFieldDef* fieldDef = fieldSet.mFieldRef;
- if (fieldDef->mName == "CLink")
- {
- auto constant = typeInstance->mConstHolder->GetConstant(fieldSet.mParam.mValue);
- if (constant != NULL)
- doCLink = constant->mBool;
- }
- if (fieldDef->mName == "Undecorated")
- {
- auto constant = typeInstance->mConstHolder->GetConstant(fieldSet.mParam.mValue);
- if (constant != NULL)
- undecorated = constant->mBool;
- }
- if (fieldDef->mName == "CallingConvention")
- {
- auto constant = typeInstance->mConstHolder->GetConstant(fieldSet.mParam.mValue);
- if (constant != NULL)
- {
- int callingConventionVal = (int)constant->mInt32;
- if ((callingConventionVal == 3) || (callingConventionVal == 1))
- callingConvention = BfCallingConvention_Stdcall;
- else if (callingConventionVal == 2)
- callingConvention = BfCallingConvention_Cdecl;
- }
- }
- }
- if (customAttr.mCtorArgs.size() == 1)
- {
- auto fileNameArg = customAttr.mCtorArgs[0];
- int strNum = 0;
- auto constant = typeInstance->mConstHolder->GetConstant(fileNameArg);
- if (constant != NULL)
- {
- if (constant->IsNull())
- continue; // Invalid
- strNum = constant->mInt32;
- }
- else
- {
- strNum = bfModule->GetStringPoolIdx(fileNameArg, typeInstance->mConstHolder);
- }
- BfIRValue dllHandleVar;
- if (!dllHandleMap.TryGetValue(strNum, &dllHandleVar))
- {
- String dllHandleName = StrFormat("bf_hs_preserve@dllHandle%d", strNum);
- dllHandleVar = bfModule->mBfIRBuilder->CreateGlobalVariable(nullPtrType, false, BfIRLinkageType_External,
- bfModule->GetDefaultValue(bfModule->GetPrimitiveType(BfTypeCode_NullPtr)), dllHandleName);
- BfIRValue namePtr = bfModule->GetStringCharPtr(strNum);
- SmallVector<BfIRValue, 2> args;
- args.push_back(namePtr);
- args.push_back(dllHandleVar);
- BfIRValue dllHandleValue = bfModule->mBfIRBuilder->CreateCall(loadSharedLibraryProc.mFunc, args);
- dllHandleMap[strNum] = dllHandleVar;
- }
- String methodImportName;
- if (undecorated)
- {
- methodImportName = methodInstance->mMethodDef->mName;
- }
- else if (doCLink)
- {
- methodImportName = methodInstance->mMethodDef->mName;
- if ((mSystem->mPtrSize == 4) && (callingConvention == BfCallingConvention_Stdcall))
- {
- int argSize = (int)methodDef->mParams.size() * mSystem->mPtrSize;
- methodImportName = StrFormat("_%s$%d", methodImportName.c_str(), argSize);
- }
- }
- else
- BfMangler::Mangle(methodImportName, GetMangleKind(), methodInstance);
- BfIRValue methodNameValue = bfModule->mBfIRBuilder->CreateGlobalStringPtr(methodImportName);
- //auto moduleMethodInstance = bfModule->ReferenceExternalMethodInstance(methodInstance);
- //auto globalVarPtr = bfModule->mBfIRBuilder->CreateBitCast(moduleMethodInstance.mFunc, nullPtrPtrType);
- auto func = bfModule->CreateDllImportGlobalVar(methodInstance, false);
- auto globalVarPtr = bfModule->mBfIRBuilder->CreateBitCast(func, nullPtrPtrType);
- BfSizedVector<BfIRValue, 2> args;
- args.push_back(bfModule->mBfIRBuilder->CreateLoad(dllHandleVar));
- args.push_back(methodNameValue);
- args.push_back(globalVarPtr);
- BfIRValue dllFuncValVoidPtr = bfModule->mBfIRBuilder->CreateCall(getSharedProcAddressInstance.mFunc, args);
- }
- }
- }
- }
- bfModule->mBfIRBuilder->CreateRetVoid();
- return loadSharedLibFunc;
- }
- void BfCompiler::GetTestMethods(BfVDataModule* bfModule, Array<TestMethod>& testMethods, HashContext& vdataHashCtx)
- {
- vdataHashCtx.Mixin(0xBEEF0001); // Marker
- auto _CheckMethod = [&](BfTypeInstance* typeInstance, BfMethodInstance* methodInstance)
- {
- auto project = methodInstance->mMethodDef->mDeclaringType->mProject;
- if (project->mTargetType != BfTargetType_BeefTest)
- return;
- if (project != bfModule->mProject)
- return;
- bool isTest = false;
- if ((methodInstance->GetCustomAttributes() != NULL) &&
- (methodInstance->GetCustomAttributes()->Contains(mTestAttributeTypeDef)))
- isTest = true;
- if (!isTest)
- return;
- if (methodInstance->mIsUnspecialized)
- {
- if (!typeInstance->IsSpecializedType())
- {
- bfModule->Fail(StrFormat("Method '%s' cannot be used for testing because it's generic", bfModule->MethodToString(methodInstance).c_str()),
- methodInstance->mMethodDef->GetRefNode());
- }
- bfModule->mHadBuildError = true;
- return;
- }
- if (!methodInstance->mMethodDef->mIsStatic)
- {
- if (!typeInstance->IsSpecializedType())
- {
- bfModule->Fail(StrFormat("Method '%s' cannot be used for testing because it's not static", bfModule->MethodToString(methodInstance).c_str()),
- methodInstance->mMethodDef->GetRefNode());
- }
- bfModule->mHadBuildError = true;
- return;
- }
- if (methodInstance->GetParamCount() > 0)
- {
- if ((methodInstance->GetParamInitializer(0) == NULL) &&
- (methodInstance->GetParamKind(0) != BfParamKind_Params))
- {
- if (!typeInstance->IsSpecializedType())
- {
- bfModule->Fail(StrFormat("Method '%s' cannot be used for testing because it contains parameters without defaults", bfModule->MethodToString(methodInstance).c_str()),
- methodInstance->mMethodDef->GetRefNode());
- }
- bfModule->mHadBuildError = true;
- return;
- }
- }
- BF_ASSERT(typeInstance->IsReified());
- TestMethod testMethod;
- testMethod.mMethodInstance = methodInstance;
- testMethods.Add(testMethod);
- BF_ASSERT_REL(!typeInstance->mModule->mIsDeleting);
- if (!bfModule->mProject->mUsedModules.Contains(typeInstance->mModule))
- {
- bfModule->mProject->mUsedModules.Add(typeInstance->mModule);
- }
- vdataHashCtx.Mixin(methodInstance->GetOwner()->mTypeId);
- vdataHashCtx.Mixin(methodInstance->mMethodDef->mIdx);
- };
- for (auto type : mContext->mResolvedTypes)
- {
- auto typeInstance = type->ToTypeInstance();
- if (typeInstance == NULL)
- continue;
- if (typeInstance->IsUnspecializedType())
- continue;
- for (auto& methodInstanceGroup : typeInstance->mMethodInstanceGroups)
- {
- if (methodInstanceGroup.mDefault != NULL)
- _CheckMethod(typeInstance, methodInstanceGroup.mDefault);
- }
- }
- }
- void BfCompiler::EmitTestMethod(BfVDataModule* bfModule, Array<TestMethod>& testMethods, BfIRValue& retValue)
- {
- for (auto& testMethod : testMethods)
- {
- auto methodInstance = testMethod.mMethodInstance;
- auto typeInstance = methodInstance->GetOwner();
- testMethod.mName += bfModule->TypeToString(typeInstance);
- if (!testMethod.mName.IsEmpty())
- testMethod.mName += ".";
- testMethod.mName += methodInstance->mMethodDef->mName;
- testMethod.mName += "\t";
- auto testAttribute = methodInstance->GetCustomAttributes()->Get(mTestAttributeTypeDef);
- for (auto& field : testAttribute->mSetField)
- {
- auto constant = typeInstance->mConstHolder->GetConstant(field.mParam.mValue);
- if ((constant != NULL) && (constant->mTypeCode == BfTypeCode_Boolean) && (constant->mBool))
- {
- BfFieldDef* fieldDef = field.mFieldRef;
- if (fieldDef->mName == "ShouldFail")
- {
- testMethod.mName += "Sf\a";
- }
- else if (fieldDef->mName == "Profile")
- {
- testMethod.mName += "Pr\a";
- }
- else if (fieldDef->mName == "Ignore")
- {
- testMethod.mName += "Ig\a";
- }
- }
- else if ((constant != NULL) && (constant->mTypeCode == BfTypeCode_StringId))
- {
- BfFieldDef* fieldDef = field.mFieldRef;
- if (fieldDef->mName == "Name")
- {
- String* str = bfModule->GetStringPoolString(field.mParam.mValue, typeInstance->mConstHolder);
- if (str != NULL)
- {
- testMethod.mName += "Name";
- testMethod.mName += SlashString(*str, true, true, true);
- testMethod.mName += '\a';
- }
- }
- }
- }
- bfModule->UpdateSrcPos(methodInstance->mMethodDef->GetRefNode(), (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
- testMethod.mName += StrFormat("\t%s\t%d\t%d", bfModule->mCurFilePosition.mFileInstance->mParser->mFileName.c_str(), bfModule->mCurFilePosition.mCurLine, bfModule->mCurFilePosition.mCurColumn);
- }
- std::stable_sort(testMethods.begin(), testMethods.end(),
- [](const TestMethod& lhs, const TestMethod& rhs)
- {
- return lhs.mName < rhs.mName;
- });
- String methodData;
- for (int methodIdx = 0; methodIdx < (int)testMethods.size(); methodIdx++)
- {
- String& methodName = testMethods[methodIdx].mName;
- if (!methodData.IsEmpty())
- methodData += "\n";
- methodData += methodName;
- }
- //////////////////////////////////////////////////////////////////////////
- auto testInitMethod = bfModule->GetInternalMethod("Test_Init");
- auto testQueryMethod = bfModule->GetInternalMethod("Test_Query");
- auto testFinishMethod = bfModule->GetInternalMethod("Test_Finish");
- auto char8PtrType = bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_Char8));
- BfIRType strCharType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->GetPrimitiveType(BfTypeCode_Char8), (int)methodData.length() + 1);
- BfIRValue strConstant = bfModule->mBfIRBuilder->CreateConstString(methodData);
- BfIRValue gv = bfModule->mBfIRBuilder->CreateGlobalVariable(strCharType,
- true, BfIRLinkageType_External,
- strConstant, "__bfTestData");
- BfIRValue strPtrVal = bfModule->mBfIRBuilder->CreateBitCast(gv, bfModule->mBfIRBuilder->MapType(char8PtrType));
- SizedArray<BfIRValue, 4> irArgs;
- irArgs.Add(strPtrVal);
- bfModule->mBfIRBuilder->CreateCall(testInitMethod.mFunc, irArgs);
- BfIRBlock testHeadBlock = bfModule->mBfIRBuilder->CreateBlock("testHead");
- BfIRBlock testEndBlock = bfModule->mBfIRBuilder->CreateBlock("testEnd");
- bfModule->mBfIRBuilder->CreateBr(testHeadBlock);
- bfModule->mBfIRBuilder->AddBlock(testHeadBlock);
- bfModule->mBfIRBuilder->SetInsertPoint(testHeadBlock);
- irArgs.clear();
- auto testVal = bfModule->mBfIRBuilder->CreateCall(testQueryMethod.mFunc, irArgs);
- auto switchVal = bfModule->mBfIRBuilder->CreateSwitch(testVal, testEndBlock, (int)testMethods.size());
- for (int methodIdx = 0; methodIdx < (int)testMethods.size(); methodIdx++)
- {
- auto methodInstance = testMethods[methodIdx].mMethodInstance;
- String& methodName = testMethods[methodIdx].mName;
- auto testBlock = bfModule->mBfIRBuilder->CreateBlock(StrFormat("test%d", methodIdx));
- bfModule->mBfIRBuilder->AddSwitchCase(switchVal, bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, methodIdx), testBlock);
- bfModule->mBfIRBuilder->AddBlock(testBlock);
- bfModule->mBfIRBuilder->SetInsertPoint(testBlock);
- auto moduleMethodInstance = bfModule->ReferenceExternalMethodInstance(methodInstance);
- irArgs.clear();
- if (methodInstance->GetParamCount() > 0)
- {
- if (methodInstance->GetParamKind(0) == BfParamKind_Params)
- {
- auto paramType = methodInstance->GetParamType(0);
- auto paramTypeInst = paramType->ToTypeInstance();
- BfTypedValue paramVal = BfTypedValue(bfModule->mBfIRBuilder->CreateAlloca(bfModule->mBfIRBuilder->MapTypeInst(paramTypeInst)), paramType);
- bfModule->InitTypeInst(paramVal, NULL, false, BfIRValue());
- //TODO: Assert 'length' var is at slot 1
- auto arrayBits = bfModule->mBfIRBuilder->CreateBitCast(paramVal.mValue, bfModule->mBfIRBuilder->MapType(paramTypeInst->mBaseType));
- auto addr = bfModule->mBfIRBuilder->CreateInBoundsGEP(arrayBits, 0, 1);
- auto storeInst = bfModule->mBfIRBuilder->CreateAlignedStore(bfModule->GetConstValue(0), addr, 4);
- irArgs.Add(paramVal.mValue);
- }
- else
- {
- for (int defaultIdx = 0; defaultIdx < (int)methodInstance->mDefaultValues.size(); defaultIdx++)
- {
- auto constHolder = methodInstance->GetOwner()->mConstHolder;
- auto defaultTypedValue = methodInstance->mDefaultValues[defaultIdx];
- auto defaultVal = bfModule->ConstantToCurrent(constHolder->GetConstant(defaultTypedValue.mValue), constHolder, defaultTypedValue.mType);
- auto castedVal = bfModule->Cast(methodInstance->mMethodDef->GetRefNode(), BfTypedValue(defaultVal, defaultTypedValue.mType), methodInstance->GetParamType(defaultIdx));
- if (castedVal)
- {
- BfExprEvaluator exprEvaluator(bfModule);
- exprEvaluator.PushArg(castedVal, irArgs);
- }
- }
- }
- }
- BfExprEvaluator exprEvaluator(bfModule);
- exprEvaluator.CreateCall(NULL, moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc, false, irArgs);
- bfModule->mBfIRBuilder->CreateBr(testHeadBlock);
- }
- bfModule->mBfIRBuilder->AddBlock(testEndBlock);
- bfModule->mBfIRBuilder->SetInsertPoint(testEndBlock);
- irArgs.clear();
- bfModule->mBfIRBuilder->CreateCall(testFinishMethod.mFunc, irArgs);
- retValue = bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 0);
- }
- void BfCompiler::CreateVData(BfVDataModule* bfModule)
- {
- bool isHotCompile = IsHotCompile();
- if ((isHotCompile) && (bfModule->mProject != mOptions.mHotProject))
- return;
- BP_ZONE("BfCompiler::CreateVData");
- BfLogSysM("CreateVData %s\n", bfModule->mProject->mName.c_str());
- CompileLog("CreateVData %s\n", bfModule->mProject->mName.c_str());
- BF_ASSERT_REL(!bfModule->mIsDeleting);
- bfModule->mProject->mUsedModules.Add(bfModule);
- auto project = bfModule->mProject;
- auto vdataContext = bfModule->mContext;
- BF_ASSERT(bfModule->mModuleName == "vdata");
- //////////////////////////////////////////////////////////////////////////
- mContext->ReflectInit();
- // Create types we'll need for vdata, so we won't change the vdata hash afterward
- // bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr));
- //
- // ///
- //
- // auto typeDefType = bfModule->ResolveTypeDef(mTypeTypeDef)->ToTypeInstance();
- // if (!typeDefType)
- // return;
- // BF_ASSERT(typeDefType != NULL);
- // vdataContext->mBfTypeType = typeDefType->ToTypeInstance();
- //
- // auto typeInstanceDefType = bfModule->ResolveTypeDef(mReflectTypeInstanceTypeDef);
- // if (!typeInstanceDefType)
- // return;
- // auto typeInstanceDefTypeInstance = typeInstanceDefType->ToTypeInstance();
- //
- // auto typeDef = mSystem->FindTypeDef("System.ClassVData");
- // BF_ASSERT(typeDef != NULL);
- // auto bfClassVDataType = bfModule->ResolveTypeDef(typeDef)->ToTypeInstance();
- // vdataContext->mBfClassVDataPtrType = bfModule->CreatePointerType(bfClassVDataType);
- //////////////////////////////////////////////////////////////////////////
- int numEntries = 0;
- int numConcreteTypes = 0;
- Array<BfType*> orderedTypes;
- for (auto type : mContext->mResolvedTypes)
- {
- numEntries++;
- BF_ASSERT((type != NULL) || (mPassInstance->HasFailed()));
- if (!type->IsReified())
- continue;
- orderedTypes.Add(type);
- CompileLog("TypeId:%d %s\n", type->mTypeId, bfModule->TypeToString(type).c_str());
- if ((type != NULL) && (type->IsObjectOrInterface()))
- {
- numConcreteTypes++;
- auto typeInst = type->ToTypeInstance();
- if (typeInst->mModule == NULL)
- {
- BF_ASSERT(mPassInstance->HasFailed());
- continue;
- }
- }
- }
- {
- BP_ZONE("BfCompiler::CreateVData sort orderedTypes");
- orderedTypes.Sort([](BfType* lhs, BfType* rhs)
- {
- return lhs->mTypeId < rhs->mTypeId;
- });
- }
- BfLogSysM("TypeEntries: %d ConcreteTypes: %d\n", numEntries, numConcreteTypes);
- HashContext vdataHashCtx;
- //vdataHashCtx.mDbgViz = true;
- vdataHashCtx.Mixin(bfModule->mProject->mVDataConfigHash);
- Array<TestMethod> testMethods;
- if (project->mTargetType == BfTargetType_BeefTest)
- GetTestMethods(bfModule, testMethods, vdataHashCtx);
- Array<String*> typeNameList;
- defer(
- {
- for (auto str : typeNameList)
- delete str;
- });
- struct _SortedTypeEntry
- {
- BfTypeInstance* mTypeInstance;
- String* mTypeName;
- int mPriority;
- _SortedTypeEntry()
- {
- mPriority = 0;
- mTypeInstance = NULL;
- mTypeName = NULL;
- }
- _SortedTypeEntry(BfModule* module, BfTypeInstance* typeInstance, Array<String*>& nameList, String*& namePtr)
- {
- if (namePtr == NULL)
- {
- namePtr = new String(module->TypeToString(typeInstance));
- nameList.Add(namePtr);
- }
- mTypeName = namePtr;
- mTypeInstance = typeInstance;
- mPriority = 0;
- }
- static void Sort(Array<_SortedTypeEntry>& list)
- {
- list.Sort(
- [](const _SortedTypeEntry& lhs, const _SortedTypeEntry& rhs)
- {
- if (lhs.mPriority != rhs.mPriority)
- return lhs.mPriority > rhs.mPriority;
- return *lhs.mTypeName < *rhs.mTypeName;
- });
- }
- static void SortRev(Array<_SortedTypeEntry>& list)
- {
- list.Sort(
- [](const _SortedTypeEntry& lhs, const _SortedTypeEntry& rhs)
- {
- if (lhs.mPriority != rhs.mPriority)
- return lhs.mPriority < rhs.mPriority;
- return *lhs.mTypeName > *rhs.mTypeName;
- });
- }
- };
- Array<_SortedTypeEntry> preStaticInitList;
- Array<_SortedTypeEntry> staticMarkList;
- Array<_SortedTypeEntry> staticTLSList;
- Array<BfType*> vdataTypeList;
- HashSet<BfModule*> usedModuleSet;
- HashSet<BfType*> reflectSkipTypeSet;
- HashSet<BfType*> reflectFieldTypeSet;
- vdataHashCtx.MixinStr(project->mStartupObject);
- vdataHashCtx.Mixin(project->mTargetType);
- for (auto type : orderedTypes)
- {
- if (type == NULL)
- continue;
- if (type->IsTemporary())
- continue;
- if ((type->IsGenericParam()) || (type->IsUnspecializedTypeVariation()))
- continue;
- auto typeInst = type->ToTypeInstance();
- if ((typeInst != NULL) && (!typeInst->IsReified()) && (!typeInst->IsUnspecializedType()))
- continue;
- if (!IsTypeUsed(type, project))
- continue;
- vdataTypeList.push_back(type);
- vdataHashCtx.Mixin(type->mTypeId);
- BF_ASSERT((type != NULL) || (mPassInstance->HasFailed()));
- if ((type != NULL) && (typeInst != NULL))
- {
- auto module = typeInst->mModule;
- if (module == NULL)
- continue;
- if (type->IsEnum())
- {
- for (auto& fieldInst : typeInst->mFieldInstances)
- {
- auto fieldDef = fieldInst.GetFieldDef();
- if (fieldDef == NULL)
- continue;
- if (!fieldDef->IsEnumCaseEntry())
- continue;
- if (fieldInst.mResolvedType->IsTuple())
- reflectFieldTypeSet.Add(fieldInst.mResolvedType);
- }
- }
- if (type->IsInterface())
- vdataHashCtx.Mixin(typeInst->mSlotNum);
- vdataHashCtx.Mixin(typeInst->mAlwaysIncludeFlags);
- vdataHashCtx.Mixin(typeInst->mHasBeenInstantiated);
- if (!module->mIsScratchModule)
- {
- BF_ASSERT(module->mIsReified);
- if (usedModuleSet.Add(module))
- {
- CompileLog("UsedModule %p %s\n", module, module->mModuleName.c_str());
- HashModuleVData(module, vdataHashCtx);
- }
- }
- vdataHashCtx.MixinStr(module->mModuleName);
- vdataHashCtx.Mixin(typeInst->mTypeDef->mSignatureHash);
- vdataHashCtx.Mixin(module->mHasForceLinkMarker);
- for (auto iface : typeInst->mInterfaces)
- {
- vdataHashCtx.Mixin(iface.mInterfaceType->mTypeId);
- vdataHashCtx.Mixin(iface.mDeclaringType->mTypeCode);
- vdataHashCtx.Mixin(iface.mDeclaringType->mProject);
- }
- if (!typeInst->IsUnspecializedType())
- {
- for (auto& methodInstGroup : typeInst->mMethodInstanceGroups)
- {
- bool isImplementedAndReified = (methodInstGroup.IsImplemented()) && (methodInstGroup.mDefault != NULL) &&
- (methodInstGroup.mDefault->mIsReified) && (!methodInstGroup.mDefault->mIsUnspecialized);
- vdataHashCtx.Mixin(isImplementedAndReified);
- }
- }
- // Could be necessary if a base type in another project adds new virtual methods (for example)
- auto baseType = typeInst->mBaseType;
- while (baseType != NULL)
- {
- vdataHashCtx.Mixin(baseType->mTypeDef->mSignatureHash);
- baseType = baseType->mBaseType;
- }
- //TODO: What was this for?
- // if (module->mProject != bfModule->mProject)
- // {
- // if ((module->mProject != NULL) && (module->mProject->mTargetType == BfTargetType_BeefDynLib))
- // continue;
- // }
- String* typeName = NULL;
- if ((typeInst->mHasStaticInitMethod) || (typeInst->mHasStaticDtorMethod))
- preStaticInitList.Add(_SortedTypeEntry(bfModule, typeInst, typeNameList, typeName));
- if ((typeInst->mHasStaticMarkMethod) && (mOptions.mEnableRealtimeLeakCheck))
- staticMarkList.Add(_SortedTypeEntry(bfModule, typeInst, typeNameList, typeName));
- if ((typeInst->mHasTLSFindMethod) && (mOptions.mEnableRealtimeLeakCheck))
- staticTLSList.Add(_SortedTypeEntry(bfModule, typeInst, typeNameList, typeName));
- }
- }
- int lastModuleRevision = bfModule->mRevision;
- Val128 vdataHash = vdataHashCtx.Finish128();
- bool wantsRebuild = vdataHash != bfModule->mDataHash;
- if (bfModule->mHadBuildError)
- wantsRebuild = true;
- // If we did one of those 'hot compile' partial vdata builds, now build the whole thing
- if ((!IsHotCompile()) && (bfModule->mHadHotObjectWrites))
- wantsRebuild = true;
- if (mOptions.mHotProject != NULL)
- {
- HashContext vdataHashCtxEx;
- vdataHashCtxEx.Mixin(mOptions.mHotProject->mName);
- vdataHashCtxEx.Mixin((int)mHotState->mNewlySlottedTypeIds.size());
- for (auto typeId : mHotState->mNewlySlottedTypeIds)
- vdataHashCtxEx.Mixin(typeId);
- vdataHashCtxEx.Mixin((int)mHotState->mSlotDefineTypeIds.size());
- for (auto typeId : mHotState->mSlotDefineTypeIds)
- vdataHashCtxEx.Mixin(typeId);
- Val128 vdataHashEx = vdataHashCtxEx.Finish128();
- if (mHotState->mVDataHashEx.IsZero())
- {
- if (!mHotState->mNewlySlottedTypeIds.IsEmpty())
- wantsRebuild = true;
- if (!mHotState->mSlotDefineTypeIds.IsEmpty())
- wantsRebuild = true;
- }
- else
- {
- if (vdataHashEx != mHotState->mVDataHashEx)
- wantsRebuild = true;
- }
- mHotState->mVDataHashEx = vdataHashEx;
- }
- if ((wantsRebuild) || (bfModule->mIsModuleMutable))
- {
- bfModule->StartNewRevision();
- if (bfModule->mAwaitingInitFinish)
- bfModule->FinishInit();
- }
- // We add the string hash into vdata hash later
- bfModule->mDataHash = vdataHash;//vdataPreStringHash;
- // This handles "no StartNewRevision" 'else' case, but also handles if vdata failed to complete from a previous compilation
- if (!bfModule->mIsModuleMutable)
- {
- CompileLog("VData unchanged, skipping\n");
- return;
- }
- auto typeResolveModule = mContext->mUnreifiedModule;
- BfTypeInstance* stringType = typeResolveModule->ResolveTypeDef(mStringTypeDef, BfPopulateType_Data)->ToTypeInstance();
- BfTypeInstance* stringViewType = typeResolveModule->ResolveTypeDef(mStringViewTypeDef, BfPopulateType_Data)->ToTypeInstance();
- BfTypeInstance* reflectSpecializedTypeInstance = typeResolveModule->ResolveTypeDef(mReflectSpecializedGenericType)->ToTypeInstance();
- BfTypeInstance* reflectUnspecializedTypeInstance = typeResolveModule->ResolveTypeDef(mReflectUnspecializedGenericType)->ToTypeInstance();
- BfTypeInstance* reflectArrayTypeInstance = typeResolveModule->ResolveTypeDef(mReflectArrayType)->ToTypeInstance();
- bool madeBfTypeData = false;
- auto typeDefType = mContext->mBfTypeType;
- bool needsFullTypeList = bfModule->IsMethodImplementedAndReified(typeDefType, "GetType");
- bool needsTypeList = needsFullTypeList || bfModule->IsMethodImplementedAndReified(typeDefType, "GetType_");
- bool needsObjectTypeData = needsTypeList || bfModule->IsMethodImplementedAndReified(vdataContext->mBfObjectType, "RawGetType") || bfModule->IsMethodImplementedAndReified(vdataContext->mBfObjectType, "GetType");
- bool needsTypeNames = bfModule->IsMethodImplementedAndReified(typeDefType, "GetName") || bfModule->IsMethodImplementedAndReified(typeDefType, "GetFullName");
- bool needsStringLiteralList = (mOptions.mAllowHotSwapping) || (bfModule->IsMethodImplementedAndReified(stringType, "Intern")) || (bfModule->IsMethodImplementedAndReified(stringViewType, "Intern"));
- BfCreateTypeDataContext createTypeDataCtx;
- if (!needsTypeList)
- {
- reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mTypeTypeDef));
- reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectTypeInstanceTypeDef));
- }
- if (!needsFullTypeList)
- {
- reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectSpecializedGenericType));
- reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectUnspecializedGenericType));
- reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectConstExprType));
- reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectArrayType));
- reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectGenericParamType));
- reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectPointerType));
- reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectSizedArrayType));
- reflectSkipTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectRefType));
- }
- HashSet<BfType*> boxeeSet;
- for (auto type : vdataTypeList)
- {
- auto typeInst = type->ToTypeInstance();
- if ((!type->IsReified()) || (type->IsUnspecializedType()))
- continue;
- if (type->IsBoxed())
- boxeeSet.Add(typeInst->GetUnderlyingType());
- }
- int usedTypeCount = 0;
- HashSet<BfType*> vDataTypeSet;
- SmallVector<BfIRValue, 256> typeDataVector;
- Array<BfType*> usedTypeDataVector;
- for (auto type : vdataTypeList)
- {
- if (type->IsTypeAlias())
- continue;
- if (type->IsTypeInstance())
- BF_ASSERT(!type->IsIncomplete());
- auto typeInst = type->ToTypeInstance();
- if ((typeInst != NULL) && (!typeInst->IsReified()) && (!typeInst->IsUnspecializedType()))
- continue;
- bool needsTypeData = (needsFullTypeList) || ((type->IsObject()) && (needsObjectTypeData));
- bool needsVData = (type->IsObject()) && (typeInst->HasBeenInstantiated());
- if ((needsObjectTypeData) && (boxeeSet.Contains(typeInst)))
- needsTypeData = true;
- bool forceReflectFields = false;
- if (bfModule->mProject->mReferencedTypeData.Contains(type))
- {
- needsTypeData = true;
- if (type->IsEnum())
- forceReflectFields = true;
- }
- if (reflectSkipTypeSet.Contains(type))
- {
- if (!bfModule->mProject->mReferencedTypeData.Contains(type))
- {
- needsTypeData = false;
- needsVData = false;
- }
- }
- BfIRValue typeVariable;
- if ((needsTypeData) || (needsVData))
- {
- if (reflectFieldTypeSet.Contains(type))
- {
- needsTypeData = true;
- forceReflectFields = true;
- }
- /*else if (reflectTypeSet.Contains(type))
- {
- needsTypeData = true;
- needsVData = true;
- }*/
- if (needsVData)
- vDataTypeSet.Add(type);
- typeVariable = bfModule->CreateTypeData(type, createTypeDataCtx, forceReflectFields, needsTypeData, needsTypeNames, needsVData);
- if (typeVariable)
- usedTypeDataVector.Add(type);
- }
- else if ((type->IsInterface()) && (typeInst->mSlotNum >= 0))
- {
- bfModule->CreateSlotOfs(typeInst);
- }
- usedTypeCount++;
- type->mDirty = false;
- if (needsTypeList)
- {
- int typeId = type->mTypeId;
- if (typeId == -1)
- continue;
- if (typeId >= (int)typeDataVector.size())
- typeDataVector.resize(typeId + 1);
- typeDataVector[typeId] = typeVariable;
- }
- }
- for (int typeId = 0; typeId < (int)typeDataVector.size(); typeId++)
- {
- if (!typeDataVector[typeId])
- typeDataVector[typeId] = bfModule->GetDefaultValue(typeDefType);
- }
- // We only need 'sTypes' if we actually reference it
- //
- {
- auto typeDefPtrType = bfModule->CreatePointerType(typeDefType);
- StringT<128> typesVariableName;
- BfMangler::MangleStaticFieldName(typesVariableName, GetMangleKind(), typeDefType->ToTypeInstance(), "sTypes", typeDefPtrType);
- auto arrayType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->MapType(typeDefType), (int)typeDataVector.size());
- auto typeDataConst = bfModule->mBfIRBuilder->CreateConstAgg_Value(arrayType, typeDataVector);
- BfIRValue typeDataArray = bfModule->mBfIRBuilder->CreateGlobalVariable(arrayType, true, BfIRLinkageType_External,
- typeDataConst, typesVariableName);
- StringT<128> typeCountVariableName;
- BfMangler::MangleStaticFieldName(typeCountVariableName, GetMangleKind(), typeDefType->ToTypeInstance(), "sTypeCount", bfModule->GetPrimitiveType(BfTypeCode_Int32));
- bfModule->mBfIRBuilder->CreateGlobalVariable(bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_Int32)), true, BfIRLinkageType_External,
- bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, (int)typeDataVector.size()), typeCountVariableName);
- }
- HashSet<int> foundStringIds;
- for (int stringId : bfModule->mStringPoolRefs)
- foundStringIds.Add(stringId);
- Array<BfModule*> orderedUsedModules;
- for (auto module : usedModuleSet)
- orderedUsedModules.push_back(module);
- std::sort(orderedUsedModules.begin(), orderedUsedModules.end(), [] (BfModule* lhs, BfModule* rhs)
- {
- return lhs->mModuleName < rhs->mModuleName;
- });
- Array<BfMethodInstance*> dllMethods;
- Array<BfIRValue> forceLinkValues;
- HashSet<int> dllNameSet;
- Array<BfCompiler::StringValueEntry> stringValueEntries;
- for (auto module : orderedUsedModules)
- {
- CheckModuleStringRefs(module, bfModule, lastModuleRevision, foundStringIds, dllNameSet, dllMethods, stringValueEntries);
- if ((module->mHasForceLinkMarker) &&
- ((!isHotCompile) || (module->mHadHotObjectWrites)) &&
- (mOptions.mPlatformType != BfPlatformType_Wasm))
- forceLinkValues.Add(bfModule->CreateForceLinkMarker(module, NULL));
- }
- if (!forceLinkValues.IsEmpty())
- {
- auto elemType = bfModule->CreatePointerType(bfModule->GetPrimitiveType(BfTypeCode_Int8));
- auto arrayType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->MapType(elemType), (int)forceLinkValues.size());
- auto typeDataConst = bfModule->mBfIRBuilder->CreateConstAgg_Value(arrayType, forceLinkValues);
- BfIRValue typeDataArray = bfModule->mBfIRBuilder->CreateGlobalVariable(arrayType, true, BfIRLinkageType_Internal,
- typeDataConst, "FORCELINK_MODULES");
- }
- // Generate strings array
- {
- if (!needsStringLiteralList)
- {
- stringValueEntries.Clear();
- }
- std::sort(stringValueEntries.begin(), stringValueEntries.end(),
- [](const StringValueEntry& lhs, const StringValueEntry& rhs)
- {
- return lhs.mId < rhs.mId;
- });
- auto stringPtrType = bfModule->CreatePointerType(stringType);
- auto stringPtrIRType = bfModule->mBfIRBuilder->MapTypeInstPtr(stringType);
- StringT<128> stringsVariableName;
- BfMangler::MangleStaticFieldName(stringsVariableName, GetMangleKind(), stringType->ToTypeInstance(), "sStringLiterals", stringPtrType);
- Array<BfIRValue> stringList;
- stringList.Add(bfModule->mBfIRBuilder->CreateConstNull(stringPtrIRType));
- for (auto& stringValueEntry : stringValueEntries)
- stringList.Add(stringValueEntry.mStringVal);
- stringList.Add(bfModule->mBfIRBuilder->CreateConstNull(stringPtrIRType));
- BfIRType stringArrayType = bfModule->mBfIRBuilder->GetSizedArrayType(stringPtrIRType, (int)stringList.size());
- auto stringArray = bfModule->mBfIRBuilder->CreateConstAgg_Value(stringArrayType, stringList);
- auto stringArrayVar = bfModule->mBfIRBuilder->CreateGlobalVariable(stringArrayType, true, BfIRLinkageType_External, stringArray, stringsVariableName);
- if (bfModule->mBfIRBuilder->DbgHasInfo())
- {
- auto dbgArrayType = bfModule->mBfIRBuilder->DbgCreateArrayType(stringList.size() * mSystem->mPtrSize * 8, mSystem->mPtrSize * 8, bfModule->mBfIRBuilder->DbgGetType(stringPtrType), (int)stringList.size());
- bfModule->mBfIRBuilder->DbgCreateGlobalVariable(bfModule->mDICompileUnit, stringsVariableName, stringsVariableName, BfIRMDNode(), 0, dbgArrayType, false, stringArrayVar);
- }
- }
- // Generate string ID array
- {
- auto stringType = bfModule->ResolveTypeDef(mStringTypeDef, BfPopulateType_Data)->ToTypeInstance();
- auto stringPtrType = bfModule->CreatePointerType(stringType);
- auto stringPtrIRType = bfModule->mBfIRBuilder->MapTypeInstPtr(stringType);
- StringT<128> stringsVariableName;
- BfMangler::MangleStaticFieldName(stringsVariableName, GetMangleKind(), stringType->ToTypeInstance(), "sIdStringLiterals", stringPtrType);
- Array<BfIRValue> stringList;
- stringList.Resize(createTypeDataCtx.mUsedStringIdMap.size());
- for (auto& kv : createTypeDataCtx.mUsedStringIdMap)
- {
- stringList[kv.mValue] = bfModule->mStringObjectPool[kv.mKey];
- }
- BfIRType stringArrayType = bfModule->mBfIRBuilder->GetSizedArrayType(stringPtrIRType, (int)createTypeDataCtx.mUsedStringIdMap.size());
- auto stringArray = bfModule->mBfIRBuilder->CreateConstAgg_Value(stringArrayType, stringList);
- auto stringArrayVar = bfModule->mBfIRBuilder->CreateGlobalVariable(stringArrayType, true, BfIRLinkageType_External, stringArray, stringsVariableName);
- if (bfModule->mBfIRBuilder->DbgHasInfo())
- {
- auto dbgArrayType = bfModule->mBfIRBuilder->DbgCreateArrayType(stringList.size() * mSystem->mPtrSize * 8, mSystem->mPtrSize * 8, bfModule->mBfIRBuilder->DbgGetType(stringPtrType), (int)stringList.size());
- bfModule->mBfIRBuilder->DbgCreateGlobalVariable(bfModule->mDICompileUnit, stringsVariableName, stringsVariableName, BfIRMDNode(), 0, dbgArrayType, false, stringArrayVar);
- }
- }
- BfIRFunction loadSharedLibFunc = CreateLoadSharedLibraries(bfModule, dllMethods);
- BfIRType nullPtrType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_NullPtr));
- BfIRType voidType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_None));
- BfIRType int32Type = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_Int32));
- Array<_SortedTypeEntry> staticInitList;
- // Populate staticInitList
- {
- Dictionary<int, BfTypeInstance*> pendingIDToInstanceMap;
- HashSet<BfTypeInstance*> handledTypes;
- BfType* staticInitPriorityAttributeType = vdataContext->mUnreifiedModule->ResolveTypeDef(mStaticInitPriorityAttributeTypeDef);
- BfType* staticInitAfterAttributeType = vdataContext->mUnreifiedModule->ResolveTypeDef(mStaticInitAfterAttributeTypeDef);
- bool forceAdd = false;
- for (int pass = 0; true; pass++)
- {
- bool hadAdd = false;
- for (auto& mapEntry : preStaticInitList)
- {
- auto typeInst = mapEntry.mTypeInstance;
- if ((typeInst != NULL) && (!typeInst->IsUnspecializedType()))
- {
- if (pass == 0)
- {
- int priority = 0;
- bool hadInitAfterAttribute = false;
- if (typeInst->mCustomAttributes != NULL)
- {
- for (auto& customAttr : typeInst->mCustomAttributes->mAttributes)
- {
- if (customAttr.mType == staticInitAfterAttributeType)
- hadInitAfterAttribute = true;
- if (customAttr.mType == staticInitPriorityAttributeType)
- {
- if (customAttr.mCtorArgs.size() == 1)
- {
- auto constant = typeInst->mConstHolder->GetConstant(customAttr.mCtorArgs[0]);
- if (constant != NULL)
- priority = constant->mInt32;
- }
- }
- }
- }
- if (!hadInitAfterAttribute)
- {
- mapEntry.mPriority = priority;
- staticInitList.Add(mapEntry);
- mapEntry.mTypeInstance = NULL;
- }
- else
- {
- pendingIDToInstanceMap.TryAdd(typeInst->mTypeId, typeInst);
- }
- }
- else
- {
- if (pendingIDToInstanceMap.ContainsKey(typeInst->mTypeId))
- {
- bool doAdd = true;
- if (!forceAdd)
- {
- for (auto& customAttr : typeInst->mCustomAttributes->mAttributes)
- {
- if (customAttr.mType == staticInitAfterAttributeType)
- {
- if (customAttr.mCtorArgs.size() == 0)
- {
- doAdd = false;
- }
- else
- {
- auto ctorArg = customAttr.mCtorArgs[0];
- auto constant = typeInst->mConstHolder->GetConstant(ctorArg);
- if (constant != NULL)
- {
- int refTypeId = constant->mInt32;
- if (pendingIDToInstanceMap.ContainsKey(refTypeId))
- doAdd = false;
- }
- }
- }
- }
- }
- if (doAdd)
- {
- staticInitList.Add(mapEntry);
- pendingIDToInstanceMap.Remove(typeInst->mTypeId);
- hadAdd = true;
- }
- }
- }
- }
- }
- if (pass == 0)
- {
- _SortedTypeEntry::Sort(staticInitList);
- }
- if ((pass > 0) && (!hadAdd) && (pendingIDToInstanceMap.size() > 0)) // Circular ref?
- forceAdd = true;
- if (pendingIDToInstanceMap.size() == 0)
- break;
- }
- }
- /// Generate "BfCallAllStaticDtors"
- BfIRFunction dtorFunc;
- {
- SmallVector<BfIRType, 2> paramTypes;
- auto dtorFuncType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes, false);
- String dtorName = (mOptions.mPlatformType == BfPlatformType_Wasm) ? "BeefDone" : "BfCallAllStaticDtors";
- dtorFunc = bfModule->mBfIRBuilder->CreateFunction(dtorFuncType, BfIRLinkageType_External, dtorName);
- bfModule->SetupIRMethod(NULL, dtorFunc, false);
- bfModule->mBfIRBuilder->SetActiveFunction(dtorFunc);
- auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
- bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
- for (int i = staticInitList.mSize - 1; i >= 0; i--)
- {
- auto typeInst = staticInitList[i].mTypeInstance;
- if (!typeInst->mHasStaticDtorMethod)
- continue;
- for (auto& methodGroup : typeInst->mMethodInstanceGroups)
- {
- auto methodInstance = methodGroup.mDefault;
- if ((methodInstance != NULL) &&
- (methodInstance->mMethodDef->mIsStatic) &&
- (methodInstance->mMethodDef->mMethodType == BfMethodType_Dtor) &&
- ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
- {
- if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
- continue;
- if (methodInstance->mHotMethod != NULL)
- methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
- auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
- bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
- }
- }
- }
- bfModule->mBfIRBuilder->CreateRetVoid();
- }
- auto targetType = project->mTargetType;
- if ((targetType == BfTargetType_BeefWindowsApplication) && (mOptions.mPlatformType != BfPlatformType_Windows))
- targetType = BfTargetType_BeefConsoleApplication;
- bool isConsoleApplication = (targetType == BfTargetType_BeefConsoleApplication) || (targetType == BfTargetType_BeefTest);
- if ((targetType == BfTargetType_BeefWindowsApplication) && (mOptions.mPlatformType != BfPlatformType_Windows))
- isConsoleApplication = true;
- bool isDllMain = (targetType == BfTargetType_BeefLib_DynamicLib) && (mOptions.mPlatformType == BfPlatformType_Windows);
- bool isPosixDynLib = ((targetType == BfTargetType_BeefLib_DynamicLib) || (targetType == BfTargetType_BeefLib_StaticLib)) &&
- (mOptions.mPlatformType != BfPlatformType_Windows);
- bool mainHasArgs = (targetType != BfTargetType_BeefLib_DynamicLib) && (targetType != BfTargetType_BeefLib_StaticLib) &&
- (mOptions.mPlatformType != BfPlatformType_Wasm);
- bool mainHasRet = ((targetType != BfTargetType_BeefLib_DynamicLib) && (targetType != BfTargetType_BeefLib_StaticLib)) || (isDllMain);
- // Generate "main"
- if (!IsHotCompile())
- {
- int rtFlags = 0;
- BfIRFunctionType mainFuncType;
- BfIRFunction mainFunc;
- BfIRFunctionType shutdownFuncType;
- BfIRFunction shutdownFunc;
- if (isConsoleApplication)
- {
- SmallVector<BfIRType, 2> paramTypes;
- paramTypes.push_back(int32Type);
- paramTypes.push_back(nullPtrType);
- mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
- mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "main");
- bfModule->SetupIRMethod(NULL, mainFunc, false);
- }
- else if (isDllMain)
- {
- SmallVector<BfIRType, 4> paramTypes;
- paramTypes.push_back(nullPtrType); // hinstDLL
- paramTypes.push_back(int32Type); // fdwReason
- paramTypes.push_back(nullPtrType); // lpvReserved
- mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
- mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "DllMain");
- if (mOptions.mMachineType == BfMachineType_x86)
- bfModule->mBfIRBuilder->SetFuncCallingConv(mainFunc, BfIRCallingConv_StdCall);
- bfModule->SetupIRMethod(NULL, mainFunc, false);
- rtFlags = 0x10; // BfRtFlags_NoThreadExitWait
- }
- else if (targetType == BfTargetType_BeefWindowsApplication)
- {
- SmallVector<BfIRType, 4> paramTypes;
- paramTypes.push_back(nullPtrType); // hInstance
- paramTypes.push_back(nullPtrType); // hPrevInstance
- paramTypes.push_back(nullPtrType); // lpCmdLine
- paramTypes.push_back(int32Type); // nCmdShow
- mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
- mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "WinMain");
- if (mOptions.mMachineType == BfMachineType_x86)
- bfModule->mBfIRBuilder->SetFuncCallingConv(mainFunc, BfIRCallingConv_StdCall);
- bfModule->SetupIRMethod(NULL, mainFunc, false);
- }
- else
- {
- BfIRFunction combinedFunc;
- SmallVector<BfIRType, 2> paramTypes;
- if (mainHasArgs)
- {
- paramTypes.push_back(int32Type);
- paramTypes.push_back(nullPtrType);
- }
- mainFuncType = bfModule->mBfIRBuilder->CreateFunctionType(mainHasRet ? int32Type : voidType, paramTypes, false);
- mainFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "BeefStart");
- bfModule->SetupIRMethod(NULL, mainFunc, false);
- combinedFunc = bfModule->mBfIRBuilder->CreateFunction(mainFuncType, BfIRLinkageType_External, "BeefMain");
- bfModule->SetupIRMethod(NULL, combinedFunc, false);
- paramTypes.clear();
- shutdownFuncType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes, false);
- shutdownFunc = bfModule->mBfIRBuilder->CreateFunction(shutdownFuncType, BfIRLinkageType_External, "BeefStop");
- bfModule->SetupIRMethod(NULL, shutdownFunc, false);
- bfModule->mBfIRBuilder->SetActiveFunction(combinedFunc);
- auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
- bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
- SmallVector<BfIRValue, 1> args;
- if (mainHasArgs)
- {
- args.push_back(bfModule->mBfIRBuilder->GetArgument(0));
- args.push_back(bfModule->mBfIRBuilder->GetArgument(1));
- }
- auto res = bfModule->mBfIRBuilder->CreateCall(mainFunc, args);
- args.clear();
- bfModule->mBfIRBuilder->CreateCall(shutdownFunc, args);
- if (mainHasRet)
- bfModule->mBfIRBuilder->CreateRet(res);
- else
- bfModule->mBfIRBuilder->CreateRetVoid();
- }
- bfModule->mBfIRBuilder->SetActiveFunction(mainFunc);
- auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
- bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
- if (rtFlags != 0)
- {
- auto addRtFlagMethod = bfModule->GetInternalMethod("AddRtFlags", 1);
- if (addRtFlagMethod)
- {
- SmallVector<BfIRValue, 1> args;
- args.push_back(bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, rtFlags));
- bfModule->mBfIRBuilder->CreateCall(addRtFlagMethod.mFunc, args);
- }
- }
- if ((mOptions.mPlatformType != BfPlatformType_Windows) && (mainHasArgs) &&
- ((targetType == BfTargetType_BeefConsoleApplication) || (targetType == BfTargetType_BeefApplication_StaticLib) ||
- (targetType == BfTargetType_BeefApplication_DynamicLib) || (targetType == BfTargetType_BeefTest)))
- {
- SmallVector<BfIRType, 2> paramTypes;
- paramTypes.push_back(int32Type);
- paramTypes.push_back(nullPtrType);
- auto setCmdLineFuncType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes, false);
- auto setCmdLineFunc = bfModule->mBfIRBuilder->CreateFunction(setCmdLineFuncType, BfIRLinkageType_External, "BfpSystem_SetCommandLine");
- bfModule->SetupIRMethod(NULL, setCmdLineFunc, false);
- SmallVector<BfIRValue, 2> args;
- args.push_back(bfModule->mBfIRBuilder->GetArgument(0));
- args.push_back(bfModule->mBfIRBuilder->GetArgument(1));
- bfModule->mBfIRBuilder->CreateCall(setCmdLineFunc, args);
- }
- BfIRBlock initSkipBlock;
- if (isDllMain)
- {
- auto initBlock = bfModule->mBfIRBuilder->CreateBlock("doInit", false);
- initSkipBlock = bfModule->mBfIRBuilder->CreateBlock("skipInit", false);
- auto cmpResult = bfModule->mBfIRBuilder->CreateCmpEQ(bfModule->mBfIRBuilder->GetArgument(1), bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 1));
- bfModule->mBfIRBuilder->CreateCondBr(cmpResult, initBlock, initSkipBlock);
- bfModule->mBfIRBuilder->AddBlock(initBlock);
- bfModule->mBfIRBuilder->SetInsertPoint(initBlock);
- auto moduleMethodInstance = bfModule->GetInternalMethod("SetModuleHandle", 1);
- if (moduleMethodInstance)
- {
- SmallVector<BfIRValue, 1> args;
- args.push_back(bfModule->mBfIRBuilder->GetArgument(0));
- bfModule->mBfIRBuilder->CreateCall(moduleMethodInstance.mFunc, args);
- }
- }
- // Do the LoadLibrary calls below priority 100
- bool didSharedLibLoad = false;
- auto _CheckSharedLibLoad = [&]()
- {
- if (!didSharedLibLoad)
- {
- bfModule->mBfIRBuilder->CreateCall(loadSharedLibFunc, SmallVector<BfIRValue, 0>());
- didSharedLibLoad = true;
- }
- };
- for (auto& staticInitEntry : staticInitList)
- {
- auto typeInst = staticInitEntry.mTypeInstance;
- if (!typeInst->mHasStaticInitMethod)
- continue;
- if (staticInitEntry.mPriority < 100)
- _CheckSharedLibLoad();
- for (auto& methodGroup : typeInst->mMethodInstanceGroups)
- {
- auto methodInstance = methodGroup.mDefault;
- if ((methodInstance != NULL) &&
- (methodInstance->mMethodDef->mIsStatic) &&
- (methodInstance->mMethodDef->mMethodType == BfMethodType_Ctor) &&
- ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
- {
- if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
- continue;
- auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
- if (methodInstance->mHotMethod != NULL)
- methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
- bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
- }
- }
- }
- _CheckSharedLibLoad();
- if (initSkipBlock)
- {
- bfModule->mBfIRBuilder->CreateBr(initSkipBlock);
- bfModule->mBfIRBuilder->AddBlock(initSkipBlock);
- bfModule->mBfIRBuilder->SetInsertPoint(initSkipBlock);
- }
- BfIRValue retValue;
- if ((targetType == BfTargetType_BeefConsoleApplication) || (targetType == BfTargetType_BeefWindowsApplication) ||
- (targetType == BfTargetType_BeefApplication_StaticLib) || (targetType == BfTargetType_BeefApplication_DynamicLib))
- {
- bool hadRet = false;
- String entryClassName = project->mStartupObject;
- auto typeDef = mSystem->FindTypeDef(entryClassName, 0, bfModule->mProject, {}, NULL, BfFindTypeDefFlag_AllowGlobal);
- if (typeDef != NULL)
- {
- auto type = bfModule->ResolveTypeDef(typeDef);
- BF_ASSERT((type != NULL) || (mPassInstance->HasFailed()));
- if (type != NULL)
- {
- BfType* stringType = vdataContext->mUnreifiedModule->ResolveTypeDef(mStringTypeDef);
- BfType* int32Type = bfModule->GetPrimitiveType(BfTypeCode_Int32);
- BfType* intType = bfModule->GetPrimitiveType(BfTypeCode_IntPtr);
- BfType* voidType = bfModule->GetPrimitiveType(BfTypeCode_None);
- bool hadValidMainMethod = false;
- BfModuleMethodInstance moduleMethodInst;
- for (auto methodDef : typeDef->mMethods)
- {
- if (methodDef->mName == "Main")
- {
- hadValidMainMethod = true;
- moduleMethodInst = bfModule->GetMethodInstanceAtIdx(type->ToTypeInstance(), methodDef->mIdx);
- if (!methodDef->mIsStatic)
- {
- mPassInstance->Fail("Main method must be static", methodDef->GetRefNode());
- hadValidMainMethod = false;
- }
- if ((moduleMethodInst.mMethodInstance->mReturnType != int32Type) &&
- (moduleMethodInst.mMethodInstance->mReturnType != intType) &&
- (moduleMethodInst.mMethodInstance->mReturnType != voidType))
- {
- mPassInstance->Fail("Main method must return void, int, or int32", methodDef->GetRefNode());
- hadValidMainMethod = false;
- }
- if (moduleMethodInst.mMethodInstance->GetParamCount() == 0)
- {
- // No params
- }
- else
- {
- auto paramType = moduleMethodInst.mMethodInstance->GetParamType(0);
- if ((moduleMethodInst.mMethodInstance->GetParamCount() != 1) || (!paramType->IsArray()) || (paramType->GetUnderlyingType() != stringType))
- {
- mPassInstance->Fail("Main method must be declared with either no parameters or a single String[] parameter", methodDef->GetRefNode());
- hadValidMainMethod = false;
- }
- }
- }
- }
- if (moduleMethodInst)
- {
- if (hadValidMainMethod)
- {
- bool hasArgs = moduleMethodInst.mMethodInstance->GetParamCount() != 0;
- BfIRType intType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_IntPtr));
- BfIRType int32Type = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_Int32));
- // Create BeefEntry thunk
- SmallVector<BfIRType, 1> paramTypes;
- if (hasArgs)
- {
- paramTypes.push_back(bfModule->mBfIRBuilder->MapType(moduleMethodInst.mMethodInstance->GetParamType(0)));
- }
- BfIRFunctionType thunkFuncType = bfModule->mBfIRBuilder->CreateFunctionType(int32Type, paramTypes, false);
- BfIRFunction thunkMainFunc = bfModule->mBfIRBuilder->CreateFunction(thunkFuncType, BfIRLinkageType_External, "BeefStartProgram");
- bfModule->SetupIRMethod(NULL, thunkMainFunc, false);
- bfModule->mBfIRBuilder->SetActiveFunction(thunkMainFunc);
- auto thunkEntryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
- bfModule->mBfIRBuilder->SetInsertPoint(thunkEntryBlock);
- SmallVector<BfIRValue, 1> args;
- if (hasArgs)
- args.push_back(bfModule->mBfIRBuilder->GetArgument(0));
- auto methodInstance = moduleMethodInst.mMethodInstance;
- if (methodInstance->mHotMethod != NULL)
- methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
- auto retVal = bfModule->mBfIRBuilder->CreateCall(moduleMethodInst.mFunc, args);
- if (moduleMethodInst.mMethodInstance->mReturnType->IsVoid())
- {
- bfModule->mBfIRBuilder->CreateRet(bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 0));
- }
- else
- {
- retVal = bfModule->mBfIRBuilder->CreateNumericCast(retVal, true, BfTypeCode_Int32);
- bfModule->mBfIRBuilder->CreateRet(retVal);
- }
- hadRet = true;
- auto internalType = bfModule->ResolveTypeDef(mInternalTypeDef);
- args.clear();
- // Call BeefEntry thunk
- bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
- if (hasArgs)
- {
- auto createParamsMethodInstance = bfModule->GetMethodByName(internalType->ToTypeInstance(), "CreateParamsArray");
- auto callValue = bfModule->mBfIRBuilder->CreateCall(createParamsMethodInstance.mFunc, SmallVector<BfIRValue, 0>());
- args.push_back(callValue);
- }
- retValue = bfModule->mBfIRBuilder->CreateCall(thunkMainFunc, args);
- if (hasArgs)
- {
- auto deleteStringArrayMethodInstance = bfModule->GetMethodByName(internalType->ToTypeInstance(), "DeleteStringArray");
- bfModule->mBfIRBuilder->CreateCall(deleteStringArrayMethodInstance.mFunc, args);
- }
- }
- }
- else
- {
- if (entryClassName.IsEmpty())
- mPassInstance->Fail("Unable to find Main method in global namespace. Consider specifying a Startup Object in the project properties.");
- else
- mPassInstance->Fail(StrFormat("Unable to find Main method in specified Startup Object '%s'", entryClassName.c_str()));
- }
- }
- }
- else
- {
- if (entryClassName.empty())
- mPassInstance->Fail(StrFormat("No entry point class specified for executable in project '%s'", project->mName.c_str()));
- else
- mPassInstance->Fail(StrFormat("Unable to find entry point class '%s' in project '%s'", entryClassName.c_str(), project->mName.c_str()));
- bfModule->mHadBuildError = true;
- }
- if (!hadRet)
- retValue = bfModule->GetConstValue32(0);
- }
- else
- {
- if (mainHasRet)
- retValue = bfModule->GetConstValue32(1);
- }
- if (targetType == BfTargetType_BeefTest)
- EmitTestMethod(bfModule, testMethods, retValue);
- BfIRBlock deinitSkipBlock;
- if (isDllMain)
- {
- auto deinitBlock = bfModule->mBfIRBuilder->CreateBlock("doDeinit", false);
- deinitSkipBlock = bfModule->mBfIRBuilder->CreateBlock("skipDeinit", false);
- auto cmpResult = bfModule->mBfIRBuilder->CreateCmpEQ(bfModule->mBfIRBuilder->GetArgument(1), bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 0));
- bfModule->mBfIRBuilder->CreateCondBr(cmpResult, deinitBlock, deinitSkipBlock);
- bfModule->mBfIRBuilder->AddBlock(deinitBlock);
- bfModule->mBfIRBuilder->SetInsertPoint(deinitBlock);
- }
- if (mOptions.mPlatformType != BfPlatformType_Wasm)
- {
- auto prevBlock = bfModule->mBfIRBuilder->GetInsertBlock();
- if (shutdownFunc)
- {
- bfModule->mBfIRBuilder->SetActiveFunction(shutdownFunc);
- auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
- bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
- }
- bfModule->mBfIRBuilder->CreateCall(dtorFunc, SizedArray<BfIRValue, 0>());
- BfModuleMethodInstance shutdownMethod = bfModule->GetInternalMethod("Shutdown");
- if (shutdownMethod)
- {
- bfModule->mBfIRBuilder->CreateCall(shutdownMethod.mFunc, SizedArray<BfIRValue, 0>());
- }
- if (shutdownFunc)
- {
- bfModule->mBfIRBuilder->CreateRetVoid();
- bfModule->mBfIRBuilder->SetActiveFunction(mainFunc);
- bfModule->mBfIRBuilder->SetInsertPoint(prevBlock);
- }
- }
- if (deinitSkipBlock)
- {
- bfModule->mBfIRBuilder->CreateBr(deinitSkipBlock);
- bfModule->mBfIRBuilder->AddBlock(deinitSkipBlock);
- bfModule->mBfIRBuilder->SetInsertPoint(deinitSkipBlock);
- }
- if (retValue)
- bfModule->mBfIRBuilder->CreateRet(retValue);
- else
- bfModule->mBfIRBuilder->CreateRetVoid();
- if ((mOptions.mAllowHotSwapping) && (bfModule->mHasFullDebugInfo))
- {
- auto int8Type = bfModule->GetPrimitiveType(BfTypeCode_Int8);
- int dataSize = 16*1024;
- auto irArrType = bfModule->mBfIRBuilder->GetSizedArrayType(bfModule->mBfIRBuilder->MapType(int8Type), dataSize);
- String name = "__BFTLS_EXTRA";
- auto irVal = bfModule->mBfIRBuilder->CreateGlobalVariable(irArrType, false, BfIRLinkageType_External, bfModule->mBfIRBuilder->CreateConstAggZero(irArrType), name, true);
- BfIRMDNode dbgArrayType = bfModule->mBfIRBuilder->DbgCreateArrayType(dataSize * 8, 8, bfModule->mBfIRBuilder->DbgGetType(int8Type), dataSize);
- bfModule->mBfIRBuilder->DbgCreateGlobalVariable(bfModule->mDICompileUnit, name, name, BfIRMDNode(), 0, dbgArrayType, false, irVal);
- }
- if (isPosixDynLib)
- {
- auto voidType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_None));
- SizedArray<BfIRType, 4> paramTypes;
- BfIRFunctionType funcType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes);
- BfIRFunction func = bfModule->mBfIRBuilder->CreateFunction(funcType, BfIRLinkageType_Internal, "BfDynLib__Startup");
- bfModule->mBfIRBuilder->SetActiveFunction(func);
- bfModule->mBfIRBuilder->Func_AddAttribute(func, -1, BFIRAttribute_Constructor);
- auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("main", true);
- bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
- SmallVector<BfIRValue, 2> startArgs;
- bfModule->mBfIRBuilder->CreateCall(mainFunc, startArgs);
- bfModule->mBfIRBuilder->CreateRetVoid();
- //////////////////////////////////////////////////////////////////////////
- func = bfModule->mBfIRBuilder->CreateFunction(funcType, BfIRLinkageType_Internal, "BfDynLib__Shutdown");
- bfModule->mBfIRBuilder->Func_AddAttribute(func, -1, BFIRAttribute_Destructor);
- bfModule->mBfIRBuilder->SetActiveFunction(func);
- entryBlock = bfModule->mBfIRBuilder->CreateBlock("main", true);
- bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
- SmallVector<BfIRValue, 2> stopArgs;
- bfModule->mBfIRBuilder->CreateCall(shutdownFunc, startArgs);
- bfModule->mBfIRBuilder->CreateRetVoid();
- }
- }
- // Generate "System.GC.MarkAllStaticMembers"
- auto gcType = vdataContext->mUnreifiedModule->ResolveTypeDef(mGCTypeDef);
- if (bfModule->IsMethodImplementedAndReified(gcType->ToTypeInstance(), "MarkAllStaticMembers"))
- {
- bfModule->PopulateType(gcType);
- auto moduleMethodInstance = bfModule->GetMethodByName(gcType->ToTypeInstance(), "MarkAllStaticMembers");
- bfModule->mBfIRBuilder->SetActiveFunction(moduleMethodInstance.mFunc);
- if (!moduleMethodInstance)
- {
- bfModule->Fail("Internal error: System.GC doesn't contain MarkAllStaticMembers method");
- }
- else
- {
- auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
- bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
- _SortedTypeEntry::Sort(staticMarkList);
- for (auto& mapEntry : staticMarkList)
- {
- auto typeInst = mapEntry.mTypeInstance;
- if (typeInst->IsUnspecializedType())
- continue;
- for (auto& methodGroup : typeInst->mMethodInstanceGroups)
- {
- auto methodInstance = methodGroup.mDefault;
- if ((methodInstance != NULL) &&
- (methodInstance->mMethodDef->mIsStatic) &&
- (methodInstance->mMethodDef->mMethodType == BfMethodType_Normal) &&
- (methodInstance->mMethodDef->mName == BF_METHODNAME_MARKMEMBERS_STATIC) &&
- ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
- {
- if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
- continue;
- auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
- if (methodInstance->mHotMethod != NULL)
- methodInstance->mHotMethod->mFlags = (BfHotDepDataFlags)(methodInstance->mHotMethod->mFlags | BfHotDepDataFlag_AlwaysCalled);
- bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
- }
- }
- }
- bfModule->mBfIRBuilder->CreateRetVoid();
- }
- }
- // Generate "System.GC.FindAllTLSMembers"
- if (bfModule->IsMethodImplementedAndReified(gcType->ToTypeInstance(), "FindAllTLSMembers"))
- {
- bfModule->PopulateType(gcType);
- auto moduleMethodInstance = bfModule->GetMethodByName(gcType->ToTypeInstance(), "FindAllTLSMembers");
- bfModule->mBfIRBuilder->SetActiveFunction(moduleMethodInstance.mFunc);
- if (!moduleMethodInstance)
- {
- bfModule->Fail("Internal error: System.GC doesn't contain FindAllTLSMembers method");
- }
- else
- {
- auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
- bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
- _SortedTypeEntry::Sort(staticTLSList);
- for (auto& mapEntry : staticTLSList)
- {
- auto typeInst = mapEntry.mTypeInstance;
- if (typeInst->IsUnspecializedType())
- continue;
- for (auto& methodGroup : typeInst->mMethodInstanceGroups)
- {
- auto methodInstance = methodGroup.mDefault;
- if ((methodInstance != NULL) &&
- (methodInstance->mMethodDef->mIsStatic) &&
- (methodInstance->mMethodDef->mMethodType == BfMethodType_Normal) &&
- (methodInstance->mMethodDef->mName == BF_METHODNAME_FIND_TLS_MEMBERS) &&
- ((methodInstance->mChainType == BfMethodChainType_ChainHead) || (methodInstance->mChainType == BfMethodChainType_None)))
- {
- if (!typeInst->IsTypeMemberAccessible(methodInstance->mMethodDef->mDeclaringType, bfModule->mProject))
- continue;
- auto methodModule = bfModule->GetMethodInstanceAtIdx(typeInst, methodInstance->mMethodDef->mIdx);
- bfModule->mBfIRBuilder->CreateCall(methodModule.mFunc, SmallVector<BfIRValue, 0>());
- }
- }
- }
- bfModule->mBfIRBuilder->CreateRetVoid();
- }
- }
- if (bfModule->mHadBuildError)
- {
- bfModule->mDataHash = 0;
- }
- }
- // This method clears out unused generic types AFTER compilation of reified types has occurred
- void BfCompiler::UpdateDependencyMap(bool deleteUnusued, bool& didWork)
- {
- BP_ZONE("BfCompiler::UpdateDependencyMap");
- BfLogSysM("Compiler::UpdateDependencyMap %d\n", deleteUnusued);
- bool madeFullPass = true;
- if (mCanceling)
- madeFullPass = false;
- if ((mResolvePassData != NULL) && (!mResolvePassData->mParsers.IsEmpty()))
- madeFullPass = false;
- SetAndRestoreValue<bool> prevAssertOnPopulateType(mContext->mAssertOnPopulateType, deleteUnusued && madeFullPass);
- if ((deleteUnusued) && (madeFullPass))
- {
- // Work queues should be empty if we're not canceling
- BF_ASSERT(mContext->mPopulateTypeWorkList.size() == 0);
- BF_ASSERT(mContext->mMethodWorkList.size() == 0);
- }
- // Remove old data in dependency maps, and find types which don't have any references (direct or indirect)
- // to a non-generic type and remove them
- for (int pass = 0; true; pass++)
- {
- // This assert can fail if we have a dependency error, where deleting a type causes a dependent type
- // to be rebuilt
- BF_ASSERT(pass < 100);
- bool foundNew = false;
- for (auto type : mContext->mResolvedTypes)
- {
- if (type != NULL)
- {
- auto depType = type->ToDependedType();
- auto typeInst = type->ToTypeInstance();
- if (depType != NULL)
- {
- extern BfModule* gLastCreatedModule;
- #ifdef _DEBUG
- for (auto itr = depType->mDependencyMap.begin(); itr != depType->mDependencyMap.end(); ++itr)
- {
- auto dependentType = itr->mKey;
- if (dependentType->IsIncomplete())
- {
- BF_ASSERT(dependentType->IsDeleting() || dependentType->IsOnDemand() || !dependentType->HasBeenReferenced() || mCanceling || !madeFullPass || dependentType->IsSpecializedByAutoCompleteMethod());
- }
- }
- #endif
- depType->mDependencyMap.mFlagsUnion = BfDependencyMap::DependencyFlag_None;
- // Not combined with previous loop because PopulateType could modify typeInst->mDependencyMap
- for (auto itr = depType->mDependencyMap.begin(); itr != depType->mDependencyMap.end();)
- {
- auto dependentType = itr->mKey;
- auto depTypeInst = dependentType->ToTypeInstance();
- auto& depData = itr->mValue;
- bool isInvalidVersion = (dependentType->mRevision > depData.mRevision);// && (deleteUnusued) && (madeFullPass);
- //TODO: Just to cause crash if dependentType is deleted
- bool isIncomplete = dependentType->IsIncomplete();
- if ((isInvalidVersion) && (!dependentType->IsDeleting()))
- {
- if (!dependentType->HasBeenReferenced())
- {
- BfLogSysM("Skipping remove of old dependent %p from %p\n", dependentType, typeInst);
- //BF_ASSERT(dependentType->IsGenericTypeInstance());
- // We have a pending type rebuild but we're not sure whether we're being deleted or not yet...
- ++itr;
- continue;
- }
- }
- if ((dependentType->IsDeleting()) || (isInvalidVersion))
- {
- if (dependentType->IsDeleting() || ((deleteUnusued) && (madeFullPass)))
- {
- // If we're deleting the type, OR the dependency of the type has been removed.
- // We detect a removed dependency by the dependent type changing but the dependency revision
- // is older than the dependent type.
- BfLogSysM("Removing old dependent %p from %p\n", dependentType, depType);
- itr = depType->mDependencyMap.erase(itr);
- }
- else
- ++itr;
- }
- else
- {
- // There needs to be more usage than just being used as part of the method specialization's MethodGenericArg.
- // Keep in mind that actually invoking a generic method creates a DependencyFlag_LocalUsage dependency. The
- // DependencyFlag_MethodGenericArg is just used by the owner during creation of the method specialization
- bool isDependentUsage = (depData.mFlags & BfDependencyMap::DependencyFlag_DependentUsageMask) != 0;
- // We need to consider specialized generic types separately, to remove unused specializations
- if (typeInst != NULL)
- {
- bool isDirectReference = (depTypeInst != NULL) && (!depTypeInst->IsOnDemand()) && (!dependentType->IsGenericTypeInstance());
- if ((depTypeInst != NULL) && (typeInst->mLastNonGenericUsedRevision != mRevision) && (isDependentUsage) &&
- ((isDirectReference) || (dependentType->IsUnspecializedType()) || (depTypeInst->mLastNonGenericUsedRevision == mRevision)))
- {
- typeInst->mLastNonGenericUsedRevision = mRevision;
- foundNew = true;
- if (!typeInst->HasBeenReferenced())
- {
- mContext->AddTypeToWorkList(typeInst);
- foundNew = true;
- }
- }
- }
- ++itr;
- }
- depType->mDependencyMap.mFlagsUnion = (BfDependencyMap::DependencyFlags)(depType->mDependencyMap.mFlagsUnion | depData.mFlags);
- }
- if ((!depType->IsGenericTypeInstance() && (!depType->IsBoxed())) ||
- (depType->IsUnspecializedType()) ||
- ((typeInst != NULL) && (typeInst->mLastNonGenericUsedRevision == mRevision)))
- {
- if ((depType->mRebuildFlags & BfTypeRebuildFlag_AwaitingReference) != 0)
- {
- mContext->MarkAsReferenced(depType);
- }
- }
- }
- }
- }
- if (mCanceling)
- madeFullPass = false;
- if (!madeFullPass)
- {
- // We can't delete types based on the dependency map when we're canceling, because we may still
- // have items in the work queues (particularly the mMethodWorkList) that will create
- // new dependencies -- things may unduly be thought to be deleted.
- return;
- }
- if (foundNew)
- {
- // This will work through generic method specializations for the types referenced above, clearing out AwaitingReference flags for
- // newly-referenced generics, and queuing up their method specializations as well
- didWork |= DoWorkLoop(false, false);
- }
- else if (deleteUnusued)
- {
- // Work queues should be empty if we're not canceling
- BF_ASSERT(mContext->mPopulateTypeWorkList.size() == 0);
- BF_ASSERT(mContext->mMethodWorkList.size() == 0);
- // We need to use a delete queue because we trigger a RebuildType for dependent types,
- // but we need to make sure we don't rebuild any types that may be next in line for
- // deletion, so we must set BfTypeRebuildFlag_DeleteQueued first to avoid that
- Array<BfDependedType*> deleteQueue;
- // We bubble out
- for (auto type : mContext->mResolvedTypes)
- {
- auto depType = type->ToDependedType();
- // Delete if we're a generic
- if ((depType != NULL) && (!depType->IsDeleting()))
- {
- auto typeInst = depType->ToTypeInstance();
- bool wantDelete = false;
- if (typeInst != NULL)
- {
- wantDelete = (typeInst->mLastNonGenericUsedRevision != mRevision) &&
- (typeInst->IsGenericTypeInstance() || typeInst->IsBoxed()) && (!typeInst->IsUnspecializedType());
- }
- wantDelete |= (depType->IsOnDemand()) && (depType->mDependencyMap.IsEmpty());
- if (wantDelete)
- {
- deleteQueue.push_back(depType);
- depType->mRebuildFlags = (BfTypeRebuildFlags)(depType->mRebuildFlags | BfTypeRebuildFlag_DeleteQueued);
- foundNew = true;
- }
- }
- }
- for (auto depType : deleteQueue)
- {
- BfLogSysM("Deleting type from deleteQueue in UpdateDependencyMap %p\n", depType);
- mContext->DeleteType(depType, true);
- }
- if (deleteQueue.size() != 0)
- {
- mContext->ValidateDependencies();
- mContext->UpdateAfterDeletingTypes();
- mContext->ValidateDependencies();
- }
- }
- if (!foundNew)
- break;
- }
- #ifdef _DEBUG
- if (deleteUnusued)
- {
- for (auto type : mContext->mResolvedTypes)
- {
- // This flag should be handled by now
- BF_ASSERT((type->mRebuildFlags & BfTypeRebuildFlag_AwaitingReference) == 0);
- }
- }
- #endif
- BP_ZONE("UpdateDependencyMap QueuedSpecializedMethodRebuildTypes");
- HashSet<BfTypeInstance*> specializerSet;
- for (auto rebuildType : mContext->mQueuedSpecializedMethodRebuildTypes)
- {
- if (rebuildType->mRevision != mRevision)
- {
- mContext->RebuildType(rebuildType);
- rebuildType->mRebuildFlags = (BfTypeRebuildFlags)(rebuildType->mRebuildFlags | BfTypeRebuildFlag_SpecializedMethodRebuild);
- for (auto& dep : rebuildType->mDependencyMap)
- {
- auto depType = dep.mKey;
- auto& depData = dep.mValue;
- auto depTypeInst = depType->ToTypeInstance();
- if (depTypeInst == NULL)
- continue;
- if ((depData.mFlags & BfDependencyMap::DependencyFlag_Calls) != 0)
- {
- specializerSet.Add(depTypeInst);
- }
- }
- }
- }
- for (auto depType : specializerSet)
- {
- mContext->QueueMethodSpecializations(depType, true);
- }
- for (auto rebuildType : mContext->mQueuedSpecializedMethodRebuildTypes)
- {
- rebuildType->mRebuildFlags = (BfTypeRebuildFlags)(rebuildType->mRebuildFlags & ~BfTypeRebuildFlag_SpecializedMethodRebuild);
- }
- mContext->mQueuedSpecializedMethodRebuildTypes.Clear();
- mDepsMayHaveDeletedTypes = false;
- }
- void BfCompiler::SanitizeDependencyMap()
- {
- BfLogSysM("SanitizeDependencyMap\n");
- for (auto type : mContext->mResolvedTypes)
- {
- if (type == NULL)
- continue;
- auto depType = type->ToDependedType();
- if (depType == NULL)
- continue;
- // Not combined with previous loop because PopulateType could modify typeInst->mDependencyMap
- for (auto itr = depType->mDependencyMap.begin(); itr != depType->mDependencyMap.end();)
- {
- auto dependentType = itr->mKey;
- auto depTypeInst = dependentType->ToTypeInstance();
- if (dependentType->IsDeleting())
- {
- BfLogSysM("SanitizeDependencyMap removing old dependent %p from %p\n", dependentType, depType);
- itr = depType->mDependencyMap.erase(itr);
- }
- else
- ++itr;
- }
- }
- mContext->RemoveInvalidWorkItems();
- mDepsMayHaveDeletedTypes = false;
- }
- // When we are unsure of whether an old generic instance will survive, we RebuildType but don't put it in any worklist.
- // One of three things happens:
- // 1) It gets built on demand
- // 2) It gets deleted in UpdateDependencyMap
- // 3) It stays undefined and we need to build it here
- bool BfCompiler::ProcessPurgatory(bool reifiedOnly)
- {
- BP_ZONE("BfCompiler::ProcessPurgatory");
- bool didWork = false;
- while (true)
- {
- mContext->RemoveInvalidWorkItems();
- //for (auto type : mGenericInstancePurgatory)
- for (int i = 0; i < (int)mGenericInstancePurgatory.size(); i++)
- {
- auto type = mGenericInstancePurgatory[i];
- if ((reifiedOnly) && (!type->IsReified()))
- continue;
- if ((reifiedOnly) && ((type->mRebuildFlags & BfTypeRebuildFlag_AwaitingReference) != 0))
- continue;
- if (!type->IsDeleting())
- {
- auto module = type->GetModule();
- if (module != NULL)
- {
- if (!module->mIsModuleMutable)
- module->StartExtension();
- module->PopulateType(type, BfPopulateType_Full);
- }
- }
- if (reifiedOnly)
- {
- mGenericInstancePurgatory.RemoveAtFast(i);
- i--;
- }
- }
- if (!reifiedOnly)
- mGenericInstancePurgatory.Clear();
- int prevPurgatorySize = (int)mGenericInstancePurgatory.size();
- if (mContext->ProcessWorkList(reifiedOnly, reifiedOnly))
- didWork = true;
- if (prevPurgatorySize == (int)mGenericInstancePurgatory.size())
- break;
- }
- return didWork;
- }
- bool BfCompiler::VerifySlotNums()
- {
- BP_ZONE("BfCompiler::VerifySlotNums");
- SmallVector<BfTypeInstance*, 16> isSlotUsed;
- for (auto type : mContext->mResolvedTypes)
- {
- if (!type->IsReified())
- continue;
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeInst->IsUnspecializedType())
- continue;
- if (typeInst->IsInterface())
- {
- if (typeInst->mSlotNum == -2)
- continue; // Not used
- if ((typeInst->mVirtualMethodTableSize > 0) && (typeInst->mSlotNum == -1))
- {
- // Slot not assigned yet
- return false;
- }
- continue;
- }
- isSlotUsed.clear();
- isSlotUsed.resize(mMaxInterfaceSlots);
- auto checkType = typeInst;
- while (checkType != NULL)
- {
- for (auto iface : checkType->mInterfaces)
- {
- int slotNum = iface.mInterfaceType->mSlotNum;
- if (slotNum >= 0)
- {
- if ((isSlotUsed[slotNum] != NULL) && (isSlotUsed[slotNum] != iface.mInterfaceType))
- return false; // Collision
- isSlotUsed[slotNum] = iface.mInterfaceType;
- }
- }
- checkType = checkType->mBaseType;
- }
- }
- return true;
- }
- bool BfCompiler::QuickGenerateSlotNums()
- {
- /*SmallVector<bool, 16> isSlotUsed;
- for (auto globalTypeEntry : mResolvedTypes)
- {
- BfType* type = globalTypeEntry->mType;
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeInst->IsInterface())
- {
- if ((typeInst->mVirtualMethodTableSize > 0) && (typeInst->mSlotNum == -1))
- {
- // Slot not assigned yet
- return false;
- }
- continue;
- }
- }
- return VerifySlotNums();*/
- // Implement later
- return false;
- }
- class BfSlotEntry
- {
- public:
- BfTypeInstance* mTypeInstance;
- int mRefCount;
- Array<BfTypeInstance*> mConcurrentRefs;
- };
- typedef std::pair<BfTypeInstance*, BfTypeInstance*> InterfacePair;
- typedef Dictionary<BfTypeInstance*, BfSlotEntry*> SlotEntryMap;
- static BfSlotEntry* GetSlotEntry(SlotEntryMap& slotEntryMap, BfTypeInstance* typeInst)
- {
- BF_ASSERT(typeInst->IsReified());
- BfSlotEntry** slotEntryPtr = NULL;
- if (!slotEntryMap.TryAdd(typeInst, NULL, &slotEntryPtr))
- return *slotEntryPtr;
- BfSlotEntry* slotEntry = new BfSlotEntry();
- slotEntry->mTypeInstance = typeInst;
- slotEntry->mRefCount = 0;
- //insertPair.first->second = slotEntry;
- *slotEntryPtr = slotEntry;
- return slotEntry;
- }
- static InterfacePair MakeInterfacePair(BfTypeInstance* iface1, BfTypeInstance* iface2)
- {
- if (iface1->mTypeId < iface2->mTypeId)
- return InterfacePair(iface1, iface2);
- return InterfacePair(iface2, iface1);
- }
- struct InterfacePairHash
- {
- size_t operator()(const InterfacePair& val) const
- {
- return (((size_t)val.first) >> 2) ^ ((size_t)val.second);
- }
- };
- bool BfCompiler::SlowGenerateSlotNums()
- {
- BP_ZONE("BfCompiler::SlowGenerateSlotNums");
- SlotEntryMap ifaceUseMap;
- std::unordered_set<InterfacePair, InterfacePairHash> concurrentInterfaceSet;
- HashSet<BfTypeInstance*> foundIFaces;
- if (mMaxInterfaceSlots < 0)
- {
- mMaxInterfaceSlots = 0;
- }
- bool isHotCompile = IsHotCompile();
- for (auto type : mContext->mResolvedTypes)
- {
- if (!type->IsReified())
- continue;
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeInst->IsUnspecializedType())
- continue;
- if (typeInst->IsInterface())
- {
- if (typeInst->mSlotNum == -2) // Not needed
- continue;
- if (!isHotCompile) // Hot compiles cannot remap slot numbers
- typeInst->mSlotNum = -1;
- if (typeInst->mVirtualMethodTableSize > 0)
- {
- GetSlotEntry(ifaceUseMap, typeInst);
- }
- continue;
- }
- foundIFaces.Clear();
- auto checkTypeInst = typeInst;
- while (checkTypeInst != NULL)
- {
- for (auto iface : checkTypeInst->mInterfaces)
- {
- auto interfaceType = iface.mInterfaceType;
- if (interfaceType->mSlotNum == -2)
- continue; // Not needed
- if ((isHotCompile) && (interfaceType->mSlotNum == -1))
- checkTypeInst->mDirty = true; // We're about to slot an interface here
- if (interfaceType->mVirtualMethodTableSize > 0)
- {
- BfSlotEntry* slotEntry = GetSlotEntry(ifaceUseMap, interfaceType);
- slotEntry->mRefCount++;
- foundIFaces.Add(iface.mInterfaceType);
- }
- }
- checkTypeInst = checkTypeInst->mBaseType;
- }
- for (auto itr1 = foundIFaces.begin(); itr1 != foundIFaces.end(); ++itr1)
- {
- auto itr2 = itr1;
- ++itr2;
- for ( ; itr2 != foundIFaces.end(); ++itr2)
- {
- auto iface1 = *itr1;
- auto iface2 = *itr2;
- InterfacePair ifacePair = MakeInterfacePair(iface1, iface2);
- if (concurrentInterfaceSet.insert(ifacePair).second)
- {
- BfSlotEntry* entry1 = GetSlotEntry(ifaceUseMap, iface1);
- BfSlotEntry* entry2 = GetSlotEntry(ifaceUseMap, iface2);
- entry1->mConcurrentRefs.push_back(iface2);
- entry2->mConcurrentRefs.push_back(iface1);
- }
- }
- }
- }
- Array<BfSlotEntry*> sortedIfaceUseMap;
- for (auto& entry : ifaceUseMap)
- {
- if (!isHotCompile)
- BF_ASSERT(entry.mValue->mTypeInstance->mSlotNum == -1);
- sortedIfaceUseMap.push_back(entry.mValue);
- }
- std::sort(sortedIfaceUseMap.begin(), sortedIfaceUseMap.end(), [] (BfSlotEntry* lhs, BfSlotEntry* rhs)
- {
- if (lhs->mRefCount != rhs->mRefCount)
- return lhs->mRefCount > rhs->mRefCount;
- return lhs->mTypeInstance->mTypeId < rhs->mTypeInstance->mTypeId;
- });
- bool failed = false;
- SmallVector<bool, 16> isSlotUsed;
- for (auto slotEntry : sortedIfaceUseMap)
- {
- BfTypeInstance* iface = slotEntry->mTypeInstance;
- if (iface->mSlotNum >= 0)
- {
- BF_ASSERT(isHotCompile);
- continue;
- }
- isSlotUsed.clear();
- if (mMaxInterfaceSlots > 0)
- isSlotUsed.resize(mMaxInterfaceSlots);
- BF_ASSERT(iface->mSlotNum == -1);
- BF_ASSERT(iface->IsInterface());
- for (auto iface2 : slotEntry->mConcurrentRefs)
- {
- int slotNum2 = iface2->mSlotNum;
- if (slotNum2 != -1)
- isSlotUsed[slotNum2] = true;
- }
- for (int checkSlot = 0; checkSlot < mMaxInterfaceSlots; checkSlot++)
- {
- if (!isSlotUsed[checkSlot])
- {
- iface->mSlotNum = checkSlot;
- break;
- }
- }
- if (iface->mSlotNum == -1)
- {
- if (isHotCompile)
- {
- failed = true;
- mPassInstance->Fail("Interface slot numbering overflow. Restart the program or revert changes.");
- break;
- }
- iface->mSlotNum = mMaxInterfaceSlots;
- if (mOptions.mIncrementalBuild)
- {
- // Allocate more than enough interface slots
- mMaxInterfaceSlots += 3;
- }
- else
- mMaxInterfaceSlots++;
- // failed = true;
- // mPassInstance->Fail(StrFormat("Interface slot numbering overflow, increase the maximum slot number from '%d'", mMaxInterfaceSlots));
- // break;
- }
- // if (iface->mSlotNum == -1)
- // {
- // failed = true;
- // mPassInstance->Fail(StrFormat("Interface slot numbering overflow, increase the maximum slot number from '%d'", mMaxInterfaceSlots));
- // break;
- // }
- if (isHotCompile)
- {
- mHotState->mNewlySlottedTypeIds.Add(iface->mTypeId);
- mHotState->mSlotDefineTypeIds.Add(iface->mTypeId);
- }
- }
- if (!failed)
- {
- bool success = VerifySlotNums();
- if ((!success) && (!isHotCompile))
- {
- BF_DBG_FATAL("Failed!");
- }
- }
- for (auto& entry : ifaceUseMap)
- delete entry.mValue;
- return true;
- }
- void BfCompiler::GenerateSlotNums()
- {
- BP_ZONE("BfCompiler::GenerateSlotNums");
- if (mMaxInterfaceSlots < 0)
- {
- if (mOptions.mIncrementalBuild)
- mMaxInterfaceSlots = 3;
- else
- mMaxInterfaceSlots = 0;
- }
- bool isHotCompile = IsHotCompile();
- for (auto type : mContext->mResolvedTypes)
- {
- if (!type->IsInterface())
- continue;
- auto typeInstance = type->ToTypeInstance();
- if ((typeInstance->mSlotNum <= 0) || (!isHotCompile))
- {
- if ((mContext->mReferencedIFaceSlots.Contains(typeInstance)) ||
- (typeInstance->mHasBeenInstantiated) || (typeInstance->IncludeAllMethods()))
- {
- if (typeInstance->mSlotNum == -2)
- typeInstance->mSlotNum = -1;
- }
- else
- typeInstance->mSlotNum = -2; // Not needed
- }
- }
- if (VerifySlotNums())
- return;
- if (!QuickGenerateSlotNums())
- SlowGenerateSlotNums();
- BfLogSysM("GenerateSlotNums mMaxInterfaceSlots: %d\n", mMaxInterfaceSlots);
- }
- void BfCompiler::GenerateDynCastData()
- {
- BP_ZONE("BfCompiler::GenerateDynCastData");
- Array<int> firstDerivedIds;
- Array<int> nextSiblingIds;
- firstDerivedIds.Resize(mCurTypeId);
- nextSiblingIds.Resize(mCurTypeId);
- for (auto type : mContext->mResolvedTypes)
- {
- if (type->IsBoxed())
- continue;
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeInst->mBaseType == NULL)
- continue;
- int baseId = typeInst->mBaseType->mTypeId;
- int firstDerivedId = firstDerivedIds[baseId];
- nextSiblingIds[typeInst->mTypeId] = firstDerivedIds[baseId];
- firstDerivedIds[baseId] = typeInst->mTypeId;
- }
- int curInheritanceId = 1;
- std::function<void(BfTypeInstance*)> _AddTypeInfo = [&](BfTypeInstance* typeInst)
- {
- if (typeInst->mInheritanceId != curInheritanceId)
- {
- typeInst->mInheritanceId = curInheritanceId;
- typeInst->mDirty = true;
- }
- curInheritanceId++;
- int childId = firstDerivedIds[typeInst->mTypeId];
- while (childId != 0)
- {
- auto childType = mContext->mTypes[childId]->ToTypeInstance();
- _AddTypeInfo(childType);
- childId = nextSiblingIds[childId];
- }
- int inheritanceCount = curInheritanceId - typeInst->mInheritanceId - 1;
- if (typeInst->mInheritanceCount != inheritanceCount)
- {
- typeInst->mInheritanceCount = inheritanceCount;
- typeInst->mDirty = true;
- }
- };
- _AddTypeInfo(mContext->mBfObjectType);
- auto valueTypeInst = mContext->mScratchModule->ResolveTypeDef(mValueTypeTypeDef)->ToTypeInstance();
- _AddTypeInfo(valueTypeInst);
- }
- void BfCompiler::UpdateRevisedTypes()
- {
- BfLogSysM("BfCompiler::UpdateRevisedTypes\n");
- BP_ZONE("BfCompiler::UpdateRevisedTypes");
- // See if we have any name conflicts and remove those
- auto typeDefItr = mSystem->mTypeDefs.begin();
- while (typeDefItr != mSystem->mTypeDefs.end())
- {
- auto typeDef = *typeDefItr;
- auto origTypeDef = typeDef;
- if (typeDef->mNextRevision != NULL)
- typeDef = typeDef->mNextRevision;
- if (typeDef->mDupDetectedRevision == mRevision)
- {
- ++typeDefItr;
- continue;
- }
- typeDef->mDupDetectedRevision = -1;
- if ((typeDef->mIsCombinedPartial) || (typeDef->mDefState == BfTypeDef::DefState_Deleted) || (typeDef->mTypeCode == BfTypeCode_Extension))
- {
- ++typeDefItr;
- continue;
- }
- if ((!typeDef->IsGlobalsContainer()) && (mSystem->ContainsNamespace(typeDef->mFullName, typeDef->mProject)))
- {
- mPassInstance->Fail(StrFormat("The name '%s' is already defined to be a namespace name", typeDef->mFullName.ToString().c_str()), typeDef->mTypeDeclaration->mNameNode);
- }
- bool removedElement = false;
- auto nextTypeDefItr = typeDefItr;
- nextTypeDefItr.MoveToNextHashMatch();
- while (nextTypeDefItr)
- {
- auto nextTypeDef = *nextTypeDefItr;
- if (nextTypeDef->mNextRevision != NULL)
- nextTypeDef = nextTypeDef->mNextRevision;
- if ((nextTypeDef->mIsCombinedPartial) || (nextTypeDef->mDefState == BfTypeDef::DefState_Deleted) || (nextTypeDef->mTypeCode == BfTypeCode_Extension) ||
- (typeDef->mFullNameEx != nextTypeDef->mFullNameEx) || (typeDef->mGenericParamDefs.size() != nextTypeDef->mGenericParamDefs.size()))
- {
- nextTypeDefItr.MoveToNextHashMatch();
- continue;
- }
- if ((typeDef->mIsPartial) && (nextTypeDef->mIsPartial) &&
- (!typeDef->IsGlobalsContainer()) &&
- (typeDef->mProject != nextTypeDef->mProject))
- {
- BfTypeDef* typeA = NULL;
- BfTypeDef* typeB = NULL;
- BfError* error = NULL;
- if (typeDef->mProject->ReferencesOrReferencedBy(nextTypeDef->mProject))
- {
- typeA = typeDef;
- typeB = nextTypeDef;
- }
- else if (nextTypeDef->mProject->ReferencesOrReferencedBy(typeDef->mProject))
- {
- typeA = nextTypeDef;
- typeB = typeDef;
- }
- if (typeA != NULL)
- {
- error = mPassInstance->Fail(StrFormat("Partial type in project '%s' cannot extend a type from a referenced project", typeA->mProject->mName.c_str()).c_str(),
- typeA->mTypeDeclaration->mNameNode);
- mPassInstance->MoreInfo(StrFormat("Previous definition in project '%s'", typeB->mProject->mName.c_str()),
- typeB->mTypeDeclaration->mNameNode);
- }
- if (error != NULL)
- error->mIsPersistent = true;
- }
- if (((!typeDef->mIsPartial) || (!nextTypeDef->mIsPartial)) &&
- (!typeDef->IsGlobalsContainer()) && (!nextTypeDef->IsGlobalsContainer()) &&
- (typeDef->mProject->ReferencesOrReferencedBy(nextTypeDef->mProject)))
- {
- nextTypeDef->mDupDetectedRevision = mRevision;
- BfError* error = NULL;
- /*if ((typeDef->mIsPartial) && (typeDef->mTypeCode != BfTypeCode_Extension))
- {
- error = mPassInstance->Fail("Missing 'partial' modifier; another partial definition of this type exists", nextTypeDef->mTypeDeclaration->mNameNode);
- mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
- }
- else if ((nextTypeDef->mIsPartial) && (nextTypeDef->mTypeCode != BfTypeCode_Extension))
- {
- error = mPassInstance->Fail("Missing 'partial' modifier; another partial definition of this type exists", typeDef->mTypeDeclaration->mNameNode);
- mPassInstance->MoreInfo("Previous definition", nextTypeDef->mTypeDeclaration->mNameNode);
- }
- else */if (nextTypeDef->mOuterType != NULL)
- {
- 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,
- nextTypeDef->mName->mString.mPtr), nextTypeDef->mTypeDeclaration->mNameNode);
- mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
- }
- else if (!nextTypeDef->mNamespace.IsEmpty())
- {
- error = mPassInstance->Fail(StrFormat("The namespace '%s' already has a definition for '%s'", nextTypeDef->mNamespace.ToString().c_str(),
- nextTypeDef->mName->mString.mPtr), nextTypeDef->mTypeDeclaration->mNameNode);
- mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
- }
- else
- {
- error = mPassInstance->Fail(StrFormat("The global namespace already has a definition for '%s'",
- nextTypeDef->mName->mString.mPtr), nextTypeDef->mTypeDeclaration->mNameNode);
- mPassInstance->MoreInfo("Previous definition", typeDef->mTypeDeclaration->mNameNode);
- }
- if (error != NULL)
- error->mIsPersistent = true;
- }
- nextTypeDefItr.MoveToNextHashMatch();
- }
- ++typeDefItr;
- }
- mContext->PreUpdateRevisedTypes();
- // If we missed out on required types previously, now we should be 'okay'
- mInInvalidState = false;
- // We can't do any yields in here - the compiler state is invalid from the time we inject a new
- // typedef revision up until we finish the associated RebuildType
- int compositeBucket = 0;
- // These are "extension" defs that were unmatched last run through
- Array<BfTypeDef*> prevSoloExtensions;
- mSystem->mTypeDefs.CheckRehash();
- Array<BfProject*> corlibProjects;
- //
- {
- BfAtomComposite objectName;
- if (mSystem->ParseAtomComposite("System.Object", objectName))
- {
- for (auto itr = mSystem->mTypeDefs.TryGet(objectName); itr != mSystem->mTypeDefs.end(); ++itr)
- {
- BfTypeDef* typeDef = *itr;
- if ((typeDef->mFullName == objectName) && (typeDef->mTypeCode == BfTypeCode_Object))
- corlibProjects.Add(typeDef->mProject);
- }
- }
- }
- // Process the typedefs one bucket at a time. When we are combining extensions or partials (globals) into a single definition then
- // we will be making multiple passes over the bucket that contains that name
- for (int bucketIdx = 0; bucketIdx < mSystem->mTypeDefs.mHashSize; bucketIdx++)
- {
- bool hadPartials = false;
- bool hadChanges = false;
- if (mSystem->mTypeDefs.mHashHeads == NULL)
- break;
- // Partials combiner
- auto outerTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
- while (outerTypeDefEntryIdx != -1)
- {
- // Make sure we can fit a composite without reallocating
- mSystem->mTypeDefs.EnsureFreeCount(1);
- auto outerTypeDefEntry = &mSystem->mTypeDefs.mEntries[outerTypeDefEntryIdx];
- auto outerTypeDef = outerTypeDefEntry->mValue;
- if (outerTypeDef->mDefState == BfTypeDef::DefState_Deleted)
- {
- hadChanges = true;
- outerTypeDefEntryIdx = mSystem->mTypeDefs.mEntries[outerTypeDefEntryIdx].mNext;
- continue;
- }
- if (outerTypeDef->mNextRevision != NULL)
- hadChanges = true;
- BfTypeDefMap::Entry* rootTypeDefEntry = NULL;
- BfTypeDef* rootTypeDef = NULL;
- BfTypeDef* compositeTypeDef = NULL;
- BfProject* compositeProject = NULL;
- auto latestOuterTypeDef = outerTypeDef->GetLatest();
- if ((outerTypeDef->mTypeCode == BfTypeCode_Extension) && (!outerTypeDef->mIsPartial))
- {
- prevSoloExtensions.Add(outerTypeDef);
- outerTypeDef->mIsPartial = true;
- }
- if ((outerTypeDef->mIsPartial) || (outerTypeDef->mIsCombinedPartial))
- {
- // Initialize mPartialUsed flags
- if (!hadPartials)
- {
- auto checkTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
- while (checkTypeDefEntryIdx != -1)
- {
- // This clears the mPartialUsed flag for the whole bucket
- auto checkTypeDef = mSystem->mTypeDefs.mEntries[checkTypeDefEntryIdx].mValue;
- if ((checkTypeDef->mIsPartial) || (checkTypeDef->mIsCombinedPartial))
- checkTypeDef->mPartialUsed = false;
- checkTypeDefEntryIdx = mSystem->mTypeDefs.mEntries[checkTypeDefEntryIdx].mNext;
- }
- hadPartials = true;
- }
- }
- bool isExplicitPartial = outerTypeDef->mIsExplicitPartial;
- bool failedToFindRootType = false;
- if ((outerTypeDef->mTypeCode == BfTypeCode_Extension) && (!outerTypeDef->mPartialUsed))
- {
- // Find root type, and we assume the composite type follows this
- auto checkTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
- while (checkTypeDefEntryIdx != -1)
- {
- auto checkTypeDefEntry = &mSystem->mTypeDefs.mEntries[checkTypeDefEntryIdx];
- auto checkTypeDef = checkTypeDefEntry->mValue;
- if ((checkTypeDefEntry->mHashCode != outerTypeDefEntry->mHashCode) ||
- (checkTypeDef->mIsCombinedPartial) ||
- (checkTypeDef->mTypeCode == BfTypeCode_Extension) ||
- (checkTypeDef->mDefState == BfTypeDef::DefState_Deleted) ||
- (checkTypeDef->mPartialUsed) ||
- (!checkTypeDef->NameEquals(outerTypeDef)) ||
- (checkTypeDef->mGenericParamDefs.size() != outerTypeDef->mGenericParamDefs.size()) ||
- (!outerTypeDef->mProject->ContainsReference(checkTypeDef->mProject)))
- {
- checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
- continue;
- }
- // Only allow extending structs, objects, and interfaces
- if ((checkTypeDef->mTypeCode == BfTypeCode_Struct) ||
- (checkTypeDef->mTypeCode == BfTypeCode_Object) ||
- (checkTypeDef->mTypeCode == BfTypeCode_Enum) ||
- (checkTypeDef->mTypeCode == BfTypeCode_Interface))
- {
- rootTypeDef = checkTypeDef;
- rootTypeDefEntry = checkTypeDefEntry;
- compositeProject = rootTypeDef->mProject;
- }
- checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
- if (compositeTypeDef != NULL)
- {
- BF_ASSERT(rootTypeDef->mFullNameEx == compositeTypeDef->mFullNameEx);
- }
- }
- if (rootTypeDef == NULL)
- {
- failedToFindRootType = true;
- isExplicitPartial = true;
- }
- }
- if ((isExplicitPartial) && (!outerTypeDef->mPartialUsed))
- {
- // For explicit partials there is no 'root type' so we want to select any partial in the 'innermost' project
- rootTypeDef = outerTypeDef;
- rootTypeDefEntry = outerTypeDefEntry;
- compositeProject = rootTypeDef->mProject;
- // Find composite type, there is no explicit position for this
- auto checkTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
- while (checkTypeDefEntryIdx != -1)
- {
- auto checkTypeDefEntry = &mSystem->mTypeDefs.mEntries[checkTypeDefEntryIdx];
- auto checkTypeDef = checkTypeDefEntry->mValue;
- if ((checkTypeDefEntry->mHashCode != outerTypeDefEntry->mHashCode) ||
- (checkTypeDef->mPartialUsed) ||
- (checkTypeDef->mDefState == BfTypeDef::DefState_Deleted) ||
- (!checkTypeDef->NameEquals(outerTypeDef)) ||
- (checkTypeDef->mGenericParamDefs.size() != outerTypeDef->mGenericParamDefs.size()))
- {
- checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
- continue;
- }
- if (!checkTypeDef->mIsCombinedPartial)
- {
- // Select the innermost project for the composite project def
- if (compositeProject != checkTypeDef->mProject)
- {
- if (checkTypeDef->mProject->ContainsReference(compositeProject))
- {
- // Fine, already contains it
- }
- else if (compositeProject->ContainsReference(checkTypeDef->mProject))
- {
- rootTypeDef = checkTypeDef;
- rootTypeDefEntry = checkTypeDefEntry;
- compositeProject = rootTypeDef->mProject;
- }
- else
- {
- // Try 'corlib'
- for (auto corlibProject : corlibProjects)
- {
- if ((rootTypeDef->mProject->ContainsReference(corlibProject)) &&
- (checkTypeDef->mProject->ContainsReference(corlibProject)))
- compositeProject = corlibProject;
- }
- }
- }
- checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
- continue;
- }
- if (!rootTypeDef->mProject->ContainsReference(checkTypeDef->mProject))
- {
- checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
- continue;
- }
- compositeTypeDef = checkTypeDef;
- if (rootTypeDef != NULL)
- {
- BF_ASSERT(rootTypeDef->mFullNameEx == compositeTypeDef->mFullNameEx);
- }
- if (compositeTypeDef->mNextRevision != NULL)
- {
- // This is an old 'next revision'
- delete compositeTypeDef->mNextRevision;
- compositeTypeDef->mNextRevision = NULL;
- if (compositeTypeDef->mDefState != BfTypeDef::DefState_Deleted)
- compositeTypeDef->mDefState = BfTypeDef::DefState_Defined;
- }
- checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
- }
- }
- // Now find extensions to apply to the rootTypeDef
- if (rootTypeDef != NULL)
- {
- bool partialsHadChanges = false;
- bool hadSignatureChange = false;
- bool compositeIsNew = false;
- if (compositeTypeDef == NULL)
- {
- BfTypeDefMap::Entry* nextEntry = NULL;
- if (rootTypeDefEntry->mNext != -1)
- nextEntry = &mSystem->mTypeDefs.mEntries[rootTypeDefEntry->mNext];
- if ((rootTypeDef->mIsExplicitPartial) || (nextEntry == NULL) ||
- (!nextEntry->mValue->mIsCombinedPartial) ||
- (nextEntry->mValue->mTypeCode != rootTypeDef->mTypeCode) ||
- (nextEntry->mValue->mIsFunction != rootTypeDef->mIsFunction) ||
- (nextEntry->mValue->mIsDelegate != rootTypeDef->mIsDelegate) ||
- (nextEntry->mValue->mGenericParamDefs.size() != rootTypeDef->mGenericParamDefs.size()))
- {
- compositeTypeDef = new BfTypeDef();
- compositeTypeDef->mSystem = rootTypeDef->mSystem;
- compositeTypeDef->mProject = compositeProject;
- compositeTypeDef->mName = rootTypeDef->mName;
- compositeTypeDef->mName->mRefCount++;
- mSystem->TrackName(compositeTypeDef);
- compositeTypeDef->mNameEx = rootTypeDef->mNameEx;
- compositeTypeDef->mNameEx->Ref();
- compositeTypeDef->mProtection = rootTypeDef->mProtection;
- compositeTypeDef->mNamespace = rootTypeDef->mNamespace;
- if (rootTypeDef->mTypeCode == BfTypeCode_Extension)
- compositeTypeDef->mTypeCode = BfTypeCode_Struct;
- else
- compositeTypeDef->mTypeCode = rootTypeDef->mTypeCode;
- compositeTypeDef->mFullName = rootTypeDef->mFullName;
- compositeTypeDef->mFullNameEx = rootTypeDef->mFullNameEx;
- compositeTypeDef->mIsOpaque = rootTypeDef->mIsOpaque;
- compositeTypeDef->mIsFunction = rootTypeDef->mIsFunction;
- compositeTypeDef->mIsDelegate = rootTypeDef->mIsDelegate;
- compositeTypeDef->mIsCombinedPartial = true;
- for (auto prevGenericParam : rootTypeDef->mGenericParamDefs)
- {
- BfGenericParamDef* copiedGenericParam = new BfGenericParamDef();
- *copiedGenericParam = *prevGenericParam;
- compositeTypeDef->mGenericParamDefs.Add(copiedGenericParam);
- }
- mSystem->mTypeDefs.AddAfter(compositeTypeDef, rootTypeDefEntry);
- partialsHadChanges = true;
- hadSignatureChange = true;
- compositeIsNew = true;
- BfLogSysM("Creating compositeTypeDef %p\n", compositeTypeDef);
- }
- else
- {
- BF_ASSERT(nextEntry->mValue->NameEquals(rootTypeDef));
- compositeTypeDef = nextEntry->mValue;
- if (rootTypeDef != NULL)
- {
- BF_ASSERT(rootTypeDef->mFullNameEx == compositeTypeDef->mFullNameEx);
- }
- if (compositeTypeDef->mNextRevision != NULL)
- {
- // This is an old 'next revision'
- mSystem->InjectNewRevision(compositeTypeDef);
- BF_ASSERT(compositeTypeDef->mNextRevision == NULL);
- }
- }
- }
- else
- {
- // These may not get caught below if the composite project changes
- for (auto checkTypeDef : compositeTypeDef->mPartials)
- {
- if (checkTypeDef->mDefState == BfTypeDef::DefState_Deleted)
- {
- partialsHadChanges = true;
- hadSignatureChange = true;
- }
- }
- }
- // Collect the partials
- BfSizedVector<BfTypeDef*, 8> typeParts;
- typeParts.push_back(rootTypeDef);
- auto checkTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
- while (checkTypeDefEntryIdx != -1)
- {
- auto checkTypeDefEntry = &mSystem->mTypeDefs.mEntries[checkTypeDefEntryIdx];
- auto checkTypeDef = checkTypeDefEntry->mValue;
- bool isValidProject = checkTypeDef->mProject->ContainsReference(compositeProject);
- if (checkTypeDef != rootTypeDef)
- {
- if ((checkTypeDef->mIsCombinedPartial) ||
- ((!checkTypeDef->mIsPartial) && (checkTypeDef->mTypeCode != BfTypeCode_Extension)) ||
- (checkTypeDef->mPartialUsed) ||
- (!checkTypeDef->NameEquals(rootTypeDef)) ||
- (checkTypeDef->mGenericParamDefs.size() != rootTypeDef->mGenericParamDefs.size()) ||
- (!isValidProject))
- {
- checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
- continue;
- }
- }
- if (checkTypeDef->mTypeCode == BfTypeCode_Extension)
- {
- // This was an extension that was orphaned but now we're taking it back
- checkTypeDef->mIsPartial = true;
- }
- compositeTypeDef->mPartialUsed = true;
- checkTypeDef->mPartialUsed = true;
- if (checkTypeDef->mDefState == BfTypeDef::DefState_Deleted)
- {
- partialsHadChanges = true;
- hadSignatureChange = true;
- }
- else
- {
- if (checkTypeDef != rootTypeDef)
- typeParts.push_back(checkTypeDef);
- if (checkTypeDef->mNextRevision != NULL)
- {
- partialsHadChanges = true;
- BF_ASSERT(checkTypeDef->mNextRevision->mGenericParamDefs.size() == rootTypeDef->mGenericParamDefs.size());
- //mSystem->InjectNewRevision(checkTypeDef);
- //BF_ASSERT(checkTypeDef->mGenericParamDefs.size() == rootTypeDef->mGenericParamDefs.size());
- }
- else if (checkTypeDef->mDefState == BfTypeDef::DefState_New)
- partialsHadChanges = true;
- }
- checkTypeDefEntryIdx = checkTypeDefEntry->mNext;
- }
- // Set this down here, because the InjectNewRevision will clear this flag
- rootTypeDef->mIsPartial = true;
- if (partialsHadChanges)
- {
- BF_ASSERT(compositeTypeDef->mNextRevision == NULL);
- mSystem->VerifyTypeDef(compositeTypeDef);
- for (auto checkTypeDef : typeParts)
- {
- mSystem->VerifyTypeDef(checkTypeDef);
- // Apply any def state that is more conservative
- if (checkTypeDef->mDefState == BfTypeDef::DefState_Signature_Changed)
- compositeTypeDef->mDefState = BfTypeDef::DefState_Signature_Changed;
- else if (checkTypeDef->mDefState == BfTypeDef::DefState_InlinedInternals_Changed)
- {
- if (compositeTypeDef->mDefState != BfTypeDef::DefState_Signature_Changed)
- compositeTypeDef->mDefState = BfTypeDef::DefState_InlinedInternals_Changed;
- }
- else if (checkTypeDef->mDefState == BfTypeDef::DefState_Internals_Changed)
- {
- if ((compositeTypeDef->mDefState != BfTypeDef::DefState_Signature_Changed) &&
- (compositeTypeDef->mDefState != BfTypeDef::DefState_InlinedInternals_Changed))
- compositeTypeDef->mDefState = BfTypeDef::DefState_Internals_Changed;
- }
- else if (checkTypeDef->mDefState == BfTypeDef::DefState_Refresh)
- {
- if ((compositeTypeDef->mDefState != BfTypeDef::DefState_Signature_Changed) &&
- (compositeTypeDef->mDefState != BfTypeDef::DefState_InlinedInternals_Changed) &&
- (compositeTypeDef->mDefState != BfTypeDef::DefState_Internals_Changed))
- compositeTypeDef->mDefState = BfTypeDef::DefState_Refresh;
- }
- BF_ASSERT(checkTypeDef->mIsPartial);
- if (checkTypeDef->mNextRevision != NULL)
- {
- mSystem->VerifyTypeDef(checkTypeDef->mNextRevision);
- mSystem->InjectNewRevision(checkTypeDef);
- }
- checkTypeDef->mIsPartial = true;
- checkTypeDef->mDefState = BfTypeDef::DefState_Defined;
- mSystem->AddToCompositePartial(mPassInstance, compositeTypeDef, checkTypeDef);
- BfLogSysM("AddToCompositePartial %p added to %p\n", checkTypeDef, compositeTypeDef);
- }
- mSystem->FinishCompositePartial(compositeTypeDef);
- if (!compositeIsNew)
- {
- if (compositeTypeDef->mNextRevision != NULL)
- {
- BF_ASSERT(compositeTypeDef->mPartials.size() != 0);
- }
- }
- // We use the root typedef's namespace search for the composite, but this should only be
- // used for cases where we CANNOT specify a typeref on an extension. IE: custom attributes
- // for a type can only be added on the root typedef. If this changes then we need to make
- // sure that we attach a definingType to attributes
- for (auto name : compositeTypeDef->mNamespaceSearch)
- mSystem->ReleaseAtomComposite(name);
- compositeTypeDef->mNamespaceSearch = rootTypeDef->mNamespaceSearch;
- for (auto name : compositeTypeDef->mNamespaceSearch)
- mSystem->RefAtomComposite(name);
- if (rootTypeDef != NULL)
- compositeTypeDef->mNamespaceSearch = rootTypeDef->mNamespaceSearch;
- else
- compositeTypeDef->mNamespaceSearch.Clear();
- //BfLogSysM("Composite type %p updating. isNew: %d\n", compositeTypeDef, compositeIsNew);
- if (compositeIsNew)
- {
- compositeTypeDef->mDefState = BfTypeDef::DefState_New;
- if (compositeTypeDef->mNextRevision->mTypeCode != BfTypeCode_Extension)
- compositeTypeDef->mTypeCode = compositeTypeDef->mNextRevision->mTypeCode;
- else
- compositeTypeDef->mNextRevision->mTypeCode = compositeTypeDef->mTypeCode;
- mSystem->InjectNewRevision(compositeTypeDef);
- // Reset 'New' state
- compositeTypeDef->mDefState = BfTypeDef::DefState_New;
- }
- else
- {
- if (compositeTypeDef->mNextRevision->mTypeCode == BfTypeCode_Extension)
- compositeTypeDef->mNextRevision->mTypeCode = compositeTypeDef->mTypeCode;
- if (hadSignatureChange)
- compositeTypeDef->mDefState = BfTypeDef::DefState_Signature_Changed;
- }
- if (compositeTypeDef->mDefState == BfTypeDef::DefState_Defined)
- {
- // No changes, just inject
- mSystem->InjectNewRevision(compositeTypeDef);
- }
- /*if (compositeTypeDef->mTypeCode == BfTypeCode_Extension)
- {
- BF_ASSERT(rootTypeDef == NULL);
- compositeTypeDef->mTypeCode = BfTypeCode_Object;
- }*/
- auto latestCompositeTypeDef = compositeTypeDef->GetLatest();
- if (latestCompositeTypeDef->mTypeCode == BfTypeCode_Extension)
- {
- BF_ASSERT(rootTypeDef == NULL);
- latestCompositeTypeDef->mTypeCode = BfTypeCode_Struct;
- }
- BfLogSysM("Partial combined type typedef %p updated from parser %p\n", compositeTypeDef, latestCompositeTypeDef->mTypeDeclaration->GetSourceData());
- }
- if (failedToFindRootType)
- {
- for (auto partialTypeDef : compositeTypeDef->GetLatest()->mPartials)
- {
- if (partialTypeDef->IsExtension())
- {
- // Couldn't find a root type def, treat ourselves as an explicit partial
- auto error = mPassInstance->Fail(StrFormat("Unable to find root type definition for extension '%s'", BfTypeUtils::TypeToString(partialTypeDef->GetLatest()).c_str()),
- partialTypeDef->GetLatest()->mTypeDeclaration->mNameNode);
- if (error != NULL)
- error->mIsPersistent = true;
- }
- }
- }
- else
- {
- for (auto partialTypeDef : compositeTypeDef->GetLatest()->mPartials)
- {
- if (partialTypeDef->IsExtension())
- {
- for (int genericParamIdx = 0; genericParamIdx < BF_MIN(partialTypeDef->mGenericParamDefs.mSize, rootTypeDef->mGenericParamDefs.mSize); genericParamIdx++)
- {
- auto rootParamDef = rootTypeDef->mGenericParamDefs[genericParamIdx];
- auto extParamDef = partialTypeDef->mGenericParamDefs[genericParamIdx];
- if ((rootParamDef->mName != extParamDef->mName) &&
- (!rootParamDef->mNameNodes.IsEmpty()) &&
- (!extParamDef->mNameNodes.IsEmpty()))
- {
- String specializedName = extParamDef->mName;
- auto paramDefTypeDef = mSystem->FindTypeDef(extParamDef->mName, 0);
- if (paramDefTypeDef != NULL)
- {
- switch (paramDefTypeDef->mTypeCode)
- {
- case BfTypeCode_Boolean: specializedName = "Boolean"; break;
- default:
- if ((paramDefTypeDef->mTypeCode >= BfTypeCode_Boolean) && (paramDefTypeDef->mTypeCode <= BfTypeCode_Double))
- {
- specializedName[0] = ::toupper(specializedName[0]);
- if (specializedName.StartsWith("Uint"))
- specializedName[1] = 'I';
- }
- }
- }
- auto error = mPassInstance->Warn(0, StrFormat("Extension generic param name '%s' does not match root definition generic param name '%s'. If generic specialization was intended then consider using 'where %s : %s'.",
- extParamDef->mName.c_str(), rootParamDef->mName.c_str(), rootParamDef->mName.c_str(), specializedName.c_str()),
- extParamDef->mNameNodes[0]);
- if (error != NULL)
- {
- error->mIsPersistent = true;
- mPassInstance->MoreInfo("See root definition", rootParamDef->mNameNodes[0]);
- }
- }
- }
- }
- }
- }
- }
- outerTypeDefEntryIdx = outerTypeDefEntry->mNext;
- }
- // Handle unused partials, apply any new revisions, process pending deletes
- if ((hadPartials) || (hadChanges))
- {
- BfTypeDef* checkMasterTypeDef = NULL;
- BfTypeDef* deletedCombinedPartial = NULL;
- outerTypeDefEntryIdx = mSystem->mTypeDefs.mHashHeads[bucketIdx];
- while (outerTypeDefEntryIdx != -1)
- {
- auto outerTypeDefEntry = &mSystem->mTypeDefs.mEntries[outerTypeDefEntryIdx];
- auto outerTypeDef = outerTypeDefEntry->mValue;
- auto nextTypeDefEntryIdx = outerTypeDefEntry->mNext;
- BfTypeDefMap::Entry* nextTypeDefEntry = NULL;
- if (nextTypeDefEntryIdx != -1)
- nextTypeDefEntry = &mSystem->mTypeDefs.mEntries[nextTypeDefEntryIdx];
- if ((outerTypeDef->mIsPartial) && (!outerTypeDef->mIsExplicitPartial) && (outerTypeDef->mTypeCode != BfTypeCode_Extension) &&
- (nextTypeDefEntry != NULL) && (!nextTypeDefEntry->mValue->mPartialUsed))
- {
- // This is a root type that we've removed all extensions from, so now we go back to treating it as the actual definition
- // instead of using the composite that immediately follows it
- BF_ASSERT(outerTypeDef->mTypeCode != BfTypeCode_Extension);
- outerTypeDef->mIsPartial = false;
- outerTypeDef->mPartialIdx = -1;
- }
- if (outerTypeDef->mDefState == BfTypeDef::DefState_Deleted)
- {
- BfLogSysM("UpdateRevisedTypes deleting outerTypeDef %p\n", outerTypeDef);
- outerTypeDef->mDefState = BfTypeDef::DefState_Deleted;
- mSystem->RemoveTypeDef(outerTypeDef);
- }
- else if (!outerTypeDef->mPartialUsed)
- {
- if (outerTypeDef->mIsCombinedPartial)
- {
- BfLogSysM("UpdateRevisedTypes deleting combinedPartial type %p\n", outerTypeDef);
- deletedCombinedPartial = outerTypeDef;
- outerTypeDef->mDefState = BfTypeDef::DefState_Deleted;
- mSystem->RemoveTypeDef(outerTypeDef);
- }
- else if (outerTypeDef->mTypeCode == BfTypeCode_Extension)
- {
- auto error = mPassInstance->Fail(StrFormat("Unable to find root type definition for extension '%s'", outerTypeDef->GetLatest()->mFullName.ToString().c_str()),
- outerTypeDef->GetLatest()->mTypeDeclaration->mNameNode);
- if (error != NULL)
- error->mIsPersistent = true;
- if (outerTypeDef->mIsPartial)
- {
- // Allow this typeDef be a full solo type by itself
- //outerTypeDef->mTypeCode = BfTypeCode_Struct;
- outerTypeDef->mIsPartial = false;
- if (outerTypeDef->mNextRevision != NULL)
- outerTypeDef->mNextRevision->mIsPartial = false;
- if (outerTypeDef->mPartialIdx != -1)
- {
- outerTypeDef->mPartialIdx = -1;
- outerTypeDef->mDefState = BfTypeDef::DefState_New;
- }
- }
- }
- }
- outerTypeDefEntryIdx = nextTypeDefEntryIdx;
- }
- }
- }
- for (auto typeDef : prevSoloExtensions)
- {
- // If this got added to a composite partial then delete the previous solo type
- if (typeDef->mIsPartial)
- {
- BfLogSysM("Solo partial going back to normal partial %p\n", typeDef);
- typeDef->mIsPartial = false;
- auto type = mContext->mScratchModule->ResolveTypeDef(typeDef, BfPopulateType_Identity);
- mContext->DeleteType(type);
- typeDef->mIsPartial = true;
- }
- }
- mContext->UpdateRevisedTypes();
- mContext->VerifyTypeLookups();
- mContext->ValidateDependencies();
- if (mStats.mTypesDeleted != 0)
- {
- mContext->UpdateAfterDeletingTypes();
- mContext->ValidateDependencies();
- }
- mContext->RemoveInvalidWorkItems();
- mSystem->mNeedsTypesHandledByCompiler = false;
- //TODO:
- //Sleep(300);
- //mSystem->CheckLockYield();
- }
- BfTypeDef* BfCompiler::GetArrayTypeDef(int dimensions)
- {
- BF_ASSERT(dimensions <= 4);
- if (dimensions == 1)
- return mArray1TypeDef;
- if (dimensions == 2)
- return mArray2TypeDef;
- if (dimensions == 3)
- return mArray3TypeDef;
- return mArray4TypeDef;
- }
- void BfCompiler::VisitAutocompleteExteriorIdentifiers()
- {
- for (auto checkNode : mResolvePassData->mExteriorAutocompleteCheckNodes)
- {
- bool isUsingDirective = false;
- BfIdentifierNode* checkIdentifier = NULL;
- if (auto usingDirective = BfNodeDynCast<BfUsingDirective>(checkNode))
- {
- checkIdentifier = usingDirective->mNamespace;
- }
- else if (auto usingDirective = BfNodeDynCast<BfUsingModDirective>(checkNode))
- {
- if (usingDirective->mTypeRef != NULL)
- {
- BF_ASSERT(mContext->mScratchModule->mCurTypeInstance == NULL);
- SetAndRestoreValue<BfTypeInstance*> prevCurTypeInstance(mContext->mScratchModule->mCurTypeInstance, NULL);
- SetAndRestoreValue<bool> prevIgnoreErrors(mContext->mScratchModule->mIgnoreErrors, true);
- mContext->mScratchModule->ResolveTypeRef(usingDirective->mTypeRef, NULL);
- if (mResolvePassData->mAutoComplete != NULL)
- mResolvePassData->mAutoComplete->CheckTypeRef(usingDirective->mTypeRef, false, isUsingDirective);
- continue;
- }
- }
- else
- checkIdentifier = BfNodeDynCast<BfIdentifierNode>(checkNode);
- if (checkIdentifier == NULL)
- continue;
- if (mResolvePassData->mAutoComplete != NULL)
- mResolvePassData->mAutoComplete->CheckIdentifier(checkIdentifier, false, isUsingDirective);
- if (auto sourceClassifier = mResolvePassData->GetSourceClassifier(checkIdentifier))
- {
- if (isUsingDirective)
- {
- while (auto qualifiedNameNode = BfNodeDynCast<BfQualifiedNameNode>(checkIdentifier))
- {
- sourceClassifier->SetElementType(qualifiedNameNode->mRight, BfSourceElementType_Namespace);
- checkIdentifier = qualifiedNameNode->mLeft;
- }
- if (checkIdentifier != NULL)
- sourceClassifier->SetElementType(checkIdentifier, BfSourceElementType_Namespace);
- }
- }
- }
- mResolvePassData->mExteriorAutocompleteCheckNodes.Clear();
- }
- void BfCompiler::VisitSourceExteriorNodes()
- {
- BP_ZONE("BfCompiler::VisitSourceExteriorNodes");
- String str;
- Array<BfAtom*> namespaceParts;
- Array<BfAstNode*> srcNodes;
- std::function<bool(BfAstNode*, bool)> _AddName = [&](BfAstNode* node, bool wantErrors)
- {
- if (node == NULL)
- return false;
- if (auto qualifiedName = BfNodeDynCast<BfQualifiedNameNode>(node))
- {
- if (!_AddName(qualifiedName->mLeft, wantErrors))
- return false;
- if (!_AddName(qualifiedName->mRight, wantErrors))
- return false;
- return true;
- }
- else if (auto qualifedTypeRef = BfNodeDynCast<BfQualifiedTypeReference>(node))
- {
- if (!_AddName(qualifedTypeRef->mLeft, wantErrors))
- return false;
- if (!_AddName(qualifedTypeRef->mRight, wantErrors))
- return false;
- return true;
- }
- else if ((node->IsA<BfIdentifierNode>()) || (node->IsA<BfNamedTypeReference>()))
- {
- srcNodes.Add(node);
- str.Clear();
- node->ToString(str);
- auto atom = mSystem->FindAtom(str);
- if (atom == NULL)
- {
- String prevNamespace;
- for (auto part : namespaceParts)
- {
- if (!prevNamespace.IsEmpty())
- prevNamespace += ".";
- prevNamespace += part->mString;
- }
- if (wantErrors)
- {
- if (prevNamespace.IsEmpty())
- mPassInstance->Fail(StrFormat("The namespace '%s' does not exist", str.c_str()), node);
- else
- mPassInstance->Fail(StrFormat("The namespace '%s' does not exist in the namespace '%s'", str.c_str(), prevNamespace.c_str()), node);
- }
- return false;
- }
- namespaceParts.Add(atom);
- return true;
- }
- return false;
- };
- auto _CheckNamespace = [&](BfParser* parser, bool wantErrors, bool& failed)
- {
- for (int i = 0; i < (int)namespaceParts.size(); i++)
- {
- BfAtomComposite checkNamespace;
- checkNamespace.mParts = &namespaceParts[0];
- checkNamespace.mSize = i + 1;
- if (!mSystem->ContainsNamespace(checkNamespace, parser->mProject))
- {
- failed = true;
- BfAtomComposite prevNamespace;
- prevNamespace.mParts = &namespaceParts[0];
- prevNamespace.mSize = i;
- if (wantErrors)
- {
- if (i == 0)
- mPassInstance->Fail(StrFormat("The namespace '%s' does not exist", namespaceParts[i]->mString.ToString().c_str()), srcNodes[i]);
- else
- 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]);
- }
- return false;
- }
- }
- return true;
- };
- auto _CheckParser = [&](BfParser* parser)
- {
- while (parser->mNextRevision != NULL)
- parser = parser->mNextRevision;
- if (parser->mAwaitingDelete)
- return;
- if (parser->mParserData == NULL)
- return;
- if (parser->mParserData->mExteriorNodesCheckIdx == mSystem->mTypeMapVersion)
- return;
- bool failed = false;
- for (auto& node : parser->mParserData->mExteriorNodes)
- {
- SetAndRestoreValue<BfSizedArray<BfNamespaceDeclaration*>*> prevCurNamespaceNodes(mContext->mCurNamespaceNodes, &node.mNamespaceNodes);
- auto exteriorAstNode = node.mNode;
- if (auto usingDirective = BfNodeDynCast<BfUsingDirective>(exteriorAstNode))
- {
- srcNodes.Clear();
- namespaceParts.Clear();
- bool success = _AddName(usingDirective->mNamespace, true);
- _CheckNamespace(parser, true, failed);
- }
- else if (auto usingDirective = BfNodeDynCast<BfUsingModDirective>(exteriorAstNode))
- {
- if (usingDirective->mTypeRef != NULL)
- {
- BF_ASSERT(mContext->mScratchModule->mCurTypeInstance == NULL);
- bool wasNamespace = false;
- if (usingDirective->mModToken->mToken == BfToken_Internal)
- {
- srcNodes.Clear();
- namespaceParts.Clear();
- if (_AddName(usingDirective->mTypeRef, false))
- {
- wasNamespace = _CheckNamespace(parser, false, failed);
- }
- }
- if (!wasNamespace)
- {
- SetAndRestoreValue<BfTypeInstance*> prevCurTypeInstance(mContext->mScratchModule->mCurTypeInstance, NULL);
- if (auto genericTypeRef = BfNodeDynCast<BfGenericInstanceTypeRef>(usingDirective->mTypeRef))
- {
- mContext->mScratchModule->ResolveTypeRefAllowUnboundGenerics(usingDirective->mTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_NoReify);
- }
- else
- mContext->mScratchModule->ResolveTypeRef(usingDirective->mTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_NoReify);
- if ((mResolvePassData != NULL) && (mResolvePassData->mAutoComplete != NULL))
- mResolvePassData->mAutoComplete->CheckTypeRef(usingDirective->mTypeRef, false, false);
- }
- }
- }
- }
- if (!failed)
- parser->mParserData->mExteriorNodesCheckIdx = mSystem->mTypeMapVersion;
- };
- if (mResolvePassData != NULL)
- {
- for (auto parser : mResolvePassData->mParsers)
- _CheckParser(parser);
- }
- else
- {
- for (auto parser : mSystem->mParsers)
- {
- _CheckParser(parser);
- }
- }
- }
- void BfCompiler::ProcessAutocompleteTempType()
- {
- BP_ZONE_F("BfCompiler::ProcessAutocompleteTempType %d", mResolvePassData->mResolveType);
- String& autoCompleteResultString = *gTLStrReturn.Get();
- autoCompleteResultString.clear();
- if (mContext->mBfObjectType == NULL)
- return; // Not initialized yet
- auto module = mContext->mScratchModule;
- auto autoComplete = mResolvePassData->mAutoComplete;
- BfLogSysM("ProcessAutocompleteTempType %d\n", autoComplete->mResolveType);
- SetAndRestoreValue<bool> prevCanceling(mCanceling, false);
- BF_ASSERT(mResolvePassData->mAutoComplete->mDefMethod == NULL);
- if (autoComplete->mResolveType == BfResolveType_GetNavigationData)
- {
- for (auto parser : mResolvePassData->mParsers)
- {
- for (auto node : parser->mSidechannelRootNode->mChildArr)
- {
- if (auto preprocNode = BfNodeDynCast<BfPreprocessorNode>(node))
- {
- if (preprocNode->mCommand->Equals("region"))
- {
- if (!autoCompleteResultString.empty())
- autoCompleteResultString += "\n";
- autoCompleteResultString += "#";
- preprocNode->mArgument->ToString(autoCompleteResultString);
- mContext->mScratchModule->UpdateSrcPos(preprocNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
- autoCompleteResultString += StrFormat("\tregion\t%d\t%d", module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
- }
- }
- }
- }
- for (auto tempTypeDef : mResolvePassData->mAutoCompleteTempTypes)
- {
- String typeName = tempTypeDef->ToString();
- BfLogSysM("BfResolveType_GetNavigationData TypeDef:%p %s\n", tempTypeDef, typeName.c_str());
- auto refNode = tempTypeDef->GetRefNode();
- if ((refNode != NULL) && (!tempTypeDef->IsGlobalsContainer()))
- {
- if (!autoCompleteResultString.empty())
- autoCompleteResultString += "\n";
- String typeName = BfTypeUtils::TypeToString(tempTypeDef, BfTypeNameFlag_OmitNamespace);
- module->UpdateSrcPos(refNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
- autoCompleteResultString += typeName;
- if (tempTypeDef->mTypeCode == BfTypeCode_Object)
- autoCompleteResultString += "\tclass";
- else if (tempTypeDef->mTypeCode == BfTypeCode_Enum)
- autoCompleteResultString += "\tenum";
- else if (tempTypeDef->mTypeCode == BfTypeCode_Struct)
- autoCompleteResultString += "\tstruct";
- else if (tempTypeDef->mTypeCode == BfTypeCode_TypeAlias)
- autoCompleteResultString += "\ttypealias";
- else
- autoCompleteResultString += "\t";
- autoCompleteResultString += StrFormat("\t%d\t%d", module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
- }
- String methodText;
- for (auto methodDef : tempTypeDef->mMethods)
- {
- if (((methodDef->mMethodType == BfMethodType_Normal) || (methodDef->mMethodType == BfMethodType_Operator) ||
- (methodDef->mMethodType == BfMethodType_Ctor) || (methodDef->mMethodType == BfMethodType_Dtor) ||
- (methodDef->mMethodType == BfMethodType_Mixin) || (methodDef->mMethodType == BfMethodType_Extension)) &&
- (methodDef->mMethodDeclaration != NULL))
- {
- methodText = methodDef->ToString();
- if (typeName != "@")
- methodText = typeName + "." + methodText;
- if (!autoCompleteResultString.empty())
- autoCompleteResultString += "\n";
- auto methodDeclaration = methodDef->GetMethodDeclaration();
- BfAstNode* refNode = methodDeclaration;
- if (methodDeclaration->mBody != NULL)
- refNode = methodDeclaration->mBody;
- else if (methodDeclaration->mNameNode != NULL)
- refNode = methodDeclaration->mNameNode;
- module->UpdateSrcPos(refNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
- const char* typeStr = (methodDef->mMethodType == BfMethodType_Extension) ? "extmethod" : "method";
- methodText += StrFormat("\t%s\t%d\t%d", typeStr, module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
- autoCompleteResultString += methodText;
- }
- }
- for (auto fieldDef : tempTypeDef->mFields)
- {
- auto fieldDeclaration = BfNodeDynCast<BfFieldDeclaration>(fieldDef->mFieldDeclaration);
- if ((fieldDeclaration == NULL) || (fieldDeclaration->mNameNode == NULL) || (BfNodeIsA<BfPropertyDeclaration>(fieldDef->mFieldDeclaration)))
- continue;
- String fieldText = fieldDef->mName;
- if (typeName != "@")
- fieldText = typeName + "." + fieldText;
- if (!autoCompleteResultString.empty())
- autoCompleteResultString += "\n";
- BfAstNode* refNode = fieldDeclaration->mNameNode;
- module->UpdateSrcPos(refNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
- fieldText += StrFormat("\tfield\t%d\t%d", module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
- autoCompleteResultString += fieldText;
- }
- for (auto propDef : tempTypeDef->mProperties)
- {
- auto propDeclaration = BfNodeDynCast<BfPropertyDeclaration>(propDef->mFieldDeclaration);
- if (propDeclaration == NULL)
- continue;
- String propText;
- BfAstNode* refNode = propDeclaration->mNameNode;
- auto indexerDeclaration = BfNodeDynCast<BfIndexerDeclaration>(propDef->mFieldDeclaration);
- if (indexerDeclaration != NULL)
- {
- refNode = indexerDeclaration->mThisToken;
- propText = "this[";
- for (int i = 0; i < indexerDeclaration->mParams.mSize; i++)
- {
- if (i > 0)
- propText += ", ";
- propText += indexerDeclaration->mParams[i]->ToString();
- }
- propText += "]";
- }
- else
- {
- if (propDeclaration->mNameNode == NULL)
- continue;
- propText = propDef->mName;
- }
- if (typeName != "@")
- propText = typeName + "." + propText;
- if (!autoCompleteResultString.empty())
- autoCompleteResultString += "\n";
- module->UpdateSrcPos(refNode, (BfSrcPosFlags)(BfSrcPosFlag_NoSetDebugLoc | BfSrcPosFlag_Force));
- propText += StrFormat("\tproperty\t%d\t%d", module->mCurFilePosition.mCurLine, module->mCurFilePosition.mCurColumn);
- autoCompleteResultString += propText;
- }
- }
- module->CleanupFileInstances();
- return;
- }
- if (autoComplete->mResolveType == BfResolveType_GetCurrentLocation)
- {
- for (auto tempTypeDef : mResolvePassData->mAutoCompleteTempTypes)
- {
- String typeName = tempTypeDef->mNamespace.ToString();
- if (!typeName.empty())
- typeName += ".";
- typeName += tempTypeDef->ToString();
- autoCompleteResultString = typeName;
- for (auto methodDef : tempTypeDef->mMethods)
- {
- BfAstNode* defNode = methodDef->mMethodDeclaration;
- if (auto propertyDeclaration = methodDef->GetPropertyDeclaration())
- defNode = propertyDeclaration;
- if (defNode == NULL)
- continue;
- auto parser = defNode->GetParser();
- if ((parser == NULL) || (parser->mCursorIdx == -1))
- continue;
- if ((defNode != NULL) &&
- (defNode->Contains(parser->mCursorIdx)))
- {
- String methodText = methodDef->ToString();
- if (typeName != "@")
- methodText = typeName + "." + methodText;
- autoCompleteResultString = methodText;
- break;
- }
- }
- }
- if (mResolvePassData->mAutoCompleteTempTypes.IsEmpty())
- {
- for (auto& kv : mResolvePassData->mEmitEmbedEntries)
- {
- if (kv.mValue.mCursorIdx < 0)
- continue;
- String typeName = kv.mKey;
- auto type = GetType(typeName);
- if (type == NULL)
- continue;
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (mResolvePassData->mParsers.IsEmpty())
- break;
- autoCompleteResultString = mContext->mScratchModule->TypeToString(typeInst);
- for (auto methodDef : typeInst->mTypeDef->mMethods)
- {
- BfAstNode* defNode = methodDef->mMethodDeclaration;
- if (auto propertyDeclaration = methodDef->GetPropertyDeclaration())
- defNode = propertyDeclaration;
- if (defNode == NULL)
- continue;
- if ((defNode != NULL) &&
- (defNode->Contains(kv.mValue.mCursorIdx)))
- {
- auto defParser = defNode->GetParser();
- if (defParser == NULL)
- continue;
- if (!defParser->mIsEmitted)
- continue;
- autoCompleteResultString += ".";
- autoCompleteResultString += methodDef->ToString();
- break;
- }
- }
- }
- }
- module->CleanupFileInstances();
- return;
- }
- mFastFinish = false;
- SetAndRestoreValue<BfMethodState*> prevMethodState(module->mCurMethodState, NULL);
- SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(module->mCurTypeInstance, NULL);
- SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(module->mCurMethodInstance, NULL);
- SetAndRestoreValue<BfTypeState*> prevTypeState(module->mContext->mCurTypeState, NULL);
- // >>> VisitExteriorIdentifiers
- mResolvePassData->mAutoComplete->SetModule(module);
- {
- BP_ZONE("VisitExteriorIdentifiers");
- VisitAutocompleteExteriorIdentifiers();
- }
- VisitSourceExteriorNodes();
- if (autoComplete->mResolveType == BfResolveType_GetFixits)
- {
- BfAstNode* conflictStart = NULL;
- BfAstNode* conflictSplit = NULL;
- for (auto parser : mResolvePassData->mParsers)
- {
- auto src = parser->mSrc;
- for (int checkIdx = 0; checkIdx < (int)parser->mSidechannelRootNode->mChildArr.mSize; checkIdx++)
- {
- auto sideNode = parser->mSidechannelRootNode->mChildArr.mVals[checkIdx];
- if (autoComplete->CheckFixit(sideNode))
- {
- if (src[sideNode->mSrcStart] == '<')
- {
- conflictStart = sideNode;
- conflictSplit = NULL;
- }
- }
- else
- {
- if (src[sideNode->mSrcStart] == '<')
- {
- conflictStart = NULL;
- conflictSplit = NULL;
- }
- else if (src[sideNode->mSrcStart] == '=')
- {
- if (conflictStart != NULL)
- conflictSplit = sideNode;
- }
- else if (src[sideNode->mSrcStart] == '>')
- {
- if (conflictSplit != NULL)
- {
- autoComplete->AddEntry(AutoCompleteEntry("fixit", StrFormat("Accept First\tdelete|%s-%d|\x01""delete|%s-%d|",
- autoComplete->FixitGetLocation(parser->mParserData, conflictSplit->mSrcStart).c_str(), sideNode->mSrcEnd - conflictSplit->mSrcStart + 1,
- autoComplete->FixitGetLocation(parser->mParserData, conflictStart->mSrcStart).c_str(), conflictStart->mSrcEnd - conflictStart->mSrcStart + 1).c_str()));
- autoComplete->AddEntry(AutoCompleteEntry("fixit", StrFormat("Accept Second\tdelete|%s-%d|\x01""delete|%s-%d|",
- autoComplete->FixitGetLocation(parser->mParserData, sideNode->mSrcStart).c_str(), sideNode->mSrcEnd - sideNode->mSrcStart + 1,
- autoComplete->FixitGetLocation(parser->mParserData, conflictStart->mSrcStart).c_str(), conflictSplit->mSrcEnd - conflictStart->mSrcStart + 1).c_str()));
- autoComplete->AddEntry(AutoCompleteEntry("fixit", StrFormat("Accept Both\tdelete|%s-%d|\x01""delete|%s-%d|\x01""delete|%s-%d|",
- autoComplete->FixitGetLocation(parser->mParserData, sideNode->mSrcStart).c_str(), sideNode->mSrcEnd - sideNode->mSrcStart + 1,
- autoComplete->FixitGetLocation(parser->mParserData, conflictSplit->mSrcStart).c_str(), conflictSplit->mSrcEnd - conflictSplit->mSrcStart + 1,
- autoComplete->FixitGetLocation(parser->mParserData, conflictStart->mSrcStart).c_str(), conflictStart->mSrcEnd - conflictStart->mSrcStart + 1).c_str()));
- conflictStart = NULL;
- conflictSplit = NULL;
- }
- }
- }
- }
- }
- }
- if (autoComplete->mResolveType == BfResolveType_GetSymbolInfo)
- {
- BfNamespaceVisitor namespaceVisitor;
- namespaceVisitor.mResolvePassData = mResolvePassData;
- namespaceVisitor.mSystem = mSystem;
- for (auto parser : mResolvePassData->mParsers)
- namespaceVisitor.Visit(parser->mRootNode);
- }
- auto _FindAcutalTypeDef = [&](BfTypeDef* tempTypeDef)
- {
- auto typeName = tempTypeDef->mFullName;
- int wantNumGenericParams = (int)tempTypeDef->mGenericParamDefs.size();
- auto actualTypeDefItr = mSystem->mTypeDefs.TryGet(typeName);
- while (actualTypeDefItr)
- {
- auto checkTypeDef = *actualTypeDefItr;
- if ((!checkTypeDef->mIsPartial) && (checkTypeDef->mName == tempTypeDef->mName) &&
- (checkTypeDef->mIsFunction == tempTypeDef->mIsFunction) && (checkTypeDef->mIsDelegate == tempTypeDef->mIsDelegate) &&
- ((checkTypeDef->mTypeCode == tempTypeDef->mTypeCode) || (tempTypeDef->mTypeCode == BfTypeCode_Extension) || (tempTypeDef->mTypeCode == BfTypeCode_Inferred)))
- {
- if ((checkTypeDef->NameEquals(tempTypeDef)) && (checkTypeDef->mIsCombinedPartial) &&
- (checkTypeDef->mGenericParamDefs.size() == tempTypeDef->mGenericParamDefs.size()) &&
- (tempTypeDef->mProject->ContainsReference(checkTypeDef->mProject)))
- {
- return mSystem->FilterDeletedTypeDef(checkTypeDef);
- }
- if ((checkTypeDef->mGenericParamDefs.size() == wantNumGenericParams) &&
- (FileNameEquals(tempTypeDef->mSource->mSourceData->ToParserData()->mFileName, checkTypeDef->mSource->mSourceData->ToParserData()->mFileName)) &&
- (tempTypeDef->mProject == checkTypeDef->mProject))
- {
- return mSystem->FilterDeletedTypeDef(checkTypeDef);
- }
- }
- actualTypeDefItr.MoveToNextHashMatch();
- }
- return (BfTypeDef*)NULL;
- };
- BfTypeDef* tempTypeDef = NULL;
- BfTypeDef* actualTypeDef = NULL;
- for (auto checkTempType : mResolvePassData->mAutoCompleteTempTypes)
- {
- if (mResolvePassData->mAutoComplete->IsAutocompleteNode(checkTempType->mTypeDeclaration))
- {
- BfTypeDef* checkActualTypeDef = _FindAcutalTypeDef(checkTempType);
- if ((actualTypeDef == NULL) || (checkActualTypeDef != NULL))
- {
- actualTypeDef = checkActualTypeDef;
- tempTypeDef = checkTempType;
- }
- mContext->HandleChangedTypeDef(checkTempType, true);
- }
- auto sourceClassifier = mResolvePassData->GetSourceClassifier(checkTempType->mTypeDeclaration->mNameNode);
- if (sourceClassifier == NULL)
- continue;
- BfSourceElementType elemType = BfSourceElementType_Type;
- if (checkTempType->mTypeCode == BfTypeCode_Interface)
- elemType = BfSourceElementType_Interface;
- else if (checkTempType->mTypeCode == BfTypeCode_Object)
- elemType = BfSourceElementType_RefType;
- else if (checkTempType->mTypeCode == BfTypeCode_Struct)
- elemType = BfSourceElementType_Struct;
- sourceClassifier->SetElementType(checkTempType->mTypeDeclaration->mNameNode, elemType);
- }
- if (tempTypeDef == NULL)
- {
- if ((autoComplete != NULL) && (autoComplete->mResolveType == BfResolveType_GoToDefinition))
- {
- autoComplete->SetModule(NULL);
- for (auto& kv : mResolvePassData->mEmitEmbedEntries)
- {
- String typeName = kv.mKey;
- auto type = GetType(typeName);
- if (type == NULL)
- continue;
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- mContext->RebuildType(typeInst);
- if (!typeInst->mModule->mIsModuleMutable)
- typeInst->mModule->StartNewRevision(BfModule::RebuildKind_All, true);
- mContext->mScratchModule->PopulateType(typeInst, Beefy::BfPopulateType_Full_Force);
- }
- DoWorkLoop();
- autoComplete->SetModule(mContext->mScratchModule);
- }
- GenerateAutocompleteInfo();
- BfLogSysM("ProcessAutocompleteTempType - no tempTypeDef\n");
- return;
- }
- if (tempTypeDef->mProject->mDisabled)
- {
- BfLogSysM("ProcessAutocompleteTempType - project disabled\n");
- return;
- }
- BfTypeState typeState;
- typeState.mCurTypeDef = tempTypeDef;
- module->mContext->mCurTypeState = &typeState;
- BfStaticSearch* staticSearch = NULL;
- if (mResolvePassData->mStaticSearchMap.TryAdd(tempTypeDef, NULL, &staticSearch))
- {
- for (auto typeRef : tempTypeDef->mStaticSearch)
- {
- auto type = module->ResolveTypeRef(typeRef, NULL, BfPopulateType_Declaration);
- if (type != NULL)
- {
- auto typeInst = type->ToTypeInstance();
- if (typeInst != NULL)
- staticSearch->mStaticTypes.Add(typeInst);
- }
- }
- }
- BfInternalAccessSet* internalAccessSet = NULL;
- if (mResolvePassData->mInternalAccessMap.TryAdd(tempTypeDef, NULL, &internalAccessSet))
- {
- for (auto typeRef : tempTypeDef->mInternalAccessSet)
- {
- if ((typeRef->IsA<BfNamedTypeReference>()) ||
- (typeRef->IsA<BfQualifiedTypeReference>()))
- {
- String checkNamespaceStr;
- typeRef->ToString(checkNamespaceStr);
- BfAtomComposite checkNamespace;
- if (mSystem->ParseAtomComposite(checkNamespaceStr, checkNamespace))
- {
- if (mSystem->ContainsNamespace(checkNamespace, tempTypeDef->mProject))
- {
- internalAccessSet->mNamespaces.Add(checkNamespace);
- continue;
- }
- }
- }
- BfType* type;
- if (auto genericTypeRef = BfNodeDynCast<BfGenericInstanceTypeRef>(typeRef))
- type = mContext->mScratchModule->ResolveTypeRefAllowUnboundGenerics(typeRef, BfPopulateType_Identity);
- else
- type = module->ResolveTypeRef(typeRef, NULL, BfPopulateType_Identity);
- if (type != NULL)
- {
- auto typeInst = type->ToTypeInstance();
- if (typeInst != NULL)
- internalAccessSet->mTypes.Add(typeInst);
- }
- }
- }
- if (tempTypeDef->mTypeCode == BfTypeCode_Extension)
- {
- BfTypeInstance* outerTypeInstance = NULL;
- if (tempTypeDef->mOuterType != NULL)
- {
- auto outerTypeDef = _FindAcutalTypeDef(tempTypeDef->mOuterType);
- if (outerTypeDef != NULL)
- {
- outerTypeInstance = (BfTypeInstance*)module->ResolveTypeDef(outerTypeDef, BfPopulateType_IdentityNoRemapAlias);
- if ((outerTypeInstance != NULL) && (outerTypeInstance->IsIncomplete()))
- module->PopulateType(outerTypeInstance, BfPopulateType_Full);
- }
- }
- SetAndRestoreValue<BfTypeInstance*> prevCurTypeInstance(module->mCurTypeInstance, outerTypeInstance);
- auto autoComplete = mResolvePassData->mAutoComplete;
- if (autoComplete->IsAutocompleteNode(tempTypeDef->mTypeDeclaration->mNameNode))
- {
- BfIdentifierNode* nameNode = tempTypeDef->mTypeDeclaration->mNameNode;
- autoComplete->AddTopLevelNamespaces(nameNode);
- autoComplete->AddTopLevelTypes(nameNode);
- autoComplete->mInsertStartIdx = nameNode->GetSrcStart();
- autoComplete->mInsertEndIdx = nameNode->GetSrcEnd();
- }
- }
- // while (actualTypeDef == NULL)
- // {
- // checkT
- // }
- if ((actualTypeDef == NULL) || (actualTypeDef->mTypeDeclaration == NULL))
- {
- GenerateAutocompleteInfo();
- return;
- }
- auto sourceClassifier = mResolvePassData->GetSourceClassifier(tempTypeDef->mTypeDeclaration);
- if (tempTypeDef->mTypeCode == BfTypeCode_Extension)
- sourceClassifier->SetElementType(tempTypeDef->mTypeDeclaration->mNameNode, actualTypeDef->mTypeCode);
- if (tempTypeDef->mTypeDeclaration->mAttributes != NULL)
- sourceClassifier->VisitChild(tempTypeDef->mTypeDeclaration->mAttributes);
- BfTypeInstance* typeInst;
- {
- BP_ZONE("ProcessAutocompleteTempType.ResolveTypeDef");
- typeInst = (BfTypeInstance*)module->ResolveTypeDef(actualTypeDef, BfPopulateType_IdentityNoRemapAlias);
- if ((typeInst != NULL) && (typeInst->IsIncomplete()))
- module->PopulateType(typeInst, BfPopulateType_Full);
- }
- if (typeInst == NULL)
- {
- return;
- }
- BF_ASSERT((typeInst->mSize != -1) || (typeInst->IsTypeAlias()));
- #ifdef _DEBUG
- if ((typeInst->mModule != NULL) && (!typeInst->mModule->mIsScratchModule))
- mLastAutocompleteModule = typeInst->mModule;
- #endif
- SetAndRestoreValue<BfTypeInstance*> prevType(module->mCurTypeInstance, typeInst);
- typeState.mType = typeInst;
- BfGenericExtensionEntry* genericExEntry = NULL;
- bool hadTempExtensionInfo = false;
- if ((tempTypeDef->IsExtension()) && (actualTypeDef->mIsCombinedPartial) && (typeInst->IsGenericTypeInstance()))
- {
- // Add to our extension info map and then take it out at the end...
- auto genericTypeInst = (BfTypeInstance*)typeInst;
- module->BuildGenericExtensionInfo(genericTypeInst, tempTypeDef);
- genericTypeInst->mGenericTypeInfo->mGenericExtensionInfo->mExtensionMap.TryGetValue(tempTypeDef, &genericExEntry);
- BF_ASSERT(genericExEntry != NULL);
- hadTempExtensionInfo = true;
- }
- if ((typeInst->IsUnspecializedType()) || (!typeInst->IsGenericTypeInstance()))
- {
- auto autoComplete = mResolvePassData->mAutoComplete;
- BfAstNode* nameNode = tempTypeDef->mTypeDeclaration->mNameNode;
- BfAstNode* refNode = nameNode;
- if (refNode == NULL)
- refNode = tempTypeDef->mTypeDeclaration->mStaticSpecifier;
- if (autoComplete->IsAutocompleteNode(refNode))
- {
- if ((actualTypeDef->mIsCombinedPartial) && (tempTypeDef->mTypeCode == BfTypeCode_Extension))
- {
- autoComplete->AddTopLevelNamespaces(tempTypeDef->mTypeDeclaration->mNameNode);
- autoComplete->AddTopLevelTypes(tempTypeDef->mTypeDeclaration->mNameNode);
- autoComplete->SetDefinitionLocation(actualTypeDef->mTypeDeclaration->mNameNode);
- }
- else
- autoComplete->SetDefinitionLocation(refNode);
- autoComplete->mDefType = actualTypeDef;
- if (nameNode != NULL)
- {
- autoComplete->mInsertStartIdx = nameNode->GetSrcStart();
- autoComplete->mInsertEndIdx = nameNode->GetSrcEnd();
- }
- if (autoComplete->mResolveType == BfResolveType_GetResultString)
- {
- autoComplete->mResultString = ":";
- autoComplete->mResultString += module->TypeToString(typeInst, (BfTypeNameFlags)(BfTypeNameFlag_ExtendedInfo | BfTypeNameFlag_ResolveGenericParamNames));
- }
- }
- }
- autoComplete->CheckInterfaceFixit(typeInst, tempTypeDef->mTypeDeclaration->mNameNode);
- if (tempTypeDef->mTypeCode == BfTypeCode_TypeAlias)
- {
- auto typeAliasDecl = (BfTypeAliasDeclaration*)tempTypeDef->mTypeDeclaration;
- if (typeAliasDecl->mAliasToType != NULL)
- {
- autoComplete->CheckTypeRef(typeAliasDecl->mAliasToType, false);
- module->ResolveTypeRef(typeAliasDecl->mAliasToType);
- }
- }
- // Save and restore mFieldResolveReentrys, we could fire off autocomplete while resolving a field
- SetAndRestoreValue<decltype (module->mContext->mFieldResolveReentrys)> prevTypeResolveReentry(module->mContext->mFieldResolveReentrys);
- module->mContext->mFieldResolveReentrys.Clear();
- if (tempTypeDef->mTypeDeclaration->mAttributes != NULL)
- {
- auto customAttrs = module->GetCustomAttributes(tempTypeDef);
- delete customAttrs;
- }
- for (int genericParamIdx = 0; genericParamIdx < (int)tempTypeDef->mGenericParamDefs.size(); genericParamIdx++)
- {
- auto genericParamDef = tempTypeDef->mGenericParamDefs[genericParamIdx];
- auto genericParamInstance = new BfGenericTypeParamInstance(tempTypeDef, genericParamIdx);
- genericParamInstance->mExternType = module->GetGenericParamType(BfGenericParamKind_Type, genericParamIdx);
- module->ResolveGenericParamConstraints(genericParamInstance, true);
- delete genericParamInstance;
- for (auto nameNode : genericParamDef->mNameNodes)
- module->HandleTypeGenericParamRef(nameNode, tempTypeDef, genericParamIdx);
- }
- //TODO: Do extern constraint stuff here
- for (auto fieldDef : tempTypeDef->mFields)
- {
- BP_ZONE("ProcessAutocompleteTempType.CheckField");
- if (BfNodeIsA<BfPropertyDeclaration>(fieldDef->mFieldDeclaration))
- continue; // Don't process auto-generated property fields
- if (fieldDef->mTypeRef != NULL)
- {
- BfResolveTypeRefFlags flags = BfResolveTypeRefFlag_None;
- if (fieldDef->GetInitializer() != NULL)
- flags = (BfResolveTypeRefFlags)(flags | BfResolveTypeRefFlag_AllowInferredSizedArray);
- if ((!BfNodeIsA<BfVarTypeReference>(fieldDef->mTypeRef)) &&
- (!BfNodeIsA<BfLetTypeReference>(fieldDef->mTypeRef)))
- module->ResolveTypeRef(fieldDef->mTypeRef, BfPopulateType_Identity, flags);
- }
- mResolvePassData->mAutoComplete->CheckTypeRef(fieldDef->mTypeRef, true);
- actualTypeDef->PopulateMemberSets();
- BfFieldDef* actualFieldDef = NULL;
- BfMemberSetEntry* memberSetEntry = NULL;
- if (actualTypeDef->mFieldSet.TryGetWith((StringImpl&)fieldDef->mName, &memberSetEntry))
- {
- auto checkFieldDef = (BfFieldDef*)memberSetEntry->mMemberDef;
- if ((checkFieldDef->mIsConst == fieldDef->mIsConst) &&
- (checkFieldDef->mIsStatic == fieldDef->mIsStatic))
- {
- actualFieldDef = checkFieldDef;
- }
- }
- if (actualFieldDef != NULL)
- {
- auto fieldInstance = &typeInst->mFieldInstances[actualFieldDef->mIdx];
- autoComplete->CheckVarResolution(fieldDef->mTypeRef, fieldInstance->mResolvedType);
- }
- auto nameNode = fieldDef->GetNameNode();
- if (((autoComplete->mIsGetDefinition) || (autoComplete->mResolveType == BfResolveType_GetResultString)) &&
- (fieldDef->mFieldDeclaration != NULL) && (autoComplete->IsAutocompleteNode(nameNode)))
- {
- for (int i = 0; i < (int)actualTypeDef->mFields.size(); i++)
- {
- auto actualFieldDef = actualTypeDef->mFields[i];
- if (actualFieldDef->mName == fieldDef->mName)
- {
- if (autoComplete->mIsGetDefinition)
- {
- autoComplete->mDefType = actualTypeDef;
- autoComplete->mDefField = actualFieldDef;
- autoComplete->SetDefinitionLocation(nameNode);
- autoComplete->mInsertStartIdx = nameNode->GetSrcStart();
- autoComplete->mInsertEndIdx = nameNode->GetSrcEnd();
- }
- else if (autoComplete->mResolveType == BfResolveType_GetResultString)
- {
- auto fieldInstance = &typeInst->mFieldInstances[actualFieldDef->mIdx];
- if (fieldInstance->mConstIdx != -1)
- {
- auto constant = typeInst->mConstHolder->GetConstantById(fieldInstance->mConstIdx);
- auto retVal = module->ConstantToCurrent(constant, typeInst->mConstHolder, fieldInstance->mResolvedType);
- BfTypedValue typedValue = BfTypedValue(retVal, fieldInstance->mResolvedType);
- autoComplete->CheckResult(fieldDef->GetRefNode(), typedValue);
- }
- }
- break;
- }
- }
- }
- BfFieldDeclaration* fieldDecl = fieldDef->GetFieldDeclaration();
- if ((fieldDecl != NULL) && (fieldDecl->mAttributes != NULL))
- {
- auto customAttrs = module->GetCustomAttributes(fieldDecl->mAttributes, fieldDef->mIsStatic ? BfAttributeTargets_StaticField : BfAttributeTargets_Field);
- delete customAttrs;
- }
- if (fieldDef->mIsConst)
- {
- module->ResolveConstField(typeInst, NULL, fieldDef);
- }
- if (fieldDef->GetInitializer() == NULL)
- {
- if (BfNodeIsA<BfVarTypeReference>(fieldDef->mTypeRef))
- {
- if ((fieldDef->mTypeRef->IsA<BfVarTypeReference>()) || (fieldDef->mTypeRef->IsA<BfLetTypeReference>()))
- mPassInstance->Fail("Implicitly-typed fields must be initialized", fieldDef->GetRefNode());
- }
- }
- }
- auto checkTypeDef = tempTypeDef;
- while (checkTypeDef != NULL)
- {
- for (auto baseType : checkTypeDef->mBaseTypes)
- {
- autoComplete->CheckTypeRef(BfNodeDynCast<BfTypeReference>(baseType), false);
- module->ResolveTypeRef_Ref(baseType, BfPopulateType_Identity);
- }
- checkTypeDef = checkTypeDef->mOuterType;
- }
- for (auto propDef : tempTypeDef->mProperties)
- {
- auto fieldDecl = propDef->GetFieldDeclaration();
- if ((fieldDecl != NULL) && (fieldDecl->mAttributes != NULL))
- {
- BfAttributeTargets target = BfAttributeTargets_Property;
- if (propDef->IsExpressionBodied())
- target = (BfAttributeTargets)(target | BfAttributeTargets_Method);
- auto customAttrs = module->GetCustomAttributes(fieldDecl->mAttributes, target);
- delete customAttrs;
- }
- auto propDeclaration = BfNodeDynCast<BfPropertyDeclaration>(fieldDecl);
- if (propDeclaration != NULL)
- autoComplete->CheckProperty(propDeclaration);
- if (BfNodeIsA<BfVarTypeReference>(propDef->mTypeRef))
- {
- // This is only valid for ConstEval properties
- }
- else
- module->ResolveTypeRef(propDef->mTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_AllowRef);
- if (auto indexerDeclaration = BfNodeDynCast<BfIndexerDeclaration>(propDef->mFieldDeclaration))
- {
- for (auto paramDecl : indexerDeclaration->mParams)
- {
- module->ResolveTypeRef(paramDecl->mTypeRef, BfPopulateType_Identity);
- }
- }
- if ((autoComplete->mIsGetDefinition) && (fieldDecl != NULL) && (autoComplete->IsAutocompleteNode(fieldDecl->mNameNode)))
- {
- auto checkType = typeInst;
- while (checkType != NULL)
- {
- for (auto checkProp : checkType->mTypeDef->mProperties)
- {
- if (checkProp->mName == propDef->mName)
- {
- auto checkPropDeclaration = BfNodeDynCast<BfPropertyDeclaration>(checkProp->mFieldDeclaration);
- if ((checkPropDeclaration->mVirtualSpecifier == NULL) || (checkPropDeclaration->mVirtualSpecifier->GetToken() == BfToken_Virtual))
- {
- autoComplete->SetDefinitionLocation(checkPropDeclaration->mNameNode);
- autoComplete->mDefType = checkType->mTypeDef;
- autoComplete->mDefProp = checkProp;
- checkType = NULL;
- break;
- }
- }
- }
- if (checkType != NULL)
- checkType = checkType->mBaseType;
- }
- }
- }
- Array<BfMethodInstance*> methodInstances;
- if (tempTypeDef->IsExtension())
- {
- BfDefBuilder::AddMethod(tempTypeDef, BfMethodType_CtorNoBody, BfProtection_Public, true, "", false);
- BfDefBuilder::AddMethod(tempTypeDef, BfMethodType_CtorNoBody, BfProtection_Public, false, "", false);
- }
- for (auto methodDef : tempTypeDef->mMethods)
- {
- auto methodDeclaration = methodDef->GetMethodDeclaration();
- if (methodDeclaration != NULL)
- autoComplete->CheckMethod(methodDeclaration, false);
- if (!methodDef->mWantsBody)
- {
- if (methodDeclaration != NULL)
- {
- if (methodDeclaration->mAttributes != NULL)
- {
- auto customAttrs = module->GetCustomAttributes(methodDeclaration->mAttributes, (methodDef->mMethodType == BfMethodType_Ctor) ? BfAttributeTargets_Constructor : BfAttributeTargets_Method);
- delete customAttrs;
- }
- }
- else if (auto methodPropertyDeclaration = methodDef->GetPropertyMethodDeclaration())
- {
- if (methodPropertyDeclaration->mAttributes != NULL)
- {
- auto customAttrs = module->GetCustomAttributes(methodPropertyDeclaration->mAttributes, BfAttributeTargets_Method);
- delete customAttrs;
- }
- }
- continue;
- }
- BP_ZONE("ProcessAutocompleteTempType.CheckMethod");
- BfMethodInstanceGroup methodInstanceGroup;
- methodInstanceGroup.mOwner = typeInst;
- methodInstanceGroup.mOnDemandKind = BfMethodOnDemandKind_AlwaysInclude;
- BfMethodInstance* methodInstance = new BfMethodInstance();
- methodInstances.push_back(methodInstance);
- methodInstance->mMethodDef = methodDef;
- methodInstance->mMethodInstanceGroup = &methodInstanceGroup;
- methodInstance->mIsAutocompleteMethod = true;
- methodInstanceGroup.mDefault = methodInstance;
- defer(methodInstanceGroup.mDefault = NULL);
- for (int genericParamIdx = 0; genericParamIdx < (int)methodDef->mGenericParams.size(); genericParamIdx++)
- {
- auto genericParamType = module->GetGenericParamType(BfGenericParamKind_Method, genericParamIdx);
- methodInstance->GetMethodInfoEx()->mMethodGenericArguments.push_back(genericParamType);
- auto genericParamInstance = new BfGenericMethodParamInstance(methodDef, genericParamIdx);
- methodInstance->GetMethodInfoEx()->mGenericParams.push_back(genericParamInstance);
- }
- for (int externConstraintIdx = 0; externConstraintIdx < (int)methodDef->mExternalConstraints.size(); externConstraintIdx++)
- {
- auto genericParamInstance = new BfGenericMethodParamInstance(methodDef, externConstraintIdx + (int)methodDef->mGenericParams.size());
- methodInstance->GetMethodInfoEx()->mGenericParams.push_back(genericParamInstance);
- }
- bool wantsProcess = !actualTypeDef->mIsFunction;
- SetAndRestoreValue<BfFilePosition> prevFilePos(module->mCurFilePosition);
- SetAndRestoreValue<BfMethodInstance*> prevMethodInst(module->mCurMethodInstance, methodInstance);
- module->DoMethodDeclaration(methodDeclaration, true, wantsProcess);
- if (wantsProcess)
- {
- module->mIncompleteMethodCount++;
- module->ProcessMethod(methodInstance);
- }
- if (methodInstance->mIRFunction)
- {
- BfLogSysM("Autocomplete removing IRFunction %d\n", methodInstance->mIRFunction.mId);
- module->mBfIRBuilder->Func_DeleteBody(methodInstance->mIRFunction);
- module->mBfIRBuilder->Func_SafeRename(methodInstance->mIRFunction);
- }
- }
- if ((mResolvePassData->mAutoComplete->mDefType != NULL) && (mResolvePassData->mAutoComplete->mDefType->GetDefinition() == actualTypeDef) &&
- (mResolvePassData->mAutoComplete->mDefMethod != NULL))
- {
- BfMethodDef* tempDefMethod = NULL;
- for (auto checkMethod : tempTypeDef->mMethods)
- {
- if (checkMethod == mResolvePassData->mAutoComplete->mDefMethod)
- tempDefMethod = checkMethod;
- }
- if (tempDefMethod != NULL)
- {
- BfMethodDef* actualReplaceMethodDef = NULL;
- for (auto checkMethodDef : actualTypeDef->mMethods)
- {
- if ((checkMethodDef->mMethodType == tempDefMethod->mMethodType) &&
- (checkMethodDef->mMethodDeclaration != NULL) && (tempDefMethod->mMethodDeclaration != NULL) &&
- (checkMethodDef->mMethodDeclaration->GetSrcStart() == tempDefMethod->mMethodDeclaration->GetSrcStart()))
- actualReplaceMethodDef = checkMethodDef;
- }
- if (actualReplaceMethodDef == NULL)
- {
- autoComplete->mReplaceLocalId = -1;
- autoComplete->mDefType = NULL;
- autoComplete->mDefField = NULL;
- autoComplete->mDefMethod = NULL;
- autoComplete->mDefProp = NULL;
- autoComplete->mDefMethodGenericParamIdx = -1;
- autoComplete->mDefTypeGenericParamIdx = -1;
- }
- else
- autoComplete->mDefMethod = actualReplaceMethodDef;
- }
- }
- if (hadTempExtensionInfo)
- {
- auto genericTypeInst = (BfTypeInstance*)typeInst;
- genericTypeInst->mGenericTypeInfo->mGenericExtensionInfo->mExtensionMap.Remove(tempTypeDef);
- }
- for (auto checkNode : mResolvePassData->mExteriorAutocompleteCheckNodes)
- {
- BP_ZONE("ProcessAutocompleteTempType.CheckIdentifier");
- bool isUsingDirective = false;
- BfIdentifierNode* checkIdentifier = NULL;
- if (auto usingDirective = BfNodeDynCast<BfUsingDirective>(checkNode))
- {
- isUsingDirective = true;
- checkIdentifier = usingDirective->mNamespace;
- }
- else
- checkIdentifier = BfNodeDynCast<BfIdentifierNode>(checkNode);
- mResolvePassData->mAutoComplete->CheckIdentifier(checkIdentifier, false, isUsingDirective);
- }
- GenerateAutocompleteInfo();
- for (auto methodInstance : methodInstances)
- delete methodInstance;
- methodInstances.Clear();
- module->CleanupFileInstances();
- prevTypeInstance.Restore();
- if (module->mCurTypeInstance == NULL)
- module->ClearConstData();
- BfLogSysM("ProcessAutocompleteTempType end\n");
- }
- BfType* BfCompiler::CheckSymbolReferenceTypeRef(BfModule* module, BfTypeReference* typeRef)
- {
- if (typeRef == NULL)
- return NULL;
- //auto resolvedType = module->ResolveTypeRef(typeRef, BfPopulateType_Declaration,
- //(BfResolveTypeRefFlags)(BfResolveTypeRefFlag_AllowRef | BfResolveTypeRefFlag_AllowGenericMethodParamConstValue | BfResolveTypeRefFlag_AllowGenericTypeParamConstValue));
- auto resolvedType = module->ResolveTypeRef(typeRef, BfPopulateType_Declaration, BfResolveTypeRefFlag_AllowRef);
- if ((resolvedType != NULL) && (resolvedType->IsTypeInstance()))
- {
- auto typeInst = resolvedType->ToTypeInstance();
- //TODO: Did we need this?
- // The ResolveTypeRef call already does mResolvePassData->HandleTypeReference, so we were adding double entries
- //mResolvePassData->HandleTypeReference(typeRef, typeInst->mTypeDef);
- }
- return resolvedType;
- }
- void BfCompiler::AddToRebuildTypeList(BfTypeInstance* typeInst, HashSet<BfTypeInstance*>& rebuildTypeInstList)
- {
- if (!mResolvePassData->mParsers.IsEmpty())
- {
- bool found = false;
- for (auto parser : mResolvePassData->mParsers)
- {
- // Only find references within the current file
- if (typeInst->mTypeDef->GetDefinition()->HasSource(parser))
- found = true;
- }
- if (!found)
- return;
- }
- bool allowRebuild = ((!typeInst->IsGenericTypeInstance()) ||
- ((typeInst->IsUnspecializedType()) && (!typeInst->IsUnspecializedTypeVariation())));
- if ((typeInst->IsClosure()) || (typeInst->IsConcreteInterfaceType()) || (typeInst->IsModifiedTypeType()))
- allowRebuild = false;
- if (allowRebuild)
- rebuildTypeInstList.Add(typeInst);
- }
- void BfCompiler::AddDepsToRebuildTypeList(BfTypeInstance* replaceTypeInst, HashSet<BfTypeInstance*>& rebuildTypeInstList)
- {
- for (auto& dep : replaceTypeInst->mDependencyMap)
- {
- auto depType = dep.mKey;
- auto depTypeInst = depType->ToTypeInstance();
- if (depTypeInst == NULL)
- continue;
- AddToRebuildTypeList(depTypeInst, rebuildTypeInstList);
- }
- }
- void BfCompiler::GetSymbolReferences()
- {
- BfLogSysM("GetSymbolReferences\n");
- if (mInInvalidState)
- return; // Don't even try
- auto context = mContext;
- if (context->mBfObjectType == NULL)
- return; // Not initialized yet
- auto module = context->mScratchModule;
- if (mResolvePassData->mAutoComplete != NULL)
- mResolvePassData->mAutoComplete->SetModule(module);
- BfTypeDef* typeDef = NULL;
- HashSet<BfTypeInstance*> rebuildTypeInstList;
- if (!mResolvePassData->mQueuedSymbolReferenceNamespace.IsEmpty())
- {
- if (!mSystem->ParseAtomComposite(mResolvePassData->mQueuedSymbolReferenceNamespace, mResolvePassData->mSymbolReferenceNamespace))
- return;
- for (auto type : mContext->mResolvedTypes)
- {
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- for (auto& lookupKV : typeInst->mLookupResults)
- {
- auto typeDef = lookupKV.mValue.mTypeDef;
- if ((typeDef != NULL) && (typeDef->mNamespace.StartsWith(mResolvePassData->mSymbolReferenceNamespace)))
- {
- AddToRebuildTypeList(typeInst, rebuildTypeInstList);
- }
- }
- }
- for (auto parser : mSystem->mParsers)
- {
- BfNamespaceVisitor namespaceVisitor;
- namespaceVisitor.mResolvePassData = mResolvePassData;
- namespaceVisitor.mSystem = mSystem;
- namespaceVisitor.Visit(parser->mRootNode);
- }
- }
- else
- {
- const char* strPtr = mResolvePassData->mQueuedReplaceTypeDef.c_str();
- typeDef = mSystem->FindTypeDefEx(strPtr);
- if ((typeDef == NULL) || (typeDef->mTypeDeclaration == NULL))
- return;
- typeDef = typeDef->GetLatest();
- mResolvePassData->mSymbolReferenceTypeDef = typeDef;
- auto replaceType = module->ResolveTypeDef(typeDef, BfPopulateType_IdentityNoRemapAlias);
- module->PopulateType(replaceType);
- auto replaceTypeInst = replaceType->ToTypeInstance();
- if (mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Local)
- {
- AddDepsToRebuildTypeList(replaceTypeInst, rebuildTypeInstList);
- // For generic types, add all references from all specialized versions
- if (replaceTypeInst->IsGenericTypeInstance())
- {
- for (auto type : mContext->mResolvedTypes)
- {
- auto typeInst = type->ToTypeInstance();
- if ((typeInst != replaceTypeInst) && (typeInst != NULL) && (typeInst->mTypeDef->GetLatest() == typeDef))
- AddDepsToRebuildTypeList(typeInst, rebuildTypeInstList);
- }
- }
- }
- AddToRebuildTypeList(replaceTypeInst, rebuildTypeInstList);
- }
- //TODO: Did we need this to be rebuildTypeInst->mModule??? Why?
- //auto rebuildModule = rebuildTypeInst->mModule;
- auto rebuildModule = context->mScratchModule;
- auto _CheckAttributes = [&](BfAttributeDirective* attrib, BfTypeDef* declaringType)
- {
- if ((mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Type) &&
- (mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Field) &&
- (mResolvePassData->mGetSymbolReferenceKind != BfGetSymbolReferenceKind_Property))
- return;
- while (attrib != NULL)
- {
- if (attrib->mAttributeTypeRef != NULL)
- {
- auto attrType = module->ResolveTypeRef(attrib->mAttributeTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_Attribute);
- BfTypeDef* attrTypeDef = NULL;
- if ((attrType != NULL) && (attrType->IsTypeInstance()))
- attrTypeDef = attrType->ToTypeInstance()->mTypeDef;
- if (attrTypeDef != NULL)
- {
- mResolvePassData->HandleTypeReference(attrib->mAttributeTypeRef, attrTypeDef);
- attrTypeDef->PopulateMemberSets();
- for (auto argExpr : attrib->mArguments)
- {
- if (auto assignExpr = BfNodeDynCast<BfAssignmentExpression>(argExpr))
- {
- auto propName = assignExpr->mLeft->ToString();
- BfMemberSetEntry* propDefEntry;
- if (attrTypeDef->mPropertySet.TryGetWith(propName, &propDefEntry))
- {
- mResolvePassData->HandlePropertyReference(assignExpr->mLeft, attrTypeDef, (BfPropertyDef*)propDefEntry->mMemberDef);
- }
- else if (attrTypeDef->mFieldSet.TryGetWith(propName, &propDefEntry))
- {
- mResolvePassData->HandleFieldReference(assignExpr->mLeft, attrTypeDef, (BfFieldDef*)propDefEntry->mMemberDef);
- }
- }
- }
- }
- }
- attrib = attrib->mNextAttribute;
- }
- };
- for (auto rebuildTypeInst : rebuildTypeInstList)
- {
- // These never have a definition. Also note that BfGenericDelegateType does not have proper generic defs
- if (rebuildTypeInst->IsOnDemand())
- continue;
- auto context = mContext;
- auto module = context->mScratchModule;
- SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(module->mCurTypeInstance, rebuildTypeInst);
- SetAndRestoreValue<bool> prevIgnoreErrors(module->mIgnoreErrors, true);
- // Run through base types for type renames
- auto typeDef = rebuildTypeInst->mTypeDef;
- if ((typeDef->mTypeDeclaration != NULL) && (typeDef->mTypeDeclaration->mNameNode != NULL))
- {
- if (typeDef->mIsCombinedPartial)
- {
- for (auto checkTypeDef : typeDef->mPartials)
- {
- auto nameNode = checkTypeDef->mTypeDeclaration->mNameNode;
- for (auto parser : mResolvePassData->mParsers)
- {
- if (nameNode->IsFromParser(parser))
- {
- mResolvePassData->HandleTypeReference(nameNode, typeDef);
- break;
- }
- }
- if (mResolvePassData->mParsers.IsEmpty())
- mResolvePassData->HandleTypeReference(nameNode, typeDef);
- if (checkTypeDef->IsExtension())
- {
- if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Type)
- {
- BfTypeState typeState;
- typeState.mCurTypeDef = checkTypeDef;
- SetAndRestoreValue<BfTypeState*> prevTypeState(module->mContext->mCurTypeState, &typeState);
- for (auto baseTypeRef : checkTypeDef->mBaseTypes)
- CheckSymbolReferenceTypeRef(module, BfNodeDynCast<BfTypeReference>(baseTypeRef));
- for (auto genericParam : checkTypeDef->mGenericParamDefs)
- {
- for (auto constraint : genericParam->mConstraints)
- {
- if (auto constraintTypeRef = BfNodeDynCast<BfTypeReference>(constraint))
- {
- module->ResolveTypeRef(constraintTypeRef, BfPopulateType_Identity);
- }
- else if (auto opConstraint = BfNodeDynCast<BfGenericOperatorConstraint>(constraint))
- {
- module->ResolveTypeRef(opConstraint->mLeftType, BfPopulateType_Identity);
- module->ResolveTypeRef(opConstraint->mRightType, BfPopulateType_Identity);
- }
- }
- }
- }
- }
- }
- }
- else
- {
- mResolvePassData->HandleTypeReference(typeDef->mTypeDeclaration->mNameNode, typeDef);
- }
- }
- if (!typeDef->mPartials.IsEmpty())
- {
- for (auto partialDef : typeDef->mPartials)
- {
- if ((partialDef->mTypeDeclaration != NULL) && (partialDef->mTypeDeclaration->mAttributes != NULL))
- _CheckAttributes(partialDef->mTypeDeclaration->mAttributes, typeDef);
- }
- }
- else
- {
- if ((typeDef->mTypeDeclaration != NULL) && (typeDef->mTypeDeclaration->mAttributes != NULL))
- _CheckAttributes(typeDef->mTypeDeclaration->mAttributes, typeDef);
- }
- if (auto typeAliasDeclaration = BfNodeDynCast<BfTypeAliasDeclaration>(typeDef->mTypeDeclaration))
- {
- CheckSymbolReferenceTypeRef(module, typeAliasDeclaration->mAliasToType);
- }
- if (mResolvePassData != NULL)
- {
- if (rebuildTypeInst->IsGenericTypeInstance())
- {
- auto genericTypeInstance = (BfTypeInstance*)rebuildTypeInst;
- for (int genericParamIdx = 0; genericParamIdx < (int)genericTypeInstance->mGenericTypeInfo->mTypeGenericArguments.size(); genericParamIdx++)
- {
- BfGenericTypeParamInstance genericParamInstance(genericTypeInstance->mTypeDef, genericParamIdx);
- auto genericParamDef = typeDef->mGenericParamDefs[genericParamIdx];
- if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_TypeGenericParam)
- {
- for (auto nameNode : genericParamDef->mNameNodes)
- if (nameNode != NULL)
- mResolvePassData->HandleTypeGenericParam(nameNode, typeDef, genericParamIdx);
- }
- rebuildModule->ResolveGenericParamConstraints(&genericParamInstance, genericTypeInstance->IsGenericTypeInstance());
- }
- }
- }
- if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Type)
- {
- for (auto baseTypeRef : typeDef->mBaseTypes)
- CheckSymbolReferenceTypeRef(module, BfNodeDynCast<BfTypeReference>(baseTypeRef));
- }
- BfTypeState typeState;
- SetAndRestoreValue<BfTypeState*> prevTypeState(module->mContext->mCurTypeState, &typeState);
- if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Property)
- {
- for (auto propDef : typeDef->mProperties)
- {
- BfPropertyDef* checkPropDef = propDef;
- BfTypeInstance* checkTypeInst = rebuildTypeInst;
- typeState.mCurTypeDef = propDef->mDeclaringType;
- module->GetBasePropertyDef(checkPropDef, checkTypeInst);
- if (auto fieldDecl = propDef->GetFieldDeclaration())
- if (fieldDecl->mNameNode != NULL)
- mResolvePassData->HandlePropertyReference(fieldDecl->mNameNode, checkTypeInst->mTypeDef, checkPropDef);
- }
- }
- if (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Field)
- {
- for (auto fieldDef : typeDef->mFields)
- {
- if (auto nameNode = fieldDef->GetNameNode())
- {
- typeState.mCurTypeDef = fieldDef->mDeclaringType;
- mResolvePassData->HandleFieldReference(nameNode, typeDef, fieldDef);
- }
- }
- }
- for (auto& fieldInst : rebuildTypeInst->mFieldInstances)
- {
- auto fieldDef = fieldInst.GetFieldDef();
- if (fieldDef != NULL)
- {
- typeState.mCurTypeDef = fieldDef->mDeclaringType;
- if (fieldDef->mTypeRef != NULL)
- CheckSymbolReferenceTypeRef(module, fieldDef->mTypeRef);
- if ((fieldDef->mIsConst) && (fieldDef->GetInitializer() != NULL))
- {
- BfMethodState methodState;
- methodState.mTempKind = BfMethodState::TempKind_Static;
- SetAndRestoreValue<BfMethodState*> prevMethodState(module->mCurMethodState, &methodState);
- BfConstResolver constResolver(module);
- constResolver.Resolve(fieldDef->GetInitializer());
- }
- if (auto fieldDecl = fieldDef->GetFieldDeclaration())
- {
- if (fieldDecl->mAttributes != NULL)
- _CheckAttributes(fieldDecl->mAttributes, fieldDef->mDeclaringType);
- }
- }
- }
- for (auto& propDef : rebuildTypeInst->mTypeDef->mProperties)
- {
- typeState.mCurTypeDef = propDef->mDeclaringType;
- if (propDef->mTypeRef != NULL)
- CheckSymbolReferenceTypeRef(module, propDef->mTypeRef);
- }
- if (rebuildModule == NULL)
- continue;
- rebuildModule->EnsureIRBuilder();
- SetAndRestoreValue<BfTypeInstance*> prevTypeInstance2(rebuildModule->mCurTypeInstance, rebuildTypeInst);
- for (auto& methodInstGroup : rebuildTypeInst->mMethodInstanceGroups)
- {
- // Run through all methods
- bool isDefault = true;
- BfMethodInstanceGroup::MapType::iterator methodItr;
- if (methodInstGroup.mMethodSpecializationMap != NULL)
- methodItr = methodInstGroup.mMethodSpecializationMap->begin();
- while (true)
- {
- BfMethodInstance* rebuildMethodInstance;
- if (isDefault)
- {
- rebuildMethodInstance = methodInstGroup.mDefault;
- if (rebuildMethodInstance == NULL)
- break;
- isDefault = false;
- }
- else
- {
- //TODO: Why did we process specialized methods?
- // This caused renaming of types picking up 'T' usage from generic methods
- break;
- // if (methodInstGroup.mMethodSpecializationMap == NULL)
- // break;
- // if (methodItr == methodInstGroup.mMethodSpecializationMap->end())
- // break;
- // rebuildMethodInstance = methodItr->mValue;
- // ++methodItr;
- }
- if ((rebuildMethodInstance->IsOrInUnspecializedVariation()) || (rebuildMethodInstance->IsSpecializedGenericMethod()))
- continue;
- SetAndRestoreValue<BfMethodInstance*> prevTypeInstance(rebuildModule->mCurMethodInstance, rebuildMethodInstance);
- auto methodDef = rebuildMethodInstance->mMethodDef;
- auto methodDeclaration = methodDef->GetMethodDeclaration();
- typeState.mCurTypeDef = methodDef->mDeclaringType;
- if ((methodDeclaration != NULL) && (methodDeclaration->mAttributes != NULL))
- _CheckAttributes(methodDeclaration->mAttributes, methodDef->mDeclaringType);
- if ((mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_Type) ||
- (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_MethodGenericParam) ||
- (mResolvePassData->mGetSymbolReferenceKind == BfGetSymbolReferenceKind_TypeGenericParam))
- {
- if (methodDef->mExplicitInterface != NULL)
- CheckSymbolReferenceTypeRef(rebuildModule, methodDef->mExplicitInterface);
- for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
- {
- auto param = methodDef->mParams[paramIdx];
- CheckSymbolReferenceTypeRef(rebuildModule, param->mTypeRef);
- }
- if (methodDef->mReturnTypeRef != NULL)
- CheckSymbolReferenceTypeRef(rebuildModule, methodDef->mReturnTypeRef);
- }
- if (rebuildMethodInstance->mIgnoreBody)
- {
- auto methodDeclaration = methodDef->GetMethodDeclaration();
- if (methodDeclaration != NULL)
- mResolvePassData->HandleMethodReference(methodDeclaration->mNameNode, typeDef, methodDef);
- for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
- {
- auto param = methodDef->mParams[paramIdx];
- if (param->mParamDeclaration != NULL)
- {
- if (auto identifierNode = BfNodeDynCast<BfIdentifierNode>(param->mParamDeclaration->mNameNode))
- mResolvePassData->HandleLocalReference(identifierNode, rebuildTypeInst->mTypeDef, rebuildMethodInstance->mMethodDef, paramIdx + 1);
- else if (auto tupleExprNode = BfNodeDynCast<BfTupleExpression>(param->mParamDeclaration->mNameNode))
- {
- for (int fieldIdx = 0; fieldIdx < (int)tupleExprNode->mValues.size(); fieldIdx++)
- {
- if (auto identifierNode = BfNodeDynCast<BfIdentifierNode>(tupleExprNode->mValues[fieldIdx]))
- mResolvePassData->HandleLocalReference(identifierNode, rebuildTypeInst->mTypeDef, rebuildMethodInstance->mMethodDef, paramIdx + 1);
- }
- }
- }
- }
- }
- else
- {
- for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
- {
- auto param = methodDef->mParams[paramIdx];
- if ((param->mParamDeclaration != NULL) && (param->mParamDeclaration->mInitializer != NULL))
- {
- auto paramType = rebuildMethodInstance->GetParamType(paramIdx);
- BfConstResolver constResolver(rebuildModule);
- constResolver.Resolve(param->mParamDeclaration->mInitializer, paramType);
- }
- }
- if (rebuildMethodInstance->mHasBeenProcessed)
- {
- if (rebuildMethodInstance->mIRFunction)
- rebuildModule->mBfIRBuilder->Func_DeleteBody(rebuildMethodInstance->mIRFunction);
- rebuildMethodInstance->mHasBeenProcessed = false;
- rebuildModule->mIncompleteMethodCount++;
- }
- else
- {
- if ((rebuildModule->mIncompleteMethodCount == 0) && (!rebuildModule->mIsScratchModule))
- {
- BF_FATAL("Shouldn't be processing this method");
- }
- }
- for (int genericParamIdx = 0; genericParamIdx < (int)rebuildMethodInstance->GetNumGenericArguments(); genericParamIdx++)
- {
- BfGenericMethodParamInstance genericParamInstance(rebuildMethodInstance->mMethodDef, genericParamIdx);
- auto genericParamDef = methodDef->mGenericParams[genericParamIdx];
- if (mResolvePassData != NULL)
- {
- for (auto nameNode : genericParamDef->mNameNodes)
- if (nameNode != NULL)
- mResolvePassData->HandleMethodGenericParam(nameNode, typeDef, methodDef, genericParamIdx);
- }
- rebuildModule->ResolveGenericParamConstraints(&genericParamInstance, rebuildMethodInstance->mIsUnspecialized);
- }
- for (int externConstraintIdx = 0; externConstraintIdx < (int)methodDef->mExternalConstraints.size(); externConstraintIdx++)
- {
- BfGenericMethodParamInstance genericParamInstance(rebuildMethodInstance->mMethodDef, externConstraintIdx + (int)methodDef->mGenericParams.size());
- auto& externConstraintDef = methodDef->mExternalConstraints[externConstraintIdx];
- CheckSymbolReferenceTypeRef(module, externConstraintDef.mTypeRef);
- rebuildModule->ResolveGenericParamConstraints(&genericParamInstance, rebuildMethodInstance->mIsUnspecialized);
- }
- rebuildModule->ProcessMethod(rebuildMethodInstance);
- }
- }
- }
- }
- }
- void BfCompiler::UpdateCompletion()
- {
- if (mIsResolveOnly)
- return;
- float typeScale = 10.0f;
- float methodScale = 1.0f;
- float queueModuleScale = 50.0f;
- float genModuleScale = 50.0f;
- mCodeGen.UpdateStats();
- BF_ASSERT(mCodeGen.mQueuedCount >= mCodeGen.mCompletionCount);
- BF_ASSERT(mStats.mModulesFinished <= mStats.mModulesStarted);
- BF_ASSERT(mCodeGen.mCompletionCount <= mStats.mModulesStarted);
- float numerator = ((mStats.mQueuedTypesProcessed * typeScale) + //(mStats.mMethodsProcessed * methodScale) +
- (mStats.mModulesFinished * queueModuleScale) + (mCodeGen.mCompletionCount * genModuleScale));
- float divisor = ((mStats.mTypesQueued * typeScale) + //(mStats.mMethodsQueued * methodScale) +
- (mStats.mModulesStarted * queueModuleScale) + (mStats.mReifiedModuleCount * genModuleScale));
- float checkPct = 0;
- if (divisor > 0)
- {
- checkPct = numerator / divisor;
- BF_ASSERT(checkPct >= 0);
- if (checkPct > mCompletionPct)
- mCompletionPct = checkPct;
- }
- else
- mCompletionPct = 0;
- if (!mHadCancel)
- BF_ASSERT(mCompletionPct <= 1.0f);
- if (mCompletionPct > 1.0f)
- mCompletionPct = 1.0f;
- }
- void BfCompiler::MarkStringPool(BfModule* module)
- {
- for (int stringId : module->mStringPoolRefs)
- {
- BfStringPoolEntry& stringPoolEntry = module->mContext->mStringObjectIdMap[stringId];
- stringPoolEntry.mLastUsedRevision = mRevision;
- }
- for (int stringId : module->mUnreifiedStringPoolRefs)
- {
- BfStringPoolEntry& stringPoolEntry = module->mContext->mStringObjectIdMap[stringId];
- stringPoolEntry.mLastUsedRevision = mRevision;
- }
- for (int stringId : module->mImportFileNames)
- {
- BfStringPoolEntry& stringPoolEntry = module->mContext->mStringObjectIdMap[stringId];
- stringPoolEntry.mLastUsedRevision = mRevision;
- }
- for (int stringId : module->mSignatureIdRefs)
- {
- BfStringPoolEntry& stringPoolEntry = module->mContext->mStringObjectIdMap[stringId];
- stringPoolEntry.mLastUsedRevision = mRevision;
- }
- /*if (module->mOptModule != NULL)
- MarkStringPool(module->mOptModule);*/
- auto altModule = module->mNextAltModule;
- while (altModule != NULL)
- {
- MarkStringPool(altModule);
- altModule = altModule->mNextAltModule;
- }
- for (auto& specModulePair : module->mSpecializedMethodModules)
- MarkStringPool(specModulePair.mValue);
- }
- void BfCompiler::MarkStringPool(BfIRConstHolder* constHolder, BfIRValue irValue)
- {
- auto constant = constHolder->GetConstant(irValue);
- if ((constant != NULL) && (constant->mTypeCode == BfTypeCode_StringId))
- {
- BfStringPoolEntry& stringPoolEntry = mContext->mStringObjectIdMap[constant->mInt32];
- stringPoolEntry.mLastUsedRevision = mRevision;
- }
- }
- void BfCompiler::ClearUnusedStringPoolEntries()
- {
- BF_ASSERT(!IsHotCompile());
- for (auto module : mContext->mModules)
- {
- MarkStringPool(module);
- }
- for (auto type : mContext->mResolvedTypes)
- {
- auto typeInstance = type->ToTypeInstance();
- if (typeInstance == NULL)
- continue;
- if (typeInstance->mCustomAttributes == NULL)
- continue;
- for (auto& attribute : typeInstance->mCustomAttributes->mAttributes)
- {
- for (auto arg : attribute.mCtorArgs)
- MarkStringPool(typeInstance->mConstHolder, arg);
- for (auto setValue : attribute.mSetProperties)
- MarkStringPool(typeInstance->mConstHolder, setValue.mParam.mValue);
- for (auto setValue : attribute.mSetField)
- MarkStringPool(typeInstance->mConstHolder, setValue.mParam.mValue);
- }
- }
- for (auto itr = mContext->mStringObjectIdMap.begin(); itr != mContext->mStringObjectIdMap.end(); )
- {
- int strId = itr->mKey;
- BfStringPoolEntry& stringPoolEntry = itr->mValue;
- if (stringPoolEntry.mLastUsedRevision != mRevision)
- {
- CompileLog("Clearing unused string: %d %s\n", itr->mKey, stringPoolEntry.mString.c_str());
- mContext->mStringObjectPool.Remove(stringPoolEntry.mString);
- itr = mContext->mStringObjectIdMap.Remove(itr);
- }
- else
- ++itr;
- }
- }
- void BfCompiler::ClearBuildCache()
- {
- mCodeGen.ClearBuildCache();
- for (auto project : mSystem->mProjects)
- {
- String libPath = mOutputDirectory + "/" + project->mName + "/" + project->mName + "__.lib";
- BfpFile_Delete(libPath.c_str(), NULL);
- }
- }
- int BfCompiler::GetVDataPrefixDataCount()
- {
- return (mSystem->mPtrSize == 4) ? 2 : 1;
- }
- int BfCompiler::GetDynCastVDataCount()
- {
- int dynElements = 1 + mMaxInterfaceSlots;
- return ((dynElements * 4) + mSystem->mPtrSize - 1) / mSystem->mPtrSize;
- }
- bool BfCompiler::IsAutocomplete()
- {
- return (mResolvePassData != NULL) && (mResolvePassData->mAutoComplete != NULL);
- }
- bool BfCompiler::IsDataResolvePass()
- {
- return (mResolvePassData != NULL) && (mResolvePassData->mResolveType == BfResolveType_GetResultString);
- }
- bool BfCompiler::WantsClassifyNode(BfAstNode* node)
- {
- return (mResolvePassData != NULL) && (mResolvePassData->GetSourceClassifier(node) != NULL);
- }
- BfAutoComplete* BfCompiler::GetAutoComplete()
- {
- if (mResolvePassData != NULL)
- return mResolvePassData->mAutoComplete;
- return NULL;
- }
- bool BfCompiler::IsHotCompile()
- {
- return mOptions.mHotProject != NULL;
- }
- bool BfCompiler::IsSkippingExtraResolveChecks()
- {
- return mIsResolveOnly && !mOptions.mExtraResolveChecks;
- }
- int BfCompiler::GetVTableMethodOffset()
- {
- if (mOptions.mHasVDataExtender)
- return 1;
- return 0;
- }
- bool BfCompiler::DoWorkLoop(bool onlyReifiedTypes, bool onlyReifiedMethods)
- {
- bool hadAnyWork = false;
- while (true)
- {
- bool didWork = false;
- didWork |= mContext->ProcessWorkList(onlyReifiedTypes, onlyReifiedMethods);
- if (!didWork)
- break;
- hadAnyWork = true;
- }
- return hadAnyWork;
- }
- BfMangler::MangleKind BfCompiler::GetMangleKind()
- {
- if (mOptions.mToolsetType == BfToolsetType_GNU)
- return BfMangler::MangleKind_GNU;
- return (mSystem->mPtrSize == 8) ? BfMangler::MangleKind_Microsoft_64 : BfMangler::MangleKind_Microsoft_32;
- }
- //////////////////////////////////////////////////////////////////////////
- int ArrTest()
- {
- //SizedArray<int, 8> intArr;
- //Array<int> intArr;
- //std::vector<int> intArr;
- BfSizedVector<int, 8> intArr;
- //int val = intArr.GetLastSafe();
- intArr.push_back(123);
- intArr.pop_back();
- intArr.push_back(234);
- intArr.push_back(345);
- //intArr.push_back(567);
- //auto itr = std::find(intArr.begin(), intArr.end(), 234);
- //intArr.erase(itr);
- for (auto itr = intArr.begin(); itr != intArr.end(); )
- {
- if (*itr == 234)
- itr = intArr.erase(itr);
- else
- itr++;
- }
- return (int)intArr.size();
- //intArr.RemoveAt(2);
- }
- //////////////////////////////////////////////////////////////////////////
- void BfCompiler::PopulateReified()
- {
- BfLogSysM("BfCompiler::PopulateReified\n");
- BP_ZONE("PopulateReified");
- BfContext* context = mContext;
- bool hasTests = mSystem->HasTestProjects();
- Array<BfMethodInstance*> impChainHeadMethods;
- // Types can pull in new dependencies, so fully populate types until they stop
- bool reifiedOnly = mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude;
- while (true)
- {
- BP_ZONE("Compile_PopulateTypes");
- int startTypeInitCount = mTypeInitCount;
- bool didWork = false;
- BfLogSysM("PopulateReified iteration start\n");
- Array<BfType*> typeList;
- typeList.Reserve(context->mResolvedTypes.GetCount());
- for (auto type : context->mResolvedTypes)
- typeList.Add(type);
- int typeCount = 0;
- for (auto type : typeList)
- {
- auto module = type->GetModule();
- typeCount++;
- if (module == NULL)
- continue;
- if (!type->IsReified())
- {
- // On compiles, only handle reified types in this loop. This fixes cases where our first instance of a dependent type
- // is found to be unreified and then we have to reify it later. It's not an error, just a compile perf issue
- continue;
- }
- // We have to not populate generic type instances because that may force us to populate a type that SHOULD be deleted
- if ((type->IsIncomplete()) && (type->IsTypeInstance()) && (!type->IsGenericTypeInstance()))
- {
- mSystem->CheckLockYield();
- module->PopulateType(type, BfPopulateType_Full);
- }
- auto typeInst = type->ToTypeInstance();
- if ((typeInst != NULL) && (typeInst->IsGenericTypeInstance()) && (!typeInst->IsUnspecializedType()) &&
- (!typeInst->IsDelegateFromTypeRef()) && (!typeInst->IsFunctionFromTypeRef()) && (!typeInst->IsTuple()))
- {
- auto unspecializedType = module->GetUnspecializedTypeInstance(typeInst);
- if (!unspecializedType->mIsReified)
- unspecializedType->mIsReified = true;
- }
- if ((type->IsValueType()) && (!type->IsUnspecializedType()))
- {
- bool dynamicBoxing = false;
- if ((typeInst != NULL) && (typeInst->mTypeOptionsIdx != -2))
- {
- auto typeOptions = mSystem->GetTypeOptions(typeInst->mTypeOptionsIdx);
- if (typeOptions != NULL)
- {
- if (typeOptions->Apply(false, BfOptionFlags_ReflectBoxing))
- dynamicBoxing = true;
- }
- }
- auto reflectKind = module->GetReflectKind(BfReflectKind_None, typeInst);
- if ((reflectKind & BfReflectKind_DynamicBoxing) != 0)
- dynamicBoxing = true;
- if (dynamicBoxing)
- {
- auto boxedType = module->CreateBoxedType(typeInst);
- module->AddDependency(boxedType, typeInst, BfDependencyMap::DependencyFlag_Allocates);
- boxedType->mHasBeenInstantiated = true;
- }
- }
- // Check reifications forced by virtuals or interfaces
- if ((typeInst != NULL) && (typeInst->mIsReified) && (!typeInst->IsUnspecializedType()) && (!typeInst->IsInterface()) &&
- (!typeInst->IsIncomplete()))
- {
- // If we have chained methods, make sure we implement the chain members if the chain head is implemented and reified
- if (typeInst->mTypeDef->mIsCombinedPartial)
- {
- typeInst->mTypeDef->PopulateMemberSets();
- bool hasUnimpChainMembers = false;
- impChainHeadMethods.Clear();
- for (auto& methodInstanceGroup : typeInst->mMethodInstanceGroups)
- {
- auto methodInstance = methodInstanceGroup.mDefault;
- if (methodInstance == NULL)
- continue;
- if (methodInstance->mChainType == BfMethodChainType_ChainHead)
- {
- if (methodInstance->IsReifiedAndImplemented())
- impChainHeadMethods.Add(methodInstance);
- }
- else if (methodInstance->mChainType == BfMethodChainType_ChainMember)
- {
- if (!methodInstance->IsReifiedAndImplemented())
- hasUnimpChainMembers = true;
- }
- else if ((methodInstance->mChainType == BfMethodChainType_None) && (methodInstance->mMethodDef->IsDefaultCtor()))
- {
- if (!methodInstance->IsReifiedAndImplemented())
- hasUnimpChainMembers = true;
- }
- else if (methodInstance->mIsInnerOverride)
- {
- if (!methodInstance->IsReifiedAndImplemented())
- {
- bool forceMethod = false;
- BfMemberSetEntry* memberSetEntry;
- if (typeInst->mTypeDef->mMethodSet.TryGetWith((StringImpl&)methodInstance->mMethodDef->mName, &memberSetEntry))
- {
- BfMethodDef* checkMethodDef = (BfMethodDef*)memberSetEntry->mMemberDef;
- while (checkMethodDef != NULL)
- {
- auto& checkMethodInstanceGroup = typeInst->mMethodInstanceGroups[checkMethodDef->mIdx];
- auto checkMethodInstance = checkMethodInstanceGroup.mDefault;
- if (checkMethodInstance != NULL)
- {
- if ((checkMethodDef->mIsExtern) && (checkMethodInstance->IsReifiedAndImplemented()))
- forceMethod = true;
- }
- checkMethodDef = checkMethodDef->mNextWithSameName;
- }
- }
- if (forceMethod)
- {
- typeInst->mModule->GetMethodInstance(methodInstance->GetOwner(), methodInstance->mMethodDef, BfTypeVector(),
- (BfGetMethodInstanceFlags)(BfGetMethodInstanceFlag_UnspecializedPass));
- }
- }
- }
- }
- if ((hasUnimpChainMembers) && (!impChainHeadMethods.IsEmpty()))
- {
- for (auto& methodInstanceGroup : typeInst->mMethodInstanceGroups)
- {
- auto methodInstance = methodInstanceGroup.mDefault;
- if (methodInstance == NULL)
- continue;
- bool forceMethod = false;
- if (methodInstance->mChainType == BfMethodChainType_ChainMember)
- {
- if (!methodInstance->IsReifiedAndImplemented())
- {
- for (auto impMethodInstance : impChainHeadMethods)
- {
- if (typeInst->mModule->CompareMethodSignatures(methodInstance, impMethodInstance))
- {
- forceMethod = true;
- }
- }
- }
- }
- else if (methodInstance->mMethodDef->IsDefaultCtor())
- {
- if (!methodInstance->IsReifiedAndImplemented())
- forceMethod = true;
- }
- if (forceMethod)
- {
- typeInst->mModule->GetMethodInstance(methodInstance->GetOwner(), methodInstance->mMethodDef, BfTypeVector(),
- (BfGetMethodInstanceFlags)(BfGetMethodInstanceFlag_UnspecializedPass));
- }
- }
- }
- }
- // Only check virtual stuff if we have been instantiated
- if ((typeInst->HasBeenInstantiated()) && (!mCanceling))
- {
- // If we have any virtual methods overrides that are unreified but the declaring virtual method is reified then we also need to reify
- for (int virtIdx = 0; virtIdx < typeInst->mVirtualMethodTable.mSize; virtIdx++)
- {
- auto& vEntry = typeInst->mVirtualMethodTable[virtIdx];
- if ((vEntry.mDeclaringMethod.mTypeInstance == NULL) ||
- (vEntry.mDeclaringMethod.mTypeInstance->IsIncomplete()) ||
- (vEntry.mImplementingMethod.mTypeInstance == NULL) ||
- (vEntry.mImplementingMethod.mTypeInstance->IsIncomplete()))
- continue;
- BfMethodInstance* declaringMethod = vEntry.mDeclaringMethod;
- if (declaringMethod == NULL)
- continue;
- if ((declaringMethod->mIsReified) && (declaringMethod->mMethodInstanceGroup->IsImplemented()))
- {
- if (vEntry.mImplementingMethod.mKind == BfMethodRefKind_AmbiguousRef)
- {
- auto checkTypeInst = typeInst;
- while (checkTypeInst != NULL)
- {
- BfMemberSetEntry* memberSetEntry;
- if (checkTypeInst->mTypeDef->mMethodSet.TryGetWith(String(declaringMethod->mMethodDef->mName), &memberSetEntry))
- {
- BfMethodDef* methodDef = (BfMethodDef*)memberSetEntry->mMemberDef;
- while (methodDef != NULL)
- {
- if ((methodDef->mIsOverride) && (methodDef->mParams.mSize == declaringMethod->mMethodDef->mParams.mSize))
- {
- auto implMethod = typeInst->mModule->GetRawMethodInstance(typeInst, methodDef);
- if ((implMethod != NULL) && (typeInst->mModule->CompareMethodSignatures(declaringMethod, implMethod)))
- {
- if ((implMethod != NULL) && ((!implMethod->mMethodInstanceGroup->IsImplemented()) || (!implMethod->mIsReified)))
- {
- didWork = true;
- if (!typeInst->mModule->mIsModuleMutable)
- typeInst->mModule->StartExtension();
- typeInst->mModule->GetMethodInstance(implMethod);
- }
- }
- }
- methodDef = methodDef->mNextWithSameName;
- }
- }
- if (checkTypeInst == declaringMethod->GetOwner())
- break;
- checkTypeInst = checkTypeInst->mBaseType;
- }
- }
- else
- {
- BfMethodInstance* implMethod = vEntry.mImplementingMethod;
- if ((implMethod != NULL) && ((!implMethod->mMethodInstanceGroup->IsImplemented()) || (!implMethod->mIsReified)))
- {
- didWork = true;
- if (!typeInst->mModule->mIsModuleMutable)
- typeInst->mModule->StartExtension();
- typeInst->mModule->GetMethodInstance(implMethod);
- }
- }
- }
- }
- auto checkType = typeInst;
- while (checkType != NULL)
- {
- if ((checkType != typeInst) && (checkType->HasBeenInstantiated()))
- {
- // We will already check this type here in its own loop
- break;
- }
- if (checkType->mDefineState < BfTypeDefineState_DefinedAndMethodsSlotted)
- break;
- for (auto& ifaceTypeInst : checkType->mInterfaces)
- {
- auto ifaceInst = ifaceTypeInst.mInterfaceType;
- int startIdx = ifaceTypeInst.mStartInterfaceTableIdx;
- int iMethodCount = (int)ifaceInst->mMethodInstanceGroups.size();
- auto declTypeDef = ifaceTypeInst.mDeclaringType;
- for (int iMethodIdx = 0; iMethodIdx < iMethodCount; iMethodIdx++)
- {
- auto& ifaceMethodInstGroup = ifaceInst->mMethodInstanceGroups[iMethodIdx];
- auto ifaceMethodInst = ifaceMethodInstGroup.mDefault;
- if (typeInst->IsObject())
- {
- // If the implementor is an object then this can be dynamically dispatched
- }
- else
- {
- // If this method is explicitly reflected then a struct's implementation may be invoked with reflection
- if (!ifaceMethodInstGroup.mExplicitlyReflected)
- continue;
- }
- if ((ifaceMethodInst == NULL) || (!ifaceMethodInst->IsReifiedAndImplemented()))
- continue;
- auto implMethodRef = &checkType->mInterfaceMethodTable[iMethodIdx + startIdx].mMethodRef;
- BfMethodInstance* implMethod = *implMethodRef;
- if (implMethod == NULL)
- continue;
- // Reify any interface methods that could be called dynamically
- if ((!implMethod->IsReifiedAndImplemented()) && (implMethod->GetNumGenericParams() == 0) && (!implMethod->mMethodDef->mIsStatic) &&
- (!implMethod->mReturnType->IsConcreteInterfaceType()))
- {
- didWork = true;
- checkType->mModule->GetMethodInstance(implMethod);
- }
- }
- }
- checkType = checkType->mBaseType;
- }
- for (auto& reifyDep : typeInst->mReifyMethodDependencies)
- {
- if ((reifyDep.mDepMethod.mTypeInstance == NULL) ||
- (reifyDep.mDepMethod.mTypeInstance->IsIncomplete()))
- continue;
- BfMethodInstance* depMethod = reifyDep.mDepMethod;
- if (depMethod == NULL)
- continue;
- if ((depMethod->mIsReified) && (depMethod->mMethodInstanceGroup->IsImplemented()))
- {
- auto methodDef = typeInst->mTypeDef->mMethods[reifyDep.mMethodIdx];
- typeInst->mModule->GetMethodInstance(typeInst, methodDef, BfTypeVector());
- }
- }
- }
- }
- }
- if (mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude)
- {
- BfLogSysM("BfCompiler::PopulateReified finding Main\n");
- for (auto project : mSystem->mProjects)
- {
- String entryClassName = project->mStartupObject;
- auto typeDef = mSystem->FindTypeDef(entryClassName, 0, project, {}, NULL, BfFindTypeDefFlag_AllowGlobal);
- if (typeDef != NULL)
- {
- typeDef->mIsAlwaysInclude = true;
- auto resolvedType = mContext->mScratchModule->ResolveTypeDef(typeDef);
- if (resolvedType != NULL)
- {
- auto resolvedTypeInst = resolvedType->ToTypeInstance();
- if (resolvedTypeInst != NULL)
- {
- auto module = resolvedTypeInst->GetModule();
- if (!module->mIsReified)
- module->ReifyModule();
- mContext->mScratchModule->PopulateType(resolvedType, BfPopulateType_Full);
- BfMemberSetEntry* memberSetEntry;
- if (resolvedTypeInst->mTypeDef->mMethodSet.TryGetWith(String("Main"), &memberSetEntry))
- {
- BfMethodDef* methodDef = (BfMethodDef*)memberSetEntry->mMemberDef;
- while (methodDef != NULL)
- {
- auto moduleMethodInstance = mContext->mScratchModule->GetMethodInstanceAtIdx(resolvedTypeInst, methodDef->mIdx);
- auto methodInstance = moduleMethodInstance.mMethodInstance;
- if (methodInstance->GetParamCount() != 0)
- {
- mContext->mScratchModule->GetInternalMethod("CreateParamsArray");
- mContext->mScratchModule->GetInternalMethod("DeleteStringArray");
- }
- methodDef = methodDef->mNextWithSameName;
- }
- }
- }
- }
- }
- }
- }
- BfLogSysM("PopulateReified iteration done\n");
- didWork |= DoWorkLoop(reifiedOnly, reifiedOnly);
- if (reifiedOnly)
- didWork |= DoWorkLoop(false, reifiedOnly);
- if (startTypeInitCount != mTypeInitCount)
- didWork = true;
- if (didWork)
- continue;
- // We get everything on the first pass through
- if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
- break;
- if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_SkipUnused)
- break;
- if (startTypeInitCount == mTypeInitCount)
- break;
- }
- }
- bool BfCompiler::IsCePaused()
- {
- return (mCeMachine != NULL) && (mCeMachine->mDbgPaused);
- }
- bool BfCompiler::EnsureCeUnpaused(BfType* refType)
- {
- if ((mCeMachine == NULL) || (!mCeMachine->mDbgPaused))
- return true;
- mCeMachine->mDebugger->mCurDbgState->mReferencedIncompleteTypes = true;
- //mPassInstance->Fail(StrFormat("Use of incomplete type '%s'", mCeMachine->mCeModule->TypeToString(refType).c_str()));
- return false;
- }
- void BfCompiler::HotCommit()
- {
- if (mHotState == NULL)
- return;
- mHotState->mCommittedHotCompileIdx = mOptions.mHotCompileIdx;
- for (auto type : mContext->mResolvedTypes)
- {
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeInst->mHotTypeData == NULL)
- continue;
- for (int typeIdx = (int)typeInst->mHotTypeData->mTypeVersions.size() - 1; typeIdx >= 0; typeIdx--)
- {
- auto hotVersion = typeInst->mHotTypeData->mTypeVersions[typeIdx];
- if (hotVersion->mCommittedHotCompileIdx != -1)
- break;
- hotVersion->mCommittedHotCompileIdx = mHotState->mCommittedHotCompileIdx;
- if ((!hotVersion->mInterfaceMapping.IsEmpty()) && (typeIdx > 0))
- {
- auto hotVersionHead = typeInst->mHotTypeData->GetLatestVersionHead();
- if ((hotVersionHead != hotVersion) && (hotVersionHead->mDataHash == hotVersion->mDataHash))
- {
- // When we have a slot failure, the data hash will match but we actually do need to use the new mInterfaceMapping entries
- // So we copy them over to the
- hotVersionHead->mInterfaceMapping = hotVersion->mInterfaceMapping;
- }
- }
- }
- }
- }
- void BfCompiler::HotResolve_Start(HotResolveFlags flags)
- {
- BfLogSysM("BfCompiler::HotResolve_Start\n");
- delete mHotResolveData;
- mHotResolveData = new HotResolveData();
- mHotResolveData->mFlags = flags;
- mHotResolveData->mHotTypeIdFlags.Resize(mCurTypeId);
- mHotResolveData->mReasons.Resize(mCurTypeId);
- if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
- {
- HotResolve_AddReachableMethod("BfCallAllStaticDtors");
- for (auto& kv : mHotData->mFuncPtrs)
- {
- auto funcRef = kv.mValue;
- HotResolve_AddReachableMethod(funcRef->mMethod, HotTypeFlag_FuncPtr, true);
- }
- }
- }
- //#define HOT_DEBUG_NAME
- bool BfCompiler::HotResolve_AddReachableMethod(BfHotMethod* hotMethod, HotTypeFlags flags, bool devirtualized, bool forceProcess)
- {
- #ifdef HOT_DEBUG_NAME
- HotResolve_PopulateMethodNameMap();
- String* namePtr = NULL;
- if (mHotData->mMethodNameMap.TryGetValue(hotMethod, &namePtr))
- {
- }
- #endif
- HotReachableData* hotReachableData;
- if (mHotResolveData->mReachableMethods.TryAdd(hotMethod, NULL, &hotReachableData))
- {
- hotMethod->mRefCount++;
- }
- else
- {
- hotReachableData->mTypeFlags = (HotTypeFlags)(hotReachableData->mTypeFlags | flags);
- if ((!devirtualized) && (!hotReachableData->mHadNonDevirtualizedCall))
- {
- hotReachableData->mHadNonDevirtualizedCall = true;
- if (!forceProcess)
- return true;
- }
- if (!forceProcess)
- return false;
- }
- hotReachableData->mTypeFlags = (HotTypeFlags)(hotReachableData->mTypeFlags | flags);
- if (!devirtualized)
- hotReachableData->mHadNonDevirtualizedCall = true;
- for (auto hotDepData : hotMethod->mReferences)
- {
- if (hotDepData->mDataKind == BfHotDepDataKind_ThisType)
- {
- auto hotThisType = (BfHotThisType*)hotDepData;
- auto hotTypeVersion = hotThisType->mTypeVersion;
- HotTypeFlags hotTypeFlags = mHotResolveData->mHotTypeIdFlags[hotTypeVersion->mTypeId];
- bool isAllocated = (hotTypeFlags & (HotTypeFlag_Heap | HotTypeFlag_CanAllocate)) != 0;
- if (!isAllocated)
- {
- if (mHotResolveData->mDeferredThisCheckMethods.Add(hotMethod))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- mHotResolveData->mDeferredThisCheckMethods.Remove(hotMethod);
- }
- }
- else if (hotDepData->mDataKind == BfHotDepDataKind_Allocation)
- {
- auto hotAllocation = (BfHotAllocation*)hotDepData;
- auto hotTypeVersion = hotAllocation->mTypeVersion;
- HotResolve_ReportType(hotTypeVersion, flags, hotMethod);
- HotResolve_ReportType(hotTypeVersion, HotTypeFlag_CanAllocate, hotMethod);
- }
- else if (hotDepData->mDataKind == BfHotDepDataKind_TypeVersion)
- {
- auto hotTypeVersion = (BfHotTypeVersion*)hotDepData;
- HotResolve_ReportType(hotTypeVersion, flags, hotMethod);
- }
- else if (hotDepData->mDataKind == BfHotDepDataKind_Method)
- {
- auto checkMethod = (BfHotMethod*)hotDepData;
- HotResolve_AddReachableMethod(checkMethod, flags, false);
- }
- else if (hotDepData->mDataKind == BfHotDepDataKind_DevirtualizedMethod)
- {
- auto checkMethod = (BfHotDevirtualizedMethod*)hotDepData;
- HotResolve_AddReachableMethod(checkMethod->mMethod, flags, true);
- }
- else if (hotDepData->mDataKind == BfHotDepDataKind_DupMethod)
- {
- auto checkMethod = (BfHotDupMethod*)hotDepData;
- HotResolve_AddReachableMethod(checkMethod->mMethod, flags, true);
- }
- }
- return true;
- }
- void BfCompiler::HotResolve_AddReachableMethod(const StringImpl& methodName)
- {
- BfLogSysM("HotResolve_AddReachableMethod %s\n", methodName.c_str());
- String mangledName = methodName;
- BfHotMethod** hotMethodPtr;
- if (!mHotData->mMethodMap.TryGetValue(mangledName, &hotMethodPtr))
- {
- BfLogSysM("Hot method not found\n");
- return;
- }
- BfHotMethod* hotMethod = *hotMethodPtr;
- while (hotMethod->mPrevVersion != NULL)
- {
- if (hotMethod->mSrcTypeVersion->mCommittedHotCompileIdx != -1)
- break;
- hotMethod = hotMethod->mPrevVersion;
- }
- HotResolve_AddReachableMethod(hotMethod, HotTypeFlag_ActiveFunction, true);
- }
- void BfCompiler::HotResolve_AddActiveMethod(BfHotMethod* hotMethod)
- {
- #ifdef HOT_DEBUG_NAME
- HotResolve_PopulateMethodNameMap();
- String* namePtr = NULL;
- if (mHotData->mMethodNameMap.TryGetValue(hotMethod, &namePtr))
- {
- }
- #endif
- if (mHotResolveData->mActiveMethods.Add(hotMethod))
- {
- hotMethod->mRefCount++;
- }
- // We don't need to mark reachable methods unless we had data changes
- if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
- {
- HotResolve_AddReachableMethod(hotMethod, HotTypeFlag_ActiveFunction, true);
- }
- if ((hotMethod->mFlags & BfHotDepDataFlag_HasDup) != 0)
- {
- for (auto depData : hotMethod->mReferences)
- {
- if (depData->mDataKind != BfHotDepDataKind_DupMethod)
- continue;
- auto hotDupMethod = (BfHotDupMethod*)depData;
- HotResolve_AddActiveMethod(hotDupMethod->mMethod);
- }
- }
- }
- void BfCompiler::HotResolve_AddActiveMethod(const StringImpl& methodName)
- {
- BfLogSysM("HotResolve_AddActiveMethod %s\n", methodName.c_str());
- StringT<512> mangledName;
- int hotCompileIdx = 0;
- int tabIdx = (int)methodName.IndexOf('\t');
- if (tabIdx != -1)
- {
- mangledName = methodName.Substring(0, tabIdx);
- hotCompileIdx = atoi(methodName.c_str() + tabIdx + 1);
- }
- else
- mangledName = methodName;
- bool isDelegateRef = false;
- BfHotMethod** hotMethodPtr;
- if (!mHotData->mMethodMap.TryGetValue(mangledName, &hotMethodPtr))
- {
- BfLogSysM("Hot method not found\n");
- return;
- }
- BfHotMethod* hotMethod = *hotMethodPtr;
- while (hotMethod->mPrevVersion != NULL)
- {
- if ((hotMethod->mSrcTypeVersion->mCommittedHotCompileIdx != -1) && (hotCompileIdx < hotMethod->mSrcTypeVersion->mCommittedHotCompileIdx))
- break;
- hotMethod = hotMethod->mPrevVersion;
- }
- HotResolve_AddActiveMethod(hotMethod);
- }
- void BfCompiler::HotResolve_AddDelegateMethod(const StringImpl& methodName)
- {
- BfLogSysM("HotResolve_HotResolve_AddDelegateMethod %s\n", methodName.c_str());
- String mangledName = methodName;
- BfHotMethod** hotMethodPtr;
- if (!mHotData->mMethodMap.TryGetValue(mangledName, &hotMethodPtr))
- {
- BfLogSysM("Hot method not found\n");
- return;
- }
- BfHotMethod* hotMethod = *hotMethodPtr;
- HotResolve_AddReachableMethod(hotMethod, HotTypeFlag_Delegate, true);
- }
- void BfCompiler::HotResolve_ReportType(BfHotTypeVersion* hotTypeVersion, HotTypeFlags flags, BfHotDepData* reason)
- {
- auto& flagsRef = mHotResolveData->mHotTypeFlags[hotTypeVersion];
- if (flagsRef == (flagsRef | flags))
- return;
- flagsRef = (HotTypeFlags)(flags | flagsRef);
- bool applyFlags = true;
- if ((flags & (BfCompiler::HotTypeFlag_ActiveFunction | BfCompiler::HotTypeFlag_Delegate | BfCompiler::HotTypeFlag_FuncPtr)) != 0)
- {
- applyFlags = (hotTypeVersion->mCommittedHotCompileIdx != -1) && (mHotState->mPendingDataChanges.Contains(hotTypeVersion->mTypeId));
- if ((!applyFlags) && (hotTypeVersion->mCommittedHotCompileIdx != -1))
- applyFlags = mHotState->mPendingFailedSlottings.Contains(hotTypeVersion->mTypeId);
- if (applyFlags)
- {
- if (reason != NULL)
- mHotResolveData->mReasons[hotTypeVersion->mTypeId] = reason;
- }
- }
- if (applyFlags)
- {
- auto& flagsIdRef = mHotResolveData->mHotTypeIdFlags[hotTypeVersion->mTypeId];
- flagsIdRef = (HotTypeFlags)(flags | flagsIdRef);
- }
- BfLogSysM("HotResolve_ReportType %p %s Flags:%X DeclHotIdx:%d\n", hotTypeVersion, mContext->TypeIdToString(hotTypeVersion->mTypeId).c_str(), flags, hotTypeVersion->mDeclHotCompileIdx);
- for (auto member : hotTypeVersion->mMembers)
- {
- HotResolve_ReportType(member, flags, reason);
- }
- }
- void BfCompiler::HotResolve_ReportType(int typeId, HotTypeFlags flags)
- {
- if ((uint)typeId >= mHotResolveData->mHotTypeIdFlags.size())
- {
- BF_DBG_FATAL("Invalid typeId");
- return;
- }
- if (mHotResolveData->mHotTypeIdFlags[typeId] == (mHotResolveData->mHotTypeIdFlags[typeId] | flags))
- return;
- auto hotTypeData = mContext->GetHotTypeData(typeId);
- if (hotTypeData != NULL)
- {
- auto hotTypeVersion = hotTypeData->GetTypeVersion(mHotState->mCommittedHotCompileIdx);
- BF_ASSERT(hotTypeVersion != NULL);
- if (hotTypeVersion != NULL)
- HotResolve_ReportType(hotTypeVersion, flags, NULL);
- }
- mHotResolveData->mHotTypeIdFlags[typeId] = (HotTypeFlags)(flags | mHotResolveData->mHotTypeIdFlags[typeId]);
- }
- void BfCompiler::HotResolve_PopulateMethodNameMap()
- {
- if (!mHotData->mMethodNameMap.IsEmpty())
- return;
- for (auto& kv : mHotData->mMethodMap)
- {
- auto hotMethod = kv.mValue;
- while (hotMethod != NULL)
- {
- mHotData->mMethodNameMap[hotMethod] = &kv.mKey;
- hotMethod = hotMethod->mPrevVersion;
- }
- }
- }
- String BfCompiler::HotResolve_Finish()
- {
- BfLogSysM("HotResolve_Finish\n");
- if (mHotState == NULL)
- {
- // It's possible we did a HotCompile with no file changes and therefore didn't actually do a compile
- return "";
- }
- String result;
- if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
- {
- BF_ASSERT(!mHotState->mPendingDataChanges.IsEmpty() || !mHotState->mPendingFailedSlottings.IsEmpty());
- }
- else
- {
- BF_ASSERT(mHotState->mPendingDataChanges.IsEmpty() && mHotState->mPendingFailedSlottings.IsEmpty());
- }
- if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
- {
- auto _AddUsedType = [&](BfTypeDef* typeDef)
- {
- auto type = mContext->mUnreifiedModule->ResolveTypeDef(mReflectTypeInstanceTypeDef);
- if (type != NULL)
- HotResolve_ReportType(type->mTypeId, BfCompiler::HotTypeFlag_Heap);
- };
- // We have some types that can be allocated in a read-only section- pretend they are on the heap
- _AddUsedType(mReflectTypeInstanceTypeDef);
- _AddUsedType(mStringTypeDef);
- // Find any virtual method overrides that may have been called.
- // These can cause new reachable virtual methods to be called, which may take more than one iteration to fully resolve
- for (int methodPass = 0; true; methodPass++)
- {
- bool didWork = false;
- for (auto hotMethod : mHotResolveData->mDeferredThisCheckMethods)
- {
- if (HotResolve_AddReachableMethod(hotMethod, BfCompiler::HotTypeFlag_ActiveFunction, true, true))
- didWork = true;
- }
- HotTypeFlags typeFlags = HotTypeFlag_None;
- for (auto& kv : mHotData->mMethodMap)
- {
- String& methodName = kv.mKey;
- auto hotMethod = kv.mValue;
- bool doCall = false;
- bool forceAdd = false;
- if (mHotResolveData->mReachableMethods.ContainsKey(hotMethod))
- continue;
- for (auto ref : hotMethod->mReferences)
- {
- if (ref->mDataKind == BfHotDepDataKind_ThisType)
- continue;
- if (ref->mDataKind != BfHotDepDataKind_VirtualDecl)
- break;
- auto hotVirtualDecl = (BfHotVirtualDeclaration*)ref;
- HotReachableData* hotReachableData;
- if (mHotResolveData->mReachableMethods.TryGetValue(hotVirtualDecl->mMethod, &hotReachableData))
- {
- #ifdef HOT_DEBUG_NAME
- HotResolve_PopulateMethodNameMap();
- String* namePtr = NULL;
- if (mHotData->mMethodNameMap.TryGetValue(hotVirtualDecl->mMethod, &namePtr))
- {
- }
- #endif
- if (hotReachableData->mHadNonDevirtualizedCall)
- {
- typeFlags = hotReachableData->mTypeFlags;
- doCall = true;
- }
- }
- }
- if (!doCall)
- {
- if ((hotMethod->mFlags & BfHotDepDataFlag_AlwaysCalled) != 0)
- {
- typeFlags = BfCompiler::HotTypeFlag_ActiveFunction;
- doCall = true;
- }
- }
- if (doCall)
- {
- if (HotResolve_AddReachableMethod(hotMethod, typeFlags, true, forceAdd))
- didWork = true;
- }
- }
- if (!didWork)
- break;
- }
- int errorCount = 0;
- for (int typeId = 0; typeId < (int)mHotResolveData->mHotTypeIdFlags.size(); typeId++)
- {
- auto flags = mHotResolveData->mHotTypeIdFlags[typeId];
- if (flags == 0)
- continue;
- auto type = mContext->mTypes[typeId];
- Dictionary<BfHotMethod*, String*> methodNameMap;
- if ((flags > BfCompiler::HotTypeFlag_UserNotUsed) &&
- ((mHotState->mPendingDataChanges.Contains(typeId)) || (mHotState->mPendingFailedSlottings.Contains(typeId))))
- {
- bool isBadTypeUsed = false;
- if ((flags & HotTypeFlag_Heap) != 0)
- isBadTypeUsed = true;
- else if ((flags & (HotTypeFlag_ActiveFunction | HotTypeFlag_Delegate | HotTypeFlag_FuncPtr)) != 0)
- {
- // If we detect an old version being used, it's only an issue if this type can actually be allocated
- if ((flags & HotTypeFlag_CanAllocate) != 0)
- {
- isBadTypeUsed = true;
- }
- }
- if (isBadTypeUsed)
- {
- bool reasonIsActiveMethod = false;
- String methodReason;
- auto reason = mHotResolveData->mReasons[typeId];
- if ((reason != NULL) && (reason->mDataKind == BfHotDepDataKind_Method))
- {
- auto hotMethod = (BfHotMethod*)reason;
- reasonIsActiveMethod = mHotResolveData->mActiveMethods.Contains(hotMethod);
- HotResolve_PopulateMethodNameMap();
- String** strPtr;
- if (mHotData->mMethodNameMap.TryGetValue(hotMethod, &strPtr))
- {
- methodReason += BfDemangler::Demangle((**strPtr), DbgLanguage_Beef, BfDemangler::Flag_BeefFixed);
- }
- }
- errorCount++;
- if (errorCount >= 1000)
- {
- result += "\n (more errors)...";
- break;
- }
- if (!result.IsEmpty())
- result += "\n";
- result += "'";
- result += mContext->TypeIdToString(typeId);
- result += "'";
- if ((flags & BfCompiler::HotTypeFlag_Heap) != 0)
- result += " allocated on the heap";
- else if ((flags & BfCompiler::HotTypeFlag_ActiveFunction) != 0)
- {
- if (reasonIsActiveMethod)
- result += StrFormat(" used by active method '%s'", methodReason.c_str());
- else if (!methodReason.IsEmpty())
- result += StrFormat(" previous data version used by deleted method '%s', reachable by an active method", methodReason.c_str());
- else
- result += " previous data version used by a deleted method reachable by an active method";
- }
- else if ((flags & BfCompiler::HotTypeFlag_Delegate) != 0)
- {
- if (!methodReason.IsEmpty())
- result += StrFormat(" previous data version used by deleted method '%s', reachable by a delegate", methodReason.c_str());
- else
- result += " previous data version used by a deleted method reachable by a delegate";
- }
- else if ((flags & BfCompiler::HotTypeFlag_FuncPtr) != 0)
- {
- if (!methodReason.IsEmpty())
- result += StrFormat(" previous data version used by deleted method '%s', reachable by a function pointer", methodReason.c_str());
- else
- result += " previous data version used by a deleted method reachable by a function pointer";
- }
- else if ((flags & BfCompiler::HotTypeFlag_UserUsed) != 0)
- result += " stated as used by the program";
- }
- }
- String typeName = mContext->TypeIdToString(typeId);
- BfLogSysM(" %d %s %02X\n", typeId, typeName.c_str(), flags);
- }
- if (result.IsEmpty())
- {
- for (auto typeId : mHotState->mPendingDataChanges)
- {
- auto type = mContext->mTypes[typeId];
- auto typeInstance = type->ToTypeInstance();
- BF_ASSERT(typeInstance->mHotTypeData->mPendingDataChange);
- typeInstance->mHotTypeData->mPendingDataChange = false;
- typeInstance->mHotTypeData->mHadDataChange = true;
- typeInstance->mHotTypeData->mVTableOrigLength = -1;
- typeInstance->mHotTypeData->mOrigInterfaceMethodsLength = -1;
- BfLogSysM("Pending data change applied to type %p\n", typeInstance);
- }
- mHotState->mPendingDataChanges.Clear();
- mHotState->mPendingFailedSlottings.Clear();
- }
- }
- ClearOldHotData();
- if ((mHotResolveData->mFlags & HotResolveFlag_HadDataChanges) != 0)
- {
- for (int pass = 0; pass < 2; pass++)
- {
- bool wantsReachable = pass == 0;
- Array<String> methodList;
- for (auto& kv : mHotData->mMethodMap)
- {
- auto hotMethod = kv.mValue;
- bool reachable = mHotResolveData->mReachableMethods.ContainsKey(hotMethod);
- if (reachable != wantsReachable)
- continue;
- String methodName;
- methodName += BfDemangler::Demangle(kv.mKey, DbgLanguage_Beef, BfDemangler::Flag_BeefFixed);
- methodName += " - ";
- methodName += kv.mKey;
- methodList.Add(methodName);
- }
- methodList.Sort([](const String& lhs, const String& rhs) { return lhs < rhs; });
- for (auto& methodName : methodList)
- BfLogSysM("%s: %s\n", wantsReachable ? "Reachable" : "Unreachable", methodName.c_str());
- }
- }
- delete mHotResolveData;
- mHotResolveData = NULL;
- return result;
- }
- void BfCompiler::ClearOldHotData()
- {
- if (mHotData == NULL)
- return;
- // TODO: Get rid of old hot data during hot compiles, too
- // if (IsHotCompile())
- // return;
- BP_ZONE("BfCompiler::ClearOldHotData");
- bool isHotCompile = IsHotCompile();
- auto itr = mHotData->mMethodMap.begin();
- while (itr != mHotData->mMethodMap.end())
- {
- String& methodName = itr->mKey;
- auto hotMethod = itr->mValue;
- bool doDelete = false;
- // If a previous version of a method is not currently active then it should be impossible to ever reach it
- while (hotMethod->mPrevVersion != NULL)
- {
- auto prevMethod = hotMethod->mPrevVersion;
- if (prevMethod->mRefCount > 1)
- {
- BF_ASSERT((mHotResolveData != NULL) && (mHotResolveData->mActiveMethods.Contains(prevMethod)));
- break;
- }
- hotMethod->mPrevVersion = prevMethod->mPrevVersion;
- prevMethod->mPrevVersion = NULL;
- prevMethod->Deref();
- }
- BF_ASSERT(hotMethod->mRefCount >= 1);
- if (hotMethod->mPrevVersion == NULL)
- {
- if (hotMethod->mRefCount <= 1)
- {
- doDelete = true;
- }
- else if ((!isHotCompile) && ((hotMethod->mFlags & (BfHotDepDataFlag_IsBound | BfHotDepDataFlag_RetainMethodWithoutBinding)) == 0))
- {
- doDelete = true;
- }
- }
- bool doRemove = doDelete;
- if ((hotMethod->mFlags & BfHotDepDataFlag_HasDup) != 0)
- {
- bool hasDupMethod = false;
- for (int idx = 0; idx < (int)hotMethod->mReferences.size(); idx++)
- {
- auto depData = hotMethod->mReferences[idx];
- if (depData->mDataKind == BfHotDepDataKind_DupMethod)
- {
- auto dupMethod = (BfHotDupMethod*)depData;
- if (doDelete)
- {
- doRemove = false;
- dupMethod->mMethod->mRefCount++;
- itr->mValue = dupMethod->mMethod;
- }
- else
- {
- if ((dupMethod->mMethod->mRefCount == 1) ||
- ((!IsHotCompile()) && (dupMethod->mMethod->mFlags & BfHotDepDataFlag_IsBound) == 0))
- {
- dupMethod->Deref();
- hotMethod->mReferences.RemoveAt(idx);
- idx--;
- }
- }
- }
- }
- }
- if (doDelete)
- {
- BfLogSysM("Deleting hot method %p %s\n", hotMethod, methodName.c_str());
- //BF_ASSERT(hotMethod->mRefCount == 1);
- hotMethod->Clear();
- hotMethod->Deref();
- if (doRemove)
- itr = mHotData->mMethodMap.Remove(itr);
- }
- else
- ++itr;
- }
- mHotData->ClearUnused(IsHotCompile());
- for (auto type : mContext->mResolvedTypes)
- {
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeInst->mHotTypeData == NULL)
- continue;
- bool foundCommittedVersion = false;
- auto latestVersionHead = typeInst->mHotTypeData->GetLatestVersionHead();
- for (int typeIdx = (int)typeInst->mHotTypeData->mTypeVersions.size() - 1; typeIdx >= 0; typeIdx--)
- {
- auto hotVersion = typeInst->mHotTypeData->mTypeVersions[typeIdx];
- if (hotVersion == latestVersionHead)
- {
- // We have to keep the latest version head -- otherwise we would lose vdata and interface mapping data
- continue;
- }
- if ((!foundCommittedVersion) && (mHotState != NULL) && (hotVersion->mDeclHotCompileIdx <= mHotState->mCommittedHotCompileIdx))
- {
- // Don't remove the latest committed version
- foundCommittedVersion = true;
- }
- else if (hotVersion->mRefCount == 1)
- {
- typeInst->mHotTypeData->mTypeVersions.RemoveAt(typeIdx);
- hotVersion->Deref();
- BF_ASSERT(typeInst->mHotTypeData->mTypeVersions.size() > 0);
- }
- }
- }
- }
- void BfCompiler::CompileReified()
- {
- BfLogSysM("BfCompiler::CompileReified\n");
- BP_ZONE("Compile_ResolveTypeDefs");
- Array<BfTypeDef*> deferTypeDefs;
- for (auto typeDef : mSystem->mTypeDefs)
- {
- mSystem->CheckLockYield();
- if (mCanceling)
- {
- BfLogSysM("Canceling from Compile typeDef loop\n");
- break;
- }
- if (typeDef->mProject->mDisabled)
- continue;
- if (typeDef->mIsPartial)
- continue;
- auto scratchModule = mContext->mScratchModule;
- bool isAlwaysInclude = (typeDef->mIsAlwaysInclude) || (typeDef->mProject->mAlwaysIncludeAll);
- auto typeOptions = scratchModule->GetTypeOptions(typeDef);
- if (typeOptions != NULL)
- isAlwaysInclude = typeOptions->Apply(isAlwaysInclude, BfOptionFlags_ReflectAlwaysIncludeType);
- if (typeDef->mProject->IsTestProject())
- {
- for (auto methodDef : typeDef->mMethods)
- {
- auto methodDeclaration = methodDef->GetMethodDeclaration();
- if ((methodDeclaration != NULL) && (methodDeclaration->mAttributes != NULL) &&
- (methodDeclaration->mAttributes->Contains("Test")))
- isAlwaysInclude = true;
- }
- }
- //TODO: Just because the type is required doesn't mean we want to reify it. Why did we have that check?
- if ((mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude) && (!isAlwaysInclude))
- {
- if (typeDef->mGenericParamDefs.IsEmpty())
- deferTypeDefs.Add(typeDef);
- continue;
- }
- scratchModule->ResolveTypeDef(typeDef, BfPopulateType_Full);
- }
- // Resolve remaining typedefs as unreified so we can check their attributes
- for (auto typeDef : deferTypeDefs)
- {
- auto type = mContext->mUnreifiedModule->ResolveTypeDef(typeDef, BfPopulateType_Identity);
- if (type == NULL)
- continue;
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeInst->mIsReified)
- continue;
- mContext->mUnreifiedModule->PopulateType(typeInst, BfPopulateType_Interfaces_Direct);
- if (typeInst->mCustomAttributes == NULL)
- continue;
- bool alwaysInclude = false;
- for (auto& customAttribute : typeInst->mCustomAttributes->mAttributes)
- {
- if (customAttribute.mType->mAttributeData != NULL)
- {
- if (customAttribute.mType->mAttributeData->mAlwaysIncludeUser != 0)
- alwaysInclude = true;
- if ((customAttribute.mType->mAttributeData->mFlags & BfAttributeFlag_AlwaysIncludeTarget) != 0)
- alwaysInclude = true;
- }
- }
- if (alwaysInclude)
- mContext->mScratchModule->PopulateType(typeInst, BfPopulateType_Full);
- }
- PopulateReified();
- }
- bool BfCompiler::DoCompile(const StringImpl& outputDirectory)
- {
- BP_ZONE("BfCompiler::Compile");
- uint32 frontendStartTick = BFTickCount();
- if (mSystem->mTypeDefs.mCount == 0)
- {
- // No-source bailout
- return true;
- }
- if (!mOptions.mErrorString.IsEmpty())
- {
- mPassInstance->Fail(mOptions.mErrorString);
- return false;
- }
- {
- String hotSwapErrors;
- String toolsetErrors;
- for (auto project : mSystem->mProjects)
- {
- project->ClearCache();
- if (project->mDisabled)
- continue;
- if (project->mCodeGenOptions.mLTOType != BfLTOType_None)
- {
- if (mOptions.mAllowHotSwapping)
- {
- if (!hotSwapErrors.IsEmpty())
- hotSwapErrors += ", ";
- hotSwapErrors += project->mName;
- }
- if (mOptions.mToolsetType != BfToolsetType_LLVM)
- {
- if (!toolsetErrors.IsEmpty())
- toolsetErrors += ", ";
- toolsetErrors += project->mName;
- }
- }
- }
- if (!hotSwapErrors.IsEmpty())
- 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()));
- if (!toolsetErrors.IsEmpty())
- 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()));
- }
- //
- {
- String attribName;
- mAttributeTypeOptionMap.Clear();
- for (int typeOptionsIdx = 0; typeOptionsIdx < (int)mSystem->mTypeOptions.size(); typeOptionsIdx++)
- {
- auto& typeOptions = mSystem->mTypeOptions[typeOptionsIdx];
- for (auto& attributeFilter : typeOptions.mAttributeFilters)
- {
- attribName = attributeFilter;
- attribName += "Attribute";
- Array<int>* arrPtr = NULL;
- mAttributeTypeOptionMap.TryAdd(attribName, NULL, &arrPtr);
- arrPtr->Add(typeOptionsIdx);
- }
- }
- }
- mRebuildFileSet.Clear();
- // Inc revision for next run through Compile
- mRevision++;
- mHasComptimeRebuilds = false;
- int revision = mRevision;
- BfLogSysM("Compile Start. Revision: %d. HasParser:%d AutoComplete:%d\n", revision,
- (mResolvePassData != NULL) && (!mResolvePassData->mParsers.IsEmpty()),
- (mResolvePassData != NULL) && (mResolvePassData->mAutoComplete != NULL));
- if (mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
- mContext->mUnreifiedModule->mIsReified = true;
- else
- mContext->mUnreifiedModule->mIsReified = false;
- if (mCeMachine != NULL)
- mCeMachine->CompileStarted();
- if (mOptions.mAllowHotSwapping)
- {
- if (mHotData == NULL)
- {
- mHotData = new HotData();
- mHotData->mCompiler = this;
- }
- }
- else
- {
- delete mHotData;
- mHotData = NULL;
- }
- if (IsHotCompile())
- {
- if (!mOptions.mAllowHotSwapping)
- {
- mPassInstance->Fail("Hot Compilation is not enabled");
- return true;
- }
- if (mHotState == NULL)
- {
- mHotState = new HotState();
- mHotState->mHotProject = mOptions.mHotProject;
- }
- else
- {
- // It should be impossible to switch hot projects without a non-hot compile between them
- BF_ASSERT(mHotState->mHotProject == mOptions.mHotProject);
- }
- }
- else
- {
- for (auto& kv : mContext->mSavedTypeDataMap)
- {
- auto savedTypeData = kv.mValue;
- delete savedTypeData->mHotTypeData;
- savedTypeData->mHotTypeData = NULL;
- }
- delete mHotState;
- mHotState = NULL;
- // This will get rid of any old method data so we don't have any more mPrevVersions
- ClearOldHotData();
- }
- int prevUnfinishedModules = mStats.mModulesStarted - mStats.mModulesFinished;
- mCompletionPct = 0;
- memset(&mStats, 0, sizeof(mStats));
- mCodeGen.ClearResults();
- mCodeGen.ResetStats();
- mStats.mModulesStarted = prevUnfinishedModules;
- if ((mLastRevisionAborted) && (!mIsResolveOnly))
- {
- auto _AddCount = [&](BfModule* module)
- {
- if (module->mAddedToCount)
- {
- if (module->mIsReified)
- mStats.mReifiedModuleCount++;
- }
- };
- for (auto mainModule : mContext->mModules)
- {
- _AddCount(mainModule);
- for (auto specKV : mainModule->mSpecializedMethodModules)
- {
- _AddCount(specKV.mValue);
- }
- }
- }
- if (IsHotCompile())
- {
- mContext->EnsureHotMangledVirtualMethodNames();
- }
- mOutputDirectory = outputDirectory;
- mSystem->StartYieldSection();
- mExtraCompileRequested = false;
- mFastFinish = false;
- mHasQueuedTypeRebuilds = false;
- mCanceling = false;
- mSystem->CheckLockYield();
- #ifdef WANT_COMPILE_LOG
- if (!mIsResolveOnly)
- {
- mCompileLogFP = fopen(StrFormat("compile%d.txt", mRevision).c_str(), "wb");
- }
- #endif
- BfTypeDef* typeDef;
- BfLogSysM("UpdateRevisedTypes Revision %d. ResolvePass:%d CursorIdx:%d\n", mRevision, mIsResolveOnly,
- ((mResolvePassData == NULL) || (mResolvePassData->mParsers.IsEmpty())) ? - 1 : mResolvePassData->mParsers[0]->mCursorIdx);
- mCompileState = CompileState_Normal;
- UpdateRevisedTypes();
- // We need to defer processing the graveyard until here, because mLookupResults contain atom references so we need to make sure
- // those aren't deleted until we can properly handle it.
- mSystem->ProcessAtomGraveyard();
- BpEnter("Compile_Start");
- mHasRequiredTypes = true;
- //HashSet<BfTypeDef*> internalTypeDefs;
-
- BfProject* corlibProject = NULL;
- auto _GetRequiredType = [&](const StringImpl& typeName, int genericArgCount = 0)
- {
- BfTypeDef* ambigiousTypeDef = NULL;
- auto typeDef = mSystem->FindTypeDef(typeName, genericArgCount, NULL, {}, &ambigiousTypeDef);
- if (typeDef == NULL)
- {
- mPassInstance->Fail(StrFormat("Unable to find system type: %s", typeName.c_str()));
- mHasRequiredTypes = false;
- }
- if (ambigiousTypeDef != NULL)
- {
- mPassInstance->Fail(StrFormat("Found multiple declarations of require type '%s'", typeName.c_str()), typeDef->GetRefNode());
- mPassInstance->MoreInfo("See additional declaration", ambigiousTypeDef->GetRefNode());
- if (typeDef->mProject != corlibProject)
- {
- auto rootTypeDef = mSystem->FindTypeDef(typeName, genericArgCount, corlibProject);
- if (rootTypeDef != NULL)
- typeDef = rootTypeDef;
- }
- }
- if (corlibProject == NULL)
- corlibProject = typeDef->mProject;
- return typeDef;
- };
- _GetRequiredType("System.Void");
- _GetRequiredType("System.Boolean");
- _GetRequiredType("System.Int");
- _GetRequiredType("System.Int8");
- _GetRequiredType("System.Int16");
- _GetRequiredType("System.Int32");
- _GetRequiredType("System.Int64");
- _GetRequiredType("System.UInt");
- _GetRequiredType("System.UInt8");
- _GetRequiredType("System.UInt16");
- _GetRequiredType("System.UInt32");
- _GetRequiredType("System.UInt64");
- _GetRequiredType("System.Char8");
- _GetRequiredType("System.Char16");
- mChar32TypeDef = _GetRequiredType("System.Char32");
- mFloatTypeDef = _GetRequiredType("System.Float");
- mDoubleTypeDef = _GetRequiredType("System.Double");
- mMathTypeDef = _GetRequiredType("System.Math");
- mBfObjectTypeDef = _GetRequiredType("System.Object");
- mArray1TypeDef = _GetRequiredType("System.Array1", 1);
- mArray2TypeDef = _GetRequiredType("System.Array2", 1);
- mArray3TypeDef = _GetRequiredType("System.Array3", 1);
- mArray4TypeDef = _GetRequiredType("System.Array4", 1);
- mSpanTypeDef = _GetRequiredType("System.Span", 1);
- mRangeTypeDef = _GetRequiredType("System.Range");
- mClosedRangeTypeDef = _GetRequiredType("System.ClosedRange");
- mIndexTypeDef = _GetRequiredType("System.Index");
- mIndexRangeTypeDef = _GetRequiredType("System.IndexRange");
- mAttributeTypeDef = _GetRequiredType("System.Attribute");
- mAttributeUsageAttributeTypeDef = _GetRequiredType("System.AttributeUsageAttribute");
- mClassVDataTypeDef = _GetRequiredType("System.ClassVData");
- mCLinkAttributeTypeDef = _GetRequiredType("System.CLinkAttribute");
- mImportAttributeTypeDef = _GetRequiredType("System.ImportAttribute");
- mExportAttributeTypeDef = _GetRequiredType("System.ExportAttribute");
- mCReprAttributeTypeDef = _GetRequiredType("System.CReprAttribute");
- mUnderlyingArrayAttributeTypeDef = _GetRequiredType("System.UnderlyingArrayAttribute");
- mAlignAttributeTypeDef = _GetRequiredType("System.AlignAttribute");
- mAllowDuplicatesAttributeTypeDef = _GetRequiredType("System.AllowDuplicatesAttribute");
- mNoDiscardAttributeTypeDef = _GetRequiredType("System.NoDiscardAttribute");
- mDisableChecksAttributeTypeDef = _GetRequiredType("System.DisableChecksAttribute");
- mDisableObjectAccessChecksAttributeTypeDef = _GetRequiredType("System.DisableObjectAccessChecksAttribute");
- mDbgRawAllocDataTypeDef = _GetRequiredType("System.DbgRawAllocData");
- mDeferredCallTypeDef = _GetRequiredType("System.DeferredCall");
- mDelegateTypeDef = _GetRequiredType("System.Delegate");
- mFunctionTypeDef = _GetRequiredType("System.Function");
- mActionTypeDef = _GetRequiredType("System.Action");
- mEnumTypeDef = _GetRequiredType("System.Enum");
- mFriendAttributeTypeDef = _GetRequiredType("System.FriendAttribute");
- mNoStaticCtorAttributeTypeDef = _GetRequiredType("System.NoStaticCtorAttribute");
- mComptimeAttributeTypeDef = _GetRequiredType("System.ComptimeAttribute");
- mIntrinsicAttributeTypeDef = _GetRequiredType("System.IntrinsicAttribute");
- mConstEvalAttributeTypeDef = _GetRequiredType("System.ConstEvalAttribute");
- mNoExtensionAttributeTypeDef = _GetRequiredType("System.NoExtensionAttribute");
- mCheckedAttributeTypeDef = _GetRequiredType("System.CheckedAttribute");
- mUncheckedAttributeTypeDef = _GetRequiredType("System.UncheckedAttribute");
- mResultTypeDef = _GetRequiredType("System.Result", 1);
- mGCTypeDef = _GetRequiredType("System.GC");
- mGenericIEnumerableTypeDef = _GetRequiredType("System.Collections.IEnumerable", 1);
- mGenericIEnumeratorTypeDef = _GetRequiredType("System.Collections.IEnumerator", 1);
- mGenericIRefEnumeratorTypeDef = _GetRequiredType("System.Collections.IRefEnumerator", 1);
- mInlineAttributeTypeDef = _GetRequiredType("System.InlineAttribute");
- mThreadTypeDef = _GetRequiredType("System.Threading.Thread");
- mInternalTypeDef = _GetRequiredType("System.Internal");
- mPlatformTypeDef = _GetRequiredType("System.Platform");
- mCompilerTypeDef = _GetRequiredType("System.Compiler");
- mCompilerGeneratorTypeDef = _GetRequiredType("System.Compiler.Generator");
- mDiagnosticsDebugTypeDef = _GetRequiredType("System.Diagnostics.Debug");
- mIDisposableTypeDef = _GetRequiredType("System.IDisposable");
- mIIntegerTypeDef = _GetRequiredType("System.IInteger");
- mIPrintableTypeDef = _GetRequiredType("System.IPrintable");
- mIHashableTypeDef = _GetRequiredType("System.IHashable");
- mIComptimeTypeApply = _GetRequiredType("System.IComptimeTypeApply");
- mIComptimeMethodApply = _GetRequiredType("System.IComptimeMethodApply");
- mIOnTypeInitTypeDef = _GetRequiredType("System.IOnTypeInit");
- mIOnTypeDoneTypeDef = _GetRequiredType("System.IOnTypeDone");
- mIOnFieldInitTypeDef = _GetRequiredType("System.IOnFieldInit");
- mIOnMethodInitTypeDef = _GetRequiredType("System.IOnMethodInit");
- mLinkNameAttributeTypeDef = _GetRequiredType("System.LinkNameAttribute");
- mCallingConventionAttributeTypeDef = _GetRequiredType("System.CallingConventionAttribute");
- mMethodRefTypeDef = _GetRequiredType("System.MethodReference", 1);
- mNullableTypeDef = _GetRequiredType("System.Nullable", 1);
- mOrderedAttributeTypeDef = _GetRequiredType("System.OrderedAttribute");
- mPointerTTypeDef = _GetRequiredType("System.Pointer", 1);
- mPointerTypeDef = _GetRequiredType("System.Pointer", 0);
- mReflectTypeIdTypeDef = _GetRequiredType("System.Reflection.TypeId");
- mReflectArrayType = _GetRequiredType("System.Reflection.ArrayType");
- mReflectGenericParamType = _GetRequiredType("System.Reflection.GenericParamType");
- mReflectFieldDataDef = _GetRequiredType("System.Reflection.TypeInstance.FieldData");
- mReflectFieldSplatDataDef = _GetRequiredType("System.Reflection.TypeInstance.FieldSplatData");
- mReflectMethodDataDef = _GetRequiredType("System.Reflection.TypeInstance.MethodData");
- mReflectParamDataDef = _GetRequiredType("System.Reflection.TypeInstance.ParamData");
- mReflectInterfaceDataDef = _GetRequiredType("System.Reflection.TypeInstance.InterfaceData");
- mReflectPointerType = _GetRequiredType("System.Reflection.PointerType");
- mReflectRefType = _GetRequiredType("System.Reflection.RefType");
- mReflectSizedArrayType = _GetRequiredType("System.Reflection.SizedArrayType");
- mReflectConstExprType = _GetRequiredType("System.Reflection.ConstExprType");
- mReflectSpecializedGenericType = _GetRequiredType("System.Reflection.SpecializedGenericType");
- mReflectTypeInstanceTypeDef = _GetRequiredType("System.Reflection.TypeInstance");
- mReflectUnspecializedGenericType = _GetRequiredType("System.Reflection.UnspecializedGenericType");
- mReflectFieldInfoTypeDef = _GetRequiredType("System.Reflection.FieldInfo");
- mReflectMethodInfoTypeDef = _GetRequiredType("System.Reflection.MethodInfo");
- mSizedArrayTypeDef = _GetRequiredType("System.SizedArray", 2);
- mStaticInitAfterAttributeTypeDef = _GetRequiredType("System.StaticInitAfterAttribute");
- mStaticInitPriorityAttributeTypeDef = _GetRequiredType("System.StaticInitPriorityAttribute");
- mStringTypeDef = _GetRequiredType("System.String");
- mStringViewTypeDef = _GetRequiredType("System.StringView");
- mTestAttributeTypeDef = _GetRequiredType("System.TestAttribute");
- mThreadStaticAttributeTypeDef = _GetRequiredType("System.ThreadStaticAttribute");
- mTypeTypeDeclDef = _GetRequiredType("System.TypeDeclaration");
- mTypeTypeDef = _GetRequiredType("System.Type");
- mUnboundAttributeTypeDef = _GetRequiredType("System.UnboundAttribute");
- mValueTypeTypeDef = _GetRequiredType("System.ValueType");
- mTupleTypeDef = _GetRequiredType("System.Tuple");
- mObsoleteAttributeTypeDef = _GetRequiredType("System.ObsoleteAttribute");
- mErrorAttributeTypeDef = _GetRequiredType("System.ErrorAttribute");
- mWarnAttributeTypeDef = _GetRequiredType("System.WarnAttribute");
- mConstSkipAttributeTypeDef = _GetRequiredType("System.ConstSkipAttribute");
- mIgnoreErrorsAttributeTypeDef = _GetRequiredType("System.IgnoreErrorsAttribute");
- mReflectAttributeTypeDef = _GetRequiredType("System.ReflectAttribute");
- mOnCompileAttributeTypeDef = _GetRequiredType("System.OnCompileAttribute");
- for (int i = 0; i < BfTypeCode_Length; i++)
- mContext->mPrimitiveStructTypes[i] = NULL;
- mContext->mBfTypeType = NULL;
- mContext->mBfClassVDataPtrType = NULL;
- if (!mHasRequiredTypes)
- {
- // Force rebuilding
- BfLogSysM("Compile missing required types\n");
- mOptions.mForceRebuildIdx++;
- }
- mSystem->CheckLockYield();
- if (mBfObjectTypeDef != NULL)
- mContext->mScratchModule->ResolveTypeDef(mBfObjectTypeDef);
- VisitSourceExteriorNodes();
- if (!mIsResolveOnly)
- {
- HashSet<BfModule*> foundVDataModuleSet;
- for (auto bfProject : mSystem->mProjects)
- {
- if (bfProject->mDisabled)
- continue;
- if ((mBfObjectTypeDef != NULL) && (!bfProject->ContainsReference(mBfObjectTypeDef->mProject)))
- {
- mPassInstance->Fail(StrFormat("Project '%s' must reference core library '%s'", bfProject->mName.c_str(), mBfObjectTypeDef->mProject->mName.c_str()));
- }
- if ((bfProject->mTargetType != BfTargetType_BeefConsoleApplication) && (bfProject->mTargetType != BfTargetType_BeefWindowsApplication) &&
- (bfProject->mTargetType != BfTargetType_BeefLib_DynamicLib) && (bfProject->mTargetType != BfTargetType_BeefLib_StaticLib) &&
- (bfProject->mTargetType != BfTargetType_C_ConsoleApplication) && (bfProject->mTargetType != BfTargetType_C_WindowsApplication) &&
- (bfProject->mTargetType != BfTargetType_BeefTest) &&
- (bfProject->mTargetType != BfTargetType_BeefApplication_StaticLib) && (bfProject->mTargetType != BfTargetType_BeefApplication_DynamicLib))
- continue;
- if (bfProject->mTargetType == BfTargetType_BeefTest)
- {
- // Force internal test methods
- auto bfModule = mContext->mScratchModule;
- bfModule->GetInternalMethod("Test_Init");
- bfModule->GetInternalMethod("Test_Query");
- bfModule->GetInternalMethod("Test_Finish");
- }
- bool found = false;
- for (auto module : mVDataModules)
- {
- if (module->mProject == bfProject)
- {
- found = true;
- foundVDataModuleSet.Add(module);
- //module->StartNewRevision();
- }
- }
- if (!found)
- {
- auto module = new BfVDataModule(mContext);
- module->mProject = bfProject;
- module->Init();
- module->FinishInit();
- module->mIsSpecialModule = true;
- BF_ASSERT(!mContext->mLockModules);
- mContext->mModules.push_back(module);
- mVDataModules.push_back(module);
- foundVDataModuleSet.Add(module);
- }
- }
- // Remove old vdata
- for (int moduleIdx = 0; moduleIdx < (int) mVDataModules.size(); moduleIdx++)
- {
- auto module = mVDataModules[moduleIdx];
- if (!foundVDataModuleSet.Contains(module))
- {
- delete module;
- mVDataModules.erase(mVDataModules.begin() + moduleIdx);
- moduleIdx--;
- mContext->mModules.Remove(module);
- }
- }
- }
- if (mIsResolveOnly)
- VisitAutocompleteExteriorIdentifiers();
- mStats.mTypesQueued = 0;
- mStats.mMethodsQueued = 0;
- mStats.mTypesQueued += (int)mContext->mPopulateTypeWorkList.size();
- mStats.mMethodsQueued += (int)mContext->mMethodWorkList.size();
- while (true)
- {
- //
- {
- if (mBfObjectTypeDef != NULL)
- mContext->mScratchModule->ResolveTypeDef(mBfObjectTypeDef, BfPopulateType_Full);
- mContext->RemapObject();
- mSystem->CheckLockYield();
- mWantsDeferMethodDecls = mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude;
- CompileReified();
- mWantsDeferMethodDecls = false;
- }
- BpLeave();
- BpEnter("Compile_End");
- mContext->mHasReifiedQueuedRebuildTypes = false;
- //
- {
- BP_ZONE("ProcessingLiveness");
- for (auto type : mContext->mResolvedTypes)
- {
- auto depType = type->ToDependedType();
- if (depType != NULL)
- depType->mRebuildFlags = (BfTypeRebuildFlags)(depType->mRebuildFlags | BfTypeRebuildFlag_AwaitingReference);
- }
- bool didWork = false;
- UpdateDependencyMap(mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_ResolveUnused, didWork);
- // If UpdateDependencyMap caused methods to be reified, then we need to run PopulateReified again-
- // because those methods may be virtual and we need to reify overrides (for example).
- // We use the DoWorkLoop result to determine if there were actually any changes from UpdateDependencyMap
- if (didWork)
- {
- PopulateReified();
- }
- }
- if (!mContext->mHasReifiedQueuedRebuildTypes)
- break;
- BfLogSysM("DoCompile looping over CompileReified due to mHasReifiedQueuedRebuildTypes\n");
- }
- // Handle purgatory (ie: old generic types)
- {
- bool didWork = ProcessPurgatory(true);
- if (mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude)
- {
- if (DoWorkLoop())
- didWork = true;
- if (didWork)
- PopulateReified();
- }
- }
- // Mark used modules
- if ((mOptions.mCompileOnDemandKind != BfCompileOnDemandKind_AlwaysInclude) && (!mCanceling))
- {
- bool hadActualTarget = false;
- if (!mIsResolveOnly)
- {
- SizedArray<BfModule*, 32> requiredModules;
- for (auto type : mContext->mResolvedTypes)
- {
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeInst->mAlwaysIncludeFlags == BfAlwaysIncludeFlag_None)
- continue;
- if (typeInst->IsGenericTypeInstance())
- {
- if ((!typeInst->IsUnspecializedType()) || (typeInst->IsUnspecializedTypeVariation()))
- continue;
- }
- auto requiredModule = typeInst->GetModule();
- if (requiredModule != NULL)
- requiredModules.push_back(requiredModule);
- }
- mContext->mReferencedIFaceSlots.Clear();
- bool hasTests = false;
- for (auto project : mSystem->mProjects)
- {
- if (project->mTargetType == BfTargetType_BeefTest)
- hasTests = true;
- project->mUsedModules.Clear();
- project->mReferencedTypeData.Clear();
- if (project->mDisabled)
- continue;
- if (project->mTargetType == BfTargetType_BeefLib)
- continue;
- hadActualTarget = true;
- for (auto requiredModule : requiredModules)
- {
- mContext->MarkUsedModules(project, requiredModule);
- }
- String entryClassName = project->mStartupObject;
- typeDef = mSystem->FindTypeDef(entryClassName, 0, project, {}, NULL, BfFindTypeDefFlag_AllowGlobal);
- if (typeDef != NULL)
- {
- auto startupType = mContext->mScratchModule->ResolveTypeDef(typeDef);
- if (startupType != NULL)
- {
- auto startupTypeInst = startupType->ToTypeInstance();
- if (startupTypeInst != NULL)
- {
- mContext->MarkUsedModules(project, startupTypeInst->GetModule());
- }
- }
- }
- if (hasTests)
- {
- HashSet<BfProject*> projectSet;
- for (auto type : mContext->mResolvedTypes)
- {
- auto typeInstance = type->ToTypeInstance();
- if (typeInstance != NULL)
- {
- for (auto& methodInstanceGroup : typeInstance->mMethodInstanceGroups)
- {
- if (methodInstanceGroup.mDefault != NULL)
- {
- auto methodInstance = methodInstanceGroup.mDefault;
- auto project = methodInstance->mMethodDef->mDeclaringType->mProject;
- if (project->mTargetType != BfTargetType_BeefTest)
- continue;
- if ((methodInstance->GetCustomAttributes() != NULL) &&
- (methodInstance->GetCustomAttributes()->Contains(mTestAttributeTypeDef)))
- {
- projectSet.Add(project);
- }
- }
- }
- if (!projectSet.IsEmpty())
- {
- for (auto project : projectSet)
- mContext->MarkUsedModules(project, typeInstance->mModule);
- projectSet.Clear();
- }
- }
- }
- }
- }
- // Leave types reified when hot compiling
- if ((!IsHotCompile()) && (hadActualTarget))
- mContext->TryUnreifyModules();
- }
- }
- // Generate slot nums
- if ((!mIsResolveOnly) && (!mCanceling))
- {
- if ((!IsHotCompile()) || (mHotState->mHasNewInterfaceTypes))
- {
- int prevSlotCount = mMaxInterfaceSlots;
- GenerateSlotNums();
- if ((prevSlotCount != -1) && (prevSlotCount != mMaxInterfaceSlots))
- {
- mInterfaceSlotCountChanged = true;
- }
- if (mHotState != NULL)
- mHotState->mHasNewInterfaceTypes = false;
- }
- }
- // Resolve unused types
- if ((mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_ResolveUnused) && (!mCanceling))
- {
- // Finish off any outstanding modules so we can code generate in parallel with handling the unreified stuff
- for (auto module : mContext->mModules)
- {
- if (!module->mIsSpecialModule)
- {
- if ((module->HasCompiledOutput()) && (module->mIsModuleMutable))
- {
- module->Finish();
- }
- }
- }
- DoWorkLoop();
- BfLogSysM("Compile QueueUnused\n");
- mCompileState = BfCompiler::CompileState_Unreified;
- BpLeave();
- BpEnter("Compile_QueueUnused");
- while (true)
- {
- BP_ZONE("Compile_QueueUnused");
- bool queuedMoreMethods = false;
- int startTypeInitCount = mTypeInitCount;
- for (auto typeDef : mSystem->mTypeDefs)
- {
- mSystem->CheckLockYield();
- if (mCanceling)
- {
- BfLogSysM("Canceling from Compile typeDef loop\n");
- break;
- }
- if (typeDef->mProject->mDisabled)
- continue;
- if (typeDef->mIsPartial)
- continue;
- if (typeDef->mTypeCode == BfTypeCode_Extension)
- continue;
- mContext->mUnreifiedModule->ResolveTypeDef(typeDef, BfPopulateType_Full);
- }
- Array<BfTypeInstance*> typeWorkList;
- Array<BfType*> typeList;
- typeList.Reserve(mContext->mResolvedTypes.GetCount());
- for (auto type : mContext->mResolvedTypes)
- typeList.Add(type);
- for (auto type : typeList)
- {
- auto module = type->GetModule();
- if (module == NULL)
- continue;
- if ((type->IsIncomplete()) && (type->IsTypeInstance()) && (!type->IsSpecializedType()))
- {
- mSystem->CheckLockYield();
- module->PopulateType(type, BfPopulateType_Full);
- }
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeInst->IsUnspecializedTypeVariation())
- continue;
- if (!typeInst->IsSpecializedType())
- {
- typeWorkList.Add(typeInst);
- }
- }
- for (auto typeInst : typeWorkList)
- {
- // Find any remaining methods for unreified processing
- for (auto&& methodInstGroup : typeInst->mMethodInstanceGroups)
- {
- if ((methodInstGroup.mOnDemandKind == BfMethodOnDemandKind_Decl_AwaitingReference) ||
- (methodInstGroup.mOnDemandKind == BfMethodOnDemandKind_NoDecl_AwaitingReference))
- {
- if ((methodInstGroup.mDefault != NULL) && (methodInstGroup.mDefault->mIsForeignMethodDef))
- {
- mContext->mUnreifiedModule->GetMethodInstance(typeInst, methodInstGroup.mDefault->mMethodDef, BfTypeVector(),
- (BfGetMethodInstanceFlags)(BfGetMethodInstanceFlag_ForeignMethodDef | BfGetMethodInstanceFlag_UnspecializedPass | BfGetMethodInstanceFlag_ExplicitResolveOnlyPass));
- queuedMoreMethods = true;
- }
- else
- {
- auto methodDef = typeInst->mTypeDef->mMethods[methodInstGroup.mMethodIdx];
- if (methodDef->mMethodType == BfMethodType_Init)
- continue;
- mContext->mUnreifiedModule->GetMethodInstance(typeInst, methodDef, BfTypeVector(),
- (BfGetMethodInstanceFlags)(BfGetMethodInstanceFlag_UnspecializedPass | BfGetMethodInstanceFlag_ExplicitResolveOnlyPass));
- queuedMoreMethods = true;
- }
- }
- }
- }
- if ((!queuedMoreMethods) && (startTypeInitCount == mTypeInitCount))
- break;
- DoWorkLoop();
- }
- bool didWork = false;
- UpdateDependencyMap(true, didWork);
- bool hadReifiedRebuild = false;
- for (auto entry : mContext->mPopulateTypeWorkList)
- {
- if ((entry != NULL) && (entry->mType->IsReified()))
- hadReifiedRebuild = true;
- }
- // Deleting types can cause reified types to rebuild, so allow that
- mCompileState = BfCompiler::CompileState_Normal;
- DoWorkLoop();
- if ((hadReifiedRebuild) && (!mIsResolveOnly))
- CompileReified();
- }
- else
- {
- DoWorkLoop();
- }
- ProcessPurgatory(false);
- // ProcessPurgatory MAY cause type rebuilds which we need to handle
- DoWorkLoop();
- BfLogSysM("Checking mDepsMayHaveDeletedTypes for SanitizeDependencyMap\n");
- if (mDepsMayHaveDeletedTypes)
- SanitizeDependencyMap();
- // Old Mark used modules
- if (!mIsResolveOnly)
- {
- // if ((!mPassInstance->HasFailed()) && (!mCanceling))
- // {
- // if ((!IsHotCompile()) || (mHotState->mHasNewInterfaceTypes))
- // {
- // GenerateSlotNums();
- // if (mHotState != NULL)
- // mHotState->mHasNewInterfaceTypes = false;
- // }
- // }
- if ((!mPassInstance->HasFailed()) && (!mCanceling))
- {
- if (!mOptions.mAllowHotSwapping)
- {
- GenerateDynCastData();
- mContext->ProcessWorkList(false, false);
- }
- mCompileState = BfCompiler::CompileState_VData;
- for (auto vdataModule : mVDataModules)
- CreateVData(vdataModule);
- for (auto vdataModule : mVDataModules)
- FixVDataHash(vdataModule);
- mCompileState = BfCompiler::CompileState_Normal;
- }
- // Don't clear out unused string pool entries while we are hot swapping, because we want string literals
- // to still be the same pointer if it's erased and then put back
- if ((!IsHotCompile()) && (!mCanceling))
- ClearUnusedStringPoolEntries();
- mContext->ValidateDependencies();
- mContext->UpdateAfterDeletingTypes();
- }
- // We need to check the specialized errors before writing out modules --
- // this call is responsible for deleting dead method specializations that contained errors, or for setting
- // the mHadBuildErrors on the module if there was a method specialization error that didn't die
- mContext->CheckSpecializedErrorData();
- mContext->Finish();
- if ((!mIsResolveOnly) && (!IsHotCompile()))
- ClearOldHotData();
- mPassInstance->TryFlushDeferredError();
- BpLeave();
- BpEnter("Compile_Finish");
- int frontendTicks = (int)(BFTickCount() - frontendStartTick);
- //TODO:!!
- //mCanceling = true;
- String moduleListStr;
- int numModulesWritten = 0;
- if (!mCanceling)
- {
- if (!mIsResolveOnly)
- {
- int idx = 0;
- BF_ASSERT(mContext->mMethodWorkList.IsEmpty());
- //bfContext->mLockModules = true;
- for (int moduleIdx = 0; moduleIdx < (int)mContext->mModules.size(); moduleIdx++)
- {
- //bool clearModule = false;
- auto mainModule = mContext->mModules[moduleIdx];
- BfModule* bfModule = mainModule;
- if (bfModule->mIsReified)
- {
- auto itr = mainModule->mSpecializedMethodModules.begin();
- while (true)
- {
- if (bfModule->mIsModuleMutable)
- {
- //clearModule = true;
- // Note that Finish will just return immediately if we have errors, we don't write out modules with errors
- // The 'mLastModuleWrittenRevision' will not be updated in the case.
- bfModule->Finish();
- mainModule->mRevision = std::max(mainModule->mRevision, bfModule->mRevision);
- }
- if (bfModule->mLastModuleWrittenRevision == mRevision)
- {
- if (!moduleListStr.empty())
- moduleListStr += ", ";
- moduleListStr += bfModule->mModuleName;
- numModulesWritten++;
- }
- if (bfModule->mParentModule != NULL)
- {
- for (auto& fileName : bfModule->mOutFileNames)
- {
- if (!mainModule->mOutFileNames.Contains(fileName))
- mainModule->mOutFileNames.push_back(fileName);
- }
- }
- if (bfModule->mNextAltModule != NULL)
- {
- bfModule = bfModule->mNextAltModule;
- }
- else
- {
- if (itr == mainModule->mSpecializedMethodModules.end())
- break;
- bfModule = itr->mValue;
- ++itr;
- }
- }
- }
- mainModule->ClearModule();
- }
- //bfContext->mLockModules = false;
- }
- else
- {
- bool isTargeted = (mResolvePassData != NULL) && (!mResolvePassData->mParsers.IsEmpty());
- if (!isTargeted)
- {
- for (auto bfModule : mContext->mModules)
- {
- if (bfModule->mIsModuleMutable)
- {
- bfModule->Finish();
- bfModule->mRevision = std::max(bfModule->mRevision, bfModule->mRevision);
- bfModule->ClearModuleData();
- }
- }
- }
- }
- }
- /*if (!moduleListStr.empty())
- mPassInstance->OutputLine(StrFormat("%d modules generated: %s", numModulesWritten, moduleListStr.c_str()));*/
- //CompileLog("%d object files written: %s\n", numModulesWritten, moduleListStr.c_str());
- //printf("Compile done, waiting for finish\n");
- while (true)
- {
- if (mCanceling)
- mCodeGen.Cancel();
- bool isDone = mCodeGen.Finish();
- UpdateCompletion();
- if (isDone)
- break;
- }
- mCodeGen.ProcessErrors(mPassInstance, mCanceling);
- mCeMachine->CompileDone();
- // This has to happen after codegen because we may delete modules that are referenced in codegen
- mContext->Cleanup();
- if ((!IsHotCompile()) && (!mIsResolveOnly) && (!mCanceling))
- {
- // Only save 'saved type data' for temporarily-deleted types like on-demand types.
- // If we don't reuse it within a compilation pass then we put those IDs up to be
- // reused later. We don't do this for hot reloading because there are cases like
- // a user renaming a type that we want to allow him to be able to undo and then
- // hot-recompile successfully.
- for (auto& kv : mContext->mSavedTypeDataMap)
- {
- auto savedTypeData = kv.mValue;
- mTypeIdFreeList.Add(savedTypeData->mTypeId);
- delete savedTypeData;
- }
- mContext->mSavedTypeDataMap.Clear();
- mContext->mSavedTypeData.Clear();
- }
- #ifdef BF_PLATFORM_WINDOWS
- if (!mIsResolveOnly)
- {
- if (!IsHotCompile())
- {
- // Remove individually-written object files from any libs that previously had them,
- // in the case that lib settings changed (ie: switching a module from Og+ to O0)
- for (auto mainModule : mContext->mModules)
- {
- BfModule* bfModule = mainModule;
- if (bfModule->mIsReified)
- {
- for (auto& outFileName : bfModule->mOutFileNames)
- {
- if (outFileName.mModuleWritten)
- BeLibManager::Get()->AddUsedFileName(outFileName.mFileName);
- }
- }
- }
- }
- auto libManager = BeLibManager::Get();
- libManager->Finish();
- if (!libManager->mErrors.IsEmpty())
- {
- for (auto& error : libManager->mErrors)
- mPassInstance->Fail(error);
- // We need to rebuild everything just to force that lib to get repopulated
- mOptions.mForceRebuildIdx++;
- }
- libManager->mErrors.Clear();
- }
- #endif
- int numObjFilesWritten = 0;
- for (auto& fileEntry : mCodeGen.mCodeGenFiles)
- {
- if (!fileEntry.mWasCached)
- numObjFilesWritten++;
- }
- mPassInstance->OutputLine(StrFormat(":low %d module%s built, %d object file%s generated",
- numModulesWritten, (numModulesWritten != 1) ? "s" : "",
- numObjFilesWritten, (numObjFilesWritten != 1) ? "s" : ""));
- if (!mIsResolveOnly)
- {
- mPassInstance->OutputLine(StrFormat(":med Frontend time: %0.2fs", frontendTicks / 1000.0f));
- if ((mCeMachine != NULL) && (mCeMachine->mRevisionExecuteTime > 0))
- {
- mPassInstance->OutputLine(StrFormat(":med Comptime execution time: %0.2fs", mCeMachine->mRevisionExecuteTime / 1000.0f));
- }
- }
- BpLeave();
- mPassInstance->WriteErrorSummary();
- if ((mCanceling) && (!mIsResolveOnly))
- {
- mPassInstance->Fail("Build canceled");
- mContext->CancelWorkItems();
- CompileLog("Compile canceled\n");
- }
- BfLogSysM("Compile Done. Revision:%d TypesPopulated:%d MethodsDeclared:%d MethodsProcessed:%d Canceled? %d\n", revision, mStats.mTypesPopulated, mStats.mMethodDeclarations, mStats.mMethodsProcessed, mCanceling);
- UpdateCompletion();
- if ((!mIsResolveOnly) && (!mPassInstance->HasFailed()) && (!mCanceling))
- {
- //BF_ASSERT(mCompletionPct >= 0.99999f);
- }
- if (mCompileLogFP != NULL)
- {
- fclose(mCompileLogFP);
- mCompileLogFP = NULL;
- }
- UpdateCompletion();
- mStats.mTotalTypes = mContext->mResolvedTypes.GetCount();
- String compileInfo;
- if (mIsResolveOnly)
- compileInfo += StrFormat("ResolveOnly ResolveType:%d Parser:%d\n", mResolvePassData->mResolveType, !mResolvePassData->mParsers.IsEmpty());
- compileInfo += StrFormat("TotalTypes:%d\nTypesPopulated:%d\nMethodsDeclared:%d\nMethodsProcessed:%d\nCanceled? %d\n", mStats.mTotalTypes, mStats.mTypesPopulated, mStats.mMethodDeclarations, mStats.mMethodsProcessed, mCanceling);
- compileInfo += StrFormat("TypesPopulated:%d\n", mStats.mTypesPopulated);
- compileInfo += StrFormat("MethodDecls:%d\nMethodsProcessed:%d\nModulesStarted:%d\nModulesFinished:%d\n", mStats.mMethodDeclarations, mStats.mMethodsProcessed, mStats.mModulesFinished);
- BpEvent("CompileDone", compileInfo.c_str());
- if (mHotState != NULL)
- {
- for (auto& fileEntry : mCodeGen.mCodeGenFiles)
- {
- if (fileEntry.mWasCached)
- continue;
- mHotState->mQueuedOutFiles.Add(fileEntry);
- }
- if (!mPassInstance->HasFailed())
- {
- // Clear these out when we know we've compiled without error
- mHotState->mNewlySlottedTypeIds.Clear();
- mHotState->mSlotDefineTypeIds.Clear();
- }
- }
- mCompileState = BfCompiler::CompileState_None;
- // extern MemReporter gBEMemReporter;
- // extern int gBEMemReporterSize;
- // gBEMemReporter.Report();
- // int memReporterSize = gBEMemReporterSize;
- mLastRevisionAborted = mCanceling;
- bool didCancel = mCanceling;
- mCanceling = false;
- mContext->ValidateDependencies();
- if (mNeedsFullRefresh)
- {
- mNeedsFullRefresh = false;
- return false;
- }
- if (didCancel)
- mLastHadComptimeRebuilds = mHasComptimeRebuilds || mLastHadComptimeRebuilds;
- else
- mLastHadComptimeRebuilds = mHasComptimeRebuilds;
- return !didCancel && !mHasQueuedTypeRebuilds;
- }
- bool BfCompiler::Compile(const StringImpl& outputDirectory)
- {
- int passIdx = 0;
- bool success = false;
- while (true)
- {
- auto passState = mPassInstance->GetState();
- success = DoCompile(outputDirectory);
- if (!mExtraCompileRequested)
- break;
-
- mPassInstance->RestoreState(passState);
- if (passIdx == 1)
- break;
- passIdx++;
- }
- if (!success)
- return false;
- if (mPassInstance->HasFailed())
- return true;
- if (!mInterfaceSlotCountChanged)
- return true;
- BfLogSysM("Interface slot count increased. Rebuilding relevant modules.\n");
- mPassInstance->OutputLine("Interface slot count increased. Rebuilding relevant modules.");
- // Recompile with the increased slot count
- success = DoCompile(outputDirectory);
- BF_ASSERT(!mInterfaceSlotCountChanged);
- return success;
- }
- void BfCompiler::ClearResults()
- {
- BP_ZONE("BfCompiler::ClearResults");
- mCodeGen.ClearResults();
- }
- // Can should still leave the system in a state such that we when we save as much progress as possible while
- // still leaving the system in a state that the next attempt at compile will resume with a valid state
- // Canceling will still process the pending PopulateType calls but may leave items in the method worklist.
- // Note that Cancel is an async request to cancel
- void BfCompiler::Cancel()
- {
- mCanceling = true;
- mFastFinish = true;
- mHadCancel = true;
- if (mCeMachine != NULL)
- {
- AutoCrit autoCrit(mCeMachine->mCritSect);
- mCeMachine->mSpecialCheck = true;
- mFastFinish = true;
- }
- BfLogSysM("BfCompiler::Cancel\n");
- BpEvent("BfCompiler::Cancel", "");
- }
- void BfCompiler::RequestFastFinish()
- {
- mFastFinish = true;
- if (mCeMachine != NULL)
- mCeMachine->mSpecialCheck = true;
- BfLogSysM("BfCompiler::RequestFastFinish\n");
- BpEvent("BfCompiler::RequestFastFinish", "");
- }
- void BfCompiler::RequestExtraCompile()
- {
- mExtraCompileRequested = true;
- }
- //#define WANT_COMPILE_LOG
- void BfCompiler::CompileLog(const char* fmt ...)
- {
- #ifdef WANT_COMPILE_LOG
- if (mCompileLogFP == NULL)
- return;
- //static int lineNum = 0;
- //lineNum++;
- va_list argList;
- va_start(argList, fmt);
- String aResult = vformat(fmt, argList);
- va_end(argList);
- //aResult = StrFormat("%d ", lineNum) + aResult;
- fwrite(aResult.c_str(), 1, aResult.length(), mCompileLogFP);
- #endif
- }
- void BfCompiler::ReportMemory(MemReporter* memReporter)
- {
- AutoCrit crit(mSystem->mDataLock);
- {
- AutoMemReporter autoMemReporter(memReporter, "Context");
- mContext->ReportMemory(memReporter);
- }
- for (auto type : mContext->mResolvedTypes)
- {
- AutoMemReporter autoMemReporter(memReporter, "Types");
- type->ReportMemory(memReporter);
- }
- for (auto module : mContext->mModules)
- {
- AutoMemReporter autoMemReporter(memReporter, "Modules");
- module->ReportMemory(memReporter);
- }
- {
- AutoMemReporter autoMemReporter(memReporter, "ScratchModule");
- mContext->mScratchModule->ReportMemory(memReporter);
- }
- for (auto vdataModule : mVDataModules)
- {
- AutoMemReporter autoMemReporter(memReporter, "VDataModules");
- vdataModule->ReportMemory(memReporter);
- }
- if (mHotData != NULL)
- {
- AutoMemReporter autoMemReporter(memReporter, "HotData");
- memReporter->Add(sizeof(HotData));
- memReporter->AddMap(mHotData->mMethodMap);
- for (auto& kv : mHotData->mMethodMap)
- {
- memReporter->AddStr(kv.mKey);
- memReporter->Add(sizeof(BfHotMethod));
- memReporter->AddVec(kv.mValue->mReferences);
- }
- }
- if (mHotState != NULL)
- {
- AutoMemReporter autoMemReporter(memReporter, "HotState");
- memReporter->Add(sizeof(HotState));
- memReporter->AddVec(mHotState->mQueuedOutFiles, false);
- memReporter->AddHashSet(mHotState->mSlotDefineTypeIds, false);
- memReporter->AddHashSet(mHotState->mPendingDataChanges, false);
- memReporter->AddMap(mHotState->mDeletedTypeNameMap, false);
- for (auto& kv : mHotState->mDeletedTypeNameMap)
- {
- memReporter->AddStr(kv.mKey, false);
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void BfCompiler::GenerateAutocompleteInfo()
- {
- BP_ZONE("BfCompiler::GetAutocompleteInfo");
- String& autoCompleteResultString = *gTLStrReturn.Get();
- autoCompleteResultString.Clear();
- auto bfModule = mResolvePassData->mAutoComplete->mModule;
- if (bfModule != NULL)
- {
- auto autoComplete = mResolvePassData->mAutoComplete;
- if (autoComplete->mResolveType == BfResolveType_GetNavigationData)
- return; // Already handled
- if (autoComplete->mResolveType == BfResolveType_GetResultString)
- {
- autoCompleteResultString = autoComplete->mResultString;
- return;
- }
- if (autoComplete->mUncertain)
- autoCompleteResultString += "uncertain\n";
- if (autoComplete->mDefaultSelection.length() != 0)
- autoCompleteResultString += StrFormat("select\t%s\n", autoComplete->mDefaultSelection.c_str());
- auto _EncodeTypeDef = [] (BfTypeDef* typeDef)
- {
- StringT<128> typeName = typeDef->mProject->mName;
- typeName += ":";
- typeName += typeDef->mFullName.ToString();
- if (!typeDef->mGenericParamDefs.IsEmpty())
- typeName += StrFormat("`%d", (int)typeDef->mGenericParamDefs.size());
- return typeName;
- };
- if (autoComplete->mResolveType == BfResolveType_GetSymbolInfo)
- {
- if (autoComplete->mDefTypeGenericParamIdx != -1)
- {
- autoCompleteResultString += StrFormat("typeGenericParam\t%d\n", autoComplete->mDefTypeGenericParamIdx);
- autoCompleteResultString += StrFormat("typeRef\t%s\n", _EncodeTypeDef(autoComplete->mDefType).c_str());
- }
- else if (autoComplete->mDefMethodGenericParamIdx != -1)
- {
- autoCompleteResultString += StrFormat("methodGenericParam\t%d\n", autoComplete->mDefMethodGenericParamIdx);
- autoCompleteResultString += StrFormat("methodRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
- }
- else if ((autoComplete->mReplaceLocalId != -1) && (autoComplete->mDefMethod != NULL))
- {
- autoCompleteResultString += StrFormat("localId\t%d\n", autoComplete->mReplaceLocalId);
- autoCompleteResultString += StrFormat("methodRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
- }
- else if (autoComplete->mDefField != NULL)
- {
- autoCompleteResultString += StrFormat("fieldRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefField->mIdx);
- }
- else if (autoComplete->mDefProp != NULL)
- {
- autoCompleteResultString += StrFormat("propertyRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefProp->mIdx);
- }
- else if (autoComplete->mDefMethod != NULL)
- {
- if (autoComplete->mDefMethod->mMethodType == BfMethodType_Ctor)
- autoCompleteResultString += StrFormat("ctorRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
- else
- autoCompleteResultString += StrFormat("methodRef\t%s\t%d\n", _EncodeTypeDef(autoComplete->mDefType).c_str(), autoComplete->mDefMethod->mIdx);
- }
- else if (autoComplete->mDefType != NULL)
- {
- autoCompleteResultString += StrFormat("typeRef\t%s\n", _EncodeTypeDef(autoComplete->mDefType).c_str());
- }
- else if (!autoComplete->mDefNamespace.IsEmpty())
- {
- autoCompleteResultString += StrFormat("namespaceRef\t%s\n", autoComplete->mDefNamespace.ToString().c_str());
- }
- if (autoComplete->mInsertEndIdx > 0)
- {
- if (!mResolvePassData->mParsers.IsEmpty())
- {
- if (mResolvePassData->mParsers[0]->mSrc[autoComplete->mInsertEndIdx - 1] == '!')
- autoComplete->mInsertEndIdx--;
- }
- }
- }
- const char* wantsDocEntry = NULL;
- if (!autoComplete->mDocumentationEntryName.IsEmpty())
- wantsDocEntry = autoComplete->mDocumentationEntryName.c_str();
- if (autoComplete->mInsertStartIdx != -1)
- {
- autoCompleteResultString += StrFormat("insertRange\t%d %d\n", autoComplete->mInsertStartIdx, autoComplete->mInsertEndIdx);
- }
- if ((autoComplete->mDefMethod == NULL) && (autoComplete->mGetDefinitionNode == NULL) && (autoComplete->mIsGetDefinition) && (autoComplete->mMethodMatchInfo != NULL))
- {
- // Take loc from methodMatchInfo
- if (autoComplete->mMethodMatchInfo->mInstanceList.size() > 0)
- {
- int bestIdx = autoComplete->mMethodMatchInfo->mBestIdx;
- auto typeInst = autoComplete->mMethodMatchInfo->mInstanceList[bestIdx].mTypeInstance;
- auto methodDef = autoComplete->mMethodMatchInfo->mInstanceList[bestIdx].mMethodDef;
- if (methodDef->mMethodDeclaration != NULL)
- {
- auto ctorDecl = BfNodeDynCast<BfConstructorDeclaration>(methodDef->mMethodDeclaration);
- if (ctorDecl != NULL)
- autoComplete->SetDefinitionLocation(ctorDecl->mThisToken);
- else
- autoComplete->SetDefinitionLocation(methodDef->GetMethodDeclaration()->mNameNode);
- }
- else // Just select type then
- autoComplete->SetDefinitionLocation(typeInst->mTypeDef->mTypeDeclaration->mNameNode);
- }
- }
- if (autoComplete->mGetDefinitionNode != NULL)
- {
- auto astNode = autoComplete->mGetDefinitionNode;
- auto bfSource = autoComplete->mGetDefinitionNode->GetSourceData()->ToParserData();
- if (bfSource != NULL)
- {
- int line = 0;
- int lineChar = 0;
- bfSource->GetLineCharAtIdx(astNode->GetSrcStart(), line, lineChar);
- autoCompleteResultString += StrFormat("defLoc\t%s\t%d\t%d\n", bfSource->mFileName.c_str(), line, lineChar);
- }
- }
- auto methodMatchInfo = autoComplete->mMethodMatchInfo;
- if ((methodMatchInfo != NULL) && (wantsDocEntry == NULL))
- {
- if ((methodMatchInfo->mInstanceList.size() > 0) && (methodMatchInfo->mBestIdx >= 0))
- {
- if (autoComplete->mIdentifierUsed != NULL)
- {
- String filter;
- if (autoComplete->mIdentifierUsed != NULL)
- autoComplete->mIdentifierUsed->ToString(filter);
- auto& bestInstance = methodMatchInfo->mInstanceList[methodMatchInfo->mBestIdx];
- auto bestMethodDef = bestInstance.mMethodDef;
- if (bestMethodDef != NULL)
- {
- for (int paramIdx = 0; paramIdx < bestMethodDef->mParams.mSize; paramIdx++)
- {
- if ((paramIdx == 0) && (bestMethodDef->mMethodType == BfMethodType_Extension))
- continue;
- autoComplete->AddEntry(AutoCompleteEntry("param", bestMethodDef->mParams[paramIdx]->mName + ":"), filter);
- }
- }
- }
- String invokeInfoText;
- invokeInfoText += StrFormat("%d", methodMatchInfo->mBestIdx);
- for (int srcPosIdx = 0; srcPosIdx < (int)methodMatchInfo->mSrcPositions.size(); srcPosIdx++)
- invokeInfoText += StrFormat(" %d", methodMatchInfo->mSrcPositions[srcPosIdx]);
- autoCompleteResultString += "invokeInfo\t";
- autoCompleteResultString += invokeInfoText;
- autoCompleteResultString += "\n";
- }
- int idx = 0;
- for (auto& methodEntry : methodMatchInfo->mInstanceList)
- {
- String methodText;
- if (methodEntry.mPayloadEnumField != NULL)
- {
- auto payloadFieldDef = methodEntry.mPayloadEnumField->GetFieldDef();
- methodText += payloadFieldDef->mName;
- methodText += "(\x1";
- auto payloadType = methodEntry.mPayloadEnumField->mResolvedType;
- BF_ASSERT(payloadType->IsTuple());
- if (payloadType->IsTuple())
- {
- auto tupleType = (BfTypeInstance*)payloadType;
- for (int fieldIdx = 0; fieldIdx < (int)tupleType->mFieldInstances.size(); fieldIdx++)
- {
- auto fieldInstance = &tupleType->mFieldInstances[fieldIdx];
- auto fieldDef = fieldInstance->GetFieldDef();
- if (fieldIdx > 0)
- methodText += ",\x1 ";
- methodText += bfModule->TypeToString(fieldInstance->mResolvedType, BfTypeNameFlag_ResolveGenericParamNames);
- if (!fieldDef->IsUnnamedTupleField())
- {
- methodText += " ";
- if (fieldDef->mName.StartsWith("_"))
- methodText += fieldDef->mName.Substring(1);
- else
- methodText += fieldDef->mName;
- }
- }
- }
- methodText += "\x1)";
- }
- else
- {
- BfMethodInstance* methodInstance = NULL;
- if (methodEntry.mMethodDef->mIdx < 0)
- {
- for (auto localMethod : mContext->mLocalMethodGraveyard)
- {
- if (localMethod->mMethodDef == methodEntry.mMethodDef)
- {
- methodInstance = localMethod->mMethodInstanceGroup->mDefault;
- break;
- }
- }
- }
- else
- methodInstance = bfModule->GetRawMethodInstanceAtIdx(methodEntry.mTypeInstance, methodEntry.mMethodDef->mIdx);
- auto curMethodInstance = methodInstance;
- curMethodInstance = methodMatchInfo->mCurMethodInstance;
- SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(bfModule->mCurTypeInstance, methodMatchInfo->mCurTypeInstance);
- SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(bfModule->mCurMethodInstance, curMethodInstance);
- Array<String> genericMethodNameOverrides;
- Array<String>* genericMethodNameOverridesPtr = NULL;
- if (methodInstance->GetNumGenericArguments() != 0)
- {
- genericMethodNameOverridesPtr = &genericMethodNameOverrides;
- for (int methodGenericArgIdx = 0; methodGenericArgIdx < (int)methodInstance->GetNumGenericArguments(); methodGenericArgIdx++)
- {
- BfType* methodGenericArg = NULL;
- if (methodEntry.mGenericArguments.size() > 0)
- methodGenericArg = methodEntry.mGenericArguments[methodGenericArgIdx];
- String argName;
- if (methodGenericArg == NULL)
- argName = methodInstance->mMethodDef->mGenericParams[methodGenericArgIdx]->mName;
- else
- argName = bfModule->TypeToString(methodGenericArg, BfTypeNameFlag_ResolveGenericParamNames, NULL);
- genericMethodNameOverrides.push_back(argName);
- }
- }
- if (methodInstance->mMethodDef->mMethodType == BfMethodType_Extension)
- methodText += "(extension) ";
- if (methodInstance->mMethodDef->mMethodType != BfMethodType_Ctor)
- {
- if (methodInstance->mReturnType != NULL)
- methodText += bfModule->TypeToString(methodInstance->mReturnType, BfTypeNameFlag_ResolveGenericParamNames, genericMethodNameOverridesPtr);
- else
- methodText += BfTypeUtils::TypeToString(methodInstance->mMethodDef->mReturnTypeRef);
- methodText += " ";
- }
- if (methodInstance->mMethodDef->mMethodType == BfMethodType_Ctor)
- methodText += "this";
- else
- {
- auto methodName = methodInstance->mMethodDef->mName;
- int splitIdx = (int)methodName.IndexOf('@');
- if (splitIdx != -1)
- methodText += methodName.Substring(0, splitIdx);
- else
- methodText += methodName;
- }
- if (methodInstance->GetNumGenericArguments() != 0)
- {
- methodText += "<";
- for (int methodGenericArgIdx = 0; methodGenericArgIdx < (int)methodInstance->GetNumGenericArguments(); methodGenericArgIdx++)
- {
- if (methodGenericArgIdx > 0)
- methodText += ", ";
- methodText += genericMethodNameOverrides[methodGenericArgIdx];
- }
- methodText += ">";
- }
- //TODO: Show default param values also
- methodText += "(\x1";
- if (methodInstance->GetParamCount() == 0)
- {
- // Hm - is this ever useful? Messes up some cases actually
- // If param resolution failed then we need to print the original param def
- /*for (int paramIdx = 0; paramIdx < (int) methodInstance->mMethodDef->mParams.size(); paramIdx++)
- {
- if (paramIdx > 0)
- methodText += ",\x1 ";
- auto paramDef = methodInstance->mMethodDef->mParams[paramIdx];
- methodText += BfTypeUtils::TypeToString(paramDef->mTypeRef);
- methodText += " ";
- methodText += paramDef->mName;
- }*/
- }
- int dispParamIdx = 0;
- StringT<64> paramName;
- for (int paramIdx = 0; paramIdx < (int)methodInstance->GetParamCount(); paramIdx++)
- {
- auto paramKind = methodInstance->GetParamKind(paramIdx);
- if ((paramKind == BfParamKind_ImplicitCapture) || (paramKind == BfParamKind_AppendIdx))
- continue;
- if (dispParamIdx > 0)
- methodText += ",\x1 ";
- if ((paramIdx == 0) && (methodInstance->mMethodDef->mMethodType == BfMethodType_Extension))
- continue;
- auto type = methodInstance->GetParamType(paramIdx);
- BfExpression* paramInitializer = methodInstance->GetParamInitializer(paramIdx);
- if (paramInitializer != NULL)
- methodText += "[";
- if (paramKind == BfParamKind_Params)
- methodText += "params ";
- if (type->IsGenericParam())
- {
- auto genericParamType = (BfGenericParamType*)type;
- if (genericParamType->mGenericParamKind == BfGenericParamKind_Method)
- {
- BfMethodInstance* checkMethodInstance = methodInstance;
- if (checkMethodInstance->GetNumGenericParams() == 0)
- checkMethodInstance = methodEntry.mCurMethodInstance;
- bool handled = false;
- if (genericParamType->mGenericParamIdx < checkMethodInstance->GetNumGenericParams())
- {
- auto genericParamInstance = checkMethodInstance->mMethodInfoEx->mGenericParams[genericParamType->mGenericParamIdx];
- auto genericParamDef = genericParamInstance->GetGenericParamDef();
- if (genericParamDef != NULL)
- {
- methodText += genericParamDef->mName;
- handled = true;
- }
- }
- if (!handled)
- {
- methodText += StrFormat("@M%d", genericParamType->mGenericParamIdx);
- }
- }
- else
- {
- BfTypeInstance* genericType = methodEntry.mTypeInstance->ToGenericTypeInstance();
- if (genericType == NULL)
- {
- if (methodEntry.mCurMethodInstance != NULL)
- genericType = methodEntry.mCurMethodInstance->GetOwner()->ToGenericTypeInstance();
- }
- bool handled = false;
- if ((genericType != NULL) && (genericParamType->mGenericParamIdx < (int)genericType->mGenericTypeInfo->mGenericParams.size()))
- {
- auto genericParamInstance = genericType->mGenericTypeInfo->mGenericParams[genericParamType->mGenericParamIdx];
- auto genericParamDef = genericParamInstance->GetGenericParamDef();
- if (genericParamDef != NULL)
- {
- methodText += genericParamDef->mName;
- handled = true;
- }
- }
- if (!handled)
- {
- methodText += StrFormat("@T%d", genericParamType->mGenericParamIdx);
- }
- }
- }
- else
- methodText += bfModule->TypeToString(type, BfTypeNameFlag_ResolveGenericParamNames, genericMethodNameOverridesPtr);
- int namePrefixCount = 0;
- methodInstance->GetParamName(paramIdx, paramName, namePrefixCount);
- if (!paramName.IsEmpty())
- {
- methodText += " ";
- for (int i = 0; i < namePrefixCount; i++)
- methodText += "@";
- methodText += paramName;
- }
- if (paramInitializer != NULL)
- {
- methodText += " = ";
- methodText += paramInitializer->ToString();
- methodText += "]";
- }
- dispParamIdx++;
- }
- methodText += "\x1)";
- }
-
- autoCompleteResultString += "invoke\t" + methodText;
- autoCompleteResultString += StrFormat("\t%d", methodEntry.mArgMatchCount);
- if (methodEntry.mMethodDef != NULL)
- {
- auto methodDeclaration = methodEntry.mMethodDef->GetMethodDeclaration();
- if ((methodDeclaration != NULL) && (methodDeclaration->mDocumentation != NULL))
- {
- String docString;
- methodDeclaration->mDocumentation->GetDocString(docString);
- autoCompleteResultString += "\x03";
- autoCompleteResultString += docString;
- }
- }
- autoCompleteResultString += "\n";
- idx++;
- }
- }
- Array<AutoCompleteEntry*> entries;
- for (auto& entry : autoComplete->mEntriesSet)
- {
- entries.Add(&entry);
- }
- std::sort(entries.begin(), entries.end(), [](AutoCompleteEntry* lhs, AutoCompleteEntry* rhs)
- {
- if (lhs->mScore == rhs->mScore)
- return stricmp(lhs->mDisplay, rhs->mDisplay) < 0;
- return lhs->mScore > rhs->mScore;
- });
- String docString;
- for (auto entry : entries)
- {
- if ((wantsDocEntry != NULL) && (entry->mDocumentation == NULL))
- continue;
- autoCompleteResultString += String(entry->mEntryType);
- autoCompleteResultString += '\t';
- for (int i = 0; i < entry->mNamePrefixCount; i++)
- autoCompleteResultString += '@';
- autoCompleteResultString += String(entry->mDisplay);
- if (entry->mMatchesLength > 0)
- {
- autoCompleteResultString += "\x02";
- for (int i = 0; i < entry->mMatchesLength; i++)
- {
- int match = entry->mMatches[i];
- char buffer[16];
- itoa(match, buffer, 16);
- autoCompleteResultString += String(buffer);
- autoCompleteResultString += ",";
- }
- autoCompleteResultString += "X";
- }
- if (entry->mDocumentation != NULL)
- {
- autoCompleteResultString += '\x03';
- autoCompleteResultString += entry->mDocumentation;
- }
- autoCompleteResultString += '\n';
- }
- }
- }
- struct TypeDefMatchHelper
- {
- public:
- struct SearchEntry
- {
- String mStr;
- int mGenericCount;
- SearchEntry()
- {
- mGenericCount = 0;
- }
- };
- public:
- StringImpl& mResult;
- Array<SearchEntry> mSearch;
- uint32 mFoundFlags;
- int32 mFoundCount;
- bool mHasDotSearch;
- String mCurTypeName;
- String mTempStr;
- public:
- TypeDefMatchHelper(StringImpl& str) : mResult(str)
- {
- mFoundFlags = 0;
- mFoundCount = 0;
- mHasDotSearch = false;
- }
- void Sanitize(StringImpl& str)
- {
- for (int i = 0; i < (int)str.length(); i++)
- {
- char c = str[i];
- if (c < (char)32)
- {
- str[i] = ' ';
- }
- }
- }
- void AddParams(BfMethodDef* methodDef, StringImpl& result)
- {
- int visParamIdx = 0;
- for (int paramIdx = 0; paramIdx < (int)methodDef->mParams.size(); paramIdx++)
- {
- auto paramDef = methodDef->mParams[paramIdx];
- if ((paramDef->mParamKind == BfParamKind_AppendIdx) || (paramDef->mParamKind == BfParamKind_ImplicitCapture))
- continue;
- if (visParamIdx > 0)
- result += ", ";
- StringT<64> refName;
- paramDef->mTypeRef->ToString(refName);
- Sanitize(refName);
- result += refName;
- result += " ";
- result += paramDef->mName;
- visParamIdx++;
- }
- }
- void AddLocation(BfAstNode* node)
- {
- if (node == NULL)
- return;
- auto parserData = node->GetSourceData()->ToParserData();
- if (parserData != NULL)
- {
- mResult += parserData->mFileName;
- int lineNum = 0;
- int column = 0;
- parserData->GetLineCharAtIdx(node->GetSrcStart(), lineNum, column);
- mResult += StrFormat("\t%d\t%d", lineNum, column);
- }
- };
- void AddFieldDef(BfFieldDef* fieldDef)
- {
- mResult += "\t";
- AddLocation(fieldDef->GetRefNode());
- mResult += "\n";
- }
- void AddPropertyDef(BfTypeDef* typeDef, BfPropertyDef* propDef)
- {
- if (propDef->mName == "[]")
- {
- mResult += "[";
- for (auto methodDef : propDef->mMethods)
- {
- if (methodDef->mMethodType == BfMethodType_PropertyGetter)
- {
- AddParams(methodDef, mResult);
- break;
- }
- }
- mResult += "]";
- }
- else
- mResult += propDef->mName;
- mResult += "\t";
- auto refNode = propDef->GetRefNode();
- if (refNode == NULL)
- refNode = typeDef->GetRefNode();
- AddLocation(refNode);
- mResult += "\n";
- }
- void GetGenericStr(BfMethodDef* methodDef, StringImpl& result)
- {
- if (methodDef->mGenericParams.IsEmpty())
- return;
- result += "<";
- for (int i = 0; i < methodDef->mGenericParams.mSize; i++)
- {
- if (i > 0)
- result += ", ";
- result += methodDef->mGenericParams[i]->mName;
- }
- result += ">";
- }
- void GetMethodDefString(BfMethodDef* methodDef, StringImpl& result, int* genericStrIdx = NULL)
- {
- if (methodDef->mMethodType == BfMethodType_Ctor)
- {
- if (methodDef->mIsStatic)
- result += "static ";
- result += "this";
- }
- else if (methodDef->mMethodType == BfMethodType_Dtor)
- {
- if (methodDef->mIsStatic)
- result += "static ";
- result += "~this";
- }
- else
- result += methodDef->mName;
- if (methodDef->mMethodType == BfMethodType_Mixin)
- result += "!";
- if (!methodDef->mGenericParams.IsEmpty())
- {
- if (genericStrIdx != NULL)
- {
- *genericStrIdx = result.mLength;
- }
- else
- {
- GetGenericStr(methodDef, result);
- }
- }
- result += "(";
- AddParams(methodDef, result);
- result += ")";
- }
- void AddMethodDef(BfMethodDef* methodDef, StringImpl* methodDefString = NULL)
- {
- if (methodDefString != NULL)
- mResult += *methodDefString;
- else
- GetMethodDefString(methodDef, mResult);
- mResult += "\t";
- AddLocation(methodDef->GetRefNode());
- mResult += "\n";
- }
- void ClearResults()
- {
- mFoundFlags = 0;
- mFoundCount = 0;
- }
- bool MergeFlags(uint32 flags)
- {
- int flagIdx = 0;
- while (flags > 0)
- {
- if (((flags & 1) != 0) && ((mFoundFlags & (1 << flagIdx)) == 0))
- {
- mFoundFlags |= (1 << flagIdx);
- mFoundCount++;
- }
- flags >>= 1;
- flagIdx++;
- }
- return mFoundCount == mSearch.mSize;
- }
- uint32 CheckMatch(const StringView& str, int startIdx = 0)
- {
- uint32 matchFlags = 0;
- for (int i = startIdx; i < mSearch.mSize; i++)
- {
- auto& search = mSearch[i];
- if (((mFoundFlags & (1 << i)) == 0) && (str.IndexOf(search.mStr, true) != -1))
- {
- bool genericMatches = true;
- if (search.mGenericCount > 0)
- {
- genericMatches = false;
- int countIdx = (int)str.LastIndexOf('`');
- if (countIdx > 0)
- {
- int genericCount = atoi(str.mPtr + countIdx + 1);
- genericMatches = ((genericCount == search.mGenericCount) || (search.mGenericCount == 1)) &&
- (countIdx == (int)search.mStr.length());
- }
- }
- if (genericMatches)
- {
- mFoundCount++;
- matchFlags |= (1 << i);
- mFoundFlags |= (1 << i);
- }
- }
- }
- return matchFlags;
- }
- bool CheckCompletesMatch(BfAtomComposite& name)
- {
- for (int i = 0; i < name.mSize; i++)
- {
- CheckMatch(name.mParts[i]->mString);
- if (mFoundCount == mSearch.mSize)
- return true;
- }
- return false;
- }
- bool IsFullMatch()
- {
- return mFoundCount == mSearch.mSize;
- }
- bool CheckMemberMatch(BfTypeDef* typeDef, const StringView& str)
- {
- if (CheckMatch(str) == 0)
- {
- if (mHasDotSearch)
- {
- mTempStr.Clear();
- mTempStr += mCurTypeName;
- mTempStr += ".";
- mTempStr += str;
- if (CheckMatch(mTempStr) == 0)
- return false;
- }
- else
- return false;
- }
- if ((IsFullMatch()) || (CheckCompletesMatch(typeDef->mFullName)))
- return true;
- return false;
- }
- };
- String BfCompiler::GetTypeDefList(bool includeLocation)
- {
- String result;
- TypeDefMatchHelper matchHelper(result);
- BfProject* curProject = NULL;
- Dictionary<BfProject*, int> projectIds;
- for (auto typeDef : mSystem->mTypeDefs)
- {
- if (typeDef->mProject != curProject)
- {
- curProject = typeDef->mProject;
- int* projectIdPtr;
- if (projectIds.TryAdd(curProject, NULL, &projectIdPtr))
- {
- *projectIdPtr = (int)projectIds.size() - 1;
- result += '+';
- result += curProject->mName;
- result += '\n';
- }
- else
- {
- char str[32];
- sprintf(str, "=%d\n", *projectIdPtr);
- result += str;
- }
- }
- if (((!typeDef->mIsPartial) || (typeDef->mIsCombinedPartial)))
- {
- if (typeDef->IsGlobalsContainer())
- {
- result += 'g';
- if (!typeDef->mNamespace.IsEmpty())
- {
- typeDef->mNamespace.ToString(result);
- result += '.';
- }
- result += ":static\n";
- continue;
- }
- else if (typeDef->mTypeCode == BfTypeCode_Interface)
- result += 'i';
- else if (typeDef->mTypeCode == BfTypeCode_Object)
- result += 'c';
- else
- result += 'v';
- String typeName = BfTypeUtils::TypeToString(typeDef, BfTypeNameFlag_InternalName);
- if (includeLocation)
- {
- result += typeName + "\t";
- matchHelper.AddLocation(typeDef->GetRefNode());
- result += "\n";
- }
- else
- {
- result += typeName + "\n";
- }
- }
- }
- return result;
- }
- String BfCompiler::GetGeneratorString(BfTypeDef* typeDef, BfTypeInstance* typeInst, const StringImpl& generatorMethodName, const StringImpl* args)
- {
- if (typeInst == NULL)
- {
- auto type = mContext->mUnreifiedModule->ResolveTypeDef(typeDef, BfPopulateType_BaseType);
- if (type != NULL)
- typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- return "";
- }
- BfTypeVector typeVector;
- typeVector.Add(typeInst);
- auto generatorTypeInst = mContext->mUnreifiedModule->ResolveTypeDef(mCompilerGeneratorTypeDef)->ToTypeInstance();
- auto methodDef = generatorTypeInst->mTypeDef->GetMethodByName(generatorMethodName);
- auto moduleMethodInstance = mContext->mUnreifiedModule->GetMethodInstance(generatorTypeInst, methodDef, typeVector);
- SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mContext->mUnreifiedModule->mCurMethodInstance, moduleMethodInstance.mMethodInstance);
- SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mContext->mUnreifiedModule->mCurTypeInstance, typeInst);
- BfExprEvaluator exprEvaluator(mContext->mUnreifiedModule);
- exprEvaluator.mBfEvalExprFlags = (BfEvalExprFlags)(BfEvalExprFlags_Comptime | BfEvalExprFlags_NoCeRebuildFlags);
- SizedArray<BfIRValue, 1> irArgs;
- if (args != NULL)
- irArgs.Add(mContext->mUnreifiedModule->GetStringObjectValue(*args));
- auto callResult = exprEvaluator.CreateCall(NULL, moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc, false, irArgs, NULL, BfCreateCallFlags_None);
- if (callResult.mValue.IsConst())
- {
- auto stringPtr = mContext->mUnreifiedModule->GetStringPoolString(callResult.mValue, mContext->mUnreifiedModule->mBfIRBuilder);
- if (stringPtr != NULL)
- return *stringPtr;
- }
- return "";
- }
- void BfCompiler::HandleGeneratorErrors(StringImpl& result)
- {
- if ((mPassInstance->mErrors.IsEmpty()) && (mPassInstance->mOutStream.IsEmpty()))
- return;
- result.Clear();
- for (auto& msg : mPassInstance->mOutStream)
- {
- String error = msg;
- error.Replace('\n', '\r');
- result += "!error\t";
- result += error;
- result += "\n";
- }
- }
- String BfCompiler::GetGeneratorTypeDefList()
- {
- String result;
- BfProject* curProject = NULL;
- Dictionary<BfProject*, int> projectIds;
- BfResolvePassData resolvePassData;
- SetAndRestoreValue<BfResolvePassData*> prevResolvePassData(mResolvePassData, &resolvePassData);
- BfPassInstance passInstance(mSystem);
- SetAndRestoreValue<BfPassInstance*> prevPassInstance(mPassInstance, &passInstance);
- for (auto typeDef : mSystem->mTypeDefs)
- {
- if (typeDef->mProject->mDisabled)
- continue;
- if (typeDef->mIsPartial)
- continue;
- auto type = mContext->mUnreifiedModule->ResolveTypeDef(typeDef, BfPopulateType_BaseType);
- if ((type != NULL) && (type->IsTypeInstance()))
- {
- auto typeInst = type->ToTypeInstance();
- if ((typeInst->mBaseType != NULL) && (typeInst->mBaseType->IsInstanceOf(mCompilerGeneratorTypeDef)))
- {
- result += typeDef->mProject->mName;
- result += ":";
- result += BfTypeUtils::TypeToString(typeDef, BfTypeNameFlag_InternalName);
- String nameString = GetGeneratorString(typeDef, typeInst, "GetName", NULL);
- if (!nameString.IsEmpty())
- result += "\t" + nameString;
- result += "\n";
- }
- }
- }
- HandleGeneratorErrors(result);
- return result;
- }
- String BfCompiler::GetGeneratorInitData(const StringImpl& typeName, const StringImpl& args)
- {
- BfResolvePassData resolvePassData;
- SetAndRestoreValue<BfResolvePassData*> prevResolvePassData(mResolvePassData, &resolvePassData);
- BfPassInstance passInstance(mSystem);
- SetAndRestoreValue<BfPassInstance*> prevPassInstance(mPassInstance, &passInstance);
- Array<BfTypeDef*> typeDefs;
- GetTypeDefs(typeName, typeDefs);
- String result;
- for (auto typeDef : typeDefs)
- {
- result += GetGeneratorString(typeDef, NULL, "InitUI", &args);
- if (!result.IsEmpty())
- break;
- }
- HandleGeneratorErrors(result);
- return result;
- }
- String BfCompiler::GetGeneratorGenData(const StringImpl& typeName, const StringImpl& args)
- {
- BfResolvePassData resolvePassData;
- SetAndRestoreValue<BfResolvePassData*> prevResolvePassData(mResolvePassData, &resolvePassData);
- BfPassInstance passInstance(mSystem);
- SetAndRestoreValue<BfPassInstance*> prevPassInstance(mPassInstance, &passInstance);
- Array<BfTypeDef*> typeDefs;
- GetTypeDefs(typeName, typeDefs);
- String result;
- for (auto typeDef : typeDefs)
- {
- result += GetGeneratorString(typeDef, NULL, "Generate", &args);
- if (!result.IsEmpty())
- break;
- }
- HandleGeneratorErrors(result);
- return result;
- }
- String BfCompiler::GetTypeDefMatches(const StringImpl& searchStr, bool includeLocation)
- {
- String result;
- TypeDefMatchHelper matchHelper(result);
- int openParenIdx = -1;
- bool parenHasDot = false;
- //
- {
- int searchIdx = 0;
- while (searchIdx < (int)searchStr.length())
- {
- int spacePos = (int)searchStr.IndexOf(' ', searchIdx);
- String str;
- if (spacePos == -1)
- str = searchStr.Substring(searchIdx);
- else
- str = searchStr.Substring(searchIdx, (int)(spacePos - searchIdx));
- str.Trim();
- TypeDefMatchHelper::SearchEntry searchEntry;
- for (int i = 0; i < (int)str.length(); i++)
- {
- char c = str[i];
- if (c == '<')
- {
- searchEntry.mGenericCount = 1;
- searchEntry.mStr = str.Substring(0, i);
- }
- else if (searchEntry.mGenericCount > 0)
- {
- if (c == ',')
- searchEntry.mGenericCount++;
- }
- }
- if (searchEntry.mStr.IsEmpty())
- searchEntry.mStr = str;
- if (str.Contains('('))
- {
- if (str.Contains('.'))
- parenHasDot = true;
- if (openParenIdx == -1)
- openParenIdx = matchHelper.mSearch.mSize;
- }
- if (!searchEntry.mStr.IsEmpty())
- matchHelper.mSearch.Add(searchEntry);
- if (str.Contains('.'))
- matchHelper.mHasDotSearch = true;
- if (spacePos == -1)
- break;
- searchIdx = spacePos + 1;
- }
- //// We sort from longest to shortest to make sure longer strings match before shorter, which
- //// matters when the shorter string is a subset of the longer string
- //matchHelper.mSearch.Sort([](const String& lhs, const String& rhs)
- // {
- // int lenCmp = (int)(rhs.length() - lhs.length());
- // if (lenCmp != 0)
- // return lenCmp < 0;
- // return lhs < rhs;
- // });
- }
- BfProject* curProject = NULL;
- Dictionary<BfProject*, int> projectIds;
- Dictionary<BfAtom*, int> atomMatchMap;
- struct ProjectInfo
- {
- Dictionary<String, int> matchedNames;
- };
- Array<ProjectInfo> projectInfos;
- projectInfos.Resize(mSystem->mProjects.size());
- String typeName;
- String foundName;
- int partialIdx = 0;
- for (auto typeDef : mSystem->mTypeDefs)
- {
- if (typeDef->mIsPartial)
- continue;
- bool fullyMatchesName = false;
- if (matchHelper.mHasDotSearch)
- {
- matchHelper.mCurTypeName.Clear();
- typeDef->mFullNameEx.ToString(matchHelper.mCurTypeName);
- matchHelper.ClearResults();
- matchHelper.CheckMatch(matchHelper.mCurTypeName);
- fullyMatchesName = matchHelper.IsFullMatch();
- }
- int matchIdx = -1;
- String tempStr;
- if (!fullyMatchesName)
- {
- for (auto fieldDef : typeDef->mFields)
- {
- if (BfNodeIsA<BfPropertyDeclaration>(fieldDef->mFieldDeclaration))
- continue;
- matchHelper.ClearResults();
- bool hasMatch = false;
- if (matchHelper.CheckMemberMatch(typeDef, fieldDef->mName))
- {
- result += "F";
- if (BfTypeUtils::TypeToString(result, typeDef, (BfTypeNameFlags)(BfTypeNameFlag_HideGlobalName | BfTypeNameFlag_InternalName)))
- result += ".";
- result += fieldDef->mName;
- matchHelper.AddFieldDef(fieldDef);
- }
- }
- for (auto propDef : typeDef->mProperties)
- {
- if (propDef->GetRefNode() == NULL)
- continue;
- matchHelper.ClearResults();
- if (matchHelper.CheckMemberMatch(typeDef, propDef->mName))
- {
- result += "P";
- if (BfTypeUtils::TypeToString(result, typeDef, (BfTypeNameFlags)(BfTypeNameFlag_HideGlobalName | BfTypeNameFlag_InternalName)))
- result += ".";
- matchHelper.AddPropertyDef(typeDef, propDef);
- }
- }
- for (auto methodDef : typeDef->mMethods)
- {
- if ((methodDef->mMethodType != BfMethodType_Normal) &&
- (methodDef->mMethodType != BfMethodType_Mixin) &&
- (methodDef->mMethodType != BfMethodType_Ctor) &&
- (methodDef->mMethodType != BfMethodType_Dtor))
- continue;
- if (methodDef->mMethodDeclaration == NULL)
- continue;
- matchHelper.ClearResults();
- bool matches = matchHelper.CheckMemberMatch(typeDef, methodDef->mName);
- bool hasTypeString = false;
- bool hasMethodString = false;
- int genericMethodParamIdx = -1;
- if ((!matches) && (openParenIdx != -1))
- {
- hasMethodString = true;
- tempStr.Clear();
- if (parenHasDot)
- {
- hasTypeString = true;
- if (BfTypeUtils::TypeToString(tempStr, typeDef, (BfTypeNameFlags)(BfTypeNameFlag_HideGlobalName | BfTypeNameFlag_InternalName)))
- tempStr += ".";
- }
- matchHelper.GetMethodDefString(methodDef, tempStr, &genericMethodParamIdx);
- matchHelper.CheckMatch(tempStr, openParenIdx);
- matches = matchHelper.IsFullMatch();
- }
- if (matches)
- {
- if (methodDef->mIsOverride)
- result += "o";
- else
- result += "M";
- if (!hasTypeString)
- {
- if (BfTypeUtils::TypeToString(result, typeDef, (BfTypeNameFlags)(BfTypeNameFlag_HideGlobalName | BfTypeNameFlag_InternalName)))
- result += ".";
- }
- if (hasMethodString)
- {
- if (genericMethodParamIdx != -1)
- {
- result += StringView(tempStr, 0, genericMethodParamIdx);
- matchHelper.GetGenericStr(methodDef, result);
- result += StringView(tempStr, genericMethodParamIdx);
- tempStr.Clear();
- matchHelper.AddMethodDef(methodDef, &tempStr);
- }
- else
- matchHelper.AddMethodDef(methodDef, &tempStr);
- }
- else
- matchHelper.AddMethodDef(methodDef);
- }
- }
- uint32 matchFlags = 0;
- for (int atomIdx = typeDef->mFullNameEx.mSize - 1; atomIdx >= 0; atomIdx--)
- {
- auto atom = typeDef->mFullNameEx.mParts[atomIdx];
- int* matchesPtr = NULL;
- if (atomMatchMap.TryAdd(atom, NULL, &matchesPtr))
- {
- matchHelper.ClearResults();
- *matchesPtr = matchHelper.CheckMatch(atom->mString);
- }
- if (*matchesPtr != 0)
- {
- if (matchIdx == -1)
- matchIdx = atomIdx;
- matchFlags |= *matchesPtr;
- }
- }
- matchHelper.ClearResults();
- if (!matchHelper.MergeFlags(matchFlags))
- {
- continue;
- }
- }
- if (typeDef->mProject != curProject)
- {
- curProject = typeDef->mProject;
- int* projectIdPtr;
- if (projectIds.TryAdd(curProject, NULL, &projectIdPtr))
- {
- *projectIdPtr = (int)projectIds.size() - 1;
- result += "+";
- result += curProject->mName;
- result += "\n";
- }
- else
- {
- char str[32];
- sprintf(str, "=%d\n", *projectIdPtr);
- result += str;
- }
- }
- typeName = BfTypeUtils::TypeToString(typeDef, BfTypeNameFlag_InternalName);
- if (matchIdx != -1)
- {
- int* matchIdxPtr = 0;
- auto projectInfo = &projectInfos[typeDef->mProject->mIdx];
- int dotCount = 0;
- foundName = typeName;
- for (int i = 0; i < (int)typeName.length(); i++)
- {
- if (typeName[i] == '.')
- {
- if (dotCount == matchIdx)
- {
- foundName.Clear();
- foundName.Append(typeName.c_str(), i);
- break;
- }
- dotCount++;
- }
- }
- if (projectInfo->matchedNames.TryAdd(foundName, NULL, &matchIdxPtr))
- {
- *matchIdxPtr = partialIdx++;
- result += StrFormat(">%d@", matchIdx);
- }
- else
- {
- result += StrFormat("<%d@", *matchIdxPtr);
- }
- }
- else
- {
- result += ":";
- }
- if (typeDef->IsGlobalsContainer())
- {
- result += "g";
- if (!typeDef->mNamespace.IsEmpty())
- {
- typeDef->mNamespace.ToString(result);
- result += ".";
- }
- result += ":static\n";
- continue;
- }
- else if (typeDef->mTypeCode == BfTypeCode_Interface)
- result += "i";
- else if (typeDef->mTypeCode == BfTypeCode_Object)
- result += "c";
- else
- result += "v";
- if (includeLocation)
- {
- result += typeName + "\t";
- matchHelper.AddLocation(typeDef->GetRefNode());
- result += "\n";
- }
- else
- {
- result += typeName + "\n";
- }
- }
- return result;
- }
- void BfCompiler::GetTypeDefs(const StringImpl& inTypeName, Array<BfTypeDef*>& typeDefs)
- {
- BfProject* project = NULL;
- int idx = 0;
- int sep = (int)inTypeName.LastIndexOf(':');
- if (sep != -1)
- {
- int startProjName = (int)inTypeName.LastIndexOf(':', sep - 1) + 1;
- idx = sep + 1;
- project = mSystem->GetProject(inTypeName.Substring(startProjName, sep - startProjName));
- }
- String typeName;
- int genericCount = 0;
- int pendingGenericCount = 0;
- for (; idx < (int)inTypeName.length(); idx++)
- {
- char c = inTypeName[idx];
- if (c == '<')
- genericCount = 1;
- else if (genericCount > 0)
- {
- if (c == ',')
- genericCount++;
- else if (c == '>')
- {
- pendingGenericCount = genericCount;
- genericCount = 0;
- }
- }
- else
- {
- if (pendingGenericCount != 0)
- {
- typeName += StrFormat("`%d", pendingGenericCount);
- pendingGenericCount = 0;
- }
- typeName += c;
- }
- }
- bool isGlobals = false;
- if (typeName == ":static")
- {
- typeName.clear();
- isGlobals = true;
- }
- if (typeName.EndsWith(".:static"))
- {
- typeName.RemoveToEnd(typeName.length() - 8);
- isGlobals = true;
- }
- for (int i = 0; i < (int)typeName.length(); i++)
- if (typeName[i] == '+')
- typeName[i] = '.';
- BfAtomComposite nameComposite;
- if ((typeName.IsEmpty()) || (mSystem->ParseAtomComposite(typeName, nameComposite)))
- {
- auto itr = mSystem->mTypeDefs.TryGet(nameComposite);
- while (itr)
- {
- auto typeDef = *itr;
- if ((!typeDef->mIsPartial) &&
- (typeDef->mProject == project) &&
- (typeDef->mFullName == nameComposite) &&
- (typeDef->IsGlobalsContainer() == isGlobals) &&
- (typeDef->GetSelfGenericParamCount() == pendingGenericCount))
- {
- typeDefs.Add(typeDef);
- }
- itr.MoveToNextHashMatch();
- }
- }
- }
- String BfCompiler::GetTypeDefInfo(const StringImpl& inTypeName)
- {
- Array<BfTypeDef*> typeDefs;
- GetTypeDefs(inTypeName, typeDefs);
- String result;
- TypeDefMatchHelper matchHelper(result);
- for (auto typeDef : typeDefs)
- {
- auto refNode = typeDef->GetRefNode();
- result += "S";
- matchHelper.AddLocation(refNode);
- result += "\n";
- for (auto fieldDef : typeDef->mFields)
- {
- result += "F";
- result += fieldDef->mName;
- matchHelper.AddFieldDef(fieldDef);
- }
- for (auto propDef : typeDef->mProperties)
- {
- if (propDef->GetRefNode() == NULL)
- continue;
- result += "P";
- matchHelper.AddPropertyDef(typeDef, propDef);
- }
- for (auto methodDef : typeDef->mMethods)
- {
- if ((methodDef->mMethodType != BfMethodType_Normal) &&
- (methodDef->mMethodType != BfMethodType_Mixin) &&
- (methodDef->mMethodType != BfMethodType_Ctor) &&
- (methodDef->mMethodType != BfMethodType_Dtor))
- continue;
- if (methodDef->mMethodDeclaration == NULL)
- continue;
- result += "M";
- matchHelper.AddMethodDef(methodDef);
- }
- }
- return result;
- }
- int BfCompiler::GetTypeId(const StringImpl& typeName)
- {
- auto type = GetType(typeName);
- if (type != NULL)
- return type->mTypeId;
- return -1;
- }
- BfType* BfCompiler::GetType(const StringImpl& fullTypeName)
- {
- AutoCrit autoCrit(mSystem->mSystemLock);
- BfPassInstance passInstance(mSystem);
- BfProject* activeProject = NULL;
- String typeName = fullTypeName;
- int colonPos = (int)typeName.LastIndexOf(':');
- if (colonPos != -1)
- {
- int startProjName = (int)typeName.LastIndexOf(':', colonPos - 1) + 1;
- activeProject = mSystem->GetProject(typeName.Substring(startProjName, colonPos - startProjName));
- typeName.Remove(0, colonPos + 1);
- }
- BfTypeState typeState;
- typeState.mPrevState = mContext->mCurTypeState;
- typeState.mActiveProject = activeProject;
- SetAndRestoreValue<BfTypeState*> prevTypeState(mContext->mCurTypeState, &typeState);
- BfParser parser(mSystem);
- parser.SetSource(typeName.c_str(), (int)typeName.length());
- parser.Parse(&passInstance);
- BfReducer reducer;
- reducer.mAlloc = parser.mAlloc;
- reducer.mPassInstance = &passInstance;
- if (parser.mRootNode->mChildArr.mSize == 0)
- return NULL;
- auto firstNode = parser.mRootNode->mChildArr[0];
- auto endIdx = parser.mRootNode->mSrcEnd;
- reducer.mVisitorPos = BfReducer::BfVisitorPos(parser.mRootNode);
- reducer.mVisitorPos.MoveNext();
- auto typeRef = reducer.CreateTypeRef(firstNode);
- if (typeRef == NULL)
- return NULL;
- BfResolvePassData resolvePass;
- SetAndRestoreValue<bool> prevIgnoreError(mContext->mScratchModule->mIgnoreErrors, true);
- SetAndRestoreValue<bool> prevIgnoreWarnings(mContext->mScratchModule->mIgnoreWarnings, true);
- SetAndRestoreValue<BfResolvePassData*> prevResolvePass(mResolvePassData, &resolvePass);
- auto type = mContext->mScratchModule->ResolveTypeRef(typeRef, BfPopulateType_Identity, (BfResolveTypeRefFlags)(
- BfResolveTypeRefFlag_NoCreate | BfResolveTypeRefFlag_AllowUnboundGeneric | BfResolveTypeRefFlag_AllowGlobalContainer));
- if (type != NULL)
- return type;
- return NULL;
- }
- int BfCompiler::GetEmitSource(const StringImpl& fileName, StringImpl* outBuffer)
- {
- int lastDollarPos = (int)fileName.LastIndexOf('$');
- if (lastDollarPos == -1)
- return -1;
- String typeName = fileName.Substring(lastDollarPos + 1);
- int typeId = GetTypeId(typeName);
- if ((typeId <= 0) || (typeId >= mContext->mTypes.mSize))
- return -1;
- auto type = mContext->mTypes[typeId];
- if (type == NULL)
- return -1;
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- return -1;
- auto typeDef = typeInst->mTypeDef;
- if (typeDef->mEmitParent == NULL)
- return -1;
- auto emitParser = typeDef->mSource->ToParser();
- if (emitParser == NULL)
- return -1;
- if (outBuffer != NULL)
- outBuffer->Append(emitParser->mSrc, emitParser->mSrcLength);
- return typeInst->mRevision;
- }
- String BfCompiler::GetEmitLocation(const StringImpl& typeName, int emitLine, int& outEmbedLine, int& outEmbedLineChar, uint64& outHash)
- {
- outEmbedLine = 0;
- int typeId = GetTypeId(typeName);
- if (typeId <= 0)
- return "";
- auto bfType = mContext->FindTypeById(typeId);
- if (bfType == NULL)
- return "";
- auto typeInst = bfType->ToTypeInstance();
- if (typeInst == NULL)
- return "";
- if (typeInst->mCeTypeInfo == NULL)
- return "";
- for (auto& kv : typeInst->mCeTypeInfo->mEmitSourceMap)
- {
- int partialIdx = (int)(kv.mKey >> 32);
- int charIdx = (int)(kv.mKey & 0xFFFFFFFF);
- auto typeDef = typeInst->mTypeDef;
- if (partialIdx > 0)
- typeDef = typeDef->mPartials[partialIdx];
- auto origParser = typeDef->GetDefinition()->GetLastSource()->ToParser();
- if (origParser == NULL)
- continue;
- auto emitParser = typeInst->mTypeDef->GetLastSource()->ToParser();
- if (emitParser == NULL)
- continue;
- int startLine = 0;
- int startLineChar = 0;
- emitParser->GetLineCharAtIdx(kv.mValue.mSrcStart, startLine, startLineChar);
- int endLine = 0;
- int endLineChar = 0;
- emitParser->GetLineCharAtIdx(kv.mValue.mSrcEnd - 1, endLine, endLineChar);
- outHash = Hash64(emitParser->mSrc + kv.mValue.mSrcStart, kv.mValue.mSrcEnd - kv.mValue.mSrcStart);
- if ((emitLine >= startLine) && (emitLine <= endLine))
- {
- origParser->GetLineCharAtIdx(charIdx, outEmbedLine, outEmbedLineChar);
- return origParser->mFileName;
- }
- }
- return "";
- }
- bool BfCompiler::WriteEmitData(const StringImpl& filePath, BfProject* project)
- {
- ZipFile zipFile;
- for (auto type : mContext->mResolvedTypes)
- {
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeInst->mTypeDef->mEmitParent == NULL)
- continue;
- if (!project->ContainsReference(typeInst->mTypeDef->mProject))
- continue;
- auto bfParser = typeInst->mTypeDef->GetLastSource()->ToParser();
- String name = bfParser->mFileName;
- if (name.StartsWith("$Emit$"))
- name.Remove(0, 6);
- String path = EncodeFileName(name);
- path.Append(".bf");
- if (!zipFile.IsOpen())
- {
- if (!zipFile.Create(filePath))
- return false;
- }
- zipFile.Add(path, Span<uint8>((uint8*)bfParser->mSrc, bfParser->mSrcLength));
- }
- if (zipFile.IsOpen())
- return zipFile.Close();
- return true;
- }
- //////////////////////////////////////////////////////////////////////////
- PerfManager* BfGetPerfManager(BfParser* bfParser);
- /*BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetDefaultTargetTriple(BfCompiler* bfCompiler)
- {
- String& autoCompleteResultString = *gTLStrReturn.Get();
- return autoCompleteResultString.c_str();
- }*/
- BF_EXPORT bool BF_CALLTYPE BfCompiler_Compile(BfCompiler* bfCompiler, BfPassInstance* bfPassInstance, const char* outputPath)
- {
- BP_ZONE("BfCompiler_Compile");
- SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, bfPassInstance);
- bfCompiler->mPassInstance = bfPassInstance;
- bfCompiler->Compile(outputPath);
- return !bfCompiler->mPassInstance->HasFailed();
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_ClearResults(BfCompiler* bfCompiler)
- {
- bfCompiler->ClearResults();
- }
- BF_EXPORT bool BF_CALLTYPE BfCompiler_ClassifySource(BfCompiler* bfCompiler, BfPassInstance* bfPassInstance, BfResolvePassData* resolvePassData)
- {
- BP_ZONE("BfCompiler_ClassifySource");
- bfCompiler->mSystem->AssertWeHaveLock();
- String& autoCompleteResultString = *gTLStrReturn.Get();
- autoCompleteResultString.clear();
- bfPassInstance->mCompiler = bfCompiler;
- for (auto parser : resolvePassData->mParsers)
- bfPassInstance->mFilterErrorsTo.Add(parser->mSourceData);
- bfPassInstance->mTrimMessagesToCursor = true;
- SetAndRestoreValue<BfResolvePassData*> prevCompilerResolvePassData(bfCompiler->mResolvePassData, resolvePassData);
- SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, bfPassInstance);
- bool canceled = false;
- if ((resolvePassData->mAutoComplete != NULL) && (!resolvePassData->mParsers.IsEmpty()))
- {
- bfCompiler->ProcessAutocompleteTempType();
- }
- else
- canceled = !bfCompiler->Compile("");
- return !canceled;
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetCollapseRegions(BfCompiler* bfCompiler, BfParser* bfParser, BfResolvePassData* resolvePassData, char* explicitEmitTypeNames)
- {
- bfCompiler->mSystem->AssertWeHaveLock();
- String& outString = *gTLStrReturn.Get();
- outString.Clear();
- class CollapseVisitor : public BfElementVisitor
- {
- public:
- BfAstNode* mParentNode;
- BfParser* mParser;
- String& mOutString;
- HashSet<int> mEndsFound;
- char mSeriesKind;
- int mStartSeriesIdx;
- int mEndSeriesIdx;
- public:
- CollapseVisitor(BfParser* parser, String& string) : mOutString(string)
- {
- mParser = parser;
- mParentNode = NULL;
- mSeriesKind = 0;
- mStartSeriesIdx = -1;
- mEndSeriesIdx = -1;
- }
- void UpdateSeries(BfAstNode* node, char kind)
- {
- if (mStartSeriesIdx != -1)
- {
- if ((node->mTriviaStart != mEndSeriesIdx) || (kind != mSeriesKind))
- {
- FlushSeries();
- mStartSeriesIdx = node->mSrcStart;
- }
- }
- else
- mStartSeriesIdx = node->mSrcStart;
- mSeriesKind = kind;
- mEndSeriesIdx = BF_MIN(node->mSrcEnd, mParser->mSrcLength -1);
- }
- void FlushSeries()
- {
- if (mStartSeriesIdx != -1)
- {
- bool ownsLine = true;
- for (int checkIdx = mStartSeriesIdx - 1; checkIdx >= 0; checkIdx--)
- {
- char c = mParser->mSrc[checkIdx];
- if (c == '\n')
- break;
- if (!isspace((uint8)c))
- {
- ownsLine = false;
- break;
- }
- }
- int anchor = mStartSeriesIdx;
- if (!ownsLine)
- {
- int checkLine = GetLineStartAfter(anchor);
- if (checkLine != -1)
- {
- anchor = checkLine;
- for (; anchor < mEndSeriesIdx; anchor++)
- {
- if (!::isspace((uint8)mParser->mSrc[anchor]))
- break;
- }
- }
- }
- Add(anchor, mEndSeriesIdx, mSeriesKind);
- }
- mStartSeriesIdx = -1;
- }
- int GetLineStartAfter(int startIdx)
- {
- for (int i = startIdx; i < mParser->mSrcLength - 1; i++)
- {
- if (mParser->mSrc[i] == '\n')
- return i + 1;
- }
- return -1;
- }
- int GetLineEndBefore(int startIdx)
- {
- for (int i = startIdx; i >= 1; i--)
- {
- if (mParser->mSrc[i] == '\n')
- return i;
- }
- return -1;
- }
- void Add(int anchor, int end, char kind = '?', int minLines = 2)
- {
- if ((anchor == -1) || (end == -1))
- return;
- if (!mEndsFound.Add(end))
- return;
- int lineCount = 1;
- for (int i = anchor; i < end; i++)
- {
- if (mParser->mSrc[i] == '\n')
- {
- lineCount++;
- if (lineCount >= minLines)
- break;
- }
- }
- if (lineCount < minLines)
- return;
- char str[1024];
- sprintf(str, "%c%d,%d\n", kind, anchor, end);
- mOutString.Append(str);
- }
- void Add(BfAstNode* anchor, BfAstNode* end, char kind = '?', int minLines = 2)
- {
- if ((anchor == NULL) || (end == NULL))
- return;
- Add(anchor->mSrcStart, end->mSrcEnd - 1, kind, minLines);
- }
- virtual void Visit(BfMethodDeclaration* methodDeclaration) override
- {
- int anchorIdx = methodDeclaration->mSrcStart;
- if (methodDeclaration->mNameNode != NULL)
- anchorIdx = methodDeclaration->mNameNode->mSrcEnd - 1;
- if (methodDeclaration->mCloseParen != NULL)
- anchorIdx = methodDeclaration->mCloseParen->mSrcEnd - 1;
- if (methodDeclaration->mBody != NULL)
- Add(anchorIdx, methodDeclaration->mBody->mSrcEnd - 1, 'M');
- BfElementVisitor::Visit(methodDeclaration);
- }
- virtual void Visit(BfNamespaceDeclaration* namespaceDeclaration) override
- {
- Add(namespaceDeclaration->mNamespaceNode, namespaceDeclaration->mBody, 'N');
- BfElementVisitor::Visit(namespaceDeclaration);
- }
- virtual void Visit(BfUsingDirective* usingDirective) override
- {
- UpdateSeries(usingDirective, 'U');
- BfElementVisitor::Visit(usingDirective);
- }
- virtual void Visit(BfTypeDeclaration* typeDeclaration) override
- {
- BfAstNode* anchor = typeDeclaration->mNameNode;
- if (anchor == NULL)
- anchor = typeDeclaration->mStaticSpecifier;
- Add(anchor, typeDeclaration->mDefineNode, 'T');
- BfElementVisitor::Visit(typeDeclaration);
- }
- virtual void Visit(BfPropertyDeclaration* properyDeclaration) override
- {
- Add(properyDeclaration->mNameNode, properyDeclaration->mDefinitionBlock, 'P');
- BfElementVisitor::Visit(properyDeclaration);
- }
- virtual void Visit(BfIndexerDeclaration* indexerDeclaration) override
- {
- Add(indexerDeclaration->mThisToken, indexerDeclaration->mDefinitionBlock, 'P');
- BfElementVisitor::Visit(indexerDeclaration);
- }
- virtual void Visit(BfPropertyMethodDeclaration* methodDeclaration) override
- {
- Add(methodDeclaration->mNameNode, methodDeclaration->mBody);
- BfElementVisitor::Visit(methodDeclaration);
- }
- virtual void Visit(BfIfStatement* ifStatement) override
- {
- Add(ifStatement->mCloseParen, ifStatement->mTrueStatement);
- if (auto elseBlock = BfNodeDynCast<BfBlock>(ifStatement->mFalseStatement))
- Add(ifStatement->mElseToken, elseBlock);
- BfElementVisitor::Visit(ifStatement);
- }
- virtual void Visit(BfRepeatStatement* repeatStatement) override
- {
- Add(repeatStatement->mRepeatToken, repeatStatement);
- if (repeatStatement->mEmbeddedStatement != NULL)
- mEndsFound.Add(repeatStatement->mEmbeddedStatement->mSrcEnd - 1);
- BfElementVisitor::Visit(repeatStatement);
- }
- virtual void Visit(BfWhileStatement* whileStatement) override
- {
- Add(whileStatement->mCloseParen, whileStatement->mEmbeddedStatement);
- BfElementVisitor::Visit(whileStatement);
- }
- virtual void Visit(BfDoStatement* doStatement) override
- {
- Add(doStatement->mDoToken, doStatement->mEmbeddedStatement);
- BfElementVisitor::Visit(doStatement);
- }
- virtual void Visit(BfForStatement* forStatement) override
- {
- Add(forStatement->mCloseParen, forStatement->mEmbeddedStatement);
- BfElementVisitor::Visit(forStatement);
- }
- virtual void Visit(BfForEachStatement* forStatement) override
- {
- Add(forStatement->mCloseParen, forStatement->mEmbeddedStatement);
- BfElementVisitor::Visit(forStatement);
- }
- virtual void Visit(BfUsingStatement* usingStatement) override
- {
- Add(usingStatement->mUsingToken, usingStatement->mEmbeddedStatement);
- BfElementVisitor::Visit(usingStatement);
- }
- virtual void Visit(BfSwitchStatement* switchStatement) override
- {
- Add(switchStatement->mOpenParen, switchStatement->mCloseBrace);
- BfElementVisitor::Visit(switchStatement);
- }
- virtual void Visit(BfLambdaBindExpression* lambdaExpression) override
- {
- Add(lambdaExpression->mFatArrowToken, lambdaExpression->mBody);
- BfElementVisitor::Visit(lambdaExpression);
- }
- virtual void Visit(BfBlock* block) override
- {
- Add(block->mOpenBrace, block->mCloseBrace);
- BfElementVisitor::Visit(block);
- }
- virtual void Visit(BfInitializerExpression* initExpr) override
- {
- Add(initExpr->mOpenBrace, initExpr->mCloseBrace);
- BfElementVisitor::Visit(initExpr);
- }
- virtual void Visit(BfInvocationExpression* invocationExpr) override
- {
- Add(invocationExpr->mOpenParen, invocationExpr->mCloseParen, '?', 3);
- BfElementVisitor::Visit(invocationExpr);
- }
- };
- CollapseVisitor collapseVisitor(bfParser, outString);
- collapseVisitor.VisitChild(bfParser->mRootNode);
- BfAstNode* condStart = NULL;
- BfAstNode* regionStart = NULL;
- BfPreprocessorNode* prevPreprocessorNode = NULL;
- int ignoredSectionStart = -1;
- if (bfParser->mSidechannelRootNode != NULL)
- {
- for (auto element : bfParser->mSidechannelRootNode->mChildArr)
- {
- if (auto preprocessorNode = BfNodeDynCast<BfPreprocessorNode>(element))
- {
- if ((ignoredSectionStart != -1) && (prevPreprocessorNode != NULL) && (prevPreprocessorNode->mCommand != NULL))
- {
- collapseVisitor.Add(prevPreprocessorNode->mCommand->mSrcStart, preprocessorNode->mSrcEnd - 1);
- ignoredSectionStart = -1;
- }
- StringView sv = preprocessorNode->mCommand->ToStringView();
- if (sv == "region")
- regionStart = preprocessorNode->mCommand;
- else if (sv == "endregion")
- {
- if (regionStart != NULL)
- collapseVisitor.Add(regionStart->mSrcStart, preprocessorNode->mCommand->mSrcStart, 'R');
- regionStart = NULL;
- }
- else if (sv == "if")
- {
- condStart = preprocessorNode->mCommand;
- }
- else if (sv == "endif")
- {
- if (condStart != NULL)
- collapseVisitor.Add(condStart->mSrcStart, preprocessorNode->mCommand->mSrcStart);
- condStart = NULL;
- }
- else if ((sv == "else") || (sv == "elif"))
- {
- if (condStart != NULL)
- collapseVisitor.Add(condStart->mSrcStart, collapseVisitor.GetLineEndBefore(preprocessorNode->mSrcStart));
- condStart = preprocessorNode->mCommand;
- }
- prevPreprocessorNode = preprocessorNode;
- }
- if (auto preprocessorNode = BfNodeDynCast<BfPreprocesorIgnoredSectionNode>(element))
- {
- if (ignoredSectionStart == -1)
- {
- for (int i = preprocessorNode->mSrcStart; i < preprocessorNode->mSrcEnd - 1; i++)
- {
- if (bfParser->mSrc[i] == '\n')
- {
- ignoredSectionStart = i + 1;
- break;
- }
- }
- }
- }
- char kind = 0;
- if (auto commentNode = BfNodeDynCast<BfCommentNode>(element))
- {
- collapseVisitor.UpdateSeries(commentNode, 'C');
- }
- }
- }
- collapseVisitor.FlushSeries();
- Array<BfTypeInstance*> explicitEmitTypes;
- String checkStr = explicitEmitTypeNames;
- for (auto typeName : checkStr.Split('\n'))
- {
- if (typeName.IsEmpty())
- continue;
- auto bfType = bfCompiler->GetType(typeName);
- if ((bfType != NULL) && (bfType->IsTypeInstance()))
- explicitEmitTypes.Add(bfType->ToTypeInstance());
- }
- // Embed emit info
- BfPassInstance bfPassInstance(bfCompiler->mSystem);
- SetAndRestoreValue<BfResolvePassData*> prevCompilerResolvePassData(bfCompiler->mResolvePassData, resolvePassData);
- SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, &bfPassInstance);
- HashSet<BfTypeDef*> typeHashSet;
- for (auto typeDef : bfParser->mTypeDefs)
- typeHashSet.Add(typeDef);
- Dictionary<int, int> foundTypeIds;
- struct _EmitSource
- {
- BfParser* mEmitParser;
- bool mIsPrimary;
- };
- Dictionary<BfTypeDef*, Dictionary<int, _EmitSource>> emitLocMap;
- bool mayHaveMissedEmits = false;
- for (auto type : bfCompiler->mContext->mResolvedTypes)
- {
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeHashSet.Contains(typeInst->mTypeDef->GetDefinition()->GetLatest()))
- {
- if (typeInst->mCeTypeInfo == NULL)
- continue;
- if ((typeInst->mCeTypeInfo->mMayHaveUniqueEmitLocations) && (!mayHaveMissedEmits))
- {
- outString += "~\n";
- mayHaveMissedEmits = true;
- }
- for (auto& kv : typeInst->mCeTypeInfo->mEmitSourceMap)
- {
- int partialIdx = (int)(kv.mKey >> 32);
- int charIdx = (int)(kv.mKey & 0xFFFFFFFF);
- auto typeDef = typeInst->mTypeDef;
- if (partialIdx > 0)
- typeDef = typeDef->mPartials[partialIdx];
- auto emitParser = typeInst->mTypeDef->GetLastSource()->ToParser();
- Dictionary<int, _EmitSource>* map = NULL;
- emitLocMap.TryAdd(typeDef->GetDefinition()->GetLatest(), NULL, &map);
- _EmitSource emitSource;
- emitSource.mEmitParser = emitParser;
- emitSource.mIsPrimary = false;
- (*map)[charIdx] = emitSource;
- }
- }
- }
- for (auto typeDef : bfParser->mTypeDefs)
- {
- auto useTypeDef = typeDef;
- if (useTypeDef->mIsPartial)
- {
- useTypeDef = bfCompiler->mSystem->GetCombinedPartial(useTypeDef);
- if (useTypeDef == NULL)
- continue;
- }
- auto _GetTypeEmbedId = [&](BfTypeInstance* typeInst, BfParser* emitParser)
- {
- int* keyPtr = NULL;
- int* valuePtr = NULL;
- if (foundTypeIds.TryAdd(typeInst->mTypeId, &keyPtr, &valuePtr))
- {
- *valuePtr = foundTypeIds.mCount - 1;
- outString += "+";
- if ((emitParser == NULL) || (!emitParser->mIsEmitted))
- {
- outString += bfCompiler->mContext->mScratchModule->TypeToString(typeInst, BfTypeNameFlag_AddProjectName);
- }
- else
- {
- int dollarPos = (int)emitParser->mFileName.LastIndexOf('$');
- if (dollarPos != -1)
- outString += emitParser->mFileName.Substring(dollarPos + 1);
- }
- outString += "\n";
- }
- return *valuePtr;
- };
- auto type = bfCompiler->mContext->mScratchModule->ResolveTypeDef(useTypeDef);
- if (type == NULL)
- continue;
- if (auto typeInst = type->ToTypeInstance())
- {
- auto origTypeInst = typeInst;
- for (auto checkIdx = explicitEmitTypes.mSize - 1; checkIdx >= 0; checkIdx--)
- {
- auto checkType = explicitEmitTypes[checkIdx];
- if (checkType->mTypeDef->GetDefinition()->GetLatest() == typeInst->mTypeDef->GetDefinition()->GetLatest())
- {
- typeInst = checkType;
- bfCompiler->mContext->mScratchModule->PopulateType(typeInst);
- break;
- }
- }
- if (typeInst->mCeTypeInfo != NULL)
- {
- for (auto& kv : typeInst->mCeTypeInfo->mEmitSourceMap)
- {
- int partialIdx = (int)(kv.mKey >> 32);
- int charIdx = (int)(kv.mKey & 0xFFFFFFFF);
- auto typeDef = typeInst->mTypeDef;
- if (partialIdx > 0)
- typeDef = typeDef->mPartials[partialIdx];
- auto parser = typeDef->GetDefinition()->GetLastSource()->ToParser();
- if (parser == NULL)
- continue;
- if (!FileNameEquals(parser->mFileName, bfParser->mFileName))
- continue;
- auto bfParser = typeDef->GetLastSource()->ToParser();
- auto emitParser = typeInst->mTypeDef->GetLastSource()->ToParser();
- if (emitParser == NULL)
- continue;
- Dictionary<int, _EmitSource>* map = NULL;
- if (emitLocMap.TryGetValue(typeDef->GetDefinition(), &map))
- {
- _EmitSource emitSource;
- emitSource.mEmitParser = emitParser;
- emitSource.mIsPrimary = true;
- (*map)[charIdx] = emitSource;
- }
- int startLine = 0;
- int startLineChar = 0;
- if (kv.mValue.mSrcStart >= 0)
- emitParser->GetLineCharAtIdx(kv.mValue.mSrcStart, startLine, startLineChar);
- int srcEnd = kv.mValue.mSrcEnd - 1;
- if (srcEnd >= emitParser->mOrigSrcLength)
- continue;
- while (srcEnd >= kv.mValue.mSrcStart)
- {
- char c = emitParser->mSrc[srcEnd];
- if (!::isspace((uint8)c))
- break;
- srcEnd--;
- }
- int embedId = _GetTypeEmbedId(typeInst, emitParser);
- if (embedId == -1)
- continue;
- int endLine = 0;
- int endLineChar = 0;
- if (srcEnd >= 0)
- emitParser->GetLineCharAtIdx(srcEnd, endLine, endLineChar);
- int textVersion = -1;
- auto declParser = typeInst->mTypeDef->mTypeDeclaration->GetParser();
- if (declParser != NULL)
- textVersion = declParser->mTextVersion;
- char emitChar = (kv.mValue.mKind == BfCeTypeEmitSourceKind_Type) ? 't' : 'm';
- outString += StrFormat("%c%d,%d,%d,%d,%d\n", emitChar, embedId, charIdx, startLine, endLine + 1, textVersion);
- }
- }
- if (typeInst->IsGenericTypeInstance())
- {
- String emitName;
- auto _CheckTypeDef = [&](BfTypeDef* typeDef)
- {
- auto parser = typeDef->GetDefinition()->GetLastSource()->ToParser();
- if (parser == NULL)
- return;
- if (!FileNameEquals(parser->mFileName, bfParser->mFileName))
- return;
- Dictionary<int, _EmitSource>* map = NULL;
- if (emitLocMap.TryGetValue(typeDef, &map))
- {
- for (auto& kv : *map)
- {
- if (kv.mValue.mIsPrimary)
- continue;
- int embedId = _GetTypeEmbedId(typeInst, NULL);
- if (embedId == -1)
- continue;
- char emitChar = 't';
- outString += StrFormat("%c%d,%d,%d,%d,%d\n", emitChar, embedId, kv.mKey, 0, 0, -1);
- }
- }
- };
- _CheckTypeDef(typeInst->mTypeDef->GetDefinition());
- for (auto partialTypeDef : typeInst->mTypeDef->mPartials)
- _CheckTypeDef(partialTypeDef);
- }
- }
- }
- return outString.c_str();
- }
- BF_EXPORT bool BF_CALLTYPE BfCompiler_VerifyTypeName(BfCompiler* bfCompiler, char* name, int cursorPos)
- {
- bfCompiler->mSystem->AssertWeHaveLock();
- String typeName = name;
- auto system = bfCompiler->mSystem;
- AutoCrit autoCrit(system->mSystemLock);
- String& autoCompleteResultString = *gTLStrReturn.Get();
- autoCompleteResultString.Clear();
- BfPassInstance passInstance(bfCompiler->mSystem);
- BfParser parser(bfCompiler->mSystem);
- parser.SetSource(typeName.c_str(), (int)typeName.length());
- parser.Parse(&passInstance);
- parser.mCursorIdx = cursorPos;
- parser.mCursorCheckIdx = cursorPos;
- BfReducer reducer;
- reducer.mAlloc = parser.mAlloc;
- reducer.mPassInstance = &passInstance;
- reducer.mAllowTypeWildcard = true;
- if (parser.mRootNode->mChildArr.mSize == 0)
- return false;
- bool attribWasClosed = false;
- bool isAttributeRef = false;
- auto firstNode = parser.mRootNode->mChildArr[0];
- auto endIdx = parser.mRootNode->mSrcEnd;
- reducer.mVisitorPos = BfReducer::BfVisitorPos(parser.mRootNode);
- if (auto tokenNode = BfNodeDynCast<BfTokenNode>(firstNode))
- {
- if (tokenNode->mToken == BfToken_LBracket)
- {
- if (auto lastToken = BfNodeDynCast<BfTokenNode>(parser.mRootNode->mChildArr.back()))
- {
- if (lastToken->mToken == BfToken_RBracket)
- {
- attribWasClosed = true;
- endIdx = lastToken->mSrcStart;
- }
- }
- isAttributeRef = true;
- if (parser.mRootNode->mChildArr.mSize < 2)
- return false;
- firstNode = parser.mRootNode->mChildArr[1];
- reducer.mVisitorPos.MoveNext();
- }
- }
- reducer.mVisitorPos.MoveNext();
- auto typeRef = reducer.CreateTypeRef(firstNode);
- if (typeRef == NULL)
- return false;
- BfResolvePassData resolvePassData;
- if (cursorPos != -1)
- {
- resolvePassData.mResolveType = BfResolveType_Autocomplete;
- parser.mParserFlags = (BfParserFlag)(parser.mParserFlags | ParserFlag_Autocomplete);
- resolvePassData.mAutoComplete = new BfAutoComplete();
- resolvePassData.mAutoComplete->mResolveType = BfResolveType_VerifyTypeName;
- resolvePassData.mAutoComplete->mSystem = bfCompiler->mSystem;
- resolvePassData.mAutoComplete->mCompiler = bfCompiler;
- resolvePassData.mAutoComplete->mModule = bfCompiler->mContext->mScratchModule;
- }
- resolvePassData.mParsers.Add(&parser);
- SetAndRestoreValue<BfResolvePassData*> prevCompilerResolvePassData(bfCompiler->mResolvePassData, &resolvePassData);
- SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, &passInstance);
- if (resolvePassData.mAutoComplete != NULL)
- {
- if (isAttributeRef)
- resolvePassData.mAutoComplete->CheckAttributeTypeRef(typeRef);
- else
- resolvePassData.mAutoComplete->CheckTypeRef(typeRef, false);
- bfCompiler->GenerateAutocompleteInfo();
- }
- if (passInstance.HasFailed())
- return false;
- if (typeRef->mSrcEnd != endIdx)
- return false;
- if (!bfCompiler->mContext->mScratchModule->ValidateTypeWildcard(typeRef, isAttributeRef))
- return false;
- if ((isAttributeRef) && (!attribWasClosed))
- return false;
- return true;
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_ClearCompletionPercentage(BfCompiler* bfCompiler)
- {
- bfCompiler->mCompletionPct = 0;
- }
- BF_EXPORT float BF_CALLTYPE BfCompiler_GetCompletionPercentage(BfCompiler* bfCompiler)
- {
- return bfCompiler->mCompletionPct;
- }
- BF_EXPORT int BF_CALLTYPE BfCompiler_GetCompileRevision(BfCompiler* bfCompiler)
- {
- return bfCompiler->mRevision;
- }
- BF_EXPORT int BF_CALLTYPE BfCompiler_GetCurConstEvalExecuteId(BfCompiler* bfCompiler)
- {
- return bfCompiler->mCurCEExecuteId;
- }
- BF_EXPORT float BF_CALLTYPE BfCompiler_GetLastHadComptimeRebuilds(BfCompiler* bfCompiler)
- {
- return bfCompiler->mLastHadComptimeRebuilds;
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_Cancel(BfCompiler* bfCompiler)
- {
- bfCompiler->Cancel();
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_RequestFastFinish(BfCompiler* bfCompiler)
- {
- bfCompiler->RequestFastFinish();
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_ClearBuildCache(BfCompiler* bfCompiler)
- {
- bfCompiler->ClearBuildCache();
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_SetBuildValue(BfCompiler* bfCompiler, char* cacheDir, char* key, char* value)
- {
- bfCompiler->mCodeGen.SetBuildValue(cacheDir, key, value);
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetBuildValue(BfCompiler* bfCompiler, char* cacheDir, char* key)
- {
- String& outString = *gTLStrReturn.Get();
- outString = bfCompiler->mCodeGen.GetBuildValue(cacheDir, key);
- return outString.c_str();
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_WriteBuildCache(BfCompiler* bfCompiler, char* cacheDir)
- {
- bfCompiler->mCodeGen.WriteBuildCache(cacheDir);
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_Delete(BfCompiler* bfCompiler)
- {
- delete bfCompiler;
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_ProgramDone()
- {
- #ifdef BF_PLATFORM_WINDOWS
- BeLibManager::Get()->Clear();
- #endif
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetGeneratorTypeDefList(BfCompiler* bfCompiler)
- {
- String& outString = *gTLStrReturn.Get();
- outString.clear();
- outString = bfCompiler->GetGeneratorTypeDefList();
- return outString.c_str();
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetGeneratorInitData(BfCompiler* bfCompiler, char* typeDefName, char* args)
- {
- String& outString = *gTLStrReturn.Get();
- outString.clear();
- outString = bfCompiler->GetGeneratorInitData(typeDefName, args);
- return outString.c_str();
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetGeneratorGenData(BfCompiler* bfCompiler, char* typeDefName, char* args)
- {
- String& outString = *gTLStrReturn.Get();
- outString.clear();
- outString = bfCompiler->GetGeneratorGenData(typeDefName, args);
- return outString.c_str();
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetTypeDefList(BfCompiler* bfCompiler, bool includeLocation)
- {
- String& outString = *gTLStrReturn.Get();
- outString.clear();
- outString = bfCompiler->GetTypeDefList(includeLocation);
- return outString.c_str();
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetTypeDefMatches(BfCompiler* bfCompiler, const char* searchStr, bool includeLocation)
- {
- String& outString = *gTLStrReturn.Get();
- outString.clear();
- outString = bfCompiler->GetTypeDefMatches(searchStr, includeLocation);
- return outString.c_str();
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetTypeDefInfo(BfCompiler* bfCompiler, const char* typeDefName)
- {
- String& outString = *gTLStrReturn.Get();
- outString.clear();
- outString = bfCompiler->GetTypeDefInfo(typeDefName);
- return outString.c_str();
- }
- BF_EXPORT int BF_CALLTYPE BfCompiler_GetTypeId(BfCompiler* bfCompiler, const char* name)
- {
- return bfCompiler->GetTypeId(name);
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetTypeInfo(BfCompiler* bfCompiler, const char* name)
- {
- bfCompiler->mSystem->AssertWeHaveLock();
- String& outString = *gTLStrReturn.Get();
- outString = "";
- String typeName = name;
- auto system = bfCompiler->mSystem;
- AutoCrit autoCrit(system->mSystemLock);
- String& autoCompleteResultString = *gTLStrReturn.Get();
- autoCompleteResultString.Clear();
- BfPassInstance passInstance(bfCompiler->mSystem);
- BfParser parser(bfCompiler->mSystem);
- parser.SetSource(typeName.c_str(), (int)typeName.length());
- parser.Parse(&passInstance);
- BfReducer reducer;
- reducer.mAlloc = parser.mAlloc;
- reducer.mPassInstance = &passInstance;
- reducer.mAllowTypeWildcard = true;
- if (parser.mRootNode->mChildArr.mSize == 0)
- return "";
- bool attribWasClosed = false;
- bool isAttributeRef = false;
- auto firstNode = parser.mRootNode->mChildArr[0];
- auto endIdx = parser.mRootNode->mSrcEnd;
- reducer.mVisitorPos = BfReducer::BfVisitorPos(parser.mRootNode);
- if (auto tokenNode = BfNodeDynCast<BfTokenNode>(firstNode))
- {
- if (tokenNode->mToken == BfToken_LBracket)
- {
- if (auto lastToken = BfNodeDynCast<BfTokenNode>(parser.mRootNode->mChildArr.back()))
- {
- if (lastToken->mToken == BfToken_RBracket)
- {
- attribWasClosed = true;
- endIdx = lastToken->mSrcStart;
- }
- }
- isAttributeRef = true;
- if (parser.mRootNode->mChildArr.mSize < 2)
- return "";
- firstNode = parser.mRootNode->mChildArr[1];
- reducer.mVisitorPos.MoveNext();
- }
- }
- reducer.mVisitorPos.MoveNext();
- auto typeRef = reducer.CreateTypeRef(firstNode);
- if (typeRef == NULL)
- return "";
- BfResolvePassData resolvePass;
- SetAndRestoreValue<bool> prevIgnoreError(bfCompiler->mContext->mScratchModule->mIgnoreErrors, true);
- SetAndRestoreValue<bool> prevIgnoreWarnings(bfCompiler->mContext->mScratchModule->mIgnoreWarnings, true);
- SetAndRestoreValue<BfResolvePassData*> prevResolvePass(bfCompiler->mResolvePassData, &resolvePass);
- auto type = bfCompiler->mContext->mScratchModule->ResolveTypeRef(typeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_NoCreate);
- if (type != NULL)
- {
- bfCompiler->mContext->mScratchModule->PopulateType(type);
- outString += "Found";
- if (auto typeInst = type->ToTypeInstance())
- {
- if (typeInst->mIsReified)
- outString += " Reified";
- if (typeInst->HasBeenInstantiated())
- outString += " Instantiated";
- if (typeInst->mTypeFailed)
- outString += " TypeFailed";
- if (auto genericTypeInst = typeInst->ToGenericTypeInstance())
- {
- if (genericTypeInst->mGenericTypeInfo->mHadValidateErrors)
- outString += " ValidateErrors";
- }
- }
- }
- return outString.c_str();
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetGenericTypeInstances(BfCompiler* bfCompiler, const char* typeName)
- {
- String& outString = *gTLStrReturn.Get();
- outString = "";
- String checkTypeName = typeName;
- auto lookupType = bfCompiler->GetType(checkTypeName);
- if (lookupType == NULL)
- {
- // Convert potentially-specialized type name into an unspecialized type name
- int chevronDepth = 0;
- int chevronStart = -1;
- for (int i = 0; i < (int)checkTypeName.mLength; i++)
- {
- char c = checkTypeName[i];
- if (c == '<')
- {
- if (chevronDepth == 0)
- chevronStart = i;
- chevronDepth++;
- }
- else if (c == '>')
- {
- chevronDepth--;
- if (chevronDepth == 0)
- {
- checkTypeName.Remove(chevronStart + 1, i - chevronStart - 1);
- i = chevronStart + 1;
- }
- }
- }
- lookupType = bfCompiler->GetType(checkTypeName);
- if (lookupType == NULL)
- return "";
- }
- auto lookupTypeInst = lookupType->ToTypeInstance();
- if (lookupTypeInst == NULL)
- return "";
- for (auto type : bfCompiler->mContext->mResolvedTypes)
- {
- auto typeInst = type->ToTypeInstance();
- if (typeInst == NULL)
- continue;
- if (typeInst->IsUnspecializedTypeVariation())
- continue;
- if (typeInst->mTypeDef->GetDefinition()->GetLatest() == lookupTypeInst->mTypeDef->GetDefinition()->GetLatest())
- {
- outString += bfCompiler->mContext->mScratchModule->TypeToString(typeInst, BfTypeNameFlag_AddProjectName);
- outString += "\n";
- }
- }
- return outString.c_str();
- }
- enum BfUsedOutputFlags
- {
- BfUsedOutputFlags_None = 0,
- BfUsedOutputFlags_FlushQueuedHotFiles = 1,
- BfUsedOutputFlags_SkipImports = 2,
- };
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetUsedOutputFileNames(BfCompiler* bfCompiler, BfProject* bfProject, BfUsedOutputFlags flags, bool* hadOutputChanges)
- {
- bfCompiler->mSystem->AssertWeHaveLock();
- BP_ZONE("BfCompiler_GetUsedOutputFileNames");
- *hadOutputChanges = false;
- String& outString = *gTLStrReturn.Get();
- outString.clear();
- Array<BfModule*> moduleList;
- moduleList.Reserve(bfProject->mUsedModules.size());
- if (bfCompiler->mOptions.mCompileOnDemandKind == BfCompileOnDemandKind_AlwaysInclude)
- {
- for (auto mainModule : bfCompiler->mContext->mModules)
- {
- if ((!mainModule->mIsReified) || (mainModule->mIsScratchModule))
- continue;
- if (bfCompiler->mOptions.mHotProject != NULL)
- continue; // Only add new objs from mCodeGen.mCodeGenFiles during hot reload
- if (!bfCompiler->IsModuleAccessible(mainModule, bfProject))
- continue;
- moduleList.push_back(mainModule);
- }
- }
- else
- {
- for (auto mainModule : bfProject->mUsedModules)
- {
- if ((!mainModule->mIsReified) || (mainModule->mIsScratchModule))
- continue;
- if (bfCompiler->mOptions.mHotProject != NULL)
- continue; // Only add new objs from mCodeGen.mCodeGenFiles during hot reload
- moduleList.push_back(mainModule);
- }
- }
- std::sort(moduleList.begin(), moduleList.end(), [&](BfModule* moduleA, BfModule* moduleB) { return moduleA->mModuleName < moduleB->mModuleName; } );
- HashSet<String> usedFileNames;
- usedFileNames.Reserve(moduleList.size());
- //for (auto mainModule : moduleList)
- for (int i = 0; i < moduleList.mSize; i++)
- {
- auto mainModule = moduleList[i];
- BF_ASSERT_REL(!mainModule->mIsDeleting);
- BF_ASSERT_REL((mainModule->mRevision > -2) && (mainModule->mRevision < 1000000));
- if ((flags & BfUsedOutputFlags_SkipImports) == 0)
- {
- for (auto fileNameIdx : mainModule->mImportFileNames)
- {
- auto fileName = bfCompiler->mContext->mStringObjectIdMap[fileNameIdx].mString;
- if (!usedFileNames.TryAdd(fileName, NULL))
- continue;
- if (!outString.empty())
- outString += "\n";
- outString += fileName;
- }
- }
- for (auto& moduleFileName : mainModule->mOutFileNames)
- {
- if (!moduleFileName.mModuleWritten)
- continue;
- bool canReference = true;
- for (auto project : moduleFileName.mProjects)
- {
- if (!bfProject->ContainsReference(project))
- canReference = false;
- if (bfProject != project)
- {
- //TODO: What was this for?
- // if (project->mTargetType == BfTargetType_BeefDynLib)
- // canReference = false;
- }
- }
- if (!canReference)
- continue;
- String fileName = moduleFileName.mFileName;
- #ifdef BF_PLATFORM_WINDOWS
- if (moduleFileName.mWroteToLib)
- fileName = BeLibManager::GetLibFilePath(fileName);
- #endif
- if (!usedFileNames.TryAdd(fileName, NULL))
- continue;
- if (!outString.empty())
- outString += "\n";
- outString += fileName;
- }
- }
- if (bfCompiler->mHotState != NULL)
- {
- Array<String> outPaths;
- for (int i = 0; i < (int)bfCompiler->mHotState->mQueuedOutFiles.size(); i++)
- {
- auto& fileEntry = bfCompiler->mHotState->mQueuedOutFiles[i];
- if (fileEntry.mProject != bfProject)
- continue;
- if (!bfCompiler->mHotState->mHotProject->mUsedModules.Contains(fileEntry.mModule))
- continue;
- outPaths.Add(fileEntry.mFileName);
- if ((flags & BfUsedOutputFlags_FlushQueuedHotFiles) != 0)
- {
- bfCompiler->mHotState->mQueuedOutFiles.RemoveAtFast(i);
- i--;
- }
- }
- std::sort(outPaths.begin(), outPaths.end(), [](const String& lhs, const String& rhs) { return lhs < rhs; });
- for (auto& path : outPaths)
- {
- if (!outString.empty())
- outString += "\n";
- outString += path;
- outString += BF_OBJ_EXT;
- }
- }
- for (auto& fileEntry : bfCompiler->mCodeGen.mCodeGenFiles)
- {
- if (fileEntry.mWasCached)
- continue;
- if (!bfProject->ContainsReference(fileEntry.mProject))
- continue;
- *hadOutputChanges = true;
- }
- return outString.c_str();
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetAutocompleteInfo(BfCompiler* bfCompiler)
- {
- String& autoCompleteResultString = *gTLStrReturn.Get();
- return autoCompleteResultString.c_str();
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetRebuildFileSet(BfCompiler* bfCompiler)
- {
- String& autoCompleteResultString = *gTLStrReturn.Get();
- for (auto& val : bfCompiler->mRebuildFileSet)
- {
- autoCompleteResultString += val;
- autoCompleteResultString += "\n";
- }
- return autoCompleteResultString.c_str();
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_FileChanged(BfCompiler* bfCompiler, const char* dirPath)
- {
- bfCompiler->mRebuildChangedFileSet.Add(dirPath);
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetSymbolReferences(BfCompiler* bfCompiler, BfPassInstance* bfPassInstance, BfResolvePassData* resolvePassData)
- {
- bfCompiler->mSystem->AssertWeHaveLock();
- BP_ZONE("BfCompiler_GetSymbolReferences");
- String& outString = *gTLStrReturn.Get();
- outString.clear();
- SetAndRestoreValue<BfResolvePassData*> prevCompilerResolvePassData(bfCompiler->mResolvePassData, resolvePassData);
- SetAndRestoreValue<BfPassInstance*> prevPassInstance(bfCompiler->mPassInstance, bfPassInstance);
- bfCompiler->GetSymbolReferences();
- std::map<String, String*> sortedParserMap;
- for (auto& parserDataPair : resolvePassData->mFoundSymbolReferencesParserData)
- {
- if (!parserDataPair.mKey->mFileName.Contains('|'))
- sortedParserMap.insert(std::make_pair(parserDataPair.mKey->mFileName, &parserDataPair.mValue));
- }
- for (auto& parserData : sortedParserMap)
- {
- if (!outString.empty())
- outString += "\n";
- outString += parserData.first + "\t" + *(parserData.second);
- }
- return outString.c_str();
- }
- BF_EXPORT bool BF_CALLTYPE BfCompiler_GetHasHotPendingDataChanges(BfCompiler* bfCompiler)
- {
- return (bfCompiler->mHotState != NULL) &&
- ((!bfCompiler->mHotState->mPendingDataChanges.IsEmpty()) || (!bfCompiler->mHotState->mPendingFailedSlottings.IsEmpty()));
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_HotCommit(BfCompiler* bfCompiler)
- {
- bfCompiler->HotCommit();
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_Start(BfCompiler* bfCompiler, int flags)
- {
- bfCompiler->HotResolve_Start((BfCompiler::HotResolveFlags)flags);
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_AddActiveMethod(BfCompiler* bfCompiler, const char* methodName)
- {
- bfCompiler->HotResolve_AddActiveMethod(methodName);
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_AddDelegateMethod(BfCompiler* bfCompiler, const char* methodName)
- {
- bfCompiler->HotResolve_AddDelegateMethod(methodName);
- }
- // 0: heap, 1: user stated 'not used', 2: user stated 'used'
- BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_ReportType(BfCompiler* bfCompiler, int typeId, int usageKind)
- {
- bfCompiler->HotResolve_ReportType(typeId, (BfCompiler::HotTypeFlags)usageKind);
- }
- // 0: heap, 1: user stated 'used', 2: user stated 'not used'
- BF_EXPORT void BF_CALLTYPE BfCompiler_HotResolve_ReportTypeRange(BfCompiler* bfCompiler, const char* typeName, int usageKind)
- {
- //TODO: Implement
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_HotResolve_Finish(BfCompiler* bfCompiler)
- {
- String& outString = *gTLStrReturn.Get();
- outString = bfCompiler->HotResolve_Finish();
- return outString.c_str();
- }
- static BfPlatformType GetPlatform(StringView str)
- {
- while (!str.IsEmpty())
- {
- char c = str[str.mLength - 1];
- if (((c >= '0') && (c <= '9')) || (c == '.'))
- str.RemoveFromEnd(1);
- else
- break;
- }
- bool hasLinux = false;
- for (auto elem : str.Split('-'))
- {
- if (elem == "linux")
- hasLinux = true;
- else if (elem == "windows")
- return BfPlatformType_Windows;
- else if (elem == "macosx")
- return BfPlatformType_macOS;
- else if (elem == "ios")
- return BfPlatformType_iOS;
- else if ((elem == "android") || (elem == "androideabi"))
- return BfPlatformType_Android;
- else if ((elem == "wasm32") || (elem == "wasm64"))
- return BfPlatformType_Wasm;
- }
- if (hasLinux)
- return BfPlatformType_Linux;
- return BfPlatformType_Unknown;
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_SetOptions(BfCompiler* bfCompiler, BfProject* hotProject, int hotIdx,
- const char* targetTriple, const char* targetCPU, int toolsetType, int simdSetting, int allocStackCount, int maxWorkerThreads,
- BfCompilerOptionFlags optionFlags, char* mallocLinkName, char* freeLinkName)
- {
- BfLogSys(bfCompiler->mSystem, "BfCompiler_SetOptions\n");
- //printf("BfCompiler_SetOptions Threads:%d\n", maxWorkerThreads);
- auto options = &bfCompiler->mOptions;
- options->mErrorString.Clear();
- options->mHotProject = hotProject;
- options->mHotCompileIdx = hotIdx;
- options->mTargetTriple = targetTriple;
- options->mTargetCPU = targetCPU;
- if (options->mTargetTriple.StartsWith("x86_64-"))
- options->mMachineType = BfMachineType_x64;
- else if (options->mTargetTriple.StartsWith("i686-"))
- options->mMachineType = BfMachineType_x86;
- else if ((options->mTargetTriple.StartsWith("arm64")) || (options->mTargetTriple.StartsWith("aarch64")))
- options->mMachineType = BfMachineType_AArch64;
- else if (options->mTargetTriple.StartsWith("armv"))
- options->mMachineType = BfMachineType_ARM;
- else if (options->mTargetTriple.StartsWith("wasm32"))
- options->mMachineType = BfMachineType_Wasm32;
- else if (options->mTargetTriple.StartsWith("wasm64"))
- options->mMachineType = BfMachineType_Wasm64;
- else
- options->mMachineType = BfMachineType_x64; // Default
- options->mPlatformType = GetPlatform(options->mTargetTriple);
- options->mCLongSize = 4;
- if ((options->mMachineType == BfMachineType_AArch64) || (options->mMachineType == BfMachineType_x64))
- {
- if (options->mPlatformType != BfPlatformType_Windows)
- options->mCLongSize = 8;
- }
- bfCompiler->mCodeGen.SetMaxThreads(maxWorkerThreads);
- if (!bfCompiler->mIsResolveOnly)
- {
- bool allowHotSwapping = (optionFlags & BfCompilerOptionFlag_EnableHotSwapping) != 0;
- bool emitDebugInfo = (optionFlags & BfCompilerOptionFlag_EmitDebugInfo) != 0;
- // These settings only matter for code generation, they are not applicable for resolveOnly
- options->mCompileOnDemandKind = BfCompileOnDemandKind_ResolveUnused;
- //options->mCompileOnDemandKind = BfCompileOnDemandKind_AlwaysInclude;
- options->mToolsetType = (BfToolsetType)toolsetType;
- options->mSIMDSetting = (BfSIMDSetting)simdSetting;
- options->mIncrementalBuild = (optionFlags & BfCompilerOptionFlag_IncrementalBuild) != 0;
- options->mWriteIR = (optionFlags & BfCompilerOptionFlag_WriteIR) != 0;
- options->mGenerateObj = (optionFlags & BfCompilerOptionFlag_GenerateOBJ) != 0;
- options->mGenerateBitcode = (optionFlags & BfCompilerOptionFlag_GenerateBitcode) != 0;
- options->mNoFramePointerElim = (optionFlags & BfCompilerOptionFlag_NoFramePointerElim) != 0;
- options->mInitLocalVariables = (optionFlags & BfCompilerOptionFlag_ClearLocalVars) != 0;
- options->mRuntimeChecks = (optionFlags & BfCompilerOptionFlag_RuntimeChecks) != 0;
- options->mEmitDynamicCastCheck = (optionFlags & BfCompilerOptionFlag_EmitDynamicCastCheck) != 0;
- options->mObjectHasDebugFlags = (optionFlags & BfCompilerOptionFlag_EnableObjectDebugFlags) != 0;
- options->mEnableRealtimeLeakCheck = ((optionFlags & BfCompilerOptionFlag_EnableRealtimeLeakCheck) != 0) && options->mObjectHasDebugFlags;
- options->mDebugAlloc = ((optionFlags & BfCompilerOptionFlag_DebugAlloc) != 0) || options->mEnableRealtimeLeakCheck;
- options->mOmitDebugHelpers = (optionFlags & BfCompilerOptionFlag_OmitDebugHelpers) != 0;
- #ifdef _WINDOWS
- // if (options->mToolsetType == BfToolsetType_GNU)
- // {
- // options->mErrorString = "Toolset 'GNU' is not available on this platform. Consider changing 'Workspace/General/Toolset'.";
- // }
- #else
- // if (options->mToolsetType == BfToolsetType_Microsoft)
- // {
- // options->mErrorString = "Toolset 'Microsoft' is not available on this platform. Consider changing 'Workspace/General/Toolset'.";
- // }
- BF_ASSERT(!options->mEnableRealtimeLeakCheck);
- #endif
- options->mEmitObjectAccessCheck = (optionFlags & BfCompilerOptionFlag_EmitObjectAccessCheck) != 0;
- options->mArithmeticChecks = (optionFlags & BfCompilerOptionFlag_ArithmeticChecks) != 0;
- options->mAllocStackCount = allocStackCount;
- if (hotProject != NULL)
- {
- String errorName;
- if (options->mAllowHotSwapping != allowHotSwapping)
- errorName = "Hot Compilation Enabled";
- else if (options->mMallocLinkName != mallocLinkName)
- errorName = "Malloc";
- else if (options->mFreeLinkName != freeLinkName)
- errorName = "Free";
- if (!options->mEmitDebugInfo)
- {
- 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'.";
- }
- else if (!errorName.IsEmpty())
- {
- options->mErrorString = StrFormat("Unable to change option '%s' during hot compilation", errorName.c_str());
- }
- }
- else
- {
- options->mAllowHotSwapping = allowHotSwapping;
- options->mHasVDataExtender = options->mAllowHotSwapping;
- options->mMallocLinkName = mallocLinkName;
- options->mFreeLinkName = freeLinkName;
- options->mEmitDebugInfo = emitDebugInfo;
- options->mEmitLineInfo = (optionFlags & BfCompilerOptionFlag_EmitLineInfo) != 0;;
- options->mEnableCustodian = (optionFlags & BfCompilerOptionFlag_EnableCustodian) != 0;
- options->mEnableSideStack = (optionFlags & BfCompilerOptionFlag_EnableSideStack) != 0;
- }
- }
- else
- {
- options->mCompileOnDemandKind = BfCompileOnDemandKind_AlwaysInclude;
- options->mAllowHotSwapping = false;
- options->mObjectHasDebugFlags = false;
- options->mEnableRealtimeLeakCheck = false;
- options->mEmitObjectAccessCheck = false;
- options->mArithmeticChecks = false;
- options->mEmitDynamicCastCheck = false;
- options->mRuntimeChecks = (optionFlags & BfCompilerOptionFlag_RuntimeChecks) != 0;
- }
- }
- BF_EXPORT void BF_CALLTYPE BfCompiler_ForceRebuild(BfCompiler* bfCompiler)
- {
- bfCompiler->mOptions.mForceRebuildIdx++;
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetEmitSource(BfCompiler* bfCompiler, char* fileName)
- {
- String& outString = *gTLStrReturn.Get();
- outString.clear();
- bfCompiler->GetEmitSource(fileName, &outString);
- if (outString.IsEmpty())
- return NULL;
- return outString.c_str();
- }
- BF_EXPORT int32 BF_CALLTYPE BfCompiler_GetEmitSourceVersion(BfCompiler* bfCompiler, char* fileName)
- {
- return bfCompiler->GetEmitSource(fileName, NULL);
- }
- BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetEmitLocation(BfCompiler* bfCompiler, char* typeName, int line, int& outEmbedLine, int& outEmbedLineChar, uint64& outHash)
- {
- String& outString = *gTLStrReturn.Get();
- outString.clear();
- outString = bfCompiler->GetEmitLocation(typeName, line, outEmbedLine, outEmbedLineChar, outHash);
- return outString.c_str();
- }
- BF_EXPORT bool BF_CALLTYPE BfCompiler_WriteEmitData(BfCompiler* bfCompiler, char* filePath, BfProject* project)
- {
- return bfCompiler->WriteEmitData(filePath, project);
- }
|