[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / lib / Parse / ParseInit.cpp
blob900927fd97fbcfdd6b9a8a33027bcbfaf9af78b8
1 //===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements initializer parsing as specified by C99 6.7.8.
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/Designator.h"
14 #include "clang/Basic/TokenKinds.h"
15 #include "clang/Parse/ParseDiagnostic.h"
16 #include "clang/Parse/Parser.h"
17 #include "clang/Parse/RAIIObjectsForParser.h"
18 #include "clang/Sema/Ownership.h"
19 #include "clang/Sema/Scope.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallString.h"
22 using namespace clang;
25 /// MayBeDesignationStart - Return true if the current token might be the start
26 /// of a designator. If we can tell it is impossible that it is a designator,
27 /// return false.
28 bool Parser::MayBeDesignationStart() {
29 switch (Tok.getKind()) {
30 default:
31 return false;
33 case tok::period: // designator: '.' identifier
34 return true;
36 case tok::l_square: { // designator: array-designator
37 if (!PP.getLangOpts().CPlusPlus11)
38 return true;
40 // C++11 lambda expressions and C99 designators can be ambiguous all the
41 // way through the closing ']' and to the next character. Handle the easy
42 // cases here, and fall back to tentative parsing if those fail.
43 switch (PP.LookAhead(0).getKind()) {
44 case tok::equal:
45 case tok::ellipsis:
46 case tok::r_square:
47 // Definitely starts a lambda expression.
48 return false;
50 case tok::amp:
51 case tok::kw_this:
52 case tok::star:
53 case tok::identifier:
54 // We have to do additional analysis, because these could be the
55 // start of a constant expression or a lambda capture list.
56 break;
58 default:
59 // Anything not mentioned above cannot occur following a '[' in a
60 // lambda expression.
61 return true;
64 // Handle the complicated case below.
65 break;
67 case tok::identifier: // designation: identifier ':'
68 return PP.LookAhead(0).is(tok::colon);
71 // Parse up to (at most) the token after the closing ']' to determine
72 // whether this is a C99 designator or a lambda.
73 RevertingTentativeParsingAction Tentative(*this);
75 LambdaIntroducer Intro;
76 LambdaIntroducerTentativeParse ParseResult;
77 if (ParseLambdaIntroducer(Intro, &ParseResult)) {
78 // Hit and diagnosed an error in a lambda.
79 // FIXME: Tell the caller this happened so they can recover.
80 return true;
83 switch (ParseResult) {
84 case LambdaIntroducerTentativeParse::Success:
85 case LambdaIntroducerTentativeParse::Incomplete:
86 // Might be a lambda-expression. Keep looking.
87 // FIXME: If our tentative parse was not incomplete, parse the lambda from
88 // here rather than throwing away then reparsing the LambdaIntroducer.
89 break;
91 case LambdaIntroducerTentativeParse::MessageSend:
92 case LambdaIntroducerTentativeParse::Invalid:
93 // Can't be a lambda-expression. Treat it as a designator.
94 // FIXME: Should we disambiguate against a message-send?
95 return true;
98 // Once we hit the closing square bracket, we look at the next
99 // token. If it's an '=', this is a designator. Otherwise, it's a
100 // lambda expression. This decision favors lambdas over the older
101 // GNU designator syntax, which allows one to omit the '=', but is
102 // consistent with GCC.
103 return Tok.is(tok::equal);
106 static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
107 Designation &Desig) {
108 // If we have exactly one array designator, this used the GNU
109 // 'designation: array-designator' extension, otherwise there should be no
110 // designators at all!
111 if (Desig.getNumDesignators() == 1 &&
112 (Desig.getDesignator(0).isArrayDesignator() ||
113 Desig.getDesignator(0).isArrayRangeDesignator()))
114 P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
115 else if (Desig.getNumDesignators() > 0)
116 P.Diag(Loc, diag::err_expected_equal_designator);
119 /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
120 /// checking to see if the token stream starts with a designator.
122 /// C99:
124 /// designation:
125 /// designator-list '='
126 /// [GNU] array-designator
127 /// [GNU] identifier ':'
129 /// designator-list:
130 /// designator
131 /// designator-list designator
133 /// designator:
134 /// array-designator
135 /// '.' identifier
137 /// array-designator:
138 /// '[' constant-expression ']'
139 /// [GNU] '[' constant-expression '...' constant-expression ']'
141 /// C++20:
143 /// designated-initializer-list:
144 /// designated-initializer-clause
145 /// designated-initializer-list ',' designated-initializer-clause
147 /// designated-initializer-clause:
148 /// designator brace-or-equal-initializer
150 /// designator:
151 /// '.' identifier
153 /// We allow the C99 syntax extensions in C++20, but do not allow the C++20
154 /// extension (a braced-init-list after the designator with no '=') in C99.
156 /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
157 /// initializer (because it is an expression). We need to consider this case
158 /// when parsing array designators.
160 /// \p CodeCompleteCB is called with Designation parsed so far.
161 ExprResult Parser::ParseInitializerWithPotentialDesignator(
162 DesignatorCompletionInfo DesignatorCompletion) {
163 // If this is the old-style GNU extension:
164 // designation ::= identifier ':'
165 // Handle it as a field designator. Otherwise, this must be the start of a
166 // normal expression.
167 if (Tok.is(tok::identifier)) {
168 const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
170 SmallString<256> NewSyntax;
171 llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
172 << " = ";
174 SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
176 assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
177 SourceLocation ColonLoc = ConsumeToken();
179 Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
180 << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
181 NewSyntax);
183 Designation D;
184 D.AddDesignator(Designator::CreateFieldDesignator(
185 FieldName, SourceLocation(), NameLoc));
186 PreferredType.enterDesignatedInitializer(
187 Tok.getLocation(), DesignatorCompletion.PreferredBaseType, D);
188 return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
189 ParseInitializer());
192 // Desig - This is initialized when we see our first designator. We may have
193 // an objc message send with no designator, so we don't want to create this
194 // eagerly.
195 Designation Desig;
197 // Parse each designator in the designator list until we find an initializer.
198 while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
199 if (Tok.is(tok::period)) {
200 // designator: '.' identifier
201 SourceLocation DotLoc = ConsumeToken();
203 if (Tok.is(tok::code_completion)) {
204 cutOffParsing();
205 Actions.CodeCompleteDesignator(DesignatorCompletion.PreferredBaseType,
206 DesignatorCompletion.InitExprs, Desig);
207 return ExprError();
209 if (Tok.isNot(tok::identifier)) {
210 Diag(Tok.getLocation(), diag::err_expected_field_designator);
211 return ExprError();
214 Desig.AddDesignator(Designator::CreateFieldDesignator(
215 Tok.getIdentifierInfo(), DotLoc, Tok.getLocation()));
216 ConsumeToken(); // Eat the identifier.
217 continue;
220 // We must have either an array designator now or an objc message send.
221 assert(Tok.is(tok::l_square) && "Unexpected token!");
223 // Handle the two forms of array designator:
224 // array-designator: '[' constant-expression ']'
225 // array-designator: '[' constant-expression '...' constant-expression ']'
227 // Also, we have to handle the case where the expression after the
228 // designator an an objc message send: '[' objc-message-expr ']'.
229 // Interesting cases are:
230 // [foo bar] -> objc message send
231 // [foo] -> array designator
232 // [foo ... bar] -> array designator
233 // [4][foo bar] -> obsolete GNU designation with objc message send.
235 // We do not need to check for an expression starting with [[ here. If it
236 // contains an Objective-C message send, then it is not an ill-formed
237 // attribute. If it is a lambda-expression within an array-designator, then
238 // it will be rejected because a constant-expression cannot begin with a
239 // lambda-expression.
240 InMessageExpressionRAIIObject InMessage(*this, true);
242 BalancedDelimiterTracker T(*this, tok::l_square);
243 T.consumeOpen();
244 SourceLocation StartLoc = T.getOpenLocation();
246 ExprResult Idx;
248 // If Objective-C is enabled and this is a typename (class message
249 // send) or send to 'super', parse this as a message send
250 // expression. We handle C++ and C separately, since C++ requires
251 // much more complicated parsing.
252 if (getLangOpts().ObjC && getLangOpts().CPlusPlus) {
253 // Send to 'super'.
254 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
255 NextToken().isNot(tok::period) &&
256 getCurScope()->isInObjcMethodScope()) {
257 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
258 return ParseAssignmentExprWithObjCMessageExprStart(
259 StartLoc, ConsumeToken(), nullptr, nullptr);
262 // Parse the receiver, which is either a type or an expression.
263 bool IsExpr;
264 void *TypeOrExpr;
265 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
266 SkipUntil(tok::r_square, StopAtSemi);
267 return ExprError();
270 // If the receiver was a type, we have a class message; parse
271 // the rest of it.
272 if (!IsExpr) {
273 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
274 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
275 SourceLocation(),
276 ParsedType::getFromOpaquePtr(TypeOrExpr),
277 nullptr);
280 // If the receiver was an expression, we still don't know
281 // whether we have a message send or an array designator; just
282 // adopt the expression for further analysis below.
283 // FIXME: potentially-potentially evaluated expression above?
284 Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
285 } else if (getLangOpts().ObjC && Tok.is(tok::identifier)) {
286 IdentifierInfo *II = Tok.getIdentifierInfo();
287 SourceLocation IILoc = Tok.getLocation();
288 ParsedType ReceiverType;
289 // Three cases. This is a message send to a type: [type foo]
290 // This is a message send to super: [super foo]
291 // This is a message sent to an expr: [super.bar foo]
292 switch (Actions.getObjCMessageKind(
293 getCurScope(), II, IILoc, II == Ident_super,
294 NextToken().is(tok::period), ReceiverType)) {
295 case Sema::ObjCSuperMessage:
296 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
297 return ParseAssignmentExprWithObjCMessageExprStart(
298 StartLoc, ConsumeToken(), nullptr, nullptr);
300 case Sema::ObjCClassMessage:
301 CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
302 ConsumeToken(); // the identifier
303 if (!ReceiverType) {
304 SkipUntil(tok::r_square, StopAtSemi);
305 return ExprError();
308 // Parse type arguments and protocol qualifiers.
309 if (Tok.is(tok::less)) {
310 SourceLocation NewEndLoc;
311 TypeResult NewReceiverType
312 = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType,
313 /*consumeLastToken=*/true,
314 NewEndLoc);
315 if (!NewReceiverType.isUsable()) {
316 SkipUntil(tok::r_square, StopAtSemi);
317 return ExprError();
320 ReceiverType = NewReceiverType.get();
323 return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
324 SourceLocation(),
325 ReceiverType,
326 nullptr);
328 case Sema::ObjCInstanceMessage:
329 // Fall through; we'll just parse the expression and
330 // (possibly) treat this like an Objective-C message send
331 // later.
332 break;
336 // Parse the index expression, if we haven't already gotten one
337 // above (which can only happen in Objective-C++).
338 // Note that we parse this as an assignment expression, not a constant
339 // expression (allowing *=, =, etc) to handle the objc case. Sema needs
340 // to validate that the expression is a constant.
341 // FIXME: We also need to tell Sema that we're in a
342 // potentially-potentially evaluated context.
343 if (!Idx.get()) {
344 Idx = ParseAssignmentExpression();
345 if (Idx.isInvalid()) {
346 SkipUntil(tok::r_square, StopAtSemi);
347 return Idx;
351 // Given an expression, we could either have a designator (if the next
352 // tokens are '...' or ']' or an objc message send. If this is an objc
353 // message send, handle it now. An objc-message send is the start of
354 // an assignment-expression production.
355 if (getLangOpts().ObjC && Tok.isNot(tok::ellipsis) &&
356 Tok.isNot(tok::r_square)) {
357 CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
358 return ParseAssignmentExprWithObjCMessageExprStart(
359 StartLoc, SourceLocation(), nullptr, Idx.get());
362 // If this is a normal array designator, remember it.
363 if (Tok.isNot(tok::ellipsis)) {
364 Desig.AddDesignator(
365 Designator::CreateArrayDesignator(Idx.get(), StartLoc));
366 } else {
367 // Handle the gnu array range extension.
368 Diag(Tok, diag::ext_gnu_array_range);
369 SourceLocation EllipsisLoc = ConsumeToken();
371 ExprResult RHS(ParseConstantExpression());
372 if (RHS.isInvalid()) {
373 SkipUntil(tok::r_square, StopAtSemi);
374 return RHS;
376 Desig.AddDesignator(Designator::CreateArrayRangeDesignator(
377 Idx.get(), RHS.get(), StartLoc, EllipsisLoc));
380 T.consumeClose();
381 Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
382 T.getCloseLocation());
385 // Okay, we're done with the designator sequence. We know that there must be
386 // at least one designator, because the only case we can get into this method
387 // without a designator is when we have an objc message send. That case is
388 // handled and returned from above.
389 assert(!Desig.empty() && "Designator is empty?");
391 // Handle a normal designator sequence end, which is an equal.
392 if (Tok.is(tok::equal)) {
393 SourceLocation EqualLoc = ConsumeToken();
394 PreferredType.enterDesignatedInitializer(
395 Tok.getLocation(), DesignatorCompletion.PreferredBaseType, Desig);
396 return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
397 ParseInitializer());
400 // Handle a C++20 braced designated initialization, which results in
401 // direct-list-initialization of the aggregate element. We allow this as an
402 // extension from C++11 onwards (when direct-list-initialization was added).
403 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
404 PreferredType.enterDesignatedInitializer(
405 Tok.getLocation(), DesignatorCompletion.PreferredBaseType, Desig);
406 return Actions.ActOnDesignatedInitializer(Desig, SourceLocation(), false,
407 ParseBraceInitializer());
410 // We read some number of designators and found something that isn't an = or
411 // an initializer. If we have exactly one array designator, this
412 // is the GNU 'designation: array-designator' extension. Otherwise, it is a
413 // parse error.
414 if (Desig.getNumDesignators() == 1 &&
415 (Desig.getDesignator(0).isArrayDesignator() ||
416 Desig.getDesignator(0).isArrayRangeDesignator())) {
417 Diag(Tok, diag::ext_gnu_missing_equal_designator)
418 << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
419 return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
420 true, ParseInitializer());
423 Diag(Tok, diag::err_expected_equal_designator);
424 return ExprError();
427 /// ParseBraceInitializer - Called when parsing an initializer that has a
428 /// leading open brace.
430 /// initializer: [C99 6.7.8]
431 /// '{' initializer-list '}'
432 /// '{' initializer-list ',' '}'
433 /// [GNU] '{' '}'
435 /// initializer-list:
436 /// designation[opt] initializer ...[opt]
437 /// initializer-list ',' designation[opt] initializer ...[opt]
439 ExprResult Parser::ParseBraceInitializer() {
440 InMessageExpressionRAIIObject InMessage(*this, false);
442 BalancedDelimiterTracker T(*this, tok::l_brace);
443 T.consumeOpen();
444 SourceLocation LBraceLoc = T.getOpenLocation();
446 /// InitExprs - This is the actual list of expressions contained in the
447 /// initializer.
448 ExprVector InitExprs;
450 if (Tok.is(tok::r_brace)) {
451 // Empty initializers are a C++ feature and a GNU extension to C.
452 if (!getLangOpts().CPlusPlus)
453 Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
454 // Match the '}'.
455 return Actions.ActOnInitList(LBraceLoc, std::nullopt, ConsumeBrace());
458 // Enter an appropriate expression evaluation context for an initializer list.
459 EnterExpressionEvaluationContext EnterContext(
460 Actions, EnterExpressionEvaluationContext::InitList);
462 bool InitExprsOk = true;
463 QualType LikelyType = PreferredType.get(T.getOpenLocation());
464 DesignatorCompletionInfo DesignatorCompletion{InitExprs, LikelyType};
465 bool CalledSignatureHelp = false;
466 auto RunSignatureHelp = [&] {
467 QualType PreferredType;
468 if (!LikelyType.isNull())
469 PreferredType = Actions.ProduceConstructorSignatureHelp(
470 LikelyType->getCanonicalTypeInternal(), T.getOpenLocation(),
471 InitExprs, T.getOpenLocation(), /*Braced=*/true);
472 CalledSignatureHelp = true;
473 return PreferredType;
476 while (true) {
477 PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp);
479 // Handle Microsoft __if_exists/if_not_exists if necessary.
480 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
481 Tok.is(tok::kw___if_not_exists))) {
482 if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
483 if (Tok.isNot(tok::comma)) break;
484 ConsumeToken();
486 if (Tok.is(tok::r_brace)) break;
487 continue;
490 // Parse: designation[opt] initializer
492 // If we know that this cannot be a designation, just parse the nested
493 // initializer directly.
494 ExprResult SubElt;
495 if (MayBeDesignationStart())
496 SubElt = ParseInitializerWithPotentialDesignator(DesignatorCompletion);
497 else
498 SubElt = ParseInitializer();
500 if (Tok.is(tok::ellipsis))
501 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
503 SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get());
505 // If we couldn't parse the subelement, bail out.
506 if (SubElt.isUsable()) {
507 InitExprs.push_back(SubElt.get());
508 } else {
509 InitExprsOk = false;
511 // We have two ways to try to recover from this error: if the code looks
512 // grammatically ok (i.e. we have a comma coming up) try to continue
513 // parsing the rest of the initializer. This allows us to emit
514 // diagnostics for later elements that we find. If we don't see a comma,
515 // assume there is a parse error, and just skip to recover.
516 // FIXME: This comment doesn't sound right. If there is a r_brace
517 // immediately, it can't be an error, since there is no other way of
518 // leaving this loop except through this if.
519 if (Tok.isNot(tok::comma)) {
520 SkipUntil(tok::r_brace, StopBeforeMatch);
521 break;
525 // If we don't have a comma continued list, we're done.
526 if (Tok.isNot(tok::comma)) break;
528 // TODO: save comma locations if some client cares.
529 ConsumeToken();
531 // Handle trailing comma.
532 if (Tok.is(tok::r_brace)) break;
535 bool closed = !T.consumeClose();
537 if (InitExprsOk && closed)
538 return Actions.ActOnInitList(LBraceLoc, InitExprs,
539 T.getCloseLocation());
541 return ExprError(); // an error occurred.
545 // Return true if a comma (or closing brace) is necessary after the
546 // __if_exists/if_not_exists statement.
547 bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
548 bool &InitExprsOk) {
549 bool trailingComma = false;
550 IfExistsCondition Result;
551 if (ParseMicrosoftIfExistsCondition(Result))
552 return false;
554 BalancedDelimiterTracker Braces(*this, tok::l_brace);
555 if (Braces.consumeOpen()) {
556 Diag(Tok, diag::err_expected) << tok::l_brace;
557 return false;
560 switch (Result.Behavior) {
561 case IEB_Parse:
562 // Parse the declarations below.
563 break;
565 case IEB_Dependent:
566 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
567 << Result.IsIfExists;
568 // Fall through to skip.
569 [[fallthrough]];
571 case IEB_Skip:
572 Braces.skipToEnd();
573 return false;
576 DesignatorCompletionInfo DesignatorCompletion{
577 InitExprs,
578 PreferredType.get(Braces.getOpenLocation()),
580 while (!isEofOrEom()) {
581 trailingComma = false;
582 // If we know that this cannot be a designation, just parse the nested
583 // initializer directly.
584 ExprResult SubElt;
585 if (MayBeDesignationStart())
586 SubElt = ParseInitializerWithPotentialDesignator(DesignatorCompletion);
587 else
588 SubElt = ParseInitializer();
590 if (Tok.is(tok::ellipsis))
591 SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
593 // If we couldn't parse the subelement, bail out.
594 if (!SubElt.isInvalid())
595 InitExprs.push_back(SubElt.get());
596 else
597 InitExprsOk = false;
599 if (Tok.is(tok::comma)) {
600 ConsumeToken();
601 trailingComma = true;
604 if (Tok.is(tok::r_brace))
605 break;
608 Braces.consumeClose();
610 return !trailingComma;