check_expr.cpp 232 KB

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