This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Casting to scm_bits_t
- To: Guile Mailing List <guile at sourceware dot cygnus dot com>
- Subject: Casting to scm_bits_t
- From: Dirk Herrmann <dirk at ida dot ing dot tu-bs dot de>
- Date: Fri, 31 Mar 2000 15:06:02 +0200 (MEST)
Hello!
I'd like to know, where in macros should we automatically cast to
scm_bits_t and where not?
Example: in SCM_SETCHARS a char* is stored as the 2nd cell element. The
definition currently looks as follows:
#define SCM_SETCHARS(x, v) (SCM_SET_CELL_WORD_1 ((x), (scm_bits_t) (v)))
Thus, any C value that can be cast to scm_bits_t will automatically be
accepted, whether it is a char* or not. While this is not really type
safe, it seems to be cleaner than if every caller of SCM_SETCHARS would
have to perform the cast of the char* value to a scm_bits_t value for
himself.
In contrast, in smob.h the data parameters are not cast automatically,
thus the user has to perform that cast at every call to SCM_NEWSMOB. But
I'm not sure if this is really desirable?
I'd suggest, to define SCM_PACK as follows (for strict typing, similarly
for the other compilation modes):
typedef union { struct { scm_bits_t n; } n; } SCM;
static SCM scm_pack(scm_bits_t b) { SCM s; s.n.n = b; return s; }
#define SCM_UNPACK(x) ((x).n.n)
#define SCM_PACK(x) (scm_pack ((scm_bits_t) (x)))
Reasons:
* It's still not possible to confuse SCM and scm_bits_t
* No need to add lots of (scm_bits_t) casts to the code.
* Type safety can still be achieved. How? OK, example:
#if defined (SCM_STRICT_TYPING)
static char * scm_i_char_ptr(char * x) { return x; }
#else
#define scm_i_char_ptr(x) (x)
#endif
#define SCM_SETCHARS(x, v) (SCM_SET_CELL_WORD_1 ((x), scm_i_char_ptr (v)))
Then, we should get a nice compiler warning that tells us about any
misuses of the macro SCM_SETCHARS. In contrast, if we require the user to
perform all of the casts in the code, like:
void * x;
SCM_SETCHARS ((scm_bits_t) x)
there is no chance any more for a central testing
place. Consequence: Casts in user code are bad, because any
casting mistake in there can't be detected by clever macro tricks.
Best regards
Dirk Herrmann