srcprop.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* Copyright 1995-2002,2006,2008-2012,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <errno.h>
  19. #include "alist.h"
  20. #include "async.h"
  21. #include "debug.h"
  22. #include "gc.h"
  23. #include "gsubr.h"
  24. #include "hash.h"
  25. #include "hashtab.h"
  26. #include "keywords.h"
  27. #include "list.h"
  28. #include "modules.h"
  29. #include "numbers.h"
  30. #include "pairs.h"
  31. #include "ports.h"
  32. #include "private-options.h"
  33. #include "smob.h"
  34. #include "symbols.h"
  35. #include "weak-table.h"
  36. #include "srcprop.h"
  37. /* {Source Properties}
  38. *
  39. * Properties of source list expressions.
  40. * Four of these have special meaning:
  41. *
  42. * filename string The name of the source file.
  43. * copy list A copy of the list expression.
  44. * line integer The source code line number.
  45. * column integer The source code column number.
  46. *
  47. * Most properties above can be set by the reader.
  48. *
  49. */
  50. SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
  51. SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
  52. SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
  53. SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
  54. static SCM scm_source_whash;
  55. /*
  56. * Source properties are stored as double cells with the
  57. * following layout:
  58. * car = tag
  59. * cbr = pos
  60. * ccr = copy
  61. * cdr = alist
  62. */
  63. #define SRCPROPSP(p) (SCM_SMOB_PREDICATE (scm_tc16_srcprops, (p)))
  64. #define SRCPROPPOS(p) (SCM_SMOB_DATA(p))
  65. #define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12)
  66. #define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL)
  67. #define SRCPROPCOPY(p) (SCM_SMOB_OBJECT_2(p))
  68. #define SRCPROPALIST(p) (SCM_SMOB_OBJECT_3(p))
  69. #define SRCPROPMAKPOS(l, c) (((l) << 12) + (c))
  70. #define SETSRCPROPPOS(p, l, c) (SCM_SET_SMOB_DATA_1 (p, SRCPROPMAKPOS (l, c)))
  71. #define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p))
  72. #define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c)
  73. #define SETSRCPROPCOPY(p, c) (SCM_SET_SMOB_OBJECT_2 (p, c))
  74. #define SETSRCPROPALIST(p, l) (SCM_SET_SMOB_OBJECT_3 (p, l))
  75. static SCM scm_srcprops_to_alist (SCM obj);
  76. scm_t_bits scm_tc16_srcprops;
  77. static int
  78. supports_source_props (SCM obj)
  79. {
  80. return (SCM_THOB_P (obj)
  81. ? (!scm_is_symbol (obj) && !scm_is_keyword (obj))
  82. : scm_is_pair (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_alist (obj), port, pstate);
  91. SCM_SET_WRITINGP (pstate, writingp);
  92. scm_putc ('>', port);
  93. return 1;
  94. }
  95. /*
  96. * We remember the last file name settings, so we can share that alist
  97. * entry. This works because scm_set_source_property_x does not use
  98. * assoc-set! for modifying the alist.
  99. *
  100. * This variable contains a protected cons, whose cdr is the cached
  101. * alist
  102. */
  103. static SCM scm_last_alist_filename;
  104. SCM
  105. scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist)
  106. {
  107. if (!SCM_UNBNDP (filename))
  108. {
  109. SCM old_alist = alist;
  110. /*
  111. have to extract the acons, and operate on that, for
  112. thread safety.
  113. */
  114. SCM last_acons = SCM_CDR (scm_last_alist_filename);
  115. if (scm_is_null (old_alist)
  116. && scm_is_eq (SCM_CDAR (last_acons), filename))
  117. {
  118. alist = last_acons;
  119. }
  120. else
  121. {
  122. alist = scm_acons (scm_sym_filename, filename, alist);
  123. if (scm_is_null (old_alist))
  124. scm_set_cdr_x (scm_last_alist_filename, alist);
  125. }
  126. }
  127. SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops,
  128. SRCPROPMAKPOS (line, col),
  129. SCM_UNPACK (copy),
  130. SCM_UNPACK (alist));
  131. }
  132. static SCM
  133. scm_srcprops_to_alist (SCM obj)
  134. {
  135. SCM alist = SRCPROPALIST (obj);
  136. if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
  137. alist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), alist);
  138. alist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), alist);
  139. alist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), alist);
  140. return alist;
  141. }
  142. SCM_DEFINE (scm_supports_source_properties_p, "supports-source-properties?", 1, 0, 0,
  143. (SCM obj),
  144. "Return #t if @var{obj} supports adding source properties,\n"
  145. "otherwise return #f.")
  146. #define FUNC_NAME s_scm_supports_source_properties_p
  147. {
  148. return scm_from_bool (supports_source_props (obj));
  149. }
  150. #undef FUNC_NAME
  151. SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
  152. (SCM obj),
  153. "Return the source property association list of @var{obj}.")
  154. #define FUNC_NAME s_scm_source_properties
  155. {
  156. if (!SCM_HEAP_OBJECT_P (obj))
  157. return SCM_EOL;
  158. else
  159. {
  160. SCM p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  161. if (SRCPROPSP (p))
  162. return scm_srcprops_to_alist (p);
  163. else
  164. /* list from set-source-properties!, or SCM_EOL for not found */
  165. return p;
  166. }
  167. }
  168. #undef FUNC_NAME
  169. #define SCM_VALIDATE_NIM(pos, scm) \
  170. SCM_MAKE_VALIDATE_MSG (pos, scm, HEAP_OBJECT_P, "non-immediate")
  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 alist),
  175. "Install the association list @var{alist} as the source property\n"
  176. "list for @var{obj}.")
  177. #define FUNC_NAME s_scm_set_source_properties_x
  178. {
  179. SCM_VALIDATE_NIM (1, obj);
  180. scm_weak_table_putq_x (scm_source_whash, obj, alist);
  181. return alist;
  182. }
  183. #undef FUNC_NAME
  184. int
  185. scm_i_has_source_properties (SCM obj)
  186. #define FUNC_NAME "%set-source-properties"
  187. {
  188. if (!SCM_HEAP_OBJECT_P (obj))
  189. return 0;
  190. else
  191. return scm_is_true (scm_weak_table_refq (scm_source_whash, obj, SCM_BOOL_F));
  192. }
  193. #undef FUNC_NAME
  194. void
  195. scm_i_set_source_properties_x (SCM obj, long line, int col, SCM fname)
  196. #define FUNC_NAME "%set-source-properties"
  197. {
  198. SCM_VALIDATE_NIM (1, obj);
  199. scm_weak_table_putq_x (scm_source_whash, obj,
  200. scm_make_srcprops (line, col, fname,
  201. SCM_COPY_SOURCE_P
  202. ? scm_copy_tree (obj)
  203. : SCM_UNDEFINED,
  204. SCM_EOL));
  205. }
  206. #undef FUNC_NAME
  207. SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
  208. (SCM obj, SCM key),
  209. "Return the source property specified by @var{key} from\n"
  210. "@var{obj}'s source property list.")
  211. #define FUNC_NAME s_scm_source_property
  212. {
  213. SCM p;
  214. if (!SCM_HEAP_OBJECT_P (obj))
  215. return SCM_BOOL_F;
  216. p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  217. if (!SRCPROPSP (p))
  218. goto alist;
  219. if (scm_is_eq (scm_sym_line, key))
  220. return scm_from_int (SRCPROPLINE (p));
  221. else if (scm_is_eq (scm_sym_column, key))
  222. return scm_from_int (SRCPROPCOL (p));
  223. else if (scm_is_eq (scm_sym_copy, key))
  224. return SRCPROPCOPY (p);
  225. else
  226. {
  227. p = SRCPROPALIST (p);
  228. alist:
  229. p = scm_assoc (key, p);
  230. return (scm_is_pair (p) ? SCM_CDR (p) : SCM_BOOL_F);
  231. }
  232. }
  233. #undef FUNC_NAME
  234. SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
  235. (SCM obj, SCM key, SCM datum),
  236. "Set the source property of object @var{obj}, which is specified by\n"
  237. "@var{key} to @var{datum}. Normally, the key will be a symbol.")
  238. #define FUNC_NAME s_scm_set_source_property_x
  239. {
  240. SCM p;
  241. SCM_VALIDATE_NIM (1, obj);
  242. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  243. p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  244. if (scm_is_eq (scm_sym_line, key))
  245. {
  246. if (SRCPROPSP (p))
  247. SETSRCPROPLINE (p, scm_to_int (datum));
  248. else
  249. scm_weak_table_putq_x (scm_source_whash, obj,
  250. scm_make_srcprops (scm_to_int (datum), 0,
  251. SCM_UNDEFINED, SCM_UNDEFINED, p));
  252. }
  253. else if (scm_is_eq (scm_sym_column, key))
  254. {
  255. if (SRCPROPSP (p))
  256. SETSRCPROPCOL (p, scm_to_int (datum));
  257. else
  258. scm_weak_table_putq_x (scm_source_whash, obj,
  259. scm_make_srcprops (0, scm_to_int (datum),
  260. SCM_UNDEFINED, SCM_UNDEFINED, p));
  261. }
  262. else if (scm_is_eq (scm_sym_copy, key))
  263. {
  264. if (SRCPROPSP (p))
  265. SETSRCPROPCOPY (p, datum);
  266. else
  267. scm_weak_table_putq_x (scm_source_whash, obj,
  268. scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
  269. }
  270. else
  271. {
  272. if (SRCPROPSP (p))
  273. SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p)));
  274. else
  275. scm_weak_table_putq_x (scm_source_whash, obj,
  276. scm_acons (key, datum, p));
  277. }
  278. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  279. return SCM_UNSPECIFIED;
  280. }
  281. #undef FUNC_NAME
  282. SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
  283. (SCM xorig, SCM x, SCM y),
  284. "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
  285. "Any source properties associated with @var{xorig} are also associated\n"
  286. "with the new pair.")
  287. #define FUNC_NAME s_scm_cons_source
  288. {
  289. SCM p, z;
  290. z = scm_cons (x, y);
  291. /* Copy source properties possibly associated with xorig. */
  292. p = scm_weak_table_refq (scm_source_whash, xorig, SCM_BOOL_F);
  293. if (scm_is_true (p))
  294. scm_weak_table_putq_x (scm_source_whash, z, p);
  295. return z;
  296. }
  297. #undef FUNC_NAME
  298. void
  299. scm_init_srcprop ()
  300. {
  301. scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
  302. scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
  303. scm_source_whash = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  304. scm_c_define ("source-whash", scm_source_whash);
  305. scm_last_alist_filename = scm_cons (SCM_EOL,
  306. scm_acons (SCM_EOL, SCM_EOL, SCM_EOL));
  307. #include "srcprop.x"
  308. }