/
scope.cc
71 lines (63 loc) · 1.67 KB
/
scope.cc
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
/* -*-Mode: c++;-*-
Copyright (c) 2003-2008 John Plevyak, All Rights Reserved
*/
#include "ifadefs.h"
#include "scope.h"
#include "sym.h"
void Scope::add_dynamic(Scope *s, Sym *sy) {
dynamic.add(s);
dynamic_container.add(sy);
}
static Sym *get_dynamic(Scope *scope, Sym *scope_container, cchar *name, Sym **container) {
Sym *s;
if ((s = scope->hash.get(name))) {
if (container && scope_container) *container = scope_container;
return s;
}
for (int i = 0; i < scope->dynamic.n; i++)
if ((s = get_dynamic(scope->dynamic[i], scope->dynamic_container.v[i], name, container))) {
if (container && scope_container) *container = scope_container;
return s;
}
return NULL;
}
Sym *Scope::get_local(cchar *name, Sym **container) {
Sym *s;
if ((s = hash.get(name))) {
if (container) *container = 0;
return s;
}
for (int i = 0; i < dynamic.n; i++)
if ((s = get_dynamic(dynamic[i], dynamic_container.v[i], name, container))) return s;
return NULL;
}
Sym *Scope::get(cchar *name, Sym **container) {
Sym *s;
if ((s = get_local(name, container))) return s;
if (up) return up->get(name, container);
return NULL;
}
Scope *Scope::global() {
Scope *s = this;
while (s->up) s = s->up;
return s;
}
Scope *Scope::module() {
Scope *s = this;
while (s->up->up) s = s->up;
return s;
}
Scope *Scope::function() {
Scope *s = this;
while (s->up->up && (!s->in || !s->in->is_fun)) s = s->up;
if (s->in->is_module) s = s->in->init->scope;
return s;
}
void Scope::put(cchar *name, Sym *s) {
MapElem<cchar *, Sym *> e(name, NULL);
MapElem<cchar *, Sym *> *x = hash.set_in(e);
if (x) {
x->value = s;
} else
hash.put(name, s);
}