procprop.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/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) && !SCM_RTL_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;
  117. SCM_VALIDATE_PROC (1, proc);
  118. ret = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
  119. if (scm_is_false (ret))
  120. {
  121. if (SCM_PROGRAM_P (proc))
  122. ret = scm_i_program_properties (proc);
  123. else if (SCM_RTL_PROGRAM_P (proc))
  124. ret = scm_i_rtl_program_properties (proc);
  125. else
  126. ret = SCM_EOL;
  127. }
  128. return ret;
  129. }
  130. #undef FUNC_NAME
  131. SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
  132. (SCM proc, SCM alist),
  133. "Set @var{proc}'s property list to @var{alist}.")
  134. #define FUNC_NAME s_scm_set_procedure_properties_x
  135. {
  136. SCM_VALIDATE_PROC (1, proc);
  137. scm_weak_table_putq_x (overrides, proc, alist);
  138. return SCM_UNSPECIFIED;
  139. }
  140. #undef FUNC_NAME
  141. SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
  142. (SCM proc, SCM key),
  143. "Return the property of @var{proc} with name @var{key}.")
  144. #define FUNC_NAME s_scm_procedure_property
  145. {
  146. SCM_VALIDATE_PROC (1, proc);
  147. return scm_assq_ref (scm_procedure_properties (proc), key);
  148. }
  149. #undef FUNC_NAME
  150. SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
  151. (SCM proc, SCM key, SCM val),
  152. "In @var{proc}'s property list, set the property named @var{key} to\n"
  153. "@var{val}.")
  154. #define FUNC_NAME s_scm_set_procedure_property_x
  155. {
  156. SCM props;
  157. SCM_VALIDATE_PROC (1, proc);
  158. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  159. props = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
  160. if (scm_is_false (props))
  161. {
  162. if (SCM_PROGRAM_P (proc))
  163. props = scm_i_program_properties (proc);
  164. else
  165. props = SCM_EOL;
  166. }
  167. scm_weak_table_putq_x (overrides, proc, scm_assq_set_x (props, key, val));
  168. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  169. return SCM_UNSPECIFIED;
  170. }
  171. #undef FUNC_NAME
  172. SCM_SYMBOL (scm_sym_source, "source");
  173. SCM_DEFINE (scm_procedure_name, "procedure-name", 1, 0, 0,
  174. (SCM proc),
  175. "Return the name of the procedure @var{proc}")
  176. #define FUNC_NAME s_scm_procedure_name
  177. {
  178. SCM props, ret;
  179. SCM_VALIDATE_PROC (1, proc);
  180. while (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc))
  181. proc = SCM_STRUCT_PROCEDURE (proc);
  182. props = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
  183. if (scm_is_pair (props))
  184. ret = scm_assq_ref (props, scm_sym_name);
  185. else if (SCM_RTL_PROGRAM_P (proc))
  186. ret = scm_i_rtl_program_name (proc);
  187. else if (SCM_PROGRAM_P (proc))
  188. ret = scm_assq_ref (scm_i_program_properties (proc), scm_sym_name);
  189. else
  190. ret = SCM_BOOL_F;
  191. return ret;
  192. }
  193. #undef FUNC_NAME
  194. SCM_GLOBAL_SYMBOL (scm_sym_documentation, "documentation");
  195. SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
  196. (SCM proc),
  197. "Return the documentation string associated with @code{proc}. By\n"
  198. "convention, if a procedure contains more than one expression and the\n"
  199. "first expression is a string constant, that string is assumed to contain\n"
  200. "documentation for that procedure.")
  201. #define FUNC_NAME s_scm_procedure_documentation
  202. {
  203. SCM props, ret;
  204. SCM_VALIDATE_PROC (1, proc);
  205. while (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc))
  206. proc = SCM_STRUCT_PROCEDURE (proc);
  207. props = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
  208. if (scm_is_pair (props))
  209. ret = scm_assq_ref (props, scm_sym_documentation);
  210. else if (SCM_RTL_PROGRAM_P (proc))
  211. ret = scm_i_rtl_program_documentation (proc);
  212. else if (SCM_PROGRAM_P (proc))
  213. ret = scm_assq_ref (scm_i_program_properties (proc), scm_sym_documentation);
  214. else
  215. ret = SCM_BOOL_F;
  216. return ret;
  217. }
  218. #undef FUNC_NAME
  219. SCM_DEFINE (scm_procedure_source, "procedure-source", 1, 0, 0,
  220. (SCM proc),
  221. "Return the source of the procedure @var{proc}.")
  222. #define FUNC_NAME s_scm_procedure_source
  223. {
  224. SCM src;
  225. SCM_VALIDATE_PROC (1, proc);
  226. do
  227. {
  228. src = scm_procedure_property (proc, scm_sym_source);
  229. if (scm_is_true (src))
  230. return src;
  231. if (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc)
  232. && SCM_HEAP_OBJECT_P ((proc = SCM_STRUCT_PROCEDURE (proc))))
  233. continue;
  234. }
  235. while (0);
  236. return SCM_BOOL_F;
  237. }
  238. #undef FUNC_NAME
  239. void
  240. scm_init_procprop ()
  241. {
  242. overrides = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  243. arity_overrides = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  244. #include "libguile/procprop.x"
  245. }
  246. /*
  247. Local Variables:
  248. c-file-style: "gnu"
  249. End:
  250. */