check_expr.cpp 243 KB

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