]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile-macros.hh
Guile compat: support scm_t_subr, a new typedef in v2.0.0
[lilypond.git] / lily / include / lily-guile-macros.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef LILY_GUILE_MACROS_HH
21 #define LILY_GUILE_MACROS_HH
22
23 #include "config.hh"
24
25 #ifndef SMOB_FREE_RETURN_VAL
26 #define SMOB_FREE_RETURN_VAL(CL) 0
27 #endif
28
29 #ifndef SCM_PACK
30 #define SCM_PACK(x) ((SCM) x)
31 #endif
32
33 #ifndef SCM_UNPACK
34 #define SCM_UNPACK(x) (x)
35 #endif
36
37 /* For backward compatability with Guile 1.8 */
38 #if !HAVE_GUILE_SUBR_TYPE
39 typedef SCM (*scm_t_subr) (GUILE_ELLIPSIS);
40 #endif
41
42 /* Unreliable with gcc-2.x
43    FIXME: should add check for x86 as well?  */
44 #define CACHE_SYMBOLS
45
46 #ifdef CACHE_SYMBOLS
47
48 /* this lets us "overload" macros such as get_property to take
49    symbols as well as strings */
50 inline SCM
51 scm_or_str2symbol (char const *c) { return scm_from_locale_symbol (c); }
52
53 inline SCM
54 scm_or_str2symbol (SCM s) {
55   assert (scm_is_symbol (s));
56   return s;
57 }
58
59 /* Using this trick we cache the value of scm_from_locale_symbol ("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 #define ly_symbol2scm(x)                                                \
65   ({                                                                    \
66     static SCM cached;                                                  \
67     /* We store this one locally, since G++ -O2 fucks up else */        \
68     SCM value = cached;                                                 \
69     if (__builtin_constant_p ((x)))                                     \
70       {                                                                 \
71         if (!cached)                                                    \
72           value = cached = scm_gc_protect_object (scm_or_str2symbol (x)); \
73       }                                                                 \
74     else                                                                \
75       value = scm_or_str2symbol (x);                                    \
76     value;                                                              \
77   })
78 #else
79 inline SCM ly_symbol2scm (char const *x) { return scm_from_locale_symbol ((x)); }
80 #endif
81
82 /*
83   TODO: rename me to ly_c_lily_module_eval
84
85   we don't have to protect the result; it's already part of the
86   exports list of the module.
87 */
88
89 #define ly_lily_module_constant(x)                                      \
90   ({                                                                    \
91     static SCM cached;                                                  \
92     /* We store this one locally, since G++ -O2 fucks up else */        \
93     SCM value = cached;                                                 \
94     if (__builtin_constant_p ((x)))                                     \
95       {                                                                 \
96         if (!cached)                                                    \
97           value = cached = scm_eval (scm_from_locale_symbol (x),                \
98                                     global_lily_module);                \
99       }                                                                 \
100     else                                                                \
101       value = scm_eval (scm_from_locale_symbol (x), global_lily_module);        \
102     value;                                                              \
103   })
104
105 /*
106   Adds the NAME as a Scheme function, and a variable to store the SCM
107   version of the function in the static variable NAME_proc
108 */
109 #define DECLARE_SCHEME_CALLBACK(NAME, ARGS)     \
110   static SCM NAME ARGS;                         \
111   static SCM NAME ## _proc
112 #define ADD_TYPE_PREDICATE(func, type_name) \
113   void \
114   func ## _type_adder ()                        \
115   {\
116     ly_add_type_predicate ((Type_predicate_ptr)func, type_name);        \
117   }\
118   ADD_SCM_INIT_FUNC(func ## _type_adder_ctor, \
119                     func ## _type_adder);
120 #define ADD_TYPE_PREDICATE(func, type_name) \
121   void \
122   func ## _type_adder ()                        \
123   {\
124     ly_add_type_predicate ((Type_predicate_ptr)func, type_name);        \
125   }\
126   ADD_SCM_INIT_FUNC(func ## _type_adder_ctor, \
127                     func ## _type_adder);
128
129 string mangle_cxx_identifier (string);
130
131 void ly_add_type_predicate (void *ptr, string name);
132 string predicate_to_typename (void *ptr);
133
134 /*
135   Make TYPE::FUNC available as a Scheme function.
136 */
137 #define MAKE_SCHEME_CALLBACK_WITH_OPTARGS(TYPE, FUNC, ARGCOUNT, OPTIONAL_COUNT, DOC) \
138   SCM TYPE ::FUNC ## _proc;                                             \
139   void                                                                  \
140   TYPE ## _ ## FUNC ## _init_functions ()                               \
141   {                                                                     \
142     string cxx = string (#TYPE) + "::" + string (#FUNC); \
143     string id = mangle_cxx_identifier (cxx); \
144     TYPE ::FUNC ## _proc = scm_c_define_gsubr (id.c_str(),                      \
145                                                (ARGCOUNT-OPTIONAL_COUNT), OPTIONAL_COUNT, 0,    \
146                                                (scm_t_subr) TYPE::FUNC); \
147     ly_add_function_documentation (TYPE :: FUNC ## _proc, id.c_str(), "", \
148                                    DOC);                                \
149     scm_c_export (id.c_str (), NULL);                                   \
150   }                                                                     \
151                                                                         \
152   ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback,                    \
153                      TYPE ## _ ## FUNC ## _init_functions);
154
155 #define MAKE_DOCUMENTED_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT, DOC)              \
156   MAKE_SCHEME_CALLBACK_WITH_OPTARGS(TYPE, FUNC, ARGCOUNT, 0, DOC);
157
158 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT)                      \
159   MAKE_SCHEME_CALLBACK_WITH_OPTARGS(TYPE,FUNC,ARGCOUNT, 0, "");
160
161 void ly_add_function_documentation (SCM proc, string fname, string varlist, string doc);
162 void ly_check_name (string cxx, string fname);
163
164 #define ADD_SCM_INIT_FUNC(name, func)           \
165   class name ## _scm_initter                    \
166   {                                             \
167   public:                                       \
168     name ## _scm_initter ()                     \
169     {                                           \
170       add_scm_init_func (func);                 \
171     }                                           \
172   }                                             \
173     _ ## name ## _scm_initter;
174
175 /* end define */
176
177 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, \
178                                ARGLIST, DOCSTRING)                      \
179   SCM FNAME ## _proc;                                                   \
180   void                                                                  \
181   INITPREFIX ## init ()                                                 \
182   {                                                                     \
183     FNAME ## _proc = scm_c_define_gsubr (PRIMNAME, REQ, OPT, VAR,       \
184                                          (scm_t_subr) FNAME); \
185     ly_check_name (#FNAME, PRIMNAME);\
186     ly_add_function_documentation (FNAME ## _proc, PRIMNAME, #ARGLIST,  \
187                                    DOCSTRING);                          \
188     scm_c_export (PRIMNAME, NULL);                                      \
189   }                                                                     \
190   ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init); \
191   SCM                                                                   \
192   FNAME ARGLIST
193
194 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)   \
195   SCM FNAME ARGLIST;                                                    \
196   LY_DEFINE_WITHOUT_DECL (FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, \
197                           DOCSTRING)
198
199 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, \
200                                   ARGLIST, DOCSTRING)                   \
201   SCM FNAME ARGLIST;                                                    \
202   LY_DEFINE_WITHOUT_DECL (CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, \
203                           VAR, ARGLIST, DOCSTRING)
204
205 #define get_property(x) internal_get_property (ly_symbol2scm (x))
206 #define get_pure_property(x,y,z) \
207   internal_get_pure_property (ly_symbol2scm (x), y, z)
208 #define get_maybe_pure_property(w,x,y,z) \
209   internal_get_maybe_pure_property (ly_symbol2scm (w), x, y, z)
210 #define get_property_data(x) internal_get_property_data (ly_symbol2scm (x))
211 #define get_object(x) internal_get_object (ly_symbol2scm (x))
212 #define set_object(x, y) internal_set_object (ly_symbol2scm (x), y)
213 #define del_property(x) internal_del_property (ly_symbol2scm (x))
214
215 #ifndef NDEBUG
216 /*
217   TODO: include modification callback support here, perhaps
218   through intermediate Grob::instrumented_set_property( .. __LINE__ ).
219  */
220 #define set_property(x, y) instrumented_set_property (ly_symbol2scm (x), y, __FILE__, __LINE__, __FUNCTION__)
221 #else
222 #define set_property(x, y) internal_set_property (ly_symbol2scm (x), y)
223 #endif
224
225
226
227 #define LY_ASSERT_TYPE(pred, var, number)                                       \
228   {                                                                     \
229     if (!pred (var)) \
230       {                                                                 \
231         scm_wrong_type_arg_msg(mangle_cxx_identifier (__FUNCTION__).c_str(), \
232                                number, var, \
233                                predicate_to_typename ((void*) &pred).c_str()); \
234       }                                                                 \
235   }
236
237 #define LY_ASSERT_SMOB(klass, var, number) LY_ASSERT_TYPE(klass::unsmob, var, number)
238
239
240
241 #endif /* LILY_GUILE_MACROS_HH */