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