pairs.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* classes: h_files */
  2. #ifndef SCM_PAIRS_H
  3. #define SCM_PAIRS_H
  4. /* Copyright (C) 1995,1996,2000,2001, 2004, 2006, 2008, 2009, 2010, 2012 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/gc.h"
  23. #if (SCM_DEBUG_PAIR_ACCESSES == 1)
  24. # define SCM_VALIDATE_PAIR(cell, expr) \
  25. ((!scm_is_pair (cell) ? scm_error_pair_access (cell), 0 : 0), (expr))
  26. #else
  27. # define SCM_VALIDATE_PAIR(cell, expr) (expr)
  28. #endif
  29. /*
  30. * Use scm_is_null_and_not_nil if it's important (for correctness)
  31. * that #nil must NOT be considered null.
  32. */
  33. #define scm_is_null_and_not_nil(x) (scm_is_eq ((x), SCM_EOL))
  34. /*
  35. * Use scm_is_null_assume_not_nil if
  36. #nil will never be tested,
  37. * for increased efficiency.
  38. */
  39. #define scm_is_null_assume_not_nil(x) (scm_is_eq ((x), SCM_EOL))
  40. /*
  41. * See the comments preceeding the definitions of SCM_BOOL_F and
  42. * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information on
  43. * how the following macro works.
  44. */
  45. #define scm_is_null_or_nil(x) \
  46. (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_ELISP_NIL, SCM_EOL))
  47. /* Older spellings for these null, nil, and pair predicates. */
  48. #define SCM_NILP(x) (scm_is_eq ((x), SCM_ELISP_NIL))
  49. #define SCM_NULL_OR_NIL_P(x) (scm_is_null_or_nil (x))
  50. #define SCM_NULLP(x) (scm_is_null (x))
  51. #define SCM_NNULLP(x) (!scm_is_null (x))
  52. #define SCM_CONSP(x) (scm_is_pair (x))
  53. #define SCM_NCONSP(x) (!SCM_CONSP (x))
  54. /* #nil is null. */
  55. #define scm_is_null(x) (scm_is_null_or_nil(x))
  56. #define SCM_CAR(x) (SCM_VALIDATE_PAIR (x, SCM_CELL_OBJECT_0 (x)))
  57. #define SCM_CDR(x) (SCM_VALIDATE_PAIR (x, SCM_CELL_OBJECT_1 (x)))
  58. #define SCM_SETCAR(x, v) (SCM_VALIDATE_PAIR (x, SCM_SET_CELL_OBJECT_0 ((x), (v))))
  59. #define SCM_SETCDR(x, v) (SCM_VALIDATE_PAIR (x, SCM_SET_CELL_OBJECT_1 ((x), (v))))
  60. #define SCM_CAAR(OBJ) SCM_CAR (SCM_CAR (OBJ))
  61. #define SCM_CDAR(OBJ) SCM_CDR (SCM_CAR (OBJ))
  62. #define SCM_CADR(OBJ) SCM_CAR (SCM_CDR (OBJ))
  63. #define SCM_CDDR(OBJ) SCM_CDR (SCM_CDR (OBJ))
  64. #define SCM_CAAAR(OBJ) SCM_CAR (SCM_CAR (SCM_CAR (OBJ)))
  65. #define SCM_CDAAR(OBJ) SCM_CDR (SCM_CAR (SCM_CAR (OBJ)))
  66. #define SCM_CADAR(OBJ) SCM_CAR (SCM_CDR (SCM_CAR (OBJ)))
  67. #define SCM_CDDAR(OBJ) SCM_CDR (SCM_CDR (SCM_CAR (OBJ)))
  68. #define SCM_CAADR(OBJ) SCM_CAR (SCM_CAR (SCM_CDR (OBJ)))
  69. #define SCM_CDADR(OBJ) SCM_CDR (SCM_CAR (SCM_CDR (OBJ)))
  70. #define SCM_CADDR(OBJ) SCM_CAR (SCM_CDR (SCM_CDR (OBJ)))
  71. #define SCM_CDDDR(OBJ) SCM_CDR (SCM_CDR (SCM_CDR (OBJ)))
  72. #define SCM_CAAAAR(OBJ) SCM_CAR (SCM_CAR (SCM_CAR (SCM_CAR (OBJ))))
  73. #define SCM_CDAAAR(OBJ) SCM_CDR (SCM_CAR (SCM_CAR (SCM_CAR (OBJ))))
  74. #define SCM_CADAAR(OBJ) SCM_CAR (SCM_CDR (SCM_CAR (SCM_CAR (OBJ))))
  75. #define SCM_CDDAAR(OBJ) SCM_CDR (SCM_CDR (SCM_CAR (SCM_CAR (OBJ))))
  76. #define SCM_CAADAR(OBJ) SCM_CAR (SCM_CAR (SCM_CDR (SCM_CAR (OBJ))))
  77. #define SCM_CDADAR(OBJ) SCM_CDR (SCM_CAR (SCM_CDR (SCM_CAR (OBJ))))
  78. #define SCM_CADDAR(OBJ) SCM_CAR (SCM_CDR (SCM_CDR (SCM_CAR (OBJ))))
  79. #define SCM_CDDDAR(OBJ) SCM_CDR (SCM_CDR (SCM_CDR (SCM_CAR (OBJ))))
  80. #define SCM_CAAADR(OBJ) SCM_CAR (SCM_CAR (SCM_CAR (SCM_CDR (OBJ))))
  81. #define SCM_CDAADR(OBJ) SCM_CDR (SCM_CAR (SCM_CAR (SCM_CDR (OBJ))))
  82. #define SCM_CADADR(OBJ) SCM_CAR (SCM_CDR (SCM_CAR (SCM_CDR (OBJ))))
  83. #define SCM_CDDADR(OBJ) SCM_CDR (SCM_CDR (SCM_CAR (SCM_CDR (OBJ))))
  84. #define SCM_CAADDR(OBJ) SCM_CAR (SCM_CAR (SCM_CDR (SCM_CDR (OBJ))))
  85. #define SCM_CDADDR(OBJ) SCM_CDR (SCM_CAR (SCM_CDR (SCM_CDR (OBJ))))
  86. #define SCM_CADDDR(OBJ) SCM_CAR (SCM_CDR (SCM_CDR (SCM_CDR (OBJ))))
  87. #define SCM_CDDDDR(OBJ) SCM_CDR (SCM_CDR (SCM_CDR (SCM_CDR (OBJ))))
  88. #if (SCM_DEBUG_PAIR_ACCESSES == 1)
  89. SCM_API void scm_error_pair_access (SCM);
  90. #endif
  91. SCM_INLINE int scm_is_pair (SCM x);
  92. SCM_INLINE SCM scm_cons (SCM x, SCM y);
  93. SCM_INLINE SCM scm_car (SCM x);
  94. SCM_INLINE SCM scm_cdr (SCM x);
  95. #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
  96. /* Return a newly allocated pair whose car is @var{x} and whose cdr is
  97. @var{y}. The pair is guaranteed to be different (in the sense of
  98. @code{eq?}) from every previously existing object. */
  99. SCM_INLINE_IMPLEMENTATION SCM
  100. scm_cons (SCM x, SCM y)
  101. {
  102. return scm_cell (SCM_UNPACK (x), SCM_UNPACK (y));
  103. }
  104. SCM_INLINE_IMPLEMENTATION int
  105. scm_is_pair (SCM x)
  106. {
  107. /* The following "workaround_for_gcc_295" avoids bad code generated by
  108. i386 gcc 2.95.4 (the Debian packaged 2.95.4-24 at least).
  109. Under the default -O2 the inlined SCM_I_CONSP test gets "optimized" so
  110. the fetch of the tag word from x is done before confirming it's a
  111. non-immediate (SCM_NIMP). Needless to say that bombs badly if x is a
  112. immediate. This was seen to afflict scm_srfi1_split_at and something
  113. deep in the bowels of ceval(). In both cases segvs resulted from
  114. deferencing a random immediate value. srfi-1.test exposes the problem
  115. through a short list, the immediate being SCM_EOL in that case.
  116. Something in syntax.test exposed the ceval() problem.
  117. Just "volatile SCM workaround_for_gcc_295 = lst" is enough to avoid the
  118. problem, without even using that variable. The "w=w" is just to
  119. prevent a warning about it being unused.
  120. */
  121. #if defined (__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ == 95
  122. volatile SCM workaround_for_gcc_295 = x;
  123. workaround_for_gcc_295 = workaround_for_gcc_295;
  124. #endif
  125. return SCM_I_CONSP (x);
  126. }
  127. SCM_INLINE_IMPLEMENTATION SCM
  128. scm_car (SCM x)
  129. {
  130. if (SCM_UNLIKELY (!scm_is_pair (x)))
  131. scm_wrong_type_arg_msg ("car", 0, x, "pair");
  132. return SCM_CAR (x);
  133. }
  134. SCM_INLINE_IMPLEMENTATION SCM
  135. scm_cdr (SCM x)
  136. {
  137. if (SCM_UNLIKELY (!scm_is_pair (x)))
  138. scm_wrong_type_arg_msg ("cdr", 0, x, "pair");
  139. return SCM_CDR (x);
  140. }
  141. #endif
  142. SCM_API SCM scm_cons2 (SCM w, SCM x, SCM y);
  143. SCM_API SCM scm_pair_p (SCM x);
  144. SCM_API SCM scm_set_car_x (SCM pair, SCM value);
  145. SCM_API SCM scm_set_cdr_x (SCM pair, SCM value);
  146. SCM_API SCM scm_cddr (SCM x);
  147. SCM_API SCM scm_cdar (SCM x);
  148. SCM_API SCM scm_cadr (SCM x);
  149. SCM_API SCM scm_caar (SCM x);
  150. SCM_API SCM scm_cdddr (SCM x);
  151. SCM_API SCM scm_cddar (SCM x);
  152. SCM_API SCM scm_cdadr (SCM x);
  153. SCM_API SCM scm_cdaar (SCM x);
  154. SCM_API SCM scm_caddr (SCM x);
  155. SCM_API SCM scm_cadar (SCM x);
  156. SCM_API SCM scm_caadr (SCM x);
  157. SCM_API SCM scm_caaar (SCM x);
  158. SCM_API SCM scm_cddddr (SCM x);
  159. SCM_API SCM scm_cdddar (SCM x);
  160. SCM_API SCM scm_cddadr (SCM x);
  161. SCM_API SCM scm_cddaar (SCM x);
  162. SCM_API SCM scm_cdaddr (SCM x);
  163. SCM_API SCM scm_cdadar (SCM x);
  164. SCM_API SCM scm_cdaadr (SCM x);
  165. SCM_API SCM scm_cdaaar (SCM x);
  166. SCM_API SCM scm_cadddr (SCM x);
  167. SCM_API SCM scm_caddar (SCM x);
  168. SCM_API SCM scm_cadadr (SCM x);
  169. SCM_API SCM scm_cadaar (SCM x);
  170. SCM_API SCM scm_caaddr (SCM x);
  171. SCM_API SCM scm_caadar (SCM x);
  172. SCM_API SCM scm_caaadr (SCM x);
  173. SCM_API SCM scm_caaaar (SCM x);
  174. SCM_INTERNAL void scm_init_pairs (void);
  175. #endif /* SCM_PAIRS_H */
  176. /*
  177. Local Variables:
  178. c-file-style: "gnu"
  179. End:
  180. */