Skip to content

Commit f40f23d

Browse files
tkoeppezygoloid
authored andcommitted
[stmt] Move grammar and description of 'condition' up
1 parent 347be70 commit f40f23d

File tree

1 file changed

+66
-68
lines changed

1 file changed

+66
-68
lines changed

source/statements.tex

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,76 @@
2424
\nontermdef{init-statement}\br
2525
expression-statement\br
2626
simple-declaration
27+
28+
\nontermdef{condition}\br
29+
expression\br
30+
attribute-specifier-seq\opt decl-specifier-seq declarator \terminal{=} initializer-clause\br
31+
attribute-specifier-seq\opt decl-specifier-seq declarator braced-init-list
2732
\end{bnf}
2833

2934
The optional \grammarterm{attribute-specifier-seq} appertains to the respective statement.
3035

36+
\pnum
37+
\indextext{\idxgram{condition}{s}!rules~for}%
38+
The rules for \grammarterm{}{condition}{s} apply both to
39+
\grammarterm{selection-statement}{s} and to the \tcode{for} and \tcode{while}
40+
statements~(\ref{stmt.iter}). The \grammarterm{}{declarator} shall not
41+
specify a function or an array. The \grammarterm{decl-specifier-seq} shall not
42+
define a class or enumeration. If the \tcode{auto} \nonterminal{type-specifier} appears in
43+
the \nonterminal{decl-specifier-seq},
44+
the type of the identifier being declared is deduced from the initializer as described in~\ref{dcl.spec.auto}.
45+
46+
\pnum
47+
\indextext{statement!declaration in \tcode{if}}%
48+
\indextext{statement!declaration in \tcode{switch}}%
49+
A name introduced by a declaration in a \grammarterm{}{condition} (either
50+
introduced by the \grammarterm{decl-specifier-seq} or the
51+
\grammarterm{}{declarator} of the condition) is in scope from its point of
52+
declaration until the end of the substatements controlled by the
53+
condition. If the name is re-declared in the outermost block of a
54+
substatement controlled by the condition, the declaration that
55+
re-declares the name is ill-formed.
56+
\begin{example}
57+
58+
\begin{codeblock}
59+
if (int x = f()) {
60+
int x; // ill-formed, redeclaration of \tcode{x}
61+
}
62+
else {
63+
int x; // ill-formed, redeclaration of \tcode{x}
64+
}
65+
\end{codeblock}
66+
\end{example}
67+
68+
\pnum
69+
The value of a \grammarterm{}{condition} that is an initialized declaration
70+
in a statement other than a \tcode{switch} statement is the value of the
71+
declared variable
72+
contextually converted to \tcode{bool} (Clause~\ref{conv}).
73+
If that
74+
conversion is ill-formed, the program is ill-formed. The value of a
75+
\grammarterm{}{condition} that is an initialized declaration in a
76+
\tcode{switch} statement is the value of the declared variable if it has
77+
integral or enumeration type, or of that variable implicitly converted
78+
to integral or enumeration type otherwise. The value of a
79+
\grammarterm{}{condition} that is an expression is the value of the
80+
expression, contextually converted to \tcode{bool}
81+
for statements other
82+
than \tcode{switch};
83+
if that conversion is ill-formed, the program is
84+
ill-formed. The value of the condition will be referred to as simply
85+
``the condition'' where the usage is unambiguous.
86+
87+
\pnum
88+
If a \grammarterm{}{condition} can be syntactically resolved as either an
89+
expression or the declaration of a block-scope name, it is interpreted as a
90+
declaration.
91+
92+
\pnum
93+
In the \grammarterm{decl-specifier-seq} of a \grammarterm{condition}, each
94+
\grammarterm{decl-specifier} shall be either a \grammarterm{type-specifier}
95+
or \tcode{constexpr}.
96+
3197
\rSec1[stmt.label]{Labeled statement}%
3298
\indextext{statement!labeled}
3399

