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