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/Parse/ParseDiagnostic.h"
14 #include "clang/Parse/Parser.h"
15 #include "clang/Parse/RAIIObjectsForParser.h"
16 #include "clang/Sema/Designator.h"
17 #include "clang/Sema/Scope.h"
18 #include "llvm/ADT/SmallString.h"
19 using namespace clang;
22 /// MayBeDesignationStart - Return true if the current token might be the start
23 /// of a designator. If we can tell it is impossible that it is a designator,
25 bool Parser::MayBeDesignationStart() {
26 switch (Tok.getKind()) {
30 case tok::period: // designator: '.' identifier
33 case tok::l_square: { // designator: array-designator
34 if (!PP.getLangOpts().CPlusPlus11)
37 // C++11 lambda expressions and C99 designators can be ambiguous all the
38 // way through the closing ']' and to the next character. Handle the easy
39 // cases here, and fall back to tentative parsing if those fail.
40 switch (PP.LookAhead(0).getKind()) {
43 // Definitely starts a lambda expression.
49 // We have to do additional analysis, because these could be the
50 // start of a constant expression or a lambda capture list.
54 // Anything not mentioned above cannot occur following a '[' in a
59 // Handle the complicated case below.
62 case tok::identifier: // designation: identifier ':'
63 return PP.LookAhead(0).is(tok::colon);
66 // Parse up to (at most) the token after the closing ']' to determine
67 // whether this is a C99 designator or a lambda.
68 RevertingTentativeParsingAction Tentative(*this);
70 LambdaIntroducer Intro;
71 LambdaIntroducerTentativeParse ParseResult;
72 if (ParseLambdaIntroducer(Intro, &ParseResult)) {
73 // Hit and diagnosed an error in a lambda.
74 // FIXME: Tell the caller this happened so they can recover.
78 switch (ParseResult) {
79 case LambdaIntroducerTentativeParse::Success:
80 case LambdaIntroducerTentativeParse::Incomplete:
81 // Might be a lambda-expression. Keep looking.
82 // FIXME: If our tentative parse was not incomplete, parse the lambda from
83 // here rather than throwing away then reparsing the LambdaIntroducer.
86 case LambdaIntroducerTentativeParse::MessageSend:
87 case LambdaIntroducerTentativeParse::Invalid:
88 // Can't be a lambda-expression. Treat it as a designator.
89 // FIXME: Should we disambiguate against a message-send?
93 // Once we hit the closing square bracket, we look at the next
94 // token. If it's an '=', this is a designator. Otherwise, it's a
95 // lambda expression. This decision favors lambdas over the older
96 // GNU designator syntax, which allows one to omit the '=', but is
97 // consistent with GCC.
98 return Tok.is(tok::equal);
101 static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
102 Designation &Desig) {
103 // If we have exactly one array designator, this used the GNU
104 // 'designation: array-designator' extension, otherwise there should be no
105 // designators at all!
106 if (Desig.getNumDesignators() == 1 &&
107 (Desig.getDesignator(0).isArrayDesignator() ||
108 Desig.getDesignator(0).isArrayRangeDesignator()))
109 P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
110 else if (Desig.getNumDesignators() > 0)
111 P.Diag(Loc, diag::err_expected_equal_designator);
114 /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
115 /// checking to see if the token stream starts with a designator.
118 /// designator-list '='
119 /// [GNU] array-designator
120 /// [GNU] identifier ':'
124 /// designator-list designator
130 /// array-designator:
131 /// '[' constant-expression ']'
132 /// [GNU] '[' constant-expression '...' constant-expression ']'
134 /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
135 /// initializer (because it is an expression). We need to consider this case
136 /// when parsing array designators.
138 ExprResult Parser::ParseInitializerWithPotentialDesignator() {
140 // If this is the old-style GNU extension:
141 // designation ::= identifier ':'
142 // Handle it as a field designator. Otherwise, this must be the start of a
143 // normal expression.
144 if (Tok.is(tok::identifier)) {
145 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
147 SmallString<256> NewSyntax;
148 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
151 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
153 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
154 SourceLocation ColonLoc = ConsumeToken();
156 Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
157 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
161 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
162 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
166 // Desig - This is initialized when we see our first designator. We may have
167 // an objc message send with no designator, so we don't want to create this
171 // Parse each designator in the designator list until we find an initializer.
172 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
173 if (Tok.is(tok::period)) {
174 // designator: '.' identifier
175 SourceLocation DotLoc = ConsumeToken();
177 if (Tok.isNot(tok::identifier)) {
178 Diag(Tok.getLocation(), diag::err_expected_field_designator);
182 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
184 ConsumeToken(); // Eat the identifier.
188 // We must have either an array designator now or an objc message send.
189 assert(Tok.is(tok::l_square) && "Unexpected token!");
191 // Handle the two forms of array designator:
192 // array-designator: '[' constant-expression ']'
193 // array-designator: '[' constant-expression '...' constant-expression ']'
195 // Also, we have to handle the case where the expression after the
196 // designator an an objc message send: '[' objc-message-expr ']'.
197 // Interesting cases are:
198 // [foo bar] -> objc message send
199 // [foo] -> array designator
200 // [foo ... bar] -> array designator
201 // [4][foo bar] -> obsolete GNU designation with objc message send.
203 // We do not need to check for an expression starting with [[ here. If it
204 // contains an Objective-C message send, then it is not an ill-formed
205 // attribute. If it is a lambda-expression within an array-designator, then
206 // it will be rejected because a constant-expression cannot begin with a
207 // lambda-expression.
208 InMessageExpressionRAIIObject InMessage(*this, true);
210 BalancedDelimiterTracker T(*this, tok::l_square);
212 SourceLocation StartLoc = T.getOpenLocation();
216 // If Objective-C is enabled and this is a typename (class message
217 // send) or send to 'super', parse this as a message send
218 // expression. We handle C++ and C separately, since C++ requires
219 // much more complicated parsing.
220 if (getLangOpts().ObjC && getLangOpts().CPlusPlus) {
222 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
223 NextToken().isNot(tok::period) &&
224 getCurScope()->isInObjcMethodScope()) {
225 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
226 return ParseAssignmentExprWithObjCMessageExprStart(
227 StartLoc, ConsumeToken(), nullptr, nullptr);
230 // Parse the receiver, which is either a type or an expression.
233 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
234 SkipUntil(tok::r_square, StopAtSemi);
238 // If the receiver was a type, we have a class message; parse
241 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
242 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
244 ParsedType::getFromOpaquePtr(TypeOrExpr),
248 // If the receiver was an expression, we still don't know
249 // whether we have a message send or an array designator; just
250 // adopt the expression for further analysis below.
251 // FIXME: potentially-potentially evaluated expression above?
252 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
253 } else if (getLangOpts().ObjC && Tok.is(tok::identifier)) {
254 IdentifierInfo *II = Tok.getIdentifierInfo();
255 SourceLocation IILoc = Tok.getLocation();
256 ParsedType ReceiverType;
257 // Three cases. This is a message send to a type: [type foo]
258 // This is a message send to super: [super foo]
259 // This is a message sent to an expr: [super.bar foo]
260 switch (Actions.getObjCMessageKind(
261 getCurScope(), II, IILoc, II == Ident_super,
262 NextToken().is(tok::period), ReceiverType)) {
263 case Sema::ObjCSuperMessage:
264 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
265 return ParseAssignmentExprWithObjCMessageExprStart(
266 StartLoc, ConsumeToken(), nullptr, nullptr);
268 case Sema::ObjCClassMessage:
269 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
270 ConsumeToken(); // the identifier
272 SkipUntil(tok::r_square, StopAtSemi);
276 // Parse type arguments and protocol qualifiers.
277 if (Tok.is(tok::less)) {
278 SourceLocation NewEndLoc;
279 TypeResult NewReceiverType
280 = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType,
281 /*consumeLastToken=*/true,
283 if (!NewReceiverType.isUsable()) {
284 SkipUntil(tok::r_square, StopAtSemi);
288 ReceiverType = NewReceiverType.get();
291 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
296 case Sema::ObjCInstanceMessage:
297 // Fall through; we'll just parse the expression and
298 // (possibly) treat this like an Objective-C message send
304 // Parse the index expression, if we haven't already gotten one
305 // above (which can only happen in Objective-C++).
306 // Note that we parse this as an assignment expression, not a constant
307 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
308 // to validate that the expression is a constant.
309 // FIXME: We also need to tell Sema that we're in a
310 // potentially-potentially evaluated context.
312 Idx = ParseAssignmentExpression();
313 if (Idx.isInvalid()) {
314 SkipUntil(tok::r_square, StopAtSemi);
319 // Given an expression, we could either have a designator (if the next
320 // tokens are '...' or ']' or an objc message send. If this is an objc
321 // message send, handle it now. An objc-message send is the start of
322 // an assignment-expression production.
323 if (getLangOpts().ObjC && Tok.isNot(tok::ellipsis) &&
324 Tok.isNot(tok::r_square)) {
325 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
326 return ParseAssignmentExprWithObjCMessageExprStart(
327 StartLoc, SourceLocation(), nullptr, Idx.get());
330 // If this is a normal array designator, remember it.
331 if (Tok.isNot(tok::ellipsis)) {
332 Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc));
334 // Handle the gnu array range extension.
335 Diag(Tok, diag::ext_gnu_array_range);
336 SourceLocation EllipsisLoc = ConsumeToken();
338 ExprResult RHS(ParseConstantExpression());
339 if (RHS.isInvalid()) {
340 SkipUntil(tok::r_square, StopAtSemi);
343 Desig.AddDesignator(Designator::getArrayRange(Idx.get(),
345 StartLoc, EllipsisLoc));
349 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
350 T.getCloseLocation());
353 // Okay, we're done with the designator sequence. We know that there must be
354 // at least one designator, because the only case we can get into this method
355 // without a designator is when we have an objc message send. That case is
356 // handled and returned from above.
357 assert(!Desig.empty() && "Designator is empty?");
359 // Handle a normal designator sequence end, which is an equal.
360 if (Tok.is(tok::equal)) {
361 SourceLocation EqualLoc = ConsumeToken();
362 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
366 // We read some number of designators and found something that isn't an = or
367 // an initializer. If we have exactly one array designator, this
368 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
370 if (Desig.getNumDesignators() == 1 &&
371 (Desig.getDesignator(0).isArrayDesignator() ||
372 Desig.getDesignator(0).isArrayRangeDesignator())) {
373 Diag(Tok, diag::ext_gnu_missing_equal_designator)
374 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
375 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
376 true, ParseInitializer());
379 Diag(Tok, diag::err_expected_equal_designator);
384 /// ParseBraceInitializer - Called when parsing an initializer that has a
385 /// leading open brace.
387 /// initializer: [C99 6.7.8]
388 /// '{' initializer-list '}'
389 /// '{' initializer-list ',' '}'
392 /// initializer-list:
393 /// designation[opt] initializer ...[opt]
394 /// initializer-list ',' designation[opt] initializer ...[opt]
396 ExprResult Parser::ParseBraceInitializer() {
397 InMessageExpressionRAIIObject InMessage(*this, false);
399 BalancedDelimiterTracker T(*this, tok::l_brace);
401 SourceLocation LBraceLoc = T.getOpenLocation();
403 /// InitExprs - This is the actual list of expressions contained in the
405 ExprVector InitExprs;
407 if (Tok.is(tok::r_brace)) {
408 // Empty initializers are a C++ feature and a GNU extension to C.
409 if (!getLangOpts().CPlusPlus)
410 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
412 return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace());
415 // Enter an appropriate expression evaluation context for an initializer list.
416 EnterExpressionEvaluationContext EnterContext(
417 Actions, EnterExpressionEvaluationContext::InitList);
419 bool InitExprsOk = true;
422 // Handle Microsoft __if_exists/if_not_exists if necessary.
423 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
424 Tok.is(tok::kw___if_not_exists))) {
425 if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
426 if (Tok.isNot(tok::comma)) break;
429 if (Tok.is(tok::r_brace)) break;
433 // Parse: designation[opt] initializer
435 // If we know that this cannot be a designation, just parse the nested
436 // initializer directly.
438 if (MayBeDesignationStart())
439 SubElt = ParseInitializerWithPotentialDesignator();
441 SubElt = ParseInitializer();
443 if (Tok.is(tok::ellipsis))
444 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
446 SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get());
448 // If we couldn't parse the subelement, bail out.
449 if (SubElt.isUsable()) {
450 InitExprs.push_back(SubElt.get());
454 // We have two ways to try to recover from this error: if the code looks
455 // grammatically ok (i.e. we have a comma coming up) try to continue
456 // parsing the rest of the initializer. This allows us to emit
457 // diagnostics for later elements that we find. If we don't see a comma,
458 // assume there is a parse error, and just skip to recover.
459 // FIXME: This comment doesn't sound right. If there is a r_brace
460 // immediately, it can't be an error, since there is no other way of
461 // leaving this loop except through this if.
462 if (Tok.isNot(tok::comma)) {
463 SkipUntil(tok::r_brace, StopBeforeMatch);
468 // If we don't have a comma continued list, we're done.
469 if (Tok.isNot(tok::comma)) break;
471 // TODO: save comma locations if some client cares.
474 // Handle trailing comma.
475 if (Tok.is(tok::r_brace)) break;
478 bool closed = !T.consumeClose();
480 if (InitExprsOk && closed)
481 return Actions.ActOnInitList(LBraceLoc, InitExprs,
482 T.getCloseLocation());
484 return ExprError(); // an error occurred.
488 // Return true if a comma (or closing brace) is necessary after the
489 // __if_exists/if_not_exists statement.
490 bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
492 bool trailingComma = false;
493 IfExistsCondition Result;
494 if (ParseMicrosoftIfExistsCondition(Result))
497 BalancedDelimiterTracker Braces(*this, tok::l_brace);
498 if (Braces.consumeOpen()) {
499 Diag(Tok, diag::err_expected) << tok::l_brace;
503 switch (Result.Behavior) {
505 // Parse the declarations below.
509 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
510 << Result.IsIfExists;
511 // Fall through to skip.
519 while (!isEofOrEom()) {
520 trailingComma = false;
521 // If we know that this cannot be a designation, just parse the nested
522 // initializer directly.
524 if (MayBeDesignationStart())
525 SubElt = ParseInitializerWithPotentialDesignator();
527 SubElt = ParseInitializer();
529 if (Tok.is(tok::ellipsis))
530 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
532 // If we couldn't parse the subelement, bail out.
533 if (!SubElt.isInvalid())
534 InitExprs.push_back(SubElt.get());
538 if (Tok.is(tok::comma)) {
540 trailingComma = true;
543 if (Tok.is(tok::r_brace))
547 Braces.consumeClose();
549 return !trailingComma;