Merge both node and array information at array degrade time.
[smatch.git] / scope.c
blob79db6550bd4098ff1df2736d77f908ee77ac505c
1 /*
2 * Symbol scoping.
4 * This is pretty trivial.
6 * Copyright (C) 2003 Transmeta Corp, all rights reserved.
7 */
8 #include <stdlib.h>
9 #include <string.h>
11 #include "lib.h"
12 #include "symbol.h"
13 #include "scope.h"
15 static struct scope toplevel_scope = { .next = &toplevel_scope };
17 struct scope *block_scope = &toplevel_scope,
18 *function_scope = &toplevel_scope,
19 *file_scope = &toplevel_scope;
21 void bind_scope(struct symbol *sym, struct scope *scope)
23 sym->scope = scope;
24 add_symbol(&scope->symbols, sym);
27 static void start_scope(struct scope **s)
29 struct scope *scope = __alloc_bytes(sizeof(*scope));
30 memset(scope, 0, sizeof(*scope));
31 scope->next = *s;
32 *s = scope;
35 void start_symbol_scope(void)
37 start_scope(&block_scope);
40 void start_function_scope(void)
42 start_scope(&function_scope);
43 start_scope(&block_scope);
46 static void remove_symbol_scope(struct symbol *sym, void *data, int flags)
48 struct symbol **ptr = sym->id_list;
50 while (*ptr != sym)
51 ptr = &(*ptr)->next_id;
52 *ptr = sym->next_id;
55 static void end_scope(struct scope **s)
57 struct scope *scope = *s;
58 struct symbol_list *symbols = scope->symbols;
60 *s = scope->next;
61 scope->symbols = NULL;
62 symbol_iterate(symbols, remove_symbol_scope, NULL);
65 void end_symbol_scope(void)
67 end_scope(&block_scope);
70 void end_function_scope(void)
72 end_scope(&block_scope);
73 end_scope(&function_scope);