X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Ftranslator-group.cc;h=ab79d1347696c3d8ea18ae9cc1871ecd0bbe964f;hb=refs%2Fheads%2Fdebian;hp=a81f9f1d5a8011aea69ea7698098cb2a1c45a575;hpb=2246ad52dd3535de56cf07ea5dbc7c51ad8cfc68;p=lilypond.git diff --git a/lily/translator-group.cc b/lily/translator-group.cc index a81f9f1d5a..ab79d13476 100644 --- a/lily/translator-group.cc +++ b/lily/translator-group.cc @@ -1,561 +1,371 @@ /* - Translator_group.cc -- implement Translator_group + This file is part of LilyPond, the GNU music typesetter. - source file of the GNU LilyPond music typesetter + Copyright (C) 1997--2012 Han-Wen Nienhuys , + Erik Sandberg - (c) 1997--2003 Han-Wen Nienhuys + LilyPond is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + LilyPond is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LilyPond. If not, see . */ -#include "music-output-def.hh" #include "translator-group.hh" -#include "translator.hh" -#include "warn.hh" -#include "moment.hh" -#include "scm-hash.hh" -#include "translator-def.hh" + +#include "context-def.hh" +#include "context.hh" +#include "dispatcher.hh" +#include "engraver.hh" +#include "engraver-group.hh" +#include "international.hh" #include "main.hh" #include "music.hh" +#include "output-def.hh" +#include "performer.hh" +#include "performer-group.hh" +#include "scheme-engraver.hh" +#include "scm-hash.hh" +#include "warn.hh" -Translator_group::Translator_group (Translator_group const&s) - : Translator (s) -{ - iterator_count_ =0; - - Scheme_hash_table * tab = new Scheme_hash_table (*s.properties_dict ()); - properties_scm_ = tab->self_scm (); - scm_gc_unprotect_object (tab->self_scm ()); -} - -Scheme_hash_table* -Translator_group::properties_dict () const -{ - return Scheme_hash_table::unsmob (properties_scm_); -} - -Translator_group::~Translator_group () +void +translator_each (SCM list, Translator_method method) { - - //assert (is_removable ()); + for (SCM p = list; scm_is_pair (p); p = scm_cdr (p)) + (unsmob_translator (scm_car (p))->*method) (); } - -Translator_group::Translator_group () +void +Translator_group::initialize () { - iterator_count_ = 0; - Scheme_hash_table *tab = new Scheme_hash_table ; - properties_scm_ = tab->self_scm (); - - scm_gc_unprotect_object (tab->self_scm ()); + precompute_method_bindings (); } void -Translator_group::check_removal () +Translator_group::connect_to_context (Context *c) { - SCM next = SCM_EOL; - for (SCM p = trans_group_list_; gh_pair_p (p); p = next) + if (context_) { - next = ly_cdr (p); - - Translator_group *trg = dynamic_cast (unsmob_translator (ly_car (p))); - - trg->check_removal (); - if (trg->is_removable ()) - terminate_translator (trg); + programming_error ("translator group is already connected to context " + + context_->context_name ()); } -} -SCM -Translator_group::add_translator (SCM list, Translator *t) -{ - /* - Must append, since list ordering must be preserved. - */ - list = gh_append2 (list, gh_cons (t->self_scm (), SCM_EOL)); - t->daddy_trans_ = this; - t->output_def_ = output_def_; - - return list; + context_ = c; + c->event_source ()->add_listener (GET_LISTENER (create_child_translator), + ly_symbol2scm ("AnnounceNewContext")); + for (SCM tr_list = simple_trans_list_; scm_is_pair (tr_list); tr_list = scm_cdr (tr_list)) + { + Translator *tr = unsmob_translator (scm_car (tr_list)); + tr->connect_to_context (c); + } } - void -Translator_group::add_used_group_translator (Translator *t) +Translator_group::disconnect_from_context () { - trans_group_list_ = add_translator (trans_group_list_,t); + for (SCM tr_list = simple_trans_list_; scm_is_pair (tr_list); tr_list = scm_cdr (tr_list)) + { + Translator *tr = unsmob_translator (scm_car (tr_list)); + tr->disconnect_from_context (context_); + } + context_->event_source ()->remove_listener (GET_LISTENER (create_child_translator), + ly_symbol2scm ("AnnounceNewContext")); + context_ = 0; + protected_events_ = SCM_EOL; } - void -Translator_group::add_fresh_group_translator (Translator*t) +Translator_group::finalize () { - Translator_group*tg = dynamic_cast (t); - - trans_group_list_ = add_translator (trans_group_list_,t); - - Translator_def * td = unsmob_translator_def (tg->definition_); - - /* - this can not move before add_translator(), because \override - operations require that we are in the hierarchy. - */ - td->apply_default_property_operations (tg); - - t->initialize (); } +/* + Both filter_performers and filter_engravers used to use a direct dynamic_cast + on the unsmobbed translator to be filtered, i.e., -bool -Translator_group::is_removable () const -{ - return trans_group_list_ == SCM_EOL && ! iterator_count_; -} + if (dynamic_cast (unsmob_translator (scm_car (*tail)))) -Translator_group * -Translator_group::find_existing_translator (SCM n, String id) + but this caused mysterious optimisation issues in several GUB builds. See + issue #818 for the background to this change. +*/ +SCM +filter_performers (SCM ell) { - if ((is_alias (n) && (id_string_ == id || id.is_empty ())) || n == ly_symbol2scm ("Current")) - return this; - - Translator_group* r = 0; - for (SCM p = trans_group_list_; !r && gh_pair_p (p); p = ly_cdr (p)) + SCM *tail = ℓ + for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p)) { - Translator * t = unsmob_translator (ly_car (p)); - - r = dynamic_cast (t)->find_existing_translator (n, id); + if (unsmob_performer (scm_car (*tail))) + *tail = scm_cdr (*tail); + else + tail = SCM_CDRLOC (*tail); } - - return r; + return ell; } - -Translator_group* -Translator_group::find_create_translator (SCM n, String id, SCM operations) +SCM +filter_engravers (SCM ell) { - Translator_group * existing = find_existing_translator (n,id); - if (existing) - return existing; - - Link_array path - = unsmob_translator_def (definition_)->path_to_acceptable_translator (n, get_output_def ()); - - if (path.size ()) - { - Translator_group * current = this; - - // start at 1. The first one (index 0) will be us. - for (int i=0; i < path.size (); i++) - { - SCM ops = (i == path.size () -1) ? operations : SCM_EOL; - - Translator_group * new_group - = path[i]->instantiate (output_def_, ops); - - if (i == path.size () -1) - { - new_group->id_string_ = id; - } - - current->add_fresh_group_translator (new_group); - apply_property_operations (new_group, ops); - - current = new_group; - } - - return current; - } - - Translator_group *ret = 0; - if (daddy_trans_) - ret = daddy_trans_->find_create_translator (n, id, operations); - else + SCM *tail = ℓ + for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p)) { - warning (_f ("can't find or create `%s' called `%s'", ly_symbol2string (n).to_str0 (), id)); - ret =0; + if (unsmob_engraver (scm_car (*tail))) + *tail = scm_cdr (*tail); + else + tail = SCM_CDRLOC (*tail); } - return ret; + return ell; } -bool -Translator_group::try_music (Music* m) -{ - bool hebbes_b = try_music_on_nongroup_children (m); - - if (!hebbes_b && daddy_trans_) - hebbes_b = daddy_trans_->try_music (m); - - return hebbes_b ; -} - -int -Translator_group::get_depth () const -{ - return (daddy_trans_) ? daddy_trans_->get_depth () + 1 : 0; -} - -Translator_group* -Translator_group::get_ancestor (int level) -{ - if (!level || !daddy_trans_) - return this; +/* + Protects the parameter from being garbage collected. The object is + protected until the next disconnect_from_context call. - return daddy_trans_->get_ancestor (level-1); -} + Whenever a child translator hears an event, the event is added to + this list. This eliminates the need for derived_mark methods in most + translators; all incoming events are instead protected by the + translator group. + TODO: Should the list also be flushed at the beginning of each new + moment? + */ void -Translator_group::terminate_translator (Translator*r) +Translator_group::protect_event (SCM ev) { - r->finalize (); - /* - Return value ignored. GC does the rest. - */ - remove_translator (r); + protected_events_ = scm_cons (ev, protected_events_); } - -/** - Remove a translator from the hierarchy. +/* + Create a new translator for a newly created child context. Triggered + by AnnounceNewContext events. */ -Translator * -Translator_group::remove_translator (Translator*trans) +IMPLEMENT_LISTENER (Translator_group, create_child_translator); +void +Translator_group::create_child_translator (SCM sev) { - assert (trans); + Stream_event *ev = unsmob_stream_event (sev); + // get from AnnounceNewContext + SCM cs = ev->get_property ("context"); + Context *new_context = unsmob_context (cs); + Context_def *def = unsmob_context_def (new_context->get_definition ()); + SCM ops = new_context->get_definition_mods (); - trans_group_list_ = scm_delq_x (trans->self_scm (), trans_group_list_); - trans->daddy_trans_ = 0; - return trans; -} + SCM trans_names = def->get_translator_names (ops); -bool -Translator_group::is_bottom_translator_b () const -{ - return !gh_symbol_p (unsmob_translator_def (definition_)->default_child_context_name ()); -} + Translator_group *g = get_translator_group (def->get_translator_group_type ()); + SCM trans_list = SCM_EOL; -Translator_group* -Translator_group::get_default_interpreter () -{ - if (!is_bottom_translator_b ()) + for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s)) { - SCM nm = unsmob_translator_def (definition_)->default_child_context_name (); - SCM st = get_output_def ()->find_translator (nm); - - Translator_def *t = unsmob_translator_def (st); - if (!t) - { - warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ())); - t = unsmob_translator_def (this->definition_); - } - Translator_group *tg = t->instantiate (output_def_, SCM_EOL); - add_fresh_group_translator (tg); - - if (!tg->is_bottom_translator_b ()) - return tg->get_default_interpreter (); + SCM definition = scm_car (s); + bool is_scheme = false; + + Translator *type = 0; + if (ly_is_symbol (definition)) + type = get_translator (definition); + else if (ly_is_pair (definition)) + { + type = get_translator (ly_symbol2scm ("Scheme_engraver")); + is_scheme = true; + } + else if (ly_is_procedure (definition)) + { + // `definition' is a procedure, which takes the context as + // an argument and evaluates to an a-list scheme engraver + // definition. + definition = scm_call_1 (definition, cs); + type = get_translator (ly_symbol2scm ("Scheme_engraver")); + is_scheme = true; + } + + if (!type) + warning (_f ("cannot find: `%s'", ly_symbol2string (scm_car (s)).c_str ())); else - return tg; + { + Translator *instance = type->clone (); + if (is_scheme) + dynamic_cast (instance)->init_from_scheme (definition); + + SCM str = instance->self_scm (); + + if (instance->must_be_last ()) + { + SCM cons = scm_cons (str, SCM_EOL); + if (scm_is_pair (trans_list)) + scm_set_cdr_x (scm_last_pair (trans_list), cons); + else + trans_list = cons; + } + else + trans_list = scm_cons (str, trans_list); + + instance->daddy_context_ = new_context; + instance->unprotect (); + } } - return this; -} -static void -static_each (SCM list, Method_pointer method) -{ - for (SCM p = list; gh_pair_p (p); p = ly_cdr (p)) - (unsmob_translator (ly_car (p))->*method) (); - -} - -void -Translator_group::each (Method_pointer method) -{ - static_each (get_simple_trans_list (), method); - static_each (trans_group_list_, method); -} + /* Filter unwanted translator types. Required to make + \with { \consists "..." } work. */ + if (dynamic_cast (g)) + g->simple_trans_list_ = filter_performers (trans_list); + else if (dynamic_cast (g)) + g->simple_trans_list_ = filter_engravers (trans_list); + // TODO: scrap Context::implementation + new_context->implementation_ = g; -/* - PROPERTIES - */ -Translator_group* -Translator_group::where_defined (SCM sym) const -{ - if (properties_dict ()->contains (sym)) - { - return (Translator_group*)this; - } + g->connect_to_context (new_context); + g->unprotect (); - return (daddy_trans_) ? daddy_trans_->where_defined (sym) : 0; + recurse_over_translators (new_context, + &Translator::initialize, + &Translator_group::initialize, + DOWN); } -/* - return SCM_EOL when not found. -*/ SCM -Translator_group::internal_get_property (SCM sym) const +Translator_group::get_simple_trans_list () { - SCM val =SCM_EOL; - if (properties_dict ()->try_retrieve (sym, &val)) - return val; - - if (daddy_trans_) - return daddy_trans_->internal_get_property (sym); - - return val; + return simple_trans_list_; } void -Translator_group::internal_set_property (SCM sym, SCM val) +precomputed_recurse_over_translators (Context *c, Translator_precompute_index idx, Direction dir) { -#ifndef NDEBUG - if (internal_type_checking_global_b) - assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"))); -#endif - - properties_dict ()->set (sym, val); -} + Translator_group *tg + = dynamic_cast (c->implementation ()); -/* - TODO: look up to check whether we have inherited var? - */ -void -Translator_group::unset_property (SCM sym) -{ - properties_dict ()->remove (sym); -} + if (tg && dir == DOWN) + { + tg->precomputed_translator_foreach (idx); + tg->call_precomputed_self_method (idx); + } + for (SCM s = c->children_contexts (); scm_is_pair (s); + s = scm_cdr (s)) + precomputed_recurse_over_translators (unsmob_context (scm_car (s)), idx, dir); -/* - Push or pop (depending on value of VAL) a single entry (ELTPROP . VAL) - entry from a translator property list by name of PROP -*/ -void -Translator_group::execute_pushpop_property (SCM prop, SCM eltprop, SCM val) -{ - if (gh_symbol_p (prop)) + if (tg && dir == UP) { - if (val != SCM_UNDEFINED) - { - SCM prev = internal_get_property (prop); - - if (gh_pair_p (prev) || prev == SCM_EOL) - { - bool ok = type_check_assignment (eltprop, val, ly_symbol2scm ("backend-type?")); - - if (ok) - { - prev = gh_cons (gh_cons (eltprop, val), prev); - internal_set_property (prop, prev); - } - } - else - { - // warning here. - } - - } - else - { - SCM prev = internal_get_property (prop); - - /* - TODO: should have scm_equal_something () for reverting - autobeam properties. - */ - SCM newprops= SCM_EOL ; - while (gh_pair_p (prev) && !SCM_EQ_P(ly_caar (prev), eltprop)) - { - newprops = gh_cons (ly_car (prev), newprops); - prev = ly_cdr (prev); - } - - if (gh_pair_p (prev)) - { - newprops = scm_reverse_x (newprops, ly_cdr (prev)); - internal_set_property (prop, newprops); - } - } + tg->precomputed_translator_foreach (idx); + tg->call_precomputed_self_method (idx); } } - - -/* - STUBS -*/ void -Translator_group::stop_translation_timestep () +recurse_over_translators (Context *c, Translator_method ptr, + Translator_group_method tg_ptr, Direction dir) { - each (&Translator::stop_translation_timestep); -} + Translator_group *tg + = dynamic_cast (c->implementation ()); -void -Translator_group::start_translation_timestep () -{ - each (&Translator::start_translation_timestep); -} + if (tg && dir == DOWN) + { + (tg->*tg_ptr) (); + translator_each (tg->get_simple_trans_list (), ptr); + } -void -Translator_group::do_announces () -{ - each (&Translator::do_announces); -} + for (SCM s = c->children_contexts (); scm_is_pair (s); + s = scm_cdr (s)) + recurse_over_translators (unsmob_context (scm_car (s)), ptr, tg_ptr, dir); -void -Translator_group::initialize () -{ - SCM tab = scm_make_vector (gh_int2scm (19), SCM_BOOL_F); - set_property ("acceptHashTable", tab); - static_each (trans_group_list_, &Translator::initialize); -} + if (tg && dir == UP) + { + translator_each (tg->get_simple_trans_list (), + ptr); -void -Translator_group::finalize () -{ - each (&Translator::finalize); + (tg->*tg_ptr) (); + } } -bool -translator_accepts_any_of (Translator*tr, SCM ifaces) +Translator_group::Translator_group () { - SCM ack_ifs = scm_assoc (ly_symbol2scm ("events-accepted"), - tr->translator_description()); - ack_ifs = gh_cdr (ack_ifs); - for (SCM s = ifaces; ly_pair_p (s); s = ly_cdr (s)) - if (scm_memq (ly_car (s), ack_ifs) != SCM_BOOL_F) - return true; - return false; + simple_trans_list_ = SCM_EOL; + protected_events_ = SCM_EOL; + context_ = 0; + smobify_self (); } -SCM -find_accept_translators (SCM gravlist, SCM ifaces) +void +Translator_group::derived_mark () const { - SCM l = SCM_EOL; - for (SCM s = gravlist; ly_pair_p (s); s = ly_cdr (s)) - { - Translator* tr = unsmob_translator (ly_car (s)); - if (translator_accepts_any_of (tr, ifaces)) - l = scm_cons (tr->self_scm (), l); - } - l = scm_reverse_x (l, SCM_EOL); - - return l; } -bool -Translator_group::try_music_on_nongroup_children (Music *m ) +void +Translator_group::precompute_method_bindings () { - SCM tab = get_property ("acceptHashTable"); - SCM name = scm_sloppy_assq (ly_symbol2scm ("name"), - m->get_property_alist (false)); - - if (!gh_pair_p (name)) - return false; - - name = gh_cdr (name); - SCM accept_list = scm_hashq_ref (tab, name, SCM_UNDEFINED); - if (accept_list == SCM_BOOL_F) + for (SCM s = simple_trans_list_; scm_is_pair (s); s = scm_cdr (s)) { - accept_list = find_accept_translators (get_simple_trans_list (), - m->get_mus_property ("types")); - scm_hashq_set_x (tab, name, accept_list); + Translator *tr = unsmob_translator (scm_car (s)); + Translator_void_method_ptr ptrs[TRANSLATOR_METHOD_PRECOMPUTE_COUNT]; + tr->fetch_precomputable_methods (ptrs); + + assert (tr); + for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++) + { + if (ptrs[i]) + precomputed_method_bindings_[i].push_back (Translator_method_binding (tr, ptrs[i])); + } } - for (SCM p = accept_list; gh_pair_p (p); p = ly_cdr (p)) - { - Translator * t = unsmob_translator (ly_car (p)); - if (t && t->try_music (m)) - return true; - } - return false; + fetch_precomputable_methods (precomputed_self_method_bindings_); } -SCM -Translator_group::properties_as_alist () const +void +Translator_group::precomputed_translator_foreach (Translator_precompute_index idx) { - return properties_dict()->to_alist(); + vector &bindings (precomputed_method_bindings_[idx]); + for (vsize i = 0; i < bindings.size (); i++) + bindings[i].invoke (); } -String -Translator_group::context_name () const +void +Translator_group::fetch_precomputable_methods (Translator_group_void_method ptrs[]) { - Translator_def * td = unsmob_translator_def (definition_ ); - return ly_symbol2string (td->get_context_name ()); + for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++) + ptrs[i] = 0; } -/* - PRE_INIT_OPS is in the order specified, and hence must be reversed. - */ void -apply_property_operations (Translator_group*tg, SCM pre_init_ops) +Translator_group::call_precomputed_self_method (Translator_precompute_index idx) { - SCM correct_order = scm_reverse (pre_init_ops); - for (SCM s = correct_order; gh_pair_p (s); s = ly_cdr (s)) - { - SCM entry = ly_car (s); - SCM type = ly_car (entry); - entry = ly_cdr (entry); - - if (type == ly_symbol2scm ("push") || type == ly_symbol2scm ("poppush")) - { - SCM val = ly_cddr (entry); - val = gh_pair_p (val) ? ly_car (val) : SCM_UNDEFINED; - - tg->execute_pushpop_property (ly_car (entry), ly_cadr (entry), val); - } - else if (type == ly_symbol2scm ("assign")) - { - tg->internal_set_property (ly_car (entry), ly_cadr (entry)); - } - } + if (precomputed_self_method_bindings_[idx]) + (*precomputed_self_method_bindings_[idx]) (this); } -SCM -names_to_translators (SCM namelist, Translator_group*tg) +Translator_group::~Translator_group () { - SCM l = SCM_EOL; - for (SCM s = namelist; gh_pair_p (s) ; s = ly_cdr (s)) - { - Translator * t = get_translator (ly_car (s)); - if (!t) - warning (_f ("can't find: `%s'", s)); - else - { - Translator * tr = t->clone (); - SCM str = tr->self_scm (); - l = gh_cons (str, l); +} - tr->daddy_trans_ = tg; - tr->output_def_ = tg->output_def_; +#include "ly-smobs.icc" - scm_gc_unprotect_object (str); - } - } - return l; -} +IMPLEMENT_SMOBS (Translator_group); +IMPLEMENT_DEFAULT_EQUAL_P (Translator_group); +IMPLEMENT_TYPE_P (Translator_group, "ly:translator-group?"); +int +Translator_group::print_smob (SCM s, SCM port, scm_print_state *) +{ + Translator_group *me = (Translator_group *) SCM_CELL_WORD_1 (s); + scm_puts ("#class_name (), port); + scm_display (me->simple_trans_list_, port); + scm_puts (" >", port); + return 1; +} SCM -Translator_group::get_simple_trans_list () +Translator_group::mark_smob (SCM smob) { - if (simple_trans_list_ != SCM_BOOL_F) - return simple_trans_list_; - - Translator_def * td = unsmob_translator_def (definition_); - - /* - The following cannot work, since start_translation_timestep () - triggers this code, and start_translation_timestep happens before - \property Voice.Voice =#'() - - */ - SCM trans_names = internal_get_property (td->get_context_name ()); - if (!gh_pair_p (trans_names)) - { - trans_names = td->get_translator_names (SCM_EOL); - } + Translator_group *me = (Translator_group *)SCM_CELL_WORD_1 (smob); - simple_trans_list_ = names_to_translators (trans_names, this); - - - static_each (simple_trans_list_, &Translator::initialize); - return simple_trans_list_; + me->derived_mark (); + scm_gc_mark (me->protected_events_); + return me->simple_trans_list_; }