objects.h 7.8 KB

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