Jens Gustedt, INRIA and ICube, France
2023年12月13日
| document number | date | comment |
|---|---|---|
| n3189 | 202312 | this paper, original proposal |
CC BY, see https://creativecommons.org/licenses/by/4.0
The C standard differentiates two terms that have surprisingly different meaning:
"integer constant", which describes a certain category of tokens after lexing
"integer constant expression", which associates special properties to a non-terminal in the grammar
This even leads to confusion in the standard itself, because the definitions of sizeof and alignof cyclicly refer to the definition of "integer constant" and "integer constant expression". In other places the term "integer constant" is seemingly used with a different meaning than its definition, namely in places where talking about a constant of integer type would be in order.
The goal of this paper is to rename the terms "integer constant", "floating constant", "character constant" to "integer literal", "floating literal" and "character literal". A table summarizing these systematic changes can be found towards the end.
Otherwise we also rely on some terminology changes as they are introduced by the paper n3187, in particular for the change of to speak of "constant sizeof expression". But this can evidently be changed if that proposal doesn’t make it.
This is the case because the long list of cases mixes cases that talk about lexical concepts and others that talk about semantic properties of certain subexpressions. We propose to make these distinctions clearer by talking consistently about "literals" when we address a lexical feature, and talk about ‘’constants’’ as a semantik concept that is attached to certain grammatical entities, but which is in general not deducible from local syntactic features.
We propose to make the following changes.
p8 An integer constant expression130) shall have integer type and shall only have operands that are integer
(削除) constants (削除ここまで)(追記) literals (追記ここまで), named (追記) constants, (追記ここまで)(削除) and (削除ここまで)compound literal constants of integer type, character(削除) constants (削除ここまで)(追記) literals (追記ここまで), (追記) constant (追記ここまで)sizeofexpressions(削除) whose results are integer constants (削除ここまで),alignofexpressions, and floating (追記) literals (追記ここまで), named (追記) constants (追記ここまで), or compound literal constants of arithmetic type that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the typeof operators,sizeofoperator, oralignofoperator.
...
p10 An arithmetic constant expression shall have arithmetic type and shall only have operands that are integer
(削除) constants (削除ここまで)(追記) literals (追記ここまで), floating(削除) constants (削除ここまで)(追記) literals (追記ここまで), named (追記) constants (追記ここまで) or compound literal constants of arithmetic type, character(削除) constants (削除ここまで)(追記) literals (追記ここまで), (追記) constant (追記ここまで)sizeofexpressions(削除) whose results are integer constants (削除ここまで), andalignofexpressions. Cast operators in an arithmetic constant expression shall only convert arithmetic types to arithmetic types, except as part of an operand to the typeof operators,sizeofoperator, oralignofoperator.
In 6.2.5
p21 An enumeration comprises a set of named
(削除) integer constant values (削除ここまで)(追記) constants of integer type (追記ここまで). ...
This misses in 6.4.8 (Preprocessing numbers) that some pp-number tokens are already interpreted as integer literals during pre-processing itself.
3 Preprocessing number tokens lexically include all floating and integer
(削除) constant tokens (削除ここまで)(追記) literals (追記ここまで).
Semantics
4 A preprocessing number does not have type or a value; it acquires both after a successful conversion (as part of translation phase 7) to a floating
(削除) constant token (削除ここまで)(追記) literal (追記ここまで) or an integer(削除) constant token (削除ここまで)(追記) literal (追記ここまで). (追記) This not withstanding for the evaluation of expressions within conditional source inclusion (6.10.1) and to determine thelimitparameter for binary resource inclusion (6.10.3), preprocessing numbers shall have the form of an integer literal and are interpreted as such. For the determination of a line number in a#linedirective (6.10.5) digit sequences that also match the requirements for a preprocessing number are interpreted as numbers as well, only that the interpretation is of a decimal integer, even if the leading digit is0. (追記ここまで)
Already in the existing text the term "integer character constant" is a misnomer, because in fact all character constants have integer type. With the proposed changes "integer character constant", would now become "integer character literal", which is equally confusing. We propose change this term to "simple character literal".
In many places, the term "integer character constant" is then currently used as if it would also include UTF-8 character constants/literals. In fact, it seems that the text that speaks of "integer character constant" has not been properly updated when introducing UTF-8 character literals. Therefor we propose to introduce a new term "narrow character literal" that comprises these two cases.
A
(削除) integer (削除ここまで)(追記) simple (追記ここまで) character(削除) constant (削除ここまで)(追記) literal (追記ここまで) is a sequence of one or more multibyte characters enclosed in single-quotes, as in’x’. A UTF-8 character(削除) constant (削除ここまで)(追記) literal (追記ここまで) is the same, except prefixed byu8. (追記) Together simple and UTF-8 character literal are narrow character literals. (追記ここまで) Awchar_tcharacter(削除) constant (削除ここまで)(追記) literal (追記ここまで) is prefixed by the letterL. A UTF-16 character(削除) constant (削除ここまで)(追記) literal (追記ここまで) is prefixed by the letteru. A UTF-32 character(削除) constant (削除ここまで)(追記) literal (追記ここまで) is prefixed by the letterU. Collectively,wchar_t, UTF-16, and UTF-32 character(削除) constants (削除ここまで)(追記) literals (追記ここまで) are called wide character(削除) constants (削除ここまで)(追記) literals (追記ここまで). ...
<stdint.h>There are inconsistencies in 6.24 what seems to be a confusion of the concepts of "integer constant" and "integer constant expression".
7.22.4 Macros for
(削除) integer constants (削除ここまで)(追記) constants of integer type (追記ここまで)
1 The following function-like macros expand to integer constant (追記) expressions (追記ここまで) suitable for initializing objects that have integer types corresponding to types defined in
<stdint.h>. Each macro name corresponds to a similar type name in 7.22.1.2 or 7.22.1.5.
2 The argument in any instance of these macros shall be an unsuffixed integer
(削除) constant (削除ここまで)(追記) literal (追記ここまで) (as defined in 6.4.4.1) with a value that does not exceed the limits for the corresponding type.
3 ...
7.22.4.1 Macros for minimum-width
(削除) integer constants (削除ここまで)(追記) constants of integer type (追記ここまで)
...
7.22.4.2 Macros for greatest-width
(削除) integer constants (削除ここまで)(追記) constants of integer type (追記ここまで)
...
First do the changes as indicated above. Then in the order as shown
| C23 | C2y | |
|---|---|---|
| integer character constant | simple character literal | as above and in 6.4.4.4 p11 and p18 |
| integer character constant | narrow character literal | otherwise |
| character constant | character literal | otherwise |
| integer constant | integer literal | other than in integer constant expression |
| floating constant | floating literal |