forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.c
266 lines (245 loc) · 7.81 KB
/
module.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
modules and top-level bindings
*/
#include <assert.h>
#include "julia.h"
jl_module_t *jl_main_module=NULL;
jl_module_t *jl_core_module=NULL;
jl_module_t *jl_base_module=NULL;
jl_module_t *jl_current_module=NULL;
jl_module_t *jl_new_module(jl_sym_t *name)
{
jl_module_t *m = (jl_module_t*)allocobj(sizeof(jl_module_t));
m->type = (jl_type_t*)jl_module_type;
m->name = name;
htable_new(&m->bindings, 0);
jl_set_const(m, name, (jl_value_t*)m);
arraylist_new(&m->imports, 0);
if (jl_core_module) {
jl_module_importall(m, jl_core_module);
}
// export own name, so import Foo.* also imports Foo
jl_module_export(m, name);
return m;
}
static jl_binding_t *new_binding(jl_sym_t *name)
{
jl_binding_t *b = (jl_binding_t*)allocb(sizeof(jl_binding_t));
b->name = name;
b->value = NULL;
b->type = (jl_type_t*)jl_any_type;
b->owner = NULL;
b->constp = 0;
b->exportp = 0;
return b;
}
// get binding for assignment
jl_binding_t *jl_get_binding_wr(jl_module_t *m, jl_sym_t *var)
{
jl_binding_t **bp = (jl_binding_t**)ptrhash_bp(&m->bindings, var);
jl_binding_t *b;
if (*bp != HT_NOTFOUND) {
if ((*bp)->owner != m) {
ios_printf(JL_STDERR,
"Warning: imported binding for %s overwritten in module %s\n", var->name, m->name->name);
}
else {
return *bp;
}
}
b = new_binding(var);
b->owner = m;
*bp = b;
return *bp;
}
// get binding for reading. might return NULL for unbound.
jl_binding_t *jl_get_binding(jl_module_t *m, jl_sym_t *var)
{
jl_binding_t *b = (jl_binding_t*)ptrhash_get(&m->bindings, var);
if (b == HT_NOTFOUND) {
for(size_t i=0; i < m->imports.len; i++) {
jl_module_t *imp = (jl_module_t*)m->imports.items[i];
b = (jl_binding_t*)ptrhash_get(&imp->bindings, var);
if (b != HT_NOTFOUND && b->exportp) {
if (b->owner != imp)
return jl_get_binding(b->owner, var);
// only import if the source module has resolved the binding;
// otherwise it might just be marked for re-export.
if (b->constp || b->value) {
return b;
}
}
}
return NULL;
}
if (b->owner != m)
return jl_get_binding(b->owner, var);
return b;
}
void jl_module_import(jl_module_t *to, jl_module_t *from, jl_sym_t *s)
{
if (to == from)
return;
jl_binding_t *b = jl_get_binding(from, s);
if (b == NULL || !b->exportp) {
ios_printf(JL_STDERR,
"Warning: could not import %s.%s into %s\n",
from->name->name, s->name, to->name->name);
}
else {
jl_binding_t **bp = (jl_binding_t**)ptrhash_bp(&to->bindings, s);
if (*bp != HT_NOTFOUND) {
if ((*bp)->owner != to) {
ios_printf(JL_STDERR,
"Warning: ignoring conflicting import of %s.%s into %s\n",
from->name->name, s->name, to->name->name);
}
else if (*bp == b) {
// importing a binding on top of itself. harmless.
}
else if ((*bp)->constp || (*bp)->value) {
if ((*bp)->constp && (*bp)->value && b->constp &&
b->value == (*bp)->value) {
// import of equivalent binding
return;
}
ios_printf(JL_STDERR,
"Warning: import of %s.%s into %s conflicts with an existing identifier; ignored.\n",
from->name->name, s->name, to->name->name);
}
else {
(*bp)->owner = b->owner;
}
}
else {
jl_binding_t *nb = new_binding(s);
nb->owner = b->owner;
*bp = nb;
}
}
}
void jl_module_importall(jl_module_t *to, jl_module_t *from)
{
if (to == from)
return;
for(size_t i=0; i < to->imports.len; i++) {
if (from == to->imports.items[i])
return;
}
arraylist_push(&to->imports, from);
// go though all "from" module's exports to check for conflicts, and
// re-resolve symbols if it's not too late.
// mostly, we want to handle "export x" occurring before the "import M.*"
// statement that actually provides x.
void **table = from->bindings.table;
for(size_t i=1; i < from->bindings.size; i+=2) {
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
if (b->exportp) {
jl_binding_t **bp =
(jl_binding_t**)ptrhash_bp(&to->bindings, b->name);
if (*bp != HT_NOTFOUND) {
jl_module_import(to, from, b->name);
}
}
}
}
}
void jl_module_export(jl_module_t *from, jl_sym_t *s)
{
jl_binding_t **bp = (jl_binding_t**)ptrhash_bp(&from->bindings, s);
if (*bp == HT_NOTFOUND) {
jl_binding_t *b = jl_get_binding(from, s);
if (b == NULL) {
b = jl_get_binding_wr(from, s);
}
if (b->owner != from) {
// create an explicit import so we can mark as re-exported
jl_module_import(from, b->owner, s);
}
bp = (jl_binding_t**)ptrhash_bp(&from->bindings, s);
}
assert(*bp != HT_NOTFOUND);
(*bp)->exportp = 1;
}
int jl_boundp(jl_module_t *m, jl_sym_t *var)
{
jl_binding_t *b = jl_get_binding(m, var);
return b && (b->value != NULL);
}
jl_value_t *jl_get_global(jl_module_t *m, jl_sym_t *var)
{
jl_binding_t *b = jl_get_binding(m, var);
if (b == NULL) return NULL;
return b->value;
}
void jl_set_global(jl_module_t *m, jl_sym_t *var, jl_value_t *val)
{
jl_binding_t *bp = jl_get_binding_wr(m, var);
if (!bp->constp) {
bp->value = val;
}
}
void jl_set_const(jl_module_t *m, jl_sym_t *var, jl_value_t *val)
{
jl_binding_t *bp = jl_get_binding_wr(m, var);
if (!bp->constp) {
bp->value = val;
bp->constp = 1;
}
}
DLLEXPORT int jl_is_const(jl_module_t *m, jl_sym_t *var)
{
if (m == NULL) m = jl_current_module;
jl_binding_t *b = jl_get_binding(m, var);
return b && b->constp;
}
void jl_checked_assignment(jl_binding_t *b, jl_value_t *rhs)
{
if (b->constp && b->value != NULL) {
//jl_errorf("cannot redefine constant %s", b->name->name);
JL_PRINTF(JL_STDERR, "Warning: redefinition of constant %s ignored.\n",
b->name->name);
}
else {
b->value = rhs;
}
}
void jl_declare_constant(jl_binding_t *b)
{
if (b->value != NULL && !b->constp) {
jl_errorf("cannot declare %s constant; it already has a value",
b->name->name);
}
b->constp = 1;
}
DLLEXPORT jl_value_t *jl_get_current_module()
{
return (jl_value_t*)jl_current_module;
}
DLLEXPORT void jl_set_current_module(jl_value_t *m)
{
assert(jl_typeis(m, jl_module_type));
jl_current_module = (jl_module_t*)m;
}
DLLEXPORT jl_value_t *jl_module_names(jl_module_t *m, int all)
{
jl_array_t *a = jl_alloc_array_1d(jl_array_symbol_type, 0);
JL_GC_PUSH(&a);
size_t i;
void **table = m->bindings.table;
for(i=1; i < m->bindings.size; i+=2) {
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
if (all || b->exportp || m == jl_main_module) {
jl_array_grow_end(a, 1);
//XXX: change to jl_arrayset if array storage allocation for Array{Symbols,1} changes:
jl_cellset(a, a->length-1, (jl_value_t*)b->name);
}
}
}
JL_GC_POP();
return (jl_value_t*)a;
}
DLLEXPORT jl_sym_t *jl_module_name(jl_module_t *m) { return m->name; }
DLLEXPORT jl_module_t *jl_module_parent(jl_module_t *m) { return m->parent; }