~sircmpwn/harec unlisted

ref: 03e62282e5d921a43107ebdf32473629342fa538 harec/src/type_store.c -rw-r--r-- 22.8 KiB
03e62282Bor Grošelj Simić check: handle zero-sized struct/tuple fields cleanly a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "check.h"
#include "eval.h"
#include "scope.h"
#include "typedef.h"
#include "type_store.h"
#include "util.h"

static void
expect(const struct location *loc, bool constraint, char *fmt, ...)
{
	if (!constraint) {
		va_list ap;
		va_start(ap, fmt);

		fprintf(stderr, "Error %s:%d:%d: ",
			loc->path, loc->lineno, loc->colno);
		vfprintf(stderr, fmt, ap);
		fprintf(stderr, "\n");
		abort();
	}
}

static void
handle_errors(struct errors *errors)
{
	struct errors *error = errors;
	while (error && error->prev) {
		error = error->prev;
	}
	while (error) {
		fprintf(stderr, "Error %s:%d:%d: %s\n", error->loc.path,
			error->loc.lineno, error->loc.colno, error->msg);
		struct errors *next = error->next;
		free(error);
		error = next;
	}
	if (errors) {
		abort();
	}
}

static char *
gen_typename(const struct type *type)
{
	size_t sz = 0;
	char *ptr = NULL;
	FILE *f = open_memstream(&ptr, &sz);
	emit_type(type, f);
	fclose(f);
	return ptr;
}

static size_t
ast_array_len(struct type_store *store, const struct ast_type *atype)
{
	// TODO: Maybe we should cache these
	struct expression in, out;
	if (atype->array.length == NULL) {
		return SIZE_UNDEFINED;
	}
	struct errors *errors = check_expression(store->check_context,
		atype->array.length, &in, NULL, NULL);
	handle_errors(errors);
	enum eval_result r = eval_expr(store->check_context, &in, &out);
	// TODO: Bubble up these errors:
	assert(r == EVAL_OK);
	assert(type_is_integer(out.result));
	if (type_is_signed(out.result)) {
		assert(out.constant.ival > 0);
	}
	return (size_t)out.constant.uval;
}

const struct type *
builtin_type_for_storage(enum type_storage storage, bool is_const)
{
	switch (storage) {
	case STORAGE_BOOL:
		return is_const ? &builtin_type_const_bool : &builtin_type_bool;
	case STORAGE_CHAR:
		return is_const ? &builtin_type_const_char : &builtin_type_char;
	case STORAGE_F32:
		return is_const ? &builtin_type_const_f32 : &builtin_type_f32;
	case STORAGE_F64:
		return is_const ? &builtin_type_const_f64 : &builtin_type_f64;
	case STORAGE_FCONST:
		return is_const ? &builtin_type_const_fconst : &builtin_type_fconst;
	case STORAGE_I8:
		return is_const ? &builtin_type_const_i8 : &builtin_type_i8;
	case STORAGE_I16:
		return is_const ? &builtin_type_const_i16 : &builtin_type_i16;
	case STORAGE_I32:
		return is_const ? &builtin_type_const_i32 : &builtin_type_i32;
	case STORAGE_I64:
		return is_const ? &builtin_type_const_i64 : &builtin_type_i64;
	case STORAGE_ICONST:
		return is_const ? &builtin_type_const_iconst : &builtin_type_iconst;
	case STORAGE_INT:
		return is_const ? &builtin_type_const_int : &builtin_type_int;
	case STORAGE_RUNE:
		return is_const ? &builtin_type_const_rune : &builtin_type_rune;
	case STORAGE_SIZE:
		return is_const ? &builtin_type_const_size : &builtin_type_size;
	case STORAGE_U8:
		return is_const ? &builtin_type_const_u8 : &builtin_type_u8;
	case STORAGE_U16:
		return is_const ? &builtin_type_const_u16 : &builtin_type_u16;
	case STORAGE_U32:
		return is_const ? &builtin_type_const_u32 : &builtin_type_u32;
	case STORAGE_U64:
		return is_const ? &builtin_type_const_u64 : &builtin_type_u64;
	case STORAGE_UINT:
		return is_const ? &builtin_type_const_uint : &builtin_type_uint;
	case STORAGE_UINTPTR:
		return is_const ? &builtin_type_const_uintptr : &builtin_type_uintptr;
	case STORAGE_VOID:
		return is_const ? &builtin_type_const_void : &builtin_type_void;
	case STORAGE_NULL:
		return &builtin_type_null; // const null and null are the same type
	case STORAGE_STRING:
		return is_const ? &builtin_type_const_str : &builtin_type_str;
	case STORAGE_ALIAS:
	case STORAGE_ARRAY:
	case STORAGE_FUNCTION:
	case STORAGE_POINTER:
	case STORAGE_SLICE:
	case STORAGE_STRUCT:
	case STORAGE_TAGGED:
	case STORAGE_TUPLE:
	case STORAGE_UNION:
	case STORAGE_ENUM:
		return NULL;
	}
	assert(0); // Unreachable
}

