blob: 8451aebc04bd67fde314b60775a71d191250a423 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements initializer parsing as specified by C99 6.7.8.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnereccc53a2008-10-26 22:36:07 +000014#include "clang/Parse/Designator.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner1e461362010-04-12 06:36:00 +000017#include "clang/Parse/Scope.h"
Steve Naroff4aa88f82007-07-19 01:06:55 +000018#include "llvm/ADT/SmallString.h"
Daniel Dunbar62a72172009-10-17 23:52:50 +000019#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
22
23/// MayBeDesignationStart - Return true if this token might be the start of a
Chris Lattner838cb212008-10-26 21:46:13 +000024/// designator. If we can tell it is impossible that it is a designator, return
Mike Stump1eb44332009-09-09 15:08:12 +000025/// false.
Chris Lattnerefcadc62008-10-26 22:41:58 +000026static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +000027 switch (K) {
28 default: return false;
29 case tok::period: // designator: '.' identifier
30 case tok::l_square: // designator: array-designator
Chris Lattnerefcadc62008-10-26 22:41:58 +000031 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +000032 case tok::identifier: // designation: identifier ':'
Chris Lattnerefcadc62008-10-26 22:41:58 +000033 return PP.LookAhead(0).is(tok::colon);
Reid Spencer5f016e22007-07-11 17:01:13 +000034 }
35}
36
Douglas Gregor6aa14d82010-04-21 22:36:40 +000037static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
38 Designation &Desig) {
39 // If we have exactly one array designator, this used the GNU
40 // 'designation: array-designator' extension, otherwise there should be no
41 // designators at all!
42 if (Desig.getNumDesignators() == 1 &&
43 (Desig.getDesignator(0).isArrayDesignator() ||
44 Desig.getDesignator(0).isArrayRangeDesignator()))
45 P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
46 else if (Desig.getNumDesignators() > 0)
47 P.Diag(Loc, diag::err_expected_equal_designator);
48}
49
Reid Spencer5f016e22007-07-11 17:01:13 +000050/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
51/// checking to see if the token stream starts with a designator.
52///
53/// designation:
54/// designator-list '='
55/// [GNU] array-designator
56/// [GNU] identifier ':'
57///
58/// designator-list:
59/// designator
60/// designator-list designator
61///
62/// designator:
63/// array-designator
64/// '.' identifier
65///
66/// array-designator:
67/// '[' constant-expression ']'
68/// [GNU] '[' constant-expression '...' constant-expression ']'
69///
70/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattner838cb212008-10-26 21:46:13 +000071/// initializer (because it is an expression). We need to consider this case
72/// when parsing array designators.
Reid Spencer5f016e22007-07-11 17:01:13 +000073///
Douglas Gregor5908a922009-03-20 23:11:49 +000074Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redl20df9b72008-12-11 22:51:44 +000075
Chris Lattnereccc53a2008-10-26 22:36:07 +000076 // If this is the old-style GNU extension:
77 // designation ::= identifier ':'
78 // Handle it as a field designator. Otherwise, this must be the start of a
79 // normal expression.
80 if (Tok.is(tok::identifier)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +000081 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregoreeae8f02009-03-28 00:41:23 +000082
Daniel Dunbar62a72172009-10-17 23:52:50 +000083 llvm::SmallString<256> NewSyntax;
Daniel Dunbar01eb9b92009-10-18 21:17:35 +000084 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbar62a72172009-10-17 23:52:50 +000085 << " = ";
Douglas Gregoreeae8f02009-03-28 00:41:23 +000086
Douglas Gregor05c13a32009-01-22 00:58:24 +000087 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner7f9690d2008-10-26 22:49:49 +000089 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregor05c13a32009-01-22 00:58:24 +000090 SourceLocation ColonLoc = ConsumeToken();
91
Douglas Gregoreeae8f02009-03-28 00:41:23 +000092 Diag(Tok, diag::ext_gnu_old_style_field_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +000093 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
94 NewSyntax.str());
Douglas Gregoreeae8f02009-03-28 00:41:23 +000095
Douglas Gregor5908a922009-03-20 23:11:49 +000096 Designation D;
Douglas Gregor05c13a32009-01-22 00:58:24 +000097 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump1eb44332009-09-09 15:08:12 +000098 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregor05c13a32009-01-22 00:58:24 +000099 ParseInitializer());
Chris Lattnereccc53a2008-10-26 22:36:07 +0000100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Chris Lattner0a68b942008-10-26 22:59:19 +0000102 // Desig - This is initialized when we see our first designator. We may have
103 // an objc message send with no designator, so we don't want to create this
104 // eagerly.
Douglas Gregor5908a922009-03-20 23:11:49 +0000105 Designation Desig;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 // Parse each designator in the designator list until we find an initializer.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000108 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
109 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 // designator: '.' identifier
Douglas Gregor05c13a32009-01-22 00:58:24 +0000111 SourceLocation DotLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Chris Lattner0a68b942008-10-26 22:59:19 +0000113 if (Tok.isNot(tok::identifier)) {
114 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000115 return ExprError();
Chris Lattner0a68b942008-10-26 22:59:19 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor5908a922009-03-20 23:11:49 +0000118 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
119 Tok.getLocation()));
Chris Lattner0a68b942008-10-26 22:59:19 +0000120 ConsumeToken(); // Eat the identifier.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000121 continue;
122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Chris Lattner7f9690d2008-10-26 22:49:49 +0000124 // We must have either an array designator now or an objc message send.
125 assert(Tok.is(tok::l_square) && "Unexpected token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chris Lattnere2329422008-10-26 23:06:54 +0000127 // Handle the two forms of array designator:
128 // array-designator: '[' constant-expression ']'
129 // array-designator: '[' constant-expression '...' constant-expression ']'
130 //
131 // Also, we have to handle the case where the expression after the
132 // designator an an objc message send: '[' objc-message-expr ']'.
133 // Interesting cases are:
134 // [foo bar] -> objc message send
135 // [foo] -> array designator
136 // [foo ... bar] -> array designator
137 // [4][foo bar] -> obsolete GNU designation with objc message send.
138 //
Chris Lattner7f9690d2008-10-26 22:49:49 +0000139 SourceLocation StartLoc = ConsumeBracket();
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000140 OwningExprResult Idx(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000142 // If Objective-C is enabled and this is a typename (class message
143 // send) or send to 'super', parse this as a message send
144 // expression. We handle C++ and C separately, since C++ requires
145 // much more complicated parsing.
146 if (getLang().ObjC1 && getLang().CPlusPlus) {
147 // Send to 'super'.
148 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000149 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope()) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000150 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
151 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
152 ConsumeToken(), 0,
153 ExprArg(Actions));
154 }
155
156 // Parse the receiver, which is either a type or an expression.
157 bool IsExpr;
158 void *TypeOrExpr;
159 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
160 SkipUntil(tok::r_square);
161 return ExprError();
162 }
163
164 // If the receiver was a type, we have a class message; parse
165 // the rest of it.
166 if (!IsExpr) {
167 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
168 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
169 SourceLocation(),
170 TypeOrExpr,
171 ExprArg(Actions));
172 }
173
174 // If the receiver was an expression, we still don't know
175 // whether we have a message send or an array designator; just
176 // adopt the expression for further analysis below.
177 // FIXME: potentially-potentially evaluated expression above?
178 Idx = OwningExprResult(Actions, TypeOrExpr);
179 } else if (getLang().ObjC1 && Tok.is(tok::identifier)) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000180 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor2725ca82010-04-21 19:57:20 +0000181 SourceLocation IILoc = Tok.getLocation();
Douglas Gregor1569f952010-04-21 20:38:13 +0000182 TypeTy *ReceiverType;
Chris Lattner1e461362010-04-12 06:36:00 +0000183 // Three cases. This is a message send to a type: [type foo]
184 // This is a message send to super: [super foo]
185 // This is a message sent to an expr: [super.bar foo]
Douglas Gregor2725ca82010-04-21 19:57:20 +0000186 switch (Action::ObjCMessageKind Kind
Douglas Gregor23c94db2010-07-02 17:43:08 +0000187 = Actions.getObjCMessageKind(getCurScope(), II, IILoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +0000188 II == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +0000189 NextToken().is(tok::period),
190 ReceiverType)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000191 case Action::ObjCSuperMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +0000192 case Action::ObjCClassMessage:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000193 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
Douglas Gregor2725ca82010-04-21 19:57:20 +0000194 if (Kind == Action::ObjCSuperMessage)
195 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
196 ConsumeToken(),
197 0,
198 ExprArg(Actions));
Douglas Gregor1569f952010-04-21 20:38:13 +0000199 ConsumeToken(); // the identifier
200 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000201 SkipUntil(tok::r_square);
202 return ExprError();
203 }
204
205 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
206 SourceLocation(),
Douglas Gregor1569f952010-04-21 20:38:13 +0000207 ReceiverType,
Douglas Gregor2725ca82010-04-21 19:57:20 +0000208 ExprArg(Actions));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000209
210 case Action::ObjCInstanceMessage:
211 // Fall through; we'll just parse the expression and
212 // (possibly) treat this like an Objective-C message send
213 // later.
214 break;
Chris Lattnereb483eb2010-04-11 08:28:14 +0000215 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000216 }
Sebastian Redl1d922962008-12-13 15:32:12 +0000217
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000218 // Parse the index expression, if we haven't already gotten one
219 // above (which can only happen in Objective-C++).
Chris Lattner7f9690d2008-10-26 22:49:49 +0000220 // Note that we parse this as an assignment expression, not a constant
221 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
222 // to validate that the expression is a constant.
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000223 // FIXME: We also need to tell Sema that we're in a
224 // potentially-potentially evaluated context.
225 if (!Idx.get()) {
226 Idx = ParseAssignmentExpression();
227 if (Idx.isInvalid()) {
228 SkipUntil(tok::r_square);
229 return move(Idx);
230 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000231 }
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Chris Lattner7f9690d2008-10-26 22:49:49 +0000233 // Given an expression, we could either have a designator (if the next
234 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump1eb44332009-09-09 15:08:12 +0000235 // message send, handle it now. An objc-message send is the start of
Chris Lattner7f9690d2008-10-26 22:49:49 +0000236 // an assignment-expression production.
Mike Stump1eb44332009-09-09 15:08:12 +0000237 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattner7f9690d2008-10-26 22:49:49 +0000238 Tok.isNot(tok::r_square)) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000239 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
Sebastian Redl1d922962008-12-13 15:32:12 +0000240 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
241 SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000242 0, move(Idx));
Chris Lattner7f9690d2008-10-26 22:49:49 +0000243 }
Chris Lattnere2329422008-10-26 23:06:54 +0000244
Chris Lattnere2329422008-10-26 23:06:54 +0000245 // If this is a normal array designator, remember it.
246 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor5908a922009-03-20 23:11:49 +0000247 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattnere2329422008-10-26 23:06:54 +0000248 } else {
249 // Handle the gnu array range extension.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000250 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000251 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000252
253 OwningExprResult RHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000254 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000256 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 }
Douglas Gregor5908a922009-03-20 23:11:49 +0000258 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
259 RHS.release(),
260 StartLoc, EllipsisLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000262
Douglas Gregor05c13a32009-01-22 00:58:24 +0000263 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Douglas Gregor5908a922009-03-20 23:11:49 +0000264 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000266
Chris Lattner0a68b942008-10-26 22:59:19 +0000267 // Okay, we're done with the designator sequence. We know that there must be
268 // at least one designator, because the only case we can get into this method
269 // without a designator is when we have an objc message send. That case is
270 // handled and returned from above.
Douglas Gregor5908a922009-03-20 23:11:49 +0000271 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000272
Chris Lattner0a68b942008-10-26 22:59:19 +0000273 // Handle a normal designator sequence end, which is an equal.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000274 if (Tok.is(tok::equal)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000275 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor5908a922009-03-20 23:11:49 +0000276 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000277 ParseInitializer());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000278 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000279
Chris Lattner0a68b942008-10-26 22:59:19 +0000280 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000281 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000282 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
283 // parse error.
Mike Stump1eb44332009-09-09 15:08:12 +0000284 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000285 (Desig.getDesignator(0).isArrayDesignator() ||
286 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000287 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +0000288 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000289 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor68c56de2009-03-27 23:40:29 +0000290 true, ParseInitializer());
Chris Lattner79ed6b52008-10-26 23:22:23 +0000291 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000292
Chris Lattner79ed6b52008-10-26 23:22:23 +0000293 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000294 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000295}
296
297
Chris Lattner0eec2b52008-10-26 22:38:55 +0000298/// ParseBraceInitializer - Called when parsing an initializer that has a
299/// leading open brace.
300///
Reid Spencer5f016e22007-07-11 17:01:13 +0000301/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000302/// '{' initializer-list '}'
303/// '{' initializer-list ',' '}'
304/// [GNU] '{' '}'
305///
306/// initializer-list:
307/// designation[opt] initializer
308/// initializer-list ',' designation[opt] initializer
309///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000310Parser::OwningExprResult Parser::ParseBraceInitializer() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 SourceLocation LBraceLoc = ConsumeBrace();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000312
Chris Lattnereccc53a2008-10-26 22:36:07 +0000313 /// InitExprs - This is the actual list of expressions contained in the
314 /// initializer.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000315 ExprVector InitExprs(Actions);
316
Chris Lattner220ad7c2008-10-26 23:35:51 +0000317 if (Tok.is(tok::r_brace)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000318 // Empty initializers are a C++ feature and a GNU extension to C.
319 if (!getLang().CPlusPlus)
320 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner220ad7c2008-10-26 23:35:51 +0000321 // Match the '}'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000322 return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions),
Douglas Gregor5908a922009-03-20 23:11:49 +0000323 ConsumeBrace());
Chris Lattner220ad7c2008-10-26 23:35:51 +0000324 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000325
Steve Naroff4aa88f82007-07-19 01:06:55 +0000326 bool InitExprsOk = true;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000327
Steve Naroff4aa88f82007-07-19 01:06:55 +0000328 while (1) {
329 // Parse: designation[opt] initializer
Sebastian Redl20df9b72008-12-11 22:51:44 +0000330
Steve Naroff4aa88f82007-07-19 01:06:55 +0000331 // If we know that this cannot be a designation, just parse the nested
332 // initializer directly.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000333 OwningExprResult SubElt(Actions);
Douglas Gregor5908a922009-03-20 23:11:49 +0000334 if (MayBeDesignationStart(Tok.getKind(), PP))
335 SubElt = ParseInitializerWithPotentialDesignator();
336 else
Steve Naroff4aa88f82007-07-19 01:06:55 +0000337 SubElt = ParseInitializer();
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Steve Naroff4aa88f82007-07-19 01:06:55 +0000339 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000340 if (!SubElt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000341 InitExprs.push_back(SubElt.release());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000342 } else {
343 InitExprsOk = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Chris Lattner65bb89c2008-04-20 19:07:56 +0000345 // We have two ways to try to recover from this error: if the code looks
Chris Lattner838cb212008-10-26 21:46:13 +0000346 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000347 // parsing the rest of the initializer. This allows us to emit
348 // diagnostics for later elements that we find. If we don't see a comma,
349 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000350 // FIXME: This comment doesn't sound right. If there is a r_brace
351 // immediately, it can't be an error, since there is no other way of
352 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000353 if (Tok.isNot(tok::comma)) {
354 SkipUntil(tok::r_brace, false, true);
355 break;
356 }
357 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000358
Steve Naroff4aa88f82007-07-19 01:06:55 +0000359 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000360 if (Tok.isNot(tok::comma)) break;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000361
Chris Lattnereccc53a2008-10-26 22:36:07 +0000362 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000363 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000364
Steve Naroff4aa88f82007-07-19 01:06:55 +0000365 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000366 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000367 }
Chris Lattner04d66662007-10-09 17:33:22 +0000368 if (InitExprsOk && Tok.is(tok::r_brace))
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000369 return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
Douglas Gregor5908a922009-03-20 23:11:49 +0000370 ConsumeBrace());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000371
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 // Match the '}'.
373 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000374 return ExprError(); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000375}
376