@@ -137,13 +203,6 @@
137203
\terminal{switch (} init-statement\opt condition \terminal{)} statement
138204
\end{bnf}
139205

140-
\begin{bnf}
141-
\nontermdef{condition}\br
142-
expression\br
143-
attribute-specifier-seq\opt decl-specifier-seq declarator \terminal{=} initializer-clause\br
144-
attribute-specifier-seq\opt decl-specifier-seq declarator braced-init-list
145-
\end{bnf}
146-
147206
See~\ref{dcl.meaning} for the optional \grammarterm{attribute-specifier-seq} in a condition.
148207
\begin{note}
149208
An \grammarterm{init-statement} ends with a semicolon.
@@ -176,67 +235,6 @@
176235
Thus after the \tcode{if} statement, \tcode{i} is no longer in scope.
177236
\end{example}
178237

179-
\pnum
180-
\indextext{\idxgram{condition}{s}!rules~for}%
181-
The rules for \grammarterm{}{condition}{s} apply both to
182-
\grammarterm{selection-statement}{s} and to the \tcode{for} and \tcode{while}
183-
statements~(\ref{stmt.iter}). The \grammarterm{}{declarator} shall not
184-
specify a function or an array. The \grammarterm{decl-specifier-seq} shall not
185-
define a class or enumeration. If the \tcode{auto} \nonterminal{type-specifier} appears in
186-
the \nonterminal{decl-specifier-seq},
187-
the type of the identifier being declared is deduced from the initializer as described in~\ref{dcl.spec.auto}.
188-
189-
\pnum
190-
\indextext{statement!declaration in \tcode{if}}%
191-
\indextext{statement!declaration in \tcode{switch}}%
192-
A name introduced by a declaration in a \grammarterm{}{condition} (either
193-
introduced by the \grammarterm{decl-specifier-seq} or the
194-
\grammarterm{}{declarator} of the condition) is in scope from its point of
195-
declaration until the end of the substatements controlled by the
196-
condition. If the name is re-declared in the outermost block of a
197-
substatement controlled by the condition, the declaration that
198-
re-declares the name is ill-formed.
199-
\begin{example}
200-
201-
\begin{codeblock}
202-
if (int x = f()) {
203-
int x; // ill-formed, redeclaration of \tcode{x}
204-
}
205-
else {
206-
int x; // ill-formed, redeclaration of \tcode{x}
207-
}
208-
\end{codeblock}
209-
\end{example}
210-
211-
\pnum
212-
The value of a \grammarterm{}{condition} that is an initialized declaration
213-
in a statement other than a \tcode{switch} statement is the value of the
214-
declared variable
215-
contextually converted to \tcode{bool} (Clause~\ref{conv}).
216-
If that
217-
conversion is ill-formed, the program is ill-formed. The value of a
218-
\grammarterm{}{condition} that is an initialized declaration in a
219-
\tcode{switch} statement is the value of the declared variable if it has
220-
integral or enumeration type, or of that variable implicitly converted
221-
to integral or enumeration type otherwise. The value of a
222-
\grammarterm{}{condition} that is an expression is the value of the
223-
expression, contextually converted to \tcode{bool}
224-
for statements other
225-
than \tcode{switch};
226-
if that conversion is ill-formed, the program is
227-
ill-formed. The value of the condition will be referred to as simply
228-
``the condition'' where the usage is unambiguous.
229-
230-
\pnum
231-
If a \grammarterm{}{condition} can be syntactically resolved as either an
232-
expression or the declaration of a block-scope name, it is interpreted as a
233-
declaration.
234-
235-
\pnum
236-
In the \grammarterm{decl-specifier-seq} of a \grammarterm{condition}, each
237-
\grammarterm{decl-specifier} shall be either a \grammarterm{type-specifier}
238-
or \tcode{constexpr}.
239-
240238
\rSec2[stmt.if]{The \tcode{if} statement}%
241239
\indextext{statement!\idxcode{if}}
242240

0 commit comments

Comments
 (0)