harec

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

commit c266689e39f0989b7f7c5f2d969d4ac9f7989490
parent 03ff7b8d5ff70bf9d5b01f1627b130381260c644
Author: Drew DeVault <sir@cmpwn.com>
Date:   Mon, 21 Dec 2020 10:11:17 -0500

check: add function parameters to scope

Diffstat:
Minclude/scope.h | 7+++++--
Msrc/check.c | 17++++++++++++++++-
Msrc/scope.c | 13++++++++++---
Msrc/type_store.c | 8+++++++-
4 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/include/scope.h b/include/scope.h @@ -12,6 +12,7 @@ struct scope_object { struct scope { struct scope_object *objects; + struct scope_object **next; // List order matters for functions struct scope *parent; }; @@ -26,7 +27,9 @@ struct scope *scope_pop(struct scope **stack, enum trace_sys sys); void scope_free(struct scope *scope); void scope_free_all(struct scopes *scopes); -void scope_insert(const struct identifier *ident, const struct type *type); -const struct type *scope_lookup(const struct identifier *ident); +void scope_insert(struct scope *scope, + const struct identifier *ident, const struct type *type); +const struct type *scope_lookup(struct scope *scope, + const struct identifier *ident); #endif diff --git a/src/check.c b/src/check.c @@ -183,7 +183,6 @@ check_function(struct context *ctx, { const struct ast_function_decl *afndecl = &adecl->function; trenter(TR_CHECK, "function"); - assert(!afndecl->prototype.params); // TODO assert(!afndecl->symbol); // TODO const struct ast_type fn_atype = { @@ -200,6 +199,18 @@ check_function(struct context *ctx, identifier_dup(&decl->ident, &afndecl->ident); decl->func.flags = afndecl->flags; + struct scope *scope = scope_push(&ctx->scope, TR_CHECK); + struct ast_function_parameters *params = afndecl->prototype.params; + while (params) { + struct identifier ident = { + .name = params->name, + }; + const struct type *type = type_store_lookup_atype( + &ctx->store, params->type); + scope_insert(scope, &ident, type); + params = params->next; + } + struct expression *body = calloc(1, sizeof(struct expression)); check_expression(ctx, &afndecl->body, body); decl->func.body = body; @@ -216,6 +227,10 @@ check_function(struct context *ctx, expect(!decl->exported, "%s function cannot be exported", flags); } + + // TODO: Add declaration to unit scope + + scope_pop(&ctx->scope, TR_CHECK); trleave(TR_CHECK, NULL); } diff --git a/src/scope.c b/src/scope.c @@ -8,6 +8,7 @@ struct scope * scope_push(struct scope **stack, enum trace_sys sys) { struct scope *new = calloc(1, sizeof(struct scope)); + new->next = &new->objects; if (*stack) { new->parent = *stack; } @@ -59,13 +60,19 @@ scope_free_all(struct scopes *scopes) } void -scope_insert(const struct identifier *ident, const struct type *type) +scope_insert(struct scope *scope, + const struct identifier *ident, + const struct type *type) { - assert(0); // TODO + struct scope_object *o = calloc(1, sizeof(struct scope_object)); + identifier_dup(&o->ident, ident); + o->type = type; + *scope->next = o; + scope->next = &o->next; } const struct type * -scope_lookup(const struct identifier *ident) +scope_lookup(struct scope *scope, const struct identifier *ident) { assert(0); // TODO } diff --git a/src/type_store.c b/src/type_store.c @@ -260,7 +260,13 @@ type_init_from_atype(struct type_store *store, type_store_lookup_atype(store, atype->func.result); type->func.variadism = atype->func.variadism; type->func.flags = atype->func.flags; - assert(!atype->func.params); // TODO + struct type_func_param *param, **next = &type->func.params; + for (struct ast_function_parameters *aparam = atype->func.params; + aparam; aparam = aparam->next) { + param = *next = calloc(1, sizeof(struct type_func_param)); + param->type = type_store_lookup_atype(store, aparam->type); + next = &param->next; + } break; case TYPE_STORAGE_POINTER: case TYPE_STORAGE_SLICE: