return_to_param: delete debug code
[smatch.git] / scope.c
blobac625b58648d051b5bd004a95abede70cf821969
1 /*
2 * Symbol scoping.
4 * This is pretty trivial.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003-2004 Linus Torvalds
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
31 #include "lib.h"
32 #include "allocate.h"
33 #include "symbol.h"
34 #include "scope.h"
36 static struct scope builtin_scope = { .next = &builtin_scope };
38 struct scope *block_scope = &builtin_scope, // regular automatic variables etc
39 *label_scope = NULL, // expr-stmt labels
40 *function_scope = &builtin_scope, // labels, arguments etc
41 *file_scope = &builtin_scope, // static
42 *global_scope = &builtin_scope; // externally visible
44 void set_current_scope(struct symbol *sym)
46 sym->scope = block_scope;
49 void bind_scope(struct symbol *sym, struct scope *scope)
51 sym->scope = scope;
52 add_symbol(&scope->symbols, sym);
55 void rebind_scope(struct symbol *sym, struct scope *new)
57 struct scope *old = sym->scope;
59 if (old == new)
60 return;
62 if (old)
63 delete_ptr_list_entry((struct ptr_list**) &old->symbols, sym, 1);
65 bind_scope(sym, new);
68 static void start_scope(struct scope **s, struct position pos)
70 struct scope *scope = __alloc_scope(0);
72 scope->token = __alloc_token(0);
73 scope->token->pos = pos;
75 scope->next = *s;
76 *s = scope;
79 void start_file_scope(void)
81 struct scope *scope = __alloc_scope(0);
83 scope->next = &builtin_scope;
84 file_scope = scope;
86 /* top-level stuff defaults to file scope, "extern" etc will choose global scope */
87 function_scope = scope;
88 block_scope = scope;
91 void start_block_scope(struct position pos)
93 start_scope(&block_scope, pos);
96 void start_function_scope(struct position pos)
98 start_scope(&block_scope, pos);
99 start_scope(&label_scope, pos);
100 function_scope = label_scope;
103 static void remove_symbol_scope(struct symbol *sym)
105 struct symbol **ptr = &sym->ident->symbols;
107 while (*ptr != sym)
108 ptr = &(*ptr)->next_id;
109 *ptr = sym->next_id;
112 static void end_scope(struct scope **s)
114 struct scope *scope = *s;
115 struct symbol_list *symbols = scope->symbols;
116 struct symbol *sym;
118 *s = scope->next;
119 scope->symbols = NULL;
120 FOR_EACH_PTR(symbols, sym) {
121 remove_symbol_scope(sym);
122 } END_FOR_EACH_PTR(sym);
125 void end_file_scope(void)
127 end_scope(&file_scope);
130 void new_file_scope(void)
132 if (file_scope != &builtin_scope)
133 end_file_scope();
134 start_file_scope();
137 void end_block_scope(void)
139 end_scope(&block_scope);
142 void end_function_scope(void)
144 end_scope(&block_scope);
145 end_label_scope();
146 function_scope = label_scope;
149 void start_label_scope(struct position pos)
151 start_scope(&label_scope, pos);
154 void end_label_scope(void)
156 struct symbol *sym;
158 FOR_EACH_PTR(label_scope->symbols, sym) {
159 if (!sym->stmt || sym->used)
160 continue;
161 if (sym->label_modifiers & MOD_UNUSED)
162 continue;
163 warning(sym->pos, "unused label '%s'", show_ident(sym->ident));
164 } END_FOR_EACH_PTR(sym);
166 end_scope(&label_scope);
169 int is_outer_scope(struct scope *scope)
171 if (scope == block_scope)
172 return 0;
173 if (scope == &builtin_scope && block_scope->next == &builtin_scope)
174 return 0;
175 return 1;
178 int is_in_scope(struct scope *outer, struct scope *inner)
180 while (inner != outer) {
181 if (inner == function_scope)
182 return 0;
183 inner = inner->next;
185 return 1;