blob: 44053f193bd42c334e922ee0926982613ef8e1eb [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 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000108}
109
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000110static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
111 Designation &Desig) {
112 // If we have exactly one array designator, this used the GNU
113 // 'designation: array-designator' extension, otherwise there should be no
114 // designators at all!
115 if (Desig.getNumDesignators() == 1 &&
116 (Desig.getDesignator(0).isArrayDesignator() ||
117 Desig.getDesignator(0).isArrayRangeDesignator()))
118 P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
119 else if (Desig.getNumDesignators() > 0)
120 P.Diag(Loc, diag::err_expected_equal_designator);
121}
122
Reid Spencer5f016e22007-07-11 17:01:13 +0000123/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
124/// checking to see if the token stream starts with a designator.
125///
126/// designation:
127/// designator-list '='
128/// [GNU] array-designator
129/// [GNU] identifier ':'
130///
131/// designator-list:
132/// designator
133/// designator-list designator
134///
135/// designator:
136/// array-designator
137/// '.' identifier
138///
139/// array-designator:
140/// '[' constant-expression ']'
141/// [GNU] '[' constant-expression '...' constant-expression ']'
142///
143/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattner838cb212008-10-26 21:46:13 +0000144/// initializer (because it is an expression). We need to consider this case
145/// when parsing array designators.
Reid Spencer5f016e22007-07-11 17:01:13 +0000146///
John McCall60d7b3a2010-08-24 06:29:42 +0000147ExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redl20df9b72008-12-11 22:51:44 +0000148
Chris Lattnereccc53a2008-10-26 22:36:07 +0000149 // If this is the old-style GNU extension:
150 // designation ::= identifier ':'
151 // Handle it as a field designator. Otherwise, this must be the start of a
152 // normal expression.
153 if (Tok.is(tok::identifier)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000154 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000155
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000156 SmallString<256> NewSyntax;
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000157 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbar62a72172009-10-17 23:52:50 +0000158 << " = ";
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000159
Douglas Gregor05c13a32009-01-22 00:58:24 +0000160 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Chris Lattner7f9690d2008-10-26 22:49:49 +0000162 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregor05c13a32009-01-22 00:58:24 +0000163 SourceLocation ColonLoc = ConsumeToken();
164
Douglas Gregor40a0f9c2011-08-27 00:13:16 +0000165 Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +0000166 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
167 NewSyntax.str());
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000168
Douglas Gregor5908a922009-03-20 23:11:49 +0000169 Designation D;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000170 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump1eb44332009-09-09 15:08:12 +0000171 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000172 ParseInitializer());
Chris Lattnereccc53a2008-10-26 22:36:07 +0000173 }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Chris Lattner0a68b942008-10-26 22:59:19 +0000175 // Desig - This is initialized when we see our first designator. We may have
176 // an objc message send with no designator, so we don't want to create this
177 // eagerly.
Douglas Gregor5908a922009-03-20 23:11:49 +0000178 Designation Desig;
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 // Parse each designator in the designator list until we find an initializer.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000181 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
182 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 // designator: '.' identifier
Douglas Gregor05c13a32009-01-22 00:58:24 +0000184 SourceLocation DotLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Chris Lattner0a68b942008-10-26 22:59:19 +0000186 if (Tok.isNot(tok::identifier)) {
187 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000188 return ExprError();
Chris Lattner0a68b942008-10-26 22:59:19 +0000189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Douglas Gregor5908a922009-03-20 23:11:49 +0000191 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
192 Tok.getLocation()));
Chris Lattner0a68b942008-10-26 22:59:19 +0000193 ConsumeToken(); // Eat the identifier.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000194 continue;
195 }
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Chris Lattner7f9690d2008-10-26 22:49:49 +0000197 // We must have either an array designator now or an objc message send.
198 assert(Tok.is(tok::l_square) && "Unexpected token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Chris Lattnere2329422008-10-26 23:06:54 +0000200 // Handle the two forms of array designator:
201 // array-designator: '[' constant-expression ']'
202 // array-designator: '[' constant-expression '...' constant-expression ']'
203 //
204 // Also, we have to handle the case where the expression after the
205 // designator an an objc message send: '[' objc-message-expr ']'.
206 // Interesting cases are:
207 // [foo bar] -> objc message send
208 // [foo] -> array designator
209 // [foo ... bar] -> array designator
210 // [4][foo bar] -> obsolete GNU designation with objc message send.
211 //
Richard Smith6ee326a2012-04-10 01:32:12 +0000212 // We do not need to check for an expression starting with [[ here. If it
213 // contains an Objective-C message send, then it is not an ill-formed
214 // attribute. If it is a lambda-expression within an array-designator, then
215 // it will be rejected because a constant-expression cannot begin with a
216 // lambda-expression.
Douglas Gregor0fbda682010-09-15 14:51:05 +0000217 InMessageExpressionRAIIObject InMessage(*this, true);
218
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000219 BalancedDelimiterTracker T(*this, tok::l_square);
220 T.consumeOpen();
221 SourceLocation StartLoc = T.getOpenLocation();
222
John McCall60d7b3a2010-08-24 06:29:42 +0000223 ExprResult Idx;
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000225 // If Objective-C is enabled and this is a typename (class message
226 // send) or send to 'super', parse this as a message send
227 // expression. We handle C++ and C separately, since C++ requires
228 // much more complicated parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +0000229 if (getLangOpts().ObjC1 && getLangOpts().CPlusPlus) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000230 // Send to 'super'.
231 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0fbda682010-09-15 14:51:05 +0000232 NextToken().isNot(tok::period) &&
233 getCurScope()->isInObjcMethodScope()) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000234 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
235 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
John McCallb3d87482010-08-24 05:47:05 +0000236 ConsumeToken(),
237 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +0000238 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000239 }
240
241 // Parse the receiver, which is either a type or an expression.
242 bool IsExpr;
243 void *TypeOrExpr;
244 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000245 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000246 return ExprError();
247 }
248
249 // If the receiver was a type, we have a class message; parse
250 // the rest of it.
251 if (!IsExpr) {
252 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
253 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
254 SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +0000255 ParsedType::getFromOpaquePtr(TypeOrExpr),
John McCall9ae2f072010-08-23 23:25:46 +0000256 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000257 }
258
259 // If the receiver was an expression, we still don't know
260 // whether we have a message send or an array designator; just
261 // adopt the expression for further analysis below.
262 // FIXME: potentially-potentially evaluated expression above?
John McCall60d7b3a2010-08-24 06:29:42 +0000263 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
David Blaikie4e4d0842012-03-11 07:00:24 +0000264 } else if (getLangOpts().ObjC1 && Tok.is(tok::identifier)) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000265 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor2725ca82010-04-21 19:57:20 +0000266 SourceLocation IILoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +0000267 ParsedType ReceiverType;
Chris Lattner1e461362010-04-12 06:36:00 +0000268 // Three cases. This is a message send to a type: [type foo]
269 // This is a message send to super: [super foo]
270 // This is a message sent to an expr: [super.bar foo]
John McCallf312b1e2010-08-26 23:41:50 +0000271 switch (Sema::ObjCMessageKind Kind
Douglas Gregor23c94db2010-07-02 17:43:08 +0000272 = Actions.getObjCMessageKind(getCurScope(), II, IILoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +0000273 II == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +0000274 NextToken().is(tok::period),
275 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +0000276 case Sema::ObjCSuperMessage:
277 case Sema::ObjCClassMessage:
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000278 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
John McCallf312b1e2010-08-26 23:41:50 +0000279 if (Kind == Sema::ObjCSuperMessage)
Douglas Gregor2725ca82010-04-21 19:57:20 +0000280 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
281 ConsumeToken(),
John McCallb3d87482010-08-24 05:47:05 +0000282 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +0000283 0);
Douglas Gregor1569f952010-04-21 20:38:13 +0000284 ConsumeToken(); // the identifier
285 if (!ReceiverType) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000286 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor2725ca82010-04-21 19:57:20 +0000287 return ExprError();
288 }
289
290 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
291 SourceLocation(),
Douglas Gregor1569f952010-04-21 20:38:13 +0000292 ReceiverType,
John McCall9ae2f072010-08-23 23:25:46 +0000293 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +0000294
John McCallf312b1e2010-08-26 23:41:50 +0000295 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +0000296 // Fall through; we'll just parse the expression and
297 // (possibly) treat this like an Objective-C message send
298 // later.
299 break;
Chris Lattnereb483eb2010-04-11 08:28:14 +0000300 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000301 }
Sebastian Redl1d922962008-12-13 15:32:12 +0000302
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000303 // Parse the index expression, if we haven't already gotten one
304 // above (which can only happen in Objective-C++).
Chris Lattner7f9690d2008-10-26 22:49:49 +0000305 // Note that we parse this as an assignment expression, not a constant
306 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
307 // to validate that the expression is a constant.
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000308 // FIXME: We also need to tell Sema that we're in a
309 // potentially-potentially evaluated context.
310 if (!Idx.get()) {
311 Idx = ParseAssignmentExpression();
312 if (Idx.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000313 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000314 return Idx;
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000315 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Chris Lattner7f9690d2008-10-26 22:49:49 +0000318 // Given an expression, we could either have a designator (if the next
319 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump1eb44332009-09-09 15:08:12 +0000320 // message send, handle it now. An objc-message send is the start of
Chris Lattner7f9690d2008-10-26 22:49:49 +0000321 // an assignment-expression production.
David Blaikie4e4d0842012-03-11 07:00:24 +0000322 if (getLangOpts().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattner7f9690d2008-10-26 22:49:49 +0000323 Tok.isNot(tok::r_square)) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000324 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
Sebastian Redl1d922962008-12-13 15:32:12 +0000325 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
326 SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +0000327 ParsedType(),
328 Idx.take());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000329 }
Chris Lattnere2329422008-10-26 23:06:54 +0000330
Chris Lattnere2329422008-10-26 23:06:54 +0000331 // If this is a normal array designator, remember it.
332 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor5908a922009-03-20 23:11:49 +0000333 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattnere2329422008-10-26 23:06:54 +0000334 } else {
335 // Handle the gnu array range extension.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000336 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000337 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000338
John McCall60d7b3a2010-08-24 06:29:42 +0000339 ExprResult RHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000340 if (RHS.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000341 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000342 return RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 }
Douglas Gregor5908a922009-03-20 23:11:49 +0000344 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
345 RHS.release(),
346 StartLoc, EllipsisLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000348
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000349 T.consumeClose();
350 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
351 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 }
Chris Lattner7f9690d2008-10-26 22:49:49 +0000353
Chris Lattner0a68b942008-10-26 22:59:19 +0000354 // Okay, we're done with the designator sequence. We know that there must be
355 // at least one designator, because the only case we can get into this method
356 // without a designator is when we have an objc message send. That case is
357 // handled and returned from above.
Douglas Gregor5908a922009-03-20 23:11:49 +0000358 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redl20df9b72008-12-11 22:51:44 +0000359
Chris Lattner0a68b942008-10-26 22:59:19 +0000360 // Handle a normal designator sequence end, which is an equal.
Chris Lattner7f9690d2008-10-26 22:49:49 +0000361 if (Tok.is(tok::equal)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000362 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor5908a922009-03-20 23:11:49 +0000363 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregor05c13a32009-01-22 00:58:24 +0000364 ParseInitializer());
Chris Lattner7f9690d2008-10-26 22:49:49 +0000365 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000366
Chris Lattner0a68b942008-10-26 22:59:19 +0000367 // We read some number of designators and found something that isn't an = or
Chris Lattner79ed6b52008-10-26 23:22:23 +0000368 // an initializer. If we have exactly one array designator, this
Chris Lattner0a68b942008-10-26 22:59:19 +0000369 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
370 // parse error.
Mike Stump1eb44332009-09-09 15:08:12 +0000371 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor5908a922009-03-20 23:11:49 +0000372 (Desig.getDesignator(0).isArrayDesignator() ||
373 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000374 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregor849b2432010-03-31 17:46:05 +0000375 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregoreeae8f02009-03-28 00:41:23 +0000376 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor68c56de2009-03-27 23:40:29 +0000377 true, ParseInitializer());
Chris Lattner79ed6b52008-10-26 23:22:23 +0000378 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000379
Chris Lattner79ed6b52008-10-26 23:22:23 +0000380 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000381 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000382}
383
384
Chris Lattner0eec2b52008-10-26 22:38:55 +0000385/// ParseBraceInitializer - Called when parsing an initializer that has a
386/// leading open brace.
387///
Reid Spencer5f016e22007-07-11 17:01:13 +0000388/// initializer: [C99 6.7.8]
Reid Spencer5f016e22007-07-11 17:01:13 +0000389/// '{' initializer-list '}'
390/// '{' initializer-list ',' '}'
391/// [GNU] '{' '}'
392///
393/// initializer-list:
Douglas Gregordcaa1ca2011-01-03 19:31:53 +0000394/// designation[opt] initializer ...[opt]
395/// initializer-list ',' designation[opt] initializer ...[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000396///
John McCall60d7b3a2010-08-24 06:29:42 +0000397ExprResult Parser::ParseBraceInitializer() {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000398 InMessageExpressionRAIIObject InMessage(*this, false);
399
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000400 BalancedDelimiterTracker T(*this, tok::l_brace);
401 T.consumeOpen();
402 SourceLocation LBraceLoc = T.getOpenLocation();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000403
Chris Lattnereccc53a2008-10-26 22:36:07 +0000404 /// InitExprs - This is the actual list of expressions contained in the
405 /// initializer.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000406 ExprVector InitExprs;
Sebastian Redla55e52c2008-11-25 22:21:31 +0000407
Chris Lattner220ad7c2008-10-26 23:35:51 +0000408 if (Tok.is(tok::r_brace)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000409 // Empty initializers are a C++ feature and a GNU extension to C.
David Blaikie4e4d0842012-03-11 07:00:24 +0000410 if (!getLangOpts().CPlusPlus)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000411 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner220ad7c2008-10-26 23:35:51 +0000412 // Match the '}'.
Dmitri Gribenko62ed8892013-05-05 20:40:26 +0000413 return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace());
Chris Lattner220ad7c2008-10-26 23:35:51 +0000414 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000415
Steve Naroff4aa88f82007-07-19 01:06:55 +0000416 bool InitExprsOk = true;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000417
Steve Naroff4aa88f82007-07-19 01:06:55 +0000418 while (1) {
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000419 // Handle Microsoft __if_exists/if_not_exists if necessary.
David Blaikie4e4d0842012-03-11 07:00:24 +0000420 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000421 Tok.is(tok::kw___if_not_exists))) {
422 if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
423 if (Tok.isNot(tok::comma)) break;
424 ConsumeToken();
425 }
426 if (Tok.is(tok::r_brace)) break;
427 continue;
428 }
429
Steve Naroff4aa88f82007-07-19 01:06:55 +0000430 // Parse: designation[opt] initializer
Sebastian Redl20df9b72008-12-11 22:51:44 +0000431
Steve Naroff4aa88f82007-07-19 01:06:55 +0000432 // If we know that this cannot be a designation, just parse the nested
433 // initializer directly.
John McCall60d7b3a2010-08-24 06:29:42 +0000434 ExprResult SubElt;
Douglas Gregorb3f323d2012-02-17 03:49:44 +0000435 if (MayBeDesignationStart())
Douglas Gregor5908a922009-03-20 23:11:49 +0000436 SubElt = ParseInitializerWithPotentialDesignator();
437 else
Steve Naroff4aa88f82007-07-19 01:06:55 +0000438 SubElt = ParseInitializer();
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Douglas Gregordcaa1ca2011-01-03 19:31:53 +0000440 if (Tok.is(tok::ellipsis))
441 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
442
Steve Naroff4aa88f82007-07-19 01:06:55 +0000443 // If we couldn't parse the subelement, bail out.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000444 if (!SubElt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000445 InitExprs.push_back(SubElt.release());
Chris Lattner65bb89c2008-04-20 19:07:56 +0000446 } else {
447 InitExprsOk = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Chris Lattner65bb89c2008-04-20 19:07:56 +0000449 // We have two ways to try to recover from this error: if the code looks
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000450 // grammatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner65bb89c2008-04-20 19:07:56 +0000451 // parsing the rest of the initializer. This allows us to emit
452 // diagnostics for later elements that we find. If we don't see a comma,
453 // assume there is a parse error, and just skip to recover.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000454 // FIXME: This comment doesn't sound right. If there is a r_brace
455 // immediately, it can't be an error, since there is no other way of
456 // leaving this loop except through this if.
Chris Lattner65bb89c2008-04-20 19:07:56 +0000457 if (Tok.isNot(tok::comma)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000458 SkipUntil(tok::r_brace, StopBeforeMatch);
Chris Lattner65bb89c2008-04-20 19:07:56 +0000459 break;
460 }
461 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000462
Steve Naroff4aa88f82007-07-19 01:06:55 +0000463 // If we don't have a comma continued list, we're done.
Chris Lattner04d66662007-10-09 17:33:22 +0000464 if (Tok.isNot(tok::comma)) break;
Sebastian Redl20df9b72008-12-11 22:51:44 +0000465
Chris Lattnereccc53a2008-10-26 22:36:07 +0000466 // TODO: save comma locations if some client cares.
Steve Naroff4aa88f82007-07-19 01:06:55 +0000467 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000468
Steve Naroff4aa88f82007-07-19 01:06:55 +0000469 // Handle trailing comma.
Chris Lattner04d66662007-10-09 17:33:22 +0000470 if (Tok.is(tok::r_brace)) break;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000471 }
Sebastian Redl20df9b72008-12-11 22:51:44 +0000472
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000473 bool closed = !T.consumeClose();
474
475 if (InitExprsOk && closed)
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000476 return Actions.ActOnInitList(LBraceLoc, InitExprs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000477 T.getCloseLocation());
478
Sebastian Redl20df9b72008-12-11 22:51:44 +0000479 return ExprError(); // an error occurred.
Reid Spencer5f016e22007-07-11 17:01:13 +0000480}
481
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000482
483// Return true if a comma (or closing brace) is necessary after the
484// __if_exists/if_not_exists statement.
485bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
486 bool &InitExprsOk) {
487 bool trailingComma = false;
488 IfExistsCondition Result;
489 if (ParseMicrosoftIfExistsCondition(Result))
490 return false;
491
492 BalancedDelimiterTracker Braces(*this, tok::l_brace);
493 if (Braces.consumeOpen()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700494 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000495 return false;
496 }
497
498 switch (Result.Behavior) {
499 case IEB_Parse:
500 // Parse the declarations below.
501 break;
502
503 case IEB_Dependent:
504 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
505 << Result.IsIfExists;
506 // Fall through to skip.
507
508 case IEB_Skip:
509 Braces.skipToEnd();
510 return false;
511 }
512
Stephen Hines651f13c2014-04-23 16:59:28 -0700513 while (!isEofOrEom()) {
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000514 trailingComma = false;
515 // If we know that this cannot be a designation, just parse the nested
516 // initializer directly.
517 ExprResult SubElt;
Douglas Gregorb3f323d2012-02-17 03:49:44 +0000518 if (MayBeDesignationStart())
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000519 SubElt = ParseInitializerWithPotentialDesignator();
520 else
521 SubElt = ParseInitializer();
522
523 if (Tok.is(tok::ellipsis))
524 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
525
526 // If we couldn't parse the subelement, bail out.
527 if (!SubElt.isInvalid())
528 InitExprs.push_back(SubElt.release());
529 else
530 InitExprsOk = false;
531
532 if (Tok.is(tok::comma)) {
533 ConsumeToken();
534 trailingComma = true;
535 }
536
537 if (Tok.is(tok::r_brace))
538 break;
539 }
540
541 Braces.consumeClose();
542
543 return !trailingComma;
544}