check_expr.cpp 261 KB

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