blob: 2cdb9d3a22a6a0581e92861873deba0d7b1e4d84 [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);
David Blaikieefdccaa2016-01-15 23:43:34 +0000219 return ParseAssignmentExprWithObjCMessageExprStart(
220 StartLoc, ConsumeToken(), nullptr, nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000221 }
222
223 // Parse the receiver, which is either a type or an expression.
224 bool IsExpr;
225 void *TypeOrExpr;
226 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000227 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000228 return ExprError();
229 }
230
231 // If the receiver was a type, we have a class message; parse
232 // the rest of it.
233 if (!IsExpr) {
234 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
235 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
236 SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +0000237 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +0000238 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000239 }
240
241 // If the receiver was an expression, we still don't know
242 // whether we have a message send or an array designator; just
243 // adopt the expression for further analysis below.
244 // FIXME: potentially-potentially evaluated expression above?
John McCalldadc5752010-08-24 06:29:42 +0000245 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
David Blaikiebbafb8a2012-03-11 07:00:24 +0000246 } else if (getLangOpts().ObjC1 && Tok.is(tok::identifier)) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000247 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000248 SourceLocation IILoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +0000249 ParsedType ReceiverType;
Chris Lattner3adb17d2010-04-12 06:36:00 +0000250 // Three cases. This is a message send to a type: [type foo]
251 // This is a message send to super: [super foo]
252 // This is a message sent to an expr: [super.bar foo]
Aaron Ballman385a8c02015-07-07 13:21:26 +0000253 switch (Actions.getObjCMessageKind(
254 getCurScope(), II, IILoc, II == Ident_super,
255 NextToken().is(tok::period), ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +0000256 case Sema::ObjCSuperMessage:
Douglas Gregore83b9562015-07-07 03:57:53 +0000257 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
David Blaikieefdccaa2016-01-15 23:43:34 +0000258 return ParseAssignmentExprWithObjCMessageExprStart(
259 StartLoc, ConsumeToken(), nullptr, nullptr);
Douglas Gregore83b9562015-07-07 03:57:53 +0000260
John McCallfaf5fb42010-08-26 23:41:50 +0000261 case Sema::ObjCClassMessage:
Douglas Gregor8d4de672010-04-21 22:36:40 +0000262 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
Douglas Gregore5798dc2010-04-21 20:38:13 +0000263 ConsumeToken(); // the identifier
264 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000265 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000266 return ExprError();
267 }
268
Douglas Gregore83b9562015-07-07 03:57:53 +0000269 // Parse type arguments and protocol qualifiers.
270 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000271 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +0000272 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000273 = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType,
274 /*consumeLastToken=*/true,
275 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +0000276 if (!NewReceiverType.isUsable()) {
277 SkipUntil(tok::r_square, StopAtSemi);
278 return ExprError();
279 }
280
281 ReceiverType = NewReceiverType.get();
282 }
283
284 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000285 SourceLocation(),
Douglas Gregore5798dc2010-04-21 20:38:13 +0000286 ReceiverType,
Craig Topper161e4db2014-05-21 06:02:52 +0000287 nullptr);
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()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000307 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000308 return Idx;
Douglas Gregor8d4de672010-04-21 22:36:40 +0000309 }
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.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000316 if (getLangOpts().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);
David Blaikieefdccaa2016-01-15 23:43:34 +0000319 return ParseAssignmentExprWithObjCMessageExprStart(
320 StartLoc, SourceLocation(), nullptr, Idx.get());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000321 }
Chris Lattner8aafd352008-10-26 23:06:54 +0000322
Chris Lattner8aafd352008-10-26 23:06:54 +0000323 // If this is a normal array designator, remember it.
324 if (Tok.isNot(tok::ellipsis)) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000325 Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc));
Chris Lattner8aafd352008-10-26 23:06:54 +0000326 } else {
327 // Handle the gnu array range extension.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000328 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000329 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000330
John McCalldadc5752010-08-24 06:29:42 +0000331 ExprResult RHS(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000332 if (RHS.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000333 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000334 return RHS;
Chris Lattner8693a512006-08-13 21:54:02 +0000335 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000336 Desig.AddDesignator(Designator::getArrayRange(Idx.get(),
337 RHS.get(),
Douglas Gregor85992cf2009-03-20 23:11:49 +0000338 StartLoc, EllipsisLoc));
Chris Lattner8693a512006-08-13 21:54:02 +0000339 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000340
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000341 T.consumeClose();
342 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
343 T.getCloseLocation());
Chris Lattner8693a512006-08-13 21:54:02 +0000344 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000345
Chris Lattner72432452008-10-26 22:59:19 +0000346 // Okay, we're done with the designator sequence. We know that there must be
347 // at least one designator, because the only case we can get into this method
348 // without a designator is when we have an objc message send. That case is
349 // handled and returned from above.
Douglas Gregor85992cf2009-03-20 23:11:49 +0000350 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redld65cea82008-12-11 22:51:44 +0000351
Chris Lattner72432452008-10-26 22:59:19 +0000352 // Handle a normal designator sequence end, which is an equal.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000353 if (Tok.is(tok::equal)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000354 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor85992cf2009-03-20 23:11:49 +0000355 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000356 ParseInitializer());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000357 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000358
Chris Lattner72432452008-10-26 22:59:19 +0000359 // We read some number of designators and found something that isn't an = or
Chris Lattner46dcba62008-10-26 23:22:23 +0000360 // an initializer. If we have exactly one array designator, this
Chris Lattner72432452008-10-26 22:59:19 +0000361 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
362 // parse error.
Mike Stump11289f42009-09-09 15:08:12 +0000363 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor85992cf2009-03-20 23:11:49 +0000364 (Desig.getDesignator(0).isArrayDesignator() ||
365 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000366 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregora771f462010-03-31 17:46:05 +0000367 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000368 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor8aa6bf52009-03-27 23:40:29 +0000369 true, ParseInitializer());
Chris Lattner46dcba62008-10-26 23:22:23 +0000370 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000371
Chris Lattner46dcba62008-10-26 23:22:23 +0000372 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000373 return ExprError();
Chris Lattner8693a512006-08-13 21:54:02 +0000374}
375
376
Chris Lattner3fa92392008-10-26 22:38:55 +0000377/// ParseBraceInitializer - Called when parsing an initializer that has a
378/// leading open brace.
379///
Chris Lattner8693a512006-08-13 21:54:02 +0000380/// initializer: [C99 6.7.8]
Chris Lattner8693a512006-08-13 21:54:02 +0000381/// '{' initializer-list '}'
382/// '{' initializer-list ',' '}'
383/// [GNU] '{' '}'
384///
385/// initializer-list:
Douglas Gregor968f23a2011-01-03 19:31:53 +0000386/// designation[opt] initializer ...[opt]
387/// initializer-list ',' designation[opt] initializer ...[opt]
Chris Lattner8693a512006-08-13 21:54:02 +0000388///
John McCalldadc5752010-08-24 06:29:42 +0000389ExprResult Parser::ParseBraceInitializer() {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000390 InMessageExpressionRAIIObject InMessage(*this, false);
391
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000392 BalancedDelimiterTracker T(*this, tok::l_brace);
393 T.consumeOpen();
394 SourceLocation LBraceLoc = T.getOpenLocation();
Sebastian Redl511ed552008-11-25 22:21:31 +0000395
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000396 /// InitExprs - This is the actual list of expressions contained in the
397 /// initializer.
Benjamin Kramerf0623432012-08-23 22:51:59 +0000398 ExprVector InitExprs;
Sebastian Redl511ed552008-11-25 22:21:31 +0000399
Chris Lattner248388e2008-10-26 23:35:51 +0000400 if (Tok.is(tok::r_brace)) {
Douglas Gregord14247a2009-01-30 22:09:00 +0000401 // Empty initializers are a C++ feature and a GNU extension to C.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000402 if (!getLangOpts().CPlusPlus)
Douglas Gregord14247a2009-01-30 22:09:00 +0000403 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner248388e2008-10-26 23:35:51 +0000404 // Match the '}'.
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000405 return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace());
Chris Lattner248388e2008-10-26 23:35:51 +0000406 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000407
Steve Narofffbd09832007-07-19 01:06:55 +0000408 bool InitExprsOk = true;
Sebastian Redld65cea82008-12-11 22:51:44 +0000409
Steve Narofffbd09832007-07-19 01:06:55 +0000410 while (1) {
Francois Pichet02513162011-12-12 23:24:39 +0000411 // Handle Microsoft __if_exists/if_not_exists if necessary.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000412 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet02513162011-12-12 23:24:39 +0000413 Tok.is(tok::kw___if_not_exists))) {
414 if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
415 if (Tok.isNot(tok::comma)) break;
416 ConsumeToken();
417 }
418 if (Tok.is(tok::r_brace)) break;
419 continue;
420 }
421
Steve Narofffbd09832007-07-19 01:06:55 +0000422 // Parse: designation[opt] initializer
Sebastian Redld65cea82008-12-11 22:51:44 +0000423
Steve Narofffbd09832007-07-19 01:06:55 +0000424 // If we know that this cannot be a designation, just parse the nested
425 // initializer directly.
John McCalldadc5752010-08-24 06:29:42 +0000426 ExprResult SubElt;
Douglas Gregora80cae12012-02-17 03:49:44 +0000427 if (MayBeDesignationStart())
Douglas Gregor85992cf2009-03-20 23:11:49 +0000428 SubElt = ParseInitializerWithPotentialDesignator();
429 else
Steve Narofffbd09832007-07-19 01:06:55 +0000430 SubElt = ParseInitializer();
Mike Stump11289f42009-09-09 15:08:12 +0000431
Douglas Gregor968f23a2011-01-03 19:31:53 +0000432 if (Tok.is(tok::ellipsis))
433 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
Kaelyn Takata15867822014-11-21 18:48:04 +0000434
435 SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get());
436
Steve Narofffbd09832007-07-19 01:06:55 +0000437 // If we couldn't parse the subelement, bail out.
Kaelyn Takata15867822014-11-21 18:48:04 +0000438 if (SubElt.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000439 InitExprs.push_back(SubElt.get());
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000440 } else {
441 InitExprsOk = false;
Mike Stump11289f42009-09-09 15:08:12 +0000442
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000443 // We have two ways to try to recover from this error: if the code looks
Chris Lattner57540c52011-04-15 05:22:18 +0000444 // grammatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000445 // parsing the rest of the initializer. This allows us to emit
446 // diagnostics for later elements that we find. If we don't see a comma,
447 // assume there is a parse error, and just skip to recover.
Sebastian Redl511ed552008-11-25 22:21:31 +0000448 // FIXME: This comment doesn't sound right. If there is a r_brace
449 // immediately, it can't be an error, since there is no other way of
450 // leaving this loop except through this if.
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000451 if (Tok.isNot(tok::comma)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000452 SkipUntil(tok::r_brace, StopBeforeMatch);
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000453 break;
454 }
455 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000456
Steve Narofffbd09832007-07-19 01:06:55 +0000457 // If we don't have a comma continued list, we're done.
Chris Lattner76c72282007-10-09 17:33:22 +0000458 if (Tok.isNot(tok::comma)) break;
Sebastian Redld65cea82008-12-11 22:51:44 +0000459
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000460 // TODO: save comma locations if some client cares.
Steve Narofffbd09832007-07-19 01:06:55 +0000461 ConsumeToken();
Sebastian Redld65cea82008-12-11 22:51:44 +0000462
Steve Narofffbd09832007-07-19 01:06:55 +0000463 // Handle trailing comma.
Chris Lattner76c72282007-10-09 17:33:22 +0000464 if (Tok.is(tok::r_brace)) break;
Steve Narofffbd09832007-07-19 01:06:55 +0000465 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000466
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000467 bool closed = !T.consumeClose();
468
469 if (InitExprsOk && closed)
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000470 return Actions.ActOnInitList(LBraceLoc, InitExprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000471 T.getCloseLocation());
472
Sebastian Redld65cea82008-12-11 22:51:44 +0000473 return ExprError(); // an error occurred.
Chris Lattner8693a512006-08-13 21:54:02 +0000474}
475
Francois Pichet02513162011-12-12 23:24:39 +0000476
477// Return true if a comma (or closing brace) is necessary after the
478// __if_exists/if_not_exists statement.
479bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
480 bool &InitExprsOk) {
481 bool trailingComma = false;
482 IfExistsCondition Result;
483 if (ParseMicrosoftIfExistsCondition(Result))
484 return false;
485
486 BalancedDelimiterTracker Braces(*this, tok::l_brace);
487 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000488 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet02513162011-12-12 23:24:39 +0000489 return false;
490 }
491
492 switch (Result.Behavior) {
493 case IEB_Parse:
494 // Parse the declarations below.
495 break;
496
497 case IEB_Dependent:
498 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
499 << Result.IsIfExists;
500 // Fall through to skip.
501
502 case IEB_Skip:
503 Braces.skipToEnd();
504 return false;
505 }
506
Richard Smith34f30512013-11-23 04:06:09 +0000507 while (!isEofOrEom()) {
Francois Pichet02513162011-12-12 23:24:39 +0000508 trailingComma = false;
509 // If we know that this cannot be a designation, just parse the nested
510 // initializer directly.
511 ExprResult SubElt;
Douglas Gregora80cae12012-02-17 03:49:44 +0000512 if (MayBeDesignationStart())
Francois Pichet02513162011-12-12 23:24:39 +0000513 SubElt = ParseInitializerWithPotentialDesignator();
514 else
515 SubElt = ParseInitializer();
516
517 if (Tok.is(tok::ellipsis))
518 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
519
520 // If we couldn't parse the subelement, bail out.
521 if (!SubElt.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000522 InitExprs.push_back(SubElt.get());
Francois Pichet02513162011-12-12 23:24:39 +0000523 else
524 InitExprsOk = false;
525
526 if (Tok.is(tok::comma)) {
527 ConsumeToken();
528 trailingComma = true;
529 }
530
531 if (Tok.is(tok::r_brace))
532 break;
533 }
534
535 Braces.consumeClose();
536
537 return !trailingComma;
538}