Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / procs.c
1 /* Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2006, 2008, 2009,
2 * 2010, 2011, 2012 Free Software Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
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.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
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
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20
21 \f
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "libguile/_scm.h"
27
28 #include "libguile/strings.h"
29 #include "libguile/vectors.h"
30 #include "libguile/smob.h"
31 #include "libguile/deprecation.h"
32
33 #include "libguile/validate.h"
34 #include "libguile/procs.h"
35 #include "libguile/procprop.h"
36 #include "libguile/objcodes.h"
37 #include "libguile/programs.h"
38 \f
39
40
41 /* {Procedures}
42 */
43
44
45 SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
46 (SCM obj),
47 "Return @code{#t} if @var{obj} is a procedure.")
48 #define FUNC_NAME s_scm_procedure_p
49 {
50 return scm_from_bool (SCM_PROGRAM_P (obj)
51 || (SCM_STRUCTP (obj) && SCM_STRUCT_APPLICABLE_P (obj))
52 || (SCM_HAS_TYP7 (obj, scm_tc7_smob)
53 && SCM_SMOB_APPLICABLE_P (obj)));
54 }
55 #undef FUNC_NAME
56
57 SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
58 (SCM obj),
59 "Return @code{#t} if @var{obj} is a thunk.")
60 #define FUNC_NAME s_scm_thunk_p
61 {
62 int req, opt, rest;
63 return scm_from_bool (scm_i_procedure_arity (obj, &req, &opt, &rest)
64 && req == 0);
65 }
66 #undef FUNC_NAME
67
68 SCM_GLOBAL_SYMBOL (scm_sym_documentation, "documentation");
69
70 SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
71 (SCM proc),
72 "Return the documentation string associated with @code{proc}. By\n"
73 "convention, if a procedure contains more than one expression and the\n"
74 "first expression is a string constant, that string is assumed to contain\n"
75 "documentation for that procedure.")
76 #define FUNC_NAME s_scm_procedure_documentation
77 {
78 SCM_VALIDATE_PROC (SCM_ARG1, proc);
79 return scm_procedure_property (proc, scm_sym_documentation);
80 }
81 #undef FUNC_NAME
82
83
84 /* Procedure-with-setter
85 */
86
87 static SCM pws_vtable;
88
89
90 SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
91 (SCM obj),
92 "Return @code{#t} if @var{obj} is a procedure with an\n"
93 "associated setter procedure.")
94 #define FUNC_NAME s_scm_procedure_with_setter_p
95 {
96 return scm_from_bool (SCM_STRUCTP (obj) && SCM_STRUCT_SETTER_P (obj));
97 }
98 #undef FUNC_NAME
99
100 SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
101 (SCM procedure, SCM setter),
102 "Create a new procedure which behaves like @var{procedure}, but\n"
103 "with the associated setter @var{setter}.")
104 #define FUNC_NAME s_scm_make_procedure_with_setter
105 {
106 SCM name, ret;
107 SCM_VALIDATE_PROC (1, procedure);
108 SCM_VALIDATE_PROC (2, setter);
109 ret = scm_make_struct (pws_vtable, SCM_INUM0,
110 scm_list_2 (procedure, setter));
111
112 /* don't use procedure_name, because don't care enough to do a reverse
113 lookup */
114 name = scm_procedure_property (procedure, scm_sym_name);
115 if (scm_is_true (name))
116 scm_set_procedure_property_x (ret, scm_sym_name, name);
117 return ret;
118 }
119 #undef FUNC_NAME
120
121 SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
122 (SCM proc),
123 "Return the procedure of @var{proc}, which must be an\n"
124 "applicable struct.")
125 #define FUNC_NAME s_scm_procedure
126 {
127 SCM_ASSERT (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc),
128 proc, SCM_ARG1, FUNC_NAME);
129 return SCM_STRUCT_PROCEDURE (proc);
130 }
131 #undef FUNC_NAME
132
133 SCM_PRIMITIVE_GENERIC (scm_setter, "setter", 1, 0, 0,
134 (SCM proc),
135 "Return the setter of @var{proc}, which must be an\n"
136 "applicable struct with a setter.")
137 #define FUNC_NAME s_scm_setter
138 {
139 if (SCM_UNLIKELY (!SCM_STRUCTP (proc)))
140 return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
141 if (SCM_STRUCT_SETTER_P (proc))
142 return SCM_STRUCT_SETTER (proc);
143 if (SCM_PUREGENERICP (proc)
144 && SCM_IS_A_P (proc, scm_class_generic_with_setter))
145 /* FIXME: might not be an accessor */
146 return SCM_GENERIC_SETTER (proc);
147 return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
148 return SCM_BOOL_F; /* not reached */
149 }
150 #undef FUNC_NAME
151
152 \f
153 void
154 scm_init_procs ()
155 {
156 pws_vtable =
157 scm_c_make_struct (scm_applicable_struct_with_setter_vtable_vtable,
158 0,
159 1,
160 SCM_UNPACK (scm_from_latin1_symbol ("pwpw")));
161
162 #include "libguile/procs.x"
163 }
164
165 /*
166 Local Variables:
167 c-file-style: "gnu"
168 End:
169 */