blob: b0735b3cab3c1664a562e036b9ad550b68034d73 [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
37/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
38/// checking to see if the token stream starts with a designator.
39///
40/// designation:
41/// designator-list '='
42/// [GNU] array-designator
43/// [GNU] identifier ':'
44///
45/// designator-list:
46/// designator
47/// designator-list designator
48///
49/// designator:
50/// array-designator
51/// '.' identifier
52///
53/// array-designator:
54/// '[' constant-expression ']'
55/// [GNU] '[' constant-expression '...' constant-expression ']'
56///
57/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattner838cb212008-10-26 21:46:13 +000058/// initializer (because it is an expression). We need to consider this case
59/// when parsing array designators.
Reid Spencer5f016e22007-07-11 17:01:13 +000060///
Douglas Gregor5908a922009-03-20 23:11:49 +000061Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redl20df9b72008-12-11 22:51:44 +000062
Chris Lattnereccc53a2008-10-26 22:36:07 +000063 // If this is the old-style GNU extension:
64 // designation ::= identifier ':'
65 // Handle it as a field designator. Otherwise, this must be the start of a
66 // normal expression.
67 if (Tok.is(tok::identifier)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +000068 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregoreeae8f02009-03-28 00:41:23 +000069
Daniel Dunbar62a72172009-10-17 23:52:50 +000070 llvm::SmallString<256> NewSyntax;
Daniel Dunbar01eb9b92009-10-18 21:17:35 +000071 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbar62a72172009-10-17 23:52:50 +000072 << " = ";
Douglas Gregoreeae8f02009-03-28 00:41:23 +000073
Douglas Gregor05c13a32009-01-22 00:58:24 +000074 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +000075
Chris Lattner7f9690d2008-10-26 22:49:49 +000076 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregor05c13a32009-01-22 00:58:24 +000077 SourceLocation ColonLoc = ConsumeToken();
78
Douglas Gregoreeae8f02009-03-28 00:41:23 +000079 Diag(Tok, diag::ext_gnu_old_style_field_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +000080 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
81 NewSyntax.str());
Douglas Gregoreeae8f02009-03-28 00:41:23 +000082
Douglas Gregor5908a922009-03-20 23:11:49 +000083 Designation D;
Douglas Gregor05c13a32009-01-22 00:58:24 +000084 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump1eb44332009-09-09 15:08:12 +000085 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregor05c13a32009-01-22 00:58:24 +000086 ParseInitializer());
Chris Lattnereccc53a2008-10-26 22:36:07 +000087 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner0a68b942008-10-26 22:59:19 +000089 // Desig - This is initialized when we see our first designator. We may have
90 // an objc message send with no designator, so we don't want to create this
91 // eagerly.
Douglas Gregor5908a922009-03-20 23:11:49 +000092 Designation Desig;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Reid Spencer5f016e22007-07-11 17:01:13 +000094 // Parse each designator in the designator list until we find an initializer.
Chris Lattner7f9690d2008-10-26 22:49:49 +000095 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
96 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000097 // designator: '.' identifier
Douglas Gregor05c13a32009-01-22 00:58:24 +000098 SourceLocation DotLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000099
Chris Lattner0a68b942008-10-26 22:59:19 +0000100 if (Tok.isNot(tok::identifier)) {
101 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000102 return ExprError();
Chris Lattner0a68b942008-10-26 22:59:19 +0000103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Douglas Gregor5908a922009-03-20 23:11:49 +0000105 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
106 Tok.getLocation()));
Chris Lattner0a68b942008-10-26 22:59:19 +0000107 ConsumeToken(); // Eat the identifier.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000108 continue;
109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Chris Lattner7f9690d2008-10-26 22:49:49 +0000111 // We must have either an array designator now or an objc message send.
112 assert(Tok.is(tok::l_square) && "Unexpected token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattnere2329422008-10-26 23:06:54 +0000114 // Handle the two forms of array designator:
115 // array-designator: '[' constant-expression ']'
116 // array-designator: '[' constant-expression '...' constant-expression ']'
117 //
118 // Also, we have to handle the case where the expression after the
119 // designator an an objc message send: '[' objc-message-expr ']'.
120 // Interesting cases are:
121 // [foo bar] -> objc message send
122 // [foo] -> array designator
123 // [foo ... bar] -> array designator
124 // [4][foo bar] -> obsolete GNU designation with objc message send.
125 //
Chris Lattner7f9690d2008-10-26 22:49:49 +0000126 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Chris Lattnereb483eb2010-04-11 08:28:14 +0000128 // If Objective-C is enabled and this is a typename (class message send) or
Chris Lattner1e461362010-04-12 06:36:00 +0000129 // send to 'super', parse this as a message send expression.
Chris Lattnereb483eb2010-04-11 08:28:14 +0000130 if (getLang().ObjC1 && Tok.is(tok::identifier)) {
131 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor2725ca82010-04-21 19:57:20 +0000132 SourceLocation IILoc = Tok.getLocation();
Chris Lattner1e461362010-04-12 06:36:00 +0000133 // Three cases. This is a message send to a type: [type foo]
134 // This is a message send to super: [super foo]
135 // This is a message sent to an expr: [super.bar foo]
Douglas Gregor2725ca82010-04-21 19:57:20 +0000136 switch (Action::ObjCMessageKind Kind
137 = Actions.getObjCMessageKind(CurScope, II, IILoc,
138 II == Ident_super,
139 NextToken().is(tok::period))) {
140 case Action::ObjCSuperMessage:
141 case Action::ObjCClassMessage: {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000142 // If we have exactly one array designator, this used the GNU
143 // 'designation: array-designator' extension, otherwise there should be no
144 // designators at all!
145 if (Desig.getNumDesignators() == 1 &&
146 (Desig.getDesignator(0).isArrayDesignator() ||
147 Desig.getDesignator(0).isArrayRangeDesignator()))
148 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
149 else if (Desig.getNumDesignators() > 0)
150 Diag(Tok, diag::err_expected_equal_designator);
151
Douglas Gregor2725ca82010-04-21 19:57:20 +0000152 if (Kind == Action::ObjCSuperMessage)
153 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
154 ConsumeToken(),
155 0,
156 ExprArg(Actions));
157
158 // FIXME: This code is redundant with ParseObjCMessageExpr.
159 // Create the type that corresponds to the identifier (which
160 // names an Objective-C class).
161 TypeTy *Type = 0;
162 if (TypeTy *TyName = Actions.getTypeName(*II, IILoc, CurScope)) {
163 DeclSpec DS;
164 const char *PrevSpec = 0;
165 unsigned DiagID = 0;
166 if (!DS.SetTypeSpecType(DeclSpec::TST_typename, IILoc, PrevSpec,
167 DiagID, TyName)) {
168 DS.SetRangeEnd(IILoc);
169 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
170 TypeResult Ty = Actions.ActOnTypeName(CurScope, DeclaratorInfo);
171 if (!Ty.isInvalid())
172 Type = Ty.get();
173 }
174 }
175
176 ConsumeToken(); // The identifier.
177 if (!Type) {
178 SkipUntil(tok::r_square);
179 return ExprError();
180 }
181
182 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
183 SourceLocation(),
184 Type,
185 ExprArg(Actions));
186 }
187
188 case Action::ObjCInstanceMessage:
189 // Fall through; we'll just parse the expression and
190 // (possibly) treat this like an Objective-C message send
191 // later.
192 break;
Chris Lattnereb483eb2010-04-11 08:28:14 +0000193 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000194 }
Sebastian Redl1d922962008-12-13 15:32:12 +0000195
Chris Lattner7f9690d2008-10-26 22:49:49 +0000196 // Note that we parse this as an assignment expression, not a constant
197 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
198 // to validate that the expression is a constant.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000199 OwningExprResult Idx(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000200 if (Idx.isInvalid()) {
Chris Lattner7f9690d2008-10-26 22:49:49 +0000201 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000202 return move(Idx);
Chris Lattner7f9690d2008-10-26 22:49:49 +0000203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Chris Lattner7f9690d2008-10-26 22:49:49 +0000205 // Given an expression, we could either have a designator (if the next
206 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump1eb44332009-09-09 15:08:12 +0000207 // message send, handle it now. An objc-message send is the start of
Chris Lattner7f9690d2008-10-26 22:49:49 +0000208 // an assignment-expression production.
Mike Stump1eb44332009-09-09 15:08:12 +0000209 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattner7f9690d2008-10-26 22:49:49 +0000210 Tok.isNot(tok::r_square)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattner0fc73f72008-10-26 23:29:41 +0000212 // If we have exactly one array designator, this used the GNU
213 // 'designation: array-designator' extension, otherwise there should be no
214 // designators at all!
Mike Stump1eb44332009-09-09 15:08:12 +0000215 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000216 (Desig.getDesignator(0).isArrayDesignator() ||
217 Desig.getDesignator(0).isArrayRangeDesignator()))
218 Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
219 else if (Desig.getNumDesignators() > 0)
220 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000221
Sebastian Redl1d922962008-12-13 15:32:12 +0000222 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
223 SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000224 0, move(Idx));
Chris Lattner7f9690d2008-10-26 22:49:49 +0000225 }
Chris Lattnere2329422008-10-26 23:06:54 +0000226
Chris Lattnere2329422008-10-26 23:06:54 +0000227 // If this is a normal array designator, remember it.
228 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor5908a922009-03-20 23:11:49 +0000229 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattnere2329422008-10-26 23:06:54 +0000230 } else {
231 // Handle the gnu array range extension.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000232 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000233 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000234
235 OwningExprResult RHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000236 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 SkipUntil(tok::r_square);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000238 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 }
Douglas Gregor5908a922009-03-20 23:11:49 +0000240 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
241 RHS.release(),
242 StartLoc, EllipsisLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000244
Douglas Gregor05c13a32009-01-22 00:58:24 +0000245 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Douglas Gregor5908a922009-03-20 23:11:49 +0000246 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000248
Chris Lattner0a68b942008-10-26 22:59:19 +0000249 // Okay, we're done with the designator sequence. We know that there must be
250 // at least one designator, because the only case we can get into this method
251 // without a designator is when we have an objc message send. That case is
252 // handled and returned from above.
Douglas Gregor5908a922009-03-20 23:11:49 +0000253 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000254
Chris Lattner0a68b942008-10-26 22:59:19 +0000255 // Handle a normal designator sequence end, which is an equal.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000256 if (Tok.is(tok::equal)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000257 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor5908a922009-03-20 23:11:49 +0000258 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000259 ParseInitializer());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000260 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000261
Chris Lattner0a68b942008-10-26 22:59:19 +0000262 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000263 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000264 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
265 // parse error.
Mike Stump1eb44332009-09-09 15:08:12 +0000266 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000267 (Desig.getDesignator(0).isArrayDesignator() ||
268 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000269 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +0000270 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000271 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor68c56de2009-03-27 23:40:29 +0000272 true, ParseInitializer());
Chris Lattner79ed6b52008-10-26 23:22:23 +0000273 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000274
Chris Lattner79ed6b52008-10-26 23:22:23 +0000275 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000276 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000277}
278
279
Chris Lattner0eec2b52008-10-26 22:38:55 +0000280/// ParseBraceInitializer - Called when parsing an initializer that has a
281/// leading open brace.
282///
Reid Spencer5f016e22007-07-11 17:01:13 +0000283/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000284/// '{' initializer-list '}'
285/// '{' initializer-list ',' '}'
286/// [GNU] '{' '}'
287///
288/// initializer-list:
289/// designation[opt] initializer
290/// initializer-list ',' designation[opt] initializer
291///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000292Parser::OwningExprResult Parser::ParseBraceInitializer() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 SourceLocation LBraceLoc = ConsumeBrace();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000294
Chris Lattnereccc53a2008-10-26 22:36:07 +0000295 /// InitExprs - This is the actual list of expressions contained in the
296 /// initializer.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000297 ExprVector InitExprs(Actions);
298
Chris Lattner220ad7c2008-10-26 23:35:51 +0000299 if (Tok.is(tok::r_brace)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000300 // Empty initializers are a C++ feature and a GNU extension to C.
301 if (!getLang().CPlusPlus)
302 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner220ad7c2008-10-26 23:35:51 +0000303 // Match the '}'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000304 return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions),
Douglas Gregor5908a922009-03-20 23:11:49 +0000305 ConsumeBrace());
Chris Lattner220ad7c2008-10-26 23:35:51 +0000306 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000307
Steve Naroff4aa88f82007-07-19 01:06:55 +0000308 bool InitExprsOk = true;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000309
Steve Naroff4aa88f82007-07-19 01:06:55 +0000310 while (1) {
311 // Parse: designation[opt] initializer
Sebastian Redl20df9b72008-12-11 22:51:44 +0000312
Steve Naroff4aa88f82007-07-19 01:06:55 +0000313 // If we know that this cannot be a designation, just parse the nested
314 // initializer directly.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000315 OwningExprResult SubElt(Actions);
Douglas Gregor5908a922009-03-20 23:11:49 +0000316 if (MayBeDesignationStart(Tok.getKind(), PP))
317 SubElt = ParseInitializerWithPotentialDesignator();
318 else
Steve Naroff4aa88f82007-07-19 01:06:55 +0000319 SubElt = ParseInitializer();
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Steve Naroff4aa88f82007-07-19 01:06:55 +0000321 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000322 if (!SubElt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000323 InitExprs.push_back(SubElt.release());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000324 } else {
325 InitExprsOk = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Chris Lattner65bb89c2008-04-20 19:07:56 +0000327 // We have two ways to try to recover from this error: if the code looks
Chris Lattner838cb212008-10-26 21:46:13 +0000328 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000329 // parsing the rest of the initializer. This allows us to emit
330 // diagnostics for later elements that we find. If we don't see a comma,
331 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000332 // FIXME: This comment doesn't sound right. If there is a r_brace
333 // immediately, it can't be an error, since there is no other way of
334 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000335 if (Tok.isNot(tok::comma)) {
336 SkipUntil(tok::r_brace, false, true);
337 break;
338 }
339 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000340
Steve Naroff4aa88f82007-07-19 01:06:55 +0000341 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000342 if (Tok.isNot(tok::comma)) break;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000343
Chris Lattnereccc53a2008-10-26 22:36:07 +0000344 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000345 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000346
Steve Naroff4aa88f82007-07-19 01:06:55 +0000347 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000348 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000349 }
Chris Lattner04d66662007-10-09 17:33:22 +0000350 if (InitExprsOk && Tok.is(tok::r_brace))
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000351 return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
Douglas Gregor5908a922009-03-20 23:11:49 +0000352 ConsumeBrace());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000353
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 // Match the '}'.
355 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000356 return ExprError(); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000357}
358