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