harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 38da859e8945d2108c52250ff444918eceeb9191
parent c6519ffd66ea5c6acff1ea890be6cf6adc53a8b3
Author: Drew DeVault <sir@cmpwn.com>
Date:   Wed, 20 Jan 2021 16:42:57 -0500

check: add constants to declarations

Diffstat:
Minclude/check.h | 8+++++++-
Msrc/check.c | 20+++++++++++++++++++-
Msrc/gen.c | 2+-
Msrc/typedef.c | 2+-
4 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/include/check.h b/include/check.h @@ -18,6 +18,11 @@ struct context { int id; }; +struct constant_decl { + const struct type *type; + const struct expression *value; +}; + enum func_decl_flags { FN_FINI = 1 << 0, FN_INIT = 1 << 1, @@ -37,7 +42,7 @@ struct global_decl { }; enum declaration_type { - DECL_CONSTANT, + DECL_CONST, DECL_FUNC, DECL_GLOBAL, DECL_TYPE, @@ -49,6 +54,7 @@ struct declaration { char *symbol; bool exported; union { + struct constant_decl constant; struct function_decl func; struct global_decl global; const struct type *_type; diff --git a/src/check.c b/src/check.c @@ -1176,6 +1176,23 @@ check_expression(struct context *ctx, } static struct declaration * +check_const(struct context *ctx, + const struct ast_decl *adecl) +{ + const struct type *type = type_store_lookup_atype( + &ctx->store, adecl->constant.type); + struct declaration *decl = xcalloc(1, sizeof(struct declaration)); + const struct scope_object *obj = scope_lookup( + ctx->unit, &adecl->constant.ident); + assert(obj && obj->otype == O_CONST); + decl->type = DECL_CONST; + decl->constant.type = type; + decl->constant.value = obj->value; + mkident(ctx, &decl->ident, &adecl->constant.ident); + return decl; +} + +static struct declaration * check_function(struct context *ctx, const struct ast_decl *adecl) { @@ -1326,7 +1343,8 @@ check_declarations(struct context *ctx, const struct ast_decl *adecl = &adecls->decl; switch (adecl->decl_type) { case AST_DECL_CONST: - break; // Handled in scan + decl = check_const(ctx, adecl); + break; case AST_DECL_FUNC: decl = check_function(ctx, adecl); break; diff --git a/src/gen.c b/src/gen.c @@ -1912,7 +1912,7 @@ static void gen_decl(struct gen_context *ctx, const struct declaration *decl) { switch (decl->type) { - case DECL_CONSTANT: + case DECL_CONST: // Nothing to do break; case DECL_FUNC: diff --git a/src/typedef.c b/src/typedef.c @@ -108,7 +108,7 @@ emit_typedefs(struct unit *unit, FILE *out) } switch (decl->type) { - case DECL_CONSTANT: + case DECL_CONST: assert(0); // TODO case DECL_FUNC: emit_func(decl, out);