blob: 90f3561cb9637c32f76034afa8a56937fd750547 [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
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/Parse/ParseDiagnostic.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000015#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000016#include "clang/Parse/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"
Chris Lattner8693a512006-08-13 21:54:02 +000020using namespace clang;
21
22
Douglas Gregora80cae12012-02-17 03:49:44 +000023/// MayBeDesignationStart - Return true if the current token might be the start
24/// of a designator. If we can tell it is impossible that it is a designator,
25/// return false.
26bool Parser::MayBeDesignationStart() {
27 switch (Tok.getKind()) {
28 default:
29 return false;
30
Chris Lattner8693a512006-08-13 21:54:02 +000031 case tok::period: // designator: '.' identifier
Douglas Gregora80cae12012-02-17 03:49:44 +000032 return true;
33
34 case tok::l_square: { // designator: array-designator
Richard Smith2bf7fdb2013-01-02 11:42:31 +000035 if (!PP.getLangOpts().CPlusPlus11)
Chris Lattnerdde65432008-10-26 22:41:58 +000036 return true;
Douglas Gregora80cae12012-02-17 03:49:44 +000037
38 // C++11 lambda expressions and C99 designators can be ambiguous all the
39 // way through the closing ']' and to the next character. Handle the easy
40 // cases here, and fall back to tentative parsing if those fail.
41 switch (PP.LookAhead(0).getKind()) {
42 case tok::equal:
43 case tok::r_square:
44 // Definitely starts a lambda expression.
45 return false;
46
47 case tok::amp:
48 case tok::kw_this:
49 case tok::identifier:
50 // We have to do additional analysis, because these could be the
51 // start of a constant expression or a lambda capture list.
52 break;
53
54 default:
55 // Anything not mentioned above cannot occur following a '[' in a
56 // lambda expression.
57 return true;
58 }
59
Douglas Gregor23b05412012-02-17 16:41:16 +000060 // Handle the complicated case below.
61 break;
Douglas Gregora80cae12012-02-17 03:49:44 +000062 }
Chris Lattner8693a512006-08-13 21:54:02 +000063 case tok::identifier: // designation: identifier ':'
Chris Lattnerdde65432008-10-26 22:41:58 +000064 return PP.LookAhead(0).is(tok::colon);
Chris Lattner8693a512006-08-13 21:54:02 +000065 }
Douglas Gregor23b05412012-02-17 16:41:16 +000066
67 // Parse up to (at most) the token after the closing ']' to determine
Richard Smith9e2f0a42014-04-13 04:31:48 +000068 // whether this is a C99 designator or a lambda.
Douglas Gregor23b05412012-02-17 16:41:16 +000069 TentativeParsingAction Tentative(*this);
Richard Smith9e2f0a42014-04-13 04:31:48 +000070
71 LambdaIntroducer Intro;
72 bool SkippedInits = false;
73 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
74
75 if (DiagID) {
76 // If this can't be a lambda capture list, it's a designator.
77 Tentative.Revert();
78 return true;
Douglas Gregor23b05412012-02-17 16:41:16 +000079 }
Richard Smith9e2f0a42014-04-13 04:31:48 +000080
81 // Once we hit the closing square bracket, we look at the next
82 // token. If it's an '=', this is a designator. Otherwise, it's a
83 // lambda expression. This decision favors lambdas over the older
84 // GNU designator syntax, which allows one to omit the '=', but is
85 // consistent with GCC.
86 tok::TokenKind Kind = Tok.getKind();
87 // FIXME: If we didn't skip any inits, parse the lambda from here
88 // rather than throwing away then reparsing the LambdaIntroducer.
89 Tentative.Revert();
90 return Kind == tok::equal;
Chris Lattner8693a512006-08-13 21:54:02 +000091}
92
Douglas Gregor8d4de672010-04-21 22:36:40 +000093static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
94 Designation &Desig) {
95 // If we have exactly one array designator, this used the GNU
96 // 'designation: array-designator' extension, otherwise there should be no
97 // designators at all!
98 if (Desig.getNumDesignators() == 1 &&
99 (Desig.getDesignator(0).isArrayDesignator() ||
100 Desig.getDesignator(0).isArrayRangeDesignator()))
101 P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
102 else if (Desig.getNumDesignators() > 0)
103 P.Diag(Loc, diag::err_expected_equal_designator);
104}
105
Chris Lattnere7dab442006-08-13 21:54:51 +0000106/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
107/// checking to see if the token stream starts with a designator.
108///
Chris Lattner8693a512006-08-13 21:54:02 +0000109/// designation:
110/// designator-list '='
111/// [GNU] array-designator
112/// [GNU] identifier ':'
113///
114/// designator-list:
115/// designator
116/// designator-list designator
117///
118/// designator:
119/// array-designator
120/// '.' identifier
121///
122/// array-designator:
123/// '[' constant-expression ']'
124/// [GNU] '[' constant-expression '...' constant-expression ']'
125///
126/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattner0c024602008-10-26 21:46:13 +0000127/// initializer (because it is an expression). We need to consider this case
128/// when parsing array designators.
Chris Lattner8693a512006-08-13 21:54:02 +0000129///
John McCalldadc5752010-08-24 06:29:42 +0000130ExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redld65cea82008-12-11 22:51:44 +0000131
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000132 // If this is the old-style GNU extension:
133 // designation ::= identifier ':'
134 // Handle it as a field designator. Otherwise, this must be the start of a
135 // normal expression.
136 if (Tok.is(tok::identifier)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000137 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000138
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000139 SmallString<256> NewSyntax;
Daniel Dunbar07d07852009-10-18 21:17:35 +0000140 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbarebd5b4a2009-10-17 23:52:50 +0000141 << " = ";
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000142
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000143 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000144
Chris Lattnere2b5e872008-10-26 22:49:49 +0000145 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000146 SourceLocation ColonLoc = ConsumeToken();
147
Douglas Gregorafeabec2011-08-27 00:13:16 +0000148 Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
Douglas Gregora771f462010-03-31 17:46:05 +0000149 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
Yaron Keren92e1b622015-03-18 10:17:07 +0000150 NewSyntax);
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000151
Douglas Gregor85992cf2009-03-20 23:11:49 +0000152 Designation D;
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000153 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump11289f42009-09-09 15:08:12 +0000154 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000155 ParseInitializer());
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000156 }
Mike Stump11289f42009-09-09 15:08:12 +0000157
Chris Lattner72432452008-10-26 22:59:19 +0000158 // Desig - This is initialized when we see our first designator. We may have
159 // an objc message send with no designator, so we don't want to create this
160 // eagerly.
Douglas Gregor85992cf2009-03-20 23:11:49 +0000161 Designation Desig;
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattner8693a512006-08-13 21:54:02 +0000163 // Parse each designator in the designator list until we find an initializer.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000164 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
165 if (Tok.is(tok::period)) {
Chris Lattner8693a512006-08-13 21:54:02 +0000166 // designator: '.' identifier
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000167 SourceLocation DotLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000168
Chris Lattner72432452008-10-26 22:59:19 +0000169 if (Tok.isNot(tok::identifier)) {
170 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000171 return ExprError();
Chris Lattner72432452008-10-26 22:59:19 +0000172 }
Mike Stump11289f42009-09-09 15:08:12 +0000173
Douglas Gregor85992cf2009-03-20 23:11:49 +0000174 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
175 Tok.getLocation()));
Chris Lattner72432452008-10-26 22:59:19 +0000176 ConsumeToken(); // Eat the identifier.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000177 continue;
178 }
Mike Stump11289f42009-09-09 15:08:12 +0000179
Chris Lattnere2b5e872008-10-26 22:49:49 +0000180 // We must have either an array designator now or an objc message send.
181 assert(Tok.is(tok::l_square) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +0000182
Chris Lattner8aafd352008-10-26 23:06:54 +0000183 // Handle the two forms of array designator:
184 // array-designator: '[' constant-expression ']'
185 // array-designator: '[' constant-expression '...' constant-expression ']'
186 //
187 // Also, we have to handle the case where the expression after the
188 // designator an an objc message send: '[' objc-message-expr ']'.
189 // Interesting cases are:
190 // [foo bar] -> objc message send
191 // [foo] -> array designator
192 // [foo ... bar] -> array designator
193 // [4][foo bar] -> obsolete GNU designation with objc message send.
194 //
Richard Smith7bdcc4a2012-04-10 01:32:12 +0000195 // We do not need to check for an expression starting with [[ here. If it
196 // contains an Objective-C message send, then it is not an ill-formed
197 // attribute. If it is a lambda-expression within an array-designator, then
198 // it will be rejected because a constant-expression cannot begin with a
199 // lambda-expression.
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000200 InMessageExpressionRAIIObject InMessage(*this, true);
201
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000202 BalancedDelimiterTracker T(*this, tok::l_square);
203 T.consumeOpen();
204 SourceLocation StartLoc = T.getOpenLocation();
205
John McCalldadc5752010-08-24 06:29:42 +0000206 ExprResult Idx;
Mike Stump11289f42009-09-09 15:08:12 +0000207
Douglas Gregor8d4de672010-04-21 22:36:40 +0000208 // If Objective-C is enabled and this is a typename (class message
209 // send) or send to 'super', parse this as a message send
210 // expression. We handle C++ and C separately, since C++ requires
211 // much more complicated parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000212 if (getLangOpts().ObjC1 && getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000213 // Send to 'super'.
214 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000215 NextToken().isNot(tok::period) &&
216 getCurScope()->isInObjcMethodScope()) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000217 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
David Blaikieefdccaa2016-01-15 23:43:34 +0000218 return ParseAssignmentExprWithObjCMessageExprStart(
219 StartLoc, ConsumeToken(), nullptr, nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000220 }
221
222 // Parse the receiver, which is either a type or an expression.
223 bool IsExpr;
224 void *TypeOrExpr;
225 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000226 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000227 return ExprError();
228 }
229
230 // If the receiver was a type, we have a class message; parse
231 // the rest of it.
232 if (!IsExpr) {
233 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
234 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
235 SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +0000236 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +0000237 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000238 }
239
240 // If the receiver was an expression, we still don't know
241 // whether we have a message send or an array designator; just
242 // adopt the expression for further analysis below.
243 // FIXME: potentially-potentially evaluated expression above?
John McCalldadc5752010-08-24 06:29:42 +0000244 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
David Blaikiebbafb8a2012-03-11 07:00:24 +0000245 } else if (getLangOpts().ObjC1 && Tok.is(tok::identifier)) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000246 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000247 SourceLocation IILoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +0000248 ParsedType ReceiverType;
Chris Lattner3adb17d2010-04-12 06:36:00 +0000249 // Three cases. This is a message send to a type: [type foo]
250 // This is a message send to super: [super foo]
251 // This is a message sent to an expr: [super.bar foo]
Aaron Ballman385a8c02015-07-07 13:21:26 +0000252 switch (Actions.getObjCMessageKind(
253 getCurScope(), II, IILoc, II == Ident_super,
254 NextToken().is(tok::period), ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +0000255 case Sema::ObjCSuperMessage:
Douglas Gregore83b9562015-07-07 03:57:53 +0000256 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
David Blaikieefdccaa2016-01-15 23:43:34 +0000257 return ParseAssignmentExprWithObjCMessageExprStart(
258 StartLoc, ConsumeToken(), nullptr, nullptr);
Douglas Gregore83b9562015-07-07 03:57:53 +0000259
John McCallfaf5fb42010-08-26 23:41:50 +0000260 case Sema::ObjCClassMessage:
Douglas Gregor8d4de672010-04-21 22:36:40 +0000261 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
Douglas Gregore5798dc2010-04-21 20:38:13 +0000262 ConsumeToken(); // the identifier
263 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000264 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000265 return ExprError();
266 }
267
Douglas Gregore83b9562015-07-07 03:57:53 +0000268 // Parse type arguments and protocol qualifiers.
269 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000270 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +0000271 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000272 = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType,
273 /*consumeLastToken=*/true,
274 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +0000275 if (!NewReceiverType.isUsable()) {
276 SkipUntil(tok::r_square, StopAtSemi);
277 return ExprError();
278 }
279
280 ReceiverType = NewReceiverType.get();
281 }
282
283 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000284 SourceLocation(),
Douglas Gregore5798dc2010-04-21 20:38:13 +0000285 ReceiverType,
Craig Topper161e4db2014-05-21 06:02:52 +0000286 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000287
John McCallfaf5fb42010-08-26 23:41:50 +0000288 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000289 // Fall through; we'll just parse the expression and
290 // (possibly) treat this like an Objective-C message send
291 // later.
292 break;
Chris Lattnera36ec422010-04-11 08:28:14 +0000293 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000294 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000295
Douglas Gregor8d4de672010-04-21 22:36:40 +0000296 // Parse the index expression, if we haven't already gotten one
297 // above (which can only happen in Objective-C++).
Chris Lattnere2b5e872008-10-26 22:49:49 +0000298 // Note that we parse this as an assignment expression, not a constant
299 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
300 // to validate that the expression is a constant.
Douglas Gregor8d4de672010-04-21 22:36:40 +0000301 // FIXME: We also need to tell Sema that we're in a
302 // potentially-potentially evaluated context.
303 if (!Idx.get()) {
304 Idx = ParseAssignmentExpression();
305 if (Idx.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000306 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000307 return Idx;
Douglas Gregor8d4de672010-04-21 22:36:40 +0000308 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000309 }
Mike Stump11289f42009-09-09 15:08:12 +0000310
Chris Lattnere2b5e872008-10-26 22:49:49 +0000311 // Given an expression, we could either have a designator (if the next
312 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump11289f42009-09-09 15:08:12 +0000313 // message send, handle it now. An objc-message send is the start of
Chris Lattnere2b5e872008-10-26 22:49:49 +0000314 // an assignment-expression production.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000315 if (getLangOpts().ObjC1 && Tok.isNot(tok::ellipsis) &&
Chris Lattnere2b5e872008-10-26 22:49:49 +0000316 Tok.isNot(tok::r_square)) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000317 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
David Blaikieefdccaa2016-01-15 23:43:34 +0000318 return ParseAssignmentExprWithObjCMessageExprStart(
319 StartLoc, SourceLocation(), nullptr, Idx.get());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000320 }
Chris Lattner8aafd352008-10-26 23:06:54 +0000321
Chris Lattner8aafd352008-10-26 23:06:54 +0000322 // If this is a normal array designator, remember it.
323 if (Tok.isNot(tok::ellipsis)) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000324 Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc));
Chris Lattner8aafd352008-10-26 23:06:54 +0000325 } else {
326 // Handle the gnu array range extension.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000327 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000328 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000329
John McCalldadc5752010-08-24 06:29:42 +0000330 ExprResult RHS(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000331 if (RHS.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000332 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000333 return RHS;
Chris Lattner8693a512006-08-13 21:54:02 +0000334 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000335 Desig.AddDesignator(Designator::getArrayRange(Idx.get(),
336 RHS.get(),
Douglas Gregor85992cf2009-03-20 23:11:49 +0000337 StartLoc, EllipsisLoc));
Chris Lattner8693a512006-08-13 21:54:02 +0000338 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000339
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000340 T.consumeClose();
341 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
342 T.getCloseLocation());
Chris Lattner8693a512006-08-13 21:54:02 +0000343 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000344
Chris Lattner72432452008-10-26 22:59:19 +0000345 // Okay, we're done with the designator sequence. We know that there must be
346 // at least one designator, because the only case we can get into this method
347 // without a designator is when we have an objc message send. That case is
348 // handled and returned from above.
Douglas Gregor85992cf2009-03-20 23:11:49 +0000349 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redld65cea82008-12-11 22:51:44 +0000350
Chris Lattner72432452008-10-26 22:59:19 +0000351 // Handle a normal designator sequence end, which is an equal.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000352 if (Tok.is(tok::equal)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000353 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor85992cf2009-03-20 23:11:49 +0000354 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000355 ParseInitializer());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000356 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000357
Chris Lattner72432452008-10-26 22:59:19 +0000358 // We read some number of designators and found something that isn't an = or
Chris Lattner46dcba62008-10-26 23:22:23 +0000359 // an initializer. If we have exactly one array designator, this
Chris Lattner72432452008-10-26 22:59:19 +0000360 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
361 // parse error.
Mike Stump11289f42009-09-09 15:08:12 +0000362 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor85992cf2009-03-20 23:11:49 +0000363 (Desig.getDesignator(0).isArrayDesignator() ||
364 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000365 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregora771f462010-03-31 17:46:05 +0000366 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000367 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor8aa6bf52009-03-27 23:40:29 +0000368 true, ParseInitializer());
Chris Lattner46dcba62008-10-26 23:22:23 +0000369 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000370
Chris Lattner46dcba62008-10-26 23:22:23 +0000371 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000372 return ExprError();
Chris Lattner8693a512006-08-13 21:54:02 +0000373}
374
375
Chris Lattner3fa92392008-10-26 22:38:55 +0000376/// ParseBraceInitializer - Called when parsing an initializer that has a
377/// leading open brace.
378///
Chris Lattner8693a512006-08-13 21:54:02 +0000379/// initializer: [C99 6.7.8]
Chris Lattner8693a512006-08-13 21:54:02 +0000380/// '{' initializer-list '}'
381/// '{' initializer-list ',' '}'
382/// [GNU] '{' '}'
383///
384/// initializer-list:
Douglas Gregor968f23a2011-01-03 19:31:53 +0000385/// designation[opt] initializer ...[opt]
386/// initializer-list ',' designation[opt] initializer ...[opt]
Chris Lattner8693a512006-08-13 21:54:02 +0000387///
John McCalldadc5752010-08-24 06:29:42 +0000388ExprResult Parser::ParseBraceInitializer() {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000389 InMessageExpressionRAIIObject InMessage(*this, false);
390
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000391 BalancedDelimiterTracker T(*this, tok::l_brace);
392 T.consumeOpen();
393 SourceLocation LBraceLoc = T.getOpenLocation();
Sebastian Redl511ed552008-11-25 22:21:31 +0000394
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000395 /// InitExprs - This is the actual list of expressions contained in the
396 /// initializer.
Benjamin Kramerf0623432012-08-23 22:51:59 +0000397 ExprVector InitExprs;
Sebastian Redl511ed552008-11-25 22:21:31 +0000398
Chris Lattner248388e2008-10-26 23:35:51 +0000399 if (Tok.is(tok::r_brace)) {
Douglas Gregord14247a2009-01-30 22:09:00 +0000400 // Empty initializers are a C++ feature and a GNU extension to C.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000401 if (!getLangOpts().CPlusPlus)
Douglas Gregord14247a2009-01-30 22:09:00 +0000402 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner248388e2008-10-26 23:35:51 +0000403 // Match the '}'.
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000404 return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace());
Chris Lattner248388e2008-10-26 23:35:51 +0000405 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000406
Richard Smithd6a15082017-01-07 00:48:55 +0000407 // Enter an appropriate expression evaluation context for an initializer list.
408 EnterExpressionEvaluationContext EnterContext(
409 Actions, EnterExpressionEvaluationContext::InitList);
410
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.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000415 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet02513162011-12-12 23:24:39 +0000416 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());
Kaelyn Takata15867822014-11-21 18:48:04 +0000437
438 SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get());
439
Steve Narofffbd09832007-07-19 01:06:55 +0000440 // If we couldn't parse the subelement, bail out.
Kaelyn Takata15867822014-11-21 18:48:04 +0000441 if (SubElt.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000442 InitExprs.push_back(SubElt.get());
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000443 } else {
444 InitExprsOk = false;
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000446 // We have two ways to try to recover from this error: if the code looks
Chris Lattner57540c52011-04-15 05:22:18 +0000447 // grammatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000448 // parsing the rest of the initializer. This allows us to emit
449 // diagnostics for later elements that we find. If we don't see a comma,
450 // assume there is a parse error, and just skip to recover.
Sebastian Redl511ed552008-11-25 22:21:31 +0000451 // FIXME: This comment doesn't sound right. If there is a r_brace
452 // immediately, it can't be an error, since there is no other way of
453 // leaving this loop except through this if.
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000454 if (Tok.isNot(tok::comma)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000455 SkipUntil(tok::r_brace, StopBeforeMatch);
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000456 break;
457 }
458 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000459
Steve Narofffbd09832007-07-19 01:06:55 +0000460 // If we don't have a comma continued list, we're done.
Chris Lattner76c72282007-10-09 17:33:22 +0000461 if (Tok.isNot(tok::comma)) break;
Sebastian Redld65cea82008-12-11 22:51:44 +0000462
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000463 // TODO: save comma locations if some client cares.
Steve Narofffbd09832007-07-19 01:06:55 +0000464 ConsumeToken();
Sebastian Redld65cea82008-12-11 22:51:44 +0000465
Steve Narofffbd09832007-07-19 01:06:55 +0000466 // Handle trailing comma.
Chris Lattner76c72282007-10-09 17:33:22 +0000467 if (Tok.is(tok::r_brace)) break;
Steve Narofffbd09832007-07-19 01:06:55 +0000468 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000469
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000470 bool closed = !T.consumeClose();
471
472 if (InitExprsOk && closed)
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000473 return Actions.ActOnInitList(LBraceLoc, InitExprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000474 T.getCloseLocation());
475
Sebastian Redld65cea82008-12-11 22:51:44 +0000476 return ExprError(); // an error occurred.
Chris Lattner8693a512006-08-13 21:54:02 +0000477}
478
Francois Pichet02513162011-12-12 23:24:39 +0000479
480// Return true if a comma (or closing brace) is necessary after the
481// __if_exists/if_not_exists statement.
482bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
483 bool &InitExprsOk) {
484 bool trailingComma = false;
485 IfExistsCondition Result;
486 if (ParseMicrosoftIfExistsCondition(Result))
487 return false;
488
489 BalancedDelimiterTracker Braces(*this, tok::l_brace);
490 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000491 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet02513162011-12-12 23:24:39 +0000492 return false;
493 }
494
495 switch (Result.Behavior) {
496 case IEB_Parse:
497 // Parse the declarations below.
498 break;
499
500 case IEB_Dependent:
501 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
502 << Result.IsIfExists;
503 // Fall through to skip.
Galina Kistanova1aead4f2017-06-01 21:23:52 +0000504 LLVM_FALLTHROUGH;
505
Francois Pichet02513162011-12-12 23:24:39 +0000506 case IEB_Skip:
507 Braces.skipToEnd();
508 return false;
509 }
510
Richard Smith34f30512013-11-23 04:06:09 +0000511 while (!isEofOrEom()) {
Francois Pichet02513162011-12-12 23:24:39 +0000512 trailingComma = false;
513 // If we know that this cannot be a designation, just parse the nested
514 // initializer directly.
515 ExprResult SubElt;
Douglas Gregora80cae12012-02-17 03:49:44 +0000516 if (MayBeDesignationStart())
Francois Pichet02513162011-12-12 23:24:39 +0000517 SubElt = ParseInitializerWithPotentialDesignator();
518 else
519 SubElt = ParseInitializer();
520
521 if (Tok.is(tok::ellipsis))
522 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
523
524 // If we couldn't parse the subelement, bail out.
525 if (!SubElt.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000526 InitExprs.push_back(SubElt.get());
Francois Pichet02513162011-12-12 23:24:39 +0000527 else
528 InitExprsOk = false;
529
530 if (Tok.is(tok::comma)) {
531 ConsumeToken();
532 trailingComma = true;
533 }
534
535 if (Tok.is(tok::r_brace))
536 break;
537 }
538
539 Braces.consumeClose();
540
541 return !trailingComma;
542}