Movatterモバイル変換


[0]ホーム

URL:


Next: ,Previous: Function Attributes,Up: C Extensions


5.25 Attribute Syntax

This section describes the syntax with which__attribute__ may beused, and the constructs to which attribute specifiers bind, for the Clanguage. Some details may vary for C++ and Objective-C. Because ofinfelicities in the grammar for attributes, some forms described heremay not be successfully parsed in all cases.

There are some problems with the semantics of attributes in C++. Forexample, there are no manglings for attributes, although they may affectcode generation, so problems may arise when attributed types are used inconjunction with templates or overloading. Similarly,typeiddoes not distinguish between types with different attributes. Supportfor attributes in C++ may be restricted in future to attributes ondeclarations only, but not on nested declarators.

SeeFunction Attributes, for details of the semantics of attributesapplying to functions. SeeVariable Attributes, for details of thesemantics of attributes applying to variables. SeeType Attributes,for details of the semantics of attributes applying to structure, unionand enumerated types.

Anattribute specifier is of the form__attribute__ ((attribute-list)). Anattribute listis a possibly empty comma-separated sequence ofattributes, whereeach attribute is one of the following:

Anattribute specifier list is a sequence of one or more attributespecifiers, not separated by any other tokens.

In GNU C, an attribute specifier list may appear after the colon following alabel, other than acase ordefault label. The onlyattribute it makes sense to use after a label isunused. Thisfeature is intended for code generated by programs which contains labelsthat may be unused but which is compiled with-Wall. It wouldnot normally be appropriate to use in it human-written code, though itcould be useful in cases where the code that jumps to the label iscontained within an#ifdef conditional. GNU C++ does not permitsuch placement of attribute lists, as it is permissible for adeclaration, which could begin with an attribute list, to be labelled inC++. Declarations cannot be labelled in C90 or C99, so the ambiguitydoes not arise there.

An attribute specifier list may appear as part of astruct,union orenum specifier. It may go either immediatelyafter thestruct,union orenum keyword, or afterthe closing brace. It is ignored if the content of the structure, unionor enumerated type is not defined in the specifier in which theattribute specifier list is used—that is, in usages such asstruct __attribute__((foo)) bar with no following opening brace. Where attribute specifiers follow the closing brace, they are consideredto relate to the structure, union or enumerated type defined, not to anyenclosing declaration the type specifier appears in, and the typedefined is not complete until after the attribute specifiers.

Otherwise, an attribute specifier appears as part of a declaration,counting declarations of unnamed parameters and type names, and relatesto that declaration (which may be nested in another declaration, forexample in the case of a parameter declaration), or to a particular declaratorwithin a declaration. Where anattribute specifier is applied to a parameter declared as a function oran array, it should apply to the function or array rather than thepointer to which the parameter is implicitly converted, but this is notyet correctly implemented.

Any list of specifiers and qualifiers at the start of a declaration maycontain attribute specifiers, whether or not such a list may in thatcontext contain storage class specifiers. (Some attributes, however,are essentially in the nature of storage class specifiers, and only makesense where storage class specifiers may be used; for example,section.) There is one necessary limitation to this syntax: thefirst old-style parameter declaration in a function definition cannotbegin with an attribute specifier, because such an attribute applies tothe function instead by syntax described below (which, however, is notyet implemented in this case). In some other cases, attributespecifiers are permitted by this grammar but not yet supported by thecompiler. All attribute specifiers in this place relate to thedeclaration as a whole. In the obsolescent usage where a type ofint is implied by the absence of type specifiers, such a list ofspecifiers and qualifiers may be an attribute specifier list with noother specifiers or qualifiers.

At present, the first parameter in a function prototype must have sometype specifier which is not an attribute specifier; this resolves anambiguity in the interpretation ofvoid f(int(__attribute__((foo)) x)), but is subject to change. At present, ifthe parentheses of a function declarator contain only attributes thenthose attributes are ignored, rather than yielding an error or warningor implying a single parameter of type int, but this is subject tochange.

