PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/libguile/srcprop.c

#
C | 364 lines | 250 code | 53 blank | 61 comment | 27 complexity | 636aec6ea541a6d437f18380338b9326 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, GPL-2.0
  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/root.h"
  32. #include "libguile/gc.h"
  33. #include "libguile/validate.h"
  34. #include "libguile/srcprop.h"
  35. #include "libguile/private-options.h"
  36. /* {Source Properties}
  37. *
  38. * Properties of source list expressions.
  39. * Four of these have special meaning:
  40. *
  41. * filename string The name of the source file.
  42. * copy list A copy of the list expression.
  43. * line integer The source code line number.
  44. * column integer The source code column number.
  45. *
  46. * Most properties above can be set by the reader.
  47. *
  48. */
  49. SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
  50. SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
  51. SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
  52. SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
  53. static SCM scm_source_whash;
  54. /*
  55. * Source properties are stored as double cells with the
  56. * following layout:
  57. * car = tag
  58. * cbr = pos
  59. * ccr = copy
  60. * cdr = alist
  61. */
  62. #define SRCPROPSP(p) (SCM_SMOB_PREDICATE (scm_tc16_srcprops, (p)))
  63. #define SRCPROPPOS(p) (SCM_SMOB_DATA(p))
  64. #define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12)
  65. #define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL)
  66. #define SRCPROPCOPY(p) (SCM_SMOB_OBJECT_2(p))
  67. #define SRCPROPALIST(p) (SCM_SMOB_OBJECT_3(p))
  68. #define SRCPROPMAKPOS(l, c) (((l) << 12) + (c))
  69. #define SETSRCPROPPOS(p, l, c) (SCM_SET_SMOB_DATA_1 (p, SRCPROPMAKPOS (l, c)))
  70. #define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p))
  71. #define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c)
  72. #define SETSRCPROPCOPY(p, c) (SCM_SET_SMOB_OBJECT_2 (p, c))
  73. #define SETSRCPROPALIST(p, l) (SCM_SET_SMOB_OBJECT_3 (p, l))
  74. static SCM scm_srcprops_to_alist (SCM obj);
  75. scm_t_bits scm_tc16_srcprops;
  76. static int
  77. supports_source_props (SCM obj)
  78. {
  79. return SCM_NIMP (obj) && !scm_is_symbol (obj) && !scm_is_keyword (obj);
  80. }
  81. static int
  82. srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
  83. {
  84. int writingp = SCM_WRITINGP (pstate);
  85. scm_puts_unlocked ("#<srcprops ", port);
  86. SCM_SET_WRITINGP (pstate, 1);
  87. scm_iprin1 (scm_srcprops_to_alist (obj), port, pstate);
  88. SCM_SET_WRITINGP (pstate, writingp);
  89. scm_putc_unlocked ('>', port);
  90. return 1;
  91. }
  92. /*
  93. * We remember the last file name settings, so we can share that alist
  94. * entry. This works because scm_set_source_property_x does not use
  95. * assoc-set! for modifying the alist.
  96. *
  97. * This variable contains a protected cons, whose cdr is the cached
  98. * alist
  99. */
  100. static SCM scm_last_alist_filename;
  101. SCM
  102. scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist)
  103. {
  104. if (!SCM_UNBNDP (filename))
  105. {
  106. SCM old_alist = alist;
  107. /*
  108. have to extract the acons, and operate on that, for
  109. thread safety.
  110. */
  111. SCM last_acons = SCM_CDR (scm_last_alist_filename);
  112. if (scm_is_null (old_alist)
  113. && scm_is_eq (SCM_CDAR (last_acons), filename))
  114. {
  115. alist = last_acons;
  116. }
  117. else
  118. {
  119. alist = scm_acons (scm_sym_filename, filename, alist);
  120. if (scm_is_null (old_alist))
  121. SCM_SETCDR (scm_last_alist_filename, alist);
  122. }
  123. }
  124. SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops,
  125. SRCPROPMAKPOS (line, col),
  126. SCM_UNPACK (copy),
  127. SCM_UNPACK (alist));
  128. }
  129. static SCM
  130. scm_srcprops_to_alist (SCM obj)
  131. {
  132. SCM alist = SRCPROPALIST (obj);
  133. if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
  134. alist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), alist);
  135. alist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), alist);
  136. alist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), alist);
  137. return alist;
  138. }
  139. SCM_DEFINE (scm_supports_source_properties_p, "supports-source-properties?", 1, 0, 0,
  140. (SCM obj),
  141. "Return #t if @var{obj} supports adding source properties,\n"
  142. "otherwise return #f.")
  143. #define FUNC_NAME s_scm_supports_source_properties_p
  144. {
  145. return scm_from_bool (supports_source_props (obj));
  146. }
  147. #undef FUNC_NAME
  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. if (SCM_IMP (obj))
  154. return SCM_EOL;
  155. else
  156. {
  157. SCM p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  158. if (SRCPROPSP (p))
  159. return scm_srcprops_to_alist (p);
  160. else
  161. /* list from set-source-properties!, or SCM_EOL for not found */
  162. return p;
  163. }
  164. }
  165. #undef FUNC_NAME
  166. /* Perhaps this procedure should look through an alist
  167. and try to make a srcprops-object...? */
  168. SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
  169. (SCM obj, SCM alist),
  170. "Install the association list @var{alist} as the source property\n"
  171. "list for @var{obj}.")
  172. #define FUNC_NAME s_scm_set_source_properties_x
  173. {
  174. SCM_VALIDATE_NIM (1, obj);
  175. scm_weak_table_putq_x (scm_source_whash, obj, alist);
  176. return alist;
  177. }
  178. #undef FUNC_NAME
  179. int
  180. scm_i_has_source_properties (SCM obj)
  181. #define FUNC_NAME "%set-source-properties"
  182. {
  183. if (SCM_IMP (obj))
  184. return 0;
  185. else
  186. return scm_is_true (scm_weak_table_refq (scm_source_whash, obj, SCM_BOOL_F));
  187. }
  188. #undef FUNC_NAME
  189. void
  190. scm_i_set_source_properties_x (SCM obj, long line, int col, SCM fname)
  191. #define FUNC_NAME "%set-source-properties"
  192. {
  193. SCM_VALIDATE_NIM (1, obj);
  194. scm_weak_table_putq_x (scm_source_whash, obj,
  195. scm_make_srcprops (line, col, fname,
  196. SCM_COPY_SOURCE_P
  197. ? scm_copy_tree (obj)
  198. : SCM_UNDEFINED,
  199. SCM_EOL));
  200. }
  201. #undef FUNC_NAME
  202. SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
  203. (SCM obj, SCM key),
  204. "Return the source property specified by @var{key} from\n"
  205. "@var{obj}'s source property list.")
  206. #define FUNC_NAME s_scm_source_property
  207. {
  208. SCM p;
  209. if (SCM_IMP (obj))
  210. return SCM_BOOL_F;
  211. p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  212. if (!SRCPROPSP (p))
  213. goto alist;
  214. if (scm_is_eq (scm_sym_line, key))
  215. return scm_from_int (SRCPROPLINE (p));
  216. else if (scm_is_eq (scm_sym_column, key))
  217. return scm_from_int (SRCPROPCOL (p));
  218. else if (scm_is_eq (scm_sym_copy, key))
  219. return SRCPROPCOPY (p);
  220. else
  221. {
  222. p = SRCPROPALIST (p);
  223. alist:
  224. p = scm_assoc (key, p);
  225. return (scm_is_pair (p) ? SCM_CDR (p) : SCM_BOOL_F);
  226. }
  227. }
  228. #undef FUNC_NAME
  229. SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
  230. (SCM obj, SCM key, SCM datum),
  231. "Set the source property of object @var{obj}, which is specified by\n"
  232. "@var{key} to @var{datum}. Normally, the key will be a symbol.")
  233. #define FUNC_NAME s_scm_set_source_property_x
  234. {
  235. SCM p;
  236. SCM_VALIDATE_NIM (1, obj);
  237. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  238. p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  239. if (scm_is_eq (scm_sym_line, key))
  240. {
  241. if (SRCPROPSP (p))
  242. SETSRCPROPLINE (p, scm_to_int (datum));
  243. else
  244. scm_weak_table_putq_x (scm_source_whash, obj,
  245. scm_make_srcprops (scm_to_int (datum), 0,
  246. SCM_UNDEFINED, SCM_UNDEFINED, p));
  247. }
  248. else if (scm_is_eq (scm_sym_column, key))
  249. {
  250. if (SRCPROPSP (p))
  251. SETSRCPROPCOL (p, scm_to_int (datum));
  252. else
  253. scm_weak_table_putq_x (scm_source_whash, obj,
  254. scm_make_srcprops (0, scm_to_int (datum),
  255. SCM_UNDEFINED, SCM_UNDEFINED, p));
  256. }
  257. else if (scm_is_eq (scm_sym_copy, key))
  258. {
  259. if (SRCPROPSP (p))
  260. SETSRCPROPCOPY (p, datum);
  261. else
  262. scm_weak_table_putq_x (scm_source_whash, obj,
  263. scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
  264. }
  265. else
  266. {
  267. if (SRCPROPSP (p))
  268. SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p)));
  269. else
  270. scm_weak_table_putq_x (scm_source_whash, obj,
  271. scm_acons (key, datum, p));
  272. }
  273. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  274. return SCM_UNSPECIFIED;
  275. }
  276. #undef FUNC_NAME
  277. SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
  278. (SCM xorig, SCM x, SCM y),
  279. "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
  280. "Any source properties associated with @var{xorig} are also associated\n"
  281. "with the new pair.")
  282. #define FUNC_NAME s_scm_cons_source
  283. {
  284. SCM p, z;
  285. z = scm_cons (x, y);
  286. /* Copy source properties possibly associated with xorig. */
  287. p = scm_weak_table_refq (scm_source_whash, xorig, SCM_BOOL_F);
  288. if (scm_is_true (p))
  289. scm_weak_table_putq_x (scm_source_whash, z, p);
  290. return z;
  291. }
  292. #undef FUNC_NAME
  293. void
  294. scm_init_srcprop ()
  295. {
  296. scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
  297. scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
  298. scm_source_whash = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  299. scm_c_define ("source-whash", scm_source_whash);
  300. scm_last_alist_filename = 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. */