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