| 1 | /* classes: h_files */ |
| 2 | |
| 3 | #ifndef SCM_PAIRS_H |
| 4 | #define SCM_PAIRS_H |
| 5 | |
| 6 | /* Copyright (C) 1995,1996,2000,2001, 2004, 2006, 2008, 2009, 2010, 2012 Free Software Foundation, Inc. |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public License |
| 10 | * as published by the Free Software Foundation; either version 3 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | */ |
| 23 | |
| 24 | \f |
| 25 | |
| 26 | #include "libguile/__scm.h" |
| 27 | |
| 28 | #include "libguile/gc.h" |
| 29 | |
| 30 | \f |
| 31 | |
| 32 | #if (SCM_DEBUG_PAIR_ACCESSES == 1) |
| 33 | # define SCM_VALIDATE_PAIR(cell, expr) \ |
| 34 | ((!scm_is_pair (cell) ? scm_error_pair_access (cell), 0 : 0), (expr)) |
| 35 | #else |
| 36 | # define SCM_VALIDATE_PAIR(cell, expr) (expr) |
| 37 | #endif |
| 38 | |
| 39 | /* |
| 40 | * Use scm_is_null_and_not_nil if it's important (for correctness) |
| 41 | * that #nil must NOT be considered null. |
| 42 | */ |
| 43 | #define scm_is_null_and_not_nil(x) (scm_is_eq ((x), SCM_EOL)) |
| 44 | |
| 45 | /* |
| 46 | * Use scm_is_null_assume_not_nil if |
| 47 | #nil will never be tested, |
| 48 | * for increased efficiency. |
| 49 | */ |
| 50 | #define scm_is_null_assume_not_nil(x) (scm_is_eq ((x), SCM_EOL)) |
| 51 | |
| 52 | /* |
| 53 | * See the comments preceeding the definitions of SCM_BOOL_F and |
| 54 | * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information on |
| 55 | * how the following macro works. |
| 56 | */ |
| 57 | #define scm_is_null_or_nil(x) \ |
| 58 | (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_ELISP_NIL, SCM_EOL)) |
| 59 | |
| 60 | |
| 61 | \f |
| 62 | |
| 63 | /* Older spellings for these null, nil, and pair predicates. */ |
| 64 | #define SCM_NILP(x) (scm_is_eq ((x), SCM_ELISP_NIL)) |
| 65 | #define SCM_NULL_OR_NIL_P(x) (scm_is_null_or_nil (x)) |
| 66 | #define SCM_NULLP(x) (scm_is_null (x)) |
| 67 | #define SCM_NNULLP(x) (!scm_is_null (x)) |
| 68 | #define SCM_CONSP(x) (scm_is_pair (x)) |
| 69 | #define SCM_NCONSP(x) (!SCM_CONSP (x)) |
| 70 | |
| 71 | |
| 72 | \f |
| 73 | |
| 74 | /* #nil is null. */ |
| 75 | #define scm_is_null(x) (scm_is_null_or_nil(x)) |
| 76 | |
| 77 | #define SCM_CAR(x) (SCM_VALIDATE_PAIR (x, SCM_CELL_OBJECT_0 (x))) |
| 78 | #define SCM_CDR(x) (SCM_VALIDATE_PAIR (x, SCM_CELL_OBJECT_1 (x))) |
| 79 | |
| 80 | #define SCM_SETCAR(x, v) (SCM_VALIDATE_PAIR (x, SCM_SET_CELL_OBJECT_0 ((x), (v)))) |
| 81 | #define SCM_SETCDR(x, v) (SCM_VALIDATE_PAIR (x, SCM_SET_CELL_OBJECT_1 ((x), (v)))) |
| 82 | |
| 83 | #define SCM_CAAR(OBJ) SCM_CAR (SCM_CAR (OBJ)) |
| 84 | #define SCM_CDAR(OBJ) SCM_CDR (SCM_CAR (OBJ)) |
| 85 | #define SCM_CADR(OBJ) SCM_CAR (SCM_CDR (OBJ)) |
| 86 | #define SCM_CDDR(OBJ) SCM_CDR (SCM_CDR (OBJ)) |
| 87 | |
| 88 | #define SCM_CAAAR(OBJ) SCM_CAR (SCM_CAR (SCM_CAR (OBJ))) |
| 89 | #define SCM_CDAAR(OBJ) SCM_CDR (SCM_CAR (SCM_CAR (OBJ))) |
| 90 | #define SCM_CADAR(OBJ) SCM_CAR (SCM_CDR (SCM_CAR (OBJ))) |
| 91 | #define SCM_CDDAR(OBJ) SCM_CDR (SCM_CDR (SCM_CAR (OBJ))) |
| 92 | #define SCM_CAADR(OBJ) SCM_CAR (SCM_CAR (SCM_CDR (OBJ))) |
| 93 | #define SCM_CDADR(OBJ) SCM_CDR (SCM_CAR (SCM_CDR (OBJ))) |
| 94 | #define SCM_CADDR(OBJ) SCM_CAR (SCM_CDR (SCM_CDR (OBJ))) |
| 95 | #define SCM_CDDDR(OBJ) SCM_CDR (SCM_CDR (SCM_CDR (OBJ))) |
| 96 | |
| 97 | #define SCM_CAAAAR(OBJ) SCM_CAR (SCM_CAR (SCM_CAR (SCM_CAR (OBJ)))) |
| 98 | #define SCM_CDAAAR(OBJ) SCM_CDR (SCM_CAR (SCM_CAR (SCM_CAR (OBJ)))) |
| 99 | #define SCM_CADAAR(OBJ) SCM_CAR (SCM_CDR (SCM_CAR (SCM_CAR (OBJ)))) |
| 100 | #define SCM_CDDAAR(OBJ) SCM_CDR (SCM_CDR (SCM_CAR (SCM_CAR (OBJ)))) |
| 101 | #define SCM_CAADAR(OBJ) SCM_CAR (SCM_CAR (SCM_CDR (SCM_CAR (OBJ)))) |
| 102 | #define SCM_CDADAR(OBJ) SCM_CDR (SCM_CAR (SCM_CDR (SCM_CAR (OBJ)))) |
| 103 | #define SCM_CADDAR(OBJ) SCM_CAR (SCM_CDR (SCM_CDR (SCM_CAR (OBJ)))) |
| 104 | #define SCM_CDDDAR(OBJ) SCM_CDR (SCM_CDR (SCM_CDR (SCM_CAR (OBJ)))) |
| 105 | #define SCM_CAAADR(OBJ) SCM_CAR (SCM_CAR (SCM_CAR (SCM_CDR (OBJ)))) |
| 106 | #define SCM_CDAADR(OBJ) SCM_CDR (SCM_CAR (SCM_CAR (SCM_CDR (OBJ)))) |
| 107 | #define SCM_CADADR(OBJ) SCM_CAR (SCM_CDR (SCM_CAR (SCM_CDR (OBJ)))) |
| 108 | #define SCM_CDDADR(OBJ) SCM_CDR (SCM_CDR (SCM_CAR (SCM_CDR (OBJ)))) |
| 109 | #define SCM_CAADDR(OBJ) SCM_CAR (SCM_CAR (SCM_CDR (SCM_CDR (OBJ)))) |
| 110 | #define SCM_CDADDR(OBJ) SCM_CDR (SCM_CAR (SCM_CDR (SCM_CDR (OBJ)))) |
| 111 | #define SCM_CADDDR(OBJ) SCM_CAR (SCM_CDR (SCM_CDR (SCM_CDR (OBJ)))) |
| 112 | #define SCM_CDDDDR(OBJ) SCM_CDR (SCM_CDR (SCM_CDR (SCM_CDR (OBJ)))) |
| 113 | |
| 114 | \f |
| 115 | |
| 116 | #if (SCM_DEBUG_PAIR_ACCESSES == 1) |
| 117 | SCM_API void scm_error_pair_access (SCM); |
| 118 | #endif |
| 119 | |
| 120 | SCM_INLINE int scm_is_pair (SCM x); |
| 121 | SCM_INLINE SCM scm_cons (SCM x, SCM y); |
| 122 | SCM_INLINE SCM scm_car (SCM x); |
| 123 | SCM_INLINE SCM scm_cdr (SCM x); |
| 124 | |
| 125 | #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES |
| 126 | /* Return a newly allocated pair whose car is @var{x} and whose cdr is |
| 127 | @var{y}. The pair is guaranteed to be different (in the sense of |
| 128 | @code{eq?}) from every previously existing object. */ |
| 129 | SCM_INLINE_IMPLEMENTATION SCM |
| 130 | scm_cons (SCM x, SCM y) |
| 131 | { |
| 132 | return scm_cell (SCM_UNPACK (x), SCM_UNPACK (y)); |
| 133 | } |
| 134 | |
| 135 | SCM_INLINE_IMPLEMENTATION int |
| 136 | scm_is_pair (SCM x) |
| 137 | { |
| 138 | /* The following "workaround_for_gcc_295" avoids bad code generated by |
| 139 | i386 gcc 2.95.4 (the Debian packaged 2.95.4-24 at least). |
| 140 | |
| 141 | Under the default -O2 the inlined SCM_I_CONSP test gets "optimized" so |
| 142 | the fetch of the tag word from x is done before confirming it's a |
| 143 | non-immediate (SCM_NIMP). Needless to say that bombs badly if x is a |
| 144 | immediate. This was seen to afflict scm_srfi1_split_at and something |
| 145 | deep in the bowels of ceval(). In both cases segvs resulted from |
| 146 | deferencing a random immediate value. srfi-1.test exposes the problem |
| 147 | through a short list, the immediate being SCM_EOL in that case. |
| 148 | Something in syntax.test exposed the ceval() problem. |
| 149 | |
| 150 | Just "volatile SCM workaround_for_gcc_295 = lst" is enough to avoid the |
| 151 | problem, without even using that variable. The "w=w" is just to |
| 152 | prevent a warning about it being unused. |
| 153 | */ |
| 154 | #if defined (__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ == 95 |
| 155 | volatile SCM workaround_for_gcc_295 = x; |
| 156 | workaround_for_gcc_295 = workaround_for_gcc_295; |
| 157 | #endif |
| 158 | |
| 159 | return SCM_I_CONSP (x); |
| 160 | } |
| 161 | |
| 162 | SCM_INLINE_IMPLEMENTATION SCM |
| 163 | scm_car (SCM x) |
| 164 | { |
| 165 | if (SCM_UNLIKELY (!scm_is_pair (x))) |
| 166 | scm_wrong_type_arg_msg ("car", 0, x, "pair"); |
| 167 | return SCM_CAR (x); |
| 168 | } |
| 169 | |
| 170 | SCM_INLINE_IMPLEMENTATION SCM |
| 171 | scm_cdr (SCM x) |
| 172 | { |
| 173 | if (SCM_UNLIKELY (!scm_is_pair (x))) |
| 174 | scm_wrong_type_arg_msg ("cdr", 0, x, "pair"); |
| 175 | return SCM_CDR (x); |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | SCM_API SCM scm_cons2 (SCM w, SCM x, SCM y); |
| 180 | SCM_API SCM scm_pair_p (SCM x); |
| 181 | SCM_API SCM scm_set_car_x (SCM pair, SCM value); |
| 182 | SCM_API SCM scm_set_cdr_x (SCM pair, SCM value); |
| 183 | |
| 184 | SCM_API SCM scm_cddr (SCM x); |
| 185 | SCM_API SCM scm_cdar (SCM x); |
| 186 | SCM_API SCM scm_cadr (SCM x); |
| 187 | SCM_API SCM scm_caar (SCM x); |
| 188 | SCM_API SCM scm_cdddr (SCM x); |
| 189 | SCM_API SCM scm_cddar (SCM x); |
| 190 | SCM_API SCM scm_cdadr (SCM x); |
| 191 | SCM_API SCM scm_cdaar (SCM x); |
| 192 | SCM_API SCM scm_caddr (SCM x); |
| 193 | SCM_API SCM scm_cadar (SCM x); |
| 194 | SCM_API SCM scm_caadr (SCM x); |
| 195 | SCM_API SCM scm_caaar (SCM x); |
| 196 | SCM_API SCM scm_cddddr (SCM x); |
| 197 | SCM_API SCM scm_cdddar (SCM x); |
| 198 | SCM_API SCM scm_cddadr (SCM x); |
| 199 | SCM_API SCM scm_cddaar (SCM x); |
| 200 | SCM_API SCM scm_cdaddr (SCM x); |
| 201 | SCM_API SCM scm_cdadar (SCM x); |
| 202 | SCM_API SCM scm_cdaadr (SCM x); |
| 203 | SCM_API SCM scm_cdaaar (SCM x); |
| 204 | SCM_API SCM scm_cadddr (SCM x); |
| 205 | SCM_API SCM scm_caddar (SCM x); |
| 206 | SCM_API SCM scm_cadadr (SCM x); |
| 207 | SCM_API SCM scm_cadaar (SCM x); |
| 208 | SCM_API SCM scm_caaddr (SCM x); |
| 209 | SCM_API SCM scm_caadar (SCM x); |
| 210 | SCM_API SCM scm_caaadr (SCM x); |
| 211 | SCM_API SCM scm_caaaar (SCM x); |
| 212 | |
| 213 | SCM_INTERNAL void scm_init_pairs (void); |
| 214 | |
| 215 | #endif /* SCM_PAIRS_H */ |
| 216 | |
| 217 | /* |
| 218 | Local Variables: |
| 219 | c-file-style: "gnu" |
| 220 | End: |
| 221 | */ |