blob: 5ab055130dc2ae87ade294823c3c0d8c75a61604 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
Chris Lattner8693a512006-08-13 21:54:02 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner8693a512006-08-13 21:54:02 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements initializer parsing as specified by C99 6.7.8.
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth3a022472012-12-04 09:13:33 +000013#include "clang/Parse/ParseDiagnostic.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000014#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000015#include "clang/Parse/RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Sema/Designator.h"
17#include "clang/Sema/Scope.h"
Steve Narofffbd09832007-07-19 01:06:55 +000018#include "llvm/ADT/SmallString.h"
Chris Lattner8693a512006-08-13 21:54:02 +000019using namespace clang;
20
21
Fangrui Song6907ce22018-07-30 19:24:48 +000022/// MayBeDesignationStart - Return true if the current token might be the start
23/// of a designator. If we can tell it is impossible that it is a designator,
Douglas Gregora80cae12012-02-17 03:49:44 +000024/// return false.
25bool Parser::MayBeDesignationStart() {
26 switch (Tok.getKind()) {
Fangrui Song6907ce22018-07-30 19:24:48 +000027 default:
Douglas Gregora80cae12012-02-17 03:49:44 +000028 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +000029
Chris Lattner8693a512006-08-13 21:54:02 +000030 case tok::period: // designator: '.' identifier
Douglas Gregora80cae12012-02-17 03:49:44 +000031 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +000032
Douglas Gregora80cae12012-02-17 03:49:44 +000033 case tok::l_square: { // designator: array-designator
Richard Smith2bf7fdb2013-01-02 11:42:31 +000034 if (!PP.getLangOpts().CPlusPlus11)
Chris Lattnerdde65432008-10-26 22:41:58 +000035 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +000036
Douglas Gregora80cae12012-02-17 03:49:44 +000037 // C++11 lambda expressions and C99 designators can be ambiguous all the
38 // way through the closing ']' and to the next character. Handle the easy
39 // cases here, and fall back to tentative parsing if those fail.
40 switch (PP.LookAhead(0).getKind()) {
41 case tok::equal:
Richard Smithb26bc342019-08-26 22:51:28 +000042 case tok::ellipsis:
Douglas Gregora80cae12012-02-17 03:49:44 +000043 case tok::r_square:
44 // Definitely starts a lambda expression.
45 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +000046
Douglas Gregora80cae12012-02-17 03:49:44 +000047 case tok::amp:
48 case tok::kw_this:
Erik Pilkingtonbe19c482019-07-30 19:21:20 +000049 case tok::star:
Douglas Gregora80cae12012-02-17 03:49:44 +000050 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;
Fangrui Song6907ce22018-07-30 19:24:48 +000054
Douglas Gregora80cae12012-02-17 03:49:44 +000055 default:
Fangrui Song6907ce22018-07-30 19:24:48 +000056 // Anything not mentioned above cannot occur following a '[' in a
Douglas Gregora80cae12012-02-17 03:49:44 +000057 // lambda expression.
Fangrui Song6907ce22018-07-30 19:24:48 +000058 return true;
Douglas Gregora80cae12012-02-17 03:49:44 +000059 }
Fangrui Song6907ce22018-07-30 19:24:48 +000060
Douglas Gregor23b05412012-02-17 16:41:16 +000061 // Handle the complicated case below.
Fangrui Song6907ce22018-07-30 19:24:48 +000062 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 }
Fangrui Song6907ce22018-07-30 19:24:48 +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.
Richard Smithe9585062019-05-20 18:01:54 +000070 RevertingTentativeParsingAction Tentative(*this);
Richard Smith9e2f0a42014-04-13 04:31:48 +000071
72 LambdaIntroducer Intro;
Richard Smithe9585062019-05-20 18:01:54 +000073 LambdaIntroducerTentativeParse ParseResult;
74 if (ParseLambdaIntroducer(Intro, &ParseResult)) {
75 // Hit and diagnosed an error in a lambda.
76 // FIXME: Tell the caller this happened so they can recover.
77 return true;
78 }
Richard Smith9e2f0a42014-04-13 04:31:48 +000079
Richard Smithe9585062019-05-20 18:01:54 +000080 switch (ParseResult) {
81 case LambdaIntroducerTentativeParse::Success:
82 case LambdaIntroducerTentativeParse::Incomplete:
83 // Might be a lambda-expression. Keep looking.
84 // FIXME: If our tentative parse was not incomplete, parse the lambda from
85 // here rather than throwing away then reparsing the LambdaIntroducer.
86 break;
87
88 case LambdaIntroducerTentativeParse::MessageSend:
89 case LambdaIntroducerTentativeParse::Invalid:
90 // Can't be a lambda-expression. Treat it as a designator.
91 // FIXME: Should we disambiguate against a message-send?
Richard Smith9e2f0a42014-04-13 04:31:48 +000092 return true;
Douglas Gregor23b05412012-02-17 16:41:16 +000093 }
Richard Smith9e2f0a42014-04-13 04:31:48 +000094
95 // Once we hit the closing square bracket, we look at the next
96 // token. If it's an '=', this is a designator. Otherwise, it's a
97 // lambda expression. This decision favors lambdas over the older
98 // GNU designator syntax, which allows one to omit the '=', but is
99 // consistent with GCC.
Richard Smithe9585062019-05-20 18:01:54 +0000100 return Tok.is(tok::equal);
Chris Lattner8693a512006-08-13 21:54:02 +0000101}
102
Douglas Gregor8d4de672010-04-21 22:36:40 +0000103static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
104 Designation &Desig) {
105 // If we have exactly one array designator, this used the GNU
106 // 'designation: array-designator' extension, otherwise there should be no
107 // designators at all!
108 if (Desig.getNumDesignators() == 1 &&
109 (Desig.getDesignator(0).isArrayDesignator() ||
110 Desig.getDesignator(0).isArrayRangeDesignator()))
111 P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
112 else if (Desig.getNumDesignators() > 0)
113 P.Diag(Loc, diag::err_expected_equal_designator);
114}
115
Chris Lattnere7dab442006-08-13 21:54:51 +0000116/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
117/// checking to see if the token stream starts with a designator.
118///
Richard Smithff9bf922019-08-31 01:00:37 +0000119/// C99:
120///
Chris Lattner8693a512006-08-13 21:54:02 +0000121/// designation:
122/// designator-list '='
123/// [GNU] array-designator
124/// [GNU] identifier ':'
125///
126/// designator-list:
127/// designator
128/// designator-list designator
129///
130/// designator:
131/// array-designator
132/// '.' identifier
133///
134/// array-designator:
135/// '[' constant-expression ']'
136/// [GNU] '[' constant-expression '...' constant-expression ']'
137///
Richard Smithff9bf922019-08-31 01:00:37 +0000138/// C++20:
139///
140/// designated-initializer-list:
141/// designated-initializer-clause
142/// designated-initializer-list ',' designated-initializer-clause
143///
144/// designated-initializer-clause:
145/// designator brace-or-equal-initializer
146///
147/// designator:
148/// '.' identifier
149///
150/// We allow the C99 syntax extensions in C++20, but do not allow the C++20
151/// extension (a braced-init-list after the designator with no '=') in C99.
152///
Chris Lattner8693a512006-08-13 21:54:02 +0000153/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
Chris Lattner0c024602008-10-26 21:46:13 +0000154/// initializer (because it is an expression). We need to consider this case
155/// when parsing array designators.
Chris Lattner8693a512006-08-13 21:54:02 +0000156///
John McCalldadc5752010-08-24 06:29:42 +0000157ExprResult Parser::ParseInitializerWithPotentialDesignator() {
Sebastian Redld65cea82008-12-11 22:51:44 +0000158
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000159 // If this is the old-style GNU extension:
160 // designation ::= identifier ':'
161 // Handle it as a field designator. Otherwise, this must be the start of a
162 // normal expression.
163 if (Tok.is(tok::identifier)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000164 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000165
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000166 SmallString<256> NewSyntax;
Daniel Dunbar07d07852009-10-18 21:17:35 +0000167 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
Daniel Dunbarebd5b4a2009-10-17 23:52:50 +0000168 << " = ";
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000169
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000170 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000171
Chris Lattnere2b5e872008-10-26 22:49:49 +0000172 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000173 SourceLocation ColonLoc = ConsumeToken();
174
Douglas Gregorafeabec2011-08-27 00:13:16 +0000175 Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
Douglas Gregora771f462010-03-31 17:46:05 +0000176 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
Yaron Keren92e1b622015-03-18 10:17:07 +0000177 NewSyntax);
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000178
Douglas Gregor85992cf2009-03-20 23:11:49 +0000179 Designation D;
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000180 D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
Mike Stump11289f42009-09-09 15:08:12 +0000181 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000182 ParseInitializer());
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000183 }
Mike Stump11289f42009-09-09 15:08:12 +0000184
Chris Lattner72432452008-10-26 22:59:19 +0000185 // Desig - This is initialized when we see our first designator. We may have
186 // an objc message send with no designator, so we don't want to create this
187 // eagerly.
Douglas Gregor85992cf2009-03-20 23:11:49 +0000188 Designation Desig;
Mike Stump11289f42009-09-09 15:08:12 +0000189
Chris Lattner8693a512006-08-13 21:54:02 +0000190 // Parse each designator in the designator list until we find an initializer.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000191 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
192 if (Tok.is(tok::period)) {
Chris Lattner8693a512006-08-13 21:54:02 +0000193 // designator: '.' identifier
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000194 SourceLocation DotLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000195
Chris Lattner72432452008-10-26 22:59:19 +0000196 if (Tok.isNot(tok::identifier)) {
197 Diag(Tok.getLocation(), diag::err_expected_field_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000198 return ExprError();
Chris Lattner72432452008-10-26 22:59:19 +0000199 }
Mike Stump11289f42009-09-09 15:08:12 +0000200
Douglas Gregor85992cf2009-03-20 23:11:49 +0000201 Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
202 Tok.getLocation()));
Chris Lattner72432452008-10-26 22:59:19 +0000203 ConsumeToken(); // Eat the identifier.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000204 continue;
205 }
Mike Stump11289f42009-09-09 15:08:12 +0000206
Chris Lattnere2b5e872008-10-26 22:49:49 +0000207 // We must have either an array designator now or an objc message send.
208 assert(Tok.is(tok::l_square) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +0000209
Chris Lattner8aafd352008-10-26 23:06:54 +0000210 // Handle the two forms of array designator:
211 // array-designator: '[' constant-expression ']'
212 // array-designator: '[' constant-expression '...' constant-expression ']'
213 //
214 // Also, we have to handle the case where the expression after the
215 // designator an an objc message send: '[' objc-message-expr ']'.
216 // Interesting cases are:
217 // [foo bar] -> objc message send
218 // [foo] -> array designator
219 // [foo ... bar] -> array designator
220 // [4][foo bar] -> obsolete GNU designation with objc message send.
221 //
Richard Smith7bdcc4a2012-04-10 01:32:12 +0000222 // We do not need to check for an expression starting with [[ here. If it
223 // contains an Objective-C message send, then it is not an ill-formed
224 // attribute. If it is a lambda-expression within an array-designator, then
225 // it will be rejected because a constant-expression cannot begin with a
226 // lambda-expression.
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000227 InMessageExpressionRAIIObject InMessage(*this, true);
Fangrui Song6907ce22018-07-30 19:24:48 +0000228
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000229 BalancedDelimiterTracker T(*this, tok::l_square);
230 T.consumeOpen();
231 SourceLocation StartLoc = T.getOpenLocation();
232
John McCalldadc5752010-08-24 06:29:42 +0000233 ExprResult Idx;
Mike Stump11289f42009-09-09 15:08:12 +0000234
Douglas Gregor8d4de672010-04-21 22:36:40 +0000235 // If Objective-C is enabled and this is a typename (class message
236 // send) or send to 'super', parse this as a message send
237 // expression. We handle C++ and C separately, since C++ requires
238 // much more complicated parsing.
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000239 if (getLangOpts().ObjC && getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000240 // Send to 'super'.
241 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Fangrui Song6907ce22018-07-30 19:24:48 +0000242 NextToken().isNot(tok::period) &&
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000243 getCurScope()->isInObjcMethodScope()) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000244 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
David Blaikieefdccaa2016-01-15 23:43:34 +0000245 return ParseAssignmentExprWithObjCMessageExprStart(
246 StartLoc, ConsumeToken(), nullptr, nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000247 }
248
249 // Parse the receiver, which is either a type or an expression.
250 bool IsExpr;
251 void *TypeOrExpr;
252 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000253 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000254 return ExprError();
255 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000256
Douglas Gregor8d4de672010-04-21 22:36:40 +0000257 // If the receiver was a type, we have a class message; parse
258 // the rest of it.
259 if (!IsExpr) {
260 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
Fangrui Song6907ce22018-07-30 19:24:48 +0000261 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
262 SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +0000263 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +0000264 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +0000265 }
266
267 // If the receiver was an expression, we still don't know
268 // whether we have a message send or an array designator; just
269 // adopt the expression for further analysis below.
270 // FIXME: potentially-potentially evaluated expression above?
John McCalldadc5752010-08-24 06:29:42 +0000271 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000272 } else if (getLangOpts().ObjC && Tok.is(tok::identifier)) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000273 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000274 SourceLocation IILoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +0000275 ParsedType ReceiverType;
Chris Lattner3adb17d2010-04-12 06:36:00 +0000276 // Three cases. This is a message send to a type: [type foo]
277 // This is a message send to super: [super foo]
278 // This is a message sent to an expr: [super.bar foo]
Aaron Ballman385a8c02015-07-07 13:21:26 +0000279 switch (Actions.getObjCMessageKind(
280 getCurScope(), II, IILoc, II == Ident_super,
281 NextToken().is(tok::period), ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +0000282 case Sema::ObjCSuperMessage:
Douglas Gregore83b9562015-07-07 03:57:53 +0000283 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
David Blaikieefdccaa2016-01-15 23:43:34 +0000284 return ParseAssignmentExprWithObjCMessageExprStart(
285 StartLoc, ConsumeToken(), nullptr, nullptr);
Douglas Gregore83b9562015-07-07 03:57:53 +0000286
John McCallfaf5fb42010-08-26 23:41:50 +0000287 case Sema::ObjCClassMessage:
Douglas Gregor8d4de672010-04-21 22:36:40 +0000288 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
Douglas Gregore5798dc2010-04-21 20:38:13 +0000289 ConsumeToken(); // the identifier
290 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000291 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000292 return ExprError();
293 }
294
Douglas Gregore83b9562015-07-07 03:57:53 +0000295 // Parse type arguments and protocol qualifiers.
296 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000297 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +0000298 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000299 = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType,
300 /*consumeLastToken=*/true,
301 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +0000302 if (!NewReceiverType.isUsable()) {
303 SkipUntil(tok::r_square, StopAtSemi);
304 return ExprError();
305 }
306
307 ReceiverType = NewReceiverType.get();
308 }
309
310 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
Fangrui Song6907ce22018-07-30 19:24:48 +0000311 SourceLocation(),
312 ReceiverType,
Craig Topper161e4db2014-05-21 06:02:52 +0000313 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000314
John McCallfaf5fb42010-08-26 23:41:50 +0000315 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000316 // Fall through; we'll just parse the expression and
317 // (possibly) treat this like an Objective-C message send
318 // later.
319 break;
Chris Lattnera36ec422010-04-11 08:28:14 +0000320 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000321 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000322
Douglas Gregor8d4de672010-04-21 22:36:40 +0000323 // Parse the index expression, if we haven't already gotten one
324 // above (which can only happen in Objective-C++).
Chris Lattnere2b5e872008-10-26 22:49:49 +0000325 // Note that we parse this as an assignment expression, not a constant
326 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
327 // to validate that the expression is a constant.
Douglas Gregor8d4de672010-04-21 22:36:40 +0000328 // FIXME: We also need to tell Sema that we're in a
329 // potentially-potentially evaluated context.
330 if (!Idx.get()) {
331 Idx = ParseAssignmentExpression();
332 if (Idx.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000333 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000334 return Idx;
Douglas Gregor8d4de672010-04-21 22:36:40 +0000335 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000336 }
Mike Stump11289f42009-09-09 15:08:12 +0000337
Chris Lattnere2b5e872008-10-26 22:49:49 +0000338 // Given an expression, we could either have a designator (if the next
339 // tokens are '...' or ']' or an objc message send. If this is an objc
Mike Stump11289f42009-09-09 15:08:12 +0000340 // message send, handle it now. An objc-message send is the start of
Chris Lattnere2b5e872008-10-26 22:49:49 +0000341 // an assignment-expression production.
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000342 if (getLangOpts().ObjC && Tok.isNot(tok::ellipsis) &&
Chris Lattnere2b5e872008-10-26 22:49:49 +0000343 Tok.isNot(tok::r_square)) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000344 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
David Blaikieefdccaa2016-01-15 23:43:34 +0000345 return ParseAssignmentExprWithObjCMessageExprStart(
346 StartLoc, SourceLocation(), nullptr, Idx.get());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000347 }
Chris Lattner8aafd352008-10-26 23:06:54 +0000348
Chris Lattner8aafd352008-10-26 23:06:54 +0000349 // If this is a normal array designator, remember it.
350 if (Tok.isNot(tok::ellipsis)) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000351 Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc));
Chris Lattner8aafd352008-10-26 23:06:54 +0000352 } else {
353 // Handle the gnu array range extension.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000354 Diag(Tok, diag::ext_gnu_array_range);
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000355 SourceLocation EllipsisLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000356
John McCalldadc5752010-08-24 06:29:42 +0000357 ExprResult RHS(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000358 if (RHS.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000359 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000360 return RHS;
Chris Lattner8693a512006-08-13 21:54:02 +0000361 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000362 Desig.AddDesignator(Designator::getArrayRange(Idx.get(),
363 RHS.get(),
Douglas Gregor85992cf2009-03-20 23:11:49 +0000364 StartLoc, EllipsisLoc));
Chris Lattner8693a512006-08-13 21:54:02 +0000365 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000366
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000367 T.consumeClose();
368 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
369 T.getCloseLocation());
Chris Lattner8693a512006-08-13 21:54:02 +0000370 }
Chris Lattnere2b5e872008-10-26 22:49:49 +0000371
Chris Lattner72432452008-10-26 22:59:19 +0000372 // Okay, we're done with the designator sequence. We know that there must be
373 // at least one designator, because the only case we can get into this method
374 // without a designator is when we have an objc message send. That case is
375 // handled and returned from above.
Douglas Gregor85992cf2009-03-20 23:11:49 +0000376 assert(!Desig.empty() && "Designator is empty?");
Sebastian Redld65cea82008-12-11 22:51:44 +0000377
Chris Lattner72432452008-10-26 22:59:19 +0000378 // Handle a normal designator sequence end, which is an equal.
Chris Lattnere2b5e872008-10-26 22:49:49 +0000379 if (Tok.is(tok::equal)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000380 SourceLocation EqualLoc = ConsumeToken();
Douglas Gregor85992cf2009-03-20 23:11:49 +0000381 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000382 ParseInitializer());
Chris Lattnere2b5e872008-10-26 22:49:49 +0000383 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000384
Richard Smithff9bf922019-08-31 01:00:37 +0000385 // Handle a C++20 braced designated initialization, which results in
386 // direct-list-initialization of the aggregate element. We allow this as an
387 // extension from C++11 onwards (when direct-list-initialization was added).
388 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
389 return Actions.ActOnDesignatedInitializer(Desig, SourceLocation(), false,
390 ParseBraceInitializer());
391 }
392
Chris Lattner72432452008-10-26 22:59:19 +0000393 // We read some number of designators and found something that isn't an = or
Chris Lattner46dcba62008-10-26 23:22:23 +0000394 // an initializer. If we have exactly one array designator, this
Chris Lattner72432452008-10-26 22:59:19 +0000395 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
396 // parse error.
Mike Stump11289f42009-09-09 15:08:12 +0000397 if (Desig.getNumDesignators() == 1 &&
Douglas Gregor85992cf2009-03-20 23:11:49 +0000398 (Desig.getDesignator(0).isArrayDesignator() ||
399 Desig.getDesignator(0).isArrayRangeDesignator())) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000400 Diag(Tok, diag::ext_gnu_missing_equal_designator)
Douglas Gregora771f462010-03-31 17:46:05 +0000401 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +0000402 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
Douglas Gregor8aa6bf52009-03-27 23:40:29 +0000403 true, ParseInitializer());
Chris Lattner46dcba62008-10-26 23:22:23 +0000404 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000405
Chris Lattner46dcba62008-10-26 23:22:23 +0000406 Diag(Tok, diag::err_expected_equal_designator);
Sebastian Redld65cea82008-12-11 22:51:44 +0000407 return ExprError();
Chris Lattner8693a512006-08-13 21:54:02 +0000408}
409
410
Chris Lattner3fa92392008-10-26 22:38:55 +0000411/// ParseBraceInitializer - Called when parsing an initializer that has a
412/// leading open brace.
413///
Chris Lattner8693a512006-08-13 21:54:02 +0000414/// initializer: [C99 6.7.8]
Chris Lattner8693a512006-08-13 21:54:02 +0000415/// '{' initializer-list '}'
416/// '{' initializer-list ',' '}'
417/// [GNU] '{' '}'
418///
419/// initializer-list:
Douglas Gregor968f23a2011-01-03 19:31:53 +0000420/// designation[opt] initializer ...[opt]
421/// initializer-list ',' designation[opt] initializer ...[opt]
Chris Lattner8693a512006-08-13 21:54:02 +0000422///
John McCalldadc5752010-08-24 06:29:42 +0000423ExprResult Parser::ParseBraceInitializer() {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000424 InMessageExpressionRAIIObject InMessage(*this, false);
Fangrui Song6907ce22018-07-30 19:24:48 +0000425
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000426 BalancedDelimiterTracker T(*this, tok::l_brace);
427 T.consumeOpen();
428 SourceLocation LBraceLoc = T.getOpenLocation();
Sebastian Redl511ed552008-11-25 22:21:31 +0000429
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000430 /// InitExprs - This is the actual list of expressions contained in the
431 /// initializer.
Benjamin Kramerf0623432012-08-23 22:51:59 +0000432 ExprVector InitExprs;
Sebastian Redl511ed552008-11-25 22:21:31 +0000433
Chris Lattner248388e2008-10-26 23:35:51 +0000434 if (Tok.is(tok::r_brace)) {
Douglas Gregord14247a2009-01-30 22:09:00 +0000435 // Empty initializers are a C++ feature and a GNU extension to C.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000436 if (!getLangOpts().CPlusPlus)
Douglas Gregord14247a2009-01-30 22:09:00 +0000437 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
Chris Lattner248388e2008-10-26 23:35:51 +0000438 // Match the '}'.
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000439 return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace());
Chris Lattner248388e2008-10-26 23:35:51 +0000440 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000441
Richard Smithd6a15082017-01-07 00:48:55 +0000442 // Enter an appropriate expression evaluation context for an initializer list.
443 EnterExpressionEvaluationContext EnterContext(
444 Actions, EnterExpressionEvaluationContext::InitList);
445
Steve Narofffbd09832007-07-19 01:06:55 +0000446 bool InitExprsOk = true;
Sebastian Redld65cea82008-12-11 22:51:44 +0000447
Steve Narofffbd09832007-07-19 01:06:55 +0000448 while (1) {
Francois Pichet02513162011-12-12 23:24:39 +0000449 // Handle Microsoft __if_exists/if_not_exists if necessary.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000450 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet02513162011-12-12 23:24:39 +0000451 Tok.is(tok::kw___if_not_exists))) {
452 if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
453 if (Tok.isNot(tok::comma)) break;
454 ConsumeToken();
455 }
456 if (Tok.is(tok::r_brace)) break;
457 continue;
458 }
459
Steve Narofffbd09832007-07-19 01:06:55 +0000460 // Parse: designation[opt] initializer
Sebastian Redld65cea82008-12-11 22:51:44 +0000461
Steve Narofffbd09832007-07-19 01:06:55 +0000462 // If we know that this cannot be a designation, just parse the nested
463 // initializer directly.
John McCalldadc5752010-08-24 06:29:42 +0000464 ExprResult SubElt;
Douglas Gregora80cae12012-02-17 03:49:44 +0000465 if (MayBeDesignationStart())
Douglas Gregor85992cf2009-03-20 23:11:49 +0000466 SubElt = ParseInitializerWithPotentialDesignator();
467 else
Steve Narofffbd09832007-07-19 01:06:55 +0000468 SubElt = ParseInitializer();
Mike Stump11289f42009-09-09 15:08:12 +0000469
Douglas Gregor968f23a2011-01-03 19:31:53 +0000470 if (Tok.is(tok::ellipsis))
471 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
Kaelyn Takata15867822014-11-21 18:48:04 +0000472
473 SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get());
474
Steve Narofffbd09832007-07-19 01:06:55 +0000475 // If we couldn't parse the subelement, bail out.
Kaelyn Takata15867822014-11-21 18:48:04 +0000476 if (SubElt.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000477 InitExprs.push_back(SubElt.get());
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000478 } else {
479 InitExprsOk = false;
Mike Stump11289f42009-09-09 15:08:12 +0000480
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000481 // We have two ways to try to recover from this error: if the code looks
Chris Lattner57540c52011-04-15 05:22:18 +0000482 // grammatically ok (i.e. we have a comma coming up) try to continue
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000483 // parsing the rest of the initializer. This allows us to emit
484 // diagnostics for later elements that we find. If we don't see a comma,
485 // assume there is a parse error, and just skip to recover.
Sebastian Redl511ed552008-11-25 22:21:31 +0000486 // FIXME: This comment doesn't sound right. If there is a r_brace
487 // immediately, it can't be an error, since there is no other way of
488 // leaving this loop except through this if.
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000489 if (Tok.isNot(tok::comma)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000490 SkipUntil(tok::r_brace, StopBeforeMatch);
Chris Lattner14cd1ee2008-04-20 19:07:56 +0000491 break;
492 }
493 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000494
Steve Narofffbd09832007-07-19 01:06:55 +0000495 // If we don't have a comma continued list, we're done.
Chris Lattner76c72282007-10-09 17:33:22 +0000496 if (Tok.isNot(tok::comma)) break;
Sebastian Redld65cea82008-12-11 22:51:44 +0000497
Chris Lattnerf3e58e22008-10-26 22:36:07 +0000498 // TODO: save comma locations if some client cares.
Steve Narofffbd09832007-07-19 01:06:55 +0000499 ConsumeToken();
Sebastian Redld65cea82008-12-11 22:51:44 +0000500
Steve Narofffbd09832007-07-19 01:06:55 +0000501 // Handle trailing comma.
Chris Lattner76c72282007-10-09 17:33:22 +0000502 if (Tok.is(tok::r_brace)) break;
Steve Narofffbd09832007-07-19 01:06:55 +0000503 }
Sebastian Redld65cea82008-12-11 22:51:44 +0000504
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000505 bool closed = !T.consumeClose();
506
507 if (InitExprsOk && closed)
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000508 return Actions.ActOnInitList(LBraceLoc, InitExprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000509 T.getCloseLocation());
510
Sebastian Redld65cea82008-12-11 22:51:44 +0000511 return ExprError(); // an error occurred.
Chris Lattner8693a512006-08-13 21:54:02 +0000512}
513
Francois Pichet02513162011-12-12 23:24:39 +0000514
515// Return true if a comma (or closing brace) is necessary after the
516// __if_exists/if_not_exists statement.
517bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
518 bool &InitExprsOk) {
519 bool trailingComma = false;
520 IfExistsCondition Result;
521 if (ParseMicrosoftIfExistsCondition(Result))
522 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000523
Francois Pichet02513162011-12-12 23:24:39 +0000524 BalancedDelimiterTracker Braces(*this, tok::l_brace);
525 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000526 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet02513162011-12-12 23:24:39 +0000527 return false;
528 }
529
530 switch (Result.Behavior) {
531 case IEB_Parse:
532 // Parse the declarations below.
533 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000534
Francois Pichet02513162011-12-12 23:24:39 +0000535 case IEB_Dependent:
536 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
537 << Result.IsIfExists;
538 // Fall through to skip.
Galina Kistanova1aead4f2017-06-01 21:23:52 +0000539 LLVM_FALLTHROUGH;
540
Francois Pichet02513162011-12-12 23:24:39 +0000541 case IEB_Skip:
542 Braces.skipToEnd();
543 return false;
544 }
545
Richard Smith34f30512013-11-23 04:06:09 +0000546 while (!isEofOrEom()) {
Francois Pichet02513162011-12-12 23:24:39 +0000547 trailingComma = false;
548 // If we know that this cannot be a designation, just parse the nested
549 // initializer directly.
550 ExprResult SubElt;
Douglas Gregora80cae12012-02-17 03:49:44 +0000551 if (MayBeDesignationStart())
Francois Pichet02513162011-12-12 23:24:39 +0000552 SubElt = ParseInitializerWithPotentialDesignator();
553 else
554 SubElt = ParseInitializer();
555
556 if (Tok.is(tok::ellipsis))
557 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
Fangrui Song6907ce22018-07-30 19:24:48 +0000558
Francois Pichet02513162011-12-12 23:24:39 +0000559 // If we couldn't parse the subelement, bail out.
560 if (!SubElt.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000561 InitExprs.push_back(SubElt.get());
Francois Pichet02513162011-12-12 23:24:39 +0000562 else
563 InitExprsOk = false;
564
565 if (Tok.is(tok::comma)) {
566 ConsumeToken();
567 trailingComma = true;
568 }
569
570 if (Tok.is(tok::r_brace))
571 break;
572 }
573
574 Braces.consumeClose();
575
576 return !trailingComma;
577}