parser.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. struct Ast;
  2. struct Scope;
  3. struct Type;
  4. struct Entity;
  5. struct DeclInfo;
  6. struct AstFile;
  7. struct AstPackage;
  8. enum AddressingMode : u8 {
  9. Addressing_Invalid = 0, // invalid addressing mode
  10. Addressing_NoValue = 1, // no value (void in C)
  11. Addressing_Value = 2, // computed value (rvalue)
  12. Addressing_Context = 3, // context value
  13. Addressing_Variable = 4, // addressable variable (lvalue)
  14. Addressing_Constant = 5, // constant
  15. Addressing_Type = 6, // type
  16. Addressing_Builtin = 7, // built-in procedure
  17. Addressing_ProcGroup = 8, // procedure group (overloaded procedure)
  18. Addressing_MapIndex = 9, // map index expression -
  19. // lhs: acts like a Variable
  20. // rhs: acts like OptionalOk
  21. Addressing_OptionalOk = 10, // rhs: acts like a value with an optional boolean part (for existence check)
  22. Addressing_OptionalOkPtr = 11, // rhs: same as OptionalOk but the value is a pointer
  23. Addressing_SoaVariable = 12, // Struct-Of-Arrays indexed variable
  24. Addressing_SwizzleValue = 13, // Swizzle indexed value
  25. Addressing_SwizzleVariable = 14, // Swizzle indexed variable
  26. };
  27. gb_global String const addressing_mode_strings[] = {
  28. str_lit("Invalid"),
  29. str_lit("NoValue"),
  30. str_lit("Value"),
  31. str_lit("Context"),
  32. str_lit("Variable"),
  33. str_lit("Constant"),
  34. str_lit("Type"),
  35. str_lit("Builtin"),
  36. str_lit("ProcGroup"),
  37. str_lit("MapIndex"),
  38. str_lit("OptionalOk"),
  39. str_lit("OptionalOkPtr"),
  40. str_lit("SoaVariable"),
  41. str_lit("SwizzleValue"),
  42. str_lit("SwizzleVariable"),
  43. };
  44. struct TypeAndValue {
  45. Type * type;
  46. AddressingMode mode;
  47. bool is_lhs; // Debug info
  48. ExactValue value;
  49. };
  50. enum ParseFileError {
  51. ParseFile_None,
  52. ParseFile_WrongExtension,
  53. ParseFile_InvalidFile,
  54. ParseFile_EmptyFile,
  55. ParseFile_Permission,
  56. ParseFile_NotFound,
  57. ParseFile_InvalidToken,
  58. ParseFile_GeneralError,
  59. ParseFile_FileTooLarge,
  60. ParseFile_DirectoryAlreadyExists,
  61. ParseFile_Count,
  62. };
  63. struct CommentGroup {
  64. Slice<Token> list; // Token_Comment
  65. };
  66. enum PackageKind {
  67. Package_Normal,
  68. Package_Runtime,
  69. Package_Init,
  70. Package_Builtin,
  71. };
  72. struct ImportedFile {
  73. AstPackage *pkg;
  74. FileInfo fi;
  75. TokenPos pos; // import
  76. isize index;
  77. };
  78. enum AstFileFlag : u32 {
  79. AstFile_IsPrivatePkg = 1<<0,
  80. AstFile_IsPrivateFile = 1<<1,
  81. AstFile_IsLazy = 1<<4,
  82. AstFile_NoInstrumentation = 1<<5,
  83. };
  84. enum AstDelayQueueKind {
  85. AstDelayQueue_Import,
  86. AstDelayQueue_Expr,
  87. AstDelayQueue_ForeignBlock,
  88. AstDelayQueue_COUNT,
  89. };
  90. struct AstFile {
  91. i32 id;
  92. u32 flags;
  93. AstPackage * pkg;
  94. Scope * scope;
  95. Ast * pkg_decl;
  96. String fullpath;
  97. String filename;
  98. String directory;
  99. Tokenizer tokenizer;
  100. Array<Token> tokens;
  101. isize curr_token_index;
  102. isize prev_token_index;
  103. Token curr_token;
  104. Token prev_token; // previous non-comment
  105. Token package_token;
  106. String package_name;
  107. u64 vet_flags;
  108. u64 feature_flags;
  109. bool vet_flags_set;
  110. bool feature_flags_set;
  111. // >= 0: In Expression
  112. // < 0: In Control Clause
  113. // NOTE(bill): Used to prevent type literals in control clauses
  114. isize expr_level;
  115. bool allow_newline; // Only valid for expr_level == 0
  116. bool allow_range; // NOTE(bill): Ranges are only allowed in certain cases
  117. bool allow_in_expr; // NOTE(bill): in expression are only allowed in certain cases
  118. bool in_foreign_block;
  119. bool allow_type;
  120. bool in_when_statement;
  121. isize total_file_decl_count;
  122. isize delayed_decl_count;
  123. Slice<Ast *> decls;
  124. Array<Ast *> imports; // 'import'
  125. isize directive_count;
  126. Ast * curr_proc;
  127. isize error_count;
  128. ParseFileError last_error;
  129. f64 time_to_tokenize; // seconds
  130. f64 time_to_parse; // seconds
  131. CommentGroup *lead_comment; // Comment (block) before the decl
  132. CommentGroup *line_comment; // Comment after the semicolon
  133. CommentGroup *docs; // current docs
  134. Array<CommentGroup *> comments; // All the comments!
  135. // This is effectively a queue but does not require any multi-threading capabilities
  136. Array<Ast *> delayed_decls_queues[AstDelayQueue_COUNT];
  137. std::atomic<isize> seen_load_directive_count;
  138. #define PARSER_MAX_FIX_COUNT 6
  139. isize fix_count;
  140. TokenPos fix_prev_pos;
  141. struct LLVMOpaqueMetadata *llvm_metadata;
  142. struct LLVMOpaqueMetadata *llvm_metadata_scope;
  143. };
  144. enum AstForeignFileKind {
  145. AstForeignFile_Invalid,
  146. AstForeignFile_S, // Source,
  147. AstForeignFile_COUNT
  148. };
  149. struct AstForeignFile {
  150. AstForeignFileKind kind;
  151. String source;
  152. };
  153. struct AstPackageExportedEntity {
  154. Ast *identifier;
  155. Entity *entity;
  156. };
  157. struct AstPackage {
  158. PackageKind kind;
  159. isize id;
  160. String name;
  161. String fullpath;
  162. Array<AstFile *> files;
  163. Array<AstForeignFile> foreign_files;
  164. bool is_single_file;
  165. isize order;
  166. BlockingMutex files_mutex;
  167. BlockingMutex foreign_files_mutex;
  168. BlockingMutex type_and_value_mutex;
  169. BlockingMutex name_mutex;
  170. // NOTE(bill): This must be a MPMCQueue
  171. MPMCQueue<AstPackageExportedEntity> exported_entity_queue;
  172. // NOTE(bill): Created/set in checker
  173. Scope * scope;
  174. DeclInfo *decl_info;
  175. bool is_extra;
  176. };
  177. struct ParseFileErrorNode {
  178. ParseFileErrorNode *next, *prev;
  179. ParseFileError err;
  180. };
  181. struct Parser {
  182. String init_fullpath;
  183. StringSet imported_files; // fullpath
  184. BlockingMutex imported_files_mutex;
  185. Array<AstPackage *> packages;
  186. BlockingMutex packages_mutex;
  187. std::atomic<isize> file_to_process_count;
  188. std::atomic<isize> total_token_count;
  189. std::atomic<isize> total_line_count;
  190. std::atomic<isize> total_seen_load_directive_count;
  191. // TODO(bill): What should this mutex be per?
  192. // * Parser
  193. // * Package
  194. // * File
  195. BlockingMutex file_decl_mutex;
  196. BlockingMutex file_error_mutex;
  197. ParseFileErrorNode * file_error_head;
  198. ParseFileErrorNode * file_error_tail;
  199. };
  200. struct ParserWorkerData {
  201. Parser *parser;
  202. ImportedFile imported_file;
  203. };
  204. struct ForeignFileWorkerData {
  205. Parser *parser;
  206. ImportedFile imported_file;
  207. AstForeignFileKind foreign_kind;
  208. };
  209. enum ProcInlining {
  210. ProcInlining_none = 0,
  211. ProcInlining_inline = 1,
  212. ProcInlining_no_inline = 2,
  213. };
  214. enum ProcTag {
  215. ProcTag_bounds_check = 1<<0,
  216. ProcTag_no_bounds_check = 1<<1,
  217. ProcTag_type_assert = 1<<2,
  218. ProcTag_no_type_assert = 1<<3,
  219. ProcTag_require_results = 1<<4,
  220. ProcTag_optional_ok = 1<<5,
  221. ProcTag_optional_allocator_error = 1<<6,
  222. };
  223. enum ProcCallingConvention : i32 {
  224. ProcCC_Invalid = 0,
  225. ProcCC_Odin = 1,
  226. ProcCC_Contextless = 2,
  227. ProcCC_CDecl = 3,
  228. ProcCC_StdCall = 4,
  229. ProcCC_FastCall = 5,
  230. ProcCC_None = 6,
  231. ProcCC_Naked = 7,
  232. ProcCC_InlineAsm = 8,
  233. ProcCC_Win64 = 9,
  234. ProcCC_SysV = 10,
  235. ProcCC_MAX,
  236. ProcCC_ForeignBlockDefault = -1,
  237. };
  238. gb_global char const *proc_calling_convention_strings[ProcCC_MAX] = {
  239. "",
  240. "odin",
  241. "contextless",
  242. "cdecl",
  243. "stdcall",
  244. "fastcall",
  245. "none",
  246. "naked",
  247. "inlineasm",
  248. "win64",
  249. "sysv",
  250. };
  251. gb_internal ProcCallingConvention default_calling_convention(void) {
  252. return ProcCC_Odin;
  253. }
  254. enum StateFlag : u8 {
  255. StateFlag_bounds_check = 1<<0,
  256. StateFlag_no_bounds_check = 1<<1,
  257. StateFlag_type_assert = 1<<2,
  258. StateFlag_no_type_assert = 1<<3,
  259. StateFlag_SelectorCallExpr = 1<<5,
  260. StateFlag_DirectiveWasFalse = 1<<6,
  261. StateFlag_BeenHandled = 1<<7,
  262. };
  263. enum ViralStateFlag : u8 {
  264. ViralStateFlag_ContainsDeferredProcedure = 1<<0,
  265. ViralStateFlag_ContainsOrBreak = 1<<1,
  266. ViralStateFlag_ContainsOrReturn = 1<<2,
  267. };
  268. enum FieldFlag : u32 {
  269. FieldFlag_NONE = 0,
  270. FieldFlag_ellipsis = 1<<0,
  271. FieldFlag_using = 1<<1,
  272. FieldFlag_no_alias = 1<<2,
  273. FieldFlag_c_vararg = 1<<3,
  274. FieldFlag_const = 1<<5,
  275. FieldFlag_any_int = 1<<6,
  276. FieldFlag_subtype = 1<<7,
  277. FieldFlag_by_ptr = 1<<8,
  278. FieldFlag_no_broadcast = 1<<9, // disallow array programming
  279. FieldFlag_no_capture = 1<<11,
  280. // Internal use by the parser only
  281. FieldFlag_Tags = 1<<15,
  282. FieldFlag_Results = 1<<16,
  283. FieldFlag_Unknown = 1u<<30,
  284. FieldFlag_Invalid = 1u<<31,
  285. // Parameter List Restrictions
  286. FieldFlag_Signature = FieldFlag_ellipsis|FieldFlag_using|FieldFlag_no_alias|FieldFlag_c_vararg|
  287. FieldFlag_const|FieldFlag_any_int|FieldFlag_by_ptr|FieldFlag_no_broadcast|
  288. FieldFlag_no_capture,
  289. FieldFlag_Struct = FieldFlag_using|FieldFlag_subtype|FieldFlag_Tags,
  290. };
  291. enum StmtAllowFlag {
  292. StmtAllowFlag_None = 0,
  293. StmtAllowFlag_In = 1<<0,
  294. StmtAllowFlag_Label = 1<<1,
  295. };
  296. enum InlineAsmDialectKind : u8 {
  297. InlineAsmDialect_Default, // ATT is default
  298. InlineAsmDialect_ATT,
  299. InlineAsmDialect_Intel,
  300. InlineAsmDialect_COUNT,
  301. };
  302. gb_global char const *inline_asm_dialect_strings[InlineAsmDialect_COUNT] = {
  303. "",
  304. "att",
  305. "intel",
  306. };
  307. enum UnionTypeKind : u8 {
  308. UnionType_Normal = 0,
  309. UnionType_no_nil = 2,
  310. UnionType_shared_nil = 3,
  311. UnionType_COUNT
  312. };
  313. gb_global char const *union_type_kind_strings[UnionType_COUNT] = {
  314. "(normal)",
  315. "#maybe",
  316. "#no_nil",
  317. "#shared_nil",
  318. };
  319. struct AstSplitArgs {
  320. Slice<Ast *> positional;
  321. Slice<Ast *> named;
  322. };
  323. #define AST_KINDS \
  324. AST_KIND(Ident, "identifier", struct { \
  325. Token token; \
  326. Entity *entity; \
  327. u32 hash; \
  328. }) \
  329. AST_KIND(Implicit, "implicit", Token) \
  330. AST_KIND(Uninit, "uninitialized value", Token) \
  331. AST_KIND(BasicLit, "basic literal", struct { \
  332. Token token; \
  333. }) \
  334. AST_KIND(BasicDirective, "basic directive", struct { \
  335. Token token; \
  336. Token name; \
  337. }) \
  338. AST_KIND(Ellipsis, "ellipsis", struct { \
  339. Token token; \
  340. Ast *expr; \
  341. }) \
  342. AST_KIND(ProcGroup, "procedure group", struct { \
  343. Token token; \
  344. Token open; \
  345. Token close; \
  346. Slice<Ast *> args; \
  347. }) \
  348. AST_KIND(ProcLit, "procedure literal", struct { \
  349. Ast *type; \
  350. Ast *body; \
  351. u64 tags; \
  352. ProcInlining inlining; \
  353. Token where_token; \
  354. Slice<Ast *> where_clauses; \
  355. DeclInfo *decl; \
  356. }) \
  357. AST_KIND(CompoundLit, "compound literal", struct { \
  358. Ast *type; \
  359. Slice<Ast *> elems; \
  360. Token open, close; \
  361. i64 max_count; \
  362. Ast *tag; \
  363. }) \
  364. AST_KIND(_ExprBegin, "", bool) \
  365. AST_KIND(BadExpr, "bad expression", struct { Token begin, end; }) \
  366. AST_KIND(TagExpr, "tag expression", struct { Token token, name; Ast *expr; }) \
  367. AST_KIND(UnaryExpr, "unary expression", struct { Token op; Ast *expr; }) \
  368. AST_KIND(BinaryExpr, "binary expression", struct { Token op; Ast *left, *right; } ) \
  369. AST_KIND(ParenExpr, "parentheses expression", struct { Ast *expr; Token open, close; }) \
  370. AST_KIND(SelectorExpr, "selector expression", struct { \
  371. Token token; \
  372. Ast *expr, *selector; \
  373. u8 swizzle_count; /*maximum of 4 components, if set, count >= 2*/ \
  374. u8 swizzle_indices; /*2 bits per component*/ \
  375. bool is_bit_field; \
  376. }) \
  377. AST_KIND(ImplicitSelectorExpr, "implicit selector expression", struct { Token token; Ast *selector; }) \
  378. AST_KIND(SelectorCallExpr, "selector call expression", struct { \
  379. Token token; \
  380. Ast *expr, *call; \
  381. bool modified_call; \
  382. }) \
  383. AST_KIND(IndexExpr, "index expression", struct { Ast *expr, *index; Token open, close; }) \
  384. AST_KIND(DerefExpr, "dereference expression", struct { Ast *expr; Token op; }) \
  385. AST_KIND(SliceExpr, "slice expression", struct { \
  386. Ast *expr; \
  387. Token open, close; \
  388. Token interval; \
  389. Ast *low, *high; \
  390. }) \
  391. AST_KIND(CallExpr, "call expression", struct { \
  392. Ast * proc; \
  393. Slice<Ast *> args; \
  394. Token open; \
  395. Token close; \
  396. Token ellipsis; \
  397. ProcInlining inlining; \
  398. bool optional_ok_one; \
  399. bool was_selector; \
  400. AstSplitArgs *split_args; \
  401. Entity *entity_procedure_of; \
  402. }) \
  403. AST_KIND(FieldValue, "field value", struct { Token eq; Ast *field, *value; }) \
  404. AST_KIND(EnumFieldValue, "enum field value", struct { \
  405. Ast *name; \
  406. Ast *value; \
  407. CommentGroup *docs; \
  408. CommentGroup *comment; \
  409. }) \
  410. AST_KIND(TernaryIfExpr, "ternary if expression", struct { Ast *x, *cond, *y; }) \
  411. AST_KIND(TernaryWhenExpr, "ternary when expression", struct { Ast *x, *cond, *y; }) \
  412. AST_KIND(OrElseExpr, "or_else expression", struct { Ast *x; Token token; Ast *y; }) \
  413. AST_KIND(OrReturnExpr, "or_return expression", struct { Ast *expr; Token token; }) \
  414. AST_KIND(OrBranchExpr, "or branch expression", struct { Ast *expr; Token token; Ast *label; }) \
  415. AST_KIND(TypeAssertion, "type assertion", struct { \
  416. Ast *expr; \
  417. Token dot; \
  418. Ast *type; \
  419. Type *type_hint; \
  420. bool ignores[2]; \
  421. }) \
  422. AST_KIND(TypeCast, "type cast", struct { Token token; Ast *type, *expr; }) \
  423. AST_KIND(AutoCast, "auto_cast", struct { Token token; Ast *expr; }) \
  424. AST_KIND(InlineAsmExpr, "inline asm expression", struct { \
  425. Token token; \
  426. Token open, close; \
  427. Slice<Ast *> param_types; \
  428. Ast *return_type; \
  429. Ast *asm_string; \
  430. Ast *constraints_string; \
  431. bool has_side_effects; \
  432. bool is_align_stack; \
  433. InlineAsmDialectKind dialect; \
  434. }) \
  435. AST_KIND(MatrixIndexExpr, "matrix index expression", struct { Ast *expr, *row_index, *column_index; Token open, close; }) \
  436. AST_KIND(_ExprEnd, "", bool) \
  437. AST_KIND(_StmtBegin, "", bool) \
  438. AST_KIND(BadStmt, "bad statement", struct { Token begin, end; }) \
  439. AST_KIND(EmptyStmt, "empty statement", struct { Token token; }) \
  440. AST_KIND(ExprStmt, "expression statement", struct { Ast *expr; } ) \
  441. AST_KIND(AssignStmt, "assign statement", struct { \
  442. Token op; \
  443. Slice<Ast *> lhs, rhs; \
  444. }) \
  445. AST_KIND(_ComplexStmtBegin, "", bool) \
  446. AST_KIND(BlockStmt, "block statement", struct { \
  447. Scope *scope; \
  448. Slice<Ast *> stmts; \
  449. Ast *label; \
  450. Token open, close; \
  451. }) \
  452. AST_KIND(IfStmt, "if statement", struct { \
  453. Scope *scope; \
  454. Token token; \
  455. Ast *label; \
  456. Ast * init; \
  457. Ast * cond; \
  458. Ast * body; \
  459. Ast * else_stmt; \
  460. }) \
  461. AST_KIND(WhenStmt, "when statement", struct { \
  462. Token token; \
  463. Ast *cond; \
  464. Ast *body; \
  465. Ast *else_stmt; \
  466. bool is_cond_determined; \
  467. bool determined_cond; \
  468. }) \
  469. AST_KIND(ReturnStmt, "return statement", struct { \
  470. Token token; \
  471. Slice<Ast *> results; \
  472. }) \
  473. AST_KIND(ForStmt, "for statement", struct { \
  474. Scope *scope; \
  475. Token token; \
  476. Ast *label; \
  477. Ast *init; \
  478. Ast *cond; \
  479. Ast *post; \
  480. Ast *body; \
  481. }) \
  482. AST_KIND(RangeStmt, "range statement", struct { \
  483. Scope *scope; \
  484. Token token; \
  485. Ast *label; \
  486. Slice<Ast *> vals; \
  487. Token in_token; \
  488. Ast *expr; \
  489. Ast *body; \
  490. bool reverse; \
  491. }) \
  492. AST_KIND(UnrollRangeStmt, "#unroll range statement", struct { \
  493. Scope *scope; \
  494. Token unroll_token; \
  495. Slice<Ast *> args; \
  496. Token for_token; \
  497. Ast *val0; \
  498. Ast *val1; \
  499. Token in_token; \
  500. Ast *expr; \
  501. Ast *body; \
  502. }) \
  503. AST_KIND(CaseClause, "case clause", struct { \
  504. Scope *scope; \
  505. Token token; \
  506. Slice<Ast *> list; \
  507. Slice<Ast *> stmts; \
  508. Entity *implicit_entity; \
  509. }) \
  510. AST_KIND(SwitchStmt, "switch statement", struct { \
  511. Scope *scope; \
  512. Token token; \
  513. Ast *label; \
  514. Ast *init; \
  515. Ast *tag; \
  516. Ast *body; \
  517. bool partial; \
  518. }) \
  519. AST_KIND(TypeSwitchStmt, "type switch statement", struct { \
  520. Scope *scope; \
  521. Token token; \
  522. Ast *label; \
  523. Ast *tag; \
  524. Ast *body; \
  525. bool partial; \
  526. }) \
  527. AST_KIND(DeferStmt, "defer statement", struct { Token token; Ast *stmt; }) \
  528. AST_KIND(BranchStmt, "branch statement", struct { Token token; Ast *label; }) \
  529. AST_KIND(UsingStmt, "using statement", struct { \
  530. Token token; \
  531. Slice<Ast *> list; \
  532. }) \
  533. AST_KIND(_ComplexStmtEnd, "", bool) \
  534. AST_KIND(_StmtEnd, "", bool) \
  535. AST_KIND(_DeclBegin, "", bool) \
  536. AST_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
  537. AST_KIND(ForeignBlockDecl, "foreign block declaration", struct { \
  538. Token token; \
  539. Ast *foreign_library; \
  540. Ast *body; \
  541. Array<Ast *> attributes; \
  542. CommentGroup *docs; \
  543. }) \
  544. AST_KIND(Label, "label", struct { \
  545. Token token; \
  546. Ast *name; \
  547. }) \
  548. AST_KIND(ValueDecl, "value declaration", struct { \
  549. Slice<Ast *> names; \
  550. Ast * type; \
  551. Slice<Ast *> values; \
  552. Array<Ast *> attributes; \
  553. CommentGroup *docs; \
  554. CommentGroup *comment; \
  555. bool is_using; \
  556. bool is_mutable; \
  557. }) \
  558. AST_KIND(PackageDecl, "package declaration", struct { \
  559. Token token; \
  560. Token name; \
  561. CommentGroup *docs; \
  562. CommentGroup *comment; \
  563. }) \
  564. AST_KIND(ImportDecl, "import declaration", struct { \
  565. AstPackage *package; \
  566. Token token; \
  567. Token relpath; \
  568. String fullpath; \
  569. Token import_name; \
  570. Array<Ast *> attributes; \
  571. CommentGroup *docs; \
  572. CommentGroup *comment; \
  573. }) \
  574. AST_KIND(ForeignImportDecl, "foreign import declaration", struct { \
  575. Token token; \
  576. Slice<Ast *> filepaths; \
  577. bool multiple_filepaths; \
  578. Token library_name; \
  579. String collection_name; \
  580. Slice<String> fullpaths; \
  581. Array<Ast *> attributes; \
  582. CommentGroup *docs; \
  583. CommentGroup *comment; \
  584. }) \
  585. AST_KIND(_DeclEnd, "", bool) \
  586. AST_KIND(Attribute, "attribute", struct { \
  587. Token token; \
  588. Slice<Ast *> elems; \
  589. Token open, close; \
  590. }) \
  591. AST_KIND(Field, "field", struct { \
  592. Slice<Ast *> names; \
  593. Ast * type; \
  594. Ast * default_value; \
  595. Token tag; \
  596. u32 flags; \
  597. CommentGroup * docs; \
  598. CommentGroup * comment; \
  599. }) \
  600. AST_KIND(BitFieldField, "bit field field", struct { \
  601. Ast * name; \
  602. Ast * type; \
  603. Ast * bit_size; \
  604. Token tag; \
  605. CommentGroup *docs; \
  606. CommentGroup *comment; \
  607. }) \
  608. AST_KIND(FieldList, "field list", struct { \
  609. Token token; \
  610. Slice<Ast *> list; \
  611. }) \
  612. AST_KIND(_TypeBegin, "", bool) \
  613. AST_KIND(TypeidType, "typeid", struct { \
  614. Token token; \
  615. Ast *specialization; \
  616. }) \
  617. AST_KIND(HelperType, "helper type", struct { \
  618. Token token; \
  619. Ast *type; \
  620. }) \
  621. AST_KIND(DistinctType, "distinct type", struct { \
  622. Token token; \
  623. Ast *type; \
  624. }) \
  625. AST_KIND(PolyType, "polymorphic type", struct { \
  626. Token token; \
  627. Ast * type; \
  628. Ast * specialization; \
  629. }) \
  630. AST_KIND(ProcType, "procedure type", struct { \
  631. Scope *scope; \
  632. Token token; \
  633. Ast *params; \
  634. Ast *results; \
  635. u64 tags; \
  636. ProcCallingConvention calling_convention; \
  637. bool generic; \
  638. bool diverging; \
  639. }) \
  640. AST_KIND(PointerType, "pointer type", struct { \
  641. Token token; \
  642. Ast *type; \
  643. Ast *tag; \
  644. }) \
  645. AST_KIND(RelativeType, "relative type", struct { \
  646. Ast *tag; \
  647. Ast *type; \
  648. }) \
  649. AST_KIND(MultiPointerType, "multi pointer type", struct { \
  650. Token token; \
  651. Ast *type; \
  652. }) \
  653. AST_KIND(ArrayType, "array type", struct { \
  654. Token token; \
  655. Ast *count; \
  656. Ast *elem; \
  657. Ast *tag; \
  658. }) \
  659. AST_KIND(DynamicArrayType, "dynamic array type", struct { \
  660. Token token; \
  661. Ast *elem; \
  662. Ast *tag; \
  663. }) \
  664. AST_KIND(StructType, "struct type", struct { \
  665. Scope *scope; \
  666. Token token; \
  667. Slice<Ast *> fields; \
  668. isize field_count; \
  669. Ast *polymorphic_params; \
  670. Ast *align; \
  671. Ast *min_field_align; \
  672. Ast *max_field_align; \
  673. Token where_token; \
  674. Slice<Ast *> where_clauses; \
  675. bool is_packed; \
  676. bool is_raw_union; \
  677. bool is_no_copy; \
  678. }) \
  679. AST_KIND(UnionType, "union type", struct { \
  680. Scope *scope; \
  681. Token token; \
  682. Slice<Ast *> variants; \
  683. Ast *polymorphic_params; \
  684. Ast * align; \
  685. UnionTypeKind kind; \
  686. Token where_token; \
  687. Slice<Ast *> where_clauses; \
  688. }) \
  689. AST_KIND(EnumType, "enum type", struct { \
  690. Scope *scope; \
  691. Token token; \
  692. Ast * base_type; \
  693. Slice<Ast *> fields; /* FieldValue */ \
  694. bool is_using; \
  695. }) \
  696. AST_KIND(BitSetType, "bit set type", struct { \
  697. Token token; \
  698. Ast * elem; \
  699. Ast * underlying; \
  700. }) \
  701. AST_KIND(BitFieldType, "bit field type", struct { \
  702. Scope *scope; \
  703. Token token; \
  704. Ast * backing_type; \
  705. Token open; \
  706. Slice<Ast *> fields; /* BitFieldField */ \
  707. Token close; \
  708. }) \
  709. AST_KIND(MapType, "map type", struct { \
  710. Token token; \
  711. Ast *count; \
  712. Ast *key; \
  713. Ast *value; \
  714. }) \
  715. AST_KIND(MatrixType, "matrix type", struct { \
  716. Token token; \
  717. Ast *row_count; \
  718. Ast *column_count; \
  719. Ast *elem; \
  720. bool is_row_major; \
  721. }) \
  722. AST_KIND(_TypeEnd, "", bool)
  723. enum AstKind : u16 {
  724. Ast_Invalid,
  725. #define AST_KIND(_kind_name_, ...) GB_JOIN2(Ast_, _kind_name_),
  726. AST_KINDS
  727. #undef AST_KIND
  728. Ast_COUNT,
  729. };
  730. gb_global String const ast_strings[] = {
  731. {cast(u8 *)"invalid node", gb_size_of("invalid node")},
  732. #define AST_KIND(_kind_name_, name, ...) {cast(u8 *)name, gb_size_of(name)-1},
  733. AST_KINDS
  734. #undef AST_KIND
  735. };
  736. #define AST_KIND(_kind_name_, name, ...) typedef __VA_ARGS__ GB_JOIN2(Ast, _kind_name_);
  737. AST_KINDS
  738. #undef AST_KIND
  739. gb_global isize const ast_variant_sizes[] = {
  740. 0,
  741. #define AST_KIND(_kind_name_, name, ...) gb_size_of(GB_JOIN2(Ast, _kind_name_)),
  742. AST_KINDS
  743. #undef AST_KIND
  744. };
  745. struct AstCommonStuff {
  746. AstKind kind; // u16
  747. u8 state_flags;
  748. u8 viral_state_flags;
  749. i32 file_id;
  750. TypeAndValue tav; // NOTE(bill): Making this a pointer is slower
  751. };
  752. struct Ast {
  753. AstKind kind; // u16
  754. u8 state_flags;
  755. u8 viral_state_flags;
  756. i32 file_id;
  757. TypeAndValue tav; // NOTE(bill): Making this a pointer is slower
  758. // IMPORTANT NOTE(bill): This must be at the end since the AST is allocated to be size of the variant
  759. union {
  760. #define AST_KIND(_kind_name_, name, ...) GB_JOIN2(Ast, _kind_name_) _kind_name_;
  761. AST_KINDS
  762. #undef AST_KIND
  763. };
  764. // NOTE(bill): I know I dislike methods but this is hopefully a temporary thing
  765. // for refactoring purposes
  766. gb_inline AstFile *file() const {
  767. // NOTE(bill): This doesn't need to call get_ast_file_from_id which
  768. return global_files[this->file_id];
  769. }
  770. gb_inline AstFile *thread_safe_file() const {
  771. return thread_safe_get_ast_file_from_id(this->file_id);
  772. }
  773. };
  774. #define ast_node(n_, Kind_, node_) GB_JOIN2(Ast, Kind_) *n_ = &(node_)->Kind_; gb_unused(n_); GB_ASSERT_MSG((node_)->kind == GB_JOIN2(Ast_, Kind_), \
  775. "expected '%.*s' got '%.*s'", \
  776. LIT(ast_strings[GB_JOIN2(Ast_, Kind_)]), LIT(ast_strings[(node_)->kind]))
  777. #define case_ast_node(n_, Kind_, node_) case GB_JOIN2(Ast_, Kind_): { ast_node(n_, Kind_, node_);
  778. #ifndef case_end
  779. #define case_end } break;
  780. #endif
  781. gb_internal gb_inline bool is_ast_expr(Ast *node) {
  782. return gb_is_between(node->kind, Ast__ExprBegin+1, Ast__ExprEnd-1);
  783. }
  784. gb_internal gb_inline bool is_ast_stmt(Ast *node) {
  785. return gb_is_between(node->kind, Ast__StmtBegin+1, Ast__StmtEnd-1);
  786. }
  787. gb_internal gb_inline bool is_ast_complex_stmt(Ast *node) {
  788. return gb_is_between(node->kind, Ast__ComplexStmtBegin+1, Ast__ComplexStmtEnd-1);
  789. }
  790. gb_internal gb_inline bool is_ast_decl(Ast *node) {
  791. return gb_is_between(node->kind, Ast__DeclBegin+1, Ast__DeclEnd-1);
  792. }
  793. gb_internal gb_inline bool is_ast_type(Ast *node) {
  794. return gb_is_between(node->kind, Ast__TypeBegin+1, Ast__TypeEnd-1);
  795. }
  796. gb_internal gb_inline bool is_ast_when_stmt(Ast *node) {
  797. return node->kind == Ast_WhenStmt;
  798. }
  799. gb_internal gb_inline gbAllocator ast_allocator(AstFile *f) {
  800. return permanent_allocator();
  801. }
  802. gb_internal Ast *alloc_ast_node(AstFile *f, AstKind kind);
  803. gb_internal gbString expr_to_string(Ast *expression);
  804. gb_internal bool allow_field_separator(AstFile *f);
  805. gb_internal void parse_enforce_tabs(AstFile *f);