srcprop.c 9.6 KB

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