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