parser.cpp 147 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191
  1. struct AstNode;
  2. struct Scope;
  3. struct DeclInfo;
  4. enum ParseFileError {
  5. ParseFile_None,
  6. ParseFile_WrongExtension,
  7. ParseFile_InvalidFile,
  8. ParseFile_EmptyFile,
  9. ParseFile_Permission,
  10. ParseFile_NotFound,
  11. ParseFile_InvalidToken,
  12. ParseFile_Count,
  13. };
  14. struct CommentGroup {
  15. Array<Token> list; // Token_Comment
  16. };
  17. enum ImportedFileKind {
  18. ImportedFile_Normal,
  19. ImportedFile_Shared,
  20. ImportedFile_Init,
  21. };
  22. struct ImportedFile {
  23. ImportedFileKind kind;
  24. String path;
  25. String rel_path;
  26. TokenPos pos; // import
  27. isize index;
  28. };
  29. struct AstFile {
  30. isize id;
  31. String fullpath;
  32. gbArena arena;
  33. Tokenizer tokenizer;
  34. Array<Token> tokens;
  35. isize curr_token_index;
  36. Token curr_token;
  37. Token prev_token; // previous non-comment
  38. // >= 0: In Expression
  39. // < 0: In Control Clause
  40. // NOTE(bill): Used to prevent type literals in control clauses
  41. isize expr_level;
  42. bool allow_range; // NOTE(bill): Ranges are only allowed in certain cases
  43. bool in_foreign_block;
  44. bool allow_type;
  45. isize when_level;
  46. Array<AstNode *> decls;
  47. ImportedFileKind file_kind;
  48. bool is_global_scope;
  49. Array<AstNode *> imports_and_exports; // 'import' 'using import' 'export'
  50. AstNode * curr_proc;
  51. isize scope_level;
  52. Scope * scope; // NOTE(bill): Created in checker
  53. DeclInfo * decl_info; // NOTE(bill): Created in checker
  54. CommentGroup lead_comment; // Comment (block) before the decl
  55. CommentGroup line_comment; // Comment after the semicolon
  56. CommentGroup docs; // current docs
  57. Array<CommentGroup> comments; // All the comments!
  58. #define PARSER_MAX_FIX_COUNT 6
  59. isize fix_count;
  60. TokenPos fix_prev_pos;
  61. };
  62. struct Parser {
  63. String init_fullpath;
  64. Array<AstFile *> files;
  65. Array<ImportedFile> imports;
  66. isize total_token_count;
  67. isize total_line_count;
  68. gbMutex file_add_mutex;
  69. gbMutex file_decl_mutex;
  70. };
  71. enum ProcInlining {
  72. ProcInlining_none = 0,
  73. ProcInlining_inline = 1,
  74. ProcInlining_no_inline = 2,
  75. };
  76. enum ProcTag {
  77. ProcTag_bounds_check = 1<<0,
  78. ProcTag_no_bounds_check = 1<<1,
  79. ProcTag_require_results = 1<<4,
  80. };
  81. enum ProcCallingConvention {
  82. ProcCC_Invalid = 0,
  83. ProcCC_Odin,
  84. ProcCC_Contextless,
  85. ProcCC_CDecl,
  86. ProcCC_StdCall,
  87. ProcCC_FastCall,
  88. // TODO(bill): Add extra calling conventions
  89. // ProcCC_VectorCall,
  90. // ProcCC_ClrCall,
  91. ProcCC_ForeignBlockDefault = -1,
  92. };
  93. enum StmtStateFlag {
  94. StmtStateFlag_bounds_check = 1<<0,
  95. StmtStateFlag_no_bounds_check = 1<<1,
  96. };
  97. enum FieldFlag {
  98. FieldFlag_NONE = 0,
  99. FieldFlag_ellipsis = 1<<0,
  100. FieldFlag_using = 1<<1,
  101. FieldFlag_no_alias = 1<<2,
  102. FieldFlag_c_vararg = 1<<3,
  103. FieldFlag_Signature = FieldFlag_ellipsis|FieldFlag_using|FieldFlag_no_alias|FieldFlag_c_vararg,
  104. FieldFlag_Struct = FieldFlag_using,
  105. };
  106. enum StmtAllowFlag {
  107. StmtAllowFlag_None = 0,
  108. StmtAllowFlag_In = 1<<0,
  109. StmtAllowFlag_Label = 1<<1,
  110. };
  111. Array<AstNode *> make_ast_node_array(AstFile *f, isize init_capacity = 8) {
  112. Array<AstNode *> a;
  113. array_init(&a, heap_allocator(), init_capacity);
  114. return a;
  115. }
  116. // NOTE(bill): This massive define is so it is possible to create a discriminated union (and extra debug info)
  117. // for the AstNode. I personally prefer discriminated unions over subtype polymorphism as I can preallocate
  118. // all the nodes and even memcpy in a different kind of node
  119. #define AST_NODE_KINDS \
  120. AST_NODE_KIND(Ident, "identifier", struct { \
  121. Token token; \
  122. }) \
  123. AST_NODE_KIND(Implicit, "implicit", Token) \
  124. AST_NODE_KIND(Undef, "undef", Token) \
  125. AST_NODE_KIND(BasicLit, "basic literal", Token) \
  126. AST_NODE_KIND(BasicDirective, "basic directive", struct { \
  127. Token token; \
  128. String name; \
  129. }) \
  130. AST_NODE_KIND(Ellipsis, "ellipsis", struct { \
  131. Token token; \
  132. AstNode *expr; \
  133. }) \
  134. AST_NODE_KIND(ProcLit, "procedure literal", struct { \
  135. AstNode *type; \
  136. AstNode *body; \
  137. u64 tags; \
  138. ProcInlining inlining; \
  139. }) \
  140. AST_NODE_KIND(CompoundLit, "compound literal", struct { \
  141. AstNode *type; \
  142. Array<AstNode *> elems; \
  143. Token open, close; \
  144. }) \
  145. AST_NODE_KIND(Alias, "alias", struct { \
  146. Token token; \
  147. AstNode *expr; \
  148. }) \
  149. AST_NODE_KIND(_ExprBegin, "", i32) \
  150. AST_NODE_KIND(BadExpr, "bad expression", struct { Token begin, end; }) \
  151. AST_NODE_KIND(TagExpr, "tag expression", struct { Token token, name; AstNode *expr; }) \
  152. AST_NODE_KIND(RunExpr, "run expression", struct { Token token, name; AstNode *expr; }) \
  153. AST_NODE_KIND(UnaryExpr, "unary expression", struct { Token op; AstNode *expr; }) \
  154. AST_NODE_KIND(BinaryExpr, "binary expression", struct { Token op; AstNode *left, *right; } ) \
  155. AST_NODE_KIND(ParenExpr, "parentheses expression", struct { AstNode *expr; Token open, close; }) \
  156. AST_NODE_KIND(SelectorExpr, "selector expression", struct { Token token; AstNode *expr, *selector; }) \
  157. AST_NODE_KIND(IndexExpr, "index expression", struct { AstNode *expr, *index; Token open, close; }) \
  158. AST_NODE_KIND(DerefExpr, "dereference expression", struct { Token op; AstNode *expr; }) \
  159. AST_NODE_KIND(SliceExpr, "slice expression", struct { \
  160. AstNode *expr; \
  161. Token open, close; \
  162. Token interval; \
  163. AstNode *low, *high; \
  164. }) \
  165. AST_NODE_KIND(CallExpr, "call expression", struct { \
  166. AstNode * proc; \
  167. Array<AstNode *> args; \
  168. Token open; \
  169. Token close; \
  170. Token ellipsis; \
  171. }) \
  172. AST_NODE_KIND(MacroCallExpr, "macro call expression", struct { \
  173. AstNode * macro; \
  174. Token bang; \
  175. Array<AstNode *> args; \
  176. Token open; \
  177. Token close; \
  178. }) \
  179. AST_NODE_KIND(FieldValue, "field value", struct { Token eq; AstNode *field, *value; }) \
  180. AST_NODE_KIND(TernaryExpr, "ternary expression", struct { AstNode *cond, *x, *y; }) \
  181. AST_NODE_KIND(TypeAssertion, "type assertion", struct { AstNode *expr; Token dot; AstNode *type; }) \
  182. AST_NODE_KIND(TypeCast, "type cast", struct { Token token; AstNode *type, *expr; }) \
  183. AST_NODE_KIND(_ExprEnd, "", i32) \
  184. AST_NODE_KIND(_StmtBegin, "", i32) \
  185. AST_NODE_KIND(BadStmt, "bad statement", struct { Token begin, end; }) \
  186. AST_NODE_KIND(EmptyStmt, "empty statement", struct { Token token; }) \
  187. AST_NODE_KIND(ExprStmt, "expression statement", struct { AstNode *expr; } ) \
  188. AST_NODE_KIND(TagStmt, "tag statement", struct { \
  189. Token token; \
  190. Token name; \
  191. AstNode *stmt; \
  192. }) \
  193. AST_NODE_KIND(AssignStmt, "assign statement", struct { \
  194. Token op; \
  195. Array<AstNode *> lhs, rhs; \
  196. }) \
  197. AST_NODE_KIND(IncDecStmt, "increment decrement statement", struct { \
  198. Token op; \
  199. AstNode *expr; \
  200. }) \
  201. AST_NODE_KIND(_ComplexStmtBegin, "", i32) \
  202. AST_NODE_KIND(BlockStmt, "block statement", struct { \
  203. Array<AstNode *> stmts; \
  204. Token open, close; \
  205. }) \
  206. AST_NODE_KIND(IfStmt, "if statement", struct { \
  207. Token token; \
  208. AstNode *init; \
  209. AstNode *cond; \
  210. AstNode *body; \
  211. AstNode *else_stmt; \
  212. }) \
  213. AST_NODE_KIND(WhenStmt, "when statement", struct { \
  214. Token token; \
  215. AstNode *cond; \
  216. AstNode *body; \
  217. AstNode *else_stmt; \
  218. bool is_cond_determined; \
  219. bool determined_cond; \
  220. }) \
  221. AST_NODE_KIND(ReturnStmt, "return statement", struct { \
  222. Token token; \
  223. Array<AstNode *> results; \
  224. }) \
  225. AST_NODE_KIND(ForStmt, "for statement", struct { \
  226. Token token; \
  227. AstNode *label; \
  228. AstNode *init; \
  229. AstNode *cond; \
  230. AstNode *post; \
  231. AstNode *body; \
  232. }) \
  233. AST_NODE_KIND(RangeStmt, "range statement", struct { \
  234. Token token; \
  235. AstNode *label; \
  236. AstNode *value; \
  237. AstNode *index; \
  238. Token in_token; \
  239. AstNode *expr; \
  240. AstNode *body; \
  241. }) \
  242. AST_NODE_KIND(CaseClause, "case clause", struct { \
  243. Token token; \
  244. Array<AstNode *> list; \
  245. Array<AstNode *> stmts; \
  246. }) \
  247. AST_NODE_KIND(SwitchStmt, "switch statement", struct { \
  248. Token token; \
  249. AstNode *label; \
  250. AstNode *init; \
  251. AstNode *tag; \
  252. AstNode *body; \
  253. }) \
  254. AST_NODE_KIND(TypeSwitchStmt, "type switch statement", struct { \
  255. Token token; \
  256. AstNode *label; \
  257. AstNode *tag; \
  258. AstNode *body; \
  259. }) \
  260. AST_NODE_KIND(DeferStmt, "defer statement", struct { Token token; AstNode *stmt; }) \
  261. AST_NODE_KIND(BranchStmt, "branch statement", struct { Token token; AstNode *label; }) \
  262. AST_NODE_KIND(UsingStmt, "using statement", struct { \
  263. Token token; \
  264. Array<AstNode *> list; \
  265. }) \
  266. AST_NODE_KIND(AsmOperand, "assembly operand", struct { \
  267. Token string; \
  268. AstNode *operand; \
  269. }) \
  270. AST_NODE_KIND(AsmStmt, "assembly statement", struct { \
  271. Token token; \
  272. bool is_volatile; \
  273. Token open, close; \
  274. Token code_string; \
  275. AstNode *output_list; \
  276. AstNode *input_list; \
  277. AstNode *clobber_list; \
  278. isize output_count, input_count, clobber_count; \
  279. }) \
  280. AST_NODE_KIND(PushContext, "context <- statement", struct { \
  281. Token token; \
  282. AstNode *expr; \
  283. AstNode *body; \
  284. }) \
  285. AST_NODE_KIND(_ComplexStmtEnd, "", i32) \
  286. AST_NODE_KIND(_StmtEnd, "", i32) \
  287. AST_NODE_KIND(_DeclBegin, "", i32) \
  288. AST_NODE_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
  289. AST_NODE_KIND(ForeignBlockDecl, "foreign block declaration", struct { \
  290. Token token; \
  291. AstNode * foreign_library; \
  292. Token open, close; \
  293. Array<AstNode *> decls; \
  294. Array<AstNode *> attributes; \
  295. bool been_handled; \
  296. CommentGroup docs; \
  297. }) \
  298. AST_NODE_KIND(Label, "label", struct { \
  299. Token token; \
  300. AstNode *name; \
  301. }) \
  302. AST_NODE_KIND(ValueDecl, "value declaration", struct { \
  303. Array<AstNode *> names; \
  304. AstNode * type; \
  305. Array<AstNode *> values; \
  306. bool is_using; \
  307. bool is_mutable; \
  308. bool been_handled; \
  309. Array<AstNode *> attributes; \
  310. CommentGroup docs; \
  311. CommentGroup comment; \
  312. }) \
  313. AST_NODE_KIND(ImportDecl, "import declaration", struct { \
  314. AstFile *file; \
  315. Token token; \
  316. Token relpath; \
  317. String fullpath; \
  318. Token import_name; \
  319. bool is_using; \
  320. bool been_handled; \
  321. CommentGroup docs; \
  322. CommentGroup comment; \
  323. }) \
  324. AST_NODE_KIND(ExportDecl, "export declaration", struct { \
  325. AstFile *file; \
  326. Token token; \
  327. Token relpath; \
  328. String fullpath; \
  329. bool been_handled; \
  330. CommentGroup docs; \
  331. CommentGroup comment; \
  332. }) \
  333. AST_NODE_KIND(ForeignImportDecl, "foreign import declaration", struct { \
  334. Token token; \
  335. Token filepath; \
  336. Token library_name; \
  337. String base_dir; \
  338. String collection_name; \
  339. String fullpath; \
  340. bool been_handled; \
  341. CommentGroup docs; \
  342. CommentGroup comment; \
  343. }) \
  344. AST_NODE_KIND(_DeclEnd, "", i32) \
  345. AST_NODE_KIND(Attribute, "attribute", struct { \
  346. Token token; \
  347. AstNode *type; \
  348. Array<AstNode *> elems; \
  349. Token open, close; \
  350. }) \
  351. AST_NODE_KIND(Field, "field", struct { \
  352. Array<AstNode *> names; \
  353. AstNode * type; \
  354. AstNode * default_value; \
  355. u32 flags; \
  356. CommentGroup docs; \
  357. CommentGroup comment; \
  358. }) \
  359. AST_NODE_KIND(FieldList, "field list", struct { \
  360. Token token; \
  361. Array<AstNode *> list; \
  362. }) \
  363. AST_NODE_KIND(UnionField, "union field", struct { \
  364. AstNode *name; \
  365. AstNode *list; \
  366. }) \
  367. AST_NODE_KIND(_TypeBegin, "", i32) \
  368. AST_NODE_KIND(TypeType, "type", struct { \
  369. Token token; \
  370. AstNode *specialization; \
  371. }) \
  372. AST_NODE_KIND(HelperType, "helper type", struct { \
  373. Token token; \
  374. AstNode *type; \
  375. }) \
  376. AST_NODE_KIND(AliasType, "alias type", struct { \
  377. Token token; \
  378. AstNode *type; \
  379. }) \
  380. AST_NODE_KIND(PolyType, "polymorphic type", struct { \
  381. Token token; \
  382. AstNode *type; \
  383. AstNode *specialization; \
  384. }) \
  385. AST_NODE_KIND(ProcType, "procedure type", struct { \
  386. Token token; \
  387. AstNode *params; \
  388. AstNode *results; \
  389. u64 tags; \
  390. ProcCallingConvention calling_convention; \
  391. bool generic; \
  392. }) \
  393. AST_NODE_KIND(PointerType, "pointer type", struct { \
  394. Token token; \
  395. AstNode *type; \
  396. }) \
  397. AST_NODE_KIND(ArrayType, "array type", struct { \
  398. Token token; \
  399. AstNode *count; \
  400. AstNode *elem; \
  401. }) \
  402. AST_NODE_KIND(DynamicArrayType, "dynamic array type", struct { \
  403. Token token; \
  404. AstNode *elem; \
  405. }) \
  406. AST_NODE_KIND(VectorType, "vector type", struct { \
  407. Token token; \
  408. AstNode *count; \
  409. AstNode *elem; \
  410. }) \
  411. AST_NODE_KIND(StructType, "struct type", struct { \
  412. Token token; \
  413. Array<AstNode *> fields; \
  414. isize field_count; \
  415. AstNode * polymorphic_params; \
  416. bool is_packed; \
  417. bool is_ordered; \
  418. bool is_raw_union; \
  419. AstNode * align; \
  420. }) \
  421. AST_NODE_KIND(UnionType, "union type", struct { \
  422. Token token; \
  423. Array<AstNode *> variants; \
  424. AstNode * align; \
  425. }) \
  426. AST_NODE_KIND(EnumType, "enum type", struct { \
  427. Token token; \
  428. AstNode * base_type; \
  429. Array<AstNode *> fields; /* FieldValue */ \
  430. }) \
  431. AST_NODE_KIND(BitFieldType, "bit field type", struct { \
  432. Token token; \
  433. Array<AstNode *> fields; /* FieldValue with : */ \
  434. AstNode * align; \
  435. }) \
  436. AST_NODE_KIND(MapType, "map type", struct { \
  437. Token token; \
  438. AstNode *count; \
  439. AstNode *key; \
  440. AstNode *value; \
  441. }) \
  442. AST_NODE_KIND(_TypeEnd, "", i32)
  443. enum AstNodeKind {
  444. AstNode_Invalid,
  445. #define AST_NODE_KIND(_kind_name_, ...) GB_JOIN2(AstNode_, _kind_name_),
  446. AST_NODE_KINDS
  447. #undef AST_NODE_KIND
  448. AstNode_Count,
  449. };
  450. String const ast_node_strings[] = {
  451. {cast(u8 *)"invalid node", gb_size_of("invalid node")},
  452. #define AST_NODE_KIND(_kind_name_, name, ...) {cast(u8 *)name, gb_size_of(name)-1},
  453. AST_NODE_KINDS
  454. #undef AST_NODE_KIND
  455. };
  456. #define AST_NODE_KIND(_kind_name_, name, ...) typedef __VA_ARGS__ GB_JOIN2(AstNode, _kind_name_);
  457. AST_NODE_KINDS
  458. #undef AST_NODE_KIND
  459. struct AstNode {
  460. AstNodeKind kind;
  461. u32 stmt_state_flags;
  462. AstFile * file;
  463. union {
  464. #define AST_NODE_KIND(_kind_name_, name, ...) GB_JOIN2(AstNode, _kind_name_) _kind_name_;
  465. AST_NODE_KINDS
  466. #undef AST_NODE_KIND
  467. };
  468. };
  469. #define ast_node(n_, Kind_, node_) GB_JOIN2(AstNode, Kind_) *n_ = &(node_)->Kind_; GB_ASSERT((node_)->kind == GB_JOIN2(AstNode_, Kind_))
  470. #define case_ast_node(n_, Kind_, node_) case GB_JOIN2(AstNode_, Kind_): { ast_node(n_, Kind_, node_);
  471. #ifndef case_end
  472. #define case_end } break;
  473. #endif
  474. gb_inline bool is_ast_node_expr(AstNode *node) {
  475. return gb_is_between(node->kind, AstNode__ExprBegin+1, AstNode__ExprEnd-1);
  476. }
  477. gb_inline bool is_ast_node_stmt(AstNode *node) {
  478. return gb_is_between(node->kind, AstNode__StmtBegin+1, AstNode__StmtEnd-1);
  479. }
  480. gb_inline bool is_ast_node_complex_stmt(AstNode *node) {
  481. return gb_is_between(node->kind, AstNode__ComplexStmtBegin+1, AstNode__ComplexStmtEnd-1);
  482. }
  483. gb_inline bool is_ast_node_decl(AstNode *node) {
  484. return gb_is_between(node->kind, AstNode__DeclBegin+1, AstNode__DeclEnd-1);
  485. }
  486. gb_inline bool is_ast_node_type(AstNode *node) {
  487. return gb_is_between(node->kind, AstNode__TypeBegin+1, AstNode__TypeEnd-1);
  488. }
  489. gb_inline bool is_ast_node_when_stmt(AstNode *node) {
  490. return node->kind == AstNode_WhenStmt;
  491. }
  492. Token ast_node_token(AstNode *node) {
  493. switch (node->kind) {
  494. case AstNode_Ident: return node->Ident.token;
  495. case AstNode_Implicit: return node->Implicit;
  496. case AstNode_Undef: return node->Undef;
  497. case AstNode_BasicLit: return node->BasicLit;
  498. case AstNode_BasicDirective: return node->BasicDirective.token;
  499. case AstNode_ProcLit: return ast_node_token(node->ProcLit.type);
  500. case AstNode_CompoundLit:
  501. if (node->CompoundLit.type != nullptr) {
  502. return ast_node_token(node->CompoundLit.type);
  503. }
  504. return node->CompoundLit.open;
  505. case AstNode_Alias: return node->Alias.token;
  506. case AstNode_TagExpr: return node->TagExpr.token;
  507. case AstNode_RunExpr: return node->RunExpr.token;
  508. case AstNode_BadExpr: return node->BadExpr.begin;
  509. case AstNode_UnaryExpr: return node->UnaryExpr.op;
  510. case AstNode_BinaryExpr: return ast_node_token(node->BinaryExpr.left);
  511. case AstNode_ParenExpr: return node->ParenExpr.open;
  512. case AstNode_CallExpr: return ast_node_token(node->CallExpr.proc);
  513. case AstNode_MacroCallExpr: return ast_node_token(node->MacroCallExpr.macro);
  514. case AstNode_SelectorExpr:
  515. if (node->SelectorExpr.selector != nullptr) {
  516. return ast_node_token(node->SelectorExpr.selector);
  517. }
  518. return node->SelectorExpr.token;
  519. case AstNode_IndexExpr: return node->IndexExpr.open;
  520. case AstNode_SliceExpr: return node->SliceExpr.open;
  521. case AstNode_Ellipsis: return node->Ellipsis.token;
  522. case AstNode_FieldValue: return node->FieldValue.eq;
  523. case AstNode_DerefExpr: return node->DerefExpr.op;
  524. case AstNode_TernaryExpr: return ast_node_token(node->TernaryExpr.cond);
  525. case AstNode_TypeAssertion: return ast_node_token(node->TypeAssertion.expr);
  526. case AstNode_TypeCast: return node->TypeCast.token;
  527. case AstNode_BadStmt: return node->BadStmt.begin;
  528. case AstNode_EmptyStmt: return node->EmptyStmt.token;
  529. case AstNode_ExprStmt: return ast_node_token(node->ExprStmt.expr);
  530. case AstNode_TagStmt: return node->TagStmt.token;
  531. case AstNode_AssignStmt: return node->AssignStmt.op;
  532. case AstNode_IncDecStmt: return ast_node_token(node->IncDecStmt.expr);
  533. case AstNode_BlockStmt: return node->BlockStmt.open;
  534. case AstNode_IfStmt: return node->IfStmt.token;
  535. case AstNode_WhenStmt: return node->WhenStmt.token;
  536. case AstNode_ReturnStmt: return node->ReturnStmt.token;
  537. case AstNode_ForStmt: return node->ForStmt.token;
  538. case AstNode_RangeStmt: return node->RangeStmt.token;
  539. case AstNode_CaseClause: return node->CaseClause.token;
  540. case AstNode_SwitchStmt: return node->SwitchStmt.token;
  541. case AstNode_TypeSwitchStmt: return node->TypeSwitchStmt.token;
  542. case AstNode_DeferStmt: return node->DeferStmt.token;
  543. case AstNode_BranchStmt: return node->BranchStmt.token;
  544. case AstNode_UsingStmt: return node->UsingStmt.token;
  545. case AstNode_AsmStmt: return node->AsmStmt.token;
  546. case AstNode_PushContext: return node->PushContext.token;
  547. case AstNode_BadDecl: return node->BadDecl.begin;
  548. case AstNode_Label: return node->Label.token;
  549. case AstNode_ValueDecl: return ast_node_token(node->ValueDecl.names[0]);
  550. case AstNode_ImportDecl: return node->ImportDecl.token;
  551. case AstNode_ExportDecl: return node->ExportDecl.token;
  552. case AstNode_ForeignImportDecl: return node->ForeignImportDecl.token;
  553. case AstNode_ForeignBlockDecl: return node->ForeignBlockDecl.token;
  554. case AstNode_Attribute:
  555. return node->Attribute.token;
  556. case AstNode_Field:
  557. if (node->Field.names.count > 0) {
  558. return ast_node_token(node->Field.names[0]);
  559. }
  560. return ast_node_token(node->Field.type);
  561. case AstNode_FieldList:
  562. return node->FieldList.token;
  563. case AstNode_UnionField:
  564. return ast_node_token(node->UnionField.name);
  565. case AstNode_TypeType: return node->TypeType.token;
  566. case AstNode_HelperType: return node->HelperType.token;
  567. case AstNode_AliasType: return node->AliasType.token;
  568. case AstNode_PolyType: return node->PolyType.token;
  569. case AstNode_ProcType: return node->ProcType.token;
  570. case AstNode_PointerType: return node->PointerType.token;
  571. case AstNode_ArrayType: return node->ArrayType.token;
  572. case AstNode_DynamicArrayType: return node->DynamicArrayType.token;
  573. case AstNode_VectorType: return node->VectorType.token;
  574. case AstNode_StructType: return node->StructType.token;
  575. case AstNode_UnionType: return node->UnionType.token;
  576. case AstNode_EnumType: return node->EnumType.token;
  577. case AstNode_BitFieldType: return node->BitFieldType.token;
  578. case AstNode_MapType: return node->MapType.token;
  579. }
  580. return empty_token;
  581. }
  582. AstNode *clone_ast_node(gbAllocator a, AstNode *node);
  583. Array<AstNode *> clone_ast_node_array(gbAllocator a, Array<AstNode *> array) {
  584. Array<AstNode *> result = {};
  585. if (array.count > 0) {
  586. array_init_count(&result, a, array.count);
  587. for_array(i, array) {
  588. result[i] = clone_ast_node(a, array[i]);
  589. }
  590. }
  591. return result;
  592. }
  593. AstNode *clone_ast_node(gbAllocator a, AstNode *node) {
  594. if (node == nullptr) {
  595. return nullptr;
  596. }
  597. AstNode *n = gb_alloc_item(a, AstNode);
  598. gb_memmove(n, node, gb_size_of(AstNode));
  599. switch (n->kind) {
  600. default: GB_PANIC("Unhandled AstNode %.*s", LIT(ast_node_strings[n->kind])); break;
  601. case AstNode_Invalid: break;
  602. case AstNode_Ident: break;
  603. case AstNode_Implicit: break;
  604. case AstNode_Undef: break;
  605. case AstNode_BasicLit: break;
  606. case AstNode_BasicDirective: break;
  607. case AstNode_PolyType:
  608. n->PolyType.type = clone_ast_node(a, n->PolyType.type);
  609. n->PolyType.specialization = clone_ast_node(a, n->PolyType.specialization);
  610. break;
  611. case AstNode_Ellipsis:
  612. n->Ellipsis.expr = clone_ast_node(a, n->Ellipsis.expr);
  613. break;
  614. case AstNode_ProcLit:
  615. n->ProcLit.type = clone_ast_node(a, n->ProcLit.type);
  616. n->ProcLit.body = clone_ast_node(a, n->ProcLit.body);
  617. break;
  618. case AstNode_CompoundLit:
  619. n->CompoundLit.type = clone_ast_node(a, n->CompoundLit.type);
  620. n->CompoundLit.elems = clone_ast_node_array(a, n->CompoundLit.elems);
  621. break;
  622. case AstNode_Alias:
  623. n->Alias.expr = clone_ast_node(a, n->Alias.expr);
  624. break;
  625. case AstNode_BadExpr: break;
  626. case AstNode_TagExpr:
  627. n->TagExpr.expr = clone_ast_node(a, n->TagExpr.expr);
  628. break;
  629. case AstNode_RunExpr:
  630. n->RunExpr.expr = clone_ast_node(a, n->RunExpr.expr);
  631. break;
  632. case AstNode_UnaryExpr:
  633. n->UnaryExpr.expr = clone_ast_node(a, n->UnaryExpr.expr);
  634. break;
  635. case AstNode_BinaryExpr:
  636. n->BinaryExpr.left = clone_ast_node(a, n->BinaryExpr.left);
  637. n->BinaryExpr.right = clone_ast_node(a, n->BinaryExpr.right);
  638. break;
  639. case AstNode_ParenExpr:
  640. n->ParenExpr.expr = clone_ast_node(a, n->ParenExpr.expr);
  641. break;
  642. case AstNode_SelectorExpr:
  643. n->SelectorExpr.expr = clone_ast_node(a, n->SelectorExpr.expr);
  644. n->SelectorExpr.selector = clone_ast_node(a, n->SelectorExpr.selector);
  645. break;
  646. case AstNode_IndexExpr:
  647. n->IndexExpr.expr = clone_ast_node(a, n->IndexExpr.expr);
  648. n->IndexExpr.index = clone_ast_node(a, n->IndexExpr.index);
  649. break;
  650. case AstNode_DerefExpr:
  651. n->DerefExpr.expr = clone_ast_node(a, n->DerefExpr.expr);
  652. break;
  653. case AstNode_SliceExpr:
  654. n->SliceExpr.expr = clone_ast_node(a, n->SliceExpr.expr);
  655. n->SliceExpr.low = clone_ast_node(a, n->SliceExpr.low);
  656. n->SliceExpr.high = clone_ast_node(a, n->SliceExpr.high);
  657. break;
  658. case AstNode_CallExpr:
  659. n->CallExpr.proc = clone_ast_node(a, n->CallExpr.proc);
  660. n->CallExpr.args = clone_ast_node_array(a, n->CallExpr.args);
  661. break;
  662. case AstNode_MacroCallExpr:
  663. n->MacroCallExpr.macro = clone_ast_node(a, n->MacroCallExpr.macro);
  664. n->MacroCallExpr.args = clone_ast_node_array(a, n->MacroCallExpr.args);
  665. break;
  666. case AstNode_FieldValue:
  667. n->FieldValue.field = clone_ast_node(a, n->FieldValue.field);
  668. n->FieldValue.value = clone_ast_node(a, n->FieldValue.value);
  669. break;
  670. case AstNode_TernaryExpr:
  671. n->TernaryExpr.cond = clone_ast_node(a, n->TernaryExpr.cond);
  672. n->TernaryExpr.x = clone_ast_node(a, n->TernaryExpr.x);
  673. n->TernaryExpr.y = clone_ast_node(a, n->TernaryExpr.y);
  674. break;
  675. case AstNode_TypeAssertion:
  676. n->TypeAssertion.expr = clone_ast_node(a, n->TypeAssertion.expr);
  677. n->TypeAssertion.type = clone_ast_node(a, n->TypeAssertion.type);
  678. break;
  679. case AstNode_TypeCast:
  680. n->TypeCast.type = clone_ast_node(a, n->TypeCast.type);
  681. n->TypeCast.expr = clone_ast_node(a, n->TypeCast.expr);
  682. break;
  683. case AstNode_BadStmt: break;
  684. case AstNode_EmptyStmt: break;
  685. case AstNode_ExprStmt:
  686. n->ExprStmt.expr = clone_ast_node(a, n->ExprStmt.expr);
  687. break;
  688. case AstNode_TagStmt:
  689. n->TagStmt.stmt = clone_ast_node(a, n->TagStmt.stmt);
  690. break;
  691. case AstNode_AssignStmt:
  692. n->AssignStmt.lhs = clone_ast_node_array(a, n->AssignStmt.lhs);
  693. n->AssignStmt.rhs = clone_ast_node_array(a, n->AssignStmt.rhs);
  694. break;
  695. case AstNode_IncDecStmt:
  696. n->IncDecStmt.expr = clone_ast_node(a, n->IncDecStmt.expr);
  697. break;
  698. case AstNode_BlockStmt:
  699. n->BlockStmt.stmts = clone_ast_node_array(a, n->BlockStmt.stmts);
  700. break;
  701. case AstNode_IfStmt:
  702. n->IfStmt.init = clone_ast_node(a, n->IfStmt.init);
  703. n->IfStmt.cond = clone_ast_node(a, n->IfStmt.cond);
  704. n->IfStmt.body = clone_ast_node(a, n->IfStmt.body);
  705. n->IfStmt.else_stmt = clone_ast_node(a, n->IfStmt.else_stmt);
  706. break;
  707. case AstNode_WhenStmt:
  708. n->WhenStmt.cond = clone_ast_node(a, n->WhenStmt.cond);
  709. n->WhenStmt.body = clone_ast_node(a, n->WhenStmt.body);
  710. n->WhenStmt.else_stmt = clone_ast_node(a, n->WhenStmt.else_stmt);
  711. break;
  712. case AstNode_ReturnStmt:
  713. n->ReturnStmt.results = clone_ast_node_array(a, n->ReturnStmt.results);
  714. break;
  715. case AstNode_ForStmt:
  716. n->ForStmt.label = clone_ast_node(a, n->ForStmt.label);
  717. n->ForStmt.init = clone_ast_node(a, n->ForStmt.init);
  718. n->ForStmt.cond = clone_ast_node(a, n->ForStmt.cond);
  719. n->ForStmt.post = clone_ast_node(a, n->ForStmt.post);
  720. n->ForStmt.body = clone_ast_node(a, n->ForStmt.body);
  721. break;
  722. case AstNode_RangeStmt:
  723. n->RangeStmt.label = clone_ast_node(a, n->RangeStmt.label);
  724. n->RangeStmt.value = clone_ast_node(a, n->RangeStmt.value);
  725. n->RangeStmt.index = clone_ast_node(a, n->RangeStmt.index);
  726. n->RangeStmt.expr = clone_ast_node(a, n->RangeStmt.expr);
  727. n->RangeStmt.body = clone_ast_node(a, n->RangeStmt.body);
  728. break;
  729. case AstNode_CaseClause:
  730. n->CaseClause.list = clone_ast_node_array(a, n->CaseClause.list);
  731. n->CaseClause.stmts = clone_ast_node_array(a, n->CaseClause.stmts);
  732. break;
  733. case AstNode_SwitchStmt:
  734. n->SwitchStmt.label = clone_ast_node(a, n->SwitchStmt.label);
  735. n->SwitchStmt.init = clone_ast_node(a, n->SwitchStmt.init);
  736. n->SwitchStmt.tag = clone_ast_node(a, n->SwitchStmt.tag);
  737. n->SwitchStmt.body = clone_ast_node(a, n->SwitchStmt.body);
  738. break;
  739. case AstNode_TypeSwitchStmt:
  740. n->TypeSwitchStmt.label = clone_ast_node(a, n->TypeSwitchStmt.label);
  741. n->TypeSwitchStmt.tag = clone_ast_node(a, n->TypeSwitchStmt.tag);
  742. n->TypeSwitchStmt.body = clone_ast_node(a, n->TypeSwitchStmt.body);
  743. break;
  744. case AstNode_DeferStmt:
  745. n->DeferStmt.stmt = clone_ast_node(a, n->DeferStmt.stmt);
  746. break;
  747. case AstNode_BranchStmt:
  748. n->BranchStmt.label = clone_ast_node(a, n->BranchStmt.label);
  749. break;
  750. case AstNode_UsingStmt:
  751. n->UsingStmt.list = clone_ast_node_array(a, n->UsingStmt.list);
  752. break;
  753. case AstNode_AsmOperand:
  754. n->AsmOperand.operand = clone_ast_node(a, n->AsmOperand.operand);
  755. break;
  756. case AstNode_AsmStmt:
  757. n->AsmStmt.output_list = clone_ast_node(a, n->AsmStmt.output_list);
  758. n->AsmStmt.input_list = clone_ast_node(a, n->AsmStmt.input_list);
  759. n->AsmStmt.clobber_list = clone_ast_node(a, n->AsmStmt.clobber_list);
  760. break;
  761. case AstNode_PushContext:
  762. n->PushContext.expr = clone_ast_node(a, n->PushContext.expr);
  763. n->PushContext.body = clone_ast_node(a, n->PushContext.body);
  764. break;
  765. case AstNode_BadDecl: break;
  766. case AstNode_ForeignBlockDecl:
  767. n->ForeignBlockDecl.foreign_library = clone_ast_node(a, n->ForeignBlockDecl.foreign_library);
  768. n->ForeignBlockDecl.decls = clone_ast_node_array(a, n->ForeignBlockDecl.decls);
  769. n->ForeignBlockDecl.attributes = clone_ast_node_array(a, n->ForeignBlockDecl.attributes);
  770. break;
  771. case AstNode_Label:
  772. n->Label.name = clone_ast_node(a, n->Label.name);
  773. break;
  774. case AstNode_ValueDecl:
  775. n->ValueDecl.names = clone_ast_node_array(a, n->ValueDecl.names);
  776. n->ValueDecl.type = clone_ast_node(a, n->ValueDecl.type);
  777. n->ValueDecl.values = clone_ast_node_array(a, n->ValueDecl.values);
  778. n->ValueDecl.attributes = clone_ast_node_array(a, n->ValueDecl.attributes);
  779. break;
  780. case AstNode_Attribute:
  781. n->Attribute.elems = clone_ast_node_array(a, n->Attribute.elems);
  782. break;
  783. case AstNode_Field:
  784. n->Field.names = clone_ast_node_array(a, n->Field.names);
  785. n->Field.type = clone_ast_node(a, n->Field.type);
  786. break;
  787. case AstNode_FieldList:
  788. n->FieldList.list = clone_ast_node_array(a, n->FieldList.list);
  789. break;
  790. case AstNode_UnionField:
  791. n->UnionField.name = clone_ast_node(a, n->UnionField.name);
  792. n->UnionField.list = clone_ast_node(a, n->UnionField.list);
  793. break;
  794. case AstNode_TypeType:
  795. n->TypeType.specialization = clone_ast_node(a, n->TypeType.specialization);
  796. break;
  797. case AstNode_HelperType:
  798. n->HelperType.type = clone_ast_node(a, n->HelperType.type);
  799. break;
  800. case AstNode_AliasType:
  801. n->AliasType.type = clone_ast_node(a, n->AliasType.type);
  802. break;
  803. case AstNode_ProcType:
  804. n->ProcType.params = clone_ast_node(a, n->ProcType.params);
  805. n->ProcType.results = clone_ast_node(a, n->ProcType.results);
  806. break;
  807. case AstNode_PointerType:
  808. n->PointerType.type = clone_ast_node(a, n->PointerType.type);
  809. break;
  810. case AstNode_ArrayType:
  811. n->ArrayType.count = clone_ast_node(a, n->ArrayType.count);
  812. n->ArrayType.elem = clone_ast_node(a, n->ArrayType.elem);
  813. break;
  814. case AstNode_DynamicArrayType:
  815. n->DynamicArrayType.elem = clone_ast_node(a, n->DynamicArrayType.elem);
  816. break;
  817. case AstNode_VectorType:
  818. n->VectorType.count = clone_ast_node(a, n->VectorType.count);
  819. n->VectorType.elem = clone_ast_node(a, n->VectorType.elem);
  820. break;
  821. case AstNode_StructType:
  822. n->StructType.fields = clone_ast_node_array(a, n->StructType.fields);
  823. n->StructType.polymorphic_params = clone_ast_node(a, n->StructType.polymorphic_params);
  824. n->StructType.align = clone_ast_node(a, n->StructType.align);
  825. break;
  826. case AstNode_UnionType:
  827. n->UnionType.variants = clone_ast_node_array(a, n->UnionType.variants);
  828. break;
  829. case AstNode_EnumType:
  830. n->EnumType.base_type = clone_ast_node(a, n->EnumType.base_type);
  831. n->EnumType.fields = clone_ast_node_array(a, n->EnumType.fields);
  832. break;
  833. case AstNode_BitFieldType:
  834. n->BitFieldType.fields = clone_ast_node_array(a, n->BitFieldType.fields);
  835. n->BitFieldType.align = clone_ast_node(a, n->BitFieldType.align);
  836. case AstNode_MapType:
  837. n->MapType.count = clone_ast_node(a, n->MapType.count);
  838. n->MapType.key = clone_ast_node(a, n->MapType.key);
  839. n->MapType.value = clone_ast_node(a, n->MapType.value);
  840. break;
  841. }
  842. return n;
  843. }
  844. void error(AstNode *node, char *fmt, ...) {
  845. Token token = {};
  846. if (node != nullptr) {
  847. token = ast_node_token(node);
  848. }
  849. va_list va;
  850. va_start(va, fmt);
  851. error_va(token, fmt, va);
  852. va_end(va);
  853. }
  854. void warning(AstNode *node, char *fmt, ...) {
  855. va_list va;
  856. va_start(va, fmt);
  857. warning_va(ast_node_token(node), fmt, va);
  858. va_end(va);
  859. }
  860. void syntax_error(AstNode *node, char *fmt, ...) {
  861. va_list va;
  862. va_start(va, fmt);
  863. syntax_error_va(ast_node_token(node), fmt, va);
  864. va_end(va);
  865. }
  866. bool ast_node_expect(AstNode *node, AstNodeKind kind) {
  867. if (node->kind != kind) {
  868. syntax_error(node, "Expected %.*s, got %.*s", LIT(ast_node_strings[node->kind]));
  869. return false;
  870. }
  871. return true;
  872. }
  873. // NOTE(bill): And this below is why is I/we need a new language! Discriminated unions are a pain in C/C++
  874. AstNode *make_ast_node(AstFile *f, AstNodeKind kind) {
  875. gbArena *arena = &f->arena;
  876. if (gb_arena_size_remaining(arena, GB_DEFAULT_MEMORY_ALIGNMENT) <= gb_size_of(AstNode)) {
  877. // NOTE(bill): If a syntax error is so bad, just quit!
  878. gb_exit(1);
  879. }
  880. AstNode *node = gb_alloc_item(gb_arena_allocator(arena), AstNode);
  881. node->kind = kind;
  882. node->file = f;
  883. return node;
  884. }
  885. AstNode *ast_bad_expr(AstFile *f, Token begin, Token end) {
  886. AstNode *result = make_ast_node(f, AstNode_BadExpr);
  887. result->BadExpr.begin = begin;
  888. result->BadExpr.end = end;
  889. return result;
  890. }
  891. AstNode *ast_tag_expr(AstFile *f, Token token, Token name, AstNode *expr) {
  892. AstNode *result = make_ast_node(f, AstNode_TagExpr);
  893. result->TagExpr.token = token;
  894. result->TagExpr.name = name;
  895. result->TagExpr.expr = expr;
  896. return result;
  897. }
  898. AstNode *ast_run_expr(AstFile *f, Token token, Token name, AstNode *expr) {
  899. AstNode *result = make_ast_node(f, AstNode_RunExpr);
  900. result->RunExpr.token = token;
  901. result->RunExpr.name = name;
  902. result->RunExpr.expr = expr;
  903. return result;
  904. }
  905. AstNode *ast_tag_stmt(AstFile *f, Token token, Token name, AstNode *stmt) {
  906. AstNode *result = make_ast_node(f, AstNode_TagStmt);
  907. result->TagStmt.token = token;
  908. result->TagStmt.name = name;
  909. result->TagStmt.stmt = stmt;
  910. return result;
  911. }
  912. AstNode *ast_unary_expr(AstFile *f, Token op, AstNode *expr) {
  913. AstNode *result = make_ast_node(f, AstNode_UnaryExpr);
  914. result->UnaryExpr.op = op;
  915. result->UnaryExpr.expr = expr;
  916. return result;
  917. }
  918. AstNode *ast_binary_expr(AstFile *f, Token op, AstNode *left, AstNode *right) {
  919. AstNode *result = make_ast_node(f, AstNode_BinaryExpr);
  920. if (left == nullptr) {
  921. syntax_error(op, "No lhs expression for binary expression '%.*s'", LIT(op.string));
  922. left = ast_bad_expr(f, op, op);
  923. }
  924. if (right == nullptr) {
  925. syntax_error(op, "No rhs expression for binary expression '%.*s'", LIT(op.string));
  926. right = ast_bad_expr(f, op, op);
  927. }
  928. result->BinaryExpr.op = op;
  929. result->BinaryExpr.left = left;
  930. result->BinaryExpr.right = right;
  931. return result;
  932. }
  933. AstNode *ast_paren_expr(AstFile *f, AstNode *expr, Token open, Token close) {
  934. AstNode *result = make_ast_node(f, AstNode_ParenExpr);
  935. result->ParenExpr.expr = expr;
  936. result->ParenExpr.open = open;
  937. result->ParenExpr.close = close;
  938. return result;
  939. }
  940. AstNode *ast_call_expr(AstFile *f, AstNode *proc, Array<AstNode *> args, Token open, Token close, Token ellipsis) {
  941. AstNode *result = make_ast_node(f, AstNode_CallExpr);
  942. result->CallExpr.proc = proc;
  943. result->CallExpr.args = args;
  944. result->CallExpr.open = open;
  945. result->CallExpr.close = close;
  946. result->CallExpr.ellipsis = ellipsis;
  947. return result;
  948. }
  949. AstNode *ast_macro_call_expr(AstFile *f, AstNode *macro, Token bang, Array<AstNode *> args, Token open, Token close) {
  950. AstNode *result = make_ast_node(f, AstNode_MacroCallExpr);
  951. result->MacroCallExpr.macro = macro;
  952. result->MacroCallExpr.bang = bang;
  953. result->MacroCallExpr.args = args;
  954. result->MacroCallExpr.open = open;
  955. result->MacroCallExpr.close = close;
  956. return result;
  957. }
  958. AstNode *ast_selector_expr(AstFile *f, Token token, AstNode *expr, AstNode *selector) {
  959. AstNode *result = make_ast_node(f, AstNode_SelectorExpr);
  960. result->SelectorExpr.expr = expr;
  961. result->SelectorExpr.selector = selector;
  962. return result;
  963. }
  964. AstNode *ast_index_expr(AstFile *f, AstNode *expr, AstNode *index, Token open, Token close) {
  965. AstNode *result = make_ast_node(f, AstNode_IndexExpr);
  966. result->IndexExpr.expr = expr;
  967. result->IndexExpr.index = index;
  968. result->IndexExpr.open = open;
  969. result->IndexExpr.close = close;
  970. return result;
  971. }
  972. AstNode *ast_slice_expr(AstFile *f, AstNode *expr, Token open, Token close, Token interval, AstNode *low, AstNode *high) {
  973. AstNode *result = make_ast_node(f, AstNode_SliceExpr);
  974. result->SliceExpr.expr = expr;
  975. result->SliceExpr.open = open;
  976. result->SliceExpr.close = close;
  977. result->SliceExpr.interval = interval;
  978. result->SliceExpr.low = low;
  979. result->SliceExpr.high = high;
  980. return result;
  981. }
  982. AstNode *ast_deref_expr(AstFile *f, AstNode *expr, Token op) {
  983. AstNode *result = make_ast_node(f, AstNode_DerefExpr);
  984. result->DerefExpr.expr = expr;
  985. result->DerefExpr.op = op;
  986. return result;
  987. }
  988. AstNode *ast_ident(AstFile *f, Token token) {
  989. AstNode *result = make_ast_node(f, AstNode_Ident);
  990. result->Ident.token = token;
  991. return result;
  992. }
  993. AstNode *ast_implicit(AstFile *f, Token token) {
  994. AstNode *result = make_ast_node(f, AstNode_Implicit);
  995. result->Implicit = token;
  996. return result;
  997. }
  998. AstNode *ast_undef(AstFile *f, Token token) {
  999. AstNode *result = make_ast_node(f, AstNode_Undef);
  1000. result->Undef = token;
  1001. return result;
  1002. }
  1003. AstNode *ast_basic_lit(AstFile *f, Token basic_lit) {
  1004. AstNode *result = make_ast_node(f, AstNode_BasicLit);
  1005. result->BasicLit = basic_lit;
  1006. return result;
  1007. }
  1008. AstNode *ast_basic_directive(AstFile *f, Token token, String name) {
  1009. AstNode *result = make_ast_node(f, AstNode_BasicDirective);
  1010. result->BasicDirective.token = token;
  1011. result->BasicDirective.name = name;
  1012. return result;
  1013. }
  1014. AstNode *ast_ellipsis(AstFile *f, Token token, AstNode *expr) {
  1015. AstNode *result = make_ast_node(f, AstNode_Ellipsis);
  1016. result->Ellipsis.token = token;
  1017. result->Ellipsis.expr = expr;
  1018. return result;
  1019. }
  1020. AstNode *ast_proc_lit(AstFile *f, AstNode *type, AstNode *body, u64 tags) {
  1021. AstNode *result = make_ast_node(f, AstNode_ProcLit);
  1022. result->ProcLit.type = type;
  1023. result->ProcLit.body = body;
  1024. result->ProcLit.tags = tags;
  1025. return result;
  1026. }
  1027. AstNode *ast_field_value(AstFile *f, AstNode *field, AstNode *value, Token eq) {
  1028. AstNode *result = make_ast_node(f, AstNode_FieldValue);
  1029. result->FieldValue.field = field;
  1030. result->FieldValue.value = value;
  1031. result->FieldValue.eq = eq;
  1032. return result;
  1033. }
  1034. AstNode *ast_compound_lit(AstFile *f, AstNode *type, Array<AstNode *> elems, Token open, Token close) {
  1035. AstNode *result = make_ast_node(f, AstNode_CompoundLit);
  1036. result->CompoundLit.type = type;
  1037. result->CompoundLit.elems = elems;
  1038. result->CompoundLit.open = open;
  1039. result->CompoundLit.close = close;
  1040. return result;
  1041. }
  1042. AstNode *ast_alias(AstFile *f, Token token, AstNode *expr) {
  1043. AstNode *result = make_ast_node(f, AstNode_Alias);
  1044. result->Alias.token = token;
  1045. result->Alias.expr = expr;
  1046. return result;
  1047. }
  1048. AstNode *ast_ternary_expr(AstFile *f, AstNode *cond, AstNode *x, AstNode *y) {
  1049. AstNode *result = make_ast_node(f, AstNode_TernaryExpr);
  1050. result->TernaryExpr.cond = cond;
  1051. result->TernaryExpr.x = x;
  1052. result->TernaryExpr.y = y;
  1053. return result;
  1054. }
  1055. AstNode *ast_type_assertion(AstFile *f, AstNode *expr, Token dot, AstNode *type) {
  1056. AstNode *result = make_ast_node(f, AstNode_TypeAssertion);
  1057. result->TypeAssertion.expr = expr;
  1058. result->TypeAssertion.dot = dot;
  1059. result->TypeAssertion.type = type;
  1060. return result;
  1061. }
  1062. AstNode *ast_type_cast(AstFile *f, Token token, AstNode *type, AstNode *expr) {
  1063. AstNode *result = make_ast_node(f, AstNode_TypeCast);
  1064. result->TypeCast.token = token;
  1065. result->TypeCast.type = type;
  1066. result->TypeCast.expr = expr;
  1067. return result;
  1068. }
  1069. AstNode *ast_bad_stmt(AstFile *f, Token begin, Token end) {
  1070. AstNode *result = make_ast_node(f, AstNode_BadStmt);
  1071. result->BadStmt.begin = begin;
  1072. result->BadStmt.end = end;
  1073. return result;
  1074. }
  1075. AstNode *ast_empty_stmt(AstFile *f, Token token) {
  1076. AstNode *result = make_ast_node(f, AstNode_EmptyStmt);
  1077. result->EmptyStmt.token = token;
  1078. return result;
  1079. }
  1080. AstNode *ast_expr_stmt(AstFile *f, AstNode *expr) {
  1081. AstNode *result = make_ast_node(f, AstNode_ExprStmt);
  1082. result->ExprStmt.expr = expr;
  1083. return result;
  1084. }
  1085. AstNode *ast_assign_stmt(AstFile *f, Token op, Array<AstNode *> lhs, Array<AstNode *> rhs) {
  1086. AstNode *result = make_ast_node(f, AstNode_AssignStmt);
  1087. result->AssignStmt.op = op;
  1088. result->AssignStmt.lhs = lhs;
  1089. result->AssignStmt.rhs = rhs;
  1090. return result;
  1091. }
  1092. AstNode *ast_inc_dec_stmt(AstFile *f, Token op, AstNode *expr) {
  1093. AstNode *result = make_ast_node(f, AstNode_IncDecStmt);
  1094. result->IncDecStmt.op = op;
  1095. result->IncDecStmt.expr = expr;
  1096. return result;
  1097. }
  1098. AstNode *ast_block_stmt(AstFile *f, Array<AstNode *> stmts, Token open, Token close) {
  1099. AstNode *result = make_ast_node(f, AstNode_BlockStmt);
  1100. result->BlockStmt.stmts = stmts;
  1101. result->BlockStmt.open = open;
  1102. result->BlockStmt.close = close;
  1103. return result;
  1104. }
  1105. AstNode *ast_if_stmt(AstFile *f, Token token, AstNode *init, AstNode *cond, AstNode *body, AstNode *else_stmt) {
  1106. AstNode *result = make_ast_node(f, AstNode_IfStmt);
  1107. result->IfStmt.token = token;
  1108. result->IfStmt.init = init;
  1109. result->IfStmt.cond = cond;
  1110. result->IfStmt.body = body;
  1111. result->IfStmt.else_stmt = else_stmt;
  1112. return result;
  1113. }
  1114. AstNode *ast_when_stmt(AstFile *f, Token token, AstNode *cond, AstNode *body, AstNode *else_stmt) {
  1115. AstNode *result = make_ast_node(f, AstNode_WhenStmt);
  1116. result->WhenStmt.token = token;
  1117. result->WhenStmt.cond = cond;
  1118. result->WhenStmt.body = body;
  1119. result->WhenStmt.else_stmt = else_stmt;
  1120. return result;
  1121. }
  1122. AstNode *ast_return_stmt(AstFile *f, Token token, Array<AstNode *> results) {
  1123. AstNode *result = make_ast_node(f, AstNode_ReturnStmt);
  1124. result->ReturnStmt.token = token;
  1125. result->ReturnStmt.results = results;
  1126. return result;
  1127. }
  1128. AstNode *ast_for_stmt(AstFile *f, Token token, AstNode *init, AstNode *cond, AstNode *post, AstNode *body) {
  1129. AstNode *result = make_ast_node(f, AstNode_ForStmt);
  1130. result->ForStmt.token = token;
  1131. result->ForStmt.init = init;
  1132. result->ForStmt.cond = cond;
  1133. result->ForStmt.post = post;
  1134. result->ForStmt.body = body;
  1135. return result;
  1136. }
  1137. AstNode *ast_range_stmt(AstFile *f, Token token, AstNode *value, AstNode *index, Token in_token, AstNode *expr, AstNode *body) {
  1138. AstNode *result = make_ast_node(f, AstNode_RangeStmt);
  1139. result->RangeStmt.token = token;
  1140. result->RangeStmt.value = value;
  1141. result->RangeStmt.index = index;
  1142. result->RangeStmt.in_token = in_token;
  1143. result->RangeStmt.expr = expr;
  1144. result->RangeStmt.body = body;
  1145. return result;
  1146. }
  1147. AstNode *ast_switch_stmt(AstFile *f, Token token, AstNode *init, AstNode *tag, AstNode *body) {
  1148. AstNode *result = make_ast_node(f, AstNode_SwitchStmt);
  1149. result->SwitchStmt.token = token;
  1150. result->SwitchStmt.init = init;
  1151. result->SwitchStmt.tag = tag;
  1152. result->SwitchStmt.body = body;
  1153. return result;
  1154. }
  1155. AstNode *ast_type_switch_stmt(AstFile *f, Token token, AstNode *tag, AstNode *body) {
  1156. AstNode *result = make_ast_node(f, AstNode_TypeSwitchStmt);
  1157. result->TypeSwitchStmt.token = token;
  1158. result->TypeSwitchStmt.tag = tag;
  1159. result->TypeSwitchStmt.body = body;
  1160. return result;
  1161. }
  1162. AstNode *ast_case_clause(AstFile *f, Token token, Array<AstNode *> list, Array<AstNode *> stmts) {
  1163. AstNode *result = make_ast_node(f, AstNode_CaseClause);
  1164. result->CaseClause.token = token;
  1165. result->CaseClause.list = list;
  1166. result->CaseClause.stmts = stmts;
  1167. return result;
  1168. }
  1169. AstNode *ast_defer_stmt(AstFile *f, Token token, AstNode *stmt) {
  1170. AstNode *result = make_ast_node(f, AstNode_DeferStmt);
  1171. result->DeferStmt.token = token;
  1172. result->DeferStmt.stmt = stmt;
  1173. return result;
  1174. }
  1175. AstNode *ast_branch_stmt(AstFile *f, Token token, AstNode *label) {
  1176. AstNode *result = make_ast_node(f, AstNode_BranchStmt);
  1177. result->BranchStmt.token = token;
  1178. result->BranchStmt.label = label;
  1179. return result;
  1180. }
  1181. AstNode *ast_using_stmt(AstFile *f, Token token, Array<AstNode *> list) {
  1182. AstNode *result = make_ast_node(f, AstNode_UsingStmt);
  1183. result->UsingStmt.token = token;
  1184. result->UsingStmt.list = list;
  1185. return result;
  1186. }
  1187. AstNode *ast_asm_operand(AstFile *f, Token string, AstNode *operand) {
  1188. AstNode *result = make_ast_node(f, AstNode_AsmOperand);
  1189. result->AsmOperand.string = string;
  1190. result->AsmOperand.operand = operand;
  1191. return result;
  1192. }
  1193. AstNode *ast_asm_stmt(AstFile *f, Token token, bool is_volatile, Token open, Token close, Token code_string,
  1194. AstNode *output_list, AstNode *input_list, AstNode *clobber_list,
  1195. isize output_count, isize input_count, isize clobber_count) {
  1196. AstNode *result = make_ast_node(f, AstNode_AsmStmt);
  1197. result->AsmStmt.token = token;
  1198. result->AsmStmt.is_volatile = is_volatile;
  1199. result->AsmStmt.open = open;
  1200. result->AsmStmt.close = close;
  1201. result->AsmStmt.code_string = code_string;
  1202. result->AsmStmt.output_list = output_list;
  1203. result->AsmStmt.input_list = input_list;
  1204. result->AsmStmt.clobber_list = clobber_list;
  1205. result->AsmStmt.output_count = output_count;
  1206. result->AsmStmt.input_count = input_count;
  1207. result->AsmStmt.clobber_count = clobber_count;
  1208. return result;
  1209. }
  1210. AstNode *ast_push_context(AstFile *f, Token token, AstNode *expr, AstNode *body) {
  1211. AstNode *result = make_ast_node(f, AstNode_PushContext);
  1212. result->PushContext.token = token;
  1213. result->PushContext.expr = expr;
  1214. result->PushContext.body = body;
  1215. return result;
  1216. }
  1217. AstNode *ast_bad_decl(AstFile *f, Token begin, Token end) {
  1218. AstNode *result = make_ast_node(f, AstNode_BadDecl);
  1219. result->BadDecl.begin = begin;
  1220. result->BadDecl.end = end;
  1221. return result;
  1222. }
  1223. AstNode *ast_field(AstFile *f, Array<AstNode *> names, AstNode *type, AstNode *default_value, u32 flags,
  1224. CommentGroup docs, CommentGroup comment) {
  1225. AstNode *result = make_ast_node(f, AstNode_Field);
  1226. result->Field.names = names;
  1227. result->Field.type = type;
  1228. result->Field.default_value = default_value;
  1229. result->Field.flags = flags;
  1230. result->Field.docs = docs;
  1231. result->Field.comment = comment;
  1232. return result;
  1233. }
  1234. AstNode *ast_field_list(AstFile *f, Token token, Array<AstNode *> list) {
  1235. AstNode *result = make_ast_node(f, AstNode_FieldList);
  1236. result->FieldList.token = token;
  1237. result->FieldList.list = list;
  1238. return result;
  1239. }
  1240. AstNode *ast_union_field(AstFile *f, AstNode *name, AstNode *list) {
  1241. AstNode *result = make_ast_node(f, AstNode_UnionField);
  1242. result->UnionField.name = name;
  1243. result->UnionField.list = list;
  1244. return result;
  1245. }
  1246. AstNode *ast_type_type(AstFile *f, Token token, AstNode *specialization) {
  1247. AstNode *result = make_ast_node(f, AstNode_TypeType);
  1248. result->TypeType.token = token;
  1249. result->TypeType.specialization = specialization;
  1250. return result;
  1251. }
  1252. AstNode *ast_helper_type(AstFile *f, Token token, AstNode *type) {
  1253. AstNode *result = make_ast_node(f, AstNode_HelperType);
  1254. result->HelperType.token = token;
  1255. result->HelperType.type = type;
  1256. return result;
  1257. }
  1258. AstNode *ast_alias_type(AstFile *f, Token token, AstNode *type) {
  1259. AstNode *result = make_ast_node(f, AstNode_AliasType);
  1260. result->AliasType.token = token;
  1261. result->AliasType.type = type;
  1262. return result;
  1263. }
  1264. AstNode *ast_poly_type(AstFile *f, Token token, AstNode *type, AstNode *specialization) {
  1265. AstNode *result = make_ast_node(f, AstNode_PolyType);
  1266. result->PolyType.token = token;
  1267. result->PolyType.type = type;
  1268. result->PolyType.specialization = specialization;
  1269. return result;
  1270. }
  1271. AstNode *ast_proc_type(AstFile *f, Token token, AstNode *params, AstNode *results, u64 tags, ProcCallingConvention calling_convention, bool generic) {
  1272. AstNode *result = make_ast_node(f, AstNode_ProcType);
  1273. result->ProcType.token = token;
  1274. result->ProcType.params = params;
  1275. result->ProcType.results = results;
  1276. result->ProcType.tags = tags;
  1277. result->ProcType.calling_convention = calling_convention;
  1278. result->ProcType.generic = generic;
  1279. return result;
  1280. }
  1281. AstNode *ast_pointer_type(AstFile *f, Token token, AstNode *type) {
  1282. AstNode *result = make_ast_node(f, AstNode_PointerType);
  1283. result->PointerType.token = token;
  1284. result->PointerType.type = type;
  1285. return result;
  1286. }
  1287. AstNode *ast_array_type(AstFile *f, Token token, AstNode *count, AstNode *elem) {
  1288. AstNode *result = make_ast_node(f, AstNode_ArrayType);
  1289. result->ArrayType.token = token;
  1290. result->ArrayType.count = count;
  1291. result->ArrayType.elem = elem;
  1292. return result;
  1293. }
  1294. AstNode *ast_dynamic_array_type(AstFile *f, Token token, AstNode *elem) {
  1295. AstNode *result = make_ast_node(f, AstNode_DynamicArrayType);
  1296. result->DynamicArrayType.token = token;
  1297. result->DynamicArrayType.elem = elem;
  1298. return result;
  1299. }
  1300. AstNode *ast_vector_type(AstFile *f, Token token, AstNode *count, AstNode *elem) {
  1301. AstNode *result = make_ast_node(f, AstNode_VectorType);
  1302. result->VectorType.token = token;
  1303. result->VectorType.count = count;
  1304. result->VectorType.elem = elem;
  1305. return result;
  1306. }
  1307. AstNode *ast_struct_type(AstFile *f, Token token, Array<AstNode *> fields, isize field_count,
  1308. AstNode *polymorphic_params, bool is_packed, bool is_ordered, bool is_raw_union,
  1309. AstNode *align) {
  1310. AstNode *result = make_ast_node(f, AstNode_StructType);
  1311. result->StructType.token = token;
  1312. result->StructType.fields = fields;
  1313. result->StructType.field_count = field_count;
  1314. result->StructType.polymorphic_params = polymorphic_params;
  1315. result->StructType.is_packed = is_packed;
  1316. result->StructType.is_ordered = is_ordered;
  1317. result->StructType.is_raw_union = is_raw_union;
  1318. result->StructType.align = align;
  1319. return result;
  1320. }
  1321. AstNode *ast_union_type(AstFile *f, Token token, Array<AstNode *> variants, AstNode *align) {
  1322. AstNode *result = make_ast_node(f, AstNode_UnionType);
  1323. result->UnionType.token = token;
  1324. result->UnionType.variants = variants;
  1325. result->UnionType.align = align;
  1326. return result;
  1327. }
  1328. AstNode *ast_enum_type(AstFile *f, Token token, AstNode *base_type, Array<AstNode *> fields) {
  1329. AstNode *result = make_ast_node(f, AstNode_EnumType);
  1330. result->EnumType.token = token;
  1331. result->EnumType.base_type = base_type;
  1332. result->EnumType.fields = fields;
  1333. return result;
  1334. }
  1335. AstNode *ast_bit_field_type(AstFile *f, Token token, Array<AstNode *> fields, AstNode *align) {
  1336. AstNode *result = make_ast_node(f, AstNode_BitFieldType);
  1337. result->BitFieldType.token = token;
  1338. result->BitFieldType.fields = fields;
  1339. result->BitFieldType.align = align;
  1340. return result;
  1341. }
  1342. AstNode *ast_map_type(AstFile *f, Token token, AstNode *key, AstNode *value) {
  1343. AstNode *result = make_ast_node(f, AstNode_MapType);
  1344. result->MapType.token = token;
  1345. result->MapType.key = key;
  1346. result->MapType.value = value;
  1347. return result;
  1348. }
  1349. AstNode *ast_foreign_block_decl(AstFile *f, Token token, AstNode *foreign_library, Token open, Token close, Array<AstNode *> decls,
  1350. CommentGroup docs) {
  1351. AstNode *result = make_ast_node(f, AstNode_ForeignBlockDecl);
  1352. result->ForeignBlockDecl.token = token;
  1353. result->ForeignBlockDecl.foreign_library = foreign_library;
  1354. result->ForeignBlockDecl.open = open;
  1355. result->ForeignBlockDecl.close = close;
  1356. result->ForeignBlockDecl.decls = decls;
  1357. result->ForeignBlockDecl.docs = docs;
  1358. result->ForeignBlockDecl.attributes.allocator = heap_allocator();
  1359. return result;
  1360. }
  1361. AstNode *ast_label_decl(AstFile *f, Token token, AstNode *name) {
  1362. AstNode *result = make_ast_node(f, AstNode_Label);
  1363. result->Label.token = token;
  1364. result->Label.name = name;
  1365. return result;
  1366. }
  1367. AstNode *ast_value_decl(AstFile *f, Array<AstNode *> names, AstNode *type, Array<AstNode *> values, bool is_mutable,
  1368. CommentGroup docs, CommentGroup comment) {
  1369. AstNode *result = make_ast_node(f, AstNode_ValueDecl);
  1370. result->ValueDecl.names = names;
  1371. result->ValueDecl.type = type;
  1372. result->ValueDecl.values = values;
  1373. result->ValueDecl.is_mutable = is_mutable;
  1374. result->ValueDecl.docs = docs;
  1375. result->ValueDecl.comment = comment;
  1376. result->ValueDecl.attributes.allocator = heap_allocator();
  1377. return result;
  1378. }
  1379. AstNode *ast_import_decl(AstFile *f, Token token, bool is_using, Token relpath, Token import_name,
  1380. CommentGroup docs, CommentGroup comment) {
  1381. AstNode *result = make_ast_node(f, AstNode_ImportDecl);
  1382. result->ImportDecl.token = token;
  1383. result->ImportDecl.is_using = is_using;
  1384. result->ImportDecl.relpath = relpath;
  1385. result->ImportDecl.import_name = import_name;
  1386. result->ImportDecl.docs = docs;
  1387. result->ImportDecl.comment = comment;
  1388. return result;
  1389. }
  1390. AstNode *ast_export_decl(AstFile *f, Token token, Token relpath,
  1391. CommentGroup docs, CommentGroup comment) {
  1392. AstNode *result = make_ast_node(f, AstNode_ExportDecl);
  1393. result->ExportDecl.token = token;
  1394. result->ExportDecl.relpath = relpath;
  1395. result->ExportDecl.docs = docs;
  1396. result->ExportDecl.comment = comment;
  1397. return result;
  1398. }
  1399. AstNode *ast_foreign_import_decl(AstFile *f, Token token, Token filepath, Token library_name,
  1400. CommentGroup docs, CommentGroup comment) {
  1401. AstNode *result = make_ast_node(f, AstNode_ForeignImportDecl);
  1402. result->ForeignImportDecl.token = token;
  1403. result->ForeignImportDecl.filepath = filepath;
  1404. result->ForeignImportDecl.library_name = library_name;
  1405. result->ForeignImportDecl.docs = docs;
  1406. result->ForeignImportDecl.comment = comment;
  1407. return result;
  1408. }
  1409. AstNode *ast_attribute(AstFile *f, Token token, Token open, Token close, Array<AstNode *> elems) {
  1410. AstNode *result = make_ast_node(f, AstNode_Attribute);
  1411. result->Attribute.token = token;
  1412. result->Attribute.open = open;
  1413. result->Attribute.elems = elems;
  1414. result->Attribute.close = close;
  1415. return result;
  1416. }
  1417. bool next_token0(AstFile *f) {
  1418. // Token prev = f->curr_token;
  1419. if (f->curr_token_index+1 < f->tokens.count) {
  1420. f->curr_token = f->tokens[++f->curr_token_index];
  1421. return true;
  1422. }
  1423. syntax_error(f->curr_token, "Token is EOF");
  1424. return false;
  1425. }
  1426. Token consume_comment(AstFile *f, isize *end_line_) {
  1427. Token tok = f->curr_token;
  1428. GB_ASSERT(tok.kind == Token_Comment);
  1429. isize end_line = tok.pos.line;
  1430. if (tok.string[1] == '*') {
  1431. for (isize i = 0; i < tok.string.len; i++) {
  1432. if (tok.string[i] == '\n') {
  1433. end_line++;
  1434. }
  1435. }
  1436. }
  1437. if (end_line_) *end_line_ = end_line;
  1438. next_token0(f);
  1439. return tok;
  1440. }
  1441. CommentGroup consume_comment_group(AstFile *f, isize n, isize *end_line_) {
  1442. Array<Token> list = {};
  1443. isize end_line = f->curr_token.pos.line;
  1444. if (f->curr_token.kind == Token_Comment) {
  1445. array_init(&list, heap_allocator());
  1446. while (f->curr_token.kind == Token_Comment &&
  1447. f->curr_token.pos.line <= end_line+n) {
  1448. array_add(&list, consume_comment(f, &end_line));
  1449. }
  1450. }
  1451. if (end_line_) *end_line_ = end_line;
  1452. CommentGroup comments = {};
  1453. comments.list = list;
  1454. array_add(&f->comments, comments);
  1455. return comments;
  1456. }
  1457. void comsume_comment_groups(AstFile *f, Token prev) {
  1458. if (f->curr_token.kind != Token_Comment) return;
  1459. CommentGroup comment = {};
  1460. isize end_line = 0;
  1461. if (f->curr_token.pos.line == prev.pos.line) {
  1462. comment = consume_comment_group(f, 0, &end_line);
  1463. if (f->curr_token.pos.line != end_line) {
  1464. f->line_comment = comment;
  1465. }
  1466. }
  1467. end_line = -1;
  1468. while (f->curr_token.kind == Token_Comment) {
  1469. comment = consume_comment_group(f, 1, &end_line);
  1470. }
  1471. if (end_line+1 == f->curr_token.pos.line) {
  1472. f->lead_comment = comment;
  1473. }
  1474. GB_ASSERT(f->curr_token.kind != Token_Comment);
  1475. }
  1476. Token advance_token(AstFile *f) {
  1477. gb_zero_item(&f->lead_comment);
  1478. gb_zero_item(&f->line_comment);
  1479. Token prev = f->prev_token = f->curr_token;
  1480. bool ok = next_token0(f);
  1481. if (ok) comsume_comment_groups(f, prev);
  1482. return prev;
  1483. }
  1484. TokenKind look_ahead_token_kind(AstFile *f, isize amount) {
  1485. GB_ASSERT(amount > 0);
  1486. TokenKind kind = Token_Invalid;
  1487. isize index = f->curr_token_index;
  1488. while (amount > 0) {
  1489. index++;
  1490. kind = f->tokens[index].kind;
  1491. if (kind != Token_Comment) {
  1492. amount--;
  1493. }
  1494. }
  1495. return kind;
  1496. }
  1497. Token expect_token(AstFile *f, TokenKind kind) {
  1498. Token prev = f->curr_token;
  1499. if (prev.kind != kind) {
  1500. String c = token_strings[kind];
  1501. String p = token_strings[prev.kind];
  1502. syntax_error(f->curr_token, "Expected '%.*s', got '%.*s'", LIT(c), LIT(p));
  1503. if (prev.kind == Token_EOF) {
  1504. gb_exit(1);
  1505. }
  1506. }
  1507. advance_token(f);
  1508. return prev;
  1509. }
  1510. Token expect_token_after(AstFile *f, TokenKind kind, char *msg) {
  1511. Token prev = f->curr_token;
  1512. if (prev.kind != kind) {
  1513. String p = token_strings[prev.kind];
  1514. syntax_error(f->curr_token, "Expected '%.*s' after %s, got '%.*s'",
  1515. LIT(token_strings[kind]),
  1516. msg,
  1517. LIT(p));
  1518. }
  1519. advance_token(f);
  1520. return prev;
  1521. }
  1522. Token expect_operator(AstFile *f) {
  1523. Token prev = f->curr_token;
  1524. if (!gb_is_between(prev.kind, Token__OperatorBegin+1, Token__OperatorEnd-1)) {
  1525. syntax_error(f->curr_token, "Expected an operator, got '%.*s'",
  1526. LIT(token_strings[prev.kind]));
  1527. } else if (!f->allow_range && (prev.kind == Token_Ellipsis || prev.kind == Token_HalfClosed)) {
  1528. syntax_error(f->curr_token, "Expected an non-range operator, got '%.*s'",
  1529. LIT(token_strings[prev.kind]));
  1530. }
  1531. advance_token(f);
  1532. return prev;
  1533. }
  1534. Token expect_keyword(AstFile *f) {
  1535. Token prev = f->curr_token;
  1536. if (!gb_is_between(prev.kind, Token__KeywordBegin+1, Token__KeywordEnd-1)) {
  1537. syntax_error(f->curr_token, "Expected a keyword, got '%.*s'",
  1538. LIT(token_strings[prev.kind]));
  1539. }
  1540. advance_token(f);
  1541. return prev;
  1542. }
  1543. bool allow_token(AstFile *f, TokenKind kind) {
  1544. Token prev = f->curr_token;
  1545. if (prev.kind == kind) {
  1546. advance_token(f);
  1547. return true;
  1548. }
  1549. return false;
  1550. }
  1551. bool is_blank_ident(String str) {
  1552. if (str.len == 1) {
  1553. return str[0] == '_';
  1554. }
  1555. return false;
  1556. }
  1557. bool is_blank_ident(Token token) {
  1558. if (token.kind == Token_Ident) {
  1559. return is_blank_ident(token.string);
  1560. }
  1561. return false;
  1562. }
  1563. bool is_blank_ident(AstNode *node) {
  1564. if (node->kind == AstNode_Ident) {
  1565. ast_node(i, Ident, node);
  1566. return is_blank_ident(i->token.string);
  1567. }
  1568. return false;
  1569. }
  1570. // NOTE(bill): Go to next statement to prevent numerous error messages popping up
  1571. void fix_advance_to_next_stmt(AstFile *f) {
  1572. for (;;) {
  1573. Token t = f->curr_token;
  1574. switch (t.kind) {
  1575. case Token_EOF:
  1576. case Token_Semicolon:
  1577. return;
  1578. case Token_foreign:
  1579. case Token_import:
  1580. case Token_export:
  1581. case Token_if:
  1582. case Token_for:
  1583. case Token_when:
  1584. case Token_return:
  1585. case Token_switch:
  1586. case Token_defer:
  1587. case Token_asm:
  1588. case Token_using:
  1589. // case Token_thread_local:
  1590. // case Token_no_alias:
  1591. case Token_break:
  1592. case Token_continue:
  1593. case Token_fallthrough:
  1594. case Token_Hash:
  1595. {
  1596. if (t.pos == f->fix_prev_pos &&
  1597. f->fix_count < PARSER_MAX_FIX_COUNT) {
  1598. f->fix_count++;
  1599. return;
  1600. }
  1601. if (f->fix_prev_pos < t.pos) {
  1602. f->fix_prev_pos = t.pos;
  1603. f->fix_count = 0; // NOTE(bill): Reset
  1604. return;
  1605. }
  1606. // NOTE(bill): Reaching here means there is a parsing bug
  1607. } break;
  1608. }
  1609. advance_token(f);
  1610. }
  1611. }
  1612. Token expect_closing(AstFile *f, TokenKind kind, String context) {
  1613. if (f->curr_token.kind != kind &&
  1614. f->curr_token.kind == Token_Semicolon &&
  1615. f->curr_token.string == "\n") {
  1616. syntax_error(f->curr_token, "Missing ',' before newline in %.*s", LIT(context));
  1617. advance_token(f);
  1618. }
  1619. return expect_token(f, kind);
  1620. }
  1621. bool is_semicolon_optional_for_node(AstFile *f, AstNode *s) {
  1622. if (s == nullptr) {
  1623. return false;
  1624. }
  1625. switch (s->kind) {
  1626. case AstNode_EmptyStmt:
  1627. return true;
  1628. case AstNode_IfStmt:
  1629. case AstNode_WhenStmt:
  1630. case AstNode_ForStmt:
  1631. case AstNode_RangeStmt:
  1632. case AstNode_SwitchStmt:
  1633. case AstNode_TypeSwitchStmt:
  1634. return true;
  1635. case AstNode_HelperType:
  1636. return is_semicolon_optional_for_node(f, s->HelperType.type);
  1637. case AstNode_AliasType:
  1638. return is_semicolon_optional_for_node(f, s->AliasType.type);
  1639. case AstNode_PointerType:
  1640. return is_semicolon_optional_for_node(f, s->PointerType.type);
  1641. case AstNode_StructType:
  1642. case AstNode_UnionType:
  1643. case AstNode_EnumType:
  1644. case AstNode_BitFieldType:
  1645. return true;
  1646. case AstNode_ProcLit:
  1647. return s->ProcLit.body != nullptr;
  1648. case AstNode_ImportDecl:
  1649. case AstNode_ExportDecl:
  1650. case AstNode_ForeignImportDecl:
  1651. return true;
  1652. case AstNode_ValueDecl:
  1653. if (s->ValueDecl.is_mutable) {
  1654. return false;
  1655. }
  1656. if (s->ValueDecl.values.count > 0) {
  1657. return is_semicolon_optional_for_node(f, s->ValueDecl.values[s->ValueDecl.values.count-1]);
  1658. }
  1659. break;
  1660. case AstNode_ForeignBlockDecl:
  1661. if (s->ForeignBlockDecl.close.pos.line != 0) {
  1662. return true;
  1663. }
  1664. if (s->ForeignBlockDecl.decls.count == 1) {
  1665. return is_semicolon_optional_for_node(f, s->ForeignBlockDecl.decls[0]);
  1666. }
  1667. break;
  1668. }
  1669. return false;
  1670. }
  1671. void expect_semicolon(AstFile *f, AstNode *s) {
  1672. if (allow_token(f, Token_Semicolon)) {
  1673. return;
  1674. }
  1675. Token prev_token = f->prev_token;
  1676. if (prev_token.kind == Token_Semicolon) {
  1677. return;
  1678. }
  1679. switch (f->curr_token.kind) {
  1680. case Token_EOF:
  1681. return;
  1682. }
  1683. if (s != nullptr) {
  1684. if (prev_token.pos.line != f->curr_token.pos.line) {
  1685. if (is_semicolon_optional_for_node(f, s)) {
  1686. return;
  1687. }
  1688. } else if (f->curr_token.kind == Token_CloseBrace) {
  1689. return;
  1690. }
  1691. String node_string = ast_node_strings[s->kind];
  1692. syntax_error(prev_token, "Expected ';' after %.*s, got %.*s",
  1693. LIT(node_string), LIT(token_strings[prev_token.kind]));
  1694. } else {
  1695. syntax_error(prev_token, "Expected ';'");
  1696. }
  1697. fix_advance_to_next_stmt(f);
  1698. }
  1699. AstNode * parse_expr(AstFile *f, bool lhs);
  1700. AstNode * parse_proc_type(AstFile *f, Token proc_token);
  1701. Array<AstNode *> parse_stmt_list(AstFile *f);
  1702. AstNode * parse_stmt(AstFile *f);
  1703. AstNode * parse_body(AstFile *f);
  1704. AstNode *parse_ident(AstFile *f) {
  1705. Token token = f->curr_token;
  1706. if (token.kind == Token_Ident) {
  1707. advance_token(f);
  1708. } else {
  1709. token.string = str_lit("_");
  1710. expect_token(f, Token_Ident);
  1711. }
  1712. return ast_ident(f, token);
  1713. }
  1714. AstNode *parse_tag_expr(AstFile *f, AstNode *expression) {
  1715. Token token = expect_token(f, Token_Hash);
  1716. Token name = expect_token(f, Token_Ident);
  1717. return ast_tag_expr(f, token, name, expression);
  1718. }
  1719. AstNode *unparen_expr(AstNode *node) {
  1720. for (;;) {
  1721. if (node == nullptr) {
  1722. return nullptr;
  1723. }
  1724. if (node->kind != AstNode_ParenExpr) {
  1725. return node;
  1726. }
  1727. node = node->ParenExpr.expr;
  1728. }
  1729. }
  1730. AstNode *parse_value(AstFile *f);
  1731. Array<AstNode *> parse_element_list(AstFile *f) {
  1732. Array<AstNode *> elems = make_ast_node_array(f);
  1733. while (f->curr_token.kind != Token_CloseBrace &&
  1734. f->curr_token.kind != Token_EOF) {
  1735. AstNode *elem = parse_value(f);
  1736. if (f->curr_token.kind == Token_Eq) {
  1737. Token eq = expect_token(f, Token_Eq);
  1738. AstNode *value = parse_value(f);
  1739. elem = ast_field_value(f, elem, value, eq);
  1740. }
  1741. array_add(&elems, elem);
  1742. if (!allow_token(f, Token_Comma)) {
  1743. break;
  1744. }
  1745. }
  1746. return elems;
  1747. }
  1748. AstNode *parse_literal_value(AstFile *f, AstNode *type) {
  1749. Array<AstNode *> elems = {};
  1750. Token open = expect_token(f, Token_OpenBrace);
  1751. f->expr_level++;
  1752. if (f->curr_token.kind != Token_CloseBrace) {
  1753. elems = parse_element_list(f);
  1754. }
  1755. f->expr_level--;
  1756. Token close = expect_closing(f, Token_CloseBrace, str_lit("compound literal"));
  1757. return ast_compound_lit(f, type, elems, open, close);
  1758. }
  1759. AstNode *parse_value(AstFile *f) {
  1760. if (f->curr_token.kind == Token_OpenBrace) {
  1761. return parse_literal_value(f, nullptr);
  1762. }
  1763. AstNode *value = parse_expr(f, false);
  1764. return value;
  1765. }
  1766. AstNode *parse_type_or_ident(AstFile *f);
  1767. void check_proc_add_tag(AstFile *f, AstNode *tag_expr, u64 *tags, ProcTag tag, String tag_name) {
  1768. if (*tags & tag) {
  1769. syntax_error(tag_expr, "Procedure tag already used: %.*s", LIT(tag_name));
  1770. }
  1771. *tags |= tag;
  1772. }
  1773. bool is_foreign_name_valid(String name) {
  1774. if (name.len == 0) {
  1775. return false;
  1776. }
  1777. isize offset = 0;
  1778. while (offset < name.len) {
  1779. Rune rune;
  1780. isize remaining = name.len - offset;
  1781. isize width = gb_utf8_decode(name.text+offset, remaining, &rune);
  1782. if (rune == GB_RUNE_INVALID && width == 1) {
  1783. return false;
  1784. } else if (rune == GB_RUNE_BOM && remaining > 0) {
  1785. return false;
  1786. }
  1787. if (offset == 0) {
  1788. switch (rune) {
  1789. case '-':
  1790. case '$':
  1791. case '.':
  1792. case '_':
  1793. break;
  1794. default:
  1795. if (!gb_char_is_alpha(cast(char)rune))
  1796. return false;
  1797. break;
  1798. }
  1799. } else {
  1800. switch (rune) {
  1801. case '-':
  1802. case '$':
  1803. case '.':
  1804. case '_':
  1805. break;
  1806. default:
  1807. if (!gb_char_is_alphanumeric(cast(char)rune)) {
  1808. return false;
  1809. }
  1810. break;
  1811. }
  1812. }
  1813. offset += width;
  1814. }
  1815. return true;
  1816. }
  1817. void parse_proc_tags(AstFile *f, u64 *tags) {
  1818. GB_ASSERT(tags != nullptr);
  1819. while (f->curr_token.kind == Token_Hash) {
  1820. AstNode *tag_expr = parse_tag_expr(f, nullptr);
  1821. ast_node(te, TagExpr, tag_expr);
  1822. String tag_name = te->name.string;
  1823. #define ELSE_IF_ADD_TAG(name) \
  1824. else if (tag_name == #name) { \
  1825. check_proc_add_tag(f, tag_expr, tags, ProcTag_##name, tag_name); \
  1826. }
  1827. if (false) {}
  1828. ELSE_IF_ADD_TAG(require_results)
  1829. ELSE_IF_ADD_TAG(bounds_check)
  1830. ELSE_IF_ADD_TAG(no_bounds_check)
  1831. else {
  1832. syntax_error(tag_expr, "Unknown procedure type tag #%.*s", LIT(tag_name));
  1833. }
  1834. #undef ELSE_IF_ADD_TAG
  1835. }
  1836. if ((*tags & ProcTag_bounds_check) && (*tags & ProcTag_no_bounds_check)) {
  1837. syntax_error(f->curr_token, "You cannot apply both #bounds_check and #no_bounds_check to a procedure");
  1838. }
  1839. }
  1840. Array<AstNode *> parse_lhs_expr_list (AstFile *f);
  1841. Array<AstNode *> parse_rhs_expr_list (AstFile *f);
  1842. AstNode * parse_simple_stmt (AstFile *f, StmtAllowFlag flags);
  1843. AstNode * parse_type (AstFile *f);
  1844. AstNode * parse_call_expr (AstFile *f, AstNode *operand);
  1845. AstNode * parse_struct_field_list(AstFile *f, isize *name_count_);
  1846. AstNode *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, TokenKind follow, bool allow_default_parameters, bool allow_type_token);
  1847. AstNode *convert_stmt_to_expr(AstFile *f, AstNode *statement, String kind) {
  1848. if (statement == nullptr) {
  1849. return nullptr;
  1850. }
  1851. if (statement->kind == AstNode_ExprStmt) {
  1852. return statement->ExprStmt.expr;
  1853. }
  1854. syntax_error(f->curr_token, "Expected '%.*s', found a simple statement.", LIT(kind));
  1855. Token end = f->curr_token;
  1856. if (f->tokens.count < f->curr_token_index) {
  1857. end = f->tokens[f->curr_token_index+1];
  1858. }
  1859. return ast_bad_expr(f, f->curr_token, end);
  1860. }
  1861. AstNode *convert_stmt_to_body(AstFile *f, AstNode *stmt) {
  1862. if (stmt->kind == AstNode_BlockStmt) {
  1863. syntax_error(stmt, "Expected a normal statement rather than a block statement");
  1864. return stmt;
  1865. }
  1866. if (stmt->kind == AstNode_EmptyStmt) {
  1867. syntax_error(stmt, "Expected a non-empty statement");
  1868. }
  1869. GB_ASSERT(is_ast_node_stmt(stmt) || is_ast_node_decl(stmt));
  1870. Token open = ast_node_token(stmt);
  1871. Token close = ast_node_token(stmt);
  1872. Array<AstNode *> stmts = make_ast_node_array(f, 1);
  1873. array_add(&stmts, stmt);
  1874. return ast_block_stmt(f, stmts, open, close);
  1875. }
  1876. AstNode *parse_operand(AstFile *f, bool lhs) {
  1877. AstNode *operand = nullptr; // Operand
  1878. switch (f->curr_token.kind) {
  1879. case Token_Ident:
  1880. return parse_ident(f);
  1881. case Token_Undef:
  1882. return ast_undef(f, expect_token(f, Token_Undef));
  1883. case Token_context:
  1884. return ast_implicit(f, expect_token(f, Token_context));
  1885. case Token_Integer:
  1886. case Token_Float:
  1887. case Token_Imag:
  1888. case Token_Rune:
  1889. return ast_basic_lit(f, advance_token(f));
  1890. case Token_size_of:
  1891. case Token_align_of:
  1892. case Token_offset_of:
  1893. case Token_type_info_of:
  1894. return parse_call_expr(f, ast_implicit(f, advance_token(f)));
  1895. case Token_String: {
  1896. Token token = advance_token(f);
  1897. if (f->curr_token.kind == Token_String) {
  1898. // NOTE(bill): Allow neighbouring string literals to be merge together to
  1899. // become one big string
  1900. String s = f->curr_token.string;
  1901. Array<u8> data = {};
  1902. array_init_count(&data, heap_allocator(), token.string.len+s.len);
  1903. gb_memmove(data.data, token.string.text, token.string.len);
  1904. while (f->curr_token.kind == Token_String) {
  1905. String s = f->curr_token.string;
  1906. isize old_count = data.count;
  1907. array_resize(&data, data.count + s.len);
  1908. gb_memmove(data.data+old_count, s.text, s.len);
  1909. advance_token(f);
  1910. }
  1911. token.string = make_string(data.data, data.count);
  1912. array_add(&f->tokenizer.allocated_strings, token.string);
  1913. }
  1914. return ast_basic_lit(f, token);
  1915. }
  1916. case Token_OpenBrace:
  1917. if (!lhs) return parse_literal_value(f, nullptr);
  1918. break;
  1919. case Token_OpenParen: {
  1920. Token open, close;
  1921. // NOTE(bill): Skip the Paren Expression
  1922. open = expect_token(f, Token_OpenParen);
  1923. f->expr_level++;
  1924. operand = parse_expr(f, false);
  1925. f->expr_level--;
  1926. close = expect_token(f, Token_CloseParen);
  1927. return ast_paren_expr(f, operand, open, close);
  1928. }
  1929. case Token_Hash: {
  1930. Token token = expect_token(f, Token_Hash);
  1931. if (allow_token(f, Token_type)) {
  1932. return ast_helper_type(f, token, parse_type(f));
  1933. }
  1934. Token name = expect_token(f, Token_Ident);
  1935. if (name.string == "alias") {
  1936. return ast_alias_type(f, token, parse_type(f));
  1937. } else if (name.string == "run") {
  1938. AstNode *expr = parse_expr(f, false);
  1939. operand = ast_run_expr(f, token, name, expr);
  1940. if (unparen_expr(expr)->kind != AstNode_CallExpr) {
  1941. syntax_error(expr, "#run can only be applied to procedure calls");
  1942. operand = ast_bad_expr(f, token, f->curr_token);
  1943. }
  1944. warning(token, "#run is not yet implemented");
  1945. } else if (name.string == "file") { return ast_basic_directive(f, token, name.string);
  1946. } else if (name.string == "line") { return ast_basic_directive(f, token, name.string);
  1947. } else if (name.string == "procedure") { return ast_basic_directive(f, token, name.string);
  1948. } else if (name.string == "caller_location") { return ast_basic_directive(f, token, name.string);
  1949. } else if (name.string == "location") {
  1950. AstNode *tag = ast_basic_directive(f, token, name.string);
  1951. return parse_call_expr(f, tag);
  1952. } else {
  1953. operand = ast_tag_expr(f, token, name, parse_expr(f, false));
  1954. }
  1955. return operand;
  1956. }
  1957. case Token_inline:
  1958. case Token_no_inline:
  1959. {
  1960. Token token = advance_token(f);
  1961. AstNode *expr = parse_operand(f, false);
  1962. if (expr->kind != AstNode_ProcLit) {
  1963. syntax_error(expr, "%.*s must be followed by a procedure literal, got %.*s", LIT(token.string), LIT(ast_node_strings[expr->kind]));
  1964. return ast_bad_expr(f, token, f->curr_token);
  1965. }
  1966. ProcInlining pi = ProcInlining_none;
  1967. if (token.kind == Token_inline) {
  1968. pi = ProcInlining_inline;
  1969. } else if (token.kind == Token_no_inline) {
  1970. pi = ProcInlining_no_inline;
  1971. }
  1972. if (pi != ProcInlining_none) {
  1973. if (expr->ProcLit.inlining != ProcInlining_none &&
  1974. expr->ProcLit.inlining != pi) {
  1975. syntax_error(expr, "You cannot apply both 'inline' and 'no_inline' to a procedure literal");
  1976. }
  1977. expr->ProcLit.inlining = pi;
  1978. }
  1979. return expr;
  1980. } break;
  1981. // Parse Procedure Type or Literal
  1982. case Token_proc: {
  1983. Token token = expect_token(f, Token_proc);
  1984. AstNode *type = parse_proc_type(f, token);
  1985. if (f->allow_type && f->expr_level < 0) {
  1986. return type;
  1987. }
  1988. u64 tags = type->ProcType.tags;
  1989. if (allow_token(f, Token_Undef)) {
  1990. return ast_proc_lit(f, type, nullptr, tags);
  1991. } else if (f->curr_token.kind == Token_OpenBrace) {
  1992. AstNode *curr_proc = f->curr_proc;
  1993. AstNode *body = nullptr;
  1994. f->curr_proc = type;
  1995. body = parse_body(f);
  1996. f->curr_proc = curr_proc;
  1997. return ast_proc_lit(f, type, body, tags);
  1998. } else if (allow_token(f, Token_do)) {
  1999. AstNode *curr_proc = f->curr_proc;
  2000. AstNode *body = nullptr;
  2001. f->curr_proc = type;
  2002. body = convert_stmt_to_body(f, parse_stmt(f));
  2003. f->curr_proc = curr_proc;
  2004. return ast_proc_lit(f, type, body, tags);
  2005. }
  2006. if (tags != 0) {
  2007. syntax_error(token, "A procedure type cannot have tags");
  2008. }
  2009. return type;
  2010. }
  2011. // Check for Types
  2012. case Token_Dollar: {
  2013. Token token = expect_token(f, Token_Dollar);
  2014. AstNode *type = parse_ident(f);
  2015. AstNode *specialization = nullptr;
  2016. if (allow_token(f, Token_Quo)) {
  2017. specialization = parse_type(f);
  2018. }
  2019. return ast_poly_type(f, token, type, specialization);
  2020. } break;
  2021. case Token_type_of: {
  2022. AstNode *i = ast_implicit(f, expect_token(f, Token_type_of));
  2023. AstNode *type = parse_call_expr(f, i);
  2024. while (f->curr_token.kind == Token_Period) {
  2025. Token token = advance_token(f);
  2026. AstNode *sel = parse_ident(f);
  2027. type = ast_selector_expr(f, token, type, sel);
  2028. }
  2029. return type;
  2030. } break;
  2031. case Token_Pointer: {
  2032. Token token = expect_token(f, Token_Pointer);
  2033. AstNode *elem = parse_type(f);
  2034. return ast_pointer_type(f, token, elem);
  2035. } break;
  2036. case Token_OpenBracket: {
  2037. Token token = expect_token(f, Token_OpenBracket);
  2038. AstNode *count_expr = nullptr;
  2039. bool is_vector = false;
  2040. if (f->curr_token.kind == Token_Ellipsis) {
  2041. count_expr = ast_unary_expr(f, expect_token(f, Token_Ellipsis), nullptr);
  2042. } else if (allow_token(f, Token_vector)) {
  2043. if (f->curr_token.kind != Token_CloseBracket) {
  2044. f->expr_level++;
  2045. count_expr = parse_expr(f, false);
  2046. f->expr_level--;
  2047. } else {
  2048. syntax_error(f->curr_token, "Vector type missing count");
  2049. }
  2050. is_vector = true;
  2051. } else if (allow_token(f, Token_dynamic)) {
  2052. expect_token(f, Token_CloseBracket);
  2053. return ast_dynamic_array_type(f, token, parse_type(f));
  2054. } else if (f->curr_token.kind != Token_CloseBracket) {
  2055. f->expr_level++;
  2056. count_expr = parse_expr(f, false);
  2057. f->expr_level--;
  2058. }
  2059. expect_token(f, Token_CloseBracket);
  2060. if (is_vector) {
  2061. return ast_vector_type(f, token, count_expr, parse_type(f));
  2062. }
  2063. return ast_array_type(f, token, count_expr, parse_type(f));
  2064. } break;
  2065. case Token_map: {
  2066. Token token = expect_token(f, Token_map);
  2067. AstNode *key = nullptr;
  2068. AstNode *value = nullptr;
  2069. Token open, close;
  2070. open = expect_token_after(f, Token_OpenBracket, "map");
  2071. key = parse_expr(f, true);
  2072. close = expect_token(f, Token_CloseBracket);
  2073. value = parse_type(f);
  2074. return ast_map_type(f, token, key, value);
  2075. } break;
  2076. case Token_struct: {
  2077. Token token = expect_token(f, Token_struct);
  2078. AstNode *polymorphic_params = nullptr;
  2079. bool is_packed = false;
  2080. bool is_ordered = false;
  2081. bool is_raw_union = false;
  2082. AstNode *align = nullptr;
  2083. if (allow_token(f, Token_OpenParen)) {
  2084. isize param_count = 0;
  2085. polymorphic_params = parse_field_list(f, &param_count, 0, Token_CloseParen, false, true);
  2086. if (param_count == 0) {
  2087. syntax_error(polymorphic_params, "Expected at least 1 polymorphic parametric");
  2088. polymorphic_params = nullptr;
  2089. }
  2090. expect_token_after(f, Token_CloseParen, "parameter list");
  2091. }
  2092. isize prev_level = f->expr_level;
  2093. f->expr_level = -1;
  2094. while (allow_token(f, Token_Hash)) {
  2095. Token tag = expect_token_after(f, Token_Ident, "#");
  2096. if (tag.string == "packed") {
  2097. if (is_packed) {
  2098. syntax_error(tag, "Duplicate struct tag '#%.*s'", LIT(tag.string));
  2099. }
  2100. is_packed = true;
  2101. } else if (tag.string == "ordered") {
  2102. if (is_ordered) {
  2103. syntax_error(tag, "Duplicate struct tag '#%.*s'", LIT(tag.string));
  2104. }
  2105. is_ordered = true;
  2106. } else if (tag.string == "align") {
  2107. if (align) {
  2108. syntax_error(tag, "Duplicate struct tag '#%.*s'", LIT(tag.string));
  2109. }
  2110. align = parse_expr(f, true);
  2111. } else if (tag.string == "raw_union") {
  2112. if (is_raw_union) {
  2113. syntax_error(tag, "Duplicate struct tag '#%.*s'", LIT(tag.string));
  2114. }
  2115. is_raw_union = true;
  2116. } else {
  2117. syntax_error(tag, "Invalid struct tag '#%.*s'", LIT(tag.string));
  2118. }
  2119. }
  2120. f->expr_level = prev_level;
  2121. if (is_packed && is_ordered) {
  2122. syntax_error(token, "'#ordered' is not needed with '#packed' which implies ordering");
  2123. }
  2124. if (is_raw_union && is_packed) {
  2125. is_packed = false;
  2126. syntax_error(token, "'#raw_union' cannot also be '#packed'");
  2127. }
  2128. if (is_raw_union && is_ordered) {
  2129. is_ordered = false;
  2130. syntax_error(token, "'#raw_union' cannot also be '#ordered'");
  2131. }
  2132. Token open = expect_token_after(f, Token_OpenBrace, "struct");
  2133. isize name_count = 0;
  2134. AstNode *fields = parse_struct_field_list(f, &name_count);
  2135. Token close = expect_token(f, Token_CloseBrace);
  2136. Array<AstNode *> decls = {};
  2137. if (fields != nullptr) {
  2138. GB_ASSERT(fields->kind == AstNode_FieldList);
  2139. decls = fields->FieldList.list;
  2140. }
  2141. return ast_struct_type(f, token, decls, name_count, polymorphic_params, is_packed, is_ordered, is_raw_union, align);
  2142. } break;
  2143. case Token_union: {
  2144. Token token = expect_token(f, Token_union);
  2145. Token open = expect_token_after(f, Token_OpenBrace, "union");
  2146. Array<AstNode *> variants = make_ast_node_array(f);
  2147. isize total_decl_name_count = 0;
  2148. AstNode *align = nullptr;
  2149. CommentGroup docs = f->lead_comment;
  2150. Token start_token = f->curr_token;
  2151. while (allow_token(f, Token_Hash)) {
  2152. Token tag = expect_token_after(f, Token_Ident, "#");
  2153. if (tag.string == "align") {
  2154. if (align) {
  2155. syntax_error(tag, "Duplicate union tag '#%.*s'", LIT(tag.string));
  2156. }
  2157. align = parse_expr(f, true);
  2158. } else {
  2159. syntax_error(tag, "Invalid union tag '#%.*s'", LIT(tag.string));
  2160. }
  2161. }
  2162. while (f->curr_token.kind != Token_CloseBrace &&
  2163. f->curr_token.kind != Token_EOF) {
  2164. AstNode *type = parse_type(f);
  2165. if (type->kind != AstNode_BadExpr) {
  2166. array_add(&variants, type);
  2167. }
  2168. if (!allow_token(f, Token_Comma)) {
  2169. break;
  2170. }
  2171. }
  2172. Token close = expect_token(f, Token_CloseBrace);
  2173. return ast_union_type(f, token, variants, align);
  2174. } break;
  2175. case Token_enum: {
  2176. Token token = expect_token(f, Token_enum);
  2177. AstNode *base_type = nullptr;
  2178. if (f->curr_token.kind != Token_OpenBrace) {
  2179. base_type = parse_type(f);
  2180. }
  2181. Token open = expect_token(f, Token_OpenBrace);
  2182. Array<AstNode *> values = parse_element_list(f);
  2183. Token close = expect_token(f, Token_CloseBrace);
  2184. return ast_enum_type(f, token, base_type, values);
  2185. } break;
  2186. case Token_bit_field: {
  2187. Token token = expect_token(f, Token_bit_field);
  2188. Array<AstNode *> fields = make_ast_node_array(f);
  2189. AstNode *align = nullptr;
  2190. Token open, close;
  2191. isize prev_level = f->expr_level;
  2192. f->expr_level = -1;
  2193. while (allow_token(f, Token_Hash)) {
  2194. Token tag = expect_token_after(f, Token_Ident, "#");
  2195. if (tag.string == "align") {
  2196. if (align) {
  2197. syntax_error(tag, "Duplicate bit_field tag '#%.*s'", LIT(tag.string));
  2198. }
  2199. align = parse_expr(f, true);
  2200. } else {
  2201. syntax_error(tag, "Invalid bit_field tag '#%.*s'", LIT(tag.string));
  2202. }
  2203. }
  2204. f->expr_level = prev_level;
  2205. open = expect_token_after(f, Token_OpenBrace, "bit_field");
  2206. while (f->curr_token.kind != Token_EOF &&
  2207. f->curr_token.kind != Token_CloseBrace) {
  2208. AstNode *name = parse_ident(f);
  2209. Token colon = expect_token(f, Token_Colon);
  2210. AstNode *value = parse_expr(f, true);
  2211. AstNode *field = ast_field_value(f, name, value, colon);
  2212. array_add(&fields, field);
  2213. if (f->curr_token.kind != Token_Comma) {
  2214. break;
  2215. }
  2216. advance_token(f);
  2217. }
  2218. close = expect_token(f, Token_CloseBrace);
  2219. return ast_bit_field_type(f, token, fields, align);
  2220. } break;
  2221. default: {
  2222. #if 0
  2223. AstNode *type = parse_type_or_ident(f);
  2224. if (type != nullptr) {
  2225. // TODO(bill): Is this correct???
  2226. // NOTE(bill): Sanity check as identifiers should be handled already
  2227. TokenPos pos = ast_node_token(type).pos;
  2228. GB_ASSERT_MSG(type->kind != AstNode_Ident, "Type cannot be identifier %.*s(%td:%td)", LIT(pos.file), pos.line, pos.column);
  2229. return type;
  2230. }
  2231. #endif
  2232. break;
  2233. }
  2234. }
  2235. return nullptr;
  2236. }
  2237. bool is_literal_type(AstNode *node) {
  2238. node = unparen_expr(node);
  2239. switch (node->kind) {
  2240. case AstNode_BadExpr:
  2241. case AstNode_Ident:
  2242. case AstNode_SelectorExpr:
  2243. case AstNode_ArrayType:
  2244. case AstNode_VectorType:
  2245. case AstNode_StructType:
  2246. case AstNode_UnionType:
  2247. case AstNode_EnumType:
  2248. case AstNode_DynamicArrayType:
  2249. case AstNode_MapType:
  2250. case AstNode_CallExpr:
  2251. return true;
  2252. }
  2253. return false;
  2254. }
  2255. AstNode *parse_call_expr(AstFile *f, AstNode *operand) {
  2256. Array<AstNode *> args = make_ast_node_array(f);
  2257. Token open_paren, close_paren;
  2258. Token ellipsis = {};
  2259. f->expr_level++;
  2260. open_paren = expect_token(f, Token_OpenParen);
  2261. while (f->curr_token.kind != Token_CloseParen &&
  2262. f->curr_token.kind != Token_EOF &&
  2263. ellipsis.pos.line == 0) {
  2264. if (f->curr_token.kind == Token_Comma) {
  2265. syntax_error(f->curr_token, "Expected an expression not ,");
  2266. } else if (f->curr_token.kind == Token_Eq) {
  2267. syntax_error(f->curr_token, "Expected an expression not =");
  2268. }
  2269. bool prefix_ellipsis = false;
  2270. if (f->curr_token.kind == Token_Ellipsis) {
  2271. prefix_ellipsis = true;
  2272. ellipsis = expect_token(f, Token_Ellipsis);
  2273. }
  2274. AstNode *arg = parse_expr(f, false);
  2275. if (f->curr_token.kind == Token_Eq) {
  2276. Token eq = expect_token(f, Token_Eq);
  2277. if (prefix_ellipsis) {
  2278. syntax_error(ellipsis, "'...' must be applied to value rather than the field name");
  2279. }
  2280. AstNode *value = parse_value(f);
  2281. arg = ast_field_value(f, arg, value, eq);
  2282. }
  2283. array_add(&args, arg);
  2284. if (!allow_token(f, Token_Comma)) {
  2285. break;
  2286. }
  2287. }
  2288. f->expr_level--;
  2289. close_paren = expect_closing(f, Token_CloseParen, str_lit("argument list"));
  2290. return ast_call_expr(f, operand, args, open_paren, close_paren, ellipsis);
  2291. }
  2292. AstNode *parse_macro_call_expr(AstFile *f, AstNode *operand) {
  2293. Array<AstNode *> args = make_ast_node_array(f);
  2294. Token bang, open_paren, close_paren;
  2295. bang = expect_token(f, Token_Not);
  2296. f->expr_level++;
  2297. open_paren = expect_token(f, Token_OpenParen);
  2298. while (f->curr_token.kind != Token_CloseParen &&
  2299. f->curr_token.kind != Token_EOF) {
  2300. if (f->curr_token.kind == Token_Comma) {
  2301. syntax_error(f->curr_token, "Expected an expression not a ,");
  2302. }
  2303. AstNode *arg = parse_expr(f, false);
  2304. array_add(&args, arg);
  2305. if (!allow_token(f, Token_Comma)) {
  2306. break;
  2307. }
  2308. }
  2309. f->expr_level--;
  2310. close_paren = expect_closing(f, Token_CloseParen, str_lit("argument list"));
  2311. return ast_macro_call_expr(f, operand, bang, args, open_paren, close_paren);
  2312. }
  2313. AstNode *parse_atom_expr(AstFile *f, AstNode *operand, bool lhs) {
  2314. if (operand == nullptr) {
  2315. if (f->allow_type) return nullptr;
  2316. Token begin = f->curr_token;
  2317. syntax_error(begin, "Expected an operand");
  2318. fix_advance_to_next_stmt(f);
  2319. operand = ast_bad_expr(f, begin, f->curr_token);
  2320. }
  2321. bool loop = true;
  2322. while (loop) {
  2323. switch (f->curr_token.kind) {
  2324. case Token_OpenParen:
  2325. operand = parse_call_expr(f, operand);
  2326. break;
  2327. case Token_Not:
  2328. operand = parse_macro_call_expr(f, operand);
  2329. break;
  2330. case Token_Period: {
  2331. Token token = advance_token(f);
  2332. switch (f->curr_token.kind) {
  2333. case Token_Ident:
  2334. operand = ast_selector_expr(f, token, operand, parse_ident(f));
  2335. break;
  2336. case Token_Integer:
  2337. operand = ast_selector_expr(f, token, operand, parse_expr(f, lhs));
  2338. break;
  2339. case Token_OpenParen: {
  2340. Token open = expect_token(f, Token_OpenParen);
  2341. AstNode *type = parse_type(f);
  2342. Token close = expect_token(f, Token_CloseParen);
  2343. operand = ast_type_assertion(f, operand, token, type);
  2344. } break;
  2345. default:
  2346. syntax_error(f->curr_token, "Expected a selector");
  2347. advance_token(f);
  2348. operand = ast_bad_expr(f, ast_node_token(operand), f->curr_token);
  2349. // operand = ast_selector_expr(f, f->curr_token, operand, nullptr);
  2350. break;
  2351. }
  2352. } break;
  2353. case Token_OpenBracket: {
  2354. if (lhs) {
  2355. // TODO(bill): Handle this
  2356. }
  2357. bool prev_allow_range = f->allow_range;
  2358. f->allow_range = false;
  2359. Token open = {}, close = {}, interval = {};
  2360. AstNode *indices[2] = {};
  2361. Token ellipsis = {};
  2362. bool is_ellipsis = false;
  2363. f->expr_level++;
  2364. open = expect_token(f, Token_OpenBracket);
  2365. if (f->curr_token.kind != Token_Ellipsis &&
  2366. f->curr_token.kind != Token_HalfClosed) {
  2367. indices[0] = parse_expr(f, false);
  2368. }
  2369. bool is_index = true;
  2370. if ((f->curr_token.kind == Token_Ellipsis ||
  2371. f->curr_token.kind == Token_HalfClosed)) {
  2372. ellipsis = advance_token(f);
  2373. is_ellipsis = true;
  2374. if (f->curr_token.kind != Token_Ellipsis &&
  2375. f->curr_token.kind != Token_HalfClosed &&
  2376. f->curr_token.kind != Token_CloseBracket &&
  2377. f->curr_token.kind != Token_EOF) {
  2378. indices[1] = parse_expr(f, false);
  2379. }
  2380. }
  2381. f->expr_level--;
  2382. close = expect_token(f, Token_CloseBracket);
  2383. if (is_ellipsis) {
  2384. operand = ast_slice_expr(f, operand, open, close, ellipsis, indices[0], indices[1]);
  2385. } else {
  2386. operand = ast_index_expr(f, operand, indices[0], open, close);
  2387. }
  2388. f->allow_range = prev_allow_range;
  2389. } break;
  2390. case Token_Pointer: // Deference
  2391. operand = ast_deref_expr(f, operand, expect_token(f, Token_Pointer));
  2392. break;
  2393. case Token_OpenBrace:
  2394. if (!lhs && is_literal_type(operand) && f->expr_level >= 0) {
  2395. operand = parse_literal_value(f, operand);
  2396. } else {
  2397. loop = false;
  2398. }
  2399. break;
  2400. default:
  2401. loop = false;
  2402. break;
  2403. }
  2404. lhs = false; // NOTE(bill): 'tis not lhs anymore
  2405. }
  2406. return operand;
  2407. }
  2408. AstNode *parse_unary_expr(AstFile *f, bool lhs) {
  2409. switch (f->curr_token.kind) {
  2410. case Token_Add:
  2411. case Token_Sub:
  2412. case Token_Not:
  2413. case Token_Xor:
  2414. case Token_And: {
  2415. Token op = advance_token(f);
  2416. return ast_unary_expr(f, op, parse_unary_expr(f, lhs));
  2417. } break;
  2418. case Token_cast: {
  2419. Token token = expect_token(f, Token_cast);
  2420. Token open = expect_token_after(f, Token_OpenParen, "cast");
  2421. AstNode *type = parse_type(f);
  2422. Token close = expect_token(f, Token_CloseParen);
  2423. return ast_type_cast(f, token, type, parse_unary_expr(f, lhs));
  2424. } break;
  2425. case Token_transmute: {
  2426. Token token = expect_token(f, Token_transmute);
  2427. Token open = expect_token_after(f, Token_OpenParen, "transmute");
  2428. AstNode *type = parse_type(f);
  2429. Token close = expect_token(f, Token_CloseParen);
  2430. return ast_type_cast(f, token, type, parse_unary_expr(f, lhs));
  2431. } break;
  2432. }
  2433. AstNode *operand = parse_operand(f, lhs);
  2434. return parse_atom_expr(f, operand, lhs);
  2435. }
  2436. bool is_ast_node_a_range(AstNode *expr) {
  2437. if (expr == nullptr) {
  2438. return false;
  2439. }
  2440. if (expr->kind != AstNode_BinaryExpr) {
  2441. return false;
  2442. }
  2443. TokenKind op = expr->BinaryExpr.op.kind;
  2444. switch (op) {
  2445. case Token_Ellipsis:
  2446. case Token_HalfClosed:
  2447. return true;
  2448. }
  2449. return false;
  2450. }
  2451. // NOTE(bill): result == priority
  2452. i32 token_precedence(AstFile *f, TokenKind t) {
  2453. switch (t) {
  2454. case Token_Question:
  2455. return 1;
  2456. case Token_Ellipsis:
  2457. case Token_HalfClosed:
  2458. if (f->allow_range) {
  2459. return 2;
  2460. }
  2461. return 0;
  2462. case Token_CmpOr:
  2463. return 3;
  2464. case Token_CmpAnd:
  2465. return 4;
  2466. case Token_CmpEq:
  2467. case Token_NotEq:
  2468. case Token_Lt:
  2469. case Token_Gt:
  2470. case Token_LtEq:
  2471. case Token_GtEq:
  2472. return 5;
  2473. case Token_Add:
  2474. case Token_Sub:
  2475. case Token_Or:
  2476. case Token_Xor:
  2477. return 6;
  2478. case Token_Mul:
  2479. case Token_Quo:
  2480. case Token_Mod:
  2481. case Token_ModMod:
  2482. case Token_And:
  2483. case Token_AndNot:
  2484. case Token_Shl:
  2485. case Token_Shr:
  2486. return 7;
  2487. }
  2488. return 0;
  2489. }
  2490. AstNode *parse_binary_expr(AstFile *f, bool lhs, i32 prec_in) {
  2491. AstNode *expr = parse_unary_expr(f, lhs);
  2492. for (i32 prec = token_precedence(f, f->curr_token.kind); prec >= prec_in; prec--) {
  2493. for (;;) {
  2494. Token op = f->curr_token;
  2495. i32 op_prec = token_precedence(f, op.kind);
  2496. if (op_prec != prec) {
  2497. // NOTE(bill): This will also catch operators that are not valid "binary" operators
  2498. break;
  2499. }
  2500. expect_operator(f); // NOTE(bill): error checks too
  2501. if (op.kind == Token_Question) {
  2502. AstNode *cond = expr;
  2503. // Token_Question
  2504. AstNode *x = parse_expr(f, lhs);
  2505. Token token_c = expect_token(f, Token_Colon);
  2506. AstNode *y = parse_expr(f, lhs);
  2507. expr = ast_ternary_expr(f, cond, x, y);
  2508. } else {
  2509. AstNode *right = parse_binary_expr(f, false, prec+1);
  2510. if (right == nullptr) {
  2511. syntax_error(op, "Expected expression on the right-hand side of the binary operator");
  2512. }
  2513. expr = ast_binary_expr(f, op, expr, right);
  2514. }
  2515. lhs = false;
  2516. }
  2517. }
  2518. return expr;
  2519. }
  2520. AstNode *parse_expr(AstFile *f, bool lhs) {
  2521. return parse_binary_expr(f, lhs, 0+1);
  2522. }
  2523. Array<AstNode *> parse_expr_list(AstFile *f, bool lhs) {
  2524. Array<AstNode *> list = make_ast_node_array(f);
  2525. for (;;) {
  2526. AstNode *e = parse_expr(f, lhs);
  2527. array_add(&list, e);
  2528. if (f->curr_token.kind != Token_Comma ||
  2529. f->curr_token.kind == Token_EOF) {
  2530. break;
  2531. }
  2532. advance_token(f);
  2533. }
  2534. return list;
  2535. }
  2536. Array<AstNode *> parse_lhs_expr_list(AstFile *f) {
  2537. return parse_expr_list(f, true);
  2538. }
  2539. Array<AstNode *> parse_rhs_expr_list(AstFile *f) {
  2540. return parse_expr_list(f, false);
  2541. }
  2542. Array<AstNode *> parse_ident_list(AstFile *f) {
  2543. Array<AstNode *> list = make_ast_node_array(f);
  2544. do {
  2545. array_add(&list, parse_ident(f));
  2546. if (f->curr_token.kind != Token_Comma ||
  2547. f->curr_token.kind == Token_EOF) {
  2548. break;
  2549. }
  2550. advance_token(f);
  2551. } while (true);
  2552. return list;
  2553. }
  2554. AstNode *parse_type_attempt(AstFile *f) {
  2555. AstNode *type = parse_type_or_ident(f);
  2556. if (type != nullptr) {
  2557. // TODO(bill): Handle?
  2558. }
  2559. return type;
  2560. }
  2561. AstNode *parse_type(AstFile *f) {
  2562. AstNode *type = parse_type_attempt(f);
  2563. if (type == nullptr) {
  2564. Token token = advance_token(f);
  2565. syntax_error(token, "Expected a type");
  2566. return ast_bad_expr(f, token, f->curr_token);
  2567. }
  2568. return type;
  2569. }
  2570. void parse_foreign_block_decl(AstFile *f, Array<AstNode *> *decls) {
  2571. AstNode *decl = parse_stmt(f);
  2572. switch (decl->kind) {
  2573. case AstNode_EmptyStmt:
  2574. case AstNode_BadStmt:
  2575. case AstNode_BadDecl:
  2576. return;
  2577. case AstNode_WhenStmt:
  2578. case AstNode_ValueDecl:
  2579. array_add(decls, decl);
  2580. return;
  2581. default:
  2582. syntax_error(decl, "Foreign blocks only allow procedure and variable declarations");
  2583. return;
  2584. }
  2585. }
  2586. AstNode *parse_foreign_block(AstFile *f, Token token) {
  2587. CommentGroup docs = f->lead_comment;
  2588. AstNode *foreign_library = nullptr;
  2589. if (f->curr_token.kind == Token_export) {
  2590. foreign_library = ast_implicit(f, expect_token(f, Token_export));
  2591. } else {
  2592. foreign_library = parse_ident(f);
  2593. }
  2594. Token open = {};
  2595. Token close = {};
  2596. Array<AstNode *> decls = make_ast_node_array(f);
  2597. bool prev_in_foreign_block = f->in_foreign_block;
  2598. defer (f->in_foreign_block = prev_in_foreign_block);
  2599. f->in_foreign_block = true;
  2600. if (f->curr_token.kind != Token_OpenBrace) {
  2601. parse_foreign_block_decl(f, &decls);
  2602. } else {
  2603. open = expect_token(f, Token_OpenBrace);
  2604. while (f->curr_token.kind != Token_CloseBrace &&
  2605. f->curr_token.kind != Token_EOF) {
  2606. parse_foreign_block_decl(f, &decls);
  2607. }
  2608. close = expect_token(f, Token_CloseBrace);
  2609. }
  2610. AstNode *decl = ast_foreign_block_decl(f, token, foreign_library, open, close, decls, docs);
  2611. expect_semicolon(f, decl);
  2612. return decl;
  2613. }
  2614. AstNode *parse_value_decl(AstFile *f, Array<AstNode *> names, CommentGroup docs) {
  2615. bool is_mutable = true;
  2616. AstNode *type = nullptr;
  2617. Array<AstNode *> values = {};
  2618. expect_token_after(f, Token_Colon, "identifier list");
  2619. if (f->curr_token.kind == Token_type) {
  2620. type = ast_type_type(f, advance_token(f), nullptr);
  2621. is_mutable = false;
  2622. } else {
  2623. type = parse_type_attempt(f);
  2624. }
  2625. if (f->curr_token.kind == Token_Eq ||
  2626. f->curr_token.kind == Token_Colon) {
  2627. Token sep = {};
  2628. if (!is_mutable) {
  2629. sep = expect_token_after(f, Token_Colon, "type");
  2630. } else {
  2631. sep = advance_token(f);
  2632. is_mutable = sep.kind != Token_Colon;
  2633. }
  2634. values = parse_rhs_expr_list(f);
  2635. if (values.count > names.count) {
  2636. syntax_error(f->curr_token, "Too many values on the right hand side of the declaration");
  2637. } else if (values.count < names.count && !is_mutable) {
  2638. syntax_error(f->curr_token, "All constant declarations must be defined");
  2639. } else if (values.count == 0) {
  2640. syntax_error(f->curr_token, "Expected an expression for this declaration");
  2641. }
  2642. }
  2643. if (is_mutable) {
  2644. if (type == nullptr && values.count == 0) {
  2645. syntax_error(f->curr_token, "Missing variable type or initialization");
  2646. return ast_bad_decl(f, f->curr_token, f->curr_token);
  2647. }
  2648. } else {
  2649. if (type == nullptr && values.count == 0 && names.count > 0) {
  2650. syntax_error(f->curr_token, "Missing constant value");
  2651. return ast_bad_decl(f, f->curr_token, f->curr_token);
  2652. }
  2653. }
  2654. if (values.data == nullptr) {
  2655. values = make_ast_node_array(f);
  2656. }
  2657. if (f->expr_level >= 0) {
  2658. AstNode *end = nullptr;
  2659. if (!is_mutable && values.count > 0) {
  2660. end = values[values.count-1];
  2661. }
  2662. if (f->curr_token.kind == Token_CloseBrace &&
  2663. f->curr_token.pos.line == f->prev_token.pos.line) {
  2664. } else {
  2665. expect_semicolon(f, end);
  2666. }
  2667. }
  2668. return ast_value_decl(f, names, type, values, is_mutable, docs, f->line_comment);
  2669. }
  2670. AstNode *parse_simple_stmt(AstFile *f, StmtAllowFlag flags) {
  2671. Token token = f->curr_token;
  2672. CommentGroup docs = f->lead_comment;
  2673. Array<AstNode *> lhs = parse_lhs_expr_list(f);
  2674. token = f->curr_token;
  2675. switch (token.kind) {
  2676. case Token_Eq:
  2677. case Token_AddEq:
  2678. case Token_SubEq:
  2679. case Token_MulEq:
  2680. case Token_QuoEq:
  2681. case Token_ModEq:
  2682. case Token_ModModEq:
  2683. case Token_AndEq:
  2684. case Token_OrEq:
  2685. case Token_XorEq:
  2686. case Token_ShlEq:
  2687. case Token_ShrEq:
  2688. case Token_AndNotEq:
  2689. case Token_CmpAndEq:
  2690. case Token_CmpOrEq:
  2691. {
  2692. if (f->curr_proc == nullptr) {
  2693. syntax_error(f->curr_token, "You cannot use a simple statement in the file scope");
  2694. return ast_bad_stmt(f, f->curr_token, f->curr_token);
  2695. }
  2696. advance_token(f);
  2697. Array<AstNode *> rhs = parse_rhs_expr_list(f);
  2698. if (rhs.count == 0) {
  2699. syntax_error(token, "No right-hand side in assignment statement.");
  2700. return ast_bad_stmt(f, token, f->curr_token);
  2701. }
  2702. return ast_assign_stmt(f, token, lhs, rhs);
  2703. } break;
  2704. case Token_in:
  2705. if (flags&StmtAllowFlag_In) {
  2706. allow_token(f, Token_in);
  2707. bool prev_allow_range = f->allow_range;
  2708. f->allow_range = true;
  2709. AstNode *expr = parse_expr(f, false);
  2710. f->allow_range = prev_allow_range;
  2711. Array<AstNode *> rhs = make_ast_node_array(f, 1);
  2712. array_add(&rhs, expr);
  2713. return ast_assign_stmt(f, token, lhs, rhs);
  2714. }
  2715. break;
  2716. case Token_Colon:
  2717. if ((flags&StmtAllowFlag_Label) && lhs.count == 1) {
  2718. TokenKind next = look_ahead_token_kind(f, 1);
  2719. switch (next) {
  2720. case Token_for:
  2721. case Token_switch: {
  2722. expect_token_after(f, Token_Colon, "identifier list");
  2723. AstNode *name = lhs[0];
  2724. AstNode *label = ast_label_decl(f, ast_node_token(name), name);
  2725. AstNode *stmt = parse_stmt(f);
  2726. #define _SET_LABEL(Kind_, label_) case GB_JOIN2(AstNode_, Kind_): (stmt->Kind_).label = label_; break
  2727. switch (stmt->kind) {
  2728. _SET_LABEL(ForStmt, label);
  2729. _SET_LABEL(RangeStmt, label);
  2730. _SET_LABEL(SwitchStmt, label);
  2731. _SET_LABEL(TypeSwitchStmt, label);
  2732. default:
  2733. syntax_error(token, "Labels can only be applied to a loop or match statement");
  2734. break;
  2735. }
  2736. #undef _SET_LABEL
  2737. return stmt;
  2738. } break;
  2739. }
  2740. }
  2741. return parse_value_decl(f, lhs, docs);
  2742. }
  2743. if (lhs.count > 1) {
  2744. syntax_error(token, "Expected 1 expression");
  2745. return ast_bad_stmt(f, token, f->curr_token);
  2746. }
  2747. #if 0
  2748. switch (token.kind) {
  2749. case Token_Inc:
  2750. case Token_Dec:
  2751. advance_token(f);
  2752. return ast_inc_dec_stmt(f, token, lhs[0]);
  2753. }
  2754. #endif
  2755. return ast_expr_stmt(f, lhs[0]);
  2756. }
  2757. AstNode *parse_block_stmt(AstFile *f, b32 is_when) {
  2758. if (!is_when && f->curr_proc == nullptr) {
  2759. syntax_error(f->curr_token, "You cannot use a block statement in the file scope");
  2760. return ast_bad_stmt(f, f->curr_token, f->curr_token);
  2761. }
  2762. return parse_body(f);
  2763. }
  2764. AstNode *parse_results(AstFile *f) {
  2765. if (!allow_token(f, Token_ArrowRight)) {
  2766. return nullptr;
  2767. }
  2768. isize prev_level = f->expr_level;
  2769. defer (f->expr_level = prev_level);
  2770. // f->expr_level = -1;
  2771. if (f->curr_token.kind != Token_OpenParen) {
  2772. CommentGroup empty_group = {};
  2773. Token begin_token = f->curr_token;
  2774. Array<AstNode *> empty_names = {};
  2775. Array<AstNode *> list = make_ast_node_array(f, 1);
  2776. AstNode *type = parse_type(f);
  2777. array_add(&list, ast_field(f, empty_names, type, nullptr, 0, empty_group, empty_group));
  2778. return ast_field_list(f, begin_token, list);
  2779. }
  2780. AstNode *list = nullptr;
  2781. expect_token(f, Token_OpenParen);
  2782. list = parse_field_list(f, nullptr, 0, Token_CloseParen, true, false);
  2783. expect_token_after(f, Token_CloseParen, "parameter list");
  2784. return list;
  2785. }
  2786. ProcCallingConvention string_to_calling_convention(String s) {
  2787. if (s == "odin") return ProcCC_Odin;
  2788. if (s == "contextless") return ProcCC_Contextless;
  2789. if (s == "cdecl") return ProcCC_CDecl;
  2790. if (s == "c") return ProcCC_CDecl;
  2791. if (s == "stdcall") return ProcCC_StdCall;
  2792. if (s == "std") return ProcCC_StdCall;
  2793. if (s == "fastcall") return ProcCC_FastCall;
  2794. if (s == "fast") return ProcCC_FastCall;
  2795. return ProcCC_Invalid;
  2796. }
  2797. AstNode *parse_proc_type(AstFile *f, Token proc_token) {
  2798. AstNode *params = nullptr;
  2799. AstNode *results = nullptr;
  2800. ProcCallingConvention cc = ProcCC_Invalid;
  2801. if (f->curr_token.kind == Token_String) {
  2802. Token token = expect_token(f, Token_String);
  2803. auto c = string_to_calling_convention(token.string);
  2804. if (c == ProcCC_Invalid) {
  2805. syntax_error(token, "Unknown procedure calling convention: '%.*s'\n", LIT(token.string));
  2806. } else {
  2807. cc = c;
  2808. }
  2809. }
  2810. if (cc == ProcCC_Invalid) {
  2811. if (f->in_foreign_block) {
  2812. cc = ProcCC_ForeignBlockDefault;
  2813. } else {
  2814. cc = ProcCC_Odin;
  2815. }
  2816. }
  2817. expect_token(f, Token_OpenParen);
  2818. params = parse_field_list(f, nullptr, FieldFlag_Signature, Token_CloseParen, true, true);
  2819. expect_token_after(f, Token_CloseParen, "parameter list");
  2820. results = parse_results(f);
  2821. u64 tags = 0;
  2822. parse_proc_tags(f, &tags);
  2823. bool is_generic = false;
  2824. for_array(i, params->FieldList.list) {
  2825. AstNode *param = params->FieldList.list[i];
  2826. ast_node(f, Field, param);
  2827. if (f->type != nullptr &&
  2828. (f->type->kind == AstNode_TypeType ||
  2829. f->type->kind == AstNode_PolyType)) {
  2830. is_generic = true;
  2831. break;
  2832. }
  2833. }
  2834. return ast_proc_type(f, proc_token, params, results, tags, cc, is_generic);
  2835. }
  2836. AstNode *parse_var_type(AstFile *f, bool allow_ellipsis, bool allow_type_token) {
  2837. if (allow_ellipsis && f->curr_token.kind == Token_Ellipsis) {
  2838. Token tok = advance_token(f);
  2839. AstNode *type = parse_type_or_ident(f);
  2840. if (type == nullptr) {
  2841. syntax_error(tok, "variadic field missing type after '...'");
  2842. type = ast_bad_expr(f, tok, f->curr_token);
  2843. }
  2844. return ast_ellipsis(f, tok, type);
  2845. }
  2846. AstNode *type = nullptr;
  2847. if (allow_type_token &&
  2848. f->curr_token.kind == Token_type) {
  2849. Token token = expect_token(f, Token_type);
  2850. AstNode *specialization = nullptr;
  2851. if (allow_token(f, Token_Quo)) {
  2852. specialization = parse_type(f);
  2853. }
  2854. type = ast_type_type(f, token, specialization);
  2855. } else {
  2856. type = parse_type_attempt(f);
  2857. }
  2858. if (type == nullptr) {
  2859. Token tok = f->curr_token;
  2860. syntax_error(tok, "Expected a type");
  2861. type = ast_bad_expr(f, tok, f->curr_token);
  2862. }
  2863. return type;
  2864. }
  2865. enum FieldPrefixKind {
  2866. FieldPrefix_Unknown = -1,
  2867. FieldPrefix_Invalid = 0,
  2868. FieldPrefix_using,
  2869. FieldPrefix_no_alias,
  2870. FieldPrefix_c_var_arg,
  2871. };
  2872. FieldPrefixKind is_token_field_prefix(AstFile *f) {
  2873. switch (f->curr_token.kind) {
  2874. case Token_EOF:
  2875. return FieldPrefix_Invalid;
  2876. case Token_using:
  2877. return FieldPrefix_using;
  2878. case Token_Hash:
  2879. advance_token(f);
  2880. switch (f->curr_token.kind) {
  2881. case Token_Ident:
  2882. if (f->curr_token.string == "no_alias") {
  2883. return FieldPrefix_no_alias;
  2884. } else if (f->curr_token.string == "c_vararg") {
  2885. return FieldPrefix_c_var_arg;
  2886. }
  2887. break;
  2888. }
  2889. return FieldPrefix_Unknown;
  2890. }
  2891. return FieldPrefix_Invalid;
  2892. }
  2893. u32 parse_field_prefixes(AstFile *f) {
  2894. i32 using_count = 0;
  2895. i32 no_alias_count = 0;
  2896. i32 c_vararg_count = 0;
  2897. for (;;) {
  2898. FieldPrefixKind kind = is_token_field_prefix(f);
  2899. if (kind == FieldPrefix_Invalid) {
  2900. break;
  2901. }
  2902. if (kind == FieldPrefix_Unknown) {
  2903. syntax_error(f->curr_token, "Unknown prefix kind '#%.*s'", LIT(f->curr_token.string));
  2904. advance_token(f);
  2905. continue;
  2906. }
  2907. switch (kind) {
  2908. case FieldPrefix_using: using_count += 1; advance_token(f); break;
  2909. case FieldPrefix_no_alias: no_alias_count += 1; advance_token(f); break;
  2910. case FieldPrefix_c_var_arg: c_vararg_count += 1; advance_token(f); break;
  2911. }
  2912. }
  2913. if (using_count > 1) syntax_error(f->curr_token, "Multiple 'using' in this field list");
  2914. if (no_alias_count > 1) syntax_error(f->curr_token, "Multiple '#no_alias' in this field list");
  2915. if (c_vararg_count > 1) syntax_error(f->curr_token, "Multiple '#c_vararg' in this field list");
  2916. u32 field_flags = 0;
  2917. if (using_count > 0) field_flags |= FieldFlag_using;
  2918. if (no_alias_count > 0) field_flags |= FieldFlag_no_alias;
  2919. if (c_vararg_count > 0) field_flags |= FieldFlag_c_vararg;
  2920. return field_flags;
  2921. }
  2922. u32 check_field_prefixes(AstFile *f, isize name_count, u32 allowed_flags, u32 set_flags) {
  2923. if (name_count > 1 && (set_flags&FieldFlag_using)) {
  2924. syntax_error(f->curr_token, "Cannot apply 'using' to more than one of the same type");
  2925. set_flags &= ~FieldFlag_using;
  2926. }
  2927. if ((allowed_flags&FieldFlag_using) == 0 && (set_flags&FieldFlag_using)) {
  2928. syntax_error(f->curr_token, "'using' is not allowed within this field list");
  2929. set_flags &= ~FieldFlag_using;
  2930. }
  2931. if ((allowed_flags&FieldFlag_no_alias) == 0 && (set_flags&FieldFlag_no_alias)) {
  2932. syntax_error(f->curr_token, "'#no_alias' is not allowed within this field list");
  2933. set_flags &= ~FieldFlag_no_alias;
  2934. }
  2935. if ((allowed_flags&FieldFlag_c_vararg) == 0 && (set_flags&FieldFlag_c_vararg)) {
  2936. syntax_error(f->curr_token, "'#c_vararg' is not allowed within this field list");
  2937. set_flags &= ~FieldFlag_c_vararg;
  2938. }
  2939. return set_flags;
  2940. }
  2941. struct AstNodeAndFlags {
  2942. AstNode *node;
  2943. u32 flags;
  2944. };
  2945. Array<AstNode *> convert_to_ident_list(AstFile *f, Array<AstNodeAndFlags> list, bool ignore_flags) {
  2946. Array<AstNode *> idents = make_ast_node_array(f, list.count);
  2947. // Convert to ident list
  2948. for_array(i, list) {
  2949. AstNode *ident = list[i].node;
  2950. if (!ignore_flags) {
  2951. if (i != 0) {
  2952. syntax_error(ident, "Illegal use of prefixes in parameter list");
  2953. }
  2954. }
  2955. switch (ident->kind) {
  2956. case AstNode_Ident:
  2957. case AstNode_BadExpr:
  2958. break;
  2959. default:
  2960. syntax_error(ident, "Expected an identifier");
  2961. ident = ast_ident(f, blank_token);
  2962. break;
  2963. }
  2964. array_add(&idents, ident);
  2965. }
  2966. return idents;
  2967. }
  2968. bool parse_expect_field_separator(AstFile *f, AstNode *param) {
  2969. Token token = f->curr_token;
  2970. if (allow_token(f, Token_Comma)) {
  2971. return true;
  2972. }
  2973. if (token.kind == Token_Semicolon) {
  2974. syntax_error(f->curr_token, "Expected a comma, got a semicolon");
  2975. advance_token(f);
  2976. return true;
  2977. }
  2978. return false;
  2979. }
  2980. bool parse_expect_struct_separator(AstFile *f, AstNode *param) {
  2981. Token token = f->curr_token;
  2982. if (allow_token(f, Token_Semicolon)) {
  2983. return true;
  2984. }
  2985. if (token.kind == Token_Colon) {
  2986. syntax_error(f->curr_token, "Expected a semicolon, got a comma");
  2987. advance_token(f);
  2988. return true;
  2989. }
  2990. if (token.kind == Token_CloseBrace) {
  2991. if (token.pos.line == f->prev_token.pos.line) {
  2992. return true;
  2993. }
  2994. }
  2995. expect_token_after(f, Token_Semicolon, "field list");
  2996. return false;
  2997. }
  2998. AstNode *parse_struct_field_list(AstFile *f, isize *name_count_) {
  2999. CommentGroup docs = f->lead_comment;
  3000. Token start_token = f->curr_token;
  3001. Array<AstNode *> decls = make_ast_node_array(f);
  3002. isize total_name_count = 0;
  3003. AstNode *params = parse_field_list(f, &total_name_count, FieldFlag_Struct, Token_CloseBrace, true, false);
  3004. if (name_count_) *name_count_ = total_name_count;
  3005. return params;
  3006. }
  3007. AstNode *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, TokenKind follow, bool allow_default_parameters, bool allow_type_token) {
  3008. TokenKind separator = Token_Comma;
  3009. Token start_token = f->curr_token;
  3010. CommentGroup docs = f->lead_comment;
  3011. Array<AstNode *> params = make_ast_node_array(f);
  3012. Array<AstNodeAndFlags> list = {}; array_init(&list, heap_allocator());
  3013. defer (array_free(&list));
  3014. isize total_name_count = 0;
  3015. bool allow_ellipsis = allowed_flags&FieldFlag_ellipsis;
  3016. bool seen_ellipsis = false;
  3017. while (f->curr_token.kind != follow &&
  3018. f->curr_token.kind != Token_Colon &&
  3019. f->curr_token.kind != Token_EOF) {
  3020. u32 flags = parse_field_prefixes(f);
  3021. AstNode *param = parse_var_type(f, allow_ellipsis, allow_type_token);
  3022. if (param->kind == AstNode_Ellipsis) {
  3023. if (seen_ellipsis) syntax_error(param, "Extra variadic parameter after ellipsis");
  3024. seen_ellipsis = true;
  3025. } else if (seen_ellipsis) {
  3026. syntax_error(param, "Extra parameter after ellipsis");
  3027. }
  3028. AstNodeAndFlags naf = {param, flags};
  3029. array_add(&list, naf);
  3030. if (!allow_token(f, Token_Comma)) {
  3031. break;
  3032. }
  3033. }
  3034. if (f->curr_token.kind == Token_Colon) {
  3035. Array<AstNode *> names = convert_to_ident_list(f, list, true); // Copy for semantic reasons
  3036. if (names.count == 0) {
  3037. syntax_error(f->curr_token, "Empty field declaration");
  3038. }
  3039. u32 set_flags = 0;
  3040. if (list.count > 0) {
  3041. set_flags = list[0].flags;
  3042. }
  3043. set_flags = check_field_prefixes(f, names.count, allowed_flags, set_flags);
  3044. total_name_count += names.count;
  3045. AstNode *type = nullptr;
  3046. AstNode *default_value = nullptr;
  3047. expect_token_after(f, Token_Colon, "field list");
  3048. if (f->curr_token.kind != Token_Eq) {
  3049. type = parse_var_type(f, allow_ellipsis, allow_type_token);
  3050. }
  3051. if (allow_token(f, Token_Eq)) {
  3052. // TODO(bill): Should this be true==lhs or false==rhs?
  3053. default_value = parse_expr(f, false);
  3054. if (!allow_default_parameters) {
  3055. syntax_error(f->curr_token, "Default parameters are only allowed for procedures");
  3056. }
  3057. }
  3058. if (default_value != nullptr && names.count > 1) {
  3059. syntax_error(f->curr_token, "Default parameters can only be applied to single values");
  3060. }
  3061. if (type != nullptr && type->kind == AstNode_Ellipsis) {
  3062. if (seen_ellipsis) syntax_error(type, "Extra variadic parameter after ellipsis");
  3063. seen_ellipsis = true;
  3064. if (names.count != 1) {
  3065. syntax_error(type, "Variadic parameters can only have one field name");
  3066. }
  3067. } else if (seen_ellipsis && default_value == nullptr) {
  3068. syntax_error(f->curr_token, "Extra parameter after ellipsis without a default value");
  3069. }
  3070. parse_expect_field_separator(f, type);
  3071. AstNode *param = ast_field(f, names, type, default_value, set_flags, docs, f->line_comment);
  3072. array_add(&params, param);
  3073. while (f->curr_token.kind != follow &&
  3074. f->curr_token.kind != Token_EOF) {
  3075. CommentGroup docs = f->lead_comment;
  3076. u32 set_flags = parse_field_prefixes(f);
  3077. Array<AstNode *> names = parse_ident_list(f);
  3078. if (names.count == 0) {
  3079. syntax_error(f->curr_token, "Empty field declaration");
  3080. break;
  3081. }
  3082. set_flags = check_field_prefixes(f, names.count, allowed_flags, set_flags);
  3083. total_name_count += names.count;
  3084. AstNode *type = nullptr;
  3085. AstNode *default_value = nullptr;
  3086. expect_token_after(f, Token_Colon, "field list");
  3087. if (f->curr_token.kind != Token_Eq) {
  3088. type = parse_var_type(f, allow_ellipsis, allow_type_token);
  3089. }
  3090. if (allow_token(f, Token_Eq)) {
  3091. // TODO(bill): Should this be true==lhs or false==rhs?
  3092. default_value = parse_expr(f, false);
  3093. if (!allow_default_parameters) {
  3094. syntax_error(f->curr_token, "Default parameters are only allowed for procedures");
  3095. }
  3096. }
  3097. if (default_value != nullptr && names.count > 1) {
  3098. syntax_error(f->curr_token, "Default parameters can only be applied to single values");
  3099. }
  3100. if (type != nullptr && type->kind == AstNode_Ellipsis) {
  3101. if (seen_ellipsis) syntax_error(type, "Extra variadic parameter after ellipsis");
  3102. seen_ellipsis = true;
  3103. if (names.count != 1) {
  3104. syntax_error(type, "Variadic parameters can only have one field name");
  3105. }
  3106. } else if (seen_ellipsis && default_value == nullptr) {
  3107. syntax_error(f->curr_token, "Extra parameter after ellipsis without a default value");
  3108. }
  3109. bool ok = parse_expect_field_separator(f, param);
  3110. AstNode *param = ast_field(f, names, type, default_value, set_flags, docs, f->line_comment);
  3111. array_add(&params, param);
  3112. if (!ok) {
  3113. break;
  3114. }
  3115. }
  3116. if (name_count_) *name_count_ = total_name_count;
  3117. return ast_field_list(f, start_token, params);
  3118. }
  3119. for_array(i, list) {
  3120. Array<AstNode *> names = {};
  3121. AstNode *type = list[i].node;
  3122. Token token = blank_token;
  3123. array_init_count(&names, heap_allocator(), 1);
  3124. token.pos = ast_node_token(type).pos;
  3125. names[0] = ast_ident(f, token);
  3126. u32 flags = check_field_prefixes(f, list.count, allowed_flags, list[i].flags);
  3127. AstNode *param = ast_field(f, names, list[i].node, nullptr, flags, docs, f->line_comment);
  3128. array_add(&params, param);
  3129. }
  3130. if (name_count_) *name_count_ = total_name_count;
  3131. return ast_field_list(f, start_token, params);
  3132. }
  3133. AstNode *parse_type_or_ident(AstFile *f) {
  3134. #if 1
  3135. bool prev_allow_type = f->allow_type;
  3136. isize prev_expr_level = f->expr_level;
  3137. defer (f->allow_type = prev_allow_type);
  3138. defer (f->expr_level = prev_expr_level);
  3139. f->allow_type = true;
  3140. f->expr_level = -1;
  3141. AstNode *operand = parse_operand(f, true);
  3142. AstNode *type = parse_atom_expr(f, operand, true);
  3143. return type;
  3144. #else
  3145. switch (f->curr_token.kind) {
  3146. case Token_Dollar: {
  3147. Token token = expect_token(f, Token_Dollar);
  3148. AstNode *type = parse_ident(f);
  3149. return ast_poly_type(f, token, type);
  3150. } break;
  3151. case Token_type_of: {
  3152. AstNode *i = ast_implicit(f, expect_token(f, Token_type_of));
  3153. AstNode *type = parse_call_expr(f, i);
  3154. while (f->curr_token.kind == Token_Period) {
  3155. Token token = advance_token(f);
  3156. AstNode *sel = parse_ident(f);
  3157. type = ast_selector_expr(f, token, type, sel);
  3158. }
  3159. return type;
  3160. } break;
  3161. case Token_Ident: {
  3162. AstNode *e = parse_ident(f);
  3163. while (f->curr_token.kind == Token_Period) {
  3164. Token token = advance_token(f);
  3165. AstNode *sel = parse_ident(f);
  3166. e = ast_selector_expr(f, token, e, sel);
  3167. }
  3168. // TODO(bill): Merge type_or_ident into the general parsing for expressions
  3169. // if (f->curr_token.kind == Token_OpenParen) {
  3170. // HACK NOTE(bill): For type_of_val(expr) et al.
  3171. // e = parse_call_expr(f, e);
  3172. // }
  3173. return e;
  3174. } break;
  3175. case Token_Pointer: {
  3176. Token token = expect_token(f, Token_Pointer);
  3177. AstNode *elem = parse_type(f);
  3178. return ast_pointer_type(f, token, elem);
  3179. } break;
  3180. case Token_atomic: {
  3181. Token token = expect_token(f, Token_atomic);
  3182. AstNode *elem = parse_type(f);
  3183. return ast_atomic_type(f, token, elem);
  3184. } break;
  3185. case Token_Hash: {
  3186. Token hash_token = expect_token(f, Token_Hash);
  3187. Token type_token = expect_token(f, Token_type);
  3188. AstNode *type = parse_type(f);
  3189. return ast_helper_type(f, hash_token, type);
  3190. }
  3191. case Token_OpenBracket: {
  3192. Token token = expect_token(f, Token_OpenBracket);
  3193. AstNode *count_expr = nullptr;
  3194. bool is_vector = false;
  3195. if (f->curr_token.kind == Token_Ellipsis) {
  3196. count_expr = ast_unary_expr(f, expect_token(f, Token_Ellipsis), nullptr);
  3197. } else if (allow_token(f, Token_vector)) {
  3198. if (f->curr_token.kind != Token_CloseBracket) {
  3199. f->expr_level++;
  3200. count_expr = parse_expr(f, false);
  3201. f->expr_level--;
  3202. } else {
  3203. syntax_error(f->curr_token, "Vector type missing count");
  3204. }
  3205. is_vector = true;
  3206. } else if (allow_token(f, Token_dynamic)) {
  3207. expect_token(f, Token_CloseBracket);
  3208. return ast_dynamic_array_type(f, token, parse_type(f));
  3209. } else if (f->curr_token.kind != Token_CloseBracket) {
  3210. f->expr_level++;
  3211. count_expr = parse_expr(f, false);
  3212. f->expr_level--;
  3213. }
  3214. expect_token(f, Token_CloseBracket);
  3215. if (is_vector) {
  3216. return ast_vector_type(f, token, count_expr, parse_type(f));
  3217. }
  3218. return ast_array_type(f, token, count_expr, parse_type(f));
  3219. } break;
  3220. case Token_map: {
  3221. Token token = expect_token(f, Token_map);
  3222. AstNode *count = nullptr;
  3223. AstNode *key = nullptr;
  3224. AstNode *value = nullptr;
  3225. Token open = expect_token_after(f, Token_OpenBracket, "map");
  3226. key = parse_expr(f, true);
  3227. if (allow_token(f, Token_Comma)) {
  3228. count = key;
  3229. key = parse_type(f);
  3230. }
  3231. Token close = expect_token(f, Token_CloseBracket);
  3232. value = parse_type(f);
  3233. return ast_map_type(f, token, count, key, value);
  3234. } break;
  3235. case Token_struct: {
  3236. Token token = expect_token(f, Token_struct);
  3237. bool is_packed = false;
  3238. bool is_ordered = false;
  3239. AstNode *align = nullptr;
  3240. isize prev_level = f->expr_level;
  3241. f->expr_level = -1;
  3242. while (allow_token(f, Token_Hash)) {
  3243. Token tag = expect_token_after(f, Token_Ident, "#");
  3244. if (tag.string == "packed") {
  3245. if (is_packed) {
  3246. syntax_error(tag, "Duplicate struct tag '#%.*s'", LIT(tag.string));
  3247. }
  3248. is_packed = true;
  3249. } else if (tag.string == "ordered") {
  3250. if (is_ordered) {
  3251. syntax_error(tag, "Duplicate struct tag '#%.*s'", LIT(tag.string));
  3252. }
  3253. is_ordered = true;
  3254. } else if (tag.string == "align") {
  3255. if (align) {
  3256. syntax_error(tag, "Duplicate struct tag '#%.*s'", LIT(tag.string));
  3257. }
  3258. align = parse_expr(f, true);
  3259. } else {
  3260. syntax_error(tag, "Invalid struct tag '#%.*s'", LIT(tag.string));
  3261. }
  3262. }
  3263. f->expr_level = prev_level;
  3264. if (is_packed && is_ordered) {
  3265. syntax_error(token, "'#ordered' is not needed with '#packed' which implies ordering");
  3266. }
  3267. Token open = expect_token_after(f, Token_OpenBrace, "struct");
  3268. isize name_count = 0;
  3269. AstNode *fields = parse_struct_field_list(f, &name_count);
  3270. Token close = expect_token(f, Token_CloseBrace);
  3271. Array<AstNode *> decls = {};
  3272. if (fields != nullptr) {
  3273. GB_ASSERT(fields->kind == AstNode_FieldList);
  3274. decls = fields->FieldList.list;
  3275. }
  3276. return ast_struct_type(f, token, decls, name_count, is_packed, is_ordered, align);
  3277. } break;
  3278. case Token_union: {
  3279. Token token = expect_token(f, Token_union);
  3280. Token open = expect_token_after(f, Token_OpenBrace, "union");
  3281. Array<AstNode *> variants = make_ast_node_array(f);
  3282. isize total_decl_name_count = 0;
  3283. CommentGroup docs = f->lead_comment;
  3284. Token start_token = f->curr_token;
  3285. while (f->curr_token.kind != Token_CloseBrace &&
  3286. f->curr_token.kind != Token_EOF) {
  3287. AstNode *type = parse_type(f);
  3288. if (type->kind != AstNode_BadExpr) {
  3289. array_add(&variants, type);
  3290. }
  3291. if (!allow_token(f, Token_Comma)) {
  3292. break;
  3293. }
  3294. }
  3295. Token close = expect_token(f, Token_CloseBrace);
  3296. return ast_union_type(f, token, variants);
  3297. } break;
  3298. case Token_raw_union: {
  3299. Token token = expect_token(f, Token_raw_union);
  3300. Token open = expect_token_after(f, Token_OpenBrace, "raw_union");
  3301. isize decl_count = 0;
  3302. AstNode *fields = parse_struct_field_list(f, &decl_count);
  3303. Token close = expect_token(f, Token_CloseBrace);
  3304. Array<AstNode *> decls = {};
  3305. if (fields != nullptr) {
  3306. GB_ASSERT(fields->kind == AstNode_FieldList);
  3307. decls = fields->FieldList.list;
  3308. }
  3309. return ast_raw_union_type(f, token, decls, decl_count);
  3310. } break;
  3311. case Token_enum: {
  3312. Token token = expect_token(f, Token_enum);
  3313. AstNode *base_type = nullptr;
  3314. if (f->curr_token.kind != Token_OpenBrace) {
  3315. base_type = parse_type(f);
  3316. }
  3317. Token open = expect_token(f, Token_OpenBrace);
  3318. Array<AstNode *> values = parse_element_list(f);
  3319. Token close = expect_token(f, Token_CloseBrace);
  3320. return ast_enum_type(f, token, base_type, values);
  3321. } break;
  3322. case Token_bit_field: {
  3323. Token token = expect_token(f, Token_bit_field);
  3324. Array<AstNode *> fields = make_ast_node_array(f);
  3325. AstNode *align = nullptr;
  3326. Token open, close;
  3327. isize prev_level = f->expr_level;
  3328. f->expr_level = -1;
  3329. while (allow_token(f, Token_Hash)) {
  3330. Token tag = expect_token_after(f, Token_Ident, "#");
  3331. if (tag.string == "align") {
  3332. if (align) {
  3333. syntax_error(tag, "Duplicate bit_field tag '#%.*s'", LIT(tag.string));
  3334. }
  3335. align = parse_expr(f, true);
  3336. } else {
  3337. syntax_error(tag, "Invalid bit_field tag '#%.*s'", LIT(tag.string));
  3338. }
  3339. }
  3340. f->expr_level = prev_level;
  3341. open = expect_token_after(f, Token_OpenBrace, "bit_field");
  3342. while (f->curr_token.kind != Token_EOF &&
  3343. f->curr_token.kind != Token_CloseBrace) {
  3344. AstNode *name = parse_ident(f);
  3345. Token colon = expect_token(f, Token_Colon);
  3346. AstNode *value = parse_expr(f, true);
  3347. AstNode *field = ast_field_value(f, name, value, colon);
  3348. array_add(&fields, field);
  3349. if (f->curr_token.kind != Token_Comma) {
  3350. break;
  3351. }
  3352. advance_token(f);
  3353. }
  3354. close = expect_token(f, Token_CloseBrace);
  3355. return ast_bit_field_type(f, token, fields, align);
  3356. } break;
  3357. case Token_proc: {
  3358. Token token = advance_token(f);
  3359. AstNode *pt = parse_proc_type(f, token, nullptr);
  3360. if (pt->ProcType.tags != 0) {
  3361. syntax_error(token, "A procedure type cannot have tags");
  3362. }
  3363. return pt;
  3364. } break;
  3365. case Token_OpenParen: {
  3366. Token open = expect_token(f, Token_OpenParen);
  3367. AstNode *type = parse_type(f);
  3368. Token close = expect_token(f, Token_CloseParen);
  3369. return ast_paren_expr(f, type, open, close);
  3370. } break;
  3371. }
  3372. return nullptr;
  3373. #endif
  3374. }
  3375. AstNode *parse_body(AstFile *f) {
  3376. Array<AstNode *> stmts = {};
  3377. Token open, close;
  3378. isize prev_expr_level = f->expr_level;
  3379. // NOTE(bill): The body may be within an expression so reset to zero
  3380. f->expr_level = 0;
  3381. open = expect_token(f, Token_OpenBrace);
  3382. stmts = parse_stmt_list(f);
  3383. close = expect_token(f, Token_CloseBrace);
  3384. f->expr_level = prev_expr_level;
  3385. return ast_block_stmt(f, stmts, open, close);
  3386. }
  3387. AstNode *parse_if_stmt(AstFile *f) {
  3388. if (f->curr_proc == nullptr) {
  3389. syntax_error(f->curr_token, "You cannot use an if statement in the file scope");
  3390. return ast_bad_stmt(f, f->curr_token, f->curr_token);
  3391. }
  3392. Token token = expect_token(f, Token_if);
  3393. AstNode *init = nullptr;
  3394. AstNode *cond = nullptr;
  3395. AstNode *body = nullptr;
  3396. AstNode *else_stmt = nullptr;
  3397. isize prev_level = f->expr_level;
  3398. f->expr_level = -1;
  3399. if (allow_token(f, Token_Semicolon)) {
  3400. cond = parse_expr(f, false);
  3401. } else {
  3402. init = parse_simple_stmt(f, StmtAllowFlag_None);
  3403. if (allow_token(f, Token_Semicolon)) {
  3404. cond = parse_expr(f, false);
  3405. } else {
  3406. cond = convert_stmt_to_expr(f, init, str_lit("boolean expression"));
  3407. init = nullptr;
  3408. }
  3409. }
  3410. f->expr_level = prev_level;
  3411. if (cond == nullptr) {
  3412. syntax_error(f->curr_token, "Expected condition for if statement");
  3413. }
  3414. if (allow_token(f, Token_do)) {
  3415. body = convert_stmt_to_body(f, parse_stmt(f));
  3416. } else {
  3417. body = parse_block_stmt(f, false);
  3418. }
  3419. if (allow_token(f, Token_else)) {
  3420. switch (f->curr_token.kind) {
  3421. case Token_if:
  3422. else_stmt = parse_if_stmt(f);
  3423. break;
  3424. case Token_OpenBrace:
  3425. else_stmt = parse_block_stmt(f, false);
  3426. break;
  3427. case Token_do: {
  3428. Token arrow = expect_token(f, Token_do);
  3429. else_stmt = convert_stmt_to_body(f, parse_stmt(f));
  3430. } break;
  3431. default:
  3432. syntax_error(f->curr_token, "Expected if statement block statement");
  3433. else_stmt = ast_bad_stmt(f, f->curr_token, f->tokens[f->curr_token_index+1]);
  3434. break;
  3435. }
  3436. }
  3437. return ast_if_stmt(f, token, init, cond, body, else_stmt);
  3438. }
  3439. AstNode *parse_when_stmt(AstFile *f) {
  3440. Token token = expect_token(f, Token_when);
  3441. AstNode *cond = nullptr;
  3442. AstNode *body = nullptr;
  3443. AstNode *else_stmt = nullptr;
  3444. isize prev_level = f->expr_level;
  3445. isize when_level = f->when_level;
  3446. defer (f->when_level = when_level);
  3447. f->expr_level = -1;
  3448. f->when_level += 1;
  3449. cond = parse_expr(f, false);
  3450. f->expr_level = prev_level;
  3451. if (cond == nullptr) {
  3452. syntax_error(f->curr_token, "Expected condition for when statement");
  3453. }
  3454. if (allow_token(f, Token_do)) {
  3455. body = convert_stmt_to_body(f, parse_stmt(f));
  3456. } else {
  3457. body = parse_block_stmt(f, true);
  3458. }
  3459. if (allow_token(f, Token_else)) {
  3460. switch (f->curr_token.kind) {
  3461. case Token_when:
  3462. else_stmt = parse_when_stmt(f);
  3463. break;
  3464. case Token_OpenBrace:
  3465. else_stmt = parse_block_stmt(f, true);
  3466. break;
  3467. case Token_do: {
  3468. Token arrow = expect_token(f, Token_do);
  3469. body = convert_stmt_to_body(f, parse_stmt(f));
  3470. } break;
  3471. default:
  3472. syntax_error(f->curr_token, "Expected when statement block statement");
  3473. else_stmt = ast_bad_stmt(f, f->curr_token, f->tokens[f->curr_token_index+1]);
  3474. break;
  3475. }
  3476. }
  3477. // if (f->curr_proc == nullptr && f->when_level > 1) {
  3478. // syntax_error(token, "Nested when statements are not currently supported at the file scope");
  3479. // return ast_bad_stmt(f, token, f->curr_token);
  3480. // }
  3481. return ast_when_stmt(f, token, cond, body, else_stmt);
  3482. }
  3483. AstNode *parse_return_stmt(AstFile *f) {
  3484. if (f->curr_proc == nullptr) {
  3485. syntax_error(f->curr_token, "You cannot use a return statement in the file scope");
  3486. return ast_bad_stmt(f, f->curr_token, f->curr_token);
  3487. }
  3488. if (f->expr_level > 0) {
  3489. syntax_error(f->curr_token, "You cannot use a return statement within an expression");
  3490. return ast_bad_stmt(f, f->curr_token, f->curr_token);
  3491. }
  3492. Token token = expect_token(f, Token_return);
  3493. Array<AstNode *> results = make_ast_node_array(f);
  3494. while (f->curr_token.kind != Token_Semicolon) {
  3495. AstNode *arg = parse_expr(f, false);
  3496. if (f->curr_token.kind == Token_Eq) {
  3497. Token eq = expect_token(f, Token_Eq);
  3498. AstNode *value = parse_value(f);
  3499. arg = ast_field_value(f, arg, value, eq);
  3500. }
  3501. array_add(&results, arg);
  3502. if (f->curr_token.kind != Token_Comma ||
  3503. f->curr_token.kind == Token_EOF) {
  3504. break;
  3505. }
  3506. advance_token(f);
  3507. }
  3508. AstNode *end = nullptr;
  3509. if (results.count > 0) {
  3510. end = results[results.count-1];
  3511. }
  3512. expect_semicolon(f, end);
  3513. return ast_return_stmt(f, token, results);
  3514. }
  3515. // AstNode *parse_give_stmt(AstFile *f) {
  3516. // if (f->curr_proc == nullptr) {
  3517. // syntax_error(f->curr_token, "You cannot use a give statement in the file scope");
  3518. // return ast_bad_stmt(f, f->curr_token, f->curr_token);
  3519. // }
  3520. // if (f->expr_level == 0) {
  3521. // syntax_error(f->curr_token, "A give statement must be used within an expression");
  3522. // return ast_bad_stmt(f, f->curr_token, f->curr_token);
  3523. // }
  3524. // Token token = expect_token(f, Token_give);
  3525. // Array<AstNode *> results;
  3526. // if (f->curr_token.kind != Token_Semicolon && f->curr_token.kind != Token_CloseBrace) {
  3527. // results = parse_rhs_expr_list(f);
  3528. // } else {
  3529. // results = make_ast_node_array(f);
  3530. // }
  3531. // AstNode *ge = ast_give_expr(f, token, results);
  3532. // expect_semicolon(f, ge);
  3533. // return ast_expr_stmt(f, ge);
  3534. // }
  3535. AstNode *parse_for_stmt(AstFile *f) {
  3536. if (f->curr_proc == nullptr) {
  3537. syntax_error(f->curr_token, "You cannot use a for statement in the file scope");
  3538. return ast_bad_stmt(f, f->curr_token, f->curr_token);
  3539. }
  3540. Token token = expect_token(f, Token_for);
  3541. AstNode *init = nullptr;
  3542. AstNode *cond = nullptr;
  3543. AstNode *post = nullptr;
  3544. AstNode *body = nullptr;
  3545. bool is_range = false;
  3546. if (f->curr_token.kind != Token_OpenBrace &&
  3547. f->curr_token.kind != Token_do) {
  3548. isize prev_level = f->expr_level;
  3549. defer (f->expr_level = prev_level);
  3550. f->expr_level = -1;
  3551. if (f->curr_token.kind == Token_in) {
  3552. Token in_token = expect_token(f, Token_in);
  3553. AstNode *rhs = nullptr;
  3554. bool prev_allow_range = f->allow_range;
  3555. f->allow_range = true;
  3556. rhs = parse_expr(f, false);
  3557. f->allow_range = prev_allow_range;
  3558. if (allow_token(f, Token_do)) {
  3559. body = convert_stmt_to_body(f, parse_stmt(f));
  3560. } else {
  3561. body = parse_block_stmt(f, false);
  3562. }
  3563. return ast_range_stmt(f, token, nullptr, nullptr, in_token, rhs, body);
  3564. }
  3565. if (f->curr_token.kind != Token_Semicolon) {
  3566. cond = parse_simple_stmt(f, StmtAllowFlag_In);
  3567. if (cond->kind == AstNode_AssignStmt && cond->AssignStmt.op.kind == Token_in) {
  3568. is_range = true;
  3569. }
  3570. }
  3571. if (!is_range && allow_token(f, Token_Semicolon)) {
  3572. init = cond;
  3573. cond = nullptr;
  3574. if (f->curr_token.kind != Token_Semicolon) {
  3575. cond = parse_simple_stmt(f, StmtAllowFlag_None);
  3576. }
  3577. expect_semicolon(f, cond);
  3578. if (f->curr_token.kind != Token_OpenBrace &&
  3579. f->curr_token.kind != Token_do) {
  3580. post = parse_simple_stmt(f, StmtAllowFlag_None);
  3581. }
  3582. }
  3583. }
  3584. if (allow_token(f, Token_do)) {
  3585. body = convert_stmt_to_body(f, parse_stmt(f));
  3586. } else {
  3587. body = parse_block_stmt(f, false);
  3588. }
  3589. if (is_range) {
  3590. GB_ASSERT(cond->kind == AstNode_AssignStmt);
  3591. Token in_token = cond->AssignStmt.op;
  3592. AstNode *value = nullptr;
  3593. AstNode *index = nullptr;
  3594. switch (cond->AssignStmt.lhs.count) {
  3595. case 1:
  3596. value = cond->AssignStmt.lhs[0];
  3597. break;
  3598. case 2:
  3599. value = cond->AssignStmt.lhs[0];
  3600. index = cond->AssignStmt.lhs[1];
  3601. break;
  3602. default:
  3603. syntax_error(cond, "Expected either 1 or 2 identifiers");
  3604. return ast_bad_stmt(f, token, f->curr_token);
  3605. }
  3606. AstNode *rhs = nullptr;
  3607. if (cond->AssignStmt.rhs.count > 0) {
  3608. rhs = cond->AssignStmt.rhs[0];
  3609. }
  3610. return ast_range_stmt(f, token, value, index, in_token, rhs, body);
  3611. }
  3612. cond = convert_stmt_to_expr(f, cond, str_lit("boolean expression"));
  3613. return ast_for_stmt(f, token, init, cond, post, body);
  3614. }
  3615. AstNode *parse_case_clause(AstFile *f, bool is_type) {
  3616. Token token = f->curr_token;
  3617. Array<AstNode *> list = {};
  3618. expect_token(f, Token_case);
  3619. bool prev_allow_range = f->allow_range;
  3620. f->allow_range = !is_type;
  3621. if (f->curr_token.kind != Token_Colon) {
  3622. list = parse_rhs_expr_list(f);
  3623. }
  3624. f->allow_range = prev_allow_range;
  3625. expect_token(f, Token_Colon); // TODO(bill): Is this the best syntax?
  3626. Array<AstNode *> stmts = parse_stmt_list(f);
  3627. return ast_case_clause(f, token, list, stmts);
  3628. }
  3629. AstNode *parse_switch_stmt(AstFile *f) {
  3630. if (f->curr_proc == nullptr) {
  3631. syntax_error(f->curr_token, "You cannot use a match statement in the file scope");
  3632. return ast_bad_stmt(f, f->curr_token, f->curr_token);
  3633. }
  3634. Token token = expect_token(f, Token_switch);
  3635. AstNode *init = nullptr;
  3636. AstNode *tag = nullptr;
  3637. AstNode *body = nullptr;
  3638. Token open, close;
  3639. bool is_type_match = false;
  3640. Array<AstNode *> list = make_ast_node_array(f);
  3641. if (f->curr_token.kind != Token_OpenBrace) {
  3642. isize prev_level = f->expr_level;
  3643. f->expr_level = -1;
  3644. defer (f->expr_level = prev_level);
  3645. if (allow_token(f, Token_in)) {
  3646. Array<AstNode *> lhs = {};
  3647. Array<AstNode *> rhs = make_ast_node_array(f, 1);
  3648. array_add(&rhs, parse_expr(f, false));
  3649. tag = ast_assign_stmt(f, token, lhs, rhs);
  3650. is_type_match = true;
  3651. } else {
  3652. tag = parse_simple_stmt(f, StmtAllowFlag_In);
  3653. if (tag->kind == AstNode_AssignStmt && tag->AssignStmt.op.kind == Token_in) {
  3654. is_type_match = true;
  3655. } else {
  3656. if (allow_token(f, Token_Semicolon)) {
  3657. init = tag;
  3658. tag = nullptr;
  3659. if (f->curr_token.kind != Token_OpenBrace) {
  3660. tag = parse_simple_stmt(f, StmtAllowFlag_None);
  3661. }
  3662. }
  3663. }
  3664. }
  3665. }
  3666. open = expect_token(f, Token_OpenBrace);
  3667. while (f->curr_token.kind == Token_case) {
  3668. array_add(&list, parse_case_clause(f, is_type_match));
  3669. }
  3670. close = expect_token(f, Token_CloseBrace);
  3671. body = ast_block_stmt(f, list, open, close);
  3672. if (!is_type_match) {
  3673. tag = convert_stmt_to_expr(f, tag, str_lit("match expression"));
  3674. return ast_switch_stmt(f, token, init, tag, body);
  3675. } else {
  3676. return ast_type_switch_stmt(f, token, tag, body);
  3677. }
  3678. }
  3679. AstNode *parse_defer_stmt(AstFile *f) {
  3680. if (f->curr_proc == nullptr) {
  3681. syntax_error(f->curr_token, "You cannot use a defer statement in the file scope");
  3682. return ast_bad_stmt(f, f->curr_token, f->curr_token);
  3683. }
  3684. Token token = expect_token(f, Token_defer);
  3685. AstNode *stmt = parse_stmt(f);
  3686. switch (stmt->kind) {
  3687. case AstNode_EmptyStmt:
  3688. syntax_error(token, "Empty statement after defer (e.g. ';')");
  3689. break;
  3690. case AstNode_DeferStmt:
  3691. syntax_error(token, "You cannot defer a defer statement");
  3692. stmt = stmt->DeferStmt.stmt;
  3693. break;
  3694. case AstNode_ReturnStmt:
  3695. syntax_error(token, "You cannot a return statement");
  3696. break;
  3697. }
  3698. return ast_defer_stmt(f, token, stmt);
  3699. }
  3700. AstNode *parse_asm_stmt(AstFile *f) {
  3701. Token token = expect_token(f, Token_asm);
  3702. bool is_volatile = false;
  3703. Token open, close, code_string;
  3704. open = expect_token(f, Token_OpenBrace);
  3705. code_string = expect_token(f, Token_String);
  3706. AstNode *output_list = nullptr;
  3707. AstNode *input_list = nullptr;
  3708. AstNode *clobber_list = nullptr;
  3709. isize output_count = 0;
  3710. isize input_count = 0;
  3711. isize clobber_count = 0;
  3712. // TODO(bill): Finish asm statement and determine syntax
  3713. // if (f->curr_token.kind != Token_CloseBrace) {
  3714. // expect_token(f, Token_Colon);
  3715. // }
  3716. close = expect_token(f, Token_CloseBrace);
  3717. return ast_asm_stmt(f, token, is_volatile, open, close, code_string,
  3718. output_list, input_list, clobber_list,
  3719. output_count, input_count, clobber_count);
  3720. }
  3721. AstNode *parse_import_decl(AstFile *f, bool is_using) {
  3722. CommentGroup docs = f->lead_comment;
  3723. Token token = expect_token(f, Token_import);
  3724. Token import_name = {};
  3725. switch (f->curr_token.kind) {
  3726. case Token_Ident:
  3727. import_name = advance_token(f);
  3728. break;
  3729. default:
  3730. import_name.pos = f->curr_token.pos;
  3731. break;
  3732. }
  3733. if (!is_using && is_blank_ident(import_name)) {
  3734. syntax_error(import_name, "Illegal import name: '_'");
  3735. }
  3736. Token file_path = expect_token_after(f, Token_String, "import");
  3737. AstNode *s = nullptr;
  3738. if (f->curr_proc != nullptr) {
  3739. syntax_error(import_name, "You cannot use 'import' within a procedure. This must be done at the file scope");
  3740. s = ast_bad_decl(f, import_name, file_path);
  3741. } else {
  3742. s = ast_import_decl(f, token, is_using, file_path, import_name, docs, f->line_comment);
  3743. array_add(&f->imports_and_exports, s);
  3744. }
  3745. expect_semicolon(f, s);
  3746. return s;
  3747. }
  3748. AstNode *parse_export_decl(AstFile *f) {
  3749. CommentGroup docs = f->lead_comment;
  3750. Token token = expect_token(f, Token_export);
  3751. Token file_path = expect_token_after(f, Token_String, "export");
  3752. AstNode *s = nullptr;
  3753. if (f->curr_proc != nullptr) {
  3754. syntax_error(token, "You cannot use 'export' within a procedure. This must be done at the file scope");
  3755. s = ast_bad_decl(f, token, file_path);
  3756. } else {
  3757. s = ast_export_decl(f, token, file_path, docs, f->line_comment);
  3758. array_add(&f->imports_and_exports, s);
  3759. }
  3760. expect_semicolon(f, s);
  3761. return s;
  3762. }
  3763. AstNode *parse_foreign_decl(AstFile *f) {
  3764. CommentGroup docs = f->lead_comment;
  3765. Token token = expect_token(f, Token_foreign);
  3766. switch (f->curr_token.kind) {
  3767. case Token_export:
  3768. case Token_Ident:
  3769. return parse_foreign_block(f, token);
  3770. case Token_import: {
  3771. Token import_token = expect_token(f, Token_import);
  3772. Token lib_name = {};
  3773. switch (f->curr_token.kind) {
  3774. case Token_Ident:
  3775. lib_name = advance_token(f);
  3776. break;
  3777. default:
  3778. lib_name.pos = token.pos;
  3779. break;
  3780. }
  3781. if (is_blank_ident(lib_name)) {
  3782. syntax_error(lib_name, "Illegal foreign_library name: '_'");
  3783. }
  3784. Token file_path = expect_token(f, Token_String);
  3785. AstNode *s = nullptr;
  3786. if (f->curr_proc != nullptr) {
  3787. syntax_error(lib_name, "You cannot use foreign_library within a procedure. This must be done at the file scope");
  3788. s = ast_bad_decl(f, lib_name, file_path);
  3789. } else {
  3790. s = ast_foreign_import_decl(f, token, file_path, lib_name, docs, f->line_comment);
  3791. }
  3792. expect_semicolon(f, s);
  3793. return s;
  3794. }
  3795. }
  3796. syntax_error(token, "Invalid foreign declaration");
  3797. return ast_bad_decl(f, token, f->curr_token);
  3798. }
  3799. AstNode *parse_stmt(AstFile *f) {
  3800. AstNode *s = nullptr;
  3801. Token token = f->curr_token;
  3802. switch (token.kind) {
  3803. // Operands
  3804. case Token_context:
  3805. if (look_ahead_token_kind(f, 1) == Token_ArrowLeft) {
  3806. advance_token(f);
  3807. Token arrow = expect_token(f, Token_ArrowLeft);
  3808. AstNode *body = nullptr;
  3809. isize prev_level = f->expr_level;
  3810. f->expr_level = -1;
  3811. AstNode *expr = parse_expr(f, false);
  3812. f->expr_level = prev_level;
  3813. if (allow_token(f, Token_do)) {
  3814. body = convert_stmt_to_body(f, parse_stmt(f));
  3815. } else {
  3816. body = parse_block_stmt(f, false);
  3817. }
  3818. return ast_push_context(f, token, expr, body);
  3819. }
  3820. /*fallthrough*/
  3821. case Token_Ident:
  3822. case Token_Integer:
  3823. case Token_Float:
  3824. case Token_Imag:
  3825. case Token_Rune:
  3826. case Token_String:
  3827. case Token_OpenParen:
  3828. case Token_Pointer:
  3829. // Unary Operators
  3830. case Token_Add:
  3831. case Token_Sub:
  3832. case Token_Xor:
  3833. case Token_Not:
  3834. case Token_And:
  3835. s = parse_simple_stmt(f, StmtAllowFlag_Label);
  3836. expect_semicolon(f, s);
  3837. return s;
  3838. case Token_foreign:
  3839. return parse_foreign_decl(f);
  3840. case Token_import:
  3841. return parse_import_decl(f, false);
  3842. case Token_export:
  3843. return parse_export_decl(f);
  3844. case Token_if: return parse_if_stmt(f);
  3845. case Token_when: return parse_when_stmt(f);
  3846. case Token_for: return parse_for_stmt(f);
  3847. case Token_switch: return parse_switch_stmt(f);
  3848. case Token_defer: return parse_defer_stmt(f);
  3849. case Token_return: return parse_return_stmt(f);
  3850. case Token_asm: return parse_asm_stmt(f);
  3851. case Token_break:
  3852. case Token_continue:
  3853. case Token_fallthrough: {
  3854. Token token = advance_token(f);
  3855. AstNode *label = nullptr;
  3856. if (token.kind != Token_fallthrough &&
  3857. f->curr_token.kind == Token_Ident) {
  3858. label = parse_ident(f);
  3859. }
  3860. s = ast_branch_stmt(f, token, label);
  3861. expect_semicolon(f, s);
  3862. return s;
  3863. }
  3864. case Token_using: {
  3865. CommentGroup docs = f->lead_comment;
  3866. Token token = expect_token(f, Token_using);
  3867. if (f->curr_token.kind == Token_import) {
  3868. return parse_import_decl(f, true);
  3869. }
  3870. AstNode *decl = nullptr;
  3871. Array<AstNode *> list = parse_lhs_expr_list(f);
  3872. if (list.count == 0) {
  3873. syntax_error(token, "Illegal use of 'using' statement");
  3874. expect_semicolon(f, nullptr);
  3875. return ast_bad_stmt(f, token, f->curr_token);
  3876. }
  3877. if (f->curr_token.kind != Token_Colon) {
  3878. expect_semicolon(f, list[list.count-1]);
  3879. return ast_using_stmt(f, token, list);
  3880. }
  3881. decl = parse_value_decl(f, list, docs);
  3882. if (decl != nullptr && decl->kind == AstNode_ValueDecl) {
  3883. if (!decl->ValueDecl.is_mutable) {
  3884. syntax_error(token, "'using' may only be applied to variable declarations");
  3885. return decl;
  3886. }
  3887. decl->ValueDecl.is_using = true;
  3888. return decl;
  3889. }
  3890. syntax_error(token, "Illegal use of 'using' statement");
  3891. return ast_bad_stmt(f, token, f->curr_token);
  3892. } break;
  3893. case Token_At: {
  3894. advance_token(f);
  3895. Array<AstNode *> elems = {};
  3896. Token open = expect_token(f, Token_OpenParen);
  3897. f->expr_level++;
  3898. if (f->curr_token.kind != Token_CloseParen) {
  3899. elems = make_ast_node_array(f);
  3900. while (f->curr_token.kind != Token_CloseParen &&
  3901. f->curr_token.kind != Token_EOF) {
  3902. AstNode *elem = parse_ident(f);
  3903. if (f->curr_token.kind == Token_Eq) {
  3904. Token eq = expect_token(f, Token_Eq);
  3905. AstNode *value = parse_value(f);
  3906. elem = ast_field_value(f, elem, value, eq);
  3907. }
  3908. array_add(&elems, elem);
  3909. if (!allow_token(f, Token_Comma)) {
  3910. break;
  3911. }
  3912. }
  3913. }
  3914. f->expr_level--;
  3915. Token close = expect_closing(f, Token_CloseParen, str_lit("attribute"));
  3916. AstNode *attribute = ast_attribute(f, token, open, close, elems);
  3917. AstNode *decl = parse_stmt(f);
  3918. if (decl->kind == AstNode_ValueDecl) {
  3919. array_add(&decl->ValueDecl.attributes, attribute);
  3920. } else if (decl->kind == AstNode_ForeignBlockDecl) {
  3921. array_add(&decl->ForeignBlockDecl.attributes, attribute);
  3922. } else {
  3923. syntax_error(decl, "Expected a value or foreign declaration after an attribute, got %.*s", LIT(ast_node_strings[decl->kind]));
  3924. return ast_bad_stmt(f, token, f->curr_token);
  3925. }
  3926. return decl;
  3927. }
  3928. case Token_Hash: {
  3929. AstNode *s = nullptr;
  3930. Token hash_token = expect_token(f, Token_Hash);
  3931. Token name = expect_token(f, Token_Ident);
  3932. String tag = name.string;
  3933. if (tag == "shared_global_scope") {
  3934. if (f->curr_proc == nullptr) {
  3935. f->is_global_scope = true;
  3936. s = ast_empty_stmt(f, f->curr_token);
  3937. } else {
  3938. syntax_error(token, "You cannot use #shared_global_scope within a procedure. This must be done at the file scope");
  3939. s = ast_bad_decl(f, token, f->curr_token);
  3940. }
  3941. expect_semicolon(f, s);
  3942. return s;
  3943. } else if (tag == "bounds_check") {
  3944. s = parse_stmt(f);
  3945. s->stmt_state_flags |= StmtStateFlag_bounds_check;
  3946. if ((s->stmt_state_flags & StmtStateFlag_no_bounds_check) != 0) {
  3947. syntax_error(token, "#bounds_check and #no_bounds_check cannot be applied together");
  3948. }
  3949. return s;
  3950. } else if (tag == "no_bounds_check") {
  3951. s = parse_stmt(f);
  3952. s->stmt_state_flags |= StmtStateFlag_no_bounds_check;
  3953. if ((s->stmt_state_flags & StmtStateFlag_bounds_check) != 0) {
  3954. syntax_error(token, "#bounds_check and #no_bounds_check cannot be applied together");
  3955. }
  3956. return s;
  3957. }
  3958. if (tag == "include") {
  3959. syntax_error(token, "#include is not a valid import declaration kind. Did you mean 'import'?");
  3960. s = ast_bad_stmt(f, token, f->curr_token);
  3961. } else {
  3962. syntax_error(token, "Unknown tag directive used: '%.*s'", LIT(tag));
  3963. s = ast_bad_stmt(f, token, f->curr_token);
  3964. }
  3965. fix_advance_to_next_stmt(f);
  3966. return s;
  3967. } break;
  3968. case Token_OpenBrace:
  3969. return parse_block_stmt(f, false);
  3970. case Token_Semicolon:
  3971. s = ast_empty_stmt(f, token);
  3972. advance_token(f);
  3973. return s;
  3974. }
  3975. syntax_error(token,
  3976. "Expected a statement, got '%.*s'",
  3977. LIT(token_strings[token.kind]));
  3978. fix_advance_to_next_stmt(f);
  3979. return ast_bad_stmt(f, token, f->curr_token);
  3980. }
  3981. Array<AstNode *> parse_stmt_list(AstFile *f) {
  3982. Array<AstNode *> list = make_ast_node_array(f);
  3983. while (f->curr_token.kind != Token_case &&
  3984. f->curr_token.kind != Token_CloseBrace &&
  3985. f->curr_token.kind != Token_EOF) {
  3986. AstNode *stmt = parse_stmt(f);
  3987. if (stmt && stmt->kind != AstNode_EmptyStmt) {
  3988. array_add(&list, stmt);
  3989. if (stmt->kind == AstNode_ExprStmt &&
  3990. stmt->ExprStmt.expr != nullptr &&
  3991. stmt->ExprStmt.expr->kind == AstNode_ProcLit) {
  3992. syntax_error(stmt, "Procedure literal evaluated but not used");
  3993. }
  3994. }
  3995. }
  3996. return list;
  3997. }
  3998. ParseFileError init_ast_file(AstFile *f, String fullpath, TokenPos *err_pos) {
  3999. f->fullpath = string_trim_whitespace(fullpath); // Just in case
  4000. if (!string_ends_with(f->fullpath, str_lit(".odin"))) {
  4001. return ParseFile_WrongExtension;
  4002. }
  4003. TokenizerInitError err = init_tokenizer(&f->tokenizer, f->fullpath);
  4004. if (err != TokenizerInit_None) {
  4005. switch (err) {
  4006. case TokenizerInit_Empty:
  4007. break;
  4008. case TokenizerInit_NotExists:
  4009. return ParseFile_NotFound;
  4010. case TokenizerInit_Permission:
  4011. return ParseFile_Permission;
  4012. default:
  4013. return ParseFile_InvalidFile;
  4014. }
  4015. }
  4016. isize file_size = f->tokenizer.end - f->tokenizer.start;
  4017. isize init_token_cap = cast(isize)gb_max(next_pow2(cast(i64)(file_size/2ll)), 16);
  4018. array_init(&f->tokens, heap_allocator(), gb_max(init_token_cap, 16));
  4019. if (err == TokenizerInit_Empty) {
  4020. Token token = {Token_EOF};
  4021. token.pos.file = fullpath;
  4022. token.pos.line = 1;
  4023. token.pos.column = 1;
  4024. array_add(&f->tokens, token);
  4025. return ParseFile_None;
  4026. }
  4027. for (;;) {
  4028. Token token = tokenizer_get_token(&f->tokenizer);
  4029. if (token.kind == Token_Invalid) {
  4030. err_pos->line = token.pos.line;
  4031. err_pos->column = token.pos.column;
  4032. return ParseFile_InvalidToken;
  4033. }
  4034. array_add(&f->tokens, token);
  4035. if (token.kind == Token_EOF) {
  4036. break;
  4037. }
  4038. }
  4039. f->curr_token_index = 0;
  4040. f->prev_token = f->tokens[f->curr_token_index];
  4041. f->curr_token = f->tokens[f->curr_token_index];
  4042. // NOTE(bill): Is this big enough or too small?
  4043. isize arena_size = gb_size_of(AstNode);
  4044. arena_size *= 2*f->tokens.count;
  4045. gb_arena_init_from_allocator(&f->arena, heap_allocator(), arena_size);
  4046. array_init(&f->comments, heap_allocator());
  4047. array_init(&f->imports_and_exports, heap_allocator());
  4048. f->curr_proc = nullptr;
  4049. return ParseFile_None;
  4050. }
  4051. void destroy_ast_file(AstFile *f) {
  4052. gb_arena_free(&f->arena);
  4053. array_free(&f->tokens);
  4054. array_free(&f->comments);
  4055. array_free(&f->imports_and_exports);
  4056. gb_free(heap_allocator(), f->tokenizer.fullpath.text);
  4057. destroy_tokenizer(&f->tokenizer);
  4058. }
  4059. bool init_parser(Parser *p) {
  4060. array_init(&p->files, heap_allocator());
  4061. array_init(&p->imports, heap_allocator());
  4062. gb_mutex_init(&p->file_add_mutex);
  4063. gb_mutex_init(&p->file_decl_mutex);
  4064. return true;
  4065. }
  4066. void destroy_parser(Parser *p) {
  4067. // TODO(bill): Fix memory leak
  4068. for_array(i, p->files) {
  4069. destroy_ast_file(p->files[i]);
  4070. }
  4071. #if 0
  4072. for_array(i, p->imports) {
  4073. // gb_free(heap_allocator(), p->imports[i].text);
  4074. }
  4075. #endif
  4076. array_free(&p->files);
  4077. array_free(&p->imports);
  4078. gb_mutex_destroy(&p->file_add_mutex);
  4079. gb_mutex_destroy(&p->file_decl_mutex);
  4080. }
  4081. // NOTE(bill): Returns true if it's added
  4082. bool try_add_import_path(Parser *p, String path, String rel_path, TokenPos pos) {
  4083. if (build_context.generate_docs) {
  4084. return false;
  4085. }
  4086. path = string_trim_whitespace(path);
  4087. rel_path = string_trim_whitespace(rel_path);
  4088. for_array(i, p->imports) {
  4089. String import = p->imports[i].path;
  4090. if (import == path) {
  4091. return false;
  4092. }
  4093. }
  4094. ImportedFile item = {};
  4095. item.kind = ImportedFile_Normal;
  4096. item.path = path;
  4097. item.rel_path = rel_path;
  4098. item.pos = pos;
  4099. item.index = p->imports.count;
  4100. array_add(&p->imports, item);
  4101. return true;
  4102. }
  4103. gb_global Rune illegal_import_runes[] = {
  4104. '"', '\'', '`', ' ', '\t', '\r', '\n', '\v', '\f',
  4105. '\\', // NOTE(bill): Disallow windows style filepaths
  4106. '!', '$', '%', '^', '&', '*', '(', ')', '=', '+',
  4107. '[', ']', '{', '}',
  4108. ';', ':', '#',
  4109. '|', ',', '<', '>', '?',
  4110. };
  4111. bool is_import_path_valid(String path) {
  4112. if (path.len > 0) {
  4113. u8 *start = path.text;
  4114. u8 *end = path.text + path.len;
  4115. u8 *curr = start;
  4116. while (curr < end) {
  4117. isize width = 1;
  4118. Rune r = curr[0];
  4119. if (r >= 0x80) {
  4120. width = gb_utf8_decode(curr, end-curr, &r);
  4121. if (r == GB_RUNE_INVALID && width == 1) {
  4122. return false;
  4123. }
  4124. else if (r == GB_RUNE_BOM && curr-start > 0) {
  4125. return false;
  4126. }
  4127. }
  4128. for (isize i = 0; i < gb_count_of(illegal_import_runes); i++) {
  4129. if (r == illegal_import_runes[i]) {
  4130. return false;
  4131. }
  4132. }
  4133. curr += width;
  4134. }
  4135. return true;
  4136. }
  4137. return false;
  4138. }
  4139. bool determine_path_from_string(Parser *p, AstNode *node, String base_dir, String original_string, String *path) {
  4140. GB_ASSERT(path != nullptr);
  4141. gbAllocator a = heap_allocator();
  4142. String collection_name = {};
  4143. isize colon_pos = -1;
  4144. for (isize j = 0; j < original_string.len; j++) {
  4145. if (original_string[j] == ':') {
  4146. colon_pos = j;
  4147. break;
  4148. }
  4149. }
  4150. String file_str = {};
  4151. if (colon_pos == 0) {
  4152. syntax_error(node, "Expected a collection name");
  4153. return false;
  4154. }
  4155. if (original_string.len > 0 && colon_pos > 0) {
  4156. collection_name = substring(original_string, 0, colon_pos);
  4157. file_str = substring(original_string, colon_pos+1, original_string.len);
  4158. } else {
  4159. file_str = original_string;
  4160. }
  4161. if (!is_import_path_valid(file_str)) {
  4162. syntax_error(node, "Invalid import path: '%.*s'", LIT(file_str));
  4163. return false;
  4164. }
  4165. gb_mutex_lock(&p->file_decl_mutex);
  4166. defer (gb_mutex_unlock(&p->file_decl_mutex));
  4167. if (node->kind == AstNode_ForeignImportDecl) {
  4168. node->ForeignImportDecl.collection_name = collection_name;
  4169. }
  4170. if (collection_name.len > 0) {
  4171. if (collection_name == "system") {
  4172. if (node->kind != AstNode_ForeignImportDecl) {
  4173. syntax_error(node, "The library collection 'system' is restrict for 'foreign_library'");
  4174. return false;
  4175. } else {
  4176. *path = file_str;
  4177. return true;
  4178. }
  4179. } else if (!find_library_collection_path(collection_name, &base_dir)) {
  4180. // NOTE(bill): It's a naughty name
  4181. syntax_error(node, "Unknown library collection: '%.*s'", LIT(collection_name));
  4182. return false;
  4183. }
  4184. } else {
  4185. #if !defined(GB_SYSTEM_WINDOWS)
  4186. // @NOTE(vassvik): foreign imports of shared libraries that are not in the system collection on
  4187. // linux/mac have to be local to the executable for consistency with shared libraries.
  4188. // Unix does not have a concept of "import library" for shared/dynamic libraries,
  4189. // so we need to pass the relative path to the linker, and add the current
  4190. // working directory of the exe to the library search paths.
  4191. // Static libraries can be linked directly with the full pathname
  4192. //
  4193. if (node->kind == AstNode_ForeignImportDecl && string_ends_with(file_str, str_lit(".so"))) {
  4194. *path = file_str;
  4195. return true;
  4196. }
  4197. #endif
  4198. }
  4199. String fullpath = string_trim_whitespace(get_fullpath_relative(a, base_dir, file_str));
  4200. *path = fullpath;
  4201. return true;
  4202. }
  4203. void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<AstNode *> decls);
  4204. void parse_setup_file_when_stmt(Parser *p, AstFile *f, String base_dir, AstNodeWhenStmt *ws) {
  4205. if (ws->body != nullptr) {
  4206. auto stmts = ws->body->BlockStmt.stmts;
  4207. parse_setup_file_decls(p, f, base_dir, stmts);
  4208. }
  4209. if (ws->else_stmt != nullptr) {
  4210. switch (ws->else_stmt->kind) {
  4211. case AstNode_BlockStmt: {
  4212. auto stmts = ws->else_stmt->BlockStmt.stmts;
  4213. parse_setup_file_decls(p, f, base_dir, stmts);
  4214. } break;
  4215. case AstNode_WhenStmt:
  4216. parse_setup_file_when_stmt(p, f, base_dir, &ws->else_stmt->WhenStmt);
  4217. break;
  4218. }
  4219. }
  4220. }
  4221. void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<AstNode *> decls) {
  4222. for_array(i, decls) {
  4223. AstNode *node = decls[i];
  4224. if (!is_ast_node_decl(node) &&
  4225. node->kind != AstNode_BadStmt &&
  4226. node->kind != AstNode_EmptyStmt &&
  4227. node->kind != AstNode_WhenStmt) {
  4228. // NOTE(bill): Sanity check
  4229. syntax_error(node, "Only declarations are allowed at file scope, got %.*s", LIT(ast_node_strings[node->kind]));
  4230. } else if (node->kind == AstNode_ImportDecl) {
  4231. ast_node(id, ImportDecl, node);
  4232. String original_string = id->relpath.string;
  4233. String import_path = {};
  4234. bool ok = determine_path_from_string(p, node, base_dir, original_string, &import_path);
  4235. if (!ok) {
  4236. decls[i] = ast_bad_decl(f, id->relpath, id->relpath);
  4237. continue;
  4238. }
  4239. id->fullpath = import_path;
  4240. try_add_import_path(p, import_path, original_string, ast_node_token(node).pos);
  4241. } else if (node->kind == AstNode_ExportDecl) {
  4242. ast_node(ed, ExportDecl, node);
  4243. String original_string = ed->relpath.string;
  4244. String export_path = {};
  4245. bool ok = determine_path_from_string(p, node, base_dir, original_string, &export_path);
  4246. if (!ok) {
  4247. decls[i] = ast_bad_decl(f, ed->relpath, ed->relpath);
  4248. continue;
  4249. }
  4250. export_path = string_trim_whitespace(export_path);
  4251. ed->fullpath = export_path;
  4252. try_add_import_path(p, export_path, original_string, ast_node_token(node).pos);
  4253. } else if (node->kind == AstNode_ForeignImportDecl) {
  4254. ast_node(fl, ForeignImportDecl, node);
  4255. String file_str = fl->filepath.string;
  4256. fl->base_dir = base_dir;
  4257. fl->fullpath = file_str;
  4258. if (fl->collection_name != "system") {
  4259. String foreign_path = {};
  4260. bool ok = determine_path_from_string(p, node, base_dir, file_str, &foreign_path);
  4261. if (!ok) {
  4262. decls[i] = ast_bad_decl(f, fl->filepath, fl->filepath);
  4263. continue;
  4264. }
  4265. fl->fullpath = foreign_path;
  4266. }
  4267. } else if (node->kind == AstNode_WhenStmt) {
  4268. ast_node(ws, WhenStmt, node);
  4269. parse_setup_file_when_stmt(p, f, base_dir, ws);
  4270. }
  4271. }
  4272. }
  4273. void parse_file(Parser *p, AstFile *f) {
  4274. if (f->tokens.count == 0) {
  4275. return;
  4276. }
  4277. if (f->tokens.count > 0 && f->tokens[0].kind == Token_EOF) {
  4278. return;
  4279. }
  4280. String filepath = f->tokenizer.fullpath;
  4281. String base_dir = filepath;
  4282. for (isize i = filepath.len-1; i >= 0; i--) {
  4283. if (base_dir[i] == '\\' ||
  4284. base_dir[i] == '/') {
  4285. break;
  4286. }
  4287. base_dir.len--;
  4288. }
  4289. comsume_comment_groups(f, f->prev_token);
  4290. f->decls = parse_stmt_list(f);
  4291. parse_setup_file_decls(p, f, base_dir, f->decls);
  4292. }
  4293. ParseFileError parse_import(Parser *p, ImportedFile imported_file) {
  4294. String import_path = imported_file.path;
  4295. String import_rel_path = imported_file.rel_path;
  4296. TokenPos pos = imported_file.pos;
  4297. AstFile *file = gb_alloc_item(heap_allocator(), AstFile);
  4298. file->file_kind = imported_file.kind;
  4299. if (file->file_kind == ImportedFile_Shared) {
  4300. file->is_global_scope = true;
  4301. }
  4302. TokenPos err_pos = {0};
  4303. ParseFileError err = init_ast_file(file, import_path, &err_pos);
  4304. if (err != ParseFile_None) {
  4305. if (err == ParseFile_EmptyFile) {
  4306. if (import_path == p->init_fullpath) {
  4307. gb_printf_err("Initial file is empty - %.*s\n", LIT(p->init_fullpath));
  4308. gb_exit(1);
  4309. }
  4310. goto skip;
  4311. }
  4312. if (pos.line != 0) {
  4313. gb_printf_err("%.*s(%td:%td) ", LIT(pos.file), pos.line, pos.column);
  4314. }
  4315. gb_printf_err("Failed to parse file: %.*s\n\t", LIT(import_rel_path));
  4316. switch (err) {
  4317. case ParseFile_WrongExtension:
  4318. gb_printf_err("Invalid file extension: File must have the extension '.odin'");
  4319. break;
  4320. case ParseFile_InvalidFile:
  4321. gb_printf_err("Invalid file or cannot be found");
  4322. break;
  4323. case ParseFile_Permission:
  4324. gb_printf_err("File permissions problem");
  4325. break;
  4326. case ParseFile_NotFound:
  4327. gb_printf_err("File cannot be found ('%.*s')", LIT(import_path));
  4328. break;
  4329. case ParseFile_InvalidToken:
  4330. gb_printf_err("Invalid token found in file at (%td:%td)", err_pos.line, err_pos.column);
  4331. break;
  4332. case ParseFile_EmptyFile:
  4333. gb_printf_err("File contains no tokens");
  4334. break;
  4335. }
  4336. gb_printf_err("\n");
  4337. return err;
  4338. }
  4339. skip:
  4340. parse_file(p, file);
  4341. gb_mutex_lock(&p->file_add_mutex);
  4342. file->id = imported_file.index;
  4343. array_add(&p->files, file);
  4344. p->total_line_count += file->tokenizer.line_count;
  4345. gb_mutex_unlock(&p->file_add_mutex);
  4346. return ParseFile_None;
  4347. }
  4348. GB_THREAD_PROC(parse_worker_file_proc) {
  4349. if (thread == nullptr) return 0;
  4350. auto *p = cast(Parser *)thread->user_data;
  4351. isize index = thread->user_index;
  4352. ImportedFile imported_file = p->imports[index];
  4353. ParseFileError err = parse_import(p, imported_file);
  4354. return cast(isize)err;
  4355. }
  4356. struct ParserThreadWork {
  4357. Parser *parser;
  4358. isize import_index;
  4359. };
  4360. ParseFileError parse_files(Parser *p, String init_filename) {
  4361. GB_ASSERT(init_filename.text[init_filename.len] == 0);
  4362. char *fullpath_str = gb_path_get_full_name(heap_allocator(), cast(char *)&init_filename[0]);
  4363. String init_fullpath = string_trim_whitespace(make_string_c(fullpath_str));
  4364. TokenPos init_pos = {};
  4365. ImportedFile init_imported_file = {ImportedFile_Init, init_fullpath, init_fullpath, init_pos};
  4366. isize shared_file_count = 0;
  4367. if (!build_context.generate_docs) {
  4368. String s = get_fullpath_core(heap_allocator(), str_lit("_preload.odin"));
  4369. ImportedFile runtime_file = {ImportedFile_Shared, s, s, init_pos};
  4370. array_add(&p->imports, runtime_file);
  4371. shared_file_count++;
  4372. }
  4373. if (!build_context.generate_docs) {
  4374. String s = get_fullpath_core(heap_allocator(), str_lit("_soft_numbers.odin"));
  4375. ImportedFile runtime_file = {ImportedFile_Shared, s, s, init_pos};
  4376. array_add(&p->imports, runtime_file);
  4377. shared_file_count++;
  4378. }
  4379. array_add(&p->imports, init_imported_file);
  4380. p->init_fullpath = init_fullpath;
  4381. /*
  4382. // IMPORTANT TODO(bill): Figure out why this doesn't work on *nix sometimes
  4383. #if USE_THREADED_PARSER && defined(GB_SYSTEM_WINDOWS)
  4384. isize thread_count = gb_max(build_context.thread_count, 1);
  4385. if (thread_count > 1) {
  4386. Array<gbThread> worker_threads = {};
  4387. array_init_count(&worker_threads, heap_allocator(), thread_count);
  4388. defer (array_free(&worker_threads));
  4389. for_array(i, p->imports) {
  4390. gbThread *t = &worker_threads[i];
  4391. gb_thread_init(t);
  4392. }
  4393. isize curr_import_index = 0;
  4394. // NOTE(bill): Make sure that these are in parsed in this order
  4395. for (isize i = 0; i < shared_file_count; i++) {
  4396. ParseFileError err = parse_import(p, p->imports[i]);
  4397. if (err != ParseFile_None) {
  4398. return err;
  4399. }
  4400. curr_import_index++;
  4401. }
  4402. for (;;) {
  4403. bool are_any_alive = false;
  4404. for_array(i, worker_threads) {
  4405. gbThread *t = &worker_threads[i];
  4406. if (gb_thread_is_running(t)) {
  4407. are_any_alive = true;
  4408. } else if (curr_import_index < p->imports.count) {
  4409. auto err = cast(ParseFileError)t->return_value;
  4410. if (err != ParseFile_None) {
  4411. for_array(i, worker_threads) {
  4412. gb_thread_destroy(&worker_threads[i]);
  4413. }
  4414. return err;
  4415. }
  4416. t->user_index = curr_import_index++;
  4417. gb_thread_start(t, parse_worker_file_proc, p);
  4418. are_any_alive = true;
  4419. }
  4420. }
  4421. if (!are_any_alive && curr_import_index >= p->imports.count) {
  4422. break;
  4423. }
  4424. }
  4425. for_array(i, worker_threads) {
  4426. gb_thread_destroy(&worker_threads[i]);
  4427. }
  4428. } else {
  4429. for_array(i, p->imports) {
  4430. ParseFileError err = parse_import(p, p->imports[i]);
  4431. if (err != ParseFile_None) {
  4432. return err;
  4433. }
  4434. }
  4435. }
  4436. #else */
  4437. isize import_index = 0;
  4438. for (; import_index < p->imports.count; import_index++) {
  4439. ParseFileError err = parse_import(p, p->imports[import_index]);
  4440. if (err != ParseFile_None) {
  4441. return err;
  4442. }
  4443. }
  4444. // #endif
  4445. for_array(i, p->files) {
  4446. p->total_token_count += p->files[i]->tokens.count;
  4447. }
  4448. return ParseFile_None;
  4449. }