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