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 | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/DeclSpec.h" |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 18 | #include "clang/Sema/PrettyDeclStackTrace.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 19 | #include "clang/Sema/Scope.h" |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
| 21 | #include "clang/Basic/PrettyStackTrace.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // C99 6.8: Statements and Blocks. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | /// ParseStatementOrDeclaration - Read 'statement' or 'declaration'. |
| 30 | /// StatementOrDeclaration: |
| 31 | /// statement |
| 32 | /// declaration |
| 33 | /// |
| 34 | /// statement: |
| 35 | /// labeled-statement |
| 36 | /// compound-statement |
| 37 | /// expression-statement |
| 38 | /// selection-statement |
| 39 | /// iteration-statement |
| 40 | /// jump-statement |
Argyrios Kyrtzidis | dee8291 | 2008-09-07 18:58:01 +0000 | [diff] [blame] | 41 | /// [C++] declaration-statement |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 42 | /// [C++] try-block |
Fariborz Jahanian | 9081457 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 43 | /// [OBC] objc-throw-statement |
| 44 | /// [OBC] objc-try-catch-statement |
Fariborz Jahanian | f89ca38 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 45 | /// [OBC] objc-synchronized-statement |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 46 | /// [GNU] asm-statement |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 47 | /// [OMP] openmp-construct [TODO] |
| 48 | /// |
| 49 | /// labeled-statement: |
| 50 | /// identifier ':' statement |
| 51 | /// 'case' constant-expression ':' statement |
| 52 | /// 'default' ':' statement |
| 53 | /// |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 54 | /// selection-statement: |
| 55 | /// if-statement |
| 56 | /// switch-statement |
| 57 | /// |
| 58 | /// iteration-statement: |
| 59 | /// while-statement |
| 60 | /// do-statement |
| 61 | /// for-statement |
| 62 | /// |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 63 | /// expression-statement: |
| 64 | /// expression[opt] ';' |
| 65 | /// |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 66 | /// jump-statement: |
| 67 | /// 'goto' identifier ';' |
| 68 | /// 'continue' ';' |
| 69 | /// 'break' ';' |
| 70 | /// 'return' expression[opt] ';' |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 71 | /// [GNU] 'goto' '*' expression ';' |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 72 | /// |
Fariborz Jahanian | 9081457 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 73 | /// [OBC] objc-throw-statement: |
| 74 | /// [OBC] '@' 'throw' expression ';' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | /// [OBC] '@' 'throw' ';' |
| 76 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 77 | StmtResult |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 78 | Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) { |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 79 | const char *SemiError = 0; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 80 | StmtResult Res; |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 81 | |
| 82 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 83 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 84 | ParsedAttributesWithRange attrs; |
| 85 | MaybeParseCXX0XAttributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 87 | // Cases in this switch statement should fall through if the parser expects |
| 88 | // the token to end in a semicolon (in which case SemiError should be set), |
| 89 | // or they directly 'return;' if not. |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 90 | tok::TokenKind Kind = Tok.getKind(); |
| 91 | SourceLocation AtLoc; |
| 92 | switch (Kind) { |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 93 | case tok::at: // May be a @try or @throw statement |
| 94 | { |
| 95 | AtLoc = ConsumeToken(); // consume @ |
Sebastian Redl | bab9a4b | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 96 | return ParseObjCAtStatement(AtLoc); |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 97 | } |
Fariborz Jahanian | 62fd2b4 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 99 | case tok::code_completion: |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 100 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 101 | ConsumeCodeCompletionToken(); |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 102 | return ParseStatementOrDeclaration(Stmts, OnlyStatement); |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 103 | |
Argyrios Kyrtzidis | 07b8b63 | 2008-07-12 21:04:42 +0000 | [diff] [blame] | 104 | case tok::identifier: |
| 105 | if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement |
| 106 | // identifier ':' statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 107 | return ParseLabeledStatement(attrs); |
Argyrios Kyrtzidis | 07b8b63 | 2008-07-12 21:04:42 +0000 | [diff] [blame] | 108 | } |
| 109 | // PASS THROUGH. |
| 110 | |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 111 | default: { |
Argyrios Kyrtzidis | 2c7137d | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 112 | if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) { |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 113 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Ted Kremenek | 5eec2b0 | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 114 | DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 115 | DeclEnd, attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 116 | return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd); |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | f8afb62 | 2006-08-10 18:26:31 +0000 | [diff] [blame] | 120 | Diag(Tok, diag::err_expected_statement); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 121 | return StmtError(); |
Chris Lattner | f8afb62 | 2006-08-10 18:26:31 +0000 | [diff] [blame] | 122 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 124 | // FIXME: Use the attributes |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 125 | // expression[opt] ';' |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 126 | ExprResult Expr(ParseExpression()); |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 127 | if (Expr.isInvalid()) { |
Argyrios Kyrtzidis | 90ab3b7 | 2010-03-31 00:37:59 +0000 | [diff] [blame] | 128 | // If the expression is invalid, skip ahead to the next semicolon or '}'. |
| 129 | // Not doing this opens us up to the possibility of infinite loops if |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 130 | // ParseExpression does not consume any tokens. |
Argyrios Kyrtzidis | 90ab3b7 | 2010-03-31 00:37:59 +0000 | [diff] [blame] | 131 | SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 132 | if (Tok.is(tok::semi)) |
| 133 | ConsumeToken(); |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 134 | return StmtError(); |
| 135 | } |
| 136 | // Otherwise, eat the semicolon. |
Douglas Gregor | 45d6bdf | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 137 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 138 | return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get())); |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 139 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 140 | |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 141 | case tok::kw_case: // C99 6.8.1: labeled-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 142 | return ParseCaseStatement(attrs); |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 143 | case tok::kw_default: // C99 6.8.1: labeled-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 144 | return ParseDefaultStatement(attrs); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 146 | case tok::l_brace: // C99 6.8.2: compound-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 147 | return ParseCompoundStatement(attrs); |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 148 | case tok::semi: { // C99 6.8.3p3: expression[opt] ';' |
| 149 | bool LeadingEmptyMacro = Tok.hasLeadingEmptyMacro(); |
| 150 | return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacro); |
| 151 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 153 | case tok::kw_if: // C99 6.8.4.1: if-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 154 | return ParseIfStatement(attrs); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 155 | case tok::kw_switch: // C99 6.8.4.2: switch-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 156 | return ParseSwitchStatement(attrs); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 158 | case tok::kw_while: // C99 6.8.5.1: while-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 159 | return ParseWhileStatement(attrs); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 160 | case tok::kw_do: // C99 6.8.5.2: do-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 161 | Res = ParseDoStatement(attrs); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 162 | SemiError = "do/while"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 163 | break; |
| 164 | case tok::kw_for: // C99 6.8.5.3: for-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 165 | return ParseForStatement(attrs); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 166 | |
| 167 | case tok::kw_goto: // C99 6.8.6.1: goto-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 168 | Res = ParseGotoStatement(attrs); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 169 | SemiError = "goto"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 170 | break; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 171 | case tok::kw_continue: // C99 6.8.6.2: continue-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 172 | Res = ParseContinueStatement(attrs); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 173 | SemiError = "continue"; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 174 | break; |
| 175 | case tok::kw_break: // C99 6.8.6.3: break-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 176 | Res = ParseBreakStatement(attrs); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 177 | SemiError = "break"; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 178 | break; |
| 179 | case tok::kw_return: // C99 6.8.6.4: return-statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 180 | Res = ParseReturnStatement(attrs); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 181 | SemiError = "return"; |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 182 | break; |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 183 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 184 | case tok::kw_asm: { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 185 | ProhibitAttributes(attrs); |
Steve Naroff | b2c80c7 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 186 | bool msAsm = false; |
| 187 | Res = ParseAsmStatement(msAsm); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 188 | Res = Actions.ActOnFinishFullStmt(Res.get()); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 189 | if (msAsm) return move(Res); |
Chris Lattner | 34a9566 | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 190 | SemiError = "asm"; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 191 | break; |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 192 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 193 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 194 | case tok::kw_try: // C++ 15: try-block |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 195 | return ParseCXXTryBlock(attrs); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 198 | // If we reached this code, the statement must end in a semicolon. |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 199 | if (Tok.is(tok::semi)) { |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 200 | ConsumeToken(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 201 | } else if (!Res.isInvalid()) { |
Chris Lattner | 8e3eed0 | 2009-06-14 00:23:56 +0000 | [diff] [blame] | 202 | // If the result was valid, then we do want to diagnose this. Use |
| 203 | // ExpectAndConsume to emit the diagnostic, even though we know it won't |
| 204 | // succeed. |
| 205 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError); |
Chris Lattner | 0046de1 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 206 | // Skip until we see a } or ;, but don't eat it. |
| 207 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 208 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 210 | return move(Res); |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 213 | /// ParseLabeledStatement - We have an identifier and a ':' after it. |
Chris Lattner | 6dfd978 | 2006-08-10 18:31:37 +0000 | [diff] [blame] | 214 | /// |
| 215 | /// labeled-statement: |
| 216 | /// identifier ':' statement |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 217 | /// [GNU] identifier ':' attributes[opt] statement |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 218 | /// |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 219 | StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) { |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 220 | assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() && |
| 221 | "Not an identifier!"); |
| 222 | |
| 223 | Token IdentTok = Tok; // Save the whole token. |
| 224 | ConsumeToken(); // eat the identifier. |
| 225 | |
| 226 | assert(Tok.is(tok::colon) && "Not a label!"); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 227 | |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 228 | // identifier ':' statement |
| 229 | SourceLocation ColonLoc = ConsumeToken(); |
| 230 | |
| 231 | // Read label attributes, if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 232 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 233 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 234 | StmtResult SubStmt(ParseStatement()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 235 | |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 236 | // 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] | 237 | if (SubStmt.isInvalid()) |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 238 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 239 | |
Sebastian Redl | 6a8002e | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 240 | return Actions.ActOnLabelStmt(IdentTok.getLocation(), |
| 241 | IdentTok.getIdentifierInfo(), |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 242 | ColonLoc, SubStmt.get(), attrs.getList()); |
Argyrios Kyrtzidis | 832e898 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 243 | } |
Chris Lattner | f8afb62 | 2006-08-10 18:26:31 +0000 | [diff] [blame] | 244 | |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 245 | /// ParseCaseStatement |
| 246 | /// labeled-statement: |
| 247 | /// 'case' constant-expression ':' statement |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 248 | /// [GNU] 'case' constant-expression '...' constant-expression ':' statement |
Chris Lattner | 8693a51 | 2006-08-13 21:54:02 +0000 | [diff] [blame] | 249 | /// |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 250 | StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 251 | assert(Tok.is(tok::kw_case) && "Not a case stmt!"); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 252 | // FIXME: Use attributes? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 254 | // It is very very common for code to contain many case statements recursively |
| 255 | // nested, as in (but usually without indentation): |
| 256 | // case 1: |
| 257 | // case 2: |
| 258 | // case 3: |
| 259 | // case 4: |
| 260 | // case 5: etc. |
| 261 | // |
| 262 | // Parsing this naively works, but is both inefficient and can cause us to run |
| 263 | // 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] | 264 | // flatten this recursion into an iterative loop. This is complex and gross, |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 265 | // but all the grossness is constrained to ParseCaseStatement (and some |
| 266 | // wierdness in the actions), so this is just local grossness :). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 268 | // TopLevelCase - This is the highest level we have parsed. 'case 1' in the |
| 269 | // example above. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 270 | StmtResult TopLevelCase(true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 272 | // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which |
| 273 | // gets updated each time a new case is parsed, and whose body is unset so |
| 274 | // far. When parsing 'case 4', this is the 'case 3' node. |
| 275 | StmtTy *DeepestParsedCaseStmt = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 277 | // While we have case statements, eat and stack them. |
| 278 | do { |
| 279 | SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 281 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 282 | Actions.CodeCompleteCase(getCurScope()); |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 283 | ConsumeCodeCompletionToken(); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Chris Lattner | 125c0ee | 2009-12-10 00:38:54 +0000 | [diff] [blame] | 286 | /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'. |
| 287 | /// Disable this form of error recovery while we're parsing the case |
| 288 | /// expression. |
| 289 | ColonProtectionRAIIObject ColonProtection(*this); |
| 290 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 291 | ExprResult LHS(ParseConstantExpression()); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 292 | if (LHS.isInvalid()) { |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 293 | SkipUntil(tok::colon); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 294 | return StmtError(); |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 295 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 297 | // GNU case range extension. |
| 298 | SourceLocation DotDotDotLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 299 | ExprResult RHS; |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 300 | if (Tok.is(tok::ellipsis)) { |
| 301 | Diag(Tok, diag::ext_gnu_case_range); |
| 302 | DotDotDotLoc = ConsumeToken(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 304 | RHS = ParseConstantExpression(); |
| 305 | if (RHS.isInvalid()) { |
| 306 | SkipUntil(tok::colon); |
| 307 | return StmtError(); |
| 308 | } |
| 309 | } |
Chris Lattner | 125c0ee | 2009-12-10 00:38:54 +0000 | [diff] [blame] | 310 | |
| 311 | ColonProtection.restore(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 312 | |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 313 | SourceLocation ColonLoc; |
John McCall | 0140bfe | 2011-01-22 09:28:32 +0000 | [diff] [blame] | 314 | if (Tok.is(tok::colon)) { |
| 315 | ColonLoc = ConsumeToken(); |
| 316 | |
| 317 | // Treat "case blah;" as a typo for "case blah:". |
| 318 | } else if (Tok.is(tok::semi)) { |
| 319 | ColonLoc = ConsumeToken(); |
| 320 | Diag(ColonLoc, diag::err_expected_colon_after) << "'case'" |
| 321 | << FixItHint::CreateReplacement(ColonLoc, ":"); |
| 322 | } else { |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 323 | SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation); |
| 324 | Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'" |
| 325 | << FixItHint::CreateInsertion(ExpectedLoc, ":"); |
| 326 | ColonLoc = ExpectedLoc; |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 327 | } |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 328 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 329 | StmtResult Case = |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 330 | Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc, |
| 331 | RHS.get(), ColonLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 333 | // If we had a sema error parsing this case, then just ignore it and |
| 334 | // continue parsing the sub-stmt. |
| 335 | if (Case.isInvalid()) { |
| 336 | if (TopLevelCase.isInvalid()) // No parsed case stmts. |
| 337 | return ParseStatement(); |
| 338 | // Otherwise, just don't add it as a nested case. |
| 339 | } else { |
| 340 | // If this is the first case statement we parsed, it becomes TopLevelCase. |
| 341 | // Otherwise we link it into the current chain. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 342 | Stmt *NextDeepest = Case.get(); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 343 | if (TopLevelCase.isInvalid()) |
| 344 | TopLevelCase = move(Case); |
| 345 | else |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 346 | Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get()); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 347 | DeepestParsedCaseStmt = NextDeepest; |
| 348 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 350 | // Handle all case statements. |
| 351 | } while (Tok.is(tok::kw_case)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 353 | assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 355 | // If we found a non-case statement, start by parsing it. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 356 | StmtResult SubStmt; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 358 | if (Tok.isNot(tok::r_brace)) { |
| 359 | SubStmt = ParseStatement(); |
| 360 | } else { |
| 361 | // Nicely diagnose the common error "switch (X) { case 4: }", which is |
| 362 | // not valid. |
| 363 | // FIXME: add insertion hint. |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 364 | Diag(Tok, diag::err_label_end_of_compound_statement); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 365 | SubStmt = true; |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 366 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 368 | // Broken sub-stmt shouldn't prevent forming the case statement properly. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 369 | if (SubStmt.isInvalid()) |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 370 | SubStmt = Actions.ActOnNullStmt(SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 372 | // Install the body into the most deeply-nested case. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 373 | Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get()); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 375 | // Return the top level parsed statement tree. |
Chris Lattner | 2b19a658 | 2009-03-04 18:24:58 +0000 | [diff] [blame] | 376 | return move(TopLevelCase); |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /// ParseDefaultStatement |
| 380 | /// labeled-statement: |
| 381 | /// 'default' ':' statement |
| 382 | /// Note that this does not parse the 'statement' at the end. |
| 383 | /// |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 384 | StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 385 | //FIXME: Use attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 386 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 387 | assert(Tok.is(tok::kw_default) && "Not a default stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 388 | SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'. |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 390 | SourceLocation ColonLoc; |
John McCall | 0140bfe | 2011-01-22 09:28:32 +0000 | [diff] [blame] | 391 | if (Tok.is(tok::colon)) { |
| 392 | ColonLoc = ConsumeToken(); |
| 393 | |
| 394 | // Treat "default;" as a typo for "default:". |
| 395 | } else if (Tok.is(tok::semi)) { |
| 396 | ColonLoc = ConsumeToken(); |
| 397 | Diag(ColonLoc, diag::err_expected_colon_after) << "'default'" |
| 398 | << FixItHint::CreateReplacement(ColonLoc, ":"); |
| 399 | } else { |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 400 | SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation); |
| 401 | Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'" |
| 402 | << FixItHint::CreateInsertion(ExpectedLoc, ":"); |
| 403 | ColonLoc = ExpectedLoc; |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 404 | } |
Douglas Gregor | 0d0a965 | 2010-12-23 22:56:40 +0000 | [diff] [blame] | 405 | |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 406 | // Diagnose the common error "switch (X) {... default: }", which is not valid. |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 407 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 408 | Diag(Tok, diag::err_label_end_of_compound_statement); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 409 | return StmtError(); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 410 | } |
| 411 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 412 | StmtResult SubStmt(ParseStatement()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 413 | if (SubStmt.isInvalid()) |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 414 | return StmtError(); |
| 415 | |
Sebastian Redl | 1cbb5918 | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 416 | return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 417 | SubStmt.get(), getCurScope()); |
Chris Lattner | d2685cf | 2006-08-10 05:59:48 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 421 | /// ParseCompoundStatement - Parse a "{}" block. |
| 422 | /// |
| 423 | /// compound-statement: [C99 6.8.2] |
| 424 | /// { block-item-list[opt] } |
| 425 | /// [GNU] { label-declarations block-item-list } [TODO] |
| 426 | /// |
| 427 | /// block-item-list: |
| 428 | /// block-item |
| 429 | /// block-item-list block-item |
| 430 | /// |
| 431 | /// block-item: |
| 432 | /// declaration |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 433 | /// [GNU] '__extension__' declaration |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 434 | /// statement |
| 435 | /// [OMP] openmp-directive [TODO] |
| 436 | /// |
| 437 | /// [GNU] label-declarations: |
| 438 | /// [GNU] label-declaration |
| 439 | /// [GNU] label-declarations label-declaration |
| 440 | /// |
| 441 | /// [GNU] label-declaration: |
| 442 | /// [GNU] '__label__' identifier-list ';' |
| 443 | /// |
| 444 | /// [OMP] openmp-directive: [TODO] |
| 445 | /// [OMP] barrier-directive |
| 446 | /// [OMP] flush-directive |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 447 | /// |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 448 | StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 449 | bool isStmtExpr) { |
| 450 | //FIXME: Use attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 451 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 452 | assert(Tok.is(tok::l_brace) && "Not a compount stmt!"); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 1a76a3c | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 454 | // Enter a scope to hold everything within the compound stmt. Compound |
| 455 | // statements can always hold declarations. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 456 | ParseScope CompoundScope(this, Scope::DeclScope); |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 457 | |
| 458 | // Parse the statements in the body. |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 459 | return ParseCompoundStatementBody(isStmtExpr); |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | |
| 463 | /// ParseCompoundStatementBody - Parse a sequence of statements and invoke the |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 464 | /// ActOnCompoundStmt action. This expects the '{' to be the current token, and |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 465 | /// consume the '}' at the end of the block. It does not manipulate the scope |
| 466 | /// stack. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 467 | StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 469 | Tok.getLocation(), |
| 470 | "in compound statement ('{}')"); |
Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 471 | InMessageExpressionRAIIObject InMessage(*this, false); |
| 472 | |
Chris Lattner | f297880 | 2007-01-21 06:52:16 +0000 | [diff] [blame] | 473 | SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'. |
| 474 | |
Chris Lattner | 010015a | 2007-05-28 07:23:54 +0000 | [diff] [blame] | 475 | // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 476 | // only allowed at the start of a compound stmt regardless of the language. |
Sebastian Redl | 511ed55 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 477 | |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 478 | StmtVector Stmts(Actions); |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 479 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Argyrios Kyrtzidis | ee56962 | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 480 | |
| 481 | if (Tok.is(tok::annot_pragma_unused)) { |
| 482 | HandlePragmaUnused(); |
| 483 | continue; |
| 484 | } |
| 485 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 486 | StmtResult R; |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 487 | if (Tok.isNot(tok::kw___extension__)) { |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 488 | R = ParseStatementOrDeclaration(Stmts, false); |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 489 | } else { |
| 490 | // __extension__ can start declarations and it can also be a unary |
| 491 | // operator for expressions. Consume multiple __extension__ markers here |
| 492 | // until we can determine which is which. |
Eli Friedman | eb3a9b0 | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 493 | // FIXME: This loses extension expressions in the AST! |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 494 | SourceLocation ExtLoc = ConsumeToken(); |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 495 | while (Tok.is(tok::kw___extension__)) |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 496 | ConsumeToken(); |
Chris Lattner | 1ff6e73 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 497 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 498 | ParsedAttributesWithRange attrs; |
| 499 | MaybeParseCXX0XAttributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 500 | |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 501 | // If this is the start of a declaration, parse it as such. |
Argyrios Kyrtzidis | 2c7137d | 2008-10-05 00:06:24 +0000 | [diff] [blame] | 502 | if (isDeclarationStatement()) { |
Eli Friedman | 15af3ee | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 503 | // __extension__ silences extension warnings in the subdeclaration. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 504 | // FIXME: Save the __extension__ on the decl as a node somehow? |
Eli Friedman | 15af3ee | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 505 | ExtensionRAIIObject O(Diags); |
| 506 | |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 507 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 508 | DeclGroupPtrTy Res = ParseDeclaration(Stmts, |
| 509 | Declarator::BlockContext, DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 510 | attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 511 | R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd); |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 512 | } else { |
Eli Friedman | eb3a9b0 | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 513 | // Otherwise this was a unary __extension__ marker. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 514 | ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc)); |
Chris Lattner | fdc0748 | 2008-03-13 06:32:11 +0000 | [diff] [blame] | 515 | |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 516 | if (Res.isInvalid()) { |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 517 | SkipUntil(tok::semi); |
| 518 | continue; |
| 519 | } |
Sebastian Redl | c2edafb | 2009-01-18 18:03:53 +0000 | [diff] [blame] | 520 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 521 | // FIXME: Use attributes? |
Chris Lattner | 1ff6e73 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 522 | // Eat the semicolon at the end of stmt and convert the expr into a |
| 523 | // statement. |
Douglas Gregor | 45d6bdf | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 524 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 525 | R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get())); |
Chris Lattner | dfaf9f8 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 528 | |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 529 | if (R.isUsable()) |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 530 | Stmts.push_back(R.release()); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 531 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 532 | |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 533 | // We broke out of the while loop because we found a '}' or EOF. |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 534 | if (Tok.isNot(tok::r_brace)) { |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 535 | Diag(Tok, diag::err_expected_rbrace); |
Chris Lattner | 0073962 | 2010-09-01 15:49:26 +0000 | [diff] [blame] | 536 | Diag(LBraceLoc, diag::note_matching) << "{"; |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 537 | return StmtError(); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 538 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 539 | |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 540 | SourceLocation RBraceLoc = ConsumeBrace(); |
Sebastian Redl | c2edafb | 2009-01-18 18:03:53 +0000 | [diff] [blame] | 541 | return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts), |
Sebastian Redl | 52f03ba | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 542 | isStmtExpr); |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 543 | } |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 544 | |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 545 | /// ParseParenExprOrCondition: |
| 546 | /// [C ] '(' expression ')' |
Chris Lattner | 10da53c | 2008-12-12 06:35:28 +0000 | [diff] [blame] | 547 | /// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true] |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 548 | /// |
| 549 | /// This function parses and performs error recovery on the specified condition |
| 550 | /// or expression (depending on whether we're in C++ or C mode). This function |
| 551 | /// goes out of its way to recover well. It returns true if there was a parser |
| 552 | /// error (the right paren couldn't be found), which indicates that the caller |
| 553 | /// should try to recover harder. It returns false if the condition is |
| 554 | /// successfully parsed. Note that a successful parse can still have semantic |
| 555 | /// errors in the condition. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 556 | bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 557 | Decl *&DeclResult, |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 558 | SourceLocation Loc, |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 559 | bool ConvertToBoolean) { |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 560 | SourceLocation LParenLoc = ConsumeParen(); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 561 | if (getLang().CPlusPlus) |
Jeffrey Yasskin | 8dfa5f1 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 562 | ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 563 | else { |
| 564 | ExprResult = ParseExpression(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 565 | DeclResult = 0; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 566 | |
| 567 | // If required, convert to a boolean value. |
| 568 | if (!ExprResult.isInvalid() && ConvertToBoolean) |
| 569 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 570 | = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get()); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 571 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 573 | // If the parser was confused by the condition and we don't have a ')', try to |
| 574 | // recover by skipping ahead to a semi and bailing out. If condexp is |
| 575 | // semantically invalid but we have well formed code, keep going. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 576 | if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) { |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 577 | SkipUntil(tok::semi); |
| 578 | // Skipping may have stopped if it found the containing ')'. If so, we can |
| 579 | // continue parsing the if statement. |
| 580 | if (Tok.isNot(tok::r_paren)) |
| 581 | return true; |
| 582 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 584 | // Otherwise the condition is valid or the rparen is present. |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 585 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 586 | return false; |
| 587 | } |
| 588 | |
| 589 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 590 | /// ParseIfStatement |
| 591 | /// if-statement: [C99 6.8.4.1] |
| 592 | /// 'if' '(' expression ')' statement |
| 593 | /// 'if' '(' expression ')' statement 'else' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 594 | /// [C++] 'if' '(' condition ')' statement |
| 595 | /// [C++] 'if' '(' condition ')' statement 'else' statement |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 596 | /// |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 597 | StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 598 | // FIXME: Use attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 599 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 600 | assert(Tok.is(tok::kw_if) && "Not an if stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 601 | SourceLocation IfLoc = ConsumeToken(); // eat the 'if'. |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 602 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 603 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 604 | Diag(Tok, diag::err_expected_lparen_after) << "if"; |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 605 | SkipUntil(tok::semi); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 606 | return StmtError(); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 607 | } |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 608 | |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 609 | bool C99orCXX = getLang().C99 || getLang().CPlusPlus; |
| 610 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 611 | // C99 6.8.4p3 - In C99, the if statement is a block. This is not |
| 612 | // the case for C90. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 613 | // |
| 614 | // C++ 6.4p3: |
| 615 | // A name introduced by a declaration in a condition is in scope from its |
| 616 | // point of declaration until the end of the substatements controlled by the |
| 617 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 618 | // C++ 3.3.2p4: |
| 619 | // Names declared in the for-init-statement, and in the condition of if, |
| 620 | // while, for, and switch statements are local to the if, while, for, or |
| 621 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 622 | // |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 623 | ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX); |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 624 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 625 | // Parse the condition. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 626 | ExprResult CondExp; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 627 | Decl *CondVar = 0; |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 628 | if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true)) |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 629 | return StmtError(); |
Chris Lattner | bc2d77c | 2008-12-12 06:19:11 +0000 | [diff] [blame] | 630 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 631 | FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 633 | // 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] | 634 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 635 | // 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] | 636 | // |
| 637 | // C++ 6.4p1: |
| 638 | // The substatement in a selection-statement (each substatement, in the else |
| 639 | // form of the if statement) implicitly defines a local scope. |
| 640 | // |
| 641 | // For C++ we create a scope for the condition and a new scope for |
| 642 | // substatements because: |
| 643 | // -When the 'then' scope exits, we want the condition declaration to still be |
| 644 | // active for the 'else' scope too. |
| 645 | // -Sema will detect name clashes by considering declarations of a |
| 646 | // 'ControlScope' as part of its direct subscope. |
| 647 | // -If we wanted the condition and substatement to be in the same scope, we |
| 648 | // would have to notify ParseStatement not to create a new scope. It's |
| 649 | // simpler to let it create a new scope. |
| 650 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 652 | C99orCXX && Tok.isNot(tok::l_brace)); |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 653 | |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 654 | // Read the 'then' stmt. |
| 655 | SourceLocation ThenStmtLoc = Tok.getLocation(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 656 | StmtResult ThenStmt(ParseStatement()); |
Chris Lattner | ac4471c | 2007-05-28 05:38:24 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 37e54f4 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 658 | // Pop the 'if' scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 659 | InnerScope.Exit(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 660 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 661 | // If it has an else, parse it. |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 662 | SourceLocation ElseLoc; |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 663 | SourceLocation ElseStmtLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 664 | StmtResult ElseStmt; |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 665 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 666 | if (Tok.is(tok::kw_else)) { |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 667 | ElseLoc = ConsumeToken(); |
Chris Lattner | df74264 | 2010-04-12 06:12:50 +0000 | [diff] [blame] | 668 | ElseStmtLoc = Tok.getLocation(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 669 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 670 | // 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] | 671 | // there is no compound stmt. C90 does not have this clause. We only do |
| 672 | // this if the body isn't a compound statement to avoid push/pop in common |
| 673 | // cases. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 674 | // |
| 675 | // C++ 6.4p1: |
| 676 | // The substatement in a selection-statement (each substatement, in the else |
| 677 | // form of the if statement) implicitly defines a local scope. |
| 678 | // |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 679 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 680 | C99orCXX && Tok.isNot(tok::l_brace)); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 682 | ElseStmt = ParseStatement(); |
Chris Lattner | df74264 | 2010-04-12 06:12:50 +0000 | [diff] [blame] | 683 | |
Chris Lattner | 37e54f4 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 684 | // Pop the 'else' scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 685 | InnerScope.Exit(); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 686 | } |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 687 | |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 688 | IfScope.Exit(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | |
Chris Lattner | bc2d77c | 2008-12-12 06:19:11 +0000 | [diff] [blame] | 690 | // If the condition was invalid, discard the if statement. We could recover |
| 691 | // better by replacing it with a valid expr, but don't do that yet. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 692 | if (CondExp.isInvalid() && !CondVar) |
Chris Lattner | bc2d77c | 2008-12-12 06:19:11 +0000 | [diff] [blame] | 693 | return StmtError(); |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 694 | |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 695 | // 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] | 696 | // 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] | 697 | // part. If both are invalid, return error. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 698 | if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) || |
| 699 | (ThenStmt.isInvalid() && ElseStmt.get() == 0) || |
| 700 | (ThenStmt.get() == 0 && ElseStmt.isInvalid())) { |
Sebastian Redl | 511ed55 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 701 | // 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] | 702 | return StmtError(); |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 703 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 705 | // Now if either are invalid, replace with a ';'. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 706 | if (ThenStmt.isInvalid()) |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 707 | ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 708 | if (ElseStmt.isInvalid()) |
Chris Lattner | 5c5808a | 2007-10-29 05:08:52 +0000 | [diff] [blame] | 709 | ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 710 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 711 | return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(), |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 712 | ElseLoc, ElseStmt.get()); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 715 | /// ParseSwitchStatement |
| 716 | /// switch-statement: |
| 717 | /// 'switch' '(' expression ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 718 | /// [C++] 'switch' '(' condition ')' statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 719 | StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 720 | // FIXME: Use attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 721 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 722 | assert(Tok.is(tok::kw_switch) && "Not a switch stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 723 | SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'. |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 724 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 725 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 726 | Diag(Tok, diag::err_expected_lparen_after) << "switch"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 727 | SkipUntil(tok::semi); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 728 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 729 | } |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 730 | |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 731 | bool C99orCXX = getLang().C99 || getLang().CPlusPlus; |
| 732 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 733 | // C99 6.8.4p3 - In C99, the switch statement is a block. This is |
| 734 | // not the case for C90. Start the switch scope. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 735 | // |
| 736 | // C++ 6.4p3: |
| 737 | // A name introduced by a declaration in a condition is in scope from its |
| 738 | // point of declaration until the end of the substatements controlled by the |
| 739 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 740 | // C++ 3.3.2p4: |
| 741 | // Names declared in the for-init-statement, and in the condition of if, |
| 742 | // while, for, and switch statements are local to the if, while, for, or |
| 743 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 744 | // |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 745 | unsigned ScopeFlags = Scope::BreakScope; |
| 746 | if (C99orCXX) |
| 747 | ScopeFlags |= Scope::DeclScope | Scope::ControlScope; |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 748 | ParseScope SwitchScope(this, ScopeFlags); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 749 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 750 | // Parse the condition. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 751 | ExprResult Cond; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 752 | Decl *CondVar = 0; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 753 | if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false)) |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 754 | return StmtError(); |
Eli Friedman | 44842d1 | 2008-12-17 22:19:57 +0000 | [diff] [blame] | 755 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 756 | StmtResult Switch |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 757 | = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 758 | |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 759 | if (Switch.isInvalid()) { |
| 760 | // Skip the switch body. |
| 761 | // FIXME: This is not optimal recovery, but parsing the body is more |
| 762 | // dangerous due to the presence of case and default statements, which |
| 763 | // will have no place to connect back with the switch. |
Douglas Gregor | 4abc32d | 2010-05-20 23:20:59 +0000 | [diff] [blame] | 764 | if (Tok.is(tok::l_brace)) { |
| 765 | ConsumeBrace(); |
| 766 | SkipUntil(tok::r_brace, false, false); |
| 767 | } else |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 768 | SkipUntil(tok::semi); |
| 769 | return move(Switch); |
| 770 | } |
| 771 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 772 | // 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] | 773 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 774 | // 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] | 775 | // |
| 776 | // C++ 6.4p1: |
| 777 | // The substatement in a selection-statement (each substatement, in the else |
| 778 | // form of the if statement) implicitly defines a local scope. |
| 779 | // |
| 780 | // See comments in ParseIfStatement for why we create a scope for the |
| 781 | // condition and a new scope for substatement in C++. |
| 782 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 784 | C99orCXX && Tok.isNot(tok::l_brace)); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 785 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 786 | // Read the body statement. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 787 | StmtResult Body(ParseStatement()); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 788 | |
Chris Lattner | 8fd2d01 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 789 | // Pop the scopes. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 790 | InnerScope.Exit(); |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 791 | SwitchScope.Exit(); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 792 | |
Chris Lattner | 8fd2d01 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 793 | if (Body.isInvalid()) |
| 794 | // FIXME: Remove the case statement list from the Switch statement. |
| 795 | Body = Actions.ActOnNullStmt(Tok.getLocation()); |
| 796 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 797 | return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /// ParseWhileStatement |
| 801 | /// while-statement: [C99 6.8.5.1] |
| 802 | /// 'while' '(' expression ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 803 | /// [C++] 'while' '(' condition ')' statement |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 804 | StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 805 | // FIXME: Use attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 806 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 807 | assert(Tok.is(tok::kw_while) && "Not a while stmt!"); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 808 | SourceLocation WhileLoc = Tok.getLocation(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 809 | ConsumeToken(); // eat the 'while'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 810 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 811 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 812 | Diag(Tok, diag::err_expected_lparen_after) << "while"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 813 | SkipUntil(tok::semi); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 814 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 815 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 816 | |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 817 | bool C99orCXX = getLang().C99 || getLang().CPlusPlus; |
| 818 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 819 | // C99 6.8.5p5 - In C99, the while statement is a block. This is not |
| 820 | // the case for C90. Start the loop scope. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 821 | // |
| 822 | // C++ 6.4p3: |
| 823 | // A name introduced by a declaration in a condition is in scope from its |
| 824 | // point of declaration until the end of the substatements controlled by the |
| 825 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 826 | // C++ 3.3.2p4: |
| 827 | // Names declared in the for-init-statement, and in the condition of if, |
| 828 | // while, for, and switch statements are local to the if, while, for, or |
| 829 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 830 | // |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 831 | unsigned ScopeFlags; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 832 | if (C99orCXX) |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 833 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | |
| 834 | Scope::DeclScope | Scope::ControlScope; |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 835 | else |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 836 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
| 837 | ParseScope WhileScope(this, ScopeFlags); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 838 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 839 | // Parse the condition. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 840 | ExprResult Cond; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 841 | Decl *CondVar = 0; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 842 | if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true)) |
Chris Lattner | c0081db | 2008-12-12 06:31:07 +0000 | [diff] [blame] | 843 | return StmtError(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 844 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 845 | FullExprArg FullCond(Actions.MakeFullExpr(Cond.get())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 846 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 847 | // C99 6.8.5p5 - 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] | 848 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 849 | // 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] | 850 | // |
| 851 | // C++ 6.5p2: |
| 852 | // The substatement in an iteration-statement implicitly defines a local scope |
| 853 | // which is entered and exited each time through the loop. |
| 854 | // |
| 855 | // See comments in ParseIfStatement for why we create a scope for the |
| 856 | // condition and a new scope for substatement in C++. |
| 857 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | ParseScope InnerScope(this, Scope::DeclScope, |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 859 | C99orCXX && Tok.isNot(tok::l_brace)); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 860 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 861 | // Read the body statement. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 862 | StmtResult Body(ParseStatement()); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 863 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 864 | // Pop the body scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 865 | InnerScope.Exit(); |
| 866 | WhileScope.Exit(); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 867 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 868 | if ((Cond.isInvalid() && !CondVar) || Body.isInvalid()) |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 869 | return StmtError(); |
| 870 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 871 | return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /// ParseDoStatement |
| 875 | /// do-statement: [C99 6.8.5.2] |
| 876 | /// 'do' statement 'while' '(' expression ')' ';' |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 877 | /// Note: this lets the caller parse the end ';'. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 878 | StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 879 | // FIXME: Use attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 880 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 881 | assert(Tok.is(tok::kw_do) && "Not a do stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 882 | SourceLocation DoLoc = ConsumeToken(); // eat the 'do'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 883 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 884 | // C99 6.8.5p5 - In C99, the do statement is a block. This is not |
| 885 | // the case for C90. Start the loop scope. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 886 | unsigned ScopeFlags; |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 887 | if (getLang().C99) |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 888 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope; |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 889 | else |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 890 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 891 | |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 892 | ParseScope DoScope(this, ScopeFlags); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 893 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 894 | // C99 6.8.5p5 - 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] | 895 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 896 | // 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] | 897 | // |
| 898 | // C++ 6.5p2: |
| 899 | // The substatement in an iteration-statement implicitly defines a local scope |
| 900 | // which is entered and exited each time through the loop. |
| 901 | // |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 902 | ParseScope InnerScope(this, Scope::DeclScope, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | (getLang().C99 || getLang().CPlusPlus) && |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 904 | Tok.isNot(tok::l_brace)); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 905 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 906 | // Read the body statement. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 907 | StmtResult Body(ParseStatement()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 908 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 909 | // Pop the body scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 910 | InnerScope.Exit(); |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 911 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 912 | if (Tok.isNot(tok::kw_while)) { |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 913 | if (!Body.isInvalid()) { |
Chris Lattner | 0046de1 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 914 | Diag(Tok, diag::err_expected_while); |
Chris Lattner | 03c4041 | 2008-11-23 23:17:07 +0000 | [diff] [blame] | 915 | Diag(DoLoc, diag::note_matching) << "do"; |
Chris Lattner | 0046de1 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 916 | SkipUntil(tok::semi, false, true); |
| 917 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 918 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 919 | } |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 920 | SourceLocation WhileLoc = ConsumeToken(); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 921 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 922 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 923 | Diag(Tok, diag::err_expected_lparen_after) << "do/while"; |
Chris Lattner | 0046de1 | 2008-11-13 18:52:53 +0000 | [diff] [blame] | 924 | SkipUntil(tok::semi, false, true); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 925 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 926 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 927 | |
Chris Lattner | 10da53c | 2008-12-12 06:35:28 +0000 | [diff] [blame] | 928 | // Parse the parenthesized condition. |
Douglas Gregor | 7f800f9 | 2009-11-24 21:34:32 +0000 | [diff] [blame] | 929 | SourceLocation LPLoc = ConsumeParen(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 930 | ExprResult Cond = ParseExpression(); |
Douglas Gregor | 7f800f9 | 2009-11-24 21:34:32 +0000 | [diff] [blame] | 931 | SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc); |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 932 | DoScope.Exit(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 933 | |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 934 | if (Cond.isInvalid() || Body.isInvalid()) |
| 935 | return StmtError(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 936 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 937 | return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc, |
| 938 | Cond.get(), RPLoc); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /// ParseForStatement |
| 942 | /// for-statement: [C99 6.8.5.3] |
| 943 | /// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement |
| 944 | /// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 945 | /// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')' |
| 946 | /// [C++] statement |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 947 | /// [OBJC2] 'for' '(' declaration 'in' expr ')' statement |
| 948 | /// [OBJC2] 'for' '(' expr 'in' expr ')' statement |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 949 | /// |
| 950 | /// [C++] for-init-statement: |
| 951 | /// [C++] expression-statement |
| 952 | /// [C++] simple-declaration |
| 953 | /// |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 954 | StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 955 | // FIXME: Use attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 956 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 957 | assert(Tok.is(tok::kw_for) && "Not a for stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 958 | SourceLocation ForLoc = ConsumeToken(); // eat the 'for'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 959 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 960 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 961 | Diag(Tok, diag::err_expected_lparen_after) << "for"; |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 962 | SkipUntil(tok::semi); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 963 | return StmtError(); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 964 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 965 | |
Chris Lattner | 934074c | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 966 | bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1; |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 967 | |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 968 | // C99 6.8.5p5 - In C99, the for statement is a block. This is not |
| 969 | // the case for C90. Start the loop scope. |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 970 | // |
| 971 | // C++ 6.4p3: |
| 972 | // A name introduced by a declaration in a condition is in scope from its |
| 973 | // point of declaration until the end of the substatements controlled by the |
| 974 | // condition. |
Argyrios Kyrtzidis | 47f9865 | 2008-09-11 23:08:39 +0000 | [diff] [blame] | 975 | // C++ 3.3.2p4: |
| 976 | // Names declared in the for-init-statement, and in the condition of if, |
| 977 | // while, for, and switch statements are local to the if, while, for, or |
| 978 | // switch statement (including the controlled statement). |
Argyrios Kyrtzidis | 504bb84 | 2008-09-11 03:06:46 +0000 | [diff] [blame] | 979 | // C++ 6.5.3p1: |
| 980 | // Names declared in the for-init-statement are in the same declarative-region |
| 981 | // as those declared in the condition. |
| 982 | // |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 983 | unsigned ScopeFlags; |
Chris Lattner | 934074c | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 984 | if (C99orCXXorObjC) |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 985 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | |
| 986 | Scope::DeclScope | Scope::ControlScope; |
Chris Lattner | 2dd1b72 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 987 | else |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 988 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
| 989 | |
| 990 | ParseScope ForScope(this, ScopeFlags); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 991 | |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 992 | SourceLocation LParenLoc = ConsumeParen(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 993 | ExprResult Value; |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 994 | |
Fariborz Jahanian | e908cab | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 995 | bool ForEach = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 996 | StmtResult FirstPart; |
Douglas Gregor | 12cc7ee | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 997 | bool SecondPartIsInvalid = false; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 998 | FullExprArg SecondPart(Actions); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 999 | ExprResult Collection; |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1000 | FullExprArg ThirdPart(Actions); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1001 | Decl *SecondVar = 0; |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1002 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1003 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1004 | Actions.CodeCompleteOrdinaryName(getCurScope(), |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1005 | C99orCXXorObjC? Sema::PCC_ForInit |
| 1006 | : Sema::PCC_Expression); |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 1007 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1010 | // Parse the first part of the for specifier. |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1011 | if (Tok.is(tok::semi)) { // for (; |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1012 | // no first part, eat the ';'. |
| 1013 | ConsumeToken(); |
Argyrios Kyrtzidis | bc28fef | 2008-10-05 15:50:46 +0000 | [diff] [blame] | 1014 | } else if (isSimpleDeclaration()) { // for (int X = 4; |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1015 | // Parse declaration, which eats the ';'. |
Chris Lattner | 934074c | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 1016 | if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode? |
Chris Lattner | ab180365 | 2006-08-10 05:22:36 +0000 | [diff] [blame] | 1017 | Diag(Tok, diag::ext_c99_variable_decl_in_for_loop); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1018 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1019 | ParsedAttributesWithRange attrs; |
| 1020 | MaybeParseCXX0XAttributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1021 | |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1022 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1023 | StmtVector Stmts(Actions); |
| 1024 | DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1025 | DeclEnd, attrs, false); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1026 | FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1028 | if (Tok.is(tok::semi)) { // for (int x = 4; |
| 1029 | ConsumeToken(); |
| 1030 | } else if ((ForEach = isTokIdentifier_in())) { |
Fariborz Jahanian | e774fa6 | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 1031 | Actions.ActOnForEachDeclStmt(DG); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | // ObjC: for (id x in expr) |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1033 | ConsumeToken(); // consume 'in' |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1034 | |
| 1035 | if (Tok.is(tok::code_completion)) { |
| 1036 | Actions.CodeCompleteObjCForCollection(getCurScope(), DG); |
| 1037 | ConsumeCodeCompletionToken(); |
| 1038 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1039 | Collection = ParseExpression(); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1040 | } else { |
| 1041 | Diag(Tok, diag::err_expected_semi_for); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1042 | } |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1043 | } else { |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 1044 | Value = ParseExpression(); |
Chris Lattner | 71e23ce | 2006-11-04 20:18:38 +0000 | [diff] [blame] | 1045 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1046 | ForEach = isTokIdentifier_in(); |
| 1047 | |
Chris Lattner | cd68f64 | 2007-06-27 01:06:29 +0000 | [diff] [blame] | 1048 | // Turn the expression into a stmt. |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1049 | if (!Value.isInvalid()) { |
| 1050 | if (ForEach) |
| 1051 | FirstPart = Actions.ActOnForEachLValueExpr(Value.get()); |
| 1052 | else |
| 1053 | FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get())); |
| 1054 | } |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1056 | if (Tok.is(tok::semi)) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1057 | ConsumeToken(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1058 | } else if (ForEach) { |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1059 | ConsumeToken(); // consume 'in' |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1060 | |
| 1061 | if (Tok.is(tok::code_completion)) { |
| 1062 | Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy()); |
| 1063 | ConsumeCodeCompletionToken(); |
| 1064 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1065 | Collection = ParseExpression(); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1066 | } else { |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame^] | 1067 | if (!Value.isInvalid()) { |
| 1068 | Diag(Tok, diag::err_expected_semi_for); |
| 1069 | } else { |
| 1070 | // Skip until semicolon or rparen, don't consume it. |
| 1071 | SkipUntil(tok::r_paren, true, true); |
| 1072 | if (Tok.is(tok::semi)) |
| 1073 | ConsumeToken(); |
| 1074 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1075 | } |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1076 | } |
Fariborz Jahanian | e908cab | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 1077 | if (!ForEach) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1078 | assert(!SecondPart.get() && "Shouldn't have a second expression yet."); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1079 | // Parse the second part of the for specifier. |
| 1080 | if (Tok.is(tok::semi)) { // for (...;; |
| 1081 | // no second part. |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame^] | 1082 | } else if (Tok.is(tok::r_paren)) { |
| 1083 | // missing both semicolons. |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1084 | } else { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1085 | ExprResult Second; |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1086 | if (getLang().CPlusPlus) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1087 | ParseCXXCondition(Second, SecondVar, ForLoc, true); |
| 1088 | else { |
| 1089 | Second = ParseExpression(); |
| 1090 | if (!Second.isInvalid()) |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1091 | Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1092 | Second.get()); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1093 | } |
Douglas Gregor | 12cc7ee | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 1094 | SecondPartIsInvalid = Second.isInvalid(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1095 | SecondPart = Actions.MakeFullExpr(Second.get()); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1096 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1097 | |
Douglas Gregor | 230a7e6 | 2011-02-17 03:38:46 +0000 | [diff] [blame^] | 1098 | if (Tok.isNot(tok::semi)) { |
| 1099 | if (!SecondPartIsInvalid || SecondVar) |
| 1100 | Diag(Tok, diag::err_expected_semi_for); |
| 1101 | else |
| 1102 | // Skip until semicolon or rparen, don't consume it. |
| 1103 | SkipUntil(tok::r_paren, true, true); |
| 1104 | } |
| 1105 | |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1106 | if (Tok.is(tok::semi)) { |
| 1107 | ConsumeToken(); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1108 | } |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1109 | |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1110 | // Parse the third part of the for specifier. |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1111 | if (Tok.isNot(tok::r_paren)) { // for (...;...;) |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1112 | ExprResult Third = ParseExpression(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1113 | ThirdPart = Actions.MakeFullExpr(Third.take()); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1114 | } |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1115 | } |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 1116 | // Match the ')'. |
Chris Lattner | 71e23ce | 2006-11-04 20:18:38 +0000 | [diff] [blame] | 1117 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1118 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1119 | // C99 6.8.5p5 - 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] | 1120 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 1121 | // 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] | 1122 | // |
| 1123 | // C++ 6.5p2: |
| 1124 | // The substatement in an iteration-statement implicitly defines a local scope |
| 1125 | // which is entered and exited each time through the loop. |
| 1126 | // |
| 1127 | // See comments in ParseIfStatement for why we create a scope for |
| 1128 | // for-init-statement/condition and a new scope for substatement in C++. |
| 1129 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | ParseScope InnerScope(this, Scope::DeclScope, |
Chris Lattner | 934074c | 2009-04-22 00:54:41 +0000 | [diff] [blame] | 1131 | C99orCXXorObjC && Tok.isNot(tok::l_brace)); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1133 | // Read the body statement. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1134 | StmtResult Body(ParseStatement()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1135 | |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1136 | // Pop the body scope if needed. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1137 | InnerScope.Exit(); |
Chris Lattner | 8fb2625 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1139 | // Leave the for-scope. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1140 | ForScope.Exit(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1141 | |
| 1142 | if (Body.isInvalid()) |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1143 | return StmtError(); |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1144 | |
| 1145 | if (!ForEach) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1146 | return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart, |
| 1147 | SecondVar, ThirdPart, RParenLoc, Body.take()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1149 | // FIXME: It isn't clear how to communicate the late destruction of |
| 1150 | // C++ temporaries used to create the collection. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1151 | return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart.take(), |
| 1152 | Collection.take(), RParenLoc, |
| 1153 | Body.take()); |
Chris Lattner | 9075bd7 | 2006-08-10 04:59:57 +0000 | [diff] [blame] | 1154 | } |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1156 | /// ParseGotoStatement |
| 1157 | /// jump-statement: |
| 1158 | /// 'goto' identifier ';' |
| 1159 | /// [GNU] 'goto' '*' expression ';' |
| 1160 | /// |
| 1161 | /// Note: this lets the caller parse the end ';'. |
| 1162 | /// |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1163 | StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1164 | // FIXME: Use attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1165 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1166 | assert(Tok.is(tok::kw_goto) && "Not a goto stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1167 | SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1168 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1169 | StmtResult Res; |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1170 | if (Tok.is(tok::identifier)) { |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1171 | Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 1172 | Tok.getIdentifierInfo()); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1173 | ConsumeToken(); |
Eli Friedman | 5d72d41 | 2009-04-28 00:51:18 +0000 | [diff] [blame] | 1174 | } else if (Tok.is(tok::star)) { |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1175 | // GNU indirect goto extension. |
| 1176 | Diag(Tok, diag::ext_gnu_indirect_goto); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1177 | SourceLocation StarLoc = ConsumeToken(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1178 | ExprResult R(ParseExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1179 | if (R.isInvalid()) { // Skip to the semicolon, but don't consume it. |
Chris Lattner | a0927ce | 2006-08-12 16:59:03 +0000 | [diff] [blame] | 1180 | SkipUntil(tok::semi, false, true); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1181 | return StmtError(); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1182 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1183 | Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take()); |
Chris Lattner | e34b2c2 | 2007-07-22 04:13:33 +0000 | [diff] [blame] | 1184 | } else { |
| 1185 | Diag(Tok, diag::err_expected_ident); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1186 | return StmtError(); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1187 | } |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1188 | |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1189 | return move(Res); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1192 | /// ParseContinueStatement |
| 1193 | /// jump-statement: |
| 1194 | /// 'continue' ';' |
| 1195 | /// |
| 1196 | /// Note: this lets the caller parse the end ';'. |
| 1197 | /// |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1198 | StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1199 | // FIXME: Use attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1200 | |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1201 | SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1202 | return Actions.ActOnContinueStmt(ContinueLoc, getCurScope()); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | /// ParseBreakStatement |
| 1206 | /// jump-statement: |
| 1207 | /// 'break' ';' |
| 1208 | /// |
| 1209 | /// Note: this lets the caller parse the end ';'. |
| 1210 | /// |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1211 | StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1212 | // FIXME: Use attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1214 | SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1215 | return Actions.ActOnBreakStmt(BreakLoc, getCurScope()); |
Chris Lattner | 33ad2ca | 2006-11-05 23:47:55 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1218 | /// ParseReturnStatement |
| 1219 | /// jump-statement: |
| 1220 | /// 'return' expression[opt] ';' |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1221 | StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1222 | // FIXME: Use attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1223 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1224 | assert(Tok.is(tok::kw_return) && "Not a return stmt!"); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 1225 | SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'. |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1226 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1227 | ExprResult R; |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1228 | if (Tok.isNot(tok::semi)) { |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1229 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1230 | Actions.CodeCompleteReturn(getCurScope()); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1231 | ConsumeCodeCompletionToken(); |
| 1232 | SkipUntil(tok::semi, false, true); |
| 1233 | return StmtError(); |
| 1234 | } |
| 1235 | |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1236 | R = ParseExpression(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1237 | if (R.isInvalid()) { // Skip to the semicolon, but don't consume it. |
Chris Lattner | a0927ce | 2006-08-12 16:59:03 +0000 | [diff] [blame] | 1238 | SkipUntil(tok::semi, false, true); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1239 | return StmtError(); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 1240 | } |
Chris Lattner | a0927ce | 2006-08-12 16:59:03 +0000 | [diff] [blame] | 1241 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1242 | return Actions.ActOnReturnStmt(ReturnLoc, R.take()); |
Chris Lattner | 503fadc | 2006-08-10 05:45:44 +0000 | [diff] [blame] | 1243 | } |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1244 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1245 | /// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this |
| 1246 | /// routine is called to skip/ignore tokens that comprise the MS asm statement. |
Abramo Bagnara | e0acd85 | 2010-12-02 18:34:55 +0000 | [diff] [blame] | 1247 | StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) { |
| 1248 | SourceLocation EndLoc; |
Steve Naroff | 4e79d34 | 2008-02-07 23:24:32 +0000 | [diff] [blame] | 1249 | if (Tok.is(tok::l_brace)) { |
| 1250 | unsigned short savedBraceCount = BraceCount; |
| 1251 | do { |
Abramo Bagnara | e0acd85 | 2010-12-02 18:34:55 +0000 | [diff] [blame] | 1252 | EndLoc = Tok.getLocation(); |
Steve Naroff | 4e79d34 | 2008-02-07 23:24:32 +0000 | [diff] [blame] | 1253 | ConsumeAnyToken(); |
| 1254 | } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | } else { |
Steve Naroff | 4e79d34 | 2008-02-07 23:24:32 +0000 | [diff] [blame] | 1256 | // From the MS website: If used without braces, the __asm keyword means |
| 1257 | // that the rest of the line is an assembly-language statement. |
| 1258 | SourceManager &SrcMgr = PP.getSourceManager(); |
Steve Naroff | db5f7d7 | 2008-02-08 03:36:19 +0000 | [diff] [blame] | 1259 | SourceLocation TokLoc = Tok.getLocation(); |
Chris Lattner | 8a42586 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 1260 | unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc); |
Steve Naroff | 8c099c3 | 2008-02-08 18:01:27 +0000 | [diff] [blame] | 1261 | do { |
Abramo Bagnara | e0acd85 | 2010-12-02 18:34:55 +0000 | [diff] [blame] | 1262 | EndLoc = TokLoc; |
Steve Naroff | 8c099c3 | 2008-02-08 18:01:27 +0000 | [diff] [blame] | 1263 | ConsumeAnyToken(); |
| 1264 | TokLoc = Tok.getLocation(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) && |
| 1266 | Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) && |
Steve Naroff | 8c099c3 | 2008-02-08 18:01:27 +0000 | [diff] [blame] | 1267 | Tok.isNot(tok::eof)); |
Steve Naroff | 4e79d34 | 2008-02-07 23:24:32 +0000 | [diff] [blame] | 1268 | } |
Mike Stump | 6da5d75 | 2009-12-11 00:04:56 +0000 | [diff] [blame] | 1269 | Token t; |
| 1270 | t.setKind(tok::string_literal); |
Chris Lattner | d28e6cc | 2010-08-17 16:00:12 +0000 | [diff] [blame] | 1271 | t.setLiteralData("\"/*FIXME: not done*/\""); |
Mike Stump | 6da5d75 | 2009-12-11 00:04:56 +0000 | [diff] [blame] | 1272 | t.clearFlag(Token::NeedsCleaning); |
Chris Lattner | d28e6cc | 2010-08-17 16:00:12 +0000 | [diff] [blame] | 1273 | t.setLength(21); |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1274 | ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1)); |
Mike Stump | 6da5d75 | 2009-12-11 00:04:56 +0000 | [diff] [blame] | 1275 | ExprVector Constraints(Actions); |
| 1276 | ExprVector Exprs(Actions); |
| 1277 | ExprVector Clobbers(Actions); |
Abramo Bagnara | e0acd85 | 2010-12-02 18:34:55 +0000 | [diff] [blame] | 1278 | return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0, |
Mike Stump | 6da5d75 | 2009-12-11 00:04:56 +0000 | [diff] [blame] | 1279 | move_arg(Constraints), move_arg(Exprs), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1280 | AsmString.take(), move_arg(Clobbers), |
Abramo Bagnara | e0acd85 | 2010-12-02 18:34:55 +0000 | [diff] [blame] | 1281 | EndLoc, true); |
Steve Naroff | b2c80c7 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1284 | /// ParseAsmStatement - Parse a GNU extended asm statement. |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1285 | /// asm-statement: |
| 1286 | /// gnu-asm-statement |
| 1287 | /// ms-asm-statement |
| 1288 | /// |
| 1289 | /// [GNU] gnu-asm-statement: |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1290 | /// 'asm' type-qualifier[opt] '(' asm-argument ')' ';' |
| 1291 | /// |
| 1292 | /// [GNU] asm-argument: |
| 1293 | /// asm-string-literal |
| 1294 | /// asm-string-literal ':' asm-operands[opt] |
| 1295 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 1296 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 1297 | /// ':' asm-clobbers |
| 1298 | /// |
| 1299 | /// [GNU] asm-clobbers: |
| 1300 | /// asm-string-literal |
| 1301 | /// asm-clobbers ',' asm-string-literal |
| 1302 | /// |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1303 | /// [MS] ms-asm-statement: |
| 1304 | /// '__asm' assembly-instruction ';'[opt] |
| 1305 | /// '__asm' '{' assembly-instruction-list '}' ';'[opt] |
| 1306 | /// |
| 1307 | /// [MS] assembly-instruction-list: |
| 1308 | /// assembly-instruction ';'[opt] |
| 1309 | /// assembly-instruction-list ';' assembly-instruction ';'[opt] |
| 1310 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1311 | StmtResult Parser::ParseAsmStatement(bool &msAsm) { |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1312 | assert(Tok.is(tok::kw_asm) && "Not an asm stmt"); |
Chris Lattner | 73c56c0 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 1313 | SourceLocation AsmLoc = ConsumeToken(); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1314 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1315 | if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) { |
Steve Naroff | b2c80c7 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 1316 | msAsm = true; |
Abramo Bagnara | e0acd85 | 2010-12-02 18:34:55 +0000 | [diff] [blame] | 1317 | return FuzzyParseMicrosoftAsmStatement(AsmLoc); |
Steve Naroff | b2c80c7 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 1318 | } |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1319 | DeclSpec DS; |
| 1320 | SourceLocation Loc = Tok.getLocation(); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1321 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1322 | |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1323 | // GNU asms accept, but warn, about type-qualifiers other than volatile. |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 1324 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1325 | Diag(Loc, diag::w_asm_qualifier_ignored) << "const"; |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 1326 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1327 | Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict"; |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1328 | |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1329 | // Remember if this was a volatile asm. |
Anders Carlsson | 660bdd1 | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 1330 | bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile; |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1331 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1332 | Diag(Tok, diag::err_expected_lparen_after) << "asm"; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1333 | SkipUntil(tok::r_paren); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1334 | return StmtError(); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1335 | } |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 1336 | Loc = ConsumeParen(); |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1337 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1338 | ExprResult AsmString(ParseAsmStringLiteral()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1339 | if (AsmString.isInvalid()) |
Sebastian Redl | b62406f | 2008-12-11 19:48:14 +0000 | [diff] [blame] | 1340 | return StmtError(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1341 | |
Anders Carlsson | 9a020f9 | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 1342 | llvm::SmallVector<IdentifierInfo *, 4> Names; |
Sebastian Redl | 511ed55 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1343 | ExprVector Constraints(Actions); |
| 1344 | ExprVector Exprs(Actions); |
| 1345 | ExprVector Clobbers(Actions); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1346 | |
Anders Carlsson | 19fe116 | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 1347 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1348 | // We have a simple asm expression like 'asm("foo")'. |
| 1349 | SourceLocation RParenLoc = ConsumeParen(); |
| 1350 | return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile, |
| 1351 | /*NumOutputs*/ 0, /*NumInputs*/ 0, 0, |
| 1352 | move_arg(Constraints), move_arg(Exprs), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1353 | AsmString.take(), move_arg(Clobbers), |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1354 | RParenLoc); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1355 | } |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1356 | |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1357 | // Parse Outputs, if present. |
Chris Lattner | 1576850 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1358 | bool AteExtraColon = false; |
| 1359 | if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) { |
| 1360 | // In C++ mode, parse "::" like ": :". |
| 1361 | AteExtraColon = Tok.is(tok::coloncolon); |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1362 | ConsumeToken(); |
| 1363 | |
Chris Lattner | 1576850 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1364 | if (!AteExtraColon && |
| 1365 | ParseAsmOperandsOpt(Names, Constraints, Exprs)) |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1366 | return StmtError(); |
| 1367 | } |
Chris Lattner | 1576850 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1368 | |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1369 | unsigned NumOutputs = Names.size(); |
| 1370 | |
| 1371 | // Parse Inputs, if present. |
Chris Lattner | 1576850 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1372 | if (AteExtraColon || |
| 1373 | Tok.is(tok::colon) || Tok.is(tok::coloncolon)) { |
| 1374 | // In C++ mode, parse "::" like ": :". |
| 1375 | if (AteExtraColon) |
| 1376 | AteExtraColon = false; |
| 1377 | else { |
| 1378 | AteExtraColon = Tok.is(tok::coloncolon); |
| 1379 | ConsumeToken(); |
| 1380 | } |
| 1381 | |
| 1382 | if (!AteExtraColon && |
| 1383 | ParseAsmOperandsOpt(Names, Constraints, Exprs)) |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1384 | return StmtError(); |
| 1385 | } |
| 1386 | |
| 1387 | assert(Names.size() == Constraints.size() && |
| 1388 | Constraints.size() == Exprs.size() && |
| 1389 | "Input operand size mismatch!"); |
| 1390 | |
| 1391 | unsigned NumInputs = Names.size() - NumOutputs; |
| 1392 | |
| 1393 | // Parse the clobbers, if present. |
Chris Lattner | 1576850 | 2009-12-20 23:08:04 +0000 | [diff] [blame] | 1394 | if (AteExtraColon || Tok.is(tok::colon)) { |
| 1395 | if (!AteExtraColon) |
| 1396 | ConsumeToken(); |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1397 | |
Chandler Carruth | 3c31aa3 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1398 | // Parse the asm-string list for clobbers if present. |
| 1399 | if (Tok.isNot(tok::r_paren)) { |
| 1400 | while (1) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1401 | ExprResult Clobber(ParseAsmStringLiteral()); |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1402 | |
Chandler Carruth | 3c31aa3 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1403 | if (Clobber.isInvalid()) |
| 1404 | break; |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1405 | |
Chandler Carruth | 3c31aa3 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1406 | Clobbers.push_back(Clobber.release()); |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1407 | |
Chandler Carruth | 3c31aa3 | 2010-07-22 07:11:21 +0000 | [diff] [blame] | 1408 | if (Tok.isNot(tok::comma)) break; |
| 1409 | ConsumeToken(); |
| 1410 | } |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc); |
| 1415 | return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile, |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1416 | NumOutputs, NumInputs, Names.data(), |
Sebastian Redl | c2edafb | 2009-01-18 18:03:53 +0000 | [diff] [blame] | 1417 | move_arg(Constraints), move_arg(Exprs), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1418 | AsmString.take(), move_arg(Clobbers), |
Sebastian Redl | 24b8e15 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1419 | RParenLoc); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | /// ParseAsmOperands - Parse the asm-operands production as used by |
Chris Lattner | bf5fff5 | 2009-12-20 23:00:41 +0000 | [diff] [blame] | 1423 | /// asm-statement, assuming the leading ':' token was eaten. |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1424 | /// |
| 1425 | /// [GNU] asm-operands: |
| 1426 | /// asm-operand |
| 1427 | /// asm-operands ',' asm-operand |
| 1428 | /// |
| 1429 | /// [GNU] asm-operand: |
| 1430 | /// asm-string-literal '(' expression ')' |
| 1431 | /// '[' identifier ']' asm-string-literal '(' expression ')' |
| 1432 | /// |
Daniel Dunbar | 70e7ead | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 1433 | // |
| 1434 | // FIXME: Avoid unnecessary std::string trashing. |
Anders Carlsson | 9a020f9 | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 1435 | bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names, |
| 1436 | llvm::SmallVectorImpl<ExprTy *> &Constraints, |
| 1437 | llvm::SmallVectorImpl<ExprTy *> &Exprs) { |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1438 | // 'asm-operands' isn't present? |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1439 | if (!isTokenStringLiteral() && Tok.isNot(tok::l_square)) |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1440 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | |
| 1442 | while (1) { |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1443 | // Read the [id] if present. |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1444 | if (Tok.is(tok::l_square)) { |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 1445 | SourceLocation Loc = ConsumeBracket(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1447 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1448 | Diag(Tok, diag::err_expected_ident); |
| 1449 | SkipUntil(tok::r_paren); |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1450 | return true; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1451 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | |
Anders Carlsson | 94ea8aa | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1453 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | 645ff3f | 2007-10-29 04:06:22 +0000 | [diff] [blame] | 1454 | ConsumeToken(); |
Anders Carlsson | 94ea8aa | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1455 | |
Anders Carlsson | 9a020f9 | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 1456 | Names.push_back(II); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1457 | MatchRHSPunctuation(tok::r_square, Loc); |
Anders Carlsson | 94ea8aa | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1458 | } else |
Anders Carlsson | 9a020f9 | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 1459 | Names.push_back(0); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1460 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1461 | ExprResult Constraint(ParseAsmStringLiteral()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1462 | if (Constraint.isInvalid()) { |
Anders Carlsson | 94ea8aa | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1463 | SkipUntil(tok::r_paren); |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1464 | return true; |
Anders Carlsson | 94ea8aa | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 1465 | } |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1466 | Constraints.push_back(Constraint.release()); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1467 | |
Chris Lattner | feb00b6 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1468 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1469 | Diag(Tok, diag::err_expected_lparen_after) << "asm operand"; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1470 | SkipUntil(tok::r_paren); |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1471 | return true; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1472 | } |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1473 | |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1474 | // Read the parenthesized expression. |
Eli Friedman | 47e7857 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 1475 | SourceLocation OpenLoc = ConsumeParen(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1476 | ExprResult Res(ParseExpression()); |
Eli Friedman | 47e7857 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 1477 | MatchRHSPunctuation(tok::r_paren, OpenLoc); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1478 | if (Res.isInvalid()) { |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1479 | SkipUntil(tok::r_paren); |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1480 | return true; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1481 | } |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1482 | Exprs.push_back(Res.release()); |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1483 | // Eat the comma and continue parsing if it exists. |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1484 | if (Tok.isNot(tok::comma)) return false; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1485 | ConsumeToken(); |
| 1486 | } |
Anders Carlsson | 2e64d1a | 2008-02-09 19:57:29 +0000 | [diff] [blame] | 1487 | |
| 1488 | return true; |
Chris Lattner | 0116c47 | 2006-08-15 06:03:28 +0000 | [diff] [blame] | 1489 | } |
Fariborz Jahanian | 8e63294 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 1490 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1491 | Decl *Parser::ParseFunctionStatementBody(Decl *Decl) { |
Chris Lattner | 12f2ea5 | 2009-03-05 00:49:17 +0000 | [diff] [blame] | 1492 | assert(Tok.is(tok::l_brace)); |
| 1493 | SourceLocation LBraceLoc = Tok.getLocation(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1494 | |
Argyrios Kyrtzidis | bd9dfb2 | 2011-01-04 00:27:27 +0000 | [diff] [blame] | 1495 | if (PP.isCodeCompletionEnabled()) |
| 1496 | if (trySkippingFunctionBodyForCodeCompletion()) |
| 1497 | return Actions.ActOnFinishFunctionBody(Decl, 0); |
Argyrios Kyrtzidis | 76dbe8c | 2011-01-03 19:44:02 +0000 | [diff] [blame] | 1498 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1499 | PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc, |
| 1500 | "parsing function body"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Fariborz Jahanian | 8e63294 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 1502 | // Do not enter a scope for the brace, as the arguments are in the same scope |
| 1503 | // (the function body) as the body itself. Instead, just read the statement |
| 1504 | // list and put it into a CompoundStmt for safe keeping. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1505 | StmtResult FnBody(ParseCompoundStatementBody()); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1506 | |
Fariborz Jahanian | 8e63294 | 2007-11-08 19:01:26 +0000 | [diff] [blame] | 1507 | // If the function body could not be parsed, make a bogus compoundstmt. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1508 | if (FnBody.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, |
Chris Lattner | 12f2ea5 | 2009-03-05 00:49:17 +0000 | [diff] [blame] | 1510 | MultiStmtArg(Actions), false); |
Sebastian Redl | 042ad95 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1511 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1512 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.take()); |
Seo Sanghyeon | 34f92ac | 2007-12-01 08:06:07 +0000 | [diff] [blame] | 1513 | } |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1514 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1515 | /// ParseFunctionTryBlock - Parse a C++ function-try-block. |
| 1516 | /// |
| 1517 | /// function-try-block: |
| 1518 | /// 'try' ctor-initializer[opt] compound-statement handler-seq |
| 1519 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1520 | Decl *Parser::ParseFunctionTryBlock(Decl *Decl) { |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1521 | assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
| 1522 | SourceLocation TryLoc = ConsumeToken(); |
| 1523 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1524 | PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc, |
| 1525 | "parsing function try block"); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1526 | |
| 1527 | // Constructor initializer list? |
| 1528 | if (Tok.is(tok::colon)) |
| 1529 | ParseConstructorInitializer(Decl); |
| 1530 | |
Argyrios Kyrtzidis | bd9dfb2 | 2011-01-04 00:27:27 +0000 | [diff] [blame] | 1531 | if (PP.isCodeCompletionEnabled()) |
| 1532 | if (trySkippingFunctionBodyForCodeCompletion()) |
| 1533 | return Actions.ActOnFinishFunctionBody(Decl, 0); |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 1534 | |
Sebastian Redl | d98ecd6 | 2009-04-26 21:08:36 +0000 | [diff] [blame] | 1535 | SourceLocation LBraceLoc = Tok.getLocation(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1536 | StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc)); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1537 | // If we failed to parse the try-catch, we just give the function an empty |
| 1538 | // compound statement as the body. |
| 1539 | if (FnBody.isInvalid()) |
Sebastian Redl | d98ecd6 | 2009-04-26 21:08:36 +0000 | [diff] [blame] | 1540 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1541 | MultiStmtArg(Actions), false); |
| 1542 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1543 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.take()); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Argyrios Kyrtzidis | bd9dfb2 | 2011-01-04 00:27:27 +0000 | [diff] [blame] | 1546 | bool Parser::trySkippingFunctionBodyForCodeCompletion() { |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 1547 | assert(Tok.is(tok::l_brace)); |
Argyrios Kyrtzidis | bd9dfb2 | 2011-01-04 00:27:27 +0000 | [diff] [blame] | 1548 | assert(PP.isCodeCompletionEnabled() && |
| 1549 | "Should only be called when in code-completion mode"); |
Argyrios Kyrtzidis | d5756a6 | 2011-01-03 22:33:06 +0000 | [diff] [blame] | 1550 | |
| 1551 | // We're in code-completion mode. Skip parsing for all function bodies unless |
| 1552 | // the body contains the code-completion point. |
| 1553 | TentativeParsingAction PA(*this); |
| 1554 | ConsumeBrace(); |
| 1555 | if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false, |
| 1556 | /*StopAtCodeCompletion=*/true)) { |
| 1557 | PA.Commit(); |
| 1558 | return true; |
| 1559 | } |
| 1560 | |
| 1561 | PA.Revert(); |
| 1562 | return false; |
| 1563 | } |
| 1564 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1565 | /// ParseCXXTryBlock - Parse a C++ try-block. |
| 1566 | /// |
| 1567 | /// try-block: |
| 1568 | /// 'try' compound-statement handler-seq |
| 1569 | /// |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1570 | StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1571 | // FIXME: Add attributes? |
Ted Kremenek | c162e8e | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1572 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1573 | assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
| 1574 | |
| 1575 | SourceLocation TryLoc = ConsumeToken(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1576 | return ParseCXXTryBlockCommon(TryLoc); |
| 1577 | } |
| 1578 | |
| 1579 | /// ParseCXXTryBlockCommon - Parse the common part of try-block and |
| 1580 | /// function-try-block. |
| 1581 | /// |
| 1582 | /// try-block: |
| 1583 | /// 'try' compound-statement handler-seq |
| 1584 | /// |
| 1585 | /// function-try-block: |
| 1586 | /// 'try' ctor-initializer[opt] compound-statement handler-seq |
| 1587 | /// |
| 1588 | /// handler-seq: |
| 1589 | /// handler handler-seq[opt] |
| 1590 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1591 | StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) { |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1592 | if (Tok.isNot(tok::l_brace)) |
| 1593 | return StmtError(Diag(Tok, diag::err_expected_lbrace)); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1594 | // FIXME: Possible draft standard bug: attribute-specifier should be allowed? |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1595 | ParsedAttributesWithRange attrs; |
| 1596 | StmtResult TryBlock(ParseCompoundStatement(attrs)); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1597 | if (TryBlock.isInvalid()) |
| 1598 | return move(TryBlock); |
| 1599 | |
| 1600 | StmtVector Handlers(Actions); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1601 | MaybeParseCXX0XAttributes(attrs); |
| 1602 | ProhibitAttributes(attrs); |
| 1603 | |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1604 | if (Tok.isNot(tok::kw_catch)) |
| 1605 | return StmtError(Diag(Tok, diag::err_expected_catch)); |
| 1606 | while (Tok.is(tok::kw_catch)) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1607 | StmtResult Handler(ParseCXXCatchBlock()); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1608 | if (!Handler.isInvalid()) |
| 1609 | Handlers.push_back(Handler.release()); |
| 1610 | } |
| 1611 | // Don't bother creating the full statement if we don't have any usable |
| 1612 | // handlers. |
| 1613 | if (Handlers.empty()) |
| 1614 | return StmtError(); |
| 1615 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1616 | return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers)); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | /// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard |
| 1620 | /// |
| 1621 | /// handler: |
| 1622 | /// 'catch' '(' exception-declaration ')' compound-statement |
| 1623 | /// |
| 1624 | /// exception-declaration: |
| 1625 | /// type-specifier-seq declarator |
| 1626 | /// type-specifier-seq abstract-declarator |
| 1627 | /// type-specifier-seq |
| 1628 | /// '...' |
| 1629 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1630 | StmtResult Parser::ParseCXXCatchBlock() { |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1631 | assert(Tok.is(tok::kw_catch) && "Expected 'catch'"); |
| 1632 | |
| 1633 | SourceLocation CatchLoc = ConsumeToken(); |
| 1634 | |
| 1635 | SourceLocation LParenLoc = Tok.getLocation(); |
| 1636 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen)) |
| 1637 | return StmtError(); |
| 1638 | |
| 1639 | // C++ 3.3.2p3: |
| 1640 | // The name in a catch exception-declaration is local to the handler and |
| 1641 | // shall not be redeclared in the outermost block of the handler. |
| 1642 | ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope); |
| 1643 | |
| 1644 | // exception-declaration is equivalent to '...' or a parameter-declaration |
| 1645 | // without default arguments. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1646 | Decl *ExceptionDecl = 0; |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1647 | if (Tok.isNot(tok::ellipsis)) { |
| 1648 | DeclSpec DS; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 1649 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 1650 | return StmtError(); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1651 | Declarator ExDecl(DS, Declarator::CXXCatchContext); |
| 1652 | ParseDeclarator(ExDecl); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1653 | ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1654 | } else |
| 1655 | ConsumeToken(); |
| 1656 | |
| 1657 | if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid()) |
| 1658 | return StmtError(); |
| 1659 | |
| 1660 | if (Tok.isNot(tok::l_brace)) |
| 1661 | return StmtError(Diag(Tok, diag::err_expected_lbrace)); |
| 1662 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1663 | // FIXME: Possible draft standard bug: attribute-specifier should be allowed? |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1664 | ParsedAttributes attrs; |
| 1665 | StmtResult Block(ParseCompoundStatement(attrs)); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1666 | if (Block.isInvalid()) |
| 1667 | return move(Block); |
| 1668 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1669 | return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take()); |
Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 1670 | } |