check_expr.cpp 186 KB

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