]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile.hh
* aclocal.m4: Regenerate.
[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--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #ifndef LILY_GUILE_HH
10 #define LILY_GUILE_HH
11
12 #define SCM_VOIDP_TEST
13
14
15 /*
16   TODO: the  GH interface is deprecated as of GUILE 1.6
17
18   Remove all gh_XXX functions.
19  */
20 #include <guile/gh.h>
21
22
23 /* GUILE only includes version in headers (libguile/version.h) as of
24    1.5.x.  For some strange reason, they call it SCM.*VERSION.
25
26    Not including config.h here, saves a lot of unnecessary
27    recompiles. */
28 #include <libguile.h>
29
30 #ifndef GUILE_MAJOR_VERSION
31 #ifdef SCM_MAJOR_VERSION
32 #define GUILE_MAJOR_VERSION SCM_MAJOR_VERSION
33 #define GUILE_MINOR_VERSION SCM_MINOR_VERSION
34 #define GUILE_PATCH_LEVEL SCM_MICRO_VERSION
35 #else
36 #include "config.h"
37 #endif
38 #endif
39
40 #include "drul-array.hh"
41
42
43 /* Guile 1.3.4 compatibility */
44 #if GUILE_MINOR_VERSION < 4
45
46
47 #ifndef SCM_CELL_TYPE
48 #define SCM_CELL_TYPE(X) SCM_CAR (X)
49 #endif
50
51 #ifndef SCM_CELL_WORD_1
52 #define SCM_CELL_WORD_1(X) SCM_CDR (X)
53 #endif
54
55 #define scm_bits_t SCM
56
57 #define fix_guile_1_3_4_scm_puts(scm_data, port) scm_puts ((char*)scm_data, port)
58 #define scm_puts(scm_data, port) fix_guile_1_3_4_scm_puts (scm_data, port)
59 #endif
60
61 /* Guile 1.4.x compatibility */
62 #if GUILE_MINOR_VERSION < 5
63
64 #define scm_t_bits scm_bits_t
65
66 #define fix_guile_1_4_gh_scm2newstr(str, lenp) gh_scm2newstr (str, (int*)lenp)
67 #define gh_scm2newstr(str, lenp) fix_guile_1_4_gh_scm2newstr (str, lenp)
68
69 #define fix_guile_1_4_scm_primitive_eval(form) scm_eval_3 (form, 1, SCM_EOL)
70 #define scm_primitive_eval(form) fix_guile_1_4_scm_primitive_eval (form)
71
72 #define scm_current_module() (SCM)0
73 #define scm_set_current_module(x) (void)x
74 #define scm_c_resolve_module(x) (SCM)0
75
76 #define scm_c_define_gsubr scm_make_gsubr
77 #define scm_c_eval_string(str) gh_eval_str ((char*)str)
78 #define scm_c_memq scm_sloppy_memq
79 #define scm_gc_protect_object scm_protect_object
80 #define scm_gc_unprotect_object scm_unprotect_object
81 #define scm_list_n scm_listify
82 #define SCM_STRING_CHARS SCM_CHARS
83 #define SCM_STRING_LENGTH SCM_LENGTH
84 #define SCM_SYMBOL_CHARS SCM_CHARS
85 #define SCM_SYMBOL_LENGTH SCM_LENGTH
86 #endif
87
88
89 #include "direction.hh"
90 #include "flower-proto.hh"
91
92 #ifndef SCM_PACK
93 #define SCM_PACK(x) ((SCM) x)
94
95 #endif
96 #ifndef SCM_UNPACK
97 #define SCM_UNPACK(x) ( x)
98 #endif
99
100 /*
101   conversion functions follow the GUILE naming convention, i.e.
102
103     A ly_B2A (B b);
104  */
105
106 SCM ly_last (SCM list);
107 SCM ly_str02scm (char const*c);
108 SCM ly_write2scm (SCM s);
109 SCM ly_deep_copy (SCM);
110 SCM ly_truncate_list (int k, SCM l );
111
112
113 #if (__GNUC__ > 2)
114 /*
115   todo: should add check for x86 as well
116  */
117 #define CACHE_SYMBOLS
118 #endif
119
120
121 /*
122   We don't use gh_symbol2scm directly, since it has const-correctness
123   problems in GUILE 1.3.4
124   
125  */
126 SCM my_gh_symbol2scm (const char* x);
127
128 #ifdef CACHE_SYMBOLS
129
130
131 /*
132   Using this trick we cache the value of gh_symbol2scm ("fooo") where
133   "fooo" is a constant string. This is done at the cost of one static
134   variable per ly_symbol2scm() use, and one boolean evaluation for
135   every call.
136
137   The overall speedup of lily is about 5% on a run of wtk1-fugue2
138
139 */
140 #define ly_symbol2scm(x) ({ static SCM cached;  \
141  SCM value = cached;  /* We store this one locally, since G++ -O2 fucks up else */   \
142  if ( __builtin_constant_p ((x)))\
143  {  if (!cached)\
144      value = cached =  scm_gc_protect_object (my_gh_symbol2scm((x)));\
145  } else\
146   value = gh_symbol2scm ((char*) (x)); \
147   value; })
148 #else
149 inline SCM ly_symbol2scm(char const* x) { return my_gh_symbol2scm((x)); }
150 #endif 
151
152
153
154 String ly_scm2string (SCM s);
155 String ly_symbol2string (SCM);
156 SCM ly_offset2scm (Offset);
157 Offset ly_scm2offset (SCM);
158 SCM ly_assoc_chain (SCM key, SCM achain);
159 SCM ly_assoc_cdr (SCM key, SCM alist);
160 Interval ly_scm2interval (SCM);
161 SCM ly_interval2scm (Drul_array<Real>);
162
163 void taint (SCM *);
164
165
166 SCM ly_parse_scm (char const* s, int* n);
167 SCM ly_quote_scm (SCM s);
168 SCM ly_type (SCM);
169 bool type_check_assignment (SCM val, SCM sym,  SCM type_symbol) ;
170 String print_scm_val (SCM val);
171 SCM ly_number2string (SCM s);
172
173 SCM parse_symbol_list (char const *);
174
175 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
176 inline SCM ly_car (SCM x) { return SCM_CAR (x); } 
177 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
178 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
179 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
180 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
181 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
182 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
183 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
184 /* inserts at front, removing dublicates */
185 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
186 {
187   return scm_acons(key, val, scm_assoc_remove_x (alist, key));
188 }
189 #ifdef PARANOID
190 #define gh_pair_p ly_pair_p
191 bool ly_pair_p (SCM x);
192 #else
193 inline bool ly_pair_p (SCM x) { return SCM_NFALSEP (scm_pair_p (x)); }
194 #endif
195 inline bool ly_symbol_p (SCM x) { return SCM_SYMBOLP (x); }
196 inline bool ly_number_p (SCM x) { return SCM_NUMBERP (x); }
197 inline bool ly_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p(x)); }
198
199 /*
200   display and print newline.
201  */
202 extern "C" { 
203 void ly_display_scm (SCM s);
204 }
205
206 #include "array.hh"
207
208 void read_lily_scm_file (String);
209 void init_lily_guile (String dir);
210
211 bool ly_dir_p (SCM s);
212 bool ly_number_pair_p (SCM);
213 bool ly_axis_p (SCM);
214
215 /*
216   these conversion functions also do a typecheck on the argument, and
217   return a default value if S has the wrong type.
218 */
219
220 Direction to_dir (SCM s);
221 bool to_boolean (SCM s);
222
223 void init_ly_protection ();
224 unsigned int ly_scm_hash (SCM s);
225
226 SCM index_cell (SCM cellp, Direction d);
227 SCM index_set_cell (SCM cellp, Direction d, SCM val);
228
229
230
231 SCM ly_snoc (SCM s, SCM list);
232 SCM ly_split_list (SCM s, SCM list);
233 SCM ly_unique (SCM list);
234 /*
235   snarfing.
236  */
237 void add_scm_init_func (void (*) ());
238
239
240 typedef SCM (*Scheme_function_unknown) ();
241
242 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
243 typedef SCM (*Scheme_function_0) ();
244 typedef SCM (*Scheme_function_1) (SCM);
245 typedef SCM (*Scheme_function_2) (SCM,SCM);      
246 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);         
247 #else
248 typedef SCM (*Scheme_function_0) (...);
249 typedef SCM (*Scheme_function_1) (...);
250 typedef SCM (*Scheme_function_2) (...);
251 typedef SCM (*Scheme_function_3) (...);
252 #endif
253
254 #define DECLARE_SCHEME_CALLBACK(NAME,ARGS) \
255         static SCM NAME ARGS; \
256         static SCM NAME ## _proc
257
258 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
259 SCM TYPE :: FUNC ## _proc;\
260 void                                                            \
261 TYPE ## _ ## FUNC ## _init_functions ()                                 \
262 {                                                               \
263   TYPE :: FUNC ## _proc = gh_new_procedure ## ARGCOUNT  ## _0 (#TYPE "::" #FUNC, \
264  ((Scheme_function_ ## ARGCOUNT)TYPE :: FUNC));                                 \
265 }                                                               \
266                                                                 \
267 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, TYPE ## _ ## FUNC ## _init_functions);       \
268
269
270 void ly_add_function_documentation (char const * fname,
271                                     char const * varlist,
272                                     char const * doc);
273
274 #define ADD_SCM_INIT_FUNC(name, func)\
275 class name ## _scm_initter {                    \
276 public:\
277   name ## _scm_initter ()                       \
278   {                                             \
279     add_scm_init_func (func);           \
280   }                                             \
281 } _ ## name ## _scm_initter;                    \
282 /* end define */
283
284 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
285 SCM FNAME ARGLIST ; \
286 SCM FNAME ## _proc;\
287 void \
288 FNAME ## init ()\
289 {\
290  FNAME ## _proc \
291     = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, (Scheme_function_unknown) FNAME);\
292   ly_add_function_documentation (PRIMNAME, #ARGLIST,  DOCSTRING);\
293 }\
294 ADD_SCM_INIT_FUNC (FNAME ## init_unique_prefix, FNAME ## init);\
295 SCM \
296 FNAME ARGLIST\
297
298
299
300
301
302 #endif // LILY_GUILE_HH