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