guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Now that SCM type is a union...


From: Ken Raeburn
Subject: Re: Now that SCM type is a union...
Date: Sat, 13 Aug 2011 01:26:03 -0400

On Aug 12, 2011, at 08:44, address@hidden wrote:
> Between stable-2.0 and master a patch changed the C representation
> of the SCM type so that it is now a union.
> 
> This code :
> 
> static SCM foo = SCM_UNSPECIFIED;
> 
> now expands to something similar to :
> 
> static SCM foo = (SCM) { ... };
> 
> This form (casting a struct or union initializer while initializing a
> global identifier) is for some reason invalid when gcc is called with
> "-std=c99" (I was about to say : "was invalid in c99", but who really
> knowns?) nor "-std=gnu99" (although it works when std is set to c89 or
> gnu89).

("Is for some reason invalid" is not very descriptive; please include specific 
compiler messages.)

That syntax -- the parenthesized type followed by a list of initializers in 
braces -- is called a "compound literal" (technically not a cast expression) 
and was added in C99.  The value of a compound literal is an anonymous object 
with a value given by the initializer elements.  (It's an lvalue, and can even 
have its address taken.)  That means it's not a "constant expression" and 
cannot be used for static initialization, under C99 rules.

The GCC documentation says that a GCC extension permits such initialization.  
From that I'd kind of expect it to work in gnu89 or gnu99 modes, and maybe not 
c89, but apparently that's not how they did it...  It wouldn't surprise me if 
it has to do with how either compound literals or constant expressions are 
handled in the compiler making it hard to support them as both addressable 
anonymous objects and constant initializers, and for C99 conformance they'd 
have to go with the former.  I guess the GCC documentation needs fixing.

> I tried to get rid of the cast to (SCM) in tags.h but the compilation
> then fails since some code relies on the cast to SCM.

Yes, to be used as a value in an expression, rather than an initializer for a 
variable, it needs to use the compound-literal syntax with the parenthesized 
type name.

> So, lets suppose I have an app written in c99 that I want to extend with
> guile, how could I compile it ?

Have an initialization function which stuffs SCM_UNSPECIFIED into a bunch of 
uninitialized SCM variables before making them accessible from other code?  
Yeah, it's kind of unfortunate.  Maybe we need a separate set of macros for 
static initialization using SCM_UNDEFINED, SCM_BOOL_F, and friends?

It looks like Guile is compiled in the default (gnu89?) mode, not C99.  It has 
a few places where static variables are initialized using SCM_BOOL_F, which 
will have the same problem.

Ken


reply via email to

[Prev in Thread] Current Thread [Next in Thread]