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