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