| 1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006 Free Software Foundation |
| 2 | * |
| 3 | * This library is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU Lesser General Public |
| 5 | * License as published by the Free Software Foundation; either |
| 6 | * version 2.1 of the License, or (at your option) any later version. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public |
| 14 | * License along with this library; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | \f |
| 20 | |
| 21 | #include <errno.h> |
| 22 | |
| 23 | #include "libguile/_scm.h" |
| 24 | #include "libguile/async.h" |
| 25 | #include "libguile/smob.h" |
| 26 | #include "libguile/alist.h" |
| 27 | #include "libguile/debug.h" |
| 28 | #include "libguile/hashtab.h" |
| 29 | #include "libguile/hash.h" |
| 30 | #include "libguile/ports.h" |
| 31 | #include "libguile/root.h" |
| 32 | #include "libguile/weaks.h" |
| 33 | |
| 34 | #include "libguile/validate.h" |
| 35 | #include "libguile/srcprop.h" |
| 36 | \f |
| 37 | /* {Source Properties} |
| 38 | * |
| 39 | * Properties of source list expressions. |
| 40 | * Five of these have special meaning and optimized storage: |
| 41 | * |
| 42 | * filename string The name of the source file. |
| 43 | * copy list A copy of the list expression. |
| 44 | * line integer The source code line number. |
| 45 | * column integer The source code column number. |
| 46 | * breakpoint boolean Sets a breakpoint on this form. |
| 47 | * |
| 48 | * Most properties above can be set by the reader. |
| 49 | * |
| 50 | */ |
| 51 | |
| 52 | SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename"); |
| 53 | SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy"); |
| 54 | SCM_GLOBAL_SYMBOL (scm_sym_line, "line"); |
| 55 | SCM_GLOBAL_SYMBOL (scm_sym_column, "column"); |
| 56 | SCM_GLOBAL_SYMBOL (scm_sym_breakpoint, "breakpoint"); |
| 57 | |
| 58 | scm_t_bits scm_tc16_srcprops; |
| 59 | static scm_t_srcprops_chunk *srcprops_chunklist = 0; |
| 60 | static scm_t_srcprops *srcprops_freelist = 0; |
| 61 | |
| 62 | |
| 63 | |
| 64 | static size_t |
| 65 | srcprops_free (SCM obj) |
| 66 | { |
| 67 | *((scm_t_srcprops **) SCM_SMOB_DATA (obj)) = srcprops_freelist; |
| 68 | srcprops_freelist = (scm_t_srcprops *) SCM_SMOB_DATA (obj); |
| 69 | return 0; /* srcprops_chunks are not freed until leaving guile */ |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static int |
| 74 | srcprops_print (SCM obj, SCM port, scm_print_state *pstate) |
| 75 | { |
| 76 | int writingp = SCM_WRITINGP (pstate); |
| 77 | scm_puts ("#<srcprops ", port); |
| 78 | SCM_SET_WRITINGP (pstate, 1); |
| 79 | scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate); |
| 80 | SCM_SET_WRITINGP (pstate, writingp); |
| 81 | scm_putc ('>', port); |
| 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | int |
| 87 | scm_c_source_property_breakpoint_p (SCM form) |
| 88 | { |
| 89 | SCM obj = scm_whash_lookup (scm_source_whash, form); |
| 90 | return SRCPROPSP (obj) && SRCPROPBRK (obj); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | SCM |
| 95 | scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM plist) |
| 96 | { |
| 97 | register scm_t_srcprops *ptr; |
| 98 | SCM_CRITICAL_SECTION_START; |
| 99 | if ((ptr = srcprops_freelist) != NULL) |
| 100 | srcprops_freelist = *(scm_t_srcprops **)ptr; |
| 101 | else |
| 102 | { |
| 103 | size_t i; |
| 104 | scm_t_srcprops_chunk *mem; |
| 105 | size_t n = sizeof (scm_t_srcprops_chunk) |
| 106 | + sizeof (scm_t_srcprops) * (SRCPROPS_CHUNKSIZE - 1); |
| 107 | mem = scm_gc_malloc (n, "srcprops"); |
| 108 | if (mem == NULL) |
| 109 | scm_memory_error ("srcprops"); |
| 110 | |
| 111 | mem->next = srcprops_chunklist; |
| 112 | srcprops_chunklist = mem; |
| 113 | ptr = &mem->srcprops[0]; |
| 114 | for (i = 1; i < SRCPROPS_CHUNKSIZE - 1; ++i) |
| 115 | *(scm_t_srcprops **)&ptr[i] = &ptr[i + 1]; |
| 116 | *(scm_t_srcprops **)&ptr[SRCPROPS_CHUNKSIZE - 1] = 0; |
| 117 | srcprops_freelist = (scm_t_srcprops *) &ptr[1]; |
| 118 | } |
| 119 | ptr->pos = SRCPROPMAKPOS (line, col); |
| 120 | ptr->fname = filename; |
| 121 | ptr->copy = copy; |
| 122 | ptr->plist = plist; |
| 123 | SCM_CRITICAL_SECTION_END; |
| 124 | SCM_RETURN_NEWSMOB (scm_tc16_srcprops, ptr); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | SCM |
| 129 | scm_srcprops_to_plist (SCM obj) |
| 130 | { |
| 131 | SCM plist = SRCPROPPLIST (obj); |
| 132 | if (!SCM_UNBNDP (SRCPROPCOPY (obj))) |
| 133 | plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist); |
| 134 | if (!SCM_UNBNDP (SRCPROPFNAME (obj))) |
| 135 | plist = scm_acons (scm_sym_filename, SRCPROPFNAME (obj), plist); |
| 136 | plist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), plist); |
| 137 | plist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), plist); |
| 138 | plist = scm_acons (scm_sym_breakpoint, scm_from_bool (SRCPROPBRK (obj)), plist); |
| 139 | return plist; |
| 140 | } |
| 141 | |
| 142 | SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0, |
| 143 | (SCM obj), |
| 144 | "Return the source property association list of @var{obj}.") |
| 145 | #define FUNC_NAME s_scm_source_properties |
| 146 | { |
| 147 | SCM p; |
| 148 | SCM_VALIDATE_NIM (1, obj); |
| 149 | if (SCM_MEMOIZEDP (obj)) |
| 150 | obj = SCM_MEMOIZED_EXP (obj); |
| 151 | else if (!scm_is_pair (obj)) |
| 152 | SCM_WRONG_TYPE_ARG (1, obj); |
| 153 | p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); |
| 154 | if (SRCPROPSP (p)) |
| 155 | return scm_srcprops_to_plist (p); |
| 156 | else |
| 157 | /* list from set-source-properties!, or SCM_EOL for not found */ |
| 158 | return p; |
| 159 | } |
| 160 | #undef FUNC_NAME |
| 161 | |
| 162 | /* Perhaps this procedure should look through an alist |
| 163 | and try to make a srcprops-object...? */ |
| 164 | SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0, |
| 165 | (SCM obj, SCM plist), |
| 166 | "Install the association list @var{plist} as the source property\n" |
| 167 | "list for @var{obj}.") |
| 168 | #define FUNC_NAME s_scm_set_source_properties_x |
| 169 | { |
| 170 | SCM handle; |
| 171 | SCM_VALIDATE_NIM (1, obj); |
| 172 | if (SCM_MEMOIZEDP (obj)) |
| 173 | obj = SCM_MEMOIZED_EXP (obj); |
| 174 | else if (!scm_is_pair (obj)) |
| 175 | SCM_WRONG_TYPE_ARG(1, obj); |
| 176 | handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist); |
| 177 | SCM_SETCDR (handle, plist); |
| 178 | return plist; |
| 179 | } |
| 180 | #undef FUNC_NAME |
| 181 | |
| 182 | SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0, |
| 183 | (SCM obj, SCM key), |
| 184 | "Return the source property specified by @var{key} from\n" |
| 185 | "@var{obj}'s source property list.") |
| 186 | #define FUNC_NAME s_scm_source_property |
| 187 | { |
| 188 | SCM p; |
| 189 | SCM_VALIDATE_NIM (1, obj); |
| 190 | if (SCM_MEMOIZEDP (obj)) |
| 191 | obj = SCM_MEMOIZED_EXP (obj); |
| 192 | else if (!scm_is_pair (obj)) |
| 193 | SCM_WRONG_TYPE_ARG (1, obj); |
| 194 | p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); |
| 195 | if (!SRCPROPSP (p)) |
| 196 | goto plist; |
| 197 | if (scm_is_eq (scm_sym_breakpoint, key)) p = scm_from_bool (SRCPROPBRK (p)); |
| 198 | else if (scm_is_eq (scm_sym_line, key)) p = scm_from_int (SRCPROPLINE (p)); |
| 199 | else if (scm_is_eq (scm_sym_column, key)) p = scm_from_int (SRCPROPCOL (p)); |
| 200 | else if (scm_is_eq (scm_sym_filename, key)) p = SRCPROPFNAME (p); |
| 201 | else if (scm_is_eq (scm_sym_copy, key)) p = SRCPROPCOPY (p); |
| 202 | else |
| 203 | { |
| 204 | p = SRCPROPPLIST (p); |
| 205 | plist: |
| 206 | p = scm_assoc (key, p); |
| 207 | return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F); |
| 208 | } |
| 209 | return SCM_UNBNDP (p) ? SCM_BOOL_F : p; |
| 210 | } |
| 211 | #undef FUNC_NAME |
| 212 | |
| 213 | SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0, |
| 214 | (SCM obj, SCM key, SCM datum), |
| 215 | "Set the source property of object @var{obj}, which is specified by\n" |
| 216 | "@var{key} to @var{datum}. Normally, the key will be a symbol.") |
| 217 | #define FUNC_NAME s_scm_set_source_property_x |
| 218 | { |
| 219 | scm_whash_handle h; |
| 220 | SCM p; |
| 221 | SCM_VALIDATE_NIM (1, obj); |
| 222 | if (SCM_MEMOIZEDP (obj)) |
| 223 | obj = SCM_MEMOIZED_EXP (obj); |
| 224 | else if (!scm_is_pair (obj)) |
| 225 | SCM_WRONG_TYPE_ARG (1, obj); |
| 226 | h = scm_whash_get_handle (scm_source_whash, obj); |
| 227 | if (SCM_WHASHFOUNDP (h)) |
| 228 | p = SCM_WHASHREF (scm_source_whash, h); |
| 229 | else |
| 230 | { |
| 231 | h = scm_whash_create_handle (scm_source_whash, obj); |
| 232 | p = SCM_EOL; |
| 233 | } |
| 234 | if (scm_is_eq (scm_sym_breakpoint, key)) |
| 235 | { |
| 236 | if (SRCPROPSP (p)) |
| 237 | { |
| 238 | if (scm_is_false (datum)) |
| 239 | CLEARSRCPROPBRK (p); |
| 240 | else |
| 241 | SETSRCPROPBRK (p); |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | SCM sp = scm_make_srcprops (0, 0, SCM_UNDEFINED, SCM_UNDEFINED, p); |
| 246 | SCM_WHASHSET (scm_source_whash, h, sp); |
| 247 | if (scm_is_false (datum)) |
| 248 | CLEARSRCPROPBRK (sp); |
| 249 | else |
| 250 | SETSRCPROPBRK (sp); |
| 251 | } |
| 252 | } |
| 253 | else if (scm_is_eq (scm_sym_line, key)) |
| 254 | { |
| 255 | if (SRCPROPSP (p)) |
| 256 | SETSRCPROPLINE (p, scm_to_int (datum)); |
| 257 | else |
| 258 | SCM_WHASHSET (scm_source_whash, h, |
| 259 | scm_make_srcprops (scm_to_int (datum), 0, |
| 260 | SCM_UNDEFINED, SCM_UNDEFINED, p)); |
| 261 | } |
| 262 | else if (scm_is_eq (scm_sym_column, key)) |
| 263 | { |
| 264 | if (SRCPROPSP (p)) |
| 265 | SETSRCPROPCOL (p, scm_to_int (datum)); |
| 266 | else |
| 267 | SCM_WHASHSET (scm_source_whash, h, |
| 268 | scm_make_srcprops (0, scm_to_int (datum), |
| 269 | SCM_UNDEFINED, SCM_UNDEFINED, p)); |
| 270 | } |
| 271 | else if (scm_is_eq (scm_sym_filename, key)) |
| 272 | { |
| 273 | if (SRCPROPSP (p)) |
| 274 | SRCPROPFNAME (p) = datum; |
| 275 | else |
| 276 | SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p)); |
| 277 | } |
| 278 | else if (scm_is_eq (scm_sym_copy, key)) |
| 279 | { |
| 280 | if (SRCPROPSP (p)) |
| 281 | SRCPROPCOPY (p) = datum; |
| 282 | else |
| 283 | SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p)); |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | if (SRCPROPSP (p)) |
| 288 | SRCPROPPLIST (p) = scm_acons (key, datum, SRCPROPPLIST (p)); |
| 289 | else |
| 290 | SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p)); |
| 291 | } |
| 292 | return SCM_UNSPECIFIED; |
| 293 | } |
| 294 | #undef FUNC_NAME |
| 295 | |
| 296 | |
| 297 | void |
| 298 | scm_init_srcprop () |
| 299 | { |
| 300 | scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0); |
| 301 | scm_set_smob_free (scm_tc16_srcprops, srcprops_free); |
| 302 | scm_set_smob_print (scm_tc16_srcprops, srcprops_print); |
| 303 | |
| 304 | scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047)); |
| 305 | scm_c_define ("source-whash", scm_source_whash); |
| 306 | |
| 307 | #include "libguile/srcprop.x" |
| 308 | } |
| 309 | |
| 310 | void |
| 311 | scm_finish_srcprop () |
| 312 | { |
| 313 | register scm_t_srcprops_chunk *ptr = srcprops_chunklist, *next; |
| 314 | size_t n= sizeof (scm_t_srcprops_chunk) |
| 315 | + sizeof (scm_t_srcprops) * (SRCPROPS_CHUNKSIZE - 1); |
| 316 | while (ptr) |
| 317 | { |
| 318 | next = ptr->next; |
| 319 | scm_gc_unregister_collectable_memory (ptr, n, "srcprops"); |
| 320 | free ((char *) ptr); |
| 321 | ptr = next; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | Local Variables: |
| 327 | c-file-style: "gnu" |
| 328 | End: |
| 329 | */ |