forked from lucvoo/sparse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scope.c
183 lines (153 loc) · 4.09 KB
/
scope.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Symbol scoping.
*
* This is pretty trivial.
*
* Copyright (C) 2003 Transmeta Corp.
* 2003-2004 Linus Torvalds
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "lib.h"
#include "allocate.h"
#include "symbol.h"
#include "scope.h"
static struct scope builtin_scope = { .next = &builtin_scope };
struct scope *block_scope = &builtin_scope, // regular automatic variables etc
*label_scope = NULL, // expr-stmt labels
*function_scope = &builtin_scope, // labels, arguments etc
*file_scope = &builtin_scope, // static
*global_scope = &builtin_scope; // externally visible
void set_current_scope(struct symbol *sym)
{
sym->scope = block_scope;
}
void bind_scope(struct symbol *sym, struct scope *scope)
{
sym->scope = scope;
add_symbol(&scope->symbols, sym);
}
void rebind_scope(struct symbol *sym, struct scope *new)
{
struct scope *old = sym->scope;
if (old == new)
return;
if (old)
delete_ptr_list_entry((struct ptr_list**) &old->symbols, sym, 1);
bind_scope(sym, new);
}
static void start_scope(struct scope **s)
{
struct scope *scope = __alloc_scope(0);
scope->next = *s;
*s = scope;
}
void start_file_scope(void)
{
struct scope *scope = __alloc_scope(0);
scope->next = &builtin_scope;
file_scope = scope;
/* top-level stuff defaults to file scope, "extern" etc will choose global scope */
function_scope = scope;
block_scope = scope;
}
void start_block_scope(void)
{
start_scope(&block_scope);
}
void start_function_scope(void)
{
start_scope(&block_scope);
start_scope(&label_scope);
function_scope = label_scope;
}
static void remove_symbol_scope(struct symbol *sym)
{
struct symbol **ptr = &sym->ident->symbols;
while (*ptr != sym)
ptr = &(*ptr)->next_id;
*ptr = sym->next_id;
}
static void end_scope(struct scope **s)
{
struct scope *scope = *s;
struct symbol_list *symbols = scope->symbols;
struct symbol *sym;
*s = scope->next;
scope->symbols = NULL;
FOR_EACH_PTR(symbols, sym) {
remove_symbol_scope(sym);
} END_FOR_EACH_PTR(sym);
}
void end_file_scope(void)
{
end_scope(&file_scope);
}
void new_file_scope(void)
{
if (file_scope != &builtin_scope)
end_file_scope();
start_file_scope();
}
void end_block_scope(void)
{
end_scope(&block_scope);
}
void end_function_scope(void)
{
end_scope(&block_scope);
end_label_scope();
function_scope = label_scope;
}
void start_label_scope(void)
{
start_scope(&label_scope);
}
void end_label_scope(void)
{
struct symbol *sym;
FOR_EACH_PTR(label_scope->symbols, sym) {
if (!sym->stmt || sym->used)
continue;
if (sym->label_modifiers & MOD_UNUSED)
continue;
warning(sym->pos, "unused label '%s'", show_ident(sym->ident));
} END_FOR_EACH_PTR(sym);
end_scope(&label_scope);
}
int is_outer_scope(struct scope *scope)
{
if (scope == block_scope)
return 0;
if (scope == &builtin_scope && block_scope->next == &builtin_scope)
return 0;
return 1;
}
int is_in_scope(struct scope *outer, struct scope *inner)
{
while (inner != outer) {
if (inner == function_scope)
return 0;
inner = inner->next;
}
return 1;
}