blob: 3b967174bc5af0dd1067f7e97478abf14cf62ccb [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"
Douglas Gregor0fbda682010-09-15 14:51:05 +000015#include "RAIIObjectsForParser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/Parse/ParseDiagnostic.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
Douglas Gregorb3f323d2012-02-17 03:49:44 +000024/// MayBeDesignationStart - Return true if the current token might be the start
25/// of a designator. If we can tell it is impossible that it is a designator,
26/// return false.
27bool Parser::MayBeDesignationStart() {
28 switch (Tok.getKind()) {
29 default:
30 return false;
31
Reid Spencer5f016e22007-07-11 17:01:13 +000032 case tok::period: // designator: '.' identifier
Douglas Gregorb3f323d2012-02-17 03:49:44 +000033 return true;
34
35 case tok::l_square: { // designator: array-designator
Richard Smith80ad52f2013-01-02 11:42:31 +000036 if (!PP.getLangOpts().CPlusPlus11)
Chris Lattnerefcadc62008-10-26 22:41:58 +000037 return true;
Douglas Gregorb3f323d2012-02-17 03:49:44 +000038
39 // C++11 lambda expressions and C99 designators can be ambiguous all the
40 // way through the closing ']' and to the next character. Handle the easy
41 // cases here, and fall back to tentative parsing if those fail.
42 switch (PP.LookAhead(0).getKind()) {
43 case tok::equal:
44 case tok::r_square:
45 // Definitely starts a lambda expression.
46 return false;
47
48 case tok::amp:
49 case tok::kw_this:
50 case tok::identifier:
51 // We have to do additional analysis, because these could be the
52 // start of a constant expression or a lambda capture list.
53 break;
54
55 default:
56 // Anything not mentioned above cannot occur following a '[' in a
57 // lambda expression.
58 return true;
59 }
60
Douglas Gregord267b3f2012-02-17 16:41:16 +000061 // Handle the complicated case below.
62 break;
Douglas Gregorb3f323d2012-02-17 03:49:44 +000063 }
Reid Spencer5f016e22007-07-11 17:01:13 +000064 case tok::identifier: // designation: identifier ':'
Chris Lattnerefcadc62008-10-26 22:41:58 +000065 return PP.LookAhead(0).is(tok::colon);
Reid Spencer5f016e22007-07-11 17:01:13 +000066 }
Douglas Gregord267b3f2012-02-17 16:41:16 +000067
68 // Parse up to (at most) the token after the closing ']' to determine
69 // whether this is a C99 designator or a lambda.
70 TentativeParsingAction Tentative(*this);
71 ConsumeBracket();
72 while (true) {
73 switch (Tok.getKind()) {
74 case tok::equal:
75 case tok::amp:
76 case tok::identifier:
77 case tok::kw_this:
78 // These tokens can occur in a capture list or a constant-expression.
79 // Keep looking.
80 ConsumeToken();
81 continue;
82
83 case tok::comma:
84 // Since a comma cannot occur in a constant-expression, this must
85 // be a lambda.
86 Tentative.Revert();
87 return false;
88
89 case tok::r_square: {
90 // Once we hit the closing square bracket, we look at the next
91 // token. If it's an '=', this is a designator. Otherwise, it's a
92 // lambda expression. This decision favors lambdas over the older
93 // GNU designator syntax, which allows one to omit the '=', but is
94 // consistent with GCC.
95 ConsumeBracket();
96 tok::TokenKind Kind = Tok.getKind();
97 Tentative.Revert();
98 return Kind == tok::equal;
99 }
100
101 default:
102 // Anything else cannot occur in a lambda capture list, so it
103 // must be a designator.
104 Tentative.Revert();
105 return true;
106 }
107 }
108
109 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000110}
111
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000112static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
113 Designation &Desig) {
114 // If we have exactly one array designator, this used the GNU
115 // 'designation: array-designator' extension, otherwise there should be no
116 // designators at all!
117 if (Desig.getNumDesignators() == 1 &&
118 (Desig.getDesignator(0).isArrayDesignator() ||
119 Desig.getDesignator(0).isArrayRangeDesignator()))
120 P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
121 else if (Desig.getNumDesignators() > 0)
122 P.Diag(Loc, diag::err_expected_equal_designator);
123}
124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
126/// checking to see if the token stream starts with a designator.
127///
128/// designation:
129/// designator-list '='
130/// [GNU] array-designator
131/// [GNU] identifier ':'
132///
133/// designator-list:
134/// designator
135/// designator-list designator
136///
137/// designator:
138/// array-designator
139/// '.' identifier
140///
141/// array-designator:
142/// '[' constant-expression ']'
143/// [GNU] '[' constant-expression '...' constant-expression ']'
144///
145/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattner838cb212008-10-26 21:46:13 +0000146/// initializer (because it is an expression). We need to consider this case
147/// when parsing array designators.
Reid Spencer5f016e22007-07-11 17:01:13 +0000148///
John McCall60d7b3a2010-08-24 06:29:42 +0000149ExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redl20df9b72008-12-11 22:51:44 +0000150
Chris Lattnereccc53a2008-10-26 22:36:07 +0000151 // If this is the old-style GNU extension:
152 // designation ::= identifier ':'
153 // Handle it as a field designator. Otherwise, this must be the start of a
154 // normal expression.
155 if (Tok.is(tok::identifier)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000156 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000157
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000158 SmallString<256> NewSyntax;
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000159 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbar62a72172009-10-17 23:52:50 +0000160 << " = ";
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000161
Douglas Gregor05c13a32009-01-22 00:58:24 +0000162 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattner7f9690d2008-10-26 22:49:49 +0000164 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregor05c13a32009-01-22 00:58:24 +0000165 SourceLocation ColonLoc = ConsumeToken();
166
Douglas Gregor40a0f9c2011-08-27 00:13:16 +0000167 Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +0000168 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
169 NewSyntax.str());
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000170
Douglas Gregor5908a922009-03-20 23:11:49 +0000171 Designation D;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000172 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump1eb44332009-09-09 15:08:12 +0000173 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000174 ParseInitializer());
Chris Lattnereccc53a2008-10-26 22:36:07 +0000175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Chris Lattner0a68b942008-10-26 22:59:19 +0000177 // Desig - This is initialized when we see our first designator. We may have
178 // an objc message send with no designator, so we don't want to create this
179 // eagerly.
Douglas Gregor5908a922009-03-20 23:11:49 +0000180 Designation Desig;
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 // Parse each designator in the designator list until we find an initializer.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000183 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
184 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 // designator: '.' identifier
Douglas Gregor05c13a32009-01-22 00:58:24 +0000186 SourceLocation DotLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Chris Lattner0a68b942008-10-26 22:59:19 +0000188 if (Tok.isNot(tok::identifier)) {
189 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000190 return ExprError();
Chris Lattner0a68b942008-10-26 22:59:19 +0000191 }
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Douglas Gregor5908a922009-03-20 23:11:49 +0000193 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
194 Tok.getLocation()));
Chris Lattner0a68b942008-10-26 22:59:19 +0000195 ConsumeToken(); // Eat the identifier.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000196 continue;
197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Chris Lattner7f9690d2008-10-26 22:49:49 +0000199 // We must have either an array designator now or an objc message send.
200 assert(Tok.is(tok::l_square) && "Unexpected token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Chris Lattnere2329422008-10-26 23:06:54 +0000202 // Handle the two forms of array designator:
203 // array-designator: '[' constant-expression ']'
204 // array-designator: '[' constant-expression '...' constant-expression ']'
205 //
206 // Also, we have to handle the case where the expression after the
207 // designator an an objc message send: '[' objc-message-expr ']'.
208 // Interesting cases are:
209 // [foo bar] -> objc message send
210 // [foo] -> array designator
211 // [foo ... bar] -> array designator
212 // [4][foo bar] -> obsolete GNU designation with objc message send.
213 //
Richard Smith6ee326a2012-04-10 01:32:12 +0000214 // We do not need to check for an expression starting with [[ here. If it
215 // contains an Objective-C message send, then it is not an ill-formed
216 // attribute. If it is a lambda-expression within an array-designator, then
217 // it will be rejected because a constant-expression cannot begin with a
218 // lambda-expression.
Douglas Gregor0fbda682010-09-15 14:51:05 +0000219 InMessageExpressionRAIIObject InMessage(*this, true);
220
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000221 BalancedDelimiterTracker T(*this, tok::l_square);
222 T.consumeOpen();
223 SourceLocation StartLoc = T.getOpenLocation();
224
John McCall60d7b3a2010-08-24 06:29:42 +0000225 ExprResult Idx;
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000227 // If Objective-C is enabled and this is a typename (class message
228 // send) or send to 'super', parse this as a message send
229 // expression. We handle C++ and C separately, since C++ requires
230 // much more complicated parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +0000231 if (getLangOpts().ObjC1 && getLangOpts().CPlusPlus) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000232 // Send to 'super'.
233 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0fbda682010-09-15 14:51:05 +0000234 NextToken().isNot(tok::period) &&
235 getCurScope()->isInObjcMethodScope()) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000236 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
237 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
John McCallb3d87482010-08-24 05:47:05 +0000238 ConsumeToken(),
239 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +0000240 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000241 }
242
243 // Parse the receiver, which is either a type or an expression.
244 bool IsExpr;
245 void *TypeOrExpr;
246 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
247 SkipUntil(tok::r_square);
248 return ExprError();
249 }
250
251 // If the receiver was a type, we have a class message; parse
252 // the rest of it.
253 if (!IsExpr) {
254 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
255 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
256 SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +0000257 ParsedType::getFromOpaquePtr(TypeOrExpr),
John McCall9ae2f072010-08-23 23:25:46 +0000258 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000259 }
260
261 // If the receiver was an expression, we still don't know
262 // whether we have a message send or an array designator; just
263 // adopt the expression for further analysis below.
264 // FIXME: potentially-potentially evaluated expression above?
John McCall60d7b3a2010-08-24 06:29:42 +0000265 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
David Blaikie4e4d0842012-03-11 07:00:24 +0000266 } else if (getLangOpts().ObjC1 && Tok.is(tok::identifier)) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000267 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor2725ca82010-04-21 19:57:20 +0000268 SourceLocation IILoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +0000269 ParsedType ReceiverType;
Chris Lattner1e461362010-04-12 06:36:00 +0000270 // Three cases. This is a message send to a type: [type foo]
271 // This is a message send to super: [super foo]
272 // This is a message sent to an expr: [super.bar foo]
John McCallf312b1e2010-08-26 23:41:50 +0000273 switch (Sema::ObjCMessageKind Kind
Douglas Gregor23c94db2010-07-02 17:43:08 +0000274 = Actions.getObjCMessageKind(getCurScope(), II, IILoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +0000275 II == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +0000276 NextToken().is(tok::period),
277 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +0000278 case Sema::ObjCSuperMessage:
279 case Sema::ObjCClassMessage:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000280 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
John McCallf312b1e2010-08-26 23:41:50 +0000281 if (Kind == Sema::ObjCSuperMessage)
Douglas Gregor2725ca82010-04-21 19:57:20 +0000282 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
283 ConsumeToken(),
John McCallb3d87482010-08-24 05:47:05 +0000284 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +0000285 0);
Douglas Gregor1569f952010-04-21 20:38:13 +0000286 ConsumeToken(); // the identifier
287 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000288 SkipUntil(tok::r_square);
289 return ExprError();
290 }
291
292 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
293 SourceLocation(),
Douglas Gregor1569f952010-04-21 20:38:13 +0000294 ReceiverType,
John McCall9ae2f072010-08-23 23:25:46 +0000295 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +0000296
John McCallf312b1e2010-08-26 23:41:50 +0000297 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +0000298 // Fall through; we'll just parse the expression and
299 // (possibly) treat this like an Objective-C message send
300 // later.
301 break;
Chris Lattnereb483eb2010-04-11 08:28:14 +0000302 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000303 }
Sebastian Redl1d922962008-12-13 15:32:12 +0000304
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000305 // Parse the index expression, if we haven't already gotten one
306 // above (which can only happen in Objective-C++).
Chris Lattner7f9690d2008-10-26 22:49:49 +0000307 // Note that we parse this as an assignment expression, not a constant
308 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
309 // to validate that the expression is a constant.
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000310 // FIXME: We also need to tell Sema that we're in a
311 // potentially-potentially evaluated context.
312 if (!Idx.get()) {
313 Idx = ParseAssignmentExpression();
314 if (Idx.isInvalid()) {
315 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000316 return Idx;
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000317 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000318 }
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Chris Lattner7f9690d2008-10-26 22:49:49 +0000320 // Given an expression, we could either have a designator (if the next
321 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump1eb44332009-09-09 15:08:12 +0000322 // message send, handle it now. An objc-message send is the start of
Chris Lattner7f9690d2008-10-26 22:49:49 +0000323 // an assignment-expression production.
David Blaikie4e4d0842012-03-11 07:00:24 +0000324 if (getLangOpts().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattner7f9690d2008-10-26 22:49:49 +0000325 Tok.isNot(tok::r_square)) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000326 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
Sebastian Redl1d922962008-12-13 15:32:12 +0000327 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
328 SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +0000329 ParsedType(),
330 Idx.take());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000331 }
Chris Lattnere2329422008-10-26 23:06:54 +0000332
Chris Lattnere2329422008-10-26 23:06:54 +0000333 // If this is a normal array designator, remember it.
334 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor5908a922009-03-20 23:11:49 +0000335 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattnere2329422008-10-26 23:06:54 +0000336 } else {
337 // Handle the gnu array range extension.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000338 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000339 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000340
John McCall60d7b3a2010-08-24 06:29:42 +0000341 ExprResult RHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000342 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000344 return RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 }
Douglas Gregor5908a922009-03-20 23:11:49 +0000346 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
347 RHS.release(),
348 StartLoc, EllipsisLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000350
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000351 T.consumeClose();
352 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
353 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000355
Chris Lattner0a68b942008-10-26 22:59:19 +0000356 // Okay, we're done with the designator sequence. We know that there must be
357 // at least one designator, because the only case we can get into this method
358 // without a designator is when we have an objc message send. That case is
359 // handled and returned from above.
Douglas Gregor5908a922009-03-20 23:11:49 +0000360 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000361
Chris Lattner0a68b942008-10-26 22:59:19 +0000362 // Handle a normal designator sequence end, which is an equal.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000363 if (Tok.is(tok::equal)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000364 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor5908a922009-03-20 23:11:49 +0000365 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000366 ParseInitializer());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000367 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000368
Chris Lattner0a68b942008-10-26 22:59:19 +0000369 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000370 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000371 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
372 // parse error.
Mike Stump1eb44332009-09-09 15:08:12 +0000373 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000374 (Desig.getDesignator(0).isArrayDesignator() ||
375 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000376 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +0000377 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000378 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor68c56de2009-03-27 23:40:29 +0000379 true, ParseInitializer());
Chris Lattner79ed6b52008-10-26 23:22:23 +0000380 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000381
Chris Lattner79ed6b52008-10-26 23:22:23 +0000382 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000383 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000384}
385
386
Chris Lattner0eec2b52008-10-26 22:38:55 +0000387/// ParseBraceInitializer - Called when parsing an initializer that has a
388/// leading open brace.
389///
Reid Spencer5f016e22007-07-11 17:01:13 +0000390/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000391/// '{' initializer-list '}'
392/// '{' initializer-list ',' '}'
393/// [GNU] '{' '}'
394///
395/// initializer-list:
Douglas Gregordcaa1ca2011-01-03 19:31:53 +0000396/// designation[opt] initializer ...[opt]
397/// initializer-list ',' designation[opt] initializer ...[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000398///
John McCall60d7b3a2010-08-24 06:29:42 +0000399ExprResult Parser::ParseBraceInitializer() {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000400 InMessageExpressionRAIIObject InMessage(*this, false);
401
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000402 BalancedDelimiterTracker T(*this, tok::l_brace);
403 T.consumeOpen();
404 SourceLocation LBraceLoc = T.getOpenLocation();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000405
Chris Lattnereccc53a2008-10-26 22:36:07 +0000406 /// InitExprs - This is the actual list of expressions contained in the
407 /// initializer.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000408 ExprVector InitExprs;
Sebastian Redla55e52c2008-11-25 22:21:31 +0000409
Chris Lattner220ad7c2008-10-26 23:35:51 +0000410 if (Tok.is(tok::r_brace)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000411 // Empty initializers are a C++ feature and a GNU extension to C.
David Blaikie4e4d0842012-03-11 07:00:24 +0000412 if (!getLangOpts().CPlusPlus)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000413 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner220ad7c2008-10-26 23:35:51 +0000414 // Match the '}'.
Benjamin Kramer5354e772012-08-23 23:38:35 +0000415 return Actions.ActOnInitList(LBraceLoc, MultiExprArg(), ConsumeBrace());
Chris Lattner220ad7c2008-10-26 23:35:51 +0000416 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000417
Steve Naroff4aa88f82007-07-19 01:06:55 +0000418 bool InitExprsOk = true;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000419
Steve Naroff4aa88f82007-07-19 01:06:55 +0000420 while (1) {
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000421 // Handle Microsoft __if_exists/if_not_exists if necessary.
David Blaikie4e4d0842012-03-11 07:00:24 +0000422 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000423 Tok.is(tok::kw___if_not_exists))) {
424 if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
425 if (Tok.isNot(tok::comma)) break;
426 ConsumeToken();
427 }
428 if (Tok.is(tok::r_brace)) break;
429 continue;
430 }
431
Steve Naroff4aa88f82007-07-19 01:06:55 +0000432 // Parse: designation[opt] initializer
Sebastian Redl20df9b72008-12-11 22:51:44 +0000433
Steve Naroff4aa88f82007-07-19 01:06:55 +0000434 // If we know that this cannot be a designation, just parse the nested
435 // initializer directly.
John McCall60d7b3a2010-08-24 06:29:42 +0000436 ExprResult SubElt;
Douglas Gregorb3f323d2012-02-17 03:49:44 +0000437 if (MayBeDesignationStart())
Douglas Gregor5908a922009-03-20 23:11:49 +0000438 SubElt = ParseInitializerWithPotentialDesignator();
439 else
Steve Naroff4aa88f82007-07-19 01:06:55 +0000440 SubElt = ParseInitializer();
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Douglas Gregordcaa1ca2011-01-03 19:31:53 +0000442 if (Tok.is(tok::ellipsis))
443 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
444
Steve Naroff4aa88f82007-07-19 01:06:55 +0000445 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000446 if (!SubElt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000447 InitExprs.push_back(SubElt.release());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000448 } else {
449 InitExprsOk = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattner65bb89c2008-04-20 19:07:56 +0000451 // We have two ways to try to recover from this error: if the code looks
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000452 // grammatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000453 // parsing the rest of the initializer. This allows us to emit
454 // diagnostics for later elements that we find. If we don't see a comma,
455 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000456 // FIXME: This comment doesn't sound right. If there is a r_brace
457 // immediately, it can't be an error, since there is no other way of
458 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000459 if (Tok.isNot(tok::comma)) {
460 SkipUntil(tok::r_brace, false, true);
461 break;
462 }
463 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000464
Steve Naroff4aa88f82007-07-19 01:06:55 +0000465 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000466 if (Tok.isNot(tok::comma)) break;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000467
Chris Lattnereccc53a2008-10-26 22:36:07 +0000468 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000469 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000470
Steve Naroff4aa88f82007-07-19 01:06:55 +0000471 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000472 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000473 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000474
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000475 bool closed = !T.consumeClose();
476
477 if (InitExprsOk && closed)
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000478 return Actions.ActOnInitList(LBraceLoc, InitExprs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000479 T.getCloseLocation());
480
Sebastian Redl20df9b72008-12-11 22:51:44 +0000481 return ExprError(); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000482}
483
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000484
485// Return true if a comma (or closing brace) is necessary after the
486// __if_exists/if_not_exists statement.
487bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
488 bool &InitExprsOk) {
489 bool trailingComma = false;
490 IfExistsCondition Result;
491 if (ParseMicrosoftIfExistsCondition(Result))
492 return false;
493
494 BalancedDelimiterTracker Braces(*this, tok::l_brace);
495 if (Braces.consumeOpen()) {
496 Diag(Tok, diag::err_expected_lbrace);
497 return false;
498 }
499
500 switch (Result.Behavior) {
501 case IEB_Parse:
502 // Parse the declarations below.
503 break;
504
505 case IEB_Dependent:
506 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
507 << Result.IsIfExists;
508 // Fall through to skip.
509
510 case IEB_Skip:
511 Braces.skipToEnd();
512 return false;
513 }
514
515 while (Tok.isNot(tok::eof)) {
516 trailingComma = false;
517 // If we know that this cannot be a designation, just parse the nested
518 // initializer directly.
519 ExprResult SubElt;
Douglas Gregorb3f323d2012-02-17 03:49:44 +0000520 if (MayBeDesignationStart())
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000521 SubElt = ParseInitializerWithPotentialDesignator();
522 else
523 SubElt = ParseInitializer();
524
525 if (Tok.is(tok::ellipsis))
526 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
527
528 // If we couldn't parse the subelement, bail out.
529 if (!SubElt.isInvalid())
530 InitExprs.push_back(SubElt.release());
531 else
532 InitExprsOk = false;
533
534 if (Tok.is(tok::comma)) {
535 ConsumeToken();
536 trailingComma = true;
537 }
538
539 if (Tok.is(tok::r_brace))
540 break;
541 }
542
543 Braces.consumeClose();
544
545 return !trailingComma;
546}