web http: fix Ma -> Mar
[bpt/guile.git] / libguile / procprop.c
CommitLineData
fe2400c9
LC
1/* Copyright (C) 1995, 1996, 1998, 2000, 2001, 2003, 2004, 2006,
2 * 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
3 *
73be1d9e 4 * This library is free software; you can redistribute it and/or
53befeb7
NJ
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
0f2d19dd 8 *
53befeb7
NJ
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
0f2d19dd 13 *
73be1d9e
MV
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
53befeb7
NJ
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
73be1d9e 18 */
1bbd0b84 19
1bbd0b84 20
0f2d19dd 21\f
dbb605f5
LC
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
0f2d19dd 25
3fc7e2c1
AW
26#define SCM_BUILDING_DEPRECATED_CODE
27
a0599745
MD
28#include "libguile/_scm.h"
29
30#include "libguile/alist.h"
3fc7e2c1
AW
31#include "libguile/deprecation.h"
32#include "libguile/deprecated.h"
a0599745
MD
33#include "libguile/eval.h"
34#include "libguile/procs.h"
35#include "libguile/gsubr.h"
5540e847 36#include "libguile/smob.h"
a0599745
MD
37#include "libguile/root.h"
38#include "libguile/vectors.h"
82c76fd3 39#include "libguile/hashtab.h"
2fb924f6 40#include "libguile/programs.h"
a0599745
MD
41
42#include "libguile/validate.h"
43#include "libguile/procprop.h"
0f2d19dd
JB
44\f
45
c083a529 46SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
3fc7e2c1 47#if (SCM_ENABLE_DEPRECATED == 1)
67e60655 48SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity");
3fc7e2c1 49#endif
fd12a19a 50SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
67e60655 51
e1bdf9e2
AW
52static SCM overrides;
53static scm_i_pthread_mutex_t overrides_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
56164a5a 54
f3cf9421
AW
55static SCM arity_overrides;
56
314b8716
AW
57int
58scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest)
67e60655 59{
f3cf9421
AW
60 SCM o;
61
62 scm_i_pthread_mutex_lock (&overrides_lock);
63 o = scm_hashq_ref (arity_overrides, proc, SCM_BOOL_F);
64 scm_i_pthread_mutex_unlock (&overrides_lock);
65
66 if (scm_is_true (o))
67 {
68 *req = scm_to_int (scm_car (o));
69 *opt = scm_to_int (scm_cadr (o));
70 *rest = scm_is_true (scm_caddr (o));
71 return 1;
72 }
73
75c3ed28 74 while (!SCM_PROGRAM_P (proc))
67e60655 75 {
75c3ed28 76 if (SCM_IMP (proc))
314b8716 77 return 0;
75c3ed28
AW
78 switch (SCM_TYP7 (proc))
79 {
80 case scm_tc7_smob:
81 if (!SCM_SMOB_APPLICABLE_P (proc))
82 return 0;
83 proc = scm_i_smob_apply_trampoline (proc);
84 break;
85 case scm_tcs_struct:
86 if (!SCM_STRUCT_APPLICABLE_P (proc))
87 return 0;
88 proc = SCM_STRUCT_PROCEDURE (proc);
89 break;
90 default:
91 return 0;
92 }
67e60655 93 }
f3cf9421 94
75c3ed28 95 return scm_i_program_arity (proc, req, opt, rest);
67e60655
MD
96}
97
f3cf9421
AW
98SCM_DEFINE (scm_set_procedure_minimum_arity_x, "set-procedure-minimum-arity!",
99 4, 0, 0, (SCM proc, SCM req, SCM opt, SCM rest),
100 "")
101#define FUNC_NAME s_scm_set_procedure_minimum_arity_x
102{
103 int t SCM_UNUSED;
104
105 SCM_VALIDATE_PROC (1, proc);
106 SCM_VALIDATE_INT_COPY (2, req, t);
107 SCM_VALIDATE_INT_COPY (3, opt, t);
108 SCM_VALIDATE_BOOL (4, rest);
109
110 scm_i_pthread_mutex_lock (&overrides_lock);
111 scm_hashq_set_x (arity_overrides, proc, scm_list_3 (req, opt, rest));
112 scm_i_pthread_mutex_unlock (&overrides_lock);
113 return SCM_UNDEFINED;
114}
115#undef FUNC_NAME
116
cb2ce548
AW
117SCM_DEFINE (scm_procedure_minimum_arity, "procedure-minimum-arity", 1, 0, 0,
118 (SCM proc),
119 "Return the \"minimum arity\" of a procedure.\n\n"
120 "If the procedure has only one arity, that arity is returned\n"
121 "as a list of three values: the number of required arguments,\n"
122 "the number of optional arguments, and a boolean indicating\n"
123 "whether or not the procedure takes rest arguments.\n\n"
124 "For a case-lambda procedure, the arity returned is the one\n"
125 "with the lowest minimum number of arguments, and the highest\n"
126 "maximum number of arguments.\n\n"
127 "If it was not possible to determine the arity of the procedure,\n"
128 "@code{#f} is returned.")
129#define FUNC_NAME s_scm_procedure_minimum_arity
130{
131 int req, opt, rest;
132
133 if (scm_i_procedure_arity (proc, &req, &opt, &rest))
134 return scm_list_3 (scm_from_int (req),
135 scm_from_int (opt),
136 scm_from_bool (rest));
137 else
138 return SCM_BOOL_F;
139}
140#undef FUNC_NAME
141
3b3b36dd 142SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
1bbd0b84 143 (SCM proc),
b7e64f8b 144 "Return @var{proc}'s property list.")
1bbd0b84 145#define FUNC_NAME s_scm_procedure_properties
0f2d19dd 146{
314b8716 147 SCM ret;
56164a5a 148
34d19ef6 149 SCM_VALIDATE_PROC (1, proc);
314b8716 150
e1bdf9e2
AW
151 scm_i_pthread_mutex_lock (&overrides_lock);
152 ret = scm_hashq_ref (overrides, proc, SCM_BOOL_F);
153 scm_i_pthread_mutex_unlock (&overrides_lock);
314b8716 154
e1bdf9e2
AW
155 if (scm_is_false (ret))
156 {
157 if (SCM_PROGRAM_P (proc))
07e424b7 158 ret = scm_i_program_properties (proc);
e1bdf9e2
AW
159 else
160 ret = SCM_EOL;
161 }
162
3fc7e2c1
AW
163#if (SCM_ENABLE_DEPRECATED == 1)
164 ret = scm_acons (scm_sym_arity, scm_procedure_minimum_arity (proc), ret);
165#endif
314b8716 166
3fc7e2c1 167 return ret;
0f2d19dd 168}
1bbd0b84 169#undef FUNC_NAME
0f2d19dd 170
3b3b36dd 171SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
56164a5a
AW
172 (SCM proc, SCM alist),
173 "Set @var{proc}'s property list to @var{alist}.")
1bbd0b84 174#define FUNC_NAME s_scm_set_procedure_properties_x
0f2d19dd 175{
56164a5a
AW
176 SCM_VALIDATE_PROC (1, proc);
177
3fc7e2c1 178#if (SCM_ENABLE_DEPRECATED == 1)
fe2400c9 179 if (scm_is_true (scm_assq (scm_sym_arity, alist)))
e1bdf9e2 180 SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL);
3fc7e2c1 181#endif
e1bdf9e2
AW
182
183 scm_i_pthread_mutex_lock (&overrides_lock);
184 scm_hashq_set_x (overrides, proc, alist);
185 scm_i_pthread_mutex_unlock (&overrides_lock);
314b8716 186
0f2d19dd
JB
187 return SCM_UNSPECIFIED;
188}
1bbd0b84 189#undef FUNC_NAME
0f2d19dd 190
3b3b36dd 191SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
56164a5a
AW
192 (SCM proc, SCM key),
193 "Return the property of @var{proc} with name @var{key}.")
1bbd0b84 194#define FUNC_NAME s_scm_procedure_property
0f2d19dd 195{
56164a5a
AW
196 SCM_VALIDATE_PROC (1, proc);
197
3fc7e2c1 198#if (SCM_ENABLE_DEPRECATED == 1)
56164a5a 199 if (scm_is_eq (key, scm_sym_arity))
3fc7e2c1
AW
200 scm_c_issue_deprecation_warning
201 ("Accessing a procedure's arity via `procedure-property' is deprecated.\n"
202 "Use `procedure-minimum-arity instead.");
203#endif
204
205 return scm_assq_ref (scm_procedure_properties (proc), key);
0f2d19dd 206}
1bbd0b84 207#undef FUNC_NAME
0f2d19dd 208
3b3b36dd 209SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
56164a5a
AW
210 (SCM proc, SCM key, SCM val),
211 "In @var{proc}'s property list, set the property named @var{key} to\n"
212 "@var{val}.")
1bbd0b84 213#define FUNC_NAME s_scm_set_procedure_property_x
0f2d19dd 214{
90fa152c 215 SCM props;
56164a5a 216
e1bdf9e2 217 SCM_VALIDATE_PROC (1, proc);
3fc7e2c1
AW
218
219#if (SCM_ENABLE_DEPRECATED == 1)
56164a5a 220 if (scm_is_eq (key, scm_sym_arity))
3fc7e2c1
AW
221 SCM_MISC_ERROR ("arity is a deprecated read-only property", SCM_EOL);
222#endif
c783b082 223
e1bdf9e2 224 scm_i_pthread_mutex_lock (&overrides_lock);
f2ed4473
AW
225 props = scm_hashq_ref (overrides, proc, SCM_BOOL_F);
226 if (scm_is_false (props))
227 {
228 if (SCM_PROGRAM_P (proc))
229 props = scm_i_program_properties (proc);
230 else
231 props = SCM_EOL;
232 }
90fa152c 233 scm_hashq_set_x (overrides, proc, scm_assq_set_x (props, key, val));
e1bdf9e2 234 scm_i_pthread_mutex_unlock (&overrides_lock);
c783b082 235
0f2d19dd
JB
236 return SCM_UNSPECIFIED;
237}
1bbd0b84 238#undef FUNC_NAME
0f2d19dd
JB
239
240\f
241
1cc91f1b 242
0f2d19dd
JB
243void
244scm_init_procprop ()
0f2d19dd 245{
e1bdf9e2 246 overrides = scm_make_weak_key_hash_table (SCM_UNDEFINED);
f3cf9421 247 arity_overrides = scm_make_weak_key_hash_table (SCM_UNDEFINED);
a0599745 248#include "libguile/procprop.x"
0f2d19dd
JB
249}
250
89e00824
ML
251
252/*
253 Local Variables:
254 c-file-style: "gnu"
255 End:
256*/