* objects.h, objects.c, goops.c, goops.h (scm_class_boolean,
[bpt/guile.git] / libguile / objects.h
CommitLineData
1d9ee7c7
MD
1/* classes: h_files */
2
729dbac3
DH
3#ifndef SCM_OBJECTS_H
4#define SCM_OBJECTS_H
1d9ee7c7 5
74b6d6e4 6/* Copyright (C) 1996,1999,2000,2001, 2003 Free Software Foundation, Inc.
0527e687 7 *
73be1d9e
MV
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.
0527e687 12 *
73be1d9e 13 * This library is distributed in the hope that it will be useful,
1d9ee7c7 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
0527e687 17 *
73be1d9e
MV
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
0527e687 22
1d9ee7c7
MD
23\f
24
da7f71d7
MD
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
1d9ee7c7
MD
36#include "libguile/__scm.h"
37#include "libguile/struct.h"
38
39\f
40
da7f71d7
MD
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 */
d8c40b9f
DH
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])
da7f71d7
MD
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))
6ee350ad 51#define SCM_CLASSF_MASK SCM_STRUCTF_MASK
da7f71d7 52
82fe8ff1
MD
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)
da7f71d7
MD
57
58#define SCM_I_OPERATORP(obj)\
c209c88e 59 ((SCM_OBJ_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR) != 0)
da7f71d7
MD
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))
e34e12f0 64#define SCM_OPERATOR_PROCEDURE(obj) (SCM_OBJ_OPERATOR_CLASS (obj)->procedure)
dae5a1e9 65#define SCM_OPERATOR_SETTER(obj) (SCM_OBJ_OPERATOR_CLASS (obj)->setter)
da7f71d7
MD
66
67#define SCM_I_ENTITYP(obj)\
c209c88e 68 ((SCM_OBJ_CLASS_FLAGS (obj) & SCM_CLASSF_ENTITY) != 0)
e34e12f0 69#define SCM_ENTITY_PROCEDURE(obj) \
d8c40b9f 70 (SCM_PACK (SCM_STRUCT_DATA (obj) [scm_struct_i_procedure]))
34d19ef6 71#define SCM_SET_ENTITY_PROCEDURE(obj, v) \
d8c40b9f
DH
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]))
322ec19d
ML
74#define SCM_SET_ENTITY_SETTER(obj, v) \
75 (SCM_STRUCT_DATA (obj) [scm_struct_i_setter] = SCM_UNPACK (v))
1d9ee7c7 76
2eafbe52
MD
77#define SCM_SET_CLASS_DESTRUCTOR(c, d) SCM_SET_VTABLE_DESTRUCTOR (c, d)
78#define SCM_SET_CLASS_INSTANCE_SIZE(c, s) \
729dbac3
DH
79 (SCM_STRUCT_DATA (c)[scm_struct_i_size] \
80 = (SCM_STRUCT_DATA (c) [scm_struct_i_size] & SCM_STRUCTF_MASK) | s)
2eafbe52 81
da7f71d7
MD
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 */
aa3bdf59 114#define SCM_METACLASS_STANDARD_LAYOUT ""
1d9ee7c7
MD
115struct scm_metaclass_standard {
116 SCM layout;
117 SCM vcell;
118 SCM vtable;
119 SCM print;
1d9ee7c7
MD
120};
121
e34e12f0 122#define SCM_METACLASS_OPERATOR_LAYOUT "popo"
da7f71d7
MD
123struct scm_metaclass_operator {
124 SCM layout;
125 SCM vcell;
126 SCM vtable;
127 SCM print;
e34e12f0 128 SCM procedure;
dae5a1e9 129 SCM setter;
da7f71d7
MD
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 */
82fe8ff1 140#define SCM_ENTITY_LAYOUT ""
1d9ee7c7 141
6ee350ad
MD
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. */
a43a8375
MD
150#define SCM_CLASSF_PURE_GENERIC (0x010 << 20)
151#define SCM_CLASSF_GOOPS_VALID (0x080 << 20)
152#define SCM_CLASSF_GOOPS (0x100 << 20)
e9385404
MV
153#define scm_si_redefined 5
154#define scm_si_hashsets 6
a43a8375 155#define SCM_CLASS_OF(x) SCM_STRUCT_VTABLE (x)
1c3e63f0 156#define SCM_OBJ_CLASS_REDEF(x) (SCM_PACK (SCM_STRUCT_VTABLE_DATA (x) [scm_si_redefined]))
dae5a1e9 157
f0574557
MD
158typedef 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
9de33deb 168#define SCM_CMETHOD_CODE(cmethod) SCM_CDR (cmethod)
f12745b6
DH
169#define SCM_CMETHOD_FORMALS(cmethod) SCM_CAR (SCM_CMETHOD_CODE (cmethod))
170#define SCM_CMETHOD_BODY(cmethod) SCM_CDR (SCM_CMETHOD_CODE (cmethod))
9de33deb
MD
171#define SCM_CMETHOD_ENV(cmethod) SCM_CAR (cmethod)
172
d0efbe61
MD
173/* Port classes */
174#define SCM_IN_PCLASS_INDEX 0x000
175#define SCM_OUT_PCLASS_INDEX 0x100
176#define SCM_INOUT_PCLASS_INDEX 0x200
177
6ee350ad 178/* Plugin proxy classes for basic types. */
33b001fd
MV
179SCM_API SCM scm_metaclass_standard;
180SCM_API SCM scm_metaclass_operator;
9de33deb 181
5e890431 182/* Goops functions. */
da0e6c2b 183SCM_API SCM scm_make_extended_class (char const *type_name, int applicablep);
74b6d6e4 184SCM_API void scm_i_inherit_applicable (SCM c);
33b001fd
MV
185SCM_API void scm_make_port_classes (long ptobnum, char *type_name);
186SCM_API void scm_change_object_class (SCM, SCM, SCM);
187SCM_API SCM scm_memoize_method (SCM x, SCM args);
188
33b001fd
MV
189SCM_API SCM scm_mcache_lookup_cmethod (SCM cache, SCM args);
190SCM_API SCM scm_mcache_compute_cmethod (SCM cache, SCM args);
9de33deb 191/* The following are declared in __scm.h
33b001fd
MV
192SCM_API SCM scm_call_generic_0 (SCM gf);
193SCM_API SCM scm_call_generic_1 (SCM gf, SCM a1);
194SCM_API SCM scm_call_generic_2 (SCM gf, SCM a1, SCM a2);
195SCM_API SCM scm_apply_generic (SCM gf, SCM args);
9de33deb 196*/
33b001fd
MV
197SCM_API SCM scm_call_generic_3 (SCM gf, SCM a1, SCM a2, SCM a3);
198SCM_API SCM scm_entity_p (SCM obj);
199SCM_API SCM scm_operator_p (SCM obj);
200SCM_API SCM scm_valid_object_procedure_p (SCM proc);
201SCM_API SCM scm_set_object_procedure_x (SCM obj, SCM proc);
a43a8375 202#ifdef GUILE_DEBUG
33b001fd 203SCM_API SCM scm_object_procedure (SCM obj);
a43a8375 204#endif
33b001fd
MV
205SCM_API SCM scm_make_class_object (SCM metaclass, SCM layout);
206SCM_API SCM scm_make_subclass_object (SCM c, SCM layout);
2d5881d5 207
33b001fd
MV
208SCM_API SCM scm_i_make_class_object (SCM metaclass, SCM layout_string,
209 unsigned long flags);
210SCM_API void scm_init_objects (void);
1d9ee7c7 211
0527e687 212#endif /* SCM_OBJECTS_H */
89e00824
ML
213
214/*
215 Local Variables:
216 c-file-style: "gnu"
217 End:
218*/