1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 2002--2020 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /*
21   Implement storage and manipulation of grob properties.
22 */
23 
24 #include "main.hh"
25 #include "input.hh"
26 #include "pointer-group-interface.hh"
27 #include "misc.hh"
28 #include "paper-score.hh"
29 #include "output-def.hh"
30 #include "spanner.hh"
31 #include "international.hh"
32 #include "item.hh"
33 #include "program-option.hh"
34 #include "profile.hh"
35 #include "unpure-pure-container.hh"
36 #include "warn.hh"
37 #include "protected-scm.hh"
38 
39 #include <cstring>
40 
41 Protected_scm grob_property_callback_stack (SCM_EOL);
42 
43 extern bool debug_property_callbacks;
44 
45 #ifdef DEBUG
46 static void
print_property_callback_stack()47 print_property_callback_stack ()
48 {
49   int frame = 0;
50   for (SCM s = grob_property_callback_stack; scm_is_pair (s); s = scm_cdr (s))
51     message (_f ("%d: %s", frame++, ly_scm_write_string (scm_car (s)).c_str ()));
52 }
53 #endif
54 
55 static Protected_scm modification_callback (SCM_EOL);
56 static Protected_scm cache_callback (SCM_EOL);
57 
58 /*
59 FIXME: this should use ly:set-option interface instead.
60 */
61 
62 LY_DEFINE (ly_set_grob_modification_callback, "ly:set-grob-modification-callback",
63            1, 0, 0, (SCM cb),
64            "Specify a procedure that will be called every time LilyPond"
65            " modifies a grob property.  The callback will receive as"
66            " arguments the grob that is being modified, the name of the"
67            " C++ file in which the modification was requested, the line"
68            " number in the C++ file in which the modification was requested,"
69            " the name of the function in which the modification was"
70            " requested, the property to be changed, and the new value for"
71            " the property.")
72 {
73   modification_callback = (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
74   return SCM_UNSPECIFIED;
75 }
76 
77 LY_DEFINE (ly_set_property_cache_callback, "ly:set-property-cache-callback",
78            1, 0, 0, (SCM cb),
79            "Specify a procedure that will be called whenever lilypond"
80            " calculates a callback function and caches the result.  The"
81            " callback will receive as arguments the grob whose property it"
82            " is, the name of the property, the name of the callback that"
83            " calculated the property, and the new (cached) value of the"
84            " property.")
85 {
86   cache_callback = (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
87   return SCM_UNSPECIFIED;
88 }
89 
90 void
instrumented_set_property(SCM sym,SCM v,char const * file,int line,char const * fun)91 Grob::instrumented_set_property (SCM sym, SCM v,
92                                  char const *file,
93                                  int line,
94                                  char const *fun)
95 {
96 #ifdef DEBUG
97   if (ly_is_procedure (modification_callback))
98     scm_apply_0 (modification_callback,
99                  scm_list_n (self_scm (),
100                              scm_from_locale_string (file),
101                              to_scm (line),
102                              scm_from_ascii_string (fun),
103                              sym, v, SCM_UNDEFINED));
104 #else
105   (void) file;
106   (void) line;
107   (void) fun;
108 #endif
109 
110   internal_set_property (sym, v);
111 }
112 
113 SCM
get_property_alist_chain(SCM def) const114 Grob::get_property_alist_chain (SCM def) const
115 {
116   return scm_list_3 (mutable_property_alist_,
117                      immutable_property_alist_,
118                      def);
119 }
120 
121 extern void check_interfaces_for_property (Grob const *me, SCM sym);
122 
123 void
internal_set_property(SCM sym,SCM v)124 Grob::internal_set_property (SCM sym, SCM v)
125 {
126   internal_set_value_on_alist (&mutable_property_alist_,
127                                sym, v);
128 
129 }
130 
131 void
internal_set_value_on_alist(SCM * alist,SCM sym,SCM v)132 Grob::internal_set_value_on_alist (SCM *alist, SCM sym, SCM v)
133 {
134   /* Perhaps we simply do the assq_set, but what the heck. */
135   if (!is_live ())
136     return;
137 
138   if (do_internal_type_checking_global)
139     {
140       if (!ly_is_procedure (v)
141           && !unsmob<Unpure_pure_container> (v)
142           && !scm_is_eq (v, ly_symbol2scm ("calculation-in-progress")))
143         type_check_assignment (sym, v, ly_symbol2scm ("backend-type?"));
144 
145       check_interfaces_for_property (this, sym);
146     }
147 
148   *alist = scm_assq_set_x (*alist, sym, v);
149 }
150 
151 SCM
internal_get_property_data(SCM sym) const152 Grob::internal_get_property_data (SCM sym) const
153 {
154 #ifdef DEBUG
155   if (profile_property_accesses)
156     note_property_access (&grob_property_lookup_table, sym);
157 #endif
158 
159   SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
160   if (scm_is_true (handle))
161     return scm_cdr (handle);
162 
163   handle = scm_sloppy_assq (sym, immutable_property_alist_);
164 
165   if (do_internal_type_checking_global && scm_is_pair (handle))
166     {
167       SCM val = scm_cdr (handle);
168       if (!ly_is_procedure (val)
169           && !unsmob<Unpure_pure_container> (val))
170         type_check_assignment (sym, val, ly_symbol2scm ("backend-type?"));
171 
172       check_interfaces_for_property (this, sym);
173     }
174 
175   return scm_is_false (handle) ? SCM_EOL : scm_cdr (handle);
176 }
177 
178 SCM
internal_get_property(SCM sym) const179 Grob::internal_get_property (SCM sym) const
180 {
181   SCM val = get_property_data (this, sym);
182 
183 #ifdef DEBUG
184   if (scm_is_eq (val, ly_symbol2scm ("calculation-in-progress")))
185     {
186       programming_error (to_string ("cyclic dependency: calculation-in-progress encountered for #'%s (%s)",
187                                     ly_symbol2string (sym).c_str (),
188                                     name ().c_str ()));//assert (1==0);
189       if (debug_property_callbacks)
190         {
191           message ("backtrace: ");
192           print_property_callback_stack ();
193         }
194     }
195 #endif
196 
197   if (Unpure_pure_container *upc = unsmob<Unpure_pure_container> (val))
198     val = upc->unpure_part ();
199 
200   if (ly_is_procedure (val))
201     {
202       Grob *me = ((Grob *)this);
203       val = me->try_callback_on_alist (&me->mutable_property_alist_, sym, val);
204     }
205 
206   return val;
207 }
208 
209 /* Unlike internal_get_property, this function does no caching. Use it, therefore, with caution. */
210 SCM
internal_get_pure_property(SCM sym,vsize start,vsize end) const211 Grob::internal_get_pure_property (SCM sym, vsize start, vsize end) const
212 {
213   SCM val = internal_get_property_data (sym);
214   if (ly_is_procedure (val))
215     return call_pure_function (val, scm_list_1 (self_scm ()), start, end);
216 
217   if (Unpure_pure_container *upc = unsmob<Unpure_pure_container> (val))
218     {
219       // Do cache, if the function ignores 'start' and 'end'
220       if (upc->is_unchanging ())
221         return internal_get_property (sym);
222       else
223         return call_pure_function (val, scm_list_1 (self_scm ()), start, end);
224     }
225 
226   return val;
227 }
228 
229 SCM
internal_get_maybe_pure_property(SCM sym,bool pure,vsize start,vsize end) const230 Grob::internal_get_maybe_pure_property (SCM sym, bool pure,
231                                         vsize start, vsize end) const
232 {
233   return pure ? internal_get_pure_property (sym, start, end) : internal_get_property (sym);
234 }
235 
236 SCM
try_callback_on_alist(SCM * alist,SCM sym,SCM proc)237 Grob::try_callback_on_alist (SCM *alist, SCM sym, SCM proc)
238 {
239   SCM marker = ly_symbol2scm ("calculation-in-progress");
240   /*
241     need to put a value in SYM to ensure that we don't get a
242     cyclic call chain.
243   */
244   *alist = scm_assq_set_x (*alist, sym, marker);
245 
246 #ifdef DEBUG
247   if (debug_property_callbacks)
248     grob_property_callback_stack = scm_cons (scm_list_3 (self_scm (), sym, proc), grob_property_callback_stack);
249 #endif
250 
251   SCM value = SCM_EOL;
252   if (ly_is_procedure (proc))
253     value = scm_call_1 (proc, self_scm ());
254 
255 #ifdef DEBUG
256   if (debug_property_callbacks)
257     grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
258 #endif
259 
260   // If the return value is *unspecified*, we are dealing with one of
261   // three situations: Either the callback erroneously tried setting
262   // the property itself.  Or it is a pseudo-callback of the
263   // before-line-breaking kind not actually doing anything.  Or the
264   // marker may already have been reset by a different invocation of
265   // the callback in which case we cross fingers and continue silently.
266   if (scm_is_eq (value, SCM_UNSPECIFIED))
267     {
268       value = get_property_data (this, sym);
269       if (scm_is_eq (value, marker))
270         *alist = scm_assq_remove_x (*alist, sym);
271       else if (!scm_is_null (value))
272         programming_error (_f ("%s.%s changed from inside callback",
273                                name (), ly_symbol2string (sym)));
274     }
275   else
276     {
277 #ifdef DEBUG
278       if (ly_is_procedure (cache_callback))
279         scm_call_4 (cache_callback,
280                     self_scm (),
281                     sym,
282                     proc,
283                     value);
284 #endif
285       internal_set_value_on_alist (alist, sym, value);
286     }
287 
288   return value;
289 }
290 
291 void
internal_set_object(SCM s,SCM v)292 Grob::internal_set_object (SCM s, SCM v)
293 {
294   /* Perhaps we simply do the assq_set, but what the heck. */
295   if (!is_live ())
296     return;
297 
298   object_alist_ = scm_assq_set_x (object_alist_, s, v);
299 }
300 
301 void
internal_del_property(SCM sym)302 Grob::internal_del_property (SCM sym)
303 {
304   mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
305 }
306 
307 SCM
internal_get_object(SCM sym) const308 Grob::internal_get_object (SCM sym) const
309 {
310   if (profile_property_accesses)
311     note_property_access (&grob_property_lookup_table, sym);
312 
313   SCM s = scm_sloppy_assq (sym, object_alist_);
314 
315   if (scm_is_true (s))
316     {
317       SCM val = scm_cdr (s);
318       if (ly_is_procedure (val)
319           || unsmob<Unpure_pure_container> (val))
320         {
321           Grob *me = ((Grob *)this);
322           val = me->try_callback_on_alist (&me->object_alist_, sym, val);
323         }
324 
325       return val;
326     }
327 
328   return SCM_EOL;
329 }
330 
331 bool
is_live() const332 Grob::is_live () const
333 {
334   return scm_is_pair (immutable_property_alist_);
335 }
336 
337 bool
internal_has_interface(SCM k) const338 Grob::internal_has_interface (SCM k) const
339 {
340   return scm_is_true (scm_c_memq (k, interfaces_));
341 }
342 
343 SCM
call_pure_function(SCM value,SCM args,vsize start,vsize end)344 call_pure_function (SCM value, SCM args, vsize start, vsize end)
345 {
346   if (Unpure_pure_container *upc = unsmob<Unpure_pure_container> (value))
347     {
348       if (upc->is_unchanging ())
349         {
350           // Don't bother forming an Unpure_pure_call here.
351           value = upc->unpure_part ();
352 
353           if (ly_is_procedure (value))
354             return scm_apply_0 (value, args);
355           return value;
356         }
357 
358       value = upc->pure_part ();
359 
360       if (ly_is_procedure (value))
361         return scm_apply_3 (value,
362                             scm_car (args),
363                             to_scm (start),
364                             to_scm (end),
365                             scm_cdr (args));
366 
367       return value;
368     }
369 
370   if (!ly_is_procedure (value))
371     return value;
372 
373   return SCM_BOOL_F;
374 }
375