check_expr.cpp 182 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677
  1. void check_expr (Checker *c, Operand *operand, AstNode *expression);
  2. void check_multi_expr (Checker *c, Operand *operand, AstNode *expression);
  3. void check_expr_or_type (Checker *c, Operand *operand, AstNode *expression);
  4. ExprKind check_expr_base (Checker *c, Operand *operand, AstNode *expression, Type *type_hint);
  5. void check_expr_with_type_hint (Checker *c, Operand *o, AstNode *e, Type *t);
  6. Type * check_type (Checker *c, AstNode *expression, Type *named_type = NULL);
  7. void check_type_decl (Checker *c, Entity *e, AstNode *type_expr, Type *def);
  8. Entity * check_selector (Checker *c, Operand *operand, AstNode *node, Type *type_hint);
  9. void check_not_tuple (Checker *c, Operand *operand);
  10. void convert_to_typed (Checker *c, Operand *operand, Type *target_type, i32 level);
  11. gbString expr_to_string (AstNode *expression);
  12. void check_entity_decl (Checker *c, Entity *e, DeclInfo *decl, Type *named_type);
  13. void check_const_decl (Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr, Type *named_type);
  14. void check_proc_body (Checker *c, Token token, DeclInfo *decl, Type *type, AstNode *body);
  15. void update_expr_type (Checker *c, AstNode *e, Type *type, bool final);
  16. bool check_is_terminating (AstNode *node);
  17. bool check_has_break (AstNode *stmt, bool implicit);
  18. void check_stmt (Checker *c, AstNode *node, u32 flags);
  19. void check_stmt_list (Checker *c, Array<AstNode *> stmts, u32 flags);
  20. void check_init_constant (Checker *c, Entity *e, Operand *operand);
  21. bool check_representable_as_constant(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value);
  22. Type * check_call_arguments (Checker *c, Operand *operand, Type *proc_type, AstNode *call);
  23. void error_operand_not_expression(Operand *o) {
  24. if (o->mode == Addressing_Type) {
  25. gbString err = expr_to_string(o->expr);
  26. error_node(o->expr, "`%s` is not an expression but a type", err);
  27. gb_string_free(err);
  28. o->mode = Addressing_Invalid;
  29. }
  30. }
  31. void error_operand_no_value(Operand *o) {
  32. if (o->mode == Addressing_NoValue) {
  33. gbString err = expr_to_string(o->expr);
  34. error_node(o->expr, "`%s` used as value", err);
  35. gb_string_free(err);
  36. o->mode = Addressing_Invalid;
  37. }
  38. }
  39. void check_scope_decls(Checker *c, Array<AstNode *> nodes, isize reserve_size) {
  40. Scope *s = c->context.scope;
  41. GB_ASSERT(!s->is_file);
  42. check_collect_entities(c, nodes, false);
  43. for_array(i, s->elements.entries) {
  44. Entity *e = s->elements.entries[i].value;
  45. switch (e->kind) {
  46. case Entity_Constant:
  47. case Entity_TypeName:
  48. case Entity_Procedure:
  49. break;
  50. default:
  51. continue;
  52. }
  53. DeclInfo **found = map_get(&c->info.entities, hash_pointer(e));
  54. if (found != NULL) {
  55. DeclInfo *d = *found;
  56. check_entity_decl(c, e, d, NULL);
  57. }
  58. }
  59. for_array(i, s->elements.entries) {
  60. Entity *e = s->elements.entries[i].value;
  61. if (e->kind != Entity_Procedure) {
  62. continue;
  63. }
  64. check_procedure_overloading(c, e);
  65. }
  66. }
  67. bool check_is_assignable_to_using_subtype(Type *src, Type *dst) {
  68. bool src_is_ptr = false;
  69. Type *prev_src = src;
  70. src = type_deref(src);
  71. src_is_ptr = src != prev_src;
  72. src = base_type(src);
  73. if (!is_type_struct(src) && !is_type_union(src)) {
  74. return false;
  75. }
  76. for (isize i = 0; i < src->Record.field_count; i++) {
  77. Entity *f = src->Record.fields[i];
  78. if (f->kind != Entity_Variable || (f->flags&EntityFlag_Using) == 0) {
  79. continue;
  80. }
  81. if (are_types_identical(f->type, dst)) {
  82. return true;
  83. }
  84. if (src_is_ptr && is_type_pointer(dst)) {
  85. if (are_types_identical(f->type, type_deref(dst))) {
  86. return true;
  87. }
  88. }
  89. bool ok = check_is_assignable_to_using_subtype(f->type, dst);
  90. if (ok) {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. // IMPORTANT TODO(bill): figure out the exact distance rules
  97. // -1 is not convertable
  98. // 0 is exact
  99. // >0 is convertable
  100. i64 check_distance_between_types(Checker *c, Operand *operand, Type *type) {
  101. if (operand->mode == Addressing_Invalid ||
  102. type == t_invalid) {
  103. return 0;
  104. }
  105. if (operand->mode == Addressing_Builtin) {
  106. return -1;
  107. }
  108. Type *s = operand->type;
  109. if (are_types_identical(s, type)) {
  110. return 0;
  111. }
  112. Type *src = base_type(s);
  113. Type *dst = base_type(type);
  114. if (is_type_untyped_nil(src)) {
  115. if (type_has_nil(dst)) {
  116. return 1;
  117. }
  118. return -1;
  119. }
  120. if (is_type_untyped(src)) {
  121. if (is_type_any(dst)) {
  122. // NOTE(bill): Anything can cast to `Any`
  123. add_type_info_type(c, s);
  124. return 10;
  125. }
  126. if (dst->kind == Type_Basic) {
  127. if (operand->mode == Addressing_Constant) {
  128. if (check_representable_as_constant(c, operand->value, dst, NULL)) {
  129. if (is_type_typed(dst) && src->kind == Type_Basic) {
  130. switch (src->Basic.kind) {
  131. case Basic_UntypedInteger:
  132. if (is_type_integer(dst) || is_type_rune(dst)) {
  133. return 1;
  134. }
  135. break;
  136. case Basic_UntypedFloat:
  137. if (is_type_float(dst)) {
  138. return 1;
  139. }
  140. break;
  141. case Basic_UntypedComplex:
  142. if (is_type_complex(dst)) {
  143. return 1;
  144. }
  145. break;
  146. }
  147. }
  148. return 2;
  149. }
  150. return -1;
  151. }
  152. if (src->kind == Type_Basic && src->Basic.kind == Basic_UntypedBool) {
  153. if (is_type_boolean(dst)) {
  154. if (is_type_typed(type)) {
  155. return 2;
  156. }
  157. return 1;
  158. }
  159. return -1;
  160. }
  161. }
  162. }
  163. if (are_types_identical(dst, src) && (!is_type_named(dst) || !is_type_named(src))) {
  164. return 1;
  165. }
  166. if (is_type_bit_field_value(operand->type) && is_type_integer(type)) {
  167. Type *bfv = base_type(operand->type);
  168. i32 bits = bfv->BitFieldValue.bits;
  169. i32 size = next_pow2((bits+7)/8);
  170. i32 dst_size = type_size_of(c->allocator, type);
  171. i32 diff = gb_abs(dst_size - size);
  172. // TODO(bill): figure out a decent rule here
  173. return 1;
  174. }
  175. if (check_is_assignable_to_using_subtype(operand->type, type)) {
  176. return 4;
  177. }
  178. // ^T <- rawptr
  179. #if 0
  180. // TODO(bill): Should C-style (not C++) pointer cast be allowed?
  181. if (is_type_pointer(dst) && is_type_rawptr(src)) {
  182. return true;
  183. }
  184. #endif
  185. #if 1
  186. // TODO(bill): Should I allow this implicit conversion at all?!
  187. // rawptr <- ^T
  188. if (are_types_identical(type, t_rawptr) && is_type_pointer(src)) {
  189. return 5;
  190. }
  191. #endif
  192. if (is_type_union(dst)) {
  193. for (isize i = 0; i < dst->Record.variant_count; i++) {
  194. Entity *f = dst->Record.variants[i];
  195. if (are_types_identical(f->type, s)) {
  196. return 1;
  197. }
  198. }
  199. }
  200. if (is_type_proc(dst)) {
  201. if (are_types_identical(src, dst)) {
  202. return 3;
  203. }
  204. }
  205. if (is_type_vector(dst)) {
  206. Type *elem = base_vector_type(dst);
  207. i64 distance = check_distance_between_types(c, operand, elem);
  208. if (distance >= 0) {
  209. return distance + 5;
  210. }
  211. }
  212. if (is_type_any(dst)) {
  213. // NOTE(bill): Anything can cast to `Any`
  214. add_type_info_type(c, s);
  215. return 10;
  216. }
  217. return -1;
  218. }
  219. i64 assign_score_function(i64 distance) {
  220. // TODO(bill): A decent score function
  221. return gb_max(1000000 - distance*distance, 0);
  222. }
  223. bool check_is_assignable_to_with_score(Checker *c, Operand *operand, Type *type, i64 *score_) {
  224. i64 score = 0;
  225. i64 distance = check_distance_between_types(c, operand, type);
  226. bool ok = distance >= 0;
  227. if (ok) {
  228. score = assign_score_function(distance);
  229. }
  230. if (score_) *score_ = score;
  231. return ok;
  232. }
  233. bool check_is_assignable_to(Checker *c, Operand *operand, Type *type) {
  234. i64 score = 0;
  235. return check_is_assignable_to_with_score(c, operand, type, &score);
  236. }
  237. // NOTE(bill): `content_name` is for debugging and error messages
  238. void check_assignment(Checker *c, Operand *operand, Type *type, String context_name) {
  239. check_not_tuple(c, operand);
  240. if (operand->mode == Addressing_Invalid) {
  241. return;
  242. }
  243. if (is_type_untyped(operand->type)) {
  244. Type *target_type = type;
  245. if (type == NULL || is_type_any(type)) {
  246. if (type == NULL && is_type_untyped_nil(operand->type)) {
  247. error_node(operand->expr, "Use of untyped nil in %.*s", LIT(context_name));
  248. operand->mode = Addressing_Invalid;
  249. return;
  250. }
  251. target_type = default_type(operand->type);
  252. if (type != NULL && !is_type_any(type)) {
  253. GB_ASSERT_MSG(is_type_typed(target_type), "%s", type_to_string(type));
  254. }
  255. add_type_info_type(c, type);
  256. add_type_info_type(c, target_type);
  257. }
  258. if (target_type != NULL && is_type_vector(target_type)) {
  259. // NOTE(bill): continue to below
  260. } else {
  261. convert_to_typed(c, operand, target_type, 0);
  262. if (operand->mode == Addressing_Invalid) {
  263. return;
  264. }
  265. }
  266. }
  267. if (type == NULL) {
  268. return;
  269. }
  270. if (!check_is_assignable_to(c, operand, type)) {
  271. gbString type_str = type_to_string(type);
  272. gbString op_type_str = type_to_string(operand->type);
  273. gbString expr_str = expr_to_string(operand->expr);
  274. if (operand->mode == Addressing_Builtin) {
  275. // TODO(bill): is this a good enough error message?
  276. // TODO(bill): Actually allow built in procedures to be passed around and thus be created on use
  277. error_node(operand->expr,
  278. "Cannot assign built-in procedure `%s` in %.*s",
  279. expr_str,
  280. LIT(context_name));
  281. } else {
  282. // TODO(bill): is this a good enough error message?
  283. error_node(operand->expr,
  284. "Cannot assign value `%s` of type `%s` to `%s` in %.*s",
  285. expr_str,
  286. op_type_str,
  287. type_str,
  288. LIT(context_name));
  289. }
  290. operand->mode = Addressing_Invalid;
  291. gb_string_free(expr_str);
  292. gb_string_free(op_type_str);
  293. gb_string_free(type_str);
  294. return;
  295. }
  296. }
  297. void populate_using_entity_map(Checker *c, AstNode *node, Type *t, Map<Entity *> *entity_map) {
  298. t = base_type(type_deref(t));
  299. gbString str = NULL;
  300. if (node != NULL) {
  301. expr_to_string(node);
  302. }
  303. if (t->kind == Type_Record) {
  304. for (isize i = 0; i < t->Record.field_count; i++) {
  305. Entity *f = t->Record.fields[i];
  306. GB_ASSERT(f->kind == Entity_Variable);
  307. String name = f->token.string;
  308. HashKey key = hash_string(name);
  309. Entity **found = map_get(entity_map, key);
  310. if (found != NULL) {
  311. Entity *e = *found;
  312. // TODO(bill): Better type error
  313. if (str != NULL) {
  314. error(e->token, "`%.*s` is already declared in `%s`", LIT(name), str);
  315. } else {
  316. error(e->token, "`%.*s` is already declared`", LIT(name));
  317. }
  318. } else {
  319. map_set(entity_map, key, f);
  320. add_entity(c, c->context.scope, NULL, f);
  321. if (f->flags & EntityFlag_Using) {
  322. populate_using_entity_map(c, node, f->type, entity_map);
  323. }
  324. }
  325. }
  326. }
  327. gb_string_free(str);
  328. }
  329. // Returns filled field_count
  330. isize check_fields(Checker *c, AstNode *node, Array<AstNode *> decls,
  331. Entity **fields, isize field_count,
  332. String context) {
  333. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  334. Map<Entity *> entity_map = {};
  335. map_init_with_reserve(&entity_map, c->tmp_allocator, 2*field_count);
  336. Entity *using_index_expr = NULL;
  337. if (node != NULL) {
  338. GB_ASSERT(node->kind != AstNode_UnionType);
  339. }
  340. isize field_index = 0;
  341. for_array(decl_index, decls) {
  342. AstNode *decl = decls[decl_index];
  343. if (decl->kind != AstNode_Field) {
  344. continue;
  345. }
  346. ast_node(f, Field, decl);
  347. Type *type = check_type(c, f->type);
  348. bool is_using = (f->flags&FieldFlag_using) != 0;
  349. if (is_using) {
  350. if (f->names.count > 1) {
  351. error_node(f->names[0], "Cannot apply `using` to more than one of the same type");
  352. is_using = false;
  353. }
  354. }
  355. for_array(name_index, f->names) {
  356. AstNode *name = f->names[name_index];
  357. if (!ast_node_expect(name, AstNode_Ident)) {
  358. continue;
  359. }
  360. Token name_token = name->Ident;
  361. Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, is_using, cast(i32)field_index);
  362. e->identifier = name;
  363. if (name_token.string == "_") {
  364. fields[field_index++] = e;
  365. } else if (name_token.string == "__tag") {
  366. error_node(name, "`__tag` is a reserved identifier for fields");
  367. } else {
  368. HashKey key = hash_string(name_token.string);
  369. Entity **found = map_get(&entity_map, key);
  370. if (found != NULL) {
  371. Entity *e = *found;
  372. // NOTE(bill): Scope checking already checks the declaration but in many cases, this can happen so why not?
  373. // This may be a little janky but it's not really that much of a problem
  374. error(name_token, "`%.*s` is already declared in this type", LIT(name_token.string));
  375. error(e->token, "\tpreviously declared");
  376. } else {
  377. map_set(&entity_map, key, e);
  378. fields[field_index++] = e;
  379. add_entity(c, c->context.scope, name, e);
  380. }
  381. add_entity_use(c, name, e);
  382. }
  383. }
  384. if (is_using) {
  385. Type *t = base_type(type_deref(type));
  386. if (!is_type_struct(t) && !is_type_raw_union(t) && !is_type_bit_field(t) &&
  387. f->names.count >= 1 &&
  388. f->names[0]->kind == AstNode_Ident) {
  389. Token name_token = f->names[0]->Ident;
  390. if (is_type_indexable(t)) {
  391. bool ok = true;
  392. for_array(emi, entity_map.entries) {
  393. Entity *e = entity_map.entries[emi].value;
  394. if (e->kind == Entity_Variable && e->flags & EntityFlag_Using) {
  395. if (is_type_indexable(e->type)) {
  396. if (e->identifier != f->names[0]) {
  397. ok = false;
  398. using_index_expr = e;
  399. break;
  400. }
  401. }
  402. }
  403. }
  404. if (ok) {
  405. using_index_expr = fields[field_index-1];
  406. } else {
  407. fields[field_index-1]->flags &= ~EntityFlag_Using;
  408. error(name_token, "Previous `using` for an index expression `%.*s`", LIT(name_token.string));
  409. }
  410. } else {
  411. gbString type_str = type_to_string(type);
  412. error(name_token, "`using` cannot be applied to the field `%.*s` of type `%s`", LIT(name_token.string), type_str);
  413. gb_string_free(type_str);
  414. continue;
  415. }
  416. }
  417. populate_using_entity_map(c, node, type, &entity_map);
  418. }
  419. }
  420. gb_temp_arena_memory_end(tmp);
  421. return field_index;
  422. }
  423. // TODO(bill): Cleanup struct field reordering
  424. // TODO(bill): Inline sorting procedure?
  425. gb_global gbAllocator __checker_allocator = {};
  426. GB_COMPARE_PROC(cmp_reorder_struct_fields) {
  427. // Rule:
  428. // `using` over non-`using`
  429. // Biggest to smallest alignment
  430. // if same alignment: biggest to smallest size
  431. // if same size: order by source order
  432. Entity *x = *(Entity **)a;
  433. Entity *y = *(Entity **)b;
  434. GB_ASSERT(x != NULL);
  435. GB_ASSERT(y != NULL);
  436. GB_ASSERT(x->kind == Entity_Variable);
  437. GB_ASSERT(y->kind == Entity_Variable);
  438. bool xu = (x->flags & EntityFlag_Using) != 0;
  439. bool yu = (y->flags & EntityFlag_Using) != 0;
  440. i64 xa = type_align_of(__checker_allocator, x->type);
  441. i64 ya = type_align_of(__checker_allocator, y->type);
  442. i64 xs = type_size_of(__checker_allocator, x->type);
  443. i64 ys = type_size_of(__checker_allocator, y->type);
  444. if (xu != yu) {
  445. return xu ? -1 : +1;
  446. }
  447. if (xa != ya) {
  448. return xa > ya ? -1 : xa < ya;
  449. }
  450. if (xs != ys) {
  451. return xs > ys ? -1 : xs < ys;
  452. }
  453. i32 diff = x->Variable.field_index - y->Variable.field_index;
  454. return diff < 0 ? -1 : diff > 0;
  455. }
  456. Entity *make_names_field_for_record(Checker *c, Scope *scope) {
  457. Entity *e = make_entity_field(c->allocator, scope,
  458. make_token_ident(str_lit("names")), t_string_slice, false, 0);
  459. e->Variable.is_immutable = true;
  460. e->flags |= EntityFlag_TypeField;
  461. return e;
  462. }
  463. void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
  464. GB_ASSERT(is_type_struct(struct_type));
  465. ast_node(st, StructType, node);
  466. isize field_count = 0;
  467. for_array(field_index, st->fields) {
  468. AstNode *field = st->fields[field_index];
  469. switch (field->kind) {
  470. case_ast_node(f, Field, field);
  471. field_count += f->names.count;
  472. case_end;
  473. }
  474. }
  475. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  476. field_count = check_fields(c, node, st->fields, fields, field_count, str_lit("struct"));
  477. struct_type->Record.is_packed = st->is_packed;
  478. struct_type->Record.is_ordered = st->is_ordered;
  479. struct_type->Record.fields = fields;
  480. struct_type->Record.fields_in_src_order = fields;
  481. struct_type->Record.field_count = field_count;
  482. struct_type->Record.names = make_names_field_for_record(c, c->context.scope);
  483. type_set_offsets(c->allocator, struct_type);
  484. if (!struct_type->failure && !st->is_packed && !st->is_ordered) {
  485. struct_type->failure = false;
  486. struct_type->Record.are_offsets_set = false;
  487. struct_type->Record.offsets = NULL;
  488. // NOTE(bill): Reorder fields for reduced size/performance
  489. Entity **reordered_fields = gb_alloc_array(c->allocator, Entity *, field_count);
  490. for (isize i = 0; i < field_count; i++) {
  491. reordered_fields[i] = struct_type->Record.fields_in_src_order[i];
  492. }
  493. // NOTE(bill): Hacky thing
  494. // TODO(bill): Probably make an inline sorting procedure rather than use global variables
  495. __checker_allocator = c->allocator;
  496. // NOTE(bill): compound literal order must match source not layout
  497. gb_sort_array(reordered_fields, field_count, cmp_reorder_struct_fields);
  498. for (isize i = 0; i < field_count; i++) {
  499. reordered_fields[i]->Variable.field_index = i;
  500. }
  501. struct_type->Record.fields = reordered_fields;
  502. }
  503. type_set_offsets(c->allocator, struct_type);
  504. if (st->align != NULL) {
  505. if (st->is_packed) {
  506. syntax_error_node(st->align, "`#align` cannot be applied with `#packed`");
  507. return;
  508. }
  509. Operand o = {};
  510. check_expr(c, &o, st->align);
  511. if (o.mode != Addressing_Constant) {
  512. if (o.mode != Addressing_Invalid) {
  513. error_node(st->align, "#align must be a constant");
  514. }
  515. return;
  516. }
  517. Type *type = base_type(o.type);
  518. if (is_type_untyped(type) || is_type_integer(type)) {
  519. if (o.value.kind == ExactValue_Integer) {
  520. i64 align = i128_to_i64(o.value.value_integer);
  521. if (align < 1 || !gb_is_power_of_two(align)) {
  522. error_node(st->align, "#align must be a power of 2, got %lld", align);
  523. return;
  524. }
  525. // NOTE(bill): Success!!!
  526. i64 custom_align = gb_clamp(align, 1, build_context.max_align);
  527. if (custom_align < align) {
  528. warning_node(st->align, "Custom alignment has been clamped to %lld from %lld", align, custom_align);
  529. }
  530. struct_type->Record.custom_align = custom_align;
  531. return;
  532. }
  533. }
  534. error_node(st->align, "#align must be an integer");
  535. return;
  536. }
  537. }
  538. void check_union_type(Checker *c, Type *union_type, AstNode *node) {
  539. GB_ASSERT(is_type_union(union_type));
  540. ast_node(ut, UnionType, node);
  541. isize variant_count = ut->variants.count+1;
  542. isize field_count = 0;
  543. for_array(i, ut->fields) {
  544. AstNode *field = ut->fields[i];
  545. if (field->kind == AstNode_Field) {
  546. ast_node(f, Field, field);
  547. field_count += f->names.count;
  548. }
  549. }
  550. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  551. Map<Entity *> entity_map = {}; // Key: String
  552. map_init_with_reserve(&entity_map, c->tmp_allocator, 2*variant_count);
  553. Entity *using_index_expr = NULL;
  554. Entity **variants = gb_alloc_array(c->allocator, Entity *, variant_count);
  555. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  556. isize variant_index = 0;
  557. variants[variant_index++] = make_entity_type_name(c->allocator, c->context.scope, empty_token, NULL);
  558. field_count = check_fields(c, NULL, ut->fields, fields, field_count, str_lit("union"));
  559. for (isize i = 0; i < field_count; i++) {
  560. Entity *f = fields[i];
  561. String name = f->token.string;
  562. map_set(&entity_map, hash_string(name), f);
  563. }
  564. union_type->Record.fields = fields;
  565. union_type->Record.fields_in_src_order = fields;
  566. union_type->Record.field_count = field_count;
  567. union_type->Record.are_offsets_set = false;
  568. union_type->Record.is_ordered = true;
  569. {
  570. Entity *__tag = make_entity_field(c->allocator, NULL, make_token_ident(str_lit("__tag")), t_int, false, -1);
  571. union_type->Record.union__tag = __tag;
  572. }
  573. for_array(i, ut->variants) {
  574. AstNode *variant = ut->variants[i];
  575. if (variant->kind != AstNode_UnionField) {
  576. continue;
  577. }
  578. ast_node(f, UnionField, variant);
  579. Token name_token = f->name->Ident;
  580. Type *base_type = make_type_struct(c->allocator);
  581. {
  582. ast_node(fl, FieldList, f->list);
  583. // NOTE(bill): Copy the contents for the common fields for now
  584. Array<AstNode *> list = {};
  585. array_init_count(&list, c->allocator, ut->fields.count+fl->list.count);
  586. gb_memmove_array(list.data, ut->fields.data, ut->fields.count);
  587. gb_memmove_array(list.data+ut->fields.count, fl->list.data, fl->list.count);
  588. isize list_count = 0;
  589. for_array(j, list) {
  590. ast_node(f, Field, list[j]);
  591. list_count += f->names.count;
  592. }
  593. Token token = name_token;
  594. token.kind = Token_struct;
  595. AstNode *dummy_struct = ast_struct_type(c->curr_ast_file, token, list, list_count, false, true, NULL);
  596. check_open_scope(c, dummy_struct);
  597. Entity **fields = gb_alloc_array(c->allocator, Entity *, list_count);
  598. isize field_count = check_fields(c, dummy_struct, list, fields, list_count, str_lit("variant"));
  599. base_type->Record.is_packed = false;
  600. base_type->Record.is_ordered = true;
  601. base_type->Record.fields = fields;
  602. base_type->Record.fields_in_src_order = fields;
  603. base_type->Record.field_count = field_count;
  604. base_type->Record.names = make_names_field_for_record(c, c->context.scope);
  605. base_type->Record.node = dummy_struct;
  606. type_set_offsets(c->allocator, base_type);
  607. check_close_scope(c);
  608. }
  609. Type *type = make_type_named(c->allocator, name_token.string, base_type, NULL);
  610. Entity *e = make_entity_type_name(c->allocator, c->context.scope, name_token, type);
  611. type->Named.type_name = e;
  612. add_entity(c, c->context.scope, f->name, e);
  613. if (name_token.string == "_") {
  614. error(name_token, "`_` cannot be used a union subtype");
  615. continue;
  616. }
  617. HashKey key = hash_string(name_token.string);
  618. if (map_get(&entity_map, key) != NULL) {
  619. // NOTE(bill): Scope checking already checks the declaration
  620. error(name_token, "`%.*s` is already declared in this union", LIT(name_token.string));
  621. } else {
  622. map_set(&entity_map, key, e);
  623. variants[variant_index++] = e;
  624. }
  625. add_entity_use(c, f->name, e);
  626. }
  627. type_set_offsets(c->allocator, union_type);
  628. gb_temp_arena_memory_end(tmp);
  629. union_type->Record.variants = variants;
  630. union_type->Record.variant_count = variant_index;
  631. }
  632. void check_raw_union_type(Checker *c, Type *union_type, AstNode *node) {
  633. GB_ASSERT(node->kind == AstNode_RawUnionType);
  634. GB_ASSERT(is_type_raw_union(union_type));
  635. ast_node(ut, RawUnionType, node);
  636. isize field_count = 0;
  637. for_array(field_index, ut->fields) {
  638. AstNode *field = ut->fields[field_index];
  639. switch (field->kind) {
  640. case_ast_node(f, Field, field);
  641. field_count += f->names.count;
  642. case_end;
  643. }
  644. }
  645. Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
  646. field_count = check_fields(c, node, ut->fields, fields, field_count, str_lit("raw_union"));
  647. union_type->Record.fields = fields;
  648. union_type->Record.field_count = field_count;
  649. union_type->Record.names = make_names_field_for_record(c, c->context.scope);
  650. }
  651. void check_enum_type(Checker *c, Type *enum_type, Type *named_type, AstNode *node) {
  652. ast_node(et, EnumType, node);
  653. GB_ASSERT(is_type_enum(enum_type));
  654. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  655. Type *base_type = t_int;
  656. if (et->base_type != NULL) {
  657. base_type = check_type(c, et->base_type);
  658. }
  659. if (base_type == NULL || !(is_type_integer(base_type) || is_type_float(base_type))) {
  660. error_node(node, "Base type for enumeration must be numeric");
  661. return;
  662. }
  663. if (is_type_enum(base_type)) {
  664. error_node(node, "Base type for enumeration cannot be another enumeration");
  665. return;
  666. }
  667. // NOTE(bill): Must be up here for the `check_init_constant` system
  668. enum_type->Record.enum_base_type = base_type;
  669. Map<Entity *> entity_map = {}; // Key: String
  670. map_init_with_reserve(&entity_map, c->tmp_allocator, 2*(et->fields.count));
  671. Entity **fields = gb_alloc_array(c->allocator, Entity *, et->fields.count);
  672. isize field_count = 0;
  673. Type *constant_type = enum_type;
  674. if (named_type != NULL) {
  675. constant_type = named_type;
  676. }
  677. ExactValue iota = exact_value_i64(-1);
  678. ExactValue min_value = exact_value_i64(0);
  679. ExactValue max_value = exact_value_i64(0);
  680. for_array(i, et->fields) {
  681. AstNode *field = et->fields[i];
  682. AstNode *ident = NULL;
  683. AstNode *init = NULL;
  684. if (field->kind == AstNode_FieldValue) {
  685. ast_node(fv, FieldValue, field);
  686. if (fv->field == NULL || fv->field->kind != AstNode_Ident) {
  687. error_node(field, "An enum field's name must be an identifier");
  688. continue;
  689. }
  690. ident = fv->field;
  691. init = fv->value;
  692. } else if (field->kind == AstNode_Ident) {
  693. ident = field;
  694. } else {
  695. error_node(field, "An enum field's name must be an identifier");
  696. continue;
  697. }
  698. String name = ident->Ident.string;
  699. if (init != NULL) {
  700. Operand o = {};
  701. check_expr(c, &o, init);
  702. if (o.mode != Addressing_Constant) {
  703. error_node(init, "Enumeration value must be a constant");
  704. o.mode = Addressing_Invalid;
  705. }
  706. if (o.mode != Addressing_Invalid) {
  707. check_assignment(c, &o, constant_type, str_lit("enumeration"));
  708. }
  709. if (o.mode != Addressing_Invalid) {
  710. iota = o.value;
  711. } else {
  712. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  713. }
  714. } else {
  715. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  716. }
  717. // NOTE(bill): Skip blank identifiers
  718. if (name == "_") {
  719. continue;
  720. } else if (name == "count") {
  721. error_node(field, "`count` is a reserved identifier for enumerations");
  722. continue;
  723. } else if (name == "min_value") {
  724. error_node(field, "`min_value` is a reserved identifier for enumerations");
  725. continue;
  726. } else if (name == "max_value") {
  727. error_node(field, "`max_value` is a reserved identifier for enumerations");
  728. continue;
  729. } else if (name == "names") {
  730. error_node(field, "`names` is a reserved identifier for enumerations");
  731. continue;
  732. }/* else if (name == "base_type") {
  733. error_node(field, "`base_type` is a reserved identifier for enumerations");
  734. continue;
  735. } */
  736. if (compare_exact_values(Token_Gt, min_value, iota)) {
  737. min_value = iota;
  738. }
  739. if (compare_exact_values(Token_Lt, max_value, iota)) {
  740. max_value = iota;
  741. }
  742. Entity *e = make_entity_constant(c->allocator, c->context.scope, ident->Ident, constant_type, iota);
  743. e->identifier = ident;
  744. e->flags |= EntityFlag_Visited;
  745. HashKey key = hash_string(name);
  746. if (map_get(&entity_map, key) != NULL) {
  747. error_node(ident, "`%.*s` is already declared in this enumeration", LIT(name));
  748. } else {
  749. map_set(&entity_map, key, e);
  750. add_entity(c, c->context.scope, NULL, e);
  751. fields[field_count++] = e;
  752. add_entity_use(c, field, e);
  753. }
  754. }
  755. GB_ASSERT(field_count <= et->fields.count);
  756. gb_temp_arena_memory_end(tmp);
  757. enum_type->Record.fields = fields;
  758. enum_type->Record.field_count = field_count;
  759. enum_type->Record.enum_count = make_entity_constant(c->allocator, c->context.scope,
  760. make_token_ident(str_lit("count")), t_int, exact_value_i64(field_count));
  761. enum_type->Record.enum_min_value = make_entity_constant(c->allocator, c->context.scope,
  762. make_token_ident(str_lit("min_value")), constant_type, min_value);
  763. enum_type->Record.enum_max_value = make_entity_constant(c->allocator, c->context.scope,
  764. make_token_ident(str_lit("max_value")), constant_type, max_value);
  765. enum_type->Record.names = make_names_field_for_record(c, c->context.scope);
  766. }
  767. void check_bit_field_type(Checker *c, Type *bit_field_type, Type *named_type, AstNode *node) {
  768. ast_node(bft, BitFieldType, node);
  769. GB_ASSERT(is_type_bit_field(bit_field_type));
  770. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  771. Map<Entity *> entity_map = {}; // Key: String
  772. map_init_with_reserve(&entity_map, c->tmp_allocator, 2*(bft->fields.count));
  773. isize field_count = 0;
  774. Entity **fields = gb_alloc_array(c->allocator, Entity *, bft->fields.count);
  775. u32 * sizes = gb_alloc_array(c->allocator, u32, bft->fields.count);
  776. u32 * offsets = gb_alloc_array(c->allocator, u32, bft->fields.count);
  777. u32 curr_offset = 0;
  778. for_array(i, bft->fields) {
  779. AstNode *field = bft->fields[i];
  780. GB_ASSERT(field->kind == AstNode_FieldValue);
  781. AstNode *ident = field->FieldValue.field;
  782. AstNode *value = field->FieldValue.value;
  783. if (ident->kind != AstNode_Ident) {
  784. error_node(field, "A bit field value's name must be an identifier");
  785. continue;
  786. }
  787. String name = ident->Ident.string;
  788. Operand o = {};
  789. check_expr(c, &o, value);
  790. if (o.mode != Addressing_Constant) {
  791. error_node(value, "Bit field bit size must be a constant");
  792. continue;
  793. }
  794. ExactValue v = exact_value_to_integer(o.value);
  795. if (v.kind != ExactValue_Integer) {
  796. error_node(value, "Bit field bit size must be a constant integer");
  797. continue;
  798. }
  799. i64 bits = i128_to_i64(v.value_integer);
  800. if (bits < 0 || bits > 128) {
  801. error_node(value, "Bit field's bit size must be within the range 1..<128, got %lld", cast(long long)bits);
  802. continue;
  803. }
  804. Type *value_type = make_type_bit_field_value(c->allocator, bits);
  805. Entity *e = make_entity_variable(c->allocator, bit_field_type->BitField.scope, ident->Ident, value_type, false);
  806. e->identifier = ident;
  807. e->flags |= EntityFlag_BitFieldValue;
  808. HashKey key = hash_string(name);
  809. if (name != "_" &&
  810. map_get(&entity_map, key) != NULL) {
  811. error_node(ident, "`%.*s` is already declared in this bit field", LIT(name));
  812. } else {
  813. map_set(&entity_map, key, e);
  814. add_entity(c, c->context.scope, NULL, e);
  815. add_entity_use(c, field, e);
  816. fields [field_count] = e;
  817. offsets[field_count] = curr_offset;
  818. sizes [field_count] = bits;
  819. field_count++;
  820. curr_offset += bits;
  821. }
  822. }
  823. GB_ASSERT(field_count <= bft->fields.count);
  824. gb_temp_arena_memory_end(tmp);
  825. bit_field_type->BitField.fields = fields;
  826. bit_field_type->BitField.field_count = field_count;
  827. bit_field_type->BitField.sizes = sizes;
  828. bit_field_type->BitField.offsets = offsets;
  829. if (bft->align != NULL) {
  830. Operand o = {};
  831. check_expr(c, &o, bft->align);
  832. if (o.mode != Addressing_Constant) {
  833. if (o.mode != Addressing_Invalid) {
  834. error_node(bft->align, "#align must be a constant");
  835. }
  836. return;
  837. }
  838. Type *type = base_type(o.type);
  839. if (is_type_untyped(type) || is_type_integer(type)) {
  840. if (o.value.kind == ExactValue_Integer) {
  841. i64 align = i128_to_i64(o.value.value_integer);
  842. if (align < 1 || !gb_is_power_of_two(align)) {
  843. error_node(bft->align, "#align must be a power of 2, got %lld", align);
  844. return;
  845. }
  846. // NOTE(bill): Success!!!
  847. i64 custom_align = gb_clamp(align, 1, build_context.max_align);
  848. if (custom_align < align) {
  849. warning_node(bft->align, "Custom alignment has been clamped to %lld from %lld", align, custom_align);
  850. }
  851. bit_field_type->BitField.custom_align = custom_align;
  852. return;
  853. }
  854. }
  855. error_node(bft->align, "#align must be an integer");
  856. return;
  857. }
  858. }
  859. Type *check_get_params(Checker *c, Scope *scope, AstNode *_params, bool *is_variadic_) {
  860. if (_params == NULL) {
  861. return NULL;
  862. }
  863. ast_node(field_list, FieldList, _params);
  864. Array<AstNode *> params = field_list->list;
  865. if (params.count == 0) {
  866. return NULL;
  867. }
  868. isize variable_count = 0;
  869. for_array(i, params) {
  870. AstNode *field = params[i];
  871. if (ast_node_expect(field, AstNode_Field)) {
  872. ast_node(f, Field, field);
  873. variable_count += gb_max(f->names.count, 1);
  874. }
  875. }
  876. bool is_variadic = false;
  877. Entity **variables = gb_alloc_array(c->allocator, Entity *, variable_count);
  878. isize variable_index = 0;
  879. for_array(i, params) {
  880. AstNode *param = params[i];
  881. if (param->kind != AstNode_Field) {
  882. continue;
  883. }
  884. ast_node(p, Field, param);
  885. AstNode *type_expr = p->type;
  886. Type *type = NULL;
  887. AstNode *default_value = p->default_value;
  888. ExactValue value = {};
  889. bool default_is_nil = false;
  890. if (type_expr == NULL) {
  891. Operand o = {};
  892. check_expr(c, &o, default_value);
  893. if (is_operand_nil(o)) {
  894. default_is_nil = true;
  895. } else if (o.mode != Addressing_Constant) {
  896. error_node(default_value, "Default parameter must be a constant");
  897. } else {
  898. value = o.value;
  899. }
  900. type = default_type(o.type);
  901. } else {
  902. if (type_expr->kind == AstNode_Ellipsis) {
  903. type_expr = type_expr->Ellipsis.expr;
  904. if (i+1 == params.count) {
  905. is_variadic = true;
  906. } else {
  907. error_node(param, "Invalid AST: Invalid variadic parameter");
  908. }
  909. }
  910. type = check_type(c, type_expr);
  911. if (default_value != NULL) {
  912. Operand o = {};
  913. check_expr_with_type_hint(c, &o, default_value, type);
  914. if (is_operand_nil(o)) {
  915. default_is_nil = true;
  916. } else if (o.mode != Addressing_Constant) {
  917. error_node(default_value, "Default parameter must be a constant");
  918. } else {
  919. value = o.value;
  920. }
  921. check_is_assignable_to(c, &o, type);
  922. }
  923. }
  924. if (type == NULL) {
  925. error_node(params[i], "Invalid parameter type");
  926. type = t_invalid;
  927. }
  928. if (is_type_untyped(type)) {
  929. error_node(params[i], "Cannot determine parameter type from a nil");
  930. type = t_invalid;
  931. }
  932. if (p->flags&FieldFlag_no_alias) {
  933. if (!is_type_pointer(type)) {
  934. error_node(params[i], "`no_alias` can only be applied to fields of pointer type");
  935. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  936. }
  937. }
  938. for_array(j, p->names) {
  939. AstNode *name = p->names[j];
  940. if (ast_node_expect(name, AstNode_Ident)) {
  941. Entity *param = make_entity_param(c->allocator, scope, name->Ident, type,
  942. (p->flags&FieldFlag_using) != 0, false);
  943. if (p->flags&FieldFlag_no_alias) {
  944. param->flags |= EntityFlag_NoAlias;
  945. }
  946. param->Variable.default_value = value;
  947. param->Variable.default_is_nil = default_is_nil;
  948. add_entity(c, scope, name, param);
  949. variables[variable_index++] = param;
  950. }
  951. }
  952. }
  953. variable_count = variable_index;
  954. if (is_variadic) {
  955. GB_ASSERT(params.count > 0);
  956. // NOTE(bill): Change last variadic parameter to be a slice
  957. // Custom Calling convention for variadic parameters
  958. Entity *end = variables[variable_count-1];
  959. end->type = make_type_slice(c->allocator, end->type);
  960. end->flags |= EntityFlag_Ellipsis;
  961. }
  962. Type *tuple = make_type_tuple(c->allocator);
  963. tuple->Tuple.variables = variables;
  964. tuple->Tuple.variable_count = variable_count;
  965. if (is_variadic_) *is_variadic_ = is_variadic;
  966. return tuple;
  967. }
  968. Type *check_get_results(Checker *c, Scope *scope, AstNode *_results) {
  969. if (_results == NULL) {
  970. return NULL;
  971. }
  972. ast_node(field_list, FieldList, _results);
  973. Array<AstNode *> results = field_list->list;
  974. if (results.count == 0) {
  975. return NULL;
  976. }
  977. Type *tuple = make_type_tuple(c->allocator);
  978. isize variable_count = 0;
  979. for_array(i, results) {
  980. AstNode *field = results[i];
  981. if (ast_node_expect(field, AstNode_Field)) {
  982. ast_node(f, Field, field);
  983. variable_count += gb_max(f->names.count, 1);
  984. }
  985. }
  986. Entity **variables = gb_alloc_array(c->allocator, Entity *, variable_count);
  987. isize variable_index = 0;
  988. for_array(i, results) {
  989. ast_node(field, Field, results[i]);
  990. Type *type = check_type(c, field->type);
  991. if (field->names.count == 0) {
  992. Token token = ast_node_token(field->type);
  993. token.string = str_lit("");
  994. Entity *param = make_entity_param(c->allocator, scope, token, type, false, false);
  995. variables[variable_index++] = param;
  996. } else {
  997. for_array(j, field->names) {
  998. Token token = ast_node_token(field->type);
  999. token.string = str_lit("");
  1000. AstNode *name = field->names[j];
  1001. if (name->kind != AstNode_Ident) {
  1002. error_node(name, "Expected an identifer for as the field name");
  1003. } else {
  1004. token = name->Ident;
  1005. }
  1006. Entity *param = make_entity_param(c->allocator, scope, token, type, false, false);
  1007. variables[variable_index++] = param;
  1008. }
  1009. }
  1010. }
  1011. for (isize i = 0; i < variable_index; i++) {
  1012. String x = variables[i]->token.string;
  1013. if (x.len == 0 || x == "_") {
  1014. continue;
  1015. }
  1016. for (isize j = i+1; j < variable_index; j++) {
  1017. String y = variables[j]->token.string;
  1018. if (y.len == 0 || y == "_") {
  1019. continue;
  1020. }
  1021. if (x == y) {
  1022. error(variables[j]->token, "Duplicate return value name `%.*s`", LIT(y));
  1023. }
  1024. }
  1025. }
  1026. tuple->Tuple.variables = variables;
  1027. tuple->Tuple.variable_count = variable_index;
  1028. return tuple;
  1029. }
  1030. Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type) {
  1031. Type *new_type = original_type;
  1032. if (build_context.ODIN_OS == "windows") {
  1033. // NOTE(bill): Changing the passing parameter value type is to match C's ABI
  1034. // IMPORTANT TODO(bill): This only matches the ABI on MSVC at the moment
  1035. // SEE: https://msdn.microsoft.com/en-us/library/zthk2dkh.aspx
  1036. Type *bt = core_type(original_type);
  1037. switch (bt->kind) {
  1038. // Okay to pass by value
  1039. // Especially the only Odin types
  1040. case Type_Basic: break;
  1041. case Type_Pointer: break;
  1042. case Type_Proc: break; // NOTE(bill): Just a pointer
  1043. // Odin only types
  1044. case Type_Slice:
  1045. case Type_DynamicArray:
  1046. case Type_Map:
  1047. break;
  1048. // Odin specific
  1049. case Type_Array:
  1050. case Type_Vector:
  1051. // Could be in C too
  1052. case Type_Record: {
  1053. i64 align = type_align_of(a, original_type);
  1054. i64 size = type_size_of(a, original_type);
  1055. switch (8*size) {
  1056. case 8: new_type = t_u8; break;
  1057. case 16: new_type = t_u16; break;
  1058. case 32: new_type = t_u32; break;
  1059. case 64: new_type = t_u64; break;
  1060. default:
  1061. new_type = make_type_pointer(a, original_type);
  1062. break;
  1063. }
  1064. } break;
  1065. }
  1066. } else if (build_context.ODIN_OS == "linux") {
  1067. Type *bt = core_type(original_type);
  1068. switch (bt->kind) {
  1069. // Okay to pass by value
  1070. // Especially the only Odin types
  1071. case Type_Basic: break;
  1072. case Type_Pointer: break;
  1073. case Type_Proc: break; // NOTE(bill): Just a pointer
  1074. // Odin only types
  1075. case Type_Slice:
  1076. case Type_DynamicArray:
  1077. case Type_Map:
  1078. break;
  1079. // Odin specific
  1080. case Type_Array:
  1081. case Type_Vector:
  1082. // Could be in C too
  1083. case Type_Record: {
  1084. i64 align = type_align_of(a, original_type);
  1085. i64 size = type_size_of(a, original_type);
  1086. if (8*size > 16) {
  1087. new_type = make_type_pointer(a, original_type);
  1088. }
  1089. } break;
  1090. }
  1091. } else {
  1092. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  1093. // their architectures
  1094. }
  1095. return new_type;
  1096. }
  1097. Type *reduce_tuple_to_single_type(Type *original_type) {
  1098. if (original_type != NULL) {
  1099. Type *t = core_type(original_type);
  1100. if (t->kind == Type_Tuple && t->Tuple.variable_count == 1) {
  1101. return t->Tuple.variables[0]->type;
  1102. }
  1103. }
  1104. return original_type;
  1105. }
  1106. Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type) {
  1107. Type *new_type = original_type;
  1108. if (new_type == NULL) {
  1109. return NULL;
  1110. }
  1111. GB_ASSERT(is_type_tuple(original_type));
  1112. if (build_context.ODIN_OS == "windows") {
  1113. Type *bt = core_type(reduce_tuple_to_single_type(original_type));
  1114. // NOTE(bill): This is just reversed engineered from LLVM IR output
  1115. switch (bt->kind) {
  1116. // Okay to pass by value
  1117. // Especially the only Odin types
  1118. case Type_Pointer: break;
  1119. case Type_Proc: break; // NOTE(bill): Just a pointer
  1120. case Type_Basic: break;
  1121. default: {
  1122. i64 align = type_align_of(a, original_type);
  1123. i64 size = type_size_of(a, original_type);
  1124. switch (8*size) {
  1125. #if 1
  1126. case 8: new_type = t_u8; break;
  1127. case 16: new_type = t_u16; break;
  1128. case 32: new_type = t_u32; break;
  1129. case 64: new_type = t_u64; break;
  1130. #endif
  1131. }
  1132. } break;
  1133. }
  1134. } else if (build_context.ODIN_OS == "linux") {
  1135. } else {
  1136. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  1137. // their architectures
  1138. }
  1139. if (new_type != original_type) {
  1140. Type *tuple = make_type_tuple(a);
  1141. tuple->Tuple.variable_count = 1;
  1142. tuple->Tuple.variables = gb_alloc_array(a, Entity *, 1);
  1143. tuple->Tuple.variables[0] = make_entity_param(a, original_type->Tuple.variables[0]->scope, empty_token, new_type, false, false);
  1144. new_type = tuple;
  1145. }
  1146. // return reduce_tuple_to_single_type(new_type);
  1147. return new_type;
  1148. }
  1149. bool abi_compat_return_by_value(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type) {
  1150. if (abi_return_type == NULL) {
  1151. return false;
  1152. }
  1153. if (cc == ProcCC_Odin) {
  1154. return false;
  1155. }
  1156. if (build_context.ODIN_OS == "windows") {
  1157. i64 size = 8*type_size_of(a, abi_return_type);
  1158. switch (size) {
  1159. case 0:
  1160. case 8:
  1161. case 16:
  1162. case 32:
  1163. case 64:
  1164. return false;
  1165. default:
  1166. return true;
  1167. }
  1168. }
  1169. return false;
  1170. }
  1171. void check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node) {
  1172. ast_node(pt, ProcType, proc_type_node);
  1173. bool variadic = false;
  1174. Type *params = check_get_params(c, c->context.scope, pt->params, &variadic);
  1175. Type *results = check_get_results(c, c->context.scope, pt->results);
  1176. isize param_count = 0;
  1177. isize result_count = 0;
  1178. if (params) param_count = params ->Tuple.variable_count;
  1179. if (results) result_count = results->Tuple.variable_count;
  1180. type->Proc.scope = c->context.scope;
  1181. type->Proc.params = params;
  1182. type->Proc.param_count = param_count;
  1183. type->Proc.results = results;
  1184. type->Proc.result_count = result_count;
  1185. type->Proc.variadic = variadic;
  1186. type->Proc.calling_convention = pt->calling_convention;
  1187. type->Proc.abi_compat_params = gb_alloc_array(c->allocator, Type *, param_count);
  1188. for (isize i = 0; i < param_count; i++) {
  1189. Type *original_type = type->Proc.params->Tuple.variables[i]->type;
  1190. Type *new_type = type_to_abi_compat_param_type(c->allocator, original_type);
  1191. type->Proc.abi_compat_params[i] = new_type;
  1192. }
  1193. // NOTE(bill): The types are the same
  1194. type->Proc.abi_compat_result_type = type_to_abi_compat_result_type(c->allocator, type->Proc.results);
  1195. type->Proc.return_by_pointer = abi_compat_return_by_value(c->allocator, pt->calling_convention, type->Proc.abi_compat_result_type);
  1196. }
  1197. Entity *check_ident(Checker *c, Operand *o, AstNode *n, Type *named_type, Type *type_hint, bool allow_import_name) {
  1198. GB_ASSERT(n->kind == AstNode_Ident);
  1199. o->mode = Addressing_Invalid;
  1200. o->expr = n;
  1201. String name = n->Ident.string;
  1202. Entity *e = scope_lookup_entity(c->context.scope, name);
  1203. if (e == NULL) {
  1204. if (name == "_") {
  1205. error(n->Ident, "`_` cannot be used as a value type");
  1206. } else {
  1207. error(n->Ident, "Undeclared name: %.*s", LIT(name));
  1208. }
  1209. o->type = t_invalid;
  1210. o->mode = Addressing_Invalid;
  1211. if (named_type != NULL) {
  1212. set_base_type(named_type, t_invalid);
  1213. }
  1214. return NULL;
  1215. }
  1216. if (e->parent_proc_decl != NULL &&
  1217. e->parent_proc_decl != c->context.curr_proc_decl) {
  1218. if (e->kind == Entity_Variable) {
  1219. error(n->Ident, "Nested procedures do not capture its parent's variables: %.*s", LIT(name));
  1220. return NULL;
  1221. } else if (e->kind == Entity_Label) {
  1222. error(n->Ident, "Nested procedures do not capture its parent's labels: %.*s", LIT(name));
  1223. return NULL;
  1224. }
  1225. }
  1226. bool is_overloaded = false;
  1227. isize overload_count = 0;
  1228. HashKey key = hash_string(name);
  1229. if (e->kind == Entity_Procedure) {
  1230. // NOTE(bill): Overloads are only allowed with the same scope
  1231. Scope *s = e->scope;
  1232. overload_count = multi_map_count(&s->elements, key);
  1233. if (overload_count > 1) {
  1234. is_overloaded = true;
  1235. }
  1236. }
  1237. if (is_overloaded) {
  1238. Scope *s = e->scope;
  1239. bool skip = false;
  1240. Entity **procs = gb_alloc_array(heap_allocator(), Entity *, overload_count);
  1241. multi_map_get_all(&s->elements, key, procs);
  1242. if (type_hint != NULL) {
  1243. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  1244. // NOTE(bill): These should be done
  1245. for (isize i = 0; i < overload_count; i++) {
  1246. Type *t = base_type(procs[i]->type);
  1247. if (t == t_invalid) {
  1248. continue;
  1249. }
  1250. Operand x = {};
  1251. x.mode = Addressing_Value;
  1252. x.type = t;
  1253. if (check_is_assignable_to(c, &x, type_hint)) {
  1254. e = procs[i];
  1255. add_entity_use(c, n, e);
  1256. skip = true;
  1257. break;
  1258. }
  1259. }
  1260. gb_temp_arena_memory_end(tmp);
  1261. }
  1262. if (!skip) {
  1263. o->mode = Addressing_Overload;
  1264. o->type = t_invalid;
  1265. o->overload_count = overload_count;
  1266. o->overload_entities = procs;
  1267. return NULL;
  1268. }
  1269. gb_free(heap_allocator(), procs);
  1270. }
  1271. add_entity_use(c, n, e);
  1272. check_entity_decl(c, e, NULL, named_type);
  1273. if (e->type == NULL) {
  1274. compiler_error("How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(name));
  1275. // return NULL;
  1276. }
  1277. e->flags |= EntityFlag_Used;
  1278. Type *type = e->type;
  1279. switch (e->kind) {
  1280. case Entity_Constant:
  1281. if (type == t_invalid) {
  1282. o->type = t_invalid;
  1283. return e;
  1284. }
  1285. o->value = e->Constant.value;
  1286. if (o->value.kind == ExactValue_Invalid) {
  1287. return e;
  1288. }
  1289. o->mode = Addressing_Constant;
  1290. break;
  1291. case Entity_Variable:
  1292. e->flags |= EntityFlag_Used;
  1293. if (type == t_invalid) {
  1294. o->type = t_invalid;
  1295. return e;
  1296. }
  1297. o->mode = Addressing_Variable;
  1298. if (e->flags & EntityFlag_Value) {
  1299. o->mode = Addressing_Value;
  1300. }
  1301. if (e->Variable.is_immutable) {
  1302. o->mode = Addressing_Immutable;
  1303. }
  1304. break;
  1305. case Entity_Procedure:
  1306. o->mode = Addressing_Value;
  1307. break;
  1308. case Entity_Builtin:
  1309. o->builtin_id = cast(BuiltinProcId)e->Builtin.id;
  1310. o->mode = Addressing_Builtin;
  1311. break;
  1312. case Entity_TypeName:
  1313. o->mode = Addressing_Type;
  1314. break;
  1315. case Entity_ImportName:
  1316. if (!allow_import_name) {
  1317. error_node(n, "Use of import `%.*s` not in selector", LIT(name));
  1318. }
  1319. return e;
  1320. case Entity_LibraryName:
  1321. error_node(n, "Use of library `%.*s` not in #foreign tag", LIT(name));
  1322. return e;
  1323. case Entity_Label:
  1324. o->mode = Addressing_NoValue;
  1325. break;
  1326. case Entity_Nil:
  1327. o->mode = Addressing_Value;
  1328. break;
  1329. default:
  1330. compiler_error("Unknown EntityKind %.*s", LIT(entity_strings[e->kind]));
  1331. break;
  1332. }
  1333. o->type = type;
  1334. return e;
  1335. }
  1336. i64 check_array_or_map_count(Checker *c, AstNode *e, bool is_map) {
  1337. if (e == NULL) {
  1338. return 0;
  1339. }
  1340. Operand o = {};
  1341. if (e->kind == AstNode_UnaryExpr &&
  1342. e->UnaryExpr.op.kind == Token_Ellipsis) {
  1343. return -1;
  1344. }
  1345. check_expr(c, &o, e);
  1346. if (o.mode != Addressing_Constant) {
  1347. if (o.mode != Addressing_Invalid) {
  1348. if (is_map) {
  1349. error_node(e, "Fixed map count must be a constant");
  1350. } else {
  1351. error_node(e, "Array count must be a constant");
  1352. }
  1353. }
  1354. return 0;
  1355. }
  1356. Type *type = base_type(o.type);
  1357. if (is_type_untyped(type) || is_type_integer(type)) {
  1358. if (o.value.kind == ExactValue_Integer) {
  1359. i64 count = i128_to_i64(o.value.value_integer);
  1360. if (is_map) {
  1361. if (count > 0) {
  1362. return count;
  1363. }
  1364. error_node(e, "Invalid fixed map count");
  1365. } else {
  1366. if (count >= 0) {
  1367. return count;
  1368. }
  1369. error_node(e, "Invalid array count");
  1370. }
  1371. return 0;
  1372. }
  1373. }
  1374. if (is_map) {
  1375. error_node(e, "Fixed map count must be an integer");
  1376. } else {
  1377. error_node(e, "Array count must be an integer");
  1378. }
  1379. return 0;
  1380. }
  1381. Type *make_optional_ok_type(gbAllocator a, Type *value) {
  1382. bool typed = true;
  1383. Type *t = make_type_tuple(a);
  1384. t->Tuple.variables = gb_alloc_array(a, Entity *, 2);
  1385. t->Tuple.variable_count = 2;
  1386. t->Tuple.variables[0] = make_entity_field(a, NULL, blank_token, value, false, 0);
  1387. t->Tuple.variables[1] = make_entity_field(a, NULL, blank_token, typed ? t_bool : t_untyped_bool, false, 1);
  1388. return t;
  1389. }
  1390. void check_map_type(Checker *c, Type *type, AstNode *node) {
  1391. GB_ASSERT(type->kind == Type_Map);
  1392. ast_node(mt, MapType, node);
  1393. i64 count = check_array_or_map_count(c, mt->count, true);
  1394. Type *key = check_type(c, mt->key);
  1395. Type *value = check_type(c, mt->value);
  1396. if (!is_type_valid_for_keys(key)) {
  1397. if (is_type_boolean(key)) {
  1398. error_node(node, "A boolean cannot be used as a key for a map");
  1399. } else {
  1400. gbString str = type_to_string(key);
  1401. error_node(node, "Invalid type of a key for a map, got `%s`", str);
  1402. gb_string_free(str);
  1403. }
  1404. }
  1405. if (count > 0) {
  1406. count = 0;
  1407. error_node(node, "Fixed map types are not yet implemented");
  1408. }
  1409. type->Map.count = count;
  1410. type->Map.key = key;
  1411. type->Map.value = value;
  1412. gbAllocator a = c->allocator;
  1413. {
  1414. // NOTE(bill): The preload types may have not been set yet
  1415. if (t_map_key == NULL) {
  1416. init_preload(c);
  1417. }
  1418. GB_ASSERT(t_map_key != NULL);
  1419. Type *entry_type = make_type_struct(a);
  1420. /*
  1421. struct {
  1422. hash: Map_Key,
  1423. next: int,
  1424. key: Key_Type,
  1425. value: Value_Type,
  1426. }
  1427. */
  1428. AstNode *dummy_node = gb_alloc_item(a, AstNode);
  1429. dummy_node->kind = AstNode_Invalid;
  1430. check_open_scope(c, dummy_node);
  1431. isize field_count = 3;
  1432. Entity **fields = gb_alloc_array(a, Entity *, field_count);
  1433. fields[0] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("key")), t_map_key, false, 0);
  1434. fields[1] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("next")), t_int, false, 1);
  1435. fields[2] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("value")), value, false, 2);
  1436. check_close_scope(c);
  1437. entry_type->Record.fields = fields;
  1438. entry_type->Record.fields_in_src_order = fields;
  1439. entry_type->Record.field_count = field_count;
  1440. type_set_offsets(a, entry_type);
  1441. type->Map.entry_type = entry_type;
  1442. }
  1443. {
  1444. Type *generated_struct_type = make_type_struct(a);
  1445. /*
  1446. struct {
  1447. hashes: [dynamic]int,
  1448. entries; [dynamic]Entry_Type,
  1449. }
  1450. */
  1451. AstNode *dummy_node = gb_alloc_item(a, AstNode);
  1452. dummy_node->kind = AstNode_Invalid;
  1453. check_open_scope(c, dummy_node);
  1454. Type *hashes_type = make_type_dynamic_array(a, t_int);
  1455. Type *entries_type = make_type_dynamic_array(a, type->Map.entry_type);
  1456. isize field_count = 2;
  1457. Entity **fields = gb_alloc_array(a, Entity *, field_count);
  1458. fields[0] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("hashes")), hashes_type, false, 0);
  1459. fields[1] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("entries")), entries_type, false, 1);
  1460. check_close_scope(c);
  1461. generated_struct_type->Record.fields = fields;
  1462. generated_struct_type->Record.fields_in_src_order = fields;
  1463. generated_struct_type->Record.field_count = field_count;
  1464. type_set_offsets(a, generated_struct_type);
  1465. type->Map.generated_struct_type = generated_struct_type;
  1466. }
  1467. type->Map.lookup_result_type = make_optional_ok_type(a, value);
  1468. // error_node(node, "`map` types are not yet implemented");
  1469. }
  1470. bool check_type_internal(Checker *c, AstNode *e, Type **type, Type *named_type) {
  1471. GB_ASSERT_NOT_NULL(type);
  1472. if (e == NULL) {
  1473. *type = t_invalid;
  1474. return true;
  1475. }
  1476. switch (e->kind) {
  1477. case_ast_node(i, Ident, e);
  1478. Operand o = {};
  1479. check_ident(c, &o, e, named_type, NULL, false);
  1480. switch (o.mode) {
  1481. case Addressing_Invalid:
  1482. break;
  1483. case Addressing_Type: {
  1484. *type = o.type;
  1485. return true;
  1486. } break;
  1487. case Addressing_NoValue: {
  1488. gbString err_str = expr_to_string(e);
  1489. error_node(e, "`%s` used as a type", err_str);
  1490. gb_string_free(err_str);
  1491. } break;
  1492. default: {
  1493. gbString err_str = expr_to_string(e);
  1494. error_node(e, "`%s` used as a type when not a type", err_str);
  1495. gb_string_free(err_str);
  1496. } break;
  1497. }
  1498. case_end;
  1499. case_ast_node(se, SelectorExpr, e);
  1500. Operand o = {};
  1501. check_selector(c, &o, e, NULL);
  1502. switch (o.mode) {
  1503. case Addressing_Invalid:
  1504. break;
  1505. case Addressing_Type:
  1506. GB_ASSERT(o.type != NULL);
  1507. *type = o.type;
  1508. return true;
  1509. case Addressing_NoValue: {
  1510. gbString err_str = expr_to_string(e);
  1511. error_node(e, "`%s` used as a type", err_str);
  1512. gb_string_free(err_str);
  1513. } break;
  1514. default: {
  1515. gbString err_str = expr_to_string(e);
  1516. error_node(e, "`%s` is not a type", err_str);
  1517. gb_string_free(err_str);
  1518. } break;
  1519. }
  1520. case_end;
  1521. case_ast_node(pe, ParenExpr, e);
  1522. *type = check_type(c, pe->expr, named_type);
  1523. return true;
  1524. case_end;
  1525. case_ast_node(ue, UnaryExpr, e);
  1526. if (ue->op.kind == Token_Pointer) {
  1527. *type = make_type_pointer(c->allocator, check_type(c, ue->expr));
  1528. return true;
  1529. } /* else if (ue->op.kind == Token_Maybe) {
  1530. *type = make_type_maybe(c->allocator, check_type(c, ue->expr));
  1531. return true;
  1532. } */
  1533. case_end;
  1534. case_ast_node(ht, HelperType, e);
  1535. *type = check_type(c, ht->type);
  1536. return true;
  1537. case_end;
  1538. case_ast_node(pt, PointerType, e);
  1539. Type *elem = check_type(c, pt->type);
  1540. i64 esz = type_size_of(c->allocator, elem);
  1541. *type = make_type_pointer(c->allocator, elem);
  1542. return true;
  1543. case_end;
  1544. case_ast_node(at, AtomicType, e);
  1545. Type *elem = check_type(c, at->type);
  1546. i64 esz = type_size_of(c->allocator, elem);
  1547. *type = make_type_atomic(c->allocator, elem);
  1548. return true;
  1549. case_end;
  1550. case_ast_node(at, ArrayType, e);
  1551. if (at->count != NULL) {
  1552. Type *elem = check_type(c, at->elem, NULL);
  1553. i64 count = check_array_or_map_count(c, at->count, false);
  1554. if (count < 0) {
  1555. error_node(at->count, ".. can only be used in conjuction with compound literals");
  1556. count = 0;
  1557. }
  1558. #if 0
  1559. i64 esz = type_size_of(c->allocator, elem);
  1560. if (esz == 0) {
  1561. gbString str = type_to_string(elem);
  1562. error_node(at->elem, "Zero sized element type `%s` is not allowed", str);
  1563. gb_string_free(str);
  1564. }
  1565. #endif
  1566. *type = make_type_array(c->allocator, elem, count);
  1567. } else {
  1568. Type *elem = check_type(c, at->elem);
  1569. #if 0
  1570. i64 esz = type_size_of(c->allocator, elem);
  1571. if (esz == 0) {
  1572. gbString str = type_to_string(elem);
  1573. error_node(at->elem, "Zero sized element type `%s` is not allowed", str);
  1574. gb_string_free(str);
  1575. }
  1576. #endif
  1577. *type = make_type_slice(c->allocator, elem);
  1578. }
  1579. return true;
  1580. case_end;
  1581. case_ast_node(dat, DynamicArrayType, e);
  1582. Type *elem = check_type(c, dat->elem);
  1583. i64 esz = type_size_of(c->allocator, elem);
  1584. #if 0
  1585. if (esz == 0) {
  1586. gbString str = type_to_string(elem);
  1587. error_node(dat->elem, "Zero sized element type `%s` is not allowed", str);
  1588. gb_string_free(str);
  1589. }
  1590. #endif
  1591. *type = make_type_dynamic_array(c->allocator, elem);
  1592. return true;
  1593. case_end;
  1594. case_ast_node(vt, VectorType, e);
  1595. Type *elem = check_type(c, vt->elem);
  1596. Type *be = base_type(elem);
  1597. i64 count = check_array_or_map_count(c, vt->count, false);
  1598. if (is_type_vector(be) || (!is_type_boolean(be) && !is_type_numeric(be))) {
  1599. gbString err_str = type_to_string(elem);
  1600. error_node(vt->elem, "Vector element type must be numerical or a boolean, got `%s`", err_str);
  1601. gb_string_free(err_str);
  1602. }
  1603. *type = make_type_vector(c->allocator, elem, count);
  1604. return true;
  1605. case_end;
  1606. case_ast_node(st, StructType, e);
  1607. *type = make_type_struct(c->allocator);
  1608. set_base_type(named_type, *type);
  1609. check_open_scope(c, e);
  1610. check_struct_type(c, *type, e);
  1611. check_close_scope(c);
  1612. (*type)->Record.node = e;
  1613. return true;
  1614. case_end;
  1615. case_ast_node(ut, UnionType, e);
  1616. *type = make_type_union(c->allocator);
  1617. set_base_type(named_type, *type);
  1618. check_open_scope(c, e);
  1619. check_union_type(c, *type, e);
  1620. check_close_scope(c);
  1621. (*type)->Record.node = e;
  1622. return true;
  1623. case_end;
  1624. case_ast_node(rut, RawUnionType, e);
  1625. *type = make_type_raw_union(c->allocator);
  1626. set_base_type(named_type, *type);
  1627. check_open_scope(c, e);
  1628. check_raw_union_type(c, *type, e);
  1629. check_close_scope(c);
  1630. (*type)->Record.node = e;
  1631. return true;
  1632. case_end;
  1633. case_ast_node(et, EnumType, e);
  1634. *type = make_type_enum(c->allocator);
  1635. set_base_type(named_type, *type);
  1636. check_open_scope(c, e);
  1637. check_enum_type(c, *type, named_type, e);
  1638. check_close_scope(c);
  1639. (*type)->Record.node = e;
  1640. return true;
  1641. case_end;
  1642. case_ast_node(et, BitFieldType, e);
  1643. *type = make_type_bit_field(c->allocator);
  1644. set_base_type(named_type, *type);
  1645. check_open_scope(c, e);
  1646. check_bit_field_type(c, *type, named_type, e);
  1647. check_close_scope(c);
  1648. return true;
  1649. case_end;
  1650. case_ast_node(pt, ProcType, e);
  1651. *type = alloc_type(c->allocator, Type_Proc);
  1652. set_base_type(named_type, *type);
  1653. check_open_scope(c, e);
  1654. check_procedure_type(c, *type, e);
  1655. check_close_scope(c);
  1656. return true;
  1657. case_end;
  1658. case_ast_node(mt, MapType, e);
  1659. *type = alloc_type(c->allocator, Type_Map);
  1660. set_base_type(named_type, *type);
  1661. check_map_type(c, *type, e);
  1662. return true;
  1663. case_end;
  1664. case_ast_node(ce, CallExpr, e);
  1665. Operand o = {};
  1666. check_expr_or_type(c, &o, e);
  1667. if (o.mode == Addressing_Type) {
  1668. *type = o.type;
  1669. return true;
  1670. }
  1671. case_end;
  1672. }
  1673. *type = t_invalid;
  1674. return false;
  1675. }
  1676. Type *check_type(Checker *c, AstNode *e, Type *named_type) {
  1677. Type *type = NULL;
  1678. bool ok = check_type_internal(c, e, &type, named_type);
  1679. if (!ok) {
  1680. gbString err_str = expr_to_string(e);
  1681. error_node(e, "`%s` is not a type", err_str);
  1682. gb_string_free(err_str);
  1683. type = t_invalid;
  1684. }
  1685. if (type == NULL) {
  1686. type = t_invalid;
  1687. }
  1688. if (type->kind == Type_Named) {
  1689. if (type->Named.base == NULL) {
  1690. gbString name = type_to_string(type);
  1691. error_node(e, "Invalid type definition of %s", name);
  1692. gb_string_free(name);
  1693. type->Named.base = t_invalid;
  1694. }
  1695. }
  1696. if (is_type_typed(type)) {
  1697. add_type_and_value(&c->info, e, Addressing_Type, type, empty_exact_value);
  1698. } else {
  1699. gbString name = type_to_string(type);
  1700. error_node(e, "Invalid type definition of %s", name);
  1701. gb_string_free(name);
  1702. type = t_invalid;
  1703. }
  1704. set_base_type(named_type, type);
  1705. return type;
  1706. }
  1707. bool check_unary_op(Checker *c, Operand *o, Token op) {
  1708. if (o->type == NULL) {
  1709. gbString str = expr_to_string(o->expr);
  1710. error_node(o->expr, "Expression has no value `%s`", str);
  1711. gb_string_free(str);
  1712. return false;
  1713. }
  1714. // TODO(bill): Handle errors correctly
  1715. Type *type = base_type(base_vector_type(o->type));
  1716. gbString str = NULL;
  1717. switch (op.kind) {
  1718. case Token_Add:
  1719. case Token_Sub:
  1720. if (!is_type_numeric(type)) {
  1721. str = expr_to_string(o->expr);
  1722. error(op, "Operator `%.*s` is not allowed with `%s`", LIT(op.string), str);
  1723. gb_string_free(str);
  1724. }
  1725. break;
  1726. case Token_Xor:
  1727. if (!is_type_integer(type) && !is_type_boolean(type)) {
  1728. error(op, "Operator `%.*s` is only allowed with integers or booleans", LIT(op.string));
  1729. }
  1730. break;
  1731. case Token_Not:
  1732. if (!is_type_boolean(type)) {
  1733. str = expr_to_string(o->expr);
  1734. error(op, "Operator `%.*s` is only allowed on boolean expression", LIT(op.string));
  1735. gb_string_free(str);
  1736. }
  1737. break;
  1738. default:
  1739. error(op, "Unknown operator `%.*s`", LIT(op.string));
  1740. return false;
  1741. }
  1742. return true;
  1743. }
  1744. bool check_binary_op(Checker *c, Operand *o, Token op) {
  1745. // TODO(bill): Handle errors correctly
  1746. Type *type = base_type(base_vector_type(o->type));
  1747. switch (op.kind) {
  1748. case Token_Sub:
  1749. case Token_SubEq:
  1750. if (!is_type_numeric(type) && !is_type_pointer(type)) {
  1751. error(op, "Operator `%.*s` is only allowed with numeric or pointer expressions", LIT(op.string));
  1752. return false;
  1753. }
  1754. if (is_type_pointer(type)) {
  1755. o->type = t_int;
  1756. }
  1757. if (base_type(type) == t_rawptr) {
  1758. gbString str = type_to_string(type);
  1759. error_node(o->expr, "Invalid pointer type for pointer arithmetic: `%s`", str);
  1760. gb_string_free(str);
  1761. return false;
  1762. }
  1763. break;
  1764. case Token_Add:
  1765. case Token_Mul:
  1766. case Token_Quo:
  1767. case Token_AddEq:
  1768. case Token_MulEq:
  1769. case Token_QuoEq:
  1770. if (!is_type_numeric(type)) {
  1771. error(op, "Operator `%.*s` is only allowed with numeric expressions", LIT(op.string));
  1772. return false;
  1773. }
  1774. break;
  1775. case Token_And:
  1776. case Token_Or:
  1777. case Token_AndEq:
  1778. case Token_OrEq:
  1779. case Token_Xor:
  1780. case Token_XorEq:
  1781. if (!is_type_integer(type) && !is_type_boolean(type)) {
  1782. error(op, "Operator `%.*s` is only allowed with integers or booleans", LIT(op.string));
  1783. return false;
  1784. }
  1785. break;
  1786. case Token_Mod:
  1787. case Token_ModMod:
  1788. case Token_AndNot:
  1789. case Token_ModEq:
  1790. case Token_ModModEq:
  1791. case Token_AndNotEq:
  1792. if (!is_type_integer(type)) {
  1793. error(op, "Operator `%.*s` is only allowed with integers", LIT(op.string));
  1794. return false;
  1795. }
  1796. break;
  1797. case Token_CmpAnd:
  1798. case Token_CmpOr:
  1799. case Token_CmpAndEq:
  1800. case Token_CmpOrEq:
  1801. if (!is_type_boolean(type)) {
  1802. error(op, "Operator `%.*s` is only allowed with boolean expressions", LIT(op.string));
  1803. return false;
  1804. }
  1805. break;
  1806. default:
  1807. error(op, "Unknown operator `%.*s`", LIT(op.string));
  1808. return false;
  1809. }
  1810. return true;
  1811. }
  1812. bool check_representable_as_constant(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value) {
  1813. if (in_value.kind == ExactValue_Invalid) {
  1814. // NOTE(bill): There's already been an error
  1815. return true;
  1816. }
  1817. type = core_type(type);
  1818. if (is_type_boolean(type)) {
  1819. return in_value.kind == ExactValue_Bool;
  1820. } else if (is_type_string(type)) {
  1821. return in_value.kind == ExactValue_String;
  1822. } else if (is_type_integer(type) || is_type_rune(type)) {
  1823. ExactValue v = exact_value_to_integer(in_value);
  1824. if (v.kind != ExactValue_Integer) {
  1825. return false;
  1826. }
  1827. if (out_value) *out_value = v;
  1828. if (is_type_untyped(type)) {
  1829. return true;
  1830. }
  1831. i128 i = v.value_integer;
  1832. u128 u = *cast(u128 *)&i;
  1833. i64 s = 8*type_size_of(c->allocator, type);
  1834. u128 umax = U128_NEG_ONE;
  1835. if (s < 128) {
  1836. umax = u128_sub(u128_shl(U128_ONE, s), U128_ONE);
  1837. } else {
  1838. // IMPORTANT TODO(bill): I NEED A PROPER BIG NUMBER LIBRARY THAT CAN SUPPORT 128 bit floats
  1839. s = 128;
  1840. }
  1841. i128 imax = i128_shl(I128_ONE, s-1ll);
  1842. switch (type->Basic.kind) {
  1843. case Basic_rune:
  1844. case Basic_i8:
  1845. case Basic_i16:
  1846. case Basic_i32:
  1847. case Basic_i64:
  1848. case Basic_i128:
  1849. case Basic_int:
  1850. return i128_le(i128_neg(imax), i) && i128_le(i, i128_sub(imax, I128_ONE));
  1851. case Basic_u8:
  1852. case Basic_u16:
  1853. case Basic_u32:
  1854. case Basic_u64:
  1855. case Basic_u128:
  1856. case Basic_uint:
  1857. return !(u128_lt(u, U128_ZERO) || u128_gt(u, umax));
  1858. case Basic_UntypedInteger:
  1859. return true;
  1860. default: GB_PANIC("Compiler error: Unknown integer type!"); break;
  1861. }
  1862. } else if (is_type_float(type)) {
  1863. ExactValue v = exact_value_to_float(in_value);
  1864. if (v.kind != ExactValue_Float) {
  1865. return false;
  1866. }
  1867. if (out_value) *out_value = v;
  1868. switch (type->Basic.kind) {
  1869. // case Basic_f16:
  1870. case Basic_f32:
  1871. case Basic_f64:
  1872. return true;
  1873. case Basic_UntypedFloat:
  1874. return true;
  1875. }
  1876. } else if (is_type_complex(type)) {
  1877. ExactValue v = exact_value_to_complex(in_value);
  1878. if (v.kind != ExactValue_Complex) {
  1879. return false;
  1880. }
  1881. switch (type->Basic.kind) {
  1882. case Basic_complex64:
  1883. case Basic_complex128: {
  1884. ExactValue real = exact_value_real(v);
  1885. ExactValue imag = exact_value_imag(v);
  1886. if (real.kind != ExactValue_Invalid &&
  1887. imag.kind != ExactValue_Invalid) {
  1888. if (out_value) *out_value = exact_binary_operator_value(Token_Add, real, exact_value_make_imag(imag));
  1889. return true;
  1890. }
  1891. } break;
  1892. case Basic_UntypedComplex:
  1893. return true;
  1894. }
  1895. return false;
  1896. }else if (is_type_pointer(type)) {
  1897. if (in_value.kind == ExactValue_Pointer) {
  1898. return true;
  1899. }
  1900. if (in_value.kind == ExactValue_Integer) {
  1901. return false;
  1902. // return true;
  1903. }
  1904. if (out_value) *out_value = in_value;
  1905. }
  1906. return false;
  1907. }
  1908. void check_is_expressible(Checker *c, Operand *o, Type *type) {
  1909. GB_ASSERT(is_type_constant_type(type));
  1910. GB_ASSERT(o->mode == Addressing_Constant);
  1911. if (!check_representable_as_constant(c, o->value, type, &o->value)) {
  1912. gbString a = expr_to_string(o->expr);
  1913. gbString b = type_to_string(type);
  1914. if (is_type_numeric(o->type) && is_type_numeric(type)) {
  1915. if (!is_type_integer(o->type) && is_type_integer(type)) {
  1916. error_node(o->expr, "`%s` truncated to `%s`", a, b);
  1917. } else {
  1918. char buf[127] = {};
  1919. String str = {};
  1920. i128 i = o->value.value_integer;
  1921. if (is_type_unsigned(o->type)) {
  1922. str = u128_to_string(*cast(u128 *)&i, buf, gb_size_of(buf));
  1923. } else {
  1924. str = i128_to_string(i, buf, gb_size_of(buf));
  1925. }
  1926. error_node(o->expr, "`%s = %.*s` overflows `%s`", a, str, b);
  1927. }
  1928. } else {
  1929. error_node(o->expr, "Cannot convert `%s` to `%s`", a, b);
  1930. }
  1931. gb_string_free(b);
  1932. gb_string_free(a);
  1933. o->mode = Addressing_Invalid;
  1934. }
  1935. }
  1936. bool check_is_expr_vector_index(Checker *c, AstNode *expr) {
  1937. // HACK(bill): Handle this correctly. Maybe with a custom AddressingMode
  1938. expr = unparen_expr(expr);
  1939. if (expr->kind == AstNode_IndexExpr) {
  1940. ast_node(ie, IndexExpr, expr);
  1941. Type *t = type_deref(type_of_expr(&c->info, ie->expr));
  1942. if (t != NULL) {
  1943. return is_type_vector(t);
  1944. }
  1945. }
  1946. return false;
  1947. }
  1948. bool check_is_vector_elem(Checker *c, AstNode *expr) {
  1949. // HACK(bill): Handle this correctly. Maybe with a custom AddressingMode
  1950. expr = unparen_expr(expr);
  1951. if (expr->kind == AstNode_SelectorExpr) {
  1952. ast_node(se, SelectorExpr, expr);
  1953. Type *t = type_deref(type_of_expr(&c->info, se->expr));
  1954. if (t != NULL && is_type_vector(t)) {
  1955. return true;
  1956. }
  1957. }
  1958. return false;
  1959. }
  1960. bool check_is_not_addressable(Checker *c, Operand *o) {
  1961. if (o->mode != Addressing_Variable) {
  1962. return true;
  1963. }
  1964. if (is_type_bit_field_value(o->type)) {
  1965. return true;
  1966. }
  1967. if (check_is_expr_vector_index(c, o->expr)) {
  1968. return true;
  1969. }
  1970. if (check_is_vector_elem(c, o->expr)) {
  1971. return true;
  1972. }
  1973. return false;
  1974. }
  1975. void check_unary_expr(Checker *c, Operand *o, Token op, AstNode *node) {
  1976. switch (op.kind) {
  1977. case Token_And: { // Pointer address
  1978. if (o->mode == Addressing_Type) {
  1979. o->type = make_type_pointer(c->allocator, o->type);
  1980. return;
  1981. }
  1982. if (check_is_not_addressable(c, o)) {
  1983. if (ast_node_expect(node, AstNode_UnaryExpr)) {
  1984. ast_node(ue, UnaryExpr, node);
  1985. gbString str = expr_to_string(ue->expr);
  1986. error(op, "Cannot take the pointer address of `%s`", str);
  1987. gb_string_free(str);
  1988. }
  1989. o->mode = Addressing_Invalid;
  1990. return;
  1991. }
  1992. o->mode = Addressing_Value;
  1993. o->type = make_type_pointer(c->allocator, o->type);
  1994. return;
  1995. }
  1996. }
  1997. if (!check_unary_op(c, o, op)) {
  1998. o->mode = Addressing_Invalid;
  1999. return;
  2000. }
  2001. if (o->mode == Addressing_Constant && !is_type_vector(o->type)) {
  2002. Type *type = base_type(o->type);
  2003. if (!is_type_constant_type(o->type)) {
  2004. gbString xt = type_to_string(o->type);
  2005. gbString err_str = expr_to_string(node);
  2006. error(op, "Invalid type, `%s`, for constant unary expression `%s`", xt, err_str);
  2007. gb_string_free(err_str);
  2008. gb_string_free(xt);
  2009. o->mode = Addressing_Invalid;
  2010. return;
  2011. }
  2012. i32 precision = 0;
  2013. if (is_type_unsigned(type)) {
  2014. precision = cast(i32)(8 * type_size_of(c->allocator, type));
  2015. }
  2016. o->value = exact_unary_operator_value(op.kind, o->value, precision);
  2017. if (is_type_typed(type)) {
  2018. if (node != NULL) {
  2019. o->expr = node;
  2020. }
  2021. check_is_expressible(c, o, type);
  2022. }
  2023. return;
  2024. }
  2025. o->mode = Addressing_Value;
  2026. }
  2027. void check_comparison(Checker *c, Operand *x, Operand *y, TokenKind op) {
  2028. if (x->mode == Addressing_Type && y->mode == Addressing_Type) {
  2029. bool comp = are_types_identical(x->type, y->type);
  2030. switch (op) {
  2031. case Token_CmpEq: comp = comp; break;
  2032. case Token_NotEq: comp = !comp; break;
  2033. }
  2034. x->mode = Addressing_Constant;
  2035. x->type = t_untyped_bool;
  2036. x->value = exact_value_bool(comp);
  2037. return;
  2038. }
  2039. gbString err_str = NULL;
  2040. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  2041. if (check_is_assignable_to(c, x, y->type) ||
  2042. check_is_assignable_to(c, y, x->type)) {
  2043. Type *err_type = x->type;
  2044. bool defined = false;
  2045. switch (op) {
  2046. case Token_CmpEq:
  2047. case Token_NotEq:
  2048. defined = is_type_comparable(x->type) ||
  2049. (is_operand_nil(*x) && type_has_nil(y->type)) ||
  2050. (is_operand_nil(*y) && type_has_nil(x->type));
  2051. break;
  2052. case Token_Lt:
  2053. case Token_Gt:
  2054. case Token_LtEq:
  2055. case Token_GtEq: {
  2056. defined = is_type_ordered(x->type);
  2057. } break;
  2058. }
  2059. if (!defined) {
  2060. if (x->type == err_type && is_operand_nil(*x)) {
  2061. err_type = y->type;
  2062. }
  2063. gb_printf_err("%d %d\n", is_operand_nil(*x), type_has_nil(y->type));
  2064. gb_printf_err("%d %d\n", is_operand_nil(*y), type_has_nil(x->type));
  2065. gbString type_string = type_to_string(err_type);
  2066. err_str = gb_string_make(c->tmp_allocator,
  2067. gb_bprintf("operator `%.*s` not defined for type `%s`", LIT(token_strings[op]), type_string));
  2068. gb_string_free(type_string);
  2069. }
  2070. } else {
  2071. gbString xt = type_to_string(x->type);
  2072. gbString yt = type_to_string(y->type);
  2073. err_str = gb_string_make(c->tmp_allocator,
  2074. gb_bprintf("mismatched types `%s` and `%s`", xt, yt));
  2075. gb_string_free(yt);
  2076. gb_string_free(xt);
  2077. }
  2078. if (err_str != NULL) {
  2079. error_node(x->expr, "Cannot compare expression, %s", err_str);
  2080. x->type = t_untyped_bool;
  2081. } else {
  2082. if (x->mode == Addressing_Constant &&
  2083. y->mode == Addressing_Constant) {
  2084. x->value = exact_value_bool(compare_exact_values(op, x->value, y->value));
  2085. } else {
  2086. x->mode = Addressing_Value;
  2087. update_expr_type(c, x->expr, default_type(x->type), true);
  2088. update_expr_type(c, y->expr, default_type(y->type), true);
  2089. }
  2090. if (is_type_vector(base_type(y->type))) {
  2091. x->type = make_type_vector(c->allocator, t_bool, base_type(y->type)->Vector.count);
  2092. } else {
  2093. x->type = t_untyped_bool;
  2094. }
  2095. }
  2096. if (err_str != NULL) {
  2097. gb_string_free(err_str);
  2098. }
  2099. gb_temp_arena_memory_end(tmp);
  2100. }
  2101. void check_shift(Checker *c, Operand *x, Operand *y, AstNode *node) {
  2102. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  2103. ast_node(be, BinaryExpr, node);
  2104. ExactValue x_val = {};
  2105. if (x->mode == Addressing_Constant) {
  2106. x_val = exact_value_to_integer(x->value);
  2107. }
  2108. bool x_is_untyped = is_type_untyped(x->type);
  2109. if (!(is_type_integer(x->type) || (x_is_untyped && x_val.kind == ExactValue_Integer))) {
  2110. gbString err_str = expr_to_string(x->expr);
  2111. error_node(node, "Shifted operand `%s` must be an integer", err_str);
  2112. gb_string_free(err_str);
  2113. x->mode = Addressing_Invalid;
  2114. return;
  2115. }
  2116. if (is_type_unsigned(y->type)) {
  2117. } else if (is_type_untyped(y->type)) {
  2118. convert_to_typed(c, y, t_untyped_integer, 0);
  2119. if (y->mode == Addressing_Invalid) {
  2120. x->mode = Addressing_Invalid;
  2121. return;
  2122. }
  2123. } else {
  2124. gbString err_str = expr_to_string(y->expr);
  2125. error_node(node, "Shift amount `%s` must be an unsigned integer", err_str);
  2126. gb_string_free(err_str);
  2127. x->mode = Addressing_Invalid;
  2128. return;
  2129. }
  2130. if (x->mode == Addressing_Constant) {
  2131. if (y->mode == Addressing_Constant) {
  2132. ExactValue y_val = exact_value_to_integer(y->value);
  2133. if (y_val.kind != ExactValue_Integer) {
  2134. gbString err_str = expr_to_string(y->expr);
  2135. error_node(node, "Shift amount `%s` must be an unsigned integer", err_str);
  2136. gb_string_free(err_str);
  2137. x->mode = Addressing_Invalid;
  2138. return;
  2139. }
  2140. i64 amount = i128_to_i64(y_val.value_integer);
  2141. if (amount > 128) {
  2142. gbString err_str = expr_to_string(y->expr);
  2143. error_node(node, "Shift amount too large: `%s`", err_str);
  2144. gb_string_free(err_str);
  2145. x->mode = Addressing_Invalid;
  2146. return;
  2147. }
  2148. if (!is_type_integer(x->type)) {
  2149. // NOTE(bill): It could be an untyped float but still representable
  2150. // as an integer
  2151. x->type = t_untyped_integer;
  2152. }
  2153. x->value = exact_value_shift(be->op.kind, x_val, exact_value_i64(amount));
  2154. if (is_type_typed(x->type)) {
  2155. check_is_expressible(c, x, base_type(x->type));
  2156. }
  2157. return;
  2158. }
  2159. TokenPos pos = ast_node_token(x->expr).pos;
  2160. if (x_is_untyped) {
  2161. ExprInfo *info = map_get(&c->info.untyped, hash_pointer(x->expr));
  2162. if (info != NULL) {
  2163. info->is_lhs = true;
  2164. }
  2165. x->mode = Addressing_Value;
  2166. // x->value = x_val;
  2167. return;
  2168. }
  2169. }
  2170. if (y->mode == Addressing_Constant && i128_lt(y->value.value_integer, I128_ZERO)) {
  2171. gbString err_str = expr_to_string(y->expr);
  2172. error_node(node, "Shift amount cannot be negative: `%s`", err_str);
  2173. gb_string_free(err_str);
  2174. }
  2175. if (!is_type_integer(x->type)) {
  2176. gbString err_str = expr_to_string(y->expr);
  2177. error_node(node, "Shift operand `%s` must be an integer", err_str);
  2178. gb_string_free(err_str);
  2179. x->mode = Addressing_Invalid;
  2180. return;
  2181. }
  2182. x->mode = Addressing_Value;
  2183. }
  2184. String check_down_cast_name(Type *dst_, Type *src_) {
  2185. String result = {};
  2186. Type *dst = type_deref(dst_);
  2187. Type *src = type_deref(src_);
  2188. Type *dst_s = base_type(dst);
  2189. GB_ASSERT(is_type_struct(dst_s) || is_type_raw_union(dst_s));
  2190. for (isize i = 0; i < dst_s->Record.field_count; i++) {
  2191. Entity *f = dst_s->Record.fields[i];
  2192. GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field);
  2193. if (f->flags & EntityFlag_Using) {
  2194. if (are_types_identical(f->type, src_)) {
  2195. return f->token.string;
  2196. }
  2197. if (are_types_identical(type_deref(f->type), src_)) {
  2198. return f->token.string;
  2199. }
  2200. if (!is_type_pointer(f->type)) {
  2201. result = check_down_cast_name(f->type, src_);
  2202. if (result.len > 0) {
  2203. return result;
  2204. }
  2205. }
  2206. }
  2207. }
  2208. return result;
  2209. }
  2210. Operand check_ptr_addition(Checker *c, TokenKind op, Operand *ptr, Operand *offset, AstNode *node) {
  2211. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  2212. ast_node(be, BinaryExpr, node);
  2213. GB_ASSERT(is_type_pointer(ptr->type));
  2214. GB_ASSERT(is_type_integer(offset->type));
  2215. GB_ASSERT(op == Token_Add || op == Token_Sub);
  2216. Operand operand = {};
  2217. operand.mode = Addressing_Value;
  2218. operand.type = ptr->type;
  2219. operand.expr = node;
  2220. if (base_type(ptr->type) == t_rawptr) {
  2221. gbString str = type_to_string(ptr->type);
  2222. error_node(node, "Invalid pointer type for pointer arithmetic: `%s`", str);
  2223. gb_string_free(str);
  2224. operand.mode = Addressing_Invalid;
  2225. return operand;
  2226. }
  2227. Type *base_ptr = base_type(ptr->type); GB_ASSERT(base_ptr->kind == Type_Pointer);
  2228. Type *elem = base_ptr->Pointer.elem;
  2229. i64 elem_size = type_size_of(c->allocator, elem);
  2230. if (elem_size <= 0) {
  2231. gbString str = type_to_string(elem);
  2232. error_node(node, "Size of pointer's element type `%s` is zero and cannot be used for pointer arithmetic", str);
  2233. gb_string_free(str);
  2234. operand.mode = Addressing_Invalid;
  2235. return operand;
  2236. }
  2237. if (ptr->mode == Addressing_Constant && offset->mode == Addressing_Constant) {
  2238. i64 ptr_val = ptr->value.value_pointer;
  2239. i64 offset_val = i128_to_i64(exact_value_to_integer(offset->value).value_integer);
  2240. i64 new_ptr_val = ptr_val;
  2241. if (op == Token_Add) {
  2242. new_ptr_val += elem_size*offset_val;
  2243. } else {
  2244. new_ptr_val -= elem_size*offset_val;
  2245. }
  2246. operand.mode = Addressing_Constant;
  2247. operand.value = exact_value_pointer(new_ptr_val);
  2248. }
  2249. return operand;
  2250. }
  2251. bool check_is_castable_to(Checker *c, Operand *operand, Type *y) {
  2252. if (check_is_assignable_to(c, operand, y)) {
  2253. return true;
  2254. }
  2255. Type *x = operand->type;
  2256. Type *src = core_type(x);
  2257. Type *dst = core_type(y);
  2258. if (are_types_identical(src, dst)) {
  2259. return true;
  2260. }
  2261. if (dst->kind == Type_Array && src->kind == Type_Array) {
  2262. if (are_types_identical(dst->Array.elem, src->Array.elem)) {
  2263. return dst->Array.count == src->Array.count;
  2264. }
  2265. }
  2266. if (dst->kind == Type_Slice && src->kind == Type_Slice) {
  2267. return are_types_identical(dst->Slice.elem, src->Slice.elem);
  2268. }
  2269. // Cast between booleans and integers
  2270. if (is_type_boolean(src) || is_type_integer(src)) {
  2271. if (is_type_boolean(dst) || is_type_integer(dst)) {
  2272. return true;
  2273. }
  2274. }
  2275. // Cast between numbers
  2276. if (is_type_integer(src) || is_type_float(src)) {
  2277. if (is_type_integer(dst) || is_type_float(dst)) {
  2278. return true;
  2279. }
  2280. }
  2281. if (is_type_integer(src) && is_type_rune(dst)) {
  2282. return true;
  2283. }
  2284. if (is_type_rune(src) && is_type_integer(dst)) {
  2285. return true;
  2286. }
  2287. if (is_type_complex(src) && is_type_complex(dst)) {
  2288. return true;
  2289. }
  2290. if (is_type_bit_field_value(src) && is_type_integer(dst)) {
  2291. return true;
  2292. }
  2293. if (is_type_bit_field_value(src) && is_type_boolean(dst)) {
  2294. return src->BitFieldValue.bits == 1;
  2295. }
  2296. // Cast between pointers
  2297. if (is_type_pointer(src) && is_type_pointer(dst)) {
  2298. Type *s = base_type(type_deref(src));
  2299. if (is_type_union(s)) {
  2300. // NOTE(bill): Should the error be here?!
  2301. // NOTE(bill): This error should suppress the next casting error as it's at the same position
  2302. gbString xs = type_to_string(x);
  2303. gbString ys = type_to_string(y);
  2304. error_node(operand->expr, "Cannot cast from a union pointer `%s` to `%s`, try using `union_cast` or cast to a `rawptr`", xs, ys);
  2305. gb_string_free(ys);
  2306. gb_string_free(xs);
  2307. return false;
  2308. }
  2309. return true;
  2310. }
  2311. // (u)int <-> rawptr
  2312. if (is_type_int_or_uint(src) && is_type_rawptr(dst)) {
  2313. return true;
  2314. }
  2315. if (is_type_rawptr(src) && is_type_int_or_uint(dst)) {
  2316. return true;
  2317. }
  2318. // []byte/[]u8 <-> string
  2319. if (is_type_u8_slice(src) && is_type_string(dst)) {
  2320. return true;
  2321. }
  2322. if (is_type_string(src) && is_type_u8_slice(dst)) {
  2323. // if (is_type_typed(src)) {
  2324. return true;
  2325. // }
  2326. }
  2327. // proc <-> proc
  2328. if (is_type_proc(src) && is_type_proc(dst)) {
  2329. return true;
  2330. }
  2331. // proc -> rawptr
  2332. if (is_type_proc(src) && is_type_rawptr(dst)) {
  2333. return true;
  2334. }
  2335. // rawptr -> proc
  2336. if (is_type_rawptr(src) && is_type_proc(dst)) {
  2337. return true;
  2338. }
  2339. return false;
  2340. }
  2341. void check_cast(Checker *c, Operand *x, Type *type) {
  2342. bool is_const_expr = x->mode == Addressing_Constant;
  2343. bool can_convert = false;
  2344. Type *bt = base_type(type);
  2345. if (is_const_expr && is_type_constant_type(bt)) {
  2346. if (core_type(bt)->kind == Type_Basic) {
  2347. if (check_representable_as_constant(c, x->value, bt, &x->value)) {
  2348. can_convert = true;
  2349. } else if (is_type_pointer(type) && check_is_castable_to(c, x, type)) {
  2350. can_convert = true;
  2351. }
  2352. }
  2353. } else if (check_is_castable_to(c, x, type)) {
  2354. if (x->mode != Addressing_Constant) {
  2355. x->mode = Addressing_Value;
  2356. } else if (is_type_slice(type) && is_type_string(x->type)) {
  2357. x->mode = Addressing_Value;
  2358. } else if (!is_type_vector(x->type) && is_type_vector(type)) {
  2359. x->mode = Addressing_Value;
  2360. }
  2361. can_convert = true;
  2362. }
  2363. if (!can_convert) {
  2364. gbString expr_str = expr_to_string(x->expr);
  2365. gbString to_type = type_to_string(type);
  2366. gbString from_type = type_to_string(x->type);
  2367. error_node(x->expr, "Cannot cast `%s` as `%s` from `%s`", expr_str, to_type, from_type);
  2368. gb_string_free(from_type);
  2369. gb_string_free(to_type);
  2370. gb_string_free(expr_str);
  2371. x->mode = Addressing_Invalid;
  2372. return;
  2373. }
  2374. if (is_type_untyped(x->type)) {
  2375. Type *final_type = type;
  2376. if (is_const_expr && !is_type_constant_type(type)) {
  2377. final_type = default_type(x->type);
  2378. }
  2379. update_expr_type(c, x->expr, final_type, true);
  2380. }
  2381. x->type = type;
  2382. }
  2383. bool check_binary_vector_expr(Checker *c, Token op, Operand *x, Operand *y) {
  2384. if (is_type_vector(x->type) && !is_type_vector(y->type)) {
  2385. if (check_is_assignable_to(c, y, x->type)) {
  2386. if (check_binary_op(c, x, op)) {
  2387. return true;
  2388. }
  2389. }
  2390. }
  2391. return false;
  2392. }
  2393. void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
  2394. GB_ASSERT(node->kind == AstNode_BinaryExpr);
  2395. Operand y_ = {}, *y = &y_;
  2396. ast_node(be, BinaryExpr, node);
  2397. Token op = be->op;
  2398. switch (op.kind) {
  2399. case Token_CmpEq:
  2400. case Token_NotEq: {
  2401. // NOTE(bill): Allow comparisons between types
  2402. check_expr_or_type(c, x, be->left);
  2403. check_expr_or_type(c, y, be->right);
  2404. bool xt = x->mode == Addressing_Type;
  2405. bool yt = y->mode == Addressing_Type;
  2406. // If only one is a type, this is an error
  2407. if (xt ^ yt) {
  2408. GB_ASSERT(xt != yt);
  2409. if (xt) error_operand_not_expression(x);
  2410. if (yt) error_operand_not_expression(y);
  2411. }
  2412. } break;
  2413. default:
  2414. check_expr(c, x, be->left);
  2415. check_expr(c, y, be->right);
  2416. break;
  2417. }
  2418. if (x->mode == Addressing_Invalid) {
  2419. return;
  2420. }
  2421. if (y->mode == Addressing_Invalid) {
  2422. x->mode = Addressing_Invalid;
  2423. x->expr = y->expr;
  2424. return;
  2425. }
  2426. if (token_is_shift(op.kind)) {
  2427. check_shift(c, x, y, node);
  2428. return;
  2429. }
  2430. if (op.kind == Token_Add || op.kind == Token_Sub) {
  2431. if (is_type_pointer(x->type) && is_type_integer(y->type)) {
  2432. *x = check_ptr_addition(c, op.kind, x, y, node);
  2433. return;
  2434. } else if (is_type_integer(x->type) && is_type_pointer(y->type)) {
  2435. if (op.kind == Token_Sub) {
  2436. gbString lhs = expr_to_string(x->expr);
  2437. gbString rhs = expr_to_string(y->expr);
  2438. error_node(node, "Invalid pointer arithmetic, did you mean `%s %.*s %s`?", rhs, LIT(op.string), lhs);
  2439. gb_string_free(rhs);
  2440. gb_string_free(lhs);
  2441. x->mode = Addressing_Invalid;
  2442. return;
  2443. }
  2444. *x = check_ptr_addition(c, op.kind, y, x, node);
  2445. return;
  2446. }
  2447. }
  2448. convert_to_typed(c, x, y->type, 0);
  2449. if (x->mode == Addressing_Invalid) {
  2450. return;
  2451. }
  2452. convert_to_typed(c, y, x->type, 0);
  2453. if (y->mode == Addressing_Invalid) {
  2454. x->mode = Addressing_Invalid;
  2455. return;
  2456. }
  2457. if (token_is_comparison(op.kind)) {
  2458. check_comparison(c, x, y, op.kind);
  2459. return;
  2460. }
  2461. if (check_binary_vector_expr(c, op, x, y)) {
  2462. x->mode = Addressing_Value;
  2463. x->type = x->type;
  2464. return;
  2465. }
  2466. if (check_binary_vector_expr(c, op, y, x)) {
  2467. x->mode = Addressing_Value;
  2468. x->type = y->type;
  2469. return;
  2470. }
  2471. if (!are_types_identical(x->type, y->type)) {
  2472. if (x->type != t_invalid &&
  2473. y->type != t_invalid) {
  2474. gbString xt = type_to_string(x->type);
  2475. gbString yt = type_to_string(y->type);
  2476. gbString expr_str = expr_to_string(x->expr);
  2477. error(op, "Mismatched types in binary expression `%s` : `%s` vs `%s`", expr_str, xt, yt);
  2478. gb_string_free(expr_str);
  2479. gb_string_free(yt);
  2480. gb_string_free(xt);
  2481. }
  2482. x->mode = Addressing_Invalid;
  2483. return;
  2484. }
  2485. if (!check_binary_op(c, x, op)) {
  2486. x->mode = Addressing_Invalid;
  2487. return;
  2488. }
  2489. switch (op.kind) {
  2490. case Token_Quo:
  2491. case Token_Mod:
  2492. case Token_ModMod:
  2493. case Token_QuoEq:
  2494. case Token_ModEq:
  2495. case Token_ModModEq:
  2496. if ((x->mode == Addressing_Constant || is_type_integer(x->type)) &&
  2497. y->mode == Addressing_Constant) {
  2498. bool fail = false;
  2499. switch (y->value.kind) {
  2500. case ExactValue_Integer:
  2501. if (i128_eq(y->value.value_integer, I128_ZERO)) {
  2502. fail = true;
  2503. }
  2504. break;
  2505. case ExactValue_Float:
  2506. if (y->value.value_float == 0.0) {
  2507. fail = true;
  2508. }
  2509. break;
  2510. }
  2511. if (fail) {
  2512. error_node(y->expr, "Division by zero not allowed");
  2513. x->mode = Addressing_Invalid;
  2514. return;
  2515. }
  2516. }
  2517. }
  2518. if (x->mode == Addressing_Constant &&
  2519. y->mode == Addressing_Constant) {
  2520. ExactValue a = x->value;
  2521. ExactValue b = y->value;
  2522. Type *type = base_type(x->type);
  2523. if (is_type_pointer(type)) {
  2524. GB_ASSERT(op.kind == Token_Sub);
  2525. i64 bytes = a.value_pointer - b.value_pointer;
  2526. i64 diff = bytes/type_size_of(c->allocator, type);
  2527. x->value = exact_value_pointer(diff);
  2528. return;
  2529. }
  2530. if (!is_type_constant_type(type)) {
  2531. gbString xt = type_to_string(x->type);
  2532. gbString err_str = expr_to_string(node);
  2533. error(op, "Invalid type, `%s`, for constant binary expression `%s`", xt, err_str);
  2534. gb_string_free(err_str);
  2535. gb_string_free(xt);
  2536. x->mode = Addressing_Invalid;
  2537. return;
  2538. }
  2539. if (op.kind == Token_Quo && is_type_integer(type)) {
  2540. op.kind = Token_QuoEq; // NOTE(bill): Hack to get division of integers
  2541. }
  2542. x->value = exact_binary_operator_value(op.kind, a, b);
  2543. if (is_type_typed(type)) {
  2544. if (node != NULL) {
  2545. x->expr = node;
  2546. }
  2547. check_is_expressible(c, x, type);
  2548. }
  2549. return;
  2550. }
  2551. x->mode = Addressing_Value;
  2552. }
  2553. void update_expr_type(Checker *c, AstNode *e, Type *type, bool final) {
  2554. HashKey key = hash_pointer(e);
  2555. ExprInfo *found = map_get(&c->info.untyped, key);
  2556. if (found == NULL) {
  2557. return;
  2558. }
  2559. ExprInfo old = *found;
  2560. switch (e->kind) {
  2561. case_ast_node(ue, UnaryExpr, e);
  2562. if (old.value.kind != ExactValue_Invalid) {
  2563. // NOTE(bill): if `e` is constant, the operands will be constant too.
  2564. // They don't need to be updated as they will be updated later and
  2565. // checked at the end of general checking stage.
  2566. break;
  2567. }
  2568. update_expr_type(c, ue->expr, type, final);
  2569. case_end;
  2570. case_ast_node(be, BinaryExpr, e);
  2571. if (old.value.kind != ExactValue_Invalid) {
  2572. // See above note in UnaryExpr case
  2573. break;
  2574. }
  2575. if (token_is_comparison(be->op.kind)) {
  2576. // NOTE(bill): Do nothing as the types are fine
  2577. } else if (token_is_shift(be->op.kind)) {
  2578. update_expr_type(c, be->left, type, final);
  2579. } else {
  2580. update_expr_type(c, be->left, type, final);
  2581. update_expr_type(c, be->right, type, final);
  2582. }
  2583. case_end;
  2584. case_ast_node(pe, ParenExpr, e);
  2585. update_expr_type(c, pe->expr, type, final);
  2586. case_end;
  2587. }
  2588. if (!final && is_type_untyped(type)) {
  2589. old.type = base_type(type);
  2590. map_set(&c->info.untyped, key, old);
  2591. return;
  2592. }
  2593. // We need to remove it and then give it a new one
  2594. map_remove(&c->info.untyped, key);
  2595. if (old.is_lhs && !is_type_integer(type)) {
  2596. gbString expr_str = expr_to_string(e);
  2597. gbString type_str = type_to_string(type);
  2598. error_node(e, "Shifted operand %s must be an integer, got %s", expr_str, type_str);
  2599. gb_string_free(type_str);
  2600. gb_string_free(expr_str);
  2601. return;
  2602. }
  2603. add_type_and_value(&c->info, e, old.mode, type, old.value);
  2604. }
  2605. void update_expr_value(Checker *c, AstNode *e, ExactValue value) {
  2606. ExprInfo *found = map_get(&c->info.untyped, hash_pointer(e));
  2607. if (found) {
  2608. found->value = value;
  2609. }
  2610. }
  2611. void convert_untyped_error(Checker *c, Operand *operand, Type *target_type) {
  2612. gbString expr_str = expr_to_string(operand->expr);
  2613. gbString type_str = type_to_string(target_type);
  2614. char *extra_text = "";
  2615. if (operand->mode == Addressing_Constant) {
  2616. if (i128_eq(operand->value.value_integer, I128_ZERO)) {
  2617. if (make_string_c(expr_str) != "nil") { // HACK NOTE(bill): Just in case
  2618. // NOTE(bill): Doesn't matter what the type is as it's still zero in the union
  2619. extra_text = " - Did you want `nil`?";
  2620. }
  2621. }
  2622. }
  2623. error_node(operand->expr, "Cannot convert `%s` to `%s`%s", expr_str, type_str, extra_text);
  2624. gb_string_free(type_str);
  2625. gb_string_free(expr_str);
  2626. operand->mode = Addressing_Invalid;
  2627. }
  2628. ExactValue convert_exact_value_for_type(ExactValue v, Type *type) {
  2629. Type *t = core_type(type);
  2630. if (is_type_boolean(t)) {
  2631. // v = exact_value_to_boolean(v);
  2632. } else if (is_type_float(t)) {
  2633. v = exact_value_to_float(v);
  2634. } else if (is_type_integer(t)) {
  2635. v = exact_value_to_integer(v);
  2636. } else if (is_type_pointer(t)) {
  2637. v = exact_value_to_integer(v);
  2638. } else if (is_type_complex(t)) {
  2639. v = exact_value_to_complex(v);
  2640. }
  2641. return v;
  2642. }
  2643. // NOTE(bill): Set initial level to 0
  2644. void convert_to_typed(Checker *c, Operand *operand, Type *target_type, i32 level) {
  2645. GB_ASSERT_NOT_NULL(target_type);
  2646. if (operand->mode == Addressing_Invalid ||
  2647. operand->mode == Addressing_Type ||
  2648. is_type_typed(operand->type) ||
  2649. target_type == t_invalid) {
  2650. return;
  2651. }
  2652. if (is_type_untyped(target_type)) {
  2653. GB_ASSERT(operand->type->kind == Type_Basic);
  2654. GB_ASSERT(target_type->kind == Type_Basic);
  2655. BasicKind x_kind = operand->type->Basic.kind;
  2656. BasicKind y_kind = target_type->Basic.kind;
  2657. if (is_type_numeric(operand->type) && is_type_numeric(target_type)) {
  2658. if (x_kind < y_kind) {
  2659. operand->type = target_type;
  2660. update_expr_type(c, operand->expr, target_type, false);
  2661. }
  2662. } else if (x_kind != y_kind) {
  2663. operand->mode = Addressing_Invalid;
  2664. convert_untyped_error(c, operand, target_type);
  2665. return;
  2666. }
  2667. return;
  2668. }
  2669. Type *t = core_type(target_type);
  2670. switch (t->kind) {
  2671. case Type_Basic:
  2672. if (operand->mode == Addressing_Constant) {
  2673. check_is_expressible(c, operand, t);
  2674. if (operand->mode == Addressing_Invalid) {
  2675. return;
  2676. }
  2677. update_expr_value(c, operand->expr, operand->value);
  2678. } else {
  2679. switch (operand->type->Basic.kind) {
  2680. case Basic_UntypedBool:
  2681. if (!is_type_boolean(target_type)) {
  2682. operand->mode = Addressing_Invalid;
  2683. convert_untyped_error(c, operand, target_type);
  2684. return;
  2685. }
  2686. break;
  2687. case Basic_UntypedInteger:
  2688. case Basic_UntypedFloat:
  2689. case Basic_UntypedComplex:
  2690. case Basic_UntypedRune:
  2691. if (!is_type_numeric(target_type)) {
  2692. operand->mode = Addressing_Invalid;
  2693. convert_untyped_error(c, operand, target_type);
  2694. return;
  2695. }
  2696. break;
  2697. case Basic_UntypedNil:
  2698. if (is_type_any(target_type)) {
  2699. target_type = t_untyped_nil;
  2700. } else if (!type_has_nil(target_type)) {
  2701. operand->mode = Addressing_Invalid;
  2702. convert_untyped_error(c, operand, target_type);
  2703. return;
  2704. }
  2705. break;
  2706. }
  2707. }
  2708. break;
  2709. case Type_Vector: {
  2710. Type *elem = base_vector_type(t);
  2711. if (check_is_assignable_to(c, operand, elem)) {
  2712. operand->mode = Addressing_Value;
  2713. } else {
  2714. operand->mode = Addressing_Invalid;
  2715. convert_untyped_error(c, operand, target_type);
  2716. return;
  2717. }
  2718. } break;
  2719. default:
  2720. if (!is_type_untyped_nil(operand->type) || !type_has_nil(target_type)) {
  2721. operand->mode = Addressing_Invalid;
  2722. convert_untyped_error(c, operand, target_type);
  2723. return;
  2724. }
  2725. target_type = t_untyped_nil;
  2726. break;
  2727. }
  2728. operand->type = target_type;
  2729. update_expr_type(c, operand->expr, target_type, true);
  2730. }
  2731. bool check_index_value(Checker *c, bool open_range, AstNode *index_value, i64 max_count, i64 *value) {
  2732. Operand operand = {Addressing_Invalid};
  2733. check_expr(c, &operand, index_value);
  2734. if (operand.mode == Addressing_Invalid) {
  2735. if (value) *value = 0;
  2736. return false;
  2737. }
  2738. convert_to_typed(c, &operand, t_int, 0);
  2739. if (operand.mode == Addressing_Invalid) {
  2740. if (value) *value = 0;
  2741. return false;
  2742. }
  2743. if (!is_type_integer(operand.type)) {
  2744. gbString expr_str = expr_to_string(operand.expr);
  2745. error_node(operand.expr, "Index `%s` must be an integer", expr_str);
  2746. gb_string_free(expr_str);
  2747. if (value) *value = 0;
  2748. return false;
  2749. }
  2750. if (operand.mode == Addressing_Constant &&
  2751. (c->context.stmt_state_flags & StmtStateFlag_no_bounds_check) == 0) {
  2752. i64 i = i128_to_i64(exact_value_to_integer(operand.value).value_integer);
  2753. if (i < 0) {
  2754. gbString expr_str = expr_to_string(operand.expr);
  2755. error_node(operand.expr, "Index `%s` cannot be a negative value", expr_str);
  2756. gb_string_free(expr_str);
  2757. if (value) *value = 0;
  2758. return false;
  2759. }
  2760. if (max_count >= 0) { // NOTE(bill): Do array bound checking
  2761. if (value) *value = i;
  2762. bool out_of_bounds = false;
  2763. if (open_range) {
  2764. out_of_bounds = i >= max_count;
  2765. } else {
  2766. out_of_bounds = i > max_count;
  2767. }
  2768. if (out_of_bounds) {
  2769. gbString expr_str = expr_to_string(operand.expr);
  2770. error_node(operand.expr, "Index `%s` is out of bounds range 0..<%lld", expr_str, max_count);
  2771. gb_string_free(expr_str);
  2772. return false;
  2773. }
  2774. return true;
  2775. }
  2776. }
  2777. // NOTE(bill): It's alright :D
  2778. if (value) *value = -1;
  2779. return true;
  2780. }
  2781. isize entity_overload_count(Scope *s, String name) {
  2782. Entity *e = scope_lookup_entity(s, name);
  2783. if (e == NULL) {
  2784. return 0;
  2785. }
  2786. if (e->kind == Entity_Procedure) {
  2787. // NOTE(bill): Overloads are only allowed with the same scope
  2788. return multi_map_count(&s->elements, hash_string(e->token.string));
  2789. }
  2790. return 1;
  2791. }
  2792. bool check_is_field_exported(Checker *c, Entity *field) {
  2793. if (field == NULL) {
  2794. // NOTE(bill): Just incase
  2795. return true;
  2796. }
  2797. if (field->kind != Entity_Variable) {
  2798. return true;
  2799. }
  2800. Scope *file_scope = field->scope;
  2801. if (file_scope == NULL) {
  2802. return true;
  2803. }
  2804. while (!file_scope->is_file) {
  2805. file_scope = file_scope->parent;
  2806. }
  2807. if (!is_entity_exported(field) && file_scope != c->context.file_scope) {
  2808. return false;
  2809. }
  2810. return true;
  2811. }
  2812. Entity *check_selector(Checker *c, Operand *operand, AstNode *node, Type *type_hint) {
  2813. ast_node(se, SelectorExpr, node);
  2814. bool check_op_expr = true;
  2815. Entity *expr_entity = NULL;
  2816. Entity *entity = NULL;
  2817. Selection sel = {}; // NOTE(bill): Not used if it's an import name
  2818. operand->expr = node;
  2819. AstNode *op_expr = se->expr;
  2820. AstNode *selector = unparen_expr(se->selector);
  2821. if (selector == NULL) {
  2822. operand->mode = Addressing_Invalid;
  2823. operand->expr = node;
  2824. return NULL;
  2825. }
  2826. if (selector->kind != AstNode_Ident && selector->kind != AstNode_BasicLit) {
  2827. // if (selector->kind != AstNode_Ident) {
  2828. error_node(selector, "Illegal selector kind: `%.*s`", LIT(ast_node_strings[selector->kind]));
  2829. operand->mode = Addressing_Invalid;
  2830. operand->expr = node;
  2831. return NULL;
  2832. }
  2833. if (op_expr->kind == AstNode_Ident) {
  2834. String op_name = op_expr->Ident.string;
  2835. Entity *e = scope_lookup_entity(c->context.scope, op_name);
  2836. add_entity_use(c, op_expr, e);
  2837. expr_entity = e;
  2838. Entity *original_e = e;
  2839. if (e != NULL && e->kind == Entity_ImportName && selector->kind == AstNode_Ident) {
  2840. // IMPORTANT NOTE(bill): This is very sloppy code but it's also very fragile
  2841. // It pretty much needs to be in this order and this way
  2842. // If you can clean this up, please do but be really careful
  2843. String import_name = op_name;
  2844. Scope *import_scope = e->ImportName.scope;
  2845. String entity_name = selector->Ident.string;
  2846. check_op_expr = false;
  2847. entity = scope_lookup_entity(import_scope, entity_name);
  2848. bool is_declared = entity != NULL;
  2849. if (is_declared) {
  2850. if (entity->kind == Entity_Builtin) {
  2851. // NOTE(bill): Builtin's are in the universe scope which is part of every scopes hierarchy
  2852. // This means that we should just ignore the found result through it
  2853. is_declared = false;
  2854. } else if (entity->scope->is_global && !import_scope->is_global) {
  2855. is_declared = false;
  2856. }
  2857. }
  2858. if (!is_declared) {
  2859. error_node(op_expr, "`%.*s` is not declared by `%.*s`", LIT(entity_name), LIT(import_name));
  2860. operand->mode = Addressing_Invalid;
  2861. operand->expr = node;
  2862. return NULL;
  2863. }
  2864. check_entity_decl(c, entity, NULL, NULL);
  2865. GB_ASSERT(entity->type != NULL);
  2866. isize overload_count = entity_overload_count(import_scope, entity_name);
  2867. bool is_overloaded = overload_count > 1;
  2868. bool implicit_is_found = map_get(&e->ImportName.scope->implicit, hash_pointer(entity)) != NULL;
  2869. bool is_not_exported = !is_entity_exported(entity);
  2870. if (!implicit_is_found) {
  2871. is_not_exported = false;
  2872. } else if (entity->kind == Entity_ImportName) {
  2873. is_not_exported = true;
  2874. }
  2875. if (is_not_exported) {
  2876. gbString sel_str = expr_to_string(selector);
  2877. error_node(op_expr, "`%s` is not exported by `%.*s`", sel_str, LIT(import_name));
  2878. gb_string_free(sel_str);
  2879. operand->mode = Addressing_Invalid;
  2880. operand->expr = node;
  2881. return NULL;
  2882. }
  2883. if (is_overloaded) {
  2884. HashKey key = hash_string(entity_name);
  2885. bool skip = false;
  2886. Entity **procs = gb_alloc_array(heap_allocator(), Entity *, overload_count);
  2887. multi_map_get_all(&import_scope->elements, key, procs);
  2888. for (isize i = 0; i < overload_count; i++) {
  2889. Type *t = base_type(procs[i]->type);
  2890. if (t == t_invalid) {
  2891. continue;
  2892. }
  2893. // NOTE(bill): Check to see if it's imported
  2894. if (map_get(&import_scope->implicit, hash_pointer(procs[i]))) {
  2895. gb_swap(Entity *, procs[i], procs[overload_count-1]);
  2896. overload_count--;
  2897. i--; // NOTE(bill): Counteract the post event
  2898. continue;
  2899. }
  2900. Operand x = {};
  2901. x.mode = Addressing_Value;
  2902. x.type = t;
  2903. if (type_hint != NULL) {
  2904. if (check_is_assignable_to(c, &x, type_hint)) {
  2905. entity = procs[i];
  2906. skip = true;
  2907. break;
  2908. }
  2909. }
  2910. }
  2911. if (overload_count > 0 && !skip) {
  2912. operand->mode = Addressing_Overload;
  2913. operand->type = t_invalid;
  2914. operand->expr = node;
  2915. operand->overload_count = overload_count;
  2916. operand->overload_entities = procs;
  2917. return procs[0];
  2918. }
  2919. }
  2920. }
  2921. }
  2922. if (check_op_expr) {
  2923. check_expr_base(c, operand, op_expr, NULL);
  2924. if (operand->mode == Addressing_Invalid) {
  2925. operand->mode = Addressing_Invalid;
  2926. operand->expr = node;
  2927. return NULL;
  2928. }
  2929. }
  2930. if (entity == NULL && selector->kind == AstNode_Ident) {
  2931. String field_name = selector->Ident.string;
  2932. sel = lookup_field(c->allocator, operand->type, field_name, operand->mode == Addressing_Type);
  2933. if (operand->mode != Addressing_Type && !check_is_field_exported(c, sel.entity)) {
  2934. error_node(op_expr, "`%.*s` is an unexported field", LIT(field_name));
  2935. operand->mode = Addressing_Invalid;
  2936. operand->expr = node;
  2937. return NULL;
  2938. }
  2939. entity = sel.entity;
  2940. // NOTE(bill): Add type info needed for fields like `names`
  2941. if (entity != NULL && (entity->flags&EntityFlag_TypeField)) {
  2942. add_type_info_type(c, operand->type);
  2943. }
  2944. }
  2945. if (entity == NULL && selector->kind == AstNode_BasicLit) {
  2946. if (is_type_struct(operand->type) || is_type_tuple(operand->type)) {
  2947. Type *type = base_type(operand->type);
  2948. Operand o = {};
  2949. check_expr(c, &o, selector);
  2950. if (o.mode != Addressing_Constant ||
  2951. !is_type_integer(o.type)) {
  2952. error_node(op_expr, "Indexed based selectors must be a constant integer %s");
  2953. operand->mode = Addressing_Invalid;
  2954. operand->expr = node;
  2955. return NULL;
  2956. }
  2957. i64 index = i128_to_i64(o.value.value_integer);
  2958. if (index < 0) {
  2959. error_node(o.expr, "Index %lld cannot be a negative value", index);
  2960. operand->mode = Addressing_Invalid;
  2961. operand->expr = node;
  2962. return NULL;
  2963. }
  2964. i64 max_count = 0;
  2965. switch (type->kind) {
  2966. case Type_Record: max_count = type->Record.field_count; break;
  2967. case Type_Tuple: max_count = type->Tuple.variable_count; break;
  2968. }
  2969. if (index >= max_count) {
  2970. error_node(o.expr, "Index %lld is out of bounds range 0..<%lld", index, max_count);
  2971. operand->mode = Addressing_Invalid;
  2972. operand->expr = node;
  2973. return NULL;
  2974. }
  2975. sel = lookup_field_from_index(heap_allocator(), type, index);
  2976. entity = sel.entity;
  2977. GB_ASSERT(entity != NULL);
  2978. } else {
  2979. error_node(op_expr, "Indexed based selectors may only be used on structs or tuples");
  2980. operand->mode = Addressing_Invalid;
  2981. operand->expr = node;
  2982. return NULL;
  2983. }
  2984. }
  2985. if (entity == NULL &&
  2986. operand->type != NULL && is_type_untyped(operand->type) && is_type_string(operand->type)) {
  2987. String s = operand->value.value_string;
  2988. operand->mode = Addressing_Constant;
  2989. operand->value = exact_value_i64(s.len);
  2990. operand->type = t_untyped_integer;
  2991. return NULL;
  2992. }
  2993. if (entity == NULL) {
  2994. gbString op_str = expr_to_string(op_expr);
  2995. gbString type_str = type_to_string(operand->type);
  2996. gbString sel_str = expr_to_string(selector);
  2997. error_node(op_expr, "`%s` of type `%s` has no field `%s`", op_str, type_str, sel_str);
  2998. gb_string_free(sel_str);
  2999. gb_string_free(type_str);
  3000. gb_string_free(op_str);
  3001. operand->mode = Addressing_Invalid;
  3002. operand->expr = node;
  3003. return NULL;
  3004. }
  3005. if (expr_entity != NULL && expr_entity->kind == Entity_Constant && entity->kind != Entity_Constant) {
  3006. gbString op_str = expr_to_string(op_expr);
  3007. gbString type_str = type_to_string(operand->type);
  3008. gbString sel_str = expr_to_string(selector);
  3009. error_node(op_expr, "Cannot access non-constant field `%s` from `%s`", sel_str, op_str);
  3010. gb_string_free(sel_str);
  3011. gb_string_free(type_str);
  3012. gb_string_free(op_str);
  3013. operand->mode = Addressing_Invalid;
  3014. operand->expr = node;
  3015. return NULL;
  3016. }
  3017. add_entity_use(c, selector, entity);
  3018. switch (entity->kind) {
  3019. case Entity_Constant:
  3020. operand->mode = Addressing_Constant;
  3021. operand->value = entity->Constant.value;
  3022. break;
  3023. case Entity_Variable:
  3024. // TODO(bill): Is this the rule I need?
  3025. if (operand->mode == Addressing_Immutable) {
  3026. // Okay
  3027. } else if (sel.indirect || operand->mode != Addressing_Value) {
  3028. operand->mode = Addressing_Variable;
  3029. } else {
  3030. operand->mode = Addressing_Value;
  3031. }
  3032. break;
  3033. case Entity_TypeName:
  3034. operand->mode = Addressing_Type;
  3035. break;
  3036. case Entity_Procedure:
  3037. operand->mode = Addressing_Value;
  3038. break;
  3039. case Entity_Builtin:
  3040. operand->mode = Addressing_Builtin;
  3041. operand->builtin_id = cast(BuiltinProcId)entity->Builtin.id;
  3042. break;
  3043. // NOTE(bill): These cases should never be hit but are here for sanity reasons
  3044. case Entity_Nil:
  3045. operand->mode = Addressing_Value;
  3046. break;
  3047. }
  3048. operand->type = entity->type;
  3049. operand->expr = node;
  3050. return entity;
  3051. }
  3052. bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) {
  3053. GB_ASSERT(call->kind == AstNode_CallExpr);
  3054. ast_node(ce, CallExpr, call);
  3055. BuiltinProc *bp = &builtin_procs[id];
  3056. {
  3057. char *err = NULL;
  3058. if (ce->args.count < bp->arg_count) {
  3059. err = "Too few";
  3060. } else if (ce->args.count > bp->arg_count && !bp->variadic) {
  3061. err = "Too many";
  3062. }
  3063. if (err != NULL) {
  3064. gbString expr = expr_to_string(ce->proc);
  3065. error(ce->close, "%s arguments for `%s`, expected %td, got %td",
  3066. err, expr,
  3067. bp->arg_count, ce->args.count);
  3068. gb_string_free(expr);
  3069. return false;
  3070. }
  3071. }
  3072. if (ce->args.count > 0) {
  3073. if (ce->args[0]->kind == AstNode_FieldValue) {
  3074. error_node(call, "`field = value` calling is not allowed on built-in procedures");
  3075. return false;
  3076. }
  3077. }
  3078. bool vari_expand = (ce->ellipsis.pos.line != 0);
  3079. if (vari_expand && id != BuiltinProc_append) {
  3080. error(ce->ellipsis, "Invalid use of `..` with built-in procedure `append`");
  3081. return false;
  3082. }
  3083. switch (id) {
  3084. case BuiltinProc_new:
  3085. case BuiltinProc_make:
  3086. case BuiltinProc_size_of:
  3087. case BuiltinProc_align_of:
  3088. case BuiltinProc_offset_of:
  3089. case BuiltinProc_type_info:
  3090. case BuiltinProc_transmute:
  3091. // NOTE(bill): The first arg may be a Type, this will be checked case by case
  3092. break;
  3093. default:
  3094. check_multi_expr(c, operand, ce->args[0]);
  3095. }
  3096. switch (id) {
  3097. default:
  3098. GB_PANIC("Implement built-in procedure: %.*s", LIT(builtin_procs[id].name));
  3099. break;
  3100. case BuiltinProc_len:
  3101. case BuiltinProc_cap: {
  3102. // len :: proc(Type) -> int
  3103. // cap :: proc(Type) -> int
  3104. Type *op_type = type_deref(operand->type);
  3105. Type *type = t_int;
  3106. AddressingMode mode = Addressing_Invalid;
  3107. ExactValue value = {};
  3108. if (is_type_string(op_type) && id == BuiltinProc_len) {
  3109. if (operand->mode == Addressing_Constant) {
  3110. mode = Addressing_Constant;
  3111. String str = operand->value.value_string;
  3112. value = exact_value_i64(str.len);
  3113. type = t_untyped_integer;
  3114. } else {
  3115. mode = Addressing_Value;
  3116. }
  3117. } else if (is_type_array(op_type)) {
  3118. Type *at = core_type(op_type);
  3119. mode = Addressing_Constant;
  3120. value = exact_value_i64(at->Array.count);
  3121. type = t_untyped_integer;
  3122. } else if (is_type_vector(op_type) && id == BuiltinProc_len) {
  3123. Type *at = core_type(op_type);
  3124. mode = Addressing_Constant;
  3125. value = exact_value_i64(at->Vector.count);
  3126. type = t_untyped_integer;
  3127. } else if (is_type_slice(op_type)) {
  3128. mode = Addressing_Value;
  3129. } else if (is_type_dynamic_array(op_type)) {
  3130. mode = Addressing_Value;
  3131. } else if (is_type_map(op_type)) {
  3132. mode = Addressing_Value;
  3133. }
  3134. if (mode == Addressing_Invalid) {
  3135. String name = builtin_procs[id].name;
  3136. gbString t = type_to_string(operand->type);
  3137. error_node(call, "`%.*s` is not supported for `%s`", LIT(name), t);
  3138. return false;
  3139. }
  3140. operand->mode = mode;
  3141. operand->value = value;
  3142. operand->type = type;
  3143. } break;
  3144. case BuiltinProc_new: {
  3145. // new :: proc(Type) -> ^Type
  3146. Operand op = {};
  3147. check_expr_or_type(c, &op, ce->args[0]);
  3148. Type *type = op.type;
  3149. if ((op.mode != Addressing_Type && type == NULL) || type == t_invalid) {
  3150. error_node(ce->args[0], "Expected a type for `new`");
  3151. return false;
  3152. }
  3153. operand->mode = Addressing_Value;
  3154. operand->type = make_type_pointer(c->allocator, type);
  3155. } break;
  3156. #if 0
  3157. case BuiltinProc_new_slice: {
  3158. // new_slice :: proc(Type, len: int) -> []Type
  3159. // new_slice :: proc(Type, len, cap: int) -> []Type
  3160. Operand op = {};
  3161. check_expr_or_type(c, &op, ce->args[0]);
  3162. Type *type = op.type;
  3163. if ((op.mode != Addressing_Type && type == NULL) || type == t_invalid) {
  3164. error_node(ce->args[0], "Expected a type for `new_slice`");
  3165. return false;
  3166. }
  3167. isize arg_count = ce->args.count;
  3168. if (arg_count < 2 || 3 < arg_count) {
  3169. error_node(ce->args[0], "`new_slice` expects 2 or 3 arguments, found %td", arg_count);
  3170. // NOTE(bill): Return the correct type to reduce errors
  3171. } else {
  3172. // If any are constant
  3173. i64 sizes[2] = {};
  3174. isize size_count = 0;
  3175. for (isize i = 1; i < arg_count; i++) {
  3176. i64 val = 0;
  3177. bool ok = check_index_value(c, ce->args[i], -1, &val);
  3178. if (ok && val >= 0) {
  3179. GB_ASSERT(size_count < gb_count_of(sizes));
  3180. sizes[size_count++] = val;
  3181. }
  3182. }
  3183. if (size_count == 2 && sizes[0] > sizes[1]) {
  3184. error_node(ce->args[1], "`new_slice` count and capacity are swapped");
  3185. // No need quit
  3186. }
  3187. }
  3188. operand->mode = Addressing_Value;
  3189. operand->type = make_type_slice(c->allocator, type);
  3190. } break;
  3191. #endif
  3192. case BuiltinProc_make: {
  3193. // make :: proc(Type, len: int) -> Type
  3194. // make :: proc(Type, len, cap: int) -> Type
  3195. Operand op = {};
  3196. check_expr_or_type(c, &op, ce->args[0]);
  3197. Type *type = op.type;
  3198. if ((op.mode != Addressing_Type && type == NULL) || type == t_invalid) {
  3199. error_node(ce->args[0], "Expected a type for `make`");
  3200. return false;
  3201. }
  3202. isize min_args = 0;
  3203. isize max_args = 1;
  3204. if (is_type_slice(type)) {
  3205. min_args = 2;
  3206. max_args = 3;
  3207. } else if (is_type_dynamic_map(type)) {
  3208. min_args = 1;
  3209. max_args = 2;
  3210. } else if (is_type_dynamic_array(type)) {
  3211. min_args = 1;
  3212. max_args = 3;
  3213. } else {
  3214. gbString str = type_to_string(type);
  3215. error_node(call, "Cannot `make` %s; type must be a slice, map, or dynamic array", str);
  3216. gb_string_free(str);
  3217. return false;
  3218. }
  3219. isize arg_count = ce->args.count;
  3220. if (arg_count < min_args || max_args < arg_count) {
  3221. error_node(ce->args[0], "`make` expects %td or %d argument, found %td", min_args, max_args, arg_count);
  3222. return false;
  3223. }
  3224. // If any are constant
  3225. i64 sizes[4] = {};
  3226. isize size_count = 0;
  3227. for (isize i = 1; i < arg_count; i++) {
  3228. i64 val = 0;
  3229. bool ok = check_index_value(c, false, ce->args[i], -1, &val);
  3230. if (ok && val >= 0) {
  3231. GB_ASSERT(size_count < gb_count_of(sizes));
  3232. sizes[size_count++] = val;
  3233. }
  3234. }
  3235. if (size_count == 2 && sizes[0] > sizes[1]) {
  3236. error_node(ce->args[1], "`make` count and capacity are swapped");
  3237. // No need quit
  3238. }
  3239. operand->mode = Addressing_Value;
  3240. operand->type = type;
  3241. } break;
  3242. case BuiltinProc_free: {
  3243. // free :: proc(^Type)
  3244. // free :: proc([]Type)
  3245. // free :: proc(string)
  3246. // free :: proc(map[K]T)
  3247. Type *type = operand->type;
  3248. bool ok = false;
  3249. if (is_type_pointer(type)) {
  3250. ok = true;
  3251. } else if (is_type_slice(type)) {
  3252. ok = true;
  3253. } else if (is_type_string(type)) {
  3254. ok = true;
  3255. } else if (is_type_dynamic_array(type)) {
  3256. ok = true;
  3257. } else if (is_type_dynamic_map(type)) {
  3258. ok = true;
  3259. }
  3260. if (!ok) {
  3261. gbString type_str = type_to_string(type);
  3262. error_node(operand->expr, "Invalid type for `free`, got `%s`", type_str);
  3263. gb_string_free(type_str);
  3264. return false;
  3265. }
  3266. operand->mode = Addressing_NoValue;
  3267. } break;
  3268. case BuiltinProc_reserve: {
  3269. // reserve :: proc([dynamic]Type, count: int) {
  3270. // reserve :: proc(map[Key]Type, count: int) {
  3271. Type *type = operand->type;
  3272. if (!is_type_dynamic_array(type) && !is_type_dynamic_map(type)) {
  3273. gbString str = type_to_string(type);
  3274. error_node(operand->expr, "Expected a dynamic array or dynamic map, got `%s`", str);
  3275. gb_string_free(str);
  3276. return false;
  3277. }
  3278. AstNode *capacity = ce->args[1];
  3279. Operand op = {};
  3280. check_expr(c, &op, capacity);
  3281. if (op.mode == Addressing_Invalid) {
  3282. return false;
  3283. }
  3284. Type *arg_type = base_type(op.type);
  3285. if (!is_type_integer(arg_type)) {
  3286. error_node(operand->expr, "`reserve` capacities must be an integer");
  3287. return false;
  3288. }
  3289. operand->type = NULL;
  3290. operand->mode = Addressing_NoValue;
  3291. } break;
  3292. case BuiltinProc_clear: {
  3293. Type *type = operand->type;
  3294. bool is_pointer = is_type_pointer(type);
  3295. type = base_type(type_deref(type));
  3296. if (!is_type_dynamic_array(type) && !is_type_map(type) && !is_type_slice(type)) {
  3297. gbString str = type_to_string(type);
  3298. error_node(operand->expr, "Invalid type for `clear`, got `%s`", str);
  3299. gb_string_free(str);
  3300. return false;
  3301. }
  3302. operand->type = NULL;
  3303. operand->mode = Addressing_NoValue;
  3304. } break;
  3305. case BuiltinProc_append: {
  3306. // append :: proc([dynamic]Type, item: ..Type)
  3307. // append :: proc([]Type, item: ..Type)
  3308. Operand prev_operand = *operand;
  3309. Type *type = operand->type;
  3310. bool is_pointer = is_type_pointer(type);
  3311. type = base_type(type_deref(type));
  3312. if (!is_type_dynamic_array(type) && !is_type_slice(type)) {
  3313. gbString str = type_to_string(type);
  3314. error_node(operand->expr, "Expected a slice or dynamic array, got `%s`", str);
  3315. gb_string_free(str);
  3316. return false;
  3317. }
  3318. bool is_addressable = operand->mode == Addressing_Variable;
  3319. if (is_pointer) {
  3320. is_addressable = true;
  3321. }
  3322. if (!is_addressable) {
  3323. error_node(operand->expr, "`append` can only operate on addressable values");
  3324. return false;
  3325. }
  3326. Type *elem = NULL;
  3327. if (is_type_dynamic_array(type)) {
  3328. elem = type->DynamicArray.elem;
  3329. } else {
  3330. elem = type->Slice.elem;
  3331. }
  3332. Type *slice_elem = make_type_slice(c->allocator, elem);
  3333. Type *proc_type_params = make_type_tuple(c->allocator);
  3334. proc_type_params->Tuple.variables = gb_alloc_array(c->allocator, Entity *, 2);
  3335. proc_type_params->Tuple.variable_count = 2;
  3336. proc_type_params->Tuple.variables[0] = make_entity_param(c->allocator, NULL, blank_token, operand->type, false, false);
  3337. proc_type_params->Tuple.variables[1] = make_entity_param(c->allocator, NULL, blank_token, slice_elem, false, false);
  3338. Type *proc_type = make_type_proc(c->allocator, NULL, proc_type_params, 2, NULL, false, true, ProcCC_Odin);
  3339. check_call_arguments(c, &prev_operand, proc_type, call);
  3340. if (prev_operand.mode == Addressing_Invalid) {
  3341. return false;
  3342. }
  3343. operand->mode = Addressing_Value;
  3344. operand->type = t_int;
  3345. } break;
  3346. case BuiltinProc_delete: {
  3347. // delete :: proc(map[Key]Value, key: Key)
  3348. Type *type = operand->type;
  3349. if (!is_type_map(type)) {
  3350. gbString str = type_to_string(type);
  3351. error_node(operand->expr, "Expected a map, got `%s`", str);
  3352. gb_string_free(str);
  3353. return false;
  3354. }
  3355. Type *key = base_type(type)->Map.key;
  3356. Operand x = {Addressing_Invalid};
  3357. AstNode *key_node = ce->args[1];
  3358. Operand op = {};
  3359. check_expr(c, &op, key_node);
  3360. if (op.mode == Addressing_Invalid) {
  3361. return false;
  3362. }
  3363. if (!check_is_assignable_to(c, &op, key)) {
  3364. gbString kt = type_to_string(key);
  3365. gbString ot = type_to_string(op.type);
  3366. error_node(operand->expr, "Expected a key of type `%s`, got `%s`", key, ot);
  3367. gb_string_free(ot);
  3368. gb_string_free(kt);
  3369. return false;
  3370. }
  3371. operand->mode = Addressing_NoValue;
  3372. } break;
  3373. case BuiltinProc_size_of: {
  3374. // size_of :: proc(Type) -> untyped int
  3375. Type *type = check_type(c, ce->args[0]);
  3376. if (type == NULL || type == t_invalid) {
  3377. error_node(ce->args[0], "Expected a type for `size_of`");
  3378. return false;
  3379. }
  3380. operand->mode = Addressing_Constant;
  3381. operand->value = exact_value_i64(type_size_of(c->allocator, type));
  3382. operand->type = t_untyped_integer;
  3383. } break;
  3384. case BuiltinProc_size_of_val:
  3385. // size_of_val :: proc(val: Type) -> untyped int
  3386. check_assignment(c, operand, NULL, str_lit("argument of `size_of_val`"));
  3387. if (operand->mode == Addressing_Invalid) {
  3388. return false;
  3389. }
  3390. operand->mode = Addressing_Constant;
  3391. operand->value = exact_value_i64(type_size_of(c->allocator, operand->type));
  3392. operand->type = t_untyped_integer;
  3393. break;
  3394. case BuiltinProc_align_of: {
  3395. // align_of :: proc(Type) -> untyped int
  3396. Type *type = check_type(c, ce->args[0]);
  3397. if (type == NULL || type == t_invalid) {
  3398. error_node(ce->args[0], "Expected a type for `align_of`");
  3399. return false;
  3400. }
  3401. operand->mode = Addressing_Constant;
  3402. operand->value = exact_value_i64(type_align_of(c->allocator, type));
  3403. operand->type = t_untyped_integer;
  3404. } break;
  3405. case BuiltinProc_align_of_val:
  3406. // align_of_val :: proc(val: Type) -> untyped int
  3407. check_assignment(c, operand, NULL, str_lit("argument of `align_of_val`"));
  3408. if (operand->mode == Addressing_Invalid) {
  3409. return false;
  3410. }
  3411. operand->mode = Addressing_Constant;
  3412. operand->value = exact_value_i64(type_align_of(c->allocator, operand->type));
  3413. operand->type = t_untyped_integer;
  3414. break;
  3415. case BuiltinProc_offset_of: {
  3416. // offset_of :: proc(Type, field) -> untyped int
  3417. Operand op = {};
  3418. Type *bt = check_type(c, ce->args[0]);
  3419. Type *type = base_type(bt);
  3420. if (type == NULL || type == t_invalid) {
  3421. error_node(ce->args[0], "Expected a type for `offset_of`");
  3422. return false;
  3423. }
  3424. AstNode *field_arg = unparen_expr(ce->args[1]);
  3425. if (field_arg == NULL ||
  3426. field_arg->kind != AstNode_Ident) {
  3427. error_node(field_arg, "Expected an identifier for field argument");
  3428. return false;
  3429. }
  3430. if (is_type_array(type) || is_type_vector(type)) {
  3431. error_node(field_arg, "Invalid type for `offset_of`");
  3432. return false;
  3433. }
  3434. ast_node(arg, Ident, field_arg);
  3435. Selection sel = lookup_field(c->allocator, type, arg->string, operand->mode == Addressing_Type);
  3436. if (sel.entity == NULL) {
  3437. gbString type_str = type_to_string(bt);
  3438. error_node(ce->args[0],
  3439. "`%s` has no field named `%.*s`", type_str, LIT(arg->string));
  3440. gb_string_free(type_str);
  3441. return false;
  3442. }
  3443. if (sel.indirect) {
  3444. gbString type_str = type_to_string(bt);
  3445. error_node(ce->args[0],
  3446. "Field `%.*s` is embedded via a pointer in `%s`", LIT(arg->string), type_str);
  3447. gb_string_free(type_str);
  3448. return false;
  3449. }
  3450. operand->mode = Addressing_Constant;
  3451. operand->value = exact_value_i64(type_offset_of_from_selection(c->allocator, type, sel));
  3452. operand->type = t_untyped_integer;
  3453. } break;
  3454. case BuiltinProc_offset_of_val: {
  3455. // offset_of_val :: proc(val: expression) -> untyped int
  3456. AstNode *arg = unparen_expr(ce->args[0]);
  3457. if (arg->kind != AstNode_SelectorExpr) {
  3458. gbString str = expr_to_string(arg);
  3459. error_node(arg, "`%s` is not a selector expression", str);
  3460. return false;
  3461. }
  3462. ast_node(s, SelectorExpr, arg);
  3463. check_expr(c, operand, s->expr);
  3464. if (operand->mode == Addressing_Invalid) {
  3465. return false;
  3466. }
  3467. Type *type = operand->type;
  3468. if (base_type(type)->kind == Type_Pointer) {
  3469. Type *p = base_type(type);
  3470. if (is_type_struct(p)) {
  3471. type = p->Pointer.elem;
  3472. }
  3473. }
  3474. if (is_type_array(type) || is_type_vector(type)) {
  3475. error_node(arg, "Invalid type for `offset_of_val`");
  3476. return false;
  3477. }
  3478. ast_node(i, Ident, s->selector);
  3479. Selection sel = lookup_field(c->allocator, type, i->string, operand->mode == Addressing_Type);
  3480. if (sel.entity == NULL) {
  3481. gbString type_str = type_to_string(type);
  3482. error_node(arg,
  3483. "`%s` has no field named `%.*s`", type_str, LIT(i->string));
  3484. return false;
  3485. }
  3486. if (sel.indirect) {
  3487. gbString type_str = type_to_string(type);
  3488. error_node(ce->args[0],
  3489. "Field `%.*s` is embedded via a pointer in `%s`", LIT(i->string), type_str);
  3490. gb_string_free(type_str);
  3491. return false;
  3492. }
  3493. operand->mode = Addressing_Constant;
  3494. // IMPORTANT TODO(bill): Fix for anonymous fields
  3495. operand->value = exact_value_i64(type_offset_of_from_selection(c->allocator, type, sel));
  3496. operand->type = t_untyped_integer;
  3497. } break;
  3498. case BuiltinProc_type_of_val:
  3499. // type_of_val :: proc(val: Type) -> type(Type)
  3500. check_assignment(c, operand, NULL, str_lit("argument of `type_of_val`"));
  3501. if (operand->mode == Addressing_Invalid || operand->mode == Addressing_Builtin) {
  3502. return false;
  3503. }
  3504. if (operand->type == NULL || operand->type == t_invalid) {
  3505. error_node(operand->expr, "Invalid argument to `type_of_val`");
  3506. return false;
  3507. }
  3508. operand->mode = Addressing_Type;
  3509. break;
  3510. case BuiltinProc_type_info: {
  3511. // type_info :: proc(Type) -> ^Type_Info
  3512. if (c->context.scope->is_global) {
  3513. compiler_error("`type_info` Cannot be declared within a #shared_global_scope due to how the internals of the compiler works");
  3514. }
  3515. // NOTE(bill): The type information may not be setup yet
  3516. init_preload(c);
  3517. AstNode *expr = ce->args[0];
  3518. Type *type = check_type(c, expr);
  3519. if (type == NULL || type == t_invalid) {
  3520. error_node(expr, "Invalid argument to `type_info`");
  3521. return false;
  3522. }
  3523. add_type_info_type(c, type);
  3524. operand->mode = Addressing_Value;
  3525. operand->type = t_type_info_ptr;
  3526. } break;
  3527. case BuiltinProc_type_info_of_val: {
  3528. // type_info_of_val :: proc(val: Type) -> ^Type_Info
  3529. if (c->context.scope->is_global) {
  3530. compiler_error("`type_info` Cannot be declared within a #shared_global_scope due to how the internals of the compiler works");
  3531. }
  3532. // NOTE(bill): The type information may not be setup yet
  3533. init_preload(c);
  3534. AstNode *expr = ce->args[0];
  3535. check_assignment(c, operand, NULL, str_lit("argument of `type_info_of_val`"));
  3536. if (operand->mode == Addressing_Invalid || operand->mode == Addressing_Builtin)
  3537. return false;
  3538. add_type_info_type(c, operand->type);
  3539. operand->mode = Addressing_Value;
  3540. operand->type = t_type_info_ptr;
  3541. } break;
  3542. case BuiltinProc_compile_assert:
  3543. // compile_assert :: proc(cond: bool) -> bool
  3544. if (!is_type_boolean(operand->type) && operand->mode != Addressing_Constant) {
  3545. gbString str = expr_to_string(ce->args[0]);
  3546. error_node(call, "`%s` is not a constant boolean", str);
  3547. gb_string_free(str);
  3548. return false;
  3549. }
  3550. if (!operand->value.value_bool) {
  3551. gbString str = expr_to_string(ce->args[0]);
  3552. error_node(call, "Compile time assertion: `%s`", str);
  3553. gb_string_free(str);
  3554. }
  3555. operand->mode = Addressing_Constant;
  3556. operand->type = t_untyped_bool;
  3557. break;
  3558. case BuiltinProc_assert:
  3559. // assert :: proc(cond: bool) -> bool
  3560. if (!is_type_boolean(operand->type)) {
  3561. gbString str = expr_to_string(ce->args[0]);
  3562. error_node(call, "`%s` is not a boolean", str);
  3563. gb_string_free(str);
  3564. return false;
  3565. }
  3566. operand->mode = Addressing_Value;
  3567. operand->type = t_untyped_bool;
  3568. break;
  3569. case BuiltinProc_panic:
  3570. // panic :: proc(msg: string)
  3571. if (!is_type_string(operand->type)) {
  3572. gbString str = expr_to_string(ce->args[0]);
  3573. error_node(call, "`%s` is not a string", str);
  3574. gb_string_free(str);
  3575. return false;
  3576. }
  3577. operand->mode = Addressing_NoValue;
  3578. break;
  3579. case BuiltinProc_copy: {
  3580. // copy :: proc(x, y: []Type) -> int
  3581. Type *dest_type = NULL, *src_type = NULL;
  3582. Type *d = base_type(operand->type);
  3583. if (d->kind == Type_Slice) {
  3584. dest_type = d->Slice.elem;
  3585. }
  3586. Operand op = {};
  3587. check_expr(c, &op, ce->args[1]);
  3588. if (op.mode == Addressing_Invalid) {
  3589. return false;
  3590. }
  3591. Type *s = base_type(op.type);
  3592. if (s->kind == Type_Slice) {
  3593. src_type = s->Slice.elem;
  3594. }
  3595. if (dest_type == NULL || src_type == NULL) {
  3596. error_node(call, "`copy` only expects slices as arguments");
  3597. return false;
  3598. }
  3599. if (!are_types_identical(dest_type, src_type)) {
  3600. gbString d_arg = expr_to_string(ce->args[0]);
  3601. gbString s_arg = expr_to_string(ce->args[1]);
  3602. gbString d_str = type_to_string(dest_type);
  3603. gbString s_str = type_to_string(src_type);
  3604. error_node(call,
  3605. "Arguments to `copy`, %s, %s, have different elem types: %s vs %s",
  3606. d_arg, s_arg, d_str, s_str);
  3607. gb_string_free(s_str);
  3608. gb_string_free(d_str);
  3609. gb_string_free(s_arg);
  3610. gb_string_free(d_arg);
  3611. return false;
  3612. }
  3613. operand->type = t_int; // Returns number of elems copied
  3614. operand->mode = Addressing_Value;
  3615. } break;
  3616. case BuiltinProc_swizzle: {
  3617. // swizzle :: proc(v: {N}T, T..) -> {M}T
  3618. Type *vector_type = base_type(operand->type);
  3619. if (!is_type_vector(vector_type)) {
  3620. gbString type_str = type_to_string(operand->type);
  3621. error_node(call,
  3622. "You can only `swizzle` a vector, got `%s`",
  3623. type_str);
  3624. gb_string_free(type_str);
  3625. return false;
  3626. }
  3627. isize max_count = vector_type->Vector.count;
  3628. i128 max_count128 = i128_from_i64(max_count);
  3629. isize arg_count = 0;
  3630. for_array(i, ce->args) {
  3631. if (i == 0) {
  3632. continue;
  3633. }
  3634. AstNode *arg = ce->args[i];
  3635. Operand op = {};
  3636. check_expr(c, &op, arg);
  3637. if (op.mode == Addressing_Invalid) {
  3638. return false;
  3639. }
  3640. Type *arg_type = base_type(op.type);
  3641. if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
  3642. error_node(op.expr, "Indices to `swizzle` must be constant integers");
  3643. return false;
  3644. }
  3645. if (i128_lt(op.value.value_integer, I128_ZERO)) {
  3646. error_node(op.expr, "Negative `swizzle` index");
  3647. return false;
  3648. }
  3649. if (i128_le(max_count128, op.value.value_integer)) {
  3650. error_node(op.expr, "`swizzle` index exceeds vector length");
  3651. return false;
  3652. }
  3653. arg_count++;
  3654. }
  3655. if (arg_count > max_count) {
  3656. error_node(call, "Too many `swizzle` indices, %td > %td", arg_count, max_count);
  3657. return false;
  3658. }
  3659. Type *elem_type = vector_type->Vector.elem;
  3660. operand->type = make_type_vector(c->allocator, elem_type, arg_count);
  3661. operand->mode = Addressing_Value;
  3662. } break;
  3663. case BuiltinProc_complex: {
  3664. // complex :: proc(real, imag: float_type) -> complex_type
  3665. Operand x = *operand;
  3666. Operand y = {};
  3667. // NOTE(bill): Invalid will be the default till fixed
  3668. operand->type = t_invalid;
  3669. operand->mode = Addressing_Invalid;
  3670. check_expr(c, &y, ce->args[1]);
  3671. if (y.mode == Addressing_Invalid) {
  3672. return false;
  3673. }
  3674. convert_to_typed(c, &x, y.type, 0); if (x.mode == Addressing_Invalid) return false;
  3675. convert_to_typed(c, &y, x.type, 0); if (y.mode == Addressing_Invalid) return false;
  3676. if (x.mode == Addressing_Constant &&
  3677. y.mode == Addressing_Constant) {
  3678. if (is_type_numeric(x.type) && exact_value_imag(x.value).value_float == 0) {
  3679. x.type = t_untyped_float;
  3680. }
  3681. if (is_type_numeric(y.type) && exact_value_imag(y.value).value_float == 0) {
  3682. y.type = t_untyped_float;
  3683. }
  3684. }
  3685. if (!are_types_identical(x.type, y.type)) {
  3686. gbString tx = type_to_string(x.type);
  3687. gbString ty = type_to_string(y.type);
  3688. error_node(call, "Mismatched types to `complex`, `%s` vs `%s`", tx, ty);
  3689. gb_string_free(ty);
  3690. gb_string_free(tx);
  3691. return false;
  3692. }
  3693. if (!is_type_float(x.type)) {
  3694. gbString s = type_to_string(x.type);
  3695. error_node(call, "Arguments have type `%s`, expected a floating point", s);
  3696. gb_string_free(s);
  3697. return false;
  3698. }
  3699. if (x.mode == Addressing_Constant && y.mode == Addressing_Constant) {
  3700. operand->value = exact_binary_operator_value(Token_Add, x.value, y.value);
  3701. operand->mode = Addressing_Constant;
  3702. } else {
  3703. operand->mode = Addressing_Value;
  3704. }
  3705. BasicKind kind = core_type(x.type)->Basic.kind;
  3706. switch (kind) {
  3707. // case Basic_f16: operand->type = t_complex32; break;
  3708. case Basic_f32: operand->type = t_complex64; break;
  3709. case Basic_f64: operand->type = t_complex128; break;
  3710. case Basic_UntypedFloat: operand->type = t_untyped_complex; break;
  3711. default: GB_PANIC("Invalid type"); break;
  3712. }
  3713. } break;
  3714. case BuiltinProc_real:
  3715. case BuiltinProc_imag: {
  3716. // real :: proc(x: type) -> float_type
  3717. // imag :: proc(x: type) -> float_type
  3718. Operand *x = operand;
  3719. if (is_type_untyped(x->type)) {
  3720. if (x->mode == Addressing_Constant) {
  3721. if (is_type_numeric(x->type)) {
  3722. x->type = t_untyped_complex;
  3723. }
  3724. } else {
  3725. convert_to_typed(c, x, t_complex128, 0);
  3726. if (x->mode == Addressing_Invalid) {
  3727. return false;
  3728. }
  3729. }
  3730. }
  3731. if (!is_type_complex(x->type)) {
  3732. gbString s = type_to_string(x->type);
  3733. error_node(call, "Argument has type `%s`, expected a complex type", s);
  3734. gb_string_free(s);
  3735. return false;
  3736. }
  3737. if (x->mode == Addressing_Constant) {
  3738. switch (id) {
  3739. case BuiltinProc_real: x->value = exact_value_real(x->value); break;
  3740. case BuiltinProc_imag: x->value = exact_value_imag(x->value); break;
  3741. }
  3742. } else {
  3743. x->mode = Addressing_Value;
  3744. }
  3745. BasicKind kind = core_type(x->type)->Basic.kind;
  3746. switch (kind) {
  3747. case Basic_complex64: x->type = t_f32; break;
  3748. case Basic_complex128: x->type = t_f64; break;
  3749. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  3750. default: GB_PANIC("Invalid type"); break;
  3751. }
  3752. } break;
  3753. case BuiltinProc_conj: {
  3754. // conj :: proc(x: type) -> type
  3755. Operand *x = operand;
  3756. if (is_type_complex(x->type)) {
  3757. if (x->mode == Addressing_Constant) {
  3758. ExactValue v = exact_value_to_complex(x->value);
  3759. f64 r = v.value_complex.real;
  3760. f64 i = v.value_complex.imag;
  3761. x->value = exact_value_complex(r, i);
  3762. x->mode = Addressing_Constant;
  3763. } else {
  3764. x->mode = Addressing_Value;
  3765. }
  3766. } else {
  3767. gbString s = type_to_string(x->type);
  3768. error_node(call, "Expected a complex or quaternion, got `%s`", s);
  3769. gb_string_free(s);
  3770. return false;
  3771. }
  3772. } break;
  3773. case BuiltinProc_slice_ptr: {
  3774. // slice_ptr :: proc(a: ^T, len: int) -> []T
  3775. // slice_ptr :: proc(a: ^T, len, cap: int) -> []T
  3776. // ^T cannot be rawptr
  3777. Type *ptr_type = base_type(operand->type);
  3778. if (!is_type_pointer(ptr_type)) {
  3779. gbString type_str = type_to_string(operand->type);
  3780. error_node(call, "Expected a pointer to `slice_ptr`, got `%s`", type_str);
  3781. gb_string_free(type_str);
  3782. return false;
  3783. }
  3784. if (ptr_type == t_rawptr) {
  3785. error_node(call, "`rawptr` cannot have pointer arithmetic");
  3786. return false;
  3787. }
  3788. isize arg_count = ce->args.count;
  3789. if (arg_count < 2 || 3 < arg_count) {
  3790. error_node(ce->args[0], "`slice_ptr` expects 2 or 3 arguments, found %td", arg_count);
  3791. // NOTE(bill): Return the correct type to reduce errors
  3792. } else {
  3793. // If any are constant
  3794. i64 sizes[2] = {};
  3795. isize size_count = 0;
  3796. for (isize i = 1; i < arg_count; i++) {
  3797. i64 val = 0;
  3798. bool ok = check_index_value(c, false, ce->args[i], -1, &val);
  3799. if (ok && val >= 0) {
  3800. GB_ASSERT(size_count < gb_count_of(sizes));
  3801. sizes[size_count++] = val;
  3802. }
  3803. }
  3804. if (size_count == 2 && sizes[0] > sizes[1]) {
  3805. error_node(ce->args[1], "`slice_ptr` count and capacity are swapped");
  3806. // No need quit
  3807. }
  3808. }
  3809. operand->type = make_type_slice(c->allocator, ptr_type->Pointer.elem);
  3810. operand->mode = Addressing_Value;
  3811. } break;
  3812. case BuiltinProc_slice_to_bytes: {
  3813. // slice_to_bytes :: proc(a: []T) -> []u8
  3814. Type *slice_type = base_type(operand->type);
  3815. if (!is_type_slice(slice_type)) {
  3816. gbString type_str = type_to_string(operand->type);
  3817. error_node(call, "Expected a slice type, got `%s`", type_str);
  3818. gb_string_free(type_str);
  3819. return false;
  3820. }
  3821. operand->type = t_u8_slice;
  3822. operand->mode = Addressing_Value;
  3823. } break;
  3824. case BuiltinProc_min: {
  3825. // min :: proc(a, b: ordered) -> ordered
  3826. Type *type = base_type(operand->type);
  3827. if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  3828. gbString type_str = type_to_string(operand->type);
  3829. error_node(call, "Expected a ordered numeric type to `min`, got `%s`", type_str);
  3830. gb_string_free(type_str);
  3831. return false;
  3832. }
  3833. AstNode *other_arg = ce->args[1];
  3834. Operand a = *operand;
  3835. Operand b = {};
  3836. check_expr(c, &b, other_arg);
  3837. if (b.mode == Addressing_Invalid) {
  3838. return false;
  3839. }
  3840. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  3841. gbString type_str = type_to_string(b.type);
  3842. error_node(call,
  3843. "Expected a ordered numeric type to `min`, got `%s`",
  3844. type_str);
  3845. gb_string_free(type_str);
  3846. return false;
  3847. }
  3848. if (a.mode == Addressing_Constant &&
  3849. b.mode == Addressing_Constant) {
  3850. ExactValue x = a.value;
  3851. ExactValue y = b.value;
  3852. operand->mode = Addressing_Constant;
  3853. if (compare_exact_values(Token_Lt, x, y)) {
  3854. operand->value = x;
  3855. operand->type = a.type;
  3856. } else {
  3857. operand->value = y;
  3858. operand->type = b.type;
  3859. }
  3860. } else {
  3861. operand->mode = Addressing_Value;
  3862. operand->type = type;
  3863. convert_to_typed(c, &a, b.type, 0);
  3864. if (a.mode == Addressing_Invalid) {
  3865. return false;
  3866. }
  3867. convert_to_typed(c, &b, a.type, 0);
  3868. if (b.mode == Addressing_Invalid) {
  3869. return false;
  3870. }
  3871. if (!are_types_identical(a.type, b.type)) {
  3872. gbString type_a = type_to_string(a.type);
  3873. gbString type_b = type_to_string(b.type);
  3874. error_node(call,
  3875. "Mismatched types to `min`, `%s` vs `%s`",
  3876. type_a, type_b);
  3877. gb_string_free(type_b);
  3878. gb_string_free(type_a);
  3879. return false;
  3880. }
  3881. }
  3882. } break;
  3883. case BuiltinProc_max: {
  3884. // min :: proc(a, b: ordered) -> ordered
  3885. Type *type = base_type(operand->type);
  3886. if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  3887. gbString type_str = type_to_string(operand->type);
  3888. error_node(call,
  3889. "Expected a ordered numeric or string type to `max`, got `%s`",
  3890. type_str);
  3891. gb_string_free(type_str);
  3892. return false;
  3893. }
  3894. AstNode *other_arg = ce->args[1];
  3895. Operand a = *operand;
  3896. Operand b = {};
  3897. check_expr(c, &b, other_arg);
  3898. if (b.mode == Addressing_Invalid) {
  3899. return false;
  3900. }
  3901. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  3902. gbString type_str = type_to_string(b.type);
  3903. error_node(call,
  3904. "Expected a ordered numeric or string type to `max`, got `%s`",
  3905. type_str);
  3906. gb_string_free(type_str);
  3907. return false;
  3908. }
  3909. if (a.mode == Addressing_Constant &&
  3910. b.mode == Addressing_Constant) {
  3911. ExactValue x = a.value;
  3912. ExactValue y = b.value;
  3913. operand->mode = Addressing_Constant;
  3914. if (compare_exact_values(Token_Gt, x, y)) {
  3915. operand->value = x;
  3916. operand->type = a.type;
  3917. } else {
  3918. operand->value = y;
  3919. operand->type = b.type;
  3920. }
  3921. } else {
  3922. operand->mode = Addressing_Value;
  3923. operand->type = type;
  3924. convert_to_typed(c, &a, b.type, 0);
  3925. if (a.mode == Addressing_Invalid) {
  3926. return false;
  3927. }
  3928. convert_to_typed(c, &b, a.type, 0);
  3929. if (b.mode == Addressing_Invalid) {
  3930. return false;
  3931. }
  3932. if (!are_types_identical(a.type, b.type)) {
  3933. gbString type_a = type_to_string(a.type);
  3934. gbString type_b = type_to_string(b.type);
  3935. error_node(call,
  3936. "Mismatched types to `max`, `%s` vs `%s`",
  3937. type_a, type_b);
  3938. gb_string_free(type_b);
  3939. gb_string_free(type_a);
  3940. return false;
  3941. }
  3942. }
  3943. } break;
  3944. case BuiltinProc_abs: {
  3945. // abs :: proc(n: numeric) -> numeric
  3946. if (!is_type_numeric(operand->type) && !is_type_vector(operand->type)) {
  3947. gbString type_str = type_to_string(operand->type);
  3948. error_node(call, "Expected a numeric type to `abs`, got `%s`", type_str);
  3949. gb_string_free(type_str);
  3950. return false;
  3951. }
  3952. if (operand->mode == Addressing_Constant) {
  3953. switch (operand->value.kind) {
  3954. case ExactValue_Integer:
  3955. operand->value.value_integer = i128_abs(operand->value.value_integer);
  3956. break;
  3957. case ExactValue_Float:
  3958. operand->value.value_float = gb_abs(operand->value.value_float);
  3959. break;
  3960. case ExactValue_Complex: {
  3961. f64 r = operand->value.value_complex.real;
  3962. f64 i = operand->value.value_complex.imag;
  3963. operand->value = exact_value_float(gb_sqrt(r*r + i*i));
  3964. } break;
  3965. default:
  3966. GB_PANIC("Invalid numeric constant");
  3967. break;
  3968. }
  3969. } else {
  3970. operand->mode = Addressing_Value;
  3971. }
  3972. if (is_type_complex(operand->type)) {
  3973. operand->type = base_complex_elem_type(operand->type);
  3974. }
  3975. GB_ASSERT(!is_type_complex(operand->type));
  3976. } break;
  3977. case BuiltinProc_clamp: {
  3978. // clamp :: proc(a, min, max: ordered) -> ordered
  3979. Type *type = base_type(operand->type);
  3980. if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  3981. gbString type_str = type_to_string(operand->type);
  3982. error_node(call, "Expected a ordered numeric or string type to `clamp`, got `%s`", type_str);
  3983. gb_string_free(type_str);
  3984. return false;
  3985. }
  3986. AstNode *min_arg = ce->args[1];
  3987. AstNode *max_arg = ce->args[2];
  3988. Operand x = *operand;
  3989. Operand y = {};
  3990. Operand z = {};
  3991. check_expr(c, &y, min_arg);
  3992. if (y.mode == Addressing_Invalid) {
  3993. return false;
  3994. }
  3995. if (!is_type_ordered(y.type) || !(is_type_numeric(y.type) || is_type_string(y.type))) {
  3996. gbString type_str = type_to_string(y.type);
  3997. error_node(call, "Expected a ordered numeric or string type to `clamp`, got `%s`", type_str);
  3998. gb_string_free(type_str);
  3999. return false;
  4000. }
  4001. check_expr(c, &z, max_arg);
  4002. if (z.mode == Addressing_Invalid) {
  4003. return false;
  4004. }
  4005. if (!is_type_ordered(z.type) || !(is_type_numeric(z.type) || is_type_string(z.type))) {
  4006. gbString type_str = type_to_string(z.type);
  4007. error_node(call, "Expected a ordered numeric or string type to `clamp`, got `%s`", type_str);
  4008. gb_string_free(type_str);
  4009. return false;
  4010. }
  4011. if (x.mode == Addressing_Constant &&
  4012. y.mode == Addressing_Constant &&
  4013. z.mode == Addressing_Constant) {
  4014. ExactValue a = x.value;
  4015. ExactValue b = y.value;
  4016. ExactValue c = z.value;
  4017. operand->mode = Addressing_Constant;
  4018. if (compare_exact_values(Token_Lt, a, b)) {
  4019. operand->value = b;
  4020. operand->type = y.type;
  4021. } else if (compare_exact_values(Token_Gt, a, c)) {
  4022. operand->value = c;
  4023. operand->type = z.type;
  4024. } else {
  4025. operand->value = a;
  4026. operand->type = x.type;
  4027. }
  4028. } else {
  4029. operand->mode = Addressing_Value;
  4030. operand->type = type;
  4031. convert_to_typed(c, &x, y.type, 0);
  4032. if (x.mode == Addressing_Invalid) { return false; }
  4033. convert_to_typed(c, &y, x.type, 0);
  4034. if (y.mode == Addressing_Invalid) { return false; }
  4035. convert_to_typed(c, &x, z.type, 0);
  4036. if (x.mode == Addressing_Invalid) { return false; }
  4037. convert_to_typed(c, &z, x.type, 0);
  4038. if (z.mode == Addressing_Invalid) { return false; }
  4039. convert_to_typed(c, &y, z.type, 0);
  4040. if (y.mode == Addressing_Invalid) { return false; }
  4041. convert_to_typed(c, &z, y.type, 0);
  4042. if (z.mode == Addressing_Invalid) { return false; }
  4043. if (!are_types_identical(x.type, y.type) || !are_types_identical(x.type, z.type)) {
  4044. gbString type_x = type_to_string(x.type);
  4045. gbString type_y = type_to_string(y.type);
  4046. gbString type_z = type_to_string(z.type);
  4047. error_node(call,
  4048. "Mismatched types to `clamp`, `%s`, `%s`, `%s`",
  4049. type_x, type_y, type_z);
  4050. gb_string_free(type_z);
  4051. gb_string_free(type_y);
  4052. gb_string_free(type_x);
  4053. return false;
  4054. }
  4055. }
  4056. } break;
  4057. case BuiltinProc_transmute: {
  4058. Operand op = {};
  4059. check_expr_or_type(c, &op, ce->args[0]);
  4060. Type *t = op.type;
  4061. if ((op.mode != Addressing_Type && t == NULL) || t == t_invalid) {
  4062. error_node(ce->args[0], "Expected a type for `transmute`");
  4063. return false;
  4064. }
  4065. AstNode *expr = ce->args[1];
  4066. Operand *o = operand;
  4067. check_expr(c, o, expr);
  4068. if (o->mode == Addressing_Invalid) {
  4069. return false;
  4070. }
  4071. if (o->mode == Addressing_Constant) {
  4072. gbString expr_str = expr_to_string(o->expr);
  4073. error_node(o->expr, "Cannot transmute a constant expression: `%s`", expr_str);
  4074. gb_string_free(expr_str);
  4075. o->mode = Addressing_Invalid;
  4076. o->expr = expr;
  4077. return false;
  4078. }
  4079. if (is_type_untyped(o->type)) {
  4080. gbString expr_str = expr_to_string(o->expr);
  4081. error_node(o->expr, "Cannot transmute untyped expression: `%s`", expr_str);
  4082. gb_string_free(expr_str);
  4083. o->mode = Addressing_Invalid;
  4084. o->expr = expr;
  4085. return false;
  4086. }
  4087. i64 srcz = type_size_of(c->allocator, o->type);
  4088. i64 dstz = type_size_of(c->allocator, t);
  4089. if (srcz != dstz) {
  4090. gbString expr_str = expr_to_string(o->expr);
  4091. gbString type_str = type_to_string(t);
  4092. error_node(o->expr, "Cannot transmute `%s` to `%s`, %lld vs %lld bytes", expr_str, type_str, srcz, dstz);
  4093. gb_string_free(type_str);
  4094. gb_string_free(expr_str);
  4095. o->mode = Addressing_Invalid;
  4096. o->expr = expr;
  4097. return false;
  4098. }
  4099. o->mode = Addressing_Value;
  4100. o->type = t;
  4101. } break;
  4102. }
  4103. return true;
  4104. }
  4105. enum CallArgumentError {
  4106. CallArgumentError_None,
  4107. CallArgumentError_WrongTypes,
  4108. CallArgumentError_NonVariadicExpand,
  4109. CallArgumentError_VariadicTuple,
  4110. CallArgumentError_MultipleVariadicExpand,
  4111. CallArgumentError_ArgumentCount,
  4112. CallArgumentError_TooFewArguments,
  4113. CallArgumentError_TooManyArguments,
  4114. CallArgumentError_InvalidFieldValue,
  4115. CallArgumentError_ParameterNotFound,
  4116. CallArgumentError_ParameterMissing,
  4117. CallArgumentError_DuplicateParameter,
  4118. };
  4119. enum CallArgumentErrorMode {
  4120. CallArgumentMode_NoErrors,
  4121. CallArgumentMode_ShowErrors,
  4122. };
  4123. struct ValidProcAndScore {
  4124. isize index;
  4125. i64 score;
  4126. };
  4127. int valid_proc_and_score_cmp(void const *a, void const *b) {
  4128. i64 si = (cast(ValidProcAndScore const *)a)->score;
  4129. i64 sj = (cast(ValidProcAndScore const *)b)->score;
  4130. return sj < si ? -1 : sj > si;
  4131. }
  4132. bool check_unpack_arguments(Checker *c, isize lhs_count, Array<Operand> *operands, Array<AstNode *> rhs, bool allow_ok) {
  4133. bool optional_ok = false;
  4134. for_array(i, rhs) {
  4135. Operand o = {};
  4136. check_multi_expr(c, &o, rhs[i]);
  4137. if (o.type == NULL || o.type->kind != Type_Tuple) {
  4138. if (allow_ok && lhs_count == 2 && rhs.count == 1 &&
  4139. (o.mode == Addressing_MapIndex || o.mode == Addressing_OptionalOk)) {
  4140. Type *tuple = make_optional_ok_type(c->allocator, o.type);
  4141. add_type_and_value(&c->info, o.expr, o.mode, tuple, o.value);
  4142. Operand val = o;
  4143. Operand ok = o;
  4144. val.mode = Addressing_Value;
  4145. ok.mode = Addressing_Value;
  4146. ok.type = t_bool;
  4147. array_add(operands, val);
  4148. array_add(operands, ok);
  4149. optional_ok = true;
  4150. } else {
  4151. array_add(operands, o);
  4152. }
  4153. } else {
  4154. TypeTuple *tuple = &o.type->Tuple;
  4155. for (isize j = 0; j < tuple->variable_count; j++) {
  4156. o.type = tuple->variables[j]->type;
  4157. array_add(operands, o);
  4158. }
  4159. }
  4160. }
  4161. return optional_ok;
  4162. }
  4163. #define CALL_ARGUMENT_CHECKER(name) CallArgumentError name(Checker *c, AstNode *call, Type *proc_type, Array<Operand> operands, CallArgumentErrorMode show_error_mode, i64 *score_)
  4164. typedef CALL_ARGUMENT_CHECKER(CallArgumentCheckerType);
  4165. CALL_ARGUMENT_CHECKER(check_call_arguments_internal) {
  4166. ast_node(ce, CallExpr, call);
  4167. isize param_count = 0;
  4168. isize param_count_excluding_defaults = 0;
  4169. bool variadic = proc_type->Proc.variadic;
  4170. bool vari_expand = (ce->ellipsis.pos.line != 0);
  4171. i64 score = 0;
  4172. bool show_error = show_error_mode == CallArgumentMode_ShowErrors;
  4173. TypeTuple *param_tuple = NULL;
  4174. if (proc_type->Proc.params != NULL) {
  4175. param_tuple = &proc_type->Proc.params->Tuple;
  4176. param_count = param_tuple->variable_count;
  4177. if (variadic) {
  4178. param_count--;
  4179. }
  4180. }
  4181. param_count_excluding_defaults = param_count;
  4182. if (param_tuple != NULL) {
  4183. for (isize i = param_count-1; i >= 0; i--) {
  4184. Entity *e = param_tuple->variables[i];
  4185. GB_ASSERT(e->kind == Entity_Variable);
  4186. if (e->Variable.default_value.kind != ExactValue_Invalid) {
  4187. param_count_excluding_defaults--;
  4188. continue;
  4189. }
  4190. if (e->Variable.default_is_nil) {
  4191. param_count_excluding_defaults--;
  4192. continue;
  4193. }
  4194. break;
  4195. }
  4196. }
  4197. if (vari_expand && !variadic) {
  4198. if (show_error) {
  4199. error(ce->ellipsis,
  4200. "Cannot use `..` in call to a non-variadic procedure: `%.*s`",
  4201. LIT(ce->proc->Ident.string));
  4202. }
  4203. if (score_) *score_ = score;
  4204. return CallArgumentError_NonVariadicExpand;
  4205. }
  4206. if (operands.count == 0 && param_count_excluding_defaults == 0) {
  4207. if (score_) *score_ = score;
  4208. return CallArgumentError_None;
  4209. }
  4210. i32 error_code = 0;
  4211. if (operands.count < param_count_excluding_defaults) {
  4212. error_code = -1;
  4213. } else if (!variadic && operands.count > param_count) {
  4214. error_code = +1;
  4215. }
  4216. if (error_code != 0) {
  4217. CallArgumentError err = CallArgumentError_TooManyArguments;
  4218. char *err_fmt = "Too many arguments for `%s`, expected %td arguments";
  4219. if (error_code < 0) {
  4220. err = CallArgumentError_TooFewArguments;
  4221. err_fmt = "Too few arguments for `%s`, expected %td arguments";
  4222. }
  4223. if (show_error) {
  4224. gbString proc_str = expr_to_string(ce->proc);
  4225. error_node(call, err_fmt, proc_str, param_count_excluding_defaults);
  4226. gb_string_free(proc_str);
  4227. }
  4228. if (score_) *score_ = score;
  4229. return err;
  4230. }
  4231. CallArgumentError err = CallArgumentError_None;
  4232. GB_ASSERT(proc_type->Proc.params != NULL);
  4233. Entity **sig_params = param_tuple->variables;
  4234. isize operand_index = 0;
  4235. isize max_operand_count = gb_min(param_count, operands.count);
  4236. for (; operand_index < max_operand_count; operand_index++) {
  4237. Type *t = sig_params[operand_index]->type;
  4238. Operand o = operands[operand_index];
  4239. if (variadic) {
  4240. o = operands[operand_index];
  4241. }
  4242. i64 s = 0;
  4243. if (!check_is_assignable_to_with_score(c, &o, t, &s)) {
  4244. if (show_error) {
  4245. check_assignment(c, &o, t, str_lit("argument"));
  4246. }
  4247. err = CallArgumentError_WrongTypes;
  4248. }
  4249. score += s;
  4250. }
  4251. if (variadic) {
  4252. bool variadic_expand = false;
  4253. Type *slice = sig_params[param_count]->type;
  4254. GB_ASSERT(is_type_slice(slice));
  4255. Type *elem = base_type(slice)->Slice.elem;
  4256. Type *t = elem;
  4257. for (; operand_index < operands.count; operand_index++) {
  4258. Operand o = operands[operand_index];
  4259. if (vari_expand) {
  4260. variadic_expand = true;
  4261. t = slice;
  4262. if (operand_index != param_count) {
  4263. if (show_error) {
  4264. error_node(o.expr, "`..` in a variadic procedure can only have one variadic argument at the end");
  4265. }
  4266. if (score_) *score_ = score;
  4267. return CallArgumentError_MultipleVariadicExpand;
  4268. }
  4269. }
  4270. i64 s = 0;
  4271. if (!check_is_assignable_to_with_score(c, &o, t, &s)) {
  4272. if (show_error) {
  4273. check_assignment(c, &o, t, str_lit("argument"));
  4274. }
  4275. err = CallArgumentError_WrongTypes;
  4276. }
  4277. score += s;
  4278. }
  4279. }
  4280. if (score_) *score_ = score;
  4281. return err;
  4282. }
  4283. bool is_call_expr_field_value(AstNodeCallExpr *ce) {
  4284. GB_ASSERT(ce != NULL);
  4285. if (ce->args.count == 0) {
  4286. return false;
  4287. }
  4288. return ce->args[0]->kind == AstNode_FieldValue;
  4289. }
  4290. isize lookup_procedure_parameter(TypeProc *pt, String parameter_name) {
  4291. isize param_count = pt->param_count;
  4292. for (isize i = 0; i < param_count; i++) {
  4293. Entity *e = pt->params->Tuple.variables[i];
  4294. String name = e->token.string;
  4295. if (name == "_") {
  4296. continue;
  4297. }
  4298. if (name == parameter_name) {
  4299. return i;
  4300. }
  4301. }
  4302. return -1;
  4303. }
  4304. CALL_ARGUMENT_CHECKER(check_named_call_arguments) {
  4305. ast_node(ce, CallExpr, call);
  4306. GB_ASSERT(is_type_proc(proc_type));
  4307. TypeProc *pt = &base_type(proc_type)->Proc;
  4308. i64 score = 0;
  4309. bool show_error = show_error_mode == CallArgumentMode_ShowErrors;
  4310. CallArgumentError err = CallArgumentError_None;
  4311. isize param_count = pt->param_count;
  4312. bool *params_visited = gb_alloc_array(c->allocator, bool, param_count);
  4313. for_array(i, ce->args) {
  4314. AstNode *arg = ce->args[i];
  4315. ast_node(fv, FieldValue, arg);
  4316. if (fv->field->kind != AstNode_Ident) {
  4317. if (show_error) {
  4318. gbString expr_str = expr_to_string(fv->field);
  4319. error_node(arg, "Invalid parameter name `%s` in procedure call", expr_str);
  4320. gb_string_free(expr_str);
  4321. }
  4322. err = CallArgumentError_InvalidFieldValue;
  4323. continue;
  4324. }
  4325. String name = fv->field->Ident.string;
  4326. isize index = lookup_procedure_parameter(pt, name);
  4327. if (index < 0) {
  4328. if (show_error) {
  4329. error_node(arg, "No parameter named `%.*s` for this procedure type", LIT(name));
  4330. }
  4331. err = CallArgumentError_ParameterNotFound;
  4332. continue;
  4333. }
  4334. if (params_visited[index]) {
  4335. if (show_error) {
  4336. error_node(arg, "Duplicate parameter `%.*s` in procedure call", LIT(name));
  4337. }
  4338. err = CallArgumentError_DuplicateParameter;
  4339. continue;
  4340. }
  4341. params_visited[index] = true;
  4342. Operand *o = &operands[i];
  4343. Type *param_type = pt->params->Tuple.variables[index]->type;
  4344. i64 s = 0;
  4345. if (!check_is_assignable_to_with_score(c, o, param_type, &s)) {
  4346. if (show_error) {
  4347. check_assignment(c, o, param_type, str_lit("procedure argument"));
  4348. }
  4349. err = CallArgumentError_WrongTypes;
  4350. }
  4351. score += s;
  4352. }
  4353. #if 1
  4354. isize param_count_to_check = param_count;
  4355. if (pt->variadic) {
  4356. param_count_to_check--;
  4357. }
  4358. for (isize i = 0; i < param_count_to_check; i++) {
  4359. if (!params_visited[i]) {
  4360. Entity *e = pt->params->Tuple.variables[i];
  4361. if (e->token.string == "_") {
  4362. continue;
  4363. }
  4364. GB_ASSERT(e->kind == Entity_Variable);
  4365. if (e->Variable.default_value.kind != ExactValue_Invalid) {
  4366. score += assign_score_function(1);
  4367. continue;
  4368. }
  4369. if (e->Variable.default_is_nil) {
  4370. score += assign_score_function(1);
  4371. continue;
  4372. }
  4373. if (show_error) {
  4374. gbString str = type_to_string(e->type);
  4375. error_node(call, "Parameter `%.*s` of type `%s` is missing in procedure call",
  4376. LIT(e->token.string), str);
  4377. gb_string_free(str);
  4378. }
  4379. err = CallArgumentError_ParameterMissing;
  4380. }
  4381. }
  4382. #endif
  4383. if (score_) *score_ = score;
  4384. return err;
  4385. }
  4386. Type *check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode *call) {
  4387. ast_node(ce, CallExpr, call);
  4388. CallArgumentCheckerType *call_checker = check_call_arguments_internal;
  4389. Array<Operand> operands = {};
  4390. defer (array_free(&operands));
  4391. if (is_call_expr_field_value(ce)) {
  4392. call_checker = check_named_call_arguments;
  4393. array_init_count(&operands, heap_allocator(), ce->args.count);
  4394. for_array(i, ce->args) {
  4395. AstNode *arg = ce->args[i];
  4396. ast_node(fv, FieldValue, arg);
  4397. check_expr_or_type(c, &operands[i], fv->value);
  4398. }
  4399. bool vari_expand = (ce->ellipsis.pos.line != 0);
  4400. if (vari_expand) {
  4401. error(ce->ellipsis, "Invalid use of `..` with `field = value` call`");
  4402. }
  4403. } else {
  4404. array_init(&operands, heap_allocator(), 2*ce->args.count);
  4405. check_unpack_arguments(c, -1, &operands, ce->args, false);
  4406. }
  4407. if (operand->mode == Addressing_Overload) {
  4408. GB_ASSERT(operand->overload_entities != NULL &&
  4409. operand->overload_count > 0);
  4410. isize overload_count = operand->overload_count;
  4411. Entity ** procs = operand->overload_entities;
  4412. ValidProcAndScore *valids = gb_alloc_array(heap_allocator(), ValidProcAndScore, overload_count);
  4413. isize valid_count = 0;
  4414. defer (gb_free(heap_allocator(), procs));
  4415. defer (gb_free(heap_allocator(), valids));
  4416. String name = procs[0]->token.string;
  4417. for (isize i = 0; i < overload_count; i++) {
  4418. Entity *e = procs[i];
  4419. DeclInfo **found = map_get(&c->info.entities, hash_pointer(e));
  4420. GB_ASSERT(found != NULL);
  4421. DeclInfo *d = *found;
  4422. check_entity_decl(c, e, d, NULL);
  4423. }
  4424. for (isize i = 0; i < overload_count; i++) {
  4425. Entity *p = procs[i];
  4426. Type *proc_type = base_type(p->type);
  4427. if (proc_type != NULL && is_type_proc(proc_type)) {
  4428. i64 score = 0;
  4429. CallArgumentError err = call_checker(c, call, proc_type, operands, CallArgumentMode_NoErrors, &score);
  4430. if (err == CallArgumentError_None) {
  4431. valids[valid_count].index = i;
  4432. valids[valid_count].score = score;
  4433. valid_count++;
  4434. }
  4435. }
  4436. }
  4437. if (valid_count > 1) {
  4438. gb_sort_array(valids, valid_count, valid_proc_and_score_cmp);
  4439. i64 best_score = valids[0].score;
  4440. for (isize i = 0; i < valid_count; i++) {
  4441. if (best_score > valids[i].score) {
  4442. valid_count = i;
  4443. break;
  4444. }
  4445. best_score = valids[i].score;
  4446. }
  4447. }
  4448. if (valid_count == 0) {
  4449. error_node(operand->expr, "No overloads for `%.*s` that match with the given arguments", LIT(name));
  4450. proc_type = t_invalid;
  4451. } else if (valid_count > 1) {
  4452. error_node(operand->expr, "Ambiguous procedure call `%.*s`, could be:", LIT(name));
  4453. for (isize i = 0; i < valid_count; i++) {
  4454. Entity *proc = procs[valids[i].index];
  4455. TokenPos pos = proc->token.pos;
  4456. gbString pt = type_to_string(proc->type);
  4457. gb_printf_err("\t%.*s :: %s at %.*s(%td:%td) with score %lld\n", LIT(name), pt, LIT(pos.file), pos.line, pos.column, valids[i].score);
  4458. gb_string_free(pt);
  4459. }
  4460. proc_type = t_invalid;
  4461. } else {
  4462. AstNode *expr = operand->expr;
  4463. while (expr->kind == AstNode_SelectorExpr) {
  4464. expr = expr->SelectorExpr.selector;
  4465. }
  4466. GB_ASSERT(expr->kind == AstNode_Ident);
  4467. Entity *e = procs[valids[0].index];
  4468. add_entity_use(c, expr, e);
  4469. proc_type = e->type;
  4470. i64 score = 0;
  4471. CallArgumentError err = call_checker(c, call, proc_type, operands, CallArgumentMode_ShowErrors, &score);
  4472. }
  4473. } else {
  4474. i64 score = 0;
  4475. CallArgumentError err = call_checker(c, call, proc_type, operands, CallArgumentMode_ShowErrors, &score);
  4476. }
  4477. return proc_type;
  4478. }
  4479. Entity *find_using_index_expr(Type *t) {
  4480. t = base_type(t);
  4481. if (t->kind != Type_Record) {
  4482. return NULL;
  4483. }
  4484. for (isize i = 0; i < t->Record.field_count; i++) {
  4485. Entity *f = t->Record.fields[i];
  4486. if (f->kind == Entity_Variable &&
  4487. (f->flags & EntityFlag_Field) != 0 &&
  4488. (f->flags & EntityFlag_Using) != 0) {
  4489. if (is_type_indexable(f->type)) {
  4490. return f;
  4491. }
  4492. Entity *res = find_using_index_expr(f->type);
  4493. if (res != NULL) {
  4494. return res;
  4495. }
  4496. }
  4497. }
  4498. return NULL;
  4499. }
  4500. ExprKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
  4501. GB_ASSERT(call->kind == AstNode_CallExpr);
  4502. ast_node(ce, CallExpr, call);
  4503. check_expr_or_type(c, operand, ce->proc);
  4504. if (ce->args.count > 0) {
  4505. bool fail = false;
  4506. bool first_is_field_value = (ce->args[0]->kind == AstNode_FieldValue);
  4507. for_array(i, ce->args) {
  4508. AstNode *arg = ce->args[i];
  4509. bool mix = false;
  4510. if (first_is_field_value) {
  4511. mix = arg->kind != AstNode_FieldValue;
  4512. } else {
  4513. mix = arg->kind == AstNode_FieldValue;
  4514. }
  4515. if (mix) {
  4516. error_node(arg, "Mixture of `field = value` and value elements in a procedure all is not allowed");
  4517. fail = true;
  4518. }
  4519. }
  4520. if (fail) {
  4521. operand->mode = Addressing_Invalid;
  4522. operand->expr = call;
  4523. return Expr_Stmt;
  4524. }
  4525. }
  4526. if (operand->mode == Addressing_Invalid) {
  4527. for_array(i, ce->args) {
  4528. AstNode *arg = ce->args[i];
  4529. if (arg->kind == AstNode_FieldValue) {
  4530. arg = arg->FieldValue.value;
  4531. }
  4532. check_expr_base(c, operand, arg, NULL);
  4533. }
  4534. operand->mode = Addressing_Invalid;
  4535. operand->expr = call;
  4536. return Expr_Stmt;
  4537. }
  4538. if (operand->mode == Addressing_Type) {
  4539. Type *t = operand->type;
  4540. gbString str = type_to_string(t);
  4541. operand->mode = Addressing_Invalid;
  4542. isize arg_count = ce->args.count;
  4543. switch (arg_count) {
  4544. case 0: error_node(call, "Missing argument in conversion to `%s`", str); break;
  4545. default: error_node(call, "Too many arguments in conversion to `%s`", str); break;
  4546. case 1: {
  4547. AstNode *arg = ce->args[0];
  4548. if (arg->kind == AstNode_FieldValue) {
  4549. error_node(call, "`field = value` cannot be used in a type conversion");
  4550. arg = arg->FieldValue.value;
  4551. // NOTE(bill): Carry on the cast regardless
  4552. }
  4553. check_expr(c, operand, arg);
  4554. if (operand->mode != Addressing_Invalid) {
  4555. check_cast(c, operand, t);
  4556. }
  4557. } break;
  4558. }
  4559. gb_string_free(str);
  4560. return Expr_Expr;
  4561. }
  4562. if (operand->mode == Addressing_Builtin) {
  4563. i32 id = operand->builtin_id;
  4564. if (!check_builtin_procedure(c, operand, call, id)) {
  4565. operand->mode = Addressing_Invalid;
  4566. }
  4567. operand->expr = call;
  4568. return builtin_procs[id].kind;
  4569. }
  4570. Type *proc_type = base_type(operand->type);
  4571. if (operand->mode != Addressing_Overload) {
  4572. bool valid_type = (proc_type != NULL) && is_type_proc(proc_type);
  4573. bool valid_mode = is_operand_value(*operand);
  4574. if (!valid_type || !valid_mode) {
  4575. AstNode *e = operand->expr;
  4576. gbString str = expr_to_string(e);
  4577. gbString type_str = type_to_string(operand->type);
  4578. error_node(e, "Cannot call a non-procedure: `%s` of type `%s`", str, type_str);
  4579. gb_string_free(type_str);
  4580. gb_string_free(str);
  4581. operand->mode = Addressing_Invalid;
  4582. operand->expr = call;
  4583. return Expr_Stmt;
  4584. }
  4585. }
  4586. proc_type = check_call_arguments(c, operand, proc_type, call);
  4587. gb_zero_item(operand);
  4588. Type *pt = base_type(proc_type);
  4589. if (pt == NULL || !is_type_proc(pt)) {
  4590. operand->mode = Addressing_Invalid;
  4591. operand->type = t_invalid;
  4592. operand->expr = call;
  4593. return Expr_Stmt;
  4594. }
  4595. switch (pt->Proc.result_count) {
  4596. case 0:
  4597. operand->mode = Addressing_NoValue;
  4598. break;
  4599. case 1:
  4600. operand->mode = Addressing_Value;
  4601. operand->type = pt->Proc.results->Tuple.variables[0]->type;
  4602. break;
  4603. default:
  4604. operand->mode = Addressing_Value;
  4605. operand->type = pt->Proc.results;
  4606. break;
  4607. }
  4608. operand->expr = call;
  4609. return Expr_Expr;
  4610. }
  4611. ExprKind check_macro_call_expr(Checker *c, Operand *operand, AstNode *call) {
  4612. GB_ASSERT(call->kind == AstNode_MacroCallExpr);
  4613. ast_node(mce, MacroCallExpr, call);
  4614. error_node(call, "Macro call expressions are not yet supported");
  4615. operand->mode = Addressing_Invalid;
  4616. operand->expr = call;
  4617. return Expr_Stmt;
  4618. }
  4619. void check_expr_with_type_hint(Checker *c, Operand *o, AstNode *e, Type *t) {
  4620. check_expr_base(c, o, e, t);
  4621. check_not_tuple(c, o);
  4622. char *err_str = NULL;
  4623. switch (o->mode) {
  4624. case Addressing_NoValue:
  4625. err_str = "used as a value";
  4626. break;
  4627. case Addressing_Type:
  4628. err_str = "is not an expression";
  4629. break;
  4630. case Addressing_Builtin:
  4631. err_str = "must be called";
  4632. break;
  4633. }
  4634. if (err_str != NULL) {
  4635. gbString str = expr_to_string(e);
  4636. error_node(e, "`%s` %s", str, err_str);
  4637. gb_string_free(str);
  4638. o->mode = Addressing_Invalid;
  4639. }
  4640. }
  4641. void check_set_mode_with_indirection(Operand *o, bool indirection) {
  4642. if (o->mode != Addressing_Immutable) {
  4643. if (indirection) {
  4644. o->mode = Addressing_Variable;
  4645. } else if (o->mode != Addressing_Variable &&
  4646. o->mode != Addressing_Constant) {
  4647. o->mode = Addressing_Value;
  4648. }
  4649. }
  4650. }
  4651. bool check_set_index_data(Operand *o, Type *type, bool indirection, i64 *max_count) {
  4652. Type *t = base_type(type_deref(type));
  4653. switch (t->kind) {
  4654. case Type_Basic:
  4655. if (is_type_string(t)) {
  4656. if (o->mode == Addressing_Constant) {
  4657. *max_count = o->value.value_string.len;
  4658. }
  4659. check_set_mode_with_indirection(o, indirection);
  4660. o->type = t_u8;
  4661. return true;
  4662. }
  4663. break;
  4664. case Type_Array:
  4665. *max_count = t->Array.count;
  4666. check_set_mode_with_indirection(o, indirection);
  4667. o->type = t->Array.elem;
  4668. return true;
  4669. case Type_Vector:
  4670. *max_count = t->Vector.count;
  4671. check_set_mode_with_indirection(o, indirection);
  4672. o->type = t->Vector.elem;
  4673. return true;
  4674. case Type_Slice:
  4675. o->type = t->Slice.elem;
  4676. if (o->mode != Addressing_Immutable) {
  4677. o->mode = Addressing_Variable;
  4678. }
  4679. return true;
  4680. case Type_DynamicArray:
  4681. o->type = t->DynamicArray.elem;
  4682. check_set_mode_with_indirection(o, indirection);
  4683. return true;
  4684. }
  4685. return false;
  4686. }
  4687. ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  4688. ExprKind kind = Expr_Stmt;
  4689. o->mode = Addressing_Invalid;
  4690. o->type = t_invalid;
  4691. switch (node->kind) {
  4692. default:
  4693. return kind;
  4694. case_ast_node(be, BadExpr, node)
  4695. return kind;
  4696. case_end;
  4697. case_ast_node(i, Implicit, node)
  4698. switch (i->kind) {
  4699. case Token_context:
  4700. if (c->context.proc_name.len == 0) {
  4701. error_node(node, "`context` is only allowed within procedures");
  4702. return kind;
  4703. }
  4704. o->mode = Addressing_Value;
  4705. o->type = t_context;
  4706. break;
  4707. default:
  4708. error_node(node, "Illegal implicit name `%.*s`", LIT(i->string));
  4709. return kind;
  4710. }
  4711. case_end;
  4712. case_ast_node(i, Ident, node);
  4713. check_ident(c, o, node, NULL, type_hint, false);
  4714. case_end;
  4715. case_ast_node(bl, BasicLit, node);
  4716. Type *t = t_invalid;
  4717. switch (bl->kind) {
  4718. case Token_Integer: t = t_untyped_integer; break;
  4719. case Token_Float: t = t_untyped_float; break;
  4720. case Token_String: t = t_untyped_string; break;
  4721. case Token_Rune: t = t_untyped_rune; break;
  4722. case Token_Imag: {
  4723. String s = bl->string;
  4724. Rune r = s[s.len-1];
  4725. switch (r) {
  4726. case 'i': t = t_untyped_complex; break;
  4727. }
  4728. } break;
  4729. default: GB_PANIC("Unknown literal"); break;
  4730. }
  4731. o->mode = Addressing_Constant;
  4732. o->type = t;
  4733. o->value = exact_value_from_basic_literal(*bl);
  4734. case_end;
  4735. case_ast_node(bd, BasicDirective, node);
  4736. if (bd->name == "file") {
  4737. o->type = t_untyped_string;
  4738. o->value = exact_value_string(bd->token.pos.file);
  4739. } else if (bd->name == "line") {
  4740. o->type = t_untyped_integer;
  4741. o->value = exact_value_i64(bd->token.pos.line);
  4742. } else if (bd->name == "procedure") {
  4743. if (c->proc_stack.count == 0) {
  4744. error_node(node, "#procedure may only be used within procedures");
  4745. o->type = t_untyped_string;
  4746. o->value = exact_value_string(str_lit(""));
  4747. } else {
  4748. o->type = t_untyped_string;
  4749. o->value = exact_value_string(c->context.proc_name);
  4750. }
  4751. } else {
  4752. GB_PANIC("Unknown basic basic directive");
  4753. }
  4754. o->mode = Addressing_Constant;
  4755. case_end;
  4756. case_ast_node(pl, ProcLit, node);
  4757. CheckerContext prev_context = c->context;
  4758. DeclInfo *decl = NULL;
  4759. Type *type = alloc_type(c->allocator, Type_Proc);
  4760. check_open_scope(c, pl->type);
  4761. {
  4762. decl = make_declaration_info(c->allocator, c->context.scope, c->context.decl);
  4763. decl->proc_decl = node;
  4764. c->context.decl = decl;
  4765. if (pl->tags != 0) {
  4766. error_node(node, "A procedure literal cannot have tags");
  4767. pl->tags = 0; // TODO(bill): Should I zero this?!
  4768. }
  4769. check_procedure_type(c, type, pl->type);
  4770. if (!is_type_proc(type)) {
  4771. gbString str = expr_to_string(node);
  4772. error_node(node, "Invalid procedure literal `%s`", str);
  4773. gb_string_free(str);
  4774. check_close_scope(c);
  4775. return kind;
  4776. }
  4777. check_procedure_later(c, c->curr_ast_file, empty_token, decl, type, pl->body, pl->tags);
  4778. }
  4779. check_close_scope(c);
  4780. c->context = prev_context;
  4781. o->mode = Addressing_Value;
  4782. o->type = type;
  4783. case_end;
  4784. case_ast_node(te, TernaryExpr, node);
  4785. Operand cond = {Addressing_Invalid};
  4786. check_expr(c, &cond, te->cond);
  4787. if (cond.mode != Addressing_Invalid && !is_type_boolean(cond.type)) {
  4788. error_node(te->cond, "Non-boolean condition in if expression");
  4789. }
  4790. Operand x = {Addressing_Invalid};
  4791. Operand y = {Addressing_Invalid};
  4792. check_expr_with_type_hint(c, &x, te->x, type_hint);
  4793. if (te->y != NULL) {
  4794. check_expr_with_type_hint(c, &y, te->y, type_hint);
  4795. } else {
  4796. error_node(node, "A ternary expression must have an else clause");
  4797. return kind;
  4798. }
  4799. if (x.type == NULL || x.type == t_invalid ||
  4800. y.type == NULL || y.type == t_invalid) {
  4801. return kind;
  4802. }
  4803. convert_to_typed(c, &x, y.type, 0);
  4804. if (x.mode == Addressing_Invalid) {
  4805. return kind;
  4806. }
  4807. convert_to_typed(c, &y, x.type, 0);
  4808. if (y.mode == Addressing_Invalid) {
  4809. x.mode = Addressing_Invalid;
  4810. return kind;
  4811. }
  4812. if (!are_types_identical(x.type, y.type)) {
  4813. gbString its = type_to_string(x.type);
  4814. gbString ets = type_to_string(y.type);
  4815. error_node(node, "Mismatched types in ternary expression, %s vs %s", its, ets);
  4816. gb_string_free(ets);
  4817. gb_string_free(its);
  4818. return kind;
  4819. }
  4820. o->type = x.type;
  4821. o->mode = Addressing_Value;
  4822. if (cond.mode == Addressing_Constant && is_type_boolean(cond.type) &&
  4823. x.mode == Addressing_Constant &&
  4824. y.mode == Addressing_Constant) {
  4825. o->mode = Addressing_Constant;
  4826. if (cond.value.value_bool) {
  4827. o->value = x.value;
  4828. } else {
  4829. o->value = y.value;
  4830. }
  4831. }
  4832. case_end;
  4833. case_ast_node(cl, CompoundLit, node);
  4834. Type *type = type_hint;
  4835. bool is_to_be_determined_array_count = false;
  4836. bool is_constant = true;
  4837. if (cl->type != NULL) {
  4838. type = NULL;
  4839. // [..]Type
  4840. if (cl->type->kind == AstNode_ArrayType && cl->type->ArrayType.count != NULL) {
  4841. AstNode *count = cl->type->ArrayType.count;
  4842. if (count->kind == AstNode_UnaryExpr &&
  4843. count->UnaryExpr.op.kind == Token_Ellipsis) {
  4844. type = make_type_array(c->allocator, check_type(c, cl->type->ArrayType.elem), -1);
  4845. is_to_be_determined_array_count = true;
  4846. }
  4847. }
  4848. if (type == NULL) {
  4849. type = check_type(c, cl->type);
  4850. }
  4851. }
  4852. if (type == NULL) {
  4853. error_node(node, "Missing type in compound literal");
  4854. return kind;
  4855. }
  4856. Type *t = base_type(type);
  4857. switch (t->kind) {
  4858. case Type_Record: {
  4859. if (!is_type_struct(t) && !is_type_union(t)) {
  4860. if (cl->elems.count != 0) {
  4861. gbString type_str = type_to_string(type);
  4862. error_node(node, "Illegal compound literal type `%s`", type_str);
  4863. gb_string_free(type_str);
  4864. }
  4865. break;
  4866. }
  4867. if (is_type_union(t)) {
  4868. is_constant = false;
  4869. }
  4870. if (cl->elems.count == 0) {
  4871. break; // NOTE(bill): No need to init
  4872. }
  4873. { // Checker values
  4874. isize field_count = t->Record.field_count;
  4875. if (cl->elems[0]->kind == AstNode_FieldValue) {
  4876. bool *fields_visited = gb_alloc_array(c->allocator, bool, field_count);
  4877. for_array(i, cl->elems) {
  4878. AstNode *elem = cl->elems[i];
  4879. if (elem->kind != AstNode_FieldValue) {
  4880. error_node(elem, "Mixture of `field = value` and value elements in a structure literal is not allowed");
  4881. continue;
  4882. }
  4883. ast_node(fv, FieldValue, elem);
  4884. if (fv->field->kind != AstNode_Ident) {
  4885. gbString expr_str = expr_to_string(fv->field);
  4886. error_node(elem, "Invalid field name `%s` in structure literal", expr_str);
  4887. gb_string_free(expr_str);
  4888. continue;
  4889. }
  4890. String name = fv->field->Ident.string;
  4891. Selection sel = lookup_field(c->allocator, type, name, o->mode == Addressing_Type);
  4892. bool is_unknown = sel.entity == NULL;
  4893. if (is_unknown) {
  4894. error_node(elem, "Unknown field `%.*s` in structure literal", LIT(name));
  4895. continue;
  4896. }
  4897. if (!is_unknown && !check_is_field_exported(c, sel.entity)) {
  4898. error_node(elem, "Cannot assign to an unexported field `%.*s` in structure literal", LIT(name));
  4899. continue;
  4900. }
  4901. if (sel.index.count > 1) {
  4902. error_node(elem, "Cannot assign to an anonymous field `%.*s` in a structure literal (at the moment)", LIT(name));
  4903. continue;
  4904. }
  4905. Entity *field = t->Record.fields[sel.index[0]];
  4906. add_entity_use(c, fv->field, field);
  4907. if (fields_visited[sel.index[0]]) {
  4908. error_node(elem, "Duplicate field `%.*s` in structure literal", LIT(name));
  4909. continue;
  4910. }
  4911. fields_visited[sel.index[0]] = true;
  4912. check_expr(c, o, fv->value);
  4913. if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type)) {
  4914. is_constant = false;
  4915. }
  4916. if (is_constant) {
  4917. is_constant = o->mode == Addressing_Constant;
  4918. }
  4919. check_assignment(c, o, field->type, str_lit("structure literal"));
  4920. }
  4921. } else {
  4922. bool all_fields_are_blank = true;
  4923. for (isize i = 0; i < t->Record.field_count; i++) {
  4924. Entity *field = t->Record.fields_in_src_order[i];
  4925. if (field->token.string != "_") {
  4926. all_fields_are_blank = false;
  4927. break;
  4928. }
  4929. }
  4930. for_array(index, cl->elems) {
  4931. AstNode *elem = cl->elems[index];
  4932. if (elem->kind == AstNode_FieldValue) {
  4933. error_node(elem, "Mixture of `field = value` and value elements in a structure literal is not allowed");
  4934. continue;
  4935. }
  4936. if (index >= field_count) {
  4937. error_node(o->expr, "Too many values in structure literal, expected %td", field_count);
  4938. break;
  4939. }
  4940. Entity *field = t->Record.fields_in_src_order[index];
  4941. if (!all_fields_are_blank && field->token.string == "_") {
  4942. // NOTE(bill): Ignore blank identifiers
  4943. continue;
  4944. }
  4945. check_expr(c, o, elem);
  4946. if (!check_is_field_exported(c, field)) {
  4947. gbString t = type_to_string(type);
  4948. error_node(o->expr, "Implicit assignment to an unexported field `%.*s` in `%s` literal",
  4949. LIT(field->token.string), t);
  4950. gb_string_free(t);
  4951. continue;
  4952. }
  4953. if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type)) {
  4954. is_constant = false;
  4955. }
  4956. if (is_constant) {
  4957. is_constant = o->mode == Addressing_Constant;
  4958. }
  4959. check_assignment(c, o, field->type, str_lit("structure literal"));
  4960. }
  4961. if (cl->elems.count < field_count) {
  4962. error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count);
  4963. }
  4964. }
  4965. }
  4966. } break;
  4967. case Type_Slice:
  4968. case Type_Array:
  4969. case Type_Vector:
  4970. case Type_DynamicArray:
  4971. {
  4972. Type *elem_type = NULL;
  4973. String context_name = {};
  4974. i64 max_type_count = -1;
  4975. if (t->kind == Type_Slice) {
  4976. elem_type = t->Slice.elem;
  4977. context_name = str_lit("slice literal");
  4978. } else if (t->kind == Type_Vector) {
  4979. elem_type = t->Vector.elem;
  4980. context_name = str_lit("vector literal");
  4981. max_type_count = t->Vector.count;
  4982. } else if (t->kind == Type_Array) {
  4983. elem_type = t->Array.elem;
  4984. context_name = str_lit("array literal");
  4985. max_type_count = t->Array.count;
  4986. } else if (t->kind == Type_DynamicArray) {
  4987. elem_type = t->DynamicArray.elem;
  4988. context_name = str_lit("dynamic array literal");
  4989. is_constant = false;
  4990. } else {
  4991. GB_PANIC("unreachable");
  4992. }
  4993. i64 max = 0;
  4994. isize index = 0;
  4995. isize elem_count = cl->elems.count;
  4996. if (is_type_any(base_type(elem_type))) {
  4997. is_constant = false;
  4998. }
  4999. for (; index < elem_count; index++) {
  5000. GB_ASSERT(cl->elems.data != NULL);
  5001. AstNode *e = cl->elems[index];
  5002. if (e == NULL) {
  5003. error_node(node, "Invalid literal element");
  5004. continue;
  5005. }
  5006. if (e->kind == AstNode_FieldValue) {
  5007. error_node(e, "`field = value` is only allowed in struct literals");
  5008. continue;
  5009. }
  5010. if (0 <= max_type_count && max_type_count <= index) {
  5011. error_node(e, "Index %lld is out of bounds (>= %lld) for %.*s", index, max_type_count, LIT(context_name));
  5012. }
  5013. Operand operand = {};
  5014. check_expr_with_type_hint(c, &operand, e, elem_type);
  5015. check_assignment(c, &operand, elem_type, context_name);
  5016. if (is_constant) {
  5017. is_constant = operand.mode == Addressing_Constant;
  5018. }
  5019. }
  5020. if (max < index) {
  5021. max = index;
  5022. }
  5023. if (t->kind == Type_Vector) {
  5024. if (t->Vector.count > 1 && gb_is_between(index, 2, t->Vector.count-1)) {
  5025. error_node(cl->elems[0], "Expected either 1 (broadcast) or %td elements in vector literal, got %td", t->Vector.count, index);
  5026. }
  5027. }
  5028. if (t->kind == Type_Array && is_to_be_determined_array_count) {
  5029. t->Array.count = max;
  5030. }
  5031. } break;
  5032. case Type_Basic: {
  5033. if (!is_type_any(t)) {
  5034. if (cl->elems.count != 0) {
  5035. error_node(node, "Illegal compound literal");
  5036. }
  5037. break;
  5038. }
  5039. if (cl->elems.count == 0) {
  5040. break; // NOTE(bill): No need to init
  5041. }
  5042. { // Checker values
  5043. Type *field_types[2] = {t_rawptr, t_type_info_ptr};
  5044. isize field_count = 2;
  5045. if (cl->elems[0]->kind == AstNode_FieldValue) {
  5046. bool fields_visited[2] = {};
  5047. for_array(i, cl->elems) {
  5048. AstNode *elem = cl->elems[i];
  5049. if (elem->kind != AstNode_FieldValue) {
  5050. error_node(elem, "Mixture of `field = value` and value elements in a `any` literal is not allowed");
  5051. continue;
  5052. }
  5053. ast_node(fv, FieldValue, elem);
  5054. if (fv->field->kind != AstNode_Ident) {
  5055. gbString expr_str = expr_to_string(fv->field);
  5056. error_node(elem, "Invalid field name `%s` in `any` literal", expr_str);
  5057. gb_string_free(expr_str);
  5058. continue;
  5059. }
  5060. String name = fv->field->Ident.string;
  5061. Selection sel = lookup_field(c->allocator, type, name, o->mode == Addressing_Type);
  5062. if (sel.entity == NULL) {
  5063. error_node(elem, "Unknown field `%.*s` in `any` literal", LIT(name));
  5064. continue;
  5065. }
  5066. isize index = sel.index[0];
  5067. if (fields_visited[index]) {
  5068. error_node(elem, "Duplicate field `%.*s` in `any` literal", LIT(name));
  5069. continue;
  5070. }
  5071. fields_visited[index] = true;
  5072. check_expr(c, o, fv->value);
  5073. // NOTE(bill): `any` literals can never be constant
  5074. is_constant = false;
  5075. check_assignment(c, o, field_types[index], str_lit("`any` literal"));
  5076. }
  5077. } else {
  5078. for_array(index, cl->elems) {
  5079. AstNode *elem = cl->elems[index];
  5080. if (elem->kind == AstNode_FieldValue) {
  5081. error_node(elem, "Mixture of `field = value` and value elements in a `any` literal is not allowed");
  5082. continue;
  5083. }
  5084. check_expr(c, o, elem);
  5085. if (index >= field_count) {
  5086. error_node(o->expr, "Too many values in `any` literal, expected %td", field_count);
  5087. break;
  5088. }
  5089. // NOTE(bill): `any` literals can never be constant
  5090. is_constant = false;
  5091. check_assignment(c, o, field_types[index], str_lit("`any` literal"));
  5092. }
  5093. if (cl->elems.count < field_count) {
  5094. error(cl->close, "Too few values in `any` literal, expected %td, got %td", field_count, cl->elems.count);
  5095. }
  5096. }
  5097. }
  5098. } break;
  5099. case Type_Map: {
  5100. if (cl->elems.count == 0) {
  5101. break;
  5102. }
  5103. is_constant = false;
  5104. { // Checker values
  5105. for_array(i, cl->elems) {
  5106. AstNode *elem = cl->elems[i];
  5107. if (elem->kind != AstNode_FieldValue) {
  5108. error_node(elem, "Only `field = value` elements are allowed in a map literal");
  5109. continue;
  5110. }
  5111. ast_node(fv, FieldValue, elem);
  5112. check_expr_with_type_hint(c, o, fv->field, t->Map.key);
  5113. check_assignment(c, o, t->Map.key, str_lit("map literal"));
  5114. if (o->mode == Addressing_Invalid) {
  5115. continue;
  5116. }
  5117. check_expr_with_type_hint(c, o, fv->value, t->Map.value);
  5118. check_assignment(c, o, t->Map.value, str_lit("map literal"));
  5119. }
  5120. }
  5121. } break;
  5122. default: {
  5123. gbString str = type_to_string(type);
  5124. error_node(node, "Invalid compound literal type `%s`", str);
  5125. gb_string_free(str);
  5126. return kind;
  5127. } break;
  5128. }
  5129. if (is_constant) {
  5130. o->mode = Addressing_Constant;
  5131. o->value = exact_value_compound(node);
  5132. } else {
  5133. o->mode = Addressing_Value;
  5134. }
  5135. o->type = type;
  5136. case_end;
  5137. case_ast_node(pe, ParenExpr, node);
  5138. kind = check_expr_base(c, o, pe->expr, type_hint);
  5139. o->expr = node;
  5140. case_end;
  5141. case_ast_node(te, TagExpr, node);
  5142. String name = te->name.string;
  5143. error_node(node, "Unknown tag expression, #%.*s", LIT(name));
  5144. if (te->expr) {
  5145. kind = check_expr_base(c, o, te->expr, type_hint);
  5146. }
  5147. o->expr = node;
  5148. case_end;
  5149. case_ast_node(re, RunExpr, node);
  5150. // TODO(bill): Tag expressions
  5151. kind = check_expr_base(c, o, re->expr, type_hint);
  5152. o->expr = node;
  5153. case_end;
  5154. case_ast_node(ta, TypeAssertion, node);
  5155. check_expr(c, o, ta->expr);
  5156. if (o->mode == Addressing_Invalid) {
  5157. o->expr = node;
  5158. return kind;
  5159. }
  5160. Type *t = check_type(c, ta->type);
  5161. if (o->mode == Addressing_Constant) {
  5162. gbString expr_str = expr_to_string(o->expr);
  5163. error_node(o->expr, "A type assertion cannot be applied to a constant expression: `%s`", expr_str);
  5164. gb_string_free(expr_str);
  5165. o->mode = Addressing_Invalid;
  5166. o->expr = node;
  5167. return kind;
  5168. }
  5169. if (is_type_untyped(o->type)) {
  5170. gbString expr_str = expr_to_string(o->expr);
  5171. error_node(o->expr, "A type assertion cannot be applied to an untyped expression: `%s`", expr_str);
  5172. gb_string_free(expr_str);
  5173. o->mode = Addressing_Invalid;
  5174. o->expr = node;
  5175. return kind;
  5176. }
  5177. bool src_is_ptr = is_type_pointer(o->type);
  5178. bool dst_is_ptr = is_type_pointer(t);
  5179. Type *src = type_deref(o->type);
  5180. Type *dst = type_deref(t);
  5181. Type *bsrc = base_type(src);
  5182. Type *bdst = base_type(dst);
  5183. if (src_is_ptr != dst_is_ptr) {
  5184. gbString src_type_str = type_to_string(o->type);
  5185. gbString dst_type_str = type_to_string(t);
  5186. error_node(o->expr, "Invalid type assertion types: `%s` and `%s`", src_type_str, dst_type_str);
  5187. gb_string_free(dst_type_str);
  5188. gb_string_free(src_type_str);
  5189. o->mode = Addressing_Invalid;
  5190. o->expr = node;
  5191. return kind;
  5192. }
  5193. if (is_type_union(src)) {
  5194. bool ok = false;
  5195. for (isize i = 1; i < bsrc->Record.variant_count; i++) {
  5196. Entity *f = bsrc->Record.variants[i];
  5197. if (are_types_identical(f->type, dst)) {
  5198. ok = true;
  5199. break;
  5200. }
  5201. }
  5202. if (!ok) {
  5203. gbString expr_str = expr_to_string(o->expr);
  5204. gbString dst_type_str = type_to_string(t);
  5205. error_node(o->expr, "Cannot type assert `%s` to `%s`", expr_str, dst_type_str);
  5206. gb_string_free(dst_type_str);
  5207. gb_string_free(expr_str);
  5208. o->mode = Addressing_Invalid;
  5209. o->expr = node;
  5210. return kind;
  5211. }
  5212. add_type_info_type(c, o->type);
  5213. add_type_info_type(c, t);
  5214. o->type = t;
  5215. o->mode = Addressing_OptionalOk;
  5216. } else if (is_type_any(o->type)) {
  5217. o->type = t;
  5218. o->mode = Addressing_OptionalOk;
  5219. add_type_info_type(c, o->type);
  5220. add_type_info_type(c, t);
  5221. } else {
  5222. error_node(o->expr, "Type assertions can only operate on unions");
  5223. o->mode = Addressing_Invalid;
  5224. o->expr = node;
  5225. return kind;
  5226. }
  5227. case_end;
  5228. case_ast_node(ue, UnaryExpr, node);
  5229. check_expr_base(c, o, ue->expr, type_hint);
  5230. if (o->mode == Addressing_Invalid) {
  5231. o->expr = node;
  5232. return kind;
  5233. }
  5234. check_unary_expr(c, o, ue->op, node);
  5235. if (o->mode == Addressing_Invalid) {
  5236. o->expr = node;
  5237. return kind;
  5238. }
  5239. case_end;
  5240. case_ast_node(be, BinaryExpr, node);
  5241. check_binary_expr(c, o, node);
  5242. if (o->mode == Addressing_Invalid) {
  5243. o->expr = node;
  5244. return kind;
  5245. }
  5246. case_end;
  5247. case_ast_node(se, SelectorExpr, node);
  5248. check_selector(c, o, node, type_hint);
  5249. case_end;
  5250. case_ast_node(ie, IndexExpr, node);
  5251. check_expr(c, o, ie->expr);
  5252. if (o->mode == Addressing_Invalid) {
  5253. o->expr = node;
  5254. return kind;
  5255. }
  5256. Type *t = base_type(type_deref(o->type));
  5257. bool is_ptr = is_type_pointer(o->type);
  5258. bool is_const = o->mode == Addressing_Constant;
  5259. if (is_type_map(t)) {
  5260. Operand key = {};
  5261. check_expr(c, &key, ie->index);
  5262. check_assignment(c, &key, t->Map.key, str_lit("map index"));
  5263. if (key.mode == Addressing_Invalid) {
  5264. o->mode = Addressing_Invalid;
  5265. o->expr = node;
  5266. return kind;
  5267. }
  5268. o->mode = Addressing_MapIndex;
  5269. o->type = t->Map.value;
  5270. o->expr = node;
  5271. return Expr_Expr;
  5272. }
  5273. i64 max_count = -1;
  5274. bool valid = check_set_index_data(o, t, is_ptr, &max_count);
  5275. if (is_const) {
  5276. valid = false;
  5277. }
  5278. if (!valid && (is_type_struct(t) || is_type_raw_union(t))) {
  5279. Entity *found = find_using_index_expr(t);
  5280. if (found != NULL) {
  5281. valid = check_set_index_data(o, found->type, is_type_pointer(found->type), &max_count);
  5282. }
  5283. }
  5284. if (!valid) {
  5285. gbString str = expr_to_string(o->expr);
  5286. if (is_const) {
  5287. error_node(o->expr, "Cannot index a constant `%s`", str);
  5288. } else {
  5289. error_node(o->expr, "Cannot index `%s`", str);
  5290. }
  5291. gb_string_free(str);
  5292. o->mode = Addressing_Invalid;
  5293. o->expr = node;
  5294. return kind;
  5295. }
  5296. if (ie->index == NULL) {
  5297. gbString str = expr_to_string(o->expr);
  5298. error_node(o->expr, "Missing index for `%s`", str);
  5299. gb_string_free(str);
  5300. o->mode = Addressing_Invalid;
  5301. o->expr = node;
  5302. return kind;
  5303. }
  5304. i64 index = 0;
  5305. bool ok = check_index_value(c, false, ie->index, max_count, &index);
  5306. case_end;
  5307. case_ast_node(se, SliceExpr, node);
  5308. check_expr(c, o, se->expr);
  5309. if (o->mode == Addressing_Invalid) {
  5310. o->mode = Addressing_Invalid;
  5311. o->expr = node;
  5312. return kind;
  5313. }
  5314. bool valid = false;
  5315. i64 max_count = -1;
  5316. Type *t = base_type(type_deref(o->type));
  5317. switch (t->kind) {
  5318. case Type_Basic:
  5319. if (is_type_string(t)) {
  5320. if (se->index3) {
  5321. error_node(node, "3-index slice on a string in not needed");
  5322. o->mode = Addressing_Invalid;
  5323. o->expr = node;
  5324. return kind;
  5325. }
  5326. valid = true;
  5327. if (o->mode == Addressing_Constant) {
  5328. max_count = o->value.value_string.len;
  5329. }
  5330. o->type = t_string;
  5331. }
  5332. break;
  5333. case Type_Array:
  5334. valid = true;
  5335. max_count = t->Array.count;
  5336. if (o->mode != Addressing_Variable) {
  5337. gbString str = expr_to_string(node);
  5338. error_node(node, "Cannot slice array `%s`, value is not addressable", str);
  5339. gb_string_free(str);
  5340. o->mode = Addressing_Invalid;
  5341. o->expr = node;
  5342. return kind;
  5343. }
  5344. o->type = make_type_slice(c->allocator, t->Array.elem);
  5345. break;
  5346. case Type_Slice:
  5347. valid = true;
  5348. break;
  5349. case Type_DynamicArray:
  5350. valid = true;
  5351. o->type = make_type_slice(c->allocator, t->DynamicArray.elem);
  5352. break;
  5353. }
  5354. if (!valid) {
  5355. gbString str = expr_to_string(o->expr);
  5356. error_node(o->expr, "Cannot slice `%s`", str);
  5357. gb_string_free(str);
  5358. o->mode = Addressing_Invalid;
  5359. o->expr = node;
  5360. return kind;
  5361. }
  5362. if (o->mode != Addressing_Immutable) {
  5363. o->mode = Addressing_Value;
  5364. }
  5365. if (se->low == NULL && se->high != NULL) {
  5366. error(se->interval0, "1st index is required if a 2nd index is specified");
  5367. // It is okay to continue as it will assume the 1st index is zero
  5368. }
  5369. if (se->index3 && (se->high == NULL || se->max == NULL)) {
  5370. error(se->close, "2nd and 3rd indices are required in a 3-index slice");
  5371. o->mode = Addressing_Invalid;
  5372. o->expr = node;
  5373. return kind;
  5374. }
  5375. if (se->index3 && se->interval0.kind != se->interval1.kind) {
  5376. error(se->close, "The interval separators for in a 3-index slice must be the same");
  5377. o->mode = Addressing_Invalid;
  5378. o->expr = node;
  5379. return kind;
  5380. }
  5381. TokenKind interval_kind = se->interval0.kind;
  5382. i64 indices[2] = {};
  5383. AstNode *nodes[3] = {se->low, se->high, se->max};
  5384. for (isize i = 0; i < gb_count_of(nodes); i++) {
  5385. i64 index = max_count;
  5386. if (nodes[i] != NULL) {
  5387. i64 capacity = -1;
  5388. if (max_count >= 0) {
  5389. capacity = max_count;
  5390. }
  5391. i64 j = 0;
  5392. if (check_index_value(c, interval_kind == Token_Ellipsis, nodes[i], capacity, &j)) {
  5393. index = j;
  5394. }
  5395. } else if (i == 0) {
  5396. index = 0;
  5397. }
  5398. indices[i] = index;
  5399. }
  5400. for (isize i = 0; i < gb_count_of(indices); i++) {
  5401. i64 a = indices[i];
  5402. for (isize j = i+1; j < gb_count_of(indices); j++) {
  5403. i64 b = indices[j];
  5404. if (a > b && b >= 0) {
  5405. error(se->close, "Invalid slice indices: [%td > %td]", a, b);
  5406. }
  5407. }
  5408. }
  5409. case_end;
  5410. case_ast_node(ce, CallExpr, node);
  5411. return check_call_expr(c, o, node);
  5412. case_end;
  5413. case_ast_node(ce, MacroCallExpr, node);
  5414. return check_macro_call_expr(c, o, node);
  5415. case_end;
  5416. case_ast_node(de, DerefExpr, node);
  5417. check_expr_or_type(c, o, de->expr);
  5418. if (o->mode == Addressing_Invalid) {
  5419. o->mode = Addressing_Invalid;
  5420. o->expr = node;
  5421. return kind;
  5422. } else {
  5423. Type *t = base_type(o->type);
  5424. if (t->kind == Type_Pointer) {
  5425. if (o->mode != Addressing_Immutable) {
  5426. o->mode = Addressing_Variable;
  5427. }
  5428. o->type = t->Pointer.elem;
  5429. } else {
  5430. gbString str = expr_to_string(o->expr);
  5431. error_node(o->expr, "Cannot dereference `%s`", str);
  5432. gb_string_free(str);
  5433. o->mode = Addressing_Invalid;
  5434. o->expr = node;
  5435. return kind;
  5436. }
  5437. }
  5438. case_end;
  5439. case AstNode_HelperType:
  5440. case AstNode_ProcType:
  5441. case AstNode_PointerType:
  5442. case AstNode_ArrayType:
  5443. case AstNode_DynamicArrayType:
  5444. case AstNode_VectorType:
  5445. case AstNode_StructType:
  5446. case AstNode_UnionType:
  5447. case AstNode_RawUnionType:
  5448. case AstNode_EnumType:
  5449. case AstNode_MapType:
  5450. o->mode = Addressing_Type;
  5451. o->type = check_type(c, node);
  5452. break;
  5453. }
  5454. kind = Expr_Expr;
  5455. o->expr = node;
  5456. return kind;
  5457. }
  5458. ExprKind check_expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
  5459. ExprKind kind = check_expr_base_internal(c, o, node, type_hint);
  5460. Type *type = NULL;
  5461. ExactValue value = {ExactValue_Invalid};
  5462. switch (o->mode) {
  5463. case Addressing_Invalid:
  5464. type = t_invalid;
  5465. break;
  5466. case Addressing_NoValue:
  5467. type = NULL;
  5468. break;
  5469. case Addressing_Constant:
  5470. type = o->type;
  5471. value = o->value;
  5472. break;
  5473. default:
  5474. type = o->type;
  5475. break;
  5476. }
  5477. if (type != NULL && is_type_untyped(type)) {
  5478. add_untyped(&c->info, node, false, o->mode, type, value);
  5479. } else {
  5480. add_type_and_value(&c->info, node, o->mode, type, value);
  5481. }
  5482. return kind;
  5483. }
  5484. void check_multi_expr(Checker *c, Operand *o, AstNode *e) {
  5485. check_expr_base(c, o, e, NULL);
  5486. switch (o->mode) {
  5487. default:
  5488. return; // NOTE(bill): Valid
  5489. case Addressing_NoValue:
  5490. error_operand_no_value(o);
  5491. break;
  5492. case Addressing_Type:
  5493. error_operand_not_expression(o);
  5494. break;
  5495. }
  5496. o->mode = Addressing_Invalid;
  5497. }
  5498. void check_not_tuple(Checker *c, Operand *o) {
  5499. if (o->mode == Addressing_Value) {
  5500. // NOTE(bill): Tuples are not first class thus never named
  5501. if (o->type->kind == Type_Tuple) {
  5502. isize count = o->type->Tuple.variable_count;
  5503. GB_ASSERT(count != 1);
  5504. error_node(o->expr,
  5505. "%td-valued tuple found where single value expected", count);
  5506. o->mode = Addressing_Invalid;
  5507. }
  5508. }
  5509. }
  5510. void check_expr(Checker *c, Operand *o, AstNode *e) {
  5511. check_multi_expr(c, o, e);
  5512. check_not_tuple(c, o);
  5513. }
  5514. void check_expr_or_type(Checker *c, Operand *o, AstNode *e) {
  5515. check_expr_base(c, o, e, NULL);
  5516. check_not_tuple(c, o);
  5517. error_operand_no_value(o);
  5518. }
  5519. gbString write_expr_to_string(gbString str, AstNode *node);
  5520. gbString write_record_fields_to_string(gbString str, Array<AstNode *> params) {
  5521. for_array(i, params) {
  5522. if (i > 0) {
  5523. str = gb_string_appendc(str, ", ");
  5524. }
  5525. str = write_expr_to_string(str, params[i]);
  5526. }
  5527. return str;
  5528. }
  5529. gbString string_append_token(gbString str, Token token) {
  5530. if (token.string.len > 0) {
  5531. return gb_string_append_length(str, &token.string[0], token.string.len);
  5532. }
  5533. return str;
  5534. }
  5535. gbString write_expr_to_string(gbString str, AstNode *node) {
  5536. if (node == NULL)
  5537. return str;
  5538. if (is_ast_node_stmt(node)) {
  5539. GB_ASSERT("stmt passed to write_expr_to_string");
  5540. }
  5541. switch (node->kind) {
  5542. default:
  5543. str = gb_string_appendc(str, "(BadExpr)");
  5544. break;
  5545. case_ast_node(i, Ident, node);
  5546. str = string_append_token(str, *i);
  5547. case_end;
  5548. case_ast_node(i, Implicit, node);
  5549. str = string_append_token(str, *i);
  5550. case_end;
  5551. case_ast_node(bl, BasicLit, node);
  5552. str = string_append_token(str, *bl);
  5553. case_end;
  5554. case_ast_node(bd, BasicDirective, node);
  5555. str = gb_string_appendc(str, "#");
  5556. str = gb_string_append_length(str, &bd->name[0], bd->name.len);
  5557. case_end;
  5558. case_ast_node(pl, ProcLit, node);
  5559. str = write_expr_to_string(str, pl->type);
  5560. case_end;
  5561. case_ast_node(cl, CompoundLit, node);
  5562. str = write_expr_to_string(str, cl->type);
  5563. str = gb_string_appendc(str, "{");
  5564. for_array(i, cl->elems) {
  5565. if (i > 0) {
  5566. str = gb_string_appendc(str, ", ");
  5567. }
  5568. str = write_expr_to_string(str, cl->elems[i]);
  5569. }
  5570. str = gb_string_appendc(str, "}");
  5571. case_end;
  5572. case_ast_node(te, TagExpr, node);
  5573. str = gb_string_appendc(str, "#");
  5574. str = string_append_token(str, te->name);
  5575. str = write_expr_to_string(str, te->expr);
  5576. case_end;
  5577. case_ast_node(ue, UnaryExpr, node);
  5578. str = string_append_token(str, ue->op);
  5579. str = write_expr_to_string(str, ue->expr);
  5580. case_end;
  5581. case_ast_node(de, DerefExpr, node);
  5582. str = write_expr_to_string(str, de->expr);
  5583. str = gb_string_appendc(str, "^");
  5584. case_end;
  5585. case_ast_node(be, BinaryExpr, node);
  5586. str = write_expr_to_string(str, be->left);
  5587. str = gb_string_appendc(str, " ");
  5588. str = string_append_token(str, be->op);
  5589. str = gb_string_appendc(str, " ");
  5590. str = write_expr_to_string(str, be->right);
  5591. case_end;
  5592. case_ast_node(pe, ParenExpr, node);
  5593. str = gb_string_appendc(str, "(");
  5594. str = write_expr_to_string(str, pe->expr);
  5595. str = gb_string_appendc(str, ")");
  5596. case_end;
  5597. case_ast_node(se, SelectorExpr, node);
  5598. str = write_expr_to_string(str, se->expr);
  5599. str = gb_string_appendc(str, ".");
  5600. str = write_expr_to_string(str, se->selector);
  5601. case_end;
  5602. case_ast_node(ta, TypeAssertion, node);
  5603. str = write_expr_to_string(str, ta->expr);
  5604. str = gb_string_appendc(str, ".(");
  5605. str = write_expr_to_string(str, ta->type);
  5606. str = gb_string_appendc(str, ")");
  5607. case_end;
  5608. case_ast_node(ie, IndexExpr, node);
  5609. str = write_expr_to_string(str, ie->expr);
  5610. str = gb_string_appendc(str, "[");
  5611. str = write_expr_to_string(str, ie->index);
  5612. str = gb_string_appendc(str, "]");
  5613. case_end;
  5614. case_ast_node(se, SliceExpr, node);
  5615. str = write_expr_to_string(str, se->expr);
  5616. str = gb_string_appendc(str, "[");
  5617. str = write_expr_to_string(str, se->low);
  5618. str = gb_string_appendc(str, "..");
  5619. str = write_expr_to_string(str, se->high);
  5620. if (se->index3) {
  5621. str = gb_string_appendc(str, "..");
  5622. str = write_expr_to_string(str, se->max);
  5623. }
  5624. str = gb_string_appendc(str, "]");
  5625. case_end;
  5626. case_ast_node(e, Ellipsis, node);
  5627. str = gb_string_appendc(str, "..");
  5628. case_end;
  5629. case_ast_node(fv, FieldValue, node);
  5630. str = write_expr_to_string(str, fv->field);
  5631. str = gb_string_appendc(str, " = ");
  5632. str = write_expr_to_string(str, fv->value);
  5633. case_end;
  5634. case_ast_node(pt, PointerType, node);
  5635. str = gb_string_appendc(str, "^");
  5636. str = write_expr_to_string(str, pt->type);
  5637. case_end;
  5638. case_ast_node(at, ArrayType, node);
  5639. str = gb_string_appendc(str, "[");
  5640. if (at->count != NULL &&
  5641. at->count->kind == AstNode_UnaryExpr &&
  5642. at->count->UnaryExpr.op.kind == Token_Ellipsis) {
  5643. str = gb_string_appendc(str, "..");
  5644. } else {
  5645. str = write_expr_to_string(str, at->count);
  5646. }
  5647. str = gb_string_appendc(str, "]");
  5648. str = write_expr_to_string(str, at->elem);
  5649. case_end;
  5650. case_ast_node(at, DynamicArrayType, node);
  5651. str = gb_string_appendc(str, "[..]");
  5652. str = write_expr_to_string(str, at->elem);
  5653. case_end;
  5654. case_ast_node(vt, VectorType, node);
  5655. str = gb_string_appendc(str, "[vector ");
  5656. str = write_expr_to_string(str, vt->count);
  5657. str = gb_string_appendc(str, "]");
  5658. str = write_expr_to_string(str, vt->elem);
  5659. case_end;
  5660. case_ast_node(f, Field, node);
  5661. if (f->flags&FieldFlag_using) {
  5662. str = gb_string_appendc(str, "using ");
  5663. }
  5664. if (f->flags&FieldFlag_no_alias) {
  5665. str = gb_string_appendc(str, "#no_alias ");
  5666. }
  5667. for_array(i, f->names) {
  5668. AstNode *name = f->names[i];
  5669. if (i > 0) {
  5670. str = gb_string_appendc(str, ", ");
  5671. }
  5672. str = write_expr_to_string(str, name);
  5673. }
  5674. if (f->names.count > 0) {
  5675. str = gb_string_appendc(str, ": ");
  5676. }
  5677. if (f->flags&FieldFlag_ellipsis) {
  5678. str = gb_string_appendc(str, "..");
  5679. }
  5680. str = write_expr_to_string(str, f->type);
  5681. case_end;
  5682. case_ast_node(f, FieldList, node);
  5683. for_array(i, f->list) {
  5684. if (i > 0) {
  5685. str = gb_string_appendc(str, ", ");
  5686. }
  5687. str = write_expr_to_string(str, f->list[i]);
  5688. }
  5689. case_end;
  5690. case_ast_node(f, UnionField, node);
  5691. str = write_expr_to_string(str, f->name);
  5692. str = gb_string_appendc(str, "{");
  5693. str = write_expr_to_string(str, f->list);
  5694. str = gb_string_appendc(str, "}");
  5695. case_end;
  5696. case_ast_node(ce, CallExpr, node);
  5697. str = write_expr_to_string(str, ce->proc);
  5698. str = gb_string_appendc(str, "(");
  5699. for_array(i, ce->args) {
  5700. AstNode *arg = ce->args[i];
  5701. if (i > 0) {
  5702. str = gb_string_appendc(str, ", ");
  5703. }
  5704. str = write_expr_to_string(str, arg);
  5705. }
  5706. str = gb_string_appendc(str, ")");
  5707. case_end;
  5708. case_ast_node(pt, ProcType, node);
  5709. str = gb_string_appendc(str, "proc(");
  5710. str = write_expr_to_string(str, pt->params);
  5711. str = gb_string_appendc(str, ")");
  5712. case_end;
  5713. case_ast_node(st, StructType, node);
  5714. str = gb_string_appendc(str, "struct ");
  5715. if (st->is_packed) str = gb_string_appendc(str, "#packed ");
  5716. if (st->is_ordered) str = gb_string_appendc(str, "#ordered ");
  5717. str = gb_string_appendc(str, "{");
  5718. str = write_record_fields_to_string(str, st->fields);
  5719. str = gb_string_appendc(str, "}");
  5720. case_end;
  5721. case_ast_node(st, RawUnionType, node);
  5722. str = gb_string_appendc(str, "raw_union ");
  5723. str = gb_string_appendc(str, "{");
  5724. str = write_record_fields_to_string(str, st->fields);
  5725. str = gb_string_appendc(str, "}");
  5726. case_end;
  5727. case_ast_node(st, UnionType, node);
  5728. str = gb_string_appendc(str, "union ");
  5729. str = gb_string_appendc(str, "{");
  5730. str = write_record_fields_to_string(str, st->fields);
  5731. str = gb_string_appendc(str, "}");
  5732. case_end;
  5733. case_ast_node(et, EnumType, node);
  5734. str = gb_string_appendc(str, "enum ");
  5735. if (et->base_type != NULL) {
  5736. str = write_expr_to_string(str, et->base_type);
  5737. str = gb_string_appendc(str, " ");
  5738. }
  5739. str = gb_string_appendc(str, "{");
  5740. for_array(i, et->fields) {
  5741. if (i > 0) {
  5742. str = gb_string_appendc(str, ", ");
  5743. }
  5744. str = write_expr_to_string(str, et->fields[i]);
  5745. }
  5746. str = gb_string_appendc(str, "}");
  5747. case_end;
  5748. case_ast_node(ht, HelperType, node);
  5749. str = gb_string_appendc(str, "#type ");
  5750. str = write_expr_to_string(str, ht->type);
  5751. case_end;
  5752. case_ast_node(at, AtomicType, node);
  5753. str = gb_string_appendc(str, "atomic ");
  5754. str = write_expr_to_string(str, at->type);
  5755. case_end;
  5756. }
  5757. return str;
  5758. }
  5759. gbString expr_to_string(AstNode *expression) {
  5760. return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression);
  5761. }