Chris Lattner | 7ad0fbe | 2006-11-05 07:46:30 +0000 | [diff] [blame] | 1 | //===--- ParseStmt.cpp - Statement and Block Parser -----------------------===// |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Statement and Block portions of the Parser |
| 10 | // interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 14 | #include "clang/AST/PrettyDeclStackTrace.h" |
Aaron Ballman | b06b15a | 2014-06-06 12:40:24 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Attributes.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/Basic/PrettyStackTrace.h" |
Richard Trieu | 0614cff | 2018-11-28 04:36:31 +0000 | [diff] [blame] | 17 | #include "clang/Parse/LoopHint.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 18 | #include "clang/Parse/Parser.h" |
Vassil Vassilev | 11ad339 | 2017-03-23 15:11:07 +0000 | [diff] [blame] | 19 | #include "clang/Parse/RAIIObjectsForParser.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 20 | #include "clang/Sema/DeclSpec.h" |
| 21 | #include "clang/Sema/Scope.h" |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 22 | #include "clang/Sema/TypoCorrection.h" |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // C99 6.8: Statements and Blocks. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 29 | /// Parse a standalone statement (for instance, as the body of an 'if', |
Richard Smith | 426a47b | 2013-10-28 22:04:30 +0000 | [diff] [blame] | 30 | /// 'while', or 'for'). |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 31 | StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc, |
| 32 | bool AllowOpenMPStandalone) { |
Richard Smith | 426a47b | 2013-10-28 22:04:30 +0000 | [diff] [blame] | 33 | StmtResult Res; |
| 34 | |
| 35 | // We may get back a null statement if we found a #pragma. Keep going until |
| 36 | // we get an actual statement. |
| 37 | do { |
| 38 | StmtVector Stmts; |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 39 | Res = ParseStatementOrDeclaration( |
| 40 | Stmts, AllowOpenMPStandalone ? ACK_StatementsOpenMPAnyExecutable |
| 41 | : ACK_StatementsOpenMPNonStandalone, |
| 42 | TrailingElseLoc); |
Richard Smith | 426a47b | 2013-10-28 22:04:30 +0000 | [diff] [blame] | 43 | } while (!Res.isInvalid() && !Res.get()); |
| 44 | |
| 45 | return Res; |
| 46 | } |
| 47 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 48 | /// ParseStatementOrDeclaration - Read 'statement' or 'declaration'. |
| 49 | /// StatementOrDeclaration: |
| 50 | /// statement |
| 51 | /// declaration |
| 52 | /// |
| 53 | /// statement: |
| 54 | /// labeled-statement |
| 55 | /// compound-statement |
| 56 | /// expression-statement |
| 57 | /// selection-statement |
| 58 | /// iteration-statement |
| 59 | /// jump-statement |
Argyrios Kyrtzidis | dee8291 | 2008-09-07 18:58:01 +0000 | [diff] [blame] | 60 | /// [C++] declaration-statement |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 61 | /// [C++] try-block |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 62 | /// [MS] seh-try-block |
Fariborz Jahanian | 9081457 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 63 | /// [OBC] objc-throw-statement |
| 64 | /// [OBC] objc-try-catch-statement |
Fariborz Jahanian | f89ca38 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 65 | /// [OBC] objc-synchronized-statement |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 66 | /// [GNU] asm-statement |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 67 | /// [OMP] openmp-construct [TODO] |
| 68 | /// |
| 69 | /// labeled-statement: |
| 70 | /// identifier ':' statement |
| 71 | /// 'case' constant-expression ':' statement |
| 72 | /// 'default' ':' statement |
| 73 | /// |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 74 | /// selection-statement: |
| 75 | /// if-statement |
| 76 | /// switch-statement |
| 77 | /// |
| 78 | /// iteration-statement: |
| 79 | /// while-statement |
| 80 | /// do-statement |
| 81 | /// for-statement |
| 82 | /// |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 83 | /// expression-statement: |
| 84 | /// expression[opt] ';' |
| 85 | /// |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 86 | /// jump-statement: |
| 87 | /// 'goto' identifier ';' |
| 88 | /// 'continue' ';' |
| 89 | /// 'break' ';' |
| 90 | /// 'return' expression[opt] ';' |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 91 | /// [GNU] 'goto' '*' expression ';' |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 92 | /// |
Fariborz Jahanian | 9081457 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 93 | /// [OBC] objc-throw-statement: |
| 94 | /// [OBC] '@' 'throw' expression ';' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | /// [OBC] '@' 'throw' ';' |
| 96 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 97 | StmtResult |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 98 | Parser::ParseStatementOrDeclaration(StmtVector &Stmts, |
Jonathan Roelofs | ce1db6d | 2017-03-14 17:29:33 +0000 | [diff] [blame] | 99 | AllowedConstructsKind Allowed, |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 100 | SourceLocation *TrailingElseLoc) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 101 | |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 102 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 103 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 104 | ParsedAttributesWithRange Attrs(AttrFactory); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 105 | MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true); |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 106 | if (!MaybeParseOpenCLUnrollHintAttribute(Attrs)) |
| 107 | return StmtError(); |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 108 | |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 109 | StmtResult Res = ParseStatementOrDeclarationAfterAttributes( |
| 110 | Stmts, Allowed, TrailingElseLoc, Attrs); |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 111 | |
| 112 | assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) && |
| 113 | "attributes on empty statement"); |
| 114 | |
| 115 | if (Attrs.empty() || Res.isInvalid()) |
| 116 | return Res; |
| 117 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 118 | return Actions.ProcessStmtAttributes(Res.get(), Attrs, Attrs.Range); |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Kaelyn Uhrain | 3dfff19 | 2013-09-27 19:40:12 +0000 | [diff] [blame] | 121 | namespace { |
| 122 | class StatementFilterCCC : public CorrectionCandidateCallback { |
| 123 | public: |
| 124 | StatementFilterCCC(Token nextTok) : NextToken(nextTok) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 125 | WantTypeSpecifiers = nextTok.isOneOf(tok::l_paren, tok::less, tok::l_square, |
| 126 | tok::identifier, tok::star, tok::amp); |
| 127 | WantExpressionKeywords = |
| 128 | nextTok.isOneOf(tok::l_paren, tok::identifier, tok::arrow, tok::period); |
| 129 | WantRemainingKeywords = |
| 130 | nextTok.isOneOf(tok::l_paren, tok::semi, tok::identifier, tok::l_brace); |
Kaelyn Uhrain | 3dfff19 | 2013-09-27 19:40:12 +0000 | [diff] [blame] | 131 | WantCXXNamedCasts = false; |
| 132 | } |
| 133 | |
Craig Topper | 2b07f02 | 2014-03-12 05:09:18 +0000 | [diff] [blame] | 134 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | 3dfff19 | 2013-09-27 19:40:12 +0000 | [diff] [blame] | 135 | if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>()) |
Kaelyn Uhrain | 07e6272 | 2013-10-01 22:00:28 +0000 | [diff] [blame] | 136 | return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD); |
Kaelyn Uhrain | 46b6cdc | 2013-09-27 19:40:16 +0000 | [diff] [blame] | 137 | if (NextToken.is(tok::equal)) |
| 138 | return candidate.getCorrectionDeclAs<VarDecl>(); |
Kaelyn Uhrain | 30943ce | 2013-09-27 23:54:23 +0000 | [diff] [blame] | 139 | if (NextToken.is(tok::period) && |
| 140 | candidate.getCorrectionDeclAs<NamespaceDecl>()) |
| 141 | return false; |
Kaelyn Uhrain | 3dfff19 | 2013-09-27 19:40:12 +0000 | [diff] [blame] | 142 | return CorrectionCandidateCallback::ValidateCandidate(candidate); |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | Token NextToken; |
| 147 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 148 | } |
Kaelyn Uhrain | 3dfff19 | 2013-09-27 19:40:12 +0000 | [diff] [blame] | 149 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 150 | StmtResult |
| 151 | Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts, |
Jonathan Roelofs | ce1db6d | 2017-03-14 17:29:33 +0000 | [diff] [blame] | 152 | AllowedConstructsKind Allowed, SourceLocation *TrailingElseLoc, |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 153 | ParsedAttributesWithRange &Attrs) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 154 | const char *SemiError = nullptr; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 155 | StmtResult Res; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 157 | // Cases in this switch statement should fall through if the parser expects |
| 158 | // the token to end in a semicolon (in which case SemiError should be set), |
| 159 | // or they directly 'return;' if not. |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 160 | Retry: |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 161 | tok::TokenKind Kind = Tok.getKind(); |
| 162 | SourceLocation AtLoc; |
| 163 | switch (Kind) { |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 164 | case tok::at: // May be a @try or @throw statement |
| 165 | { |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 166 | ProhibitAttributes(Attrs); // TODO: is it correct? |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 167 | AtLoc = ConsumeToken(); // consume @ |
Sebastian Redl | bab9a4b | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 168 | return ParseObjCAtStatement(AtLoc); |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 169 | } |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 170 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 171 | case tok::code_completion: |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 172 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 173 | cutOffParsing(); |
| 174 | return StmtError(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 175 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 176 | case tok::identifier: { |
| 177 | Token Next = NextToken(); |
| 178 | if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement |
Argyrios Kyrtzidis | 07b8b63 | 2008-07-12 21:04:42 +0000 | [diff] [blame] | 179 | // identifier ':' statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 180 | return ParseLabeledStatement(Attrs); |
Argyrios Kyrtzidis | 07b8b63 | 2008-07-12 21:04:42 +0000 | [diff] [blame] | 181 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 182 | |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 183 | // Look up the identifier, and typo-correct it to a keyword if it's not |
| 184 | // found. |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 185 | if (Next.isNot(tok::coloncolon)) { |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 186 | // Try to limit which sets of keywords should be included in typo |
| 187 | // correction based on what the next token is. |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 188 | if (TryAnnotateName(/*IsAddressOfOperand*/ false, |
| 189 | llvm::make_unique<StatementFilterCCC>(Next)) == |
| 190 | ANK_Error) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 191 | // Handle errors here by skipping up to the next semicolon or '}', and |
| 192 | // eat the semicolon if that's what stopped us. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 193 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 194 | if (Tok.is(tok::semi)) |
| 195 | ConsumeToken(); |
| 196 | return StmtError(); |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 197 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 198 | |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 199 | // If the identifier was typo-corrected, try again. |
| 200 | if (Tok.isNot(tok::identifier)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 201 | goto Retry; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 202 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 203 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 204 | // Fall through |
Galina Kistanova | 387ab8b | 2017-06-01 21:28:26 +0000 | [diff] [blame] | 205 | LLVM_FALLTHROUGH; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 206 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 208 | default: { |
David Majnemer | 6ac7dd1 | 2016-08-01 16:39:29 +0000 | [diff] [blame] | 209 | if ((getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt || |
| 210 | Allowed == ACK_Any) && |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 211 | isDeclarationStatement()) { |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 212 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 213 | DeclGroupPtrTy Decl = ParseDeclaration(DeclaratorContext::BlockContext, |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 214 | DeclEnd, Attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 215 | return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd); |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | f8afb62 | 2006-08-10 18:26:31 +0000 | [diff] [blame] | 219 | Diag(Tok, diag::err_expected_statement); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 220 | return StmtError(); |
Chris Lattner | f8afb62 | 2006-08-10 18:26:31 +0000 | [diff] [blame] | 221 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 223 | return ParseExprStatement(); |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 224 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 225 | |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 226 | case tok::kw_case: // C99 6.8.1: labeled-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 227 | return ParseCaseStatement(); |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 228 | case tok::kw_default: // C99 6.8.1: labeled-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 229 | return ParseDefaultStatement(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 231 | case tok::l_brace: // C99 6.8.2: compound-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 232 | return ParseCompoundStatement(); |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 233 | case tok::semi: { // C99 6.8.3p3: expression[opt] ';' |
Argyrios Kyrtzidis | 43ea78b | 2011-09-01 21:53:45 +0000 | [diff] [blame] | 234 | bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro(); |
| 235 | return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro); |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 236 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 238 | case tok::kw_if: // C99 6.8.4.1: if-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 239 | return ParseIfStatement(TrailingElseLoc); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 240 | case tok::kw_switch: // C99 6.8.4.2: switch-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 241 | return ParseSwitchStatement(TrailingElseLoc); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 243 | case tok::kw_while: // C99 6.8.5.1: while-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 244 | return ParseWhileStatement(TrailingElseLoc); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 245 | case tok::kw_do: // C99 6.8.5.2: do-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 246 | Res = ParseDoStatement(); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 247 | SemiError = "do/while"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 248 | break; |
| 249 | case tok::kw_for: // C99 6.8.5.3: for-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 250 | return ParseForStatement(TrailingElseLoc); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 251 | |
| 252 | case tok::kw_goto: // C99 6.8.6.1: goto-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 253 | Res = ParseGotoStatement(); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 254 | SemiError = "goto"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 255 | break; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 256 | case tok::kw_continue: // C99 6.8.6.2: continue-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 257 | Res = ParseContinueStatement(); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 258 | SemiError = "continue"; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 259 | break; |
| 260 | case tok::kw_break: // C99 6.8.6.3: break-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 261 | Res = ParseBreakStatement(); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 262 | SemiError = "break"; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 263 | break; |
| 264 | case tok::kw_return: // C99 6.8.6.4: return-statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 265 | Res = ParseReturnStatement(); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 266 | SemiError = "return"; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 267 | break; |
Richard Smith | 0e304ea | 2015-10-22 04:46:14 +0000 | [diff] [blame] | 268 | case tok::kw_co_return: // C++ Coroutines: co_return statement |
| 269 | Res = ParseReturnStatement(); |
| 270 | SemiError = "co_return"; |
| 271 | break; |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 272 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 273 | case tok::kw_asm: { |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 274 | ProhibitAttributes(Attrs); |
Steve Naroff | b2c80c7 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 275 | bool msAsm = false; |
| 276 | Res = ParseAsmStatement(msAsm); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 277 | Res = Actions.ActOnFinishFullStmt(Res.get()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 278 | if (msAsm) return Res; |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 279 | SemiError = "asm"; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 280 | break; |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 281 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 282 | |
Reid Kleckner | 6d8d22a | 2014-06-25 00:28:35 +0000 | [diff] [blame] | 283 | case tok::kw___if_exists: |
| 284 | case tok::kw___if_not_exists: |
| 285 | ProhibitAttributes(Attrs); |
| 286 | ParseMicrosoftIfExistsStatement(Stmts); |
| 287 | // An __if_exists block is like a compound statement, but it doesn't create |
| 288 | // a new scope. |
| 289 | return StmtEmpty(); |
| 290 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 291 | case tok::kw_try: // C++ 15: try-block |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 292 | return ParseCXXTryBlock(); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 293 | |
| 294 | case tok::kw___try: |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 295 | ProhibitAttributes(Attrs); // TODO: is it correct? |
| 296 | return ParseSEHTryBlock(); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 297 | |
Nico Weber | c7d0596 | 2014-07-06 22:32:59 +0000 | [diff] [blame] | 298 | case tok::kw___leave: |
| 299 | Res = ParseSEHLeaveStatement(); |
| 300 | SemiError = "__leave"; |
| 301 | break; |
| 302 | |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 303 | case tok::annot_pragma_vis: |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 304 | ProhibitAttributes(Attrs); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 305 | HandlePragmaVisibility(); |
| 306 | return StmtEmpty(); |
| 307 | |
| 308 | case tok::annot_pragma_pack: |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 309 | ProhibitAttributes(Attrs); |
Eli Friedman | ec52f92 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 310 | HandlePragmaPack(); |
| 311 | return StmtEmpty(); |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 312 | |
Eli Friedman | bbbbac6 | 2012-10-09 22:46:54 +0000 | [diff] [blame] | 313 | case tok::annot_pragma_msstruct: |
| 314 | ProhibitAttributes(Attrs); |
| 315 | HandlePragmaMSStruct(); |
| 316 | return StmtEmpty(); |
| 317 | |
Eli Friedman | ae8ee25 | 2012-10-08 23:52:38 +0000 | [diff] [blame] | 318 | case tok::annot_pragma_align: |
| 319 | ProhibitAttributes(Attrs); |
| 320 | HandlePragmaAlign(); |
| 321 | return StmtEmpty(); |
| 322 | |
Eli Friedman | bbbbac6 | 2012-10-09 22:46:54 +0000 | [diff] [blame] | 323 | case tok::annot_pragma_weak: |
| 324 | ProhibitAttributes(Attrs); |
| 325 | HandlePragmaWeak(); |
| 326 | return StmtEmpty(); |
| 327 | |
| 328 | case tok::annot_pragma_weakalias: |
| 329 | ProhibitAttributes(Attrs); |
| 330 | HandlePragmaWeakAlias(); |
| 331 | return StmtEmpty(); |
| 332 | |
| 333 | case tok::annot_pragma_redefine_extname: |
| 334 | ProhibitAttributes(Attrs); |
| 335 | HandlePragmaRedefineExtname(); |
| 336 | return StmtEmpty(); |
| 337 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 338 | case tok::annot_pragma_fp_contract: |
Richard Smith | ca9b0b6 | 2013-11-15 21:10:54 +0000 | [diff] [blame] | 339 | ProhibitAttributes(Attrs); |
Lang Hames | a930e71 | 2012-10-21 01:10:01 +0000 | [diff] [blame] | 340 | Diag(Tok, diag::err_pragma_fp_contract_scope); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 341 | ConsumeAnnotationToken(); |
Lang Hames | a930e71 | 2012-10-21 01:10:01 +0000 | [diff] [blame] | 342 | return StmtError(); |
| 343 | |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 344 | case tok::annot_pragma_fp: |
| 345 | ProhibitAttributes(Attrs); |
| 346 | Diag(Tok, diag::err_pragma_fp_scope); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 347 | ConsumeAnnotationToken(); |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 348 | return StmtError(); |
| 349 | |
Kevin P. Neal | 2c0bc8b | 2018-08-14 17:06:56 +0000 | [diff] [blame] | 350 | case tok::annot_pragma_fenv_access: |
| 351 | ProhibitAttributes(Attrs); |
| 352 | HandlePragmaFEnvAccess(); |
| 353 | return StmtEmpty(); |
| 354 | |
Eli Friedman | 68be164 | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 355 | case tok::annot_pragma_opencl_extension: |
| 356 | ProhibitAttributes(Attrs); |
| 357 | HandlePragmaOpenCLExtension(); |
| 358 | return StmtEmpty(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 359 | |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 360 | case tok::annot_pragma_captured: |
Richard Smith | 12a41bd | 2013-09-16 21:17:44 +0000 | [diff] [blame] | 361 | ProhibitAttributes(Attrs); |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 362 | return HandlePragmaCaptured(); |
| 363 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 364 | case tok::annot_pragma_openmp: |
Richard Smith | 12a41bd | 2013-09-16 21:17:44 +0000 | [diff] [blame] | 365 | ProhibitAttributes(Attrs); |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 366 | return ParseOpenMPDeclarativeOrExecutableDirective(Allowed); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 367 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 368 | case tok::annot_pragma_ms_pointers_to_members: |
| 369 | ProhibitAttributes(Attrs); |
| 370 | HandlePragmaMSPointersToMembers(); |
| 371 | return StmtEmpty(); |
| 372 | |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 373 | case tok::annot_pragma_ms_pragma: |
| 374 | ProhibitAttributes(Attrs); |
| 375 | HandlePragmaMSPragma(); |
| 376 | return StmtEmpty(); |
Aaron Ballman | b06b15a | 2014-06-06 12:40:24 +0000 | [diff] [blame] | 377 | |
Alexey Bataev | 3d42f34 | 2015-11-20 07:02:57 +0000 | [diff] [blame] | 378 | case tok::annot_pragma_ms_vtordisp: |
| 379 | ProhibitAttributes(Attrs); |
| 380 | HandlePragmaMSVtorDisp(); |
| 381 | return StmtEmpty(); |
| 382 | |
Aaron Ballman | b06b15a | 2014-06-06 12:40:24 +0000 | [diff] [blame] | 383 | case tok::annot_pragma_loop_hint: |
| 384 | ProhibitAttributes(Attrs); |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 385 | return ParsePragmaLoopHint(Stmts, Allowed, TrailingElseLoc, Attrs); |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 386 | |
| 387 | case tok::annot_pragma_dump: |
| 388 | HandlePragmaDump(); |
| 389 | return StmtEmpty(); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 390 | |
| 391 | case tok::annot_pragma_attribute: |
| 392 | HandlePragmaAttribute(); |
| 393 | return StmtEmpty(); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 396 | // If we reached this code, the statement must end in a semicolon. |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 397 | if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) { |
Chris Lattner | 8e3eed0 | 2009-06-14 00:23:56 +0000 | [diff] [blame] | 398 | // If the result was valid, then we do want to diagnose this. Use |
| 399 | // ExpectAndConsume to emit the diagnostic, even though we know it won't |
| 400 | // succeed. |
| 401 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError); |
Chris Lattner | 0046de1 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 402 | // Skip until we see a } or ;, but don't eat it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 403 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 404 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 406 | return Res; |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 409 | /// Parse an expression statement. |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 410 | StmtResult Parser::ParseExprStatement() { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 411 | // If a case keyword is missing, this is where it should be inserted. |
| 412 | Token OldToken = Tok; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 413 | |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 414 | ExprStatementTokLoc = Tok.getLocation(); |
| 415 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 416 | // expression[opt] ';' |
Douglas Gregor | da6c89d | 2011-04-27 06:18:01 +0000 | [diff] [blame] | 417 | ExprResult Expr(ParseExpression()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 418 | if (Expr.isInvalid()) { |
| 419 | // If the expression is invalid, skip ahead to the next semicolon or '}'. |
| 420 | // Not doing this opens us up to the possibility of infinite loops if |
| 421 | // ParseExpression does not consume any tokens. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 422 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 423 | if (Tok.is(tok::semi)) |
| 424 | ConsumeToken(); |
John McCall | eaef89b | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 425 | return Actions.ActOnExprStmtError(); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 426 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 427 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 428 | if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() && |
| 429 | Actions.CheckCaseExpression(Expr.get())) { |
| 430 | // If a constant expression is followed by a colon inside a switch block, |
| 431 | // suggest a missing case keyword. |
| 432 | Diag(OldToken, diag::err_expected_case_before_expression) |
| 433 | << FixItHint::CreateInsertion(OldToken.getLocation(), "case "); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 434 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 435 | // Recover parsing as a case statement. |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 436 | return ParseCaseStatement(/*MissingCase=*/true, Expr); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 437 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 438 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 439 | // Otherwise, eat the semicolon. |
| 440 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
Aaron Ballman | fb6deeb | 2019-01-04 16:58:14 +0000 | [diff] [blame] | 441 | return Actions.ActOnExprStmt(Expr, isExprValueDiscarded()); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 442 | } |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 443 | |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 444 | /// ParseSEHTryBlockCommon |
| 445 | /// |
| 446 | /// seh-try-block: |
| 447 | /// '__try' compound-statement seh-handler |
| 448 | /// |
| 449 | /// seh-handler: |
| 450 | /// seh-except-block |
| 451 | /// seh-finally-block |
| 452 | /// |
Nico Weber | dd25674 | 2015-02-25 01:43:27 +0000 | [diff] [blame] | 453 | StmtResult Parser::ParseSEHTryBlock() { |
| 454 | assert(Tok.is(tok::kw___try) && "Expected '__try'"); |
| 455 | SourceLocation TryLoc = ConsumeToken(); |
| 456 | |
Nico Weber | fc3fe4f | 2015-02-25 02:22:06 +0000 | [diff] [blame] | 457 | if (Tok.isNot(tok::l_brace)) |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 458 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 459 | |
Momchil Velikov | 57c681f | 2017-08-10 15:43:06 +0000 | [diff] [blame] | 460 | StmtResult TryBlock(ParseCompoundStatement( |
| 461 | /*isStmtExpr=*/false, |
| 462 | Scope::DeclScope | Scope::CompoundStmtScope | Scope::SEHTryScope)); |
| 463 | if (TryBlock.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 464 | return TryBlock; |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 465 | |
| 466 | StmtResult Handler; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 467 | if (Tok.is(tok::identifier) && |
Douglas Gregor | 60060d6 | 2011-10-21 03:57:52 +0000 | [diff] [blame] | 468 | Tok.getIdentifierInfo() == getSEHExceptKeyword()) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 469 | SourceLocation Loc = ConsumeToken(); |
| 470 | Handler = ParseSEHExceptBlock(Loc); |
| 471 | } else if (Tok.is(tok::kw___finally)) { |
| 472 | SourceLocation Loc = ConsumeToken(); |
| 473 | Handler = ParseSEHFinallyBlock(Loc); |
| 474 | } else { |
Nico Weber | fc3fe4f | 2015-02-25 02:22:06 +0000 | [diff] [blame] | 475 | return StmtError(Diag(Tok, diag::err_seh_expected_handler)); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | if(Handler.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 479 | return Handler; |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 480 | |
| 481 | return Actions.ActOnSEHTryBlock(false /* IsCXXTry */, |
| 482 | TryLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 483 | TryBlock.get(), |
Warren Hunt | f6be4cb | 2014-07-25 20:52:51 +0000 | [diff] [blame] | 484 | Handler.get()); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /// ParseSEHExceptBlock - Handle __except |
| 488 | /// |
| 489 | /// seh-except-block: |
| 490 | /// '__except' '(' seh-filter-expression ')' compound-statement |
| 491 | /// |
| 492 | StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) { |
| 493 | PoisonIdentifierRAIIObject raii(Ident__exception_code, false), |
| 494 | raii2(Ident___exception_code, false), |
| 495 | raii3(Ident_GetExceptionCode, false); |
| 496 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 497 | if (ExpectAndConsume(tok::l_paren)) |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 498 | return StmtError(); |
| 499 | |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 500 | ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope | |
| 501 | Scope::SEHExceptScope); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 502 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 503 | if (getLangOpts().Borland) { |
Francois Pichet | bfaf477 | 2011-04-28 03:14:31 +0000 | [diff] [blame] | 504 | Ident__exception_info->setIsPoisoned(false); |
| 505 | Ident___exception_info->setIsPoisoned(false); |
| 506 | Ident_GetExceptionInfo->setIsPoisoned(false); |
| 507 | } |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 508 | |
| 509 | ExprResult FilterExpr; |
| 510 | { |
| 511 | ParseScopeFlags FilterScope(this, getCurScope()->getFlags() | |
| 512 | Scope::SEHFilterScope); |
Reid Kleckner | 85368fb | 2015-04-02 22:09:32 +0000 | [diff] [blame] | 513 | FilterExpr = Actions.CorrectDelayedTyposInExpr(ParseExpression()); |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 514 | } |
Francois Pichet | bfaf477 | 2011-04-28 03:14:31 +0000 | [diff] [blame] | 515 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 516 | if (getLangOpts().Borland) { |
Francois Pichet | bfaf477 | 2011-04-28 03:14:31 +0000 | [diff] [blame] | 517 | Ident__exception_info->setIsPoisoned(true); |
| 518 | Ident___exception_info->setIsPoisoned(true); |
| 519 | Ident_GetExceptionInfo->setIsPoisoned(true); |
| 520 | } |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 521 | |
| 522 | if(FilterExpr.isInvalid()) |
| 523 | return StmtError(); |
| 524 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 525 | if (ExpectAndConsume(tok::r_paren)) |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 526 | return StmtError(); |
| 527 | |
Nico Weber | fc3fe4f | 2015-02-25 02:22:06 +0000 | [diff] [blame] | 528 | if (Tok.isNot(tok::l_brace)) |
| 529 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
| 530 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 531 | StmtResult Block(ParseCompoundStatement()); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 532 | |
| 533 | if(Block.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 534 | return Block; |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 535 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 536 | return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get()); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /// ParseSEHFinallyBlock - Handle __finally |
| 540 | /// |
| 541 | /// seh-finally-block: |
| 542 | /// '__finally' compound-statement |
| 543 | /// |
Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 544 | StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 545 | PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false), |
| 546 | raii2(Ident___abnormal_termination, false), |
| 547 | raii3(Ident_AbnormalTermination, false); |
| 548 | |
Nico Weber | fc3fe4f | 2015-02-25 02:22:06 +0000 | [diff] [blame] | 549 | if (Tok.isNot(tok::l_brace)) |
| 550 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
| 551 | |
Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 552 | ParseScope FinallyScope(this, 0); |
| 553 | Actions.ActOnStartSEHFinallyBlock(); |
| 554 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 555 | StmtResult Block(ParseCompoundStatement()); |
Nico Weber | ce90329 | 2015-03-09 03:17:15 +0000 | [diff] [blame] | 556 | if(Block.isInvalid()) { |
| 557 | Actions.ActOnAbortSEHFinallyBlock(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 558 | return Block; |
Nico Weber | ce90329 | 2015-03-09 03:17:15 +0000 | [diff] [blame] | 559 | } |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 560 | |
Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 561 | return Actions.ActOnFinishSEHFinallyBlock(FinallyLoc, Block.get()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Nico Weber | c7d0596 | 2014-07-06 22:32:59 +0000 | [diff] [blame] | 564 | /// Handle __leave |
| 565 | /// |
| 566 | /// seh-leave-statement: |
| 567 | /// '__leave' ';' |
| 568 | /// |
| 569 | StmtResult Parser::ParseSEHLeaveStatement() { |
| 570 | SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'. |
| 571 | return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope()); |
| 572 | } |
| 573 | |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 574 | /// ParseLabeledStatement - We have an identifier and a ':' after it. |
Chris Lattner | 6dfd978 | 2006-08-10 18:31:37 +0000 | [diff] [blame] | 575 | /// |
| 576 | /// labeled-statement: |
| 577 | /// identifier ':' statement |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 578 | /// [GNU] identifier ':' attributes[opt] statement |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 579 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 580 | StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 581 | assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() && |
| 582 | "Not an identifier!"); |
| 583 | |
| 584 | Token IdentTok = Tok; // Save the whole token. |
| 585 | ConsumeToken(); // eat the identifier. |
| 586 | |
| 587 | assert(Tok.is(tok::colon) && "Not a label!"); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 588 | |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 589 | // identifier ':' statement |
| 590 | SourceLocation ColonLoc = ConsumeToken(); |
| 591 | |
Richard Smith | a3e01cf | 2013-11-15 22:45:29 +0000 | [diff] [blame] | 592 | // Read label attributes, if present. |
| 593 | StmtResult SubStmt; |
| 594 | if (Tok.is(tok::kw___attribute)) { |
| 595 | ParsedAttributesWithRange TempAttrs(AttrFactory); |
| 596 | ParseGNUAttributes(TempAttrs); |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 597 | |
Richard Smith | a3e01cf | 2013-11-15 22:45:29 +0000 | [diff] [blame] | 598 | // In C++, GNU attributes only apply to the label if they are followed by a |
| 599 | // semicolon, to disambiguate label attributes from attributes on a labeled |
| 600 | // declaration. |
| 601 | // |
| 602 | // This doesn't quite match what GCC does; if the attribute list is empty |
| 603 | // and followed by a semicolon, GCC will reject (it appears to parse the |
| 604 | // attributes as part of a statement in that case). That looks like a bug. |
| 605 | if (!getLangOpts().CPlusPlus || Tok.is(tok::semi)) |
| 606 | attrs.takeAllFrom(TempAttrs); |
| 607 | else if (isDeclarationStatement()) { |
| 608 | StmtVector Stmts; |
| 609 | // FIXME: We should do this whether or not we have a declaration |
| 610 | // statement, but that doesn't work correctly (because ProhibitAttributes |
| 611 | // can't handle GNU attributes), so only call it in the one case where |
| 612 | // GNU attributes are allowed. |
| 613 | SubStmt = ParseStatementOrDeclarationAfterAttributes( |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 614 | Stmts, /*Allowed=*/ACK_StatementsOpenMPNonStandalone, nullptr, |
| 615 | TempAttrs); |
Richard Smith | a3e01cf | 2013-11-15 22:45:29 +0000 | [diff] [blame] | 616 | if (!TempAttrs.empty() && !SubStmt.isInvalid()) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 617 | SubStmt = Actions.ProcessStmtAttributes(SubStmt.get(), TempAttrs, |
| 618 | TempAttrs.Range); |
Richard Smith | a3e01cf | 2013-11-15 22:45:29 +0000 | [diff] [blame] | 619 | } else { |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 620 | Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi; |
Richard Smith | a3e01cf | 2013-11-15 22:45:29 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
| 624 | // If we've not parsed a statement yet, parse one now. |
| 625 | if (!SubStmt.isInvalid() && !SubStmt.isUsable()) |
| 626 | SubStmt = ParseStatement(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 627 | |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 628 | // Broken substmt shouldn't prevent the label from being added to the AST. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 629 | if (SubStmt.isInvalid()) |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 630 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 631 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 632 | LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(), |
| 633 | IdentTok.getLocation()); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 634 | Actions.ProcessDeclAttributeList(Actions.CurScope, LD, attrs); |
| 635 | attrs.clear(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 636 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 637 | return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc, |
| 638 | SubStmt.get()); |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 639 | } |
Chris Lattner | f8afb62 | 2006-08-10 18:26:31 +0000 | [diff] [blame] | 640 | |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 641 | /// ParseCaseStatement |
| 642 | /// labeled-statement: |
| 643 | /// 'case' constant-expression ':' statement |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 644 | /// [GNU] 'case' constant-expression '...' constant-expression ':' statement |
Chris Lattner | 8693a51 | 2006-08-13 21:54:02 +0000 | [diff] [blame] | 645 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 646 | StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) { |
Richard Smith | d4257d8 | 2011-04-21 22:48:40 +0000 | [diff] [blame] | 647 | assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 649 | // It is very very common for code to contain many case statements recursively |
| 650 | // nested, as in (but usually without indentation): |
| 651 | // case 1: |
| 652 | // case 2: |
| 653 | // case 3: |
| 654 | // case 4: |
| 655 | // case 5: etc. |
| 656 | // |
| 657 | // Parsing this naively works, but is both inefficient and can cause us to run |
| 658 | // out of stack space in our recursive descent parser. As a special case, |
Chris Lattner | 2b19a658 | 2009-03-04 18:24:58 +0000 | [diff] [blame] | 659 | // flatten this recursion into an iterative loop. This is complex and gross, |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 660 | // but all the grossness is constrained to ParseCaseStatement (and some |
Richard Smith | a3e01cf | 2013-11-15 22:45:29 +0000 | [diff] [blame] | 661 | // weirdness in the actions), so this is just local grossness :). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 663 | // TopLevelCase - This is the highest level we have parsed. 'case 1' in the |
| 664 | // example above. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 665 | StmtResult TopLevelCase(true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 667 | // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which |
| 668 | // gets updated each time a new case is parsed, and whose body is unset so |
| 669 | // far. When parsing 'case 4', this is the 'case 3' node. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 670 | Stmt *DeepestParsedCaseStmt = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 672 | // While we have case statements, eat and stack them. |
David Majnemer | 0ac67fa | 2011-06-13 05:50:12 +0000 | [diff] [blame] | 673 | SourceLocation ColonLoc; |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 674 | do { |
Richard Trieu | 2c850c0 | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 675 | SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() : |
| 676 | ConsumeToken(); // eat the 'case'. |
Serge Pavlov | 921c2ba | 2014-05-21 14:48:43 +0000 | [diff] [blame] | 677 | ColonLoc = SourceLocation(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 679 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 680 | Actions.CodeCompleteCase(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 681 | cutOffParsing(); |
| 682 | return StmtError(); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 683 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 684 | |
Chris Lattner | 125c0ee | 2009-12-10 00:38:54 +0000 | [diff] [blame] | 685 | /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'. |
| 686 | /// Disable this form of error recovery while we're parsing the case |
| 687 | /// expression. |
| 688 | ColonProtectionRAIIObject ColonProtection(*this); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 689 | |
Serge Pavlov | 921c2ba | 2014-05-21 14:48:43 +0000 | [diff] [blame] | 690 | ExprResult LHS; |
| 691 | if (!MissingCase) { |
Richard Smith | ef6c43d | 2018-07-26 18:41:30 +0000 | [diff] [blame] | 692 | LHS = ParseCaseExpression(CaseLoc); |
Serge Pavlov | 921c2ba | 2014-05-21 14:48:43 +0000 | [diff] [blame] | 693 | if (LHS.isInvalid()) { |
| 694 | // If constant-expression is parsed unsuccessfully, recover by skipping |
| 695 | // current case statement (moving to the colon that ends it). |
Richard Smith | ef6c43d | 2018-07-26 18:41:30 +0000 | [diff] [blame] | 696 | if (!SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) |
| 697 | return StmtError(); |
Serge Pavlov | 921c2ba | 2014-05-21 14:48:43 +0000 | [diff] [blame] | 698 | } |
| 699 | } else { |
| 700 | LHS = Expr; |
| 701 | MissingCase = false; |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 702 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 704 | // GNU case range extension. |
| 705 | SourceLocation DotDotDotLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 706 | ExprResult RHS; |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 707 | if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) { |
| 708 | Diag(DotDotDotLoc, diag::ext_gnu_case_range); |
Richard Smith | ef6c43d | 2018-07-26 18:41:30 +0000 | [diff] [blame] | 709 | RHS = ParseCaseExpression(CaseLoc); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 710 | if (RHS.isInvalid()) { |
Richard Smith | ef6c43d | 2018-07-26 18:41:30 +0000 | [diff] [blame] | 711 | if (!SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) |
| 712 | return StmtError(); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 715 | |
Chris Lattner | 125c0ee | 2009-12-10 00:38:54 +0000 | [diff] [blame] | 716 | ColonProtection.restore(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 717 | |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 718 | if (TryConsumeToken(tok::colon, ColonLoc)) { |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 719 | } else if (TryConsumeToken(tok::semi, ColonLoc) || |
| 720 | TryConsumeToken(tok::coloncolon, ColonLoc)) { |
| 721 | // Treat "case blah;" or "case blah::" as a typo for "case blah:". |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 722 | Diag(ColonLoc, diag::err_expected_after) |
| 723 | << "'case'" << tok::colon |
| 724 | << FixItHint::CreateReplacement(ColonLoc, ":"); |
John McCall | 0140bfe | 2011-01-22 09:28:32 +0000 | [diff] [blame] | 725 | } else { |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 726 | SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation); |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 727 | Diag(ExpectedLoc, diag::err_expected_after) |
| 728 | << "'case'" << tok::colon |
| 729 | << FixItHint::CreateInsertion(ExpectedLoc, ":"); |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 730 | ColonLoc = ExpectedLoc; |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 731 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 732 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 733 | StmtResult Case = |
Richard Smith | ef6c43d | 2018-07-26 18:41:30 +0000 | [diff] [blame] | 734 | Actions.ActOnCaseStmt(CaseLoc, LHS, DotDotDotLoc, RHS, ColonLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 736 | // If we had a sema error parsing this case, then just ignore it and |
| 737 | // continue parsing the sub-stmt. |
| 738 | if (Case.isInvalid()) { |
| 739 | if (TopLevelCase.isInvalid()) // No parsed case stmts. |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 740 | return ParseStatement(/*TrailingElseLoc=*/nullptr, |
| 741 | /*AllowOpenMPStandalone=*/true); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 742 | // Otherwise, just don't add it as a nested case. |
| 743 | } else { |
| 744 | // If this is the first case statement we parsed, it becomes TopLevelCase. |
| 745 | // Otherwise we link it into the current chain. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 746 | Stmt *NextDeepest = Case.get(); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 747 | if (TopLevelCase.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 748 | TopLevelCase = Case; |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 749 | else |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 750 | Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get()); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 751 | DeepestParsedCaseStmt = NextDeepest; |
| 752 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 754 | // Handle all case statements. |
| 755 | } while (Tok.is(tok::kw_case)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 757 | // If we found a non-case statement, start by parsing it. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 758 | StmtResult SubStmt; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 760 | if (Tok.isNot(tok::r_brace)) { |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 761 | SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr, |
| 762 | /*AllowOpenMPStandalone=*/true); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 763 | } else { |
| 764 | // Nicely diagnose the common error "switch (X) { case 4: }", which is |
Serge Pavlov | 921c2ba | 2014-05-21 14:48:43 +0000 | [diff] [blame] | 765 | // not valid. If ColonLoc doesn't point to a valid text location, there was |
| 766 | // another parsing error, so avoid producing extra diagnostics. |
| 767 | if (ColonLoc.isValid()) { |
| 768 | SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc); |
| 769 | Diag(AfterColonLoc, diag::err_label_end_of_compound_statement) |
| 770 | << FixItHint::CreateInsertion(AfterColonLoc, " ;"); |
| 771 | } |
| 772 | SubStmt = StmtError(); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 773 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 775 | // Install the body into the most deeply-nested case. |
Serge Pavlov | 921c2ba | 2014-05-21 14:48:43 +0000 | [diff] [blame] | 776 | if (DeepestParsedCaseStmt) { |
| 777 | // Broken sub-stmt shouldn't prevent forming the case statement properly. |
| 778 | if (SubStmt.isInvalid()) |
| 779 | SubStmt = Actions.ActOnNullStmt(SourceLocation()); |
| 780 | Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get()); |
| 781 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 782 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 783 | // Return the top level parsed statement tree. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 784 | return TopLevelCase; |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /// ParseDefaultStatement |
| 788 | /// labeled-statement: |
| 789 | /// 'default' ':' statement |
| 790 | /// Note that this does not parse the 'statement' at the end. |
| 791 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 792 | StmtResult Parser::ParseDefaultStatement() { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 793 | assert(Tok.is(tok::kw_default) && "Not a default stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 794 | SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'. |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 796 | SourceLocation ColonLoc; |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 797 | if (TryConsumeToken(tok::colon, ColonLoc)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 798 | } else if (TryConsumeToken(tok::semi, ColonLoc)) { |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 799 | // Treat "default;" as a typo for "default:". |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 800 | Diag(ColonLoc, diag::err_expected_after) |
| 801 | << "'default'" << tok::colon |
| 802 | << FixItHint::CreateReplacement(ColonLoc, ":"); |
John McCall | 0140bfe | 2011-01-22 09:28:32 +0000 | [diff] [blame] | 803 | } else { |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 804 | SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation); |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 805 | Diag(ExpectedLoc, diag::err_expected_after) |
| 806 | << "'default'" << tok::colon |
| 807 | << FixItHint::CreateInsertion(ExpectedLoc, ":"); |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 808 | ColonLoc = ExpectedLoc; |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 809 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 810 | |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 811 | StmtResult SubStmt; |
| 812 | |
| 813 | if (Tok.isNot(tok::r_brace)) { |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 814 | SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr, |
| 815 | /*AllowOpenMPStandalone=*/true); |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 816 | } else { |
| 817 | // Diagnose the common error "switch (X) {... default: }", which is |
| 818 | // not valid. |
David Majnemer | c6a9987 | 2011-06-14 15:24:38 +0000 | [diff] [blame] | 819 | SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc); |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 820 | Diag(AfterColonLoc, diag::err_label_end_of_compound_statement) |
| 821 | << FixItHint::CreateInsertion(AfterColonLoc, " ;"); |
| 822 | SubStmt = true; |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 825 | // Broken sub-stmt shouldn't prevent forming the case statement properly. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 826 | if (SubStmt.isInvalid()) |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 827 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 828 | |
Sebastian Redl | 1cbb5918 | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 829 | return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 830 | SubStmt.get(), getCurScope()); |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 833 | StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) { |
Momchil Velikov | 57c681f | 2017-08-10 15:43:06 +0000 | [diff] [blame] | 834 | return ParseCompoundStatement(isStmtExpr, |
| 835 | Scope::DeclScope | Scope::CompoundStmtScope); |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 836 | } |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 837 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 838 | /// ParseCompoundStatement - Parse a "{}" block. |
| 839 | /// |
| 840 | /// compound-statement: [C99 6.8.2] |
| 841 | /// { block-item-list[opt] } |
| 842 | /// [GNU] { label-declarations block-item-list } [TODO] |
| 843 | /// |
| 844 | /// block-item-list: |
| 845 | /// block-item |
| 846 | /// block-item-list block-item |
| 847 | /// |
| 848 | /// block-item: |
| 849 | /// declaration |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 850 | /// [GNU] '__extension__' declaration |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 851 | /// statement |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 852 | /// |
| 853 | /// [GNU] label-declarations: |
| 854 | /// [GNU] label-declaration |
| 855 | /// [GNU] label-declarations label-declaration |
| 856 | /// |
| 857 | /// [GNU] label-declaration: |
| 858 | /// [GNU] '__label__' identifier-list ';' |
| 859 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 860 | StmtResult Parser::ParseCompoundStatement(bool isStmtExpr, |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 861 | unsigned ScopeFlags) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 862 | assert(Tok.is(tok::l_brace) && "Not a compount stmt!"); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 863 | |
Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 864 | // Enter a scope to hold everything within the compound stmt. Compound |
| 865 | // statements can always hold declarations. |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 866 | ParseScope CompoundScope(this, ScopeFlags); |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 867 | |
| 868 | // Parse the statements in the body. |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 869 | return ParseCompoundStatementBody(isStmtExpr); |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Lang Hames | 2954cea | 2012-11-03 22:29:05 +0000 | [diff] [blame] | 872 | /// Parse any pragmas at the start of the compound expression. We handle these |
| 873 | /// separately since some pragmas (FP_CONTRACT) must appear before any C |
| 874 | /// statement in the compound, but may be intermingled with other pragmas. |
| 875 | void Parser::ParseCompoundStatementLeadingPragmas() { |
| 876 | bool checkForPragmas = true; |
| 877 | while (checkForPragmas) { |
| 878 | switch (Tok.getKind()) { |
| 879 | case tok::annot_pragma_vis: |
| 880 | HandlePragmaVisibility(); |
| 881 | break; |
| 882 | case tok::annot_pragma_pack: |
| 883 | HandlePragmaPack(); |
| 884 | break; |
| 885 | case tok::annot_pragma_msstruct: |
| 886 | HandlePragmaMSStruct(); |
| 887 | break; |
| 888 | case tok::annot_pragma_align: |
| 889 | HandlePragmaAlign(); |
| 890 | break; |
| 891 | case tok::annot_pragma_weak: |
| 892 | HandlePragmaWeak(); |
| 893 | break; |
| 894 | case tok::annot_pragma_weakalias: |
| 895 | HandlePragmaWeakAlias(); |
| 896 | break; |
| 897 | case tok::annot_pragma_redefine_extname: |
| 898 | HandlePragmaRedefineExtname(); |
| 899 | break; |
| 900 | case tok::annot_pragma_opencl_extension: |
| 901 | HandlePragmaOpenCLExtension(); |
| 902 | break; |
| 903 | case tok::annot_pragma_fp_contract: |
| 904 | HandlePragmaFPContract(); |
| 905 | break; |
Adam Nemet | 60d3264 | 2017-04-04 21:18:36 +0000 | [diff] [blame] | 906 | case tok::annot_pragma_fp: |
| 907 | HandlePragmaFP(); |
| 908 | break; |
Kevin P. Neal | 2c0bc8b | 2018-08-14 17:06:56 +0000 | [diff] [blame] | 909 | case tok::annot_pragma_fenv_access: |
| 910 | HandlePragmaFEnvAccess(); |
| 911 | break; |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 912 | case tok::annot_pragma_ms_pointers_to_members: |
| 913 | HandlePragmaMSPointersToMembers(); |
| 914 | break; |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 915 | case tok::annot_pragma_ms_pragma: |
| 916 | HandlePragmaMSPragma(); |
| 917 | break; |
Alexey Bataev | 3d42f34 | 2015-11-20 07:02:57 +0000 | [diff] [blame] | 918 | case tok::annot_pragma_ms_vtordisp: |
| 919 | HandlePragmaMSVtorDisp(); |
| 920 | break; |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 921 | case tok::annot_pragma_dump: |
| 922 | HandlePragmaDump(); |
| 923 | break; |
Lang Hames | 2954cea | 2012-11-03 22:29:05 +0000 | [diff] [blame] | 924 | default: |
| 925 | checkForPragmas = false; |
| 926 | break; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | } |
| 931 | |
Roman Lebedev | 377748f | 2018-11-20 18:59:05 +0000 | [diff] [blame] | 932 | /// Consume any extra semi-colons resulting in null statements, |
| 933 | /// returning true if any tok::semi were consumed. |
| 934 | bool Parser::ConsumeNullStmt(StmtVector &Stmts) { |
| 935 | if (!Tok.is(tok::semi)) |
| 936 | return false; |
| 937 | |
| 938 | SourceLocation StartLoc = Tok.getLocation(); |
| 939 | SourceLocation EndLoc; |
| 940 | |
| 941 | while (Tok.is(tok::semi) && !Tok.hasLeadingEmptyMacro() && |
| 942 | Tok.getLocation().isValid() && !Tok.getLocation().isMacroID()) { |
| 943 | EndLoc = Tok.getLocation(); |
| 944 | |
| 945 | // Don't just ConsumeToken() this tok::semi, do store it in AST. |
| 946 | StmtResult R = ParseStatementOrDeclaration(Stmts, ACK_Any); |
| 947 | if (R.isUsable()) |
| 948 | Stmts.push_back(R.get()); |
| 949 | } |
| 950 | |
| 951 | // Did not consume any extra semi. |
| 952 | if (EndLoc.isInvalid()) |
| 953 | return false; |
| 954 | |
| 955 | Diag(StartLoc, diag::warn_null_statement) |
| 956 | << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); |
| 957 | return true; |
| 958 | } |
| 959 | |
Aaron Ballman | fb6deeb | 2019-01-04 16:58:14 +0000 | [diff] [blame] | 960 | bool Parser::isExprValueDiscarded() { |
| 961 | if (Actions.isCurCompoundStmtAStmtExpr()) { |
| 962 | // Look to see if the next two tokens close the statement expression; |
| 963 | // if so, this expression statement is the last statement in a |
| 964 | // statment expression. |
| 965 | return Tok.isNot(tok::r_brace) || NextToken().isNot(tok::r_paren); |
| 966 | } |
| 967 | return true; |
| 968 | } |
| 969 | |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 970 | /// ParseCompoundStatementBody - Parse a sequence of statements and invoke the |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 971 | /// ActOnCompoundStmt action. This expects the '{' to be the current token, and |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 972 | /// consume the '}' at the end of the block. It does not manipulate the scope |
| 973 | /// stack. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 974 | StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 976 | Tok.getLocation(), |
| 977 | "in compound statement ('{}')"); |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 978 | |
| 979 | // Record the state of the FP_CONTRACT pragma, restore on leaving the |
| 980 | // compound statement. |
| 981 | Sema::FPContractStateRAII SaveFPContractState(Actions); |
| 982 | |
Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 983 | InMessageExpressionRAIIObject InMessage(*this, false); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 984 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 985 | if (T.consumeOpen()) |
| 986 | return StmtError(); |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 987 | |
Richard Smith | 6eb9b9e | 2018-02-03 00:44:57 +0000 | [diff] [blame] | 988 | Sema::CompoundScopeRAII CompoundScope(Actions, isStmtExpr); |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 989 | |
Lang Hames | 2954cea | 2012-11-03 22:29:05 +0000 | [diff] [blame] | 990 | // Parse any pragmas at the beginning of the compound statement. |
| 991 | ParseCompoundStatementLeadingPragmas(); |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 992 | |
Lang Hames | 2954cea | 2012-11-03 22:29:05 +0000 | [diff] [blame] | 993 | StmtVector Stmts; |
Lang Hames | a930e71 | 2012-10-21 01:10:01 +0000 | [diff] [blame] | 994 | |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 995 | // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are |
| 996 | // only allowed at the start of a compound stmt regardless of the language. |
| 997 | while (Tok.is(tok::kw___label__)) { |
| 998 | SourceLocation LabelLoc = ConsumeToken(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 999 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1000 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 1001 | while (1) { |
| 1002 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1003 | Diag(Tok, diag::err_expected) << tok::identifier; |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 1004 | break; |
| 1005 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 1007 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1008 | SourceLocation IdLoc = ConsumeToken(); |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 1009 | DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1010 | |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1011 | if (!TryConsumeToken(tok::comma)) |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 1012 | break; |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 1013 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1014 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1015 | DeclSpec DS(AttrFactory); |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1016 | DeclGroupPtrTy Res = |
| 1017 | Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 1018 | StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1019 | |
Chris Lattner | 02f1b61 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1020 | ExpectAndConsumeSemi(diag::err_expected_semi_declaration); |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 1021 | if (R.isUsable()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1022 | Stmts.push_back(R.get()); |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 1023 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1024 | |
Richard Smith | 752ada8 | 2015-11-17 23:32:01 +0000 | [diff] [blame] | 1025 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
| 1026 | Tok.isNot(tok::eof)) { |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 1027 | if (Tok.is(tok::annot_pragma_unused)) { |
| 1028 | HandlePragmaUnused(); |
| 1029 | continue; |
| 1030 | } |
| 1031 | |
Roman Lebedev | 377748f | 2018-11-20 18:59:05 +0000 | [diff] [blame] | 1032 | if (ConsumeNullStmt(Stmts)) |
| 1033 | continue; |
| 1034 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1035 | StmtResult R; |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1036 | if (Tok.isNot(tok::kw___extension__)) { |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 1037 | R = ParseStatementOrDeclaration(Stmts, ACK_Any); |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 1038 | } else { |
| 1039 | // __extension__ can start declarations and it can also be a unary |
| 1040 | // operator for expressions. Consume multiple __extension__ markers here |
| 1041 | // until we can determine which is which. |
Eli Friedman | eb3a9b0 | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 1042 | // FIXME: This loses extension expressions in the AST! |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 1043 | SourceLocation ExtLoc = ConsumeToken(); |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1044 | while (Tok.is(tok::kw___extension__)) |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 1045 | ConsumeToken(); |
Chris Lattner | 1ff6e73 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 1046 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1047 | ParsedAttributesWithRange attrs(AttrFactory); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1048 | MaybeParseCXX11Attributes(attrs, nullptr, |
| 1049 | /*MightBeObjCMessageSend*/ true); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1050 | |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 1051 | // If this is the start of a declaration, parse it as such. |
Argyrios Kyrtzidis | 2c7137d | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 1052 | if (isDeclarationStatement()) { |
Eli Friedman | 15af3ee | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 1053 | // __extension__ silences extension warnings in the subdeclaration. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1054 | // FIXME: Save the __extension__ on the decl as a node somehow? |
Eli Friedman | 15af3ee | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 1055 | ExtensionRAIIObject O(Diags); |
| 1056 | |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1057 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1058 | DeclGroupPtrTy Res = |
| 1059 | ParseDeclaration(DeclaratorContext::BlockContext, DeclEnd, attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1060 | R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd); |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 1061 | } else { |
Eli Friedman | eb3a9b0 | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 1062 | // Otherwise this was a unary __extension__ marker. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1063 | ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc)); |
Chris Lattner | fdc0748 | 2008-03-13 06:32:11 +0000 | [diff] [blame] | 1064 | |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1065 | if (Res.isInvalid()) { |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 1066 | SkipUntil(tok::semi); |
| 1067 | continue; |
| 1068 | } |
Sebastian Redl | c2edafb | 2009-01-18 18:03:53 +0000 | [diff] [blame] | 1069 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1070 | // FIXME: Use attributes? |
Chris Lattner | 1ff6e73 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 1071 | // Eat the semicolon at the end of stmt and convert the expr into a |
| 1072 | // statement. |
Douglas Gregor | 45d6bdf | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 1073 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
Aaron Ballman | fb6deeb | 2019-01-04 16:58:14 +0000 | [diff] [blame] | 1074 | R = Actions.ActOnExprStmt(Res, isExprValueDiscarded()); |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1077 | |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1078 | if (R.isUsable()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1079 | Stmts.push_back(R.get()); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1080 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1081 | |
Argyrios Kyrtzidis | 6db8501 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 1082 | SourceLocation CloseLoc = Tok.getLocation(); |
| 1083 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 1084 | // We broke out of the while loop because we found a '}' or EOF. |
Nico Weber | a48b6c2 | 2012-12-30 23:36:56 +0000 | [diff] [blame] | 1085 | if (!T.consumeClose()) |
Argyrios Kyrtzidis | 6db8501 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 1086 | // Recover by creating a compound statement with what we parsed so far, |
| 1087 | // instead of dropping everything and returning StmtError(); |
Nico Weber | a48b6c2 | 2012-12-30 23:36:56 +0000 | [diff] [blame] | 1088 | CloseLoc = T.getCloseLocation(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1089 | |
Argyrios Kyrtzidis | 6db8501 | 2012-03-24 02:26:51 +0000 | [diff] [blame] | 1090 | return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1091 | Stmts, isStmtExpr); |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 1092 | } |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1093 | |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1094 | /// ParseParenExprOrCondition: |
| 1095 | /// [C ] '(' expression ')' |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1096 | /// [C++] '(' condition ')' |
| 1097 | /// [C++1z] '(' init-statement[opt] condition ')' |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1098 | /// |
| 1099 | /// This function parses and performs error recovery on the specified condition |
| 1100 | /// or expression (depending on whether we're in C++ or C mode). This function |
| 1101 | /// goes out of its way to recover well. It returns true if there was a parser |
| 1102 | /// error (the right paren couldn't be found), which indicates that the caller |
| 1103 | /// should try to recover harder. It returns false if the condition is |
| 1104 | /// successfully parsed. Note that a successful parse can still have semantic |
| 1105 | /// errors in the condition. |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1106 | bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt, |
| 1107 | Sema::ConditionResult &Cond, |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1108 | SourceLocation Loc, |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1109 | Sema::ConditionKind CK) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1110 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1111 | T.consumeOpen(); |
| 1112 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1113 | if (getLangOpts().CPlusPlus) |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1114 | Cond = ParseCXXCondition(InitStmt, Loc, CK); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1115 | else { |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1116 | ExprResult CondExpr = ParseExpression(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1117 | |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1118 | // If required, convert to a boolean value. |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1119 | if (CondExpr.isInvalid()) |
| 1120 | Cond = Sema::ConditionError(); |
| 1121 | else |
| 1122 | Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1123 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1124 | |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1125 | // If the parser was confused by the condition and we don't have a ')', try to |
| 1126 | // recover by skipping ahead to a semi and bailing out. If condexp is |
| 1127 | // semantically invalid but we have well formed code, keep going. |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1128 | if (Cond.isInvalid() && Tok.isNot(tok::r_paren)) { |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1129 | SkipUntil(tok::semi); |
| 1130 | // Skipping may have stopped if it found the containing ')'. If so, we can |
| 1131 | // continue parsing the if statement. |
| 1132 | if (Tok.isNot(tok::r_paren)) |
| 1133 | return true; |
| 1134 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1135 | |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1136 | // Otherwise the condition is valid or the rparen is present. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1137 | T.consumeClose(); |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | 70d4498 | 2012-04-28 16:24:20 +0000 | [diff] [blame] | 1139 | // Check for extraneous ')'s to catch things like "if (foo())) {". We know |
| 1140 | // that all callers are looking for a statement after the condition, so ")" |
| 1141 | // isn't valid. |
| 1142 | while (Tok.is(tok::r_paren)) { |
| 1143 | Diag(Tok, diag::err_extraneous_rparen_in_condition) |
| 1144 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1145 | ConsumeParen(); |
| 1146 | } |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1147 | |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1148 | return false; |
| 1149 | } |
| 1150 | |
| 1151 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1152 | /// ParseIfStatement |
| 1153 | /// if-statement: [C99 6.8.4.1] |
| 1154 | /// 'if' '(' expression ')' statement |
| 1155 | /// 'if' '(' expression ')' statement 'else' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1156 | /// [C++] 'if' '(' condition ')' statement |
| 1157 | /// [C++] 'if' '(' condition ')' statement 'else' statement |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1158 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1159 | StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1160 | assert(Tok.is(tok::kw_if) && "Not an if stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1161 | SourceLocation IfLoc = ConsumeToken(); // eat the 'if'. |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1162 | |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 1163 | bool IsConstexpr = false; |
| 1164 | if (Tok.is(tok::kw_constexpr)) { |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 1165 | Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 1166 | : diag::ext_constexpr_if); |
| 1167 | IsConstexpr = true; |
| 1168 | ConsumeToken(); |
| 1169 | } |
| 1170 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1171 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1172 | Diag(Tok, diag::err_expected_lparen_after) << "if"; |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1173 | SkipUntil(tok::semi); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1174 | return StmtError(); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1175 | } |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1176 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1177 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1178 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1179 | // C99 6.8.4p3 - In C99, the if statement is a block. This is not |
| 1180 | // the case for C90. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1181 | // |
| 1182 | // C++ 6.4p3: |
| 1183 | // A name introduced by a declaration in a condition is in scope from its |
| 1184 | // point of declaration until the end of the substatements controlled by the |
| 1185 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1186 | // C++ 3.3.2p4: |
| 1187 | // Names declared in the for-init-statement, and in the condition of if, |
| 1188 | // while, for, and switch statements are local to the if, while, for, or |
| 1189 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1190 | // |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1191 | ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX); |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1192 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1193 | // Parse the condition. |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1194 | StmtResult InitStmt; |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1195 | Sema::ConditionResult Cond; |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1196 | if (ParseParenExprOrCondition(&InitStmt, Cond, IfLoc, |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 1197 | IsConstexpr ? Sema::ConditionKind::ConstexprIf |
| 1198 | : Sema::ConditionKind::Boolean)) |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1199 | return StmtError(); |
Chris Lattner | bc2d77c | 2008-12-12 06:19:11 +0000 | [diff] [blame] | 1200 | |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 1201 | llvm::Optional<bool> ConstexprCondition; |
| 1202 | if (IsConstexpr) |
| 1203 | ConstexprCondition = Cond.getKnownValue(); |
| 1204 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1205 | // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1206 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1207 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1208 | // |
| 1209 | // C++ 6.4p1: |
| 1210 | // The substatement in a selection-statement (each substatement, in the else |
| 1211 | // form of the if statement) implicitly defines a local scope. |
| 1212 | // |
| 1213 | // For C++ we create a scope for the condition and a new scope for |
| 1214 | // substatements because: |
| 1215 | // -When the 'then' scope exits, we want the condition declaration to still be |
| 1216 | // active for the 'else' scope too. |
| 1217 | // -Sema will detect name clashes by considering declarations of a |
| 1218 | // 'ControlScope' as part of its direct subscope. |
| 1219 | // -If we wanted the condition and substatement to be in the same scope, we |
| 1220 | // would have to notify ParseStatement not to create a new scope. It's |
| 1221 | // simpler to let it create a new scope. |
| 1222 | // |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 1223 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1224 | |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1225 | // Read the 'then' stmt. |
| 1226 | SourceLocation ThenStmtLoc = Tok.getLocation(); |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1227 | |
| 1228 | SourceLocation InnerStatementTrailingElseLoc; |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 1229 | StmtResult ThenStmt; |
| 1230 | { |
| 1231 | EnterExpressionEvaluationContext PotentiallyDiscarded( |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1232 | Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr, |
Nicolas Lesser | b6d5c58 | 2018-07-12 18:45:41 +0000 | [diff] [blame] | 1233 | Sema::ExpressionEvaluationContextRecord::EK_Other, |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 1234 | /*ShouldEnter=*/ConstexprCondition && !*ConstexprCondition); |
| 1235 | ThenStmt = ParseStatement(&InnerStatementTrailingElseLoc); |
| 1236 | } |
Chris Lattner | ac4471c | 2007-05-28 05:38:24 +0000 | [diff] [blame] | 1237 | |
Chris Lattner | 37e54f4 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 1238 | // Pop the 'if' scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1239 | InnerScope.Exit(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1240 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1241 | // If it has an else, parse it. |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1242 | SourceLocation ElseLoc; |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1243 | SourceLocation ElseStmtLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1244 | StmtResult ElseStmt; |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1245 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1246 | if (Tok.is(tok::kw_else)) { |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1247 | if (TrailingElseLoc) |
| 1248 | *TrailingElseLoc = Tok.getLocation(); |
| 1249 | |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1250 | ElseLoc = ConsumeToken(); |
Chris Lattner | df74264 | 2010-04-12 06:12:50 +0000 | [diff] [blame] | 1251 | ElseStmtLoc = Tok.getLocation(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1253 | // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1254 | // there is no compound stmt. C90 does not have this clause. We only do |
| 1255 | // this if the body isn't a compound statement to avoid push/pop in common |
| 1256 | // cases. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1257 | // |
| 1258 | // C++ 6.4p1: |
| 1259 | // The substatement in a selection-statement (each substatement, in the else |
| 1260 | // form of the if statement) implicitly defines a local scope. |
| 1261 | // |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 1262 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, |
| 1263 | Tok.is(tok::l_brace)); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1264 | |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 1265 | EnterExpressionEvaluationContext PotentiallyDiscarded( |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1266 | Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr, |
Nicolas Lesser | b6d5c58 | 2018-07-12 18:45:41 +0000 | [diff] [blame] | 1267 | Sema::ExpressionEvaluationContextRecord::EK_Other, |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 1268 | /*ShouldEnter=*/ConstexprCondition && *ConstexprCondition); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1269 | ElseStmt = ParseStatement(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | 37e54f4 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 1271 | // Pop the 'else' scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1272 | InnerScope.Exit(); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 1273 | } else if (Tok.is(tok::code_completion)) { |
| 1274 | Actions.CodeCompleteAfterIf(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1275 | cutOffParsing(); |
| 1276 | return StmtError(); |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1277 | } else if (InnerStatementTrailingElseLoc.isValid()) { |
| 1278 | Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1279 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1280 | |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1281 | IfScope.Exit(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1282 | |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1283 | // If the then or else stmt is invalid and the other is valid (and present), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | // make turn the invalid one into a null stmt to avoid dropping the other |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1285 | // part. If both are invalid, return error. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1286 | if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) || |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1287 | (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) || |
| 1288 | (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) { |
Sebastian Redl | 511ed55 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1289 | // Both invalid, or one is invalid and other is non-present: return error. |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1290 | return StmtError(); |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1291 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1292 | |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1293 | // Now if either are invalid, replace with a ';'. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1294 | if (ThenStmt.isInvalid()) |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1295 | ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1296 | if (ElseStmt.isInvalid()) |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 1297 | ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1298 | |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1299 | return Actions.ActOnIfStmt(IfLoc, IsConstexpr, InitStmt.get(), Cond, |
| 1300 | ThenStmt.get(), ElseLoc, ElseStmt.get()); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1303 | /// ParseSwitchStatement |
| 1304 | /// switch-statement: |
| 1305 | /// 'switch' '(' expression ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1306 | /// [C++] 'switch' '(' condition ')' statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1307 | StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1308 | assert(Tok.is(tok::kw_switch) && "Not a switch stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1309 | SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'. |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1310 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1311 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1312 | Diag(Tok, diag::err_expected_lparen_after) << "switch"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1313 | SkipUntil(tok::semi); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1314 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1315 | } |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1316 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1317 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1318 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1319 | // C99 6.8.4p3 - In C99, the switch statement is a block. This is |
| 1320 | // not the case for C90. Start the switch scope. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1321 | // |
| 1322 | // C++ 6.4p3: |
| 1323 | // A name introduced by a declaration in a condition is in scope from its |
| 1324 | // point of declaration until the end of the substatements controlled by the |
| 1325 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1326 | // C++ 3.3.2p4: |
| 1327 | // Names declared in the for-init-statement, and in the condition of if, |
| 1328 | // while, for, and switch statements are local to the if, while, for, or |
| 1329 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1330 | // |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1331 | unsigned ScopeFlags = Scope::SwitchScope; |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1332 | if (C99orCXX) |
| 1333 | ScopeFlags |= Scope::DeclScope | Scope::ControlScope; |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1334 | ParseScope SwitchScope(this, ScopeFlags); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1336 | // Parse the condition. |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1337 | StmtResult InitStmt; |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1338 | Sema::ConditionResult Cond; |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1339 | if (ParseParenExprOrCondition(&InitStmt, Cond, SwitchLoc, |
| 1340 | Sema::ConditionKind::Switch)) |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1341 | return StmtError(); |
Eli Friedman | 44842d1 | 2008-12-17 22:19:57 +0000 | [diff] [blame] | 1342 | |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1343 | StmtResult Switch = |
| 1344 | Actions.ActOnStartOfSwitchStmt(SwitchLoc, InitStmt.get(), Cond); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1345 | |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1346 | if (Switch.isInvalid()) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1347 | // Skip the switch body. |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1348 | // FIXME: This is not optimal recovery, but parsing the body is more |
| 1349 | // dangerous due to the presence of case and default statements, which |
| 1350 | // will have no place to connect back with the switch. |
Douglas Gregor | 4abc32d | 2010-05-20 23:20:59 +0000 | [diff] [blame] | 1351 | if (Tok.is(tok::l_brace)) { |
| 1352 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1353 | SkipUntil(tok::r_brace); |
Douglas Gregor | 4abc32d | 2010-05-20 23:20:59 +0000 | [diff] [blame] | 1354 | } else |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1355 | SkipUntil(tok::semi); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1356 | return Switch; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1357 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1359 | // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1360 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1361 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1362 | // |
| 1363 | // C++ 6.4p1: |
| 1364 | // The substatement in a selection-statement (each substatement, in the else |
| 1365 | // form of the if statement) implicitly defines a local scope. |
| 1366 | // |
| 1367 | // See comments in ParseIfStatement for why we create a scope for the |
| 1368 | // condition and a new scope for substatement in C++. |
| 1369 | // |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1370 | getCurScope()->AddFlags(Scope::BreakScope); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 1371 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1372 | |
Hans Wennborg | 852c346 | 2014-06-17 00:09:05 +0000 | [diff] [blame] | 1373 | // We have incremented the mangling number for the SwitchScope and the |
| 1374 | // InnerScope, which is one too many. |
| 1375 | if (C99orCXX) |
David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 1376 | getCurScope()->decrementMSManglingNumber(); |
Hans Wennborg | 852c346 | 2014-06-17 00:09:05 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1378 | // Read the body statement. |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1379 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1380 | |
Chris Lattner | 8fd2d01 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 1381 | // Pop the scopes. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1382 | InnerScope.Exit(); |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1383 | SwitchScope.Exit(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1384 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1385 | return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | /// ParseWhileStatement |
| 1389 | /// while-statement: [C99 6.8.5.1] |
| 1390 | /// 'while' '(' expression ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1391 | /// [C++] 'while' '(' condition ')' statement |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1392 | StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1393 | assert(Tok.is(tok::kw_while) && "Not a while stmt!"); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1394 | SourceLocation WhileLoc = Tok.getLocation(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1395 | ConsumeToken(); // eat the 'while'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1396 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1397 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1398 | Diag(Tok, diag::err_expected_lparen_after) << "while"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1399 | SkipUntil(tok::semi); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1400 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1401 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1402 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1403 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1404 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1405 | // C99 6.8.5p5 - In C99, the while statement is a block. This is not |
| 1406 | // the case for C90. Start the loop scope. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1407 | // |
| 1408 | // C++ 6.4p3: |
| 1409 | // A name introduced by a declaration in a condition is in scope from its |
| 1410 | // point of declaration until the end of the substatements controlled by the |
| 1411 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1412 | // C++ 3.3.2p4: |
| 1413 | // Names declared in the for-init-statement, and in the condition of if, |
| 1414 | // while, for, and switch statements are local to the if, while, for, or |
| 1415 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1416 | // |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1417 | unsigned ScopeFlags; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1418 | if (C99orCXX) |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1419 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | |
| 1420 | Scope::DeclScope | Scope::ControlScope; |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1421 | else |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1422 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
| 1423 | ParseScope WhileScope(this, ScopeFlags); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1424 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1425 | // Parse the condition. |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1426 | Sema::ConditionResult Cond; |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1427 | if (ParseParenExprOrCondition(nullptr, Cond, WhileLoc, |
| 1428 | Sema::ConditionKind::Boolean)) |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 1429 | return StmtError(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1430 | |
Justin Bogner | e4ebb6c | 2013-12-03 07:36:55 +0000 | [diff] [blame] | 1431 | // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1432 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1433 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1434 | // |
| 1435 | // C++ 6.5p2: |
| 1436 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1437 | // which is entered and exited each time through the loop. |
| 1438 | // |
| 1439 | // See comments in ParseIfStatement for why we create a scope for the |
| 1440 | // condition and a new scope for substatement in C++. |
| 1441 | // |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 1442 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1443 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1444 | // Read the body statement. |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1445 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1446 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1447 | // Pop the body scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1448 | InnerScope.Exit(); |
| 1449 | WhileScope.Exit(); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1450 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1451 | if (Cond.isInvalid() || Body.isInvalid()) |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1452 | return StmtError(); |
| 1453 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1454 | return Actions.ActOnWhileStmt(WhileLoc, Cond, Body.get()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | /// ParseDoStatement |
| 1458 | /// do-statement: [C99 6.8.5.2] |
| 1459 | /// 'do' statement 'while' '(' expression ')' ';' |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1460 | /// Note: this lets the caller parse the end ';'. |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1461 | StmtResult Parser::ParseDoStatement() { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1462 | assert(Tok.is(tok::kw_do) && "Not a do stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1463 | SourceLocation DoLoc = ConsumeToken(); // eat the 'do'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1465 | // C99 6.8.5p5 - In C99, the do statement is a block. This is not |
| 1466 | // the case for C90. Start the loop scope. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1467 | unsigned ScopeFlags; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1468 | if (getLangOpts().C99) |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1469 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope; |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1470 | else |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1471 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1472 | |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1473 | ParseScope DoScope(this, ScopeFlags); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1474 | |
Justin Bogner | e4ebb6c | 2013-12-03 07:36:55 +0000 | [diff] [blame] | 1475 | // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1476 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1477 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Argyrios Kyrtzidis | fea3801 | 2008-09-11 04:46:46 +0000 | [diff] [blame] | 1478 | // |
| 1479 | // C++ 6.5p2: |
| 1480 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1481 | // which is entered and exited each time through the loop. |
| 1482 | // |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 1483 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
| 1484 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1486 | // Read the body statement. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1487 | StmtResult Body(ParseStatement()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1489 | // Pop the body scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1490 | InnerScope.Exit(); |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1491 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1492 | if (Tok.isNot(tok::kw_while)) { |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1493 | if (!Body.isInvalid()) { |
Chris Lattner | 0046de1 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 1494 | Diag(Tok, diag::err_expected_while); |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1495 | Diag(DoLoc, diag::note_matching) << "'do'"; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1496 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 0046de1 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 1497 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1498 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1499 | } |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1500 | SourceLocation WhileLoc = ConsumeToken(); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1502 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1503 | Diag(Tok, diag::err_expected_lparen_after) << "do/while"; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1504 | SkipUntil(tok::semi, StopBeforeMatch); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1505 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1506 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1507 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 1508 | // Parse the parenthesized expression. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1509 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1510 | T.consumeOpen(); |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1511 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 1512 | // A do-while expression is not a condition, so can't have attributes. |
| 1513 | DiagnoseAndSkipCXX11Attributes(); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1514 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1515 | ExprResult Cond = ParseExpression(); |
Alex Lorenz | c38ba66 | 2017-10-30 22:55:11 +0000 | [diff] [blame] | 1516 | // Correct the typos in condition before closing the scope. |
| 1517 | if (Cond.isUsable()) |
| 1518 | Cond = Actions.CorrectDelayedTyposInExpr(Cond); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1519 | T.consumeClose(); |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1520 | DoScope.Exit(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1521 | |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1522 | if (Cond.isInvalid() || Body.isInvalid()) |
| 1523 | return StmtError(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1524 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1525 | return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(), |
| 1526 | Cond.get(), T.getCloseLocation()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 1529 | bool Parser::isForRangeIdentifier() { |
| 1530 | assert(Tok.is(tok::identifier)); |
| 1531 | |
| 1532 | const Token &Next = NextToken(); |
| 1533 | if (Next.is(tok::colon)) |
| 1534 | return true; |
| 1535 | |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1536 | if (Next.isOneOf(tok::l_square, tok::kw_alignas)) { |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 1537 | TentativeParsingAction PA(*this); |
| 1538 | ConsumeToken(); |
| 1539 | SkipCXX11Attributes(); |
| 1540 | bool Result = Tok.is(tok::colon); |
| 1541 | PA.Revert(); |
| 1542 | return Result; |
| 1543 | } |
| 1544 | |
| 1545 | return false; |
| 1546 | } |
| 1547 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1548 | /// ParseForStatement |
| 1549 | /// for-statement: [C99 6.8.5.3] |
| 1550 | /// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement |
| 1551 | /// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1552 | /// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')' |
| 1553 | /// [C++] statement |
Richard Smith | 0e304ea | 2015-10-22 04:46:14 +0000 | [diff] [blame] | 1554 | /// [C++0x] 'for' |
| 1555 | /// 'co_await'[opt] [Coroutines] |
| 1556 | /// '(' for-range-declaration ':' for-range-initializer ')' |
| 1557 | /// statement |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1558 | /// [OBJC2] 'for' '(' declaration 'in' expr ')' statement |
| 1559 | /// [OBJC2] 'for' '(' expr 'in' expr ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1560 | /// |
| 1561 | /// [C++] for-init-statement: |
| 1562 | /// [C++] expression-statement |
| 1563 | /// [C++] simple-declaration |
| 1564 | /// |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1565 | /// [C++0x] for-range-declaration: |
| 1566 | /// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1567 | /// [C++0x] for-range-initializer: |
| 1568 | /// [C++0x] expression |
| 1569 | /// [C++0x] braced-init-list [TODO] |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1570 | StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1571 | assert(Tok.is(tok::kw_for) && "Not a for stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1572 | SourceLocation ForLoc = ConsumeToken(); // eat the 'for'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1573 | |
Richard Smith | 0e304ea | 2015-10-22 04:46:14 +0000 | [diff] [blame] | 1574 | SourceLocation CoawaitLoc; |
| 1575 | if (Tok.is(tok::kw_co_await)) |
| 1576 | CoawaitLoc = ConsumeToken(); |
| 1577 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1578 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1579 | Diag(Tok, diag::err_expected_lparen_after) << "for"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1580 | SkipUntil(tok::semi); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1581 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1582 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1583 | |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1584 | bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus || |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 1585 | getLangOpts().ObjC; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1586 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 1587 | // C99 6.8.5p5 - In C99, the for statement is a block. This is not |
| 1588 | // the case for C90. Start the loop scope. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1589 | // |
| 1590 | // C++ 6.4p3: |
| 1591 | // A name introduced by a declaration in a condition is in scope from its |
| 1592 | // point of declaration until the end of the substatements controlled by the |
| 1593 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 1594 | // C++ 3.3.2p4: |
| 1595 | // Names declared in the for-init-statement, and in the condition of if, |
| 1596 | // while, for, and switch statements are local to the if, while, for, or |
| 1597 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1598 | // C++ 6.5.3p1: |
| 1599 | // Names declared in the for-init-statement are in the same declarative-region |
| 1600 | // as those declared in the condition. |
| 1601 | // |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1602 | unsigned ScopeFlags = 0; |
Chris Lattner | 934074c | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 1603 | if (C99orCXXorObjC) |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1604 | ScopeFlags = Scope::DeclScope | Scope::ControlScope; |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1605 | |
| 1606 | ParseScope ForScope(this, ScopeFlags); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1607 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1608 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1609 | T.consumeOpen(); |
| 1610 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1611 | ExprResult Value; |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1612 | |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1613 | bool ForEach = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1614 | StmtResult FirstPart; |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1615 | Sema::ConditionResult SecondPart; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1616 | ExprResult Collection; |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1617 | ForRangeInfo ForRangeInfo; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1618 | FullExprArg ThirdPart(Actions); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1619 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1620 | if (Tok.is(tok::code_completion)) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1621 | Actions.CodeCompleteOrdinaryName(getCurScope(), |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1622 | C99orCXXorObjC? Sema::PCC_ForInit |
| 1623 | : Sema::PCC_Expression); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1624 | cutOffParsing(); |
| 1625 | return StmtError(); |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1626 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1627 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1628 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1629 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1630 | |
Roman Lebedev | 377748f | 2018-11-20 18:59:05 +0000 | [diff] [blame] | 1631 | SourceLocation EmptyInitStmtSemiLoc; |
| 1632 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1633 | // Parse the first part of the for specifier. |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1634 | if (Tok.is(tok::semi)) { // for (; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1635 | ProhibitAttributes(attrs); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1636 | // no first part, eat the ';'. |
Roman Lebedev | 377748f | 2018-11-20 18:59:05 +0000 | [diff] [blame] | 1637 | SourceLocation SemiLoc = Tok.getLocation(); |
| 1638 | if (!Tok.hasLeadingEmptyMacro() && !SemiLoc.isMacroID()) |
| 1639 | EmptyInitStmtSemiLoc = SemiLoc; |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1640 | ConsumeToken(); |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 1641 | } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) && |
| 1642 | isForRangeIdentifier()) { |
| 1643 | ProhibitAttributes(attrs); |
| 1644 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
| 1645 | SourceLocation Loc = ConsumeToken(); |
| 1646 | MaybeParseCXX11Attributes(attrs); |
| 1647 | |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1648 | ForRangeInfo.ColonLoc = ConsumeToken(); |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 1649 | if (Tok.is(tok::l_brace)) |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1650 | ForRangeInfo.RangeExpr = ParseBraceInitializer(); |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 1651 | else |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1652 | ForRangeInfo.RangeExpr = ParseExpression(); |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 1653 | |
Richard Smith | 83d3f15 | 2014-11-27 01:54:27 +0000 | [diff] [blame] | 1654 | Diag(Loc, diag::err_for_range_identifier) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 1655 | << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus17) |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 1656 | ? FixItHint::CreateInsertion(Loc, "auto &&") |
| 1657 | : FixItHint()); |
| 1658 | |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1659 | ForRangeInfo.LoopVar = Actions.ActOnCXXForRangeIdentifier( |
| 1660 | getCurScope(), Loc, Name, attrs, attrs.Range.getEnd()); |
Eli Friedman | 0ffc31c | 2011-12-20 01:50:37 +0000 | [diff] [blame] | 1661 | } else if (isForInitDeclaration()) { // for (int X = 4; |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 1662 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
| 1663 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1664 | // Parse declaration, which eats the ';'. |
George Burgess IV | 4d45645 | 2018-06-28 21:36:00 +0000 | [diff] [blame] | 1665 | if (!C99orCXXorObjC) { // Use of C99-style for loops in C90 mode? |
Chris Lattner | ab180365 | 2006-08-10 05:22:36 +0000 | [diff] [blame] | 1666 | Diag(Tok, diag::ext_c99_variable_decl_in_for_loop); |
George Burgess IV | 4d45645 | 2018-06-28 21:36:00 +0000 | [diff] [blame] | 1667 | Diag(Tok, diag::warn_gcc_variable_decl_in_for_loop); |
| 1668 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1669 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1670 | // In C++0x, "for (T NS:a" might not be a typo for :: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1671 | bool MightBeForRangeStmt = getLangOpts().CPlusPlus; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1672 | ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt); |
| 1673 | |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1674 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 1675 | DeclGroupPtrTy DG = ParseSimpleDeclaration( |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1676 | DeclaratorContext::ForContext, DeclEnd, attrs, false, |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1677 | MightBeForRangeStmt ? &ForRangeInfo : nullptr); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1678 | FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation()); |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1679 | if (ForRangeInfo.ParsedForRangeDecl()) { |
| 1680 | Diag(ForRangeInfo.ColonLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1681 | diag::warn_cxx98_compat_for_range : diag::ext_for_range); |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1682 | ForRangeInfo.LoopVar = FirstPart; |
| 1683 | FirstPart = StmtResult(); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1684 | } else if (Tok.is(tok::semi)) { // for (int x = 4; |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1685 | ConsumeToken(); |
| 1686 | } else if ((ForEach = isTokIdentifier_in())) { |
Fariborz Jahanian | e774fa6 | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 1687 | Actions.ActOnForEachDeclStmt(DG); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | // ObjC: for (id x in expr) |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1689 | ConsumeToken(); // consume 'in' |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1690 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1691 | if (Tok.is(tok::code_completion)) { |
| 1692 | Actions.CodeCompleteObjCForCollection(getCurScope(), DG); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1693 | cutOffParsing(); |
| 1694 | return StmtError(); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1695 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1696 | Collection = ParseExpression(); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1697 | } else { |
| 1698 | Diag(Tok, diag::err_expected_semi_for); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1699 | } |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1700 | } else { |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1701 | ProhibitAttributes(attrs); |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 1702 | Value = Actions.CorrectDelayedTyposInExpr(ParseExpression()); |
Chris Lattner | 71e23ce | 2006-11-04 20:18:38 +0000 | [diff] [blame] | 1703 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1704 | ForEach = isTokIdentifier_in(); |
| 1705 | |
Chris Lattner | cd68f64 | 2007-06-27 01:06:29 +0000 | [diff] [blame] | 1706 | // Turn the expression into a stmt. |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1707 | if (!Value.isInvalid()) { |
| 1708 | if (ForEach) |
| 1709 | FirstPart = Actions.ActOnForEachLValueExpr(Value.get()); |
Aaron Ballman | fb6deeb | 2019-01-04 16:58:14 +0000 | [diff] [blame] | 1710 | else { |
| 1711 | // We already know this is not an init-statement within a for loop, so |
| 1712 | // if we are parsing a C++11 range-based for loop, we should treat this |
| 1713 | // expression statement as being a discarded value expression because |
| 1714 | // we will err below. This way we do not warn on an unused expression |
| 1715 | // that was an error in the first place, like with: for (expr : expr); |
| 1716 | bool IsRangeBasedFor = |
| 1717 | getLangOpts().CPlusPlus11 && !ForEach && Tok.is(tok::colon); |
| 1718 | FirstPart = Actions.ActOnExprStmt(Value, !IsRangeBasedFor); |
| 1719 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1720 | } |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1721 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1722 | if (Tok.is(tok::semi)) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1723 | ConsumeToken(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1724 | } else if (ForEach) { |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1725 | ConsumeToken(); // consume 'in' |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1726 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1727 | if (Tok.is(tok::code_completion)) { |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1728 | Actions.CodeCompleteObjCForCollection(getCurScope(), nullptr); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1729 | cutOffParsing(); |
| 1730 | return StmtError(); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1731 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1732 | Collection = ParseExpression(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1733 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) { |
Richard Smith | 4f848f1 | 2011-12-20 22:56:20 +0000 | [diff] [blame] | 1734 | // User tried to write the reasonable, but ill-formed, for-range-statement |
| 1735 | // for (expr : expr) { ... } |
| 1736 | Diag(Tok, diag::err_for_range_expected_decl) |
| 1737 | << FirstPart.get()->getSourceRange(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1738 | SkipUntil(tok::r_paren, StopBeforeMatch); |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1739 | SecondPart = Sema::ConditionError(); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1740 | } else { |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1741 | if (!Value.isInvalid()) { |
| 1742 | Diag(Tok, diag::err_expected_semi_for); |
| 1743 | } else { |
| 1744 | // Skip until semicolon or rparen, don't consume it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1745 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1746 | if (Tok.is(tok::semi)) |
| 1747 | ConsumeToken(); |
| 1748 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1749 | } |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1750 | } |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1751 | |
| 1752 | // Parse the second part of the for specifier. |
| 1753 | getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope); |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1754 | if (!ForEach && !ForRangeInfo.ParsedForRangeDecl() && |
| 1755 | !SecondPart.isInvalid()) { |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1756 | // Parse the second part of the for specifier. |
| 1757 | if (Tok.is(tok::semi)) { // for (...;; |
| 1758 | // no second part. |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1759 | } else if (Tok.is(tok::r_paren)) { |
| 1760 | // missing both semicolons. |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1761 | } else { |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1762 | if (getLangOpts().CPlusPlus) { |
| 1763 | // C++2a: We've parsed an init-statement; we might have a |
| 1764 | // for-range-declaration next. |
| 1765 | bool MightBeForRangeStmt = !ForRangeInfo.ParsedForRangeDecl(); |
| 1766 | ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt); |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1767 | SecondPart = |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1768 | ParseCXXCondition(nullptr, ForLoc, Sema::ConditionKind::Boolean, |
| 1769 | MightBeForRangeStmt ? &ForRangeInfo : nullptr); |
| 1770 | |
| 1771 | if (ForRangeInfo.ParsedForRangeDecl()) { |
| 1772 | Diag(FirstPart.get() ? FirstPart.get()->getBeginLoc() |
| 1773 | : ForRangeInfo.ColonLoc, |
| 1774 | getLangOpts().CPlusPlus2a |
| 1775 | ? diag::warn_cxx17_compat_for_range_init_stmt |
| 1776 | : diag::ext_for_range_init_stmt) |
| 1777 | << (FirstPart.get() ? FirstPart.get()->getSourceRange() |
| 1778 | : SourceRange()); |
Roman Lebedev | 377748f | 2018-11-20 18:59:05 +0000 | [diff] [blame] | 1779 | if (EmptyInitStmtSemiLoc.isValid()) { |
| 1780 | Diag(EmptyInitStmtSemiLoc, diag::warn_empty_init_statement) |
| 1781 | << /*for-loop*/ 2 |
| 1782 | << FixItHint::CreateRemoval(EmptyInitStmtSemiLoc); |
| 1783 | } |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1784 | } |
| 1785 | } else { |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1786 | ExprResult SecondExpr = ParseExpression(); |
| 1787 | if (SecondExpr.isInvalid()) |
| 1788 | SecondPart = Sema::ConditionError(); |
| 1789 | else |
| 1790 | SecondPart = |
| 1791 | Actions.ActOnCondition(getCurScope(), ForLoc, SecondExpr.get(), |
| 1792 | Sema::ConditionKind::Boolean); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1793 | } |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1794 | } |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1795 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1796 | |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1797 | // Parse the third part of the for statement. |
| 1798 | if (!ForEach && !ForRangeInfo.ParsedForRangeDecl()) { |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1799 | if (Tok.isNot(tok::semi)) { |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1800 | if (!SecondPart.isInvalid()) |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1801 | Diag(Tok, diag::err_expected_semi_for); |
| 1802 | else |
| 1803 | // Skip until semicolon or rparen, don't consume it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1804 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1807 | if (Tok.is(tok::semi)) { |
| 1808 | ConsumeToken(); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1809 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1810 | |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1811 | if (Tok.isNot(tok::r_paren)) { // for (...;...;) |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1812 | ExprResult Third = ParseExpression(); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 1813 | // FIXME: The C++11 standard doesn't actually say that this is a |
| 1814 | // discarded-value expression, but it clearly should be. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1815 | ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get()); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1816 | } |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1817 | } |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 1818 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1819 | T.consumeClose(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1820 | |
Richard Smith | 0e304ea | 2015-10-22 04:46:14 +0000 | [diff] [blame] | 1821 | // C++ Coroutines [stmt.iter]: |
| 1822 | // 'co_await' can only be used for a range-based for statement. |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1823 | if (CoawaitLoc.isValid() && !ForRangeInfo.ParsedForRangeDecl()) { |
Richard Smith | 0e304ea | 2015-10-22 04:46:14 +0000 | [diff] [blame] | 1824 | Diag(CoawaitLoc, diag::err_for_co_await_not_range_for); |
| 1825 | CoawaitLoc = SourceLocation(); |
| 1826 | } |
| 1827 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1828 | // We need to perform most of the semantic analysis for a C++0x for-range |
| 1829 | // statememt before parsing the body, in order to be able to deduce the type |
| 1830 | // of an auto-typed loop variable. |
| 1831 | StmtResult ForRangeStmt; |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1832 | StmtResult ForEachStmt; |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 1833 | |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1834 | if (ForRangeInfo.ParsedForRangeDecl()) { |
Denis Zobnin | 7d6b924 | 2016-02-02 17:33:09 +0000 | [diff] [blame] | 1835 | ExprResult CorrectedRange = |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1836 | Actions.CorrectDelayedTyposInExpr(ForRangeInfo.RangeExpr.get()); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1837 | ForRangeStmt = Actions.ActOnCXXForRangeStmt( |
| 1838 | getCurScope(), ForLoc, CoawaitLoc, FirstPart.get(), |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1839 | ForRangeInfo.LoopVar.get(), ForRangeInfo.ColonLoc, CorrectedRange.get(), |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1840 | T.getCloseLocation(), Sema::BFRK_Build); |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1841 | |
| 1842 | // Similarly, we need to do the semantic analysis for a for-range |
| 1843 | // statement immediately in order to close over temporaries correctly. |
| 1844 | } else if (ForEach) { |
Sam Panzer | 2c4ca0f | 2012-08-16 21:47:25 +0000 | [diff] [blame] | 1845 | ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1846 | FirstPart.get(), |
| 1847 | Collection.get(), |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1848 | T.getCloseLocation()); |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 1849 | } else { |
| 1850 | // In OpenMP loop region loop control variable must be captured and be |
| 1851 | // private. Perform analysis of first part (if any). |
| 1852 | if (getLangOpts().OpenMP && FirstPart.isUsable()) { |
| 1853 | Actions.ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get()); |
| 1854 | } |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Justin Bogner | e4ebb6c | 2013-12-03 07:36:55 +0000 | [diff] [blame] | 1857 | // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if |
Chris Lattner | 8f44d20 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 1858 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1859 | // if the body isn't a compound statement to avoid push/pop in common cases. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 1860 | // |
| 1861 | // C++ 6.5p2: |
| 1862 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1863 | // which is entered and exited each time through the loop. |
| 1864 | // |
| 1865 | // See comments in ParseIfStatement for why we create a scope for |
| 1866 | // for-init-statement/condition and a new scope for substatement in C++. |
| 1867 | // |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 1868 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC, |
| 1869 | Tok.is(tok::l_brace)); |
| 1870 | |
| 1871 | // The body of the for loop has the same local mangling number as the |
| 1872 | // for-init-statement. |
| 1873 | // It will only be incremented if the body contains other things that would |
| 1874 | // normally increment the mangling number (like a compound statement). |
| 1875 | if (C99orCXXorObjC) |
David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 1876 | getCurScope()->decrementMSManglingNumber(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1877 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1878 | // Read the body statement. |
Nico Weber | 3cef108 | 2011-12-22 23:26:17 +0000 | [diff] [blame] | 1879 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1881 | // Pop the body scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1882 | InnerScope.Exit(); |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1883 | |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1884 | // Leave the for-scope. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1885 | ForScope.Exit(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1886 | |
| 1887 | if (Body.isInvalid()) |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1888 | return StmtError(); |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1889 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1890 | if (ForEach) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1891 | return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(), |
| 1892 | Body.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1893 | |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1894 | if (ForRangeInfo.ParsedForRangeDecl()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1895 | return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1896 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1897 | return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(), |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1898 | SecondPart, ThirdPart, T.getCloseLocation(), |
| 1899 | Body.get()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1900 | } |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1901 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1902 | /// ParseGotoStatement |
| 1903 | /// jump-statement: |
| 1904 | /// 'goto' identifier ';' |
| 1905 | /// [GNU] 'goto' '*' expression ';' |
| 1906 | /// |
| 1907 | /// Note: this lets the caller parse the end ';'. |
| 1908 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1909 | StmtResult Parser::ParseGotoStatement() { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1910 | assert(Tok.is(tok::kw_goto) && "Not a goto stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1911 | SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1912 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1913 | StmtResult Res; |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1914 | if (Tok.is(tok::identifier)) { |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1915 | LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(), |
| 1916 | Tok.getLocation()); |
| 1917 | Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1918 | ConsumeToken(); |
Eli Friedman | 5d72d41 | 2009-04-28 00:51:18 +0000 | [diff] [blame] | 1919 | } else if (Tok.is(tok::star)) { |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1920 | // GNU indirect goto extension. |
| 1921 | Diag(Tok, diag::ext_gnu_indirect_goto); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1922 | SourceLocation StarLoc = ConsumeToken(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1923 | ExprResult R(ParseExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1924 | if (R.isInvalid()) { // Skip to the semicolon, but don't consume it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1925 | SkipUntil(tok::semi, StopBeforeMatch); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1926 | return StmtError(); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1927 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1928 | Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get()); |
Chris Lattner | e34b2c2 | 2007-07-22 04:13:33 +0000 | [diff] [blame] | 1929 | } else { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1930 | Diag(Tok, diag::err_expected) << tok::identifier; |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1931 | return StmtError(); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1932 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1933 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1934 | return Res; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1937 | /// ParseContinueStatement |
| 1938 | /// jump-statement: |
| 1939 | /// 'continue' ';' |
| 1940 | /// |
| 1941 | /// Note: this lets the caller parse the end ';'. |
| 1942 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1943 | StmtResult Parser::ParseContinueStatement() { |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1944 | SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1945 | return Actions.ActOnContinueStmt(ContinueLoc, getCurScope()); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | /// ParseBreakStatement |
| 1949 | /// jump-statement: |
| 1950 | /// 'break' ';' |
| 1951 | /// |
| 1952 | /// Note: this lets the caller parse the end ';'. |
| 1953 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1954 | StmtResult Parser::ParseBreakStatement() { |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1955 | SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1956 | return Actions.ActOnBreakStmt(BreakLoc, getCurScope()); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1959 | /// ParseReturnStatement |
| 1960 | /// jump-statement: |
| 1961 | /// 'return' expression[opt] ';' |
Richard Smith | 0e304ea | 2015-10-22 04:46:14 +0000 | [diff] [blame] | 1962 | /// 'return' braced-init-list ';' |
| 1963 | /// 'co_return' expression[opt] ';' |
| 1964 | /// 'co_return' braced-init-list ';' |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1965 | StmtResult Parser::ParseReturnStatement() { |
Richard Smith | 0e304ea | 2015-10-22 04:46:14 +0000 | [diff] [blame] | 1966 | assert((Tok.is(tok::kw_return) || Tok.is(tok::kw_co_return)) && |
| 1967 | "Not a return stmt!"); |
| 1968 | bool IsCoreturn = Tok.is(tok::kw_co_return); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1969 | SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1970 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1971 | ExprResult R; |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1972 | if (Tok.isNot(tok::semi)) { |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame^] | 1973 | if (!IsCoreturn) |
| 1974 | PreferredType.enterReturn(Actions, Tok.getLocation()); |
Richard Smith | 0e304ea | 2015-10-22 04:46:14 +0000 | [diff] [blame] | 1975 | // FIXME: Code completion for co_return. |
| 1976 | if (Tok.is(tok::code_completion) && !IsCoreturn) { |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame^] | 1977 | Actions.CodeCompleteExpression(getCurScope(), |
| 1978 | PreferredType.get(Tok.getLocation())); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1979 | cutOffParsing(); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1980 | return StmtError(); |
| 1981 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1982 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1983 | if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) { |
Douglas Gregor | e9e27d9 | 2011-03-11 23:10:44 +0000 | [diff] [blame] | 1984 | R = ParseInitializer(); |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1985 | if (R.isUsable()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1986 | Diag(R.get()->getBeginLoc(), |
| 1987 | getLangOpts().CPlusPlus11 |
| 1988 | ? diag::warn_cxx98_compat_generalized_initializer_lists |
| 1989 | : diag::ext_generalized_initializer_lists) |
| 1990 | << R.get()->getSourceRange(); |
Douglas Gregor | e9e27d9 | 2011-03-11 23:10:44 +0000 | [diff] [blame] | 1991 | } else |
Nico Weber | 3ce01c3 | 2015-01-04 08:07:54 +0000 | [diff] [blame] | 1992 | R = ParseExpression(); |
Serge Pavlov | f79bd5c | 2013-12-04 03:51:59 +0000 | [diff] [blame] | 1993 | if (R.isInvalid()) { |
| 1994 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1995 | return StmtError(); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1996 | } |
Chris Lattner | a0927ce | 2006-08-12 16:59:03 +0000 | [diff] [blame] | 1997 | } |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 1998 | if (IsCoreturn) |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1999 | return Actions.ActOnCoreturnStmt(getCurScope(), ReturnLoc, R.get()); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2000 | return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope()); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 2001 | } |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 2002 | |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 2003 | StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts, |
Jonathan Roelofs | ce1db6d | 2017-03-14 17:29:33 +0000 | [diff] [blame] | 2004 | AllowedConstructsKind Allowed, |
Aaron Ballman | b06b15a | 2014-06-06 12:40:24 +0000 | [diff] [blame] | 2005 | SourceLocation *TrailingElseLoc, |
| 2006 | ParsedAttributesWithRange &Attrs) { |
| 2007 | // Create temporary attribute list. |
| 2008 | ParsedAttributesWithRange TempAttrs(AttrFactory); |
| 2009 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2010 | // Get loop hints and consume annotated token. |
Aaron Ballman | b06b15a | 2014-06-06 12:40:24 +0000 | [diff] [blame] | 2011 | while (Tok.is(tok::annot_pragma_loop_hint)) { |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2012 | LoopHint Hint; |
| 2013 | if (!HandlePragmaLoopHint(Hint)) |
| 2014 | continue; |
Aaron Ballman | b06b15a | 2014-06-06 12:40:24 +0000 | [diff] [blame] | 2015 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 2016 | ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc, |
Aaron Ballman | b06b15a | 2014-06-06 12:40:24 +0000 | [diff] [blame] | 2017 | ArgsUnion(Hint.ValueExpr)}; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 2018 | TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr, |
| 2019 | Hint.PragmaNameLoc->Loc, ArgHints, 4, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2020 | ParsedAttr::AS_Pragma); |
Aaron Ballman | b06b15a | 2014-06-06 12:40:24 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | // Get the next statement. |
| 2024 | MaybeParseCXX11Attributes(Attrs); |
| 2025 | |
| 2026 | StmtResult S = ParseStatementOrDeclarationAfterAttributes( |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 2027 | Stmts, Allowed, TrailingElseLoc, Attrs); |
Aaron Ballman | b06b15a | 2014-06-06 12:40:24 +0000 | [diff] [blame] | 2028 | |
| 2029 | Attrs.takeAllFrom(TempAttrs); |
| 2030 | return S; |
| 2031 | } |
| 2032 | |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2033 | Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) { |
Chris Lattner | 12f2ea5 | 2009-03-05 00:49:17 +0000 | [diff] [blame] | 2034 | assert(Tok.is(tok::l_brace)); |
| 2035 | SourceLocation LBraceLoc = Tok.getLocation(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2036 | |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 2037 | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, LBraceLoc, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2038 | "parsing function body"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2039 | |
Alexey Bataev | 3d42f34 | 2015-11-20 07:02:57 +0000 | [diff] [blame] | 2040 | // Save and reset current vtordisp stack if we have entered a C++ method body. |
| 2041 | bool IsCXXMethod = |
| 2042 | getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl); |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 2043 | Sema::PragmaStackSentinelRAII |
| 2044 | PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod); |
Alexey Bataev | 3d42f34 | 2015-11-20 07:02:57 +0000 | [diff] [blame] | 2045 | |
Fariborz Jahanian | 8e63294 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 2046 | // Do not enter a scope for the brace, as the arguments are in the same scope |
| 2047 | // (the function body) as the body itself. Instead, just read the statement |
| 2048 | // list and put it into a CompoundStmt for safe keeping. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2049 | StmtResult FnBody(ParseCompoundStatementBody()); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 2050 | |
Fariborz Jahanian | 8e63294 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 2051 | // If the function body could not be parsed, make a bogus compoundstmt. |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2052 | if (FnBody.isInvalid()) { |
| 2053 | Sema::CompoundScopeRAII CompoundScope(Actions); |
Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 2054 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false); |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2055 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 2056 | |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2057 | BodyScope.Exit(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2058 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.get()); |
Seo Sanghyeon | 34f92ac | 2007-12-01 08:06:07 +0000 | [diff] [blame] | 2059 | } |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2060 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2061 | /// ParseFunctionTryBlock - Parse a C++ function-try-block. |
| 2062 | /// |
| 2063 | /// function-try-block: |
| 2064 | /// 'try' ctor-initializer[opt] compound-statement handler-seq |
| 2065 | /// |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2066 | Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) { |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2067 | assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
| 2068 | SourceLocation TryLoc = ConsumeToken(); |
| 2069 | |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 2070 | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, TryLoc, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2071 | "parsing function try block"); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2072 | |
| 2073 | // Constructor initializer list? |
| 2074 | if (Tok.is(tok::colon)) |
| 2075 | ParseConstructorInitializer(Decl); |
Douglas Gregor | 5ca153f | 2011-09-07 20:36:12 +0000 | [diff] [blame] | 2076 | else |
| 2077 | Actions.ActOnDefaultCtorInitializers(Decl); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2078 | |
Alexey Bataev | 3d42f34 | 2015-11-20 07:02:57 +0000 | [diff] [blame] | 2079 | // Save and reset current vtordisp stack if we have entered a C++ method body. |
| 2080 | bool IsCXXMethod = |
| 2081 | getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl); |
Denis Zobnin | 2290dac | 2016-04-29 11:27:00 +0000 | [diff] [blame] | 2082 | Sema::PragmaStackSentinelRAII |
| 2083 | PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod); |
Alexey Bataev | 3d42f34 | 2015-11-20 07:02:57 +0000 | [diff] [blame] | 2084 | |
Sebastian Redl | d98ecd6 | 2009-04-26 21:08:36 +0000 | [diff] [blame] | 2085 | SourceLocation LBraceLoc = Tok.getLocation(); |
David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 2086 | StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true)); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2087 | // If we failed to parse the try-catch, we just give the function an empty |
| 2088 | // compound statement as the body. |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2089 | if (FnBody.isInvalid()) { |
| 2090 | Sema::CompoundScopeRAII CompoundScope(Actions); |
Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 2091 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false); |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2092 | } |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2093 | |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2094 | BodyScope.Exit(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2095 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.get()); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Erik Verbruggen | 6e92251 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2098 | bool Parser::trySkippingFunctionBody() { |
Erik Verbruggen | 6e92251 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2099 | assert(SkipFunctionBodies && |
| 2100 | "Should only be called when SkipFunctionBodies is enabled"); |
Argyrios Kyrtzidis | 289e4a3 | 2012-10-31 17:29:28 +0000 | [diff] [blame] | 2101 | if (!PP.isCodeCompletionEnabled()) { |
Olivier Goffart | f9e890c | 2016-06-16 21:40:06 +0000 | [diff] [blame] | 2102 | SkipFunctionBody(); |
Argyrios Kyrtzidis | 289e4a3 | 2012-10-31 17:29:28 +0000 | [diff] [blame] | 2103 | return true; |
| 2104 | } |
| 2105 | |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2106 | // We're in code-completion mode. Skip parsing for all function bodies unless |
| 2107 | // the body contains the code-completion point. |
| 2108 | TentativeParsingAction PA(*this); |
Olivier Goffart | f9e890c | 2016-06-16 21:40:06 +0000 | [diff] [blame] | 2109 | bool IsTryCatch = Tok.is(tok::kw_try); |
| 2110 | CachedTokens Toks; |
| 2111 | bool ErrorInPrologue = ConsumeAndStoreFunctionPrologue(Toks); |
| 2112 | if (llvm::any_of(Toks, [](const Token &Tok) { |
| 2113 | return Tok.is(tok::code_completion); |
| 2114 | })) { |
| 2115 | PA.Revert(); |
| 2116 | return false; |
| 2117 | } |
| 2118 | if (ErrorInPrologue) { |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2119 | PA.Commit(); |
Olivier Goffart | f9e890c | 2016-06-16 21:40:06 +0000 | [diff] [blame] | 2120 | SkipMalformedDecl(); |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2121 | return true; |
| 2122 | } |
Olivier Goffart | f9e890c | 2016-06-16 21:40:06 +0000 | [diff] [blame] | 2123 | if (!SkipUntil(tok::r_brace, StopAtCodeCompletion)) { |
| 2124 | PA.Revert(); |
| 2125 | return false; |
| 2126 | } |
| 2127 | while (IsTryCatch && Tok.is(tok::kw_catch)) { |
| 2128 | if (!SkipUntil(tok::l_brace, StopAtCodeCompletion) || |
| 2129 | !SkipUntil(tok::r_brace, StopAtCodeCompletion)) { |
| 2130 | PA.Revert(); |
| 2131 | return false; |
| 2132 | } |
| 2133 | } |
| 2134 | PA.Commit(); |
| 2135 | return true; |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2138 | /// ParseCXXTryBlock - Parse a C++ try-block. |
| 2139 | /// |
| 2140 | /// try-block: |
| 2141 | /// 'try' compound-statement handler-seq |
| 2142 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2143 | StmtResult Parser::ParseCXXTryBlock() { |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2144 | assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
| 2145 | |
| 2146 | SourceLocation TryLoc = ConsumeToken(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2147 | return ParseCXXTryBlockCommon(TryLoc); |
| 2148 | } |
| 2149 | |
| 2150 | /// ParseCXXTryBlockCommon - Parse the common part of try-block and |
| 2151 | /// function-try-block. |
| 2152 | /// |
| 2153 | /// try-block: |
| 2154 | /// 'try' compound-statement handler-seq |
| 2155 | /// |
| 2156 | /// function-try-block: |
| 2157 | /// 'try' ctor-initializer[opt] compound-statement handler-seq |
| 2158 | /// |
| 2159 | /// handler-seq: |
| 2160 | /// handler handler-seq[opt] |
| 2161 | /// |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2162 | /// [Borland] try-block: |
| 2163 | /// 'try' compound-statement seh-except-block |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 2164 | /// 'try' compound-statement seh-finally-block |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2165 | /// |
David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 2166 | StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) { |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2167 | if (Tok.isNot(tok::l_brace)) |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2168 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2169 | |
Momchil Velikov | 57c681f | 2017-08-10 15:43:06 +0000 | [diff] [blame] | 2170 | StmtResult TryBlock(ParseCompoundStatement( |
| 2171 | /*isStmtExpr=*/false, Scope::DeclScope | Scope::TryScope | |
| 2172 | Scope::CompoundStmtScope | |
| 2173 | (FnTry ? Scope::FnTryCatchScope : 0))); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2174 | if (TryBlock.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2175 | return TryBlock; |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2176 | |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2177 | // Borland allows SEH-handlers with 'try' |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 2178 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2179 | if ((Tok.is(tok::identifier) && |
| 2180 | Tok.getIdentifierInfo() == getSEHExceptKeyword()) || |
| 2181 | Tok.is(tok::kw___finally)) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2182 | // TODO: Factor into common return ParseSEHHandlerCommon(...) |
| 2183 | StmtResult Handler; |
Douglas Gregor | 60060d6 | 2011-10-21 03:57:52 +0000 | [diff] [blame] | 2184 | if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2185 | SourceLocation Loc = ConsumeToken(); |
| 2186 | Handler = ParseSEHExceptBlock(Loc); |
| 2187 | } |
| 2188 | else { |
| 2189 | SourceLocation Loc = ConsumeToken(); |
| 2190 | Handler = ParseSEHFinallyBlock(Loc); |
| 2191 | } |
| 2192 | if(Handler.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2193 | return Handler; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2194 | |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2195 | return Actions.ActOnSEHTryBlock(true /* IsCXXTry */, |
| 2196 | TryLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2197 | TryBlock.get(), |
Warren Hunt | f6be4cb | 2014-07-25 20:52:51 +0000 | [diff] [blame] | 2198 | Handler.get()); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2199 | } |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2200 | else { |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2201 | StmtVector Handlers; |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 2202 | |
| 2203 | // C++11 attributes can't appear here, despite this context seeming |
| 2204 | // statement-like. |
| 2205 | DiagnoseAndSkipCXX11Attributes(); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2206 | |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2207 | if (Tok.isNot(tok::kw_catch)) |
| 2208 | return StmtError(Diag(Tok, diag::err_expected_catch)); |
| 2209 | while (Tok.is(tok::kw_catch)) { |
David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 2210 | StmtResult Handler(ParseCXXCatchBlock(FnTry)); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2211 | if (!Handler.isInvalid()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2212 | Handlers.push_back(Handler.get()); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2213 | } |
| 2214 | // Don't bother creating the full statement if we don't have any usable |
| 2215 | // handlers. |
| 2216 | if (Handlers.empty()) |
| 2217 | return StmtError(); |
| 2218 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2219 | return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2220 | } |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | /// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard |
| 2224 | /// |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2225 | /// handler: |
| 2226 | /// 'catch' '(' exception-declaration ')' compound-statement |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2227 | /// |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2228 | /// exception-declaration: |
| 2229 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 2230 | /// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt] |
| 2231 | /// '...' |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2232 | /// |
David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 2233 | StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) { |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2234 | assert(Tok.is(tok::kw_catch) && "Expected 'catch'"); |
| 2235 | |
| 2236 | SourceLocation CatchLoc = ConsumeToken(); |
| 2237 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2238 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2239 | if (T.expectAndConsume()) |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2240 | return StmtError(); |
| 2241 | |
| 2242 | // C++ 3.3.2p3: |
| 2243 | // The name in a catch exception-declaration is local to the handler and |
| 2244 | // shall not be redeclared in the outermost block of the handler. |
David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 2245 | ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope | |
David Blaikie | 3403feb | 2012-11-13 18:51:45 +0000 | [diff] [blame] | 2246 | (FnCatch ? Scope::FnTryCatchScope : 0)); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2247 | |
| 2248 | // exception-declaration is equivalent to '...' or a parameter-declaration |
| 2249 | // without default arguments. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2250 | Decl *ExceptionDecl = nullptr; |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2251 | if (Tok.isNot(tok::ellipsis)) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2252 | ParsedAttributesWithRange Attributes(AttrFactory); |
| 2253 | MaybeParseCXX11Attributes(Attributes); |
| 2254 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2255 | DeclSpec DS(AttrFactory); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2256 | DS.takeAttributesFrom(Attributes); |
| 2257 | |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2258 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 2259 | return StmtError(); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2260 | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2261 | Declarator ExDecl(DS, DeclaratorContext::CXXCatchContext); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2262 | ParseDeclarator(ExDecl); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2263 | ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2264 | } else |
| 2265 | ConsumeToken(); |
| 2266 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2267 | T.consumeClose(); |
| 2268 | if (T.getCloseLocation().isInvalid()) |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2269 | return StmtError(); |
| 2270 | |
| 2271 | if (Tok.isNot(tok::l_brace)) |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2272 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2273 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2274 | // FIXME: Possible draft standard bug: attribute-specifier should be allowed? |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2275 | StmtResult Block(ParseCompoundStatement()); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2276 | if (Block.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2277 | return Block; |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2278 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2279 | return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get()); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 2280 | } |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2281 | |
| 2282 | void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) { |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2283 | IfExistsCondition Result; |
Francois Pichet | a5b3fcb | 2011-05-07 17:30:27 +0000 | [diff] [blame] | 2284 | if (ParseMicrosoftIfExistsCondition(Result)) |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2285 | return; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2286 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2287 | // Handle dependent statements by parsing the braces as a compound statement. |
| 2288 | // This is not the same behavior as Visual C++, which don't treat this as a |
| 2289 | // compound statement, but for Clang's type checking we can't have anything |
| 2290 | // inside these braces escaping to the surrounding code. |
| 2291 | if (Result.Behavior == IEB_Dependent) { |
| 2292 | if (!Tok.is(tok::l_brace)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2293 | Diag(Tok, diag::err_expected) << tok::l_brace; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2294 | return; |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2295 | } |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2296 | |
| 2297 | StmtResult Compound = ParseCompoundStatement(); |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2298 | if (Compound.isInvalid()) |
| 2299 | return; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2300 | |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2301 | StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc, |
| 2302 | Result.IsIfExists, |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2303 | Result.SS, |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2304 | Result.Name, |
| 2305 | Compound.get()); |
| 2306 | if (DepResult.isUsable()) |
| 2307 | Stmts.push_back(DepResult.get()); |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2308 | return; |
| 2309 | } |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2310 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2311 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
| 2312 | if (Braces.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2313 | Diag(Tok, diag::err_expected) << tok::l_brace; |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2314 | return; |
| 2315 | } |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2316 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2317 | switch (Result.Behavior) { |
| 2318 | case IEB_Parse: |
| 2319 | // Parse the statements below. |
| 2320 | break; |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 2321 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2322 | case IEB_Dependent: |
| 2323 | llvm_unreachable("Dependent case handled above"); |
Chad Rosier | 67055f5 | 2012-07-10 21:35:27 +0000 | [diff] [blame] | 2324 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2325 | case IEB_Skip: |
| 2326 | Braces.skipToEnd(); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2327 | return; |
| 2328 | } |
| 2329 | |
| 2330 | // Condition is true, parse the statements. |
| 2331 | while (Tok.isNot(tok::r_brace)) { |
Alexey Bataev | c4fad65 | 2016-01-13 11:18:54 +0000 | [diff] [blame] | 2332 | StmtResult R = ParseStatementOrDeclaration(Stmts, ACK_Any); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2333 | if (R.isUsable()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2334 | Stmts.push_back(R.get()); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2335 | } |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2336 | Braces.consumeClose(); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 2337 | } |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 2338 | |
| 2339 | bool Parser::ParseOpenCLUnrollHintAttribute(ParsedAttributes &Attrs) { |
| 2340 | MaybeParseGNUAttributes(Attrs); |
| 2341 | |
| 2342 | if (Attrs.empty()) |
| 2343 | return true; |
| 2344 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2345 | if (Attrs.begin()->getKind() != ParsedAttr::AT_OpenCLUnrollHint) |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 2346 | return true; |
| 2347 | |
| 2348 | if (!(Tok.is(tok::kw_for) || Tok.is(tok::kw_while) || Tok.is(tok::kw_do))) { |
| 2349 | Diag(Tok, diag::err_opencl_unroll_hint_on_non_loop); |
| 2350 | return false; |
| 2351 | } |
| 2352 | return true; |
| 2353 | } |