procprop.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Copyright (C) 1995,1996,1998,2000,2001,2003,2004, 2006, 2008 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
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but 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 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/_scm.h"
  21. #include "libguile/alist.h"
  22. #include "libguile/eval.h"
  23. #include "libguile/procs.h"
  24. #include "libguile/gsubr.h"
  25. #include "libguile/objects.h"
  26. #include "libguile/smob.h"
  27. #include "libguile/root.h"
  28. #include "libguile/vectors.h"
  29. #include "libguile/hashtab.h"
  30. #include "libguile/validate.h"
  31. #include "libguile/procprop.h"
  32. SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
  33. SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity");
  34. SCM
  35. scm_i_procedure_arity (SCM proc)
  36. {
  37. int a = 0, o = 0, r = 0;
  38. if (SCM_IMP (proc))
  39. return SCM_BOOL_F;
  40. loop:
  41. switch (SCM_TYP7 (proc))
  42. {
  43. case scm_tc7_subr_1o:
  44. o = 1;
  45. case scm_tc7_subr_0:
  46. break;
  47. case scm_tc7_subr_2o:
  48. o = 1;
  49. case scm_tc7_subr_1:
  50. case scm_tc7_dsubr:
  51. case scm_tc7_cxr:
  52. a += 1;
  53. break;
  54. case scm_tc7_subr_2:
  55. a += 2;
  56. break;
  57. case scm_tc7_subr_3:
  58. a += 3;
  59. break;
  60. case scm_tc7_asubr:
  61. case scm_tc7_rpsubr:
  62. case scm_tc7_lsubr:
  63. r = 1;
  64. break;
  65. case scm_tc7_lsubr_2:
  66. a += 2;
  67. r = 1;
  68. break;
  69. case scm_tc7_smob:
  70. if (SCM_SMOB_APPLICABLE_P (proc))
  71. {
  72. int type = SCM_SMOB_DESCRIPTOR (proc).gsubr_type;
  73. a += SCM_GSUBR_REQ (type);
  74. o = SCM_GSUBR_OPT (type);
  75. r = SCM_GSUBR_REST (type);
  76. break;
  77. }
  78. else
  79. {
  80. return SCM_BOOL_F;
  81. }
  82. case scm_tc7_cclo:
  83. if (scm_is_eq (SCM_CCLO_SUBR (proc), scm_f_gsubr_apply))
  84. {
  85. int type = scm_to_int (SCM_GSUBR_TYPE (proc));
  86. a += SCM_GSUBR_REQ (type);
  87. o = SCM_GSUBR_OPT (type);
  88. r = SCM_GSUBR_REST (type);
  89. break;
  90. }
  91. else
  92. {
  93. proc = SCM_CCLO_SUBR (proc);
  94. a -= 1;
  95. goto loop;
  96. }
  97. case scm_tc7_pws:
  98. proc = SCM_PROCEDURE (proc);
  99. goto loop;
  100. case scm_tcs_closures:
  101. proc = SCM_CLOSURE_FORMALS (proc);
  102. if (scm_is_null (proc))
  103. break;
  104. while (scm_is_pair (proc))
  105. {
  106. ++a;
  107. proc = SCM_CDR (proc);
  108. }
  109. if (!scm_is_null (proc))
  110. r = 1;
  111. break;
  112. case scm_tcs_struct:
  113. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  114. {
  115. r = 1;
  116. break;
  117. }
  118. else if (!SCM_I_OPERATORP (proc))
  119. return SCM_BOOL_F;
  120. proc = (SCM_I_ENTITYP (proc)
  121. ? SCM_ENTITY_PROCEDURE (proc)
  122. : SCM_OPERATOR_PROCEDURE (proc));
  123. a -= 1;
  124. goto loop;
  125. default:
  126. return SCM_BOOL_F;
  127. }
  128. return scm_list_3 (scm_from_int (a), scm_from_int (o), scm_from_bool(r));
  129. }
  130. /* XXX - instead of using a stand-in value for everything except
  131. closures, we should find other ways to store the procedure
  132. properties for those other kinds of procedures. For example, subrs
  133. have their own property slot, which is unused at present.
  134. */
  135. static SCM
  136. scm_stand_in_scm_proc(SCM proc)
  137. {
  138. SCM handle, answer;
  139. handle = scm_hashq_get_handle (scm_stand_in_procs, proc);
  140. if (scm_is_false (handle))
  141. {
  142. answer = scm_closure (scm_list_2 (SCM_EOL, SCM_BOOL_F), SCM_EOL);
  143. scm_hashq_set_x (scm_stand_in_procs, proc, answer);
  144. }
  145. else
  146. answer = SCM_CDR (handle);
  147. return answer;
  148. }
  149. SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
  150. (SCM proc),
  151. "Return @var{obj}'s property list.")
  152. #define FUNC_NAME s_scm_procedure_properties
  153. {
  154. SCM_VALIDATE_PROC (1, proc);
  155. return scm_acons (scm_sym_arity, scm_i_procedure_arity (proc),
  156. SCM_PROCPROPS (SCM_CLOSUREP (proc)
  157. ? proc
  158. : scm_stand_in_scm_proc (proc)));
  159. }
  160. #undef FUNC_NAME
  161. SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
  162. (SCM proc, SCM new_val),
  163. "Set @var{obj}'s property list to @var{alist}.")
  164. #define FUNC_NAME s_scm_set_procedure_properties_x
  165. {
  166. if (!SCM_CLOSUREP (proc))
  167. proc = scm_stand_in_scm_proc(proc);
  168. SCM_VALIDATE_CLOSURE (1, proc);
  169. SCM_SETPROCPROPS (proc, new_val);
  170. return SCM_UNSPECIFIED;
  171. }
  172. #undef FUNC_NAME
  173. SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
  174. (SCM p, SCM k),
  175. "Return the property of @var{obj} with name @var{key}.")
  176. #define FUNC_NAME s_scm_procedure_property
  177. {
  178. SCM assoc;
  179. if (scm_is_eq (k, scm_sym_arity))
  180. {
  181. SCM arity;
  182. SCM_ASSERT (scm_is_true (arity = scm_i_procedure_arity (p)),
  183. p, SCM_ARG1, FUNC_NAME);
  184. return arity;
  185. }
  186. SCM_VALIDATE_PROC (1, p);
  187. assoc = scm_sloppy_assq (k,
  188. SCM_PROCPROPS (SCM_CLOSUREP (p)
  189. ? p
  190. : scm_stand_in_scm_proc (p)));
  191. return (SCM_NIMP (assoc) ? SCM_CDR (assoc) : SCM_BOOL_F);
  192. }
  193. #undef FUNC_NAME
  194. SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
  195. (SCM p, SCM k, SCM v),
  196. "In @var{obj}'s property list, set the property named @var{key} to\n"
  197. "@var{value}.")
  198. #define FUNC_NAME s_scm_set_procedure_property_x
  199. {
  200. SCM assoc;
  201. if (!SCM_CLOSUREP (p))
  202. p = scm_stand_in_scm_proc(p);
  203. SCM_VALIDATE_CLOSURE (1, p);
  204. if (scm_is_eq (k, scm_sym_arity))
  205. SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL);
  206. assoc = scm_sloppy_assq (k, SCM_PROCPROPS (p));
  207. if (SCM_NIMP (assoc))
  208. SCM_SETCDR (assoc, v);
  209. else
  210. SCM_SETPROCPROPS (p, scm_acons (k, v, SCM_PROCPROPS (p)));
  211. return SCM_UNSPECIFIED;
  212. }
  213. #undef FUNC_NAME
  214. void
  215. scm_init_procprop ()
  216. {
  217. #include "libguile/procprop.x"
  218. }
  219. /*
  220. Local Variables:
  221. c-file-style: "gnu"
  222. End:
  223. */