parser.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. struct Ast;
  2. struct Scope;
  3. struct Type;
  4. struct Entity;
  5. struct DeclInfo;
  6. struct AstFile;
  7. struct AstPackage;
  8. enum AddressingMode {
  9. Addressing_Invalid, // invalid addressing mode
  10. Addressing_NoValue, // no value (void in C)
  11. Addressing_Value, // computed value (rvalue)
  12. Addressing_Context, // context value
  13. Addressing_Variable, // addressable variable (lvalue)
  14. Addressing_Constant, // constant
  15. Addressing_Type, // type
  16. Addressing_Builtin, // built-in procedure
  17. Addressing_ProcGroup, // procedure group (overloaded procedure)
  18. Addressing_MapIndex, // map index expression -
  19. // lhs: acts like a Variable
  20. // rhs: acts like OptionalOk
  21. Addressing_OptionalOk, // rhs: acts like a value with an optional boolean part (for existence check)
  22. Addressing_SoaVariable, // Struct-Of-Arrays indexed variable
  23. Addressing_AtomOpAssign, // Specialized for custom atom operations for assignments
  24. };
  25. struct TypeAndValue {
  26. AddressingMode mode;
  27. Type * type;
  28. ExactValue value;
  29. };
  30. enum ParseFileError {
  31. ParseFile_None,
  32. ParseFile_WrongExtension,
  33. ParseFile_InvalidFile,
  34. ParseFile_EmptyFile,
  35. ParseFile_Permission,
  36. ParseFile_NotFound,
  37. ParseFile_InvalidToken,
  38. ParseFile_GeneralError,
  39. ParseFile_FileTooLarge,
  40. ParseFile_Count,
  41. };
  42. struct CommentGroup {
  43. Slice<Token> list; // Token_Comment
  44. };
  45. enum PackageKind {
  46. Package_Normal,
  47. Package_Runtime,
  48. Package_Init,
  49. };
  50. struct ImportedPackage {
  51. PackageKind kind;
  52. String path;
  53. String rel_path;
  54. TokenPos pos; // import
  55. isize index;
  56. };
  57. struct ImportedFile {
  58. AstPackage *pkg;
  59. FileInfo fi;
  60. TokenPos pos; // import
  61. isize index;
  62. };
  63. struct AstFile {
  64. i32 id;
  65. AstPackage * pkg;
  66. Scope * scope;
  67. Arena arena;
  68. Ast * pkg_decl;
  69. String fullpath;
  70. Tokenizer tokenizer;
  71. Array<Token> tokens;
  72. isize curr_token_index;
  73. Token curr_token;
  74. Token prev_token; // previous non-comment
  75. Token package_token;
  76. String package_name;
  77. // >= 0: In Expression
  78. // < 0: In Control Clause
  79. // NOTE(bill): Used to prevent type literals in control clauses
  80. isize expr_level;
  81. bool allow_range; // NOTE(bill): Ranges are only allowed in certain cases
  82. bool allow_in_expr; // NOTE(bill): in expression are only allowed in certain cases
  83. bool in_foreign_block;
  84. bool allow_type;
  85. Slice<Ast *> decls;
  86. Array<Ast *> imports; // 'import'
  87. isize directive_count;
  88. Ast * curr_proc;
  89. isize error_count;
  90. ParseFileError last_error;
  91. f64 time_to_tokenize; // seconds
  92. f64 time_to_parse; // seconds
  93. bool is_private;
  94. bool is_test;
  95. CommentGroup *lead_comment; // Comment (block) before the decl
  96. CommentGroup *line_comment; // Comment after the semicolon
  97. CommentGroup *docs; // current docs
  98. Array<CommentGroup *> comments; // All the comments!
  99. #define PARSER_MAX_FIX_COUNT 6
  100. isize fix_count;
  101. TokenPos fix_prev_pos;
  102. struct LLVMOpaqueMetadata *llvm_metadata;
  103. struct LLVMOpaqueMetadata *llvm_metadata_scope;
  104. };
  105. enum AstForeignFileKind {
  106. AstForeignFile_Invalid,
  107. AstForeignFile_S, // Source,
  108. AstForeignFile_COUNT
  109. };
  110. struct AstForeignFile {
  111. AstForeignFileKind kind;
  112. String source;
  113. };
  114. struct AstPackage {
  115. PackageKind kind;
  116. isize id;
  117. String name;
  118. String fullpath;
  119. Array<AstFile *> files;
  120. Array<AstForeignFile> foreign_files;
  121. bool is_single_file;
  122. // NOTE(bill): Created/set in checker
  123. Scope * scope;
  124. DeclInfo *decl_info;
  125. bool used;
  126. bool is_extra;
  127. };
  128. struct Parser {
  129. String init_fullpath;
  130. StringSet imported_files; // fullpath
  131. StringMap<AstPackage *> package_map; // Key(package name)
  132. Array<AstPackage *> packages;
  133. Array<ImportedPackage> package_imports;
  134. isize file_to_process_count;
  135. isize total_token_count;
  136. isize total_line_count;
  137. gbMutex file_add_mutex;
  138. gbMutex file_decl_mutex;
  139. };
  140. gb_global ThreadPool parser_thread_pool = {};
  141. struct ParserWorkerData {
  142. Parser *parser;
  143. ImportedFile imported_file;
  144. };
  145. struct ForeignFileWorkerData {
  146. Parser *parser;
  147. ImportedFile imported_file;
  148. AstForeignFileKind foreign_kind;
  149. };
  150. enum ProcInlining {
  151. ProcInlining_none = 0,
  152. ProcInlining_inline = 1,
  153. ProcInlining_no_inline = 2,
  154. };
  155. enum ProcTag {
  156. ProcTag_bounds_check = 1<<0,
  157. ProcTag_no_bounds_check = 1<<1,
  158. ProcTag_require_results = 1<<4,
  159. ProcTag_optional_ok = 1<<5,
  160. };
  161. enum ProcCallingConvention {
  162. ProcCC_Invalid = 0,
  163. ProcCC_Odin = 1,
  164. ProcCC_Contextless = 2,
  165. ProcCC_CDecl = 3,
  166. ProcCC_StdCall = 4,
  167. ProcCC_FastCall = 5,
  168. ProcCC_None = 6,
  169. ProcCC_InlineAsm = 7,
  170. ProcCC_MAX,
  171. ProcCC_ForeignBlockDefault = -1,
  172. };
  173. enum StateFlag : u16 {
  174. StateFlag_bounds_check = 1<<0,
  175. StateFlag_no_bounds_check = 1<<1,
  176. StateFlag_no_deferred = 1<<5,
  177. StateFlag_BeenHandled = 1<<15,
  178. };
  179. enum ViralStateFlag : u16 {
  180. ViralStateFlag_ContainsDeferredProcedure = 1<<0,
  181. };
  182. enum FieldFlag {
  183. FieldFlag_NONE = 0,
  184. FieldFlag_ellipsis = 1<<0,
  185. FieldFlag_using = 1<<1,
  186. FieldFlag_no_alias = 1<<2,
  187. FieldFlag_c_vararg = 1<<3,
  188. FieldFlag_auto_cast = 1<<4,
  189. FieldFlag_const = 1<<5,
  190. FieldFlag_Tags = 1<<10,
  191. FieldFlag_Results = 1<<16,
  192. FieldFlag_Signature = FieldFlag_ellipsis|FieldFlag_using|FieldFlag_no_alias|FieldFlag_c_vararg|FieldFlag_auto_cast|FieldFlag_const,
  193. FieldFlag_Struct = FieldFlag_using|FieldFlag_Tags,
  194. };
  195. enum StmtAllowFlag {
  196. StmtAllowFlag_None = 0,
  197. StmtAllowFlag_In = 1<<0,
  198. StmtAllowFlag_Label = 1<<1,
  199. };
  200. enum InlineAsmDialectKind : u8 {
  201. InlineAsmDialect_Default, // ATT is default
  202. InlineAsmDialect_ATT,
  203. InlineAsmDialect_Intel,
  204. InlineAsmDialect_COUNT,
  205. };
  206. char const *inline_asm_dialect_strings[InlineAsmDialect_COUNT] = {
  207. "",
  208. "att",
  209. "intel",
  210. };
  211. #define AST_KINDS \
  212. AST_KIND(Ident, "identifier", struct { \
  213. Token token; \
  214. Entity *entity; \
  215. }) \
  216. AST_KIND(Implicit, "implicit", Token) \
  217. AST_KIND(Undef, "undef", Token) \
  218. AST_KIND(BasicLit, "basic literal", struct { \
  219. Token token; \
  220. }) \
  221. AST_KIND(BasicDirective, "basic directive", struct { \
  222. Token token; \
  223. String name; \
  224. }) \
  225. AST_KIND(Ellipsis, "ellipsis", struct { \
  226. Token token; \
  227. Ast *expr; \
  228. }) \
  229. AST_KIND(ProcGroup, "procedure group", struct { \
  230. Token token; \
  231. Token open; \
  232. Token close; \
  233. Slice<Ast *> args; \
  234. }) \
  235. AST_KIND(ProcLit, "procedure literal", struct { \
  236. Ast *type; \
  237. Ast *body; \
  238. u64 tags; \
  239. ProcInlining inlining; \
  240. Token where_token; \
  241. Slice<Ast *> where_clauses; \
  242. DeclInfo *decl; \
  243. }) \
  244. AST_KIND(CompoundLit, "compound literal", struct { \
  245. Ast *type; \
  246. Slice<Ast *> elems; \
  247. Token open, close; \
  248. i64 max_count; \
  249. }) \
  250. AST_KIND(_ExprBegin, "", bool) \
  251. AST_KIND(BadExpr, "bad expression", struct { Token begin, end; }) \
  252. AST_KIND(TagExpr, "tag expression", struct { Token token, name; Ast *expr; }) \
  253. AST_KIND(UnaryExpr, "unary expression", struct { Token op; Ast *expr; }) \
  254. AST_KIND(BinaryExpr, "binary expression", struct { Token op; Ast *left, *right; } ) \
  255. AST_KIND(ParenExpr, "parentheses expression", struct { Ast *expr; Token open, close; }) \
  256. AST_KIND(SelectorExpr, "selector expression", struct { Token token; Ast *expr, *selector; }) \
  257. AST_KIND(ImplicitSelectorExpr, "implicit selector expression", struct { Token token; Ast *selector; }) \
  258. AST_KIND(SelectorCallExpr, "selector call expression", struct { Token token; Ast *expr, *call; bool modified_call; }) \
  259. AST_KIND(IndexExpr, "index expression", struct { Ast *expr, *index; Token open, close; }) \
  260. AST_KIND(DerefExpr, "dereference expression", struct { Token op; Ast *expr; }) \
  261. AST_KIND(SliceExpr, "slice expression", struct { \
  262. Ast *expr; \
  263. Token open, close; \
  264. Token interval; \
  265. Ast *low, *high; \
  266. }) \
  267. AST_KIND(CallExpr, "call expression", struct { \
  268. Ast * proc; \
  269. Slice<Ast *> args; \
  270. Token open; \
  271. Token close; \
  272. Token ellipsis; \
  273. ProcInlining inlining; \
  274. bool optional_ok_one; \
  275. }) \
  276. AST_KIND(FieldValue, "field value", struct { Token eq; Ast *field, *value; }) \
  277. AST_KIND(TernaryExpr, "ternary expression", struct { Ast *cond, *x, *y; }) \
  278. AST_KIND(TernaryIfExpr, "ternary if expression", struct { Ast *x, *cond, *y; }) \
  279. AST_KIND(TernaryWhenExpr, "ternary when expression", struct { Ast *x, *cond, *y; }) \
  280. AST_KIND(TypeAssertion, "type assertion", struct { Ast *expr; Token dot; Ast *type; Type *type_hint; }) \
  281. AST_KIND(TypeCast, "type cast", struct { Token token; Ast *type, *expr; }) \
  282. AST_KIND(AutoCast, "auto_cast", struct { Token token; Ast *expr; }) \
  283. AST_KIND(InlineAsmExpr, "inline asm expression", struct { \
  284. Token token; \
  285. Token open, close; \
  286. Slice<Ast *> param_types; \
  287. Ast *return_type; \
  288. Ast *asm_string; \
  289. Ast *constraints_string; \
  290. bool has_side_effects; \
  291. bool is_align_stack; \
  292. InlineAsmDialectKind dialect; \
  293. }) \
  294. AST_KIND(_ExprEnd, "", bool) \
  295. AST_KIND(_StmtBegin, "", bool) \
  296. AST_KIND(BadStmt, "bad statement", struct { Token begin, end; }) \
  297. AST_KIND(EmptyStmt, "empty statement", struct { Token token; }) \
  298. AST_KIND(ExprStmt, "expression statement", struct { Ast *expr; } ) \
  299. AST_KIND(TagStmt, "tag statement", struct { \
  300. Token token; \
  301. Token name; \
  302. Ast * stmt; \
  303. }) \
  304. AST_KIND(AssignStmt, "assign statement", struct { \
  305. Token op; \
  306. Slice<Ast *> lhs, rhs; \
  307. }) \
  308. AST_KIND(_ComplexStmtBegin, "", bool) \
  309. AST_KIND(BlockStmt, "block statement", struct { \
  310. Slice<Ast *> stmts; \
  311. Ast *label; \
  312. Token open, close; \
  313. }) \
  314. AST_KIND(IfStmt, "if statement", struct { \
  315. Token token; \
  316. Ast *label; \
  317. Ast * init; \
  318. Ast * cond; \
  319. Ast * body; \
  320. Ast * else_stmt; \
  321. }) \
  322. AST_KIND(WhenStmt, "when statement", struct { \
  323. Token token; \
  324. Ast *cond; \
  325. Ast *body; \
  326. Ast *else_stmt; \
  327. bool is_cond_determined; \
  328. bool determined_cond; \
  329. }) \
  330. AST_KIND(ReturnStmt, "return statement", struct { \
  331. Token token; \
  332. Slice<Ast *> results; \
  333. }) \
  334. AST_KIND(ForStmt, "for statement", struct { \
  335. Token token; \
  336. Ast *label; \
  337. Ast *init; \
  338. Ast *cond; \
  339. Ast *post; \
  340. Ast *body; \
  341. }) \
  342. AST_KIND(RangeStmt, "range statement", struct { \
  343. Token token; \
  344. Ast *label; \
  345. Ast *val0; \
  346. Ast *val1; \
  347. Token in_token; \
  348. Ast *expr; \
  349. Ast *body; \
  350. }) \
  351. AST_KIND(InlineRangeStmt, "inline range statement", struct { \
  352. Token inline_token; \
  353. Token for_token; \
  354. Ast *val0; \
  355. Ast *val1; \
  356. Token in_token; \
  357. Ast *expr; \
  358. Ast *body; \
  359. }) \
  360. AST_KIND(CaseClause, "case clause", struct { \
  361. Token token; \
  362. Slice<Ast *> list; \
  363. Slice<Ast *> stmts; \
  364. Entity *implicit_entity; \
  365. }) \
  366. AST_KIND(SwitchStmt, "switch statement", struct { \
  367. Token token; \
  368. Ast *label; \
  369. Ast *init; \
  370. Ast *tag; \
  371. Ast *body; \
  372. bool partial; \
  373. }) \
  374. AST_KIND(TypeSwitchStmt, "type switch statement", struct { \
  375. Token token; \
  376. Ast *label; \
  377. Ast *tag; \
  378. Ast *body; \
  379. bool partial; \
  380. }) \
  381. AST_KIND(DeferStmt, "defer statement", struct { Token token; Ast *stmt; }) \
  382. AST_KIND(BranchStmt, "branch statement", struct { Token token; Ast *label; }) \
  383. AST_KIND(UsingStmt, "using statement", struct { \
  384. Token token; \
  385. Slice<Ast *> list; \
  386. }) \
  387. AST_KIND(_ComplexStmtEnd, "", bool) \
  388. AST_KIND(_StmtEnd, "", bool) \
  389. AST_KIND(_DeclBegin, "", bool) \
  390. AST_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
  391. AST_KIND(ForeignBlockDecl, "foreign block declaration", struct { \
  392. Token token; \
  393. Ast *foreign_library; \
  394. Ast *body; \
  395. Array<Ast *> attributes; \
  396. CommentGroup *docs; \
  397. }) \
  398. AST_KIND(Label, "label", struct { \
  399. Token token; \
  400. Ast *name; \
  401. }) \
  402. AST_KIND(ValueDecl, "value declaration", struct { \
  403. Slice<Ast *> names; \
  404. Ast * type; \
  405. Slice<Ast *> values; \
  406. Array<Ast *> attributes; \
  407. CommentGroup *docs; \
  408. CommentGroup *comment; \
  409. bool is_using; \
  410. bool is_mutable; \
  411. }) \
  412. AST_KIND(PackageDecl, "package declaration", struct { \
  413. Token token; \
  414. Token name; \
  415. CommentGroup *docs; \
  416. CommentGroup *comment; \
  417. }) \
  418. AST_KIND(ImportDecl, "import declaration", struct { \
  419. AstPackage *package; \
  420. Token token; \
  421. Token relpath; \
  422. String fullpath; \
  423. Token import_name; \
  424. CommentGroup *docs; \
  425. CommentGroup *comment; \
  426. bool is_using; \
  427. }) \
  428. AST_KIND(ForeignImportDecl, "foreign import declaration", struct { \
  429. Token token; \
  430. Slice<Token> filepaths; \
  431. Token library_name; \
  432. String collection_name; \
  433. Slice<String> fullpaths; \
  434. Array<Ast *> attributes; \
  435. CommentGroup *docs; \
  436. CommentGroup *comment; \
  437. }) \
  438. AST_KIND(_DeclEnd, "", bool) \
  439. AST_KIND(Attribute, "attribute", struct { \
  440. Token token; \
  441. Slice<Ast *> elems; \
  442. Token open, close; \
  443. }) \
  444. AST_KIND(Field, "field", struct { \
  445. Slice<Ast *> names; \
  446. Ast * type; \
  447. Ast * default_value; \
  448. Token tag; \
  449. u32 flags; \
  450. CommentGroup * docs; \
  451. CommentGroup * comment; \
  452. }) \
  453. AST_KIND(FieldList, "field list", struct { \
  454. Token token; \
  455. Slice<Ast *> list; \
  456. }) \
  457. AST_KIND(_TypeBegin, "", bool) \
  458. AST_KIND(TypeidType, "typeid", struct { \
  459. Token token; \
  460. Ast *specialization; \
  461. }) \
  462. AST_KIND(HelperType, "helper type", struct { \
  463. Token token; \
  464. Ast *type; \
  465. }) \
  466. AST_KIND(DistinctType, "distinct type", struct { \
  467. Token token; \
  468. Ast *type; \
  469. }) \
  470. AST_KIND(PolyType, "polymorphic type", struct { \
  471. Token token; \
  472. Ast * type; \
  473. Ast * specialization; \
  474. }) \
  475. AST_KIND(ProcType, "procedure type", struct { \
  476. Token token; \
  477. Ast *params; \
  478. Ast *results; \
  479. u64 tags; \
  480. ProcCallingConvention calling_convention; \
  481. bool generic; \
  482. bool diverging; \
  483. }) \
  484. AST_KIND(PointerType, "pointer type", struct { \
  485. Token token; \
  486. Ast *type; \
  487. }) \
  488. AST_KIND(RelativeType, "relative type", struct { \
  489. Ast *tag; \
  490. Ast *type; \
  491. }) \
  492. AST_KIND(ArrayType, "array type", struct { \
  493. Token token; \
  494. Ast *count; \
  495. Ast *elem; \
  496. Ast *tag; \
  497. }) \
  498. AST_KIND(DynamicArrayType, "dynamic array type", struct { \
  499. Token token; \
  500. Ast *elem; \
  501. Ast *tag; \
  502. }) \
  503. AST_KIND(StructType, "struct type", struct { \
  504. Token token; \
  505. Slice<Ast *> fields; \
  506. isize field_count; \
  507. Ast *polymorphic_params; \
  508. Ast *align; \
  509. Token where_token; \
  510. Slice<Ast *> where_clauses; \
  511. bool is_packed; \
  512. bool is_raw_union; \
  513. }) \
  514. AST_KIND(UnionType, "union type", struct { \
  515. Token token; \
  516. Slice<Ast *> variants; \
  517. Ast *polymorphic_params; \
  518. Ast * align; \
  519. bool maybe; \
  520. bool no_nil; \
  521. Token where_token; \
  522. Slice<Ast *> where_clauses; \
  523. }) \
  524. AST_KIND(EnumType, "enum type", struct { \
  525. Token token; \
  526. Ast * base_type; \
  527. Slice<Ast *> fields; /* FieldValue */ \
  528. bool is_using; \
  529. }) \
  530. AST_KIND(BitSetType, "bit set type", struct { \
  531. Token token; \
  532. Ast * elem; \
  533. Ast * underlying; \
  534. }) \
  535. AST_KIND(MapType, "map type", struct { \
  536. Token token; \
  537. Ast *count; \
  538. Ast *key; \
  539. Ast *value; \
  540. }) \
  541. AST_KIND(_TypeEnd, "", bool)
  542. enum AstKind {
  543. Ast_Invalid,
  544. #define AST_KIND(_kind_name_, ...) GB_JOIN2(Ast_, _kind_name_),
  545. AST_KINDS
  546. #undef AST_KIND
  547. Ast_COUNT,
  548. };
  549. String const ast_strings[] = {
  550. {cast(u8 *)"invalid node", gb_size_of("invalid node")},
  551. #define AST_KIND(_kind_name_, name, ...) {cast(u8 *)name, gb_size_of(name)-1},
  552. AST_KINDS
  553. #undef AST_KIND
  554. };
  555. #define AST_KIND(_kind_name_, name, ...) typedef __VA_ARGS__ GB_JOIN2(Ast, _kind_name_);
  556. AST_KINDS
  557. #undef AST_KIND
  558. isize const ast_variant_sizes[] = {
  559. 0,
  560. #define AST_KIND(_kind_name_, name, ...) gb_size_of(GB_JOIN2(Ast, _kind_name_)),
  561. AST_KINDS
  562. #undef AST_KIND
  563. };
  564. struct AstCommonStuff {
  565. AstKind kind;
  566. u16 state_flags;
  567. u16 viral_state_flags;
  568. AstFile * file;
  569. Scope * scope;
  570. TypeAndValue tav; // TODO(bill): Make this a pointer to minimize pointer size
  571. };
  572. struct Ast {
  573. AstKind kind;
  574. u16 state_flags;
  575. u16 viral_state_flags;
  576. AstFile * file;
  577. Scope * scope;
  578. TypeAndValue tav; // TODO(bill): Make this a pointer to minimize pointer size
  579. // IMPORTANT NOTE(bill): This must be at the end since the AST is allocated to be size of the variant
  580. union {
  581. #define AST_KIND(_kind_name_, name, ...) GB_JOIN2(Ast, _kind_name_) _kind_name_;
  582. AST_KINDS
  583. #undef AST_KIND
  584. };
  585. };
  586. #define ast_node(n_, Kind_, node_) GB_JOIN2(Ast, Kind_) *n_ = &(node_)->Kind_; GB_ASSERT_MSG((node_)->kind == GB_JOIN2(Ast_, Kind_), \
  587. "expected '%.*s' got '%.*s'", \
  588. LIT(ast_strings[GB_JOIN2(Ast_, Kind_)]), LIT(ast_strings[(node_)->kind]))
  589. #define case_ast_node(n_, Kind_, node_) case GB_JOIN2(Ast_, Kind_): { ast_node(n_, Kind_, node_);
  590. #ifndef case_end
  591. #define case_end } break;
  592. #endif
  593. gb_inline bool is_ast_expr(Ast *node) {
  594. return gb_is_between(node->kind, Ast__ExprBegin+1, Ast__ExprEnd-1);
  595. }
  596. gb_inline bool is_ast_stmt(Ast *node) {
  597. return gb_is_between(node->kind, Ast__StmtBegin+1, Ast__StmtEnd-1);
  598. }
  599. gb_inline bool is_ast_complex_stmt(Ast *node) {
  600. return gb_is_between(node->kind, Ast__ComplexStmtBegin+1, Ast__ComplexStmtEnd-1);
  601. }
  602. gb_inline bool is_ast_decl(Ast *node) {
  603. return gb_is_between(node->kind, Ast__DeclBegin+1, Ast__DeclEnd-1);
  604. }
  605. gb_inline bool is_ast_type(Ast *node) {
  606. return gb_is_between(node->kind, Ast__TypeBegin+1, Ast__TypeEnd-1);
  607. }
  608. gb_inline bool is_ast_when_stmt(Ast *node) {
  609. return node->kind == Ast_WhenStmt;
  610. }
  611. gb_global Arena global_ast_arena = {};
  612. gbAllocator ast_allocator(AstFile *f) {
  613. Arena *arena = f ? &f->arena : &global_ast_arena;
  614. // Arena *arena = &global_ast_arena;
  615. return arena_allocator(arena);
  616. }
  617. Ast *alloc_ast_node(AstFile *f, AstKind kind);