srcprop.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_NIMP (obj) && !scm_is_symbol (obj) && !scm_is_keyword (obj);
  81. }
  82. static int
  83. srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
  84. {
  85. int writingp = SCM_WRITINGP (pstate);
  86. scm_puts ("#<srcprops ", port);
  87. SCM_SET_WRITINGP (pstate, 1);
  88. scm_iprin1 (scm_srcprops_to_alist (obj), port, pstate);
  89. SCM_SET_WRITINGP (pstate, writingp);
  90. scm_putc ('>', port);
  91. return 1;
  92. }
  93. /*
  94. * We remember the last file name settings, so we can share that alist
  95. * entry. This works because scm_set_source_property_x does not use
  96. * assoc-set! for modifying the alist.
  97. *
  98. * This variable contains a protected cons, whose cdr is the cached
  99. * alist
  100. */
  101. static SCM scm_last_alist_filename;
  102. SCM
  103. scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist)
  104. {
  105. if (!SCM_UNBNDP (filename))
  106. {
  107. SCM old_alist = alist;
  108. /*
  109. have to extract the acons, and operate on that, for
  110. thread safety.
  111. */
  112. SCM last_acons = SCM_CDR (scm_last_alist_filename);
  113. if (scm_is_null (old_alist)
  114. && scm_is_eq (SCM_CDAR (last_acons), filename))
  115. {
  116. alist = last_acons;
  117. }
  118. else
  119. {
  120. alist = scm_acons (scm_sym_filename, filename, alist);
  121. if (scm_is_null (old_alist))
  122. scm_set_cdr_x (scm_last_alist_filename, alist);
  123. }
  124. }
  125. SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops,
  126. SRCPROPMAKPOS (line, col),
  127. SCM_UNPACK (copy),
  128. SCM_UNPACK (alist));
  129. }
  130. static SCM
  131. scm_srcprops_to_alist (SCM obj)
  132. {
  133. SCM alist = SRCPROPALIST (obj);
  134. if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
  135. alist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), alist);
  136. alist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), alist);
  137. alist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), alist);
  138. return alist;
  139. }
  140. SCM_DEFINE (scm_supports_source_properties_p, "supports-source-properties?", 1, 0, 0,
  141. (SCM obj),
  142. "Return #t if @var{obj} supports adding source properties,\n"
  143. "otherwise return #f.")
  144. #define FUNC_NAME s_scm_supports_source_properties_p
  145. {
  146. return scm_from_bool (supports_source_props (obj));
  147. }
  148. #undef FUNC_NAME
  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. if (SCM_IMP (obj))
  155. return SCM_EOL;
  156. else
  157. {
  158. SCM p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  159. if (SRCPROPSP (p))
  160. return scm_srcprops_to_alist (p);
  161. else
  162. /* list from set-source-properties!, or SCM_EOL for not found */
  163. return p;
  164. }
  165. }
  166. #undef FUNC_NAME
  167. #define SCM_VALIDATE_NIM(pos, scm) \
  168. SCM_MAKE_VALIDATE_MSG (pos, scm, NIMP, "non-immediate")
  169. /* Perhaps this procedure should look through an alist
  170. and try to make a srcprops-object...? */
  171. SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
  172. (SCM obj, SCM alist),
  173. "Install the association list @var{alist} as the source property\n"
  174. "list for @var{obj}.")
  175. #define FUNC_NAME s_scm_set_source_properties_x
  176. {
  177. SCM_VALIDATE_NIM (1, obj);
  178. scm_weak_table_putq_x (scm_source_whash, obj, alist);
  179. return alist;
  180. }
  181. #undef FUNC_NAME
  182. int
  183. scm_i_has_source_properties (SCM obj)
  184. #define FUNC_NAME "%set-source-properties"
  185. {
  186. if (SCM_IMP (obj))
  187. return 0;
  188. else
  189. return scm_is_true (scm_weak_table_refq (scm_source_whash, obj, SCM_BOOL_F));
  190. }
  191. #undef FUNC_NAME
  192. void
  193. scm_i_set_source_properties_x (SCM obj, long line, int col, SCM fname)
  194. #define FUNC_NAME "%set-source-properties"
  195. {
  196. SCM_VALIDATE_NIM (1, obj);
  197. scm_weak_table_putq_x (scm_source_whash, obj,
  198. scm_make_srcprops (line, col, fname,
  199. SCM_COPY_SOURCE_P
  200. ? scm_copy_tree (obj)
  201. : SCM_UNDEFINED,
  202. SCM_EOL));
  203. }
  204. #undef FUNC_NAME
  205. SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
  206. (SCM obj, SCM key),
  207. "Return the source property specified by @var{key} from\n"
  208. "@var{obj}'s source property list.")
  209. #define FUNC_NAME s_scm_source_property
  210. {
  211. SCM p;
  212. if (SCM_IMP (obj))
  213. return SCM_BOOL_F;
  214. p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  215. if (!SRCPROPSP (p))
  216. goto alist;
  217. if (scm_is_eq (scm_sym_line, key))
  218. return scm_from_int (SRCPROPLINE (p));
  219. else if (scm_is_eq (scm_sym_column, key))
  220. return scm_from_int (SRCPROPCOL (p));
  221. else if (scm_is_eq (scm_sym_copy, key))
  222. return SRCPROPCOPY (p);
  223. else
  224. {
  225. p = SRCPROPALIST (p);
  226. alist:
  227. p = scm_assoc (key, p);
  228. return (scm_is_pair (p) ? SCM_CDR (p) : SCM_BOOL_F);
  229. }
  230. }
  231. #undef FUNC_NAME
  232. SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
  233. (SCM obj, SCM key, SCM datum),
  234. "Set the source property of object @var{obj}, which is specified by\n"
  235. "@var{key} to @var{datum}. Normally, the key will be a symbol.")
  236. #define FUNC_NAME s_scm_set_source_property_x
  237. {
  238. SCM p;
  239. SCM_VALIDATE_NIM (1, obj);
  240. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  241. p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  242. if (scm_is_eq (scm_sym_line, key))
  243. {
  244. if (SRCPROPSP (p))
  245. SETSRCPROPLINE (p, scm_to_int (datum));
  246. else
  247. scm_weak_table_putq_x (scm_source_whash, obj,
  248. scm_make_srcprops (scm_to_int (datum), 0,
  249. SCM_UNDEFINED, SCM_UNDEFINED, p));
  250. }
  251. else if (scm_is_eq (scm_sym_column, key))
  252. {
  253. if (SRCPROPSP (p))
  254. SETSRCPROPCOL (p, scm_to_int (datum));
  255. else
  256. scm_weak_table_putq_x (scm_source_whash, obj,
  257. scm_make_srcprops (0, scm_to_int (datum),
  258. SCM_UNDEFINED, SCM_UNDEFINED, p));
  259. }
  260. else if (scm_is_eq (scm_sym_copy, key))
  261. {
  262. if (SRCPROPSP (p))
  263. SETSRCPROPCOPY (p, datum);
  264. else
  265. scm_weak_table_putq_x (scm_source_whash, obj,
  266. scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
  267. }
  268. else
  269. {
  270. if (SRCPROPSP (p))
  271. SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p)));
  272. else
  273. scm_weak_table_putq_x (scm_source_whash, obj,
  274. scm_acons (key, datum, p));
  275. }
  276. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  277. return SCM_UNSPECIFIED;
  278. }
  279. #undef FUNC_NAME
  280. SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
  281. (SCM xorig, SCM x, SCM y),
  282. "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
  283. "Any source properties associated with @var{xorig} are also associated\n"
  284. "with the new pair.")
  285. #define FUNC_NAME s_scm_cons_source
  286. {
  287. SCM p, z;
  288. z = scm_cons (x, y);
  289. /* Copy source properties possibly associated with xorig. */
  290. p = scm_weak_table_refq (scm_source_whash, xorig, SCM_BOOL_F);
  291. if (scm_is_true (p))
  292. scm_weak_table_putq_x (scm_source_whash, z, p);
  293. return z;
  294. }
  295. #undef FUNC_NAME
  296. void
  297. scm_init_srcprop ()
  298. {
  299. scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
  300. scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
  301. scm_source_whash = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  302. scm_c_define ("source-whash", scm_source_whash);
  303. scm_last_alist_filename = scm_cons (SCM_EOL,
  304. scm_acons (SCM_EOL, SCM_EOL, SCM_EOL));
  305. #include "srcprop.x"
  306. }