| 1 | /* Copyright (C) 1995,1996 Mikael Djurfeldt |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2, or (at your option) |
| 6 | * any later version. |
| 7 | * |
| 8 | * This program 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 |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; see the file COPYING. If not, write to |
| 15 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 16 | * |
| 17 | * As a special exception, the Free Software Foundation gives permission |
| 18 | * for additional uses of the text contained in its release of GUILE. |
| 19 | * |
| 20 | * The exception is that, if you link the GUILE library with other files |
| 21 | * to produce an executable, this does not by itself cause the |
| 22 | * resulting executable to be covered by the GNU General Public License. |
| 23 | * Your use of that executable is in no way restricted on account of |
| 24 | * linking the GUILE library code into it. |
| 25 | * |
| 26 | * This exception does not however invalidate any other reasons why |
| 27 | * the executable file might be covered by the GNU General Public License. |
| 28 | * |
| 29 | * This exception applies only to the code released by the |
| 30 | * Free Software Foundation under the name GUILE. If you copy |
| 31 | * code from other Free Software Foundation releases into a copy of |
| 32 | * GUILE, as the General Public License permits, the exception does |
| 33 | * not apply to the code that you add in this way. To avoid misleading |
| 34 | * anyone as to the status of such modified files, you must delete |
| 35 | * this exception notice from them. |
| 36 | * |
| 37 | * If you write modifications of your own for GUILE, it is your choice |
| 38 | * whether to permit this exception to apply to your modifications. |
| 39 | * If you do not wish that, delete this exception notice. |
| 40 | * |
| 41 | * The author can be reached at djurfeldt@nada.kth.se |
| 42 | * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN |
| 43 | */ |
| 44 | \f |
| 45 | |
| 46 | #include <stdio.h> |
| 47 | #include "_scm.h" |
| 48 | #include "smob.h" |
| 49 | #include "alist.h" |
| 50 | #include "debug.h" |
| 51 | #include "hashtab.h" |
| 52 | #include "hash.h" |
| 53 | #include "weaks.h" |
| 54 | |
| 55 | #include "srcprop.h" |
| 56 | \f |
| 57 | /* {Source Properties} |
| 58 | * |
| 59 | * Properties of source list expressions. |
| 60 | * Five of these have special meaning and optimized storage: |
| 61 | * |
| 62 | * filename string The name of the source file. |
| 63 | * copy list A copy of the list expression. |
| 64 | * line integer The source code line number. |
| 65 | * column integer The source code column number. |
| 66 | * breakpoint boolean Sets a breakpoint on this form. |
| 67 | * |
| 68 | * Most properties above can be set by the reader. |
| 69 | * |
| 70 | */ |
| 71 | |
| 72 | SCM scm_i_filename; |
| 73 | SCM scm_i_copy; |
| 74 | SCM scm_i_line; |
| 75 | SCM scm_i_column; |
| 76 | SCM scm_i_breakpoint; |
| 77 | |
| 78 | long scm_tc16_srcprops; |
| 79 | static scm_srcprops_chunk *srcprops_chunklist = 0; |
| 80 | static scm_srcprops *srcprops_freelist = 0; |
| 81 | |
| 82 | |
| 83 | static SCM marksrcprops SCM_P ((SCM obj)); |
| 84 | |
| 85 | static SCM |
| 86 | marksrcprops (obj) |
| 87 | SCM obj; |
| 88 | { |
| 89 | SCM_SETGC8MARK (obj); |
| 90 | scm_gc_mark (SRCPROPFNAME (obj)); |
| 91 | scm_gc_mark (SRCPROPCOPY (obj)); |
| 92 | return SRCPROPPLIST (obj); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static scm_sizet freesrcprops SCM_P ((SCM obj)); |
| 97 | |
| 98 | static scm_sizet |
| 99 | freesrcprops (obj) |
| 100 | SCM obj; |
| 101 | { |
| 102 | *((scm_srcprops **) SCM_CDR (obj)) = srcprops_freelist; |
| 103 | srcprops_freelist = (scm_srcprops *) SCM_CDR (obj); |
| 104 | return 0; /* srcprops_chunks are not freed until leaving guile */ |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static int prinsrcprops SCM_P ((SCM obj, SCM port, scm_print_state *pstate)); |
| 109 | |
| 110 | static int |
| 111 | prinsrcprops (obj, port, pstate) |
| 112 | SCM obj; |
| 113 | SCM port; |
| 114 | scm_print_state *pstate; |
| 115 | { |
| 116 | int writingp = SCM_WRITINGP (pstate); |
| 117 | scm_gen_puts (scm_regular_string, "#<srcprops ", port); |
| 118 | SCM_SET_WRITINGP (pstate, 1); |
| 119 | scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate); |
| 120 | SCM_SET_WRITINGP (pstate, writingp); |
| 121 | scm_gen_putc ('>', port); |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | static scm_smobfuns srcpropssmob = |
| 126 | {marksrcprops, freesrcprops, prinsrcprops, 0}; |
| 127 | |
| 128 | |
| 129 | SCM |
| 130 | scm_make_srcprops (line, col, filename, copy, plist) |
| 131 | int line; |
| 132 | int col; |
| 133 | SCM filename; |
| 134 | SCM copy; |
| 135 | SCM plist; |
| 136 | { |
| 137 | register SCM ans; |
| 138 | register scm_srcprops *ptr; |
| 139 | SCM_DEFER_INTS; |
| 140 | if ((ptr = srcprops_freelist) != NULL) |
| 141 | srcprops_freelist = *(scm_srcprops **)ptr; |
| 142 | else |
| 143 | { |
| 144 | int i; |
| 145 | scm_srcprops_chunk *mem; |
| 146 | scm_sizet n = sizeof (scm_srcprops_chunk) |
| 147 | + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1); |
| 148 | SCM_SYSCALL (mem = (scm_srcprops_chunk *) malloc (n)); |
| 149 | SCM_ASSERT (mem, SCM_UNDEFINED, SCM_NALLOC, "srcprops"); |
| 150 | scm_mallocated += n; |
| 151 | mem->next = srcprops_chunklist; |
| 152 | srcprops_chunklist = mem; |
| 153 | ptr = &mem->srcprops[0]; |
| 154 | for (i = 1; i < SRCPROPS_CHUNKSIZE - 1; ++i) |
| 155 | *(scm_srcprops **)&ptr[i] = &ptr[i + 1]; |
| 156 | *(scm_srcprops **)&ptr[SRCPROPS_CHUNKSIZE - 1] = 0; |
| 157 | srcprops_freelist = (scm_srcprops *) &ptr[1]; |
| 158 | } |
| 159 | SCM_NEWCELL (ans); |
| 160 | SCM_SETCAR (ans, scm_tc16_srcprops); |
| 161 | ptr->pos = SRCPROPMAKPOS (line, col); |
| 162 | ptr->fname = filename; |
| 163 | ptr->copy = copy; |
| 164 | ptr->plist = plist; |
| 165 | SCM_SETCDR (ans, (SCM) ptr); |
| 166 | SCM_ALLOW_INTS; |
| 167 | return ans; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | SCM |
| 172 | scm_srcprops_to_plist (obj) |
| 173 | SCM obj; |
| 174 | { |
| 175 | SCM plist = SRCPROPPLIST (obj); |
| 176 | if (!SCM_UNBNDP (SRCPROPCOPY (obj))) |
| 177 | plist = scm_acons (scm_i_copy, SRCPROPCOPY (obj), plist); |
| 178 | if (!SCM_UNBNDP (SRCPROPFNAME (obj))) |
| 179 | plist = scm_acons (scm_i_filename, SRCPROPFNAME (obj), plist); |
| 180 | plist = scm_acons (scm_i_column, SCM_MAKINUM (SRCPROPCOL (obj)), plist); |
| 181 | plist = scm_acons (scm_i_line, SCM_MAKINUM (SRCPROPLINE (obj)), plist); |
| 182 | plist = scm_acons (scm_i_breakpoint, SRCPROPBRK (obj), plist); |
| 183 | return plist; |
| 184 | } |
| 185 | |
| 186 | SCM_PROC (s_source_properties, "source-properties", 1, 0, 0, scm_source_properties); |
| 187 | |
| 188 | SCM |
| 189 | scm_source_properties (obj) |
| 190 | SCM obj; |
| 191 | { |
| 192 | SCM p; |
| 193 | SCM_ASSERT (SCM_NIMP (obj), obj, SCM_ARG1, s_source_properties); |
| 194 | if (SCM_MEMOIZEDP (obj)) |
| 195 | obj = SCM_MEMOIZED_EXP (obj); |
| 196 | #ifndef SCM_RECKLESS |
| 197 | else if (SCM_NCONSP (obj)) |
| 198 | scm_wrong_type_arg (s_source_properties, 1, obj); |
| 199 | #endif |
| 200 | p = scm_hashq_ref (scm_source_whash, obj, (SCM) NULL); |
| 201 | if (p != (SCM) NULL && SRCPROPSP (p)) |
| 202 | return scm_srcprops_to_plist (p); |
| 203 | return SCM_EOL; |
| 204 | } |
| 205 | |
| 206 | /* Perhaps this procedure should look through an alist |
| 207 | and try to make a srcprops-object...? */ |
| 208 | SCM_PROC (s_set_source_properties_x, "set-source-properties!", 2, 0, 0, scm_set_source_properties_x); |
| 209 | |
| 210 | SCM |
| 211 | scm_set_source_properties_x (obj, plist) |
| 212 | SCM obj; |
| 213 | SCM plist; |
| 214 | { |
| 215 | SCM handle; |
| 216 | SCM_ASSERT (SCM_NIMP (obj), obj, SCM_ARG1, s_set_source_properties_x); |
| 217 | if (SCM_MEMOIZEDP (obj)) |
| 218 | obj = SCM_MEMOIZED_EXP (obj); |
| 219 | #ifndef SCM_RECKLESS |
| 220 | else if (SCM_NCONSP (obj)) |
| 221 | scm_wrong_type_arg (s_set_source_properties_x, 1, obj); |
| 222 | #endif |
| 223 | handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist); |
| 224 | SCM_SETCDR (handle, plist); |
| 225 | return plist; |
| 226 | } |
| 227 | |
| 228 | SCM_PROC (s_source_property, "source-property", 2, 0, 0, scm_source_property); |
| 229 | |
| 230 | SCM |
| 231 | scm_source_property (obj, key) |
| 232 | SCM obj; |
| 233 | SCM key; |
| 234 | { |
| 235 | SCM p; |
| 236 | SCM_ASSERT (SCM_NIMP (obj), obj, SCM_ARG1, s_source_property); |
| 237 | if (SCM_MEMOIZEDP (obj)) |
| 238 | obj = SCM_MEMOIZED_EXP (obj); |
| 239 | #ifndef SCM_RECKLESS |
| 240 | else if (SCM_NCONSP (obj)) |
| 241 | scm_wrong_type_arg (s_source_property, 1, obj); |
| 242 | #endif |
| 243 | p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); |
| 244 | if (SCM_IMP (p) || !SRCPROPSP (p)) |
| 245 | goto plist; |
| 246 | if (scm_i_breakpoint == key) p = SRCPROPBRK (p); |
| 247 | else if (scm_i_line == key) p = SCM_MAKINUM (SRCPROPLINE (p)); |
| 248 | else if (scm_i_column == key) p = SCM_MAKINUM (SRCPROPCOL (p)); |
| 249 | else if (scm_i_filename == key) p = SRCPROPFNAME (p); |
| 250 | else if (scm_i_copy == key) p = SRCPROPCOPY (p); |
| 251 | else |
| 252 | { |
| 253 | p = SRCPROPPLIST (p); |
| 254 | plist: |
| 255 | p = scm_assoc (key, p); |
| 256 | return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F); |
| 257 | } |
| 258 | return SCM_UNBNDP (p) ? SCM_BOOL_F : p; |
| 259 | } |
| 260 | |
| 261 | SCM_PROC (s_set_source_property_x, "set-source-property!", 3, 0, 0, scm_set_source_property_x); |
| 262 | |
| 263 | SCM |
| 264 | scm_set_source_property_x (obj, key, datum) |
| 265 | SCM obj; |
| 266 | SCM key; |
| 267 | SCM datum; |
| 268 | { |
| 269 | scm_whash_handle h; |
| 270 | SCM p; |
| 271 | SCM_ASSERT (SCM_NIMP (obj), obj, SCM_ARG1, s_set_source_property_x); |
| 272 | if (SCM_MEMOIZEDP (obj)) |
| 273 | obj = SCM_MEMOIZED_EXP (obj); |
| 274 | #ifndef SCM_RECKLESS |
| 275 | else if (SCM_NCONSP (obj)) |
| 276 | scm_wrong_type_arg (s_set_source_property_x, 1, obj); |
| 277 | #endif |
| 278 | h = scm_whash_get_handle (scm_source_whash, obj); |
| 279 | if (SCM_WHASHFOUNDP (h)) |
| 280 | p = SCM_WHASHREF (scm_source_whash, h); |
| 281 | else |
| 282 | { |
| 283 | h = scm_whash_create_handle (scm_source_whash, obj); |
| 284 | p = SCM_EOL; |
| 285 | } |
| 286 | if (scm_i_breakpoint == key) |
| 287 | if (SCM_FALSEP (datum)) |
| 288 | CLEARSRCPROPBRK (SCM_NIMP (p) && SRCPROPSP (p) |
| 289 | ? p |
| 290 | : SCM_WHASHSET (scm_source_whash, h, |
| 291 | scm_make_srcprops (0, 0, SCM_UNDEFINED, |
| 292 | SCM_UNDEFINED, p))); |
| 293 | else |
| 294 | SETSRCPROPBRK (SCM_NIMP (p) && SRCPROPSP (p) |
| 295 | ? p |
| 296 | : SCM_WHASHSET (scm_source_whash, h, |
| 297 | scm_make_srcprops (0, 0, SCM_UNDEFINED, |
| 298 | SCM_UNDEFINED, p))); |
| 299 | else if (scm_i_line == key) |
| 300 | { |
| 301 | if (SCM_NIMP (p) && SRCPROPSP (p)) |
| 302 | SETSRCPROPLINE (p, datum); |
| 303 | else |
| 304 | SCM_WHASHSET (scm_source_whash, h, |
| 305 | scm_make_srcprops (datum, 0, SCM_UNDEFINED, SCM_UNDEFINED, p)); |
| 306 | } |
| 307 | else if (scm_i_column == key) |
| 308 | { |
| 309 | if (SCM_NIMP (p) && SRCPROPSP (p)) |
| 310 | SETSRCPROPCOL (p, datum); |
| 311 | else |
| 312 | SCM_WHASHSET (scm_source_whash, h, |
| 313 | scm_make_srcprops (0, datum, SCM_UNDEFINED, SCM_UNDEFINED, p)); |
| 314 | } |
| 315 | else if (scm_i_filename == key) |
| 316 | { |
| 317 | if (SCM_NIMP (p) && SRCPROPSP (p)) |
| 318 | SRCPROPFNAME (p) = datum; |
| 319 | else |
| 320 | SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p)); |
| 321 | } |
| 322 | else if (scm_i_filename == key) |
| 323 | { |
| 324 | if (SCM_NIMP (p) && SRCPROPSP (p)) |
| 325 | SRCPROPCOPY (p) = datum; |
| 326 | else |
| 327 | SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p)); |
| 328 | } |
| 329 | else |
| 330 | SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p)); |
| 331 | return SCM_UNSPECIFIED; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | void |
| 336 | scm_init_srcprop () |
| 337 | { |
| 338 | scm_tc16_srcprops = scm_newsmob (&srcpropssmob); |
| 339 | scm_source_whash = scm_make_weak_key_hash_table (SCM_MAKINUM (2047)); |
| 340 | |
| 341 | scm_i_filename = SCM_CAR (scm_sysintern ("filename", SCM_UNDEFINED)); |
| 342 | scm_i_copy = SCM_CAR (scm_sysintern ("copy", SCM_UNDEFINED)); |
| 343 | scm_i_line = SCM_CAR (scm_sysintern ("line", SCM_UNDEFINED)); |
| 344 | scm_i_column = SCM_CAR (scm_sysintern ("column", SCM_UNDEFINED)); |
| 345 | scm_i_breakpoint = SCM_CAR (scm_sysintern ("breakpoint", SCM_UNDEFINED)); |
| 346 | |
| 347 | scm_sysintern ("source-whash", scm_source_whash); |
| 348 | #include "srcprop.x" |
| 349 | } |
| 350 | |
| 351 | void |
| 352 | scm_finish_srcprop () |
| 353 | { |
| 354 | register scm_srcprops_chunk *ptr = srcprops_chunklist, *next; |
| 355 | while (ptr) |
| 356 | { |
| 357 | next = ptr->next; |
| 358 | free ((char *) ptr); |
| 359 | scm_mallocated -= sizeof (scm_srcprops_chunk) |
| 360 | + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1); |
| 361 | ptr = next; |
| 362 | } |
| 363 | } |