static const struct type *
builtin_for_atype(const struct ast_type *atype)
{
	if (atype->flags & TYPE_ERROR) {
		return NULL;
	}
	bool is_const = (atype->flags & TYPE_CONST) != 0;
	return builtin_type_for_storage(atype->storage, is_const);
}

static const struct type *
builtin_for_type(const struct type *type)
{
	if (type->flags & TYPE_ERROR) {
		return NULL;
	}
	bool is_const = (type->flags & TYPE_CONST) != 0;
	return builtin_type_for_storage(type->storage, is_const);
}

static void
struct_insert_field(struct type_store *store, struct struct_field **fields,
	enum type_storage storage, size_t *size, size_t *usize, size_t *align,
	const struct ast_struct_union_type *atype, bool *ccompat)
{
	assert(atype->member_type == MEMBER_TYPE_FIELD);
	while (fields && *fields && strcmp((*fields)->name, atype->field.name) < 0) {
		fields = &(*fields)->next;
	}
	struct struct_field *field, _temp = {0};
	if (fields != NULL) {
		field = *fields;
		assert(field == NULL || strcmp(field->name, atype->field.name) != 0);
		*fields = xcalloc(1, sizeof(struct struct_field));
		(*fields)->next = field;
		field = *fields;
	} else {
		field = &_temp;
	}
	// TODO: Bubble this error up

	field->name = strdup(atype->field.name);
	field->type = type_store_lookup_atype(store, atype->field.type);
	expect(&atype->field.type->loc, field->type->size,
			"Struct field size cannot be zero");

	if (atype->offset) {
		*ccompat = false;
		struct expression in, out;
		struct errors *errors = check_expression(store->check_context,
			atype->offset, &in, NULL, NULL);
		handle_errors(errors);
		enum eval_result r = eval_expr(store->check_context, &in, &out);
		// TODO: Bubble up
		assert(r == EVAL_OK);
		assert(type_is_integer(out.result));
		if (type_is_signed(out.result)) {
			assert(out.constant.ival >= 0);
		}
		size_t offs = (size_t)out.constant.uval;
		field->offset = offs;
	} else {
		size_t offs = *size;
		if (offs % field->type->align) {
			offs += field->type->align - (offs % field->type->align);
		}
		field->offset = offs;
		assert(field->offset % field->type->align == 0);
	}

	if (field->type->size == SIZE_UNDEFINED || *size == SIZE_UNDEFINED) {
		*size = SIZE_UNDEFINED;
	} else if (storage == STORAGE_STRUCT) {
		*size = field->offset + field->type->size;
	} else {
		*usize = field->type->size > *usize ? field->type->size : *usize;
	}
	*align = field->type->align > *align ? field->type->align : *align;
}

