check_expr.cpp 307 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813
  1. enum CallArgumentError {
  2. CallArgumentError_None,
  3. CallArgumentError_NoneProcedureType,
  4. CallArgumentError_WrongTypes,
  5. CallArgumentError_NonVariadicExpand,
  6. CallArgumentError_VariadicTuple,
  7. CallArgumentError_MultipleVariadicExpand,
  8. CallArgumentError_AmbiguousPolymorphicVariadic,
  9. CallArgumentError_ArgumentCount,
  10. CallArgumentError_TooFewArguments,
  11. CallArgumentError_TooManyArguments,
  12. CallArgumentError_InvalidFieldValue,
  13. CallArgumentError_ParameterNotFound,
  14. CallArgumentError_ParameterMissing,
  15. CallArgumentError_DuplicateParameter,
  16. CallArgumentError_NoneConstantParameter,
  17. CallArgumentError_MAX,
  18. };
  19. char const *CallArgumentError_strings[CallArgumentError_MAX] = {
  20. "None",
  21. "NoneProcedureType",
  22. "WrongTypes",
  23. "NonVariadicExpand",
  24. "VariadicTuple",
  25. "MultipleVariadicExpand",
  26. "AmbiguousPolymorphicVariadic",
  27. "ArgumentCount",
  28. "TooFewArguments",
  29. "TooManyArguments",
  30. "InvalidFieldValue",
  31. "ParameterNotFound",
  32. "ParameterMissing",
  33. "DuplicateParameter",
  34. "NoneConstantParameter",
  35. };
  36. enum CallArgumentErrorMode {
  37. CallArgumentMode_NoErrors,
  38. CallArgumentMode_ShowErrors,
  39. };
  40. struct CallArgumentData {
  41. Entity *gen_entity;
  42. i64 score;
  43. Type * result_type;
  44. };
  45. struct PolyProcData {
  46. Entity * gen_entity;
  47. ProcInfo proc_info;
  48. };
  49. struct ValidIndexAndScore {
  50. isize index;
  51. i64 score;
  52. };
  53. int valid_index_and_score_cmp(void const *a, void const *b) {
  54. i64 si = (cast(ValidIndexAndScore const *)a)->score;
  55. i64 sj = (cast(ValidIndexAndScore const *)b)->score;
  56. return sj < si ? -1 : sj > si;
  57. }
  58. #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)
  59. typedef CALL_ARGUMENT_CHECKER(CallArgumentCheckerType);
  60. void check_expr (CheckerContext *c, Operand *operand, Ast *expression);
  61. void check_multi_expr (CheckerContext *c, Operand *operand, Ast *expression);
  62. void check_multi_expr_or_type (CheckerContext *c, Operand *operand, Ast *expression);
  63. void check_expr_or_type (CheckerContext *c, Operand *operand, Ast *expression, Type *type_hint);
  64. ExprKind check_expr_base (CheckerContext *c, Operand *operand, Ast *expression, Type *type_hint);
  65. void check_expr_with_type_hint (CheckerContext *c, Operand *o, Ast *e, Type *t);
  66. Type * check_type (CheckerContext *c, Ast *expression);
  67. Type * check_type_expr (CheckerContext *c, Ast *expression, Type *named_type);
  68. Type * make_optional_ok_type (Type *value, bool typed=true);
  69. void check_type_decl (CheckerContext *c, Entity *e, Ast *type_expr, Type *def);
  70. Entity * check_selector (CheckerContext *c, Operand *operand, Ast *node, Type *type_hint);
  71. Entity * check_ident (CheckerContext *c, Operand *o, Ast *n, Type *named_type, Type *type_hint, bool allow_import_name);
  72. Entity * find_polymorphic_record_entity (CheckerContext *c, Type *original_type, isize param_count, Array<Operand> const &ordered_operands, bool *failure);
  73. void check_not_tuple (CheckerContext *c, Operand *operand);
  74. void convert_to_typed (CheckerContext *c, Operand *operand, Type *target_type);
  75. gbString expr_to_string (Ast *expression);
  76. void check_entity_decl (CheckerContext *c, Entity *e, DeclInfo *decl, Type *named_type);
  77. void check_const_decl (CheckerContext *c, Entity *e, Ast *type_expr, Ast *init_expr, Type *named_type);
  78. void check_proc_body (CheckerContext *c, Token token, DeclInfo *decl, Type *type, Ast *body);
  79. void update_expr_type (CheckerContext *c, Ast *e, Type *type, bool final);
  80. bool check_is_terminating (Ast *node, String const &label);
  81. bool check_has_break (Ast *stmt, String const &label, bool implicit);
  82. void check_stmt (CheckerContext *c, Ast *node, u32 flags);
  83. void check_stmt_list (CheckerContext *c, Slice<Ast *> const &stmts, u32 flags);
  84. void check_init_constant (CheckerContext *c, Entity *e, Operand *operand);
  85. bool check_representable_as_constant(CheckerContext *c, ExactValue in_value, Type *type, ExactValue *out_value);
  86. bool check_procedure_type (CheckerContext *c, Type *type, Ast *proc_type_node, Array<Operand> *operands = nullptr);
  87. void check_struct_type (CheckerContext *c, Type *struct_type, Ast *node, Array<Operand> *poly_operands,
  88. Type *named_type = nullptr, Type *original_type_for_poly = nullptr);
  89. void check_union_type (CheckerContext *c, Type *union_type, Ast *node, Array<Operand> *poly_operands,
  90. Type *named_type = nullptr, Type *original_type_for_poly = nullptr);
  91. CallArgumentData check_call_arguments (CheckerContext *c, Operand *operand, Type *proc_type, Ast *call);
  92. Type * check_init_variable (CheckerContext *c, Entity *e, Operand *operand, String context_name);
  93. Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type, ProcCallingConvention cc);
  94. Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type, ProcCallingConvention cc);
  95. bool abi_compat_return_by_pointer(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type);
  96. void set_procedure_abi_types(Type *type);
  97. void check_assignment_error_suggestion(CheckerContext *c, Operand *o, Type *type);
  98. void add_map_key_type_dependencies(CheckerContext *ctx, Type *key);
  99. Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem);
  100. Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem);
  101. Entity *entity_from_expr(Ast *expr) {
  102. expr = unparen_expr(expr);
  103. switch (expr->kind) {
  104. case Ast_Ident:
  105. return expr->Ident.entity;
  106. case Ast_SelectorExpr:
  107. return entity_from_expr(expr->SelectorExpr.selector);
  108. }
  109. return nullptr;
  110. }
  111. void error_operand_not_expression(Operand *o) {
  112. if (o->mode == Addressing_Type) {
  113. gbString err = expr_to_string(o->expr);
  114. error(o->expr, "'%s' is not an expression but a type", err);
  115. gb_string_free(err);
  116. o->mode = Addressing_Invalid;
  117. }
  118. }
  119. void error_operand_no_value(Operand *o) {
  120. if (o->mode == Addressing_NoValue) {
  121. gbString err = expr_to_string(o->expr);
  122. Ast *x = unparen_expr(o->expr);
  123. if (x->kind == Ast_CallExpr) {
  124. error(o->expr, "'%s' call does not return a value and cannot be used as a value", err);
  125. } else {
  126. error(o->expr, "'%s' used as a value", err);
  127. }
  128. gb_string_free(err);
  129. o->mode = Addressing_Invalid;
  130. }
  131. }
  132. void check_scope_decls(CheckerContext *c, Slice<Ast *> const &nodes, isize reserve_size) {
  133. Scope *s = c->scope;
  134. check_collect_entities(c, nodes);
  135. for_array(i, s->elements.entries) {
  136. Entity *e = s->elements.entries[i].value;
  137. switch (e->kind) {
  138. case Entity_Constant:
  139. case Entity_TypeName:
  140. case Entity_Procedure:
  141. break;
  142. default:
  143. continue;
  144. }
  145. DeclInfo *d = decl_info_of_entity(e);
  146. if (d != nullptr) {
  147. check_entity_decl(c, e, d, nullptr);
  148. }
  149. }
  150. }
  151. isize check_is_assignable_to_using_subtype(Type *src, Type *dst, isize level = 0, bool src_is_ptr = false) {
  152. Type *prev_src = src;
  153. src = type_deref(src);
  154. if (!src_is_ptr) {
  155. src_is_ptr = src != prev_src;
  156. }
  157. src = base_type(src);
  158. if (!is_type_struct(src)) {
  159. return 0;
  160. }
  161. for_array(i, src->Struct.fields) {
  162. Entity *f = src->Struct.fields[i];
  163. if (f->kind != Entity_Variable || (f->flags&EntityFlag_Using) == 0) {
  164. continue;
  165. }
  166. if (are_types_identical(f->type, dst)) {
  167. return level+1;
  168. }
  169. if (src_is_ptr && is_type_pointer(dst)) {
  170. if (are_types_identical(f->type, type_deref(dst))) {
  171. return level+1;
  172. }
  173. }
  174. isize nested_level = check_is_assignable_to_using_subtype(f->type, dst, level+1, src_is_ptr);
  175. if (nested_level > 0) {
  176. return nested_level;
  177. }
  178. }
  179. return 0;
  180. }
  181. bool find_or_generate_polymorphic_procedure(CheckerContext *c, Entity *base_entity, Type *type,
  182. Array<Operand> *param_operands, Ast *poly_def_node, PolyProcData *poly_proc_data) {
  183. ///////////////////////////////////////////////////////////////////////////////
  184. // //
  185. // TODO CLEANUP(bill): This procedure is very messy and hacky. Clean this!!! //
  186. // //
  187. ///////////////////////////////////////////////////////////////////////////////
  188. if (base_entity == nullptr) {
  189. return false;
  190. }
  191. if (!is_type_proc(base_entity->type)) {
  192. return false;
  193. }
  194. String name = base_entity->token.string;
  195. Type *src = base_type(base_entity->type);
  196. Type *dst = nullptr;
  197. if (type != nullptr) dst = base_type(type);
  198. if (param_operands == nullptr) {
  199. GB_ASSERT(dst != nullptr);
  200. }
  201. if (param_operands != nullptr) {
  202. GB_ASSERT(dst == nullptr);
  203. }
  204. if (!src->Proc.is_polymorphic || src->Proc.is_poly_specialized) {
  205. return false;
  206. }
  207. if (dst != nullptr) {
  208. if (dst->Proc.is_polymorphic) {
  209. return false;
  210. }
  211. if (dst->Proc.param_count != src->Proc.param_count ||
  212. dst->Proc.result_count != src->Proc.result_count) {
  213. return false;
  214. }
  215. }
  216. DeclInfo *old_decl = decl_info_of_entity(base_entity);
  217. if (old_decl == nullptr) {
  218. return false;
  219. }
  220. gbAllocator a = heap_allocator();
  221. Array<Operand> operands = {};
  222. if (param_operands) {
  223. operands = *param_operands;
  224. } else {
  225. operands = array_make<Operand>(a, 0, dst->Proc.param_count);
  226. for (isize i = 0; i < dst->Proc.param_count; i++) {
  227. Entity *param = dst->Proc.params->Tuple.variables[i];
  228. Operand o = {Addressing_Value};
  229. o.type = param->type;
  230. array_add(&operands, o);
  231. }
  232. }
  233. defer (if (param_operands == nullptr) {
  234. array_free(&operands);
  235. });
  236. CheckerContext nctx = *c;
  237. Scope *scope = create_scope(base_entity->scope);
  238. scope->flags |= ScopeFlag_Proc;
  239. nctx.scope = scope;
  240. nctx.allow_polymorphic_types = true;
  241. if (nctx.polymorphic_scope == nullptr) {
  242. nctx.polymorphic_scope = scope;
  243. }
  244. if (param_operands == nullptr) {
  245. // c->no_polymorphic_errors = false;
  246. }
  247. auto *pt = &src->Proc;
  248. // NOTE(bill): This is slightly memory leaking if the type already exists
  249. // Maybe it's better to check with the previous types first?
  250. Type *final_proc_type = alloc_type_proc(scope, nullptr, 0, nullptr, 0, false, pt->calling_convention);
  251. bool success = check_procedure_type(&nctx, final_proc_type, pt->node, &operands);
  252. if (!success) {
  253. return false;
  254. }
  255. auto *found_gen_procs = map_get(&nctx.info->gen_procs, hash_pointer(base_entity->identifier));
  256. if (found_gen_procs) {
  257. auto procs = *found_gen_procs;
  258. for_array(i, procs) {
  259. Entity *other = procs[i];
  260. Type *pt = base_type(other->type);
  261. if (are_types_identical(pt, final_proc_type)) {
  262. if (poly_proc_data) {
  263. poly_proc_data->gen_entity = other;
  264. }
  265. return true;
  266. }
  267. }
  268. }
  269. #if 0
  270. bool generate_type_again = nctx.no_polymorphic_errors;
  271. if (generate_type_again) {
  272. #else
  273. {
  274. #endif
  275. // LEAK TODO(bill): This is technically a memory leak as it has to generate the type twice
  276. bool prev_no_polymorphic_errors = nctx.no_polymorphic_errors;
  277. defer (nctx.no_polymorphic_errors = prev_no_polymorphic_errors);
  278. nctx.no_polymorphic_errors = false;
  279. // NOTE(bill): Reset scope from the failed procedure type
  280. scope_reset(scope);
  281. // LEAK TODO(bill): Cloning this AST may be leaky
  282. Ast *cloned_proc_type_node = clone_ast(pt->node);
  283. success = check_procedure_type(&nctx, final_proc_type, cloned_proc_type_node, &operands);
  284. if (!success) {
  285. return false;
  286. }
  287. if (found_gen_procs) {
  288. auto procs = *found_gen_procs;
  289. for_array(i, procs) {
  290. Entity *other = procs[i];
  291. Type *pt = base_type(other->type);
  292. if (are_types_identical(pt, final_proc_type)) {
  293. if (poly_proc_data) {
  294. poly_proc_data->gen_entity = other;
  295. }
  296. return true;
  297. }
  298. }
  299. }
  300. }
  301. Ast *proc_lit = clone_ast(old_decl->proc_lit);
  302. ast_node(pl, ProcLit, proc_lit);
  303. // NOTE(bill): Associate the scope declared above withinth this procedure declaration's type
  304. add_scope(&nctx, pl->type, final_proc_type->Proc.scope);
  305. final_proc_type->Proc.is_poly_specialized = true;
  306. final_proc_type->Proc.is_polymorphic = true;
  307. for (isize i = 0; i < operands.count; i++) {
  308. Operand o = operands[i];
  309. if (final_proc_type == o.type ||
  310. base_entity->type == o.type) {
  311. // NOTE(bill): Cycle
  312. final_proc_type->Proc.is_poly_specialized = false;
  313. break;
  314. }
  315. }
  316. u64 tags = base_entity->Procedure.tags;
  317. Ast *ident = clone_ast(base_entity->identifier);
  318. Token token = ident->Ident.token;
  319. DeclInfo *d = make_decl_info(scope, old_decl->parent);
  320. d->gen_proc_type = final_proc_type;
  321. d->type_expr = pl->type;
  322. d->proc_lit = proc_lit;
  323. Entity *entity = alloc_entity_procedure(nullptr, token, final_proc_type, tags);
  324. entity->identifier = ident;
  325. add_entity_and_decl_info(&nctx, ident, entity, d);
  326. // NOTE(bill): Set the scope afterwards as this is not real overloading
  327. entity->scope = scope->parent;
  328. entity->file = base_entity->file;
  329. entity->pkg = base_entity->pkg;
  330. AstFile *file = nullptr;
  331. {
  332. Scope *s = entity->scope;
  333. while (s != nullptr && s->file == nullptr) {
  334. file = s->file;
  335. s = s->parent;
  336. }
  337. }
  338. ProcInfo proc_info = {};
  339. proc_info.file = file;
  340. proc_info.token = token;
  341. proc_info.decl = d;
  342. proc_info.type = final_proc_type;
  343. proc_info.body = pl->body;
  344. proc_info.tags = tags;
  345. proc_info.generated_from_polymorphic = true;
  346. proc_info.poly_def_node = poly_def_node;
  347. if (found_gen_procs) {
  348. array_add(found_gen_procs, entity);
  349. } else {
  350. auto array = array_make<Entity *>(heap_allocator());
  351. array_add(&array, entity);
  352. map_set(&nctx.checker->info.gen_procs, hash_pointer(base_entity->identifier), array);
  353. }
  354. GB_ASSERT(entity != nullptr);
  355. if (poly_proc_data) {
  356. poly_proc_data->gen_entity = entity;
  357. poly_proc_data->proc_info = proc_info;
  358. }
  359. // NOTE(bill): Check the newly generated procedure body
  360. check_procedure_later(nctx.checker, proc_info);
  361. return true;
  362. }
  363. bool check_polymorphic_procedure_assignment(CheckerContext *c, Operand *operand, Type *type, Ast *poly_def_node, PolyProcData *poly_proc_data) {
  364. if (operand->expr == nullptr) return false;
  365. Entity *base_entity = entity_of_node(operand->expr);
  366. if (base_entity == nullptr) return false;
  367. return find_or_generate_polymorphic_procedure(c, base_entity, type, nullptr, poly_def_node, poly_proc_data);
  368. }
  369. bool find_or_generate_polymorphic_procedure_from_parameters(CheckerContext *c, Entity *base_entity, Array<Operand> *operands, Ast *poly_def_node, PolyProcData *poly_proc_data) {
  370. return find_or_generate_polymorphic_procedure(c, base_entity, nullptr, operands, poly_def_node, poly_proc_data);
  371. }
  372. bool check_type_specialization_to(CheckerContext *c, Type *specialization, Type *type, bool compound, bool modify_type);
  373. bool is_polymorphic_type_assignable(CheckerContext *c, Type *poly, Type *source, bool compound, bool modify_type);
  374. bool check_cast_internal(CheckerContext *c, Operand *x, Type *type);
  375. #define MAXIMUM_TYPE_DISTANCE 10
  376. i64 check_distance_between_types(CheckerContext *c, Operand *operand, Type *type) {
  377. if (operand->mode == Addressing_Invalid ||
  378. type == t_invalid) {
  379. return -1;
  380. }
  381. if (operand->mode == Addressing_Builtin) {
  382. return -1;
  383. }
  384. if (operand->mode == Addressing_Type) {
  385. if (is_type_typeid(type)) {
  386. add_type_info_type(c, operand->type);
  387. return 4;
  388. }
  389. return -1;
  390. }
  391. Type *s = operand->type;
  392. if (are_types_identical(s, type)) {
  393. return 0;
  394. }
  395. Type *src = base_type(s);
  396. Type *dst = base_type(type);
  397. if (is_type_untyped_undef(src)) {
  398. if (type_has_undef(dst)) {
  399. return 1;
  400. }
  401. return -1;
  402. }
  403. if (is_type_untyped_nil(src)) {
  404. if (type_has_nil(dst)) {
  405. return 1;
  406. }
  407. return -1;
  408. }
  409. if (is_type_untyped(src)) {
  410. if (is_type_any(dst)) {
  411. // NOTE(bill): Anything can cast to 'Any'
  412. add_type_info_type(c, s);
  413. return MAXIMUM_TYPE_DISTANCE;
  414. }
  415. if (dst->kind == Type_Basic) {
  416. if (operand->mode == Addressing_Constant) {
  417. if (check_representable_as_constant(c, operand->value, dst, nullptr)) {
  418. if (is_type_typed(dst) && src->kind == Type_Basic) {
  419. switch (src->Basic.kind) {
  420. case Basic_UntypedRune:
  421. if (is_type_integer(dst) || is_type_rune(dst)) {
  422. return 1;
  423. }
  424. break;
  425. case Basic_UntypedInteger:
  426. if (is_type_integer(dst) || is_type_rune(dst)) {
  427. return 1;
  428. }
  429. break;
  430. case Basic_UntypedFloat:
  431. if (is_type_float(dst)) {
  432. return 1;
  433. }
  434. break;
  435. case Basic_UntypedComplex:
  436. if (is_type_complex(dst)) {
  437. return 1;
  438. }
  439. if (is_type_quaternion(dst)) {
  440. return 2;
  441. }
  442. break;
  443. case Basic_UntypedQuaternion:
  444. if (is_type_quaternion(dst)) {
  445. return 1;
  446. }
  447. break;
  448. }
  449. }
  450. return 2;
  451. }
  452. return -1;
  453. }
  454. if (src->kind == Type_Basic && src->Basic.kind == Basic_UntypedRune) {
  455. if (is_type_integer(dst) || is_type_rune(dst)) {
  456. if (is_type_typed(type)) {
  457. return 2;
  458. }
  459. return 1;
  460. }
  461. return -1;
  462. }
  463. if (src->kind == Type_Basic && src->Basic.kind == Basic_UntypedBool) {
  464. if (is_type_boolean(dst)) {
  465. if (is_type_typed(type)) {
  466. return 2;
  467. }
  468. return 1;
  469. }
  470. return -1;
  471. }
  472. }
  473. }
  474. if (is_type_enum(dst) && are_types_identical(dst->Enum.base_type, operand->type)) {
  475. if (c->in_enum_type) {
  476. return 3;
  477. }
  478. }
  479. {
  480. isize subtype_level = check_is_assignable_to_using_subtype(operand->type, type);
  481. if (subtype_level > 0) {
  482. return 4 + subtype_level;
  483. }
  484. }
  485. // ^T <- rawptr
  486. #if 0
  487. // TODO(bill): Should C-style (not C++) pointer cast be allowed?
  488. if (is_type_pointer(dst) && is_type_rawptr(src)) {
  489. return true;
  490. }
  491. #endif
  492. #if 1
  493. // TODO(bill): Should I allow this implicit conversion at all?!
  494. // rawptr <- ^T
  495. if (are_types_identical(type, t_rawptr) && is_type_pointer(src)) {
  496. return 5;
  497. }
  498. #endif
  499. if (is_type_polymorphic(dst) && !is_type_polymorphic(src)) {
  500. bool modify_type = !c->no_polymorphic_errors;
  501. if (is_polymorphic_type_assignable(c, type, s, false, modify_type)) {
  502. return 2;
  503. }
  504. }
  505. if (is_type_union(dst)) {
  506. for_array(i, dst->Union.variants) {
  507. Type *vt = dst->Union.variants[i];
  508. if (are_types_identical(vt, s)) {
  509. return 1;
  510. }
  511. }
  512. }
  513. if (is_type_relative_pointer(dst)) {
  514. i64 score = check_distance_between_types(c, operand, dst->RelativePointer.pointer_type);
  515. if (score >= 0) {
  516. return score+2;
  517. }
  518. }
  519. if (is_type_relative_slice(dst)) {
  520. i64 score = check_distance_between_types(c, operand, dst->RelativeSlice.slice_type);
  521. if (score >= 0) {
  522. return score+2;
  523. }
  524. }
  525. if (is_type_proc(dst)) {
  526. if (are_types_identical(src, dst)) {
  527. return 3;
  528. }
  529. PolyProcData poly_proc_data = {};
  530. if (check_polymorphic_procedure_assignment(c, operand, type, operand->expr, &poly_proc_data)) {
  531. add_entity_use(c, operand->expr, poly_proc_data.gen_entity);
  532. return 4;
  533. }
  534. }
  535. if (is_type_array(dst)) {
  536. Type *elem = base_array_type(dst);
  537. i64 distance = check_distance_between_types(c, operand, elem);
  538. if (distance >= 0) {
  539. return distance + 6;
  540. }
  541. }
  542. if (is_type_any(dst)) {
  543. if (!is_type_polymorphic(src)) {
  544. if (operand->mode == Addressing_Context && operand->type == t_context) {
  545. return -1;
  546. } else {
  547. // NOTE(bill): Anything can cast to 'Any'
  548. add_type_info_type(c, s);
  549. return MAXIMUM_TYPE_DISTANCE;
  550. }
  551. }
  552. }
  553. Ast *expr = unparen_expr(operand->expr);
  554. if (expr != nullptr && expr->kind == Ast_AutoCast) {
  555. Operand x = *operand;
  556. x.expr = expr->AutoCast.expr;
  557. bool ok = check_cast_internal(c, &x, type);
  558. if (ok) {
  559. return MAXIMUM_TYPE_DISTANCE;
  560. }
  561. }
  562. return -1;
  563. }
  564. i64 assign_score_function(i64 distance, bool is_variadic=false) {
  565. // 3*x^2 + 1 > x^2 + x + 1 (for positive x)
  566. i64 const c = 3*MAXIMUM_TYPE_DISTANCE*MAXIMUM_TYPE_DISTANCE + 1;
  567. // TODO(bill): A decent score function
  568. i64 d = distance*distance; // x^2
  569. if (is_variadic && d >= 0) {
  570. d += distance + 1; // x^2 + x + 1
  571. }
  572. return gb_max(c - d, 0);
  573. }
  574. bool check_is_assignable_to_with_score(CheckerContext *c, Operand *operand, Type *type, i64 *score_, bool is_variadic=false) {
  575. i64 score = 0;
  576. i64 distance = check_distance_between_types(c, operand, type);
  577. bool ok = distance >= 0;
  578. if (ok) {
  579. score = assign_score_function(distance, is_variadic);
  580. }
  581. if (score_) *score_ = score;
  582. return ok;
  583. }
  584. bool check_is_assignable_to(CheckerContext *c, Operand *operand, Type *type) {
  585. i64 score = 0;
  586. return check_is_assignable_to_with_score(c, operand, type, &score);
  587. }
  588. // NOTE(bill): 'content_name' is for debugging and error messages
  589. void check_assignment(CheckerContext *c, Operand *operand, Type *type, String context_name) {
  590. check_not_tuple(c, operand);
  591. if (operand->mode == Addressing_Invalid) {
  592. return;
  593. }
  594. if (is_type_untyped(operand->type)) {
  595. Type *target_type = type;
  596. if (type == nullptr || is_type_any(type)) {
  597. if (type == nullptr && is_type_untyped_nil(operand->type)) {
  598. error(operand->expr, "Use of untyped nil in %.*s", LIT(context_name));
  599. operand->mode = Addressing_Invalid;
  600. return;
  601. }
  602. if (type == nullptr && is_type_untyped_undef(operand->type)) {
  603. error(operand->expr, "Use of --- in %.*s", LIT(context_name));
  604. operand->mode = Addressing_Invalid;
  605. return;
  606. }
  607. target_type = default_type(operand->type);
  608. if (type != nullptr && !is_type_any(type)) {
  609. GB_ASSERT_MSG(is_type_typed(target_type), "%s", type_to_string(type));
  610. }
  611. add_type_info_type(c, type);
  612. add_type_info_type(c, target_type);
  613. }
  614. convert_to_typed(c, operand, target_type);
  615. if (operand->mode == Addressing_Invalid) {
  616. return;
  617. }
  618. }
  619. if (type == nullptr) {
  620. return;
  621. }
  622. if (operand->mode == Addressing_ProcGroup) {
  623. Array<Entity *> procs = proc_group_entities(c, *operand);
  624. bool good = false;
  625. // NOTE(bill): These should be done
  626. for_array(i, procs) {
  627. Type *t = base_type(procs[i]->type);
  628. if (t == t_invalid) {
  629. continue;
  630. }
  631. Operand x = {};
  632. x.mode = Addressing_Value;
  633. x.type = t;
  634. if (check_is_assignable_to(c, &x, type)) {
  635. Entity *e = procs[i];
  636. add_entity_use(c, operand->expr, e);
  637. good = true;
  638. break;
  639. }
  640. }
  641. if (!good) {
  642. gbString expr_str = expr_to_string(operand->expr);
  643. gbString op_type_str = type_to_string(operand->type);
  644. gbString type_str = type_to_string(type);
  645. defer (gb_string_free(type_str));
  646. defer (gb_string_free(op_type_str));
  647. defer (gb_string_free(expr_str));
  648. // TODO(bill): is this a good enough error message?
  649. error(operand->expr,
  650. "Cannot assign overloaded procedure '%s' to '%s' in %.*s",
  651. expr_str,
  652. op_type_str,
  653. LIT(context_name));
  654. operand->mode = Addressing_Invalid;
  655. }
  656. return;
  657. }
  658. if (check_is_assignable_to(c, operand, type)) {
  659. if (operand->mode == Addressing_Type && is_type_typeid(type)) {
  660. add_type_info_type(c, operand->type);
  661. add_type_and_value(c->info, operand->expr, Addressing_Value, type, exact_value_typeid(operand->type));
  662. }
  663. } else {
  664. gbString expr_str = expr_to_string(operand->expr);
  665. gbString op_type_str = type_to_string(operand->type);
  666. gbString type_str = type_to_string(type);
  667. defer (gb_string_free(type_str));
  668. defer (gb_string_free(op_type_str));
  669. defer (gb_string_free(expr_str));
  670. switch (operand->mode) {
  671. case Addressing_Builtin:
  672. // TODO(bill): Actually allow built in procedures to be passed around and thus be created on use
  673. error(operand->expr,
  674. "Cannot assign built-in procedure '%s' in %.*s",
  675. expr_str,
  676. LIT(context_name));
  677. break;
  678. case Addressing_Type:
  679. error(operand->expr,
  680. "Cannot assign '%s' which is a type in %.*s",
  681. op_type_str,
  682. LIT(context_name));
  683. break;
  684. default:
  685. // TODO(bill): is this a good enough error message?
  686. error(operand->expr,
  687. "Cannot assign value '%s' of type '%s' to '%s' in %.*s",
  688. expr_str,
  689. op_type_str,
  690. type_str,
  691. LIT(context_name));
  692. check_assignment_error_suggestion(c, operand, type);
  693. break;
  694. }
  695. operand->mode = Addressing_Invalid;
  696. return;
  697. }
  698. }
  699. bool is_polymorphic_type_assignable(CheckerContext *c, Type *poly, Type *source, bool compound, bool modify_type) {
  700. Operand o = {Addressing_Value};
  701. o.type = source;
  702. switch (poly->kind) {
  703. case Type_Basic:
  704. if (compound) return are_types_identical(poly, source);
  705. return check_is_assignable_to(c, &o, poly);
  706. case Type_Named: {
  707. if (check_type_specialization_to(c, poly, source, compound, modify_type)) {
  708. return true;
  709. }
  710. if (compound || !is_type_generic(poly)) {
  711. return are_types_identical(poly, source);
  712. }
  713. return check_is_assignable_to(c, &o, poly);
  714. }
  715. case Type_Generic: {
  716. if (poly->Generic.specialized != nullptr) {
  717. Type *s = poly->Generic.specialized;
  718. if (!check_type_specialization_to(c, s, source, compound, modify_type)) {
  719. return false;
  720. }
  721. }
  722. if (modify_type) {
  723. Type *ds = default_type(source); // IMPORTANT TODO(bill): IS THIS CORRECT?
  724. gb_memmove(poly, ds, gb_size_of(Type));
  725. }
  726. return true;
  727. }
  728. case Type_Pointer:
  729. if (source->kind == Type_Pointer) {
  730. isize level = check_is_assignable_to_using_subtype(source->Pointer.elem, poly->Pointer.elem);
  731. if (level > 0) {
  732. return true;
  733. }
  734. return is_polymorphic_type_assignable(c, poly->Pointer.elem, source->Pointer.elem, true, modify_type);
  735. }
  736. return false;
  737. case Type_Array:
  738. if (source->kind == Type_Array) {
  739. // IMPORTANT TODO(bill): Which is correct?
  740. // if (poly->Array.generic_count != nullptr && modify_type) {
  741. if (poly->Array.generic_count != nullptr) {
  742. Type *gt = poly->Array.generic_count;
  743. GB_ASSERT(gt->kind == Type_Generic);
  744. Entity *e = scope_lookup(gt->Generic.scope, gt->Generic.name);
  745. GB_ASSERT(e != nullptr);
  746. if (e->kind == Entity_TypeName) {
  747. poly->Array.generic_count = nullptr;
  748. poly->Array.count = source->Array.count;
  749. e->kind = Entity_Constant;
  750. e->Constant.value = exact_value_i64(source->Array.count);
  751. e->type = t_untyped_integer;
  752. } else if (e->kind == Entity_Constant) {
  753. poly->Array.generic_count = nullptr;
  754. if (e->Constant.value.kind != ExactValue_Integer) {
  755. return false;
  756. }
  757. i64 count = big_int_to_i64(&e->Constant.value.value_integer);
  758. if (count != source->Array.count) {
  759. return false;
  760. }
  761. poly->Array.count = source->Array.count;
  762. } else {
  763. return false;
  764. }
  765. }
  766. if (poly->Array.count == source->Array.count) {
  767. return is_polymorphic_type_assignable(c, poly->Array.elem, source->Array.elem, true, modify_type);
  768. }
  769. }
  770. return false;
  771. case Type_DynamicArray:
  772. if (source->kind == Type_DynamicArray) {
  773. return is_polymorphic_type_assignable(c, poly->DynamicArray.elem, source->DynamicArray.elem, true, modify_type);
  774. }
  775. return false;
  776. case Type_Slice:
  777. if (source->kind == Type_Slice) {
  778. return is_polymorphic_type_assignable(c, poly->Slice.elem, source->Slice.elem, true, modify_type);
  779. }
  780. return false;
  781. case Type_Enum:
  782. return false;
  783. case Type_BitSet:
  784. if (source->kind == Type_BitSet) {
  785. if (!is_polymorphic_type_assignable(c, poly->BitSet.elem, source->BitSet.elem, true, modify_type)) {
  786. return false;
  787. }
  788. if (poly->BitSet.underlying == nullptr) {
  789. if (modify_type) {
  790. poly->BitSet.underlying = source->BitSet.underlying;
  791. }
  792. } else if (!is_polymorphic_type_assignable(c, poly->BitSet.underlying, source->BitSet.underlying, true, modify_type)) {
  793. return false;
  794. }
  795. return true;
  796. }
  797. return false;
  798. case Type_Union:
  799. if (source->kind == Type_Union) {
  800. TypeUnion *x = &poly->Union;
  801. TypeUnion *y = &source->Union;
  802. if (x->variants.count != y->variants.count) {
  803. return false;
  804. }
  805. for_array(i, x->variants) {
  806. Type *a = x->variants[i];
  807. Type *b = y->variants[i];
  808. bool ok = is_polymorphic_type_assignable(c, a, b, false, modify_type);
  809. if (!ok) return false;
  810. }
  811. return true;
  812. }
  813. return false;
  814. case Type_Struct:
  815. if (source->kind == Type_Struct) {
  816. if (poly->Struct.soa_kind == source->Struct.soa_kind &&
  817. poly->Struct.soa_kind != StructSoa_None) {
  818. bool ok = is_polymorphic_type_assignable(c, poly->Struct.soa_elem, source->Struct.soa_elem, true, modify_type);
  819. if (ok) switch (source->Struct.soa_kind) {
  820. case StructSoa_Fixed:
  821. default:
  822. GB_PANIC("Unhandled SOA Kind");
  823. break;
  824. case StructSoa_Slice:
  825. if (modify_type) {
  826. Type *type = make_soa_struct_slice(c, nullptr, poly->Struct.node, poly->Struct.soa_elem);
  827. gb_memmove(poly, type, gb_size_of(*type));
  828. }
  829. break;
  830. case StructSoa_Dynamic:
  831. if (modify_type) {
  832. Type *type = make_soa_struct_dynamic_array(c, nullptr, poly->Struct.node, poly->Struct.soa_elem);
  833. gb_memmove(poly, type, gb_size_of(*type));
  834. }
  835. break;
  836. }
  837. return ok;
  838. }
  839. // return check_is_assignable_to(c, &o, poly);
  840. }
  841. return false;
  842. case Type_Tuple:
  843. GB_PANIC("This should never happen");
  844. return false;
  845. case Type_Proc:
  846. if (source->kind == Type_Proc) {
  847. // return check_is_assignable_to(c, &o, poly);
  848. // TODO(bill): Polymorphic type assignment
  849. #if 1
  850. TypeProc *x = &poly->Proc;
  851. TypeProc *y = &source->Proc;
  852. if (x->calling_convention != y->calling_convention) {
  853. return false;
  854. }
  855. if (x->c_vararg != y->c_vararg) {
  856. return false;
  857. }
  858. if (x->variadic != y->variadic) {
  859. return false;
  860. }
  861. if (x->param_count != y->param_count) {
  862. return false;
  863. }
  864. if (x->result_count != y->result_count) {
  865. return false;
  866. }
  867. for (isize i = 0; i < x->param_count; i++) {
  868. Entity *a = x->params->Tuple.variables[i];
  869. Entity *b = y->params->Tuple.variables[i];
  870. bool ok = is_polymorphic_type_assignable(c, a->type, b->type, false, modify_type);
  871. if (!ok) return false;
  872. }
  873. for (isize i = 0; i < x->result_count; i++) {
  874. Entity *a = x->results->Tuple.variables[i];
  875. Entity *b = y->results->Tuple.variables[i];
  876. bool ok = is_polymorphic_type_assignable(c, a->type, b->type, false, modify_type);
  877. if (!ok) return false;
  878. }
  879. if (modify_type) {
  880. set_procedure_abi_types(source);
  881. }
  882. return true;
  883. #endif
  884. }
  885. return false;
  886. case Type_Map:
  887. if (source->kind == Type_Map) {
  888. bool key = is_polymorphic_type_assignable(c, poly->Map.key, source->Map.key, true, modify_type);
  889. bool value = is_polymorphic_type_assignable(c, poly->Map.value, source->Map.value, true, modify_type);
  890. return key || value;
  891. }
  892. return false;
  893. }
  894. return false;
  895. }
  896. bool check_cycle(CheckerContext *c, Entity *curr, bool report) {
  897. if (curr->state != EntityState_InProgress) {
  898. return false;
  899. }
  900. for_array(i, *c->type_path) {
  901. Entity *prev = (*c->type_path)[i];
  902. if (prev == curr) {
  903. if (report) {
  904. error(curr->token, "Illegal declaration cycle of `%.*s`", LIT(curr->token.string));
  905. for (isize j = i; j < c->type_path->count; j++) {
  906. Entity *curr = (*c->type_path)[j];
  907. error(curr->token, "\t%.*s refers to", LIT(curr->token.string));
  908. }
  909. error(curr->token, "\t%.*s", LIT(curr->token.string));
  910. }
  911. return true;
  912. }
  913. }
  914. return false;
  915. }
  916. Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *named_type, Type *type_hint, bool allow_import_name) {
  917. GB_ASSERT(n->kind == Ast_Ident);
  918. o->mode = Addressing_Invalid;
  919. o->expr = n;
  920. String name = n->Ident.token.string;
  921. Entity *e = scope_lookup(c->scope, name);
  922. if (e == nullptr) {
  923. if (is_blank_ident(name)) {
  924. error(n, "'_' cannot be used as a value type");
  925. } else {
  926. error(n, "Undeclared name: %.*s", LIT(name));
  927. }
  928. o->type = t_invalid;
  929. o->mode = Addressing_Invalid;
  930. if (named_type != nullptr) {
  931. set_base_type(named_type, t_invalid);
  932. }
  933. return nullptr;
  934. }
  935. if (e->parent_proc_decl != nullptr &&
  936. e->parent_proc_decl != c->curr_proc_decl) {
  937. if (e->kind == Entity_Variable) {
  938. if ((e->flags & EntityFlag_Static) == 0) {
  939. error(n, "Nested procedures do not capture its parent's variables: %.*s", LIT(name));
  940. return nullptr;
  941. }
  942. } else if (e->kind == Entity_Label) {
  943. error(n, "Nested procedures do not capture its parent's labels: %.*s", LIT(name));
  944. return nullptr;
  945. }
  946. }
  947. if (e->kind == Entity_ProcGroup) {
  948. auto *pge = &e->ProcGroup;
  949. DeclInfo *d = decl_info_of_entity(e);
  950. check_entity_decl(c, e, d, nullptr);
  951. Array<Entity *> procs = pge->entities;
  952. bool skip = false;
  953. if (type_hint != nullptr) {
  954. // NOTE(bill): These should be done
  955. for_array(i, procs) {
  956. Type *t = base_type(procs[i]->type);
  957. if (t == t_invalid) {
  958. continue;
  959. }
  960. Operand x = {};
  961. x.mode = Addressing_Value;
  962. x.type = t;
  963. if (check_is_assignable_to(c, &x, type_hint)) {
  964. e = procs[i];
  965. add_entity_use(c, n, e);
  966. skip = true;
  967. break;
  968. }
  969. }
  970. }
  971. if (!skip) {
  972. o->mode = Addressing_ProcGroup;
  973. o->type = t_invalid;
  974. o->proc_group = e;
  975. return nullptr;
  976. }
  977. }
  978. add_entity_use(c, n, e);
  979. if (e->state == EntityState_Unresolved) {
  980. check_entity_decl(c, e, nullptr, named_type);
  981. }
  982. if (e->type == nullptr) {
  983. // TODO(bill): Which is correct? return or compiler_error?
  984. // compiler_error("How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(name));
  985. return nullptr;
  986. }
  987. e->flags |= EntityFlag_Used;
  988. Type *type = e->type;
  989. o->type = type;
  990. switch (e->kind) {
  991. case Entity_Constant:
  992. if (type == t_invalid) {
  993. o->type = t_invalid;
  994. return e;
  995. }
  996. o->value = e->Constant.value;
  997. if (o->value.kind == ExactValue_Invalid) {
  998. return e;
  999. }
  1000. if (o->value.kind == ExactValue_Procedure) {
  1001. Entity *proc = strip_entity_wrapping(o->value.value_procedure);
  1002. if (proc != nullptr) {
  1003. o->mode = Addressing_Value;
  1004. o->type = proc->type;
  1005. return proc;
  1006. }
  1007. }
  1008. o->mode = Addressing_Constant;
  1009. break;
  1010. case Entity_Variable:
  1011. e->flags |= EntityFlag_Used;
  1012. if (type == t_invalid) {
  1013. o->type = t_invalid;
  1014. return e;
  1015. }
  1016. o->mode = Addressing_Variable;
  1017. if (e->flags & EntityFlag_Value) {
  1018. o->mode = Addressing_Value;
  1019. }
  1020. break;
  1021. case Entity_Procedure:
  1022. o->mode = Addressing_Value;
  1023. o->value = exact_value_procedure(n);
  1024. break;
  1025. case Entity_Builtin:
  1026. o->builtin_id = cast(BuiltinProcId)e->Builtin.id;
  1027. o->mode = Addressing_Builtin;
  1028. break;
  1029. case Entity_TypeName:
  1030. o->mode = Addressing_Type;
  1031. if (check_cycle(c, e, true)) {
  1032. type = t_invalid;
  1033. }
  1034. break;
  1035. case Entity_ImportName:
  1036. if (!allow_import_name) {
  1037. error(n, "Use of import '%.*s' not in selector", LIT(name));
  1038. }
  1039. return e;
  1040. case Entity_LibraryName:
  1041. error(n, "Use of library '%.*s' not in foreign block", LIT(name));
  1042. return e;
  1043. case Entity_Label:
  1044. o->mode = Addressing_NoValue;
  1045. break;
  1046. case Entity_Nil:
  1047. o->mode = Addressing_Value;
  1048. break;
  1049. default:
  1050. compiler_error("Unknown EntityKind %.*s", LIT(entity_strings[e->kind]));
  1051. break;
  1052. }
  1053. return e;
  1054. }
  1055. bool check_unary_op(CheckerContext *c, Operand *o, Token op) {
  1056. if (o->type == nullptr) {
  1057. gbString str = expr_to_string(o->expr);
  1058. error(o->expr, "Expression has no value '%s'", str);
  1059. gb_string_free(str);
  1060. return false;
  1061. }
  1062. // TODO(bill): Handle errors correctly
  1063. Type *type = base_type(core_array_type(o->type));
  1064. gbString str = nullptr;
  1065. switch (op.kind) {
  1066. case Token_Add:
  1067. case Token_Sub:
  1068. if (!is_type_numeric(type)) {
  1069. str = expr_to_string(o->expr);
  1070. error(op, "Operator '%.*s' is not allowed with '%s'", LIT(op.string), str);
  1071. gb_string_free(str);
  1072. }
  1073. break;
  1074. case Token_Xor:
  1075. if (!is_type_integer(type) && !is_type_boolean(type) && !is_type_bit_set(type)) {
  1076. error(op, "Operator '%.*s' is only allowed with integers, booleans, or bit sets", LIT(op.string));
  1077. }
  1078. break;
  1079. case Token_Not:
  1080. if (!is_type_boolean(type)) {
  1081. str = expr_to_string(o->expr);
  1082. error(op, "Operator '%.*s' is only allowed on boolean expression", LIT(op.string));
  1083. gb_string_free(str);
  1084. }
  1085. break;
  1086. default:
  1087. error(op, "Unknown operator '%.*s'", LIT(op.string));
  1088. return false;
  1089. }
  1090. return true;
  1091. }
  1092. bool check_binary_op(CheckerContext *c, Operand *o, Token op) {
  1093. // TODO(bill): Handle errors correctly
  1094. Type *type = base_type(core_array_type(o->type));
  1095. Type *ct = core_type(type);
  1096. switch (op.kind) {
  1097. case Token_Sub:
  1098. case Token_SubEq:
  1099. if (!is_type_numeric(type)) {
  1100. error(op, "Operator '%.*s' is only allowed with numeric expressions", LIT(op.string));
  1101. return false;
  1102. }
  1103. break;
  1104. case Token_Mul:
  1105. case Token_Quo:
  1106. case Token_MulEq:
  1107. case Token_QuoEq:
  1108. case Token_AddEq:
  1109. if (!is_type_numeric(type)) {
  1110. error(op, "Operator '%.*s' is only allowed with numeric expressions", LIT(op.string));
  1111. return false;
  1112. }
  1113. break;
  1114. case Token_Add:
  1115. if (is_type_string(type)) {
  1116. if (o->mode == Addressing_Constant) {
  1117. return true;
  1118. }
  1119. error(op, "String concatenation is only allowed with constant strings");
  1120. return false;
  1121. } else if (!is_type_numeric(type)) {
  1122. error(op, "Operator '%.*s' is only allowed with numeric expressions", LIT(op.string));
  1123. return false;
  1124. }
  1125. break;
  1126. case Token_And:
  1127. case Token_Or:
  1128. case Token_AndEq:
  1129. case Token_OrEq:
  1130. case Token_Xor:
  1131. case Token_XorEq:
  1132. if (!is_type_integer(ct) && !is_type_boolean(ct) && !is_type_bit_set(ct)) {
  1133. error(op, "Operator '%.*s' is only allowed with integers, booleans, or bit sets", LIT(op.string));
  1134. return false;
  1135. }
  1136. break;
  1137. case Token_Mod:
  1138. case Token_ModMod:
  1139. case Token_ModEq:
  1140. case Token_ModModEq:
  1141. if (!is_type_integer(type)) {
  1142. error(op, "Operator '%.*s' is only allowed with integers", LIT(op.string));
  1143. return false;
  1144. }
  1145. if (is_type_simd_vector(o->type)) {
  1146. switch (op.kind) {
  1147. case Token_ModMod:
  1148. case Token_ModModEq:
  1149. error(op, "Operator '%.*s' is only allowed with integers", LIT(op.string));
  1150. return false;
  1151. }
  1152. }
  1153. break;
  1154. case Token_AndNot:
  1155. case Token_AndNotEq:
  1156. if (!is_type_integer(ct) && !is_type_bit_set(ct)) {
  1157. error(op, "Operator '%.*s' is only allowed with integers and bit sets", LIT(op.string));
  1158. return false;
  1159. }
  1160. if (is_type_simd_vector(o->type)) {
  1161. switch (op.kind) {
  1162. case Token_AndNot:
  1163. case Token_AndNotEq:
  1164. error(op, "Operator '%.*s' is only allowed with integers", LIT(op.string));
  1165. return false;
  1166. }
  1167. }
  1168. break;
  1169. case Token_CmpAnd:
  1170. case Token_CmpOr:
  1171. case Token_CmpAndEq:
  1172. case Token_CmpOrEq:
  1173. if (!is_type_boolean(type)) {
  1174. error(op, "Operator '%.*s' is only allowed with boolean expressions", LIT(op.string));
  1175. return false;
  1176. }
  1177. break;
  1178. default:
  1179. error(op, "Unknown operator '%.*s'", LIT(op.string));
  1180. return false;
  1181. }
  1182. return true;
  1183. }
  1184. bool check_representable_as_constant(CheckerContext *c, ExactValue in_value, Type *type, ExactValue *out_value) {
  1185. if (in_value.kind == ExactValue_Invalid) {
  1186. // NOTE(bill): There's already been an error
  1187. return true;
  1188. }
  1189. type = core_type(type);
  1190. if (type == t_invalid) {
  1191. return false;
  1192. } else if (is_type_boolean(type)) {
  1193. return in_value.kind == ExactValue_Bool;
  1194. } else if (is_type_string(type)) {
  1195. return in_value.kind == ExactValue_String;
  1196. } else if (is_type_integer(type) || is_type_rune(type)) {
  1197. if (in_value.kind == ExactValue_Bool) {
  1198. return false;
  1199. }
  1200. ExactValue v = exact_value_to_integer(in_value);
  1201. if (v.kind != ExactValue_Integer) {
  1202. return false;
  1203. }
  1204. if (out_value) *out_value = v;
  1205. if (is_type_untyped(type)) {
  1206. return true;
  1207. }
  1208. BigInt i = v.value_integer;
  1209. i64 bit_size = type_size_of(type);
  1210. BigInt umax = {};
  1211. BigInt imin = {};
  1212. BigInt imax = {};
  1213. if (bit_size < 16) {
  1214. big_int_from_u64(&umax, unsigned_integer_maxs[bit_size]);
  1215. big_int_from_i64(&imin, signed_integer_mins[bit_size]);
  1216. big_int_from_i64(&imax, signed_integer_maxs[bit_size]);
  1217. } else {
  1218. big_int_from_u64(&umax, 1);
  1219. big_int_from_i64(&imin, 1);
  1220. big_int_from_i64(&imax, 1);
  1221. BigInt bi128 = {};
  1222. BigInt bi127 = {};
  1223. big_int_from_i64(&bi128, 128);
  1224. big_int_from_i64(&bi127, 127);
  1225. big_int_shl_eq(&umax, &bi128);
  1226. big_int_sub_eq(&umax, &BIG_INT_ONE);
  1227. big_int_shl_eq(&imin, &bi127);
  1228. big_int_neg(&imin, &imin);
  1229. big_int_shl_eq(&imax, &bi127);
  1230. big_int_sub_eq(&imax, &BIG_INT_ONE);
  1231. }
  1232. switch (type->Basic.kind) {
  1233. case Basic_rune:
  1234. case Basic_i8:
  1235. case Basic_i16:
  1236. case Basic_i32:
  1237. case Basic_i64:
  1238. case Basic_i128:
  1239. case Basic_int:
  1240. case Basic_i16le:
  1241. case Basic_i32le:
  1242. case Basic_i64le:
  1243. case Basic_i128le:
  1244. case Basic_i16be:
  1245. case Basic_i32be:
  1246. case Basic_i64be:
  1247. case Basic_i128be:
  1248. {
  1249. // return imin <= i && i <= imax;
  1250. int a = big_int_cmp(&imin, &i);
  1251. int b = big_int_cmp(&i, &imax);
  1252. return (a <= 0) && (b <= 0);
  1253. }
  1254. case Basic_u8:
  1255. case Basic_u16:
  1256. case Basic_u32:
  1257. case Basic_u64:
  1258. case Basic_u128:
  1259. case Basic_uint:
  1260. case Basic_uintptr:
  1261. case Basic_u16le:
  1262. case Basic_u32le:
  1263. case Basic_u64le:
  1264. case Basic_u128le:
  1265. case Basic_u16be:
  1266. case Basic_u32be:
  1267. case Basic_u64be:
  1268. case Basic_u128be:
  1269. {
  1270. // return 0ull <= i && i <= umax;
  1271. int b = big_int_cmp(&i, &umax);
  1272. return !i.neg && (b <= 0);
  1273. }
  1274. case Basic_UntypedInteger:
  1275. return true;
  1276. default: GB_PANIC("Compiler error: Unknown integer type!"); break;
  1277. }
  1278. } else if (is_type_float(type)) {
  1279. ExactValue v = exact_value_to_float(in_value);
  1280. if (v.kind != ExactValue_Float) {
  1281. return false;
  1282. }
  1283. if (out_value) *out_value = v;
  1284. switch (type->Basic.kind) {
  1285. // case Basic_f16:
  1286. case Basic_f32:
  1287. case Basic_f64:
  1288. return true;
  1289. case Basic_f32le:
  1290. case Basic_f64le:
  1291. case Basic_f32be:
  1292. case Basic_f64be:
  1293. return true;
  1294. case Basic_UntypedFloat:
  1295. return true;
  1296. default: GB_PANIC("Compiler error: Unknown float type!"); break;
  1297. }
  1298. } else if (is_type_complex(type)) {
  1299. ExactValue v = exact_value_to_complex(in_value);
  1300. if (v.kind != ExactValue_Complex) {
  1301. return false;
  1302. }
  1303. switch (type->Basic.kind) {
  1304. case Basic_complex64:
  1305. case Basic_complex128: {
  1306. ExactValue real = exact_value_real(v);
  1307. ExactValue imag = exact_value_imag(v);
  1308. if (real.kind != ExactValue_Invalid &&
  1309. imag.kind != ExactValue_Invalid) {
  1310. if (out_value) *out_value = exact_value_complex(exact_value_to_f64(real), exact_value_to_f64(imag));
  1311. return true;
  1312. }
  1313. break;
  1314. }
  1315. case Basic_UntypedComplex:
  1316. return true;
  1317. default: GB_PANIC("Compiler error: Unknown complex type!"); break;
  1318. }
  1319. return false;
  1320. } else if (is_type_quaternion(type)) {
  1321. ExactValue v = exact_value_to_quaternion(in_value);
  1322. if (v.kind != ExactValue_Quaternion) {
  1323. return false;
  1324. }
  1325. switch (type->Basic.kind) {
  1326. case Basic_quaternion128:
  1327. case Basic_quaternion256: {
  1328. ExactValue real = exact_value_real(v);
  1329. ExactValue imag = exact_value_imag(v);
  1330. ExactValue jmag = exact_value_jmag(v);
  1331. ExactValue kmag = exact_value_kmag(v);
  1332. if (real.kind != ExactValue_Invalid &&
  1333. imag.kind != ExactValue_Invalid) {
  1334. if (out_value) *out_value = exact_value_quaternion(exact_value_to_f64(real), exact_value_to_f64(imag), exact_value_to_f64(jmag), exact_value_to_f64(kmag));
  1335. return true;
  1336. }
  1337. break;
  1338. }
  1339. case Basic_UntypedComplex:
  1340. if (out_value) *out_value = exact_value_to_quaternion(*out_value);
  1341. return true;
  1342. case Basic_UntypedQuaternion:
  1343. return true;
  1344. default: GB_PANIC("Compiler error: Unknown complex type!"); break;
  1345. }
  1346. return false;
  1347. } else if (is_type_pointer(type)) {
  1348. if (in_value.kind == ExactValue_Pointer) {
  1349. return true;
  1350. }
  1351. if (in_value.kind == ExactValue_Integer) {
  1352. return false;
  1353. // return true;
  1354. }
  1355. if (in_value.kind == ExactValue_String) {
  1356. return false;
  1357. }
  1358. if (out_value) *out_value = in_value;
  1359. } else if (is_type_bit_set(type)) {
  1360. if (in_value.kind == ExactValue_Integer) {
  1361. return true;
  1362. }
  1363. }
  1364. return false;
  1365. }
  1366. void check_assignment_error_suggestion(CheckerContext *c, Operand *o, Type *type) {
  1367. gbString a = expr_to_string(o->expr);
  1368. gbString b = type_to_string(type);
  1369. defer(
  1370. gb_string_free(b);
  1371. gb_string_free(a);
  1372. );
  1373. Type *src = base_type(o->type);
  1374. Type *dst = base_type(type);
  1375. if (is_type_array(src) && is_type_slice(dst)) {
  1376. Type *s = src->Array.elem;
  1377. Type *d = dst->Slice.elem;
  1378. if (are_types_identical(s, d)) {
  1379. error_line("\tSuggestion: the array expression may be sliced with %s[:]\n", a);
  1380. }
  1381. } else if (are_types_identical(src, dst)) {
  1382. error_line("\tSuggestion: the expression may be directly casted to type %s\n", b);
  1383. } else if (are_types_identical(src, t_string) && is_type_u8_slice(dst)) {
  1384. error_line("\tSuggestion: a string may be transmuted to %s\n", b);
  1385. } else if (is_type_u8_slice(src) && are_types_identical(dst, t_string)) {
  1386. error_line("\tSuggestion: the expression may be casted to %s\n", b);
  1387. }
  1388. }
  1389. void check_cast_error_suggestion(CheckerContext *c, Operand *o, Type *type) {
  1390. gbString a = expr_to_string(o->expr);
  1391. gbString b = type_to_string(type);
  1392. defer(
  1393. gb_string_free(b);
  1394. gb_string_free(a);
  1395. );
  1396. Type *src = base_type(o->type);
  1397. Type *dst = base_type(type);
  1398. if (is_type_array(src) && is_type_slice(dst)) {
  1399. Type *s = src->Array.elem;
  1400. Type *d = dst->Slice.elem;
  1401. if (are_types_identical(s, d)) {
  1402. error_line("\tSuggestion: the array expression may be sliced with %s[:]\n", a);
  1403. }
  1404. } else if (is_type_pointer(o->type) && is_type_integer(type)) {
  1405. if (is_type_uintptr(type)) {
  1406. error_line("\tSuggestion: a pointer may be directly casted to %s\n", b);
  1407. } else {
  1408. error_line("\tSuggestion: for a pointer to be casted to an integer, it must be converted to 'uintptr' first\n");
  1409. i64 x = type_size_of(o->type);
  1410. i64 y = type_size_of(type);
  1411. if (x != y) {
  1412. error_line("\tNote: the type of expression and the type of the cast have a different size in bytes, %lld vs %lld\n", x, y);
  1413. }
  1414. }
  1415. } else if (is_type_integer(o->type) && is_type_pointer(type)) {
  1416. if (is_type_uintptr(o->type)) {
  1417. error_line("\tSuggestion: %a may be directly casted to %s\n", a, b);
  1418. } else {
  1419. error_line("\tSuggestion: for an integer to be casted to a pointer, it must be converted to 'uintptr' first\n");
  1420. }
  1421. } else if (are_types_identical(src, t_string) && is_type_u8_slice(dst)) {
  1422. error_line("\tSuggestion: a string may be transmuted to %s\n", b);
  1423. } else if (is_type_u8_slice(src) && are_types_identical(dst, t_string)) {
  1424. error_line("\tSuggestion: the expression may be casted to %s\n", b);
  1425. }
  1426. }
  1427. void check_is_expressible(CheckerContext *c, Operand *o, Type *type) {
  1428. GB_ASSERT(o->mode == Addressing_Constant);
  1429. if (!is_type_constant_type(type) || !check_representable_as_constant(c, o->value, type, &o->value)) {
  1430. gbString a = expr_to_string(o->expr);
  1431. gbString b = type_to_string(type);
  1432. defer(
  1433. gb_string_free(b);
  1434. gb_string_free(a);
  1435. o->mode = Addressing_Invalid;
  1436. );
  1437. if (is_type_numeric(o->type) && is_type_numeric(type)) {
  1438. if (!is_type_integer(o->type) && is_type_integer(type)) {
  1439. error(o->expr, "'%s' truncated to '%s'", a, b);
  1440. } else {
  1441. error(o->expr, "Cannot convert '%s' to '%s'", a, b);
  1442. check_assignment_error_suggestion(c, o, type);
  1443. }
  1444. } else {
  1445. error(o->expr, "Cannot convert '%s' to '%s'", a, b);
  1446. check_assignment_error_suggestion(c, o, type);
  1447. }
  1448. }
  1449. }
  1450. bool check_is_not_addressable(CheckerContext *c, Operand *o) {
  1451. if (o->mode == Addressing_OptionalOk) {
  1452. Ast *expr = unselector_expr(o->expr);
  1453. if (expr->kind != Ast_TypeAssertion) {
  1454. return true;
  1455. }
  1456. ast_node(ta, TypeAssertion, expr);
  1457. TypeAndValue tv = ta->expr->tav;
  1458. if (is_type_pointer(tv.type)) {
  1459. return false;
  1460. }
  1461. if (is_type_union(tv.type) && tv.mode == Addressing_Variable) {
  1462. return false;
  1463. }
  1464. if (is_type_any(tv.type)) {
  1465. return false;
  1466. }
  1467. return true;
  1468. }
  1469. if (o->mode == Addressing_MapIndex) {
  1470. return false;
  1471. }
  1472. Ast *expr = unparen_expr(o->expr);
  1473. if (expr->kind == Ast_CompoundLit) {
  1474. return false;
  1475. }
  1476. if (o->mode != Addressing_Variable) {
  1477. return true;
  1478. }
  1479. return false;
  1480. }
  1481. void check_unary_expr(CheckerContext *c, Operand *o, Token op, Ast *node) {
  1482. switch (op.kind) {
  1483. case Token_And: { // Pointer address
  1484. if (check_is_not_addressable(c, o)) {
  1485. if (ast_node_expect(node, Ast_UnaryExpr)) {
  1486. ast_node(ue, UnaryExpr, node);
  1487. gbString str = expr_to_string(ue->expr);
  1488. defer (gb_string_free(str));
  1489. Entity *e = entity_of_node(o->expr);
  1490. if (e != nullptr && (e->flags & EntityFlag_Param) != 0) {
  1491. error(op, "Cannot take the pointer address of '%s' which is a procedure parameter", str);
  1492. } else {
  1493. switch (o->mode) {
  1494. case Addressing_SoaVariable:
  1495. error(op, "Cannot take the pointer address of '%s' as it is an indirect index of an SOA struct", str);
  1496. break;
  1497. case Addressing_Constant:
  1498. error(op, "Cannot take the pointer address of '%s' which is a constant", str);
  1499. break;
  1500. default:
  1501. error(op, "Cannot take the pointer address of '%s'", str);
  1502. break;
  1503. }
  1504. }
  1505. }
  1506. o->mode = Addressing_Invalid;
  1507. return;
  1508. }
  1509. o->mode = Addressing_Value;
  1510. o->type = alloc_type_pointer(o->type);
  1511. return;
  1512. }
  1513. }
  1514. if (!check_unary_op(c, o, op)) {
  1515. o->mode = Addressing_Invalid;
  1516. return;
  1517. }
  1518. if (o->mode == Addressing_Constant) {
  1519. Type *type = base_type(o->type);
  1520. if (!is_type_constant_type(o->type)) {
  1521. gbString xt = type_to_string(o->type);
  1522. gbString err_str = expr_to_string(node);
  1523. error(op, "Invalid type, '%s', for constant unary expression '%s'", xt, err_str);
  1524. gb_string_free(err_str);
  1525. gb_string_free(xt);
  1526. o->mode = Addressing_Invalid;
  1527. return;
  1528. }
  1529. if (op.kind == Token_Xor && is_type_untyped(type)) {
  1530. gbString err_str = expr_to_string(node);
  1531. error(op, "Bitwise not cannot be applied to untyped constants '%s'", err_str);
  1532. gb_string_free(err_str);
  1533. o->mode = Addressing_Invalid;
  1534. return;
  1535. }
  1536. if (op.kind == Token_Sub && is_type_unsigned(type)) {
  1537. gbString err_str = expr_to_string(node);
  1538. error(op, "A unsigned constant cannot be negated '%s'", err_str);
  1539. gb_string_free(err_str);
  1540. o->mode = Addressing_Invalid;
  1541. return;
  1542. }
  1543. i32 precision = 0;
  1544. if (is_type_typed(type)) {
  1545. precision = cast(i32)(8 * type_size_of(type));
  1546. }
  1547. bool is_unsigned = is_type_unsigned(type);
  1548. if (is_type_rune(type)) {
  1549. GB_ASSERT(!is_unsigned);
  1550. }
  1551. o->value = exact_unary_operator_value(op.kind, o->value, precision, is_unsigned);
  1552. if (is_type_typed(type)) {
  1553. if (node != nullptr) {
  1554. o->expr = node;
  1555. }
  1556. check_is_expressible(c, o, type);
  1557. }
  1558. return;
  1559. }
  1560. o->mode = Addressing_Value;
  1561. }
  1562. void add_comparison_procedures_for_fields(CheckerContext *c, Type *t) {
  1563. if (t == nullptr) {
  1564. return;
  1565. }
  1566. t = base_type(t);
  1567. if (!is_type_comparable(t)) {
  1568. return;
  1569. }
  1570. switch (t->kind) {
  1571. case Type_Basic:
  1572. switch (t->Basic.kind) {
  1573. case Basic_complex64:
  1574. add_package_dependency(c, "runtime", "complex64_eq");
  1575. add_package_dependency(c, "runtime", "complex64_ne");
  1576. break;
  1577. case Basic_complex128:
  1578. add_package_dependency(c, "runtime", "complex128_eq");
  1579. add_package_dependency(c, "runtime", "complex128_ne");
  1580. break;
  1581. case Basic_quaternion128:
  1582. add_package_dependency(c, "runtime", "quaternion128_eq");
  1583. add_package_dependency(c, "runtime", "quaternion128_ne");
  1584. break;
  1585. case Basic_quaternion256:
  1586. add_package_dependency(c, "runtime", "quaternion256_eq");
  1587. add_package_dependency(c, "runtime", "quaternion256_ne");
  1588. break;
  1589. case Basic_cstring:
  1590. add_package_dependency(c, "runtime", "cstring_to_string");
  1591. /*fallthrough*/
  1592. case Basic_string:
  1593. add_package_dependency(c, "runtime", "string_eq");
  1594. add_package_dependency(c, "runtime", "string_ne");
  1595. break;
  1596. }
  1597. break;
  1598. case Type_Struct:
  1599. for_array(i, t->Struct.fields) {
  1600. add_comparison_procedures_for_fields(c, t->Struct.fields[i]->type);
  1601. }
  1602. break;
  1603. }
  1604. }
  1605. void check_comparison(CheckerContext *c, Operand *x, Operand *y, TokenKind op) {
  1606. if (x->mode == Addressing_Type && y->mode == Addressing_Type) {
  1607. bool comp = are_types_identical(x->type, y->type);
  1608. switch (op) {
  1609. case Token_CmpEq: /* comp = comp; */ break;
  1610. case Token_NotEq: comp = !comp; break;
  1611. }
  1612. x->mode = Addressing_Constant;
  1613. x->type = t_untyped_bool;
  1614. x->value = exact_value_bool(comp);
  1615. return;
  1616. }
  1617. if (x->mode == Addressing_Type && is_type_typeid(y->type)) {
  1618. add_type_info_type(c, x->type);
  1619. add_type_info_type(c, y->type);
  1620. add_type_and_value(c->info, x->expr, Addressing_Value, y->type, exact_value_typeid(x->type));
  1621. x->mode = Addressing_Value;
  1622. x->type = t_untyped_bool;
  1623. return;
  1624. } else if (is_type_typeid(x->type) && y->mode == Addressing_Type) {
  1625. add_type_info_type(c, x->type);
  1626. add_type_info_type(c, y->type);
  1627. add_type_and_value(c->info, y->expr, Addressing_Value, x->type, exact_value_typeid(y->type));
  1628. x->mode = Addressing_Value;
  1629. x->type = t_untyped_bool;
  1630. return;
  1631. }
  1632. gbString err_str = nullptr;
  1633. if (check_is_assignable_to(c, x, y->type) ||
  1634. check_is_assignable_to(c, y, x->type)) {
  1635. Type *err_type = x->type;
  1636. bool defined = false;
  1637. switch (op) {
  1638. case Token_CmpEq:
  1639. case Token_NotEq:
  1640. defined = (is_type_comparable(x->type) && is_type_comparable(y->type)) ||
  1641. (is_operand_nil(*x) && type_has_nil(y->type)) ||
  1642. (is_operand_nil(*y) && type_has_nil(x->type));
  1643. break;
  1644. case Token_Lt:
  1645. case Token_Gt:
  1646. case Token_LtEq:
  1647. case Token_GtEq:
  1648. if (are_types_identical(x->type, y->type) && is_type_bit_set(x->type)) {
  1649. defined = true;
  1650. } else {
  1651. defined = is_type_ordered(x->type) && is_type_ordered(y->type);
  1652. }
  1653. break;
  1654. }
  1655. if (!defined) {
  1656. if (x->type == err_type && is_operand_nil(*x)) {
  1657. err_type = y->type;
  1658. }
  1659. gbString type_string = type_to_string(err_type);
  1660. defer (gb_string_free(type_string));
  1661. err_str = gb_string_make(temporary_allocator(),
  1662. gb_bprintf("operator '%.*s' not defined for type '%s'", LIT(token_strings[op]), type_string));
  1663. } else {
  1664. Type *comparison_type = x->type;
  1665. if (x->type == err_type && is_operand_nil(*x)) {
  1666. comparison_type = y->type;
  1667. }
  1668. add_comparison_procedures_for_fields(c, comparison_type);
  1669. }
  1670. } else {
  1671. gbString xt, yt;
  1672. if (x->mode == Addressing_ProcGroup) {
  1673. xt = gb_string_make(heap_allocator(), "procedure group");
  1674. } else {
  1675. xt = type_to_string(x->type);
  1676. }
  1677. if (y->mode == Addressing_ProcGroup) {
  1678. yt = gb_string_make(heap_allocator(), "procedure group");
  1679. } else {
  1680. yt = type_to_string(y->type);
  1681. }
  1682. err_str = gb_string_make(temporary_allocator(), gb_bprintf("mismatched types '%s' and '%s'", xt, yt));
  1683. gb_string_free(yt);
  1684. gb_string_free(xt);
  1685. }
  1686. if (err_str != nullptr) {
  1687. error(x->expr, "Cannot compare expression, %s", err_str);
  1688. x->type = t_untyped_bool;
  1689. } else {
  1690. if (x->mode == Addressing_Constant &&
  1691. y->mode == Addressing_Constant) {
  1692. if (is_type_constant_type(x->type)) {
  1693. if (is_type_bit_set(x->type)) {
  1694. switch (op) {
  1695. case Token_CmpEq:
  1696. case Token_NotEq:
  1697. x->value = exact_value_bool(compare_exact_values(op, x->value, y->value));
  1698. break;
  1699. case Token_Lt:
  1700. case Token_LtEq:
  1701. {
  1702. ExactValue lhs = x->value;
  1703. ExactValue rhs = y->value;
  1704. ExactValue res = exact_binary_operator_value(Token_And, lhs, rhs);
  1705. res = exact_value_bool(compare_exact_values(op, res, lhs));
  1706. if (op == Token_Lt) {
  1707. res = exact_binary_operator_value(Token_And, res, exact_value_bool(compare_exact_values(op, lhs, rhs)));
  1708. }
  1709. x->value = res;
  1710. break;
  1711. }
  1712. case Token_Gt:
  1713. case Token_GtEq:
  1714. {
  1715. ExactValue lhs = x->value;
  1716. ExactValue rhs = y->value;
  1717. ExactValue res = exact_binary_operator_value(Token_And, lhs, rhs);
  1718. res = exact_value_bool(compare_exact_values(op, res, rhs));
  1719. if (op == Token_Gt) {
  1720. res = exact_binary_operator_value(Token_And, res, exact_value_bool(compare_exact_values(op, lhs, rhs)));
  1721. }
  1722. x->value = res;
  1723. break;
  1724. }
  1725. }
  1726. } else {
  1727. x->value = exact_value_bool(compare_exact_values(op, x->value, y->value));
  1728. }
  1729. } else {
  1730. x->mode = Addressing_Value;
  1731. }
  1732. } else {
  1733. x->mode = Addressing_Value;
  1734. update_expr_type(c, x->expr, default_type(x->type), true);
  1735. update_expr_type(c, y->expr, default_type(y->type), true);
  1736. i64 size = 0;
  1737. if (!is_type_untyped(x->type)) size = gb_max(size, type_size_of(x->type));
  1738. if (!is_type_untyped(y->type)) size = gb_max(size, type_size_of(y->type));
  1739. if (is_type_string(x->type) || is_type_string(y->type)) {
  1740. switch (op) {
  1741. case Token_CmpEq: add_package_dependency(c, "runtime", "string_eq"); break;
  1742. case Token_NotEq: add_package_dependency(c, "runtime", "string_ne"); break;
  1743. case Token_Lt: add_package_dependency(c, "runtime", "string_lt"); break;
  1744. case Token_Gt: add_package_dependency(c, "runtime", "string_gt"); break;
  1745. case Token_LtEq: add_package_dependency(c, "runtime", "string_le"); break;
  1746. case Token_GtEq: add_package_dependency(c, "runtime", "string_gt"); break;
  1747. }
  1748. } else if (is_type_complex(x->type) || is_type_complex(y->type)) {
  1749. switch (op) {
  1750. case Token_CmpEq:
  1751. switch (8*size) {
  1752. case 64: add_package_dependency(c, "runtime", "complex64_eq"); break;
  1753. case 128: add_package_dependency(c, "runtime", "complex128_eq"); break;
  1754. }
  1755. break;
  1756. case Token_NotEq:
  1757. switch (8*size) {
  1758. case 64: add_package_dependency(c, "runtime", "complex64_ne"); break;
  1759. case 128: add_package_dependency(c, "runtime", "complex128_ne"); break;
  1760. }
  1761. break;
  1762. }
  1763. } else if (is_type_quaternion(x->type) || is_type_quaternion(y->type)) {
  1764. switch (op) {
  1765. case Token_CmpEq:
  1766. switch (8*size) {
  1767. case 128: add_package_dependency(c, "runtime", "quaternion128_eq"); break;
  1768. case 256: add_package_dependency(c, "runtime", "quaternion256_eq"); break;
  1769. }
  1770. break;
  1771. case Token_NotEq:
  1772. switch (8*size) {
  1773. case 128: add_package_dependency(c, "runtime", "quaternion128_ne"); break;
  1774. case 256: add_package_dependency(c, "runtime", "quaternion256_ne"); break;
  1775. }
  1776. break;
  1777. }
  1778. }
  1779. }
  1780. x->type = t_untyped_bool;
  1781. }
  1782. }
  1783. void check_shift(CheckerContext *c, Operand *x, Operand *y, Ast *node, Type *type_hint) {
  1784. GB_ASSERT(node->kind == Ast_BinaryExpr);
  1785. ast_node(be, BinaryExpr, node);
  1786. ExactValue x_val = {};
  1787. if (x->mode == Addressing_Constant) {
  1788. x_val = exact_value_to_integer(x->value);
  1789. }
  1790. bool x_is_untyped = is_type_untyped(x->type);
  1791. if (!(is_type_integer(x->type) || (x_is_untyped && x_val.kind == ExactValue_Integer))) {
  1792. gbString err_str = expr_to_string(x->expr);
  1793. error(node, "Shifted operand '%s' must be an integer", err_str);
  1794. gb_string_free(err_str);
  1795. x->mode = Addressing_Invalid;
  1796. return;
  1797. }
  1798. if (is_type_unsigned(y->type)) {
  1799. } else if (is_type_untyped(y->type)) {
  1800. convert_to_typed(c, y, t_untyped_integer);
  1801. if (y->mode == Addressing_Invalid) {
  1802. x->mode = Addressing_Invalid;
  1803. return;
  1804. }
  1805. } else {
  1806. gbString err_str = expr_to_string(y->expr);
  1807. error(node, "Shift amount '%s' must be an unsigned integer", err_str);
  1808. gb_string_free(err_str);
  1809. x->mode = Addressing_Invalid;
  1810. return;
  1811. }
  1812. if (x->mode == Addressing_Constant) {
  1813. if (y->mode == Addressing_Constant) {
  1814. ExactValue y_val = exact_value_to_integer(y->value);
  1815. if (y_val.kind != ExactValue_Integer) {
  1816. gbString err_str = expr_to_string(y->expr);
  1817. error(node, "Shift amount '%s' must be an unsigned integer", err_str);
  1818. gb_string_free(err_str);
  1819. x->mode = Addressing_Invalid;
  1820. return;
  1821. }
  1822. BigInt max_shift = {};
  1823. big_int_from_u64(&max_shift, 128);
  1824. if (big_int_cmp(&y_val.value_integer, &max_shift) > 0) {
  1825. gbString err_str = expr_to_string(y->expr);
  1826. error(node, "Shift amount too large: '%s'", err_str);
  1827. gb_string_free(err_str);
  1828. x->mode = Addressing_Invalid;
  1829. return;
  1830. }
  1831. if (!is_type_integer(x->type)) {
  1832. // NOTE(bill): It could be an untyped float but still representable
  1833. // as an integer
  1834. x->type = t_untyped_integer;
  1835. }
  1836. x->value = exact_value_shift(be->op.kind, x_val, y_val);
  1837. if (is_type_typed(x->type)) {
  1838. check_is_expressible(c, x, base_type(x->type));
  1839. }
  1840. return;
  1841. }
  1842. TokenPos pos = ast_token(x->expr).pos;
  1843. if (x_is_untyped) {
  1844. ExprInfo *info = check_get_expr_info(&c->checker->info, x->expr);
  1845. if (info != nullptr) {
  1846. info->is_lhs = true;
  1847. }
  1848. x->mode = Addressing_Value;
  1849. if (type_hint && is_type_integer(type_hint)) {
  1850. x->type = type_hint;
  1851. }
  1852. // x->value = x_val;
  1853. return;
  1854. }
  1855. }
  1856. if (y->mode == Addressing_Constant && y->value.value_integer.neg) {
  1857. gbString err_str = expr_to_string(y->expr);
  1858. error(node, "Shift amount cannot be negative: '%s'", err_str);
  1859. gb_string_free(err_str);
  1860. }
  1861. if (!is_type_integer(x->type)) {
  1862. gbString err_str = expr_to_string(y->expr);
  1863. error(node, "Shift operand '%s' must be an integer", err_str);
  1864. gb_string_free(err_str);
  1865. x->mode = Addressing_Invalid;
  1866. return;
  1867. }
  1868. x->mode = Addressing_Value;
  1869. }
  1870. bool check_is_castable_to(CheckerContext *c, Operand *operand, Type *y) {
  1871. if (check_is_assignable_to(c, operand, y)) {
  1872. return true;
  1873. }
  1874. bool is_constant = operand->mode == Addressing_Constant;
  1875. Type *x = operand->type;
  1876. Type *src = core_type(x);
  1877. Type *dst = core_type(y);
  1878. if (are_types_identical(src, dst)) {
  1879. return true;
  1880. }
  1881. if (is_constant && is_type_untyped(src) && is_type_string(src)) {
  1882. if (is_type_u8_array(dst)) {
  1883. String s = operand->value.value_string;
  1884. return s.len == dst->Array.count;
  1885. }
  1886. if (is_type_rune_array(dst)) {
  1887. String s = operand->value.value_string;
  1888. return gb_utf8_strnlen(s.text, s.len) == dst->Array.count;
  1889. }
  1890. }
  1891. if (dst->kind == Type_Array && src->kind == Type_Array) {
  1892. if (are_types_identical(dst->Array.elem, src->Array.elem)) {
  1893. return dst->Array.count == src->Array.count;
  1894. }
  1895. }
  1896. if (dst->kind == Type_Slice && src->kind == Type_Slice) {
  1897. return are_types_identical(dst->Slice.elem, src->Slice.elem);
  1898. }
  1899. // Cast between booleans and integers
  1900. if (is_type_boolean(src) || is_type_integer(src)) {
  1901. if (is_type_boolean(dst) || is_type_integer(dst)) {
  1902. return true;
  1903. }
  1904. }
  1905. // Cast between numbers
  1906. if (is_type_integer(src) || is_type_float(src)) {
  1907. if (is_type_integer(dst) || is_type_float(dst)) {
  1908. return true;
  1909. }
  1910. }
  1911. if (is_type_integer(src) && is_type_rune(dst)) {
  1912. return true;
  1913. }
  1914. if (is_type_rune(src) && is_type_integer(dst)) {
  1915. return true;
  1916. }
  1917. if (is_type_complex(src) && is_type_complex(dst)) {
  1918. return true;
  1919. }
  1920. if (is_type_complex(src) && is_type_quaternion(dst)) {
  1921. return true;
  1922. }
  1923. if (is_type_quaternion(src) && is_type_quaternion(dst)) {
  1924. return true;
  1925. }
  1926. // Cast between pointers
  1927. if (is_type_pointer(src) && is_type_pointer(dst)) {
  1928. return true;
  1929. }
  1930. // uintptr <-> pointer
  1931. if (is_type_uintptr(src) && is_type_pointer(dst)) {
  1932. return true;
  1933. }
  1934. if (is_type_pointer(src) && is_type_uintptr(dst)) {
  1935. return true;
  1936. }
  1937. // []byte/[]u8 <-> string (not cstring)
  1938. if (is_type_u8_slice(src) && (is_type_string(dst) && !is_type_cstring(dst))) {
  1939. return true;
  1940. }
  1941. if ((is_type_string(src) && !is_type_cstring(src)) && is_type_u8_slice(dst)) {
  1942. // if (is_type_typed(src)) {
  1943. // return true;
  1944. // }
  1945. }
  1946. // cstring -> string
  1947. if (are_types_identical(src, t_cstring) && are_types_identical(dst, t_string)) {
  1948. if (operand->mode != Addressing_Constant) {
  1949. add_package_dependency(c, "runtime", "cstring_to_string");
  1950. }
  1951. return true;
  1952. }
  1953. // cstring -> ^u8
  1954. if (are_types_identical(src, t_cstring) && is_type_u8_ptr(dst)) {
  1955. return !is_constant;
  1956. }
  1957. // cstring -> rawptr
  1958. if (are_types_identical(src, t_cstring) && is_type_rawptr(dst)) {
  1959. return !is_constant;
  1960. }
  1961. // ^u8 -> cstring
  1962. if (is_type_u8_ptr(src) && are_types_identical(dst, t_cstring)) {
  1963. return !is_constant;
  1964. }
  1965. // rawptr -> cstring
  1966. if (is_type_rawptr(src) && are_types_identical(dst, t_cstring)) {
  1967. return !is_constant;
  1968. }
  1969. // proc <-> proc
  1970. if (is_type_proc(src) && is_type_proc(dst)) {
  1971. return true;
  1972. }
  1973. // proc -> rawptr
  1974. if (is_type_proc(src) && is_type_rawptr(dst)) {
  1975. return true;
  1976. }
  1977. // rawptr -> proc
  1978. if (is_type_rawptr(src) && is_type_proc(dst)) {
  1979. return true;
  1980. }
  1981. return false;
  1982. }
  1983. bool check_cast_internal(CheckerContext *c, Operand *x, Type *type) {
  1984. bool is_const_expr = x->mode == Addressing_Constant;
  1985. bool can_convert = false;
  1986. Type *bt = base_type(type);
  1987. if (is_const_expr && is_type_constant_type(bt)) {
  1988. if (core_type(bt)->kind == Type_Basic) {
  1989. if (check_representable_as_constant(c, x->value, bt, &x->value)) {
  1990. return true;
  1991. } else if (is_type_pointer(type) && check_is_castable_to(c, x, type)) {
  1992. return true;
  1993. }
  1994. }
  1995. } else if (check_is_castable_to(c, x, type)) {
  1996. if (x->mode != Addressing_Constant) {
  1997. x->mode = Addressing_Value;
  1998. } else if (is_type_slice(type) && is_type_string(x->type)) {
  1999. x->mode = Addressing_Value;
  2000. } else if (is_type_union(type)) {
  2001. x->mode = Addressing_Value;
  2002. }
  2003. return true;
  2004. }
  2005. return false;
  2006. }
  2007. void check_cast(CheckerContext *c, Operand *x, Type *type) {
  2008. if (!is_operand_value(*x)) {
  2009. error(x->expr, "Only values can be casted");
  2010. x->mode = Addressing_Invalid;
  2011. return;
  2012. }
  2013. bool is_const_expr = x->mode == Addressing_Constant;
  2014. bool can_convert = check_cast_internal(c, x, type);
  2015. if (!can_convert) {
  2016. gbString expr_str = expr_to_string(x->expr);
  2017. gbString to_type = type_to_string(type);
  2018. gbString from_type = type_to_string(x->type);
  2019. error(x->expr, "Cannot cast '%s' as '%s' from '%s'", expr_str, to_type, from_type);
  2020. gb_string_free(from_type);
  2021. gb_string_free(to_type);
  2022. gb_string_free(expr_str);
  2023. check_cast_error_suggestion(c, x, type);
  2024. x->mode = Addressing_Invalid;
  2025. return;
  2026. }
  2027. if (is_type_untyped(x->type)) {
  2028. Type *final_type = type;
  2029. if (is_const_expr && !is_type_constant_type(type)) {
  2030. final_type = default_type(x->type);
  2031. }
  2032. update_expr_type(c, x->expr, final_type, true);
  2033. }
  2034. if (build_context.vet_extra) {
  2035. if (are_types_identical(x->type, type)) {
  2036. gbString str = type_to_string(type);
  2037. warning(x->expr, "Unneeded cast to the same type '%s'", str);
  2038. gb_string_free(str);
  2039. }
  2040. }
  2041. x->type = type;
  2042. }
  2043. bool check_transmute(CheckerContext *c, Ast *node, Operand *o, Type *t) {
  2044. if (!is_operand_value(*o)) {
  2045. error(o->expr, "'transmute' can only be applied to values");
  2046. o->mode = Addressing_Invalid;
  2047. return false;
  2048. }
  2049. if (o->mode == Addressing_Constant) {
  2050. gbString expr_str = expr_to_string(o->expr);
  2051. error(o->expr, "Cannot transmute a constant expression: '%s'", expr_str);
  2052. gb_string_free(expr_str);
  2053. o->mode = Addressing_Invalid;
  2054. o->expr = node;
  2055. return false;
  2056. }
  2057. if (is_type_untyped(o->type)) {
  2058. gbString expr_str = expr_to_string(o->expr);
  2059. error(o->expr, "Cannot transmute untyped expression: '%s'", expr_str);
  2060. gb_string_free(expr_str);
  2061. o->mode = Addressing_Invalid;
  2062. o->expr = node;
  2063. return false;
  2064. }
  2065. i64 srcz = type_size_of(o->type);
  2066. i64 dstz = type_size_of(t);
  2067. if (srcz != dstz) {
  2068. gbString expr_str = expr_to_string(o->expr);
  2069. gbString type_str = type_to_string(t);
  2070. error(o->expr, "Cannot transmute '%s' to '%s', %lld vs %lld bytes", expr_str, type_str, srcz, dstz);
  2071. gb_string_free(type_str);
  2072. gb_string_free(expr_str);
  2073. o->mode = Addressing_Invalid;
  2074. o->expr = node;
  2075. return false;
  2076. }
  2077. if (build_context.vet_extra) {
  2078. if (are_types_identical(o->type, t)) {
  2079. gbString str = type_to_string(t);
  2080. warning(o->expr, "Unneeded transmute to the same type '%s'", str);
  2081. gb_string_free(str);
  2082. }
  2083. }
  2084. o->mode = Addressing_Value;
  2085. o->type = t;
  2086. return true;
  2087. }
  2088. bool check_binary_array_expr(CheckerContext *c, Token op, Operand *x, Operand *y) {
  2089. if (is_type_array(x->type) && !is_type_array(y->type)) {
  2090. if (check_is_assignable_to(c, y, x->type)) {
  2091. if (check_binary_op(c, x, op)) {
  2092. return true;
  2093. }
  2094. }
  2095. }
  2096. return false;
  2097. }
  2098. void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, Type *type_hint, bool use_lhs_as_type_hint=false) {
  2099. GB_ASSERT(node->kind == Ast_BinaryExpr);
  2100. Operand y_ = {}, *y = &y_;
  2101. ast_node(be, BinaryExpr, node);
  2102. defer({
  2103. node->viral_state_flags |= be->left->viral_state_flags;
  2104. node->viral_state_flags |= be->right->viral_state_flags;
  2105. });
  2106. Token op = be->op;
  2107. switch (op.kind) {
  2108. case Token_CmpEq:
  2109. case Token_NotEq: {
  2110. // NOTE(bill): Allow comparisons between types
  2111. check_expr_or_type(c, x, be->left, type_hint);
  2112. check_expr_or_type(c, y, be->right, x->type);
  2113. bool xt = x->mode == Addressing_Type;
  2114. bool yt = y->mode == Addressing_Type;
  2115. // If only one is a type, this is an error
  2116. if (xt ^ yt) {
  2117. GB_ASSERT(xt != yt);
  2118. if (xt) {
  2119. if (!is_type_typeid(y->type)) {
  2120. error_operand_not_expression(x);
  2121. }
  2122. }
  2123. if (yt) {
  2124. if (!is_type_typeid(x->type)) {
  2125. error_operand_not_expression(y);
  2126. }
  2127. }
  2128. }
  2129. break;
  2130. }
  2131. case Token_in:
  2132. case Token_not_in:
  2133. // IMPORTANT NOTE(bill): This uses right-left evaluation in type checking only no in
  2134. check_expr(c, y, be->right);
  2135. if (is_type_bit_set(y->type)) {
  2136. Type *elem = base_type(y->type)->BitSet.elem;
  2137. check_expr_with_type_hint(c, x, be->left, elem);
  2138. } else if (is_type_map(y->type)) {
  2139. Type *key = base_type(y->type)->Map.key;
  2140. check_expr_with_type_hint(c, x, be->left, key);
  2141. } else {
  2142. check_expr(c, x, be->left);
  2143. }
  2144. if (x->mode == Addressing_Invalid) {
  2145. return;
  2146. }
  2147. if (y->mode == Addressing_Invalid) {
  2148. x->mode = Addressing_Invalid;
  2149. x->expr = y->expr;
  2150. return;
  2151. }
  2152. if (is_type_map(y->type)) {
  2153. Type *yt = base_type(y->type);
  2154. if (op.kind == Token_in) {
  2155. check_assignment(c, x, yt->Map.key, str_lit("map 'in'"));
  2156. } else {
  2157. check_assignment(c, x, yt->Map.key, str_lit("map 'not_in'"));
  2158. }
  2159. add_package_dependency(c, "runtime", "__dynamic_map_get");
  2160. } else if (is_type_bit_set(y->type)) {
  2161. Type *yt = base_type(y->type);
  2162. if (op.kind == Token_in) {
  2163. check_assignment(c, x, yt->BitSet.elem, str_lit("bit_set 'in'"));
  2164. } else {
  2165. check_assignment(c, x, yt->BitSet.elem, str_lit("bit_set 'not_in'"));
  2166. }
  2167. if (x->mode == Addressing_Constant && y->mode == Addressing_Constant) {
  2168. ExactValue k = exact_value_to_integer(x->value);
  2169. ExactValue v = exact_value_to_integer(y->value);
  2170. GB_ASSERT(k.kind == ExactValue_Integer);
  2171. GB_ASSERT(v.kind == ExactValue_Integer);
  2172. i64 key = big_int_to_i64(&k.value_integer);
  2173. i64 lower = yt->BitSet.lower;
  2174. i64 upper = yt->BitSet.upper;
  2175. if (lower <= key && key <= upper) {
  2176. i64 bit = 1ll<<key;
  2177. i64 bits = big_int_to_i64(&v.value_integer);
  2178. x->mode = Addressing_Constant;
  2179. x->type = t_untyped_bool;
  2180. if (op.kind == Token_in) {
  2181. x->value = exact_value_bool((bit & bits) != 0);
  2182. } else {
  2183. x->value = exact_value_bool((bit & bits) == 0);
  2184. }
  2185. x->expr = node;
  2186. return;
  2187. } else {
  2188. error(x->expr, "key '%lld' out of range of bit set, %lld..%lld", key, lower, upper);
  2189. x->mode = Addressing_Invalid;
  2190. }
  2191. }
  2192. } else {
  2193. gbString t = type_to_string(y->type);
  2194. error(x->expr, "expected either a map or bitset for 'in', got %s", t);
  2195. gb_string_free(t);
  2196. x->expr = node;
  2197. x->mode = Addressing_Invalid;
  2198. return;
  2199. }
  2200. if (x->mode != Addressing_Invalid) {
  2201. x->mode = Addressing_Value;
  2202. x->type = t_untyped_bool;
  2203. }
  2204. x->expr = node;
  2205. return;
  2206. default:
  2207. check_expr_with_type_hint(c, x, be->left, type_hint);
  2208. if (use_lhs_as_type_hint) {
  2209. check_expr_with_type_hint(c, y, be->right, x->type);
  2210. } else {
  2211. check_expr_with_type_hint(c, y, be->right, type_hint);
  2212. }
  2213. break;
  2214. }
  2215. if (x->mode == Addressing_Invalid) {
  2216. return;
  2217. }
  2218. if (y->mode == Addressing_Invalid) {
  2219. x->mode = Addressing_Invalid;
  2220. x->expr = y->expr;
  2221. return;
  2222. }
  2223. if (x->mode == Addressing_Builtin) {
  2224. x->mode = Addressing_Invalid;
  2225. error(x->expr, "built-in expression in binary expression");
  2226. return;
  2227. }
  2228. if (y->mode == Addressing_Builtin) {
  2229. x->mode = Addressing_Invalid;
  2230. error(y->expr, "built-in expression in binary expression");
  2231. return;
  2232. }
  2233. if (token_is_shift(op.kind)) {
  2234. check_shift(c, x, y, node, type_hint);
  2235. return;
  2236. }
  2237. convert_to_typed(c, x, y->type);
  2238. if (x->mode == Addressing_Invalid) {
  2239. return;
  2240. }
  2241. convert_to_typed(c, y, x->type);
  2242. if (y->mode == Addressing_Invalid) {
  2243. x->mode = Addressing_Invalid;
  2244. return;
  2245. }
  2246. if (token_is_comparison(op.kind)) {
  2247. check_comparison(c, x, y, op.kind);
  2248. return;
  2249. }
  2250. if (check_binary_array_expr(c, op, x, y)) {
  2251. x->mode = Addressing_Value;
  2252. x->type = x->type;
  2253. return;
  2254. }
  2255. if (check_binary_array_expr(c, op, y, x)) {
  2256. x->mode = Addressing_Value;
  2257. x->type = y->type;
  2258. return;
  2259. }
  2260. if (!are_types_identical(x->type, y->type)) {
  2261. if (x->type != t_invalid &&
  2262. y->type != t_invalid) {
  2263. gbString xt = type_to_string(x->type);
  2264. gbString yt = type_to_string(y->type);
  2265. gbString expr_str = expr_to_string(x->expr);
  2266. error(op, "Mismatched types in binary expression '%s' : '%s' vs '%s'", expr_str, xt, yt);
  2267. gb_string_free(expr_str);
  2268. gb_string_free(yt);
  2269. gb_string_free(xt);
  2270. }
  2271. x->mode = Addressing_Invalid;
  2272. return;
  2273. }
  2274. if (!check_binary_op(c, x, op)) {
  2275. x->mode = Addressing_Invalid;
  2276. return;
  2277. }
  2278. switch (op.kind) {
  2279. case Token_Quo:
  2280. case Token_Mod:
  2281. case Token_ModMod:
  2282. case Token_QuoEq:
  2283. case Token_ModEq:
  2284. case Token_ModModEq:
  2285. if ((x->mode == Addressing_Constant || is_type_integer(x->type)) &&
  2286. y->mode == Addressing_Constant) {
  2287. bool fail = false;
  2288. switch (y->value.kind) {
  2289. case ExactValue_Integer:
  2290. if (big_int_is_zero(&y->value.value_integer)) {
  2291. fail = true;
  2292. }
  2293. break;
  2294. case ExactValue_Float:
  2295. if (y->value.value_float == 0.0) {
  2296. fail = true;
  2297. }
  2298. break;
  2299. }
  2300. if (fail) {
  2301. error(y->expr, "Division by zero not allowed");
  2302. x->mode = Addressing_Invalid;
  2303. return;
  2304. }
  2305. }
  2306. break;
  2307. case Token_CmpAnd:
  2308. case Token_CmpOr:
  2309. if (be->left->viral_state_flags & ViralStateFlag_ContainsDeferredProcedure) {
  2310. error(be->left, "Procedure calls that have an associated deferred procedure are not allowed within logical binary expressions");
  2311. }
  2312. if (be->right->viral_state_flags & ViralStateFlag_ContainsDeferredProcedure) {
  2313. error(be->right, "Procedure calls that have an associated deferred procedure are not allowed within logical binary expressions");
  2314. }
  2315. break;
  2316. }
  2317. if (x->mode == Addressing_Constant &&
  2318. y->mode == Addressing_Constant) {
  2319. ExactValue a = x->value;
  2320. ExactValue b = y->value;
  2321. // Type *type = base_type(x->type);
  2322. Type *type = x->type;
  2323. if (is_type_pointer(type)) {
  2324. GB_ASSERT(op.kind == Token_Sub);
  2325. i64 bytes = a.value_pointer - b.value_pointer;
  2326. i64 diff = bytes/type_size_of(type);
  2327. x->value = exact_value_pointer(diff);
  2328. return;
  2329. }
  2330. if (!is_type_constant_type(type)) {
  2331. gbString xt = type_to_string(x->type);
  2332. gbString err_str = expr_to_string(node);
  2333. error(op, "Invalid type, '%s', for constant binary expression '%s'", xt, err_str);
  2334. gb_string_free(err_str);
  2335. gb_string_free(xt);
  2336. x->mode = Addressing_Invalid;
  2337. return;
  2338. }
  2339. if (op.kind == Token_Quo && is_type_integer(type)) {
  2340. op.kind = Token_QuoEq; // NOTE(bill): Hack to get division of integers
  2341. }
  2342. x->value = exact_binary_operator_value(op.kind, a, b);
  2343. if (is_type_typed(type)) {
  2344. if (node != nullptr) {
  2345. x->expr = node;
  2346. }
  2347. check_is_expressible(c, x, type);
  2348. }
  2349. return;
  2350. } else if (is_type_string(x->type)) {
  2351. error(node, "String concatenation is only allowed with constant strings");
  2352. x->mode = Addressing_Invalid;
  2353. return;
  2354. }
  2355. if (op.kind == Token_Quo || op.kind == Token_QuoEq) {
  2356. Type *bt = base_type(x->type);
  2357. if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
  2358. case Basic_complex64: add_package_dependency(c, "runtime", "quo_complex64"); break;
  2359. case Basic_complex128: add_package_dependency(c, "runtime", "quo_complex128"); break;
  2360. case Basic_quaternion128: add_package_dependency(c, "runtime", "quo_quaternion128"); break;
  2361. case Basic_quaternion256: add_package_dependency(c, "runtime", "quo_quaternion256"); break;
  2362. }
  2363. } else if (op.kind == Token_Mul || op.kind == Token_MulEq) {
  2364. Type *bt = base_type(x->type);
  2365. if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
  2366. case Basic_quaternion128: add_package_dependency(c, "runtime", "mul_quaternion128"); break;
  2367. case Basic_quaternion256: add_package_dependency(c, "runtime", "mul_quaternion256"); break;
  2368. }
  2369. }
  2370. x->mode = Addressing_Value;
  2371. }
  2372. void update_expr_type(CheckerContext *c, Ast *e, Type *type, bool final) {
  2373. ExprInfo *found = check_get_expr_info(&c->checker->info, e);
  2374. if (found == nullptr) {
  2375. return;
  2376. }
  2377. ExprInfo old = *found;
  2378. switch (e->kind) {
  2379. case_ast_node(ue, UnaryExpr, e);
  2380. if (old.value.kind != ExactValue_Invalid) {
  2381. // NOTE(bill): if 'e' is constant, the operands will be constant too.
  2382. // They don't need to be updated as they will be updated later and
  2383. // checked at the end of general checking stage.
  2384. break;
  2385. }
  2386. update_expr_type(c, ue->expr, type, final);
  2387. case_end;
  2388. case_ast_node(be, BinaryExpr, e);
  2389. if (old.value.kind != ExactValue_Invalid) {
  2390. // See above note in UnaryExpr case
  2391. break;
  2392. }
  2393. if (token_is_comparison(be->op.kind)) {
  2394. // NOTE(bill): Do nothing as the types are fine
  2395. } else if (token_is_shift(be->op.kind)) {
  2396. update_expr_type(c, be->left, type, final);
  2397. } else {
  2398. update_expr_type(c, be->left, type, final);
  2399. update_expr_type(c, be->right, type, final);
  2400. }
  2401. case_end;
  2402. case_ast_node(pe, ParenExpr, e);
  2403. update_expr_type(c, pe->expr, type, final);
  2404. case_end;
  2405. }
  2406. if (!final && is_type_untyped(type)) {
  2407. old.type = base_type(type);
  2408. check_set_expr_info(&c->checker->info, e, old);
  2409. return;
  2410. }
  2411. // We need to remove it and then give it a new one
  2412. check_remove_expr_info(&c->checker->info, e);
  2413. if (old.is_lhs && !is_type_integer(type)) {
  2414. gbString expr_str = expr_to_string(e);
  2415. gbString type_str = type_to_string(type);
  2416. error(e, "Shifted operand %s must be an integer, got %s", expr_str, type_str);
  2417. gb_string_free(type_str);
  2418. gb_string_free(expr_str);
  2419. return;
  2420. }
  2421. add_type_and_value(&c->checker->info, e, old.mode, type, old.value);
  2422. }
  2423. void update_expr_value(CheckerContext *c, Ast *e, ExactValue value) {
  2424. ExprInfo *found = check_get_expr_info(&c->checker->info, e);
  2425. if (found) {
  2426. found->value = value;
  2427. }
  2428. }
  2429. void convert_untyped_error(CheckerContext *c, Operand *operand, Type *target_type) {
  2430. gbString expr_str = expr_to_string(operand->expr);
  2431. gbString type_str = type_to_string(target_type);
  2432. char const *extra_text = "";
  2433. if (operand->mode == Addressing_Constant) {
  2434. if (big_int_is_zero(&operand->value.value_integer)) {
  2435. if (make_string_c(expr_str) != "nil") { // HACK NOTE(bill): Just in case
  2436. // NOTE(bill): Doesn't matter what the type is as it's still zero in the union
  2437. extra_text = " - Did you want 'nil'?";
  2438. }
  2439. }
  2440. }
  2441. error(operand->expr, "Cannot convert '%s' to '%s'%s", expr_str, type_str, extra_text);
  2442. gb_string_free(type_str);
  2443. gb_string_free(expr_str);
  2444. operand->mode = Addressing_Invalid;
  2445. }
  2446. ExactValue convert_exact_value_for_type(ExactValue v, Type *type) {
  2447. Type *t = core_type(type);
  2448. if (is_type_boolean(t)) {
  2449. // v = exact_value_to_boolean(v);
  2450. } else if (is_type_float(t)) {
  2451. v = exact_value_to_float(v);
  2452. } else if (is_type_integer(t)) {
  2453. v = exact_value_to_integer(v);
  2454. } else if (is_type_pointer(t)) {
  2455. v = exact_value_to_integer(v);
  2456. } else if (is_type_complex(t)) {
  2457. v = exact_value_to_complex(v);
  2458. } else if (is_type_quaternion(t)) {
  2459. v = exact_value_to_quaternion(v);
  2460. }
  2461. return v;
  2462. }
  2463. void convert_to_typed(CheckerContext *c, Operand *operand, Type *target_type) {
  2464. GB_ASSERT_NOT_NULL(target_type);
  2465. if (operand->mode == Addressing_Invalid ||
  2466. operand->mode == Addressing_Type ||
  2467. is_type_typed(operand->type) ||
  2468. target_type == t_invalid) {
  2469. return;
  2470. }
  2471. if (is_type_untyped(target_type)) {
  2472. GB_ASSERT(operand->type->kind == Type_Basic);
  2473. GB_ASSERT(target_type->kind == Type_Basic);
  2474. BasicKind x_kind = operand->type->Basic.kind;
  2475. BasicKind y_kind = target_type->Basic.kind;
  2476. if (is_type_numeric(operand->type) && is_type_numeric(target_type)) {
  2477. if (x_kind < y_kind) {
  2478. operand->type = target_type;
  2479. update_expr_type(c, operand->expr, target_type, false);
  2480. }
  2481. } else if (x_kind != y_kind) {
  2482. operand->mode = Addressing_Invalid;
  2483. convert_untyped_error(c, operand, target_type);
  2484. return;
  2485. }
  2486. return;
  2487. }
  2488. Type *t = base_type(target_type);
  2489. if (c->in_enum_type) {
  2490. t = core_type(target_type);
  2491. }
  2492. switch (t->kind) {
  2493. case Type_Basic:
  2494. if (operand->mode == Addressing_Constant) {
  2495. check_is_expressible(c, operand, t);
  2496. if (operand->mode == Addressing_Invalid) {
  2497. return;
  2498. }
  2499. update_expr_value(c, operand->expr, operand->value);
  2500. } else {
  2501. switch (operand->type->Basic.kind) {
  2502. case Basic_UntypedBool:
  2503. if (!is_type_boolean(target_type)) {
  2504. operand->mode = Addressing_Invalid;
  2505. convert_untyped_error(c, operand, target_type);
  2506. return;
  2507. }
  2508. break;
  2509. case Basic_UntypedInteger:
  2510. case Basic_UntypedFloat:
  2511. case Basic_UntypedComplex:
  2512. case Basic_UntypedQuaternion:
  2513. case Basic_UntypedRune:
  2514. if (!is_type_numeric(target_type)) {
  2515. operand->mode = Addressing_Invalid;
  2516. convert_untyped_error(c, operand, target_type);
  2517. return;
  2518. }
  2519. break;
  2520. case Basic_UntypedNil:
  2521. if (is_type_any(target_type)) {
  2522. target_type = t_untyped_nil;
  2523. } else if (is_type_cstring(target_type)) {
  2524. target_type = t_untyped_nil;
  2525. } else if (!type_has_nil(target_type)) {
  2526. operand->mode = Addressing_Invalid;
  2527. convert_untyped_error(c, operand, target_type);
  2528. return;
  2529. }
  2530. break;
  2531. }
  2532. }
  2533. break;
  2534. case Type_Array: {
  2535. Type *elem = base_array_type(t);
  2536. if (check_is_assignable_to(c, operand, elem)) {
  2537. operand->mode = Addressing_Value;
  2538. } else {
  2539. if (operand->value.kind == ExactValue_String) {
  2540. String s = operand->value.value_string;
  2541. if (is_type_u8_array(t)) {
  2542. if (s.len == t->Array.count) {
  2543. break;
  2544. }
  2545. } else if (is_type_rune_array(t)) {
  2546. isize rune_count = gb_utf8_strnlen(s.text, s.len);
  2547. if (rune_count == t->Array.count) {
  2548. break;
  2549. }
  2550. }
  2551. }
  2552. operand->mode = Addressing_Invalid;
  2553. convert_untyped_error(c, operand, target_type);
  2554. return;
  2555. }
  2556. break;
  2557. }
  2558. case Type_Union:
  2559. if (!is_operand_nil(*operand) && !is_operand_undef(*operand)) {
  2560. isize count = t->Union.variants.count;
  2561. ValidIndexAndScore *valids = gb_alloc_array(temporary_allocator(), ValidIndexAndScore, count);
  2562. isize valid_count = 0;
  2563. isize first_success_index = -1;
  2564. for_array(i, t->Union.variants) {
  2565. Type *vt = t->Union.variants[i];
  2566. i64 score = 0;
  2567. if (check_is_assignable_to_with_score(c, operand, vt, &score)) {
  2568. valids[valid_count].index = i;
  2569. valids[valid_count].score = score;
  2570. valid_count += 1;
  2571. if (first_success_index < 0) {
  2572. first_success_index = i;
  2573. }
  2574. }
  2575. }
  2576. if (valid_count > 1) {
  2577. gb_sort_array(valids, valid_count, valid_index_and_score_cmp);
  2578. i64 best_score = valids[0].score;
  2579. Type *best_type = t->Union.variants[valids[0].index];
  2580. for (isize i = 1; i < valid_count; i++) {
  2581. auto v = valids[i];
  2582. Type *vt = t->Union.variants[v.index];
  2583. if (best_score > v.score) {
  2584. valid_count = i;
  2585. break;
  2586. }
  2587. best_score = v.score;
  2588. }
  2589. first_success_index = valids[0].index;
  2590. }
  2591. gbString type_str = type_to_string(target_type);
  2592. defer (gb_string_free(type_str));
  2593. if (valid_count == 1) {
  2594. operand->mode = Addressing_Value;
  2595. operand->type = t->Union.variants[first_success_index];
  2596. target_type = t->Union.variants[first_success_index];
  2597. break;
  2598. } else if (valid_count > 1) {
  2599. begin_error_block();
  2600. defer (end_error_block());
  2601. GB_ASSERT(first_success_index >= 0);
  2602. operand->mode = Addressing_Invalid;
  2603. convert_untyped_error(c, operand, target_type);
  2604. error_line("Ambiguous type conversion to '%s', which variant did you mean:\n\t", type_str);
  2605. i32 j = 0;
  2606. for (i32 i = 0; i < valid_count; i++) {
  2607. ValidIndexAndScore valid = valids[i];
  2608. if (j > 0 && valid_count > 2) error_line(", ");
  2609. if (j == valid_count-1) {
  2610. if (valid_count == 2) error_line(" ");
  2611. error_line("or ");
  2612. }
  2613. gbString str = type_to_string(t->Union.variants[valid.index]);
  2614. error_line("'%s'", str);
  2615. gb_string_free(str);
  2616. j++;
  2617. }
  2618. error_line("\n\n");
  2619. return;
  2620. } else if (is_type_untyped_undef(operand->type) && type_has_undef(target_type)) {
  2621. target_type = t_untyped_undef;
  2622. } else if (!is_type_untyped_nil(operand->type) || !type_has_nil(target_type)) {
  2623. begin_error_block();
  2624. defer (end_error_block());
  2625. operand->mode = Addressing_Invalid;
  2626. convert_untyped_error(c, operand, target_type);
  2627. if (count > 0) {
  2628. error_line("'%s' is a union which only excepts the following types:\n", type_str);
  2629. error_line("\t");
  2630. for (i32 i = 0; i < count; i++) {
  2631. Type *v = t->Union.variants[i];
  2632. if (i > 0 && count > 2) error_line(", ");
  2633. if (i == count-1) {
  2634. if (count == 2) error_line(" ");
  2635. error_line("or ");
  2636. }
  2637. gbString str = type_to_string(v);
  2638. error_line("'%s'", str);
  2639. gb_string_free(str);
  2640. }
  2641. error_line("\n\n");
  2642. }
  2643. return;
  2644. }
  2645. }
  2646. /* fallthrough */
  2647. default:
  2648. if (is_type_untyped_undef(operand->type) && type_has_undef(target_type)) {
  2649. target_type = t_untyped_undef;
  2650. } else if (is_type_untyped_nil(operand->type) && type_has_nil(target_type)) {
  2651. target_type = t_untyped_nil;
  2652. } else {
  2653. operand->mode = Addressing_Invalid;
  2654. convert_untyped_error(c, operand, target_type);
  2655. return;
  2656. }
  2657. break;
  2658. }
  2659. operand->type = target_type;
  2660. update_expr_type(c, operand->expr, target_type, true);
  2661. }
  2662. bool check_index_value(CheckerContext *c, bool open_range, Ast *index_value, i64 max_count, i64 *value, Type *type_hint=nullptr) {
  2663. Operand operand = {Addressing_Invalid};
  2664. check_expr_with_type_hint(c, &operand, index_value, type_hint);
  2665. if (operand.mode == Addressing_Invalid) {
  2666. if (value) *value = 0;
  2667. return false;
  2668. }
  2669. Type *index_type = t_int;
  2670. if (type_hint != nullptr) {
  2671. index_type = type_hint;
  2672. }
  2673. convert_to_typed(c, &operand, index_type);
  2674. if (operand.mode == Addressing_Invalid) {
  2675. if (value) *value = 0;
  2676. return false;
  2677. }
  2678. if (type_hint != nullptr) {
  2679. if (!check_is_assignable_to(c, &operand, type_hint)) {
  2680. gbString expr_str = expr_to_string(operand.expr);
  2681. gbString index_type_str = type_to_string(type_hint);
  2682. error(operand.expr, "Index '%s' must be an enum of type '%s'", expr_str, index_type_str);
  2683. gb_string_free(index_type_str);
  2684. gb_string_free(expr_str);
  2685. if (value) *value = 0;
  2686. return false;
  2687. }
  2688. } else if (!is_type_integer(operand.type) && !is_type_enum(operand.type)) {
  2689. gbString expr_str = expr_to_string(operand.expr);
  2690. error(operand.expr, "Index '%s' must be an integer", expr_str);
  2691. gb_string_free(expr_str);
  2692. if (value) *value = 0;
  2693. return false;
  2694. }
  2695. if (operand.mode == Addressing_Constant &&
  2696. (c->state_flags & StateFlag_no_bounds_check) == 0) {
  2697. BigInt i = exact_value_to_integer(operand.value).value_integer;
  2698. if (i.neg && !is_type_enum(index_type)) {
  2699. gbString expr_str = expr_to_string(operand.expr);
  2700. error(operand.expr, "Index '%s' cannot be a negative value", expr_str);
  2701. gb_string_free(expr_str);
  2702. if (value) *value = 0;
  2703. return false;
  2704. }
  2705. if (max_count >= 0) {
  2706. if (is_type_enum(index_type)) {
  2707. Type *bt = base_type(index_type);
  2708. GB_ASSERT(bt->kind == Type_Enum);
  2709. ExactValue lo = bt->Enum.min_value;
  2710. ExactValue hi = bt->Enum.max_value;
  2711. String lo_str = {};
  2712. String hi_str = {};
  2713. if (bt->Enum.fields.count > 0) {
  2714. lo_str = bt->Enum.fields[bt->Enum.min_value_index]->token.string;
  2715. hi_str = bt->Enum.fields[bt->Enum.max_value_index]->token.string;
  2716. }
  2717. bool out_of_bounds = false;
  2718. if (compare_exact_values(Token_Lt, operand.value, lo) || compare_exact_values(Token_Gt, operand.value, hi)) {
  2719. out_of_bounds = true;
  2720. }
  2721. if (out_of_bounds) {
  2722. gbString expr_str = expr_to_string(operand.expr);
  2723. if (lo_str.len > 0) {
  2724. error(operand.expr, "Index '%s' is out of bounds range %.*s .. %.*s", expr_str, LIT(lo_str), LIT(hi_str));
  2725. } else {
  2726. gbString index_type_str = type_to_string(index_type);
  2727. error(operand.expr, "Index '%s' is out of bounds range of enum type %s", expr_str, index_type_str);
  2728. gb_string_free(index_type_str);
  2729. }
  2730. gb_string_free(expr_str);
  2731. return false;
  2732. }
  2733. if (value) *value = exact_value_to_i64(exact_value_sub(operand.value, lo));
  2734. return true;
  2735. } else { // NOTE(bill): Do array bound checking
  2736. i64 v = -1;
  2737. if (i.len <= 1) {
  2738. v = big_int_to_i64(&i);
  2739. }
  2740. if (value) *value = v;
  2741. bool out_of_bounds = false;
  2742. if (v < 0) {
  2743. out_of_bounds = true;
  2744. } else if (open_range) {
  2745. out_of_bounds = v > max_count;
  2746. } else {
  2747. out_of_bounds = v >= max_count;
  2748. }
  2749. if (out_of_bounds) {
  2750. gbString expr_str = expr_to_string(operand.expr);
  2751. error(operand.expr, "Index '%s' is out of bounds range 0..<%lld", expr_str, max_count);
  2752. gb_string_free(expr_str);
  2753. return false;
  2754. }
  2755. return true;
  2756. }
  2757. } else {
  2758. if (value) *value = exact_value_to_i64(operand.value);
  2759. return true;
  2760. }
  2761. }
  2762. // NOTE(bill): It's alright :D
  2763. if (value) *value = -1;
  2764. return true;
  2765. }
  2766. ExactValue get_constant_field_single(CheckerContext *c, ExactValue value, i32 index, bool *success_, bool *finish_) {
  2767. if (value.kind == ExactValue_String) {
  2768. GB_ASSERT(0 <= index && index < value.value_string.len);
  2769. u8 val = value.value_string[index];
  2770. if (success_) *success_ = true;
  2771. if (finish_) *finish_ = true;
  2772. return exact_value_u64(val);
  2773. }
  2774. if (value.kind != ExactValue_Compound) {
  2775. if (success_) *success_ = true;
  2776. if (finish_) *finish_ = true;
  2777. return value;
  2778. }
  2779. Ast *node = value.value_compound;
  2780. switch (node->kind) {
  2781. case_ast_node(cl, CompoundLit, node);
  2782. if (cl->elems.count == 0) {
  2783. if (success_) *success_ = true;
  2784. if (finish_) *finish_ = true;
  2785. return empty_exact_value;
  2786. }
  2787. if (cl->elems[0]->kind == Ast_FieldValue) {
  2788. if (is_type_struct(node->tav.type)) {
  2789. for_array(i, cl->elems) {
  2790. Ast *elem = cl->elems[i];
  2791. if (elem->kind != Ast_FieldValue) {
  2792. continue;
  2793. }
  2794. ast_node(fv, FieldValue, elem);
  2795. String name = fv->field->Ident.token.string;
  2796. Selection sub_sel = lookup_field(node->tav.type, name, false);
  2797. defer (array_free(&sub_sel.index));
  2798. if (sub_sel.index[0] == index) {
  2799. value = fv->value->tav.value;
  2800. break;
  2801. }
  2802. }
  2803. } else if (is_type_array(node->tav.type) || is_type_enumerated_array(node->tav.type)) {
  2804. for_array(i, cl->elems) {
  2805. Ast *elem = cl->elems[i];
  2806. if (elem->kind != Ast_FieldValue) {
  2807. continue;
  2808. }
  2809. ast_node(fv, FieldValue, elem);
  2810. if (is_ast_range(fv->field)) {
  2811. ast_node(ie, BinaryExpr, fv->field);
  2812. TypeAndValue lo_tav = ie->left->tav;
  2813. TypeAndValue hi_tav = ie->right->tav;
  2814. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  2815. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  2816. TokenKind op = ie->op.kind;
  2817. i64 lo = exact_value_to_i64(lo_tav.value);
  2818. i64 hi = exact_value_to_i64(hi_tav.value);
  2819. i64 corrected_index = index;
  2820. if (is_type_enumerated_array(node->tav.type)) {
  2821. Type *bt = base_type(node->tav.type);
  2822. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  2823. corrected_index = index + exact_value_to_i64(bt->EnumeratedArray.min_value);
  2824. }
  2825. if (op == Token_Ellipsis) {
  2826. if (lo <= corrected_index && corrected_index <= hi) {
  2827. TypeAndValue tav = fv->value->tav;
  2828. if (success_) *success_ = true;
  2829. if (finish_) *finish_ = false;
  2830. return tav.value;
  2831. }
  2832. } else {
  2833. if (lo <= corrected_index && corrected_index < hi) {
  2834. TypeAndValue tav = fv->value->tav;
  2835. if (success_) *success_ = true;
  2836. if (finish_) *finish_ = false;
  2837. return tav.value;
  2838. }
  2839. }
  2840. } else {
  2841. TypeAndValue index_tav = fv->field->tav;
  2842. GB_ASSERT(index_tav.mode == Addressing_Constant);
  2843. ExactValue index_value = index_tav.value;
  2844. if (is_type_enumerated_array(node->tav.type)) {
  2845. Type *bt = base_type(node->tav.type);
  2846. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  2847. index_value = exact_value_sub(index_value, bt->EnumeratedArray.min_value);
  2848. }
  2849. i64 field_index = exact_value_to_i64(index_value);
  2850. if (index == field_index) {
  2851. TypeAndValue tav = fv->value->tav;
  2852. if (success_) *success_ = true;
  2853. if (finish_) *finish_ = false;
  2854. return tav.value;;
  2855. }
  2856. }
  2857. }
  2858. }
  2859. } else {
  2860. i32 count = (i32)cl->elems.count;
  2861. if (count < index) {
  2862. if (success_) *success_ = false;
  2863. if (finish_) *finish_ = true;
  2864. return empty_exact_value;
  2865. }
  2866. if (cl->elems.count <= index) {
  2867. if (success_) *success_ = false;
  2868. if (finish_) *finish_ = false;
  2869. return value;
  2870. }
  2871. TypeAndValue tav = cl->elems[index]->tav;
  2872. if (tav.mode == Addressing_Constant) {
  2873. if (success_) *success_ = true;
  2874. if (finish_) *finish_ = false;
  2875. return tav.value;
  2876. } else {
  2877. GB_ASSERT(is_type_untyped_nil(tav.type));
  2878. if (success_) *success_ = true;
  2879. if (finish_) *finish_ = false;
  2880. return tav.value;
  2881. }
  2882. }
  2883. case_end;
  2884. default:
  2885. // TODO(bill): Should this be a general fallback?
  2886. if (success_) *success_ = true;
  2887. if (finish_) *finish_ = true;
  2888. return empty_exact_value;
  2889. }
  2890. if (finish_) *finish_ = false;
  2891. return value;
  2892. }
  2893. ExactValue get_constant_field(CheckerContext *c, Operand const *operand, Selection sel, bool *success_) {
  2894. if (operand->mode != Addressing_Constant) {
  2895. if (success_) *success_ = false;
  2896. return empty_exact_value;
  2897. }
  2898. if (sel.indirect) {
  2899. if (success_) *success_ = false;
  2900. return empty_exact_value;
  2901. }
  2902. if (sel.index.count == 0) {
  2903. if (success_) *success_ = false;
  2904. return empty_exact_value;
  2905. }
  2906. ExactValue value = operand->value;
  2907. if (value.kind == ExactValue_Compound) {
  2908. i32 depth = 0;
  2909. while (sel.index.count > 0) {
  2910. i32 index = sel.index[0];
  2911. sel = sub_selection(sel, 1);
  2912. bool finish = false;
  2913. value = get_constant_field_single(c, value, index, success_, &finish);
  2914. if (finish) {
  2915. return value;
  2916. }
  2917. }
  2918. if (success_) *success_ = true;
  2919. return value;
  2920. } else if (value.kind == ExactValue_Quaternion) {
  2921. // @QuaternionLayout
  2922. Quaternion256 q = *value.value_quaternion;
  2923. GB_ASSERT(sel.index.count == 1);
  2924. switch (sel.index[0]) {
  2925. case 3: // w
  2926. if (success_) *success_ = true;
  2927. return exact_value_float(q.real);
  2928. case 0: // x
  2929. if (success_) *success_ = true;
  2930. return exact_value_float(q.imag);
  2931. case 1: // y
  2932. if (success_) *success_ = true;
  2933. return exact_value_float(q.jmag);
  2934. case 2: // z
  2935. if (success_) *success_ = true;
  2936. return exact_value_float(q.kmag);
  2937. }
  2938. if (success_) *success_ = false;
  2939. return empty_exact_value;
  2940. } else if (value.kind == ExactValue_Complex) {
  2941. // @QuaternionLayout
  2942. Complex128 c = *value.value_complex;
  2943. GB_ASSERT(sel.index.count == 1);
  2944. switch (sel.index[0]) {
  2945. case 0: // real
  2946. if (success_) *success_ = true;
  2947. return exact_value_float(c.real);
  2948. case 1: // imag
  2949. if (success_) *success_ = true;
  2950. return exact_value_float(c.imag);
  2951. }
  2952. if (success_) *success_ = false;
  2953. return empty_exact_value;
  2954. }
  2955. if (success_) *success_ = true;
  2956. return empty_exact_value;
  2957. }
  2958. Entity *check_selector(CheckerContext *c, Operand *operand, Ast *node, Type *type_hint) {
  2959. ast_node(se, SelectorExpr, node);
  2960. bool check_op_expr = true;
  2961. Entity *expr_entity = nullptr;
  2962. Entity *entity = nullptr;
  2963. Selection sel = {}; // NOTE(bill): Not used if it's an import name
  2964. if (!c->allow_arrow_right_selector_expr && se->token.kind == Token_ArrowRight) {
  2965. error(node, "Illegal use of -> selector shorthand outside of a call");
  2966. operand->mode = Addressing_Invalid;
  2967. operand->expr = node;
  2968. return nullptr;
  2969. }
  2970. operand->expr = node;
  2971. Ast *op_expr = se->expr;
  2972. Ast *selector = unparen_expr(se->selector);
  2973. if (selector == nullptr) {
  2974. operand->mode = Addressing_Invalid;
  2975. operand->expr = node;
  2976. return nullptr;
  2977. }
  2978. if (selector->kind != Ast_Ident) {
  2979. error(selector, "Illegal selector kind: '%.*s'", LIT(ast_strings[selector->kind]));
  2980. operand->mode = Addressing_Invalid;
  2981. operand->expr = node;
  2982. return nullptr;
  2983. }
  2984. if (op_expr->kind == Ast_Ident) {
  2985. String op_name = op_expr->Ident.token.string;
  2986. Entity *e = scope_lookup(c->scope, op_name);
  2987. add_entity_use(c, op_expr, e);
  2988. expr_entity = e;
  2989. Entity *original_e = e;
  2990. if (e != nullptr && e->kind == Entity_ImportName && selector->kind == Ast_Ident) {
  2991. // IMPORTANT NOTE(bill): This is very sloppy code but it's also very fragile
  2992. // It pretty much needs to be in this order and this way
  2993. // If you can clean this up, please do but be really careful
  2994. String import_name = op_name;
  2995. Scope *import_scope = e->ImportName.scope;
  2996. String entity_name = selector->Ident.token.string;
  2997. check_op_expr = false;
  2998. entity = scope_lookup_current(import_scope, entity_name);
  2999. bool is_declared = entity != nullptr;
  3000. bool allow_builtin = false;
  3001. if (is_declared) {
  3002. if (entity->kind == Entity_Builtin) {
  3003. // NOTE(bill): Builtin's are in the universal scope which is part of every scopes hierarchy
  3004. // This means that we should just ignore the found result through it
  3005. allow_builtin = entity->scope == import_scope;
  3006. } else if ((entity->scope->flags&ScopeFlag_Global) == ScopeFlag_Global && (import_scope->flags&ScopeFlag_Global) == 0) {
  3007. is_declared = false;
  3008. }
  3009. }
  3010. if (!is_declared) {
  3011. error(op_expr, "'%.*s' is not declared by '%.*s'", LIT(entity_name), LIT(import_name));
  3012. operand->mode = Addressing_Invalid;
  3013. operand->expr = node;
  3014. return nullptr;
  3015. }
  3016. check_entity_decl(c, entity, nullptr, nullptr);
  3017. if (entity->kind == Entity_ProcGroup) {
  3018. operand->mode = Addressing_ProcGroup;
  3019. operand->proc_group = entity;
  3020. add_type_and_value(c->info, operand->expr, operand->mode, operand->type, operand->value);
  3021. return entity;
  3022. }
  3023. GB_ASSERT_MSG(entity->type != nullptr, "%.*s (%.*s)", LIT(entity->token.string), LIT(entity_strings[entity->kind]));
  3024. if (!is_entity_exported(entity, allow_builtin)) {
  3025. gbString sel_str = expr_to_string(selector);
  3026. error(op_expr, "'%s' is not exported by '%.*s'", sel_str, LIT(import_name));
  3027. gb_string_free(sel_str);
  3028. // NOTE(bill): make the state valid still, even if it's "invalid"
  3029. // operand->mode = Addressing_Invalid;
  3030. // operand->expr = node;
  3031. // return nullptr;
  3032. }
  3033. if (entity->kind == Entity_ProcGroup) {
  3034. Array<Entity *> procs = entity->ProcGroup.entities;
  3035. bool skip = false;
  3036. for_array(i, procs) {
  3037. Entity *p = procs[i];
  3038. Type *t = base_type(p->type);
  3039. if (t == t_invalid) {
  3040. continue;
  3041. }
  3042. Operand x = {};
  3043. x.mode = Addressing_Value;
  3044. x.type = t;
  3045. if (type_hint != nullptr) {
  3046. if (check_is_assignable_to(c, &x, type_hint)) {
  3047. entity = p;
  3048. skip = true;
  3049. break;
  3050. }
  3051. }
  3052. }
  3053. if (!skip) {
  3054. GB_ASSERT(entity != nullptr);
  3055. operand->mode = Addressing_ProcGroup;
  3056. operand->type = t_invalid;
  3057. operand->expr = node;
  3058. operand->proc_group = entity;
  3059. return entity;
  3060. }
  3061. }
  3062. }
  3063. }
  3064. if (check_op_expr) {
  3065. check_expr_base(c, operand, op_expr, nullptr);
  3066. if (operand->mode == Addressing_Invalid) {
  3067. operand->mode = Addressing_Invalid;
  3068. operand->expr = node;
  3069. return nullptr;
  3070. }
  3071. }
  3072. if (entity == nullptr && selector->kind == Ast_Ident) {
  3073. String field_name = selector->Ident.token.string;
  3074. if (is_type_dynamic_array(type_deref(operand->type))) {
  3075. init_mem_allocator(c->checker);
  3076. }
  3077. sel = lookup_field(operand->type, field_name, operand->mode == Addressing_Type);
  3078. entity = sel.entity;
  3079. // NOTE(bill): Add type info needed for fields like 'names'
  3080. if (entity != nullptr && (entity->flags&EntityFlag_TypeField)) {
  3081. add_type_info_type(c, operand->type);
  3082. }
  3083. if (is_type_enum(operand->type)) {
  3084. add_type_info_type(c, operand->type);
  3085. }
  3086. }
  3087. if (entity == nullptr) {
  3088. gbString op_str = expr_to_string(op_expr);
  3089. gbString type_str = type_to_string(operand->type);
  3090. gbString sel_str = expr_to_string(selector);
  3091. error(op_expr, "'%s' of type '%s' has no field '%s'", op_str, type_str, sel_str);
  3092. gb_string_free(sel_str);
  3093. gb_string_free(type_str);
  3094. gb_string_free(op_str);
  3095. operand->mode = Addressing_Invalid;
  3096. operand->expr = node;
  3097. return nullptr;
  3098. }
  3099. if (expr_entity != nullptr && expr_entity->kind == Entity_Constant && entity->kind != Entity_Constant) {
  3100. bool success = false;
  3101. ExactValue field_value = get_constant_field(c, operand, sel, &success);
  3102. if (success) {
  3103. operand->mode = Addressing_Constant;
  3104. operand->expr = node;
  3105. operand->value = field_value;
  3106. operand->type = entity->type;
  3107. add_entity_use(c, selector, entity);
  3108. add_type_and_value(c->info, operand->expr, operand->mode, operand->type, operand->value);
  3109. return entity;
  3110. }
  3111. gbString op_str = expr_to_string(op_expr);
  3112. gbString type_str = type_to_string(operand->type);
  3113. gbString sel_str = expr_to_string(selector);
  3114. error(op_expr, "Cannot access non-constant field '%s' from '%s'", sel_str, op_str);
  3115. gb_string_free(sel_str);
  3116. gb_string_free(type_str);
  3117. gb_string_free(op_str);
  3118. operand->mode = Addressing_Invalid;
  3119. operand->expr = node;
  3120. return nullptr;
  3121. }
  3122. if (operand->mode == Addressing_Constant && entity->kind != Entity_Constant) {
  3123. bool success = false;
  3124. ExactValue field_value = get_constant_field(c, operand, sel, &success);
  3125. if (success) {
  3126. operand->mode = Addressing_Constant;
  3127. operand->expr = node;
  3128. operand->value = field_value;
  3129. operand->type = entity->type;
  3130. add_entity_use(c, selector, entity);
  3131. add_type_and_value(c->info, operand->expr, operand->mode, operand->type, operand->value);
  3132. return entity;
  3133. }
  3134. gbString op_str = expr_to_string(op_expr);
  3135. gbString type_str = type_to_string(operand->type);
  3136. gbString sel_str = expr_to_string(selector);
  3137. error(op_expr, "Cannot access non-constant field '%s' from '%s'", sel_str, op_str);
  3138. gb_string_free(sel_str);
  3139. gb_string_free(type_str);
  3140. gb_string_free(op_str);
  3141. operand->mode = Addressing_Invalid;
  3142. operand->expr = node;
  3143. return nullptr;
  3144. }
  3145. if (expr_entity != nullptr && is_type_polymorphic(expr_entity->type)) {
  3146. gbString op_str = expr_to_string(op_expr);
  3147. gbString type_str = type_to_string(operand->type);
  3148. gbString sel_str = expr_to_string(selector);
  3149. error(op_expr, "Cannot access field '%s' from non-specialized polymorphic type '%s'", sel_str, op_str);
  3150. gb_string_free(sel_str);
  3151. gb_string_free(type_str);
  3152. gb_string_free(op_str);
  3153. operand->mode = Addressing_Invalid;
  3154. operand->expr = node;
  3155. return nullptr;
  3156. }
  3157. add_entity_use(c, selector, entity);
  3158. operand->type = entity->type;
  3159. operand->expr = node;
  3160. switch (entity->kind) {
  3161. case Entity_Constant:
  3162. operand->value = entity->Constant.value;
  3163. operand->mode = Addressing_Constant;
  3164. if (operand->value.kind == ExactValue_Procedure) {
  3165. Entity *proc = strip_entity_wrapping(operand->value.value_procedure);
  3166. if (proc != nullptr) {
  3167. operand->mode = Addressing_Value;
  3168. operand->type = proc->type;
  3169. }
  3170. }
  3171. break;
  3172. case Entity_Variable:
  3173. if (sel.indirect) {
  3174. operand->mode = Addressing_Variable;
  3175. } else if (operand->mode == Addressing_Context) {
  3176. // Do nothing
  3177. } else if (operand->mode == Addressing_MapIndex) {
  3178. operand->mode = Addressing_Value;
  3179. } else if (entity->flags & EntityFlag_SoaPtrField) {
  3180. operand->mode = Addressing_SoaVariable;
  3181. } else if (operand->mode == Addressing_OptionalOk) {
  3182. operand->mode = Addressing_Value;
  3183. } else if (operand->mode == Addressing_SoaVariable) {
  3184. operand->mode = Addressing_Variable;
  3185. } else if (operand->mode != Addressing_Value) {
  3186. operand->mode = Addressing_Variable;
  3187. } else {
  3188. operand->mode = Addressing_Value;
  3189. }
  3190. break;
  3191. case Entity_TypeName:
  3192. operand->mode = Addressing_Type;
  3193. break;
  3194. case Entity_Procedure:
  3195. operand->mode = Addressing_Value;
  3196. operand->value = exact_value_procedure(node);
  3197. break;
  3198. case Entity_Builtin:
  3199. operand->mode = Addressing_Builtin;
  3200. operand->builtin_id = cast(BuiltinProcId)entity->Builtin.id;
  3201. break;
  3202. case Entity_ProcGroup:
  3203. operand->mode = Addressing_ProcGroup;
  3204. operand->proc_group = entity;
  3205. break;
  3206. // NOTE(bill): These cases should never be hit but are here for sanity reasons
  3207. case Entity_Nil:
  3208. operand->mode = Addressing_Value;
  3209. break;
  3210. }
  3211. add_type_and_value(c->info, operand->expr, operand->mode, operand->type, operand->value);
  3212. return entity;
  3213. }
  3214. bool is_type_normal_pointer(Type *ptr, Type **elem) {
  3215. ptr = base_type(ptr);
  3216. if (is_type_pointer(ptr)) {
  3217. if (is_type_rawptr(ptr)) {
  3218. return false;
  3219. }
  3220. if (elem) *elem = ptr->Pointer.elem;
  3221. return true;
  3222. }
  3223. return false;
  3224. }
  3225. bool check_identifier_exists(Scope *s, Ast *node, bool nested = false, Scope **out_scope = nullptr) {
  3226. switch (node->kind) {
  3227. case_ast_node(i, Ident, node);
  3228. String name = i->token.string;
  3229. if (nested) {
  3230. Entity *e = scope_lookup_current(s, name);
  3231. if (e != nullptr) {
  3232. if (out_scope) *out_scope = e->scope;
  3233. return true;
  3234. }
  3235. } else {
  3236. Entity *e = scope_lookup(s, name);
  3237. if (e != nullptr) {
  3238. if (out_scope) *out_scope = e->scope;
  3239. return true;
  3240. }
  3241. }
  3242. case_end;
  3243. case_ast_node(se, SelectorExpr, node);
  3244. Ast *lhs = se->expr;
  3245. Ast *rhs = se->selector;
  3246. Scope *lhs_scope = nullptr;
  3247. if (check_identifier_exists(s, lhs, nested, &lhs_scope)) {
  3248. return check_identifier_exists(lhs_scope, rhs, true);
  3249. }
  3250. case_end;
  3251. }
  3252. return false;
  3253. }
  3254. typedef bool (BuiltinTypeIsProc)(Type *t);
  3255. BuiltinTypeIsProc *builtin_type_is_procs[BuiltinProc__type_simple_boolean_end - BuiltinProc__type_simple_boolean_begin] = {
  3256. nullptr, // BuiltinProc__type_simple_boolean_begin
  3257. is_type_boolean,
  3258. is_type_integer,
  3259. is_type_rune,
  3260. is_type_float,
  3261. is_type_complex,
  3262. is_type_quaternion,
  3263. is_type_string,
  3264. is_type_typeid,
  3265. is_type_any,
  3266. is_type_endian_little,
  3267. is_type_endian_big,
  3268. is_type_unsigned,
  3269. is_type_numeric,
  3270. is_type_ordered,
  3271. is_type_ordered_numeric,
  3272. is_type_indexable,
  3273. is_type_sliceable,
  3274. is_type_comparable,
  3275. is_type_simple_compare,
  3276. is_type_dereferenceable,
  3277. is_type_valid_for_keys,
  3278. is_type_named,
  3279. is_type_pointer,
  3280. is_type_array,
  3281. is_type_enumerated_array,
  3282. is_type_slice,
  3283. is_type_dynamic_array,
  3284. is_type_map,
  3285. is_type_struct,
  3286. is_type_union,
  3287. is_type_enum,
  3288. is_type_proc,
  3289. is_type_bit_set,
  3290. is_type_simd_vector,
  3291. is_type_polymorphic_record_specialized,
  3292. is_type_polymorphic_record_unspecialized,
  3293. type_has_nil,
  3294. };
  3295. bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
  3296. ast_node(ce, CallExpr, call);
  3297. if (ce->inlining != ProcInlining_none) {
  3298. error(call, "Inlining operators are not allowed on built-in procedures");
  3299. }
  3300. BuiltinProc *bp = &builtin_procs[id];
  3301. {
  3302. char const *err = nullptr;
  3303. if (ce->args.count < bp->arg_count) {
  3304. err = "Too few";
  3305. } else if (ce->args.count > bp->arg_count && !bp->variadic) {
  3306. err = "Too many";
  3307. }
  3308. if (err != nullptr) {
  3309. gbString expr = expr_to_string(ce->proc);
  3310. error(ce->close, "%s arguments for '%s', expected %td, got %td",
  3311. err, expr,
  3312. bp->arg_count, ce->args.count);
  3313. gb_string_free(expr);
  3314. return false;
  3315. }
  3316. }
  3317. switch (id) {
  3318. case BuiltinProc_size_of:
  3319. case BuiltinProc_align_of:
  3320. case BuiltinProc_offset_of:
  3321. case BuiltinProc_type_info_of:
  3322. case BuiltinProc_typeid_of:
  3323. case BuiltinProc_len:
  3324. case BuiltinProc_min:
  3325. case BuiltinProc_max:
  3326. // NOTE(bill): The first arg may be a Type, this will be checked case by case
  3327. break;
  3328. case BuiltinProc_DIRECTIVE: {
  3329. ast_node(bd, BasicDirective, ce->proc);
  3330. String name = bd->name;
  3331. if (name == "defined") {
  3332. break;
  3333. }
  3334. if (name == "config") {
  3335. break;
  3336. }
  3337. /*fallthrough*/
  3338. }
  3339. default:
  3340. if (BuiltinProc__type_begin < id && id < BuiltinProc__type_end) {
  3341. check_expr_or_type(c, operand, ce->args[0]);
  3342. } else if (ce->args.count > 0) {
  3343. check_multi_expr(c, operand, ce->args[0]);
  3344. }
  3345. break;
  3346. }
  3347. String builtin_name = builtin_procs[id].name;
  3348. if (ce->args.count > 0) {
  3349. if (ce->args[0]->kind == Ast_FieldValue) {
  3350. error(call, "'field = value' calling is not allowed on built-in procedures");
  3351. return false;
  3352. }
  3353. }
  3354. switch (id) {
  3355. default:
  3356. GB_PANIC("Implement built-in procedure: %.*s", LIT(builtin_name));
  3357. break;
  3358. case BuiltinProc_DIRECTIVE: {
  3359. ast_node(bd, BasicDirective, ce->proc);
  3360. String name = bd->name;
  3361. if (name == "location") {
  3362. if (ce->args.count > 1) {
  3363. error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count);
  3364. }
  3365. if (ce->args.count > 0) {
  3366. Ast *arg = ce->args[0];
  3367. Entity *e = nullptr;
  3368. Operand o = {};
  3369. if (arg->kind == Ast_Ident) {
  3370. e = check_ident(c, &o, arg, nullptr, nullptr, true);
  3371. } else if (arg->kind == Ast_SelectorExpr) {
  3372. e = check_selector(c, &o, arg, nullptr);
  3373. }
  3374. if (e == nullptr) {
  3375. error(ce->args[0], "'#location' expected a valid entity name");
  3376. }
  3377. }
  3378. operand->type = t_source_code_location;
  3379. operand->mode = Addressing_Value;
  3380. } else if (name == "load") {
  3381. if (ce->args.count != 1) {
  3382. error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count);
  3383. return false;
  3384. }
  3385. Ast *arg = ce->args[0];
  3386. Operand o = {};
  3387. check_expr(c, &o, arg);
  3388. if (o.mode != Addressing_Constant) {
  3389. error(arg, "'#load' expected a constant string argument");
  3390. return false;
  3391. }
  3392. if (!is_type_string(o.type)) {
  3393. gbString str = type_to_string(o.type);
  3394. error(arg, "'#load' expected a constant string, got %s", str);
  3395. gb_string_free(str);
  3396. return false;
  3397. }
  3398. gbAllocator a = heap_allocator();
  3399. GB_ASSERT(o.value.kind == ExactValue_String);
  3400. String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id));
  3401. String original_string = o.value.value_string;
  3402. gbMutex *ignore_mutex = nullptr;
  3403. String path = {};
  3404. bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
  3405. char *c_str = alloc_cstring(a, path);
  3406. defer (gb_free(a, c_str));
  3407. gbFile f = {};
  3408. gbFileError file_err = gb_file_open(&f, c_str);
  3409. defer (gb_file_close(&f));
  3410. switch (file_err) {
  3411. default:
  3412. case gbFileError_Invalid:
  3413. error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str);
  3414. return false;
  3415. case gbFileError_NotExists:
  3416. error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str);
  3417. return false;
  3418. case gbFileError_Permission:
  3419. error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str);
  3420. return false;
  3421. case gbFileError_None:
  3422. // Okay
  3423. break;
  3424. }
  3425. String result = {};
  3426. isize file_size = cast(isize)gb_file_size(&f);
  3427. if (file_size > 0) {
  3428. u8 *data = cast(u8 *)gb_alloc(a, file_size+1);
  3429. gb_file_read_at(&f, data, file_size, 0);
  3430. data[file_size] = '\0';
  3431. result.text = data;
  3432. result.len = file_size;
  3433. }
  3434. operand->type = t_u8_slice;
  3435. operand->mode = Addressing_Constant;
  3436. operand->value = exact_value_string(result);
  3437. } else if (name == "assert") {
  3438. if (ce->args.count != 1) {
  3439. error(call, "'#assert' expects 1 argument, got %td", ce->args.count);
  3440. return false;
  3441. }
  3442. if (!is_type_boolean(operand->type) || operand->mode != Addressing_Constant) {
  3443. gbString str = expr_to_string(ce->args[0]);
  3444. error(call, "'%s' is not a constant boolean", str);
  3445. gb_string_free(str);
  3446. return false;
  3447. }
  3448. if (!operand->value.value_bool) {
  3449. gbString arg = expr_to_string(ce->args[0]);
  3450. error(call, "Compile time assertion: %s", arg);
  3451. if (c->proc_name != "") {
  3452. gbString str = type_to_string(c->curr_proc_sig);
  3453. error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
  3454. gb_string_free(str);
  3455. }
  3456. gb_string_free(arg);
  3457. }
  3458. operand->type = t_untyped_bool;
  3459. operand->mode = Addressing_Constant;
  3460. } else if (name == "panic") {
  3461. if (ce->args.count != 1) {
  3462. error(call, "'#panic' expects 1 argument, got %td", ce->args.count);
  3463. return false;
  3464. }
  3465. if (!is_type_string(operand->type) && operand->mode != Addressing_Constant) {
  3466. gbString str = expr_to_string(ce->args[0]);
  3467. error(call, "'%s' is not a constant string", str);
  3468. gb_string_free(str);
  3469. return false;
  3470. }
  3471. error(call, "Compile time panic: %.*s", LIT(operand->value.value_string));
  3472. if (c->proc_name != "") {
  3473. gbString str = type_to_string(c->curr_proc_sig);
  3474. error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
  3475. gb_string_free(str);
  3476. }
  3477. operand->type = t_invalid;
  3478. operand->mode = Addressing_NoValue;
  3479. } else if (name == "defined") {
  3480. if (ce->args.count != 1) {
  3481. error(call, "'#defined' expects 1 argument, got %td", ce->args.count);
  3482. return false;
  3483. }
  3484. Ast *arg = unparen_expr(ce->args[0]);
  3485. if (arg == nullptr || (arg->kind != Ast_Ident && arg->kind != Ast_SelectorExpr)) {
  3486. error(call, "'#defined' expects an identifier or selector expression, got %.*s", LIT(ast_strings[arg->kind]));
  3487. return false;
  3488. }
  3489. if (c->curr_proc_decl == nullptr) {
  3490. error(call, "'#defined' is only allowed within a procedure, prefer the replacement '#config(NAME, default_value)'");
  3491. return false;
  3492. }
  3493. bool is_defined = check_identifier_exists(c->scope, arg);
  3494. operand->type = t_untyped_bool;
  3495. operand->mode = Addressing_Constant;
  3496. operand->value = exact_value_bool(false);
  3497. } else if (name == "config") {
  3498. if (ce->args.count != 2) {
  3499. error(call, "'#config' expects 2 argument, got %td", ce->args.count);
  3500. return false;
  3501. }
  3502. Ast *arg = unparen_expr(ce->args[0]);
  3503. if (arg == nullptr || arg->kind != Ast_Ident) {
  3504. error(call, "'#config' expects an identifier, got %.*s", LIT(ast_strings[arg->kind]));
  3505. return false;
  3506. }
  3507. Ast *def_arg = unparen_expr(ce->args[1]);
  3508. Operand def = {};
  3509. check_expr(c, &def, def_arg);
  3510. if (def.mode != Addressing_Constant) {
  3511. error(def_arg, "'#config' default value must be a constant");
  3512. return false;
  3513. }
  3514. String name = arg->Ident.token.string;
  3515. operand->type = def.type;
  3516. operand->mode = def.mode;
  3517. operand->value = def.value;
  3518. Entity *found = scope_lookup_current(config_pkg->scope, name);
  3519. if (found != nullptr) {
  3520. if (found->kind != Entity_Constant) {
  3521. error(arg, "'#config' entity '%.*s' found but expected a constant", LIT(name));
  3522. } else {
  3523. operand->type = found->type;
  3524. operand->mode = Addressing_Constant;
  3525. operand->value = found->Constant.value;
  3526. }
  3527. }
  3528. } else {
  3529. GB_PANIC("Unhandled #%.*s", LIT(name));
  3530. }
  3531. break;
  3532. }
  3533. case BuiltinProc_len:
  3534. check_expr_or_type(c, operand, ce->args[0]);
  3535. if (operand->mode == Addressing_Invalid) {
  3536. return false;
  3537. }
  3538. /* fallthrough */
  3539. case BuiltinProc_cap:
  3540. {
  3541. // len :: proc(Type) -> int
  3542. // cap :: proc(Type) -> int
  3543. Type *op_type = type_deref(operand->type);
  3544. Type *type = t_int;
  3545. AddressingMode mode = Addressing_Invalid;
  3546. ExactValue value = {};
  3547. if (is_type_string(op_type) && id == BuiltinProc_len) {
  3548. if (operand->mode == Addressing_Constant) {
  3549. mode = Addressing_Constant;
  3550. String str = operand->value.value_string;
  3551. value = exact_value_i64(str.len);
  3552. type = t_untyped_integer;
  3553. } else {
  3554. mode = Addressing_Value;
  3555. if (is_type_cstring(op_type)) {
  3556. add_package_dependency(c, "runtime", "cstring_len");
  3557. }
  3558. }
  3559. } else if (is_type_array(op_type)) {
  3560. Type *at = core_type(op_type);
  3561. mode = Addressing_Constant;
  3562. value = exact_value_i64(at->Array.count);
  3563. type = t_untyped_integer;
  3564. } else if (is_type_enumerated_array(op_type) && id == BuiltinProc_len) {
  3565. Type *at = core_type(op_type);
  3566. mode = Addressing_Constant;
  3567. value = exact_value_i64(at->EnumeratedArray.count);
  3568. type = t_untyped_integer;
  3569. } else if (is_type_slice(op_type) && id == BuiltinProc_len) {
  3570. mode = Addressing_Value;
  3571. } else if (is_type_dynamic_array(op_type)) {
  3572. mode = Addressing_Value;
  3573. } else if (is_type_map(op_type)) {
  3574. mode = Addressing_Value;
  3575. } else if (operand->mode == Addressing_Type && is_type_enum(op_type) && id == BuiltinProc_len) {
  3576. Type *bt = base_type(op_type);
  3577. mode = Addressing_Constant;
  3578. value = exact_value_i64(bt->Enum.fields.count);
  3579. type = t_untyped_integer;
  3580. } else if (is_type_struct(op_type)) {
  3581. Type *bt = base_type(op_type);
  3582. if (bt->Struct.soa_kind == StructSoa_Fixed) {
  3583. mode = Addressing_Constant;
  3584. value = exact_value_i64(bt->Struct.soa_count);
  3585. type = t_untyped_integer;
  3586. } else if ((bt->Struct.soa_kind == StructSoa_Slice && id == BuiltinProc_len) ||
  3587. bt->Struct.soa_kind == StructSoa_Dynamic) {
  3588. mode = Addressing_Value;
  3589. }
  3590. }
  3591. if (operand->mode == Addressing_Type && mode != Addressing_Constant) {
  3592. mode = Addressing_Invalid;
  3593. }
  3594. if (mode == Addressing_Invalid) {
  3595. gbString t = type_to_string(operand->type);
  3596. error(call, "'%.*s' is not supported for '%s'", LIT(builtin_name), t);
  3597. return false;
  3598. }
  3599. operand->mode = mode;
  3600. operand->value = value;
  3601. operand->type = type;
  3602. break;
  3603. }
  3604. case BuiltinProc_size_of: {
  3605. // size_of :: proc(Type or expr) -> untyped int
  3606. Operand o = {};
  3607. check_expr_or_type(c, &o, ce->args[0]);
  3608. if (o.mode == Addressing_Invalid) {
  3609. return false;
  3610. }
  3611. Type *t = o.type;
  3612. if (t == nullptr || t == t_invalid) {
  3613. error(ce->args[0], "Invalid argument for 'size_of'");
  3614. return false;
  3615. }
  3616. t = default_type(t);
  3617. operand->mode = Addressing_Constant;
  3618. operand->value = exact_value_i64(type_size_of(t));
  3619. operand->type = t_untyped_integer;
  3620. break;
  3621. }
  3622. case BuiltinProc_align_of: {
  3623. // align_of :: proc(Type or expr) -> untyped int
  3624. Operand o = {};
  3625. check_expr_or_type(c, &o, ce->args[0]);
  3626. if (o.mode == Addressing_Invalid) {
  3627. return false;
  3628. }
  3629. Type *t = o.type;
  3630. if (t == nullptr || t == t_invalid) {
  3631. error(ce->args[0], "Invalid argument for 'align_of'");
  3632. return false;
  3633. }
  3634. t = default_type(t);
  3635. operand->mode = Addressing_Constant;
  3636. operand->value = exact_value_i64(type_align_of(t));
  3637. operand->type = t_untyped_integer;
  3638. break;
  3639. }
  3640. case BuiltinProc_offset_of: {
  3641. // offset_of :: proc(Type, field) -> uintptr
  3642. Operand op = {};
  3643. Type *bt = check_type(c, ce->args[0]);
  3644. Type *type = base_type(bt);
  3645. if (type == nullptr || type == t_invalid) {
  3646. error(ce->args[0], "Expected a type for 'offset_of'");
  3647. return false;
  3648. }
  3649. Ast *field_arg = unparen_expr(ce->args[1]);
  3650. if (field_arg == nullptr ||
  3651. field_arg->kind != Ast_Ident) {
  3652. error(field_arg, "Expected an identifier for field argument");
  3653. return false;
  3654. }
  3655. if (is_type_array(type)) {
  3656. error(field_arg, "Invalid type for 'offset_of'");
  3657. return false;
  3658. }
  3659. ast_node(arg, Ident, field_arg);
  3660. Selection sel = lookup_field(type, arg->token.string, operand->mode == Addressing_Type);
  3661. if (sel.entity == nullptr) {
  3662. gbString type_str = type_to_string(bt);
  3663. error(ce->args[0],
  3664. "'%s' has no field named '%.*s'", type_str, LIT(arg->token.string));
  3665. gb_string_free(type_str);
  3666. return false;
  3667. }
  3668. if (sel.indirect) {
  3669. gbString type_str = type_to_string(bt);
  3670. error(ce->args[0],
  3671. "Field '%.*s' is embedded via a pointer in '%s'", LIT(arg->token.string), type_str);
  3672. gb_string_free(type_str);
  3673. return false;
  3674. }
  3675. operand->mode = Addressing_Constant;
  3676. operand->value = exact_value_i64(type_offset_of_from_selection(type, sel));
  3677. operand->type = t_uintptr;
  3678. break;
  3679. }
  3680. case BuiltinProc_type_of: {
  3681. // type_of :: proc(val: Type) -> type(Type)
  3682. Ast *expr = ce->args[0];
  3683. Operand o = {};
  3684. check_expr_or_type(c, &o, expr);
  3685. // check_assignment(c, operand, nullptr, str_lit("argument of 'type_of'"));
  3686. if (o.mode == Addressing_Invalid || o.mode == Addressing_Builtin) {
  3687. return false;
  3688. }
  3689. if (o.type == nullptr || o.type == t_invalid || is_type_asm_proc(o.type)) {
  3690. error(o.expr, "Invalid argument to 'type_of'");
  3691. return false;
  3692. }
  3693. // NOTE(bill): Prevent type cycles for procedure declarations
  3694. if (c->curr_proc_sig == o.type) {
  3695. gbString s = expr_to_string(o.expr);
  3696. error(o.expr, "Invalid cyclic type usage from 'type_of', got '%s'", s);
  3697. gb_string_free(s);
  3698. return false;
  3699. }
  3700. if (is_type_polymorphic(o.type)) {
  3701. error(o.expr, "'type_of' of polymorphic type cannot be determined");
  3702. return false;
  3703. }
  3704. operand->mode = Addressing_Type;
  3705. operand->type = o.type;
  3706. break;
  3707. }
  3708. case BuiltinProc_type_info_of: {
  3709. // type_info_of :: proc(Type) -> ^Type_Info
  3710. if (c->scope->flags&ScopeFlag_Global) {
  3711. compiler_error("'type_info_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
  3712. }
  3713. // NOTE(bill): The type information may not be setup yet
  3714. init_core_type_info(c->checker);
  3715. Ast *expr = ce->args[0];
  3716. Operand o = {};
  3717. check_expr_or_type(c, &o, expr);
  3718. if (o.mode == Addressing_Invalid) {
  3719. return false;
  3720. }
  3721. Type *t = o.type;
  3722. if (t == nullptr || t == t_invalid || is_type_asm_proc(o.type) || is_type_polymorphic(t)) {
  3723. if (is_type_polymorphic(t)) {
  3724. error(ce->args[0], "Invalid argument for 'type_info_of', unspecialized polymorphic type");
  3725. } else {
  3726. error(ce->args[0], "Invalid argument for 'type_info_of'");
  3727. }
  3728. return false;
  3729. }
  3730. t = default_type(t);
  3731. add_type_info_type(c, t);
  3732. if (is_operand_value(o) && is_type_typeid(t)) {
  3733. add_package_dependency(c, "runtime", "__type_info_of");
  3734. } else if (o.mode != Addressing_Type) {
  3735. error(expr, "Expected a type or typeid for 'type_info_of'");
  3736. return false;
  3737. }
  3738. operand->mode = Addressing_Value;
  3739. operand->type = t_type_info_ptr;
  3740. break;
  3741. }
  3742. case BuiltinProc_typeid_of: {
  3743. // typeid_of :: proc(Type) -> typeid
  3744. if (c->scope->flags&ScopeFlag_Global) {
  3745. compiler_error("'typeid_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
  3746. }
  3747. // NOTE(bill): The type information may not be setup yet
  3748. init_core_type_info(c->checker);
  3749. Ast *expr = ce->args[0];
  3750. Operand o = {};
  3751. check_expr_or_type(c, &o, expr);
  3752. if (o.mode == Addressing_Invalid) {
  3753. return false;
  3754. }
  3755. Type *t = o.type;
  3756. if (t == nullptr || t == t_invalid || is_type_asm_proc(o.type) || is_type_polymorphic(operand->type)) {
  3757. error(ce->args[0], "Invalid argument for 'typeid_of'");
  3758. return false;
  3759. }
  3760. t = default_type(t);
  3761. add_type_info_type(c, t);
  3762. if (o.mode != Addressing_Type) {
  3763. error(expr, "Expected a type for 'typeid_of'");
  3764. return false;
  3765. }
  3766. operand->mode = Addressing_Value;
  3767. operand->type = t_typeid;
  3768. operand->value = exact_value_typeid(t);
  3769. break;
  3770. }
  3771. case BuiltinProc_swizzle: {
  3772. // swizzle :: proc(v: [N]T, ..int) -> [M]T
  3773. Type *type = base_type(operand->type);
  3774. i64 max_count = 0;
  3775. Type *elem_type = nullptr;
  3776. if (!is_type_array(type) && !is_type_simd_vector(type)) {
  3777. gbString type_str = type_to_string(operand->type);
  3778. error(call,
  3779. "'swizzle' is only allowed on an array or #simd vector, got '%s'",
  3780. type_str);
  3781. gb_string_free(type_str);
  3782. return false;
  3783. }
  3784. if (type->kind == Type_Array) {
  3785. max_count = type->Array.count;
  3786. elem_type = type->Array.elem;
  3787. } else if (type->kind == Type_SimdVector) {
  3788. max_count = type->SimdVector.count;
  3789. elem_type = type->SimdVector.elem;
  3790. if (!build_context.use_llvm_api) {
  3791. error(call, "'swizzle' with #simd vector is not supported on this backend");
  3792. }
  3793. }
  3794. i64 arg_count = 0;
  3795. for_array(i, ce->args) {
  3796. if (i == 0) {
  3797. continue;
  3798. }
  3799. Ast *arg = ce->args[i];
  3800. Operand op = {};
  3801. check_expr(c, &op, arg);
  3802. if (op.mode == Addressing_Invalid) {
  3803. return false;
  3804. }
  3805. Type *arg_type = base_type(op.type);
  3806. if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
  3807. error(op.expr, "Indices to 'swizzle' must be constant integers");
  3808. return false;
  3809. }
  3810. if (op.value.value_integer.neg) {
  3811. error(op.expr, "Negative 'swizzle' index");
  3812. return false;
  3813. }
  3814. BigInt mc = {};
  3815. big_int_from_i64(&mc, max_count);
  3816. if (big_int_cmp(&mc, &op.value.value_integer) <= 0) {
  3817. error(op.expr, "'swizzle' index exceeds length");
  3818. return false;
  3819. }
  3820. arg_count++;
  3821. }
  3822. if (arg_count > max_count) {
  3823. error(call, "Too many 'swizzle' indices, %td > %td", arg_count, max_count);
  3824. return false;
  3825. }
  3826. if (arg_count < max_count) {
  3827. operand->type = alloc_type_array(elem_type, arg_count);
  3828. }
  3829. operand->mode = Addressing_Value;
  3830. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  3831. operand->type = type_hint;
  3832. }
  3833. break;
  3834. }
  3835. case BuiltinProc_complex: {
  3836. // complex :: proc(real, imag: float_type) -> complex_type
  3837. Operand x = *operand;
  3838. Operand y = {};
  3839. // NOTE(bill): Invalid will be the default till fixed
  3840. operand->type = t_invalid;
  3841. operand->mode = Addressing_Invalid;
  3842. check_expr(c, &y, ce->args[1]);
  3843. if (y.mode == Addressing_Invalid) {
  3844. return false;
  3845. }
  3846. convert_to_typed(c, &x, y.type); if (x.mode == Addressing_Invalid) return false;
  3847. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  3848. if (x.mode == Addressing_Constant &&
  3849. y.mode == Addressing_Constant) {
  3850. if (is_type_numeric(x.type) && exact_value_imag(x.value).value_float == 0) {
  3851. x.type = t_untyped_float;
  3852. }
  3853. if (is_type_numeric(y.type) && exact_value_imag(y.value).value_float == 0) {
  3854. y.type = t_untyped_float;
  3855. }
  3856. }
  3857. if (!are_types_identical(x.type, y.type)) {
  3858. gbString tx = type_to_string(x.type);
  3859. gbString ty = type_to_string(y.type);
  3860. error(call, "Mismatched types to 'complex', '%s' vs '%s'", tx, ty);
  3861. gb_string_free(ty);
  3862. gb_string_free(tx);
  3863. return false;
  3864. }
  3865. if (!is_type_float(x.type)) {
  3866. gbString s = type_to_string(x.type);
  3867. error(call, "Arguments have type '%s', expected a floating point", s);
  3868. gb_string_free(s);
  3869. return false;
  3870. }
  3871. if (x.mode == Addressing_Constant && y.mode == Addressing_Constant) {
  3872. f64 r = exact_value_to_float(x.value).value_float;
  3873. f64 i = exact_value_to_float(y.value).value_float;
  3874. operand->value = exact_value_complex(r, i);
  3875. operand->mode = Addressing_Constant;
  3876. } else {
  3877. operand->mode = Addressing_Value;
  3878. }
  3879. BasicKind kind = core_type(x.type)->Basic.kind;
  3880. switch (kind) {
  3881. // case Basic_f16: operand->type = t_complex32; break;
  3882. case Basic_f32: operand->type = t_complex64; break;
  3883. case Basic_f64: operand->type = t_complex128; break;
  3884. case Basic_UntypedFloat: operand->type = t_untyped_complex; break;
  3885. default: GB_PANIC("Invalid type"); break;
  3886. }
  3887. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  3888. operand->type = type_hint;
  3889. }
  3890. break;
  3891. }
  3892. case BuiltinProc_quaternion: {
  3893. // quaternion :: proc(real, imag, jmag, kmag: float_type) -> complex_type
  3894. Operand x = *operand;
  3895. Operand y = {};
  3896. Operand z = {};
  3897. Operand w = {};
  3898. // NOTE(bill): Invalid will be the default till fixed
  3899. operand->type = t_invalid;
  3900. operand->mode = Addressing_Invalid;
  3901. check_expr(c, &y, ce->args[1]);
  3902. if (y.mode == Addressing_Invalid) {
  3903. return false;
  3904. }
  3905. check_expr(c, &z, ce->args[2]);
  3906. if (y.mode == Addressing_Invalid) {
  3907. return false;
  3908. }
  3909. check_expr(c, &w, ce->args[3]);
  3910. if (y.mode == Addressing_Invalid) {
  3911. return false;
  3912. }
  3913. convert_to_typed(c, &x, y.type); if (x.mode == Addressing_Invalid) return false;
  3914. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  3915. convert_to_typed(c, &z, x.type); if (z.mode == Addressing_Invalid) return false;
  3916. convert_to_typed(c, &w, x.type); if (w.mode == Addressing_Invalid) return false;
  3917. if (x.mode == Addressing_Constant &&
  3918. y.mode == Addressing_Constant &&
  3919. z.mode == Addressing_Constant &&
  3920. w.mode == Addressing_Constant) {
  3921. if (is_type_numeric(x.type) && exact_value_imag(x.value).value_float == 0) {
  3922. x.type = t_untyped_float;
  3923. }
  3924. if (is_type_numeric(y.type) && exact_value_imag(y.value).value_float == 0) {
  3925. y.type = t_untyped_float;
  3926. }
  3927. if (is_type_numeric(z.type) && exact_value_imag(z.value).value_float == 0) {
  3928. z.type = t_untyped_float;
  3929. }
  3930. if (is_type_numeric(w.type) && exact_value_imag(w.value).value_float == 0) {
  3931. w.type = t_untyped_float;
  3932. }
  3933. }
  3934. if (!(are_types_identical(x.type, y.type) && are_types_identical(x.type, z.type) && are_types_identical(x.type, w.type))) {
  3935. gbString tx = type_to_string(x.type);
  3936. gbString ty = type_to_string(y.type);
  3937. gbString tz = type_to_string(z.type);
  3938. gbString tw = type_to_string(w.type);
  3939. error(call, "Mismatched types to 'quaternion', '%s' vs '%s' vs '%s' vs '%s'", tx, ty, tz, tw);
  3940. gb_string_free(tw);
  3941. gb_string_free(tz);
  3942. gb_string_free(ty);
  3943. gb_string_free(tx);
  3944. return false;
  3945. }
  3946. if (!is_type_float(x.type)) {
  3947. gbString s = type_to_string(x.type);
  3948. error(call, "Arguments have type '%s', expected a floating point", s);
  3949. gb_string_free(s);
  3950. return false;
  3951. }
  3952. if (x.mode == Addressing_Constant && y.mode == Addressing_Constant && z.mode == Addressing_Constant && w.mode == Addressing_Constant) {
  3953. f64 r = exact_value_to_float(x.value).value_float;
  3954. f64 i = exact_value_to_float(y.value).value_float;
  3955. f64 j = exact_value_to_float(z.value).value_float;
  3956. f64 k = exact_value_to_float(w.value).value_float;
  3957. operand->value = exact_value_quaternion(r, i, j, k);
  3958. operand->mode = Addressing_Constant;
  3959. } else {
  3960. operand->mode = Addressing_Value;
  3961. }
  3962. BasicKind kind = core_type(x.type)->Basic.kind;
  3963. switch (kind) {
  3964. case Basic_f32: operand->type = t_quaternion128; break;
  3965. case Basic_f64: operand->type = t_quaternion256; break;
  3966. case Basic_UntypedFloat: operand->type = t_untyped_quaternion; break;
  3967. default: GB_PANIC("Invalid type"); break;
  3968. }
  3969. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  3970. operand->type = type_hint;
  3971. }
  3972. break;
  3973. }
  3974. case BuiltinProc_real:
  3975. case BuiltinProc_imag: {
  3976. // real :: proc(x: type) -> float_type
  3977. // imag :: proc(x: type) -> float_type
  3978. Operand *x = operand;
  3979. if (is_type_untyped(x->type)) {
  3980. if (x->mode == Addressing_Constant) {
  3981. if (is_type_numeric(x->type)) {
  3982. x->type = t_untyped_complex;
  3983. }
  3984. } else if (is_type_quaternion(x->type)) {
  3985. convert_to_typed(c, x, t_quaternion256);
  3986. if (x->mode == Addressing_Invalid) {
  3987. return false;
  3988. }
  3989. } else{
  3990. convert_to_typed(c, x, t_complex128);
  3991. if (x->mode == Addressing_Invalid) {
  3992. return false;
  3993. }
  3994. }
  3995. }
  3996. if (!is_type_complex(x->type) && !is_type_quaternion(x->type)) {
  3997. gbString s = type_to_string(x->type);
  3998. error(call, "Argument has type '%s', expected a complex or quaternion type", s);
  3999. gb_string_free(s);
  4000. return false;
  4001. }
  4002. if (x->mode == Addressing_Constant) {
  4003. switch (id) {
  4004. case BuiltinProc_real: x->value = exact_value_real(x->value); break;
  4005. case BuiltinProc_imag: x->value = exact_value_imag(x->value); break;
  4006. }
  4007. } else {
  4008. x->mode = Addressing_Value;
  4009. }
  4010. BasicKind kind = core_type(x->type)->Basic.kind;
  4011. switch (kind) {
  4012. case Basic_complex64: x->type = t_f32; break;
  4013. case Basic_complex128: x->type = t_f64; break;
  4014. case Basic_quaternion128: x->type = t_f32; break;
  4015. case Basic_quaternion256: x->type = t_f64; break;
  4016. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  4017. case Basic_UntypedQuaternion: x->type = t_untyped_float; break;
  4018. default: GB_PANIC("Invalid type"); break;
  4019. }
  4020. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  4021. operand->type = type_hint;
  4022. }
  4023. break;
  4024. }
  4025. case BuiltinProc_jmag:
  4026. case BuiltinProc_kmag: {
  4027. // jmag :: proc(x: type) -> float_type
  4028. // kmag :: proc(x: type) -> float_type
  4029. Operand *x = operand;
  4030. if (is_type_untyped(x->type)) {
  4031. if (x->mode == Addressing_Constant) {
  4032. if (is_type_numeric(x->type)) {
  4033. x->type = t_untyped_complex;
  4034. }
  4035. } else{
  4036. convert_to_typed(c, x, t_quaternion256);
  4037. if (x->mode == Addressing_Invalid) {
  4038. return false;
  4039. }
  4040. }
  4041. }
  4042. if (!is_type_quaternion(x->type)) {
  4043. gbString s = type_to_string(x->type);
  4044. error(call, "Argument has type '%s', expected a quaternion type", s);
  4045. gb_string_free(s);
  4046. return false;
  4047. }
  4048. if (x->mode == Addressing_Constant) {
  4049. switch (id) {
  4050. case BuiltinProc_jmag: x->value = exact_value_jmag(x->value); break;
  4051. case BuiltinProc_kmag: x->value = exact_value_kmag(x->value); break;
  4052. }
  4053. } else {
  4054. x->mode = Addressing_Value;
  4055. }
  4056. BasicKind kind = core_type(x->type)->Basic.kind;
  4057. switch (kind) {
  4058. case Basic_quaternion128: x->type = t_f32; break;
  4059. case Basic_quaternion256: x->type = t_f64; break;
  4060. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  4061. case Basic_UntypedQuaternion: x->type = t_untyped_float; break;
  4062. default: GB_PANIC("Invalid type"); break;
  4063. }
  4064. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  4065. operand->type = type_hint;
  4066. }
  4067. break;
  4068. }
  4069. case BuiltinProc_conj: {
  4070. // conj :: proc(x: type) -> type
  4071. Operand *x = operand;
  4072. if (is_type_complex(x->type)) {
  4073. if (x->mode == Addressing_Constant) {
  4074. ExactValue v = exact_value_to_complex(x->value);
  4075. f64 r = v.value_complex->real;
  4076. f64 i = -v.value_complex->imag;
  4077. x->value = exact_value_complex(r, i);
  4078. x->mode = Addressing_Constant;
  4079. } else {
  4080. x->mode = Addressing_Value;
  4081. }
  4082. } else if (is_type_quaternion(x->type)) {
  4083. if (x->mode == Addressing_Constant) {
  4084. ExactValue v = exact_value_to_quaternion(x->value);
  4085. f64 r = +v.value_quaternion->real;
  4086. f64 i = -v.value_quaternion->imag;
  4087. f64 j = -v.value_quaternion->jmag;
  4088. f64 k = -v.value_quaternion->kmag;
  4089. x->value = exact_value_quaternion(r, i, j, k);
  4090. x->mode = Addressing_Constant;
  4091. } else {
  4092. x->mode = Addressing_Value;
  4093. }
  4094. } else {
  4095. gbString s = type_to_string(x->type);
  4096. error(call, "Expected a complex or quaternion, got '%s'", s);
  4097. gb_string_free(s);
  4098. return false;
  4099. }
  4100. break;
  4101. }
  4102. case BuiltinProc_expand_to_tuple: {
  4103. Type *type = base_type(operand->type);
  4104. if (!is_type_struct(type) && !is_type_array(type)) {
  4105. gbString type_str = type_to_string(operand->type);
  4106. error(call, "Expected a struct or array type, got '%s'", type_str);
  4107. gb_string_free(type_str);
  4108. return false;
  4109. }
  4110. gbAllocator a = permanent_allocator();
  4111. Type *tuple = alloc_type_tuple();
  4112. if (is_type_struct(type)) {
  4113. isize variable_count = type->Struct.fields.count;
  4114. array_init(&tuple->Tuple.variables, a, variable_count);
  4115. // TODO(bill): Should I copy each of the entities or is this good enough?
  4116. gb_memmove_array(tuple->Tuple.variables.data, type->Struct.fields.data, variable_count);
  4117. } else if (is_type_array(type)) {
  4118. isize variable_count = type->Array.count;
  4119. array_init(&tuple->Tuple.variables, a, variable_count);
  4120. for (isize i = 0; i < variable_count; i++) {
  4121. tuple->Tuple.variables[i] = alloc_entity_array_elem(nullptr, blank_token, type->Array.elem, cast(i32)i);
  4122. }
  4123. }
  4124. operand->type = tuple;
  4125. operand->mode = Addressing_Value;
  4126. if (tuple->Tuple.variables.count == 1) {
  4127. operand->type = tuple->Tuple.variables[0]->type;
  4128. }
  4129. break;
  4130. }
  4131. case BuiltinProc_min: {
  4132. // min :: proc($T: typeid) -> ordered
  4133. // min :: proc(a: ..ordered) -> ordered
  4134. check_multi_expr_or_type(c, operand, ce->args[0]);
  4135. Type *original_type = operand->type;
  4136. Type *type = base_type(operand->type);
  4137. if (operand->mode == Addressing_Type && is_type_enumerated_array(type)) {
  4138. // Okay
  4139. } else if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  4140. gbString type_str = type_to_string(original_type);
  4141. error(call, "Expected a ordered numeric type to 'min', got '%s'", type_str);
  4142. gb_string_free(type_str);
  4143. return false;
  4144. }
  4145. if (operand->mode == Addressing_Type) {
  4146. if (ce->args.count != 1) {
  4147. error(call, "If 'min' gets a type, only 1 arguments is allowed, got %td", ce->args.count);
  4148. return false;
  4149. }
  4150. if (is_type_boolean(type)) {
  4151. operand->mode = Addressing_Constant;
  4152. operand->type = original_type;
  4153. operand->value = exact_value_bool(false);
  4154. return true;
  4155. } else if (is_type_integer(type)) {
  4156. operand->mode = Addressing_Constant;
  4157. operand->type = original_type;
  4158. if (is_type_unsigned(type)) {
  4159. operand->value = exact_value_u64(0);
  4160. return true;
  4161. } else {
  4162. i64 sz = 8*type_size_of(type);
  4163. ExactValue a = exact_value_i64(1);
  4164. ExactValue b = exact_value_i64(sz-1);
  4165. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  4166. v = exact_unary_operator_value(Token_Sub, v, cast(i32)sz, false);
  4167. operand->value = v;
  4168. return true;
  4169. }
  4170. } else if (is_type_float(type)) {
  4171. operand->mode = Addressing_Constant;
  4172. operand->type = original_type;
  4173. switch (type_size_of(type)) {
  4174. case 4:
  4175. operand->value = exact_value_float(-3.402823466e+38f);
  4176. break;
  4177. case 8:
  4178. operand->value = exact_value_float(-1.7976931348623158e+308);
  4179. break;
  4180. default:
  4181. GB_PANIC("Unhandled float type");
  4182. break;
  4183. }
  4184. return true;
  4185. } else if (is_type_enum(type)) {
  4186. operand->mode = Addressing_Constant;
  4187. operand->type = original_type;
  4188. operand->value = type->Enum.min_value;
  4189. return true;
  4190. } else if (is_type_enumerated_array(type)) {
  4191. Type *bt = base_type(type);
  4192. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  4193. operand->mode = Addressing_Constant;
  4194. operand->type = bt->EnumeratedArray.index;
  4195. operand->value = bt->EnumeratedArray.min_value;
  4196. return true;
  4197. }
  4198. gbString type_str = type_to_string(original_type);
  4199. error(call, "Invalid type for 'min', got %s", type_str);
  4200. gb_string_free(type_str);
  4201. return false;
  4202. }
  4203. bool all_constant = operand->mode == Addressing_Constant;
  4204. auto operands = array_make<Operand>(heap_allocator(), 0, ce->args.count);
  4205. defer (array_free(&operands));
  4206. array_add(&operands, *operand);
  4207. for (isize i = 1; i < ce->args.count; i++) {
  4208. Ast *other_arg = ce->args[i];
  4209. Operand b = {};
  4210. check_expr(c, &b, other_arg);
  4211. if (b.mode == Addressing_Invalid) {
  4212. return false;
  4213. }
  4214. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  4215. gbString type_str = type_to_string(b.type);
  4216. error(call,
  4217. "Expected a ordered numeric type to 'min', got '%s'",
  4218. type_str);
  4219. gb_string_free(type_str);
  4220. return false;
  4221. }
  4222. array_add(&operands, b);
  4223. if (all_constant) {
  4224. all_constant = b.mode == Addressing_Constant;
  4225. }
  4226. }
  4227. if (all_constant) {
  4228. ExactValue value = operands[0].value;
  4229. Type *type = operands[0].type;
  4230. for (isize i = 1; i < operands.count; i++) {
  4231. Operand y = operands[i];
  4232. if (compare_exact_values(Token_Lt, value, y.value)) {
  4233. // okay
  4234. } else {
  4235. value = y.value;
  4236. type = y.type;
  4237. }
  4238. }
  4239. operand->value = value;
  4240. operand->type = type;
  4241. } else {
  4242. operand->mode = Addressing_Value;
  4243. operand->type = original_type;
  4244. for_array(i, operands) {
  4245. Operand *a = &operands[i];
  4246. for_array(j, operands) {
  4247. if (i == j) {
  4248. continue;
  4249. }
  4250. Operand *b = &operands[j];
  4251. convert_to_typed(c, a, b->type);
  4252. if (a->mode == Addressing_Invalid) {
  4253. return false;
  4254. }
  4255. convert_to_typed(c, b, a->type);
  4256. if (b->mode == Addressing_Invalid) {
  4257. return false;
  4258. }
  4259. }
  4260. }
  4261. for (isize i = 0; i < operands.count-1; i++) {
  4262. Operand *a = &operands[i];
  4263. Operand *b = &operands[i+1];
  4264. if (!are_types_identical(a->type, b->type)) {
  4265. gbString type_a = type_to_string(a->type);
  4266. gbString type_b = type_to_string(b->type);
  4267. error(a->expr,
  4268. "Mismatched types to 'min', '%s' vs '%s'",
  4269. type_a, type_b);
  4270. gb_string_free(type_b);
  4271. gb_string_free(type_a);
  4272. return false;
  4273. }
  4274. }
  4275. {
  4276. Type *bt = base_type(operands[0].type);
  4277. if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "min_f32");
  4278. if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "min_f64");
  4279. operand->type = operands[0].type;
  4280. }
  4281. }
  4282. break;
  4283. }
  4284. case BuiltinProc_max: {
  4285. // max :: proc($T: typeid) -> ordered
  4286. // max :: proc(a: ..ordered) -> ordered
  4287. check_multi_expr_or_type(c, operand, ce->args[0]);
  4288. Type *original_type = operand->type;
  4289. Type *type = base_type(operand->type);
  4290. if (operand->mode == Addressing_Type && is_type_enumerated_array(type)) {
  4291. // Okay
  4292. } else if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  4293. gbString type_str = type_to_string(original_type);
  4294. error(call, "Expected a ordered numeric type to 'max', got '%s'", type_str);
  4295. gb_string_free(type_str);
  4296. return false;
  4297. }
  4298. if (operand->mode == Addressing_Type) {
  4299. if (ce->args.count != 1) {
  4300. error(call, "If 'max' gets a type, only 1 arguments is allowed, got %td", ce->args.count);
  4301. return false;
  4302. }
  4303. if (is_type_boolean(type)) {
  4304. operand->mode = Addressing_Constant;
  4305. operand->type = original_type;
  4306. operand->value = exact_value_bool(true);
  4307. return true;
  4308. } else if (is_type_integer(type)) {
  4309. operand->mode = Addressing_Constant;
  4310. operand->type = original_type;
  4311. if (is_type_unsigned(type)) {
  4312. i64 sz = 8*type_size_of(type);
  4313. ExactValue a = exact_value_i64(1);
  4314. ExactValue b = exact_value_i64(sz);
  4315. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  4316. v = exact_binary_operator_value(Token_Sub, v, a);
  4317. operand->value = v;
  4318. return true;
  4319. } else {
  4320. i64 sz = 8*type_size_of(type);
  4321. ExactValue a = exact_value_i64(1);
  4322. ExactValue b = exact_value_i64(sz-1);
  4323. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  4324. v = exact_binary_operator_value(Token_Sub, v, a);
  4325. operand->value = v;
  4326. return true;
  4327. }
  4328. } else if (is_type_float(type)) {
  4329. operand->mode = Addressing_Constant;
  4330. operand->type = original_type;
  4331. switch (type_size_of(type)) {
  4332. case 4:
  4333. operand->value = exact_value_float(3.402823466e+38f);
  4334. break;
  4335. case 8:
  4336. operand->value = exact_value_float(1.7976931348623158e+308);
  4337. break;
  4338. default:
  4339. GB_PANIC("Unhandled float type");
  4340. break;
  4341. }
  4342. return true;
  4343. } else if (is_type_enum(type)) {
  4344. operand->mode = Addressing_Constant;
  4345. operand->type = original_type;
  4346. operand->value = type->Enum.max_value;
  4347. return true;
  4348. } else if (is_type_enumerated_array(type)) {
  4349. Type *bt = base_type(type);
  4350. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  4351. operand->mode = Addressing_Constant;
  4352. operand->type = bt->EnumeratedArray.index;
  4353. operand->value = bt->EnumeratedArray.max_value;
  4354. return true;
  4355. }
  4356. gbString type_str = type_to_string(original_type);
  4357. error(call, "Invalid type for 'max', got %s", type_str);
  4358. gb_string_free(type_str);
  4359. return false;
  4360. }
  4361. bool all_constant = operand->mode == Addressing_Constant;
  4362. auto operands = array_make<Operand>(heap_allocator(), 0, ce->args.count);
  4363. defer (array_free(&operands));
  4364. array_add(&operands, *operand);
  4365. for (isize i = 1; i < ce->args.count; i++) {
  4366. Ast *arg = ce->args[i];
  4367. Operand b = {};
  4368. check_expr(c, &b, arg);
  4369. if (b.mode == Addressing_Invalid) {
  4370. return false;
  4371. }
  4372. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  4373. gbString type_str = type_to_string(b.type);
  4374. error(arg,
  4375. "Expected a ordered numeric type to 'max', got '%s'",
  4376. type_str);
  4377. gb_string_free(type_str);
  4378. return false;
  4379. }
  4380. array_add(&operands, b);
  4381. if (all_constant) {
  4382. all_constant = b.mode == Addressing_Constant;
  4383. }
  4384. }
  4385. if (all_constant) {
  4386. ExactValue value = operands[0].value;
  4387. Type *type = operands[0].type;
  4388. for (isize i = 1; i < operands.count; i++) {
  4389. Operand y = operands[i];
  4390. if (compare_exact_values(Token_Gt, value, y.value)) {
  4391. // okay
  4392. } else {
  4393. type = y.type;
  4394. value = y.value;
  4395. }
  4396. }
  4397. operand->value = value;
  4398. operand->type = type;
  4399. } else {
  4400. operand->mode = Addressing_Value;
  4401. operand->type = original_type;
  4402. for_array(i, operands) {
  4403. Operand *a = &operands[i];
  4404. for_array(j, operands) {
  4405. if (i == j) {
  4406. continue;
  4407. }
  4408. Operand *b = &operands[j];
  4409. convert_to_typed(c, a, b->type);
  4410. if (a->mode == Addressing_Invalid) {
  4411. return false;
  4412. }
  4413. convert_to_typed(c, b, a->type);
  4414. if (b->mode == Addressing_Invalid) {
  4415. return false;
  4416. }
  4417. }
  4418. }
  4419. for (isize i = 0; i < operands.count-1; i++) {
  4420. Operand *a = &operands[i];
  4421. Operand *b = &operands[i+1];
  4422. if (!are_types_identical(a->type, b->type)) {
  4423. gbString type_a = type_to_string(a->type);
  4424. gbString type_b = type_to_string(b->type);
  4425. error(a->expr,
  4426. "Mismatched types to 'max', '%s' vs '%s'",
  4427. type_a, type_b);
  4428. gb_string_free(type_b);
  4429. gb_string_free(type_a);
  4430. return false;
  4431. }
  4432. }
  4433. {
  4434. Type *bt = base_type(operands[0].type);
  4435. if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "max_f32");
  4436. if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "max_f64");
  4437. operand->type = operands[0].type;
  4438. }
  4439. }
  4440. break;
  4441. }
  4442. case BuiltinProc_abs: {
  4443. // abs :: proc(n: numeric) -> numeric
  4444. if (!(is_type_numeric(operand->type) && !is_type_array(operand->type))) {
  4445. gbString type_str = type_to_string(operand->type);
  4446. error(call, "Expected a numeric type to 'abs', got '%s'", type_str);
  4447. gb_string_free(type_str);
  4448. return false;
  4449. }
  4450. if (operand->mode == Addressing_Constant) {
  4451. switch (operand->value.kind) {
  4452. case ExactValue_Integer:
  4453. operand->value.value_integer.neg = false;
  4454. break;
  4455. case ExactValue_Float:
  4456. operand->value.value_float = gb_abs(operand->value.value_float);
  4457. break;
  4458. case ExactValue_Complex: {
  4459. f64 r = operand->value.value_complex->real;
  4460. f64 i = operand->value.value_complex->imag;
  4461. operand->value = exact_value_float(gb_sqrt(r*r + i*i));
  4462. break;
  4463. }
  4464. default:
  4465. GB_PANIC("Invalid numeric constant");
  4466. break;
  4467. }
  4468. } else {
  4469. operand->mode = Addressing_Value;
  4470. {
  4471. Type *bt = base_type(operand->type);
  4472. if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "abs_f32");
  4473. if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "abs_f64");
  4474. if (are_types_identical(bt, t_complex64)) add_package_dependency(c, "runtime", "abs_complex64");
  4475. if (are_types_identical(bt, t_complex128)) add_package_dependency(c, "runtime", "abs_complex128");
  4476. if (are_types_identical(bt, t_quaternion128)) add_package_dependency(c, "runtime", "abs_quaternion128");
  4477. if (are_types_identical(bt, t_quaternion256)) add_package_dependency(c, "runtime", "abs_quaternion256");
  4478. }
  4479. }
  4480. if (is_type_complex(operand->type)) {
  4481. operand->type = base_complex_elem_type(operand->type);
  4482. }
  4483. GB_ASSERT(!is_type_complex(operand->type));
  4484. break;
  4485. }
  4486. case BuiltinProc_clamp: {
  4487. // clamp :: proc(a, min, max: ordered) -> ordered
  4488. Type *type = operand->type;
  4489. if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  4490. gbString type_str = type_to_string(operand->type);
  4491. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  4492. gb_string_free(type_str);
  4493. return false;
  4494. }
  4495. Ast *min_arg = ce->args[1];
  4496. Ast *max_arg = ce->args[2];
  4497. Operand x = *operand;
  4498. Operand y = {};
  4499. Operand z = {};
  4500. check_expr(c, &y, min_arg);
  4501. if (y.mode == Addressing_Invalid) {
  4502. return false;
  4503. }
  4504. if (!is_type_ordered(y.type) || !(is_type_numeric(y.type) || is_type_string(y.type))) {
  4505. gbString type_str = type_to_string(y.type);
  4506. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  4507. gb_string_free(type_str);
  4508. return false;
  4509. }
  4510. check_expr(c, &z, max_arg);
  4511. if (z.mode == Addressing_Invalid) {
  4512. return false;
  4513. }
  4514. if (!is_type_ordered(z.type) || !(is_type_numeric(z.type) || is_type_string(z.type))) {
  4515. gbString type_str = type_to_string(z.type);
  4516. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  4517. gb_string_free(type_str);
  4518. return false;
  4519. }
  4520. if (x.mode == Addressing_Constant &&
  4521. y.mode == Addressing_Constant &&
  4522. z.mode == Addressing_Constant) {
  4523. ExactValue a = x.value;
  4524. ExactValue b = y.value;
  4525. ExactValue c = z.value;
  4526. operand->mode = Addressing_Constant;
  4527. if (compare_exact_values(Token_Lt, a, b)) {
  4528. operand->value = b;
  4529. operand->type = y.type;
  4530. } else if (compare_exact_values(Token_Gt, a, c)) {
  4531. operand->value = c;
  4532. operand->type = z.type;
  4533. } else {
  4534. operand->value = a;
  4535. operand->type = x.type;
  4536. }
  4537. } else {
  4538. operand->mode = Addressing_Value;
  4539. operand->type = type;
  4540. Operand *ops[3] = {&x, &y, &z};
  4541. for (isize i = 0; i < 3; i++) {
  4542. Operand *a = ops[i];
  4543. for (isize j = 0; j < 3; j++) {
  4544. if (i == j) continue;
  4545. Operand *b = ops[j];
  4546. convert_to_typed(c, a, b->type);
  4547. if (a->mode == Addressing_Invalid) { return false; }
  4548. }
  4549. }
  4550. if (!are_types_identical(x.type, y.type) || !are_types_identical(x.type, z.type)) {
  4551. gbString type_x = type_to_string(x.type);
  4552. gbString type_y = type_to_string(y.type);
  4553. gbString type_z = type_to_string(z.type);
  4554. error(call,
  4555. "Mismatched types to 'clamp', '%s', '%s', '%s'",
  4556. type_x, type_y, type_z);
  4557. gb_string_free(type_z);
  4558. gb_string_free(type_y);
  4559. gb_string_free(type_x);
  4560. return false;
  4561. }
  4562. {
  4563. Type *bt = base_type(x.type);
  4564. if (are_types_identical(bt, t_f32)) {
  4565. add_package_dependency(c, "runtime", "min_f32");
  4566. add_package_dependency(c, "runtime", "max_f32");
  4567. }
  4568. if (are_types_identical(bt, t_f64)) {
  4569. add_package_dependency(c, "runtime", "min_f64");
  4570. add_package_dependency(c, "runtime", "max_f64");
  4571. }
  4572. operand->type = ops[0]->type;
  4573. }
  4574. }
  4575. break;
  4576. }
  4577. case BuiltinProc_simd_vector: {
  4578. Operand x = {};
  4579. Operand y = {};
  4580. x = *operand;
  4581. if (!is_type_integer(x.type) || x.mode != Addressing_Constant) {
  4582. error(call, "Expected a constant integer for 'intrinsics.simd_vector'");
  4583. operand->mode = Addressing_Type;
  4584. operand->type = t_invalid;
  4585. return false;
  4586. }
  4587. if (x.value.value_integer.neg) {
  4588. error(call, "Negative vector element length");
  4589. operand->mode = Addressing_Type;
  4590. operand->type = t_invalid;
  4591. return false;
  4592. }
  4593. i64 count = big_int_to_i64(&x.value.value_integer);
  4594. check_expr_or_type(c, &y, ce->args[1]);
  4595. if (y.mode != Addressing_Type) {
  4596. error(call, "Expected a type 'intrinsics.simd_vector'");
  4597. operand->mode = Addressing_Type;
  4598. operand->type = t_invalid;
  4599. return false;
  4600. }
  4601. Type *elem = y.type;
  4602. if (!is_type_valid_vector_elem(elem)) {
  4603. gbString str = type_to_string(elem);
  4604. error(call, "Invalid element type for 'intrinsics.simd_vector', expected an integer or float with no specific endianness, got '%s'", str);
  4605. gb_string_free(str);
  4606. operand->mode = Addressing_Type;
  4607. operand->type = t_invalid;
  4608. return false;
  4609. }
  4610. operand->mode = Addressing_Type;
  4611. operand->type = alloc_type_simd_vector(count, elem);
  4612. break;
  4613. }
  4614. case BuiltinProc_soa_struct: {
  4615. Operand x = {};
  4616. Operand y = {};
  4617. x = *operand;
  4618. if (!is_type_integer(x.type) || x.mode != Addressing_Constant) {
  4619. error(call, "Expected a constant integer for 'intrinsics.soa_struct'");
  4620. operand->mode = Addressing_Type;
  4621. operand->type = t_invalid;
  4622. return false;
  4623. }
  4624. if (x.value.value_integer.neg) {
  4625. error(call, "Negative array element length");
  4626. operand->mode = Addressing_Type;
  4627. operand->type = t_invalid;
  4628. return false;
  4629. }
  4630. i64 count = big_int_to_i64(&x.value.value_integer);
  4631. check_expr_or_type(c, &y, ce->args[1]);
  4632. if (y.mode != Addressing_Type) {
  4633. error(call, "Expected a type 'intrinsics.soa_struct'");
  4634. operand->mode = Addressing_Type;
  4635. operand->type = t_invalid;
  4636. return false;
  4637. }
  4638. Type *elem = y.type;
  4639. Type *bt_elem = base_type(elem);
  4640. if (!is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  4641. gbString str = type_to_string(elem);
  4642. error(call, "Invalid type for 'intrinsics.soa_struct', expected a struct or array of length 4 or below, got '%s'", str);
  4643. gb_string_free(str);
  4644. operand->mode = Addressing_Type;
  4645. operand->type = t_invalid;
  4646. return false;
  4647. }
  4648. operand->mode = Addressing_Type;
  4649. Type *soa_struct = nullptr;
  4650. Scope *scope = nullptr;
  4651. if (is_type_array(elem)) {
  4652. Type *old_array = base_type(elem);
  4653. soa_struct = alloc_type_struct();
  4654. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), old_array->Array.count);
  4655. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_array->Array.count);
  4656. soa_struct->Struct.node = operand->expr;
  4657. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  4658. soa_struct->Struct.soa_elem = elem;
  4659. soa_struct->Struct.soa_count = count;
  4660. scope = create_scope(c->scope);
  4661. soa_struct->Struct.scope = scope;
  4662. String params_xyzw[4] = {
  4663. str_lit("x"),
  4664. str_lit("y"),
  4665. str_lit("z"),
  4666. str_lit("w")
  4667. };
  4668. for (i64 i = 0; i < old_array->Array.count; i++) {
  4669. Type *array_type = alloc_type_array(old_array->Array.elem, count);
  4670. Token token = {};
  4671. token.string = params_xyzw[i];
  4672. Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
  4673. soa_struct->Struct.fields[i] = new_field;
  4674. add_entity(c->checker, scope, nullptr, new_field);
  4675. add_entity_use(c, nullptr, new_field);
  4676. }
  4677. } else {
  4678. GB_ASSERT(is_type_struct(elem));
  4679. Type *old_struct = base_type(elem);
  4680. soa_struct = alloc_type_struct();
  4681. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), old_struct->Struct.fields.count);
  4682. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_struct->Struct.tags.count);
  4683. soa_struct->Struct.node = operand->expr;
  4684. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  4685. soa_struct->Struct.soa_elem = elem;
  4686. soa_struct->Struct.soa_count = count;
  4687. scope = create_scope(old_struct->Struct.scope->parent);
  4688. soa_struct->Struct.scope = scope;
  4689. for_array(i, old_struct->Struct.fields) {
  4690. Entity *old_field = old_struct->Struct.fields[i];
  4691. if (old_field->kind == Entity_Variable) {
  4692. Type *array_type = alloc_type_array(old_field->type, count);
  4693. Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_src_index);
  4694. soa_struct->Struct.fields[i] = new_field;
  4695. add_entity(c->checker, scope, nullptr, new_field);
  4696. } else {
  4697. soa_struct->Struct.fields[i] = old_field;
  4698. }
  4699. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  4700. }
  4701. }
  4702. Token token = {};
  4703. token.string = str_lit("Base_Type");
  4704. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  4705. add_entity(c->checker, scope, nullptr, base_type_entity);
  4706. add_type_info_type(c, soa_struct);
  4707. operand->type = soa_struct;
  4708. break;
  4709. }
  4710. case BuiltinProc_alloca:
  4711. {
  4712. Operand sz = {};
  4713. Operand al = {};
  4714. check_expr(c, &sz, ce->args[0]);
  4715. if (sz.mode == Addressing_Invalid) {
  4716. return false;
  4717. }
  4718. check_expr(c, &al, ce->args[1]);
  4719. if (al.mode == Addressing_Invalid) {
  4720. return false;
  4721. }
  4722. convert_to_typed(c, &sz, t_int); if (sz.mode == Addressing_Invalid) return false;
  4723. convert_to_typed(c, &al, t_int); if (al.mode == Addressing_Invalid) return false;
  4724. if (!is_type_integer(sz.type) || !is_type_integer(al.type)) {
  4725. error(operand->expr, "Both parameters to '%.*s' must integers", LIT(builtin_name));
  4726. return false;
  4727. }
  4728. if (sz.mode == Addressing_Constant) {
  4729. i64 i_sz = exact_value_to_i64(sz.value);
  4730. if (i_sz < 0) {
  4731. error(sz.expr, "Size parameter to '%.*s' must be non-negative, got %lld", LIT(builtin_name), cast(long long)i_sz);
  4732. return false;
  4733. }
  4734. }
  4735. if (al.mode == Addressing_Constant) {
  4736. i64 i_al = exact_value_to_i64(al.value);
  4737. if (i_al < 0) {
  4738. error(al.expr, "Alignment parameter to '%.*s' must be non-negative, got %lld", LIT(builtin_name), cast(long long)i_al);
  4739. return false;
  4740. }
  4741. if (i_al > 1<<29) {
  4742. error(al.expr, "Alignment parameter to '%.*s' must not exceed '1<<29', got %lld", LIT(builtin_name), cast(long long)i_al);
  4743. return false;
  4744. }
  4745. if (!gb_is_power_of_two(cast(isize)i_al) && i_al != 0) {
  4746. error(al.expr, "Alignment parameter to '%.*s' must be a power of 2 or 0, got %lld", LIT(builtin_name), cast(long long)i_al);
  4747. return false;
  4748. }
  4749. } else {
  4750. error(al.expr, "Alignment parameter to '%.*s' must be constant", LIT(builtin_name));
  4751. }
  4752. operand->type = t_u8_ptr;
  4753. operand->mode = Addressing_Value;
  4754. break;
  4755. }
  4756. case BuiltinProc_cpu_relax:
  4757. operand->mode = Addressing_NoValue;
  4758. break;
  4759. case BuiltinProc_atomic_fence:
  4760. case BuiltinProc_atomic_fence_acq:
  4761. case BuiltinProc_atomic_fence_rel:
  4762. case BuiltinProc_atomic_fence_acqrel:
  4763. operand->mode = Addressing_NoValue;
  4764. break;
  4765. case BuiltinProc_volatile_store:
  4766. /*fallthrough*/
  4767. case BuiltinProc_atomic_store:
  4768. case BuiltinProc_atomic_store_rel:
  4769. case BuiltinProc_atomic_store_relaxed:
  4770. case BuiltinProc_atomic_store_unordered:
  4771. {
  4772. Type *elem = nullptr;
  4773. if (!is_type_normal_pointer(operand->type, &elem)) {
  4774. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4775. return false;
  4776. }
  4777. Operand x = {};
  4778. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4779. check_assignment(c, &x, elem, builtin_name);
  4780. operand->type = nullptr;
  4781. operand->mode = Addressing_NoValue;
  4782. break;
  4783. }
  4784. case BuiltinProc_volatile_load:
  4785. /*fallthrough*/
  4786. case BuiltinProc_atomic_load:
  4787. case BuiltinProc_atomic_load_acq:
  4788. case BuiltinProc_atomic_load_relaxed:
  4789. case BuiltinProc_atomic_load_unordered:
  4790. {
  4791. Type *elem = nullptr;
  4792. if (!is_type_normal_pointer(operand->type, &elem)) {
  4793. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4794. return false;
  4795. }
  4796. operand->type = elem;
  4797. operand->mode = Addressing_Value;
  4798. break;
  4799. }
  4800. case BuiltinProc_atomic_add:
  4801. case BuiltinProc_atomic_add_acq:
  4802. case BuiltinProc_atomic_add_rel:
  4803. case BuiltinProc_atomic_add_acqrel:
  4804. case BuiltinProc_atomic_add_relaxed:
  4805. case BuiltinProc_atomic_sub:
  4806. case BuiltinProc_atomic_sub_acq:
  4807. case BuiltinProc_atomic_sub_rel:
  4808. case BuiltinProc_atomic_sub_acqrel:
  4809. case BuiltinProc_atomic_sub_relaxed:
  4810. case BuiltinProc_atomic_and:
  4811. case BuiltinProc_atomic_and_acq:
  4812. case BuiltinProc_atomic_and_rel:
  4813. case BuiltinProc_atomic_and_acqrel:
  4814. case BuiltinProc_atomic_and_relaxed:
  4815. case BuiltinProc_atomic_nand:
  4816. case BuiltinProc_atomic_nand_acq:
  4817. case BuiltinProc_atomic_nand_rel:
  4818. case BuiltinProc_atomic_nand_acqrel:
  4819. case BuiltinProc_atomic_nand_relaxed:
  4820. case BuiltinProc_atomic_or:
  4821. case BuiltinProc_atomic_or_acq:
  4822. case BuiltinProc_atomic_or_rel:
  4823. case BuiltinProc_atomic_or_acqrel:
  4824. case BuiltinProc_atomic_or_relaxed:
  4825. case BuiltinProc_atomic_xor:
  4826. case BuiltinProc_atomic_xor_acq:
  4827. case BuiltinProc_atomic_xor_rel:
  4828. case BuiltinProc_atomic_xor_acqrel:
  4829. case BuiltinProc_atomic_xor_relaxed:
  4830. case BuiltinProc_atomic_xchg:
  4831. case BuiltinProc_atomic_xchg_acq:
  4832. case BuiltinProc_atomic_xchg_rel:
  4833. case BuiltinProc_atomic_xchg_acqrel:
  4834. case BuiltinProc_atomic_xchg_relaxed:
  4835. {
  4836. Type *elem = nullptr;
  4837. if (!is_type_normal_pointer(operand->type, &elem)) {
  4838. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4839. return false;
  4840. }
  4841. Operand x = {};
  4842. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4843. check_assignment(c, &x, elem, builtin_name);
  4844. operand->type = elem;
  4845. operand->mode = Addressing_Value;
  4846. break;
  4847. }
  4848. case BuiltinProc_atomic_cxchg:
  4849. case BuiltinProc_atomic_cxchg_acq:
  4850. case BuiltinProc_atomic_cxchg_rel:
  4851. case BuiltinProc_atomic_cxchg_acqrel:
  4852. case BuiltinProc_atomic_cxchg_relaxed:
  4853. case BuiltinProc_atomic_cxchg_failrelaxed:
  4854. case BuiltinProc_atomic_cxchg_failacq:
  4855. case BuiltinProc_atomic_cxchg_acq_failrelaxed:
  4856. case BuiltinProc_atomic_cxchg_acqrel_failrelaxed:
  4857. case BuiltinProc_atomic_cxchgweak:
  4858. case BuiltinProc_atomic_cxchgweak_acq:
  4859. case BuiltinProc_atomic_cxchgweak_rel:
  4860. case BuiltinProc_atomic_cxchgweak_acqrel:
  4861. case BuiltinProc_atomic_cxchgweak_relaxed:
  4862. case BuiltinProc_atomic_cxchgweak_failrelaxed:
  4863. case BuiltinProc_atomic_cxchgweak_failacq:
  4864. case BuiltinProc_atomic_cxchgweak_acq_failrelaxed:
  4865. case BuiltinProc_atomic_cxchgweak_acqrel_failrelaxed:
  4866. {
  4867. Type *elem = nullptr;
  4868. if (!is_type_normal_pointer(operand->type, &elem)) {
  4869. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4870. return false;
  4871. }
  4872. Operand x = {};
  4873. Operand y = {};
  4874. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4875. check_expr_with_type_hint(c, &y, ce->args[2], elem);
  4876. check_assignment(c, &x, elem, builtin_name);
  4877. check_assignment(c, &y, elem, builtin_name);
  4878. operand->mode = Addressing_Value;
  4879. operand->type = make_optional_ok_type(elem, /*typed*/false);
  4880. break;
  4881. }
  4882. break;
  4883. case BuiltinProc_type_base_type:
  4884. if (operand->mode != Addressing_Type) {
  4885. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4886. } else {
  4887. operand->type = base_type(operand->type);
  4888. }
  4889. operand->mode = Addressing_Type;
  4890. break;
  4891. case BuiltinProc_type_core_type:
  4892. if (operand->mode != Addressing_Type) {
  4893. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4894. } else {
  4895. operand->type = core_type(operand->type);
  4896. }
  4897. operand->mode = Addressing_Type;
  4898. break;
  4899. case BuiltinProc_type_elem_type:
  4900. if (operand->mode != Addressing_Type) {
  4901. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4902. } else {
  4903. Type *bt = base_type(operand->type);
  4904. switch (bt->kind) {
  4905. case Type_Basic:
  4906. switch (bt->Basic.kind) {
  4907. case Basic_complex64: operand->type = t_f32; break;
  4908. case Basic_complex128: operand->type = t_f64; break;
  4909. case Basic_quaternion128: operand->type = t_f32; break;
  4910. case Basic_quaternion256: operand->type = t_f64; break;
  4911. }
  4912. break;
  4913. case Type_Pointer: operand->type = bt->Pointer.elem; break;
  4914. case Type_Array: operand->type = bt->Array.elem; break;
  4915. case Type_EnumeratedArray: operand->type = bt->EnumeratedArray.elem; break;
  4916. case Type_Slice: operand->type = bt->Slice.elem; break;
  4917. case Type_DynamicArray: operand->type = bt->DynamicArray.elem; break;
  4918. }
  4919. }
  4920. operand->mode = Addressing_Type;
  4921. break;
  4922. case BuiltinProc_type_is_boolean:
  4923. case BuiltinProc_type_is_integer:
  4924. case BuiltinProc_type_is_rune:
  4925. case BuiltinProc_type_is_float:
  4926. case BuiltinProc_type_is_complex:
  4927. case BuiltinProc_type_is_quaternion:
  4928. case BuiltinProc_type_is_string:
  4929. case BuiltinProc_type_is_typeid:
  4930. case BuiltinProc_type_is_any:
  4931. case BuiltinProc_type_is_endian_little:
  4932. case BuiltinProc_type_is_endian_big:
  4933. case BuiltinProc_type_is_unsigned:
  4934. case BuiltinProc_type_is_numeric:
  4935. case BuiltinProc_type_is_ordered:
  4936. case BuiltinProc_type_is_ordered_numeric:
  4937. case BuiltinProc_type_is_indexable:
  4938. case BuiltinProc_type_is_sliceable:
  4939. case BuiltinProc_type_is_comparable:
  4940. case BuiltinProc_type_is_simple_compare:
  4941. case BuiltinProc_type_is_dereferenceable:
  4942. case BuiltinProc_type_is_valid_map_key:
  4943. case BuiltinProc_type_is_named:
  4944. case BuiltinProc_type_is_pointer:
  4945. case BuiltinProc_type_is_array:
  4946. case BuiltinProc_type_is_slice:
  4947. case BuiltinProc_type_is_dynamic_array:
  4948. case BuiltinProc_type_is_map:
  4949. case BuiltinProc_type_is_struct:
  4950. case BuiltinProc_type_is_union:
  4951. case BuiltinProc_type_is_enum:
  4952. case BuiltinProc_type_is_proc:
  4953. case BuiltinProc_type_is_bit_field:
  4954. case BuiltinProc_type_is_bit_field_value:
  4955. case BuiltinProc_type_is_bit_set:
  4956. case BuiltinProc_type_is_simd_vector:
  4957. case BuiltinProc_type_is_specialized_polymorphic_record:
  4958. case BuiltinProc_type_is_unspecialized_polymorphic_record:
  4959. case BuiltinProc_type_has_nil:
  4960. GB_ASSERT(BuiltinProc__type_simple_boolean_begin < id && id < BuiltinProc__type_simple_boolean_end);
  4961. operand->value = exact_value_bool(false);
  4962. if (operand->mode != Addressing_Type) {
  4963. gbString str = expr_to_string(ce->args[0]);
  4964. error(operand->expr, "Expected a type for '%.*s', got '%s'", LIT(builtin_name), str);
  4965. gb_string_free(str);
  4966. } else {
  4967. i32 i = id - cast(i32)BuiltinProc__type_simple_boolean_begin;
  4968. auto procedure = builtin_type_is_procs[i];
  4969. GB_ASSERT_MSG(procedure != nullptr, "%.*s", LIT(builtin_name));
  4970. operand->value = exact_value_bool(procedure(operand->type));
  4971. }
  4972. operand->mode = Addressing_Constant;
  4973. operand->type = t_untyped_bool;
  4974. break;
  4975. case BuiltinProc_type_has_field:
  4976. {
  4977. Operand op = {};
  4978. Type *bt = check_type(c, ce->args[0]);
  4979. Type *type = base_type(bt);
  4980. if (type == nullptr || type == t_invalid) {
  4981. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  4982. return false;
  4983. }
  4984. Operand x = {};
  4985. check_expr(c, &x, ce->args[1]);
  4986. if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
  4987. error(ce->args[1], "Expected a const string for field argument");
  4988. return false;
  4989. }
  4990. String field_name = x.value.value_string;
  4991. Selection sel = lookup_field(type, field_name, false);
  4992. operand->mode = Addressing_Constant;
  4993. operand->value = exact_value_bool(sel.index.count != 0);
  4994. operand->type = t_untyped_bool;
  4995. break;
  4996. }
  4997. break;
  4998. case BuiltinProc_type_is_specialization_of:
  4999. {
  5000. if (operand->mode != Addressing_Type) {
  5001. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  5002. operand->mode = Addressing_Invalid;
  5003. operand->type = t_invalid;
  5004. return false;
  5005. }
  5006. Type *t = operand->type;
  5007. Type *s = nullptr;
  5008. bool prev_ips = c->in_polymorphic_specialization;
  5009. c->in_polymorphic_specialization = true;
  5010. s = check_type(c, ce->args[1]);
  5011. c->in_polymorphic_specialization = prev_ips;
  5012. if (s == t_invalid) {
  5013. error(ce->args[1], "Invalid specialization type for '%.*s'", LIT(builtin_name));
  5014. operand->mode = Addressing_Invalid;
  5015. operand->type = t_invalid;
  5016. return false;
  5017. }
  5018. operand->mode = Addressing_Constant;
  5019. operand->type = t_untyped_bool;
  5020. operand->value = exact_value_bool(check_type_specialization_to(c, s, t, false, false));
  5021. }
  5022. break;
  5023. case BuiltinProc_type_struct_field_count:
  5024. operand->value = exact_value_i64(0);
  5025. if (operand->mode != Addressing_Type) {
  5026. error(operand->expr, "Expected a struct type for '%.*s'", LIT(builtin_name));
  5027. } else if (!is_type_struct(operand->type)) {
  5028. error(operand->expr, "Expected a struct type for '%.*s'", LIT(builtin_name));
  5029. } else {
  5030. Type *bt = base_type(operand->type);
  5031. operand->value = exact_value_i64(bt->Struct.fields.count);
  5032. }
  5033. operand->mode = Addressing_Constant;
  5034. operand->type = t_untyped_integer;
  5035. break;
  5036. case BuiltinProc_type_proc_parameter_count:
  5037. operand->value = exact_value_i64(0);
  5038. if (operand->mode != Addressing_Type) {
  5039. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5040. } else if (!is_type_proc(operand->type)) {
  5041. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5042. } else {
  5043. Type *bt = base_type(operand->type);
  5044. operand->value = exact_value_i64(bt->Proc.param_count);
  5045. }
  5046. operand->mode = Addressing_Constant;
  5047. operand->type = t_untyped_integer;
  5048. break;
  5049. case BuiltinProc_type_proc_return_count:
  5050. operand->value = exact_value_i64(0);
  5051. if (operand->mode != Addressing_Type) {
  5052. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5053. } else if (!is_type_proc(operand->type)) {
  5054. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5055. } else {
  5056. Type *bt = base_type(operand->type);
  5057. operand->value = exact_value_i64(bt->Proc.result_count);
  5058. }
  5059. operand->mode = Addressing_Constant;
  5060. operand->type = t_untyped_integer;
  5061. break;
  5062. case BuiltinProc_type_proc_parameter_type:
  5063. if (operand->mode != Addressing_Type || !is_type_proc(operand->type)) {
  5064. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5065. return false;
  5066. } else {
  5067. if (is_type_polymorphic(operand->type)) {
  5068. error(operand->expr, "Expected a non-polymorphic procedure type for '%.*s'", LIT(builtin_name));
  5069. return false;
  5070. }
  5071. Operand op = {};
  5072. check_expr(c, &op, ce->args[1]);
  5073. if (op.mode != Addressing_Constant && !is_type_integer(op.type)) {
  5074. error(op.expr, "Expected a constant integer for the index of procedure parameter value");
  5075. return false;
  5076. }
  5077. i64 index = exact_value_to_i64(op.value);
  5078. if (index < 0) {
  5079. error(op.expr, "Expected a non-negative integer for the index of procedure parameter value, got %lld", cast(long long)index);
  5080. return false;
  5081. }
  5082. Entity *param = nullptr;
  5083. i64 count = 0;
  5084. Type *bt = base_type(operand->type);
  5085. if (bt->kind == Type_Proc) {
  5086. count = bt->Proc.param_count;
  5087. if (index < count) {
  5088. param = bt->Proc.params->Tuple.variables[index];
  5089. }
  5090. }
  5091. if (index >= count) {
  5092. error(op.expr, "Index of procedure parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  5093. return false;
  5094. }
  5095. GB_ASSERT(param != nullptr);
  5096. switch (param->kind) {
  5097. case Entity_Constant:
  5098. operand->mode = Addressing_Constant;
  5099. operand->type = param->type;
  5100. operand->value = param->Constant.value;
  5101. break;
  5102. case Entity_TypeName:
  5103. case Entity_Variable:
  5104. operand->mode = Addressing_Type;
  5105. operand->type = param->type;
  5106. break;
  5107. default:
  5108. GB_PANIC("Unhandled procedure entity type %d", param->kind);
  5109. break;
  5110. }
  5111. }
  5112. break;
  5113. case BuiltinProc_type_proc_return_type:
  5114. if (operand->mode != Addressing_Type || !is_type_proc(operand->type)) {
  5115. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5116. return false;
  5117. } else {
  5118. if (is_type_polymorphic(operand->type)) {
  5119. error(operand->expr, "Expected a non-polymorphic procedure type for '%.*s'", LIT(builtin_name));
  5120. return false;
  5121. }
  5122. Operand op = {};
  5123. check_expr(c, &op, ce->args[1]);
  5124. if (op.mode != Addressing_Constant && !is_type_integer(op.type)) {
  5125. error(op.expr, "Expected a constant integer for the index of procedure parameter value");
  5126. return false;
  5127. }
  5128. i64 index = exact_value_to_i64(op.value);
  5129. if (index < 0) {
  5130. error(op.expr, "Expected a non-negative integer for the index of procedure parameter value, got %lld", cast(long long)index);
  5131. return false;
  5132. }
  5133. Entity *param = nullptr;
  5134. i64 count = 0;
  5135. Type *bt = base_type(operand->type);
  5136. if (bt->kind == Type_Proc) {
  5137. count = bt->Proc.result_count;
  5138. if (index < count) {
  5139. param = bt->Proc.results->Tuple.variables[index];
  5140. }
  5141. }
  5142. if (index >= count) {
  5143. error(op.expr, "Index of procedure parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  5144. return false;
  5145. }
  5146. GB_ASSERT(param != nullptr);
  5147. switch (param->kind) {
  5148. case Entity_Constant:
  5149. operand->mode = Addressing_Constant;
  5150. operand->type = param->type;
  5151. operand->value = param->Constant.value;
  5152. break;
  5153. case Entity_TypeName:
  5154. case Entity_Variable:
  5155. operand->mode = Addressing_Type;
  5156. operand->type = param->type;
  5157. break;
  5158. default:
  5159. GB_PANIC("Unhandled procedure entity type %d", param->kind);
  5160. break;
  5161. }
  5162. }
  5163. break;
  5164. case BuiltinProc_type_polymorphic_record_parameter_count:
  5165. operand->value = exact_value_i64(0);
  5166. if (operand->mode != Addressing_Type) {
  5167. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  5168. } else {
  5169. Type *bt = base_type(operand->type);
  5170. if (bt->kind == Type_Struct) {
  5171. if (bt->Struct.polymorphic_params != nullptr) {
  5172. operand->value = exact_value_i64(bt->Struct.polymorphic_params->Tuple.variables.count);
  5173. }
  5174. } else if (bt->kind == Type_Union) {
  5175. if (bt->Union.polymorphic_params != nullptr) {
  5176. operand->value = exact_value_i64(bt->Union.polymorphic_params->Tuple.variables.count);
  5177. }
  5178. } else {
  5179. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  5180. }
  5181. }
  5182. operand->mode = Addressing_Constant;
  5183. operand->type = t_untyped_integer;
  5184. break;
  5185. case BuiltinProc_type_polymorphic_record_parameter_value:
  5186. if (operand->mode != Addressing_Type) {
  5187. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  5188. return false;
  5189. } else if (!is_type_polymorphic_record_specialized(operand->type)) {
  5190. error(operand->expr, "Expected a specialized polymorphic record type for '%.*s'", LIT(builtin_name));
  5191. return false;
  5192. } else {
  5193. Operand op = {};
  5194. check_expr(c, &op, ce->args[1]);
  5195. if (op.mode != Addressing_Constant && !is_type_integer(op.type)) {
  5196. error(op.expr, "Expected a constant integer for the index of record parameter value");
  5197. return false;
  5198. }
  5199. i64 index = exact_value_to_i64(op.value);
  5200. if (index < 0) {
  5201. error(op.expr, "Expected a non-negative integer for the index of record parameter value, got %lld", cast(long long)index);
  5202. return false;
  5203. }
  5204. Entity *param = nullptr;
  5205. i64 count = 0;
  5206. Type *bt = base_type(operand->type);
  5207. if (bt->kind == Type_Struct) {
  5208. if (bt->Struct.polymorphic_params != nullptr) {
  5209. count = bt->Struct.polymorphic_params->Tuple.variables.count;
  5210. if (index < count) {
  5211. param = bt->Struct.polymorphic_params->Tuple.variables[cast(isize)index];
  5212. }
  5213. }
  5214. } else if (bt->kind == Type_Union) {
  5215. if (bt->Union.polymorphic_params != nullptr) {
  5216. count = bt->Union.polymorphic_params->Tuple.variables.count;
  5217. if (index < count) {
  5218. param = bt->Union.polymorphic_params->Tuple.variables[cast(isize)index];
  5219. }
  5220. }
  5221. } else {
  5222. error(operand->expr, "Expected a specialized polymorphic record type for '%.*s'", LIT(builtin_name));
  5223. return false;
  5224. }
  5225. if (index >= count) {
  5226. error(op.expr, "Index of record parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  5227. return false;
  5228. }
  5229. GB_ASSERT(param != nullptr);
  5230. switch (param->kind) {
  5231. case Entity_Constant:
  5232. operand->mode = Addressing_Constant;
  5233. operand->type = param->type;
  5234. operand->value = param->Constant.value;
  5235. break;
  5236. case Entity_TypeName:
  5237. operand->mode = Addressing_Type;
  5238. operand->type = param->type;
  5239. break;
  5240. default:
  5241. GB_PANIC("Unhandled polymorphic record type");
  5242. break;
  5243. }
  5244. }
  5245. break;
  5246. case BuiltinProc_type_field_index_of:
  5247. {
  5248. Operand op = {};
  5249. Type *bt = check_type(c, ce->args[0]);
  5250. Type *type = base_type(bt);
  5251. if (type == nullptr || type == t_invalid) {
  5252. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  5253. return false;
  5254. }
  5255. Operand x = {};
  5256. check_expr(c, &x, ce->args[1]);
  5257. if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
  5258. error(ce->args[1], "Expected a const string for field argument");
  5259. return false;
  5260. }
  5261. String field_name = x.value.value_string;
  5262. Selection sel = lookup_field(type, field_name, false);
  5263. if (sel.entity == nullptr) {
  5264. gbString type_str = type_to_string(bt);
  5265. error(ce->args[0],
  5266. "'%s' has no field named '%.*s'", type_str, LIT(field_name));
  5267. gb_string_free(type_str);
  5268. return false;
  5269. }
  5270. if (sel.indirect) {
  5271. gbString type_str = type_to_string(bt);
  5272. error(ce->args[0],
  5273. "Field '%.*s' is embedded via a pointer in '%s'", LIT(field_name), type_str);
  5274. gb_string_free(type_str);
  5275. return false;
  5276. }
  5277. operand->mode = Addressing_Constant;
  5278. operand->value = exact_value_u64(sel.index[0]);
  5279. operand->type = t_uintptr;
  5280. break;
  5281. }
  5282. break;
  5283. case BuiltinProc_type_equal_proc:
  5284. {
  5285. Operand op = {};
  5286. Type *bt = check_type(c, ce->args[0]);
  5287. Type *type = base_type(bt);
  5288. if (type == nullptr || type == t_invalid) {
  5289. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  5290. return false;
  5291. }
  5292. if (!is_type_comparable(type)) {
  5293. gbString t = type_to_string(type);
  5294. error(ce->args[0], "Expected a comparable type for '%.*s', got %s", LIT(builtin_name), t);
  5295. gb_string_free(t);
  5296. return false;
  5297. }
  5298. operand->mode = Addressing_Value;
  5299. operand->type = t_equal_proc;
  5300. break;
  5301. }
  5302. case BuiltinProc_type_hasher_proc:
  5303. {
  5304. Operand op = {};
  5305. Type *bt = check_type(c, ce->args[0]);
  5306. Type *type = base_type(bt);
  5307. if (type == nullptr || type == t_invalid) {
  5308. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  5309. return false;
  5310. }
  5311. if (!is_type_valid_for_keys(type)) {
  5312. gbString t = type_to_string(type);
  5313. error(ce->args[0], "Expected a valid type for map keys for '%.*s', got %s", LIT(builtin_name), t);
  5314. gb_string_free(t);
  5315. return false;
  5316. }
  5317. add_map_key_type_dependencies(c, type);
  5318. operand->mode = Addressing_Value;
  5319. operand->type = t_hasher_proc;
  5320. break;
  5321. }
  5322. }
  5323. return true;
  5324. }
  5325. isize add_dependencies_from_unpacking(CheckerContext *c, Entity **lhs, isize lhs_count, isize tuple_index, isize tuple_count) {
  5326. if (lhs != nullptr) {
  5327. for (isize j = 0; (tuple_index + j) < lhs_count && j < tuple_count; j++) {
  5328. Entity *e = lhs[tuple_index + j];
  5329. if (e != nullptr) {
  5330. DeclInfo *decl = decl_info_of_entity(e);
  5331. if (decl != nullptr) {
  5332. c->decl = decl; // will be reset by the 'defer' any way
  5333. for_array(k, decl->deps.entries) {
  5334. Entity *dep = decl->deps.entries[k].ptr;
  5335. add_declaration_dependency(c, dep); // TODO(bill): Should this be here?
  5336. }
  5337. }
  5338. }
  5339. }
  5340. }
  5341. return tuple_count;
  5342. }
  5343. bool check_assignment_arguments(CheckerContext *ctx, Array<Operand> const &lhs, Array<Operand> *operands, Slice<Ast *> const &rhs) {
  5344. bool optional_ok = false;
  5345. isize tuple_index = 0;
  5346. for_array(i, rhs) {
  5347. CheckerContext c_ = *ctx;
  5348. CheckerContext *c = &c_;
  5349. Operand o = {};
  5350. Type *type_hint = nullptr;
  5351. if (tuple_index < lhs.count) {
  5352. type_hint = lhs[tuple_index].type;
  5353. }
  5354. check_expr_base(c, &o, rhs[i], type_hint);
  5355. if (o.mode == Addressing_NoValue) {
  5356. error_operand_no_value(&o);
  5357. o.mode = Addressing_Invalid;
  5358. }
  5359. if (o.type == nullptr || o.type->kind != Type_Tuple) {
  5360. if (lhs.count == 2 && rhs.count == 1 &&
  5361. (o.mode == Addressing_MapIndex || o.mode == Addressing_OptionalOk)) {
  5362. Type *tuple = make_optional_ok_type(o.type);
  5363. add_type_and_value(&c->checker->info, o.expr, o.mode, tuple, o.value);
  5364. Operand val = o;
  5365. Operand ok = o;
  5366. val.mode = Addressing_Value;
  5367. ok.mode = Addressing_Value;
  5368. ok.type = t_untyped_bool;
  5369. array_add(operands, val);
  5370. array_add(operands, ok);
  5371. optional_ok = true;
  5372. tuple_index += 2;
  5373. } else if (o.mode == Addressing_OptionalOk && is_type_tuple(o.type)) {
  5374. Type *tuple = o.type;
  5375. GB_ASSERT(tuple->Tuple.variables.count == 2);
  5376. Ast *expr = unparen_expr(o.expr);
  5377. if (expr->kind == Ast_CallExpr) {
  5378. expr->CallExpr.optional_ok_one = true;
  5379. }
  5380. Operand val = o;
  5381. val.type = tuple->Tuple.variables[0]->type;
  5382. val.mode = Addressing_Value;
  5383. array_add(operands, val);
  5384. tuple_index += tuple->Tuple.variables.count;
  5385. } else {
  5386. array_add(operands, o);
  5387. tuple_index += 1;
  5388. }
  5389. } else {
  5390. TypeTuple *tuple = &o.type->Tuple;
  5391. if (o.mode == Addressing_OptionalOk && is_type_tuple(o.type) && lhs.count == 1) {
  5392. GB_ASSERT(tuple->variables.count == 2);
  5393. Ast *expr = unparen_expr(o.expr);
  5394. if (expr->kind == Ast_CallExpr) {
  5395. expr->CallExpr.optional_ok_one = true;
  5396. }
  5397. Operand val = o;
  5398. val.type = tuple->variables[0]->type;
  5399. val.mode = Addressing_Value;
  5400. array_add(operands, val);
  5401. tuple_index += tuple->variables.count;
  5402. add_type_and_value(c->info, val.expr, val.mode, val.type, val.value);
  5403. } else {
  5404. for_array(j, tuple->variables) {
  5405. o.type = tuple->variables[j]->type;
  5406. array_add(operands, o);
  5407. }
  5408. tuple_index += tuple->variables.count;
  5409. }
  5410. }
  5411. }
  5412. return optional_ok;
  5413. }
  5414. bool check_unpack_arguments(CheckerContext *ctx, Entity **lhs, isize lhs_count, Array<Operand> *operands, Slice<Ast *> const &rhs, bool allow_ok, bool is_variadic) {
  5415. bool optional_ok = false;
  5416. isize tuple_index = 0;
  5417. for_array(i, rhs) {
  5418. CheckerContext c_ = *ctx;
  5419. CheckerContext *c = &c_;
  5420. Operand o = {};
  5421. Type *type_hint = nullptr;
  5422. if (lhs != nullptr && tuple_index < lhs_count) {
  5423. // NOTE(bill): override DeclInfo for dependency
  5424. Entity *e = lhs[tuple_index];
  5425. if (e != nullptr) {
  5426. // DeclInfo *decl = decl_info_of_entity(e);
  5427. // if (decl) c->decl = decl;
  5428. type_hint = e->type;
  5429. if (e->flags & EntityFlag_Ellipsis) {
  5430. GB_ASSERT(is_type_slice(e->type));
  5431. GB_ASSERT(e->type->kind == Type_Slice);
  5432. type_hint = e->type->Slice.elem;
  5433. }
  5434. }
  5435. } else if (lhs != nullptr && tuple_index >= lhs_count && is_variadic) {
  5436. // NOTE(bill): override DeclInfo for dependency
  5437. Entity *e = lhs[lhs_count-1];
  5438. if (e != nullptr) {
  5439. // DeclInfo *decl = decl_info_of_entity(e);
  5440. // if (decl) c->decl = decl;
  5441. type_hint = e->type;
  5442. if (e->flags & EntityFlag_Ellipsis) {
  5443. GB_ASSERT(is_type_slice(e->type));
  5444. GB_ASSERT(e->type->kind == Type_Slice);
  5445. type_hint = e->type->Slice.elem;
  5446. }
  5447. }
  5448. }
  5449. check_expr_base(c, &o, rhs[i], type_hint);
  5450. if (o.mode == Addressing_NoValue) {
  5451. error_operand_no_value(&o);
  5452. o.mode = Addressing_Invalid;
  5453. }
  5454. if (o.type == nullptr || o.type->kind != Type_Tuple) {
  5455. if (allow_ok && lhs_count == 2 && rhs.count == 1 &&
  5456. (o.mode == Addressing_MapIndex || o.mode == Addressing_OptionalOk)) {
  5457. Type *tuple = make_optional_ok_type(o.type);
  5458. add_type_and_value(&c->checker->info, o.expr, o.mode, tuple, o.value);
  5459. Operand val = o;
  5460. Operand ok = o;
  5461. val.mode = Addressing_Value;
  5462. ok.mode = Addressing_Value;
  5463. // ok.type = t_bool;
  5464. ok.type = t_untyped_bool;
  5465. array_add(operands, val);
  5466. array_add(operands, ok);
  5467. optional_ok = true;
  5468. tuple_index += add_dependencies_from_unpacking(c, lhs, lhs_count, tuple_index, 2);
  5469. } else {
  5470. array_add(operands, o);
  5471. tuple_index += 1;
  5472. }
  5473. } else {
  5474. TypeTuple *tuple = &o.type->Tuple;
  5475. if (o.mode == Addressing_OptionalOk && lhs_count == 1) {
  5476. GB_ASSERT(tuple->variables.count == 2);
  5477. Ast *expr = unparen_expr(o.expr);
  5478. if (expr->kind == Ast_CallExpr) {
  5479. expr->CallExpr.optional_ok_one = true;
  5480. }
  5481. Operand val = o;
  5482. val.type = tuple->variables[0]->type;
  5483. val.mode = Addressing_Value;
  5484. array_add(operands, val);
  5485. isize count = tuple->variables.count;
  5486. tuple_index += add_dependencies_from_unpacking(c, lhs, lhs_count, tuple_index, count);
  5487. add_type_and_value(c->info, val.expr, val.mode, val.type, val.value);
  5488. } else {
  5489. for_array(j, tuple->variables) {
  5490. o.type = tuple->variables[j]->type;
  5491. array_add(operands, o);
  5492. }
  5493. isize count = tuple->variables.count;
  5494. tuple_index += add_dependencies_from_unpacking(c, lhs, lhs_count, tuple_index, count);
  5495. }
  5496. }
  5497. }
  5498. return optional_ok;
  5499. }
  5500. CALL_ARGUMENT_CHECKER(check_call_arguments_internal) {
  5501. ast_node(ce, CallExpr, call);
  5502. GB_ASSERT(is_type_proc(proc_type));
  5503. proc_type = base_type(proc_type);
  5504. TypeProc *pt = &proc_type->Proc;
  5505. isize param_count = 0;
  5506. isize param_count_excluding_defaults = 0;
  5507. bool variadic = pt->variadic;
  5508. bool vari_expand = (ce->ellipsis.pos.line != 0);
  5509. i64 score = 0;
  5510. bool show_error = show_error_mode == CallArgumentMode_ShowErrors;
  5511. TypeTuple *param_tuple = nullptr;
  5512. if (pt->params != nullptr) {
  5513. param_tuple = &pt->params->Tuple;
  5514. param_count = param_tuple->variables.count;
  5515. if (variadic) {
  5516. for (isize i = param_count-1; i >= 0; i--) {
  5517. Entity *e = param_tuple->variables[i];
  5518. if (e->kind == Entity_TypeName) {
  5519. break;
  5520. }
  5521. if (e->kind == Entity_Variable) {
  5522. if (e->Variable.param_value.kind != ParameterValue_Invalid) {
  5523. param_count--;
  5524. continue;
  5525. }
  5526. }
  5527. break;
  5528. }
  5529. param_count--;
  5530. }
  5531. }
  5532. param_count_excluding_defaults = param_count;
  5533. if (param_tuple != nullptr) {
  5534. for (isize i = param_count-1; i >= 0; i--) {
  5535. Entity *e = param_tuple->variables[i];
  5536. if (e->kind == Entity_TypeName) {
  5537. break;
  5538. }
  5539. if (e->kind == Entity_Variable) {
  5540. if (e->Variable.param_value.kind != ParameterValue_Invalid) {
  5541. param_count_excluding_defaults--;
  5542. continue;
  5543. }
  5544. }
  5545. break;
  5546. }
  5547. }
  5548. CallArgumentError err = CallArgumentError_None;
  5549. Type *final_proc_type = proc_type;
  5550. Entity *gen_entity = nullptr;
  5551. if (vari_expand && !variadic) {
  5552. if (show_error) {
  5553. error(ce->ellipsis,
  5554. "Cannot use '..' in call to a non-variadic procedure: '%.*s'",
  5555. LIT(ce->proc->Ident.token.string));
  5556. }
  5557. err = CallArgumentError_NonVariadicExpand;
  5558. } else if (vari_expand && pt->c_vararg) {
  5559. if (show_error) {
  5560. error(ce->ellipsis,
  5561. "Cannot use '..' in call to a '#c_vararg' variadic procedure: '%.*s'",
  5562. LIT(ce->proc->Ident.token.string));
  5563. }
  5564. err = CallArgumentError_NonVariadicExpand;
  5565. } else if (operands.count == 0 && param_count_excluding_defaults == 0) {
  5566. err = CallArgumentError_None;
  5567. if (variadic) {
  5568. GB_ASSERT(param_tuple != nullptr && param_tuple->variables.count > 0);
  5569. Type *t = param_tuple->variables[0]->type;
  5570. if (is_type_polymorphic(t)) {
  5571. error(call, "Ambiguous call to a polymorphic variadic procedure with no variadic input");
  5572. err = CallArgumentError_AmbiguousPolymorphicVariadic;
  5573. }
  5574. }
  5575. } else {
  5576. i32 error_code = 0;
  5577. if (operands.count < param_count_excluding_defaults) {
  5578. error_code = -1;
  5579. } else if (!variadic && operands.count > param_count) {
  5580. error_code = +1;
  5581. }
  5582. if (error_code != 0) {
  5583. err = CallArgumentError_TooManyArguments;
  5584. char const *err_fmt = "Too many arguments for '%s', expected %td arguments";
  5585. if (error_code < 0) {
  5586. err = CallArgumentError_TooFewArguments;
  5587. err_fmt = "Too few arguments for '%s', expected %td arguments";
  5588. }
  5589. if (show_error) {
  5590. gbString proc_str = expr_to_string(ce->proc);
  5591. error(call, err_fmt, proc_str, param_count_excluding_defaults);
  5592. gb_string_free(proc_str);
  5593. }
  5594. } else {
  5595. // NOTE(bill): Generate the procedure type for this generic instance
  5596. PolyProcData poly_proc_data = {};
  5597. if (pt->is_polymorphic && !pt->is_poly_specialized) {
  5598. if (find_or_generate_polymorphic_procedure_from_parameters(c, entity, &operands, call, &poly_proc_data)) {
  5599. gen_entity = poly_proc_data.gen_entity;
  5600. GB_ASSERT(is_type_proc(gen_entity->type));
  5601. final_proc_type = gen_entity->type;
  5602. } else {
  5603. err = CallArgumentError_WrongTypes;
  5604. }
  5605. }
  5606. GB_ASSERT(is_type_proc(final_proc_type));
  5607. TypeProc *pt = &final_proc_type->Proc;
  5608. GB_ASSERT(pt->params != nullptr);
  5609. auto sig_params = pt->params->Tuple.variables;
  5610. isize operand_index = 0;
  5611. isize max_operand_count = gb_min(param_count, operands.count);
  5612. for (; operand_index < max_operand_count; operand_index++) {
  5613. Entity *e = sig_params[operand_index];
  5614. Type *t = e->type;
  5615. Operand o = operands[operand_index];
  5616. if (o.expr != nullptr) {
  5617. call->viral_state_flags |= o.expr->viral_state_flags;
  5618. }
  5619. if (e->kind == Entity_TypeName) {
  5620. // GB_ASSERT(!variadic);
  5621. if (o.mode == Addressing_Invalid) {
  5622. continue;
  5623. } else if (o.mode != Addressing_Type) {
  5624. if (show_error) {
  5625. error(o.expr, "Expected a type for the argument '%.*s'", LIT(e->token.string));
  5626. }
  5627. err = CallArgumentError_WrongTypes;
  5628. }
  5629. if (are_types_identical(e->type, o.type)) {
  5630. score += assign_score_function(1);
  5631. } else {
  5632. score += assign_score_function(MAXIMUM_TYPE_DISTANCE);
  5633. }
  5634. continue;
  5635. }
  5636. bool param_is_variadic = pt->variadic && pt->variadic_index == operand_index;
  5637. i64 s = 0;
  5638. if (!check_is_assignable_to_with_score(c, &o, t, &s, param_is_variadic)) {
  5639. bool ok = false;
  5640. if (e->flags & EntityFlag_AutoCast) {
  5641. ok = check_is_castable_to(c, &o, t);
  5642. }
  5643. if (ok) {
  5644. s = assign_score_function(MAXIMUM_TYPE_DISTANCE);
  5645. } else {
  5646. if (show_error) {
  5647. check_assignment(c, &o, t, str_lit("argument"));
  5648. }
  5649. err = CallArgumentError_WrongTypes;
  5650. }
  5651. }
  5652. score += s;
  5653. if (e->flags & EntityFlag_ConstInput) {
  5654. if (o.mode != Addressing_Constant) {
  5655. if (show_error) {
  5656. error(o.expr, "Expected a constant value for the argument '%.*s'", LIT(e->token.string));
  5657. }
  5658. err = CallArgumentError_NoneConstantParameter;
  5659. }
  5660. }
  5661. if (o.mode == Addressing_Type && is_type_typeid(e->type)) {
  5662. add_type_info_type(c, o.type);
  5663. add_type_and_value(c->info, o.expr, Addressing_Value, e->type, exact_value_typeid(o.type));
  5664. }
  5665. }
  5666. if (variadic) {
  5667. bool variadic_expand = false;
  5668. Type *slice = sig_params[param_count]->type;
  5669. GB_ASSERT(is_type_slice(slice));
  5670. Type *elem = base_type(slice)->Slice.elem;
  5671. Type *t = elem;
  5672. if (is_type_polymorphic(t)) {
  5673. error(call, "Ambiguous call to a polymorphic variadic procedure with no variadic input");
  5674. err = CallArgumentError_AmbiguousPolymorphicVariadic;
  5675. }
  5676. for (; operand_index < operands.count; operand_index++) {
  5677. Operand o = operands[operand_index];
  5678. if (vari_expand) {
  5679. variadic_expand = true;
  5680. t = slice;
  5681. if (operand_index != param_count) {
  5682. if (show_error) {
  5683. error(o.expr, "'..' in a variadic procedure can only have one variadic argument at the end");
  5684. }
  5685. if (data) {
  5686. data->score = score;
  5687. data->result_type = final_proc_type->Proc.results;
  5688. data->gen_entity = gen_entity;
  5689. }
  5690. return CallArgumentError_MultipleVariadicExpand;
  5691. }
  5692. }
  5693. i64 s = 0;
  5694. if (!check_is_assignable_to_with_score(c, &o, t, &s, true)) {
  5695. if (show_error) {
  5696. check_assignment(c, &o, t, str_lit("argument"));
  5697. }
  5698. err = CallArgumentError_WrongTypes;
  5699. }
  5700. score += s;
  5701. if (is_type_any(elem)) {
  5702. add_type_info_type(c, o.type);
  5703. }
  5704. if (o.mode == Addressing_Type && is_type_typeid(t)) {
  5705. add_type_info_type(c, o.type);
  5706. add_type_and_value(c->info, o.expr, Addressing_Value, t, exact_value_typeid(o.type));
  5707. }
  5708. }
  5709. }
  5710. }
  5711. }
  5712. if (data) {
  5713. data->score = score;
  5714. data->result_type = final_proc_type->Proc.results;
  5715. data->gen_entity = gen_entity;
  5716. }
  5717. return err;
  5718. }
  5719. bool is_call_expr_field_value(AstCallExpr *ce) {
  5720. GB_ASSERT(ce != nullptr);
  5721. if (ce->args.count == 0) {
  5722. return false;
  5723. }
  5724. return ce->args[0]->kind == Ast_FieldValue;
  5725. }
  5726. isize lookup_procedure_parameter(TypeProc *pt, String parameter_name) {
  5727. isize param_count = pt->param_count;
  5728. for (isize i = 0; i < param_count; i++) {
  5729. Entity *e = pt->params->Tuple.variables[i];
  5730. String name = e->token.string;
  5731. if (is_blank_ident(name)) {
  5732. continue;
  5733. }
  5734. if (name == parameter_name) {
  5735. return i;
  5736. }
  5737. }
  5738. return -1;
  5739. }
  5740. isize lookup_procedure_result(TypeProc *pt, String result_name) {
  5741. isize result_count = pt->result_count;
  5742. for (isize i = 0; i < result_count; i++) {
  5743. Entity *e = pt->results->Tuple.variables[i];
  5744. String name = e->token.string;
  5745. if (is_blank_ident(name)) {
  5746. continue;
  5747. }
  5748. if (name == result_name) {
  5749. return i;
  5750. }
  5751. }
  5752. return -1;
  5753. }
  5754. CALL_ARGUMENT_CHECKER(check_named_call_arguments) {
  5755. ast_node(ce, CallExpr, call);
  5756. GB_ASSERT(is_type_proc(proc_type));
  5757. proc_type = base_type(proc_type);
  5758. TypeProc *pt = &proc_type->Proc;
  5759. i64 score = 0;
  5760. bool show_error = show_error_mode == CallArgumentMode_ShowErrors;
  5761. CallArgumentError err = CallArgumentError_None;
  5762. isize param_count = pt->param_count;
  5763. bool *visited = gb_alloc_array(temporary_allocator(), bool, param_count);
  5764. auto ordered_operands = array_make<Operand>(temporary_allocator(), param_count);
  5765. defer ({
  5766. for_array(i, ordered_operands) {
  5767. Operand const &o = ordered_operands[i];
  5768. if (o.expr != nullptr) {
  5769. call->viral_state_flags |= o.expr->viral_state_flags;
  5770. }
  5771. }
  5772. });
  5773. for_array(i, ce->args) {
  5774. Ast *arg = ce->args[i];
  5775. ast_node(fv, FieldValue, arg);
  5776. if (fv->field->kind != Ast_Ident) {
  5777. if (show_error) {
  5778. gbString expr_str = expr_to_string(fv->field);
  5779. error(arg, "Invalid parameter name '%s' in procedure call", expr_str);
  5780. gb_string_free(expr_str);
  5781. }
  5782. err = CallArgumentError_InvalidFieldValue;
  5783. continue;
  5784. }
  5785. String name = fv->field->Ident.token.string;
  5786. isize index = lookup_procedure_parameter(pt, name);
  5787. if (index < 0) {
  5788. if (show_error) {
  5789. error(arg, "No parameter named '%.*s' for this procedure type", LIT(name));
  5790. }
  5791. err = CallArgumentError_ParameterNotFound;
  5792. continue;
  5793. }
  5794. if (visited[index]) {
  5795. if (show_error) {
  5796. error(arg, "Duplicate parameter '%.*s' in procedure call", LIT(name));
  5797. }
  5798. err = CallArgumentError_DuplicateParameter;
  5799. continue;
  5800. }
  5801. visited[index] = true;
  5802. ordered_operands[index] = operands[i];
  5803. }
  5804. // NOTE(bill): Check for default values and missing parameters
  5805. isize param_count_to_check = param_count;
  5806. if (pt->variadic) {
  5807. param_count_to_check--;
  5808. }
  5809. for (isize i = 0; i < param_count_to_check; i++) {
  5810. if (!visited[i]) {
  5811. Entity *e = pt->params->Tuple.variables[i];
  5812. if (is_blank_ident(e->token)) {
  5813. continue;
  5814. }
  5815. if (e->kind == Entity_Variable) {
  5816. if (e->Variable.param_value.kind != ParameterValue_Invalid) {
  5817. score += assign_score_function(1);
  5818. continue;
  5819. }
  5820. }
  5821. if (show_error) {
  5822. if (e->kind == Entity_TypeName) {
  5823. error(call, "Type parameter '%.*s' is missing in procedure call",
  5824. LIT(e->token.string));
  5825. } else if (e->kind == Entity_Constant && e->Constant.value.kind != ExactValue_Invalid) {
  5826. // Ignore
  5827. } else {
  5828. gbString str = type_to_string(e->type);
  5829. error(call, "Parameter '%.*s' of type '%s' is missing in procedure call",
  5830. LIT(e->token.string), str);
  5831. gb_string_free(str);
  5832. }
  5833. }
  5834. err = CallArgumentError_ParameterMissing;
  5835. }
  5836. }
  5837. Entity *gen_entity = nullptr;
  5838. if (pt->is_polymorphic && !pt->is_poly_specialized && err == CallArgumentError_None) {
  5839. PolyProcData poly_proc_data = {};
  5840. if (find_or_generate_polymorphic_procedure_from_parameters(c, entity, &ordered_operands, call, &poly_proc_data)) {
  5841. gen_entity = poly_proc_data.gen_entity;
  5842. Type *gept = base_type(gen_entity->type);
  5843. GB_ASSERT(is_type_proc(gept));
  5844. proc_type = gept;
  5845. pt = &gept->Proc;
  5846. }
  5847. }
  5848. for (isize i = 0; i < param_count; i++) {
  5849. Entity *e = pt->params->Tuple.variables[i];
  5850. Operand *o = &ordered_operands[i];
  5851. bool param_is_variadic = pt->variadic && pt->variadic_index == i;
  5852. if (o->mode == Addressing_Invalid) {
  5853. if (param_is_variadic) {
  5854. Type *slice = e->type;
  5855. GB_ASSERT(is_type_slice(slice));
  5856. Type *elem = base_type(slice)->Slice.elem;
  5857. if (is_type_polymorphic(elem)) {
  5858. error(call, "Ambiguous call to a polymorphic variadic procedure with no variadic input");
  5859. err = CallArgumentError_AmbiguousPolymorphicVariadic;
  5860. return err;
  5861. }
  5862. }
  5863. continue;
  5864. }
  5865. if (e->kind == Entity_TypeName) {
  5866. GB_ASSERT(pt->is_polymorphic);
  5867. if (o->mode != Addressing_Type) {
  5868. if (show_error) {
  5869. error(o->expr, "Expected a type for the argument '%.*s'", LIT(e->token.string));
  5870. }
  5871. err = CallArgumentError_WrongTypes;
  5872. }
  5873. if (are_types_identical(e->type, o->type)) {
  5874. score += assign_score_function(1);
  5875. } else {
  5876. score += assign_score_function(MAXIMUM_TYPE_DISTANCE);
  5877. }
  5878. } else {
  5879. i64 s = 0;
  5880. if (!check_is_assignable_to_with_score(c, o, e->type, &s, param_is_variadic)) {
  5881. bool ok = false;
  5882. if (e->flags & EntityFlag_AutoCast) {
  5883. ok = check_is_castable_to(c, o, e->type);
  5884. }
  5885. if (ok) {
  5886. s = assign_score_function(MAXIMUM_TYPE_DISTANCE);
  5887. } else {
  5888. if (show_error) {
  5889. check_assignment(c, o, e->type, str_lit("procedure argument"));
  5890. }
  5891. err = CallArgumentError_WrongTypes;
  5892. }
  5893. if (e->flags & EntityFlag_ConstInput) {
  5894. if (o->mode != Addressing_Constant) {
  5895. if (show_error) {
  5896. error(o->expr, "Expected a constant value for the argument '%.*s'", LIT(e->token.string));
  5897. }
  5898. err = CallArgumentError_NoneConstantParameter;
  5899. }
  5900. }
  5901. }
  5902. score += s;
  5903. }
  5904. if (o->mode == Addressing_Type && is_type_typeid(e->type)) {
  5905. add_type_info_type(c, o->type);
  5906. add_type_and_value(c->info, o->expr, Addressing_Value, e->type, exact_value_typeid(o->type));
  5907. }
  5908. }
  5909. if (data) {
  5910. data->score = score;
  5911. data->result_type = pt->results;
  5912. data->gen_entity = gen_entity;
  5913. }
  5914. return err;
  5915. }
  5916. Entity **populate_proc_parameter_list(CheckerContext *c, Type *proc_type, isize *lhs_count_, bool *is_variadic) {
  5917. Entity **lhs = nullptr;
  5918. isize lhs_count = -1;
  5919. if (proc_type == nullptr) {
  5920. return nullptr;
  5921. }
  5922. GB_ASSERT(is_type_proc(proc_type));
  5923. TypeProc *pt = &base_type(proc_type)->Proc;
  5924. *is_variadic = pt->variadic;
  5925. if (!pt->is_polymorphic || pt->is_poly_specialized) {
  5926. if (pt->params != nullptr) {
  5927. lhs = pt->params->Tuple.variables.data;
  5928. lhs_count = pt->params->Tuple.variables.count;
  5929. }
  5930. } else {
  5931. // NOTE(bill): Create 'lhs' list in order to ignore parameters which are polymorphic
  5932. if (pt->params == nullptr) {
  5933. lhs_count = 0;
  5934. } else {
  5935. lhs_count = pt->params->Tuple.variables.count;
  5936. }
  5937. lhs = gb_alloc_array(heap_allocator(), Entity *, lhs_count);
  5938. for (isize i = 0; i < lhs_count; i++) {
  5939. Entity *e = pt->params->Tuple.variables[i];
  5940. if (!is_type_polymorphic(e->type)) {
  5941. lhs[i] = e;
  5942. }
  5943. }
  5944. }
  5945. if (lhs_count_) *lhs_count_ = lhs_count;
  5946. return lhs;
  5947. }
  5948. bool evaluate_where_clauses(CheckerContext *ctx, Ast *call_expr, Scope *scope, Slice<Ast *> *clauses, bool print_err) {
  5949. if (clauses != nullptr) {
  5950. for_array(i, *clauses) {
  5951. Ast *clause = (*clauses)[i];
  5952. Operand o = {};
  5953. check_expr(ctx, &o, clause);
  5954. if (o.mode != Addressing_Constant) {
  5955. if (print_err) error(clause, "'where' clauses expect a constant boolean evaluation");
  5956. if (print_err && call_expr) error(call_expr, "at caller location");
  5957. return false;
  5958. } else if (o.value.kind != ExactValue_Bool) {
  5959. if (print_err) error(clause, "'where' clauses expect a constant boolean evaluation");
  5960. if (print_err && call_expr) error(call_expr, "at caller location");
  5961. return false;
  5962. } else if (!o.value.value_bool) {
  5963. if (print_err) {
  5964. gbString str = expr_to_string(clause);
  5965. error(clause, "'where' clause evaluated to false:\n\t%s", str);
  5966. gb_string_free(str);
  5967. if (scope != nullptr) {
  5968. isize print_count = 0;
  5969. for_array(j, scope->elements.entries) {
  5970. Entity *e = scope->elements.entries[j].value;
  5971. switch (e->kind) {
  5972. case Entity_TypeName: {
  5973. if (print_count == 0) error_line("\n\tWith the following definitions:\n");
  5974. gbString str = type_to_string(e->type);
  5975. error_line("\t\t%.*s :: %s;\n", LIT(e->token.string), str);
  5976. gb_string_free(str);
  5977. print_count += 1;
  5978. break;
  5979. }
  5980. case Entity_Constant: {
  5981. if (print_count == 0) error_line("\n\tWith the following definitions:\n");
  5982. gbString str = exact_value_to_string(e->Constant.value);
  5983. if (is_type_untyped(e->type)) {
  5984. error_line("\t\t%.*s :: %s;\n", LIT(e->token.string), str);
  5985. } else {
  5986. gbString t = type_to_string(e->type);
  5987. error_line("\t\t%.*s : %s : %s;\n", LIT(e->token.string), t, str);
  5988. gb_string_free(t);
  5989. }
  5990. gb_string_free(str);
  5991. print_count += 1;
  5992. break;
  5993. }
  5994. }
  5995. }
  5996. }
  5997. if (call_expr) error(call_expr, "at caller location");
  5998. }
  5999. return false;
  6000. }
  6001. }
  6002. }
  6003. return true;
  6004. }
  6005. CallArgumentData check_call_arguments(CheckerContext *c, Operand *operand, Type *proc_type, Ast *call, Slice<Ast *> const &args) {
  6006. ast_node(ce, CallExpr, call);
  6007. CallArgumentCheckerType *call_checker = check_call_arguments_internal;
  6008. Array<Operand> operands = {};
  6009. defer (array_free(&operands));
  6010. Type *result_type = t_invalid;
  6011. if (is_call_expr_field_value(ce)) {
  6012. call_checker = check_named_call_arguments;
  6013. operands = array_make<Operand>(heap_allocator(), args.count);
  6014. // NOTE(bill): This is give type hints for the named parameters
  6015. // in order to improve the type inference system
  6016. StringMap<Type *> type_hint_map = {}; // Key: String
  6017. string_map_init(&type_hint_map, heap_allocator(), 2*args.count);
  6018. defer (string_map_destroy(&type_hint_map));
  6019. Type *ptype = nullptr;
  6020. bool single_case = true;
  6021. if (operand->mode == Addressing_ProcGroup) {
  6022. single_case = false;
  6023. Array<Entity *> procs = proc_group_entities(c, *operand);
  6024. if (procs.count == 1) {
  6025. ptype = procs[0]->type;
  6026. single_case = true;
  6027. }
  6028. } else {
  6029. ptype = proc_type;
  6030. }
  6031. if (single_case) {
  6032. Type *bptype = base_type(ptype);
  6033. if (is_type_proc(bptype)) {
  6034. TypeProc *pt = &bptype->Proc;
  6035. TypeTuple *param_tuple = nullptr;
  6036. if (pt->params != nullptr) {
  6037. param_tuple = &pt->params->Tuple;
  6038. }
  6039. if (param_tuple != nullptr) {
  6040. for_array(i, param_tuple->variables) {
  6041. Entity *e = param_tuple->variables[i];
  6042. if (is_blank_ident(e->token)) {
  6043. continue;
  6044. }
  6045. string_map_set(&type_hint_map, e->token.string, e->type);
  6046. }
  6047. }
  6048. }
  6049. } else {
  6050. Array<Entity *> procs = proc_group_entities(c, *operand);
  6051. for_array(j, procs) {
  6052. Type *proc_type = base_type(procs[j]->type);
  6053. if (is_type_proc(proc_type)) {
  6054. TypeProc *pt = &proc_type->Proc;
  6055. TypeTuple *param_tuple = nullptr;
  6056. if (pt->params != nullptr) {
  6057. param_tuple = &pt->params->Tuple;
  6058. }
  6059. if (param_tuple == nullptr) {
  6060. continue;
  6061. }
  6062. for_array(i, param_tuple->variables) {
  6063. Entity *e = param_tuple->variables[i];
  6064. if (is_blank_ident(e->token)) {
  6065. continue;
  6066. }
  6067. StringHashKey key = string_hash_string(e->token.string);
  6068. Type **found = string_map_get(&type_hint_map, key);
  6069. if (found) {
  6070. Type *t = *found;
  6071. if (t == nullptr) {
  6072. // NOTE(bill): Ambiguous named parameter across all types
  6073. continue;
  6074. }
  6075. if (are_types_identical(t, e->type)) {
  6076. // NOTE(bill): No need to set again
  6077. } else {
  6078. // NOTE(bill): Ambiguous named parameter across all types so set it to a nullptr
  6079. string_map_set(&type_hint_map, key, cast(Type *)nullptr);
  6080. }
  6081. } else {
  6082. string_map_set(&type_hint_map, key, e->type);
  6083. }
  6084. }
  6085. }
  6086. }
  6087. }
  6088. for_array(i, args) {
  6089. Ast *arg = args[i];
  6090. ast_node(fv, FieldValue, arg);
  6091. Ast *field = fv->field;
  6092. Type *type_hint = nullptr;
  6093. if (field != nullptr && field->kind == Ast_Ident) {
  6094. String key = field->Ident.token.string;
  6095. Type **found = string_map_get(&type_hint_map, key);
  6096. if (found) {
  6097. type_hint = *found;
  6098. }
  6099. }
  6100. check_expr_or_type(c, &operands[i], fv->value, type_hint);
  6101. }
  6102. } else {
  6103. operands = array_make<Operand>(heap_allocator(), 0, 2*args.count);
  6104. Entity **lhs = nullptr;
  6105. isize lhs_count = -1;
  6106. bool is_variadic = false;
  6107. if (proc_type != nullptr && is_type_proc(proc_type)) {
  6108. lhs = populate_proc_parameter_list(c, proc_type, &lhs_count, &is_variadic);
  6109. }
  6110. if (operand->mode != Addressing_ProcGroup) {
  6111. check_unpack_arguments(c, lhs, lhs_count, &operands, args, false, is_variadic);
  6112. }
  6113. }
  6114. if (operand->mode == Addressing_ProcGroup) {
  6115. check_entity_decl(c, operand->proc_group, nullptr, nullptr);
  6116. Array<Entity *> procs = proc_group_entities(c, *operand);
  6117. if (procs.count == 1) {
  6118. Ast *ident = operand->expr;
  6119. while (ident->kind == Ast_SelectorExpr) {
  6120. Ast *s = ident->SelectorExpr.selector;
  6121. ident = s;
  6122. }
  6123. Entity *e = procs[0];
  6124. Entity **lhs = nullptr;
  6125. isize lhs_count = -1;
  6126. bool is_variadic = false;
  6127. lhs = populate_proc_parameter_list(c, e->type, &lhs_count, &is_variadic);
  6128. check_unpack_arguments(c, lhs, lhs_count, &operands, args, false, is_variadic);
  6129. CallArgumentData data = {};
  6130. CallArgumentError err = call_checker(c, call, e->type, e, operands, CallArgumentMode_ShowErrors, &data);
  6131. if (err != CallArgumentError_None) {
  6132. // handle error
  6133. }
  6134. Entity *entity_to_use = data.gen_entity != nullptr ? data.gen_entity : e;
  6135. add_entity_use(c, ident, entity_to_use);
  6136. return data;
  6137. }
  6138. Entity **lhs = nullptr;
  6139. isize lhs_count = -1;
  6140. {
  6141. // NOTE(bill, 2019-07-13): This code is used to improve the type inference for procedure groups
  6142. // where the same positional parameter has the same type value (and ellipsis)
  6143. bool proc_arg_count_all_equal = true;
  6144. isize proc_arg_count = -1;
  6145. for_array(i, procs) {
  6146. Entity *p = procs[i];
  6147. Type *pt = base_type(p->type);
  6148. if (pt != nullptr && is_type_proc(pt)) {
  6149. if (proc_arg_count < 0) {
  6150. proc_arg_count = pt->Proc.param_count;
  6151. } else {
  6152. if (proc_arg_count != pt->Proc.param_count) {
  6153. proc_arg_count_all_equal = false;
  6154. break;
  6155. }
  6156. }
  6157. }
  6158. }
  6159. if (proc_arg_count >= 0 && proc_arg_count_all_equal) {
  6160. lhs_count = proc_arg_count;
  6161. if (lhs_count > 0) {
  6162. lhs = gb_alloc_array(heap_allocator(), Entity *, lhs_count);
  6163. for (isize param_index = 0; param_index < lhs_count; param_index++) {
  6164. Entity *e = nullptr;
  6165. for_array(j, procs) {
  6166. Entity *p = procs[j];
  6167. Type *pt = base_type(p->type);
  6168. if (pt != nullptr && is_type_proc(pt)) {
  6169. if (e == nullptr) {
  6170. e = pt->Proc.params->Tuple.variables[param_index];
  6171. } else {
  6172. Entity *f = pt->Proc.params->Tuple.variables[param_index];
  6173. if (e == f) {
  6174. continue;
  6175. }
  6176. if (are_types_identical(e->type, f->type)) {
  6177. bool ee = (e->flags & EntityFlag_Ellipsis) != 0;
  6178. bool fe = (f->flags & EntityFlag_Ellipsis) != 0;
  6179. if (ee == fe) {
  6180. continue;
  6181. }
  6182. }
  6183. // NOTE(bill): Entities are not close enough to be used
  6184. e = nullptr;
  6185. break;
  6186. }
  6187. }
  6188. }
  6189. lhs[param_index] = e;
  6190. }
  6191. }
  6192. }
  6193. }
  6194. check_unpack_arguments(c, lhs, lhs_count, &operands, args, false, false);
  6195. if (lhs != nullptr) {
  6196. gb_free(heap_allocator(), lhs);
  6197. }
  6198. auto valids = array_make<ValidIndexAndScore>(heap_allocator(), 0, procs.count);
  6199. defer (array_free(&valids));
  6200. auto proc_entities = array_make<Entity *>(heap_allocator(), 0, procs.count*2 + 1);
  6201. defer (array_free(&proc_entities));
  6202. for_array(i, procs) {
  6203. array_add(&proc_entities, procs[i]);
  6204. }
  6205. gbString expr_name = expr_to_string(operand->expr);
  6206. defer (gb_string_free(expr_name));
  6207. for_array(i, procs) {
  6208. Entity *p = procs[i];
  6209. Type *pt = base_type(p->type);
  6210. if (pt != nullptr && is_type_proc(pt)) {
  6211. CallArgumentError err = CallArgumentError_None;
  6212. CallArgumentData data = {};
  6213. CheckerContext ctx = *c;
  6214. ctx.no_polymorphic_errors = true;
  6215. ctx.allow_polymorphic_types = is_type_polymorphic(pt);
  6216. ctx.hide_polymorphic_errors = true;
  6217. err = call_checker(&ctx, call, pt, p, operands, CallArgumentMode_NoErrors, &data);
  6218. if (err != CallArgumentError_None) {
  6219. continue;
  6220. }
  6221. isize index = i;
  6222. if (data.gen_entity != nullptr) {
  6223. Entity *e = data.gen_entity;
  6224. DeclInfo *decl = data.gen_entity->decl_info;
  6225. ctx.scope = decl->scope;
  6226. ctx.decl = decl;
  6227. ctx.proc_name = e->token.string;
  6228. ctx.curr_proc_decl = decl;
  6229. ctx.curr_proc_sig = e->type;
  6230. GB_ASSERT(decl->proc_lit->kind == Ast_ProcLit);
  6231. if (!evaluate_where_clauses(&ctx, call, decl->scope, &decl->proc_lit->ProcLit.where_clauses, false)) {
  6232. continue;
  6233. }
  6234. array_add(&proc_entities, data.gen_entity);
  6235. index = proc_entities.count-1;
  6236. }
  6237. ValidIndexAndScore item = {};
  6238. item.index = index;
  6239. item.score = data.score;
  6240. array_add(&valids, item);
  6241. }
  6242. }
  6243. if (valids.count > 1) {
  6244. gb_sort_array(valids.data, valids.count, valid_index_and_score_cmp);
  6245. i64 best_score = valids[0].score;
  6246. Entity *best_entity = proc_entities[valids[0].index];
  6247. GB_ASSERT(best_entity != nullptr);
  6248. for (isize i = 1; i < valids.count; i++) {
  6249. if (best_score > valids[i].score) {
  6250. valids.count = i;
  6251. break;
  6252. }
  6253. if (best_entity == proc_entities[valids[i].index]) {
  6254. valids.count = i;
  6255. break;
  6256. }
  6257. }
  6258. }
  6259. if (valids.count == 0) {
  6260. begin_error_block();
  6261. defer (end_error_block());
  6262. error(operand->expr, "No procedures or ambiguous call for procedure group '%s' that match with the given arguments", expr_name);
  6263. if (operands.count == 0) {
  6264. error_line("\tNo given arguments\n");
  6265. } else {
  6266. error_line("\tGiven argument types: (");
  6267. for_array(i, operands) {
  6268. Operand o = operands[i];
  6269. if (i > 0) error_line(", ");
  6270. gbString type = type_to_string(o.type);
  6271. defer (gb_string_free(type));
  6272. error_line("%s", type);
  6273. }
  6274. error_line(")\n");
  6275. }
  6276. if (procs.count > 0) {
  6277. error_line("Did you mean to use one of the following:\n");
  6278. }
  6279. for_array(i, procs) {
  6280. Entity *proc = procs[i];
  6281. TokenPos pos = proc->token.pos;
  6282. Type *t = base_type(proc->type);
  6283. if (t == t_invalid) continue;
  6284. GB_ASSERT(t->kind == Type_Proc);
  6285. gbString pt;
  6286. defer (gb_string_free(pt));
  6287. if (t->Proc.node != nullptr) {
  6288. pt = expr_to_string(t->Proc.node);
  6289. } else {
  6290. pt = type_to_string(t);
  6291. }
  6292. String prefix = {};
  6293. String prefix_sep = {};
  6294. if (proc->pkg) {
  6295. prefix = proc->pkg->name;
  6296. prefix_sep = str_lit(".");
  6297. }
  6298. String name = proc->token.string;
  6299. char const *sep = "::";
  6300. if (proc->kind == Entity_Variable) {
  6301. sep = ":=";
  6302. }
  6303. error_line("\t%.*s%.*s%.*s %s %s at %s\n", LIT(prefix), LIT(prefix_sep), LIT(name), sep, pt, token_pos_to_string(pos));
  6304. }
  6305. if (procs.count > 0) {
  6306. error_line("\n");
  6307. }
  6308. result_type = t_invalid;
  6309. } else if (valids.count > 1) {
  6310. begin_error_block();
  6311. defer (end_error_block());
  6312. error(operand->expr, "Ambiguous procedure group call '%s' that match with the given arguments", expr_name);
  6313. error_line("\tGiven argument types: (");
  6314. for_array(i, operands) {
  6315. Operand o = operands[i];
  6316. if (i > 0) error_line(", ");
  6317. gbString type = type_to_string(o.type);
  6318. defer (gb_string_free(type));
  6319. error_line("%s", type);
  6320. }
  6321. error_line(")\n");
  6322. for (isize i = 0; i < valids.count; i++) {
  6323. Entity *proc = proc_entities[valids[i].index];
  6324. GB_ASSERT(proc != nullptr);
  6325. TokenPos pos = proc->token.pos;
  6326. Type *t = base_type(proc->type); GB_ASSERT(t->kind == Type_Proc);
  6327. gbString pt = nullptr;
  6328. defer (gb_string_free(pt));
  6329. if (t->Proc.node != nullptr) {
  6330. pt = expr_to_string(t->Proc.node);
  6331. } else {
  6332. pt = type_to_string(t);
  6333. }
  6334. String name = proc->token.string;
  6335. char const *sep = "::";
  6336. if (proc->kind == Entity_Variable) {
  6337. sep = ":=";
  6338. }
  6339. error_line("\t%.*s %s %s ", LIT(name), sep, pt);
  6340. if (proc->decl_info->proc_lit != nullptr) {
  6341. GB_ASSERT(proc->decl_info->proc_lit->kind == Ast_ProcLit);
  6342. auto *pl = &proc->decl_info->proc_lit->ProcLit;
  6343. if (pl->where_token.kind != Token_Invalid) {
  6344. error_line("\n\t\twhere ");
  6345. for_array(j, pl->where_clauses) {
  6346. Ast *clause = pl->where_clauses[j];
  6347. if (j != 0) {
  6348. error_line("\t\t ");
  6349. }
  6350. gbString str = expr_to_string(clause);
  6351. error_line("%s", str);
  6352. gb_string_free(str);
  6353. if (j != pl->where_clauses.count-1) {
  6354. error_line(",");
  6355. }
  6356. }
  6357. error_line("\n\t");
  6358. }
  6359. }
  6360. error_line("at %s\n", token_pos_to_string(pos));
  6361. }
  6362. result_type = t_invalid;
  6363. } else {
  6364. Ast *ident = operand->expr;
  6365. while (ident->kind == Ast_SelectorExpr) {
  6366. Ast *s = ident->SelectorExpr.selector;
  6367. ident = s;
  6368. }
  6369. Entity *e = proc_entities[valids[0].index];
  6370. GB_ASSERT(e != nullptr);
  6371. proc_type = e->type;
  6372. CallArgumentData data = {};
  6373. CallArgumentError err = call_checker(c, call, proc_type, e, operands, CallArgumentMode_ShowErrors, &data);
  6374. Entity *entity_to_use = data.gen_entity != nullptr ? data.gen_entity : e;
  6375. add_entity_use(c, ident, entity_to_use);
  6376. if (data.gen_entity != nullptr) {
  6377. Entity *e = data.gen_entity;
  6378. DeclInfo *decl = data.gen_entity->decl_info;
  6379. CheckerContext ctx = *c;
  6380. ctx.scope = decl->scope;
  6381. ctx.decl = decl;
  6382. ctx.proc_name = e->token.string;
  6383. ctx.curr_proc_decl = decl;
  6384. ctx.curr_proc_sig = e->type;
  6385. GB_ASSERT(decl->proc_lit->kind == Ast_ProcLit);
  6386. evaluate_where_clauses(&ctx, call, decl->scope, &decl->proc_lit->ProcLit.where_clauses, true);
  6387. decl->where_clauses_evaluated = true;
  6388. }
  6389. return data;
  6390. }
  6391. } else {
  6392. Ast *ident = operand->expr;
  6393. while (ident->kind == Ast_SelectorExpr) {
  6394. Ast *s = ident->SelectorExpr.selector;
  6395. ident = s;
  6396. }
  6397. Entity *e = entity_of_node(ident);
  6398. CallArgumentData data = {};
  6399. CallArgumentError err = call_checker(c, call, proc_type, e, operands, CallArgumentMode_ShowErrors, &data);
  6400. Entity *entity_to_use = data.gen_entity != nullptr ? data.gen_entity : e;
  6401. add_entity_use(c, ident, entity_to_use);
  6402. if (data.gen_entity != nullptr) {
  6403. Entity *e = data.gen_entity;
  6404. DeclInfo *decl = data.gen_entity->decl_info;
  6405. CheckerContext ctx = *c;
  6406. ctx.scope = decl->scope;
  6407. ctx.decl = decl;
  6408. ctx.proc_name = e->token.string;
  6409. ctx.curr_proc_decl = decl;
  6410. ctx.curr_proc_sig = e->type;
  6411. GB_ASSERT(decl->proc_lit->kind == Ast_ProcLit);
  6412. evaluate_where_clauses(&ctx, call, decl->scope, &decl->proc_lit->ProcLit.where_clauses, true);
  6413. decl->where_clauses_evaluated = true;
  6414. }
  6415. return data;
  6416. }
  6417. CallArgumentData data = {};
  6418. data.result_type = t_invalid;
  6419. return data;
  6420. }
  6421. isize lookup_polymorphic_record_parameter(Type *t, String parameter_name) {
  6422. if (!is_type_polymorphic_record(t)) {
  6423. return -1;
  6424. }
  6425. TypeTuple *params = get_record_polymorphic_params(t);
  6426. if (params == nullptr) {
  6427. return -1;
  6428. }
  6429. for_array(i, params->variables) {
  6430. Entity *e = params->variables[i];
  6431. String name = e->token.string;
  6432. if (is_blank_ident(name)) {
  6433. continue;
  6434. }
  6435. if (name == parameter_name) {
  6436. return i;
  6437. }
  6438. }
  6439. return -1;
  6440. }
  6441. CallArgumentError check_polymorphic_record_type(CheckerContext *c, Operand *operand, Ast *call) {
  6442. ast_node(ce, CallExpr, call);
  6443. Type *original_type = operand->type;
  6444. GB_ASSERT(is_type_polymorphic_record(original_type));
  6445. bool show_error = true;
  6446. Array<Operand> operands = {};
  6447. defer (array_free(&operands));
  6448. bool named_fields = false;
  6449. {
  6450. // NOTE(bill, 2019-10-26): Allow a cycle in the parameters but not in the fields themselves
  6451. auto prev_type_path = c->type_path;
  6452. c->type_path = new_checker_type_path();
  6453. defer ({
  6454. destroy_checker_type_path(c->type_path);
  6455. c->type_path = prev_type_path;
  6456. });
  6457. if (is_call_expr_field_value(ce)) {
  6458. named_fields = true;
  6459. operands = array_make<Operand>(heap_allocator(), ce->args.count);
  6460. for_array(i, ce->args) {
  6461. Ast *arg = ce->args[i];
  6462. ast_node(fv, FieldValue, arg);
  6463. if (fv->field->kind == Ast_Ident) {
  6464. String name = fv->field->Ident.token.string;
  6465. isize index = lookup_polymorphic_record_parameter(original_type, name);
  6466. if (index >= 0) {
  6467. TypeTuple *params = get_record_polymorphic_params(original_type);
  6468. Entity *e = params->variables[i];
  6469. if (e->kind == Entity_Constant) {
  6470. check_expr_with_type_hint(c, &operands[i], fv->value, e->type);
  6471. continue;
  6472. }
  6473. }
  6474. }
  6475. check_expr_or_type(c, &operands[i], fv->value);
  6476. }
  6477. bool vari_expand = (ce->ellipsis.pos.line != 0);
  6478. if (vari_expand) {
  6479. error(ce->ellipsis, "Invalid use of '..' in a polymorphic type call'");
  6480. }
  6481. } else {
  6482. operands = array_make<Operand>(heap_allocator(), 0, 2*ce->args.count);
  6483. Entity **lhs = nullptr;
  6484. isize lhs_count = -1;
  6485. TypeTuple *params = get_record_polymorphic_params(original_type);
  6486. if (params != nullptr) {
  6487. lhs = params->variables.data;
  6488. lhs_count = params->variables.count;
  6489. }
  6490. check_unpack_arguments(c, lhs, lhs_count, &operands, ce->args, false, false);
  6491. }
  6492. }
  6493. CallArgumentError err = CallArgumentError_None;
  6494. TypeTuple *tuple = get_record_polymorphic_params(original_type);
  6495. isize param_count = tuple->variables.count;
  6496. isize minimum_param_count = param_count;
  6497. for (minimum_param_count = tuple->variables.count-1; minimum_param_count >= 0; minimum_param_count--) {
  6498. Entity *e = tuple->variables[minimum_param_count];
  6499. if (e->kind != Entity_Constant) {
  6500. break;
  6501. }
  6502. if (e->Constant.param_value.kind == ParameterValue_Invalid) {
  6503. break;
  6504. }
  6505. }
  6506. Array<Operand> ordered_operands = operands;
  6507. if (!named_fields) {
  6508. ordered_operands = array_make<Operand>(permanent_allocator(), param_count);
  6509. array_copy(&ordered_operands, operands, 0);
  6510. } else {
  6511. bool *visited = gb_alloc_array(temporary_allocator(), bool, param_count);
  6512. // LEAK(bill)
  6513. ordered_operands = array_make<Operand>(permanent_allocator(), param_count);
  6514. for_array(i, ce->args) {
  6515. Ast *arg = ce->args[i];
  6516. ast_node(fv, FieldValue, arg);
  6517. if (fv->field->kind != Ast_Ident) {
  6518. if (show_error) {
  6519. gbString expr_str = expr_to_string(fv->field);
  6520. error(arg, "Invalid parameter name '%s' in polymorphic type call", expr_str);
  6521. gb_string_free(expr_str);
  6522. }
  6523. err = CallArgumentError_InvalidFieldValue;
  6524. continue;
  6525. }
  6526. String name = fv->field->Ident.token.string;
  6527. isize index = lookup_polymorphic_record_parameter(original_type, name);
  6528. if (index < 0) {
  6529. if (show_error) {
  6530. error(arg, "No parameter named '%.*s' for this polymorphic type", LIT(name));
  6531. }
  6532. err = CallArgumentError_ParameterNotFound;
  6533. continue;
  6534. }
  6535. if (visited[index]) {
  6536. if (show_error) {
  6537. error(arg, "Duplicate parameter '%.*s' in polymorphic type", LIT(name));
  6538. }
  6539. err = CallArgumentError_DuplicateParameter;
  6540. continue;
  6541. }
  6542. visited[index] = true;
  6543. ordered_operands[index] = operands[i];
  6544. }
  6545. for (isize i = 0; i < param_count; i++) {
  6546. if (!visited[i]) {
  6547. Entity *e = tuple->variables[i];
  6548. if (is_blank_ident(e->token)) {
  6549. continue;
  6550. }
  6551. if (show_error) {
  6552. if (e->kind == Entity_TypeName) {
  6553. error(call, "Type parameter '%.*s' is missing in polymorphic type call",
  6554. LIT(e->token.string));
  6555. } else {
  6556. gbString str = type_to_string(e->type);
  6557. error(call, "Parameter '%.*s' of type '%s' is missing in polymorphic type call",
  6558. LIT(e->token.string), str);
  6559. gb_string_free(str);
  6560. }
  6561. }
  6562. err = CallArgumentError_ParameterMissing;
  6563. }
  6564. }
  6565. }
  6566. if (err != 0) {
  6567. operand->mode = Addressing_Invalid;
  6568. return err;
  6569. }
  6570. if (minimum_param_count != param_count) {
  6571. if (param_count < ordered_operands.count) {
  6572. error(call, "Too many polymorphic type arguments, expected a maximum of %td, got %td", param_count, ordered_operands.count);
  6573. err = CallArgumentError_TooManyArguments;
  6574. } else if (minimum_param_count > ordered_operands.count) {
  6575. error(call, "Too few polymorphic type arguments, expected a minimum of %td, got %td", minimum_param_count, ordered_operands.count);
  6576. err = CallArgumentError_TooFewArguments;
  6577. }
  6578. } else {
  6579. if (param_count < ordered_operands.count) {
  6580. error(call, "Too many polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
  6581. err = CallArgumentError_TooManyArguments;
  6582. } else if (param_count > ordered_operands.count) {
  6583. error(call, "Too few polymorphic type arguments, expected %td, got %td", param_count, ordered_operands.count);
  6584. err = CallArgumentError_TooFewArguments;
  6585. }
  6586. }
  6587. if (err != 0) {
  6588. return err;
  6589. }
  6590. if (minimum_param_count != param_count) {
  6591. isize missing_count = 0;
  6592. // NOTE(bill): Replace missing operands with the default values (if possible)
  6593. for_array(i, ordered_operands) {
  6594. Operand *o = &ordered_operands[i];
  6595. if (o->expr == nullptr) {
  6596. Entity *e = tuple->variables[i];
  6597. if (e->kind == Entity_Constant) {
  6598. missing_count += 1;
  6599. o->mode = Addressing_Constant;
  6600. o->type = default_type(e->type);
  6601. o->expr = unparen_expr(e->Constant.param_value.original_ast_expr);
  6602. if (e->Constant.param_value.kind == ParameterValue_Constant) {
  6603. o->value = e->Constant.param_value.value;
  6604. }
  6605. }
  6606. }
  6607. }
  6608. }
  6609. i64 score = 0;
  6610. for (isize i = 0; i < param_count; i++) {
  6611. Entity *e = tuple->variables[i];
  6612. Operand *o = &ordered_operands[i];
  6613. if (o->mode == Addressing_Invalid) {
  6614. continue;
  6615. }
  6616. if (e->kind == Entity_TypeName) {
  6617. if (o->mode != Addressing_Type) {
  6618. if (show_error) {
  6619. error(o->expr, "Expected a type for the argument '%.*s'", LIT(e->token.string));
  6620. }
  6621. err = CallArgumentError_WrongTypes;
  6622. }
  6623. if (are_types_identical(e->type, o->type)) {
  6624. score += assign_score_function(1);
  6625. } else {
  6626. score += assign_score_function(MAXIMUM_TYPE_DISTANCE);
  6627. }
  6628. } else {
  6629. i64 s = 0;
  6630. if (o->type->kind == Type_Generic) {
  6631. // Polymorphic name!
  6632. score += assign_score_function(1);
  6633. continue;
  6634. } else if (!check_is_assignable_to_with_score(c, o, e->type, &s)) {
  6635. if (show_error) {
  6636. check_assignment(c, o, e->type, str_lit("polymorphic type argument"));
  6637. }
  6638. err = CallArgumentError_WrongTypes;
  6639. }
  6640. o->type = e->type;
  6641. if (o->mode != Addressing_Constant) {
  6642. bool valid = false;
  6643. if (is_type_proc(o->type)) {
  6644. Entity *proc_entity = entity_from_expr(o->expr);
  6645. valid = proc_entity != nullptr;
  6646. }
  6647. if (!valid) {
  6648. if (show_error) {
  6649. error(o->expr, "Expected a constant value for this polymorphic type argument");
  6650. }
  6651. err = CallArgumentError_NoneConstantParameter;
  6652. }
  6653. }
  6654. score += s;
  6655. }
  6656. // NOTE(bill): Add type info the parameters
  6657. add_type_info_type(c, o->type);
  6658. }
  6659. {
  6660. bool failure = false;
  6661. Entity *found_entity = find_polymorphic_record_entity(c, original_type, param_count, ordered_operands, &failure);
  6662. if (found_entity) {
  6663. operand->mode = Addressing_Type;
  6664. operand->type = found_entity->type;
  6665. return err;
  6666. }
  6667. String generated_name = make_string_c(expr_to_string(call));
  6668. CheckerContext ctx = *c;
  6669. // NOTE(bill): We need to make sure the lookup scope for the record is the same as where it was created
  6670. ctx.scope = polymorphic_record_parent_scope(original_type);
  6671. GB_ASSERT(ctx.scope != nullptr);
  6672. Type *named_type = alloc_type_named(generated_name, nullptr, nullptr);
  6673. Type *bt = base_type(original_type);
  6674. if (bt->kind == Type_Struct) {
  6675. Ast *node = clone_ast(bt->Struct.node);
  6676. Type *struct_type = alloc_type_struct();
  6677. struct_type->Struct.node = node;
  6678. struct_type->Struct.polymorphic_parent = original_type;
  6679. set_base_type(named_type, struct_type);
  6680. check_open_scope(&ctx, node);
  6681. check_struct_type(&ctx, struct_type, node, &ordered_operands, named_type, original_type);
  6682. check_close_scope(&ctx);
  6683. } else if (bt->kind == Type_Union) {
  6684. Ast *node = clone_ast(bt->Union.node);
  6685. Type *union_type = alloc_type_union();
  6686. union_type->Union.node = node;
  6687. union_type->Union.polymorphic_parent = original_type;
  6688. set_base_type(named_type, union_type);
  6689. check_open_scope(&ctx, node);
  6690. check_union_type(&ctx, union_type, node, &ordered_operands, named_type, original_type);
  6691. check_close_scope(&ctx);
  6692. } else {
  6693. GB_PANIC("Unsupported parametric polymorphic record type");
  6694. }
  6695. operand->mode = Addressing_Type;
  6696. operand->type = named_type;
  6697. }
  6698. return err;
  6699. }
  6700. ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *call, Ast *proc, Slice<Ast *> const &args, ProcInlining inlining, Type *type_hint) {
  6701. if (proc != nullptr &&
  6702. proc->kind == Ast_BasicDirective) {
  6703. ast_node(bd, BasicDirective, proc);
  6704. String name = bd->name;
  6705. if (name == "location" || name == "assert" || name == "panic" || name == "defined" || name == "config" || name == "load") {
  6706. operand->mode = Addressing_Builtin;
  6707. operand->builtin_id = BuiltinProc_DIRECTIVE;
  6708. operand->expr = proc;
  6709. operand->type = t_invalid;
  6710. add_type_and_value(&c->checker->info, proc, operand->mode, operand->type, operand->value);
  6711. } else {
  6712. GB_PANIC("Unhandled #%.*s", LIT(name));
  6713. }
  6714. if (inlining != ProcInlining_none) {
  6715. error(call, "Inlining operators are not allowed on built-in procedures");
  6716. }
  6717. } else {
  6718. if (proc != nullptr) {
  6719. check_expr_or_type(c, operand, proc);
  6720. } else {
  6721. GB_ASSERT(operand->expr != nullptr);
  6722. }
  6723. }
  6724. if (args.count > 0) {
  6725. bool fail = false;
  6726. bool first_is_field_value = (args[0]->kind == Ast_FieldValue);
  6727. for_array(i, args) {
  6728. Ast *arg = args[i];
  6729. bool mix = false;
  6730. if (first_is_field_value) {
  6731. mix = arg->kind != Ast_FieldValue;
  6732. } else {
  6733. mix = arg->kind == Ast_FieldValue;
  6734. }
  6735. if (mix) {
  6736. error(arg, "Mixture of 'field = value' and value elements in a procedure call is not allowed");
  6737. fail = true;
  6738. }
  6739. }
  6740. if (fail) {
  6741. operand->mode = Addressing_Invalid;
  6742. operand->expr = call;
  6743. return Expr_Stmt;
  6744. }
  6745. }
  6746. if (operand->mode == Addressing_Invalid) {
  6747. for_array(i, args) {
  6748. Ast *arg = args[i];
  6749. if (arg->kind == Ast_FieldValue) {
  6750. arg = arg->FieldValue.value;
  6751. }
  6752. check_expr_base(c, operand, arg, nullptr);
  6753. }
  6754. operand->mode = Addressing_Invalid;
  6755. operand->expr = call;
  6756. return Expr_Stmt;
  6757. }
  6758. if (operand->mode == Addressing_Type) {
  6759. Type *t = operand->type;
  6760. if (is_type_polymorphic_record(t)) {
  6761. auto err = check_polymorphic_record_type(c, operand, call);
  6762. if (err == 0) {
  6763. Ast *ident = operand->expr;
  6764. while (ident->kind == Ast_SelectorExpr) {
  6765. Ast *s = ident->SelectorExpr.selector;
  6766. ident = s;
  6767. }
  6768. Type *ot = operand->type;
  6769. GB_ASSERT(ot->kind == Type_Named);
  6770. Entity *e = ot->Named.type_name;
  6771. add_entity_use(c, ident, e);
  6772. add_type_and_value(&c->checker->info, call, Addressing_Type, ot, empty_exact_value);
  6773. } else {
  6774. operand->mode = Addressing_Invalid;
  6775. operand->type = t_invalid;
  6776. }
  6777. } else {
  6778. gbString str = type_to_string(t);
  6779. defer (gb_string_free(str));
  6780. operand->mode = Addressing_Invalid;
  6781. isize arg_count = args.count;
  6782. switch (arg_count) {
  6783. case 0: error(call, "Missing argument in conversion to '%s'", str); break;
  6784. default: error(call, "Too many arguments in conversion to '%s'", str); break;
  6785. case 1: {
  6786. Ast *arg = args[0];
  6787. if (arg->kind == Ast_FieldValue) {
  6788. error(call, "'field = value' cannot be used in a type conversion");
  6789. arg = arg->FieldValue.value;
  6790. // NOTE(bill): Carry on the cast regardless
  6791. }
  6792. check_expr(c, operand, arg);
  6793. if (operand->mode != Addressing_Invalid) {
  6794. check_cast(c, operand, t);
  6795. }
  6796. break;
  6797. }
  6798. }
  6799. }
  6800. return Expr_Expr;
  6801. }
  6802. if (operand->mode == Addressing_Builtin) {
  6803. i32 id = operand->builtin_id;
  6804. if (!check_builtin_procedure(c, operand, call, id, type_hint)) {
  6805. operand->mode = Addressing_Invalid;
  6806. operand->type = t_invalid;
  6807. }
  6808. operand->expr = call;
  6809. return builtin_procs[id].kind;
  6810. }
  6811. Entity *e = entity_of_node(operand->expr);
  6812. if (e != nullptr && e->kind == Entity_Procedure) {
  6813. if (e->Procedure.deferred_procedure.entity != nullptr) {
  6814. call->viral_state_flags |= ViralStateFlag_ContainsDeferredProcedure;
  6815. }
  6816. }
  6817. Type *proc_type = base_type(operand->type);
  6818. if (operand->mode != Addressing_ProcGroup) {
  6819. bool valid_type = (proc_type != nullptr) && is_type_proc(proc_type);
  6820. bool valid_mode = is_operand_value(*operand);
  6821. if (!valid_type || !valid_mode) {
  6822. Ast *e = operand->expr;
  6823. gbString str = expr_to_string(e);
  6824. gbString type_str = type_to_string(operand->type);
  6825. error(e, "Cannot call a non-procedure: '%s' of type '%s'", str, type_str);
  6826. gb_string_free(type_str);
  6827. gb_string_free(str);
  6828. operand->mode = Addressing_Invalid;
  6829. operand->expr = call;
  6830. return Expr_Stmt;
  6831. }
  6832. }
  6833. CallArgumentData data = check_call_arguments(c, operand, proc_type, call, args);
  6834. Type *result_type = data.result_type;
  6835. gb_zero_item(operand);
  6836. operand->expr = call;
  6837. if (result_type == t_invalid) {
  6838. operand->mode = Addressing_Invalid;
  6839. operand->type = t_invalid;
  6840. return Expr_Stmt;
  6841. }
  6842. Type *pt = base_type(proc_type);
  6843. if (pt->kind == Type_Proc && pt->Proc.calling_convention == ProcCC_Odin) {
  6844. if ((c->scope->flags & ScopeFlag_ContextDefined) == 0) {
  6845. error(call, "'context' has not been defined within this scope, but is required for this procedure call");
  6846. }
  6847. }
  6848. if (result_type == nullptr) {
  6849. operand->mode = Addressing_NoValue;
  6850. } else {
  6851. GB_ASSERT(is_type_tuple(result_type));
  6852. isize count = result_type->Tuple.variables.count;
  6853. switch (count) {
  6854. case 0:
  6855. operand->mode = Addressing_NoValue;
  6856. break;
  6857. case 1:
  6858. operand->mode = Addressing_Value;
  6859. operand->type = result_type->Tuple.variables[0]->type;
  6860. break;
  6861. default:
  6862. operand->mode = Addressing_Value;
  6863. operand->type = result_type;
  6864. break;
  6865. }
  6866. }
  6867. switch (inlining) {
  6868. case ProcInlining_inline: {
  6869. if (proc != nullptr) {
  6870. Entity *e = entity_from_expr(proc);
  6871. if (e != nullptr && e->kind == Entity_Procedure) {
  6872. DeclInfo *decl = e->decl_info;
  6873. if (decl->proc_lit) {
  6874. ast_node(pl, ProcLit, decl->proc_lit);
  6875. if (pl->inlining == ProcInlining_no_inline) {
  6876. error(call, "'inline' cannot be applied to a procedure that has be marked as 'no_inline'");
  6877. }
  6878. }
  6879. }
  6880. }
  6881. break;
  6882. }
  6883. case ProcInlining_no_inline:
  6884. break;
  6885. }
  6886. operand->expr = call;
  6887. if (pt->kind == Type_Proc && pt->Proc.optional_ok) {
  6888. operand->mode = Addressing_OptionalOk;
  6889. }
  6890. // add_type_and_value(c->info, operand->expr, operand->mode, operand->type, operand->value);
  6891. return Expr_Expr;
  6892. }
  6893. void check_expr_with_type_hint(CheckerContext *c, Operand *o, Ast *e, Type *t) {
  6894. check_expr_base(c, o, e, t);
  6895. check_not_tuple(c, o);
  6896. char const *err_str = nullptr;
  6897. switch (o->mode) {
  6898. case Addressing_NoValue:
  6899. err_str = "used as a value";
  6900. break;
  6901. case Addressing_Type:
  6902. if (t == nullptr || !is_type_typeid(t)) {
  6903. err_str = "is not an expression but a type";
  6904. }
  6905. break;
  6906. case Addressing_Builtin:
  6907. err_str = "must be called";
  6908. break;
  6909. }
  6910. if (err_str != nullptr) {
  6911. gbString str = expr_to_string(e);
  6912. error(e, "'%s' %s", str, err_str);
  6913. gb_string_free(str);
  6914. o->mode = Addressing_Invalid;
  6915. }
  6916. }
  6917. bool check_set_index_data(Operand *o, Type *t, bool indirection, i64 *max_count, Type *original_type) {
  6918. switch (t->kind) {
  6919. case Type_Basic:
  6920. if (t->Basic.kind == Basic_string) {
  6921. if (o->mode == Addressing_Constant) {
  6922. *max_count = o->value.value_string.len;
  6923. }
  6924. if (o->mode != Addressing_Constant) {
  6925. o->mode = Addressing_Value;
  6926. }
  6927. o->type = t_u8;
  6928. return true;
  6929. } else if (t->Basic.kind == Basic_UntypedString) {
  6930. if (o->mode == Addressing_Constant) {
  6931. *max_count = o->value.value_string.len;
  6932. o->type = t_u8;
  6933. return true;
  6934. }
  6935. return false;
  6936. }
  6937. break;
  6938. case Type_Array:
  6939. *max_count = t->Array.count;
  6940. if (indirection) {
  6941. o->mode = Addressing_Variable;
  6942. } else if (o->mode != Addressing_Variable &&
  6943. o->mode != Addressing_Constant) {
  6944. o->mode = Addressing_Value;
  6945. }
  6946. o->type = t->Array.elem;
  6947. return true;
  6948. case Type_EnumeratedArray:
  6949. *max_count = t->EnumeratedArray.count;
  6950. if (indirection) {
  6951. o->mode = Addressing_Variable;
  6952. } else if (o->mode != Addressing_Variable &&
  6953. o->mode != Addressing_Constant) {
  6954. o->mode = Addressing_Value;
  6955. }
  6956. o->type = t->EnumeratedArray.elem;
  6957. return true;
  6958. case Type_Slice:
  6959. o->type = t->Slice.elem;
  6960. if (o->mode != Addressing_Constant) {
  6961. o->mode = Addressing_Variable;
  6962. }
  6963. return true;
  6964. case Type_RelativeSlice:
  6965. {
  6966. Type *slice_type = base_type(t->RelativeSlice.slice_type);
  6967. GB_ASSERT(slice_type->kind == Type_Slice);
  6968. o->type = slice_type->Slice.elem;
  6969. if (o->mode != Addressing_Constant) {
  6970. o->mode = Addressing_Variable;
  6971. }
  6972. }
  6973. return true;
  6974. case Type_DynamicArray:
  6975. o->type = t->DynamicArray.elem;
  6976. if (o->mode != Addressing_Constant) {
  6977. o->mode = Addressing_Variable;
  6978. }
  6979. return true;
  6980. case Type_Struct:
  6981. if (t->Struct.soa_kind != StructSoa_None) {
  6982. if (t->Struct.soa_kind == StructSoa_Fixed) {
  6983. *max_count = t->Struct.soa_count;
  6984. }
  6985. o->type = t->Struct.soa_elem;
  6986. if (o->mode == Addressing_SoaVariable || o->mode == Addressing_Variable) {
  6987. o->mode = Addressing_SoaVariable;
  6988. } else {
  6989. o->mode = Addressing_Value;
  6990. }
  6991. return true;
  6992. }
  6993. return false;
  6994. }
  6995. if (is_type_pointer(original_type) && indirection) {
  6996. Type *ptr = base_type(original_type);
  6997. if (ptr->kind == Type_Pointer && o->mode == Addressing_SoaVariable) {
  6998. o->type = ptr->Pointer.elem;
  6999. o->mode = Addressing_Value;
  7000. return true;
  7001. }
  7002. }
  7003. return false;
  7004. }
  7005. bool ternary_compare_types(Type *x, Type *y) {
  7006. if (is_type_untyped_undef(x) && type_has_undef(y)) {
  7007. return true;
  7008. } else if (is_type_untyped_nil(x) && type_has_nil(y)) {
  7009. return true;
  7010. } else if (is_type_untyped_undef(y) && type_has_undef(x)) {
  7011. return true;
  7012. } else if (is_type_untyped_nil(y) && type_has_nil(x)) {
  7013. return true;
  7014. }
  7015. return are_types_identical(x, y);
  7016. }
  7017. bool check_range(CheckerContext *c, Ast *node, Operand *x, Operand *y, ExactValue *inline_for_depth_, Type *type_hint=nullptr) {
  7018. if (!is_ast_range(node)) {
  7019. return false;
  7020. }
  7021. ast_node(ie, BinaryExpr, node);
  7022. check_expr_with_type_hint(c, x, ie->left, type_hint);
  7023. if (x->mode == Addressing_Invalid) {
  7024. return false;
  7025. }
  7026. check_expr_with_type_hint(c, y, ie->right, type_hint);
  7027. if (y->mode == Addressing_Invalid) {
  7028. return false;
  7029. }
  7030. convert_to_typed(c, x, y->type);
  7031. if (x->mode == Addressing_Invalid) {
  7032. return false;
  7033. }
  7034. convert_to_typed(c, y, x->type);
  7035. if (y->mode == Addressing_Invalid) {
  7036. return false;
  7037. }
  7038. convert_to_typed(c, x, default_type(y->type));
  7039. if (x->mode == Addressing_Invalid) {
  7040. return false;
  7041. }
  7042. convert_to_typed(c, y, default_type(x->type));
  7043. if (y->mode == Addressing_Invalid) {
  7044. return false;
  7045. }
  7046. if (!are_types_identical(x->type, y->type)) {
  7047. if (x->type != t_invalid &&
  7048. y->type != t_invalid) {
  7049. gbString xt = type_to_string(x->type);
  7050. gbString yt = type_to_string(y->type);
  7051. gbString expr_str = expr_to_string(x->expr);
  7052. error(ie->op, "Mismatched types in interval expression '%s' : '%s' vs '%s'", expr_str, xt, yt);
  7053. gb_string_free(expr_str);
  7054. gb_string_free(yt);
  7055. gb_string_free(xt);
  7056. }
  7057. return false;
  7058. }
  7059. Type *type = x->type;
  7060. if (!is_type_integer(type) && !is_type_float(type) && !is_type_pointer(type) && !is_type_enum(type)) {
  7061. error(ie->op, "Only numerical and pointer types are allowed within interval expressions");
  7062. return false;
  7063. }
  7064. if (x->mode == Addressing_Constant &&
  7065. y->mode == Addressing_Constant) {
  7066. ExactValue a = x->value;
  7067. ExactValue b = y->value;
  7068. GB_ASSERT(are_types_identical(x->type, y->type));
  7069. TokenKind op = Token_Lt;
  7070. switch (ie->op.kind) {
  7071. case Token_Ellipsis: op = Token_LtEq; break;
  7072. case Token_RangeHalf: op = Token_Lt; break;
  7073. default: error(ie->op, "Invalid range operator"); break;
  7074. }
  7075. bool ok = compare_exact_values(op, a, b);
  7076. if (!ok) {
  7077. // TODO(bill): Better error message
  7078. error(ie->op, "Invalid interval range");
  7079. return false;
  7080. }
  7081. ExactValue inline_for_depth = exact_value_sub(b, a);
  7082. if (ie->op.kind == Token_Ellipsis) {
  7083. inline_for_depth = exact_value_increment_one(inline_for_depth);
  7084. }
  7085. if (inline_for_depth_) *inline_for_depth_ = inline_for_depth;
  7086. } else if (inline_for_depth_ != nullptr) {
  7087. error(ie->op, "Interval expressions must be constant");
  7088. return false;
  7089. }
  7090. add_type_and_value(&c->checker->info, ie->left, x->mode, x->type, x->value);
  7091. add_type_and_value(&c->checker->info, ie->right, y->mode, y->type, y->value);
  7092. return true;
  7093. }
  7094. bool check_is_operand_compound_lit_constant(CheckerContext *c, Operand *o) {
  7095. if (is_operand_nil(*o)) {
  7096. return true;
  7097. }
  7098. Ast *expr = unparen_expr(o->expr);
  7099. if (expr != nullptr) {
  7100. Entity *e = strip_entity_wrapping(entity_from_expr(expr));
  7101. if (e != nullptr && e->kind == Entity_Procedure) {
  7102. return true;
  7103. }
  7104. if (expr->kind == Ast_ProcLit) {
  7105. add_type_and_value(c->info, expr, Addressing_Constant, type_of_expr(expr), exact_value_procedure(expr));
  7106. return true;
  7107. }
  7108. }
  7109. return o->mode == Addressing_Constant;
  7110. }
  7111. ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
  7112. u32 prev_state_flags = c->state_flags;
  7113. defer (c->state_flags = prev_state_flags);
  7114. if (node->state_flags != 0) {
  7115. u32 in = node->state_flags;
  7116. u32 out = c->state_flags;
  7117. if (in & StateFlag_no_bounds_check) {
  7118. out |= StateFlag_no_bounds_check;
  7119. out &= ~StateFlag_bounds_check;
  7120. } else if (in & StateFlag_bounds_check) {
  7121. out |= StateFlag_bounds_check;
  7122. out &= ~StateFlag_no_bounds_check;
  7123. }
  7124. c->state_flags = out;
  7125. }
  7126. ExprKind kind = Expr_Stmt;
  7127. o->mode = Addressing_Invalid;
  7128. o->type = t_invalid;
  7129. switch (node->kind) {
  7130. default:
  7131. return kind;
  7132. case_ast_node(be, BadExpr, node)
  7133. return kind;
  7134. case_end;
  7135. case_ast_node(i, Implicit, node)
  7136. switch (i->kind) {
  7137. case Token_context:
  7138. {
  7139. if (c->proc_name.len == 0 && c->curr_proc_sig == nullptr) {
  7140. error(node, "'context' is only allowed within procedures %p", c->curr_proc_decl);
  7141. return kind;
  7142. }
  7143. if (unparen_expr(c->assignment_lhs_hint) == node) {
  7144. c->scope->flags |= ScopeFlag_ContextDefined;
  7145. }
  7146. if ((c->scope->flags & ScopeFlag_ContextDefined) == 0) {
  7147. error(node, "'context' has not been defined within this scope");
  7148. // Continue with value
  7149. }
  7150. init_core_context(c->checker);
  7151. o->mode = Addressing_Context;
  7152. o->type = t_context;
  7153. }
  7154. break;
  7155. default:
  7156. error(node, "Illegal implicit name '%.*s'", LIT(i->string));
  7157. return kind;
  7158. }
  7159. case_end;
  7160. case_ast_node(i, Ident, node);
  7161. check_ident(c, o, node, nullptr, type_hint, false);
  7162. case_end;
  7163. case_ast_node(u, Undef, node);
  7164. o->mode = Addressing_Value;
  7165. o->type = t_untyped_undef;
  7166. case_end;
  7167. case_ast_node(bl, BasicLit, node);
  7168. Type *t = t_invalid;
  7169. switch (node->tav.value.kind) {
  7170. case ExactValue_String: t = t_untyped_string; break;
  7171. case ExactValue_Float: t = t_untyped_float; break;
  7172. case ExactValue_Complex: t = t_untyped_complex; break;
  7173. case ExactValue_Quaternion: t = t_untyped_quaternion; break;
  7174. case ExactValue_Integer:
  7175. t = t_untyped_integer;
  7176. if (bl->token.kind == Token_Rune) {
  7177. t = t_untyped_rune;
  7178. }
  7179. break;
  7180. default:
  7181. GB_PANIC("Unhandled value type for basic literal");
  7182. break;
  7183. }
  7184. o->mode = Addressing_Constant;
  7185. o->type = t;
  7186. o->value = node->tav.value;
  7187. case_end;
  7188. case_ast_node(bd, BasicDirective, node);
  7189. o->mode = Addressing_Constant;
  7190. if (bd->name == "file") {
  7191. o->type = t_untyped_string;
  7192. o->value = exact_value_string(get_file_path_string(bd->token.pos.file_id));
  7193. } else if (bd->name == "line") {
  7194. o->type = t_untyped_integer;
  7195. o->value = exact_value_i64(bd->token.pos.line);
  7196. } else if (bd->name == "procedure") {
  7197. if (c->curr_proc_decl == nullptr) {
  7198. error(node, "#procedure may only be used within procedures");
  7199. o->type = t_untyped_string;
  7200. o->value = exact_value_string(str_lit(""));
  7201. } else {
  7202. o->type = t_untyped_string;
  7203. o->value = exact_value_string(c->proc_name);
  7204. }
  7205. } else if (bd->name == "caller_location") {
  7206. init_core_source_code_location(c->checker);
  7207. error(node, "#caller_location may only be used as a default argument parameter");
  7208. o->type = t_source_code_location;
  7209. o->mode = Addressing_Value;
  7210. } else {
  7211. GB_PANIC("Unknown basic directive");
  7212. }
  7213. case_end;
  7214. case_ast_node(pg, ProcGroup, node);
  7215. error(node, "Illegal use of a procedure group");
  7216. o->mode = Addressing_Invalid;
  7217. case_end;
  7218. case_ast_node(pl, ProcLit, node);
  7219. CheckerContext ctx = *c;
  7220. DeclInfo *decl = nullptr;
  7221. Type *type = alloc_type(Type_Proc);
  7222. check_open_scope(&ctx, pl->type);
  7223. {
  7224. decl = make_decl_info(ctx.scope, ctx.decl);
  7225. decl->proc_lit = node;
  7226. ctx.decl = decl;
  7227. defer (ctx.decl = ctx.decl->parent);
  7228. if (pl->tags != 0) {
  7229. error(node, "A procedure literal cannot have tags");
  7230. pl->tags = 0; // TODO(bill): Should I zero this?!
  7231. }
  7232. check_procedure_type(&ctx, type, pl->type);
  7233. if (!is_type_proc(type)) {
  7234. gbString str = expr_to_string(node);
  7235. error(node, "Invalid procedure literal '%s'", str);
  7236. gb_string_free(str);
  7237. check_close_scope(&ctx);
  7238. return kind;
  7239. }
  7240. if (pl->body == nullptr) {
  7241. error(node, "A procedure literal must have a body");
  7242. return kind;
  7243. }
  7244. pl->decl = decl;
  7245. check_procedure_later(ctx.checker, ctx.file, empty_token, decl, type, pl->body, pl->tags);
  7246. }
  7247. check_close_scope(&ctx);
  7248. o->mode = Addressing_Value;
  7249. o->type = type;
  7250. case_end;
  7251. case_ast_node(te, TernaryExpr, node);
  7252. Operand cond = {Addressing_Invalid};
  7253. check_expr(c, &cond, te->cond);
  7254. node->viral_state_flags |= te->cond->viral_state_flags;
  7255. if (cond.mode != Addressing_Invalid && !is_type_boolean(cond.type)) {
  7256. error(te->cond, "Non-boolean condition in if expression");
  7257. }
  7258. Operand x = {Addressing_Invalid};
  7259. Operand y = {Addressing_Invalid};
  7260. check_expr_or_type(c, &x, te->x, type_hint);
  7261. node->viral_state_flags |= te->x->viral_state_flags;
  7262. if (te->y != nullptr) {
  7263. check_expr_or_type(c, &y, te->y, type_hint);
  7264. node->viral_state_flags |= te->y->viral_state_flags;
  7265. } else {
  7266. error(node, "A ternary expression must have an else clause");
  7267. return kind;
  7268. }
  7269. if (x.type == nullptr || x.type == t_invalid ||
  7270. y.type == nullptr || y.type == t_invalid) {
  7271. return kind;
  7272. }
  7273. if (x.mode == Addressing_Type && y.mode == Addressing_Type &&
  7274. cond.mode == Addressing_Constant && is_type_boolean(cond.type)) {
  7275. o->mode = Addressing_Type;
  7276. if (cond.value.value_bool) {
  7277. o->type = x.type;
  7278. o->expr = x.expr;
  7279. } else {
  7280. o->type = y.type;
  7281. o->expr = y.expr;
  7282. }
  7283. return Expr_Expr;
  7284. }
  7285. convert_to_typed(c, &x, y.type);
  7286. if (x.mode == Addressing_Invalid) {
  7287. return kind;
  7288. }
  7289. convert_to_typed(c, &y, x.type);
  7290. if (y.mode == Addressing_Invalid) {
  7291. x.mode = Addressing_Invalid;
  7292. return kind;
  7293. }
  7294. if (!ternary_compare_types(x.type, y.type)) {
  7295. gbString its = type_to_string(x.type);
  7296. gbString ets = type_to_string(y.type);
  7297. error(node, "Mismatched types in ternary expression, %s vs %s", its, ets);
  7298. gb_string_free(ets);
  7299. gb_string_free(its);
  7300. return kind;
  7301. }
  7302. Type *type = x.type;
  7303. if (is_type_untyped_nil(type) || is_type_untyped_undef(type)) {
  7304. type = y.type;
  7305. }
  7306. o->type = type;
  7307. o->mode = Addressing_Value;
  7308. if (cond.mode == Addressing_Constant && is_type_boolean(cond.type) &&
  7309. x.mode == Addressing_Constant &&
  7310. y.mode == Addressing_Constant) {
  7311. o->mode = Addressing_Constant;
  7312. if (cond.value.value_bool) {
  7313. o->value = x.value;
  7314. } else {
  7315. o->value = y.value;
  7316. }
  7317. }
  7318. case_end;
  7319. case_ast_node(te, TernaryIfExpr, node);
  7320. Operand cond = {Addressing_Invalid};
  7321. check_expr(c, &cond, te->cond);
  7322. node->viral_state_flags |= te->cond->viral_state_flags;
  7323. if (cond.mode != Addressing_Invalid && !is_type_boolean(cond.type)) {
  7324. error(te->cond, "Non-boolean condition in ternary if expression");
  7325. }
  7326. Operand x = {Addressing_Invalid};
  7327. Operand y = {Addressing_Invalid};
  7328. check_expr_or_type(c, &x, te->x, type_hint);
  7329. node->viral_state_flags |= te->x->viral_state_flags;
  7330. if (te->y != nullptr) {
  7331. check_expr_or_type(c, &y, te->y, type_hint);
  7332. node->viral_state_flags |= te->y->viral_state_flags;
  7333. } else {
  7334. error(node, "A ternary expression must have an else clause");
  7335. return kind;
  7336. }
  7337. if (x.type == nullptr || x.type == t_invalid ||
  7338. y.type == nullptr || y.type == t_invalid) {
  7339. return kind;
  7340. }
  7341. convert_to_typed(c, &x, y.type);
  7342. if (x.mode == Addressing_Invalid) {
  7343. return kind;
  7344. }
  7345. convert_to_typed(c, &y, x.type);
  7346. if (y.mode == Addressing_Invalid) {
  7347. x.mode = Addressing_Invalid;
  7348. return kind;
  7349. }
  7350. if (!ternary_compare_types(x.type, y.type)) {
  7351. gbString its = type_to_string(x.type);
  7352. gbString ets = type_to_string(y.type);
  7353. error(node, "Mismatched types in ternary if expression, %s vs %s", its, ets);
  7354. gb_string_free(ets);
  7355. gb_string_free(its);
  7356. return kind;
  7357. }
  7358. Type *type = x.type;
  7359. if (is_type_untyped_nil(type) || is_type_untyped_undef(type)) {
  7360. type = y.type;
  7361. }
  7362. o->type = type;
  7363. o->mode = Addressing_Value;
  7364. // if (cond.mode == Addressing_Constant && is_type_boolean(cond.type) &&
  7365. // x.mode == Addressing_Constant &&
  7366. // y.mode == Addressing_Constant) {
  7367. // o->mode = Addressing_Constant;
  7368. // if (cond.value.value_bool) {
  7369. // o->value = x.value;
  7370. // } else {
  7371. // o->value = y.value;
  7372. // }
  7373. // }
  7374. case_end;
  7375. case_ast_node(te, TernaryWhenExpr, node);
  7376. Operand cond = {};
  7377. check_expr(c, &cond, te->cond);
  7378. node->viral_state_flags |= te->cond->viral_state_flags;
  7379. if (cond.mode != Addressing_Constant || !is_type_boolean(cond.type)) {
  7380. error(te->cond, "Expected a constant boolean condition in ternary when expression");
  7381. return kind;
  7382. }
  7383. if (cond.value.value_bool) {
  7384. check_expr_or_type(c, o, te->x, type_hint);
  7385. node->viral_state_flags |= te->x->viral_state_flags;
  7386. } else {
  7387. if (te->y != nullptr) {
  7388. check_expr_or_type(c, o, te->y, type_hint);
  7389. node->viral_state_flags |= te->y->viral_state_flags;
  7390. } else {
  7391. error(node, "A ternary when expression must have an else clause");
  7392. return kind;
  7393. }
  7394. }
  7395. case_end;
  7396. case_ast_node(cl, CompoundLit, node);
  7397. Type *type = type_hint;
  7398. bool is_to_be_determined_array_count = false;
  7399. bool is_constant = true;
  7400. if (cl->type != nullptr) {
  7401. type = nullptr;
  7402. // [?]Type
  7403. if (cl->type->kind == Ast_ArrayType && cl->type->ArrayType.count != nullptr) {
  7404. Ast *count = cl->type->ArrayType.count;
  7405. if (count->kind == Ast_UnaryExpr &&
  7406. count->UnaryExpr.op.kind == Token_Question) {
  7407. type = alloc_type_array(check_type(c, cl->type->ArrayType.elem), -1);
  7408. is_to_be_determined_array_count = true;
  7409. }
  7410. if (cl->elems.count > 0) {
  7411. if (cl->type->ArrayType.tag != nullptr) {
  7412. Ast *tag = cl->type->ArrayType.tag;
  7413. GB_ASSERT(tag->kind == Ast_BasicDirective);
  7414. String name = tag->BasicDirective.name;
  7415. if (name == "soa") {
  7416. error(node, "#soa arrays are not supported for compound literals");
  7417. return kind;
  7418. }
  7419. }
  7420. }
  7421. }
  7422. if (cl->type->kind == Ast_DynamicArrayType && cl->type->DynamicArrayType.tag != nullptr) {
  7423. if (cl->elems.count > 0) {
  7424. Ast *tag = cl->type->DynamicArrayType.tag;
  7425. GB_ASSERT(tag->kind == Ast_BasicDirective);
  7426. String name = tag->BasicDirective.name;
  7427. if (name == "soa") {
  7428. error(node, "#soa arrays are not supported for compound literals");
  7429. return kind;
  7430. }
  7431. }
  7432. }
  7433. if (type == nullptr) {
  7434. type = check_type(c, cl->type);
  7435. }
  7436. }
  7437. if (type == nullptr) {
  7438. error(node, "Missing type in compound literal");
  7439. return kind;
  7440. }
  7441. Type *t = base_type(type);
  7442. if (is_type_polymorphic(t)) {
  7443. gbString str = type_to_string(type);
  7444. error(node, "Cannot use a polymorphic type for a compound literal, got '%s'", str);
  7445. o->expr = node;
  7446. o->type = type;
  7447. gb_string_free(str);
  7448. return kind;
  7449. }
  7450. switch (t->kind) {
  7451. case Type_Struct: {
  7452. if (cl->elems.count == 0) {
  7453. break; // NOTE(bill): No need to init
  7454. }
  7455. if (t->Struct.is_raw_union) {
  7456. if (cl->elems.count != 0) {
  7457. gbString type_str = type_to_string(type);
  7458. error(node, "Illegal compound literal type '%s'", type_str);
  7459. gb_string_free(type_str);
  7460. }
  7461. break;
  7462. }
  7463. isize field_count = t->Struct.fields.count;
  7464. isize min_field_count = t->Struct.fields.count;
  7465. for (isize i = min_field_count-1; i >= 0; i--) {
  7466. Entity *e = t->Struct.fields[i];
  7467. GB_ASSERT(e->kind == Entity_Variable);
  7468. if (e->Variable.param_value.kind != ParameterValue_Invalid) {
  7469. min_field_count--;
  7470. } else {
  7471. break;
  7472. }
  7473. }
  7474. if (cl->elems[0]->kind == Ast_FieldValue) {
  7475. bool *fields_visited = gb_alloc_array(temporary_allocator(), bool, field_count);
  7476. for_array(i, cl->elems) {
  7477. Ast *elem = cl->elems[i];
  7478. if (elem->kind != Ast_FieldValue) {
  7479. error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
  7480. continue;
  7481. }
  7482. ast_node(fv, FieldValue, elem);
  7483. if (fv->field->kind != Ast_Ident) {
  7484. gbString expr_str = expr_to_string(fv->field);
  7485. error(elem, "Invalid field name '%s' in structure literal", expr_str);
  7486. gb_string_free(expr_str);
  7487. continue;
  7488. }
  7489. String name = fv->field->Ident.token.string;
  7490. Selection sel = lookup_field(type, name, o->mode == Addressing_Type);
  7491. bool is_unknown = sel.entity == nullptr;
  7492. if (is_unknown) {
  7493. error(elem, "Unknown field '%.*s' in structure literal", LIT(name));
  7494. continue;
  7495. }
  7496. if (sel.index.count > 1) {
  7497. error(elem, "Cannot assign to an anonymous field '%.*s' in a structure literal (at the moment)", LIT(name));
  7498. continue;
  7499. }
  7500. Entity *field = t->Struct.fields[sel.index[0]];
  7501. add_entity_use(c, fv->field, field);
  7502. if (fields_visited[sel.index[0]]) {
  7503. error(elem, "Duplicate field '%.*s' in structure literal", LIT(name));
  7504. continue;
  7505. }
  7506. fields_visited[sel.index[0]] = true;
  7507. Operand o = {};
  7508. check_expr_or_type(c, &o, fv->value, field->type);
  7509. if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type) || is_type_typeid(field->type)) {
  7510. is_constant = false;
  7511. }
  7512. if (is_constant) {
  7513. is_constant = check_is_operand_compound_lit_constant(c, &o);
  7514. }
  7515. check_assignment(c, &o, field->type, str_lit("structure literal"));
  7516. }
  7517. } else {
  7518. bool seen_field_value = false;
  7519. for_array(index, cl->elems) {
  7520. Entity *field = nullptr;
  7521. Ast *elem = cl->elems[index];
  7522. if (elem->kind == Ast_FieldValue) {
  7523. seen_field_value = true;
  7524. error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
  7525. continue;
  7526. } else if (seen_field_value) {
  7527. error(elem, "Value elements cannot be used after a 'field = value'");
  7528. continue;
  7529. }
  7530. if (index >= field_count) {
  7531. error(elem, "Too many values in structure literal, expected %td, got %td", field_count, cl->elems.count);
  7532. break;
  7533. }
  7534. if (field == nullptr) {
  7535. field = t->Struct.fields[index];
  7536. }
  7537. Operand o = {};
  7538. check_expr_or_type(c, &o, elem, field->type);
  7539. if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type) || is_type_typeid(field->type)) {
  7540. is_constant = false;
  7541. }
  7542. if (is_constant) {
  7543. is_constant = check_is_operand_compound_lit_constant(c, &o);
  7544. }
  7545. check_assignment(c, &o, field->type, str_lit("structure literal"));
  7546. }
  7547. if (cl->elems.count < field_count) {
  7548. if (min_field_count < field_count) {
  7549. if (cl->elems.count < min_field_count) {
  7550. error(cl->close, "Too few values in structure literal, expected at least %td, got %td", min_field_count, cl->elems.count);
  7551. }
  7552. } else {
  7553. error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count);
  7554. }
  7555. }
  7556. }
  7557. break;
  7558. }
  7559. case Type_Slice:
  7560. case Type_Array:
  7561. case Type_DynamicArray:
  7562. case Type_SimdVector:
  7563. {
  7564. Type *elem_type = nullptr;
  7565. String context_name = {};
  7566. i64 max_type_count = -1;
  7567. if (t->kind == Type_Slice) {
  7568. elem_type = t->Slice.elem;
  7569. context_name = str_lit("slice literal");
  7570. } else if (t->kind == Type_Array) {
  7571. elem_type = t->Array.elem;
  7572. context_name = str_lit("array literal");
  7573. if (!is_to_be_determined_array_count) {
  7574. max_type_count = t->Array.count;
  7575. }
  7576. } else if (t->kind == Type_DynamicArray) {
  7577. elem_type = t->DynamicArray.elem;
  7578. context_name = str_lit("dynamic array literal");
  7579. is_constant = false;
  7580. if (!build_context.no_dynamic_literals) {
  7581. add_package_dependency(c, "runtime", "__dynamic_array_reserve");
  7582. add_package_dependency(c, "runtime", "__dynamic_array_append");
  7583. }
  7584. } else if (t->kind == Type_SimdVector) {
  7585. elem_type = t->SimdVector.elem;
  7586. context_name = str_lit("simd vector literal");
  7587. max_type_count = t->SimdVector.count;
  7588. } else {
  7589. GB_PANIC("unreachable");
  7590. }
  7591. i64 max = 0;
  7592. Type *bet = base_type(elem_type);
  7593. if (!elem_type_can_be_constant(bet)) {
  7594. is_constant = false;
  7595. }
  7596. if (bet == t_invalid) {
  7597. break;
  7598. }
  7599. if (cl->elems.count > 0 && cl->elems[0]->kind == Ast_FieldValue) {
  7600. if (is_type_simd_vector(t)) {
  7601. error(cl->elems[0], "'field = value' is not allowed for SIMD vector literals");
  7602. } else {
  7603. RangeCache rc = range_cache_make(heap_allocator());
  7604. defer (range_cache_destroy(&rc));
  7605. for_array(i, cl->elems) {
  7606. Ast *elem = cl->elems[i];
  7607. if (elem->kind != Ast_FieldValue) {
  7608. error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
  7609. continue;
  7610. }
  7611. ast_node(fv, FieldValue, elem);
  7612. if (is_ast_range(fv->field)) {
  7613. Token op = fv->field->BinaryExpr.op;
  7614. Operand x = {};
  7615. Operand y = {};
  7616. bool ok = check_range(c, fv->field, &x, &y, nullptr);
  7617. if (!ok) {
  7618. continue;
  7619. }
  7620. if (x.mode != Addressing_Constant || !is_type_integer(core_type(x.type))) {
  7621. error(x.expr, "Expected a constant integer as an array field");
  7622. continue;
  7623. }
  7624. if (y.mode != Addressing_Constant || !is_type_integer(core_type(y.type))) {
  7625. error(y.expr, "Expected a constant integer as an array field");
  7626. continue;
  7627. }
  7628. i64 lo = exact_value_to_i64(x.value);
  7629. i64 hi = exact_value_to_i64(y.value);
  7630. i64 max_index = hi;
  7631. if (op.kind == Token_RangeHalf) { // ..< (exclusive)
  7632. hi -= 1;
  7633. } else { // .. (inclusive)
  7634. max_index += 1;
  7635. }
  7636. bool new_range = range_cache_add_range(&rc, lo, hi);
  7637. if (!new_range) {
  7638. error(elem, "Overlapping field range index %lld %.*s %lld for %.*s", lo, LIT(op.string), hi, LIT(context_name));
  7639. continue;
  7640. }
  7641. if (max_type_count >= 0 && (lo < 0 || lo >= max_type_count)) {
  7642. error(elem, "Index %lld is out of bounds (0..<%lld) for %.*s", lo, max_type_count, LIT(context_name));
  7643. continue;
  7644. }
  7645. if (max_type_count >= 0 && (hi < 0 || hi >= max_type_count)) {
  7646. error(elem, "Index %lld is out of bounds (0..<%lld) for %.*s", hi, max_type_count, LIT(context_name));
  7647. continue;
  7648. }
  7649. if (max < hi) {
  7650. max = max_index;
  7651. }
  7652. Operand operand = {};
  7653. check_expr_with_type_hint(c, &operand, fv->value, elem_type);
  7654. check_assignment(c, &operand, elem_type, context_name);
  7655. is_constant = is_constant && operand.mode == Addressing_Constant;
  7656. } else {
  7657. Operand op_index = {};
  7658. check_expr(c, &op_index, fv->field);
  7659. if (op_index.mode != Addressing_Constant || !is_type_integer(core_type(op_index.type))) {
  7660. error(elem, "Expected a constant integer as an array field");
  7661. continue;
  7662. }
  7663. // add_type_and_value(c->info, op_index.expr, op_index.mode, op_index.type, op_index.value);
  7664. i64 index = exact_value_to_i64(op_index.value);
  7665. if (max_type_count >= 0 && (index < 0 || index >= max_type_count)) {
  7666. error(elem, "Index %lld is out of bounds (0..<%lld) for %.*s", index, max_type_count, LIT(context_name));
  7667. continue;
  7668. }
  7669. bool new_index = range_cache_add_index(&rc, index);
  7670. if (!new_index) {
  7671. error(elem, "Duplicate field index %lld for %.*s", index, LIT(context_name));
  7672. continue;
  7673. }
  7674. if (max < index+1) {
  7675. max = index+1;
  7676. }
  7677. Operand operand = {};
  7678. check_expr_with_type_hint(c, &operand, fv->value, elem_type);
  7679. check_assignment(c, &operand, elem_type, context_name);
  7680. is_constant = is_constant && operand.mode == Addressing_Constant;
  7681. }
  7682. }
  7683. cl->max_count = max;
  7684. }
  7685. } else {
  7686. isize index = 0;
  7687. for (; index < cl->elems.count; index++) {
  7688. Ast *e = cl->elems[index];
  7689. if (e == nullptr) {
  7690. error(node, "Invalid literal element");
  7691. continue;
  7692. }
  7693. if (e->kind == Ast_FieldValue) {
  7694. error(e, "Mixture of 'field = value' and value elements in a literal is not allowed");
  7695. continue;
  7696. }
  7697. if (0 <= max_type_count && max_type_count <= index) {
  7698. error(e, "Index %lld is out of bounds (>= %lld) for %.*s", index, max_type_count, LIT(context_name));
  7699. }
  7700. Operand operand = {};
  7701. check_expr_with_type_hint(c, &operand, e, elem_type);
  7702. check_assignment(c, &operand, elem_type, context_name);
  7703. is_constant = is_constant && operand.mode == Addressing_Constant;
  7704. }
  7705. if (max < index) {
  7706. max = index;
  7707. }
  7708. }
  7709. if (t->kind == Type_Array) {
  7710. if (is_to_be_determined_array_count) {
  7711. t->Array.count = max;
  7712. } else if (cl->elems.count > 0 && cl->elems[0]->kind != Ast_FieldValue) {
  7713. if (0 < max && max < t->Array.count) {
  7714. error(node, "Expected %lld values for this array literal, got %lld", cast(long long)t->Array.count, cast(long long)max);
  7715. }
  7716. }
  7717. }
  7718. if (t->kind == Type_SimdVector) {
  7719. if (!is_constant) {
  7720. error(node, "Expected all constant elements for a simd vector");
  7721. }
  7722. if (t->SimdVector.is_x86_mmx) {
  7723. error(node, "Compound literals are not allowed with intrinsics.x86_mmx");
  7724. }
  7725. }
  7726. if (t->kind == Type_DynamicArray) {
  7727. if (build_context.no_dynamic_literals && cl->elems.count) {
  7728. error(node, "Compound literals of dynamic types have been disabled");
  7729. }
  7730. }
  7731. break;
  7732. }
  7733. case Type_EnumeratedArray:
  7734. {
  7735. Type *elem_type = t->EnumeratedArray.elem;
  7736. Type *index_type = t->EnumeratedArray.index;
  7737. String context_name = str_lit("enumerated array literal");
  7738. i64 max_type_count = t->EnumeratedArray.count;
  7739. gbString index_type_str = type_to_string(index_type);
  7740. defer (gb_string_free(index_type_str));
  7741. i64 total_lo = exact_value_to_i64(t->EnumeratedArray.min_value);
  7742. i64 total_hi = exact_value_to_i64(t->EnumeratedArray.max_value);
  7743. String total_lo_string = {};
  7744. String total_hi_string = {};
  7745. GB_ASSERT(is_type_enum(index_type));
  7746. {
  7747. Type *bt = base_type(index_type);
  7748. GB_ASSERT(bt->kind == Type_Enum);
  7749. for_array(i, bt->Enum.fields) {
  7750. Entity *f = bt->Enum.fields[i];
  7751. if (f->kind != Entity_Constant) {
  7752. continue;
  7753. }
  7754. if (total_lo_string.len == 0 && compare_exact_values(Token_CmpEq, f->Constant.value, t->EnumeratedArray.min_value)) {
  7755. total_lo_string = f->token.string;
  7756. }
  7757. if (total_hi_string.len == 0 && compare_exact_values(Token_CmpEq, f->Constant.value, t->EnumeratedArray.max_value)) {
  7758. total_hi_string = f->token.string;
  7759. }
  7760. if (total_lo_string.len != 0 && total_hi_string.len != 0) {
  7761. break;
  7762. }
  7763. }
  7764. }
  7765. i64 max = 0;
  7766. Type *bet = base_type(elem_type);
  7767. if (!elem_type_can_be_constant(bet)) {
  7768. is_constant = false;
  7769. }
  7770. if (bet == t_invalid) {
  7771. break;
  7772. }
  7773. if (cl->elems.count > 0 && cl->elems[0]->kind == Ast_FieldValue) {
  7774. RangeCache rc = range_cache_make(heap_allocator());
  7775. defer (range_cache_destroy(&rc));
  7776. for_array(i, cl->elems) {
  7777. Ast *elem = cl->elems[i];
  7778. if (elem->kind != Ast_FieldValue) {
  7779. error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
  7780. continue;
  7781. }
  7782. ast_node(fv, FieldValue, elem);
  7783. if (is_ast_range(fv->field)) {
  7784. Token op = fv->field->BinaryExpr.op;
  7785. Operand x = {};
  7786. Operand y = {};
  7787. bool ok = check_range(c, fv->field, &x, &y, nullptr, index_type);
  7788. if (!ok) {
  7789. continue;
  7790. }
  7791. if (x.mode != Addressing_Constant || !are_types_identical(x.type, index_type)) {
  7792. error(x.expr, "Expected a constant enum of type '%s' as an array field", index_type_str);
  7793. continue;
  7794. }
  7795. if (y.mode != Addressing_Constant || !are_types_identical(x.type, index_type)) {
  7796. error(y.expr, "Expected a constant enum of type '%s' as an array field", index_type_str);
  7797. continue;
  7798. }
  7799. i64 lo = exact_value_to_i64(x.value);
  7800. i64 hi = exact_value_to_i64(y.value);
  7801. i64 max_index = hi;
  7802. if (op.kind == Token_RangeHalf) {
  7803. hi -= 1;
  7804. }
  7805. bool new_range = range_cache_add_range(&rc, lo, hi);
  7806. if (!new_range) {
  7807. gbString lo_str = expr_to_string(x.expr);
  7808. gbString hi_str = expr_to_string(y.expr);
  7809. error(elem, "Overlapping field range index %s %.*s %s for %.*s", lo_str, LIT(op.string), hi_str, LIT(context_name));
  7810. gb_string_free(hi_str);
  7811. gb_string_free(lo_str);
  7812. continue;
  7813. }
  7814. // NOTE(bill): These are sanity checks for invalid enum values
  7815. if (max_type_count >= 0 && (lo < total_lo || lo > total_hi)) {
  7816. gbString lo_str = expr_to_string(x.expr);
  7817. error(elem, "Index %s is out of bounds (%.*s .. %.*s) for %.*s", lo_str, LIT(total_lo_string), LIT(total_hi_string), LIT(context_name));
  7818. gb_string_free(lo_str);
  7819. continue;
  7820. }
  7821. if (max_type_count >= 0 && (hi < 0 || hi > total_hi)) {
  7822. gbString hi_str = expr_to_string(y.expr);
  7823. error(elem, "Index %s is out of bounds (%.*s .. %.*s) for %.*s", hi_str, LIT(total_lo_string), LIT(total_hi_string), LIT(context_name));
  7824. gb_string_free(hi_str);
  7825. continue;
  7826. }
  7827. if (max < hi) {
  7828. max = max_index;
  7829. }
  7830. Operand operand = {};
  7831. check_expr_with_type_hint(c, &operand, fv->value, elem_type);
  7832. check_assignment(c, &operand, elem_type, context_name);
  7833. is_constant = is_constant && operand.mode == Addressing_Constant;
  7834. } else {
  7835. Operand op_index = {};
  7836. check_expr_with_type_hint(c, &op_index, fv->field, index_type);
  7837. if (op_index.mode != Addressing_Constant || !are_types_identical(op_index.type, index_type)) {
  7838. error(op_index.expr, "Expected a constant enum of type '%s' as an array field", index_type_str);
  7839. continue;
  7840. }
  7841. i64 index = exact_value_to_i64(op_index.value);
  7842. if (max_type_count >= 0 && (index < total_lo || index > total_hi)) {
  7843. gbString idx_str = expr_to_string(op_index.expr);
  7844. error(elem, "Index %s is out of bounds (%.*s .. %.*s) for %.*s", idx_str, LIT(total_lo_string), LIT(total_hi_string), LIT(context_name));
  7845. gb_string_free(idx_str);
  7846. continue;
  7847. }
  7848. bool new_index = range_cache_add_index(&rc, index);
  7849. if (!new_index) {
  7850. gbString idx_str = expr_to_string(op_index.expr);
  7851. error(elem, "Duplicate field index %s for %.*s", idx_str, LIT(context_name));
  7852. gb_string_free(idx_str);
  7853. continue;
  7854. }
  7855. if (max < index+1) {
  7856. max = index+1;
  7857. }
  7858. Operand operand = {};
  7859. check_expr_with_type_hint(c, &operand, fv->value, elem_type);
  7860. check_assignment(c, &operand, elem_type, context_name);
  7861. is_constant = is_constant && operand.mode == Addressing_Constant;
  7862. }
  7863. }
  7864. cl->max_count = max;
  7865. } else {
  7866. isize index = 0;
  7867. for (; index < cl->elems.count; index++) {
  7868. Ast *e = cl->elems[index];
  7869. if (e == nullptr) {
  7870. error(node, "Invalid literal element");
  7871. continue;
  7872. }
  7873. if (e->kind == Ast_FieldValue) {
  7874. error(e, "Mixture of 'field = value' and value elements in a literal is not allowed");
  7875. continue;
  7876. }
  7877. if (0 <= max_type_count && max_type_count <= index) {
  7878. error(e, "Index %lld is out of bounds (>= %lld) for %.*s", index, max_type_count, LIT(context_name));
  7879. }
  7880. Operand operand = {};
  7881. check_expr_with_type_hint(c, &operand, e, elem_type);
  7882. check_assignment(c, &operand, elem_type, context_name);
  7883. is_constant = is_constant && operand.mode == Addressing_Constant;
  7884. }
  7885. if (max < index) {
  7886. max = index;
  7887. }
  7888. }
  7889. if (cl->elems.count > 0 && cl->elems[0]->kind != Ast_FieldValue) {
  7890. if (0 < max && max < t->EnumeratedArray.count) {
  7891. error(node, "Expected %lld values for this enumerated array literal, got %lld", cast(long long)t->EnumeratedArray.count, cast(long long)max);
  7892. } else {
  7893. error(node, "Enumerated array literals must only have 'field = value' elements, bare elements are not allowed");
  7894. }
  7895. }
  7896. break;
  7897. }
  7898. case Type_Basic: {
  7899. if (!is_type_any(t)) {
  7900. if (cl->elems.count != 0) {
  7901. error(node, "Illegal compound literal");
  7902. }
  7903. break;
  7904. }
  7905. if (cl->elems.count == 0) {
  7906. break; // NOTE(bill): No need to init
  7907. }
  7908. { // Checker values
  7909. Type *field_types[2] = {t_rawptr, t_typeid};
  7910. isize field_count = 2;
  7911. if (cl->elems[0]->kind == Ast_FieldValue) {
  7912. bool fields_visited[2] = {};
  7913. for_array(i, cl->elems) {
  7914. Ast *elem = cl->elems[i];
  7915. if (elem->kind != Ast_FieldValue) {
  7916. error(elem, "Mixture of 'field = value' and value elements in a 'any' literal is not allowed");
  7917. continue;
  7918. }
  7919. ast_node(fv, FieldValue, elem);
  7920. if (fv->field->kind != Ast_Ident) {
  7921. gbString expr_str = expr_to_string(fv->field);
  7922. error(elem, "Invalid field name '%s' in 'any' literal", expr_str);
  7923. gb_string_free(expr_str);
  7924. continue;
  7925. }
  7926. String name = fv->field->Ident.token.string;
  7927. Selection sel = lookup_field(type, name, o->mode == Addressing_Type);
  7928. if (sel.entity == nullptr) {
  7929. error(elem, "Unknown field '%.*s' in 'any' literal", LIT(name));
  7930. continue;
  7931. }
  7932. isize index = sel.index[0];
  7933. if (fields_visited[index]) {
  7934. error(elem, "Duplicate field '%.*s' in 'any' literal", LIT(name));
  7935. continue;
  7936. }
  7937. fields_visited[index] = true;
  7938. check_expr(c, o, fv->value);
  7939. // NOTE(bill): 'any' literals can never be constant
  7940. is_constant = false;
  7941. check_assignment(c, o, field_types[index], str_lit("'any' literal"));
  7942. }
  7943. } else {
  7944. for_array(index, cl->elems) {
  7945. Ast *elem = cl->elems[index];
  7946. if (elem->kind == Ast_FieldValue) {
  7947. error(elem, "Mixture of 'field = value' and value elements in a 'any' literal is not allowed");
  7948. continue;
  7949. }
  7950. check_expr(c, o, elem);
  7951. if (index >= field_count) {
  7952. error(o->expr, "Too many values in 'any' literal, expected %td", field_count);
  7953. break;
  7954. }
  7955. // NOTE(bill): 'any' literals can never be constant
  7956. is_constant = false;
  7957. check_assignment(c, o, field_types[index], str_lit("'any' literal"));
  7958. }
  7959. if (cl->elems.count < field_count) {
  7960. error(cl->close, "Too few values in 'any' literal, expected %td, got %td", field_count, cl->elems.count);
  7961. }
  7962. }
  7963. }
  7964. break;
  7965. }
  7966. case Type_Map: {
  7967. if (cl->elems.count == 0) {
  7968. break;
  7969. }
  7970. is_constant = false;
  7971. { // Checker values
  7972. bool key_is_typeid = is_type_typeid(t->Map.key);
  7973. bool value_is_typeid = is_type_typeid(t->Map.value);
  7974. for_array(i, cl->elems) {
  7975. Ast *elem = cl->elems[i];
  7976. if (elem->kind != Ast_FieldValue) {
  7977. error(elem, "Only 'field = value' elements are allowed in a map literal");
  7978. continue;
  7979. }
  7980. ast_node(fv, FieldValue, elem);
  7981. if (key_is_typeid) {
  7982. check_expr_or_type(c, o, fv->field, t->Map.key);
  7983. } else {
  7984. check_expr_with_type_hint(c, o, fv->field, t->Map.key);
  7985. }
  7986. check_assignment(c, o, t->Map.key, str_lit("map literal"));
  7987. if (o->mode == Addressing_Invalid) {
  7988. continue;
  7989. }
  7990. if (value_is_typeid) {
  7991. check_expr_or_type(c, o, fv->value, t->Map.value);
  7992. } else {
  7993. check_expr_with_type_hint(c, o, fv->value, t->Map.value);
  7994. }
  7995. check_assignment(c, o, t->Map.value, str_lit("map literal"));
  7996. }
  7997. }
  7998. if (build_context.no_dynamic_literals && cl->elems.count) {
  7999. error(node, "Compound literals of dynamic types have been disabled");
  8000. } else {
  8001. add_package_dependency(c, "runtime", "__dynamic_map_reserve");
  8002. add_package_dependency(c, "runtime", "__dynamic_map_set");
  8003. }
  8004. break;
  8005. }
  8006. case Type_BitSet: {
  8007. if (cl->elems.count == 0) {
  8008. break; // NOTE(bill): No need to init
  8009. }
  8010. Type *et = base_type(t->BitSet.elem);
  8011. isize field_count = 0;
  8012. if (et->kind == Type_Enum) {
  8013. field_count = et->Enum.fields.count;
  8014. }
  8015. if (cl->elems[0]->kind == Ast_FieldValue) {
  8016. error(cl->elems[0], "'field = value' in a bit_set a literal is not allowed");
  8017. is_constant = false;
  8018. } else {
  8019. for_array(index, cl->elems) {
  8020. Entity *field = nullptr;
  8021. Ast *elem = cl->elems[index];
  8022. if (elem->kind == Ast_FieldValue) {
  8023. error(elem, "'field = value' in a bit_set a literal is not allowed");
  8024. continue;
  8025. }
  8026. check_expr_with_type_hint(c, o, elem, et);
  8027. if (is_constant) {
  8028. is_constant = o->mode == Addressing_Constant;
  8029. }
  8030. check_assignment(c, o, t->BitSet.elem, str_lit("bit_set literal"));
  8031. if (o->mode == Addressing_Constant) {
  8032. i64 lower = t->BitSet.lower;
  8033. i64 upper = t->BitSet.upper;
  8034. i64 v = exact_value_to_i64(o->value);
  8035. if (lower <= v && v <= upper) {
  8036. // okay
  8037. } else {
  8038. error(elem, "Bit field value out of bounds, %lld not in the range %lld .. %lld", v, lower, upper);
  8039. continue;
  8040. }
  8041. }
  8042. }
  8043. }
  8044. break;
  8045. }
  8046. default: {
  8047. if (cl->elems.count == 0) {
  8048. break; // NOTE(bill): No need to init
  8049. }
  8050. gbString str = type_to_string(type);
  8051. error(node, "Invalid compound literal type '%s'", str);
  8052. gb_string_free(str);
  8053. return kind;
  8054. }
  8055. }
  8056. if (is_constant) {
  8057. o->mode = Addressing_Constant;
  8058. if (is_type_bit_set(type)) {
  8059. // NOTE(bill): Encode as an integer
  8060. i64 lower = base_type(type)->BitSet.lower;
  8061. u64 bits = 0;
  8062. for_array(index, cl->elems) {
  8063. Entity *field = nullptr;
  8064. Ast *elem = cl->elems[index];
  8065. GB_ASSERT(elem->kind != Ast_FieldValue);
  8066. TypeAndValue tav = elem->tav;
  8067. ExactValue i = exact_value_to_integer(tav.value);
  8068. if (i.kind != ExactValue_Integer) {
  8069. continue;
  8070. }
  8071. i64 val = big_int_to_i64(&i.value_integer);
  8072. val -= lower;
  8073. u64 bit = u64(1ll<<val);
  8074. bits |= bit;
  8075. }
  8076. o->value = exact_value_u64(bits);
  8077. } else if (is_type_constant_type(type) && cl->elems.count == 0) {
  8078. ExactValue value = exact_value_compound(node);
  8079. Type *bt = core_type(type);
  8080. if (bt->kind == Type_Basic) {
  8081. if (bt->Basic.flags & BasicFlag_Boolean) {
  8082. value = exact_value_bool(false);
  8083. } else if (bt->Basic.flags & BasicFlag_Integer) {
  8084. value = exact_value_i64(0);
  8085. } else if (bt->Basic.flags & BasicFlag_Unsigned) {
  8086. value = exact_value_i64(0);
  8087. } else if (bt->Basic.flags & BasicFlag_Float) {
  8088. value = exact_value_float(0);
  8089. } else if (bt->Basic.flags & BasicFlag_Complex) {
  8090. value = exact_value_complex(0, 0);
  8091. } else if (bt->Basic.flags & BasicFlag_Quaternion) {
  8092. value = exact_value_quaternion(0, 0, 0, 0);
  8093. } else if (bt->Basic.flags & BasicFlag_Pointer) {
  8094. value = exact_value_pointer(0);
  8095. } else if (bt->Basic.flags & BasicFlag_String) {
  8096. String empty_string = {};
  8097. value = exact_value_string(empty_string);
  8098. } else if (bt->Basic.flags & BasicFlag_Rune) {
  8099. value = exact_value_i64(0);
  8100. }
  8101. }
  8102. o->value = value;
  8103. } else {
  8104. o->value = exact_value_compound(node);
  8105. }
  8106. } else {
  8107. o->mode = Addressing_Value;
  8108. }
  8109. o->type = type;
  8110. case_end;
  8111. case_ast_node(pe, ParenExpr, node);
  8112. kind = check_expr_base(c, o, pe->expr, type_hint);
  8113. node->viral_state_flags |= pe->expr->viral_state_flags;
  8114. o->expr = node;
  8115. case_end;
  8116. case_ast_node(te, TagExpr, node);
  8117. String name = te->name.string;
  8118. error(node, "Unknown tag expression, #%.*s", LIT(name));
  8119. if (te->expr) {
  8120. kind = check_expr_base(c, o, te->expr, type_hint);
  8121. node->viral_state_flags |= te->expr->viral_state_flags;
  8122. }
  8123. o->expr = node;
  8124. case_end;
  8125. case_ast_node(ta, TypeAssertion, node);
  8126. check_expr(c, o, ta->expr);
  8127. node->viral_state_flags |= ta->expr->viral_state_flags;
  8128. if (o->mode == Addressing_Invalid) {
  8129. o->expr = node;
  8130. return kind;
  8131. }
  8132. if (o->mode == Addressing_Constant) {
  8133. gbString expr_str = expr_to_string(o->expr);
  8134. error(o->expr, "A type assertion cannot be applied to a constant expression: '%s'", expr_str);
  8135. gb_string_free(expr_str);
  8136. o->mode = Addressing_Invalid;
  8137. o->expr = node;
  8138. return kind;
  8139. }
  8140. if (is_type_untyped(o->type)) {
  8141. gbString expr_str = expr_to_string(o->expr);
  8142. error(o->expr, "A type assertion cannot be applied to an untyped expression: '%s'", expr_str);
  8143. gb_string_free(expr_str);
  8144. o->mode = Addressing_Invalid;
  8145. o->expr = node;
  8146. return kind;
  8147. }
  8148. bool src_is_ptr = is_type_pointer(o->type);
  8149. Type *src = type_deref(o->type);
  8150. Type *bsrc = base_type(src);
  8151. if (ta->type != nullptr && ta->type->kind == Ast_UnaryExpr && ta->type->UnaryExpr.op.kind == Token_Question) {
  8152. if (!is_type_union(src)) {
  8153. gbString str = type_to_string(o->type);
  8154. error(o->expr, "Type assertions with .? can only operate on unions, got %s", str);
  8155. gb_string_free(str);
  8156. o->mode = Addressing_Invalid;
  8157. o->expr = node;
  8158. return kind;
  8159. }
  8160. if (bsrc->Union.variants.count != 1 && type_hint != nullptr) {
  8161. bool allowed = false;
  8162. for_array(i, bsrc->Union.variants) {
  8163. Type *vt = bsrc->Union.variants[i];
  8164. if (are_types_identical(vt, type_hint)) {
  8165. allowed = true;
  8166. add_type_info_type(c, vt);
  8167. break;
  8168. }
  8169. }
  8170. if (allowed) {
  8171. add_type_info_type(c, o->type);
  8172. o->type = type_hint;
  8173. o->mode = Addressing_OptionalOk;
  8174. return kind;
  8175. }
  8176. }
  8177. if (bsrc->Union.variants.count != 1) {
  8178. error(o->expr, "Type assertions with .? can only operate on unions with 1 variant, got %lld", cast(long long)bsrc->Union.variants.count);
  8179. o->mode = Addressing_Invalid;
  8180. o->expr = node;
  8181. return kind;
  8182. }
  8183. add_type_info_type(c, o->type);
  8184. add_type_info_type(c, bsrc->Union.variants[0]);
  8185. o->type = bsrc->Union.variants[0];
  8186. o->mode = Addressing_OptionalOk;
  8187. } else {
  8188. Type *t = check_type(c, ta->type);
  8189. Type *dst = t;
  8190. Type *bdst = base_type(dst);
  8191. if (is_type_union(src)) {
  8192. bool ok = false;
  8193. for_array(i, bsrc->Union.variants) {
  8194. Type *vt = bsrc->Union.variants[i];
  8195. if (are_types_identical(vt, dst)) {
  8196. ok = true;
  8197. break;
  8198. }
  8199. }
  8200. if (!ok) {
  8201. gbString expr_str = expr_to_string(o->expr);
  8202. gbString dst_type_str = type_to_string(t);
  8203. defer (gb_string_free(expr_str));
  8204. defer (gb_string_free(dst_type_str));
  8205. if (bsrc->Union.variants.count == 0) {
  8206. error(o->expr, "Cannot type assert '%s' to '%s' as this is an empty union", expr_str, dst_type_str);
  8207. } else {
  8208. error(o->expr, "Cannot type assert '%s' to '%s' as it is not a variant of that union", expr_str, dst_type_str);
  8209. }
  8210. o->mode = Addressing_Invalid;
  8211. o->expr = node;
  8212. return kind;
  8213. }
  8214. add_type_info_type(c, o->type);
  8215. add_type_info_type(c, t);
  8216. o->type = t;
  8217. o->mode = Addressing_OptionalOk;
  8218. } else if (is_type_any(src)) {
  8219. o->type = t;
  8220. o->mode = Addressing_OptionalOk;
  8221. add_type_info_type(c, o->type);
  8222. add_type_info_type(c, t);
  8223. } else {
  8224. gbString str = type_to_string(o->type);
  8225. error(o->expr, "Type assertions can only operate on unions and 'any', got %s", str);
  8226. gb_string_free(str);
  8227. o->mode = Addressing_Invalid;
  8228. o->expr = node;
  8229. return kind;
  8230. }
  8231. }
  8232. add_package_dependency(c, "runtime", "type_assertion_check");
  8233. add_package_dependency(c, "runtime", "type_assertion_check2");
  8234. case_end;
  8235. case_ast_node(tc, TypeCast, node);
  8236. check_expr_or_type(c, o, tc->type);
  8237. if (o->mode != Addressing_Type) {
  8238. gbString str = expr_to_string(tc->type);
  8239. error(tc->type, "Expected a type, got %s", str);
  8240. gb_string_free(str);
  8241. o->mode = Addressing_Invalid;
  8242. }
  8243. if (o->mode == Addressing_Invalid) {
  8244. o->expr = node;
  8245. return kind;
  8246. }
  8247. Type *type = o->type;
  8248. check_expr_base(c, o, tc->expr, type);
  8249. node->viral_state_flags |= tc->expr->viral_state_flags;
  8250. if (o->mode != Addressing_Invalid) {
  8251. switch (tc->token.kind) {
  8252. case Token_transmute:
  8253. check_transmute(c, node, o, type);
  8254. break;
  8255. case Token_cast:
  8256. check_cast(c, o, type);
  8257. break;
  8258. default:
  8259. error(node, "Invalid AST: Invalid casting expression");
  8260. o->mode = Addressing_Invalid;
  8261. break;
  8262. }
  8263. }
  8264. return Expr_Expr;
  8265. case_end;
  8266. case_ast_node(ac, AutoCast, node);
  8267. check_expr_base(c, o, ac->expr, type_hint);
  8268. node->viral_state_flags |= ac->expr->viral_state_flags;
  8269. if (o->mode == Addressing_Invalid) {
  8270. o->expr = node;
  8271. return kind;
  8272. }
  8273. if (type_hint) {
  8274. check_cast(c, o, type_hint);
  8275. }
  8276. o->expr = node;
  8277. return Expr_Expr;
  8278. case_end;
  8279. case_ast_node(ue, UnaryExpr, node);
  8280. Ast *prev_unary_address_hint = c->unary_address_hint;
  8281. c->unary_address_hint = unparen_expr(node);
  8282. check_expr_base(c, o, ue->expr, type_hint);
  8283. c->unary_address_hint = prev_unary_address_hint;
  8284. node->viral_state_flags |= ue->expr->viral_state_flags;
  8285. if (o->mode == Addressing_Invalid) {
  8286. o->expr = node;
  8287. return kind;
  8288. }
  8289. check_unary_expr(c, o, ue->op, node);
  8290. if (o->mode == Addressing_Invalid) {
  8291. o->expr = node;
  8292. return kind;
  8293. }
  8294. case_end;
  8295. case_ast_node(be, BinaryExpr, node);
  8296. check_binary_expr(c, o, node, type_hint, true);
  8297. if (o->mode == Addressing_Invalid) {
  8298. o->expr = node;
  8299. return kind;
  8300. }
  8301. case_end;
  8302. case_ast_node(se, SelectorExpr, node);
  8303. check_selector(c, o, node, type_hint);
  8304. node->viral_state_flags |= se->expr->viral_state_flags;
  8305. case_end;
  8306. case_ast_node(se, SelectorCallExpr, node);
  8307. // IMPORTANT NOTE(bill, 2020-05-22): This is a complete hack to get a shorthand which is extremely useful for vtables
  8308. // COM APIs is a great example of where this kind of thing is extremely useful
  8309. // General idea:
  8310. //
  8311. // x->y(123) == x.y(x, 123)
  8312. //
  8313. // How this has been implemented at the moment is quite hacky but it's done so to reduce need for huge backend changes
  8314. // Just regenerating a new AST aids things
  8315. //
  8316. // TODO(bill): Is this a good hack or not?
  8317. //
  8318. // NOTE(bill, 2020-05-22): I'm going to regret this decision, ain't I?
  8319. bool allow_arrow_right_selector_expr;
  8320. allow_arrow_right_selector_expr = c->allow_arrow_right_selector_expr;
  8321. c->allow_arrow_right_selector_expr = true;
  8322. Operand x = {};
  8323. ExprKind kind = check_expr_base(c, &x, se->expr, nullptr);
  8324. c->allow_arrow_right_selector_expr = allow_arrow_right_selector_expr;
  8325. if (x.mode == Addressing_Invalid || x.type == t_invalid) {
  8326. o->mode = Addressing_Invalid;
  8327. o->type = t_invalid;
  8328. o->expr = node;
  8329. return kind;
  8330. }
  8331. if (!is_type_proc(x.type)) {
  8332. gbString type_str = type_to_string(x.type);
  8333. error(se->call, "Selector call expressions expect a procedure type for the call, got '%s'", type_str);
  8334. gb_string_free(type_str);
  8335. o->mode = Addressing_Invalid;
  8336. o->type = t_invalid;
  8337. o->expr = node;
  8338. return Expr_Stmt;
  8339. }
  8340. ast_node(ce, CallExpr, se->call);
  8341. GB_ASSERT(x.expr->kind == Ast_SelectorExpr);
  8342. Ast *first_arg = x.expr->SelectorExpr.expr;
  8343. GB_ASSERT(first_arg != nullptr);
  8344. Type *pt = base_type(x.type);
  8345. GB_ASSERT(pt->kind == Type_Proc);
  8346. Type *first_type = nullptr;
  8347. String first_arg_name = {};
  8348. if (pt->Proc.param_count > 0) {
  8349. Entity *f = pt->Proc.params->Tuple.variables[0];
  8350. first_type = f->type;
  8351. first_arg_name = f->token.string;
  8352. }
  8353. if (first_arg_name.len == 0) {
  8354. first_arg_name = str_lit("_");
  8355. }
  8356. if (first_type == nullptr) {
  8357. error(se->call, "Selector call expressions expect a procedure type for the call with at least 1 parameter");
  8358. o->mode = Addressing_Invalid;
  8359. o->type = t_invalid;
  8360. o->expr = node;
  8361. return Expr_Stmt;
  8362. }
  8363. Operand y = {};
  8364. y.mode = first_arg->tav.mode;
  8365. y.type = first_arg->tav.type;
  8366. y.value = first_arg->tav.value;
  8367. if (check_is_assignable_to(c, &y, first_type)) {
  8368. // Do nothing, it's valid
  8369. } else {
  8370. Operand z = y;
  8371. z.type = type_deref(y.type);
  8372. if (check_is_assignable_to(c, &z, first_type)) {
  8373. // NOTE(bill): AST GENERATION HACK!
  8374. Token op = {Token_Pointer};
  8375. first_arg = ast_deref_expr(first_arg->file, first_arg, op);
  8376. } else if (y.mode == Addressing_Variable) {
  8377. Operand w = y;
  8378. w.type = alloc_type_pointer(y.type);
  8379. if (check_is_assignable_to(c, &w, first_type)) {
  8380. // NOTE(bill): AST GENERATION HACK!
  8381. Token op = {Token_And};
  8382. first_arg = ast_unary_expr(first_arg->file, op, first_arg);
  8383. }
  8384. }
  8385. }
  8386. if (ce->args.count > 0) {
  8387. bool fail = false;
  8388. bool first_is_field_value = (ce->args[0]->kind == Ast_FieldValue);
  8389. for_array(i, ce->args) {
  8390. Ast *arg = ce->args[i];
  8391. bool mix = false;
  8392. if (first_is_field_value) {
  8393. mix = arg->kind != Ast_FieldValue;
  8394. } else {
  8395. mix = arg->kind == Ast_FieldValue;
  8396. }
  8397. if (mix) {
  8398. fail = true;
  8399. break;
  8400. }
  8401. }
  8402. if (!fail && first_is_field_value) {
  8403. Token op = {Token_Eq};
  8404. AstFile *f = first_arg->file;
  8405. first_arg = ast_field_value(f, ast_ident(f, make_token_ident(first_arg_name)), first_arg, op);
  8406. }
  8407. }
  8408. auto modified_args = slice_make<Ast *>(heap_allocator(), ce->args.count+1);
  8409. modified_args[0] = first_arg;
  8410. slice_copy(&modified_args, ce->args, 1);
  8411. ce->args = modified_args;
  8412. se->modified_call = true;
  8413. allow_arrow_right_selector_expr = c->allow_arrow_right_selector_expr;
  8414. c->allow_arrow_right_selector_expr = true;
  8415. check_expr_base(c, o, se->call, type_hint);
  8416. c->allow_arrow_right_selector_expr = allow_arrow_right_selector_expr;
  8417. o->expr = node;
  8418. return Expr_Expr;
  8419. case_end;
  8420. case_ast_node(ise, ImplicitSelectorExpr, node);
  8421. o->type = t_invalid;
  8422. o->expr = node;
  8423. o->mode = Addressing_Invalid;
  8424. Type *th = type_hint;
  8425. if (th == nullptr) {
  8426. gbString str = expr_to_string(node);
  8427. error(node, "Cannot determine type for implicit selector expression '%s'", str);
  8428. gb_string_free(str);
  8429. return Expr_Expr;
  8430. }
  8431. o->type = th;
  8432. Type *enum_type = th;
  8433. if (!is_type_enum(th)) {
  8434. bool show_error = true;
  8435. if (is_type_union(th)) {
  8436. Type *union_type = base_type(th);
  8437. isize enum_count = 0;
  8438. Type *et = nullptr;
  8439. for_array(i, union_type->Union.variants) {
  8440. Type *vt = union_type->Union.variants[i];
  8441. if (is_type_enum(vt)) {
  8442. enum_count += 1;
  8443. et = vt;
  8444. }
  8445. }
  8446. if (enum_count == 1) {
  8447. show_error = false;
  8448. enum_type = et;
  8449. }
  8450. }
  8451. if (show_error) {
  8452. gbString typ = type_to_string(th);
  8453. gbString str = expr_to_string(node);
  8454. error(node, "Invalid type '%s' for implicit selector expression '%s'", typ, str);
  8455. gb_string_free(str);
  8456. gb_string_free(typ);
  8457. return Expr_Expr;
  8458. }
  8459. }
  8460. GB_ASSERT(ise->selector->kind == Ast_Ident);
  8461. String name = ise->selector->Ident.token.string;
  8462. enum_type = base_type(enum_type);
  8463. GB_ASSERT(enum_type->kind == Type_Enum);
  8464. Entity *e = scope_lookup_current(enum_type->Enum.scope, name);
  8465. if (e == nullptr) {
  8466. gbString typ = type_to_string(th);
  8467. error(node, "Undeclared name %.*s for type '%s'", LIT(name), typ);
  8468. gb_string_free(typ);
  8469. return Expr_Expr;
  8470. }
  8471. GB_ASSERT(are_types_identical(base_type(e->type), enum_type));
  8472. GB_ASSERT(e->kind == Entity_Constant);
  8473. o->value = e->Constant.value;
  8474. o->mode = Addressing_Constant;
  8475. o->type = e->type;
  8476. return Expr_Expr;
  8477. case_end;
  8478. case_ast_node(ie, IndexExpr, node);
  8479. check_expr(c, o, ie->expr);
  8480. node->viral_state_flags |= ie->expr->viral_state_flags;
  8481. if (o->mode == Addressing_Invalid) {
  8482. o->expr = node;
  8483. return kind;
  8484. }
  8485. Type *t = base_type(type_deref(o->type));
  8486. bool is_ptr = is_type_pointer(o->type);
  8487. bool is_const = o->mode == Addressing_Constant;
  8488. if (is_type_map(t)) {
  8489. Operand key = {};
  8490. if (is_type_typeid(t->Map.key)) {
  8491. check_expr_or_type(c, &key, ie->index, t->Map.key);
  8492. } else {
  8493. check_expr_with_type_hint(c, &key, ie->index, t->Map.key);
  8494. }
  8495. check_assignment(c, &key, t->Map.key, str_lit("map index"));
  8496. if (key.mode == Addressing_Invalid) {
  8497. o->mode = Addressing_Invalid;
  8498. o->expr = node;
  8499. return kind;
  8500. }
  8501. o->mode = Addressing_MapIndex;
  8502. o->type = t->Map.value;
  8503. o->expr = node;
  8504. add_package_dependency(c, "runtime", "__dynamic_map_get");
  8505. add_package_dependency(c, "runtime", "__dynamic_map_set");
  8506. return Expr_Expr;
  8507. }
  8508. if (t->kind == Type_Struct) {
  8509. TypeAtomOpTable *atom_op_table = t->Struct.atom_op_table;
  8510. if (atom_op_table != nullptr) {
  8511. if (atom_op_table->op[TypeAtomOp_index_set]) {
  8512. if (c->assignment_lhs_hint == node) {
  8513. o->mode = Addressing_AtomOpAssign;
  8514. o->type = o->type;
  8515. o->expr = node;
  8516. return kind;
  8517. }
  8518. }
  8519. if (atom_op_table->op[TypeAtomOp_index_get]) {
  8520. Entity *e = atom_op_table->op[TypeAtomOp_index_get];
  8521. if (ie->index == nullptr) {
  8522. gbString str = expr_to_string(o->expr);
  8523. error(o->expr, "Missing index for '%s'", str);
  8524. gb_string_free(str);
  8525. o->mode = Addressing_Invalid;
  8526. o->expr = node;
  8527. return kind;
  8528. }
  8529. GB_ASSERT(e->identifier != nullptr);
  8530. Ast *proc_ident = clone_ast(e->identifier);
  8531. auto args = array_make<Ast *>(heap_allocator(), 2);
  8532. args[0] = ie->expr;
  8533. args[1] = ie->index;
  8534. GB_ASSERT(c->file != nullptr);
  8535. Ast *fake_call = ast_call_expr(c->file, proc_ident, args, ie->open, ie->close, {});
  8536. check_expr_base(c, o, fake_call, type_hint);
  8537. AtomOpMapEntry entry = {TypeAtomOp_index_get, fake_call};
  8538. map_set(&c->info->atom_op_map, hash_pointer(node), entry);
  8539. o->expr = node;
  8540. return kind;
  8541. }
  8542. }
  8543. }
  8544. i64 max_count = -1;
  8545. bool valid = check_set_index_data(o, t, is_ptr, &max_count, o->type);
  8546. if (is_const) {
  8547. if (is_type_array(t)) {
  8548. // OKay
  8549. } else if (is_type_slice(t)) {
  8550. // Okay
  8551. } else if (is_type_enumerated_array(t)) {
  8552. // Okay
  8553. } else if (is_type_string(t)) {
  8554. // Okay
  8555. } else if (is_type_relative_slice(t)) {
  8556. // Okay
  8557. } else {
  8558. valid = false;
  8559. }
  8560. }
  8561. if (!valid) {
  8562. gbString str = expr_to_string(o->expr);
  8563. gbString type_str = type_to_string(o->type);
  8564. defer (gb_string_free(str));
  8565. defer (gb_string_free(type_str));
  8566. if (is_const) {
  8567. error(o->expr, "Cannot index constant '%s' of type '%s'", str, type_str);
  8568. } else {
  8569. error(o->expr, "Cannot index '%s' of type '%s'", str, type_str);
  8570. }
  8571. o->mode = Addressing_Invalid;
  8572. o->expr = node;
  8573. return kind;
  8574. }
  8575. if (ie->index == nullptr) {
  8576. gbString str = expr_to_string(o->expr);
  8577. error(o->expr, "Missing index for '%s'", str);
  8578. gb_string_free(str);
  8579. o->mode = Addressing_Invalid;
  8580. o->expr = node;
  8581. return kind;
  8582. }
  8583. Type *index_type_hint = nullptr;
  8584. if (is_type_enumerated_array(t)) {
  8585. Type *bt = base_type(t);
  8586. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  8587. index_type_hint = bt->EnumeratedArray.index;
  8588. }
  8589. i64 index = 0;
  8590. bool ok = check_index_value(c, false, ie->index, max_count, &index, index_type_hint);
  8591. if (is_const) {
  8592. if (index < 0) {
  8593. if (max_count < 0) {
  8594. }
  8595. gbString str = expr_to_string(o->expr);
  8596. error(o->expr, "Cannot index a constant '%s'", str);
  8597. error_line("\tSuggestion: store the constant into a variable in order to index it with a variable index\n");
  8598. gb_string_free(str);
  8599. o->mode = Addressing_Invalid;
  8600. o->expr = node;
  8601. return kind;
  8602. } else if (ok) {
  8603. ExactValue value = type_and_value_of_expr(ie->expr).value;
  8604. o->mode = Addressing_Constant;
  8605. bool success = false;
  8606. bool finish = false;
  8607. o->value = get_constant_field_single(c, value, cast(i32)index, &success, &finish);
  8608. if (!success) {
  8609. gbString str = expr_to_string(o->expr);
  8610. error(o->expr, "Cannot index a constant '%s' with index %lld", str, cast(long long)index);
  8611. error_line("\tSuggestion: store the constant into a variable in order to index it with a variable index\n");
  8612. gb_string_free(str);
  8613. o->mode = Addressing_Invalid;
  8614. o->expr = node;
  8615. return kind;
  8616. }
  8617. }
  8618. }
  8619. case_end;
  8620. case_ast_node(se, SliceExpr, node);
  8621. check_expr(c, o, se->expr);
  8622. node->viral_state_flags |= se->expr->viral_state_flags;
  8623. if (o->mode == Addressing_Invalid) {
  8624. o->mode = Addressing_Invalid;
  8625. o->expr = node;
  8626. return kind;
  8627. }
  8628. bool valid = false;
  8629. i64 max_count = -1;
  8630. Type *t = base_type(type_deref(o->type));
  8631. switch (t->kind) {
  8632. case Type_Basic:
  8633. if (t->Basic.kind == Basic_string || t->Basic.kind == Basic_UntypedString) {
  8634. valid = true;
  8635. if (o->mode == Addressing_Constant) {
  8636. max_count = o->value.value_string.len;
  8637. }
  8638. o->type = type_deref(o->type);
  8639. }
  8640. break;
  8641. case Type_Array:
  8642. valid = true;
  8643. max_count = t->Array.count;
  8644. if (o->mode != Addressing_Variable && !is_type_pointer(o->type)) {
  8645. gbString str = expr_to_string(node);
  8646. error(node, "Cannot slice array '%s', value is not addressable", str);
  8647. gb_string_free(str);
  8648. o->mode = Addressing_Invalid;
  8649. o->expr = node;
  8650. return kind;
  8651. }
  8652. o->type = alloc_type_slice(t->Array.elem);
  8653. break;
  8654. case Type_Slice:
  8655. valid = true;
  8656. o->type = type_deref(o->type);
  8657. break;
  8658. case Type_DynamicArray:
  8659. valid = true;
  8660. o->type = alloc_type_slice(t->DynamicArray.elem);
  8661. break;
  8662. case Type_Struct:
  8663. if (is_type_soa_struct(t)) {
  8664. valid = true;
  8665. o->type = make_soa_struct_slice(c, nullptr, nullptr, t->Struct.soa_elem);
  8666. } else {
  8667. TypeAtomOpTable *atom_op_table = t->Struct.atom_op_table;
  8668. if (atom_op_table != nullptr && atom_op_table->op[TypeAtomOp_slice]) {
  8669. Entity *e = atom_op_table->op[TypeAtomOp_slice];
  8670. GB_ASSERT(e->identifier != nullptr);
  8671. Ast *proc_ident = clone_ast(e->identifier);
  8672. Ast *expr = se->expr;
  8673. if (o->mode == Addressing_Variable) {
  8674. expr = ast_unary_expr(c->file, {Token_And, STR_LIT("&")}, expr);
  8675. } else if (is_type_pointer(o->type)) {
  8676. // Okay
  8677. } else {
  8678. gbString str = expr_to_string(node);
  8679. error(node, "Cannot slice '%s', value is not addressable", str);
  8680. gb_string_free(str);
  8681. o->mode = Addressing_Invalid;
  8682. o->expr = node;
  8683. return kind;
  8684. }
  8685. auto args = array_make<Ast *>(heap_allocator(), 1);
  8686. args[0] = expr;
  8687. GB_ASSERT(c->file != nullptr);
  8688. Ast *fake_call = ast_call_expr(c->file, proc_ident, args, se->open, se->close, {});
  8689. check_expr_base(c, o, fake_call, type_hint);
  8690. AtomOpMapEntry entry = {TypeAtomOp_slice, fake_call};
  8691. map_set(&c->info->atom_op_map, hash_pointer(node), entry);
  8692. valid = true;
  8693. }
  8694. }
  8695. break;
  8696. case Type_RelativeSlice:
  8697. valid = true;
  8698. o->type = t->RelativeSlice.slice_type;
  8699. if (o->mode != Addressing_Variable) {
  8700. gbString str = expr_to_string(node);
  8701. error(node, "Cannot relative slice '%s', value is not addressable", str);
  8702. gb_string_free(str);
  8703. o->mode = Addressing_Invalid;
  8704. o->expr = node;
  8705. return kind;
  8706. }
  8707. break;
  8708. }
  8709. if (!valid) {
  8710. gbString str = expr_to_string(o->expr);
  8711. gbString type_str = type_to_string(o->type);
  8712. error(o->expr, "Cannot slice '%s' of type '%s'", str, type_str);
  8713. gb_string_free(type_str);
  8714. gb_string_free(str);
  8715. o->mode = Addressing_Invalid;
  8716. o->expr = node;
  8717. return kind;
  8718. }
  8719. o->mode = Addressing_Value;
  8720. if (se->low == nullptr && se->high != nullptr) {
  8721. // error(se->interval0, "1st index is required if a 2nd index is specified");
  8722. // It is okay to continue as it will assume the 1st index is zero
  8723. }
  8724. i64 indices[2] = {};
  8725. Ast *nodes[2] = {se->low, se->high};
  8726. for (isize i = 0; i < gb_count_of(nodes); i++) {
  8727. i64 index = max_count;
  8728. if (nodes[i] != nullptr) {
  8729. i64 capacity = -1;
  8730. if (max_count >= 0) {
  8731. capacity = max_count;
  8732. }
  8733. i64 j = 0;
  8734. if (check_index_value(c, true, nodes[i], capacity, &j)) {
  8735. index = j;
  8736. }
  8737. node->viral_state_flags |= nodes[i]->viral_state_flags;
  8738. } else if (i == 0) {
  8739. index = 0;
  8740. }
  8741. indices[i] = index;
  8742. }
  8743. for (isize i = 0; i < gb_count_of(indices); i++) {
  8744. i64 a = indices[i];
  8745. for (isize j = i+1; j < gb_count_of(indices); j++) {
  8746. i64 b = indices[j];
  8747. if (a > b && b >= 0) {
  8748. error(se->close, "Invalid slice indices: [%td > %td]", a, b);
  8749. }
  8750. }
  8751. }
  8752. if (is_type_string(t) && max_count >= 0) {
  8753. bool all_constant = true;
  8754. for (isize i = 0; i < gb_count_of(nodes); i++) {
  8755. if (nodes[i] != nullptr) {
  8756. TypeAndValue tav = type_and_value_of_expr(nodes[i]);
  8757. if (tav.mode != Addressing_Constant) {
  8758. all_constant = false;
  8759. break;
  8760. }
  8761. }
  8762. }
  8763. if (!all_constant) {
  8764. gbString str = expr_to_string(o->expr);
  8765. error(o->expr, "Cannot slice '%s' with non-constant indices", str);
  8766. error_line("\tSuggestion: store the constant into a variable in order to index it with a variable index\n");
  8767. gb_string_free(str);
  8768. o->mode = Addressing_Value; // NOTE(bill): Keep subsequent values going without erring
  8769. o->expr = node;
  8770. return kind;
  8771. }
  8772. String s = {};
  8773. if (o->value.kind == ExactValue_String) {
  8774. s = o->value.value_string;
  8775. }
  8776. o->mode = Addressing_Constant;
  8777. o->type = t;
  8778. o->value = exact_value_string(substring(s, indices[0], indices[1]));
  8779. }
  8780. case_end;
  8781. case_ast_node(ce, CallExpr, node);
  8782. return check_call_expr(c, o, node, ce->proc, ce->args, ce->inlining, type_hint);
  8783. case_end;
  8784. case_ast_node(de, DerefExpr, node);
  8785. check_expr_or_type(c, o, de->expr);
  8786. node->viral_state_flags |= de->expr->viral_state_flags;
  8787. if (o->mode == Addressing_Invalid) {
  8788. o->mode = Addressing_Invalid;
  8789. o->expr = node;
  8790. return kind;
  8791. } else {
  8792. Type *t = base_type(o->type);
  8793. if (t->kind == Type_Pointer && !is_type_empty_union(t->Pointer.elem)) {
  8794. o->mode = Addressing_Variable;
  8795. o->type = t->Pointer.elem;
  8796. } else if (t->kind == Type_RelativePointer) {
  8797. if (o->mode != Addressing_Variable) {
  8798. gbString str = expr_to_string(o->expr);
  8799. gbString typ = type_to_string(o->type);
  8800. error(o->expr, "Cannot dereference relative pointer '%s' of type '%s' as it does not have a variable addressing mode", str, typ);
  8801. gb_string_free(typ);
  8802. gb_string_free(str);
  8803. }
  8804. // NOTE(bill): This is required because when dereferencing, the original type has been lost
  8805. add_type_info_type(c, o->type);
  8806. Type *ptr_type = base_type(t->RelativePointer.pointer_type);
  8807. GB_ASSERT(ptr_type->kind == Type_Pointer);
  8808. o->mode = Addressing_Variable;
  8809. o->type = ptr_type->Pointer.elem;
  8810. } else {
  8811. gbString str = expr_to_string(o->expr);
  8812. gbString typ = type_to_string(o->type);
  8813. error(o->expr, "Cannot dereference '%s' of type '%s'", str, typ);
  8814. gb_string_free(typ);
  8815. gb_string_free(str);
  8816. o->mode = Addressing_Invalid;
  8817. o->expr = node;
  8818. return kind;
  8819. }
  8820. }
  8821. case_end;
  8822. case_ast_node(ia, InlineAsmExpr, node);
  8823. if (c->curr_proc_decl == nullptr) {
  8824. error(node, "Inline asm expressions are only allowed within a procedure body");
  8825. }
  8826. if (!build_context.use_llvm_api) {
  8827. error(node, "Inline asm expressions are only currently allowed with -llvm-api");
  8828. }
  8829. auto param_types = array_make<Type *>(heap_allocator(), ia->param_types.count);
  8830. Type *return_type = nullptr;
  8831. for_array(i, ia->param_types) {
  8832. param_types[i] = check_type(c, ia->param_types[i]);
  8833. }
  8834. if (ia->return_type != nullptr) {
  8835. return_type = check_type(c, ia->return_type);
  8836. }
  8837. Operand x = {};
  8838. check_expr(c, &x, ia->asm_string);
  8839. if (x.mode != Addressing_Constant || !is_type_string(x.type)) {
  8840. error(x.expr, "Expected a constant string for the inline asm main parameter");
  8841. }
  8842. check_expr(c, &x, ia->constraints_string);
  8843. if (x.mode != Addressing_Constant || !is_type_string(x.type)) {
  8844. error(x.expr, "Expected a constant string for the inline asm constraints parameter");
  8845. }
  8846. Scope *scope = create_scope(c->scope);
  8847. scope->flags |= ScopeFlag_Proc;
  8848. Type *params = alloc_type_tuple();
  8849. Type *results = alloc_type_tuple();
  8850. if (param_types.count != 0) {
  8851. array_init(&params->Tuple.variables, heap_allocator(), param_types.count);
  8852. for_array(i, param_types) {
  8853. params->Tuple.variables[i] = alloc_entity_param(scope, blank_token, param_types[i], false, true);
  8854. }
  8855. }
  8856. if (return_type != nullptr) {
  8857. array_init(&results->Tuple.variables, heap_allocator(), 1);
  8858. results->Tuple.variables[0] = alloc_entity_param(scope, blank_token, return_type, false, true);
  8859. }
  8860. Type *pt = alloc_type_proc(scope, params, param_types.count, results, return_type != nullptr ? 1 : 0, false, ProcCC_InlineAsm);
  8861. o->type = pt;
  8862. o->mode = Addressing_Value;
  8863. o->expr = node;
  8864. return Expr_Expr;
  8865. case_end;
  8866. case Ast_TypeidType:
  8867. case Ast_PolyType:
  8868. case Ast_ProcType:
  8869. case Ast_PointerType:
  8870. case Ast_ArrayType:
  8871. case Ast_DynamicArrayType:
  8872. case Ast_StructType:
  8873. case Ast_UnionType:
  8874. case Ast_EnumType:
  8875. case Ast_MapType:
  8876. case Ast_BitSetType:
  8877. o->mode = Addressing_Type;
  8878. o->type = check_type(c, node);
  8879. break;
  8880. }
  8881. kind = Expr_Expr;
  8882. o->expr = node;
  8883. return kind;
  8884. }
  8885. ExprKind check_expr_base(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
  8886. ExprKind kind = check_expr_base_internal(c, o, node, type_hint);
  8887. if (o->type != nullptr && is_type_untyped(o->type)) {
  8888. add_untyped(&c->checker->info, node, false, o->mode, o->type, o->value);
  8889. }
  8890. add_type_and_value(&c->checker->info, node, o->mode, o->type, o->value);
  8891. return kind;
  8892. }
  8893. void check_multi_expr_or_type(CheckerContext *c, Operand *o, Ast *e) {
  8894. check_expr_base(c, o, e, nullptr);
  8895. switch (o->mode) {
  8896. default:
  8897. return; // NOTE(bill): Valid
  8898. case Addressing_NoValue:
  8899. error_operand_no_value(o);
  8900. break;
  8901. }
  8902. o->mode = Addressing_Invalid;
  8903. }
  8904. void check_multi_expr(CheckerContext *c, Operand *o, Ast *e) {
  8905. check_expr_base(c, o, e, nullptr);
  8906. switch (o->mode) {
  8907. default:
  8908. return; // NOTE(bill): Valid
  8909. case Addressing_NoValue:
  8910. error_operand_no_value(o);
  8911. break;
  8912. case Addressing_Type:
  8913. error_operand_not_expression(o);
  8914. break;
  8915. }
  8916. o->mode = Addressing_Invalid;
  8917. }
  8918. void check_not_tuple(CheckerContext *c, Operand *o) {
  8919. if (o->mode == Addressing_Value) {
  8920. // NOTE(bill): Tuples are not first class thus never named
  8921. if (o->type->kind == Type_Tuple) {
  8922. isize count = o->type->Tuple.variables.count;
  8923. error(o->expr,
  8924. "%td-valued tuple found where single value expected", count);
  8925. o->mode = Addressing_Invalid;
  8926. GB_ASSERT(count != 1);
  8927. }
  8928. }
  8929. }
  8930. void check_expr(CheckerContext *c, Operand *o, Ast *e) {
  8931. check_multi_expr(c, o, e);
  8932. check_not_tuple(c, o);
  8933. }
  8934. void check_expr_or_type(CheckerContext *c, Operand *o, Ast *e, Type *type_hint) {
  8935. check_expr_base(c, o, e, type_hint);
  8936. check_not_tuple(c, o);
  8937. error_operand_no_value(o);
  8938. }
  8939. bool is_exact_value_zero(ExactValue const &v) {
  8940. switch (v.kind) {
  8941. case ExactValue_Invalid:
  8942. return true;
  8943. case ExactValue_Bool:
  8944. return !v.value_bool;
  8945. case ExactValue_String:
  8946. return v.value_string.len == 0;
  8947. case ExactValue_Integer:
  8948. return big_int_is_zero(&v.value_integer);
  8949. case ExactValue_Float:
  8950. return v.value_float == 0.0;
  8951. case ExactValue_Complex:
  8952. if (v.value_complex) {
  8953. return v.value_complex->real == 0.0 && v.value_complex->imag == 0.0;
  8954. }
  8955. return true;
  8956. case ExactValue_Quaternion:
  8957. if (v.value_quaternion) {
  8958. return v.value_quaternion->real == 0.0 &&
  8959. v.value_quaternion->imag == 0.0 &&
  8960. v.value_quaternion->jmag == 0.0 &&
  8961. v.value_quaternion->kmag == 0.0;
  8962. }
  8963. return true;
  8964. case ExactValue_Pointer:
  8965. return v.value_pointer == 0;
  8966. case ExactValue_Compound:
  8967. if (v.value_compound == nullptr) {
  8968. return true;
  8969. } else {
  8970. ast_node(cl, CompoundLit, v.value_compound);
  8971. if (cl->elems.count == 0) {
  8972. return true;
  8973. } else {
  8974. for_array(i, cl->elems) {
  8975. Ast *elem = cl->elems[i];
  8976. if (elem->tav.mode != Addressing_Constant) {
  8977. // if (elem->tav.value.kind != ExactValue_Invalid) {
  8978. return false;
  8979. // }
  8980. }
  8981. if (!is_exact_value_zero(elem->tav.value)) {
  8982. return false;
  8983. }
  8984. }
  8985. return true;
  8986. }
  8987. }
  8988. case ExactValue_Procedure:
  8989. return v.value_procedure == nullptr;
  8990. case ExactValue_Typeid:
  8991. return v.value_typeid == nullptr;
  8992. }
  8993. return true;
  8994. }
  8995. gbString write_expr_to_string(gbString str, Ast *node, bool shorthand);
  8996. gbString write_struct_fields_to_string(gbString str, Slice<Ast *> const &params) {
  8997. for_array(i, params) {
  8998. if (i > 0) {
  8999. str = gb_string_appendc(str, ", ");
  9000. }
  9001. str = write_expr_to_string(str, params[i], false);
  9002. }
  9003. return str;
  9004. }
  9005. gbString string_append_string(gbString str, String string) {
  9006. if (string.len > 0) {
  9007. return gb_string_append_length(str, &string[0], string.len);
  9008. }
  9009. return str;
  9010. }
  9011. gbString string_append_token(gbString str, Token token) {
  9012. if (token.kind == Token_String) {
  9013. str = gb_string_append_rune(str, '"');
  9014. } else if (token.kind == Token_Rune) {
  9015. str = gb_string_append_rune(str, '\'');
  9016. }
  9017. str = string_append_string(str, token.string);
  9018. if (token.kind == Token_String) {
  9019. str = gb_string_append_rune(str, '"');
  9020. } else if (token.kind == Token_Rune) {
  9021. str = gb_string_append_rune(str, '\'');
  9022. }
  9023. return str;
  9024. }
  9025. gbString write_expr_to_string(gbString str, Ast *node, bool shorthand) {
  9026. if (node == nullptr)
  9027. return str;
  9028. if (is_ast_stmt(node)) {
  9029. GB_ASSERT("stmt passed to write_expr_to_string");
  9030. }
  9031. switch (node->kind) {
  9032. default:
  9033. str = gb_string_appendc(str, "(BadExpr)");
  9034. break;
  9035. case_ast_node(i, Ident, node);
  9036. str = string_append_token(str, i->token);
  9037. case_end;
  9038. case_ast_node(i, Implicit, node);
  9039. str = string_append_token(str, *i);
  9040. case_end;
  9041. case_ast_node(bl, BasicLit, node);
  9042. str = string_append_token(str, bl->token);
  9043. case_end;
  9044. case_ast_node(bd, BasicDirective, node);
  9045. str = gb_string_append_rune(str, '#');
  9046. str = string_append_string(str, bd->name);
  9047. case_end;
  9048. case_ast_node(ud, Undef, node);
  9049. str = gb_string_appendc(str, "---");
  9050. case_end;
  9051. case_ast_node(pg, ProcGroup, node);
  9052. str = gb_string_appendc(str, "proc{");
  9053. for_array(i, pg->args) {
  9054. if (i > 0) str = gb_string_appendc(str, ", ");
  9055. str = write_expr_to_string(str, pg->args[i], shorthand);
  9056. }
  9057. str = gb_string_append_rune(str, '}');
  9058. case_end;
  9059. case_ast_node(pl, ProcLit, node);
  9060. str = write_expr_to_string(str, pl->type, shorthand);
  9061. if (pl->body) {
  9062. str = gb_string_appendc(str, " {...}");
  9063. } else {
  9064. str = gb_string_appendc(str, " ---");
  9065. }
  9066. case_end;
  9067. case_ast_node(cl, CompoundLit, node);
  9068. str = write_expr_to_string(str, cl->type, shorthand);
  9069. str = gb_string_append_rune(str, '{');
  9070. if (shorthand) {
  9071. str = gb_string_appendc(str, "...");
  9072. } else {
  9073. for_array(i, cl->elems) {
  9074. if (i > 0) str = gb_string_appendc(str, ", ");
  9075. str = write_expr_to_string(str, cl->elems[i], shorthand);
  9076. }
  9077. }
  9078. str = gb_string_append_rune(str, '}');
  9079. case_end;
  9080. case_ast_node(te, TagExpr, node);
  9081. str = gb_string_append_rune(str, '#');
  9082. str = string_append_token(str, te->name);
  9083. str = write_expr_to_string(str, te->expr, shorthand);
  9084. case_end;
  9085. case_ast_node(ue, UnaryExpr, node);
  9086. str = string_append_token(str, ue->op);
  9087. str = write_expr_to_string(str, ue->expr, shorthand);
  9088. case_end;
  9089. case_ast_node(de, DerefExpr, node);
  9090. str = write_expr_to_string(str, de->expr, shorthand);
  9091. str = gb_string_append_rune(str, '^');
  9092. case_end;
  9093. case_ast_node(be, BinaryExpr, node);
  9094. str = write_expr_to_string(str, be->left, shorthand);
  9095. str = gb_string_append_rune(str, ' ');
  9096. str = string_append_token(str, be->op);
  9097. str = gb_string_append_rune(str, ' ');
  9098. str = write_expr_to_string(str, be->right, shorthand);
  9099. case_end;
  9100. case_ast_node(te, TernaryExpr, node);
  9101. str = write_expr_to_string(str, te->cond, shorthand);
  9102. str = gb_string_appendc(str, " ? ");
  9103. str = write_expr_to_string(str, te->x, shorthand);
  9104. str = gb_string_appendc(str, " : ");
  9105. str = write_expr_to_string(str, te->y, shorthand);
  9106. case_end;
  9107. case_ast_node(te, TernaryIfExpr, node);
  9108. str = write_expr_to_string(str, te->x, shorthand);
  9109. str = gb_string_appendc(str, " if ");
  9110. str = write_expr_to_string(str, te->cond, shorthand);
  9111. str = gb_string_appendc(str, " else ");
  9112. str = write_expr_to_string(str, te->y, shorthand);
  9113. case_end;
  9114. case_ast_node(te, TernaryWhenExpr, node);
  9115. str = write_expr_to_string(str, te->x, shorthand);
  9116. str = gb_string_appendc(str, " when ");
  9117. str = write_expr_to_string(str, te->cond, shorthand);
  9118. str = gb_string_appendc(str, " else ");
  9119. str = write_expr_to_string(str, te->y, shorthand);
  9120. case_end;
  9121. case_ast_node(pe, ParenExpr, node);
  9122. str = gb_string_append_rune(str, '(');
  9123. str = write_expr_to_string(str, pe->expr, shorthand);
  9124. str = gb_string_append_rune(str, ')');
  9125. case_end;
  9126. case_ast_node(se, SelectorExpr, node);
  9127. str = write_expr_to_string(str, se->expr, shorthand);
  9128. str = string_append_token(str, se->token);
  9129. str = write_expr_to_string(str, se->selector, shorthand);
  9130. case_end;
  9131. case_ast_node(se, ImplicitSelectorExpr, node);
  9132. str = gb_string_append_rune(str, '.');
  9133. str = write_expr_to_string(str, se->selector, shorthand);
  9134. case_end;
  9135. case_ast_node(se, SelectorCallExpr, node);
  9136. str = write_expr_to_string(str, se->expr, shorthand);
  9137. str = gb_string_appendc(str, "(");
  9138. ast_node(ce, CallExpr, se->call);
  9139. isize start = se->modified_call ? 1 : 0;
  9140. for (isize i = start; i < ce->args.count; i++) {
  9141. Ast *arg = ce->args[i];
  9142. if (i > start) {
  9143. str = gb_string_appendc(str, ", ");
  9144. }
  9145. str = write_expr_to_string(str, arg, shorthand);
  9146. }
  9147. str = gb_string_appendc(str, ")");
  9148. case_end;
  9149. case_ast_node(ta, TypeAssertion, node);
  9150. str = write_expr_to_string(str, ta->expr, shorthand);
  9151. str = gb_string_appendc(str, ".(");
  9152. str = write_expr_to_string(str, ta->type, shorthand);
  9153. str = gb_string_append_rune(str, ')');
  9154. case_end;
  9155. case_ast_node(tc, TypeCast, node);
  9156. str = string_append_token(str, tc->token);
  9157. str = gb_string_append_rune(str, '(');
  9158. str = write_expr_to_string(str, tc->type, shorthand);
  9159. str = gb_string_append_rune(str, ')');
  9160. str = write_expr_to_string(str, tc->expr, shorthand);
  9161. case_end;
  9162. case_ast_node(ac, AutoCast, node);
  9163. str = string_append_token(str, ac->token);
  9164. str = gb_string_append_rune(str, ' ');
  9165. str = write_expr_to_string(str, ac->expr, shorthand);
  9166. case_end;
  9167. case_ast_node(ie, IndexExpr, node);
  9168. str = write_expr_to_string(str, ie->expr, shorthand);
  9169. str = gb_string_append_rune(str, '[');
  9170. str = write_expr_to_string(str, ie->index, shorthand);
  9171. str = gb_string_append_rune(str, ']');
  9172. case_end;
  9173. case_ast_node(se, SliceExpr, node);
  9174. str = write_expr_to_string(str, se->expr, shorthand);
  9175. str = gb_string_append_rune(str, '[');
  9176. str = write_expr_to_string(str, se->low, shorthand);
  9177. str = string_append_token(str, se->interval);
  9178. str = write_expr_to_string(str, se->high, shorthand);
  9179. str = gb_string_append_rune(str, ']');
  9180. case_end;
  9181. case_ast_node(e, Ellipsis, node);
  9182. str = gb_string_appendc(str, "..");
  9183. str = write_expr_to_string(str, e->expr, shorthand);
  9184. case_end;
  9185. case_ast_node(fv, FieldValue, node);
  9186. str = write_expr_to_string(str, fv->field, shorthand);
  9187. str = gb_string_appendc(str, " = ");
  9188. str = write_expr_to_string(str, fv->value, shorthand);
  9189. case_end;
  9190. case_ast_node(ht, HelperType, node);
  9191. str = gb_string_appendc(str, "#type ");
  9192. str = write_expr_to_string(str, ht->type, shorthand);
  9193. case_end;
  9194. case_ast_node(ht, DistinctType, node);
  9195. str = gb_string_appendc(str, "distinct ");
  9196. str = write_expr_to_string(str, ht->type, shorthand);
  9197. case_end;
  9198. case_ast_node(pt, PolyType, node);
  9199. str = gb_string_append_rune(str, '$');
  9200. str = write_expr_to_string(str, pt->type, shorthand);
  9201. if (pt->specialization != nullptr) {
  9202. str = gb_string_append_rune(str, '/');
  9203. str = write_expr_to_string(str, pt->specialization, shorthand);
  9204. }
  9205. case_end;
  9206. case_ast_node(pt, PointerType, node);
  9207. str = gb_string_append_rune(str, '^');
  9208. str = write_expr_to_string(str, pt->type, shorthand);
  9209. case_end;
  9210. case_ast_node(at, ArrayType, node);
  9211. str = gb_string_append_rune(str, '[');
  9212. if (at->count != nullptr &&
  9213. at->count->kind == Ast_UnaryExpr &&
  9214. at->count->UnaryExpr.op.kind == Token_Question) {
  9215. str = gb_string_appendc(str, "?");
  9216. } else {
  9217. str = write_expr_to_string(str, at->count, shorthand);
  9218. }
  9219. str = gb_string_append_rune(str, ']');
  9220. str = write_expr_to_string(str, at->elem, shorthand);
  9221. case_end;
  9222. case_ast_node(at, DynamicArrayType, node);
  9223. str = gb_string_appendc(str, "[dynamic]");
  9224. str = write_expr_to_string(str, at->elem, shorthand);
  9225. case_end;
  9226. case_ast_node(bs, BitSetType, node);
  9227. str = gb_string_appendc(str, "bit_set[");
  9228. str = write_expr_to_string(str, bs->elem, shorthand);
  9229. str = gb_string_appendc(str, "]");
  9230. case_end;
  9231. case_ast_node(mt, MapType, node);
  9232. str = gb_string_appendc(str, "map[");
  9233. str = write_expr_to_string(str, mt->key, shorthand);
  9234. str = gb_string_append_rune(str, ']');
  9235. str = write_expr_to_string(str, mt->value, shorthand);
  9236. case_end;
  9237. case_ast_node(f, Field, node);
  9238. if (f->flags&FieldFlag_using) {
  9239. str = gb_string_appendc(str, "using ");
  9240. }
  9241. if (f->flags&FieldFlag_no_alias) {
  9242. str = gb_string_appendc(str, "#no_alias ");
  9243. }
  9244. if (f->flags&FieldFlag_c_vararg) {
  9245. str = gb_string_appendc(str, "#c_vararg ");
  9246. }
  9247. if (f->flags&FieldFlag_auto_cast) {
  9248. str = gb_string_appendc(str, "auto_cast ");
  9249. }
  9250. for_array(i, f->names) {
  9251. Ast *name = f->names[i];
  9252. if (i > 0) str = gb_string_appendc(str, ", ");
  9253. str = write_expr_to_string(str, name, shorthand);
  9254. }
  9255. if (f->names.count > 0) {
  9256. if (f->type == nullptr && f->default_value != nullptr) {
  9257. str = gb_string_append_rune(str, ' ');
  9258. }
  9259. str = gb_string_appendc(str, ":");
  9260. }
  9261. if (f->type != nullptr) {
  9262. str = gb_string_append_rune(str, ' ');
  9263. str = write_expr_to_string(str, f->type, shorthand);
  9264. }
  9265. if (f->default_value != nullptr) {
  9266. if (f->type != nullptr) {
  9267. str = gb_string_append_rune(str, ' ');
  9268. }
  9269. str = gb_string_appendc(str, "= ");
  9270. str = write_expr_to_string(str, f->default_value, shorthand);
  9271. }
  9272. case_end;
  9273. case_ast_node(f, FieldList, node);
  9274. bool has_name = false;
  9275. for_array(i, f->list) {
  9276. ast_node(field, Field, f->list[i]);
  9277. if (field->names.count > 1) {
  9278. has_name = true;
  9279. break;
  9280. }
  9281. if (field->names.count == 0) {
  9282. continue;
  9283. }
  9284. if (!is_blank_ident(field->names[0])) {
  9285. has_name = true;
  9286. break;
  9287. }
  9288. }
  9289. for_array(i, f->list) {
  9290. if (i > 0) str = gb_string_appendc(str, ", ");
  9291. if (has_name) {
  9292. str = write_expr_to_string(str, f->list[i], shorthand);
  9293. } else {
  9294. ast_node(field, Field, f->list[i]);
  9295. if (field->flags&FieldFlag_using) {
  9296. str = gb_string_appendc(str, "using ");
  9297. }
  9298. if (field->flags&FieldFlag_no_alias) {
  9299. str = gb_string_appendc(str, "#no_alias ");
  9300. }
  9301. if (field->flags&FieldFlag_c_vararg) {
  9302. str = gb_string_appendc(str, "#c_vararg ");
  9303. }
  9304. str = write_expr_to_string(str, field->type, shorthand);
  9305. }
  9306. }
  9307. case_end;
  9308. case_ast_node(ce, CallExpr, node);
  9309. switch (ce->inlining) {
  9310. case ProcInlining_inline:
  9311. str = gb_string_appendc(str, "inline ");
  9312. break;
  9313. case ProcInlining_no_inline:
  9314. str = gb_string_appendc(str, "no_inline ");
  9315. break;
  9316. }
  9317. str = write_expr_to_string(str, ce->proc, shorthand);
  9318. str = gb_string_appendc(str, "(");
  9319. for_array(i, ce->args) {
  9320. Ast *arg = ce->args[i];
  9321. if (i > 0) {
  9322. str = gb_string_appendc(str, ", ");
  9323. }
  9324. str = write_expr_to_string(str, arg, shorthand);
  9325. }
  9326. str = gb_string_appendc(str, ")");
  9327. case_end;
  9328. case_ast_node(tt, TypeidType, node);
  9329. str = gb_string_appendc(str, "typeid");
  9330. if (tt->specialization) {
  9331. str = gb_string_appendc(str, "/");
  9332. str = write_expr_to_string(str, tt->specialization, shorthand);
  9333. }
  9334. case_end;
  9335. case_ast_node(pt, ProcType, node);
  9336. str = gb_string_appendc(str, "proc(");
  9337. str = write_expr_to_string(str, pt->params, shorthand);
  9338. str = gb_string_appendc(str, ")");
  9339. if (pt->results != nullptr) {
  9340. str = gb_string_appendc(str, " -> ");
  9341. bool parens_needed = false;
  9342. if (pt->results && pt->results->kind == Ast_FieldList) {
  9343. for_array(i, pt->results->FieldList.list) {
  9344. Ast *field = pt->results->FieldList.list[i];
  9345. ast_node(f, Field, field);
  9346. if (f->names.count != 0) {
  9347. parens_needed = true;
  9348. break;
  9349. }
  9350. }
  9351. }
  9352. if (parens_needed) {
  9353. str = gb_string_append_rune(str, '(');
  9354. }
  9355. str = write_expr_to_string(str, pt->results, shorthand);
  9356. if (parens_needed) {
  9357. str = gb_string_append_rune(str, ')');
  9358. }
  9359. }
  9360. case_end;
  9361. case_ast_node(st, StructType, node);
  9362. str = gb_string_appendc(str, "struct ");
  9363. if (st->is_packed) str = gb_string_appendc(str, "#packed ");
  9364. if (st->is_raw_union) str = gb_string_appendc(str, "#raw_union ");
  9365. str = gb_string_append_rune(str, '{');
  9366. if (shorthand) {
  9367. str = gb_string_appendc(str, "...");
  9368. } else {
  9369. str = write_struct_fields_to_string(str, st->fields);
  9370. }
  9371. str = gb_string_append_rune(str, '}');
  9372. case_end;
  9373. case_ast_node(st, UnionType, node);
  9374. str = gb_string_appendc(str, "union ");
  9375. str = gb_string_append_rune(str, '{');
  9376. if (shorthand) {
  9377. str = gb_string_appendc(str, "...");
  9378. } else {
  9379. str = write_struct_fields_to_string(str, st->variants);
  9380. }
  9381. str = gb_string_append_rune(str, '}');
  9382. case_end;
  9383. case_ast_node(et, EnumType, node);
  9384. str = gb_string_appendc(str, "enum ");
  9385. if (et->base_type != nullptr) {
  9386. str = write_expr_to_string(str, et->base_type, shorthand);
  9387. str = gb_string_append_rune(str, ' ');
  9388. }
  9389. str = gb_string_append_rune(str, '{');
  9390. if (shorthand) {
  9391. str = gb_string_appendc(str, "...");
  9392. } else {
  9393. for_array(i, et->fields) {
  9394. if (i > 0) {
  9395. str = gb_string_appendc(str, ", ");
  9396. }
  9397. str = write_expr_to_string(str, et->fields[i], shorthand);
  9398. }
  9399. }
  9400. str = gb_string_append_rune(str, '}');
  9401. case_end;
  9402. case_ast_node(rt, RelativeType, node);
  9403. str = write_expr_to_string(str, rt->tag, shorthand);
  9404. str = gb_string_appendc(str, "" );
  9405. str = write_expr_to_string(str, rt->type, shorthand);
  9406. case_end;
  9407. case_ast_node(ia, InlineAsmExpr, node);
  9408. str = gb_string_appendc(str, "asm(");
  9409. for_array(i, ia->param_types) {
  9410. if (i > 0) {
  9411. str = gb_string_appendc(str, ", ");
  9412. }
  9413. str = write_expr_to_string(str, ia->param_types[i], shorthand);
  9414. }
  9415. str = gb_string_appendc(str, ")");
  9416. if (ia->return_type != nullptr) {
  9417. str = gb_string_appendc(str, " -> ");
  9418. str = write_expr_to_string(str, ia->return_type, shorthand);
  9419. }
  9420. if (ia->has_side_effects) {
  9421. str = gb_string_appendc(str, " #side_effects");
  9422. }
  9423. if (ia->is_align_stack) {
  9424. str = gb_string_appendc(str, " #stack_align");
  9425. }
  9426. if (ia->dialect) {
  9427. str = gb_string_appendc(str, " #");
  9428. str = gb_string_appendc(str, inline_asm_dialect_strings[ia->dialect]);
  9429. }
  9430. str = gb_string_appendc(str, " {");
  9431. if (shorthand) {
  9432. str = gb_string_appendc(str, "...");
  9433. } else {
  9434. str = write_expr_to_string(str, ia->asm_string, shorthand);
  9435. str = gb_string_appendc(str, ", ");
  9436. str = write_expr_to_string(str, ia->constraints_string, shorthand);
  9437. }
  9438. str = gb_string_appendc(str, "}");
  9439. case_end;
  9440. }
  9441. return str;
  9442. }
  9443. gbString expr_to_string(Ast *expression) {
  9444. return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression, false);
  9445. }
  9446. gbString expr_to_string_shorthand(Ast *expression) {
  9447. return write_expr_to_string(gb_string_make(heap_allocator(), ""), expression, true);
  9448. }