]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
apply Julian's patch to fix install-info warnings
[lilypond.git] / lily / grob-property.cc
1 /*
2   Implement storage and manipulation of grob properties.
3 */
4
5 #include <cstring>
6
7 #include "main.hh"
8 #include "input.hh"
9 #include "pointer-group-interface.hh"
10 #include "misc.hh"
11 #include "paper-score.hh"
12 #include "output-def.hh"
13 #include "spanner.hh"
14 #include "international.hh"
15 #include "item.hh"
16 #include "misc.hh"
17 #include "item.hh"
18 #include "program-option.hh"
19 #include "profile.hh"
20 #include "simple-closure.hh"
21 #include "warn.hh"
22 #include "protected-scm.hh"
23
24 Protected_scm grob_property_callback_stack = SCM_EOL;
25
26 extern bool debug_property_callbacks;
27
28 #ifndef NDEBUG
29 static void
30 print_property_callback_stack ()
31 {
32   int frame = 0;
33   for (SCM s = grob_property_callback_stack; scm_is_pair (s); s = scm_cdr (s))
34     message (_f ("%d: %s", frame++, ly_scm_write_string (scm_car (s)).c_str ()));
35 }
36 #endif
37
38 static SCM modification_callback = SCM_EOL;
39 static SCM cache_callback = SCM_EOL;
40
41
42 /*
43
44 FIXME: this should use ly:set-option interface instead.
45
46 */
47
48 LY_DEFINE (ly_set_grob_modification_callback, "ly:set-grob-modification-callback",
49            1, 0, 0, (SCM cb),
50            "Specify a procedure that will be called every time LilyPond"
51            " modifies a grob property.  The callback will receive as"
52            " arguments the grob that is being modified, the name of the"
53            " C++ file in which the modification was requested, the line"
54            " number in the C++ file in which the modification was requested,"
55            " the name of the function in which the modification was"
56            " requested, the property to be changed, and the new value for"
57            " the property.")
58 {
59   modification_callback =  (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
60   return SCM_UNSPECIFIED;
61 }
62
63 LY_DEFINE (ly_set_property_cache_callback, "ly:set-property-cache-callback",
64            1, 0, 0, (SCM cb),
65            "Specify a procedure that will be called whenever lilypond"
66            " calculates a callback function and caches the result.  The"
67            " callback will receive as arguments the grob whose property it"
68            " is, the name of the property, the name of the callback that"
69            " calculated the property, and the new (cached) value of the"
70            " property.")
71 {
72   cache_callback =  (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
73   return SCM_UNSPECIFIED;
74 }
75
76
77 void
78 Grob::instrumented_set_property (SCM sym, SCM v,
79                                  char const *file,
80                                  int line,
81                                  char const *fun)
82 {
83 #ifndef NDEBUG
84   if (ly_is_procedure (modification_callback))
85     scm_apply_0 (modification_callback,
86                  scm_list_n (self_scm (),
87                              scm_from_locale_string (file),
88                              scm_from_int (line),
89                              scm_from_locale_string (fun),
90                              sym, v, SCM_UNDEFINED));
91 #else
92   (void) file;
93   (void) line;
94   (void) fun;
95 #endif
96   
97   internal_set_property (sym, v);
98 }
99
100 SCM
101 Grob::get_property_alist_chain (SCM def) const
102 {
103   return scm_list_n (mutable_property_alist_,
104                      immutable_property_alist_,
105                      def,
106                      SCM_UNDEFINED);
107 }
108
109 extern void check_interfaces_for_property (Grob const *me, SCM sym);
110
111 void
112 Grob::internal_set_property (SCM sym, SCM v)
113 {
114   internal_set_value_on_alist (&mutable_property_alist_,
115                                sym, v);
116
117 }
118
119 void
120 Grob::internal_set_value_on_alist (SCM *alist, SCM sym, SCM v)
121 {
122   /* Perhaps we simply do the assq_set, but what the heck. */
123   if (!is_live ())
124     return;
125
126   if (do_internal_type_checking_global)
127     {
128       if (!ly_is_procedure (v)
129           && !is_simple_closure (v)
130           && v != ly_symbol2scm ("calculation-in-progress"))
131         type_check_assignment (sym, v, ly_symbol2scm ("backend-type?"));
132
133       check_interfaces_for_property (this, sym);
134     }
135
136   *alist = scm_assq_set_x (*alist, sym, v);
137 }
138
139 SCM
140 Grob::internal_get_property_data (SCM sym) const
141 {
142 #ifndef NDEBUG
143   if (profile_property_accesses)
144     note_property_access (&grob_property_lookup_table, sym);
145 #endif
146   
147   SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
148   if (handle != SCM_BOOL_F)
149     return scm_cdr (handle);
150
151   handle = scm_sloppy_assq (sym, immutable_property_alist_);
152
153   if (do_internal_type_checking_global && scm_is_pair (handle))
154     {
155       SCM val = scm_cdr (handle);
156       if (!ly_is_procedure (val) && !is_simple_closure (val))
157         type_check_assignment (sym, val, ly_symbol2scm ("backend-type?"));
158
159       check_interfaces_for_property (this, sym);
160     }
161   
162   return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
163 }
164
165 SCM
166 Grob::internal_get_property (SCM sym) const
167 {
168   SCM val = get_property_data (sym);
169
170 #ifndef NDEBUG
171   if (val == ly_symbol2scm ("calculation-in-progress"))
172     {
173       programming_error (_f ("cyclic dependency: calculation-in-progress encountered for #'%s (%s)",
174                              ly_symbol2string (sym).c_str (),
175                              name ().c_str ()));
176       if (debug_property_callbacks)
177         {
178           message ("backtrace: ");
179           print_property_callback_stack ();
180         }
181     }
182 #endif
183   
184   if (ly_is_procedure (val)
185       || is_simple_closure (val))
186     {
187       Grob *me = ((Grob*)this);
188       val = me->try_callback_on_alist (&me->mutable_property_alist_, sym, val);
189     }
190   
191   return val;
192 }
193
194 /* Unlike internal_get_property, this function does no caching. Use it, therefore, with caution. */
195 SCM
196 Grob::internal_get_pure_property (SCM sym, int start, int end) const
197 {
198   SCM val = internal_get_property_data (sym);
199   if (ly_is_procedure (val))
200     return call_pure_function (val, scm_list_1 (self_scm ()), start, end);
201   if (is_simple_closure (val))
202     return evaluate_with_simple_closure (self_scm (),
203                                          simple_closure_expression (val),
204                                          true, start, end);
205   return val;
206 }
207
208 SCM
209 Grob::internal_get_maybe_pure_property (SCM sym, bool pure, int start, int end) const
210 {
211   return pure ? internal_get_pure_property (sym, start, end) : internal_get_property (sym);
212 }
213
214 SCM
215 Grob::try_callback_on_alist (SCM *alist, SCM sym, SCM proc)
216 {      
217   SCM marker = ly_symbol2scm ("calculation-in-progress");
218   /*
219     need to put a value in SYM to ensure that we don't get a
220     cyclic call chain.
221   */
222   *alist = scm_assq_set_x (*alist, sym, marker);
223
224 #ifndef NDEBUG
225   if (debug_property_callbacks)
226     grob_property_callback_stack = scm_cons (scm_list_3 (self_scm (), sym, proc), grob_property_callback_stack);
227 #endif
228
229   SCM value = SCM_EOL;
230   if (ly_is_procedure (proc))
231     value = scm_call_1 (proc, self_scm ());
232   else if (is_simple_closure (proc))
233     {
234       value = evaluate_with_simple_closure (self_scm (),
235                                             simple_closure_expression (proc),
236                                             false, 0, 0);
237     }
238   
239 #ifndef NDEBUG
240   if (debug_property_callbacks)
241     grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
242 #endif
243           
244   /*
245     If the function returns SCM_UNSPECIFIED, we assume the
246     property has been set with an explicit set_property ()
247     call.
248   */
249   if (value == SCM_UNSPECIFIED)
250     {
251       value = get_property_data (sym);
252       assert (value == SCM_EOL || value == marker);
253       if (value == marker)
254         *alist = scm_assq_remove_x (*alist, marker);
255     }
256   else
257     {
258 #ifndef NDEBUG
259       if (ly_is_procedure (cache_callback))
260         scm_apply_0 (cache_callback,
261                      scm_list_n (self_scm (),
262                                  sym,
263                                  proc,
264                                  value,
265                                  SCM_UNDEFINED));
266 #endif
267       internal_set_value_on_alist (alist, sym, value);
268     }
269   
270   return value;
271 }
272
273 void
274 Grob::internal_set_object (SCM s, SCM v)
275 {
276   /* Perhaps we simply do the assq_set, but what the heck. */
277   if (!is_live ())
278     return;
279
280   object_alist_ = scm_assq_set_x (object_alist_, s, v);
281 }
282
283 void
284 Grob::internal_del_property (SCM sym)
285 {
286   mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
287 }
288
289 SCM
290 Grob::internal_get_object (SCM sym) const
291 {
292   if (profile_property_accesses)
293     note_property_access (&grob_property_lookup_table, sym);
294
295   SCM s = scm_sloppy_assq (sym, object_alist_);
296   
297   if (s != SCM_BOOL_F)
298     {
299       SCM val = scm_cdr (s);
300       if (ly_is_procedure (val)
301           || is_simple_closure (val))
302         {
303           Grob *me = ((Grob*)this);
304           val = me->try_callback_on_alist (&me->object_alist_, sym, val);
305         }
306       
307       return val;
308     }
309
310   return SCM_EOL;
311 }
312
313 bool
314 Grob::is_live () const
315 {
316   return scm_is_pair (immutable_property_alist_);
317 }
318
319 bool
320 Grob::internal_has_interface (SCM k)
321 {
322   return scm_c_memq (k, interfaces_) != SCM_BOOL_F;
323 }
324
325 SCM
326 call_pure_function (SCM unpure, SCM args, int start, int end)
327 {
328   SCM scm_call_pure_function = ly_lily_module_constant ("call-pure-function");
329
330   return scm_apply_0 (scm_call_pure_function,
331                       scm_list_4 (unpure, args, scm_from_int (start), scm_from_int (end)));
332 }
333