objects.h 7.8 KB

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