static void
struct_init_from_atype(struct type_store *store, enum type_storage storage,
	size_t *size, size_t *align, struct struct_field **fields,
	const struct ast_struct_union_type *atype, bool *ccompat)
{
	// TODO: fields with size SIZE_UNDEFINED
	size_t usize = 0;
	assert(storage == STORAGE_STRUCT || storage == STORAGE_UNION);
	while (atype) {
		size_t sub = *size;
		switch (atype->member_type) {
		case MEMBER_TYPE_FIELD:
			struct_insert_field(store, fields, storage,
				size, &usize, align, atype, ccompat);
			break;
		case MEMBER_TYPE_EMBEDDED:
			if (atype->embedded->storage == STORAGE_UNION) {
				*ccompat = false;
				// We need to set the offset of all union
				// members to the maximum alignment of the union
				// members, so first we do a dry run to compute
				// it:
				size_t offs = 0, align_1 = 0;
				struct_init_from_atype(store, STORAGE_UNION,
					&offs, &align_1, NULL,
					&atype->embedded->struct_union, ccompat);
				// Insert padding per the results:
				*size += *size % align_1;
				// Then insert the fields for real:
				sub = *size;
				struct_init_from_atype(store, STORAGE_UNION,
					&sub, align, fields,
					&atype->embedded->struct_union, ccompat);
			} else {
				struct_init_from_atype(store, STORAGE_STRUCT,
					&sub, align, fields,
					&atype->embedded->struct_union, ccompat);
			}

			if (storage == STORAGE_UNION) {
				usize = sub > usize ? sub : usize;
			} else {
				*size += sub;
			}
			break;
		case MEMBER_TYPE_ALIAS:
			assert(0); // TODO
		}
		atype = atype->next;
	}

	if (storage == STORAGE_UNION) {
		*size = usize;
	}
}

static size_t
sum_tagged_memb(struct type_store *store,
		const struct type_tagged_union *u)
{
	size_t nmemb = 0;
	for (; u; u = u->next) {
		const struct type *type = u->type;
		if (type->storage == STORAGE_TAGGED) {
			nmemb += sum_tagged_memb(store, &type->tagged);
		} else {
			++nmemb;
		}
	}
	return nmemb;
}

static size_t
sum_atagged_memb(struct type_store *store,
		const struct ast_tagged_union_type *u)
{
	size_t nmemb = 0;
	for (; u; u = u->next) {
		const struct type *type =
			type_store_lookup_atype(store, u->type);
		if (type->storage == STORAGE_TAGGED) {
			nmemb += sum_tagged_memb(store, &type->tagged);
		} else {
			++nmemb;
		}
	}
	return nmemb;
}

static void
collect_tagged_memb(struct type_store *store,
		struct type_tagged_union **ta,
		const struct type_tagged_union *src,
		size_t *i)
{
	for (; src; src = src->next) {
		const struct type *type = src->type;
		if (type->storage == STORAGE_TAGGED) {
			collect_tagged_memb(store, ta, &type->tagged, i);
			continue;
		}
		struct type_tagged_union *tu;
		ta[*i] = tu = xcalloc(1, sizeof(struct type_tagged_union));
		tu->type = type;
		*i += 1;
	}
}

static void
collect_atagged_memb(struct type_store *store,
		struct type_tagged_union **ta,
		const struct ast_tagged_union_type *atu,
		size_t *i)
{
	for (; atu; atu = atu->next) {
		const struct type *type =
			type_store_lookup_atype(store, atu->type);
		if (type->storage == STORAGE_TAGGED) {
			collect_tagged_memb(store, ta, &type->tagged, i);
			continue;
		}
		struct type_tagged_union *tu;
		ta[*i] = tu = xcalloc(1, sizeof(struct type_tagged_union));
		tu->type = type;
		*i += 1;
	}
}

