]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile-macros.hh
a273f5ac5d3da229e0cd49122edba438cddfabc5
[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--2012 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 {
56   assert (scm_is_symbol (s));
57   return s;
58 }
59
60 /* Using this trick we cache the value of scm_from_locale_symbol ("fooo") where
61    "fooo" is a constant string. This is done at the cost of one static
62    variable per ly_symbol2scm() use, and one boolean evaluation for
63    every call.
64  */
65 #define ly_symbol2scm(x)                                                \
66   ({                                                                    \
67     static SCM cached;                                                  \
68     /* We store this one locally, since G++ -O2 fucks up else */        \
69     SCM value = cached;                                                 \
70     if (__builtin_constant_p ((x)))                                     \
71       {                                                                 \
72         if (!cached)                                                    \
73           value = cached = scm_gc_protect_object (scm_or_str2symbol (x)); \
74       }                                                                 \
75     else                                                                \
76       value = scm_or_str2symbol (x);                                    \
77     value;                                                              \
78   })
79 #else
80 inline SCM ly_symbol2scm (char const *x) { return scm_from_locale_symbol ((x)); }
81 #endif
82
83 /*
84   TODO: rename me to ly_c_lily_module_eval
85
86   we don't have to protect the result; it's already part of the
87   exports list of the module.
88 */
89
90 #define ly_lily_module_constant(x)                                      \
91   ({                                                                    \
92     static SCM cached;                                                  \
93     /* We store this one locally, since G++ -O2 fucks up else */        \
94     SCM value = cached;                                                 \
95     if (__builtin_constant_p ((x)))                                     \
96       {                                                                 \
97         if (!cached)                                                    \
98           value = cached = scm_eval (scm_from_locale_symbol (x),                \
99                                     global_lily_module);                \
100       }                                                                 \
101     else                                                                \
102       value = scm_eval (scm_from_locale_symbol (x), global_lily_module);        \
103     value;                                                              \
104   })
105
106 /*
107   Adds the NAME as a Scheme function, and a variable to store the SCM
108   version of the function in the static variable NAME_proc
109 */
110 #define DECLARE_SCHEME_CALLBACK(NAME, ARGS)     \
111   static SCM NAME ARGS;                         \
112   static SCM NAME ## _proc
113 #define ADD_TYPE_PREDICATE(func, type_name) \
114   void \
115   func ## _type_adder ()                        \
116   {\
117     ly_add_type_predicate ((Type_predicate_ptr)func, type_name);        \
118   }\
119   ADD_SCM_INIT_FUNC(func ## _type_adder_ctor, \
120                     func ## _type_adder);
121 #define ADD_TYPE_PREDICATE(func, type_name) \
122   void \
123   func ## _type_adder ()                        \
124   {\
125     ly_add_type_predicate ((Type_predicate_ptr)func, type_name);        \
126   }\
127   ADD_SCM_INIT_FUNC(func ## _type_adder_ctor, \
128                     func ## _type_adder);
129
130 string mangle_cxx_identifier (string);
131
132 void ly_add_type_predicate (void *ptr, const string &name);
133 string predicate_to_typename (void *ptr);
134
135 /*
136   Make TYPE::FUNC available as a Scheme function.
137 */
138 #define MAKE_SCHEME_CALLBACK_WITH_OPTARGS(TYPE, FUNC, ARGCOUNT, OPTIONAL_COUNT, DOC) \
139   SCM TYPE ::FUNC ## _proc;                                             \
140   void                                                                  \
141   TYPE ## _ ## FUNC ## _init_functions ()                               \
142   {                                                                     \
143     string cxx = string (#TYPE) + "::" + string (#FUNC); \
144     string id = mangle_cxx_identifier (cxx); \
145     TYPE ::FUNC ## _proc = scm_c_define_gsubr (id.c_str(),                      \
146                                                (ARGCOUNT-OPTIONAL_COUNT), OPTIONAL_COUNT, 0,    \
147                                                (scm_t_subr) TYPE::FUNC); \
148     ly_add_function_documentation (TYPE :: FUNC ## _proc, id.c_str(), "", \
149                                    DOC);                                \
150     scm_c_export (id.c_str (), NULL);                                   \
151   }                                                                     \
152                                                                         \
153   ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback,                    \
154                      TYPE ## _ ## FUNC ## _init_functions);
155
156 #define MAKE_DOCUMENTED_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT, DOC)              \
157   MAKE_SCHEME_CALLBACK_WITH_OPTARGS(TYPE, FUNC, ARGCOUNT, 0, DOC);
158
159 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT)                      \
160   MAKE_SCHEME_CALLBACK_WITH_OPTARGS(TYPE,FUNC,ARGCOUNT, 0, "");
161
162 void ly_add_function_documentation (SCM proc, const string &fname, const string &varlist, const string &doc);
163 void ly_check_name (const string &cxx, const string &fname);
164
165 #define ADD_SCM_INIT_FUNC(name, func)           \
166   class name ## _scm_initter                    \
167   {                                             \
168   public:                                       \
169     name ## _scm_initter ()                     \
170     {                                           \
171       add_scm_init_func (func);                 \
172     }                                           \
173   }                                             \
174     _ ## name ## _scm_initter;
175
176 /* end define */
177
178 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, \
179                                ARGLIST, DOCSTRING)                      \
180   SCM FNAME ## _proc;                                                   \
181   void                                                                  \
182   INITPREFIX ## init ()                                                 \
183   {                                                                     \
184     FNAME ## _proc = scm_c_define_gsubr (PRIMNAME, REQ, OPT, VAR,       \
185                                          (scm_t_subr) FNAME); \
186     ly_check_name (#FNAME, PRIMNAME);\
187     ly_add_function_documentation (FNAME ## _proc, PRIMNAME, #ARGLIST,  \
188                                    DOCSTRING);                          \
189     scm_c_export (PRIMNAME, NULL);                                      \
190   }                                                                     \
191   ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init); \
192   SCM                                                                   \
193   FNAME ARGLIST
194
195 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)   \
196   SCM FNAME ARGLIST;                                                    \
197   LY_DEFINE_WITHOUT_DECL (FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, \
198                           DOCSTRING)
199
200 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, \
201                                   ARGLIST, DOCSTRING)                   \
202   SCM FNAME ARGLIST;                                                    \
203   LY_DEFINE_WITHOUT_DECL (CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, \
204                           VAR, ARGLIST, DOCSTRING)
205
206 #define get_property(x) internal_get_property (ly_symbol2scm (x))
207 #define get_pure_property(x,y,z) \
208   internal_get_pure_property (ly_symbol2scm (x), y, z)
209 #define get_maybe_pure_property(w,x,y,z) \
210   internal_get_maybe_pure_property (ly_symbol2scm (w), x, y, z)
211 #define get_property_data(x) internal_get_property_data (ly_symbol2scm (x))
212 #define get_object(x) internal_get_object (ly_symbol2scm (x))
213 #define set_object(x, y) internal_set_object (ly_symbol2scm (x), y)
214 #define del_property(x) internal_del_property (ly_symbol2scm (x))
215
216 #ifndef NDEBUG
217 /*
218   TODO: include modification callback support here, perhaps
219   through intermediate Grob::instrumented_set_property( .. __LINE__ ).
220  */
221 #define set_property(x, y) instrumented_set_property (ly_symbol2scm (x), y, __FILE__, __LINE__, __FUNCTION__)
222 #else
223 #define set_property(x, y) internal_set_property (ly_symbol2scm (x), y)
224 #endif
225
226 #define LY_ASSERT_TYPE(pred, var, number)                                       \
227   {                                                                     \
228     if (!pred (var)) \
229       {                                                                 \
230         scm_wrong_type_arg_msg(mangle_cxx_identifier (__FUNCTION__).c_str(), \
231                                number, var, \
232                                predicate_to_typename ((void*) &pred).c_str()); \
233       }                                                                 \
234   }
235
236 #define LY_ASSERT_SMOB(klass, var, number) LY_ASSERT_TYPE(klass::unsmob, var, number)
237
238 #endif /* LILY_GUILE_MACROS_HH */