check_expr.cpp 183 KB

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