srcprop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2005 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., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 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 <errno.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_GLOBAL_SYMBOL (scm_sym_filename, "filename");
  71. SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
  72. SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
  73. SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
  74. SCM_GLOBAL_SYMBOL (scm_sym_breakpoint, "breakpoint");
  75. scm_t_bits scm_tc16_srcprops;
  76. static scm_t_srcprops_chunk *srcprops_chunklist = 0;
  77. static scm_t_srcprops *srcprops_freelist = 0;
  78. static SCM
  79. srcprops_mark (SCM obj)
  80. {
  81. scm_gc_mark (SRCPROPFNAME (obj));
  82. scm_gc_mark (SRCPROPCOPY (obj));
  83. return SRCPROPPLIST (obj);
  84. }
  85. static size_t
  86. srcprops_free (SCM obj)
  87. {
  88. *((scm_t_srcprops **) SCM_CELL_WORD_1 (obj)) = srcprops_freelist;
  89. srcprops_freelist = (scm_t_srcprops *) SCM_CELL_WORD_1 (obj);
  90. return 0; /* srcprops_chunks are not freed until leaving guile */
  91. }
  92. static int
  93. srcprops_print (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 (long line, int col, SCM filename, SCM copy, SCM plist)
  105. {
  106. register scm_t_srcprops *ptr;
  107. SCM_DEFER_INTS;
  108. if ((ptr = srcprops_freelist) != NULL)
  109. srcprops_freelist = *(scm_t_srcprops **)ptr;
  110. else
  111. {
  112. size_t i;
  113. scm_t_srcprops_chunk *mem;
  114. size_t n = sizeof (scm_t_srcprops_chunk)
  115. + sizeof (scm_t_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
  116. SCM_SYSCALL (mem = (scm_t_srcprops_chunk *) malloc (n));
  117. if (mem == NULL)
  118. scm_memory_error ("srcprops");
  119. scm_mallocated += n;
  120. mem->next = srcprops_chunklist;
  121. srcprops_chunklist = mem;
  122. ptr = &mem->srcprops[0];
  123. for (i = 1; i < SRCPROPS_CHUNKSIZE - 1; ++i)
  124. *(scm_t_srcprops **)&ptr[i] = &ptr[i + 1];
  125. *(scm_t_srcprops **)&ptr[SRCPROPS_CHUNKSIZE - 1] = 0;
  126. srcprops_freelist = (scm_t_srcprops *) &ptr[1];
  127. }
  128. ptr->pos = SRCPROPMAKPOS (line, col);
  129. ptr->fname = filename;
  130. ptr->copy = copy;
  131. ptr->plist = plist;
  132. SCM_ALLOW_INTS;
  133. SCM_RETURN_NEWSMOB (scm_tc16_srcprops, ptr);
  134. }
  135. SCM
  136. scm_srcprops_to_plist (SCM obj)
  137. {
  138. SCM plist = SRCPROPPLIST (obj);
  139. if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
  140. plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist);
  141. if (!SCM_UNBNDP (SRCPROPFNAME (obj)))
  142. plist = scm_acons (scm_sym_filename, SRCPROPFNAME (obj), plist);
  143. plist = scm_acons (scm_sym_column, SCM_MAKINUM (SRCPROPCOL (obj)), plist);
  144. plist = scm_acons (scm_sym_line, SCM_MAKINUM (SRCPROPLINE (obj)), plist);
  145. plist = scm_acons (scm_sym_breakpoint, SRCPROPBRK (obj), plist);
  146. return plist;
  147. }
  148. SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
  149. (SCM obj),
  150. "Return the source property association list of @var{obj}.")
  151. #define FUNC_NAME s_scm_source_properties
  152. {
  153. SCM p;
  154. SCM_VALIDATE_NIM (1,obj);
  155. if (SCM_MEMOIZEDP (obj))
  156. obj = SCM_MEMOIZED_EXP (obj);
  157. #ifndef SCM_RECKLESS
  158. else if (SCM_NECONSP (obj))
  159. SCM_WRONG_TYPE_ARG (1, obj);
  160. #endif
  161. p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
  162. if (SRCPROPSP (p))
  163. return scm_srcprops_to_plist (p);
  164. else
  165. /* list from set-source-properties!, or SCM_EOL for not found */
  166. return p;
  167. }
  168. #undef FUNC_NAME
  169. /* Perhaps this procedure should look through an alist
  170. and try to make a srcprops-object...? */
  171. SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
  172. (SCM obj, SCM plist),
  173. "Install the association list @var{plist} as the source property\n"
  174. "list for @var{obj}.")
  175. #define FUNC_NAME s_scm_set_source_properties_x
  176. {
  177. SCM handle;
  178. SCM_VALIDATE_NIM (1,obj);
  179. if (SCM_MEMOIZEDP (obj))
  180. obj = SCM_MEMOIZED_EXP (obj);
  181. #ifndef SCM_RECKLESS
  182. else if (SCM_NECONSP (obj))
  183. SCM_WRONG_TYPE_ARG(1, obj);
  184. #endif
  185. handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist);
  186. SCM_SETCDR (handle, plist);
  187. return plist;
  188. }
  189. #undef FUNC_NAME
  190. SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
  191. (SCM obj, SCM key),
  192. "Return the source property specified by @var{key} from\n"
  193. "@var{obj}'s source property list.")
  194. #define FUNC_NAME s_scm_source_property
  195. {
  196. SCM p;
  197. SCM_VALIDATE_NIM (1,obj);
  198. if (SCM_MEMOIZEDP (obj))
  199. obj = SCM_MEMOIZED_EXP (obj);
  200. #ifndef SCM_RECKLESS
  201. else if (SCM_NECONSP (obj))
  202. SCM_WRONG_TYPE_ARG (1, obj);
  203. #endif
  204. p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
  205. if (SCM_IMP (p) || !SRCPROPSP (p))
  206. goto plist;
  207. if (SCM_EQ_P (scm_sym_breakpoint, key)) p = SRCPROPBRK (p);
  208. else if (SCM_EQ_P (scm_sym_line, key)) p = SCM_MAKINUM (SRCPROPLINE (p));
  209. else if (SCM_EQ_P (scm_sym_column, key)) p = SCM_MAKINUM (SRCPROPCOL (p));
  210. else if (SCM_EQ_P (scm_sym_filename, key)) p = SRCPROPFNAME (p);
  211. else if (SCM_EQ_P (scm_sym_copy, key)) p = SRCPROPCOPY (p);
  212. else
  213. {
  214. p = SRCPROPPLIST (p);
  215. plist:
  216. p = scm_assoc (key, p);
  217. return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
  218. }
  219. return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
  220. }
  221. #undef FUNC_NAME
  222. SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
  223. (SCM obj, SCM key, SCM datum),
  224. "Set the source property of object @var{obj}, which is specified by\n"
  225. "@var{key} to @var{datum}. Normally, the key will be a symbol.")
  226. #define FUNC_NAME s_scm_set_source_property_x
  227. {
  228. scm_whash_handle h;
  229. SCM p;
  230. SCM_VALIDATE_NIM (1,obj);
  231. if (SCM_MEMOIZEDP (obj))
  232. obj = SCM_MEMOIZED_EXP (obj);
  233. #ifndef SCM_RECKLESS
  234. else if (SCM_NECONSP (obj))
  235. SCM_WRONG_TYPE_ARG (1, obj);
  236. #endif
  237. h = scm_whash_get_handle (scm_source_whash, obj);
  238. if (SCM_WHASHFOUNDP (h))
  239. p = SCM_WHASHREF (scm_source_whash, h);
  240. else
  241. {
  242. h = scm_whash_create_handle (scm_source_whash, obj);
  243. p = SCM_EOL;
  244. }
  245. if (SCM_EQ_P (scm_sym_breakpoint, key))
  246. {
  247. if (SRCPROPSP (p))
  248. {
  249. if (SCM_FALSEP (datum))
  250. CLEARSRCPROPBRK (p);
  251. else
  252. SETSRCPROPBRK (p);
  253. }
  254. else
  255. {
  256. SCM sp = scm_make_srcprops (0, 0, SCM_UNDEFINED, SCM_UNDEFINED, p);
  257. SCM_WHASHSET (scm_source_whash, h, sp);
  258. if (SCM_FALSEP (datum))
  259. CLEARSRCPROPBRK (sp);
  260. else
  261. SETSRCPROPBRK (sp);
  262. }
  263. }
  264. else if (SCM_EQ_P (scm_sym_line, key))
  265. {
  266. SCM_VALIDATE_INUM (3,datum);
  267. if (SRCPROPSP (p))
  268. SETSRCPROPLINE (p, SCM_INUM (datum));
  269. else
  270. SCM_WHASHSET (scm_source_whash, h,
  271. scm_make_srcprops (SCM_INUM (datum), 0,
  272. SCM_UNDEFINED, SCM_UNDEFINED, p));
  273. }
  274. else if (SCM_EQ_P (scm_sym_column, key))
  275. {
  276. SCM_VALIDATE_INUM (3,datum);
  277. if (SRCPROPSP (p))
  278. SETSRCPROPCOL (p, SCM_INUM (datum));
  279. else
  280. SCM_WHASHSET (scm_source_whash, h,
  281. scm_make_srcprops (0, SCM_INUM (datum),
  282. SCM_UNDEFINED, SCM_UNDEFINED, p));
  283. }
  284. else if (SCM_EQ_P (scm_sym_filename, key))
  285. {
  286. if (SRCPROPSP (p))
  287. SRCPROPFNAME (p) = datum;
  288. else
  289. SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p));
  290. }
  291. else if (SCM_EQ_P (scm_sym_copy, key))
  292. {
  293. if (SRCPROPSP (p))
  294. SRCPROPCOPY (p) = datum;
  295. else
  296. SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
  297. }
  298. else
  299. SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
  300. return SCM_UNSPECIFIED;
  301. }
  302. #undef FUNC_NAME
  303. void
  304. scm_init_srcprop ()
  305. {
  306. scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
  307. scm_set_smob_mark (scm_tc16_srcprops, srcprops_mark);
  308. scm_set_smob_free (scm_tc16_srcprops, srcprops_free);
  309. scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
  310. scm_source_whash = scm_make_weak_key_hash_table (SCM_MAKINUM (2047));
  311. scm_c_define ("source-whash", scm_source_whash);
  312. #include "libguile/srcprop.x"
  313. }
  314. void
  315. scm_finish_srcprop ()
  316. {
  317. register scm_t_srcprops_chunk *ptr = srcprops_chunklist, *next;
  318. while (ptr)
  319. {
  320. next = ptr->next;
  321. free ((char *) ptr);
  322. scm_mallocated -= sizeof (scm_t_srcprops_chunk)
  323. + sizeof (scm_t_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
  324. ptr = next;
  325. }
  326. }
  327. /*
  328. Local Variables:
  329. c-file-style: "gnu"
  330. End:
  331. */