1 /* Copyright (C) 1995,1996,1998,2000,2001,2003,2004, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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
25 #define SCM_BUILDING_DEPRECATED_CODE
27 #include "libguile/_scm.h"
29 #include "libguile/alist.h"
30 #include "libguile/deprecation.h"
31 #include "libguile/deprecated.h"
32 #include "libguile/eval.h"
33 #include "libguile/procs.h"
34 #include "libguile/gsubr.h"
35 #include "libguile/smob.h"
36 #include "libguile/root.h"
37 #include "libguile/vectors.h"
38 #include "libguile/hashtab.h"
39 #include "libguile/programs.h"
41 #include "libguile/validate.h"
42 #include "libguile/procprop.h"
45 SCM_GLOBAL_SYMBOL (scm_sym_system_procedure
, "system-procedure");
46 #if (SCM_ENABLE_DEPRECATED == 1)
47 SCM_GLOBAL_SYMBOL (scm_sym_arity
, "arity");
49 SCM_GLOBAL_SYMBOL (scm_sym_name
, "name");
52 static scm_i_pthread_mutex_t overrides_lock
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
55 scm_i_procedure_arity (SCM proc
, int *req
, int *opt
, int *rest
)
57 while (!SCM_PROGRAM_P (proc
))
61 switch (SCM_TYP7 (proc
))
64 if (!SCM_SMOB_APPLICABLE_P (proc
))
66 proc
= scm_i_smob_apply_trampoline (proc
);
69 if (!SCM_STRUCT_APPLICABLE_P (proc
))
71 proc
= SCM_STRUCT_PROCEDURE (proc
);
77 return scm_i_program_arity (proc
, req
, opt
, rest
);
80 SCM_DEFINE (scm_procedure_minimum_arity
, "procedure-minimum-arity", 1, 0, 0,
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
96 if (scm_i_procedure_arity (proc
, &req
, &opt
, &rest
))
97 return scm_list_3 (scm_from_int (req
),
99 scm_from_bool (rest
));
105 SCM_DEFINE (scm_procedure_properties
, "procedure-properties", 1, 0, 0,
107 "Return @var{obj}'s property list.")
108 #define FUNC_NAME s_scm_procedure_properties
112 SCM_VALIDATE_PROC (1, proc
);
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
);
118 if (scm_is_false (ret
))
120 if (SCM_PROGRAM_P (proc
))
121 ret
= scm_i_program_properties (proc
);
126 #if (SCM_ENABLE_DEPRECATED == 1)
127 ret
= scm_acons (scm_sym_arity
, scm_procedure_minimum_arity (proc
), ret
);
134 SCM_DEFINE (scm_set_procedure_properties_x
, "set-procedure-properties!", 2, 0, 0,
135 (SCM proc
, SCM alist
),
136 "Set @var{proc}'s property list to @var{alist}.")
137 #define FUNC_NAME s_scm_set_procedure_properties_x
139 SCM_VALIDATE_PROC (1, proc
);
141 #if (SCM_ENABLE_DEPRECATED == 1)
142 if (scm_assq (alist
, scm_sym_arity
))
143 SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL
);
146 scm_i_pthread_mutex_lock (&overrides_lock
);
147 scm_hashq_set_x (overrides
, proc
, alist
);
148 scm_i_pthread_mutex_unlock (&overrides_lock
);
150 return SCM_UNSPECIFIED
;
154 SCM_DEFINE (scm_procedure_property
, "procedure-property", 2, 0, 0,
156 "Return the property of @var{proc} with name @var{key}.")
157 #define FUNC_NAME s_scm_procedure_property
159 SCM_VALIDATE_PROC (1, proc
);
161 #if (SCM_ENABLE_DEPRECATED == 1)
162 if (scm_is_eq (key
, scm_sym_arity
))
163 scm_c_issue_deprecation_warning
164 ("Accessing a procedure's arity via `procedure-property' is deprecated.\n"
165 "Use `procedure-minimum-arity instead.");
168 return scm_assq_ref (scm_procedure_properties (proc
), key
);
172 SCM_DEFINE (scm_set_procedure_property_x
, "set-procedure-property!", 3, 0, 0,
173 (SCM proc
, SCM key
, SCM val
),
174 "In @var{proc}'s property list, set the property named @var{key} to\n"
176 #define FUNC_NAME s_scm_set_procedure_property_x
180 SCM_VALIDATE_PROC (1, proc
);
182 #if (SCM_ENABLE_DEPRECATED == 1)
183 if (scm_is_eq (key
, scm_sym_arity
))
184 SCM_MISC_ERROR ("arity is a deprecated read-only property", SCM_EOL
);
187 scm_i_pthread_mutex_lock (&overrides_lock
);
188 props
= scm_hashq_ref (overrides
, proc
, SCM_BOOL_F
);
189 if (scm_is_false (props
))
191 if (SCM_PROGRAM_P (proc
))
192 props
= scm_i_program_properties (proc
);
196 scm_hashq_set_x (overrides
, proc
, scm_assq_set_x (props
, key
, val
));
197 scm_i_pthread_mutex_unlock (&overrides_lock
);
199 return SCM_UNSPECIFIED
;
209 overrides
= scm_make_weak_key_hash_table (SCM_UNDEFINED
);
210 #include "libguile/procprop.x"