chiark / gitweb /
Import upstream sources.
[cparse] / scope.c
1 #include "cparse.h"
2
3 static struct scope *scope;
4
5 void enter_scope(void) {
6   struct scope *new_scope;
7
8   NEW(new_scope);
9   new_scope->parent = scope;
10   scope = new_scope;
11 }
12
13 void exit_scope(void) {
14   scope = scope->parent;
15 }
16
17 /* might be called before the declaration_specifiers have been filled in */
18 void add_declaration(struct declarator *d) {
19   /* skip nameless declarators to simplify some callers */
20   if(d->name) {
21     if(!scope->declarations)
22       scope->declarations = dict_new();
23     dict_add(scope->declarations, d->name, d);
24   }
25   /* XXX redeclaration */
26 }
27
28 struct declarator *lookup(const char *name) {
29   const struct scope *s;
30   struct declarator *d;
31
32   for(s = scope; s; s = s->parent)
33     if(s->declarations && (d = dict_get(s->declarations, name)))
34       return d;
35   return 0;
36 }
37
38 void scope_init(void) {
39   scope = 0;
40   enter_scope();
41 }
42
43 /*
44 Local Variables:
45 c-basic-offset:2
46 comment-column:40
47 fill-column:79
48 End:
49 */