]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-def.cc
Issue 4135/2: Replace mark_smob static member functions with non-static members
[lilypond.git] / lily / context-def.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2014 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 /* TODO: should junk this class an replace by
21    a single list of context modifications?  */
22
23 #include "context-def.hh"
24
25 #include "context.hh"
26 #include "context-mod.hh"
27 #include "international.hh"
28 #include "output-def.hh"
29 #include "translator.hh"
30 #include "warn.hh"
31
32 Context_def::Context_def ()
33 {
34   context_aliases_ = SCM_EOL;
35   translator_group_type_ = SCM_EOL;
36   accept_mods_ = SCM_EOL;
37   translator_mods_ = SCM_EOL;
38   property_ops_ = SCM_EOL;
39   context_name_ = SCM_EOL;
40   default_child_ = SCM_EOL;
41   description_ = SCM_EOL;
42   input_location_ = SCM_EOL;
43
44   smobify_self ();
45
46   input_location_ = Input ().smobbed_copy ();
47   context_name_ = ly_symbol2scm ("");
48 }
49
50 Input *
51 Context_def::origin () const
52 {
53   return Input::unsmob (input_location_);
54 }
55
56 Context_def::Context_def (Context_def const &s)
57 {
58   context_aliases_ = SCM_EOL;
59   translator_group_type_ = SCM_EOL;
60   accept_mods_ = SCM_EOL;
61   translator_mods_ = SCM_EOL;
62   property_ops_ = SCM_EOL;
63   context_name_ = SCM_EOL;
64   description_ = SCM_EOL;
65   default_child_ = SCM_EOL;
66   input_location_ = SCM_EOL;
67   smobify_self ();
68
69   description_ = s.description_;
70   input_location_ = s.origin ()->smobbed_copy ();
71   default_child_ = s.default_child_;
72   accept_mods_ = s.accept_mods_;
73   property_ops_ = s.property_ops_;
74   translator_mods_ = s.translator_mods_;
75   context_aliases_ = s.context_aliases_;
76   translator_group_type_ = s.translator_group_type_;
77   context_name_ = s.context_name_;
78 }
79
80 Context_def::~Context_def ()
81 {
82 }
83
84 const char Context_def::type_p_name_[] = "ly:context-def?";
85
86 int
87 Context_def::print_smob (SCM smob, SCM port, scm_print_state *)
88 {
89   Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
90
91   scm_puts ("#<Context_def ", port);
92   scm_display (me->context_name_, port);
93   scm_puts (" ", port);
94   string loc = me->origin ()->location_string ();
95   scm_puts (loc.c_str (), port);
96   scm_puts (">", port);
97   return 1;
98 }
99
100 SCM
101 Context_def::mark_smob ()
102 {
103   ASSERT_LIVE_IS_ALLOWED (self_scm ());
104
105   scm_gc_mark (description_);
106   scm_gc_mark (context_aliases_);
107   scm_gc_mark (accept_mods_);
108   scm_gc_mark (translator_mods_);
109   scm_gc_mark (property_ops_);
110   scm_gc_mark (translator_group_type_);
111   scm_gc_mark (default_child_);
112   scm_gc_mark (input_location_);
113
114   return context_name_;
115 }
116
117 void
118 Context_def::add_context_mod (SCM mod)
119 {
120   SCM tag = scm_car (mod);
121   if (ly_symbol2scm ("description") == tag)
122     {
123       description_ = scm_cadr (mod);
124       return;
125     }
126
127   /*
128     other modifiers take symbols as argument.
129   */
130   SCM sym = scm_cadr (mod);
131   if (scm_is_string (sym))
132     sym = scm_string_to_symbol (sym);
133
134   if (ly_symbol2scm ("default-child") == tag)
135     default_child_ = sym;
136   else if (ly_symbol2scm ("consists") == tag
137            || ly_symbol2scm ("remove") == tag)
138     {
139       translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
140     }
141   else if (ly_symbol2scm ("accepts") == tag
142            || ly_symbol2scm ("denies") == tag)
143     accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
144   else if (ly_symbol2scm ("pop") == tag
145            || ly_symbol2scm ("push") == tag
146            || ly_symbol2scm ("assign") == tag
147            || ly_symbol2scm ("unset") == tag
148            || ly_symbol2scm ("apply") == tag)
149     property_ops_ = scm_cons (mod, property_ops_);
150   else if (ly_symbol2scm ("alias") == tag)
151     context_aliases_ = scm_cons (sym, context_aliases_);
152   else if (ly_symbol2scm ("translator-type") == tag)
153     translator_group_type_ = sym;
154   else if (ly_symbol2scm ("context-name") == tag)
155     context_name_ = sym;
156   else
157     programming_error ("unknown context mod tag");
158 }
159
160 SCM
161 Context_def::get_accepted (SCM user_mod) const
162 {
163   SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
164   SCM acc = SCM_EOL;
165   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
166     {
167       SCM tag = scm_caar (s);
168       SCM sym = scm_cadar (s);
169       if (tag == ly_symbol2scm ("accepts"))
170         acc = scm_cons (sym, acc);
171       else if (tag == ly_symbol2scm ("denies"))
172         acc = scm_delete_x (sym, acc);
173     }
174
175   SCM def = get_default_child (user_mod);
176   if (scm_is_symbol (def))
177     {
178       acc = scm_delete_x (def, acc);
179       acc = scm_cons (def, acc);
180     }
181
182   return acc;
183 }
184
185 SCM
186 Context_def::get_default_child (SCM user_mod) const
187 {
188   SCM name = default_child_;
189   for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
190     {
191       SCM entry = scm_car (s);
192       if (scm_car (entry) == ly_symbol2scm ("default-child"))
193         {
194           name = scm_cadr (entry);
195           break;
196         }
197     }
198
199   return name;
200 }
201
202 /*
203   Given a name of a context that we want to create, finds a list of context
204   definitions such that:
205    - the first element in the list defines a context that is a valid child of
206      the context defined by this Context_def
207    - each subsequent element in the list defines a context that is a valid child
208      of the context defined by the preceding element in the list
209    - the last element in the list defines a context with the given name
210
211   The ADDITIONAL_ACCEPTS parameter is a list of additional contexts that this
212   specific context def (but not any of the child context defs) should accept.
213 */
214 vector<Context_def *>
215 Context_def::path_to_acceptable_context (SCM type_sym,
216                                          Output_def *odef,
217                                          SCM additional_accepts) const
218 {
219   set<const Context_def *> seen;
220   return internal_path_to_acceptable_context (type_sym, odef, additional_accepts, &seen);
221 }
222
223 /*
224 The SEEN parameter is a set which keeps track of visited contexts, allowing
225 contexts of the same type to be nested.
226 */
227 vector<Context_def *>
228 Context_def::internal_path_to_acceptable_context (SCM type_sym,
229                                                   Output_def *odef,
230                                                   SCM additional_accepts,
231                                                   set<const Context_def *> *seen) const
232 {
233   assert (scm_is_symbol (type_sym));
234
235   SCM accepted = get_accepted (additional_accepts);
236
237   vector<Context_def *> accepteds;
238   for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
239     if (Context_def *t = Context_def::unsmob (find_context_def (odef,
240                                                                scm_car (s))))
241       accepteds.push_back (t);
242
243   vector<Context_def *> best_result;
244   for (vsize i = 0; i < accepteds.size (); i++)
245     {
246       /* do not check aliases, because \context Staff should not
247          create RhythmicStaff. */
248       if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
249         {
250           best_result.push_back (accepteds[i]);
251           return best_result;
252         }
253     }
254
255   seen->insert (this);
256   vsize best_depth = INT_MAX;
257   for (vsize i = 0; i < accepteds.size (); i++)
258     {
259       Context_def *g = accepteds[i];
260
261       if (!seen->count (g))
262         {
263           vector<Context_def *> result
264             = g->internal_path_to_acceptable_context (type_sym, odef, SCM_EOL, seen);
265           if (result.size () && result.size () < best_depth)
266             {
267               best_depth = result.size ();
268               result.insert (result.begin (), g);
269               best_result = result;
270             }
271         }
272     }
273   seen->erase (this);
274
275   return best_result;
276 }
277
278 SCM
279 Context_def::get_translator_names (SCM user_mod) const
280 {
281   SCM l1 = SCM_EOL;
282
283   SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
284
285   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
286     {
287       SCM tag = scm_caar (s);
288       SCM arg = scm_cadar (s);
289
290       if (scm_is_string (arg))
291         arg = scm_string_to_symbol (arg);
292
293       if (ly_symbol2scm ("consists") == tag)
294         l1 = scm_cons (arg, l1);
295       else if (ly_symbol2scm ("remove") == tag
296                && (scm_is_pair (arg)
297                    || ly_is_procedure (arg)
298                    || get_translator (arg)))
299         l1 = scm_delete_x (arg, l1);
300     }
301
302   return l1;
303 }
304
305 Context *
306 Context_def::instantiate (SCM ops)
307 {
308   Context *context = new Context ();
309
310   context->definition_ = self_scm ();
311   context->definition_mods_ = ops;
312   context->aliases_ = context_aliases_;
313   context->accepts_list_ = get_accepted (ops);
314   context->default_child_ = get_default_child (ops);
315
316   return context;
317 }
318
319 SCM
320 Context_def::make_scm ()
321 {
322   Context_def *t = new Context_def;
323   return t->unprotect ();
324 }
325
326 void
327 Context_def::apply_default_property_operations (Context *tg)
328 {
329   apply_property_operations (tg, scm_reverse (property_ops_));
330 }
331
332 SCM
333 Context_def::to_alist () const
334 {
335   SCM ell = SCM_EOL;
336
337   ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
338                             get_translator_names (SCM_EOL)), ell);
339   ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
340   ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
341   ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
342                   ell);
343   if (scm_is_symbol (default_child_))
344     ell = scm_acons (ly_symbol2scm ("default-child"), default_child_, ell);
345   ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
346                   ell);
347   ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
348                   ell);
349   ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
350                   ell);
351
352   if (scm_is_symbol (translator_group_type_))
353     ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
354                               translator_group_type_), ell);
355   return ell;
356 }
357
358 SCM
359 Context_def::lookup (SCM sym) const
360 {
361   if (scm_is_eq (ly_symbol2scm ("default-child"), sym))
362     return default_child_;
363   else if (scm_is_eq (ly_symbol2scm ("consists"), sym))
364     return get_translator_names (SCM_EOL);
365   else if (scm_is_eq (ly_symbol2scm ("description"), sym))
366     return description_;
367   else if (scm_is_eq (ly_symbol2scm ("aliases"), sym))
368     return context_aliases_;
369   else if (scm_is_eq (ly_symbol2scm ("accepts"), sym))
370     return get_accepted (SCM_EOL);
371   else if (scm_is_eq (ly_symbol2scm ("property-ops"), sym))
372     return property_ops_;
373   else if (scm_is_eq (ly_symbol2scm ("context-name"), sym))
374     return context_name_;
375   else if (scm_is_eq (ly_symbol2scm ("group-type"), sym))
376     return translator_group_type_;
377   return SCM_UNDEFINED;
378 }
379
380 bool
381 Context_def::is_alias (SCM sym) const
382 {
383   if (scm_is_eq (sym, ly_symbol2scm ("Bottom")))
384     return !scm_is_symbol (get_default_child (SCM_EOL));
385
386   if (scm_is_eq (sym, get_context_name ()))
387     return true;
388
389   return scm_is_true (scm_c_memq (sym, context_aliases_));
390 }
391
392 LY_DEFINE (ly_context_def_lookup, "ly:context-def-lookup",
393            2, 1, 0, (SCM def, SCM sym, SCM val),
394            "Return the value of @var{sym} in context definition @var{def}"
395            " (e.g., @code{\\Voice}).  If no value is found, return"
396            " @var{val} or @code{'()} if @var{val} is undefined."
397            " @var{sym} can be any of @samp{default-child}, @samp{consists},"
398            " @samp{description}, @samp{aliases}, @samp{accepts},"
399            " @samp{property-ops}, @samp{context-name}, @samp{group-type}.")
400 {
401   LY_ASSERT_SMOB (Context_def, def, 1);
402   Context_def *cd = Context_def::unsmob (def);
403   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
404
405   SCM res = cd->lookup (sym);
406
407   scm_remember_upto_here_1 (def);
408
409   if (SCM_UNBNDP (res))
410     res = SCM_EOL;
411
412   if (scm_is_null (res) && !SCM_UNBNDP (val))
413     return val;
414
415   return res;
416 }
417
418 LY_DEFINE (ly_context_def_modify, "ly:context-def-modify",
419            2, 0, 0, (SCM def, SCM mod),
420            "Return the result of applying the context-mod @var{mod} to"
421            " the context definition @var{def}.  Does not change @var{def}.")
422 {
423   LY_ASSERT_SMOB (Context_def, def, 1);
424   LY_ASSERT_SMOB (Context_mod, mod, 2);
425
426   Context_def *cd = Context_def::unsmob (def)->clone ();
427
428   for (SCM s = Context_mod::unsmob (mod)->get_mods ();
429        scm_is_pair (s);
430        s = scm_cdr (s))
431     cd->add_context_mod (scm_car (s));
432
433   return cd->unprotect ();
434 }