simplify inline function infrastructure
[bpt/guile.git] / libguile / procs.c
1 /* Copyright (C) 1995,1996,1997,1999,2000,2001, 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/strings.h"
28 #include "libguile/vectors.h"
29 #include "libguile/smob.h"
30 #include "libguile/deprecation.h"
31
32 #include "libguile/validate.h"
33 #include "libguile/procs.h"
34 #include "libguile/procprop.h"
35 #include "libguile/objcodes.h"
36 #include "libguile/programs.h"
37 \f
38
39
40 /* {Procedures}
41 */
42
43
44 SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
45 (SCM obj),
46 "Return @code{#t} if @var{obj} is a procedure.")
47 #define FUNC_NAME s_scm_procedure_p
48 {
49 if (SCM_NIMP (obj))
50 switch (SCM_TYP7 (obj))
51 {
52 case scm_tcs_struct:
53 if (!((SCM_OBJ_CLASS_FLAGS (obj) & SCM_CLASSF_PURE_GENERIC)
54 || SCM_STRUCT_APPLICABLE_P (obj)))
55 break;
56 case scm_tc7_program:
57 return SCM_BOOL_T;
58 case scm_tc7_smob:
59 return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj).apply);
60 default:
61 return SCM_BOOL_F;
62 }
63 return SCM_BOOL_F;
64 }
65 #undef FUNC_NAME
66
67 SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
68 (SCM obj),
69 "Return @code{#t} if @var{obj} is a thunk.")
70 #define FUNC_NAME s_scm_thunk_p
71 {
72 int req, opt, rest;
73 return scm_from_bool (scm_i_procedure_arity (obj, &req, &opt, &rest)
74 && req == 0);
75 }
76 #undef FUNC_NAME
77
78 SCM_SYMBOL (sym_documentation, "documentation");
79
80 SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
81 (SCM proc),
82 "Return the documentation string associated with @code{proc}. By\n"
83 "convention, if a procedure contains more than one expression and the\n"
84 "first expression is a string constant, that string is assumed to contain\n"
85 "documentation for that procedure.")
86 #define FUNC_NAME s_scm_procedure_documentation
87 {
88 SCM_VALIDATE_PROC (SCM_ARG1, proc);
89 return scm_procedure_property (proc, sym_documentation);
90 }
91 #undef FUNC_NAME
92
93
94 /* Procedure-with-setter
95 */
96
97 static SCM pws_vtable;
98
99
100 SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
101 (SCM obj),
102 "Return @code{#t} if @var{obj} is a procedure with an\n"
103 "associated setter procedure.")
104 #define FUNC_NAME s_scm_procedure_with_setter_p
105 {
106 return scm_from_bool (SCM_STRUCTP (obj) && SCM_STRUCT_SETTER_P (obj));
107 }
108 #undef FUNC_NAME
109
110 SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
111 (SCM procedure, SCM setter),
112 "Create a new procedure which behaves like @var{procedure}, but\n"
113 "with the associated setter @var{setter}.")
114 #define FUNC_NAME s_scm_make_procedure_with_setter
115 {
116 SCM name, ret;
117 SCM_VALIDATE_PROC (1, procedure);
118 SCM_VALIDATE_PROC (2, setter);
119 ret = scm_make_struct (pws_vtable, SCM_INUM0,
120 scm_list_2 (procedure, setter));
121
122 /* don't use procedure_name, because don't care enough to do a reverse
123 lookup */
124 name = scm_procedure_property (procedure, scm_sym_name);
125 if (scm_is_true (name))
126 scm_set_procedure_property_x (ret, scm_sym_name, name);
127 return ret;
128 }
129 #undef FUNC_NAME
130
131 SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
132 (SCM proc),
133 "Return the procedure of @var{proc}, which must be an\n"
134 "applicable struct.")
135 #define FUNC_NAME s_scm_procedure
136 {
137 SCM_ASSERT (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc),
138 proc, SCM_ARG1, FUNC_NAME);
139 return SCM_STRUCT_PROCEDURE (proc);
140 }
141 #undef FUNC_NAME
142
143 SCM_PRIMITIVE_GENERIC (scm_setter, "setter", 1, 0, 0,
144 (SCM proc),
145 "Return the setter of @var{proc}, which must be an\n"
146 "applicable struct with a setter.")
147 #define FUNC_NAME s_scm_setter
148 {
149 SCM_GASSERT1 (SCM_STRUCTP (proc), g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
150 if (SCM_STRUCT_SETTER_P (proc))
151 return SCM_STRUCT_SETTER (proc);
152 if (SCM_PUREGENERICP (proc)
153 && SCM_IS_A_P (proc, scm_class_generic_with_setter))
154 /* FIXME: might not be an accessor */
155 return SCM_GENERIC_SETTER (proc);
156 SCM_WTA_DISPATCH_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
157 return SCM_BOOL_F; /* not reached */
158 }
159 #undef FUNC_NAME
160
161 \f
162 void
163 scm_init_procs ()
164 {
165 pws_vtable =
166 scm_c_make_struct (scm_applicable_struct_with_setter_vtable_vtable,
167 0,
168 1,
169 SCM_UNPACK (scm_from_latin1_symbol ("pwpw")));
170
171 #include "libguile/procs.x"
172 }
173
174 /*
175 Local Variables:
176 c-file-style: "gnu"
177 End:
178 */