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