]> git.donarmstrong.com Git - lilypond.git/blob - lily/engraver.cc
Issue 4814: grob.cc segfaults with gcc6
[lilypond.git] / lily / engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--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 #include "engraver.hh"
21
22 #include "context.hh"
23 #include "grob-properties.hh"
24 #include "international.hh"
25 #include "music.hh"
26 #include "paper-column.hh"
27 #include "score-engraver.hh"
28 #include "spanner.hh"
29 #include "stream-event.hh"
30 #include "warn.hh"
31
32 Engraver_group *
33 Engraver::get_daddy_engraver () const
34 {
35   return dynamic_cast<Engraver_group *> (get_daddy_translator ());
36 }
37
38 void
39 Engraver::announce_grob (Grob_info inf, Context *reroute_context)
40 {
41   get_daddy_engraver ()->announce_grob (inf, START, reroute_context);
42 }
43
44 void
45 Engraver::announce_end_grob (Grob_info inf, Context *reroute_context)
46 {
47   get_daddy_engraver ()->announce_grob (inf, STOP, reroute_context);
48 }
49
50 Grob_info
51 Engraver::make_grob_info (Grob *e, SCM cause)
52 {
53   /* TODO: Remove Music code when it's no longer needed */
54   if (Music *m = unsmob<Music> (cause))
55     {
56       cause = m->to_event ()->unprotect ();
57     }
58   if (scm_is_null (e->get_property ("cause"))
59       && (unsmob<Stream_event> (cause) || unsmob<Grob> (cause)))
60     e->set_property ("cause", cause);
61
62   return Grob_info (this, e);
63 }
64
65 /*
66   CAUSE is the object (typically a Stream_event object)  that
67   was the reason for making E.
68 */
69 void
70 Engraver::announce_grob (Grob *e, SCM cause)
71 {
72   announce_grob (make_grob_info (e, cause));
73 }
74
75 /*
76   CAUSE is the object (typically a grob or stream-event object) that
77   was the reason for ending E.  */
78 void
79 Engraver::announce_end_grob (Grob *e, SCM cause)
80 {
81   announce_end_grob (make_grob_info (e, cause));
82 }
83
84 Engraver::Engraver ()
85 {
86 }
87
88 #ifdef DEBUG
89 static SCM creation_callback = SCM_EOL;
90 LY_DEFINE (ly_set_grob_creation_callback, "ly:set-grob-creation-callback",
91            1, 0, 0, (SCM cb),
92            "Specify a procedure that will be called every time a new grob"
93            " is created.  The callback will receive as arguments the grob"
94            " that was created, the name of the C++ source file that caused"
95            " the grob to be created, and the corresponding line number in"
96            " the C++ source file.")
97 {
98   LY_ASSERT_TYPE (ly_is_procedure, cb, 1);
99
100   creation_callback = cb;
101
102   return SCM_UNSPECIFIED;
103 }
104 #endif
105
106 Grob *
107 Engraver::internal_make_grob (SCM symbol,
108                               SCM cause,
109                               char const *file,
110                               int line,
111                               char const *fun)
112 {
113 #ifndef DEBUG
114   (void)file;
115   (void)line;
116   (void)fun;
117 #endif
118
119   SCM props = Grob_property_info (context (), symbol).updated ();
120
121   Grob *grob = 0;
122
123   SCM handle = scm_sloppy_assq (ly_symbol2scm ("meta"), props);
124   SCM klass = scm_cdr (scm_sloppy_assq (ly_symbol2scm ("class"), scm_cdr (handle)));
125
126   if (scm_is_eq (klass, ly_symbol2scm ("Item")))
127     grob = new Item (props);
128   else if (scm_is_eq (klass, ly_symbol2scm ("Spanner")))
129     grob = new Spanner (props);
130   else if (scm_is_eq (klass, ly_symbol2scm ("Paper_column")))
131     grob = new Paper_column (props);
132
133   assert (grob);
134   announce_grob (grob, cause);
135
136 #ifdef DEBUG
137   if (ly_is_procedure (creation_callback))
138     scm_call_4 (creation_callback,
139                 grob->self_scm (), scm_from_utf8_string (file),
140                 scm_from_int (line), scm_from_ascii_string (fun));
141 #endif
142
143   return grob;
144 }
145
146 Item *
147 Engraver::internal_make_item (SCM x, SCM cause,
148                               char const *file, int line, char const *fun)
149 {
150   Item *it = dynamic_cast<Item *> (internal_make_grob (x, cause, file, line, fun));
151   assert (it);
152   return it;
153 }
154
155 Paper_column *
156 Engraver::internal_make_column (SCM x, char const *file, int line, char const *fun)
157 {
158   return dynamic_cast<Paper_column *> (internal_make_grob (x, SCM_EOL, file, line, fun));
159 }
160
161 Spanner *
162 Engraver::internal_make_spanner (SCM x, SCM cause,
163                                  char const *file, int line, char const *fun)
164 {
165   Spanner *sp = dynamic_cast<Spanner *> (internal_make_grob (x, cause, file, line, fun));
166   assert (sp);
167   return sp;
168 }
169
170 bool
171 ly_is_grob_cause (SCM obj)
172 {
173   return unsmob<Grob> (obj) || unsmob<Stream_event> (obj) || scm_is_null (obj);
174 }