1 /*
2 * Symbol scoping.
3 *
4 * This is pretty trivial.
5 *
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003-2004 Linus Torvalds
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
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.
26 */
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include "lib.h"
32 #include "allocate.h"
33 #include "symbol.h"
34 #include "scope.h"
35
36 static struct scope builtin_scope = { .next = &builtin_scope };
37
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
43
set_current_scope(struct symbol * sym)44 void set_current_scope(struct symbol *sym)
45 {
46 sym->scope = block_scope;
47 }
48
bind_scope(struct symbol * sym,struct scope * scope)49 void bind_scope(struct symbol *sym, struct scope *scope)
50 {
51 sym->scope = scope;
52 add_symbol(&scope->symbols, sym);
53 }
54
55
rebind_scope(struct symbol * sym,struct scope * new)56 void rebind_scope(struct symbol *sym, struct scope *new)
57 {
58 struct scope *old = sym->scope;
59
60 if (old == new)
61 return;
62
63 if (old)
64 delete_ptr_list_entry((struct ptr_list**) &old->symbols, sym, 1);
65
66 bind_scope(sym, new);
67 }
68
start_scope(struct scope ** s)69 static void start_scope(struct scope **s)
70 {
71 struct scope *scope = __alloc_scope(0);
72 scope->next = *s;
73 *s = scope;
74 }
75
start_file_scope(void)76 void start_file_scope(void)
77 {
78 struct scope *scope = __alloc_scope(0);
79
80 scope->next = &builtin_scope;
81 file_scope = scope;
82
83 /* top-level stuff defaults to file scope, "extern" etc will choose global scope */
84 function_scope = scope;
85 block_scope = scope;
86 }
87
start_block_scope(void)88 void start_block_scope(void)
89 {
90 start_scope(&block_scope);
91 }
92
start_function_scope(void)93 void start_function_scope(void)
94 {
95 start_scope(&block_scope);
96 start_scope(&label_scope);
97 function_scope = label_scope;
98 }
99
remove_symbol_scope(struct symbol * sym)100 static void remove_symbol_scope(struct symbol *sym)
101 {
102 struct symbol **ptr = &sym->ident->symbols;
103
104 while (*ptr != sym)
105 ptr = &(*ptr)->next_id;
106 *ptr = sym->next_id;
107 }
108
end_scope(struct scope ** s)109 static void end_scope(struct scope **s)
110 {
111 struct scope *scope = *s;
112 struct symbol_list *symbols = scope->symbols;
113 struct symbol *sym;
114
115 *s = scope->next;
116 scope->symbols = NULL;
117 FOR_EACH_PTR(symbols, sym) {
118 remove_symbol_scope(sym);
119 } END_FOR_EACH_PTR(sym);
120 }
121
end_file_scope(void)122 void end_file_scope(void)
123 {
124 end_scope(&file_scope);
125 }
126
new_file_scope(void)127 void new_file_scope(void)
128 {
129 if (file_scope != &builtin_scope)
130 end_file_scope();
131 start_file_scope();
132 }
133
end_block_scope(void)134 void end_block_scope(void)
135 {
136 end_scope(&block_scope);
137 }
138
end_function_scope(void)139 void end_function_scope(void)
140 {
141 end_scope(&block_scope);
142 end_label_scope();
143 function_scope = label_scope;
144 }
145
start_label_scope(void)146 void start_label_scope(void)
147 {
148 start_scope(&label_scope);
149 }
150
end_label_scope(void)151 void end_label_scope(void)
152 {
153 struct symbol *sym;
154
155 FOR_EACH_PTR(label_scope->symbols, sym) {
156 if (!sym->stmt || sym->used)
157 continue;
158 if (sym->label_modifiers & MOD_UNUSED)
159 continue;
160 warning(sym->pos, "unused label '%s'", show_ident(sym->ident));
161 } END_FOR_EACH_PTR(sym);
162
163 end_scope(&label_scope);
164 }
165
is_outer_scope(struct scope * scope)166 int is_outer_scope(struct scope *scope)
167 {
168 if (scope == block_scope)
169 return 0;
170 if (scope == &builtin_scope && block_scope->next == &builtin_scope)
171 return 0;
172 return 1;
173 }
174
is_in_scope(struct scope * outer,struct scope * inner)175 int is_in_scope(struct scope *outer, struct scope *inner)
176 {
177 while (inner != outer) {
178 if (inner == function_scope)
179 return 0;
180 inner = inner->next;
181 }
182 return 1;
183 }
184