1 /* Copyright (C) 1998,2000,2001,2002,2003,2004,2006,2007,2008,2009 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
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.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include "libguile/_scm.h"
29 #include "libguile/eval.h"
30 #include "libguile/smob.h"
31 #include "libguile/procprop.h"
32 #include "libguile/vectors.h"
33 #include "libguile/hashtab.h"
34 #include "libguile/struct.h"
35 #include "libguile/variable.h"
36 #include "libguile/fluids.h"
37 #include "libguile/deprecation.h"
39 #include "libguile/modules.h"
41 int scm_module_system_booted_p
= 0;
43 scm_t_bits scm_module_tag
;
45 static SCM the_module
;
47 static SCM the_root_module_var
;
49 static SCM
unbound_variable (const char *func
, SCM sym
)
51 scm_error (scm_from_locale_symbol ("unbound-variable"), func
,
52 "Unbound variable: ~S", scm_list_1 (sym
), SCM_BOOL_F
);
56 scm_the_root_module (void)
58 if (scm_module_system_booted_p
)
59 return SCM_VARIABLE_REF (the_root_module_var
);
64 SCM_DEFINE (scm_current_module
, "current-module", 0, 0, 0,
66 "Return the current module.")
67 #define FUNC_NAME s_scm_current_module
69 SCM curr
= scm_fluid_ref (the_module
);
71 return scm_is_true (curr
) ? curr
: scm_the_root_module ();
75 static void scm_post_boot_init_modules (void);
77 SCM_DEFINE (scm_set_current_module
, "set-current-module", 1, 0, 0,
79 "Set the current module to @var{module} and return\n"
80 "the previous current module.")
81 #define FUNC_NAME s_scm_set_current_module
85 if (!scm_module_system_booted_p
)
86 scm_post_boot_init_modules ();
88 SCM_VALIDATE_MODULE (SCM_ARG1
, module
);
90 old
= scm_current_module ();
91 scm_fluid_set_x (the_module
, module
);
97 SCM_DEFINE (scm_interaction_environment
, "interaction-environment", 0, 0, 0,
99 "Return a specifier for the environment that contains\n"
100 "implementation--defined bindings, typically a superset of those\n"
101 "listed in the report. The intent is that this procedure will\n"
102 "return the environment in which the implementation would\n"
103 "evaluate expressions dynamically typed by the user.")
104 #define FUNC_NAME s_scm_interaction_environment
106 return scm_current_module ();
111 scm_c_call_with_current_module (SCM module
,
112 SCM (*func
)(void *), void *data
)
114 return scm_c_with_fluid (the_module
, module
, func
, data
);
118 scm_dynwind_current_module (SCM module
)
120 scm_dynwind_fluid (the_module
, module
);
124 convert "A B C" to scheme list (A B C)
127 convert_module_name (const char *name
)
138 while (*ptr
&& *ptr
!= ' ')
142 SCM sym
= scm_from_locale_symboln (name
, ptr
-name
);
143 *tail
= scm_cons (sym
, SCM_EOL
);
144 tail
= SCM_CDRLOC (*tail
);
152 static SCM process_define_module_var
;
153 static SCM process_use_modules_var
;
154 static SCM resolve_module_var
;
157 scm_c_resolve_module (const char *name
)
159 return scm_resolve_module (convert_module_name (name
));
163 scm_resolve_module (SCM name
)
165 return scm_call_1 (SCM_VARIABLE_REF (resolve_module_var
), name
);
169 scm_c_define_module (const char *name
,
170 void (*init
)(void *), void *data
)
172 SCM module
= scm_call_1 (SCM_VARIABLE_REF (process_define_module_var
),
173 scm_list_1 (convert_module_name (name
)));
175 scm_c_call_with_current_module (module
, (SCM (*)(void*))init
, data
);
180 scm_c_use_module (const char *name
)
182 scm_call_1 (SCM_VARIABLE_REF (process_use_modules_var
),
183 scm_list_1 (scm_list_1 (convert_module_name (name
))));
186 static SCM module_export_x_var
;
189 scm_module_export (SCM module
, SCM namelist
)
191 return scm_call_2 (SCM_VARIABLE_REF (module_export_x_var
),
197 @code{scm_c_export}(@var{name-list})
199 @code{scm_c_export} exports the named bindings from the current
200 module, making them visible to users of the module. This function
201 takes a list of string arguments, terminated by NULL, e.g.
204 scm_c_export ("add-double-record", "bamboozle-money", NULL);
208 scm_c_export (const char *name
, ...)
213 SCM names
= scm_cons (scm_from_locale_symbol (name
), SCM_EOL
);
214 SCM
*tail
= SCM_CDRLOC (names
);
218 const char *n
= va_arg (ap
, const char *);
221 *tail
= scm_cons (scm_from_locale_symbol (n
), SCM_EOL
);
222 tail
= SCM_CDRLOC (*tail
);
225 scm_module_export (scm_current_module (), names
);
232 SCM_SYMBOL (sym_module
, "module");
235 scm_lookup_closure_module (SCM proc
)
237 if (scm_is_false (proc
))
238 return scm_the_root_module ();
239 else if (SCM_EVAL_CLOSURE_P (proc
))
240 return SCM_PACK (SCM_SMOB_DATA (proc
));
245 /* FIXME: The `module' property is no longer set. See
246 `set-module-eval-closure!' in `boot-9.scm'. */
249 mod
= scm_procedure_property (proc
, sym_module
);
250 if (scm_is_false (mod
))
251 mod
= scm_the_root_module ();
257 * C level implementation of the standard eval closure
259 * This increases loading speed substantially. The code may be
260 * replaced by something based on environments.[ch], in a future
264 /* The `module-make-local-var!' variable. */
265 static SCM module_make_local_var_x_var
= SCM_UNSPECIFIED
;
267 /* The `default-duplicate-binding-procedures' variable. */
268 static SCM default_duplicate_binding_procedures_var
= SCM_UNSPECIFIED
;
270 /* Return the list of default duplicate binding handlers (procedures). */
272 default_duplicate_binding_handlers (void)
276 get_handlers
= SCM_VARIABLE_REF (default_duplicate_binding_procedures_var
);
278 return (scm_call_0 (get_handlers
));
281 /* Resolve the import of SYM in MODULE, where SYM is currently provided by
282 both IFACE1 as VAR1 and IFACE2 as VAR2. Return the variable chosen by the
283 duplicate binding handlers or `#f'. */
285 resolve_duplicate_binding (SCM module
, SCM sym
,
286 SCM iface1
, SCM var1
,
287 SCM iface2
, SCM var2
)
289 SCM result
= SCM_BOOL_F
;
291 if (!scm_is_eq (var1
, var2
))
294 SCM handlers
, h
, handler_args
;
296 val1
= SCM_VARIABLE_REF (var1
);
297 val2
= SCM_VARIABLE_REF (var2
);
299 val1
= (val1
== SCM_UNSPECIFIED
) ? SCM_BOOL_F
: val1
;
300 val2
= (val2
== SCM_UNSPECIFIED
) ? SCM_BOOL_F
: val2
;
302 handlers
= SCM_MODULE_DUPLICATE_HANDLERS (module
);
303 if (scm_is_false (handlers
))
304 handlers
= default_duplicate_binding_handlers ();
306 handler_args
= scm_list_n (module
, sym
,
307 iface1
, val1
, iface2
, val2
,
312 scm_is_pair (h
) && scm_is_false (result
);
315 result
= scm_apply (SCM_CAR (h
), handler_args
, SCM_EOL
);
324 SCM scm_pre_modules_obarray
;
326 /* Lookup SYM as an imported variable of MODULE. */
328 module_imported_variable (SCM module
, SCM sym
)
330 #define SCM_BOUND_THING_P scm_is_true
331 register SCM var
, imports
;
333 /* Search cached imported bindings. */
334 imports
= SCM_MODULE_IMPORT_OBARRAY (module
);
335 var
= scm_hashq_ref (imports
, sym
, SCM_UNDEFINED
);
336 if (SCM_BOUND_THING_P (var
))
340 /* Search the use list for yet uncached imported bindings, possibly
341 resolving duplicates as needed and caching the result in the import
344 SCM found_var
= SCM_BOOL_F
, found_iface
= SCM_BOOL_F
;
346 for (uses
= SCM_MODULE_USES (module
);
348 uses
= SCM_CDR (uses
))
352 iface
= SCM_CAR (uses
);
353 var
= scm_module_variable (iface
, sym
);
355 if (SCM_BOUND_THING_P (var
))
357 if (SCM_BOUND_THING_P (found_var
))
359 /* SYM is a duplicate binding (imported more than once) so we
360 need to resolve it. */
361 found_var
= resolve_duplicate_binding (module
, sym
,
362 found_iface
, found_var
,
364 if (scm_is_eq (found_var
, var
))
368 /* Keep track of the variable we found and check for other
369 occurences of SYM in the use list. */
370 found_var
= var
, found_iface
= iface
;
374 if (SCM_BOUND_THING_P (found_var
))
376 /* Save the lookup result for future reference. */
377 (void) scm_hashq_set_x (imports
, sym
, found_var
);
383 #undef SCM_BOUND_THING_P
386 SCM_DEFINE (scm_module_local_variable
, "module-local-variable", 2, 0, 0,
387 (SCM module
, SCM sym
),
388 "Return the variable bound to @var{sym} in @var{module}. Return "
389 "@code{#f} is @var{sym} is not bound locally in @var{module}.")
390 #define FUNC_NAME s_scm_module_local_variable
392 #define SCM_BOUND_THING_P(b) \
397 if (scm_module_system_booted_p
)
398 SCM_VALIDATE_MODULE (1, module
);
400 SCM_VALIDATE_SYMBOL (2, sym
);
402 if (scm_is_false (module
))
403 return scm_hashq_ref (scm_pre_modules_obarray
, sym
, SCM_UNDEFINED
);
405 /* 1. Check module obarray */
406 b
= scm_hashq_ref (SCM_MODULE_OBARRAY (module
), sym
, SCM_UNDEFINED
);
407 if (SCM_BOUND_THING_P (b
))
410 /* 2. Search imported bindings. In order to be consistent with
411 `module-variable', the binder gets called only when no imported binding
413 b
= module_imported_variable (module
, sym
);
414 if (SCM_BOUND_THING_P (b
))
418 /* 3. Query the custom binder. */
419 SCM binder
= SCM_MODULE_BINDER (module
);
421 if (scm_is_true (binder
))
423 b
= scm_call_3 (binder
, module
, sym
, SCM_BOOL_F
);
424 if (SCM_BOUND_THING_P (b
))
431 #undef SCM_BOUND_THING_P
435 SCM_DEFINE (scm_module_variable
, "module-variable", 2, 0, 0,
436 (SCM module
, SCM sym
),
437 "Return the variable bound to @var{sym} in @var{module}. This "
438 "may be both a local variable or an imported variable. Return "
439 "@code{#f} is @var{sym} is not bound in @var{module}.")
440 #define FUNC_NAME s_scm_module_variable
442 #define SCM_BOUND_THING_P(b) \
447 if (scm_module_system_booted_p
)
448 SCM_VALIDATE_MODULE (1, module
);
450 SCM_VALIDATE_SYMBOL (2, sym
);
452 if (scm_is_false (module
))
453 return scm_hashq_ref (scm_pre_modules_obarray
, sym
, SCM_UNDEFINED
);
455 /* 1. Check module obarray */
456 var
= scm_hashq_ref (SCM_MODULE_OBARRAY (module
), sym
, SCM_UNDEFINED
);
457 if (SCM_BOUND_THING_P (var
))
460 /* 2. Search among the imported variables. */
461 var
= module_imported_variable (module
, sym
);
462 if (SCM_BOUND_THING_P (var
))
466 /* 3. Query the custom binder. */
469 binder
= SCM_MODULE_BINDER (module
);
470 if (scm_is_true (binder
))
472 var
= scm_call_3 (binder
, module
, sym
, SCM_BOOL_F
);
473 if (SCM_BOUND_THING_P (var
))
480 #undef SCM_BOUND_THING_P
484 scm_t_bits scm_tc16_eval_closure
;
486 #define SCM_F_EVAL_CLOSURE_INTERFACE (1<<16)
487 #define SCM_EVAL_CLOSURE_INTERFACE_P(e) \
488 (SCM_CELL_WORD_0 (e) & SCM_F_EVAL_CLOSURE_INTERFACE)
490 /* NOTE: This function may be called by a smob application
491 or from another C function directly. */
493 scm_eval_closure_lookup (SCM eclo
, SCM sym
, SCM definep
)
495 SCM module
= SCM_PACK (SCM_SMOB_DATA (eclo
));
496 if (scm_is_true (definep
))
498 if (SCM_EVAL_CLOSURE_INTERFACE_P (eclo
))
500 return scm_call_2 (SCM_VARIABLE_REF (module_make_local_var_x_var
),
504 return scm_module_variable (module
, sym
);
507 SCM_DEFINE (scm_standard_eval_closure
, "standard-eval-closure", 1, 0, 0,
509 "Return an eval closure for the module @var{module}.")
510 #define FUNC_NAME s_scm_standard_eval_closure
512 SCM_RETURN_NEWSMOB (scm_tc16_eval_closure
, SCM_UNPACK (module
));
517 SCM_DEFINE (scm_standard_interface_eval_closure
,
518 "standard-interface-eval-closure", 1, 0, 0,
520 "Return a interface eval closure for the module @var{module}. "
521 "Such a closure does not allow new bindings to be added.")
522 #define FUNC_NAME s_scm_standard_interface_eval_closure
524 SCM_RETURN_NEWSMOB (scm_tc16_eval_closure
| SCM_F_EVAL_CLOSURE_INTERFACE
,
525 SCM_UNPACK (module
));
529 SCM_DEFINE (scm_eval_closure_module
,
530 "eval-closure-module", 1, 0, 0,
532 "Return the module associated with this eval closure.")
533 /* the idea is that eval closures are really not the way to do things, they're
534 superfluous given our module system. this function lets mmacros migrate away
535 from eval closures. */
536 #define FUNC_NAME s_scm_eval_closure_module
538 SCM_MAKE_VALIDATE_MSG (SCM_ARG1
, eval_closure
, EVAL_CLOSURE_P
,
540 return SCM_SMOB_OBJECT (eval_closure
);
545 scm_module_lookup_closure (SCM module
)
547 if (scm_is_false (module
))
550 return SCM_MODULE_EVAL_CLOSURE (module
);
554 scm_current_module_lookup_closure ()
556 if (scm_module_system_booted_p
)
557 return scm_module_lookup_closure (scm_current_module ());
562 SCM_SYMBOL (sym_sys_pre_modules_transformer
, "%pre-modules-transformer");
564 SCM_DEFINE (scm_module_transformer
, "module-transformer", 1, 0, 0,
566 "Returns the syntax expander for the given module.")
567 #define FUNC_NAME s_scm_module_transformer
569 if (SCM_UNLIKELY (scm_is_false (module
)))
570 { SCM v
= scm_hashq_ref (scm_pre_modules_obarray
,
571 sym_sys_pre_modules_transformer
,
573 if (scm_is_false (v
))
576 return SCM_VARIABLE_REF (v
);
580 SCM_VALIDATE_MODULE (SCM_ARG1
, module
);
581 return SCM_MODULE_TRANSFORMER (module
);
587 scm_current_module_transformer ()
589 return scm_module_transformer (scm_current_module ());
592 SCM_DEFINE (scm_module_import_interface
, "module-import-interface", 2, 0, 0,
593 (SCM module
, SCM sym
),
594 "Return the module or interface from which @var{sym} is imported "
595 "in @var{module}. If @var{sym} is not imported (i.e., it is not "
596 "defined in @var{module} or it is a module-local binding instead "
597 "of an imported one), then @code{#f} is returned.")
598 #define FUNC_NAME s_scm_module_import_interface
600 SCM var
, result
= SCM_BOOL_F
;
602 SCM_VALIDATE_MODULE (1, module
);
603 SCM_VALIDATE_SYMBOL (2, sym
);
605 var
= scm_module_variable (module
, sym
);
606 if (scm_is_true (var
))
608 /* Look for the module that provides VAR. */
611 local_var
= scm_hashq_ref (SCM_MODULE_OBARRAY (module
), sym
,
613 if (scm_is_eq (local_var
, var
))
617 /* Look for VAR among the used modules. */
618 SCM uses
, imported_var
;
620 for (uses
= SCM_MODULE_USES (module
);
621 scm_is_pair (uses
) && scm_is_false (result
);
622 uses
= SCM_CDR (uses
))
624 imported_var
= scm_module_variable (SCM_CAR (uses
), sym
);
625 if (scm_is_eq (imported_var
, var
))
626 result
= SCM_CAR (uses
);
635 SCM_SYMBOL (sym_sys_module_public_interface
, "%module-public-interface");
637 SCM_DEFINE (scm_module_public_interface
, "module-public-interface", 1, 0, 0,
639 "Return the public interface of @var{module}.\n\n"
640 "If @var{module} has no public interface, @code{#f} is returned.")
641 #define FUNC_NAME s_scm_module_public_interface
645 SCM_VALIDATE_MODULE (1, module
);
646 var
= scm_module_local_variable (module
, sym_sys_module_public_interface
);
647 if (scm_is_true (var
))
648 return SCM_VARIABLE_REF (var
);
656 * looks up the variable bound to SYM according to PROC. PROC should be
657 * a `eval closure' of some module.
659 * When no binding exists, and DEFINEP is true, create a new binding
660 * with a initial value of SCM_UNDEFINED. Return `#f' when DEFINEP as
661 * false and no binding exists.
663 * When PROC is `#f', it is ignored and the binding is searched for in
664 * the scm_pre_modules_obarray (a `eq' hash table).
668 scm_sym2var (SCM sym
, SCM proc
, SCM definep
)
669 #define FUNC_NAME "scm_sym2var"
675 if (SCM_EVAL_CLOSURE_P (proc
))
677 /* Bypass evaluator in the standard case. */
678 var
= scm_eval_closure_lookup (proc
, sym
, definep
);
681 var
= scm_call_2 (proc
, sym
, definep
);
687 if (scm_is_false (definep
))
688 var
= scm_hashq_ref (scm_pre_modules_obarray
, sym
, SCM_BOOL_F
);
691 handle
= scm_hashq_create_handle_x (scm_pre_modules_obarray
,
693 var
= SCM_CDR (handle
);
694 if (scm_is_false (var
))
696 var
= scm_make_variable (SCM_UNDEFINED
);
697 SCM_SETCDR (handle
, var
);
702 if (scm_is_true (var
) && !SCM_VARIABLEP (var
))
703 SCM_MISC_ERROR ("~S is not bound to a variable", scm_list_1 (sym
));
710 scm_c_module_lookup (SCM module
, const char *name
)
712 return scm_module_lookup (module
, scm_from_locale_symbol (name
));
716 scm_module_lookup (SCM module
, SCM sym
)
717 #define FUNC_NAME "module-lookup"
720 SCM_VALIDATE_MODULE (1, module
);
722 var
= scm_sym2var (sym
, scm_module_lookup_closure (module
), SCM_BOOL_F
);
723 if (scm_is_false (var
))
724 unbound_variable (FUNC_NAME
, sym
);
730 scm_c_lookup (const char *name
)
732 return scm_lookup (scm_from_locale_symbol (name
));
739 scm_sym2var (sym
, scm_current_module_lookup_closure (), SCM_BOOL_F
);
740 if (scm_is_false (var
))
741 unbound_variable (NULL
, sym
);
746 scm_c_module_define (SCM module
, const char *name
, SCM value
)
748 return scm_module_define (module
, scm_from_locale_symbol (name
), value
);
752 scm_module_define (SCM module
, SCM sym
, SCM value
)
753 #define FUNC_NAME "module-define"
756 SCM_VALIDATE_MODULE (1, module
);
758 var
= scm_sym2var (sym
, scm_module_lookup_closure (module
), SCM_BOOL_T
);
759 SCM_VARIABLE_SET (var
, value
);
765 scm_c_define (const char *name
, SCM value
)
767 return scm_define (scm_from_locale_symbol (name
), value
);
770 SCM_DEFINE (scm_define
, "define!", 2, 0, 0,
771 (SCM sym
, SCM value
),
772 "Define @var{sym} to be @var{value} in the current module."
773 "Returns the variable itself. Note that this is a procedure, "
775 #define FUNC_NAME s_scm_define
778 SCM_VALIDATE_SYMBOL (SCM_ARG1
, sym
);
779 var
= scm_sym2var (sym
, scm_current_module_lookup_closure (), SCM_BOOL_T
);
780 SCM_VARIABLE_SET (var
, value
);
785 SCM_DEFINE (scm_module_reverse_lookup
, "module-reverse-lookup", 2, 0, 0,
786 (SCM module
, SCM variable
),
787 "Return the symbol under which @var{variable} is bound in "
788 "@var{module} or @var{#f} if @var{variable} is not visible "
789 "from @var{module}. If @var{module} is @code{#f}, then the "
790 "pre-module obarray is used.")
791 #define FUNC_NAME s_scm_module_reverse_lookup
796 if (scm_is_false (module
))
797 obarray
= scm_pre_modules_obarray
;
800 SCM_VALIDATE_MODULE (1, module
);
801 obarray
= SCM_MODULE_OBARRAY (module
);
804 if (!SCM_HASHTABLE_P (obarray
))
807 /* XXX - We do not use scm_hash_fold here to avoid searching the
808 whole obarray. We should have a scm_hash_find procedure. */
810 n
= SCM_HASHTABLE_N_BUCKETS (obarray
);
811 for (i
= 0; i
< n
; ++i
)
813 SCM ls
= SCM_HASHTABLE_BUCKET (obarray
, i
), handle
;
814 while (!scm_is_null (ls
))
816 handle
= SCM_CAR (ls
);
818 if (SCM_CAR (handle
) == SCM_PACK (NULL
))
820 /* FIXME: We hit a weak pair whose car has become unreachable.
821 We should remove the pair in question or something. */
825 if (SCM_CDR (handle
) == variable
)
826 return SCM_CAR (handle
);
833 /* Try the `uses' list. */
835 SCM uses
= SCM_MODULE_USES (module
);
836 while (scm_is_pair (uses
))
838 SCM sym
= scm_module_reverse_lookup (SCM_CAR (uses
), variable
);
839 if (scm_is_true (sym
))
841 uses
= SCM_CDR (uses
);
849 SCM_DEFINE (scm_get_pre_modules_obarray
, "%get-pre-modules-obarray", 0, 0, 0,
851 "Return the obarray that is used for all new bindings before "
852 "the module system is booted. The first call to "
853 "@code{set-current-module} will boot the module system.")
854 #define FUNC_NAME s_scm_get_pre_modules_obarray
856 return scm_pre_modules_obarray
;
860 SCM_SYMBOL (scm_sym_system_module
, "system-module");
863 scm_modules_prehistory ()
865 scm_pre_modules_obarray
= scm_c_make_hash_table (1533);
871 #include "libguile/modules.x"
872 module_make_local_var_x_var
= scm_c_define ("module-make-local-var!",
874 scm_tc16_eval_closure
= scm_make_smob_type ("eval-closure", 0);
875 scm_set_smob_apply (scm_tc16_eval_closure
, scm_eval_closure_lookup
, 2, 0, 0);
877 the_module
= scm_make_fluid ();
881 scm_post_boot_init_modules ()
883 SCM module_type
= SCM_VARIABLE_REF (scm_c_lookup ("module-type"));
884 scm_module_tag
= (SCM_CELL_WORD_1 (module_type
) + scm_tc3_struct
);
886 resolve_module_var
= scm_c_lookup ("resolve-module");
887 process_define_module_var
= scm_c_lookup ("process-define-module");
888 process_use_modules_var
= scm_c_lookup ("process-use-modules");
889 module_export_x_var
= scm_c_lookup ("module-export!");
890 the_root_module_var
= scm_c_lookup ("the-root-module");
891 default_duplicate_binding_procedures_var
=
892 scm_c_lookup ("default-duplicate-binding-procedures");
894 scm_module_system_booted_p
= 1;