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