~sircmpwn/hare unlisted

6d2c3789e6d2204af218252cc931118f0212d98a — Drew DeVault 2 months ago 1d2385c
Allow prototypes to be exported

This is useful when exporting functions defined by a separate module,
e.g. assembly code.
3 files changed, 27 insertions(+), 7 deletions(-)

M include/module.h
M src/check.c
M src/gen.c
M include/module.h => include/module.h +1 -1
@@ 410,7 410,7 @@ struct ha_declaration_function {
	const struct ha_type *type;
	struct ha_scope *parameters;
	struct ha_scope *scope;
	struct ha_expression expression;
	struct ha_expression *expression;
	struct ha_declaration_fn_attributes attrs;
};


M src/check.c => src/check.c +17 -3
@@ 1144,15 1144,15 @@ ha_check_declaration_function(struct ha_context *context,
	decl->fndec.scope = ha_context_push_scope(context);
	decl->fndec.scope->bindings = NULL;

	struct ha_expression body;
	ha_check_expression(context, afndecl->expression, &body);
	struct ha_expression *body = calloc(1, sizeof(struct ha_expression));
	ha_check_expression(context, afndecl->expression, body);

	ha_context_pop_scope(context);
	ha_context_pop_scope(context);

	decl->fndec.expression = body;

	const struct ha_type *result_type = body.result_type;
	const struct ha_type *result_type = body->result_type;
	struct ha_ast_type *afntype = afndecl->type;
	const struct ha_type *declared = ha_context_lookup_atype(
			context, afntype->func.result_type);


@@ 1337,6 1337,20 @@ ha_scan_declaration_function(struct ha_context *context,
		if (afndecl->attrs.symbol) {
			symbol->name_override = afndecl->attrs.symbol;
		}

		/* Exporting a prototype? */
		if ((adecl->flags & HA_DECL_FLAGS_EXPORT)
				&& !context->importing) {
			struct ha_declaration out = {0};
			ha_context_make_identifier(context,
				&out.ident, &adecl->ident);
			out.type = HA_DECL_FUNCTION;
			out.flags = adecl->flags;
			out.fndec.parameters = &param_scope->scope;
			out.fndec.parameters->bindings = bindings;
			out.fndec.type = fntype;
			ha_context_insert_declaration(context, &out);
		}
		return;
	}


M src/gen.c => src/gen.c +9 -3
@@ 2818,7 2818,7 @@ ha_build_ir_body(struct ha_qbe_body *qbe_body,
	ret.class = HA_QBE_INSTR_CLASS_JUMP;
	ret.jmp.instr = HA_QBE_JUMP_INSTR_RET;

	const struct ha_expression *result = &decl->expression;
	const struct ha_expression *result = decl->expression;
	result = ha_expr_implicit_cast(result, decl->type->func.result_type);
	ha_build_ir_expr(&context, qbe_body, &ret.jmp.ret.arg, result);



@@ 2984,8 2984,14 @@ ha_build_ir(struct ha_qbe_ir *qbe_ir, const struct ha_context *context)
		struct ha_qbe_declaration *decl = NULL;
		switch (decls->decl.type) {
		case HA_DECL_FUNCTION:
			decl = calloc(1, sizeof(struct ha_qbe_declaration));
			ha_build_ir_decl_function(decl, &decls->decl);
			if (decls->decl.fndec.expression) {
				decl = calloc(1, sizeof(struct ha_qbe_declaration));
				ha_build_ir_decl_function(decl, &decls->decl);
			} else {
				/* Does not require code gen */
				decls = decls->next;
				continue;
			}
			break;
		case HA_DECL_GLOBAL:
			decl = calloc(1, sizeof(struct ha_qbe_declaration));