blob: 45f1825f7e62e9ef07bcdee051641e4d2a358e52 [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
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore9bba4f2010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/Designator.h"
18#include "clang/Sema/Scope.h"
Steve Narofffbd09832007-07-19 01:06:55 +000019#include "llvm/ADT/SmallString.h"
Daniel Dunbarebd5b4a2009-10-17 23:52:50 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner8693a512006-08-13 21:54:02 +000021using namespace clang;
22
23
Douglas Gregora80cae12012-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
Chris Lattner8693a512006-08-13 21:54:02 +000032 case tok::period: // designator: '.' identifier
Douglas Gregora80cae12012-02-17 03:49:44 +000033 return true;
34
35 case tok::l_square: { // designator: array-designator
36 if (!PP.getLangOptions().CPlusPlus0x)
Chris Lattnerdde65432008-10-26 22:41:58 +000037 return true;
Douglas Gregora80cae12012-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
61 // Parse up to (at most) the token after the closing ']' to determine
62 // whether this is a C99 designator or a lambda.
63 TentativeParsingAction Tentative(*this);
64 ConsumeBracket();
65 while (true) {
66 switch (Tok.getKind()) {
67 case tok::equal:
68 case tok::amp:
69 case tok::identifier:
70 case tok::kw_this:
71 // These tokens can occur in a capture list or a constant-expression.
72 // Keep looking.
73 ConsumeToken();
74 continue;
75
76 case tok::comma:
77 // Since a comma cannot occur in a constant-expression, this must
78 // be a lambda.
79 Tentative.Revert();
80 return false;
81
82 case tok::r_square: {
83 // Once we hit the closing square bracket, we look at the next
84 // token. If it's an '=', this is a designator. Otherwise, it's a
85 // lambda expression. This decision favors lambdas over the older
86 // GNU designator syntax, which allows one to omit the '=', but is
87 // consistent with GCC.
88 ConsumeBracket();
89 tok::TokenKind Kind = Tok.getKind();
90 Tentative.Revert();
91 return Kind == tok::equal;
92 }
93
94 default:
95 // Anything else cannot occur in a lambda capture list, so it
96 // must be a designator.
97 Tentative.Revert();
98 return true;
99 }
100 }
101
102 return true;
103 }
Chris Lattner8693a512006-08-13 21:54:02 +0000104 case tok::identifier: // designation: identifier ':'
Chris Lattnerdde65432008-10-26 22:41:58 +0000105 return PP.LookAhead(0).is(tok::colon);
Chris Lattner8693a512006-08-13 21:54:02 +0000106 }
107}
108
Douglas Gregor8d4de672010-04-21 22:36:40 +0000109static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
110 Designation &Desig) {
111 // If we have exactly one array designator, this used the GNU
112 // 'designation: array-designator' extension, otherwise there should be no
113 // designators at all!
114 if (Desig.getNumDesignators() == 1 &&
115 (Desig.getDesignator(0).isArrayDesignator() ||
116 Desig.getDesignator(0).isArrayRangeDesignator()))
117 P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
118 else if (Desig.getNumDesignators() > 0)
119 P.Diag(Loc, diag::err_expected_equal_designator);
120}
121
Chris Lattnere7dab442006-08-13 21:54:51 +0000122/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
123/// checking to see if the token stream starts with a designator.
124///
Chris Lattner8693a512006-08-13 21:54:02 +0000125/// designation:
126/// designator-list '='
127/// [GNU] array-designator
128/// [GNU] identifier ':'
129///
130/// designator-list:
131/// designator
132/// designator-list designator
133///
134/// designator:
135/// array-designator
136/// '.' identifier
137///
138/// array-designator:
139/// '[' constant-expression ']'
140/// [GNU] '[' constant-expression '...' constant-expression ']'
141///
142/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattner0c024602008-10-26 21:46:13 +0000143/// initializer (because it is an expression). We need to consider this case
144/// when parsing array designators.
Chris Lattner8693a512006-08-13 21:54:02 +0000145///
John McCalldadc5752010-08-24 06:29:42 +0000146ExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redld65cea82008-12-11 22:51:44 +0000147
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000148 // If this is the old-style GNU extension:
149 // designation ::= identifier ':'
150 // Handle it as a field designator. Otherwise, this must be the start of a
151 // normal expression.
152 if (Tok.is(tok::identifier)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000153 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000154
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000155 SmallString<256> NewSyntax;
Daniel Dunbar07d07852009-10-18 21:17:35 +0000156 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbarebd5b4a2009-10-17 23:52:50 +0000157 << " = ";
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000158
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000159 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000160
Chris Lattnere2b5e872008-10-26 22:49:49 +0000161 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000162 SourceLocation ColonLoc = ConsumeToken();
163
Douglas Gregorafeabec2011-08-27 00:13:16 +0000164 Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
Douglas Gregora771f462010-03-31 17:46:05 +0000165 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
166 NewSyntax.str());
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000167
Douglas Gregor85992cf2009-03-20 23:11:49 +0000168 Designation D;
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000169 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump11289f42009-09-09 15:08:12 +0000170 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000171 ParseInitializer());
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000172 }
Mike Stump11289f42009-09-09 15:08:12 +0000173
Chris Lattner72432452008-10-26 22:59:19 +0000174 // Desig - This is initialized when we see our first designator. We may have
175 // an objc message send with no designator, so we don't want to create this
176 // eagerly.
Douglas Gregor85992cf2009-03-20 23:11:49 +0000177 Designation Desig;
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner8693a512006-08-13 21:54:02 +0000179 // Parse each designator in the designator list until we find an initializer.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000180 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
181 if (Tok.is(tok::period)) {
Chris Lattner8693a512006-08-13 21:54:02 +0000182 // designator: '.' identifier
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000183 SourceLocation DotLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000184
Chris Lattner72432452008-10-26 22:59:19 +0000185 if (Tok.isNot(tok::identifier)) {
186 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000187 return ExprError();
Chris Lattner72432452008-10-26 22:59:19 +0000188 }
Mike Stump11289f42009-09-09 15:08:12 +0000189
Douglas Gregor85992cf2009-03-20 23:11:49 +0000190 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
191 Tok.getLocation()));
Chris Lattner72432452008-10-26 22:59:19 +0000192 ConsumeToken(); // Eat the identifier.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000193 continue;
194 }
Mike Stump11289f42009-09-09 15:08:12 +0000195
Chris Lattnere2b5e872008-10-26 22:49:49 +0000196 // We must have either an array designator now or an objc message send.
197 assert(Tok.is(tok::l_square) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +0000198
Chris Lattner8aafd352008-10-26 23:06:54 +0000199 // Handle the two forms of array designator:
200 // array-designator: '[' constant-expression ']'
201 // array-designator: '[' constant-expression '...' constant-expression ']'
202 //
203 // Also, we have to handle the case where the expression after the
204 // designator an an objc message send: '[' objc-message-expr ']'.
205 // Interesting cases are:
206 // [foo bar] -> objc message send
207 // [foo] -> array designator
208 // [foo ... bar] -> array designator
209 // [4][foo bar] -> obsolete GNU designation with objc message send.
210 //
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000211 InMessageExpressionRAIIObject InMessage(*this, true);
212
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000213 BalancedDelimiterTracker T(*this, tok::l_square);
214 T.consumeOpen();
215 SourceLocation StartLoc = T.getOpenLocation();
216
John McCalldadc5752010-08-24 06:29:42 +0000217 ExprResult Idx;
Mike Stump11289f42009-09-09 15:08:12 +0000218
Douglas Gregor8d4de672010-04-21 22:36:40 +0000219 // If Objective-C is enabled and this is a typename (class message
220 // send) or send to 'super', parse this as a message send
221 // expression. We handle C++ and C separately, since C++ requires
222 // much more complicated parsing.
223 if (getLang().ObjC1 && getLang().CPlusPlus) {
224 // Send to 'super'.
225 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000226 NextToken().isNot(tok::period) &&
227 getCurScope()->isInObjcMethodScope()) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000228 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
229 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
John McCallba7bf592010-08-24 05:47:05 +0000230 ConsumeToken(),
231 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +0000232 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000233 }
234
235 // Parse the receiver, which is either a type or an expression.
236 bool IsExpr;
237 void *TypeOrExpr;
238 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
239 SkipUntil(tok::r_square);
240 return ExprError();
241 }
242
243 // If the receiver was a type, we have a class message; parse
244 // the rest of it.
245 if (!IsExpr) {
246 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
247 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
248 SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +0000249 ParsedType::getFromOpaquePtr(TypeOrExpr),
John McCallb268a282010-08-23 23:25:46 +0000250 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000251 }
252
253 // If the receiver was an expression, we still don't know
254 // whether we have a message send or an array designator; just
255 // adopt the expression for further analysis below.
256 // FIXME: potentially-potentially evaluated expression above?
John McCalldadc5752010-08-24 06:29:42 +0000257 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +0000258 } else if (getLang().ObjC1 && Tok.is(tok::identifier)) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000259 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000260 SourceLocation IILoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +0000261 ParsedType ReceiverType;
Chris Lattner3adb17d2010-04-12 06:36:00 +0000262 // Three cases. This is a message send to a type: [type foo]
263 // This is a message send to super: [super foo]
264 // This is a message sent to an expr: [super.bar foo]
John McCallfaf5fb42010-08-26 23:41:50 +0000265 switch (Sema::ObjCMessageKind Kind
Douglas Gregor0be31a22010-07-02 17:43:08 +0000266 = Actions.getObjCMessageKind(getCurScope(), II, IILoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000267 II == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +0000268 NextToken().is(tok::period),
269 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +0000270 case Sema::ObjCSuperMessage:
271 case Sema::ObjCClassMessage:
Douglas Gregor8d4de672010-04-21 22:36:40 +0000272 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
John McCallfaf5fb42010-08-26 23:41:50 +0000273 if (Kind == Sema::ObjCSuperMessage)
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000274 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
275 ConsumeToken(),
John McCallba7bf592010-08-24 05:47:05 +0000276 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +0000277 0);
Douglas Gregore5798dc2010-04-21 20:38:13 +0000278 ConsumeToken(); // the identifier
279 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000280 SkipUntil(tok::r_square);
281 return ExprError();
282 }
283
284 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
285 SourceLocation(),
Douglas Gregore5798dc2010-04-21 20:38:13 +0000286 ReceiverType,
John McCallb268a282010-08-23 23:25:46 +0000287 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000288
John McCallfaf5fb42010-08-26 23:41:50 +0000289 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000290 // Fall through; we'll just parse the expression and
291 // (possibly) treat this like an Objective-C message send
292 // later.
293 break;
Chris Lattnera36ec422010-04-11 08:28:14 +0000294 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000295 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000296
Douglas Gregor8d4de672010-04-21 22:36:40 +0000297 // Parse the index expression, if we haven't already gotten one
298 // above (which can only happen in Objective-C++).
Chris Lattnere2b5e872008-10-26 22:49:49 +0000299 // Note that we parse this as an assignment expression, not a constant
300 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
301 // to validate that the expression is a constant.
Douglas Gregor8d4de672010-04-21 22:36:40 +0000302 // FIXME: We also need to tell Sema that we're in a
303 // potentially-potentially evaluated context.
304 if (!Idx.get()) {
305 Idx = ParseAssignmentExpression();
306 if (Idx.isInvalid()) {
307 SkipUntil(tok::r_square);
308 return move(Idx);
309 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000310 }
Mike Stump11289f42009-09-09 15:08:12 +0000311
Chris Lattnere2b5e872008-10-26 22:49:49 +0000312 // Given an expression, we could either have a designator (if the next
313 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump11289f42009-09-09 15:08:12 +0000314 // message send, handle it now. An objc-message send is the start of
Chris Lattnere2b5e872008-10-26 22:49:49 +0000315 // an assignment-expression production.
Mike Stump11289f42009-09-09 15:08:12 +0000316 if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattnere2b5e872008-10-26 22:49:49 +0000317 Tok.isNot(tok::r_square)) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000318 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000319 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
320 SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +0000321 ParsedType(),
322 Idx.take());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000323 }
Chris Lattner8aafd352008-10-26 23:06:54 +0000324
Chris Lattner8aafd352008-10-26 23:06:54 +0000325 // If this is a normal array designator, remember it.
326 if (Tok.isNot(tok::ellipsis)) {
Douglas Gregor85992cf2009-03-20 23:11:49 +0000327 Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
Chris Lattner8aafd352008-10-26 23:06:54 +0000328 } else {
329 // Handle the gnu array range extension.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000330 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000331 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000332
John McCalldadc5752010-08-24 06:29:42 +0000333 ExprResult RHS(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000334 if (RHS.isInvalid()) {
Chris Lattner8693a512006-08-13 21:54:02 +0000335 SkipUntil(tok::r_square);
Sebastian Redld65cea82008-12-11 22:51:44 +0000336 return move(RHS);
Chris Lattner8693a512006-08-13 21:54:02 +0000337 }
Douglas Gregor85992cf2009-03-20 23:11:49 +0000338 Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
339 RHS.release(),
340 StartLoc, EllipsisLoc));
Chris Lattner8693a512006-08-13 21:54:02 +0000341 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000342
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000343 T.consumeClose();
344 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
345 T.getCloseLocation());
Chris Lattner8693a512006-08-13 21:54:02 +0000346 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000347
Chris Lattner72432452008-10-26 22:59:19 +0000348 // Okay, we're done with the designator sequence. We know that there must be
349 // at least one designator, because the only case we can get into this method
350 // without a designator is when we have an objc message send. That case is
351 // handled and returned from above.
Douglas Gregor85992cf2009-03-20 23:11:49 +0000352 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redld65cea82008-12-11 22:51:44 +0000353
Chris Lattner72432452008-10-26 22:59:19 +0000354 // Handle a normal designator sequence end, which is an equal.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000355 if (Tok.is(tok::equal)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000356 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor85992cf2009-03-20 23:11:49 +0000357 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000358 ParseInitializer());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000359 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000360
Chris Lattner72432452008-10-26 22:59:19 +0000361 // We read some number of designators and found something that isn't an = or
Chris Lattner46dcba62008-10-26 23:22:23 +0000362 // an initializer. If we have exactly one array designator, this
Chris Lattner72432452008-10-26 22:59:19 +0000363 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
364 // parse error.
Mike Stump11289f42009-09-09 15:08:12 +0000365 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor85992cf2009-03-20 23:11:49 +0000366 (Desig.getDesignator(0).isArrayDesignator() ||
367 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000368 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregora771f462010-03-31 17:46:05 +0000369 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000370 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor8aa6bf52009-03-27 23:40:29 +0000371 true, ParseInitializer());
Chris Lattner46dcba62008-10-26 23:22:23 +0000372 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000373
Chris Lattner46dcba62008-10-26 23:22:23 +0000374 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000375 return ExprError();
Chris Lattner8693a512006-08-13 21:54:02 +0000376}
377
378
Chris Lattner3fa92392008-10-26 22:38:55 +0000379/// ParseBraceInitializer - Called when parsing an initializer that has a
380/// leading open brace.
381///
Chris Lattner8693a512006-08-13 21:54:02 +0000382/// initializer: [C99 6.7.8]
Chris Lattner8693a512006-08-13 21:54:02 +0000383/// '{' initializer-list '}'
384/// '{' initializer-list ',' '}'
385/// [GNU] '{' '}'
386///
387/// initializer-list:
Douglas Gregor968f23a2011-01-03 19:31:53 +0000388/// designation[opt] initializer ...[opt]
389/// initializer-list ',' designation[opt] initializer ...[opt]
Chris Lattner8693a512006-08-13 21:54:02 +0000390///
John McCalldadc5752010-08-24 06:29:42 +0000391ExprResult Parser::ParseBraceInitializer() {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000392 InMessageExpressionRAIIObject InMessage(*this, false);
393
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000394 BalancedDelimiterTracker T(*this, tok::l_brace);
395 T.consumeOpen();
396 SourceLocation LBraceLoc = T.getOpenLocation();
Sebastian Redl511ed552008-11-25 22:21:31 +0000397
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000398 /// InitExprs - This is the actual list of expressions contained in the
399 /// initializer.
Sebastian Redl511ed552008-11-25 22:21:31 +0000400 ExprVector InitExprs(Actions);
401
Chris Lattner248388e2008-10-26 23:35:51 +0000402 if (Tok.is(tok::r_brace)) {
Douglas Gregord14247a2009-01-30 22:09:00 +0000403 // Empty initializers are a C++ feature and a GNU extension to C.
404 if (!getLang().CPlusPlus)
405 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner248388e2008-10-26 23:35:51 +0000406 // Match the '}'.
John McCallfaf5fb42010-08-26 23:41:50 +0000407 return Actions.ActOnInitList(LBraceLoc, MultiExprArg(Actions),
Douglas Gregor85992cf2009-03-20 23:11:49 +0000408 ConsumeBrace());
Chris Lattner248388e2008-10-26 23:35:51 +0000409 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000410
Steve Narofffbd09832007-07-19 01:06:55 +0000411 bool InitExprsOk = true;
Sebastian Redld65cea82008-12-11 22:51:44 +0000412
Steve Narofffbd09832007-07-19 01:06:55 +0000413 while (1) {
Francois Pichet02513162011-12-12 23:24:39 +0000414 // Handle Microsoft __if_exists/if_not_exists if necessary.
415 if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
416 Tok.is(tok::kw___if_not_exists))) {
417 if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
418 if (Tok.isNot(tok::comma)) break;
419 ConsumeToken();
420 }
421 if (Tok.is(tok::r_brace)) break;
422 continue;
423 }
424
Steve Narofffbd09832007-07-19 01:06:55 +0000425 // Parse: designation[opt] initializer
Sebastian Redld65cea82008-12-11 22:51:44 +0000426
Steve Narofffbd09832007-07-19 01:06:55 +0000427 // If we know that this cannot be a designation, just parse the nested
428 // initializer directly.
John McCalldadc5752010-08-24 06:29:42 +0000429 ExprResult SubElt;
Douglas Gregora80cae12012-02-17 03:49:44 +0000430 if (MayBeDesignationStart())
Douglas Gregor85992cf2009-03-20 23:11:49 +0000431 SubElt = ParseInitializerWithPotentialDesignator();
432 else
Steve Narofffbd09832007-07-19 01:06:55 +0000433 SubElt = ParseInitializer();
Mike Stump11289f42009-09-09 15:08:12 +0000434
Douglas Gregor968f23a2011-01-03 19:31:53 +0000435 if (Tok.is(tok::ellipsis))
436 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
437
Steve Narofffbd09832007-07-19 01:06:55 +0000438 // If we couldn't parse the subelement, bail out.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000439 if (!SubElt.isInvalid()) {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000440 InitExprs.push_back(SubElt.release());
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000441 } else {
442 InitExprsOk = false;
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000444 // We have two ways to try to recover from this error: if the code looks
Chris Lattner57540c52011-04-15 05:22:18 +0000445 // grammatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000446 // parsing the rest of the initializer. This allows us to emit
447 // diagnostics for later elements that we find. If we don't see a comma,
448 // assume there is a parse error, and just skip to recover.
Sebastian Redl511ed552008-11-25 22:21:31 +0000449 // FIXME: This comment doesn't sound right. If there is a r_brace
450 // immediately, it can't be an error, since there is no other way of
451 // leaving this loop except through this if.
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000452 if (Tok.isNot(tok::comma)) {
453 SkipUntil(tok::r_brace, false, true);
454 break;
455 }
456 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000457
Steve Narofffbd09832007-07-19 01:06:55 +0000458 // If we don't have a comma continued list, we're done.
Chris Lattner76c72282007-10-09 17:33:22 +0000459 if (Tok.isNot(tok::comma)) break;
Sebastian Redld65cea82008-12-11 22:51:44 +0000460
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000461 // TODO: save comma locations if some client cares.
Steve Narofffbd09832007-07-19 01:06:55 +0000462 ConsumeToken();
Sebastian Redld65cea82008-12-11 22:51:44 +0000463
Steve Narofffbd09832007-07-19 01:06:55 +0000464 // Handle trailing comma.
Chris Lattner76c72282007-10-09 17:33:22 +0000465 if (Tok.is(tok::r_brace)) break;
Steve Narofffbd09832007-07-19 01:06:55 +0000466 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000467
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000468 bool closed = !T.consumeClose();
469
470 if (InitExprsOk && closed)
471 return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
472 T.getCloseLocation());
473
Sebastian Redld65cea82008-12-11 22:51:44 +0000474 return ExprError(); // an error occurred.
Chris Lattner8693a512006-08-13 21:54:02 +0000475}
476
Francois Pichet02513162011-12-12 23:24:39 +0000477
478// Return true if a comma (or closing brace) is necessary after the
479// __if_exists/if_not_exists statement.
480bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
481 bool &InitExprsOk) {
482 bool trailingComma = false;
483 IfExistsCondition Result;
484 if (ParseMicrosoftIfExistsCondition(Result))
485 return false;
486
487 BalancedDelimiterTracker Braces(*this, tok::l_brace);
488 if (Braces.consumeOpen()) {
489 Diag(Tok, diag::err_expected_lbrace);
490 return false;
491 }
492
493 switch (Result.Behavior) {
494 case IEB_Parse:
495 // Parse the declarations below.
496 break;
497
498 case IEB_Dependent:
499 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
500 << Result.IsIfExists;
501 // Fall through to skip.
502
503 case IEB_Skip:
504 Braces.skipToEnd();
505 return false;
506 }
507
508 while (Tok.isNot(tok::eof)) {
509 trailingComma = false;
510 // If we know that this cannot be a designation, just parse the nested
511 // initializer directly.
512 ExprResult SubElt;
Douglas Gregora80cae12012-02-17 03:49:44 +0000513 if (MayBeDesignationStart())
Francois Pichet02513162011-12-12 23:24:39 +0000514 SubElt = ParseInitializerWithPotentialDesignator();
515 else
516 SubElt = ParseInitializer();
517
518 if (Tok.is(tok::ellipsis))
519 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
520
521 // If we couldn't parse the subelement, bail out.
522 if (!SubElt.isInvalid())
523 InitExprs.push_back(SubElt.release());
524 else
525 InitExprsOk = false;
526
527 if (Tok.is(tok::comma)) {
528 ConsumeToken();
529 trailingComma = true;
530 }
531
532 if (Tok.is(tok::r_brace))
533 break;
534 }
535
536 Braces.consumeClose();
537
538 return !trailingComma;
539}