blob: 58dd1ee3b349ea49060da77e6f256b7ae52443cd (
plain)
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
|
/*
* Symbol scoping.
*
* This is pretty trivial.
*
* Copyright (C) 2003 Transmeta Corp.
* 2003-2004 Linus Torvalds
*
* Licensed under the Open Software License version 1.1
*/
#include <stdlib.h>
#include <string.h>
#include "lib.h"
#include "allocate.h"
#include "symbol.h"
#include "scope.h"
static struct scope toplevel_scope = { .next = &toplevel_scope };
struct scope *block_scope = &toplevel_scope,
*function_scope = &toplevel_scope,
*file_scope = &toplevel_scope;
void bind_scope(struct symbol *sym, struct scope *scope)
{
sym->scope = scope;
add_symbol(&scope->symbols, sym);
}
static void start_scope(struct scope **s)
{
struct scope *scope = __alloc_scope(0);
memset(scope, 0, sizeof(*scope));
scope->next = *s;
*s = scope;
}
void start_symbol_scope(void)
{
start_scope(&block_scope);
}
void start_function_scope(void)
{
start_scope(&function_scope);
start_scope(&block_scope);
}
static void remove_symbol_scope(struct symbol *sym)
{
struct symbol **ptr = sym->id_list;
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_symbol_scope(void)
{
end_scope(&block_scope);
}
void end_function_scope(void)
{
end_scope(&block_scope);
end_scope(&function_scope);
}
|