procedure-properties incorporates program-properties
[bpt/guile.git] / libguile / procprop.c
CommitLineData
fd12a19a 1/* Copyright (C) 1995,1996,1998,2000,2001,2003,2004, 2006, 2008, 2009, 2010 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 42SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity");
fd12a19a 43SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
67e60655 44
e1bdf9e2
AW
45static SCM overrides;
46static scm_i_pthread_mutex_t overrides_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
56164a5a 47
314b8716
AW
48int
49scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest)
67e60655 50{
75c3ed28 51 while (!SCM_PROGRAM_P (proc))
67e60655 52 {
75c3ed28 53 if (SCM_IMP (proc))
314b8716 54 return 0;
75c3ed28
AW
55 switch (SCM_TYP7 (proc))
56 {
57 case scm_tc7_smob:
58 if (!SCM_SMOB_APPLICABLE_P (proc))
59 return 0;
60 proc = scm_i_smob_apply_trampoline (proc);
61 break;
62 case scm_tcs_struct:
63 if (!SCM_STRUCT_APPLICABLE_P (proc))
64 return 0;
65 proc = SCM_STRUCT_PROCEDURE (proc);
66 break;
67 default:
68 return 0;
69 }
67e60655 70 }
75c3ed28 71 return scm_i_program_arity (proc, req, opt, rest);
67e60655
MD
72}
73
3b3b36dd 74SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
1bbd0b84 75 (SCM proc),
b380b885 76 "Return @var{obj}'s property list.")
1bbd0b84 77#define FUNC_NAME s_scm_procedure_properties
0f2d19dd 78{
314b8716
AW
79 SCM ret;
80 int req, opt, rest;
56164a5a 81
34d19ef6 82 SCM_VALIDATE_PROC (1, proc);
314b8716 83
e1bdf9e2
AW
84 scm_i_pthread_mutex_lock (&overrides_lock);
85 ret = scm_hashq_ref (overrides, proc, SCM_BOOL_F);
86 scm_i_pthread_mutex_unlock (&overrides_lock);
314b8716 87
e1bdf9e2
AW
88 if (scm_is_false (ret))
89 {
90 if (SCM_PROGRAM_P (proc))
91 ret = scm_program_properties (proc);
92 else
93 ret = SCM_EOL;
94 }
95
314b8716
AW
96 scm_i_procedure_arity (proc, &req, &opt, &rest);
97
98 return scm_acons (scm_sym_arity,
99 scm_list_3 (scm_from_int (req),
100 scm_from_int (opt),
101 scm_from_bool (rest)),
102 ret);
0f2d19dd 103}
1bbd0b84 104#undef FUNC_NAME
0f2d19dd 105
3b3b36dd 106SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
56164a5a
AW
107 (SCM proc, SCM alist),
108 "Set @var{proc}'s property list to @var{alist}.")
1bbd0b84 109#define FUNC_NAME s_scm_set_procedure_properties_x
0f2d19dd 110{
56164a5a
AW
111 SCM_VALIDATE_PROC (1, proc);
112
e1bdf9e2
AW
113 if (scm_assq (alist, scm_sym_arity))
114 SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL);
115
116 scm_i_pthread_mutex_lock (&overrides_lock);
117 scm_hashq_set_x (overrides, proc, alist);
118 scm_i_pthread_mutex_unlock (&overrides_lock);
314b8716 119
0f2d19dd
JB
120 return SCM_UNSPECIFIED;
121}
1bbd0b84 122#undef FUNC_NAME
0f2d19dd 123
3b3b36dd 124SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
56164a5a
AW
125 (SCM proc, SCM key),
126 "Return the property of @var{proc} with name @var{key}.")
1bbd0b84 127#define FUNC_NAME s_scm_procedure_property
0f2d19dd 128{
56164a5a
AW
129 SCM_VALIDATE_PROC (1, proc);
130
131 if (scm_is_eq (key, scm_sym_arity))
132 /* avoid a cons in this case */
314b8716
AW
133 {
134 int req, opt, rest;
135 scm_i_procedure_arity (proc, &req, &opt, &rest);
136 return scm_list_3 (scm_from_int (req),
137 scm_from_int (opt),
138 scm_from_bool (rest));
139 }
56164a5a 140 else
c783b082 141 {
e1bdf9e2
AW
142 SCM alist;
143 alist = scm_procedure_properties (proc);
144 return scm_assq_ref (alist, key);
c783b082 145 }
0f2d19dd 146}
1bbd0b84 147#undef FUNC_NAME
0f2d19dd 148
3b3b36dd 149SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
56164a5a
AW
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}.")
1bbd0b84 153#define FUNC_NAME s_scm_set_procedure_property_x
0f2d19dd 154{
e1bdf9e2 155 SCM alist;
56164a5a 156
e1bdf9e2 157 SCM_VALIDATE_PROC (1, proc);
56164a5a 158 if (scm_is_eq (key, scm_sym_arity))
1bbd0b84 159 SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL);
c783b082 160
e1bdf9e2
AW
161 alist = scm_procedure_properties (proc);
162 scm_i_pthread_mutex_lock (&overrides_lock);
163 scm_hashq_set_x (overrides, proc, scm_assq_set_x (alist, key, val));
164 scm_i_pthread_mutex_unlock (&overrides_lock);
c783b082 165
0f2d19dd
JB
166 return SCM_UNSPECIFIED;
167}
1bbd0b84 168#undef FUNC_NAME
0f2d19dd
JB
169
170\f
171
1cc91f1b 172
0f2d19dd
JB
173void
174scm_init_procprop ()
0f2d19dd 175{
e1bdf9e2 176 overrides = scm_make_weak_key_hash_table (SCM_UNDEFINED);
a0599745 177#include "libguile/procprop.x"
0f2d19dd
JB
178}
179
89e00824
ML
180
181/*
182 Local Variables:
183 c-file-style: "gnu"
184 End:
185*/