srcprop.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* Copyright 1995-2002,2006,2008-2012,2018,2020
  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. * Three of these have special meaning:
  41. *
  42. * filename The name of the source file.
  43. * line The source code line number.
  44. * column 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_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 | col (untagged)
  57. * cbr = line
  58. * ccr = filename
  59. * cdr = alist
  60. */
  61. static scm_t_bits tc16_srcprops;
  62. #define SRCPROPSP(p) (SCM_SMOB_PREDICATE (tc16_srcprops, (p)))
  63. #define SRCPROPCOL(p) (scm_from_int (SCM_SMOB_FLAGS (p)))
  64. #define SRCPROPLINE(p) (SCM_SMOB_OBJECT_1 (p))
  65. #define SRCPROPFNAME(p) (SCM_SMOB_OBJECT_2 (p))
  66. #define SRCPROPALIST(p) (SCM_SMOB_OBJECT_3 (p))
  67. #define SETSRCPROPCOL(p, c) (SCM_SET_SMOB_FLAGS (p, scm_to_int (c)))
  68. #define SETSRCPROPLINE(p, l) (SCM_SET_SMOB_OBJECT_1 (p, l))
  69. #define SETSRCPROPFNAME(p, x) (SCM_SET_SMOB_OBJECT_2 (p, x))
  70. #define SETSRCPROPALIST(p, x) (SCM_SET_SMOB_OBJECT_3 (p, x))
  71. static SCM scm_srcprops_to_alist (SCM obj);
  72. static int
  73. supports_source_props (SCM obj)
  74. {
  75. return SCM_NIMP (obj) && !scm_is_symbol (obj) && !scm_is_keyword (obj);
  76. }
  77. static int
  78. srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
  79. {
  80. int writingp = SCM_WRITINGP (pstate);
  81. scm_puts ("#<srcprops ", port);
  82. SCM_SET_WRITINGP (pstate, 1);
  83. scm_iprin1 (scm_srcprops_to_alist (obj), port, pstate);
  84. SCM_SET_WRITINGP (pstate, writingp);
  85. scm_putc ('>', port);
  86. return 1;
  87. }
  88. SCM
  89. scm_i_make_srcprops (SCM line, SCM col, SCM filename, SCM alist)
  90. {
  91. SCM_RETURN_NEWSMOB3 (tc16_srcprops | (scm_to_int (col) << 16),
  92. SCM_UNPACK (line),
  93. SCM_UNPACK (filename),
  94. SCM_UNPACK (alist));
  95. }
  96. static SCM
  97. scm_srcprops_to_alist (SCM obj)
  98. {
  99. SCM alist = SRCPROPALIST (obj);
  100. if (scm_is_true (SRCPROPFNAME (obj)))
  101. alist = scm_acons (scm_sym_filename, SRCPROPFNAME (obj), alist);
  102. alist = scm_acons (scm_sym_column, SRCPROPCOL (obj), alist);
  103. alist = scm_acons (scm_sym_line, SRCPROPLINE (obj), alist);
  104. return alist;
  105. }
  106. SCM_DEFINE (scm_supports_source_properties_p, "supports-source-properties?", 1, 0, 0,
  107. (SCM obj),
  108. "Return #t if @var{obj} supports adding source properties,\n"
  109. "otherwise return #f.")
  110. #define FUNC_NAME s_scm_supports_source_properties_p
  111. {
  112. return scm_from_bool (supports_source_props (obj));
  113. }
  114. #undef FUNC_NAME
  115. SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
  116. (SCM obj),
  117. "Return the source property association list of @var{obj}.")
  118. #define FUNC_NAME s_scm_source_properties
  119. {
  120. if (SCM_IMP (obj))
  121. return SCM_EOL;
  122. else
  123. {
  124. SCM p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  125. if (SRCPROPSP (p))
  126. return scm_srcprops_to_alist (p);
  127. else
  128. /* list from set-source-properties!, or SCM_EOL for not found */
  129. return p;
  130. }
  131. }
  132. #undef FUNC_NAME
  133. #define SCM_VALIDATE_NIM(pos, scm) \
  134. SCM_MAKE_VALIDATE_MSG (pos, scm, NIMP, "non-immediate")
  135. /* Perhaps this procedure should look through an alist
  136. and try to make a srcprops-object...? */
  137. SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
  138. (SCM obj, SCM alist),
  139. "Install the association list @var{alist} as the source property\n"
  140. "list for @var{obj}.")
  141. #define FUNC_NAME s_scm_set_source_properties_x
  142. {
  143. SCM_VALIDATE_NIM (1, obj);
  144. scm_weak_table_putq_x (scm_source_whash, obj, alist);
  145. return alist;
  146. }
  147. #undef FUNC_NAME
  148. int
  149. scm_i_has_source_properties (SCM obj)
  150. #define FUNC_NAME "%set-source-properties"
  151. {
  152. if (SCM_IMP (obj))
  153. return 0;
  154. else
  155. return scm_is_true (scm_weak_table_refq (scm_source_whash, obj, SCM_BOOL_F));
  156. }
  157. #undef FUNC_NAME
  158. void
  159. scm_i_set_source_properties_x (SCM obj, SCM line, SCM col, SCM fname)
  160. #define FUNC_NAME "%set-source-properties"
  161. {
  162. SCM_VALIDATE_NIM (1, obj);
  163. scm_weak_table_putq_x (scm_source_whash, obj,
  164. scm_i_make_srcprops (line, col, fname, SCM_EOL));
  165. }
  166. #undef FUNC_NAME
  167. SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
  168. (SCM obj, SCM key),
  169. "Return the source property specified by @var{key} from\n"
  170. "@var{obj}'s source property list.")
  171. #define FUNC_NAME s_scm_source_property
  172. {
  173. SCM p;
  174. if (SCM_IMP (obj))
  175. return SCM_BOOL_F;
  176. p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  177. if (!SRCPROPSP (p))
  178. goto alist;
  179. if (scm_is_eq (scm_sym_line, key))
  180. return SRCPROPLINE (p);
  181. else if (scm_is_eq (scm_sym_column, key))
  182. return SRCPROPCOL (p);
  183. else if (scm_is_eq (scm_sym_filename, key))
  184. return SRCPROPFNAME (p);
  185. else
  186. {
  187. p = SRCPROPALIST (p);
  188. alist:
  189. p = scm_assoc (key, p);
  190. return (scm_is_pair (p) ? SCM_CDR (p) : SCM_BOOL_F);
  191. }
  192. }
  193. #undef FUNC_NAME
  194. static scm_i_pthread_mutex_t source_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  195. SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
  196. (SCM obj, SCM key, SCM datum),
  197. "Set the source property of object @var{obj}, which is specified by\n"
  198. "@var{key} to @var{datum}. Normally, the key will be a symbol.")
  199. #define FUNC_NAME s_scm_set_source_property_x
  200. {
  201. SCM p;
  202. SCM_VALIDATE_NIM (1, obj);
  203. scm_i_pthread_mutex_lock (&source_mutex);
  204. p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
  205. if (scm_is_eq (scm_sym_line, key))
  206. {
  207. if (SRCPROPSP (p))
  208. SETSRCPROPLINE (p, datum);
  209. else
  210. scm_weak_table_putq_x (scm_source_whash, obj,
  211. scm_i_make_srcprops (datum, SCM_INUM0,
  212. SCM_BOOL_F, p));
  213. }
  214. else if (scm_is_eq (scm_sym_column, key))
  215. {
  216. if (SRCPROPSP (p))
  217. SETSRCPROPCOL (p, datum);
  218. else
  219. scm_weak_table_putq_x (scm_source_whash, obj,
  220. scm_i_make_srcprops (SCM_INUM0, datum,
  221. SCM_BOOL_F, p));
  222. }
  223. else if (scm_is_eq (scm_sym_filename, key))
  224. {
  225. if (SRCPROPSP (p))
  226. SETSRCPROPFNAME (p, datum);
  227. else
  228. scm_weak_table_putq_x (scm_source_whash, obj,
  229. scm_i_make_srcprops (SCM_INUM0, SCM_INUM0,
  230. datum, p));
  231. }
  232. else
  233. {
  234. if (SRCPROPSP (p))
  235. SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p)));
  236. else
  237. scm_weak_table_putq_x (scm_source_whash, obj,
  238. scm_acons (key, datum, p));
  239. }
  240. scm_i_pthread_mutex_unlock (&source_mutex);
  241. return SCM_UNSPECIFIED;
  242. }
  243. #undef FUNC_NAME
  244. SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
  245. (SCM xorig, SCM x, SCM y),
  246. "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
  247. "Any source properties associated with @var{xorig} are also associated\n"
  248. "with the new pair.")
  249. #define FUNC_NAME s_scm_cons_source
  250. {
  251. SCM p, z;
  252. z = scm_cons (x, y);
  253. /* Copy source properties possibly associated with xorig. */
  254. p = scm_weak_table_refq (scm_source_whash, xorig, SCM_BOOL_F);
  255. if (scm_is_true (p))
  256. scm_weak_table_putq_x (scm_source_whash, z, p);
  257. return z;
  258. }
  259. #undef FUNC_NAME
  260. void
  261. scm_init_srcprop ()
  262. {
  263. tc16_srcprops = scm_make_smob_type ("srcprops", 0);
  264. scm_set_smob_print (tc16_srcprops, srcprops_print);
  265. scm_source_whash = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  266. scm_c_define ("source-whash", scm_source_whash);
  267. #include "srcprop.x"
  268. }