modules.c 27 KB

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