12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280 |
- enum CallArgumentError {
- CallArgumentError_None,
- CallArgumentError_NoneProcedureType,
- CallArgumentError_WrongTypes,
- CallArgumentError_NonVariadicExpand,
- CallArgumentError_VariadicTuple,
- CallArgumentError_MultipleVariadicExpand,
- CallArgumentError_AmbiguousPolymorphicVariadic,
- CallArgumentError_ArgumentCount,
- CallArgumentError_TooFewArguments,
- CallArgumentError_TooManyArguments,
- CallArgumentError_InvalidFieldValue,
- CallArgumentError_ParameterNotFound,
- CallArgumentError_ParameterMissing,
- CallArgumentError_DuplicateParameter,
- CallArgumentError_NoneConstantParameter,
- CallArgumentError_OutOfOrderParameters,
- CallArgumentError_MAX,
- };
- gb_global char const *CallArgumentError_strings[CallArgumentError_MAX] = {
- "None",
- "NoneProcedureType",
- "WrongTypes",
- "NonVariadicExpand",
- "VariadicTuple",
- "MultipleVariadicExpand",
- "AmbiguousPolymorphicVariadic",
- "ArgumentCount",
- "TooFewArguments",
- "TooManyArguments",
- "InvalidFieldValue",
- "ParameterNotFound",
- "ParameterMissing",
- "DuplicateParameter",
- "NoneConstantParameter",
- "OutOfOrderParameters",
- };
- enum struct CallArgumentErrorMode {
- NoErrors,
- ShowErrors,
- };
- struct CallArgumentData {
- Entity *gen_entity;
- i64 score;
- Type * result_type;
- };
- struct PolyProcData {
- Entity * gen_entity;
- ProcInfo *proc_info;
- };
- struct ValidIndexAndScore {
- isize index;
- i64 score;
- };
- gb_internal int valid_index_and_score_cmp(void const *a, void const *b) {
- i64 si = (cast(ValidIndexAndScore const *)a)->score;
- i64 sj = (cast(ValidIndexAndScore const *)b)->score;
- return sj < si ? -1 : sj > si;
- }
- gb_internal void check_expr (CheckerContext *c, Operand *operand, Ast *expression);
- gb_internal void check_multi_expr (CheckerContext *c, Operand *operand, Ast *expression);
- gb_internal void check_multi_expr_or_type (CheckerContext *c, Operand *operand, Ast *expression);
- gb_internal void check_multi_expr_with_type_hint(CheckerContext *c, Operand *o, Ast *e, Type *type_hint);
- gb_internal void check_expr_or_type (CheckerContext *c, Operand *operand, Ast *expression, Type *type_hint);
- gb_internal ExprKind check_expr_base (CheckerContext *c, Operand *operand, Ast *expression, Type *type_hint);
- gb_internal void check_expr_with_type_hint (CheckerContext *c, Operand *o, Ast *e, Type *t);
- gb_internal Type * check_type (CheckerContext *c, Ast *expression);
- gb_internal Type * check_type_expr (CheckerContext *c, Ast *expression, Type *named_type);
- gb_internal Type * make_optional_ok_type (Type *value, bool typed=true);
- gb_internal Entity * check_selector (CheckerContext *c, Operand *operand, Ast *node, Type *type_hint);
- gb_internal Entity * check_ident (CheckerContext *c, Operand *o, Ast *n, Type *named_type, Type *type_hint, bool allow_import_name);
- gb_internal void check_not_tuple (CheckerContext *c, Operand *operand);
- gb_internal void convert_to_typed (CheckerContext *c, Operand *operand, Type *target_type);
- gb_internal gbString expr_to_string (Ast *expression);
- gb_internal gbString expr_to_string (Ast *expression, gbAllocator allocator);
- gb_internal void update_untyped_expr_type (CheckerContext *c, Ast *e, Type *type, bool final);
- gb_internal bool check_is_terminating (Ast *node, String const &label);
- gb_internal bool check_has_break (Ast *stmt, String const &label, bool implicit);
- gb_internal void check_stmt (CheckerContext *c, Ast *node, u32 flags);
- gb_internal void check_stmt_list (CheckerContext *c, Slice<Ast *> const &stmts, u32 flags);
- gb_internal void check_init_constant (CheckerContext *c, Entity *e, Operand *operand);
- gb_internal bool check_representable_as_constant(CheckerContext *c, ExactValue in_value, Type *type, ExactValue *out_value);
- gb_internal bool check_procedure_type (CheckerContext *c, Type *type, Ast *proc_type_node, Array<Operand> const *operands = nullptr);
- gb_internal void check_struct_type (CheckerContext *c, Type *struct_type, Ast *node, Array<Operand> *poly_operands,
- Type *named_type = nullptr, Type *original_type_for_poly = nullptr);
- gb_internal void check_union_type (CheckerContext *c, Type *union_type, Ast *node, Array<Operand> *poly_operands,
- Type *named_type = nullptr, Type *original_type_for_poly = nullptr);
- gb_internal Type * check_init_variable (CheckerContext *c, Entity *e, Operand *operand, String context_name);
- gb_internal void check_assignment_error_suggestion(CheckerContext *c, Operand *o, Type *type, i64 max_bit_size=0);
- gb_internal void add_map_key_type_dependencies(CheckerContext *ctx, Type *key);
- gb_internal Type *make_soa_struct_fixed(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem, i64 count, Type *generic_type);
- gb_internal Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem);
- gb_internal Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem);
- gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint);
- gb_internal void check_promote_optional_ok(CheckerContext *c, Operand *x, Type **val_type_, Type **ok_type_, bool change_operand=true);
- gb_internal void check_or_else_right_type(CheckerContext *c, Ast *expr, String const &name, Type *right_type);
- gb_internal void check_or_else_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_);
- gb_internal void check_or_else_expr_no_value_error(CheckerContext *c, String const &name, Operand const &x, Type *type_hint);
- gb_internal void check_or_return_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_);
- gb_internal bool is_diverging_expr(Ast *expr);
- gb_internal isize get_procedure_param_count_excluding_defaults(Type *pt, isize *param_count_);
- gb_internal bool is_expr_inferred_fixed_array(Ast *type_expr);
- gb_internal Entity *find_polymorphic_record_entity(GenTypesData *found_gen_types, isize param_count, Array<Operand> const &ordered_operands);
- gb_internal bool complete_soa_type(Checker *checker, Type *t, bool wait_to_finish);
- gb_internal bool check_is_castable_to(CheckerContext *c, Operand *operand, Type *y);
- gb_internal bool is_exact_value_zero(ExactValue const &v);
- enum LoadDirectiveResult {
- LoadDirective_Success = 0,
- LoadDirective_Error = 1,
- LoadDirective_NotFound = 2,
- };
- gb_internal bool is_load_directive_call(Ast *call) {
- call = unparen_expr(call);
- if (call->kind != Ast_CallExpr) {
- return false;
- }
- ast_node(ce, CallExpr, call);
- if (ce->proc->kind != Ast_BasicDirective) {
- return false;
- }
- ast_node(bd, BasicDirective, ce->proc);
- String name = bd->name.string;
- return name == "load";
- }
- gb_internal LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found);
- gb_internal void check_did_you_mean_print(DidYouMeanAnswers *d, char const *prefix = "") {
- auto results = did_you_mean_results(d);
- if (results.count != 0) {
- error_line("\tSuggestion: Did you mean?\n");
- for (auto const &result : results) {
- String const &target = result.target;
- error_line("\t\t%s%.*s\n", prefix, LIT(target));
- // error_line("\t\t%.*s %td\n", LIT(target), results[i].distance);
- }
- }
- }
- gb_internal void populate_check_did_you_mean_objc_entity(StringSet *set, Entity *e, bool is_type) {
- if (e->kind != Entity_TypeName) {
- return;
- }
- if (e->TypeName.objc_metadata == nullptr) {
- return;
- }
- TypeNameObjCMetadata *objc_metadata = e->TypeName.objc_metadata;
- Type *t = base_type(e->type);
- GB_ASSERT(t->kind == Type_Struct);
- if (is_type) {
- for (auto const &entry : objc_metadata->type_entries) {
- string_set_add(set, entry.name);
- }
- } else {
- for (auto const &entry : objc_metadata->value_entries) {
- string_set_add(set, entry.name);
- }
- }
- for (Entity *f : t->Struct.fields) {
- if (f->flags & EntityFlag_Using && f->type != nullptr) {
- if (f->type->kind == Type_Named && f->type->Named.type_name) {
- populate_check_did_you_mean_objc_entity(set, f->type->Named.type_name, is_type);
- }
- }
- }
- }
- gb_internal void check_did_you_mean_objc_entity(String const &name, Entity *e, bool is_type, char const *prefix = "") {
- if (build_context.terse_errors) { return; }
- ERROR_BLOCK();
- GB_ASSERT(e->kind == Entity_TypeName);
- GB_ASSERT(e->TypeName.objc_metadata != nullptr);
- auto *objc_metadata = e->TypeName.objc_metadata;
- MUTEX_GUARD(objc_metadata->mutex);
- StringSet set = {};
- defer (string_set_destroy(&set));
- populate_check_did_you_mean_objc_entity(&set, e, is_type);
- DidYouMeanAnswers d = did_you_mean_make(heap_allocator(), set.entries.count, name);
- defer (did_you_mean_destroy(&d));
- for (String const &target : set) {
- did_you_mean_append(&d, target);
- }
- check_did_you_mean_print(&d, prefix);
- }
- gb_internal void check_did_you_mean_type(String const &name, Array<Entity *> const &fields, char const *prefix = "") {
- if (build_context.terse_errors) { return; }
- ERROR_BLOCK();
- DidYouMeanAnswers d = did_you_mean_make(heap_allocator(), fields.count, name);
- defer (did_you_mean_destroy(&d));
- for (Entity *e : fields) {
- did_you_mean_append(&d, e->token.string);
- }
- check_did_you_mean_print(&d, prefix);
- }
- gb_internal void check_did_you_mean_type(String const &name, Slice<Entity *> const &fields, char const *prefix = "") {
- if (build_context.terse_errors) { return; }
- ERROR_BLOCK();
- DidYouMeanAnswers d = did_you_mean_make(heap_allocator(), fields.count, name);
- defer (did_you_mean_destroy(&d));
- for (Entity *e : fields) {
- did_you_mean_append(&d, e->token.string);
- }
- check_did_you_mean_print(&d, prefix);
- }
- gb_internal void check_did_you_mean_scope(String const &name, Scope *scope, char const *prefix = "") {
- if (build_context.terse_errors) { return; }
- ERROR_BLOCK();
- DidYouMeanAnswers d = did_you_mean_make(heap_allocator(), scope->elements.count, name);
- defer (did_you_mean_destroy(&d));
- rw_mutex_shared_lock(&scope->mutex);
- for (auto const &entry : scope->elements) {
- Entity *e = entry.value;
- did_you_mean_append(&d, e->token.string);
- }
- rw_mutex_shared_unlock(&scope->mutex);
- check_did_you_mean_print(&d, prefix);
- }
- gb_internal Entity *entity_from_expr(Ast *expr) {
- expr = unparen_expr(expr);
- if (expr == nullptr) {
- return nullptr;
- }
- switch (expr->kind) {
- case Ast_Ident:
- return expr->Ident.entity;
- case Ast_SelectorExpr:
- return entity_from_expr(expr->SelectorExpr.selector);
- }
- return nullptr;
- }
- gb_internal void error_operand_not_expression(Operand *o) {
- if (o->mode == Addressing_Type) {
- gbString err = expr_to_string(o->expr);
- error(o->expr, "'%s' is not an expression but a type", err);
- gb_string_free(err);
- o->mode = Addressing_Invalid;
- }
- }
- gb_internal void error_operand_no_value(Operand *o) {
- if (o->mode == Addressing_NoValue) {
- Ast *x = unparen_expr(o->expr);
- if (x != nullptr && x->kind == Ast_CallExpr) {
- Ast *p = unparen_expr(x->CallExpr.proc);
- if (p->kind == Ast_BasicDirective) {
- String tag = p->BasicDirective.name.string;
- if (tag == "panic" ||
- tag == "assert") {
- return;
- }
- }
- }
- gbString err = expr_to_string(o->expr);
- if (x != nullptr && x->kind == Ast_CallExpr) {
- error(o->expr, "'%s' call does not return a value and cannot be used as a value", err);
- } else {
- error(o->expr, "'%s' used as a value", err);
- }
- gb_string_free(err);
- o->mode = Addressing_Invalid;
- }
- }
- gb_internal void add_map_get_dependencies(CheckerContext *c) {
- if (build_context.dynamic_map_calls) {
- add_package_dependency(c, "runtime", "__dynamic_map_get");
- } else {
- add_package_dependency(c, "runtime", "map_desired_position");
- add_package_dependency(c, "runtime", "map_probe_distance");
- }
- }
- gb_internal void add_map_set_dependencies(CheckerContext *c) {
- init_core_source_code_location(c->checker);
- if (t_map_set_proc == nullptr) {
- Type *map_set_args[5] = {/*map*/t_rawptr, /*hash*/t_uintptr, /*key*/t_rawptr, /*value*/t_rawptr, /*#caller_location*/t_source_code_location};
- t_map_set_proc = alloc_type_proc_from_types(map_set_args, gb_count_of(map_set_args), t_rawptr, false, ProcCC_Odin);
- }
- if (build_context.dynamic_map_calls) {
- add_package_dependency(c, "runtime", "__dynamic_map_set");
- } else {
- add_package_dependency(c, "runtime", "__dynamic_map_check_grow");
- add_package_dependency(c, "runtime", "map_insert_hash_dynamic");
- }
- }
- gb_internal void add_map_reserve_dependencies(CheckerContext *c) {
- init_core_source_code_location(c->checker);
- add_package_dependency(c, "runtime", "__dynamic_map_reserve");
- }
- gb_internal void check_scope_decls(CheckerContext *c, Slice<Ast *> const &nodes, isize reserve_size) {
- Scope *s = c->scope;
- check_collect_entities(c, nodes);
- for (auto const &entry : s->elements) {
- Entity *e = entry.value;\
- switch (e->kind) {
- case Entity_Constant:
- case Entity_TypeName:
- case Entity_Procedure:
- break;
- default:
- continue;
- }
- DeclInfo *d = decl_info_of_entity(e);
- if (d != nullptr) {
- check_entity_decl(c, e, d, nullptr);
- }
- }
- }
- gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, Entity *base_entity, Type *type,
- Array<Operand> const *param_operands, Ast *poly_def_node, PolyProcData *poly_proc_data) {
- ///////////////////////////////////////////////////////////////////////////////
- // //
- // TODO CLEANUP(bill): This procedure is very messy and hacky. Clean this!!! //
- // //
- ///////////////////////////////////////////////////////////////////////////////
- CheckerInfo *info = old_c->info;
- if (base_entity == nullptr) {
- return false;
- }
- if (!is_type_proc(base_entity->type)) {
- return false;
- }
- if (base_entity->flags & EntityFlag_Disabled) {
- return false;
- }
- String name = base_entity->token.string;
- Type *src = base_type(base_entity->type);
- Type *dst = nullptr;
- if (type != nullptr) {
- dst = base_type(type);
- }
- if (param_operands == nullptr) {
- GB_ASSERT(dst != nullptr);
- }
- if (param_operands != nullptr) {
- GB_ASSERT(dst == nullptr);
- }
- if (!src->Proc.is_polymorphic || src->Proc.is_poly_specialized) {
- return false;
- }
- if (dst != nullptr) {
- if (dst->Proc.is_polymorphic) {
- return false;
- }
- if (dst->Proc.param_count != src->Proc.param_count ||
- dst->Proc.result_count != src->Proc.result_count) {
- return false;
- }
- }
- DeclInfo *old_decl = decl_info_of_entity(base_entity);
- if (old_decl == nullptr) {
- return false;
- }
- gbAllocator a = heap_allocator();
- Array<Operand> operands = {};
- if (param_operands) {
- operands = *param_operands;
- } else {
- operands = array_make<Operand>(a, 0, dst->Proc.param_count);
- for (isize i = 0; i < dst->Proc.param_count; i++) {
- Entity *param = dst->Proc.params->Tuple.variables[i];
- Operand o = {Addressing_Value};
- o.type = param->type;
- array_add(&operands, o);
- }
- }
- defer (if (param_operands == nullptr) {
- array_free(&operands);
- });
- CheckerContext nctx = *old_c;
- Scope *scope = create_scope(info, base_entity->scope);
- scope->flags |= ScopeFlag_Proc;
- nctx.scope = scope;
- nctx.allow_polymorphic_types = true;
- if (nctx.polymorphic_scope == nullptr) {
- nctx.polymorphic_scope = scope;
- }
- auto *pt = &src->Proc;
- // NOTE(bill): This is slightly memory leaking if the type already exists
- // Maybe it's better to check with the previous types first?
- Type *final_proc_type = alloc_type_proc(scope, nullptr, 0, nullptr, 0, false, pt->calling_convention);
- bool success = check_procedure_type(&nctx, final_proc_type, pt->node, &operands);
- if (!success) {
- return false;
- }
- GenProcsData *gen_procs = nullptr;
- GB_ASSERT(base_entity->identifier.load()->kind == Ast_Ident);
- GB_ASSERT(base_entity->kind == Entity_Procedure);
- mutex_lock(&base_entity->Procedure.gen_procs_mutex); // @entity-mutex
- gen_procs = base_entity->Procedure.gen_procs;
- if (gen_procs) {
- rw_mutex_shared_lock(&gen_procs->mutex); // @local-mutex
- mutex_unlock(&base_entity->Procedure.gen_procs_mutex); // @entity-mutex
- for (Entity *other : gen_procs->procs) {
- Type *pt = base_type(other->type);
- if (are_types_identical(pt, final_proc_type)) {
- rw_mutex_shared_unlock(&gen_procs->mutex); // @local-mutex
- if (poly_proc_data) {
- poly_proc_data->gen_entity = other;
- }
- return true;
- }
- }
- rw_mutex_shared_unlock(&gen_procs->mutex); // @local-mutex
- } else {
- gen_procs = gb_alloc_item(permanent_allocator(), GenProcsData);
- gen_procs->procs.allocator = heap_allocator();
- base_entity->Procedure.gen_procs = gen_procs;
- mutex_unlock(&base_entity->Procedure.gen_procs_mutex); // @entity-mutex
- }
- {
- // LEAK NOTE(bill): This is technically a memory leak as it has to generate the type twice
- bool prev_no_polymorphic_errors = nctx.no_polymorphic_errors;
- defer (nctx.no_polymorphic_errors = prev_no_polymorphic_errors);
- nctx.no_polymorphic_errors = false;
- // NOTE(bill): Reset scope from the failed procedure type
- scope->head_child.store(nullptr, std::memory_order_relaxed);
- string_map_clear(&scope->elements);
- ptr_set_clear(&scope->imported);
- // LEAK NOTE(bill): Cloning this AST may be leaky but this is not really an issue due to arena-based allocation
- Ast *cloned_proc_type_node = clone_ast(pt->node);
- success = check_procedure_type(&nctx, final_proc_type, cloned_proc_type_node, &operands);
- if (!success) {
- return false;
- }
- rw_mutex_shared_lock(&gen_procs->mutex); // @local-mutex
- for (Entity *other : gen_procs->procs) {
- Type *pt = base_type(other->type);
- if (are_types_identical(pt, final_proc_type)) {
- rw_mutex_shared_unlock(&gen_procs->mutex); // @local-mutex
- if (poly_proc_data) {
- poly_proc_data->gen_entity = other;
- }
- DeclInfo *decl = other->decl_info;
- if (decl->proc_checked_state != ProcCheckedState_Checked) {
- ProcInfo *proc_info = gb_alloc_item(permanent_allocator(), ProcInfo);
- proc_info->file = other->file;
- proc_info->token = other->token;
- proc_info->decl = decl;
- proc_info->type = other->type;
- proc_info->body = decl->proc_lit->ProcLit.body;
- proc_info->tags = other->Procedure.tags;;
- proc_info->generated_from_polymorphic = true;
- proc_info->poly_def_node = poly_def_node;
- check_procedure_later(nctx.checker, proc_info);
- }
- return true;
- }
- }
- rw_mutex_shared_unlock(&gen_procs->mutex); // @local-mutex
- }
- Ast *proc_lit = clone_ast(old_decl->proc_lit);
- ast_node(pl, ProcLit, proc_lit);
- // NOTE(bill): Associate the scope declared above withinth this procedure declaration's type
- add_scope(&nctx, pl->type, final_proc_type->Proc.scope);
- final_proc_type->Proc.is_poly_specialized = true;
- final_proc_type->Proc.is_polymorphic = true;
- final_proc_type->Proc.variadic = src->Proc.variadic;
- final_proc_type->Proc.require_results = src->Proc.require_results;
- final_proc_type->Proc.c_vararg = src->Proc.c_vararg;
- final_proc_type->Proc.has_named_results = src->Proc.has_named_results;
- final_proc_type->Proc.diverging = src->Proc.diverging;
- final_proc_type->Proc.return_by_pointer = src->Proc.return_by_pointer;
- final_proc_type->Proc.optional_ok = src->Proc.optional_ok;
- final_proc_type->Proc.enable_target_feature = src->Proc.enable_target_feature;
- final_proc_type->Proc.require_target_feature = src->Proc.require_target_feature;
- for (isize i = 0; i < operands.count; i++) {
- Operand o = operands[i];
- if (final_proc_type == o.type ||
- base_entity->type == o.type) {
- // NOTE(bill): Cycle
- final_proc_type->Proc.is_poly_specialized = false;
- break;
- }
- }
- u64 tags = base_entity->Procedure.tags;
- Ast *ident = clone_ast(base_entity->identifier);
- Token token = ident->Ident.token;
- DeclInfo *d = make_decl_info(scope, old_decl->parent);
- d->gen_proc_type = final_proc_type;
- d->type_expr = pl->type;
- d->proc_lit = proc_lit;
- d->proc_checked_state = ProcCheckedState_Unchecked;
- d->defer_use_checked = false;
- Entity *entity = alloc_entity_procedure(nullptr, token, final_proc_type, tags);
- entity->state.store(EntityState_Resolved);
- entity->identifier = ident;
- add_entity_and_decl_info(&nctx, ident, entity, d);
- // NOTE(bill): Set the scope afterwards as this is not real overloading
- entity->scope = scope->parent;
- entity->file = base_entity->file;
- entity->pkg = base_entity->pkg;
- entity->flags = 0;
- entity->Procedure.optimization_mode = base_entity->Procedure.optimization_mode;
- if (base_entity->flags & EntityFlag_Cold) {
- entity->flags |= EntityFlag_Cold;
- }
- if (base_entity->flags & EntityFlag_Disabled) {
- entity->flags |= EntityFlag_Disabled;
- }
- d->entity = entity;
- AstFile *file = nullptr;
- {
- Scope *s = entity->scope;
- while (s != nullptr && s->file == nullptr) {
- file = s->file;
- s = s->parent;
- }
- }
- rw_mutex_lock(&gen_procs->mutex); // @local-mutex
- array_add(&gen_procs->procs, entity);
- rw_mutex_unlock(&gen_procs->mutex); // @local-mutex
- ProcInfo *proc_info = gb_alloc_item(permanent_allocator(), ProcInfo);
- proc_info->file = file;
- proc_info->token = token;
- proc_info->decl = d;
- proc_info->type = final_proc_type;
- proc_info->body = pl->body;
- proc_info->tags = tags;
- proc_info->generated_from_polymorphic = true;
- proc_info->poly_def_node = poly_def_node;
- if (poly_proc_data) {
- poly_proc_data->gen_entity = entity;
- poly_proc_data->proc_info = proc_info;
- entity->Procedure.generated_from_polymorphic = proc_info->generated_from_polymorphic;
- }
- // NOTE(bill): Check the newly generated procedure body
- check_procedure_later(nctx.checker, proc_info);
- return true;
- }
- gb_internal bool check_polymorphic_procedure_assignment(CheckerContext *c, Operand *operand, Type *type, Ast *poly_def_node, PolyProcData *poly_proc_data) {
- if (operand->expr == nullptr) return false;
- Entity *base_entity = entity_from_expr(operand->expr);
- if (base_entity == nullptr) return false;
- return find_or_generate_polymorphic_procedure(c, base_entity, type, nullptr, poly_def_node, poly_proc_data);
- }
- gb_internal bool find_or_generate_polymorphic_procedure_from_parameters(CheckerContext *c, Entity *base_entity, Array<Operand> const *operands, Ast *poly_def_node, PolyProcData *poly_proc_data) {
- return find_or_generate_polymorphic_procedure(c, base_entity, nullptr, operands, poly_def_node, poly_proc_data);
- }
- gb_internal bool check_type_specialization_to(CheckerContext *c, Type *specialization, Type *type, bool compound, bool modify_type);
- gb_internal bool is_polymorphic_type_assignable(CheckerContext *c, Type *poly, Type *source, bool compound, bool modify_type);
- gb_internal bool check_cast_internal(CheckerContext *c, Operand *x, Type *type);
- #define MAXIMUM_TYPE_DISTANCE 10
- gb_internal i64 check_distance_between_types(CheckerContext *c, Operand *operand, Type *type, bool allow_array_programming) {
- if (c == nullptr) {
- GB_ASSERT(operand->mode == Addressing_Value);
- GB_ASSERT(is_type_typed(operand->type));
- }
- if (operand->mode == Addressing_Invalid ||
- type == t_invalid) {
- return -1;
- }
- if (operand->mode == Addressing_Builtin) {
- return -1;
- }
- if (operand->mode == Addressing_Type) {
- if (is_type_typeid(type)) {
- if (is_type_polymorphic(operand->type)) {
- return -1;
- }
- add_type_info_type(c, operand->type);
- return 4;
- }
- return -1;
- }
- if (operand->mode == Addressing_ProcGroup && !is_type_proc(type)) {
- return -1;
- }
- Type *s = operand->type;
- if (are_types_identical(s, type)) {
- return 0;
- }
- Type *src = base_type(s);
- Type *dst = base_type(type);
- if (is_type_untyped_uninit(src)) {
- return 1;
- }
- if (is_type_untyped_nil(src)) {
- if (type_has_nil(dst)) {
- return 1;
- }
- return -1;
- }
- if (is_type_untyped(src)) {
- if (is_type_any(dst)) {
- // NOTE(bill): Anything can cast to 'Any'
- add_type_info_type(c, s);
- return MAXIMUM_TYPE_DISTANCE;
- }
- if (dst->kind == Type_Basic) {
- if (operand->mode == Addressing_Constant) {
- if (check_representable_as_constant(c, operand->value, dst, nullptr)) {
- if (is_type_typed(dst) && src->kind == Type_Basic) {
- switch (src->Basic.kind) {
- case Basic_UntypedBool:
- if (is_type_boolean(dst)) {
- return 1;
- }
- break;
- case Basic_UntypedRune:
- if (is_type_integer(dst) || is_type_rune(dst)) {
- return 1;
- }
- break;
- case Basic_UntypedInteger:
- if (is_type_integer(dst) || is_type_rune(dst)) {
- return 1;
- }
- break;
- case Basic_UntypedString:
- if (is_type_string(dst)) {
- return 1;
- }
- break;
- case Basic_UntypedFloat:
- if (is_type_float(dst)) {
- return 1;
- }
- break;
- case Basic_UntypedComplex:
- if (is_type_complex(dst)) {
- return 1;
- }
- if (is_type_quaternion(dst)) {
- return 2;
- }
- break;
- case Basic_UntypedQuaternion:
- if (is_type_quaternion(dst)) {
- return 1;
- }
- break;
- }
- }
- return 2;
- }
- return -1;
- }
- if (src->kind == Type_Basic) {
- Type *d = base_array_type(dst);
- i64 score = -1;
- switch (src->Basic.kind) {
- case Basic_UntypedBool:
- if (is_type_boolean(d)) {
- score = 1;
- }
- break;
- case Basic_UntypedRune:
- if (is_type_integer(d) || is_type_rune(d)) {
- score = 1;
- }
- break;
- case Basic_UntypedInteger:
- if (is_type_integer(d) || is_type_rune(d)) {
- score = 1;
- }
- break;
- case Basic_UntypedString:
- if (is_type_string(d)) {
- score = 1;
- }
- break;
- case Basic_UntypedFloat:
- if (is_type_float(d)) {
- score = 1;
- }
- break;
- case Basic_UntypedComplex:
- if (is_type_complex(d)) {
- score = 1;
- }
- if (is_type_quaternion(d)) {
- score = 2;
- }
- break;
- case Basic_UntypedQuaternion:
- if (is_type_quaternion(d)) {
- score = 1;
- }
- break;
- }
- if (score > 0) {
- if (is_type_typed(d)) {
- score += 1;
- }
- if (d != dst) {
- score += 6;
- }
- }
- return score;
- }
- }
- }
- if (is_type_enum(dst) && are_types_identical(dst->Enum.base_type, operand->type)) {
- if (c->in_enum_type) {
- return 3;
- }
- }
- {
- isize subtype_level = check_is_assignable_to_using_subtype(operand->type, type);
- if (subtype_level > 0) {
- return 4 + subtype_level;
- }
- }
- // rawptr <- ^T
- if (are_types_identical(type, t_rawptr) && is_type_pointer(src)) {
- return 5;
- }
- // rawptr <- [^]T
- if (are_types_identical(type, t_rawptr) && is_type_multi_pointer(src)) {
- return 5;
- }
- // ^T <- [^]T
- if (dst->kind == Type_Pointer && src->kind == Type_MultiPointer) {
- if (are_types_identical(dst->Pointer.elem, src->MultiPointer.elem)) {
- return 4;
- }
- }
- // [^]T <- ^T
- if (dst->kind == Type_MultiPointer && src->kind == Type_Pointer) {
- if (are_types_identical(dst->MultiPointer.elem, src->Pointer.elem)) {
- return 4;
- }
- }
- if (is_type_polymorphic(dst) && !is_type_polymorphic(src)) {
- bool modify_type = !c->no_polymorphic_errors;
- if (is_polymorphic_type_assignable(c, type, s, false, modify_type)) {
- return 2;
- }
- }
- if (is_type_union(dst)) {
- for (Type *vt : dst->Union.variants) {
- if (are_types_identical(vt, s)) {
- return 1;
- }
- if (is_type_proc(vt)) {
- if (are_types_identical(base_type(vt), src)) {
- return 1;
- }
- }
- }
- if (dst->Union.variants.count == 1) {
- Type *vt = dst->Union.variants[0];
- i64 score = check_distance_between_types(c, operand, vt, allow_array_programming);
- if (score >= 0) {
- return score+2;
- }
- } else if (is_type_untyped(src)) {
- i64 prev_lowest_score = -1;
- i64 lowest_score = -1;
- for (Type *vt : dst->Union.variants) {
- i64 score = check_distance_between_types(c, operand, vt, allow_array_programming);
- if (score >= 0) {
- if (lowest_score < 0) {
- lowest_score = score;
- } else {
- if (prev_lowest_score < 0) {
- prev_lowest_score = lowest_score;
- } else {
- prev_lowest_score = gb_min(prev_lowest_score, lowest_score);
- }
- lowest_score = gb_min(lowest_score, score);
- }
- }
- }
- if (lowest_score >= 0) {
- if (prev_lowest_score != lowest_score) { // remove possible ambiguities
- return lowest_score+2;
- }
- }
- }
- }
- if (is_type_proc(dst)) {
- if (are_types_identical(src, dst)) {
- return 3;
- }
- PolyProcData poly_proc_data = {};
- if (check_polymorphic_procedure_assignment(c, operand, type, operand->expr, &poly_proc_data)) {
- Entity *e = poly_proc_data.gen_entity;
- add_type_and_value(c, operand->expr, Addressing_Value, e->type, {});
- add_entity_use(c, operand->expr, e);
- return 4;
- }
- }
-
- if (is_type_complex_or_quaternion(dst)) {
- Type *elem = base_complex_elem_type(dst);
- if (are_types_identical(elem, base_type(src))) {
- return 5;
- }
- }
- if (allow_array_programming) {
- if (is_type_array(dst)) {
- Type *elem = base_array_type(dst);
- i64 distance = check_distance_between_types(c, operand, elem, allow_array_programming);
- if (distance >= 0) {
- return distance + 6;
- }
- }
- if (is_type_simd_vector(dst)) {
- Type *dst_elem = base_array_type(dst);
- i64 distance = check_distance_between_types(c, operand, dst_elem, allow_array_programming);
- if (distance >= 0) {
- return distance + 6;
- }
- }
- }
-
- if (is_type_matrix(dst)) {
- if (are_types_identical(src, dst)) {
- return 5;
- }
- if (dst->Matrix.row_count == dst->Matrix.column_count) {
- Type *dst_elem = base_array_type(dst);
- i64 distance = check_distance_between_types(c, operand, dst_elem, allow_array_programming);
- if (distance >= 0) {
- return distance + 7;
- }
- }
- }
- if (is_type_any(dst)) {
- if (!is_type_polymorphic(src)) {
- if (operand->mode == Addressing_Context && operand->type == t_context) {
- return -1;
- } else {
- // NOTE(bill): Anything can cast to 'Any'
- add_type_info_type(c, s);
- return MAXIMUM_TYPE_DISTANCE;
- }
- }
- }
- Ast *expr = unparen_expr(operand->expr);
- if (expr != nullptr) {
- if (expr->kind == Ast_AutoCast) {
- Operand x = *operand;
- x.expr = expr->AutoCast.expr;
- if (check_cast_internal(c, &x, type)) {
- return MAXIMUM_TYPE_DISTANCE;
- }
- }
- }
- return -1;
- }
- gb_internal i64 assign_score_function(i64 distance, bool is_variadic=false) {
- // 3*x^2 + 1 > x^2 + x + 1 (for positive x)
- i64 const c = 3*MAXIMUM_TYPE_DISTANCE*MAXIMUM_TYPE_DISTANCE + 1;
- // TODO(bill): A decent score function
- i64 d = distance*distance; // x^2
- if (is_variadic && d >= 0) {
- d += distance + 1; // x^2 + x + 1
- }
- return gb_max(c - d, 0);
- }
- gb_internal bool check_is_assignable_to_with_score(CheckerContext *c, Operand *operand, Type *type, i64 *score_, bool is_variadic=false, bool allow_array_programming=true) {
- if (c == nullptr) {
- GB_ASSERT(operand->mode == Addressing_Value);
- GB_ASSERT(is_type_typed(operand->type));
- }
- if (operand->mode == Addressing_Invalid || type == t_invalid) {
- if (score_) *score_ = 0;
- return false;
- }
- // Handle polymorphic procedure used as default parameter
- if (operand->mode == Addressing_Value && is_type_proc(type) && is_type_proc(operand->type)) {
- Entity *e = entity_from_expr(operand->expr);
- if (e != nullptr && e->kind == Entity_Procedure && is_type_polymorphic(e->type) && !is_type_polymorphic(type)) {
- // Special case: Allow a polymorphic procedure to be used as default value for concrete proc type
- // during the initial check. It will be properly instantiated when actually used.
- if (score_) *score_ = assign_score_function(1);
- return true;
- }
- }
- i64 score = check_distance_between_types(c, operand, type, allow_array_programming);
- if (score >= 0) {
- if (score_) *score_ = assign_score_function(score, is_variadic);
- return true;
- }
- if (score_) *score_ = 0;
- return false;
- }
- gb_internal bool check_is_assignable_to(CheckerContext *c, Operand *operand, Type *type, bool allow_array_programming=true) {
- i64 score = 0;
- return check_is_assignable_to_with_score(c, operand, type, &score, /*is_variadic*/false, allow_array_programming);
- }
- gb_internal bool internal_check_is_assignable_to(Type *src, Type *dst) {
- Operand x = {};
- x.type = src;
- x.mode = Addressing_Value;
- return check_is_assignable_to(nullptr, &x, dst);
- }
- gb_internal AstPackage *get_package_of_type(Type *type) {
- for (;;) {
- if (type == nullptr) {
- return nullptr;
- }
- switch (type->kind) {
- case Type_Basic:
- return builtin_pkg;
- case Type_Named:
- if (type->Named.type_name != nullptr) {
- return type->Named.type_name->pkg;
- }
- return nullptr;
- case Type_Pointer:
- type = type->Pointer.elem;
- continue;
- case Type_Array:
- type = type->Array.elem;
- continue;
- case Type_Slice:
- type = type->Slice.elem;
- continue;
- case Type_DynamicArray:
- type = type->DynamicArray.elem;
- continue;
- }
- return nullptr;
- }
- }
- // NOTE(bill): 'context_name' is for debugging and error messages
- gb_internal void check_assignment(CheckerContext *c, Operand *operand, Type *type, String context_name) {
- check_not_tuple(c, operand);
- if (operand->mode == Addressing_Invalid) {
- return;
- }
- // Grab definite or indefinite article matching `context_name`, or "" if not found.
- String article = error_article(context_name);
- if (is_type_untyped(operand->type)) {
- Type *target_type = type;
- if (type == nullptr || is_type_any(type)) {
- if (type == nullptr && is_type_untyped_uninit(operand->type)) {
- error(operand->expr, "Use of --- in %.*s%.*s", LIT(article), LIT(context_name));
- operand->mode = Addressing_Invalid;
- return;
- }
- if (type == nullptr && is_type_untyped_nil(operand->type)) {
- error(operand->expr, "Use of untyped nil in %.*s%.*s", LIT(article), LIT(context_name));
- operand->mode = Addressing_Invalid;
- return;
- }
- target_type = default_type(operand->type);
- if (type != nullptr && !is_type_any(type)) {
- GB_ASSERT_MSG(is_type_typed(target_type), "%s", type_to_string(type));
- }
- add_type_info_type(c, type);
- add_type_info_type(c, target_type);
- }
- convert_to_typed(c, operand, target_type);
- if (operand->mode == Addressing_Invalid) {
- return;
- }
- }
- if (type == nullptr) {
- return;
- }
- if (operand->mode == Addressing_ProcGroup) {
- bool good = false;
- if (type != nullptr && is_type_proc(type)) {
- Array<Entity *> procs = proc_group_entities(c, *operand);
- // NOTE(bill): These should be done
- for (Entity *e : procs) {
- Type *t = base_type(e->type);
- if (t == t_invalid) {
- continue;
- }
- Operand x = {};
- x.mode = Addressing_Value;
- x.type = t;
- if (check_is_assignable_to(c, &x, type)) {
- add_entity_use(c, operand->expr, e);
- good = true;
- break;
- }
- }
- }
- if (!good) {
- gbString expr_str = expr_to_string(operand->expr);
- gbString op_type_str = type_to_string(operand->type);
- gbString type_str = type_to_string(type);
- defer (gb_string_free(type_str));
- defer (gb_string_free(op_type_str));
- defer (gb_string_free(expr_str));
- // TODO(bill): is this a good enough error message?
- error(operand->expr,
- "Cannot assign overloaded procedure group '%s' to '%s' in %.*s%.*s",
- expr_str,
- op_type_str,
- LIT(article),
- LIT(context_name));
- operand->mode = Addressing_Invalid;
- }
- convert_to_typed(c, operand, type);
- return;
- }
- if (check_is_assignable_to(c, operand, type)) {
- if (operand->mode == Addressing_Type && is_type_typeid(type)) {
- add_type_info_type(c, operand->type);
- add_type_and_value(c, operand->expr, Addressing_Value, type, exact_value_typeid(operand->type));
- }
- } else {
- gbString expr_str = expr_to_string(operand->expr);
- gbString op_type_str = type_to_string(operand->type);
- gbString type_str = type_to_string(type);
- defer (gb_string_free(type_str));
- defer (gb_string_free(op_type_str));
- defer (gb_string_free(expr_str));
- switch (operand->mode) {
- case Addressing_Builtin:
- error(operand->expr,
- "Cannot assign built-in procedure '%s' to %.*s%.*s",
- expr_str,
- LIT(article),
- LIT(context_name));
- break;
- case Addressing_Type:
- if (is_type_polymorphic(operand->type)) {
- error(operand->expr,
- "Cannot assign '%s', a polymorphic type, to %.*s%.*s",
- op_type_str,
- LIT(article),
- LIT(context_name));
- } else {
- ERROR_BLOCK();
- error(operand->expr,
- "Cannot assign '%s', a type, to %.*s%.*s",
- op_type_str,
- LIT(article),
- LIT(context_name));
- if (type && are_types_identical(type, t_any)) {
- error_line("\tSuggestion: 'typeid_of(%s)'", expr_str);
- }
- }
- break;
- default:
- // TODO(bill): is this a good enough error message?
- {
- gbString op_type_extra = gb_string_make(heap_allocator(), "");
- gbString type_extra = gb_string_make(heap_allocator(), "");
- defer (gb_string_free(op_type_extra));
- defer (gb_string_free(type_extra));
- isize on = gb_string_length(op_type_str);
- isize tn = gb_string_length(type_str);
- if (on == tn && gb_strncmp(op_type_str, type_str, on) == 0) {
- AstPackage *op_pkg = get_package_of_type(operand->type);
- AstPackage *type_pkg = get_package_of_type(type);
- if (op_pkg != nullptr) {
- op_type_extra = gb_string_append_fmt(op_type_extra, " (package %.*s)", LIT(op_pkg->name));
- }
- if (type_pkg != nullptr) {
- type_extra = gb_string_append_fmt(type_extra, " (package %.*s)", LIT(type_pkg->name));
- }
- }
- ERROR_BLOCK();
- error(operand->expr,
- "Cannot assign value '%s' of type '%s%s' to '%s%s' in %.*s%.*s",
- expr_str,
- op_type_str, op_type_extra,
- type_str, type_extra,
- LIT(article),
- LIT(context_name));
- check_assignment_error_suggestion(c, operand, type);
- Type *src = base_type(operand->type);
- Type *dst = base_type(type);
- if (context_name == "procedure argument") {
- if (is_type_slice(src) && are_types_identical(src->Slice.elem, dst)) {
- gbString a = expr_to_string(operand->expr);
- error_line("\tSuggestion: Did you mean to pass the slice into the variadic parameter with ..%s?\n\n", a);
- gb_string_free(a);
- }
- }
- if (src->kind == dst->kind && src->kind == Type_Proc) {
- Type *x = src;
- Type *y = dst;
- bool same_inputs = are_types_identical_internal(x->Proc.params, y->Proc.params, false);
- bool same_outputs = are_types_identical_internal(x->Proc.results, y->Proc.results, false);
- if (same_inputs && same_outputs &&
- x->Proc.calling_convention != y->Proc.calling_convention) {
- gbString s_expected = type_to_string(y);
- gbString s_got = type_to_string(x);
- error_line("\tNote: The calling conventions differ between the procedure signature types\n");
- error_line("\t Expected \"%s\", got \"%s\"\n",
- proc_calling_convention_strings[y->Proc.calling_convention],
- proc_calling_convention_strings[x->Proc.calling_convention]);
- error_line("\t Expected: %s\n", s_expected);
- error_line("\t Got: %s\n", s_got);
- gb_string_free(s_got);
- gb_string_free(s_expected);
- } else if (same_inputs && same_outputs &&
- x->Proc.diverging != y->Proc.diverging) {
- gbString s_expected = type_to_string(y);
- if (y->Proc.diverging) {
- s_expected = gb_string_appendc(s_expected, " -> !");
- }
- gbString s_got = type_to_string(x);
- if (x->Proc.diverging) {
- s_got = gb_string_appendc(s_got, " -> !");
- }
- error_line("\tNote: One of the procedures is diverging while the other isn't\n");
- error_line("\t Expected: %s\n", s_expected);
- error_line("\t Got: %s\n", s_got);
- gb_string_free(s_got);
- gb_string_free(s_expected);
- } else if (same_inputs && !same_outputs) {
- gbString s_expected = type_to_string(y->Proc.results);
- gbString s_got = type_to_string(x->Proc.results);
- error_line("\tNote: The return types differ between the procedure signature types\n");
- error_line("\t Expected: %s\n", s_expected);
- error_line("\t Got: %s\n", s_got);
- gb_string_free(s_got);
- gb_string_free(s_expected);
- } else if (!same_inputs && same_outputs) {
- gbString s_expected = type_to_string(y->Proc.params);
- gbString s_got = type_to_string(x->Proc.params);
- error_line("\tNote: The input parameter types differ between the procedure signature types\n");
- error_line("\t Expected: %s\n", s_expected);
- error_line("\t Got: %s\n", s_got);
- gb_string_free(s_got);
- gb_string_free(s_expected);
- } else {
- gbString s_expected = type_to_string(y);
- gbString s_got = type_to_string(x);
- error_line("\tNote: The signature type do not match whatsoever\n");
- error_line("\t Expected: %s\n", s_expected);
- error_line("\t Got: %s\n", s_got);
- gb_string_free(s_got);
- gb_string_free(s_expected);
- }
- }
- }
- break;
- }
- operand->mode = Addressing_Invalid;
- return;
- }
- }
- gb_internal bool polymorphic_assign_index(Type **gt_, i64 *dst_count, i64 source_count) {
- Type *gt = *gt_;
-
- GB_ASSERT(gt->kind == Type_Generic);
- Entity *e = scope_lookup(gt->Generic.scope, gt->Generic.name);
- GB_ASSERT(e != nullptr);
- if (e->kind == Entity_TypeName) {
- *gt_ = nullptr;
- *dst_count = source_count;
- e->kind = Entity_Constant;
- e->Constant.value = exact_value_i64(source_count);
- e->type = t_untyped_integer;
- return true;
- } else if (e->kind == Entity_Constant) {
- *gt_ = nullptr;
- if (e->Constant.value.kind != ExactValue_Integer) {
- return false;
- }
- i64 count = big_int_to_i64(&e->Constant.value.value_integer);
- if (count != source_count) {
- return false;
- }
- *dst_count = source_count;
- return true;
- }
- return false;
- }
- gb_internal bool is_polymorphic_type_assignable(CheckerContext *c, Type *poly, Type *source, bool compound, bool modify_type) {
- Operand o = {Addressing_Value};
- o.type = source;
- switch (poly->kind) {
- case Type_Basic:
- if (compound) return are_types_identical(poly, source);
- return check_is_assignable_to(c, &o, poly);
- case Type_Named: {
- if (check_type_specialization_to(c, poly, source, compound, modify_type)) {
- return true;
- }
- if (compound || !is_type_generic(poly)) {
- return are_types_identical(poly, source);
- }
- return check_is_assignable_to(c, &o, poly);
- }
- case Type_Generic: {
- if (poly->Generic.specialized != nullptr) {
- Type *s = poly->Generic.specialized;
- if (!check_type_specialization_to(c, s, source, compound, modify_type)) {
- return false;
- }
- }
- if (modify_type) {
- Type *ds = default_type(source);
- gb_memmove(poly, ds, gb_size_of(Type));
- }
- return true;
- }
- case Type_Pointer:
- if (source->kind == Type_Pointer) {
- isize level = check_is_assignable_to_using_subtype(source->Pointer.elem, poly->Pointer.elem, /*level*/0, /*src_is_ptr*/false, /*allow_polymorphic*/true);
- if (level > 0) {
- return true;
- }
- return is_polymorphic_type_assignable(c, poly->Pointer.elem, source->Pointer.elem, true, modify_type);
- } else if (source->kind == Type_MultiPointer) {
- isize level = check_is_assignable_to_using_subtype(source->MultiPointer.elem, poly->Pointer.elem);
- if (level > 0) {
- return true;
- }
- return is_polymorphic_type_assignable(c, poly->Pointer.elem, source->MultiPointer.elem, true, modify_type);
- }
- return false;
- case Type_MultiPointer:
- if (source->kind == Type_MultiPointer) {
- isize level = check_is_assignable_to_using_subtype(source->MultiPointer.elem, poly->MultiPointer.elem);
- if (level > 0) {
- return true;
- }
- return is_polymorphic_type_assignable(c, poly->MultiPointer.elem, source->MultiPointer.elem, true, modify_type);
- } else if (source->kind == Type_Pointer) {
- isize level = check_is_assignable_to_using_subtype(source->Pointer.elem, poly->MultiPointer.elem);
- if (level > 0) {
- return true;
- }
- return is_polymorphic_type_assignable(c, poly->MultiPointer.elem, source->Pointer.elem, true, modify_type);
- }
- return false;
- case Type_Array:
- if (source->kind == Type_Array) {
- if (poly->Array.generic_count != nullptr) {
- if (!polymorphic_assign_index(&poly->Array.generic_count, &poly->Array.count, source->Array.count)) {
- return false;
- }
- }
- if (poly->Array.count == source->Array.count) {
- return is_polymorphic_type_assignable(c, poly->Array.elem, source->Array.elem, true, modify_type);
- }
- } else if (source->kind == Type_EnumeratedArray) {
- if (poly->Array.generic_count != nullptr) {
- Type *gt = poly->Array.generic_count;
- GB_ASSERT(gt->kind == Type_Generic);
- Entity *e = scope_lookup(gt->Generic.scope, gt->Generic.name);
- GB_ASSERT(e != nullptr);
- if (e->kind == Entity_TypeName) {
- Type *index = source->EnumeratedArray.index;
- Type *it = base_type(index);
- if (it->kind != Type_Enum) {
- return false;
- }
- poly->kind = Type_EnumeratedArray;
- poly->cached_size = -1;
- poly->cached_align = -1;
- poly->flags.exchange(source->flags);
- poly->failure = false;
- poly->EnumeratedArray.elem = source->EnumeratedArray.elem;
- poly->EnumeratedArray.index = source->EnumeratedArray.index;
- poly->EnumeratedArray.min_value = source->EnumeratedArray.min_value;
- poly->EnumeratedArray.max_value = source->EnumeratedArray.max_value;
- poly->EnumeratedArray.count = source->EnumeratedArray.count;
- poly->EnumeratedArray.op = source->EnumeratedArray.op;
- e->kind = Entity_TypeName;
- e->TypeName.is_type_alias = true;
- e->type = index;
- if (poly->EnumeratedArray.count == source->EnumeratedArray.count) {
- return is_polymorphic_type_assignable(c, poly->EnumeratedArray.elem, source->EnumeratedArray.elem, true, modify_type);
- }
- }
- }
- }
- return false;
- case Type_EnumeratedArray:
- if (source->kind == Type_EnumeratedArray) {
- if (poly->EnumeratedArray.op != source->EnumeratedArray.op) {
- return false;
- }
- if (poly->EnumeratedArray.op) {
- if (poly->EnumeratedArray.count != source->EnumeratedArray.count) {
- return false;
- }
- if (compare_exact_values(Token_NotEq, *poly->EnumeratedArray.min_value, *source->EnumeratedArray.min_value)) {
- return false;
- }
- if (compare_exact_values(Token_NotEq, *poly->EnumeratedArray.max_value, *source->EnumeratedArray.max_value)) {
- return false;
- }
- return is_polymorphic_type_assignable(c, poly->EnumeratedArray.index, source->EnumeratedArray.index, true, modify_type);
- }
- bool index = is_polymorphic_type_assignable(c, poly->EnumeratedArray.index, source->EnumeratedArray.index, true, modify_type);
- bool elem = is_polymorphic_type_assignable(c, poly->EnumeratedArray.elem, source->EnumeratedArray.elem, true, modify_type);
- return index || elem;
- }
- return false;
- case Type_DynamicArray:
- if (source->kind == Type_DynamicArray) {
- return is_polymorphic_type_assignable(c, poly->DynamicArray.elem, source->DynamicArray.elem, true, modify_type);
- }
- return false;
- case Type_Slice:
- if (source->kind == Type_Slice) {
- return is_polymorphic_type_assignable(c, poly->Slice.elem, source->Slice.elem, true, modify_type);
- }
- return false;
- case Type_Enum:
- return false;
- case Type_BitSet:
- if (source->kind == Type_BitSet) {
- if (!is_polymorphic_type_assignable(c, poly->BitSet.elem, source->BitSet.elem, true, modify_type)) {
- return false;
- }
-
- // For generic types like bit_set[$T] the upper and lower of the poly type will be zeroes since
- // it could not figure that stuff out when the poly type was created.
- if (poly->BitSet.upper == 0 && modify_type) {
- poly->BitSet.upper = source->BitSet.upper;
- }
- if (poly->BitSet.lower == 0 && modify_type) {
- poly->BitSet.lower = source->BitSet.lower;
- }
- if (poly->BitSet.underlying == nullptr) {
- if (modify_type) {
- poly->BitSet.underlying = source->BitSet.underlying;
- }
- } else if (!is_polymorphic_type_assignable(c, poly->BitSet.underlying, source->BitSet.underlying, true, modify_type)) {
- return false;
- }
- return true;
- }
- return false;
- case Type_Union:
- if (source->kind == Type_Union) {
- TypeUnion *x = &poly->Union;
- TypeUnion *y = &source->Union;
- if (x->variants.count != y->variants.count) {
- return false;
- }
- for_array(i, x->variants) {
- Type *a = x->variants[i];
- Type *b = y->variants[i];
- bool ok = is_polymorphic_type_assignable(c, a, b, false, modify_type);
- if (!ok) return false;
- }
- return true;
- }
- return false;
- case Type_Struct:
- if (source->kind == Type_Struct) {
- if (poly->Struct.soa_kind == source->Struct.soa_kind &&
- poly->Struct.soa_kind != StructSoa_None) {
- bool ok = is_polymorphic_type_assignable(c, poly->Struct.soa_elem, source->Struct.soa_elem, true, modify_type);
- if (ok) switch (source->Struct.soa_kind) {
- case StructSoa_None:
- default:
- GB_PANIC("Unhandled SOA Kind");
- break;
- case StructSoa_Fixed:
- if (modify_type) {
- Type *type = make_soa_struct_fixed(c, nullptr, poly->Struct.node, poly->Struct.soa_elem, poly->Struct.soa_count, nullptr);
- gb_memmove(poly, type, gb_size_of(*type));
- }
- break;
- case StructSoa_Slice:
- if (modify_type) {
- Type *type = make_soa_struct_slice(c, nullptr, poly->Struct.node, poly->Struct.soa_elem);
- gb_memmove(poly, type, gb_size_of(*type));
- }
- break;
- case StructSoa_Dynamic:
- if (modify_type) {
- Type *type = make_soa_struct_dynamic_array(c, nullptr, poly->Struct.node, poly->Struct.soa_elem);
- gb_memmove(poly, type, gb_size_of(*type));
- }
- break;
- }
- return ok;
- }
- // NOTE(bill): Check for subtypes of
- // return check_is_assignable_to(c, &o, poly); // && is_type_subtype_of_and_allow_polymorphic(o.type, poly);
- }
- return false;
- case Type_BitField:
- if (source->kind == Type_BitField) {
- return is_polymorphic_type_assignable(c, poly->BitField.backing_type, source->BitField.backing_type, true, modify_type);
- }
- return false;
- case Type_Tuple:
- GB_PANIC("This should never happen");
- return false;
- case Type_Proc:
- if (source->kind == Type_Proc) {
- TypeProc *x = &poly->Proc;
- TypeProc *y = &source->Proc;
- if (x->calling_convention != y->calling_convention) {
- return false;
- }
- if (x->c_vararg != y->c_vararg) {
- return false;
- }
- if (x->variadic != y->variadic) {
- return false;
- }
- if (x->param_count != y->param_count) {
- return false;
- }
- if (x->result_count != y->result_count) {
- return false;
- }
- for (isize i = 0; i < x->param_count; i++) {
- Entity *a = x->params->Tuple.variables[i];
- Entity *b = y->params->Tuple.variables[i];
- bool ok = is_polymorphic_type_assignable(c, a->type, b->type, false, modify_type);
- if (!ok) return false;
- }
- for (isize i = 0; i < x->result_count; i++) {
- Entity *a = x->results->Tuple.variables[i];
- Entity *b = y->results->Tuple.variables[i];
- bool ok = is_polymorphic_type_assignable(c, a->type, b->type, false, modify_type);
- if (!ok) return false;
- }
- return true;
- }
- return false;
- case Type_Map:
- if (source->kind == Type_Map) {
- bool key = is_polymorphic_type_assignable(c, poly->Map.key, source->Map.key, true, modify_type);
- bool value = is_polymorphic_type_assignable(c, poly->Map.value, source->Map.value, true, modify_type);
- if (key || value) {
- poly->Map.lookup_result_type = nullptr;
- init_map_internal_types(poly);
- return true;
- }
- }
- return false;
-
- case Type_Matrix:
- if (source->kind == Type_Matrix) {
- if (poly->Matrix.generic_row_count != nullptr) {
- poly->Matrix.stride_in_bytes = 0;
- if (!polymorphic_assign_index(&poly->Matrix.generic_row_count, &poly->Matrix.row_count, source->Matrix.row_count)) {
- return false;
- }
- }
- if (poly->Matrix.generic_column_count != nullptr) {
- poly->Matrix.stride_in_bytes = 0;
- if (!polymorphic_assign_index(&poly->Matrix.generic_column_count, &poly->Matrix.column_count, source->Matrix.column_count)) {
- return false;
- }
- }
- if (poly->Matrix.row_count == source->Matrix.row_count &&
- poly->Matrix.column_count == source->Matrix.column_count) {
- return is_polymorphic_type_assignable(c, poly->Matrix.elem, source->Matrix.elem, true, modify_type);
- }
- }
- return false;
- case Type_SimdVector:
- if (source->kind == Type_SimdVector) {
- if (poly->SimdVector.generic_count != nullptr) {
- if (!polymorphic_assign_index(&poly->SimdVector.generic_count, &poly->SimdVector.count, source->SimdVector.count)) {
- return false;
- }
- }
- if (poly->SimdVector.count == source->SimdVector.count) {
- return is_polymorphic_type_assignable(c, poly->SimdVector.elem, source->SimdVector.elem, true, modify_type);
- }
- }
- return false;
- }
- return false;
- }
- gb_internal bool check_cycle(CheckerContext *c, Entity *curr, bool report) {
- if (curr->state != EntityState_InProgress) {
- return false;
- }
- for_array(i, *c->type_path) {
- Entity *prev = c->type_path->data[i];
- if (prev == curr) {
- if (report) {
- error(curr->token, "Illegal declaration cycle of `%.*s`", LIT(curr->token.string));
- for (isize j = i; j < c->type_path->count; j++) {
- Entity *curr = (*c->type_path)[j];
- error(curr->token, "\t%.*s refers to", LIT(curr->token.string));
- }
- error(curr->token, "\t%.*s", LIT(curr->token.string));
- curr->type = t_invalid;
- }
- return true;
- }
- }
- return false;
- }
- struct CIdentSuggestion {
- String name;
- String msg;
- };
- // NOTE(bill): this linear look-up table might be slow but because it's an error case, it should be fine
- gb_internal CIdentSuggestion const c_ident_suggestions[] = {
- {str_lit("while"), str_lit("'for'? Odin only has one loop construct: 'for'")},
- {str_lit("sizeof"), str_lit("'size_of'?")},
- {str_lit("alignof"), str_lit("'align_of'?")},
- {str_lit("offsetof"), str_lit("'offset_of'?")},
- {str_lit("_Bool"), str_lit("'bool'?")},
- {str_lit("char"), str_lit("'u8', 'i8', or 'c.char' (which is part of 'core:c')?")},
- {str_lit("short"), str_lit("'i16' or 'c.short' (which is part of 'core:c')?")},
- {str_lit("long"), str_lit("'c.long' (which is part of 'core:c')?")},
- {str_lit("float"), str_lit("'f32'?")},
- {str_lit("double"), str_lit("'f64'?")},
- {str_lit("unsigned"), str_lit("'c.uint' (which is part of 'core:c')?")},
- {str_lit("signed"), str_lit("'c.int' (which is part of 'core:c')?")},
- {str_lit("size_t"), str_lit("'uint', or 'c.size_t' (which is part of 'core:c')?")},
- {str_lit("ssize_t"), str_lit("'int', or 'c.ssize_t' (which is part of 'core:c')?")},
- {str_lit("uintptr_t"), str_lit("'uintptr'?")},
- {str_lit("intptr_t"), str_lit("'uintptr' or `int` or something else?")},
- {str_lit("ptrdiff_t"), str_lit("'int' or 'c.ptrdiff_t' (which is part of 'core:c')?")},
- {str_lit("intmax_t"), str_lit("'c.intmax_t' (which is part of 'core:c')?")},
- {str_lit("uintmax_t"), str_lit("'c.uintmax_t' (which is part of 'core:c')?")},
- {str_lit("uint8_t"), str_lit("'u8'?")},
- {str_lit("int8_t"), str_lit("'i8'?")},
- {str_lit("uint16_t"), str_lit("'u16'?")},
- {str_lit("int16_t"), str_lit("'i16'?")},
- {str_lit("uint32_t"), str_lit("'u32'?")},
- {str_lit("int32_t"), str_lit("'i32'?")},
- {str_lit("uint64_t"), str_lit("'u64'?")},
- {str_lit("int64_t"), str_lit("'i64'?")},
- {str_lit("uint128_t"), str_lit("'u128'?")},
- {str_lit("int128_t"), str_lit("'i128'?")},
- {str_lit("float32"), str_lit("'f32'?")},
- {str_lit("float64"), str_lit("'f64'?")},
- {str_lit("float32_t"), str_lit("'f32'?")},
- {str_lit("float64_t"), str_lit("'f64'?")},
- };
- gb_internal Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *named_type, Type *type_hint, bool allow_import_name) {
- GB_ASSERT(n->kind == Ast_Ident);
- o->mode = Addressing_Invalid;
- o->expr = n;
- String name = n->Ident.token.string;
- Entity *e = scope_lookup(c->scope, name);
- if (e == nullptr) {
- if (is_blank_ident(name)) {
- error(n, "'_' cannot be used as a value");
- } else {
- ERROR_BLOCK();
- error(n, "Undeclared name: %.*s", LIT(name));
- // NOTE(bill): Loads of checks for C programmers
- for (CIdentSuggestion const &suggestion : c_ident_suggestions) {
- if (name == suggestion.name) {
- error_line("\tSuggestion: Did you mean %.*s\n", LIT(suggestion.msg));
- }
- }
- }
- o->type = t_invalid;
- o->mode = Addressing_Invalid;
- if (named_type != nullptr) {
- set_base_type(named_type, t_invalid);
- }
- return nullptr;
- }
- GB_ASSERT((e->flags & EntityFlag_Overridden) == 0);
- if (e->parent_proc_decl != nullptr &&
- e->parent_proc_decl != c->curr_proc_decl) {
- if (e->kind == Entity_Variable) {
- if ((e->flags & EntityFlag_Static) == 0) {
- error(n, "Nested procedures do not capture its parent's variables: %.*s", LIT(name));
- return nullptr;
- }
- } else if (e->kind == Entity_Label) {
- error(n, "Nested procedures do not capture its parent's labels: %.*s", LIT(name));
- return nullptr;
- }
- }
- if (e->kind == Entity_ProcGroup) {
- auto *pge = &e->ProcGroup;
- DeclInfo *d = decl_info_of_entity(e);
- check_entity_decl(c, e, d, nullptr);
- Array<Entity *> procs = pge->entities;
- bool skip = false;
- if (type_hint != nullptr && is_type_proc(type_hint)) {
- // NOTE(bill): These should be done
- for (Entity *proc : procs) {
- Type *t = base_type(proc->type);
- if (t == t_invalid) {
- continue;
- }
- Operand x = {};
- x.mode = Addressing_Value;
- x.type = t;
- if (check_is_assignable_to(c, &x, type_hint)) {
- e = proc;
- add_entity_use(c, n, e);
- skip = true;
- break;
- }
- }
- }
- if (!skip) {
- o->mode = Addressing_ProcGroup;
- o->type = t_invalid;
- o->proc_group = e;
- return nullptr;
- }
- }
- add_entity_use(c, n, e);
- if (e->state == EntityState_Unresolved) {
- check_entity_decl(c, e, nullptr, named_type);
- }
- if (e->type == nullptr) {
- // TODO(bill): Which is correct? return or compiler_error?
- // compiler_error("How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(name));
- return nullptr;
- }
- e->flags |= EntityFlag_Used;
- Type *type = e->type;
- o->type = type;
- switch (e->kind) {
- case Entity_Constant:
- if (type == t_invalid) {
- o->type = t_invalid;
- return e;
- }
- o->value = e->Constant.value;
- if (o->value.kind == ExactValue_Invalid) {
- return e;
- }
- if (o->value.kind == ExactValue_Procedure) {
- Entity *proc = strip_entity_wrapping(o->value.value_procedure);
- if (proc != nullptr) {
- o->mode = Addressing_Value;
- o->type = proc->type;
- return proc;
- }
- }
- o->mode = Addressing_Constant;
- break;
- case Entity_Variable:
- e->flags |= EntityFlag_Used;
- if (type == t_invalid) {
- o->type = t_invalid;
- return e;
- }
- o->mode = Addressing_Variable;
- if (e->flags & EntityFlag_Value) {
- o->mode = Addressing_Value;
- }
- break;
- case Entity_Procedure:
- o->mode = Addressing_Value;
- o->value = exact_value_procedure(n);
- break;
- case Entity_Builtin:
- o->builtin_id = cast(BuiltinProcId)e->Builtin.id;
- o->mode = Addressing_Builtin;
- break;
- case Entity_TypeName:
- o->mode = Addressing_Type;
- if (check_cycle(c, e, true)) {
- o->type = t_invalid;
- }
- if (o->type != nullptr && o->type->kind == Type_Named && o->type->Named.type_name->TypeName.is_type_alias) {
- Type *bt = base_type(o->type);
- if (bt != nullptr) {
- o->type = bt;
- }
- }
- break;
- case Entity_ImportName:
- if (!allow_import_name) {
- error(n, "Use of import name '%.*s' not in the form of 'x.y'", LIT(name));
- }
- return e;
- case Entity_LibraryName:
- if (!allow_import_name) {
- error(n, "Use of library '%.*s' not in foreign block", LIT(name));
- }
- return e;
- case Entity_Label:
- o->mode = Addressing_NoValue;
- break;
- case Entity_Nil:
- o->mode = Addressing_Value;
- break;
- default:
- compiler_error("Unknown EntityKind %.*s", LIT(entity_strings[e->kind]));
- break;
- }
- return e;
- }
- gb_internal bool check_unary_op(CheckerContext *c, Operand *o, Token op) {
- if (o->type == nullptr) {
- gbString str = expr_to_string(o->expr);
- error(o->expr, "Expression has no value '%s'", str);
- gb_string_free(str);
- return false;
- }
- if (o->mode == Addressing_Type) {
- gbString str = type_to_string(o->type);
- error(o->expr, "Expected an expression for operator '%.*s', got type '%s'", LIT(op.string), str);
- gb_string_free(str);
- return false;
- }
- Type *type = base_type(core_array_type(o->type));
- gbString str = nullptr;
- switch (op.kind) {
- case Token_Add:
- case Token_Sub:
- if (!is_type_numeric(type)) {
- str = expr_to_string(o->expr);
- error(op, "Operator '%.*s' is not allowed with '%s'", LIT(op.string), str);
- gb_string_free(str);
- }
- break;
- case Token_Xor:
- if (!is_type_integer(type) && !is_type_boolean(type) && !is_type_bit_set(type)) {
- error(op, "Operator '%.*s' is only allowed with integers, booleans, or bit sets", LIT(op.string));
- }
- break;
- case Token_Not:
- if (!is_type_boolean(type) || is_type_array_like(o->type)) {
- ERROR_BLOCK();
- error(op, "Operator '%.*s' is only allowed on boolean expressions", LIT(op.string));
- if (is_type_integer(type)) {
- str = expr_to_string(o->expr);
- error_line("\tSuggestion: Did you mean to do one of the following?\n");
- error_line("\t\t'%s == 0'?\n", str);
- error_line("\t\tUse of the bitwise not operator '~'?\n");
- gb_string_free(str);
- }
- } else {
- o->type = t_untyped_bool;
- }
- break;
- case Token_Mul:
- {
- ERROR_BLOCK();
- error(op, "Operator '%.*s' is not a valid unary operator in Odin", LIT(op.string));
- if (is_type_pointer(o->type)) {
- str = expr_to_string(o->expr);
- error_line("\tSuggestion: Did you mean '%s^'?\n", str);
- o->type = type_deref(o->type);
- } else if (is_type_multi_pointer(o->type)) {
- str = expr_to_string(o->expr);
- error_line("\tSuggestion: The value is a multi-pointer, did you mean '%s[0]'?\n", str);
- o->type = type_deref(o->type, true);
- }
- }
- break;
- default:
- error(op, "Unknown operator '%.*s'", LIT(op.string));
- return false;
- }
- return true;
- }
- gb_internal bool check_binary_op(CheckerContext *c, Operand *o, Token op) {
- Type *main_type = o->type;
- Type *type = base_type(core_array_type(main_type));
- Type *ct = core_type(type);
- switch (op.kind) {
- case Token_Sub:
- case Token_SubEq:
- if (is_type_bit_set(type)) {
- return true;
- } else if (!is_type_numeric(type)) {
- error(op, "Operator '%.*s' is only allowed with numeric expressions", LIT(op.string));
- return false;
- }
- break;
- case Token_Quo:
- case Token_QuoEq:
- if (is_type_matrix(main_type)) {
- error(op, "Operator '%.*s' is not allowed with matrix types", LIT(op.string));
- return false;
- } else if (is_type_simd_vector(main_type) && is_type_integer(type)) {
- error(op, "Operator '%.*s' is not allowed with #simd types with integer elements", LIT(op.string));
- return false;
- }
- /*fallthrough*/
- case Token_Mul:
- case Token_MulEq:
- case Token_AddEq:
- if (is_type_bit_set(type)) {
- return true;
- } else if (!is_type_numeric(type)) {
- error(op, "Operator '%.*s' is only allowed with numeric expressions", LIT(op.string));
- return false;
- }
- break;
- case Token_Add:
- if (is_type_string(type)) {
- if (o->mode == Addressing_Constant) {
- return true;
- }
- error(op, "String concatenation is only allowed with constant strings");
- return false;
- } else if (is_type_bit_set(type)) {
- return true;
- } else if (!is_type_numeric(type)) {
- error(op, "Operator '%.*s' is only allowed with numeric expressions", LIT(op.string));
- return false;
- }
- break;
- case Token_And:
- case Token_Or:
- case Token_AndEq:
- case Token_OrEq:
- case Token_Xor:
- case Token_XorEq:
- if (!is_type_integer(ct) && !is_type_boolean(ct) && !is_type_bit_set(ct)) {
- error(op, "Operator '%.*s' is only allowed with integers, booleans, or bit sets", LIT(op.string));
- return false;
- }
- break;
- case Token_Mod:
- case Token_ModMod:
- case Token_ModEq:
- case Token_ModModEq:
- if (is_type_matrix(main_type)) {
- error(op, "Operator '%.*s' is not allowed with matrix types", LIT(op.string));
- return false;
- }
- if (!is_type_integer(type)) {
- error(op, "Operator '%.*s' is only allowed with integers", LIT(op.string));
- return false;
- } else if (is_type_simd_vector(main_type)) {
- error(op, "Operator '%.*s' is not allowed with #simd types with integer elements", LIT(op.string));
- return false;
- }
- break;
- case Token_AndNot:
- case Token_AndNotEq:
- if (!is_type_integer(ct) && !is_type_bit_set(ct)) {
- error(op, "Operator '%.*s' is only allowed with integers and bit sets", LIT(op.string));
- return false;
- }
- break;
- case Token_CmpAnd:
- case Token_CmpOr:
- case Token_CmpAndEq:
- case Token_CmpOrEq:
- if (!is_type_boolean(type)) {
- error(op, "Operator '%.*s' is only allowed with boolean expressions", LIT(op.string));
- return false;
- }
- break;
- default:
- error(op, "Unknown operator '%.*s'", LIT(op.string));
- return false;
- }
- return true;
- }
- gb_internal bool check_representable_as_constant(CheckerContext *c, ExactValue in_value, Type *type, ExactValue *out_value) {
- if (in_value.kind == ExactValue_Invalid) {
- // NOTE(bill): There's already been an error
- return true;
- }
- type = core_type(type);
- if (type == t_invalid) {
- return false;
- } else if (is_type_boolean(type)) {
- return in_value.kind == ExactValue_Bool;
- } else if (is_type_string(type)) {
- return in_value.kind == ExactValue_String;
- } else if (is_type_integer(type) || is_type_rune(type)) {
- if (in_value.kind == ExactValue_Bool) {
- return false;
- }
- ExactValue v = exact_value_to_integer(in_value);
- if (v.kind != ExactValue_Integer) {
- return false;
- }
- if (out_value) *out_value = v;
- if (is_type_untyped(type)) {
- return true;
- }
- BigInt i = v.value_integer;
- i64 byte_size = type_size_of(type);
- BigInt umax = {};
- BigInt imin = {};
- BigInt imax = {};
- if (c->bit_field_bit_size > 0) {
- i64 bit_size = gb_min(cast(i64)(8*byte_size), cast(i64)c->bit_field_bit_size);
- big_int_from_u64(&umax, 1);
- big_int_from_i64(&imin, 1);
- big_int_from_i64(&imax, 1);
- BigInt bu = {};
- BigInt bi = {};
- big_int_from_i64(&bu, bit_size);
- big_int_from_i64(&bi, bit_size-1);
- big_int_shl_eq(&umax, &bu);
- mp_decr(&umax);
- big_int_shl_eq(&imin, &bi);
- big_int_neg(&imin, &imin);
- big_int_shl_eq(&imax, &bi);
- mp_decr(&imax);
- } else {
- if (byte_size < 16) {
- big_int_from_u64(&umax, unsigned_integer_maxs[byte_size]);
- big_int_from_i64(&imin, signed_integer_mins[byte_size]);
- big_int_from_i64(&imax, signed_integer_maxs[byte_size]);
- } else {
- big_int_from_u64(&umax, 1);
- big_int_from_i64(&imin, 1);
- big_int_from_i64(&imax, 1);
- BigInt bi128 = {};
- BigInt bi127 = {};
- big_int_from_i64(&bi128, 128);
- big_int_from_i64(&bi127, 127);
- big_int_shl_eq(&umax, &bi128);
- mp_decr(&umax);
- big_int_shl_eq(&imin, &bi127);
- big_int_neg(&imin, &imin);
- big_int_shl_eq(&imax, &bi127);
- mp_decr(&imax);
- }
- }
- switch (type->Basic.kind) {
- case Basic_rune:
- case Basic_i8:
- case Basic_i16:
- case Basic_i32:
- case Basic_i64:
- case Basic_i128:
- case Basic_int:
- case Basic_i16le:
- case Basic_i32le:
- case Basic_i64le:
- case Basic_i128le:
- case Basic_i16be:
- case Basic_i32be:
- case Basic_i64be:
- case Basic_i128be:
- {
- // return imin <= i && i <= imax;
- int a = big_int_cmp(&imin, &i);
- int b = big_int_cmp(&i, &imax);
- return (a <= 0) && (b <= 0);
- }
- case Basic_u8:
- case Basic_u16:
- case Basic_u32:
- case Basic_u64:
- case Basic_u128:
- case Basic_uint:
- case Basic_uintptr:
- case Basic_u16le:
- case Basic_u32le:
- case Basic_u64le:
- case Basic_u128le:
- case Basic_u16be:
- case Basic_u32be:
- case Basic_u64be:
- case Basic_u128be:
- {
- // return 0ull <= i && i <= umax;
- int b = big_int_cmp(&i, &umax);
- return !i.sign && (b <= 0);
- }
- case Basic_UntypedInteger:
- return true;
- default: GB_PANIC("Compiler error: Unknown integer type!"); break;
- }
- } else if (is_type_float(type)) {
- ExactValue v = exact_value_to_float(in_value);
- if (v.kind != ExactValue_Float) {
- return false;
- }
- if (out_value) *out_value = v;
- switch (type->Basic.kind) {
- case Basic_f16:
- case Basic_f32:
- case Basic_f64:
- return true;
- case Basic_f16le:
- case Basic_f16be:
- case Basic_f32le:
- case Basic_f32be:
- case Basic_f64le:
- case Basic_f64be:
- return true;
- case Basic_UntypedFloat:
- return true;
- default: GB_PANIC("Compiler error: Unknown float type!"); break;
- }
- } else if (is_type_complex(type)) {
- ExactValue v = exact_value_to_complex(in_value);
- if (v.kind != ExactValue_Complex) {
- return false;
- }
- switch (type->Basic.kind) {
- case Basic_complex32:
- case Basic_complex64:
- case Basic_complex128: {
- ExactValue real = exact_value_real(v);
- ExactValue imag = exact_value_imag(v);
- if (real.kind != ExactValue_Invalid &&
- imag.kind != ExactValue_Invalid) {
- if (out_value) *out_value = exact_value_complex(exact_value_to_f64(real), exact_value_to_f64(imag));
- return true;
- }
- break;
- }
- case Basic_UntypedComplex:
- return true;
- default: GB_PANIC("Compiler error: Unknown complex type!"); break;
- }
- return false;
- } else if (is_type_quaternion(type)) {
- ExactValue v = exact_value_to_quaternion(in_value);
- if (v.kind != ExactValue_Quaternion) {
- return false;
- }
- switch (type->Basic.kind) {
- case Basic_quaternion64:
- case Basic_quaternion128:
- case Basic_quaternion256: {
- ExactValue real = exact_value_real(v);
- ExactValue imag = exact_value_imag(v);
- ExactValue jmag = exact_value_jmag(v);
- ExactValue kmag = exact_value_kmag(v);
- if (real.kind != ExactValue_Invalid &&
- imag.kind != ExactValue_Invalid) {
- if (out_value) *out_value = exact_value_quaternion(exact_value_to_f64(real), exact_value_to_f64(imag), exact_value_to_f64(jmag), exact_value_to_f64(kmag));
- return true;
- }
- break;
- }
- case Basic_UntypedComplex:
- if (out_value) *out_value = exact_value_to_quaternion(*out_value);
- return true;
- case Basic_UntypedQuaternion:
- return true;
- default: GB_PANIC("Compiler error: Unknown complex type!"); break;
- }
- return false;
- } else if (is_type_pointer(type)) {
- if (in_value.kind == ExactValue_Pointer) {
- return true;
- }
- if (in_value.kind == ExactValue_Integer) {
- return false;
- // return true;
- }
- if (in_value.kind == ExactValue_String) {
- return false;
- }
- if (out_value) *out_value = in_value;
- } else if (is_type_bit_set(type)) {
- if (in_value.kind == ExactValue_Integer) {
- return true;
- }
- }
- return false;
- }
- gb_internal bool check_integer_exceed_suggestion(CheckerContext *c, Operand *o, Type *type, i64 max_bit_size=0) {
- if (is_type_integer(type) && o->value.kind == ExactValue_Integer) {
- gbString b = type_to_string(type);
- defer (gb_string_free(b));
- if (is_type_enum(o->type)) {
- if (check_is_castable_to(c, o, type)) {
- gbString ot = type_to_string(o->type);
- error_line("\tSuggestion: Try casting the '%s' expression to '%s'", ot, b);
- gb_string_free(ot);
- }
- return true;
- }
- i64 sz = type_size_of(type);
- i64 bit_size = 8*sz;
- bool size_changed = false;
- if (max_bit_size > 0) {
- size_changed = (bit_size != max_bit_size);
- bit_size = gb_min(bit_size, max_bit_size);
- }
- BigInt *bi = &o->value.value_integer;
- if (is_type_unsigned(type)) {
- BigInt one = big_int_make_u64(1);
- BigInt max_size = big_int_make_u64(1);
- BigInt bits = big_int_make_i64(bit_size);
- big_int_shl_eq(&max_size, &bits);
- big_int_sub_eq(&max_size, &one);
- if (big_int_is_neg(bi)) {
- error_line("\tA negative value cannot be represented by the unsigned integer type '%s'\n", b);
- BigInt dst = {};
- big_int_neg(&dst, bi);
- if (big_int_cmp(&dst, &max_size) < 0) {
- big_int_sub_eq(&dst, &one);
- String dst_str = big_int_to_string(temporary_allocator(), &dst);
- gbString t = type_to_string(type);
- error_line("\tSuggestion: ~%s(%.*s)\n", t, LIT(dst_str));
- gb_string_free(t);
- }
- } else {
- String max_size_str = big_int_to_string(temporary_allocator(), &max_size);
- if (size_changed) {
- error_line("\tThe maximum value that can be represented with that bit_field's field of '%s | %u' is '%.*s'\n", b, bit_size, LIT(max_size_str));
- } else {
- error_line("\tThe maximum value that can be represented by '%s' is '%.*s'\n", b, LIT(max_size_str));
- }
- }
- } else {
- BigInt zero = big_int_make_u64(0);
- BigInt one = big_int_make_u64(1);
- BigInt max_size = big_int_make_u64(1);
- BigInt bits = big_int_make_i64(bit_size - 1);
- big_int_shl_eq(&max_size, &bits);
- String max_size_str = {};
- if (big_int_is_neg(bi)) {
- big_int_neg(&max_size, &max_size);
- max_size_str = big_int_to_string(temporary_allocator(), &max_size);
- } else {
- big_int_sub_eq(&max_size, &one);
- max_size_str = big_int_to_string(temporary_allocator(), &max_size);
- }
- if (size_changed) {
- error_line("\tThe maximum value that can be represented with that bit_field's field of '%s | %u' is '%.*s'\n", b, bit_size, LIT(max_size_str));
- } else {
- error_line("\tThe maximum value that can be represented by '%s' is '%.*s'\n", b, LIT(max_size_str));
- }
- }
- return true;
- }
- return false;
- }
- gb_internal void check_assignment_error_suggestion(CheckerContext *c, Operand *o, Type *type, i64 max_bit_size) {
- gbString a = expr_to_string(o->expr);
- gbString b = type_to_string(type);
- defer(
- gb_string_free(b);
- gb_string_free(a);
- );
- Type *src = base_type(o->type);
- Type *dst = base_type(type);
- if (is_type_array(src) && is_type_slice(dst)) {
- Type *s = src->Array.elem;
- Type *d = dst->Slice.elem;
- if (are_types_identical(s, d)) {
- error_line("\tSuggestion: The array expression may be sliced with %s[:]\n", a);
- }
- } else if (is_type_dynamic_array(src) && is_type_slice(dst)) {
- Type *s = src->DynamicArray.elem;
- Type *d = dst->Slice.elem;
- if (are_types_identical(s, d)) {
- error_line("\tSuggestion: The dynamic array expression may be sliced with %s[:]\n", a);
- }
- }else if (are_types_identical(src, dst) && !are_types_identical(o->type, type)) {
- error_line("\tSuggestion: The expression may be directly casted to type %s\n", b);
- } else if (are_types_identical(src, t_string) && is_type_u8_slice(dst)) {
- error_line("\tSuggestion: A string may be transmuted to %s\n", b);
- error_line("\t This is an UNSAFE operation as string data is assumed to be immutable,\n");
- error_line("\t whereas slices in general are assumed to be mutable.\n");
- } else if (is_type_u8_slice(src) && are_types_identical(dst, t_string) && o->mode != Addressing_Constant) {
- error_line("\tSuggestion: The expression may be casted to %s\n", b);
- } else if (check_integer_exceed_suggestion(c, o, type, max_bit_size)) {
- return;
- } else if (is_expr_inferred_fixed_array(c->type_hint_expr) && is_type_array_like(type) && is_type_array_like(o->type)) {
- gbString s = expr_to_string(c->type_hint_expr);
- error_line("\tSuggestion: Make sure that `%s` is attached to the compound literal directly\n", s);
- gb_string_free(s);
- } else if (is_type_pointer(type) &&
- o->mode == Addressing_Variable &&
- are_types_identical(type_deref(type), o->type)) {
- gbString s = expr_to_string(o->expr);
- error_line("\tSuggestion: Did you mean `&%s`\n", s);
- gb_string_free(s);
- } else if (is_type_pointer(o->type) &&
- are_types_identical(type_deref(o->type), type)) {
- gbString s = expr_to_string(o->expr);
- error_line("\tSuggestion: Did you mean `%s^`\n", s);
- gb_string_free(s);
- }
- }
- gb_internal void check_cast_error_suggestion(CheckerContext *c, Operand *o, Type *type) {
- gbString a = expr_to_string(o->expr);
- gbString b = type_to_string(type);
- defer(
- gb_string_free(b);
- gb_string_free(a);
- );
- Type *src = base_type(o->type);
- Type *dst = base_type(type);
- if (is_type_array(src) && is_type_slice(dst)) {
- Type *s = src->Array.elem;
- Type *d = dst->Slice.elem;
- if (are_types_identical(s, d)) {
- error_line("\tSuggestion: the array expression may be sliced with %s[:]\n", a);
- }
- } else if (is_type_pointer(o->type) && is_type_integer(type)) {
- if (is_type_uintptr(type)) {
- error_line("\tSuggestion: a pointer may be directly casted to %s\n", b);
- } else {
- error_line("\tSuggestion: for a pointer to be casted to an integer, it must be converted to 'uintptr' first\n");
- i64 x = type_size_of(o->type);
- i64 y = type_size_of(type);
- if (x != y) {
- error_line("\tNote: the type of expression and the type of the cast have a different size in bytes, %lld vs %lld\n", x, y);
- }
- }
- } else if (is_type_integer(o->type) && is_type_pointer(type)) {
- if (is_type_uintptr(o->type)) {
- error_line("\tSuggestion: %a may be directly casted to %s\n", a, b);
- } else {
- error_line("\tSuggestion: for an integer to be casted to a pointer, it must be converted to 'uintptr' first\n");
- }
- } else if (are_types_identical(src, t_string) && is_type_u8_slice(dst)) {
- error_line("\tSuggestion: a string may be transmuted to %s\n", b);
- } else if (check_integer_exceed_suggestion(c, o, type)) {
- return;
- }
- }
- gb_internal bool check_is_expressible(CheckerContext *ctx, Operand *o, Type *type) {
- GB_ASSERT(o->mode == Addressing_Constant);
- ExactValue out_value = o->value;
- if (is_type_constant_type(type) && check_representable_as_constant(ctx, o->value, type, &out_value)) {
- o->value = out_value;
- return true;
- } else {
- o->value = out_value;
- gbString a = expr_to_string(o->expr);
- gbString b = type_to_string(type);
- gbString c = type_to_string(o->type);
- gbString s = exact_value_to_string(o->value);
- defer(
- gb_string_free(s);
- gb_string_free(c);
- gb_string_free(b);
- gb_string_free(a);
- o->mode = Addressing_Invalid;
- );
- ERROR_BLOCK();
- if (is_type_numeric(o->type) && is_type_numeric(type)) {
- if (!is_type_integer(o->type) && is_type_integer(type)) {
- error(o->expr, "'%s' truncated to '%s', got %s", a, b, s);
- } else {
- i64 max_bit_size = 0;
- if (ctx->bit_field_bit_size) {
- max_bit_size = ctx->bit_field_bit_size;
- }
- if (are_types_identical(o->type, type)) {
- error(o->expr, "Numeric value '%s' from '%s' cannot be represented by '%s'", s, a, b);
- } else {
- error(o->expr, "Cannot convert numeric value '%s' from '%s' to '%s' from '%s'", s, a, b, c);
- }
- check_assignment_error_suggestion(ctx, o, type, max_bit_size);
- }
- } else {
- error(o->expr, "Cannot convert '%s' to '%s' from '%s', got %s", a, b, c, s);
- check_assignment_error_suggestion(ctx, o, type);
- }
- return false;
- }
- }
- gb_internal bool check_is_not_addressable(CheckerContext *c, Operand *o) {
- if (o->expr && o->expr->kind == Ast_SelectorExpr) {
- if (o->expr->SelectorExpr.is_bit_field) {
- return true;
- }
- }
- if (o->mode == Addressing_OptionalOk) {
- Ast *expr = unselector_expr(o->expr);
- if (expr->kind != Ast_TypeAssertion) {
- return true;
- }
- ast_node(ta, TypeAssertion, expr);
- TypeAndValue tv = ta->expr->tav;
- if (is_type_pointer(tv.type)) {
- return false;
- }
- if (is_type_union(tv.type) && tv.mode == Addressing_Variable) {
- return false;
- }
- if (is_type_any(tv.type)) {
- return false;
- }
- return true;
- }
- if (o->mode == Addressing_MapIndex) {
- return false;
- }
- Ast *expr = unparen_expr(o->expr);
- if (expr->kind == Ast_CompoundLit) {
- return false;
- }
- return o->mode != Addressing_Variable && o->mode != Addressing_SoaVariable;
- }
- gb_internal ExactValue exact_bit_set_all_set_mask(Type *type) {
- type = base_type(type);
- GB_ASSERT(type->kind == Type_BitSet);
- i64 lower = type->BitSet.lower;
- i64 upper = type->BitSet.upper;
- Type *elem = type->BitSet.elem;
- Type *underlying = type->BitSet.underlying;
- bool is_backed = underlying != nullptr;
- gb_unused(is_backed);
- BigInt b_lower = {};
- BigInt b_upper = {};
- big_int_from_i64(&b_lower, lower);
- big_int_from_i64(&b_upper, upper);
- BigInt one = {};
- big_int_from_u64(&one, 1);
- BigInt mask = {};
- if (elem == nullptr) {
- big_int_from_i64(&mask, -1);
- } else if (is_type_enum(elem)) {
- Type *e = base_type(elem);
- GB_ASSERT(e->kind == Type_Enum);
- gb_unused(e);
- if ((big_int_cmp(&e->Enum.min_value->value_integer, &b_lower) == 0 || is_backed) &&
- big_int_cmp(&e->Enum.max_value->value_integer, &b_upper) == 0) {
- i64 lower_base = is_backed ? gb_min(0, lower) : lower;
- BigInt b_lower_base = {};
- big_int_from_i64(&b_lower_base, lower_base);
- for (Entity *f : e->Enum.fields) {
- if (f->kind != Entity_Constant) {
- continue;
- }
- if (f->Constant.value.kind != ExactValue_Integer) {
- continue;
- }
- BigInt shift_amount = {};
- big_int_sub(&shift_amount, &f->Constant.value.value_integer, &b_lower_base);
- BigInt value = {};
- big_int_shl(&value, &one, &shift_amount);
- big_int_or(&mask, &mask, &value);
- }
- } else {
- // TODO(bill): enum range based");
- big_int_from_i64(&mask, -1);
- }
- } else {
- i64 lower_base = lower;
- for (i64 x = lower; x <= upper; x++) {
- BigInt shift_amount = {};
- big_int_from_i64(&shift_amount, x - lower_base);
- BigInt value = {};
- big_int_shl(&value, &one, &shift_amount);
- big_int_or(&mask, &mask, &value);
- }
- }
- ExactValue res = {};
- res.kind = ExactValue_Integer;
- res.value_integer = mask;
- return res;
- }
- gb_internal void check_unary_expr(CheckerContext *c, Operand *o, Token op, Ast *node) {
- switch (op.kind) {
- case Token_And: { // Pointer address
- if (check_is_not_addressable(c, o)) {
- if (ast_node_expect(node, Ast_UnaryExpr)) {
- ast_node(ue, UnaryExpr, node);
- gbString str = expr_to_string(ue->expr);
- defer (gb_string_free(str));
- Entity *e = entity_of_node(ue->expr);
- if (e != nullptr && (e->flags & EntityFlag_Param) != 0) {
- error(op, "Cannot take the pointer address of '%s' which is a procedure parameter", str);
- } else if (e != nullptr && (e->flags & EntityFlag_BitFieldField) != 0) {
- error(op, "Cannot take the pointer address of '%s' which is a bit_field's field", str);
- } else {
- switch (o->mode) {
- case Addressing_Constant:
- error(op, "Cannot take the pointer address of '%s' which is a constant", str);
- break;
- case Addressing_SwizzleValue:
- case Addressing_SwizzleVariable:
- error(op, "Cannot take the pointer address of '%s' which is a swizzle intermediate array value", str);
- break;
- default:
- {
- ERROR_BLOCK();
- error(op, "Cannot take the pointer address of '%s'", str);
- if (e == nullptr) {
- break;
- }
- if ((e->flags & EntityFlag_ForValue) != 0) {
- Type *parent_type = type_deref(e->Variable.for_loop_parent_type);
- if (parent_type != nullptr && is_type_string(parent_type)) {
- error_line("\tSuggestion: Iterating over a string produces an intermediate 'rune' value which cannot be addressed.\n");
- } else if (parent_type != nullptr && is_type_tuple(parent_type)) {
- error_line("\tSuggestion: Iterating over a procedure does not produce values which are addressable.\n");
- } else {
- error_line("\tSuggestion: Did you want to pass the iterable value to the for statement by pointer to get addressable semantics?\n");
- }
- if (parent_type != nullptr && is_type_map(parent_type)) {
- error_line("\t Prefer doing 'for key, &%.*s in ...'\n", LIT(e->token.string));
- } else {
- error_line("\t Prefer doing 'for &%.*s in ...'\n", LIT(e->token.string));
- }
- }
- if ((e->flags & EntityFlag_SwitchValue) != 0) {
- error_line("\tSuggestion: Did you want to pass the value to the switch statement by pointer to get addressable semantics?\n");
- error_line("\t Prefer doing 'switch &%.*s in ...'\n", LIT(e->token.string));
- }
- }
- break;
- }
- }
- }
- o->mode = Addressing_Invalid;
- return;
- }
- if (o->mode == Addressing_SoaVariable) {
- ast_node(ue, UnaryExpr, node);
- if (ast_node_expect(ue->expr, Ast_IndexExpr)) {
- ast_node(ie, IndexExpr, ue->expr);
- Type *soa_type = type_deref(type_of_expr(ie->expr));
- GB_ASSERT(is_type_soa_struct(soa_type));
- o->type = alloc_type_soa_pointer(soa_type);
- } else {
- o->type = alloc_type_pointer(o->type);
- }
- } else {
- o->type = alloc_type_pointer(o->type);
- }
- switch (o->mode) {
- case Addressing_OptionalOk:
- case Addressing_MapIndex:
- o->mode = Addressing_OptionalOkPtr;
- break;
- default:
- o->mode = Addressing_Value;
- break;
- }
- return;
- }
- }
- if (!check_unary_op(c, o, op)) {
- o->mode = Addressing_Invalid;
- return;
- }
- if (o->mode == Addressing_Constant) {
- Type *type = base_type(o->type);
- if (!is_type_constant_type(o->type)) {
- if (is_type_array_like(o->type)) {
- o->mode = Addressing_Value;
- return;
- }
- gbString xt = type_to_string(o->type);
- gbString err_str = expr_to_string(node);
- error(op, "Invalid type, '%s', for constant unary expression '%s'", xt, err_str);
- gb_string_free(err_str);
- gb_string_free(xt);
- o->mode = Addressing_Invalid;
- return;
- }
- if (op.kind == Token_Xor && is_type_untyped(type)) {
- gbString err_str = expr_to_string(node);
- error(op, "Bitwise not cannot be applied to untyped constants '%s'", err_str);
- gb_string_free(err_str);
- o->mode = Addressing_Invalid;
- return;
- }
- if (op.kind == Token_Sub && is_type_unsigned(type)) {
- gbString err_str = expr_to_string(node);
- error(op, "A unsigned constant cannot be negated '%s'", err_str);
- gb_string_free(err_str);
- o->mode = Addressing_Invalid;
- return;
- }
- i32 precision = 0;
- if (is_type_typed(type)) {
- precision = cast(i32)(8 * type_size_of(type));
- }
- bool is_unsigned = is_type_unsigned(type);
- if (is_type_rune(type)) {
- GB_ASSERT(!is_unsigned);
- }
- o->value = exact_unary_operator_value(op.kind, o->value, precision, is_unsigned);
- if (op.kind == Token_Xor && is_type_bit_set(type)) {
- ExactValue mask = exact_bit_set_all_set_mask(type);
- o->value = exact_binary_operator_value(Token_And, o->value, mask);
- }
- if (is_type_typed(type)) {
- if (node != nullptr) {
- o->expr = node;
- }
- check_is_expressible(c, o, type);
- }
- return;
- }
- o->mode = Addressing_Value;
- }
- gb_internal void add_comparison_procedures_for_fields(CheckerContext *c, Type *t) {
- if (t == nullptr) {
- return;
- }
- t = base_type(t);
- if (!is_type_comparable(t)) {
- return;
- }
- switch (t->kind) {
- case Type_Basic:
- switch (t->Basic.kind) {
- case Basic_complex32:
- add_package_dependency(c, "runtime", "complex32_eq");
- add_package_dependency(c, "runtime", "complex32_ne");
- break;
- case Basic_complex64:
- add_package_dependency(c, "runtime", "complex64_eq");
- add_package_dependency(c, "runtime", "complex64_ne");
- break;
- case Basic_complex128:
- add_package_dependency(c, "runtime", "complex128_eq");
- add_package_dependency(c, "runtime", "complex128_ne");
- break;
- case Basic_quaternion64:
- add_package_dependency(c, "runtime", "quaternion64_eq");
- add_package_dependency(c, "runtime", "quaternion64_ne");
- break;
- case Basic_quaternion128:
- add_package_dependency(c, "runtime", "quaternion128_eq");
- add_package_dependency(c, "runtime", "quaternion128_ne");
- break;
- case Basic_quaternion256:
- add_package_dependency(c, "runtime", "quaternion256_eq");
- add_package_dependency(c, "runtime", "quaternion256_ne");
- break;
- case Basic_cstring:
- add_package_dependency(c, "runtime", "cstring_eq");
- add_package_dependency(c, "runtime", "cstring_ne");
- break;
- case Basic_string:
- add_package_dependency(c, "runtime", "string_eq");
- add_package_dependency(c, "runtime", "string_ne");
- break;
- }
- break;
- case Type_Struct:
- for (Entity *field : t->Struct.fields) {
- add_comparison_procedures_for_fields(c, field->type);
- }
- break;
- }
- }
- gb_internal void check_comparison(CheckerContext *c, Ast *node, Operand *x, Operand *y, TokenKind op) {
- if (x->mode == Addressing_Type && y->mode == Addressing_Type) {
- bool comp = are_types_identical(x->type, y->type);
- switch (op) {
- case Token_CmpEq: /* comp = comp; */ break;
- case Token_NotEq: comp = !comp; break;
- }
- x->mode = Addressing_Constant;
- x->type = t_untyped_bool;
- x->value = exact_value_bool(comp);
- return;
- }
- if (x->mode == Addressing_Type && is_type_typeid(y->type)) {
- add_type_info_type(c, x->type);
- add_type_info_type(c, y->type);
- add_type_and_value(c, x->expr, Addressing_Value, y->type, exact_value_typeid(x->type));
- x->mode = Addressing_Value;
- x->type = t_untyped_bool;
- return;
- } else if (is_type_typeid(x->type) && y->mode == Addressing_Type) {
- add_type_info_type(c, x->type);
- add_type_info_type(c, y->type);
- add_type_and_value(c, y->expr, Addressing_Value, x->type, exact_value_typeid(y->type));
- x->mode = Addressing_Value;
- x->type = t_untyped_bool;
- return;
- }
- TEMPORARY_ALLOCATOR_GUARD();
- gbString err_str = nullptr;
- if (check_is_assignable_to(c, x, y->type) ||
- check_is_assignable_to(c, y, x->type)) {
- Type *err_type = x->type;
- bool defined = false;
- switch (op) {
- case Token_CmpEq:
- case Token_NotEq:
- defined = (is_type_comparable(x->type) && is_type_comparable(y->type)) ||
- (is_operand_nil(*x) && type_has_nil(y->type)) ||
- (is_operand_nil(*y) && type_has_nil(x->type));
- break;
- case Token_Lt:
- case Token_Gt:
- case Token_LtEq:
- case Token_GtEq:
- if (are_types_identical(x->type, y->type) && is_type_bit_set(x->type)) {
- defined = true;
- } else {
- defined = is_type_ordered(x->type) && is_type_ordered(y->type);
- }
- break;
- }
- if (!defined) {
- gbString xs = type_to_string(x->type, temporary_allocator());
- gbString ys = type_to_string(y->type, temporary_allocator());
- if (!is_type_comparable(x->type)) {
- err_str = gb_string_make(temporary_allocator(),
- gb_bprintf("Type '%s' is not simply comparable, so operator '%.*s' is not defined for it", xs, LIT(token_strings[op]))
- );
- } else if (!is_type_comparable(y->type)) {
- err_str = gb_string_make(temporary_allocator(),
- gb_bprintf("Type '%s' is not simply comparable, so operator '%.*s' is not defined for it", ys, LIT(token_strings[op]))
- );
- } else {
- err_str = gb_string_make(temporary_allocator(),
- gb_bprintf("Operator '%.*s' not defined between the types '%s' and '%s'", LIT(token_strings[op]), xs, ys)
- );
- }
- } else {
- Type *comparison_type = x->type;
- if (x->type == err_type && is_operand_nil(*x)) {
- comparison_type = y->type;
- }
- add_comparison_procedures_for_fields(c, comparison_type);
- }
- } else {
- gbString xt, yt;
- if (x->mode == Addressing_ProcGroup) {
- xt = gb_string_make(temporary_allocator(), "procedure group");
- } else {
- xt = type_to_string(x->type);
- }
- if (y->mode == Addressing_ProcGroup) {
- yt = gb_string_make(temporary_allocator(), "procedure group");
- } else {
- yt = type_to_string(y->type);
- }
- err_str = gb_string_make(temporary_allocator(), gb_bprintf("Mismatched types '%s' and '%s'", xt, yt));
- }
- if (err_str != nullptr) {
- error(node, "Cannot compare expression. %s.", err_str);
- x->type = t_untyped_bool;
- } else {
- if (x->mode == Addressing_Constant &&
- y->mode == Addressing_Constant) {
- if (is_type_constant_type(x->type)) {
- if (is_type_bit_set(x->type)) {
- switch (op) {
- case Token_CmpEq:
- case Token_NotEq:
- x->value = exact_value_bool(compare_exact_values(op, x->value, y->value));
- break;
- case Token_Lt:
- case Token_LtEq:
- {
- ExactValue lhs = x->value;
- ExactValue rhs = y->value;
- ExactValue res = exact_binary_operator_value(Token_And, lhs, rhs);
- res = exact_value_bool(compare_exact_values(op, res, lhs));
- if (op == Token_Lt) {
- res = exact_binary_operator_value(Token_And, res, exact_value_bool(compare_exact_values(op, lhs, rhs)));
- }
- x->value = res;
- break;
- }
- case Token_Gt:
- case Token_GtEq:
- {
- ExactValue lhs = x->value;
- ExactValue rhs = y->value;
- ExactValue res = exact_binary_operator_value(Token_And, lhs, rhs);
- res = exact_value_bool(compare_exact_values(op, res, rhs));
- if (op == Token_Gt) {
- res = exact_binary_operator_value(Token_And, res, exact_value_bool(compare_exact_values(op, lhs, rhs)));
- }
- x->value = res;
- break;
- }
- }
- } else {
- x->value = exact_value_bool(compare_exact_values(op, x->value, y->value));
- }
- } else {
- x->mode = Addressing_Value;
- }
- } else {
- x->mode = Addressing_Value;
- update_untyped_expr_type(c, x->expr, default_type(x->type), true);
- update_untyped_expr_type(c, y->expr, default_type(y->type), true);
- i64 size = 0;
- if (!is_type_untyped(x->type)) size = gb_max(size, type_size_of(x->type));
- if (!is_type_untyped(y->type)) size = gb_max(size, type_size_of(y->type));
- if (is_type_cstring(x->type) && is_type_cstring(y->type)) {
- switch (op) {
- case Token_CmpEq: add_package_dependency(c, "runtime", "cstring_eq"); break;
- case Token_NotEq: add_package_dependency(c, "runtime", "cstring_ne"); break;
- case Token_Lt: add_package_dependency(c, "runtime", "cstring_lt"); break;
- case Token_Gt: add_package_dependency(c, "runtime", "cstring_gt"); break;
- case Token_LtEq: add_package_dependency(c, "runtime", "cstring_le"); break;
- case Token_GtEq: add_package_dependency(c, "runtime", "cstring_gt"); break;
- }
- } else if (is_type_string(x->type) || is_type_string(y->type)) {
- switch (op) {
- case Token_CmpEq: add_package_dependency(c, "runtime", "string_eq"); break;
- case Token_NotEq: add_package_dependency(c, "runtime", "string_ne"); break;
- case Token_Lt: add_package_dependency(c, "runtime", "string_lt"); break;
- case Token_Gt: add_package_dependency(c, "runtime", "string_gt"); break;
- case Token_LtEq: add_package_dependency(c, "runtime", "string_le"); break;
- case Token_GtEq: add_package_dependency(c, "runtime", "string_gt"); break;
- }
- } else if (is_type_complex(x->type) || is_type_complex(y->type)) {
- switch (op) {
- case Token_CmpEq:
- switch (8*size) {
- case 64: add_package_dependency(c, "runtime", "complex64_eq"); break;
- case 128: add_package_dependency(c, "runtime", "complex128_eq"); break;
- }
- break;
- case Token_NotEq:
- switch (8*size) {
- case 64: add_package_dependency(c, "runtime", "complex64_ne"); break;
- case 128: add_package_dependency(c, "runtime", "complex128_ne"); break;
- }
- break;
- }
- } else if (is_type_quaternion(x->type) || is_type_quaternion(y->type)) {
- switch (op) {
- case Token_CmpEq:
- switch (8*size) {
- case 128: add_package_dependency(c, "runtime", "quaternion128_eq"); break;
- case 256: add_package_dependency(c, "runtime", "quaternion256_eq"); break;
- }
- break;
- case Token_NotEq:
- switch (8*size) {
- case 128: add_package_dependency(c, "runtime", "quaternion128_ne"); break;
- case 256: add_package_dependency(c, "runtime", "quaternion256_ne"); break;
- }
- break;
- }
- }
- }
- x->type = t_untyped_bool;
- }
- }
- gb_internal void check_shift(CheckerContext *c, Operand *x, Operand *y, Ast *node, Type *type_hint) {
- GB_ASSERT(node->kind == Ast_BinaryExpr);
- ast_node(be, BinaryExpr, node);
- bool y_is_untyped = is_type_untyped(y->type);
- if (y_is_untyped) {
- convert_to_typed(c, y, t_untyped_integer);
- if (y->mode == Addressing_Invalid) {
- x->mode = Addressing_Invalid;
- return;
- }
- } else if (!is_type_unsigned(y->type)) {
- gbString y_str = expr_to_string(y->expr);
- error(y->expr, "Shift amount '%s' must be an unsigned integer", y_str);
- gb_string_free(y_str);
- x->mode = Addressing_Invalid;
- return;
- }
- bool x_is_untyped = is_type_untyped(x->type);
- if (!(x_is_untyped || is_type_integer(x->type))) {
- gbString x_str = expr_to_string(x->expr);
- error(x->expr, "Shifted operand '%s' must be an integer", x_str);
- gb_string_free(x_str);
- x->mode = Addressing_Invalid;
- return;
- }
- if (y->mode == Addressing_Constant) {
- if (big_int_is_neg(&y->value.value_integer)) {
- gbString y_str = expr_to_string(y->expr);
- error(y->expr, "Shift amount '%s' cannot be negative", y_str);
- gb_string_free(y_str);
- x->mode = Addressing_Invalid;
- return;
- }
- BigInt max_shift = {};
- big_int_from_u64(&max_shift, MAX_BIG_INT_SHIFT);
- if (big_int_cmp(&y->value.value_integer, &max_shift) > 0) {
- gbString y_str = expr_to_string(y->expr);
- error(y->expr, "Shift amount '%s' must be <= %u", y_str, MAX_BIG_INT_SHIFT);
- gb_string_free(y_str);
- x->mode = Addressing_Invalid;
- return;
- }
- if (x->mode == Addressing_Constant) {
- if (x_is_untyped) {
- convert_to_typed(c, x, t_untyped_integer);
- if (x->mode == Addressing_Invalid) {
- return;
- }
- x->expr = node;
- x->value = exact_value_shift(be->op.kind, exact_value_to_integer(x->value), exact_value_to_integer(y->value));
- return;
- }
- x->expr = node;
- x->value = exact_value_shift(be->op.kind, x->value, y->value);
- check_is_expressible(c, x, x->type);
- return;
- }
- if (y_is_untyped) {
- convert_to_typed(c, y, t_uint);
- }
- return;
- }
- if (x->mode == Addressing_Constant) {
- if (x_is_untyped) {
- if (type_hint) {
- if (is_type_integer(type_hint)) {
- convert_to_typed(c, x, type_hint);
- } else if (is_type_any(type_hint)) {
- convert_to_typed(c, x, default_type(t_untyped_integer));
- } else {
- gbString x_str = expr_to_string(x->expr);
- gbString type_str = type_to_string(type_hint);
- error(x->expr, "Shifted operand '%s' cannot convert to non-integer type '%s'", x_str, type_str);
- gb_string_free(x_str);
- gb_string_free(type_str);
- x->mode = Addressing_Invalid;
- return;
- }
- } else {
- check_is_expressible(c, x, default_type(t_untyped_integer));
- }
- if (x->mode == Addressing_Invalid) {
- return;
- }
- }
- x->mode = Addressing_Value;
- }
- }
- gb_internal bool check_is_castable_to(CheckerContext *c, Operand *operand, Type *y) {
- if (check_is_assignable_to(c, operand, y)) {
- return true;
- }
- bool is_constant = operand->mode == Addressing_Constant;
- Type *x = operand->type;
- Type *src = core_type(x);
- Type *dst = core_type(y);
- if (are_types_identical(src, dst)) {
- return true;
- }
- // if (is_type_tuple(src)) {
- // Ast *expr = unparen_expr(operand->expr);
- // if (expr && expr->kind == Ast_CallExpr) {
- // // NOTE(bill, 2021-04-19): Allow casting procedure calls with #optional_ok
- // ast_node(ce, CallExpr, expr);
- // Type *pt = base_type(type_of_expr(ce->proc));
- // if (pt->kind == Type_Proc && pt->Proc.optional_ok) {
- // if (pt->Proc.result_count > 0) {
- // Operand op = *operand;
- // op.type = pt->Proc.results->Tuple.variables[0]->type;
- // bool ok = check_is_castable_to(c, &op, y);
- // if (ok) {
- // ce->optional_ok_one = true;
- // }
- // return ok;
- // }
- // }
- // }
- // }
- if (is_constant && is_type_untyped(src) && is_type_string(src)) {
- if (is_type_u8_array(dst)) {
- String s = operand->value.value_string;
- return s.len == dst->Array.count;
- }
- if (is_type_rune_array(dst)) {
- String s = operand->value.value_string;
- return gb_utf8_strnlen(s.text, s.len) == dst->Array.count;
- }
- }
- if (dst->kind == Type_Array && src->kind == Type_Array) {
- if (are_types_identical(dst->Array.elem, src->Array.elem)) {
- return dst->Array.count == src->Array.count;
- }
- }
- if (dst->kind == Type_Slice && src->kind == Type_Slice) {
- return are_types_identical(dst->Slice.elem, src->Slice.elem);
- }
- // Cast between booleans and integers
- if (is_type_boolean(src) || is_type_integer(src)) {
- if (is_type_boolean(dst) || is_type_integer(dst)) {
- return true;
- }
- }
- // Cast between numbers
- if (is_type_integer(src) || is_type_float(src)) {
- if (is_type_integer(dst) || is_type_float(dst)) {
- return true;
- }
- }
- if (is_type_bit_field(src)) {
- return are_types_identical(core_type(src->BitField.backing_type), dst);
- }
- if (is_type_bit_field(dst)) {
- return are_types_identical(src, core_type(dst->BitField.backing_type));
- }
- if (is_type_integer(src) && is_type_rune(dst)) {
- return true;
- }
- if (is_type_rune(src) && is_type_integer(dst)) {
- return true;
- }
- if (is_type_complex(src) && is_type_complex(dst)) {
- return true;
- }
- if (is_type_float(src) && is_type_complex(dst)) {
- return true;
- }
- if (is_type_float(src) && is_type_quaternion(dst)) {
- return true;
- }
- if (is_type_complex(src) && is_type_quaternion(dst)) {
- return true;
- }
- if (is_type_quaternion(src) && is_type_quaternion(dst)) {
- return true;
- }
-
- if (is_type_matrix(src) && is_type_matrix(dst)) {
- GB_ASSERT(src->kind == Type_Matrix);
- GB_ASSERT(dst->kind == Type_Matrix);
- Operand op = *operand;
- op.type = src->Matrix.elem;
- if (!check_is_castable_to(c, &op, dst->Matrix.elem)) {
- return false;
- }
-
- if (src->Matrix.row_count != src->Matrix.column_count) {
- i64 src_count = src->Matrix.row_count*src->Matrix.column_count;
- i64 dst_count = dst->Matrix.row_count*dst->Matrix.column_count;
- return src_count == dst_count;
- }
-
- return is_matrix_square(dst) && is_matrix_square(src);
- }
- // Cast between pointers
- if (is_type_pointer(src) && is_type_pointer(dst)) {
- return true;
- }
- if (is_type_multi_pointer(src) && is_type_multi_pointer(dst)) {
- return true;
- }
- if (is_type_multi_pointer(src) && is_type_pointer(dst)) {
- return true;
- }
- if (is_type_pointer(src) && is_type_multi_pointer(dst)) {
- return true;
- }
- // uintptr <-> pointer
- if (is_type_uintptr(src) && is_type_pointer(dst)) {
- return true;
- }
- if (is_type_pointer(src) && is_type_uintptr(dst)) {
- return true;
- }
- if (is_type_uintptr(src) && is_type_multi_pointer(dst)) {
- return true;
- }
- if (is_type_multi_pointer(src) && is_type_uintptr(dst)) {
- return true;
- }
- // []byte/[]u8 <-> string (not cstring)
- if (is_type_u8_slice(src) && (is_type_string(dst) && !is_type_cstring(dst))) {
- return true;
- }
- // cstring -> string
- if (are_types_identical(src, t_cstring) && are_types_identical(dst, t_string)) {
- if (operand->mode != Addressing_Constant) {
- add_package_dependency(c, "runtime", "cstring_to_string");
- }
- return true;
- }
- // cstring -> ^u8
- if (are_types_identical(src, t_cstring) && is_type_u8_ptr(dst)) {
- return !is_constant;
- }
- // cstring -> [^]u8
- if (are_types_identical(src, t_cstring) && is_type_u8_multi_ptr(dst)) {
- return !is_constant;
- }
- // cstring -> rawptr
- if (are_types_identical(src, t_cstring) && is_type_rawptr(dst)) {
- return !is_constant;
- }
- // ^u8 -> cstring
- if (is_type_u8_ptr(src) && are_types_identical(dst, t_cstring)) {
- return !is_constant;
- }
- // [^]u8 -> cstring
- if (is_type_u8_multi_ptr(src) && are_types_identical(dst, t_cstring)) {
- return !is_constant;
- }
- // rawptr -> cstring
- if (is_type_rawptr(src) && are_types_identical(dst, t_cstring)) {
- return !is_constant;
- }
- // proc <-> proc
- if (is_type_proc(src) && is_type_proc(dst)) {
- if (is_type_polymorphic(dst)) {
- if (is_type_polymorphic(src) &&
- operand->mode == Addressing_Variable) {
- return true;
- }
- return false;
- }
- return true;
- }
- // proc -> rawptr
- if (is_type_proc(src) && is_type_rawptr(dst)) {
- return true;
- }
- // rawptr -> proc
- if (is_type_rawptr(src) && is_type_proc(dst)) {
- return true;
- }
- if (is_type_array(dst)) {
- Type *elem = base_array_type(dst);
- if (check_is_castable_to(c, operand, elem)) {
- return true;
- }
- }
- if (is_type_simd_vector(src) && is_type_simd_vector(dst)) {
- if (src->SimdVector.count != dst->SimdVector.count) {
- return false;
- }
- Type *elem_src = base_array_type(src);
- Type *elem_dst = base_array_type(dst);
- Operand x = {};
- x.type = elem_src;
- x.mode = Addressing_Value;
- return check_is_castable_to(c, &x, elem_dst);
- }
- if (is_type_simd_vector(dst)) {
- Type *elem = base_array_type(dst);
- if (check_is_castable_to(c, operand, elem)) {
- return true;
- }
- }
- return false;
- }
- gb_internal bool check_cast_internal(CheckerContext *c, Operand *x, Type *type) {
- bool is_const_expr = x->mode == Addressing_Constant;
- Type *bt = base_type(type);
- if (is_const_expr && is_type_constant_type(bt)) {
- if (core_type(bt)->kind == Type_Basic) {
- if (check_representable_as_constant(c, x->value, bt, &x->value)) {
- return true;
- } else if (check_is_castable_to(c, x, type)) {
- if (is_type_pointer(type)) {
- return true;
- }
- }
- } else if (check_is_castable_to(c, x, type)) {
- x->value = {};
- x->mode = Addressing_Value;
- return true;
- }
- } else if (check_is_castable_to(c, x, type)) {
- if (x->mode != Addressing_Constant) {
- x->mode = Addressing_Value;
- } else if (is_type_slice(type) && is_type_string(x->type)) {
- x->mode = Addressing_Value;
- } else if (is_type_union(type)) {
- x->mode = Addressing_Value;
- }
- if (x->mode == Addressing_Value) {
- x->value = {};
- }
- return true;
- }
- return false;
- }
- gb_internal void check_cast(CheckerContext *c, Operand *x, Type *type, bool forbid_identical = false) {
- if (!is_operand_value(*x)) {
- error(x->expr, "Only values can be casted");
- x->mode = Addressing_Invalid;
- return;
- }
- bool is_const_expr = x->mode == Addressing_Constant;
- bool can_convert = check_cast_internal(c, x, type);
- if (!can_convert) {
- TEMPORARY_ALLOCATOR_GUARD();
- gbString expr_str = expr_to_string(x->expr, temporary_allocator());
- gbString to_type = type_to_string(type, temporary_allocator());
- gbString from_type = type_to_string(x->type, temporary_allocator());
- x->mode = Addressing_Invalid;
- ERROR_BLOCK();
- error(x->expr, "Cannot cast '%s' as '%s' from '%s'", expr_str, to_type, from_type);
- if (is_const_expr) {
- gbString val_str = exact_value_to_string(x->value);
- if (is_type_float(x->type) && is_type_integer(type)) {
- error_line("\t%s cannot be represented without truncation/rounding as the type '%s'\n", val_str, to_type);
- // NOTE(bill): keep the mode and modify the type to minimize errors further on
- x->mode = Addressing_Constant;
- x->type = type;
- } else {
- error_line("\t'%s' cannot be represented as the type '%s'\n", val_str, to_type);
- if (is_type_numeric(type)) {
- // NOTE(bill): keep the mode and modify the type to minimize errors further on
- x->mode = Addressing_Constant;
- x->type = type;
- }
- }
- gb_string_free(val_str);
- }
- check_cast_error_suggestion(c, x, type);
- return;
- }
- if (is_type_untyped(x->type)) {
- Type *final_type = type;
- if (is_const_expr && !is_type_constant_type(type)) {
- if (is_type_union(type)) {
- convert_to_typed(c, x, type);
- }
- final_type = default_type(x->type);
- }
- update_untyped_expr_type(c, x->expr, final_type, true);
- } else {
- Type *src = core_type(x->type);
- Type *dst = core_type(type);
- if (src != dst) {
- bool const REQUIRE = true;
- if (is_type_integer_128bit(src) && is_type_float(dst)) {
- add_package_dependency(c, "runtime", "floattidf_unsigned", REQUIRE);
- add_package_dependency(c, "runtime", "floattidf", REQUIRE);
- } else if (is_type_integer_128bit(dst) && is_type_float(src)) {
- add_package_dependency(c, "runtime", "fixunsdfti", REQUIRE);
- add_package_dependency(c, "runtime", "fixunsdfdi", REQUIRE);
- } else if (src == t_f16 && is_type_float(dst)) {
- add_package_dependency(c, "runtime", "gnu_h2f_ieee", REQUIRE);
- add_package_dependency(c, "runtime", "extendhfsf2", REQUIRE);
- } else if (is_type_float(dst) && dst == t_f16) {
- add_package_dependency(c, "runtime", "truncsfhf2", REQUIRE);
- add_package_dependency(c, "runtime", "truncdfhf2", REQUIRE);
- add_package_dependency(c, "runtime", "gnu_f2h_ieee", REQUIRE);
- }
- }
- // If we check polymorphic procedures, we risk erring on
- // identical casts that cannot be foreseen or otherwise
- // forbidden, so just skip them.
- if (forbid_identical && check_vet_flags(c) & VetFlag_Cast &&
- (c->curr_proc_sig == nullptr || !is_type_polymorphic(c->curr_proc_sig))) {
- Type *src_exact = x->type;
- Type *dst_exact = type;
- if (src_exact != nullptr &&
- dst_exact != nullptr &&
- are_types_identical(src_exact, dst_exact)
- ) {
- gbString oper_str = expr_to_string(x->expr);
- gbString to_type = type_to_string(dst_exact);
- error(x->expr, "Unneeded cast of '%s' to identical type '%s'", oper_str, to_type);
- gb_string_free(oper_str);
- gb_string_free(to_type);
- }
- }
- }
- x->type = type;
- }
- gb_internal bool check_transmute(CheckerContext *c, Ast *node, Operand *o, Type *t, bool forbid_identical = false) {
- if (!is_operand_value(*o)) {
- error(o->expr, "'transmute' can only be applied to values");
- o->mode = Addressing_Invalid;
- return false;
- }
- Operand src = *o;
- Type *src_t = o->type;
- Type *dst_t = t;
- Type *src_bt = base_type(src_t);
- Type *dst_bt = base_type(dst_t);
- if (is_type_untyped(src_t)) {
- gbString expr_str = expr_to_string(o->expr);
- error(o->expr, "Cannot transmute untyped expression: '%s'", expr_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return false;
- }
- if (dst_bt == nullptr || dst_bt == t_invalid) {
- GB_ASSERT(global_error_collector.count != 0);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return false;
- }
- if (src_bt == nullptr || src_bt == t_invalid) {
- // NOTE(bill): this should be an error
- GB_ASSERT(global_error_collector.count != 0);
- o->mode = Addressing_Value;
- o->expr = node;
- o->type = dst_t;
- return true;
- }
- i64 srcz = type_size_of(src_t);
- i64 dstz = type_size_of(dst_t);
- if (srcz != dstz) {
- gbString expr_str = expr_to_string(o->expr);
- gbString type_str = type_to_string(dst_t);
- error(o->expr, "Cannot transmute '%s' to '%s', %lld vs %lld bytes", expr_str, type_str, srcz, dstz);
- gb_string_free(type_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return false;
- }
- o->expr = node;
- o->type = dst_t;
- if (o->mode == Addressing_Constant) {
- if (are_types_identical(src_bt, dst_bt)) {
- return true;
- }
- if ((is_type_integer(src_t) && is_type_integer(dst_t)) ||
- is_type_integer(src_t) && is_type_bit_set(dst_t)) {
- if (types_have_same_internal_endian(src_t, dst_t)) {
- ExactValue src_v = exact_value_to_integer(o->value);
- GB_ASSERT(src_v.kind == ExactValue_Integer || src_v.kind == ExactValue_Invalid);
- BigInt v = src_v.value_integer;
- BigInt smax = {};
- BigInt umax = {};
- big_int_from_u64(&smax, 0);
- big_int_not(&smax, &smax, cast(i32)(srcz*8 - 1), false);
- big_int_from_u64(&umax, 1);
- BigInt sz_in_bits = big_int_make_i64(srcz*8);
- big_int_shl_eq(&umax, &sz_in_bits);
- if (is_type_unsigned(src_t) && !is_type_unsigned(dst_t)) {
- if (big_int_cmp(&v, &smax) >= 0) {
- big_int_sub_eq(&v, &umax);
- }
- } else if (!is_type_unsigned(src_t) && is_type_unsigned(dst_t)) {
- if (big_int_is_neg(&v)) {
- big_int_add_eq(&v, &umax);
- }
- }
- o->value.kind = ExactValue_Integer;
- o->value.value_integer = v;
- return true;
- }
- }
- } else {
- // If we check polymorphic procedures, we risk erring on
- // identical casts that cannot be foreseen or otherwise
- // forbidden, so just skip them.
- if (forbid_identical && check_vet_flags(c) & VetFlag_Cast &&
- (c->curr_proc_sig == nullptr || !is_type_polymorphic(c->curr_proc_sig)) &&
- check_is_castable_to(c, &src, dst_t)) {
- if (are_types_identical(src_t, dst_t)) {
- gbString oper_str = expr_to_string(o->expr);
- gbString to_type = type_to_string(dst_t);
- error(o->expr, "Unneeded transmute of '%s' to identical type '%s'", oper_str, to_type);
- gb_string_free(oper_str);
- gb_string_free(to_type);
- } else if (is_type_internally_pointer_like(src_t) &&
- is_type_internally_pointer_like(dst_t)) {
- error(o->expr, "Use of 'transmute' where 'cast' would be preferred since the types are pointer-like");
- } else if (are_types_identical(src_bt, dst_bt)) {
- gbString oper_str = expr_to_string(o->expr);
- gbString to_type = type_to_string(dst_t);
- error(o->expr, "Unneeded transmute of '%s' to identical type '%s'", oper_str, to_type);
- gb_string_free(oper_str);
- gb_string_free(to_type);
- } else if (is_type_integer(src_t) && is_type_integer(dst_t) &&
- types_have_same_internal_endian(src_t, dst_t) &&
- type_endian_kind_of(src_t) == type_endian_kind_of(dst_t)) {
- gbString oper_type = type_to_string(src_t);
- gbString to_type = type_to_string(dst_t);
- error(o->expr, "Use of 'transmute' where 'cast' would be preferred since both are integers of the same endianness, from '%s' to '%s'", oper_type, to_type);
- gb_string_free(to_type);
- gb_string_free(oper_type);
- }
- }
- }
- o->mode = Addressing_Value;
- o->value = {};
- return true;
- }
- gb_internal bool check_binary_array_expr(CheckerContext *c, Token op, Operand *x, Operand *y) {
- if (is_type_array_like(x->type) || is_type_array_like(y->type)) {
- if (op.kind == Token_CmpAnd || op.kind == Token_CmpOr) {
- error(op, "Array programming is not allowed with the operator '%.*s'", LIT(op.string));
- }
- }
- if (is_type_array(x->type) && !is_type_array(y->type)) {
- if (check_is_assignable_to(c, y, x->type)) {
- if (check_binary_op(c, x, op)) {
- return true;
- }
- }
- }
- if (is_type_simd_vector(x->type) && !is_type_simd_vector(y->type)) {
- if (check_is_assignable_to(c, y, x->type)) {
- if (check_binary_op(c, x, op)) {
- return true;
- }
- }
- }
- return false;
- }
- gb_internal bool is_ise_expr(Ast *node) {
- node = unparen_expr(node);
- return node->kind == Ast_ImplicitSelectorExpr;
- }
- gb_internal bool can_use_other_type_as_type_hint(bool use_lhs_as_type_hint, Type *other_type) {
- if (use_lhs_as_type_hint) { // RHS in this case
- return other_type != nullptr && other_type != t_invalid && is_type_typed(other_type);
- }
- return false;
- }
- gb_internal Type *check_matrix_type_hint(Type *matrix, Type *type_hint) {
- Type *xt = base_type(matrix);
- if (type_hint != nullptr) {
- Type *th = base_type(type_hint);
- if (are_types_identical(th, xt)) {
- return type_hint;
- } else if (xt->kind == Type_Matrix && th->kind == Type_Matrix) {
- if (!are_types_identical(xt->Matrix.elem, th->Matrix.elem)) {
- // ignore
- } if (xt->Matrix.row_count == th->Matrix.row_count &&
- xt->Matrix.column_count == th->Matrix.column_count) {
- return type_hint;
- }
- } else if (xt->kind == Type_Matrix && th->kind == Type_Array) {
- if (!are_types_identical(xt->Matrix.elem, th->Array.elem)) {
- // ignore
- } else if (xt->Matrix.row_count == 1 && xt->Matrix.column_count == th->Array.count) {
- return type_hint;
- } else if (xt->Matrix.column_count == 1 && xt->Matrix.row_count == th->Array.count) {
- return type_hint;
- }
- }
- }
- return matrix;
- }
- gb_internal void check_binary_matrix(CheckerContext *c, Token const &op, Operand *x, Operand *y, Type *type_hint, bool use_lhs_as_type_hint) {
- if (!check_binary_op(c, x, op)) {
- x->mode = Addressing_Invalid;
- return;
- }
-
- Type *xt = base_type(x->type);
- Type *yt = base_type(y->type);
-
- if (is_type_matrix(x->type)) {
- GB_ASSERT(xt->kind == Type_Matrix);
- if (op.kind == Token_Mul) {
- if (yt->kind == Type_Matrix) {
- if (!are_types_identical(xt->Matrix.elem, yt->Matrix.elem)) {
- goto matrix_error;
- }
-
- if (xt->Matrix.column_count != yt->Matrix.row_count) {
- goto matrix_error;
- }
- if (xt->Matrix.is_row_major != yt->Matrix.is_row_major) {
- goto matrix_error;
- }
- x->mode = Addressing_Value;
- if (are_types_identical(xt, yt)) {
- if (are_types_identical(x->type, y->type)) {
- return;
- }
- if (!is_type_named(x->type) && is_type_named(y->type)) {
- // prefer the named type
- x->type = y->type;
- }
- } else {
- bool is_row_major = xt->Matrix.is_row_major && yt->Matrix.is_row_major;
- x->type = alloc_type_matrix(xt->Matrix.elem, xt->Matrix.row_count, yt->Matrix.column_count, nullptr, nullptr, is_row_major);
- }
- goto matrix_success;
- } else if (yt->kind == Type_Array) {
- if (!are_types_identical(xt->Matrix.elem, yt->Array.elem)) {
- goto matrix_error;
- }
-
- if (xt->Matrix.column_count != yt->Array.count) {
- goto matrix_error;
- }
-
- // Treat arrays as column vectors
- x->mode = Addressing_Value;
- if (xt->Matrix.row_count == yt->Array.count) {
- x->type = y->type;
- } else {
- x->type = alloc_type_matrix(xt->Matrix.elem, xt->Matrix.row_count, 1, nullptr, nullptr, xt->Matrix.is_row_major);
- }
- goto matrix_success;
- }
- }
- if (!are_types_identical(xt, yt)) {
- goto matrix_error;
- }
- x->mode = Addressing_Value;
- x->type = xt;
- goto matrix_success;
- } else {
- GB_ASSERT(!is_type_matrix(xt));
- GB_ASSERT(is_type_matrix(yt));
-
- if (op.kind == Token_Mul) {
- // NOTE(bill): no need to handle the matrix case here since it should be handled above
- if (xt->kind == Type_Array) {
- if (!are_types_identical(yt->Matrix.elem, xt->Array.elem)) {
- goto matrix_error;
- }
-
- if (xt->Array.count != yt->Matrix.row_count) {
- goto matrix_error;
- }
-
- // Treat arrays as row vectors
- x->mode = Addressing_Value;
- if (yt->Matrix.column_count == xt->Array.count) {
- x->type = x->type;
- } else {
- x->type = alloc_type_matrix(yt->Matrix.elem, 1, yt->Matrix.column_count, nullptr, nullptr, yt->Matrix.is_row_major);
- }
- goto matrix_success;
- } else if (are_types_identical(yt->Matrix.elem, xt)) {
- x->type = check_matrix_type_hint(y->type, type_hint);
- return;
- }
- }
- if (!are_types_identical(xt, yt)) {
- goto matrix_error;
- }
- x->mode = Addressing_Value;
- x->type = xt;
- goto matrix_success;
- }
- matrix_success:
- x->type = check_matrix_type_hint(x->type, type_hint);
- return;
-
-
- matrix_error:
- gbString xts = type_to_string(x->type);
- gbString yts = type_to_string(y->type);
- gbString expr_str = expr_to_string(x->expr);
- error(op, "Mismatched types in binary matrix expression '%s' for operator '%.*s' : '%s' vs '%s'", expr_str, LIT(op.string), xts, yts);
- gb_string_free(expr_str);
- gb_string_free(yts);
- gb_string_free(xts);
- x->type = t_invalid;
- x->mode = Addressing_Invalid;
- return;
-
- }
- gb_internal void check_binary_expr_dependency(CheckerContext *c, Token op, Type *bt, bool REQUIRE) {
- if (op.kind == Token_Mod || op.kind == Token_ModEq ||
- op.kind == Token_ModMod || op.kind == Token_ModModEq) {
- if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
- case Basic_u128: add_package_dependency(c, "runtime", "umodti3", REQUIRE); break;
- case Basic_i128: add_package_dependency(c, "runtime", "modti3", REQUIRE); break;
- }
- } else if (op.kind == Token_Quo || op.kind == Token_QuoEq) {
- if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
- case Basic_complex32: add_package_dependency(c, "runtime", "quo_complex32"); break;
- case Basic_complex64: add_package_dependency(c, "runtime", "quo_complex64"); break;
- case Basic_complex128: add_package_dependency(c, "runtime", "quo_complex128"); break;
- case Basic_quaternion64: add_package_dependency(c, "runtime", "quo_quaternion64"); break;
- case Basic_quaternion128: add_package_dependency(c, "runtime", "quo_quaternion128"); break;
- case Basic_quaternion256: add_package_dependency(c, "runtime", "quo_quaternion256"); break;
- case Basic_u128: add_package_dependency(c, "runtime", "udivti3", REQUIRE); break;
- case Basic_i128: add_package_dependency(c, "runtime", "divti3", REQUIRE); break;
- }
- } else if (op.kind == Token_Mul || op.kind == Token_MulEq) {
- if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
- case Basic_quaternion64: add_package_dependency(c, "runtime", "mul_quaternion64"); break;
- case Basic_quaternion128: add_package_dependency(c, "runtime", "mul_quaternion128"); break;
- case Basic_quaternion256: add_package_dependency(c, "runtime", "mul_quaternion256"); break;
- case Basic_u128:
- case Basic_i128:
- if (is_arch_wasm()) {
- add_package_dependency(c, "runtime", "__multi3", REQUIRE);
- }
- break;
- }
- } else if (op.kind == Token_Shl || op.kind == Token_ShlEq) {
- if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
- case Basic_u128:
- case Basic_i128:
- if (is_arch_wasm()) {
- add_package_dependency(c, "runtime", "__ashlti3", REQUIRE);
- }
- break;
- }
- } else if (op.kind == Token_Shr || op.kind == Token_ShrEq) {
- if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
- case Basic_u128:
- case Basic_i128:
- if (is_arch_wasm()) {
- add_package_dependency(c, "runtime", "__lshrti3", REQUIRE);
- }
- break;
- }
- }
- }
- gb_internal void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, Type *type_hint, bool use_lhs_as_type_hint=false) {
- GB_ASSERT(node->kind == Ast_BinaryExpr);
- Operand y_ = {}, *y = &y_;
- ast_node(be, BinaryExpr, node);
- defer({
- node->viral_state_flags |= be->left->viral_state_flags;
- node->viral_state_flags |= be->right->viral_state_flags;
- });
- Token op = be->op;
- switch (op.kind) {
- case Token_CmpEq:
- case Token_NotEq: {
- // NOTE(bill): Allow comparisons between types
- if (is_ise_expr(be->left)) {
- // Evalute the right before the left for an '.X' expression
- check_expr_or_type(c, y, be->right, nullptr /* ignore type hint */);
- check_expr_or_type(c, x, be->left, y->type);
- } else {
- check_expr_or_type(c, x, be->left, nullptr /* ignore type hint */);
- check_expr_or_type(c, y, be->right, x->type);
- }
- bool xt = x->mode == Addressing_Type;
- bool yt = y->mode == Addressing_Type;
- // If only one is a type, this is an error
- if (xt ^ yt) {
- GB_ASSERT(xt != yt);
- if (xt) {
- if (!is_type_typeid(y->type)) {
- error_operand_not_expression(x);
- }
- }
- if (yt) {
- if (!is_type_typeid(x->type)) {
- error_operand_not_expression(y);
- }
- }
- }
- break;
- }
- case Token_in:
- case Token_not_in:
- {
- // IMPORTANT NOTE(bill): This uses right-left evaluation in type checking only no in
- check_expr(c, y, be->right);
- Type *rhs_type = type_deref(y->type);
- if (rhs_type == nullptr) {
- error(y->expr, "Cannot use '%.*s' on an expression with no value", LIT(op.string));
- x->mode = Addressing_Invalid;
- x->expr = node;
- return;
- }
- if (is_type_bit_set(rhs_type)) {
- Type *elem = base_type(rhs_type)->BitSet.elem;
- check_expr_with_type_hint(c, x, be->left, elem);
- } else if (is_type_map(rhs_type)) {
- Type *key = base_type(rhs_type)->Map.key;
- check_expr_with_type_hint(c, x, be->left, key);
- } else {
- check_expr(c, x, be->left);
- }
- if (x->mode == Addressing_Invalid) {
- return;
- }
- if (y->mode == Addressing_Invalid) {
- x->mode = Addressing_Invalid;
- x->expr = y->expr;
- return;
- }
- if (is_type_map(rhs_type)) {
- Type *yt = base_type(rhs_type);
- if (op.kind == Token_in) {
- check_assignment(c, x, yt->Map.key, str_lit("map 'in'"));
- } else {
- check_assignment(c, x, yt->Map.key, str_lit("map 'not_in'"));
- }
- add_map_get_dependencies(c);
- } else if (is_type_bit_set(rhs_type)) {
- Type *yt = base_type(rhs_type);
- if (op.kind == Token_in) {
- check_assignment(c, x, yt->BitSet.elem, str_lit("bit_set 'in'"));
- } else {
- check_assignment(c, x, yt->BitSet.elem, str_lit("bit_set 'not_in'"));
- }
- if (x->mode == Addressing_Constant && y->mode == Addressing_Constant) {
- ExactValue k = exact_value_to_integer(x->value);
- ExactValue v = exact_value_to_integer(y->value);
- GB_ASSERT(k.kind == ExactValue_Integer);
- GB_ASSERT(v.kind == ExactValue_Integer);
- i64 key = big_int_to_i64(&k.value_integer);
- i64 lower = yt->BitSet.lower;
- i64 upper = yt->BitSet.upper;
- if (lower <= key && key <= upper) {
- i64 bit = 1ll<<key;
- i64 bits = big_int_to_i64(&v.value_integer);
- x->mode = Addressing_Constant;
- x->type = t_untyped_bool;
- if (op.kind == Token_in) {
- x->value = exact_value_bool((bit & bits) != 0);
- } else {
- x->value = exact_value_bool((bit & bits) == 0);
- }
- x->expr = node;
- return;
- } else {
- error(x->expr, "key '%lld' out of range of bit set, %lld..%lld", key, lower, upper);
- x->mode = Addressing_Invalid;
- }
- }
- } else {
- gbString t = type_to_string(y->type);
- error(x->expr, "expected either a map or bitset for 'in', got %s", t);
- gb_string_free(t);
- x->expr = node;
- x->mode = Addressing_Invalid;
- return;
- }
- if (x->mode != Addressing_Invalid) {
- x->mode = Addressing_Value;
- x->type = t_untyped_bool;
- }
- x->expr = node;
- return;
- }
- default:
- if (is_ise_expr(be->left)) {
- // Evalute the right before the left for an '.X' expression
- check_expr_or_type(c, y, be->right, type_hint);
- if (can_use_other_type_as_type_hint(use_lhs_as_type_hint, y->type)) { // RHS in this case
- check_expr_or_type(c, x, be->left, y->type);
- } else {
- check_expr_with_type_hint(c, x, be->left, type_hint);
- }
- } else {
- check_expr_with_type_hint(c, x, be->left, type_hint);
- if (can_use_other_type_as_type_hint(use_lhs_as_type_hint, x->type)) {
- check_expr_with_type_hint(c, y, be->right, x->type);
- } else {
- check_expr_with_type_hint(c, y, be->right, type_hint);
- }
- }
- break;
- }
- if (x->mode == Addressing_Invalid) {
- return;
- }
- if (y->mode == Addressing_Invalid) {
- x->mode = Addressing_Invalid;
- x->expr = y->expr;
- return;
- }
- if (x->mode == Addressing_Builtin) {
- x->mode = Addressing_Invalid;
- error(x->expr, "built-in expression in binary expression");
- return;
- }
- if (y->mode == Addressing_Builtin) {
- x->mode = Addressing_Invalid;
- error(y->expr, "built-in expression in binary expression");
- return;
- }
- if (x->mode == Addressing_ProcGroup) {
- x->mode = Addressing_Invalid;
- if (x->proc_group != nullptr) {
- error(x->expr, "procedure group '%.*s' used in binary expression", LIT(x->proc_group->token.string));
- } else {
- error(x->expr, "procedure group used in binary expression");
- }
- return;
- }
- if (y->mode == Addressing_ProcGroup) {
- x->mode = Addressing_Invalid;
- if (x->proc_group != nullptr) {
- error(y->expr, "procedure group '%.*s' used in binary expression", LIT(y->proc_group->token.string));
- } else {
- error(y->expr, "procedure group used in binary expression");
- }
- return;
- }
- bool REQUIRE = true;
- Type *btx = base_type(x->type);
- Type *bty = base_type(y->type);
- check_binary_expr_dependency(c, op, btx, REQUIRE);
- check_binary_expr_dependency(c, op, bty, REQUIRE);
- if (token_is_shift(op.kind)) {
- check_shift(c, x, y, node, type_hint);
- return;
- }
- switch (op.kind) {
- case Token_Quo:
- case Token_Mod:
- case Token_ModMod:
- case Token_QuoEq:
- case Token_ModEq:
- case Token_ModModEq:
- if (is_type_integer(y->type) && !is_type_untyped(y->type) &&
- is_type_float(x->type) && is_type_untyped(x->type)) {
- char const *suggestion = "\tSuggestion: Try explicitly casting the constant value for clarity";
- gbString t = type_to_string(y->type);
- if (x->value.kind != ExactValue_Invalid) {
- gbString s = exact_value_to_string(x->value);
- warning(node, "Dividing an untyped float '%s' by '%s' will perform integer division\n%s", s, t, suggestion);
- gb_string_free(s);
- } else {
- warning(node, "Dividing an untyped float by '%s' will perform integer division\n%s", t, suggestion);
- }
- gb_string_free(t);
- }
- break;
- }
- convert_to_typed(c, x, y->type);
- if (x->mode == Addressing_Invalid) {
- return;
- }
- convert_to_typed(c, y, x->type);
- if (y->mode == Addressing_Invalid) {
- x->mode = Addressing_Invalid;
- return;
- }
- if (token_is_comparison(op.kind)) {
- check_comparison(c, node, x, y, op.kind);
- return;
- }
- if (check_binary_array_expr(c, op, x, y)) {
- x->mode = Addressing_Value;
- x->type = x->type;
- return;
- }
- if (check_binary_array_expr(c, op, y, x)) {
- x->mode = Addressing_Value;
- x->type = y->type;
- return;
- }
- if (is_type_matrix(x->type) || is_type_matrix(y->type)) {
- check_binary_matrix(c, op, x, y, type_hint, use_lhs_as_type_hint);
- x->expr = node;
- return;
- }
- if ((op.kind == Token_CmpAnd || op.kind == Token_CmpOr) &&
- is_type_boolean(x->type) && is_type_boolean(y->type)) {
- // NOTE(bill, 2022-06-26)
- // Allow any boolean types within `&&` and `||`
- // This is an exception to all other binary expressions since the result
- // of a comparison will always be an untyped boolean, and allowing
- // any boolean between these two simplifies a lot of expressions
- } else if (!are_types_identical(x->type, y->type)) {
- if (x->type != t_invalid &&
- y->type != t_invalid) {
- gbString xt = type_to_string(x->type);
- gbString yt = type_to_string(y->type);
- gbString expr_str = expr_to_string(node);
- error(op, "Mismatched types in binary expression '%s' : '%s' vs '%s'", expr_str, xt, yt);
- gb_string_free(expr_str);
- gb_string_free(yt);
- gb_string_free(xt);
- }
- x->mode = Addressing_Invalid;
- return;
- }
- if (!check_binary_op(c, x, op)) {
- x->mode = Addressing_Invalid;
- return;
- }
- switch (op.kind) {
- case Token_Quo:
- case Token_Mod:
- case Token_ModMod:
- case Token_QuoEq:
- case Token_ModEq:
- case Token_ModModEq:
- if ((x->mode == Addressing_Constant || is_type_integer(x->type)) &&
- y->mode == Addressing_Constant) {
- bool fail = false;
- switch (y->value.kind) {
- case ExactValue_Integer:
- if (big_int_is_zero(&y->value.value_integer)) {
- fail = true;
- }
- break;
- case ExactValue_Float:
- if (y->value.value_float == 0.0) {
- fail = true;
- }
- break;
- }
- if (fail) {
- error(y->expr, "Division by zero not allowed");
- x->mode = Addressing_Invalid;
- return;
- }
- }
- break;
- case Token_CmpAnd:
- case Token_CmpOr:
- if (be->left->viral_state_flags & ViralStateFlag_ContainsDeferredProcedure) {
- error(be->left, "Procedure calls that have an associated deferred procedure are not allowed within logical binary expressions");
- }
- if (be->right->viral_state_flags & ViralStateFlag_ContainsDeferredProcedure) {
- error(be->right, "Procedure calls that have an associated deferred procedure are not allowed within logical binary expressions");
- }
- break;
- }
- if (x->mode == Addressing_Constant &&
- y->mode == Addressing_Constant) {
- ExactValue a = x->value;
- ExactValue b = y->value;
- if (!is_type_constant_type(x->type)) {
- x->mode = Addressing_Value;
- return;
- }
- if (op.kind == Token_Quo && is_type_integer(x->type)) {
- op.kind = Token_QuoEq; // NOTE(bill): Hack to get division of integers
- }
- if (is_type_bit_set(x->type)) {
- switch (op.kind) {
- case Token_Add: op.kind = Token_Or; break;
- case Token_Sub: op.kind = Token_AndNot; break;
- }
- }
- x->value = exact_binary_operator_value(op.kind, a, b);
- if (is_type_typed(x->type)) {
- if (node != nullptr) {
- x->expr = node;
- }
- check_is_expressible(c, x, x->type);
- }
- return;
- } else if (is_type_string(x->type)) {
- error(node, "String concatenation is only allowed with constant strings");
- x->mode = Addressing_Invalid;
- return;
- }
- x->mode = Addressing_Value;
- }
- gb_internal Operand make_operand_from_node(Ast *node) {
- GB_ASSERT(node != nullptr);
- Operand x = {};
- x.expr = node;
- x.mode = node->tav.mode;
- x.type = node->tav.type;
- x.value = node->tav.value;
- return x;
- }
- gb_internal void update_untyped_expr_type(CheckerContext *c, Ast *e, Type *type, bool final) {
- GB_ASSERT(e != nullptr);
- ExprInfo *old = check_get_expr_info(c, e);
- if (old == nullptr) {
- if (type != nullptr && type != t_invalid) {
- if (e->tav.type == nullptr || e->tav.type == t_invalid) {
- add_type_and_value(c, e, e->tav.mode, type ? type : e->tav.type, e->tav.value);
- if (e->kind == Ast_TernaryIfExpr) {
- update_untyped_expr_type(c, e->TernaryIfExpr.x, type, final);
- update_untyped_expr_type(c, e->TernaryIfExpr.y, type, final);
- }
- }
- }
- return;
- }
- switch (e->kind) {
- case_ast_node(ue, UnaryExpr, e);
- if (old->value.kind != ExactValue_Invalid) {
- // NOTE(bill): if 'e' is constant, the operands will be constant too.
- // They don't need to be updated as they will be updated later and
- // checked at the end of general checking stage.
- break;
- }
- update_untyped_expr_type(c, ue->expr, type, final);
- case_end;
- case_ast_node(be, BinaryExpr, e);
- if (old->value.kind != ExactValue_Invalid) {
- // See above note in UnaryExpr case
- break;
- }
- if (token_is_comparison(be->op.kind)) {
- // NOTE(bill): Do nothing as the types are fine
- } else if (token_is_shift(be->op.kind)) {
- update_untyped_expr_type(c, be->left, type, final);
- } else {
- update_untyped_expr_type(c, be->left, type, final);
- update_untyped_expr_type(c, be->right, type, final);
- }
- case_end;
- case_ast_node(te, TernaryIfExpr, e);
- if (old->value.kind != ExactValue_Invalid) {
- // See above note in UnaryExpr case
- break;
- }
-
- // NOTE(bill): This is a bit of a hack to get around the edge cases of ternary if expressions
- // having an untyped value
- Operand x = make_operand_from_node(te->x);
- Operand y = make_operand_from_node(te->y);
- if (x.mode != Addressing_Constant || check_is_expressible(c, &x, type)) {
- update_untyped_expr_type(c, te->x, type, final);
- }
- if (y.mode != Addressing_Constant || check_is_expressible(c, &y, type)) {
- update_untyped_expr_type(c, te->y, type, final);
- }
-
- case_end;
- case_ast_node(te, TernaryWhenExpr, e);
- if (old->value.kind != ExactValue_Invalid) {
- // See above note in UnaryExpr case
- break;
- }
- update_untyped_expr_type(c, te->x, type, final);
- update_untyped_expr_type(c, te->y, type, final);
- case_end;
- case_ast_node(ore, OrReturnExpr, e);
- if (old->value.kind != ExactValue_Invalid) {
- // See above note in UnaryExpr case
- break;
- }
- update_untyped_expr_type(c, ore->expr, type, final);
- case_end;
- case_ast_node(obe, OrBranchExpr, e);
- if (old->value.kind != ExactValue_Invalid) {
- // See above note in UnaryExpr case
- break;
- }
- update_untyped_expr_type(c, obe->expr, type, final);
- case_end;
- case_ast_node(oee, OrElseExpr, e);
- if (old->value.kind != ExactValue_Invalid) {
- // See above note in UnaryExpr case
- break;
- }
- update_untyped_expr_type(c, oee->x, type, final);
- update_untyped_expr_type(c, oee->y, type, final);
- case_end;
- case_ast_node(pe, ParenExpr, e);
- update_untyped_expr_type(c, pe->expr, type, final);
- case_end;
- }
- if (!final && is_type_untyped(type)) {
- old->type = base_type(type);
- return;
- }
- // We need to remove it and then give it a new one
- check_remove_expr_info(c, e);
- if (old->is_lhs && !is_type_integer(type)) {
- gbString expr_str = expr_to_string(e);
- gbString type_str = type_to_string(type);
- error(e, "Shifted operand %s must be an integer, got %s", expr_str, type_str);
- gb_string_free(type_str);
- gb_string_free(expr_str);
- return;
- }
- add_type_and_value(c, e, old->mode, type, old->value);
- }
- gb_internal void update_untyped_expr_value(CheckerContext *c, Ast *e, ExactValue value) {
- GB_ASSERT(e != nullptr);
- ExprInfo *found = check_get_expr_info(c, e);
- if (found) {
- found->value = value;
- }
- }
- gb_internal void convert_untyped_error(CheckerContext *c, Operand *operand, Type *target_type, bool ignore_error_block=false) {
- gbString expr_str = expr_to_string(operand->expr);
- gbString type_str = type_to_string(target_type);
- gbString from_type_str = type_to_string(operand->type);
- char const *extra_text = "";
- if (operand->mode == Addressing_Constant) {
- if (big_int_is_zero(&operand->value.value_integer)) {
- if (make_string_c(expr_str) != "nil") { // HACK NOTE(bill): Just in case
- // NOTE(bill): Doesn't matter what the type is as it's still zero in the union
- extra_text = " - Did you want 'nil'?";
- }
- }
- }
- if (!ignore_error_block) {
- begin_error_block();
- }
- error(operand->expr, "Cannot convert untyped value '%s' to '%s' from '%s'%s", expr_str, type_str, from_type_str, extra_text);
- if (operand->value.kind == ExactValue_String) {
- String key = operand->value.value_string;
- if (is_type_string(operand->type) && is_type_enum(target_type)) {
- Type *et = base_type(target_type);
- check_did_you_mean_type(key, et->Enum.fields, ".");
- }
- }
- gb_string_free(from_type_str);
- gb_string_free(type_str);
- gb_string_free(expr_str);
- operand->mode = Addressing_Invalid;
- if (!ignore_error_block) {
- end_error_block();
- }
- }
- gb_internal ExactValue convert_exact_value_for_type(ExactValue v, Type *type) {
- Type *t = core_type(type);
- if (is_type_boolean(t)) {
- // v = exact_value_to_boolean(v);
- } else if (is_type_float(t)) {
- v = exact_value_to_float(v);
- } else if (is_type_integer(t)) {
- v = exact_value_to_integer(v);
- } else if (is_type_pointer(t)) {
- v = exact_value_to_integer(v);
- } else if (is_type_complex(t)) {
- v = exact_value_to_complex(v);
- } else if (is_type_quaternion(t)) {
- v = exact_value_to_quaternion(v);
- }
- return v;
- }
- gb_internal void convert_to_typed(CheckerContext *c, Operand *operand, Type *target_type) {
- // GB_ASSERT_NOT_NULL(target_type);
- if (target_type == nullptr || operand->mode == Addressing_Invalid ||
- operand->mode == Addressing_Type ||
- is_type_typed(operand->type) ||
- target_type == t_invalid) {
- return;
- }
- if (is_type_untyped(target_type)) {
- GB_ASSERT(operand->type->kind == Type_Basic);
- GB_ASSERT(target_type->kind == Type_Basic);
- BasicKind x_kind = operand->type->Basic.kind;
- BasicKind y_kind = target_type->Basic.kind;
- if (is_type_numeric(operand->type) && is_type_numeric(target_type)) {
- if (x_kind < y_kind) {
- operand->type = target_type;
- update_untyped_expr_type(c, operand->expr, target_type, false);
- }
- } else if (x_kind != y_kind) {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- return;
- }
- Type *t = base_type(target_type);
- if (c->in_enum_type) {
- t = core_type(target_type);
- }
- switch (t->kind) {
- case Type_Basic:
- if (operand->mode == Addressing_Constant) {
- check_is_expressible(c, operand, t);
- if (operand->mode == Addressing_Invalid) {
- return;
- }
- update_untyped_expr_value(c, operand->expr, operand->value);
- } else {
- switch (operand->type->Basic.kind) {
- case Basic_UntypedBool:
- if (!is_type_boolean(target_type)) {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- case Basic_UntypedInteger:
- case Basic_UntypedFloat:
- case Basic_UntypedComplex:
- case Basic_UntypedQuaternion:
- case Basic_UntypedRune:
- if (!is_type_numeric(target_type)) {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- case Basic_UntypedNil:
- if (is_type_any(target_type)) {
- // target_type = t_untyped_nil;
- } else if (is_type_cstring(target_type)) {
- // target_type = t_untyped_nil;
- } else if (!type_has_nil(target_type)) {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- }
- }
- break;
- case Type_Array: {
- Type *elem = base_array_type(t);
- if (check_is_assignable_to(c, operand, elem)) {
- operand->mode = Addressing_Value;
- } else {
- if (operand->value.kind == ExactValue_String) {
- String s = operand->value.value_string;
- if (is_type_u8_array(t)) {
- if (s.len == t->Array.count) {
- break;
- }
- } else if (is_type_rune_array(t)) {
- isize rune_count = gb_utf8_strnlen(s.text, s.len);
- if (rune_count == t->Array.count) {
- break;
- }
- }
- }
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- }
-
- case Type_SimdVector: {
- Type *elem = base_array_type(t);
- if (check_is_assignable_to(c, operand, elem)) {
- operand->mode = Addressing_Value;
- } else {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- }
-
- case Type_Matrix: {
- Type *elem = base_array_type(t);
- if (check_is_assignable_to(c, operand, elem)) {
- if (t->Matrix.row_count != t->Matrix.column_count) {
- operand->mode = Addressing_Invalid;
- ERROR_BLOCK();
-
- convert_untyped_error(c, operand, target_type, true);
- error_line("\tNote: Only a square matrix types can be initialized with a scalar value\n");
- return;
- } else {
- operand->mode = Addressing_Value;
- }
- } else {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- }
-
- case Type_Union:
- // IMPORTANT NOTE HACK(bill): This is just to allow for comparisons against `0` with the `os.Error` type
- // as a kind of transition period
- if (!build_context.strict_style &&
- operand->mode == Addressing_Constant &&
- target_type->kind == Type_Named &&
- (c->pkg == nullptr || c->pkg->name != "os") &&
- target_type->Named.name == "Error") {
- Entity *e = target_type->Named.type_name;
- if (e->pkg && e->pkg->name == "os") {
- if (is_exact_value_zero(operand->value) &&
- (operand->value.kind == ExactValue_Integer ||
- operand->value.kind == ExactValue_Float)) {
- operand->mode = Addressing_Value;
- // target_type = t_untyped_nil;
- operand->value = empty_exact_value;
- update_untyped_expr_value(c, operand->expr, operand->value);
- break;
- }
- }
- }
- // "fallthrough"
- if (!is_operand_nil(*operand) && !is_operand_uninit(*operand)) {
- TEMPORARY_ALLOCATOR_GUARD();
- isize count = t->Union.variants.count;
- ValidIndexAndScore *valids = gb_alloc_array(temporary_allocator(), ValidIndexAndScore, count);
- isize valid_count = 0;
- isize first_success_index = -1;
- for_array(i, t->Union.variants) {
- Type *vt = t->Union.variants[i];
- i64 score = 0;
- if (check_is_assignable_to_with_score(c, operand, vt, &score)) {
- valids[valid_count].index = i;
- valids[valid_count].score = score;
- valid_count += 1;
- if (first_success_index < 0) {
- first_success_index = i;
- }
- }
- }
- if (valid_count > 1) {
- gb_sort_array(valids, valid_count, valid_index_and_score_cmp);
- i64 best_score = valids[0].score;
- for (isize i = 1; i < valid_count; i++) {
- auto v = valids[i];
- if (best_score > v.score) {
- valid_count = i;
- break;
- }
- best_score = v.score;
- }
- first_success_index = valids[0].index;
- }
- gbString type_str = type_to_string(target_type);
- defer (gb_string_free(type_str));
- if (valid_count == 1) {
- Type *new_type = t->Union.variants[first_success_index];
- target_type = new_type;
- if (is_type_union(new_type)) {
- convert_to_typed(c, operand, new_type);
- break;
- }
- operand->type = new_type;
- operand->mode = Addressing_Value;
- break;
- } else if (valid_count > 1) {
- ERROR_BLOCK();
- GB_ASSERT(first_success_index >= 0);
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type, true);
- error_line("Ambiguous type conversion to '%s', which variant did you mean:\n\t", type_str);
- i32 j = 0;
- for (i32 i = 0; i < valid_count; i++) {
- ValidIndexAndScore valid = valids[i];
- if (j > 0 && valid_count > 2) error_line(", ");
- if (j == valid_count-1) {
- if (valid_count == 2) error_line(" ");
- error_line("or ");
- }
- gbString str = type_to_string(t->Union.variants[valid.index]);
- error_line("'%s'", str);
- gb_string_free(str);
- j++;
- }
- error_line("\n\n");
- return;
- } else if (is_type_untyped_uninit(operand->type)) {
- target_type = t_untyped_uninit;
- } else if (!is_type_untyped_nil(operand->type) || !type_has_nil(target_type)) {
- ERROR_BLOCK();
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type, true);
- if (count > 0) {
- error_line("'%s' is a union which only excepts the following types:\n", type_str);
- error_line("\t");
- for (i32 i = 0; i < count; i++) {
- Type *v = t->Union.variants[i];
- if (i > 0 && count > 2) error_line(", ");
- if (i == count-1) {
- if (count == 2) error_line(" ");
- if (count > 1) {
- error_line("or ");
- }
- }
- gbString str = type_to_string(v);
- error_line("'%s'", str);
- gb_string_free(str);
- }
- error_line("\n\n");
- }
- return;
- }
- }
- /* fallthrough */
- default:
- if (is_type_untyped_uninit(operand->type)) {
- target_type = t_untyped_uninit;
- } else if (is_type_untyped_nil(operand->type) && type_has_nil(target_type)) {
- target_type = t_untyped_nil;
- } else {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- }
-
- if (is_type_any(target_type) && is_type_untyped(operand->type)) {
- if (is_type_untyped_nil(operand->type) && is_type_untyped_uninit(operand->type)) {
- } else {
- target_type = default_type(operand->type);
- }
- }
- update_untyped_expr_type(c, operand->expr, target_type, true);
- operand->type = target_type;
- }
- gb_internal bool check_index_value(CheckerContext *c, Type *main_type, bool open_range, Ast *index_value, i64 max_count, i64 *value, Type *type_hint=nullptr) {
- Operand operand = {Addressing_Invalid};
- check_expr_with_type_hint(c, &operand, index_value, type_hint);
- if (operand.mode == Addressing_Invalid) {
- if (value) *value = 0;
- // NOTE(bill): return true here to propagate the errors better
- return true;
- }
- Type *index_type = t_int;
- if (type_hint != nullptr) {
- index_type = type_hint;
- }
- convert_to_typed(c, &operand, index_type);
- if (operand.mode == Addressing_Invalid) {
- if (value) *value = 0;
- return false;
- }
- if (type_hint != nullptr) {
- if (!check_is_assignable_to(c, &operand, type_hint)) {
- gbString expr_str = expr_to_string(operand.expr);
- gbString index_type_str = type_to_string(type_hint);
- error(operand.expr, "Index '%s' must be an enum of type '%s'", expr_str, index_type_str);
- gb_string_free(index_type_str);
- gb_string_free(expr_str);
- if (value) *value = 0;
- return false;
- }
- } else if (!is_type_integer(operand.type) && !is_type_enum(operand.type)) {
- gbString expr_str = expr_to_string(operand.expr);
- gbString type_str = type_to_string(operand.type);
- error(operand.expr, "Index '%s' must be an integer, got %s", expr_str, type_str);
- gb_string_free(type_str);
- gb_string_free(expr_str);
- if (value) *value = 0;
- return false;
- }
- if (operand.mode == Addressing_Constant &&
- (c->state_flags & StateFlag_no_bounds_check) == 0) {
- BigInt i = exact_value_to_integer(operand.value).value_integer;
- if (i.sign && !is_type_enum(index_type) && !is_type_multi_pointer(main_type)) {
- TEMPORARY_ALLOCATOR_GUARD();
- String idx_str = big_int_to_string(temporary_allocator(), &i);
- gbString expr_str = expr_to_string(operand.expr, temporary_allocator());
- error(operand.expr, "Index '%s' cannot be a negative value, got %.*s", expr_str, LIT(idx_str));
- if (value) *value = 0;
- return false;
- }
- if (max_count >= 0) {
- if (is_type_enum(index_type)) {
- Type *bt = base_type(index_type);
- GB_ASSERT(bt->kind == Type_Enum);
- ExactValue const &lo = *bt->Enum.min_value;
- ExactValue const &hi = *bt->Enum.max_value;
- String lo_str = {};
- String hi_str = {};
- if (bt->Enum.fields.count > 0) {
- isize lo_idx = gb_clamp(bt->Enum.min_value_index, 0, bt->Enum.fields.count - 1);
- isize hi_idx = gb_clamp(bt->Enum.max_value_index, 0, bt->Enum.fields.count - 1);
- lo_str = bt->Enum.fields[lo_idx]->token.string;
- hi_str = bt->Enum.fields[hi_idx]->token.string;
- }
- bool out_of_bounds = false;
- if (compare_exact_values(Token_Lt, operand.value, lo) || compare_exact_values(Token_Gt, operand.value, hi)) {
- out_of_bounds = true;
- }
- if (out_of_bounds) {
- gbString expr_str = expr_to_string(operand.expr);
- if (lo_str.len > 0) {
- error(operand.expr, "Index '%s' is out of bounds range %.*s ..= %.*s", expr_str, LIT(lo_str), LIT(hi_str));
- } else {
- gbString index_type_str = type_to_string(index_type);
- error(operand.expr, "Index '%s' is out of bounds range of enum type %s", expr_str, index_type_str);
- gb_string_free(index_type_str);
- }
- gb_string_free(expr_str);
- return false;
- }
- if (value) *value = exact_value_to_i64(exact_value_sub(operand.value, lo));
- return true;
- } else { // NOTE(bill): Do array bound checking
- i64 v = -1;
- if (i.used <= 1) {
- v = big_int_to_i64(&i);
- }
- if (value) *value = v;
- bool out_of_bounds = false;
- if (v < 0) {
- out_of_bounds = true;
- } else if (open_range) {
- out_of_bounds = v > max_count;
- } else {
- out_of_bounds = v >= max_count;
- }
- if (out_of_bounds) {
- TEMPORARY_ALLOCATOR_GUARD();
- String idx_str = big_int_to_string(temporary_allocator(), &i);
- gbString expr_str = expr_to_string(operand.expr, temporary_allocator());
- error(operand.expr, "Index '%s' is out of bounds range 0..<%lld, got %.*s", expr_str, max_count, LIT(idx_str));
- return false;
- }
- return true;
- }
- } else {
- if (value) *value = exact_value_to_i64(operand.value);
- return true;
- }
- }
- // NOTE(bill): It's alright :D
- if (value) *value = -1;
- return true;
- }
- gb_internal ExactValue get_constant_field_single(CheckerContext *c, ExactValue value, i32 index, bool *success_, bool *finish_) {
- if (value.kind == ExactValue_String) {
- GB_ASSERT(0 <= index && index < value.value_string.len);
- u8 val = value.value_string[index];
- if (success_) *success_ = true;
- if (finish_) *finish_ = true;
- return exact_value_u64(val);
- }
- if (value.kind != ExactValue_Compound) {
- if (success_) *success_ = true;
- if (finish_) *finish_ = true;
- return value;
- }
- Ast *node = value.value_compound;
- switch (node->kind) {
- case_ast_node(cl, CompoundLit, node);
- if (cl->elems.count == 0) {
- if (success_) *success_ = true;
- if (finish_) *finish_ = true;
- return empty_exact_value;
- }
- if (cl->elems[0]->kind == Ast_FieldValue) {
- if (is_type_struct(node->tav.type)) {
- bool found = false;
- for (Ast *elem : cl->elems) {
- if (elem->kind != Ast_FieldValue) {
- continue;
- }
- ast_node(fv, FieldValue, elem);
- String name = fv->field->Ident.token.string;
- Selection sub_sel = lookup_field(node->tav.type, name, false);
- defer (array_free(&sub_sel.index));
- if (sub_sel.index.count > 0 &&
- sub_sel.index[0] == index) {
- value = fv->value->tav.value;
- found = true;
- break;
- }
- }
- if (!found) {
- // Use the zero value if it is not found
- value = {};
- }
- } else if (is_type_array(node->tav.type) || is_type_enumerated_array(node->tav.type)) {
- for (Ast *elem : cl->elems) {
- if (elem->kind != Ast_FieldValue) {
- continue;
- }
- ast_node(fv, FieldValue, elem);
- if (is_ast_range(fv->field)) {
- ast_node(ie, BinaryExpr, fv->field);
- TypeAndValue lo_tav = ie->left->tav;
- TypeAndValue hi_tav = ie->right->tav;
- GB_ASSERT(lo_tav.mode == Addressing_Constant);
- GB_ASSERT(hi_tav.mode == Addressing_Constant);
- TokenKind op = ie->op.kind;
- i64 lo = exact_value_to_i64(lo_tav.value);
- i64 hi = exact_value_to_i64(hi_tav.value);
- i64 corrected_index = index;
- if (is_type_enumerated_array(node->tav.type)) {
- Type *bt = base_type(node->tav.type);
- GB_ASSERT(bt->kind == Type_EnumeratedArray);
- corrected_index = index + exact_value_to_i64(*bt->EnumeratedArray.min_value);
- }
- if (op != Token_RangeHalf) {
- if (lo <= corrected_index && corrected_index <= hi) {
- TypeAndValue tav = fv->value->tav;
- if (success_) *success_ = true;
- if (finish_) *finish_ = false;
- return tav.value;
- }
- } else {
- if (lo <= corrected_index && corrected_index < hi) {
- TypeAndValue tav = fv->value->tav;
- if (success_) *success_ = true;
- if (finish_) *finish_ = false;
- return tav.value;
- }
- }
- } else {
- TypeAndValue index_tav = fv->field->tav;
- GB_ASSERT(index_tav.mode == Addressing_Constant);
- ExactValue index_value = index_tav.value;
- if (is_type_enumerated_array(node->tav.type)) {
- Type *bt = base_type(node->tav.type);
- GB_ASSERT(bt->kind == Type_EnumeratedArray);
- index_value = exact_value_sub(index_value, *bt->EnumeratedArray.min_value);
- }
- i64 field_index = exact_value_to_i64(index_value);
- if (index == field_index) {
- TypeAndValue tav = fv->value->tav;
- if (success_) *success_ = true;
- if (finish_) *finish_ = false;
- return tav.value;
- }
- }
- }
- }
- } else {
- i32 count = (i32)cl->elems.count;
- if (count < index) {
- if (success_) *success_ = false;
- if (finish_) *finish_ = true;
- return empty_exact_value;
- }
- if (cl->elems.count <= index) {
- if (success_) *success_ = false;
- if (finish_) *finish_ = false;
- return value;
- }
- TypeAndValue tav = cl->elems[index]->tav;
- if (tav.mode == Addressing_Constant) {
- if (success_) *success_ = true;
- if (finish_) *finish_ = false;
- return tav.value;
- } else if (is_type_proc(tav.type)) {
- if (success_) *success_ = true;
- if (finish_) *finish_ = false;
- return tav.value;
- } else {
- GB_ASSERT_MSG(is_type_untyped_nil(tav.type), "%s", type_to_string(tav.type));
- if (success_) *success_ = true;
- if (finish_) *finish_ = false;
- return tav.value;
- }
- }
- case_end;
- default:
- if (success_) *success_ = true;
- if (finish_) *finish_ = true;
- return empty_exact_value;
- }
- if (finish_) *finish_ = false;
- return value;
- }
- gb_internal ExactValue get_constant_field(CheckerContext *c, Operand const *operand, Selection sel, bool *success_) {
- if (operand->mode != Addressing_Constant) {
- if (success_) *success_ = false;
- return empty_exact_value;
- }
- if (sel.indirect) {
- if (success_) *success_ = false;
- return empty_exact_value;
- }
- if (sel.index.count == 0) {
- if (success_) *success_ = false;
- return empty_exact_value;
- }
- ExactValue value = operand->value;
- if (value.kind == ExactValue_Compound) {
- while (sel.index.count > 0) {
- i32 index = sel.index[0];
- sel = sub_selection(sel, 1);
- bool finish = false;
- value = get_constant_field_single(c, value, index, success_, &finish);
- if (finish) {
- return value;
- }
- }
- if (success_) *success_ = true;
- return value;
- } else if (value.kind == ExactValue_Quaternion) {
- // @QuaternionLayout
- Quaternion256 q = *value.value_quaternion;
- GB_ASSERT(sel.index.count == 1);
- switch (sel.index[0]) {
- case 3: // w
- if (success_) *success_ = true;
- return exact_value_float(q.real);
- case 0: // x
- if (success_) *success_ = true;
- return exact_value_float(q.imag);
- case 1: // y
- if (success_) *success_ = true;
- return exact_value_float(q.jmag);
- case 2: // z
- if (success_) *success_ = true;
- return exact_value_float(q.kmag);
- }
- if (success_) *success_ = false;
- return empty_exact_value;
- } else if (value.kind == ExactValue_Complex) {
- // @QuaternionLayout
- Complex128 c = *value.value_complex;
- GB_ASSERT(sel.index.count == 1);
- switch (sel.index[0]) {
- case 0: // real
- if (success_) *success_ = true;
- return exact_value_float(c.real);
- case 1: // imag
- if (success_) *success_ = true;
- return exact_value_float(c.imag);
- }
- if (success_) *success_ = false;
- return empty_exact_value;
- }
- if (success_) *success_ = true;
- return empty_exact_value;
- }
- gb_internal Type *determine_swizzle_array_type(Type *original_type, Type *type_hint, isize new_count) {
- Type *array_type = base_type(type_deref(original_type));
- GB_ASSERT(array_type->kind == Type_Array || array_type->kind == Type_SimdVector);
- if (array_type->kind == Type_SimdVector) {
- Type *elem_type = array_type->SimdVector.elem;
- return alloc_type_simd_vector(new_count, elem_type);
- }
- Type *elem_type = array_type->Array.elem;
- Type *swizzle_array_type = nullptr;
- Type *bth = base_type(type_deref(type_hint));
- if (bth != nullptr && bth->kind == Type_Array &&
- bth->Array.count == new_count &&
- are_types_identical(bth->Array.elem, elem_type)) {
- swizzle_array_type = type_hint;
- } else {
- i64 max_count = array_type->Array.count;
- if (new_count == max_count) {
- swizzle_array_type = original_type;
- } else {
- swizzle_array_type = alloc_type_array(elem_type, new_count);
- }
- }
- return swizzle_array_type;
- }
- gb_internal bool is_entity_declared_for_selector(Entity *entity, Scope *import_scope, bool *allow_builtin) {
- bool is_declared = entity != nullptr;
- if (is_declared) {
- if (entity->kind == Entity_Builtin) {
- // NOTE(bill): Builtin's are in the universal scope which is part of every scopes hierarchy
- // This means that we should just ignore the found result through it
- *allow_builtin = entity->scope == import_scope ||
- (entity->scope != builtin_pkg->scope && entity->scope != intrinsics_pkg->scope);
- } else if ((entity->scope->flags&ScopeFlag_Global) == ScopeFlag_Global && (import_scope->flags&ScopeFlag_Global) == 0) {
- is_declared = false;
- }
- }
- return is_declared;
- }
- // NOTE(bill, 2022-02-03): see `check_const_decl` for why it exists reasoning
- gb_internal Entity *check_entity_from_ident_or_selector(CheckerContext *c, Ast *node, bool ident_only) {
- if (node == nullptr) {
- return nullptr;
- }
- /*if (node->kind == Ast_TernaryWhenExpr) {
- ast_node(we, TernaryWhenExpr, node);
- if (we->cond == nullptr) {
- return nullptr;
- }
- if (we->cond->tav.mode != Addressing_Constant) {
- return nullptr;
- }
- if (we->cond->tav.value.kind != ExactValue_Bool) {
- return nullptr;
- }
- if (we->cond->tav.value.value_bool) {
- return check_entity_from_ident_or_selector(c, we->x, ident_only);
- } else {
- Entity *e = check_entity_from_ident_or_selector(c, we->y, ident_only);
- return e;
- }
- } else */if (node->kind == Ast_Ident) {
- String name = node->Ident.token.string;
- return scope_lookup(c->scope, name);
- } else if (!ident_only) if (node->kind == Ast_SelectorExpr) {
- ast_node(se, SelectorExpr, node);
- if (se->token.kind == Token_ArrowRight) {
- return nullptr;
- }
- Ast *op_expr = se->expr;
- Ast *selector = unparen_expr(se->selector);
- if (selector == nullptr) {
- return nullptr;
- }
- if (selector->kind != Ast_Ident) {
- return nullptr;
- }
- Entity *entity = nullptr;
- Entity *expr_entity = nullptr;
- bool check_op_expr = true;
- if (op_expr->kind == Ast_Ident) {
- String op_name = op_expr->Ident.token.string;
- Entity *e = scope_lookup(c->scope, op_name);
- if (e == nullptr) {
- return nullptr;
- }
- add_entity_use(c, op_expr, e);
- expr_entity = e;
- if (e != nullptr && e->kind == Entity_ImportName && selector->kind == Ast_Ident) {
- // IMPORTANT NOTE(bill): This is very sloppy code but it's also very fragile
- // It pretty much needs to be in this order and this way
- // If you can clean this up, please do but be really careful
- String import_name = op_name;
- Scope *import_scope = e->ImportName.scope;
- String entity_name = selector->Ident.token.string;
- check_op_expr = false;
- entity = scope_lookup_current(import_scope, entity_name);
- bool allow_builtin = false;
- if (!is_entity_declared_for_selector(entity, import_scope, &allow_builtin)) {
- return nullptr;
- }
- check_entity_decl(c, entity, nullptr, nullptr);
- if (entity->kind == Entity_ProcGroup) {
- return entity;
- }
- GB_ASSERT_MSG(entity->type != nullptr, "%.*s (%.*s)", LIT(entity->token.string), LIT(entity_strings[entity->kind]));
- }
- }
- Operand operand = {};
- if (check_op_expr) {
- check_expr_base(c, &operand, op_expr, nullptr);
- if (operand.mode == Addressing_Invalid) {
- return nullptr;
- }
- }
- if (entity == nullptr && selector->kind == Ast_Ident) {
- String field_name = selector->Ident.token.string;
- if (is_type_dynamic_array(type_deref(operand.type))) {
- init_mem_allocator(c->checker);
- }
- auto sel = lookup_field(operand.type, field_name, operand.mode == Addressing_Type);
- entity = sel.entity;
- }
- if (entity != nullptr) {
- return entity;
- }
- }
- return nullptr;
- }
- gb_internal Entity *check_selector(CheckerContext *c, Operand *operand, Ast *node, Type *type_hint) {
- ast_node(se, SelectorExpr, node);
- bool check_op_expr = true;
- Entity *expr_entity = nullptr;
- Entity *entity = nullptr;
- Selection sel = {}; // NOTE(bill): Not used if it's an import name
- if (!c->allow_arrow_right_selector_expr && se->token.kind == Token_ArrowRight) {
- ERROR_BLOCK();
- error(node, "Illegal use of -> selector shorthand outside of a call");
- gbString x = expr_to_string(se->expr);
- gbString y = expr_to_string(se->selector);
- error_line("\tSuggestion: Did you mean '%s.%s'?\n", x, y);
- gb_string_free(y);
- gb_string_free(x);
- // TODO(bill): Should this terminate here or propagate onwards?
- // operand->mode = Addressing_Invalid;
- // operand->expr = node;
- // return nullptr;
- }
- operand->expr = node;
- Ast *op_expr = se->expr;
- Ast *selector = unparen_expr(se->selector);
- if (selector == nullptr) {
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- if (selector->kind != Ast_Ident) {
- error(selector, "Illegal selector kind: '%.*s'", LIT(ast_strings[selector->kind]));
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- if (op_expr->kind == Ast_Ident) {
- String op_name = op_expr->Ident.token.string;
- Entity *e = scope_lookup(c->scope, op_name);
- add_entity_use(c, op_expr, e);
- expr_entity = e;
- if (e != nullptr && (e->kind == Entity_Procedure || e->kind == Entity_ProcGroup) && selector->kind == Ast_Ident) {
- gbString sel_str = expr_to_string(selector);
- error(node, "'%s' is not declared by by '%.*s'", sel_str, LIT(e->token.string));
- gb_string_free(sel_str);
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- } else if (e != nullptr && e->kind == Entity_ImportName && selector->kind == Ast_Ident) {
- // IMPORTANT NOTE(bill): This is very sloppy code but it's also very fragile
- // It pretty much needs to be in this order and this way
- // If you can clean this up, please do but be really careful
- String import_name = op_name;
- Scope *import_scope = e->ImportName.scope;
- String entity_name = selector->Ident.token.string;
- if (import_scope == nullptr) {
- ERROR_BLOCK();
- error(node, "'%.*s' is not imported in this file, '%.*s' is unavailable", LIT(import_name), LIT(entity_name));
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- check_op_expr = false;
- entity = scope_lookup_current(import_scope, entity_name);
- bool allow_builtin = false;
- if (!is_entity_declared_for_selector(entity, import_scope, &allow_builtin)) {
- ERROR_BLOCK();
- error(node, "'%.*s' is not declared by '%.*s'", LIT(entity_name), LIT(import_name));
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- check_did_you_mean_scope(entity_name, import_scope);
- return nullptr;
- }
- if (!is_entity_exported(entity, allow_builtin)) {
- gbString sel_str = expr_to_string(selector);
- error(node, "'%s' is not exported by '%.*s'", sel_str, LIT(import_name));
- gb_string_free(sel_str);
- // NOTE(bill): make the state valid still, even if it's "invalid"
- // operand->mode = Addressing_Invalid;
- // operand->expr = node;
- // return nullptr;
- }
- check_entity_decl(c, entity, nullptr, nullptr);
- if (entity->kind == Entity_ProcGroup) {
- operand->mode = Addressing_ProcGroup;
- operand->proc_group = entity;
- add_type_and_value(c, operand->expr, operand->mode, operand->type, operand->value);
- return entity;
- }
- GB_ASSERT_MSG(entity->type != nullptr, "%.*s (%.*s)", LIT(entity->token.string), LIT(entity_strings[entity->kind]));
- }
- }
- if (check_op_expr) {
- check_expr_base(c, operand, op_expr, nullptr);
- if (operand->mode == Addressing_Invalid) {
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- }
- if (operand->type && is_type_soa_struct(type_deref(operand->type))) {
- complete_soa_type(c->checker, type_deref(operand->type), false);
- }
- if (entity == nullptr && selector->kind == Ast_Ident) {
- String field_name = selector->Ident.token.string;
- Type *t = type_deref(operand->type);
- if (t == nullptr) {
- error(operand->expr, "Cannot use a selector expression on 0-value expression");
- } else {
- if (is_type_dynamic_array(t)) {
- init_mem_allocator(c->checker);
- }
- sel = lookup_field(operand->type, field_name, operand->mode == Addressing_Type);
- entity = sel.entity;
- // NOTE(bill): Add type info needed for fields like 'names'
- if (entity != nullptr && (entity->flags&EntityFlag_TypeField)) {
- add_type_info_type(c, operand->type);
- }
- if (is_type_enum(operand->type)) {
- add_type_info_type(c, operand->type);
- }
- }
- }
- if (operand->type && is_type_simd_vector(type_deref(operand->type))) {
- String field_name = selector->Ident.token.string;
- if (field_name.len == 1) {
- error(op_expr, "Extracting an element from a #simd array using .%.*s syntax is disallowed, prefer `simd.extract`", LIT(field_name));
- } else {
- error(op_expr, "Extracting elements from a #simd array using .%.*s syntax is disallowed, prefer `swizzle`", LIT(field_name));
- }
- return nullptr;
- }
- if (entity == nullptr && selector->kind == Ast_Ident && operand->type != nullptr &&
- (is_type_array(type_deref(operand->type)))) {
- String field_name = selector->Ident.token.string;
- if (1 < field_name.len && field_name.len <= 4) {
- u8 swizzles_xyzw[4] = {'x', 'y', 'z', 'w'};
- u8 swizzles_rgba[4] = {'r', 'g', 'b', 'a'};
- bool found_xyzw = false;
- bool found_rgba = false;
- for (isize i = 0; i < field_name.len; i++) {
- bool valid = false;
- for (isize j = 0; j < 4; j++) {
- if (field_name.text[i] == swizzles_xyzw[j]) {
- found_xyzw = true;
- valid = true;
- break;
- }
- if (field_name.text[i] == swizzles_rgba[j]) {
- found_rgba = true;
- valid = true;
- break;
- }
- }
- if (!valid) {
- goto end_of_array_selector_swizzle;
- }
- }
- u8 *swizzles = nullptr;
- u8 index_count = cast(u8)field_name.len;
- if (found_xyzw && found_rgba) {
- gbString op_str = expr_to_string(op_expr);
- error(op_expr, "Mixture of swizzle kinds for field index, got %s", op_str);
- gb_string_free(op_str);
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- u8 indices = 0;
- if (found_xyzw) {
- swizzles = swizzles_xyzw;
- } else if (found_rgba) {
- swizzles = swizzles_rgba;
- }
- for (isize i = 0; i < field_name.len; i++) {
- for (isize j = 0; j < 4; j++) {
- if (field_name.text[i] == swizzles[j]) {
- indices |= cast(u8)(j)<<(i*2);
- break;
- }
- }
- }
- Type *original_type = operand->type;
- Type *array_type = base_type(type_deref(original_type));
- GB_ASSERT(array_type->kind == Type_Array);
- i64 array_count = get_array_type_count(array_type);
- for (u8 i = 0; i < index_count; i++) {
- u8 idx = indices>>(i*2) & 3;
- if (idx >= array_count) {
- char c = 0;
- if (found_xyzw) {
- c = swizzles_xyzw[idx];
- } else if (found_rgba) {
- c = swizzles_rgba[idx];
- } else {
- GB_PANIC("unknown swizzle kind");
- }
- error(selector->Ident.token, "Swizzle value is out of bounds, got %c, max count %lld", c, array_count);
- break;
- }
- }
- se->swizzle_count = index_count;
- se->swizzle_indices = indices;
- AddressingMode prev_mode = operand->mode;
- operand->mode = Addressing_SwizzleValue;
- operand->type = determine_swizzle_array_type(original_type, type_hint, index_count);
- operand->expr = node;
- switch (prev_mode) {
- case Addressing_Variable:
- case Addressing_SoaVariable:
- case Addressing_SwizzleVariable:
- operand->mode = Addressing_SwizzleVariable;
- break;
- case Addressing_Value:
- if (is_type_pointer(original_type)) {
- operand->mode = Addressing_SwizzleVariable;
- }
- break;
- }
- Entity *swizzle_entity = alloc_entity_variable(nullptr, make_token_ident(field_name), operand->type, EntityState_Resolved);
- add_type_and_value(c, operand->expr, operand->mode, operand->type, operand->value);
- return swizzle_entity;
- }
- end_of_array_selector_swizzle:;
- }
- if (entity == nullptr) {
- gbString op_str = expr_to_string(op_expr);
- gbString type_str = type_to_string_shorthand(operand->type);
- gbString sel_str = expr_to_string(selector);
- if (operand->mode == Addressing_Type) {
- if (is_type_polymorphic(operand->type, true)) {
- error(op_expr, "Type '%s' has no field nor polymorphic parameter '%s'", op_str, sel_str);
- } else {
- error(op_expr, "Type '%s' has no field '%s'", op_str, sel_str);
- }
- } else {
- ERROR_BLOCK();
- error(op_expr, "'%s' of type '%s' has no field '%s'", op_str, type_str, sel_str);
- if (operand->type != nullptr && selector->kind == Ast_Ident) {
- String const &name = selector->Ident.token.string;
- Type *bt = base_type(operand->type);
- if (operand->type->kind == Type_Named &&
- operand->type->Named.type_name &&
- operand->type->Named.type_name->kind == Entity_TypeName &&
- operand->type->Named.type_name->TypeName.objc_metadata) {
- check_did_you_mean_objc_entity(name, operand->type->Named.type_name, operand->mode == Addressing_Type);
- } else if (bt->kind == Type_Struct) {
- check_did_you_mean_type(name, bt->Struct.fields);
- } else if (bt->kind == Type_Enum) {
- check_did_you_mean_type(name, bt->Enum.fields);
- }
- }
- }
- gb_string_free(sel_str);
- gb_string_free(type_str);
- gb_string_free(op_str);
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- if (expr_entity != nullptr && expr_entity->kind == Entity_Constant && entity->kind != Entity_Constant) {
- bool success = false;
- ExactValue field_value = get_constant_field(c, operand, sel, &success);
- if (success) {
- operand->mode = Addressing_Constant;
- operand->expr = node;
- operand->value = field_value;
- operand->type = entity->type;
- add_entity_use(c, selector, entity);
- add_type_and_value(c, operand->expr, operand->mode, operand->type, operand->value);
- return entity;
- }
- gbString op_str = expr_to_string(op_expr);
- gbString type_str = type_to_string_shorthand(operand->type);
- gbString sel_str = expr_to_string(selector);
- error(op_expr, "Cannot access non-constant field '%s' from '%s'", sel_str, op_str);
- gb_string_free(sel_str);
- gb_string_free(type_str);
- gb_string_free(op_str);
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- if (operand->mode == Addressing_Constant && entity->kind != Entity_Constant) {
- bool success = false;
- ExactValue field_value = get_constant_field(c, operand, sel, &success);
- if (success) {
- operand->mode = Addressing_Constant;
- operand->expr = node;
- operand->value = field_value;
- operand->type = entity->type;
- add_entity_use(c, selector, entity);
- add_type_and_value(c, operand->expr, operand->mode, operand->type, operand->value);
- return entity;
- }
- gbString op_str = expr_to_string(op_expr);
- gbString type_str = type_to_string_shorthand(operand->type);
- gbString sel_str = expr_to_string(selector);
- error(op_expr, "Cannot access non-constant field '%s' from '%s'", sel_str, op_str);
- gb_string_free(sel_str);
- gb_string_free(type_str);
- gb_string_free(op_str);
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- if (expr_entity != nullptr && is_type_polymorphic(expr_entity->type)) {
- gbString op_str = expr_to_string(op_expr);
- gbString type_str = type_to_string_shorthand(operand->type);
- gbString sel_str = expr_to_string(selector);
- error(op_expr, "Cannot access field '%s' from non-specialized polymorphic type '%s'", sel_str, op_str);
- gb_string_free(sel_str);
- gb_string_free(type_str);
- gb_string_free(op_str);
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- add_entity_use(c, selector, entity);
- operand->type = entity->type;
- operand->expr = node;
- if (entity->flags & EntityFlag_BitFieldField) {
- add_package_dependency(c, "runtime", "__write_bits");
- add_package_dependency(c, "runtime", "__read_bits");
- }
- switch (entity->kind) {
- case Entity_Constant:
- operand->value = entity->Constant.value;
- operand->mode = Addressing_Constant;
- if (operand->value.kind == ExactValue_Procedure) {
- Entity *proc = strip_entity_wrapping(operand->value.value_procedure);
- if (proc != nullptr) {
- operand->mode = Addressing_Value;
- operand->type = proc->type;
- }
- }
- break;
- case Entity_Variable:
- if (sel.is_bit_field) {
- se->is_bit_field = true;
- }
- if (sel.indirect) {
- operand->mode = Addressing_Variable;
- } else if (operand->mode == Addressing_Context) {
- // Do nothing
- } else if (operand->mode == Addressing_MapIndex) {
- operand->mode = Addressing_Value;
- } else if (entity->flags & EntityFlag_SoaPtrField) {
- operand->mode = Addressing_SoaVariable;
- } else if (operand->mode == Addressing_OptionalOk || operand->mode == Addressing_OptionalOkPtr) {
- operand->mode = Addressing_Value;
- } else if (operand->mode == Addressing_SoaVariable) {
- operand->mode = Addressing_Variable;
- } else if (operand->mode != Addressing_Value) {
- operand->mode = Addressing_Variable;
- } else {
- operand->mode = Addressing_Value;
- }
- break;
- case Entity_TypeName:
- operand->mode = Addressing_Type;
- break;
- case Entity_Procedure:
- operand->mode = Addressing_Value;
- operand->value = exact_value_procedure(node);
- break;
- case Entity_Builtin:
- operand->mode = Addressing_Builtin;
- operand->builtin_id = cast(BuiltinProcId)entity->Builtin.id;
- break;
- case Entity_ProcGroup:
- operand->mode = Addressing_ProcGroup;
- operand->proc_group = entity;
- break;
- // NOTE(bill): These cases should never be hit but are here for sanity reasons
- case Entity_Nil:
- operand->mode = Addressing_Value;
- break;
- }
- add_type_and_value(c, operand->expr, operand->mode, operand->type, operand->value);
- return entity;
- }
- gb_internal bool is_type_normal_pointer(Type *ptr, Type **elem) {
- ptr = base_type(ptr);
- if (is_type_pointer(ptr)) {
- if (is_type_rawptr(ptr)) {
- return false;
- }
- if (elem) *elem = ptr->Pointer.elem;
- return true;
- }
- return false;
- }
- gb_internal bool is_type_valid_atomic_type(Type *elem) {
- elem = core_type(elem);
- if (is_type_internally_pointer_like(elem)) {
- return true;
- }
- if (elem->kind == Type_BitSet) {
- elem = bit_set_to_int(elem);
- }
- if (elem->kind != Type_Basic) {
- return false;
- }
- return (elem->Basic.flags & (BasicFlag_Boolean|BasicFlag_OrderedNumeric)) != 0;
- }
- gb_internal bool check_identifier_exists(Scope *s, Ast *node, bool nested = false, Scope **out_scope = nullptr) {
- switch (node->kind) {
- case_ast_node(i, Ident, node);
- String name = i->token.string;
- if (nested) {
- Entity *e = scope_lookup_current(s, name);
- if (e != nullptr) {
- if (out_scope) *out_scope = e->scope;
- return true;
- }
- } else {
- Entity *e = scope_lookup(s, name);
- if (e != nullptr) {
- if (out_scope) *out_scope = e->scope;
- return true;
- }
- }
- case_end;
- case_ast_node(se, SelectorExpr, node);
- Ast *lhs = se->expr;
- Ast *rhs = se->selector;
- Scope *lhs_scope = nullptr;
- if (check_identifier_exists(s, lhs, nested, &lhs_scope)) {
- return check_identifier_exists(lhs_scope, rhs, true);
- }
- case_end;
- }
- return false;
- }
- gb_internal bool check_assignment_arguments(CheckerContext *ctx, Array<Operand> const &lhs, Array<Operand> *operands, Slice<Ast *> const &rhs) {
- bool optional_ok = false;
- isize tuple_index = 0;
- for (Ast *rhs_expr : rhs) {
- CheckerContext c_ = *ctx;
- CheckerContext *c = &c_;
- Operand o = {};
- Type *type_hint = nullptr;
- if (tuple_index < lhs.count) {
- type_hint = lhs[tuple_index].type;
- }
- check_expr_base(c, &o, rhs_expr, type_hint);
- if (o.mode == Addressing_NoValue) {
- error_operand_no_value(&o);
- o.mode = Addressing_Invalid;
- }
- if (o.type == nullptr || o.type->kind != Type_Tuple) {
- if (lhs.count == 2 && rhs.count == 1 &&
- (o.mode == Addressing_MapIndex || o.mode == Addressing_OptionalOk || o.mode == Addressing_OptionalOkPtr)) {
- Ast *expr = unparen_expr(o.expr);
- Operand val0 = o;
- Operand val1 = o;
- val0.mode = Addressing_Value;
- val1.mode = Addressing_Value;
- val1.type = t_untyped_bool;
- check_promote_optional_ok(c, &o, nullptr, &val1.type);
- if (expr->kind == Ast_TypeAssertion &&
- (o.mode == Addressing_OptionalOk || o.mode == Addressing_OptionalOkPtr)) {
- // NOTE(bill): Used only for optimizations in the backend
- if (is_blank_ident(lhs[0].expr)) {
- expr->TypeAssertion.ignores[0] = true;
- }
- if (is_blank_ident(lhs[1].expr)) {
- expr->TypeAssertion.ignores[1] = true;
- }
- }
- array_add(operands, val0);
- array_add(operands, val1);
- optional_ok = true;
- tuple_index += 2;
- } else if (o.mode == Addressing_OptionalOk && is_type_tuple(o.type)) {
- Type *tuple = o.type;
- GB_ASSERT(tuple->Tuple.variables.count == 2);
- Ast *expr = unparen_expr(o.expr);
- if (expr->kind == Ast_CallExpr) {
- expr->CallExpr.optional_ok_one = true;
- }
- Operand val = o;
- val.type = tuple->Tuple.variables[0]->type;
- val.mode = Addressing_Value;
- array_add(operands, val);
- tuple_index += tuple->Tuple.variables.count;
- } else {
- array_add(operands, o);
- tuple_index += 1;
- }
- } else {
- TypeTuple *tuple = &o.type->Tuple;
- for (Entity *e : tuple->variables) {
- o.type = e->type;
- array_add(operands, o);
- }
- tuple_index += tuple->variables.count;
- }
- }
- return optional_ok;
- }
- typedef u32 UnpackFlags;
- enum UnpackFlag : u32 {
- UnpackFlag_None = 0,
- UnpackFlag_AllowOk = 1<<0,
- UnpackFlag_AllowUndef = 1<<1,
- };
- gb_internal bool check_unpack_arguments(CheckerContext *ctx, Entity **lhs, isize lhs_count, Array<Operand> *operands, Slice<Ast *> const &rhs_arguments, UnpackFlags flags,
- isize variadic_index = -1) {
- auto const &add_dependencies_from_unpacking = [](CheckerContext *c, Entity **lhs, isize lhs_count, isize tuple_index, isize tuple_count) -> isize {
- if (lhs == nullptr || c->decl == nullptr) {
- return tuple_count;
- }
- for (isize j = 0; (tuple_index + j) < lhs_count && j < tuple_count; j++) {
- Entity *e = lhs[tuple_index + j];
- if (e == nullptr) {
- continue;
- }
- DeclInfo *decl = decl_info_of_entity(e);
- if (decl == nullptr) {
- continue;
- }
- rw_mutex_shared_lock(&decl->deps_mutex);
- rw_mutex_lock(&c->decl->deps_mutex);
- for (Entity *dep : decl->deps) {
- ptr_set_add(&c->decl->deps, dep);
- }
- rw_mutex_unlock(&c->decl->deps_mutex);
- rw_mutex_shared_unlock(&decl->deps_mutex);
- }
- return tuple_count;
- };
- bool allow_ok = (flags & UnpackFlag_AllowOk) != 0;
- bool allow_undef = (flags & UnpackFlag_AllowUndef) != 0;
- bool is_variadic = variadic_index > -1;
- if (!is_variadic) {
- variadic_index = lhs_count;
- }
- bool optional_ok = false;
- isize tuple_index = 0;
- for (Ast *rhs : rhs_arguments) {
- if (rhs->kind == Ast_FieldValue) {
- error(rhs, "Invalid use of 'field = value'");
- rhs = rhs->FieldValue.value;
- }
- CheckerContext c_ = *ctx;
- CheckerContext *c = &c_;
- Operand o = {};
- Type *type_hint = nullptr;
- if (lhs != nullptr) {
- if (tuple_index < variadic_index) {
- // NOTE(bill): override DeclInfo for dependency
- Entity *e = lhs[tuple_index];
- if (e != nullptr) {
- type_hint = e->type;
- }
- } else if (is_variadic) {
- Entity *e = lhs[variadic_index];
- if (e != nullptr) {
- GB_ASSERT(e->flags & EntityFlag_Ellipsis);
- GB_ASSERT(is_type_slice(e->type));
- type_hint = e->type->Slice.elem;
- }
- }
- }
- Ast *rhs_expr = unparen_expr(rhs);
- if (allow_undef && rhs_expr != nullptr && rhs_expr->kind == Ast_Uninit) {
- // NOTE(bill): Just handle this very specific logic here
- o.type = t_untyped_uninit;
- o.mode = Addressing_Value;
- o.expr = rhs;
- add_type_and_value(c, rhs, o.mode, o.type, o.value);
- } else {
- check_expr_base(c, &o, rhs, type_hint);
- }
- if (o.mode == Addressing_NoValue) {
- error_operand_no_value(&o);
- o.mode = Addressing_Invalid;
- }
- if (o.type == nullptr || o.type->kind != Type_Tuple) {
- if (allow_ok && lhs_count == 2 && rhs_arguments.count == 1 &&
- (o.mode == Addressing_MapIndex || o.mode == Addressing_OptionalOk || o.mode == Addressing_OptionalOkPtr)) {
- Ast *expr = unparen_expr(o.expr);
- Operand val0 = o;
- Operand val1 = o;
- val0.mode = Addressing_Value;
- val1.mode = Addressing_Value;
- val1.type = t_untyped_bool;
- check_promote_optional_ok(c, &o, nullptr, &val1.type);
- if (expr->kind == Ast_TypeAssertion &&
- (o.mode == Addressing_OptionalOk || o.mode == Addressing_OptionalOkPtr)) {
- // NOTE(bill): Used only for optimizations in the backend
- if (is_blank_ident(lhs[0]->token)) {
- expr->TypeAssertion.ignores[0] = true;
- }
- if (is_blank_ident(lhs[1]->token)) {
- expr->TypeAssertion.ignores[1] = true;
- }
- }
- array_add(operands, val0);
- array_add(operands, val1);
- optional_ok = true;
- tuple_index += add_dependencies_from_unpacking(c, lhs, lhs_count, tuple_index, 2);
- } else {
- array_add(operands, o);
- tuple_index += 1;
- }
- } else {
- TypeTuple *tuple = &o.type->Tuple;
- for (Entity *e : tuple->variables) {
- o.type = e->type;
- array_add(operands, o);
- }
- isize count = tuple->variables.count;
- tuple_index += add_dependencies_from_unpacking(c, lhs, lhs_count, tuple_index, count);
- }
- }
- return optional_ok;
- }
- gb_internal isize get_procedure_param_count_excluding_defaults(Type *pt, isize *param_count_) {
- GB_ASSERT(pt != nullptr);
- GB_ASSERT(pt->kind == Type_Proc);
- isize param_count = 0;
- isize param_count_excluding_defaults = 0;
- bool variadic = pt->Proc.variadic;
- TypeTuple *param_tuple = nullptr;
- if (pt->Proc.params != nullptr) {
- param_tuple = &pt->Proc.params->Tuple;
- param_count = param_tuple->variables.count;
- if (variadic) {
- for (isize i = param_count-1; i >= 0; i--) {
- Entity *e = param_tuple->variables[i];
- if (e->kind == Entity_TypeName) {
- break;
- }
- if (e->kind == Entity_Variable) {
- if (e->Variable.param_value.kind != ParameterValue_Invalid) {
- param_count--;
- continue;
- }
- }
- break;
- }
- param_count--;
- }
- }
- param_count_excluding_defaults = param_count;
- if (param_tuple != nullptr) {
- for (isize i = param_count-1; i >= 0; i--) {
- Entity *e = param_tuple->variables[i];
- if (e->kind == Entity_TypeName) {
- break;
- }
- if (e->kind == Entity_Variable) {
- if (e->Variable.param_value.kind != ParameterValue_Invalid) {
- param_count_excluding_defaults--;
- continue;
- }
- }
- break;
- }
- }
- if (param_count_) *param_count_ = param_count;
- return param_count_excluding_defaults;
- }
- gb_internal isize lookup_procedure_parameter(TypeProc *pt, String const ¶meter_name) {
- isize param_count = pt->param_count;
- for (isize i = 0; i < param_count; i++) {
- Entity *e = pt->params->Tuple.variables[i];
- String name = e->token.string;
- if (is_blank_ident(name)) {
- continue;
- }
- if (name == parameter_name) {
- return i;
- }
- }
- return -1;
- }
- gb_internal isize lookup_procedure_parameter(Type *type, String const ¶meter_name) {
- type = base_type(type);
- GB_ASSERT(type->kind == Type_Proc);
- return lookup_procedure_parameter(&type->Proc, parameter_name);
- }
- gb_internal CallArgumentError check_call_arguments_internal(CheckerContext *c, Ast *call,
- Entity *entity, Type *proc_type,
- Array<Operand> positional_operands, Array<Operand> const &named_operands,
- CallArgumentErrorMode show_error_mode,
- CallArgumentData *data) {
- TEMPORARY_ALLOCATOR_GUARD();
- CallArgumentError err = CallArgumentError_None;
- ast_node(ce, CallExpr, call);
- GB_ASSERT(is_type_proc(proc_type));
- proc_type = base_type(proc_type);
- TypeProc *pt = &proc_type->Proc;
- isize param_count = 0;
- isize param_count_excluding_defaults = get_procedure_param_count_excluding_defaults(proc_type, ¶m_count);
- bool variadic = pt->variadic;
- bool vari_expand = (ce->ellipsis.pos.line != 0);
- i64 score = 0;
- bool show_error = show_error_mode == CallArgumentErrorMode::ShowErrors;
- Type *final_proc_type = proc_type;
- Entity *gen_entity = nullptr;
- if (vari_expand && !variadic) {
- if (show_error) {
- error(ce->ellipsis,
- "Cannot use '..' in call to a non-variadic procedure: '%.*s'",
- LIT(ce->proc->Ident.token.string));
- }
- err = CallArgumentError_NonVariadicExpand;
- } else if (vari_expand && pt->c_vararg) {
- if (show_error) {
- error(ce->ellipsis,
- "Cannot use '..' in call to a '#c_vararg' variadic procedure: '%.*s'",
- LIT(ce->proc->Ident.token.string));
- }
- err = CallArgumentError_NonVariadicExpand;
- }
- GB_ASSERT(ce->split_args);
- auto visited = slice_make<bool>(temporary_allocator(), pt->param_count);
- auto ordered_operands = array_make<Operand>(temporary_allocator(), pt->param_count);
- defer ({
- for (Operand const &o : ordered_operands) {
- if (o.expr != nullptr) {
- call->viral_state_flags |= o.expr->viral_state_flags;
- }
- }
- });
- isize positional_operand_count = positional_operands.count;
- if (variadic) {
- positional_operand_count = gb_min(positional_operands.count, pt->variadic_index);
- } else if (positional_operand_count > pt->param_count) {
- err = CallArgumentError_TooManyArguments;
- char const *err_fmt = "Too many arguments for '%s', expected %td arguments, got %td";
- if (show_error) {
- gbString proc_str = expr_to_string(ce->proc);
- defer (gb_string_free(proc_str));
- error(call, err_fmt, proc_str, param_count_excluding_defaults, positional_operands.count);
- }
- return err;
- }
- positional_operand_count = gb_min(positional_operand_count, pt->param_count);
- for (isize i = 0; i < positional_operand_count; i++) {
- ordered_operands[i] = positional_operands[i];
- visited[i] = true;
- }
- auto variadic_operands = slice(slice_from_array(positional_operands), positional_operand_count, positional_operands.count);
- bool named_variadic_param = false;
- if (named_operands.count != 0) {
- GB_ASSERT(ce->split_args->named.count == named_operands.count);
- for_array(i, ce->split_args->named) {
- Ast *arg = ce->split_args->named[i];
- Operand operand = named_operands[i];
- ast_node(fv, FieldValue, arg);
- if (fv->field->kind != Ast_Ident) {
- if (show_error) {
- gbString expr_str = expr_to_string(fv->field);
- error(arg, "Invalid parameter name '%s' in procedure call", expr_str);
- gb_string_free(expr_str);
- }
- err = CallArgumentError_InvalidFieldValue;
- continue;
- }
- String name = fv->field->Ident.token.string;
- isize param_index = lookup_procedure_parameter(pt, name);
- if (param_index < 0) {
- if (show_error) {
- error(arg, "No parameter named '%.*s' for this procedure type", LIT(name));
- }
- err = CallArgumentError_ParameterNotFound;
- continue;
- }
- if (pt->variadic && param_index == pt->variadic_index) {
- named_variadic_param = true;
- }
- if (visited[param_index]) {
- if (show_error) {
- error(arg, "Duplicate parameter '%.*s' in procedure call", LIT(name));
- }
- err = CallArgumentError_DuplicateParameter;
- continue;
- }
- visited[param_index] = true;
- ordered_operands[param_index] = operand;
- }
- }
- isize dummy_argument_count = 0;
- bool actually_variadic = false;
- if (variadic) {
- if (visited[pt->variadic_index] &&
- positional_operand_count < positional_operands.count) {
- if (show_error) {
- String name = pt->params->Tuple.variables[pt->variadic_index]->token.string;
- error(call, "Variadic parameters already handled with a named argument '%.*s' in procedure call", LIT(name));
- }
- err = CallArgumentError_DuplicateParameter;
- } else if (!visited[pt->variadic_index]) {
- visited[pt->variadic_index] = true;
- Operand *variadic_operand = &ordered_operands[pt->variadic_index];
- if (vari_expand) {
- if (variadic_operands.count == 0) {
- error(call, "'..' in the wrong position");
- } else {
- GB_ASSERT(variadic_operands.count != 0);
- *variadic_operand = variadic_operands[0];
- variadic_operand->type = default_type(variadic_operand->type);
- actually_variadic = true;
- }
- } else {
- AstFile *f = call->file();
- // HACK(bill): this is an awful hack
- Operand o = {};
- o.mode = Addressing_Value;
- o.expr = ast_ident(f, make_token_ident("nil"));
- o.expr->Ident.token.pos = ast_token(call).pos;
- if (variadic_operands.count != 0) {
- actually_variadic = true;
- o.expr->Ident.token.pos = ast_token(variadic_operands[0].expr).pos;
- Entity *vt = pt->params->Tuple.variables[pt->variadic_index];
- o.type = vt->type;
- } else {
- dummy_argument_count += 1;
- o.type = t_untyped_nil;
- }
- *variadic_operand = o;
- }
- }
- }
- for (isize i = 0; i < pt->param_count; i++) {
- if (!visited[i]) {
- Entity *e = pt->params->Tuple.variables[i];
- bool context_allocator_error = false;
- if (e->kind == Entity_Variable) {
- if (e->Variable.param_value.kind != ParameterValue_Invalid) {
- if (ast_file_vet_explicit_allocators(c->file)) {
- // NOTE(lucas): check if we are trying to default to context.allocator or context.temp_allocator
- if (e->Variable.param_value.original_ast_expr->kind == Ast_SelectorExpr) {
- auto& expr = e->Variable.param_value.original_ast_expr->SelectorExpr.expr;
- auto& selector = e->Variable.param_value.original_ast_expr->SelectorExpr.selector;
- if (expr->kind == Ast_Implicit &&
- expr->Implicit.string == STR_LIT("context") &&
- selector->kind == Ast_Ident &&
- (selector->Ident.token.string == STR_LIT("allocator") ||
- selector->Ident.token.string == STR_LIT("temp_allocator"))) {
- context_allocator_error = true;
- }
- }
- }
- if (!context_allocator_error) {
- ordered_operands[i].mode = Addressing_Value;
- ordered_operands[i].type = e->type;
- ordered_operands[i].expr = e->Variable.param_value.original_ast_expr;
- dummy_argument_count += 1;
- score += assign_score_function(1);
- continue;
- }
- }
- }
- if (show_error) {
- if (context_allocator_error) {
- gbString str = type_to_string(e->type);
- error(call, "Parameter '%.*s' of type '%s' must be explicitly provided in procedure call",
- LIT(e->token.string), str);
- gb_string_free(str);
- } else if (e->kind == Entity_TypeName) {
- error(call, "Type parameter '%.*s' is missing in procedure call",
- LIT(e->token.string));
- } else if (e->kind == Entity_Constant && e->Constant.value.kind != ExactValue_Invalid) {
- // Ignore
- } else {
- gbString str = type_to_string(e->type);
- error(call, "Parameter '%.*s' of type '%s' is missing in procedure call",
- LIT(e->token.string), str);
- gb_string_free(str);
- }
- }
- err = CallArgumentError_ParameterMissing;
- }
- }
- auto eval_param_and_score = [](CheckerContext *c, Operand *o, Type *param_type, CallArgumentError &err, bool param_is_variadic, Entity *e, bool show_error) -> i64 {
- bool allow_array_programming = !(e && (e->flags & EntityFlag_NoBroadcast));
- i64 s = 0;
- if (!check_is_assignable_to_with_score(c, o, param_type, &s, param_is_variadic, allow_array_programming)) {
- bool ok = false;
- if (e && (e->flags & EntityFlag_AnyInt)) {
- if (is_type_integer(param_type)) {
- ok = check_is_castable_to(c, o, param_type);
- }
- }
- if (!allow_array_programming && check_is_assignable_to_with_score(c, o, param_type, nullptr, param_is_variadic, !allow_array_programming)) {
- if (show_error) {
- error(o->expr, "'#no_broadcast' disallows automatic broadcasting a value across all elements of an array-like type in a procedure argument");
- }
- }
- if (ok) {
- s = assign_score_function(MAXIMUM_TYPE_DISTANCE);
- } else {
- if (show_error) {
- check_assignment(c, o, param_type, str_lit("procedure argument"));
- }
- err = CallArgumentError_WrongTypes;
- }
- } else if (show_error) {
- check_assignment(c, o, param_type, str_lit("procedure argument"));
- }
- if (e && e->flags & EntityFlag_ConstInput) {
- if (o->mode != Addressing_Constant) {
- if (show_error) {
- error(o->expr, "Expected a constant value for the argument '%.*s'", LIT(e->token.string));
- }
- err = CallArgumentError_NoneConstantParameter;
- }
- }
- if (!err && is_type_any(param_type)) {
- add_type_info_type(c, o->type);
- }
- if (o->mode == Addressing_Type && is_type_typeid(param_type)) {
- add_type_info_type(c, o->type);
- add_type_and_value(c, o->expr, Addressing_Value, param_type, exact_value_typeid(o->type));
- } else if (show_error && is_type_untyped(o->type)) {
- update_untyped_expr_type(c, o->expr, param_type, true);
- }
- return s;
- };
- if (ordered_operands.count == 0 && param_count_excluding_defaults == 0) {
- err = CallArgumentError_None;
- if (variadic) {
- GB_ASSERT(pt->params != nullptr && pt->params->Tuple.variables.count > 0);
- Type *t = pt->params->Tuple.variables[0]->type;
- if (is_type_polymorphic(t)) {
- if (show_error) {
- error(call, "Ambiguous call to a polymorphic variadic procedure with no variadic input");
- }
- err = CallArgumentError_AmbiguousPolymorphicVariadic;
- }
- }
- } else {
- if (pt->is_polymorphic && !pt->is_poly_specialized && err == CallArgumentError_None) {
- PolyProcData poly_proc_data = {};
- if (find_or_generate_polymorphic_procedure_from_parameters(c, entity, &ordered_operands, call, &poly_proc_data)) {
- gen_entity = poly_proc_data.gen_entity;
- Type *gept = base_type(gen_entity->type);
- GB_ASSERT(is_type_proc(gept));
- final_proc_type = gen_entity->type;
- pt = &gept->Proc;
- } else {
- err = CallArgumentError_WrongTypes;
- }
- }
- for (isize i = 0; i < pt->param_count; i++) {
- Operand *o = &ordered_operands[i];
- if (o->mode == Addressing_Invalid) {
- continue;
- }
- Entity *e = pt->params->Tuple.variables[i];
- bool param_is_variadic = pt->variadic && pt->variadic_index == i;
- if (e->kind == Entity_TypeName) {
- GB_ASSERT(pt->is_polymorphic);
- if (o->mode != Addressing_Type) {
- if (show_error) {
- error(o->expr, "Expected a type for the argument '%.*s'", LIT(e->token.string));
- }
- err = CallArgumentError_WrongTypes;
- }
- if (are_types_identical(e->type, o->type)) {
- score += assign_score_function(1);
- } else {
- score += assign_score_function(MAXIMUM_TYPE_DISTANCE);
- }
- continue;
- }
- if (param_is_variadic) {
- if (!named_variadic_param) {
- continue;
- }
- }
- score += eval_param_and_score(c, o, e->type, err, false, e, show_error);
- }
- }
- if (variadic) {
- Entity *var_entity = pt->params->Tuple.variables[pt->variadic_index];
- Type *slice = var_entity->type;
- GB_ASSERT(is_type_slice(slice));
- Type *elem = base_type(slice)->Slice.elem;
- Type *t = elem;
- if (is_type_polymorphic(t)) {
- if (show_error) {
- error(call, "Ambiguous call to a polymorphic variadic procedure with no variadic input %s", type_to_string(final_proc_type));
- }
- err = CallArgumentError_AmbiguousPolymorphicVariadic;
- }
- for_array(operand_index, variadic_operands) {
- Operand *o = &variadic_operands[operand_index];
- if (vari_expand) {
- t = slice;
- if (operand_index > 0) {
- if (show_error) {
- error(o->expr, "'..' in a variadic procedure can only have one variadic argument at the end");
- }
- if (data) {
- data->score = score;
- data->result_type = final_proc_type->Proc.results;
- data->gen_entity = gen_entity;
- }
- return CallArgumentError_MultipleVariadicExpand;
- }
- }
- score += eval_param_and_score(c, o, t, err, true, var_entity, show_error);
- }
- if (!vari_expand && variadic_operands.count != 0) {
- // NOTE(bill, 2024-07-14): minimize the stack usage for variadic parameters with the backing array
- if (c->decl) {
- bool found = false;
- for (auto &vr : c->decl->variadic_reuses) {
- if (are_types_identical(slice, vr.slice_type)) {
- vr.max_count = gb_max(vr.max_count, variadic_operands.count);
- found = true;
- break;
- }
- }
- if (!found) {
- array_add(&c->decl->variadic_reuses, VariadicReuseData{slice, variadic_operands.count});
- }
- }
- }
- }
- if (data) {
- data->score = score;
- data->result_type = final_proc_type->Proc.results;
- data->gen_entity = gen_entity;
- Ast *proc_lit = nullptr;
- if (ce->proc->tav.value.kind == ExactValue_Procedure) {
- Ast *vp = unparen_expr(ce->proc->tav.value.value_procedure);
- if (vp && vp->kind == Ast_ProcLit) {
- proc_lit = vp;
- }
- }
- if (proc_lit == nullptr) {
- add_type_and_value(c, ce->proc, Addressing_Value, final_proc_type, {});
- }
- }
- return err;
- }
- gb_internal bool is_call_expr_field_value(AstCallExpr *ce) {
- GB_ASSERT(ce != nullptr);
- if (ce->args.count == 0) {
- return false;
- }
- return ce->args[0]->kind == Ast_FieldValue;
- }
- gb_internal Entity **populate_proc_parameter_list(CheckerContext *c, Type *proc_type, isize *lhs_count_) {
- Entity **lhs = nullptr;
- isize lhs_count = -1;
- if (proc_type == nullptr || proc_type == t_invalid) {
- return nullptr;
- }
- GB_ASSERT(is_type_proc(proc_type));
- TypeProc *pt = &base_type(proc_type)->Proc;
- if (!pt->is_polymorphic || pt->is_poly_specialized) {
- if (pt->params != nullptr) {
- lhs = pt->params->Tuple.variables.data;
- lhs_count = pt->params->Tuple.variables.count;
- }
- } else {
- // NOTE(bill): Create 'lhs' list in order to ignore parameters which are polymorphic
- if (pt->params == nullptr) {
- lhs_count = 0;
- } else {
- lhs_count = pt->params->Tuple.variables.count;
- }
- lhs = gb_alloc_array(permanent_allocator(), Entity *, lhs_count);
- for (isize i = 0; i < lhs_count; i++) {
- Entity *e = pt->params->Tuple.variables[i];
- if (!is_type_polymorphic(e->type)) {
- lhs[i] = e;
- }
- }
- }
- if (lhs_count_) *lhs_count_ = lhs_count;
- return lhs;
- }
- gb_internal bool evaluate_where_clauses(CheckerContext *ctx, Ast *call_expr, Scope *scope, Slice<Ast *> *clauses, bool print_err) {
- if (clauses != nullptr) {
- for (Ast *clause : *clauses) {
- Operand o = {};
- check_expr(ctx, &o, clause);
- if (o.mode != Addressing_Constant) {
- if (print_err) error(clause, "'where' clauses expect a constant boolean evaluation");
- if (print_err && call_expr) error(call_expr, "at caller location");
- return false;
- } else if (o.value.kind != ExactValue_Bool) {
- if (print_err) error(clause, "'where' clauses expect a constant boolean evaluation");
- if (print_err && call_expr) error(call_expr, "at caller location");
- return false;
- } else if (!o.value.value_bool) {
- if (print_err) {
- ERROR_BLOCK();
-
- gbString str = expr_to_string(clause);
- error(clause, "'where' clause evaluated to false:\n\t%s", str);
- gb_string_free(str);
- if (scope != nullptr) {
- isize print_count = 0;
- for (auto const &entry : scope->elements) {
- Entity *e = entry.value;
- switch (e->kind) {
- case Entity_TypeName: {
- if (print_count == 0) error_line("\n\tWith the following definitions:\n");
- gbString str = type_to_string(e->type);
- error_line("\t\t%.*s :: %s;\n", LIT(e->token.string), str);
- gb_string_free(str);
- print_count += 1;
- break;
- }
- case Entity_Constant: {
- if (print_count == 0) error_line("\n\tWith the following definitions:\n");
- gbString str = exact_value_to_string(e->Constant.value);
- if (is_type_untyped(e->type)) {
- error_line("\t\t%.*s :: %s;\n", LIT(e->token.string), str);
- } else {
- gbString t = type_to_string(e->type);
- error_line("\t\t%.*s : %s : %s;\n", LIT(e->token.string), t, str);
- gb_string_free(t);
- }
- gb_string_free(str);
- print_count += 1;
- break;
- }
- }
- }
- }
- if (call_expr) {
- TokenPos pos = ast_token(call_expr).pos;
- error_line("%s at caller location\n", token_pos_to_string(pos));
- }
- }
- return false;
- }
- if (ast_file_vet_style(ctx->file)) {
- Ast *c = unparen_expr(clause);
- if (c->kind == Ast_BinaryExpr && c->BinaryExpr.op.kind == Token_CmpAnd) {
- ERROR_BLOCK();
- error(c, "Prefer to separate 'where' clauses with a comma rather than '&&'");
- gbString x = expr_to_string(c->BinaryExpr.left);
- gbString y = expr_to_string(c->BinaryExpr.right);
- error_line("\tSuggestion: '%s, %s'\n", x, y);
- gb_string_free(y);
- gb_string_free(x);
- }
- }
- }
- }
- return true;
- }
- gb_internal bool check_named_arguments(CheckerContext *c, Type *type, Slice<Ast *> const &named_args, Array<Operand> *named_operands, bool show_error) {
- bool success = true;
- type = base_type(type);
- if (named_args.count > 0) {
- TypeProc *pt = nullptr;
- if (is_type_proc(type)) {
- pt = &type->Proc;
- }
- for_array(i, named_args) {
- Ast *arg = named_args[i];
- if (arg->kind != Ast_FieldValue) {
- if (show_error) {
- error(arg, "Expected a 'field = value'");
- }
- return false;
- }
- ast_node(fv, FieldValue, arg);
- if (fv->field->kind != Ast_Ident) {
- if (show_error) {
- gbString expr_str = expr_to_string(fv->field);
- error(arg, "Invalid parameter name '%s' in procedure call", expr_str);
- gb_string_free(expr_str);
- }
- success = false;
- continue;
- }
- String key = fv->field->Ident.token.string;
- Ast *value = fv->value;
- Type *type_hint = nullptr;
- if (pt) {
- isize param_index = lookup_procedure_parameter(pt, key);
- if (param_index < 0) {
- if (show_error) {
- error(value, "No parameter named '%.*s' for this procedure type", LIT(key));
- }
- success = false;
- continue;
- }
- Entity *e = pt->params->Tuple.variables[param_index];
- if (!is_type_polymorphic(e->type)) {
- type_hint = e->type;
- }
- }
- Operand o = {};
- check_expr_with_type_hint(c, &o, value, type_hint);
- if (o.mode == Addressing_Invalid) {
- success = false;
- }
- array_add(named_operands, o);
- }
- }
- return success;
- }
- gb_internal bool check_call_arguments_single(CheckerContext *c, Ast *call, Operand *operand,
- Entity *e, Type *proc_type,
- Array<Operand> const &positional_operands, Array<Operand> const &named_operands,
- CallArgumentErrorMode show_error_mode,
- CallArgumentData *data) {
- bool return_on_failure = show_error_mode == CallArgumentErrorMode::NoErrors;
- Ast *ident = operand->expr;
- while (ident->kind == Ast_SelectorExpr) {
- Ast *s = ident->SelectorExpr.selector;
- ident = s;
- }
- if (e == nullptr) {
- e = entity_of_node(ident);
- if (e != nullptr) {
- proc_type = e->type;
- }
- }
- GB_ASSERT(proc_type != nullptr);
- proc_type = base_type(proc_type);
- if (proc_type == t_invalid) {
- return false;
- }
- GB_ASSERT(proc_type->kind == Type_Proc);
- CallArgumentError err = check_call_arguments_internal(c, call, e, proc_type, positional_operands, named_operands, show_error_mode, data);
- if (return_on_failure && err != CallArgumentError_None) {
- return false;
- }
- Entity *entity_to_use = data->gen_entity != nullptr ? data->gen_entity : e;
- if (!return_on_failure && entity_to_use != nullptr) {
- add_entity_use(c, ident, entity_to_use);
- update_untyped_expr_type(c, operand->expr, entity_to_use->type, true);
- add_type_and_value(c, operand->expr, operand->mode, entity_to_use->type, operand->value);
- }
- if (data->gen_entity != nullptr) {
- Entity *e = data->gen_entity;
- DeclInfo *decl = data->gen_entity->decl_info;
- CheckerContext ctx = *c;
- ctx.scope = decl->scope;
- ctx.decl = decl;
- ctx.proc_name = e->token.string;
- ctx.curr_proc_decl = decl;
- ctx.curr_proc_sig = e->type;
- GB_ASSERT(decl->proc_lit->kind == Ast_ProcLit);
- bool ok = evaluate_where_clauses(&ctx, call, decl->scope, &decl->proc_lit->ProcLit.where_clauses, !return_on_failure);
- if (return_on_failure) {
- if (!ok) {
- return false;
- }
- } else {
- decl->where_clauses_evaluated = true;
- if (ok && (data->gen_entity->flags & EntityFlag_ProcBodyChecked) == 0) {
- check_procedure_later(c->checker, e->file, e->token, decl, e->type, decl->proc_lit->ProcLit.body, decl->proc_lit->ProcLit.tags);
- }
- if (is_type_proc(data->gen_entity->type)) {
- Type *t = base_type(entity_to_use->type);
- data->result_type = t->Proc.results;
- }
- }
- }
- return true;
- }
- gb_internal CallArgumentData check_call_arguments_proc_group(CheckerContext *c, Operand *operand, Ast *call) {
- ast_node(ce, CallExpr, call);
- GB_ASSERT(ce->split_args != nullptr);
- Slice<Ast *> const &positional_args = ce->split_args->positional;
- Slice<Ast *> const &named_args = ce->split_args->named;
- CallArgumentData data = {};
- data.result_type = t_invalid;
- GB_ASSERT(operand->mode == Addressing_ProcGroup);
- auto procs = proc_group_entities_cloned(c, *operand);
- if (procs.count > 1) {
- isize max_arg_count = positional_args.count + named_args.count;
- for (Ast *arg : positional_args) {
- // NOTE(bill): The only thing that may have multiple values
- // will be a call expression (assuming `or_return` and `()` will be stripped)
- arg = strip_or_return_expr(arg);
- if (arg && arg->kind == Ast_CallExpr) {
- max_arg_count = ISIZE_MAX;
- break;
- }
- }
- if (max_arg_count != ISIZE_MAX) for (Ast *arg : named_args) {
- // NOTE(bill): The only thing that may have multiple values
- // will be a call expression (assuming `or_return` and `()` will be stripped)
- if (arg->kind == Ast_FieldValue) {
- arg = strip_or_return_expr(arg->FieldValue.value);
- if (arg && arg->kind == Ast_CallExpr) {
- max_arg_count = ISIZE_MAX;
- break;
- }
- }
- }
- // ignore named arguments first
- for (Ast *arg : named_args) {
- if (arg->kind != Ast_FieldValue) {
- continue;
- }
- ast_node(fv, FieldValue, arg);
- if (fv->field->kind != Ast_Ident) {
- continue;
- }
- String key = fv->field->Ident.token.string;
- for (isize proc_index = procs.count-1; proc_index >= 0; proc_index--) {
- Type *t = procs[proc_index]->type;
- if (is_type_proc(t)) {
- isize param_index = lookup_procedure_parameter(t, key);
- if (param_index < 0) {
- array_unordered_remove(&procs, proc_index);
- }
- }
- }
- }
- if (procs.count == 0) {
- // if any of the named arguments are wrong, the `procs` will be empty
- // just start from scratch
- array_free(&procs);
- procs = proc_group_entities_cloned(c, *operand);
- }
- // filter by positional argument length
- for (isize proc_index = 0; proc_index < procs.count; /**/) {
- Entity *proc = procs[proc_index];
- Type *pt = base_type(proc->type);
- if (!(pt != nullptr && is_type_proc(pt))) {
- proc_index++;
- continue;
- }
- isize param_count = 0;
- isize param_count_excluding_defaults = get_procedure_param_count_excluding_defaults(pt, ¶m_count);
- if (param_count_excluding_defaults > max_arg_count) {
- array_unordered_remove(&procs, proc_index);
- continue;
- }
- proc_index++;
- }
- }
- Entity **lhs = nullptr;
- isize lhs_count = -1;
- i32 variadic_index = -1;
- auto positional_operands = array_make<Operand>(heap_allocator(), 0, 0);
- auto named_operands = array_make<Operand>(heap_allocator(), 0, 0);
- defer (array_free(&positional_operands));
- defer (array_free(&named_operands));
- if (procs.count == 1) {
- Entity *e = procs[0];
- Type *pt = base_type(e->type);
- if (pt != nullptr && is_type_proc(pt)) {
- lhs = populate_proc_parameter_list(c, pt, &lhs_count);
- if (pt->Proc.variadic) {
- variadic_index = pt->Proc.variadic_index;
- }
- }
- check_unpack_arguments(c, lhs, lhs_count, &positional_operands, positional_args, UnpackFlag_None, variadic_index);
- if (check_named_arguments(c, e->type, named_args, &named_operands, true)) {
- check_call_arguments_single(c, call, operand,
- e, e->type,
- positional_operands, named_operands,
- CallArgumentErrorMode::ShowErrors,
- &data);
- }
- return data;
- }
- {
- // NOTE(bill, 2019-07-13): This code is used to improve the type inference for procedure groups
- // where the same positional parameter has the same type value (and ellipsis)
- isize proc_arg_count = -1;
- for (Entity *p : procs) {
- Type *pt = base_type(p->type);
- if (pt != nullptr && is_type_proc(pt)) {
- if (proc_arg_count < 0) {
- proc_arg_count = pt->Proc.param_count;
- } else {
- proc_arg_count = gb_min(proc_arg_count, pt->Proc.param_count);
- }
- }
- }
- if (proc_arg_count >= 0) {
- lhs_count = proc_arg_count;
- if (lhs_count > 0) {
- lhs = gb_alloc_array(heap_allocator(), Entity *, lhs_count);
- for (isize param_index = 0; param_index < lhs_count; param_index++) {
- Entity *e = nullptr;
- for (Entity *p : procs) {
- Type *pt = base_type(p->type);
- if (!(pt != nullptr && is_type_proc(pt))) {
- continue;
- }
- if (e == nullptr) {
- e = pt->Proc.params->Tuple.variables[param_index];
- } else {
- Entity *f = pt->Proc.params->Tuple.variables[param_index];
- if (e == f) {
- continue;
- }
- if (are_types_identical(e->type, f->type)) {
- bool ee = (e->flags & EntityFlag_Ellipsis) != 0;
- bool fe = (f->flags & EntityFlag_Ellipsis) != 0;
- if (ee == fe) {
- continue;
- }
- }
- // NOTE(bill): Entities are not close enough to be used
- e = nullptr;
- break;
- }
- }
- lhs[param_index] = e;
- }
- for (Entity *p : procs) {
- Type *pt = base_type(p->type);
- if (!(pt != nullptr && is_type_proc(pt))) {
- continue;
- }
-
- if (pt->Proc.is_polymorphic) {
- if (variadic_index == -1) {
- variadic_index = pt->Proc.variadic_index;
- } else if (variadic_index != pt->Proc.variadic_index) {
- variadic_index = -1;
- break;
- }
- } else {
- variadic_index = -1;
- break;
- }
- }
- }
- }
- }
- check_unpack_arguments(c, lhs, lhs_count, &positional_operands, positional_args, UnpackFlag_None, variadic_index);
- for_array(i, named_args) {
- Ast *arg = named_args[i];
- if (arg->kind != Ast_FieldValue) {
- error(arg, "Expected a 'field = value'");
- return data;
- }
- ast_node(fv, FieldValue, arg);
- if (fv->field->kind != Ast_Ident) {
- gbString expr_str = expr_to_string(fv->field);
- error(arg, "Invalid parameter name '%s' in procedure call", expr_str);
- gb_string_free(expr_str);
- return data;
- }
- String key = fv->field->Ident.token.string;
- Ast *value = fv->value;
- Type *type_hint = nullptr;
- for (isize lhs_idx = 0; lhs_idx < lhs_count; lhs_idx++) {
- Entity *e = lhs[lhs_idx];
- if (e != nullptr && e->token.string == key &&
- !is_type_polymorphic(e->type)) {
- type_hint = e->type;
- break;
- }
- }
- Operand o = {};
- check_expr_with_type_hint(c, &o, value, type_hint);
- array_add(&named_operands, o);
- }
- gb_free(heap_allocator(), lhs);
- auto valids = array_make<ValidIndexAndScore>(heap_allocator(), 0, procs.count);
- defer (array_free(&valids));
- auto proc_entities = array_make<Entity *>(heap_allocator(), 0, procs.count*2 + 1);
- defer (array_free(&proc_entities));
- for (Entity *proc : procs) {
- array_add(&proc_entities, proc);
- }
- int max_matched_features = 0;
- gbString expr_name = expr_to_string(operand->expr);
- defer (gb_string_free(expr_name));
- for_array(i, procs) {
- Entity *p = procs[i];
- if (p->flags & EntityFlag_Disabled) {
- continue;
- }
- Type *pt = base_type(p->type);
- if (pt != nullptr && is_type_proc(pt)) {
- CallArgumentData data = {};
- CheckerContext ctx = *c;
- ctx.no_polymorphic_errors = true;
- ctx.allow_polymorphic_types = is_type_polymorphic(pt);
- ctx.hide_polymorphic_errors = true;
- bool is_a_candidate = check_call_arguments_single(&ctx, call, operand,
- p, pt,
- positional_operands, named_operands,
- CallArgumentErrorMode::NoErrors,
- &data);
- if (!is_a_candidate) {
- continue;
- }
- isize index = i;
- ValidIndexAndScore item = {};
- item.score = data.score;
- if (data.gen_entity != nullptr) {
- array_add(&proc_entities, data.gen_entity);
- index = proc_entities.count-1;
- // prefer non-polymorphic procedures over polymorphic
- item.score += assign_score_function(1);
- }
- max_matched_features = gb_max(max_matched_features, matched_target_features(&pt->Proc));
- item.index = index;
- array_add(&valids, item);
- }
- }
- if (max_matched_features > 0) {
- for_array(i, valids) {
- Entity *p = procs[valids[i].index];
- Type *t = base_type(p->type);
- GB_ASSERT(t->kind == Type_Proc);
- int matched = matched_target_features(&t->Proc);
- valids[i].score += assign_score_function(max_matched_features-matched);
- }
- }
- if (valids.count > 1) {
- array_sort(valids, valid_index_and_score_cmp);
- i64 best_score = valids[0].score;
- Entity *best_entity = proc_entities[valids[0].index];
- GB_ASSERT(best_entity != nullptr);
- for (isize i = 1; i < valids.count; i++) {
- if (best_score > valids[i].score) {
- valids.count = i;
- break;
- }
- if (best_entity == proc_entities[valids[i].index]) {
- valids.count = i;
- break;
- }
- }
- }
- auto print_argument_types = [&]() {
- error_line("\tGiven argument types: (");
- isize i = 0;
- for (Operand const &o : positional_operands) {
- if (i++ > 0) error_line(", ");
- gbString type = type_to_string(o.type);
- defer (gb_string_free(type));
- error_line("%s", type);
- }
- for (Operand const &o : named_operands) {
- if (i++ > 0) error_line(", ");
- gbString type = type_to_string(o.type);
- defer (gb_string_free(type));
- if (i < ce->split_args->named.count) {
- Ast *named_field = ce->split_args->named[i];
- ast_node(fv, FieldValue, named_field);
- gbString field = expr_to_string(fv->field);
- defer (gb_string_free(field));
- error_line("%s = %s", field, type);
- } else {
- error_line("%s", type);
- }
- }
- error_line(")\n");
- };
- if (valids.count == 0) {
- ERROR_BLOCK();
- error(operand->expr, "No procedures or ambiguous call for procedure group '%s' that match with the given arguments", expr_name);
- if (positional_operands.count == 0 && named_operands.count == 0) {
- error_line("\tNo given arguments\n");
- } else {
- print_argument_types();
- }
- if (procs.count == 0) {
- procs = proc_group_entities_cloned(c, *operand);
- }
- if (procs.count > 0) {
- error_line("Did you mean to use one of the following:\n");
- }
- // Try to reduce the list further for `$T: typeid` like parameters
- bool *possibly_ignore = gb_alloc_array(temporary_allocator(), bool, procs.count);
- isize possibly_ignore_set = 0;
- if (true) {
- // NOTE(bill): This currently only checks for #soa types
- for_array(i, procs) {
- Entity *proc = procs[i];
- Type *t = base_type(proc->type);
- if (t == nullptr || t->kind != Type_Proc) {
- continue;
- }
- TypeProc *pt = &t->Proc;
- if (pt->param_count == 0) {
- continue;
- }
- for_array(j, pt->params->Tuple.variables) {
- Entity *v = pt->params->Tuple.variables[j];
- if (v->kind != Entity_TypeName) {
- continue;
- }
- Type *dst_t = base_type(v->type);
- while (dst_t->kind == Type_Generic && dst_t->Generic.specialized) {
- dst_t = dst_t->Generic.specialized;
- }
- if (j >= positional_operands.count) {
- continue;
- }
- Operand const &o = positional_operands[j];
- if (o.mode != Addressing_Type) {
- continue;
- }
- Type *t = base_type(o.type);
- if (t->kind == dst_t->kind) {
- continue;
- }
- Type *st = base_type(type_deref(o.type));
- Type *dt = base_type(type_deref(dst_t));
- if (st->kind == dt->kind) {
- continue;
- }
- if (is_type_soa_struct(st)) {
- possibly_ignore[i] = true;
- possibly_ignore_set += 1;
- continue;
- }
- }
- }
- }
- if (possibly_ignore_set == procs.count) {
- possibly_ignore_set = 0;
- }
- isize max_name_length = 0;
- isize max_type_length = 0;
- for_array(i, procs) {
- if (possibly_ignore_set != 0 && possibly_ignore[i]) {
- continue;
- }
- Entity *proc = procs[i];
- Type *t = base_type(proc->type);
- if (t == t_invalid) continue;
- String prefix = {};
- String prefix_sep = {};
- if (proc->pkg) {
- prefix = proc->pkg->name;
- prefix_sep = str_lit(".");
- }
- String name = proc->token.string;
- max_name_length = gb_max(max_name_length, prefix.len + prefix_sep.len + name.len);
- gbString pt;
- if (t->Proc.node != nullptr) {
- pt = expr_to_string(t->Proc.node);
- } else {
- pt = type_to_string(t);
- }
- max_type_length = gb_max(max_type_length, gb_string_length(pt));
- gb_string_free(pt);
- }
- isize max_spaces = gb_max(max_name_length, max_type_length);
- char *spaces = gb_alloc_array(temporary_allocator(), char, max_spaces+1);
- for (isize i = 0; i < max_spaces; i++) {
- spaces[i] = ' ';
- }
- spaces[max_spaces] = 0;
- for_array(i, procs) {
- if (possibly_ignore_set != 0 && possibly_ignore[i]) {
- continue;
- }
- Entity *proc = procs[i];
- TokenPos pos = proc->token.pos;
- Type *t = base_type(proc->type);
- if (t == t_invalid) continue;
- GB_ASSERT(t->kind == Type_Proc);
- gbString pt;
- defer (gb_string_free(pt));
- if (t->Proc.node != nullptr) {
- pt = expr_to_string(t->Proc.node);
- } else {
- pt = type_to_string(t);
- }
- String prefix = {};
- String prefix_sep = {};
- if (proc->pkg) {
- prefix = proc->pkg->name;
- prefix_sep = str_lit(".");
- }
- String name = proc->token.string;
- isize len = prefix.len + prefix_sep.len + name.len;
- int name_padding = cast(int)gb_max(max_name_length - len, 0);
- int type_padding = cast(int)gb_max(max_type_length - gb_string_length(pt), 0);
- char const *sep = "::";
- if (proc->kind == Entity_Variable) {
- sep = ":=";
- }
- error_line("\t%.*s%.*s%.*s %.*s%s %s %.*sat %s\n",
- LIT(prefix), LIT(prefix_sep), LIT(name),
- name_padding, spaces,
- sep,
- pt,
- type_padding, spaces,
- token_pos_to_string(pos)
- );
- }
- if (procs.count > 0) {
- error_line("\n");
- }
- data.result_type = t_invalid;
- } else if (valids.count > 1) {
- ERROR_BLOCK();
- error(operand->expr, "Ambiguous procedure group call '%s' that match with the given arguments", expr_name);
- if (positional_operands.count == 0 && named_operands.count == 0) {
- error_line("\tNo given arguments\n");
- } else {
- print_argument_types();
- }
- for (auto const &valid : valids) {
- Entity *proc = proc_entities[valid.index];
- GB_ASSERT(proc != nullptr);
- TokenPos pos = proc->token.pos;
- Type *t = base_type(proc->type); GB_ASSERT(t->kind == Type_Proc);
- gbString pt = nullptr;
- defer (gb_string_free(pt));
- if (t->Proc.node != nullptr) {
- pt = expr_to_string(t->Proc.node);
- } else {
- pt = type_to_string(t);
- }
- String name = proc->token.string;
- char const *sep = "::";
- if (proc->kind == Entity_Variable) {
- sep = ":=";
- }
- error_line("\t%.*s %s %s ", LIT(name), sep, pt);
- if (proc->decl_info->proc_lit != nullptr) {
- GB_ASSERT(proc->decl_info->proc_lit->kind == Ast_ProcLit);
- auto *pl = &proc->decl_info->proc_lit->ProcLit;
- if (pl->where_token.kind != Token_Invalid) {
- error_line("\n\t\twhere ");
- for_array(j, pl->where_clauses) {
- Ast *clause = pl->where_clauses[j];
- if (j != 0) {
- error_line("\t\t ");
- }
- gbString str = expr_to_string(clause);
- error_line("%s", str);
- gb_string_free(str);
- if (j != pl->where_clauses.count-1) {
- error_line(",");
- }
- }
- error_line("\n\t");
- }
- }
- error_line("at %s\n", token_pos_to_string(pos));
- }
- data.result_type = t_invalid;
- } else {
- GB_ASSERT(valids.count == 1);
- Ast *ident = operand->expr;
- while (ident->kind == Ast_SelectorExpr) {
- Ast *s = ident->SelectorExpr.selector;
- ident = s;
- }
- Entity *e = proc_entities[valids[0].index];
- GB_ASSERT(e != nullptr);
- Array<Operand> named_operands = {};
- check_call_arguments_single(c, call, operand,
- e, e->type,
- positional_operands, named_operands,
- CallArgumentErrorMode::ShowErrors,
- &data);
- return data;
- }
- return data;
- }
- gb_internal CallArgumentData check_call_arguments(CheckerContext *c, Operand *operand, Ast *call) {
- Type *proc_type = nullptr;
- CallArgumentData data = {};
- data.result_type = t_invalid;
- proc_type = base_type(operand->type);
- TypeProc *pt = nullptr;
- if (proc_type) {
- pt = &proc_type->Proc;
- }
- TEMPORARY_ALLOCATOR_GUARD();
- ast_node(ce, CallExpr, call);
- bool any_failure = false;
- // Split positional and named args into separate arrays/slices
- Slice<Ast *> positional_args = {};
- Slice<Ast *> named_args = {};
- if (ce->split_args == nullptr) {
- positional_args = ce->args;
- for (isize i = 0; i < ce->args.count; i++) {
- Ast *arg = ce->args.data[i];
- if (arg->kind == Ast_FieldValue) {
- positional_args.count = i;
- break;
- }
- }
- named_args = slice(ce->args, positional_args.count, ce->args.count);
- auto split_args = gb_alloc_item(permanent_allocator(), AstSplitArgs);
- split_args->positional = positional_args;
- split_args->named = named_args;
- ce->split_args = split_args;
- } else {
- positional_args = ce->split_args->positional;
- named_args = ce->split_args->named;
- }
- if (operand->mode == Addressing_ProcGroup) {
- return check_call_arguments_proc_group(c, operand, call);
- }
- auto positional_operands = array_make<Operand>(heap_allocator(), 0, positional_args.count);
- auto named_operands = array_make<Operand>(heap_allocator(), 0, 0);
- defer (array_free(&positional_operands));
- defer (array_free(&named_operands));
- if (positional_args.count > 0) {
- Entity **lhs = nullptr;
- isize lhs_count = -1;
- i32 variadic_index = -1;
- if (pt != nullptr) {
- lhs = populate_proc_parameter_list(c, proc_type, &lhs_count);
- if (pt->variadic) {
- variadic_index = pt->variadic_index;
- }
- }
- check_unpack_arguments(c, lhs, lhs_count, &positional_operands, positional_args, UnpackFlag_None, variadic_index);
- }
- if (named_args.count > 0) {
- for_array(i, named_args) {
- Ast *arg = named_args[i];
- if (arg->kind != Ast_FieldValue) {
- error(arg, "Expected a 'field = value'");
- return data;
- }
- ast_node(fv, FieldValue, arg);
- if (fv->field->kind != Ast_Ident) {
- gbString expr_str = expr_to_string(fv->field);
- error(arg, "Invalid parameter name '%s' in procedure call", expr_str);
- any_failure = true;
- gb_string_free(expr_str);
- continue;
- }
- String key = fv->field->Ident.token.string;
- Ast *value = fv->value;
- isize param_index = lookup_procedure_parameter(pt, key);
- Type *type_hint = nullptr;
- if (param_index >= 0) {
- Entity *e = pt->params->Tuple.variables[param_index];
- type_hint = e->type;
- }
- Operand o = {};
- check_expr_with_type_hint(c, &o, value, type_hint);
- if (o.mode == Addressing_Invalid) {
- any_failure = true;
- }
- array_add(&named_operands, o);
- }
- }
- if (!any_failure) {
- check_call_arguments_single(c, call, operand,
- nullptr, proc_type,
- positional_operands, named_operands,
- CallArgumentErrorMode::ShowErrors,
- &data);
- } else if (pt) {
- data.result_type = pt->results;
- }
- return data;
- }
- gb_internal isize lookup_polymorphic_record_parameter(Type *t, String parameter_name) {
- if (!is_type_polymorphic_record(t)) {
- return -1;
- }
- TypeTuple *params = get_record_polymorphic_params(t);
- if (params == nullptr) {
- return -1;
- }
- for_array(i, params->variables) {
- Entity *e = params->variables[i];
- String name = e->token.string;
- if (is_blank_ident(name)) {
- continue;
- }
- if (name == parameter_name) {
- return i;
- }
- }
- return -1;
- }
- gb_internal CallArgumentError check_polymorphic_record_type(CheckerContext *c, Operand *operand, Ast *call) {
- ast_node(ce, CallExpr, call);
- Type *original_type = operand->type;
- GB_ASSERT(is_type_polymorphic_record(original_type));
- bool show_error = true;
- Array<Operand> operands = {};
- defer (array_free(&operands));
- CallArgumentError err = CallArgumentError_None;
- bool named_fields = false;
- {
- // NOTE(bill, 2019-10-26): Allow a cycle in the parameters but not in the fields themselves
- auto prev_type_path = c->type_path;
- c->type_path = new_checker_type_path();
- defer ({
- destroy_checker_type_path(c->type_path);
- c->type_path = prev_type_path;
- });
- if (is_call_expr_field_value(ce)) {
- named_fields = true;
- operands = array_make<Operand>(heap_allocator(), ce->args.count);
- for_array(i, ce->args) {
- Ast *arg = ce->args[i];
- ast_node(fv, FieldValue, arg);
- if (fv->value == nullptr) {
- error(fv->eq, "Expected a value");
- err = CallArgumentError_InvalidFieldValue;
- continue;
- }
- if (fv->field->kind == Ast_Ident) {
- String name = fv->field->Ident.token.string;
- isize index = lookup_polymorphic_record_parameter(original_type, name);
- if (index >= 0) {
- TypeTuple *params = get_record_polymorphic_params(original_type);
- Entity *e = params->variables[index];
- if (e->kind == Entity_Constant) {
- check_expr_with_type_hint(c, &operands[i], fv->value, e->type);
- continue;
- }
- }
- }
- check_expr_or_type(c, &operands[i], fv->value);
- }
- bool vari_expand = (ce->ellipsis.pos.line != 0);
- if (vari_expand) {
- error(ce->ellipsis, "Invalid use of '..' in a polymorphic type call'");
- }
- } else {
- operands = array_make<Operand>(heap_allocator(), 0, 2*ce->args.count);
- Entity **lhs = nullptr;
- isize lhs_count = -1;
- TypeTuple *params = get_record_polymorphic_params(original_type);
- if (params != nullptr) {
- lhs = params->variables.data;
- lhs_count = params->variables.count;
- }
- check_unpack_arguments(c, lhs, lhs_count, &operands, ce->args, UnpackFlag_None);
- }
- }
- if (err != 0) {
- operand->mode = Addressing_Invalid;
- return err;
- }
- TypeTuple *tuple = get_record_polymorphic_params(original_type);
- isize param_count = tuple->variables.count;
- isize minimum_param_count = param_count;
- for (; minimum_param_count > 0; minimum_param_count--) {
- Entity *e = tuple->variables[minimum_param_count-1];
- if (e->kind != Entity_Constant) {
- break;
- }
- if (e->Constant.param_value.kind == ParameterValue_Invalid) {
- break;
- }
- }
- Array<Operand> ordered_operands = operands;
- if (!named_fields) {
- ordered_operands = array_make<Operand>(permanent_allocator(), operands.count);
- array_copy(&ordered_operands, operands, 0);
- } else {
- TEMPORARY_ALLOCATOR_GUARD();
- bool *visited = gb_alloc_array(temporary_allocator(), bool, param_count);
- // LEAK(bill)
- ordered_operands = array_make<Operand>(permanent_allocator(), param_count);
- for_array(i, ce->args) {
- Ast *arg = ce->args[i];
- ast_node(fv, FieldValue, arg);
- if (fv->field->kind != Ast_Ident) {
- if (show_error) {
- gbString expr_str = expr_to_string(fv->field);
- error(arg, "Invalid parameter name '%s' in polymorphic type call", expr_str);
- gb_string_free(expr_str);
- }
- err = CallArgumentError_InvalidFieldValue;
- continue;
- }
- String name = fv->field->Ident.token.string;
- isize index = lookup_polymorphic_record_parameter(original_type, name);
- if (index < 0) {
- if (show_error) {
- error(arg, "No parameter named '%.*s' for this polymorphic type", LIT(name));
- }
- err = CallArgumentError_ParameterNotFound;
- continue;
- }
- if (visited[index]) {
- if (show_error) {
- error(arg, "Duplicate parameter '%.*s' in polymorphic type", LIT(name));
- }
- err = CallArgumentError_DuplicateParameter;
- continue;
- }
- visited[index] = true;
- ordered_operands[index] = operands[i];
- }
- for (isize i = 0; i < param_count; i++) {
- if (!visited[i]) {
- Entity *e = tuple->variables[i];
- if (is_blank_ident(e->token)) {
- continue;
- }
- if (show_error) {
- if (e->kind == Entity_TypeName) {
- error(call, "Type parameter '%.*s' is missing in polymorphic type call",
- LIT(e->token.string));
- } else {
- gbString str = type_to_string(e->type);
- error(call, "Parameter '%.*s' of type '%s' is missing in polymorphic type call",
- LIT(e->token.string), str);
- gb_string_free(str);
- }
- }
- err = CallArgumentError_ParameterMissing;
- }
- }
- }
- if (err != 0) {
- operand->mode = Addressing_Invalid;
- return err;
- }
- while (ordered_operands.count > 0) {
- if (ordered_operands[ordered_operands.count-1].expr != nullptr) {
- break;
- }
- array_pop(&ordered_operands);
- }
- if (minimum_param_count != param_count) {
- if (param_count < ordered_operands.count) {
- error(call, "Too many polymorphic type arguments, expected a maximum of %td, got %td", param_count, ordered_operands.count);
- err = CallArgumentError_TooManyArguments;
- } else if (minimum_param_count > ordered_operands.count) {
- error(call, "Too few polymorphic type arguments, expected a minimum of %td, got %td", minimum_param_count, ordered_operands.count);
- err = CallArgumentError_TooFewArguments;
- }
- } else {
- if (param_count < ordered_operands.count) {
- error(call, "Too many polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
- err = CallArgumentError_TooManyArguments;
- } else if (param_count > ordered_operands.count) {
- error(call, "Too few polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
- err = CallArgumentError_TooFewArguments;
- }
- }
- if (err != 0) {
- return err;
- }
- if (minimum_param_count != param_count) {
- array_resize(&ordered_operands, param_count);
- isize missing_count = 0;
- // NOTE(bill): Replace missing operands with the default values (if possible)
- for_array(i, ordered_operands) {
- Operand *o = &ordered_operands[i];
- if (o->expr == nullptr) {
- Entity *e = tuple->variables[i];
- if (e->kind == Entity_Constant) {
- missing_count += 1;
- o->mode = Addressing_Constant;
- o->type = default_type(e->type);
- o->expr = unparen_expr(e->Constant.param_value.original_ast_expr);
- if (e->Constant.param_value.kind == ParameterValue_Constant) {
- o->value = e->Constant.param_value.value;
- }
- } else if (e->kind == Entity_TypeName) {
- missing_count += 1;
- o->mode = Addressing_Type;
- o->type = e->type;
- o->expr = e->identifier;
- }
- }
- }
- }
- isize oo_count = gb_min(param_count, ordered_operands.count);
- i64 score = 0;
- for (isize i = 0; i < oo_count; i++) {
- Entity *e = tuple->variables[i];
- Operand *o = &ordered_operands[i];
- if (o->mode == Addressing_Invalid) {
- continue;
- }
- if (e->kind == Entity_TypeName) {
- if (o->mode != Addressing_Type) {
- if (show_error) {
- gbString expr = expr_to_string(o->expr);
- error(o->expr, "Expected a type for the argument '%.*s', got %s", LIT(e->token.string), expr);
- gb_string_free(expr);
- }
- err = CallArgumentError_WrongTypes;
- }
- if (are_types_identical(e->type, o->type)) {
- score += assign_score_function(1);
- } else {
- score += assign_score_function(MAXIMUM_TYPE_DISTANCE);
- }
- } else {
- i64 s = 0;
- if (o->type->kind == Type_Generic) {
- // Polymorphic name!
- score += assign_score_function(1);
- continue;
- } else if (!check_is_assignable_to_with_score(c, o, e->type, &s)) {
- if (show_error) {
- check_assignment(c, o, e->type, str_lit("polymorphic type argument"));
- }
- err = CallArgumentError_WrongTypes;
- }
- o->type = e->type;
- if (o->mode != Addressing_Constant) {
- bool valid = false;
- if (is_type_proc(o->type)) {
- Entity *proc_entity = entity_from_expr(o->expr);
- valid = proc_entity != nullptr;
- }
- if (!valid) {
- if (show_error) {
- error(o->expr, "Expected a constant value for this polymorphic type argument");
- }
- err = CallArgumentError_NoneConstantParameter;
- }
- }
- score += s;
- }
- // NOTE(bill): Add type info the parameters
- // TODO(bill, 2022-01-23): why was this line added in the first place? I'm commenting it out for the time being
- // add_type_info_type(c, o->type);
- }
- if (show_error && err) {
- return err;
- }
- {
- GenTypesData *found_gen_types = ensure_polymorphic_record_entity_has_gen_types(c, original_type);
- mutex_lock(&found_gen_types->mutex);
- defer (mutex_unlock(&found_gen_types->mutex));
- Entity *found_entity = find_polymorphic_record_entity(found_gen_types, param_count, ordered_operands);
- if (found_entity) {
- operand->mode = Addressing_Type;
- operand->type = found_entity->type;
- return err;
- }
- CheckerContext ctx = *c;
- // NOTE(bill): We need to make sure the lookup scope for the record is the same as where it was created
- ctx.scope = polymorphic_record_parent_scope(original_type);
- GB_ASSERT(ctx.scope != nullptr);
- Type *bt = base_type(original_type);
- String generated_name = make_string_c(expr_to_string(call));
- Type *named_type = alloc_type_named(generated_name, nullptr, nullptr);
- if (bt->kind == Type_Struct) {
- Ast *node = clone_ast(bt->Struct.node);
- Type *struct_type = alloc_type_struct();
- struct_type->Struct.node = node;
- struct_type->Struct.polymorphic_parent = original_type;
- set_base_type(named_type, struct_type);
- check_open_scope(&ctx, node);
- check_struct_type(&ctx, struct_type, node, &ordered_operands, named_type, original_type);
- check_close_scope(&ctx);
- } else if (bt->kind == Type_Union) {
- Ast *node = clone_ast(bt->Union.node);
- Type *union_type = alloc_type_union();
- union_type->Union.node = node;
- union_type->Union.polymorphic_parent = original_type;
- set_base_type(named_type, union_type);
- check_open_scope(&ctx, node);
- check_union_type(&ctx, union_type, node, &ordered_operands, named_type, original_type);
- check_close_scope(&ctx);
- } else {
- GB_PANIC("Unsupported parametric polymorphic record type");
- }
- bt = base_type(named_type);
- if (bt->kind == Type_Struct || bt->kind == Type_Union) {
- GB_ASSERT(original_type->kind == Type_Named);
- Entity *e = original_type->Named.type_name;
- GB_ASSERT(e->kind == Entity_TypeName);
- gbString s = gb_string_make_reserve(heap_allocator(), e->token.string.len+3);
- s = gb_string_append_fmt(s, "%.*s(", LIT(e->token.string));
- TypeTuple *tuple = get_record_polymorphic_params(bt);
- if (tuple != nullptr) for_array(i, tuple->variables) {
- Entity *v = tuple->variables[i];
- String name = v->token.string;
- if (i > 0) {
- s = gb_string_append_fmt(s, ", ");
- }
- s = gb_string_append_fmt(s, "$%.*s", LIT(name));
- if (v->kind == Entity_TypeName) {
- if (v->type->kind != Type_Generic) {
- s = gb_string_append_fmt(s, "=");
- s = write_type_to_string(s, v->type, false);
- }
- } else if (v->kind == Entity_Constant) {
- if (v->Constant.value.kind != ExactValue_Invalid) {
- s = gb_string_append_fmt(s, "=");
- s = write_exact_value_to_string(s, v->Constant.value);
- }
- }
- }
- s = gb_string_append_fmt(s, ")");
- String new_name = make_string_c(s);
- named_type->Named.name = new_name;
- if (named_type->Named.type_name) {
- named_type->Named.type_name->token.string = new_name;
- }
- }
- operand->mode = Addressing_Type;
- operand->type = named_type;
- }
- return err;
- }
- // returns true on success
- gb_internal bool check_call_parameter_mixture(Slice<Ast *> const &args, char const *context, bool allow_mixed=false) {
- bool success = true;
- if (args.count > 0) {
- if (allow_mixed) {
- bool was_named = false;
- for (Ast *arg : args) {
- if (was_named && arg->kind != Ast_FieldValue) {
- error(arg, "Non-named parameter is not allowed to follow named parameter i.e. 'field = value' in a %s", context);
- success = false;
- break;
- }
- was_named = was_named || arg->kind == Ast_FieldValue;
- }
- } else {
- bool first_is_field_value = (args[0]->kind == Ast_FieldValue);
- for (Ast *arg : args) {
- bool mix = false;
- if (first_is_field_value) {
- mix = arg->kind != Ast_FieldValue;
- } else {
- mix = arg->kind == Ast_FieldValue;
- }
- if (mix) {
- error(arg, "Mixture of 'field = value' and value elements in a %s is not allowed", context);
- success = false;
- }
- }
- }
- }
- return success;
- }
- #define CHECK_CALL_PARAMETER_MIXTURE_OR_RETURN(context_, ...) if (!check_call_parameter_mixture(args, context_, ##__VA_ARGS__)) { \
- operand->mode = Addressing_Invalid; \
- operand->expr = call; \
- return Expr_Stmt; \
- }
- gb_internal ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *call, Ast *proc, Slice<Ast *> const &args, ProcInlining inlining, Type *type_hint) {
- if (proc != nullptr &&
- proc->kind == Ast_BasicDirective) {
- ast_node(bd, BasicDirective, proc);
- String name = bd->name.string;
- if (
- name == "location" ||
- name == "exists" ||
- name == "assert" ||
- name == "panic" ||
- name == "defined" ||
- name == "config" ||
- name == "load" ||
- name == "load_directory" ||
- name == "load_hash" ||
- name == "hash" ||
- name == "caller_expression"
- ) {
- operand->mode = Addressing_Builtin;
- operand->builtin_id = BuiltinProc_DIRECTIVE;
- operand->expr = proc;
- operand->type = t_invalid;
- add_type_and_value(c, proc, operand->mode, operand->type, operand->value);
- } else {
- error(proc, "Unknown directive: #%.*s", LIT(name));
- operand->expr = proc;
- operand->type = t_invalid;
- operand->mode = Addressing_Invalid;
- return Expr_Expr;
- }
- if (inlining != ProcInlining_none) {
- error(call, "Inlining operators are not allowed on built-in procedures");
- }
- } else {
- if (proc != nullptr) {
- check_expr_or_type(c, operand, proc);
- } else {
- GB_ASSERT(operand->expr != nullptr);
- }
- }
- if (operand->mode == Addressing_Invalid) {
- CHECK_CALL_PARAMETER_MIXTURE_OR_RETURN("procedure call");
- for (Ast *arg : args) {
- if (arg->kind == Ast_FieldValue) {
- arg = arg->FieldValue.value;
- }
- check_expr_base(c, operand, arg, nullptr);
- }
- operand->mode = Addressing_Invalid;
- operand->expr = call;
- return Expr_Stmt;
- }
- if (operand->mode == Addressing_Type) {
- Type *t = operand->type;
- if (is_type_polymorphic_record(t)) {
- CHECK_CALL_PARAMETER_MIXTURE_OR_RETURN("polymorphic type construction");
- if (!is_type_named(t)) {
- gbString s = expr_to_string(operand->expr);
- error(call, "Illegal use of an unnamed polymorphic record, %s", s);
- gb_string_free(s);
- operand->mode = Addressing_Invalid;
- operand->type = t_invalid;;
- return Expr_Expr;
- }
- auto err = check_polymorphic_record_type(c, operand, call);
- if (err == 0) {
- Ast *ident = operand->expr;
- while (ident->kind == Ast_SelectorExpr) {
- Ast *s = ident->SelectorExpr.selector;
- ident = s;
- }
- Type *ot = operand->type;
- GB_ASSERT(ot->kind == Type_Named);
- Entity *e = ot->Named.type_name;
- add_entity_use(c, ident, e);
- add_type_and_value(c, call, Addressing_Type, ot, empty_exact_value);
- } else {
- operand->mode = Addressing_Invalid;
- operand->type = t_invalid;
- }
- } else {
- CHECK_CALL_PARAMETER_MIXTURE_OR_RETURN("type conversion");
- operand->mode = Addressing_Invalid;
- isize arg_count = args.count;
- switch (arg_count) {
- case 0:
- {
- gbString str = type_to_string(t);
- error(call, "Missing argument in conversion to '%s'", str);
- gb_string_free(str);
- } break;
- default:
- {
- gbString str = type_to_string(t);
- if (t->kind == Type_Basic) {
- ERROR_BLOCK();
- switch (t->Basic.kind) {
- case Basic_complex32:
- case Basic_complex64:
- case Basic_complex128:
- error(call, "Too many arguments in conversion to '%s'", str);
- error_line("\tSuggestion: %s(1+2i) or construct with 'complex'\n", str);
- break;
- case Basic_quaternion64:
- case Basic_quaternion128:
- case Basic_quaternion256:
- error(call, "Too many arguments in conversion to '%s'", str);
- error_line("\tSuggestion: %s(1+2i+3j+4k) or construct with 'quaternion'\n", str);
- break;
- default:
- error(call, "Too many arguments in conversion to '%s'", str);
- }
- } else {
- error(call, "Too many arguments in conversion to '%s'", str);
- }
- gb_string_free(str);
- } break;
- case 1: {
- Ast *arg = args[0];
- if (arg->kind == Ast_FieldValue) {
- error(call, "'field = value' cannot be used in a type conversion");
- arg = arg->FieldValue.value;
- // NOTE(bill): Carry on the cast regardless
- }
- check_expr_with_type_hint(c, operand, arg, t);
- if (operand->mode != Addressing_Invalid) {
- if (is_type_polymorphic(t)) {
- error(call, "A polymorphic type cannot be used in a type conversion");
- } else {
- // NOTE(bill): Otherwise the compiler can override the polymorphic type
- // as it assumes it is determining the type
- check_cast(c, operand, t);
- }
- }
- operand->type = t;
- operand->expr = call;
- if (operand->mode != Addressing_Invalid) {
- update_untyped_expr_type(c, arg, t, false);
- }
- break;
- }
- }
- }
- return Expr_Expr;
- }
- if (operand->mode == Addressing_Builtin) {
- CHECK_CALL_PARAMETER_MIXTURE_OR_RETURN("builtin call");
- i32 id = operand->builtin_id;
- Entity *e = entity_of_node(operand->expr);
- if (e != nullptr && e->token.string == "expand_to_tuple") {
- error(operand->expr, "'expand_to_tuple' has been replaced with 'expand_values'");
- }
- if (!check_builtin_procedure(c, operand, call, id, type_hint)) {
- operand->mode = Addressing_Invalid;
- operand->type = t_invalid;
- }
- operand->expr = call;
- return builtin_procs[id].kind;
- }
- CHECK_CALL_PARAMETER_MIXTURE_OR_RETURN(operand->mode == Addressing_ProcGroup ? "procedure group call": "procedure call", true);
- Entity *initial_entity = entity_of_node(operand->expr);
- if (initial_entity != nullptr && initial_entity->kind == Entity_Procedure) {
- if (initial_entity->Procedure.deferred_procedure.entity != nullptr) {
- call->viral_state_flags |= ViralStateFlag_ContainsDeferredProcedure;
- if (c->decl) {
- c->decl->defer_used += 1;
- }
- }
- add_entity_use(c, operand->expr, initial_entity);
- if (initial_entity->Procedure.entry_point_only) {
- if (c->curr_proc_decl && c->curr_proc_decl->entity == c->info->entry_point) {
- // Okay
- } else {
- error(operand->expr, "Procedures with the attribute '@(entry_point_only)' can only be called directly from the user-level entry point procedure");
- }
- }
- }
- if (operand->mode != Addressing_ProcGroup) {
- Type *proc_type = base_type(operand->type);
- bool valid_type = (proc_type != nullptr) && is_type_proc(proc_type);
- bool valid_mode = is_operand_value(*operand);
- if (!valid_type || !valid_mode) {
- Ast *e = operand->expr;
- gbString str = expr_to_string(e);
- gbString type_str = type_to_string(operand->type);
- error(e, "Cannot call a non-procedure: '%s' of type '%s'", str, type_str);
- gb_string_free(type_str);
- gb_string_free(str);
- operand->mode = Addressing_Invalid;
- operand->expr = call;
- return Expr_Stmt;
- }
- }
- CallArgumentData data = check_call_arguments(c, operand, call);
- Type *result_type = data.result_type;
- gb_zero_item(operand);
- operand->expr = call;
- if (result_type == t_invalid) {
- operand->mode = Addressing_Invalid;
- operand->type = t_invalid;
- return Expr_Stmt;
- }
- Type *pt = base_type(operand->type);
- if (pt == nullptr) {
- pt = t_invalid;
- }
- if (pt == t_invalid) {
- if (operand->expr != nullptr && operand->expr->kind == Ast_CallExpr) {
- pt = type_of_expr(operand->expr->CallExpr.proc);
- }
- if (pt == t_invalid && data.gen_entity) {
- pt = data.gen_entity->type;
- }
- }
- pt = base_type(pt);
- if (pt->kind == Type_Proc && pt->Proc.calling_convention == ProcCC_Odin) {
- if ((c->scope->flags & ScopeFlag_ContextDefined) == 0) {
- ERROR_BLOCK();
- error(call, "'context' has not been defined within this scope, but is required for this procedure call");
- error_line("\tSuggestion: 'context = runtime.default_context()'");
- }
- }
- if (result_type == nullptr) {
- operand->mode = Addressing_NoValue;
- } else {
- GB_ASSERT(is_type_tuple(result_type));
- isize count = result_type->Tuple.variables.count;
- switch (count) {
- case 0:
- operand->mode = Addressing_NoValue;
- break;
- case 1:
- operand->mode = Addressing_Value;
- operand->type = result_type->Tuple.variables[0]->type;
- break;
- default:
- operand->mode = Addressing_Value;
- operand->type = result_type;
- break;
- }
- }
- bool is_call_inlined = false;
- switch (inlining) {
- case ProcInlining_inline:
- is_call_inlined = true;
- if (proc != nullptr) {
- Entity *e = entity_from_expr(proc);
- if (e != nullptr && e->kind == Entity_Procedure) {
- DeclInfo *decl = e->decl_info;
- if (decl->proc_lit) {
- ast_node(pl, ProcLit, decl->proc_lit);
- if (pl->inlining == ProcInlining_no_inline) {
- error(call, "'#force_inline' cannot be applied to a procedure that has been marked as '#force_no_inline'");
- }
- }
- }
- }
- break;
- case ProcInlining_no_inline:
- break;
- case ProcInlining_none:
- if (proc != nullptr) {
- Entity *e = entity_from_expr(proc);
- if (e != nullptr && e->kind == Entity_Procedure) {
- DeclInfo *decl = e->decl_info;
- if (decl->proc_lit) {
- ast_node(pl, ProcLit, decl->proc_lit);
- if (pl->inlining == ProcInlining_inline) {
- is_call_inlined = true;
- }
- }
- }
- }
- }
- {
- String invalid;
- if (pt->kind == Type_Proc && pt->Proc.require_target_feature.len != 0) {
- if (!check_target_feature_is_valid_for_target_arch(pt->Proc.require_target_feature, &invalid)) {
- error(call, "Called procedure requires target feature '%.*s' which is invalid for the build target", LIT(invalid));
- } else if (!check_target_feature_is_enabled(pt->Proc.require_target_feature, &invalid)) {
- error(call, "Calling this procedure requires target feature '%.*s' to be enabled", LIT(invalid));
- }
- }
- if (pt->kind == Type_Proc && pt->Proc.enable_target_feature.len != 0) {
- if (!check_target_feature_is_valid_for_target_arch(pt->Proc.enable_target_feature, &invalid)) {
- error(call, "Called procedure enables target feature '%.*s' which is invalid for the build target", LIT(invalid));
- }
- // NOTE: Due to restrictions in LLVM you can not inline calls with a superset of features.
- if (is_call_inlined) {
- if (c->curr_proc_decl == nullptr) {
- error(call, "Calling a '#force_inline' procedure that enables target features is not allowed at file scope");
- } else {
- GB_ASSERT(c->curr_proc_decl->entity);
- GB_ASSERT(c->curr_proc_decl->entity->type->kind == Type_Proc);
- String scope_features = c->curr_proc_decl->entity->type->Proc.enable_target_feature;
- if (!check_target_feature_is_superset_of(scope_features, pt->Proc.enable_target_feature, &invalid)) {
- ERROR_BLOCK();
- error(call, "Inlined procedure enables target feature '%.*s', this requires the calling procedure to at least enable the same feature", LIT(invalid));
- error_line("\tSuggested Example: @(enable_target_feature=\"%.*s\")\n", LIT(invalid));
- }
- }
- }
- }
- }
- operand->expr = call;
- {
- Type *type = nullptr;
- if (operand->expr != nullptr && operand->expr->kind == Ast_CallExpr) {
- type = type_of_expr(operand->expr->CallExpr.proc);
- }
- if (type == nullptr) {
- type = pt;
- }
- type = base_type(type);
- if (type->kind == Type_Proc && type->Proc.optional_ok && type->Proc.result_count > 0) {
- operand->mode = Addressing_OptionalOk;
- operand->type = type->Proc.results->Tuple.variables[0]->type;
- if (operand->expr != nullptr && operand->expr->kind == Ast_CallExpr) {
- operand->expr->CallExpr.optional_ok_one = true;
- }
- }
- }
- return Expr_Expr;
- }
- gb_internal void check_expr_with_type_hint(CheckerContext *c, Operand *o, Ast *e, Type *t) {
- check_expr_base(c, o, e, t);
- check_not_tuple(c, o);
- char const *err_str = nullptr;
- switch (o->mode) {
- case Addressing_NoValue:
- err_str = "used as a value";
- break;
- case Addressing_Type:
- if (t == nullptr || !is_type_typeid(t)) {
- err_str = "is not an expression but a type, in this context it is ambiguous";
- }
- break;
- case Addressing_Builtin:
- err_str = "must be called";
- break;
- }
- if (err_str != nullptr) {
- gbString str = expr_to_string(e);
- error(e, "'%s' %s", str, err_str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- }
- }
- gb_internal bool check_set_index_data(Operand *o, Type *t, bool indirection, i64 *max_count, Type *original_type) {
- switch (t->kind) {
- case Type_Basic:
- if (t->Basic.kind == Basic_string) {
- if (o->mode == Addressing_Constant) {
- *max_count = o->value.value_string.len;
- }
- if (o->mode != Addressing_Constant) {
- o->mode = Addressing_Value;
- }
- o->type = t_u8;
- return true;
- } else if (t->Basic.kind == Basic_UntypedString) {
- if (o->mode == Addressing_Constant) {
- *max_count = o->value.value_string.len;
- o->type = t_u8;
- return true;
- }
- return false;
- }
- break;
- case Type_MultiPointer:
- o->type = t->MultiPointer.elem;
- if (o->mode != Addressing_Constant) {
- o->mode = Addressing_Variable;
- }
- return true;
- case Type_Array:
- *max_count = t->Array.count;
- if (indirection) {
- o->mode = Addressing_Variable;
- } else if (o->mode != Addressing_Variable &&
- o->mode != Addressing_Constant) {
- o->mode = Addressing_Value;
- }
- o->type = t->Array.elem;
- return true;
- case Type_EnumeratedArray:
- *max_count = t->EnumeratedArray.count;
- if (indirection) {
- o->mode = Addressing_Variable;
- } else if (o->mode != Addressing_Variable &&
- o->mode != Addressing_Constant) {
- o->mode = Addressing_Value;
- }
- o->type = t->EnumeratedArray.elem;
- return true;
-
- case Type_Matrix:
- if (indirection) {
- o->mode = Addressing_Variable;
- } else if (o->mode != Addressing_Variable) {
- o->mode = Addressing_Value;
- }
- if (t->Matrix.is_row_major) {
- *max_count = t->Matrix.row_count;
- o->type = alloc_type_array(t->Matrix.elem, t->Matrix.column_count);
- } else {
- *max_count = t->Matrix.column_count;
- o->type = alloc_type_array(t->Matrix.elem, t->Matrix.row_count);
- }
- return true;
- case Type_Slice:
- o->type = t->Slice.elem;
- if (o->mode != Addressing_Constant) {
- o->mode = Addressing_Variable;
- }
- return true;
- case Type_DynamicArray:
- o->type = t->DynamicArray.elem;
- if (o->mode != Addressing_Constant) {
- o->mode = Addressing_Variable;
- }
- return true;
- case Type_Struct:
- if (t->Struct.soa_kind != StructSoa_None) {
- if (t->Struct.soa_kind == StructSoa_Fixed) {
- *max_count = t->Struct.soa_count;
- }
- o->type = t->Struct.soa_elem;
- if (o->mode == Addressing_SoaVariable || o->mode == Addressing_Variable || indirection) {
- o->mode = Addressing_SoaVariable;
- } else {
- o->mode = Addressing_Value;
- }
- return true;
- }
- return false;
- }
- if (is_type_pointer(original_type) && indirection) {
- Type *ptr = base_type(original_type);
- if (ptr->kind == Type_MultiPointer && o->mode == Addressing_SoaVariable) {
- o->type = ptr->MultiPointer.elem;
- o->mode = Addressing_Value;
- return true;
- }
- }
- return false;
- }
- gb_internal bool ternary_compare_types(Type *x, Type *y) {
- if (is_type_untyped_uninit(x)) {
- return true;
- } else if (is_type_untyped_nil(x) && type_has_nil(y)) {
- return true;
- } else if (is_type_untyped_uninit(y)) {
- return true;
- } else if (is_type_untyped_nil(y) && type_has_nil(x)) {
- return true;
- }
- return are_types_identical(x, y);
- }
- gb_internal bool check_range(CheckerContext *c, Ast *node, bool is_for_loop, Operand *x, Operand *y, ExactValue *inline_for_depth_, Type *type_hint=nullptr) {
- if (!is_ast_range(node)) {
- return false;
- }
- ast_node(ie, BinaryExpr, node);
- check_expr_with_type_hint(c, x, ie->left, type_hint);
- if (x->mode == Addressing_Invalid) {
- return false;
- }
- check_expr_with_type_hint(c, y, ie->right, type_hint);
- if (y->mode == Addressing_Invalid) {
- return false;
- }
- convert_to_typed(c, x, y->type);
- if (x->mode == Addressing_Invalid) {
- return false;
- }
- convert_to_typed(c, y, x->type);
- if (y->mode == Addressing_Invalid) {
- return false;
- }
- convert_to_typed(c, x, default_type(y->type));
- if (x->mode == Addressing_Invalid) {
- return false;
- }
- convert_to_typed(c, y, default_type(x->type));
- if (y->mode == Addressing_Invalid) {
- return false;
- }
- if (!are_types_identical(x->type, y->type)) {
- if (x->type != t_invalid &&
- y->type != t_invalid) {
- gbString xt = type_to_string(x->type);
- gbString yt = type_to_string(y->type);
- gbString expr_str = expr_to_string(x->expr);
- error(ie->op, "Mismatched types in interval expression '%s' : '%s' vs '%s'", expr_str, xt, yt);
- gb_string_free(expr_str);
- gb_string_free(yt);
- gb_string_free(xt);
- }
- return false;
- }
- Type *type = x->type;
- if (is_for_loop) {
- if (!is_type_integer(type) && !is_type_float(type) && !is_type_enum(type)) {
- error(ie->op, "Only numerical types are allowed within interval expressions");
- return false;
- }
- } else {
- if (!is_type_integer(type) && !is_type_float(type) && !is_type_pointer(type) && !is_type_enum(type)) {
- error(ie->op, "Only numerical and pointer types are allowed within interval expressions");
- return false;
- }
- }
- if (x->mode == Addressing_Constant &&
- y->mode == Addressing_Constant) {
- ExactValue a = x->value;
- ExactValue b = y->value;
- GB_ASSERT(are_types_identical(x->type, y->type));
- TokenKind op = Token_Lt;
- switch (ie->op.kind) {
- case Token_Ellipsis: op = Token_LtEq; break; // ..
- case Token_RangeFull: op = Token_LtEq; break; // ..=
- case Token_RangeHalf: op = Token_Lt; break; // ..<
- default: error(ie->op, "Invalid range operator"); break;
- }
- bool ok = compare_exact_values(op, a, b);
- if (!ok) {
- // TODO(bill): Better error message
- error(ie->op, "Invalid interval range");
- return false;
- }
- ExactValue inline_for_depth = exact_value_sub(b, a);
- if (ie->op.kind != Token_RangeHalf) {
- inline_for_depth = exact_value_increment_one(inline_for_depth);
- }
- if (inline_for_depth_) *inline_for_depth_ = inline_for_depth;
- } else if (inline_for_depth_ != nullptr) {
- error(ie->op, "Interval expressions must be constant");
- return false;
- }
- add_type_and_value(c, ie->left, x->mode, x->type, x->value);
- add_type_and_value(c, ie->right, y->mode, y->type, y->value);
- return true;
- }
- gb_internal bool check_is_operand_compound_lit_constant(CheckerContext *c, Operand *o) {
- if (is_operand_nil(*o)) {
- return true;
- }
- Ast *expr = unparen_expr(o->expr);
- if (expr != nullptr) {
- Entity *e = strip_entity_wrapping(entity_from_expr(expr));
- if (e != nullptr && e->kind == Entity_Procedure) {
- return true;
- }
- if (expr->kind == Ast_ProcLit) {
- add_type_and_value(c, expr, Addressing_Constant, type_of_expr(expr), exact_value_procedure(expr));
- return true;
- }
- }
- return o->mode == Addressing_Constant;
- }
- gb_internal bool attempt_implicit_selector_expr(CheckerContext *c, Operand *o, AstImplicitSelectorExpr *ise, Type *th) {
- if (is_type_enum(th)) {
- Type *enum_type = base_type(th);
- GB_ASSERT(enum_type->kind == Type_Enum);
- String name = ise->selector->Ident.token.string;
- Entity *e = scope_lookup_current(enum_type->Enum.scope, name);
- if (e == nullptr) {
- return false;
- }
- GB_ASSERT(are_types_identical(base_type(e->type), enum_type));
- GB_ASSERT(e->kind == Entity_Constant);
- o->value = e->Constant.value;
- o->mode = Addressing_Constant;
- o->type = e->type;
- return true;
- }
- if (is_type_union(th)) {
- TEMPORARY_ALLOCATOR_GUARD();
- Type *union_type = base_type(th);
- auto operands = array_make<Operand>(temporary_allocator(), 0, union_type->Union.variants.count);
- for (Type *vt : union_type->Union.variants) {
- Operand x = {};
- if (attempt_implicit_selector_expr(c, &x, ise, vt)) {
- array_add(&operands, x);
- }
- }
- if (operands.count == 1) {
- *o = operands[0];
- return true;
- }
- }
- return false;
- }
- gb_internal ExprKind check_implicit_selector_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ast_node(ise, ImplicitSelectorExpr, node);
- o->type = t_invalid;
- o->expr = node;
- o->mode = Addressing_Invalid;
- Type *th = type_hint;
- if (th == nullptr) {
- gbString str = expr_to_string(node);
- error(node, "Cannot determine type for implicit selector expression '%s'", str);
- gb_string_free(str);
- return Expr_Expr;
- }
- o->type = th;
- bool ok = attempt_implicit_selector_expr(c, o, ise, th);
- if (!ok) {
- String name = ise->selector->Ident.token.string;
- if (is_type_enum(th)) {
- ERROR_BLOCK();
- Type *bt = base_type(th);
- GB_ASSERT(bt->kind == Type_Enum);
- gbString typ = type_to_string(th);
- defer (gb_string_free(typ));
- error(node, "Undeclared name '%.*s' for type '%s'", LIT(name), typ);
- check_did_you_mean_type(name, bt->Enum.fields);
- } else if (is_type_bit_set(th) && is_type_enum(th->BitSet.elem)) {
- ERROR_BLOCK();
- gbString typ = type_to_string(th);
- gbString str = expr_to_string(node);
- error(node, "Cannot convert enum value to '%s'", typ);
- error_line("\tSuggestion: Did you mean '{ %s }'?\n", str);
- gb_string_free(typ);
- gb_string_free(str);
- } else {
- gbString typ = type_to_string(th);
- gbString str = expr_to_string(node);
- error(node, "Invalid type '%s' for implicit selector expression '%s'", typ, str);
- gb_string_free(str);
- gb_string_free(typ);
- }
- }
- o->expr = node;
- return Expr_Expr;
- }
- gb_internal void check_promote_optional_ok(CheckerContext *c, Operand *x, Type **val_type_, Type **ok_type_, bool change_operand) {
- switch (x->mode) {
- case Addressing_MapIndex:
- case Addressing_OptionalOk:
- case Addressing_OptionalOkPtr:
- if (val_type_) *val_type_ = x->type;
- break;
- default:
- if (ok_type_) *ok_type_ = x->type;
- return;
- }
- Ast *expr = unparen_expr(x->expr);
- if (expr->kind == Ast_CallExpr) {
- Type *pt = base_type(type_of_expr(expr->CallExpr.proc));
- if (is_type_proc(pt)) {
- Type *tuple = pt->Proc.results;
- if (pt->Proc.result_count >= 2) {
- if (ok_type_) *ok_type_ = tuple->Tuple.variables[1]->type;
- }
- if (change_operand) {
- expr->CallExpr.optional_ok_one = false;
- x->type = tuple;
- add_type_and_value(c, x->expr, x->mode, tuple, x->value);
- }
- return;
- }
- }
- Type *tuple = make_optional_ok_type(x->type);
- if (ok_type_) *ok_type_ = tuple->Tuple.variables[1]->type;
- if (change_operand) {
- add_type_and_value(c, x->expr, x->mode, tuple, x->value);
- x->type = tuple;
- GB_ASSERT(is_type_tuple(type_of_expr(x->expr)));
- }
- }
- gb_internal void check_matrix_index_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ast_node(ie, MatrixIndexExpr, node);
-
- check_expr(c, o, ie->expr);
- node->viral_state_flags |= ie->expr->viral_state_flags;
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return;
- }
-
- Type *t = base_type(type_deref(o->type));
- bool is_ptr = is_type_pointer(o->type);
- bool is_const = o->mode == Addressing_Constant;
-
- if (t->kind != Type_Matrix) {
- gbString str = expr_to_string(o->expr);
- gbString type_str = type_to_string(o->type);
- defer (gb_string_free(str));
- defer (gb_string_free(type_str));
- if (is_const) {
- error(o->expr, "Cannot use matrix indexing on constant '%s' of type '%s'", str, type_str);
- } else {
- error(o->expr, "Cannot use matrix indexing on '%s' of type '%s'", str, type_str);
- }
- o->mode = Addressing_Invalid;
- o->expr = node;
- return;
- }
- o->type = t->Matrix.elem;
- if (is_ptr) {
- o->mode = Addressing_Variable;
- } else if (o->mode != Addressing_Variable) {
- o->mode = Addressing_Value;
- }
-
- if (ie->row_index == nullptr) {
- gbString str = expr_to_string(o->expr);
- error(o->expr, "Missing row index for '%s'", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return;
- }
- if (ie->column_index == nullptr) {
- gbString str = expr_to_string(o->expr);
- error(o->expr, "Missing column index for '%s'", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return;
- }
-
- i64 row_count = t->Matrix.row_count;
- i64 column_count = t->Matrix.column_count;
-
- i64 row_index = 0;
- i64 column_index = 0;
- bool row_ok = check_index_value(c, t, false, ie->row_index, row_count, &row_index, nullptr);
- bool column_ok = check_index_value(c, t, false, ie->column_index, column_count, &column_index, nullptr);
- if (is_const && (ie->row_index->tav.mode != Addressing_Constant || ie->column_index->tav.mode != Addressing_Constant)) {
- error(o->expr, "Cannot index constant matrix with non-constant indices '%s'", expr_to_string(node));
- }
- gb_unused(row_ok);
- gb_unused(column_ok);
- }
- struct TypeAndToken {
- Type *type;
- Token token;
- };
- typedef PtrMap<uintptr, TypeAndToken> SeenMap;
- gb_internal void add_constant_switch_case(CheckerContext *ctx, SeenMap *seen, Operand operand, bool use_expr = true) {
- if (operand.mode != Addressing_Constant) {
- return;
- }
- if (operand.value.kind == ExactValue_Invalid) {
- return;
- }
- uintptr key = hash_exact_value(operand.value);
- GB_ASSERT(key != 0);
- isize count = multi_map_count(seen, key);
- if (count) {
- TEMPORARY_ALLOCATOR_GUARD();
- TypeAndToken *taps = gb_alloc_array(temporary_allocator(), TypeAndToken, count);
- multi_map_get_all(seen, key, taps);
- for (isize i = 0; i < count; i++) {
- TypeAndToken tap = taps[i];
- Operand to = {};
- to.mode = Addressing_Value;
- to.type = tap.type;
- if (!check_is_assignable_to_with_score(ctx, &to, operand.type, nullptr)) {
- continue;
- }
- TokenPos pos = tap.token.pos;
- if (use_expr) {
- gbString expr_str = expr_to_string(operand.expr);
- error(operand.expr,
- "Duplicate case '%s'\n"
- "\tprevious case at %s",
- expr_str,
- token_pos_to_string(pos));
- gb_string_free(expr_str);
- } else {
- error(operand.expr, "Duplicate case found with previous case at %s", token_pos_to_string(pos));
- }
- return;
- }
- }
- TypeAndToken tap = {operand.type, ast_token(operand.expr)};
- multi_map_insert(seen, key, tap);
- }
- gb_internal void add_to_seen_map(CheckerContext *ctx, SeenMap *seen, TokenKind upper_op, Operand const &x, Operand const &lhs, Operand const &rhs) {
- if (is_type_enum(x.type)) {
- // TODO(bill): Fix this logic so it's fast!!!
- i64 v0 = exact_value_to_i64(lhs.value);
- i64 v1 = exact_value_to_i64(rhs.value);
- Operand v = {};
- v.mode = Addressing_Constant;
- v.type = x.type;
- v.expr = x.expr;
- Type *bt = base_type(x.type);
- GB_ASSERT(bt->kind == Type_Enum);
- for (i64 vi = v0; vi <= v1; vi++) {
- if (upper_op != Token_LtEq && vi == v1) {
- break;
- }
- v.value = exact_value_i64(vi);
- add_constant_switch_case(ctx, seen, v);
- }
- } else {
- add_constant_switch_case(ctx, seen, lhs);
- if (upper_op == Token_LtEq) {
- add_constant_switch_case(ctx, seen, rhs);
- }
- }
- }
- gb_internal void add_to_seen_map(CheckerContext *ctx, SeenMap *seen, Operand const &x) {
- add_constant_switch_case(ctx, seen, x);
- }
- gb_internal ExprKind check_basic_directive_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ast_node(bd, BasicDirective, node);
- ExprKind kind = Expr_Expr;
- o->mode = Addressing_Constant;
- String name = bd->name.string;
- if (name == "file") {
- String file = get_file_path_string(bd->token.pos.file_id);
- switch (build_context.source_code_location_info) {
- case SourceCodeLocationInfo_Normal:
- break;
- case SourceCodeLocationInfo_Obfuscated:
- file = obfuscate_string(file, "F");
- break;
- case SourceCodeLocationInfo_Filename:
- file = last_path_element(file);
- break;
- case SourceCodeLocationInfo_None:
- file = str_lit("");
- break;
- }
- o->type = t_untyped_string;
- o->value = exact_value_string(file);
- } else if (name == "directory") {
- String file = get_file_path_string(bd->token.pos.file_id);
- String path = dir_from_path(file);
- switch (build_context.source_code_location_info) {
- case SourceCodeLocationInfo_Normal:
- break;
- case SourceCodeLocationInfo_Obfuscated:
- path = obfuscate_string(path, "D");
- break;
- case SourceCodeLocationInfo_Filename:
- path = last_path_element(path);
- break;
- case SourceCodeLocationInfo_None:
- path = str_lit("");
- break;
- }
- o->type = t_untyped_string;
- o->value = exact_value_string(path);
- } else if (name == "line") {
- i32 line = bd->token.pos.line;
- switch (build_context.source_code_location_info) {
- case SourceCodeLocationInfo_Normal:
- break;
- case SourceCodeLocationInfo_Obfuscated:
- line = obfuscate_i32(line);
- break;
- case SourceCodeLocationInfo_Filename:
- break;
- case SourceCodeLocationInfo_None:
- line = 0;
- break;
- }
- o->type = t_untyped_integer;
- o->value = exact_value_i64(line);
- } else if (name == "procedure") {
- if (c->curr_proc_decl == nullptr) {
- error(node, "#procedure may only be used within procedures");
- o->type = t_untyped_string;
- o->value = exact_value_string(str_lit(""));
- } else {
- String p = c->proc_name;
- switch (build_context.source_code_location_info) {
- case SourceCodeLocationInfo_Normal:
- break;
- case SourceCodeLocationInfo_Obfuscated:
- p = obfuscate_string(p, "P");
- break;
- case SourceCodeLocationInfo_Filename:
- break;
- case SourceCodeLocationInfo_None:
- p = str_lit("");
- break;
- }
- o->type = t_untyped_string;
- o->value = exact_value_string(p);
- }
- } else if (name == "caller_location") {
- init_core_source_code_location(c->checker);
- error(node, "#caller_location may only be used as a default argument parameter");
- o->type = t_source_code_location;
- o->mode = Addressing_Value;
- } else if (name == "caller_expression") {
- error(node, "#caller_expression may only be used as a default argument parameter");
- o->type = t_string;
- o->mode = Addressing_Value;
- } else if (name == "branch_location") {
- if (!c->in_defer) {
- error(node, "#branch_location may only be used within a 'defer' statement");
- } else if (c->curr_proc_decl) {
- Entity *e = c->curr_proc_decl->entity;
- if (e != nullptr) {
- GB_ASSERT(e->kind == Entity_Procedure);
- e->Procedure.uses_branch_location = true;
- }
- }
- o->type = t_source_code_location;
- o->mode = Addressing_Value;
- } else {
- if (name == "location") {
- init_core_source_code_location(c->checker);
- error(node, "'#location' must be used as a call, i.e. #location(proc), where #location() defaults to the procedure in which it was used.");
- o->type = t_source_code_location;
- o->mode = Addressing_Value;
- } else if (
- name == "assert" ||
- name == "defined" ||
- name == "config" ||
- name == "exists" ||
- name == "load" ||
- name == "load_hash" ||
- name == "load_directory" ||
- name == "load_or"
- ) {
- error(node, "'#%.*s' must be used as a call", LIT(name));
- o->type = t_invalid;
- o->mode = Addressing_Invalid;
- } else {
- error(node, "Unknown directive: #%.*s", LIT(name));
- o->type = t_invalid;
- o->mode = Addressing_Invalid;
- }
- }
- return kind;
- }
- gb_internal ExprKind check_ternary_if_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ExprKind kind = Expr_Expr;
- Operand cond = {Addressing_Invalid};
- ast_node(te, TernaryIfExpr, node);
- check_expr(c, &cond, te->cond);
- node->viral_state_flags |= te->cond->viral_state_flags;
- if (cond.mode != Addressing_Invalid && !is_type_boolean(cond.type)) {
- error(te->cond, "Non-boolean condition in ternary if expression");
- }
- Operand x = {Addressing_Invalid};
- Operand y = {Addressing_Invalid};
- check_expr_or_type(c, &x, te->x, type_hint);
- node->viral_state_flags |= te->x->viral_state_flags;
- if (te->y != nullptr) {
- Type *th = type_hint;
- if (type_hint == nullptr && is_type_typed(x.type)) {
- th = x.type;
- }
- check_expr_or_type(c, &y, te->y, th);
- node->viral_state_flags |= te->y->viral_state_flags;
- } else {
- error(node, "A ternary expression must have an else clause");
- return kind;
- }
- if (x.mode == Addressing_Type || y.mode == Addressing_Type) {
- Ast *type_expr = (x.mode == Addressing_Type) ? x.expr : y.expr;
- gbString type_string = expr_to_string(type_expr);
- error(node, "Type %s is invalid operand for ternary if expression", type_string);
- gb_string_free(type_string);
- return kind;
- }
- bool use_type_hint = type_hint != nullptr && (is_operand_nil(x) || is_operand_nil(y));
- convert_to_typed(c, &x, use_type_hint ? type_hint : y.type);
- if (x.mode == Addressing_Invalid) {
- return kind;
- }
- convert_to_typed(c, &y, use_type_hint ? type_hint : x.type);
- if (y.mode == Addressing_Invalid) {
- x.mode = Addressing_Invalid;
- return kind;
- }
- // NOTE(bill, 2023-01-30): Allow for expression like this:
- // x: union{f32} = f32(123) if cond else nil
- if (type_hint && !is_type_any(type_hint)) {
- if (check_is_assignable_to(c, &x, type_hint) && check_is_assignable_to(c, &y, type_hint)) {
- check_cast(c, &x, type_hint);
- check_cast(c, &y, type_hint);
- }
- }
- if (!ternary_compare_types(x.type, y.type)) {
- gbString its = type_to_string(x.type);
- gbString ets = type_to_string(y.type);
- error(node, "Mismatched types in ternary if expression, %s vs %s", its, ets);
- gb_string_free(ets);
- gb_string_free(its);
- return kind;
- }
- o->type = x.type;
- if (is_type_untyped_nil(o->type) || is_type_untyped_uninit(o->type)) {
- o->type = y.type;
- }
- o->mode = Addressing_Value;
- o->expr = node;
- if (type_hint != nullptr && is_type_untyped(o->type) && !is_type_any(type_hint)) {
- if (check_cast_internal(c, &x, type_hint) &&
- check_cast_internal(c, &y, type_hint)) {
- convert_to_typed(c, o, type_hint);
- update_untyped_expr_type(c, node, type_hint, !is_type_untyped(type_hint));
- o->type = type_hint;
- }
- }
- return kind;
- }
- gb_internal ExprKind check_ternary_when_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ExprKind kind = Expr_Expr;
- Operand cond = {};
- ast_node(te, TernaryWhenExpr, node);
- check_expr(c, &cond, te->cond);
- node->viral_state_flags |= te->cond->viral_state_flags;
- if (cond.mode != Addressing_Constant || !is_type_boolean(cond.type)) {
- error(te->cond, "Expected a constant boolean condition in ternary when expression");
- return kind;
- }
- if (cond.value.value_bool) {
- check_expr_or_type(c, o, te->x, type_hint);
- node->viral_state_flags |= te->x->viral_state_flags;
- } else {
- if (te->y != nullptr) {
- check_expr_or_type(c, o, te->y, type_hint);
- node->viral_state_flags |= te->y->viral_state_flags;
- } else {
- error(node, "A ternary when expression must have an else clause");
- return kind;
- }
- }
- return kind;
- }
- gb_internal ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ast_node(oe, OrElseExpr, node);
- String name = oe->token.string;
- Ast *arg = oe->x;
- Ast *default_value = oe->y;
- Operand x = {};
- Operand y = {};
- // NOTE(bill, 2022-08-11): edge case to handle #load(path) or_else default
- if (is_load_directive_call(arg)) {
- LoadDirectiveResult res = check_load_directive(c, &x, arg, type_hint, false);
- // Allow for chaining of '#load(path) or_else #load(path)'
- if (!(is_load_directive_call(default_value) && res == LoadDirective_Success)) {
- bool y_is_diverging = false;
- check_expr_base(c, &y, default_value, x.type);
- switch (y.mode) {
- case Addressing_NoValue:
- if (is_diverging_expr(y.expr)) {
- // Allow
- y.mode = Addressing_Value;
- y_is_diverging = true;
- } else {
- error_operand_no_value(&y);
- y.mode = Addressing_Invalid;
- }
- break;
- case Addressing_Type:
- error_operand_not_expression(&y);
- y.mode = Addressing_Invalid;
- break;
- }
- if (y.mode == Addressing_Invalid) {
- o->mode = Addressing_Value;
- o->type = t_invalid;
- o->expr = node;
- return Expr_Expr;
- }
- if (!y_is_diverging) {
- check_assignment(c, &y, x.type, name);
- if (y.mode != Addressing_Constant) {
- error(y.expr, "expected a constant expression on the right-hand side of 'or_else' in conjuction with '#load'");
- }
- }
- }
- if (res == LoadDirective_Success) {
- *o = x;
- } else {
- *o = y;
- }
- o->expr = node;
- return Expr_Expr;
- }
- check_multi_expr_with_type_hint(c, &x, arg, type_hint);
- if (x.mode == Addressing_Invalid) {
- o->mode = Addressing_Value;
- o->type = t_invalid;
- o->expr = node;
- return Expr_Expr;
- }
- Type *left_type = nullptr;
- Type *right_type = nullptr;
- check_or_else_split_types(c, &x, name, &left_type, &right_type);
- add_type_and_value(c, arg, x.mode, x.type, x.value);
- bool y_is_diverging = false;
- check_expr_base(c, &y, default_value, left_type);
- switch (y.mode) {
- case Addressing_NoValue:
- if (is_diverging_expr(y.expr)) {
- // Allow
- y.mode = Addressing_Value;
- y_is_diverging = true;
- } else {
- error_operand_no_value(&y);
- y.mode = Addressing_Invalid;
- }
- break;
- case Addressing_Type:
- error_operand_not_expression(&y);
- y.mode = Addressing_Invalid;
- break;
- }
- if (y.mode == Addressing_Invalid) {
- o->mode = Addressing_Value;
- o->type = t_invalid;
- o->expr = node;
- return Expr_Expr;
- }
- if (left_type != nullptr) {
- if (!y_is_diverging) {
- if (is_type_tuple(left_type)) {
- if (!is_type_tuple(y.type)) {
- error(y.expr, "Found a single value where a %td-valued expression was expected", left_type->Tuple.variables.count);
- } else if (!are_types_identical(left_type, y.type)) {
- gbString xt = type_to_string(left_type);
- gbString yt = type_to_string(y.type);
- error(y.expr, "Mismatched types, expected (%s), got (%s)", xt, yt);
- gb_string_free(yt);
- gb_string_free(xt);
- }
- } else {
- check_assignment(c, &y, left_type, name);
- }
- }
- } else {
- check_or_else_expr_no_value_error(c, name, x, type_hint);
- }
- if (left_type == nullptr) {
- left_type = t_invalid;
- }
- o->mode = Addressing_Value;
- o->type = left_type;
- o->expr = node;
- return Expr_Expr;
- }
- gb_internal ExprKind check_or_return_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ast_node(re, OrReturnExpr, node);
- String name = re->token.string;
- Operand x = {};
- check_multi_expr_with_type_hint(c, &x, re->expr, type_hint);
- if (x.mode == Addressing_Invalid) {
- o->mode = Addressing_Value;
- o->type = t_invalid;
- o->expr = node;
- return Expr_Expr;
- }
- Type *left_type = nullptr;
- Type *right_type = nullptr;
- check_or_return_split_types(c, &x, name, &left_type, &right_type);
- add_type_and_value(c, re->expr, x.mode, x.type, x.value);
- if (right_type == nullptr) {
- check_or_else_expr_no_value_error(c, name, x, type_hint);
- } else {
- Type *proc_type = base_type(c->curr_proc_sig);
- GB_ASSERT(proc_type->kind == Type_Proc);
- Type *result_type = proc_type->Proc.results;
- if (result_type == nullptr) {
- error(node, "'%.*s' requires the current procedure to have at least one return value", LIT(name));
- } else {
- GB_ASSERT(result_type->kind == Type_Tuple);
- auto const &vars = result_type->Tuple.variables;
- Type *end_type = vars[vars.count-1]->type;
- if (vars.count > 1) {
- if (!proc_type->Proc.has_named_results) {
- error(node, "'%.*s' within a procedure with more than 1 return value requires that the return values are named, allowing for early return", LIT(name));
- }
- }
- Operand rhs = {};
- rhs.type = right_type;
- rhs.mode = Addressing_Value;
- if (is_type_boolean(right_type) && is_type_boolean(end_type)) {
- // NOTE(bill): allow implicit conversion between boolean types
- // within 'or_return' to improve the experience using third-party code
- } else if (!check_is_assignable_to(c, &rhs, end_type)) {
- ERROR_BLOCK();
- // TODO(bill): better error message
- gbString a = type_to_string(right_type);
- gbString b = type_to_string(end_type);
- gbString ret_type = type_to_string(result_type);
- error(node, "Cannot assign end value of type '%s' to '%s' in '%.*s'", a, b, LIT(name));
- if (vars.count == 1) {
- error_line("\tProcedure return value type: %s\n", ret_type);
- } else {
- error_line("\tProcedure return value types: (%s)\n", ret_type);
- }
- gb_string_free(ret_type);
- gb_string_free(b);
- gb_string_free(a);
- }
- }
- }
- o->expr = node;
- o->type = left_type;
- if (left_type != nullptr) {
- o->mode = Addressing_Value;
- } else {
- o->mode = Addressing_NoValue;
- }
- if (c->curr_proc_sig == nullptr) {
- error(node, "'%.*s' can only be used within a procedure", LIT(name));
- }
- if (c->in_defer) {
- error(node, "'or_return' cannot be used within a defer statement");
- }
- return Expr_Expr;
- }
- gb_internal ExprKind check_or_branch_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ast_node(be, OrBranchExpr, node);
- String name = be->token.string;
- Operand x = {};
- check_multi_expr_with_type_hint(c, &x, be->expr, type_hint);
- if (x.mode == Addressing_Invalid) {
- o->mode = Addressing_Value;
- o->type = t_invalid;
- o->expr = node;
- return Expr_Expr;
- }
- Type *left_type = nullptr;
- Type *right_type = nullptr;
- check_or_return_split_types(c, &x, name, &left_type, &right_type);
- add_type_and_value(c, be->expr, x.mode, x.type, x.value);
- if (right_type == nullptr) {
- check_or_else_expr_no_value_error(c, name, x, type_hint);
- } else {
- if (is_type_boolean(right_type) || type_has_nil(right_type)) {
- // okay
- } else {
- gbString s = type_to_string(right_type);
- error(node, "'%.*s' requires a boolean or nil-able type, got %s", LIT(name), s);
- gb_string_free(s);
- }
- }
- o->expr = node;
- o->type = left_type;
- if (left_type != nullptr) {
- o->mode = Addressing_Value;
- } else {
- o->mode = Addressing_NoValue;
- }
- if (c->curr_proc_sig == nullptr) {
- error(node, "'%.*s' can only be used within a procedure", LIT(name));
- }
- Ast *label = be->label;
- switch (be->token.kind) {
- case Token_or_break:
- node->viral_state_flags |= ViralStateFlag_ContainsOrBreak;
- if ((c->stmt_flags & Stmt_BreakAllowed) == 0 && label == nullptr) {
- error(be->token, "'%.*s' only allowed in non-inline loops or 'switch' statements", LIT(name));
- }
- break;
- case Token_or_continue:
- if ((c->stmt_flags & Stmt_ContinueAllowed) == 0 && label == nullptr) {
- error(be->token, "'%.*s' only allowed in non-inline loops", LIT(name));
- }
- break;
- }
- if (label != nullptr) {
- if (c->in_defer) {
- error(label, "A labelled '%.*s' cannot be used within a 'defer'", LIT(name));
- return Expr_Expr;
- }
- if (label->kind != Ast_Ident) {
- error(label, "A branch statement's label name must be an identifier");
- return Expr_Expr;
- }
- Ast *ident = label;
- String name = ident->Ident.token.string;
- Operand o = {};
- Entity *e = check_ident(c, &o, ident, nullptr, nullptr, false);
- if (e == nullptr) {
- error(ident, "Undeclared label name: %.*s", LIT(name));
- return Expr_Expr;
- }
- add_entity_use(c, ident, e);
- if (e->kind != Entity_Label) {
- error(ident, "'%.*s' is not a label", LIT(name));
- return Expr_Expr;
- }
- Ast *parent = e->Label.parent;
- GB_ASSERT(parent != nullptr);
- switch (parent->kind) {
- case Ast_BlockStmt:
- case Ast_IfStmt:
- case Ast_SwitchStmt:
- if (be->token.kind != Token_or_break) {
- error(label, "Label '%.*s' can only be used with 'or_break'", LIT(e->token.string));
- }
- break;
- case Ast_RangeStmt:
- case Ast_ForStmt:
- if ((be->token.kind != Token_or_break) && (be->token.kind != Token_or_continue)) {
- error(label, "Label '%.*s' can only be used with 'or_break' and 'or_continue'", LIT(e->token.string));
- }
- break;
- }
- }
- return Expr_Expr;
- }
- gb_internal void check_compound_literal_field_values(CheckerContext *c, Slice<Ast *> const &elems, Operand *o, Type *type, bool &is_constant) {
- Type *bt = base_type(type);
- StringSet fields_visited = {};
- defer (string_set_destroy(&fields_visited));
- StringMap<String> fields_visited_through_raw_union = {};
- defer (string_map_destroy(&fields_visited_through_raw_union));
- String assignment_str = str_lit("structure literal");
- if (bt->kind == Type_BitField) {
- assignment_str = str_lit("bit_field literal");
- }
- for (Ast *elem : elems) {
- if (elem->kind != Ast_FieldValue) {
- error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
- continue;
- }
- ast_node(fv, FieldValue, elem);
- Ast *ident = fv->field;
- if (ident->kind == Ast_ImplicitSelectorExpr) {
- gbString expr_str = expr_to_string(ident);
- error(ident, "Field names do not start with a '.', remove the '.' in structure literal", expr_str);
- gb_string_free(expr_str);
- ident = ident->ImplicitSelectorExpr.selector;
- }
- if (ident->kind != Ast_Ident) {
- gbString expr_str = expr_to_string(ident);
- error(elem, "Invalid field name '%s' in structure literal", expr_str);
- gb_string_free(expr_str);
- continue;
- }
- String name = ident->Ident.token.string;
- Selection sel = lookup_field(type, name, o->mode == Addressing_Type);
- bool is_unknown = sel.entity == nullptr;
- if (is_unknown) {
- error(ident, "Unknown field '%.*s' in structure literal", LIT(name));
- continue;
- }
- Entity *field = nullptr;
- if (bt->kind == Type_Struct) {
- field = bt->Struct.fields[sel.index[0]];
- } else if (bt->kind == Type_BitField) {
- field = bt->BitField.fields[sel.index[0]];
- } else {
- GB_PANIC("Unknown type");
- }
- add_entity_use(c, ident, field);
- if (string_set_update(&fields_visited, name)) {
- if (sel.index.count > 1) {
- if (String *found = string_map_get(&fields_visited_through_raw_union, sel.entity->token.string)) {
- error(ident, "Field '%.*s' is already initialized due to a previously assigned struct #raw_union field '%.*s'", LIT(sel.entity->token.string), LIT(*found));
- } else {
- error(ident, "Duplicate or reused field '%.*s' in %.*s", LIT(sel.entity->token.string), LIT(assignment_str));
- }
- } else {
- error(ident, "Duplicate field '%.*s' in %.*s", LIT(field->token.string), LIT(assignment_str));
- }
- continue;
- } else if (String *found = string_map_get(&fields_visited_through_raw_union, sel.entity->token.string)) {
- error(ident, "Field '%.*s' is already initialized due to a previously assigned struct #raw_union field '%.*s'", LIT(sel.entity->token.string), LIT(*found));
- continue;
- }
- if (sel.indirect) {
- error(ident, "Cannot assign to the %d-nested anonymous indirect field '%.*s' in a %.*s", cast(int)sel.index.count-1, LIT(name), LIT(assignment_str));
- continue;
- }
- if (sel.index.count > 1) {
- GB_ASSERT(bt->kind == Type_Struct);
- if (is_constant) {
- Type *ft = type;
- for (i32 index : sel.index) {
- Type *bt = base_type(ft);
- switch (bt->kind) {
- case Type_Struct:
- if (bt->Struct.is_raw_union) {
- is_constant = false;
- break;
- }
- ft = bt->Struct.fields[index]->type;
- break;
- case Type_Array:
- ft = bt->Array.elem;
- break;
- case Type_BitField:
- is_constant = false;
- ft = bt->BitField.fields[index]->type;
- break;
- default:
- GB_PANIC("invalid type: %s", type_to_string(ft));
- break;
- }
- }
- if (is_constant &&
- (is_type_any(ft) || is_type_union(ft) || is_type_raw_union(ft) || is_type_typeid(ft))) {
- is_constant = false;
- }
- }
- Type *nested_ft = bt;
- for (i32 index : sel.index) {
- Type *bt = base_type(nested_ft);
- switch (bt->kind) {
- case Type_Struct:
- if (bt->Struct.is_raw_union) {
- for (Entity *re : bt->Struct.fields) {
- string_map_set(&fields_visited_through_raw_union, re->token.string, sel.entity->token.string);
- }
- }
- nested_ft = bt->Struct.fields[index]->type;
- break;
- case Type_Array:
- nested_ft = bt->Array.elem;
- break;
- case Type_BitField:
- nested_ft = bt->BitField.fields[index]->type;
- break;
- default:
- GB_PANIC("invalid type %s", type_to_string(nested_ft));
- break;
- }
- }
- field = sel.entity;
- }
- Operand o = {};
- check_expr_or_type(c, &o, fv->value, field->type);
- if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type) || is_type_typeid(field->type)) {
- is_constant = false;
- }
- if (is_constant) {
- is_constant = check_is_operand_compound_lit_constant(c, &o);
- }
- u8 prev_bit_field_bit_size = c->bit_field_bit_size;
- if (field->kind == Entity_Variable && field->Variable.bit_field_bit_size) {
- // HACK NOTE(bill): This is a bit of a hack, but it will work fine for this use case
- c->bit_field_bit_size = field->Variable.bit_field_bit_size;
- }
- check_assignment(c, &o, field->type, assignment_str);
- c->bit_field_bit_size = prev_bit_field_bit_size;
- }
- }
- gb_internal bool is_expr_inferred_fixed_array(Ast *type_expr) {
- type_expr = unparen_expr(type_expr);
- if (type_expr == nullptr) {
- return false;
- }
- // [?]Type
- if (type_expr->kind == Ast_ArrayType && type_expr->ArrayType.count != nullptr) {
- Ast *count = type_expr->ArrayType.count;
- if (count->kind == Ast_UnaryExpr &&
- count->UnaryExpr.op.kind == Token_Question) {
- return true;
- }
- }
- return false;
- }
- gb_internal bool check_for_dynamic_literals(CheckerContext *c, Ast *node, AstCompoundLit *cl) {
- if (cl->elems.count > 0 && (check_feature_flags(c, node) & OptInFeatureFlag_DynamicLiterals) == 0 && !build_context.dynamic_literals) {
- ERROR_BLOCK();
- error(node, "Compound literals of dynamic types are disabled by default");
- error_line("\tSuggestion: If you want to enable them for this specific file, add '#+feature dynamic-literals' at the top of the file\n");
- error_line("\tWarning: Please understand that dynamic literals will implicitly allocate using the current 'context.allocator' in that scope\n");
- if (build_context.ODIN_DEFAULT_TO_NIL_ALLOCATOR) {
- error_line("\tWarning: As '-default-to-panic-allocator' has been set, the dynamic compound literal may not be initialized as expected\n");
- } else if (build_context.ODIN_DEFAULT_TO_PANIC_ALLOCATOR) {
- error_line("\tWarning: As '-default-to-panic-allocator' has been set, the dynamic compound literal may not be initialized as expected\n");
- }
- return false;
- }
- return cl->elems.count > 0;
- }
- gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ExprKind kind = Expr_Expr;
- ast_node(cl, CompoundLit, node);
- Type *type = type_hint;
- if (type != nullptr && is_type_untyped(type)) {
- type = nullptr;
- }
- bool is_to_be_determined_array_count = false;
- bool is_constant = true;
- bool is_soa = false;
- Ast *type_expr = cl->type;
- bool used_type_hint_expr = false;
- if (type_expr == nullptr && c->type_hint_expr != nullptr) {
- if (is_expr_inferred_fixed_array(c->type_hint_expr)) {
- type_expr = clone_ast(c->type_hint_expr);
- used_type_hint_expr = true;
- }
- }
- if (type_expr != nullptr) {
- type = nullptr;
- // [?]Type
- if (type_expr->kind == Ast_ArrayType) {
- Ast *count = type_expr->ArrayType.count;
- if (count != nullptr) {
- if (count->kind == Ast_UnaryExpr &&
- count->UnaryExpr.op.kind == Token_Question) {
- type = alloc_type_array(check_type(c, type_expr->ArrayType.elem), -1);
- is_to_be_determined_array_count = true;
- }
- } else {
- type = alloc_type_slice(check_type(c, type_expr->ArrayType.elem));
- }
- if (cl->elems.count > 0) {
- if (type_expr->ArrayType.tag != nullptr) {
- Ast *tag = type_expr->ArrayType.tag;
- GB_ASSERT(tag->kind == Ast_BasicDirective);
- String name = tag->BasicDirective.name.string;
- if (name == "soa") {
- is_soa = true;
- if (count == nullptr) {
- error(node, "#soa slices are not supported for compound literals");
- return kind;
- } else if (count->kind == Ast_UnaryExpr &&
- count->UnaryExpr.op.kind == Token_Question) {
- error(node, "#soa fixed length arrays must specify their length and cannot use ?");
- }
- }
- }
- }
- } else if (type_expr->kind == Ast_DynamicArrayType && type_expr->DynamicArrayType.tag != nullptr) {
- if (cl->elems.count > 0) {
- Ast *tag = type_expr->DynamicArrayType.tag;
- GB_ASSERT(tag->kind == Ast_BasicDirective);
- String name = tag->BasicDirective.name.string;
- if (name == "soa") {
- is_soa = true;
- error(node, "#soa dynamic arrays are not supported for compound literals");
- return kind;
- }
- }
- }
- if (type == nullptr) {
- type = check_type(c, type_expr);
- }
- }
- if (type == nullptr) {
- error(node, "Missing type in compound literal");
- return kind;
- }
- Type *t = base_type(type);
- if (is_type_polymorphic(t)) {
- gbString str = type_to_string(type);
- error(node, "Cannot use a polymorphic type for a compound literal, got '%s'", str);
- o->expr = node;
- o->type = type;
- gb_string_free(str);
- return kind;
- }
- switch (t->kind) {
- case Type_Struct:
- if (cl->elems.count == 0) {
- break; // NOTE(bill): No need to init
- }
- if (t->Struct.soa_kind == StructSoa_None) {
- if (t->Struct.is_raw_union) {
- if (cl->elems.count > 0) {
- // NOTE: unions cannot be constant
- is_constant = false;
- if (cl->elems[0]->kind != Ast_FieldValue) {
- gbString type_str = type_to_string(type);
- error(node, "%s ('struct #raw_union') compound literals are only allowed to contain 'field = value' elements", type_str);
- gb_string_free(type_str);
- } else {
- if (cl->elems.count != 1) {
- gbString type_str = type_to_string(type);
- error(node, "%s ('struct #raw_union') compound literals are only allowed to contain up to 1 'field = value' element, got %td", type_str, cl->elems.count);
- gb_string_free(type_str);
- } else {
- check_compound_literal_field_values(c, cl->elems, o, type, is_constant);
- }
- }
- }
- break;
- }
- wait_signal_until_available(&t->Struct.fields_wait_signal);
- isize field_count = t->Struct.fields.count;
- isize min_field_count = t->Struct.fields.count;
- for (isize i = min_field_count-1; i >= 0; i--) {
- Entity *e = t->Struct.fields[i];
- GB_ASSERT(e->kind == Entity_Variable);
- if (e->Variable.param_value.kind != ParameterValue_Invalid) {
- min_field_count--;
- } else {
- break;
- }
- }
- if (cl->elems[0]->kind == Ast_FieldValue) {
- check_compound_literal_field_values(c, cl->elems, o, type, is_constant);
- } else {
- bool seen_field_value = false;
- for_array(index, cl->elems) {
- Entity *field = nullptr;
- Ast *elem = cl->elems[index];
- if (elem->kind == Ast_FieldValue) {
- seen_field_value = true;
- error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
- continue;
- } else if (seen_field_value) {
- error(elem, "Value elements cannot be used after a 'field = value'");
- continue;
- }
- if (index >= field_count) {
- error(elem, "Too many values in structure literal, expected %td, got %td", field_count, cl->elems.count);
- break;
- }
- if (field == nullptr) {
- field = t->Struct.fields[index];
- }
- Operand o = {};
- check_expr_or_type(c, &o, elem, field->type);
- if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type) || is_type_typeid(field->type)) {
- is_constant = false;
- }
- if (is_constant) {
- is_constant = check_is_operand_compound_lit_constant(c, &o);
- }
- check_assignment(c, &o, field->type, str_lit("structure literal"));
- }
- if (cl->elems.count < field_count) {
- if (min_field_count < field_count) {
- if (cl->elems.count < min_field_count) {
- error(cl->close, "Too few values in structure literal, expected at least %td, got %td", min_field_count, cl->elems.count);
- }
- } else {
- error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count);
- }
- }
- }
- break;
- } else if (t->Struct.soa_kind != StructSoa_Fixed) {
- error(node, "#soa slices and dynamic arrays are not supported for compound literals");
- break;
- }
- /*fallthrough*/
- case Type_Slice:
- case Type_Array:
- case Type_DynamicArray:
- case Type_SimdVector:
- case Type_Matrix:
- {
- Type *elem_type = nullptr;
- String context_name = {};
- i64 max_type_count = -1;
- if (t->kind == Type_Struct) {
- GB_ASSERT(t->Struct.soa_kind == StructSoa_Fixed);
- elem_type = t->Struct.soa_elem;
- context_name = str_lit("#soa array literal");
- if (!is_to_be_determined_array_count) {
- max_type_count = t->Struct.soa_count;
- }
- } else if (t->kind == Type_Slice) {
- elem_type = t->Slice.elem;
- context_name = str_lit("slice literal");
- } else if (t->kind == Type_Array) {
- elem_type = t->Array.elem;
- context_name = str_lit("array literal");
- if (!is_to_be_determined_array_count) {
- max_type_count = t->Array.count;
- }
- } else if (t->kind == Type_Array) {
- elem_type = t->Array.elem;
- context_name = str_lit("array literal");
- if (!is_to_be_determined_array_count) {
- max_type_count = t->Array.count;
- }
- } else if (t->kind == Type_DynamicArray) {
- elem_type = t->DynamicArray.elem;
- context_name = str_lit("dynamic array literal");
- is_constant = false;
- } else if (t->kind == Type_SimdVector) {
- elem_type = t->SimdVector.elem;
- context_name = str_lit("simd vector literal");
- max_type_count = t->SimdVector.count;
- } else if (t->kind == Type_Matrix) {
- elem_type = t->Matrix.elem;
- context_name = str_lit("matrix literal");
- max_type_count = t->Matrix.row_count*t->Matrix.column_count;
- } else {
- GB_PANIC("unreachable");
- }
- i64 max = 0;
- Type *bet = base_type(elem_type);
- if (!elem_type_can_be_constant(bet)) {
- is_constant = false;
- }
- if (bet == t_invalid) {
- break;
- }
- if (cl->elems.count > 0 && cl->elems[0]->kind == Ast_FieldValue) {
- RangeCache rc = range_cache_make(heap_allocator());
- defer (range_cache_destroy(&rc));
- for (Ast *elem : cl->elems) {
- if (elem->kind != Ast_FieldValue) {
- error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
- continue;
- }
- ast_node(fv, FieldValue, elem);
- if (is_ast_range(fv->field)) {
- Token op = fv->field->BinaryExpr.op;
- Operand x = {};
- Operand y = {};
- bool ok = check_range(c, fv->field, false, &x, &y, nullptr);
- if (!ok) {
- continue;
- }
- if (x.mode != Addressing_Constant || !is_type_integer(core_type(x.type))) {
- error(x.expr, "Expected a constant integer as an array field");
- continue;
- }
- if (y.mode != Addressing_Constant || !is_type_integer(core_type(y.type))) {
- error(y.expr, "Expected a constant integer as an array field");
- continue;
- }
- i64 lo = exact_value_to_i64(x.value);
- i64 hi = exact_value_to_i64(y.value);
- i64 max_index = hi;
- if (op.kind == Token_RangeHalf) { // ..< (exclusive)
- hi -= 1;
- } else { // .. (inclusive)
- max_index += 1;
- }
- bool new_range = range_cache_add_range(&rc, lo, hi);
- if (!new_range) {
- error(elem, "Overlapping field range index %lld %.*s %lld for %.*s", lo, LIT(op.string), hi, LIT(context_name));
- continue;
- }
- if (max_type_count >= 0 && (lo < 0 || lo >= max_type_count)) {
- error(elem, "Index %lld is out of bounds (0..<%lld) for %.*s", lo, max_type_count, LIT(context_name));
- continue;
- }
- if (max_type_count >= 0 && (hi < 0 || hi >= max_type_count)) {
- error(elem, "Index %lld is out of bounds (0..<%lld) for %.*s", hi, max_type_count, LIT(context_name));
- continue;
- }
- if (max < hi) {
- max = max_index;
- }
- Operand operand = {};
- check_expr_with_type_hint(c, &operand, fv->value, elem_type);
- check_assignment(c, &operand, elem_type, context_name);
- is_constant = is_constant && operand.mode == Addressing_Constant;
- } else {
- Operand op_index = {};
- check_expr(c, &op_index, fv->field);
- if (op_index.mode != Addressing_Constant || !is_type_integer(core_type(op_index.type))) {
- error(elem, "Expected a constant integer as an array field");
- continue;
- }
- // add_type_and_value(c, op_index.expr, op_index.mode, op_index.type, op_index.value);
- i64 index = exact_value_to_i64(op_index.value);
- if (max_type_count >= 0 && (index < 0 || index >= max_type_count)) {
- error(elem, "Index %lld is out of bounds (0..<%lld) for %.*s", index, max_type_count, LIT(context_name));
- continue;
- }
- bool new_index = range_cache_add_index(&rc, index);
- if (!new_index) {
- error(elem, "Duplicate field index %lld for %.*s", index, LIT(context_name));
- continue;
- }
- if (max < index+1) {
- max = index+1;
- }
- Operand operand = {};
- check_expr_with_type_hint(c, &operand, fv->value, elem_type);
- check_assignment(c, &operand, elem_type, context_name);
- is_constant = is_constant && operand.mode == Addressing_Constant;
- }
- }
- cl->max_count = max;
- } else {
- isize index = 0;
- for (; index < cl->elems.count; index++) {
- Ast *e = cl->elems[index];
- if (e == nullptr) {
- error(node, "Invalid literal element");
- continue;
- }
- if (e->kind == Ast_FieldValue) {
- error(e, "Mixture of 'field = value' and value elements in a literal is not allowed");
- continue;
- }
- if (0 <= max_type_count && max_type_count <= index) {
- error(e, "Index %lld is out of bounds (>= %lld) for %.*s", index, max_type_count, LIT(context_name));
- }
- Operand operand = {};
- check_expr_with_type_hint(c, &operand, e, elem_type);
- check_assignment(c, &operand, elem_type, context_name);
- is_constant = is_constant && operand.mode == Addressing_Constant;
- }
- if (max < index) {
- max = index;
- }
- }
- if (t->kind == Type_Array) {
- if (is_to_be_determined_array_count) {
- t->Array.count = max;
- } else if (cl->elems.count > 0 && cl->elems[0]->kind != Ast_FieldValue) {
- if (0 < max && max < t->Array.count) {
- error(node, "Expected %lld values for this array literal, got %lld", cast(long long)t->Array.count, cast(long long)max);
- }
- }
- } else if (t->kind == Type_Struct) {
- GB_ASSERT(t->Struct.soa_kind == StructSoa_Fixed);
- if (is_to_be_determined_array_count) {
- t->Struct.soa_count = cast(i32)max;
- } else if (cl->elems.count > 0 && cl->elems[0]->kind != Ast_FieldValue) {
- if (0 < max && max < t->Struct.soa_count) {
- error(node, "Expected %lld values for this #soa array literal, got %lld", cast(long long)t->Struct.soa_count, cast(long long)max);
- }
- }
- }
- if (t->kind == Type_SimdVector) {
- if (!is_constant) {
- // error(node, "Expected all constant elements for a simd vector");
- }
- }
- if (t->kind == Type_DynamicArray) {
- if (check_for_dynamic_literals(c, node, cl)) {
- add_package_dependency(c, "runtime", "__dynamic_array_reserve");
- add_package_dependency(c, "runtime", "__dynamic_array_append");
- }
- }
- if (t->kind == Type_Matrix) {
- if (cl->elems.count > 0 && cl->elems[0]->kind != Ast_FieldValue) {
- if (0 < max && max < max_type_count) {
- error(node, "Expected %lld values for this matrix literal, got %lld", cast(long long)max_type_count, cast(long long)max);
- }
- }
- }
- break;
- }
- case Type_EnumeratedArray:
- {
- Type *elem_type = t->EnumeratedArray.elem;
- Type *index_type = t->EnumeratedArray.index;
- String context_name = str_lit("enumerated array literal");
- i64 max_type_count = t->EnumeratedArray.count;
- gbString index_type_str = type_to_string(index_type);
- defer (gb_string_free(index_type_str));
- i64 total_lo = exact_value_to_i64(*t->EnumeratedArray.min_value);
- i64 total_hi = exact_value_to_i64(*t->EnumeratedArray.max_value);
- String total_lo_string = {};
- String total_hi_string = {};
- GB_ASSERT(is_type_enum(index_type));
- {
- Type *bt = base_type(index_type);
- GB_ASSERT(bt->kind == Type_Enum);
- for (Entity *f : bt->Enum.fields) {
- if (f->kind != Entity_Constant) {
- continue;
- }
- if (total_lo_string.len == 0 && compare_exact_values(Token_CmpEq, f->Constant.value, *t->EnumeratedArray.min_value)) {
- total_lo_string = f->token.string;
- }
- if (total_hi_string.len == 0 && compare_exact_values(Token_CmpEq, f->Constant.value, *t->EnumeratedArray.max_value)) {
- total_hi_string = f->token.string;
- }
- if (total_lo_string.len != 0 && total_hi_string.len != 0) {
- break;
- }
- }
- }
- i64 max = 0;
- Type *bet = base_type(elem_type);
- if (!elem_type_can_be_constant(bet)) {
- is_constant = false;
- }
- if (bet == t_invalid) {
- break;
- }
- bool is_partial = cl->tag && (cl->tag->BasicDirective.name.string == "partial");
- SeenMap seen = {}; // NOTE(bill): Multimap, Key: ExactValue
- defer (map_destroy(&seen));
- if (cl->elems.count > 0 && cl->elems[0]->kind == Ast_FieldValue) {
- RangeCache rc = range_cache_make(heap_allocator());
- defer (range_cache_destroy(&rc));
- for (Ast *elem : cl->elems) {
- if (elem->kind != Ast_FieldValue) {
- error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
- continue;
- }
- ast_node(fv, FieldValue, elem);
- if (is_ast_range(fv->field)) {
- Token op = fv->field->BinaryExpr.op;
- Operand x = {};
- Operand y = {};
- bool ok = check_range(c, fv->field, false, &x, &y, nullptr, index_type);
- if (!ok) {
- continue;
- }
- if (x.mode != Addressing_Constant || !are_types_identical(x.type, index_type)) {
- error(x.expr, "Expected a constant enum of type '%s' as an array field", index_type_str);
- continue;
- }
- if (y.mode != Addressing_Constant || !are_types_identical(x.type, index_type)) {
- error(y.expr, "Expected a constant enum of type '%s' as an array field", index_type_str);
- continue;
- }
- i64 lo = exact_value_to_i64(x.value);
- i64 hi = exact_value_to_i64(y.value);
- i64 max_index = hi;
- if (op.kind == Token_RangeHalf) {
- hi -= 1;
- }
- bool new_range = range_cache_add_range(&rc, lo, hi);
- if (!new_range) {
- gbString lo_str = expr_to_string(x.expr);
- gbString hi_str = expr_to_string(y.expr);
- error(elem, "Overlapping field range index %s %.*s %s for %.*s", lo_str, LIT(op.string), hi_str, LIT(context_name));
- gb_string_free(hi_str);
- gb_string_free(lo_str);
- continue;
- }
- // NOTE(bill): These are sanity checks for invalid enum values
- if (max_type_count >= 0 && (lo < total_lo || lo > total_hi)) {
- gbString lo_str = expr_to_string(x.expr);
- error(elem, "Index %s is out of bounds (%.*s .. %.*s) for %.*s", lo_str, LIT(total_lo_string), LIT(total_hi_string), LIT(context_name));
- gb_string_free(lo_str);
- continue;
- }
- if (max_type_count >= 0 && (hi < 0 || hi > total_hi)) {
- gbString hi_str = expr_to_string(y.expr);
- error(elem, "Index %s is out of bounds (%.*s .. %.*s) for %.*s", hi_str, LIT(total_lo_string), LIT(total_hi_string), LIT(context_name));
- gb_string_free(hi_str);
- continue;
- }
- if (max < hi) {
- max = max_index;
- }
- Operand operand = {};
- check_expr_with_type_hint(c, &operand, fv->value, elem_type);
- check_assignment(c, &operand, elem_type, context_name);
- is_constant = is_constant && operand.mode == Addressing_Constant;
- TokenKind upper_op = Token_LtEq;
- if (op.kind == Token_RangeHalf) {
- upper_op = Token_Lt;
- }
- add_to_seen_map(c, &seen, upper_op, x, x, y);
- } else {
- Operand op_index = {};
- check_expr_with_type_hint(c, &op_index, fv->field, index_type);
- if (op_index.mode != Addressing_Constant || !are_types_identical(op_index.type, index_type)) {
- error(op_index.expr, "Expected a constant enum of type '%s' as an array field", index_type_str);
- continue;
- }
- i64 index = exact_value_to_i64(op_index.value);
- if (max_type_count >= 0 && (index < total_lo || index > total_hi)) {
- gbString idx_str = expr_to_string(op_index.expr);
- error(elem, "Index %s is out of bounds (%.*s .. %.*s) for %.*s", idx_str, LIT(total_lo_string), LIT(total_hi_string), LIT(context_name));
- gb_string_free(idx_str);
- continue;
- }
- bool new_index = range_cache_add_index(&rc, index);
- if (!new_index) {
- gbString idx_str = expr_to_string(op_index.expr);
- error(elem, "Duplicate field index %s for %.*s", idx_str, LIT(context_name));
- gb_string_free(idx_str);
- continue;
- }
- if (max < index+1) {
- max = index+1;
- }
- Operand operand = {};
- check_expr_with_type_hint(c, &operand, fv->value, elem_type);
- check_assignment(c, &operand, elem_type, context_name);
- is_constant = is_constant && operand.mode == Addressing_Constant;
- add_to_seen_map(c, &seen, op_index);
- }
- }
- cl->max_count = max;
- } else {
- isize index = 0;
- for (; index < cl->elems.count; index++) {
- Ast *e = cl->elems[index];
- if (e == nullptr) {
- error(node, "Invalid literal element");
- continue;
- }
- if (e->kind == Ast_FieldValue) {
- error(e, "Mixture of 'field = value' and value elements in a literal is not allowed");
- continue;
- }
- if (0 <= max_type_count && max_type_count <= index) {
- error(e, "Index %lld is out of bounds (>= %lld) for %.*s", index, max_type_count, LIT(context_name));
- }
- Operand operand = {};
- check_expr_with_type_hint(c, &operand, e, elem_type);
- check_assignment(c, &operand, elem_type, context_name);
- is_constant = is_constant && operand.mode == Addressing_Constant;
- }
- if (max < index) {
- max = index;
- }
- }
- bool was_error = false;
- if (cl->elems.count > 0 && cl->elems[0]->kind != Ast_FieldValue) {
- if (0 < max && max < t->EnumeratedArray.count) {
- error(node, "Expected %lld values for this enumerated array literal, got %lld", cast(long long)t->EnumeratedArray.count, cast(long long)max);
- was_error = true;
- } else {
- error(node, "Enumerated array literals must only have 'field = value' elements, bare elements are not allowed");
- was_error = true;
- }
- }
- // NOTE(bill): Check for missing cases when `#partial literal` is not present
- if (cl->elems.count > 0 && !was_error && !is_partial) {
- TEMPORARY_ALLOCATOR_GUARD();
- Type *et = base_type(index_type);
- GB_ASSERT(et->kind == Type_Enum);
- auto fields = et->Enum.fields;
- auto unhandled = array_make<Entity *>(temporary_allocator(), 0, fields.count);
- for (Entity *f : fields) {
- if (f->kind != Entity_Constant) {
- continue;
- }
- ExactValue v = f->Constant.value;
- uintptr hash = hash_exact_value(v);
- auto found = map_get(&seen, hash);
- if (!found) {
- array_add(&unhandled, f);
- }
- }
- if (unhandled.count > 0) {
- ERROR_BLOCK();
- if (unhandled.count == 1) {
- error_no_newline(node, "Unhandled enumerated array case: %.*s", LIT(unhandled[0]->token.string));
- } else {
- error(node, "Unhandled enumerated array cases:");
- for_array(i, unhandled) {
- Entity *f = unhandled[i];
- error_line("\t%.*s\n", LIT(f->token.string));
- }
- }
- if (!build_context.terse_errors) {
- error_line("\n");
- error_line("\tSuggestion: Was '#partial %s{...}' wanted?\n", type_to_string(type));
- }
- }
- }
- break;
- }
- case Type_Basic: {
- if (!is_type_any(t)) {
- if (cl->elems.count != 0) {
- gbString s = type_to_string(t);
- error(node, "Illegal compound literal, %s cannot be used as a compound literal with fields", s);
- gb_string_free(s);
- is_constant = false;
- }
- break;
- }
- if (cl->elems.count == 0) {
- break; // NOTE(bill): No need to init
- }
- { // Checker values
- Type *field_types[2] = {t_rawptr, t_typeid};
- isize field_count = 2;
- if (cl->elems[0]->kind == Ast_FieldValue) {
- bool fields_visited[2] = {};
- for_array(i, cl->elems) {
- Ast *elem = cl->elems[i];
- if (elem->kind != Ast_FieldValue) {
- error(elem, "Mixture of 'field = value' and value elements in a 'any' literal is not allowed");
- continue;
- }
- ast_node(fv, FieldValue, elem);
- if (fv->field->kind != Ast_Ident) {
- gbString expr_str = expr_to_string(fv->field);
- error(elem, "Invalid field name '%s' in 'any' literal", expr_str);
- gb_string_free(expr_str);
- continue;
- }
- String name = fv->field->Ident.token.string;
- Selection sel = lookup_field(type, name, o->mode == Addressing_Type);
- if (sel.entity == nullptr) {
- error(elem, "Unknown field '%.*s' in 'any' literal", LIT(name));
- continue;
- }
- isize index = sel.index[0];
- if (fields_visited[index]) {
- error(elem, "Duplicate field '%.*s' in 'any' literal", LIT(name));
- continue;
- }
- fields_visited[index] = true;
- check_expr(c, o, fv->value);
- // NOTE(bill): 'any' literals can never be constant
- is_constant = false;
- check_assignment(c, o, field_types[index], str_lit("'any' literal"));
- }
- } else {
- for_array(index, cl->elems) {
- Ast *elem = cl->elems[index];
- if (elem->kind == Ast_FieldValue) {
- error(elem, "Mixture of 'field = value' and value elements in a 'any' literal is not allowed");
- continue;
- }
- check_expr(c, o, elem);
- if (index >= field_count) {
- error(o->expr, "Too many values in 'any' literal, expected %td", field_count);
- break;
- }
- // NOTE(bill): 'any' literals can never be constant
- is_constant = false;
- check_assignment(c, o, field_types[index], str_lit("'any' literal"));
- }
- if (cl->elems.count < field_count) {
- error(cl->close, "Too few values in 'any' literal, expected %td, got %td", field_count, cl->elems.count);
- }
- }
- }
- break;
- }
- case Type_Map: {
- if (cl->elems.count == 0) {
- break;
- }
- is_constant = false;
- { // Checker values
- bool key_is_typeid = is_type_typeid(t->Map.key);
- bool value_is_typeid = is_type_typeid(t->Map.value);
- for (Ast *elem : cl->elems) {
- if (elem->kind != Ast_FieldValue) {
- error(elem, "Only 'field = value' elements are allowed in a map literal");
- continue;
- }
- ast_node(fv, FieldValue, elem);
- if (key_is_typeid) {
- check_expr_or_type(c, o, fv->field, t->Map.key);
- } else {
- check_expr_with_type_hint(c, o, fv->field, t->Map.key);
- }
- check_assignment(c, o, t->Map.key, str_lit("map literal"));
- if (o->mode == Addressing_Invalid) {
- continue;
- }
- if (value_is_typeid) {
- check_expr_or_type(c, o, fv->value, t->Map.value);
- } else {
- check_expr_with_type_hint(c, o, fv->value, t->Map.value);
- }
- check_assignment(c, o, t->Map.value, str_lit("map literal"));
- }
- }
- if (check_for_dynamic_literals(c, node, cl)) {
- add_map_reserve_dependencies(c);
- add_map_set_dependencies(c);
- }
- break;
- }
- case Type_BitSet: {
- if (cl->elems.count == 0) {
- break; // NOTE(bill): No need to init
- }
- Type *et = base_type(t->BitSet.elem);
- isize field_count = 0;
- if (et != nullptr && et->kind == Type_Enum) {
- field_count = et->Enum.fields.count;
- }
- if (is_type_array(bit_set_to_int(t))) {
- is_constant = false;
- }
- for (Ast *elem : cl->elems) {
- if (elem->kind == Ast_FieldValue) {
- error(elem, "'field = value' in a bit_set literal is not allowed");
- is_constant = false;
- continue;
- }
- check_expr_with_type_hint(c, o, elem, et);
- if (is_constant) {
- is_constant = o->mode == Addressing_Constant;
- }
- if (elem->kind == Ast_BinaryExpr) {
- switch (elem->BinaryExpr.op.kind) {
- case Token_Or:
- {
- gbString x = expr_to_string(elem->BinaryExpr.left);
- gbString y = expr_to_string(elem->BinaryExpr.right);
- gbString e = expr_to_string(elem);
- error(elem, "Was the following intended? '%s, %s'; if not, surround the expression with parentheses '(%s)'", x, y, e);
- gb_string_free(e);
- gb_string_free(y);
- gb_string_free(x);
- }
- break;
- }
- }
- check_assignment(c, o, t->BitSet.elem, str_lit("bit_set literal"));
- if (o->mode == Addressing_Constant) {
- i64 lower = t->BitSet.lower;
- i64 upper = t->BitSet.upper;
- i64 v = exact_value_to_i64(o->value);
- if (lower <= v && v <= upper) {
- // okay
- } else {
- gbString s = expr_to_string(o->expr);
- error(elem, "Bit field value out of bounds, %s (%lld) not in the range %lld .. %lld", s, v, lower, upper);
- gb_string_free(s);
- continue;
- }
- }
- }
- break;
- }
- case Type_BitField: {
- if (cl->elems.count == 0) {
- break; // NOTE(bill): No need to init
- }
- is_constant = false;
- if (cl->elems[0]->kind != Ast_FieldValue) {
- gbString type_str = type_to_string(type);
- error(node, "%s ('bit_field') compound literals are only allowed to contain 'field = value' elements", type_str);
- gb_string_free(type_str);
- } else {
- check_compound_literal_field_values(c, cl->elems, o, type, is_constant);
- }
- break;
- }
- default: {
- if (cl->elems.count == 0) {
- break; // NOTE(bill): No need to init
- }
- gbString str = type_to_string(type);
- error(node, "Invalid compound literal type '%s'", str);
- gb_string_free(str);
- return kind;
- }
- }
- if (is_constant) {
- o->mode = Addressing_Constant;
- if (is_type_bit_set(type)) {
- // NOTE(bill): Encode as an integer
- Type *bt = base_type(type);
- BigInt bits = {};
- BigInt one = {};
- big_int_from_u64(&one, 1);
- for (Ast *e : cl->elems) {
- GB_ASSERT(e->kind != Ast_FieldValue);
- TypeAndValue tav = e->tav;
- if (tav.mode != Addressing_Constant) {
- continue;
- }
- if (tav.value.kind != ExactValue_Integer) {
- continue;
- }
- i64 v = big_int_to_i64(&tav.value.value_integer);
- i64 lower = bt->BitSet.lower;
- u64 index = cast(u64)(v-lower);
- BigInt bit = {};
- big_int_from_u64(&bit, index);
- big_int_shl(&bit, &one, &bit);
- big_int_or(&bits, &bits, &bit);
- }
- o->value.kind = ExactValue_Integer;
- o->value.value_integer = bits;
- } else if (is_type_constant_type(type) && cl->elems.count == 0) {
- ExactValue value = exact_value_compound(node);
- Type *bt = core_type(type);
- if (bt->kind == Type_Basic) {
- if (bt->Basic.flags & BasicFlag_Boolean) {
- value = exact_value_bool(false);
- } else if (bt->Basic.flags & BasicFlag_Integer) {
- value = exact_value_i64(0);
- } else if (bt->Basic.flags & BasicFlag_Unsigned) {
- value = exact_value_i64(0);
- } else if (bt->Basic.flags & BasicFlag_Float) {
- value = exact_value_float(0);
- } else if (bt->Basic.flags & BasicFlag_Complex) {
- value = exact_value_complex(0, 0);
- } else if (bt->Basic.flags & BasicFlag_Quaternion) {
- value = exact_value_quaternion(0, 0, 0, 0);
- } else if (bt->Basic.flags & BasicFlag_Pointer) {
- value = exact_value_pointer(0);
- } else if (bt->Basic.flags & BasicFlag_String) {
- String empty_string = {};
- value = exact_value_string(empty_string);
- } else if (bt->Basic.flags & BasicFlag_Rune) {
- value = exact_value_i64(0);
- }
- }
- o->value = value;
- } else {
- o->value = exact_value_compound(node);
- }
- } else {
- o->mode = Addressing_Value;
- }
- o->type = type;
- return kind;
- }
- gb_internal ExprKind check_type_assertion(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ExprKind kind = Expr_Expr;
- ast_node(ta, TypeAssertion, node);
- check_expr(c, o, ta->expr);
- node->viral_state_flags |= ta->expr->viral_state_flags;
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- if (o->mode == Addressing_Constant) {
- gbString expr_str = expr_to_string(o->expr);
- error(o->expr, "A type assertion cannot be applied to a constant expression: '%s'", expr_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- if (is_type_untyped(o->type)) {
- gbString expr_str = expr_to_string(o->expr);
- error(o->expr, "A type assertion cannot be applied to an untyped expression: '%s'", expr_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- Type *src = type_deref(o->type);
- Type *bsrc = base_type(src);
- if (ta->type != nullptr && ta->type->kind == Ast_UnaryExpr && ta->type->UnaryExpr.op.kind == Token_Question) {
- if (!is_type_union(src)) {
- gbString str = type_to_string(o->type);
- error(o->expr, "Type assertions with .? can only operate on unions, got %s", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- if (bsrc->Union.variants.count != 1 && type_hint != nullptr) {
- bool allowed = false;
- for (Type *vt : bsrc->Union.variants) {
- if (are_types_identical(vt, type_hint)) {
- allowed = true;
- add_type_info_type(c, vt);
- break;
- }
- }
- if (allowed) {
- add_type_info_type(c, o->type);
- o->type = type_hint;
- o->mode = Addressing_OptionalOk;
- goto end;
- }
- }
- if (bsrc->Union.variants.count != 1) {
- error(o->expr, "Type assertions with .? can only operate on unions with 1 variant, got %lld", cast(long long)bsrc->Union.variants.count);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- add_type_info_type(c, o->type);
- add_type_info_type(c, bsrc->Union.variants[0]);
- o->type = bsrc->Union.variants[0];
- o->mode = Addressing_OptionalOk;
- } else {
- Type *t = check_type(c, ta->type);
- Type *dst = t;
- if (is_type_union(src)) {
- bool ok = false;
- for (Type *vt : bsrc->Union.variants) {
- if (are_types_identical(vt, dst)) {
- ok = true;
- break;
- }
- }
- if (!ok) {
- gbString expr_str = expr_to_string(o->expr);
- gbString dst_type_str = type_to_string(t);
- defer (gb_string_free(expr_str));
- defer (gb_string_free(dst_type_str));
- if (bsrc->Union.variants.count == 0) {
- error(o->expr, "Cannot type assert '%s' to '%s' as this is an empty union", expr_str, dst_type_str);
- } else {
- error(o->expr, "Cannot type assert '%s' to '%s' as it is not a variant of that union", expr_str, dst_type_str);
- }
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- add_type_info_type(c, o->type);
- add_type_info_type(c, t);
- o->type = t;
- o->mode = Addressing_OptionalOk;
- } else if (is_type_any(src)) {
- o->type = t;
- o->mode = Addressing_OptionalOk;
- add_type_info_type(c, o->type);
- add_type_info_type(c, t);
- } else {
- gbString str = type_to_string(o->type);
- error(o->expr, "Type assertions can only operate on unions and 'any', got %s", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- }
- end:;
- if ((c->state_flags & StateFlag_no_type_assert) == 0) {
- add_package_dependency(c, "runtime", "type_assertion_check");
- add_package_dependency(c, "runtime", "type_assertion_check2");
- }
- return kind;
- }
- gb_internal ExprKind check_selector_call_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ast_node(se, SelectorCallExpr, node);
- // IMPORTANT NOTE(bill, 2020-05-22): This is a complete hack to get a shorthand which is extremely useful for vtables
- // COM APIs is a great example of where this kind of thing is extremely useful
- // General idea:
- //
- // x->y(123) == x.y(x, 123)
- //
- // How this has been implemented at the moment is quite hacky but it's done so to reduce need for huge backend changes
- // Just regenerating a new AST aids things
- //
- // TODO(bill): Is this a good hack or not?
- //
- // NOTE(bill, 2020-05-22): I'm going to regret this decision, ain't I?
- if (se->modified_call) {
- // Prevent double evaluation
- o->expr = node;
- o->type = node->tav.type;
- o->value = node->tav.value;
- o->mode = node->tav.mode;
- return Expr_Expr;
- }
- bool allow_arrow_right_selector_expr;
- allow_arrow_right_selector_expr = c->allow_arrow_right_selector_expr;
- c->allow_arrow_right_selector_expr = true;
- Operand x = {};
- ExprKind kind = check_expr_base(c, &x, se->expr, nullptr);
- c->allow_arrow_right_selector_expr = allow_arrow_right_selector_expr;
- if (x.mode == Addressing_Invalid || (x.type == t_invalid && x.mode != Addressing_ProcGroup)) {
- o->mode = Addressing_Invalid;
- o->type = t_invalid;
- o->expr = node;
- return kind;
- }
- if (!is_type_proc(x.type) && x.mode != Addressing_ProcGroup) {
- gbString type_str = type_to_string(x.type);
- error(se->call, "Selector call expressions expect a procedure type for the call, got '%s'", type_str);
- gb_string_free(type_str);
- o->mode = Addressing_Invalid;
- o->type = t_invalid;
- o->expr = node;
- return Expr_Stmt;
- }
- ast_node(ce, CallExpr, se->call);
- GB_ASSERT(x.expr->kind == Ast_SelectorExpr);
- Ast *first_arg = x.expr->SelectorExpr.expr;
- GB_ASSERT(first_arg != nullptr);
- Entity *e = entity_of_node(se->expr);
- if (!(e != nullptr && (e->kind == Entity_Procedure || e->kind == Entity_ProcGroup))) {
- first_arg->state_flags |= StateFlag_SelectorCallExpr;
- }
- if (e->kind != Entity_ProcGroup) {
- Type *pt = base_type(x.type);
- GB_ASSERT_MSG(pt->kind == Type_Proc, "%.*s %.*s %s", LIT(e->token.string), LIT(entity_strings[e->kind]), type_to_string(x.type));
- Type *first_type = nullptr;
- String first_arg_name = {};
- if (pt->Proc.param_count > 0) {
- Entity *f = pt->Proc.params->Tuple.variables[0];
- first_type = f->type;
- first_arg_name = f->token.string;
- }
- if (first_arg_name.len == 0) {
- first_arg_name = str_lit("_");
- }
- if (first_type == nullptr) {
- error(se->call, "Selector call expressions expect a procedure type for the call with at least 1 parameter");
- o->mode = Addressing_Invalid;
- o->type = t_invalid;
- o->expr = node;
- return Expr_Stmt;
- }
- Operand y = {};
- y.mode = first_arg->tav.mode;
- y.type = first_arg->tav.type;
- y.value = first_arg->tav.value;
- if (check_is_assignable_to(c, &y, first_type)) {
- // Do nothing, it's valid
- } else {
- Operand z = y;
- z.type = type_deref(y.type);
- if (check_is_assignable_to(c, &z, first_type)) {
- // NOTE(bill): AST GENERATION HACK!
- Token op = {Token_Pointer};
- first_arg = ast_deref_expr(first_arg->file(), first_arg, op);
- } else if (y.mode == Addressing_Variable) {
- Operand w = y;
- w.type = alloc_type_pointer(y.type);
- if (check_is_assignable_to(c, &w, first_type)) {
- // NOTE(bill): AST GENERATION HACK!
- Token op = {Token_And};
- first_arg = ast_unary_expr(first_arg->file(), op, first_arg);
- }
- }
- }
- if (ce->args.count > 0) {
- bool fail = false;
- bool first_is_field_value = (ce->args[0]->kind == Ast_FieldValue);
- for (Ast *arg : ce->args) {
- bool mix = false;
- if (first_is_field_value) {
- mix = arg->kind != Ast_FieldValue;
- } else {
- mix = arg->kind == Ast_FieldValue;
- }
- if (mix) {
- fail = true;
- break;
- }
- }
- if (!fail && first_is_field_value) {
- Token op = {Token_Eq};
- AstFile *f = first_arg->file();
- first_arg = ast_field_value(f, ast_ident(f, make_token_ident(first_arg_name)), first_arg, op);
- }
- }
- }
- auto modified_args = slice_make<Ast *>(heap_allocator(), ce->args.count+1);
- modified_args[0] = first_arg;
- slice_copy(&modified_args, ce->args, 1);
- ce->args = modified_args;
- se->modified_call = true;
- allow_arrow_right_selector_expr = c->allow_arrow_right_selector_expr;
- c->allow_arrow_right_selector_expr = true;
- check_expr_base(c, o, se->call, type_hint);
- c->allow_arrow_right_selector_expr = allow_arrow_right_selector_expr;
- o->expr = node;
- return Expr_Expr;
- }
- gb_internal ExprKind check_index_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ExprKind kind = Expr_Expr;
- ast_node(ie, IndexExpr, node);
- check_expr(c, o, ie->expr);
- node->viral_state_flags |= ie->expr->viral_state_flags;
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- Type *t = base_type(type_deref(o->type));
- bool is_ptr = is_type_pointer(o->type);
- bool is_const = o->mode == Addressing_Constant;
- if (is_type_map(t)) {
- Operand key = {};
- if (is_type_typeid(t->Map.key)) {
- check_expr_or_type(c, &key, ie->index, t->Map.key);
- } else {
- check_expr_with_type_hint(c, &key, ie->index, t->Map.key);
- }
- check_assignment(c, &key, t->Map.key, str_lit("map index"));
- if (key.mode == Addressing_Invalid) {
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- o->mode = Addressing_MapIndex;
- o->type = t->Map.value;
- o->expr = node;
- add_map_get_dependencies(c);
- add_map_set_dependencies(c);
- return Expr_Expr;
- }
- i64 max_count = -1;
- bool valid = check_set_index_data(o, t, is_ptr, &max_count, o->type);
- if (is_const) {
- if (is_type_array(t)) {
- // Okay
- } else if (is_type_slice(t)) {
- // Okay
- } else if (is_type_enumerated_array(t)) {
- // Okay
- } else if (is_type_string(t)) {
- // Okay
- } else if (is_type_matrix(t)) {
- // Okay
- } else {
- valid = false;
- }
- }
- if (!valid) {
- gbString str = expr_to_string(o->expr);
- gbString type_str = type_to_string(o->type);
- defer (gb_string_free(str));
- defer (gb_string_free(type_str));
- if (is_const) {
- error(o->expr, "Cannot index constant '%s' of type '%s'", str, type_str);
- } else {
- error(o->expr, "Cannot index '%s' of type '%s'", str, type_str);
- }
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- if (ie->index == nullptr) {
- gbString str = expr_to_string(o->expr);
- error(o->expr, "Missing index for '%s'", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- Type *index_type_hint = nullptr;
- if (is_type_enumerated_array(t)) {
- Type *bt = base_type(t);
- GB_ASSERT(bt->kind == Type_EnumeratedArray);
- index_type_hint = bt->EnumeratedArray.index;
- }
- i64 index = 0;
- bool ok = check_index_value(c, t, false, ie->index, max_count, &index, index_type_hint);
- if (is_const) {
- if (index < 0) {
- ERROR_BLOCK();
- gbString str = expr_to_string(o->expr);
- error(o->expr, "Cannot index a constant '%s'", str);
- if (!build_context.terse_errors) {
- error_line("\tSuggestion: store the constant into a variable in order to index it with a variable index\n");
- }
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- } else if (ok && !is_type_matrix(t)) {
- TypeAndValue tav = type_and_value_of_expr(ie->expr);
- ExactValue value = tav.value;
- o->mode = Addressing_Constant;
- bool success = false;
- bool finish = false;
- o->value = get_constant_field_single(c, value, cast(i32)index, &success, &finish);
- if (!success) {
- ERROR_BLOCK();
- gbString str = expr_to_string(o->expr);
- error(o->expr, "Cannot index a constant '%s' with index %lld", str, cast(long long)index);
- if (!build_context.terse_errors) {
- error_line("\tSuggestion: store the constant into a variable in order to index it with a variable index\n");
- }
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- }
- }
- if (type_hint != nullptr && is_type_matrix(t)) {
- // TODO(bill): allow matrix columns to be assignable to other types which are the same internally
- // if a type hint exists
- }
- return kind;
- }
- gb_internal ExprKind check_slice_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ExprKind kind = Expr_Stmt;
- ast_node(se, SliceExpr, node);
- check_expr(c, o, se->expr);
- node->viral_state_flags |= se->expr->viral_state_flags;
- if (o->mode == Addressing_Invalid) {
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- bool valid = false;
- i64 max_count = -1;
- Type *t = base_type(type_deref(o->type));
- switch (t->kind) {
- case Type_Basic:
- if (t->Basic.kind == Basic_string || t->Basic.kind == Basic_UntypedString) {
- valid = true;
- if (o->mode == Addressing_Constant) {
- max_count = o->value.value_string.len;
- }
- o->type = type_deref(o->type);
- }
- break;
- case Type_Array:
- valid = true;
- max_count = t->Array.count;
- if (o->mode != Addressing_Variable && !is_type_pointer(o->type)) {
- gbString str = expr_to_string(node);
- error(node, "Cannot slice array '%s', value is not addressable", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- o->type = alloc_type_slice(t->Array.elem);
- break;
- case Type_MultiPointer:
- valid = true;
- o->type = type_deref(o->type);
- break;
- case Type_Slice:
- valid = true;
- o->type = type_deref(o->type);
- break;
- case Type_DynamicArray:
- valid = true;
- o->type = alloc_type_slice(t->DynamicArray.elem);
- break;
- case Type_Struct:
- if (is_type_soa_struct(t)) {
- valid = true;
- if (t->Struct.soa_kind == StructSoa_Fixed) {
- max_count = t->Struct.soa_count;
- if (o->mode != Addressing_Variable && !is_type_pointer(o->type)) {
- gbString str = expr_to_string(node);
- error(node, "Cannot slice #soa array '%s', value is not addressable", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- }
- o->type = make_soa_struct_slice(c, nullptr, nullptr, t->Struct.soa_elem);
- }
- break;
- case Type_EnumeratedArray:
- {
- gbString str = expr_to_string(o->expr);
- gbString type_str = type_to_string(o->type);
- error(o->expr, "Cannot slice '%s' of type '%s', as enumerated arrays cannot be sliced", str, type_str);
- gb_string_free(type_str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- break;
- }
- if (!valid) {
- gbString str = expr_to_string(o->expr);
- gbString type_str = type_to_string(o->type);
- error(o->expr, "Cannot slice '%s' of type '%s'", str, type_str);
- gb_string_free(type_str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- if (se->low == nullptr && se->high != nullptr) {
- // It is okay to continue as it will assume the 1st index is zero
- }
- i64 indices[2] = {};
- Ast *nodes[2] = {se->low, se->high};
- for (isize i = 0; i < gb_count_of(nodes); i++) {
- i64 index = max_count;
- if (nodes[i] != nullptr) {
- i64 capacity = -1;
- if (max_count >= 0) {
- capacity = max_count;
- }
- i64 j = 0;
- if (check_index_value(c, t, true, nodes[i], capacity, &j)) {
- index = j;
- }
- node->viral_state_flags |= nodes[i]->viral_state_flags;
- } else if (i == 0) {
- index = 0;
- }
- indices[i] = index;
- }
- for (isize i = 0; i < gb_count_of(indices); i++) {
- i64 a = indices[i];
- for (isize j = i+1; j < gb_count_of(indices); j++) {
- i64 b = indices[j];
- if (a > b && b >= 0) {
- error(se->close, "Invalid slice indices: [%td > %td]", a, b);
- }
- }
- }
- if (max_count < 0) {
- if (o->mode == Addressing_Constant) {
- gbString s = expr_to_string(se->expr);
- error(se->expr, "Cannot slice constant value '%s'", s);
- gb_string_free(s);
- }
- }
- if (t->kind == Type_MultiPointer && se->high != nullptr) {
- /*
- x[:] -> [^]T
- x[i:] -> [^]T
- x[:n] -> []T
- x[i:n] -> []T
- */
- o->type = alloc_type_slice(t->MultiPointer.elem);
- }
- o->mode = Addressing_Value;
- if (is_type_string(t) && max_count >= 0) {
- bool all_constant = true;
- for (isize i = 0; i < gb_count_of(nodes); i++) {
- if (nodes[i] != nullptr) {
- TypeAndValue tav = type_and_value_of_expr(nodes[i]);
- if (tav.mode != Addressing_Constant) {
- all_constant = false;
- break;
- }
- }
- }
- if (!all_constant) {
- ERROR_BLOCK();
- gbString str = expr_to_string(o->expr);
- error(o->expr, "Cannot slice '%s' with non-constant indices", str);
- if (!build_context.terse_errors) {
- error_line("\tSuggestion: store the constant into a variable in order to index it with a variable index\n");
- }
- gb_string_free(str);
- o->mode = Addressing_Value; // NOTE(bill): Keep subsequent values going without erring
- o->expr = node;
- return kind;
- }
- String s = {};
- if (o->value.kind == ExactValue_String) {
- s = o->value.value_string;
- }
- o->mode = Addressing_Constant;
- o->type = t;
- o->value = exact_value_string(substring(s, cast(isize)indices[0], cast(isize)indices[1]));
- }
- return kind;
- }
- gb_internal ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- u32 prev_state_flags = c->state_flags;
- defer (c->state_flags = prev_state_flags);
- if (node->state_flags != 0) {
- u32 in = node->state_flags;
- u32 out = c->state_flags;
- if (in & StateFlag_no_bounds_check) {
- out |= StateFlag_no_bounds_check;
- out &= ~StateFlag_bounds_check;
- } else if (in & StateFlag_bounds_check) {
- out |= StateFlag_bounds_check;
- out &= ~StateFlag_no_bounds_check;
- }
- if (in & StateFlag_no_type_assert) {
- out |= StateFlag_no_type_assert;
- out &= ~StateFlag_type_assert;
- } else if (in & StateFlag_type_assert) {
- out |= StateFlag_type_assert;
- out &= ~StateFlag_no_type_assert;
- }
- c->state_flags = out;
- }
- ExprKind kind = Expr_Stmt;
- o->mode = Addressing_Invalid;
- o->type = t_invalid;
- o->value = {ExactValue_Invalid};
- switch (node->kind) {
- default:
- return kind;
- case_ast_node(be, BadExpr, node)
- return kind;
- case_end;
- case_ast_node(i, Implicit, node);
- switch (i->kind) {
- case Token_context:
- {
- if (c->proc_name.len == 0 && c->curr_proc_sig == nullptr) {
- error(node, "'context' is only allowed within procedures");
- return kind;
- }
- if (unparen_expr(c->assignment_lhs_hint) == node) {
- c->scope->flags |= ScopeFlag_ContextDefined;
- }
- if ((c->scope->flags & ScopeFlag_ContextDefined) == 0) {
- error(node, "'context' has not been defined within this scope");
- // Continue with value
- }
- init_core_context(c->checker);
- o->mode = Addressing_Context;
- o->type = t_context;
- }
- break;
- default:
- error(node, "Illegal implicit name '%.*s'", LIT(i->string));
- return kind;
- }
- case_end;
- case_ast_node(i, Ident, node);
- check_ident(c, o, node, nullptr, type_hint, false);
- case_end;
- case_ast_node(u, Uninit, node);
- o->mode = Addressing_Value;
- o->type = t_untyped_uninit;
- error(node, "Global variables will always be zeroed if left unassigned, --- is disallowed");
- case_end;
- case_ast_node(bl, BasicLit, node);
- Type *t = t_invalid;
- switch (node->tav.value.kind) {
- case ExactValue_String: t = t_untyped_string; break;
- case ExactValue_Float: t = t_untyped_float; break;
- case ExactValue_Complex: t = t_untyped_complex; break;
- case ExactValue_Quaternion: t = t_untyped_quaternion; break;
- case ExactValue_Integer:
- t = t_untyped_integer;
- if (bl->token.kind == Token_Rune) {
- t = t_untyped_rune;
- }
- break;
- default:
- GB_PANIC("Unhandled value type for basic literal");
- break;
- }
- o->mode = Addressing_Constant;
- o->type = t;
- o->value = node->tav.value;
- case_end;
- case_ast_node(bd, BasicDirective, node);
- kind = check_basic_directive_expr(c, o, node, type_hint);
- case_end;
- case_ast_node(pg, ProcGroup, node);
- error(node, "Illegal use of a procedure group");
- o->mode = Addressing_Invalid;
- case_end;
- case_ast_node(pl, ProcLit, node);
- CheckerContext ctx = *c;
- DeclInfo *decl = nullptr;
- Type *type = alloc_type(Type_Proc);
- check_open_scope(&ctx, pl->type);
- {
- decl = make_decl_info(ctx.scope, ctx.decl);
- decl->proc_lit = node;
- ctx.decl = decl;
- defer (ctx.decl = ctx.decl->parent);
- if (pl->tags != 0) {
- error(node, "A procedure literal cannot have tags");
- pl->tags = 0; // TODO(bill): Should I zero this?!
- }
- check_procedure_type(&ctx, type, pl->type);
- if (!is_type_proc(type)) {
- gbString str = expr_to_string(node);
- error(node, "Invalid procedure literal '%s'", str);
- gb_string_free(str);
- check_close_scope(&ctx);
- return kind;
- }
- if (pl->body == nullptr) {
- error(node, "A procedure literal must have a body");
- return kind;
- }
- pl->decl = decl;
- check_procedure_later(ctx.checker, ctx.file, empty_token, decl, type, pl->body, pl->tags);
- mutex_lock(&ctx.checker->nested_proc_lits_mutex);
- array_add(&ctx.checker->nested_proc_lits, decl);
- mutex_unlock(&ctx.checker->nested_proc_lits_mutex);
- }
- check_close_scope(&ctx);
- o->mode = Addressing_Value;
- o->type = type;
- case_end;
- case_ast_node(te, TernaryIfExpr, node);
- kind = check_ternary_if_expr(c, o, node, type_hint);
- case_end;
- case_ast_node(te, TernaryWhenExpr, node);
- kind = check_ternary_when_expr(c, o, node, type_hint);
- case_end;
- case_ast_node(oe, OrElseExpr, node);
- return check_or_else_expr(c, o, node, type_hint);
- case_end;
- case_ast_node(re, OrReturnExpr, node);
- node->viral_state_flags |= ViralStateFlag_ContainsOrReturn;
- return check_or_return_expr(c, o, node, type_hint);
- case_end;
- case_ast_node(re, OrBranchExpr, node);
- return check_or_branch_expr(c, o, node, type_hint);
- case_end;
- case_ast_node(cl, CompoundLit, node);
- kind = check_compound_literal(c, o, node, type_hint);
- case_end;
- case_ast_node(pe, ParenExpr, node);
- kind = check_expr_base(c, o, pe->expr, type_hint);
- node->viral_state_flags |= pe->expr->viral_state_flags;
- o->expr = node;
- case_end;
- case_ast_node(te, TagExpr, node);
- String name = te->name.string;
- error(node, "Unknown tag expression, #%.*s", LIT(name));
- if (te->expr) {
- kind = check_expr_base(c, o, te->expr, type_hint);
- node->viral_state_flags |= te->expr->viral_state_flags;
- }
- o->expr = node;
- case_end;
- case_ast_node(ta, TypeAssertion, node);
- kind = check_type_assertion(c, o, node, type_hint);
- case_end;
- case_ast_node(tc, TypeCast, node);
- check_expr_or_type(c, o, tc->type);
- if (o->mode != Addressing_Type) {
- gbString str = expr_to_string(tc->type);
- error(tc->type, "Expected a type, got %s", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- }
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- Type *type = o->type;
- check_expr_base(c, o, tc->expr, type);
- node->viral_state_flags |= tc->expr->viral_state_flags;
- if (o->mode != Addressing_Invalid) {
- switch (tc->token.kind) {
- case Token_transmute:
- check_transmute(c, node, o, type, true);
- break;
- case Token_cast:
- check_cast(c, o, type, true);
- break;
- default:
- error(node, "Invalid AST: Invalid casting expression");
- o->mode = Addressing_Invalid;
- break;
- }
- }
- return Expr_Expr;
- case_end;
- case_ast_node(ac, AutoCast, node);
- check_expr_base(c, o, ac->expr, type_hint);
- node->viral_state_flags |= ac->expr->viral_state_flags;
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- if (type_hint) {
- check_cast(c, o, type_hint);
- }
- o->expr = node;
- return Expr_Expr;
- case_end;
- case_ast_node(ue, UnaryExpr, node);
- Type *th = type_hint;
- if (ue->op.kind == Token_And) {
- th = type_deref(th);
- }
- check_expr_base(c, o, ue->expr, th);
- node->viral_state_flags |= ue->expr->viral_state_flags;
- if (o->mode != Addressing_Invalid) {
- check_unary_expr(c, o, ue->op, node);
- }
- o->expr = node;
- return Expr_Expr;
- case_end;
- case_ast_node(be, BinaryExpr, node);
- check_binary_expr(c, o, node, type_hint, true);
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- case_end;
- case_ast_node(se, SelectorExpr, node);
- check_selector(c, o, node, type_hint);
- node->viral_state_flags |= se->expr->viral_state_flags;
- case_end;
- case_ast_node(se, SelectorCallExpr, node);
- return check_selector_call_expr(c, o, node, type_hint);
- case_end;
- case_ast_node(ise, ImplicitSelectorExpr, node);
- return check_implicit_selector_expr(c, o, node, type_hint);
- case_end;
- case_ast_node(ie, IndexExpr, node);
- kind = check_index_expr(c, o, node, type_hint);
- case_end;
- case_ast_node(se, SliceExpr, node);
- kind = check_slice_expr(c, o, node, type_hint);
- case_end;
-
- case_ast_node(mie, MatrixIndexExpr, node);
- check_matrix_index_expr(c, o, node, type_hint);
- o->expr = node;
- return Expr_Expr;
- case_end;
- case_ast_node(ce, CallExpr, node);
- return check_call_expr(c, o, node, ce->proc, ce->args, ce->inlining, type_hint);
- case_end;
- case_ast_node(de, DerefExpr, node);
- check_expr_or_type(c, o, de->expr);
- node->viral_state_flags |= de->expr->viral_state_flags;
- if (o->mode == Addressing_Invalid) {
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- } else if (o->mode == Addressing_Type) {
- gbString str = expr_to_string(o->expr);
- error(o->expr, "Cannot dereference '%s' because it is a type", str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- } else {
- Type *t = base_type(o->type);
- if (t->kind == Type_Pointer && !is_type_empty_union(t->Pointer.elem)) {
- o->mode = Addressing_Variable;
- o->type = t->Pointer.elem;
- } else if (t->kind == Type_SoaPointer) {
- o->mode = Addressing_SoaVariable;
- o->type = type_deref(t);
- } else {
- gbString str = expr_to_string(o->expr);
- gbString typ = type_to_string(o->type);
- ERROR_BLOCK();
- error(o->expr, "Cannot dereference '%s' of type '%s'", str, typ);
- if (o->type && is_type_multi_pointer(o->type)) {
- if (!build_context.terse_errors) {
- error_line("\tDid you mean '%s[0]'?\n", str);
- }
- }
- gb_string_free(typ);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- }
- case_end;
- case_ast_node(ia, InlineAsmExpr, node);
- if (c->curr_proc_decl == nullptr) {
- error(node, "Inline asm expressions are only allowed within a procedure body");
- }
- auto param_types = array_make<Type *>(heap_allocator(), ia->param_types.count);
- Type *return_type = nullptr;
- for_array(i, ia->param_types) {
- param_types[i] = check_type(c, ia->param_types[i]);
- }
- if (ia->return_type != nullptr) {
- return_type = check_type(c, ia->return_type);
- }
- Operand x = {};
- check_expr(c, &x, ia->asm_string);
- if (x.mode != Addressing_Constant || !is_type_string(x.type)) {
- error(x.expr, "Expected a constant string for the inline asm main parameter");
- }
- check_expr(c, &x, ia->constraints_string);
- if (x.mode != Addressing_Constant || !is_type_string(x.type)) {
- error(x.expr, "Expected a constant string for the inline asm constraints parameter");
- }
- Scope *scope = create_scope(c->info, c->scope);
- scope->flags |= ScopeFlag_Proc;
- Type *params = alloc_type_tuple();
- Type *results = alloc_type_tuple();
- if (param_types.count != 0) {
- slice_init(¶ms->Tuple.variables, heap_allocator(), param_types.count);
- for_array(i, param_types) {
- params->Tuple.variables[i] = alloc_entity_param(scope, blank_token, param_types[i], false, true);
- }
- }
- if (return_type != nullptr) {
- slice_init(&results->Tuple.variables, heap_allocator(), 1);
- results->Tuple.variables[0] = alloc_entity_param(scope, blank_token, return_type, false, true);
- }
- Type *pt = alloc_type_proc(scope, params, param_types.count, results, return_type != nullptr ? 1 : 0, false, ProcCC_InlineAsm);
- o->type = pt;
- o->mode = Addressing_Value;
- o->expr = node;
- return Expr_Expr;
- case_end;
- case Ast_DistinctType:
- case Ast_TypeidType:
- case Ast_PolyType:
- case Ast_ProcType:
- case Ast_PointerType:
- case Ast_MultiPointerType:
- case Ast_ArrayType:
- case Ast_DynamicArrayType:
- case Ast_StructType:
- case Ast_UnionType:
- case Ast_EnumType:
- case Ast_MapType:
- case Ast_BitSetType:
- case Ast_MatrixType:
- case Ast_RelativeType:
- o->mode = Addressing_Type;
- o->type = check_type(c, node);
- break;
- }
- kind = Expr_Expr;
- o->expr = node;
- return kind;
- }
- gb_internal ExprKind check_expr_base(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ExprKind kind = check_expr_base_internal(c, o, node, type_hint);
- if (o->type != nullptr && core_type(o->type) == nullptr) {
- o->type = t_invalid;
- gbString xs = expr_to_string(o->expr);
- if (o->mode == Addressing_Type) {
- error(o->expr, "Invalid type usage '%s'", xs);
- } else {
- error(o->expr, "Invalid expression '%s'", xs);
- }
- gb_string_free(xs);
- }
- if (o->type != nullptr && is_type_untyped(o->type)) {
- add_untyped(c, node, o->mode, o->type, o->value);
- }
- check_rtti_type_disallowed(node, o->type, "An expression is using a type, %s, which has been disallowed");
- add_type_and_value(c, node, o->mode, o->type, o->value);
- return kind;
- }
- gb_internal void check_multi_expr_or_type(CheckerContext *c, Operand *o, Ast *e) {
- check_expr_base(c, o, e, nullptr);
- switch (o->mode) {
- default:
- return; // NOTE(bill): Valid
- case Addressing_NoValue:
- error_operand_no_value(o);
- break;
- }
- o->mode = Addressing_Invalid;
- }
- gb_internal void check_multi_expr(CheckerContext *c, Operand *o, Ast *e) {
- check_expr_base(c, o, e, nullptr);
- switch (o->mode) {
- default:
- return; // NOTE(bill): Valid
- case Addressing_NoValue:
- error_operand_no_value(o);
- break;
- case Addressing_Type:
- error_operand_not_expression(o);
- break;
- }
- o->mode = Addressing_Invalid;
- }
- gb_internal void check_multi_expr_with_type_hint(CheckerContext *c, Operand *o, Ast *e, Type *type_hint) {
- check_expr_base(c, o, e, type_hint);
- switch (o->mode) {
- default:
- return; // NOTE(bill): Valid
- case Addressing_NoValue:
- error_operand_no_value(o);
- break;
- case Addressing_Type:
- error_operand_not_expression(o);
- break;
- }
- o->mode = Addressing_Invalid;
- }
- gb_internal void check_not_tuple(CheckerContext *c, Operand *o) {
- if (o->mode == Addressing_Value) {
- // NOTE(bill): Tuples are not first class thus never named
- if (o->type->kind == Type_Tuple) {
- isize count = o->type->Tuple.variables.count;
- error(o->expr,
- "%td-valued expression found where single value expected", count);
- o->mode = Addressing_Invalid;
- GB_ASSERT(count != 1);
- }
- }
- }
- gb_internal void check_expr(CheckerContext *c, Operand *o, Ast *e) {
- check_multi_expr(c, o, e);
- check_not_tuple(c, o);
- }
- gb_internal void check_expr_or_type(CheckerContext *c, Operand *o, Ast *e, Type *type_hint) {
- check_expr_base(c, o, e, type_hint);
- check_not_tuple(c, o);
- error_operand_no_value(o);
- }
- gb_internal bool is_exact_value_zero(ExactValue const &v) {
- switch (v.kind) {
- case ExactValue_Invalid:
- return true;
- case ExactValue_Bool:
- return !v.value_bool;
- case ExactValue_String:
- return v.value_string.len == 0;
- case ExactValue_Integer:
- return big_int_is_zero(&v.value_integer);
- case ExactValue_Float:
- return v.value_float == 0.0;
- case ExactValue_Complex:
- if (v.value_complex) {
- return v.value_complex->real == 0.0 && v.value_complex->imag == 0.0;
- }
- return true;
- case ExactValue_Quaternion:
- if (v.value_quaternion) {
- return v.value_quaternion->real == 0.0 &&
- v.value_quaternion->imag == 0.0 &&
- v.value_quaternion->jmag == 0.0 &&
- v.value_quaternion->kmag == 0.0;
- }
- return true;
- case ExactValue_Pointer:
- return v.value_pointer == 0;
- case ExactValue_Compound:
- if (v.value_compound == nullptr) {
- return true;
- } else {
- ast_node(cl, CompoundLit, v.value_compound);
- if (cl->elems.count == 0) {
- return true;
- } else {
- for (Ast *elem : cl->elems) {
- if (elem->tav.mode != Addressing_Constant) {
- return false;
- }
- if (!is_exact_value_zero(elem->tav.value)) {
- return false;
- }
- }
- return true;
- }
- }
- case ExactValue_Procedure:
- return v.value_procedure == nullptr;
- case ExactValue_Typeid:
- return v.value_typeid == nullptr;
- }
- return true;
- }
- gb_internal gbString write_expr_to_string(gbString str, Ast *node, bool shorthand);
- gb_internal gbString write_struct_fields_to_string(gbString str, Slice<Ast *> const ¶ms) {
- for_array(i, params) {
- if (i > 0) {
- str = gb_string_appendc(str, ", ");
- }
- str = write_expr_to_string(str, params[i], false);
- }
- return str;
- }
- gb_internal gbString string_append_string(gbString str, String string) {
- if (string.len > 0) {
- return gb_string_append_length(str, &string[0], string.len);
- }
- return str;
- }
- gb_internal gbString string_append_token(gbString str, Token token) {
- str = string_append_string(str, token.string);
- return str;
- }
- gb_internal gbString write_expr_to_string(gbString str, Ast *node, bool shorthand) {
- if (node == nullptr)
- return str;
- if (is_ast_stmt(node)) {
- GB_ASSERT("stmt passed to write_expr_to_string");
- }
- switch (node->kind) {
- default:
- str = gb_string_appendc(str, "(BadExpr)");
- break;
- case_ast_node(i, Ident, node);
- str = string_append_token(str, i->token);
- case_end;
- case_ast_node(i, Implicit, node);
- str = string_append_token(str, *i);
- case_end;
- case_ast_node(bl, BasicLit, node);
- str = string_append_token(str, bl->token);
- case_end;
- case_ast_node(bd, BasicDirective, node);
- str = gb_string_append_rune(str, '#');
- str = string_append_string(str, bd->name.string);
- case_end;
- case_ast_node(ud, Uninit, node);
- str = gb_string_appendc(str, "---");
- case_end;
- case_ast_node(pg, ProcGroup, node);
- str = gb_string_appendc(str, "proc{");
- for_array(i, pg->args) {
- if (i > 0) str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, pg->args[i], shorthand);
- }
- str = gb_string_append_rune(str, '}');
- case_end;
- case_ast_node(pl, ProcLit, node);
- str = write_expr_to_string(str, pl->type, shorthand);
- if (pl->body) {
- str = gb_string_appendc(str, " {...}");
- } else {
- str = gb_string_appendc(str, " ---");
- }
- case_end;
- case_ast_node(cl, CompoundLit, node);
- str = write_expr_to_string(str, cl->type, shorthand);
- str = gb_string_append_rune(str, '{');
- if (shorthand) {
- str = gb_string_appendc(str, "...");
- } else {
- for_array(i, cl->elems) {
- if (i > 0) str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, cl->elems[i], shorthand);
- }
- }
- str = gb_string_append_rune(str, '}');
- case_end;
- case_ast_node(te, TagExpr, node);
- str = gb_string_append_rune(str, '#');
- str = string_append_token(str, te->name);
- str = write_expr_to_string(str, te->expr, shorthand);
- case_end;
- case_ast_node(ue, UnaryExpr, node);
- str = string_append_token(str, ue->op);
- str = write_expr_to_string(str, ue->expr, shorthand);
- case_end;
- case_ast_node(de, DerefExpr, node);
- str = write_expr_to_string(str, de->expr, shorthand);
- str = gb_string_append_rune(str, '^');
- case_end;
- case_ast_node(be, BinaryExpr, node);
- str = write_expr_to_string(str, be->left, shorthand);
- str = gb_string_append_rune(str, ' ');
- str = string_append_token(str, be->op);
- str = gb_string_append_rune(str, ' ');
- str = write_expr_to_string(str, be->right, shorthand);
- case_end;
- case_ast_node(te, TernaryIfExpr, node);
- TokenPos x = ast_token(te->x).pos;
- TokenPos cond = ast_token(te->cond).pos;
- if (x < cond) {
- str = write_expr_to_string(str, te->x, shorthand);
- str = gb_string_appendc(str, " if ");
- str = write_expr_to_string(str, te->cond, shorthand);
- str = gb_string_appendc(str, " else ");
- str = write_expr_to_string(str, te->y, shorthand);
- } else {
- str = write_expr_to_string(str, te->cond, shorthand);
- str = gb_string_appendc(str, " ? ");
- str = write_expr_to_string(str, te->x, shorthand);
- str = gb_string_appendc(str, " : ");
- str = write_expr_to_string(str, te->y, shorthand);
- }
- case_end;
- case_ast_node(te, TernaryWhenExpr, node);
- str = write_expr_to_string(str, te->x, shorthand);
- str = gb_string_appendc(str, " when ");
- str = write_expr_to_string(str, te->cond, shorthand);
- str = gb_string_appendc(str, " else ");
- str = write_expr_to_string(str, te->y, shorthand);
- case_end;
- case_ast_node(oe, OrElseExpr, node);
- str = write_expr_to_string(str, oe->x, shorthand);
- str = gb_string_appendc(str, " or_else ");
- str = write_expr_to_string(str, oe->y, shorthand);
- case_end;
- case_ast_node(oe, OrReturnExpr, node);
- str = write_expr_to_string(str, oe->expr, shorthand);
- str = gb_string_appendc(str, " or_return");
- case_end;
- case_ast_node(oe, OrBranchExpr, node);
- str = write_expr_to_string(str, oe->expr, shorthand);
- str = gb_string_append_rune(str, ' ');
- str = string_append_token(str, oe->token);
- if (oe->label) {
- str = gb_string_append_rune(str, ' ');
- str = write_expr_to_string(str, oe->label, shorthand);
- }
- case_end;
- case_ast_node(pe, ParenExpr, node);
- str = gb_string_append_rune(str, '(');
- str = write_expr_to_string(str, pe->expr, shorthand);
- str = gb_string_append_rune(str, ')');
- case_end;
- case_ast_node(se, SelectorExpr, node);
- str = write_expr_to_string(str, se->expr, shorthand);
- str = string_append_token(str, se->token);
- str = write_expr_to_string(str, se->selector, shorthand);
- case_end;
- case_ast_node(se, ImplicitSelectorExpr, node);
- str = gb_string_append_rune(str, '.');
- str = write_expr_to_string(str, se->selector, shorthand);
- case_end;
- case_ast_node(se, SelectorCallExpr, node);
- str = write_expr_to_string(str, se->expr, shorthand);
- str = gb_string_appendc(str, "(");
- ast_node(ce, CallExpr, se->call);
- isize start = se->modified_call ? 1 : 0;
- for (isize i = start; i < ce->args.count; i++) {
- Ast *arg = ce->args[i];
- if (i > start) {
- str = gb_string_appendc(str, ", ");
- }
- str = write_expr_to_string(str, arg, shorthand);
- }
- str = gb_string_appendc(str, ")");
- case_end;
- case_ast_node(ta, TypeAssertion, node);
- str = write_expr_to_string(str, ta->expr, shorthand);
- if (ta->type != nullptr &&
- ta->type->kind == Ast_UnaryExpr &&
- ta->type->UnaryExpr.op.kind == Token_Question) {
- str = gb_string_appendc(str, ".?");
- } else {
- str = gb_string_appendc(str, ".(");
- str = write_expr_to_string(str, ta->type, shorthand);
- str = gb_string_append_rune(str, ')');
- }
- case_end;
- case_ast_node(tc, TypeCast, node);
- str = string_append_token(str, tc->token);
- str = gb_string_append_rune(str, '(');
- str = write_expr_to_string(str, tc->type, shorthand);
- str = gb_string_append_rune(str, ')');
- str = write_expr_to_string(str, tc->expr, shorthand);
- case_end;
- case_ast_node(ac, AutoCast, node);
- str = string_append_token(str, ac->token);
- str = gb_string_append_rune(str, ' ');
- str = write_expr_to_string(str, ac->expr, shorthand);
- case_end;
- case_ast_node(ie, IndexExpr, node);
- str = write_expr_to_string(str, ie->expr, shorthand);
- str = gb_string_append_rune(str, '[');
- str = write_expr_to_string(str, ie->index, shorthand);
- str = gb_string_append_rune(str, ']');
- case_end;
- case_ast_node(se, SliceExpr, node);
- str = write_expr_to_string(str, se->expr, shorthand);
- str = gb_string_append_rune(str, '[');
- str = write_expr_to_string(str, se->low, shorthand);
- str = string_append_token(str, se->interval);
- str = write_expr_to_string(str, se->high, shorthand);
- str = gb_string_append_rune(str, ']');
- case_end;
- case_ast_node(mie, MatrixIndexExpr, node);
- str = write_expr_to_string(str, mie->expr, shorthand);
- str = gb_string_append_rune(str, '[');
- str = write_expr_to_string(str, mie->row_index, shorthand);
- str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, mie->column_index, shorthand);
- str = gb_string_append_rune(str, ']');
- case_end;
-
- case_ast_node(e, Ellipsis, node);
- str = gb_string_appendc(str, "..");
- str = write_expr_to_string(str, e->expr, shorthand);
- case_end;
- case_ast_node(fv, FieldValue, node);
- str = write_expr_to_string(str, fv->field, shorthand);
- str = gb_string_appendc(str, " = ");
- str = write_expr_to_string(str, fv->value, shorthand);
- case_end;
- case_ast_node(fv, EnumFieldValue, node);
- str = write_expr_to_string(str, fv->name, shorthand);
- if (fv->value) {
- str = gb_string_appendc(str, " = ");
- str = write_expr_to_string(str, fv->value, shorthand);
- }
- case_end;
- case_ast_node(ht, HelperType, node);
- str = gb_string_appendc(str, "#type ");
- str = write_expr_to_string(str, ht->type, shorthand);
- case_end;
- case_ast_node(ht, DistinctType, node);
- str = gb_string_appendc(str, "distinct ");
- str = write_expr_to_string(str, ht->type, shorthand);
- case_end;
- case_ast_node(pt, PolyType, node);
- str = gb_string_append_rune(str, '$');
- str = write_expr_to_string(str, pt->type, shorthand);
- if (pt->specialization != nullptr) {
- str = gb_string_append_rune(str, '/');
- str = write_expr_to_string(str, pt->specialization, shorthand);
- }
- case_end;
- case_ast_node(pt, PointerType, node);
- if (pt->tag) {
- str = write_expr_to_string(str, pt->tag, false);
- }
- str = gb_string_append_rune(str, '^');
- str = write_expr_to_string(str, pt->type, shorthand);
- case_end;
- case_ast_node(pt, MultiPointerType, node);
- str = gb_string_appendc(str, "[^]");
- str = write_expr_to_string(str, pt->type, shorthand);
- case_end;
- case_ast_node(at, ArrayType, node);
- if (at->tag) {
- str = write_expr_to_string(str, at->tag, false);
- }
- str = gb_string_append_rune(str, '[');
- if (at->count != nullptr &&
- at->count->kind == Ast_UnaryExpr &&
- at->count->UnaryExpr.op.kind == Token_Question) {
- str = gb_string_appendc(str, "?");
- } else {
- str = write_expr_to_string(str, at->count, shorthand);
- }
- str = gb_string_append_rune(str, ']');
- str = write_expr_to_string(str, at->elem, shorthand);
- case_end;
- case_ast_node(at, DynamicArrayType, node);
- if (at->tag) {
- str = write_expr_to_string(str, at->tag, false);
- }
- str = gb_string_appendc(str, "[dynamic]");
- str = write_expr_to_string(str, at->elem, shorthand);
- case_end;
- case_ast_node(bs, BitSetType, node);
- str = gb_string_appendc(str, "bit_set[");
- str = write_expr_to_string(str, bs->elem, shorthand);
- str = gb_string_appendc(str, "]");
- case_end;
- case_ast_node(mt, MapType, node);
- str = gb_string_appendc(str, "map[");
- str = write_expr_to_string(str, mt->key, shorthand);
- str = gb_string_append_rune(str, ']');
- str = write_expr_to_string(str, mt->value, shorthand);
- case_end;
-
- case_ast_node(mt, MatrixType, node);
- str = gb_string_appendc(str, "matrix[");
- str = write_expr_to_string(str, mt->row_count, shorthand);
- str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, mt->column_count, shorthand);
- str = gb_string_append_rune(str, ']');
- str = write_expr_to_string(str, mt->elem, shorthand);
- case_end;
- case_ast_node(f, Field, node);
- if (f->flags&FieldFlag_using) {
- str = gb_string_appendc(str, "using ");
- }
- if (f->flags&FieldFlag_no_alias) {
- str = gb_string_appendc(str, "#no_alias ");
- }
- if (f->flags&FieldFlag_c_vararg) {
- str = gb_string_appendc(str, "#c_vararg ");
- }
- if (f->flags&FieldFlag_any_int) {
- str = gb_string_appendc(str, "#any_int ");
- }
- if (f->flags&FieldFlag_no_broadcast) {
- str = gb_string_appendc(str, "#no_broadcast ");
- }
- if (f->flags&FieldFlag_const) {
- str = gb_string_appendc(str, "#const ");
- }
- if (f->flags&FieldFlag_subtype) {
- str = gb_string_appendc(str, "#subtype ");
- }
- for_array(i, f->names) {
- Ast *name = f->names[i];
- if (i > 0) str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, name, shorthand);
- }
- if (f->names.count > 0) {
- if (f->type == nullptr && f->default_value != nullptr) {
- str = gb_string_append_rune(str, ' ');
- }
- str = gb_string_appendc(str, ":");
- }
- if (f->type != nullptr) {
- str = gb_string_append_rune(str, ' ');
- str = write_expr_to_string(str, f->type, shorthand);
- }
- if (f->default_value != nullptr) {
- if (f->type != nullptr) {
- str = gb_string_append_rune(str, ' ');
- }
- str = gb_string_appendc(str, "= ");
- str = write_expr_to_string(str, f->default_value, shorthand);
- }
- case_end;
- case_ast_node(f, FieldList, node);
- bool has_name = false;
- for_array(i, f->list) {
- ast_node(field, Field, f->list[i]);
- if (field->names.count > 1) {
- has_name = true;
- break;
- }
- if (field->names.count == 0) {
- continue;
- }
- if (!is_blank_ident(field->names[0])) {
- has_name = true;
- break;
- }
- }
- for_array(i, f->list) {
- if (i > 0) str = gb_string_appendc(str, ", ");
- if (has_name) {
- str = write_expr_to_string(str, f->list[i], shorthand);
- } else {
- ast_node(field, Field, f->list[i]);
- if (field->flags&FieldFlag_using) {
- str = gb_string_appendc(str, "using ");
- }
- if (field->flags&FieldFlag_no_alias) {
- str = gb_string_appendc(str, "#no_alias ");
- }
- if (field->flags&FieldFlag_c_vararg) {
- str = gb_string_appendc(str, "#c_vararg ");
- }
- if (field->flags&FieldFlag_any_int) {
- str = gb_string_appendc(str, "#any_int ");
- }
- if (field->flags&FieldFlag_no_broadcast) {
- str = gb_string_appendc(str, "#no_broadcast ");
- }
- if (field->flags&FieldFlag_const) {
- str = gb_string_appendc(str, "#const ");
- }
- if (field->flags&FieldFlag_subtype) {
- str = gb_string_appendc(str, "#subtype ");
- }
- str = write_expr_to_string(str, field->type, shorthand);
- }
- }
- case_end;
- case_ast_node(ce, CallExpr, node);
- switch (ce->inlining) {
- case ProcInlining_inline:
- str = gb_string_appendc(str, "#force_inline ");
- break;
- case ProcInlining_no_inline:
- str = gb_string_appendc(str, "#force_no_inline ");
- break;
- }
- str = write_expr_to_string(str, ce->proc, shorthand);
- str = gb_string_appendc(str, "(");
- isize idx0 = cast(isize)ce->was_selector;
- for (isize i = idx0; i < ce->args.count; i++) {
- Ast *arg = ce->args[i];
- if (i > idx0) {
- str = gb_string_appendc(str, ", ");
- }
- str = write_expr_to_string(str, arg, shorthand);
- }
- str = gb_string_appendc(str, ")");
- case_end;
- case_ast_node(tt, TypeidType, node);
- str = gb_string_appendc(str, "typeid");
- if (tt->specialization) {
- str = gb_string_appendc(str, "/");
- str = write_expr_to_string(str, tt->specialization, shorthand);
- }
- case_end;
- case_ast_node(pt, ProcType, node);
- str = gb_string_appendc(str, "proc(");
- str = write_expr_to_string(str, pt->params, shorthand);
- str = gb_string_appendc(str, ")");
- if (pt->results != nullptr) {
- str = gb_string_appendc(str, " -> ");
- bool parens_needed = false;
- if (pt->results && pt->results->kind == Ast_FieldList) {
- for (Ast *field : pt->results->FieldList.list) {
- ast_node(f, Field, field);
- if (f->names.count != 0) {
- parens_needed = true;
- break;
- }
- }
- }
- if (parens_needed) {
- str = gb_string_append_rune(str, '(');
- }
- str = write_expr_to_string(str, pt->results, shorthand);
- if (parens_needed) {
- str = gb_string_append_rune(str, ')');
- }
- }
- case_end;
- case_ast_node(st, StructType, node);
- str = gb_string_appendc(str, "struct ");
- if (st->polymorphic_params) {
- str = gb_string_append_rune(str, '(');
- str = write_expr_to_string(str, st->polymorphic_params, shorthand);
- str = gb_string_appendc(str, ") ");
- }
- if (st->is_packed) str = gb_string_appendc(str, "#packed ");
- if (st->is_raw_union) str = gb_string_appendc(str, "#raw_union ");
- if (st->align) {
- str = gb_string_appendc(str, "#align ");
- str = write_expr_to_string(str, st->align, shorthand);
- str = gb_string_append_rune(str, ' ');
- }
- str = gb_string_append_rune(str, '{');
- if (shorthand) {
- str = gb_string_appendc(str, "...");
- } else {
- str = write_struct_fields_to_string(str, st->fields);
- }
- str = gb_string_append_rune(str, '}');
- case_end;
- case_ast_node(st, UnionType, node);
- str = gb_string_appendc(str, "union ");
- if (st->polymorphic_params) {
- str = gb_string_append_rune(str, '(');
- str = write_expr_to_string(str, st->polymorphic_params, shorthand);
- str = gb_string_appendc(str, ") ");
- }
- switch (st->kind) {
- case UnionType_no_nil: str = gb_string_appendc(str, "#no_nil "); break;
- case UnionType_shared_nil: str = gb_string_appendc(str, "#shared_nil "); break;
- }
- if (st->align) {
- str = gb_string_appendc(str, "#align ");
- str = write_expr_to_string(str, st->align, shorthand);
- str = gb_string_append_rune(str, ' ');
- }
- str = gb_string_append_rune(str, '{');
- if (shorthand) {
- str = gb_string_appendc(str, "...");
- } else {
- str = write_struct_fields_to_string(str, st->variants);
- }
- str = gb_string_append_rune(str, '}');
- case_end;
- case_ast_node(et, EnumType, node);
- str = gb_string_appendc(str, "enum ");
- if (et->base_type != nullptr) {
- str = write_expr_to_string(str, et->base_type, shorthand);
- str = gb_string_append_rune(str, ' ');
- }
- str = gb_string_append_rune(str, '{');
- if (shorthand) {
- str = gb_string_appendc(str, "...");
- } else {
- for_array(i, et->fields) {
- if (i > 0) {
- str = gb_string_appendc(str, ", ");
- }
- str = write_expr_to_string(str, et->fields[i], shorthand);
- }
- }
- str = gb_string_append_rune(str, '}');
- case_end;
- case_ast_node(rt, RelativeType, node);
- str = write_expr_to_string(str, rt->tag, shorthand);
- str = gb_string_appendc(str, "" );
- str = write_expr_to_string(str, rt->type, shorthand);
- case_end;
- case_ast_node(f, BitFieldField, node);
- str = write_expr_to_string(str, f->name, shorthand);
- str = gb_string_appendc(str, ": ");
- str = write_expr_to_string(str, f->type, shorthand);
- str = gb_string_appendc(str, " | ");
- str = write_expr_to_string(str, f->bit_size, shorthand);
- case_end;
- case_ast_node(bf, BitFieldType, node);
- str = gb_string_appendc(str, "bit_field ");
- if (!shorthand) {
- str = write_expr_to_string(str, bf->backing_type, shorthand);
- }
- str = gb_string_appendc(str, " {");
- if (shorthand) {
- str = gb_string_appendc(str, "...");
- } else {
- for_array(i, bf->fields) {
- if (i > 0) {
- str = gb_string_appendc(str, ", ");
- }
- str = write_expr_to_string(str, bf->fields[i], false);
- }
- }
- str = gb_string_appendc(str, "}");
- case_end;
- case_ast_node(ia, InlineAsmExpr, node);
- str = gb_string_appendc(str, "asm(");
- for_array(i, ia->param_types) {
- if (i > 0) {
- str = gb_string_appendc(str, ", ");
- }
- str = write_expr_to_string(str, ia->param_types[i], shorthand);
- }
- str = gb_string_appendc(str, ")");
- if (ia->return_type != nullptr) {
- str = gb_string_appendc(str, " -> ");
- str = write_expr_to_string(str, ia->return_type, shorthand);
- }
- if (ia->has_side_effects) {
- str = gb_string_appendc(str, " #side_effects");
- }
- if (ia->is_align_stack) {
- str = gb_string_appendc(str, " #stack_align");
- }
- if (ia->dialect) {
- str = gb_string_appendc(str, " #");
- str = gb_string_appendc(str, inline_asm_dialect_strings[ia->dialect]);
- }
- str = gb_string_appendc(str, " {");
- if (shorthand) {
- str = gb_string_appendc(str, "...");
- } else {
- str = write_expr_to_string(str, ia->asm_string, shorthand);
- str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, ia->constraints_string, shorthand);
- }
- str = gb_string_appendc(str, "}");
- case_end;
- }
- return str;
- }
- gb_internal gbString expr_to_string(Ast *expression) {
- return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression, false);
- }
- gb_internal gbString expr_to_string(Ast *expression, gbAllocator allocator) {
- return write_expr_to_string(gb_string_make(allocator, ""), expression, false);
- }
- gb_internal gbString expr_to_string_shorthand(Ast *expression) {
- return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression, true);
- }
|