-
Notifications
You must be signed in to change notification settings - Fork 7
/
bring.cxx
151 lines (129 loc) · 3.55 KB
/
bring.cxx
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
#include <stdio.h>
#include "nmtbl.h"
#include "token.h"
#include "trnod.h"
#include "util.h"
//=============================================================================
// Global data structures:
// Ring where all global variables are entered
b_ring b_ring::global_b_ring(b_ring::global);
// Current ring (record or procedure body)
b_ring *b_ring::curr_b_ring = &b_ring::global_b_ring;
b_ring *b_ring::top_b_ring = &b_ring::global_b_ring;
rename_item* rename_item::list;
void symbol::translate(token* t)
{
if( path != NULL) {
t->set_trans(dprintf("%s%s", path, out_name->text));
} else {
t->set_trans(out_name->text);
}
}
// Constructor for ring
b_ring::b_ring(int scope) {
b_ring::scope = scope;
outer = NULL;
inherite = NULL;
syms = NULL;
with = NULL;
}
// Search for an entry at one level of binding ring
symbol* b_ring::shallow_search(token* t)
{
nm_entry* nm = t->name;
for (symbol* vr = syms; vr != NULL; vr = vr->next) {
if (vr->in_name == nm) {
return vr->tag != symbol::s_dummy ? vr : (symbol*)0;
}
}
return NULL;
}
bool b_ring::find_scope(b_ring* type)
{
for (b_ring* scope = this; scope != NULL; scope = scope->outer) {
if (type == scope) {
return true;
}
}
return false;
}
// Search for an entry in this binding ring
symbol* b_ring::search(token* t)
{
nm_entry* nm = t->name;
for (b_ring* scope = this; scope != NULL; scope = scope->outer) {
for (b_ring* br = scope; br != NULL; br = br->inherite) {
for (symbol* vr = br->syms; vr != NULL; vr = vr->next) {
if (vr->in_name == nm) {
return vr->tag != symbol::s_dummy ? vr : (symbol*)0;
}
}
}
}
return NULL;
}
void b_ring::make_unique(symbol* sym)
{
symbol *sp, **spp;
int version = 0;
check_loop:
// for (spp = &top_b_ring->syms; (sp = *spp) != NULL; spp = &sp->next) {
for (spp = &global_b_ring.syms; (sp = *spp) != NULL; spp = &sp->next) {
if (sp->out_name == sym->out_name) {
char buf[256];
sprintf(buf, "%s%d", sym->in_name->text, ++version);
assert(strlen(buf) < sizeof(buf));
sym->out_name = nm_entry::add(buf, TKN_IDENT);
goto check_loop;
}
}
if (scope != global) {
*spp = sp = new symbol; // this symbol will never be found
sp->ring = top_b_ring;
sp->next = NULL;
sp->type = NULL;
sp->in_name = sym->in_name;
sp->out_name = sym->out_name;
sp->tag = symbol::s_dummy;
}
}
// Create variable block and add it to this binding ring
symbol* b_ring::add(nm_entry* in_name, nm_entry* out_name, int tag, tpexpr* type)
{
symbol *sym = new symbol;
sym->in_name = in_name;
sym->out_name = out_name;
sym->flags = 0;
sym->tag = tag;
sym->type = type;
sym->path = NULL;
sym->ring = this;
if (compile_system_library) {
sym->out_name = rename_item::rename(sym->out_name);
sym->flags |= symbol::f_syslib;
} else {
while (sym->out_name->flags & nm_entry::macro) {
sym->out_name = nm_entry::add(dprintf("%s_", sym->out_name->text),
TKN_IDENT);
}
}
if (scope != record &&
(tag == symbol::s_proc || tag == symbol::s_type || scope == global))
{
make_unique(sym);
}
sym->next = syms;
syms = sym;
return sym;
}
void b_ring::make_vars_static()
{
for (symbol *sym = syms; sym != NULL; sym = sym->next) {
if ((sym->flags & (symbol::f_exported|symbol::f_val_param|symbol::f_var_param))
== symbol::f_exported)
{
sym->flags |= symbol::f_static;
sym->ring->make_unique(sym);
}
}
}