modules.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /* Copyright (C) 1998,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012 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. if (scm_module_system_booted_p)
  68. return scm_fluid_ref (the_module);
  69. else
  70. return SCM_BOOL_F;
  71. }
  72. #undef FUNC_NAME
  73. static void scm_post_boot_init_modules (void);
  74. SCM_DEFINE (scm_set_current_module, "set-current-module", 1, 0, 0,
  75. (SCM module),
  76. "Set the current module to @var{module} and return\n"
  77. "the previous current module.")
  78. #define FUNC_NAME s_scm_set_current_module
  79. {
  80. SCM old;
  81. if (!scm_module_system_booted_p)
  82. scm_post_boot_init_modules ();
  83. SCM_VALIDATE_MODULE (SCM_ARG1, module);
  84. old = scm_current_module ();
  85. scm_fluid_set_x (the_module, module);
  86. return old;
  87. }
  88. #undef FUNC_NAME
  89. SCM_DEFINE (scm_interaction_environment, "interaction-environment", 0, 0, 0,
  90. (),
  91. "Return a specifier for the environment that contains\n"
  92. "implementation--defined bindings, typically a superset of those\n"
  93. "listed in the report. The intent is that this procedure will\n"
  94. "return the environment in which the implementation would\n"
  95. "evaluate expressions dynamically typed by the user.")
  96. #define FUNC_NAME s_scm_interaction_environment
  97. {
  98. return scm_current_module ();
  99. }
  100. #undef FUNC_NAME
  101. SCM
  102. scm_c_call_with_current_module (SCM module,
  103. SCM (*func)(void *), void *data)
  104. {
  105. return scm_c_with_fluid (the_module, module, func, data);
  106. }
  107. void
  108. scm_dynwind_current_module (SCM module)
  109. {
  110. scm_dynwind_fluid (the_module, module);
  111. }
  112. /*
  113. convert "A B C" to scheme list (A B C)
  114. */
  115. static SCM
  116. convert_module_name (const char *name)
  117. {
  118. SCM list = SCM_EOL;
  119. SCM *tail = &list;
  120. const char *ptr;
  121. while (*name)
  122. {
  123. while (*name == ' ')
  124. name++;
  125. ptr = name;
  126. while (*ptr && *ptr != ' ')
  127. ptr++;
  128. if (ptr > name)
  129. {
  130. SCM sym = scm_from_utf8_symboln (name, ptr-name);
  131. *tail = scm_cons (sym, SCM_EOL);
  132. tail = SCM_CDRLOC (*tail);
  133. }
  134. name = ptr;
  135. }
  136. return list;
  137. }
  138. SCM
  139. scm_c_resolve_module (const char *name)
  140. {
  141. return scm_resolve_module (convert_module_name (name));
  142. }
  143. SCM
  144. scm_resolve_module (SCM name)
  145. {
  146. return scm_call_1 (SCM_VARIABLE_REF (resolve_module_var), name);
  147. }
  148. SCM
  149. scm_c_define_module (const char *name,
  150. void (*init)(void *), void *data)
  151. {
  152. SCM module = scm_call_1 (SCM_VARIABLE_REF (define_module_star_var),
  153. convert_module_name (name));
  154. if (init)
  155. scm_c_call_with_current_module (module, (SCM (*)(void*))init, data);
  156. return module;
  157. }
  158. void
  159. scm_c_use_module (const char *name)
  160. {
  161. scm_call_1 (SCM_VARIABLE_REF (process_use_modules_var),
  162. scm_list_1 (scm_list_1 (convert_module_name (name))));
  163. }
  164. SCM
  165. scm_module_export (SCM module, SCM namelist)
  166. {
  167. return scm_call_2 (SCM_VARIABLE_REF (module_export_x_var),
  168. module, namelist);
  169. }
  170. /*
  171. @code{scm_c_export}(@var{name-list})
  172. @code{scm_c_export} exports the named bindings from the current
  173. module, making them visible to users of the module. This function
  174. takes a list of string arguments, terminated by NULL, e.g.
  175. @example
  176. scm_c_export ("add-double-record", "bamboozle-money", NULL);
  177. @end example
  178. */
  179. void
  180. scm_c_export (const char *name, ...)
  181. {
  182. if (name)
  183. {
  184. va_list ap;
  185. SCM names = scm_cons (scm_from_utf8_symbol (name), SCM_EOL);
  186. SCM *tail = SCM_CDRLOC (names);
  187. va_start (ap, name);
  188. while (1)
  189. {
  190. const char *n = va_arg (ap, const char *);
  191. if (n == NULL)
  192. break;
  193. *tail = scm_cons (scm_from_utf8_symbol (n), SCM_EOL);
  194. tail = SCM_CDRLOC (*tail);
  195. }
  196. va_end (ap);
  197. scm_module_export (scm_current_module (), names);
  198. }
  199. }
  200. /*
  201. * C level implementation of the standard eval closure
  202. *
  203. * This increases loading speed substantially. The code may be
  204. * replaced by something based on environments.[ch], in a future
  205. * release.
  206. */
  207. /* Return the list of default duplicate binding handlers (procedures). */
  208. static inline SCM
  209. default_duplicate_binding_handlers (void)
  210. {
  211. SCM get_handlers;
  212. get_handlers = SCM_VARIABLE_REF (default_duplicate_binding_procedures_var);
  213. return (scm_call_0 (get_handlers));
  214. }
  215. /* Resolve the import of SYM in MODULE, where SYM is currently provided by
  216. both IFACE1 as VAR1 and IFACE2 as VAR2. Return the variable chosen by the
  217. duplicate binding handlers or `#f'. */
  218. static inline SCM
  219. resolve_duplicate_binding (SCM module, SCM sym,
  220. SCM iface1, SCM var1,
  221. SCM iface2, SCM var2)
  222. {
  223. SCM args[8];
  224. SCM handlers;
  225. SCM result = SCM_BOOL_F;
  226. if (scm_is_eq (var1, var2))
  227. return var1;
  228. args[0] = module;
  229. args[1] = sym;
  230. args[2] = iface1;
  231. args[3] = SCM_VARIABLE_REF (var1);
  232. if (SCM_UNBNDP (args[3]))
  233. args[3] = SCM_BOOL_F;
  234. args[4] = iface2;
  235. args[5] = SCM_VARIABLE_REF (var2);
  236. if (SCM_UNBNDP (args[5]))
  237. args[5] = SCM_BOOL_F;
  238. args[6] = scm_hashq_ref (SCM_MODULE_IMPORT_OBARRAY (module), sym, SCM_BOOL_F);
  239. args[7] = SCM_BOOL_F;
  240. handlers = SCM_MODULE_DUPLICATE_HANDLERS (module);
  241. if (scm_is_false (handlers))
  242. handlers = default_duplicate_binding_handlers ();
  243. for (; scm_is_pair (handlers); handlers = SCM_CDR (handlers))
  244. {
  245. if (scm_is_true (args[6]))
  246. {
  247. args[7] = SCM_VARIABLE_REF (args[6]);
  248. if (SCM_UNBNDP (args[7]))
  249. args[7] = SCM_BOOL_F;
  250. }
  251. result = scm_call_n (SCM_CAR (handlers), args, 8);
  252. if (scm_is_true (result))
  253. return result;
  254. }
  255. return SCM_BOOL_F;
  256. }
  257. /* No lock is needed for access to this variable, as there are no
  258. threads before modules are booted. */
  259. SCM scm_pre_modules_obarray;
  260. /* Lookup SYM as an imported variable of MODULE. */
  261. static inline SCM
  262. module_imported_variable (SCM module, SCM sym)
  263. {
  264. #define SCM_BOUND_THING_P scm_is_true
  265. register SCM var, imports;
  266. /* Search cached imported bindings. */
  267. imports = SCM_MODULE_IMPORT_OBARRAY (module);
  268. var = scm_hashq_ref (imports, sym, SCM_UNDEFINED);
  269. if (SCM_BOUND_THING_P (var))
  270. return var;
  271. {
  272. /* Search the use list for yet uncached imported bindings, possibly
  273. resolving duplicates as needed and caching the result in the import
  274. obarray. */
  275. SCM uses;
  276. SCM found_var = SCM_BOOL_F, found_iface = SCM_BOOL_F;
  277. for (uses = SCM_MODULE_USES (module);
  278. scm_is_pair (uses);
  279. uses = SCM_CDR (uses))
  280. {
  281. SCM iface;
  282. iface = SCM_CAR (uses);
  283. var = scm_module_variable (iface, sym);
  284. if (SCM_BOUND_THING_P (var))
  285. {
  286. if (SCM_BOUND_THING_P (found_var))
  287. {
  288. /* SYM is a duplicate binding (imported more than once) so we
  289. need to resolve it. */
  290. found_var = resolve_duplicate_binding (module, sym,
  291. found_iface, found_var,
  292. iface, var);
  293. /* Note that it could be that FOUND_VAR doesn't belong
  294. either to FOUND_IFACE or to IFACE, if it was created
  295. by merge-generics. The right thing to do there would
  296. be to treat the import obarray as the iface, but the
  297. import obarray isn't actually a module. Oh well. */
  298. if (scm_is_eq (found_var, var))
  299. found_iface = iface;
  300. }
  301. else
  302. /* Keep track of the variable we found and check for other
  303. occurences of SYM in the use list. */
  304. found_var = var, found_iface = iface;
  305. }
  306. }
  307. if (SCM_BOUND_THING_P (found_var))
  308. {
  309. /* Save the lookup result for future reference. */
  310. (void) scm_hashq_set_x (imports, sym, found_var);
  311. return found_var;
  312. }
  313. }
  314. return SCM_BOOL_F;
  315. #undef SCM_BOUND_THING_P
  316. }
  317. SCM_DEFINE (scm_module_local_variable, "module-local-variable", 2, 0, 0,
  318. (SCM module, SCM sym),
  319. "Return the variable bound to @var{sym} in @var{module}. Return "
  320. "@code{#f} is @var{sym} is not bound locally in @var{module}.")
  321. #define FUNC_NAME s_scm_module_local_variable
  322. {
  323. #define SCM_BOUND_THING_P(b) \
  324. (scm_is_true (b))
  325. register SCM b;
  326. if (scm_module_system_booted_p)
  327. SCM_VALIDATE_MODULE (1, module);
  328. SCM_VALIDATE_SYMBOL (2, sym);
  329. if (scm_is_false (module))
  330. return scm_hashq_ref (scm_pre_modules_obarray, sym, SCM_UNDEFINED);
  331. /* 1. Check module obarray */
  332. b = scm_hashq_ref (SCM_MODULE_OBARRAY (module), sym, SCM_UNDEFINED);
  333. if (SCM_BOUND_THING_P (b))
  334. return b;
  335. /* At this point we should just be able to return #f, but there is the
  336. possibility that a custom binder establishes a mapping for this
  337. variable.
  338. However a custom binder should be called only if there is no
  339. imported binding with the name SYM. So here instead of the order:
  340. 2. Search imported bindings. In order to be consistent with
  341. `module-variable', the binder gets called only when no
  342. imported binding matches SYM.
  343. 3. Query the custom binder.
  344. we first check if there is a binder at all, and if not, just return
  345. #f directly.
  346. */
  347. {
  348. SCM binder = SCM_MODULE_BINDER (module);
  349. if (scm_is_true (binder))
  350. {
  351. /* 2. */
  352. b = module_imported_variable (module, sym);
  353. if (SCM_BOUND_THING_P (b))
  354. return SCM_BOOL_F;
  355. /* 3. */
  356. b = scm_call_3 (binder, module, sym, SCM_BOOL_F);
  357. if (SCM_BOUND_THING_P (b))
  358. return b;
  359. }
  360. }
  361. return SCM_BOOL_F;
  362. #undef SCM_BOUND_THING_P
  363. }
  364. #undef FUNC_NAME
  365. SCM_DEFINE (scm_module_variable, "module-variable", 2, 0, 0,
  366. (SCM module, SCM sym),
  367. "Return the variable bound to @var{sym} in @var{module}. This "
  368. "may be both a local variable or an imported variable. Return "
  369. "@code{#f} is @var{sym} is not bound in @var{module}.")
  370. #define FUNC_NAME s_scm_module_variable
  371. {
  372. #define SCM_BOUND_THING_P(b) \
  373. (scm_is_true (b))
  374. register SCM var;
  375. if (scm_module_system_booted_p)
  376. SCM_VALIDATE_MODULE (1, module);
  377. SCM_VALIDATE_SYMBOL (2, sym);
  378. if (scm_is_false (module))
  379. return scm_hashq_ref (scm_pre_modules_obarray, sym, SCM_UNDEFINED);
  380. /* 1. Check module obarray */
  381. var = scm_hashq_ref (SCM_MODULE_OBARRAY (module), sym, SCM_UNDEFINED);
  382. if (SCM_BOUND_THING_P (var))
  383. return var;
  384. /* 2. Search among the imported variables. */
  385. var = module_imported_variable (module, sym);
  386. if (SCM_BOUND_THING_P (var))
  387. return var;
  388. {
  389. /* 3. Query the custom binder. */
  390. SCM binder;
  391. binder = SCM_MODULE_BINDER (module);
  392. if (scm_is_true (binder))
  393. {
  394. var = scm_call_3 (binder, module, sym, SCM_BOOL_F);
  395. if (SCM_BOUND_THING_P (var))
  396. return var;
  397. }
  398. }
  399. return SCM_BOOL_F;
  400. #undef SCM_BOUND_THING_P
  401. }
  402. #undef FUNC_NAME
  403. SCM
  404. scm_module_ensure_local_variable (SCM module, SCM sym)
  405. #define FUNC_NAME "module-ensure-local-variable"
  406. {
  407. if (SCM_LIKELY (scm_module_system_booted_p))
  408. {
  409. SCM_VALIDATE_MODULE (1, module);
  410. SCM_VALIDATE_SYMBOL (2, sym);
  411. return scm_call_2 (SCM_VARIABLE_REF (module_make_local_var_x_var),
  412. module, sym);
  413. }
  414. {
  415. SCM handle, var;
  416. handle = scm_hashq_create_handle_x (scm_pre_modules_obarray,
  417. sym, SCM_BOOL_F);
  418. var = SCM_CDR (handle);
  419. if (scm_is_false (var))
  420. {
  421. var = scm_make_variable (SCM_UNDEFINED);
  422. SCM_SETCDR (handle, var);
  423. }
  424. return var;
  425. }
  426. }
  427. #undef FUNC_NAME
  428. SCM_SYMBOL (sym_macroexpand, "macroexpand");
  429. SCM_DEFINE (scm_module_transformer, "module-transformer", 1, 0, 0,
  430. (SCM module),
  431. "Returns the syntax expander for the given module.")
  432. #define FUNC_NAME s_scm_module_transformer
  433. {
  434. if (SCM_UNLIKELY (scm_is_false (module)))
  435. {
  436. SCM v = scm_hashq_ref (scm_pre_modules_obarray,
  437. sym_macroexpand,
  438. SCM_BOOL_F);
  439. if (scm_is_false (v))
  440. SCM_MISC_ERROR ("no module, and `macroexpand' unbound", SCM_EOL);
  441. return SCM_VARIABLE_REF (v);
  442. }
  443. else
  444. {
  445. SCM_VALIDATE_MODULE (SCM_ARG1, module);
  446. return SCM_MODULE_TRANSFORMER (module);
  447. }
  448. }
  449. #undef FUNC_NAME
  450. SCM
  451. scm_current_module_transformer ()
  452. {
  453. return scm_module_transformer (scm_current_module ());
  454. }
  455. SCM_DEFINE (scm_module_import_interface, "module-import-interface", 2, 0, 0,
  456. (SCM module, SCM sym),
  457. "Return the module or interface from which @var{sym} is imported "
  458. "in @var{module}. If @var{sym} is not imported (i.e., it is not "
  459. "defined in @var{module} or it is a module-local binding instead "
  460. "of an imported one), then @code{#f} is returned.")
  461. #define FUNC_NAME s_scm_module_import_interface
  462. {
  463. SCM var, result = SCM_BOOL_F;
  464. SCM_VALIDATE_MODULE (1, module);
  465. SCM_VALIDATE_SYMBOL (2, sym);
  466. var = scm_module_variable (module, sym);
  467. if (scm_is_true (var))
  468. {
  469. /* Look for the module that provides VAR. */
  470. SCM local_var;
  471. local_var = scm_hashq_ref (SCM_MODULE_OBARRAY (module), sym,
  472. SCM_UNDEFINED);
  473. if (scm_is_eq (local_var, var))
  474. result = module;
  475. else
  476. {
  477. /* Look for VAR among the used modules. */
  478. SCM uses, imported_var;
  479. for (uses = SCM_MODULE_USES (module);
  480. scm_is_pair (uses) && scm_is_false (result);
  481. uses = SCM_CDR (uses))
  482. {
  483. imported_var = scm_module_variable (SCM_CAR (uses), sym);
  484. if (scm_is_eq (imported_var, var))
  485. result = SCM_CAR (uses);
  486. }
  487. }
  488. }
  489. return result;
  490. }
  491. #undef FUNC_NAME
  492. SCM
  493. scm_module_public_interface (SCM module)
  494. {
  495. return scm_call_1 (SCM_VARIABLE_REF (module_public_interface_var), module);
  496. }
  497. SCM
  498. scm_c_module_lookup (SCM module, const char *name)
  499. {
  500. return scm_module_lookup (module, scm_from_utf8_symbol (name));
  501. }
  502. SCM
  503. scm_module_lookup (SCM module, SCM sym)
  504. #define FUNC_NAME "module-lookup"
  505. {
  506. SCM var;
  507. var = scm_module_variable (module, sym);
  508. if (scm_is_false (var))
  509. unbound_variable (FUNC_NAME, sym);
  510. return var;
  511. }
  512. #undef FUNC_NAME
  513. SCM
  514. scm_c_lookup (const char *name)
  515. {
  516. return scm_lookup (scm_from_utf8_symbol (name));
  517. }
  518. SCM
  519. scm_lookup (SCM sym)
  520. {
  521. return scm_module_lookup (scm_current_module (), sym);
  522. }
  523. SCM
  524. scm_public_variable (SCM module_name, SCM name)
  525. {
  526. SCM mod, iface;
  527. mod = scm_call_3 (scm_variable_ref (resolve_module_var), module_name,
  528. k_ensure, SCM_BOOL_F);
  529. if (scm_is_false (mod))
  530. scm_misc_error ("public-lookup", "Module named ~s does not exist",
  531. scm_list_1 (module_name));
  532. iface = scm_module_public_interface (mod);
  533. if (scm_is_false (iface))
  534. scm_misc_error ("public-lookup", "Module ~s has no public interface",
  535. scm_list_1 (mod));
  536. return scm_module_variable (iface, name);
  537. }
  538. SCM
  539. scm_private_variable (SCM module_name, SCM name)
  540. {
  541. SCM mod;
  542. mod = scm_call_3 (scm_variable_ref (resolve_module_var), module_name,
  543. k_ensure, SCM_BOOL_F);
  544. if (scm_is_false (mod))
  545. scm_misc_error ("private-lookup", "Module named ~s does not exist",
  546. scm_list_1 (module_name));
  547. return scm_module_variable (mod, name);
  548. }
  549. SCM
  550. scm_c_public_variable (const char *module_name, const char *name)
  551. {
  552. return scm_public_variable (convert_module_name (module_name),
  553. scm_from_utf8_symbol (name));
  554. }
  555. SCM
  556. scm_c_private_variable (const char *module_name, const char *name)
  557. {
  558. return scm_private_variable (convert_module_name (module_name),
  559. scm_from_utf8_symbol (name));
  560. }
  561. SCM
  562. scm_public_lookup (SCM module_name, SCM name)
  563. {
  564. SCM var;
  565. var = scm_public_variable (module_name, name);
  566. if (scm_is_false (var))
  567. scm_misc_error ("public-lookup", "No variable bound to ~s in module ~s",
  568. scm_list_2 (name, module_name));
  569. return var;
  570. }
  571. SCM
  572. scm_private_lookup (SCM module_name, SCM name)
  573. {
  574. SCM var;
  575. var = scm_private_variable (module_name, name);
  576. if (scm_is_false (var))
  577. scm_misc_error ("private-lookup", "No variable bound to ~s in module ~s",
  578. scm_list_2 (name, module_name));
  579. return var;
  580. }
  581. SCM
  582. scm_c_public_lookup (const char *module_name, const char *name)
  583. {
  584. return scm_public_lookup (convert_module_name (module_name),
  585. scm_from_utf8_symbol (name));
  586. }
  587. SCM
  588. scm_c_private_lookup (const char *module_name, const char *name)
  589. {
  590. return scm_private_lookup (convert_module_name (module_name),
  591. scm_from_utf8_symbol (name));
  592. }
  593. SCM
  594. scm_public_ref (SCM module_name, SCM name)
  595. {
  596. return scm_variable_ref (scm_public_lookup (module_name, name));
  597. }
  598. SCM
  599. scm_private_ref (SCM module_name, SCM name)
  600. {
  601. return scm_variable_ref (scm_private_lookup (module_name, name));
  602. }
  603. SCM
  604. scm_c_public_ref (const char *module_name, const char *name)
  605. {
  606. return scm_public_ref (convert_module_name (module_name),
  607. scm_from_utf8_symbol (name));
  608. }
  609. SCM
  610. scm_c_private_ref (const char *module_name, const char *name)
  611. {
  612. return scm_private_ref (convert_module_name (module_name),
  613. scm_from_utf8_symbol (name));
  614. }
  615. SCM
  616. scm_c_module_define (SCM module, const char *name, SCM value)
  617. {
  618. return scm_module_define (module, scm_from_utf8_symbol (name), value);
  619. }
  620. SCM
  621. scm_module_define (SCM module, SCM sym, SCM value)
  622. #define FUNC_NAME "module-define"
  623. {
  624. SCM var;
  625. var = scm_module_ensure_local_variable (module, sym);
  626. SCM_VARIABLE_SET (var, value);
  627. return var;
  628. }
  629. #undef FUNC_NAME
  630. SCM
  631. scm_c_define (const char *name, SCM value)
  632. {
  633. return scm_define (scm_from_utf8_symbol (name), value);
  634. }
  635. SCM_DEFINE (scm_define, "define!", 2, 0, 0,
  636. (SCM sym, SCM value),
  637. "Define @var{sym} to be @var{value} in the current module."
  638. "Returns the variable itself. Note that this is a procedure, "
  639. "not a macro.")
  640. #define FUNC_NAME s_scm_define
  641. {
  642. SCM_VALIDATE_SYMBOL (SCM_ARG1, sym);
  643. return scm_module_define (scm_current_module (), sym, value);
  644. }
  645. #undef FUNC_NAME
  646. SCM_DEFINE (scm_module_reverse_lookup, "module-reverse-lookup", 2, 0, 0,
  647. (SCM module, SCM variable),
  648. "Return the symbol under which @var{variable} is bound in "
  649. "@var{module} or @var{#f} if @var{variable} is not visible "
  650. "from @var{module}. If @var{module} is @code{#f}, then the "
  651. "pre-module obarray is used.")
  652. #define FUNC_NAME s_scm_module_reverse_lookup
  653. {
  654. SCM obarray;
  655. long i, n;
  656. if (scm_is_false (module))
  657. obarray = scm_pre_modules_obarray;
  658. else
  659. {
  660. SCM_VALIDATE_MODULE (1, module);
  661. obarray = SCM_MODULE_OBARRAY (module);
  662. }
  663. SCM_VALIDATE_VARIABLE (SCM_ARG2, variable);
  664. if (!SCM_HASHTABLE_P (obarray))
  665. return SCM_BOOL_F;
  666. /* XXX - We do not use scm_hash_fold here to avoid searching the
  667. whole obarray. We should have a scm_hash_find procedure. */
  668. n = SCM_HASHTABLE_N_BUCKETS (obarray);
  669. for (i = 0; i < n; ++i)
  670. {
  671. SCM ls = SCM_HASHTABLE_BUCKET (obarray, i), handle;
  672. while (!scm_is_null (ls))
  673. {
  674. handle = SCM_CAR (ls);
  675. if (scm_is_eq (SCM_CDR (handle), variable))
  676. return SCM_CAR (handle);
  677. ls = SCM_CDR (ls);
  678. }
  679. }
  680. if (!scm_is_false (module))
  681. {
  682. /* Try the `uses' list. */
  683. SCM uses = SCM_MODULE_USES (module);
  684. while (scm_is_pair (uses))
  685. {
  686. SCM sym = scm_module_reverse_lookup (SCM_CAR (uses), variable);
  687. if (scm_is_true (sym))
  688. return sym;
  689. uses = SCM_CDR (uses);
  690. }
  691. }
  692. return SCM_BOOL_F;
  693. }
  694. #undef FUNC_NAME
  695. SCM_DEFINE (scm_get_pre_modules_obarray, "%get-pre-modules-obarray", 0, 0, 0,
  696. (),
  697. "Return the obarray that is used for all new bindings before "
  698. "the module system is booted. The first call to "
  699. "@code{set-current-module} will boot the module system.")
  700. #define FUNC_NAME s_scm_get_pre_modules_obarray
  701. {
  702. return scm_pre_modules_obarray;
  703. }
  704. #undef FUNC_NAME
  705. SCM_SYMBOL (scm_sym_system_module, "system-module");
  706. void
  707. scm_modules_prehistory ()
  708. {
  709. scm_pre_modules_obarray = scm_c_make_hash_table (1790);
  710. }
  711. void
  712. scm_init_modules ()
  713. {
  714. #include "libguile/modules.x"
  715. module_make_local_var_x_var = scm_c_define ("module-make-local-var!",
  716. SCM_UNDEFINED);
  717. the_module = scm_make_fluid ();
  718. }
  719. static void
  720. scm_post_boot_init_modules ()
  721. {
  722. SCM module_type = SCM_VARIABLE_REF (scm_c_lookup ("module-type"));
  723. scm_module_tag = (SCM_CELL_WORD_1 (module_type) + scm_tc3_struct);
  724. resolve_module_var = scm_c_lookup ("resolve-module");
  725. define_module_star_var = scm_c_lookup ("define-module*");
  726. process_use_modules_var = scm_c_lookup ("process-use-modules");
  727. module_export_x_var = scm_c_lookup ("module-export!");
  728. the_root_module_var = scm_c_lookup ("the-root-module");
  729. default_duplicate_binding_procedures_var =
  730. scm_c_lookup ("default-duplicate-binding-procedures");
  731. module_public_interface_var = scm_c_lookup ("module-public-interface");
  732. k_ensure = scm_from_locale_keyword ("ensure");
  733. scm_module_system_booted_p = 1;
  734. }
  735. /*
  736. Local Variables:
  737. c-file-style: "gnu"
  738. End:
  739. */