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