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