~sircmpwn/harec

7f428e7c869bef3d52d12ccdaf8d7002d4fae9e9 — Drew DeVault 2 years ago d1e9fce
Allow @init/@fini funcs to share name

Much like @test.

Signed-off-by: Drew DeVault <sir@cmpwn.com>
3 files changed, 15 insertions(+), 5 deletions(-)

M src/check.c
M src/gen.c
M tests/09-funcs.ha
M src/check.c => src/check.c +3 -5
@@ 3307,7 3307,7 @@ scan_function(struct context *ctx, const struct ast_function_decl *decl)
	const struct type *fntype = type_store_lookup_atype(
			ctx->store, &fn_atype);

	if (!(decl->flags & FN_TEST)) {
	if (!decl->flags) {
		struct identifier ident = {0};
		if (decl->symbol) {
			ident.name = strdup(decl->symbol);


@@ 3496,10 3496,8 @@ scan_decl_start(struct context *ctx, struct scope *imports, struct ast_decl *dec
		}
		break;
	case AST_DECL_FUNC:
		if ((decl->function.flags & FN_TEST)) {
			if (ctx->is_test) {
				scan_function(ctx, &decl->function);
			}
		if (decl->function.flags) {
			scan_function(ctx, &decl->function);
			return;
		}
		struct ast_function_decl *func = &decl->function;

M src/gen.c => src/gen.c +4 -0
@@ 2902,6 2902,10 @@ gen_function_decl(struct gen_context *ctx, const struct declaration *decl)

	if (func->flags & FN_TEST) {
		qdef->name = gen_name(ctx, "testfunc.%d");
	} else if (func->flags & FN_INIT) {
		qdef->name = gen_name(ctx, "initfunc.%d");
	} else if (func->flags & FN_FINI) {
		qdef->name = gen_name(ctx, "finifunc.%d");
	} else {
		qdef->name = decl->symbol ? strdup(decl->symbol)
			: ident_to_sym(&decl->ident);

M tests/09-funcs.ha => tests/09-funcs.ha +8 -0
@@ 38,10 38,18 @@ let x: int = 42;
	x = 1337;
};

@init fn init() void = {
	void; // Should be allowed to have the same name
};

@fini fn fini() void = {
	rt::exit(42); // Magic number
};

@fini fn fini() void = {
	void; // Should be allowed to have the same name
};

export fn main() void = {
	assert(simple() == 69);
	pointers();