1193326Sed//===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This file implements initializer parsing as specified by C99 6.7.8.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#include "clang/Parse/Parser.h"
15249423Sdim#include "RAIIObjectsForParser.h"
16193326Sed#include "clang/Parse/ParseDiagnostic.h"
17212904Sdim#include "clang/Sema/Designator.h"
18212904Sdim#include "clang/Sema/Scope.h"
19193326Sed#include "llvm/ADT/SmallString.h"
20198398Srdivacky#include "llvm/Support/raw_ostream.h"
21193326Sedusing namespace clang;
22193326Sed
23193326Sed
24234353Sdim/// MayBeDesignationStart - Return true if the current token might be the start
25234353Sdim/// of a designator.  If we can tell it is impossible that it is a designator,
26234353Sdim/// return false.
27234353Sdimbool Parser::MayBeDesignationStart() {
28234353Sdim  switch (Tok.getKind()) {
29234353Sdim  default:
30234353Sdim    return false;
31234353Sdim
32193326Sed  case tok::period:      // designator: '.' identifier
33234353Sdim    return true;
34234353Sdim
35234353Sdim  case tok::l_square: {  // designator: array-designator
36249423Sdim    if (!PP.getLangOpts().CPlusPlus11)
37193326Sed      return true;
38234353Sdim
39234353Sdim    // C++11 lambda expressions and C99 designators can be ambiguous all the
40234353Sdim    // way through the closing ']' and to the next character. Handle the easy
41234353Sdim    // cases here, and fall back to tentative parsing if those fail.
42234353Sdim    switch (PP.LookAhead(0).getKind()) {
43234353Sdim    case tok::equal:
44234353Sdim    case tok::r_square:
45234353Sdim      // Definitely starts a lambda expression.
46234353Sdim      return false;
47234353Sdim
48234353Sdim    case tok::amp:
49234353Sdim    case tok::kw_this:
50234353Sdim    case tok::identifier:
51234353Sdim      // We have to do additional analysis, because these could be the
52234353Sdim      // start of a constant expression or a lambda capture list.
53234353Sdim      break;
54234353Sdim
55234353Sdim    default:
56234353Sdim      // Anything not mentioned above cannot occur following a '[' in a
57234353Sdim      // lambda expression.
58234353Sdim      return true;
59234353Sdim    }
60234353Sdim
61234353Sdim    // Handle the complicated case below.
62234353Sdim    break;
63234353Sdim  }
64193326Sed  case tok::identifier:  // designation: identifier ':'
65193326Sed    return PP.LookAhead(0).is(tok::colon);
66193326Sed  }
67234353Sdim
68234353Sdim  // Parse up to (at most) the token after the closing ']' to determine
69276479Sdim  // whether this is a C99 designator or a lambda.
70234353Sdim  TentativeParsingAction Tentative(*this);
71276479Sdim
72276479Sdim  LambdaIntroducer Intro;
73276479Sdim  bool SkippedInits = false;
74276479Sdim  Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
75276479Sdim
76276479Sdim  if (DiagID) {
77276479Sdim    // If this can't be a lambda capture list, it's a designator.
78276479Sdim    Tentative.Revert();
79276479Sdim    return true;
80234353Sdim  }
81276479Sdim
82276479Sdim  // Once we hit the closing square bracket, we look at the next
83276479Sdim  // token. If it's an '=', this is a designator. Otherwise, it's a
84276479Sdim  // lambda expression. This decision favors lambdas over the older
85276479Sdim  // GNU designator syntax, which allows one to omit the '=', but is
86276479Sdim  // consistent with GCC.
87276479Sdim  tok::TokenKind Kind = Tok.getKind();
88276479Sdim  // FIXME: If we didn't skip any inits, parse the lambda from here
89276479Sdim  // rather than throwing away then reparsing the LambdaIntroducer.
90276479Sdim  Tentative.Revert();
91276479Sdim  return Kind == tok::equal;
92193326Sed}
93193326Sed
94207619Srdivackystatic void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
95207619Srdivacky                                       Designation &Desig) {
96207619Srdivacky  // If we have exactly one array designator, this used the GNU
97207619Srdivacky  // 'designation: array-designator' extension, otherwise there should be no
98207619Srdivacky  // designators at all!
99207619Srdivacky  if (Desig.getNumDesignators() == 1 &&
100207619Srdivacky      (Desig.getDesignator(0).isArrayDesignator() ||
101207619Srdivacky       Desig.getDesignator(0).isArrayRangeDesignator()))
102207619Srdivacky    P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
103207619Srdivacky  else if (Desig.getNumDesignators() > 0)
104207619Srdivacky    P.Diag(Loc, diag::err_expected_equal_designator);
105207619Srdivacky}
106207619Srdivacky
107193326Sed/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
108193326Sed/// checking to see if the token stream starts with a designator.
109193326Sed///
110193326Sed///       designation:
111193326Sed///         designator-list '='
112193326Sed/// [GNU]   array-designator
113193326Sed/// [GNU]   identifier ':'
114193326Sed///
115193326Sed///       designator-list:
116193326Sed///         designator
117193326Sed///         designator-list designator
118193326Sed///
119193326Sed///       designator:
120193326Sed///         array-designator
121193326Sed///         '.' identifier
122193326Sed///
123193326Sed///       array-designator:
124193326Sed///         '[' constant-expression ']'
125193326Sed/// [GNU]   '[' constant-expression '...' constant-expression ']'
126193326Sed///
127193326Sed/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
128193326Sed/// initializer (because it is an expression).  We need to consider this case
129193326Sed/// when parsing array designators.
130193326Sed///
131212904SdimExprResult Parser::ParseInitializerWithPotentialDesignator() {
132193326Sed
133193326Sed  // If this is the old-style GNU extension:
134193326Sed  //   designation ::= identifier ':'
135193326Sed  // Handle it as a field designator.  Otherwise, this must be the start of a
136193326Sed  // normal expression.
137193326Sed  if (Tok.is(tok::identifier)) {
138193326Sed    const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
139193326Sed
140234353Sdim    SmallString<256> NewSyntax;
141198398Srdivacky    llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
142198398Srdivacky                                         << " = ";
143193326Sed
144193326Sed    SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
145198092Srdivacky
146193326Sed    assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
147193326Sed    SourceLocation ColonLoc = ConsumeToken();
148193326Sed
149226633Sdim    Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
150206084Srdivacky      << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
151288943Sdim                                      NewSyntax);
152193326Sed
153193326Sed    Designation D;
154193326Sed    D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
155198092Srdivacky    return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
156193326Sed                                              ParseInitializer());
157193326Sed  }
158198092Srdivacky
159193326Sed  // Desig - This is initialized when we see our first designator.  We may have
160193326Sed  // an objc message send with no designator, so we don't want to create this
161193326Sed  // eagerly.
162193326Sed  Designation Desig;
163198092Srdivacky
164193326Sed  // Parse each designator in the designator list until we find an initializer.
165193326Sed  while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
166193326Sed    if (Tok.is(tok::period)) {
167193326Sed      // designator: '.' identifier
168193326Sed      SourceLocation DotLoc = ConsumeToken();
169198092Srdivacky
170193326Sed      if (Tok.isNot(tok::identifier)) {
171193326Sed        Diag(Tok.getLocation(), diag::err_expected_field_designator);
172193326Sed        return ExprError();
173193326Sed      }
174198092Srdivacky
175193326Sed      Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
176193326Sed                                               Tok.getLocation()));
177193326Sed      ConsumeToken(); // Eat the identifier.
178193326Sed      continue;
179193326Sed    }
180198092Srdivacky
181193326Sed    // We must have either an array designator now or an objc message send.
182193326Sed    assert(Tok.is(tok::l_square) && "Unexpected token!");
183198092Srdivacky
184193326Sed    // Handle the two forms of array designator:
185193326Sed    //   array-designator: '[' constant-expression ']'
186193326Sed    //   array-designator: '[' constant-expression '...' constant-expression ']'
187193326Sed    //
188193326Sed    // Also, we have to handle the case where the expression after the
189193326Sed    // designator an an objc message send: '[' objc-message-expr ']'.
190193326Sed    // Interesting cases are:
191193326Sed    //   [foo bar]         -> objc message send
192193326Sed    //   [foo]             -> array designator
193193326Sed    //   [foo ... bar]     -> array designator
194193326Sed    //   [4][foo bar]      -> obsolete GNU designation with objc message send.
195193326Sed    //
196234353Sdim    // We do not need to check for an expression starting with [[ here. If it
197234353Sdim    // contains an Objective-C message send, then it is not an ill-formed
198234353Sdim    // attribute. If it is a lambda-expression within an array-designator, then
199234353Sdim    // it will be rejected because a constant-expression cannot begin with a
200234353Sdim    // lambda-expression.
201218893Sdim    InMessageExpressionRAIIObject InMessage(*this, true);
202218893Sdim
203226633Sdim    BalancedDelimiterTracker T(*this, tok::l_square);
204226633Sdim    T.consumeOpen();
205226633Sdim    SourceLocation StartLoc = T.getOpenLocation();
206226633Sdim
207212904Sdim    ExprResult Idx;
208198092Srdivacky
209207619Srdivacky    // If Objective-C is enabled and this is a typename (class message
210207619Srdivacky    // send) or send to 'super', parse this as a message send
211207619Srdivacky    // expression.  We handle C++ and C separately, since C++ requires
212207619Srdivacky    // much more complicated parsing.
213234353Sdim    if  (getLangOpts().ObjC1 && getLangOpts().CPlusPlus) {
214207619Srdivacky      // Send to 'super'.
215207619Srdivacky      if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
216218893Sdim          NextToken().isNot(tok::period) &&
217218893Sdim          getCurScope()->isInObjcMethodScope()) {
218207619Srdivacky        CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
219207619Srdivacky        return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
220212904Sdim                                                           ConsumeToken(),
221212904Sdim                                                           ParsedType(),
222276479Sdim                                                           nullptr);
223207619Srdivacky      }
224193326Sed
225207619Srdivacky      // Parse the receiver, which is either a type or an expression.
226207619Srdivacky      bool IsExpr;
227207619Srdivacky      void *TypeOrExpr;
228207619Srdivacky      if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
229261991Sdim        SkipUntil(tok::r_square, StopAtSemi);
230207619Srdivacky        return ExprError();
231207619Srdivacky      }
232207619Srdivacky
233207619Srdivacky      // If the receiver was a type, we have a class message; parse
234207619Srdivacky      // the rest of it.
235207619Srdivacky      if (!IsExpr) {
236207619Srdivacky        CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
237207619Srdivacky        return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
238207619Srdivacky                                                           SourceLocation(),
239212904Sdim                                   ParsedType::getFromOpaquePtr(TypeOrExpr),
240276479Sdim                                                           nullptr);
241207619Srdivacky      }
242207619Srdivacky
243207619Srdivacky      // If the receiver was an expression, we still don't know
244207619Srdivacky      // whether we have a message send or an array designator; just
245207619Srdivacky      // adopt the expression for further analysis below.
246207619Srdivacky      // FIXME: potentially-potentially evaluated expression above?
247212904Sdim      Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
248234353Sdim    } else if (getLangOpts().ObjC1 && Tok.is(tok::identifier)) {
249207619Srdivacky      IdentifierInfo *II = Tok.getIdentifierInfo();
250207619Srdivacky      SourceLocation IILoc = Tok.getLocation();
251212904Sdim      ParsedType ReceiverType;
252207619Srdivacky      // Three cases. This is a message send to a type: [type foo]
253207619Srdivacky      // This is a message send to super:  [super foo]
254207619Srdivacky      // This is a message sent to an expr:  [super.bar foo]
255288943Sdim      switch (Actions.getObjCMessageKind(
256288943Sdim          getCurScope(), II, IILoc, II == Ident_super,
257288943Sdim          NextToken().is(tok::period), ReceiverType)) {
258212904Sdim      case Sema::ObjCSuperMessage:
259288943Sdim        CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
260288943Sdim        return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
261288943Sdim                                                           ConsumeToken(),
262288943Sdim                                                           ParsedType(),
263288943Sdim                                                           nullptr);
264288943Sdim
265212904Sdim      case Sema::ObjCClassMessage:
266207619Srdivacky        CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
267207619Srdivacky        ConsumeToken(); // the identifier
268207619Srdivacky        if (!ReceiverType) {
269261991Sdim          SkipUntil(tok::r_square, StopAtSemi);
270207619Srdivacky          return ExprError();
271207619Srdivacky        }
272207619Srdivacky
273288943Sdim        // Parse type arguments and protocol qualifiers.
274288943Sdim        if (Tok.is(tok::less)) {
275288943Sdim          SourceLocation NewEndLoc;
276288943Sdim          TypeResult NewReceiverType
277288943Sdim            = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType,
278288943Sdim                                                     /*consumeLastToken=*/true,
279288943Sdim                                                     NewEndLoc);
280288943Sdim          if (!NewReceiverType.isUsable()) {
281288943Sdim            SkipUntil(tok::r_square, StopAtSemi);
282288943Sdim            return ExprError();
283288943Sdim          }
284288943Sdim
285288943Sdim          ReceiverType = NewReceiverType.get();
286288943Sdim        }
287288943Sdim
288288943Sdim        return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
289207619Srdivacky                                                           SourceLocation(),
290207619Srdivacky                                                           ReceiverType,
291276479Sdim                                                           nullptr);
292207619Srdivacky
293212904Sdim      case Sema::ObjCInstanceMessage:
294207619Srdivacky        // Fall through; we'll just parse the expression and
295207619Srdivacky        // (possibly) treat this like an Objective-C message send
296207619Srdivacky        // later.
297207619Srdivacky        break;
298207619Srdivacky      }
299193326Sed    }
300193326Sed
301207619Srdivacky    // Parse the index expression, if we haven't already gotten one
302207619Srdivacky    // above (which can only happen in Objective-C++).
303193326Sed    // Note that we parse this as an assignment expression, not a constant
304193326Sed    // expression (allowing *=, =, etc) to handle the objc case.  Sema needs
305193326Sed    // to validate that the expression is a constant.
306207619Srdivacky    // FIXME: We also need to tell Sema that we're in a
307207619Srdivacky    // potentially-potentially evaluated context.
308207619Srdivacky    if (!Idx.get()) {
309207619Srdivacky      Idx = ParseAssignmentExpression();
310207619Srdivacky      if (Idx.isInvalid()) {
311261991Sdim        SkipUntil(tok::r_square, StopAtSemi);
312243830Sdim        return Idx;
313207619Srdivacky      }
314193326Sed    }
315198092Srdivacky
316193326Sed    // Given an expression, we could either have a designator (if the next
317193326Sed    // tokens are '...' or ']' or an objc message send.  If this is an objc
318198092Srdivacky    // message send, handle it now.  An objc-message send is the start of
319193326Sed    // an assignment-expression production.
320234353Sdim    if (getLangOpts().ObjC1 && Tok.isNot(tok::ellipsis) &&
321193326Sed        Tok.isNot(tok::r_square)) {
322207619Srdivacky      CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
323193326Sed      return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
324193326Sed                                                         SourceLocation(),
325212904Sdim                                                         ParsedType(),
326276479Sdim                                                         Idx.get());
327193326Sed    }
328193326Sed
329193326Sed    // If this is a normal array designator, remember it.
330193326Sed    if (Tok.isNot(tok::ellipsis)) {
331276479Sdim      Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc));
332193326Sed    } else {
333193326Sed      // Handle the gnu array range extension.
334193326Sed      Diag(Tok, diag::ext_gnu_array_range);
335193326Sed      SourceLocation EllipsisLoc = ConsumeToken();
336193326Sed
337212904Sdim      ExprResult RHS(ParseConstantExpression());
338193326Sed      if (RHS.isInvalid()) {
339261991Sdim        SkipUntil(tok::r_square, StopAtSemi);
340243830Sdim        return RHS;
341193326Sed      }
342276479Sdim      Desig.AddDesignator(Designator::getArrayRange(Idx.get(),
343276479Sdim                                                    RHS.get(),
344193326Sed                                                    StartLoc, EllipsisLoc));
345193326Sed    }
346193326Sed
347226633Sdim    T.consumeClose();
348226633Sdim    Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
349226633Sdim                                                        T.getCloseLocation());
350193326Sed  }
351193326Sed
352193326Sed  // Okay, we're done with the designator sequence.  We know that there must be
353193326Sed  // at least one designator, because the only case we can get into this method
354193326Sed  // without a designator is when we have an objc message send.  That case is
355193326Sed  // handled and returned from above.
356193326Sed  assert(!Desig.empty() && "Designator is empty?");
357193326Sed
358193326Sed  // Handle a normal designator sequence end, which is an equal.
359193326Sed  if (Tok.is(tok::equal)) {
360193326Sed    SourceLocation EqualLoc = ConsumeToken();
361193326Sed    return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
362193326Sed                                              ParseInitializer());
363193326Sed  }
364193326Sed
365193326Sed  // We read some number of designators and found something that isn't an = or
366193326Sed  // an initializer.  If we have exactly one array designator, this
367193326Sed  // is the GNU 'designation: array-designator' extension.  Otherwise, it is a
368193326Sed  // parse error.
369198092Srdivacky  if (Desig.getNumDesignators() == 1 &&
370193326Sed      (Desig.getDesignator(0).isArrayDesignator() ||
371193326Sed       Desig.getDesignator(0).isArrayRangeDesignator())) {
372193326Sed    Diag(Tok, diag::ext_gnu_missing_equal_designator)
373206084Srdivacky      << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
374193326Sed    return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
375193326Sed                                              true, ParseInitializer());
376193326Sed  }
377193326Sed
378193326Sed  Diag(Tok, diag::err_expected_equal_designator);
379193326Sed  return ExprError();
380193326Sed}
381193326Sed
382193326Sed
383193326Sed/// ParseBraceInitializer - Called when parsing an initializer that has a
384193326Sed/// leading open brace.
385193326Sed///
386193326Sed///       initializer: [C99 6.7.8]
387193326Sed///         '{' initializer-list '}'
388193326Sed///         '{' initializer-list ',' '}'
389193326Sed/// [GNU]   '{' '}'
390193326Sed///
391193326Sed///       initializer-list:
392218893Sdim///         designation[opt] initializer ...[opt]
393218893Sdim///         initializer-list ',' designation[opt] initializer ...[opt]
394193326Sed///
395212904SdimExprResult Parser::ParseBraceInitializer() {
396218893Sdim  InMessageExpressionRAIIObject InMessage(*this, false);
397218893Sdim
398226633Sdim  BalancedDelimiterTracker T(*this, tok::l_brace);
399226633Sdim  T.consumeOpen();
400226633Sdim  SourceLocation LBraceLoc = T.getOpenLocation();
401193326Sed
402193326Sed  /// InitExprs - This is the actual list of expressions contained in the
403193326Sed  /// initializer.
404243830Sdim  ExprVector InitExprs;
405193326Sed
406193326Sed  if (Tok.is(tok::r_brace)) {
407193326Sed    // Empty initializers are a C++ feature and a GNU extension to C.
408234353Sdim    if (!getLangOpts().CPlusPlus)
409193326Sed      Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
410193326Sed    // Match the '}'.
411251662Sdim    return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace());
412193326Sed  }
413193326Sed
414193326Sed  bool InitExprsOk = true;
415193326Sed
416193326Sed  while (1) {
417234353Sdim    // Handle Microsoft __if_exists/if_not_exists if necessary.
418234353Sdim    if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
419234353Sdim        Tok.is(tok::kw___if_not_exists))) {
420234353Sdim      if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
421234353Sdim        if (Tok.isNot(tok::comma)) break;
422234353Sdim        ConsumeToken();
423234353Sdim      }
424234353Sdim      if (Tok.is(tok::r_brace)) break;
425234353Sdim      continue;
426234353Sdim    }
427234353Sdim
428193326Sed    // Parse: designation[opt] initializer
429193326Sed
430193326Sed    // If we know that this cannot be a designation, just parse the nested
431193326Sed    // initializer directly.
432212904Sdim    ExprResult SubElt;
433234353Sdim    if (MayBeDesignationStart())
434193326Sed      SubElt = ParseInitializerWithPotentialDesignator();
435193326Sed    else
436193326Sed      SubElt = ParseInitializer();
437198092Srdivacky
438218893Sdim    if (Tok.is(tok::ellipsis))
439218893Sdim      SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
440280031Sdim
441280031Sdim    SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get());
442280031Sdim
443193326Sed    // If we couldn't parse the subelement, bail out.
444280031Sdim    if (SubElt.isUsable()) {
445276479Sdim      InitExprs.push_back(SubElt.get());
446193326Sed    } else {
447193326Sed      InitExprsOk = false;
448198092Srdivacky
449193326Sed      // We have two ways to try to recover from this error: if the code looks
450221345Sdim      // grammatically ok (i.e. we have a comma coming up) try to continue
451193326Sed      // parsing the rest of the initializer.  This allows us to emit
452193326Sed      // diagnostics for later elements that we find.  If we don't see a comma,
453193326Sed      // assume there is a parse error, and just skip to recover.
454193326Sed      // FIXME: This comment doesn't sound right. If there is a r_brace
455193326Sed      // immediately, it can't be an error, since there is no other way of
456193326Sed      // leaving this loop except through this if.
457193326Sed      if (Tok.isNot(tok::comma)) {
458261991Sdim        SkipUntil(tok::r_brace, StopBeforeMatch);
459193326Sed        break;
460193326Sed      }
461193326Sed    }
462193326Sed
463193326Sed    // If we don't have a comma continued list, we're done.
464193326Sed    if (Tok.isNot(tok::comma)) break;
465193326Sed
466193326Sed    // TODO: save comma locations if some client cares.
467193326Sed    ConsumeToken();
468193326Sed
469193326Sed    // Handle trailing comma.
470193326Sed    if (Tok.is(tok::r_brace)) break;
471193326Sed  }
472226633Sdim
473226633Sdim  bool closed = !T.consumeClose();
474226633Sdim
475226633Sdim  if (InitExprsOk && closed)
476243830Sdim    return Actions.ActOnInitList(LBraceLoc, InitExprs,
477226633Sdim                                 T.getCloseLocation());
478193326Sed
479193326Sed  return ExprError(); // an error occurred.
480193326Sed}
481193326Sed
482234353Sdim
483234353Sdim// Return true if a comma (or closing brace) is necessary after the
484234353Sdim// __if_exists/if_not_exists statement.
485234353Sdimbool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
486234353Sdim                                                    bool &InitExprsOk) {
487234353Sdim  bool trailingComma = false;
488234353Sdim  IfExistsCondition Result;
489234353Sdim  if (ParseMicrosoftIfExistsCondition(Result))
490234353Sdim    return false;
491234353Sdim
492234353Sdim  BalancedDelimiterTracker Braces(*this, tok::l_brace);
493234353Sdim  if (Braces.consumeOpen()) {
494276479Sdim    Diag(Tok, diag::err_expected) << tok::l_brace;
495234353Sdim    return false;
496234353Sdim  }
497234353Sdim
498234353Sdim  switch (Result.Behavior) {
499234353Sdim  case IEB_Parse:
500234353Sdim    // Parse the declarations below.
501234353Sdim    break;
502234353Sdim
503234353Sdim  case IEB_Dependent:
504234353Sdim    Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
505234353Sdim      << Result.IsIfExists;
506234353Sdim    // Fall through to skip.
507234353Sdim
508234353Sdim  case IEB_Skip:
509234353Sdim    Braces.skipToEnd();
510234353Sdim    return false;
511234353Sdim  }
512234353Sdim
513276479Sdim  while (!isEofOrEom()) {
514234353Sdim    trailingComma = false;
515234353Sdim    // If we know that this cannot be a designation, just parse the nested
516234353Sdim    // initializer directly.
517234353Sdim    ExprResult SubElt;
518234353Sdim    if (MayBeDesignationStart())
519234353Sdim      SubElt = ParseInitializerWithPotentialDesignator();
520234353Sdim    else
521234353Sdim      SubElt = ParseInitializer();
522234353Sdim
523234353Sdim    if (Tok.is(tok::ellipsis))
524234353Sdim      SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
525234353Sdim
526234353Sdim    // If we couldn't parse the subelement, bail out.
527234353Sdim    if (!SubElt.isInvalid())
528276479Sdim      InitExprs.push_back(SubElt.get());
529234353Sdim    else
530234353Sdim      InitExprsOk = false;
531234353Sdim
532234353Sdim    if (Tok.is(tok::comma)) {
533234353Sdim      ConsumeToken();
534234353Sdim      trailingComma = true;
535234353Sdim    }
536234353Sdim
537234353Sdim    if (Tok.is(tok::r_brace))
538234353Sdim      break;
539234353Sdim  }
540234353Sdim
541234353Sdim  Braces.consumeClose();
542234353Sdim
543234353Sdim  return !trailingComma;
544234353Sdim}
545