| 1 | /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2006, |
| 2 | * 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public License |
| 6 | * as published by the Free Software Foundation; either version 3 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 | * 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | \f |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | # include <config.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <errno.h> |
| 27 | |
| 28 | #include "libguile/_scm.h" |
| 29 | #include "libguile/async.h" |
| 30 | #include "libguile/smob.h" |
| 31 | #include "libguile/alist.h" |
| 32 | #include "libguile/debug.h" |
| 33 | #include "libguile/hashtab.h" |
| 34 | #include "libguile/hash.h" |
| 35 | #include "libguile/ports.h" |
| 36 | #include "libguile/root.h" |
| 37 | #include "libguile/gc.h" |
| 38 | |
| 39 | #include "libguile/validate.h" |
| 40 | #include "libguile/srcprop.h" |
| 41 | #include "libguile/private-options.h" |
| 42 | |
| 43 | \f |
| 44 | /* {Source Properties} |
| 45 | * |
| 46 | * Properties of source list expressions. |
| 47 | * Four of these have special meaning: |
| 48 | * |
| 49 | * filename string The name of the source file. |
| 50 | * copy list A copy of the list expression. |
| 51 | * line integer The source code line number. |
| 52 | * column integer The source code column number. |
| 53 | * |
| 54 | * Most properties above can be set by the reader. |
| 55 | * |
| 56 | */ |
| 57 | |
| 58 | SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename"); |
| 59 | SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy"); |
| 60 | SCM_GLOBAL_SYMBOL (scm_sym_line, "line"); |
| 61 | SCM_GLOBAL_SYMBOL (scm_sym_column, "column"); |
| 62 | |
| 63 | static SCM scm_source_whash; |
| 64 | |
| 65 | |
| 66 | /* |
| 67 | * Source properties are stored as double cells with the |
| 68 | * following layout: |
| 69 | |
| 70 | * car = tag |
| 71 | * cbr = pos |
| 72 | * ccr = copy |
| 73 | * cdr = alist |
| 74 | */ |
| 75 | |
| 76 | #define SRCPROPSP(p) (SCM_SMOB_PREDICATE (scm_tc16_srcprops, (p))) |
| 77 | #define SRCPROPPOS(p) (SCM_SMOB_DATA(p)) |
| 78 | #define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12) |
| 79 | #define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL) |
| 80 | #define SRCPROPCOPY(p) (SCM_SMOB_OBJECT_2(p)) |
| 81 | #define SRCPROPALIST(p) (SCM_SMOB_OBJECT_3(p)) |
| 82 | #define SRCPROPMAKPOS(l, c) (((l) << 12) + (c)) |
| 83 | #define SETSRCPROPPOS(p, l, c) (SCM_SET_SMOB_DATA_1 (p, SRCPROPMAKPOS (l, c))) |
| 84 | #define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p)) |
| 85 | #define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c) |
| 86 | #define SETSRCPROPCOPY(p, c) (SCM_SET_SMOB_OBJECT_2 (p, c)) |
| 87 | #define SETSRCPROPALIST(p, l) (SCM_SET_SMOB_OBJECT_3 (p, l)) |
| 88 | |
| 89 | |
| 90 | static SCM scm_srcprops_to_alist (SCM obj); |
| 91 | |
| 92 | |
| 93 | scm_t_bits scm_tc16_srcprops; |
| 94 | |
| 95 | |
| 96 | static int |
| 97 | supports_source_props (SCM obj) |
| 98 | { |
| 99 | return SCM_NIMP (obj) && !scm_is_symbol (obj) && !scm_is_keyword (obj); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | static int |
| 104 | srcprops_print (SCM obj, SCM port, scm_print_state *pstate) |
| 105 | { |
| 106 | int writingp = SCM_WRITINGP (pstate); |
| 107 | scm_puts_unlocked ("#<srcprops ", port); |
| 108 | SCM_SET_WRITINGP (pstate, 1); |
| 109 | scm_iprin1 (scm_srcprops_to_alist (obj), port, pstate); |
| 110 | SCM_SET_WRITINGP (pstate, writingp); |
| 111 | scm_putc_unlocked ('>', port); |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* |
| 117 | * We remember the last file name settings, so we can share that alist |
| 118 | * entry. This works because scm_set_source_property_x does not use |
| 119 | * assoc-set! for modifying the alist. |
| 120 | * |
| 121 | * This variable contains a protected cons, whose cdr is the cached |
| 122 | * alist |
| 123 | */ |
| 124 | static SCM scm_last_alist_filename; |
| 125 | |
| 126 | SCM |
| 127 | scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist) |
| 128 | { |
| 129 | if (!SCM_UNBNDP (filename)) |
| 130 | { |
| 131 | SCM old_alist = alist; |
| 132 | |
| 133 | /* |
| 134 | have to extract the acons, and operate on that, for |
| 135 | thread safety. |
| 136 | */ |
| 137 | SCM last_acons = SCM_CDR (scm_last_alist_filename); |
| 138 | if (scm_is_null (old_alist) |
| 139 | && scm_is_eq (SCM_CDAR (last_acons), filename)) |
| 140 | { |
| 141 | alist = last_acons; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | alist = scm_acons (scm_sym_filename, filename, alist); |
| 146 | if (scm_is_null (old_alist)) |
| 147 | SCM_SETCDR (scm_last_alist_filename, alist); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops, |
| 152 | SRCPROPMAKPOS (line, col), |
| 153 | SCM_UNPACK (copy), |
| 154 | SCM_UNPACK (alist)); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | static SCM |
| 159 | scm_srcprops_to_alist (SCM obj) |
| 160 | { |
| 161 | SCM alist = SRCPROPALIST (obj); |
| 162 | if (!SCM_UNBNDP (SRCPROPCOPY (obj))) |
| 163 | alist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), alist); |
| 164 | alist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), alist); |
| 165 | alist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), alist); |
| 166 | return alist; |
| 167 | } |
| 168 | |
| 169 | SCM_DEFINE (scm_supports_source_properties_p, "supports-source-properties?", 1, 0, 0, |
| 170 | (SCM obj), |
| 171 | "Return #t if @var{obj} supports adding source properties,\n" |
| 172 | "otherwise return #f.") |
| 173 | #define FUNC_NAME s_scm_supports_source_properties_p |
| 174 | { |
| 175 | return scm_from_bool (supports_source_props (obj)); |
| 176 | } |
| 177 | #undef FUNC_NAME |
| 178 | |
| 179 | SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0, |
| 180 | (SCM obj), |
| 181 | "Return the source property association list of @var{obj}.") |
| 182 | #define FUNC_NAME s_scm_source_properties |
| 183 | { |
| 184 | if (SCM_IMP (obj)) |
| 185 | return SCM_EOL; |
| 186 | else |
| 187 | { |
| 188 | SCM p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL); |
| 189 | |
| 190 | if (SRCPROPSP (p)) |
| 191 | return scm_srcprops_to_alist (p); |
| 192 | else |
| 193 | /* list from set-source-properties!, or SCM_EOL for not found */ |
| 194 | return p; |
| 195 | } |
| 196 | } |
| 197 | #undef FUNC_NAME |
| 198 | |
| 199 | /* Perhaps this procedure should look through an alist |
| 200 | and try to make a srcprops-object...? */ |
| 201 | SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0, |
| 202 | (SCM obj, SCM alist), |
| 203 | "Install the association list @var{alist} as the source property\n" |
| 204 | "list for @var{obj}.") |
| 205 | #define FUNC_NAME s_scm_set_source_properties_x |
| 206 | { |
| 207 | SCM_VALIDATE_NIM (1, obj); |
| 208 | |
| 209 | scm_weak_table_putq_x (scm_source_whash, obj, alist); |
| 210 | |
| 211 | return alist; |
| 212 | } |
| 213 | #undef FUNC_NAME |
| 214 | |
| 215 | int |
| 216 | scm_i_has_source_properties (SCM obj) |
| 217 | #define FUNC_NAME "%set-source-properties" |
| 218 | { |
| 219 | if (SCM_IMP (obj)) |
| 220 | return 0; |
| 221 | else |
| 222 | return scm_is_true (scm_weak_table_refq (scm_source_whash, obj, SCM_BOOL_F)); |
| 223 | } |
| 224 | #undef FUNC_NAME |
| 225 | |
| 226 | |
| 227 | void |
| 228 | scm_i_set_source_properties_x (SCM obj, long line, int col, SCM fname) |
| 229 | #define FUNC_NAME "%set-source-properties" |
| 230 | { |
| 231 | SCM_VALIDATE_NIM (1, obj); |
| 232 | |
| 233 | scm_weak_table_putq_x (scm_source_whash, obj, |
| 234 | scm_make_srcprops (line, col, fname, |
| 235 | SCM_COPY_SOURCE_P |
| 236 | ? scm_copy_tree (obj) |
| 237 | : SCM_UNDEFINED, |
| 238 | SCM_EOL)); |
| 239 | } |
| 240 | #undef FUNC_NAME |
| 241 | |
| 242 | SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0, |
| 243 | (SCM obj, SCM key), |
| 244 | "Return the source property specified by @var{key} from\n" |
| 245 | "@var{obj}'s source property list.") |
| 246 | #define FUNC_NAME s_scm_source_property |
| 247 | { |
| 248 | SCM p; |
| 249 | |
| 250 | if (SCM_IMP (obj)) |
| 251 | return SCM_BOOL_F; |
| 252 | |
| 253 | p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL); |
| 254 | |
| 255 | if (!SRCPROPSP (p)) |
| 256 | goto alist; |
| 257 | if (scm_is_eq (scm_sym_line, key)) |
| 258 | return scm_from_int (SRCPROPLINE (p)); |
| 259 | else if (scm_is_eq (scm_sym_column, key)) |
| 260 | return scm_from_int (SRCPROPCOL (p)); |
| 261 | else if (scm_is_eq (scm_sym_copy, key)) |
| 262 | return SRCPROPCOPY (p); |
| 263 | else |
| 264 | { |
| 265 | p = SRCPROPALIST (p); |
| 266 | alist: |
| 267 | p = scm_assoc (key, p); |
| 268 | return (scm_is_pair (p) ? SCM_CDR (p) : SCM_BOOL_F); |
| 269 | } |
| 270 | } |
| 271 | #undef FUNC_NAME |
| 272 | |
| 273 | SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0, |
| 274 | (SCM obj, SCM key, SCM datum), |
| 275 | "Set the source property of object @var{obj}, which is specified by\n" |
| 276 | "@var{key} to @var{datum}. Normally, the key will be a symbol.") |
| 277 | #define FUNC_NAME s_scm_set_source_property_x |
| 278 | { |
| 279 | SCM p; |
| 280 | SCM_VALIDATE_NIM (1, obj); |
| 281 | |
| 282 | scm_i_pthread_mutex_lock (&scm_i_misc_mutex); |
| 283 | p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL); |
| 284 | |
| 285 | if (scm_is_eq (scm_sym_line, key)) |
| 286 | { |
| 287 | if (SRCPROPSP (p)) |
| 288 | SETSRCPROPLINE (p, scm_to_int (datum)); |
| 289 | else |
| 290 | scm_weak_table_putq_x (scm_source_whash, obj, |
| 291 | scm_make_srcprops (scm_to_int (datum), 0, |
| 292 | SCM_UNDEFINED, SCM_UNDEFINED, p)); |
| 293 | } |
| 294 | else if (scm_is_eq (scm_sym_column, key)) |
| 295 | { |
| 296 | if (SRCPROPSP (p)) |
| 297 | SETSRCPROPCOL (p, scm_to_int (datum)); |
| 298 | else |
| 299 | scm_weak_table_putq_x (scm_source_whash, obj, |
| 300 | scm_make_srcprops (0, scm_to_int (datum), |
| 301 | SCM_UNDEFINED, SCM_UNDEFINED, p)); |
| 302 | } |
| 303 | else if (scm_is_eq (scm_sym_copy, key)) |
| 304 | { |
| 305 | if (SRCPROPSP (p)) |
| 306 | SETSRCPROPCOPY (p, datum); |
| 307 | else |
| 308 | scm_weak_table_putq_x (scm_source_whash, obj, |
| 309 | scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p)); |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | if (SRCPROPSP (p)) |
| 314 | SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p))); |
| 315 | else |
| 316 | scm_weak_table_putq_x (scm_source_whash, obj, |
| 317 | scm_acons (key, datum, p)); |
| 318 | } |
| 319 | scm_i_pthread_mutex_unlock (&scm_i_misc_mutex); |
| 320 | |
| 321 | return SCM_UNSPECIFIED; |
| 322 | } |
| 323 | #undef FUNC_NAME |
| 324 | |
| 325 | |
| 326 | SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0, |
| 327 | (SCM xorig, SCM x, SCM y), |
| 328 | "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n" |
| 329 | "Any source properties associated with @var{xorig} are also associated\n" |
| 330 | "with the new pair.") |
| 331 | #define FUNC_NAME s_scm_cons_source |
| 332 | { |
| 333 | SCM p, z; |
| 334 | z = scm_cons (x, y); |
| 335 | /* Copy source properties possibly associated with xorig. */ |
| 336 | p = scm_weak_table_refq (scm_source_whash, xorig, SCM_BOOL_F); |
| 337 | if (scm_is_true (p)) |
| 338 | scm_weak_table_putq_x (scm_source_whash, z, p); |
| 339 | return z; |
| 340 | } |
| 341 | #undef FUNC_NAME |
| 342 | |
| 343 | |
| 344 | void |
| 345 | scm_init_srcprop () |
| 346 | { |
| 347 | scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0); |
| 348 | scm_set_smob_print (scm_tc16_srcprops, srcprops_print); |
| 349 | |
| 350 | scm_source_whash = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY); |
| 351 | scm_c_define ("source-whash", scm_source_whash); |
| 352 | |
| 353 | scm_last_alist_filename = scm_cons (SCM_EOL, |
| 354 | scm_acons (SCM_EOL, SCM_EOL, SCM_EOL)); |
| 355 | |
| 356 | #include "libguile/srcprop.x" |
| 357 | } |
| 358 | |
| 359 | |
| 360 | /* |
| 361 | Local Variables: |
| 362 | c-file-style: "gnu" |
| 363 | End: |
| 364 | */ |