12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602 |
- enum CallArgumentError {
- CallArgumentError_None,
- CallArgumentError_NoneProcedureType,
- CallArgumentError_WrongTypes,
- CallArgumentError_NonVariadicExpand,
- CallArgumentError_VariadicTuple,
- CallArgumentError_MultipleVariadicExpand,
- CallArgumentError_ArgumentCount,
- CallArgumentError_TooFewArguments,
- CallArgumentError_TooManyArguments,
- CallArgumentError_InvalidFieldValue,
- CallArgumentError_ParameterNotFound,
- CallArgumentError_ParameterMissing,
- CallArgumentError_DuplicateParameter,
- CallArgumentError_NoneConstantParameter,
- };
- enum CallArgumentErrorMode {
- CallArgumentMode_NoErrors,
- CallArgumentMode_ShowErrors,
- };
- struct CallArgumentData {
- Entity *gen_entity;
- i64 score;
- Type * result_type;
- };
- struct PolyProcData {
- Entity * gen_entity;
- ProcInfo proc_info;
- };
- struct ValidIndexAndScore {
- isize index;
- i64 score;
- };
- int valid_index_and_score_cmp(void const *a, void const *b) {
- i64 si = (cast(ValidIndexAndScore const *)a)->score;
- i64 sj = (cast(ValidIndexAndScore const *)b)->score;
- return sj < si ? -1 : sj > si;
- }
- #define CALL_ARGUMENT_CHECKER(name) CallArgumentError name(CheckerContext *c, Ast *call, Type *proc_type, Entity *entity, Array<Operand> operands, CallArgumentErrorMode show_error_mode, CallArgumentData *data)
- typedef CALL_ARGUMENT_CHECKER(CallArgumentCheckerType);
- void check_expr (CheckerContext *c, Operand *operand, Ast *expression);
- void check_multi_expr (CheckerContext *c, Operand *operand, Ast *expression);
- void check_expr_or_type (CheckerContext *c, Operand *operand, Ast *expression, Type *type_hint = nullptr);
- ExprKind check_expr_base (CheckerContext *c, Operand *operand, Ast *expression, Type *type_hint);
- void check_expr_with_type_hint (CheckerContext *c, Operand *o, Ast *e, Type *t);
- Type * check_type (CheckerContext *c, Ast *expression);
- Type * check_type_expr (CheckerContext *c, Ast *expression, Type *named_type);
- Type * make_optional_ok_type (Type *value);
- void check_type_decl (CheckerContext *c, Entity *e, Ast *type_expr, Type *def);
- Entity * check_selector (CheckerContext *c, Operand *operand, Ast *node, Type *type_hint);
- Entity * check_ident (CheckerContext *c, Operand *o, Ast *n, Type *named_type, Type *type_hint, bool allow_import_name);
- Entity * find_polymorphic_struct_entity (CheckerContext *c, Type *original_type, isize param_count, Array<Operand> ordered_operands);
- void check_not_tuple (CheckerContext *c, Operand *operand);
- void convert_to_typed (CheckerContext *c, Operand *operand, Type *target_type);
- gbString expr_to_string (Ast *expression);
- void check_entity_decl (CheckerContext *c, Entity *e, DeclInfo *decl, Type *named_type);
- void check_const_decl (CheckerContext *c, Entity *e, Ast *type_expr, Ast *init_expr, Type *named_type);
- void check_proc_body (CheckerContext *c, Token token, DeclInfo *decl, Type *type, Ast *body);
- void update_expr_type (CheckerContext *c, Ast *e, Type *type, bool final);
- bool check_is_terminating (Ast *node);
- bool check_has_break (Ast *stmt, bool implicit);
- void check_stmt (CheckerContext *c, Ast *node, u32 flags);
- void check_stmt_list (CheckerContext *c, Array<Ast *> const &stmts, u32 flags);
- void check_init_constant (CheckerContext *c, Entity *e, Operand *operand);
- bool check_representable_as_constant(CheckerContext *c, ExactValue in_value, Type *type, ExactValue *out_value);
- bool check_procedure_type (CheckerContext *c, Type *type, Ast *proc_type_node, Array<Operand> *operands = nullptr);
- void check_struct_type (CheckerContext *c, Type *struct_type, Ast *node, Array<Operand> *poly_operands,
- Type *named_type = nullptr, Type *original_type_for_poly = nullptr);
- CallArgumentData check_call_arguments (CheckerContext *c, Operand *operand, Type *proc_type, Ast *call);
- Type * check_init_variable (CheckerContext *c, Entity *e, Operand *operand, String context_name);
- void error_operand_not_expression(Operand *o) {
- if (o->mode == Addressing_Type) {
- gbString err = expr_to_string(o->expr);
- error(o->expr, "'%s' is not an expression but a type", err);
- gb_string_free(err);
- o->mode = Addressing_Invalid;
- }
- }
- void error_operand_no_value(Operand *o) {
- if (o->mode == Addressing_NoValue) {
- gbString err = expr_to_string(o->expr);
- Ast *x = unparen_expr(o->expr);
- if (x->kind == Ast_CallExpr) {
- error(o->expr, "'%s' call does not return a value and cannot be used as a value", err);
- } else {
- error(o->expr, "'%s' used as a value", err);
- }
- gb_string_free(err);
- o->mode = Addressing_Invalid;
- }
- }
- void check_scope_decls(CheckerContext *c, Array<Ast *> const &nodes, isize reserve_size) {
- Scope *s = c->scope;
- check_collect_entities(c, nodes);
- for_array(i, s->elements.entries) {
- Entity *e = s->elements.entries[i].value;
- switch (e->kind) {
- case Entity_Constant:
- case Entity_TypeName:
- case Entity_Procedure:
- break;
- default:
- continue;
- }
- DeclInfo *d = decl_info_of_entity(e);
- if (d != nullptr) {
- check_entity_decl(c, e, d, nullptr);
- }
- }
- }
- bool check_is_assignable_to_using_subtype(Type *src, Type *dst) {
- bool src_is_ptr = false;
- Type *prev_src = src;
- src = type_deref(src);
- src_is_ptr = src != prev_src;
- src = base_type(src);
- if (!is_type_struct(src)) {
- return false;
- }
- for_array(i, src->Struct.fields) {
- Entity *f = src->Struct.fields[i];
- if (f->kind != Entity_Variable || (f->flags&EntityFlag_Using) == 0) {
- continue;
- }
- if (are_types_identical(f->type, dst)) {
- return true;
- }
- if (src_is_ptr && is_type_pointer(dst)) {
- if (are_types_identical(f->type, type_deref(dst))) {
- return true;
- }
- }
- bool ok = check_is_assignable_to_using_subtype(f->type, dst);
- if (ok) {
- return true;
- }
- }
- return false;
- }
- bool find_or_generate_polymorphic_procedure(CheckerContext *c, Entity *base_entity, Type *type,
- Array<Operand> *param_operands, Ast *poly_def_node, PolyProcData *poly_proc_data) {
- ///////////////////////////////////////////////////////////////////////////////
- // //
- // TODO CLEANUP(bill): This procedure is very messy and hacky. Clean this!!! //
- // //
- ///////////////////////////////////////////////////////////////////////////////
- if (base_entity == nullptr) {
- return false;
- }
- if (!is_type_proc(base_entity->type)) {
- return false;
- }
- String name = base_entity->token.string;
- Type *src = base_type(base_entity->type);
- Type *dst = nullptr;
- if (type != nullptr) dst = base_type(type);
- if (param_operands == nullptr) {
- GB_ASSERT(dst != nullptr);
- }
- if (param_operands != nullptr) {
- GB_ASSERT(dst == nullptr);
- }
- if (!src->Proc.is_polymorphic || src->Proc.is_poly_specialized) {
- return false;
- }
- if (dst != nullptr) {
- if (dst->Proc.is_polymorphic) {
- return false;
- }
- if (dst->Proc.param_count != src->Proc.param_count ||
- dst->Proc.result_count != src->Proc.result_count) {
- return false;
- }
- }
- DeclInfo *old_decl = decl_info_of_entity(base_entity);
- if (old_decl == nullptr) {
- return false;
- }
- gbAllocator a = heap_allocator();
- Array<Operand> operands = {};
- if (param_operands) {
- operands = *param_operands;
- } else {
- operands = array_make<Operand>(a, 0, dst->Proc.param_count);
- for (isize i = 0; i < dst->Proc.param_count; i++) {
- Entity *param = dst->Proc.params->Tuple.variables[i];
- Operand o = {Addressing_Value};
- o.type = param->type;
- array_add(&operands, o);
- }
- }
- defer (if (param_operands == nullptr) {
- array_free(&operands);
- });
- CheckerContext nctx = *c;
- Scope *scope = create_scope(base_entity->scope, a);
- scope->flags |= ScopeFlag_Proc;
- nctx.scope = scope;
- nctx.allow_polymorphic_types = true;
- if (nctx.polymorphic_scope == nullptr) {
- nctx.polymorphic_scope = scope;
- }
- if (param_operands == nullptr) {
- // c->no_polymorphic_errors = false;
- }
- bool generate_type_again = nctx.no_polymorphic_errors;
- auto *pt = &src->Proc;
- // NOTE(bill): This is slightly memory leaking if the type already exists
- // Maybe it's better to check with the previous types first?
- Type *final_proc_type = alloc_type_proc(scope, nullptr, 0, nullptr, 0, false, pt->calling_convention);
- bool success = check_procedure_type(&nctx, final_proc_type, pt->node, &operands);
- if (!success) {
- return false;
- }
- auto *found_gen_procs = map_get(&nctx.info->gen_procs, hash_pointer(base_entity->identifier));
- if (found_gen_procs) {
- auto procs = *found_gen_procs;
- for_array(i, procs) {
- Entity *other = procs[i];
- Type *pt = base_type(other->type);
- if (are_types_identical(pt, final_proc_type)) {
- if (poly_proc_data) {
- poly_proc_data->gen_entity = other;
- }
- return true;
- }
- }
- }
- if (generate_type_again) {
- // LEAK TODO(bill): This is technically a memory leak as it has to generate the type twice
- bool prev_no_polymorphic_errors = nctx.no_polymorphic_errors;
- defer (nctx.no_polymorphic_errors = prev_no_polymorphic_errors);
- nctx.no_polymorphic_errors = false;
- // NOTE(bill): Reset scope from the failed procedure type
- scope_reset(scope);
- success = check_procedure_type(&nctx, final_proc_type, pt->node, &operands);
- if (!success) {
- return false;
- }
- if (found_gen_procs) {
- auto procs = *found_gen_procs;
- for_array(i, procs) {
- Entity *other = procs[i];
- Type *pt = base_type(other->type);
- if (are_types_identical(pt, final_proc_type)) {
- if (poly_proc_data) {
- poly_proc_data->gen_entity = other;
- }
- return true;
- }
- }
- }
- }
- Ast *proc_lit = clone_ast(old_decl->proc_lit);
- ast_node(pl, ProcLit, proc_lit);
- // NOTE(bill): Associate the scope declared above withinth this procedure declaration's type
- add_scope(&nctx, pl->type, final_proc_type->Proc.scope);
- final_proc_type->Proc.is_poly_specialized = true;
- final_proc_type->Proc.is_polymorphic = true;
- for (isize i = 0; i < operands.count; i++) {
- Operand o = operands[i];
- if (final_proc_type == o.type ||
- base_entity->type == o.type) {
- // NOTE(bill): Cycle
- final_proc_type->Proc.is_poly_specialized = false;
- break;
- }
- }
- u64 tags = base_entity->Procedure.tags;
- Ast *ident = clone_ast(base_entity->identifier);
- Token token = ident->Ident.token;
- DeclInfo *d = make_decl_info(nctx.allocator, scope, old_decl->parent);
- d->gen_proc_type = final_proc_type;
- d->type_expr = pl->type;
- d->proc_lit = proc_lit;
- Entity *entity = alloc_entity_procedure(nullptr, token, final_proc_type, tags);
- entity->identifier = ident;
- add_entity_and_decl_info(&nctx, ident, entity, d);
- // NOTE(bill): Set the scope afterwards as this is not real overloading
- entity->scope = scope->parent;
- entity->pkg = base_entity->pkg;
- AstFile *file = nullptr;
- {
- Scope *s = entity->scope;
- while (s != nullptr && s->file == nullptr) {
- file = s->file;
- s = s->parent;
- }
- }
- ProcInfo proc_info = {};
- proc_info.file = file;
- proc_info.token = token;
- proc_info.decl = d;
- proc_info.type = final_proc_type;
- proc_info.body = pl->body;
- proc_info.tags = tags;
- proc_info.generated_from_polymorphic = true;
- proc_info.poly_def_node = poly_def_node;
- if (found_gen_procs) {
- array_add(found_gen_procs, entity);
- } else {
- auto array = array_make<Entity *>(heap_allocator());
- array_add(&array, entity);
- map_set(&nctx.checker->info.gen_procs, hash_pointer(base_entity->identifier), array);
- }
- GB_ASSERT(entity != nullptr);
- if (poly_proc_data) {
- poly_proc_data->gen_entity = entity;
- poly_proc_data->proc_info = proc_info;
- }
- // NOTE(bill): Check the newly generated procedure body
- check_procedure_later(nctx.checker, proc_info);
- return true;
- }
- bool check_polymorphic_procedure_assignment(CheckerContext *c, Operand *operand, Type *type, Ast *poly_def_node, PolyProcData *poly_proc_data) {
- if (operand->expr == nullptr) return false;
- Entity *base_entity = entity_of_ident(operand->expr);
- if (base_entity == nullptr) return false;
- return find_or_generate_polymorphic_procedure(c, base_entity, type, nullptr, poly_def_node, poly_proc_data);
- }
- bool find_or_generate_polymorphic_procedure_from_parameters(CheckerContext *c, Entity *base_entity, Array<Operand> *operands, Ast *poly_def_node, PolyProcData *poly_proc_data) {
- return find_or_generate_polymorphic_procedure(c, base_entity, nullptr, operands, poly_def_node, poly_proc_data);
- }
- bool check_type_specialization_to(CheckerContext *c, Type *specialization, Type *type, bool compound, bool modify_type);
- bool is_polymorphic_type_assignable(CheckerContext *c, Type *poly, Type *source, bool compound, bool modify_type);
- bool check_cast_internal(CheckerContext *c, Operand *x, Type *type);
- i64 check_distance_between_types(CheckerContext *c, Operand *operand, Type *type) {
- if (operand->mode == Addressing_Invalid ||
- type == t_invalid) {
- return -1;
- }
- if (operand->mode == Addressing_Builtin) {
- return -1;
- }
- if (operand->mode == Addressing_Type) {
- return -1;
- }
- Type *s = operand->type;
- if (are_types_identical(s, type)) {
- return 0;
- }
- Type *src = base_type(s);
- Type *dst = base_type(type);
- if (is_type_untyped_undef(src)) {
- if (type_has_undef(dst)) {
- return 1;
- }
- return -1;
- }
- if (is_type_untyped_nil(src)) {
- if (type_has_nil(dst)) {
- return 1;
- }
- return -1;
- }
- if (is_type_untyped(src)) {
- if (is_type_any(dst)) {
- // NOTE(bill): Anything can cast to 'Any'
- add_type_info_type(c, s);
- return 10;
- }
- if (dst->kind == Type_Basic) {
- if (operand->mode == Addressing_Constant) {
- if (check_representable_as_constant(c, operand->value, dst, nullptr)) {
- if (is_type_typed(dst) && src->kind == Type_Basic) {
- switch (src->Basic.kind) {
- case Basic_UntypedRune:
- if (is_type_integer(dst) || is_type_rune(dst)) {
- return 1;
- }
- break;
- case Basic_UntypedInteger:
- if (is_type_integer(dst) || is_type_rune(dst)) {
- return 1;
- }
- break;
- case Basic_UntypedFloat:
- if (is_type_float(dst)) {
- return 1;
- }
- break;
- case Basic_UntypedComplex:
- if (is_type_complex(dst)) {
- return 1;
- }
- break;
- }
- }
- return 2;
- }
- return -1;
- }
- if (src->kind == Type_Basic && src->Basic.kind == Basic_UntypedRune) {
- if (is_type_integer(dst) || is_type_rune(dst)) {
- if (is_type_typed(type)) {
- return 2;
- }
- return 1;
- }
- return -1;
- }
- if (src->kind == Type_Basic && src->Basic.kind == Basic_UntypedBool) {
- if (is_type_boolean(dst)) {
- if (is_type_typed(type)) {
- return 2;
- }
- return 1;
- }
- return -1;
- }
- }
- }
- if (is_type_enum(dst) && are_types_identical(dst->Enum.base_type, operand->type)) {
- if (c->in_enum_type) {
- return 3;
- }
- }
- #if 0
- if (are_types_identical(dst, src) && (!is_type_named(dst) || !is_type_named(src))) {
- return 1;
- }
- #endif
- if (is_type_bit_field_value(operand->type) && is_type_integer(type)) {
- Type *bfv = base_type(operand->type);
- i32 bits = bfv->BitFieldValue.bits;
- i32 size = next_pow2((bits+7)/8);
- i32 dst_size = cast(i32)type_size_of(type);
- i32 diff = gb_abs(dst_size - size);
- // TODO(bill): figure out a decent rule here
- return 1;
- }
- if (check_is_assignable_to_using_subtype(operand->type, type)) {
- return 4;
- }
- // ^T <- rawptr
- #if 0
- // TODO(bill): Should C-style (not C++) pointer cast be allowed?
- if (is_type_pointer(dst) && is_type_rawptr(src)) {
- return true;
- }
- #endif
- #if 1
- // TODO(bill): Should I allow this implicit conversion at all?!
- // rawptr <- ^T
- if (are_types_identical(type, t_rawptr) && is_type_pointer(src)) {
- return 5;
- }
- #endif
- if (is_type_polymorphic(dst) && !is_type_polymorphic(src)) {
- bool modify_type = !c->no_polymorphic_errors;
- if (is_polymorphic_type_assignable(c, type, s, false, modify_type)) {
- return 2;
- }
- }
- if (is_type_union(dst)) {
- for_array(i, dst->Union.variants) {
- Type *vt = dst->Union.variants[i];
- if (are_types_identical(vt, s)) {
- return 1;
- }
- }
- }
- if (is_type_proc(dst)) {
- if (are_types_identical(src, dst)) {
- return 3;
- }
- PolyProcData poly_proc_data = {};
- if (check_polymorphic_procedure_assignment(c, operand, type, operand->expr, &poly_proc_data)) {
- add_entity_use(c, operand->expr, poly_proc_data.gen_entity);
- return 4;
- }
- }
- if (is_type_array(dst)) {
- Type *elem = base_array_type(dst);
- i64 distance = check_distance_between_types(c, operand, elem);
- if (distance >= 0) {
- return distance + 6;
- }
- }
- if (is_type_any(dst)) {
- if (!is_type_polymorphic(src)) {
- if (operand->mode == Addressing_Immutable && operand->type == t_context) {
- return -1;
- } else {
- // NOTE(bill): Anything can cast to 'Any'
- add_type_info_type(c, s);
- return 10;
- }
- }
- }
- Ast *expr = unparen_expr(operand->expr);
- if (expr != nullptr && expr->kind == Ast_AutoCast) {
- Operand x = *operand;
- x.expr = expr->AutoCast.expr;
- bool ok = check_cast_internal(c, &x, type);
- if (ok) {
- return 10;
- }
- }
- return -1;
- }
- i64 assign_score_function(i64 distance) {
- // TODO(bill): A decent score function
- return gb_max(1000000 - distance*distance, 0);
- }
- bool check_is_assignable_to_with_score(CheckerContext *c, Operand *operand, Type *type, i64 *score_) {
- i64 score = 0;
- i64 distance = check_distance_between_types(c, operand, type);
- bool ok = distance >= 0;
- if (ok) {
- score = assign_score_function(distance);
- }
- if (score_) *score_ = score;
- return ok;
- }
- bool check_is_assignable_to(CheckerContext *c, Operand *operand, Type *type) {
- i64 score = 0;
- return check_is_assignable_to_with_score(c, operand, type, &score);
- }
- // NOTE(bill): 'content_name' is for debugging and error messages
- void check_assignment(CheckerContext *c, Operand *operand, Type *type, String context_name) {
- check_not_tuple(c, operand);
- if (operand->mode == Addressing_Invalid) {
- return;
- }
- #if 0
- if (operand->mode == Addressing_Type) {
- Type *t = base_type(type);
- if (t->kind == Type_Pointer &&
- t->Pointer.elem == t_type_info) {
- add_type_info_type(c, type);
- return;
- }
- }
- #endif
- if (is_type_untyped(operand->type)) {
- Type *target_type = type;
- if (type == nullptr || is_type_any(type)) {
- if (type == nullptr && is_type_untyped_nil(operand->type)) {
- error(operand->expr, "Use of untyped nil in %.*s", LIT(context_name));
- operand->mode = Addressing_Invalid;
- return;
- }
- if (type == nullptr && is_type_untyped_undef(operand->type)) {
- error(operand->expr, "Use of --- in %.*s", LIT(context_name));
- operand->mode = Addressing_Invalid;
- return;
- }
- target_type = default_type(operand->type);
- if (type != nullptr && !is_type_any(type)) {
- GB_ASSERT_MSG(is_type_typed(target_type), "%s", type_to_string(type));
- }
- add_type_info_type(c, type);
- add_type_info_type(c, target_type);
- }
- convert_to_typed(c, operand, target_type);
- if (operand->mode == Addressing_Invalid) {
- return;
- }
- }
- if (type == nullptr) {
- return;
- }
- if (operand->mode == Addressing_ProcGroup) {
- Array<Entity *> procs = proc_group_entities(c, *operand);
- bool good = false;
- // NOTE(bill): These should be done
- for_array(i, procs) {
- Type *t = base_type(procs[i]->type);
- if (t == t_invalid) {
- continue;
- }
- Operand x = {};
- x.mode = Addressing_Value;
- x.type = t;
- if (check_is_assignable_to(c, &x, type)) {
- Entity *e = procs[i];
- add_entity_use(c, operand->expr, e);
- good = true;
- break;
- }
- }
- if (!good) {
- gbString expr_str = expr_to_string(operand->expr);
- gbString op_type_str = type_to_string(operand->type);
- gbString type_str = type_to_string(type);
- defer (gb_string_free(type_str));
- defer (gb_string_free(op_type_str));
- defer (gb_string_free(expr_str));
- // TODO(bill): is this a good enough error message?
- error(operand->expr,
- "Cannot assign overloaded procedure '%s' to '%s' in %.*s",
- expr_str,
- op_type_str,
- LIT(context_name));
- operand->mode = Addressing_Invalid;
- }
- return;
- }
- if (!check_is_assignable_to(c, operand, type)) {
- gbString expr_str = expr_to_string(operand->expr);
- gbString op_type_str = type_to_string(operand->type);
- gbString type_str = type_to_string(type);
- defer (gb_string_free(type_str));
- defer (gb_string_free(op_type_str));
- defer (gb_string_free(expr_str));
- switch (operand->mode) {
- case Addressing_Builtin:
- // TODO(bill): Actually allow built in procedures to be passed around and thus be created on use
- error(operand->expr,
- "Cannot assign built-in procedure '%s' in %.*s",
- expr_str,
- LIT(context_name));
- break;
- case Addressing_Type:
- error(operand->expr,
- "Cannot assign '%s' which is a type in %.*s",
- op_type_str,
- LIT(context_name));
- break;
- default:
- // TODO(bill): is this a good enough error message?
- error(operand->expr,
- "Cannot assign value '%s' of type '%s' to '%s' in %.*s",
- expr_str,
- op_type_str,
- type_str,
- LIT(context_name));
- break;
- }
- operand->mode = Addressing_Invalid;
- return;
- }
- }
- bool is_polymorphic_type_assignable(CheckerContext *c, Type *poly, Type *source, bool compound, bool modify_type) {
- Operand o = {Addressing_Value};
- o.type = source;
- switch (poly->kind) {
- case Type_Basic:
- if (compound) return are_types_identical(poly, source);
- return check_is_assignable_to(c, &o, poly);
- case Type_Named: {
- if (check_type_specialization_to(c, poly, source, compound, modify_type)) {
- return true;
- }
- if (compound || !is_type_generic(poly)) {
- return are_types_identical(poly, source);
- }
- return check_is_assignable_to(c, &o, poly);
- }
- case Type_Generic: {
- if (poly->Generic.specialized != nullptr) {
- Type *s = poly->Generic.specialized;
- if (!check_type_specialization_to(c, s, source, compound, modify_type)) {
- return false;
- }
- }
- if (modify_type) {
- Type *ds = default_type(source);
- gb_memmove(poly, ds, gb_size_of(Type));
- }
- return true;
- }
- case Type_Pointer:
- if (source->kind == Type_Pointer) {
- return is_polymorphic_type_assignable(c, poly->Pointer.elem, source->Pointer.elem, true, modify_type);
- }
- return false;
- case Type_Array:
- if (source->kind == Type_Array) {
- // IMPORTANT TODO(bill): Which is correct?
- // if (poly->Array.generic_count != nullptr && modify_type) {
- if (poly->Array.generic_count != nullptr) {
- Type *gt = poly->Array.generic_count;
- GB_ASSERT(gt->kind == Type_Generic);
- Entity *e = scope_lookup(gt->Generic.scope, gt->Generic.name);
- GB_ASSERT(e != nullptr);
- if (e->kind == Entity_TypeName) {
- poly->Array.generic_count = nullptr;
- poly->Array.count = source->Array.count;
- e->kind = Entity_Constant;
- e->Constant.value = exact_value_i64(source->Array.count);
- e->type = t_untyped_integer;
- } else if (e->kind == Entity_Constant) {
- poly->Array.generic_count = nullptr;
- if (e->Constant.value.kind != ExactValue_Integer) {
- return false;
- }
- i64 count = e->Constant.value.value_integer;
- if (count != source->Array.count) {
- return false;
- }
- poly->Array.count = source->Array.count;
- } else {
- return false;
- }
- }
- if (poly->Array.count == source->Array.count) {
- return is_polymorphic_type_assignable(c, poly->Array.elem, source->Array.elem, true, modify_type);
- }
- }
- return false;
- case Type_DynamicArray:
- if (source->kind == Type_DynamicArray) {
- return is_polymorphic_type_assignable(c, poly->DynamicArray.elem, source->DynamicArray.elem, true, modify_type);
- }
- return false;
- case Type_Slice:
- if (source->kind == Type_Slice) {
- return is_polymorphic_type_assignable(c, poly->Slice.elem, source->Slice.elem, true, modify_type);
- }
- return false;
- case Type_Enum:
- return false;
- case Type_Union:
- if (source->kind == Type_Union) {
- TypeUnion *x = &poly->Union;
- TypeUnion *y = &source->Union;
- if (x->variants.count != y->variants.count) {
- return false;
- }
- for_array(i, x->variants) {
- Type *a = x->variants[i];
- Type *b = y->variants[i];
- bool ok = is_polymorphic_type_assignable(c, a, b, false, modify_type);
- if (!ok) return false;
- }
- return true;
- }
- return false;
- case Type_Struct:
- if (source->kind == Type_Struct) {
- // return check_is_assignable_to(c, &o, poly);
- }
- return false;
- case Type_Tuple:
- GB_PANIC("This should never happen");
- return false;
- case Type_Proc:
- if (source->kind == Type_Proc) {
- // return check_is_assignable_to(c, &o, poly);
- // TODO(bill): Polymorphic type assignment
- #if 1
- TypeProc *x = &poly->Proc;
- TypeProc *y = &source->Proc;
- if (x->calling_convention != y->calling_convention) {
- return false;
- }
- if (x->c_vararg != y->c_vararg) {
- return false;
- }
- if (x->variadic != y->variadic) {
- return false;
- }
- if (x->param_count != y->param_count) {
- return false;
- }
- if (x->result_count != y->result_count) {
- return false;
- }
- for (isize i = 0; i < x->param_count; i++) {
- Entity *a = x->params->Tuple.variables[i];
- Entity *b = y->params->Tuple.variables[i];
- bool ok = is_polymorphic_type_assignable(c, a->type, b->type, false, modify_type);
- if (!ok) return false;
- }
- for (isize i = 0; i < x->result_count; i++) {
- Entity *a = x->results->Tuple.variables[i];
- Entity *b = y->results->Tuple.variables[i];
- bool ok = is_polymorphic_type_assignable(c, a->type, b->type, false, modify_type);
- if (!ok) return false;
- }
- // TODO(bill): Polymorphic type assignment
- return true;
- #endif
- }
- return false;
- case Type_Map:
- if (source->kind == Type_Map) {
- bool key = is_polymorphic_type_assignable(c, poly->Map.key, source->Map.key, true, modify_type);
- bool value = is_polymorphic_type_assignable(c, poly->Map.value, source->Map.value, true, modify_type);
- return key || value;
- }
- return false;
- }
- return false;
- }
- bool check_cycle(CheckerContext *c, Entity *curr, bool report) {
- if (curr->state != EntityState_InProgress) {
- return false;
- }
- for_array(i, *c->type_path) {
- Entity *prev = (*c->type_path)[i];
- if (prev == curr) {
- if (report) {
- error(curr->token, "Illegal declaration cycle of `%.*s`", LIT(curr->token.string));
- for (isize j = i; j < c->type_path->count; j++) {
- Entity *curr = (*c->type_path)[j];
- error(curr->token, "\t%.*s refers to", LIT(curr->token.string));
- }
- error(curr->token, "\t%.*s", LIT(curr->token.string));
- }
- return true;
- }
- }
- return false;
- }
- Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *named_type, Type *type_hint, bool allow_import_name) {
- GB_ASSERT(n->kind == Ast_Ident);
- o->mode = Addressing_Invalid;
- o->expr = n;
- String name = n->Ident.token.string;
- Entity *e = scope_lookup(c->scope, name);
- if (e == nullptr) {
- if (is_blank_ident(name)) {
- error(n, "'_' cannot be used as a value type");
- } else {
- error(n, "Undeclared name: %.*s", LIT(name));
- }
- o->type = t_invalid;
- o->mode = Addressing_Invalid;
- if (named_type != nullptr) {
- set_base_type(named_type, t_invalid);
- }
- return nullptr;
- }
- if (e->parent_proc_decl != nullptr &&
- e->parent_proc_decl != c->curr_proc_decl) {
- if (e->kind == Entity_Variable) {
- error(n, "Nested procedures do not capture its parent's variables: %.*s", LIT(name));
- return nullptr;
- } else if (e->kind == Entity_Label) {
- error(n, "Nested procedures do not capture its parent's labels: %.*s", LIT(name));
- return nullptr;
- }
- }
- HashKey key = hash_string(e->token.string);
- if (e->kind == Entity_ProcGroup) {
- auto *pge = &e->ProcGroup;
- DeclInfo *d = decl_info_of_entity(e);
- check_entity_decl(c, e, d, nullptr);
- Array<Entity *> procs = pge->entities;
- bool skip = false;
- if (type_hint != nullptr) {
- // NOTE(bill): These should be done
- for_array(i, procs) {
- Type *t = base_type(procs[i]->type);
- if (t == t_invalid) {
- continue;
- }
- Operand x = {};
- x.mode = Addressing_Value;
- x.type = t;
- if (check_is_assignable_to(c, &x, type_hint)) {
- e = procs[i];
- add_entity_use(c, n, e);
- skip = true;
- break;
- }
- }
- }
- if (!skip) {
- o->mode = Addressing_ProcGroup;
- o->type = t_invalid;
- o->proc_group = e;
- return nullptr;
- }
- }
- add_entity_use(c, n, e);
- if (e->state == EntityState_Unresolved) {
- check_entity_decl(c, e, nullptr, named_type);
- }
- if (e->type == nullptr) {
- // TODO(bill): Which is correct? return or compiler_error?
- // compiler_error("How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(name));
- return nullptr;
- }
- e->flags |= EntityFlag_Used;
- Type *type = e->type;
- switch (e->kind) {
- case Entity_Constant:
- if (type == t_invalid) {
- o->type = t_invalid;
- return e;
- }
- o->value = e->Constant.value;
- if (o->value.kind == ExactValue_Invalid) {
- return e;
- }
- o->mode = Addressing_Constant;
- break;
- case Entity_Variable:
- e->flags |= EntityFlag_Used;
- if (type == t_invalid) {
- o->type = t_invalid;
- return e;
- }
- o->mode = Addressing_Variable;
- if (e->flags & EntityFlag_Value) {
- o->mode = Addressing_Value;
- }
- if (e->Variable.is_immutable) {
- o->mode = Addressing_Immutable;
- }
- break;
- case Entity_Procedure:
- o->mode = Addressing_Value;
- break;
- case Entity_Builtin:
- o->builtin_id = cast(BuiltinProcId)e->Builtin.id;
- o->mode = Addressing_Builtin;
- break;
- case Entity_TypeName:
- o->mode = Addressing_Type;
- if (check_cycle(c, e, true)) {
- type = t_invalid;
- }
- break;
- case Entity_ImportName:
- if (!allow_import_name) {
- error(n, "Use of import '%.*s' not in selector", LIT(name));
- }
- return e;
- case Entity_LibraryName:
- error(n, "Use of library '%.*s' not in foreign block", LIT(name));
- return e;
- case Entity_Label:
- o->mode = Addressing_NoValue;
- break;
- case Entity_Nil:
- o->mode = Addressing_Value;
- break;
- default:
- compiler_error("Unknown EntityKind %.*s", LIT(entity_strings[e->kind]));
- break;
- }
- o->type = type;
- return e;
- }
- bool check_unary_op(CheckerContext *c, Operand *o, Token op) {
- if (o->type == nullptr) {
- gbString str = expr_to_string(o->expr);
- error(o->expr, "Expression has no value '%s'", str);
- gb_string_free(str);
- return false;
- }
- // TODO(bill): Handle errors correctly
- Type *type = base_type(core_array_type(o->type));
- gbString str = nullptr;
- switch (op.kind) {
- case Token_Add:
- case Token_Sub:
- if (!is_type_numeric(type)) {
- str = expr_to_string(o->expr);
- error(op, "Operator '%.*s' is not allowed with '%s'", LIT(op.string), str);
- gb_string_free(str);
- }
- break;
- case Token_Xor:
- if (!is_type_integer(type) && !is_type_boolean(type)) {
- error(op, "Operator '%.*s' is only allowed with integers or booleans", LIT(op.string));
- }
- break;
- case Token_Not:
- if (!is_type_boolean(type)) {
- str = expr_to_string(o->expr);
- error(op, "Operator '%.*s' is only allowed on boolean expression", LIT(op.string));
- gb_string_free(str);
- }
- break;
- default:
- error(op, "Unknown operator '%.*s'", LIT(op.string));
- return false;
- }
- return true;
- }
- bool check_binary_op(CheckerContext *c, Operand *o, Token op) {
- // TODO(bill): Handle errors correctly
- Type *type = base_type(core_array_type(o->type));
- switch (op.kind) {
- case Token_Sub:
- case Token_SubEq:
- if (!is_type_numeric(type) && !is_type_pointer(type)) {
- error(op, "Operator '%.*s' is only allowed with numeric or pointer expressions", LIT(op.string));
- return false;
- }
- #if defined(NO_POINTER_ARITHMETIC)
- if (is_type_pointer(type)) {
- error(o->expr, "Pointer arithmetic is not supported");
- return false;
- }
- #else
- if (is_type_pointer(type)) {
- o->type = t_int;
- }
- if (base_type(type) == t_rawptr) {
- gbString str = type_to_string(type);
- error(o->expr, "Invalid pointer type for pointer arithmetic: '%s'", str);
- gb_string_free(str);
- return false;
- }
- break;
- #endif
- case Token_Mul:
- case Token_Quo:
- case Token_AddEq:
- case Token_MulEq:
- case Token_QuoEq:
- if (!is_type_numeric(type)) {
- error(op, "Operator '%.*s' is only allowed with numeric expressions", LIT(op.string));
- return false;
- }
- break;
- case Token_Add:
- if (is_type_string(type)) {
- if (o->mode == Addressing_Constant) {
- return true;
- }
- error(op, "String concatenation is only allowed with constant strings");
- return false;
- } else if (!is_type_numeric(type)) {
- error(op, "Operator '%.*s' is only allowed with numeric expressions", LIT(op.string));
- return false;
- }
- break;
- case Token_And:
- case Token_Or:
- case Token_AndEq:
- case Token_OrEq:
- case Token_Xor:
- case Token_XorEq:
- if (!is_type_integer(type) && !is_type_boolean(type)) {
- error(op, "Operator '%.*s' is only allowed with integers or booleans", LIT(op.string));
- return false;
- }
- break;
- case Token_Mod:
- case Token_ModMod:
- case Token_AndNot:
- case Token_ModEq:
- case Token_ModModEq:
- case Token_AndNotEq:
- if (!is_type_integer(type)) {
- error(op, "Operator '%.*s' is only allowed with integers", LIT(op.string));
- return false;
- }
- break;
- case Token_CmpAnd:
- case Token_CmpOr:
- case Token_CmpAndEq:
- case Token_CmpOrEq:
- if (!is_type_boolean(type)) {
- error(op, "Operator '%.*s' is only allowed with boolean expressions", LIT(op.string));
- return false;
- }
- break;
- default:
- error(op, "Unknown operator '%.*s'", LIT(op.string));
- return false;
- }
- return true;
- }
- bool check_representable_as_constant(CheckerContext *c, ExactValue in_value, Type *type, ExactValue *out_value) {
- if (in_value.kind == ExactValue_Invalid) {
- // NOTE(bill): There's already been an error
- return true;
- }
- type = core_type(type);
- if (type == t_invalid) {
- return false;
- } else if (is_type_boolean(type)) {
- return in_value.kind == ExactValue_Bool;
- } else if (is_type_string(type)) {
- return in_value.kind == ExactValue_String;
- } else if (is_type_integer(type) || is_type_rune(type)) {
- ExactValue v = exact_value_to_integer(in_value);
- if (v.kind != ExactValue_Integer) {
- return false;
- }
- if (out_value) *out_value = v;
- if (is_type_untyped(type)) {
- return true;
- }
- i64 i = v.value_integer;
- u64 u = bit_cast<u64>(i);
- i64 bit_size = type_size_of(type);
- u64 umax = unsigned_integer_maxs[bit_size];
- i64 imin = signed_integer_mins[bit_size];
- i64 imax = signed_integer_maxs[bit_size];
- switch (type->Basic.kind) {
- case Basic_rune:
- case Basic_i8:
- case Basic_i16:
- case Basic_i32:
- case Basic_int:
- return imin <= i && i <= imax;
- case Basic_u8:
- case Basic_u16:
- case Basic_u32:
- case Basic_uint:
- case Basic_uintptr:
- return 0ull <= u && u <= umax;
- case Basic_u64:
- return 0ull <= i;
- case Basic_i64:
- return true;
- case Basic_UntypedInteger:
- return true;
- default: GB_PANIC("Compiler error: Unknown integer type!"); break;
- }
- } else if (is_type_float(type)) {
- ExactValue v = exact_value_to_float(in_value);
- if (v.kind != ExactValue_Float) {
- return false;
- }
- if (out_value) *out_value = v;
- switch (type->Basic.kind) {
- // case Basic_f16:
- case Basic_f32:
- case Basic_f64:
- return true;
- case Basic_UntypedFloat:
- return true;
- default: GB_PANIC("Compiler error: Unknown float type!"); break;
- }
- } else if (is_type_complex(type)) {
- ExactValue v = exact_value_to_complex(in_value);
- if (v.kind != ExactValue_Complex) {
- return false;
- }
- switch (type->Basic.kind) {
- case Basic_complex64:
- case Basic_complex128: {
- ExactValue real = exact_value_real(v);
- ExactValue imag = exact_value_imag(v);
- if (real.kind != ExactValue_Invalid &&
- imag.kind != ExactValue_Invalid) {
- if (out_value) *out_value = exact_binary_operator_value(Token_Add, real, exact_value_make_imag(imag));
- return true;
- }
- break;
- }
- case Basic_UntypedComplex:
- return true;
- default: GB_PANIC("Compiler error: Unknown complex type!"); break;
- }
- return false;
- } else if (is_type_pointer(type)) {
- if (in_value.kind == ExactValue_Pointer) {
- return true;
- }
- if (in_value.kind == ExactValue_Integer) {
- return false;
- // return true;
- }
- if (out_value) *out_value = in_value;
- }
- return false;
- }
- void check_is_expressible(CheckerContext *c, Operand *o, Type *type) {
- GB_ASSERT(is_type_constant_type(type));
- GB_ASSERT(o->mode == Addressing_Constant);
- if (!check_representable_as_constant(c, o->value, type, &o->value)) {
- gbString a = expr_to_string(o->expr);
- gbString b = type_to_string(type);
- if (is_type_numeric(o->type) && is_type_numeric(type)) {
- if (!is_type_integer(o->type) && is_type_integer(type)) {
- error(o->expr, "'%s' truncated to '%s'", a, b);
- } else {
- char buf[127] = {};
- String str = {};
- i64 i = o->value.value_integer;
- if (is_type_unsigned(o->type)) {
- str = u64_to_string(bit_cast<u64>(i), buf, gb_size_of(buf));
- } else {
- str = i64_to_string(i, buf, gb_size_of(buf));
- }
- error(o->expr, "'%s = %.*s' overflows '%s'", a, LIT(str), b);
- }
- } else {
- error(o->expr, "Cannot convert '%s' to '%s'", a, b);
- }
- gb_string_free(b);
- gb_string_free(a);
- o->mode = Addressing_Invalid;
- }
- }
- bool check_is_not_addressable(CheckerContext *c, Operand *o) {
- if (o->mode == Addressing_OptionalOk) {
- Ast *expr = unselector_expr(o->expr);
- if (expr->kind != Ast_TypeAssertion) {
- return true;
- }
- ast_node(ta, TypeAssertion, expr);
- TypeAndValue tv = ta->expr->tav;
- if (is_type_pointer(tv.type)) {
- return false;
- }
- if (is_type_union(tv.type) && tv.mode == Addressing_Variable) {
- return false;
- }
- if (is_type_any(tv.type)) {
- return false;
- }
- return true;
- }
- if (o->mode != Addressing_Variable) {
- return true;
- }
- if (is_type_bit_field_value(o->type)) {
- return true;
- }
- return false;
- }
- void check_unary_expr(CheckerContext *c, Operand *o, Token op, Ast *node) {
- switch (op.kind) {
- case Token_And: { // Pointer address
- if (check_is_not_addressable(c, o)) {
- if (ast_node_expect(node, Ast_UnaryExpr)) {
- ast_node(ue, UnaryExpr, node);
- gbString str = expr_to_string(ue->expr);
- error(op, "Cannot take the pointer address of '%s'", str);
- gb_string_free(str);
- }
- o->mode = Addressing_Invalid;
- return;
- }
- o->mode = Addressing_Value;
- o->type = alloc_type_pointer(o->type);
- return;
- }
- }
- if (!check_unary_op(c, o, op)) {
- o->mode = Addressing_Invalid;
- return;
- }
- if (o->mode == Addressing_Constant) {
- Type *type = base_type(o->type);
- if (!is_type_constant_type(o->type)) {
- gbString xt = type_to_string(o->type);
- gbString err_str = expr_to_string(node);
- error(op, "Invalid type, '%s', for constant unary expression '%s'", xt, err_str);
- gb_string_free(err_str);
- gb_string_free(xt);
- o->mode = Addressing_Invalid;
- return;
- }
- i32 precision = 0;
- if (is_type_unsigned(type)) {
- precision = cast(i32)(8 * type_size_of(type));
- }
- if (op.kind == Token_Xor && is_type_untyped(type)) {
- gbString err_str = expr_to_string(node);
- error(op, "Bitwise not cannot be applied to untyped constants '%s'", err_str);
- gb_string_free(err_str);
- o->mode = Addressing_Invalid;
- return;
- }
- if (op.kind == Token_Sub && is_type_unsigned(type)) {
- gbString err_str = expr_to_string(node);
- error(op, "A unsigned constant cannot be negated '%s'", err_str);
- gb_string_free(err_str);
- o->mode = Addressing_Invalid;
- return;
- }
- o->value = exact_unary_operator_value(op.kind, o->value, precision, is_type_unsigned(type));
- if (is_type_typed(type)) {
- if (node != nullptr) {
- o->expr = node;
- }
- check_is_expressible(c, o, type);
- }
- return;
- }
- o->mode = Addressing_Value;
- }
- void check_comparison(CheckerContext *c, Operand *x, Operand *y, TokenKind op) {
- if (x->mode == Addressing_Type && y->mode == Addressing_Type) {
- bool comp = are_types_identical(x->type, y->type);
- switch (op) {
- case Token_CmpEq: /* comp = comp; */ break;
- case Token_NotEq: comp = !comp; break;
- }
- x->mode = Addressing_Constant;
- x->type = t_untyped_bool;
- x->value = exact_value_bool(comp);
- return;
- }
- gbString err_str = nullptr;
- defer (if (err_str != nullptr) {
- gb_string_free(err_str);
- });
- if (check_is_assignable_to(c, x, y->type) ||
- check_is_assignable_to(c, y, x->type)) {
- Type *err_type = x->type;
- bool defined = false;
- switch (op) {
- case Token_CmpEq:
- case Token_NotEq:
- defined = is_type_comparable(x->type) ||
- (is_operand_nil(*x) && type_has_nil(y->type)) ||
- (is_operand_nil(*y) && type_has_nil(x->type));
- break;
- case Token_Lt:
- case Token_Gt:
- case Token_LtEq:
- case Token_GtEq:
- defined = is_type_ordered(x->type);
- break;
- }
- if (!defined) {
- if (x->type == err_type && is_operand_nil(*x)) {
- err_type = y->type;
- }
- gbString type_string = type_to_string(err_type);
- defer (gb_string_free(type_string));
- err_str = gb_string_make(c->allocator,
- gb_bprintf("operator '%.*s' not defined for type '%s'", LIT(token_strings[op]), type_string));
- }
- } else {
- gbString xt, yt;
- if (x->mode == Addressing_ProcGroup) {
- xt = gb_string_make(heap_allocator(), "procedure group");
- } else {
- xt = type_to_string(x->type);
- }
- if (y->mode == Addressing_ProcGroup) {
- yt = gb_string_make(heap_allocator(), "procedure group");
- } else {
- yt = type_to_string(y->type);
- }
- err_str = gb_string_make(c->allocator,
- gb_bprintf("mismatched types '%s' and '%s'", xt, yt));
- gb_string_free(yt);
- gb_string_free(xt);
- }
- if (err_str != nullptr) {
- error(x->expr, "Cannot compare expression, %s", err_str);
- x->type = t_untyped_bool;
- } else {
- if (x->mode == Addressing_Constant &&
- y->mode == Addressing_Constant) {
- x->value = exact_value_bool(compare_exact_values(op, x->value, y->value));
- } else {
- x->mode = Addressing_Value;
- update_expr_type(c, x->expr, default_type(x->type), true);
- update_expr_type(c, y->expr, default_type(y->type), true);
- i64 size = 0;
- if (!is_type_untyped(x->type)) size = gb_max(size, type_size_of(x->type));
- if (!is_type_untyped(y->type)) size = gb_max(size, type_size_of(y->type));
- if (is_type_string(x->type) || is_type_string(y->type)) {
- switch (op) {
- case Token_CmpEq: add_package_dependency(c, "runtime", "__string_eq"); break;
- case Token_NotEq: add_package_dependency(c, "runtime", "__string_ne"); break;
- case Token_Lt: add_package_dependency(c, "runtime", "__string_lt"); break;
- case Token_Gt: add_package_dependency(c, "runtime", "__string_gt"); break;
- case Token_LtEq: add_package_dependency(c, "runtime", "__string_le"); break;
- case Token_GtEq: add_package_dependency(c, "runtime", "__string_gt"); break;
- }
- } else if (is_type_complex(x->type) || is_type_complex(y->type)) {
- switch (op) {
- case Token_CmpEq:
- switch (8*size) {
- case 64: add_package_dependency(c, "runtime", "__complex64_eq"); break;
- case 128: add_package_dependency(c, "runtime", "__complex128_eq"); break;
- }
- break;
- case Token_NotEq:
- switch (8*size) {
- case 64: add_package_dependency(c, "runtime", "__complex64_ne"); break;
- case 128: add_package_dependency(c, "runtime", "__complex128_ne"); break;
- }
- break;
- }
- }
- }
- x->type = t_untyped_bool;
- }
- }
- void check_shift(CheckerContext *c, Operand *x, Operand *y, Ast *node) {
- GB_ASSERT(node->kind == Ast_BinaryExpr);
- ast_node(be, BinaryExpr, node);
- ExactValue x_val = {};
- if (x->mode == Addressing_Constant) {
- x_val = exact_value_to_integer(x->value);
- }
- bool x_is_untyped = is_type_untyped(x->type);
- if (!(is_type_integer(x->type) || (x_is_untyped && x_val.kind == ExactValue_Integer))) {
- gbString err_str = expr_to_string(x->expr);
- error(node, "Shifted operand '%s' must be an integer", err_str);
- gb_string_free(err_str);
- x->mode = Addressing_Invalid;
- return;
- }
- if (is_type_unsigned(y->type)) {
- } else if (is_type_untyped(y->type)) {
- convert_to_typed(c, y, t_untyped_integer);
- if (y->mode == Addressing_Invalid) {
- x->mode = Addressing_Invalid;
- return;
- }
- } else {
- gbString err_str = expr_to_string(y->expr);
- error(node, "Shift amount '%s' must be an unsigned integer", err_str);
- gb_string_free(err_str);
- x->mode = Addressing_Invalid;
- return;
- }
- if (x->mode == Addressing_Constant) {
- if (y->mode == Addressing_Constant) {
- ExactValue y_val = exact_value_to_integer(y->value);
- if (y_val.kind != ExactValue_Integer) {
- gbString err_str = expr_to_string(y->expr);
- error(node, "Shift amount '%s' must be an unsigned integer", err_str);
- gb_string_free(err_str);
- x->mode = Addressing_Invalid;
- return;
- }
- i64 amount = y_val.value_integer;
- if (amount > 128) {
- gbString err_str = expr_to_string(y->expr);
- error(node, "Shift amount too large: '%s'", err_str);
- gb_string_free(err_str);
- x->mode = Addressing_Invalid;
- return;
- }
- if (!is_type_integer(x->type)) {
- // NOTE(bill): It could be an untyped float but still representable
- // as an integer
- x->type = t_untyped_integer;
- }
- x->value = exact_value_shift(be->op.kind, x_val, exact_value_i64(amount));
- if (is_type_typed(x->type)) {
- check_is_expressible(c, x, base_type(x->type));
- }
- return;
- }
- TokenPos pos = ast_token(x->expr).pos;
- if (x_is_untyped) {
- ExprInfo *info = check_get_expr_info(&c->checker->info, x->expr);
- if (info != nullptr) {
- info->is_lhs = true;
- }
- x->mode = Addressing_Value;
- // x->value = x_val;
- return;
- }
- }
- if (y->mode == Addressing_Constant && y->value.value_integer < 0) {
- gbString err_str = expr_to_string(y->expr);
- error(node, "Shift amount cannot be negative: '%s'", err_str);
- gb_string_free(err_str);
- }
- if (!is_type_integer(x->type)) {
- gbString err_str = expr_to_string(y->expr);
- error(node, "Shift operand '%s' must be an integer", err_str);
- gb_string_free(err_str);
- x->mode = Addressing_Invalid;
- return;
- }
- x->mode = Addressing_Value;
- }
- Operand check_ptr_addition(CheckerContext *c, TokenKind op, Operand *ptr, Operand *offset, Ast *node) {
- GB_ASSERT(node->kind == Ast_BinaryExpr);
- ast_node(be, BinaryExpr, node);
- GB_ASSERT(is_type_pointer(ptr->type));
- GB_ASSERT(is_type_integer(offset->type));
- GB_ASSERT(op == Token_Add || op == Token_Sub);
- Operand operand = {};
- operand.mode = Addressing_Value;
- operand.type = ptr->type;
- operand.expr = node;
- if (base_type(ptr->type) == t_rawptr) {
- gbString str = type_to_string(ptr->type);
- error(node, "Invalid pointer type for pointer arithmetic: '%s'", str);
- gb_string_free(str);
- operand.mode = Addressing_Invalid;
- return operand;
- }
- #if defined(NO_POINTER_ARITHMETIC)
- operand.mode = Addressing_Invalid;
- error(operand.expr, "Pointer arithmetic is not supported");
- return operand;
- #else
- Type *base_ptr = base_type(ptr->type); GB_ASSERT(base_ptr->kind == Type_Pointer);
- Type *elem = base_ptr->Pointer.elem;
- i64 elem_size = type_size_of(elem);
- if (elem_size <= 0) {
- gbString str = type_to_string(elem);
- error(node, "Size of pointer's element type '%s' is zero and cannot be used for pointer arithmetic", str);
- gb_string_free(str);
- operand.mode = Addressing_Invalid;
- return operand;
- }
- if (ptr->mode == Addressing_Constant && offset->mode == Addressing_Constant) {
- i64 ptr_val = ptr->value.value_pointer;
- i64 offset_val = exact_value_to_integer(offset->value).value_integer;
- i64 new_ptr_val = ptr_val;
- if (op == Token_Add) {
- new_ptr_val += elem_size*offset_val;
- } else {
- new_ptr_val -= elem_size*offset_val;
- }
- operand.mode = Addressing_Constant;
- operand.value = exact_value_pointer(new_ptr_val);
- }
- return operand;
- #endif
- }
- bool check_is_castable_to(CheckerContext *c, Operand *operand, Type *y) {
- if (check_is_assignable_to(c, operand, y)) {
- return true;
- }
- Type *x = operand->type;
- Type *src = core_type(x);
- Type *dst = core_type(y);
- if (are_types_identical(src, dst)) {
- return true;
- }
- if (dst->kind == Type_Array && src->kind == Type_Array) {
- if (are_types_identical(dst->Array.elem, src->Array.elem)) {
- return dst->Array.count == src->Array.count;
- }
- }
- if (dst->kind == Type_Slice && src->kind == Type_Slice) {
- return are_types_identical(dst->Slice.elem, src->Slice.elem);
- }
- // Cast between booleans and integers
- if (is_type_boolean(src) || is_type_integer(src)) {
- if (is_type_boolean(dst) || is_type_integer(dst)) {
- return true;
- }
- }
- // Cast between numbers
- if (is_type_integer(src) || is_type_float(src)) {
- if (is_type_integer(dst) || is_type_float(dst)) {
- return true;
- }
- }
- if (is_type_integer(src) && is_type_rune(dst)) {
- return true;
- }
- if (is_type_rune(src) && is_type_integer(dst)) {
- return true;
- }
- if (is_type_complex(src) && is_type_complex(dst)) {
- return true;
- }
- if (is_type_bit_field_value(src) && is_type_integer(dst)) {
- return true;
- }
- if (is_type_bit_field_value(src) && is_type_boolean(dst)) {
- return src->BitFieldValue.bits == 1;
- }
- // Cast between pointers
- if (is_type_pointer(src) && is_type_pointer(dst)) {
- #if 0
- Type *s = base_type(type_deref(src));
- if (is_type_union(s)) {
- // NOTE(bill): Should the error be here?!
- // NOTE(bill): This error should suppress the next casting error as it's at the same position
- gbString xs = type_to_string(x);
- gbString ys = type_to_string(y);
- error(operand->expr, "Cannot cast from a union pointer '%s' to '%s', try using 'union_cast' or cast to a 'rawptr'", xs, ys);
- gb_string_free(ys);
- gb_string_free(xs);
- return false;
- }
- #endif
- return true;
- }
- // (u)int <-> pointer
- if (is_type_uintptr(src) && is_type_pointer(dst)) {
- return true;
- }
- if (is_type_pointer(src) && is_type_uintptr(dst)) {
- return true;
- }
- // []byte/[]u8 <-> string
- if (is_type_u8_slice(src) && is_type_string(dst)) {
- return true;
- }
- if (is_type_string(src) && is_type_u8_slice(dst)) {
- // if (is_type_typed(src)) {
- return true;
- // }
- }
- // cstring -> string
- if (src == t_cstring && dst == t_string) {
- if (operand->mode != Addressing_Constant) {
- add_package_dependency(c, "runtime", "__cstring_to_string");
- }
- return true;
- }
- // cstring -> ^u8
- if (src == t_cstring && is_type_u8_ptr(dst)) {
- return true;
- }
- // ^u8 -> cstring
- if (is_type_u8_ptr(src) && dst == t_cstring) {
- return true;
- }
- // proc <-> proc
- if (is_type_proc(src) && is_type_proc(dst)) {
- return true;
- }
- // proc -> rawptr
- if (is_type_proc(src) && is_type_rawptr(dst)) {
- return true;
- }
- // rawptr -> proc
- if (is_type_rawptr(src) && is_type_proc(dst)) {
- return true;
- }
- return false;
- }
- bool check_cast_internal(CheckerContext *c, Operand *x, Type *type) {
- bool is_const_expr = x->mode == Addressing_Constant;
- bool can_convert = false;
- Type *bt = base_type(type);
- if (is_const_expr && is_type_constant_type(bt)) {
- if (core_type(bt)->kind == Type_Basic) {
- if (check_representable_as_constant(c, x->value, bt, &x->value)) {
- return true;
- } else if (is_type_pointer(type) && check_is_castable_to(c, x, type)) {
- return true;
- }
- }
- } else if (check_is_castable_to(c, x, type)) {
- if (x->mode != Addressing_Constant) {
- x->mode = Addressing_Value;
- } else if (is_type_slice(type) && is_type_string(x->type)) {
- x->mode = Addressing_Value;
- }
- return true;
- }
- return false;
- }
- void check_cast(CheckerContext *c, Operand *x, Type *type) {
- if (!is_operand_value(*x)) {
- error(x->expr, "Only values can be casted");
- x->mode = Addressing_Invalid;
- return;
- }
- bool is_const_expr = x->mode == Addressing_Constant;
- bool can_convert = check_cast_internal(c, x, type);
- if (!can_convert) {
- gbString expr_str = expr_to_string(x->expr);
- gbString to_type = type_to_string(type);
- gbString from_type = type_to_string(x->type);
- error(x->expr, "Cannot cast '%s' as '%s' from '%s'", expr_str, to_type, from_type);
- gb_string_free(from_type);
- gb_string_free(to_type);
- gb_string_free(expr_str);
- x->mode = Addressing_Invalid;
- return;
- }
- if (is_type_untyped(x->type)) {
- Type *final_type = type;
- if (is_const_expr && !is_type_constant_type(type)) {
- final_type = default_type(x->type);
- }
- update_expr_type(c, x->expr, final_type, true);
- }
- x->type = type;
- }
- bool check_transmute(CheckerContext *c, Ast *node, Operand *o, Type *t) {
- if (!is_operand_value(*o)) {
- error(o->expr, "'transmute' can only be applied to values");
- o->mode = Addressing_Invalid;
- return false;
- }
- if (o->mode == Addressing_Constant) {
- gbString expr_str = expr_to_string(o->expr);
- error(o->expr, "Cannot transmute a constant expression: '%s'", expr_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return false;
- }
- if (is_type_untyped(o->type)) {
- gbString expr_str = expr_to_string(o->expr);
- error(o->expr, "Cannot transmute untyped expression: '%s'", expr_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return false;
- }
- i64 srcz = type_size_of(o->type);
- i64 dstz = type_size_of(t);
- if (srcz != dstz) {
- gbString expr_str = expr_to_string(o->expr);
- gbString type_str = type_to_string(t);
- error(o->expr, "Cannot transmute '%s' to '%s', %lld vs %lld bytes", expr_str, type_str, srcz, dstz);
- gb_string_free(type_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return false;
- }
- o->mode = Addressing_Value;
- o->type = t;
- return true;
- }
- bool check_binary_array_expr(CheckerContext *c, Token op, Operand *x, Operand *y) {
- if (is_type_array(x->type) && !is_type_array(y->type)) {
- if (check_is_assignable_to(c, y, x->type)) {
- if (check_binary_op(c, x, op)) {
- return true;
- }
- }
- }
- return false;
- }
- void check_binary_expr(CheckerContext *c, Operand *x, Ast *node) {
- GB_ASSERT(node->kind == Ast_BinaryExpr);
- Operand y_ = {}, *y = &y_;
- ast_node(be, BinaryExpr, node);
- Token op = be->op;
- switch (op.kind) {
- case Token_CmpEq:
- case Token_NotEq: {
- // NOTE(bill): Allow comparisons between types
- check_expr_or_type(c, x, be->left);
- check_expr_or_type(c, y, be->right);
- bool xt = x->mode == Addressing_Type;
- bool yt = y->mode == Addressing_Type;
- // If only one is a type, this is an error
- if (xt ^ yt) {
- GB_ASSERT(xt != yt);
- if (xt) error_operand_not_expression(x);
- if (yt) error_operand_not_expression(y);
- }
- break;
- }
- default:
- check_expr(c, x, be->left);
- check_expr(c, y, be->right);
- break;
- }
- if (x->mode == Addressing_Invalid) {
- return;
- }
- if (y->mode == Addressing_Invalid) {
- x->mode = Addressing_Invalid;
- x->expr = y->expr;
- return;
- }
- if (token_is_shift(op.kind)) {
- check_shift(c, x, y, node);
- return;
- }
- if (op.kind == Token_Add || op.kind == Token_Sub) {
- if (is_type_pointer(x->type) && is_type_integer(y->type)) {
- *x = check_ptr_addition(c, op.kind, x, y, node);
- return;
- } else if (is_type_integer(x->type) && is_type_pointer(y->type)) {
- if (op.kind == Token_Sub) {
- gbString lhs = expr_to_string(x->expr);
- gbString rhs = expr_to_string(y->expr);
- error(node, "Invalid pointer arithmetic, did you mean '%s %.*s %s'?", rhs, LIT(op.string), lhs);
- gb_string_free(rhs);
- gb_string_free(lhs);
- x->mode = Addressing_Invalid;
- return;
- }
- *x = check_ptr_addition(c, op.kind, y, x, node);
- return;
- }
- }
- convert_to_typed(c, x, y->type);
- if (x->mode == Addressing_Invalid) {
- return;
- }
- convert_to_typed(c, y, x->type);
- if (y->mode == Addressing_Invalid) {
- x->mode = Addressing_Invalid;
- return;
- }
- if (token_is_comparison(op.kind)) {
- check_comparison(c, x, y, op.kind);
- return;
- }
- if (check_binary_array_expr(c, op, x, y)) {
- x->mode = Addressing_Value;
- x->type = x->type;
- return;
- }
- if (check_binary_array_expr(c, op, y, x)) {
- x->mode = Addressing_Value;
- x->type = y->type;
- return;
- }
- if (!are_types_identical(x->type, y->type)) {
- if (x->type != t_invalid &&
- y->type != t_invalid) {
- gbString xt = type_to_string(x->type);
- gbString yt = type_to_string(y->type);
- gbString expr_str = expr_to_string(x->expr);
- error(op, "Mismatched types in binary expression '%s' : '%s' vs '%s'", expr_str, xt, yt);
- gb_string_free(expr_str);
- gb_string_free(yt);
- gb_string_free(xt);
- }
- x->mode = Addressing_Invalid;
- return;
- }
- if (!check_binary_op(c, x, op)) {
- x->mode = Addressing_Invalid;
- return;
- }
- switch (op.kind) {
- case Token_Quo:
- case Token_Mod:
- case Token_ModMod:
- case Token_QuoEq:
- case Token_ModEq:
- case Token_ModModEq:
- if ((x->mode == Addressing_Constant || is_type_integer(x->type)) &&
- y->mode == Addressing_Constant) {
- bool fail = false;
- switch (y->value.kind) {
- case ExactValue_Integer:
- if (y->value.value_integer == 0 ) {
- fail = true;
- }
- break;
- case ExactValue_Float:
- if (y->value.value_float == 0.0) {
- fail = true;
- }
- break;
- }
- if (fail) {
- error(y->expr, "Division by zero not allowed");
- x->mode = Addressing_Invalid;
- return;
- }
- }
- }
- if (x->mode == Addressing_Constant &&
- y->mode == Addressing_Constant) {
- ExactValue a = x->value;
- ExactValue b = y->value;
- Type *type = base_type(x->type);
- if (is_type_pointer(type)) {
- GB_ASSERT(op.kind == Token_Sub);
- i64 bytes = a.value_pointer - b.value_pointer;
- i64 diff = bytes/type_size_of(type);
- x->value = exact_value_pointer(diff);
- return;
- }
- if (!is_type_constant_type(type)) {
- gbString xt = type_to_string(x->type);
- gbString err_str = expr_to_string(node);
- error(op, "Invalid type, '%s', for constant binary expression '%s'", xt, err_str);
- gb_string_free(err_str);
- gb_string_free(xt);
- x->mode = Addressing_Invalid;
- return;
- }
- if (op.kind == Token_Quo && is_type_integer(type)) {
- op.kind = Token_QuoEq; // NOTE(bill): Hack to get division of integers
- }
- x->value = exact_binary_operator_value(op.kind, a, b);
- if (is_type_typed(type)) {
- if (node != nullptr) {
- x->expr = node;
- }
- check_is_expressible(c, x, type);
- }
- return;
- } else if (is_type_string(x->type)) {
- error(node, "String concatenation is only allowed with constant strings");
- x->mode = Addressing_Invalid;
- return;
- }
- x->mode = Addressing_Value;
- }
- void update_expr_type(CheckerContext *c, Ast *e, Type *type, bool final) {
- ExprInfo *found = check_get_expr_info(&c->checker->info, e);
- if (found == nullptr) {
- return;
- }
- ExprInfo old = *found;
- switch (e->kind) {
- case_ast_node(ue, UnaryExpr, e);
- if (old.value.kind != ExactValue_Invalid) {
- // NOTE(bill): if 'e' is constant, the operands will be constant too.
- // They don't need to be updated as they will be updated later and
- // checked at the end of general checking stage.
- break;
- }
- update_expr_type(c, ue->expr, type, final);
- case_end;
- case_ast_node(be, BinaryExpr, e);
- if (old.value.kind != ExactValue_Invalid) {
- // See above note in UnaryExpr case
- break;
- }
- if (token_is_comparison(be->op.kind)) {
- // NOTE(bill): Do nothing as the types are fine
- } else if (token_is_shift(be->op.kind)) {
- update_expr_type(c, be->left, type, final);
- } else {
- update_expr_type(c, be->left, type, final);
- update_expr_type(c, be->right, type, final);
- }
- case_end;
- case_ast_node(pe, ParenExpr, e);
- update_expr_type(c, pe->expr, type, final);
- case_end;
- }
- if (!final && is_type_untyped(type)) {
- old.type = base_type(type);
- check_set_expr_info(&c->checker->info, e, old);
- return;
- }
- // We need to remove it and then give it a new one
- check_remove_expr_info(&c->checker->info, e);
- if (old.is_lhs && !is_type_integer(type)) {
- gbString expr_str = expr_to_string(e);
- gbString type_str = type_to_string(type);
- error(e, "Shifted operand %s must be an integer, got %s", expr_str, type_str);
- gb_string_free(type_str);
- gb_string_free(expr_str);
- return;
- }
- add_type_and_value(&c->checker->info, e, old.mode, type, old.value);
- }
- void update_expr_value(CheckerContext *c, Ast *e, ExactValue value) {
- ExprInfo *found = check_get_expr_info(&c->checker->info, e);
- if (found) {
- found->value = value;
- }
- }
- void convert_untyped_error(CheckerContext *c, Operand *operand, Type *target_type) {
- gbString expr_str = expr_to_string(operand->expr);
- gbString type_str = type_to_string(target_type);
- char *extra_text = "";
- if (operand->mode == Addressing_Constant) {
- if (operand->value.value_integer == 0) {
- if (make_string_c(expr_str) != "nil") { // HACK NOTE(bill): Just in case
- // NOTE(bill): Doesn't matter what the type is as it's still zero in the union
- extra_text = " - Did you want 'nil'?";
- }
- }
- }
- error(operand->expr, "Cannot convert '%s' to '%s'%s", expr_str, type_str, extra_text);
- gb_string_free(type_str);
- gb_string_free(expr_str);
- operand->mode = Addressing_Invalid;
- }
- ExactValue convert_exact_value_for_type(ExactValue v, Type *type) {
- Type *t = core_type(type);
- if (is_type_boolean(t)) {
- // v = exact_value_to_boolean(v);
- } else if (is_type_float(t)) {
- v = exact_value_to_float(v);
- } else if (is_type_integer(t)) {
- v = exact_value_to_integer(v);
- } else if (is_type_pointer(t)) {
- v = exact_value_to_integer(v);
- } else if (is_type_complex(t)) {
- v = exact_value_to_complex(v);
- }
- return v;
- }
- void convert_to_typed(CheckerContext *c, Operand *operand, Type *target_type) {
- GB_ASSERT_NOT_NULL(target_type);
- if (operand->mode == Addressing_Invalid ||
- operand->mode == Addressing_Type ||
- is_type_typed(operand->type) ||
- target_type == t_invalid) {
- return;
- }
- if (is_type_untyped(target_type)) {
- GB_ASSERT(operand->type->kind == Type_Basic);
- GB_ASSERT(target_type->kind == Type_Basic);
- BasicKind x_kind = operand->type->Basic.kind;
- BasicKind y_kind = target_type->Basic.kind;
- if (is_type_numeric(operand->type) && is_type_numeric(target_type)) {
- if (x_kind < y_kind) {
- operand->type = target_type;
- update_expr_type(c, operand->expr, target_type, false);
- }
- } else if (x_kind != y_kind) {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- return;
- }
- Type *t = core_type(target_type);
- switch (t->kind) {
- case Type_Basic:
- if (operand->mode == Addressing_Constant) {
- check_is_expressible(c, operand, t);
- if (operand->mode == Addressing_Invalid) {
- return;
- }
- update_expr_value(c, operand->expr, operand->value);
- } else {
- switch (operand->type->Basic.kind) {
- case Basic_UntypedBool:
- if (!is_type_boolean(target_type)) {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- case Basic_UntypedInteger:
- case Basic_UntypedFloat:
- case Basic_UntypedComplex:
- case Basic_UntypedRune:
- if (!is_type_numeric(target_type)) {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- case Basic_UntypedNil:
- if (is_type_any(target_type)) {
- target_type = t_untyped_nil;
- } else if (is_type_cstring(target_type)) {
- target_type = t_untyped_nil;
- } else if (!type_has_nil(target_type)) {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- }
- }
- break;
- case Type_Array: {
- Type *elem = base_array_type(t);
- if (check_is_assignable_to(c, operand, elem)) {
- operand->mode = Addressing_Value;
- } else {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- }
- case Type_Union:
- if (!is_operand_nil(*operand) && !is_operand_undef(*operand)) {
- isize count = t->Union.variants.count;
- ValidIndexAndScore *valids = gb_alloc_array(c->allocator, ValidIndexAndScore, count);
- defer (gb_free(c->allocator, valids));
- isize valid_count = 0;
- isize first_success_index = -1;
- for_array(i, t->Union.variants) {
- Type *vt = t->Union.variants[i];
- i64 score = 0;
- if (check_is_assignable_to_with_score(c, operand, vt, &score)) {
- valids[valid_count].index = i;
- valids[valid_count].score = score;
- valid_count += 1;
- if (first_success_index < 0) {
- first_success_index = i;
- }
- }
- }
- if (valid_count > 1) {
- gb_sort_array(valids, valid_count, valid_index_and_score_cmp);
- i64 best_score = valids[0].score;
- Type *best_type = t->Union.variants[valids[0].index];
- for (isize i = 1; i < valid_count; i++) {
- auto v = valids[i];
- Type *vt = t->Union.variants[v.index];
- if (best_score > v.score) {
- valid_count = i;
- break;
- }
- best_score = v.score;
- }
- first_success_index = valids[0].index;
- }
- gbString type_str = type_to_string(target_type);
- defer (gb_string_free(type_str));
- if (valid_count == 1) {
- operand->mode = Addressing_Value;
- operand->type = t->Union.variants[first_success_index];
- target_type = t->Union.variants[first_success_index];
- break;
- } else if (valid_count > 1) {
- GB_ASSERT(first_success_index >= 0);
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- gb_printf_err("Ambiguous type conversion to '%s', which variant did you mean:\n\t", type_str);
- i32 j = 0;
- for (i32 i = 0; i < valid_count; i++) {
- ValidIndexAndScore valid = valids[i];
- if (j > 0 && valid_count > 2) gb_printf_err(", ");
- if (j == valid_count-1) {
- if (valid_count == 2) gb_printf_err(" ");
- gb_printf_err("or ");
- }
- gbString str = type_to_string(t->Union.variants[valid.index]);
- gb_printf_err("'%s'", str);
- gb_string_free(str);
- j++;
- }
- gb_printf_err("\n\n");
- return;
- } else if (is_type_untyped_undef(operand->type) && type_has_undef(target_type)) {
- target_type = t_untyped_undef;
- } else if (!is_type_untyped_nil(operand->type) || !type_has_nil(target_type)) {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- if (count > 0) {
- gb_printf_err("'%s' is a union which only excepts the following types:\n", type_str);
- gb_printf_err("\t");
- for (i32 i = 0; i < count; i++) {
- Type *v = t->Union.variants[i];
- if (i > 0 && count > 2) gb_printf_err(", ");
- if (i == count-1) {
- if (count == 2) gb_printf_err(" ");
- gb_printf_err("or ");
- }
- gbString str = type_to_string(v);
- gb_printf_err("'%s'", str);
- gb_string_free(str);
- }
- gb_printf_err("\n\n");
- }
- return;
- }
- }
- /* fallthrough */
- default:
- if (is_type_untyped_undef(operand->type) && type_has_undef(target_type)) {
- target_type = t_untyped_undef;
- } else if (is_type_untyped_nil(operand->type) && type_has_nil(target_type)) {
- target_type = t_untyped_nil;
- } else {
- operand->mode = Addressing_Invalid;
- convert_untyped_error(c, operand, target_type);
- return;
- }
- break;
- }
- operand->type = target_type;
- update_expr_type(c, operand->expr, target_type, true);
- }
- bool check_index_value(CheckerContext *c, bool open_range, Ast *index_value, i64 max_count, i64 *value) {
- Operand operand = {Addressing_Invalid};
- check_expr(c, &operand, index_value);
- if (operand.mode == Addressing_Invalid) {
- if (value) *value = 0;
- return false;
- }
- convert_to_typed(c, &operand, t_int);
- if (operand.mode == Addressing_Invalid) {
- if (value) *value = 0;
- return false;
- }
- if (!is_type_integer(operand.type)) {
- gbString expr_str = expr_to_string(operand.expr);
- error(operand.expr, "Index '%s' must be an integer", expr_str);
- gb_string_free(expr_str);
- if (value) *value = 0;
- return false;
- }
- if (operand.mode == Addressing_Constant &&
- (c->stmt_state_flags & StmtStateFlag_no_bounds_check) == 0) {
- i64 i = exact_value_to_integer(operand.value).value_integer;
- if (i < 0) {
- gbString expr_str = expr_to_string(operand.expr);
- error(operand.expr, "Index '%s' cannot be a negative value", expr_str);
- gb_string_free(expr_str);
- if (value) *value = 0;
- return false;
- }
- if (max_count >= 0) { // NOTE(bill): Do array bound checking
- if (value) *value = i;
- bool out_of_bounds = false;
- if (open_range) {
- out_of_bounds = i > max_count;
- } else {
- out_of_bounds = i >= max_count;
- }
- if (out_of_bounds) {
- gbString expr_str = expr_to_string(operand.expr);
- error(operand.expr, "Index '%s' is out of bounds range 0..<%lld", expr_str, max_count);
- gb_string_free(expr_str);
- return false;
- }
- return true;
- }
- }
- // NOTE(bill): It's alright :D
- if (value) *value = -1;
- return true;
- }
- Entity *check_selector(CheckerContext *c, Operand *operand, Ast *node, Type *type_hint) {
- ast_node(se, SelectorExpr, node);
- bool check_op_expr = true;
- Entity *expr_entity = nullptr;
- Entity *entity = nullptr;
- Selection sel = {}; // NOTE(bill): Not used if it's an import name
- operand->expr = node;
- Ast *op_expr = se->expr;
- Ast *selector = unparen_expr(se->selector);
- if (selector == nullptr) {
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- if (selector->kind != Ast_Ident) {
- // if (selector->kind != Ast_Ident) {
- error(selector, "Illegal selector kind: '%.*s'", LIT(ast_strings[selector->kind]));
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- if (op_expr->kind == Ast_Ident) {
- String op_name = op_expr->Ident.token.string;
- Entity *e = scope_lookup(c->scope, op_name);
- add_entity_use(c, op_expr, e);
- expr_entity = e;
- Entity *original_e = e;
- if (e != nullptr && e->kind == Entity_ImportName && selector->kind == Ast_Ident) {
- // IMPORTANT NOTE(bill): This is very sloppy code but it's also very fragile
- // It pretty much needs to be in this order and this way
- // If you can clean this up, please do but be really careful
- String import_name = op_name;
- Scope *import_scope = e->ImportName.scope;
- String entity_name = selector->Ident.token.string;
- check_op_expr = false;
- entity = scope_lookup_current(import_scope, entity_name);
- bool is_declared = entity != nullptr;
- if (is_declared) {
- if (entity->kind == Entity_Builtin) {
- // NOTE(bill): Builtin's are in the universal scope which is part of every scopes hierarchy
- // This means that we should just ignore the found result through it
- is_declared = false;
- } else if ((entity->scope->flags&ScopeFlag_Global) == ScopeFlag_Global && (import_scope->flags&ScopeFlag_Global) == 0) {
- is_declared = false;
- }
- }
- if (!is_declared) {
- error(op_expr, "'%.*s' is not declared by '%.*s'", LIT(entity_name), LIT(import_name));
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- check_entity_decl(c, entity, nullptr, nullptr);
- GB_ASSERT(entity->type != nullptr);
- if (!is_entity_exported(entity)) {
- gbString sel_str = expr_to_string(selector);
- error(op_expr, "'%s' is not exported by '%.*s'", sel_str, LIT(import_name));
- gb_string_free(sel_str);
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- if (entity->kind == Entity_ProcGroup) {
- Array<Entity *> procs = entity->ProcGroup.entities;
- bool skip = false;
- for_array(i, procs) {
- Entity *p = procs[i];
- Type *t = base_type(p->type);
- if (t == t_invalid) {
- continue;
- }
- Operand x = {};
- x.mode = Addressing_Value;
- x.type = t;
- if (type_hint != nullptr) {
- if (check_is_assignable_to(c, &x, type_hint)) {
- entity = p;
- skip = true;
- break;
- }
- }
- }
- if (!skip) {
- GB_ASSERT(entity != nullptr);
- operand->mode = Addressing_ProcGroup;
- operand->type = t_invalid;
- operand->expr = node;
- operand->proc_group = entity;
- return entity;
- }
- }
- }
- }
- if (check_op_expr) {
- check_expr_base(c, operand, op_expr, nullptr);
- if (operand->mode == Addressing_Invalid) {
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- }
- if (entity == nullptr && selector->kind == Ast_Ident) {
- String field_name = selector->Ident.token.string;
- if (is_type_dynamic_array(type_deref(operand->type))) {
- init_mem_allocator(c->checker);
- }
- sel = lookup_field(operand->type, field_name, operand->mode == Addressing_Type);
- entity = sel.entity;
- // NOTE(bill): Add type info needed for fields like 'names'
- if (entity != nullptr && (entity->flags&EntityFlag_TypeField)) {
- add_type_info_type(c, operand->type);
- }
- if (is_type_enum(operand->type)) {
- add_type_info_type(c, operand->type);
- }
- }
- if (entity == nullptr) {
- gbString op_str = expr_to_string(op_expr);
- gbString type_str = type_to_string(operand->type);
- gbString sel_str = expr_to_string(selector);
- error(op_expr, "'%s' of type '%s' has no field '%s'", op_str, type_str, sel_str);
- gb_string_free(sel_str);
- gb_string_free(type_str);
- gb_string_free(op_str);
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- if (expr_entity != nullptr && expr_entity->kind == Entity_Constant && entity->kind != Entity_Constant) {
- gbString op_str = expr_to_string(op_expr);
- gbString type_str = type_to_string(operand->type);
- gbString sel_str = expr_to_string(selector);
- error(op_expr, "Cannot access non-constant field '%s' from '%s'", sel_str, op_str);
- gb_string_free(sel_str);
- gb_string_free(type_str);
- gb_string_free(op_str);
- operand->mode = Addressing_Invalid;
- operand->expr = node;
- return nullptr;
- }
- add_entity_use(c, selector, entity);
- switch (entity->kind) {
- case Entity_Constant:
- operand->mode = Addressing_Constant;
- operand->value = entity->Constant.value;
- break;
- case Entity_Variable:
- // TODO(bill): Is this the rule I need?
- if (operand->mode == Addressing_Immutable) {
- // Okay
- } else if (sel.indirect || operand->mode != Addressing_Value) {
- operand->mode = Addressing_Variable;
- } else {
- operand->mode = Addressing_Value;
- }
- break;
- case Entity_TypeName:
- operand->mode = Addressing_Type;
- break;
- case Entity_Procedure:
- operand->mode = Addressing_Value;
- break;
- case Entity_Builtin:
- operand->mode = Addressing_Builtin;
- operand->builtin_id = cast(BuiltinProcId)entity->Builtin.id;
- break;
- case Entity_ProcGroup:
- operand->mode = Addressing_ProcGroup;
- operand->proc_group = entity;
- break;
- // NOTE(bill): These cases should never be hit but are here for sanity reasons
- case Entity_Nil:
- operand->mode = Addressing_Value;
- break;
- }
- operand->type = entity->type;
- operand->expr = node;
- return entity;
- }
- bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id) {
- GB_ASSERT(call->kind == Ast_CallExpr);
- ast_node(ce, CallExpr, call);
- BuiltinProc *bp = &builtin_procs[id];
- {
- char *err = nullptr;
- if (ce->args.count < bp->arg_count) {
- err = "Too few";
- } else if (ce->args.count > bp->arg_count && !bp->variadic) {
- err = "Too many";
- }
- if (err != nullptr) {
- gbString expr = expr_to_string(ce->proc);
- error(ce->close, "%s arguments for '%s', expected %td, got %td",
- err, expr,
- bp->arg_count, ce->args.count);
- gb_string_free(expr);
- return false;
- }
- }
- switch (id) {
- // case BuiltinProc_new:
- case BuiltinProc_make:
- case BuiltinProc_size_of:
- case BuiltinProc_align_of:
- case BuiltinProc_offset_of:
- case BuiltinProc_type_info_of:
- case BuiltinProc_typeid_of:
- // NOTE(bill): The first arg may be a Type, this will be checked case by case
- break;
- default:
- if (ce->args.count > 0) {
- check_multi_expr(c, operand, ce->args[0]);
- }
- break;
- }
- if (ce->args.count > 0) {
- if (ce->args[0]->kind == Ast_FieldValue) {
- error(call, "'field = value' calling is not allowed on built-in procedures");
- return false;
- }
- }
- switch (id) {
- default:
- GB_PANIC("Implement built-in procedure: %.*s", LIT(builtin_procs[id].name));
- break;
- case BuiltinProc_DIRECTIVE: {
- ast_node(bd, BasicDirective, ce->proc);
- String name = bd->name;
- if (name == "location") {
- if (ce->args.count > 1) {
- error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count);
- }
- if (ce->args.count > 0) {
- Ast *arg = ce->args[0];
- Entity *e = nullptr;
- Operand o = {};
- if (arg->kind == Ast_Ident) {
- e = check_ident(c, &o, arg, nullptr, nullptr, true);
- } else if (arg->kind == Ast_SelectorExpr) {
- e = check_selector(c, &o, arg, nullptr);
- }
- if (e == nullptr) {
- error(ce->args[0], "'#location' expected a valid entity name");
- }
- }
- operand->type = t_source_code_location;
- operand->mode = Addressing_Value;
- } else if (name == "assert") {
- if (ce->args.count != 1) {
- error(call, "'#assert' expects 1 argument, got %td", ce->args.count);
- return false;
- }
- if (!is_type_boolean(operand->type) && operand->mode != Addressing_Constant) {
- gbString str = expr_to_string(ce->args[0]);
- error(call, "'%s' is not a constant boolean", str);
- gb_string_free(str);
- return false;
- }
- if (!operand->value.value_bool) {
- gbString arg = expr_to_string(ce->args[0]);
- error(call, "Compile time assertion: %s", arg);
- gb_string_free(arg);
- }
- operand->type = t_untyped_bool;
- operand->mode = Addressing_Constant;
- } else {
- GB_PANIC("Unhandled #%.*s", LIT(name));
- }
- break;
- }
- case BuiltinProc_len:
- case BuiltinProc_cap: {
- // len :: proc(Type) -> int
- // cap :: proc(Type) -> int
- Type *op_type = type_deref(operand->type);
- Type *type = t_int;
- AddressingMode mode = Addressing_Invalid;
- ExactValue value = {};
- if (is_type_string(op_type) && id == BuiltinProc_len) {
- if (operand->mode == Addressing_Constant) {
- mode = Addressing_Constant;
- String str = operand->value.value_string;
- value = exact_value_i64(str.len);
- type = t_untyped_integer;
- } else {
- mode = Addressing_Value;
- if (is_type_cstring(op_type)) {
- add_package_dependency(c, "runtime", "__cstring_len");
- }
- }
- } else if (is_type_array(op_type)) {
- Type *at = core_type(op_type);
- mode = Addressing_Constant;
- value = exact_value_i64(at->Array.count);
- type = t_untyped_integer;
- } else if (is_type_slice(op_type) && id == BuiltinProc_len) {
- mode = Addressing_Value;
- } else if (is_type_dynamic_array(op_type)) {
- mode = Addressing_Value;
- } else if (is_type_map(op_type)) {
- mode = Addressing_Value;
- }
- if (mode == Addressing_Invalid) {
- String name = builtin_procs[id].name;
- gbString t = type_to_string(operand->type);
- error(call, "'%.*s' is not supported for '%s'", LIT(name), t);
- return false;
- }
- operand->mode = mode;
- operand->value = value;
- operand->type = type;
- break;
- }
- #if 0
- case BuiltinProc_new: {
- // new :: proc(Type) -> ^Type
- Operand op = {};
- check_expr_or_type(c, &op, ce->args[0]);
- Type *type = op.type;
- if ((op.mode != Addressing_Type && type == nullptr) || type == t_invalid) {
- error(ce->args[0], "Expected a type for 'new'");
- return false;
- }
- operand->mode = Addressing_Value;
- operand->type = alloc_type_pointer(type);
- break;
- }
- #endif
- #if 0
- case BuiltinProc_new_slice: {
- // new_slice :: proc(Type, len: int) -> []Type
- // proc new_slice(Type, len, cap: int) -> []Type
- Operand op = {};
- check_expr_or_type(c, &op, ce->args[0]);
- Type *type = op.type;
- if ((op.mode != Addressing_Type && type == nullptr) || type == t_invalid) {
- error(ce->args[0], "Expected a type for 'new_slice'");
- return false;
- }
- isize arg_count = ce->args.count;
- if (arg_count < 2 || 3 < arg_count) {
- error(ce->args[0], "'new_slice' expects 2 or 3 arguments, found %td", arg_count);
- // NOTE(bill): Return the correct type to reduce errors
- } else {
- // If any are constant
- i64 sizes[2] = {};
- isize size_count = 0;
- for (isize i = 1; i < arg_count; i++) {
- i64 val = 0;
- bool ok = check_index_value(c, ce->args[i], -1, &val);
- if (ok && val >= 0) {
- GB_ASSERT(size_count < gb_count_of(sizes));
- sizes[size_count++] = val;
- }
- }
- if (size_count == 2 && sizes[0] > sizes[1]) {
- error(ce->args[1], "'new_slice' count and capacity are swapped");
- // No need quit
- }
- }
- operand->mode = Addressing_Value;
- operand->type = alloc_type_slice(type);
- break;
- }
- #endif
- case BuiltinProc_make: {
- // make :: proc(Type, len: int) -> Type
- // proc make(Type, len, cap: int) -> Type
- Operand op = {};
- check_expr_or_type(c, &op, ce->args[0]);
- Type *type = op.type;
- if ((op.mode != Addressing_Type && type == nullptr) || type == t_invalid) {
- error(ce->args[0], "Expected a type for 'make'");
- return false;
- }
- isize min_args = 0;
- isize max_args = 1;
- if (is_type_slice(type)) {
- min_args = 2;
- max_args = 2;
- add_package_dependency(c, "mem", "alloc");
- } else if (is_type_map(type)) {
- min_args = 1;
- max_args = 2;
- add_package_dependency(c, "runtime", "__dynamic_map_reserve");
- } else if (is_type_dynamic_array(type)) {
- min_args = 1;
- max_args = 3;
- add_package_dependency(c, "runtime", "__dynamic_array_make");
- } else {
- gbString str = type_to_string(type);
- error(call, "Cannot 'make' %s; type must be a slice, map, or dynamic array", str);
- gb_string_free(str);
- return false;
- }
- isize arg_count = ce->args.count;
- if (arg_count < min_args || max_args < arg_count) {
- error(ce->args[0], "'make' expects %td or %d argument, found %td", min_args, max_args, arg_count);
- return false;
- }
- // If any are constant
- i64 sizes[4] = {};
- isize size_count = 0;
- for (isize i = 1; i < arg_count; i++) {
- i64 val = 0;
- bool ok = check_index_value(c, false, ce->args[i], -1, &val);
- if (ok && val >= 0) {
- GB_ASSERT(size_count < gb_count_of(sizes));
- sizes[size_count++] = val;
- }
- }
- if (size_count == 2 && sizes[0] > sizes[1]) {
- error(ce->args[1], "'make' count and capacity are swapped");
- // No need quit
- }
- operand->mode = Addressing_Value;
- operand->type = type;
- break;
- }
- #if 0
- case BuiltinProc_free: {
- // free :: proc(^Type)
- // proc free([]Type)
- // proc free(string)
- // proc free(map[K]T)
- Type *type = operand->type;
- bool ok = false;
- if (is_type_pointer(type)) {
- ok = true;
- } else if (is_type_slice(type)) {
- ok = true;
- } else if (is_type_string(type)) {
- ok = true;
- } else if (is_type_dynamic_array(type)) {
- ok = true;
- } else if (is_type_dynamic_map(type)) {
- ok = true;
- }
- if (!ok) {
- gbString type_str = type_to_string(type);
- error(operand->expr, "Invalid type for 'free', got '%s'", type_str);
- gb_string_free(type_str);
- return false;
- }
- operand->mode = Addressing_NoValue;
- break;
- }
- #endif
- #if 0
- case BuiltinProc_reserve: {
- // reserve :: proc([dynamic]Type, count: int) {
- // reserve :: proc(map[Key]Type, count: int) {
- Type *type = operand->type;
- if (!is_type_dynamic_array(type) && !is_type_dynamic_map(type)) {
- gbString str = type_to_string(type);
- error(operand->expr, "Expected a dynamic array or dynamic map, got '%s'", str);
- gb_string_free(str);
- return false;
- }
- Ast *capacity = ce->args[1];
- Operand op = {};
- check_expr(c, &op, capacity);
- if (op.mode == Addressing_Invalid) {
- return false;
- }
- Type *arg_type = base_type(op.type);
- if (!is_type_integer(arg_type)) {
- error(operand->expr, "'reserve' capacities must be an integer");
- return false;
- }
- operand->type = nullptr;
- operand->mode = Addressing_NoValue;
- break;
- }
- #endif
- #if 0
- case BuiltinProc_clear: {
- Type *type = operand->type;
- bool is_pointer = is_type_pointer(type);
- type = base_type(type_deref(type));
- if (!is_type_dynamic_array(type) && !is_type_map(type) && !is_type_slice(type)) {
- gbString str = type_to_string(type);
- error(operand->expr, "Invalid type for 'clear', got '%s'", str);
- gb_string_free(str);
- return false;
- }
- operand->type = nullptr;
- operand->mode = Addressing_NoValue;
- break;
- }
- #endif
- #if 0
- case BuiltinProc_append: {
- // append :: proc([dynamic]Type, item: ..Type)
- // proc append([]Type, item: ..Type)
- Operand prev_operand = *operand;
- Type *type = operand->type;
- bool is_pointer = is_type_pointer(type);
- type = base_type(type_deref(type));
- if (!is_type_dynamic_array(type) && !is_type_slice(type)) {
- gbString str = type_to_string(type);
- error(operand->expr, "Expected a slice or dynamic array, got '%s'", str);
- gb_string_free(str);
- return false;
- }
- bool is_addressable = operand->mode == Addressing_Variable;
- if (is_pointer) {
- is_addressable = true;
- }
- if (!is_addressable) {
- error(operand->expr, "'append' can only operate on addressable values");
- return false;
- }
- Type *elem = nullptr;
- if (is_type_dynamic_array(type)) {
- elem = type->DynamicArray.elem;
- } else {
- elem = type->Slice.elem;
- }
- Type *slice_elem = alloc_type_slice(elem);
- Type *proc_type_params = alloc_type_tuple(c->allocator);
- proc_type_params->Tuple.variables = gb_alloc_array(c->allocator, Entity *, 2);
- proc_type_params->Tuple.variable_count = 2;
- proc_type_params->Tuple.variables[0] = alloc_entity_param(c->allocator, nullptr, blank_token, operand->type, false, false);
- proc_type_params->Tuple.variables[1] = alloc_entity_param(c->allocator, nullptr, blank_token, slice_elem, false, false);
- Type *proc_type = alloc_type_proc(nullptr, proc_type_params, 2, nullptr, false, true, ProcCC_Odin);
- check_call_arguments(c, &prev_operand, proc_type, call);
- if (prev_operand.mode == Addressing_Invalid) {
- return false;
- }
- operand->mode = Addressing_Value;
- operand->type = t_int;
- break;
- }
- #endif
- #if 0
- case BuiltinProc_delete: {
- // delete :: proc(map[Key]Value, key: Key)
- Type *type = operand->type;
- if (!is_type_map(type)) {
- gbString str = type_to_string(type);
- error(operand->expr, "Expected a map, got '%s'", str);
- gb_string_free(str);
- return false;
- }
- Type *key = base_type(type)->Map.key;
- Operand x = {Addressing_Invalid};
- Ast *key_node = ce->args[1];
- Operand op = {};
- check_expr(c, &op, key_node);
- if (op.mode == Addressing_Invalid) {
- return false;
- }
- if (!check_is_assignable_to(c, &op, key)) {
- gbString kt = type_to_string(key);
- gbString ot = type_to_string(op.type);
- error(operand->expr, "Expected a key of type '%s', got '%s'", key, ot);
- gb_string_free(ot);
- gb_string_free(kt);
- return false;
- }
- operand->mode = Addressing_NoValue;
- break;
- }
- #endif
- case BuiltinProc_size_of: {
- // size_of :: proc(Type or expr) -> untyped int
- Operand o = {};
- check_expr_or_type(c, &o, ce->args[0]);
- if (o.mode == Addressing_Invalid) {
- return false;
- }
- Type *t = o.type;
- if (t == nullptr || t == t_invalid) {
- error(ce->args[0], "Invalid argument for 'size_of'");
- return false;
- }
- t = default_type(t);
- operand->mode = Addressing_Constant;
- operand->value = exact_value_i64(type_size_of(t));
- operand->type = t_untyped_integer;
- break;
- }
- case BuiltinProc_align_of: {
- // align_of :: proc(Type or expr) -> untyped int
- Operand o = {};
- check_expr_or_type(c, &o, ce->args[0]);
- if (o.mode == Addressing_Invalid) {
- return false;
- }
- Type *t = o.type;
- if (t == nullptr || t == t_invalid) {
- error(ce->args[0], "Invalid argument for 'align_of'");
- return false;
- }
- t = default_type(t);
- operand->mode = Addressing_Constant;
- operand->value = exact_value_i64(type_align_of(t));
- operand->type = t_untyped_integer;
- break;
- }
- case BuiltinProc_offset_of: {
- // offset_of :: proc(Type, field) -> uintptr
- Operand op = {};
- Type *bt = check_type(c, ce->args[0]);
- Type *type = base_type(bt);
- if (type == nullptr || type == t_invalid) {
- error(ce->args[0], "Expected a type for 'offset_of'");
- return false;
- }
- Ast *field_arg = unparen_expr(ce->args[1]);
- if (field_arg == nullptr ||
- field_arg->kind != Ast_Ident) {
- error(field_arg, "Expected an identifier for field argument");
- return false;
- }
- if (is_type_array(type)) {
- error(field_arg, "Invalid type for 'offset_of'");
- return false;
- }
- ast_node(arg, Ident, field_arg);
- Selection sel = lookup_field(type, arg->token.string, operand->mode == Addressing_Type);
- if (sel.entity == nullptr) {
- gbString type_str = type_to_string(bt);
- error(ce->args[0],
- "'%s' has no field named '%.*s'", type_str, LIT(arg->token.string));
- gb_string_free(type_str);
- return false;
- }
- if (sel.indirect) {
- gbString type_str = type_to_string(bt);
- error(ce->args[0],
- "Field '%.*s' is embedded via a pointer in '%s'", LIT(arg->token.string), type_str);
- gb_string_free(type_str);
- return false;
- }
- operand->mode = Addressing_Constant;
- operand->value = exact_value_i64(type_offset_of_from_selection(type, sel));
- operand->type = t_uintptr;
- break;
- }
- case BuiltinProc_type_of: {
- // type_of :: proc(val: Type) -> type(Type)
- Ast *expr = ce->args[0];
- Operand o = {};
- check_expr_or_type(c, &o, expr);
- // check_assignment(c, operand, nullptr, str_lit("argument of 'type_of'"));
- if (o.mode == Addressing_Invalid || o.mode == Addressing_Builtin) {
- return false;
- }
- if (o.type == nullptr || o.type == t_invalid) {
- error(o.expr, "Invalid argument to 'type_of'");
- return false;
- }
- if (o.type == nullptr || o.type == t_invalid) {
- error(o.expr, "Invalid argument to 'type_of'");
- return false;
- }
- // NOTE(bill): Prevent type cycles for procedure declarations
- if (c->curr_proc_sig == o.type) {
- gbString s = expr_to_string(o.expr);
- error(o.expr, "Invalid cyclic type usage from 'type_of', got '%s'", s);
- gb_string_free(s);
- return false;
- }
- if (is_type_polymorphic(o.type)) {
- error(o.expr, "'type_of' of polymorphic type cannot be determined");
- return false;
- }
- operand->mode = Addressing_Type;
- operand->type = o.type;
- break;
- }
- case BuiltinProc_type_info_of: {
- // type_info_of :: proc(Type) -> ^Type_Info
- if (c->scope->flags&ScopeFlag_Global) {
- compiler_error("'type_info_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
- }
- // NOTE(bill): The type information may not be setup yet
- init_core_type_info(c->checker);
- Ast *expr = ce->args[0];
- Operand o = {};
- check_expr_or_type(c, &o, expr);
- if (o.mode == Addressing_Invalid) {
- return false;
- }
- Type *t = o.type;
- if (t == nullptr || t == t_invalid || is_type_polymorphic(operand->type)) {
- error(ce->args[0], "Invalid argument for 'type_info_of'");
- return false;
- }
- t = default_type(t);
- add_type_info_type(c, t);
- if (is_operand_value(o) && is_type_typeid(t)) {
- add_package_dependency(c, "runtime", "__type_info_of");
- } else if (o.mode != Addressing_Type) {
- error(expr, "Expected a type or typeid for 'type_info_of'");
- return false;
- }
- operand->mode = Addressing_Value;
- operand->type = t_type_info_ptr;
- break;
- }
- case BuiltinProc_typeid_of: {
- // typeid_of :: proc(Type) -> typeid
- if (c->scope->flags&ScopeFlag_Global) {
- compiler_error("'typeid_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
- }
- // NOTE(bill): The type information may not be setup yet
- init_core_type_info(c->checker);
- Ast *expr = ce->args[0];
- Operand o = {};
- check_expr_or_type(c, &o, expr);
- if (o.mode == Addressing_Invalid) {
- return false;
- }
- Type *t = o.type;
- if (t == nullptr || t == t_invalid || is_type_polymorphic(operand->type)) {
- error(ce->args[0], "Invalid argument for 'typeid_of'");
- return false;
- }
- t = default_type(t);
- add_type_info_type(c, t);
- t = base_type(t);
- if (is_operand_value(o) && are_types_identical(t, t_type_info_ptr)) {
- add_package_dependency(c, "runtime", "__typeid_of");
- } else if (o.mode != Addressing_Type) {
- error(expr, "Expected a type or type info for 'typeid_of'");
- return false;
- }
- operand->mode = Addressing_Value;
- operand->type = t_typeid;
- break;
- }
- case BuiltinProc_swizzle: {
- // swizzle :: proc(v: [N]T, ...int) -> [M]T
- Type *type = base_type(operand->type);
- if (!is_type_array(type)) {
- gbString type_str = type_to_string(operand->type);
- error(call,
- "You can only 'swizzle' an array, got '%s'",
- type_str);
- gb_string_free(type_str);
- return false;
- }
- i64 max_count = type->Array.count;
- Type *elem_type = type->Array.elem;
- i64 arg_count = 0;
- for_array(i, ce->args) {
- if (i == 0) {
- continue;
- }
- Ast *arg = ce->args[i];
- Operand op = {};
- check_expr(c, &op, arg);
- if (op.mode == Addressing_Invalid) {
- return false;
- }
- Type *arg_type = base_type(op.type);
- if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
- error(op.expr, "Indices to 'swizzle' must be constant integers");
- return false;
- }
- if (op.value.value_integer < 0) {
- error(op.expr, "Negative 'swizzle' index");
- return false;
- }
- if (max_count <= op.value.value_integer) {
- error(op.expr, "'swizzle' index exceeds length");
- return false;
- }
- arg_count++;
- }
- if (arg_count > max_count) {
- error(call, "Too many 'swizzle' indices, %td > %td", arg_count, max_count);
- return false;
- }
- if (arg_count < max_count) {
- operand->type = alloc_type_array(elem_type, arg_count);
- }
- operand->mode = Addressing_Value;
- break;
- }
- case BuiltinProc_complex: {
- // complex :: proc(real, imag: float_type) -> complex_type
- Operand x = *operand;
- Operand y = {};
- // NOTE(bill): Invalid will be the default till fixed
- operand->type = t_invalid;
- operand->mode = Addressing_Invalid;
- check_expr(c, &y, ce->args[1]);
- if (y.mode == Addressing_Invalid) {
- return false;
- }
- convert_to_typed(c, &x, y.type); if (x.mode == Addressing_Invalid) return false;
- convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
- if (x.mode == Addressing_Constant &&
- y.mode == Addressing_Constant) {
- if (is_type_numeric(x.type) && exact_value_imag(x.value).value_float == 0) {
- x.type = t_untyped_float;
- }
- if (is_type_numeric(y.type) && exact_value_imag(y.value).value_float == 0) {
- y.type = t_untyped_float;
- }
- }
- if (!are_types_identical(x.type, y.type)) {
- gbString tx = type_to_string(x.type);
- gbString ty = type_to_string(y.type);
- error(call, "Mismatched types to 'complex', '%s' vs '%s'", tx, ty);
- gb_string_free(ty);
- gb_string_free(tx);
- return false;
- }
- if (!is_type_float(x.type)) {
- gbString s = type_to_string(x.type);
- error(call, "Arguments have type '%s', expected a floating point", s);
- gb_string_free(s);
- return false;
- }
- if (x.mode == Addressing_Constant && y.mode == Addressing_Constant) {
- operand->value = exact_binary_operator_value(Token_Add, x.value, y.value);
- operand->mode = Addressing_Constant;
- } else {
- operand->mode = Addressing_Value;
- }
- BasicKind kind = core_type(x.type)->Basic.kind;
- switch (kind) {
- // case Basic_f16: operand->type = t_complex32; break;
- case Basic_f32: operand->type = t_complex64; break;
- case Basic_f64: operand->type = t_complex128; break;
- case Basic_UntypedFloat: operand->type = t_untyped_complex; break;
- default: GB_PANIC("Invalid type"); break;
- }
- break;
- }
- case BuiltinProc_real:
- case BuiltinProc_imag: {
- // real :: proc(x: type) -> float_type
- // proc imag(x: type) -> float_type
- Operand *x = operand;
- if (is_type_untyped(x->type)) {
- if (x->mode == Addressing_Constant) {
- if (is_type_numeric(x->type)) {
- x->type = t_untyped_complex;
- }
- } else {
- convert_to_typed(c, x, t_complex128);
- if (x->mode == Addressing_Invalid) {
- return false;
- }
- }
- }
- if (!is_type_complex(x->type)) {
- gbString s = type_to_string(x->type);
- error(call, "Argument has type '%s', expected a complex type", s);
- gb_string_free(s);
- return false;
- }
- if (x->mode == Addressing_Constant) {
- switch (id) {
- case BuiltinProc_real: x->value = exact_value_real(x->value); break;
- case BuiltinProc_imag: x->value = exact_value_imag(x->value); break;
- }
- } else {
- x->mode = Addressing_Value;
- }
- BasicKind kind = core_type(x->type)->Basic.kind;
- switch (kind) {
- case Basic_complex64: x->type = t_f32; break;
- case Basic_complex128: x->type = t_f64; break;
- case Basic_UntypedComplex: x->type = t_untyped_float; break;
- default: GB_PANIC("Invalid type"); break;
- }
- break;
- }
- case BuiltinProc_conj: {
- // conj :: proc(x: type) -> type
- Operand *x = operand;
- if (is_type_complex(x->type)) {
- if (x->mode == Addressing_Constant) {
- ExactValue v = exact_value_to_complex(x->value);
- f64 r = v.value_complex.real;
- f64 i = v.value_complex.imag;
- x->value = exact_value_complex(r, i);
- x->mode = Addressing_Constant;
- } else {
- x->mode = Addressing_Value;
- }
- } else {
- gbString s = type_to_string(x->type);
- error(call, "Expected a complex or quaternion, got '%s'", s);
- gb_string_free(s);
- return false;
- }
- break;
- }
- #if 0
- case BuiltinProc_slice_ptr: {
- // slice_ptr :: proc(a: ^T, len: int) -> []T
- // proc slice_ptr(a: ^T, len, cap: int) -> []T
- // ^T cannot be rawptr
- Type *ptr_type = base_type(operand->type);
- if (!is_type_pointer(ptr_type)) {
- gbString type_str = type_to_string(operand->type);
- error(call, "Expected a pointer to 'slice_ptr', got '%s'", type_str);
- gb_string_free(type_str);
- return false;
- }
- if (ptr_type == t_rawptr) {
- error(call, "'rawptr' cannot have pointer arithmetic");
- return false;
- }
- isize arg_count = ce->args.count;
- if (arg_count < 2 || 3 < arg_count) {
- error(ce->args[0], "'slice_ptr' expects 2 or 3 arguments, found %td", arg_count);
- // NOTE(bill): Return the correct type to reduce errors
- } else {
- // If any are constant
- i64 sizes[2] = {};
- isize size_count = 0;
- for (isize i = 1; i < arg_count; i++) {
- i64 val = 0;
- bool ok = check_index_value(c, false, ce->args[i], -1, &val);
- if (ok && val >= 0) {
- GB_ASSERT(size_count < gb_count_of(sizes));
- sizes[size_count++] = val;
- }
- }
- if (size_count == 2 && sizes[0] > sizes[1]) {
- error(ce->args[1], "'slice_ptr' count and capacity are swapped");
- // No need quit
- }
- }
- operand->type = alloc_type_slice(ptr_type->Pointer.elem);
- operand->mode = Addressing_Value;
- break;
- }
- case BuiltinProc_slice_to_bytes: {
- // slice_to_bytes :: proc(a: []T) -> []u8
- Type *slice_type = base_type(operand->type);
- if (!is_type_slice(slice_type)) {
- gbString type_str = type_to_string(operand->type);
- error(call, "Expected a slice type, got '%s'", type_str);
- gb_string_free(type_str);
- return false;
- }
- operand->type = t_u8_slice;
- operand->mode = Addressing_Value;
- break;
- }
- #endif
- case BuiltinProc_expand_to_tuple: {
- Type *type = base_type(operand->type);
- if (!is_type_struct(type) &&
- !is_type_union(type)) {
- gbString type_str = type_to_string(operand->type);
- error(call, "Expected a struct or union type, got '%s'", type_str);
- gb_string_free(type_str);
- return false;
- }
- gbAllocator a = c->allocator;
- Type *tuple = alloc_type_tuple();
- isize variable_count = type->Struct.fields.count;
- array_init(&tuple->Tuple.variables, a, variable_count);
- // TODO(bill): Should I copy each of the entities or is this good enough?
- gb_memmove_array(tuple->Tuple.variables.data, type->Struct.fields.data, variable_count);
- operand->type = tuple;
- operand->mode = Addressing_Value;
- break;
- }
- case BuiltinProc_min: {
- // min :: proc(a, b: ordered) -> ordered
- Type *type = base_type(operand->type);
- if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
- gbString type_str = type_to_string(operand->type);
- error(call, "Expected a ordered numeric type to 'min', got '%s'", type_str);
- gb_string_free(type_str);
- return false;
- }
- Ast *other_arg = ce->args[1];
- Operand a = *operand;
- Operand b = {};
- check_expr(c, &b, other_arg);
- if (b.mode == Addressing_Invalid) {
- return false;
- }
- if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
- gbString type_str = type_to_string(b.type);
- error(call,
- "Expected a ordered numeric type to 'min', got '%s'",
- type_str);
- gb_string_free(type_str);
- return false;
- }
- if (a.mode == Addressing_Constant &&
- b.mode == Addressing_Constant) {
- ExactValue x = a.value;
- ExactValue y = b.value;
- operand->mode = Addressing_Constant;
- if (compare_exact_values(Token_Lt, x, y)) {
- operand->value = x;
- operand->type = a.type;
- } else {
- operand->value = y;
- operand->type = b.type;
- }
- } else {
- operand->mode = Addressing_Value;
- operand->type = type;
- convert_to_typed(c, &a, b.type);
- if (a.mode == Addressing_Invalid) {
- return false;
- }
- convert_to_typed(c, &b, a.type);
- if (b.mode == Addressing_Invalid) {
- return false;
- }
- if (!are_types_identical(a.type, b.type)) {
- gbString type_a = type_to_string(a.type);
- gbString type_b = type_to_string(b.type);
- error(call,
- "Mismatched types to 'min', '%s' vs '%s'",
- type_a, type_b);
- gb_string_free(type_b);
- gb_string_free(type_a);
- return false;
- }
- {
- Type *bt = base_type(a.type);
- if (bt == t_f32) add_package_dependency(c, "runtime", "__min_f32");
- if (bt == t_f64) add_package_dependency(c, "runtime", "__min_f64");
- }
- }
- break;
- }
- case BuiltinProc_max: {
- // min :: proc(a, b: ordered) -> ordered
- Type *type = base_type(operand->type);
- if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
- gbString type_str = type_to_string(operand->type);
- error(call,
- "Expected a ordered numeric or string type to 'max', got '%s'",
- type_str);
- gb_string_free(type_str);
- return false;
- }
- Ast *other_arg = ce->args[1];
- Operand a = *operand;
- Operand b = {};
- check_expr(c, &b, other_arg);
- if (b.mode == Addressing_Invalid) {
- return false;
- }
- if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
- gbString type_str = type_to_string(b.type);
- error(call,
- "Expected a ordered numeric or string type to 'max', got '%s'",
- type_str);
- gb_string_free(type_str);
- return false;
- }
- if (a.mode == Addressing_Constant &&
- b.mode == Addressing_Constant) {
- ExactValue x = a.value;
- ExactValue y = b.value;
- operand->mode = Addressing_Constant;
- if (compare_exact_values(Token_Gt, x, y)) {
- operand->value = x;
- operand->type = a.type;
- } else {
- operand->value = y;
- operand->type = b.type;
- }
- } else {
- operand->mode = Addressing_Value;
- operand->type = type;
- convert_to_typed(c, &a, b.type);
- if (a.mode == Addressing_Invalid) {
- return false;
- }
- convert_to_typed(c, &b, a.type);
- if (b.mode == Addressing_Invalid) {
- return false;
- }
- if (!are_types_identical(a.type, b.type)) {
- gbString type_a = type_to_string(a.type);
- gbString type_b = type_to_string(b.type);
- error(call,
- "Mismatched types to 'max', '%s' vs '%s'",
- type_a, type_b);
- gb_string_free(type_b);
- gb_string_free(type_a);
- return false;
- }
- {
- Type *bt = base_type(a.type);
- if (bt == t_f32) add_package_dependency(c, "runtime", "__max_f32");
- if (bt == t_f64) add_package_dependency(c, "runtime", "__max_f64");
- }
- }
- break;
- }
- case BuiltinProc_abs: {
- // abs :: proc(n: numeric) -> numeric
- if (!is_type_numeric(operand->type)) {
- gbString type_str = type_to_string(operand->type);
- error(call, "Expected a numeric type to 'abs', got '%s'", type_str);
- gb_string_free(type_str);
- return false;
- }
- if (operand->mode == Addressing_Constant) {
- switch (operand->value.kind) {
- case ExactValue_Integer:
- operand->value.value_integer = gb_abs(operand->value.value_integer);
- break;
- case ExactValue_Float:
- operand->value.value_float = gb_abs(operand->value.value_float);
- break;
- case ExactValue_Complex: {
- f64 r = operand->value.value_complex.real;
- f64 i = operand->value.value_complex.imag;
- operand->value = exact_value_float(gb_sqrt(r*r + i*i));
- break;
- }
- default:
- GB_PANIC("Invalid numeric constant");
- break;
- }
- } else {
- operand->mode = Addressing_Value;
- {
- Type *bt = base_type(operand->type);
- if (bt == t_f32) add_package_dependency(c, "runtime", "__abs_f32");
- if (bt == t_f64) add_package_dependency(c, "runtime", "__abs_f64");
- if (bt == t_complex64) add_package_dependency(c, "runtime", "__abs_complex64");
- if (bt == t_complex128) add_package_dependency(c, "runtime", "__abs_complex128");
- }
- }
- if (is_type_complex(operand->type)) {
- operand->type = base_complex_elem_type(operand->type);
- }
- GB_ASSERT(!is_type_complex(operand->type));
- break;
- }
- case BuiltinProc_clamp: {
- // clamp :: proc(a, min, max: ordered) -> ordered
- Type *type = base_type(operand->type);
- if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
- gbString type_str = type_to_string(operand->type);
- error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
- gb_string_free(type_str);
- return false;
- }
- Ast *min_arg = ce->args[1];
- Ast *max_arg = ce->args[2];
- Operand x = *operand;
- Operand y = {};
- Operand z = {};
- check_expr(c, &y, min_arg);
- if (y.mode == Addressing_Invalid) {
- return false;
- }
- if (!is_type_ordered(y.type) || !(is_type_numeric(y.type) || is_type_string(y.type))) {
- gbString type_str = type_to_string(y.type);
- error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
- gb_string_free(type_str);
- return false;
- }
- check_expr(c, &z, max_arg);
- if (z.mode == Addressing_Invalid) {
- return false;
- }
- if (!is_type_ordered(z.type) || !(is_type_numeric(z.type) || is_type_string(z.type))) {
- gbString type_str = type_to_string(z.type);
- error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
- gb_string_free(type_str);
- return false;
- }
- if (x.mode == Addressing_Constant &&
- y.mode == Addressing_Constant &&
- z.mode == Addressing_Constant) {
- ExactValue a = x.value;
- ExactValue b = y.value;
- ExactValue c = z.value;
- operand->mode = Addressing_Constant;
- if (compare_exact_values(Token_Lt, a, b)) {
- operand->value = b;
- operand->type = y.type;
- } else if (compare_exact_values(Token_Gt, a, c)) {
- operand->value = c;
- operand->type = z.type;
- } else {
- operand->value = a;
- operand->type = x.type;
- }
- } else {
- operand->mode = Addressing_Value;
- operand->type = type;
- convert_to_typed(c, &x, y.type);
- if (x.mode == Addressing_Invalid) { return false; }
- convert_to_typed(c, &y, x.type);
- if (y.mode == Addressing_Invalid) { return false; }
- convert_to_typed(c, &x, z.type);
- if (x.mode == Addressing_Invalid) { return false; }
- convert_to_typed(c, &z, x.type);
- if (z.mode == Addressing_Invalid) { return false; }
- convert_to_typed(c, &y, z.type);
- if (y.mode == Addressing_Invalid) { return false; }
- convert_to_typed(c, &z, y.type);
- if (z.mode == Addressing_Invalid) { return false; }
- if (!are_types_identical(x.type, y.type) || !are_types_identical(x.type, z.type)) {
- gbString type_x = type_to_string(x.type);
- gbString type_y = type_to_string(y.type);
- gbString type_z = type_to_string(z.type);
- error(call,
- "Mismatched types to 'clamp', '%s', '%s', '%s'",
- type_x, type_y, type_z);
- gb_string_free(type_z);
- gb_string_free(type_y);
- gb_string_free(type_x);
- return false;
- }
- {
- Type *bt = base_type(x.type);
- if (bt == t_f32) {
- add_package_dependency(c, "runtime", "__min_f32");
- add_package_dependency(c, "runtime", "__max_f32");
- }
- if (bt == t_f64) {
- add_package_dependency(c, "runtime", "__min_f64");
- add_package_dependency(c, "runtime", "__max_f64");
- }
- }
- }
- break;
- }
- #if 0
- case BuiltinProc_transmute: {
- Operand op = {};
- check_expr_or_type(c, &op, ce->args[0]);
- Type *t = op.type;
- if ((op.mode != Addressing_Type && t == nullptr) || t == t_invalid) {
- error(ce->args[0], "Expected a type for 'transmute'");
- return false;
- }
- Ast *expr = ce->args[1];
- Operand *o = operand;
- check_expr(c, o, expr);
- if (o->mode == Addressing_Invalid) {
- return false;
- }
- if (o->mode == Addressing_Constant) {
- gbString expr_str = expr_to_string(o->expr);
- error(o->expr, "Cannot transmute a constant expression: '%s'", expr_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = expr;
- return false;
- }
- if (is_type_untyped(o->type)) {
- gbString expr_str = expr_to_string(o->expr);
- error(o->expr, "Cannot transmute untyped expression: '%s'", expr_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = expr;
- return false;
- }
- i64 srcz = type_size_of(o->type);
- i64 dstz = type_size_of(t);
- if (srcz != dstz) {
- gbString expr_str = expr_to_string(o->expr);
- gbString type_str = type_to_string(t);
- error(o->expr, "Cannot transmute '%s' to '%s', %lld vs %lld bytes", expr_str, type_str, srcz, dstz);
- gb_string_free(type_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = expr;
- return false;
- }
- o->mode = Addressing_Value;
- o->type = t;
- break;
- }
- #endif
- }
- return true;
- }
- isize add_dependencies_from_unpacking(CheckerContext *c, Entity **lhs, isize lhs_count, isize tuple_index, isize tuple_count) {
- if (lhs != nullptr) {
- for (isize j = 0; (tuple_index + j) < lhs_count && j < tuple_count; j++) {
- Entity *e = lhs[tuple_index + j];
- DeclInfo *decl = decl_info_of_entity(e);
- if (decl != nullptr) {
- c->decl = decl; // will be reset by the 'defer' any way
- for_array(k, decl->deps.entries) {
- Entity *dep = decl->deps.entries[k].ptr;
- add_declaration_dependency(c, dep); // TODO(bill): Should this be here?
- }
- }
- }
- }
- return tuple_count;
- }
- void check_unpack_arguments(CheckerContext *ctx, Entity **lhs, isize lhs_count, Array<Operand> *operands, Array<Ast *> const &rhs, bool allow_ok, bool *optional_ok_ = nullptr) {
- bool optional_ok = false;
- isize tuple_index = 0;
- for_array(i, rhs) {
- CheckerContext c_ = *ctx;
- CheckerContext *c = &c_;
- Operand o = {};
- Type *type_hint = nullptr;
- if (lhs != nullptr && tuple_index < lhs_count) {
- // NOTE(bill): override DeclInfo for dependency
- Entity *e = lhs[tuple_index];
- DeclInfo *decl = decl_info_of_entity(e);
- if (decl) c->decl = decl;
- type_hint = e->type;
- }
- check_expr_base(c, &o, rhs[i], type_hint);
- if (o.mode == Addressing_NoValue) {
- error_operand_no_value(&o);
- o.mode = Addressing_Invalid;
- }
- if (o.type == nullptr || o.type->kind != Type_Tuple) {
- if (allow_ok && lhs_count == 2 && rhs.count == 1 &&
- (o.mode == Addressing_MapIndex || o.mode == Addressing_OptionalOk)) {
- Type *tuple = make_optional_ok_type(o.type);
- add_type_and_value(&c->checker->info, o.expr, o.mode, tuple, o.value);
- Operand val = o;
- Operand ok = o;
- val.mode = Addressing_Value;
- ok.mode = Addressing_Value;
- ok.type = t_bool;
- array_add(operands, val);
- array_add(operands, ok);
- optional_ok = true;
- tuple_index += add_dependencies_from_unpacking(c, lhs, lhs_count, tuple_index, 2);
- } else {
- array_add(operands, o);
- tuple_index += 1;
- }
- } else {
- TypeTuple *tuple = &o.type->Tuple;
- for_array(j, tuple->variables) {
- o.type = tuple->variables[j]->type;
- array_add(operands, o);
- }
- isize count = tuple->variables.count;
- tuple_index += add_dependencies_from_unpacking(c, lhs, lhs_count, tuple_index, count);
- }
- }
- if (optional_ok_) *optional_ok_ = optional_ok;
- }
- CALL_ARGUMENT_CHECKER(check_call_arguments_internal) {
- ast_node(ce, CallExpr, call);
- GB_ASSERT(is_type_proc(proc_type));
- proc_type = base_type(proc_type);
- TypeProc *pt = &proc_type->Proc;
- isize param_count = 0;
- isize param_count_excluding_defaults = 0;
- bool variadic = pt->variadic;
- bool vari_expand = (ce->ellipsis.pos.line != 0);
- i64 score = 0;
- bool show_error = show_error_mode == CallArgumentMode_ShowErrors;
- TypeTuple *param_tuple = nullptr;
- if (pt->params != nullptr) {
- param_tuple = &pt->params->Tuple;
- param_count = param_tuple->variables.count;
- if (variadic) {
- for (isize i = param_count-1; i >= 0; i--) {
- Entity *e = param_tuple->variables[i];
- if (e->kind == Entity_TypeName) {
- break;
- }
- if (e->kind == Entity_Variable) {
- if (e->Variable.default_value.kind != ExactValue_Invalid ||
- e->Variable.default_is_nil ||
- e->Variable.default_is_location) {
- param_count--;
- continue;
- }
- }
- break;
- }
- param_count--;
- }
- }
- param_count_excluding_defaults = param_count;
- if (param_tuple != nullptr) {
- for (isize i = param_count-1; i >= 0; i--) {
- Entity *e = param_tuple->variables[i];
- if (e->kind == Entity_TypeName) {
- break;
- }
- if (e->kind == Entity_Variable) {
- if (e->Variable.default_value.kind != ExactValue_Invalid ||
- e->Variable.default_is_nil ||
- e->Variable.default_is_location) {
- param_count_excluding_defaults--;
- continue;
- }
- }
- break;
- }
- }
- CallArgumentError err = CallArgumentError_None;
- Type *final_proc_type = proc_type;
- Entity *gen_entity = nullptr;
- if (vari_expand && !variadic) {
- if (show_error) {
- error(ce->ellipsis,
- "Cannot use '...' in call to a non-variadic procedure: '%.*s'",
- LIT(ce->proc->Ident.token.string));
- }
- err = CallArgumentError_NonVariadicExpand;
- } else if (vari_expand && pt->c_vararg) {
- if (show_error) {
- error(ce->ellipsis,
- "Cannot use '...' in call to a '#c_vararg' variadic procedure: '%.*s'",
- LIT(ce->proc->Ident.token.string));
- }
- err = CallArgumentError_NonVariadicExpand;
- } else if (operands.count == 0 && param_count_excluding_defaults == 0) {
- err = CallArgumentError_None;
- } else {
- i32 error_code = 0;
- if (operands.count < param_count_excluding_defaults) {
- error_code = -1;
- } else if (!variadic && operands.count > param_count) {
- error_code = +1;
- }
- if (error_code != 0) {
- err = CallArgumentError_TooManyArguments;
- char *err_fmt = "Too many arguments for '%s', expected %td arguments";
- if (error_code < 0) {
- err = CallArgumentError_TooFewArguments;
- err_fmt = "Too few arguments for '%s', expected %td arguments";
- }
- if (show_error) {
- gbString proc_str = expr_to_string(ce->proc);
- error(call, err_fmt, proc_str, param_count_excluding_defaults);
- gb_string_free(proc_str);
- }
- } else {
- // NOTE(bill): Generate the procedure type for this generic instance
- PolyProcData poly_proc_data = {};
- if (pt->is_polymorphic && !pt->is_poly_specialized) {
- if (find_or_generate_polymorphic_procedure_from_parameters(c, entity, &operands, call, &poly_proc_data)) {
- gen_entity = poly_proc_data.gen_entity;
- GB_ASSERT(is_type_proc(gen_entity->type));
- final_proc_type = gen_entity->type;
- }
- }
- GB_ASSERT(is_type_proc(final_proc_type));
- TypeProc *pt = &final_proc_type->Proc;
- GB_ASSERT(pt->params != nullptr);
- auto sig_params = pt->params->Tuple.variables;
- isize operand_index = 0;
- isize max_operand_count = gb_min(param_count, operands.count);
- for (; operand_index < max_operand_count; operand_index++) {
- Entity *e = sig_params[operand_index];
- Type *t = e->type;
- Operand o = operands[operand_index];
- if (e->kind == Entity_TypeName) {
- // GB_ASSERT(!variadic);
- if (o.mode == Addressing_Invalid) {
- continue;
- } else if (o.mode != Addressing_Type) {
- if (show_error) {
- error(o.expr, "Expected a type for the argument '%.*s'", LIT(e->token.string));
- }
- err = CallArgumentError_WrongTypes;
- }
- if (are_types_identical(e->type, o.type)) {
- score += assign_score_function(1);
- } else {
- score += assign_score_function(10);
- }
- continue;
- }
- i64 s = 0;
- if (!check_is_assignable_to_with_score(c, &o, t, &s)) {
- if (show_error) {
- check_assignment(c, &o, t, str_lit("argument"));
- }
- err = CallArgumentError_WrongTypes;
- }
- score += s;
- }
- if (variadic) {
- bool variadic_expand = false;
- Type *slice = sig_params[param_count]->type;
- GB_ASSERT(is_type_slice(slice));
- Type *elem = base_type(slice)->Slice.elem;
- Type *t = elem;
- for (; operand_index < operands.count; operand_index++) {
- Operand o = operands[operand_index];
- if (vari_expand) {
- variadic_expand = true;
- t = slice;
- if (operand_index != param_count) {
- if (show_error) {
- error(o.expr, "'...' in a variadic procedure can only have one variadic argument at the end");
- }
- if (data) {
- data->score = score;
- data->result_type = final_proc_type->Proc.results;
- data->gen_entity = gen_entity;
- }
- return CallArgumentError_MultipleVariadicExpand;
- }
- }
- i64 s = 0;
- if (!check_is_assignable_to_with_score(c, &o, t, &s)) {
- if (show_error) {
- check_assignment(c, &o, t, str_lit("argument"));
- }
- err = CallArgumentError_WrongTypes;
- }
- score += s;
- if (is_type_any(elem)) {
- add_type_info_type(c, o.type);
- }
- }
- }
- }
- }
- if (data) {
- data->score = score;
- data->result_type = final_proc_type->Proc.results;
- data->gen_entity = gen_entity;
- }
- return err;
- }
- bool is_call_expr_field_value(AstCallExpr *ce) {
- GB_ASSERT(ce != nullptr);
- if (ce->args.count == 0) {
- return false;
- }
- return ce->args[0]->kind == Ast_FieldValue;
- }
- isize lookup_procedure_parameter(TypeProc *pt, String parameter_name) {
- isize param_count = pt->param_count;
- for (isize i = 0; i < param_count; i++) {
- Entity *e = pt->params->Tuple.variables[i];
- String name = e->token.string;
- if (is_blank_ident(name)) {
- continue;
- }
- if (name == parameter_name) {
- return i;
- }
- }
- return -1;
- }
- isize lookup_procedure_result(TypeProc *pt, String result_name) {
- isize result_count = pt->result_count;
- for (isize i = 0; i < result_count; i++) {
- Entity *e = pt->results->Tuple.variables[i];
- String name = e->token.string;
- if (is_blank_ident(name)) {
- continue;
- }
- if (name == result_name) {
- return i;
- }
- }
- return -1;
- }
- CALL_ARGUMENT_CHECKER(check_named_call_arguments) {
- ast_node(ce, CallExpr, call);
- GB_ASSERT(is_type_proc(proc_type));
- proc_type = base_type(proc_type);
- TypeProc *pt = &proc_type->Proc;
- i64 score = 0;
- bool show_error = show_error_mode == CallArgumentMode_ShowErrors;
- CallArgumentError err = CallArgumentError_None;
- isize param_count = pt->param_count;
- bool *visited = gb_alloc_array(c->allocator, bool, param_count);
- defer (gb_free(c->allocator, visited));
- auto ordered_operands = array_make<Operand>(c->allocator, param_count);
- defer (array_free(&ordered_operands));
- for_array(i, ce->args) {
- Ast *arg = ce->args[i];
- ast_node(fv, FieldValue, arg);
- if (fv->field->kind != Ast_Ident) {
- if (show_error) {
- gbString expr_str = expr_to_string(fv->field);
- error(arg, "Invalid parameter name '%s' in procedure call", expr_str);
- gb_string_free(expr_str);
- }
- err = CallArgumentError_InvalidFieldValue;
- continue;
- }
- String name = fv->field->Ident.token.string;
- isize index = lookup_procedure_parameter(pt, name);
- if (index < 0) {
- if (show_error) {
- error(arg, "No parameter named '%.*s' for this procedure type", LIT(name));
- }
- err = CallArgumentError_ParameterNotFound;
- continue;
- }
- if (visited[index]) {
- if (show_error) {
- error(arg, "Duplicate parameter '%.*s' in procedure call", LIT(name));
- }
- err = CallArgumentError_DuplicateParameter;
- continue;
- }
- visited[index] = true;
- ordered_operands[index] = operands[i];
- }
- // NOTE(bill): Check for default values and missing parameters
- isize param_count_to_check = param_count;
- if (pt->variadic) {
- param_count_to_check--;
- }
- for (isize i = 0; i < param_count_to_check; i++) {
- if (!visited[i]) {
- Entity *e = pt->params->Tuple.variables[i];
- if (is_blank_ident(e->token)) {
- continue;
- }
- if (e->kind == Entity_Variable) {
- if (e->Variable.default_value.kind != ExactValue_Invalid) {
- score += assign_score_function(1);
- continue;
- } else if (e->Variable.default_is_nil) {
- score += assign_score_function(1);
- continue;
- }
- }
- if (show_error) {
- if (e->kind == Entity_TypeName) {
- error(call, "Type parameter '%.*s' is missing in procedure call",
- LIT(e->token.string));
- } else if (e->kind == Entity_Constant && e->Constant.value.kind != ExactValue_Invalid) {
- // Ignore
- } else {
- gbString str = type_to_string(e->type);
- error(call, "Parameter '%.*s' of type '%s' is missing in procedure call",
- LIT(e->token.string), str);
- gb_string_free(str);
- }
- }
- err = CallArgumentError_ParameterMissing;
- }
- }
- Entity *gen_entity = nullptr;
- if (pt->is_polymorphic && !pt->is_poly_specialized && err == CallArgumentError_None) {
- PolyProcData poly_proc_data = {};
- if (find_or_generate_polymorphic_procedure_from_parameters(c, entity, &ordered_operands, call, &poly_proc_data)) {
- gen_entity = poly_proc_data.gen_entity;
- Type *gept = base_type(gen_entity->type);
- GB_ASSERT(is_type_proc(gept));
- proc_type = gept;
- pt = &gept->Proc;
- }
- }
- for (isize i = 0; i < param_count; i++) {
- Operand *o = &ordered_operands[i];
- if (o->mode == Addressing_Invalid) {
- continue;
- }
- Entity *e = pt->params->Tuple.variables[i];
- if (e->kind == Entity_TypeName) {
- GB_ASSERT(pt->is_polymorphic);
- if (o->mode != Addressing_Type) {
- if (show_error) {
- error(o->expr, "Expected a type for the argument '%.*s'", LIT(e->token.string));
- }
- err = CallArgumentError_WrongTypes;
- }
- if (are_types_identical(e->type, o->type)) {
- score += assign_score_function(1);
- } else {
- score += assign_score_function(10);
- }
- } else {
- i64 s = 0;
- if (!check_is_assignable_to_with_score(c, o, e->type, &s)) {
- if (show_error) {
- check_assignment(c, o, e->type, str_lit("procedure argument"));
- }
- err = CallArgumentError_WrongTypes;
- }
- score += s;
- }
- }
- if (data) {
- data->score = score;
- data->result_type = pt->results;
- data->gen_entity = gen_entity;
- }
- return err;
- }
- CallArgumentData check_call_arguments(CheckerContext *c, Operand *operand, Type *proc_type, Ast *call) {
- ast_node(ce, CallExpr, call);
- CallArgumentCheckerType *call_checker = check_call_arguments_internal;
- Array<Operand> operands = {};
- defer (array_free(&operands));
- Type *result_type = t_invalid;
- if (is_call_expr_field_value(ce)) {
- call_checker = check_named_call_arguments;
- operands = array_make<Operand>(heap_allocator(), ce->args.count);
- for_array(i, ce->args) {
- Ast *arg = ce->args[i];
- ast_node(fv, FieldValue, arg);
- check_expr_or_type(c, &operands[i], fv->value);
- }
- bool vari_expand = (ce->ellipsis.pos.line != 0);
- if (vari_expand) {
- // error(ce->ellipsis, "Invalid use of '...' with 'field = value' call'");
- }
- } else {
- operands = array_make<Operand>(heap_allocator(), 0, 2*ce->args.count);
- check_unpack_arguments(c, nullptr, -1, &operands, ce->args, false);
- }
- if (operand->mode == Addressing_ProcGroup) {
- check_entity_decl(c, operand->proc_group, nullptr, nullptr);
- Array<Entity *> procs = proc_group_entities(c, *operand);
- if (procs.count == 1) {
- Ast *ident = operand->expr;
- while (ident->kind == Ast_SelectorExpr) {
- Ast *s = ident->SelectorExpr.selector;
- ident = s;
- }
- Entity *e = procs[0];
- CallArgumentData data = {};
- CallArgumentError err = call_checker(c, call, e->type, e, operands, CallArgumentMode_ShowErrors, &data);
- Entity *entity_to_use = data.gen_entity != nullptr ? data.gen_entity : e;
- add_entity_use(c, ident, entity_to_use);
- return data;
- }
- ValidIndexAndScore *valids = gb_alloc_array(heap_allocator(), ValidIndexAndScore, procs.count);
- isize valid_count = 0;
- defer (gb_free(heap_allocator(), valids));
- gbString expr_name = expr_to_string(operand->expr);
- defer (gb_string_free(expr_name));
- for_array(i, procs) {
- Entity *p = procs[i];
- check_entity_decl(c, p, nullptr, nullptr);
- Type *pt = base_type(p->type);
- if (pt != nullptr && is_type_proc(pt)) {
- CallArgumentError err = CallArgumentError_None;
- CallArgumentData data = {};
- CheckerContext ctx = *c;
- ctx.no_polymorphic_errors = true;
- ctx.allow_polymorphic_types = is_type_polymorphic(pt);
- err = call_checker(&ctx, call, pt, p, operands, CallArgumentMode_NoErrors, &data);
- if (err == CallArgumentError_None) {
- valids[valid_count].index = i;
- valids[valid_count].score = data.score;
- valid_count++;
- }
- }
- }
- if (valid_count > 1) {
- gb_sort_array(valids, valid_count, valid_index_and_score_cmp);
- i64 best_score = valids[0].score;
- Entity *best_entity = procs[valids[0].index];
- for (isize i = 1; i < valid_count; i++) {
- if (best_score > valids[i].score) {
- valid_count = i;
- break;
- }
- if (best_entity == procs[valids[i].index]) {
- valid_count = i;
- break;
- }
- best_score = valids[i].score;
- }
- }
- if (valid_count == 0) {
- error(operand->expr, "No procedures or ambiguous call for procedure group '%s' that match with the given arguments", expr_name);
- gb_printf_err("\tGiven argument types: (");
- for_array(i, operands) {
- Operand o = operands[i];
- if (i > 0) gb_printf_err(", ");
- gbString type = type_to_string(o.type);
- defer (gb_string_free(type));
- gb_printf_err("%s", type);
- }
- gb_printf_err(")\n");
- if (procs.count > 0) {
- gb_printf_err("Did you mean to use one of the following:\n");
- }
- for_array(i, procs) {
- Entity *proc = procs[i];
- TokenPos pos = proc->token.pos;
- Type *t = base_type(proc->type);
- if (t == t_invalid) continue;
- GB_ASSERT(t->kind == Type_Proc);
- gbString pt;
- defer (gb_string_free(pt));
- if (t->Proc.node != nullptr) {
- pt = expr_to_string(t->Proc.node);
- } else {
- pt = type_to_string(t);
- }
- String name = proc->token.string;
- char const *sep = "::";
- if (proc->kind == Entity_Variable) {
- sep = ":=";
- }
- // gb_printf_err("\t%.*s %s %s at %.*s(%td:%td) with score %lld\n", LIT(name), sep, pt, LIT(pos.file), pos.line, pos.column, cast(long long)valids[i].score);
- gb_printf_err("\t%.*s %s %s at %.*s(%td:%td)\n", LIT(name), sep, pt, LIT(pos.file), pos.line, pos.column);
- }
- if (procs.count > 0) {
- gb_printf_err("\n");
- }
- result_type = t_invalid;
- } else if (valid_count > 1) {
- error(operand->expr, "Ambiguous procedure group call '%s' that match with the given arguments", expr_name);
- gb_printf_err("\tGiven argument types: (");
- for_array(i, operands) {
- Operand o = operands[i];
- if (i > 0) gb_printf_err(", ");
- gbString type = type_to_string(o.type);
- defer (gb_string_free(type));
- gb_printf_err("%s", type);
- }
- gb_printf_err(")\n");
- for (isize i = 0; i < valid_count; i++) {
- Entity *proc = procs[valids[i].index];
- TokenPos pos = proc->token.pos;
- Type *t = base_type(proc->type); GB_ASSERT(t->kind == Type_Proc);
- gbString pt;
- defer (gb_string_free(pt));
- if (t->Proc.node != nullptr) {
- pt = expr_to_string(t->Proc.node);
- } else {
- pt = type_to_string(t);
- }
- String name = proc->token.string;
- char const *sep = "::";
- if (proc->kind == Entity_Variable) {
- sep = ":=";
- }
- gb_printf_err("\t%.*s %s %s at %.*s(%td:%td)\n", LIT(name), sep, pt, LIT(pos.file), pos.line, pos.column);
- }
- result_type = t_invalid;
- } else {
- Ast *ident = operand->expr;
- while (ident->kind == Ast_SelectorExpr) {
- Ast *s = ident->SelectorExpr.selector;
- ident = s;
- }
- Entity *e = procs[valids[0].index];
- proc_type = e->type;
- CallArgumentData data = {};
- CallArgumentError err = call_checker(c, call, proc_type, e, operands, CallArgumentMode_ShowErrors, &data);
- Entity *entity_to_use = data.gen_entity != nullptr ? data.gen_entity : e;
- add_entity_use(c, ident, entity_to_use);
- return data;
- }
- } else {
- Ast *ident = operand->expr;
- while (ident->kind == Ast_SelectorExpr) {
- Ast *s = ident->SelectorExpr.selector;
- ident = s;
- }
- Entity *e = entity_of_ident(ident);
- CallArgumentData data = {};
- CallArgumentError err = call_checker(c, call, proc_type, e, operands, CallArgumentMode_ShowErrors, &data);
- Entity *entity_to_use = data.gen_entity != nullptr ? data.gen_entity : e;
- add_entity_use(c, ident, entity_to_use);
- return data;
- }
- CallArgumentData data = {};
- data.result_type = t_invalid;
- return data;
- }
- isize lookup_polymorphic_struct_parameter(TypeStruct *st, String parameter_name) {
- if (!st->is_polymorphic) return -1;
- TypeTuple *params = &st->polymorphic_params->Tuple;
- for_array(i, params->variables) {
- Entity *e = params->variables[i];
- String name = e->token.string;
- if (is_blank_ident(name)) {
- continue;
- }
- if (name == parameter_name) {
- return i;
- }
- }
- return -1;
- }
- CallArgumentError check_polymorphic_struct_type(CheckerContext *c, Operand *operand, Ast *call) {
- ast_node(ce, CallExpr, call);
- Type *original_type = operand->type;
- Type *struct_type = base_type(operand->type);
- GB_ASSERT(struct_type->kind == Type_Struct);
- TypeStruct *st = &struct_type->Struct;
- GB_ASSERT(st->is_polymorphic);
- bool show_error = true;
- Array<Operand> operands = {};
- defer (array_free(&operands));
- bool named_fields = false;
- if (is_call_expr_field_value(ce)) {
- named_fields = true;
- operands = array_make<Operand>(heap_allocator(), ce->args.count);
- for_array(i, ce->args) {
- Ast *arg = ce->args[i];
- ast_node(fv, FieldValue, arg);
- check_expr_or_type(c, &operands[i], fv->value);
- }
- bool vari_expand = (ce->ellipsis.pos.line != 0);
- if (vari_expand) {
- error(ce->ellipsis, "Invalid use of '...' in a polymorphic type call'");
- }
- } else {
- operands = array_make<Operand>(heap_allocator(), 0, 2*ce->args.count);
- check_unpack_arguments(c, nullptr, -1, &operands, ce->args, false);
- }
- CallArgumentError err = CallArgumentError_None;
- TypeTuple *tuple = &st->polymorphic_params->Tuple;
- isize param_count = tuple->variables.count;
- Array<Operand> ordered_operands = operands;
- if (named_fields) {
- bool *visited = gb_alloc_array(c->allocator, bool, param_count);
- // LEAK(bill)
- ordered_operands = array_make<Operand>(c->allocator, param_count);
- for_array(i, ce->args) {
- Ast *arg = ce->args[i];
- ast_node(fv, FieldValue, arg);
- if (fv->field->kind != Ast_Ident) {
- if (show_error) {
- gbString expr_str = expr_to_string(fv->field);
- error(arg, "Invalid parameter name '%s' in polymorphic type call", expr_str);
- gb_string_free(expr_str);
- }
- err = CallArgumentError_InvalidFieldValue;
- continue;
- }
- String name = fv->field->Ident.token.string;
- isize index = lookup_polymorphic_struct_parameter(st, name);
- if (index < 0) {
- if (show_error) {
- error(arg, "No parameter named '%.*s' for this polymorphic type", LIT(name));
- }
- err = CallArgumentError_ParameterNotFound;
- continue;
- }
- if (visited[index]) {
- if (show_error) {
- error(arg, "Duplicate parameter '%.*s' in polymorphic type", LIT(name));
- }
- err = CallArgumentError_DuplicateParameter;
- continue;
- }
- visited[index] = true;
- ordered_operands[index] = operands[i];
- }
- for (isize i = 0; i < param_count; i++) {
- if (!visited[i]) {
- Entity *e = tuple->variables[i];
- if (is_blank_ident(e->token)) {
- continue;
- }
- if (show_error) {
- if (e->kind == Entity_TypeName) {
- error(call, "Type parameter '%.*s' is missing in polymorphic type call",
- LIT(e->token.string));
- } else {
- gbString str = type_to_string(e->type);
- error(call, "Parameter '%.*s' of type '%s' is missing in polymorphic type call",
- LIT(e->token.string), str);
- gb_string_free(str);
- }
- }
- err = CallArgumentError_ParameterMissing;
- }
- }
- }
- if (err != 0) {
- operand->mode = Addressing_Invalid;
- return err;
- }
- i64 score = 0;
- for (isize i = 0; i < param_count; i++) {
- Operand *o = &ordered_operands[i];
- if (o->mode == Addressing_Invalid) {
- continue;
- }
- Entity *e = tuple->variables[i];
- if (e->kind == Entity_TypeName) {
- if (o->mode != Addressing_Type) {
- if (show_error) {
- error(o->expr, "Expected a type for the argument '%.*s'", LIT(e->token.string));
- }
- err = CallArgumentError_WrongTypes;
- }
- if (are_types_identical(e->type, o->type)) {
- score += assign_score_function(1);
- } else {
- score += assign_score_function(10);
- }
- } else {
- i64 s = 0;
- if (!check_is_assignable_to_with_score(c, o, e->type, &s)) {
- if (show_error) {
- check_assignment(c, o, e->type, str_lit("polymorphic type argument"));
- }
- err = CallArgumentError_WrongTypes;
- }
- o->type = e->type;
- if (o->mode != Addressing_Constant) {
- if (show_error) {
- error(o->expr, "Expected a constant value for this polymorphic type argument");
- }
- err = CallArgumentError_NoneConstantParameter;
- }
- score += s;
- }
- }
- if (param_count < ordered_operands.count) {
- error(call, "Too many polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
- err = CallArgumentError_TooManyArguments;
- } else if (param_count > ordered_operands.count) {
- error(call, "Too few polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
- err = CallArgumentError_TooFewArguments;
- }
- if (err != 0) {
- return err;
- }
- {
- // TODO(bill): Check for previous types
- gbAllocator a = c->allocator;
- Entity *found_entity = find_polymorphic_struct_entity(c, original_type, param_count, ordered_operands);
- if (found_entity) {
- operand->mode = Addressing_Type;
- operand->type = found_entity->type;
- return err;
- }
- String generated_name = make_string_c(expr_to_string(call));
- Type *named_type = alloc_type_named(generated_name, nullptr, nullptr);
- Ast *node = clone_ast(st->node);
- Type *struct_type = alloc_type_struct();
- struct_type->Struct.node = node;
- struct_type->Struct.polymorphic_parent = original_type;
- set_base_type(named_type, struct_type);
- check_open_scope(c, node);
- check_struct_type(c, struct_type, node, &ordered_operands, named_type, original_type);
- check_close_scope(c);
- operand->mode = Addressing_Type;
- operand->type = named_type;
- }
- return err;
- }
- ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *call) {
- ast_node(ce, CallExpr, call);
- if (ce->proc != nullptr &&
- ce->proc->kind == Ast_BasicDirective) {
- ast_node(bd, BasicDirective, ce->proc);
- String name = bd->name;
- if (name == "location" || name == "assert") {
- operand->mode = Addressing_Builtin;
- operand->builtin_id = BuiltinProc_DIRECTIVE;
- operand->expr = ce->proc;
- operand->type = t_invalid;
- add_type_and_value(&c->checker->info, ce->proc, operand->mode, operand->type, operand->value);
- } else {
- GB_PANIC("Unhandled #%.*s", LIT(name));
- }
- } else {
- check_expr_or_type(c, operand, ce->proc);
- }
- if (ce->args.count > 0) {
- bool fail = false;
- bool first_is_field_value = (ce->args[0]->kind == Ast_FieldValue);
- for_array(i, ce->args) {
- Ast *arg = ce->args[i];
- bool mix = false;
- if (first_is_field_value) {
- mix = arg->kind != Ast_FieldValue;
- } else {
- mix = arg->kind == Ast_FieldValue;
- }
- if (mix) {
- error(arg, "Mixture of 'field = value' and value elements in a procedure all is not allowed");
- fail = true;
- }
- }
- if (fail) {
- operand->mode = Addressing_Invalid;
- operand->expr = call;
- return Expr_Stmt;
- }
- }
- if (operand->mode == Addressing_Invalid) {
- for_array(i, ce->args) {
- Ast *arg = ce->args[i];
- if (arg->kind == Ast_FieldValue) {
- arg = arg->FieldValue.value;
- }
- check_expr_base(c, operand, arg, nullptr);
- }
- operand->mode = Addressing_Invalid;
- operand->expr = call;
- return Expr_Stmt;
- }
- if (operand->mode == Addressing_Type) {
- Type *t = operand->type;
- if (is_type_polymorphic_struct(t)) {
- auto err = check_polymorphic_struct_type(c, operand, call);
- if (err == 0) {
- Ast *ident = operand->expr;
- while (ident->kind == Ast_SelectorExpr) {
- Ast *s = ident->SelectorExpr.selector;
- ident = s;
- }
- Type *ot = operand->type; GB_ASSERT(ot->kind == Type_Named);
- Entity *e = ot->Named.type_name;
- add_entity_use(c, ident, e);
- add_type_and_value(&c->checker->info, call, Addressing_Type, ot, empty_exact_value);
- } else {
- operand->mode = Addressing_Invalid;
- operand->type = t_invalid;
- }
- } else {
- gbString str = type_to_string(t);
- defer (gb_string_free(str));
- operand->mode = Addressing_Invalid;
- isize arg_count = ce->args.count;
- switch (arg_count) {
- case 0: error(call, "Missing argument in conversion to '%s'", str); break;
- default: error(call, "Too many arguments in conversion to '%s'", str); break;
- case 1: {
- Ast *arg = ce->args[0];
- if (arg->kind == Ast_FieldValue) {
- error(call, "'field = value' cannot be used in a type conversion");
- arg = arg->FieldValue.value;
- // NOTE(bill): Carry on the cast regardless
- }
- check_expr(c, operand, arg);
- if (operand->mode != Addressing_Invalid) {
- check_cast(c, operand, t);
- }
- break;
- }
- }
- }
- return Expr_Expr;
- }
- if (operand->mode == Addressing_Builtin) {
- i32 id = operand->builtin_id;
- if (!check_builtin_procedure(c, operand, call, id)) {
- operand->mode = Addressing_Invalid;
- }
- operand->expr = call;
- return builtin_procs[id].kind;
- }
- Type *proc_type = base_type(operand->type);
- if (operand->mode != Addressing_ProcGroup) {
- bool valid_type = (proc_type != nullptr) && is_type_proc(proc_type);
- bool valid_mode = is_operand_value(*operand);
- if (!valid_type || !valid_mode) {
- Ast *e = operand->expr;
- gbString str = expr_to_string(e);
- gbString type_str = type_to_string(operand->type);
- error(e, "Cannot call a non-procedure: '%s' of type '%s'", str, type_str);
- gb_string_free(type_str);
- gb_string_free(str);
- operand->mode = Addressing_Invalid;
- operand->expr = call;
- return Expr_Stmt;
- }
- }
- // NOTE(bill): Should this be here or on the `add_entity_use`?
- // if (ce->proc != nullptr) {
- // Entity *e = entity_of_node(&c->info, ce->proc);
- // if (e != nullptr && e->kind == Entity_Procedure) {
- // String msg = e->Procedure.deprecated_message;
- // if (msg.len > 0) {
- // warning(call, "%.*s is deprecated: %.*s", LIT(e->token.string), LIT(msg));
- // }
- // }
- // }
- CallArgumentData data = check_call_arguments(c, operand, proc_type, call);
- Type *result_type = data.result_type;
- gb_zero_item(operand);
- operand->expr = call;
- if (result_type == t_invalid) {
- operand->mode = Addressing_Invalid;
- operand->type = t_invalid;
- return Expr_Stmt;
- }
- Type *pt = base_type(proc_type);
- if (result_type == nullptr) {
- operand->mode = Addressing_NoValue;
- } else {
- GB_ASSERT(is_type_tuple(result_type));
- switch (result_type->Tuple.variables.count) {
- case 0:
- operand->mode = Addressing_NoValue;
- break;
- case 1:
- operand->mode = Addressing_Value;
- operand->type = result_type->Tuple.variables[0]->type;
- break;
- default:
- operand->mode = Addressing_Value;
- operand->type = result_type;
- break;
- }
- }
- operand->expr = call;
- return Expr_Expr;
- }
- void check_expr_with_type_hint(CheckerContext *c, Operand *o, Ast *e, Type *t) {
- check_expr_base(c, o, e, t);
- check_not_tuple(c, o);
- char *err_str = nullptr;
- switch (o->mode) {
- case Addressing_NoValue:
- err_str = "used as a value";
- break;
- case Addressing_Type:
- err_str = "is not an expression";
- break;
- case Addressing_Builtin:
- err_str = "must be called";
- break;
- }
- if (err_str != nullptr) {
- gbString str = expr_to_string(e);
- error(e, "'%s' %s", str, err_str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- }
- }
- void check_set_mode_with_indirection(Operand *o, bool indirection) {
- if (o->mode != Addressing_Immutable) {
- if (indirection) {
- o->mode = Addressing_Variable;
- } else if (o->mode != Addressing_Variable &&
- o->mode != Addressing_Constant) {
- o->mode = Addressing_Value;
- }
- }
- }
- bool check_set_index_data(Operand *o, Type *type, bool indirection, i64 *max_count) {
- Type *t = base_type(type_deref(type));
- switch (t->kind) {
- case Type_Basic:
- if (t->Basic.kind == Basic_string) {
- if (o->mode == Addressing_Constant) {
- *max_count = o->value.value_string.len;
- }
- check_set_mode_with_indirection(o, indirection);
- o->type = t_u8;
- return true;
- }
- break;
- case Type_Array:
- *max_count = t->Array.count;
- check_set_mode_with_indirection(o, indirection);
- o->type = t->Array.elem;
- return true;
- case Type_Slice:
- o->type = t->Slice.elem;
- if (o->mode != Addressing_Immutable) {
- o->mode = Addressing_Variable;
- }
- return true;
- case Type_DynamicArray:
- o->type = t->DynamicArray.elem;
- check_set_mode_with_indirection(o, indirection);
- return true;
- }
- return false;
- }
- bool ternary_compare_types(Type *x, Type *y) {
- if (is_type_untyped_undef(x) && type_has_undef(y)) {
- return true;
- } else if (is_type_untyped_nil(x) && type_has_nil(y)) {
- return true;
- } else if (is_type_untyped_undef(y) && type_has_undef(x)) {
- return true;
- } else if (is_type_untyped_nil(y) && type_has_nil(x)) {
- return true;
- }
- return are_types_identical(x, y);
- }
- ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ExprKind kind = Expr_Stmt;
- o->mode = Addressing_Invalid;
- o->type = t_invalid;
- switch (node->kind) {
- default:
- return kind;
- case_ast_node(be, BadExpr, node)
- return kind;
- case_end;
- case_ast_node(i, Implicit, node)
- switch (i->kind) {
- case Token_context:
- if (c->proc_name.len == 0) {
- error(node, "'context' is only allowed within procedures");
- return kind;
- }
- init_core_context(c->checker);
- o->mode = Addressing_Immutable;
- o->type = t_context;
- break;
- case Token_size_of:
- o->mode = Addressing_Builtin;
- o->builtin_id = BuiltinProc_size_of;
- break;
- case Token_align_of:
- o->mode = Addressing_Builtin;
- o->builtin_id = BuiltinProc_align_of;
- break;
- case Token_offset_of:
- o->mode = Addressing_Builtin;
- o->builtin_id = BuiltinProc_offset_of;
- break;
- case Token_type_of:
- o->mode = Addressing_Builtin;
- o->builtin_id = BuiltinProc_type_of;
- break;
- default:
- error(node, "Illegal implicit name '%.*s'", LIT(i->string));
- return kind;
- }
- case_end;
- case_ast_node(i, Ident, node);
- check_ident(c, o, node, nullptr, type_hint, false);
- case_end;
- case_ast_node(u, Undef, node);
- o->mode = Addressing_Value;
- o->type = t_untyped_undef;
- case_end;
- case_ast_node(bl, BasicLit, node);
- // NOTE(bill, 2018-06-17): Placing this in the parser is slower than
- // placing it here for some reason. So don't move it to the parsing
- // stage if you _think_ it will be faster, only do it if you _know_ it
- // will be faster.
- Type *t = t_invalid;
- switch (bl->token.kind) {
- case Token_Integer: t = t_untyped_integer; break;
- case Token_Float: t = t_untyped_float; break;
- case Token_String: t = t_untyped_string; break;
- case Token_Rune: t = t_untyped_rune; break;
- case Token_Imag: {
- String s = bl->token.string;
- Rune r = s[s.len-1];
- switch (r) {
- case 'i': t = t_untyped_complex; break;
- }
- break;
- }
- default:
- GB_PANIC("Unknown literal");
- break;
- }
- o->mode = Addressing_Constant;
- o->type = t;
- o->value = exact_value_from_basic_literal(bl->token);
- case_end;
- case_ast_node(bd, BasicDirective, node);
- o->mode = Addressing_Constant;
- if (bd->name == "file") {
- o->type = t_untyped_string;
- o->value = exact_value_string(bd->token.pos.file);
- } else if (bd->name == "line") {
- o->type = t_untyped_integer;
- o->value = exact_value_i64(bd->token.pos.line);
- } else if (bd->name == "procedure") {
- if (c->curr_proc_decl == nullptr) {
- error(node, "#procedure may only be used within procedures");
- o->type = t_untyped_string;
- o->value = exact_value_string(str_lit(""));
- } else {
- o->type = t_untyped_string;
- o->value = exact_value_string(c->proc_name);
- }
- } else if (bd->name == "caller_location") {
- init_core_source_code_location(c->checker);
- error(node, "#caller_location may only be used as a default argument parameter");
- o->type = t_source_code_location;
- o->mode = Addressing_Value;
- } else {
- GB_PANIC("Unknown basic directive");
- }
- case_end;
- case_ast_node(pg, ProcGroup, node);
- error(node, "Illegal use of a procedure group");
- o->mode = Addressing_Invalid;
- case_end;
- case_ast_node(pl, ProcLit, node);
- CheckerContext ctx = *c;
- DeclInfo *decl = nullptr;
- Type *type = alloc_type(Type_Proc);
- check_open_scope(&ctx, pl->type);
- {
- decl = make_decl_info(ctx.allocator, ctx.scope, ctx.decl);
- decl->proc_lit = node;
- ctx.decl = decl;
- if (pl->tags != 0) {
- error(node, "A procedure literal cannot have tags");
- pl->tags = 0; // TODO(bill): Should I zero this?!
- }
- check_procedure_type(&ctx, type, pl->type);
- if (!is_type_proc(type)) {
- gbString str = expr_to_string(node);
- error(node, "Invalid procedure literal '%s'", str);
- gb_string_free(str);
- check_close_scope(&ctx);
- return kind;
- }
- if (pl->body == nullptr) {
- error(node, "A procedure literal must have a body");
- return kind;
- }
- check_procedure_later(ctx.checker, ctx.file, empty_token, decl, type, pl->body, pl->tags);
- }
- check_close_scope(&ctx);
- o->mode = Addressing_Value;
- o->type = type;
- case_end;
- case_ast_node(te, TernaryExpr, node);
- Operand cond = {Addressing_Invalid};
- check_expr(c, &cond, te->cond);
- if (cond.mode != Addressing_Invalid && !is_type_boolean(cond.type)) {
- error(te->cond, "Non-boolean condition in if expression");
- }
- Operand x = {Addressing_Invalid};
- Operand y = {Addressing_Invalid};
- check_expr_or_type(c, &x, te->x, type_hint);
- if (te->y != nullptr) {
- check_expr_or_type(c, &y, te->y, type_hint);
- } else {
- error(node, "A ternary expression must have an else clause");
- return kind;
- }
- if (x.type == nullptr || x.type == t_invalid ||
- y.type == nullptr || y.type == t_invalid) {
- return kind;
- }
- if (x.mode == Addressing_Type && y.mode == Addressing_Type &&
- cond.mode == Addressing_Constant && is_type_boolean(cond.type)) {
- o->mode = Addressing_Type;
- if (cond.value.value_bool) {
- o->type = x.type;
- o->expr = x.expr;
- } else {
- o->type = y.type;
- o->expr = y.expr;
- }
- return Expr_Expr;
- }
- convert_to_typed(c, &x, y.type);
- if (x.mode == Addressing_Invalid) {
- return kind;
- }
- convert_to_typed(c, &y, x.type);
- if (y.mode == Addressing_Invalid) {
- x.mode = Addressing_Invalid;
- return kind;
- }
- if (!ternary_compare_types(x.type, y.type)) {
- gbString its = type_to_string(x.type);
- gbString ets = type_to_string(y.type);
- error(node, "Mismatched types in ternary expression, %s vs %s", its, ets);
- gb_string_free(ets);
- gb_string_free(its);
- return kind;
- }
- Type *type = x.type;
- if (is_type_untyped_nil(type) || is_type_untyped_undef(type)) {
- type = y.type;
- }
- o->type = type;
- o->mode = Addressing_Value;
- if (cond.mode == Addressing_Constant && is_type_boolean(cond.type) &&
- x.mode == Addressing_Constant &&
- y.mode == Addressing_Constant) {
- o->mode = Addressing_Constant;
- if (cond.value.value_bool) {
- o->value = x.value;
- } else {
- o->value = y.value;
- }
- }
- case_end;
- case_ast_node(cl, CompoundLit, node);
- Type *type = type_hint;
- bool is_to_be_determined_array_count = false;
- bool is_constant = true;
- if (cl->type != nullptr) {
- type = nullptr;
- // [?]Type
- if (cl->type->kind == Ast_ArrayType && cl->type->ArrayType.count != nullptr) {
- Ast *count = cl->type->ArrayType.count;
- if (count->kind == Ast_UnaryExpr &&
- count->UnaryExpr.op.kind == Token_Question) {
- type = alloc_type_array(check_type(c, cl->type->ArrayType.elem), -1);
- is_to_be_determined_array_count = true;
- }
- }
- if (type == nullptr) {
- type = check_type(c, cl->type);
- }
- }
- if (type == nullptr) {
- error(node, "Missing type in compound literal");
- return kind;
- }
- Type *t = base_type(type);
- if (is_type_polymorphic(t)) {
- gbString str = type_to_string(type);
- error(node, "Cannot use a polymorphic type for a compound literal, got '%s'", str);
- o->expr = node;
- o->type = type;
- gb_string_free(str);
- return kind;
- }
- switch (t->kind) {
- case Type_Struct: {
- if (is_type_union(t)) {
- is_constant = false;
- }
- if (cl->elems.count == 0) {
- break; // NOTE(bill): No need to init
- }
- if (!is_type_struct(t)) {
- if (cl->elems.count != 0) {
- gbString type_str = type_to_string(type);
- error(node, "Illegal compound literal type '%s'", type_str);
- gb_string_free(type_str);
- }
- break;
- }
- { // Checker values
- isize field_count = t->Struct.fields.count;
- isize min_field_count = t->Struct.fields.count;
- for (isize i = min_field_count-1; i >= 0; i--) {
- Entity *e = t->Struct.fields[i];
- GB_ASSERT(e->kind == Entity_Variable);
- if (e->Variable.default_is_nil) {
- min_field_count--;
- } else if (e->Variable.default_is_undef) {
- min_field_count--;
- } else if (e->Variable.default_value.kind != ExactValue_Invalid) {
- min_field_count--;
- } else {
- break;
- }
- }
- if (cl->elems[0]->kind == Ast_FieldValue) {
- bool *fields_visited = gb_alloc_array(c->allocator, bool, field_count);
- for_array(i, cl->elems) {
- Ast *elem = cl->elems[i];
- if (elem->kind != Ast_FieldValue) {
- error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
- continue;
- }
- ast_node(fv, FieldValue, elem);
- if (fv->field->kind != Ast_Ident) {
- gbString expr_str = expr_to_string(fv->field);
- error(elem, "Invalid field name '%s' in structure literal", expr_str);
- gb_string_free(expr_str);
- continue;
- }
- String name = fv->field->Ident.token.string;
- Selection sel = lookup_field(type, name, o->mode == Addressing_Type);
- bool is_unknown = sel.entity == nullptr;
- if (is_unknown) {
- error(elem, "Unknown field '%.*s' in structure literal", LIT(name));
- continue;
- }
- if (sel.index.count > 1) {
- error(elem, "Cannot assign to an anonymous field '%.*s' in a structure literal (at the moment)", LIT(name));
- continue;
- }
- Entity *field = t->Struct.fields[sel.index[0]];
- add_entity_use(c, fv->field, field);
- if (fields_visited[sel.index[0]]) {
- error(elem, "Duplicate field '%.*s' in structure literal", LIT(name));
- continue;
- }
- fields_visited[sel.index[0]] = true;
- check_expr_with_type_hint(c, o, fv->value, field->type);
- if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type)) {
- is_constant = false;
- }
- if (is_constant) {
- is_constant = o->mode == Addressing_Constant;
- }
- check_assignment(c, o, field->type, str_lit("structure literal"));
- }
- } else {
- bool seen_field_value = false;
- for_array(index, cl->elems) {
- Entity *field = nullptr;
- Ast *elem = cl->elems[index];
- if (elem->kind == Ast_FieldValue) {
- seen_field_value = true;
- // error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
- // continue;
- } else if (seen_field_value) {
- error(elem, "Value elements cannot be used after a 'field = value'");
- continue;
- }
- if (index >= field_count) {
- error(o->expr, "Too many values in structure literal, expected %td, got %td", field_count, cl->elems.count);
- break;
- }
- if (field == nullptr) {
- field = t->Struct.fields[index];
- }
- check_expr_with_type_hint(c, o, elem, field->type);
- if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type)) {
- is_constant = false;
- }
- if (is_constant) {
- is_constant = o->mode == Addressing_Constant;
- }
- check_assignment(c, o, field->type, str_lit("structure literal"));
- }
- if (cl->elems.count < field_count) {
- if (min_field_count < field_count) {
- if (cl->elems.count < min_field_count) {
- error(cl->close, "Too few values in structure literal, expected at least %td, got %td", min_field_count, cl->elems.count);
- }
- } else {
- error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count);
- }
- }
- }
- }
- break;
- }
- case Type_Slice:
- case Type_Array:
- case Type_DynamicArray:
- {
- Type *elem_type = nullptr;
- String context_name = {};
- i64 max_type_count = -1;
- if (t->kind == Type_Slice) {
- elem_type = t->Slice.elem;
- context_name = str_lit("slice literal");
- } else if (t->kind == Type_Array) {
- elem_type = t->Array.elem;
- context_name = str_lit("array literal");
- if (!is_to_be_determined_array_count) {
- max_type_count = t->Array.count;
- }
- } else if (t->kind == Type_DynamicArray) {
- elem_type = t->DynamicArray.elem;
- context_name = str_lit("dynamic array literal");
- is_constant = false;
- add_package_dependency(c, "runtime", "__dynamic_array_reserve");
- add_package_dependency(c, "runtime", "__dynamic_array_append");
- } else {
- GB_PANIC("unreachable");
- }
- i64 max = 0;
- isize index = 0;
- Type *bet = base_type(elem_type);
- if (!elem_type_can_be_constant(bet)) {
- is_constant = false;
- }
- if (bet == t_invalid) {
- break;
- }
- for (; index < cl->elems.count; index++) {
- Ast *e = cl->elems[index];
- if (e == nullptr) {
- error(node, "Invalid literal element");
- continue;
- }
- if (e->kind == Ast_FieldValue) {
- error(e, "'field = value' is only allowed in struct literals");
- continue;
- }
- if (0 <= max_type_count && max_type_count <= index) {
- error(e, "Index %lld is out of bounds (>= %lld) for %.*s", index, max_type_count, LIT(context_name));
- }
- Operand operand = {};
- check_expr_with_type_hint(c, &operand, e, elem_type);
- check_assignment(c, &operand, elem_type, context_name);
- is_constant = is_constant && operand.mode == Addressing_Constant;
- }
- if (max < index) {
- max = index;
- }
- if (t->kind == Type_Array) {
- if (is_to_be_determined_array_count) {
- t->Array.count = max;
- } else if (0 < max && max < t->Array.count) {
- error(node, "Expected %lld values for this array literal, got %lld", cast(long long)t->Array.count, cast(long long)max);
- }
- }
- break;
- }
- case Type_Basic: {
- if (!is_type_any(t)) {
- if (cl->elems.count != 0) {
- error(node, "Illegal compound literal");
- }
- break;
- }
- if (cl->elems.count == 0) {
- break; // NOTE(bill): No need to init
- }
- { // Checker values
- Type *field_types[2] = {t_rawptr, t_typeid};
- isize field_count = 2;
- if (cl->elems[0]->kind == Ast_FieldValue) {
- bool fields_visited[2] = {};
- for_array(i, cl->elems) {
- Ast *elem = cl->elems[i];
- if (elem->kind != Ast_FieldValue) {
- error(elem, "Mixture of 'field = value' and value elements in a 'any' literal is not allowed");
- continue;
- }
- ast_node(fv, FieldValue, elem);
- if (fv->field->kind != Ast_Ident) {
- gbString expr_str = expr_to_string(fv->field);
- error(elem, "Invalid field name '%s' in 'any' literal", expr_str);
- gb_string_free(expr_str);
- continue;
- }
- String name = fv->field->Ident.token.string;
- Selection sel = lookup_field(type, name, o->mode == Addressing_Type);
- if (sel.entity == nullptr) {
- error(elem, "Unknown field '%.*s' in 'any' literal", LIT(name));
- continue;
- }
- isize index = sel.index[0];
- if (fields_visited[index]) {
- error(elem, "Duplicate field '%.*s' in 'any' literal", LIT(name));
- continue;
- }
- fields_visited[index] = true;
- check_expr(c, o, fv->value);
- // NOTE(bill): 'any' literals can never be constant
- is_constant = false;
- check_assignment(c, o, field_types[index], str_lit("'any' literal"));
- }
- } else {
- for_array(index, cl->elems) {
- Ast *elem = cl->elems[index];
- if (elem->kind == Ast_FieldValue) {
- error(elem, "Mixture of 'field = value' and value elements in a 'any' literal is not allowed");
- continue;
- }
- check_expr(c, o, elem);
- if (index >= field_count) {
- error(o->expr, "Too many values in 'any' literal, expected %td", field_count);
- break;
- }
- // NOTE(bill): 'any' literals can never be constant
- is_constant = false;
- check_assignment(c, o, field_types[index], str_lit("'any' literal"));
- }
- if (cl->elems.count < field_count) {
- error(cl->close, "Too few values in 'any' literal, expected %td, got %td", field_count, cl->elems.count);
- }
- }
- }
- break;
- }
- case Type_Map: {
- if (cl->elems.count == 0) {
- break;
- }
- is_constant = false;
- { // Checker values
- for_array(i, cl->elems) {
- Ast *elem = cl->elems[i];
- if (elem->kind != Ast_FieldValue) {
- error(elem, "Only 'field = value' elements are allowed in a map literal");
- continue;
- }
- ast_node(fv, FieldValue, elem);
- check_expr_with_type_hint(c, o, fv->field, t->Map.key);
- check_assignment(c, o, t->Map.key, str_lit("map literal"));
- if (o->mode == Addressing_Invalid) {
- continue;
- }
- check_expr_with_type_hint(c, o, fv->value, t->Map.value);
- check_assignment(c, o, t->Map.value, str_lit("map literal"));
- }
- }
- add_package_dependency(c, "runtime", "__dynamic_map_reserve");
- add_package_dependency(c, "runtime", "__dynamic_map_set");
- break;
- }
- default: {
- if (cl->elems.count == 0) {
- break; // NOTE(bill): No need to init
- }
- gbString str = type_to_string(type);
- error(node, "Invalid compound literal type '%s'", str);
- gb_string_free(str);
- return kind;
- }
- }
- if (is_constant) {
- o->mode = Addressing_Constant;
- o->value = exact_value_compound(node);
- } else {
- o->mode = Addressing_Value;
- }
- o->type = type;
- case_end;
- case_ast_node(pe, ParenExpr, node);
- kind = check_expr_base(c, o, pe->expr, type_hint);
- o->expr = node;
- case_end;
- case_ast_node(te, TagExpr, node);
- String name = te->name.string;
- error(node, "Unknown tag expression, #%.*s", LIT(name));
- if (te->expr) {
- kind = check_expr_base(c, o, te->expr, type_hint);
- }
- o->expr = node;
- case_end;
- case_ast_node(re, RunExpr, node);
- // TODO(bill): Tag expressions
- kind = check_expr_base(c, o, re->expr, type_hint);
- o->expr = node;
- case_end;
- case_ast_node(ta, TypeAssertion, node);
- check_expr(c, o, ta->expr);
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- Type *t = check_type(c, ta->type);
- if (o->mode == Addressing_Constant) {
- gbString expr_str = expr_to_string(o->expr);
- error(o->expr, "A type assertion cannot be applied to a constant expression: '%s'", expr_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- if (is_type_untyped(o->type)) {
- gbString expr_str = expr_to_string(o->expr);
- error(o->expr, "A type assertion cannot be applied to an untyped expression: '%s'", expr_str);
- gb_string_free(expr_str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- bool src_is_ptr = is_type_pointer(o->type);
- Type *src = type_deref(o->type);
- Type *dst = t;
- Type *bsrc = base_type(src);
- Type *bdst = base_type(dst);
- if (is_type_union(src)) {
- bool ok = false;
- for_array(i, bsrc->Union.variants) {
- Type *vt = bsrc->Union.variants[i];
- if (are_types_identical(vt, dst)) {
- ok = true;
- break;
- }
- }
- if (!ok) {
- gbString expr_str = expr_to_string(o->expr);
- gbString dst_type_str = type_to_string(t);
- defer (gb_string_free(expr_str));
- defer (gb_string_free(dst_type_str));
- if (bsrc->Union.variants.count == 0) {
- error(o->expr, "Cannot type assert '%s' to '%s' as this is an empty union", expr_str, dst_type_str);
- } else {
- error(o->expr, "Cannot type assert '%s' to '%s' as it is not a variant of that union", expr_str, dst_type_str);
- }
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- add_type_info_type(c, o->type);
- add_type_info_type(c, t);
- o->type = t;
- o->mode = Addressing_OptionalOk;
- } else if (is_type_any(src)) {
- o->type = t;
- o->mode = Addressing_OptionalOk;
- add_type_info_type(c, o->type);
- add_type_info_type(c, t);
- } else {
- gbString str = type_to_string(o->type);
- error(o->expr, "Type assertions can only operate on unions and 'any', got %s", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- add_package_dependency(c, "runtime", "type_assertion_check");
- case_end;
- case_ast_node(tc, TypeCast, node);
- check_expr_or_type(c, o, tc->type);
- if (o->mode != Addressing_Type) {
- gbString str = expr_to_string(tc->type);
- error(tc->type, "Expected a type, got %s", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- }
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- Type *type = o->type;
- check_expr_base(c, o, tc->expr, type);
- if (o->mode != Addressing_Invalid) {
- switch (tc->token.kind) {
- case Token_transmute:
- check_transmute(c, node, o, type);
- break;
- case Token_cast:
- check_cast(c, o, type);
- break;
- default:
- error(node, "Invalid AST: Invalid casting expression");
- o->mode = Addressing_Invalid;
- break;
- }
- }
- return Expr_Expr;
- case_end;
- case_ast_node(ac, AutoCast, node);
- check_expr_base(c, o, ac->expr, type_hint);
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- if (type_hint) {
- check_cast(c, o, type_hint);
- }
- o->expr = node;
- return Expr_Expr;
- case_end;
- case_ast_node(ue, UnaryExpr, node);
- check_expr_base(c, o, ue->expr, type_hint);
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- check_unary_expr(c, o, ue->op, node);
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- case_end;
- case_ast_node(be, BinaryExpr, node);
- check_binary_expr(c, o, node);
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- case_end;
- case_ast_node(se, SelectorExpr, node);
- check_selector(c, o, node, type_hint);
- case_end;
- case_ast_node(ie, IndexExpr, node);
- check_expr(c, o, ie->expr);
- if (o->mode == Addressing_Invalid) {
- o->expr = node;
- return kind;
- }
- Type *t = base_type(type_deref(o->type));
- bool is_ptr = is_type_pointer(o->type);
- bool is_const = o->mode == Addressing_Constant;
- if (is_type_map(t)) {
- Operand key = {};
- check_expr(c, &key, ie->index);
- check_assignment(c, &key, t->Map.key, str_lit("map index"));
- if (key.mode == Addressing_Invalid) {
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- o->mode = Addressing_MapIndex;
- o->type = t->Map.value;
- o->expr = node;
- add_package_dependency(c, "runtime", "__dynamic_map_get");
- add_package_dependency(c, "runtime", "__dynamic_map_set");
- return Expr_Expr;
- }
- i64 max_count = -1;
- bool valid = check_set_index_data(o, t, is_ptr, &max_count);
- if (is_const) {
- valid = false;
- }
- if (!valid) {
- gbString str = expr_to_string(o->expr);
- gbString type_str = type_to_string(o->type);
- defer (gb_string_free(str));
- defer (gb_string_free(type_str));
- if (is_const) {
- error(o->expr, "Cannot index a constant '%s'", str);
- } else {
- error(o->expr, "Cannot index '%s' of type '%s'", str, type_str);
- }
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- if (ie->index == nullptr) {
- gbString str = expr_to_string(o->expr);
- error(o->expr, "Missing index for '%s'", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- i64 index = 0;
- bool ok = check_index_value(c, false, ie->index, max_count, &index);
- case_end;
- case_ast_node(se, SliceExpr, node);
- check_expr(c, o, se->expr);
- if (o->mode == Addressing_Invalid) {
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- bool valid = false;
- i64 max_count = -1;
- Type *t = base_type(type_deref(o->type));
- switch (t->kind) {
- case Type_Basic:
- if (t->Basic.kind == Basic_string) {
- valid = true;
- if (o->mode == Addressing_Constant) {
- max_count = o->value.value_string.len;
- }
- o->type = t_string;
- }
- break;
- case Type_Array:
- valid = true;
- max_count = t->Array.count;
- if (o->mode != Addressing_Variable) {
- gbString str = expr_to_string(node);
- error(node, "Cannot slice array '%s', value is not addressable", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- o->type = alloc_type_slice(t->Array.elem);
- break;
- case Type_Slice:
- valid = true;
- o->type = type_deref(o->type);
- break;
- case Type_DynamicArray:
- valid = true;
- o->type = alloc_type_slice(t->DynamicArray.elem);
- break;
- }
- if (!valid) {
- gbString str = expr_to_string(o->expr);
- error(o->expr, "Cannot slice '%s'", str);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- if (o->mode != Addressing_Immutable) {
- o->mode = Addressing_Value;
- }
- if (se->low == nullptr && se->high != nullptr) {
- // error(se->interval0, "1st index is required if a 2nd index is specified");
- // It is okay to continue as it will assume the 1st index is zero
- }
- TokenKind interval_kind = se->interval.kind;
- i64 indices[2] = {};
- Ast *nodes[2] = {se->low, se->high};
- for (isize i = 0; i < gb_count_of(nodes); i++) {
- i64 index = max_count;
- if (nodes[i] != nullptr) {
- i64 capacity = -1;
- if (max_count >= 0) {
- capacity = max_count;
- }
- i64 j = 0;
- if (check_index_value(c, interval_kind == Token_Ellipsis, nodes[i], capacity, &j)) {
- index = j;
- }
- } else if (i == 0) {
- index = 0;
- }
- indices[i] = index;
- }
- for (isize i = 0; i < gb_count_of(indices); i++) {
- i64 a = indices[i];
- for (isize j = i+1; j < gb_count_of(indices); j++) {
- i64 b = indices[j];
- if (a > b && b >= 0) {
- error(se->close, "Invalid slice indices: [%td > %td]", a, b);
- }
- }
- }
- case_end;
- case_ast_node(ce, CallExpr, node);
- return check_call_expr(c, o, node);
- case_end;
- case_ast_node(de, DerefExpr, node);
- check_expr_or_type(c, o, de->expr);
- if (o->mode == Addressing_Invalid) {
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- } else {
- Type *t = base_type(o->type);
- if (t->kind == Type_Pointer && !is_type_empty_union(t->Pointer.elem)) {
- if (o->mode != Addressing_Immutable) {
- o->mode = Addressing_Variable;
- }
- o->type = t->Pointer.elem;
- } else {
- gbString str = expr_to_string(o->expr);
- gbString typ = type_to_string(o->type);
- error(o->expr, "Cannot dereference '%s' of type '%s'", str, typ);
- gb_string_free(typ);
- gb_string_free(str);
- o->mode = Addressing_Invalid;
- o->expr = node;
- return kind;
- }
- }
- case_end;
- case Ast_TypeType:
- case Ast_PolyType:
- case Ast_ProcType:
- case Ast_PointerType:
- case Ast_ArrayType:
- case Ast_DynamicArrayType:
- case Ast_StructType:
- case Ast_UnionType:
- // case Ast_RawUnionType:
- case Ast_EnumType:
- case Ast_MapType:
- o->mode = Addressing_Type;
- o->type = check_type(c, node);
- break;
- }
- kind = Expr_Expr;
- o->expr = node;
- return kind;
- }
- ExprKind check_expr_base(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
- ExprKind kind = check_expr_base_internal(c, o, node, type_hint);
- Type *type = nullptr;
- ExactValue value = {ExactValue_Invalid};
- switch (o->mode) {
- case Addressing_Invalid:
- type = t_invalid;
- break;
- case Addressing_NoValue:
- type = nullptr;
- break;
- case Addressing_Constant:
- value = o->value;
- type = o->type;
- break;
- default:
- type = o->type;
- break;
- }
- if (type != nullptr && is_type_untyped(type)) {
- add_untyped(&c->checker->info, node, false, o->mode, type, value);
- } else {
- add_type_and_value(&c->checker->info, node, o->mode, type, value);
- }
- return kind;
- }
- void check_multi_expr(CheckerContext *c, Operand *o, Ast *e) {
- check_expr_base(c, o, e, nullptr);
- switch (o->mode) {
- default:
- return; // NOTE(bill): Valid
- case Addressing_NoValue:
- error_operand_no_value(o);
- break;
- case Addressing_Type:
- error_operand_not_expression(o);
- break;
- }
- o->mode = Addressing_Invalid;
- }
- void check_not_tuple(CheckerContext *c, Operand *o) {
- if (o->mode == Addressing_Value) {
- // NOTE(bill): Tuples are not first class thus never named
- if (o->type->kind == Type_Tuple) {
- isize count = o->type->Tuple.variables.count;
- GB_ASSERT(count != 1);
- error(o->expr,
- "%td-valued tuple found where single value expected", count);
- o->mode = Addressing_Invalid;
- }
- }
- }
- void check_expr(CheckerContext *c, Operand *o, Ast *e) {
- check_multi_expr(c, o, e);
- check_not_tuple(c, o);
- }
- void check_expr_or_type(CheckerContext *c, Operand *o, Ast *e, Type *type_hint) {
- check_expr_base(c, o, e, type_hint);
- check_not_tuple(c, o);
- error_operand_no_value(o);
- }
- gbString write_expr_to_string(gbString str, Ast *node);
- gbString write_struct_fields_to_string(gbString str, Array<Ast *> const ¶ms) {
- for_array(i, params) {
- if (i > 0) {
- str = gb_string_appendc(str, ", ");
- }
- str = write_expr_to_string(str, params[i]);
- }
- return str;
- }
- gbString string_append_string(gbString str, String string) {
- if (string.len > 0) {
- return gb_string_append_length(str, &string[0], string.len);
- }
- return str;
- }
- gbString string_append_token(gbString str, Token token) {
- return string_append_string(str, token.string);
- }
- gbString write_expr_to_string(gbString str, Ast *node) {
- if (node == nullptr)
- return str;
- if (is_ast_stmt(node)) {
- GB_ASSERT("stmt passed to write_expr_to_string");
- }
- switch (node->kind) {
- default:
- str = gb_string_appendc(str, "(BadExpr)");
- break;
- case_ast_node(i, Ident, node);
- str = string_append_token(str, i->token);
- case_end;
- case_ast_node(i, Implicit, node);
- str = string_append_token(str, *i);
- case_end;
- case_ast_node(bl, BasicLit, node);
- str = string_append_token(str, bl->token);
- case_end;
- case_ast_node(bd, BasicDirective, node);
- str = gb_string_append_rune(str, '#');
- str = string_append_string(str, bd->name);
- case_end;
- case_ast_node(ud, Undef, node);
- str = gb_string_appendc(str, "---");
- case_end;
- case_ast_node(pg, ProcGroup, node);
- str = gb_string_appendc(str, "proc[");
- for_array(i, pg->args) {
- if (i > 0) str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, pg->args[i]);
- }
- str = gb_string_append_rune(str, ']');
- case_end;
- case_ast_node(pl, ProcLit, node);
- str = write_expr_to_string(str, pl->type);
- case_end;
- case_ast_node(cl, CompoundLit, node);
- str = write_expr_to_string(str, cl->type);
- str = gb_string_append_rune(str, '{');
- for_array(i, cl->elems) {
- if (i > 0) str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, cl->elems[i]);
- }
- str = gb_string_append_rune(str, '}');
- case_end;
- case_ast_node(te, TagExpr, node);
- str = gb_string_append_rune(str, '#');
- str = string_append_token(str, te->name);
- str = write_expr_to_string(str, te->expr);
- case_end;
- case_ast_node(ue, UnaryExpr, node);
- str = string_append_token(str, ue->op);
- str = write_expr_to_string(str, ue->expr);
- case_end;
- case_ast_node(de, DerefExpr, node);
- str = write_expr_to_string(str, de->expr);
- str = gb_string_append_rune(str, '^');
- case_end;
- case_ast_node(be, BinaryExpr, node);
- str = write_expr_to_string(str, be->left);
- str = gb_string_append_rune(str, ' ');
- str = string_append_token(str, be->op);
- str = gb_string_append_rune(str, ' ');
- str = write_expr_to_string(str, be->right);
- case_end;
- case_ast_node(te, TernaryExpr, node);
- str = write_expr_to_string(str, te->cond);
- str = gb_string_appendc(str, " ? ");
- str = write_expr_to_string(str, te->x);
- str = gb_string_appendc(str, " : ");
- str = write_expr_to_string(str, te->y);
- case_end;
- case_ast_node(pe, ParenExpr, node);
- str = gb_string_append_rune(str, '(');
- str = write_expr_to_string(str, pe->expr);
- str = gb_string_append_rune(str, ')');
- case_end;
- case_ast_node(se, SelectorExpr, node);
- str = write_expr_to_string(str, se->expr);
- str = gb_string_append_rune(str, '.');
- str = write_expr_to_string(str, se->selector);
- case_end;
- case_ast_node(ta, TypeAssertion, node);
- str = write_expr_to_string(str, ta->expr);
- str = gb_string_appendc(str, ".(");
- str = write_expr_to_string(str, ta->type);
- str = gb_string_append_rune(str, ')');
- case_end;
- case_ast_node(tc, TypeCast, node);
- str = string_append_token(str, tc->token);
- str = gb_string_append_rune(str, '(');
- str = write_expr_to_string(str, tc->type);
- str = gb_string_append_rune(str, ')');
- str = write_expr_to_string(str, tc->expr);
- case_end;
- case_ast_node(ac, AutoCast, node);
- str = string_append_token(str, ac->token);
- str = gb_string_append_rune(str, ' ');
- str = write_expr_to_string(str, ac->expr);
- case_end;
- case_ast_node(ie, IndexExpr, node);
- str = write_expr_to_string(str, ie->expr);
- str = gb_string_append_rune(str, '[');
- str = write_expr_to_string(str, ie->index);
- str = gb_string_append_rune(str, ']');
- case_end;
- case_ast_node(se, SliceExpr, node);
- str = write_expr_to_string(str, se->expr);
- str = gb_string_append_rune(str, '[');
- str = write_expr_to_string(str, se->low);
- str = string_append_token(str, se->interval);
- str = write_expr_to_string(str, se->high);
- str = gb_string_append_rune(str, ']');
- case_end;
- case_ast_node(e, Ellipsis, node);
- str = gb_string_appendc(str, "...");
- str = write_expr_to_string(str, e->expr);
- case_end;
- case_ast_node(fv, FieldValue, node);
- str = write_expr_to_string(str, fv->field);
- str = gb_string_appendc(str, " = ");
- str = write_expr_to_string(str, fv->value);
- case_end;
- case_ast_node(ht, HelperType, node);
- str = gb_string_appendc(str, "#type ");
- str = write_expr_to_string(str, ht->type);
- case_end;
- case_ast_node(ht, DistinctType, node);
- str = gb_string_appendc(str, "distinct ");
- str = write_expr_to_string(str, ht->type);
- case_end;
- case_ast_node(pt, PolyType, node);
- str = gb_string_append_rune(str, '$');
- str = write_expr_to_string(str, pt->type);
- if (pt->specialization != nullptr) {
- str = gb_string_append_rune(str, '/');
- str = write_expr_to_string(str, pt->specialization);
- }
- case_end;
- case_ast_node(pt, PointerType, node);
- str = gb_string_append_rune(str, '^');
- str = write_expr_to_string(str, pt->type);
- case_end;
- case_ast_node(at, ArrayType, node);
- str = gb_string_append_rune(str, '[');
- if (at->count != nullptr &&
- at->count->kind == Ast_UnaryExpr &&
- at->count->UnaryExpr.op.kind == Token_Question) {
- str = gb_string_appendc(str, "?");
- } else {
- str = write_expr_to_string(str, at->count);
- }
- str = gb_string_append_rune(str, ']');
- str = write_expr_to_string(str, at->elem);
- case_end;
- case_ast_node(at, DynamicArrayType, node);
- str = gb_string_appendc(str, "[dynamic]");
- str = write_expr_to_string(str, at->elem);
- case_end;
- case_ast_node(mt, MapType, node);
- str = gb_string_appendc(str, "map[");
- str = write_expr_to_string(str, mt->key);
- str = gb_string_append_rune(str, ']');
- str = write_expr_to_string(str, mt->value);
- case_end;
- case_ast_node(f, Field, node);
- if (f->flags&FieldFlag_using) {
- str = gb_string_appendc(str, "using ");
- }
- if (f->flags&FieldFlag_no_alias) {
- str = gb_string_appendc(str, "#no_alias ");
- }
- if (f->flags&FieldFlag_c_vararg) {
- str = gb_string_appendc(str, "#c_vararg ");
- }
- for_array(i, f->names) {
- Ast *name = f->names[i];
- if (i > 0) str = gb_string_appendc(str, ", ");
- str = write_expr_to_string(str, name);
- }
- if (f->names.count > 0) {
- if (f->type == nullptr && f->default_value != nullptr) {
- str = gb_string_append_rune(str, ' ');
- }
- str = gb_string_appendc(str, ":");
- }
- if (f->type != nullptr) {
- str = gb_string_append_rune(str, ' ');
- str = write_expr_to_string(str, f->type);
- }
- if (f->default_value != nullptr) {
- if (f->type != nullptr) {
- str = gb_string_append_rune(str, ' ');
- }
- str = gb_string_appendc(str, "= ");
- str = write_expr_to_string(str, f->default_value);
- }
- case_end;
- case_ast_node(f, FieldList, node);
- bool has_name = false;
- for_array(i, f->list) {
- ast_node(field, Field, f->list[i]);
- if (field->names.count > 1) {
- has_name = true;
- break;
- }
- if (field->names.count == 0) {
- continue;
- }
- if (!is_blank_ident(field->names[0])) {
- has_name = true;
- break;
- }
- }
- for_array(i, f->list) {
- if (i > 0) str = gb_string_appendc(str, ", ");
- if (has_name) {
- str = write_expr_to_string(str, f->list[i]);
- } else {
- ast_node(field, Field, f->list[i]);
- if (field->flags&FieldFlag_using) {
- str = gb_string_appendc(str, "using ");
- }
- if (field->flags&FieldFlag_no_alias) {
- str = gb_string_appendc(str, "#no_alias ");
- }
- if (field->flags&FieldFlag_c_vararg) {
- str = gb_string_appendc(str, "#c_vararg ");
- }
- str = write_expr_to_string(str, field->type);
- }
- }
- case_end;
- case_ast_node(f, UnionField, node);
- str = write_expr_to_string(str, f->name);
- str = gb_string_append_rune(str, '{');
- str = write_expr_to_string(str, f->list);
- str = gb_string_append_rune(str, '}');
- case_end;
- case_ast_node(ce, CallExpr, node);
- str = write_expr_to_string(str, ce->proc);
- str = gb_string_appendc(str, "(");
- for_array(i, ce->args) {
- Ast *arg = ce->args[i];
- if (i > 0) {
- str = gb_string_appendc(str, ", ");
- }
- str = write_expr_to_string(str, arg);
- }
- str = gb_string_appendc(str, ")");
- case_end;
- case_ast_node(tt, TypeType, node);
- str = gb_string_appendc(str, "type");
- if (tt->specialization) {
- str = gb_string_appendc(str, "/");
- str = write_expr_to_string(str, tt->specialization);
- }
- case_end;
- case_ast_node(pt, ProcType, node);
- str = gb_string_appendc(str, "proc(");
- str = write_expr_to_string(str, pt->params);
- str = gb_string_appendc(str, ")");
- if (pt->results != nullptr) {
- str = gb_string_appendc(str, " -> ");
- str = write_expr_to_string(str, pt->results);
- }
- case_end;
- case_ast_node(st, StructType, node);
- str = gb_string_appendc(str, "struct ");
- if (st->is_packed) str = gb_string_appendc(str, "#packed ");
- if (st->is_raw_union) str = gb_string_appendc(str, "#raw_union ");
- str = gb_string_append_rune(str, '{');
- str = write_struct_fields_to_string(str, st->fields);
- str = gb_string_append_rune(str, '}');
- case_end;
- case_ast_node(st, UnionType, node);
- str = gb_string_appendc(str, "union ");
- str = gb_string_append_rune(str, '{');
- str = write_struct_fields_to_string(str, st->variants);
- str = gb_string_append_rune(str, '}');
- case_end;
- case_ast_node(et, EnumType, node);
- str = gb_string_appendc(str, "enum ");
- if (et->base_type != nullptr) {
- str = write_expr_to_string(str, et->base_type);
- str = gb_string_append_rune(str, ' ');
- }
- str = gb_string_append_rune(str, '{');
- for_array(i, et->fields) {
- if (i > 0) {
- str = gb_string_appendc(str, ", ");
- }
- str = write_expr_to_string(str, et->fields[i]);
- }
- str = gb_string_append_rune(str, '}');
- case_end;
- }
- return str;
- }
- gbString expr_to_string(Ast *expression) {
- return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression);
- }
|