An attribute specifier list may appear immediately before a declarator(other than the first) in a comma-separated list of declarators in adeclaration of more than one identifier using a single list ofspecifiers and qualifiers. Such attribute specifiers applyonly to the identifier before whose declarator they appear. Forexample, in

     __attribute__((noreturn)) void d0 (void),         __attribute__((format(printf, 1, 2))) d1 (const char *, ...),          d2 (void)

thenoreturn attribute applies to all the functionsdeclared; theformat attribute only applies tod1.

An attribute specifier list may appear immediately before the comma,= or semicolon terminating the declaration of an identifier otherthan a function definition. At present, such attribute specifiers applyto the declared object or function, but in future they may attach to theoutermost adjacent declarator. In simple cases there is no difference,but, for example, in

     void (****f)(void) __attribute__((noreturn));

at present thenoreturn attribute applies tof, whichcauses a warning sincef is not a function, but in future it mayapply to the function****f. The precise semantics of whatattributes in such cases will apply to are not yet specified. Where anassembler name for an object or function is specified (seeAsm Labels), at present the attribute must follow theasmspecification; in future, attributes before theasm specificationmay apply to the adjacent declarator, and those after it to the declaredobject or function.

An attribute specifier list may, in future, be permitted to appear afterthe declarator in a function definition (before any old-style parameterdeclarations or the function body).

Attribute specifiers may be mixed with type qualifiers appearing insidethe[] of a parameter array declarator, in the C99 construct bywhich such qualifiers are applied to the pointer to which the array isimplicitly converted. Such attribute specifiers apply to the pointer,not to the array, but at present this is not implemented and they areignored.

An attribute specifier list may appear at the start of a nesteddeclarator. At present, there are some limitations in this usage: theattributes correctly apply to the declarator, but for most individualattributes the semantics this implies are not implemented. When attribute specifiers follow the* of a pointerdeclarator, they may be mixed with any type qualifiers present. The following describes the formal semantics of this syntax. It will make themost sense if you are familiar with the formal specification ofdeclarators in the ISO C standard.

Consider (as in C99 subclause 6.7.5 paragraph 4) a declarationTD1, whereT contains declaration specifiers that specify a typeType (such asint) andD1 is a declarator thatcontains an identifierident. The type specified foridentfor derived declarators whose type does not include an attributespecifier is as in the ISO C standard.

IfD1 has the form(attribute-specifier-list D ),and the declarationT D specifies the type“derived-declarator-type-listType” forident, thenT D1 specifies the type “derived-declarator-type-listattribute-specifier-listType” forident.

IfD1 has the form*type-qualifier-and-attribute-specifier-list D, and thedeclarationT D specifies the type“derived-declarator-type-listType” forident, thenT D1 specifies the type “derived-declarator-type-listtype-qualifier-and-attribute-specifier-listType” forident.

For example,

     void (__attribute__((noreturn)) ****f) (void);

specifies the type “pointer to pointer to pointer to pointer tonon-returning function returningvoid”. As another example,

     char *__attribute__((aligned(8))) *f;

specifies the type “pointer to 8-byte-aligned pointer tochar”. Note again that this does not work with most attributes; for example,the usage of `aligned' and `noreturn' attributes given aboveis not yet supported.

For compatibility with existing code written for compiler versions thatdid not implement attributes on nested declarators, some laxity isallowed in the placing of attributes. If an attribute that only appliesto types is applied to a declaration, it will be treated as applying tothe type of that declaration. If an attribute that only applies todeclarations is applied to the type of a declaration, it will be treatedas applying to that declaration; and, for compatibility with codeplacing the attributes immediately before the identifier declared, suchan attribute applied to a function return type will be treated asapplying to the function type, and such an attribute applied to an arrayelement type will be treated as applying to the array type. If anattribute that only applies to function types is applied to apointer-to-function type, it will be treated as applying to the pointertarget type; if such an attribute is applied to a function return typethat is not a pointer-to-function type, it will be treated as applyingto the function type.


[8]ページ先頭

©2009-2025 Movatter.jp