ParseInit.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Parse/ParseDiagnostic.h"
  13. #include "clang/Parse/Parser.h"
  14. #include "clang/Parse/RAIIObjectsForParser.h"
  15. #include "clang/Sema/Designator.h"
  16. #include "clang/Sema/Scope.h"
  17. #include "llvm/ADT/SmallString.h"
  18. using namespace clang;
  19. /// MayBeDesignationStart - Return true if the current token might be the start
  20. /// of a designator. If we can tell it is impossible that it is a designator,
  21. /// return false.
  22. bool Parser::MayBeDesignationStart() {
  23. switch (Tok.getKind()) {
  24. default:
  25. return false;
  26. case tok::period: // designator: '.' identifier
  27. return true;
  28. case tok::l_square: { // designator: array-designator
  29. if (!PP.getLangOpts().CPlusPlus11)
  30. return true;
  31. // C++11 lambda expressions and C99 designators can be ambiguous all the
  32. // way through the closing ']' and to the next character. Handle the easy
  33. // cases here, and fall back to tentative parsing if those fail.
  34. switch (PP.LookAhead(0).getKind()) {
  35. case tok::equal:
  36. case tok::ellipsis:
  37. case tok::r_square:
  38. // Definitely starts a lambda expression.
  39. return false;
  40. case tok::amp:
  41. case tok::kw_this:
  42. case tok::star:
  43. case tok::identifier:
  44. // We have to do additional analysis, because these could be the
  45. // start of a constant expression or a lambda capture list.
  46. break;
  47. default:
  48. // Anything not mentioned above cannot occur following a '[' in a
  49. // lambda expression.
  50. return true;
  51. }
  52. // Handle the complicated case below.
  53. break;
  54. }
  55. case tok::identifier: // designation: identifier ':'
  56. return PP.LookAhead(0).is(tok::colon);
  57. }
  58. // Parse up to (at most) the token after the closing ']' to determine
  59. // whether this is a C99 designator or a lambda.
  60. RevertingTentativeParsingAction Tentative(*this);
  61. LambdaIntroducer Intro;
  62. LambdaIntroducerTentativeParse ParseResult;
  63. if (ParseLambdaIntroducer(Intro, &ParseResult)) {
  64. // Hit and diagnosed an error in a lambda.
  65. // FIXME: Tell the caller this happened so they can recover.
  66. return true;
  67. }
  68. switch (ParseResult) {
  69. case LambdaIntroducerTentativeParse::Success:
  70. case LambdaIntroducerTentativeParse::Incomplete:
  71. // Might be a lambda-expression. Keep looking.
  72. // FIXME: If our tentative parse was not incomplete, parse the lambda from
  73. // here rather than throwing away then reparsing the LambdaIntroducer.
  74. break;
  75. case LambdaIntroducerTentativeParse::MessageSend:
  76. case LambdaIntroducerTentativeParse::Invalid:
  77. // Can't be a lambda-expression. Treat it as a designator.
  78. // FIXME: Should we disambiguate against a message-send?
  79. return true;
  80. }
  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. return Tok.is(tok::equal);
  87. }
  88. static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
  89. Designation &Desig) {
  90. // If we have exactly one array designator, this used the GNU
  91. // 'designation: array-designator' extension, otherwise there should be no
  92. // designators at all!
  93. if (Desig.getNumDesignators() == 1 &&
  94. (Desig.getDesignator(0).isArrayDesignator() ||
  95. Desig.getDesignator(0).isArrayRangeDesignator()))
  96. P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
  97. else if (Desig.getNumDesignators() > 0)
  98. P.Diag(Loc, diag::err_expected_equal_designator);
  99. }
  100. /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
  101. /// checking to see if the token stream starts with a designator.
  102. ///
  103. /// C99:
  104. ///
  105. /// designation:
  106. /// designator-list '='
  107. /// [GNU] array-designator
  108. /// [GNU] identifier ':'
  109. ///
  110. /// designator-list:
  111. /// designator
  112. /// designator-list designator
  113. ///
  114. /// designator:
  115. /// array-designator
  116. /// '.' identifier
  117. ///
  118. /// array-designator:
  119. /// '[' constant-expression ']'
  120. /// [GNU] '[' constant-expression '...' constant-expression ']'
  121. ///
  122. /// C++20:
  123. ///
  124. /// designated-initializer-list:
  125. /// designated-initializer-clause
  126. /// designated-initializer-list ',' designated-initializer-clause
  127. ///
  128. /// designated-initializer-clause:
  129. /// designator brace-or-equal-initializer
  130. ///
  131. /// designator:
  132. /// '.' identifier
  133. ///
  134. /// We allow the C99 syntax extensions in C++20, but do not allow the C++20
  135. /// extension (a braced-init-list after the designator with no '=') in C99.
  136. ///
  137. /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
  138. /// initializer (because it is an expression). We need to consider this case
  139. /// when parsing array designators.
  140. ///
  141. ExprResult Parser::ParseInitializerWithPotentialDesignator() {
  142. // If this is the old-style GNU extension:
  143. // designation ::= identifier ':'
  144. // Handle it as a field designator. Otherwise, this must be the start of a
  145. // normal expression.
  146. if (Tok.is(tok::identifier)) {
  147. const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
  148. SmallString<256> NewSyntax;
  149. llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
  150. << " = ";
  151. SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
  152. assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
  153. SourceLocation ColonLoc = ConsumeToken();
  154. Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
  155. << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
  156. NewSyntax);
  157. Designation D;
  158. D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
  159. return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
  160. ParseInitializer());
  161. }
  162. // Desig - This is initialized when we see our first designator. We may have
  163. // an objc message send with no designator, so we don't want to create this
  164. // eagerly.
  165. Designation Desig;
  166. // Parse each designator in the designator list until we find an initializer.
  167. while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
  168. if (Tok.is(tok::period)) {
  169. // designator: '.' identifier
  170. SourceLocation DotLoc = ConsumeToken();
  171. if (Tok.isNot(tok::identifier)) {
  172. Diag(Tok.getLocation(), diag::err_expected_field_designator);
  173. return ExprError();
  174. }
  175. Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
  176. Tok.getLocation()));
  177. ConsumeToken(); // Eat the identifier.
  178. continue;
  179. }
  180. // We must have either an array designator now or an objc message send.
  181. assert(Tok.is(tok::l_square) && "Unexpected token!");
  182. // Handle the two forms of array designator:
  183. // array-designator: '[' constant-expression ']'
  184. // array-designator: '[' constant-expression '...' constant-expression ']'
  185. //
  186. // Also, we have to handle the case where the expression after the
  187. // designator an an objc message send: '[' objc-message-expr ']'.
  188. // Interesting cases are:
  189. // [foo bar] -> objc message send
  190. // [foo] -> array designator
  191. // [foo ... bar] -> array designator
  192. // [4][foo bar] -> obsolete GNU designation with objc message send.
  193. //
  194. // We do not need to check for an expression starting with [[ here. If it
  195. // contains an Objective-C message send, then it is not an ill-formed
  196. // attribute. If it is a lambda-expression within an array-designator, then
  197. // it will be rejected because a constant-expression cannot begin with a
  198. // lambda-expression.
  199. InMessageExpressionRAIIObject InMessage(*this, true);
  200. BalancedDelimiterTracker T(*this, tok::l_square);
  201. T.consumeOpen();
  202. SourceLocation StartLoc = T.getOpenLocation();
  203. ExprResult Idx;
  204. // If Objective-C is enabled and this is a typename (class message
  205. // send) or send to 'super', parse this as a message send
  206. // expression. We handle C++ and C separately, since C++ requires
  207. // much more complicated parsing.
  208. if (getLangOpts().ObjC && getLangOpts().CPlusPlus) {
  209. // Send to 'super'.
  210. if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
  211. NextToken().isNot(tok::period) &&
  212. getCurScope()->isInObjcMethodScope()) {
  213. CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
  214. return ParseAssignmentExprWithObjCMessageExprStart(
  215. StartLoc, ConsumeToken(), nullptr, nullptr);
  216. }
  217. // Parse the receiver, which is either a type or an expression.
  218. bool IsExpr;
  219. void *TypeOrExpr;
  220. if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
  221. SkipUntil(tok::r_square, StopAtSemi);
  222. return ExprError();
  223. }
  224. // If the receiver was a type, we have a class message; parse
  225. // the rest of it.
  226. if (!IsExpr) {
  227. CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
  228. return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
  229. SourceLocation(),
  230. ParsedType::getFromOpaquePtr(TypeOrExpr),
  231. nullptr);
  232. }
  233. // If the receiver was an expression, we still don't know
  234. // whether we have a message send or an array designator; just
  235. // adopt the expression for further analysis below.
  236. // FIXME: potentially-potentially evaluated expression above?
  237. Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
  238. } else if (getLangOpts().ObjC && Tok.is(tok::identifier)) {
  239. IdentifierInfo *II = Tok.getIdentifierInfo();
  240. SourceLocation IILoc = Tok.getLocation();
  241. ParsedType ReceiverType;
  242. // Three cases. This is a message send to a type: [type foo]
  243. // This is a message send to super: [super foo]
  244. // This is a message sent to an expr: [super.bar foo]
  245. switch (Actions.getObjCMessageKind(
  246. getCurScope(), II, IILoc, II == Ident_super,
  247. NextToken().is(tok::period), ReceiverType)) {
  248. case Sema::ObjCSuperMessage:
  249. CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
  250. return ParseAssignmentExprWithObjCMessageExprStart(
  251. StartLoc, ConsumeToken(), nullptr, nullptr);
  252. case Sema::ObjCClassMessage:
  253. CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
  254. ConsumeToken(); // the identifier
  255. if (!ReceiverType) {
  256. SkipUntil(tok::r_square, StopAtSemi);
  257. return ExprError();
  258. }
  259. // Parse type arguments and protocol qualifiers.
  260. if (Tok.is(tok::less)) {
  261. SourceLocation NewEndLoc;
  262. TypeResult NewReceiverType
  263. = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType,
  264. /*consumeLastToken=*/true,
  265. NewEndLoc);
  266. if (!NewReceiverType.isUsable()) {
  267. SkipUntil(tok::r_square, StopAtSemi);
  268. return ExprError();
  269. }
  270. ReceiverType = NewReceiverType.get();
  271. }
  272. return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
  273. SourceLocation(),
  274. ReceiverType,
  275. nullptr);
  276. case Sema::ObjCInstanceMessage:
  277. // Fall through; we'll just parse the expression and
  278. // (possibly) treat this like an Objective-C message send
  279. // later.
  280. break;
  281. }
  282. }
  283. // Parse the index expression, if we haven't already gotten one
  284. // above (which can only happen in Objective-C++).
  285. // Note that we parse this as an assignment expression, not a constant
  286. // expression (allowing *=, =, etc) to handle the objc case. Sema needs
  287. // to validate that the expression is a constant.
  288. // FIXME: We also need to tell Sema that we're in a
  289. // potentially-potentially evaluated context.
  290. if (!Idx.get()) {
  291. Idx = ParseAssignmentExpression();
  292. if (Idx.isInvalid()) {
  293. SkipUntil(tok::r_square, StopAtSemi);
  294. return Idx;
  295. }
  296. }
  297. // Given an expression, we could either have a designator (if the next
  298. // tokens are '...' or ']' or an objc message send. If this is an objc
  299. // message send, handle it now. An objc-message send is the start of
  300. // an assignment-expression production.
  301. if (getLangOpts().ObjC && Tok.isNot(tok::ellipsis) &&
  302. Tok.isNot(tok::r_square)) {
  303. CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
  304. return ParseAssignmentExprWithObjCMessageExprStart(
  305. StartLoc, SourceLocation(), nullptr, Idx.get());
  306. }
  307. // If this is a normal array designator, remember it.
  308. if (Tok.isNot(tok::ellipsis)) {
  309. Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc));
  310. } else {
  311. // Handle the gnu array range extension.
  312. Diag(Tok, diag::ext_gnu_array_range);
  313. SourceLocation EllipsisLoc = ConsumeToken();
  314. ExprResult RHS(ParseConstantExpression());
  315. if (RHS.isInvalid()) {
  316. SkipUntil(tok::r_square, StopAtSemi);
  317. return RHS;
  318. }
  319. Desig.AddDesignator(Designator::getArrayRange(Idx.get(),
  320. RHS.get(),
  321. StartLoc, EllipsisLoc));
  322. }
  323. T.consumeClose();
  324. Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
  325. T.getCloseLocation());
  326. }
  327. // Okay, we're done with the designator sequence. We know that there must be
  328. // at least one designator, because the only case we can get into this method
  329. // without a designator is when we have an objc message send. That case is
  330. // handled and returned from above.
  331. assert(!Desig.empty() && "Designator is empty?");
  332. // Handle a normal designator sequence end, which is an equal.
  333. if (Tok.is(tok::equal)) {
  334. SourceLocation EqualLoc = ConsumeToken();
  335. return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
  336. ParseInitializer());
  337. }
  338. // Handle a C++20 braced designated initialization, which results in
  339. // direct-list-initialization of the aggregate element. We allow this as an
  340. // extension from C++11 onwards (when direct-list-initialization was added).
  341. if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
  342. return Actions.ActOnDesignatedInitializer(Desig, SourceLocation(), false,
  343. ParseBraceInitializer());
  344. }
  345. // We read some number of designators and found something that isn't an = or
  346. // an initializer. If we have exactly one array designator, this
  347. // is the GNU 'designation: array-designator' extension. Otherwise, it is a
  348. // parse error.
  349. if (Desig.getNumDesignators() == 1 &&
  350. (Desig.getDesignator(0).isArrayDesignator() ||
  351. Desig.getDesignator(0).isArrayRangeDesignator())) {
  352. Diag(Tok, diag::ext_gnu_missing_equal_designator)
  353. << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
  354. return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
  355. true, ParseInitializer());
  356. }
  357. Diag(Tok, diag::err_expected_equal_designator);
  358. return ExprError();
  359. }
  360. /// ParseBraceInitializer - Called when parsing an initializer that has a
  361. /// leading open brace.
  362. ///
  363. /// initializer: [C99 6.7.8]
  364. /// '{' initializer-list '}'
  365. /// '{' initializer-list ',' '}'
  366. /// [GNU] '{' '}'
  367. ///
  368. /// initializer-list:
  369. /// designation[opt] initializer ...[opt]
  370. /// initializer-list ',' designation[opt] initializer ...[opt]
  371. ///
  372. ExprResult Parser::ParseBraceInitializer() {
  373. InMessageExpressionRAIIObject InMessage(*this, false);
  374. BalancedDelimiterTracker T(*this, tok::l_brace);
  375. T.consumeOpen();
  376. SourceLocation LBraceLoc = T.getOpenLocation();
  377. /// InitExprs - This is the actual list of expressions contained in the
  378. /// initializer.
  379. ExprVector InitExprs;
  380. if (Tok.is(tok::r_brace)) {
  381. // Empty initializers are a C++ feature and a GNU extension to C.
  382. if (!getLangOpts().CPlusPlus)
  383. Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
  384. // Match the '}'.
  385. return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace());
  386. }
  387. // Enter an appropriate expression evaluation context for an initializer list.
  388. EnterExpressionEvaluationContext EnterContext(
  389. Actions, EnterExpressionEvaluationContext::InitList);
  390. bool InitExprsOk = true;
  391. while (1) {
  392. // Handle Microsoft __if_exists/if_not_exists if necessary.
  393. if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
  394. Tok.is(tok::kw___if_not_exists))) {
  395. if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
  396. if (Tok.isNot(tok::comma)) break;
  397. ConsumeToken();
  398. }
  399. if (Tok.is(tok::r_brace)) break;
  400. continue;
  401. }
  402. // Parse: designation[opt] initializer
  403. // If we know that this cannot be a designation, just parse the nested
  404. // initializer directly.
  405. ExprResult SubElt;
  406. if (MayBeDesignationStart())
  407. SubElt = ParseInitializerWithPotentialDesignator();
  408. else
  409. SubElt = ParseInitializer();
  410. if (Tok.is(tok::ellipsis))
  411. SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
  412. SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get());
  413. // If we couldn't parse the subelement, bail out.
  414. if (SubElt.isUsable()) {
  415. InitExprs.push_back(SubElt.get());
  416. } else {
  417. InitExprsOk = false;
  418. // We have two ways to try to recover from this error: if the code looks
  419. // grammatically ok (i.e. we have a comma coming up) try to continue
  420. // parsing the rest of the initializer. This allows us to emit
  421. // diagnostics for later elements that we find. If we don't see a comma,
  422. // assume there is a parse error, and just skip to recover.
  423. // FIXME: This comment doesn't sound right. If there is a r_brace
  424. // immediately, it can't be an error, since there is no other way of
  425. // leaving this loop except through this if.
  426. if (Tok.isNot(tok::comma)) {
  427. SkipUntil(tok::r_brace, StopBeforeMatch);
  428. break;
  429. }
  430. }
  431. // If we don't have a comma continued list, we're done.
  432. if (Tok.isNot(tok::comma)) break;
  433. // TODO: save comma locations if some client cares.
  434. ConsumeToken();
  435. // Handle trailing comma.
  436. if (Tok.is(tok::r_brace)) break;
  437. }
  438. bool closed = !T.consumeClose();
  439. if (InitExprsOk && closed)
  440. return Actions.ActOnInitList(LBraceLoc, InitExprs,
  441. T.getCloseLocation());
  442. return ExprError(); // an error occurred.
  443. }
  444. // Return true if a comma (or closing brace) is necessary after the
  445. // __if_exists/if_not_exists statement.
  446. bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
  447. bool &InitExprsOk) {
  448. bool trailingComma = false;
  449. IfExistsCondition Result;
  450. if (ParseMicrosoftIfExistsCondition(Result))
  451. return false;
  452. BalancedDelimiterTracker Braces(*this, tok::l_brace);
  453. if (Braces.consumeOpen()) {
  454. Diag(Tok, diag::err_expected) << tok::l_brace;
  455. return false;
  456. }
  457. switch (Result.Behavior) {
  458. case IEB_Parse:
  459. // Parse the declarations below.
  460. break;
  461. case IEB_Dependent:
  462. Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
  463. << Result.IsIfExists;
  464. // Fall through to skip.
  465. LLVM_FALLTHROUGH;
  466. case IEB_Skip:
  467. Braces.skipToEnd();
  468. return false;
  469. }
  470. while (!isEofOrEom()) {
  471. trailingComma = false;
  472. // If we know that this cannot be a designation, just parse the nested
  473. // initializer directly.
  474. ExprResult SubElt;
  475. if (MayBeDesignationStart())
  476. SubElt = ParseInitializerWithPotentialDesignator();
  477. else
  478. SubElt = ParseInitializer();
  479. if (Tok.is(tok::ellipsis))
  480. SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
  481. // If we couldn't parse the subelement, bail out.
  482. if (!SubElt.isInvalid())
  483. InitExprs.push_back(SubElt.get());
  484. else
  485. InitExprsOk = false;
  486. if (Tok.is(tok::comma)) {
  487. ConsumeToken();
  488. trailingComma = true;
  489. }
  490. if (Tok.is(tok::r_brace))
  491. break;
  492. }
  493. Braces.consumeClose();
  494. return !trailingComma;
  495. }