~mcf/cproc

eb4320fcc6caf885038fe8b7b7d33ed87eb56608 — Michael Forney 5 months ago b2dc110
decl: Disallow members with incomplete types

Make an exception for flexible array members, though we should also
check that the flexible array member, if any, is last.
3 files changed, 32 insertions(+), 1 deletions(-)

M decl.c
A test/struct-flexible-array.c
A test/struct-flexible-array.qbe
M decl.c => decl.c +3 -1
@@ 685,7 685,9 @@ addmember(struct structbuilder *b, struct qualtype mt, char *name, int align, ui
	struct member *m;
	size_t end;

	// XXX: check incomplete type, except for flexible array member
	/* XXX: flexible array member must be last */
	if (mt.type->incomplete && mt.type->kind != TYPEARRAY)
		error(&tok.loc, "struct member '%s' has incomplete type", name);
	if (mt.type->kind == TYPEFUNC)
		error(&tok.loc, "struct member '%s' has function type", name);
	assert(mt.type->align > 0);

A test/struct-flexible-array.c => test/struct-flexible-array.c +10 -0
@@ 0,0 1,10 @@
struct s {
	int a;
	short b[];
};

int x = sizeof(struct s);

int f(struct s *s) {
	return s->b[2];
}

A test/struct-flexible-array.qbe => test/struct-flexible-array.qbe +19 -0
@@ 0,0 1,19 @@
export data $x = align 4 { w 4, }
export
function w $f(l %.1) {
@start.1
	%.2 =l alloc8 8
	storel %.1, %.2
@body.2
	%.3 =l loadl %.2
	%.4 =l copy %.3
	%.5 =l mul 4, 1
	%.6 =l add %.4, %.5
	%.7 =l copy %.6
	%.8 =l extsw 2
	%.9 =l mul %.8, 2
	%.10 =l add %.7, %.9
	%.11 =w loadsh %.10
	%.12 =w extsh %.11
	ret %.12
}