objects.h 9.2 KB

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