static int
tagged_cmp(const void *ptr_a, const void *ptr_b)
{
	const struct type_tagged_union **a =
		(const struct type_tagged_union **)ptr_a;
	const struct type_tagged_union **b =
		(const struct type_tagged_union **)ptr_b;
	return (*a)->type->id < (*b)->type->id ? -1
		: (*a)->type->id > (*b)->type->id ? 1 : 0;
}

static void
tagged_init(struct type *type, struct type_tagged_union **tu, size_t nmemb)
{
	// Prune duplicates
	for (size_t i = 1; i < nmemb; ++i)
	for (size_t j = 0; j < i; ++j) {
		if (tu[j]->type->id == tu[i]->type->id) {
			memmove(&tu[i], &tu[i + 1], (nmemb - i - 1)
				* sizeof(struct type_tagged_union *));
			--nmemb;
			break;
		}
	}

	// Sort by ID
	qsort(tu, nmemb, sizeof(tu[0]), tagged_cmp);

	// First one free
	type->tagged = *tu[0];
	free(tu[0]);

	type->size = type->tagged.type->size;
	type->align = type->tagged.type->align;

	struct type_tagged_union **next = &type->tagged.next;
	for (size_t i = 1; i < nmemb; ++i) {
		if (tu[i]->type->size == SIZE_UNDEFINED) {
			char *type = gen_typename(tu[i]->type);
			fprintf(stderr,
				"Cannot create tagged union from type of undefined size %s\n",
				type);
			free(type);
		}

		if (tu[i]->type->size > type->size) {
			type->size = tu[i]->type->size;
		}
		if (tu[i]->type->align > type->align) {
			type->align = tu[i]->type->align;
		}

		*next = tu[i];
		next = &tu[i]->next;
	}

	if (type->align == 0) {
		type->align = builtin_type_uint.align;
	}

	type->size += builtin_type_uint.size % type->align
		+ builtin_type_uint.align;
	if (type->align < builtin_type_uint.align) {
		type->align = builtin_type_uint.align;
	}
}

static void
tagged_init_from_atype(struct type_store *store,
	struct type *type, const struct ast_type *atype)
{
	size_t nmemb = sum_atagged_memb(store, &atype->tagged_union);
	struct type_tagged_union **tu =
		xcalloc(nmemb, sizeof(struct type_tagged_union *));
	size_t i = 0;
	collect_atagged_memb(store, tu, &atype->tagged_union, &i);
	tagged_init(type, tu, nmemb);
}

static void
tuple_init_from_atype(struct type_store *store,
	struct type *type, const struct ast_type *atype)
{
	type->size = 0, type->align = 0;
	const struct ast_tuple_type *atuple = &atype->tuple;
	struct type_tuple *cur = &type->tuple;
	while (atuple) {
		cur->type = type_store_lookup_atype(store, atuple->type);
		expect(&atuple->type->loc, cur->type->size,
				"Type of size zero cannot be a tuple member");
		cur->offset = type->size % cur->type->align + type->size;
		type->size += type->size % cur->type->align + cur->type->size;
		if (type->align < cur->type->align) {
			type->align = cur->type->align;
		}

		atuple = atuple->next;
		if (atuple) {
			cur->next = xcalloc(1, sizeof(struct type_tuple));
			cur = cur->next;
		}
	}
}

static const struct type *type_store_lookup_type(struct type_store *store, const struct type *type);

