procprop.c 9.5 KB

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