check_expr.cpp 209 KB

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