srcprop.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006, 2008 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 License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * 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
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <errno.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/async.h"
  24. #include "libguile/smob.h"
  25. #include "libguile/alist.h"
  26. #include "libguile/debug.h"
  27. #include "libguile/hashtab.h"
  28. #include "libguile/hash.h"
  29. #include "libguile/ports.h"
  30. #include "libguile/root.h"
  31. #include "libguile/weaks.h"
  32. #include "libguile/validate.h"
  33. #include "libguile/srcprop.h"
  34. /* {Source Properties}
  35. *
  36. * Properties of source list expressions.
  37. * Five of these have special meaning:
  38. *
  39. * filename string The name of the source file.
  40. * copy list A copy of the list expression.
  41. * line integer The source code line number.
  42. * column integer The source code column number.
  43. * breakpoint boolean Sets a breakpoint on this form.
  44. *
  45. * Most properties above can be set by the reader.
  46. *
  47. */
  48. SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
  49. SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
  50. SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
  51. SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
  52. SCM_GLOBAL_SYMBOL (scm_sym_breakpoint, "breakpoint");
  53. /*
  54. * Source properties are stored as double cells with the
  55. * following layout:
  56. * car = tag
  57. * cbr = pos
  58. * ccr = copy
  59. * cdr = plist
  60. */
  61. #define SRCPROPSP(p) (SCM_SMOB_PREDICATE (scm_tc16_srcprops, (p)))
  62. #define SRCPROPBRK(p) (SCM_SMOB_FLAGS (p) & SCM_SOURCE_PROPERTY_FLAG_BREAK)
  63. #define SRCPROPPOS(p) (SCM_CELL_WORD(p,1))
  64. #define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12)
  65. #define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL)
  66. #define SRCPROPCOPY(p) (SCM_CELL_OBJECT(p,2))
  67. #define SRCPROPPLIST(p) (SCM_CELL_OBJECT_3(p))
  68. #define SETSRCPROPBRK(p) \
  69. (SCM_SET_SMOB_FLAGS ((p), \
  70. SCM_SMOB_FLAGS (p) | SCM_SOURCE_PROPERTY_FLAG_BREAK))
  71. #define CLEARSRCPROPBRK(p) \
  72. (SCM_SET_SMOB_FLAGS ((p), \
  73. SCM_SMOB_FLAGS (p) & ~SCM_SOURCE_PROPERTY_FLAG_BREAK))
  74. #define SRCPROPMAKPOS(l, c) (((l) << 12) + (c))
  75. #define SETSRCPROPPOS(p, l, c) (SCM_SET_CELL_WORD(p,1, SRCPROPMAKPOS (l, c)))
  76. #define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p))
  77. #define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c)
  78. #define SETSRCPROPCOPY(p, c) (SCM_SET_CELL_WORD(p, 2, c))
  79. #define SETSRCPROPPLIST(p, l) (SCM_SET_CELL_WORD(p, 3, l))
  80. scm_t_bits scm_tc16_srcprops;
  81. static SCM
  82. srcprops_mark (SCM obj)
  83. {
  84. scm_gc_mark (SRCPROPCOPY (obj));
  85. return SRCPROPPLIST (obj);
  86. }
  87. static int
  88. srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
  89. {
  90. int writingp = SCM_WRITINGP (pstate);
  91. scm_puts ("#<srcprops ", port);
  92. SCM_SET_WRITINGP (pstate, 1);
  93. scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate);
  94. SCM_SET_WRITINGP (pstate, writingp);
  95. scm_putc ('>', port);
  96. return 1;
  97. }
  98. int
  99. scm_c_source_property_breakpoint_p (SCM form)
  100. {
  101. SCM obj = scm_whash_lookup (scm_source_whash, form);
  102. return SRCPROPSP (obj) && SRCPROPBRK (obj);
  103. }
  104. /*
  105. * We remember the last file name settings, so we can share that plist
  106. * entry. This works because scm_set_source_property_x does not use
  107. * assoc-set! for modifying the plist.
  108. *
  109. * This variable contains a protected cons, whose cdr is the cached
  110. * plist
  111. */
  112. static SCM scm_last_plist_filename;
  113. SCM
  114. scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM plist)
  115. {
  116. if (!SCM_UNBNDP (filename))
  117. {
  118. SCM old_plist = plist;
  119. /*
  120. have to extract the acons, and operate on that, for
  121. thread safety.
  122. */
  123. SCM last_acons = SCM_CDR (scm_last_plist_filename);
  124. if (old_plist == SCM_EOL
  125. && SCM_CDAR (last_acons) == filename)
  126. {
  127. plist = last_acons;
  128. }
  129. else
  130. {
  131. plist = scm_acons (scm_sym_filename, filename, plist);
  132. if (old_plist == SCM_EOL)
  133. SCM_SETCDR (scm_last_plist_filename, plist);
  134. }
  135. }
  136. SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops,
  137. SRCPROPMAKPOS (line, col),
  138. copy,
  139. plist);
  140. }
  141. SCM
  142. scm_srcprops_to_plist (SCM obj)
  143. {
  144. SCM plist = SRCPROPPLIST (obj);
  145. if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
  146. plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist);
  147. plist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), plist);
  148. plist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), plist);
  149. plist = scm_acons (scm_sym_breakpoint, scm_from_bool (SRCPROPBRK (obj)), plist);
  150. return plist;
  151. }
  152. SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
  153. (SCM obj),
  154. "Return the source property association list of @var{obj}.")
  155. #define FUNC_NAME s_scm_source_properties
  156. {
  157. SCM p;
  158. SCM_VALIDATE_NIM (1, obj);
  159. if (SCM_MEMOIZEDP (obj))
  160. obj = SCM_MEMOIZED_EXP (obj);
  161. else if (!scm_is_pair (obj))
  162. SCM_WRONG_TYPE_ARG (1, obj);
  163. p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
  164. if (SRCPROPSP (p))
  165. return scm_srcprops_to_plist (p);
  166. else
  167. /* list from set-source-properties!, or SCM_EOL for not found */
  168. return p;
  169. }
  170. #undef FUNC_NAME
  171. /* Perhaps this procedure should look through an alist
  172. and try to make a srcprops-object...? */
  173. SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
  174. (SCM obj, SCM plist),
  175. "Install the association list @var{plist} as the source property\n"
  176. "list for @var{obj}.")
  177. #define FUNC_NAME s_scm_set_source_properties_x
  178. {
  179. SCM handle;
  180. SCM_VALIDATE_NIM (1, obj);
  181. if (SCM_MEMOIZEDP (obj))
  182. obj = SCM_MEMOIZED_EXP (obj);
  183. else if (!scm_is_pair (obj))
  184. SCM_WRONG_TYPE_ARG(1, obj);
  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. else if (!scm_is_pair (obj))
  201. SCM_WRONG_TYPE_ARG (1, obj);
  202. p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
  203. if (!SRCPROPSP (p))
  204. goto plist;
  205. if (scm_is_eq (scm_sym_breakpoint, key)) p = scm_from_bool (SRCPROPBRK (p));
  206. else if (scm_is_eq (scm_sym_line, key)) p = scm_from_int (SRCPROPLINE (p));
  207. else if (scm_is_eq (scm_sym_column, key)) p = scm_from_int (SRCPROPCOL (p));
  208. else if (scm_is_eq (scm_sym_copy, key)) p = SRCPROPCOPY (p);
  209. else
  210. {
  211. p = SRCPROPPLIST (p);
  212. plist:
  213. p = scm_assoc (key, p);
  214. return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
  215. }
  216. return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
  217. }
  218. #undef FUNC_NAME
  219. SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
  220. (SCM obj, SCM key, SCM datum),
  221. "Set the source property of object @var{obj}, which is specified by\n"
  222. "@var{key} to @var{datum}. Normally, the key will be a symbol.")
  223. #define FUNC_NAME s_scm_set_source_property_x
  224. {
  225. scm_whash_handle h;
  226. SCM p;
  227. SCM_VALIDATE_NIM (1, obj);
  228. if (SCM_MEMOIZEDP (obj))
  229. obj = SCM_MEMOIZED_EXP (obj);
  230. else if (!scm_is_pair (obj))
  231. SCM_WRONG_TYPE_ARG (1, obj);
  232. h = scm_whash_get_handle (scm_source_whash, obj);
  233. if (SCM_WHASHFOUNDP (h))
  234. p = SCM_WHASHREF (scm_source_whash, h);
  235. else
  236. {
  237. h = scm_whash_create_handle (scm_source_whash, obj);
  238. p = SCM_EOL;
  239. }
  240. if (scm_is_eq (scm_sym_breakpoint, key))
  241. {
  242. if (SRCPROPSP (p))
  243. {
  244. if (scm_is_false (datum))
  245. CLEARSRCPROPBRK (p);
  246. else
  247. SETSRCPROPBRK (p);
  248. }
  249. else
  250. {
  251. SCM sp = scm_make_srcprops (0, 0, SCM_UNDEFINED, SCM_UNDEFINED, p);
  252. SCM_WHASHSET (scm_source_whash, h, sp);
  253. if (scm_is_false (datum))
  254. CLEARSRCPROPBRK (sp);
  255. else
  256. SETSRCPROPBRK (sp);
  257. }
  258. }
  259. else if (scm_is_eq (scm_sym_line, key))
  260. {
  261. if (SRCPROPSP (p))
  262. SETSRCPROPLINE (p, scm_to_int (datum));
  263. else
  264. SCM_WHASHSET (scm_source_whash, h,
  265. scm_make_srcprops (scm_to_int (datum), 0,
  266. SCM_UNDEFINED, SCM_UNDEFINED, p));
  267. }
  268. else if (scm_is_eq (scm_sym_column, key))
  269. {
  270. if (SRCPROPSP (p))
  271. SETSRCPROPCOL (p, scm_to_int (datum));
  272. else
  273. SCM_WHASHSET (scm_source_whash, h,
  274. scm_make_srcprops (0, scm_to_int (datum),
  275. SCM_UNDEFINED, SCM_UNDEFINED, p));
  276. }
  277. else if (scm_is_eq (scm_sym_copy, key))
  278. {
  279. if (SRCPROPSP (p))
  280. SETSRCPROPCOPY (p, datum);
  281. else
  282. SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
  283. }
  284. else
  285. {
  286. if (SRCPROPSP (p))
  287. SETSRCPROPPLIST (p, scm_acons (key, datum, SRCPROPPLIST (p)));
  288. else
  289. SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
  290. }
  291. return SCM_UNSPECIFIED;
  292. }
  293. #undef FUNC_NAME
  294. void
  295. scm_init_srcprop ()
  296. {
  297. scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
  298. scm_set_smob_mark (scm_tc16_srcprops, srcprops_mark);
  299. scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
  300. scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047));
  301. scm_c_define ("source-whash", scm_source_whash);
  302. scm_last_plist_filename
  303. = scm_permanent_object (scm_cons (SCM_EOL,
  304. scm_acons (SCM_EOL, SCM_EOL, SCM_EOL)));
  305. #include "libguile/srcprop.x"
  306. }
  307. /*
  308. Local Variables:
  309. c-file-style: "gnu"
  310. End:
  311. */