9b2a0ed5a0b2ad50e34245c9df4f1476239a4604
[bpt/guile.git] / libguile / objects.h
1 /* classes: h_files */
2
3 #ifndef SCM_OBJECTS_H
4 #define SCM_OBJECTS_H
5
6 /* Copyright (C) 1996,1999,2000,2001, 2003, 2006, 2008, 2009 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 \f
24
25 /* This file and objects.c contains those minimal pieces of the Guile
26 * Object Oriented Programming System which need to be included in
27 * libguile.
28 *
29 * {Objects and structs}
30 *
31 * Objects are currently based upon structs. Although the struct
32 * implementation will change thoroughly in the future, objects will
33 * still be based upon structs.
34 */
35
36 #include "libguile/__scm.h"
37 #include "libguile/struct.h"
38
39 \f
40
41 /* {Class flags}
42 *
43 * These are used for efficient identification of instances of a
44 * certain class or its subclasses when traversal of the inheritance
45 * graph would be too costly.
46 */
47 #define SCM_CLASS_FLAGS(class) (SCM_STRUCT_DATA (class) [scm_struct_i_flags])
48 #define SCM_OBJ_CLASS_FLAGS(obj) (SCM_STRUCT_VTABLE_DATA (obj) [scm_struct_i_flags])
49 #define SCM_SET_CLASS_FLAGS(c, f) (SCM_CLASS_FLAGS (c) |= (f))
50 #define SCM_CLEAR_CLASS_FLAGS(c, f) (SCM_CLASS_FLAGS (c) &= ~(f))
51 #define SCM_CLASSF_MASK SCM_STRUCTF_MASK
52
53 #define SCM_CLASSF_ENTITY SCM_STRUCTF_ENTITY
54 /* Operator classes need to be identified in the evaluator.
55 (Entities also have SCM_CLASSF_OPERATOR set in their vtable.) */
56 #define SCM_CLASSF_OPERATOR (1L << 29)
57
58 #define SCM_I_OPERATORP(obj)\
59 ((SCM_OBJ_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR) != 0)
60 #define SCM_OPERATOR_CLASS(obj)\
61 ((struct scm_metaclass_operator *) SCM_STRUCT_DATA (obj))
62 #define SCM_OBJ_OPERATOR_CLASS(obj)\
63 ((struct scm_metaclass_operator *) SCM_STRUCT_VTABLE_DATA (obj))
64 #define SCM_OPERATOR_PROCEDURE(obj) (SCM_OBJ_OPERATOR_CLASS (obj)->procedure)
65 #define SCM_OPERATOR_SETTER(obj) (SCM_OBJ_OPERATOR_CLASS (obj)->setter)
66
67 #define SCM_I_ENTITYP(obj)\
68 ((SCM_OBJ_CLASS_FLAGS (obj) & SCM_CLASSF_ENTITY) != 0)
69 #define SCM_ENTITY_PROCEDURE(obj) \
70 (SCM_PACK (SCM_STRUCT_DATA (obj) [scm_struct_i_procedure]))
71 #define SCM_SET_ENTITY_PROCEDURE(obj, v) \
72 (SCM_STRUCT_DATA (obj) [scm_struct_i_procedure] = SCM_UNPACK (v))
73 #define SCM_ENTITY_SETTER(obj) (SCM_PACK (SCM_STRUCT_DATA (obj)[scm_struct_i_setter]))
74 #define SCM_SET_ENTITY_SETTER(obj, v) \
75 (SCM_STRUCT_DATA (obj) [scm_struct_i_setter] = SCM_UNPACK (v))
76
77 #define SCM_SET_CLASS_DESTRUCTOR(c, d) SCM_SET_VTABLE_DESTRUCTOR (c, d)
78 #define SCM_SET_CLASS_INSTANCE_SIZE(c, s) \
79 (SCM_STRUCT_DATA (c)[scm_struct_i_size] \
80 = (SCM_STRUCT_DATA (c) [scm_struct_i_size] & SCM_STRUCTF_MASK) | s)
81
82 /* {Operator classes}
83 *
84 * Instances of operator classes can work as operators, i. e., they
85 * can be applied to arguments just as if they were ordinary
86 * procedures.
87 *
88 * For instances of operator classes, the procedures to be applied are
89 * stored in four dedicated slots in the associated class object.
90 * Which one is selected depends on the number of arguments in the
91 * application.
92 *
93 * If zero arguments are passed, the first will be selected.
94 * If one argument is passed, the second will be selected.
95 * If two arguments are passed, the third will be selected.
96 * If three or more arguments are passed, the fourth will be selected.
97 *
98 * This is complicated and may seem gratuitous but has to do with the
99 * architecture of the evaluator. Using only one procedure would
100 * result in a great deal less efficient application, loss of
101 * tail-recursion and would be difficult to reconcile with the
102 * debugging evaluator.
103 *
104 * Also, using this "forked" application in low-level code has the
105 * advantage of speeding up some code. An example is method dispatch
106 * for generic operators applied to few arguments. On the user level,
107 * the "forked" application will be hidden by mechanisms in the GOOPS
108 * package.
109 *
110 * Operator classes have the metaclass <operator-metaclass>.
111 *
112 * An example of an operator class is the class <tk-command>.
113 */
114 #define SCM_METACLASS_STANDARD_LAYOUT ""
115 struct scm_metaclass_standard {
116 SCM layout;
117 SCM vcell;
118 SCM vtable;
119 SCM print;
120 };
121
122 #define SCM_METACLASS_OPERATOR_LAYOUT "popo"
123 struct scm_metaclass_operator {
124 SCM layout;
125 SCM vcell;
126 SCM vtable;
127 SCM print;
128 SCM procedure;
129 SCM setter;
130 };
131
132 /* {Entity classes}
133 *
134 * For instances of entity classes (entities), the procedures to be
135 * applied are stored in the instance itself rather than in the class
136 * object as is the case for instances of operator classes (see above).
137 *
138 * An example of an entity class is the class of generic methods.
139 */
140 #define SCM_ENTITY_LAYOUT ""
141
142 /* {Interface to Goops}
143 *
144 * The evaluator contains a multi-method dispatch mechanism.
145 * This interface is used by that mechanism and during creation of
146 * smob and struct classes.
147 */
148
149 /* Internal representation of Goops objects. */
150 #define SCM_CLASSF_PURE_GENERIC (0x010 << 20)
151 #define SCM_CLASSF_GOOPS_VALID (0x080 << 20)
152 #define SCM_CLASSF_GOOPS (0x100 << 20)
153 #define scm_si_redefined 5
154 #define scm_si_hashsets 6
155 #define SCM_CLASS_OF(x) SCM_STRUCT_VTABLE (x)
156 #define SCM_OBJ_CLASS_REDEF(x) (SCM_PACK (SCM_STRUCT_VTABLE_DATA (x) [scm_si_redefined]))
157
158 typedef struct scm_effective_slot_definition {
159 SCM name;
160 long location;
161 SCM init_value;
162 SCM (*get) (SCM obj, SCM slotdef);
163 SCM (*set) (SCM obj, SCM slotdef, SCM value);
164 } scm_effective_slot_definition;
165
166 #define SCM_ESLOTDEF(x) ((scm_effective_slot_definition *) SCM_CDR (x))
167
168 #define SCM_CMETHOD_CODE(cmethod) SCM_CDR (cmethod)
169 #define SCM_CMETHOD_FORMALS(cmethod) SCM_CAR (SCM_CMETHOD_CODE (cmethod))
170 #define SCM_CMETHOD_BODY(cmethod) SCM_CDR (SCM_CMETHOD_CODE (cmethod))
171 #define SCM_CMETHOD_ENV(cmethod) SCM_CAR (cmethod)
172
173 /* Port classes */
174 #define SCM_IN_PCLASS_INDEX 0
175 #define SCM_OUT_PCLASS_INDEX SCM_I_MAX_PORT_TYPE_COUNT
176 #define SCM_INOUT_PCLASS_INDEX (2 * SCM_I_MAX_PORT_TYPE_COUNT)
177
178 /* Plugin proxy classes for basic types. */
179 SCM_API SCM scm_metaclass_standard;
180 SCM_API SCM scm_metaclass_operator;
181
182 /* Goops functions. */
183 SCM_API SCM scm_make_extended_class (char const *type_name, int applicablep);
184 SCM_INTERNAL void scm_i_inherit_applicable (SCM c);
185 SCM_API void scm_make_port_classes (long ptobnum, char *type_name);
186 SCM_API void scm_change_object_class (SCM, SCM, SCM);
187 SCM_API SCM scm_memoize_method (SCM x, SCM args);
188
189 SCM_API SCM scm_mcache_lookup_cmethod (SCM cache, SCM args);
190 SCM_API SCM scm_mcache_compute_cmethod (SCM cache, SCM args);
191 /* The following are declared in __scm.h
192 SCM_API SCM scm_call_generic_0 (SCM gf);
193 SCM_API SCM scm_call_generic_1 (SCM gf, SCM a1);
194 SCM_API SCM scm_call_generic_2 (SCM gf, SCM a1, SCM a2);
195 SCM_API SCM scm_apply_generic (SCM gf, SCM args);
196 */
197 SCM_API SCM scm_call_generic_3 (SCM gf, SCM a1, SCM a2, SCM a3);
198 SCM_API SCM scm_entity_p (SCM obj);
199 SCM_API SCM scm_operator_p (SCM obj);
200 SCM_API SCM scm_valid_object_procedure_p (SCM proc);
201 SCM_API SCM scm_set_object_procedure_x (SCM obj, SCM proc);
202 #ifdef GUILE_DEBUG
203 SCM_API SCM scm_object_procedure (SCM obj);
204 #endif
205 SCM_API SCM scm_make_class_object (SCM metaclass, SCM layout);
206 SCM_API SCM scm_make_subclass_object (SCM c, SCM layout);
207
208 SCM_INTERNAL SCM scm_i_make_class_object (SCM metaclass, SCM layout_string,
209 unsigned long flags);
210 SCM_INTERNAL void scm_init_objects (void);
211
212 #endif /* SCM_OBJECTS_H */
213
214 /*
215 Local Variables:
216 c-file-style: "gnu"
217 End:
218 */