1 //===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements initializer parsing as specified by C99 6.7.8.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Basic/TokenKinds.h"
14 #include "clang/Parse/ParseDiagnostic.h"
15 #include "clang/Parse/Parser.h"
16 #include "clang/Parse/RAIIObjectsForParser.h"
17 #include "clang/Sema/Designator.h"
18 #include "clang/Sema/EnterExpressionEvaluationContext.h"
19 #include "clang/Sema/Ownership.h"
20 #include "clang/Sema/Scope.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallString.h"
23 using namespace clang
;
26 /// MayBeDesignationStart - Return true if the current token might be the start
27 /// of a designator. If we can tell it is impossible that it is a designator,
29 bool Parser::MayBeDesignationStart() {
30 switch (Tok
.getKind()) {
34 case tok::period
: // designator: '.' identifier
37 case tok::l_square
: { // designator: array-designator
38 if (!PP
.getLangOpts().CPlusPlus11
)
41 // C++11 lambda expressions and C99 designators can be ambiguous all the
42 // way through the closing ']' and to the next character. Handle the easy
43 // cases here, and fall back to tentative parsing if those fail.
44 switch (PP
.LookAhead(0).getKind()) {
48 // Definitely starts a lambda expression.
55 // We have to do additional analysis, because these could be the
56 // start of a constant expression or a lambda capture list.
60 // Anything not mentioned above cannot occur following a '[' in a
65 // Handle the complicated case below.
68 case tok::identifier
: // designation: identifier ':'
69 return PP
.LookAhead(0).is(tok::colon
);
72 // Parse up to (at most) the token after the closing ']' to determine
73 // whether this is a C99 designator or a lambda.
74 RevertingTentativeParsingAction
Tentative(*this);
76 LambdaIntroducer Intro
;
77 LambdaIntroducerTentativeParse ParseResult
;
78 if (ParseLambdaIntroducer(Intro
, &ParseResult
)) {
79 // Hit and diagnosed an error in a lambda.
80 // FIXME: Tell the caller this happened so they can recover.
84 switch (ParseResult
) {
85 case LambdaIntroducerTentativeParse::Success
:
86 case LambdaIntroducerTentativeParse::Incomplete
:
87 // Might be a lambda-expression. Keep looking.
88 // FIXME: If our tentative parse was not incomplete, parse the lambda from
89 // here rather than throwing away then reparsing the LambdaIntroducer.
92 case LambdaIntroducerTentativeParse::MessageSend
:
93 case LambdaIntroducerTentativeParse::Invalid
:
94 // Can't be a lambda-expression. Treat it as a designator.
95 // FIXME: Should we disambiguate against a message-send?
99 // Once we hit the closing square bracket, we look at the next
100 // token. If it's an '=', this is a designator. Otherwise, it's a
101 // lambda expression. This decision favors lambdas over the older
102 // GNU designator syntax, which allows one to omit the '=', but is
103 // consistent with GCC.
104 return Tok
.is(tok::equal
);
107 static void CheckArrayDesignatorSyntax(Parser
&P
, SourceLocation Loc
,
108 Designation
&Desig
) {
109 // If we have exactly one array designator, this used the GNU
110 // 'designation: array-designator' extension, otherwise there should be no
111 // designators at all!
112 if (Desig
.getNumDesignators() == 1 &&
113 (Desig
.getDesignator(0).isArrayDesignator() ||
114 Desig
.getDesignator(0).isArrayRangeDesignator()))
115 P
.Diag(Loc
, diag::ext_gnu_missing_equal_designator
);
116 else if (Desig
.getNumDesignators() > 0)
117 P
.Diag(Loc
, diag::err_expected_equal_designator
);
120 /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
121 /// checking to see if the token stream starts with a designator.
126 /// designator-list '='
127 /// [GNU] array-designator
128 /// [GNU] identifier ':'
132 /// designator-list designator
138 /// array-designator:
139 /// '[' constant-expression ']'
140 /// [GNU] '[' constant-expression '...' constant-expression ']'
144 /// designated-initializer-list:
145 /// designated-initializer-clause
146 /// designated-initializer-list ',' designated-initializer-clause
148 /// designated-initializer-clause:
149 /// designator brace-or-equal-initializer
154 /// We allow the C99 syntax extensions in C++20, but do not allow the C++20
155 /// extension (a braced-init-list after the designator with no '=') in C99.
157 /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
158 /// initializer (because it is an expression). We need to consider this case
159 /// when parsing array designators.
161 /// \p CodeCompleteCB is called with Designation parsed so far.
162 ExprResult
Parser::ParseInitializerWithPotentialDesignator(
163 DesignatorCompletionInfo DesignatorCompletion
) {
164 // If this is the old-style GNU extension:
165 // designation ::= identifier ':'
166 // Handle it as a field designator. Otherwise, this must be the start of a
167 // normal expression.
168 if (Tok
.is(tok::identifier
)) {
169 const IdentifierInfo
*FieldName
= Tok
.getIdentifierInfo();
171 SmallString
<256> NewSyntax
;
172 llvm::raw_svector_ostream(NewSyntax
) << '.' << FieldName
->getName()
175 SourceLocation NameLoc
= ConsumeToken(); // Eat the identifier.
177 assert(Tok
.is(tok::colon
) && "MayBeDesignationStart not working properly!");
178 SourceLocation ColonLoc
= ConsumeToken();
180 Diag(NameLoc
, diag::ext_gnu_old_style_field_designator
)
181 << FixItHint::CreateReplacement(SourceRange(NameLoc
, ColonLoc
),
185 D
.AddDesignator(Designator::CreateFieldDesignator(
186 FieldName
, SourceLocation(), NameLoc
));
187 PreferredType
.enterDesignatedInitializer(
188 Tok
.getLocation(), DesignatorCompletion
.PreferredBaseType
, D
);
189 return Actions
.ActOnDesignatedInitializer(D
, ColonLoc
, true,
193 // Desig - This is initialized when we see our first designator. We may have
194 // an objc message send with no designator, so we don't want to create this
198 // Parse each designator in the designator list until we find an initializer.
199 while (Tok
.is(tok::period
) || Tok
.is(tok::l_square
)) {
200 if (Tok
.is(tok::period
)) {
201 // designator: '.' identifier
202 SourceLocation DotLoc
= ConsumeToken();
204 if (Tok
.is(tok::code_completion
)) {
206 Actions
.CodeCompleteDesignator(DesignatorCompletion
.PreferredBaseType
,
207 DesignatorCompletion
.InitExprs
, Desig
);
210 if (Tok
.isNot(tok::identifier
)) {
211 Diag(Tok
.getLocation(), diag::err_expected_field_designator
);
215 Desig
.AddDesignator(Designator::CreateFieldDesignator(
216 Tok
.getIdentifierInfo(), DotLoc
, Tok
.getLocation()));
217 ConsumeToken(); // Eat the identifier.
221 // We must have either an array designator now or an objc message send.
222 assert(Tok
.is(tok::l_square
) && "Unexpected token!");
224 // Handle the two forms of array designator:
225 // array-designator: '[' constant-expression ']'
226 // array-designator: '[' constant-expression '...' constant-expression ']'
228 // Also, we have to handle the case where the expression after the
229 // designator an an objc message send: '[' objc-message-expr ']'.
230 // Interesting cases are:
231 // [foo bar] -> objc message send
232 // [foo] -> array designator
233 // [foo ... bar] -> array designator
234 // [4][foo bar] -> obsolete GNU designation with objc message send.
236 // We do not need to check for an expression starting with [[ here. If it
237 // contains an Objective-C message send, then it is not an ill-formed
238 // attribute. If it is a lambda-expression within an array-designator, then
239 // it will be rejected because a constant-expression cannot begin with a
240 // lambda-expression.
241 InMessageExpressionRAIIObject
InMessage(*this, true);
243 BalancedDelimiterTracker
T(*this, tok::l_square
);
245 SourceLocation StartLoc
= T
.getOpenLocation();
249 // If Objective-C is enabled and this is a typename (class message
250 // send) or send to 'super', parse this as a message send
251 // expression. We handle C++ and C separately, since C++ requires
252 // much more complicated parsing.
253 if (getLangOpts().ObjC
&& getLangOpts().CPlusPlus
) {
255 if (Tok
.is(tok::identifier
) && Tok
.getIdentifierInfo() == Ident_super
&&
256 NextToken().isNot(tok::period
) &&
257 getCurScope()->isInObjcMethodScope()) {
258 CheckArrayDesignatorSyntax(*this, StartLoc
, Desig
);
259 return ParseAssignmentExprWithObjCMessageExprStart(
260 StartLoc
, ConsumeToken(), nullptr, nullptr);
263 // Parse the receiver, which is either a type or an expression.
266 if (ParseObjCXXMessageReceiver(IsExpr
, TypeOrExpr
)) {
267 SkipUntil(tok::r_square
, StopAtSemi
);
271 // If the receiver was a type, we have a class message; parse
274 CheckArrayDesignatorSyntax(*this, StartLoc
, Desig
);
275 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc
,
277 ParsedType::getFromOpaquePtr(TypeOrExpr
),
281 // If the receiver was an expression, we still don't know
282 // whether we have a message send or an array designator; just
283 // adopt the expression for further analysis below.
284 // FIXME: potentially-potentially evaluated expression above?
285 Idx
= ExprResult(static_cast<Expr
*>(TypeOrExpr
));
286 } else if (getLangOpts().ObjC
&& Tok
.is(tok::identifier
)) {
287 IdentifierInfo
*II
= Tok
.getIdentifierInfo();
288 SourceLocation IILoc
= Tok
.getLocation();
289 ParsedType ReceiverType
;
290 // Three cases. This is a message send to a type: [type foo]
291 // This is a message send to super: [super foo]
292 // This is a message sent to an expr: [super.bar foo]
293 switch (Actions
.getObjCMessageKind(
294 getCurScope(), II
, IILoc
, II
== Ident_super
,
295 NextToken().is(tok::period
), ReceiverType
)) {
296 case Sema::ObjCSuperMessage
:
297 CheckArrayDesignatorSyntax(*this, StartLoc
, Desig
);
298 return ParseAssignmentExprWithObjCMessageExprStart(
299 StartLoc
, ConsumeToken(), nullptr, nullptr);
301 case Sema::ObjCClassMessage
:
302 CheckArrayDesignatorSyntax(*this, StartLoc
, Desig
);
303 ConsumeToken(); // the identifier
305 SkipUntil(tok::r_square
, StopAtSemi
);
309 // Parse type arguments and protocol qualifiers.
310 if (Tok
.is(tok::less
)) {
311 SourceLocation NewEndLoc
;
312 TypeResult NewReceiverType
313 = parseObjCTypeArgsAndProtocolQualifiers(IILoc
, ReceiverType
,
314 /*consumeLastToken=*/true,
316 if (!NewReceiverType
.isUsable()) {
317 SkipUntil(tok::r_square
, StopAtSemi
);
321 ReceiverType
= NewReceiverType
.get();
324 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc
,
329 case Sema::ObjCInstanceMessage
:
330 // Fall through; we'll just parse the expression and
331 // (possibly) treat this like an Objective-C message send
337 // Parse the index expression, if we haven't already gotten one
338 // above (which can only happen in Objective-C++).
339 // Note that we parse this as an assignment expression, not a constant
340 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
341 // to validate that the expression is a constant.
342 // FIXME: We also need to tell Sema that we're in a
343 // potentially-potentially evaluated context.
345 Idx
= ParseAssignmentExpression();
346 if (Idx
.isInvalid()) {
347 SkipUntil(tok::r_square
, StopAtSemi
);
352 // Given an expression, we could either have a designator (if the next
353 // tokens are '...' or ']' or an objc message send. If this is an objc
354 // message send, handle it now. An objc-message send is the start of
355 // an assignment-expression production.
356 if (getLangOpts().ObjC
&& Tok
.isNot(tok::ellipsis
) &&
357 Tok
.isNot(tok::r_square
)) {
358 CheckArrayDesignatorSyntax(*this, Tok
.getLocation(), Desig
);
359 return ParseAssignmentExprWithObjCMessageExprStart(
360 StartLoc
, SourceLocation(), nullptr, Idx
.get());
363 // If this is a normal array designator, remember it.
364 if (Tok
.isNot(tok::ellipsis
)) {
365 Desig
.AddDesignator(Designator::CreateArrayDesignator(Idx
.get(),
368 // Handle the gnu array range extension.
369 Diag(Tok
, diag::ext_gnu_array_range
);
370 SourceLocation EllipsisLoc
= ConsumeToken();
372 ExprResult
RHS(ParseConstantExpression());
373 if (RHS
.isInvalid()) {
374 SkipUntil(tok::r_square
, StopAtSemi
);
377 Desig
.AddDesignator(Designator::CreateArrayRangeDesignator(
378 Idx
.get(), RHS
.get(), StartLoc
, EllipsisLoc
));
382 Desig
.getDesignator(Desig
.getNumDesignators() - 1).setRBracketLoc(
383 T
.getCloseLocation());
386 // Okay, we're done with the designator sequence. We know that there must be
387 // at least one designator, because the only case we can get into this method
388 // without a designator is when we have an objc message send. That case is
389 // handled and returned from above.
390 assert(!Desig
.empty() && "Designator is empty?");
392 // Handle a normal designator sequence end, which is an equal.
393 if (Tok
.is(tok::equal
)) {
394 SourceLocation EqualLoc
= ConsumeToken();
395 PreferredType
.enterDesignatedInitializer(
396 Tok
.getLocation(), DesignatorCompletion
.PreferredBaseType
, Desig
);
397 return Actions
.ActOnDesignatedInitializer(Desig
, EqualLoc
, false,
401 // Handle a C++20 braced designated initialization, which results in
402 // direct-list-initialization of the aggregate element. We allow this as an
403 // extension from C++11 onwards (when direct-list-initialization was added).
404 if (Tok
.is(tok::l_brace
) && getLangOpts().CPlusPlus11
) {
405 PreferredType
.enterDesignatedInitializer(
406 Tok
.getLocation(), DesignatorCompletion
.PreferredBaseType
, Desig
);
407 return Actions
.ActOnDesignatedInitializer(Desig
, SourceLocation(), false,
408 ParseBraceInitializer());
411 // We read some number of designators and found something that isn't an = or
412 // an initializer. If we have exactly one array designator, this
413 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
415 if (Desig
.getNumDesignators() == 1 &&
416 (Desig
.getDesignator(0).isArrayDesignator() ||
417 Desig
.getDesignator(0).isArrayRangeDesignator())) {
418 Diag(Tok
, diag::ext_gnu_missing_equal_designator
)
419 << FixItHint::CreateInsertion(Tok
.getLocation(), "= ");
420 return Actions
.ActOnDesignatedInitializer(Desig
, Tok
.getLocation(),
421 true, ParseInitializer());
424 Diag(Tok
, diag::err_expected_equal_designator
);
428 /// ParseBraceInitializer - Called when parsing an initializer that has a
429 /// leading open brace.
431 /// initializer: [C99 6.7.8]
432 /// '{' initializer-list '}'
433 /// '{' initializer-list ',' '}'
436 /// initializer-list:
437 /// designation[opt] initializer ...[opt]
438 /// initializer-list ',' designation[opt] initializer ...[opt]
440 ExprResult
Parser::ParseBraceInitializer() {
441 InMessageExpressionRAIIObject
InMessage(*this, false);
443 BalancedDelimiterTracker
T(*this, tok::l_brace
);
445 SourceLocation LBraceLoc
= T
.getOpenLocation();
447 /// InitExprs - This is the actual list of expressions contained in the
449 ExprVector InitExprs
;
451 if (Tok
.is(tok::r_brace
)) {
452 // Empty initializers are a C++ feature and a GNU extension to C before C23.
453 if (!getLangOpts().CPlusPlus
) {
454 Diag(LBraceLoc
, getLangOpts().C23
455 ? diag::warn_c23_compat_empty_initializer
456 : diag::ext_c_empty_initializer
);
459 return Actions
.ActOnInitList(LBraceLoc
, std::nullopt
, ConsumeBrace());
462 // Enter an appropriate expression evaluation context for an initializer list.
463 EnterExpressionEvaluationContext
EnterContext(
464 Actions
, EnterExpressionEvaluationContext::InitList
);
466 bool InitExprsOk
= true;
467 QualType LikelyType
= PreferredType
.get(T
.getOpenLocation());
468 DesignatorCompletionInfo DesignatorCompletion
{InitExprs
, LikelyType
};
469 bool CalledSignatureHelp
= false;
470 auto RunSignatureHelp
= [&] {
471 QualType PreferredType
;
472 if (!LikelyType
.isNull())
473 PreferredType
= Actions
.ProduceConstructorSignatureHelp(
474 LikelyType
->getCanonicalTypeInternal(), T
.getOpenLocation(),
475 InitExprs
, T
.getOpenLocation(), /*Braced=*/true);
476 CalledSignatureHelp
= true;
477 return PreferredType
;
481 PreferredType
.enterFunctionArgument(Tok
.getLocation(), RunSignatureHelp
);
483 // Handle Microsoft __if_exists/if_not_exists if necessary.
484 if (getLangOpts().MicrosoftExt
&& (Tok
.is(tok::kw___if_exists
) ||
485 Tok
.is(tok::kw___if_not_exists
))) {
486 if (ParseMicrosoftIfExistsBraceInitializer(InitExprs
, InitExprsOk
)) {
487 if (Tok
.isNot(tok::comma
)) break;
490 if (Tok
.is(tok::r_brace
)) break;
494 // Parse: designation[opt] initializer
496 // If we know that this cannot be a designation, just parse the nested
497 // initializer directly.
499 if (MayBeDesignationStart())
500 SubElt
= ParseInitializerWithPotentialDesignator(DesignatorCompletion
);
502 SubElt
= ParseInitializer();
504 if (Tok
.is(tok::ellipsis
))
505 SubElt
= Actions
.ActOnPackExpansion(SubElt
.get(), ConsumeToken());
507 SubElt
= Actions
.CorrectDelayedTyposInExpr(SubElt
.get());
509 // If we couldn't parse the subelement, bail out.
510 if (SubElt
.isUsable()) {
511 InitExprs
.push_back(SubElt
.get());
515 // We have two ways to try to recover from this error: if the code looks
516 // grammatically ok (i.e. we have a comma coming up) try to continue
517 // parsing the rest of the initializer. This allows us to emit
518 // diagnostics for later elements that we find. If we don't see a comma,
519 // assume there is a parse error, and just skip to recover.
520 // FIXME: This comment doesn't sound right. If there is a r_brace
521 // immediately, it can't be an error, since there is no other way of
522 // leaving this loop except through this if.
523 if (Tok
.isNot(tok::comma
)) {
524 SkipUntil(tok::r_brace
, StopBeforeMatch
);
529 // If we don't have a comma continued list, we're done.
530 if (Tok
.isNot(tok::comma
)) break;
532 // TODO: save comma locations if some client cares.
535 // Handle trailing comma.
536 if (Tok
.is(tok::r_brace
)) break;
539 bool closed
= !T
.consumeClose();
541 if (InitExprsOk
&& closed
)
542 return Actions
.ActOnInitList(LBraceLoc
, InitExprs
,
543 T
.getCloseLocation());
545 return ExprError(); // an error occurred.
549 // Return true if a comma (or closing brace) is necessary after the
550 // __if_exists/if_not_exists statement.
551 bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector
&InitExprs
,
553 bool trailingComma
= false;
554 IfExistsCondition Result
;
555 if (ParseMicrosoftIfExistsCondition(Result
))
558 BalancedDelimiterTracker
Braces(*this, tok::l_brace
);
559 if (Braces
.consumeOpen()) {
560 Diag(Tok
, diag::err_expected
) << tok::l_brace
;
564 switch (Result
.Behavior
) {
566 // Parse the declarations below.
570 Diag(Result
.KeywordLoc
, diag::warn_microsoft_dependent_exists
)
571 << Result
.IsIfExists
;
572 // Fall through to skip.
580 DesignatorCompletionInfo DesignatorCompletion
{
582 PreferredType
.get(Braces
.getOpenLocation()),
584 while (!isEofOrEom()) {
585 trailingComma
= false;
586 // If we know that this cannot be a designation, just parse the nested
587 // initializer directly.
589 if (MayBeDesignationStart())
590 SubElt
= ParseInitializerWithPotentialDesignator(DesignatorCompletion
);
592 SubElt
= ParseInitializer();
594 if (Tok
.is(tok::ellipsis
))
595 SubElt
= Actions
.ActOnPackExpansion(SubElt
.get(), ConsumeToken());
597 // If we couldn't parse the subelement, bail out.
598 if (!SubElt
.isInvalid())
599 InitExprs
.push_back(SubElt
.get());
603 if (Tok
.is(tok::comma
)) {
605 trailingComma
= true;
608 if (Tok
.is(tok::r_brace
))
612 Braces
.consumeClose();
614 return !trailingComma
;