blob: 42287d68b331f098bea6d1d8975256987312b7b9 [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"
Douglas Gregore9bba4f2010-09-15 14:51:05 +000015#include "RAIIObjectsForParser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Parse/ParseDiagnostic.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
Richard Smith2bf7fdb2013-01-02 11:42:31 +000036 if (!PP.getLangOpts().CPlusPlus11)
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
Douglas Gregor23b05412012-02-17 16:41:16 +000061 // Handle the complicated case below.
62 break;
Douglas Gregora80cae12012-02-17 03:49:44 +000063 }
Chris Lattner8693a512006-08-13 21:54:02 +000064 case tok::identifier: // designation: identifier ':'
Chris Lattnerdde65432008-10-26 22:41:58 +000065 return PP.LookAhead(0).is(tok::colon);
Chris Lattner8693a512006-08-13 21:54:02 +000066 }
Douglas Gregor23b05412012-02-17 16:41:16 +000067
68 // Parse up to (at most) the token after the closing ']' to determine
Richard Smith9e2f0a42014-04-13 04:31:48 +000069 // whether this is a C99 designator or a lambda.
Douglas Gregor23b05412012-02-17 16:41:16 +000070 TentativeParsingAction Tentative(*this);
Richard Smith9e2f0a42014-04-13 04:31:48 +000071
72 LambdaIntroducer Intro;
73 bool SkippedInits = false;
74 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
75
76 if (DiagID) {
77 // If this can't be a lambda capture list, it's a designator.
78 Tentative.Revert();
79 return true;
Douglas Gregor23b05412012-02-17 16:41:16 +000080 }
Richard Smith9e2f0a42014-04-13 04:31:48 +000081
82 // Once we hit the closing square bracket, we look at the next
83 // token. If it's an '=', this is a designator. Otherwise, it's a
84 // lambda expression. This decision favors lambdas over the older
85 // GNU designator syntax, which allows one to omit the '=', but is
86 // consistent with GCC.
87 tok::TokenKind Kind = Tok.getKind();
88 // FIXME: If we didn't skip any inits, parse the lambda from here
89 // rather than throwing away then reparsing the LambdaIntroducer.
90 Tentative.Revert();
91 return Kind == tok::equal;
Chris Lattner8693a512006-08-13 21:54:02 +000092}
93
Douglas Gregor8d4de672010-04-21 22:36:40 +000094static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
95 Designation &Desig) {
96 // If we have exactly one array designator, this used the GNU
97 // 'designation: array-designator' extension, otherwise there should be no
98 // designators at all!
99 if (Desig.getNumDesignators() == 1 &&
100 (Desig.getDesignator(0).isArrayDesignator() ||
101 Desig.getDesignator(0).isArrayRangeDesignator()))
102 P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
103 else if (Desig.getNumDesignators() > 0)
104 P.Diag(Loc, diag::err_expected_equal_designator);
105}
106
Chris Lattnere7dab442006-08-13 21:54:51 +0000107/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
108/// checking to see if the token stream starts with a designator.
109///
Chris Lattner8693a512006-08-13 21:54:02 +0000110/// designation:
111/// designator-list '='
112/// [GNU] array-designator
113/// [GNU] identifier ':'
114///
115/// designator-list:
116/// designator
117/// designator-list designator
118///
119/// designator:
120/// array-designator
121/// '.' identifier
122///
123/// array-designator:
124/// '[' constant-expression ']'
125/// [GNU] '[' constant-expression '...' constant-expression ']'
126///
127/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattner0c024602008-10-26 21:46:13 +0000128/// initializer (because it is an expression). We need to consider this case
129/// when parsing array designators.
Chris Lattner8693a512006-08-13 21:54:02 +0000130///
John McCalldadc5752010-08-24 06:29:42 +0000131ExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redld65cea82008-12-11 22:51:44 +0000132
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000133 // If this is the old-style GNU extension:
134 // designation ::= identifier ':'
135 // Handle it as a field designator. Otherwise, this must be the start of a
136 // normal expression.
137 if (Tok.is(tok::identifier)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000138 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000139
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000140 SmallString<256> NewSyntax;
Daniel Dunbar07d07852009-10-18 21:17:35 +0000141 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbarebd5b4a2009-10-17 23:52:50 +0000142 << " = ";
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000143
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000144 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000145
Chris Lattnere2b5e872008-10-26 22:49:49 +0000146 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000147 SourceLocation ColonLoc = ConsumeToken();
148
Douglas Gregorafeabec2011-08-27 00:13:16 +0000149 Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
Douglas Gregora771f462010-03-31 17:46:05 +0000150 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
Yaron Keren92e1b622015-03-18 10:17:07 +0000151 NewSyntax);
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000152
Douglas Gregor85992cf2009-03-20 23:11:49 +0000153 Designation D;
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000154 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump11289f42009-09-09 15:08:12 +0000155 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000156 ParseInitializer());
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000157 }
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattner72432452008-10-26 22:59:19 +0000159 // Desig - This is initialized when we see our first designator. We may have
160 // an objc message send with no designator, so we don't want to create this
161 // eagerly.
Douglas Gregor85992cf2009-03-20 23:11:49 +0000162 Designation Desig;
Mike Stump11289f42009-09-09 15:08:12 +0000163
Chris Lattner8693a512006-08-13 21:54:02 +0000164 // Parse each designator in the designator list until we find an initializer.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000165 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
166 if (Tok.is(tok::period)) {
Chris Lattner8693a512006-08-13 21:54:02 +0000167 // designator: '.' identifier
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000168 SourceLocation DotLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000169
Chris Lattner72432452008-10-26 22:59:19 +0000170 if (Tok.isNot(tok::identifier)) {
171 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000172 return ExprError();
Chris Lattner72432452008-10-26 22:59:19 +0000173 }
Mike Stump11289f42009-09-09 15:08:12 +0000174
Douglas Gregor85992cf2009-03-20 23:11:49 +0000175 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
176 Tok.getLocation()));
Chris Lattner72432452008-10-26 22:59:19 +0000177 ConsumeToken(); // Eat the identifier.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000178 continue;
179 }
Mike Stump11289f42009-09-09 15:08:12 +0000180
Chris Lattnere2b5e872008-10-26 22:49:49 +0000181 // We must have either an array designator now or an objc message send.
182 assert(Tok.is(tok::l_square) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +0000183
Chris Lattner8aafd352008-10-26 23:06:54 +0000184 // Handle the two forms of array designator:
185 // array-designator: '[' constant-expression ']'
186 // array-designator: '[' constant-expression '...' constant-expression ']'
187 //
188 // Also, we have to handle the case where the expression after the
189 // designator an an objc message send: '[' objc-message-expr ']'.
190 // Interesting cases are:
191 // [foo bar] -> objc message send
192 // [foo] -> array designator
193 // [foo ... bar] -> array designator
194 // [4][foo bar] -> obsolete GNU designation with objc message send.
195 //
Richard Smith7bdcc4a2012-04-10 01:32:12 +0000196 // We do not need to check for an expression starting with [[ here. If it
197 // contains an Objective-C message send, then it is not an ill-formed
198 // attribute. If it is a lambda-expression within an array-designator, then
199 // it will be rejected because a constant-expression cannot begin with a
200 // lambda-expression.
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000201 InMessageExpressionRAIIObject InMessage(*this, true);
202
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000203 BalancedDelimiterTracker T(*this, tok::l_square);
204 T.consumeOpen();
205 SourceLocation StartLoc = T.getOpenLocation();
206
John McCalldadc5752010-08-24 06:29:42 +0000207 ExprResult Idx;
Mike Stump11289f42009-09-09 15:08:12 +0000208
Douglas Gregor8d4de672010-04-21 22:36:40 +0000209 // If Objective-C is enabled and this is a typename (class message
210 // send) or send to 'super', parse this as a message send
211 // expression. We handle C++ and C separately, since C++ requires
212 // much more complicated parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000213 if (getLangOpts().ObjC1 && getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000214 // Send to 'super'.
215 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000216 NextToken().isNot(tok::period) &&
217 getCurScope()->isInObjcMethodScope()) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000218 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
219 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
John McCallba7bf592010-08-24 05:47:05 +0000220 ConsumeToken(),
221 ParsedType(),
Craig Topper161e4db2014-05-21 06:02:52 +0000222 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000223 }
224
225 // Parse the receiver, which is either a type or an expression.
226 bool IsExpr;
227 void *TypeOrExpr;
228 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000229 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000230 return ExprError();
231 }
232
233 // If the receiver was a type, we have a class message; parse
234 // the rest of it.
235 if (!IsExpr) {
236 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
237 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
238 SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +0000239 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +0000240 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000241 }
242
243 // If the receiver was an expression, we still don't know
244 // whether we have a message send or an array designator; just
245 // adopt the expression for further analysis below.
246 // FIXME: potentially-potentially evaluated expression above?
John McCalldadc5752010-08-24 06:29:42 +0000247 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
David Blaikiebbafb8a2012-03-11 07:00:24 +0000248 } else if (getLangOpts().ObjC1 && Tok.is(tok::identifier)) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000249 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000250 SourceLocation IILoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +0000251 ParsedType ReceiverType;
Chris Lattner3adb17d2010-04-12 06:36:00 +0000252 // Three cases. This is a message send to a type: [type foo]
253 // This is a message send to super: [super foo]
254 // This is a message sent to an expr: [super.bar foo]
John McCallfaf5fb42010-08-26 23:41:50 +0000255 switch (Sema::ObjCMessageKind Kind
Douglas Gregor0be31a22010-07-02 17:43:08 +0000256 = Actions.getObjCMessageKind(getCurScope(), II, IILoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000257 II == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +0000258 NextToken().is(tok::period),
259 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +0000260 case Sema::ObjCSuperMessage:
261 case Sema::ObjCClassMessage:
Douglas Gregor8d4de672010-04-21 22:36:40 +0000262 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
John McCallfaf5fb42010-08-26 23:41:50 +0000263 if (Kind == Sema::ObjCSuperMessage)
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000264 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
265 ConsumeToken(),
John McCallba7bf592010-08-24 05:47:05 +0000266 ParsedType(),
Craig Topper161e4db2014-05-21 06:02:52 +0000267 nullptr);
Douglas Gregore5798dc2010-04-21 20:38:13 +0000268 ConsumeToken(); // the identifier
269 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000270 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000271 return ExprError();
272 }
273
274 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
275 SourceLocation(),
Douglas Gregore5798dc2010-04-21 20:38:13 +0000276 ReceiverType,
Craig Topper161e4db2014-05-21 06:02:52 +0000277 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000278
John McCallfaf5fb42010-08-26 23:41:50 +0000279 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000280 // Fall through; we'll just parse the expression and
281 // (possibly) treat this like an Objective-C message send
282 // later.
283 break;
Chris Lattnera36ec422010-04-11 08:28:14 +0000284 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000285 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000286
Douglas Gregor8d4de672010-04-21 22:36:40 +0000287 // Parse the index expression, if we haven't already gotten one
288 // above (which can only happen in Objective-C++).
Chris Lattnere2b5e872008-10-26 22:49:49 +0000289 // Note that we parse this as an assignment expression, not a constant
290 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
291 // to validate that the expression is a constant.
Douglas Gregor8d4de672010-04-21 22:36:40 +0000292 // FIXME: We also need to tell Sema that we're in a
293 // potentially-potentially evaluated context.
294 if (!Idx.get()) {
295 Idx = ParseAssignmentExpression();
296 if (Idx.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000297 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000298 return Idx;
Douglas Gregor8d4de672010-04-21 22:36:40 +0000299 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000300 }
Mike Stump11289f42009-09-09 15:08:12 +0000301
Chris Lattnere2b5e872008-10-26 22:49:49 +0000302 // Given an expression, we could either have a designator (if the next
303 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump11289f42009-09-09 15:08:12 +0000304 // message send, handle it now. An objc-message send is the start of
Chris Lattnere2b5e872008-10-26 22:49:49 +0000305 // an assignment-expression production.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000306 if (getLangOpts().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattnere2b5e872008-10-26 22:49:49 +0000307 Tok.isNot(tok::r_square)) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000308 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000309 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
310 SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +0000311 ParsedType(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000312 Idx.get());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000313 }
Chris Lattner8aafd352008-10-26 23:06:54 +0000314
Chris Lattner8aafd352008-10-26 23:06:54 +0000315 // If this is a normal array designator, remember it.
316 if (Tok.isNot(tok::ellipsis)) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000317 Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc));
Chris Lattner8aafd352008-10-26 23:06:54 +0000318 } else {
319 // Handle the gnu array range extension.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000320 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000321 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000322
John McCalldadc5752010-08-24 06:29:42 +0000323 ExprResult RHS(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000324 if (RHS.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000325 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000326 return RHS;
Chris Lattner8693a512006-08-13 21:54:02 +0000327 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000328 Desig.AddDesignator(Designator::getArrayRange(Idx.get(),
329 RHS.get(),
Douglas Gregor85992cf2009-03-20 23:11:49 +0000330 StartLoc, EllipsisLoc));
Chris Lattner8693a512006-08-13 21:54:02 +0000331 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000332
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000333 T.consumeClose();
334 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
335 T.getCloseLocation());
Chris Lattner8693a512006-08-13 21:54:02 +0000336 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000337
Chris Lattner72432452008-10-26 22:59:19 +0000338 // Okay, we're done with the designator sequence. We know that there must be
339 // at least one designator, because the only case we can get into this method
340 // without a designator is when we have an objc message send. That case is
341 // handled and returned from above.
Douglas Gregor85992cf2009-03-20 23:11:49 +0000342 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redld65cea82008-12-11 22:51:44 +0000343
Chris Lattner72432452008-10-26 22:59:19 +0000344 // Handle a normal designator sequence end, which is an equal.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000345 if (Tok.is(tok::equal)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000346 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor85992cf2009-03-20 23:11:49 +0000347 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000348 ParseInitializer());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000349 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000350
Chris Lattner72432452008-10-26 22:59:19 +0000351 // We read some number of designators and found something that isn't an = or
Chris Lattner46dcba62008-10-26 23:22:23 +0000352 // an initializer. If we have exactly one array designator, this
Chris Lattner72432452008-10-26 22:59:19 +0000353 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
354 // parse error.
Mike Stump11289f42009-09-09 15:08:12 +0000355 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor85992cf2009-03-20 23:11:49 +0000356 (Desig.getDesignator(0).isArrayDesignator() ||
357 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000358 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregora771f462010-03-31 17:46:05 +0000359 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000360 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor8aa6bf52009-03-27 23:40:29 +0000361 true, ParseInitializer());
Chris Lattner46dcba62008-10-26 23:22:23 +0000362 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000363
Chris Lattner46dcba62008-10-26 23:22:23 +0000364 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000365 return ExprError();
Chris Lattner8693a512006-08-13 21:54:02 +0000366}
367
368
Chris Lattner3fa92392008-10-26 22:38:55 +0000369/// ParseBraceInitializer - Called when parsing an initializer that has a
370/// leading open brace.
371///
Chris Lattner8693a512006-08-13 21:54:02 +0000372/// initializer: [C99 6.7.8]
Chris Lattner8693a512006-08-13 21:54:02 +0000373/// '{' initializer-list '}'
374/// '{' initializer-list ',' '}'
375/// [GNU] '{' '}'
376///
377/// initializer-list:
Douglas Gregor968f23a2011-01-03 19:31:53 +0000378/// designation[opt] initializer ...[opt]
379/// initializer-list ',' designation[opt] initializer ...[opt]
Chris Lattner8693a512006-08-13 21:54:02 +0000380///
John McCalldadc5752010-08-24 06:29:42 +0000381ExprResult Parser::ParseBraceInitializer() {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000382 InMessageExpressionRAIIObject InMessage(*this, false);
383
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000384 BalancedDelimiterTracker T(*this, tok::l_brace);
385 T.consumeOpen();
386 SourceLocation LBraceLoc = T.getOpenLocation();
Sebastian Redl511ed552008-11-25 22:21:31 +0000387
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000388 /// InitExprs - This is the actual list of expressions contained in the
389 /// initializer.
Benjamin Kramerf0623432012-08-23 22:51:59 +0000390 ExprVector InitExprs;
Sebastian Redl511ed552008-11-25 22:21:31 +0000391
Chris Lattner248388e2008-10-26 23:35:51 +0000392 if (Tok.is(tok::r_brace)) {
Douglas Gregord14247a2009-01-30 22:09:00 +0000393 // Empty initializers are a C++ feature and a GNU extension to C.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000394 if (!getLangOpts().CPlusPlus)
Douglas Gregord14247a2009-01-30 22:09:00 +0000395 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner248388e2008-10-26 23:35:51 +0000396 // Match the '}'.
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000397 return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace());
Chris Lattner248388e2008-10-26 23:35:51 +0000398 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000399
Steve Narofffbd09832007-07-19 01:06:55 +0000400 bool InitExprsOk = true;
Sebastian Redld65cea82008-12-11 22:51:44 +0000401
Steve Narofffbd09832007-07-19 01:06:55 +0000402 while (1) {
Francois Pichet02513162011-12-12 23:24:39 +0000403 // Handle Microsoft __if_exists/if_not_exists if necessary.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000404 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet02513162011-12-12 23:24:39 +0000405 Tok.is(tok::kw___if_not_exists))) {
406 if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
407 if (Tok.isNot(tok::comma)) break;
408 ConsumeToken();
409 }
410 if (Tok.is(tok::r_brace)) break;
411 continue;
412 }
413
Steve Narofffbd09832007-07-19 01:06:55 +0000414 // Parse: designation[opt] initializer
Sebastian Redld65cea82008-12-11 22:51:44 +0000415
Steve Narofffbd09832007-07-19 01:06:55 +0000416 // If we know that this cannot be a designation, just parse the nested
417 // initializer directly.
John McCalldadc5752010-08-24 06:29:42 +0000418 ExprResult SubElt;
Douglas Gregora80cae12012-02-17 03:49:44 +0000419 if (MayBeDesignationStart())
Douglas Gregor85992cf2009-03-20 23:11:49 +0000420 SubElt = ParseInitializerWithPotentialDesignator();
421 else
Steve Narofffbd09832007-07-19 01:06:55 +0000422 SubElt = ParseInitializer();
Mike Stump11289f42009-09-09 15:08:12 +0000423
Douglas Gregor968f23a2011-01-03 19:31:53 +0000424 if (Tok.is(tok::ellipsis))
425 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
Kaelyn Takata15867822014-11-21 18:48:04 +0000426
427 SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get());
428
Steve Narofffbd09832007-07-19 01:06:55 +0000429 // If we couldn't parse the subelement, bail out.
Kaelyn Takata15867822014-11-21 18:48:04 +0000430 if (SubElt.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000431 InitExprs.push_back(SubElt.get());
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000432 } else {
433 InitExprsOk = false;
Mike Stump11289f42009-09-09 15:08:12 +0000434
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000435 // We have two ways to try to recover from this error: if the code looks
Chris Lattner57540c52011-04-15 05:22:18 +0000436 // grammatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000437 // parsing the rest of the initializer. This allows us to emit
438 // diagnostics for later elements that we find. If we don't see a comma,
439 // assume there is a parse error, and just skip to recover.
Sebastian Redl511ed552008-11-25 22:21:31 +0000440 // FIXME: This comment doesn't sound right. If there is a r_brace
441 // immediately, it can't be an error, since there is no other way of
442 // leaving this loop except through this if.
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000443 if (Tok.isNot(tok::comma)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000444 SkipUntil(tok::r_brace, StopBeforeMatch);
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000445 break;
446 }
447 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000448
Steve Narofffbd09832007-07-19 01:06:55 +0000449 // If we don't have a comma continued list, we're done.
Chris Lattner76c72282007-10-09 17:33:22 +0000450 if (Tok.isNot(tok::comma)) break;
Sebastian Redld65cea82008-12-11 22:51:44 +0000451
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000452 // TODO: save comma locations if some client cares.
Steve Narofffbd09832007-07-19 01:06:55 +0000453 ConsumeToken();
Sebastian Redld65cea82008-12-11 22:51:44 +0000454
Steve Narofffbd09832007-07-19 01:06:55 +0000455 // Handle trailing comma.
Chris Lattner76c72282007-10-09 17:33:22 +0000456 if (Tok.is(tok::r_brace)) break;
Steve Narofffbd09832007-07-19 01:06:55 +0000457 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000458
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000459 bool closed = !T.consumeClose();
460
461 if (InitExprsOk && closed)
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000462 return Actions.ActOnInitList(LBraceLoc, InitExprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000463 T.getCloseLocation());
464
Sebastian Redld65cea82008-12-11 22:51:44 +0000465 return ExprError(); // an error occurred.
Chris Lattner8693a512006-08-13 21:54:02 +0000466}
467
Francois Pichet02513162011-12-12 23:24:39 +0000468
469// Return true if a comma (or closing brace) is necessary after the
470// __if_exists/if_not_exists statement.
471bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
472 bool &InitExprsOk) {
473 bool trailingComma = false;
474 IfExistsCondition Result;
475 if (ParseMicrosoftIfExistsCondition(Result))
476 return false;
477
478 BalancedDelimiterTracker Braces(*this, tok::l_brace);
479 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000480 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet02513162011-12-12 23:24:39 +0000481 return false;
482 }
483
484 switch (Result.Behavior) {
485 case IEB_Parse:
486 // Parse the declarations below.
487 break;
488
489 case IEB_Dependent:
490 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
491 << Result.IsIfExists;
492 // Fall through to skip.
493
494 case IEB_Skip:
495 Braces.skipToEnd();
496 return false;
497 }
498
Richard Smith34f30512013-11-23 04:06:09 +0000499 while (!isEofOrEom()) {
Francois Pichet02513162011-12-12 23:24:39 +0000500 trailingComma = false;
501 // If we know that this cannot be a designation, just parse the nested
502 // initializer directly.
503 ExprResult SubElt;
Douglas Gregora80cae12012-02-17 03:49:44 +0000504 if (MayBeDesignationStart())
Francois Pichet02513162011-12-12 23:24:39 +0000505 SubElt = ParseInitializerWithPotentialDesignator();
506 else
507 SubElt = ParseInitializer();
508
509 if (Tok.is(tok::ellipsis))
510 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
511
512 // If we couldn't parse the subelement, bail out.
513 if (!SubElt.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000514 InitExprs.push_back(SubElt.get());
Francois Pichet02513162011-12-12 23:24:39 +0000515 else
516 InitExprsOk = false;
517
518 if (Tok.is(tok::comma)) {
519 ConsumeToken();
520 trailingComma = true;
521 }
522
523 if (Tok.is(tok::r_brace))
524 break;
525 }
526
527 Braces.consumeClose();
528
529 return !trailingComma;
530}