srcprop.c 11 KB

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