ast.odin 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. package odin_ast
  2. import "core:odin/token"
  3. Proc_Tag :: enum {
  4. Bounds_Check,
  5. No_Bounds_Check,
  6. Require_Results,
  7. }
  8. Proc_Tags :: distinct bit_set[Proc_Tag; u32];
  9. Proc_Inlining :: enum u32 {
  10. None = 0,
  11. Inline = 1,
  12. No_Inline = 2,
  13. }
  14. Proc_Calling_Convention :: enum i32 {
  15. Invalid = 0,
  16. Odin,
  17. Contextless,
  18. C_Decl,
  19. Std_Call,
  20. Fast_Call,
  21. Foreign_Block_Default = -1,
  22. }
  23. Node_State_Flag :: enum {
  24. Bounds_Check,
  25. No_Bounds_Check,
  26. }
  27. Node_State_Flags :: distinct bit_set[Node_State_Flag];
  28. Comment_Group :: struct {
  29. list: []token.Token,
  30. }
  31. Node :: struct {
  32. pos: token.Pos,
  33. end: token.Pos,
  34. derived: any,
  35. state_flags: Node_State_Flags,
  36. }
  37. Expr :: struct {
  38. using expr_base: Node,
  39. }
  40. Stmt :: struct {
  41. using stmt_base: Node,
  42. }
  43. Decl :: struct {
  44. using decl_base: Stmt,
  45. }
  46. // Expressions
  47. Bad_Expr :: struct {
  48. using node: Expr,
  49. }
  50. Ident :: struct {
  51. using node: Expr,
  52. name: string,
  53. }
  54. Implicit :: struct {
  55. using node: Expr,
  56. tok: token.Token,
  57. }
  58. Undef :: struct {
  59. using node: Expr,
  60. tok: token.Kind,
  61. }
  62. Basic_Lit :: struct {
  63. using node: Expr,
  64. tok: token.Token,
  65. }
  66. Basic_Directive :: struct {
  67. using node: Expr,
  68. tok: token.Token,
  69. name: string,
  70. }
  71. Ellipsis :: struct {
  72. using node: Expr,
  73. tok: token.Kind,
  74. expr: ^Expr,
  75. }
  76. Proc_Lit :: struct {
  77. using node: Expr,
  78. type: ^Proc_Type,
  79. body: ^Stmt,
  80. tags: Proc_Tags,
  81. inlining: Proc_Inlining,
  82. }
  83. Comp_Lit :: struct {
  84. using node: Expr,
  85. type: ^Expr,
  86. open: token.Pos,
  87. elems: []^Expr,
  88. close: token.Pos,
  89. }
  90. Tag_Expr :: struct {
  91. using node: Expr,
  92. op: token.Token,
  93. name: string,
  94. expr: ^Expr,
  95. }
  96. Unary_Expr :: struct {
  97. using node: Expr,
  98. op: token.Token,
  99. expr: ^Expr,
  100. }
  101. Binary_Expr :: struct {
  102. using node: Expr,
  103. left: ^Expr,
  104. op: token.Token,
  105. right: ^Expr,
  106. }
  107. Paren_Expr :: struct {
  108. using node: Expr,
  109. open: token.Pos,
  110. expr: ^Expr,
  111. close: token.Pos,
  112. }
  113. Selector_Expr :: struct {
  114. using node: Expr,
  115. expr: ^Expr,
  116. field: ^Ident,
  117. }
  118. Implicit_Selector_Expr :: struct {
  119. using node: Expr,
  120. field: ^Ident,
  121. }
  122. Index_Expr :: struct {
  123. using node: Expr,
  124. expr: ^Expr,
  125. open: token.Pos,
  126. index: ^Expr,
  127. close: token.Pos,
  128. }
  129. Deref_Expr :: struct {
  130. using node: Expr,
  131. expr: ^Expr,
  132. op: token.Token,
  133. }
  134. Slice_Expr :: struct {
  135. using node: Expr,
  136. expr: ^Expr,
  137. open: token.Pos,
  138. low: ^Expr,
  139. interval: token.Token,
  140. high: ^Expr,
  141. close: token.Pos,
  142. }
  143. Call_Expr :: struct {
  144. using node: Expr,
  145. inlining: Proc_Inlining,
  146. expr: ^Expr,
  147. open: token.Pos,
  148. args: []^Expr,
  149. ellipsis: token.Token,
  150. close: token.Pos,
  151. }
  152. Field_Value :: struct {
  153. using node: Expr,
  154. field: ^Expr,
  155. sep: token.Pos,
  156. value: ^Expr,
  157. }
  158. Ternary_Expr :: struct {
  159. using node: Expr,
  160. cond: ^Expr,
  161. op1: token.Token,
  162. x: ^Expr,
  163. op2: token.Token,
  164. y: ^Expr,
  165. }
  166. Type_Assertion :: struct {
  167. using node: Expr,
  168. expr: ^Expr,
  169. dot: token.Pos,
  170. open: token.Pos,
  171. type: ^Expr,
  172. close: token.Pos,
  173. }
  174. Type_Cast :: struct {
  175. using node: Expr,
  176. tok: token.Token,
  177. open: token.Pos,
  178. type: ^Expr,
  179. close: token.Pos,
  180. expr: ^Expr,
  181. }
  182. Auto_Cast :: struct {
  183. using node: Expr,
  184. op: token.Token,
  185. expr: ^Expr,
  186. }
  187. // Statements
  188. Bad_Stmt :: struct {
  189. using node: Stmt,
  190. }
  191. Empty_Stmt :: struct {
  192. using node: Stmt,
  193. semicolon: token.Pos, // Position of the following ';'
  194. }
  195. Expr_Stmt :: struct {
  196. using node: Stmt,
  197. expr: ^Expr,
  198. }
  199. Tag_Stmt :: struct {
  200. using node: Stmt,
  201. op: token.Token,
  202. name: string,
  203. stmt: ^Stmt,
  204. }
  205. Assign_Stmt :: struct {
  206. using node: Stmt,
  207. lhs: []^Expr,
  208. op: token.Token,
  209. rhs: []^Expr,
  210. }
  211. Block_Stmt :: struct {
  212. using node: Stmt,
  213. label: ^Expr,
  214. open: token.Pos,
  215. stmts: []^Stmt,
  216. close: token.Pos,
  217. }
  218. If_Stmt :: struct {
  219. using node: Stmt,
  220. label: ^Expr,
  221. if_pos: token.Pos,
  222. init: ^Stmt,
  223. cond: ^Expr,
  224. body: ^Stmt,
  225. else_stmt: ^Stmt,
  226. }
  227. When_Stmt :: struct {
  228. using node: Stmt,
  229. when_pos: token.Pos,
  230. cond: ^Expr,
  231. body: ^Stmt,
  232. else_stmt: ^Stmt,
  233. }
  234. Return_Stmt :: struct {
  235. using node: Stmt,
  236. results: []^Expr,
  237. }
  238. Defer_Stmt :: struct {
  239. using node: Stmt,
  240. stmt: ^Stmt,
  241. }
  242. For_Stmt :: struct {
  243. using node: Stmt,
  244. label: ^Expr,
  245. for_pos: token.Pos,
  246. init: ^Stmt,
  247. cond: ^Expr,
  248. post: ^Stmt,
  249. body: ^Stmt,
  250. }
  251. Range_Stmt :: struct {
  252. using node: Stmt,
  253. label: ^Expr,
  254. for_pos: token.Pos,
  255. val0: ^Expr,
  256. val1: ^Expr,
  257. in_pos: token.Pos,
  258. expr: ^Expr,
  259. body: ^Stmt,
  260. }
  261. Case_Clause :: struct {
  262. using node: Stmt,
  263. case_pos: token.Pos,
  264. list: []^Expr,
  265. terminator: token.Token,
  266. body: []^Stmt,
  267. }
  268. Switch_Stmt :: struct {
  269. using node: Stmt,
  270. label: ^Expr,
  271. switch_pos: token.Pos,
  272. init: ^Stmt,
  273. cond: ^Expr,
  274. body: ^Stmt,
  275. complete: bool,
  276. }
  277. Type_Switch_Stmt :: struct {
  278. using node: Stmt,
  279. label: ^Expr,
  280. switch_pos: token.Pos,
  281. tag: ^Stmt,
  282. expr: ^Expr,
  283. body: ^Stmt,
  284. complete: bool,
  285. }
  286. Branch_Stmt :: struct {
  287. using node: Stmt,
  288. tok: token.Token,
  289. label: ^Ident,
  290. }
  291. Using_Stmt :: struct {
  292. using node: Stmt,
  293. list: []^Expr,
  294. }
  295. // Declarations
  296. Bad_Decl :: struct {
  297. using node: Decl,
  298. }
  299. Value_Decl :: struct {
  300. using node: Decl,
  301. docs: ^Comment_Group,
  302. attributes: [dynamic]^Attribute, // dynamic as parsing will add to them lazily
  303. names: []^Expr,
  304. type: ^Expr,
  305. values: []^Expr,
  306. comment: ^Comment_Group,
  307. is_using: bool,
  308. is_mutable: bool,
  309. }
  310. Package_Decl :: struct {
  311. using node: Decl,
  312. docs: ^Comment_Group,
  313. token: token.Token,
  314. name: string,
  315. comment: ^Comment_Group,
  316. }
  317. Import_Decl :: struct {
  318. using node: Decl,
  319. docs: ^Comment_Group,
  320. is_using: bool,
  321. import_tok: token.Token,
  322. name: token.Token,
  323. relpath: token.Token,
  324. fullpath: string,
  325. comment: ^Comment_Group,
  326. }
  327. Foreign_Block_Decl :: struct {
  328. using node: Decl,
  329. docs: ^Comment_Group,
  330. attributes: [dynamic]^Attribute, // dynamic as parsing will add to them lazily
  331. tok: token.Token,
  332. foreign_library: ^Expr,
  333. body: ^Stmt,
  334. }
  335. Foreign_Import_Decl :: struct {
  336. using node: Decl,
  337. docs: ^Comment_Group,
  338. foreign_tok: token.Token,
  339. import_tok: token.Token,
  340. name: ^Ident,
  341. collection_name: string,
  342. fullpaths: []string,
  343. comment: ^Comment_Group,
  344. }
  345. // Other things
  346. unparen_expr :: proc(expr: ^Expr) -> ^Expr {
  347. if expr == nil {
  348. return nil;
  349. }
  350. for {
  351. e, ok := expr.derived.(Paren_Expr);
  352. if !ok do break;
  353. expr = e.expr;
  354. }
  355. return expr;
  356. }
  357. Field_Flag :: enum {
  358. Ellipsis,
  359. Using,
  360. No_Alias,
  361. C_Vararg,
  362. Auto_Cast,
  363. In,
  364. Results,
  365. Default_Parameters,
  366. Typeid_Token,
  367. }
  368. Field_Flags :: distinct bit_set[Field_Flag];
  369. Field_Flags_Struct :: Field_Flags{
  370. Field_Flag.Using,
  371. };
  372. Field_Flags_Record_Poly_Params :: Field_Flags{
  373. Field_Flag.Typeid_Token,
  374. };
  375. Field_Flags_Signature :: Field_Flags{
  376. Field_Flag.Ellipsis,
  377. Field_Flag.Using,
  378. Field_Flag.No_Alias,
  379. Field_Flag.C_Vararg,
  380. Field_Flag.Auto_Cast,
  381. Field_Flag.Default_Parameters,
  382. };
  383. Field_Flags_Signature_Params :: Field_Flags_Signature | {Field_Flag.Typeid_Token};
  384. Field_Flags_Signature_Results :: Field_Flags_Signature;
  385. Proc_Group :: struct {
  386. using node: Expr,
  387. tok: token.Token,
  388. open: token.Pos,
  389. args: []^Expr,
  390. close: token.Pos,
  391. }
  392. Attribute :: struct {
  393. using node: Node,
  394. tok: token.Kind,
  395. open: token.Pos,
  396. elems: []^Expr,
  397. close: token.Pos,
  398. }
  399. Field :: struct {
  400. using node: Node,
  401. docs: ^Comment_Group,
  402. names: []^Expr, // Could be polymorphic
  403. type: ^Expr,
  404. default_value: ^Expr,
  405. flags: Field_Flags,
  406. comment: ^Comment_Group,
  407. }
  408. Field_List :: struct {
  409. using node: Node,
  410. open: token.Pos,
  411. list: []^Field,
  412. close: token.Pos,
  413. }
  414. // Types
  415. Typeid_Type :: struct {
  416. using node: Expr,
  417. tok: token.Kind,
  418. specialization: ^Expr,
  419. }
  420. Helper_Type :: struct {
  421. using node: Expr,
  422. tok: token.Kind,
  423. type: ^Expr,
  424. }
  425. Distinct_Type :: struct {
  426. using node: Expr,
  427. tok: token.Kind,
  428. type: ^Expr,
  429. }
  430. Opaque_Type :: struct {
  431. using node: Expr,
  432. tok: token.Kind,
  433. type: ^Expr,
  434. }
  435. Poly_Type :: struct {
  436. using node: Expr,
  437. dollar: token.Pos,
  438. type: ^Ident,
  439. specialization: ^Expr,
  440. }
  441. Proc_Type :: struct {
  442. using node: Expr,
  443. tok: token.Token,
  444. calling_convention: Proc_Calling_Convention,
  445. params: ^Field_List,
  446. arrow: token.Pos,
  447. results: ^Field_List,
  448. tags: Proc_Tags,
  449. generic: bool,
  450. diverging: bool,
  451. }
  452. Pointer_Type :: struct {
  453. using node: Expr,
  454. pointer: token.Pos,
  455. elem: ^Expr,
  456. }
  457. Array_Type :: struct {
  458. using node: Expr,
  459. open: token.Pos,
  460. len: ^Expr, // Ellipsis node for [?]T arrray types, nil for slice types
  461. close: token.Pos,
  462. elem: ^Expr,
  463. }
  464. Dynamic_Array_Type :: struct {
  465. using node: Expr,
  466. open: token.Pos,
  467. dynamic_pos: token.Pos,
  468. close: token.Pos,
  469. elem: ^Expr,
  470. }
  471. Struct_Type :: struct {
  472. using node: Expr,
  473. tok_pos: token.Pos,
  474. poly_params: ^Field_List,
  475. align: ^Expr,
  476. is_packed: bool,
  477. is_raw_union: bool,
  478. fields: ^Field_List,
  479. name_count: int,
  480. }
  481. Union_Type :: struct {
  482. using node: Expr,
  483. tok_pos: token.Pos,
  484. poly_params: ^Field_List,
  485. align: ^Expr,
  486. variants: []^Expr,
  487. }
  488. Enum_Type :: struct {
  489. using node: Expr,
  490. tok_pos: token.Pos,
  491. base_type: ^Expr,
  492. open: token.Pos,
  493. fields: []^Expr,
  494. close: token.Pos,
  495. is_using: bool,
  496. }
  497. Bit_Field_Type :: struct {
  498. using node: Expr,
  499. tok_pos: token.Pos,
  500. align: ^Expr,
  501. open: token.Pos,
  502. fields: []^Field_Value, // Field_Value with ':' rather than '='
  503. close: token.Pos,
  504. }
  505. Bit_Set_Type :: struct {
  506. using node: Expr,
  507. tok_pos: token.Pos,
  508. open: token.Pos,
  509. elem: ^Expr,
  510. underlying: ^Expr,
  511. close: token.Pos,
  512. }
  513. Map_Type :: struct {
  514. using node: Expr,
  515. tok_pos: token.Pos,
  516. key: ^Expr,
  517. value: ^Expr,
  518. }