struct.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* classes: h_files */
  2. #ifndef SCM_STRUCT_H
  3. #define SCM_STRUCT_H
  4. /* Copyright (C) 1995,1997,1999,2000,2001, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2015 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. #include "libguile/__scm.h"
  22. #include "libguile/print.h"
  23. /* The relationship between a struct and its vtable is a bit complicated,
  24. because we want structs to be used as GOOPS' native representation -- which
  25. in turn means we need support for changing the "class" (vtable) of an
  26. "instance" (struct). This necessitates some indirection and trickery.
  27. To summarize, structs are laid out this way:
  28. .-------.
  29. | |
  30. .----------------+---v------------- -
  31. | vtable | data | slot0 | slot1 |
  32. `----------------+----------------- -
  33. | .-------.
  34. | | |
  35. .---v------------+---v------------- -
  36. | vtable | data | slot0 | slot1 |
  37. `----------------+----------------- -
  38. |
  39. v
  40. ...
  41. .-------.
  42. | | |
  43. .---v------------+---v------------- -
  44. .-| vtable | data | slot0 | slot1 |
  45. | `----------------+----------------- -
  46. | ^
  47. `-----'
  48. The DATA indirection (which corresponds to `SCM_STRUCT_DATA ()') is necessary
  49. to implement class redefinition.
  50. For more details, see:
  51. http://wingolog.org/archives/2009/11/09/class-redefinition-in-guile
  52. */
  53. /* All vtables have the following fields. */
  54. #define SCM_VTABLE_BASE_LAYOUT \
  55. "pr" /* layout */ \
  56. "uh" /* flags */ \
  57. "sr" /* self */ \
  58. "uh" /* finalizer */ \
  59. "pw" /* printer */ \
  60. "ph" /* name (hidden from make-struct for back-compat reasons) */ \
  61. "uh" /* size */ \
  62. "uh" /* reserved */
  63. #define scm_vtable_index_layout 0 /* A symbol describing the physical arrangement of this type. */
  64. #define scm_vtable_index_flags 1 /* Class flags */
  65. #define scm_vtable_index_self 2 /* A pointer to the vtable itself */
  66. #define scm_vtable_index_instance_finalize 3 /* Finalizer for instances of this struct type. */
  67. #define scm_vtable_index_instance_printer 4 /* A printer for this struct type. */
  68. #define scm_vtable_index_name 5 /* Name of this vtable. */
  69. #define scm_vtable_index_size 6 /* Number of fields, for simple structs. */
  70. #define scm_vtable_index_reserved_7 7
  71. #define scm_vtable_offset_user 8 /* Where do user fields start in the vtable? */
  72. /* All applicable structs have the following fields. */
  73. #define SCM_APPLICABLE_BASE_LAYOUT \
  74. "pw" /* procedure */
  75. #define SCM_APPLICABLE_WITH_SETTER_BASE_LAYOUT \
  76. "pw" /* procedure */ \
  77. "pw" /* setter */
  78. #define scm_applicable_struct_index_procedure 0 /* The procedure of an applicable
  79. struct. Only valid if the
  80. struct's vtable has the
  81. applicable flag set. */
  82. #define scm_applicable_struct_index_setter 1 /* The setter of an applicable
  83. struct. Only valid if the
  84. struct's vtable has the
  85. setter flag set. */
  86. #define SCM_VTABLE_FLAG_VALIDATED (1L << 0) /* the layout of this vtable been validated? */
  87. #define SCM_VTABLE_FLAG_VTABLE (1L << 1) /* instances of this vtable are themselves vtables? */
  88. #define SCM_VTABLE_FLAG_APPLICABLE_VTABLE (1L << 2) /* instances of this vtable are applicable vtables? */
  89. #define SCM_VTABLE_FLAG_APPLICABLE (1L << 3) /* instances of this vtable are applicable? */
  90. #define SCM_VTABLE_FLAG_SETTER_VTABLE (1L << 4) /* instances of this vtable are applicable-with-setter vtables? */
  91. #define SCM_VTABLE_FLAG_SETTER (1L << 5) /* instances of this vtable are applicable-with-setters? */
  92. #define SCM_VTABLE_FLAG_SIMPLE (1L << 6) /* instances of this vtable have only "p" fields and no tail array*/
  93. #define SCM_VTABLE_FLAG_SIMPLE_RW (1L << 7) /* instances of this vtable have only "pw" fields and no tail array */
  94. #define SCM_VTABLE_FLAG_RESERVED_0 (1L << 8)
  95. #define SCM_VTABLE_FLAG_RESERVED_1 (1L << 9)
  96. #define SCM_VTABLE_FLAG_RESERVED_2 (1L << 10)
  97. #define SCM_VTABLE_FLAG_SMOB_0 (1L << 11)
  98. #define SCM_VTABLE_FLAG_GOOPS_0 (1L << 12)
  99. #define SCM_VTABLE_FLAG_GOOPS_1 (1L << 13)
  100. #define SCM_VTABLE_FLAG_GOOPS_2 (1L << 14)
  101. #define SCM_VTABLE_FLAG_GOOPS_3 (1L << 15)
  102. #define SCM_VTABLE_USER_FLAG_SHIFT 16
  103. typedef void (*scm_t_struct_finalize) (SCM obj);
  104. #define SCM_STRUCTP(X) (!SCM_IMP(X) && (SCM_TYP3(X) == scm_tc3_struct))
  105. #define SCM_STRUCT_SLOTS(X) ((SCM*)SCM_CELL_WORD_1 ((X)))
  106. #define SCM_STRUCT_SLOT_REF(X,I) (SCM_STRUCT_SLOTS (X)[(I)])
  107. #define SCM_STRUCT_SLOT_SET(X,I,V) SCM_STRUCT_SLOTS (X)[(I)]=(V)
  108. #define SCM_STRUCT_DATA(X) ((scm_t_bits*)SCM_CELL_WORD_1 (X))
  109. #define SCM_STRUCT_DATA_REF(X,I) (SCM_STRUCT_DATA (X)[(I)])
  110. #define SCM_STRUCT_DATA_SET(X,I,V) SCM_STRUCT_DATA (X)[(I)]=(V)
  111. /* The SCM_VTABLE_* macros assume that you're passing them a struct which is a
  112. valid vtable. */
  113. #define SCM_VTABLE_LAYOUT(X) (SCM_STRUCT_SLOT_REF ((X), scm_vtable_index_layout))
  114. #define SCM_SET_VTABLE_LAYOUT(X,L) (SCM_STRUCT_SLOT_SET ((X), scm_vtable_index_layout, L))
  115. #define SCM_VTABLE_FLAGS(X) (SCM_STRUCT_DATA_REF (X, scm_vtable_index_flags))
  116. #define SCM_SET_VTABLE_FLAGS(X,F) (SCM_STRUCT_DATA_REF (X, scm_vtable_index_flags) |= (F))
  117. #define SCM_CLEAR_VTABLE_FLAGS(X,F) (SCM_STRUCT_DATA_REF (X, scm_vtable_index_flags) &= (~(F)))
  118. #define SCM_VTABLE_FLAG_IS_SET(X,F) (SCM_STRUCT_DATA_REF (X, scm_vtable_index_flags) & (F))
  119. #define SCM_VTABLE_INSTANCE_FINALIZER(X) ((scm_t_struct_finalize)SCM_STRUCT_DATA_REF (X, scm_vtable_index_instance_finalize))
  120. #define SCM_SET_VTABLE_INSTANCE_FINALIZER(X,P) (SCM_STRUCT_DATA_SET (X, scm_vtable_index_instance_finalize, (scm_t_bits)(P)))
  121. #define SCM_VTABLE_INSTANCE_PRINTER(X) (SCM_STRUCT_SLOT_REF (X, scm_vtable_index_instance_printer))
  122. #define SCM_SET_VTABLE_INSTANCE_PRINTER(X,P) (SCM_STRUCT_SLOT_SET (X, scm_vtable_index_instance_printer, (P)))
  123. #define SCM_VTABLE_NAME(X) (SCM_STRUCT_SLOT_REF (X, scm_vtable_index_name))
  124. #define SCM_SET_VTABLE_NAME(X,V) (SCM_STRUCT_SLOT_SET (X, scm_vtable_index_name, V))
  125. /* Structs hold a pointer to their vtable's data, not the vtable itself. To get
  126. the vtable we have to do an indirection through the self slot. */
  127. #define SCM_STRUCT_VTABLE_DATA(X) ((scm_t_bits*)(SCM_CELL_WORD_0 (X) - scm_tc3_struct))
  128. #define SCM_STRUCT_VTABLE_SLOTS(X) ((SCM*)(SCM_CELL_WORD_0 (X) - scm_tc3_struct))
  129. #define SCM_STRUCT_VTABLE(X) (SCM_STRUCT_VTABLE_SLOTS(X)[scm_vtable_index_self])
  130. /* But often we just need to access the vtable's data; we can do that without
  131. the data->self->data indirection. */
  132. #define SCM_STRUCT_LAYOUT(X) (SCM_STRUCT_VTABLE_SLOTS (X)[scm_vtable_index_layout])
  133. #define SCM_STRUCT_PRINTER(X) (SCM_STRUCT_VTABLE_SLOTS (X)[scm_vtable_index_instance_printer])
  134. #define SCM_STRUCT_FINALIZER(X) ((scm_t_struct_finalize)SCM_STRUCT_VTABLE_DATA (X)[scm_vtable_index_instance_finalize])
  135. #define SCM_STRUCT_VTABLE_FLAGS(X) (SCM_STRUCT_VTABLE_DATA (X)[scm_vtable_index_flags])
  136. #define SCM_STRUCT_VTABLE_FLAG_IS_SET(X,F) (SCM_STRUCT_VTABLE_DATA (X)[scm_vtable_index_flags]&(F))
  137. #define SCM_STRUCT_APPLICABLE_P(X) (SCM_STRUCT_VTABLE_FLAG_IS_SET ((X), SCM_VTABLE_FLAG_APPLICABLE))
  138. #define SCM_STRUCT_SETTER_P(X) (SCM_STRUCT_VTABLE_FLAG_IS_SET ((X), SCM_VTABLE_FLAG_SETTER))
  139. #define SCM_STRUCT_PROCEDURE(X) (SCM_STRUCT_SLOT_REF (X, scm_applicable_struct_index_procedure))
  140. #define SCM_SET_STRUCT_PROCEDURE(X,P) (SCM_STRUCT_SLOT_SET (X, scm_applicable_struct_index_procedure, P))
  141. #define SCM_STRUCT_SETTER(X) (SCM_STRUCT_SLOT_REF (X, scm_applicable_struct_index_setter))
  142. #define SCM_SET_STRUCT_SETTER(X,P) (SCM_STRUCT_SLOT_SET (X, scm_applicable_struct_index_setter, P))
  143. SCM_API SCM scm_standard_vtable_vtable;
  144. SCM_API SCM scm_applicable_struct_vtable_vtable;
  145. SCM_API SCM scm_applicable_struct_with_setter_vtable_vtable;
  146. SCM_API SCM scm_make_struct_layout (SCM fields);
  147. SCM_API SCM scm_struct_p (SCM x);
  148. SCM_API SCM scm_struct_vtable_p (SCM x);
  149. SCM_INTERNAL SCM scm_allocate_struct (SCM vtable, SCM n_words);
  150. SCM_API SCM scm_make_struct (SCM vtable, SCM tail_array_size, SCM init);
  151. SCM_API SCM scm_c_make_struct (SCM vtable, size_t n_tail, size_t n_inits,
  152. scm_t_bits init, ...);
  153. SCM_API SCM scm_c_make_structv (SCM vtable, size_t n_tail, size_t n_inits,
  154. scm_t_bits init[]);
  155. SCM_API SCM scm_make_vtable (SCM fields, SCM printer);
  156. SCM_INTERNAL SCM scm_i_make_vtable_vtable (SCM fields);
  157. SCM_API SCM scm_struct_ref (SCM handle, SCM pos);
  158. SCM_API SCM scm_struct_set_x (SCM handle, SCM pos, SCM val);
  159. SCM_API SCM scm_struct_vtable (SCM handle);
  160. SCM_API SCM scm_struct_vtable_name (SCM vtable);
  161. SCM_API SCM scm_set_struct_vtable_name_x (SCM vtable, SCM name);
  162. SCM_API void scm_print_struct (SCM exp, SCM port, scm_print_state *);
  163. SCM_INTERNAL SCM scm_i_struct_equalp (SCM s1, SCM s2);
  164. SCM_INTERNAL unsigned long scm_struct_ihashq (SCM, unsigned long, void *);
  165. SCM_INTERNAL SCM scm_i_alloc_struct (scm_t_bits *vtable_data, int n_words);
  166. SCM_INTERNAL void scm_i_struct_inherit_vtable_magic (SCM vtable, SCM obj);
  167. SCM_INTERNAL void scm_init_struct (void);
  168. #endif /* SCM_STRUCT_H */
  169. /*
  170. Local Variables:
  171. c-file-style: "gnu"
  172. End:
  173. */