srcprop.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006, 2008, 2009, 2010, 2011 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/gc.h"
  33. #include "libguile/validate.h"
  34. #include "libguile/srcprop.h"
  35. /* {Source Properties}
  36. *
  37. * Properties of source list expressions.
  38. * Four of these have special meaning:
  39. *
  40. * filename string The name of the source file.
  41. * copy list A copy of the list expression.
  42. * line integer The source code line number.
  43. * column integer The source code column number.
  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 scm_source_whash;
  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 = alist
  60. */
  61. #define SRCPROPSP(p) (SCM_SMOB_PREDICATE (scm_tc16_srcprops, (p)))
  62. #define SRCPROPPOS(p) (SCM_SMOB_DATA(p))
  63. #define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12)
  64. #define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL)
  65. #define SRCPROPCOPY(p) (SCM_SMOB_OBJECT_2(p))
  66. #define SRCPROPALIST(p) (SCM_SMOB_OBJECT_3(p))
  67. #define SRCPROPMAKPOS(l, c) (((l) << 12) + (c))
  68. #define SETSRCPROPPOS(p, l, c) (SCM_SET_SMOB_DATA_1 (p, SRCPROPMAKPOS (l, c)))
  69. #define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p))
  70. #define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c)
  71. #define SETSRCPROPCOPY(p, c) (SCM_SET_SMOB_OBJECT_2 (p, c))
  72. #define SETSRCPROPALIST(p, l) (SCM_SET_SMOB_OBJECT_3 (p, l))
  73. static SCM scm_srcprops_to_alist (SCM obj);
  74. scm_t_bits scm_tc16_srcprops;
  75. static int
  76. srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
  77. {
  78. int writingp = SCM_WRITINGP (pstate);
  79. scm_puts ("#<srcprops ", port);
  80. SCM_SET_WRITINGP (pstate, 1);
  81. scm_iprin1 (scm_srcprops_to_alist (obj), port, pstate);
  82. SCM_SET_WRITINGP (pstate, writingp);
  83. scm_putc ('>', port);
  84. return 1;
  85. }
  86. /*
  87. * We remember the last file name settings, so we can share that alist
  88. * entry. This works because scm_set_source_property_x does not use
  89. * assoc-set! for modifying the alist.
  90. *
  91. * This variable contains a protected cons, whose cdr is the cached
  92. * alist
  93. */
  94. static SCM scm_last_alist_filename;
  95. SCM
  96. scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist)
  97. {
  98. if (!SCM_UNBNDP (filename))
  99. {
  100. SCM old_alist = alist;
  101. /*
  102. have to extract the acons, and operate on that, for
  103. thread safety.
  104. */
  105. SCM last_acons = SCM_CDR (scm_last_alist_filename);
  106. if (scm_is_null (old_alist)
  107. && scm_is_eq (SCM_CDAR (last_acons), filename))
  108. {
  109. alist = last_acons;
  110. }
  111. else
  112. {
  113. alist = scm_acons (scm_sym_filename, filename, alist);
  114. if (scm_is_null (old_alist))
  115. SCM_SETCDR (scm_last_alist_filename, alist);
  116. }
  117. }
  118. SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops,
  119. SRCPROPMAKPOS (line, col),
  120. SCM_UNPACK (copy),
  121. SCM_UNPACK (alist));
  122. }
  123. static SCM
  124. scm_srcprops_to_alist (SCM obj)
  125. {
  126. SCM alist = SRCPROPALIST (obj);
  127. if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
  128. alist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), alist);
  129. alist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), alist);
  130. alist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), alist);
  131. return alist;
  132. }
  133. SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
  134. (SCM obj),
  135. "Return the source property association list of @var{obj}.")
  136. #define FUNC_NAME s_scm_source_properties
  137. {
  138. SCM p;
  139. SCM_VALIDATE_NIM (1, obj);
  140. p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
  141. if (SRCPROPSP (p))
  142. return scm_srcprops_to_alist (p);
  143. else
  144. /* list from set-source-properties!, or SCM_EOL for not found */
  145. return p;
  146. }
  147. #undef FUNC_NAME
  148. /* Perhaps this procedure should look through an alist
  149. and try to make a srcprops-object...? */
  150. SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
  151. (SCM obj, SCM alist),
  152. "Install the association list @var{alist} as the source property\n"
  153. "list for @var{obj}.")
  154. #define FUNC_NAME s_scm_set_source_properties_x
  155. {
  156. SCM_VALIDATE_NIM (1, obj);
  157. scm_hashq_set_x (scm_source_whash, obj, alist);
  158. return alist;
  159. }
  160. #undef FUNC_NAME
  161. SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
  162. (SCM obj, SCM key),
  163. "Return the source property specified by @var{key} from\n"
  164. "@var{obj}'s source property list.")
  165. #define FUNC_NAME s_scm_source_property
  166. {
  167. SCM p;
  168. SCM_VALIDATE_NIM (1, obj);
  169. p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
  170. if (!SRCPROPSP (p))
  171. goto alist;
  172. if (scm_is_eq (scm_sym_line, key))
  173. p = scm_from_int (SRCPROPLINE (p));
  174. else if (scm_is_eq (scm_sym_column, key))
  175. p = scm_from_int (SRCPROPCOL (p));
  176. else if (scm_is_eq (scm_sym_copy, key))
  177. p = SRCPROPCOPY (p);
  178. else
  179. {
  180. p = SRCPROPALIST (p);
  181. alist:
  182. p = scm_assoc (key, p);
  183. return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
  184. }
  185. return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
  186. }
  187. #undef FUNC_NAME
  188. SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
  189. (SCM obj, SCM key, SCM datum),
  190. "Set the source property of object @var{obj}, which is specified by\n"
  191. "@var{key} to @var{datum}. Normally, the key will be a symbol.")
  192. #define FUNC_NAME s_scm_set_source_property_x
  193. {
  194. SCM p;
  195. SCM_VALIDATE_NIM (1, obj);
  196. p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
  197. if (scm_is_eq (scm_sym_line, key))
  198. {
  199. if (SRCPROPSP (p))
  200. SETSRCPROPLINE (p, scm_to_int (datum));
  201. else
  202. scm_hashq_set_x (scm_source_whash, obj,
  203. scm_make_srcprops (scm_to_int (datum), 0,
  204. SCM_UNDEFINED, SCM_UNDEFINED, p));
  205. }
  206. else if (scm_is_eq (scm_sym_column, key))
  207. {
  208. if (SRCPROPSP (p))
  209. SETSRCPROPCOL (p, scm_to_int (datum));
  210. else
  211. scm_hashq_set_x (scm_source_whash, obj,
  212. scm_make_srcprops (0, scm_to_int (datum),
  213. SCM_UNDEFINED, SCM_UNDEFINED, p));
  214. }
  215. else if (scm_is_eq (scm_sym_copy, key))
  216. {
  217. if (SRCPROPSP (p))
  218. SETSRCPROPCOPY (p, datum);
  219. else
  220. scm_hashq_set_x (scm_source_whash, obj,
  221. scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
  222. }
  223. else
  224. {
  225. if (SRCPROPSP (p))
  226. SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p)));
  227. else
  228. scm_hashq_set_x (scm_source_whash, obj,
  229. scm_acons (key, datum, p));
  230. }
  231. return SCM_UNSPECIFIED;
  232. }
  233. #undef FUNC_NAME
  234. SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
  235. (SCM xorig, SCM x, SCM y),
  236. "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
  237. "Any source properties associated with @var{xorig} are also associated\n"
  238. "with the new pair.")
  239. #define FUNC_NAME s_scm_cons_source
  240. {
  241. SCM p, z;
  242. z = scm_cons (x, y);
  243. /* Copy source properties possibly associated with xorig. */
  244. p = scm_hashq_ref (scm_source_whash, xorig, SCM_BOOL_F);
  245. if (scm_is_true (p))
  246. scm_hashq_set_x (scm_source_whash, z, p);
  247. return z;
  248. }
  249. #undef FUNC_NAME
  250. void
  251. scm_init_srcprop ()
  252. {
  253. scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
  254. scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
  255. scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047));
  256. scm_c_define ("source-whash", scm_source_whash);
  257. scm_last_alist_filename = scm_cons (SCM_EOL,
  258. scm_acons (SCM_EOL, SCM_EOL, SCM_EOL));
  259. #include "libguile/srcprop.x"
  260. }
  261. /*
  262. Local Variables:
  263. c-file-style: "gnu"
  264. End:
  265. */