More conversion from "iterate()" to an explicit FOR_EACH_PTR()
[smatch.git] / scope.c
blobe3fcb7ec87a846e1935f8d21b6ff802d4b8e65f0
1 /*
2 * Symbol scoping.
4 * This is pretty trivial.
6 * Copyright (C) 2003 Transmeta Corp.
8 * Licensed under the Open Software License version 1.1
9 */
10 #include <stdlib.h>
11 #include <string.h>
13 #include "lib.h"
14 #include "symbol.h"
15 #include "scope.h"
17 static struct scope toplevel_scope = { .next = &toplevel_scope };
19 struct scope *block_scope = &toplevel_scope,
20 *function_scope = &toplevel_scope,
21 *file_scope = &toplevel_scope;
23 void bind_scope(struct symbol *sym, struct scope *scope)
25 sym->scope = scope;
26 add_symbol(&scope->symbols, sym);
29 static void start_scope(struct scope **s)
31 struct scope *scope = __alloc_scope(0);
32 memset(scope, 0, sizeof(*scope));
33 scope->next = *s;
34 *s = scope;
37 void start_symbol_scope(void)
39 start_scope(&block_scope);
42 void start_function_scope(void)
44 start_scope(&function_scope);
45 start_scope(&block_scope);
48 static void remove_symbol_scope(struct symbol *sym)
50 struct symbol **ptr = sym->id_list;
52 while (*ptr != sym)
53 ptr = &(*ptr)->next_id;
54 *ptr = sym->next_id;
57 static void end_scope(struct scope **s)
59 struct scope *scope = *s;
60 struct symbol_list *symbols = scope->symbols;
61 struct symbol *sym;
63 *s = scope->next;
64 scope->symbols = NULL;
65 FOR_EACH_PTR(symbols, sym) {
66 remove_symbol_scope(sym);
67 } END_FOR_EACH_PTR;
70 void end_symbol_scope(void)
72 end_scope(&block_scope);
75 void end_function_scope(void)
77 end_scope(&block_scope);
78 end_scope(&function_scope);