guile: Add as_a_scm_t_subr
[deliverable/binutils-gdb.git] / gdb / guile / guile-internal.h
index 509120ba36206b81fa525569bdcf62979b3e16be..017309af7532cda2c5eaed9609c9740a972f24a3 100644 (file)
@@ -50,6 +50,50 @@ typedef struct
 
 #define END_VARIABLES { NULL, SCM_BOOL_F, NULL }
 
+#ifdef __cplusplus
+
+/* Although scm_t_subr is meant to hold a function pointer, at least
+   in some versions of guile, it is actually a typedef to "void *".
+   That means that in C++, an explicit cast is necessary to convert
+   function pointer to scm_t_subr.  But a cast also makes it possible
+   to pass function pointers with the wrong type by mistake.  So
+   instead of adding such casts throughout, we use 'as_a_scm_t_subr'
+   to do the conversion, which (only) has overloads for function
+   pointer types that are valid.
+
+   See https://lists.gnu.org/archive/html/guile-devel/2013-03/msg00001.html.
+*/
+
+static inline scm_t_subr
+as_a_scm_t_subr (SCM (*func) (void))
+{
+  return (scm_t_subr) func;
+}
+
+static inline scm_t_subr
+as_a_scm_t_subr (SCM (*func) (SCM))
+{
+  return (scm_t_subr) func;
+}
+
+static inline scm_t_subr
+as_a_scm_t_subr (SCM (*func) (SCM, SCM))
+{
+  return (scm_t_subr) func;
+}
+
+static inline scm_t_subr
+as_a_scm_t_subr (SCM (*func) (SCM, SCM, SCM))
+{
+  return (scm_t_subr) func;
+}
+
+#else
+
+/* In C, just do an implicit conversion.  */
+#define as_a_scm_t_subr(func) func
+
+#endif
 /* Scheme functions to define during initialization.  */
 
 typedef struct
This page took 0.023356 seconds and 4 git commands to generate.