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