helper: taking the address is not a complicated variable
[smatch.git] / scope.c
blob772cb9f66952c79e7dfb3814efb4fee8d0bafb32
1 /*
2 * Symbol scoping.
4 * This is pretty trivial.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003-2004 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdio.h>
15 #include "lib.h"
16 #include "allocate.h"
17 #include "symbol.h"
18 #include "scope.h"
20 static struct scope builtin_scope = { .next = &builtin_scope };
22 struct scope *block_scope = &builtin_scope, // regular automatic variables etc
23 *function_scope = &builtin_scope, // labels, arguments etc
24 *file_scope = &builtin_scope, // static
25 *global_scope = &builtin_scope; // externally visible
27 void bind_scope(struct symbol *sym, struct scope *scope)
29 sym->scope = scope;
30 add_symbol(&scope->symbols, sym);
33 static void start_scope(struct scope **s, struct position pos)
35 struct scope *scope = __alloc_scope(0);
36 memset(scope, 0, sizeof(*scope));
37 scope->token = __alloc_token(0);
38 scope->token->pos = pos;
39 scope->next = *s;
40 *s = scope;
43 void start_file_scope(void)
45 struct scope *scope = __alloc_scope(0);
47 memset(scope, 0, sizeof(*scope));
48 scope->next = &builtin_scope;
49 file_scope = scope;
51 /* top-level stuff defaults to file scope, "extern" etc will choose global scope */
52 function_scope = scope;
53 block_scope = scope;
56 void start_symbol_scope(struct position pos)
58 start_scope(&block_scope, pos);
61 void start_function_scope(struct position pos)
63 start_scope(&function_scope, pos);
64 start_scope(&block_scope, pos);
67 static void remove_symbol_scope(struct symbol *sym)
69 struct symbol **ptr = &sym->ident->symbols;
71 while (*ptr != sym)
72 ptr = &(*ptr)->next_id;
73 *ptr = sym->next_id;
76 static void end_scope(struct scope **s)
78 struct scope *scope = *s;
79 struct symbol_list *symbols = scope->symbols;
80 struct symbol *sym;
82 *s = scope->next;
83 scope->symbols = NULL;
84 FOR_EACH_PTR(symbols, sym) {
85 remove_symbol_scope(sym);
86 } END_FOR_EACH_PTR(sym);
89 void end_file_scope(void)
91 end_scope(&file_scope);
94 void new_file_scope(void)
96 if (file_scope != &builtin_scope)
97 end_file_scope();
98 start_file_scope();
101 void end_symbol_scope(void)
103 end_scope(&block_scope);
106 void end_function_scope(void)
108 end_scope(&block_scope);
109 end_scope(&function_scope);
112 int is_outer_scope(struct scope *scope)
114 if (scope == block_scope)
115 return 0;
116 if (scope == &builtin_scope && block_scope->next == &builtin_scope)
117 return 0;
118 return 1;