static void
type_init_from_atype(struct type_store *store,
	struct type *type,
	const struct ast_type *atype)
{
	type->storage = atype->storage;
	type->flags = atype->flags;

	const struct scope_object *obj;
	const struct identifier *ident;
	const struct type *builtin;
	struct identifier temp;
	switch (type->storage) {
	case STORAGE_FCONST:
	case STORAGE_ICONST:
		assert(0); // Invariant
	case STORAGE_BOOL:
	case STORAGE_CHAR:
	case STORAGE_F32:
	case STORAGE_F64:
	case STORAGE_I8:
	case STORAGE_I16:
	case STORAGE_I32:
	case STORAGE_I64:
	case STORAGE_INT:
	case STORAGE_NULL:
	case STORAGE_RUNE:
	case STORAGE_SIZE:
	case STORAGE_STRING:
	case STORAGE_U8:
	case STORAGE_U16:
	case STORAGE_U32:
	case STORAGE_U64:
	case STORAGE_UINT:
	case STORAGE_UINTPTR:
	case STORAGE_VOID:
		builtin = builtin_type_for_storage(type->storage, false);
		type->size = builtin->size;
		type->align = builtin->align;
		break;
	case STORAGE_ALIAS:
		ident = &atype->alias;
		if (ident->ns == NULL) {
			temp = *ident;
			temp.ns = store->check_context->ns;
			ident = &temp;
		}

		obj = scope_lookup(store->check_context->scope, ident);
		if (!obj) {
			obj = scope_lookup(store->check_context->scope,
				&atype->alias);
		}
		if (!obj) {
			expect(&atype->loc, !atype->unwrap,
					"Cannot unwrap unknown type alias %s",
					identifier_unparse(ident));
			identifier_dup(&type->alias.ident, ident);
			type->alias.type = NULL;
			type->size = SIZE_UNDEFINED;
			type->align = SIZE_UNDEFINED;
			return;
		}

		expect(&atype->loc, obj->otype == O_TYPE,
				"Object '%s' is not a type",
				identifier_unparse(&obj->ident));

		if (atype->unwrap) {
			*type = *type_dealias(obj->type);
			break;
		}

		identifier_dup(&type->alias.ident, &obj->ident);
		type->alias.type = obj->type;
		type->size = type->alias.type->size;
		type->align = type->alias.type->align;
		break;
	case STORAGE_ARRAY:
		type->array.length = ast_array_len(store, atype);
		type->array.members = type_store_lookup_atype(
				store, atype->array.members);
		// TODO: Bubble this up:
		assert(type->array.members->size != SIZE_UNDEFINED);

		type->align = type->array.members->align;
		if (type->array.length == SIZE_UNDEFINED) {
			type->size = SIZE_UNDEFINED;
		} else {
			type->size = type->array.members->size * type->array.length;
		}
		break;
	case STORAGE_ENUM:
		type->_enum.storage = atype->_enum.storage;
		const struct type *storage =
			builtin_type_for_storage(type->_enum.storage, true);
 		// TODO: Bubble this up
		assert(type_is_integer(storage));
		type->size = storage->size;
		type->align = storage->size;

		struct scope *scope = scope_push(&store->check_context->scope);
		// TODO: Check for duplicates
		struct ast_enum_field *avalue = atype->_enum.values;
		struct type_enum_value **values = &type->_enum.values;
		intmax_t iimplicit = 0;
		uintmax_t uimplicit = 0;
		while (avalue) {
			struct type_enum_value *value = *values =
				xcalloc(sizeof(struct type_enum_value), 1);
			value->name = strdup(avalue->name);
			if (avalue->value != NULL) {
				struct expression in, out;
				struct errors *errors = check_expression(
					store->check_context, avalue->value,
					&in, storage, NULL);
				handle_errors(errors);
				enum eval_result r =
					eval_expr(store->check_context, &in, &out);
				// TODO: Bubble this up
				assert(r == EVAL_OK && type_is_assignable(storage, out.result));
				if (type_is_signed(storage)) {
					iimplicit = out.constant.ival;
				} else {
					uimplicit = out.constant.uval;
				}
			}
			// TODO: Test that the value fits into this precision
			if (type_is_signed(storage)) {
				value->ival = iimplicit++;
			} else {
				value->uval = uimplicit++;
			}

			struct identifier name = {
				.name = value->name,
				.ns = NULL,
			};
			// TODO: This leaks:
			struct expression *vexpr = xcalloc(1, sizeof(struct expression));
			vexpr->type = EXPR_CONSTANT;
			vexpr->result = storage;
			vexpr->constant.uval = value->uval;
			scope_insert(scope, O_CONST, &name, &name, storage, vexpr);

			values = &value->next;
			avalue = avalue->next;
		}
		scope_pop(&store->check_context->scope);
		scope_free(scope);
		break;
	case STORAGE_FUNCTION:
		type->size = SIZE_UNDEFINED;
		type->align = SIZE_UNDEFINED;
		type->func.result =
			type_store_lookup_atype(store, atype->func.result);
		type->func.variadism = atype->func.variadism;
		type->func.flags = atype->func.flags;
		struct type_func_param *param, **next = &type->func.params;
		for (struct ast_function_parameters *aparam = atype->func.params;
				aparam; aparam = aparam->next) {
			param = *next = xcalloc(1, sizeof(struct type_func_param));
			param->type = type_store_lookup_atype(store, aparam->type);
			if (atype->func.variadism == VARIADISM_HARE
					&& !aparam->next) {
				param->type = type_store_lookup_slice(
					store, param->type);
			}
			next = &param->next;
		}
		break;
	case STORAGE_POINTER:
		type->size = 8; // XXX: ARCH
		type->align = 8;
		type->pointer.flags = atype->pointer.flags;
		type->pointer.referent = type_store_lookup_atype(
			store, atype->pointer.referent);
		break;
	case STORAGE_SLICE:
		type->size = 24; // XXX: ARCH
		type->align = 8;
		type->array.members = type_store_lookup_atype(
			store, atype->array.members);
		type->array.length = SIZE_UNDEFINED;
		break;
	case STORAGE_STRUCT:
	case STORAGE_UNION:
		type->struct_union.c_compat = true;
		struct_init_from_atype(store, type->storage, &type->size,
			&type->align, &type->struct_union.fields,
			&atype->struct_union, &type->struct_union.c_compat);
		if (!type->struct_union.c_compat) {
			// Recompute size
			type->size = 0;
			for (struct struct_field *f = type->struct_union.fields;
					f; f = f->next) {
				if (f->offset + f->type->size > type->size) {
					type->size = f->offset + f->type->size;
				}
			}
		}
		break;
	case STORAGE_TAGGED:
		tagged_init_from_atype(store, type, atype);
		break;
	case STORAGE_TUPLE:
		tuple_init_from_atype(store, type, atype);
		break;
	}
}

