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