/src/llvm-project/clang/lib/Parse/ParseInit.cpp
Line | Count | Source (jump to first uncovered line) |
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/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; |
24 | | |
25 | | |
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, |
28 | | /// return false. |
29 | 126 | bool Parser::MayBeDesignationStart() { |
30 | 126 | switch (Tok.getKind()) { |
31 | 50 | default: |
32 | 50 | return false; |
33 | | |
34 | 0 | case tok::period: // designator: '.' identifier |
35 | 0 | return true; |
36 | | |
37 | 0 | case tok::l_square: { // designator: array-designator |
38 | 0 | if (!PP.getLangOpts().CPlusPlus11) |
39 | 0 | return true; |
40 | | |
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 | 0 | switch (PP.LookAhead(0).getKind()) { |
45 | 0 | case tok::equal: |
46 | 0 | case tok::ellipsis: |
47 | 0 | case tok::r_square: |
48 | | // Definitely starts a lambda expression. |
49 | 0 | return false; |
50 | | |
51 | 0 | case tok::amp: |
52 | 0 | case tok::kw_this: |
53 | 0 | case tok::star: |
54 | 0 | case tok::identifier: |
55 | | // We have to do additional analysis, because these could be the |
56 | | // start of a constant expression or a lambda capture list. |
57 | 0 | break; |
58 | | |
59 | 0 | default: |
60 | | // Anything not mentioned above cannot occur following a '[' in a |
61 | | // lambda expression. |
62 | 0 | return true; |
63 | 0 | } |
64 | | |
65 | | // Handle the complicated case below. |
66 | 0 | break; |
67 | 0 | } |
68 | 76 | case tok::identifier: // designation: identifier ':' |
69 | 76 | return PP.LookAhead(0).is(tok::colon); |
70 | 126 | } |
71 | | |
72 | | // Parse up to (at most) the token after the closing ']' to determine |
73 | | // whether this is a C99 designator or a lambda. |
74 | 0 | RevertingTentativeParsingAction Tentative(*this); |
75 | |
|
76 | 0 | LambdaIntroducer Intro; |
77 | 0 | LambdaIntroducerTentativeParse ParseResult; |
78 | 0 | if (ParseLambdaIntroducer(Intro, &ParseResult)) { |
79 | | // Hit and diagnosed an error in a lambda. |
80 | | // FIXME: Tell the caller this happened so they can recover. |
81 | 0 | return true; |
82 | 0 | } |
83 | | |
84 | 0 | switch (ParseResult) { |
85 | 0 | case LambdaIntroducerTentativeParse::Success: |
86 | 0 | 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. |
90 | 0 | break; |
91 | | |
92 | 0 | case LambdaIntroducerTentativeParse::MessageSend: |
93 | 0 | case LambdaIntroducerTentativeParse::Invalid: |
94 | | // Can't be a lambda-expression. Treat it as a designator. |
95 | | // FIXME: Should we disambiguate against a message-send? |
96 | 0 | return true; |
97 | 0 | } |
98 | | |
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 | 0 | return Tok.is(tok::equal); |
105 | 0 | } |
106 | | |
107 | | static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc, |
108 | 0 | 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 | 0 | if (Desig.getNumDesignators() == 1 && |
113 | 0 | (Desig.getDesignator(0).isArrayDesignator() || |
114 | 0 | Desig.getDesignator(0).isArrayRangeDesignator())) |
115 | 0 | P.Diag(Loc, diag::ext_gnu_missing_equal_designator); |
116 | 0 | else if (Desig.getNumDesignators() > 0) |
117 | 0 | P.Diag(Loc, diag::err_expected_equal_designator); |
118 | 0 | } |
119 | | |
120 | | /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production |
121 | | /// checking to see if the token stream starts with a designator. |
122 | | /// |
123 | | /// C99: |
124 | | /// |
125 | | /// designation: |
126 | | /// designator-list '=' |
127 | | /// [GNU] array-designator |
128 | | /// [GNU] identifier ':' |
129 | | /// |
130 | | /// designator-list: |
131 | | /// designator |
132 | | /// designator-list designator |
133 | | /// |
134 | | /// designator: |
135 | | /// array-designator |
136 | | /// '.' identifier |
137 | | /// |
138 | | /// array-designator: |
139 | | /// '[' constant-expression ']' |
140 | | /// [GNU] '[' constant-expression '...' constant-expression ']' |
141 | | /// |
142 | | /// C++20: |
143 | | /// |
144 | | /// designated-initializer-list: |
145 | | /// designated-initializer-clause |
146 | | /// designated-initializer-list ',' designated-initializer-clause |
147 | | /// |
148 | | /// designated-initializer-clause: |
149 | | /// designator brace-or-equal-initializer |
150 | | /// |
151 | | /// designator: |
152 | | /// '.' identifier |
153 | | /// |
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. |
156 | | /// |
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. |
160 | | /// |
161 | | /// \p CodeCompleteCB is called with Designation parsed so far. |
162 | | ExprResult Parser::ParseInitializerWithPotentialDesignator( |
163 | 1 | 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 | 1 | if (Tok.is(tok::identifier)) { |
169 | 1 | const IdentifierInfo *FieldName = Tok.getIdentifierInfo(); |
170 | | |
171 | 1 | SmallString<256> NewSyntax; |
172 | 1 | llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName() |
173 | 1 | << " = "; |
174 | | |
175 | 1 | SourceLocation NameLoc = ConsumeToken(); // Eat the identifier. |
176 | | |
177 | 1 | assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!"); |
178 | 0 | SourceLocation ColonLoc = ConsumeToken(); |
179 | | |
180 | 1 | Diag(NameLoc, diag::ext_gnu_old_style_field_designator) |
181 | 1 | << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc), |
182 | 1 | NewSyntax); |
183 | | |
184 | 1 | Designation D; |
185 | 1 | D.AddDesignator(Designator::CreateFieldDesignator( |
186 | 1 | FieldName, SourceLocation(), NameLoc)); |
187 | 1 | PreferredType.enterDesignatedInitializer( |
188 | 1 | Tok.getLocation(), DesignatorCompletion.PreferredBaseType, D); |
189 | 1 | return Actions.ActOnDesignatedInitializer(D, ColonLoc, true, |
190 | 1 | ParseInitializer()); |
191 | 1 | } |
192 | | |
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 |
195 | | // eagerly. |
196 | 0 | Designation Desig; |
197 | | |
198 | | // Parse each designator in the designator list until we find an initializer. |
199 | 0 | while (Tok.is(tok::period) || Tok.is(tok::l_square)) { |
200 | 0 | if (Tok.is(tok::period)) { |
201 | | // designator: '.' identifier |
202 | 0 | SourceLocation DotLoc = ConsumeToken(); |
203 | |
|
204 | 0 | if (Tok.is(tok::code_completion)) { |
205 | 0 | cutOffParsing(); |
206 | 0 | Actions.CodeCompleteDesignator(DesignatorCompletion.PreferredBaseType, |
207 | 0 | DesignatorCompletion.InitExprs, Desig); |
208 | 0 | return ExprError(); |
209 | 0 | } |
210 | 0 | if (Tok.isNot(tok::identifier)) { |
211 | 0 | Diag(Tok.getLocation(), diag::err_expected_field_designator); |
212 | 0 | return ExprError(); |
213 | 0 | } |
214 | | |
215 | 0 | Desig.AddDesignator(Designator::CreateFieldDesignator( |
216 | 0 | Tok.getIdentifierInfo(), DotLoc, Tok.getLocation())); |
217 | 0 | ConsumeToken(); // Eat the identifier. |
218 | 0 | continue; |
219 | 0 | } |
220 | | |
221 | | // We must have either an array designator now or an objc message send. |
222 | 0 | assert(Tok.is(tok::l_square) && "Unexpected token!"); |
223 | | |
224 | | // Handle the two forms of array designator: |
225 | | // array-designator: '[' constant-expression ']' |
226 | | // array-designator: '[' constant-expression '...' constant-expression ']' |
227 | | // |
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. |
235 | | // |
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 | 0 | InMessageExpressionRAIIObject InMessage(*this, true); |
242 | |
|
243 | 0 | BalancedDelimiterTracker T(*this, tok::l_square); |
244 | 0 | T.consumeOpen(); |
245 | 0 | SourceLocation StartLoc = T.getOpenLocation(); |
246 | |
|
247 | 0 | ExprResult Idx; |
248 | | |
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 | 0 | if (getLangOpts().ObjC && getLangOpts().CPlusPlus) { |
254 | | // Send to 'super'. |
255 | 0 | if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super && |
256 | 0 | NextToken().isNot(tok::period) && |
257 | 0 | getCurScope()->isInObjcMethodScope()) { |
258 | 0 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
259 | 0 | return ParseAssignmentExprWithObjCMessageExprStart( |
260 | 0 | StartLoc, ConsumeToken(), nullptr, nullptr); |
261 | 0 | } |
262 | | |
263 | | // Parse the receiver, which is either a type or an expression. |
264 | 0 | bool IsExpr; |
265 | 0 | void *TypeOrExpr; |
266 | 0 | if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) { |
267 | 0 | SkipUntil(tok::r_square, StopAtSemi); |
268 | 0 | return ExprError(); |
269 | 0 | } |
270 | | |
271 | | // If the receiver was a type, we have a class message; parse |
272 | | // the rest of it. |
273 | 0 | if (!IsExpr) { |
274 | 0 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
275 | 0 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
276 | 0 | SourceLocation(), |
277 | 0 | ParsedType::getFromOpaquePtr(TypeOrExpr), |
278 | 0 | nullptr); |
279 | 0 | } |
280 | | |
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 | 0 | Idx = ExprResult(static_cast<Expr*>(TypeOrExpr)); |
286 | 0 | } else if (getLangOpts().ObjC && Tok.is(tok::identifier)) { |
287 | 0 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
288 | 0 | SourceLocation IILoc = Tok.getLocation(); |
289 | 0 | 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 | 0 | switch (Actions.getObjCMessageKind( |
294 | 0 | getCurScope(), II, IILoc, II == Ident_super, |
295 | 0 | NextToken().is(tok::period), ReceiverType)) { |
296 | 0 | case Sema::ObjCSuperMessage: |
297 | 0 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
298 | 0 | return ParseAssignmentExprWithObjCMessageExprStart( |
299 | 0 | StartLoc, ConsumeToken(), nullptr, nullptr); |
300 | | |
301 | 0 | case Sema::ObjCClassMessage: |
302 | 0 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
303 | 0 | ConsumeToken(); // the identifier |
304 | 0 | if (!ReceiverType) { |
305 | 0 | SkipUntil(tok::r_square, StopAtSemi); |
306 | 0 | return ExprError(); |
307 | 0 | } |
308 | | |
309 | | // Parse type arguments and protocol qualifiers. |
310 | 0 | if (Tok.is(tok::less)) { |
311 | 0 | SourceLocation NewEndLoc; |
312 | 0 | TypeResult NewReceiverType |
313 | 0 | = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType, |
314 | 0 | /*consumeLastToken=*/true, |
315 | 0 | NewEndLoc); |
316 | 0 | if (!NewReceiverType.isUsable()) { |
317 | 0 | SkipUntil(tok::r_square, StopAtSemi); |
318 | 0 | return ExprError(); |
319 | 0 | } |
320 | | |
321 | 0 | ReceiverType = NewReceiverType.get(); |
322 | 0 | } |
323 | | |
324 | 0 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
325 | 0 | SourceLocation(), |
326 | 0 | ReceiverType, |
327 | 0 | nullptr); |
328 | | |
329 | 0 | case Sema::ObjCInstanceMessage: |
330 | | // Fall through; we'll just parse the expression and |
331 | | // (possibly) treat this like an Objective-C message send |
332 | | // later. |
333 | 0 | break; |
334 | 0 | } |
335 | 0 | } |
336 | | |
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. |
344 | 0 | if (!Idx.get()) { |
345 | 0 | Idx = ParseAssignmentExpression(); |
346 | 0 | if (Idx.isInvalid()) { |
347 | 0 | SkipUntil(tok::r_square, StopAtSemi); |
348 | 0 | return Idx; |
349 | 0 | } |
350 | 0 | } |
351 | | |
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 | 0 | if (getLangOpts().ObjC && Tok.isNot(tok::ellipsis) && |
357 | 0 | Tok.isNot(tok::r_square)) { |
358 | 0 | CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig); |
359 | 0 | return ParseAssignmentExprWithObjCMessageExprStart( |
360 | 0 | StartLoc, SourceLocation(), nullptr, Idx.get()); |
361 | 0 | } |
362 | | |
363 | | // If this is a normal array designator, remember it. |
364 | 0 | if (Tok.isNot(tok::ellipsis)) { |
365 | 0 | Desig.AddDesignator(Designator::CreateArrayDesignator(Idx.get(), |
366 | 0 | StartLoc)); |
367 | 0 | } else { |
368 | | // Handle the gnu array range extension. |
369 | 0 | Diag(Tok, diag::ext_gnu_array_range); |
370 | 0 | SourceLocation EllipsisLoc = ConsumeToken(); |
371 | |
|
372 | 0 | ExprResult RHS(ParseConstantExpression()); |
373 | 0 | if (RHS.isInvalid()) { |
374 | 0 | SkipUntil(tok::r_square, StopAtSemi); |
375 | 0 | return RHS; |
376 | 0 | } |
377 | 0 | Desig.AddDesignator(Designator::CreateArrayRangeDesignator( |
378 | 0 | Idx.get(), RHS.get(), StartLoc, EllipsisLoc)); |
379 | 0 | } |
380 | | |
381 | 0 | T.consumeClose(); |
382 | 0 | Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc( |
383 | 0 | T.getCloseLocation()); |
384 | 0 | } |
385 | | |
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 | 0 | assert(!Desig.empty() && "Designator is empty?"); |
391 | | |
392 | | // Handle a normal designator sequence end, which is an equal. |
393 | 0 | if (Tok.is(tok::equal)) { |
394 | 0 | SourceLocation EqualLoc = ConsumeToken(); |
395 | 0 | PreferredType.enterDesignatedInitializer( |
396 | 0 | Tok.getLocation(), DesignatorCompletion.PreferredBaseType, Desig); |
397 | 0 | return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false, |
398 | 0 | ParseInitializer()); |
399 | 0 | } |
400 | | |
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 | 0 | if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) { |
405 | 0 | PreferredType.enterDesignatedInitializer( |
406 | 0 | Tok.getLocation(), DesignatorCompletion.PreferredBaseType, Desig); |
407 | 0 | return Actions.ActOnDesignatedInitializer(Desig, SourceLocation(), false, |
408 | 0 | ParseBraceInitializer()); |
409 | 0 | } |
410 | | |
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 |
414 | | // parse error. |
415 | 0 | if (Desig.getNumDesignators() == 1 && |
416 | 0 | (Desig.getDesignator(0).isArrayDesignator() || |
417 | 0 | Desig.getDesignator(0).isArrayRangeDesignator())) { |
418 | 0 | Diag(Tok, diag::ext_gnu_missing_equal_designator) |
419 | 0 | << FixItHint::CreateInsertion(Tok.getLocation(), "= "); |
420 | 0 | return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(), |
421 | 0 | true, ParseInitializer()); |
422 | 0 | } |
423 | | |
424 | 0 | Diag(Tok, diag::err_expected_equal_designator); |
425 | 0 | return ExprError(); |
426 | 0 | } |
427 | | |
428 | | /// ParseBraceInitializer - Called when parsing an initializer that has a |
429 | | /// leading open brace. |
430 | | /// |
431 | | /// initializer: [C99 6.7.8] |
432 | | /// '{' initializer-list '}' |
433 | | /// '{' initializer-list ',' '}' |
434 | | /// [C23] '{' '}' |
435 | | /// |
436 | | /// initializer-list: |
437 | | /// designation[opt] initializer ...[opt] |
438 | | /// initializer-list ',' designation[opt] initializer ...[opt] |
439 | | /// |
440 | 125 | ExprResult Parser::ParseBraceInitializer() { |
441 | 125 | InMessageExpressionRAIIObject InMessage(*this, false); |
442 | | |
443 | 125 | BalancedDelimiterTracker T(*this, tok::l_brace); |
444 | 125 | T.consumeOpen(); |
445 | 125 | SourceLocation LBraceLoc = T.getOpenLocation(); |
446 | | |
447 | | /// InitExprs - This is the actual list of expressions contained in the |
448 | | /// initializer. |
449 | 125 | ExprVector InitExprs; |
450 | | |
451 | 125 | if (Tok.is(tok::r_brace)) { |
452 | | // Empty initializers are a C++ feature and a GNU extension to C before C23. |
453 | 1 | if (!getLangOpts().CPlusPlus) { |
454 | 0 | Diag(LBraceLoc, getLangOpts().C23 |
455 | 0 | ? diag::warn_c23_compat_empty_initializer |
456 | 0 | : diag::ext_c_empty_initializer); |
457 | 0 | } |
458 | | // Match the '}'. |
459 | 1 | return Actions.ActOnInitList(LBraceLoc, std::nullopt, ConsumeBrace()); |
460 | 1 | } |
461 | | |
462 | | // Enter an appropriate expression evaluation context for an initializer list. |
463 | 124 | EnterExpressionEvaluationContext EnterContext( |
464 | 124 | Actions, EnterExpressionEvaluationContext::InitList); |
465 | | |
466 | 124 | bool InitExprsOk = true; |
467 | 124 | QualType LikelyType = PreferredType.get(T.getOpenLocation()); |
468 | 124 | DesignatorCompletionInfo DesignatorCompletion{InitExprs, LikelyType}; |
469 | 124 | bool CalledSignatureHelp = false; |
470 | 124 | auto RunSignatureHelp = [&] { |
471 | 0 | QualType PreferredType; |
472 | 0 | if (!LikelyType.isNull()) |
473 | 0 | PreferredType = Actions.ProduceConstructorSignatureHelp( |
474 | 0 | LikelyType->getCanonicalTypeInternal(), T.getOpenLocation(), |
475 | 0 | InitExprs, T.getOpenLocation(), /*Braced=*/true); |
476 | 0 | CalledSignatureHelp = true; |
477 | 0 | return PreferredType; |
478 | 0 | }; |
479 | | |
480 | 126 | while (true) { |
481 | 126 | PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp); |
482 | | |
483 | | // Handle Microsoft __if_exists/if_not_exists if necessary. |
484 | 126 | if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) || |
485 | 0 | Tok.is(tok::kw___if_not_exists))) { |
486 | 0 | if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) { |
487 | 0 | if (Tok.isNot(tok::comma)) break; |
488 | 0 | ConsumeToken(); |
489 | 0 | } |
490 | 0 | if (Tok.is(tok::r_brace)) break; |
491 | 0 | continue; |
492 | 0 | } |
493 | | |
494 | | // Parse: designation[opt] initializer |
495 | | |
496 | | // If we know that this cannot be a designation, just parse the nested |
497 | | // initializer directly. |
498 | 126 | ExprResult SubElt; |
499 | 126 | if (MayBeDesignationStart()) |
500 | 1 | SubElt = ParseInitializerWithPotentialDesignator(DesignatorCompletion); |
501 | 125 | else |
502 | 125 | SubElt = ParseInitializer(); |
503 | | |
504 | 126 | if (Tok.is(tok::ellipsis)) |
505 | 0 | SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken()); |
506 | | |
507 | 126 | SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get()); |
508 | | |
509 | | // If we couldn't parse the subelement, bail out. |
510 | 126 | if (SubElt.isUsable()) { |
511 | 33 | InitExprs.push_back(SubElt.get()); |
512 | 93 | } else { |
513 | 93 | InitExprsOk = false; |
514 | | |
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 | 93 | if (Tok.isNot(tok::comma)) { |
524 | 93 | SkipUntil(tok::r_brace, StopBeforeMatch); |
525 | 93 | break; |
526 | 93 | } |
527 | 93 | } |
528 | | |
529 | | // If we don't have a comma continued list, we're done. |
530 | 33 | if (Tok.isNot(tok::comma)) break; |
531 | | |
532 | | // TODO: save comma locations if some client cares. |
533 | 2 | ConsumeToken(); |
534 | | |
535 | | // Handle trailing comma. |
536 | 2 | if (Tok.is(tok::r_brace)) break; |
537 | 2 | } |
538 | | |
539 | 124 | bool closed = !T.consumeClose(); |
540 | | |
541 | 124 | if (InitExprsOk && closed) |
542 | 2 | return Actions.ActOnInitList(LBraceLoc, InitExprs, |
543 | 2 | T.getCloseLocation()); |
544 | | |
545 | 122 | return ExprError(); // an error occurred. |
546 | 124 | } |
547 | | |
548 | | |
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, |
552 | 0 | bool &InitExprsOk) { |
553 | 0 | bool trailingComma = false; |
554 | 0 | IfExistsCondition Result; |
555 | 0 | if (ParseMicrosoftIfExistsCondition(Result)) |
556 | 0 | return false; |
557 | | |
558 | 0 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
559 | 0 | if (Braces.consumeOpen()) { |
560 | 0 | Diag(Tok, diag::err_expected) << tok::l_brace; |
561 | 0 | return false; |
562 | 0 | } |
563 | | |
564 | 0 | switch (Result.Behavior) { |
565 | 0 | case IEB_Parse: |
566 | | // Parse the declarations below. |
567 | 0 | break; |
568 | | |
569 | 0 | case IEB_Dependent: |
570 | 0 | Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) |
571 | 0 | << Result.IsIfExists; |
572 | | // Fall through to skip. |
573 | 0 | [[fallthrough]]; |
574 | |
|
575 | 0 | case IEB_Skip: |
576 | 0 | Braces.skipToEnd(); |
577 | 0 | return false; |
578 | 0 | } |
579 | | |
580 | 0 | DesignatorCompletionInfo DesignatorCompletion{ |
581 | 0 | InitExprs, |
582 | 0 | PreferredType.get(Braces.getOpenLocation()), |
583 | 0 | }; |
584 | 0 | while (!isEofOrEom()) { |
585 | 0 | trailingComma = false; |
586 | | // If we know that this cannot be a designation, just parse the nested |
587 | | // initializer directly. |
588 | 0 | ExprResult SubElt; |
589 | 0 | if (MayBeDesignationStart()) |
590 | 0 | SubElt = ParseInitializerWithPotentialDesignator(DesignatorCompletion); |
591 | 0 | else |
592 | 0 | SubElt = ParseInitializer(); |
593 | |
|
594 | 0 | if (Tok.is(tok::ellipsis)) |
595 | 0 | SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken()); |
596 | | |
597 | | // If we couldn't parse the subelement, bail out. |
598 | 0 | if (!SubElt.isInvalid()) |
599 | 0 | InitExprs.push_back(SubElt.get()); |
600 | 0 | else |
601 | 0 | InitExprsOk = false; |
602 | |
|
603 | 0 | if (Tok.is(tok::comma)) { |
604 | 0 | ConsumeToken(); |
605 | 0 | trailingComma = true; |
606 | 0 | } |
607 | |
|
608 | 0 | if (Tok.is(tok::r_brace)) |
609 | 0 | break; |
610 | 0 | } |
611 | |
|
612 | 0 | Braces.consumeClose(); |
613 | |
|
614 | 0 | return !trailingComma; |
615 | 0 | } |