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