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