check_expr.cpp 183 KB

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