check_expr.cpp 202 KB

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