Fix "occurrances" typos in getopt-long code and test
[bpt/guile.git] / libguile / procprop.c
CommitLineData
f2ed4473 1/* Copyright (C) 1995,1996,1998,2000,2001,2003,2004, 2006, 2008, 2009, 2010, 2011 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
3fc7e2c1
AW
25#define SCM_BUILDING_DEPRECATED_CODE
26
a0599745
MD
27#include "libguile/_scm.h"
28
29#include "libguile/alist.h"
3fc7e2c1
AW
30#include "libguile/deprecation.h"
31#include "libguile/deprecated.h"
a0599745
MD
32#include "libguile/eval.h"
33#include "libguile/procs.h"
34#include "libguile/gsubr.h"
5540e847 35#include "libguile/smob.h"
a0599745
MD
36#include "libguile/root.h"
37#include "libguile/vectors.h"
82c76fd3 38#include "libguile/hashtab.h"
2fb924f6 39#include "libguile/programs.h"
a0599745
MD
40
41#include "libguile/validate.h"
42#include "libguile/procprop.h"
0f2d19dd
JB
43\f
44
c083a529 45SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
3fc7e2c1 46#if (SCM_ENABLE_DEPRECATED == 1)
67e60655 47SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity");
3fc7e2c1 48#endif
fd12a19a 49SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
67e60655 50
e1bdf9e2
AW
51static SCM overrides;
52static scm_i_pthread_mutex_t overrides_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
56164a5a 53
314b8716
AW
54int
55scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest)
67e60655 56{
75c3ed28 57 while (!SCM_PROGRAM_P (proc))
67e60655 58 {
75c3ed28 59 if (SCM_IMP (proc))
314b8716 60 return 0;
75c3ed28
AW
61 switch (SCM_TYP7 (proc))
62 {
63 case scm_tc7_smob:
64 if (!SCM_SMOB_APPLICABLE_P (proc))
65 return 0;
66 proc = scm_i_smob_apply_trampoline (proc);
67 break;
68 case scm_tcs_struct:
69 if (!SCM_STRUCT_APPLICABLE_P (proc))
70 return 0;
71 proc = SCM_STRUCT_PROCEDURE (proc);
72 break;
73 default:
74 return 0;
75 }
67e60655 76 }
75c3ed28 77 return scm_i_program_arity (proc, req, opt, rest);
67e60655
MD
78}
79
cb2ce548
AW
80SCM_DEFINE (scm_procedure_minimum_arity, "procedure-minimum-arity", 1, 0, 0,
81 (SCM proc),
82 "Return the \"minimum arity\" of a procedure.\n\n"
83 "If the procedure has only one arity, that arity is returned\n"
84 "as a list of three values: the number of required arguments,\n"
85 "the number of optional arguments, and a boolean indicating\n"
86 "whether or not the procedure takes rest arguments.\n\n"
87 "For a case-lambda procedure, the arity returned is the one\n"
88 "with the lowest minimum number of arguments, and the highest\n"
89 "maximum number of arguments.\n\n"
90 "If it was not possible to determine the arity of the procedure,\n"
91 "@code{#f} is returned.")
92#define FUNC_NAME s_scm_procedure_minimum_arity
93{
94 int req, opt, rest;
95
96 if (scm_i_procedure_arity (proc, &req, &opt, &rest))
97 return scm_list_3 (scm_from_int (req),
98 scm_from_int (opt),
99 scm_from_bool (rest));
100 else
101 return SCM_BOOL_F;
102}
103#undef FUNC_NAME
104
3b3b36dd 105SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
1bbd0b84 106 (SCM proc),
b380b885 107 "Return @var{obj}'s property list.")
1bbd0b84 108#define FUNC_NAME s_scm_procedure_properties
0f2d19dd 109{
314b8716 110 SCM ret;
56164a5a 111
34d19ef6 112 SCM_VALIDATE_PROC (1, proc);
314b8716 113
e1bdf9e2
AW
114 scm_i_pthread_mutex_lock (&overrides_lock);
115 ret = scm_hashq_ref (overrides, proc, SCM_BOOL_F);
116 scm_i_pthread_mutex_unlock (&overrides_lock);
314b8716 117
e1bdf9e2
AW
118 if (scm_is_false (ret))
119 {
120 if (SCM_PROGRAM_P (proc))
07e424b7 121 ret = scm_i_program_properties (proc);
e1bdf9e2
AW
122 else
123 ret = SCM_EOL;
124 }
125
3fc7e2c1
AW
126#if (SCM_ENABLE_DEPRECATED == 1)
127 ret = scm_acons (scm_sym_arity, scm_procedure_minimum_arity (proc), ret);
128#endif
314b8716 129
3fc7e2c1 130 return ret;
0f2d19dd 131}
1bbd0b84 132#undef FUNC_NAME
0f2d19dd 133
3b3b36dd 134SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
56164a5a
AW
135 (SCM proc, SCM alist),
136 "Set @var{proc}'s property list to @var{alist}.")
1bbd0b84 137#define FUNC_NAME s_scm_set_procedure_properties_x
0f2d19dd 138{
56164a5a
AW
139 SCM_VALIDATE_PROC (1, proc);
140
3fc7e2c1 141#if (SCM_ENABLE_DEPRECATED == 1)
e1bdf9e2
AW
142 if (scm_assq (alist, scm_sym_arity))
143 SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL);
3fc7e2c1 144#endif
e1bdf9e2
AW
145
146 scm_i_pthread_mutex_lock (&overrides_lock);
147 scm_hashq_set_x (overrides, proc, alist);
148 scm_i_pthread_mutex_unlock (&overrides_lock);
314b8716 149
0f2d19dd
JB
150 return SCM_UNSPECIFIED;
151}
1bbd0b84 152#undef FUNC_NAME
0f2d19dd 153
3b3b36dd 154SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
56164a5a
AW
155 (SCM proc, SCM key),
156 "Return the property of @var{proc} with name @var{key}.")
1bbd0b84 157#define FUNC_NAME s_scm_procedure_property
0f2d19dd 158{
56164a5a
AW
159 SCM_VALIDATE_PROC (1, proc);
160
3fc7e2c1 161#if (SCM_ENABLE_DEPRECATED == 1)
56164a5a 162 if (scm_is_eq (key, scm_sym_arity))
3fc7e2c1
AW
163 scm_c_issue_deprecation_warning
164 ("Accessing a procedure's arity via `procedure-property' is deprecated.\n"
165 "Use `procedure-minimum-arity instead.");
166#endif
167
168 return scm_assq_ref (scm_procedure_properties (proc), key);
0f2d19dd 169}
1bbd0b84 170#undef FUNC_NAME
0f2d19dd 171
3b3b36dd 172SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
56164a5a
AW
173 (SCM proc, SCM key, SCM val),
174 "In @var{proc}'s property list, set the property named @var{key} to\n"
175 "@var{val}.")
1bbd0b84 176#define FUNC_NAME s_scm_set_procedure_property_x
0f2d19dd 177{
90fa152c 178 SCM props;
56164a5a 179
e1bdf9e2 180 SCM_VALIDATE_PROC (1, proc);
3fc7e2c1
AW
181
182#if (SCM_ENABLE_DEPRECATED == 1)
56164a5a 183 if (scm_is_eq (key, scm_sym_arity))
3fc7e2c1
AW
184 SCM_MISC_ERROR ("arity is a deprecated read-only property", SCM_EOL);
185#endif
c783b082 186
e1bdf9e2 187 scm_i_pthread_mutex_lock (&overrides_lock);
f2ed4473
AW
188 props = scm_hashq_ref (overrides, proc, SCM_BOOL_F);
189 if (scm_is_false (props))
190 {
191 if (SCM_PROGRAM_P (proc))
192 props = scm_i_program_properties (proc);
193 else
194 props = SCM_EOL;
195 }
90fa152c 196 scm_hashq_set_x (overrides, proc, scm_assq_set_x (props, key, val));
e1bdf9e2 197 scm_i_pthread_mutex_unlock (&overrides_lock);
c783b082 198
0f2d19dd
JB
199 return SCM_UNSPECIFIED;
200}
1bbd0b84 201#undef FUNC_NAME
0f2d19dd
JB
202
203\f
204
1cc91f1b 205
0f2d19dd
JB
206void
207scm_init_procprop ()
0f2d19dd 208{
e1bdf9e2 209 overrides = scm_make_weak_key_hash_table (SCM_UNDEFINED);
a0599745 210#include "libguile/procprop.x"
0f2d19dd
JB
211}
212
89e00824
ML
213
214/*
215 Local Variables:
216 c-file-style: "gnu"
217 End:
218*/