]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
* lily/tuplet-engraver.cc (start_translation_timestep): only read
[lilypond.git] / lily / context.cc
1 /*
2   context.cc -- implement Context
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "context.hh"
10
11 #include "program-option.hh"
12 #include "context-def.hh"
13 #include "ly-smobs.icc"
14 #include "main.hh"
15 #include "output-def.hh"
16 #include "scm-hash.hh"
17 #include "score-context.hh"
18 #include "translator-group.hh"
19 #include "warn.hh"
20 #include "lilypond-key.hh"
21
22 bool
23 Context::is_removable () const
24 {
25   return context_list_ == SCM_EOL && ! iterator_count_
26     && !dynamic_cast<Score_context const *> (this);
27 }
28
29 void
30 Context::check_removal ()
31 {
32   for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
33     {
34       Context *trg = unsmob_context (scm_car (p));
35
36       trg->check_removal ();
37       if (trg->is_removable ())
38         {
39           recurse_over_translators (trg, &Translator::finalize,
40                                     &Translator_group::finalize,
41                                     UP);
42           remove_context (trg);
43         }
44     }
45 }
46
47 Context::Context (Context const &)
48 {
49   assert (false);
50 }
51
52 Scheme_hash_table *
53 Context::properties_dict () const
54 {
55   return Scheme_hash_table::unsmob (properties_scm_);
56 }
57
58 void
59 Context::add_context (Context *t)
60 {
61   SCM ts = t->self_scm ();
62   context_list_ = ly_append2 (context_list_,
63                               scm_cons (ts, SCM_EOL));
64
65   t->daddy_context_ = this;
66   if (!t->init_)
67     {
68       t->init_ = true;
69
70       scm_gc_unprotect_object (ts);
71       Context_def *td = unsmob_context_def (t->definition_);
72
73       /* This cannot move before add_context (), because \override
74          operations require that we are in the hierarchy.  */
75       td->apply_default_property_operations (t);
76
77       recurse_over_translators (t,
78                                 &Translator::initialize,
79                                 &Translator_group::initialize,
80                                 DOWN);
81     }
82 }
83
84 Object_key const *
85 Context::get_key () const
86 {
87   return key_;
88 }
89
90 Context::Context (Object_key const *key)
91 {
92   key_ = key;
93   daddy_context_ = 0;
94   init_ = false;
95   aliases_ = SCM_EOL;
96   iterator_count_ = 0;
97   implementation_ = 0;
98   properties_scm_ = SCM_EOL;
99   accepts_list_ = SCM_EOL;
100   context_list_ = SCM_EOL;
101   definition_ = SCM_EOL;
102
103   smobify_self ();
104   properties_scm_ = (new Scheme_hash_table)->self_scm ();
105   scm_gc_unprotect_object (properties_scm_);
106
107   if (key_)
108     scm_gc_unprotect_object (key_->self_scm ());
109 }
110
111 /* TODO:  this shares code with find_create_context ().  */
112 Context *
113 Context::create_unique_context (SCM n, SCM operations)
114 {
115   /*
116     Don't create multiple score contexts.
117   */
118   if (dynamic_cast<Global_context *> (this)
119       && dynamic_cast<Global_context *> (this)->get_score_context ())
120     return get_score_context ()->create_unique_context (n, operations);
121
122   /*
123     TODO: use accepts_list_.
124   */
125   Link_array<Context_def> path
126     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
127
128   if (path.size ())
129     {
130       Context *current = this;
131
132       // start at 1.  The first one (index 0) will be us.
133       for (int i = 0; i < path.size (); i++)
134         {
135           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
136
137           current = current->create_context (path[i],
138                                              "\\new",
139                                              ops);
140         }
141
142       return current;
143     }
144
145   /*
146     Don't go up to Global_context, because global goes down to
147     Score_context
148   */
149   Context *ret = 0;
150   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
151     ret = daddy_context_->create_unique_context (n, operations);
152   else
153     {
154       warning (_f ("can't find or create new `%s'",
155                    ly_symbol2string (n).to_str0 ()));
156       ret = 0;
157     }
158   return ret;
159 }
160
161 Context *
162 Context::find_create_context (SCM n, String id, SCM operations)
163 {
164   /*
165     Don't create multiple score contexts.
166   */
167   if (dynamic_cast<Global_context *> (this)
168       && dynamic_cast<Global_context *> (this)->get_score_context ())
169     return get_score_context ()->find_create_context (n, id, operations);
170
171   if (Context *existing = find_context_below (this, n, id))
172     return existing;
173
174   if (n == ly_symbol2scm ("Bottom"))
175     {
176       Context *tg = get_default_interpreter ();
177       return tg;
178     }
179
180   /*
181     TODO: use accepts_list_.
182   */
183   Link_array<Context_def> path
184     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
185
186   if (path.size ())
187     {
188       Context *current = this;
189
190       // start at 1.  The first one (index 0) will be us.
191       for (int i = 0; i < path.size (); i++)
192         {
193           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
194
195           String this_id = "";
196           if (i == path.size () -1)
197             {
198               this_id = id;
199             }
200
201           current = current->create_context (path[i],
202                                              this_id,
203                                              ops);
204         }
205
206       return current;
207     }
208
209   /*
210     Don't go up to Global_context, because global goes down to
211     Score_context
212   */
213   Context *ret = 0;
214   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
215     ret = daddy_context_->find_create_context (n, id, operations);
216   else
217     {
218       warning (_f ("can't find or create `%s' called `%s'",
219                    ly_symbol2string (n).to_str0 (), id));
220       ret = 0;
221     }
222   return ret;
223 }
224
225 Context *
226 Context::create_context (Context_def *cdef,
227                          String id,
228                          SCM ops)
229 {
230   String type = ly_symbol2string (cdef->get_context_name ());
231   Object_key const *key = get_context_key (type, id);
232   Context *new_context
233     = cdef->instantiate (ops, key);
234
235   new_context->id_string_ = id;
236   add_context (new_context);
237   apply_property_operations (new_context, ops);
238
239   return new_context;
240 }
241
242 Object_key const *
243 Context::get_context_key (String type, String id)
244 {
245   if (!use_object_keys)
246     return 0;
247       
248   String now_key = type + "@" + id;
249
250   int disambiguation_count = 0;
251   if (context_counts_.find (now_key) != context_counts_.end ())
252     {
253       disambiguation_count = context_counts_[now_key];
254     }
255
256   context_counts_[now_key] = disambiguation_count + 1;
257
258   return new Lilypond_context_key (get_key (),
259                                    now_mom (),
260                                    type, id,
261                                    disambiguation_count);
262 }
263
264 Object_key const *
265 Context::get_grob_key (String name)
266 {
267   if (!use_object_keys)
268     return 0;
269   
270   int disambiguation_count = 0;
271   if (grob_counts_.find (name) != grob_counts_.end ())
272     {
273       disambiguation_count = grob_counts_[name];
274     }
275   grob_counts_[name] = disambiguation_count + 1;
276
277   Object_key *k = new Lilypond_grob_key (get_key (),
278                                          now_mom (),
279                                          name,
280                                          disambiguation_count);
281
282   return k;
283 }
284
285 /*
286   Default child context as a SCM string, or something else if there is
287   none.
288 */
289 SCM
290 Context::default_child_context_name () const
291 {
292   return scm_is_pair (accepts_list_)
293     ? scm_car (accepts_list_) 
294     : SCM_EOL;
295 }
296
297 bool
298 Context::is_bottom_context () const
299 {
300   return !scm_is_symbol (default_child_context_name ());
301 }
302
303 Context *
304 Context::get_default_interpreter ()
305 {
306   if (!is_bottom_context ())
307     {
308       SCM nm = default_child_context_name ();
309       SCM st = find_context_def (get_output_def (), nm);
310
311       String name = ly_symbol2string (nm);
312       Context_def *t = unsmob_context_def (st);
313       if (!t)
314         {
315           warning (_f ("can't find or create: `%s'", name.to_str0 ()));
316           t = unsmob_context_def (this->definition_);
317         }
318
319       Context *tg = create_context (t, "", SCM_EOL);
320       if (!tg->is_bottom_context ())
321         return tg->get_default_interpreter ();
322       else
323         return tg;
324     }
325   return this;
326 }
327
328 /*
329   PROPERTIES
330 */
331 Context *
332 Context::where_defined (SCM sym) const
333 {
334   if (properties_dict ()->contains (sym))
335     {
336       return (Context *)this;
337     }
338
339   return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
340 }
341
342 //#define PROFILE_PROPERTY_ACCESSES
343
344 SCM context_property_lookup_table;
345 LY_DEFINE(ly_context_property_lookup_stats, "ly:context-property-lookup-stats",
346           0,0,0, (),
347           "")
348 {
349   return context_property_lookup_table ?  context_property_lookup_table
350     : scm_c_make_hash_table (1);
351 }
352
353
354 /*
355   return SCM_EOL when not found.
356 */
357 SCM
358 Context::internal_get_property (SCM sym) const
359 {
360 #ifdef PROFILE_PROPERTY_ACCESSES
361   extern void note_property_access (SCM *table, SCM sym);
362   note_property_access (&context_property_lookup_table, sym);
363 #endif
364   
365   SCM val = SCM_EOL;
366   if (properties_dict ()->try_retrieve (sym, &val))
367     return val;
368
369   if (daddy_context_)
370     return daddy_context_->internal_get_property (sym);
371
372   return val;
373 }
374
375 bool
376 Context::is_alias (SCM sym) const
377 {
378   if (sym == ly_symbol2scm ("Bottom")
379       && !scm_is_pair (accepts_list_))
380     return true;
381   if (sym == unsmob_context_def (definition_)->get_context_name ())
382     return true;
383
384   return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
385 }
386
387 void
388 Context::add_alias (SCM sym)
389 {
390   aliases_ = scm_cons (sym, aliases_);
391 }
392
393 void
394 Context::internal_set_property (SCM sym, SCM val)
395 {
396 #ifndef NDEBUG
397   if (do_internal_type_checking_global)
398     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
399 #endif
400
401   properties_dict ()->set (sym, val);
402 }
403
404 /*
405   TODO: look up to check whether we have inherited var?
406 */
407 void
408 Context::unset_property (SCM sym)
409 {
410   properties_dict ()->remove (sym);
411 }
412
413 /**
414    Remove a context from the hierarchy.
415 */
416 Context *
417 Context::remove_context (Context *trans)
418 {
419   assert (trans);
420
421   context_list_ = scm_delq_x (trans->self_scm (), context_list_);
422   trans->daddy_context_ = 0;
423   return trans;
424 }
425
426 /*
427   ID == "" means accept any ID.
428 */
429 Context *
430 find_context_below (Context *where,
431                     SCM type, String id)
432 {
433   if (where->is_alias (type))
434     {
435       if (id == "" || where->id_string () == id)
436         return where;
437     }
438
439   Context *found = 0;
440   for (SCM s = where->children_contexts ();
441        !found && scm_is_pair (s); s = scm_cdr (s))
442     {
443       Context *tr = unsmob_context (scm_car (s));
444
445       found = find_context_below (tr, type, id);
446     }
447
448   return found;
449 }
450
451 SCM
452 Context::properties_as_alist () const
453 {
454   return properties_dict ()->to_alist ();
455 }
456
457 SCM
458 Context::context_name_symbol () const
459 {
460   Context_def *td = unsmob_context_def (definition_);
461   return td->get_context_name ();
462 }
463
464 String
465 Context::context_name () const
466 {
467   return ly_symbol2string (context_name_symbol ());
468 }
469
470 Score_context *
471 Context::get_score_context () const
472 {
473   if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
474     return sc;
475   else if (daddy_context_)
476     return daddy_context_->get_score_context ();
477   else
478     return 0;
479 }
480
481 Output_def *
482 Context::get_output_def () const
483 {
484   return daddy_context_ ? daddy_context_->get_output_def () : 0;
485 }
486
487 Context::~Context ()
488 {
489 }
490
491 Moment
492 Context::now_mom () const
493 {
494   return daddy_context_->now_mom ();
495 }
496
497 int
498 Context::print_smob (SCM s, SCM port, scm_print_state *)
499 {
500   Context *sc = (Context *) SCM_CELL_WORD_1 (s);
501
502   scm_puts ("#<", port);
503   scm_puts (classname (sc), port);
504   if (Context_def *d = unsmob_context_def (sc->definition_))
505     {
506       scm_puts (" ", port);
507       scm_display (d->get_context_name (), port);
508     }
509
510   if (!sc->id_string_.is_empty ())
511     {
512       scm_puts ("=", port);
513       scm_puts (sc->id_string_.to_str0 (), port);
514     }
515
516   scm_puts (" ", port);
517
518   scm_display (sc->context_list_, port);
519   scm_puts (" >", port);
520
521   return 1;
522 }
523
524 SCM
525 Context::mark_smob (SCM sm)
526 {
527   Context *me = (Context *) SCM_CELL_WORD_1 (sm);
528   if (me->key_)
529     scm_gc_mark (me->key_->self_scm ());
530   
531   scm_gc_mark (me->context_list_);
532   scm_gc_mark (me->aliases_);
533   scm_gc_mark (me->definition_);
534   scm_gc_mark (me->properties_scm_);
535   scm_gc_mark (me->accepts_list_);
536   if (me->implementation_)
537     scm_gc_mark (me->implementation_->self_scm ());
538
539   return me->properties_scm_;
540 }
541
542 IMPLEMENT_SMOBS (Context);
543 IMPLEMENT_DEFAULT_EQUAL_P (Context);
544 IMPLEMENT_TYPE_P (Context, "ly:context?");
545
546 bool
547 Context::try_music (Music *m)
548 {
549   Translator_group *t = implementation ();
550   if (!t)
551     return false;
552
553   bool b = t->try_music (m);
554   if (!b && daddy_context_)
555     b = daddy_context_->try_music (m);
556
557   return b;
558 }
559
560 Global_context *
561 Context::get_global_context () const
562 {
563   if (dynamic_cast<Global_context *> ((Context *) this))
564     return dynamic_cast<Global_context *> ((Context *) this);
565
566   if (daddy_context_)
567     return daddy_context_->get_global_context ();
568
569   programming_error ("no Global context");
570   return 0;
571 }
572
573 Context *
574 Context::get_parent_context () const
575 {
576   return daddy_context_;
577 }
578
579 Translator_group *
580 Context::implementation () const
581 {
582   return implementation_;
583 }
584
585 void
586 Context::clear_key_disambiguations ()
587 {
588   if (!use_object_keys)
589     return;
590   
591   grob_counts_.clear ();
592   context_counts_.clear ();
593   for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
594     {
595       unsmob_context (scm_car (s))->clear_key_disambiguations ();
596     }
597 }
598
599
600 /*
601   Ugh. Where to put this? 
602 */
603 Rational
604 measure_length (Context const *context)
605 {
606   SCM l = context->get_property ("measureLength");
607   Rational length (1); 
608   if (unsmob_moment (l))
609     length = unsmob_moment (l)->main_part_;
610   return length;
611 }
612
613 Moment
614 measure_position (Context const *context)
615 {
616   SCM sm = context->get_property ("measurePosition");
617
618   Moment m = 0;
619   if (unsmob_moment (sm))
620     {
621       m = *unsmob_moment (sm);
622
623       if (m.main_part_ < Rational (0))
624         {
625           Rational length (measure_length (context));
626           while (m.main_part_ < Rational (0))
627             m.main_part_ += length;
628         }
629     }
630
631   return m;
632 }