]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile.hh
release commit
[lilypond.git] / lily / include / lily-guile.hh
1 /*
2   lily-guile.hh encapsulate guile
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #ifndef LILY_GUILE_HH
10 #define LILY_GUILE_HH
11
12 #include <libguile.h>
13 #include "flower-proto.hh"
14
15 #include "drul-array.hh"
16 #include "direction.hh"
17
18 #if SCM_MINOR_VERSION < 7
19 /* guile-1.6.x compatibility */
20 #define scm_gc_unregister_collectable_memory(a, b, c) scm_done_free (b)
21 #define scm_gc_register_collectable_memory(a, b, c) scm_done_malloc (b)
22 #define SCM_VECTOR_REF(v,i) (SCM_VELTS ((v))[(i)])
23 #define scm_from_int(x) SCM_MAKINUM (x)
24 #define scm_is_integer(x) SCM_INUMP (x)
25 #define scm_is_string(x) SCM_STRINGP(x)
26 #define scm_hash_table_p scm_vector_p
27 #define scm_i_string_chars(x) SCM_STRING_CHARS(x)
28 #define scm_i_string_length(x) SCM_STRING_LENGTH(x)
29 inline bool ly_c_number_p (SCM x) { return SCM_NUMBERP (x); }
30 #define scm_is_number(x) (scm_number_p(x)==SCM_BOOL_T)
31 inline int ly_scm2int (SCM x) { return scm_num2int (x, 0, "ly_scm2int"); }
32 #define scm_to_int(x) (ly_scm2int(x))
33 inline bool ly_c_symbol_p (SCM x) { return SCM_SYMBOLP (x); }
34 #define scm_is_symbol(x) ly_c_symbol_p(x)
35 inline bool ly_c_boolean_p (SCM x) { return SCM_BOOLP (x); }
36 #define scm_is_bool(x) ly_c_boolean_p(x)
37 inline bool ly_c_eq_p (SCM x, SCM y) { return SCM_EQ_P (x, y); }
38 #define scm_is_eq(x,y)  (SCM_EQ_P((x), (y)))
39
40 #define scm_is_pair(x) (SCM_CONSP(x))
41
42
43 inline double ly_scm2double (SCM x) { return scm_num2dbl (x, "ly_scm2double"); }
44 #define scm_to_double(x) (ly_scm2double(x))
45 #define scm_from_double(x) (scm_make_real(x))
46
47
48
49 #endif /* SCM_MINOR_VERSION < 7 */
50
51 #ifndef SMOB_FREE_RETURN_VAL
52 #define SMOB_FREE_RETURN_VAL(CL) 0
53 #endif
54
55 #ifndef SCM_PACK
56 #define SCM_PACK(x) ((SCM) x)
57 #endif
58
59 #ifndef SCM_UNPACK
60 #define SCM_UNPACK(x) (x)
61 #endif
62
63 /** Conversion functions follow the GUILE naming convention, i.e.
64     A ly_B2A (B b);  */
65
66 SCM ly_last (SCM list);
67 SCM ly_write2scm (SCM s);
68 SCM ly_deep_copy (SCM);
69 SCM ly_truncate_list (int k, SCM lst);
70
71 #if (__GNUC__ > 2)
72 /* Unreliable with gcc-2.x
73    FIXME: should add check for x86 as well?  */
74 #define CACHE_SYMBOLS
75 #endif
76
77 #ifdef CACHE_SYMBOLS
78
79 /* Using this trick we cache the value of scm_str2symbol ("fooo") where
80   "fooo" is a constant string. This is done at the cost of one static
81   variable per ly_symbol2scm() use, and one boolean evaluation for
82   every call.
83
84   The overall speedup of lily is about 5% on a run of wtk1-fugue2.  */
85 #define ly_symbol2scm(x) \
86 ({ \
87   static SCM cached; \
88   /* We store this one locally, since G++ -O2 fucks up else */ \
89   SCM value = cached; \
90   if ( __builtin_constant_p ((x))) \
91     { \
92       if (!cached) \
93         value = cached =  scm_gc_protect_object (scm_str2symbol ((x))); \
94     } \
95   else \
96     value = scm_str2symbol ((char*) (x)); \
97   value; \
98 })
99 #else
100 inline SCM ly_symbol2scm(char const* x) { return scm_str2symbol ((x)); }
101 #endif
102
103 extern SCM global_lily_module;
104
105 /*
106   TODO: rename me to ly_c_lily_module_eval
107  */
108 #define ly_scheme_function(x) \
109 ({ \
110   static SCM cached; \
111   /* We store this one locally, since G++ -O2 fucks up else */ \
112   SCM value = cached; \
113   if ( __builtin_constant_p ((x))) \
114     { \
115       if (!cached) \
116         value = cached = scm_gc_protect_object (scm_eval (scm_str2symbol (x), \
117                                                 global_lily_module)); \
118     } \
119   else \
120     value = scm_eval (scm_str2symbol (x), global_lily_module); \
121   value; \
122 })
123
124
125 String ly_scm2string (SCM s);
126 String ly_symbol2string (SCM);
127 SCM ly_offset2scm (Offset);
128 Offset ly_scm2offset (SCM);
129 SCM ly_assoc_chain (SCM key, SCM achain);
130 SCM ly_assoc_cdr (SCM key, SCM alist);
131 SCM ly_assoc_get (SCM key, SCM alist, SCM def);
132 Interval ly_scm2interval (SCM);
133 Drul_array<Real> ly_scm2realdrul (SCM);
134 Slice int_list_to_slice (SCM l);
135 SCM ly_interval2scm (Drul_array<Real>);
136 char *ly_scm2newstr (SCM str, size_t *lenp);
137
138 Real robust_scm2double (SCM, double);
139 int robust_scm2int (SCM, int);
140 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
141 Interval robust_scm2interval (SCM, Drul_array<Real>);
142 Offset robust_scm2offset (SCM, Offset);
143
144 SCM ly_quote_scm (SCM s);
145 bool type_check_assignment (SCM val, SCM sym,  SCM type_symbol) ;
146 String print_scm_val (SCM val);
147 SCM ly_number2string (SCM s);
148
149 SCM parse_symbol_list (char const *);
150 SCM robust_list_ref(int i, SCM l);
151 SCM alist_to_hashq (SCM);
152
153
154 /* inserts at front, removing dublicates */
155 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
156 {
157   return scm_acons(key, val, scm_assoc_remove_x (alist, key));
158 }
159 inline bool ly_c_char_p (SCM x) { return SCM_CHARP (x); }
160 inline bool ly_c_vector_p (SCM x) { return SCM_VECTORP (x); }
161 inline bool ly_c_list_p (SCM x) { return SCM_NFALSEP (scm_list_p (x)); }
162 inline bool ly_c_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
163 inline bool ly_c_equal_p (SCM x, SCM y) {
164   return SCM_NFALSEP (scm_equal_p (x, y));
165 }
166 inline SCM ly_cdr (SCM x) {
167   if (SCM_NCONSP(x))
168     abort();
169   return SCM_CDR (x);
170 }
171 inline SCM ly_car (SCM x) {
172   if (SCM_NCONSP(x))
173     abort();    
174   return SCM_CAR (x);
175 }
176 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
177 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
178 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
179 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
180 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
181 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
182 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
183 inline SCM ly_cadar (SCM x) { return SCM_CADAR (x); }
184 inline bool ly_scm2bool (SCM x) { return SCM_NFALSEP (x); }
185 inline char ly_scm2char (SCM x) { return SCM_CHAR(x); }
186 inline unsigned long ly_length (SCM x) {
187   return scm_num2ulong (scm_length (x), 0, "ly_length");
188 }
189 inline unsigned long ly_vector_length (SCM x) { return SCM_VECTOR_LENGTH (x); }
190
191 inline SCM ly_bool2scm (bool x) { return SCM_BOOL (x); }
192
193 inline SCM ly_append2 (SCM x1, SCM x2) {
194   return scm_append (scm_listify (x1, x2, SCM_UNDEFINED));
195 }
196 inline SCM ly_append3 (SCM x1, SCM x2, SCM x3) {
197   return scm_append (scm_listify (x1, x2, x3, SCM_UNDEFINED));
198 }
199 inline SCM ly_append4 (SCM x1, SCM x2, SCM x3, SCM x4) {
200   return scm_append (scm_listify (x1, x2, x3, x4, SCM_UNDEFINED));
201 }
202
203 /*
204   display and print newline.
205  */
206 extern "C" {
207 void ly_display_scm (SCM s);
208 }
209
210 void read_lily_scm_file (String);
211 void ly_c_init_guile ();
212
213 bool is_direction (SCM s);
214 bool is_number_pair (SCM);
215 bool is_axis (SCM);
216
217 /*
218   these conversion functions also do a typecheck on the argument, and
219   return a default value if S has the wrong type.
220 */
221
222 Direction to_dir (SCM s);
223 bool to_boolean (SCM s);
224
225 void init_ly_protection ();
226 unsigned int ly_scm_hash (SCM s);
227
228 SCM index_get_cell (SCM cell, Direction d);
229 SCM index_set_cell (SCM cell, Direction d, SCM val);
230
231 SCM ly_snoc (SCM s, SCM list);
232 SCM ly_split_list (SCM s, SCM lst);
233 SCM ly_unique (SCM lst);
234 SCM ly_list_qsort_uniq_x (SCM lst);
235
236 SCM ly_output_formats();
237 SCM ly_kpathsea_expand_path (SCM);
238
239 /*
240   snarfing.
241  */
242 void add_scm_init_func (void (*) ());
243
244 extern "C" {
245 typedef SCM (*Scheme_function_unknown) ();
246 }
247
248 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
249 typedef SCM (*Scheme_function_0) ();
250 typedef SCM (*Scheme_function_1) (SCM);
251 typedef SCM (*Scheme_function_2) (SCM,SCM);     
252 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);        
253 #else
254 typedef SCM (*Scheme_function_0) (...);
255 typedef SCM (*Scheme_function_1) (...);
256 typedef SCM (*Scheme_function_2) (...);
257 typedef SCM (*Scheme_function_3) (...);
258 #endif
259
260
261 /*
262   Adds the NAME as a Scheme function, and a variable to store the SCM
263   version of the function in the static variable NAME_proc
264  */
265 #define DECLARE_SCHEME_CALLBACK(NAME, ARGS) \
266         static SCM NAME ARGS; \
267         static SCM NAME ## _proc
268
269 /*
270   Make TYPE::FUNC available as a Scheme function.
271  */
272 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
273 SCM TYPE :: FUNC ## _proc; \
274 void \
275 TYPE ## _ ## FUNC ## _init_functions () \
276 { \
277   TYPE :: FUNC ## _proc = scm_c_define_gsubr (#TYPE "::" #FUNC, \
278                                               (ARGCOUNT), 0, 0, \
279                           (Scheme_function_unknown)TYPE :: FUNC); \
280   scm_c_export (#TYPE "::" #FUNC, NULL); \
281 } \
282  \
283 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, \
284                    TYPE ## _ ## FUNC ## _init_functions);
285
286
287 void
288 ly_add_function_documentation (SCM proc, char const *fname,
289                                char const *varlist,
290                                char const *doc);
291
292 #define ADD_SCM_INIT_FUNC(name, func) \
293 class name ## _scm_initter \
294 { \
295 public: \
296   name ## _scm_initter () \
297   { \
298     add_scm_init_func (func); \
299   } \
300 } _ ## name ## _scm_initter; \
301 /* end define */
302
303 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, \
304                                ARGLIST, DOCSTRING) \
305 SCM FNAME ## _proc; \
306 void \
307 INITPREFIX ## init () \
308 { \
309   FNAME ## _proc = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, \
310                                        (Scheme_function_unknown) FNAME); \
311   ly_add_function_documentation (FNAME ## _proc, PRIMNAME, #ARGLIST, \
312                                  DOCSTRING); \
313   scm_c_export (PRIMNAME, NULL); \
314 } \
315 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init); \
316 SCM \
317 FNAME ARGLIST
318
319
320 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
321 SCM FNAME ARGLIST; \
322 LY_DEFINE_WITHOUT_DECL (FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, \
323                         DOCSTRING)
324
325 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, \
326                                   ARGLIST, DOCSTRING) \
327 SCM FNAME ARGLIST; \
328 LY_DEFINE_WITHOUT_DECL (CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, \
329                         VAR, ARGLIST, DOCSTRING)
330
331 #define get_property(x) internal_get_property (ly_symbol2scm (x))
332 #define set_property(x,y) internal_set_property (ly_symbol2scm (x), y)
333
334 #endif /* LILY_GUILE_HH */