static const struct type *
_type_store_lookup_type(
	struct type_store *store,
	const struct type *type,
	bool recur)
{
	const struct type *builtin = builtin_for_type(type);
	if (builtin) {
		return builtin;
	}

	struct type psuedotype = *type;
	if (type->storage == STORAGE_ALIAS && !recur) {
		// References to type aliases always inherit the flags that the
		// alias was defined with
		//
		// TODO: This is likely problematic for forward references
		// TODO: Does this need a spec update?
		const struct scope_object *obj = scope_lookup(
			store->check_context->scope, &type->alias.ident);
		if (obj) {
			psuedotype.flags |= obj->type->flags;
			type = &psuedotype;
		}
	}

	uint32_t hash = type_hash(type);
	struct type_bucket **next = &store->buckets[hash % TYPE_STORE_BUCKETS],
		*bucket = NULL;

	while (*next) {
		bucket = *next;
		if (bucket->type.id == hash) {
			return &bucket->type;
		}
		next = &bucket->next;
	}

	bucket = *next = xcalloc(1, sizeof(struct type_bucket));
	bucket->type = *type;
	bucket->type.id = hash;
	return &bucket->type;
}

static const struct type *
type_store_lookup_type(struct type_store *store, const struct type *type)
{
	return _type_store_lookup_type(store, type, false);
}

const struct type *
type_store_lookup_atype(struct type_store *store, const struct ast_type *atype)
{
	const struct type *builtin = builtin_for_atype(atype);
	if (builtin) {
		return builtin;
	}
	struct type type = {0};
	type_init_from_atype(store, &type, atype);
	return type_store_lookup_type(store, &type);
}

