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