ParseInit.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. /// designation:
  104. /// designator-list '='
  105. /// [GNU] array-designator
  106. /// [GNU] identifier ':'
  107. ///
  108. /// designator-list:
  109. /// designator
  110. /// designator-list designator
  111. ///
  112. /// designator:
  113. /// array-designator
  114. /// '.' identifier
  115. ///
  116. /// array-designator:
  117. /// '[' constant-expression ']'
  118. /// [GNU] '[' constant-expression '...' constant-expression ']'
  119. ///
  120. /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
  121. /// initializer (because it is an expression). We need to consider this case
  122. /// when parsing array designators.
  123. ///
  124. ExprResult Parser::ParseInitializerWithPotentialDesignator() {
  125. // If this is the old-style GNU extension:
  126. // designation ::= identifier ':'
  127. // Handle it as a field designator. Otherwise, this must be the start of a
  128. // normal expression.
  129. if (Tok.is(tok::identifier)) {
  130. const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
  131. SmallString<256> NewSyntax;
  132. llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
  133. << " = ";
  134. SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
  135. assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
  136. SourceLocation ColonLoc = ConsumeToken();
  137. Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
  138. << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
  139. NewSyntax);
  140. Designation D;
  141. D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
  142. return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
  143. ParseInitializer());
  144. }
  145. // Desig - This is initialized when we see our first designator. We may have
  146. // an objc message send with no designator, so we don't want to create this
  147. // eagerly.
  148. Designation Desig;
  149. // Parse each designator in the designator list until we find an initializer.
  150. while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
  151. if (Tok.is(tok::period)) {
  152. // designator: '.' identifier
  153. SourceLocation DotLoc = ConsumeToken();
  154. if (Tok.isNot(tok::identifier)) {
  155. Diag(Tok.getLocation(), diag::err_expected_field_designator);
  156. return ExprError();
  157. }
  158. Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
  159. Tok.getLocation()));
  160. ConsumeToken(); // Eat the identifier.
  161. continue;
  162. }
  163. // We must have either an array designator now or an objc message send.
  164. assert(Tok.is(tok::l_square) && "Unexpected token!");
  165. // Handle the two forms of array designator:
  166. // array-designator: '[' constant-expression ']'
  167. // array-designator: '[' constant-expression '...' constant-expression ']'
  168. //
  169. // Also, we have to handle the case where the expression after the
  170. // designator an an objc message send: '[' objc-message-expr ']'.
  171. // Interesting cases are:
  172. // [foo bar] -> objc message send
  173. // [foo] -> array designator
  174. // [foo ... bar] -> array designator
  175. // [4][foo bar] -> obsolete GNU designation with objc message send.
  176. //
  177. // We do not need to check for an expression starting with [[ here. If it
  178. // contains an Objective-C message send, then it is not an ill-formed
  179. // attribute. If it is a lambda-expression within an array-designator, then
  180. // it will be rejected because a constant-expression cannot begin with a
  181. // lambda-expression.
  182. InMessageExpressionRAIIObject InMessage(*this, true);
  183. BalancedDelimiterTracker T(*this, tok::l_square);
  184. T.consumeOpen();
  185. SourceLocation StartLoc = T.getOpenLocation();
  186. ExprResult Idx;
  187. // If Objective-C is enabled and this is a typename (class message
  188. // send) or send to 'super', parse this as a message send
  189. // expression. We handle C++ and C separately, since C++ requires
  190. // much more complicated parsing.
  191. if (getLangOpts().ObjC && getLangOpts().CPlusPlus) {
  192. // Send to 'super'.
  193. if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
  194. NextToken().isNot(tok::period) &&
  195. getCurScope()->isInObjcMethodScope()) {
  196. CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
  197. return ParseAssignmentExprWithObjCMessageExprStart(
  198. StartLoc, ConsumeToken(), nullptr, nullptr);
  199. }
  200. // Parse the receiver, which is either a type or an expression.
  201. bool IsExpr;
  202. void *TypeOrExpr;
  203. if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
  204. SkipUntil(tok::r_square, StopAtSemi);
  205. return ExprError();
  206. }
  207. // If the receiver was a type, we have a class message; parse
  208. // the rest of it.
  209. if (!IsExpr) {
  210. CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
  211. return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
  212. SourceLocation(),
  213. ParsedType::getFromOpaquePtr(TypeOrExpr),
  214. nullptr);
  215. }
  216. // If the receiver was an expression, we still don't know
  217. // whether we have a message send or an array designator; just
  218. // adopt the expression for further analysis below.
  219. // FIXME: potentially-potentially evaluated expression above?
  220. Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
  221. } else if (getLangOpts().ObjC && Tok.is(tok::identifier)) {
  222. IdentifierInfo *II = Tok.getIdentifierInfo();
  223. SourceLocation IILoc = Tok.getLocation();
  224. ParsedType ReceiverType;
  225. // Three cases. This is a message send to a type: [type foo]
  226. // This is a message send to super: [super foo]
  227. // This is a message sent to an expr: [super.bar foo]
  228. switch (Actions.getObjCMessageKind(
  229. getCurScope(), II, IILoc, II == Ident_super,
  230. NextToken().is(tok::period), ReceiverType)) {
  231. case Sema::ObjCSuperMessage:
  232. CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
  233. return ParseAssignmentExprWithObjCMessageExprStart(
  234. StartLoc, ConsumeToken(), nullptr, nullptr);
  235. case Sema::ObjCClassMessage:
  236. CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
  237. ConsumeToken(); // the identifier
  238. if (!ReceiverType) {
  239. SkipUntil(tok::r_square, StopAtSemi);
  240. return ExprError();
  241. }
  242. // Parse type arguments and protocol qualifiers.
  243. if (Tok.is(tok::less)) {
  244. SourceLocation NewEndLoc;
  245. TypeResult NewReceiverType
  246. = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType,
  247. /*consumeLastToken=*/true,
  248. NewEndLoc);
  249. if (!NewReceiverType.isUsable()) {
  250. SkipUntil(tok::r_square, StopAtSemi);
  251. return ExprError();
  252. }
  253. ReceiverType = NewReceiverType.get();
  254. }
  255. return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
  256. SourceLocation(),
  257. ReceiverType,
  258. nullptr);
  259. case Sema::ObjCInstanceMessage:
  260. // Fall through; we'll just parse the expression and
  261. // (possibly) treat this like an Objective-C message send
  262. // later.
  263. break;
  264. }
  265. }
  266. // Parse the index expression, if we haven't already gotten one
  267. // above (which can only happen in Objective-C++).
  268. // Note that we parse this as an assignment expression, not a constant
  269. // expression (allowing *=, =, etc) to handle the objc case. Sema needs
  270. // to validate that the expression is a constant.
  271. // FIXME: We also need to tell Sema that we're in a
  272. // potentially-potentially evaluated context.
  273. if (!Idx.get()) {
  274. Idx = ParseAssignmentExpression();
  275. if (Idx.isInvalid()) {
  276. SkipUntil(tok::r_square, StopAtSemi);
  277. return Idx;
  278. }
  279. }
  280. // Given an expression, we could either have a designator (if the next
  281. // tokens are '...' or ']' or an objc message send. If this is an objc
  282. // message send, handle it now. An objc-message send is the start of
  283. // an assignment-expression production.
  284. if (getLangOpts().ObjC && Tok.isNot(tok::ellipsis) &&
  285. Tok.isNot(tok::r_square)) {
  286. CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
  287. return ParseAssignmentExprWithObjCMessageExprStart(
  288. StartLoc, SourceLocation(), nullptr, Idx.get());
  289. }
  290. // If this is a normal array designator, remember it.
  291. if (Tok.isNot(tok::ellipsis)) {
  292. Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc));
  293. } else {
  294. // Handle the gnu array range extension.
  295. Diag(Tok, diag::ext_gnu_array_range);
  296. SourceLocation EllipsisLoc = ConsumeToken();
  297. ExprResult RHS(ParseConstantExpression());
  298. if (RHS.isInvalid()) {
  299. SkipUntil(tok::r_square, StopAtSemi);
  300. return RHS;
  301. }
  302. Desig.AddDesignator(Designator::getArrayRange(Idx.get(),
  303. RHS.get(),
  304. StartLoc, EllipsisLoc));
  305. }
  306. T.consumeClose();
  307. Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
  308. T.getCloseLocation());
  309. }
  310. // Okay, we're done with the designator sequence. We know that there must be
  311. // at least one designator, because the only case we can get into this method
  312. // without a designator is when we have an objc message send. That case is
  313. // handled and returned from above.
  314. assert(!Desig.empty() && "Designator is empty?");
  315. // Handle a normal designator sequence end, which is an equal.
  316. if (Tok.is(tok::equal)) {
  317. SourceLocation EqualLoc = ConsumeToken();
  318. return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
  319. ParseInitializer());
  320. }
  321. // We read some number of designators and found something that isn't an = or
  322. // an initializer. If we have exactly one array designator, this
  323. // is the GNU 'designation: array-designator' extension. Otherwise, it is a
  324. // parse error.
  325. if (Desig.getNumDesignators() == 1 &&
  326. (Desig.getDesignator(0).isArrayDesignator() ||
  327. Desig.getDesignator(0).isArrayRangeDesignator())) {
  328. Diag(Tok, diag::ext_gnu_missing_equal_designator)
  329. << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
  330. return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
  331. true, ParseInitializer());
  332. }
  333. Diag(Tok, diag::err_expected_equal_designator);
  334. return ExprError();
  335. }
  336. /// ParseBraceInitializer - Called when parsing an initializer that has a
  337. /// leading open brace.
  338. ///
  339. /// initializer: [C99 6.7.8]
  340. /// '{' initializer-list '}'
  341. /// '{' initializer-list ',' '}'
  342. /// [GNU] '{' '}'
  343. ///
  344. /// initializer-list:
  345. /// designation[opt] initializer ...[opt]
  346. /// initializer-list ',' designation[opt] initializer ...[opt]
  347. ///
  348. ExprResult Parser::ParseBraceInitializer() {
  349. InMessageExpressionRAIIObject InMessage(*this, false);
  350. BalancedDelimiterTracker T(*this, tok::l_brace);
  351. T.consumeOpen();
  352. SourceLocation LBraceLoc = T.getOpenLocation();
  353. /// InitExprs - This is the actual list of expressions contained in the
  354. /// initializer.
  355. ExprVector InitExprs;
  356. if (Tok.is(tok::r_brace)) {
  357. // Empty initializers are a C++ feature and a GNU extension to C.
  358. if (!getLangOpts().CPlusPlus)
  359. Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
  360. // Match the '}'.
  361. return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace());
  362. }
  363. // Enter an appropriate expression evaluation context for an initializer list.
  364. EnterExpressionEvaluationContext EnterContext(
  365. Actions, EnterExpressionEvaluationContext::InitList);
  366. bool InitExprsOk = true;
  367. while (1) {
  368. // Handle Microsoft __if_exists/if_not_exists if necessary.
  369. if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
  370. Tok.is(tok::kw___if_not_exists))) {
  371. if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) {
  372. if (Tok.isNot(tok::comma)) break;
  373. ConsumeToken();
  374. }
  375. if (Tok.is(tok::r_brace)) break;
  376. continue;
  377. }
  378. // Parse: designation[opt] initializer
  379. // If we know that this cannot be a designation, just parse the nested
  380. // initializer directly.
  381. ExprResult SubElt;
  382. if (MayBeDesignationStart())
  383. SubElt = ParseInitializerWithPotentialDesignator();
  384. else
  385. SubElt = ParseInitializer();
  386. if (Tok.is(tok::ellipsis))
  387. SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
  388. SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get());
  389. // If we couldn't parse the subelement, bail out.
  390. if (SubElt.isUsable()) {
  391. InitExprs.push_back(SubElt.get());
  392. } else {
  393. InitExprsOk = false;
  394. // We have two ways to try to recover from this error: if the code looks
  395. // grammatically ok (i.e. we have a comma coming up) try to continue
  396. // parsing the rest of the initializer. This allows us to emit
  397. // diagnostics for later elements that we find. If we don't see a comma,
  398. // assume there is a parse error, and just skip to recover.
  399. // FIXME: This comment doesn't sound right. If there is a r_brace
  400. // immediately, it can't be an error, since there is no other way of
  401. // leaving this loop except through this if.
  402. if (Tok.isNot(tok::comma)) {
  403. SkipUntil(tok::r_brace, StopBeforeMatch);
  404. break;
  405. }
  406. }
  407. // If we don't have a comma continued list, we're done.
  408. if (Tok.isNot(tok::comma)) break;
  409. // TODO: save comma locations if some client cares.
  410. ConsumeToken();
  411. // Handle trailing comma.
  412. if (Tok.is(tok::r_brace)) break;
  413. }
  414. bool closed = !T.consumeClose();
  415. if (InitExprsOk && closed)
  416. return Actions.ActOnInitList(LBraceLoc, InitExprs,
  417. T.getCloseLocation());
  418. return ExprError(); // an error occurred.
  419. }
  420. // Return true if a comma (or closing brace) is necessary after the
  421. // __if_exists/if_not_exists statement.
  422. bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs,
  423. bool &InitExprsOk) {
  424. bool trailingComma = false;
  425. IfExistsCondition Result;
  426. if (ParseMicrosoftIfExistsCondition(Result))
  427. return false;
  428. BalancedDelimiterTracker Braces(*this, tok::l_brace);
  429. if (Braces.consumeOpen()) {
  430. Diag(Tok, diag::err_expected) << tok::l_brace;
  431. return false;
  432. }
  433. switch (Result.Behavior) {
  434. case IEB_Parse:
  435. // Parse the declarations below.
  436. break;
  437. case IEB_Dependent:
  438. Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
  439. << Result.IsIfExists;
  440. // Fall through to skip.
  441. LLVM_FALLTHROUGH;
  442. case IEB_Skip:
  443. Braces.skipToEnd();
  444. return false;
  445. }
  446. while (!isEofOrEom()) {
  447. trailingComma = false;
  448. // If we know that this cannot be a designation, just parse the nested
  449. // initializer directly.
  450. ExprResult SubElt;
  451. if (MayBeDesignationStart())
  452. SubElt = ParseInitializerWithPotentialDesignator();
  453. else
  454. SubElt = ParseInitializer();
  455. if (Tok.is(tok::ellipsis))
  456. SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
  457. // If we couldn't parse the subelement, bail out.
  458. if (!SubElt.isInvalid())
  459. InitExprs.push_back(SubElt.get());
  460. else
  461. InitExprsOk = false;
  462. if (Tok.is(tok::comma)) {
  463. ConsumeToken();
  464. trailingComma = true;
  465. }
  466. if (Tok.is(tok::r_brace))
  467. break;
  468. }
  469. Braces.consumeClose();
  470. return !trailingComma;
  471. }