blob: b0735b3cab3c1664a562e036b9ad550b68034d73 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
Chris Lattner8693a512006-08-13 21:54:02 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8693a512006-08-13 21:54:02 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements initializer parsing as specified by C99 6.7.8.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerf3e58e22008-10-26 22:36:07 +000014#include "clang/Parse/Designator.h"
Chris Lattner8693a512006-08-13 21:54:02 +000015#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner3adb17d2010-04-12 06:36:00 +000017#include "clang/Parse/Scope.h"
Steve Narofffbd09832007-07-19 01:06:55 +000018#include "llvm/ADT/SmallString.h"
Daniel Dunbarebd5b4a2009-10-17 23:52:50 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattner8693a512006-08-13 21:54:02 +000020using namespace clang;
21
22
23/// MayBeDesignationStart - Return true if this token might be the start of a
Chris Lattner0c024602008-10-26 21:46:13 +000024/// designator. If we can tell it is impossible that it is a designator, return
Mike Stump11289f42009-09-09 15:08:12 +000025/// false.
Chris Lattnerdde65432008-10-26 22:41:58 +000026static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
Chris Lattner8693a512006-08-13 21:54:02 +000027 switch (K) {
28 default: return false;
29 case tok::period: // designator: '.' identifier
30 case tok::l_square: // designator: array-designator
Chris Lattnerdde65432008-10-26 22:41:58 +000031 return true;
Chris Lattner8693a512006-08-13 21:54:02 +000032 case tok::identifier: // designation: identifier ':'
Chris Lattnerdde65432008-10-26 22:41:58 +000033 return PP.LookAhead(0).is(tok::colon);
Chris Lattner8693a512006-08-13 21:54:02 +000034 }
35}
36
Chris Lattnere7dab442006-08-13 21:54:51 +000037/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
38/// checking to see if the token stream starts with a designator.
39///
Chris Lattner8693a512006-08-13 21:54:02 +000040/// 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 Lattner0c024602008-10-26 21:46:13 +000058/// initializer (because it is an expression). We need to consider this case
59/// when parsing array designators.
Chris Lattner8693a512006-08-13 21:54:02 +000060///
Douglas Gregor85992cf2009-03-20 23:11:49 +000061Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redld65cea82008-12-11 22:51:44 +000062
Chris Lattnerf3e58e22008-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 Gregore4a0bb72009-01-22 00:58:24 +000068 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +000069
Daniel Dunbarebd5b4a2009-10-17 23:52:50 +000070 llvm::SmallString<256> NewSyntax;
Daniel Dunbar07d07852009-10-18 21:17:35 +000071 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbarebd5b4a2009-10-17 23:52:50 +000072 << " = ";
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +000073
Douglas Gregore4a0bb72009-01-22 00:58:24 +000074 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +000075
Chris Lattnere2b5e872008-10-26 22:49:49 +000076 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregore4a0bb72009-01-22 00:58:24 +000077 SourceLocation ColonLoc = ConsumeToken();
78
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +000079 Diag(Tok, diag::ext_gnu_old_style_field_designator)
Douglas Gregora771f462010-03-31 17:46:05 +000080 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
81 NewSyntax.str());
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +000082
Douglas Gregor85992cf2009-03-20 23:11:49 +000083 Designation D;
Douglas Gregore4a0bb72009-01-22 00:58:24 +000084 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump11289f42009-09-09 15:08:12 +000085 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregore4a0bb72009-01-22 00:58:24 +000086 ParseInitializer());
Chris Lattnerf3e58e22008-10-26 22:36:07 +000087 }
Mike Stump11289f42009-09-09 15:08:12 +000088
Chris Lattner72432452008-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 Gregor85992cf2009-03-20 23:11:49 +000092 Designation Desig;
Mike Stump11289f42009-09-09 15:08:12 +000093
Chris Lattner8693a512006-08-13 21:54:02 +000094 // Parse each designator in the designator list until we find an initializer.
Chris Lattnere2b5e872008-10-26 22:49:49 +000095 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
96 if (Tok.is(tok::period)) {
Chris Lattner8693a512006-08-13 21:54:02 +000097 // designator: '.' identifier
Douglas Gregore4a0bb72009-01-22 00:58:24 +000098 SourceLocation DotLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000099
Chris Lattner72432452008-10-26 22:59:19 +0000100 if (Tok.isNot(tok::identifier)) {
101 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000102 return ExprError();
Chris Lattner72432452008-10-26 22:59:19 +0000103 }
Mike Stump11289f42009-09-09 15:08:12 +0000104
Douglas Gregor85992cf2009-03-20 23:11:49 +0000105 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
106 Tok.getLocation()));
Chris Lattner72432452008-10-26 22:59:19 +0000107 ConsumeToken(); // Eat the identifier.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000108 continue;
109 }
Mike Stump11289f42009-09-09 15:08:12 +0000110
Chris Lattnere2b5e872008-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 Stump11289f42009-09-09 15:08:12 +0000113
Chris Lattner8aafd352008-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 Lattnere2b5e872008-10-26 22:49:49 +0000126 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +0000127
Chris Lattnera36ec422010-04-11 08:28:14 +0000128 // If Objective-C is enabled and this is a typename (class message send) or
Chris Lattner3adb17d2010-04-12 06:36:00 +0000129 // send to 'super', parse this as a message send expression.
Chris Lattnera36ec422010-04-11 08:28:14 +0000130 if (getLang().ObjC1 && Tok.is(tok::identifier)) {
131 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000132 SourceLocation IILoc = Tok.getLocation();
Chris Lattner3adb17d2010-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 Gregor0c78ad92010-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 Lattnera36ec422010-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 Gregor0c78ad92010-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 Lattnera36ec422010-04-11 08:28:14 +0000193 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000194 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000195
Chris Lattnere2b5e872008-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 Redl59b5e512008-12-11 21:36:32 +0000199 OwningExprResult Idx(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000200 if (Idx.isInvalid()) {
Chris Lattnere2b5e872008-10-26 22:49:49 +0000201 SkipUntil(tok::r_square);
Sebastian Redld65cea82008-12-11 22:51:44 +0000202 return move(Idx);
Chris Lattnere2b5e872008-10-26 22:49:49 +0000203 }
Mike Stump11289f42009-09-09 15:08:12 +0000204
Chris Lattnere2b5e872008-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 Stump11289f42009-09-09 15:08:12 +0000207 // message send, handle it now. An objc-message send is the start of
Chris Lattnere2b5e872008-10-26 22:49:49 +0000208 // an assignment-expression production.
Mike Stump11289f42009-09-09 15:08:12 +0000209 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattnere2b5e872008-10-26 22:49:49 +0000210 Tok.isNot(tok::r_square)) {
Mike Stump11289f42009-09-09 15:08:12 +0000211
Chris Lattner9a53fdc2008-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 Stump11289f42009-09-09 15:08:12 +0000215 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor85992cf2009-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 Redld65cea82008-12-11 22:51:44 +0000221
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000222 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
223 SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000224 0, move(Idx));
Chris Lattnere2b5e872008-10-26 22:49:49 +0000225 }
Chris Lattner8aafd352008-10-26 23:06:54 +0000226
Chris Lattner8aafd352008-10-26 23:06:54 +0000227 // If this is a normal array designator, remember it.
228 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor85992cf2009-03-20 23:11:49 +0000229 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattner8aafd352008-10-26 23:06:54 +0000230 } else {
231 // Handle the gnu array range extension.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000232 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000233 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000234
235 OwningExprResult RHS(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000236 if (RHS.isInvalid()) {
Chris Lattner8693a512006-08-13 21:54:02 +0000237 SkipUntil(tok::r_square);
Sebastian Redld65cea82008-12-11 22:51:44 +0000238 return move(RHS);
Chris Lattner8693a512006-08-13 21:54:02 +0000239 }
Douglas Gregor85992cf2009-03-20 23:11:49 +0000240 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
241 RHS.release(),
242 StartLoc, EllipsisLoc));
Chris Lattner8693a512006-08-13 21:54:02 +0000243 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000244
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000245 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Douglas Gregor85992cf2009-03-20 23:11:49 +0000246 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc);
Chris Lattner8693a512006-08-13 21:54:02 +0000247 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000248
Chris Lattner72432452008-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 Gregor85992cf2009-03-20 23:11:49 +0000253 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redld65cea82008-12-11 22:51:44 +0000254
Chris Lattner72432452008-10-26 22:59:19 +0000255 // Handle a normal designator sequence end, which is an equal.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000256 if (Tok.is(tok::equal)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000257 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor85992cf2009-03-20 23:11:49 +0000258 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000259 ParseInitializer());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000260 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000261
Chris Lattner72432452008-10-26 22:59:19 +0000262 // We read some number of designators and found something that isn't an = or
Chris Lattner46dcba62008-10-26 23:22:23 +0000263 // an initializer. If we have exactly one array designator, this
Chris Lattner72432452008-10-26 22:59:19 +0000264 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
265 // parse error.
Mike Stump11289f42009-09-09 15:08:12 +0000266 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor85992cf2009-03-20 23:11:49 +0000267 (Desig.getDesignator(0).isArrayDesignator() ||
268 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000269 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregora771f462010-03-31 17:46:05 +0000270 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000271 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor8aa6bf52009-03-27 23:40:29 +0000272 true, ParseInitializer());
Chris Lattner46dcba62008-10-26 23:22:23 +0000273 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000274
Chris Lattner46dcba62008-10-26 23:22:23 +0000275 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000276 return ExprError();
Chris Lattner8693a512006-08-13 21:54:02 +0000277}
278
279
Chris Lattner3fa92392008-10-26 22:38:55 +0000280/// ParseBraceInitializer - Called when parsing an initializer that has a
281/// leading open brace.
282///
Chris Lattner8693a512006-08-13 21:54:02 +0000283/// initializer: [C99 6.7.8]
Chris Lattner8693a512006-08-13 21:54:02 +0000284/// '{' initializer-list '}'
285/// '{' initializer-list ',' '}'
286/// [GNU] '{' '}'
287///
288/// initializer-list:
289/// designation[opt] initializer
290/// initializer-list ',' designation[opt] initializer
291///
Sebastian Redld65cea82008-12-11 22:51:44 +0000292Parser::OwningExprResult Parser::ParseBraceInitializer() {
Chris Lattner04132372006-10-16 06:12:55 +0000293 SourceLocation LBraceLoc = ConsumeBrace();
Sebastian Redl511ed552008-11-25 22:21:31 +0000294
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000295 /// InitExprs - This is the actual list of expressions contained in the
296 /// initializer.
Sebastian Redl511ed552008-11-25 22:21:31 +0000297 ExprVector InitExprs(Actions);
298
Chris Lattner248388e2008-10-26 23:35:51 +0000299 if (Tok.is(tok::r_brace)) {
Douglas Gregord14247a2009-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 Lattner248388e2008-10-26 23:35:51 +0000303 // Match the '}'.
Sebastian Redlb5d49352009-01-19 22:31:54 +0000304 return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions),
Douglas Gregor85992cf2009-03-20 23:11:49 +0000305 ConsumeBrace());
Chris Lattner248388e2008-10-26 23:35:51 +0000306 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000307
Steve Narofffbd09832007-07-19 01:06:55 +0000308 bool InitExprsOk = true;
Sebastian Redld65cea82008-12-11 22:51:44 +0000309
Steve Narofffbd09832007-07-19 01:06:55 +0000310 while (1) {
311 // Parse: designation[opt] initializer
Sebastian Redld65cea82008-12-11 22:51:44 +0000312
Steve Narofffbd09832007-07-19 01:06:55 +0000313 // If we know that this cannot be a designation, just parse the nested
314 // initializer directly.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000315 OwningExprResult SubElt(Actions);
Douglas Gregor85992cf2009-03-20 23:11:49 +0000316 if (MayBeDesignationStart(Tok.getKind(), PP))
317 SubElt = ParseInitializerWithPotentialDesignator();
318 else
Steve Narofffbd09832007-07-19 01:06:55 +0000319 SubElt = ParseInitializer();
Mike Stump11289f42009-09-09 15:08:12 +0000320
Steve Narofffbd09832007-07-19 01:06:55 +0000321 // If we couldn't parse the subelement, bail out.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000322 if (!SubElt.isInvalid()) {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000323 InitExprs.push_back(SubElt.release());
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000324 } else {
325 InitExprsOk = false;
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000327 // We have two ways to try to recover from this error: if the code looks
Chris Lattner0c024602008-10-26 21:46:13 +0000328 // gramatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner14cd1ee2008-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 Redl511ed552008-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 Lattner14cd1ee2008-04-20 19:07:56 +0000335 if (Tok.isNot(tok::comma)) {
336 SkipUntil(tok::r_brace, false, true);
337 break;
338 }
339 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000340
Steve Narofffbd09832007-07-19 01:06:55 +0000341 // If we don't have a comma continued list, we're done.
Chris Lattner76c72282007-10-09 17:33:22 +0000342 if (Tok.isNot(tok::comma)) break;
Sebastian Redld65cea82008-12-11 22:51:44 +0000343
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000344 // TODO: save comma locations if some client cares.
Steve Narofffbd09832007-07-19 01:06:55 +0000345 ConsumeToken();
Sebastian Redld65cea82008-12-11 22:51:44 +0000346
Steve Narofffbd09832007-07-19 01:06:55 +0000347 // Handle trailing comma.
Chris Lattner76c72282007-10-09 17:33:22 +0000348 if (Tok.is(tok::r_brace)) break;
Steve Narofffbd09832007-07-19 01:06:55 +0000349 }
Chris Lattner76c72282007-10-09 17:33:22 +0000350 if (InitExprsOk && Tok.is(tok::r_brace))
Sebastian Redlb5d49352009-01-19 22:31:54 +0000351 return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
Douglas Gregor85992cf2009-03-20 23:11:49 +0000352 ConsumeBrace());
Sebastian Redld65cea82008-12-11 22:51:44 +0000353
Chris Lattner8693a512006-08-13 21:54:02 +0000354 // Match the '}'.
Chris Lattner04f80192006-08-15 04:55:54 +0000355 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Sebastian Redld65cea82008-12-11 22:51:44 +0000356 return ExprError(); // an error occurred.
Chris Lattner8693a512006-08-13 21:54:02 +0000357}
358