checker.cpp 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169
  1. #include "entity.cpp"
  2. #include "types.cpp"
  3. void check_expr(CheckerContext *c, Operand *operand, Ast *expression);
  4. void check_expr_or_type(CheckerContext *c, Operand *operand, Ast *expression, Type *type_hint=nullptr);
  5. void add_comparison_procedures_for_fields(CheckerContext *c, Type *t);
  6. bool is_operand_value(Operand o) {
  7. switch (o.mode) {
  8. case Addressing_Value:
  9. case Addressing_Context:
  10. case Addressing_Variable:
  11. case Addressing_Constant:
  12. case Addressing_MapIndex:
  13. case Addressing_OptionalOk:
  14. case Addressing_OptionalOkPtr:
  15. case Addressing_SoaVariable:
  16. case Addressing_SwizzleValue:
  17. case Addressing_SwizzleVariable:
  18. return true;
  19. }
  20. return false;
  21. }
  22. bool is_operand_nil(Operand o) {
  23. return o.mode == Addressing_Value && o.type == t_untyped_nil;
  24. }
  25. bool is_operand_undef(Operand o) {
  26. return o.mode == Addressing_Value && o.type == t_untyped_undef;
  27. }
  28. void scope_reset(Scope *scope) {
  29. if (scope == nullptr) return;
  30. scope->head_child.store(nullptr, std::memory_order_relaxed);
  31. string_map_clear(&scope->elements);
  32. ptr_set_clear(&scope->imported);
  33. }
  34. void scope_reserve(Scope *scope, isize capacity) {
  35. isize cap = 2*capacity;
  36. if (cap > scope->elements.hashes.count) {
  37. string_map_rehash(&scope->elements, capacity);
  38. }
  39. }
  40. i32 is_scope_an_ancestor(Scope *parent, Scope *child) {
  41. i32 i = 0;
  42. while (child != nullptr) {
  43. if (parent == child) {
  44. return i;
  45. }
  46. child = child->parent;
  47. i++;
  48. }
  49. return -1;
  50. }
  51. void entity_graph_node_set_destroy(EntityGraphNodeSet *s) {
  52. if (s->hashes.data != nullptr) {
  53. ptr_set_destroy(s);
  54. }
  55. }
  56. void entity_graph_node_set_add(EntityGraphNodeSet *s, EntityGraphNode *n) {
  57. if (s->hashes.data == nullptr) {
  58. ptr_set_init(s, heap_allocator());
  59. }
  60. ptr_set_add(s, n);
  61. }
  62. bool entity_graph_node_set_exists(EntityGraphNodeSet *s, EntityGraphNode *n) {
  63. return ptr_set_exists(s, n);
  64. }
  65. void entity_graph_node_set_remove(EntityGraphNodeSet *s, EntityGraphNode *n) {
  66. ptr_set_remove(s, n);
  67. }
  68. void entity_graph_node_destroy(EntityGraphNode *n, gbAllocator a) {
  69. entity_graph_node_set_destroy(&n->pred);
  70. entity_graph_node_set_destroy(&n->succ);
  71. gb_free(a, n);
  72. }
  73. int entity_graph_node_cmp(EntityGraphNode **data, isize i, isize j) {
  74. EntityGraphNode *x = data[i];
  75. EntityGraphNode *y = data[j];
  76. u64 a = x->entity->order_in_src;
  77. u64 b = y->entity->order_in_src;
  78. if (x->dep_count < y->dep_count) {
  79. return -1;
  80. }
  81. if (x->dep_count == y->dep_count) {
  82. return a < b ? -1 : b > a;
  83. }
  84. return +1;
  85. }
  86. void entity_graph_node_swap(EntityGraphNode **data, isize i, isize j) {
  87. EntityGraphNode *x = data[i];
  88. EntityGraphNode *y = data[j];
  89. data[i] = y;
  90. data[j] = x;
  91. x->index = j;
  92. y->index = i;
  93. }
  94. void import_graph_node_set_destroy(ImportGraphNodeSet *s) {
  95. if (s->hashes.data != nullptr) {
  96. ptr_set_destroy(s);
  97. }
  98. }
  99. void import_graph_node_set_add(ImportGraphNodeSet *s, ImportGraphNode *n) {
  100. if (s->hashes.data == nullptr) {
  101. ptr_set_init(s, heap_allocator());
  102. }
  103. ptr_set_add(s, n);
  104. }
  105. bool import_graph_node_set_exists(ImportGraphNodeSet *s, ImportGraphNode *n) {
  106. return ptr_set_exists(s, n);
  107. }
  108. void import_graph_node_set_remove(ImportGraphNodeSet *s, ImportGraphNode *n) {
  109. ptr_set_remove(s, n);
  110. }
  111. ImportGraphNode *import_graph_node_create(gbAllocator a, AstPackage *pkg) {
  112. ImportGraphNode *n = gb_alloc_item(a, ImportGraphNode);
  113. n->pkg = pkg;
  114. n->scope = pkg->scope;
  115. return n;
  116. }
  117. void import_graph_node_destroy(ImportGraphNode *n, gbAllocator a) {
  118. import_graph_node_set_destroy(&n->pred);
  119. import_graph_node_set_destroy(&n->succ);
  120. gb_free(a, n);
  121. }
  122. int import_graph_node_cmp(ImportGraphNode **data, isize i, isize j) {
  123. ImportGraphNode *x = data[i];
  124. ImportGraphNode *y = data[j];
  125. GB_ASSERT(x != y);
  126. GB_ASSERT(x->scope != y->scope);
  127. bool xg = (x->scope->flags&ScopeFlag_Global) != 0;
  128. bool yg = (y->scope->flags&ScopeFlag_Global) != 0;
  129. if (xg != yg) return xg ? -1 : +1;
  130. if (xg && yg) return x->pkg->id < y->pkg->id ? +1 : -1;
  131. if (x->dep_count < y->dep_count) return -1;
  132. if (x->dep_count > y->dep_count) return +1;
  133. return 0;
  134. }
  135. void import_graph_node_swap(ImportGraphNode **data, isize i, isize j) {
  136. ImportGraphNode *x = data[i];
  137. ImportGraphNode *y = data[j];
  138. data[i] = y;
  139. data[j] = x;
  140. x->index = j;
  141. y->index = i;
  142. }
  143. GB_COMPARE_PROC(ast_node_cmp) {
  144. Ast *x = *cast(Ast **)a;
  145. Ast *y = *cast(Ast **)b;
  146. Token i = ast_token(x);
  147. Token j = ast_token(y);
  148. return token_pos_cmp(i.pos, j.pos);
  149. }
  150. void init_decl_info(DeclInfo *d, Scope *scope, DeclInfo *parent) {
  151. d->parent = parent;
  152. d->scope = scope;
  153. ptr_set_init(&d->deps, heap_allocator());
  154. ptr_set_init(&d->type_info_deps, heap_allocator());
  155. array_init (&d->labels, heap_allocator());
  156. }
  157. DeclInfo *make_decl_info(Scope *scope, DeclInfo *parent) {
  158. DeclInfo *d = gb_alloc_item(permanent_allocator(), DeclInfo);
  159. init_decl_info(d, scope, parent);
  160. return d;
  161. }
  162. void destroy_declaration_info(DeclInfo *d) {
  163. ptr_set_destroy(&d->deps);
  164. array_free(&d->labels);
  165. }
  166. bool decl_info_has_init(DeclInfo *d) {
  167. if (d->init_expr != nullptr) {
  168. return true;
  169. }
  170. if (d->proc_lit != nullptr) {
  171. switch (d->proc_lit->kind) {
  172. case_ast_node(pl, ProcLit, d->proc_lit);
  173. if (pl->body != nullptr) {
  174. return true;
  175. }
  176. case_end;
  177. }
  178. }
  179. return false;
  180. }
  181. Scope *create_scope(CheckerInfo *info, Scope *parent, isize init_elements_capacity=DEFAULT_SCOPE_CAPACITY) {
  182. Scope *s = gb_alloc_item(permanent_allocator(), Scope);
  183. s->parent = parent;
  184. string_map_init(&s->elements, heap_allocator(), init_elements_capacity);
  185. ptr_set_init(&s->imported, heap_allocator(), 0);
  186. if (parent != nullptr && parent != builtin_pkg->scope) {
  187. Scope *prev_head_child = parent->head_child.exchange(s, std::memory_order_acq_rel);
  188. if (prev_head_child) {
  189. prev_head_child->next.store(s, std::memory_order_release);
  190. }
  191. }
  192. if (parent != nullptr && parent->flags & ScopeFlag_ContextDefined) {
  193. s->flags |= ScopeFlag_ContextDefined;
  194. }
  195. return s;
  196. }
  197. Scope *create_scope_from_file(CheckerInfo *info, AstFile *f) {
  198. GB_ASSERT(f != nullptr);
  199. GB_ASSERT(f->pkg != nullptr);
  200. GB_ASSERT(f->pkg->scope != nullptr);
  201. isize init_elements_capacity = gb_max(DEFAULT_SCOPE_CAPACITY, 2*f->total_file_decl_count);
  202. Scope *s = create_scope(info, f->pkg->scope, init_elements_capacity);
  203. s->flags |= ScopeFlag_File;
  204. s->file = f;
  205. f->scope = s;
  206. return s;
  207. }
  208. Scope *create_scope_from_package(CheckerContext *c, AstPackage *pkg) {
  209. GB_ASSERT(pkg != nullptr);
  210. isize total_pkg_decl_count = 0;
  211. for_array(j, pkg->files) {
  212. total_pkg_decl_count += pkg->files.data[j]->total_file_decl_count;
  213. }
  214. isize init_elements_capacity = gb_max(DEFAULT_SCOPE_CAPACITY, 2*total_pkg_decl_count);
  215. Scope *s = create_scope(c->info, builtin_pkg->scope, init_elements_capacity);
  216. s->flags |= ScopeFlag_Pkg;
  217. s->pkg = pkg;
  218. pkg->scope = s;
  219. if (pkg->fullpath == c->checker->parser->init_fullpath || pkg->kind == Package_Init) {
  220. s->flags |= ScopeFlag_Init;
  221. }
  222. if (pkg->kind == Package_Runtime) {
  223. s->flags |= ScopeFlag_Global;
  224. }
  225. if (s->flags & (ScopeFlag_Init|ScopeFlag_Global)) {
  226. s->flags |= ScopeFlag_HasBeenImported;
  227. }
  228. s->flags |= ScopeFlag_ContextDefined;
  229. return s;
  230. }
  231. void destroy_scope(Scope *scope) {
  232. for_array(i, scope->elements.entries) {
  233. Entity *e =scope->elements.entries[i].value;
  234. if (e->kind == Entity_Variable) {
  235. if (!(e->flags & EntityFlag_Used)) {
  236. #if 0
  237. warning(e->token, "Unused variable '%.*s'", LIT(e->token.string));
  238. #endif
  239. }
  240. }
  241. }
  242. for (Scope *child = scope->head_child; child != nullptr; child = child->next) {
  243. destroy_scope(child);
  244. }
  245. string_map_destroy(&scope->elements);
  246. ptr_set_destroy(&scope->imported);
  247. // NOTE(bill): No need to free scope as it "should" be allocated in an arena (except for the global scope)
  248. }
  249. void add_scope(CheckerContext *c, Ast *node, Scope *scope) {
  250. GB_ASSERT(node != nullptr);
  251. GB_ASSERT(scope != nullptr);
  252. scope->node = node;
  253. node->scope = scope;
  254. }
  255. void check_open_scope(CheckerContext *c, Ast *node) {
  256. node = unparen_expr(node);
  257. GB_ASSERT(node->kind == Ast_Invalid ||
  258. is_ast_stmt(node) ||
  259. is_ast_type(node));
  260. Scope *scope = create_scope(c->info, c->scope);
  261. add_scope(c, node, scope);
  262. switch (node->kind) {
  263. case Ast_ProcType:
  264. scope->flags |= ScopeFlag_Proc;
  265. break;
  266. case Ast_StructType:
  267. case Ast_EnumType:
  268. case Ast_UnionType:
  269. case Ast_BitSetType:
  270. scope->flags |= ScopeFlag_Type;
  271. break;
  272. }
  273. c->scope = scope;
  274. c->state_flags |= StateFlag_bounds_check;
  275. }
  276. void check_close_scope(CheckerContext *c) {
  277. c->scope = c->scope->parent;
  278. }
  279. Entity *scope_lookup_current(Scope *s, String const &name) {
  280. Entity **found = string_map_get(&s->elements, name);
  281. if (found) {
  282. return *found;
  283. }
  284. return nullptr;
  285. }
  286. void scope_lookup_parent(Scope *scope, String const &name, Scope **scope_, Entity **entity_) {
  287. bool gone_thru_proc = false;
  288. bool gone_thru_package = false;
  289. StringHashKey key = string_hash_string(name);
  290. for (Scope *s = scope; s != nullptr; s = s->parent) {
  291. Entity **found = string_map_get(&s->elements, key);
  292. if (found) {
  293. Entity *e = *found;
  294. if (gone_thru_proc) {
  295. // IMPORTANT TODO(bill): Is this correct?!
  296. if (e->kind == Entity_Label) {
  297. continue;
  298. }
  299. if (e->kind == Entity_Variable) {
  300. if (e->scope->flags&ScopeFlag_File) {
  301. // Global variables are file to access
  302. } else if (e->flags&EntityFlag_Static) {
  303. // Allow static/thread_local variables to be referenced
  304. } else {
  305. continue;
  306. }
  307. }
  308. }
  309. if (entity_) *entity_ = e;
  310. if (scope_) *scope_ = s;
  311. return;
  312. }
  313. if (s->flags&ScopeFlag_Proc) {
  314. gone_thru_proc = true;
  315. }
  316. if (s->flags&ScopeFlag_Pkg) {
  317. gone_thru_package = true;
  318. }
  319. }
  320. if (entity_) *entity_ = nullptr;
  321. if (scope_) *scope_ = nullptr;
  322. }
  323. Entity *scope_lookup(Scope *s, String const &name) {
  324. Entity *entity = nullptr;
  325. scope_lookup_parent(s, name, nullptr, &entity);
  326. return entity;
  327. }
  328. Entity *scope_insert_with_name(Scope *s, String const &name, Entity *entity) {
  329. if (name == "") {
  330. return nullptr;
  331. }
  332. StringHashKey key = string_hash_string(name);
  333. Entity **found = string_map_get(&s->elements, key);
  334. if (found) {
  335. return *found;
  336. }
  337. if (s->parent != nullptr && (s->parent->flags & ScopeFlag_Proc) != 0) {
  338. Entity **found = string_map_get(&s->parent->elements, key);
  339. if (found) {
  340. if ((*found)->flags & EntityFlag_Result) {
  341. return *found;
  342. }
  343. }
  344. }
  345. string_map_set(&s->elements, key, entity);
  346. if (entity->scope == nullptr) {
  347. entity->scope = s;
  348. }
  349. return nullptr;
  350. }
  351. Entity *scope_insert(Scope *s, Entity *entity) {
  352. String name = entity->token.string;
  353. return scope_insert_with_name(s, name, entity);
  354. }
  355. GB_COMPARE_PROC(entity_variable_pos_cmp) {
  356. Entity *x = *cast(Entity **)a;
  357. Entity *y = *cast(Entity **)b;
  358. return token_pos_cmp(x->token.pos, y->token.pos);
  359. }
  360. enum VettedEntityKind {
  361. VettedEntity_Invalid,
  362. VettedEntity_Unused,
  363. VettedEntity_Shadowed,
  364. };
  365. struct VettedEntity {
  366. VettedEntityKind kind;
  367. Entity *entity;
  368. Entity *other;
  369. };
  370. void init_vetted_entity(VettedEntity *ve, VettedEntityKind kind, Entity *entity, Entity *other=nullptr) {
  371. ve->kind = kind;
  372. ve->entity = entity;
  373. ve->other = other;
  374. }
  375. GB_COMPARE_PROC(vetted_entity_variable_pos_cmp) {
  376. Entity *x = (cast(VettedEntity *)a)->entity;
  377. Entity *y = (cast(VettedEntity *)b)->entity;
  378. GB_ASSERT(x != nullptr);
  379. GB_ASSERT(y != nullptr);
  380. return token_pos_cmp(x->token.pos, y->token.pos);
  381. }
  382. bool check_vet_shadowing(Checker *c, Entity *e, VettedEntity *ve) {
  383. if (e->kind != Entity_Variable) {
  384. return false;
  385. }
  386. String name = e->token.string;
  387. if (name == "_") {
  388. return false;
  389. }
  390. if (e->flags & EntityFlag_Param) {
  391. return false;
  392. }
  393. if (e->scope->flags & (ScopeFlag_Global|ScopeFlag_File|ScopeFlag_Proc)) {
  394. return false;
  395. }
  396. Scope *parent = e->scope->parent;
  397. if (parent->flags & (ScopeFlag_Global|ScopeFlag_File)) {
  398. return false;
  399. }
  400. Entity *shadowed = scope_lookup(parent, name);
  401. if (shadowed == nullptr) {
  402. return false;
  403. }
  404. if (shadowed->kind != Entity_Variable) {
  405. return false;
  406. }
  407. if (shadowed->scope->flags & (ScopeFlag_Global|ScopeFlag_File)) {
  408. // return false;
  409. }
  410. // NOTE(bill): The entities must be in the same file
  411. if (e->token.pos.file_id != shadowed->token.pos.file_id) {
  412. return false;
  413. }
  414. // NOTE(bill): The shaded identifier must appear before this one to be an
  415. // instance of shadowing
  416. if (token_pos_cmp(shadowed->token.pos, e->token.pos) > 0) {
  417. return false;
  418. }
  419. // NOTE(bill): If the types differ, don't complain
  420. if (!are_types_identical(e->type, shadowed->type)) {
  421. return false;
  422. }
  423. // NOTE(bill): Ignore intentional redeclaration
  424. // x := x;
  425. // Suggested in issue #637 (2020-05-11)
  426. if ((e->flags & EntityFlag_Using) == 0 && e->kind == Entity_Variable) {
  427. Ast *init = unparen_expr(e->Variable.init_expr);
  428. if (init != nullptr && init->kind == Ast_Ident) {
  429. // TODO(bill): Which logic is better? Same name or same entity
  430. // bool ignore = init->Ident.token.string == name;
  431. bool ignore = init->Ident.entity == shadowed;
  432. if (ignore) {
  433. return false;
  434. }
  435. }
  436. }
  437. zero_item(ve);
  438. ve->kind = VettedEntity_Shadowed;
  439. ve->entity = e;
  440. ve->other = shadowed;
  441. return true;
  442. }
  443. bool check_vet_unused(Checker *c, Entity *e, VettedEntity *ve) {
  444. if ((e->flags&EntityFlag_Used) == 0) {
  445. switch (e->kind) {
  446. case Entity_Variable:
  447. if (e->scope->flags & (ScopeFlag_Global|ScopeFlag_Type|ScopeFlag_File)) {
  448. return false;
  449. }
  450. case Entity_ImportName:
  451. case Entity_LibraryName:
  452. zero_item(ve);
  453. ve->kind = VettedEntity_Unused;
  454. ve->entity = e;
  455. return true;
  456. }
  457. }
  458. return false;
  459. }
  460. void check_scope_usage(Checker *c, Scope *scope) {
  461. bool vet_unused = true;
  462. bool vet_shadowing = true;
  463. Array<VettedEntity> vetted_entities = {};
  464. array_init(&vetted_entities, heap_allocator());
  465. for_array(i, scope->elements.entries) {
  466. Entity *e = scope->elements.entries[i].value;
  467. if (e == nullptr) continue;
  468. VettedEntity ve = {};
  469. if (vet_unused && check_vet_unused(c, e, &ve)) {
  470. array_add(&vetted_entities, ve);
  471. }
  472. if (vet_shadowing && check_vet_shadowing(c, e, &ve)) {
  473. array_add(&vetted_entities, ve);
  474. }
  475. }
  476. gb_sort(vetted_entities.data, vetted_entities.count, gb_size_of(VettedEntity), vetted_entity_variable_pos_cmp);
  477. for_array(i, vetted_entities) {
  478. auto ve = vetted_entities[i];
  479. Entity *e = ve.entity;
  480. Entity *other = ve.other;
  481. String name = e->token.string;
  482. if (build_context.vet) {
  483. switch (ve.kind) {
  484. case VettedEntity_Unused:
  485. error(e->token, "'%.*s' declared but not used", LIT(name));
  486. break;
  487. case VettedEntity_Shadowed:
  488. if (e->flags&EntityFlag_Using) {
  489. error(e->token, "Declaration of '%.*s' from 'using' shadows declaration at line %lld", LIT(name), cast(long long)other->token.pos.line);
  490. } else {
  491. error(e->token, "Declaration of '%.*s' shadows declaration at line %lld", LIT(name), cast(long long)other->token.pos.line);
  492. }
  493. break;
  494. default:
  495. break;
  496. }
  497. }
  498. if (e->kind == Entity_Variable && (e->flags & (EntityFlag_Param|EntityFlag_Using)) == 0) {
  499. i64 sz = type_size_of(e->type);
  500. // TODO(bill): When is a good size warn?
  501. // Is 128 KiB good enough?
  502. if (sz >= 1ll<<17) {
  503. gbString type_str = type_to_string(e->type);
  504. warning(e->token, "Declaration of '%.*s' may cause a stack overflow due to its type '%s' having a size of %lld bytes", LIT(name), type_str, cast(long long)sz);
  505. gb_string_free(type_str);
  506. }
  507. }
  508. }
  509. array_free(&vetted_entities);
  510. for (Scope *child = scope->head_child; child != nullptr; child = child->next) {
  511. if (child->flags & (ScopeFlag_Proc|ScopeFlag_Type|ScopeFlag_File)) {
  512. // Ignore these
  513. } else {
  514. check_scope_usage(c, child);
  515. }
  516. }
  517. }
  518. void add_dependency(DeclInfo *d, Entity *e) {
  519. ptr_set_add(&d->deps, e);
  520. }
  521. void add_type_info_dependency(DeclInfo *d, Type *type) {
  522. if (d == nullptr) {
  523. // GB_ASSERT(type == t_invalid);
  524. return;
  525. }
  526. ptr_set_add(&d->type_info_deps, type);
  527. }
  528. AstPackage *get_core_package(CheckerInfo *info, String name) {
  529. gbAllocator a = heap_allocator();
  530. String path = get_fullpath_core(a, name);
  531. defer (gb_free(a, path.text));
  532. auto found = string_map_get(&info->packages, path);
  533. GB_ASSERT_MSG(found != nullptr, "Missing core package %.*s", LIT(name));
  534. return *found;
  535. }
  536. void add_package_dependency(CheckerContext *c, char const *package_name, char const *name) {
  537. String n = make_string_c(name);
  538. AstPackage *p = get_core_package(&c->checker->info, make_string_c(package_name));
  539. Entity *e = scope_lookup(p->scope, n);
  540. e->flags |= EntityFlag_Used;
  541. GB_ASSERT_MSG(e != nullptr, "%s", name);
  542. GB_ASSERT(c->decl != nullptr);
  543. ptr_set_add(&c->decl->deps, e);
  544. }
  545. void add_declaration_dependency(CheckerContext *c, Entity *e) {
  546. if (e == nullptr) {
  547. return;
  548. }
  549. if (c->decl != nullptr) {
  550. add_dependency(c->decl, e);
  551. }
  552. }
  553. Entity *add_global_entity(Entity *entity, Scope *scope=builtin_pkg->scope) {
  554. String name = entity->token.string;
  555. defer (entity->state = EntityState_Resolved);
  556. if (gb_memchr(name.text, ' ', name.len)) {
  557. return entity; // NOTE(bill): Usually an 'untyped thing'
  558. }
  559. if (scope_insert(scope, entity)) {
  560. compiler_error("double declaration");
  561. }
  562. return entity;
  563. }
  564. void add_global_constant(char const *name, Type *type, ExactValue value) {
  565. Entity *entity = alloc_entity(Entity_Constant, nullptr, make_token_ident(name), type);
  566. entity->Constant.value = value;
  567. add_global_entity(entity);
  568. }
  569. void add_global_string_constant(char const *name, String const &value) {
  570. add_global_constant(name, t_untyped_string, exact_value_string(value));
  571. }
  572. void add_global_bool_constant(char const *name, bool value) {
  573. add_global_constant(name, t_untyped_bool, exact_value_bool(value));
  574. }
  575. void add_global_type_entity(String name, Type *type) {
  576. add_global_entity(alloc_entity_type_name(nullptr, make_token_ident(name), type));
  577. }
  578. AstPackage *create_builtin_package(char const *name) {
  579. gbAllocator a = permanent_allocator();
  580. AstPackage *pkg = gb_alloc_item(a, AstPackage);
  581. pkg->name = make_string_c(name);
  582. pkg->kind = Package_Normal;
  583. pkg->scope = create_scope(nullptr, nullptr);
  584. pkg->scope->flags |= ScopeFlag_Pkg | ScopeFlag_Global | ScopeFlag_Builtin;
  585. pkg->scope->pkg = pkg;
  586. return pkg;
  587. }
  588. void init_universal(void) {
  589. BuildContext *bc = &build_context;
  590. builtin_pkg = create_builtin_package("builtin");
  591. intrinsics_pkg = create_builtin_package("intrinsics");
  592. config_pkg = create_builtin_package("config");
  593. // Types
  594. for (isize i = 0; i < gb_count_of(basic_types); i++) {
  595. add_global_type_entity(basic_types[i].Basic.name, &basic_types[i]);
  596. }
  597. add_global_type_entity(str_lit("byte"), &basic_types[Basic_u8]);
  598. {
  599. Type *equal_args[2] = {t_rawptr, t_rawptr};
  600. t_equal_proc = alloc_type_proc_from_types(equal_args, 2, t_bool, false, ProcCC_Contextless);
  601. Type *hasher_args[2] = {t_rawptr, t_uintptr};
  602. t_hasher_proc = alloc_type_proc_from_types(hasher_args, 2, t_uintptr, false, ProcCC_Contextless);
  603. }
  604. // Constants
  605. add_global_entity(alloc_entity_nil(str_lit("nil"), t_untyped_nil));
  606. add_global_bool_constant("true", true);
  607. add_global_bool_constant("false", false);
  608. // TODO(bill): Set through flags in the compiler
  609. add_global_string_constant("ODIN_OS", bc->ODIN_OS);
  610. add_global_string_constant("ODIN_ARCH", bc->ODIN_ARCH);
  611. add_global_string_constant("ODIN_ENDIAN", bc->ODIN_ENDIAN);
  612. add_global_string_constant("ODIN_VENDOR", bc->ODIN_VENDOR);
  613. add_global_string_constant("ODIN_VERSION", bc->ODIN_VERSION);
  614. add_global_string_constant("ODIN_ROOT", bc->ODIN_ROOT);
  615. add_global_bool_constant("ODIN_DEBUG", bc->ODIN_DEBUG);
  616. add_global_bool_constant("ODIN_DISABLE_ASSERT", bc->ODIN_DISABLE_ASSERT);
  617. add_global_bool_constant("ODIN_DEFAULT_TO_NIL_ALLOCATOR", bc->ODIN_DEFAULT_TO_NIL_ALLOCATOR);
  618. add_global_bool_constant("ODIN_NO_DYNAMIC_LITERALS", bc->no_dynamic_literals);
  619. add_global_bool_constant("ODIN_TEST", bc->command_kind == Command_test);
  620. // Builtin Procedures
  621. for (isize i = 0; i < gb_count_of(builtin_procs); i++) {
  622. BuiltinProcId id = cast(BuiltinProcId)i;
  623. String name = builtin_procs[i].name;
  624. if (name != "") {
  625. Entity *entity = alloc_entity(Entity_Builtin, nullptr, make_token_ident(name), t_invalid);
  626. entity->Builtin.id = id;
  627. switch (builtin_procs[i].pkg) {
  628. case BuiltinProcPkg_builtin:
  629. add_global_entity(entity, builtin_pkg->scope);
  630. break;
  631. case BuiltinProcPkg_intrinsics:
  632. add_global_entity(entity, intrinsics_pkg->scope);
  633. GB_ASSERT(scope_lookup_current(intrinsics_pkg->scope, name) != nullptr);
  634. break;
  635. }
  636. }
  637. }
  638. bool defined_values_double_declaration = false;
  639. for_array(i, bc->defined_values.entries) {
  640. char const *name = cast(char const *)cast(uintptr)bc->defined_values.entries[i].key.key;
  641. ExactValue value = bc->defined_values.entries[i].value;
  642. GB_ASSERT(value.kind != ExactValue_Invalid);
  643. Type *type = nullptr;
  644. switch (value.kind) {
  645. case ExactValue_Bool:
  646. type = t_untyped_bool;
  647. break;
  648. case ExactValue_String:
  649. type = t_untyped_string;
  650. break;
  651. case ExactValue_Integer:
  652. type = t_untyped_integer;
  653. break;
  654. case ExactValue_Float:
  655. type = t_untyped_float;
  656. break;
  657. }
  658. GB_ASSERT(type != nullptr);
  659. Entity *entity = alloc_entity_constant(nullptr, make_token_ident(name), type, value);
  660. entity->state = EntityState_Resolved;
  661. if (scope_insert(config_pkg->scope, entity)) {
  662. error(entity->token, "'%s' defined as an argument is already declared at the global scope", name);
  663. defined_values_double_declaration = true;
  664. // NOTE(bill): Just exit early before anything, even though the compiler will do that anyway
  665. }
  666. }
  667. if (defined_values_double_declaration) {
  668. gb_exit(1);
  669. }
  670. t_u8_ptr = alloc_type_pointer(t_u8);
  671. t_int_ptr = alloc_type_pointer(t_int);
  672. t_i64_ptr = alloc_type_pointer(t_i64);
  673. t_f64_ptr = alloc_type_pointer(t_f64);
  674. t_u8_slice = alloc_type_slice(t_u8);
  675. t_string_slice = alloc_type_slice(t_string);
  676. }
  677. void init_checker_info(CheckerInfo *i) {
  678. #define TIME_SECTION(str) do { debugf("[Subsection] %s\n", str); if (build_context.show_more_timings) timings_start_section(&global_timings, str_lit(str)); } while (0)
  679. gbAllocator a = heap_allocator();
  680. TIME_SECTION("checker info: general");
  681. array_init(&i->definitions, a);
  682. array_init(&i->entities, a);
  683. map_init(&i->global_untyped, a);
  684. string_map_init(&i->foreigns, a);
  685. map_init(&i->gen_procs, a);
  686. map_init(&i->gen_types, a);
  687. array_init(&i->type_info_types, a);
  688. map_init(&i->type_info_map, a);
  689. string_map_init(&i->files, a);
  690. string_map_init(&i->packages, a);
  691. array_init(&i->variable_init_order, a);
  692. array_init(&i->testing_procedures, a, 0, 0);
  693. array_init(&i->required_foreign_imports_through_force, a, 0, 0);
  694. i->allow_identifier_uses = build_context.query_data_set_settings.kind == QueryDataSet_GoToDefinitions;
  695. if (i->allow_identifier_uses) {
  696. array_init(&i->identifier_uses, a);
  697. }
  698. TIME_SECTION("checker info: mpmc queues");
  699. mpmc_init(&i->entity_queue, a, 1<<20);
  700. mpmc_init(&i->definition_queue, a, 1<<20);
  701. mpmc_init(&i->required_global_variable_queue, a, 1<<10);
  702. mpmc_init(&i->required_foreign_imports_through_force_queue, a, 1<<10);
  703. TIME_SECTION("checker info: mutexes");
  704. mutex_init(&i->gen_procs_mutex);
  705. mutex_init(&i->gen_types_mutex);
  706. mutex_init(&i->lazy_mutex);
  707. mutex_init(&i->global_untyped_mutex);
  708. mutex_init(&i->type_info_mutex);
  709. mutex_init(&i->deps_mutex);
  710. mutex_init(&i->identifier_uses_mutex);
  711. mutex_init(&i->foreign_mutex);
  712. gb_semaphore_init(&i->collect_semaphore);
  713. #undef TIME_SECTION
  714. }
  715. void destroy_checker_info(CheckerInfo *i) {
  716. array_free(&i->definitions);
  717. array_free(&i->entities);
  718. map_destroy(&i->global_untyped);
  719. string_map_destroy(&i->foreigns);
  720. map_destroy(&i->gen_procs);
  721. map_destroy(&i->gen_types);
  722. array_free(&i->type_info_types);
  723. map_destroy(&i->type_info_map);
  724. string_map_destroy(&i->files);
  725. string_map_destroy(&i->packages);
  726. array_free(&i->variable_init_order);
  727. array_free(&i->identifier_uses);
  728. array_free(&i->required_foreign_imports_through_force);
  729. mpmc_destroy(&i->entity_queue);
  730. mpmc_destroy(&i->definition_queue);
  731. mpmc_destroy(&i->required_global_variable_queue);
  732. mpmc_destroy(&i->required_foreign_imports_through_force_queue);
  733. mutex_destroy(&i->gen_procs_mutex);
  734. mutex_destroy(&i->gen_types_mutex);
  735. mutex_destroy(&i->lazy_mutex);
  736. mutex_destroy(&i->global_untyped_mutex);
  737. mutex_destroy(&i->type_info_mutex);
  738. mutex_destroy(&i->deps_mutex);
  739. mutex_destroy(&i->identifier_uses_mutex);
  740. mutex_destroy(&i->foreign_mutex);
  741. }
  742. CheckerContext make_checker_context(Checker *c) {
  743. CheckerContext ctx = {};
  744. ctx.checker = c;
  745. ctx.info = &c->info;
  746. ctx.scope = builtin_pkg->scope;
  747. ctx.pkg = builtin_pkg;
  748. ctx.type_path = new_checker_type_path();
  749. ctx.type_level = 0;
  750. ctx.poly_path = new_checker_poly_path();
  751. ctx.poly_level = 0;
  752. return ctx;
  753. }
  754. void destroy_checker_context(CheckerContext *ctx) {
  755. destroy_checker_type_path(ctx->type_path);
  756. destroy_checker_poly_path(ctx->poly_path);
  757. }
  758. void add_curr_ast_file(CheckerContext *ctx, AstFile *file) {
  759. if (file != nullptr) {
  760. TokenPos zero_pos = {};
  761. global_error_collector.prev = zero_pos;
  762. ctx->file = file;
  763. ctx->decl = file->pkg->decl_info;
  764. ctx->scope = file->scope;
  765. ctx->pkg = file->pkg;
  766. }
  767. }
  768. void reset_checker_context(CheckerContext *ctx, AstFile *file, UntypedExprInfoMap *untyped) {
  769. if (ctx == nullptr) {
  770. return;
  771. }
  772. destroy_checker_context(ctx);
  773. auto *queue = ctx->procs_to_check_queue;
  774. *ctx = make_checker_context(ctx->checker);
  775. add_curr_ast_file(ctx, file);
  776. ctx->procs_to_check_queue = queue;
  777. ctx->untyped = untyped;
  778. }
  779. void init_checker(Checker *c) {
  780. #define TIME_SECTION(str) do { debugf("[Subsection] %s\n", str); if (build_context.show_more_timings) timings_start_section(&global_timings, str_lit(str)); } while (0)
  781. gbAllocator a = heap_allocator();
  782. TIME_SECTION("init checker info");
  783. init_checker_info(&c->info);
  784. c->info.checker = c;
  785. TIME_SECTION("init proc queues");
  786. mpmc_init(&c->procs_with_deferred_to_check, a, 1<<10);
  787. // NOTE(bill): 1 Mi elements should be enough on average
  788. mpmc_init(&c->procs_to_check_queue, heap_allocator(), 1<<20);
  789. gb_semaphore_init(&c->procs_to_check_semaphore);
  790. mpmc_init(&c->global_untyped_queue, a, 1<<20);
  791. c->builtin_ctx = make_checker_context(c);
  792. #undef TIME_SECTION
  793. }
  794. void destroy_checker(Checker *c) {
  795. destroy_checker_info(&c->info);
  796. destroy_checker_context(&c->builtin_ctx);
  797. mpmc_destroy(&c->procs_to_check_queue);
  798. gb_semaphore_destroy(&c->procs_to_check_semaphore);
  799. mpmc_destroy(&c->global_untyped_queue);
  800. }
  801. TypeAndValue type_and_value_of_expr(Ast *expr) {
  802. TypeAndValue tav = {};
  803. if (expr != nullptr) {
  804. tav = expr->tav;
  805. }
  806. return tav;
  807. }
  808. Type *type_of_expr(Ast *expr) {
  809. TypeAndValue tav = expr->tav;
  810. if (tav.mode != Addressing_Invalid) {
  811. return tav.type;
  812. }
  813. {
  814. Entity *entity = entity_of_node(expr);
  815. if (entity) {
  816. return entity->type;
  817. }
  818. }
  819. return nullptr;
  820. }
  821. Entity *implicit_entity_of_node(Ast *clause) {
  822. if (clause != nullptr && clause->kind == Ast_CaseClause) {
  823. return clause->CaseClause.implicit_entity;
  824. }
  825. return nullptr;
  826. }
  827. Entity *entity_of_node(Ast *expr) {
  828. expr = unparen_expr(expr);
  829. switch (expr->kind) {
  830. case_ast_node(ident, Ident, expr);
  831. Entity *e = ident->entity;
  832. if (e && e->flags & EntityFlag_Overridden) {
  833. // GB_PANIC("use of an overriden entity: %.*s", LIT(e->token.string));
  834. }
  835. return e;
  836. case_end;
  837. case_ast_node(se, SelectorExpr, expr);
  838. Ast *s = unselector_expr(se->selector);
  839. return entity_of_node(s);
  840. case_end;
  841. case_ast_node(cc, CaseClause, expr);
  842. return cc->implicit_entity;
  843. case_end;
  844. }
  845. return nullptr;
  846. }
  847. DeclInfo *decl_info_of_entity(Entity *e) {
  848. if (e != nullptr) {
  849. return e->decl_info;
  850. }
  851. return nullptr;
  852. }
  853. DeclInfo *decl_info_of_ident(Ast *ident) {
  854. return decl_info_of_entity(entity_of_node(ident));
  855. }
  856. AstFile *ast_file_of_filename(CheckerInfo *i, String filename) {
  857. AstFile **found = string_map_get(&i->files, filename);
  858. if (found != nullptr) {
  859. return *found;
  860. }
  861. return nullptr;
  862. }
  863. Scope *scope_of_node(Ast *node) {
  864. return node->scope;
  865. }
  866. ExprInfo *check_get_expr_info(CheckerContext *c, Ast *expr) {
  867. if (c->untyped != nullptr) {
  868. ExprInfo **found = map_get(c->untyped, hash_pointer(expr));
  869. if (found) {
  870. return *found;
  871. }
  872. return nullptr;
  873. } else {
  874. mutex_lock(&c->info->global_untyped_mutex);
  875. defer (mutex_unlock(&c->info->global_untyped_mutex));
  876. ExprInfo **found = map_get(&c->info->global_untyped, hash_pointer(expr));
  877. if (found) {
  878. return *found;
  879. }
  880. return nullptr;
  881. }
  882. }
  883. void check_set_expr_info(CheckerContext *c, Ast *expr, AddressingMode mode, Type *type, ExactValue value) {
  884. if (c->untyped != nullptr) {
  885. map_set(c->untyped, hash_pointer(expr), make_expr_info(mode, type, value, false));
  886. } else {
  887. mutex_lock(&c->info->global_untyped_mutex);
  888. map_set(&c->info->global_untyped, hash_pointer(expr), make_expr_info(mode, type, value, false));
  889. mutex_unlock(&c->info->global_untyped_mutex);
  890. }
  891. }
  892. void check_remove_expr_info(CheckerContext *c, Ast *e) {
  893. if (c->untyped != nullptr) {
  894. map_remove(c->untyped, hash_pointer(e));
  895. } else {
  896. mutex_lock(&c->info->global_untyped_mutex);
  897. map_remove(&c->info->global_untyped, hash_pointer(e));
  898. mutex_unlock(&c->info->global_untyped_mutex);
  899. }
  900. }
  901. isize type_info_index(CheckerInfo *info, Type *type, bool error_on_failure) {
  902. type = default_type(type);
  903. if (type == t_llvm_bool) {
  904. type = t_bool;
  905. }
  906. mutex_lock(&info->type_info_mutex);
  907. isize entry_index = -1;
  908. HashKey key = hash_type(type);
  909. isize *found_entry_index = map_get(&info->type_info_map, key);
  910. if (found_entry_index) {
  911. entry_index = *found_entry_index;
  912. }
  913. if (entry_index < 0) {
  914. // NOTE(bill): Do manual search
  915. // TODO(bill): This is O(n) and can be very slow
  916. for_array(i, info->type_info_map.entries){
  917. auto *e = &info->type_info_map.entries[i];
  918. Type *prev_type = cast(Type *)cast(uintptr)e->key.key;
  919. if (are_types_identical(prev_type, type)) {
  920. entry_index = e->value;
  921. // NOTE(bill): Add it to the search map
  922. map_set(&info->type_info_map, key, entry_index);
  923. break;
  924. }
  925. }
  926. }
  927. mutex_unlock(&info->type_info_mutex);
  928. if (error_on_failure && entry_index < 0) {
  929. compiler_error("Type_Info for '%s' could not be found", type_to_string(type));
  930. }
  931. return entry_index;
  932. }
  933. void add_untyped(CheckerContext *c, Ast *expr, AddressingMode mode, Type *type, ExactValue value) {
  934. if (expr == nullptr) {
  935. return;
  936. }
  937. if (mode == Addressing_Invalid) {
  938. return;
  939. }
  940. if (mode == Addressing_Constant && type == t_invalid) {
  941. compiler_error("add_untyped - invalid type: %s", type_to_string(type));
  942. }
  943. if (!is_type_untyped(type)) {
  944. return;
  945. }
  946. check_set_expr_info(c, expr, mode, type, value);
  947. }
  948. void add_type_and_value(CheckerInfo *i, Ast *expr, AddressingMode mode, Type *type, ExactValue value) {
  949. if (expr == nullptr) {
  950. return;
  951. }
  952. if (mode == Addressing_Invalid) {
  953. return;
  954. }
  955. if (mode == Addressing_Constant && type == t_invalid) {
  956. return;
  957. }
  958. Ast *prev_expr = nullptr;
  959. for (;;) {
  960. if (prev_expr != expr) {
  961. expr->tav.mode = mode;
  962. expr->tav.type = type;
  963. if (mode == Addressing_Constant || mode == Addressing_Invalid) {
  964. expr->tav.value = value;
  965. } else if (mode == Addressing_Value && is_type_typeid(type)) {
  966. expr->tav.value = value;
  967. } else if (mode == Addressing_Value && is_type_proc(type)) {
  968. expr->tav.value = value;
  969. }
  970. prev_expr = expr;
  971. } else {
  972. break;
  973. }
  974. expr = unparen_expr(expr);
  975. }
  976. }
  977. void add_entity_definition(CheckerInfo *i, Ast *identifier, Entity *entity) {
  978. GB_ASSERT(identifier != nullptr);
  979. GB_ASSERT(identifier->kind == Ast_Ident);
  980. // if (is_blank_ident(identifier)) {
  981. // return;
  982. // }
  983. if (identifier->Ident.entity != nullptr) {
  984. // NOTE(bill): Identifier has already been handled
  985. return;
  986. }
  987. GB_ASSERT(entity != nullptr);
  988. identifier->Ident.entity = entity;
  989. entity->identifier = identifier;
  990. mpmc_enqueue(&i->definition_queue, entity);
  991. }
  992. bool redeclaration_error(String name, Entity *prev, Entity *found) {
  993. TokenPos pos = found->token.pos;
  994. Entity *up = found->using_parent;
  995. if (up != nullptr) {
  996. if (pos == up->token.pos) {
  997. // NOTE(bill): Error should have been handled already
  998. return false;
  999. }
  1000. if (found->flags & EntityFlag_Result) {
  1001. error(prev->token,
  1002. "Direct shadowing of the named return value '%.*s' in this scope through 'using'\n"
  1003. "\tat %s",
  1004. LIT(name),
  1005. token_pos_to_string(up->token.pos));
  1006. } else {
  1007. error(prev->token,
  1008. "Redeclaration of '%.*s' in this scope through 'using'\n"
  1009. "\tat %s",
  1010. LIT(name),
  1011. token_pos_to_string(up->token.pos));
  1012. }
  1013. } else {
  1014. if (pos == prev->token.pos) {
  1015. // NOTE(bill): Error should have been handled already
  1016. return false;
  1017. }
  1018. if (found->flags & EntityFlag_Result) {
  1019. error(prev->token,
  1020. "Direct shadowing of the named return value '%.*s' in this scope\n"
  1021. "\tat %s",
  1022. LIT(name),
  1023. token_pos_to_string(pos));
  1024. } else {
  1025. error(prev->token,
  1026. "Redeclaration of '%.*s' in this scope\n"
  1027. "\tat %s",
  1028. LIT(name),
  1029. token_pos_to_string(pos));
  1030. }
  1031. }
  1032. return false;
  1033. }
  1034. void add_entity_flags_from_file(CheckerContext *c, Entity *e, Scope *scope) {
  1035. if (c->file != nullptr && (c->file->flags & AstFile_IsLazy) != 0 && scope->flags & ScopeFlag_File) {
  1036. AstPackage *pkg = c->file->pkg;
  1037. if (pkg->kind == Package_Init && e->kind == Entity_Procedure && e->token.string == "main") {
  1038. // Do nothing
  1039. } else if (e->flags & EntityFlag_Test) {
  1040. // Do nothing
  1041. } else {
  1042. e->flags |= EntityFlag_Lazy;
  1043. }
  1044. }
  1045. }
  1046. bool add_entity_with_name(CheckerContext *c, Scope *scope, Ast *identifier, Entity *entity, String name) {
  1047. if (scope == nullptr) {
  1048. return false;
  1049. }
  1050. if (!is_blank_ident(name)) {
  1051. Entity *ie = scope_insert(scope, entity);
  1052. if (ie != nullptr) {
  1053. return redeclaration_error(name, entity, ie);
  1054. }
  1055. }
  1056. if (identifier != nullptr) {
  1057. if (entity->file == nullptr) {
  1058. entity->file = c->file;
  1059. }
  1060. add_entity_definition(c->info, identifier, entity);
  1061. }
  1062. return true;
  1063. }
  1064. bool add_entity(CheckerContext *c, Scope *scope, Ast *identifier, Entity *entity) {
  1065. return add_entity_with_name(c, scope, identifier, entity, entity->token.string);
  1066. }
  1067. void add_entity_use(CheckerContext *c, Ast *identifier, Entity *entity) {
  1068. if (entity == nullptr) {
  1069. return;
  1070. }
  1071. if (identifier != nullptr) {
  1072. if (identifier->kind != Ast_Ident) {
  1073. return;
  1074. }
  1075. if (entity->identifier == nullptr) {
  1076. entity->identifier = identifier;
  1077. }
  1078. identifier->Ident.entity = entity;
  1079. if (c->info->allow_identifier_uses) {
  1080. mutex_lock(&c->info->identifier_uses_mutex);
  1081. array_add(&c->info->identifier_uses, identifier);
  1082. mutex_unlock(&c->info->identifier_uses_mutex);
  1083. }
  1084. String dmsg = entity->deprecated_message;
  1085. if (dmsg.len > 0) {
  1086. warning(identifier, "%.*s is deprecated: %.*s", LIT(entity->token.string), LIT(dmsg));
  1087. }
  1088. }
  1089. entity->flags |= EntityFlag_Used;
  1090. add_declaration_dependency(c, entity);
  1091. if (entity_has_deferred_procedure(entity)) {
  1092. Entity *deferred = entity->Procedure.deferred_procedure.entity;
  1093. add_entity_use(c, nullptr, deferred);
  1094. }
  1095. }
  1096. bool could_entity_be_lazy(Entity *e, DeclInfo *d) {
  1097. if ((e->flags & EntityFlag_Lazy) == 0) {
  1098. return false;
  1099. }
  1100. if (e->flags & EntityFlag_Test) {
  1101. return false;
  1102. } else if (e->kind == Entity_Variable && e->Variable.is_export) {
  1103. return false;
  1104. } else if (e->kind == Entity_Procedure && e->Procedure.is_export) {
  1105. return false;
  1106. }
  1107. for_array(i, d->attributes) {
  1108. Ast *attr = d->attributes[i];
  1109. if (attr->kind != Ast_Attribute) continue;
  1110. for_array(j, attr->Attribute.elems) {
  1111. Ast *elem = attr->Attribute.elems[j];
  1112. String name = {};
  1113. switch (elem->kind) {
  1114. case_ast_node(i, Ident, elem);
  1115. name = i->token.string;
  1116. case_end;
  1117. case_ast_node(i, Implicit, elem);
  1118. name = i->string;
  1119. case_end;
  1120. case_ast_node(fv, FieldValue, elem);
  1121. if (fv->field->kind == Ast_Ident) {
  1122. name = fv->field->Ident.token.string;
  1123. }
  1124. case_end;
  1125. }
  1126. if (name.len != 0) {
  1127. if (name == "test") {
  1128. return false;
  1129. } else if (name == "export") {
  1130. return false;
  1131. }
  1132. }
  1133. }
  1134. }
  1135. return true;
  1136. }
  1137. void add_entity_and_decl_info(CheckerContext *c, Ast *identifier, Entity *e, DeclInfo *d, bool is_exported) {
  1138. GB_ASSERT(identifier->kind == Ast_Ident);
  1139. GB_ASSERT(e != nullptr && d != nullptr);
  1140. GB_ASSERT(identifier->Ident.token.string == e->token.string);
  1141. if (!could_entity_be_lazy(e, d)) {
  1142. e->flags &= ~EntityFlag_Lazy;
  1143. }
  1144. if (e->scope != nullptr) {
  1145. Scope *scope = e->scope;
  1146. if (scope->flags & ScopeFlag_File && is_entity_kind_exported(e->kind) && is_exported) {
  1147. AstPackage *pkg = scope->file->pkg;
  1148. GB_ASSERT(pkg->scope == scope->parent);
  1149. GB_ASSERT(c->pkg == pkg);
  1150. // NOTE(bill): as multiple threads could be accessing this, it needs to be wrapped
  1151. // The current hash map for scopes is not thread safe
  1152. AstPackageExportedEntity ee = {identifier, e};
  1153. mpmc_enqueue(&pkg->exported_entity_queue, ee);
  1154. // mutex_lock(&c->info->scope_mutex);
  1155. // add_entity(c, pkg->scope, identifier, e);
  1156. // mutex_unlock(&c->info->scope_mutex);
  1157. } else {
  1158. add_entity(c, scope, identifier, e);
  1159. }
  1160. }
  1161. CheckerInfo *info = c->info;
  1162. add_entity_definition(info, identifier, e);
  1163. GB_ASSERT(e->decl_info == nullptr);
  1164. e->decl_info = d;
  1165. d->entity = e;
  1166. e->pkg = c->pkg;
  1167. isize queue_count = -1;
  1168. bool is_lazy = false;
  1169. is_lazy = (e->flags & EntityFlag_Lazy) == EntityFlag_Lazy;
  1170. if (!is_lazy) {
  1171. queue_count = mpmc_enqueue(&info->entity_queue, e);
  1172. }
  1173. if (e->token.pos.file_id != 0) {
  1174. e->order_in_src = cast(u64)(e->token.pos.file_id)<<32 | u32(e->token.pos.offset);
  1175. } else {
  1176. GB_ASSERT(!is_lazy);
  1177. e->order_in_src = cast(u64)(1+queue_count);
  1178. }
  1179. }
  1180. void add_implicit_entity(CheckerContext *c, Ast *clause, Entity *e) {
  1181. GB_ASSERT(clause != nullptr);
  1182. GB_ASSERT(e != nullptr);
  1183. GB_ASSERT(clause->kind == Ast_CaseClause);
  1184. clause->CaseClause.implicit_entity = e;
  1185. }
  1186. void add_type_info_type(CheckerContext *c, Type *t) {
  1187. void add_type_info_type_internal(CheckerContext *c, Type *t);
  1188. mutex_lock(&c->info->type_info_mutex);
  1189. add_type_info_type_internal(c, t);
  1190. mutex_unlock(&c->info->type_info_mutex);
  1191. }
  1192. void add_type_info_type_internal(CheckerContext *c, Type *t) {
  1193. if (t == nullptr) {
  1194. return;
  1195. }
  1196. t = default_type(t);
  1197. if (is_type_untyped(t)) {
  1198. return; // Could be nil
  1199. }
  1200. if (is_type_polymorphic(base_type(t))) {
  1201. return;
  1202. }
  1203. add_type_info_dependency(c->decl, t);
  1204. auto found = map_get(&c->info->type_info_map, hash_type(t));
  1205. if (found != nullptr) {
  1206. // Types have already been added
  1207. return;
  1208. }
  1209. bool prev = false;
  1210. isize ti_index = -1;
  1211. for_array(i, c->info->type_info_map.entries) {
  1212. auto *e = &c->info->type_info_map.entries[i];
  1213. Type *prev_type = cast(Type *)cast(uintptr)e->key.key;
  1214. if (are_types_identical(t, prev_type)) {
  1215. // Duplicate entry
  1216. ti_index = e->value;
  1217. prev = true;
  1218. break;
  1219. }
  1220. }
  1221. if (ti_index < 0) {
  1222. // Unique entry
  1223. // NOTE(bill): map entries grow linearly and in order
  1224. ti_index = c->info->type_info_types.count;
  1225. array_add(&c->info->type_info_types, t);
  1226. }
  1227. map_set(&c->checker->info.type_info_map, hash_type(t), ti_index);
  1228. if (prev) {
  1229. // NOTE(bill): If a previous one exists already, no need to continue
  1230. return;
  1231. }
  1232. // Add nested types
  1233. if (t->kind == Type_Named) {
  1234. // NOTE(bill): Just in case
  1235. add_type_info_type_internal(c, t->Named.base);
  1236. return;
  1237. }
  1238. Type *bt = base_type(t);
  1239. add_type_info_type_internal(c, bt);
  1240. switch (bt->kind) {
  1241. case Type_Invalid:
  1242. break;
  1243. case Type_Basic:
  1244. switch (bt->Basic.kind) {
  1245. case Basic_cstring:
  1246. add_type_info_type_internal(c, t_u8_ptr);
  1247. break;
  1248. case Basic_string:
  1249. add_type_info_type_internal(c, t_u8_ptr);
  1250. add_type_info_type_internal(c, t_int);
  1251. break;
  1252. case Basic_any:
  1253. add_type_info_type_internal(c, t_type_info_ptr);
  1254. add_type_info_type_internal(c, t_rawptr);
  1255. break;
  1256. case Basic_typeid:
  1257. break;
  1258. case Basic_complex64:
  1259. add_type_info_type_internal(c, t_type_info_float);
  1260. add_type_info_type_internal(c, t_f32);
  1261. break;
  1262. case Basic_complex128:
  1263. add_type_info_type_internal(c, t_type_info_float);
  1264. add_type_info_type_internal(c, t_f64);
  1265. break;
  1266. case Basic_quaternion128:
  1267. add_type_info_type_internal(c, t_type_info_float);
  1268. add_type_info_type_internal(c, t_f32);
  1269. break;
  1270. case Basic_quaternion256:
  1271. add_type_info_type_internal(c, t_type_info_float);
  1272. add_type_info_type_internal(c, t_f64);
  1273. break;
  1274. }
  1275. break;
  1276. case Type_BitSet:
  1277. add_type_info_type_internal(c, bt->BitSet.elem);
  1278. add_type_info_type_internal(c, bt->BitSet.underlying);
  1279. break;
  1280. case Type_Pointer:
  1281. add_type_info_type_internal(c, bt->Pointer.elem);
  1282. break;
  1283. case Type_Array:
  1284. add_type_info_type_internal(c, bt->Array.elem);
  1285. add_type_info_type_internal(c, alloc_type_pointer(bt->Array.elem));
  1286. add_type_info_type_internal(c, t_int);
  1287. break;
  1288. case Type_EnumeratedArray:
  1289. add_type_info_type_internal(c, bt->EnumeratedArray.index);
  1290. add_type_info_type_internal(c, t_int);
  1291. add_type_info_type_internal(c, bt->EnumeratedArray.elem);
  1292. add_type_info_type_internal(c, alloc_type_pointer(bt->EnumeratedArray.elem));
  1293. break;
  1294. case Type_DynamicArray:
  1295. add_type_info_type_internal(c, bt->DynamicArray.elem);
  1296. add_type_info_type_internal(c, alloc_type_pointer(bt->DynamicArray.elem));
  1297. add_type_info_type_internal(c, t_int);
  1298. add_type_info_type_internal(c, t_allocator);
  1299. break;
  1300. case Type_Slice:
  1301. add_type_info_type_internal(c, bt->Slice.elem);
  1302. add_type_info_type_internal(c, alloc_type_pointer(bt->Slice.elem));
  1303. add_type_info_type_internal(c, t_int);
  1304. break;
  1305. case Type_Enum:
  1306. add_type_info_type_internal(c, bt->Enum.base_type);
  1307. break;
  1308. case Type_Union:
  1309. if (union_tag_size(t) > 0) {
  1310. add_type_info_type_internal(c, union_tag_type(t));
  1311. } else {
  1312. add_type_info_type_internal(c, t_type_info_ptr);
  1313. }
  1314. for_array(i, bt->Union.variants) {
  1315. add_type_info_type_internal(c, bt->Union.variants[i]);
  1316. }
  1317. break;
  1318. case Type_Struct:
  1319. if (bt->Struct.scope != nullptr) {
  1320. for_array(i, bt->Struct.scope->elements.entries) {
  1321. Entity *e = bt->Struct.scope->elements.entries[i].value;
  1322. switch (bt->Struct.soa_kind) {
  1323. case StructSoa_Dynamic:
  1324. add_type_info_type_internal(c, t_allocator);
  1325. /*fallthrough*/
  1326. case StructSoa_Slice:
  1327. case StructSoa_Fixed:
  1328. add_type_info_type_internal(c, alloc_type_pointer(e->type));
  1329. break;
  1330. default:
  1331. add_type_info_type_internal(c, e->type);
  1332. break;
  1333. }
  1334. }
  1335. }
  1336. for_array(i, bt->Struct.fields) {
  1337. Entity *f = bt->Struct.fields[i];
  1338. add_type_info_type_internal(c, f->type);
  1339. }
  1340. add_comparison_procedures_for_fields(c, bt);
  1341. break;
  1342. case Type_Map:
  1343. init_map_internal_types(bt);
  1344. add_type_info_type_internal(c, bt->Map.key);
  1345. add_type_info_type_internal(c, bt->Map.value);
  1346. add_type_info_type_internal(c, bt->Map.generated_struct_type);
  1347. break;
  1348. case Type_Tuple:
  1349. for_array(i, bt->Tuple.variables) {
  1350. Entity *var = bt->Tuple.variables[i];
  1351. add_type_info_type_internal(c, var->type);
  1352. }
  1353. break;
  1354. case Type_Proc:
  1355. add_type_info_type_internal(c, bt->Proc.params);
  1356. add_type_info_type_internal(c, bt->Proc.results);
  1357. break;
  1358. case Type_SimdVector:
  1359. add_type_info_type_internal(c, bt->SimdVector.elem);
  1360. break;
  1361. case Type_RelativePointer:
  1362. add_type_info_type_internal(c, bt->RelativePointer.pointer_type);
  1363. add_type_info_type_internal(c, bt->RelativePointer.base_integer);
  1364. break;
  1365. case Type_RelativeSlice:
  1366. add_type_info_type_internal(c, bt->RelativeSlice.slice_type);
  1367. add_type_info_type_internal(c, bt->RelativeSlice.base_integer);
  1368. break;
  1369. default:
  1370. GB_PANIC("Unhandled type: %*.s %d", LIT(type_strings[bt->kind]), bt->kind);
  1371. break;
  1372. }
  1373. }
  1374. gb_global bool global_procedure_body_in_worker_queue = false;
  1375. void check_procedure_later(CheckerContext *c, ProcInfo *info) {
  1376. GB_ASSERT(info != nullptr);
  1377. GB_ASSERT(info->decl != nullptr);
  1378. if (build_context.threaded_checker && global_procedure_body_in_worker_queue) {
  1379. GB_ASSERT(c->procs_to_check_queue != nullptr);
  1380. }
  1381. auto *queue = c->procs_to_check_queue ? c->procs_to_check_queue : &c->checker->procs_to_check_queue;
  1382. mpmc_enqueue(queue, info);
  1383. }
  1384. void check_procedure_later(CheckerContext *c, AstFile *file, Token token, DeclInfo *decl, Type *type, Ast *body, u64 tags) {
  1385. ProcInfo *info = gb_alloc_item(permanent_allocator(), ProcInfo);
  1386. info->file = file;
  1387. info->token = token;
  1388. info->decl = decl;
  1389. info->type = type;
  1390. info->body = body;
  1391. info->tags = tags;
  1392. check_procedure_later(c, info);
  1393. }
  1394. void add_min_dep_type_info(Checker *c, Type *t) {
  1395. if (t == nullptr) {
  1396. return;
  1397. }
  1398. t = default_type(t);
  1399. if (is_type_untyped(t)) {
  1400. return; // Could be nil
  1401. }
  1402. if (is_type_polymorphic(base_type(t))) {
  1403. return;
  1404. }
  1405. auto *set = &c->info.minimum_dependency_type_info_set;
  1406. isize ti_index = type_info_index(&c->info, t, false);
  1407. if (ti_index < 0) {
  1408. add_type_info_type(&c->builtin_ctx, t); // Missing the type information
  1409. ti_index = type_info_index(&c->info, t, false);
  1410. }
  1411. GB_ASSERT(ti_index >= 0);
  1412. if (ptr_set_update(set, ti_index)) {
  1413. // Type Already exists
  1414. return;
  1415. }
  1416. // Add nested types
  1417. if (t->kind == Type_Named) {
  1418. // NOTE(bill): Just in case
  1419. add_min_dep_type_info(c, t->Named.base);
  1420. return;
  1421. }
  1422. Type *bt = base_type(t);
  1423. add_min_dep_type_info(c, bt);
  1424. switch (bt->kind) {
  1425. case Type_Invalid:
  1426. break;
  1427. case Type_Basic:
  1428. switch (bt->Basic.kind) {
  1429. case Basic_string:
  1430. add_min_dep_type_info(c, t_u8_ptr);
  1431. add_min_dep_type_info(c, t_int);
  1432. break;
  1433. case Basic_any:
  1434. add_min_dep_type_info(c, t_rawptr);
  1435. add_min_dep_type_info(c, t_typeid);
  1436. break;
  1437. case Basic_complex64:
  1438. add_min_dep_type_info(c, t_type_info_float);
  1439. add_min_dep_type_info(c, t_f32);
  1440. break;
  1441. case Basic_complex128:
  1442. add_min_dep_type_info(c, t_type_info_float);
  1443. add_min_dep_type_info(c, t_f64);
  1444. break;
  1445. case Basic_quaternion128:
  1446. add_min_dep_type_info(c, t_type_info_float);
  1447. add_min_dep_type_info(c, t_f32);
  1448. break;
  1449. case Basic_quaternion256:
  1450. add_min_dep_type_info(c, t_type_info_float);
  1451. add_min_dep_type_info(c, t_f64);
  1452. break;
  1453. }
  1454. break;
  1455. case Type_BitSet:
  1456. add_min_dep_type_info(c, bt->BitSet.elem);
  1457. add_min_dep_type_info(c, bt->BitSet.underlying);
  1458. break;
  1459. case Type_Pointer:
  1460. add_min_dep_type_info(c, bt->Pointer.elem);
  1461. break;
  1462. case Type_Array:
  1463. add_min_dep_type_info(c, bt->Array.elem);
  1464. add_min_dep_type_info(c, alloc_type_pointer(bt->Array.elem));
  1465. add_min_dep_type_info(c, t_int);
  1466. break;
  1467. case Type_EnumeratedArray:
  1468. add_min_dep_type_info(c, bt->EnumeratedArray.index);
  1469. add_min_dep_type_info(c, t_int);
  1470. add_min_dep_type_info(c, bt->EnumeratedArray.elem);
  1471. add_min_dep_type_info(c, alloc_type_pointer(bt->EnumeratedArray.elem));
  1472. break;
  1473. case Type_DynamicArray:
  1474. add_min_dep_type_info(c, bt->DynamicArray.elem);
  1475. add_min_dep_type_info(c, alloc_type_pointer(bt->DynamicArray.elem));
  1476. add_min_dep_type_info(c, t_int);
  1477. add_min_dep_type_info(c, t_allocator);
  1478. break;
  1479. case Type_Slice:
  1480. add_min_dep_type_info(c, bt->Slice.elem);
  1481. add_min_dep_type_info(c, alloc_type_pointer(bt->Slice.elem));
  1482. add_min_dep_type_info(c, t_int);
  1483. break;
  1484. case Type_Enum:
  1485. add_min_dep_type_info(c, bt->Enum.base_type);
  1486. break;
  1487. case Type_Union:
  1488. if (union_tag_size(t) > 0) {
  1489. add_min_dep_type_info(c, union_tag_type(t));
  1490. } else {
  1491. add_min_dep_type_info(c, t_type_info_ptr);
  1492. }
  1493. for_array(i, bt->Union.variants) {
  1494. add_min_dep_type_info(c, bt->Union.variants[i]);
  1495. }
  1496. break;
  1497. case Type_Struct:
  1498. if (bt->Struct.scope != nullptr) {
  1499. for_array(i, bt->Struct.scope->elements.entries) {
  1500. Entity *e = bt->Struct.scope->elements.entries[i].value;
  1501. switch (bt->Struct.soa_kind) {
  1502. case StructSoa_Dynamic:
  1503. add_min_dep_type_info(c, t_allocator);
  1504. /*fallthrough*/
  1505. case StructSoa_Slice:
  1506. case StructSoa_Fixed:
  1507. add_min_dep_type_info(c, alloc_type_pointer(e->type));
  1508. break;
  1509. default:
  1510. add_min_dep_type_info(c, e->type);
  1511. break;
  1512. }
  1513. }
  1514. }
  1515. for_array(i, bt->Struct.fields) {
  1516. Entity *f = bt->Struct.fields[i];
  1517. add_min_dep_type_info(c, f->type);
  1518. }
  1519. break;
  1520. case Type_Map:
  1521. init_map_internal_types(bt);
  1522. add_min_dep_type_info(c, bt->Map.key);
  1523. add_min_dep_type_info(c, bt->Map.value);
  1524. add_min_dep_type_info(c, bt->Map.generated_struct_type);
  1525. break;
  1526. case Type_Tuple:
  1527. for_array(i, bt->Tuple.variables) {
  1528. Entity *var = bt->Tuple.variables[i];
  1529. add_min_dep_type_info(c, var->type);
  1530. }
  1531. break;
  1532. case Type_Proc:
  1533. add_min_dep_type_info(c, bt->Proc.params);
  1534. add_min_dep_type_info(c, bt->Proc.results);
  1535. break;
  1536. case Type_SimdVector:
  1537. add_min_dep_type_info(c, bt->SimdVector.elem);
  1538. break;
  1539. case Type_RelativePointer:
  1540. add_min_dep_type_info(c, bt->RelativePointer.pointer_type);
  1541. add_min_dep_type_info(c, bt->RelativePointer.base_integer);
  1542. break;
  1543. case Type_RelativeSlice:
  1544. add_min_dep_type_info(c, bt->RelativeSlice.slice_type);
  1545. add_min_dep_type_info(c, bt->RelativeSlice.base_integer);
  1546. break;
  1547. default:
  1548. GB_PANIC("Unhandled type: %*.s", LIT(type_strings[bt->kind]));
  1549. break;
  1550. }
  1551. }
  1552. void add_dependency_to_set(Checker *c, Entity *entity) {
  1553. if (entity == nullptr) {
  1554. return;
  1555. }
  1556. CheckerInfo *info = &c->info;
  1557. auto *set = &info->minimum_dependency_set;
  1558. if (entity->type != nullptr &&
  1559. is_type_polymorphic(entity->type)) {
  1560. DeclInfo *decl = decl_info_of_entity(entity);
  1561. if (decl != nullptr && decl->gen_proc_type == nullptr) {
  1562. return;
  1563. }
  1564. }
  1565. if (ptr_set_update(set, entity)) {
  1566. return;
  1567. }
  1568. DeclInfo *decl = decl_info_of_entity(entity);
  1569. if (decl == nullptr) {
  1570. return;
  1571. }
  1572. for_array(i, decl->type_info_deps.entries) {
  1573. Type *type = decl->type_info_deps.entries[i].ptr;
  1574. add_min_dep_type_info(c, type);
  1575. }
  1576. for_array(i, decl->deps.entries) {
  1577. Entity *e = decl->deps.entries[i].ptr;
  1578. add_dependency_to_set(c, e);
  1579. if (e->kind == Entity_Procedure && e->Procedure.is_foreign) {
  1580. Entity *fl = e->Procedure.foreign_library;
  1581. if (fl != nullptr) {
  1582. GB_ASSERT_MSG(fl->kind == Entity_LibraryName &&
  1583. (fl->flags&EntityFlag_Used),
  1584. "%.*s", LIT(entity->token.string));
  1585. add_dependency_to_set(c, fl);
  1586. }
  1587. } else if (e->kind == Entity_Variable && e->Variable.is_foreign) {
  1588. Entity *fl = e->Variable.foreign_library;
  1589. if (fl != nullptr) {
  1590. GB_ASSERT_MSG(fl->kind == Entity_LibraryName &&
  1591. (fl->flags&EntityFlag_Used),
  1592. "%.*s", LIT(entity->token.string));
  1593. add_dependency_to_set(c, fl);
  1594. }
  1595. }
  1596. }
  1597. }
  1598. void force_add_dependency_entity(Checker *c, Scope *scope, String const &name) {
  1599. Entity *e = scope_lookup(scope, name);
  1600. if (e == nullptr) {
  1601. return;
  1602. }
  1603. GB_ASSERT_MSG(e != nullptr, "unable to find %.*s", LIT(name));
  1604. e->flags |= EntityFlag_Used;
  1605. add_dependency_to_set(c, e);
  1606. }
  1607. void generate_minimum_dependency_set(Checker *c, Entity *start) {
  1608. isize entity_count = c->info.entities.count;
  1609. isize min_dep_set_cap = next_pow2_isize(entity_count*4); // empirically determined factor
  1610. ptr_set_init(&c->info.minimum_dependency_set, heap_allocator(), min_dep_set_cap);
  1611. ptr_set_init(&c->info.minimum_dependency_type_info_set, heap_allocator());
  1612. String required_runtime_entities[] = {
  1613. // Odin types
  1614. str_lit("Type_Info"),
  1615. str_lit("Source_Code_Location"),
  1616. str_lit("Context"),
  1617. str_lit("Allocator"),
  1618. str_lit("Logger"),
  1619. // Global variables
  1620. str_lit("args__"),
  1621. str_lit("type_table"),
  1622. // Odin internal procedures
  1623. str_lit("__init_context"),
  1624. str_lit("__type_info_of"),
  1625. str_lit("cstring_to_string"),
  1626. // Pseudo-CRT required procedures
  1627. str_lit("memset"),
  1628. str_lit("memcpy"),
  1629. str_lit("memmove"),
  1630. // Utility procedures
  1631. str_lit("memory_equal"),
  1632. str_lit("memory_compare"),
  1633. str_lit("memory_compare_zero"),
  1634. // Extended data type internal procedures
  1635. str_lit("umodti3"),
  1636. str_lit("udivti3"),
  1637. str_lit("modti3"),
  1638. str_lit("divti3"),
  1639. str_lit("fixdfti"),
  1640. str_lit("floattidf"),
  1641. str_lit("floattidf_unsigned"),
  1642. str_lit("truncsfhf2"),
  1643. str_lit("truncdfhf2"),
  1644. str_lit("gnu_h2f_ieee"),
  1645. str_lit("gnu_f2h_ieee"),
  1646. str_lit("extendhfsf2"),
  1647. };
  1648. for (isize i = 0; i < gb_count_of(required_runtime_entities); i++) {
  1649. force_add_dependency_entity(c, c->info.runtime_package->scope, required_runtime_entities[i]);
  1650. }
  1651. if (build_context.no_crt) {
  1652. String required_no_crt_entities[] = {
  1653. // NOTE(bill): Only if these exist
  1654. str_lit("_tls_index"),
  1655. str_lit("_fltused"),
  1656. };
  1657. for (isize i = 0; i < gb_count_of(required_no_crt_entities); i++) {
  1658. force_add_dependency_entity(c, c->info.runtime_package->scope, required_no_crt_entities[i]);
  1659. }
  1660. }
  1661. if (!build_context.no_bounds_check) {
  1662. String bounds_check_entities[] = {
  1663. // Bounds checking related procedures
  1664. str_lit("bounds_check_error"),
  1665. str_lit("slice_expr_error_hi"),
  1666. str_lit("slice_expr_error_lo_hi"),
  1667. };
  1668. for (isize i = 0; i < gb_count_of(bounds_check_entities); i++) {
  1669. force_add_dependency_entity(c, c->info.runtime_package->scope, bounds_check_entities[i]);
  1670. }
  1671. }
  1672. for_array(i, c->info.definitions) {
  1673. Entity *e = c->info.definitions[i];
  1674. if (e->scope == builtin_pkg->scope) { // TODO(bill): is the check enough?
  1675. if (e->type == nullptr) {
  1676. add_dependency_to_set(c, e);
  1677. }
  1678. } else if (e->kind == Entity_Procedure && e->Procedure.is_export) {
  1679. add_dependency_to_set(c, e);
  1680. } else if (e->kind == Entity_Variable && e->Variable.is_export) {
  1681. add_dependency_to_set(c, e);
  1682. }
  1683. }
  1684. for (Entity *e; mpmc_dequeue(&c->info.required_foreign_imports_through_force_queue, &e); /**/) {
  1685. array_add(&c->info.required_foreign_imports_through_force, e);
  1686. add_dependency_to_set(c, e);
  1687. }
  1688. for (Entity *e; mpmc_dequeue(&c->info.required_global_variable_queue, &e); /**/) {
  1689. e->flags |= EntityFlag_Used;
  1690. add_dependency_to_set(c, e);
  1691. }
  1692. for_array(i, c->info.entities) {
  1693. Entity *e = c->info.entities[i];
  1694. switch (e->kind) {
  1695. case Entity_Variable:
  1696. if (e->Variable.is_export) {
  1697. add_dependency_to_set(c, e);
  1698. }
  1699. break;
  1700. case Entity_Procedure:
  1701. if (e->Procedure.is_export) {
  1702. add_dependency_to_set(c, e);
  1703. }
  1704. break;
  1705. }
  1706. }
  1707. if (build_context.command_kind == Command_test) {
  1708. AstPackage *testing_package = get_core_package(&c->info, str_lit("testing"));
  1709. Scope *testing_scope = testing_package->scope;
  1710. // Add all of testing library as a dependency
  1711. for_array(i, testing_scope->elements.entries) {
  1712. Entity *e = testing_scope->elements.entries[i].value;
  1713. if (e != nullptr) {
  1714. e->flags |= EntityFlag_Used;
  1715. add_dependency_to_set(c, e);
  1716. }
  1717. }
  1718. Entity *test_signature = scope_lookup_current(testing_scope, str_lit("Test_Signature"));
  1719. AstPackage *pkg = c->info.init_package;
  1720. Scope *s = pkg->scope;
  1721. for_array(i, s->elements.entries) {
  1722. Entity *e = s->elements.entries[i].value;
  1723. if (e->kind != Entity_Procedure) {
  1724. continue;
  1725. }
  1726. // if (e->file == nullptr || !e->file->is_test) {
  1727. if (e->file == nullptr) {
  1728. continue;
  1729. }
  1730. if ((e->flags & EntityFlag_Test) == 0) {
  1731. continue;
  1732. }
  1733. String name = e->token.string;
  1734. bool is_tester = true;
  1735. Type *t = base_type(e->type);
  1736. GB_ASSERT(t->kind == Type_Proc);
  1737. if (are_types_identical(t, base_type(test_signature->type))) {
  1738. // Good
  1739. } else {
  1740. gbString str = type_to_string(t);
  1741. error(e->token, "Testing procedures must have a signature type of proc(^testing.T), got %s", str);
  1742. gb_string_free(str);
  1743. is_tester = false;
  1744. }
  1745. if (is_tester) {
  1746. add_dependency_to_set(c, e);
  1747. array_add(&c->info.testing_procedures, e);
  1748. }
  1749. }
  1750. } else if (start != nullptr) {
  1751. start->flags |= EntityFlag_Used;
  1752. add_dependency_to_set(c, start);
  1753. }
  1754. }
  1755. bool is_entity_a_dependency(Entity *e) {
  1756. if (e == nullptr) return false;
  1757. switch (e->kind) {
  1758. case Entity_Procedure:
  1759. return true;
  1760. case Entity_Constant:
  1761. case Entity_Variable:
  1762. return e->pkg != nullptr;
  1763. case Entity_TypeName:
  1764. return false;
  1765. }
  1766. return false;
  1767. }
  1768. void add_entity_dependency_from_procedure_parameters(Map<EntityGraphNode *> *M, EntityGraphNode *n, Type *tuple) {
  1769. if (tuple == nullptr) {
  1770. return;
  1771. }
  1772. Entity *e = n->entity;
  1773. bool print_deps = false;
  1774. GB_ASSERT(tuple->kind == Type_Tuple);
  1775. TypeTuple *t = &tuple->Tuple;
  1776. for_array(i, t->variables) {
  1777. Entity *v = t->variables[i];
  1778. EntityGraphNode **found = map_get(M, hash_pointer(v));
  1779. if (found == nullptr) {
  1780. continue;
  1781. }
  1782. EntityGraphNode *m = *found;
  1783. entity_graph_node_set_add(&n->succ, m);
  1784. entity_graph_node_set_add(&m->pred, n);
  1785. }
  1786. }
  1787. Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info, gbAllocator allocator) {
  1788. #define TIME_SECTION(str) do { debugf("[Section] %s\n", str); if (build_context.show_more_timings) timings_start_section(&global_timings, str_lit(str)); } while (0)
  1789. Map<EntityGraphNode *> M = {}; // Key: Entity *
  1790. map_init(&M, allocator, info->entities.count);
  1791. defer (map_destroy(&M));
  1792. for_array(i, info->entities) {
  1793. Entity *e = info->entities[i];
  1794. DeclInfo *d = e->decl_info;
  1795. if (is_entity_a_dependency(e)) {
  1796. EntityGraphNode *n = gb_alloc_item(allocator, EntityGraphNode);
  1797. n->entity = e;
  1798. map_set(&M, hash_pointer(e), n);
  1799. }
  1800. }
  1801. TIME_SECTION("generate_entity_dependency_graph: Calculate edges for graph M - Part 1");
  1802. // Calculate edges for graph M
  1803. for_array(i, M.entries) {
  1804. EntityGraphNode *n = M.entries[i].value;
  1805. Entity *e = n->entity;
  1806. DeclInfo *decl = decl_info_of_entity(e);
  1807. GB_ASSERT(decl != nullptr);
  1808. for_array(j, decl->deps.entries) {
  1809. Entity *dep = decl->deps.entries[j].ptr;
  1810. if (dep->flags & EntityFlag_Field) {
  1811. continue;
  1812. }
  1813. GB_ASSERT(dep != nullptr);
  1814. if (is_entity_a_dependency(dep)) {
  1815. EntityGraphNode **m_ = map_get(&M, hash_pointer(dep));
  1816. GB_ASSERT(m_ != nullptr);
  1817. EntityGraphNode *m = *m_;
  1818. entity_graph_node_set_add(&n->succ, m);
  1819. entity_graph_node_set_add(&m->pred, n);
  1820. }
  1821. }
  1822. }
  1823. // TODO(bill): This could be multithreaded to improve performance
  1824. // This means that the entity graph node set will have to be thread safe
  1825. TIME_SECTION("generate_entity_dependency_graph: Calculate edges for graph M - Part 2");
  1826. auto G = array_make<EntityGraphNode *>(allocator, 0, M.entries.count);
  1827. for_array(i, M.entries) {
  1828. auto *entry = &M.entries[i];
  1829. auto *e = cast(Entity *)cast(uintptr)entry->key.key;
  1830. EntityGraphNode *n = entry->value;
  1831. if (e->kind == Entity_Procedure) {
  1832. // Connect each pred 'p' of 'n' with each succ 's' and from
  1833. // the procedure node
  1834. for_array(j, n->pred.entries) {
  1835. EntityGraphNode *p = n->pred.entries[j].ptr;
  1836. // Ignore self-cycles
  1837. if (p != n) {
  1838. // Each succ 's' of 'n' becomes a succ of 'p', and
  1839. // each pred 'p' of 'n' becomes a pred of 's'
  1840. for_array(k, n->succ.entries) {
  1841. EntityGraphNode *s = n->succ.entries[k].ptr;
  1842. // Ignore self-cycles
  1843. if (s != n) {
  1844. if (p->entity->kind == Entity_Procedure &&
  1845. s->entity->kind == Entity_Procedure) {
  1846. // NOTE(bill, 2020-11-15): Only care about variable initialization ordering
  1847. // TODO(bill): This is probably wrong!!!!
  1848. continue;
  1849. }
  1850. // IMPORTANT NOTE/TODO(bill, 2020-11-15): These three calls take the majority of the
  1851. // the time to process
  1852. entity_graph_node_set_add(&p->succ, s);
  1853. entity_graph_node_set_add(&s->pred, p);
  1854. // Remove edge to 'n'
  1855. entity_graph_node_set_remove(&s->pred, n);
  1856. }
  1857. }
  1858. // Remove edge to 'n'
  1859. entity_graph_node_set_remove(&p->succ, n);
  1860. }
  1861. }
  1862. } else if (e->kind == Entity_Variable) {
  1863. array_add(&G, n);
  1864. }
  1865. }
  1866. TIME_SECTION("generate_entity_dependency_graph: Dependency Count Checker");
  1867. for_array(i, G) {
  1868. EntityGraphNode *n = G[i];
  1869. n->index = i;
  1870. n->dep_count = n->succ.entries.count;
  1871. GB_ASSERT(n->dep_count >= 0);
  1872. }
  1873. // f64 succ_count = 0.0;
  1874. // f64 pred_count = 0.0;
  1875. // f64 succ_capacity = 0.0;
  1876. // f64 pred_capacity = 0.0;
  1877. // f64 succ_max = 0.0;
  1878. // f64 pred_max = 0.0;
  1879. // for_array(i, G) {
  1880. // EntityGraphNode *n = G[i];
  1881. // succ_count += n->succ.entries.count;
  1882. // pred_count += n->pred.entries.count;
  1883. // succ_capacity += n->succ.entries.capacity;
  1884. // pred_capacity += n->pred.entries.capacity;
  1885. // succ_max = gb_max(succ_max, n->succ.entries.capacity);
  1886. // pred_max = gb_max(pred_max, n->pred.entries.capacity);
  1887. // }
  1888. // f64 count = cast(f64)G.count;
  1889. // gb_printf_err(">>>count pred: %f succ: %f\n", pred_count/count, succ_count/count);
  1890. // gb_printf_err(">>>capacity pred: %f succ: %f\n", pred_capacity/count, succ_capacity/count);
  1891. // gb_printf_err(">>>max pred: %f succ: %f\n", pred_max, succ_max);
  1892. return G;
  1893. #undef TIME_SECTION
  1894. }
  1895. void check_single_global_entity(Checker *c, Entity *e, DeclInfo *d);
  1896. Entity *find_core_entity(Checker *c, String name) {
  1897. Entity *e = scope_lookup_current(c->info.runtime_package->scope, name);
  1898. if (e == nullptr) {
  1899. compiler_error("Could not find type declaration for '%.*s'\n"
  1900. , LIT(name));
  1901. // NOTE(bill): This will exit the program as it's cannot continue without it!
  1902. }
  1903. return e;
  1904. }
  1905. Type *find_core_type(Checker *c, String name) {
  1906. Entity *e = scope_lookup_current(c->info.runtime_package->scope, name);
  1907. if (e == nullptr) {
  1908. compiler_error("Could not find type declaration for '%.*s'\n"
  1909. , LIT(name));
  1910. // NOTE(bill): This will exit the program as it's cannot continue without it!
  1911. }
  1912. if (e->type == nullptr) {
  1913. check_single_global_entity(c, e, e->decl_info);
  1914. }
  1915. GB_ASSERT(e->type != nullptr);
  1916. return e->type;
  1917. }
  1918. Entity *find_entity_in_pkg(CheckerInfo *info, String const &pkg, String const &name) {
  1919. AstPackage *package = get_core_package(info, pkg);
  1920. Entity *e = scope_lookup_current(package->scope, name);
  1921. if (e == nullptr) {
  1922. compiler_error("Could not find type declaration for '%.*s.%.*s'\n", LIT(pkg), LIT(name));
  1923. // NOTE(bill): This will exit the program as it's cannot continue without it!
  1924. }
  1925. return e;
  1926. }
  1927. Type *find_type_in_pkg(CheckerInfo *info, String const &pkg, String const &name) {
  1928. AstPackage *package = get_core_package(info, pkg);
  1929. Entity *e = scope_lookup_current(package->scope, name);
  1930. if (e == nullptr) {
  1931. compiler_error("Could not find type declaration for '%.*s.%.*s'\n", LIT(pkg), LIT(name));
  1932. // NOTE(bill): This will exit the program as it's cannot continue without it!
  1933. }
  1934. GB_ASSERT(e->type != nullptr);
  1935. return e->type;
  1936. }
  1937. CheckerTypePath *new_checker_type_path() {
  1938. gbAllocator a = heap_allocator();
  1939. auto *tp = gb_alloc_item(a, CheckerTypePath);
  1940. array_init(tp, a, 0, 16);
  1941. return tp;
  1942. }
  1943. void destroy_checker_type_path(CheckerTypePath *tp) {
  1944. array_free(tp);
  1945. gb_free(heap_allocator(), tp);
  1946. }
  1947. void check_type_path_push(CheckerContext *c, Entity *e) {
  1948. GB_ASSERT(c->type_path != nullptr);
  1949. GB_ASSERT(e != nullptr);
  1950. array_add(c->type_path, e);
  1951. }
  1952. Entity *check_type_path_pop(CheckerContext *c) {
  1953. GB_ASSERT(c->type_path != nullptr);
  1954. return array_pop(c->type_path);
  1955. }
  1956. CheckerPolyPath *new_checker_poly_path(void) {
  1957. gbAllocator a = heap_allocator();
  1958. auto *pp = gb_alloc_item(a, CheckerPolyPath);
  1959. array_init(pp, a, 0, 16);
  1960. return pp;
  1961. }
  1962. void destroy_checker_poly_path(CheckerPolyPath *pp) {
  1963. array_free(pp);
  1964. gb_free(heap_allocator(), pp);
  1965. }
  1966. void check_poly_path_push(CheckerContext *c, Type *t) {
  1967. GB_ASSERT(c->poly_path != nullptr);
  1968. GB_ASSERT(t != nullptr);
  1969. GB_ASSERT(is_type_polymorphic(t));
  1970. array_add(c->poly_path, t);
  1971. }
  1972. Type *check_poly_path_pop(CheckerContext *c) {
  1973. GB_ASSERT(c->poly_path != nullptr);
  1974. return array_pop(c->poly_path);
  1975. }
  1976. Array<Entity *> proc_group_entities(CheckerContext *c, Operand o) {
  1977. Array<Entity *> procs = {};
  1978. if (o.mode == Addressing_ProcGroup) {
  1979. GB_ASSERT(o.proc_group != nullptr);
  1980. if (o.proc_group->kind == Entity_ProcGroup) {
  1981. check_entity_decl(c, o.proc_group, nullptr, nullptr);
  1982. return o.proc_group->ProcGroup.entities;
  1983. }
  1984. }
  1985. return procs;
  1986. }
  1987. void init_core_type_info(Checker *c) {
  1988. if (t_type_info != nullptr) {
  1989. return;
  1990. }
  1991. Entity *type_info_entity = find_core_entity(c, str_lit("Type_Info"));
  1992. t_type_info = type_info_entity->type;
  1993. t_type_info_ptr = alloc_type_pointer(t_type_info);
  1994. GB_ASSERT(is_type_struct(type_info_entity->type));
  1995. TypeStruct *tis = &base_type(type_info_entity->type)->Struct;
  1996. Entity *type_info_enum_value = find_core_entity(c, str_lit("Type_Info_Enum_Value"));
  1997. t_type_info_enum_value = type_info_enum_value->type;
  1998. t_type_info_enum_value_ptr = alloc_type_pointer(t_type_info_enum_value);
  1999. GB_ASSERT(tis->fields.count == 5);
  2000. Entity *type_info_variant = tis->fields[4];
  2001. Type *tiv_type = type_info_variant->type;
  2002. GB_ASSERT(is_type_union(tiv_type));
  2003. t_type_info_named = find_core_type(c, str_lit("Type_Info_Named"));
  2004. t_type_info_integer = find_core_type(c, str_lit("Type_Info_Integer"));
  2005. t_type_info_rune = find_core_type(c, str_lit("Type_Info_Rune"));
  2006. t_type_info_float = find_core_type(c, str_lit("Type_Info_Float"));
  2007. t_type_info_quaternion = find_core_type(c, str_lit("Type_Info_Quaternion"));
  2008. t_type_info_complex = find_core_type(c, str_lit("Type_Info_Complex"));
  2009. t_type_info_string = find_core_type(c, str_lit("Type_Info_String"));
  2010. t_type_info_boolean = find_core_type(c, str_lit("Type_Info_Boolean"));
  2011. t_type_info_any = find_core_type(c, str_lit("Type_Info_Any"));
  2012. t_type_info_typeid = find_core_type(c, str_lit("Type_Info_Type_Id"));
  2013. t_type_info_pointer = find_core_type(c, str_lit("Type_Info_Pointer"));
  2014. t_type_info_procedure = find_core_type(c, str_lit("Type_Info_Procedure"));
  2015. t_type_info_array = find_core_type(c, str_lit("Type_Info_Array"));
  2016. t_type_info_enumerated_array = find_core_type(c, str_lit("Type_Info_Enumerated_Array"));
  2017. t_type_info_dynamic_array = find_core_type(c, str_lit("Type_Info_Dynamic_Array"));
  2018. t_type_info_slice = find_core_type(c, str_lit("Type_Info_Slice"));
  2019. t_type_info_tuple = find_core_type(c, str_lit("Type_Info_Tuple"));
  2020. t_type_info_struct = find_core_type(c, str_lit("Type_Info_Struct"));
  2021. t_type_info_union = find_core_type(c, str_lit("Type_Info_Union"));
  2022. t_type_info_enum = find_core_type(c, str_lit("Type_Info_Enum"));
  2023. t_type_info_map = find_core_type(c, str_lit("Type_Info_Map"));
  2024. t_type_info_bit_set = find_core_type(c, str_lit("Type_Info_Bit_Set"));
  2025. t_type_info_simd_vector = find_core_type(c, str_lit("Type_Info_Simd_Vector"));
  2026. t_type_info_relative_pointer = find_core_type(c, str_lit("Type_Info_Relative_Pointer"));
  2027. t_type_info_relative_slice = find_core_type(c, str_lit("Type_Info_Relative_Slice"));
  2028. t_type_info_named_ptr = alloc_type_pointer(t_type_info_named);
  2029. t_type_info_integer_ptr = alloc_type_pointer(t_type_info_integer);
  2030. t_type_info_rune_ptr = alloc_type_pointer(t_type_info_rune);
  2031. t_type_info_float_ptr = alloc_type_pointer(t_type_info_float);
  2032. t_type_info_quaternion_ptr = alloc_type_pointer(t_type_info_quaternion);
  2033. t_type_info_complex_ptr = alloc_type_pointer(t_type_info_complex);
  2034. t_type_info_string_ptr = alloc_type_pointer(t_type_info_string);
  2035. t_type_info_boolean_ptr = alloc_type_pointer(t_type_info_boolean);
  2036. t_type_info_any_ptr = alloc_type_pointer(t_type_info_any);
  2037. t_type_info_typeid_ptr = alloc_type_pointer(t_type_info_typeid);
  2038. t_type_info_pointer_ptr = alloc_type_pointer(t_type_info_pointer);
  2039. t_type_info_procedure_ptr = alloc_type_pointer(t_type_info_procedure);
  2040. t_type_info_array_ptr = alloc_type_pointer(t_type_info_array);
  2041. t_type_info_enumerated_array_ptr = alloc_type_pointer(t_type_info_enumerated_array);
  2042. t_type_info_dynamic_array_ptr = alloc_type_pointer(t_type_info_dynamic_array);
  2043. t_type_info_slice_ptr = alloc_type_pointer(t_type_info_slice);
  2044. t_type_info_tuple_ptr = alloc_type_pointer(t_type_info_tuple);
  2045. t_type_info_struct_ptr = alloc_type_pointer(t_type_info_struct);
  2046. t_type_info_union_ptr = alloc_type_pointer(t_type_info_union);
  2047. t_type_info_enum_ptr = alloc_type_pointer(t_type_info_enum);
  2048. t_type_info_map_ptr = alloc_type_pointer(t_type_info_map);
  2049. t_type_info_bit_set_ptr = alloc_type_pointer(t_type_info_bit_set);
  2050. t_type_info_simd_vector_ptr = alloc_type_pointer(t_type_info_simd_vector);
  2051. t_type_info_relative_pointer_ptr = alloc_type_pointer(t_type_info_relative_pointer);
  2052. t_type_info_relative_slice_ptr = alloc_type_pointer(t_type_info_relative_slice);
  2053. }
  2054. void init_mem_allocator(Checker *c) {
  2055. if (t_allocator != nullptr) {
  2056. return;
  2057. }
  2058. AstPackage *pkg = get_core_package(&c->info, str_lit("runtime"));
  2059. String name = str_lit("Allocator");
  2060. Entity *e = scope_lookup_current(pkg->scope, name);
  2061. if (e == nullptr) {
  2062. compiler_error("Could not find type declaration for '%.*s'\n", LIT(name));
  2063. // NOTE(bill): This will exit the program as it's cannot continue without it!
  2064. }
  2065. t_allocator = e->type;
  2066. t_allocator_ptr = alloc_type_pointer(t_allocator);
  2067. }
  2068. void init_core_context(Checker *c) {
  2069. if (t_context != nullptr) {
  2070. return;
  2071. }
  2072. t_context = find_core_type(c, str_lit("Context"));
  2073. GB_ASSERT(t_context != nullptr);
  2074. t_context_ptr = alloc_type_pointer(t_context);
  2075. }
  2076. void init_core_source_code_location(Checker *c) {
  2077. if (t_source_code_location != nullptr) {
  2078. return;
  2079. }
  2080. t_source_code_location = find_core_type(c, str_lit("Source_Code_Location"));
  2081. t_source_code_location_ptr = alloc_type_pointer(t_allocator);
  2082. }
  2083. void init_core_map_type(Checker *c) {
  2084. if (t_map_hash == nullptr) {
  2085. Entity *e = find_core_entity(c, str_lit("Map_Hash"));
  2086. if (e->state == EntityState_Unresolved) {
  2087. check_entity_decl(&c->builtin_ctx, e, nullptr, nullptr);
  2088. }
  2089. t_map_hash = e->type;
  2090. GB_ASSERT(t_map_hash != nullptr);
  2091. }
  2092. if (t_map_header == nullptr) {
  2093. Entity *e = find_core_entity(c, str_lit("Map_Header"));
  2094. if (e->state == EntityState_Unresolved) {
  2095. check_entity_decl(&c->builtin_ctx, e, nullptr, nullptr);
  2096. }
  2097. t_map_header = e->type;
  2098. GB_ASSERT(t_map_header != nullptr);
  2099. }
  2100. }
  2101. void init_preload(Checker *c) {
  2102. init_core_type_info(c);
  2103. init_mem_allocator(c);
  2104. init_core_context(c);
  2105. init_core_source_code_location(c);
  2106. init_core_map_type(c);
  2107. }
  2108. ExactValue check_decl_attribute_value(CheckerContext *c, Ast *value) {
  2109. ExactValue ev = {};
  2110. if (value != nullptr) {
  2111. Operand op = {};
  2112. check_expr(c, &op, value);
  2113. if (op.mode) {
  2114. if (op.mode == Addressing_Constant) {
  2115. ev = op.value;
  2116. } else {
  2117. error(value, "Expected a constant attribute element");
  2118. }
  2119. }
  2120. }
  2121. return ev;
  2122. }
  2123. DECL_ATTRIBUTE_PROC(foreign_block_decl_attribute) {
  2124. ExactValue ev = check_decl_attribute_value(c, value);
  2125. if (name == "default_calling_convention") {
  2126. if (ev.kind == ExactValue_String) {
  2127. auto cc = string_to_calling_convention(ev.value_string);
  2128. if (cc == ProcCC_Invalid) {
  2129. error(elem, "Unknown procedure calling convention: '%.*s'\n", LIT(ev.value_string));
  2130. } else {
  2131. c->foreign_context.default_cc = cc;
  2132. }
  2133. } else {
  2134. error(elem, "Expected a string value for '%.*s'", LIT(name));
  2135. }
  2136. return true;
  2137. } else if (name == "link_prefix") {
  2138. if (ev.kind == ExactValue_String) {
  2139. String link_prefix = ev.value_string;
  2140. if (!is_foreign_name_valid(link_prefix)) {
  2141. error(elem, "Invalid link prefix: '%.*s'\n", LIT(link_prefix));
  2142. } else {
  2143. c->foreign_context.link_prefix = link_prefix;
  2144. }
  2145. } else {
  2146. error(elem, "Expected a string value for '%.*s'", LIT(name));
  2147. }
  2148. return true;
  2149. } else if (name == "private") {
  2150. EntityVisiblityKind kind = EntityVisiblity_PrivateToPackage;
  2151. if (ev.kind == ExactValue_Invalid) {
  2152. // Okay
  2153. } else if (ev.kind == ExactValue_String) {
  2154. String v = ev.value_string;
  2155. if (v == "file") {
  2156. kind = EntityVisiblity_PrivateToFile;
  2157. } else if (v == "package") {
  2158. kind = EntityVisiblity_PrivateToPackage;
  2159. } else {
  2160. error(value, "'%.*s' expects no parameter, or a string literal containing \"file\" or \"package\"", LIT(name));
  2161. }
  2162. } else {
  2163. error(value, "'%.*s' expects no parameter, or a string literal containing \"file\" or \"package\"", LIT(name));
  2164. }
  2165. c->foreign_context.visibility_kind = kind;
  2166. return true;
  2167. }
  2168. return false;
  2169. }
  2170. DECL_ATTRIBUTE_PROC(proc_decl_attribute) {
  2171. if (name == "test") {
  2172. if (value != nullptr) {
  2173. error(value, "'%.*s' expects no parameter, or a string literal containing \"file\" or \"package\"", LIT(name));
  2174. }
  2175. ac->test = true;
  2176. return true;
  2177. } else if (name == "export") {
  2178. ExactValue ev = check_decl_attribute_value(c, value);
  2179. if (ev.kind == ExactValue_Invalid) {
  2180. ac->is_export = true;
  2181. } else if (ev.kind == ExactValue_Bool) {
  2182. ac->is_export = ev.value_bool;
  2183. } else {
  2184. error(value, "Expected either a boolean or no parameter for 'export'");
  2185. return false;
  2186. }
  2187. return true;
  2188. } else if (name == "deferred") {
  2189. if (value != nullptr) {
  2190. Operand o = {};
  2191. check_expr(c, &o, value);
  2192. Entity *e = entity_of_node(o.expr);
  2193. if (e != nullptr && e->kind == Entity_Procedure) {
  2194. warning(elem, "'%.*s' is deprecated, please use one of the following instead: 'deferred_none', 'deferred_in', 'deferred_out'", LIT(name));
  2195. if (ac->deferred_procedure.entity != nullptr) {
  2196. error(elem, "Previous usage of a 'deferred_*' attribute");
  2197. }
  2198. ac->deferred_procedure.kind = DeferredProcedure_out;
  2199. ac->deferred_procedure.entity = e;
  2200. return true;
  2201. }
  2202. }
  2203. error(elem, "Expected a procedure entity for '%.*s'", LIT(name));
  2204. return false;
  2205. } else if (name == "deferred_none") {
  2206. if (value != nullptr) {
  2207. Operand o = {};
  2208. check_expr(c, &o, value);
  2209. Entity *e = entity_of_node(o.expr);
  2210. if (e != nullptr && e->kind == Entity_Procedure) {
  2211. ac->deferred_procedure.kind = DeferredProcedure_none;
  2212. ac->deferred_procedure.entity = e;
  2213. return true;
  2214. }
  2215. }
  2216. error(elem, "Expected a procedure entity for '%.*s'", LIT(name));
  2217. return false;
  2218. } else if (name == "deferred_in") {
  2219. if (value != nullptr) {
  2220. Operand o = {};
  2221. check_expr(c, &o, value);
  2222. Entity *e = entity_of_node(o.expr);
  2223. if (e != nullptr && e->kind == Entity_Procedure) {
  2224. if (ac->deferred_procedure.entity != nullptr) {
  2225. error(elem, "Previous usage of a 'deferred_*' attribute");
  2226. }
  2227. ac->deferred_procedure.kind = DeferredProcedure_in;
  2228. ac->deferred_procedure.entity = e;
  2229. return true;
  2230. }
  2231. }
  2232. error(elem, "Expected a procedure entity for '%.*s'", LIT(name));
  2233. return false;
  2234. } else if (name == "deferred_out") {
  2235. if (value != nullptr) {
  2236. Operand o = {};
  2237. check_expr(c, &o, value);
  2238. Entity *e = entity_of_node(o.expr);
  2239. if (e != nullptr && e->kind == Entity_Procedure) {
  2240. if (ac->deferred_procedure.entity != nullptr) {
  2241. error(elem, "Previous usage of a 'deferred_*' attribute");
  2242. }
  2243. ac->deferred_procedure.kind = DeferredProcedure_out;
  2244. ac->deferred_procedure.entity = e;
  2245. return true;
  2246. }
  2247. }
  2248. error(elem, "Expected a procedure entity for '%.*s'", LIT(name));
  2249. return false;
  2250. } else if (name == "deferred_in_out") {
  2251. if (value != nullptr) {
  2252. Operand o = {};
  2253. check_expr(c, &o, value);
  2254. Entity *e = entity_of_node(o.expr);
  2255. if (e != nullptr && e->kind == Entity_Procedure) {
  2256. if (ac->deferred_procedure.entity != nullptr) {
  2257. error(elem, "Previous usage of a 'deferred_*' attribute");
  2258. }
  2259. ac->deferred_procedure.kind = DeferredProcedure_in_out;
  2260. ac->deferred_procedure.entity = e;
  2261. return true;
  2262. }
  2263. }
  2264. error(elem, "Expected a procedure entity for '%.*s'", LIT(name));
  2265. return false;
  2266. } else if (name == "link_name") {
  2267. ExactValue ev = check_decl_attribute_value(c, value);
  2268. if (ev.kind == ExactValue_String) {
  2269. ac->link_name = ev.value_string;
  2270. if (!is_foreign_name_valid(ac->link_name)) {
  2271. error(elem, "Invalid link name: %.*s", LIT(ac->link_name));
  2272. }
  2273. } else {
  2274. error(elem, "Expected a string value for '%.*s'", LIT(name));
  2275. }
  2276. return true;
  2277. } else if (name == "link_prefix") {
  2278. ExactValue ev = check_decl_attribute_value(c, value);
  2279. if (ev.kind == ExactValue_String) {
  2280. ac->link_prefix = ev.value_string;
  2281. if (!is_foreign_name_valid(ac->link_prefix)) {
  2282. error(elem, "Invalid link prefix: %.*s", LIT(ac->link_prefix));
  2283. }
  2284. } else {
  2285. error(elem, "Expected a string value for '%.*s'", LIT(name));
  2286. }
  2287. return true;
  2288. } else if (name == "deprecated") {
  2289. ExactValue ev = check_decl_attribute_value(c, value);
  2290. if (ev.kind == ExactValue_String) {
  2291. String msg = ev.value_string;
  2292. if (msg.len == 0) {
  2293. error(elem, "Deprecation message cannot be an empty string");
  2294. } else {
  2295. ac->deprecated_message = msg;
  2296. }
  2297. } else {
  2298. error(elem, "Expected a string value for '%.*s'", LIT(name));
  2299. }
  2300. return true;
  2301. } else if (name == "require_results") {
  2302. if (value != nullptr) {
  2303. error(elem, "Expected no value for '%.*s'", LIT(name));
  2304. }
  2305. ac->require_results = true;
  2306. return true;
  2307. } else if (name == "disabled") {
  2308. ExactValue ev = check_decl_attribute_value(c, value);
  2309. if (ev.kind == ExactValue_Bool) {
  2310. ac->has_disabled_proc = true;
  2311. ac->disabled_proc = ev.value_bool;
  2312. } else {
  2313. error(elem, "Expected a boolean value for '%.*s'", LIT(name));
  2314. }
  2315. return true;
  2316. } else if (name == "cold") {
  2317. if (value == nullptr) {
  2318. ac->set_cold = true;
  2319. } else {
  2320. ExactValue ev = check_decl_attribute_value(c, value);
  2321. if (ev.kind == ExactValue_Bool) {
  2322. ac->set_cold = ev.value_bool;
  2323. } else {
  2324. error(elem, "Expected a boolean value for '%.*s' or no value whatsoever", LIT(name));
  2325. }
  2326. }
  2327. return true;
  2328. } else if (name == "optimization_mode") {
  2329. ExactValue ev = check_decl_attribute_value(c, value);
  2330. if (ev.kind == ExactValue_String) {
  2331. String mode = ev.value_string;
  2332. if (mode == "none") {
  2333. ac->optimization_mode = ProcedureOptimizationMode_None;
  2334. } else if (mode == "minimal") {
  2335. ac->optimization_mode = ProcedureOptimizationMode_Minimal;
  2336. } else if (mode == "size") {
  2337. ac->optimization_mode = ProcedureOptimizationMode_Size;
  2338. } else if (mode == "speed") {
  2339. ac->optimization_mode = ProcedureOptimizationMode_Speed;
  2340. } else {
  2341. error(elem, "Invalid optimization_mode for '%.*s'. Valid modes:", LIT(name));
  2342. error_line("\tnone\n");
  2343. error_line("\tminimal\n");
  2344. error_line("\tsize\n");
  2345. error_line("\tspeed\n");
  2346. }
  2347. } else {
  2348. error(elem, "Expected a string for '%.*s'", LIT(name));
  2349. }
  2350. return true;
  2351. }
  2352. return false;
  2353. }
  2354. DECL_ATTRIBUTE_PROC(var_decl_attribute) {
  2355. ExactValue ev = check_decl_attribute_value(c, value);
  2356. if (name == "static") {
  2357. if (value != nullptr) {
  2358. error(elem, "'static' does not have any parameters");
  2359. }
  2360. ac->is_static = true;
  2361. return true;
  2362. } else if (name == "thread_local") {
  2363. if (ac->init_expr_list_count > 0) {
  2364. error(elem, "A thread local variable declaration cannot have initialization values");
  2365. } else if (c->foreign_context.curr_library) {
  2366. error(elem, "A foreign block variable cannot be thread local");
  2367. } else if (ac->is_export) {
  2368. error(elem, "An exported variable cannot be thread local");
  2369. } else if (ev.kind == ExactValue_Invalid) {
  2370. ac->thread_local_model = str_lit("default");
  2371. } else if (ev.kind == ExactValue_String) {
  2372. String model = ev.value_string;
  2373. if (model == "default" ||
  2374. model == "localdynamic" ||
  2375. model == "initialexec" ||
  2376. model == "localexec") {
  2377. ac->thread_local_model = model;
  2378. } else {
  2379. error(elem, "Invalid thread local model '%.*s'. Valid models:", LIT(model));
  2380. error_line("\tdefault\n");
  2381. error_line("\tlocaldynamic\n");
  2382. error_line("\tinitialexec\n");
  2383. error_line("\tlocalexec\n");
  2384. }
  2385. } else {
  2386. error(elem, "Expected either no value or a string for '%.*s'", LIT(name));
  2387. }
  2388. return true;
  2389. }
  2390. if (c->curr_proc_decl != nullptr) {
  2391. error(elem, "Only a variable at file scope can have a '%.*s'", LIT(name));
  2392. return true;
  2393. }
  2394. if (name == "require") {
  2395. if (value != nullptr) {
  2396. error(elem, "'static' does not have any parameters");
  2397. }
  2398. ac->require_declaration = true;
  2399. return true;
  2400. } else if (name == "export") {
  2401. ExactValue ev = check_decl_attribute_value(c, value);
  2402. if (ev.kind == ExactValue_Invalid) {
  2403. ac->is_export = true;
  2404. } else if (ev.kind == ExactValue_Bool) {
  2405. ac->is_export = ev.value_bool;
  2406. } else {
  2407. error(value, "Expected either a boolean or no parameter for 'export'");
  2408. return false;
  2409. }
  2410. if (ac->thread_local_model != "") {
  2411. error(elem, "An exported variable cannot be thread local");
  2412. }
  2413. return true;
  2414. } else if (name == "link_name") {
  2415. if (ev.kind == ExactValue_String) {
  2416. ac->link_name = ev.value_string;
  2417. if (!is_foreign_name_valid(ac->link_name)) {
  2418. error(elem, "Invalid link name: %.*s", LIT(ac->link_name));
  2419. }
  2420. } else {
  2421. error(elem, "Expected a string value for '%.*s'", LIT(name));
  2422. }
  2423. return true;
  2424. } else if (name == "link_prefix") {
  2425. if (ev.kind == ExactValue_String) {
  2426. ac->link_prefix = ev.value_string;
  2427. if (!is_foreign_name_valid(ac->link_prefix)) {
  2428. error(elem, "Invalid link prefix: %.*s", LIT(ac->link_prefix));
  2429. }
  2430. } else {
  2431. error(elem, "Expected a string value for '%.*s'", LIT(name));
  2432. }
  2433. return true;
  2434. } else if (name == "link_section") {
  2435. if (ev.kind == ExactValue_String) {
  2436. ac->link_section = ev.value_string;
  2437. if (!is_foreign_name_valid(ac->link_section)) {
  2438. error(elem, "Invalid link section: %.*s", LIT(ac->link_section));
  2439. }
  2440. } else {
  2441. error(elem, "Expected a string value for '%.*s'", LIT(name));
  2442. }
  2443. return true;
  2444. }
  2445. return false;
  2446. }
  2447. DECL_ATTRIBUTE_PROC(const_decl_attribute) {
  2448. if (name == "private") {
  2449. // NOTE(bill): Handled elsewhere `check_collect_value_decl`
  2450. return true;
  2451. }
  2452. return false;
  2453. }
  2454. DECL_ATTRIBUTE_PROC(type_decl_attribute) {
  2455. if (name == "private") {
  2456. // NOTE(bill): Handled elsewhere `check_collect_value_decl`
  2457. return true;
  2458. }
  2459. return false;
  2460. }
  2461. #include "check_expr.cpp"
  2462. #include "check_builtin.cpp"
  2463. #include "check_type.cpp"
  2464. #include "check_decl.cpp"
  2465. #include "check_stmt.cpp"
  2466. void check_decl_attributes(CheckerContext *c, Array<Ast *> const &attributes, DeclAttributeProc *proc, AttributeContext *ac) {
  2467. if (attributes.count == 0) return;
  2468. String original_link_prefix = {};
  2469. if (ac) {
  2470. original_link_prefix = ac->link_prefix;
  2471. }
  2472. StringSet set = {};
  2473. string_set_init(&set, heap_allocator());
  2474. defer (string_set_destroy(&set));
  2475. for_array(i, attributes) {
  2476. Ast *attr = attributes[i];
  2477. if (attr->kind != Ast_Attribute) continue;
  2478. for_array(j, attr->Attribute.elems) {
  2479. Ast *elem = attr->Attribute.elems[j];
  2480. String name = {};
  2481. Ast *value = nullptr;
  2482. switch (elem->kind) {
  2483. case_ast_node(i, Ident, elem);
  2484. name = i->token.string;
  2485. case_end;
  2486. case_ast_node(i, Implicit, elem);
  2487. name = i->string;
  2488. case_end;
  2489. case_ast_node(fv, FieldValue, elem);
  2490. if (fv->field->kind == Ast_Ident) {
  2491. name = fv->field->Ident.token.string;
  2492. } else if (fv->field->kind == Ast_Implicit) {
  2493. name = fv->field->Implicit.string;
  2494. } else {
  2495. GB_PANIC("Unknown Field Value name");
  2496. }
  2497. value = fv->value;
  2498. case_end;
  2499. default:
  2500. error(elem, "Invalid attribute element");
  2501. continue;
  2502. }
  2503. if (string_set_exists(&set, name)) {
  2504. error(elem, "Previous declaration of '%.*s'", LIT(name));
  2505. continue;
  2506. } else {
  2507. string_set_add(&set, name);
  2508. }
  2509. if (!proc(c, elem, name, value, ac)) {
  2510. if (!build_context.ignore_unknown_attributes) {
  2511. error(elem, "Unknown attribute element name '%.*s'", LIT(name));
  2512. }
  2513. }
  2514. }
  2515. }
  2516. if (ac) {
  2517. if (ac->link_prefix.text == original_link_prefix.text) {
  2518. if (ac->link_name.len > 0) {
  2519. ac->link_prefix.text = nullptr;
  2520. ac->link_prefix.len = 0;
  2521. }
  2522. }
  2523. }
  2524. }
  2525. isize get_total_value_count(Slice<Ast *> const &values) {
  2526. isize count = 0;
  2527. for_array(i, values) {
  2528. Type *t = type_of_expr(values[i]);
  2529. if (t == nullptr) {
  2530. count += 1;
  2531. continue;
  2532. }
  2533. t = core_type(t);
  2534. if (t->kind == Type_Tuple) {
  2535. count += t->Tuple.variables.count;
  2536. } else {
  2537. count += 1;
  2538. }
  2539. }
  2540. return count;
  2541. }
  2542. bool check_arity_match(CheckerContext *c, AstValueDecl *vd, bool is_global) {
  2543. isize lhs = vd->names.count;
  2544. isize rhs = 0;
  2545. if (is_global) {
  2546. // NOTE(bill): Disallow global variables to be multi-valued for a few reasons
  2547. rhs = vd->values.count;
  2548. } else {
  2549. rhs = get_total_value_count(vd->values);
  2550. }
  2551. if (rhs == 0) {
  2552. if (vd->type == nullptr) {
  2553. error(vd->names[0], "Missing type or initial expression");
  2554. return false;
  2555. }
  2556. } else if (lhs < rhs) {
  2557. if (lhs < vd->values.count) {
  2558. Ast *n = vd->values[lhs];
  2559. gbString str = expr_to_string(n);
  2560. error(n, "Extra initial expression '%s'", str);
  2561. gb_string_free(str);
  2562. } else {
  2563. error(vd->names[0], "Extra initial expression");
  2564. }
  2565. return false;
  2566. } else if (lhs > rhs) {
  2567. if (!is_global && rhs != 1) {
  2568. Ast *n = vd->names[rhs];
  2569. gbString str = expr_to_string(n);
  2570. error(n, "Missing expression for '%s'", str);
  2571. gb_string_free(str);
  2572. return false;
  2573. } else if (is_global) {
  2574. Ast *n = vd->values[rhs-1];
  2575. error(n, "Expected %td expressions on the right hand side, got %td", lhs, rhs);
  2576. error_line("Note: Global declarations do not allow for multi-valued expressions");
  2577. return false;
  2578. }
  2579. }
  2580. return true;
  2581. }
  2582. void check_collect_entities_from_when_stmt(CheckerContext *c, AstWhenStmt *ws) {
  2583. Operand operand = {Addressing_Invalid};
  2584. if (!ws->is_cond_determined) {
  2585. check_expr(c, &operand, ws->cond);
  2586. if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) {
  2587. error(ws->cond, "Non-boolean condition in 'when' statement");
  2588. }
  2589. if (operand.mode != Addressing_Constant) {
  2590. error(ws->cond, "Non-constant condition in 'when' statement");
  2591. }
  2592. ws->is_cond_determined = true;
  2593. ws->determined_cond = operand.value.kind == ExactValue_Bool && operand.value.value_bool;
  2594. }
  2595. if (ws->body == nullptr || ws->body->kind != Ast_BlockStmt) {
  2596. error(ws->cond, "Invalid body for 'when' statement");
  2597. } else {
  2598. if (ws->determined_cond) {
  2599. check_collect_entities(c, ws->body->BlockStmt.stmts);
  2600. } else if (ws->else_stmt) {
  2601. switch (ws->else_stmt->kind) {
  2602. case Ast_BlockStmt:
  2603. check_collect_entities(c, ws->else_stmt->BlockStmt.stmts);
  2604. break;
  2605. case Ast_WhenStmt:
  2606. check_collect_entities_from_when_stmt(c, &ws->else_stmt->WhenStmt);
  2607. break;
  2608. default:
  2609. error(ws->else_stmt, "Invalid 'else' statement in 'when' statement");
  2610. break;
  2611. }
  2612. }
  2613. }
  2614. }
  2615. void check_builtin_attributes(CheckerContext *ctx, Entity *e, Array<Ast *> *attributes) {
  2616. switch (e->kind) {
  2617. case Entity_ProcGroup:
  2618. case Entity_Procedure:
  2619. case Entity_TypeName:
  2620. // Okay
  2621. break;
  2622. default:
  2623. return;
  2624. }
  2625. if (!((ctx->scope->flags&ScopeFlag_File) && ctx->scope->file->pkg->kind == Package_Runtime)) {
  2626. return;
  2627. }
  2628. for_array(j, *attributes) {
  2629. Ast *attr = (*attributes)[j];
  2630. if (attr->kind != Ast_Attribute) continue;
  2631. for (isize k = 0; k < attr->Attribute.elems.count; k++) {
  2632. Ast *elem = attr->Attribute.elems[k];
  2633. String name = {};
  2634. Ast *value = nullptr;
  2635. switch (elem->kind) {
  2636. case_ast_node(i, Ident, elem);
  2637. name = i->token.string;
  2638. case_end;
  2639. case_ast_node(fv, FieldValue, elem);
  2640. GB_ASSERT(fv->field->kind == Ast_Ident);
  2641. name = fv->field->Ident.token.string;
  2642. value = fv->value;
  2643. case_end;
  2644. default:
  2645. continue;
  2646. }
  2647. if (name == "builtin") {
  2648. add_entity(ctx, builtin_pkg->scope, nullptr, e);
  2649. GB_ASSERT(scope_lookup(builtin_pkg->scope, e->token.string) != nullptr);
  2650. if (value != nullptr) {
  2651. error(value, "'builtin' cannot have a field value");
  2652. }
  2653. // Remove the builtin tag
  2654. attr->Attribute.elems[k] = attr->Attribute.elems[attr->Attribute.elems.count-1];
  2655. attr->Attribute.elems.count -= 1;
  2656. k--;
  2657. }
  2658. }
  2659. }
  2660. for (isize i = 0; i < attributes->count; i++) {
  2661. Ast *attr = (*attributes)[i];
  2662. if (attr->kind != Ast_Attribute) continue;
  2663. if (attr->Attribute.elems.count == 0) {
  2664. (*attributes)[i] = (*attributes)[attributes->count-1];
  2665. attributes->count--;
  2666. i--;
  2667. }
  2668. }
  2669. }
  2670. void check_collect_value_decl(CheckerContext *c, Ast *decl) {
  2671. if (decl->state_flags & StateFlag_BeenHandled) return;
  2672. decl->state_flags |= StateFlag_BeenHandled;
  2673. ast_node(vd, ValueDecl, decl);
  2674. EntityVisiblityKind entity_visibility_kind = c->foreign_context.visibility_kind;
  2675. bool is_test = false;
  2676. for_array(i, vd->attributes) {
  2677. Ast *attr = vd->attributes[i];
  2678. if (attr->kind != Ast_Attribute) continue;
  2679. auto *elems = &attr->Attribute.elems;
  2680. for (isize j = 0; j < elems->count; j++) {
  2681. Ast *elem = (*elems)[j];
  2682. String name = {};
  2683. Ast *value = nullptr;
  2684. switch (elem->kind) {
  2685. case_ast_node(i, Ident, elem);
  2686. name = i->token.string;
  2687. case_end;
  2688. case_ast_node(fv, FieldValue, elem);
  2689. GB_ASSERT(fv->field->kind == Ast_Ident);
  2690. name = fv->field->Ident.token.string;
  2691. value = fv->value;
  2692. case_end;
  2693. default:
  2694. continue;
  2695. }
  2696. if (name == "private") {
  2697. EntityVisiblityKind kind = EntityVisiblity_PrivateToPackage;
  2698. bool success = false;
  2699. if (value != nullptr) {
  2700. if (value->kind == Ast_BasicLit && value->BasicLit.token.kind == Token_String) {
  2701. String v = value->BasicLit.token.string;
  2702. if (v == "file") {
  2703. kind = EntityVisiblity_PrivateToFile;
  2704. success = true;
  2705. } else if (v == "package") {
  2706. kind = EntityVisiblity_PrivateToPackage;
  2707. success = true;
  2708. }
  2709. }
  2710. } else {
  2711. success = true;
  2712. }
  2713. if (!success) {
  2714. error(value, "'%.*s' expects no parameter, or a string literal containing \"file\" or \"package\"", LIT(name));
  2715. }
  2716. if (entity_visibility_kind >= kind) {
  2717. error(elem, "Previous declaration of '%.*s'", LIT(name));
  2718. } else {
  2719. entity_visibility_kind = kind;
  2720. }
  2721. slice_unordered_remove(elems, j);
  2722. j -= 1;
  2723. } else if (name == "test") {
  2724. is_test = true;
  2725. }
  2726. }
  2727. }
  2728. if (entity_visibility_kind != EntityVisiblity_Public && !(c->scope->flags&ScopeFlag_File)) {
  2729. error(decl, "Attribute 'private' is not allowed on a non file scope entity");
  2730. }
  2731. if (vd->is_mutable) {
  2732. if (!(c->scope->flags&ScopeFlag_File)) {
  2733. // NOTE(bill): local scope -> handle later and in order
  2734. return;
  2735. }
  2736. for_array(i, vd->names) {
  2737. Ast *name = vd->names[i];
  2738. Ast *value = nullptr;
  2739. if (i < vd->values.count) {
  2740. value = vd->values[i];
  2741. }
  2742. if (name->kind != Ast_Ident) {
  2743. error(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_strings[name->kind]));
  2744. continue;
  2745. }
  2746. Entity *e = alloc_entity_variable(c->scope, name->Ident.token, nullptr);
  2747. e->identifier = name;
  2748. if (entity_visibility_kind != EntityVisiblity_Public) {
  2749. e->flags |= EntityFlag_NotExported;
  2750. }
  2751. if (vd->is_using) {
  2752. vd->is_using = false; // NOTE(bill): This error will be only caught once
  2753. error(name, "'using' is not allowed at the file scope");
  2754. }
  2755. Ast *fl = c->foreign_context.curr_library;
  2756. if (fl != nullptr) {
  2757. GB_ASSERT(fl->kind == Ast_Ident);
  2758. e->Variable.is_foreign = true;
  2759. e->Variable.foreign_library_ident = fl;
  2760. e->Variable.link_prefix = c->foreign_context.link_prefix;
  2761. }
  2762. Ast *init_expr = value;
  2763. DeclInfo *d = make_decl_info(c->scope, c->decl);
  2764. d->decl_node = decl;
  2765. d->comment = vd->comment;
  2766. d->docs = vd->docs;
  2767. d->entity = e;
  2768. d->type_expr = vd->type;
  2769. d->init_expr = init_expr;
  2770. d->attributes = vd->attributes;
  2771. bool is_exported = entity_visibility_kind != EntityVisiblity_PrivateToFile;
  2772. add_entity_and_decl_info(c, name, e, d, is_exported);
  2773. }
  2774. check_arity_match(c, vd, true);
  2775. } else {
  2776. for_array(i, vd->names) {
  2777. Ast *name = vd->names[i];
  2778. if (name->kind != Ast_Ident) {
  2779. error(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_strings[name->kind]));
  2780. continue;
  2781. }
  2782. Ast *init = unparen_expr(vd->values[i]);
  2783. if (init == nullptr) {
  2784. error(name, "Expected a value for this constant value declaration");
  2785. continue;
  2786. }
  2787. Token token = name->Ident.token;
  2788. Ast *fl = c->foreign_context.curr_library;
  2789. Entity *e = nullptr;
  2790. DeclInfo *d = make_decl_info(c->scope, c->decl);
  2791. d->decl_node = decl;
  2792. d->comment = vd->comment;
  2793. d->docs = vd->docs;
  2794. d->attributes = vd->attributes;
  2795. d->type_expr = vd->type;
  2796. d->init_expr = init;
  2797. if (is_ast_type(init)) {
  2798. e = alloc_entity_type_name(d->scope, token, nullptr);
  2799. // if (vd->type != nullptr) {
  2800. // error(name, "A type declaration cannot have an type parameter");
  2801. // }
  2802. } else if (init->kind == Ast_ProcLit) {
  2803. if (c->scope->flags&ScopeFlag_Type) {
  2804. error(name, "Procedure declarations are not allowed within a struct");
  2805. continue;
  2806. }
  2807. ast_node(pl, ProcLit, init);
  2808. e = alloc_entity_procedure(d->scope, token, nullptr, pl->tags);
  2809. if (fl != nullptr) {
  2810. GB_ASSERT(fl->kind == Ast_Ident);
  2811. e->Procedure.foreign_library_ident = fl;
  2812. e->Procedure.is_foreign = true;
  2813. GB_ASSERT(pl->type->kind == Ast_ProcType);
  2814. auto cc = pl->type->ProcType.calling_convention;
  2815. if (cc == ProcCC_ForeignBlockDefault) {
  2816. cc = ProcCC_CDecl;
  2817. if (c->foreign_context.default_cc > 0) {
  2818. cc = c->foreign_context.default_cc;
  2819. }
  2820. }
  2821. e->Procedure.link_prefix = c->foreign_context.link_prefix;
  2822. GB_ASSERT(cc != ProcCC_Invalid);
  2823. pl->type->ProcType.calling_convention = cc;
  2824. }
  2825. d->proc_lit = init;
  2826. d->init_expr = init;
  2827. if (is_test) {
  2828. e->flags |= EntityFlag_Test;
  2829. }
  2830. } else if (init->kind == Ast_ProcGroup) {
  2831. ast_node(pg, ProcGroup, init);
  2832. e = alloc_entity_proc_group(d->scope, token, nullptr);
  2833. if (fl != nullptr) {
  2834. error(name, "Procedure groups are not allowed within a foreign block");
  2835. }
  2836. } else {
  2837. e = alloc_entity_constant(d->scope, token, nullptr, empty_exact_value);
  2838. }
  2839. e->identifier = name;
  2840. if (entity_visibility_kind != EntityVisiblity_Public) {
  2841. e->flags |= EntityFlag_NotExported;
  2842. }
  2843. add_entity_flags_from_file(c, e, c->scope);
  2844. if (vd->is_using) {
  2845. if (e->kind == Entity_TypeName && init->kind == Ast_EnumType) {
  2846. d->is_using = true;
  2847. } else {
  2848. error(name, "'using' is not allowed on this constant value declaration");
  2849. }
  2850. }
  2851. if (e->kind != Entity_Procedure) {
  2852. if (fl != nullptr) {
  2853. begin_error_block();
  2854. defer (end_error_block());
  2855. AstKind kind = init->kind;
  2856. error(name, "Only procedures and variables are allowed to be in a foreign block, got %.*s", LIT(ast_strings[kind]));
  2857. if (kind == Ast_ProcType) {
  2858. error_line("\tDid you forget to append '---' to the procedure?\n");
  2859. }
  2860. }
  2861. }
  2862. check_builtin_attributes(c, e, &d->attributes);
  2863. bool is_exported = entity_visibility_kind != EntityVisiblity_PrivateToFile;
  2864. add_entity_and_decl_info(c, name, e, d, is_exported);
  2865. }
  2866. check_arity_match(c, vd, true);
  2867. }
  2868. }
  2869. void check_add_foreign_block_decl(CheckerContext *ctx, Ast *decl) {
  2870. if (decl->state_flags & StateFlag_BeenHandled) return;
  2871. decl->state_flags |= StateFlag_BeenHandled;
  2872. ast_node(fb, ForeignBlockDecl, decl);
  2873. Ast *foreign_library = fb->foreign_library;
  2874. CheckerContext c = *ctx;
  2875. if (foreign_library->kind == Ast_Ident) {
  2876. c.foreign_context.curr_library = foreign_library;
  2877. } else {
  2878. error(foreign_library, "Foreign block name must be an identifier or 'export'");
  2879. c.foreign_context.curr_library = nullptr;
  2880. }
  2881. check_decl_attributes(&c, fb->attributes, foreign_block_decl_attribute, nullptr);
  2882. ast_node(block, BlockStmt, fb->body);
  2883. check_collect_entities(&c, block->stmts);
  2884. }
  2885. // NOTE(bill): If file_scopes == nullptr, this will act like a local scope
  2886. void check_collect_entities(CheckerContext *c, Slice<Ast *> const &nodes) {
  2887. AstFile *curr_file = nullptr;
  2888. if ((c->scope->flags&ScopeFlag_File) != 0) {
  2889. curr_file = c->scope->file;
  2890. GB_ASSERT(curr_file != nullptr);
  2891. }
  2892. for_array(decl_index, nodes) {
  2893. Ast *decl = nodes[decl_index];
  2894. if (!is_ast_decl(decl) && !is_ast_when_stmt(decl)) {
  2895. if (curr_file && decl->kind == Ast_ExprStmt) {
  2896. Ast *expr = decl->ExprStmt.expr;
  2897. if (expr->kind == Ast_CallExpr && expr->CallExpr.proc->kind == Ast_BasicDirective) {
  2898. if (c->collect_delayed_decls) {
  2899. if (decl->state_flags & StateFlag_BeenHandled) return;
  2900. decl->state_flags |= StateFlag_BeenHandled;
  2901. mpmc_enqueue(&curr_file->delayed_decls_queues[AstDelayQueue_Expr], expr);
  2902. }
  2903. continue;
  2904. }
  2905. }
  2906. continue;
  2907. }
  2908. switch (decl->kind) {
  2909. case_ast_node(bd, BadDecl, decl);
  2910. case_end;
  2911. case_ast_node(ws, WhenStmt, decl);
  2912. // Will be handled later
  2913. case_end;
  2914. case_ast_node(vd, ValueDecl, decl);
  2915. check_collect_value_decl(c, decl);
  2916. case_end;
  2917. case_ast_node(id, ImportDecl, decl);
  2918. if (curr_file == nullptr) {
  2919. error(decl, "import declarations are only allowed in the file scope");
  2920. // NOTE(bill): _Should_ be caught by the parser
  2921. // TODO(bill): Better error handling if it isn't
  2922. continue;
  2923. }
  2924. // Will be handled later
  2925. mpmc_enqueue(&curr_file->delayed_decls_queues[AstDelayQueue_Import], decl);
  2926. case_end;
  2927. case_ast_node(fl, ForeignImportDecl, decl);
  2928. if ((c->scope->flags&ScopeFlag_File) == 0) {
  2929. error(decl, "%.*s declarations are only allowed in the file scope", LIT(fl->token.string));
  2930. // NOTE(bill): _Should_ be caught by the parser
  2931. // TODO(bill): Better error handling if it isn't
  2932. continue;
  2933. }
  2934. check_add_foreign_import_decl(c, decl);
  2935. case_end;
  2936. case_ast_node(fb, ForeignBlockDecl, decl);
  2937. check_add_foreign_block_decl(c, decl);
  2938. case_end;
  2939. default:
  2940. if (c->scope->flags&ScopeFlag_File) {
  2941. error(decl, "Only declarations are allowed at file scope");
  2942. }
  2943. break;
  2944. }
  2945. }
  2946. // NOTE(bill): 'when' stmts need to be handled after the other as the condition may refer to something
  2947. // declared after this stmt in source
  2948. if (curr_file == nullptr) {
  2949. for_array(decl_index, nodes) {
  2950. Ast *decl = nodes[decl_index];
  2951. if (decl->kind == Ast_WhenStmt) {
  2952. check_collect_entities_from_when_stmt(c, &decl->WhenStmt);
  2953. }
  2954. }
  2955. }
  2956. }
  2957. CheckerContext *create_checker_context(Checker *c) {
  2958. CheckerContext *ctx = gb_alloc_item(permanent_allocator(), CheckerContext);
  2959. *ctx = make_checker_context(c);
  2960. return ctx;
  2961. }
  2962. void check_single_global_entity(Checker *c, Entity *e, DeclInfo *d) {
  2963. GB_ASSERT(e != nullptr);
  2964. GB_ASSERT(d != nullptr);
  2965. if (d->scope != e->scope) {
  2966. return;
  2967. }
  2968. if (e->state == EntityState_Resolved) {
  2969. return;
  2970. }
  2971. CheckerContext *ctx = create_checker_context(c);
  2972. GB_ASSERT(d->scope->flags&ScopeFlag_File);
  2973. AstFile *file = d->scope->file;
  2974. add_curr_ast_file(ctx, file);
  2975. AstPackage *pkg = file->pkg;
  2976. GB_ASSERT(ctx->pkg != nullptr);
  2977. GB_ASSERT(e->pkg != nullptr);
  2978. ctx->decl = d;
  2979. ctx->scope = d->scope;
  2980. if (pkg->kind == Package_Init) {
  2981. if (e->kind != Entity_Procedure && e->token.string == "main") {
  2982. error(e->token, "'main' is reserved as the entry point procedure in the initial scope");
  2983. return;
  2984. }
  2985. } else if (pkg->kind == Package_Runtime) {
  2986. if (e->token.string == "main") {
  2987. error(e->token, "'main' is reserved as the entry point procedure in the initial scope");
  2988. return;
  2989. }
  2990. }
  2991. check_entity_decl(ctx, e, d, nullptr);
  2992. }
  2993. void check_all_global_entities(Checker *c) {
  2994. // NOTE(bill): This must be single threaded
  2995. // Don't bother trying
  2996. for_array(i, c->info.entities) {
  2997. Entity *e = c->info.entities[i];
  2998. if (e->flags & EntityFlag_Lazy) {
  2999. continue;
  3000. }
  3001. DeclInfo *d = e->decl_info;
  3002. check_single_global_entity(c, e, d);
  3003. if (e->type != nullptr && is_type_typed(e->type)) {
  3004. (void)type_size_of(e->type);
  3005. (void)type_align_of(e->type);
  3006. }
  3007. }
  3008. }
  3009. bool is_string_an_identifier(String s) {
  3010. isize offset = 0;
  3011. if (s.len < 1) {
  3012. return false;
  3013. }
  3014. while (offset < s.len) {
  3015. bool ok = false;
  3016. Rune r = -1;
  3017. isize size = gb_utf8_decode(s.text+offset, s.len-offset, &r);
  3018. if (offset == 0) {
  3019. ok = rune_is_letter(r);
  3020. } else {
  3021. ok = rune_is_letter(r) || rune_is_digit(r);
  3022. }
  3023. if (!ok) {
  3024. return false;
  3025. }
  3026. offset += size;
  3027. }
  3028. return offset == s.len;
  3029. }
  3030. String path_to_entity_name(String name, String fullpath, bool strip_extension=true) {
  3031. if (name.len != 0) {
  3032. return name;
  3033. }
  3034. // NOTE(bill): use file name (without extension) as the identifier
  3035. // If it is a valid identifier
  3036. String filename = fullpath;
  3037. isize slash = 0;
  3038. isize dot = 0;
  3039. for (isize i = filename.len-1; i >= 0; i--) {
  3040. u8 c = filename[i];
  3041. if (c == '/' || c == '\\') {
  3042. break;
  3043. }
  3044. slash = i;
  3045. }
  3046. filename = substring(filename, slash, filename.len);
  3047. if (strip_extension) {
  3048. dot = filename.len;
  3049. while (dot --> 0) {
  3050. u8 c = filename[dot];
  3051. if (c == '.') {
  3052. break;
  3053. }
  3054. }
  3055. if (dot > 0) {
  3056. filename = substring(filename, 0, dot);
  3057. }
  3058. }
  3059. if (is_string_an_identifier(filename)) {
  3060. return filename;
  3061. } else {
  3062. return str_lit("_");
  3063. }
  3064. }
  3065. #if 1
  3066. void add_import_dependency_node(Checker *c, Ast *decl, Map<ImportGraphNode *> *M) {
  3067. AstPackage *parent_pkg = decl->file->pkg;
  3068. switch (decl->kind) {
  3069. case_ast_node(id, ImportDecl, decl);
  3070. String path = id->fullpath;
  3071. if (is_package_name_reserved(path)) {
  3072. return;
  3073. }
  3074. AstPackage **found = string_map_get(&c->info.packages, path);
  3075. if (found == nullptr) {
  3076. for_array(pkg_index, c->info.packages.entries) {
  3077. AstPackage *pkg = c->info.packages.entries[pkg_index].value;
  3078. gb_printf_err("%.*s\n", LIT(pkg->fullpath));
  3079. }
  3080. Token token = ast_token(decl);
  3081. gb_printf_err("%s\n", token_pos_to_string(token.pos));
  3082. GB_PANIC("Unable to find package: %.*s", LIT(path));
  3083. }
  3084. AstPackage *pkg = *found;
  3085. GB_ASSERT(pkg->scope != nullptr);
  3086. id->package = pkg;
  3087. ImportGraphNode **found_node = nullptr;
  3088. ImportGraphNode *m = nullptr;
  3089. ImportGraphNode *n = nullptr;
  3090. found_node = map_get(M, hash_pointer(pkg));
  3091. GB_ASSERT(found_node != nullptr);
  3092. m = *found_node;
  3093. found_node = map_get(M, hash_pointer(parent_pkg));
  3094. GB_ASSERT(found_node != nullptr);
  3095. n = *found_node;
  3096. // TODO(bill): How should the edges be attached for 'import'?
  3097. import_graph_node_set_add(&n->succ, m);
  3098. import_graph_node_set_add(&m->pred, n);
  3099. ptr_set_add(&m->scope->imported, n->scope);
  3100. case_end;
  3101. case_ast_node(ws, WhenStmt, decl);
  3102. if (ws->body != nullptr) {
  3103. auto stmts = ws->body->BlockStmt.stmts;
  3104. for_array(i, stmts) {
  3105. add_import_dependency_node(c, stmts[i], M);
  3106. }
  3107. }
  3108. if (ws->else_stmt != nullptr) {
  3109. switch (ws->else_stmt->kind) {
  3110. case Ast_BlockStmt: {
  3111. auto stmts = ws->else_stmt->BlockStmt.stmts;
  3112. for_array(i, stmts) {
  3113. add_import_dependency_node(c, stmts[i], M);
  3114. }
  3115. break;
  3116. }
  3117. case Ast_WhenStmt:
  3118. add_import_dependency_node(c, ws->else_stmt, M);
  3119. break;
  3120. }
  3121. }
  3122. case_end;
  3123. }
  3124. }
  3125. Array<ImportGraphNode *> generate_import_dependency_graph(Checker *c) {
  3126. Map<ImportGraphNode *> M = {}; // Key: AstPackage *
  3127. map_init(&M, heap_allocator(), 2*c->parser->packages.count);
  3128. defer (map_destroy(&M));
  3129. for_array(i, c->parser->packages) {
  3130. AstPackage *pkg = c->parser->packages[i];
  3131. ImportGraphNode *n = import_graph_node_create(heap_allocator(), pkg);
  3132. map_set(&M, hash_pointer(pkg), n);
  3133. }
  3134. // Calculate edges for graph M
  3135. for_array(i, c->parser->packages) {
  3136. AstPackage *p = c->parser->packages[i];
  3137. for_array(j, p->files) {
  3138. AstFile *f = p->files[j];
  3139. for_array(k, f->decls) {
  3140. Ast *decl = f->decls[k];
  3141. add_import_dependency_node(c, decl, &M);
  3142. }
  3143. }
  3144. }
  3145. Array<ImportGraphNode *> G = {};
  3146. array_init(&G, heap_allocator(), 0, M.entries.count);
  3147. for_array(i, M.entries) {
  3148. auto n = M.entries[i].value;
  3149. n->index = i;
  3150. n->dep_count = n->succ.entries.count;
  3151. GB_ASSERT(n->dep_count >= 0);
  3152. array_add(&G, n);
  3153. }
  3154. return G;
  3155. }
  3156. struct ImportPathItem {
  3157. AstPackage *pkg;
  3158. Ast * decl;
  3159. };
  3160. Array<ImportPathItem> find_import_path(Checker *c, AstPackage *start, AstPackage *end, PtrSet<AstPackage *> *visited) {
  3161. Array<ImportPathItem> empty_path = {};
  3162. if (ptr_set_update(visited, start)) {
  3163. return empty_path;
  3164. }
  3165. String path = start->fullpath;
  3166. AstPackage **found = string_map_get(&c->info.packages, path);
  3167. if (found) {
  3168. AstPackage *pkg = *found;
  3169. GB_ASSERT(pkg != nullptr);
  3170. for_array(i, pkg->files) {
  3171. AstFile *f = pkg->files[i];
  3172. for_array(j, f->imports) {
  3173. AstPackage *pkg = nullptr;
  3174. Ast *decl = f->imports[j];
  3175. if (decl->kind == Ast_ImportDecl) {
  3176. pkg = decl->ImportDecl.package;
  3177. } else {
  3178. continue;
  3179. }
  3180. if (pkg == nullptr || pkg->scope == nullptr) {
  3181. continue;
  3182. }
  3183. if (pkg->kind == Package_Runtime) {
  3184. // NOTE(bill): Allow cyclic imports within the runtime package for the time being
  3185. continue;
  3186. }
  3187. ImportPathItem item = {pkg, decl};
  3188. if (pkg == end) {
  3189. auto path = array_make<ImportPathItem>(heap_allocator());
  3190. array_add(&path, item);
  3191. return path;
  3192. }
  3193. auto next_path = find_import_path(c, pkg, end, visited);
  3194. if (next_path.count > 0) {
  3195. array_add(&next_path, item);
  3196. return next_path;
  3197. }
  3198. }
  3199. }
  3200. }
  3201. return empty_path;
  3202. }
  3203. #endif
  3204. String get_invalid_import_name(String input) {
  3205. isize slash = 0;
  3206. for (isize i = input.len-1; i >= 0; i--) {
  3207. if (input[i] == '/' || input[i] == '\\') {
  3208. break;
  3209. }
  3210. slash = i;
  3211. }
  3212. input = substring(input, slash, input.len);
  3213. return input;
  3214. }
  3215. void check_add_import_decl(CheckerContext *ctx, Ast *decl) {
  3216. if (decl->state_flags & StateFlag_BeenHandled) return;
  3217. decl->state_flags |= StateFlag_BeenHandled;
  3218. ast_node(id, ImportDecl, decl);
  3219. Token token = id->relpath;
  3220. Scope *parent_scope = ctx->scope;
  3221. GB_ASSERT(parent_scope->flags&ScopeFlag_File);
  3222. auto *pkgs = &ctx->checker->info.packages;
  3223. Scope *scope = nullptr;
  3224. bool force_use = false;
  3225. if (id->fullpath == "builtin") {
  3226. scope = builtin_pkg->scope;
  3227. force_use = true;
  3228. } else if (id->fullpath == "intrinsics") {
  3229. scope = intrinsics_pkg->scope;
  3230. force_use = true;
  3231. } else {
  3232. AstPackage **found = string_map_get(pkgs, id->fullpath);
  3233. if (found == nullptr) {
  3234. for_array(pkg_index, pkgs->entries) {
  3235. AstPackage *pkg = pkgs->entries[pkg_index].value;
  3236. gb_printf_err("%.*s\n", LIT(pkg->fullpath));
  3237. }
  3238. gb_printf_err("%s\n", token_pos_to_string(token.pos));
  3239. GB_PANIC("Unable to find scope for package: %.*s", LIT(id->fullpath));
  3240. } else {
  3241. AstPackage *pkg = *found;
  3242. scope = pkg->scope;
  3243. }
  3244. }
  3245. GB_ASSERT(scope->flags&ScopeFlag_Pkg);
  3246. if (ptr_set_update(&parent_scope->imported, scope)) {
  3247. // error(token, "Multiple import of the same file within this scope");
  3248. }
  3249. String import_name = path_to_entity_name(id->import_name.string, id->fullpath, false);
  3250. // NOTE(bill, 2019-05-19): If the directory path is not a valid entity name, force the user to assign a custom one
  3251. // if (import_name.len == 0 || import_name == "_") {
  3252. // import_name = scope->pkg->name;
  3253. // }
  3254. if (import_name.len == 0 || is_blank_ident(import_name)) {
  3255. if (id->is_using) {
  3256. // TODO(bill): Should this be a warning?
  3257. } else {
  3258. if (id->import_name.string == "") {
  3259. String invalid_name = id->fullpath;
  3260. invalid_name = get_invalid_import_name(invalid_name);
  3261. error(id->token, "Import name %.*s, is not a valid identifier. Perhaps you want to reference the package by a different name like this: import <new_name> \"%.*s\" ", LIT(invalid_name), LIT(invalid_name));
  3262. } else {
  3263. error(token, "Import name, %.*s, cannot be use as an import name as it is not a valid identifier", LIT(id->import_name.string));
  3264. }
  3265. }
  3266. } else {
  3267. GB_ASSERT(id->import_name.pos.line != 0);
  3268. id->import_name.string = import_name;
  3269. Entity *e = alloc_entity_import_name(parent_scope, id->import_name, t_invalid,
  3270. id->fullpath, id->import_name.string,
  3271. scope);
  3272. add_entity(ctx, parent_scope, nullptr, e);
  3273. if (force_use || id->is_using) {
  3274. add_entity_use(ctx, nullptr, e);
  3275. }
  3276. }
  3277. if (id->is_using) {
  3278. if (parent_scope->flags & ScopeFlag_Global) {
  3279. error(id->import_name, "built-in package imports cannot use using");
  3280. return;
  3281. }
  3282. // NOTE(bill): Add imported entities to this file's scope
  3283. for_array(elem_index, scope->elements.entries) {
  3284. String name = scope->elements.entries[elem_index].key.string;
  3285. Entity *e = scope->elements.entries[elem_index].value;
  3286. if (e->scope == parent_scope) continue;
  3287. if (is_entity_exported(e, true)) {
  3288. Entity *found = scope_lookup_current(parent_scope, name);
  3289. if (found != nullptr) {
  3290. // NOTE(bill):
  3291. // Date: 2019-03-17
  3292. // The order has to be the other way around as `using` adds the entity into the that
  3293. // file scope otherwise the error would be the wrong way around
  3294. redeclaration_error(name, found, e);
  3295. } else {
  3296. add_entity_with_name(ctx, parent_scope, e->identifier, e, name);
  3297. }
  3298. }
  3299. }
  3300. }
  3301. scope->flags |= ScopeFlag_HasBeenImported;
  3302. }
  3303. DECL_ATTRIBUTE_PROC(foreign_import_decl_attribute) {
  3304. if (name == "force" || name == "require") {
  3305. if (value != nullptr) {
  3306. error(elem, "Expected no parameter for '%.*s'", LIT(name));
  3307. }
  3308. ac->require_declaration = true;
  3309. return true;
  3310. }
  3311. return false;
  3312. }
  3313. void check_add_foreign_import_decl(CheckerContext *ctx, Ast *decl) {
  3314. if (decl->state_flags & StateFlag_BeenHandled) return;
  3315. decl->state_flags |= StateFlag_BeenHandled;
  3316. ast_node(fl, ForeignImportDecl, decl);
  3317. Scope *parent_scope = ctx->scope;
  3318. GB_ASSERT(parent_scope->flags&ScopeFlag_File);
  3319. GB_ASSERT(fl->fullpaths.count > 0);
  3320. String fullpath = fl->fullpaths[0];
  3321. String library_name = path_to_entity_name(fl->library_name.string, fullpath);
  3322. if (is_blank_ident(library_name)) {
  3323. error(fl->token, "File name, %.*s, cannot be as a library name as it is not a valid identifier", LIT(fl->library_name.string));
  3324. return;
  3325. }
  3326. // if (fl->collection_name != "system") {
  3327. // char *c_str = gb_alloc_array(heap_allocator(), char, fullpath.len+1);
  3328. // defer (gb_free(heap_allocator(), c_str));
  3329. // gb_memmove(c_str, fullpath.text, fullpath.len);
  3330. // c_str[fullpath.len] = '\0';
  3331. // gbFile f = {};
  3332. // gbFileError file_err = gb_file_open(&f, c_str);
  3333. // defer (gb_file_close(&f));
  3334. // switch (file_err) {
  3335. // case gbFileError_Invalid:
  3336. // error(decl, "Invalid file or cannot be found ('%.*s')", LIT(fullpath));
  3337. // return;
  3338. // case gbFileError_NotExists:
  3339. // error(decl, "File cannot be found ('%.*s')", LIT(fullpath));
  3340. // return;
  3341. // }
  3342. // }
  3343. GB_ASSERT(fl->library_name.pos.line != 0);
  3344. fl->library_name.string = library_name;
  3345. Entity *e = alloc_entity_library_name(parent_scope, fl->library_name, t_invalid,
  3346. fl->fullpaths, library_name);
  3347. add_entity_flags_from_file(ctx, e, parent_scope);
  3348. add_entity(ctx, parent_scope, nullptr, e);
  3349. AttributeContext ac = {};
  3350. check_decl_attributes(ctx, fl->attributes, foreign_import_decl_attribute, &ac);
  3351. if (ac.require_declaration) {
  3352. mpmc_enqueue(&ctx->info->required_foreign_imports_through_force_queue, e);
  3353. add_entity_use(ctx, nullptr, e);
  3354. }
  3355. }
  3356. // Returns true if a new package is present
  3357. bool collect_file_decls(CheckerContext *ctx, Slice<Ast *> const &decls);
  3358. bool collect_file_decls_from_when_stmt(CheckerContext *ctx, AstWhenStmt *ws);
  3359. bool collect_when_stmt_from_file(CheckerContext *ctx, AstWhenStmt *ws) {
  3360. Operand operand = {Addressing_Invalid};
  3361. if (!ws->is_cond_determined) {
  3362. check_expr(ctx, &operand, ws->cond);
  3363. if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) {
  3364. error(ws->cond, "Non-boolean condition in 'when' statement");
  3365. }
  3366. if (operand.mode != Addressing_Constant) {
  3367. error(ws->cond, "Non-constant condition in 'when' statement");
  3368. }
  3369. ws->is_cond_determined = true;
  3370. ws->determined_cond = operand.value.kind == ExactValue_Bool && operand.value.value_bool;
  3371. }
  3372. if (ws->body == nullptr || ws->body->kind != Ast_BlockStmt) {
  3373. error(ws->cond, "Invalid body for 'when' statement");
  3374. } else {
  3375. if (ws->determined_cond) {
  3376. check_collect_entities(ctx, ws->body->BlockStmt.stmts);
  3377. return true;
  3378. } else if (ws->else_stmt) {
  3379. switch (ws->else_stmt->kind) {
  3380. case Ast_BlockStmt:
  3381. check_collect_entities(ctx, ws->else_stmt->BlockStmt.stmts);
  3382. return true;
  3383. case Ast_WhenStmt:
  3384. collect_when_stmt_from_file(ctx, &ws->else_stmt->WhenStmt);
  3385. return true;
  3386. default:
  3387. error(ws->else_stmt, "Invalid 'else' statement in 'when' statement");
  3388. break;
  3389. }
  3390. }
  3391. }
  3392. return false;
  3393. }
  3394. bool collect_file_decls_from_when_stmt(CheckerContext *ctx, AstWhenStmt *ws) {
  3395. Operand operand = {Addressing_Invalid};
  3396. if (!ws->is_cond_determined) {
  3397. check_expr(ctx, &operand, ws->cond);
  3398. if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) {
  3399. error(ws->cond, "Non-boolean condition in 'when' statement");
  3400. }
  3401. if (operand.mode != Addressing_Constant) {
  3402. error(ws->cond, "Non-constant condition in 'when' statement");
  3403. }
  3404. ws->is_cond_determined = true;
  3405. ws->determined_cond = operand.value.kind == ExactValue_Bool && operand.value.value_bool;
  3406. }
  3407. if (ws->body == nullptr || ws->body->kind != Ast_BlockStmt) {
  3408. error(ws->cond, "Invalid body for 'when' statement");
  3409. } else {
  3410. if (ws->determined_cond) {
  3411. return collect_file_decls(ctx, ws->body->BlockStmt.stmts);
  3412. } else if (ws->else_stmt) {
  3413. switch (ws->else_stmt->kind) {
  3414. case Ast_BlockStmt:
  3415. return collect_file_decls(ctx, ws->else_stmt->BlockStmt.stmts);
  3416. case Ast_WhenStmt:
  3417. return collect_file_decls_from_when_stmt(ctx, &ws->else_stmt->WhenStmt);
  3418. default:
  3419. error(ws->else_stmt, "Invalid 'else' statement in 'when' statement");
  3420. break;
  3421. }
  3422. }
  3423. }
  3424. return false;
  3425. }
  3426. bool collect_file_decl(CheckerContext *ctx, Ast *decl) {
  3427. GB_ASSERT(ctx->scope->flags&ScopeFlag_File);
  3428. AstFile *curr_file = ctx->scope->file;
  3429. GB_ASSERT(curr_file != nullptr);
  3430. if (decl->state_flags & StateFlag_BeenHandled) {
  3431. return false;
  3432. }
  3433. switch (decl->kind) {
  3434. case_ast_node(vd, ValueDecl, decl);
  3435. check_collect_value_decl(ctx, decl);
  3436. case_end;
  3437. case_ast_node(id, ImportDecl, decl);
  3438. check_add_import_decl(ctx, decl);
  3439. case_end;
  3440. case_ast_node(fl, ForeignImportDecl, decl);
  3441. check_add_foreign_import_decl(ctx, decl);
  3442. case_end;
  3443. case_ast_node(fb, ForeignBlockDecl, decl);
  3444. check_add_foreign_block_decl(ctx, decl);
  3445. case_end;
  3446. case_ast_node(ws, WhenStmt, decl);
  3447. if (!ws->is_cond_determined) {
  3448. if (collect_when_stmt_from_file(ctx, ws)) {
  3449. return true;
  3450. }
  3451. CheckerContext nctx = *ctx;
  3452. nctx.collect_delayed_decls = true;
  3453. if (collect_file_decls_from_when_stmt(&nctx, ws)) {
  3454. return true;
  3455. }
  3456. } else {
  3457. CheckerContext nctx = *ctx;
  3458. nctx.collect_delayed_decls = true;
  3459. if (collect_file_decls_from_when_stmt(&nctx, ws)) {
  3460. return true;
  3461. }
  3462. }
  3463. case_end;
  3464. case_ast_node(es, ExprStmt, decl);
  3465. GB_ASSERT(ctx->collect_delayed_decls);
  3466. decl->state_flags |= StateFlag_BeenHandled;
  3467. if (es->expr->kind == Ast_CallExpr) {
  3468. ast_node(ce, CallExpr, es->expr);
  3469. if (ce->proc->kind == Ast_BasicDirective) {
  3470. mpmc_enqueue(&curr_file->delayed_decls_queues[AstDelayQueue_Expr], es->expr);
  3471. }
  3472. }
  3473. case_end;
  3474. }
  3475. return false;
  3476. }
  3477. bool collect_file_decls(CheckerContext *ctx, Slice<Ast *> const &decls) {
  3478. GB_ASSERT(ctx->scope->flags&ScopeFlag_File);
  3479. for_array(i, decls) {
  3480. if (collect_file_decl(ctx, decls[i])) {
  3481. return true;
  3482. }
  3483. }
  3484. return false;
  3485. }
  3486. void check_create_file_scopes(Checker *c) {
  3487. for_array(i, c->parser->packages) {
  3488. AstPackage *pkg = c->parser->packages[i];
  3489. isize total_pkg_decl_count = 0;
  3490. for_array(j, pkg->files) {
  3491. AstFile *f = pkg->files[j];
  3492. string_map_set(&c->info.files, f->fullpath, f);
  3493. create_scope_from_file(nullptr, f);
  3494. total_pkg_decl_count += f->total_file_decl_count;
  3495. }
  3496. mpmc_init(&pkg->exported_entity_queue, heap_allocator(), total_pkg_decl_count);
  3497. }
  3498. }
  3499. struct ThreadProcCheckerSection {
  3500. Checker *checker;
  3501. isize offset;
  3502. isize count;
  3503. };
  3504. void check_with_workers(Checker *c, gbThreadProc *proc, isize total_count) {
  3505. isize thread_count = gb_max(build_context.thread_count, 1);
  3506. isize worker_count = thread_count-1; // NOTE(bill): The main thread will also be used for work
  3507. if (!build_context.threaded_checker) {
  3508. worker_count = 0;
  3509. }
  3510. gb_semaphore_post(&c->info.collect_semaphore, cast(i32)thread_count);
  3511. if (worker_count == 0) {
  3512. ThreadProcCheckerSection section_all = {};
  3513. section_all.checker = c;
  3514. section_all.offset = 0;
  3515. section_all.count = total_count;
  3516. gbThread dummy_main_thread = {};
  3517. dummy_main_thread.user_data = &section_all;
  3518. proc(&dummy_main_thread);
  3519. return;
  3520. }
  3521. isize file_load_count = (total_count+thread_count-1)/thread_count;
  3522. isize remaining_count = total_count;
  3523. ThreadProcCheckerSection *thread_data = gb_alloc_array(permanent_allocator(), ThreadProcCheckerSection, thread_count);
  3524. for (isize i = 0; i < thread_count; i++) {
  3525. ThreadProcCheckerSection *data = thread_data + i;
  3526. data->checker = c;
  3527. data->offset = total_count-remaining_count;
  3528. data->count = file_load_count;
  3529. remaining_count -= file_load_count;
  3530. }
  3531. GB_ASSERT(remaining_count <= 0);
  3532. gbThread *threads = gb_alloc_array(permanent_allocator(), gbThread, worker_count);
  3533. for (isize i = 0; i < worker_count; i++) {
  3534. gb_thread_init(threads+i);
  3535. }
  3536. for (isize i = 0; i < worker_count; i++) {
  3537. gb_thread_start(threads+i, proc, thread_data+i);
  3538. }
  3539. gbThread dummy_main_thread = {};
  3540. dummy_main_thread.user_data = thread_data+worker_count;
  3541. proc(&dummy_main_thread);
  3542. gb_semaphore_wait(&c->info.collect_semaphore);
  3543. for (isize i = 0; i < worker_count; i++) {
  3544. gb_thread_destroy(threads+i);
  3545. }
  3546. }
  3547. GB_THREAD_PROC(thread_proc_collect_entities) {
  3548. auto *data = cast(ThreadProcCheckerSection *)thread->user_data;
  3549. Checker *c = data->checker;
  3550. CheckerContext collect_entity_ctx = make_checker_context(c);
  3551. defer (destroy_checker_context(&collect_entity_ctx));
  3552. CheckerContext *ctx = &collect_entity_ctx;
  3553. UntypedExprInfoMap untyped = {};
  3554. map_init(&untyped, heap_allocator());
  3555. defer (map_destroy(&untyped));
  3556. isize offset = data->offset;
  3557. isize file_end = gb_min(offset+data->count, c->info.files.entries.count);
  3558. for (isize i = offset; i < file_end; i++) {
  3559. AstFile *f = c->info.files.entries[i].value;
  3560. reset_checker_context(ctx, f, &untyped);
  3561. check_collect_entities(ctx, f->decls);
  3562. GB_ASSERT(ctx->collect_delayed_decls == false);
  3563. add_untyped_expressions(&c->info, ctx->untyped);
  3564. }
  3565. gb_semaphore_release(&c->info.collect_semaphore);
  3566. return 0;
  3567. }
  3568. void check_collect_entities_all(Checker *c) {
  3569. check_with_workers(c, thread_proc_collect_entities, c->info.files.entries.count);
  3570. }
  3571. void check_export_entites_in_pkg(CheckerContext *ctx, AstPackage *pkg, UntypedExprInfoMap *untyped) {
  3572. if (pkg->files.count != 0) {
  3573. AstPackageExportedEntity item = {};
  3574. while (mpmc_dequeue(&pkg->exported_entity_queue, &item)) {
  3575. AstFile *f = item.entity->file;
  3576. if (ctx->file != f) {
  3577. reset_checker_context(ctx, f, untyped);
  3578. }
  3579. add_entity(ctx, pkg->scope, item.identifier, item.entity);
  3580. add_untyped_expressions(ctx->info, untyped);
  3581. }
  3582. }
  3583. }
  3584. GB_THREAD_PROC(thread_proc_check_export_entites) {
  3585. auto data = cast(ThreadProcCheckerSection *)thread->user_data;
  3586. Checker *c = data->checker;
  3587. CheckerContext ctx = make_checker_context(c);
  3588. defer (destroy_checker_context(&ctx));
  3589. UntypedExprInfoMap untyped = {};
  3590. map_init(&untyped, heap_allocator());
  3591. defer (map_destroy(&untyped));
  3592. isize end = gb_min(data->offset + data->count, c->info.packages.entries.count);
  3593. for (isize i = data->offset; i < end; i++) {
  3594. AstPackage *pkg = c->info.packages.entries[i].value;
  3595. check_export_entites_in_pkg(&ctx, pkg, &untyped);
  3596. }
  3597. gb_semaphore_release(&c->info.collect_semaphore);
  3598. return 0;
  3599. }
  3600. void check_export_entites(Checker *c) {
  3601. check_with_workers(c, thread_proc_check_export_entites, c->info.packages.entries.count);
  3602. }
  3603. void check_import_entities(Checker *c) {
  3604. #define TIME_SECTION(str) do { debugf("[Section] %s\n", str); if (build_context.show_more_timings) timings_start_section(&global_timings, str_lit(str)); } while (0)
  3605. Array<ImportGraphNode *> dep_graph = generate_import_dependency_graph(c);
  3606. defer ({
  3607. for_array(i, dep_graph) {
  3608. import_graph_node_destroy(dep_graph[i], heap_allocator());
  3609. }
  3610. array_free(&dep_graph);
  3611. });
  3612. TIME_SECTION("check_import_entities - sort packages");
  3613. // NOTE(bill): Priority queue
  3614. auto pq = priority_queue_create(dep_graph, import_graph_node_cmp, import_graph_node_swap);
  3615. PtrSet<AstPackage *> emitted = {};
  3616. ptr_set_init(&emitted, heap_allocator());
  3617. defer (ptr_set_destroy(&emitted));
  3618. Array<ImportGraphNode *> package_order = {};
  3619. array_init(&package_order, heap_allocator(), 0, c->parser->packages.count);
  3620. defer (array_free(&package_order));
  3621. while (pq.queue.count > 0) {
  3622. ImportGraphNode *n = priority_queue_pop(&pq);
  3623. AstPackage *pkg = n->pkg;
  3624. if (n->dep_count > 0) {
  3625. PtrSet<AstPackage *> visited = {};
  3626. ptr_set_init(&visited, heap_allocator());
  3627. defer (ptr_set_destroy(&visited));
  3628. auto path = find_import_path(c, pkg, pkg, &visited);
  3629. defer (array_free(&path));
  3630. if (path.count > 1) {
  3631. ImportPathItem item = path[path.count-1];
  3632. String pkg_name = item.pkg->name;
  3633. error(item.decl, "Cyclic importation of '%.*s'", LIT(pkg_name));
  3634. for (isize i = 0; i < path.count; i++) {
  3635. error(item.decl, "'%.*s' refers to", LIT(pkg_name));
  3636. item = path[i];
  3637. pkg_name = item.pkg->name;
  3638. }
  3639. error(item.decl, "'%.*s'", LIT(pkg_name));
  3640. }
  3641. }
  3642. for_array(i, n->pred.entries) {
  3643. ImportGraphNode *p = n->pred.entries[i].ptr;
  3644. p->dep_count = gb_max(p->dep_count-1, 0);
  3645. priority_queue_fix(&pq, p->index);
  3646. }
  3647. if (pkg == nullptr) {
  3648. continue;
  3649. }
  3650. if (ptr_set_update(&emitted, pkg)) {
  3651. continue;
  3652. }
  3653. array_add(&package_order, n);
  3654. }
  3655. TIME_SECTION("check_import_entities - collect file decls");
  3656. CheckerContext ctx = make_checker_context(c);
  3657. UntypedExprInfoMap untyped = {};
  3658. map_init(&untyped, heap_allocator());
  3659. defer (map_destroy(&untyped));
  3660. isize min_pkg_index = 0;
  3661. for (isize pkg_index = 0; pkg_index < package_order.count; pkg_index++) {
  3662. ImportGraphNode *node = package_order[pkg_index];
  3663. AstPackage *pkg = node->pkg;
  3664. for_array(i, pkg->files) {
  3665. AstFile *f = pkg->files[i];
  3666. reset_checker_context(&ctx, f, &untyped);
  3667. ctx.collect_delayed_decls = true;
  3668. MPMCQueue<Ast *> *q = nullptr;
  3669. // Check import declarations first to simplify things
  3670. for (Ast *id = nullptr; mpmc_dequeue(&f->delayed_decls_queues[AstDelayQueue_Import], &id); /**/) {
  3671. check_add_import_decl(&ctx, id);
  3672. }
  3673. if (collect_file_decls(&ctx, f->decls)) {
  3674. check_export_entites_in_pkg(&ctx, pkg, &untyped);
  3675. pkg_index = min_pkg_index-1;
  3676. break;
  3677. }
  3678. add_untyped_expressions(ctx.info, &untyped);
  3679. }
  3680. if (pkg_index < 0) {
  3681. continue;
  3682. }
  3683. min_pkg_index = pkg_index;
  3684. }
  3685. TIME_SECTION("check_import_entities - check delayed entities");
  3686. for_array(i, package_order) {
  3687. ImportGraphNode *node = package_order[i];
  3688. GB_ASSERT(node->scope->flags&ScopeFlag_Pkg);
  3689. AstPackage *pkg = node->scope->pkg;
  3690. for_array(i, pkg->files) {
  3691. AstFile *f = pkg->files[i];
  3692. reset_checker_context(&ctx, f, &untyped);
  3693. auto *q = &f->delayed_decls_queues[AstDelayQueue_Import];
  3694. for (Ast *decl = nullptr; mpmc_dequeue(q, &decl); /**/) {
  3695. check_add_import_decl(&ctx, decl);
  3696. }
  3697. add_untyped_expressions(ctx.info, &untyped);
  3698. }
  3699. for_array(i, pkg->files) {
  3700. AstFile *f = pkg->files[i];
  3701. reset_checker_context(&ctx, f, &untyped);
  3702. auto *q = &f->delayed_decls_queues[AstDelayQueue_Expr];
  3703. for (Ast *expr = nullptr; mpmc_dequeue(q, &expr); /**/) {
  3704. Operand o = {};
  3705. check_expr(&ctx, &o, expr);
  3706. }
  3707. add_untyped_expressions(ctx.info, &untyped);
  3708. }
  3709. }
  3710. #undef TIME_SECTION
  3711. }
  3712. Array<Entity *> find_entity_path(Entity *start, Entity *end, Map<Entity *> *visited = nullptr);
  3713. bool find_entity_path_tuple(Type *tuple, Entity *end, Map<Entity *> *visited, Array<Entity *> *path_) {
  3714. GB_ASSERT(path_ != nullptr);
  3715. if (tuple == nullptr) {
  3716. return false;
  3717. }
  3718. GB_ASSERT(tuple->kind == Type_Tuple);
  3719. for_array(i, tuple->Tuple.variables) {
  3720. Entity *var = tuple->Tuple.variables[i];
  3721. DeclInfo *var_decl = var->decl_info;
  3722. if (var_decl == nullptr) {
  3723. continue;
  3724. }
  3725. for_array(i, var_decl->deps.entries) {
  3726. Entity *dep = var_decl->deps.entries[i].ptr;
  3727. if (dep == end) {
  3728. auto path = array_make<Entity *>(heap_allocator());
  3729. array_add(&path, dep);
  3730. *path_ = path;
  3731. return true;
  3732. }
  3733. auto next_path = find_entity_path(dep, end, visited);
  3734. if (next_path.count > 0) {
  3735. array_add(&next_path, dep);
  3736. *path_ = next_path;
  3737. return true;
  3738. }
  3739. }
  3740. }
  3741. return false;
  3742. }
  3743. Array<Entity *> find_entity_path(Entity *start, Entity *end, Map<Entity *> *visited) {
  3744. Map<Entity *> visited_ = {};
  3745. bool made_visited = false;
  3746. if (visited == nullptr) {
  3747. made_visited = true;
  3748. map_init(&visited_, heap_allocator());
  3749. visited = &visited_;
  3750. }
  3751. defer (if (made_visited) {
  3752. map_destroy(&visited_);
  3753. });
  3754. Array<Entity *> empty_path = {};
  3755. HashKey key = hash_pointer(start);
  3756. if (map_get(visited, key) != nullptr) {
  3757. return empty_path;
  3758. }
  3759. map_set(visited, key, start);
  3760. DeclInfo *decl = start->decl_info;
  3761. if (decl) {
  3762. if (start->kind == Entity_Procedure) {
  3763. Type *t = base_type(start->type);
  3764. GB_ASSERT(t->kind == Type_Proc);
  3765. Array<Entity *> path = {};
  3766. if (find_entity_path_tuple(t->Proc.params, end, visited, &path)) {
  3767. return path;
  3768. }
  3769. if (find_entity_path_tuple(t->Proc.results, end, visited, &path)) {
  3770. return path;
  3771. }
  3772. } else {
  3773. for_array(i, decl->deps.entries) {
  3774. Entity *dep = decl->deps.entries[i].ptr;
  3775. if (dep == end) {
  3776. auto path = array_make<Entity *>(heap_allocator());
  3777. array_add(&path, dep);
  3778. return path;
  3779. }
  3780. auto next_path = find_entity_path(dep, end, visited);
  3781. if (next_path.count > 0) {
  3782. array_add(&next_path, dep);
  3783. return next_path;
  3784. }
  3785. }
  3786. }
  3787. }
  3788. return empty_path;
  3789. }
  3790. void calculate_global_init_order(Checker *c) {
  3791. #define TIME_SECTION(str) do { debugf("[Section] %s\n", str); if (build_context.show_more_timings) timings_start_section(&global_timings, str_lit(str)); } while (0)
  3792. CheckerInfo *info = &c->info;
  3793. TIME_SECTION("calculate_global_init_order: generate entity dependency graph");
  3794. Array<EntityGraphNode *> dep_graph = generate_entity_dependency_graph(info, heap_allocator());
  3795. defer ({
  3796. for_array(i, dep_graph) {
  3797. entity_graph_node_destroy(dep_graph[i], heap_allocator());
  3798. }
  3799. array_free(&dep_graph);
  3800. });
  3801. TIME_SECTION("calculate_global_init_order: priority queue create");
  3802. // NOTE(bill): Priority queue
  3803. auto pq = priority_queue_create(dep_graph, entity_graph_node_cmp, entity_graph_node_swap);
  3804. PtrSet<DeclInfo *> emitted = {};
  3805. ptr_set_init(&emitted, heap_allocator());
  3806. defer (ptr_set_destroy(&emitted));
  3807. TIME_SECTION("calculate_global_init_order: queue sort");
  3808. while (pq.queue.count > 0) {
  3809. EntityGraphNode *n = priority_queue_pop(&pq);
  3810. Entity *e = n->entity;
  3811. if (n->dep_count > 0) {
  3812. auto path = find_entity_path(e, e);
  3813. defer (array_free(&path));
  3814. if (path.count > 0) {
  3815. Entity *e = path[0];
  3816. error(e->token, "Cyclic initialization of '%.*s'", LIT(e->token.string));
  3817. for (isize i = path.count-1; i >= 0; i--) {
  3818. error(e->token, "\t'%.*s' refers to", LIT(e->token.string));
  3819. e = path[i];
  3820. }
  3821. error(e->token, "\t'%.*s'", LIT(e->token.string));
  3822. }
  3823. }
  3824. for_array(i, n->pred.entries) {
  3825. EntityGraphNode *p = n->pred.entries[i].ptr;
  3826. p->dep_count -= 1;
  3827. p->dep_count = gb_max(p->dep_count, 0);
  3828. priority_queue_fix(&pq, p->index);
  3829. }
  3830. DeclInfo *d = decl_info_of_entity(e);
  3831. if (e->kind != Entity_Variable) {
  3832. continue;
  3833. }
  3834. // IMPORTANT NOTE(bill, 2019-08-29): Just add it regardless of the ordering
  3835. // because it does not need any initialization other than zero
  3836. // if (!decl_info_has_init(d)) {
  3837. // continue;
  3838. // }
  3839. if (ptr_set_update(&emitted, d)) {
  3840. continue;
  3841. }
  3842. array_add(&info->variable_init_order, d);
  3843. }
  3844. if (false) {
  3845. gb_printf("Variable Initialization Order:\n");
  3846. for_array(i, info->variable_init_order) {
  3847. DeclInfo *d = info->variable_init_order[i];
  3848. Entity *e = d->entity;
  3849. gb_printf("\t'%.*s' %llu\n", LIT(e->token.string), cast(unsigned long long)e->order_in_src);
  3850. }
  3851. gb_printf("\n");
  3852. }
  3853. #undef TIME_SECTION
  3854. }
  3855. void check_proc_info(Checker *c, ProcInfo *pi, UntypedExprInfoMap *untyped, ProcBodyQueue *procs_to_check_queue) {
  3856. if (pi == nullptr) {
  3857. return;
  3858. }
  3859. if (pi->type == nullptr) {
  3860. return;
  3861. }
  3862. if (pi->decl->proc_checked) {
  3863. return;
  3864. }
  3865. CheckerContext ctx = make_checker_context(c);
  3866. defer (destroy_checker_context(&ctx));
  3867. reset_checker_context(&ctx, pi->file, untyped);
  3868. ctx.decl = pi->decl;
  3869. ctx.procs_to_check_queue = procs_to_check_queue;
  3870. TypeProc *pt = &pi->type->Proc;
  3871. String name = pi->token.string;
  3872. if (pt->is_polymorphic && !pt->is_poly_specialized) {
  3873. Token token = pi->token;
  3874. if (pi->poly_def_node != nullptr) {
  3875. token = ast_token(pi->poly_def_node);
  3876. }
  3877. error(token, "Unspecialized polymorphic procedure '%.*s'", LIT(name));
  3878. return;
  3879. }
  3880. if (pt->is_polymorphic && pt->is_poly_specialized) {
  3881. Entity *e = pi->decl->entity;
  3882. if ((e->flags & EntityFlag_Used) == 0) {
  3883. // NOTE(bill, 2019-08-31): It was never used, don't check
  3884. return;
  3885. }
  3886. }
  3887. bool bounds_check = (pi->tags & ProcTag_bounds_check) != 0;
  3888. bool no_bounds_check = (pi->tags & ProcTag_no_bounds_check) != 0;
  3889. if (bounds_check) {
  3890. ctx.state_flags |= StateFlag_bounds_check;
  3891. ctx.state_flags &= ~StateFlag_no_bounds_check;
  3892. } else if (no_bounds_check) {
  3893. ctx.state_flags |= StateFlag_no_bounds_check;
  3894. ctx.state_flags &= ~StateFlag_bounds_check;
  3895. }
  3896. if (pi->body != nullptr && pi->decl->entity != nullptr) {
  3897. GB_ASSERT((pi->decl->entity->flags & EntityFlag_ProcBodyChecked) == 0);
  3898. }
  3899. check_proc_body(&ctx, pi->token, pi->decl, pi->type, pi->body);
  3900. if (pi->body != nullptr && pi->decl->entity != nullptr) {
  3901. pi->decl->entity->flags |= EntityFlag_ProcBodyChecked;
  3902. }
  3903. pi->decl->proc_checked = true;
  3904. add_untyped_expressions(&c->info, ctx.untyped);
  3905. }
  3906. GB_STATIC_ASSERT(sizeof(isize) == sizeof(void *));
  3907. void check_unchecked_bodies(Checker *c) {
  3908. // NOTE(2021-02-26, bill): Sanity checker
  3909. // This is a partial hack to make sure all procedure bodies have been checked
  3910. // even ones which should not exist, due to the multithreaded nature of the parser
  3911. // HACK TODO(2021-02-26, bill): Actually fix this race condition
  3912. UntypedExprInfoMap untyped = {};
  3913. map_init(&untyped, heap_allocator());
  3914. defer (map_destroy(&untyped));
  3915. for_array(i, c->info.minimum_dependency_set.entries) {
  3916. Entity *e = c->info.minimum_dependency_set.entries[i].ptr;
  3917. if (e == nullptr || e->kind != Entity_Procedure) {
  3918. continue;
  3919. }
  3920. if (e->Procedure.is_foreign) {
  3921. continue;
  3922. }
  3923. if ((e->flags & EntityFlag_ProcBodyChecked) == 0) {
  3924. GB_ASSERT(e->decl_info != nullptr);
  3925. ProcInfo pi = {};
  3926. pi.file = e->file;
  3927. pi.token = e->token;
  3928. pi.decl = e->decl_info;
  3929. pi.type = e->type;
  3930. Ast *pl = e->decl_info->proc_lit;
  3931. GB_ASSERT(pl != nullptr);
  3932. pi.body = pl->ProcLit.body;
  3933. pi.tags = pl->ProcLit.tags;
  3934. if (pi.body == nullptr) {
  3935. continue;
  3936. }
  3937. map_clear(&untyped);
  3938. check_proc_info(c, &pi, &untyped, nullptr);
  3939. }
  3940. }
  3941. }
  3942. void check_test_procedures(Checker *c) {
  3943. if (build_context.test_names.entries.count == 0) {
  3944. return;
  3945. }
  3946. AstPackage *pkg = c->info.init_package;
  3947. Scope *s = pkg->scope;
  3948. for_array(i, build_context.test_names.entries) {
  3949. String name = build_context.test_names.entries[i].value;
  3950. Entity *e = scope_lookup(s, name);
  3951. if (e == nullptr) {
  3952. Token tok = {};
  3953. if (pkg->files.count != 0) {
  3954. tok = pkg->files[0]->tokens[0];
  3955. }
  3956. error(tok, "Unable to find the test '%.*s' in 'package %.*s' ", LIT(name), LIT(pkg->name));
  3957. }
  3958. }
  3959. for (isize i = 0; i < c->info.testing_procedures.count; /**/) {
  3960. Entity *e = c->info.testing_procedures[i];
  3961. String name = e->token.string;
  3962. if (!string_set_exists(&build_context.test_names, name)) {
  3963. array_ordered_remove(&c->info.testing_procedures, i);
  3964. } else {
  3965. i += 1;
  3966. }
  3967. }
  3968. }
  3969. gb_global std::atomic<isize> total_bodies_checked;
  3970. bool consume_proc_info_queue(Checker *c, ProcInfo *pi, ProcBodyQueue *q, UntypedExprInfoMap *untyped) {
  3971. GB_ASSERT(pi->decl != nullptr);
  3972. if (pi->decl->parent && pi->decl->parent->entity) {
  3973. Entity *parent = pi->decl->parent->entity;
  3974. // NOTE(bill): Only check a nested procedure if its parent's body has been checked first
  3975. // This is prevent any possible race conditions in evaluation when multithreaded
  3976. // NOTE(bill): In single threaded mode, this should never happen
  3977. if (parent->kind == Entity_Procedure && (parent->flags & EntityFlag_ProcBodyChecked) == 0) {
  3978. mpmc_enqueue(q, pi);
  3979. return true;
  3980. }
  3981. }
  3982. if (untyped) {
  3983. map_clear(untyped);
  3984. }
  3985. check_proc_info(c, pi, untyped, q);
  3986. total_bodies_checked.fetch_add(1, std::memory_order_relaxed);
  3987. return false;
  3988. }
  3989. struct ThreadProcBodyData {
  3990. Checker *checker;
  3991. ProcBodyQueue *queue;
  3992. u32 thread_index;
  3993. u32 thread_count;
  3994. ThreadProcBodyData *all_data;
  3995. };
  3996. GB_THREAD_PROC(thread_proc_body) {
  3997. ThreadProcBodyData *data = cast(ThreadProcBodyData *)thread->user_data;
  3998. Checker *c = data->checker;
  3999. GB_ASSERT(c != nullptr);
  4000. ThreadProcBodyData *all_data = data->all_data;
  4001. ProcBodyQueue *this_queue = data->queue;
  4002. UntypedExprInfoMap untyped = {};
  4003. map_init(&untyped, heap_allocator());
  4004. defer (map_destroy(&untyped));
  4005. gbRandom r = {}; gb_random_init(&r);
  4006. for (ProcInfo *pi; mpmc_dequeue(this_queue, &pi); /**/) {
  4007. consume_proc_info_queue(c, pi, this_queue, &untyped);
  4008. }
  4009. gb_semaphore_release(&c->procs_to_check_semaphore);
  4010. return 0;
  4011. }
  4012. void check_procedure_bodies(Checker *c) {
  4013. GB_ASSERT(c != nullptr);
  4014. u32 thread_count = cast(u32)gb_max(build_context.thread_count, 1);
  4015. u32 worker_count = thread_count-1; // NOTE(bill): The main thread will also be used for work
  4016. if (!build_context.threaded_checker) {
  4017. worker_count = 0;
  4018. auto *q = &c->procs_to_check_queue;
  4019. ProcInfo *pi = nullptr;
  4020. while (mpmc_dequeue(q, &pi)) {
  4021. consume_proc_info_queue(c, pi, q, nullptr);
  4022. }
  4023. debugf("Total Procedure Bodies Checked: %td\n", total_bodies_checked.load(std::memory_order_relaxed));
  4024. return;
  4025. }
  4026. global_procedure_body_in_worker_queue = true;
  4027. isize original_queue_count = c->procs_to_check_queue.count.load(std::memory_order_relaxed);
  4028. isize load_count = (original_queue_count+thread_count-1)/thread_count;
  4029. ThreadProcBodyData *thread_data = gb_alloc_array(permanent_allocator(), ThreadProcBodyData, thread_count);
  4030. for (u32 i = 0; i < thread_count; i++) {
  4031. ThreadProcBodyData *data = thread_data + i;
  4032. data->checker = c;
  4033. data->queue = gb_alloc_item(permanent_allocator(), ProcBodyQueue);
  4034. data->thread_index = i;
  4035. data->thread_count = thread_count;
  4036. data->all_data = thread_data;
  4037. // NOTE(bill) 2x the amount assumes on average only 1 nested procedure
  4038. // TODO(bill): Determine a good heuristic
  4039. mpmc_init(data->queue, heap_allocator(), next_pow2_isize(load_count*2));
  4040. }
  4041. // Distibute the work load into multiple queues
  4042. for (isize j = 0; j < load_count; j++) {
  4043. for (isize i = 0; i < thread_count; i++) {
  4044. ProcBodyQueue *queue = thread_data[i].queue;
  4045. ProcInfo *pi = nullptr;
  4046. if (!mpmc_dequeue(&c->procs_to_check_queue, &pi)) {
  4047. break;
  4048. }
  4049. mpmc_enqueue(queue, pi);
  4050. }
  4051. }
  4052. isize total_queued = 0;
  4053. for (isize i = 0; i < thread_count; i++) {
  4054. ProcBodyQueue *queue = thread_data[i].queue;
  4055. total_queued += queue->count.load();
  4056. }
  4057. GB_ASSERT(total_queued == original_queue_count);
  4058. gb_semaphore_post(&c->procs_to_check_semaphore, cast(i32)thread_count);
  4059. gbThread *threads = gb_alloc_array(permanent_allocator(), gbThread, worker_count);
  4060. for (isize i = 0; i < worker_count; i++) {
  4061. gb_thread_init(threads+i);
  4062. }
  4063. for (isize i = 0; i < worker_count; i++) {
  4064. gb_thread_start(threads+i, thread_proc_body, thread_data+i);
  4065. }
  4066. gbThread dummy_main_thread = {};
  4067. dummy_main_thread.user_data = thread_data+worker_count;
  4068. thread_proc_body(&dummy_main_thread);
  4069. gb_semaphore_wait(&c->procs_to_check_semaphore);
  4070. for (isize i = 0; i < worker_count; i++) {
  4071. gb_thread_destroy(threads+i);
  4072. }
  4073. isize global_remaining = c->procs_to_check_queue.count.load(std::memory_order_relaxed);
  4074. GB_ASSERT(global_remaining == 0);
  4075. debugf("Total Procedure Bodies Checked: %td\n", total_bodies_checked.load(std::memory_order_relaxed));
  4076. global_procedure_body_in_worker_queue = false;
  4077. }
  4078. void add_untyped_expressions(CheckerInfo *cinfo, UntypedExprInfoMap *untyped) {
  4079. if (untyped == nullptr) {
  4080. return;
  4081. }
  4082. for_array(i, untyped->entries) {
  4083. Ast *expr = cast(Ast *)cast(uintptr)untyped->entries[i].key.key;
  4084. ExprInfo *info = untyped->entries[i].value;
  4085. if (expr != nullptr && info != nullptr) {
  4086. mpmc_enqueue(&cinfo->checker->global_untyped_queue, UntypedExprInfo{expr, info});
  4087. }
  4088. }
  4089. map_clear(untyped);
  4090. }
  4091. void check_deferred_procedures(Checker *c) {
  4092. for (Entity *src = nullptr; mpmc_dequeue(&c->procs_with_deferred_to_check, &src); /**/) {
  4093. GB_ASSERT(src->kind == Entity_Procedure);
  4094. DeferredProcedureKind dst_kind = src->Procedure.deferred_procedure.kind;
  4095. Entity *dst = src->Procedure.deferred_procedure.entity;
  4096. GB_ASSERT(dst != nullptr);
  4097. GB_ASSERT(dst->kind == Entity_Procedure);
  4098. if (is_type_polymorphic(src->type) || is_type_polymorphic(dst->type)) {
  4099. switch (dst_kind) {
  4100. case DeferredProcedure_none:
  4101. error(src->token, "'deferred_none' cannot be used with a polymorphic procedure");
  4102. break;
  4103. case DeferredProcedure_in:
  4104. error(src->token, "'deferred_in' cannot be used with a polymorphic procedure");
  4105. break;
  4106. case DeferredProcedure_out:
  4107. error(src->token, "'deferred_out' cannot be used with a polymorphic procedure");
  4108. break;
  4109. case DeferredProcedure_in_out:
  4110. error(src->token, "'deferred_in_out' cannot be used with a polymorphic procedure");
  4111. break;
  4112. }
  4113. continue;
  4114. }
  4115. GB_ASSERT(is_type_proc(src->type));
  4116. GB_ASSERT(is_type_proc(dst->type));
  4117. Type *src_params = base_type(src->type)->Proc.params;
  4118. Type *src_results = base_type(src->type)->Proc.results;
  4119. Type *dst_params = base_type(dst->type)->Proc.params;
  4120. if (dst_kind == DeferredProcedure_none) {
  4121. if (dst_params == nullptr) {
  4122. // Okay
  4123. continue;
  4124. }
  4125. error(src->token, "Deferred procedure '%.*s' must have no input parameters", LIT(dst->token.string));
  4126. } else if (dst_kind == DeferredProcedure_in) {
  4127. if (src_params == nullptr && dst_params == nullptr) {
  4128. // Okay
  4129. continue;
  4130. }
  4131. if ((src_params == nullptr && dst_params != nullptr) ||
  4132. (src_params != nullptr && dst_params == nullptr)) {
  4133. error(src->token, "Deferred procedure '%.*s' parameters do not match the inputs of initial procedure '%.*s'", LIT(src->token.string), LIT(dst->token.string));
  4134. continue;
  4135. }
  4136. GB_ASSERT(src_params->kind == Type_Tuple);
  4137. GB_ASSERT(dst_params->kind == Type_Tuple);
  4138. auto const &sv = src_params->Tuple.variables;
  4139. auto const &dv = dst_params->Tuple.variables;
  4140. if (are_types_identical(src_params, dst_params)) {
  4141. // Okay!
  4142. } else {
  4143. gbString s = type_to_string(src_params);
  4144. gbString d = type_to_string(dst_params);
  4145. error(src->token, "Deferred procedure '%.*s' parameters do not match the inputs of initial procedure '%.*s':\n\t(%s) =/= (%s)",
  4146. LIT(src->token.string), LIT(dst->token.string),
  4147. s, d
  4148. );
  4149. gb_string_free(d);
  4150. gb_string_free(s);
  4151. continue;
  4152. }
  4153. } else if (dst_kind == DeferredProcedure_out) {
  4154. if (src_results == nullptr && dst_params == nullptr) {
  4155. // Okay
  4156. continue;
  4157. }
  4158. if ((src_results == nullptr && dst_params != nullptr) ||
  4159. (src_results != nullptr && dst_params == nullptr)) {
  4160. error(src->token, "Deferred procedure '%.*s' parameters do not match the results of initial procedure '%.*s'", LIT(src->token.string), LIT(dst->token.string));
  4161. continue;
  4162. }
  4163. GB_ASSERT(src_results->kind == Type_Tuple);
  4164. GB_ASSERT(dst_params->kind == Type_Tuple);
  4165. auto const &sv = src_results->Tuple.variables;
  4166. auto const &dv = dst_params->Tuple.variables;
  4167. if (are_types_identical(src_results, dst_params)) {
  4168. // Okay!
  4169. } else {
  4170. gbString s = type_to_string(src_results);
  4171. gbString d = type_to_string(dst_params);
  4172. error(src->token, "Deferred procedure '%.*s' parameters do not match the results of initial procedure '%.*s':\n\t(%s) =/= (%s)",
  4173. LIT(src->token.string), LIT(dst->token.string),
  4174. s, d
  4175. );
  4176. gb_string_free(d);
  4177. gb_string_free(s);
  4178. continue;
  4179. }
  4180. } else if (dst_kind == DeferredProcedure_in_out) {
  4181. if (src_params == nullptr && src_results == nullptr && dst_params == nullptr) {
  4182. // Okay
  4183. continue;
  4184. }
  4185. GB_ASSERT(dst_params->kind == Type_Tuple);
  4186. Type *tsrc = alloc_type_tuple();
  4187. auto &sv = tsrc->Tuple.variables;
  4188. auto const &dv = dst_params->Tuple.variables;
  4189. isize len = 0;
  4190. if (src_params != nullptr) {
  4191. GB_ASSERT(src_params->kind == Type_Tuple);
  4192. len += src_params->Tuple.variables.count;
  4193. }
  4194. if (src_results != nullptr) {
  4195. GB_ASSERT(src_results->kind == Type_Tuple);
  4196. len += src_results->Tuple.variables.count;
  4197. }
  4198. array_init(&sv, heap_allocator(), 0, len);
  4199. if (src_params != nullptr) {
  4200. for_array(i, src_params->Tuple.variables) {
  4201. array_add(&sv, src_params->Tuple.variables[i]);
  4202. }
  4203. }
  4204. if (src_results != nullptr) {
  4205. for_array(i, src_results->Tuple.variables) {
  4206. array_add(&sv, src_results->Tuple.variables[i]);
  4207. }
  4208. }
  4209. if (are_types_identical(tsrc, dst_params)) {
  4210. // Okay!
  4211. } else {
  4212. gbString s = type_to_string(tsrc);
  4213. gbString d = type_to_string(dst_params);
  4214. error(src->token, "Deferred procedure '%.*s' parameters do not match the results of initial procedure '%.*s':\n\t(%s) =/= (%s)",
  4215. LIT(src->token.string), LIT(dst->token.string),
  4216. s, d
  4217. );
  4218. gb_string_free(d);
  4219. gb_string_free(s);
  4220. continue;
  4221. }
  4222. }
  4223. }
  4224. }
  4225. void check_unique_package_names(Checker *c) {
  4226. StringMap<AstPackage *> pkgs = {}; // Key: package name
  4227. string_map_init(&pkgs, heap_allocator(), 2*c->info.packages.entries.count);
  4228. defer (string_map_destroy(&pkgs));
  4229. for_array(i, c->info.packages.entries) {
  4230. AstPackage *pkg = c->info.packages.entries[i].value;
  4231. if (pkg->files.count == 0) {
  4232. continue; // Sanity check
  4233. }
  4234. String name = pkg->name;
  4235. auto key = string_hash_string(name);
  4236. auto *found = string_map_get(&pkgs, key);
  4237. if (found == nullptr) {
  4238. string_map_set(&pkgs, key, pkg);
  4239. continue;
  4240. }
  4241. error(pkg->files[0]->pkg_decl, "Duplicate declaration of 'package %.*s'", LIT(name));
  4242. error_line("\tA package name must be unique\n"
  4243. "\tThere is no relation between a package name and the directory that contains it, so they can be completely different\n"
  4244. "\tA package name is required for link name prefixing to have a consistent ABI\n");
  4245. error((*found)->files[0]->pkg_decl, "found at previous location");
  4246. }
  4247. }
  4248. void check_add_entities_from_queues(Checker *c) {
  4249. {
  4250. isize cap = c->info.entities.count + c->info.entity_queue.count.load(std::memory_order_relaxed);
  4251. array_reserve(&c->info.entities, cap);
  4252. for (Entity *e; mpmc_dequeue(&c->info.entity_queue, &e); /**/) {
  4253. array_add(&c->info.entities, e);
  4254. }
  4255. }
  4256. {
  4257. isize cap = c->info.definitions.count + c->info.definition_queue.count.load(std::memory_order_relaxed);
  4258. array_reserve(&c->info.definitions, cap);
  4259. for (Entity *e; mpmc_dequeue(&c->info.definition_queue, &e); /**/) {
  4260. array_add(&c->info.definitions, e);
  4261. }
  4262. }
  4263. }
  4264. void check_parsed_files(Checker *c) {
  4265. #define TIME_SECTION(str) do { debugf("[Section] %s\n", str); if (build_context.show_more_timings) timings_start_section(&global_timings, str_lit(str)); } while (0)
  4266. TIME_SECTION("map full filepaths to scope");
  4267. add_type_info_type(&c->builtin_ctx, t_invalid);
  4268. // Map full filepaths to Scopes
  4269. for_array(i, c->parser->packages) {
  4270. AstPackage *p = c->parser->packages[i];
  4271. Scope *scope = create_scope_from_package(&c->builtin_ctx, p);
  4272. p->decl_info = make_decl_info(scope, c->builtin_ctx.decl);
  4273. string_map_set(&c->info.packages, p->fullpath, p);
  4274. if (scope->flags&ScopeFlag_Init) {
  4275. c->info.init_package = p;
  4276. c->info.init_scope = scope;
  4277. }
  4278. if (p->kind == Package_Runtime) {
  4279. GB_ASSERT(c->info.runtime_package == nullptr);
  4280. c->info.runtime_package = p;
  4281. }
  4282. }
  4283. TIME_SECTION("create file scopes");
  4284. check_create_file_scopes(c);
  4285. TIME_SECTION("collect entities");
  4286. check_collect_entities_all(c);
  4287. TIME_SECTION("export entities - pre");
  4288. check_export_entites(c);
  4289. // NOTE: Timing Section handled internally
  4290. check_import_entities(c);
  4291. TIME_SECTION("export entities - post");
  4292. check_export_entites(c);
  4293. TIME_SECTION("add entities from packages");
  4294. check_add_entities_from_queues(c);
  4295. TIME_SECTION("check all global entities");
  4296. check_all_global_entities(c);
  4297. TIME_SECTION("init preload");
  4298. init_preload(c);
  4299. TIME_SECTION("add global untyped expression to queue");
  4300. add_untyped_expressions(&c->info, &c->info.global_untyped);
  4301. CheckerContext prev_context = c->builtin_ctx;
  4302. defer (c->builtin_ctx = prev_context);
  4303. c->builtin_ctx.decl = make_decl_info(nullptr, nullptr);
  4304. TIME_SECTION("check procedure bodies");
  4305. check_procedure_bodies(c);
  4306. TIME_SECTION("add entities from procedure bodiess");
  4307. check_add_entities_from_queues(c);
  4308. TIME_SECTION("check scope usage");
  4309. for_array(i, c->info.files.entries) {
  4310. AstFile *f = c->info.files.entries[i].value;
  4311. check_scope_usage(c, f->scope);
  4312. }
  4313. TIME_SECTION("generate minimum dependency set");
  4314. generate_minimum_dependency_set(c, c->info.entry_point);
  4315. TIME_SECTION("check test procedures");
  4316. check_test_procedures(c);
  4317. TIME_SECTION("calculate global init order");
  4318. // Calculate initialization order of global variables
  4319. calculate_global_init_order(c);
  4320. TIME_SECTION("check bodies have all been checked");
  4321. check_unchecked_bodies(c);
  4322. TIME_SECTION("add untyped expression values");
  4323. // Add untyped expression values
  4324. for (UntypedExprInfo u = {}; mpmc_dequeue(&c->global_untyped_queue, &u); /**/) {
  4325. GB_ASSERT(u.expr != nullptr && u.info != nullptr);
  4326. if (is_type_typed(u.info->type)) {
  4327. compiler_error("%s (type %s) is typed!", expr_to_string(u.expr), type_to_string(u.info->type));
  4328. }
  4329. add_type_and_value(&c->info, u.expr, u.info->mode, u.info->type, u.info->value);
  4330. }
  4331. TIME_SECTION("add type information");
  4332. // Add "Basic" type information
  4333. for (isize i = 0; i < Basic_COUNT; i++) {
  4334. Type *t = &basic_types[i];
  4335. if (t->Basic.size > 0 &&
  4336. (t->Basic.flags & BasicFlag_LLVM) == 0) {
  4337. add_type_info_type(&c->builtin_ctx, t);
  4338. }
  4339. }
  4340. TIME_SECTION("check for type cycles and inline cycles");
  4341. // NOTE(bill): Check for illegal cyclic type declarations
  4342. for_array(i, c->info.definitions) {
  4343. Entity *e = c->info.definitions[i];
  4344. if (e->kind == Entity_TypeName && e->type != nullptr) {
  4345. (void)type_align_of(e->type);
  4346. } else if (e->kind == Entity_Procedure) {
  4347. DeclInfo *decl = e->decl_info;
  4348. ast_node(pl, ProcLit, decl->proc_lit);
  4349. if (pl->inlining == ProcInlining_inline) {
  4350. for_array(j, decl->deps.entries) {
  4351. Entity *dep = decl->deps.entries[j].ptr;
  4352. if (dep == e) {
  4353. error(e->token, "Cannot inline recursive procedure '%.*s'", LIT(e->token.string));
  4354. break;
  4355. }
  4356. }
  4357. }
  4358. }
  4359. }
  4360. TIME_SECTION("add type info for type definitions");
  4361. for_array(i, c->info.definitions) {
  4362. Entity *e = c->info.definitions[i];
  4363. if (e->kind == Entity_TypeName && e->type != nullptr) {
  4364. i64 align = type_align_of(e->type);
  4365. if (align > 0 && ptr_set_exists(&c->info.minimum_dependency_set, e)) {
  4366. add_type_info_type(&c->builtin_ctx, e->type);
  4367. }
  4368. }
  4369. }
  4370. TIME_SECTION("check deferred procedures");
  4371. check_deferred_procedures(c);
  4372. TIME_SECTION("check entry point");
  4373. if (build_context.build_mode == BuildMode_Executable && !build_context.no_entry_point && build_context.command_kind != Command_test) {
  4374. Scope *s = c->info.init_scope;
  4375. GB_ASSERT(s != nullptr);
  4376. GB_ASSERT(s->flags&ScopeFlag_Init);
  4377. Entity *e = scope_lookup_current(s, str_lit("main"));
  4378. if (e == nullptr) {
  4379. Token token = {};
  4380. token.pos.file_id = 0;
  4381. token.pos.line = 1;
  4382. token.pos.column = 1;
  4383. if (s->pkg->files.count > 0) {
  4384. AstFile *f = s->pkg->files[0];
  4385. if (f->tokens.count > 0) {
  4386. token = f->tokens[0];
  4387. }
  4388. }
  4389. error(token, "Undefined entry point procedure 'main'");
  4390. }
  4391. }
  4392. TIME_SECTION("check unique package names");
  4393. check_unique_package_names(c);
  4394. TIME_SECTION("type check finish");
  4395. #undef TIME_SECTION
  4396. }