]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-group.cc
* lily/paper-score.cc (process): move gc stat stuff to init.ly
[lilypond.git] / lily / translator-group.cc
1 /*
2   Translator_group.cc -- implement Translator_group
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "music-output-def.hh"
10 #include "translator-group.hh"
11 #include "translator.hh"
12 #include "warn.hh"
13 #include "moment.hh"
14 #include "scm-hash.hh"
15 #include "context-def.hh"
16 #include "main.hh"
17 #include "music.hh"
18
19 Translator_group::Translator_group (Translator_group const&s)
20   : Translator (s)
21 {
22   iterator_count_ =0;
23   
24   Scheme_hash_table * tab =  new Scheme_hash_table (*s.properties_dict ());
25   properties_scm_ = tab->self_scm ();
26   scm_gc_unprotect_object (tab->self_scm ());
27 }
28
29 Scheme_hash_table*
30 Translator_group::properties_dict () const
31 {
32   return Scheme_hash_table::unsmob (properties_scm_);
33 }
34
35 Translator_group::~Translator_group ()
36 {
37   
38   //assert (is_removable ());
39 }
40
41
42 Translator_group::Translator_group ()
43 {
44   iterator_count_  = 0;
45   Scheme_hash_table *tab = new Scheme_hash_table ;
46   properties_scm_ = tab->self_scm ();
47
48   scm_gc_unprotect_object (tab->self_scm ());
49 }
50
51 void
52 Translator_group::check_removal ()
53 {
54   SCM next = SCM_EOL; 
55   for (SCM p = trans_group_list_; gh_pair_p (p); p = next)
56     {
57       next = ly_cdr (p);
58
59       Translator_group *trg =  dynamic_cast<Translator_group*> (unsmob_translator (ly_car (p)));
60
61       trg->check_removal ();
62       if (trg->is_removable ())
63         terminate_translator (trg);
64     }
65 }
66
67 SCM
68 Translator_group::add_translator (SCM list, Translator *t)
69 {
70   /*
71     Must append, since list ordering must be preserved.
72    */
73   list = gh_append2 (list, gh_cons (t->self_scm (), SCM_EOL));
74   t->daddy_trans_ = this;
75   t->output_def_ = output_def_;
76
77   return list;
78 }
79
80
81 void
82 Translator_group::add_used_group_translator (Translator *t)
83 {
84   trans_group_list_ = add_translator (trans_group_list_,t);
85 }
86
87
88 void
89 Translator_group::add_fresh_group_translator (Translator*t)
90 {
91   Translator_group*tg = dynamic_cast<Translator_group*> (t);
92   trans_group_list_ = add_translator (trans_group_list_,t);
93   scm_gc_unprotect_object (t->self_scm ());
94
95   Context_def * td = unsmob_context_def (tg->definition_);
96
97   /*
98     this can not move before add_translator(), because \override
99     operations require that we are in the hierarchy.
100    */
101   td->apply_default_property_operations (tg);
102
103   t->initialize ();
104 }
105
106 bool
107 Translator_group::is_removable () const
108 {
109   return trans_group_list_ == SCM_EOL && ! iterator_count_;
110 }
111
112 Translator_group *
113 Translator_group::find_existing_translator (SCM n, String id)
114 {
115   if ((is_alias (n) && (id_string_ == id || id.is_empty ())) || n == ly_symbol2scm ("Current"))
116     return this;
117
118   Translator_group* r = 0;
119   for (SCM p = trans_group_list_; !r && gh_pair_p (p); p = ly_cdr (p))
120     {
121       Translator *  t = unsmob_translator (ly_car (p));
122       
123       r = dynamic_cast<Translator_group*> (t)->find_existing_translator (n, id);    }
124
125   return r;
126 }
127
128
129 Translator_group*
130 Translator_group::find_create_translator (SCM n, String id, SCM operations)
131 {
132   Translator_group * existing = find_existing_translator (n,id);
133   if (existing)
134     return existing;
135
136
137   /*
138     TODO: use accepts_list_.
139    */
140   Link_array<Context_def> path
141     = unsmob_context_def (definition_)->path_to_acceptable_translator (n, get_output_def ());
142
143   if (path.size ())
144     {
145       Translator_group * current = this;
146
147       // start at 1.  The first one (index 0) will be us.
148       for (int i=0; i < path.size (); i++)
149         {
150           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
151
152           Translator_group * new_group
153             = path[i]->instantiate (output_def_, ops);
154
155           if (i == path.size () -1)
156             {
157               new_group->id_string_ = id;
158             }
159
160           current->add_fresh_group_translator (new_group);
161           apply_property_operations (new_group, ops);
162           
163           current = new_group;
164         }
165
166       return current;
167     }
168
169   Translator_group *ret = 0;
170   if (daddy_trans_)
171     ret = daddy_trans_->find_create_translator (n, id, operations);
172   else
173     {
174       warning (_f ("Cannot find or create `%s' called `%s'",
175                    ly_symbol2string (n).to_str0 (), id));
176       ret =0;
177     }
178   return ret;
179 }
180
181 bool
182 Translator_group::try_music (Music* m)
183 {
184   bool hebbes_b = try_music_on_nongroup_children (m);
185   
186   if (!hebbes_b && daddy_trans_)
187     hebbes_b = daddy_trans_->try_music (m);
188   
189   return hebbes_b ;
190 }
191
192 int
193 Translator_group::get_depth () const
194 {
195   return (daddy_trans_) ? daddy_trans_->get_depth ()  + 1 : 0;
196 }
197
198 void
199 Translator_group::terminate_translator (Translator*r)
200 {
201   r->finalize ();
202   /*
203     Return value ignored. GC does the rest.
204    */
205   remove_translator (r);
206 }
207
208
209 /**
210    Remove a translator from the hierarchy.
211  */
212 Translator *
213 Translator_group::remove_translator (Translator*trans)
214 {
215   assert (trans);
216
217   trans_group_list_ = scm_delq_x (trans->self_scm (), trans_group_list_);
218   trans->daddy_trans_ = 0;
219   return trans;
220 }
221
222
223 /*
224   Default child context as a SCM string, or something else if there is
225   none.
226 */
227 SCM
228 default_child_context_name (Translator_group const *tg)
229 {
230   return gh_pair_p (tg->accepts_list_)
231     ? ly_car (scm_last_pair (tg->accepts_list_))
232     : SCM_EOL;
233 }
234
235
236 bool
237 Translator_group::is_bottom_context () const
238 {
239   return !gh_symbol_p (default_child_context_name (this));
240 }
241
242 Translator_group*
243 Translator_group::get_default_interpreter ()
244 {
245   if (!is_bottom_context ())
246     {
247       SCM nm = default_child_context_name (this);
248       SCM st = get_output_def ()->find_translator (nm);
249
250       Context_def *t = unsmob_context_def (st);
251       if (!t)
252         {
253           warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
254           t = unsmob_context_def (this->definition_);
255         }
256       Translator_group *tg = t->instantiate (output_def_, SCM_EOL);
257       add_fresh_group_translator (tg);
258       if (!tg->is_bottom_context ())
259         return tg->get_default_interpreter ();
260       else
261         return tg;
262     }
263   return this;
264 }
265
266 static void
267 static_each (SCM list, Method_pointer method)
268 {
269   for (SCM p = list; gh_pair_p (p); p = ly_cdr (p))
270     (unsmob_translator (ly_car (p))->*method) ();
271   
272 }
273
274 void
275 Translator_group::each (Method_pointer method) 
276 {
277   static_each (get_simple_trans_list (), method);
278   static_each (trans_group_list_, method);
279 }
280
281
282 /*
283   PROPERTIES
284  */
285 Translator_group*
286 Translator_group::where_defined (SCM sym) const
287 {
288   if (properties_dict ()->contains (sym))
289     {
290       return (Translator_group*)this;
291     }
292
293   return (daddy_trans_) ? daddy_trans_->where_defined (sym) : 0;
294 }
295
296 /*
297   return SCM_EOL when not found.
298 */
299 SCM
300 Translator_group::internal_get_property (SCM sym) const
301 {
302   SCM val =SCM_EOL;
303   if (properties_dict ()->try_retrieve (sym, &val))
304     return val;
305
306   if (daddy_trans_)
307     return daddy_trans_->internal_get_property (sym);
308   
309   return val;
310 }
311
312 void
313 Translator_group::internal_set_property (SCM sym, SCM val)
314 {
315 #ifndef NDEBUG
316   if (internal_type_checking_global_b)
317     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
318 #endif
319   
320   properties_dict ()->set (sym, val);
321 }
322
323 /*
324   TODO: look up to check whether we have inherited var? 
325  */
326 void
327 Translator_group::unset_property (SCM sym)
328 {
329   properties_dict ()->remove (sym);
330 }
331
332
333 /*
334   Push or pop (depending on value of VAL) a single entry (ELTPROP . VAL)
335   entry from a translator property list by name of PROP
336 */
337 void
338 Translator_group::execute_pushpop_property (SCM prop, SCM eltprop, SCM val)
339 {
340   if (gh_symbol_p (prop))
341     {
342       if (val != SCM_UNDEFINED)
343         {
344           SCM prev = internal_get_property (prop);
345
346           if (gh_pair_p (prev) || prev == SCM_EOL)
347             {
348               bool ok = type_check_assignment (eltprop, val, ly_symbol2scm ("backend-type?"));
349               
350               if (ok)
351                 {
352                   prev = gh_cons (gh_cons (eltprop, val), prev);
353                   internal_set_property (prop, prev);
354                 }
355             }
356           else
357             {
358               // warning here.
359             }
360           
361         }
362       else
363         {
364           SCM prev = internal_get_property (prop);
365
366           /*
367             TODO: should have scm_equal_something () for reverting
368             autobeam properties.
369            */
370           SCM newprops= SCM_EOL ;
371           while (gh_pair_p (prev) && !SCM_EQ_P(ly_caar (prev), eltprop))
372             {
373               newprops = gh_cons (ly_car (prev), newprops);
374               prev = ly_cdr (prev);
375             }
376           
377           if (gh_pair_p (prev))
378             {
379               newprops = scm_reverse_x (newprops, ly_cdr (prev));
380               internal_set_property (prop, newprops);
381             }
382         }
383     }
384 }
385
386
387
388 /*
389   STUBS
390 */
391 void
392 Translator_group::stop_translation_timestep ()
393 {
394   each (&Translator::stop_translation_timestep);
395 }
396
397 void
398 Translator_group::start_translation_timestep ()
399 {
400   each (&Translator::start_translation_timestep);
401 }
402
403 void
404 Translator_group::do_announces ()
405 {
406   each (&Translator::do_announces);
407 }
408
409 void
410 Translator_group::initialize ()
411 {
412   SCM tab = scm_make_vector (gh_int2scm (19), SCM_BOOL_F);
413   set_property ("acceptHashTable", tab);
414   each (&Translator::initialize);
415 }
416
417 void
418 Translator_group::finalize ()
419 {
420   each (&Translator::finalize);
421 }
422
423 bool
424 translator_accepts_any_of (Translator*tr, SCM ifaces)
425 {
426   SCM ack_ifs = scm_assoc (ly_symbol2scm ("events-accepted"),
427                            tr->translator_description());
428   ack_ifs = gh_cdr (ack_ifs);
429   for (SCM s = ifaces; ly_pair_p (s); s = ly_cdr (s))
430     if (scm_memq (ly_car (s), ack_ifs) != SCM_BOOL_F)
431       return true;
432   return false;
433 }
434
435 SCM
436 find_accept_translators (SCM gravlist, SCM ifaces)
437 {
438   SCM l = SCM_EOL;
439   for (SCM s = gravlist; ly_pair_p (s);  s = ly_cdr (s))
440     {
441       Translator* tr = unsmob_translator (ly_car (s));
442       if (translator_accepts_any_of (tr, ifaces))
443         l = scm_cons (tr->self_scm (), l); 
444     }
445   l = scm_reverse_x (l, SCM_EOL);
446
447   return l;
448 }
449
450 bool
451 Translator_group::try_music_on_nongroup_children (Music *m )
452 {
453   SCM tab = get_property ("acceptHashTable");
454   SCM name = scm_sloppy_assq (ly_symbol2scm ("name"),
455                               m->get_property_alist (false));
456
457   if (!gh_pair_p (name))
458     return false;
459
460   name = gh_cdr (name);
461   SCM accept_list = scm_hashq_ref (tab, name, SCM_UNDEFINED);
462   if (accept_list == SCM_BOOL_F)
463     {
464       accept_list = find_accept_translators (get_simple_trans_list (),
465                                              m->get_mus_property ("types"));
466       scm_hashq_set_x (tab, name, accept_list);
467     }
468
469   for (SCM p = accept_list; gh_pair_p (p); p = ly_cdr (p))
470     {
471       Translator * t = unsmob_translator (ly_car (p));
472       if (t && t->try_music (m))
473         return true;
474     }
475   return false;
476 }
477
478 SCM
479 Translator_group::properties_as_alist () const
480 {
481   return properties_dict()->to_alist();
482 }
483
484 String
485 Translator_group::context_name () const
486 {
487   Context_def * td = unsmob_context_def (definition_ );
488   return ly_symbol2string (td->get_context_name ());
489 }
490
491 /*
492   PRE_INIT_OPS is in the order specified, and hence must be reversed.
493  */
494 void
495 apply_property_operations (Translator_group*tg, SCM pre_init_ops)
496 {
497   SCM correct_order = scm_reverse (pre_init_ops);
498   for (SCM s = correct_order; gh_pair_p (s); s = ly_cdr (s))
499     {
500       SCM entry = ly_car (s);
501       SCM type = ly_car (entry);
502       entry = ly_cdr (entry); 
503       
504       if (type == ly_symbol2scm ("push") || type == ly_symbol2scm ("poppush"))
505         {
506           SCM val = ly_cddr (entry);
507           val = gh_pair_p (val) ? ly_car (val) : SCM_UNDEFINED;
508
509           tg->execute_pushpop_property (ly_car (entry), ly_cadr (entry), val);
510         }
511       else if (type == ly_symbol2scm ("assign"))
512         {
513           tg->internal_set_property (ly_car (entry), ly_cadr (entry));
514         }
515     }
516 }
517
518 SCM
519 names_to_translators (SCM namelist, Translator_group*tg)
520 {
521   SCM l = SCM_EOL;
522   for (SCM s = namelist; gh_pair_p (s) ; s = ly_cdr (s))
523     {
524       Translator * t = get_translator (ly_car (s));
525       if (!t)
526         warning (_f ("can't find: `%s'", s));
527       else
528         {
529           Translator * tr = t->clone ();
530           SCM str = tr->self_scm ();
531           l = gh_cons (str, l);
532
533           tr->daddy_trans_ = tg;
534           tr->output_def_  = tg->output_def_;
535
536           scm_gc_unprotect_object (str);
537         }
538     }
539   return l;
540 }
541
542
543 SCM
544 Translator_group::get_simple_trans_list ()
545 {
546   return simple_trans_list_;
547
548 }
549
550
551   
552 #if 0
553 SCM
554 Translator_group::get_simple_trans_list ()
555 {
556   if (simple_trans_list_ != SCM_BOOL_F)
557     return simple_trans_list_;
558   
559   Context_def * td = unsmob_context_def (definition_);
560
561   /*
562     The following cannot work, since start_translation_timestep ()
563     triggers this code, and start_translation_timestep happens before
564     \property Voice.Voice =#'()
565     
566    */
567       trans_names = td->get_translator_names (SCM_EOL); 
568   
569   SCM trans_names = internal_get_property (td->get_context_name ());
570   if (!gh_pair_p (trans_names))
571     {
572     }
573
574   simple_trans_list_ = names_to_translators (trans_names, this);
575
576   
577   static_each (simple_trans_list_, &Translator::initialize);
578   return simple_trans_list_;
579 }
580 #endif