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