harec

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

commit 485ad4fff22a12f9c5185ec6b738f5ee8c886758
parent 5fd1cf53d7757b8266477c8915271e966154c565
Author: Bor Grošelj Simić <bor.groseljsimic@telemach.net>
Date:   Sun,  5 Dec 2021 03:08:54 +0100

scope: separate scope_object initialization and insertion

Signed-off-by: Bor Grošelj Simić <bor.groseljsimic@telemach.net>

Diffstat:
Minclude/scope.h | 6++++++
Msrc/scope.c | 42++++++++++++++++++++++++++----------------
2 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/include/scope.h b/include/scope.h @@ -75,6 +75,12 @@ struct scope *scope_lookup_ancestor(struct scope *scope, void scope_free(struct scope *scope); void scope_free_all(struct scopes *scopes); +void scope_object_init(struct scope_object *obj, enum object_type otype, + const struct identifier *ident, const struct identifier *name, + const struct type *type, struct expression *value); + +void scope_insert_from_object(struct scope *scope, struct scope_object *object); + const struct scope_object *scope_insert( struct scope *scope, enum object_type otype, const struct identifier *ident, const struct identifier *name, diff --git a/src/scope.c b/src/scope.c @@ -18,9 +18,7 @@ scope_push(struct scope **stack, enum scope_class class) struct scope *new = xcalloc(1, sizeof(struct scope)); new->class = class; new->next = &new->objects; - if (*stack) { - new->parent = *stack; - } + new->parent = *stack; *stack = new; return new; } @@ -85,34 +83,46 @@ scope_free_all(struct scopes *scopes) } } -const struct scope_object * -scope_insert(struct scope *scope, enum object_type otype, +void +scope_object_init(struct scope_object *object, enum object_type otype, const struct identifier *ident, const struct identifier *name, const struct type *type, struct expression *value) { - struct scope_object *o = xcalloc(1, sizeof(struct scope_object)); - identifier_dup(&o->ident, ident); - identifier_dup(&o->name, name); - o->otype = otype; - o->type = type; - o->value = value; + identifier_dup(&object->ident, ident); + identifier_dup(&object->name, name); + object->otype = otype; + object->type = type; + object->value = value; if (value) { assert(otype == O_CONST); assert(value->type == EXPR_CONSTANT); } +} +void +scope_insert_from_object(struct scope *scope, struct scope_object *object) +{ // Linked list - *scope->next = o; - scope->next = &o->lnext; + *scope->next = object; + scope->next = &object->lnext; // Hash map - uint32_t hash = name_hash(FNV1A_INIT, name); + uint32_t hash = name_hash(FNV1A_INIT, &object->name); struct scope_object **bucket = &scope->buckets[hash % SCOPE_BUCKETS]; if (*bucket) { - o->mnext = *bucket; + object->mnext = *bucket; } - *bucket = o; + *bucket = object; +} +const struct scope_object * +scope_insert(struct scope *scope, enum object_type otype, + const struct identifier *ident, const struct identifier *name, + const struct type *type, struct expression *value) +{ + struct scope_object *o = xcalloc(1, sizeof(struct scope_object)); + scope_object_init(o, otype, ident, name, type, value); + scope_insert_from_object(scope, o); return o; }