type.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. struct Scope;
  2. enum BasicKind {
  3. Basic_Invalid,
  4. Basic_bool,
  5. Basic_i8,
  6. Basic_i16,
  7. Basic_i32,
  8. Basic_i64,
  9. Basic_i128,
  10. Basic_u8,
  11. Basic_u16,
  12. Basic_u32,
  13. Basic_u64,
  14. Basic_u128,
  15. Basic_f32,
  16. Basic_f64,
  17. Basic_int,
  18. Basic_uint,
  19. Basic_rawptr,
  20. Basic_string,
  21. Basic_any,
  22. Basic_UntypedBool,
  23. Basic_UntypedInteger,
  24. Basic_UntypedFloat,
  25. Basic_UntypedPointer,
  26. Basic_UntypedString,
  27. Basic_UntypedRune,
  28. Basic_Count,
  29. Basic_byte = Basic_u8,
  30. Basic_rune = Basic_i32,
  31. };
  32. enum BasicFlag : u32 {
  33. BasicFlag_Boolean = GB_BIT(0),
  34. BasicFlag_Integer = GB_BIT(1),
  35. BasicFlag_Unsigned = GB_BIT(2),
  36. BasicFlag_Float = GB_BIT(3),
  37. BasicFlag_Pointer = GB_BIT(4),
  38. BasicFlag_String = GB_BIT(5),
  39. BasicFlag_Rune = GB_BIT(6),
  40. BasicFlag_Untyped = GB_BIT(7),
  41. BasicFlag_Numeric = BasicFlag_Integer | BasicFlag_Float,
  42. BasicFlag_Ordered = BasicFlag_Numeric | BasicFlag_String | BasicFlag_Pointer,
  43. BasicFlag_ConstantType = BasicFlag_Boolean | BasicFlag_Numeric | BasicFlag_Pointer | BasicFlag_String | BasicFlag_Rune,
  44. };
  45. struct BasicType {
  46. BasicKind kind;
  47. u32 flags;
  48. String name;
  49. };
  50. #define TYPE_KINDS \
  51. TYPE_KIND(Invalid), \
  52. TYPE_KIND(Basic), \
  53. TYPE_KIND(Array), \
  54. TYPE_KIND(Vector), \
  55. TYPE_KIND(Slice), \
  56. TYPE_KIND(Record), \
  57. TYPE_KIND(Pointer), \
  58. TYPE_KIND(Named), \
  59. TYPE_KIND(Tuple), \
  60. TYPE_KIND(Proc), \
  61. TYPE_KIND(Count),
  62. enum TypeKind {
  63. #define TYPE_KIND(k) GB_JOIN2(Type_, k)
  64. TYPE_KINDS
  65. #undef TYPE_KIND
  66. };
  67. String const type_strings[] = {
  68. #define TYPE_KIND(k) {cast(u8 *)#k, gb_size_of(#k)-1}
  69. TYPE_KINDS
  70. #undef TYPE_KIND
  71. };
  72. enum TypeFlag {
  73. TypeFlag_thread_local = GB_BIT(0),
  74. TypeFlag_volatile = GB_BIT(1),
  75. };
  76. enum TypeRecordKind {
  77. TypeRecord_Invalid,
  78. TypeRecord_Struct,
  79. TypeRecord_Enum,
  80. TypeRecord_RawUnion,
  81. TypeRecord_Union, // Tagged
  82. TypeRecord_Count,
  83. };
  84. struct Type {
  85. u32 flags;
  86. TypeKind kind;
  87. union {
  88. BasicType Basic;
  89. struct {
  90. Type *elem;
  91. i64 count;
  92. } Array;
  93. struct {
  94. Type *elem;
  95. i64 count;
  96. } Vector;
  97. struct {
  98. Type *elem;
  99. } Slice;
  100. struct {
  101. TypeRecordKind kind;
  102. // All record types
  103. // Theses are arrays
  104. Entity **fields; // Entity_Variable (otherwise Entity_TypeName if union)
  105. isize field_count; // == offset_count is struct
  106. AstNode *node;
  107. // enum only
  108. Type * enum_base; // Default is `int`
  109. // struct only
  110. i64 * struct_offsets;
  111. b32 struct_are_offsets_set;
  112. b32 struct_is_packed;
  113. b32 struct_is_ordered;
  114. Entity **fields_in_src_order; // Entity_Variable
  115. // Entity_Constant or Entity_TypeName
  116. Entity **other_fields;
  117. isize other_field_count;
  118. } Record;
  119. struct { Type *elem; } Pointer;
  120. struct {
  121. String name;
  122. Type * base;
  123. Entity *type_name; // Entity_TypeName
  124. } Named;
  125. struct {
  126. Entity **variables; // Entity_Variable
  127. isize variable_count;
  128. } Tuple;
  129. struct {
  130. Scope *scope;
  131. Type * params; // Type_Tuple
  132. Type * results; // Type_Tuple
  133. isize param_count;
  134. isize result_count;
  135. b32 variadic;
  136. } Proc;
  137. };
  138. };
  139. gbString type_to_string(Type *type, gbAllocator a = gb_heap_allocator());
  140. Type *get_base_type(Type *t) {
  141. for (;;) {
  142. if (t == NULL || t->kind != Type_Named) {
  143. break;
  144. }
  145. t = t->Named.base;
  146. }
  147. return t;
  148. }
  149. void set_base_type(Type *t, Type *base) {
  150. if (t && t->kind == Type_Named) {
  151. t->Named.base = base;
  152. }
  153. }
  154. Type *alloc_type(gbAllocator a, TypeKind kind) {
  155. Type *t = gb_alloc_item(a, Type);
  156. t->kind = kind;
  157. return t;
  158. }
  159. Type *make_type_basic(gbAllocator a, BasicType basic) {
  160. Type *t = alloc_type(a, Type_Basic);
  161. t->Basic = basic;
  162. return t;
  163. }
  164. Type *make_type_array(gbAllocator a, Type *elem, i64 count) {
  165. Type *t = alloc_type(a, Type_Array);
  166. t->Array.elem = elem;
  167. t->Array.count = count;
  168. return t;
  169. }
  170. Type *make_type_vector(gbAllocator a, Type *elem, i64 count) {
  171. Type *t = alloc_type(a, Type_Vector);
  172. t->Vector.elem = elem;
  173. t->Vector.count = count;
  174. return t;
  175. }
  176. Type *make_type_slice(gbAllocator a, Type *elem) {
  177. Type *t = alloc_type(a, Type_Slice);
  178. t->Array.elem = elem;
  179. return t;
  180. }
  181. Type *make_type_struct(gbAllocator a) {
  182. Type *t = alloc_type(a, Type_Record);
  183. t->Record.kind = TypeRecord_Struct;
  184. return t;
  185. }
  186. Type *make_type_union(gbAllocator a) {
  187. Type *t = alloc_type(a, Type_Record);
  188. t->Record.kind = TypeRecord_Union;
  189. return t;
  190. }
  191. Type *make_type_raw_union(gbAllocator a) {
  192. Type *t = alloc_type(a, Type_Record);
  193. t->Record.kind = TypeRecord_RawUnion;
  194. return t;
  195. }
  196. Type *make_type_enum(gbAllocator a) {
  197. Type *t = alloc_type(a, Type_Record);
  198. t->Record.kind = TypeRecord_Enum;
  199. return t;
  200. }
  201. Type *make_type_pointer(gbAllocator a, Type *elem) {
  202. Type *t = alloc_type(a, Type_Pointer);
  203. t->Pointer.elem = elem;
  204. return t;
  205. }
  206. Type *make_type_named(gbAllocator a, String name, Type *base, Entity *type_name) {
  207. Type *t = alloc_type(a, Type_Named);
  208. t->Named.name = name;
  209. t->Named.base = base;
  210. t->Named.type_name = type_name;
  211. return t;
  212. }
  213. Type *make_type_tuple(gbAllocator a) {
  214. Type *t = alloc_type(a, Type_Tuple);
  215. return t;
  216. }
  217. Type *make_type_proc(gbAllocator a, Scope *scope, Type *params, isize param_count, Type *results, isize result_count, b32 variadic) {
  218. Type *t = alloc_type(a, Type_Proc);
  219. if (variadic) {
  220. if (param_count == 0) {
  221. GB_PANIC("variadic procedure must have at least one parameter");
  222. }
  223. GB_ASSERT(params != NULL && params->kind == Type_Tuple);
  224. Entity *e = params->Tuple.variables[param_count-1];
  225. if (get_base_type(e->type)->kind != Type_Slice) {
  226. // NOTE(bill): For custom calling convention
  227. GB_PANIC("variadic parameter must be of type slice");
  228. }
  229. }
  230. t->Proc.scope = scope;
  231. t->Proc.params = params;
  232. t->Proc.param_count = param_count;
  233. t->Proc.results = results;
  234. t->Proc.result_count = result_count;
  235. t->Proc.variadic = variadic;
  236. return t;
  237. }
  238. Type *type_deref(Type *t) {
  239. if (t != NULL) {
  240. Type *bt = get_base_type(t);
  241. if (bt == NULL)
  242. return NULL;
  243. if (bt != NULL && bt->kind == Type_Pointer)
  244. return bt->Pointer.elem;
  245. }
  246. return t;
  247. }
  248. #define STR_LIT(x) {cast(u8 *)(x), gb_size_of(x)-1}
  249. gb_global Type basic_types[] = {
  250. {0, Type_Basic, {Basic_Invalid, 0, STR_LIT("invalid type")}},
  251. {0, Type_Basic, {Basic_bool, BasicFlag_Boolean, STR_LIT("bool")}},
  252. {0, Type_Basic, {Basic_i8, BasicFlag_Integer, STR_LIT("i8")}},
  253. {0, Type_Basic, {Basic_i16, BasicFlag_Integer, STR_LIT("i16")}},
  254. {0, Type_Basic, {Basic_i32, BasicFlag_Integer, STR_LIT("i32")}},
  255. {0, Type_Basic, {Basic_i64, BasicFlag_Integer, STR_LIT("i64")}},
  256. {0, Type_Basic, {Basic_i128, BasicFlag_Integer, STR_LIT("i128")}},
  257. {0, Type_Basic, {Basic_u8, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("u8")}},
  258. {0, Type_Basic, {Basic_u16, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("u16")}},
  259. {0, Type_Basic, {Basic_u32, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("u32")}},
  260. {0, Type_Basic, {Basic_u64, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("u64")}},
  261. {0, Type_Basic, {Basic_u128, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("u128")}},
  262. {0, Type_Basic, {Basic_f32, BasicFlag_Float, STR_LIT("f32")}},
  263. {0, Type_Basic, {Basic_f64, BasicFlag_Float, STR_LIT("f64")}},
  264. {0, Type_Basic, {Basic_int, BasicFlag_Integer, STR_LIT("int")}},
  265. {0, Type_Basic, {Basic_uint, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("uint")}},
  266. {0, Type_Basic, {Basic_rawptr, BasicFlag_Pointer, STR_LIT("rawptr")}},
  267. {0, Type_Basic, {Basic_string, BasicFlag_String, STR_LIT("string")}},
  268. {0, Type_Basic, {Basic_any, 0, STR_LIT("any")}},
  269. {0, Type_Basic, {Basic_UntypedBool, BasicFlag_Boolean | BasicFlag_Untyped, STR_LIT("untyped bool")}},
  270. {0, Type_Basic, {Basic_UntypedInteger, BasicFlag_Integer | BasicFlag_Untyped, STR_LIT("untyped integer")}},
  271. {0, Type_Basic, {Basic_UntypedFloat, BasicFlag_Float | BasicFlag_Untyped, STR_LIT("untyped float")}},
  272. {0, Type_Basic, {Basic_UntypedPointer, BasicFlag_Pointer | BasicFlag_Untyped, STR_LIT("untyped pointer")}},
  273. {0, Type_Basic, {Basic_UntypedString, BasicFlag_String | BasicFlag_Untyped, STR_LIT("untyped string")}},
  274. {0, Type_Basic, {Basic_UntypedRune, BasicFlag_Integer | BasicFlag_Untyped, STR_LIT("untyped rune")}},
  275. };
  276. gb_global Type basic_type_aliases[] = {
  277. {0, Type_Basic, {Basic_byte, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("byte")}},
  278. {0, Type_Basic, {Basic_rune, BasicFlag_Integer, STR_LIT("rune")}},
  279. };
  280. gb_global Type *t_invalid = &basic_types[Basic_Invalid];
  281. gb_global Type *t_bool = &basic_types[Basic_bool];
  282. gb_global Type *t_i8 = &basic_types[Basic_i8];
  283. gb_global Type *t_i16 = &basic_types[Basic_i16];
  284. gb_global Type *t_i32 = &basic_types[Basic_i32];
  285. gb_global Type *t_i64 = &basic_types[Basic_i64];
  286. gb_global Type *t_i128 = &basic_types[Basic_i128];
  287. gb_global Type *t_u8 = &basic_types[Basic_u8];
  288. gb_global Type *t_u16 = &basic_types[Basic_u16];
  289. gb_global Type *t_u32 = &basic_types[Basic_u32];
  290. gb_global Type *t_u64 = &basic_types[Basic_u64];
  291. gb_global Type *t_u128 = &basic_types[Basic_u128];
  292. gb_global Type *t_f32 = &basic_types[Basic_f32];
  293. gb_global Type *t_f64 = &basic_types[Basic_f64];
  294. gb_global Type *t_int = &basic_types[Basic_int];
  295. gb_global Type *t_uint = &basic_types[Basic_uint];
  296. gb_global Type *t_rawptr = &basic_types[Basic_rawptr];
  297. gb_global Type *t_string = &basic_types[Basic_string];
  298. gb_global Type *t_any = &basic_types[Basic_any];
  299. gb_global Type *t_untyped_bool = &basic_types[Basic_UntypedBool];
  300. gb_global Type *t_untyped_integer = &basic_types[Basic_UntypedInteger];
  301. gb_global Type *t_untyped_float = &basic_types[Basic_UntypedFloat];
  302. gb_global Type *t_untyped_pointer = &basic_types[Basic_UntypedPointer];
  303. gb_global Type *t_untyped_string = &basic_types[Basic_UntypedString];
  304. gb_global Type *t_untyped_rune = &basic_types[Basic_UntypedRune];
  305. gb_global Type *t_byte = &basic_type_aliases[Basic_byte];
  306. gb_global Type *t_rune = &basic_type_aliases[Basic_rune];
  307. gb_global Type *t_type_info = NULL;
  308. gb_global Type *t_type_info_ptr = NULL;
  309. gb_global Type *t_type_info_member = NULL;
  310. gb_global Type *t_type_info_member_ptr = NULL;
  311. gb_global Type *t_type_info_named = NULL;
  312. gb_global Type *t_type_info_integer = NULL;
  313. gb_global Type *t_type_info_float = NULL;
  314. gb_global Type *t_type_info_string = NULL;
  315. gb_global Type *t_type_info_boolean = NULL;
  316. gb_global Type *t_type_info_pointer = NULL;
  317. gb_global Type *t_type_info_procedure = NULL;
  318. gb_global Type *t_type_info_array = NULL;
  319. gb_global Type *t_type_info_slice = NULL;
  320. gb_global Type *t_type_info_vector = NULL;
  321. gb_global Type *t_type_info_tuple = NULL;
  322. gb_global Type *t_type_info_struct = NULL;
  323. gb_global Type *t_type_info_union = NULL;
  324. gb_global Type *t_type_info_raw_union = NULL;
  325. gb_global Type *t_type_info_enum = NULL;
  326. gb_global Type *t_type_info_any = NULL;
  327. b32 is_type_named(Type *t) {
  328. if (t->kind == Type_Basic)
  329. return true;
  330. return t->kind == Type_Named;
  331. }
  332. b32 is_type_boolean(Type *t) {
  333. t = get_base_type(t);
  334. if (t->kind == Type_Basic)
  335. return (t->Basic.flags & BasicFlag_Boolean) != 0;
  336. return false;
  337. }
  338. b32 is_type_integer(Type *t) {
  339. t = get_base_type(t);
  340. if (t->kind == Type_Basic)
  341. return (t->Basic.flags & BasicFlag_Integer) != 0;
  342. return false;
  343. }
  344. b32 is_type_unsigned(Type *t) {
  345. t = get_base_type(t);
  346. if (t->kind == Type_Basic)
  347. return (t->Basic.flags & BasicFlag_Unsigned) != 0;
  348. return false;
  349. }
  350. b32 is_type_numeric(Type *t) {
  351. t = get_base_type(t);
  352. if (t->kind == Type_Basic)
  353. return (t->Basic.flags & BasicFlag_Numeric) != 0;
  354. if (t->kind == Type_Vector)
  355. return is_type_numeric(t->Vector.elem);
  356. return false;
  357. }
  358. b32 is_type_string(Type *t) {
  359. t = get_base_type(t);
  360. if (t->kind == Type_Basic)
  361. return (t->Basic.flags & BasicFlag_String) != 0;
  362. return false;
  363. }
  364. b32 is_type_typed(Type *t) {
  365. t = get_base_type(t);
  366. if (t->kind == Type_Basic)
  367. return (t->Basic.flags & BasicFlag_Untyped) == 0;
  368. return true;
  369. }
  370. b32 is_type_untyped(Type *t) {
  371. t = get_base_type(t);
  372. if (t->kind == Type_Basic)
  373. return (t->Basic.flags & BasicFlag_Untyped) != 0;
  374. return false;
  375. }
  376. b32 is_type_ordered(Type *t) {
  377. t = get_base_type(t);
  378. if (t->kind == Type_Basic)
  379. return (t->Basic.flags & BasicFlag_Ordered) != 0;
  380. if (t->kind == Type_Pointer)
  381. return true;
  382. return false;
  383. }
  384. b32 is_type_constant_type(Type *t) {
  385. t = get_base_type(t);
  386. if (t->kind == Type_Basic)
  387. return (t->Basic.flags & BasicFlag_ConstantType) != 0;
  388. return false;
  389. }
  390. b32 is_type_float(Type *t) {
  391. t = get_base_type(t);
  392. if (t->kind == Type_Basic)
  393. return (t->Basic.flags & BasicFlag_Float) != 0;
  394. return false;
  395. }
  396. b32 is_type_pointer(Type *t) {
  397. t = get_base_type(t);
  398. if (t->kind == Type_Basic)
  399. return (t->Basic.flags & BasicFlag_Pointer) != 0;
  400. return t->kind == Type_Pointer;
  401. }
  402. b32 is_type_int_or_uint(Type *t) {
  403. if (t->kind == Type_Basic)
  404. return (t->Basic.kind == Basic_int) || (t->Basic.kind == Basic_uint);
  405. return false;
  406. }
  407. b32 is_type_rawptr(Type *t) {
  408. if (t->kind == Type_Basic)
  409. return t->Basic.kind == Basic_rawptr;
  410. return false;
  411. }
  412. b32 is_type_u8(Type *t) {
  413. if (t->kind == Type_Basic)
  414. return t->Basic.kind == Basic_u8;
  415. return false;
  416. }
  417. b32 is_type_slice(Type *t) {
  418. t = get_base_type(t);
  419. return t->kind == Type_Slice;
  420. }
  421. b32 is_type_u8_slice(Type *t) {
  422. t = get_base_type(t);
  423. if (t->kind == Type_Slice)
  424. return is_type_u8(t->Slice.elem);
  425. return false;
  426. }
  427. b32 is_type_vector(Type *t) {
  428. return t->kind == Type_Vector;
  429. }
  430. b32 is_type_proc(Type *t) {
  431. t = get_base_type(t);
  432. return t->kind == Type_Proc;
  433. }
  434. Type *base_vector_type(Type *t) {
  435. if (is_type_vector(t)) {
  436. return t->Vector.elem;
  437. }
  438. return t;
  439. }
  440. b32 is_type_enum(Type *t) {
  441. t = get_base_type(t);
  442. return (t->kind == Type_Record && t->Record.kind == TypeRecord_Enum);
  443. }
  444. b32 is_type_struct(Type *t) {
  445. t = get_base_type(t);
  446. return (t->kind == Type_Record && t->Record.kind == TypeRecord_Struct);
  447. }
  448. b32 is_type_union(Type *t) {
  449. t = get_base_type(t);
  450. return (t->kind == Type_Record && t->Record.kind == TypeRecord_Union);
  451. }
  452. b32 is_type_raw_union(Type *t) {
  453. t = get_base_type(t);
  454. return (t->kind == Type_Record && t->Record.kind == TypeRecord_RawUnion);
  455. }
  456. Type *get_enum_base_type(Type *t) {
  457. Type *bt = get_base_type(t);
  458. if (is_type_enum(bt)) {
  459. return bt->Record.enum_base;
  460. }
  461. return t;
  462. }
  463. b32 is_type_any(Type *t) {
  464. t = get_base_type(t);
  465. return (t->kind == Type_Basic && t->Basic.kind == Basic_any);
  466. }
  467. b32 is_type_comparable(Type *t) {
  468. t = get_base_type(t);
  469. switch (t->kind) {
  470. case Type_Basic:
  471. return true;
  472. case Type_Pointer:
  473. return true;
  474. case Type_Record: {
  475. if (false && is_type_struct(t)) {
  476. // TODO(bill): Should I even allow this?
  477. for (isize i = 0; i < t->Record.field_count; i++) {
  478. if (!is_type_comparable(t->Record.fields[i]->type))
  479. return false;
  480. }
  481. } else if (is_type_enum(t)) {
  482. return is_type_comparable(t->Record.enum_base);
  483. }
  484. return false;
  485. } break;
  486. case Type_Array:
  487. return is_type_comparable(t->Array.elem);
  488. case Type_Vector:
  489. return is_type_comparable(t->Vector.elem);
  490. case Type_Proc:
  491. return true;
  492. }
  493. return false;
  494. }
  495. b32 are_types_identical(Type *x, Type *y) {
  496. if (x == y)
  497. return true;
  498. if ((x == NULL && y != NULL) ||
  499. (x != NULL && y == NULL)) {
  500. return false;
  501. }
  502. switch (x->kind) {
  503. case Type_Basic:
  504. if (y->kind == Type_Basic)
  505. return x->Basic.kind == y->Basic.kind;
  506. break;
  507. case Type_Array:
  508. if (y->kind == Type_Array)
  509. return (x->Array.count == y->Array.count) && are_types_identical(x->Array.elem, y->Array.elem);
  510. break;
  511. case Type_Vector:
  512. if (y->kind == Type_Vector)
  513. return (x->Vector.count == y->Vector.count) && are_types_identical(x->Vector.elem, y->Vector.elem);
  514. break;
  515. case Type_Slice:
  516. if (y->kind == Type_Slice)
  517. return are_types_identical(x->Slice.elem, y->Slice.elem);
  518. break;
  519. case Type_Record:
  520. if (y->kind == Type_Record) {
  521. if (x->Record.kind == y->Record.kind) {
  522. switch (x->Record.kind) {
  523. case TypeRecord_Struct:
  524. case TypeRecord_RawUnion:
  525. case TypeRecord_Union:
  526. if (x->Record.field_count == y->Record.field_count) {
  527. for (isize i = 0; i < x->Record.field_count; i++) {
  528. if (!are_types_identical(x->Record.fields[i]->type, y->Record.fields[i]->type)) {
  529. return false;
  530. }
  531. }
  532. return true;
  533. }
  534. break;
  535. case TypeRecord_Enum:
  536. return are_types_identical(x->Record.enum_base, y->Record.enum_base);
  537. }
  538. }
  539. }
  540. break;
  541. case Type_Pointer:
  542. if (y->kind == Type_Pointer)
  543. return are_types_identical(x->Pointer.elem, y->Pointer.elem);
  544. break;
  545. case Type_Named:
  546. if (y->kind == Type_Named) {
  547. return x->Named.base == y->Named.base;
  548. }
  549. break;
  550. case Type_Tuple:
  551. if (y->kind == Type_Tuple) {
  552. if (x->Tuple.variable_count == y->Tuple.variable_count) {
  553. for (isize i = 0; i < x->Tuple.variable_count; i++) {
  554. if (!are_types_identical(x->Tuple.variables[i]->type, y->Tuple.variables[i]->type))
  555. return false;
  556. }
  557. return true;
  558. }
  559. }
  560. break;
  561. case Type_Proc:
  562. if (y->kind == Type_Proc) {
  563. return are_types_identical(x->Proc.params, y->Proc.params) &&
  564. are_types_identical(x->Proc.results, y->Proc.results);
  565. }
  566. break;
  567. }
  568. return false;
  569. }
  570. Type *default_type(Type *type) {
  571. if (type->kind == Type_Basic) {
  572. switch (type->Basic.kind) {
  573. case Basic_UntypedBool: return &basic_types[Basic_bool];
  574. case Basic_UntypedInteger: return &basic_types[Basic_int];
  575. case Basic_UntypedFloat: return &basic_types[Basic_f64];
  576. case Basic_UntypedString: return &basic_types[Basic_string];
  577. case Basic_UntypedRune: return &basic_types[Basic_rune];
  578. case Basic_UntypedPointer: return &basic_types[Basic_rawptr];
  579. }
  580. }
  581. return type;
  582. }
  583. // NOTE(bill): Internal sizes of certain types
  584. // string: 2*word_size (ptr+len)
  585. // slice: 3*word_size (ptr+len+cap)
  586. // array: count*size_of(elem) aligned
  587. // NOTE(bill): Alignment of structures and other types are to be compatible with C
  588. struct BaseTypeSizes {
  589. i64 word_size;
  590. i64 max_align;
  591. };
  592. // TODO(bill): Change
  593. gb_global i64 basic_type_sizes[] = {
  594. 0, // Basic_Invalid
  595. 1, // Basic_bool
  596. 1, // Basic_i8
  597. 2, // Basic_i16
  598. 4, // Basic_i32
  599. 8, // Basic_i64
  600. 16, // Basic_i128
  601. 1, // Basic_u8
  602. 2, // Basic_u16
  603. 4, // Basic_u32
  604. 8, // Basic_u64
  605. 16, // Basic_u128
  606. 4, // Basic_f32
  607. 8, // Basic_f64
  608. };
  609. struct Selection {
  610. Entity *entity;
  611. gbArray(isize) index;
  612. b32 indirect; // Set if there was a pointer deref anywhere down the line
  613. };
  614. Selection empty_selection = {};
  615. Selection make_selection(Entity *entity, gbArray(isize) index, b32 indirect) {
  616. Selection s = {entity, index, indirect};
  617. return s;
  618. }
  619. void selection_add_index(Selection *s, isize index) {
  620. if (s->index == NULL) {
  621. gb_array_init(s->index, gb_heap_allocator());
  622. }
  623. gb_array_append(s->index, index);
  624. }
  625. gb_global Entity *entity__any_type_info = NULL;
  626. gb_global Entity *entity__any_data = NULL;
  627. Selection lookup_field(Type *type_, String field_name, b32 is_type, Selection sel = empty_selection) {
  628. GB_ASSERT(type_ != NULL);
  629. if (are_strings_equal(field_name, make_string("_"))) {
  630. return empty_selection;
  631. }
  632. Type *type = type_deref(type_);
  633. b32 is_ptr = type != type_;
  634. type = get_base_type(type);
  635. if (type->kind == Type_Basic) {
  636. switch (type->Basic.kind) {
  637. case Basic_any: {
  638. String type_info_str = make_string("type_info");
  639. String data_str = make_string("data");
  640. if (entity__any_type_info == NULL) {
  641. Token token = {Token_Identifier};
  642. token.string = type_info_str;
  643. entity__any_type_info = make_entity_field(gb_heap_allocator(), NULL, token, t_type_info_ptr, false, 0);
  644. }
  645. if (entity__any_data == NULL) {
  646. Token token = {Token_Identifier};
  647. token.string = data_str;
  648. entity__any_data = make_entity_field(gb_heap_allocator(), NULL, token, t_rawptr, false, 1);
  649. }
  650. if (are_strings_equal(field_name, type_info_str)) {
  651. selection_add_index(&sel, 0);
  652. sel.entity = entity__any_type_info;
  653. return sel;
  654. } else if (are_strings_equal(field_name, data_str)) {
  655. selection_add_index(&sel, 1);
  656. sel.entity = entity__any_data;
  657. return sel;
  658. }
  659. } break;
  660. }
  661. return sel;
  662. }
  663. if (type->kind != Type_Record) {
  664. return sel;
  665. }
  666. if (is_type) {
  667. if (is_type_union(type)) {
  668. for (isize i = 0; i < type->Record.field_count; i++) {
  669. Entity *f = type->Record.fields[i];
  670. GB_ASSERT(f->kind == Entity_TypeName);
  671. String str = f->token.string;
  672. if (are_strings_equal(field_name, str)) {
  673. return make_selection(f, NULL, i);
  674. }
  675. }
  676. }
  677. for (isize i = 0; i < type->Record.other_field_count; i++) {
  678. Entity *f = type->Record.other_fields[i];
  679. GB_ASSERT(f->kind != Entity_Variable);
  680. String str = f->token.string;
  681. if (are_strings_equal(field_name, str)) {
  682. return make_selection(f, NULL, i);
  683. }
  684. }
  685. } else if (!is_type_enum(type) && !is_type_union(type)) {
  686. for (isize i = 0; i < type->Record.field_count; i++) {
  687. Entity *f = type->Record.fields[i];
  688. GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field);
  689. String str = f->token.string;
  690. if (are_strings_equal(field_name, str)) {
  691. selection_add_index(&sel, i);
  692. sel.entity = f;
  693. return sel;
  694. }
  695. if (f->Variable.anonymous) {
  696. isize prev_count = 0;
  697. if (sel.index != NULL) {
  698. prev_count = gb_array_count(sel.index);
  699. }
  700. selection_add_index(&sel, i); // HACK(bill): Leaky memory
  701. sel = lookup_field(f->type, field_name, is_type, sel);
  702. if (sel.entity != NULL) {
  703. if (is_type_pointer(f->type))
  704. sel.indirect = true;
  705. return sel;
  706. }
  707. gb_array_count(sel.index) = prev_count;
  708. }
  709. }
  710. }
  711. return sel;
  712. }
  713. i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t);
  714. i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t);
  715. i64 type_offset_of(BaseTypeSizes s, gbAllocator allocator, Type *t, i64 index);
  716. i64 align_formula(i64 size, i64 align) {
  717. i64 result = size + align-1;
  718. return result - result%align;
  719. }
  720. i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
  721. t = get_base_type(t);
  722. switch (t->kind) {
  723. case Type_Array:
  724. return type_align_of(s, allocator, t->Array.elem);
  725. case Type_Vector: {
  726. i64 size = type_size_of(s, allocator, t->Vector.elem);
  727. size *= t->Vector.count;
  728. size = next_pow2(size);
  729. // TODO(bill): Type_Vector type_align_of
  730. return gb_clamp(size, s.max_align, 4*s.max_align);
  731. } break;
  732. case Type_Record: {
  733. switch (t->Record.kind) {
  734. case TypeRecord_Struct:
  735. if (!t->Record.struct_is_packed) {
  736. i64 max = 1;
  737. for (isize i = 0; i < t->Record.field_count; i++) {
  738. i64 align = type_align_of(s, allocator, t->Record.fields[i]->type);
  739. if (max < align)
  740. max = align;
  741. }
  742. return max;
  743. }
  744. break;
  745. case TypeRecord_Union: {
  746. i64 max = s.word_size;
  747. for (isize i = 1; i < t->Record.field_count; i++) {
  748. // NOTE(bill): field zero is null
  749. i64 align = type_align_of(s, allocator, t->Record.fields[i]->type);
  750. if (max < align)
  751. max = align;
  752. }
  753. return max;
  754. } break;
  755. case TypeRecord_RawUnion: {
  756. i64 max = 1;
  757. for (isize i = 0; i < t->Record.field_count; i++) {
  758. i64 align = type_align_of(s, allocator, t->Record.fields[i]->type);
  759. if (max < align)
  760. max = align;
  761. }
  762. return max;
  763. } break;
  764. case TypeRecord_Enum:
  765. return type_align_of(s, allocator, t->Record.enum_base);
  766. }
  767. } break;
  768. }
  769. return gb_clamp(next_pow2(type_size_of(s, allocator, t)), 1, s.max_align);
  770. }
  771. i64 *type_set_offsets_of(BaseTypeSizes s, gbAllocator allocator, Entity **fields, isize field_count, b32 is_packed) {
  772. // TODO(bill): use arena allocation
  773. i64 *offsets = gb_alloc_array(allocator, i64, field_count);
  774. i64 curr_offset = 0;
  775. if (is_packed) {
  776. for (isize i = 0; i < field_count; i++) {
  777. offsets[i] = curr_offset;
  778. curr_offset += type_size_of(s, allocator, fields[i]->type);
  779. }
  780. } else {
  781. for (isize i = 0; i < field_count; i++) {
  782. i64 align = type_align_of(s, allocator, fields[i]->type);
  783. curr_offset = align_formula(curr_offset, align);
  784. offsets[i] = curr_offset;
  785. curr_offset += type_size_of(s, allocator, fields[i]->type);
  786. }
  787. }
  788. return offsets;
  789. }
  790. b32 type_set_offsets(BaseTypeSizes s, gbAllocator allocator, Type *t) {
  791. GB_ASSERT(is_type_struct(t));
  792. if (!t->Record.struct_are_offsets_set) {
  793. t->Record.struct_offsets = type_set_offsets_of(s, allocator, t->Record.fields, t->Record.field_count, t->Record.struct_is_packed);
  794. t->Record.struct_are_offsets_set = true;
  795. return true;
  796. }
  797. return false;
  798. }
  799. i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
  800. t = get_base_type(t);
  801. switch (t->kind) {
  802. case Type_Basic: {
  803. GB_ASSERT(is_type_typed(t));
  804. BasicKind kind = t->Basic.kind;
  805. if (kind < gb_count_of(basic_type_sizes)) {
  806. i64 size = basic_type_sizes[kind];
  807. if (size > 0)
  808. return size;
  809. }
  810. if (kind == Basic_string) {
  811. return 2 * s.word_size;
  812. } else if (kind == Basic_any) {
  813. return 2 * s.word_size;
  814. }
  815. } break;
  816. case Type_Array: {
  817. i64 count = t->Array.count;
  818. if (count == 0)
  819. return 0;
  820. i64 align = type_align_of(s, allocator, t->Array.elem);
  821. i64 size = type_size_of(s, allocator, t->Array.elem);
  822. i64 alignment = align_formula(size, align);
  823. return alignment*(count-1) + size;
  824. } break;
  825. case Type_Vector: {
  826. i64 count = t->Vector.count;
  827. if (count == 0)
  828. return 0;
  829. // i64 align = type_align_of(s, allocator, t->Vector.elem);
  830. i64 bit_size = 8*type_size_of(s, allocator, t->Vector.elem);
  831. if (is_type_boolean(t->Vector.elem)) {
  832. bit_size = 1; // NOTE(bill): LLVM can store booleans as 1 bit because a boolean _is_ an `i1`
  833. // Silly LLVM spec
  834. }
  835. i64 total_size_in_bits = bit_size * count;
  836. i64 total_size = (total_size_in_bits+7)/8;
  837. return total_size;
  838. // i64 alignment = align_formula(size, align);
  839. // return alignment*(count-1) + size;
  840. } break;
  841. case Type_Slice: // ptr + len + cap
  842. return 3 * s.word_size;
  843. case Type_Record: {
  844. switch (t->Record.kind) {
  845. case TypeRecord_Struct: {
  846. i64 count = t->Record.field_count;
  847. if (count == 0)
  848. return 0;
  849. type_set_offsets(s, allocator, t);
  850. return t->Record.struct_offsets[count-1] + type_size_of(s, allocator, t->Record.fields[count-1]->type);
  851. } break;
  852. case TypeRecord_Union: {
  853. i64 count = t->Record.field_count;
  854. i64 max = 0;
  855. // NOTE(bill): Zeroth field is invalid
  856. for (isize i = 1; i < count; i++) {
  857. i64 size = type_size_of(s, allocator, t->Record.fields[i]->type);
  858. if (max < size)
  859. max = size;
  860. }
  861. return align_formula(max, s.max_align) + type_size_of(s, allocator, t_int);
  862. } break;
  863. case TypeRecord_RawUnion: {
  864. i64 count = t->Record.field_count;
  865. i64 max = 0;
  866. for (isize i = 0; i < count; i++) {
  867. i64 size = type_size_of(s, allocator, t->Record.fields[i]->type);
  868. if (max < size)
  869. max = size;
  870. }
  871. return max;
  872. } break;
  873. case TypeRecord_Enum: {
  874. return type_size_of(s, allocator, t->Record.enum_base);
  875. } break;
  876. }
  877. } break;
  878. }
  879. // Catch all
  880. return s.word_size;
  881. }
  882. i64 type_offset_of(BaseTypeSizes s, gbAllocator allocator, Type *t, isize index) {
  883. if (t->kind == Type_Record && t->Record.kind == TypeRecord_Struct) {
  884. type_set_offsets(s, allocator, t);
  885. if (gb_is_between(index, 0, t->Record.field_count-1)) {
  886. return t->Record.struct_offsets[index];
  887. }
  888. }
  889. return 0;
  890. }
  891. i64 type_offset_of_from_selection(BaseTypeSizes s, gbAllocator allocator, Type *t, Selection sel) {
  892. i64 offset = 0;
  893. for (isize i = 0; i < gb_array_count(sel.index); i++) {
  894. isize index = sel.index[i];
  895. t = get_base_type(t);
  896. if (t->kind == Type_Record && t->Record.kind == TypeRecord_Struct) {
  897. type_set_offsets(s, allocator, t);
  898. GB_ASSERT(gb_is_between(index, 0, t->Record.field_count-1));
  899. offset += t->Record.struct_offsets[index];
  900. t = t->Record.fields[index]->type;
  901. }
  902. }
  903. return offset;
  904. }
  905. gbString write_type_to_string(gbString str, Type *type) {
  906. if (type == NULL) {
  907. return gb_string_appendc(str, "<no type>");
  908. }
  909. switch (type->kind) {
  910. case Type_Basic:
  911. str = gb_string_append_length(str, type->Basic.name.text, type->Basic.name.len);
  912. break;
  913. case Type_Array:
  914. str = gb_string_appendc(str, gb_bprintf("[%td]", type->Array.count));
  915. str = write_type_to_string(str, type->Array.elem);
  916. break;
  917. case Type_Vector:
  918. str = gb_string_appendc(str, gb_bprintf("{%td}", type->Vector.count));
  919. str = write_type_to_string(str, type->Vector.elem);
  920. break;
  921. case Type_Slice:
  922. str = gb_string_appendc(str, "[]");
  923. str = write_type_to_string(str, type->Array.elem);
  924. break;
  925. case Type_Record: {
  926. switch (type->Record.kind) {
  927. case TypeRecord_Struct:
  928. str = gb_string_appendc(str, "struct{");
  929. for (isize i = 0; i < type->Record.field_count; i++) {
  930. Entity *f = type->Record.fields[i];
  931. GB_ASSERT(f->kind == Entity_Variable);
  932. if (i > 0)
  933. str = gb_string_appendc(str, "; ");
  934. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  935. str = gb_string_appendc(str, ": ");
  936. str = write_type_to_string(str, f->type);
  937. }
  938. str = gb_string_appendc(str, "}");
  939. break;
  940. case TypeRecord_Union:
  941. str = gb_string_appendc(str, "union{");
  942. for (isize i = 0; i < type->Record.field_count; i++) {
  943. Entity *f = type->Record.fields[i];
  944. GB_ASSERT(f->kind == Entity_TypeName);
  945. if (i > 0) {
  946. str = gb_string_appendc(str, "; ");
  947. }
  948. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  949. str = gb_string_appendc(str, ": ");
  950. str = write_type_to_string(str, f->type);
  951. }
  952. str = gb_string_appendc(str, "}");
  953. break;
  954. case TypeRecord_RawUnion:
  955. str = gb_string_appendc(str, "raw_union{");
  956. for (isize i = 0; i < type->Record.field_count; i++) {
  957. Entity *f = type->Record.fields[i];
  958. GB_ASSERT(f->kind == Entity_Variable);
  959. if (i > 0)
  960. str = gb_string_appendc(str, ", ");
  961. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  962. str = gb_string_appendc(str, ": ");
  963. str = write_type_to_string(str, f->type);
  964. }
  965. str = gb_string_appendc(str, "}");
  966. break;
  967. case TypeRecord_Enum:
  968. str = gb_string_appendc(str, "enum ");
  969. str = write_type_to_string(str, type->Record.enum_base);
  970. break;
  971. }
  972. } break;
  973. case Type_Pointer:
  974. str = gb_string_appendc(str, "^");
  975. str = write_type_to_string(str, type->Pointer.elem);
  976. break;
  977. case Type_Named:
  978. if (type->Named.type_name != NULL) {
  979. str = gb_string_append_length(str, type->Named.name.text, type->Named.name.len);
  980. } else {
  981. // NOTE(bill): Just in case
  982. str = gb_string_appendc(str, "<named type>");
  983. }
  984. break;
  985. case Type_Tuple:
  986. if (type->Tuple.variable_count > 0) {
  987. for (isize i = 0; i < type->Tuple.variable_count; i++) {
  988. Entity *var = type->Tuple.variables[i];
  989. if (var != NULL) {
  990. GB_ASSERT(var->kind == Entity_Variable);
  991. if (i > 0)
  992. str = gb_string_appendc(str, ", ");
  993. str = write_type_to_string(str, var->type);
  994. }
  995. }
  996. }
  997. break;
  998. case Type_Proc:
  999. str = gb_string_appendc(str, "proc(");
  1000. if (type->Proc.params)
  1001. str = write_type_to_string(str, type->Proc.params);
  1002. str = gb_string_appendc(str, ")");
  1003. if (type->Proc.results) {
  1004. str = gb_string_appendc(str, " -> ");
  1005. str = write_type_to_string(str, type->Proc.results);
  1006. }
  1007. break;
  1008. }
  1009. return str;
  1010. }
  1011. gbString type_to_string(Type *type, gbAllocator a) {
  1012. gbString str = gb_string_make(a, "");
  1013. return write_type_to_string(str, type);
  1014. }