blob: 33abc932541dd9c7fb7df9c0a499f65c72aa2c02 [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
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregor0fbda682010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Designator.h"
18#include "clang/Sema/Scope.h"
Steve Naroff4aa88f82007-07-19 01:06:55 +000019#include "llvm/ADT/SmallString.h"
Daniel Dunbar62a72172009-10-17 23:52:50 +000020#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
23
24/// MayBeDesignationStart - Return true if this token might be the start of a
Chris Lattner838cb212008-10-26 21:46:13 +000025/// designator. If we can tell it is impossible that it is a designator, return
Mike Stump1eb44332009-09-09 15:08:12 +000026/// false.
Chris Lattnerefcadc62008-10-26 22:41:58 +000027static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +000028 switch (K) {
29 default: return false;
30 case tok::period: // designator: '.' identifier
31 case tok::l_square: // designator: array-designator
Chris Lattnerefcadc62008-10-26 22:41:58 +000032 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +000033 case tok::identifier: // designation: identifier ':'
Chris Lattnerefcadc62008-10-26 22:41:58 +000034 return PP.LookAhead(0).is(tok::colon);
Reid Spencer5f016e22007-07-11 17:01:13 +000035 }
36}
37
Douglas Gregor6aa14d82010-04-21 22:36:40 +000038static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
39 Designation &Desig) {
40 // If we have exactly one array designator, this used the GNU
41 // 'designation: array-designator' extension, otherwise there should be no
42 // designators at all!
43 if (Desig.getNumDesignators() == 1 &&
44 (Desig.getDesignator(0).isArrayDesignator() ||
45 Desig.getDesignator(0).isArrayRangeDesignator()))
46 P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
47 else if (Desig.getNumDesignators() > 0)
48 P.Diag(Loc, diag::err_expected_equal_designator);
49}
50
Reid Spencer5f016e22007-07-11 17:01:13 +000051/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
52/// checking to see if the token stream starts with a designator.
53///
54/// designation:
55/// designator-list '='
56/// [GNU] array-designator
57/// [GNU] identifier ':'
58///
59/// designator-list:
60/// designator
61/// designator-list designator
62///
63/// designator:
64/// array-designator
65/// '.' identifier
66///
67/// array-designator:
68/// '[' constant-expression ']'
69/// [GNU] '[' constant-expression '...' constant-expression ']'
70///
71/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattner838cb212008-10-26 21:46:13 +000072/// initializer (because it is an expression). We need to consider this case
73/// when parsing array designators.
Reid Spencer5f016e22007-07-11 17:01:13 +000074///
John McCall60d7b3a2010-08-24 06:29:42 +000075ExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redl20df9b72008-12-11 22:51:44 +000076
Chris Lattnereccc53a2008-10-26 22:36:07 +000077 // If this is the old-style GNU extension:
78 // designation ::= identifier ':'
79 // Handle it as a field designator. Otherwise, this must be the start of a
80 // normal expression.
81 if (Tok.is(tok::identifier)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +000082 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregoreeae8f02009-03-28 00:41:23 +000083
Daniel Dunbar62a72172009-10-17 23:52:50 +000084 llvm::SmallString<256> NewSyntax;
Daniel Dunbar01eb9b92009-10-18 21:17:35 +000085 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbar62a72172009-10-17 23:52:50 +000086 << " = ";
Douglas Gregoreeae8f02009-03-28 00:41:23 +000087
Douglas Gregor05c13a32009-01-22 00:58:24 +000088 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner7f9690d2008-10-26 22:49:49 +000090 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregor05c13a32009-01-22 00:58:24 +000091 SourceLocation ColonLoc = ConsumeToken();
92
Douglas Gregor40a0f9c2011-08-27 00:13:16 +000093 Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +000094 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
95 NewSyntax.str());
Douglas Gregoreeae8f02009-03-28 00:41:23 +000096
Douglas Gregor5908a922009-03-20 23:11:49 +000097 Designation D;
Douglas Gregor05c13a32009-01-22 00:58:24 +000098 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump1eb44332009-09-09 15:08:12 +000099 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000100 ParseInitializer());
Chris Lattnereccc53a2008-10-26 22:36:07 +0000101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner0a68b942008-10-26 22:59:19 +0000103 // Desig - This is initialized when we see our first designator. We may have
104 // an objc message send with no designator, so we don't want to create this
105 // eagerly.
Douglas Gregor5908a922009-03-20 23:11:49 +0000106 Designation Desig;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 // Parse each designator in the designator list until we find an initializer.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000109 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
110 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 // designator: '.' identifier
Douglas Gregor05c13a32009-01-22 00:58:24 +0000112 SourceLocation DotLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattner0a68b942008-10-26 22:59:19 +0000114 if (Tok.isNot(tok::identifier)) {
115 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000116 return ExprError();
Chris Lattner0a68b942008-10-26 22:59:19 +0000117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Douglas Gregor5908a922009-03-20 23:11:49 +0000119 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
120 Tok.getLocation()));
Chris Lattner0a68b942008-10-26 22:59:19 +0000121 ConsumeToken(); // Eat the identifier.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000122 continue;
123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner7f9690d2008-10-26 22:49:49 +0000125 // We must have either an array designator now or an objc message send.
126 assert(Tok.is(tok::l_square) && "Unexpected token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Chris Lattnere2329422008-10-26 23:06:54 +0000128 // Handle the two forms of array designator:
129 // array-designator: '[' constant-expression ']'
130 // array-designator: '[' constant-expression '...' constant-expression ']'
131 //
132 // Also, we have to handle the case where the expression after the
133 // designator an an objc message send: '[' objc-message-expr ']'.
134 // Interesting cases are:
135 // [foo bar] -> objc message send
136 // [foo] -> array designator
137 // [foo ... bar] -> array designator
138 // [4][foo bar] -> obsolete GNU designation with objc message send.
139 //
Douglas Gregor0fbda682010-09-15 14:51:05 +0000140 InMessageExpressionRAIIObject InMessage(*this, true);
141
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000142 BalancedDelimiterTracker T(*this, tok::l_square);
143 T.consumeOpen();
144 SourceLocation StartLoc = T.getOpenLocation();
145
John McCall60d7b3a2010-08-24 06:29:42 +0000146 ExprResult Idx;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000148 // If Objective-C is enabled and this is a typename (class message
149 // send) or send to 'super', parse this as a message send
150 // expression. We handle C++ and C separately, since C++ requires
151 // much more complicated parsing.
152 if (getLang().ObjC1 && getLang().CPlusPlus) {
153 // Send to 'super'.
154 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0fbda682010-09-15 14:51:05 +0000155 NextToken().isNot(tok::period) &&
156 getCurScope()->isInObjcMethodScope()) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000157 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
158 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
John McCallb3d87482010-08-24 05:47:05 +0000159 ConsumeToken(),
160 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +0000161 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000162 }
163
164 // Parse the receiver, which is either a type or an expression.
165 bool IsExpr;
166 void *TypeOrExpr;
167 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
168 SkipUntil(tok::r_square);
169 return ExprError();
170 }
171
172 // If the receiver was a type, we have a class message; parse
173 // the rest of it.
174 if (!IsExpr) {
175 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
176 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
177 SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +0000178 ParsedType::getFromOpaquePtr(TypeOrExpr),
John McCall9ae2f072010-08-23 23:25:46 +0000179 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000180 }
181
182 // If the receiver was an expression, we still don't know
183 // whether we have a message send or an array designator; just
184 // adopt the expression for further analysis below.
185 // FIXME: potentially-potentially evaluated expression above?
John McCall60d7b3a2010-08-24 06:29:42 +0000186 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000187 } else if (getLang().ObjC1 && Tok.is(tok::identifier)) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000188 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor2725ca82010-04-21 19:57:20 +0000189 SourceLocation IILoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +0000190 ParsedType ReceiverType;
Chris Lattner1e461362010-04-12 06:36:00 +0000191 // Three cases. This is a message send to a type: [type foo]
192 // This is a message send to super: [super foo]
193 // This is a message sent to an expr: [super.bar foo]
John McCallf312b1e2010-08-26 23:41:50 +0000194 switch (Sema::ObjCMessageKind Kind
Douglas Gregor23c94db2010-07-02 17:43:08 +0000195 = Actions.getObjCMessageKind(getCurScope(), II, IILoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +0000196 II == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +0000197 NextToken().is(tok::period),
198 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +0000199 case Sema::ObjCSuperMessage:
200 case Sema::ObjCClassMessage:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000201 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
John McCallf312b1e2010-08-26 23:41:50 +0000202 if (Kind == Sema::ObjCSuperMessage)
Douglas Gregor2725ca82010-04-21 19:57:20 +0000203 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
204 ConsumeToken(),
John McCallb3d87482010-08-24 05:47:05 +0000205 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +0000206 0);
Douglas Gregor1569f952010-04-21 20:38:13 +0000207 ConsumeToken(); // the identifier
208 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000209 SkipUntil(tok::r_square);
210 return ExprError();
211 }
212
213 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
214 SourceLocation(),
Douglas Gregor1569f952010-04-21 20:38:13 +0000215 ReceiverType,
John McCall9ae2f072010-08-23 23:25:46 +0000216 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +0000217
John McCallf312b1e2010-08-26 23:41:50 +0000218 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +0000219 // Fall through; we'll just parse the expression and
220 // (possibly) treat this like an Objective-C message send
221 // later.
222 break;
Chris Lattnereb483eb2010-04-11 08:28:14 +0000223 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000224 }
Sebastian Redl1d922962008-12-13 15:32:12 +0000225
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000226 // Parse the index expression, if we haven't already gotten one
227 // above (which can only happen in Objective-C++).
Chris Lattner7f9690d2008-10-26 22:49:49 +0000228 // Note that we parse this as an assignment expression, not a constant
229 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
230 // to validate that the expression is a constant.
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000231 // FIXME: We also need to tell Sema that we're in a
232 // potentially-potentially evaluated context.
233 if (!Idx.get()) {
234 Idx = ParseAssignmentExpression();
235 if (Idx.isInvalid()) {
236 SkipUntil(tok::r_square);
237 return move(Idx);
238 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000239 }
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Chris Lattner7f9690d2008-10-26 22:49:49 +0000241 // Given an expression, we could either have a designator (if the next
242 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump1eb44332009-09-09 15:08:12 +0000243 // message send, handle it now. An objc-message send is the start of
Chris Lattner7f9690d2008-10-26 22:49:49 +0000244 // an assignment-expression production.
Mike Stump1eb44332009-09-09 15:08:12 +0000245 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattner7f9690d2008-10-26 22:49:49 +0000246 Tok.isNot(tok::r_square)) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000247 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
Sebastian Redl1d922962008-12-13 15:32:12 +0000248 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
249 SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +0000250 ParsedType(),
251 Idx.take());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000252 }
Chris Lattnere2329422008-10-26 23:06:54 +0000253
Chris Lattnere2329422008-10-26 23:06:54 +0000254 // If this is a normal array designator, remember it.
255 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor5908a922009-03-20 23:11:49 +0000256 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattnere2329422008-10-26 23:06:54 +0000257 } else {
258 // Handle the gnu array range extension.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000259 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000260 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000261
John McCall60d7b3a2010-08-24 06:29:42 +0000262 ExprResult RHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000263 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000265 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 }
Douglas Gregor5908a922009-03-20 23:11:49 +0000267 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
268 RHS.release(),
269 StartLoc, EllipsisLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000271
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000272 T.consumeClose();
273 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
274 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000276
Chris Lattner0a68b942008-10-26 22:59:19 +0000277 // Okay, we're done with the designator sequence. We know that there must be
278 // at least one designator, because the only case we can get into this method
279 // without a designator is when we have an objc message send. That case is
280 // handled and returned from above.
Douglas Gregor5908a922009-03-20 23:11:49 +0000281 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000282
Chris Lattner0a68b942008-10-26 22:59:19 +0000283 // Handle a normal designator sequence end, which is an equal.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000284 if (Tok.is(tok::equal)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000285 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor5908a922009-03-20 23:11:49 +0000286 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000287 ParseInitializer());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000288 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000289
Chris Lattner0a68b942008-10-26 22:59:19 +0000290 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000291 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000292 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
293 // parse error.
Mike Stump1eb44332009-09-09 15:08:12 +0000294 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000295 (Desig.getDesignator(0).isArrayDesignator() ||
296 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000297 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +0000298 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000299 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor68c56de2009-03-27 23:40:29 +0000300 true, ParseInitializer());
Chris Lattner79ed6b52008-10-26 23:22:23 +0000301 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000302
Chris Lattner79ed6b52008-10-26 23:22:23 +0000303 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000304 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000305}
306
307
Chris Lattner0eec2b52008-10-26 22:38:55 +0000308/// ParseBraceInitializer - Called when parsing an initializer that has a
309/// leading open brace.
310///
Reid Spencer5f016e22007-07-11 17:01:13 +0000311/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000312/// '{' initializer-list '}'
313/// '{' initializer-list ',' '}'
314/// [GNU] '{' '}'
315///
316/// initializer-list:
Douglas Gregordcaa1ca2011-01-03 19:31:53 +0000317/// designation[opt] initializer ...[opt]
318/// initializer-list ',' designation[opt] initializer ...[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000319///
John McCall60d7b3a2010-08-24 06:29:42 +0000320ExprResult Parser::ParseBraceInitializer() {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000321 InMessageExpressionRAIIObject InMessage(*this, false);
322
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000323 BalancedDelimiterTracker T(*this, tok::l_brace);
324 T.consumeOpen();
325 SourceLocation LBraceLoc = T.getOpenLocation();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000326
Chris Lattnereccc53a2008-10-26 22:36:07 +0000327 /// InitExprs - This is the actual list of expressions contained in the
328 /// initializer.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000329 ExprVector InitExprs(Actions);
330
Chris Lattner220ad7c2008-10-26 23:35:51 +0000331 if (Tok.is(tok::r_brace)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000332 // Empty initializers are a C++ feature and a GNU extension to C.
333 if (!getLang().CPlusPlus)
334 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner220ad7c2008-10-26 23:35:51 +0000335 // Match the '}'.
John McCallf312b1e2010-08-26 23:41:50 +0000336 return Actions.ActOnInitList(LBraceLoc, MultiExprArg(Actions),
Douglas Gregor5908a922009-03-20 23:11:49 +0000337 ConsumeBrace());
Chris Lattner220ad7c2008-10-26 23:35:51 +0000338 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000339
Steve Naroff4aa88f82007-07-19 01:06:55 +0000340 bool InitExprsOk = true;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000341
Steve Naroff4aa88f82007-07-19 01:06:55 +0000342 while (1) {
343 // Parse: designation[opt] initializer
Sebastian Redl20df9b72008-12-11 22:51:44 +0000344
Steve Naroff4aa88f82007-07-19 01:06:55 +0000345 // If we know that this cannot be a designation, just parse the nested
346 // initializer directly.
John McCall60d7b3a2010-08-24 06:29:42 +0000347 ExprResult SubElt;
Douglas Gregor5908a922009-03-20 23:11:49 +0000348 if (MayBeDesignationStart(Tok.getKind(), PP))
349 SubElt = ParseInitializerWithPotentialDesignator();
350 else
Steve Naroff4aa88f82007-07-19 01:06:55 +0000351 SubElt = ParseInitializer();
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Douglas Gregordcaa1ca2011-01-03 19:31:53 +0000353 if (Tok.is(tok::ellipsis))
354 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
355
Steve Naroff4aa88f82007-07-19 01:06:55 +0000356 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000357 if (!SubElt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000358 InitExprs.push_back(SubElt.release());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000359 } else {
360 InitExprsOk = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Chris Lattner65bb89c2008-04-20 19:07:56 +0000362 // We have two ways to try to recover from this error: if the code looks
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000363 // grammatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000364 // parsing the rest of the initializer. This allows us to emit
365 // diagnostics for later elements that we find. If we don't see a comma,
366 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000367 // FIXME: This comment doesn't sound right. If there is a r_brace
368 // immediately, it can't be an error, since there is no other way of
369 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000370 if (Tok.isNot(tok::comma)) {
371 SkipUntil(tok::r_brace, false, true);
372 break;
373 }
374 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000375
Steve Naroff4aa88f82007-07-19 01:06:55 +0000376 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000377 if (Tok.isNot(tok::comma)) break;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000378
Chris Lattnereccc53a2008-10-26 22:36:07 +0000379 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000380 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000381
Steve Naroff4aa88f82007-07-19 01:06:55 +0000382 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000383 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000384 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000385
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000386 bool closed = !T.consumeClose();
387
388 if (InitExprsOk && closed)
389 return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
390 T.getCloseLocation());
391
Sebastian Redl20df9b72008-12-11 22:51:44 +0000392 return ExprError(); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000393}
394