C vs. C++ parsers and the __attribute__ syntax

Joseph S. Myers jsm@polyomino.org.uk
Wed Oct 27 15:16:00 GMT 2004


On Wed, 27 Oct 2004, Ulrich Weigand wrote:

> The question is now how to express this syntactically.  Is there 
> a document that formally specifies the syntax of the attribute
> extension?  The info pages appear a bit vague ...

"Attribute Syntax" in the manual, but there may well be bugs in both the 
documentation and the code.

> typedef struct test * __attribute__ ((mode (SI))) test_ptr32;
> 
> would be the correct way to declare a SImode pointer type to
> 'struct test', if the placement of attribute is analogous to
> 'const' or 'volatile' -- it is 'test_ptr32' that is of mode
> SImode, not '*test_ptr32'.
> 
> And indeed, this does have the effect I intended with the C
> parser.  However, the C++ parser, while it accepts the above
> construct, appears to simply ignore the attribute.

This is the proper way.

> On the hand, *this* declaration:
> 
> typedef struct test __attribute__ ((mode (SI))) *test_ptr32;

In this case, the attribute is part of the declaration specifiers, and 
applies to the declaration as a whole.  This form (and similarly postfix 
attributes) is most cleanly used only for those attributes which act like 
storage class specifiers in applying to declarations but not types.  
However, many such forms are accepted for compatibility with existing code 
and adjusted to apply to the type, or in the case of vector_size 
attributes to some type nested within there.

> How would I declare a type like 'an SImode pointer to a
> TImode integer'?

typedef int (__attribute__((mode(TI))) (*__attribute__((mode(SI))) type));

-- 
Joseph S. Myers               http://www.srcf.ucam.org/~jsm28/gcc/
    jsm@polyomino.org.uk (personal mail)
    joseph@codesourcery.com (CodeSourcery mail)
    jsm28@gcc.gnu.org (Bugzilla assignments and CCs)



More information about the Gcc mailing list