remove all deprecated code
[bpt/guile.git] / libguile / procprop.c
1 /* Copyright (C) 1995,1996,1998,2000,2001,2003,2004, 2006, 2008, 2009, 2010, 2011 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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
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.
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
16 * 02110-1301 USA
17 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
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"
31 #include "libguile/smob.h"
32 #include "libguile/root.h"
33 #include "libguile/vectors.h"
34 #include "libguile/hashtab.h"
35 #include "libguile/programs.h"
36
37 #include "libguile/validate.h"
38 #include "libguile/procprop.h"
39 \f
40
41 SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
42 SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
43
44 static SCM overrides;
45 static scm_i_pthread_mutex_t overrides_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
46
47 int
48 scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest)
49 {
50 while (!SCM_PROGRAM_P (proc))
51 {
52 if (SCM_IMP (proc))
53 return 0;
54 switch (SCM_TYP7 (proc))
55 {
56 case scm_tc7_smob:
57 if (!SCM_SMOB_APPLICABLE_P (proc))
58 return 0;
59 proc = scm_i_smob_apply_trampoline (proc);
60 break;
61 case scm_tcs_struct:
62 if (!SCM_STRUCT_APPLICABLE_P (proc))
63 return 0;
64 proc = SCM_STRUCT_PROCEDURE (proc);
65 break;
66 default:
67 return 0;
68 }
69 }
70 return scm_i_program_arity (proc, req, opt, rest);
71 }
72
73 SCM_DEFINE (scm_procedure_minimum_arity, "procedure-minimum-arity", 1, 0, 0,
74 (SCM proc),
75 "Return the \"minimum arity\" of a procedure.\n\n"
76 "If the procedure has only one arity, that arity is returned\n"
77 "as a list of three values: the number of required arguments,\n"
78 "the number of optional arguments, and a boolean indicating\n"
79 "whether or not the procedure takes rest arguments.\n\n"
80 "For a case-lambda procedure, the arity returned is the one\n"
81 "with the lowest minimum number of arguments, and the highest\n"
82 "maximum number of arguments.\n\n"
83 "If it was not possible to determine the arity of the procedure,\n"
84 "@code{#f} is returned.")
85 #define FUNC_NAME s_scm_procedure_minimum_arity
86 {
87 int req, opt, rest;
88
89 if (scm_i_procedure_arity (proc, &req, &opt, &rest))
90 return scm_list_3 (scm_from_int (req),
91 scm_from_int (opt),
92 scm_from_bool (rest));
93 else
94 return SCM_BOOL_F;
95 }
96 #undef FUNC_NAME
97
98 SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
99 (SCM proc),
100 "Return @var{obj}'s property list.")
101 #define FUNC_NAME s_scm_procedure_properties
102 {
103 SCM ret;
104
105 SCM_VALIDATE_PROC (1, proc);
106
107 scm_i_pthread_mutex_lock (&overrides_lock);
108 ret = scm_hashq_ref (overrides, proc, SCM_BOOL_F);
109 scm_i_pthread_mutex_unlock (&overrides_lock);
110
111 if (scm_is_false (ret))
112 {
113 if (SCM_PROGRAM_P (proc))
114 ret = scm_i_program_properties (proc);
115 else
116 ret = SCM_EOL;
117 }
118
119 return ret;
120 }
121 #undef FUNC_NAME
122
123 SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
124 (SCM proc, SCM alist),
125 "Set @var{proc}'s property list to @var{alist}.")
126 #define FUNC_NAME s_scm_set_procedure_properties_x
127 {
128 SCM_VALIDATE_PROC (1, proc);
129
130 scm_i_pthread_mutex_lock (&overrides_lock);
131 scm_hashq_set_x (overrides, proc, alist);
132 scm_i_pthread_mutex_unlock (&overrides_lock);
133
134 return SCM_UNSPECIFIED;
135 }
136 #undef FUNC_NAME
137
138 SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
139 (SCM proc, SCM key),
140 "Return the property of @var{proc} with name @var{key}.")
141 #define FUNC_NAME s_scm_procedure_property
142 {
143 SCM_VALIDATE_PROC (1, proc);
144
145 return scm_assq_ref (scm_procedure_properties (proc), key);
146 }
147 #undef FUNC_NAME
148
149 SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
150 (SCM proc, SCM key, SCM val),
151 "In @var{proc}'s property list, set the property named @var{key} to\n"
152 "@var{val}.")
153 #define FUNC_NAME s_scm_set_procedure_property_x
154 {
155 SCM props;
156
157 SCM_VALIDATE_PROC (1, proc);
158
159 props = scm_procedure_properties (proc);
160
161 scm_i_pthread_mutex_lock (&overrides_lock);
162 scm_hashq_set_x (overrides, proc, scm_assq_set_x (props, key, val));
163 scm_i_pthread_mutex_unlock (&overrides_lock);
164
165 return SCM_UNSPECIFIED;
166 }
167 #undef FUNC_NAME
168
169 \f
170
171
172 void
173 scm_init_procprop ()
174 {
175 overrides = scm_make_weak_key_hash_table (SCM_UNDEFINED);
176 #include "libguile/procprop.x"
177 }
178
179
180 /*
181 Local Variables:
182 c-file-style: "gnu"
183 End:
184 */