Fix memory leak in `lock-mutex' (aka. `scm_lock_mutex'.)
[bpt/guile.git] / libguile / modules.c
CommitLineData
1606312f 1/* Copyright (C) 1998,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
608860a5 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
1ffa265b 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
1ffa265b 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
6e8d25a6 18
6e8d25a6 19
1ffa265b 20\f
dbb605f5
LC
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
1ffa265b 24
d02b98e9
MV
25#include <stdarg.h>
26
a0599745 27#include "libguile/_scm.h"
1ffa265b 28
a0599745 29#include "libguile/eval.h"
fb43bf74 30#include "libguile/smob.h"
a0599745 31#include "libguile/procprop.h"
152abe96
MD
32#include "libguile/vectors.h"
33#include "libguile/hashtab.h"
34#include "libguile/struct.h"
35#include "libguile/variable.h"
7f763132 36#include "libguile/fluids.h"
d02b98e9 37#include "libguile/deprecation.h"
1ffa265b 38
a0599745 39#include "libguile/modules.h"
1ffa265b 40
86d31dfe 41int scm_module_system_booted_p = 0;
e3365c07 42
92c2555f 43scm_t_bits scm_module_tag;
e3365c07 44
1c1a0823 45/* The current module, a fluid. */
1ffa265b
MD
46static SCM the_module;
47
1c1a0823
AW
48/* Most of the module system is implemented in Scheme. These bindings from
49 boot-9 are needed to provide the Scheme interface. */
3ac8359a 50static SCM the_root_module_var;
1c1a0823 51static SCM module_make_local_var_x_var;
993dae86
AW
52static SCM process_define_module_var;
53static SCM process_use_modules_var;
54static SCM resolve_module_var;
55static SCM module_public_interface_var;
56static SCM module_export_x_var;
57static SCM default_duplicate_binding_procedures_var;
58
3ac8359a 59
7ab42fa2
AW
60static SCM unbound_variable (const char *func, SCM sym)
61{
62 scm_error (scm_from_locale_symbol ("unbound-variable"), func,
63 "Unbound variable: ~S", scm_list_1 (sym), SCM_BOOL_F);
64}
65
f39fc3b3
AW
66SCM
67scm_the_root_module (void)
3ac8359a
NJ
68{
69 if (scm_module_system_booted_p)
70 return SCM_VARIABLE_REF (the_root_module_var);
71 else
72 return SCM_BOOL_F;
73}
74
55000e5f
MV
75SCM_DEFINE (scm_current_module, "current-module", 0, 0, 0,
76 (),
77 "Return the current module.")
78#define FUNC_NAME s_scm_current_module
1ffa265b 79{
3ac8359a
NJ
80 SCM curr = scm_fluid_ref (the_module);
81
f39fc3b3 82 return scm_is_true (curr) ? curr : scm_the_root_module ();
1ffa265b 83}
55000e5f 84#undef FUNC_NAME
1ffa265b 85
86d31dfe 86static void scm_post_boot_init_modules (void);
1ffa265b 87
55000e5f
MV
88SCM_DEFINE (scm_set_current_module, "set-current-module", 1, 0, 0,
89 (SCM module),
9401323e 90 "Set the current module to @var{module} and return\n"
55000e5f
MV
91 "the previous current module.")
92#define FUNC_NAME s_scm_set_current_module
1ffa265b 93{
55000e5f
MV
94 SCM old;
95
86d31dfe
MV
96 if (!scm_module_system_booted_p)
97 scm_post_boot_init_modules ();
98
99 SCM_VALIDATE_MODULE (SCM_ARG1, module);
55000e5f
MV
100
101 old = scm_current_module ();
102 scm_fluid_set_x (the_module, module);
103
1ffa265b
MD
104 return old;
105}
55000e5f 106#undef FUNC_NAME
1ffa265b 107
e3365c07
MD
108SCM_DEFINE (scm_interaction_environment, "interaction-environment", 0, 0, 0,
109 (),
1e6808ea
MG
110 "Return a specifier for the environment that contains\n"
111 "implementation--defined bindings, typically a superset of those\n"
112 "listed in the report. The intent is that this procedure will\n"
113 "return the environment in which the implementation would\n"
114 "evaluate expressions dynamically typed by the user.")
e3365c07
MD
115#define FUNC_NAME s_scm_interaction_environment
116{
aa767bc5 117 return scm_current_module ();
e3365c07
MD
118}
119#undef FUNC_NAME
120
1ffa265b 121SCM
d02b98e9
MV
122scm_c_call_with_current_module (SCM module,
123 SCM (*func)(void *), void *data)
1ffa265b 124{
d02b98e9 125 return scm_c_with_fluid (the_module, module, func, data);
1ffa265b
MD
126}
127
cb1cfc42 128void
661ae7ab 129scm_dynwind_current_module (SCM module)
cb1cfc42 130{
661ae7ab 131 scm_dynwind_fluid (the_module, module);
cb1cfc42 132}
06e80f59
HWN
133
134/*
135 convert "A B C" to scheme list (A B C)
136 */
d02b98e9
MV
137static SCM
138convert_module_name (const char *name)
281004cc 139{
d02b98e9
MV
140 SCM list = SCM_EOL;
141 SCM *tail = &list;
281004cc 142
d02b98e9
MV
143 const char *ptr;
144 while (*name)
145 {
146 while (*name == ' ')
147 name++;
148 ptr = name;
149 while (*ptr && *ptr != ' ')
150 ptr++;
151 if (ptr > name)
152 {
cc95e00a
MV
153 SCM sym = scm_from_locale_symboln (name, ptr-name);
154 *tail = scm_cons (sym, SCM_EOL);
d02b98e9
MV
155 tail = SCM_CDRLOC (*tail);
156 }
157 name = ptr;
158 }
159
160 return list;
1ffa265b
MD
161}
162
9e57344b 163SCM
d02b98e9 164scm_c_resolve_module (const char *name)
9e57344b 165{
d02b98e9 166 return scm_resolve_module (convert_module_name (name));
9e57344b
MV
167}
168
55000e5f 169SCM
d02b98e9 170scm_resolve_module (SCM name)
55000e5f 171{
fdc28395 172 return scm_call_1 (SCM_VARIABLE_REF (resolve_module_var), name);
55000e5f
MV
173}
174
175SCM
d02b98e9
MV
176scm_c_define_module (const char *name,
177 void (*init)(void *), void *data)
55000e5f 178{
fdc28395 179 SCM module = scm_call_1 (SCM_VARIABLE_REF (process_define_module_var),
1afff620 180 scm_list_1 (convert_module_name (name)));
d02b98e9
MV
181 if (init)
182 scm_c_call_with_current_module (module, (SCM (*)(void*))init, data);
183 return module;
55000e5f
MV
184}
185
d02b98e9
MV
186void
187scm_c_use_module (const char *name)
90184345 188{
fdc28395 189 scm_call_1 (SCM_VARIABLE_REF (process_use_modules_var),
b64f4200 190 scm_list_1 (scm_list_1 (convert_module_name (name))));
90184345
MD
191}
192
608860a5
LC
193SCM
194scm_module_export (SCM module, SCM namelist)
06e80f59 195{
8c330007
MD
196 return scm_call_2 (SCM_VARIABLE_REF (module_export_x_var),
197 module, namelist);
06e80f59
HWN
198}
199
eb880cef
MV
200
201/*
202 @code{scm_c_export}(@var{name-list})
203
204 @code{scm_c_export} exports the named bindings from the current
205 module, making them visible to users of the module. This function
206 takes a list of string arguments, terminated by NULL, e.g.
207
208 @example
209 scm_c_export ("add-double-record", "bamboozle-money", NULL);
210 @end example
211*/
d02b98e9
MV
212void
213scm_c_export (const char *name, ...)
281004cc 214{
eb880cef 215 if (name)
d02b98e9 216 {
eb880cef 217 va_list ap;
cc95e00a 218 SCM names = scm_cons (scm_from_locale_symbol (name), SCM_EOL);
eb880cef
MV
219 SCM *tail = SCM_CDRLOC (names);
220 va_start (ap, name);
221 while (1)
222 {
223 const char *n = va_arg (ap, const char *);
224 if (n == NULL)
225 break;
cc95e00a 226 *tail = scm_cons (scm_from_locale_symbol (n), SCM_EOL);
eb880cef
MV
227 tail = SCM_CDRLOC (*tail);
228 }
229 va_end (ap);
608860a5 230 scm_module_export (scm_current_module (), names);
d02b98e9 231 }
281004cc
MD
232}
233
06e80f59 234
e3365c07 235/* Environments */
d164a5af 236
86d31dfe
MV
237SCM_SYMBOL (sym_module, "module");
238
239SCM
240scm_lookup_closure_module (SCM proc)
241{
7888309b 242 if (scm_is_false (proc))
f39fc3b3 243 return scm_the_root_module ();
86d31dfe
MV
244 else if (SCM_EVAL_CLOSURE_P (proc))
245 return SCM_PACK (SCM_SMOB_DATA (proc));
246 else
247 {
490cf750
LC
248 SCM mod;
249
31ac29b6
AW
250 /* FIXME: The `module' property is no longer set on eval closures, as it
251 introduced a circular reference that precludes garbage collection of
252 modules with the current weak hash table semantics (see
253 http://lists.gnu.org/archive/html/guile-devel/2009-01/msg00102.html and
254 http://thread.gmane.org/gmane.comp.programming.garbage-collection.boehmgc/2465
255 for details). Since it doesn't appear to be used (only in this
256 function, which has 1 caller), we no longer extend
257 `set-module-eval-closure!' to set the `module' property. */
490cf750
LC
258 abort ();
259
260 mod = scm_procedure_property (proc, sym_module);
7888309b 261 if (scm_is_false (mod))
f39fc3b3 262 mod = scm_the_root_module ();
86d31dfe
MV
263 return mod;
264 }
265}
266
152abe96
MD
267/*
268 * C level implementation of the standard eval closure
269 *
dd18d312
NJ
270 * This increases loading speed substantially. The code may be
271 * replaced by something based on environments.[ch], in a future
272 * release.
152abe96
MD
273 */
274
608860a5
LC
275/* Return the list of default duplicate binding handlers (procedures). */
276static inline SCM
277default_duplicate_binding_handlers (void)
278{
279 SCM get_handlers;
280
281 get_handlers = SCM_VARIABLE_REF (default_duplicate_binding_procedures_var);
282
283 return (scm_call_0 (get_handlers));
284}
285
286/* Resolve the import of SYM in MODULE, where SYM is currently provided by
287 both IFACE1 as VAR1 and IFACE2 as VAR2. Return the variable chosen by the
288 duplicate binding handlers or `#f'. */
289static inline SCM
290resolve_duplicate_binding (SCM module, SCM sym,
291 SCM iface1, SCM var1,
292 SCM iface2, SCM var2)
293{
294 SCM result = SCM_BOOL_F;
295
296 if (!scm_is_eq (var1, var2))
297 {
298 SCM val1, val2;
299 SCM handlers, h, handler_args;
300
301 val1 = SCM_VARIABLE_REF (var1);
302 val2 = SCM_VARIABLE_REF (var2);
303
304 val1 = (val1 == SCM_UNSPECIFIED) ? SCM_BOOL_F : val1;
305 val2 = (val2 == SCM_UNSPECIFIED) ? SCM_BOOL_F : val2;
306
307 handlers = SCM_MODULE_DUPLICATE_HANDLERS (module);
308 if (scm_is_false (handlers))
309 handlers = default_duplicate_binding_handlers ();
310
311 handler_args = scm_list_n (module, sym,
312 iface1, val1, iface2, val2,
313 var1, val1,
314 SCM_UNDEFINED);
315
316 for (h = handlers;
317 scm_is_pair (h) && scm_is_false (result);
318 h = SCM_CDR (h))
319 {
320 result = scm_apply (SCM_CAR (h), handler_args, SCM_EOL);
321 }
322 }
323 else
324 result = var1;
325
326 return result;
327}
328
73dea589
AW
329SCM scm_pre_modules_obarray;
330
608860a5
LC
331/* Lookup SYM as an imported variable of MODULE. */
332static inline SCM
333module_imported_variable (SCM module, SCM sym)
334{
335#define SCM_BOUND_THING_P scm_is_true
336 register SCM var, imports;
337
338 /* Search cached imported bindings. */
339 imports = SCM_MODULE_IMPORT_OBARRAY (module);
340 var = scm_hashq_ref (imports, sym, SCM_UNDEFINED);
341 if (SCM_BOUND_THING_P (var))
342 return var;
343
344 {
345 /* Search the use list for yet uncached imported bindings, possibly
346 resolving duplicates as needed and caching the result in the import
347 obarray. */
348 SCM uses;
349 SCM found_var = SCM_BOOL_F, found_iface = SCM_BOOL_F;
350
351 for (uses = SCM_MODULE_USES (module);
352 scm_is_pair (uses);
353 uses = SCM_CDR (uses))
354 {
355 SCM iface;
356
357 iface = SCM_CAR (uses);
358 var = scm_module_variable (iface, sym);
359
360 if (SCM_BOUND_THING_P (var))
361 {
362 if (SCM_BOUND_THING_P (found_var))
363 {
364 /* SYM is a duplicate binding (imported more than once) so we
365 need to resolve it. */
366 found_var = resolve_duplicate_binding (module, sym,
367 found_iface, found_var,
368 iface, var);
369 if (scm_is_eq (found_var, var))
370 found_iface = iface;
371 }
372 else
373 /* Keep track of the variable we found and check for other
374 occurences of SYM in the use list. */
375 found_var = var, found_iface = iface;
376 }
377 }
378
379 if (SCM_BOUND_THING_P (found_var))
380 {
381 /* Save the lookup result for future reference. */
382 (void) scm_hashq_set_x (imports, sym, found_var);
383 return found_var;
384 }
385 }
386
387 return SCM_BOOL_F;
388#undef SCM_BOUND_THING_P
389}
390
391SCM_DEFINE (scm_module_local_variable, "module-local-variable", 2, 0, 0,
392 (SCM module, SCM sym),
393 "Return the variable bound to @var{sym} in @var{module}. Return "
394 "@code{#f} is @var{sym} is not bound locally in @var{module}.")
395#define FUNC_NAME s_scm_module_local_variable
152abe96 396{
dc187f33 397#define SCM_BOUND_THING_P(b) \
7888309b 398 (scm_is_true (b))
dc187f33 399
608860a5
LC
400 register SCM b;
401
608860a5
LC
402 if (scm_module_system_booted_p)
403 SCM_VALIDATE_MODULE (1, module);
404
405 SCM_VALIDATE_SYMBOL (2, sym);
406
165a7596
AW
407 if (scm_is_false (module))
408 return scm_hashq_ref (scm_pre_modules_obarray, sym, SCM_UNDEFINED);
608860a5 409
152abe96 410 /* 1. Check module obarray */
608860a5 411 b = scm_hashq_ref (SCM_MODULE_OBARRAY (module), sym, SCM_UNDEFINED);
dc187f33 412 if (SCM_BOUND_THING_P (b))
152abe96 413 return b;
608860a5
LC
414
415 /* 2. Search imported bindings. In order to be consistent with
416 `module-variable', the binder gets called only when no imported binding
417 matches SYM. */
418 b = module_imported_variable (module, sym);
419 if (SCM_BOUND_THING_P (b))
420 return SCM_BOOL_F;
421
152abe96 422 {
608860a5 423 /* 3. Query the custom binder. */
e3365c07 424 SCM binder = SCM_MODULE_BINDER (module);
608860a5 425
7888309b 426 if (scm_is_true (binder))
152abe96 427 {
fdc28395 428 b = scm_call_3 (binder, module, sym, SCM_BOOL_F);
dc187f33 429 if (SCM_BOUND_THING_P (b))
152abe96
MD
430 return b;
431 }
432 }
608860a5
LC
433
434 return SCM_BOOL_F;
435
436#undef SCM_BOUND_THING_P
437}
438#undef FUNC_NAME
439
440SCM_DEFINE (scm_module_variable, "module-variable", 2, 0, 0,
441 (SCM module, SCM sym),
442 "Return the variable bound to @var{sym} in @var{module}. This "
443 "may be both a local variable or an imported variable. Return "
444 "@code{#f} is @var{sym} is not bound in @var{module}.")
445#define FUNC_NAME s_scm_module_variable
446{
447#define SCM_BOUND_THING_P(b) \
448 (scm_is_true (b))
449
450 register SCM var;
451
452 if (scm_module_system_booted_p)
453 SCM_VALIDATE_MODULE (1, module);
454
455 SCM_VALIDATE_SYMBOL (2, sym);
456
73dea589
AW
457 if (scm_is_false (module))
458 return scm_hashq_ref (scm_pre_modules_obarray, sym, SCM_UNDEFINED);
459
608860a5
LC
460 /* 1. Check module obarray */
461 var = scm_hashq_ref (SCM_MODULE_OBARRAY (module), sym, SCM_UNDEFINED);
462 if (SCM_BOUND_THING_P (var))
463 return var;
464
465 /* 2. Search among the imported variables. */
466 var = module_imported_variable (module, sym);
467 if (SCM_BOUND_THING_P (var))
468 return var;
469
152abe96 470 {
608860a5
LC
471 /* 3. Query the custom binder. */
472 SCM binder;
473
474 binder = SCM_MODULE_BINDER (module);
475 if (scm_is_true (binder))
152abe96 476 {
608860a5
LC
477 var = scm_call_3 (binder, module, sym, SCM_BOOL_F);
478 if (SCM_BOUND_THING_P (var))
479 return var;
152abe96 480 }
152abe96 481 }
608860a5
LC
482
483 return SCM_BOOL_F;
484
dc187f33 485#undef SCM_BOUND_THING_P
152abe96 486}
608860a5 487#undef FUNC_NAME
152abe96 488
92c2555f 489scm_t_bits scm_tc16_eval_closure;
152abe96 490
34dfef51 491#define SCM_F_EVAL_CLOSURE_INTERFACE (1<<0)
86d31dfe 492#define SCM_EVAL_CLOSURE_INTERFACE_P(e) \
34dfef51 493 (SCM_SMOB_FLAGS (e) & SCM_F_EVAL_CLOSURE_INTERFACE)
86d31dfe 494
fb43bf74
KN
495/* NOTE: This function may be called by a smob application
496 or from another C function directly. */
497SCM
498scm_eval_closure_lookup (SCM eclo, SCM sym, SCM definep)
152abe96 499{
fb43bf74 500 SCM module = SCM_PACK (SCM_SMOB_DATA (eclo));
7888309b 501 if (scm_is_true (definep))
86d31dfe
MV
502 {
503 if (SCM_EVAL_CLOSURE_INTERFACE_P (eclo))
504 return SCM_BOOL_F;
fdc28395
KN
505 return scm_call_2 (SCM_VARIABLE_REF (module_make_local_var_x_var),
506 module, sym);
86d31dfe 507 }
152abe96 508 else
608860a5 509 return scm_module_variable (module, sym);
152abe96
MD
510}
511
512SCM_DEFINE (scm_standard_eval_closure, "standard-eval-closure", 1, 0, 0,
513 (SCM module),
84526793 514 "Return an eval closure for the module @var{module}.")
152abe96
MD
515#define FUNC_NAME s_scm_standard_eval_closure
516{
e841c3e0 517 SCM_RETURN_NEWSMOB (scm_tc16_eval_closure, SCM_UNPACK (module));
152abe96
MD
518}
519#undef FUNC_NAME
520
e4da0740 521
86d31dfe
MV
522SCM_DEFINE (scm_standard_interface_eval_closure,
523 "standard-interface-eval-closure", 1, 0, 0,
524 (SCM module),
525 "Return a interface eval closure for the module @var{module}. "
526 "Such a closure does not allow new bindings to be added.")
527#define FUNC_NAME s_scm_standard_interface_eval_closure
528{
34dfef51 529 SCM_RETURN_NEWSMOB (scm_tc16_eval_closure | (SCM_F_EVAL_CLOSURE_INTERFACE<<16),
86d31dfe
MV
530 SCM_UNPACK (module));
531}
532#undef FUNC_NAME
533
daedb492
AW
534SCM_DEFINE (scm_eval_closure_module,
535 "eval-closure-module", 1, 0, 0,
536 (SCM eval_closure),
537 "Return the module associated with this eval closure.")
538/* the idea is that eval closures are really not the way to do things, they're
539 superfluous given our module system. this function lets mmacros migrate away
540 from eval closures. */
541#define FUNC_NAME s_scm_eval_closure_module
542{
543 SCM_MAKE_VALIDATE_MSG (SCM_ARG1, eval_closure, EVAL_CLOSURE_P,
544 "eval-closure");
545 return SCM_SMOB_OBJECT (eval_closure);
546}
547#undef FUNC_NAME
548
d02b98e9
MV
549SCM
550scm_module_lookup_closure (SCM module)
551{
7888309b 552 if (scm_is_false (module))
d02b98e9
MV
553 return SCM_BOOL_F;
554 else
555 return SCM_MODULE_EVAL_CLOSURE (module);
556}
557
558SCM
559scm_current_module_lookup_closure ()
560{
561 if (scm_module_system_booted_p)
562 return scm_module_lookup_closure (scm_current_module ());
563 else
564 return SCM_BOOL_F;
565}
566
4f692ace 567SCM_SYMBOL (sym_macroexpand, "macroexpand");
b7e6589f 568
5f161164
AW
569SCM_DEFINE (scm_module_transformer, "module-transformer", 1, 0, 0,
570 (SCM module),
571 "Returns the syntax expander for the given module.")
572#define FUNC_NAME s_scm_module_transformer
d02b98e9 573{
b7e6589f 574 if (SCM_UNLIKELY (scm_is_false (module)))
4f692ace
AW
575 {
576 SCM v = scm_hashq_ref (scm_pre_modules_obarray,
577 sym_macroexpand,
b7e6589f
AW
578 SCM_BOOL_F);
579 if (scm_is_false (v))
4f692ace
AW
580 SCM_MISC_ERROR ("no module, and `macroexpand' unbound", SCM_EOL);
581 return SCM_VARIABLE_REF (v);
b7e6589f 582 }
d02b98e9 583 else
5f161164
AW
584 {
585 SCM_VALIDATE_MODULE (SCM_ARG1, module);
586 return SCM_MODULE_TRANSFORMER (module);
587 }
d02b98e9 588}
5f161164 589#undef FUNC_NAME
d02b98e9
MV
590
591SCM
592scm_current_module_transformer ()
593{
b7e6589f 594 return scm_module_transformer (scm_current_module ());
d02b98e9
MV
595}
596
109c2c9f
MD
597SCM_DEFINE (scm_module_import_interface, "module-import-interface", 2, 0, 0,
598 (SCM module, SCM sym),
608860a5
LC
599 "Return the module or interface from which @var{sym} is imported "
600 "in @var{module}. If @var{sym} is not imported (i.e., it is not "
601 "defined in @var{module} or it is a module-local binding instead "
602 "of an imported one), then @code{#f} is returned.")
109c2c9f
MD
603#define FUNC_NAME s_scm_module_import_interface
604{
608860a5
LC
605 SCM var, result = SCM_BOOL_F;
606
607 SCM_VALIDATE_MODULE (1, module);
608 SCM_VALIDATE_SYMBOL (2, sym);
609
610 var = scm_module_variable (module, sym);
611 if (scm_is_true (var))
109c2c9f 612 {
608860a5
LC
613 /* Look for the module that provides VAR. */
614 SCM local_var;
615
616 local_var = scm_hashq_ref (SCM_MODULE_OBARRAY (module), sym,
617 SCM_UNDEFINED);
618 if (scm_is_eq (local_var, var))
619 result = module;
620 else
621 {
622 /* Look for VAR among the used modules. */
623 SCM uses, imported_var;
624
625 for (uses = SCM_MODULE_USES (module);
626 scm_is_pair (uses) && scm_is_false (result);
627 uses = SCM_CDR (uses))
628 {
629 imported_var = scm_module_variable (SCM_CAR (uses), sym);
630 if (scm_is_eq (imported_var, var))
631 result = SCM_CAR (uses);
632 }
633 }
109c2c9f 634 }
608860a5
LC
635
636 return result;
109c2c9f
MD
637}
638#undef FUNC_NAME
639
993dae86
AW
640SCM
641scm_module_public_interface (SCM module)
dc68fdb9 642{
993dae86 643 return scm_call_1 (SCM_VARIABLE_REF (module_public_interface_var), module);
dc68fdb9 644}
dc68fdb9 645
86d31dfe
MV
646/* scm_sym2var
647 *
648 * looks up the variable bound to SYM according to PROC. PROC should be
649 * a `eval closure' of some module.
650 *
651 * When no binding exists, and DEFINEP is true, create a new binding
652 * with a initial value of SCM_UNDEFINED. Return `#f' when DEFINEP as
653 * false and no binding exists.
654 *
655 * When PROC is `#f', it is ignored and the binding is searched for in
656 * the scm_pre_modules_obarray (a `eq' hash table).
657 */
658
86d31dfe
MV
659SCM
660scm_sym2var (SCM sym, SCM proc, SCM definep)
661#define FUNC_NAME "scm_sym2var"
662{
663 SCM var;
664
665 if (SCM_NIMP (proc))
666 {
667 if (SCM_EVAL_CLOSURE_P (proc))
668 {
669 /* Bypass evaluator in the standard case. */
670 var = scm_eval_closure_lookup (proc, sym, definep);
671 }
672 else
fdc28395 673 var = scm_call_2 (proc, sym, definep);
86d31dfe
MV
674 }
675 else
676 {
677 SCM handle;
678
7888309b 679 if (scm_is_false (definep))
86d31dfe
MV
680 var = scm_hashq_ref (scm_pre_modules_obarray, sym, SCM_BOOL_F);
681 else
682 {
683 handle = scm_hashq_create_handle_x (scm_pre_modules_obarray,
684 sym, SCM_BOOL_F);
685 var = SCM_CDR (handle);
7888309b 686 if (scm_is_false (var))
86d31dfe
MV
687 {
688 var = scm_make_variable (SCM_UNDEFINED);
86d31dfe
MV
689 SCM_SETCDR (handle, var);
690 }
691 }
692 }
693
7888309b 694 if (scm_is_true (var) && !SCM_VARIABLEP (var))
1afff620 695 SCM_MISC_ERROR ("~S is not bound to a variable", scm_list_1 (sym));
86d31dfe
MV
696
697 return var;
698}
699#undef FUNC_NAME
700
701SCM
702scm_c_module_lookup (SCM module, const char *name)
703{
cc95e00a 704 return scm_module_lookup (module, scm_from_locale_symbol (name));
86d31dfe
MV
705}
706
707SCM
708scm_module_lookup (SCM module, SCM sym)
709#define FUNC_NAME "module-lookup"
710{
711 SCM var;
712 SCM_VALIDATE_MODULE (1, module);
713
714 var = scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
7888309b 715 if (scm_is_false (var))
7ab42fa2 716 unbound_variable (FUNC_NAME, sym);
86d31dfe
MV
717 return var;
718}
719#undef FUNC_NAME
720
721SCM
722scm_c_lookup (const char *name)
723{
cc95e00a 724 return scm_lookup (scm_from_locale_symbol (name));
86d31dfe
MV
725}
726
727SCM
728scm_lookup (SCM sym)
729{
730 SCM var =
731 scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_F);
7888309b 732 if (scm_is_false (var))
7ab42fa2 733 unbound_variable (NULL, sym);
86d31dfe
MV
734 return var;
735}
736
737SCM
738scm_c_module_define (SCM module, const char *name, SCM value)
739{
cc95e00a 740 return scm_module_define (module, scm_from_locale_symbol (name), value);
86d31dfe
MV
741}
742
743SCM
744scm_module_define (SCM module, SCM sym, SCM value)
745#define FUNC_NAME "module-define"
746{
747 SCM var;
748 SCM_VALIDATE_MODULE (1, module);
749
750 var = scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_T);
751 SCM_VARIABLE_SET (var, value);
752 return var;
753}
754#undef FUNC_NAME
755
756SCM
757scm_c_define (const char *name, SCM value)
758{
cc95e00a 759 return scm_define (scm_from_locale_symbol (name), value);
86d31dfe
MV
760}
761
c7a2a803
AW
762SCM_DEFINE (scm_define, "define!", 2, 0, 0,
763 (SCM sym, SCM value),
764 "Define @var{sym} to be @var{value} in the current module."
765 "Returns the variable itself. Note that this is a procedure, "
766 "not a macro.")
767#define FUNC_NAME s_scm_define
86d31dfe 768{
c7a2a803
AW
769 SCM var;
770 SCM_VALIDATE_SYMBOL (SCM_ARG1, sym);
771 var = scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_T);
86d31dfe
MV
772 SCM_VARIABLE_SET (var, value);
773 return var;
774}
c7a2a803 775#undef FUNC_NAME
86d31dfe 776
608860a5
LC
777SCM_DEFINE (scm_module_reverse_lookup, "module-reverse-lookup", 2, 0, 0,
778 (SCM module, SCM variable),
779 "Return the symbol under which @var{variable} is bound in "
780 "@var{module} or @var{#f} if @var{variable} is not visible "
781 "from @var{module}. If @var{module} is @code{#f}, then the "
782 "pre-module obarray is used.")
783#define FUNC_NAME s_scm_module_reverse_lookup
86d31dfe
MV
784{
785 SCM obarray;
c014a02e 786 long i, n;
86d31dfe 787
7888309b 788 if (scm_is_false (module))
86d31dfe
MV
789 obarray = scm_pre_modules_obarray;
790 else
791 {
792 SCM_VALIDATE_MODULE (1, module);
793 obarray = SCM_MODULE_OBARRAY (module);
794 }
795
1606312f
LC
796 SCM_VALIDATE_VARIABLE (SCM_ARG2, variable);
797
6dc1cd1e
MV
798 if (!SCM_HASHTABLE_P (obarray))
799 return SCM_BOOL_F;
800
86d31dfe
MV
801 /* XXX - We do not use scm_hash_fold here to avoid searching the
802 whole obarray. We should have a scm_hash_find procedure. */
803
c35738c1 804 n = SCM_HASHTABLE_N_BUCKETS (obarray);
86d31dfe
MV
805 for (i = 0; i < n; ++i)
806 {
4057a3e0 807 SCM ls = SCM_HASHTABLE_BUCKET (obarray, i), handle;
d2e53ed6 808 while (!scm_is_null (ls))
86d31dfe
MV
809 {
810 handle = SCM_CAR (ls);
741e83fc
LC
811
812 if (SCM_CAR (handle) == SCM_PACK (NULL))
813 {
814 /* FIXME: We hit a weak pair whose car has become unreachable.
815 We should remove the pair in question or something. */
816 }
817 else
818 {
819 if (SCM_CDR (handle) == variable)
820 return SCM_CAR (handle);
821 }
822
86d31dfe
MV
823 ls = SCM_CDR (ls);
824 }
825 }
826
1606312f
LC
827 if (!scm_is_false (module))
828 {
829 /* Try the `uses' list. */
830 SCM uses = SCM_MODULE_USES (module);
831 while (scm_is_pair (uses))
832 {
833 SCM sym = scm_module_reverse_lookup (SCM_CAR (uses), variable);
834 if (scm_is_true (sym))
835 return sym;
836 uses = SCM_CDR (uses);
837 }
838 }
86d31dfe
MV
839
840 return SCM_BOOL_F;
841}
842#undef FUNC_NAME
843
844SCM_DEFINE (scm_get_pre_modules_obarray, "%get-pre-modules-obarray", 0, 0, 0,
845 (),
846 "Return the obarray that is used for all new bindings before "
847 "the module system is booted. The first call to "
848 "@code{set-current-module} will boot the module system.")
849#define FUNC_NAME s_scm_get_pre_modules_obarray
850{
851 return scm_pre_modules_obarray;
852}
853#undef FUNC_NAME
854
d02b98e9
MV
855SCM_SYMBOL (scm_sym_system_module, "system-module");
856
86d31dfe
MV
857void
858scm_modules_prehistory ()
859{
f39448c5 860 scm_pre_modules_obarray = scm_c_make_hash_table (1533);
86d31dfe
MV
861}
862
1ffa265b
MD
863void
864scm_init_modules ()
865{
a0599745 866#include "libguile/modules.x"
86d31dfe
MV
867 module_make_local_var_x_var = scm_c_define ("module-make-local-var!",
868 SCM_UNDEFINED);
e841c3e0 869 scm_tc16_eval_closure = scm_make_smob_type ("eval-closure", 0);
e841c3e0 870 scm_set_smob_apply (scm_tc16_eval_closure, scm_eval_closure_lookup, 2, 0, 0);
55000e5f 871
f39448c5 872 the_module = scm_make_fluid ();
1ffa265b
MD
873}
874
86d31dfe 875static void
1ffa265b
MD
876scm_post_boot_init_modules ()
877{
86d31dfe 878 SCM module_type = SCM_VARIABLE_REF (scm_c_lookup ("module-type"));
904a077d 879 scm_module_tag = (SCM_CELL_WORD_1 (module_type) + scm_tc3_struct);
d02b98e9 880
f39448c5
AW
881 resolve_module_var = scm_c_lookup ("resolve-module");
882 process_define_module_var = scm_c_lookup ("process-define-module");
883 process_use_modules_var = scm_c_lookup ("process-use-modules");
884 module_export_x_var = scm_c_lookup ("module-export!");
885 the_root_module_var = scm_c_lookup ("the-root-module");
886 default_duplicate_binding_procedures_var =
887 scm_c_lookup ("default-duplicate-binding-procedures");
993dae86 888 module_public_interface_var = scm_c_lookup ("module-public-interface");
d02b98e9 889
e3365c07 890 scm_module_system_booted_p = 1;
1ffa265b 891}
89e00824
ML
892
893/*
894 Local Variables:
895 c-file-style: "gnu"
896 End:
897*/