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