check_expr.cpp 221 KB

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