const struct type *
type_store_lookup_with_flags(struct type_store *store,
	const struct type *type, unsigned int flags)
{
	if (type->flags == flags) {
		return type;
	}
	struct type new = *type;
	new.flags = flags;
	return type_store_lookup_type(store, &new);
}

const struct type *
type_store_lookup_pointer(struct type_store *store,
	const struct type *referent, unsigned int ptrflags)
{
	struct type ptr = {
		.storage = STORAGE_POINTER,
		.pointer = {
			.referent = referent,
			.flags = ptrflags,
		},
		.size = 8, // XXX: ARCH
		.align = 8,
	};
	return type_store_lookup_type(store, &ptr);
}

const struct type *
type_store_lookup_array(struct type_store *store,
	const struct type *members, size_t len)
{
	struct type array = {
		.storage = STORAGE_ARRAY,
		.array = {
			.members = members,
			.length = len,
		},
		.size = len == SIZE_UNDEFINED
			? SIZE_UNDEFINED : members->size * len,
		.align = members->align,
	};
	return type_store_lookup_type(store, &array);
}

const struct type *
type_store_lookup_slice(struct type_store *store, const struct type *members)
{
	struct type slice = {
		.storage = STORAGE_SLICE,
		.array = {
			.members = members,
			.length = SIZE_UNDEFINED,
		},
		.size = 24, // XXX: ARCH
		.align = 8,
	};
	return type_store_lookup_type(store, &slice);
}

const struct type *
type_store_lookup_alias(struct type_store *store,
	const struct identifier *ident, const struct type *secondary)
{
	struct type alias = {
		.storage = STORAGE_ALIAS,
		.alias = {
			.ident = *ident,
			.type = secondary,
		},
		.size = secondary->size,
		.align = secondary->align,
		.flags = secondary->flags,
	};
	// XXX: This needs to be updated on updates to type_flags (types.h)
	unsigned int flags[] = {
		0,
		TYPE_CONST,
		TYPE_ERROR,
		TYPE_ERROR | TYPE_CONST,
	};
	for (size_t i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
		const struct type *flagged = type_store_lookup_with_flags(store,
			secondary, flags[i]);
		alias.flags = flagged->flags;
		alias.alias.type = flagged;
		struct type *falias =
			(struct type *)type_store_lookup_type(store, &alias);
		if (falias->alias.type == NULL) {
			// Finish filling in forward referenced type
			falias->alias.type = type_store_lookup_with_flags(store,
				secondary, secondary->flags | flags[i]);
			falias->size = secondary->size;
			falias->align = secondary->align;
			falias->flags = secondary->flags | flags[i];
		}
	}
	alias.flags = secondary->flags;
	alias.alias.type = secondary;
	struct type *type = (struct type *)type_store_lookup_type(store, &alias);
	return type;
}

const struct type *
type_store_lookup_tagged(struct type_store *store,
		struct type_tagged_union *tags)
{
	struct type type = {
		.storage = STORAGE_TAGGED,
	};
	size_t nmemb = sum_tagged_memb(store, tags);
	struct type_tagged_union **tu =
		xcalloc(nmemb, sizeof(struct type_tagged_union *));
	size_t i = 0;
	collect_tagged_memb(store, tu, tags, &i);
	tagged_init(&type, tu, nmemb);
	return type_store_lookup_type(store, &type);
}

const struct type *
type_store_lookup_tuple(struct type_store *store, struct type_tuple *values)
{
	struct type type = {
		.storage = STORAGE_TUPLE,
		.tuple = *values,
	};
	for (struct type_tuple *t = values; t; t = t->next) {
		if (t->type->align > type.align) {
			type.align = t->type->align;
		}
		t->offset = type.size % t->type->align + type.size;
		type.size += type.size % t->type->align + t->type->size;
	}
	return type_store_lookup_type(store, &type);
}