xref: /openbsd-src/gnu/llvm/clang/lib/Parse/ParseInit.cpp (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This file implements initializer parsing as specified by C99 6.7.8.
10e5dd7070Spatrick //
11e5dd7070Spatrick //===----------------------------------------------------------------------===//
12e5dd7070Spatrick 
13ec727ea7Spatrick #include "clang/Basic/TokenKinds.h"
14e5dd7070Spatrick #include "clang/Parse/ParseDiagnostic.h"
15e5dd7070Spatrick #include "clang/Parse/Parser.h"
16e5dd7070Spatrick #include "clang/Parse/RAIIObjectsForParser.h"
17e5dd7070Spatrick #include "clang/Sema/Designator.h"
18ec727ea7Spatrick #include "clang/Sema/Ownership.h"
19e5dd7070Spatrick #include "clang/Sema/Scope.h"
20ec727ea7Spatrick #include "llvm/ADT/STLExtras.h"
21e5dd7070Spatrick #include "llvm/ADT/SmallString.h"
22e5dd7070Spatrick using namespace clang;
23e5dd7070Spatrick 
24e5dd7070Spatrick 
25e5dd7070Spatrick /// MayBeDesignationStart - Return true if the current token might be the start
26e5dd7070Spatrick /// of a designator.  If we can tell it is impossible that it is a designator,
27e5dd7070Spatrick /// return false.
MayBeDesignationStart()28e5dd7070Spatrick bool Parser::MayBeDesignationStart() {
29e5dd7070Spatrick   switch (Tok.getKind()) {
30e5dd7070Spatrick   default:
31e5dd7070Spatrick     return false;
32e5dd7070Spatrick 
33e5dd7070Spatrick   case tok::period:      // designator: '.' identifier
34e5dd7070Spatrick     return true;
35e5dd7070Spatrick 
36e5dd7070Spatrick   case tok::l_square: {  // designator: array-designator
37e5dd7070Spatrick     if (!PP.getLangOpts().CPlusPlus11)
38e5dd7070Spatrick       return true;
39e5dd7070Spatrick 
40e5dd7070Spatrick     // C++11 lambda expressions and C99 designators can be ambiguous all the
41e5dd7070Spatrick     // way through the closing ']' and to the next character. Handle the easy
42e5dd7070Spatrick     // cases here, and fall back to tentative parsing if those fail.
43e5dd7070Spatrick     switch (PP.LookAhead(0).getKind()) {
44e5dd7070Spatrick     case tok::equal:
45e5dd7070Spatrick     case tok::ellipsis:
46e5dd7070Spatrick     case tok::r_square:
47e5dd7070Spatrick       // Definitely starts a lambda expression.
48e5dd7070Spatrick       return false;
49e5dd7070Spatrick 
50e5dd7070Spatrick     case tok::amp:
51e5dd7070Spatrick     case tok::kw_this:
52e5dd7070Spatrick     case tok::star:
53e5dd7070Spatrick     case tok::identifier:
54e5dd7070Spatrick       // We have to do additional analysis, because these could be the
55e5dd7070Spatrick       // start of a constant expression or a lambda capture list.
56e5dd7070Spatrick       break;
57e5dd7070Spatrick 
58e5dd7070Spatrick     default:
59e5dd7070Spatrick       // Anything not mentioned above cannot occur following a '[' in a
60e5dd7070Spatrick       // lambda expression.
61e5dd7070Spatrick       return true;
62e5dd7070Spatrick     }
63e5dd7070Spatrick 
64e5dd7070Spatrick     // Handle the complicated case below.
65e5dd7070Spatrick     break;
66e5dd7070Spatrick   }
67e5dd7070Spatrick   case tok::identifier:  // designation: identifier ':'
68e5dd7070Spatrick     return PP.LookAhead(0).is(tok::colon);
69e5dd7070Spatrick   }
70e5dd7070Spatrick 
71e5dd7070Spatrick   // Parse up to (at most) the token after the closing ']' to determine
72e5dd7070Spatrick   // whether this is a C99 designator or a lambda.
73e5dd7070Spatrick   RevertingTentativeParsingAction Tentative(*this);
74e5dd7070Spatrick 
75e5dd7070Spatrick   LambdaIntroducer Intro;
76e5dd7070Spatrick   LambdaIntroducerTentativeParse ParseResult;
77e5dd7070Spatrick   if (ParseLambdaIntroducer(Intro, &ParseResult)) {
78e5dd7070Spatrick     // Hit and diagnosed an error in a lambda.
79e5dd7070Spatrick     // FIXME: Tell the caller this happened so they can recover.
80e5dd7070Spatrick     return true;
81e5dd7070Spatrick   }
82e5dd7070Spatrick 
83e5dd7070Spatrick   switch (ParseResult) {
84e5dd7070Spatrick   case LambdaIntroducerTentativeParse::Success:
85e5dd7070Spatrick   case LambdaIntroducerTentativeParse::Incomplete:
86e5dd7070Spatrick     // Might be a lambda-expression. Keep looking.
87e5dd7070Spatrick     // FIXME: If our tentative parse was not incomplete, parse the lambda from
88e5dd7070Spatrick     // here rather than throwing away then reparsing the LambdaIntroducer.
89e5dd7070Spatrick     break;
90e5dd7070Spatrick 
91e5dd7070Spatrick   case LambdaIntroducerTentativeParse::MessageSend:
92e5dd7070Spatrick   case LambdaIntroducerTentativeParse::Invalid:
93e5dd7070Spatrick     // Can't be a lambda-expression. Treat it as a designator.
94e5dd7070Spatrick     // FIXME: Should we disambiguate against a message-send?
95e5dd7070Spatrick     return true;
96e5dd7070Spatrick   }
97e5dd7070Spatrick 
98e5dd7070Spatrick   // Once we hit the closing square bracket, we look at the next
99e5dd7070Spatrick   // token. If it's an '=', this is a designator. Otherwise, it's a
100e5dd7070Spatrick   // lambda expression. This decision favors lambdas over the older
101e5dd7070Spatrick   // GNU designator syntax, which allows one to omit the '=', but is
102e5dd7070Spatrick   // consistent with GCC.
103e5dd7070Spatrick   return Tok.is(tok::equal);
104e5dd7070Spatrick }
105e5dd7070Spatrick 
CheckArrayDesignatorSyntax(Parser & P,SourceLocation Loc,Designation & Desig)106e5dd7070Spatrick static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
107e5dd7070Spatrick                                        Designation &Desig) {
108e5dd7070Spatrick   // If we have exactly one array designator, this used the GNU
109e5dd7070Spatrick   // 'designation: array-designator' extension, otherwise there should be no
110e5dd7070Spatrick   // designators at all!
111e5dd7070Spatrick   if (Desig.getNumDesignators() == 1 &&
112e5dd7070Spatrick       (Desig.getDesignator(0).isArrayDesignator() ||
113e5dd7070Spatrick        Desig.getDesignator(0).isArrayRangeDesignator()))
114e5dd7070Spatrick     P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
115e5dd7070Spatrick   else if (Desig.getNumDesignators() > 0)
116e5dd7070Spatrick     P.Diag(Loc, diag::err_expected_equal_designator);
117e5dd7070Spatrick }
118e5dd7070Spatrick 
119e5dd7070Spatrick /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
120e5dd7070Spatrick /// checking to see if the token stream starts with a designator.
121e5dd7070Spatrick ///
122e5dd7070Spatrick /// C99:
123e5dd7070Spatrick ///
124e5dd7070Spatrick ///       designation:
125e5dd7070Spatrick ///         designator-list '='
126e5dd7070Spatrick /// [GNU]   array-designator
127e5dd7070Spatrick /// [GNU]   identifier ':'
128e5dd7070Spatrick ///
129e5dd7070Spatrick ///       designator-list:
130e5dd7070Spatrick ///         designator
131e5dd7070Spatrick ///         designator-list designator
132e5dd7070Spatrick ///
133e5dd7070Spatrick ///       designator:
134e5dd7070Spatrick ///         array-designator
135e5dd7070Spatrick ///         '.' identifier
136e5dd7070Spatrick ///
137e5dd7070Spatrick ///       array-designator:
138e5dd7070Spatrick ///         '[' constant-expression ']'
139e5dd7070Spatrick /// [GNU]   '[' constant-expression '...' constant-expression ']'
140e5dd7070Spatrick ///
141e5dd7070Spatrick /// C++20:
142e5dd7070Spatrick ///
143e5dd7070Spatrick ///       designated-initializer-list:
144e5dd7070Spatrick ///         designated-initializer-clause
145e5dd7070Spatrick ///         designated-initializer-list ',' designated-initializer-clause
146e5dd7070Spatrick ///
147e5dd7070Spatrick ///       designated-initializer-clause:
148e5dd7070Spatrick ///         designator brace-or-equal-initializer
149e5dd7070Spatrick ///
150e5dd7070Spatrick ///       designator:
151e5dd7070Spatrick ///         '.' identifier
152e5dd7070Spatrick ///
153e5dd7070Spatrick /// We allow the C99 syntax extensions in C++20, but do not allow the C++20
154e5dd7070Spatrick /// extension (a braced-init-list after the designator with no '=') in C99.
155e5dd7070Spatrick ///
156e5dd7070Spatrick /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
157e5dd7070Spatrick /// initializer (because it is an expression).  We need to consider this case
158e5dd7070Spatrick /// when parsing array designators.
159e5dd7070Spatrick ///
160ec727ea7Spatrick /// \p CodeCompleteCB is called with Designation parsed so far.
ParseInitializerWithPotentialDesignator(DesignatorCompletionInfo DesignatorCompletion)161ec727ea7Spatrick ExprResult Parser::ParseInitializerWithPotentialDesignator(
162a9ac8606Spatrick     DesignatorCompletionInfo DesignatorCompletion) {
163e5dd7070Spatrick   // If this is the old-style GNU extension:
164e5dd7070Spatrick   //   designation ::= identifier ':'
165e5dd7070Spatrick   // Handle it as a field designator.  Otherwise, this must be the start of a
166e5dd7070Spatrick   // normal expression.
167e5dd7070Spatrick   if (Tok.is(tok::identifier)) {
168e5dd7070Spatrick     const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
169e5dd7070Spatrick 
170e5dd7070Spatrick     SmallString<256> NewSyntax;
171e5dd7070Spatrick     llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
172e5dd7070Spatrick                                          << " = ";
173e5dd7070Spatrick 
174e5dd7070Spatrick     SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
175e5dd7070Spatrick 
176e5dd7070Spatrick     assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
177e5dd7070Spatrick     SourceLocation ColonLoc = ConsumeToken();
178e5dd7070Spatrick 
179e5dd7070Spatrick     Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
180e5dd7070Spatrick       << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
181e5dd7070Spatrick                                       NewSyntax);
182e5dd7070Spatrick 
183e5dd7070Spatrick     Designation D;
184e5dd7070Spatrick     D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
185a9ac8606Spatrick     PreferredType.enterDesignatedInitializer(
186a9ac8606Spatrick         Tok.getLocation(), DesignatorCompletion.PreferredBaseType, D);
187e5dd7070Spatrick     return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
188e5dd7070Spatrick                                               ParseInitializer());
189e5dd7070Spatrick   }
190e5dd7070Spatrick 
191e5dd7070Spatrick   // Desig - This is initialized when we see our first designator.  We may have
192e5dd7070Spatrick   // an objc message send with no designator, so we don't want to create this
193e5dd7070Spatrick   // eagerly.
194e5dd7070Spatrick   Designation Desig;
195e5dd7070Spatrick 
196e5dd7070Spatrick   // Parse each designator in the designator list until we find an initializer.
197e5dd7070Spatrick   while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
198e5dd7070Spatrick     if (Tok.is(tok::period)) {
199e5dd7070Spatrick       // designator: '.' identifier
200e5dd7070Spatrick       SourceLocation DotLoc = ConsumeToken();
201e5dd7070Spatrick 
202ec727ea7Spatrick       if (Tok.is(tok::code_completion)) {
203ec727ea7Spatrick         cutOffParsing();
204a9ac8606Spatrick         Actions.CodeCompleteDesignator(DesignatorCompletion.PreferredBaseType,
205a9ac8606Spatrick                                        DesignatorCompletion.InitExprs, Desig);
206ec727ea7Spatrick         return ExprError();
207ec727ea7Spatrick       }
208e5dd7070Spatrick       if (Tok.isNot(tok::identifier)) {
209e5dd7070Spatrick         Diag(Tok.getLocation(), diag::err_expected_field_designator);
210e5dd7070Spatrick         return ExprError();
211e5dd7070Spatrick       }
212e5dd7070Spatrick 
213e5dd7070Spatrick       Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
214e5dd7070Spatrick                                                Tok.getLocation()));
215e5dd7070Spatrick       ConsumeToken(); // Eat the identifier.
216e5dd7070Spatrick       continue;
217e5dd7070Spatrick     }
218e5dd7070Spatrick 
219e5dd7070Spatrick     // We must have either an array designator now or an objc message send.
220e5dd7070Spatrick     assert(Tok.is(tok::l_square) && "Unexpected token!");
221e5dd7070Spatrick 
222e5dd7070Spatrick     // Handle the two forms of array designator:
223e5dd7070Spatrick     //   array-designator: '[' constant-expression ']'
224e5dd7070Spatrick     //   array-designator: '[' constant-expression '...' constant-expression ']'
225e5dd7070Spatrick     //
226e5dd7070Spatrick     // Also, we have to handle the case where the expression after the
227e5dd7070Spatrick     // designator an an objc message send: '[' objc-message-expr ']'.
228e5dd7070Spatrick     // Interesting cases are:
229e5dd7070Spatrick     //   [foo bar]         -> objc message send
230e5dd7070Spatrick     //   [foo]             -> array designator
231e5dd7070Spatrick     //   [foo ... bar]     -> array designator
232e5dd7070Spatrick     //   [4][foo bar]      -> obsolete GNU designation with objc message send.
233e5dd7070Spatrick     //
234e5dd7070Spatrick     // We do not need to check for an expression starting with [[ here. If it
235e5dd7070Spatrick     // contains an Objective-C message send, then it is not an ill-formed
236e5dd7070Spatrick     // attribute. If it is a lambda-expression within an array-designator, then
237e5dd7070Spatrick     // it will be rejected because a constant-expression cannot begin with a
238e5dd7070Spatrick     // lambda-expression.
239e5dd7070Spatrick     InMessageExpressionRAIIObject InMessage(*this, true);
240e5dd7070Spatrick 
241e5dd7070Spatrick     BalancedDelimiterTracker T(*this, tok::l_square);
242e5dd7070Spatrick     T.consumeOpen();
243e5dd7070Spatrick     SourceLocation StartLoc = T.getOpenLocation();
244e5dd7070Spatrick 
245e5dd7070Spatrick     ExprResult Idx;
246e5dd7070Spatrick 
247e5dd7070Spatrick     // If Objective-C is enabled and this is a typename (class message
248e5dd7070Spatrick     // send) or send to 'super', parse this as a message send
249e5dd7070Spatrick     // expression.  We handle C++ and C separately, since C++ requires
250e5dd7070Spatrick     // much more complicated parsing.
251e5dd7070Spatrick     if  (getLangOpts().ObjC && getLangOpts().CPlusPlus) {
252e5dd7070Spatrick       // Send to 'super'.
253e5dd7070Spatrick       if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
254e5dd7070Spatrick           NextToken().isNot(tok::period) &&
255e5dd7070Spatrick           getCurScope()->isInObjcMethodScope()) {
256e5dd7070Spatrick         CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
257e5dd7070Spatrick         return ParseAssignmentExprWithObjCMessageExprStart(
258e5dd7070Spatrick             StartLoc, ConsumeToken(), nullptr, nullptr);
259e5dd7070Spatrick       }
260e5dd7070Spatrick 
261e5dd7070Spatrick       // Parse the receiver, which is either a type or an expression.
262e5dd7070Spatrick       bool IsExpr;
263e5dd7070Spatrick       void *TypeOrExpr;
264e5dd7070Spatrick       if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
265e5dd7070Spatrick         SkipUntil(tok::r_square, StopAtSemi);
266e5dd7070Spatrick         return ExprError();
267e5dd7070Spatrick       }
268e5dd7070Spatrick 
269e5dd7070Spatrick       // If the receiver was a type, we have a class message; parse
270e5dd7070Spatrick       // the rest of it.
271e5dd7070Spatrick       if (!IsExpr) {
272e5dd7070Spatrick         CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
273e5dd7070Spatrick         return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
274e5dd7070Spatrick                                                            SourceLocation(),
275e5dd7070Spatrick                                    ParsedType::getFromOpaquePtr(TypeOrExpr),
276e5dd7070Spatrick                                                            nullptr);
277e5dd7070Spatrick       }
278e5dd7070Spatrick 
279e5dd7070Spatrick       // If the receiver was an expression, we still don't know
280e5dd7070Spatrick       // whether we have a message send or an array designator; just
281e5dd7070Spatrick       // adopt the expression for further analysis below.
282e5dd7070Spatrick       // FIXME: potentially-potentially evaluated expression above?
283e5dd7070Spatrick       Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
284e5dd7070Spatrick     } else if (getLangOpts().ObjC && Tok.is(tok::identifier)) {
285e5dd7070Spatrick       IdentifierInfo *II = Tok.getIdentifierInfo();
286e5dd7070Spatrick       SourceLocation IILoc = Tok.getLocation();
287e5dd7070Spatrick       ParsedType ReceiverType;
288e5dd7070Spatrick       // Three cases. This is a message send to a type: [type foo]
289e5dd7070Spatrick       // This is a message send to super:  [super foo]
290e5dd7070Spatrick       // This is a message sent to an expr:  [super.bar foo]
291e5dd7070Spatrick       switch (Actions.getObjCMessageKind(
292e5dd7070Spatrick           getCurScope(), II, IILoc, II == Ident_super,
293e5dd7070Spatrick           NextToken().is(tok::period), ReceiverType)) {
294e5dd7070Spatrick       case Sema::ObjCSuperMessage:
295e5dd7070Spatrick         CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
296e5dd7070Spatrick         return ParseAssignmentExprWithObjCMessageExprStart(
297e5dd7070Spatrick             StartLoc, ConsumeToken(), nullptr, nullptr);
298e5dd7070Spatrick 
299e5dd7070Spatrick       case Sema::ObjCClassMessage:
300e5dd7070Spatrick         CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
301e5dd7070Spatrick         ConsumeToken(); // the identifier
302e5dd7070Spatrick         if (!ReceiverType) {
303e5dd7070Spatrick           SkipUntil(tok::r_square, StopAtSemi);
304e5dd7070Spatrick           return ExprError();
305e5dd7070Spatrick         }
306e5dd7070Spatrick 
307e5dd7070Spatrick         // Parse type arguments and protocol qualifiers.
308e5dd7070Spatrick         if (Tok.is(tok::less)) {
309e5dd7070Spatrick           SourceLocation NewEndLoc;
310e5dd7070Spatrick           TypeResult NewReceiverType
311e5dd7070Spatrick             = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType,
312e5dd7070Spatrick                                                      /*consumeLastToken=*/true,
313e5dd7070Spatrick                                                      NewEndLoc);
314e5dd7070Spatrick           if (!NewReceiverType.isUsable()) {
315e5dd7070Spatrick             SkipUntil(tok::r_square, StopAtSemi);
316e5dd7070Spatrick             return ExprError();
317e5dd7070Spatrick           }
318e5dd7070Spatrick 
319e5dd7070Spatrick           ReceiverType = NewReceiverType.get();
320e5dd7070Spatrick         }
321e5dd7070Spatrick 
322e5dd7070Spatrick         return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
323e5dd7070Spatrick                                                            SourceLocation(),
324e5dd7070Spatrick                                                            ReceiverType,
325e5dd7070Spatrick                                                            nullptr);
326e5dd7070Spatrick 
327e5dd7070Spatrick       case Sema::ObjCInstanceMessage:
328e5dd7070Spatrick         // Fall through; we'll just parse the expression and
329e5dd7070Spatrick         // (possibly) treat this like an Objective-C message send
330e5dd7070Spatrick         // later.
331e5dd7070Spatrick         break;
332e5dd7070Spatrick       }
333e5dd7070Spatrick     }
334e5dd7070Spatrick 
335e5dd7070Spatrick     // Parse the index expression, if we haven't already gotten one
336e5dd7070Spatrick     // above (which can only happen in Objective-C++).
337e5dd7070Spatrick     // Note that we parse this as an assignment expression, not a constant
338e5dd7070Spatrick     // expression (allowing *=, =, etc) to handle the objc case.  Sema needs
339e5dd7070Spatrick     // to validate that the expression is a constant.
340e5dd7070Spatrick     // FIXME: We also need to tell Sema that we're in a
341e5dd7070Spatrick     // potentially-potentially evaluated context.
342e5dd7070Spatrick     if (!Idx.get()) {
343e5dd7070Spatrick       Idx = ParseAssignmentExpression();
344e5dd7070Spatrick       if (Idx.isInvalid()) {
345e5dd7070Spatrick         SkipUntil(tok::r_square, StopAtSemi);
346e5dd7070Spatrick         return Idx;
347e5dd7070Spatrick       }
348e5dd7070Spatrick     }
349e5dd7070Spatrick 
350e5dd7070Spatrick     // Given an expression, we could either have a designator (if the next
351e5dd7070Spatrick     // tokens are '...' or ']' or an objc message send.  If this is an objc
352e5dd7070Spatrick     // message send, handle it now.  An objc-message send is the start of
353e5dd7070Spatrick     // an assignment-expression production.
354e5dd7070Spatrick     if (getLangOpts().ObjC && Tok.isNot(tok::ellipsis) &&
355e5dd7070Spatrick         Tok.isNot(tok::r_square)) {
356e5dd7070Spatrick       CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
357e5dd7070Spatrick       return ParseAssignmentExprWithObjCMessageExprStart(
358e5dd7070Spatrick           StartLoc, SourceLocation(), nullptr, Idx.get());
359e5dd7070Spatrick     }
360e5dd7070Spatrick 
361e5dd7070Spatrick     // If this is a normal array designator, remember it.
362e5dd7070Spatrick     if (Tok.isNot(tok::ellipsis)) {
363e5dd7070Spatrick       Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc));
364e5dd7070Spatrick     } else {
365e5dd7070Spatrick       // Handle the gnu array range extension.
366e5dd7070Spatrick       Diag(Tok, diag::ext_gnu_array_range);
367e5dd7070Spatrick       SourceLocation EllipsisLoc = ConsumeToken();
368e5dd7070Spatrick 
369e5dd7070Spatrick       ExprResult RHS(ParseConstantExpression());
370e5dd7070Spatrick       if (RHS.isInvalid()) {
371e5dd7070Spatrick         SkipUntil(tok::r_square, StopAtSemi);
372e5dd7070Spatrick         return RHS;
373e5dd7070Spatrick       }
374e5dd7070Spatrick       Desig.AddDesignator(Designator::getArrayRange(Idx.get(),
375e5dd7070Spatrick                                                     RHS.get(),
376e5dd7070Spatrick                                                     StartLoc, EllipsisLoc));
377e5dd7070Spatrick     }
378e5dd7070Spatrick 
379e5dd7070Spatrick     T.consumeClose();
380e5dd7070Spatrick     Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
381e5dd7070Spatrick                                                         T.getCloseLocation());
382e5dd7070Spatrick   }
383e5dd7070Spatrick 
384e5dd7070Spatrick   // Okay, we're done with the designator sequence.  We know that there must be
385e5dd7070Spatrick   // at least one designator, because the only case we can get into this method
386e5dd7070Spatrick   // without a designator is when we have an objc message send.  That case is
387e5dd7070Spatrick   // handled and returned from above.
388e5dd7070Spatrick   assert(!Desig.empty() && "Designator is empty?");
389e5dd7070Spatrick 
390e5dd7070Spatrick   // Handle a normal designator sequence end, which is an equal.
391e5dd7070Spatrick   if (Tok.is(tok::equal)) {
392e5dd7070Spatrick     SourceLocation EqualLoc = ConsumeToken();
393a9ac8606Spatrick     PreferredType.enterDesignatedInitializer(
394a9ac8606Spatrick         Tok.getLocation(), DesignatorCompletion.PreferredBaseType, Desig);
395e5dd7070Spatrick     return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
396e5dd7070Spatrick                                               ParseInitializer());
397e5dd7070Spatrick   }
398e5dd7070Spatrick 
399e5dd7070Spatrick   // Handle a C++20 braced designated initialization, which results in
400e5dd7070Spatrick   // direct-list-initialization of the aggregate element. We allow this as an
401e5dd7070Spatrick   // extension from C++11 onwards (when direct-list-initialization was added).
402e5dd7070Spatrick   if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
403a9ac8606Spatrick     PreferredType.enterDesignatedInitializer(
404a9ac8606Spatrick         Tok.getLocation(), DesignatorCompletion.PreferredBaseType, Desig);
405e5dd7070Spatrick     return Actions.ActOnDesignatedInitializer(Desig, SourceLocation(), false,
406e5dd7070Spatrick                                               ParseBraceInitializer());
407e5dd7070Spatrick   }
408e5dd7070Spatrick 
409e5dd7070Spatrick   // We read some number of designators and found something that isn't an = or
410e5dd7070Spatrick   // an initializer.  If we have exactly one array designator, this
411e5dd7070Spatrick   // is the GNU 'designation: array-designator' extension.  Otherwise, it is a
412e5dd7070Spatrick   // parse error.
413e5dd7070Spatrick   if (Desig.getNumDesignators() == 1 &&
414e5dd7070Spatrick       (Desig.getDesignator(0).isArrayDesignator() ||
415e5dd7070Spatrick        Desig.getDesignator(0).isArrayRangeDesignator())) {
416e5dd7070Spatrick     Diag(Tok, diag::ext_gnu_missing_equal_designator)
417e5dd7070Spatrick       << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
418e5dd7070Spatrick     return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
419e5dd7070Spatrick                                               true, ParseInitializer());
420e5dd7070Spatrick   }
421e5dd7070Spatrick 
422e5dd7070Spatrick   Diag(Tok, diag::err_expected_equal_designator);
423e5dd7070Spatrick   return ExprError();
424e5dd7070Spatrick }
425e5dd7070Spatrick 
426e5dd7070Spatrick /// ParseBraceInitializer - Called when parsing an initializer that has a
427e5dd7070Spatrick /// leading open brace.
428e5dd7070Spatrick ///
429e5dd7070Spatrick ///       initializer: [C99 6.7.8]
430e5dd7070Spatrick ///         '{' initializer-list '}'
431e5dd7070Spatrick ///         '{' initializer-list ',' '}'
432e5dd7070Spatrick /// [GNU]   '{' '}'
433e5dd7070Spatrick ///
434e5dd7070Spatrick ///       initializer-list:
435e5dd7070Spatrick ///         designation[opt] initializer ...[opt]
436e5dd7070Spatrick ///         initializer-list ',' designation[opt] initializer ...[opt]
437e5dd7070Spatrick ///
ParseBraceInitializer()438e5dd7070Spatrick ExprResult Parser::ParseBraceInitializer() {
439e5dd7070Spatrick   InMessageExpressionRAIIObject InMessage(*this, false);
440e5dd7070Spatrick 
441e5dd7070Spatrick   BalancedDelimiterTracker T(*this, tok::l_brace);
442e5dd7070Spatrick   T.consumeOpen();
443e5dd7070Spatrick   SourceLocation LBraceLoc = T.getOpenLocation();
444e5dd7070Spatrick 
445e5dd7070Spatrick   /// InitExprs - This is the actual list of expressions contained in the
446e5dd7070Spatrick   /// initializer.
447e5dd7070Spatrick   ExprVector InitExprs;
448e5dd7070Spatrick 
449e5dd7070Spatrick   if (Tok.is(tok::r_brace)) {
450e5dd7070Spatrick     // Empty initializers are a C++ feature and a GNU extension to C.
451e5dd7070Spatrick     if (!getLangOpts().CPlusPlus)
452e5dd7070Spatrick       Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
453e5dd7070Spatrick     // Match the '}'.
454*12c85518Srobert     return Actions.ActOnInitList(LBraceLoc, std::nullopt, ConsumeBrace());
455e5dd7070Spatrick   }
456e5dd7070Spatrick 
457e5dd7070Spatrick   // Enter an appropriate expression evaluation context for an initializer list.
458e5dd7070Spatrick   EnterExpressionEvaluationContext EnterContext(
459e5dd7070Spatrick       Actions, EnterExpressionEvaluationContext::InitList);
460e5dd7070Spatrick 
461e5dd7070Spatrick   bool InitExprsOk = true;
462*12c85518Srobert   QualType LikelyType = PreferredType.get(T.getOpenLocation());
463*12c85518Srobert   DesignatorCompletionInfo DesignatorCompletion{InitExprs, LikelyType};
464*12c85518Srobert   bool CalledSignatureHelp = false;
465*12c85518Srobert   auto RunSignatureHelp = [&] {
466*12c85518Srobert     QualType PreferredType;
467*12c85518Srobert     if (!LikelyType.isNull())
468*12c85518Srobert       PreferredType = Actions.ProduceConstructorSignatureHelp(
469*12c85518Srobert           LikelyType->getCanonicalTypeInternal(), T.getOpenLocation(),
470*12c85518Srobert           InitExprs, T.getOpenLocation(), /*Braced=*/true);
471*12c85518Srobert     CalledSignatureHelp = true;
472*12c85518Srobert     return PreferredType;
473ec727ea7Spatrick   };
474e5dd7070Spatrick 
475*12c85518Srobert   while (true) {
476*12c85518Srobert     PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp);
477*12c85518Srobert 
478e5dd7070Spatrick     // Handle Microsoft __if_exists/if_not_exists if necessary.
479e5dd7070Spatrick     if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
480e5dd7070Spatrick         Tok.is(tok::kw___if_not_exists))) {
481e5dd7070Spatrick       if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
482e5dd7070Spatrick         if (Tok.isNot(tok::comma)) break;
483e5dd7070Spatrick         ConsumeToken();
484e5dd7070Spatrick       }
485e5dd7070Spatrick       if (Tok.is(tok::r_brace)) break;
486e5dd7070Spatrick       continue;
487e5dd7070Spatrick     }
488e5dd7070Spatrick 
489e5dd7070Spatrick     // Parse: designation[opt] initializer
490e5dd7070Spatrick 
491e5dd7070Spatrick     // If we know that this cannot be a designation, just parse the nested
492e5dd7070Spatrick     // initializer directly.
493e5dd7070Spatrick     ExprResult SubElt;
494e5dd7070Spatrick     if (MayBeDesignationStart())
495a9ac8606Spatrick       SubElt = ParseInitializerWithPotentialDesignator(DesignatorCompletion);
496e5dd7070Spatrick     else
497e5dd7070Spatrick       SubElt = ParseInitializer();
498e5dd7070Spatrick 
499e5dd7070Spatrick     if (Tok.is(tok::ellipsis))
500e5dd7070Spatrick       SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
501e5dd7070Spatrick 
502e5dd7070Spatrick     SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get());
503e5dd7070Spatrick 
504e5dd7070Spatrick     // If we couldn't parse the subelement, bail out.
505e5dd7070Spatrick     if (SubElt.isUsable()) {
506e5dd7070Spatrick       InitExprs.push_back(SubElt.get());
507e5dd7070Spatrick     } else {
508e5dd7070Spatrick       InitExprsOk = false;
509e5dd7070Spatrick 
510e5dd7070Spatrick       // We have two ways to try to recover from this error: if the code looks
511e5dd7070Spatrick       // grammatically ok (i.e. we have a comma coming up) try to continue
512e5dd7070Spatrick       // parsing the rest of the initializer.  This allows us to emit
513e5dd7070Spatrick       // diagnostics for later elements that we find.  If we don't see a comma,
514e5dd7070Spatrick       // assume there is a parse error, and just skip to recover.
515e5dd7070Spatrick       // FIXME: This comment doesn't sound right. If there is a r_brace
516e5dd7070Spatrick       // immediately, it can't be an error, since there is no other way of
517e5dd7070Spatrick       // leaving this loop except through this if.
518e5dd7070Spatrick       if (Tok.isNot(tok::comma)) {
519e5dd7070Spatrick         SkipUntil(tok::r_brace, StopBeforeMatch);
520e5dd7070Spatrick         break;
521e5dd7070Spatrick       }
522e5dd7070Spatrick     }
523e5dd7070Spatrick 
524e5dd7070Spatrick     // If we don't have a comma continued list, we're done.
525e5dd7070Spatrick     if (Tok.isNot(tok::comma)) break;
526e5dd7070Spatrick 
527e5dd7070Spatrick     // TODO: save comma locations if some client cares.
528e5dd7070Spatrick     ConsumeToken();
529e5dd7070Spatrick 
530e5dd7070Spatrick     // Handle trailing comma.
531e5dd7070Spatrick     if (Tok.is(tok::r_brace)) break;
532e5dd7070Spatrick   }
533e5dd7070Spatrick 
534e5dd7070Spatrick   bool closed = !T.consumeClose();
535e5dd7070Spatrick 
536e5dd7070Spatrick   if (InitExprsOk && closed)
537e5dd7070Spatrick     return Actions.ActOnInitList(LBraceLoc, InitExprs,
538e5dd7070Spatrick                                  T.getCloseLocation());
539e5dd7070Spatrick 
540e5dd7070Spatrick   return ExprError(); // an error occurred.
541e5dd7070Spatrick }
542e5dd7070Spatrick 
543e5dd7070Spatrick 
544e5dd7070Spatrick // Return true if a comma (or closing brace) is necessary after the
545e5dd7070Spatrick // __if_exists/if_not_exists statement.
ParseMicrosoftIfExistsBraceInitializer(ExprVector & InitExprs,bool & InitExprsOk)546e5dd7070Spatrick bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
547e5dd7070Spatrick                                                     bool &InitExprsOk) {
548e5dd7070Spatrick   bool trailingComma = false;
549e5dd7070Spatrick   IfExistsCondition Result;
550e5dd7070Spatrick   if (ParseMicrosoftIfExistsCondition(Result))
551e5dd7070Spatrick     return false;
552e5dd7070Spatrick 
553e5dd7070Spatrick   BalancedDelimiterTracker Braces(*this, tok::l_brace);
554e5dd7070Spatrick   if (Braces.consumeOpen()) {
555e5dd7070Spatrick     Diag(Tok, diag::err_expected) << tok::l_brace;
556e5dd7070Spatrick     return false;
557e5dd7070Spatrick   }
558e5dd7070Spatrick 
559e5dd7070Spatrick   switch (Result.Behavior) {
560e5dd7070Spatrick   case IEB_Parse:
561e5dd7070Spatrick     // Parse the declarations below.
562e5dd7070Spatrick     break;
563e5dd7070Spatrick 
564e5dd7070Spatrick   case IEB_Dependent:
565e5dd7070Spatrick     Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
566e5dd7070Spatrick       << Result.IsIfExists;
567e5dd7070Spatrick     // Fall through to skip.
568*12c85518Srobert     [[fallthrough]];
569e5dd7070Spatrick 
570e5dd7070Spatrick   case IEB_Skip:
571e5dd7070Spatrick     Braces.skipToEnd();
572e5dd7070Spatrick     return false;
573e5dd7070Spatrick   }
574e5dd7070Spatrick 
575a9ac8606Spatrick   DesignatorCompletionInfo DesignatorCompletion{
576a9ac8606Spatrick       InitExprs,
577a9ac8606Spatrick       PreferredType.get(Braces.getOpenLocation()),
578ec727ea7Spatrick   };
579e5dd7070Spatrick   while (!isEofOrEom()) {
580e5dd7070Spatrick     trailingComma = false;
581e5dd7070Spatrick     // If we know that this cannot be a designation, just parse the nested
582e5dd7070Spatrick     // initializer directly.
583e5dd7070Spatrick     ExprResult SubElt;
584e5dd7070Spatrick     if (MayBeDesignationStart())
585a9ac8606Spatrick       SubElt = ParseInitializerWithPotentialDesignator(DesignatorCompletion);
586e5dd7070Spatrick     else
587e5dd7070Spatrick       SubElt = ParseInitializer();
588e5dd7070Spatrick 
589e5dd7070Spatrick     if (Tok.is(tok::ellipsis))
590e5dd7070Spatrick       SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
591e5dd7070Spatrick 
592e5dd7070Spatrick     // If we couldn't parse the subelement, bail out.
593e5dd7070Spatrick     if (!SubElt.isInvalid())
594e5dd7070Spatrick       InitExprs.push_back(SubElt.get());
595e5dd7070Spatrick     else
596e5dd7070Spatrick       InitExprsOk = false;
597e5dd7070Spatrick 
598e5dd7070Spatrick     if (Tok.is(tok::comma)) {
599e5dd7070Spatrick       ConsumeToken();
600e5dd7070Spatrick       trailingComma = true;
601e5dd7070Spatrick     }
602e5dd7070Spatrick 
603e5dd7070Spatrick     if (Tok.is(tok::r_brace))
604e5dd7070Spatrick       break;
605e5dd7070Spatrick   }
606e5dd7070Spatrick 
607e5dd7070Spatrick   Braces.consumeClose();
608e5dd7070Spatrick 
609e5dd7070Spatrick   return !trailingComma;
610e5dd7070Spatrick }
611