blob: 4347294141ac1f70e0b9e1b391e6b4917a7ca8c6 [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"
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Sema/Designator.h"
17#include "clang/Sema/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///
John McCall60d7b3a2010-08-24 06:29:42 +000074ExprResult 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();
John McCall60d7b3a2010-08-24 06:29:42 +0000140 ExprResult Idx;
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,
John McCallb3d87482010-08-24 05:47:05 +0000152 ConsumeToken(),
153 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +0000154 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000155 }
156
157 // Parse the receiver, which is either a type or an expression.
158 bool IsExpr;
159 void *TypeOrExpr;
160 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
161 SkipUntil(tok::r_square);
162 return ExprError();
163 }
164
165 // If the receiver was a type, we have a class message; parse
166 // the rest of it.
167 if (!IsExpr) {
168 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
169 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
170 SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +0000171 ParsedType::getFromOpaquePtr(TypeOrExpr),
John McCall9ae2f072010-08-23 23:25:46 +0000172 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000173 }
174
175 // If the receiver was an expression, we still don't know
176 // whether we have a message send or an array designator; just
177 // adopt the expression for further analysis below.
178 // FIXME: potentially-potentially evaluated expression above?
John McCall60d7b3a2010-08-24 06:29:42 +0000179 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000180 } else if (getLang().ObjC1 && Tok.is(tok::identifier)) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000181 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor2725ca82010-04-21 19:57:20 +0000182 SourceLocation IILoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +0000183 ParsedType ReceiverType;
Chris Lattner1e461362010-04-12 06:36:00 +0000184 // Three cases. This is a message send to a type: [type foo]
185 // This is a message send to super: [super foo]
186 // This is a message sent to an expr: [super.bar foo]
John McCallf312b1e2010-08-26 23:41:50 +0000187 switch (Sema::ObjCMessageKind Kind
Douglas Gregor23c94db2010-07-02 17:43:08 +0000188 = Actions.getObjCMessageKind(getCurScope(), II, IILoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +0000189 II == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +0000190 NextToken().is(tok::period),
191 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +0000192 case Sema::ObjCSuperMessage:
193 case Sema::ObjCClassMessage:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000194 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
John McCallf312b1e2010-08-26 23:41:50 +0000195 if (Kind == Sema::ObjCSuperMessage)
Douglas Gregor2725ca82010-04-21 19:57:20 +0000196 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
197 ConsumeToken(),
John McCallb3d87482010-08-24 05:47:05 +0000198 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +0000199 0);
Douglas Gregor1569f952010-04-21 20:38:13 +0000200 ConsumeToken(); // the identifier
201 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000202 SkipUntil(tok::r_square);
203 return ExprError();
204 }
205
206 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
207 SourceLocation(),
Douglas Gregor1569f952010-04-21 20:38:13 +0000208 ReceiverType,
John McCall9ae2f072010-08-23 23:25:46 +0000209 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +0000210
John McCallf312b1e2010-08-26 23:41:50 +0000211 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +0000212 // Fall through; we'll just parse the expression and
213 // (possibly) treat this like an Objective-C message send
214 // later.
215 break;
Chris Lattnereb483eb2010-04-11 08:28:14 +0000216 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000217 }
Sebastian Redl1d922962008-12-13 15:32:12 +0000218
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000219 // Parse the index expression, if we haven't already gotten one
220 // above (which can only happen in Objective-C++).
Chris Lattner7f9690d2008-10-26 22:49:49 +0000221 // Note that we parse this as an assignment expression, not a constant
222 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
223 // to validate that the expression is a constant.
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000224 // FIXME: We also need to tell Sema that we're in a
225 // potentially-potentially evaluated context.
226 if (!Idx.get()) {
227 Idx = ParseAssignmentExpression();
228 if (Idx.isInvalid()) {
229 SkipUntil(tok::r_square);
230 return move(Idx);
231 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000232 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattner7f9690d2008-10-26 22:49:49 +0000234 // Given an expression, we could either have a designator (if the next
235 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump1eb44332009-09-09 15:08:12 +0000236 // message send, handle it now. An objc-message send is the start of
Chris Lattner7f9690d2008-10-26 22:49:49 +0000237 // an assignment-expression production.
Mike Stump1eb44332009-09-09 15:08:12 +0000238 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattner7f9690d2008-10-26 22:49:49 +0000239 Tok.isNot(tok::r_square)) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000240 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
Sebastian Redl1d922962008-12-13 15:32:12 +0000241 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
242 SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +0000243 ParsedType(),
244 Idx.take());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000245 }
Chris Lattnere2329422008-10-26 23:06:54 +0000246
Chris Lattnere2329422008-10-26 23:06:54 +0000247 // If this is a normal array designator, remember it.
248 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor5908a922009-03-20 23:11:49 +0000249 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattnere2329422008-10-26 23:06:54 +0000250 } else {
251 // Handle the gnu array range extension.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000252 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000253 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000254
John McCall60d7b3a2010-08-24 06:29:42 +0000255 ExprResult RHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000256 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000258 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 }
Douglas Gregor5908a922009-03-20 23:11:49 +0000260 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
261 RHS.release(),
262 StartLoc, EllipsisLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000264
Douglas Gregor05c13a32009-01-22 00:58:24 +0000265 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Douglas Gregor5908a922009-03-20 23:11:49 +0000266 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000268
Chris Lattner0a68b942008-10-26 22:59:19 +0000269 // Okay, we're done with the designator sequence. We know that there must be
270 // at least one designator, because the only case we can get into this method
271 // without a designator is when we have an objc message send. That case is
272 // handled and returned from above.
Douglas Gregor5908a922009-03-20 23:11:49 +0000273 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000274
Chris Lattner0a68b942008-10-26 22:59:19 +0000275 // Handle a normal designator sequence end, which is an equal.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000276 if (Tok.is(tok::equal)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000277 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor5908a922009-03-20 23:11:49 +0000278 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000279 ParseInitializer());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000280 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000281
Chris Lattner0a68b942008-10-26 22:59:19 +0000282 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000283 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000284 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
285 // parse error.
Mike Stump1eb44332009-09-09 15:08:12 +0000286 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000287 (Desig.getDesignator(0).isArrayDesignator() ||
288 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000289 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +0000290 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000291 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor68c56de2009-03-27 23:40:29 +0000292 true, ParseInitializer());
Chris Lattner79ed6b52008-10-26 23:22:23 +0000293 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000294
Chris Lattner79ed6b52008-10-26 23:22:23 +0000295 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000296 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000297}
298
299
Chris Lattner0eec2b52008-10-26 22:38:55 +0000300/// ParseBraceInitializer - Called when parsing an initializer that has a
301/// leading open brace.
302///
Reid Spencer5f016e22007-07-11 17:01:13 +0000303/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000304/// '{' initializer-list '}'
305/// '{' initializer-list ',' '}'
306/// [GNU] '{' '}'
307///
308/// initializer-list:
309/// designation[opt] initializer
310/// initializer-list ',' designation[opt] initializer
311///
John McCall60d7b3a2010-08-24 06:29:42 +0000312ExprResult Parser::ParseBraceInitializer() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 SourceLocation LBraceLoc = ConsumeBrace();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000314
Chris Lattnereccc53a2008-10-26 22:36:07 +0000315 /// InitExprs - This is the actual list of expressions contained in the
316 /// initializer.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000317 ExprVector InitExprs(Actions);
318
Chris Lattner220ad7c2008-10-26 23:35:51 +0000319 if (Tok.is(tok::r_brace)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000320 // Empty initializers are a C++ feature and a GNU extension to C.
321 if (!getLang().CPlusPlus)
322 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner220ad7c2008-10-26 23:35:51 +0000323 // Match the '}'.
John McCallf312b1e2010-08-26 23:41:50 +0000324 return Actions.ActOnInitList(LBraceLoc, MultiExprArg(Actions),
Douglas Gregor5908a922009-03-20 23:11:49 +0000325 ConsumeBrace());
Chris Lattner220ad7c2008-10-26 23:35:51 +0000326 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000327
Steve Naroff4aa88f82007-07-19 01:06:55 +0000328 bool InitExprsOk = true;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000329
Steve Naroff4aa88f82007-07-19 01:06:55 +0000330 while (1) {
331 // Parse: designation[opt] initializer
Sebastian Redl20df9b72008-12-11 22:51:44 +0000332
Steve Naroff4aa88f82007-07-19 01:06:55 +0000333 // If we know that this cannot be a designation, just parse the nested
334 // initializer directly.
John McCall60d7b3a2010-08-24 06:29:42 +0000335 ExprResult SubElt;
Douglas Gregor5908a922009-03-20 23:11:49 +0000336 if (MayBeDesignationStart(Tok.getKind(), PP))
337 SubElt = ParseInitializerWithPotentialDesignator();
338 else
Steve Naroff4aa88f82007-07-19 01:06:55 +0000339 SubElt = ParseInitializer();
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Steve Naroff4aa88f82007-07-19 01:06:55 +0000341 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000342 if (!SubElt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000343 InitExprs.push_back(SubElt.release());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000344 } else {
345 InitExprsOk = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattner65bb89c2008-04-20 19:07:56 +0000347 // We have two ways to try to recover from this error: if the code looks
Chris Lattner838cb212008-10-26 21:46:13 +0000348 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000349 // parsing the rest of the initializer. This allows us to emit
350 // diagnostics for later elements that we find. If we don't see a comma,
351 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000352 // FIXME: This comment doesn't sound right. If there is a r_brace
353 // immediately, it can't be an error, since there is no other way of
354 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000355 if (Tok.isNot(tok::comma)) {
356 SkipUntil(tok::r_brace, false, true);
357 break;
358 }
359 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000360
Steve Naroff4aa88f82007-07-19 01:06:55 +0000361 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000362 if (Tok.isNot(tok::comma)) break;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000363
Chris Lattnereccc53a2008-10-26 22:36:07 +0000364 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000365 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000366
Steve Naroff4aa88f82007-07-19 01:06:55 +0000367 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000368 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000369 }
Chris Lattner04d66662007-10-09 17:33:22 +0000370 if (InitExprsOk && Tok.is(tok::r_brace))
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000371 return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
Douglas Gregor5908a922009-03-20 23:11:49 +0000372 ConsumeBrace());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000373
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 // Match the '}'.
375 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000376 return ExprError(); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000377}
378