better scm_subr_p deprecation
[bpt/guile.git] / libguile / procprop.c
CommitLineData
e20d7001 1/* Copyright (C) 1995,1996,1998,2000,2001,2003,2004, 2006, 2008, 2009 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
0f2d19dd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
0f2d19dd 20\f
dbb605f5
LC
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
0f2d19dd 24
a0599745
MD
25#include "libguile/_scm.h"
26
27#include "libguile/alist.h"
28#include "libguile/eval.h"
29#include "libguile/procs.h"
30#include "libguile/gsubr.h"
5540e847 31#include "libguile/smob.h"
a0599745
MD
32#include "libguile/root.h"
33#include "libguile/vectors.h"
82c76fd3 34#include "libguile/hashtab.h"
2fb924f6 35#include "libguile/programs.h"
a0599745
MD
36
37#include "libguile/validate.h"
38#include "libguile/procprop.h"
0f2d19dd
JB
39\f
40
c083a529 41SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
67e60655
MD
42SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity");
43
314b8716
AW
44static SCM props;
45static scm_i_pthread_mutex_t props_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
56164a5a 46
314b8716
AW
47int
48scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest)
67e60655 49{
08906709 50 if (SCM_IMP (proc))
314b8716 51 return 0;
67e60655
MD
52 loop:
53 switch (SCM_TYP7 (proc))
54 {
2fb924f6 55 case scm_tc7_program:
314b8716 56 return scm_i_program_arity (proc, req, opt, rest);
5540e847 57 case scm_tc7_smob:
68b06924 58 if (SCM_SMOB_APPLICABLE_P (proc))
362306b9
DH
59 {
60 int type = SCM_SMOB_DESCRIPTOR (proc).gsubr_type;
314b8716
AW
61 *req = SCM_GSUBR_REQ (type);
62 *opt = SCM_GSUBR_OPT (type);
63 *rest = SCM_GSUBR_REST (type);
64 return 1;
362306b9
DH
65 }
66 else
314b8716 67 return 0;
e20d7001
LC
68 case scm_tc7_gsubr:
69 {
70 unsigned int type = SCM_GSUBR_TYPE (proc);
314b8716
AW
71 *req = SCM_GSUBR_REQ (type);
72 *opt = SCM_GSUBR_OPT (type);
73 *rest = SCM_GSUBR_REST (type);
74 return 1;
e20d7001 75 }
904a077d 76 case scm_tcs_struct:
314b8716
AW
77 if (!SCM_STRUCT_APPLICABLE_P (proc))
78 return 0;
b6cf4d02 79 proc = SCM_STRUCT_PROCEDURE (proc);
815ce8d5 80 goto loop;
08906709 81 default:
314b8716 82 return 0;
67e60655 83 }
67e60655
MD
84}
85
56164a5a
AW
86/* FIXME: instead of the weak hash, perhaps for some kinds of procedures, use
87 other means; for example subrs have their own property slot, which is unused
88 at present. */
0f2d19dd 89
3b3b36dd 90SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
1bbd0b84 91 (SCM proc),
b380b885 92 "Return @var{obj}'s property list.")
1bbd0b84 93#define FUNC_NAME s_scm_procedure_properties
0f2d19dd 94{
314b8716
AW
95 SCM ret;
96 int req, opt, rest;
56164a5a 97
34d19ef6 98 SCM_VALIDATE_PROC (1, proc);
314b8716
AW
99
100 scm_i_pthread_mutex_lock (&props_lock);
101 ret = scm_hashq_ref (props, proc, SCM_EOL);
102 scm_i_pthread_mutex_unlock (&props_lock);
103
104 scm_i_procedure_arity (proc, &req, &opt, &rest);
105
106 return scm_acons (scm_sym_arity,
107 scm_list_3 (scm_from_int (req),
108 scm_from_int (opt),
109 scm_from_bool (rest)),
110 ret);
0f2d19dd 111}
1bbd0b84 112#undef FUNC_NAME
0f2d19dd 113
3b3b36dd 114SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
56164a5a
AW
115 (SCM proc, SCM alist),
116 "Set @var{proc}'s property list to @var{alist}.")
1bbd0b84 117#define FUNC_NAME s_scm_set_procedure_properties_x
0f2d19dd 118{
56164a5a
AW
119 SCM_VALIDATE_PROC (1, proc);
120
314b8716
AW
121 scm_i_pthread_mutex_lock (&props_lock);
122 scm_hashq_set_x (props, proc, alist);
123 scm_i_pthread_mutex_unlock (&props_lock);
124
0f2d19dd
JB
125 return SCM_UNSPECIFIED;
126}
1bbd0b84 127#undef FUNC_NAME
0f2d19dd 128
3b3b36dd 129SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
56164a5a
AW
130 (SCM proc, SCM key),
131 "Return the property of @var{proc} with name @var{key}.")
1bbd0b84 132#define FUNC_NAME s_scm_procedure_property
0f2d19dd 133{
56164a5a
AW
134 SCM_VALIDATE_PROC (1, proc);
135
136 if (scm_is_eq (key, scm_sym_arity))
137 /* avoid a cons in this case */
314b8716
AW
138 {
139 int req, opt, rest;
140 scm_i_procedure_arity (proc, &req, &opt, &rest);
141 return scm_list_3 (scm_from_int (req),
142 scm_from_int (opt),
143 scm_from_bool (rest));
144 }
56164a5a 145 else
c783b082 146 {
314b8716
AW
147 SCM ret;
148
149 scm_i_pthread_mutex_lock (&props_lock);
150 ret = scm_hashq_ref (props, proc, SCM_EOL);
151 scm_i_pthread_mutex_unlock (&props_lock);
152
153 return scm_assq_ref (ret, key);
c783b082 154 }
0f2d19dd 155}
1bbd0b84 156#undef FUNC_NAME
0f2d19dd 157
3b3b36dd 158SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
56164a5a
AW
159 (SCM proc, SCM key, SCM val),
160 "In @var{proc}'s property list, set the property named @var{key} to\n"
161 "@var{val}.")
1bbd0b84 162#define FUNC_NAME s_scm_set_procedure_property_x
0f2d19dd 163{
56164a5a
AW
164 SCM_VALIDATE_PROC (1, proc);
165
166 if (scm_is_eq (key, scm_sym_arity))
1bbd0b84 167 SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL);
c783b082 168
314b8716
AW
169 scm_i_pthread_mutex_lock (&props_lock);
170 scm_hashq_set_x (props, proc,
171 scm_assq_set_x (scm_hashq_ref (props, proc,
172 SCM_EOL),
173 key, val));
174 scm_i_pthread_mutex_unlock (&props_lock);
c783b082 175
0f2d19dd
JB
176 return SCM_UNSPECIFIED;
177}
1bbd0b84 178#undef FUNC_NAME
0f2d19dd
JB
179
180\f
181
1cc91f1b 182
0f2d19dd
JB
183void
184scm_init_procprop ()
0f2d19dd 185{
314b8716 186 props = scm_make_weak_key_hash_table (SCM_UNDEFINED);
a0599745 187#include "libguile/procprop.x"
0f2d19dd
JB
188}
189
89e00824
ML
190
191/*
192 Local Variables:
193 c-file-style: "gnu"
194 End:
195*/