Files
cppdraft_translate/cppdraft/dcl/attr/grammar.md
2025-10-25 03:02:53 +03:00

16 KiB
Raw Blame History

[dcl.attr.grammar]

9 Declarations [dcl]

9.13 Attributes [dcl.attr]

9.13.1 Attribute syntax and semantics [dcl.attr.grammar]

1

#

Attributes and annotations specify additional information for various source constructs such as types, variables, names, contract assertions, blocks, or translation units.

attribute-specifier-seq:
attribute-specifier attribute-specifier-seqopt

attribute-specifier:
[ [ attribute-using-prefixopt attribute-list ] ]
[ [ annotation-list ] ]
alignment-specifier

alignment-specifier:
alignas ( type-id ...opt )
alignas ( constant-expression ...opt )

attribute-using-prefix:
using attribute-namespace :

attribute-list:
attributeopt
attribute-list , attributeopt
attribute ...
attribute-list , attribute ...

annotation-list:
annotation ...opt
annotation-list , annotation ...opt

attribute:
attribute-token attribute-argument-clauseopt

annotation:
= constant-expression

attribute-token:
identifier
attribute-scoped-token

attribute-scoped-token:
attribute-namespace :: identifier

attribute-namespace:
identifier

attribute-argument-clause:
( balanced-token-seqopt )

balanced-token-seq:
balanced-token balanced-token-seqopt

balanced-token:
( balanced-token-seqopt )
[ balanced-token-seqopt ]
{ balanced-token-seqopt }
[: balanced-token-seqopt :]
any token other than (, ), [, ], {, }, [:, or :]

2

#

If an attribute-specifier contains an attribute-using-prefix, the attribute-list following that attribute-using-prefix shall not contain an attribute-scoped-token and every attribute-token in that attribute-list is treated as if its identifier were prefixed with N::, where N is the attribute-namespace specified in the attribute-using-prefix.

[Note 1:

This rule imposes no constraints on how an attribute-using-prefix affects the tokens in an attribute-argument-clause.

— end note]

[Example 1: using CC: opt(1), debug // same as CC::opt(1), CC::debugvoid f() {}using CC: opt(1) CC::debug // same as CC::opt(1) CC::debugvoid g() {}using CC: CC::opt(1) // error: cannot combine using and scoped attribute tokenvoid h() {} — end example]

3

#

[Note 2:

For each individual attribute, the form of thebalanced-token-seq will be specified.

— end note]

4

#

In an attribute-list, an ellipsis may appear only if thatattribute's specification permits it.

An attribute followed by an ellipsis is a pack expansion.

An attribute-specifier that contains an attribute-list with no attributes and no alignment-specifier has no effect.

The order in which the attribute-tokens appear in anattribute-list is not significant.

If akeyword or an alternative token that satisfies the syntactic requirements of an identifier ([lex.name]) is contained in an attribute-token, it is considered an identifier.

Noname lookup is performed on any of the identifiers contained in anattribute-token.

The attribute-token determines additional requirements on the attribute-argument-clause (if any).

5

#

An annotation followed by an ellipsis is a pack expansion ([temp.variadic]).

6

#

Each attribute-specifier-seq is said to appertain to some entity or statement, identified by the syntactic context where it appears ([stmt], [dcl], [dcl.decl]).

If an attribute-specifier-seq that appertains to some entity or statement contains an attribute or alignment-specifier that is not allowed to apply to that entity or statement, the program is ill-formed.

If an attribute-specifier-seq appertains to a friend declaration ([class.friend]), that declaration shall be a definition.

[Note 3:

An attribute-specifier-seq cannot appertain to an explicit instantiation ([temp.explicit]).

— end note]

7

#

For an attribute-token (including an attribute-scoped-token) not specified in this document, the behavior is implementation-defined; any such attribute-token that is not recognized by the implementation is ignored.

[Note 4:

A program is ill-formed if it contains an attribute specified in [dcl.attr] that violates the rules specifying to which entity or statement the attribute can apply or the syntax rules for the attribute's attribute-argument-clause, if any.

— end note]

[Note 5:

The attributes specified in [dcl.attr] have optional semantics: given a well-formed program, removing all instances of any one of those attributes results in a program whose set of possible executions ([intro.abstract]) for a given input is a subset of those of the original program for the same input, absent implementation-defined guarantees with respect to that attribute.

— end note]

An attribute-token is reserved for future standardization if

it is not an attribute-scoped-token and is not specified in this document, or

it is an attribute-scoped-token and its attribute-namespace isstd followed by zero or more digits.

Each implementation should choose a distinctive name for theattribute-namespace in an attribute-scoped-token.

8

#

Two consecutive left square bracket tokens shall appear only when introducing an attribute-specifier or within the balanced-token-seq of an attribute-argument-clause.

[Note 6:

If two consecutive left square brackets appear where an attribute-specifier is not allowed, the program is ill-formed even if the brackets match an alternative grammar production.

— end note]

[Example 2: int p[10];void f() {int x = 42, y[5]; int(p[[x] { return x; }()]); // error: invalid attribute on a nested declarator-id and// not a function-style cast of an element of p. y)]]; // well-formed implementation-defined attribute.} — end example]