procprop.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* Copyright (C) 1995,1996,1998,2000,2001,2003,2004, 2006, 2008, 2009, 2010, 2011, 2012, 2013 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 "libguile/_scm.h"
  22. #include "libguile/alist.h"
  23. #include "libguile/eval.h"
  24. #include "libguile/procs.h"
  25. #include "libguile/gsubr.h"
  26. #include "libguile/smob.h"
  27. #include "libguile/root.h"
  28. #include "libguile/vectors.h"
  29. #include "libguile/weak-table.h"
  30. #include "libguile/programs.h"
  31. #include "libguile/vm-builtins.h"
  32. #include "libguile/validate.h"
  33. #include "libguile/procprop.h"
  34. SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
  35. SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
  36. static SCM overrides;
  37. static SCM arity_overrides;
  38. int
  39. scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest)
  40. {
  41. SCM o;
  42. o = scm_weak_table_refq (arity_overrides, proc, SCM_BOOL_F);
  43. if (scm_is_true (o))
  44. {
  45. *req = scm_to_int (scm_car (o));
  46. *opt = scm_to_int (scm_cadr (o));
  47. *rest = scm_is_true (scm_caddr (o));
  48. return 1;
  49. }
  50. while (!SCM_PROGRAM_P (proc))
  51. {
  52. if (SCM_STRUCTP (proc))
  53. {
  54. if (!SCM_STRUCT_APPLICABLE_P (proc))
  55. return 0;
  56. proc = SCM_STRUCT_PROCEDURE (proc);
  57. }
  58. else if (SCM_HAS_TYP7 (proc, scm_tc7_smob))
  59. {
  60. if (!SCM_SMOB_APPLICABLE_P (proc))
  61. return 0;
  62. if (!scm_i_program_arity (SCM_SMOB_DESCRIPTOR (proc).apply_trampoline,
  63. req, opt, rest))
  64. return 0;
  65. /* The trampoline gets the smob too, which users don't
  66. see. */
  67. *req -= 1;
  68. return 1;
  69. }
  70. else
  71. return 0;
  72. }
  73. return scm_i_program_arity (proc, req, opt, rest);
  74. }
  75. SCM_DEFINE (scm_set_procedure_minimum_arity_x, "set-procedure-minimum-arity!",
  76. 4, 0, 0, (SCM proc, SCM req, SCM opt, SCM rest),
  77. "")
  78. #define FUNC_NAME s_scm_set_procedure_minimum_arity_x
  79. {
  80. int t SCM_UNUSED;
  81. SCM_VALIDATE_PROC (1, proc);
  82. SCM_VALIDATE_INT_COPY (2, req, t);
  83. SCM_VALIDATE_INT_COPY (3, opt, t);
  84. SCM_VALIDATE_BOOL (4, rest);
  85. scm_weak_table_putq_x (arity_overrides, proc, scm_list_3 (req, opt, rest));
  86. return SCM_UNDEFINED;
  87. }
  88. #undef FUNC_NAME
  89. SCM_DEFINE (scm_procedure_minimum_arity, "procedure-minimum-arity", 1, 0, 0,
  90. (SCM proc),
  91. "Return the \"minimum arity\" of a procedure.\n\n"
  92. "If the procedure has only one arity, that arity is returned\n"
  93. "as a list of three values: the number of required arguments,\n"
  94. "the number of optional arguments, and a boolean indicating\n"
  95. "whether or not the procedure takes rest arguments.\n\n"
  96. "For a case-lambda procedure, the arity returned is the one\n"
  97. "with the lowest minimum number of arguments, and the highest\n"
  98. "maximum number of arguments.\n\n"
  99. "If it was not possible to determine the arity of the procedure,\n"
  100. "@code{#f} is returned.")
  101. #define FUNC_NAME s_scm_procedure_minimum_arity
  102. {
  103. int req, opt, rest;
  104. if (scm_i_procedure_arity (proc, &req, &opt, &rest))
  105. return scm_list_3 (scm_from_int (req),
  106. scm_from_int (opt),
  107. scm_from_bool (rest));
  108. else
  109. return SCM_BOOL_F;
  110. }
  111. #undef FUNC_NAME
  112. SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
  113. (SCM proc),
  114. "Return @var{proc}'s property list.")
  115. #define FUNC_NAME s_scm_procedure_properties
  116. {
  117. SCM ret, user_props;
  118. SCM_VALIDATE_PROC (1, proc);
  119. user_props = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
  120. if (scm_is_pair (user_props) && scm_is_true (scm_car (user_props)))
  121. return scm_cdr (user_props);
  122. if (SCM_PROGRAM_P (proc))
  123. ret = scm_i_program_properties (proc);
  124. else
  125. ret = SCM_EOL;
  126. if (scm_is_pair (user_props))
  127. for (user_props = scm_cdr (user_props);
  128. scm_is_pair (user_props);
  129. user_props = scm_cdr (user_props))
  130. ret = scm_assq_set_x (ret, scm_caar (user_props), scm_cdar (user_props));
  131. return ret;
  132. }
  133. #undef FUNC_NAME
  134. SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
  135. (SCM proc, SCM alist),
  136. "Set @var{proc}'s property list to @var{alist}.")
  137. #define FUNC_NAME s_scm_set_procedure_properties_x
  138. {
  139. SCM_VALIDATE_PROC (1, proc);
  140. scm_weak_table_putq_x (overrides, proc, scm_cons (SCM_BOOL_T, alist));
  141. return SCM_UNSPECIFIED;
  142. }
  143. #undef FUNC_NAME
  144. SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
  145. (SCM proc, SCM key),
  146. "Return the property of @var{proc} with name @var{key}.")
  147. #define FUNC_NAME s_scm_procedure_property
  148. {
  149. SCM user_props;
  150. SCM_VALIDATE_PROC (1, proc);
  151. if (scm_is_eq (key, scm_sym_name))
  152. return scm_procedure_name (proc);
  153. if (scm_is_eq (key, scm_sym_documentation))
  154. return scm_procedure_documentation (proc);
  155. user_props = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
  156. if (scm_is_true (user_props))
  157. {
  158. SCM pair = scm_assq (key, scm_cdr (user_props));
  159. if (scm_is_pair (pair))
  160. return scm_cdr (pair);
  161. if (scm_is_true (scm_car (user_props)))
  162. return SCM_BOOL_F;
  163. }
  164. return scm_assq_ref (scm_procedure_properties (proc), key);
  165. }
  166. #undef FUNC_NAME
  167. SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
  168. (SCM proc, SCM key, SCM val),
  169. "In @var{proc}'s property list, set the property named @var{key} to\n"
  170. "@var{val}.")
  171. #define FUNC_NAME s_scm_set_procedure_property_x
  172. {
  173. SCM user_props, override_p;
  174. SCM_VALIDATE_PROC (1, proc);
  175. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  176. user_props = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
  177. if (scm_is_false (user_props))
  178. {
  179. override_p = SCM_BOOL_F;
  180. user_props = SCM_EOL;
  181. }
  182. else
  183. {
  184. override_p = scm_car (user_props);
  185. user_props = scm_cdr (user_props);
  186. }
  187. scm_weak_table_putq_x (overrides, proc,
  188. scm_cons (override_p,
  189. scm_assq_set_x (user_props, key, val)));
  190. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  191. return SCM_UNSPECIFIED;
  192. }
  193. #undef FUNC_NAME
  194. SCM_SYMBOL (scm_sym_source, "source");
  195. SCM_DEFINE (scm_procedure_name, "procedure-name", 1, 0, 0,
  196. (SCM proc),
  197. "Return the name of the procedure @var{proc}")
  198. #define FUNC_NAME s_scm_procedure_name
  199. {
  200. SCM user_props;
  201. SCM_VALIDATE_PROC (1, proc);
  202. user_props = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
  203. if (scm_is_true (user_props))
  204. {
  205. SCM pair = scm_assq (scm_sym_name, scm_cdr (user_props));
  206. if (scm_is_pair (pair))
  207. return scm_cdr (pair);
  208. if (scm_is_true (scm_car (user_props)))
  209. return SCM_BOOL_F;
  210. }
  211. if (SCM_PROGRAM_P (proc))
  212. return scm_i_program_name (proc);
  213. else if (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc))
  214. return scm_procedure_name (SCM_STRUCT_PROCEDURE (proc));
  215. else
  216. return SCM_BOOL_F;
  217. }
  218. #undef FUNC_NAME
  219. SCM_GLOBAL_SYMBOL (scm_sym_documentation, "documentation");
  220. SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
  221. (SCM proc),
  222. "Return the documentation string associated with @code{proc}. By\n"
  223. "convention, if a procedure contains more than one expression and the\n"
  224. "first expression is a string constant, that string is assumed to contain\n"
  225. "documentation for that procedure.")
  226. #define FUNC_NAME s_scm_procedure_documentation
  227. {
  228. SCM user_props;
  229. SCM_VALIDATE_PROC (1, proc);
  230. while (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc))
  231. proc = SCM_STRUCT_PROCEDURE (proc);
  232. user_props = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
  233. if (scm_is_true (user_props))
  234. {
  235. SCM pair = scm_assq (scm_sym_documentation, scm_cdr (user_props));
  236. if (scm_is_pair (pair))
  237. return scm_cdr (pair);
  238. if (scm_is_true (scm_car (user_props)))
  239. return SCM_BOOL_F;
  240. }
  241. if (SCM_PROGRAM_P (proc))
  242. return scm_i_program_documentation (proc);
  243. else
  244. return SCM_BOOL_F;
  245. }
  246. #undef FUNC_NAME
  247. SCM_DEFINE (scm_procedure_source, "procedure-source", 1, 0, 0,
  248. (SCM proc),
  249. "Return the source of the procedure @var{proc}.")
  250. #define FUNC_NAME s_scm_procedure_source
  251. {
  252. SCM src;
  253. SCM_VALIDATE_PROC (1, proc);
  254. do
  255. {
  256. src = scm_procedure_property (proc, scm_sym_source);
  257. if (scm_is_true (src))
  258. return src;
  259. if (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc)
  260. && SCM_HEAP_OBJECT_P ((proc = SCM_STRUCT_PROCEDURE (proc))))
  261. continue;
  262. }
  263. while (0);
  264. return SCM_BOOL_F;
  265. }
  266. #undef FUNC_NAME
  267. void
  268. scm_init_procprop ()
  269. {
  270. overrides = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  271. arity_overrides = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  272. #include "libguile/procprop.x"
  273. scm_init_vm_builtin_properties ();
  274. }
  275. /*
  276. Local Variables:
  277. c-file-style: "gnu"
  278. End:
  279. */