srcprop.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2006,
  2. * 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <errno.h>
  23. #include "libguile/_scm.h"
  24. #include "libguile/async.h"
  25. #include "libguile/smob.h"
  26. #include "libguile/alist.h"
  27. #include "libguile/debug.h"
  28. #include "libguile/hashtab.h"
  29. #include "libguile/hash.h"
  30. #include "libguile/ports.h"
  31. #include "libguile/gc.h"
  32. #include "libguile/validate.h"
  33. #include "libguile/srcprop.h"
  34. #include "libguile/private-options.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. static 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. supports_source_props (SCM obj)
  77. {
  78. return SCM_NIMP (obj) && !scm_is_symbol (obj) && !scm_is_keyword (obj);
  79. }
  80. static int
  81. srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
  82. {
  83. int writingp = SCM_WRITINGP (pstate);
  84. scm_puts ("#<srcprops ", port);
  85. SCM_SET_WRITINGP (pstate, 1);
  86. scm_iprin1 (scm_srcprops_to_alist (obj), port, pstate);
  87. SCM_SET_WRITINGP (pstate, writingp);
  88. scm_putc ('>', port);
  89. return 1;
  90. }
  91. /*
  92. * We remember the last file name settings, so we can share that alist
  93. * entry. This works because scm_set_source_property_x does not use
  94. * assoc-set! for modifying the alist.
  95. *
  96. * This variable contains a protected cons, whose cdr is the cached
  97. * alist
  98. */
  99. static SCM scm_last_alist_filename;
  100. SCM
  101. scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist)
  102. {
  103. if (!SCM_UNBNDP (filename))
  104. {
  105. SCM old_alist = alist;
  106. /*
  107. have to extract the acons, and operate on that, for
  108. thread safety.
  109. */
  110. SCM last_acons = SCM_CDR (scm_last_alist_filename);
  111. if (scm_is_null (old_alist)
  112. && scm_is_eq (SCM_CDAR (last_acons), filename))
  113. {
  114. alist = last_acons;
  115. }
  116. else
  117. {
  118. alist = scm_acons (scm_sym_filename, filename, alist);
  119. if (scm_is_null (old_alist))
  120. scm_set_cdr_x (scm_last_alist_filename, alist);
  121. }
  122. }
  123. SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops,
  124. SRCPROPMAKPOS (line, col),
  125. SCM_UNPACK (copy),
  126. SCM_UNPACK (alist));
  127. }
  128. static SCM
  129. scm_srcprops_to_alist (SCM obj)
  130. {
  131. SCM alist = SRCPROPALIST (obj);
  132. if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
  133. alist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), alist);
  134. alist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), alist);
  135. alist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), alist);
  136. return alist;
  137. }
  138. SCM_DEFINE (scm_supports_source_properties_p, "supports-source-properties?", 1, 0, 0,
  139. (SCM obj),
  140. "Return #t if @var{obj} supports adding source properties,\n"
  141. "otherwise return #f.")
  142. #define FUNC_NAME s_scm_supports_source_properties_p
  143. {
  144. return scm_from_bool (supports_source_props (obj));
  145. }
  146. #undef FUNC_NAME
  147. SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
  148. (SCM obj),
  149. "Return the source property association list of @var{obj}.")
  150. #define FUNC_NAME s_scm_source_properties
  151. {
  152. if (SCM_IMP (obj))
  153. return SCM_EOL;
  154. else
  155. {
  156. SCM p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  157. if (SRCPROPSP (p))
  158. return scm_srcprops_to_alist (p);
  159. else
  160. /* list from set-source-properties!, or SCM_EOL for not found */
  161. return p;
  162. }
  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 alist),
  169. "Install the association list @var{alist} as the source property\n"
  170. "list for @var{obj}.")
  171. #define FUNC_NAME s_scm_set_source_properties_x
  172. {
  173. SCM_VALIDATE_NIM (1, obj);
  174. scm_weak_table_putq_x (scm_source_whash, obj, alist);
  175. return alist;
  176. }
  177. #undef FUNC_NAME
  178. int
  179. scm_i_has_source_properties (SCM obj)
  180. #define FUNC_NAME "%set-source-properties"
  181. {
  182. if (SCM_IMP (obj))
  183. return 0;
  184. else
  185. return scm_is_true (scm_weak_table_refq (scm_source_whash, obj, SCM_BOOL_F));
  186. }
  187. #undef FUNC_NAME
  188. void
  189. scm_i_set_source_properties_x (SCM obj, long line, int col, SCM fname)
  190. #define FUNC_NAME "%set-source-properties"
  191. {
  192. SCM_VALIDATE_NIM (1, obj);
  193. scm_weak_table_putq_x (scm_source_whash, obj,
  194. scm_make_srcprops (line, col, fname,
  195. SCM_COPY_SOURCE_P
  196. ? scm_copy_tree (obj)
  197. : SCM_UNDEFINED,
  198. SCM_EOL));
  199. }
  200. #undef FUNC_NAME
  201. SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
  202. (SCM obj, SCM key),
  203. "Return the source property specified by @var{key} from\n"
  204. "@var{obj}'s source property list.")
  205. #define FUNC_NAME s_scm_source_property
  206. {
  207. SCM p;
  208. if (SCM_IMP (obj))
  209. return SCM_BOOL_F;
  210. p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  211. if (!SRCPROPSP (p))
  212. goto alist;
  213. if (scm_is_eq (scm_sym_line, key))
  214. return scm_from_int (SRCPROPLINE (p));
  215. else if (scm_is_eq (scm_sym_column, key))
  216. return scm_from_int (SRCPROPCOL (p));
  217. else if (scm_is_eq (scm_sym_copy, key))
  218. return SRCPROPCOPY (p);
  219. else
  220. {
  221. p = SRCPROPALIST (p);
  222. alist:
  223. p = scm_assoc (key, p);
  224. return (scm_is_pair (p) ? SCM_CDR (p) : SCM_BOOL_F);
  225. }
  226. }
  227. #undef FUNC_NAME
  228. SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
  229. (SCM obj, SCM key, SCM datum),
  230. "Set the source property of object @var{obj}, which is specified by\n"
  231. "@var{key} to @var{datum}. Normally, the key will be a symbol.")
  232. #define FUNC_NAME s_scm_set_source_property_x
  233. {
  234. SCM p;
  235. SCM_VALIDATE_NIM (1, obj);
  236. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  237. p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  238. if (scm_is_eq (scm_sym_line, key))
  239. {
  240. if (SRCPROPSP (p))
  241. SETSRCPROPLINE (p, scm_to_int (datum));
  242. else
  243. scm_weak_table_putq_x (scm_source_whash, obj,
  244. scm_make_srcprops (scm_to_int (datum), 0,
  245. SCM_UNDEFINED, SCM_UNDEFINED, p));
  246. }
  247. else if (scm_is_eq (scm_sym_column, key))
  248. {
  249. if (SRCPROPSP (p))
  250. SETSRCPROPCOL (p, scm_to_int (datum));
  251. else
  252. scm_weak_table_putq_x (scm_source_whash, obj,
  253. scm_make_srcprops (0, scm_to_int (datum),
  254. SCM_UNDEFINED, SCM_UNDEFINED, p));
  255. }
  256. else if (scm_is_eq (scm_sym_copy, key))
  257. {
  258. if (SRCPROPSP (p))
  259. SETSRCPROPCOPY (p, datum);
  260. else
  261. scm_weak_table_putq_x (scm_source_whash, obj,
  262. scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
  263. }
  264. else
  265. {
  266. if (SRCPROPSP (p))
  267. SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p)));
  268. else
  269. scm_weak_table_putq_x (scm_source_whash, obj,
  270. scm_acons (key, datum, p));
  271. }
  272. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  273. return SCM_UNSPECIFIED;
  274. }
  275. #undef FUNC_NAME
  276. SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
  277. (SCM xorig, SCM x, SCM y),
  278. "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
  279. "Any source properties associated with @var{xorig} are also associated\n"
  280. "with the new pair.")
  281. #define FUNC_NAME s_scm_cons_source
  282. {
  283. SCM p, z;
  284. z = scm_cons (x, y);
  285. /* Copy source properties possibly associated with xorig. */
  286. p = scm_weak_table_refq (scm_source_whash, xorig, SCM_BOOL_F);
  287. if (scm_is_true (p))
  288. scm_weak_table_putq_x (scm_source_whash, z, p);
  289. return z;
  290. }
  291. #undef FUNC_NAME
  292. void
  293. scm_init_srcprop ()
  294. {
  295. scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
  296. scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
  297. scm_source_whash = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  298. scm_c_define ("source-whash", scm_source_whash);
  299. scm_last_alist_filename = scm_cons (SCM_EOL,
  300. scm_acons (SCM_EOL, SCM_EOL, SCM_EOL));
  301. #include "libguile/srcprop.x"
  302. }
  303. /*
  304. Local Variables:
  305. c-file-style: "gnu"
  306. End:
  307. */